code
stringlengths
86
54.5k
code_codestyle
int64
0
371
style_context
stringlengths
87
49.2k
style_context_codestyle
int64
0
349
label
int64
0
1
from __future__ import annotations def a ( snake_case__: int , snake_case__: int ): '''simple docstring''' lowercase_ = [] create_all_state(1 , snake_case__ , snake_case__ , [] , snake_case__ ) return result def a ( snake_case__: int , snake_case__: int , snake_case__: int , snake_case__: list[int] , snake_case__: list[list[int]] , ): '''simple docstring''' if level == 0: total_list.append(current_list[:] ) return for i in range(snake_case__ , total_number - level + 2 ): current_list.append(snake_case__ ) create_all_state(i + 1 , snake_case__ , level - 1 , snake_case__ , snake_case__ ) current_list.pop() def a ( snake_case__: list[list[int]] ): '''simple docstring''' for i in total_list: print(*snake_case__ ) if __name__ == "__main__": __a = 4 __a = 2 __a = generate_all_combinations(n, k) print_all_state(total_list)
30
import unittest import numpy as np import torch from .utils_summarization import build_mask, compute_token_type_ids, process_story, truncate_or_pad class lowercase__( unittest.TestCase ): """simple docstring""" def _lowercase ( self : List[str] ) -> List[Any]: lowercase_ = 1_0 def _lowercase ( self : int ) -> List[str]: lowercase_ = [1, 2, 3, 4] lowercase_ = [1, 2, 3, 4, 0, 0, 0, 0, 0, 0] self.assertEqual(truncate_or_pad(SCREAMING_SNAKE_CASE_ , self.block_size , 0 ) , SCREAMING_SNAKE_CASE_ ) def _lowercase ( self : int ) -> Optional[Any]: lowercase_ = [1, 2, 3, 4, 5, 6, 7, 8, 9, 1_0] lowercase_ = [1, 2, 3, 4, 5, 6, 7, 8, 9, 1_0] self.assertEqual(truncate_or_pad(SCREAMING_SNAKE_CASE_ , self.block_size , 0 ) , SCREAMING_SNAKE_CASE_ ) def _lowercase ( self : Union[str, Any] ) -> Optional[int]: lowercase_ = [1, 2, 3, 4, 5, 6, 7, 8, 9, 1_0, 1_1, 1_2, 1_3] lowercase_ = [1, 2, 3, 4, 5, 6, 7, 8, 9, 1_0] self.assertEqual(truncate_or_pad(SCREAMING_SNAKE_CASE_ , self.block_size , 0 ) , SCREAMING_SNAKE_CASE_ ) def _lowercase ( self : Any ) -> List[Any]: lowercase_ = '''It was the year of Our Lord one thousand seven hundred and seventy-five.\n\nSpiritual revelations were conceded to England at that favoured period, as at this.''' lowercase_ , lowercase_ = process_story(SCREAMING_SNAKE_CASE_ ) self.assertEqual(SCREAMING_SNAKE_CASE_ , [] ) def _lowercase ( self : List[str] ) -> List[str]: lowercase_ = '''''' lowercase_ , lowercase_ = process_story(SCREAMING_SNAKE_CASE_ ) self.assertEqual(SCREAMING_SNAKE_CASE_ , [] ) self.assertEqual(SCREAMING_SNAKE_CASE_ , [] ) def _lowercase ( self : Union[str, Any] ) -> Union[str, Any]: lowercase_ = ( '''It was the year of Our Lord one thousand seven hundred and ''' '''seventy-five\n\nSpiritual revelations were conceded to England ''' '''at that favoured period, as at this.\n@highlight\n\nIt was the best of times''' ) lowercase_ , lowercase_ = process_story(SCREAMING_SNAKE_CASE_ ) lowercase_ = [ '''It was the year of Our Lord one thousand seven hundred and seventy-five.''', '''Spiritual revelations were conceded to England at that favoured period, as at this.''', ] self.assertEqual(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) lowercase_ = ['''It was the best of times.'''] self.assertEqual(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) def _lowercase ( self : Union[str, Any] ) -> Optional[Any]: lowercase_ = torch.tensor([1, 2, 3, 4] ) lowercase_ = torch.tensor([1, 1, 1, 1] ) np.testing.assert_array_equal(build_mask(SCREAMING_SNAKE_CASE_ , 0 ).numpy() , expected.numpy() ) def _lowercase ( self : List[Any] ) -> Tuple: lowercase_ = torch.tensor([1, 2, 3, 4, 2_3, 2_3, 2_3] ) lowercase_ = torch.tensor([1, 1, 1, 1, 0, 0, 0] ) np.testing.assert_array_equal(build_mask(SCREAMING_SNAKE_CASE_ , 2_3 ).numpy() , expected.numpy() ) def _lowercase ( self : int ) -> Dict: lowercase_ = torch.tensor([8, 2, 3, 4, 1, 1, 1] ) lowercase_ = torch.tensor([1, 1, 1, 1, 0, 0, 0] ) np.testing.assert_array_equal(build_mask(SCREAMING_SNAKE_CASE_ , 1 ).numpy() , expected.numpy() ) def _lowercase ( self : List[str] ) -> Tuple: lowercase_ = 1_0_1 lowercase_ = torch.tensor([[1, 2, 3, 4, 5, 6], [1, 2, 3, 1_0_1, 5, 6], [1, 1_0_1, 3, 4, 1_0_1, 6]] ) lowercase_ = torch.tensor([[1, 1, 1, 1, 1, 1], [1, 1, 1, 0, 0, 0], [1, 0, 0, 0, 1, 1]] ) lowercase_ = compute_token_type_ids(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) np.testing.assert_array_equal(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ )
30
1
"""simple docstring""" import unittest from transformers import CamembertTokenizer, CamembertTokenizerFast from transformers.testing_utils import get_tests_dir, require_sentencepiece, require_tokenizers, slow from transformers.utils import is_torch_available from ...test_tokenization_common import TokenizerTesterMixin _lowercase : List[Any] = get_tests_dir('fixtures/test_sentencepiece.model') _lowercase : int = get_tests_dir('fixtures/test_sentencepiece_bpe.model') _lowercase : Any = 'pt' if is_torch_available() else 'tf' @require_sentencepiece @require_tokenizers class _UpperCAmelCase ( _lowerCAmelCase , unittest.TestCase ): a__ : Optional[Any] = CamembertTokenizer a__ : List[Any] = CamembertTokenizerFast a__ : Optional[int] = True a__ : Any = True def a ( self : str ): super().setUp() # We have a SentencePiece fixture for testing __UpperCAmelCase = CamembertTokenizer(__UpperCAmelCase ) tokenizer.save_pretrained(self.tmpdirname ) def a ( self : Union[str, Any] ): __UpperCAmelCase = '''<pad>''' __UpperCAmelCase = 1 self.assertEqual(self.get_tokenizer()._convert_token_to_id(__UpperCAmelCase ) , __UpperCAmelCase ) self.assertEqual(self.get_tokenizer()._convert_id_to_token(__UpperCAmelCase ) , __UpperCAmelCase ) def a ( self : Union[str, Any] ): __UpperCAmelCase = list(self.get_tokenizer().get_vocab().keys() ) self.assertEqual(vocab_keys[0] , '''<s>NOTUSED''' ) self.assertEqual(vocab_keys[1] , '''<pad>''' ) self.assertEqual(vocab_keys[-1] , '''<mask>''' ) self.assertEqual(len(__UpperCAmelCase ) , 10_04 ) def a ( self : Tuple ): self.assertEqual(self.get_tokenizer().vocab_size , 10_05 ) def a ( self : List[str] ): __UpperCAmelCase = CamembertTokenizer(__UpperCAmelCase ) tokenizer.save_pretrained(self.tmpdirname ) __UpperCAmelCase = CamembertTokenizerFast.from_pretrained(self.tmpdirname ) __UpperCAmelCase = '''I was born in 92000, and this is falsé.''' __UpperCAmelCase = tokenizer.encode(__UpperCAmelCase ) __UpperCAmelCase = rust_tokenizer.encode(__UpperCAmelCase ) self.assertListEqual(__UpperCAmelCase , __UpperCAmelCase ) __UpperCAmelCase = tokenizer.encode(__UpperCAmelCase , add_special_tokens=__UpperCAmelCase ) __UpperCAmelCase = rust_tokenizer.encode(__UpperCAmelCase , add_special_tokens=__UpperCAmelCase ) self.assertListEqual(__UpperCAmelCase , __UpperCAmelCase ) # <unk> tokens are not the same for `rust` than for `slow`. # Because spm gives back raw token instead of `unk` in EncodeAsPieces # tokens = tokenizer.tokenize(sequence) __UpperCAmelCase = tokenizer.convert_ids_to_tokens(__UpperCAmelCase ) __UpperCAmelCase = rust_tokenizer.tokenize(__UpperCAmelCase ) self.assertListEqual(__UpperCAmelCase , __UpperCAmelCase ) def a ( self : List[str] ): if not self.test_rust_tokenizer: return __UpperCAmelCase = self.get_tokenizer() __UpperCAmelCase = self.get_rust_tokenizer() __UpperCAmelCase = '''I was born in 92000, and this is falsé.''' __UpperCAmelCase = tokenizer.tokenize(__UpperCAmelCase ) __UpperCAmelCase = rust_tokenizer.tokenize(__UpperCAmelCase ) self.assertListEqual(__UpperCAmelCase , __UpperCAmelCase ) __UpperCAmelCase = tokenizer.encode(__UpperCAmelCase , add_special_tokens=__UpperCAmelCase ) __UpperCAmelCase = rust_tokenizer.encode(__UpperCAmelCase , add_special_tokens=__UpperCAmelCase ) self.assertListEqual(__UpperCAmelCase , __UpperCAmelCase ) __UpperCAmelCase = self.get_rust_tokenizer() __UpperCAmelCase = tokenizer.encode(__UpperCAmelCase ) __UpperCAmelCase = rust_tokenizer.encode(__UpperCAmelCase ) self.assertListEqual(__UpperCAmelCase , __UpperCAmelCase ) @slow def a ( self : Tuple ): __UpperCAmelCase = {'''input_ids''': [[5, 54, 71_96, 2_97, 30, 23, 7_76, 18, 11, 32_15, 37_05, 82_52, 22, 31_64, 11_81, 21_16, 29, 16, 8_13, 25, 7_91, 33_14, 20, 34_46, 38, 2_75_75, 1_20, 6, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [5, 4_68, 17, 11, 90_88, 20, 15_17, 8, 2_28_04, 1_88_18, 10, 38, 6_29, 6_07, 6_07, 1_42, 19, 71_96, 8_67, 56, 1_03_26, 24, 22_67, 20, 4_16, 50_72, 1_56_12, 2_33, 7_34, 7, 23_99, 27, 16, 30_15, 16_49, 7, 24, 20, 43_38, 23_99, 27, 13, 34_00, 14, 13, 61_89, 8, 9_30, 9, 6]], '''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, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]} # noqa: E501 # fmt: on # camembert is a french model. So we also use french texts. __UpperCAmelCase = [ '''Le transformeur est un modèle d\'apprentissage profond introduit en 2017, ''' '''utilisé principalement dans le domaine du traitement automatique des langues (TAL).''', '''À l\'instar des réseaux de neurones récurrents (RNN), les transformeurs sont conçus ''' '''pour gérer des données séquentielles, telles que le langage naturel, pour des tâches ''' '''telles que la traduction et la synthèse de texte.''', ] self.tokenizer_integration_test_util( expected_encoding=__UpperCAmelCase , model_name='''camembert-base''' , revision='''3a0641d9a1aeb7e848a74299e7e4c4bca216b4cf''' , sequences=__UpperCAmelCase , )
354
"""simple docstring""" import gc import unittest from transformers import MODEL_FOR_MASKED_LM_MAPPING, TF_MODEL_FOR_MASKED_LM_MAPPING, FillMaskPipeline, pipeline from transformers.pipelines import PipelineException from transformers.testing_utils import ( is_pipeline_test, is_torch_available, nested_simplify, require_tf, require_torch, require_torch_gpu, slow, ) from .test_pipelines_common import ANY @is_pipeline_test class _UpperCAmelCase ( unittest.TestCase ): a__ : Optional[int] = MODEL_FOR_MASKED_LM_MAPPING a__ : Tuple = TF_MODEL_FOR_MASKED_LM_MAPPING def a ( self : List[str] ): super().tearDown() # clean-up as much as possible GPU memory occupied by PyTorch gc.collect() if is_torch_available(): import torch torch.cuda.empty_cache() @require_tf def a ( self : Tuple ): __UpperCAmelCase = pipeline(task='''fill-mask''' , model='''sshleifer/tiny-distilroberta-base''' , top_k=2 , framework='''tf''' ) __UpperCAmelCase = unmasker('''My name is <mask>''' ) self.assertEqual( nested_simplify(_lowercase , decimals=6 ) , [ {'''sequence''': '''My name is grouped''', '''score''': 2.1E-05, '''token''': 3_80_15, '''token_str''': ''' grouped'''}, {'''sequence''': '''My name is accuser''', '''score''': 2.1E-05, '''token''': 2_55_06, '''token_str''': ''' accuser'''}, ] , ) __UpperCAmelCase = unmasker('''The largest city in France is <mask>''' ) self.assertEqual( nested_simplify(_lowercase , decimals=6 ) , [ { '''sequence''': '''The largest city in France is grouped''', '''score''': 2.1E-05, '''token''': 3_80_15, '''token_str''': ''' grouped''', }, { '''sequence''': '''The largest city in France is accuser''', '''score''': 2.1E-05, '''token''': 2_55_06, '''token_str''': ''' accuser''', }, ] , ) __UpperCAmelCase = unmasker('''My name is <mask>''' , targets=[''' Patrick''', ''' Clara''', ''' Teven'''] , top_k=3 ) self.assertEqual( nested_simplify(_lowercase , decimals=6 ) , [ {'''sequence''': '''My name is Clara''', '''score''': 2E-05, '''token''': 1_36_06, '''token_str''': ''' Clara'''}, {'''sequence''': '''My name is Patrick''', '''score''': 2E-05, '''token''': 34_99, '''token_str''': ''' Patrick'''}, {'''sequence''': '''My name is Te''', '''score''': 1.9E-05, '''token''': 29_41, '''token_str''': ''' Te'''}, ] , ) @require_torch def a ( self : Optional[int] ): __UpperCAmelCase = pipeline(task='''fill-mask''' , model='''sshleifer/tiny-distilroberta-base''' , top_k=2 , framework='''pt''' ) __UpperCAmelCase = unmasker('''My name is <mask>''' ) self.assertEqual( nested_simplify(_lowercase , decimals=6 ) , [ {'''sequence''': '''My name is Maul''', '''score''': 2.2E-05, '''token''': 3_56_76, '''token_str''': ''' Maul'''}, {'''sequence''': '''My name isELS''', '''score''': 2.2E-05, '''token''': 1_64_16, '''token_str''': '''ELS'''}, ] , ) __UpperCAmelCase = unmasker('''The largest city in France is <mask>''' ) self.assertEqual( nested_simplify(_lowercase , decimals=6 ) , [ { '''sequence''': '''The largest city in France is Maul''', '''score''': 2.2E-05, '''token''': 3_56_76, '''token_str''': ''' Maul''', }, {'''sequence''': '''The largest city in France isELS''', '''score''': 2.2E-05, '''token''': 1_64_16, '''token_str''': '''ELS'''}, ] , ) __UpperCAmelCase = unmasker('''My name is <mask>''' , targets=[''' Patrick''', ''' Clara''', ''' Teven'''] , top_k=3 ) self.assertEqual( nested_simplify(_lowercase , decimals=6 ) , [ {'''sequence''': '''My name is Patrick''', '''score''': 2.1E-05, '''token''': 34_99, '''token_str''': ''' Patrick'''}, {'''sequence''': '''My name is Te''', '''score''': 2E-05, '''token''': 29_41, '''token_str''': ''' Te'''}, {'''sequence''': '''My name is Clara''', '''score''': 2E-05, '''token''': 1_36_06, '''token_str''': ''' Clara'''}, ] , ) __UpperCAmelCase = unmasker('''My name is <mask> <mask>''' , top_k=2 ) self.assertEqual( nested_simplify(_lowercase , decimals=6 ) , [ [ { '''score''': 2.2E-05, '''token''': 3_56_76, '''token_str''': ''' Maul''', '''sequence''': '''<s>My name is Maul<mask></s>''', }, {'''score''': 2.2E-05, '''token''': 1_64_16, '''token_str''': '''ELS''', '''sequence''': '''<s>My name isELS<mask></s>'''}, ], [ { '''score''': 2.2E-05, '''token''': 3_56_76, '''token_str''': ''' Maul''', '''sequence''': '''<s>My name is<mask> Maul</s>''', }, {'''score''': 2.2E-05, '''token''': 1_64_16, '''token_str''': '''ELS''', '''sequence''': '''<s>My name is<mask>ELS</s>'''}, ], ] , ) @require_torch_gpu def a ( self : Any ): __UpperCAmelCase = pipeline('''fill-mask''' , model='''hf-internal-testing/tiny-random-distilbert''' , device=0 , framework='''pt''' ) # convert model to fp16 pipe.model.half() __UpperCAmelCase = pipe('''Paris is the [MASK] of France.''' ) # We actually don't care about the result, we just want to make sure # it works, meaning the float16 tensor got casted back to float32 # for postprocessing. self.assertIsInstance(_lowercase , _lowercase ) @slow @require_torch def a ( self : int ): __UpperCAmelCase = pipeline(task='''fill-mask''' , model='''distilroberta-base''' , top_k=2 , framework='''pt''' ) self.run_large_test(_lowercase ) @slow @require_tf def a ( self : Optional[Any] ): __UpperCAmelCase = pipeline(task='''fill-mask''' , model='''distilroberta-base''' , top_k=2 , framework='''tf''' ) self.run_large_test(_lowercase ) def a ( self : Dict , _lowercase : str ): __UpperCAmelCase = unmasker('''My name is <mask>''' ) self.assertEqual( nested_simplify(_lowercase ) , [ {'''sequence''': '''My name is John''', '''score''': 0.008, '''token''': 6_10, '''token_str''': ''' John'''}, {'''sequence''': '''My name is Chris''', '''score''': 0.007, '''token''': 15_73, '''token_str''': ''' Chris'''}, ] , ) __UpperCAmelCase = unmasker('''The largest city in France is <mask>''' ) self.assertEqual( nested_simplify(_lowercase ) , [ { '''sequence''': '''The largest city in France is Paris''', '''score''': 0.251, '''token''': 22_01, '''token_str''': ''' Paris''', }, { '''sequence''': '''The largest city in France is Lyon''', '''score''': 0.214, '''token''': 1_27_90, '''token_str''': ''' Lyon''', }, ] , ) __UpperCAmelCase = unmasker('''My name is <mask>''' , targets=[''' Patrick''', ''' Clara''', ''' Teven'''] , top_k=3 ) self.assertEqual( nested_simplify(_lowercase ) , [ {'''sequence''': '''My name is Patrick''', '''score''': 0.005, '''token''': 34_99, '''token_str''': ''' Patrick'''}, {'''sequence''': '''My name is Clara''', '''score''': 0.000, '''token''': 1_36_06, '''token_str''': ''' Clara'''}, {'''sequence''': '''My name is Te''', '''score''': 0.000, '''token''': 29_41, '''token_str''': ''' Te'''}, ] , ) @require_torch def a ( self : List[Any] ): __UpperCAmelCase = pipeline(task='''fill-mask''' , model='''sshleifer/tiny-distilroberta-base''' , framework='''pt''' ) __UpperCAmelCase = None __UpperCAmelCase = None self.run_pipeline_test(_lowercase , [] ) @require_tf def a ( self : str ): __UpperCAmelCase = pipeline(task='''fill-mask''' , model='''sshleifer/tiny-distilroberta-base''' , framework='''tf''' ) __UpperCAmelCase = None __UpperCAmelCase = None self.run_pipeline_test(_lowercase , [] ) def a ( self : Optional[int] , _lowercase : Optional[Any] , _lowercase : Optional[int] , _lowercase : Tuple ): if tokenizer is None or tokenizer.mask_token_id is None: self.skipTest('''The provided tokenizer has no mask token, (probably reformer or wav2vec2)''' ) __UpperCAmelCase = FillMaskPipeline(model=_lowercase , tokenizer=_lowercase ) __UpperCAmelCase = [ F'''This is another {tokenizer.mask_token} test''', ] return fill_masker, examples def a ( self : int , _lowercase : Tuple , _lowercase : Tuple ): __UpperCAmelCase = fill_masker.tokenizer __UpperCAmelCase = fill_masker.model __UpperCAmelCase = fill_masker( F'''This is a {tokenizer.mask_token}''' , ) self.assertEqual( _lowercase , [ {'''sequence''': ANY(_lowercase ), '''score''': ANY(_lowercase ), '''token''': ANY(_lowercase ), '''token_str''': ANY(_lowercase )}, {'''sequence''': ANY(_lowercase ), '''score''': ANY(_lowercase ), '''token''': ANY(_lowercase ), '''token_str''': ANY(_lowercase )}, {'''sequence''': ANY(_lowercase ), '''score''': ANY(_lowercase ), '''token''': ANY(_lowercase ), '''token_str''': ANY(_lowercase )}, {'''sequence''': ANY(_lowercase ), '''score''': ANY(_lowercase ), '''token''': ANY(_lowercase ), '''token_str''': ANY(_lowercase )}, {'''sequence''': ANY(_lowercase ), '''score''': ANY(_lowercase ), '''token''': ANY(_lowercase ), '''token_str''': ANY(_lowercase )}, ] , ) __UpperCAmelCase = fill_masker([F'''This is a {tokenizer.mask_token}'''] ) self.assertEqual( _lowercase , [ {'''sequence''': ANY(_lowercase ), '''score''': ANY(_lowercase ), '''token''': ANY(_lowercase ), '''token_str''': ANY(_lowercase )}, {'''sequence''': ANY(_lowercase ), '''score''': ANY(_lowercase ), '''token''': ANY(_lowercase ), '''token_str''': ANY(_lowercase )}, {'''sequence''': ANY(_lowercase ), '''score''': ANY(_lowercase ), '''token''': ANY(_lowercase ), '''token_str''': ANY(_lowercase )}, {'''sequence''': ANY(_lowercase ), '''score''': ANY(_lowercase ), '''token''': ANY(_lowercase ), '''token_str''': ANY(_lowercase )}, {'''sequence''': ANY(_lowercase ), '''score''': ANY(_lowercase ), '''token''': ANY(_lowercase ), '''token_str''': ANY(_lowercase )}, ] , ) __UpperCAmelCase = fill_masker([F'''This is a {tokenizer.mask_token}''', F'''Another {tokenizer.mask_token} great test.'''] ) self.assertEqual( _lowercase , [ [ {'''sequence''': ANY(_lowercase ), '''score''': ANY(_lowercase ), '''token''': ANY(_lowercase ), '''token_str''': ANY(_lowercase )}, {'''sequence''': ANY(_lowercase ), '''score''': ANY(_lowercase ), '''token''': ANY(_lowercase ), '''token_str''': ANY(_lowercase )}, {'''sequence''': ANY(_lowercase ), '''score''': ANY(_lowercase ), '''token''': ANY(_lowercase ), '''token_str''': ANY(_lowercase )}, {'''sequence''': ANY(_lowercase ), '''score''': ANY(_lowercase ), '''token''': ANY(_lowercase ), '''token_str''': ANY(_lowercase )}, {'''sequence''': ANY(_lowercase ), '''score''': ANY(_lowercase ), '''token''': ANY(_lowercase ), '''token_str''': ANY(_lowercase )}, ], [ {'''sequence''': ANY(_lowercase ), '''score''': ANY(_lowercase ), '''token''': ANY(_lowercase ), '''token_str''': ANY(_lowercase )}, {'''sequence''': ANY(_lowercase ), '''score''': ANY(_lowercase ), '''token''': ANY(_lowercase ), '''token_str''': ANY(_lowercase )}, {'''sequence''': ANY(_lowercase ), '''score''': ANY(_lowercase ), '''token''': ANY(_lowercase ), '''token_str''': ANY(_lowercase )}, {'''sequence''': ANY(_lowercase ), '''score''': ANY(_lowercase ), '''token''': ANY(_lowercase ), '''token_str''': ANY(_lowercase )}, {'''sequence''': ANY(_lowercase ), '''score''': ANY(_lowercase ), '''token''': ANY(_lowercase ), '''token_str''': ANY(_lowercase )}, ], ] , ) with self.assertRaises(_lowercase ): fill_masker([None] ) # No mask_token is not supported with self.assertRaises(_lowercase ): fill_masker('''This is''' ) self.run_test_top_k(_lowercase , _lowercase ) self.run_test_targets(_lowercase , _lowercase ) self.run_test_top_k_targets(_lowercase , _lowercase ) self.fill_mask_with_duplicate_targets_and_top_k(_lowercase , _lowercase ) self.fill_mask_with_multiple_masks(_lowercase , _lowercase ) def a ( self : Optional[Any] , _lowercase : Optional[Any] , _lowercase : List[Any] ): __UpperCAmelCase = tokenizer.get_vocab() __UpperCAmelCase = sorted(vocab.keys() )[:2] # Pipeline argument __UpperCAmelCase = FillMaskPipeline(model=_lowercase , tokenizer=_lowercase , targets=_lowercase ) __UpperCAmelCase = fill_masker(F'''This is a {tokenizer.mask_token}''' ) self.assertEqual( _lowercase , [ {'''sequence''': ANY(_lowercase ), '''score''': ANY(_lowercase ), '''token''': ANY(_lowercase ), '''token_str''': ANY(_lowercase )}, {'''sequence''': ANY(_lowercase ), '''score''': ANY(_lowercase ), '''token''': ANY(_lowercase ), '''token_str''': ANY(_lowercase )}, ] , ) __UpperCAmelCase = {vocab[el] for el in targets} self.assertEqual({el['''token'''] for el in outputs} , _lowercase ) __UpperCAmelCase = [tokenizer.decode([x] ) for x in target_ids] self.assertEqual({el['''token_str'''] for el in outputs} , set(_lowercase ) ) # Call argument __UpperCAmelCase = FillMaskPipeline(model=_lowercase , tokenizer=_lowercase ) __UpperCAmelCase = fill_masker(F'''This is a {tokenizer.mask_token}''' , targets=_lowercase ) self.assertEqual( _lowercase , [ {'''sequence''': ANY(_lowercase ), '''score''': ANY(_lowercase ), '''token''': ANY(_lowercase ), '''token_str''': ANY(_lowercase )}, {'''sequence''': ANY(_lowercase ), '''score''': ANY(_lowercase ), '''token''': ANY(_lowercase ), '''token_str''': ANY(_lowercase )}, ] , ) __UpperCAmelCase = {vocab[el] for el in targets} self.assertEqual({el['''token'''] for el in outputs} , _lowercase ) __UpperCAmelCase = [tokenizer.decode([x] ) for x in target_ids] self.assertEqual({el['''token_str'''] for el in outputs} , set(_lowercase ) ) # Score equivalence __UpperCAmelCase = fill_masker(F'''This is a {tokenizer.mask_token}''' , targets=_lowercase ) __UpperCAmelCase = [top_mask['''token_str'''] for top_mask in outputs] __UpperCAmelCase = [top_mask['''score'''] for top_mask in outputs] # For some BPE tokenizers, `</w>` is removed during decoding, so `token_str` won't be the same as in `targets`. if set(_lowercase ) == set(_lowercase ): __UpperCAmelCase = fill_masker(F'''This is a {tokenizer.mask_token}''' , targets=_lowercase ) __UpperCAmelCase = [top_mask['''score'''] for top_mask in unmasked_targets] self.assertEqual(nested_simplify(_lowercase ) , nested_simplify(_lowercase ) ) # Raises with invalid with self.assertRaises(_lowercase ): __UpperCAmelCase = fill_masker(F'''This is a {tokenizer.mask_token}''' , targets=[] ) # For some tokenizers, `""` is actually in the vocabulary and the expected error won't raised if "" not in tokenizer.get_vocab(): with self.assertRaises(_lowercase ): __UpperCAmelCase = fill_masker(F'''This is a {tokenizer.mask_token}''' , targets=[''''''] ) with self.assertRaises(_lowercase ): __UpperCAmelCase = fill_masker(F'''This is a {tokenizer.mask_token}''' , targets='''''' ) def a ( self : List[Any] , _lowercase : Tuple , _lowercase : Optional[Any] ): __UpperCAmelCase = FillMaskPipeline(model=_lowercase , tokenizer=_lowercase , top_k=2 ) __UpperCAmelCase = fill_masker(F'''This is a {tokenizer.mask_token}''' ) self.assertEqual( _lowercase , [ {'''sequence''': ANY(_lowercase ), '''score''': ANY(_lowercase ), '''token''': ANY(_lowercase ), '''token_str''': ANY(_lowercase )}, {'''sequence''': ANY(_lowercase ), '''score''': ANY(_lowercase ), '''token''': ANY(_lowercase ), '''token_str''': ANY(_lowercase )}, ] , ) __UpperCAmelCase = FillMaskPipeline(model=_lowercase , tokenizer=_lowercase ) __UpperCAmelCase = fill_masker(F'''This is a {tokenizer.mask_token}''' , top_k=2 ) self.assertEqual( _lowercase , [ {'''sequence''': ANY(_lowercase ), '''score''': ANY(_lowercase ), '''token''': ANY(_lowercase ), '''token_str''': ANY(_lowercase )}, {'''sequence''': ANY(_lowercase ), '''score''': ANY(_lowercase ), '''token''': ANY(_lowercase ), '''token_str''': ANY(_lowercase )}, ] , ) self.assertEqual(nested_simplify(_lowercase ) , nested_simplify(_lowercase ) ) def a ( self : Optional[int] , _lowercase : int , _lowercase : Tuple ): __UpperCAmelCase = tokenizer.get_vocab() __UpperCAmelCase = FillMaskPipeline(model=_lowercase , tokenizer=_lowercase ) # top_k=2, ntargets=3 __UpperCAmelCase = sorted(vocab.keys() )[:3] __UpperCAmelCase = fill_masker(F'''This is a {tokenizer.mask_token}''' , top_k=2 , targets=_lowercase ) # If we use the most probably targets, and filter differently, we should still # have the same results __UpperCAmelCase = [el['''token_str'''] for el in sorted(_lowercase , key=lambda _lowercase : x["score"] , reverse=_lowercase )] # For some BPE tokenizers, `</w>` is removed during decoding, so `token_str` won't be the same as in `targets`. if set(_lowercase ).issubset(_lowercase ): __UpperCAmelCase = fill_masker(F'''This is a {tokenizer.mask_token}''' , top_k=3 , targets=_lowercase ) # They should yield exactly the same result self.assertEqual(nested_simplify(_lowercase ) , nested_simplify(_lowercase ) ) def a ( self : Union[str, Any] , _lowercase : Tuple , _lowercase : Union[str, Any] ): __UpperCAmelCase = FillMaskPipeline(model=_lowercase , tokenizer=_lowercase ) __UpperCAmelCase = tokenizer.get_vocab() # String duplicates + id duplicates __UpperCAmelCase = sorted(vocab.keys() )[:3] __UpperCAmelCase = [targets[0], targets[1], targets[0], targets[2], targets[1]] __UpperCAmelCase = fill_masker(F'''My name is {tokenizer.mask_token}''' , targets=_lowercase , top_k=10 ) # The target list contains duplicates, so we can't output more # than them self.assertEqual(len(_lowercase ) , 3 ) def a ( self : Dict , _lowercase : Dict , _lowercase : Any ): __UpperCAmelCase = FillMaskPipeline(model=_lowercase , tokenizer=_lowercase ) __UpperCAmelCase = fill_masker( F'''This is a {tokenizer.mask_token} {tokenizer.mask_token} {tokenizer.mask_token}''' , top_k=2 ) self.assertEqual( _lowercase , [ [ {'''sequence''': ANY(_lowercase ), '''score''': ANY(_lowercase ), '''token''': ANY(_lowercase ), '''token_str''': ANY(_lowercase )}, {'''sequence''': ANY(_lowercase ), '''score''': ANY(_lowercase ), '''token''': ANY(_lowercase ), '''token_str''': ANY(_lowercase )}, ], [ {'''sequence''': ANY(_lowercase ), '''score''': ANY(_lowercase ), '''token''': ANY(_lowercase ), '''token_str''': ANY(_lowercase )}, {'''sequence''': ANY(_lowercase ), '''score''': ANY(_lowercase ), '''token''': ANY(_lowercase ), '''token_str''': ANY(_lowercase )}, ], [ {'''sequence''': ANY(_lowercase ), '''score''': ANY(_lowercase ), '''token''': ANY(_lowercase ), '''token_str''': ANY(_lowercase )}, {'''sequence''': ANY(_lowercase ), '''score''': ANY(_lowercase ), '''token''': ANY(_lowercase ), '''token_str''': ANY(_lowercase )}, ], ] , )
86
0
'''simple docstring''' from bisect import bisect from itertools import accumulate def snake_case_ ( __SCREAMING_SNAKE_CASE : Dict , __SCREAMING_SNAKE_CASE : List[Any] , __SCREAMING_SNAKE_CASE : int , __SCREAMING_SNAKE_CASE : int ): """simple docstring""" lowercase_ : str = sorted(zip(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) , key=lambda __SCREAMING_SNAKE_CASE : x[0] / x[1] , reverse=__SCREAMING_SNAKE_CASE ) lowercase_ , lowercase_ : List[Any] = [i[0] for i in r], [i[1] for i in r] lowercase_ : List[str] = list(accumulate(__SCREAMING_SNAKE_CASE ) ) lowercase_ : List[Any] = bisect(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) return ( 0 if k == 0 else sum(vl[:k] ) + (w - acc[k - 1]) * (vl[k]) / (wt[k]) if k != n else sum(vl[:k] ) ) if __name__ == "__main__": import doctest doctest.testmod()
93
import os # Precomputes a list of the 100 first triangular numbers lowercase_ = [int(0.5 * n * (n + 1)) for n in range(1, 101)] def _snake_case( ) -> int: '''simple docstring''' A__ = os.path.dirname(os.path.realpath(SCREAMING_SNAKE_CASE__ ) ) A__ = os.path.join(SCREAMING_SNAKE_CASE__ , 'words.txt' ) A__ = '' with open(SCREAMING_SNAKE_CASE__ ) as f: A__ = f.readline() A__ = [word.strip('"' ) for word in words.strip('\r\n' ).split(',' )] A__ = [ word for word in [sum(ord(SCREAMING_SNAKE_CASE__ ) - 64 for x in word ) for word in words] if word in TRIANGULAR_NUMBERS ] return len(SCREAMING_SNAKE_CASE__ ) if __name__ == "__main__": print(solution())
7
0
"""simple docstring""" lowercase_ = { "Pillow": "Pillow<10.0.0", "accelerate": "accelerate>=0.20.3", "av": "av==9.2.0", "beautifulsoup4": "beautifulsoup4", "black": "black~=23.1", "codecarbon": "codecarbon==1.2.0", "cookiecutter": "cookiecutter==1.7.3", "dataclasses": "dataclasses", "datasets": "datasets!=2.5.0", "decord": "decord==0.6.0", "deepspeed": "deepspeed>=0.9.3", "diffusers": "diffusers", "dill": "dill<0.3.5", "evaluate": "evaluate>=0.2.0", "fairscale": "fairscale>0.3", "faiss-cpu": "faiss-cpu", "fastapi": "fastapi", "filelock": "filelock", "flax": "flax>=0.4.1,<=0.7.0", "ftfy": "ftfy", "fugashi": "fugashi>=1.0", "GitPython": "GitPython<3.1.19", "hf-doc-builder": "hf-doc-builder>=0.3.0", "huggingface-hub": "huggingface-hub>=0.14.1,<1.0", "importlib_metadata": "importlib_metadata", "ipadic": "ipadic>=1.0.0,<2.0", "isort": "isort>=5.5.4", "jax": "jax>=0.2.8,!=0.3.2,<=0.4.13", "jaxlib": "jaxlib>=0.1.65,<=0.4.13", "jieba": "jieba", "kenlm": "kenlm", "keras-nlp": "keras-nlp>=0.3.1", "librosa": "librosa", "nltk": "nltk", "natten": "natten>=0.14.6", "numpy": "numpy>=1.17", "onnxconverter-common": "onnxconverter-common", "onnxruntime-tools": "onnxruntime-tools>=1.4.2", "onnxruntime": "onnxruntime>=1.4.0", "opencv-python": "opencv-python", "optuna": "optuna", "optax": "optax>=0.0.8,<=0.1.4", "packaging": "packaging>=20.0", "parameterized": "parameterized", "phonemizer": "phonemizer", "protobuf": "protobuf", "psutil": "psutil", "pyyaml": "pyyaml>=5.1", "pydantic": "pydantic<2", "pytest": "pytest>=7.2.0", "pytest-timeout": "pytest-timeout", "pytest-xdist": "pytest-xdist", "python": "python>=3.8.0", "ray[tune]": "ray[tune]", "regex": "regex!=2019.12.17", "requests": "requests", "rhoknp": "rhoknp>=1.1.0,<1.3.1", "rjieba": "rjieba", "rouge-score": "rouge-score!=0.0.7,!=0.0.8,!=0.1,!=0.1.1", "ruff": "ruff>=0.0.241,<=0.0.259", "sacrebleu": "sacrebleu>=1.4.12,<2.0.0", "sacremoses": "sacremoses", "safetensors": "safetensors>=0.3.1", "sagemaker": "sagemaker>=2.31.0", "scikit-learn": "scikit-learn", "sentencepiece": "sentencepiece>=0.1.91,!=0.1.92", "sigopt": "sigopt", "starlette": "starlette", "sudachipy": "sudachipy>=0.6.6", "sudachidict_core": "sudachidict_core>=20220729", "tensorflow-cpu": "tensorflow-cpu>=2.6,<2.14", "tensorflow": "tensorflow>=2.6,<2.14", "tensorflow-text": "tensorflow-text<2.14", "tf2onnx": "tf2onnx", "timeout-decorator": "timeout-decorator", "timm": "timm", "tokenizers": "tokenizers>=0.11.1,!=0.11.3,<0.14", "torch": "torch>=1.9,!=1.12.0", "torchaudio": "torchaudio", "torchvision": "torchvision", "pyctcdecode": "pyctcdecode>=0.4.0", "tqdm": "tqdm>=4.27", "unidic": "unidic>=1.0.2", "unidic_lite": "unidic_lite>=1.0.7", "urllib3": "urllib3<2.0.0", "uvicorn": "uvicorn", }
367
"""simple docstring""" import torch from diffusers import UnCLIPScheduler from .test_schedulers import SchedulerCommonTest class __lowerCAmelCase ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' __UpperCAmelCase : List[str] = (UnCLIPScheduler,) def __UpperCAmelCase ( self , **_a ): __a = { '''num_train_timesteps''': 1_000, '''variance_type''': '''fixed_small_log''', '''clip_sample''': True, '''clip_sample_range''': 1.0, '''prediction_type''': '''epsilon''', } config.update(**_a ) return config def __UpperCAmelCase ( self ): for timesteps in [1, 5, 100, 1_000]: self.check_over_configs(num_train_timesteps=_a ) def __UpperCAmelCase ( self ): for variance in ["fixed_small_log", "learned_range"]: self.check_over_configs(variance_type=_a ) def __UpperCAmelCase ( self ): for clip_sample in [True, False]: self.check_over_configs(clip_sample=_a ) def __UpperCAmelCase ( self ): for clip_sample_range in [1, 5, 10, 20]: self.check_over_configs(clip_sample_range=_a ) def __UpperCAmelCase ( self ): for prediction_type in ["epsilon", "sample"]: self.check_over_configs(prediction_type=_a ) def __UpperCAmelCase ( self ): for time_step in [0, 500, 999]: for prev_timestep in [None, 5, 100, 250, 500, 750]: if prev_timestep is not None and prev_timestep >= time_step: continue self.check_over_forward(time_step=_a , prev_timestep=_a ) def __UpperCAmelCase ( self ): __a = self.scheduler_classes[0] __a = self.get_scheduler_config(variance_type='''fixed_small_log''' ) __a = scheduler_class(**_a ) assert torch.sum(torch.abs(scheduler._get_variance(0 ) - 1.0_000E-10 ) ) < 1E-5 assert torch.sum(torch.abs(scheduler._get_variance(487 ) - 0.054_9625 ) ) < 1E-5 assert torch.sum(torch.abs(scheduler._get_variance(999 ) - 0.999_4987 ) ) < 1E-5 def __UpperCAmelCase ( self ): __a = self.scheduler_classes[0] __a = self.get_scheduler_config(variance_type='''learned_range''' ) __a = scheduler_class(**_a ) __a = 0.5 assert scheduler._get_variance(1 , predicted_variance=_a ) - -10.171_2790 < 1E-5 assert scheduler._get_variance(487 , predicted_variance=_a ) - -5.799_8052 < 1E-5 assert scheduler._get_variance(999 , predicted_variance=_a ) - -0.001_0011 < 1E-5 def __UpperCAmelCase ( self ): __a = self.scheduler_classes[0] __a = self.get_scheduler_config() __a = scheduler_class(**_a ) __a = scheduler.timesteps __a = self.dummy_model() __a = self.dummy_sample_deter __a = torch.manual_seed(0 ) for i, t in enumerate(_a ): # 1. predict noise residual __a = model(_a , _a ) # 2. predict previous mean of sample x_t-1 __a = scheduler.step(_a , _a , _a , generator=_a ).prev_sample __a = pred_prev_sample __a = torch.sum(torch.abs(_a ) ) __a = torch.mean(torch.abs(_a ) ) assert abs(result_sum.item() - 252.268_2495 ) < 1E-2 assert abs(result_mean.item() - 0.328_4743 ) < 1E-3 def __UpperCAmelCase ( self ): __a = self.scheduler_classes[0] __a = self.get_scheduler_config() __a = scheduler_class(**_a ) scheduler.set_timesteps(25 ) __a = scheduler.timesteps __a = self.dummy_model() __a = self.dummy_sample_deter __a = torch.manual_seed(0 ) for i, t in enumerate(_a ): # 1. predict noise residual __a = model(_a , _a ) if i + 1 == timesteps.shape[0]: __a = None else: __a = timesteps[i + 1] # 2. predict previous mean of sample x_t-1 __a = scheduler.step( _a , _a , _a , prev_timestep=_a , generator=_a ).prev_sample __a = pred_prev_sample __a = torch.sum(torch.abs(_a ) ) __a = torch.mean(torch.abs(_a ) ) assert abs(result_sum.item() - 258.204_4983 ) < 1E-2 assert abs(result_mean.item() - 0.336_2038 ) < 1E-3 def __UpperCAmelCase ( self ): pass def __UpperCAmelCase ( self ): pass
11
0
'''simple docstring''' import argparse import glob import logging import os import time from argparse import Namespace import numpy as np import torch from lightning_base import BaseTransformer, add_generic_args, generic_train from torch.utils.data import DataLoader, TensorDataset from transformers import glue_compute_metrics as compute_metrics from transformers import glue_convert_examples_to_features as convert_examples_to_features from transformers import glue_output_modes, glue_tasks_num_labels from transformers import glue_processors as processors __lowercase = logging.getLogger(__name__) class a__( lowerCAmelCase__ ): '''simple docstring''' UpperCAmelCase_ : Any = '''sequence-classification''' def __init__( self , __lowerCAmelCase): """simple docstring""" if type(__lowerCAmelCase) == dict: lowerCAmelCase = Namespace(**__lowerCAmelCase) lowerCAmelCase = glue_output_modes[hparams.task] lowerCAmelCase = glue_tasks_num_labels[hparams.task] super().__init__(__lowerCAmelCase , __lowerCAmelCase , self.mode) def a_ ( self , **__lowerCAmelCase): """simple docstring""" return self.model(**__lowerCAmelCase) def a_ ( self , __lowerCAmelCase , __lowerCAmelCase): """simple docstring""" lowerCAmelCase = {"""input_ids""": batch[0], """attention_mask""": batch[1], """labels""": batch[3]} if self.config.model_type not in ["distilbert", "bart"]: lowerCAmelCase = batch[2] if self.config.model_type in ["""bert""", """xlnet""", """albert"""] else None lowerCAmelCase = self(**__lowerCAmelCase) lowerCAmelCase = outputs[0] lowerCAmelCase = self.trainer.lr_schedulers[0]["""scheduler"""] lowerCAmelCase = {"""loss""": loss, """rate""": lr_scheduler.get_last_lr()[-1]} return {"loss": loss, "log": tensorboard_logs} def a_ ( self): """simple docstring""" lowerCAmelCase = self.hparams lowerCAmelCase = processors[args.task]() lowerCAmelCase = processor.get_labels() for mode in ["train", "dev"]: lowerCAmelCase = self._feature_file(__lowerCAmelCase) if os.path.exists(__lowerCAmelCase) and not args.overwrite_cache: logger.info("""Loading features from cached file %s""" , __lowerCAmelCase) else: logger.info("""Creating features from dataset file at %s""" , args.data_dir) lowerCAmelCase = ( processor.get_dev_examples(args.data_dir) if mode == """dev""" else processor.get_train_examples(args.data_dir) ) lowerCAmelCase = convert_examples_to_features( __lowerCAmelCase , self.tokenizer , max_length=args.max_seq_length , label_list=self.labels , output_mode=args.glue_output_mode , ) logger.info("""Saving features into cached file %s""" , __lowerCAmelCase) torch.save(__lowerCAmelCase , __lowerCAmelCase) def a_ ( self , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase = False): """simple docstring""" lowerCAmelCase = """dev""" if mode == """test""" else mode lowerCAmelCase = self._feature_file(__lowerCAmelCase) logger.info("""Loading features from cached file %s""" , __lowerCAmelCase) lowerCAmelCase = torch.load(__lowerCAmelCase) lowerCAmelCase = torch.tensor([f.input_ids for f in features] , dtype=torch.long) lowerCAmelCase = torch.tensor([f.attention_mask for f in features] , dtype=torch.long) lowerCAmelCase = torch.tensor([f.token_type_ids for f in features] , dtype=torch.long) if self.hparams.glue_output_mode == "classification": lowerCAmelCase = torch.tensor([f.label for f in features] , dtype=torch.long) elif self.hparams.glue_output_mode == "regression": lowerCAmelCase = torch.tensor([f.label for f in features] , dtype=torch.float) return DataLoader( TensorDataset(__lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase) , batch_size=__lowerCAmelCase , shuffle=__lowerCAmelCase , ) def a_ ( self , __lowerCAmelCase , __lowerCAmelCase): """simple docstring""" lowerCAmelCase = {"""input_ids""": batch[0], """attention_mask""": batch[1], """labels""": batch[3]} if self.config.model_type not in ["distilbert", "bart"]: lowerCAmelCase = batch[2] if self.config.model_type in ["""bert""", """xlnet""", """albert"""] else None lowerCAmelCase = self(**__lowerCAmelCase) lowerCAmelCase , lowerCAmelCase = outputs[:2] lowerCAmelCase = logits.detach().cpu().numpy() lowerCAmelCase = inputs["""labels"""].detach().cpu().numpy() return {"val_loss": tmp_eval_loss.detach().cpu(), "pred": preds, "target": out_label_ids} def a_ ( self , __lowerCAmelCase): """simple docstring""" lowerCAmelCase = torch.stack([x["""val_loss"""] for x in outputs]).mean().detach().cpu().item() lowerCAmelCase = np.concatenate([x["""pred"""] for x in outputs] , axis=0) if self.hparams.glue_output_mode == "classification": lowerCAmelCase = np.argmax(__lowerCAmelCase , axis=1) elif self.hparams.glue_output_mode == "regression": lowerCAmelCase = np.squeeze(__lowerCAmelCase) lowerCAmelCase = np.concatenate([x["""target"""] for x in outputs] , axis=0) lowerCAmelCase = [[] for _ in range(out_label_ids.shape[0])] lowerCAmelCase = [[] for _ in range(out_label_ids.shape[0])] lowerCAmelCase = {**{"""val_loss""": val_loss_mean}, **compute_metrics(self.hparams.task , __lowerCAmelCase , __lowerCAmelCase)} lowerCAmelCase = dict(results.items()) lowerCAmelCase = results return ret, preds_list, out_label_list def a_ ( self , __lowerCAmelCase): """simple docstring""" lowerCAmelCase , lowerCAmelCase , lowerCAmelCase = self._eval_end(__lowerCAmelCase) lowerCAmelCase = ret["""log"""] return {"val_loss": logs["val_loss"], "log": logs, "progress_bar": logs} def a_ ( self , __lowerCAmelCase): """simple docstring""" lowerCAmelCase , lowerCAmelCase , lowerCAmelCase = self._eval_end(__lowerCAmelCase) lowerCAmelCase = ret["""log"""] # `val_loss` is the key returned by `self._eval_end()` but actually refers to `test_loss` return {"avg_test_loss": logs["val_loss"], "log": logs, "progress_bar": logs} @staticmethod def a_ ( __lowerCAmelCase , __lowerCAmelCase): """simple docstring""" BaseTransformer.add_model_specific_args(__lowerCAmelCase , __lowerCAmelCase) parser.add_argument( """--max_seq_length""" , default=128 , type=__lowerCAmelCase , help=( """The maximum total input sequence length after tokenization. Sequences longer """ """than this will be truncated, sequences shorter will be padded.""" ) , ) parser.add_argument( """--task""" , default="""""" , type=__lowerCAmelCase , required=__lowerCAmelCase , help="""The GLUE task to run""" , ) parser.add_argument( """--gpus""" , default=0 , type=__lowerCAmelCase , help="""The number of GPUs allocated for this, it is by default 0 meaning none""" , ) parser.add_argument( """--overwrite_cache""" , action="""store_true""" , help="""Overwrite the cached training and evaluation sets""") return parser def snake_case__ ( ) -> Optional[Any]: '''simple docstring''' lowerCAmelCase = argparse.ArgumentParser() add_generic_args(_A , os.getcwd() ) lowerCAmelCase = GLUETransformer.add_model_specific_args(_A , os.getcwd() ) lowerCAmelCase = parser.parse_args() # If output_dir not provided, a folder will be generated in pwd if args.output_dir is None: lowerCAmelCase = os.path.join( """./results""" , f"{args.task}_{time.strftime('%Y%m%d_%H%M%S' )}" , ) os.makedirs(args.output_dir ) lowerCAmelCase = GLUETransformer(_A ) lowerCAmelCase = generic_train(_A , _A ) # Optionally, predict on dev set and write to output_dir if args.do_predict: lowerCAmelCase = sorted(glob.glob(os.path.join(args.output_dir , """checkpoint-epoch=*.ckpt""" ) , recursive=_A ) ) lowerCAmelCase = model.load_from_checkpoint(checkpoints[-1] ) return trainer.test(_A ) if __name__ == "__main__": main()
272
'''simple docstring''' import enum import warnings from .. import MODEL_FOR_CAUSAL_LM_MAPPING, TF_MODEL_FOR_CAUSAL_LM_MAPPING from ..utils import add_end_docstrings, is_tf_available from .base import PIPELINE_INIT_ARGS, Pipeline if is_tf_available(): import tensorflow as tf class a__( enum.Enum ): '''simple docstring''' UpperCAmelCase_ : Dict = 0 UpperCAmelCase_ : Dict = 1 UpperCAmelCase_ : Any = 2 @add_end_docstrings(lowerCAmelCase__ ) class a__( lowerCAmelCase__ ): '''simple docstring''' UpperCAmelCase_ : int = ''' In 1991, the remains of Russian Tsar Nicholas II and his family (except for Alexei and Maria) are discovered. The voice of Nicholas\'s young son, Tsarevich Alexei Nikolaevich, narrates the remainder of the story. 1883 Western Siberia, a young Grigori Rasputin is asked by his father and a group of men to perform magic. Rasputin has a vision and denounces one of the men as a horse thief. Although his father initially slaps him for making such an accusation, Rasputin watches as the man is chased outside and beaten. Twenty years later, Rasputin sees a vision of the Virgin Mary, prompting him to become a priest. Rasputin quickly becomes famous, with people, even a bishop, begging for his blessing. <eod> </s> <eos> ''' def __init__( self , *__lowerCAmelCase , **__lowerCAmelCase): """simple docstring""" super().__init__(*__lowerCAmelCase , **__lowerCAmelCase) self.check_model_type( TF_MODEL_FOR_CAUSAL_LM_MAPPING if self.framework == """tf""" else MODEL_FOR_CAUSAL_LM_MAPPING) if "prefix" not in self._preprocess_params: # This is very specific. The logic is quite complex and needs to be done # as a "default". # It also defines both some preprocess_kwargs and generate_kwargs # which is why we cannot put them in their respective methods. lowerCAmelCase = None if self.model.config.prefix is not None: lowerCAmelCase = self.model.config.prefix if prefix is None and self.model.__class__.__name__ in [ "XLNetLMHeadModel", "TransfoXLLMHeadModel", "TFXLNetLMHeadModel", "TFTransfoXLLMHeadModel", ]: # For XLNet and TransformerXL we add an article to the prompt to give more state to the model. lowerCAmelCase = self.XL_PREFIX if prefix is not None: # Recalculate some generate_kwargs linked to prefix. lowerCAmelCase , lowerCAmelCase , lowerCAmelCase = self._sanitize_parameters(prefix=__lowerCAmelCase , **self._forward_params) lowerCAmelCase = {**self._preprocess_params, **preprocess_params} lowerCAmelCase = {**self._forward_params, **forward_params} def a_ ( self , __lowerCAmelCase=None , __lowerCAmelCase=None , __lowerCAmelCase=None , __lowerCAmelCase=None , __lowerCAmelCase=None , __lowerCAmelCase=None , __lowerCAmelCase=None , __lowerCAmelCase=None , **__lowerCAmelCase , ): """simple docstring""" lowerCAmelCase = {} if prefix is not None: lowerCAmelCase = prefix if prefix: lowerCAmelCase = self.tokenizer( __lowerCAmelCase , padding=__lowerCAmelCase , add_special_tokens=__lowerCAmelCase , return_tensors=self.framework) lowerCAmelCase = prefix_inputs["""input_ids"""].shape[-1] if handle_long_generation is not None: if handle_long_generation not in {"hole"}: raise ValueError( f"{handle_long_generation} is not a valid value for `handle_long_generation` parameter expected" """ [None, 'hole']""") lowerCAmelCase = handle_long_generation preprocess_params.update(__lowerCAmelCase) lowerCAmelCase = generate_kwargs lowerCAmelCase = {} if return_full_text is not None and return_type is None: if return_text is not None: raise ValueError("""`return_text` is mutually exclusive with `return_full_text`""") if return_tensors is not None: raise ValueError("""`return_full_text` is mutually exclusive with `return_tensors`""") lowerCAmelCase = ReturnType.FULL_TEXT if return_full_text else ReturnType.NEW_TEXT if return_tensors is not None and return_type is None: if return_text is not None: raise ValueError("""`return_text` is mutually exclusive with `return_tensors`""") lowerCAmelCase = ReturnType.TENSORS if return_type is not None: lowerCAmelCase = return_type if clean_up_tokenization_spaces is not None: lowerCAmelCase = clean_up_tokenization_spaces if stop_sequence is not None: lowerCAmelCase = self.tokenizer.encode(__lowerCAmelCase , add_special_tokens=__lowerCAmelCase) if len(__lowerCAmelCase) > 1: warnings.warn( """Stopping on a multiple token sequence is not yet supported on transformers. The first token of""" """ the stop sequence will be used as the stop sequence string in the interim.""") lowerCAmelCase = stop_sequence_ids[0] return preprocess_params, forward_params, postprocess_params def a_ ( self , *__lowerCAmelCase , **__lowerCAmelCase): """simple docstring""" if self.model.__class__.__name__ in ["TransfoXLLMHeadModel"]: kwargs.update({"""add_space_before_punct_symbol""": True}) return super()._parse_and_tokenize(*__lowerCAmelCase , **__lowerCAmelCase) def __call__( self , __lowerCAmelCase , **__lowerCAmelCase): """simple docstring""" return super().__call__(__lowerCAmelCase , **__lowerCAmelCase) def a_ ( self , __lowerCAmelCase , __lowerCAmelCase="" , __lowerCAmelCase=None , **__lowerCAmelCase): """simple docstring""" lowerCAmelCase = self.tokenizer( prefix + prompt_text , padding=__lowerCAmelCase , add_special_tokens=__lowerCAmelCase , return_tensors=self.framework) lowerCAmelCase = prompt_text if handle_long_generation == "hole": lowerCAmelCase = inputs["""input_ids"""].shape[-1] if "max_new_tokens" in generate_kwargs: lowerCAmelCase = generate_kwargs["""max_new_tokens"""] else: lowerCAmelCase = generate_kwargs.get("""max_length""" , self.model.config.max_length) - cur_len if new_tokens < 0: raise ValueError("""We cannot infer how many new tokens are expected""") if cur_len + new_tokens > self.tokenizer.model_max_length: lowerCAmelCase = self.tokenizer.model_max_length - new_tokens if keep_length <= 0: raise ValueError( """We cannot use `hole` to handle this generation the number of desired tokens exceeds the""" """ models max length""") lowerCAmelCase = inputs["""input_ids"""][:, -keep_length:] if "attention_mask" in inputs: lowerCAmelCase = inputs["""attention_mask"""][:, -keep_length:] return inputs def a_ ( self , __lowerCAmelCase , **__lowerCAmelCase): """simple docstring""" lowerCAmelCase = model_inputs["""input_ids"""] lowerCAmelCase = model_inputs.get("""attention_mask""" , __lowerCAmelCase) # Allow empty prompts if input_ids.shape[1] == 0: lowerCAmelCase = None lowerCAmelCase = None lowerCAmelCase = 1 else: lowerCAmelCase = input_ids.shape[0] lowerCAmelCase = model_inputs.pop("""prompt_text""") # If there is a prefix, we may need to adjust the generation length. Do so without permanently modifying # generate_kwargs, as some of the parameterization may come from the initialization of the pipeline. lowerCAmelCase = generate_kwargs.pop("""prefix_length""" , 0) if prefix_length > 0: lowerCAmelCase = """max_new_tokens""" in generate_kwargs or ( """generation_config""" in generate_kwargs and generate_kwargs["""generation_config"""].max_new_tokens is not None ) if not has_max_new_tokens: lowerCAmelCase = generate_kwargs.get("""max_length""") or self.model.config.max_length generate_kwargs["max_length"] += prefix_length lowerCAmelCase = """min_new_tokens""" in generate_kwargs or ( """generation_config""" in generate_kwargs and generate_kwargs["""generation_config"""].min_new_tokens is not None ) if not has_min_new_tokens and "min_length" in generate_kwargs: generate_kwargs["min_length"] += prefix_length # BS x SL lowerCAmelCase = self.model.generate(input_ids=__lowerCAmelCase , attention_mask=__lowerCAmelCase , **__lowerCAmelCase) lowerCAmelCase = generated_sequence.shape[0] if self.framework == "pt": lowerCAmelCase = generated_sequence.reshape(__lowerCAmelCase , out_b // in_b , *generated_sequence.shape[1:]) elif self.framework == "tf": lowerCAmelCase = tf.reshape(__lowerCAmelCase , (in_b, out_b // in_b, *generated_sequence.shape[1:])) return {"generated_sequence": generated_sequence, "input_ids": input_ids, "prompt_text": prompt_text} def a_ ( self , __lowerCAmelCase , __lowerCAmelCase=ReturnType.FULL_TEXT , __lowerCAmelCase=True): """simple docstring""" lowerCAmelCase = model_outputs["""generated_sequence"""][0] lowerCAmelCase = model_outputs["""input_ids"""] lowerCAmelCase = model_outputs["""prompt_text"""] lowerCAmelCase = generated_sequence.numpy().tolist() lowerCAmelCase = [] for sequence in generated_sequence: if return_type == ReturnType.TENSORS: lowerCAmelCase = {"""generated_token_ids""": sequence} elif return_type in {ReturnType.NEW_TEXT, ReturnType.FULL_TEXT}: # Decode text lowerCAmelCase = self.tokenizer.decode( __lowerCAmelCase , skip_special_tokens=__lowerCAmelCase , clean_up_tokenization_spaces=__lowerCAmelCase , ) # Remove PADDING prompt of the sequence if XLNet or Transfo-XL model is used if input_ids is None: lowerCAmelCase = 0 else: lowerCAmelCase = len( self.tokenizer.decode( input_ids[0] , skip_special_tokens=__lowerCAmelCase , clean_up_tokenization_spaces=__lowerCAmelCase , )) if return_type == ReturnType.FULL_TEXT: lowerCAmelCase = prompt_text + text[prompt_length:] else: lowerCAmelCase = text[prompt_length:] lowerCAmelCase = {"""generated_text""": all_text} records.append(__lowerCAmelCase) return records
272
1
'''simple docstring''' import numpy as np import datasets lowerCamelCase = """ Compute the Mahalanobis Distance Mahalonobis distance is the distance between a point and a distribution. And not between two distinct points. It is effectively a multivariate equivalent of the Euclidean distance. It was introduced by Prof. P. C. Mahalanobis in 1936 and has been used in various statistical applications ever since [source: https://www.machinelearningplus.com/statistics/mahalanobis-distance/] """ lowerCamelCase = """\ @article{de2000mahalanobis, title={The mahalanobis distance}, author={De Maesschalck, Roy and Jouan-Rimbaud, Delphine and Massart, D{\'e}sir{\'e} L}, journal={Chemometrics and intelligent laboratory systems}, volume={50}, number={1}, pages={1--18}, year={2000}, publisher={Elsevier} } """ lowerCamelCase = """ Args: X: List of datapoints to be compared with the `reference_distribution`. reference_distribution: List of datapoints from the reference distribution we want to compare to. Returns: mahalanobis: The Mahalonobis distance for each datapoint in `X`. Examples: >>> mahalanobis_metric = datasets.load_metric(\"mahalanobis\") >>> results = mahalanobis_metric.compute(reference_distribution=[[0, 1], [1, 0]], X=[[0, 1]]) >>> print(results) {'mahalanobis': array([0.5])} """ @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class _UpperCamelCase ( datasets.Metric ): '''simple docstring''' def __lowerCamelCase ( self : List[Any]): '''simple docstring''' return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { 'X': datasets.Sequence(datasets.Value('float' , id='sequence') , id='X'), }) , ) def __lowerCamelCase ( self : List[Any] , _lowerCAmelCase : List[Any] , _lowerCAmelCase : List[str]): '''simple docstring''' __lowercase =np.array(_lowerCAmelCase) __lowercase =np.array(_lowerCAmelCase) # Assert that arrays are 2D if len(X.shape) != 2: raise ValueError('Expected `X` to be a 2D vector') if len(reference_distribution.shape) != 2: raise ValueError('Expected `reference_distribution` to be a 2D vector') if reference_distribution.shape[0] < 2: raise ValueError( 'Expected `reference_distribution` to be a 2D vector with more than one element in the first dimension') # Get mahalanobis distance for each prediction __lowercase =X - np.mean(_lowerCAmelCase) __lowercase =np.cov(reference_distribution.T) try: __lowercase =np.linalg.inv(_lowerCAmelCase) except np.linalg.LinAlgError: __lowercase =np.linalg.pinv(_lowerCAmelCase) __lowercase =np.dot(_lowerCAmelCase , _lowerCAmelCase) __lowercase =np.dot(_lowerCAmelCase , X_minus_mu.T).diagonal() return {"mahalanobis": mahal_dist}
369
'''simple docstring''' from __future__ import annotations import math import random from typing import Any class _UpperCamelCase : '''simple docstring''' def __init__( self : List[Any]): '''simple docstring''' __lowercase =[] __lowercase =0 __lowercase =0 def __lowerCamelCase ( self : List[Any]): '''simple docstring''' return self.head == self.tail def __lowerCamelCase ( self : str , _lowerCAmelCase : Any): '''simple docstring''' self.data.append(_lowerCAmelCase) __lowercase =self.tail + 1 def __lowerCamelCase ( self : Dict): '''simple docstring''' __lowercase =self.data[self.head] __lowercase =self.head + 1 return ret def __lowerCamelCase ( self : Optional[Any]): '''simple docstring''' return self.tail - self.head def __lowerCamelCase ( self : Optional[int]): '''simple docstring''' print(self.data) print('**************') print(self.data[self.head : self.tail]) class _UpperCamelCase : '''simple docstring''' def __init__( self : Optional[int] , _lowerCAmelCase : Any): '''simple docstring''' __lowercase =data __lowercase =None __lowercase =None __lowercase =1 def __lowerCamelCase ( self : Any): '''simple docstring''' return self.data def __lowerCamelCase ( self : Optional[int]): '''simple docstring''' return self.left def __lowerCamelCase ( self : Tuple): '''simple docstring''' return self.right def __lowerCamelCase ( self : Dict): '''simple docstring''' return self.height def __lowerCamelCase ( self : int , _lowerCAmelCase : Any): '''simple docstring''' __lowercase =data def __lowerCamelCase ( self : Optional[Any] , _lowerCAmelCase : MyNode | None): '''simple docstring''' __lowercase =node def __lowerCamelCase ( self : List[Any] , _lowerCAmelCase : MyNode | None): '''simple docstring''' __lowercase =node def __lowerCamelCase ( self : Optional[int] , _lowerCAmelCase : int): '''simple docstring''' __lowercase =height def _A ( _lowerCAmelCase ): """simple docstring""" if node is None: return 0 return node.get_height() def _A ( _lowerCAmelCase , _lowerCAmelCase ): """simple docstring""" if a > b: return a return b def _A ( _lowerCAmelCase ): """simple docstring""" print('left rotation node:' , node.get_data() ) __lowercase =node.get_left() assert ret is not None node.set_left(ret.get_right() ) ret.set_right(_lowerCAmelCase ) __lowercase =my_max(get_height(node.get_right() ) , get_height(node.get_left() ) ) + 1 node.set_height(_lowerCAmelCase ) __lowercase =my_max(get_height(ret.get_right() ) , get_height(ret.get_left() ) ) + 1 ret.set_height(_lowerCAmelCase ) return ret def _A ( _lowerCAmelCase ): """simple docstring""" print('right rotation node:' , node.get_data() ) __lowercase =node.get_right() assert ret is not None node.set_right(ret.get_left() ) ret.set_left(_lowerCAmelCase ) __lowercase =my_max(get_height(node.get_right() ) , get_height(node.get_left() ) ) + 1 node.set_height(_lowerCAmelCase ) __lowercase =my_max(get_height(ret.get_right() ) , get_height(ret.get_left() ) ) + 1 ret.set_height(_lowerCAmelCase ) return ret def _A ( _lowerCAmelCase ): """simple docstring""" __lowercase =node.get_left() assert left_child is not None node.set_left(left_rotation(_lowerCAmelCase ) ) return right_rotation(_lowerCAmelCase ) def _A ( _lowerCAmelCase ): """simple docstring""" __lowercase =node.get_right() assert right_child is not None node.set_right(right_rotation(_lowerCAmelCase ) ) return left_rotation(_lowerCAmelCase ) def _A ( _lowerCAmelCase , _lowerCAmelCase ): """simple docstring""" if node is None: return MyNode(_lowerCAmelCase ) if data < node.get_data(): node.set_left(insert_node(node.get_left() , _lowerCAmelCase ) ) if ( get_height(node.get_left() ) - get_height(node.get_right() ) == 2 ): # an unbalance detected __lowercase =node.get_left() assert left_child is not None if ( data < left_child.get_data() ): # new node is the left child of the left child __lowercase =right_rotation(_lowerCAmelCase ) else: __lowercase =lr_rotation(_lowerCAmelCase ) else: node.set_right(insert_node(node.get_right() , _lowerCAmelCase ) ) if get_height(node.get_right() ) - get_height(node.get_left() ) == 2: __lowercase =node.get_right() assert right_child is not None if data < right_child.get_data(): __lowercase =rl_rotation(_lowerCAmelCase ) else: __lowercase =left_rotation(_lowerCAmelCase ) __lowercase =my_max(get_height(node.get_right() ) , get_height(node.get_left() ) ) + 1 node.set_height(_lowerCAmelCase ) return node def _A ( _lowerCAmelCase ): """simple docstring""" while True: __lowercase =root.get_right() if right_child is None: break __lowercase =right_child return root.get_data() def _A ( _lowerCAmelCase ): """simple docstring""" while True: __lowercase =root.get_left() if left_child is None: break __lowercase =left_child return root.get_data() def _A ( _lowerCAmelCase , _lowerCAmelCase ): """simple docstring""" __lowercase =root.get_left() __lowercase =root.get_right() if root.get_data() == data: if left_child is not None and right_child is not None: __lowercase =get_left_most(_lowerCAmelCase ) root.set_data(_lowerCAmelCase ) root.set_right(del_node(_lowerCAmelCase , _lowerCAmelCase ) ) elif left_child is not None: __lowercase =left_child elif right_child is not None: __lowercase =right_child else: return None elif root.get_data() > data: if left_child is None: print('No such data' ) return root else: root.set_left(del_node(_lowerCAmelCase , _lowerCAmelCase ) ) else: # root.get_data() < data if right_child is None: return root else: root.set_right(del_node(_lowerCAmelCase , _lowerCAmelCase ) ) if get_height(_lowerCAmelCase ) - get_height(_lowerCAmelCase ) == 2: assert right_child is not None if get_height(right_child.get_right() ) > get_height(right_child.get_left() ): __lowercase =left_rotation(_lowerCAmelCase ) else: __lowercase =rl_rotation(_lowerCAmelCase ) elif get_height(_lowerCAmelCase ) - get_height(_lowerCAmelCase ) == -2: assert left_child is not None if get_height(left_child.get_left() ) > get_height(left_child.get_right() ): __lowercase =right_rotation(_lowerCAmelCase ) else: __lowercase =lr_rotation(_lowerCAmelCase ) __lowercase =my_max(get_height(root.get_right() ) , get_height(root.get_left() ) ) + 1 root.set_height(_lowerCAmelCase ) return root class _UpperCamelCase : '''simple docstring''' def __init__( self : Tuple): '''simple docstring''' __lowercase =None def __lowerCamelCase ( self : Optional[int]): '''simple docstring''' return get_height(self.root) def __lowerCamelCase ( self : List[str] , _lowerCAmelCase : Any): '''simple docstring''' print('insert:' + str(_lowerCAmelCase)) __lowercase =insert_node(self.root , _lowerCAmelCase) def __lowerCamelCase ( self : List[Any] , _lowerCAmelCase : Any): '''simple docstring''' print('delete:' + str(_lowerCAmelCase)) if self.root is None: print('Tree is empty!') return __lowercase =del_node(self.root , _lowerCAmelCase) def __str__( self : int , ): # a level traversale, gives a more intuitive look on the tree '''simple docstring''' __lowercase ='' __lowercase =MyQueue() q.push(self.root) __lowercase =self.get_height() if layer == 0: return output __lowercase =0 while not q.is_empty(): __lowercase =q.pop() __lowercase =' ' * int(math.pow(2 , layer - 1)) output += space if node is None: output += "*" q.push(_lowerCAmelCase) q.push(_lowerCAmelCase) else: output += str(node.get_data()) q.push(node.get_left()) q.push(node.get_right()) output += space __lowercase =cnt + 1 for i in range(1_0_0): if cnt == math.pow(2 , _lowerCAmelCase) - 1: __lowercase =layer - 1 if layer == 0: output += "\n*************************************" return output output += "\n" break output += "\n*************************************" return output def _A ( ): """simple docstring""" import doctest doctest.testmod() if __name__ == "__main__": _test() lowerCamelCase = AVLtree() lowerCamelCase = list(range(10)) random.shuffle(lst) for i in lst: t.insert(i) print(str(t)) random.shuffle(lst) for i in lst: t.del_node(i) print(str(t))
48
0
"""simple docstring""" from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_sentencepiece_available, is_tokenizers_available, is_torch_available, ) lowerCamelCase__ = {"""configuration_fnet""": ["""FNET_PRETRAINED_CONFIG_ARCHIVE_MAP""", """FNetConfig"""]} try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCamelCase__ = ["""FNetTokenizer"""] try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCamelCase__ = ["""FNetTokenizerFast"""] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCamelCase__ = [ """FNET_PRETRAINED_MODEL_ARCHIVE_LIST""", """FNetForMaskedLM""", """FNetForMultipleChoice""", """FNetForNextSentencePrediction""", """FNetForPreTraining""", """FNetForQuestionAnswering""", """FNetForSequenceClassification""", """FNetForTokenClassification""", """FNetLayer""", """FNetModel""", """FNetPreTrainedModel""", ] if TYPE_CHECKING: from .configuration_fnet import FNET_PRETRAINED_CONFIG_ARCHIVE_MAP, FNetConfig try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_fnet import FNetTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_fnet_fast import FNetTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_fnet import ( FNET_PRETRAINED_MODEL_ARCHIVE_LIST, FNetForMaskedLM, FNetForMultipleChoice, FNetForNextSentencePrediction, FNetForPreTraining, FNetForQuestionAnswering, FNetForSequenceClassification, FNetForTokenClassification, FNetLayer, FNetModel, FNetPreTrainedModel, ) else: import sys lowerCamelCase__ = _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)
86
"""simple docstring""" import gc import unittest import numpy as np import torch from diffusers import AutoencoderKL, DDIMScheduler, DiTPipeline, DPMSolverMultistepScheduler, TransformeraDModel from diffusers.utils import is_xformers_available, load_numpy, slow, torch_device from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu from ..pipeline_params import ( CLASS_CONDITIONED_IMAGE_GENERATION_BATCH_PARAMS, CLASS_CONDITIONED_IMAGE_GENERATION_PARAMS, ) from ..test_pipelines_common import PipelineTesterMixin enable_full_determinism() class A__ ( _lowerCamelCase , unittest.TestCase): A_ : Union[str, Any] = DiTPipeline A_ : Union[str, Any] = CLASS_CONDITIONED_IMAGE_GENERATION_PARAMS A_ : List[Any] = PipelineTesterMixin.required_optional_params - { 'latents', 'num_images_per_prompt', 'callback', 'callback_steps', } A_ : Optional[Any] = CLASS_CONDITIONED_IMAGE_GENERATION_BATCH_PARAMS A_ : Tuple = False def __lowerCamelCase ( self ): torch.manual_seed(0 ) __lowerCAmelCase : List[str] = TransformeraDModel( sample_size=16 , num_layers=2 , patch_size=4 , attention_head_dim=8 , num_attention_heads=2 , in_channels=4 , out_channels=8 , attention_bias=_SCREAMING_SNAKE_CASE , activation_fn='gelu-approximate' , num_embeds_ada_norm=10_00 , norm_type='ada_norm_zero' , norm_elementwise_affine=_SCREAMING_SNAKE_CASE , ) __lowerCAmelCase : str = AutoencoderKL() __lowerCAmelCase : Union[str, Any] = DDIMScheduler() __lowerCAmelCase : Dict = {'transformer': transformer.eval(), 'vae': vae.eval(), 'scheduler': scheduler} return components def __lowerCamelCase ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE=0 ): if str(_SCREAMING_SNAKE_CASE ).startswith('mps' ): __lowerCAmelCase : List[str] = torch.manual_seed(_SCREAMING_SNAKE_CASE ) else: __lowerCAmelCase : List[str] = torch.Generator(device=_SCREAMING_SNAKE_CASE ).manual_seed(_SCREAMING_SNAKE_CASE ) __lowerCAmelCase : Any = { 'class_labels': [1], 'generator': generator, 'num_inference_steps': 2, 'output_type': 'numpy', } return inputs def __lowerCamelCase ( self ): __lowerCAmelCase : List[str] = 'cpu' __lowerCAmelCase : Any = self.get_dummy_components() __lowerCAmelCase : Union[str, Any] = self.pipeline_class(**_SCREAMING_SNAKE_CASE ) pipe.to(_SCREAMING_SNAKE_CASE ) pipe.set_progress_bar_config(disable=_SCREAMING_SNAKE_CASE ) __lowerCAmelCase : Tuple = self.get_dummy_inputs(_SCREAMING_SNAKE_CASE ) __lowerCAmelCase : List[str] = pipe(**_SCREAMING_SNAKE_CASE ).images __lowerCAmelCase : List[str] = image[0, -3:, -3:, -1] self.assertEqual(image.shape , (1, 16, 16, 3) ) __lowerCAmelCase : Optional[int] = np.array([0.2946, 0.6601, 0.4329, 0.3296, 0.4144, 0.5319, 0.7273, 0.5013, 0.4457] ) __lowerCAmelCase : List[Any] = np.abs(image_slice.flatten() - expected_slice ).max() self.assertLessEqual(_SCREAMING_SNAKE_CASE , 1E-3 ) def __lowerCamelCase ( self ): self._test_inference_batch_single_identical(relax_max_difference=_SCREAMING_SNAKE_CASE , expected_max_diff=1E-3 ) @unittest.skipIf( torch_device != 'cuda' or not is_xformers_available() , reason='XFormers attention is only available with CUDA and `xformers` installed' , ) def __lowerCamelCase ( self ): self._test_xformers_attention_forwardGenerator_pass(expected_max_diff=1E-3 ) @require_torch_gpu @slow class A__ ( unittest.TestCase): def __lowerCamelCase ( self ): super().tearDown() gc.collect() torch.cuda.empty_cache() def __lowerCamelCase ( self ): __lowerCAmelCase : Dict = torch.manual_seed(0 ) __lowerCAmelCase : int = DiTPipeline.from_pretrained('facebook/DiT-XL-2-256' ) pipe.to('cuda' ) __lowerCAmelCase : Optional[Any] = ['vase', 'umbrella', 'white shark', 'white wolf'] __lowerCAmelCase : Optional[Any] = pipe.get_label_ids(_SCREAMING_SNAKE_CASE ) __lowerCAmelCase : Optional[Any] = pipe(_SCREAMING_SNAKE_CASE , generator=_SCREAMING_SNAKE_CASE , num_inference_steps=40 , output_type='np' ).images for word, image in zip(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ): __lowerCAmelCase : Tuple = load_numpy( f"https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/dit/{word}.npy" ) assert np.abs((expected_image - image).max() ) < 1E-2 def __lowerCamelCase ( self ): __lowerCAmelCase : Any = DiTPipeline.from_pretrained('facebook/DiT-XL-2-512' ) __lowerCAmelCase : Union[str, Any] = DPMSolverMultistepScheduler.from_config(pipe.scheduler.config ) pipe.to('cuda' ) __lowerCAmelCase : Dict = ['vase', 'umbrella'] __lowerCAmelCase : List[str] = pipe.get_label_ids(_SCREAMING_SNAKE_CASE ) __lowerCAmelCase : Any = torch.manual_seed(0 ) __lowerCAmelCase : Optional[Any] = pipe(_SCREAMING_SNAKE_CASE , generator=_SCREAMING_SNAKE_CASE , num_inference_steps=25 , output_type='np' ).images for word, image in zip(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ): __lowerCAmelCase : Dict = load_numpy( 'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main' f"/dit/{word}_512.npy" ) assert np.abs((expected_image - image).max() ) < 1E-1
86
1
from dataclasses import dataclass, field from typing import ClassVar, Dict from ..features import Features, Value from .base import TaskTemplate @dataclass(frozen=_lowercase ) class UpperCAmelCase_ ( _lowercase ): '''simple docstring''' _lowercase : Optional[int] = field(default='''language-modeling''' , metadata={'''include_in_asdict_even_if_is_default''': True} ) _lowercase : str = Features({'''text''': Value('''string''' )} ) _lowercase : Union[str, Any] = Features({} ) _lowercase : Any = '''text''' @property def _lowercase ( self ): """simple docstring""" return {self.text_column: "text"}
360
'''simple docstring''' from ...configuration_utils import PretrainedConfig class UpperCAmelCase_ ( _SCREAMING_SNAKE_CASE ): '''simple docstring''' _lowercase : Tuple = '''bert-generation''' def __init__( self , _lowercase=50_358 , _lowercase=1_024 , _lowercase=24 , _lowercase=16 , _lowercase=4_096 , _lowercase="gelu" , _lowercase=0.1 , _lowercase=0.1 , _lowercase=512 , _lowercase=0.02 , _lowercase=1e-12 , _lowercase=0 , _lowercase=2 , _lowercase=1 , _lowercase="absolute" , _lowercase=True , **_lowercase , ): """simple docstring""" super().__init__(pad_token_id=_lowercase , bos_token_id=_lowercase , eos_token_id=_lowercase , **_lowercase ) _lowerCAmelCase = vocab_size _lowerCAmelCase = hidden_size _lowerCAmelCase = num_hidden_layers _lowerCAmelCase = num_attention_heads _lowerCAmelCase = hidden_act _lowerCAmelCase = intermediate_size _lowerCAmelCase = hidden_dropout_prob _lowerCAmelCase = attention_probs_dropout_prob _lowerCAmelCase = max_position_embeddings _lowerCAmelCase = initializer_range _lowerCAmelCase = layer_norm_eps _lowerCAmelCase = position_embedding_type _lowerCAmelCase = use_cache
229
0
from typing import TYPE_CHECKING from ...file_utils import _LazyModule, is_tokenizers_available, is_torch_available from ...utils import OptionalDependencyNotAvailable lowerCAmelCase = {'''configuration_gpt_neox''': ['''GPT_NEOX_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''GPTNeoXConfig''']} try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase = ['''GPTNeoXTokenizerFast'''] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase = [ '''GPT_NEOX_PRETRAINED_MODEL_ARCHIVE_LIST''', '''GPTNeoXForCausalLM''', '''GPTNeoXForQuestionAnswering''', '''GPTNeoXForSequenceClassification''', '''GPTNeoXForTokenClassification''', '''GPTNeoXLayer''', '''GPTNeoXModel''', '''GPTNeoXPreTrainedModel''', ] if TYPE_CHECKING: from .configuration_gpt_neox import GPT_NEOX_PRETRAINED_CONFIG_ARCHIVE_MAP, GPTNeoXConfig try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_gpt_neox_fast import GPTNeoXTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_gpt_neox import ( GPT_NEOX_PRETRAINED_MODEL_ARCHIVE_LIST, GPTNeoXForCausalLM, GPTNeoXForQuestionAnswering, GPTNeoXForSequenceClassification, GPTNeoXForTokenClassification, GPTNeoXLayer, GPTNeoXModel, GPTNeoXPreTrainedModel, ) else: import sys lowerCAmelCase = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
295
from math import isqrt def _lowerCamelCase( lowercase__ ) -> bool: '''simple docstring''' return all(number % divisor != 0 for divisor in range(2 , isqrt(lowercase__ ) + 1 ) ) def _lowerCamelCase( lowercase__ = 1_0**6 ) -> int: '''simple docstring''' __lowercase= 0 __lowercase= 1 __lowercase= 7 while prime_candidate < max_prime: primes_count += is_prime(lowercase__ ) cube_index += 1 prime_candidate += 6 * cube_index return primes_count if __name__ == "__main__": print(F'{solution() = }')
295
1
"""simple docstring""" import os import sys import unittest _UpperCamelCase : Any = os.path.abspath(os.path.dirname(os.path.dirname(os.path.dirname(__file__)))) sys.path.append(os.path.join(git_repo_path, 'utils')) import check_dummies # noqa: E402 from check_dummies import create_dummy_files, create_dummy_object, find_backend, read_init # noqa: E402 # Align TRANSFORMERS_PATH in check_dummies with the current path _UpperCamelCase : List[Any] = os.path.join(git_repo_path, 'src', 'transformers') _UpperCamelCase : Union[str, Any] = '\n{0} = None\n' _UpperCamelCase : Dict = '\nclass {0}(metaclass=DummyObject):\n _backends = {1}\n\n def __init__(self, *args, **kwargs):\n requires_backends(self, {1})\n' _UpperCamelCase : List[Any] = '\ndef {0}(*args, **kwargs):\n requires_backends({0}, {1})\n' class snake_case ( unittest.TestCase ): def lowerCamelCase__ ( self : Optional[int] ): '''simple docstring''' a : List[Any] = find_backend(' _import_structure["models.albert"].append("AlbertTokenizerFast")' ) self.assertIsNone(A ) a : str = find_backend(' if not is_tokenizers_available():' ) self.assertEqual(A , 'tokenizers' ) a : List[str] = find_backend(' if not is_tensorflow_text_available():' ) self.assertEqual(A , 'tensorflow_text' ) a : str = find_backend(' if not (is_sentencepiece_available() and is_tokenizers_available()):' ) self.assertEqual(A , 'sentencepiece_and_tokenizers' ) a : Optional[int] = find_backend( ' if not (is_sentencepiece_available() and is_tensorflow_text_available()):' ) self.assertEqual(A , 'sentencepiece_and_tensorflow_text' ) a : List[Any] = find_backend( ' if not (is_sentencepiece_available() and is_tokenizers_available() and is_vision_available()):' ) self.assertEqual(A , 'sentencepiece_and_tokenizers_and_vision' ) def lowerCamelCase__ ( self : List[Any] ): '''simple docstring''' a : Optional[Any] = read_init() # We don't assert on the exact list of keys to allow for smooth grow of backend-specific objects self.assertIn('torch' , A ) self.assertIn('tensorflow_text' , A ) self.assertIn('sentencepiece_and_tokenizers' , A ) # Likewise, we can't assert on the exact content of a key self.assertIn('BertModel' , objects['torch'] ) self.assertIn('TFBertModel' , objects['tf'] ) self.assertIn('FlaxBertModel' , objects['flax'] ) self.assertIn('BertModel' , objects['torch'] ) self.assertIn('TFBertTokenizer' , objects['tensorflow_text'] ) self.assertIn('convert_slow_tokenizer' , objects['sentencepiece_and_tokenizers'] ) def lowerCamelCase__ ( self : int ): '''simple docstring''' a : Union[str, Any] = create_dummy_object('CONSTANT' , '\'torch\'' ) self.assertEqual(A , '\nCONSTANT = None\n' ) a : Optional[Any] = create_dummy_object('function' , '\'torch\'' ) self.assertEqual( A , '\ndef function(*args, **kwargs):\n requires_backends(function, \'torch\')\n' ) a : Tuple = '\nclass FakeClass(metaclass=DummyObject):\n _backends = \'torch\'\n\n def __init__(self, *args, **kwargs):\n requires_backends(self, \'torch\')\n' a : Optional[Any] = create_dummy_object('FakeClass' , '\'torch\'' ) self.assertEqual(A , A ) def lowerCamelCase__ ( self : Optional[int] ): '''simple docstring''' a : Dict = '# This file is autogenerated by the command `make fix-copies`, do not edit.\nfrom ..utils import DummyObject, requires_backends\n\n\nCONSTANT = None\n\n\ndef function(*args, **kwargs):\n requires_backends(function, ["torch"])\n\n\nclass FakeClass(metaclass=DummyObject):\n _backends = ["torch"]\n\n def __init__(self, *args, **kwargs):\n requires_backends(self, ["torch"])\n' a : Optional[int] = create_dummy_files({'torch': ['CONSTANT', 'function', 'FakeClass']} ) self.assertEqual(dummy_files['torch'] , A )
186
"""simple docstring""" import inspect import unittest import numpy as np from transformers import ViTConfig, is_flax_available from transformers.testing_utils import require_flax, slow from ...test_configuration_common import ConfigTester from ...test_modeling_flax_common import FlaxModelTesterMixin, floats_tensor if is_flax_available(): import jax from transformers.models.vit.modeling_flax_vit import FlaxViTForImageClassification, FlaxViTModel class snake_case ( unittest.TestCase ): def __init__( self : List[str] , A : Union[str, Any] , A : Optional[Any]=1_3 , A : List[Any]=3_0 , A : List[Any]=2 , A : Optional[Any]=3 , A : Union[str, Any]=True , A : Union[str, Any]=True , A : Optional[int]=3_2 , A : Tuple=5 , A : List[str]=4 , A : List[Any]=3_7 , A : Optional[Any]="gelu" , A : Any=0.1 , A : Tuple=0.1 , A : Optional[int]=1_0 , A : Union[str, Any]=0.02 , ): '''simple docstring''' a : Optional[Any] = parent a : Tuple = batch_size a : int = image_size a : str = patch_size a : List[str] = num_channels a : List[str] = is_training a : List[str] = use_labels a : Optional[int] = hidden_size a : Optional[Any] = num_hidden_layers a : Optional[int] = num_attention_heads a : str = intermediate_size a : List[str] = hidden_act a : List[str] = hidden_dropout_prob a : Union[str, Any] = attention_probs_dropout_prob a : List[Any] = type_sequence_label_size a : Optional[Any] = initializer_range # in ViT, the seq length equals the number of patches + 1 (we add 1 for the [CLS] token) a : Optional[int] = (image_size // patch_size) ** 2 a : List[Any] = num_patches + 1 def lowerCamelCase__ ( self : Tuple ): '''simple docstring''' a : List[Any] = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) a : str = ViTConfig( image_size=self.image_size , patch_size=self.patch_size , num_channels=self.num_channels , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , is_decoder=A , initializer_range=self.initializer_range , ) return config, pixel_values def lowerCamelCase__ ( self : Union[str, Any] , A : str , A : Union[str, Any] ): '''simple docstring''' a : Tuple = FlaxViTModel(config=A ) a : int = model(A ) # expected sequence length = num_patches + 1 (we add 1 for the [CLS] token) a : Optional[Any] = (self.image_size, self.image_size) a : List[str] = (self.patch_size, self.patch_size) a : Optional[Any] = (image_size[1] // patch_size[1]) * (image_size[0] // patch_size[0]) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, num_patches + 1, self.hidden_size) ) def lowerCamelCase__ ( self : Tuple , A : Dict , A : Optional[int] ): '''simple docstring''' a : Optional[Any] = self.type_sequence_label_size a : List[Any] = FlaxViTForImageClassification(config=A ) a : Tuple = model(A ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.type_sequence_label_size) ) # test greyscale images a : Dict = 1 a : Tuple = FlaxViTForImageClassification(A ) a : List[Any] = floats_tensor([self.batch_size, 1, self.image_size, self.image_size] ) a : Optional[int] = model(A ) def lowerCamelCase__ ( self : Optional[int] ): '''simple docstring''' a : Optional[int] = self.prepare_config_and_inputs() ( ( a ), ( a ), ) : Dict = config_and_inputs a : List[Any] = {'pixel_values': pixel_values} return config, inputs_dict @require_flax class snake_case ( UpperCAmelCase , unittest.TestCase ): __magic_name__ = (FlaxViTModel, FlaxViTForImageClassification) if is_flax_available() else () def lowerCamelCase__ ( self : Any ): '''simple docstring''' a : Any = FlaxViTModelTester(self ) a : List[str] = ConfigTester(self , config_class=A , has_text_modality=A , hidden_size=3_7 ) def lowerCamelCase__ ( self : Tuple ): '''simple docstring''' self.config_tester.run_common_tests() def lowerCamelCase__ ( self : List[Any] ): '''simple docstring''' a : Union[str, Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*A ) def lowerCamelCase__ ( self : List[Any] ): '''simple docstring''' a : Union[str, Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_image_classification(*A ) def lowerCamelCase__ ( self : Dict ): '''simple docstring''' a, a : Optional[Any] = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: a : Tuple = model_class(A ) a : str = inspect.signature(model.__call__ ) # signature.parameters is an OrderedDict => so arg_names order is deterministic a : List[str] = [*signature.parameters.keys()] a : Dict = ['pixel_values'] self.assertListEqual(arg_names[:1] , A ) def lowerCamelCase__ ( self : Any ): '''simple docstring''' a, a : Tuple = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: with self.subTest(model_class.__name__ ): a : List[Any] = self._prepare_for_class(A , A ) a : Tuple = model_class(A ) @jax.jit def model_jitted(A : Tuple , **A : int ): return model(pixel_values=A , **A ) with self.subTest('JIT Enabled' ): a : List[str] = model_jitted(**A ).to_tuple() with self.subTest('JIT Disabled' ): with jax.disable_jit(): a : List[str] = model_jitted(**A ).to_tuple() self.assertEqual(len(A ) , len(A ) ) for jitted_output, output in zip(A , A ): self.assertEqual(jitted_output.shape , output.shape ) @slow def lowerCamelCase__ ( self : List[str] ): '''simple docstring''' for model_class_name in self.all_model_classes: a : List[str] = model_class_name.from_pretrained('google/vit-base-patch16-224' ) a : Optional[Any] = model(np.ones((1, 3, 2_2_4, 2_2_4) ) ) self.assertIsNotNone(A )
186
1
import logging from dataclasses import dataclass, field from typing import Optional from seqaseq_trainer import arg_to_scheduler from transformers import TrainingArguments UpperCAmelCase_ : Union[str, Any] = logging.getLogger(__name__) @dataclass class SCREAMING_SNAKE_CASE__ ( lowercase__ ): snake_case__ : Optional[float] = field( default=0.0 , metadata={'''help''': '''The label smoothing epsilon to apply (if not zero).'''} ) snake_case__ : bool = field(default=lowercase__ , metadata={'''help''': '''Whether to SortishSamler or not.'''} ) snake_case__ : bool = field( default=lowercase__ , metadata={'''help''': '''Whether to use generate to calculate generative metrics (ROUGE, BLEU).'''} ) snake_case__ : bool = field(default=lowercase__ , metadata={'''help''': '''whether to use adafactor'''} ) snake_case__ : Optional[float] = field( default=lowercase__ , metadata={'''help''': '''Encoder layer dropout probability. Goes into model.config.'''} ) snake_case__ : Optional[float] = field( default=lowercase__ , metadata={'''help''': '''Decoder layer dropout probability. Goes into model.config.'''} ) snake_case__ : Optional[float] = field(default=lowercase__ , metadata={'''help''': '''Dropout probability. Goes into model.config.'''} ) snake_case__ : Optional[float] = field( default=lowercase__ , metadata={'''help''': '''Attention dropout probability. Goes into model.config.'''} ) snake_case__ : Optional[str] = field( default='''linear''' , metadata={'''help''': f"Which lr scheduler to use. Selected in {sorted(arg_to_scheduler.keys() )}"} , )
32
def SCREAMING_SNAKE_CASE_ ( __A : list[int] , __A : str ) -> list[int]: """simple docstring""" a_ : Any = int(__A ) # Initialize Result a_ : Tuple = [] # Traverse through all denomination for denomination in reversed(__A ): # Find denominations while int(__A ) >= int(__A ): total_value -= int(__A ) answer.append(__A ) # Append the "answers" array return answer # Driver Code if __name__ == "__main__": UpperCAmelCase_ : Union[str, Any] = [] UpperCAmelCase_ : Union[str, Any] = '0' if ( input('Do you want to enter your denominations ? (yY/n): ').strip().lower() == "y" ): UpperCAmelCase_ : List[Any] = int(input('Enter the number of denominations you want to add: ').strip()) for i in range(0, n): denominations.append(int(input(F'Denomination {i}: ').strip())) UpperCAmelCase_ : str = input('Enter the change you want to make in Indian Currency: ').strip() else: # All denominations of Indian Currency if user does not enter UpperCAmelCase_ : List[Any] = [1, 2, 5, 10, 20, 50, 100, 500, 2000] UpperCAmelCase_ : str = input('Enter the change you want to make: ').strip() if int(value) == 0 or int(value) < 0: print('The total value cannot be zero or negative.') else: print(F'Following is minimal change for {value}: ') UpperCAmelCase_ : Optional[Any] = find_minimum_change(denominations, value) # Print result for i in range(len(answer)): print(answer[i], end=' ')
32
1
"""simple docstring""" def _A ( UpperCamelCase_ : int = 200) -> int: '''simple docstring''' __lowercase = [1, 2, 5, 10, 20, 50, 100, 200] __lowercase = [0] * (pence + 1) __lowercase = 1 # base case: 1 way to make 0 pence for coin in coins: for i in range(UpperCamelCase_, pence + 1, 1): number_of_ways[i] += number_of_ways[i - coin] return number_of_ways[pence] if __name__ == "__main__": assert solution(2_00) == 7_36_82
144
"""simple docstring""" import doctest import glob import importlib import inspect import os import re from contextlib import contextmanager from functools import wraps from unittest.mock import patch import numpy as np import pytest from absl.testing import parameterized import datasets from datasets import load_metric from .utils import for_all_test_methods, local, slow # mark all tests as integration _a = pytest.mark.integration _a = {'comet'} _a = importlib.util.find_spec('fairseq') is not None _a = {'code_eval'} _a = os.name == 'nt' _a = {'bertscore', 'frugalscore', 'perplexity'} _a = importlib.util.find_spec('transformers') is not None def _A ( UpperCamelCase_ : Dict) -> Any: '''simple docstring''' @wraps(UpperCamelCase_) def wrapper(self : Dict, UpperCamelCase_ : Dict): if not _has_fairseq and metric_name in REQUIRE_FAIRSEQ: self.skipTest("\"test requires Fairseq\"") else: test_case(self, UpperCamelCase_) return wrapper def _A ( UpperCamelCase_ : Dict) -> int: '''simple docstring''' @wraps(UpperCamelCase_) def wrapper(self : int, UpperCamelCase_ : str): if not _has_transformers and metric_name in REQUIRE_TRANSFORMERS: self.skipTest("\"test requires transformers\"") else: test_case(self, UpperCamelCase_) return wrapper def _A ( UpperCamelCase_ : Tuple) -> str: '''simple docstring''' @wraps(UpperCamelCase_) def wrapper(self : Optional[int], UpperCamelCase_ : Optional[Any]): if _on_windows and metric_name in UNSUPPORTED_ON_WINDOWS: self.skipTest("\"test not supported on Windows\"") else: test_case(self, UpperCamelCase_) return wrapper def _A ( ) -> str: '''simple docstring''' __lowercase = [metric_dir.split(os.sep)[-2] for metric_dir in glob.glob("./metrics/*/")] return [{"testcase_name": x, "metric_name": x} for x in metrics if x != "gleu"] # gleu is unfinished @parameterized.named_parameters(get_local_metric_names() ) @for_all_test_methods( lowercase ,lowercase ,lowercase ) @local class _lowerCAmelCase ( parameterized.TestCase ): """simple docstring""" __UpperCAmelCase : Optional[int] = {} __UpperCAmelCase : Tuple = None @pytest.mark.filterwarnings("ignore:metric_module_factory is deprecated:FutureWarning" ) @pytest.mark.filterwarnings("ignore:load_metric is deprecated:FutureWarning" ) def _lowercase ( self : Dict, UpperCAmelCase__ : int ): __lowercase = "[...]" __lowercase = importlib.import_module( datasets.load.metric_module_factory(os.path.join("metrics", UpperCAmelCase__ ) ).module_path ) __lowercase = datasets.load.import_main_class(metric_module.__name__, dataset=UpperCAmelCase__ ) # check parameters __lowercase = inspect.signature(metric._compute ).parameters self.assertTrue(all(p.kind != p.VAR_KEYWORD for p in parameters.values() ) ) # no **kwargs # run doctest with self.patch_intensive_calls(UpperCAmelCase__, metric_module.__name__ ): with self.use_local_metrics(): try: __lowercase = doctest.testmod(UpperCAmelCase__, verbose=UpperCAmelCase__, raise_on_error=UpperCAmelCase__ ) except doctest.UnexpectedException as e: raise e.exc_info[1] # raise the exception that doctest caught self.assertEqual(results.failed, 0 ) self.assertGreater(results.attempted, 1 ) @slow def _lowercase ( self : List[Any], UpperCAmelCase__ : Optional[Any] ): __lowercase = "[...]" __lowercase = importlib.import_module( datasets.load.metric_module_factory(os.path.join("metrics", UpperCAmelCase__ ) ).module_path ) # run doctest with self.use_local_metrics(): __lowercase = doctest.testmod(UpperCAmelCase__, verbose=UpperCAmelCase__, raise_on_error=UpperCAmelCase__ ) self.assertEqual(results.failed, 0 ) self.assertGreater(results.attempted, 1 ) @contextmanager def _lowercase ( self : List[Any], UpperCAmelCase__ : Any, UpperCAmelCase__ : Tuple ): if metric_name in self.INTENSIVE_CALLS_PATCHER: with self.INTENSIVE_CALLS_PATCHER[metric_name](UpperCAmelCase__ ): yield else: yield @contextmanager def _lowercase ( self : List[Any] ): def load_local_metric(UpperCAmelCase__ : Any, *UpperCAmelCase__ : List[Any], **UpperCAmelCase__ : Any ): return load_metric(os.path.join("metrics", UpperCAmelCase__ ), *UpperCAmelCase__, **UpperCAmelCase__ ) with patch("datasets.load_metric" ) as mock_load_metric: __lowercase = load_local_metric yield @classmethod def _lowercase ( cls : Optional[Any], UpperCAmelCase__ : List[Any] ): def wrapper(UpperCAmelCase__ : Tuple ): __lowercase = contextmanager(UpperCAmelCase__ ) __lowercase = patcher return patcher return wrapper @LocalMetricTest.register_intensive_calls_patcher("bleurt") def _A ( UpperCamelCase_ : Any) -> Optional[Any]: '''simple docstring''' import tensorflow.compat.va as tf from bleurt.score import Predictor tf.flags.DEFINE_string("sv", "", "") # handle pytest cli flags class _lowerCAmelCase ( lowercase ): """simple docstring""" def _lowercase ( self : Tuple, UpperCAmelCase__ : Tuple ): assert len(input_dict["input_ids"] ) == 2 return np.array([1.03, 1.04] ) # mock predict_fn which is supposed to do a forward pass with a bleurt model with patch("bleurt.score._create_predictor") as mock_create_predictor: __lowercase = MockedPredictor() yield @LocalMetricTest.register_intensive_calls_patcher("bertscore") def _A ( UpperCamelCase_ : Tuple) -> int: '''simple docstring''' import torch def bert_cos_score_idf(UpperCamelCase_ : Tuple, UpperCamelCase_ : str, *UpperCamelCase_ : Optional[Any], **UpperCamelCase_ : Dict): return torch.tensor([[1.0, 1.0, 1.0]] * len(UpperCamelCase_)) # mock get_model which is supposed to do download a bert model # mock bert_cos_score_idf which is supposed to do a forward pass with a bert model with patch("bert_score.scorer.get_model"), patch( "bert_score.scorer.bert_cos_score_idf") as mock_bert_cos_score_idf: __lowercase = bert_cos_score_idf yield @LocalMetricTest.register_intensive_calls_patcher("comet") def _A ( UpperCamelCase_ : Tuple) -> List[Any]: '''simple docstring''' def load_from_checkpoint(UpperCamelCase_ : Tuple): class _lowerCAmelCase : """simple docstring""" def _lowercase ( self : str, UpperCAmelCase__ : int, *UpperCAmelCase__ : Dict, **UpperCAmelCase__ : Dict ): assert len(UpperCAmelCase__ ) == 2 __lowercase = [0.19, 0.92] return scores, sum(UpperCAmelCase__ ) / len(UpperCAmelCase__ ) return Model() # mock load_from_checkpoint which is supposed to do download a bert model # mock load_from_checkpoint which is supposed to do download a bert model with patch("comet.download_model") as mock_download_model: __lowercase = None with patch("comet.load_from_checkpoint") as mock_load_from_checkpoint: __lowercase = load_from_checkpoint yield def _A ( ) -> Tuple: '''simple docstring''' __lowercase = load_metric(os.path.join("metrics", "seqeval")) __lowercase = "ERROR" __lowercase = F"""Scheme should be one of [IOB1, IOB2, IOE1, IOE2, IOBES, BILOU], got {wrong_scheme}""" with pytest.raises(UpperCamelCase_, match=re.escape(UpperCamelCase_)): metric.compute(predictions=[], references=[], scheme=UpperCamelCase_)
144
1
def __magic_name__ ( __a : str ): '''simple docstring''' if n_term == "": return [] UpperCamelCase__ = [] for temp in range(int(__a ) ): series.append(f"1/{temp + 1}" if series else """1""" ) return series if __name__ == "__main__": lowerCamelCase_ = input('''Enter the last number (nth term) of the Harmonic Series''') print('''Formula of Harmonic Series => 1+1/2+1/3 ..... 1/n''') print(harmonic_series(nth_term))
244
from typing import Any, Callable, Dict, List, Optional, Union import torch from transformers import CLIPImageProcessor, CLIPTextModel, CLIPTokenizer from diffusers import ( AutoencoderKL, DDIMScheduler, DiffusionPipeline, LMSDiscreteScheduler, PNDMScheduler, StableDiffusionPipeline, UNetaDConditionModel, ) from diffusers.pipelines.stable_diffusion import StableDiffusionPipelineOutput from diffusers.pipelines.stable_diffusion.safety_checker import StableDiffusionSafetyChecker lowerCamelCase_ = '''CompVis/stable-diffusion-v1-1''' lowerCamelCase_ = '''CompVis/stable-diffusion-v1-2''' lowerCamelCase_ = '''CompVis/stable-diffusion-v1-3''' lowerCamelCase_ = '''CompVis/stable-diffusion-v1-4''' class __A( __lowerCamelCase ): """simple docstring""" def __init__(self , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = True , ): super()._init_() UpperCamelCase__ = StableDiffusionPipeline.from_pretrained(SCREAMING_SNAKE_CASE_ ) UpperCamelCase__ = StableDiffusionPipeline.from_pretrained(SCREAMING_SNAKE_CASE_ ) UpperCamelCase__ = StableDiffusionPipeline.from_pretrained(SCREAMING_SNAKE_CASE_ ) UpperCamelCase__ = StableDiffusionPipeline( vae=SCREAMING_SNAKE_CASE_ , text_encoder=SCREAMING_SNAKE_CASE_ , tokenizer=SCREAMING_SNAKE_CASE_ , unet=SCREAMING_SNAKE_CASE_ , scheduler=SCREAMING_SNAKE_CASE_ , safety_checker=SCREAMING_SNAKE_CASE_ , feature_extractor=SCREAMING_SNAKE_CASE_ , requires_safety_checker=SCREAMING_SNAKE_CASE_ , ) self.register_modules(pipelinea=self.pipea , pipelinea=self.pipea , pipelinea=self.pipea , pipelinea=self.pipea ) @property def UpperCAmelCase_ (self ): return {k: getattr(self , SCREAMING_SNAKE_CASE_ ) for k in self.config.keys() if not k.startswith("""_""" )} def UpperCAmelCase_ (self , SCREAMING_SNAKE_CASE_ = "auto" ): if slice_size == "auto": # half the attention head size is usually a good trade-off between # speed and memory UpperCamelCase__ = self.unet.config.attention_head_dim // 2 self.unet.set_attention_slice(SCREAMING_SNAKE_CASE_ ) def UpperCAmelCase_ (self ): self.enable_attention_slicing(SCREAMING_SNAKE_CASE_ ) @torch.no_grad() def UpperCAmelCase_ (self , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = 5_12 , SCREAMING_SNAKE_CASE_ = 5_12 , SCREAMING_SNAKE_CASE_ = 50 , SCREAMING_SNAKE_CASE_ = 7.5 , SCREAMING_SNAKE_CASE_ = None , SCREAMING_SNAKE_CASE_ = 1 , SCREAMING_SNAKE_CASE_ = 0.0 , SCREAMING_SNAKE_CASE_ = None , SCREAMING_SNAKE_CASE_ = None , SCREAMING_SNAKE_CASE_ = "pil" , SCREAMING_SNAKE_CASE_ = True , SCREAMING_SNAKE_CASE_ = None , SCREAMING_SNAKE_CASE_ = 1 , **SCREAMING_SNAKE_CASE_ , ): return self.pipea( prompt=SCREAMING_SNAKE_CASE_ , height=SCREAMING_SNAKE_CASE_ , width=SCREAMING_SNAKE_CASE_ , num_inference_steps=SCREAMING_SNAKE_CASE_ , guidance_scale=SCREAMING_SNAKE_CASE_ , negative_prompt=SCREAMING_SNAKE_CASE_ , num_images_per_prompt=SCREAMING_SNAKE_CASE_ , eta=SCREAMING_SNAKE_CASE_ , generator=SCREAMING_SNAKE_CASE_ , latents=SCREAMING_SNAKE_CASE_ , output_type=SCREAMING_SNAKE_CASE_ , return_dict=SCREAMING_SNAKE_CASE_ , callback=SCREAMING_SNAKE_CASE_ , callback_steps=SCREAMING_SNAKE_CASE_ , **SCREAMING_SNAKE_CASE_ , ) @torch.no_grad() def UpperCAmelCase_ (self , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = 5_12 , SCREAMING_SNAKE_CASE_ = 5_12 , SCREAMING_SNAKE_CASE_ = 50 , SCREAMING_SNAKE_CASE_ = 7.5 , SCREAMING_SNAKE_CASE_ = None , SCREAMING_SNAKE_CASE_ = 1 , SCREAMING_SNAKE_CASE_ = 0.0 , SCREAMING_SNAKE_CASE_ = None , SCREAMING_SNAKE_CASE_ = None , SCREAMING_SNAKE_CASE_ = "pil" , SCREAMING_SNAKE_CASE_ = True , SCREAMING_SNAKE_CASE_ = None , SCREAMING_SNAKE_CASE_ = 1 , **SCREAMING_SNAKE_CASE_ , ): return self.pipea( prompt=SCREAMING_SNAKE_CASE_ , height=SCREAMING_SNAKE_CASE_ , width=SCREAMING_SNAKE_CASE_ , num_inference_steps=SCREAMING_SNAKE_CASE_ , guidance_scale=SCREAMING_SNAKE_CASE_ , negative_prompt=SCREAMING_SNAKE_CASE_ , num_images_per_prompt=SCREAMING_SNAKE_CASE_ , eta=SCREAMING_SNAKE_CASE_ , generator=SCREAMING_SNAKE_CASE_ , latents=SCREAMING_SNAKE_CASE_ , output_type=SCREAMING_SNAKE_CASE_ , return_dict=SCREAMING_SNAKE_CASE_ , callback=SCREAMING_SNAKE_CASE_ , callback_steps=SCREAMING_SNAKE_CASE_ , **SCREAMING_SNAKE_CASE_ , ) @torch.no_grad() def UpperCAmelCase_ (self , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = 5_12 , SCREAMING_SNAKE_CASE_ = 5_12 , SCREAMING_SNAKE_CASE_ = 50 , SCREAMING_SNAKE_CASE_ = 7.5 , SCREAMING_SNAKE_CASE_ = None , SCREAMING_SNAKE_CASE_ = 1 , SCREAMING_SNAKE_CASE_ = 0.0 , SCREAMING_SNAKE_CASE_ = None , SCREAMING_SNAKE_CASE_ = None , SCREAMING_SNAKE_CASE_ = "pil" , SCREAMING_SNAKE_CASE_ = True , SCREAMING_SNAKE_CASE_ = None , SCREAMING_SNAKE_CASE_ = 1 , **SCREAMING_SNAKE_CASE_ , ): return self.pipea( prompt=SCREAMING_SNAKE_CASE_ , height=SCREAMING_SNAKE_CASE_ , width=SCREAMING_SNAKE_CASE_ , num_inference_steps=SCREAMING_SNAKE_CASE_ , guidance_scale=SCREAMING_SNAKE_CASE_ , negative_prompt=SCREAMING_SNAKE_CASE_ , num_images_per_prompt=SCREAMING_SNAKE_CASE_ , eta=SCREAMING_SNAKE_CASE_ , generator=SCREAMING_SNAKE_CASE_ , latents=SCREAMING_SNAKE_CASE_ , output_type=SCREAMING_SNAKE_CASE_ , return_dict=SCREAMING_SNAKE_CASE_ , callback=SCREAMING_SNAKE_CASE_ , callback_steps=SCREAMING_SNAKE_CASE_ , **SCREAMING_SNAKE_CASE_ , ) @torch.no_grad() def UpperCAmelCase_ (self , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = 5_12 , SCREAMING_SNAKE_CASE_ = 5_12 , SCREAMING_SNAKE_CASE_ = 50 , SCREAMING_SNAKE_CASE_ = 7.5 , SCREAMING_SNAKE_CASE_ = None , SCREAMING_SNAKE_CASE_ = 1 , SCREAMING_SNAKE_CASE_ = 0.0 , SCREAMING_SNAKE_CASE_ = None , SCREAMING_SNAKE_CASE_ = None , SCREAMING_SNAKE_CASE_ = "pil" , SCREAMING_SNAKE_CASE_ = True , SCREAMING_SNAKE_CASE_ = None , SCREAMING_SNAKE_CASE_ = 1 , **SCREAMING_SNAKE_CASE_ , ): return self.pipea( prompt=SCREAMING_SNAKE_CASE_ , height=SCREAMING_SNAKE_CASE_ , width=SCREAMING_SNAKE_CASE_ , num_inference_steps=SCREAMING_SNAKE_CASE_ , guidance_scale=SCREAMING_SNAKE_CASE_ , negative_prompt=SCREAMING_SNAKE_CASE_ , num_images_per_prompt=SCREAMING_SNAKE_CASE_ , eta=SCREAMING_SNAKE_CASE_ , generator=SCREAMING_SNAKE_CASE_ , latents=SCREAMING_SNAKE_CASE_ , output_type=SCREAMING_SNAKE_CASE_ , return_dict=SCREAMING_SNAKE_CASE_ , callback=SCREAMING_SNAKE_CASE_ , callback_steps=SCREAMING_SNAKE_CASE_ , **SCREAMING_SNAKE_CASE_ , ) @torch.no_grad() def UpperCAmelCase_ (self , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = 5_12 , SCREAMING_SNAKE_CASE_ = 5_12 , SCREAMING_SNAKE_CASE_ = 50 , SCREAMING_SNAKE_CASE_ = 7.5 , SCREAMING_SNAKE_CASE_ = None , SCREAMING_SNAKE_CASE_ = 1 , SCREAMING_SNAKE_CASE_ = 0.0 , SCREAMING_SNAKE_CASE_ = None , SCREAMING_SNAKE_CASE_ = None , SCREAMING_SNAKE_CASE_ = "pil" , SCREAMING_SNAKE_CASE_ = True , SCREAMING_SNAKE_CASE_ = None , SCREAMING_SNAKE_CASE_ = 1 , **SCREAMING_SNAKE_CASE_ , ): UpperCamelCase__ = """cuda""" if torch.cuda.is_available() else """cpu""" self.to(SCREAMING_SNAKE_CASE_ ) # Checks if the height and width are divisible by 8 or not if height % 8 != 0 or width % 8 != 0: raise ValueError(F"`height` and `width` must be divisible by 8 but are {height} and {width}." ) # Get first result from Stable Diffusion Checkpoint v1.1 UpperCamelCase__ = self.textaimg_sda_a( prompt=SCREAMING_SNAKE_CASE_ , height=SCREAMING_SNAKE_CASE_ , width=SCREAMING_SNAKE_CASE_ , num_inference_steps=SCREAMING_SNAKE_CASE_ , guidance_scale=SCREAMING_SNAKE_CASE_ , negative_prompt=SCREAMING_SNAKE_CASE_ , num_images_per_prompt=SCREAMING_SNAKE_CASE_ , eta=SCREAMING_SNAKE_CASE_ , generator=SCREAMING_SNAKE_CASE_ , latents=SCREAMING_SNAKE_CASE_ , output_type=SCREAMING_SNAKE_CASE_ , return_dict=SCREAMING_SNAKE_CASE_ , callback=SCREAMING_SNAKE_CASE_ , callback_steps=SCREAMING_SNAKE_CASE_ , **SCREAMING_SNAKE_CASE_ , ) # Get first result from Stable Diffusion Checkpoint v1.2 UpperCamelCase__ = self.textaimg_sda_a( prompt=SCREAMING_SNAKE_CASE_ , height=SCREAMING_SNAKE_CASE_ , width=SCREAMING_SNAKE_CASE_ , num_inference_steps=SCREAMING_SNAKE_CASE_ , guidance_scale=SCREAMING_SNAKE_CASE_ , negative_prompt=SCREAMING_SNAKE_CASE_ , num_images_per_prompt=SCREAMING_SNAKE_CASE_ , eta=SCREAMING_SNAKE_CASE_ , generator=SCREAMING_SNAKE_CASE_ , latents=SCREAMING_SNAKE_CASE_ , output_type=SCREAMING_SNAKE_CASE_ , return_dict=SCREAMING_SNAKE_CASE_ , callback=SCREAMING_SNAKE_CASE_ , callback_steps=SCREAMING_SNAKE_CASE_ , **SCREAMING_SNAKE_CASE_ , ) # Get first result from Stable Diffusion Checkpoint v1.3 UpperCamelCase__ = self.textaimg_sda_a( prompt=SCREAMING_SNAKE_CASE_ , height=SCREAMING_SNAKE_CASE_ , width=SCREAMING_SNAKE_CASE_ , num_inference_steps=SCREAMING_SNAKE_CASE_ , guidance_scale=SCREAMING_SNAKE_CASE_ , negative_prompt=SCREAMING_SNAKE_CASE_ , num_images_per_prompt=SCREAMING_SNAKE_CASE_ , eta=SCREAMING_SNAKE_CASE_ , generator=SCREAMING_SNAKE_CASE_ , latents=SCREAMING_SNAKE_CASE_ , output_type=SCREAMING_SNAKE_CASE_ , return_dict=SCREAMING_SNAKE_CASE_ , callback=SCREAMING_SNAKE_CASE_ , callback_steps=SCREAMING_SNAKE_CASE_ , **SCREAMING_SNAKE_CASE_ , ) # Get first result from Stable Diffusion Checkpoint v1.4 UpperCamelCase__ = self.textaimg_sda_a( prompt=SCREAMING_SNAKE_CASE_ , height=SCREAMING_SNAKE_CASE_ , width=SCREAMING_SNAKE_CASE_ , num_inference_steps=SCREAMING_SNAKE_CASE_ , guidance_scale=SCREAMING_SNAKE_CASE_ , negative_prompt=SCREAMING_SNAKE_CASE_ , num_images_per_prompt=SCREAMING_SNAKE_CASE_ , eta=SCREAMING_SNAKE_CASE_ , generator=SCREAMING_SNAKE_CASE_ , latents=SCREAMING_SNAKE_CASE_ , output_type=SCREAMING_SNAKE_CASE_ , return_dict=SCREAMING_SNAKE_CASE_ , callback=SCREAMING_SNAKE_CASE_ , callback_steps=SCREAMING_SNAKE_CASE_ , **SCREAMING_SNAKE_CASE_ , ) # Get all result images into a single list and pass it via StableDiffusionPipelineOutput for final result return StableDiffusionPipelineOutput([resa[0], resa[0], resa[0], resa[0]] )
244
1
'''simple docstring''' import random import unittest import numpy as np import torch from diffusers import ( DPMSolverMultistepScheduler, EulerAncestralDiscreteScheduler, EulerDiscreteScheduler, LMSDiscreteScheduler, OnnxStableDiffusionUpscalePipeline, PNDMScheduler, ) from diffusers.utils import floats_tensor from diffusers.utils.testing_utils import ( is_onnx_available, load_image, nightly, require_onnxruntime, require_torch_gpu, ) from ..test_pipelines_onnx_common import OnnxPipelineTesterMixin if is_onnx_available(): import onnxruntime as ort class snake_case__ ( __SCREAMING_SNAKE_CASE , unittest.TestCase ): """simple docstring""" lowerCamelCase = """ssube/stable-diffusion-x4-upscaler-onnx""" def lowerCAmelCase ( self : Any , UpperCamelCase__ : Union[str, Any]=0 ) -> Dict: """simple docstring""" snake_case : Dict = floats_tensor((1, 3, 128, 128) , rng=random.Random(UpperCamelCase__ ) ) snake_case : Any = torch.manual_seed(UpperCamelCase__ ) snake_case : int = { '''prompt''': '''A painting of a squirrel eating a burger''', '''image''': image, '''generator''': generator, '''num_inference_steps''': 3, '''guidance_scale''': 7.5, '''output_type''': '''numpy''', } return inputs def lowerCAmelCase ( self : Dict ) -> Optional[Any]: """simple docstring""" snake_case : Optional[Any] = OnnxStableDiffusionUpscalePipeline.from_pretrained(self.hub_checkpoint , provider='''CPUExecutionProvider''' ) pipe.set_progress_bar_config(disable=UpperCamelCase__ ) snake_case : int = self.get_dummy_inputs() snake_case : List[str] = pipe(**UpperCamelCase__ ).images snake_case : List[str] = image[0, -3:, -3:, -1].flatten() # started as 128, should now be 512 assert image.shape == (1, 512, 512, 3) snake_case : int = np.array( [0.6_974_782, 0.68_902_093, 0.70_135_885, 0.7_583_618, 0.7_804_545, 0.7_854_912, 0.78_667_426, 0.78_743_863, 0.78_070_223] ) assert np.abs(image_slice - expected_slice ).max() < 1e-1 def lowerCAmelCase ( self : List[str] ) -> Dict: """simple docstring""" snake_case : Any = OnnxStableDiffusionUpscalePipeline.from_pretrained(self.hub_checkpoint , provider='''CPUExecutionProvider''' ) snake_case : List[str] = PNDMScheduler.from_config(pipe.scheduler.config , skip_prk_steps=UpperCamelCase__ ) pipe.set_progress_bar_config(disable=UpperCamelCase__ ) snake_case : Optional[int] = self.get_dummy_inputs() snake_case : Union[str, Any] = pipe(**UpperCamelCase__ ).images snake_case : Optional[Any] = image[0, -3:, -3:, -1] assert image.shape == (1, 512, 512, 3) snake_case : Any = np.array( [0.6_898_892, 0.59_240_556, 0.52_499_527, 0.58_866_215, 0.52_258_235, 0.52_572_715, 0.62_414_473, 0.6_174_387, 0.6_214_964] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-1 def lowerCAmelCase ( self : Dict ) -> Union[str, Any]: """simple docstring""" snake_case : Optional[int] = OnnxStableDiffusionUpscalePipeline.from_pretrained(self.hub_checkpoint , provider='''CPUExecutionProvider''' ) snake_case : List[Any] = DPMSolverMultistepScheduler.from_config(pipe.scheduler.config ) pipe.set_progress_bar_config(disable=UpperCamelCase__ ) snake_case : Dict = self.get_dummy_inputs() snake_case : List[Any] = pipe(**UpperCamelCase__ ).images snake_case : Union[str, Any] = image[0, -3:, -3:, -1] assert image.shape == (1, 512, 512, 3) snake_case : Optional[Any] = np.array( [0.7_659_278, 0.76_437_664, 0.75_579_107, 0.7_691_116, 0.77_666_986, 0.7_727_672, 0.7_758_664, 0.7_812_226, 0.76_942_515] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-1 def lowerCAmelCase ( self : Any ) -> Dict: """simple docstring""" snake_case : str = OnnxStableDiffusionUpscalePipeline.from_pretrained(self.hub_checkpoint , provider='''CPUExecutionProvider''' ) snake_case : Optional[int] = EulerDiscreteScheduler.from_config(pipe.scheduler.config ) pipe.set_progress_bar_config(disable=UpperCamelCase__ ) snake_case : Dict = self.get_dummy_inputs() snake_case : Any = pipe(**UpperCamelCase__ ).images snake_case : List[Any] = image[0, -3:, -3:, -1] assert image.shape == (1, 512, 512, 3) snake_case : Optional[Any] = np.array( [0.6_974_782, 0.68_902_093, 0.70_135_885, 0.7_583_618, 0.7_804_545, 0.7_854_912, 0.78_667_426, 0.78_743_863, 0.78_070_223] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-1 def lowerCAmelCase ( self : Optional[Any] ) -> str: """simple docstring""" snake_case : List[Any] = OnnxStableDiffusionUpscalePipeline.from_pretrained(self.hub_checkpoint , provider='''CPUExecutionProvider''' ) snake_case : Optional[Any] = EulerAncestralDiscreteScheduler.from_config(pipe.scheduler.config ) pipe.set_progress_bar_config(disable=UpperCamelCase__ ) snake_case : Any = self.get_dummy_inputs() snake_case : Union[str, Any] = pipe(**UpperCamelCase__ ).images snake_case : str = image[0, -3:, -3:, -1] assert image.shape == (1, 512, 512, 3) snake_case : Any = np.array( [0.77_424_496, 0.773_601, 0.7_645_288, 0.7_769_598, 0.7_772_739, 0.7_738_688, 0.78_187_233, 0.77_879_584, 0.767_043] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-1 @nightly @require_onnxruntime @require_torch_gpu class snake_case__ ( unittest.TestCase ): """simple docstring""" @property def lowerCAmelCase ( self : str ) -> str: """simple docstring""" return ( "CUDAExecutionProvider", { "gpu_mem_limit": "15000000000", # 15GB "arena_extend_strategy": "kSameAsRequested", }, ) @property def lowerCAmelCase ( self : Dict ) -> Optional[int]: """simple docstring""" snake_case : List[str] = ort.SessionOptions() snake_case : List[str] = False return options def lowerCAmelCase ( self : List[str] ) -> Optional[int]: """simple docstring""" snake_case : Any = load_image( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main''' '''/img2img/sketch-mountains-input.jpg''' ) snake_case : Any = init_image.resize((128, 128) ) # using the PNDM scheduler by default snake_case : Union[str, Any] = OnnxStableDiffusionUpscalePipeline.from_pretrained( '''ssube/stable-diffusion-x4-upscaler-onnx''' , provider=self.gpu_provider , sess_options=self.gpu_options , ) pipe.set_progress_bar_config(disable=UpperCamelCase__ ) snake_case : Union[str, Any] = '''A fantasy landscape, trending on artstation''' snake_case : str = torch.manual_seed(0 ) snake_case : List[str] = pipe( prompt=UpperCamelCase__ , image=UpperCamelCase__ , guidance_scale=7.5 , num_inference_steps=10 , generator=UpperCamelCase__ , output_type='''np''' , ) snake_case : int = output.images snake_case : str = images[0, 255:258, 383:386, -1] assert images.shape == (1, 512, 512, 3) snake_case : Optional[int] = np.array([0.4_883, 0.4_947, 0.4_980, 0.4_975, 0.4_982, 0.4_980, 0.5_000, 0.5_006, 0.4_972] ) # TODO: lower the tolerance after finding the cause of onnxruntime reproducibility issues assert np.abs(image_slice.flatten() - expected_slice ).max() < 2e-2 def lowerCAmelCase ( self : Dict ) -> Optional[int]: """simple docstring""" snake_case : Tuple = load_image( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main''' '''/img2img/sketch-mountains-input.jpg''' ) snake_case : List[Any] = init_image.resize((128, 128) ) snake_case : Optional[Any] = LMSDiscreteScheduler.from_pretrained( '''ssube/stable-diffusion-x4-upscaler-onnx''' , subfolder='''scheduler''' ) snake_case : Union[str, Any] = OnnxStableDiffusionUpscalePipeline.from_pretrained( '''ssube/stable-diffusion-x4-upscaler-onnx''' , scheduler=UpperCamelCase__ , provider=self.gpu_provider , sess_options=self.gpu_options , ) pipe.set_progress_bar_config(disable=UpperCamelCase__ ) snake_case : Optional[Any] = '''A fantasy landscape, trending on artstation''' snake_case : List[Any] = torch.manual_seed(0 ) snake_case : List[str] = pipe( prompt=UpperCamelCase__ , image=UpperCamelCase__ , guidance_scale=7.5 , num_inference_steps=20 , generator=UpperCamelCase__ , output_type='''np''' , ) snake_case : str = output.images snake_case : int = images[0, 255:258, 383:386, -1] assert images.shape == (1, 512, 512, 3) snake_case : int = np.array( [0.50_173_753, 0.50_223_356, 0.502_039, 0.50_233_036, 0.5_023_725, 0.5_022_601, 0.5_018_758, 0.50_234_085, 0.50_241_566] ) # TODO: lower the tolerance after finding the cause of onnxruntime reproducibility issues assert np.abs(image_slice.flatten() - expected_slice ).max() < 2e-2
83
'''simple docstring''' from collections import OrderedDict from typing import List, Mapping from packaging import version from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging lowercase__ = logging.get_logger(__name__) lowercase__ = { "google/efficientnet-b7": "https://huggingface.co/google/efficientnet-b7/resolve/main/config.json", } class snake_case__ ( __SCREAMING_SNAKE_CASE ): """simple docstring""" lowerCamelCase = """efficientnet""" def __init__( self : str , UpperCamelCase__ : int = 3 , UpperCamelCase__ : int = 600 , UpperCamelCase__ : float = 2.0 , UpperCamelCase__ : float = 3.1 , UpperCamelCase__ : int = 8 , UpperCamelCase__ : List[int] = [3, 3, 5, 3, 5, 5, 3] , UpperCamelCase__ : List[int] = [32, 16, 24, 40, 80, 112, 192] , UpperCamelCase__ : List[int] = [16, 24, 40, 80, 112, 192, 320] , UpperCamelCase__ : List[int] = [] , UpperCamelCase__ : List[int] = [1, 2, 2, 2, 1, 2, 1] , UpperCamelCase__ : List[int] = [1, 2, 2, 3, 3, 4, 1] , UpperCamelCase__ : List[int] = [1, 6, 6, 6, 6, 6, 6] , UpperCamelCase__ : float = 0.25 , UpperCamelCase__ : str = "swish" , UpperCamelCase__ : int = 2560 , UpperCamelCase__ : str = "mean" , UpperCamelCase__ : float = 0.02 , UpperCamelCase__ : float = 0.001 , UpperCamelCase__ : float = 0.99 , UpperCamelCase__ : float = 0.5 , UpperCamelCase__ : float = 0.2 , **UpperCamelCase__ : Any , ) -> Any: """simple docstring""" super().__init__(**UpperCamelCase__ ) snake_case : Dict = num_channels snake_case : List[Any] = image_size snake_case : Any = width_coefficient snake_case : int = depth_coefficient snake_case : List[str] = depth_divisor snake_case : Tuple = kernel_sizes snake_case : Optional[Any] = in_channels snake_case : Optional[Any] = out_channels snake_case : Dict = depthwise_padding snake_case : Optional[Any] = strides snake_case : List[str] = num_block_repeats snake_case : Any = expand_ratios snake_case : Any = squeeze_expansion_ratio snake_case : Optional[Any] = hidden_act snake_case : Optional[int] = hidden_dim snake_case : Dict = pooling_type snake_case : Any = initializer_range snake_case : Optional[Any] = batch_norm_eps snake_case : Tuple = batch_norm_momentum snake_case : Any = dropout_rate snake_case : str = drop_connect_rate snake_case : Dict = sum(UpperCamelCase__ ) * 4 class snake_case__ ( __SCREAMING_SNAKE_CASE ): """simple docstring""" lowerCamelCase = version.parse("""1.11""" ) @property def lowerCAmelCase ( self : int ) -> Mapping[str, Mapping[int, str]]: """simple docstring""" return OrderedDict( [ ('''pixel_values''', {0: '''batch''', 1: '''num_channels''', 2: '''height''', 3: '''width'''}), ] ) @property def lowerCAmelCase ( self : Tuple ) -> float: """simple docstring""" return 1e-5
83
1
import argparse import json import os import fairseq import torch from fairseq.data import Dictionary from transformers import ( HubertConfig, HubertForCTC, HubertModel, WavaVecaCTCTokenizer, WavaVecaFeatureExtractor, WavaVecaProcessor, logging, ) logging.set_verbosity_info() a_ :int = logging.get_logger(__name__) a_ :Optional[int] = { "post_extract_proj": "feature_projection.projection", "encoder.pos_conv.0": "encoder.pos_conv_embed.conv", "self_attn.k_proj": "encoder.layers.*.attention.k_proj", "self_attn.v_proj": "encoder.layers.*.attention.v_proj", "self_attn.q_proj": "encoder.layers.*.attention.q_proj", "self_attn.out_proj": "encoder.layers.*.attention.out_proj", "self_attn_layer_norm": "encoder.layers.*.layer_norm", "fc1": "encoder.layers.*.feed_forward.intermediate_dense", "fc2": "encoder.layers.*.feed_forward.output_dense", "final_layer_norm": "encoder.layers.*.final_layer_norm", "encoder.layer_norm": "encoder.layer_norm", "w2v_model.layer_norm": "feature_projection.layer_norm", "w2v_encoder.proj": "lm_head", "mask_emb": "masked_spec_embed", } def lowercase_ (A : Optional[Any] , A : Tuple , A : Union[str, Any] , A : Tuple , A : Optional[Any] ): for attribute in key.split('.' ): snake_case__ : Any = getattr(A , A ) if weight_type is not None: snake_case__ : Union[str, Any] = getattr(A , A ).shape else: snake_case__ : Tuple = hf_pointer.shape assert hf_shape == value.shape, ( F'''Shape of hf {key + "." + weight_type if weight_type is not None else ""} is {hf_shape}, but should be''' F''' {value.shape} for {full_name}''' ) if weight_type == "weight": snake_case__ : Tuple = value elif weight_type == "weight_g": snake_case__ : List[Any] = value elif weight_type == "weight_v": snake_case__ : Tuple = value elif weight_type == "bias": snake_case__ : str = value else: snake_case__ : Optional[Any] = value logger.info(F'''{key + "." + weight_type if weight_type is not None else ""} was initialized from {full_name}.''' ) def lowercase_ (A : Optional[Any] , A : List[str] , A : Any ): snake_case__ : Union[str, Any] = [] snake_case__ : str = fairseq_model.state_dict() snake_case__ : List[Any] = hf_model.hubert.feature_extractor if is_finetuned else hf_model.feature_extractor for name, value in fairseq_dict.items(): snake_case__ : Any = False if "conv_layers" in name: load_conv_layer( A , A , A , A , hf_model.config.feat_extract_norm == 'group' , ) snake_case__ : Any = True else: for key, mapped_key in MAPPING.items(): snake_case__ : Any = 'hubert.' + mapped_key if (is_finetuned and mapped_key != 'lm_head') else mapped_key if key in name or (key.split('w2v_model.' )[-1] == name.split('.' )[0] and not is_finetuned): snake_case__ : List[str] = True if "*" in mapped_key: snake_case__ : Optional[Any] = name.split(A )[0].split('.' )[-2] snake_case__ : List[str] = mapped_key.replace('*' , A ) if "weight_g" in name: snake_case__ : Union[str, Any] = 'weight_g' elif "weight_v" in name: snake_case__ : int = 'weight_v' elif "weight" in name: snake_case__ : Tuple = 'weight' elif "bias" in name: snake_case__ : int = 'bias' else: snake_case__ : List[Any] = None set_recursively(A , A , A , A , A ) continue if not is_used: unused_weights.append(A ) logger.warning(F'''Unused weights: {unused_weights}''' ) def lowercase_ (A : Union[str, Any] , A : str , A : Any , A : int , A : List[Any] ): snake_case__ : Optional[int] = full_name.split('conv_layers.' )[-1] snake_case__ : int = name.split('.' ) snake_case__ : str = int(items[0] ) snake_case__ : Optional[Any] = int(items[1] ) if type_id == 0: if "bias" in name: assert value.shape == feature_extractor.conv_layers[layer_id].conv.bias.data.shape, ( F'''{full_name} has size {value.shape}, but''' F''' {feature_extractor.conv_layers[layer_id].conv.bias.data.shape} was found.''' ) snake_case__ : List[str] = value logger.info(F'''Feat extract conv layer {layer_id} was initialized from {full_name}.''' ) elif "weight" in name: assert value.shape == feature_extractor.conv_layers[layer_id].conv.weight.data.shape, ( F'''{full_name} has size {value.shape}, but''' F''' {feature_extractor.conv_layers[layer_id].conv.weight.data.shape} was found.''' ) snake_case__ : Any = value logger.info(F'''Feat extract conv layer {layer_id} was initialized from {full_name}.''' ) elif (type_id == 2 and not use_group_norm) or (type_id == 2 and layer_id == 0 and use_group_norm): if "bias" in name: assert value.shape == feature_extractor.conv_layers[layer_id].layer_norm.bias.data.shape, ( F'''{full_name} has size {value.shape}, but {feature_extractor[layer_id].layer_norm.bias.data.shape} was''' " found." ) snake_case__ : List[Any] = value logger.info(F'''Feat extract layer norm weight of layer {layer_id} was initialized from {full_name}.''' ) elif "weight" in name: assert value.shape == feature_extractor.conv_layers[layer_id].layer_norm.weight.data.shape, ( F'''{full_name} has size {value.shape}, but''' F''' {feature_extractor[layer_id].layer_norm.weight.data.shape} was found.''' ) snake_case__ : Any = value logger.info(F'''Feat extract layer norm weight of layer {layer_id} was initialized from {full_name}.''' ) else: unused_weights.append(A ) @torch.no_grad() def lowercase_ (A : List[str] , A : Optional[int] , A : List[Any]=None , A : Union[str, Any]=None , A : List[str]=True ): if config_path is not None: snake_case__ : Tuple = HubertConfig.from_pretrained(A ) else: snake_case__ : Tuple = HubertConfig() if is_finetuned: if dict_path: snake_case__ : int = Dictionary.load(A ) # important change bos & pad token id since CTC symbol is <pad> and # not <s> as in fairseq snake_case__ : Dict = target_dict.pad_index snake_case__ : Union[str, Any] = target_dict.bos_index snake_case__ : Optional[Any] = target_dict.eos_index snake_case__ : Union[str, Any] = len(target_dict.symbols ) snake_case__ : List[Any] = os.path.join(A , 'vocab.json' ) if not os.path.isdir(A ): logger.error('--pytorch_dump_folder_path ({}) should be a directory'.format(A ) ) return os.makedirs(A , exist_ok=A ) with open(A , 'w' , encoding='utf-8' ) as vocab_handle: json.dump(target_dict.indices , A ) snake_case__ : Tuple = WavaVecaCTCTokenizer( A , unk_token=target_dict.unk_word , pad_token=target_dict.pad_word , bos_token=target_dict.bos_word , eos_token=target_dict.eos_word , word_delimiter_token='|' , do_lower_case=A , ) snake_case__ : List[Any] = True if config.feat_extract_norm == 'layer' else False snake_case__ : List[str] = WavaVecaFeatureExtractor( feature_size=1 , sampling_rate=1_6_0_0_0 , padding_value=0 , do_normalize=A , return_attention_mask=A , ) snake_case__ : List[Any] = WavaVecaProcessor(feature_extractor=A , tokenizer=A ) processor.save_pretrained(A ) snake_case__ : Optional[Any] = HubertForCTC(A ) else: snake_case__ : int = HubertModel(A ) if is_finetuned: snake_case__ , snake_case__ , snake_case__ : Tuple = fairseq.checkpoint_utils.load_model_ensemble_and_task( [checkpoint_path] , arg_overrides={'data': '/'.join(dict_path.split('/' )[:-1] )} ) else: snake_case__ , snake_case__ , snake_case__ : List[str] = fairseq.checkpoint_utils.load_model_ensemble_and_task([checkpoint_path] ) snake_case__ : str = model[0].eval() recursively_load_weights(A , A , A ) hf_wavavec.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("--dict_path", default=None, type=str, help="Path to dict of fine-tuned model") parser.add_argument("--config_path", default=None, type=str, help="Path to hf config.json of model to convert") parser.add_argument( "--not_finetuned", action="store_true", help="Whether the model to convert is a fine-tuned model or not" ) a_ :List[Any] = parser.parse_args() convert_hubert_checkpoint( args.checkpoint_path, args.pytorch_dump_folder_path, args.config_path, args.dict_path, not args.not_finetuned )
277
from collections import deque from .hash_table import HashTable class snake_case__ ( lowerCAmelCase_ ): """simple docstring""" def __init__( self : Optional[Any], *_snake_case : Optional[Any], **_snake_case : List[Any] ) ->Optional[int]: super().__init__(*_snake_case, **_snake_case ) def lowercase_ ( self : Optional[Any], _snake_case : Tuple, _snake_case : Dict ) ->Dict: snake_case__ : int = deque([] ) if self.values[key] is None else self.values[key] self.values[key].appendleft(_snake_case ) snake_case__ : Dict = self.values[key] def lowercase_ ( self : Any ) ->Optional[Any]: return ( sum(self.charge_factor - len(_snake_case ) for slot in self.values ) / self.size_table * self.charge_factor ) def lowercase_ ( self : Union[str, Any], _snake_case : str, _snake_case : Optional[int]=None ) ->Optional[Any]: if not ( len(self.values[key] ) == self.charge_factor and self.values.count(_snake_case ) == 0 ): return key return super()._collision_resolution(_snake_case, _snake_case )
277
1
"""simple docstring""" from collections.abc import Sequence from queue import Queue class _snake_case : def __init__( self : str , UpperCAmelCase : Optional[Any] , UpperCAmelCase : Any , UpperCAmelCase : Optional[int] , UpperCAmelCase : Dict=None , UpperCAmelCase : int=None ): __lowerCamelCase : Any = start __lowerCamelCase : Union[str, Any] = end __lowerCamelCase : Optional[Any] = val __lowerCamelCase : str = (start + end) // 2 __lowerCamelCase : List[str] = left __lowerCamelCase : List[Any] = right def __repr__( self : Optional[Any] ): return F"""SegmentTreeNode(start={self.start}, end={self.end}, val={self.val})""" class _snake_case : def __init__( self : List[Any] , UpperCAmelCase : Sequence , UpperCAmelCase : Dict ): __lowerCamelCase : Any = collection __lowerCamelCase : List[Any] = function if self.collection: __lowerCamelCase : str = self._build_tree(0 , len(UpperCAmelCase ) - 1 ) def lowerCamelCase__ ( self : Dict , UpperCAmelCase : Union[str, Any] , UpperCAmelCase : Tuple ): self._update_tree(self.root , UpperCAmelCase , UpperCAmelCase ) def lowerCamelCase__ ( self : int , UpperCAmelCase : Any , UpperCAmelCase : int ): return self._query_range(self.root , UpperCAmelCase , UpperCAmelCase ) def lowerCamelCase__ ( self : Any , UpperCAmelCase : Any , UpperCAmelCase : str ): if start == end: return SegmentTreeNode(UpperCAmelCase , UpperCAmelCase , self.collection[start] ) __lowerCamelCase : Union[str, Any] = (start + end) // 2 __lowerCamelCase : Union[str, Any] = self._build_tree(UpperCAmelCase , UpperCAmelCase ) __lowerCamelCase : Any = self._build_tree(mid + 1 , UpperCAmelCase ) return SegmentTreeNode(UpperCAmelCase , UpperCAmelCase , self.fn(left.val , right.val ) , UpperCAmelCase , UpperCAmelCase ) def lowerCamelCase__ ( self : List[Any] , UpperCAmelCase : List[Any] , UpperCAmelCase : int , UpperCAmelCase : Optional[int] ): if node.start == i and node.end == i: __lowerCamelCase : Any = val return if i <= node.mid: self._update_tree(node.left , UpperCAmelCase , UpperCAmelCase ) else: self._update_tree(node.right , UpperCAmelCase , UpperCAmelCase ) __lowerCamelCase : Dict = self.fn(node.left.val , node.right.val ) def lowerCamelCase__ ( self : str , UpperCAmelCase : List[Any] , UpperCAmelCase : str , UpperCAmelCase : str ): if node.start == i and node.end == j: return node.val if i <= node.mid: if j <= node.mid: # range in left child tree return self._query_range(node.left , UpperCAmelCase , UpperCAmelCase ) else: # range in left child tree and right child tree return self.fn( self._query_range(node.left , UpperCAmelCase , node.mid ) , self._query_range(node.right , node.mid + 1 , UpperCAmelCase ) , ) else: # range in right child tree return self._query_range(node.right , UpperCAmelCase , UpperCAmelCase ) def lowerCamelCase__ ( self : Optional[Any] ): if self.root is not None: __lowerCamelCase : Union[str, Any] = Queue() queue.put(self.root ) while not queue.empty(): __lowerCamelCase : Any = queue.get() yield node if node.left is not None: queue.put(node.left ) if node.right is not None: queue.put(node.right ) if __name__ == "__main__": import operator for fn in [operator.add, max, min]: print('''*''' * 50) __A = SegmentTree([2, 1, 5, 3, 4], fn) for node in arr.traverse(): print(node) print() arr.update(1, 5) for node in arr.traverse(): print(node) print() print(arr.query_range(3, 4)) # 7 print(arr.query_range(2, 2)) # 5 print(arr.query_range(1, 3)) # 13 print()
64
"""simple docstring""" from ...utils import ( OptionalDependencyNotAvailable, is_torch_available, is_transformers_available, is_transformers_version, ) try: if not (is_transformers_available() and is_torch_available() and is_transformers_version('''>=''', '''4.25.0''')): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from ...utils.dummy_torch_and_transformers_objects import ( VersatileDiffusionDualGuidedPipeline, VersatileDiffusionImageVariationPipeline, VersatileDiffusionPipeline, VersatileDiffusionTextToImagePipeline, ) else: from .modeling_text_unet import UNetFlatConditionModel from .pipeline_versatile_diffusion import VersatileDiffusionPipeline from .pipeline_versatile_diffusion_dual_guided import VersatileDiffusionDualGuidedPipeline from .pipeline_versatile_diffusion_image_variation import VersatileDiffusionImageVariationPipeline from .pipeline_versatile_diffusion_text_to_image import VersatileDiffusionTextToImagePipeline
64
1
import copy import os import cva import numpy as np from matplotlib import pyplot as plt class __snake_case : '''simple docstring''' def __init__( self : str ): __snake_case: Optional[Any] = """""" __snake_case: Any = """""" __snake_case: Tuple = [] __snake_case: Union[str, Any] = 0 __snake_case: Optional[int] = 256 __snake_case: Union[str, Any] = 0 __snake_case: str = 0 __snake_case: List[Any] = 0 __snake_case: List[Any] = 0 def UpperCAmelCase__ ( self : List[Any] , A : List[Any] ): __snake_case: str = cva.imread(A , 0 ) __snake_case: Optional[Any] = copy.deepcopy(self.img ) __snake_case , __snake_case , __snake_case: Optional[int] = plt.hist(self.img.ravel() , 256 , [0, 256] , label="""x""" ) __snake_case: List[Any] = np.sum(A ) for i in range(len(A ) ): __snake_case: Dict = x[i] / self.k self.sk += prk __snake_case: Optional[Any] = (self.L - 1) * self.sk if self.rem != 0: __snake_case: Union[str, Any] = int(last % last ) __snake_case: Optional[Any] = int(last + 1 if self.rem >= 0.5 else last ) self.last_list.append(A ) __snake_case: str = int(np.ma.count(self.img ) / self.img[1].size ) __snake_case: Optional[int] = self.img[1].size for i in range(self.number_of_cols ): for j in range(self.number_of_rows ): __snake_case: int = self.img[j][i] if num != self.last_list[num]: __snake_case: Dict = self.last_list[num] cva.imwrite("""output_data/output.jpg""" , self.img ) def UpperCAmelCase__ ( self : Any ): plt.hist(self.img.ravel() , 256 , [0, 256] ) def UpperCAmelCase__ ( self : Any ): cva.imshow("""Output-Image""" , self.img ) cva.imshow("""Input-Image""" , self.original_image ) cva.waitKey(5_000 ) cva.destroyAllWindows() if __name__ == "__main__": __UpperCAmelCase : int = os.path.join(os.path.basename(__file__), "image_data/input.jpg") __UpperCAmelCase : Optional[Any] = ConstantStretch() stretcher.stretch(file_path) stretcher.plot_histogram() stretcher.show_image()
111
from __future__ import annotations import typing from collections import Counter def A__ ( SCREAMING_SNAKE_CASE__) -> typing.Counter[int]: __snake_case: typing.Counter[int] = Counter() for base in range(1 , max_perimeter + 1): for perpendicular in range(SCREAMING_SNAKE_CASE__ , max_perimeter + 1): __snake_case: Dict = (base * base + perpendicular * perpendicular) ** 0.5 if hypotenuse == int(SCREAMING_SNAKE_CASE__): __snake_case: Any = int(base + perpendicular + hypotenuse) if perimeter > max_perimeter: continue triplets[perimeter] += 1 return triplets def A__ ( SCREAMING_SNAKE_CASE__ = 1000) -> int: __snake_case: List[str] = pythagorean_triple(SCREAMING_SNAKE_CASE__) return triplets.most_common(1)[0][0] if __name__ == "__main__": print(f'Perimeter {solution()} has maximum solutions')
111
1
"""simple docstring""" _UpperCamelCase : List[Any] = 8.31_44_62 # Unit - J mol-1 K-1 def snake_case (A_ :float , A_ :float , A_ :float ): '''simple docstring''' if moles < 0 or kelvin < 0 or volume < 0: raise ValueError('Invalid inputs. Enter positive value.' ) return moles * kelvin * UNIVERSAL_GAS_CONSTANT / volume def snake_case (A_ :float , A_ :float , A_ :float ): '''simple docstring''' if moles < 0 or kelvin < 0 or pressure < 0: raise ValueError('Invalid inputs. Enter positive value.' ) return moles * kelvin * UNIVERSAL_GAS_CONSTANT / pressure if __name__ == "__main__": from doctest import testmod testmod()
186
"""simple docstring""" from __future__ import annotations import math import numpy as np from numpy.linalg import norm def snake_case (A_ :np.ndarray , A_ :np.ndarray ): '''simple docstring''' return math.sqrt(sum(pow(a - b , 2 ) for a, b in zip(A_ , A_ ) ) ) def snake_case (A_ :np.ndarray , A_ :np.ndarray ): '''simple docstring''' if dataset.ndim != value_array.ndim: a : Optional[Any] = ( 'Wrong input data\'s dimensions... ' f'''dataset : {dataset.ndim}, value_array : {value_array.ndim}''' ) raise ValueError(A_ ) try: if dataset.shape[1] != value_array.shape[1]: a : Optional[int] = ( 'Wrong input data\'s shape... ' f'''dataset : {dataset.shape[1]}, value_array : {value_array.shape[1]}''' ) raise ValueError(A_ ) except IndexError: if dataset.ndim != value_array.ndim: raise TypeError('Wrong shape' ) if dataset.dtype != value_array.dtype: a : Optional[Any] = ( 'Input data have different datatype... ' f'''dataset : {dataset.dtype}, value_array : {value_array.dtype}''' ) raise TypeError(A_ ) a : Tuple = [] for value in value_array: a : List[Any] = euclidean(A_ , dataset[0] ) a : int = dataset[0].tolist() for dataset_value in dataset[1:]: a : Optional[int] = euclidean(A_ , A_ ) if dist > temp_dist: a : List[str] = temp_dist a : int = dataset_value.tolist() answer.append([vector, dist] ) return answer def snake_case (A_ :np.ndarray , A_ :np.ndarray ): '''simple docstring''' return np.dot(A_ , A_ ) / (norm(A_ ) * norm(A_ )) if __name__ == "__main__": import doctest doctest.testmod()
186
1
"""simple docstring""" import logging import math import os from dataclasses import dataclass, field from glob import glob from typing import Optional from torch.utils.data import ConcatDataset import transformers from transformers import ( CONFIG_MAPPING, MODEL_WITH_LM_HEAD_MAPPING, AutoConfig, AutoModelWithLMHead, AutoTokenizer, DataCollatorForLanguageModeling, DataCollatorForPermutationLanguageModeling, DataCollatorForWholeWordMask, HfArgumentParser, LineByLineTextDataset, LineByLineWithRefDataset, PreTrainedTokenizer, TextDataset, Trainer, TrainingArguments, set_seed, ) from transformers.trainer_utils import is_main_process SCREAMING_SNAKE_CASE : int = logging.getLogger(__name__) SCREAMING_SNAKE_CASE : Dict = list(MODEL_WITH_LM_HEAD_MAPPING.keys()) SCREAMING_SNAKE_CASE : Any = tuple(conf.model_type for conf in MODEL_CONFIG_CLASSES) @dataclass class _UpperCAmelCase : '''simple docstring''' lowerCamelCase__ =field( default=__snake_case, metadata={ 'help': ( 'The model checkpoint for weights initialization. Leave None if you want to train a model from' ' scratch.' ) }, ) lowerCamelCase__ =field( default=__snake_case, metadata={'help': 'If training from scratch, pass a model type from the list: ' + ', '.join(__snake_case )}, ) lowerCamelCase__ =field( default=__snake_case, metadata={'help': 'Pretrained config name or path if not the same as model_name'} ) lowerCamelCase__ =field( default=__snake_case, metadata={'help': 'Pretrained tokenizer name or path if not the same as model_name'} ) lowerCamelCase__ =field( default=__snake_case, metadata={'help': 'Where do you want to store the pretrained models downloaded from huggingface.co'}, ) @dataclass class _UpperCAmelCase : '''simple docstring''' lowerCamelCase__ =field( default=__snake_case, metadata={'help': 'The input training data file (a text file).'} ) lowerCamelCase__ =field( default=__snake_case, metadata={ 'help': ( 'The input training data files (multiple files in glob format). ' 'Very often splitting large files to smaller files can prevent tokenizer going out of memory' ) }, ) lowerCamelCase__ =field( default=__snake_case, metadata={'help': 'An optional input evaluation data file to evaluate the perplexity on (a text file).'}, ) lowerCamelCase__ =field( default=__snake_case, metadata={'help': 'An optional input train ref data file for whole word mask in Chinese.'}, ) lowerCamelCase__ =field( default=__snake_case, metadata={'help': 'An optional input eval ref data file for whole word mask in Chinese.'}, ) lowerCamelCase__ =field( default=__snake_case, metadata={'help': 'Whether distinct lines of text in the dataset are to be handled as distinct sequences.'}, ) lowerCamelCase__ =field( default=__snake_case, metadata={'help': 'Train with masked-language modeling loss instead of language modeling.'} ) lowerCamelCase__ =field(default=__snake_case, metadata={'help': 'Whether ot not to use whole word mask.'} ) lowerCamelCase__ =field( default=0.1_5, metadata={'help': 'Ratio of tokens to mask for masked language modeling loss'} ) lowerCamelCase__ =field( default=1 / 6, metadata={ 'help': ( 'Ratio of length of a span of masked tokens to surrounding context length for permutation language' ' modeling.' ) }, ) lowerCamelCase__ =field( default=5, metadata={'help': 'Maximum length of a span of masked tokens for permutation language modeling.'} ) lowerCamelCase__ =field( default=-1, metadata={ 'help': ( 'Optional input sequence length after tokenization.' 'The training dataset will be truncated in block of this size for training.' 'Default to the model max input length for single sentence inputs (take into account special tokens).' ) }, ) lowerCamelCase__ =field( default=__snake_case, metadata={'help': 'Overwrite the cached training and evaluation sets'} ) def lowercase ( _snake_case : DataTrainingArguments , _snake_case : PreTrainedTokenizer , _snake_case : bool = False , _snake_case : Optional[str] = None , ) ->Any: """simple docstring""" def _dataset(_snake_case : List[Any] , _snake_case : str=None ): if args.line_by_line: if ref_path is not None: if not args.whole_word_mask or not args.mlm: raise ValueError('''You need to set world whole masking and mlm to True for Chinese Whole Word Mask''' ) return LineByLineWithRefDataset( tokenizer=_snake_case , file_path=_snake_case , block_size=args.block_size , ref_path=_snake_case , ) return LineByLineTextDataset(tokenizer=_snake_case , file_path=_snake_case , block_size=args.block_size ) else: return TextDataset( tokenizer=_snake_case , file_path=_snake_case , block_size=args.block_size , overwrite_cache=args.overwrite_cache , cache_dir=_snake_case , ) if evaluate: return _dataset(args.eval_data_file , args.eval_ref_file ) elif args.train_data_files: return ConcatDataset([_dataset(_snake_case ) for f in glob(args.train_data_files )] ) else: return _dataset(args.train_data_file , args.train_ref_file ) def lowercase ( ) ->List[Any]: """simple docstring""" __snake_case : List[Any] = HfArgumentParser((ModelArguments, DataTrainingArguments, TrainingArguments) ) __snake_case , __snake_case , __snake_case : Union[str, Any] = parser.parse_args_into_dataclasses() if data_args.eval_data_file is None and training_args.do_eval: raise ValueError( '''Cannot do evaluation without an evaluation data file. Either supply a file to --eval_data_file ''' '''or remove the --do_eval argument.''' ) if ( os.path.exists(training_args.output_dir ) and os.listdir(training_args.output_dir ) and training_args.do_train and not training_args.overwrite_output_dir ): raise ValueError( f"""Output directory ({training_args.output_dir}) already exists and is not empty. Use""" ''' --overwrite_output_dir to overcome.''' ) # Setup logging logging.basicConfig( format='''%(asctime)s - %(levelname)s - %(name)s - %(message)s''' , datefmt='''%m/%d/%Y %H:%M:%S''' , level=logging.INFO if training_args.local_rank in [-1, 0] else logging.WARN , ) logger.warning( '''Process rank: %s, device: %s, n_gpu: %s, distributed training: %s, 16-bits training: %s''' , training_args.local_rank , training_args.device , training_args.n_gpu , bool(training_args.local_rank != -1 ) , training_args.fpaa , ) # Set the verbosity to info of the Transformers logger (on main process only): if is_main_process(training_args.local_rank ): transformers.utils.logging.set_verbosity_info() transformers.utils.logging.enable_default_handler() transformers.utils.logging.enable_explicit_format() logger.info('''Training/evaluation parameters %s''' , _snake_case ) # Set seed set_seed(training_args.seed ) # Load pretrained model and tokenizer # # Distributed training: # The .from_pretrained methods guarantee that only one local process can concurrently # download model & vocab. if model_args.config_name: __snake_case : Optional[Any] = AutoConfig.from_pretrained(model_args.config_name , cache_dir=model_args.cache_dir ) elif model_args.model_name_or_path: __snake_case : Optional[Any] = AutoConfig.from_pretrained(model_args.model_name_or_path , cache_dir=model_args.cache_dir ) else: __snake_case : Tuple = CONFIG_MAPPING[model_args.model_type]() logger.warning('''You are instantiating a new config instance from scratch.''' ) if model_args.tokenizer_name: __snake_case : Dict = AutoTokenizer.from_pretrained(model_args.tokenizer_name , cache_dir=model_args.cache_dir ) elif model_args.model_name_or_path: __snake_case : List[Any] = AutoTokenizer.from_pretrained(model_args.model_name_or_path , cache_dir=model_args.cache_dir ) else: raise ValueError( '''You are instantiating a new tokenizer from scratch. This is not supported, but you can do it from another''' ''' script, save it,and load it from here, using --tokenizer_name''' ) if model_args.model_name_or_path: __snake_case : int = AutoModelWithLMHead.from_pretrained( model_args.model_name_or_path , from_tf=bool('''.ckpt''' in model_args.model_name_or_path ) , config=_snake_case , cache_dir=model_args.cache_dir , ) else: logger.info('''Training new model from scratch''' ) __snake_case : List[Any] = AutoModelWithLMHead.from_config(_snake_case ) model.resize_token_embeddings(len(_snake_case ) ) if config.model_type in ["bert", "roberta", "distilbert", "camembert"] and not data_args.mlm: raise ValueError( '''BERT and RoBERTa-like models do not have LM heads but masked LM heads. They must be run using the''' '''--mlm flag (masked language modeling).''' ) if data_args.block_size <= 0: __snake_case : List[str] = tokenizer.max_len # Our input block size will be the max possible for the model else: __snake_case : Optional[int] = min(data_args.block_size , tokenizer.max_len ) # Get datasets __snake_case : Optional[Any] = ( get_dataset(_snake_case , tokenizer=_snake_case , cache_dir=model_args.cache_dir ) if training_args.do_train else None ) __snake_case : Any = ( get_dataset(_snake_case , tokenizer=_snake_case , evaluate=_snake_case , cache_dir=model_args.cache_dir ) if training_args.do_eval else None ) if config.model_type == "xlnet": __snake_case : List[Any] = DataCollatorForPermutationLanguageModeling( tokenizer=_snake_case , plm_probability=data_args.plm_probability , max_span_length=data_args.max_span_length , ) else: if data_args.mlm and data_args.whole_word_mask: __snake_case : Optional[Any] = DataCollatorForWholeWordMask( tokenizer=_snake_case , mlm_probability=data_args.mlm_probability ) else: __snake_case : Union[str, Any] = DataCollatorForLanguageModeling( tokenizer=_snake_case , mlm=data_args.mlm , mlm_probability=data_args.mlm_probability ) # Initialize our Trainer __snake_case : Optional[int] = Trainer( model=_snake_case , args=_snake_case , data_collator=_snake_case , train_dataset=_snake_case , eval_dataset=_snake_case , prediction_loss_only=_snake_case , ) # Training if training_args.do_train: __snake_case : Dict = ( model_args.model_name_or_path if model_args.model_name_or_path is not None and os.path.isdir(model_args.model_name_or_path ) else None ) trainer.train(model_path=_snake_case ) trainer.save_model() # For convenience, we also re-save the tokenizer to the same directory, # so that you can share your model easily on huggingface.co/models =) if trainer.is_world_master(): tokenizer.save_pretrained(training_args.output_dir ) # Evaluation __snake_case : int = {} if training_args.do_eval: logger.info('''*** Evaluate ***''' ) __snake_case : Dict = trainer.evaluate() __snake_case : Dict = math.exp(eval_output['''eval_loss'''] ) __snake_case : List[Any] = {'''perplexity''': perplexity} __snake_case : str = os.path.join(training_args.output_dir , '''eval_results_lm.txt''' ) if trainer.is_world_master(): with open(_snake_case , '''w''' ) as writer: logger.info('''***** Eval results *****''' ) for key in sorted(result.keys() ): logger.info(''' %s = %s''' , _snake_case , str(result[key] ) ) writer.write('''%s = %s\n''' % (key, str(result[key] )) ) results.update(_snake_case ) return results def lowercase ( _snake_case : Optional[int] ) ->Tuple: """simple docstring""" main() if __name__ == "__main__": main()
102
"""simple docstring""" import unittest import numpy as np import torch from diffusers import DDIMPipeline, DDIMScheduler, UNetaDModel from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu, slow, torch_device from ..pipeline_params import UNCONDITIONAL_IMAGE_GENERATION_BATCH_PARAMS, UNCONDITIONAL_IMAGE_GENERATION_PARAMS from ..test_pipelines_common import PipelineTesterMixin enable_full_determinism() class lowerCAmelCase__ ( lowercase, unittest.TestCase ): '''simple docstring''' lowerCamelCase__ = DDIMPipeline lowerCamelCase__ = UNCONDITIONAL_IMAGE_GENERATION_PARAMS lowerCamelCase__ = PipelineTesterMixin.required_optional_params - { """num_images_per_prompt""", """latents""", """callback""", """callback_steps""", } lowerCamelCase__ = UNCONDITIONAL_IMAGE_GENERATION_BATCH_PARAMS lowerCamelCase__ = False def A_ ( self ): torch.manual_seed(0 ) _lowerCamelCase : List[Any] = UNetaDModel( block_out_channels=(32, 64) , layers_per_block=2 , sample_size=32 , in_channels=3 , out_channels=3 , down_block_types=('DownBlock2D', 'AttnDownBlock2D') , up_block_types=('AttnUpBlock2D', 'UpBlock2D') , ) _lowerCamelCase : List[str] = DDIMScheduler() _lowerCamelCase : Optional[int] = {'unet': unet, 'scheduler': scheduler} return components def A_ ( self , lowercase , lowercase=0 ): if str(lowercase ).startswith('mps' ): _lowerCamelCase : Dict = torch.manual_seed(lowercase ) else: _lowerCamelCase : List[str] = torch.Generator(device=lowercase ).manual_seed(lowercase ) _lowerCamelCase : Tuple = { 'batch_size': 1, 'generator': generator, 'num_inference_steps': 2, 'output_type': 'numpy', } return inputs def A_ ( self ): _lowerCamelCase : Any = 'cpu' _lowerCamelCase : Tuple = self.get_dummy_components() _lowerCamelCase : Optional[Any] = self.pipeline_class(**lowercase ) pipe.to(lowercase ) pipe.set_progress_bar_config(disable=lowercase ) _lowerCamelCase : str = self.get_dummy_inputs(lowercase ) _lowerCamelCase : int = pipe(**lowercase ).images _lowerCamelCase : Any = image[0, -3:, -3:, -1] self.assertEqual(image.shape , (1, 32, 32, 3) ) _lowerCamelCase : Tuple = np.array( [1.000E00, 5.717E-01, 4.717E-01, 1.000E00, 0.000E00, 1.000E00, 3.000E-04, 0.000E00, 9.000E-04] ) _lowerCamelCase : str = np.abs(image_slice.flatten() - expected_slice ).max() self.assertLessEqual(lowercase , 1E-3 ) def A_ ( self ): super().test_dict_tuple_outputs_equivalent(expected_max_difference=3E-3 ) def A_ ( self ): super().test_save_load_local(expected_max_difference=3E-3 ) def A_ ( self ): super().test_save_load_optional_components(expected_max_difference=3E-3 ) def A_ ( self ): super().test_inference_batch_single_identical(expected_max_diff=3E-3 ) @slow @require_torch_gpu class lowerCAmelCase__ ( unittest.TestCase ): '''simple docstring''' def A_ ( self ): _lowerCamelCase : Optional[Any] = 'google/ddpm-cifar10-32' _lowerCamelCase : Optional[Any] = UNetaDModel.from_pretrained(lowercase ) _lowerCamelCase : Dict = DDIMScheduler() _lowerCamelCase : Dict = DDIMPipeline(unet=lowercase , scheduler=lowercase ) ddim.to(lowercase ) ddim.set_progress_bar_config(disable=lowercase ) _lowerCamelCase : List[str] = torch.manual_seed(0 ) _lowerCamelCase : str = ddim(generator=lowercase , eta=0.0 , output_type='numpy' ).images _lowerCamelCase : Dict = image[0, -3:, -3:, -1] assert image.shape == (1, 32, 32, 3) _lowerCamelCase : List[Any] = np.array([0.17_23, 0.16_17, 0.16_00, 0.16_26, 0.14_97, 0.15_13, 0.15_05, 0.14_42, 0.14_53] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-2 def A_ ( self ): _lowerCamelCase : Optional[int] = 'google/ddpm-ema-bedroom-256' _lowerCamelCase : str = UNetaDModel.from_pretrained(lowercase ) _lowerCamelCase : str = DDIMScheduler.from_pretrained(lowercase ) _lowerCamelCase : Optional[int] = DDIMPipeline(unet=lowercase , scheduler=lowercase ) ddpm.to(lowercase ) ddpm.set_progress_bar_config(disable=lowercase ) _lowerCamelCase : Tuple = torch.manual_seed(0 ) _lowerCamelCase : int = ddpm(generator=lowercase , output_type='numpy' ).images _lowerCamelCase : Dict = image[0, -3:, -3:, -1] assert image.shape == (1, 256, 256, 3) _lowerCamelCase : str = np.array([0.00_60, 0.02_01, 0.03_44, 0.00_24, 0.00_18, 0.00_02, 0.00_22, 0.00_00, 0.00_69] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-2
96
0
__A : Tuple = [ '''Audio''', '''Array2D''', '''Array3D''', '''Array4D''', '''Array5D''', '''ClassLabel''', '''Features''', '''Sequence''', '''Value''', '''Image''', '''Translation''', '''TranslationVariableLanguages''', ] from .audio import Audio from .features import ArrayaD, ArrayaD, ArrayaD, ArrayaD, ClassLabel, Features, Sequence, Value from .image import Image from .translation import Translation, TranslationVariableLanguages
323
import unittest import numpy as np from transformers.testing_utils import require_torch, require_vision from transformers.utils import is_torch_available, is_vision_available from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs if is_torch_available(): import torch if is_vision_available(): from PIL import Image from transformers import MobileNetVaImageProcessor class __A ( unittest.TestCase ): def __init__( self : List[str] , UpperCAmelCase_ : int , UpperCAmelCase_ : Optional[int]=7 , UpperCAmelCase_ : Tuple=3 , UpperCAmelCase_ : int=18 , UpperCAmelCase_ : List[str]=30 , UpperCAmelCase_ : str=400 , UpperCAmelCase_ : Union[str, Any]=True , UpperCAmelCase_ : Any=None , UpperCAmelCase_ : List[str]=True , UpperCAmelCase_ : Union[str, Any]=None , ): lowerCAmelCase : Any = size if size is not None else {'shortest_edge': 20} lowerCAmelCase : str = crop_size if crop_size is not None else {'height': 18, 'width': 18} lowerCAmelCase : List[Any] = parent lowerCAmelCase : Optional[Any] = batch_size lowerCAmelCase : int = num_channels lowerCAmelCase : int = image_size lowerCAmelCase : Tuple = min_resolution lowerCAmelCase : Any = max_resolution lowerCAmelCase : int = do_resize lowerCAmelCase : Dict = size lowerCAmelCase : int = do_center_crop lowerCAmelCase : str = crop_size def lowercase__ ( self : Optional[int] ): return { "do_resize": self.do_resize, "size": self.size, "do_center_crop": self.do_center_crop, "crop_size": self.crop_size, } @require_torch @require_vision class __A ( lowerCAmelCase , unittest.TestCase ): lowerCAmelCase_ : Optional[Any] = MobileNetVaImageProcessor if is_vision_available() else None def lowercase__ ( self : int ): lowerCAmelCase : List[str] = MobileNetVaImageProcessingTester(self ) @property def lowercase__ ( self : int ): return self.image_processor_tester.prepare_image_processor_dict() def lowercase__ ( self : Dict ): lowerCAmelCase : Any = self.image_processing_class(**self.image_processor_dict ) self.assertTrue(hasattr(UpperCAmelCase_ , 'do_resize' ) ) self.assertTrue(hasattr(UpperCAmelCase_ , 'size' ) ) self.assertTrue(hasattr(UpperCAmelCase_ , 'do_center_crop' ) ) self.assertTrue(hasattr(UpperCAmelCase_ , 'crop_size' ) ) def lowercase__ ( self : int ): lowerCAmelCase : Union[str, Any] = self.image_processing_class.from_dict(self.image_processor_dict ) self.assertEqual(image_processor.size , {'shortest_edge': 20} ) self.assertEqual(image_processor.crop_size , {'height': 18, 'width': 18} ) lowerCAmelCase : List[str] = self.image_processing_class.from_dict(self.image_processor_dict , size=42 , crop_size=84 ) self.assertEqual(image_processor.size , {'shortest_edge': 42} ) self.assertEqual(image_processor.crop_size , {'height': 84, 'width': 84} ) def lowercase__ ( self : str ): pass def lowercase__ ( self : List[str] ): # Initialize image_processing lowerCAmelCase : List[str] = self.image_processing_class(**self.image_processor_dict ) # create random PIL images lowerCAmelCase : 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 lowerCAmelCase : str = image_processing(image_inputs[0] , return_tensors='pt' ).pixel_values self.assertEqual( encoded_images.shape , ( 1, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size['height'], self.image_processor_tester.crop_size['width'], ) , ) # Test batched lowerCAmelCase : Dict = image_processing(UpperCAmelCase_ , return_tensors='pt' ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size['height'], self.image_processor_tester.crop_size['width'], ) , ) def lowercase__ ( self : Optional[Any] ): # Initialize image_processing lowerCAmelCase : Any = self.image_processing_class(**self.image_processor_dict ) # create random numpy tensors lowerCAmelCase : Optional[int] = 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 lowerCAmelCase : 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.crop_size['height'], self.image_processor_tester.crop_size['width'], ) , ) # Test batched lowerCAmelCase : Optional[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.crop_size['height'], self.image_processor_tester.crop_size['width'], ) , ) def lowercase__ ( self : Dict ): # Initialize image_processing lowerCAmelCase : Optional[int] = self.image_processing_class(**self.image_processor_dict ) # create random PyTorch tensors lowerCAmelCase : 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 lowerCAmelCase : 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.crop_size['height'], self.image_processor_tester.crop_size['width'], ) , ) # Test batched lowerCAmelCase : 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.crop_size['height'], self.image_processor_tester.crop_size['width'], ) , )
323
1
import inspect import os import unittest import torch import accelerate from accelerate import debug_launcher from accelerate.test_utils import ( execute_subprocess_async, require_cpu, require_huggingface_suite, require_multi_gpu, require_single_gpu, ) from accelerate.utils import patch_environment @require_huggingface_suite class SCREAMING_SNAKE_CASE__ ( unittest.TestCase ): '''simple docstring''' def _lowerCAmelCase ( self ): A : Dict = inspect.getfile(accelerate.test_utils ) A : str = os.path.sep.join( mod_file.split(os.path.sep )[:-1] + ["""scripts""", """external_deps""", """test_metrics.py"""] ) from accelerate.test_utils.scripts.external_deps import test_metrics # noqa: F401 A : str = test_metrics @require_cpu def _lowerCAmelCase ( self ): debug_launcher(self.test_metrics.main, num_processes=1 ) @require_cpu def _lowerCAmelCase ( self ): debug_launcher(self.test_metrics.main ) @require_single_gpu def _lowerCAmelCase ( self ): self.test_metrics.main() @require_multi_gpu def _lowerCAmelCase ( self ): print(f'''Found {torch.cuda.device_count()} devices.''' ) A : Optional[Any] = ["""torchrun""", f'''--nproc_per_node={torch.cuda.device_count()}''', self.test_file_path] with patch_environment(omp_num_threads=1 ): execute_subprocess_async(lowerCamelCase__, env=os.environ.copy() )
116
import unittest import numpy as np from transformers import AlbertConfig, is_flax_available from transformers.testing_utils import require_flax, slow from ...test_modeling_flax_common import FlaxModelTesterMixin, ids_tensor, random_attention_mask if is_flax_available(): import jax.numpy as jnp from transformers.models.albert.modeling_flax_albert import ( FlaxAlbertForMaskedLM, FlaxAlbertForMultipleChoice, FlaxAlbertForPreTraining, FlaxAlbertForQuestionAnswering, FlaxAlbertForSequenceClassification, FlaxAlbertForTokenClassification, FlaxAlbertModel, ) class SCREAMING_SNAKE_CASE__ ( unittest.TestCase ): '''simple docstring''' def __init__( self, lowerCamelCase__, lowerCamelCase__=13, lowerCamelCase__=7, lowerCamelCase__=True, lowerCamelCase__=True, lowerCamelCase__=True, lowerCamelCase__=True, lowerCamelCase__=99, lowerCamelCase__=32, lowerCamelCase__=5, lowerCamelCase__=4, lowerCamelCase__=37, lowerCamelCase__="gelu", lowerCamelCase__=0.1, lowerCamelCase__=0.1, lowerCamelCase__=512, lowerCamelCase__=16, lowerCamelCase__=2, lowerCamelCase__=0.02, lowerCamelCase__=4, ): A : List[str] = parent A : Optional[int] = batch_size A : Union[str, Any] = seq_length A : Any = is_training A : List[str] = use_attention_mask A : Union[str, Any] = use_token_type_ids A : Any = use_labels A : str = vocab_size A : Union[str, Any] = hidden_size A : str = num_hidden_layers A : List[Any] = num_attention_heads A : Optional[int] = intermediate_size A : Optional[Any] = hidden_act A : Dict = hidden_dropout_prob A : List[Any] = attention_probs_dropout_prob A : Optional[int] = max_position_embeddings A : int = type_vocab_size A : str = type_sequence_label_size A : List[Any] = initializer_range A : str = num_choices def _lowerCAmelCase ( self ): A : Optional[int] = ids_tensor([self.batch_size, self.seq_length], self.vocab_size ) A : Union[str, Any] = None if self.use_attention_mask: A : Optional[int] = random_attention_mask([self.batch_size, self.seq_length] ) A : int = None if self.use_token_type_ids: A : List[Any] = ids_tensor([self.batch_size, self.seq_length], self.type_vocab_size ) A : Optional[int] = AlbertConfig( vocab_size=self.vocab_size, hidden_size=self.hidden_size, num_hidden_layers=self.num_hidden_layers, num_attention_heads=self.num_attention_heads, intermediate_size=self.intermediate_size, hidden_act=self.hidden_act, hidden_dropout_prob=self.hidden_dropout_prob, attention_probs_dropout_prob=self.attention_probs_dropout_prob, max_position_embeddings=self.max_position_embeddings, type_vocab_size=self.type_vocab_size, is_decoder=lowerCamelCase__, initializer_range=self.initializer_range, ) return config, input_ids, token_type_ids, attention_mask def _lowerCAmelCase ( self ): A : Dict = self.prepare_config_and_inputs() A , A , A , A : str = config_and_inputs A : Union[str, Any] = {"""input_ids""": input_ids, """token_type_ids""": token_type_ids, """attention_mask""": attention_mask} return config, inputs_dict @require_flax class SCREAMING_SNAKE_CASE__ ( SCREAMING_SNAKE_CASE__ , unittest.TestCase ): '''simple docstring''' __lowerCamelCase : Any = ( ( FlaxAlbertModel, FlaxAlbertForPreTraining, FlaxAlbertForMaskedLM, FlaxAlbertForMultipleChoice, FlaxAlbertForQuestionAnswering, FlaxAlbertForSequenceClassification, FlaxAlbertForTokenClassification, FlaxAlbertForQuestionAnswering, ) if is_flax_available() else () ) def _lowerCAmelCase ( self ): A : Dict = FlaxAlbertModelTester(self ) @slow def _lowerCAmelCase ( self ): for model_class_name in self.all_model_classes: A : Dict = model_class_name.from_pretrained("""albert-base-v2""" ) A : Optional[int] = model(np.ones((1, 1) ) ) self.assertIsNotNone(lowerCamelCase__ ) @require_flax class SCREAMING_SNAKE_CASE__ ( unittest.TestCase ): '''simple docstring''' @slow def _lowerCAmelCase ( self ): A : Dict = FlaxAlbertModel.from_pretrained("""albert-base-v2""" ) A : List[str] = np.array([[0, 345, 232, 328, 740, 140, 1695, 69, 6078, 1588, 2]] ) A : str = np.array([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]] ) A : Optional[int] = model(lowerCamelCase__, attention_mask=lowerCamelCase__ )[0] A : str = (1, 11, 768) self.assertEqual(output.shape, lowerCamelCase__ ) A : Optional[int] = np.array( [[[-0.6513, 1.5035, -0.2766], [-0.6515, 1.5046, -0.2780], [-0.6512, 1.5049, -0.2784]]] ) self.assertTrue(jnp.allclose(output[:, 1:4, 1:4], lowerCamelCase__, atol=1e-4 ) )
116
1
from ..utils import DummyObject, requires_backends class __lowerCAmelCase ( metaclass=__magic_name__ ): UpperCamelCase__ = ['''keras_nlp'''] def __init__( self :Union[str, Any] , *__magic_name__ :Union[str, Any] , **__magic_name__ :str ): '''simple docstring''' requires_backends(self , ["""keras_nlp"""] )
347
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available, is_vision_available __UpperCamelCase : Optional[Any] = { "configuration_mobilenet_v2": [ "MOBILENET_V2_PRETRAINED_CONFIG_ARCHIVE_MAP", "MobileNetV2Config", "MobileNetV2OnnxConfig", ], } try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __UpperCamelCase : int = ["MobileNetV2FeatureExtractor"] __UpperCamelCase : Tuple = ["MobileNetV2ImageProcessor"] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __UpperCamelCase : int = [ "MOBILENET_V2_PRETRAINED_MODEL_ARCHIVE_LIST", "MobileNetV2ForImageClassification", "MobileNetV2ForSemanticSegmentation", "MobileNetV2Model", "MobileNetV2PreTrainedModel", "load_tf_weights_in_mobilenet_v2", ] if TYPE_CHECKING: from .configuration_mobilenet_va import ( MOBILENET_V2_PRETRAINED_CONFIG_ARCHIVE_MAP, MobileNetVaConfig, MobileNetVaOnnxConfig, ) try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .feature_extraction_mobilenet_va import MobileNetVaFeatureExtractor from .image_processing_mobilenet_va import MobileNetVaImageProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_mobilenet_va import ( MOBILENET_V2_PRETRAINED_MODEL_ARCHIVE_LIST, MobileNetVaForImageClassification, MobileNetVaForSemanticSegmentation, MobileNetVaModel, MobileNetVaPreTrainedModel, load_tf_weights_in_mobilenet_va, ) else: import sys __UpperCamelCase : Optional[Any] = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
347
1
"""simple docstring""" import argparse import os import evaluate import torch from datasets import load_dataset from torch.optim import AdamW from torch.utils.data import DataLoader from transformers import AutoModelForSequenceClassification, AutoTokenizer, get_linear_schedule_with_warmup, set_seed from accelerate import Accelerator, DistributedType ######################################################################## # This is a fully working simple example to use Accelerate, # specifically showcasing how to properly calculate the metrics on the # validation dataset when in a distributed system, and builds off the # `nlp_example.py` script. # # This example trains a Bert base model on GLUE MRPC # in any of the following settings (with the same script): # - single CPU or single GPU # - multi GPUS (using PyTorch distributed mode) # - (multi) TPUs # - fp16 (mixed-precision) or fp32 (normal precision) # # To help focus on the differences in the code, building `DataLoaders` # was refactored into its own function. # New additions from the base script can be found quickly by # looking for the # New Code # tags # # To run it in each of these various modes, follow the instructions # in the readme for examples: # https://github.com/huggingface/accelerate/tree/main/examples # ######################################################################## lowercase_ = 16 lowercase_ = 32 def lowerCAmelCase ( __UpperCamelCase , __UpperCamelCase = 1_6 ): """simple docstring""" __A = AutoTokenizer.from_pretrained('''bert-base-cased''' ) __A = load_dataset('''glue''' , '''mrpc''' ) def tokenize_function(__UpperCamelCase ): # max_length=None => use the model max length (it's actually the default) __A = tokenizer(examples['''sentence1'''] , examples['''sentence2'''] , truncation=lowerCamelCase_ , max_length=lowerCamelCase_ ) return outputs # Apply the method we just defined to all the examples in all the splits of the dataset # starting with the main process first: with accelerator.main_process_first(): __A = datasets.map( lowerCamelCase_ , batched=lowerCamelCase_ , remove_columns=['''idx''', '''sentence1''', '''sentence2'''] , ) # We also rename the 'label' column to 'labels' which is the expected name for labels by the models of the # transformers library __A = tokenized_datasets.rename_column('''label''' , '''labels''' ) def collate_fn(__UpperCamelCase ): # On TPU it's best to pad everything to the same length or training will be very slow. __A = 1_2_8 if accelerator.distributed_type == DistributedType.TPU else None # When using mixed precision we want round multiples of 8/16 if accelerator.mixed_precision == "fp8": __A = 1_6 elif accelerator.mixed_precision != "no": __A = 8 else: __A = None return tokenizer.pad( lowerCamelCase_ , padding='''longest''' , max_length=lowerCamelCase_ , pad_to_multiple_of=lowerCamelCase_ , return_tensors='''pt''' , ) # Instantiate dataloaders. __A = DataLoader( tokenized_datasets['''train'''] , shuffle=lowerCamelCase_ , collate_fn=lowerCamelCase_ , batch_size=lowerCamelCase_ ) __A = DataLoader( tokenized_datasets['''validation'''] , shuffle=lowerCamelCase_ , collate_fn=lowerCamelCase_ , batch_size=lowerCamelCase_ ) return train_dataloader, eval_dataloader # For testing only if os.environ.get('TESTING_MOCKED_DATALOADERS', None) == "1": from accelerate.test_utils.training import mocked_dataloaders lowercase_ = mocked_dataloaders # noqa: F811 def lowerCAmelCase ( __UpperCamelCase , __UpperCamelCase ): """simple docstring""" if os.environ.get('''TESTING_MOCKED_DATALOADERS''' , lowerCamelCase_ ) == "1": __A = 2 # Initialize accelerator __A = Accelerator(cpu=args.cpu , mixed_precision=args.mixed_precision ) # Sample hyper-parameters for learning rate, batch size, seed and a few other HPs __A = config['''lr'''] __A = int(config['''num_epochs'''] ) __A = int(config['''seed'''] ) __A = int(config['''batch_size'''] ) __A = evaluate.load('''glue''' , '''mrpc''' ) # If the batch size is too big we use gradient accumulation __A = 1 if batch_size > MAX_GPU_BATCH_SIZE and accelerator.distributed_type != DistributedType.TPU: __A = batch_size // MAX_GPU_BATCH_SIZE __A = MAX_GPU_BATCH_SIZE set_seed(lowerCamelCase_ ) __A = get_dataloaders(lowerCamelCase_ , lowerCamelCase_ ) # Instantiate the model (we build the model here so that the seed also control new weights initialization) __A = AutoModelForSequenceClassification.from_pretrained('''bert-base-cased''' , return_dict=lowerCamelCase_ ) # We could avoid this line since the accelerator is set with `device_placement=True` (default value). # Note that if you are placing tensors on devices manually, this line absolutely needs to be before the optimizer # creation otherwise training will not work on TPU (`accelerate` will kindly throw an error to make us aware of that). __A = model.to(accelerator.device ) # Instantiate optimizer __A = AdamW(params=model.parameters() , lr=lowerCamelCase_ ) # Instantiate scheduler __A = get_linear_schedule_with_warmup( optimizer=lowerCamelCase_ , num_warmup_steps=1_0_0 , num_training_steps=(len(lowerCamelCase_ ) * num_epochs) // gradient_accumulation_steps , ) # Prepare everything # There is no specific order to remember, we just need to unpack the objects in the same order we gave them to the # prepare method. __A = accelerator.prepare( lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ ) # Now we train the model for epoch in range(lowerCamelCase_ ): model.train() for step, batch in enumerate(lowerCamelCase_ ): # We could avoid this line since we set the accelerator with `device_placement=True`. batch.to(accelerator.device ) __A = model(**lowerCamelCase_ ) __A = outputs.loss __A = loss / gradient_accumulation_steps accelerator.backward(lowerCamelCase_ ) if step % gradient_accumulation_steps == 0: optimizer.step() lr_scheduler.step() optimizer.zero_grad() model.eval() __A = 0 for step, batch in enumerate(lowerCamelCase_ ): # We could avoid this line since we set the accelerator with `device_placement=True`. batch.to(accelerator.device ) with torch.no_grad(): __A = model(**lowerCamelCase_ ) __A = outputs.logits.argmax(dim=-1 ) __A = accelerator.gather((predictions, batch['''labels''']) ) # New Code # # First we check if it's a distributed system if accelerator.use_distributed: # Then see if we're on the last batch of our eval dataloader if step == len(lowerCamelCase_ ) - 1: # Last batch needs to be truncated on distributed systems as it contains additional samples __A = predictions[: len(eval_dataloader.dataset ) - samples_seen] __A = references[: len(eval_dataloader.dataset ) - samples_seen] else: # Otherwise we add the number of samples seen samples_seen += references.shape[0] # All of this can be avoided if you use `Accelerator.gather_for_metrics` instead of `Accelerator.gather`: # accelerator.gather_for_metrics((predictions, batch["labels"])) metric.add_batch( predictions=lowerCamelCase_ , references=lowerCamelCase_ , ) __A = metric.compute() # Use accelerator.print to print only on the main process. accelerator.print(f'epoch {epoch}:' , lowerCamelCase_ ) def lowerCAmelCase ( ): """simple docstring""" __A = argparse.ArgumentParser(description='''Simple example of training script.''' ) parser.add_argument( '''--mixed_precision''' , type=lowerCamelCase_ , default=lowerCamelCase_ , choices=['''no''', '''fp16''', '''bf16''', '''fp8'''] , help='''Whether to use mixed precision. Choose''' '''between fp16 and bf16 (bfloat16). Bf16 requires PyTorch >= 1.10.''' '''and an Nvidia Ampere GPU.''' , ) parser.add_argument('''--cpu''' , action='''store_true''' , help='''If passed, will train on the CPU.''' ) __A = parser.parse_args() __A = {'''lr''': 2e-5, '''num_epochs''': 3, '''seed''': 4_2, '''batch_size''': 1_6} training_function(lowerCamelCase_ , lowerCamelCase_ ) if __name__ == "__main__": main()
266
# Function to print upper half of diamond (pyramid) def lowerCAmelCase__ ( lowerCamelCase_ : Optional[int]): '''simple docstring''' for i in range(0 ,lowerCamelCase_): for _ in range(0 ,n - i - 1): # printing spaces print(''' ''' ,end='''''') for _ in range(0 ,i + 1): # printing stars print('''* ''' ,end='''''') print() def lowerCAmelCase__ ( lowerCamelCase_ : str): '''simple docstring''' for i in range(lowerCamelCase_ ,0 ,-1): for _ in range(lowerCamelCase_ ,0 ,-1): # printing stars print('''* ''' ,end='''''') print() for _ in range(n - i + 1 ,0 ,-1): # printing spaces print(''' ''' ,end='''''') def lowerCAmelCase__ ( lowerCamelCase_ : Tuple): '''simple docstring''' if n <= 0: print(''' ... .... nothing printing :(''') return floyd(lowerCamelCase_) # upper half reverse_floyd(lowerCamelCase_) # lower half if __name__ == "__main__": print(R'| /\ | |- | |- |--| |\ /| |-') print(R'|/ \| |- |_ |_ |__| | \/ | |_') __snake_case : int =1 while K: __snake_case : Optional[int] =int(input('enter the number and , and see the magic : ')) print() pretty_print(user_number) __snake_case : str =int(input('press 0 to exit... and 1 to continue...')) print('Good Bye...')
129
0
'''simple docstring''' from collections import deque def __lowerCamelCase ( __lowerCAmelCase : Optional[Any] ) -> List[Any]: snake_case = len(__lowerCAmelCase ) snake_case = deque() snake_case = [False for _ in range(__lowerCAmelCase )] snake_case = [-1 for _ in range(__lowerCAmelCase )] snake_case = index_of[:] def strong_connect(__lowerCAmelCase : List[str] , __lowerCAmelCase : Tuple , __lowerCAmelCase : List[str] ): snake_case = index # the number when this node is seen snake_case = index # lowest rank node reachable from here index += 1 stack.append(__lowerCAmelCase ) snake_case = True for w in g[v]: if index_of[w] == -1: snake_case = strong_connect(__lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase ) snake_case = ( lowlink_of[w] if lowlink_of[w] < lowlink_of[v] else lowlink_of[v] ) elif on_stack[w]: snake_case = ( lowlink_of[w] if lowlink_of[w] < lowlink_of[v] else lowlink_of[v] ) if lowlink_of[v] == index_of[v]: snake_case = [] snake_case = stack.pop() snake_case = False component.append(__lowerCAmelCase ) while w != v: snake_case = stack.pop() snake_case = False component.append(__lowerCAmelCase ) components.append(__lowerCAmelCase ) return index snake_case = [] for v in range(__lowerCAmelCase ): if index_of[v] == -1: strong_connect(__lowerCAmelCase , 0 , __lowerCAmelCase ) return components def __lowerCamelCase ( __lowerCAmelCase : Dict , __lowerCAmelCase : Optional[Any] ) -> List[Any]: snake_case = [[] for _ in range(__lowerCAmelCase )] for u, v in edges: g[u].append(__lowerCAmelCase ) return g if __name__ == "__main__": # Test _SCREAMING_SNAKE_CASE = 7 _SCREAMING_SNAKE_CASE = [0, 0, 1, 2, 3, 3, 4, 4, 6] _SCREAMING_SNAKE_CASE = [1, 3, 2, 0, 1, 4, 5, 6, 5] _SCREAMING_SNAKE_CASE = [(u, v) for u, v in zip(source, target)] _SCREAMING_SNAKE_CASE = create_graph(n_vertices, edges) assert [[5], [6], [4], [3, 2, 1, 0]] == tarjan(g)
3
'''simple docstring''' def __lowerCamelCase ( __lowerCAmelCase : Dict ) -> Optional[Any]: return [ { 0: [1, 2], 1: [0, 2], 2: [0, 1, 3, 5], 3: [2, 4], 4: [3], 5: [2, 6, 8], 6: [5, 7], 7: [6, 8], 8: [5, 7], }, { 0: [6], 1: [9], 2: [4, 5], 3: [4], 4: [2, 3], 5: [2], 6: [0, 7], 7: [6], 8: [], 9: [1], }, { 0: [4], 1: [6], 2: [], 3: [5, 6, 7], 4: [0, 6], 5: [3, 8, 9], 6: [1, 3, 4, 7], 7: [3, 6, 8, 9], 8: [5, 7], 9: [5, 7], }, { 0: [1, 3], 1: [0, 2, 4], 2: [1, 3, 4], 3: [0, 2, 4], 4: [1, 2, 3], }, ][index] def __lowerCamelCase ( __lowerCAmelCase : dict[int, list[int]] ) -> list[tuple[int, int]]: snake_case = 0 snake_case = len(__lowerCAmelCase ) # No of vertices in graph snake_case = [0] * n snake_case = [False] * n def dfs(__lowerCAmelCase : Tuple , __lowerCAmelCase : str , __lowerCAmelCase : Union[str, Any] , __lowerCAmelCase : List[str] ): snake_case = True snake_case = id_ id_ += 1 for to in graph[at]: if to == parent: pass elif not visited[to]: dfs(__lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , id_ ) snake_case = min(low[at] , low[to] ) if id_ <= low[to]: bridges.append((at, to) if at < to else (to, at) ) else: # This edge is a back edge and cannot be a bridge snake_case = min(low[at] , low[to] ) snake_case = [] for i in range(__lowerCAmelCase ): if not visited[i]: dfs(__lowerCAmelCase , -1 , __lowerCAmelCase , id_ ) return bridges if __name__ == "__main__": import doctest doctest.testmod()
3
1
from __future__ import annotations def SCREAMING_SNAKE_CASE__ ( lowerCamelCase__ , lowerCamelCase__ ) -> List[str]: # Checks if the entire collection has been sorted if len(lowerCamelCase__ ) <= 1 or n <= 1: return insert_next(lowerCamelCase__ , n - 1 ) rec_insertion_sort(lowerCamelCase__ , n - 1 ) def SCREAMING_SNAKE_CASE__ ( lowerCamelCase__ , lowerCamelCase__ ) -> str: # Checks order between adjacent elements if index >= len(lowerCamelCase__ ) or collection[index - 1] <= collection[index]: return # Swaps adjacent elements since they are not in ascending order __lowerCamelCase , __lowerCamelCase : Union[str, Any] = ( collection[index], collection[index - 1], ) insert_next(lowerCamelCase__ , index + 1 ) if __name__ == "__main__": a =input("""Enter integers separated by spaces: """) a =[int(num) for num in numbers.split()] rec_insertion_sort(number_list, len(number_list)) print(number_list)
73
import re import warnings from contextlib import contextmanager from ...processing_utils import ProcessorMixin class A_ ( SCREAMING_SNAKE_CASE ): _UpperCAmelCase : Any = ['''image_processor''', '''tokenizer'''] _UpperCAmelCase : List[Any] = '''AutoImageProcessor''' _UpperCAmelCase : Dict = '''AutoTokenizer''' def __init__( self : Union[str, Any] ,SCREAMING_SNAKE_CASE__ : Optional[int]=None ,SCREAMING_SNAKE_CASE__ : List[Any]=None ,**SCREAMING_SNAKE_CASE__ : Union[str, Any]): __lowerCamelCase : List[str] = None if "feature_extractor" in kwargs: warnings.warn( 'The `feature_extractor` argument is deprecated and will be removed in v5, use `image_processor`' ' instead.' ,SCREAMING_SNAKE_CASE__ ,) __lowerCamelCase : Union[str, Any] = kwargs.pop('feature_extractor') __lowerCamelCase : Dict = image_processor if image_processor is not None else feature_extractor if image_processor is None: raise ValueError('You need to specify an `image_processor`.') if tokenizer is None: raise ValueError('You need to specify a `tokenizer`.') super().__init__(SCREAMING_SNAKE_CASE__ ,SCREAMING_SNAKE_CASE__) __lowerCamelCase : Dict = self.image_processor __lowerCamelCase : Optional[int] = False def __call__( self : int ,*SCREAMING_SNAKE_CASE__ : Union[str, Any] ,**SCREAMING_SNAKE_CASE__ : Union[str, Any]): # For backward compatibility if self._in_target_context_manager: return self.current_processor(*SCREAMING_SNAKE_CASE__ ,**SCREAMING_SNAKE_CASE__) __lowerCamelCase : Optional[int] = kwargs.pop('images' ,SCREAMING_SNAKE_CASE__) __lowerCamelCase : Union[str, Any] = kwargs.pop('text' ,SCREAMING_SNAKE_CASE__) if len(SCREAMING_SNAKE_CASE__) > 0: __lowerCamelCase : int = args[0] __lowerCamelCase : List[str] = args[1:] if images is None and text is None: raise ValueError('You need to specify either an `images` or `text` input to process.') if images is not None: __lowerCamelCase : Optional[int] = self.image_processor(SCREAMING_SNAKE_CASE__ ,*SCREAMING_SNAKE_CASE__ ,**SCREAMING_SNAKE_CASE__) if text is not None: __lowerCamelCase : List[Any] = self.tokenizer(SCREAMING_SNAKE_CASE__ ,**SCREAMING_SNAKE_CASE__) if text is None: return inputs elif images is None: return encodings else: __lowerCamelCase : Optional[Any] = encodings['input_ids'] return inputs def lowerCAmelCase ( self : int ,*SCREAMING_SNAKE_CASE__ : Union[str, Any] ,**SCREAMING_SNAKE_CASE__ : Dict): return self.tokenizer.batch_decode(*SCREAMING_SNAKE_CASE__ ,**SCREAMING_SNAKE_CASE__) def lowerCAmelCase ( self : Optional[Any] ,*SCREAMING_SNAKE_CASE__ : List[Any] ,**SCREAMING_SNAKE_CASE__ : Any): return self.tokenizer.decode(*SCREAMING_SNAKE_CASE__ ,**SCREAMING_SNAKE_CASE__) @contextmanager def lowerCAmelCase ( self : Tuple): warnings.warn( '`as_target_processor` is deprecated and will be removed in v5 of Transformers. You can process your ' 'labels by using the argument `text` of the regular `__call__` method (either in the same call as ' 'your images inputs, or in a separate call.') __lowerCamelCase : List[Any] = True __lowerCamelCase : str = self.tokenizer yield __lowerCamelCase : Tuple = self.image_processor __lowerCamelCase : Tuple = False def lowerCAmelCase ( self : Any ,SCREAMING_SNAKE_CASE__ : str ,SCREAMING_SNAKE_CASE__ : int=False ,SCREAMING_SNAKE_CASE__ : List[Any]=None): if added_vocab is None: __lowerCamelCase : str = self.tokenizer.get_added_vocab() __lowerCamelCase : Union[str, Any] = {} while tokens: __lowerCamelCase : Tuple = re.search(R'<s_(.*?)>' ,SCREAMING_SNAKE_CASE__ ,re.IGNORECASE) if start_token is None: break __lowerCamelCase : Dict = start_token.group(1) __lowerCamelCase : List[str] = re.search(RF"</s_{key}>" ,SCREAMING_SNAKE_CASE__ ,re.IGNORECASE) __lowerCamelCase : Optional[int] = start_token.group() if end_token is None: __lowerCamelCase : List[Any] = tokens.replace(SCREAMING_SNAKE_CASE__ ,'') else: __lowerCamelCase : Tuple = end_token.group() __lowerCamelCase : int = re.escape(SCREAMING_SNAKE_CASE__) __lowerCamelCase : str = re.escape(SCREAMING_SNAKE_CASE__) __lowerCamelCase : Union[str, Any] = re.search(F"{start_token_escaped}(.*?){end_token_escaped}" ,SCREAMING_SNAKE_CASE__ ,re.IGNORECASE) if content is not None: __lowerCamelCase : List[Any] = content.group(1).strip() if r"<s_" in content and r"</s_" in content: # non-leaf node __lowerCamelCase : str = self.tokenajson(SCREAMING_SNAKE_CASE__ ,is_inner_value=SCREAMING_SNAKE_CASE__ ,added_vocab=SCREAMING_SNAKE_CASE__) if value: if len(SCREAMING_SNAKE_CASE__) == 1: __lowerCamelCase : Tuple = value[0] __lowerCamelCase : int = value else: # leaf nodes __lowerCamelCase : Tuple = [] for leaf in content.split(R'<sep/>'): __lowerCamelCase : List[Any] = leaf.strip() if leaf in added_vocab and leaf[0] == "<" and leaf[-2:] == "/>": __lowerCamelCase : str = leaf[1:-2] # for categorical special tokens output[key].append(SCREAMING_SNAKE_CASE__) if len(output[key]) == 1: __lowerCamelCase : Dict = output[key][0] __lowerCamelCase : Dict = tokens[tokens.find(SCREAMING_SNAKE_CASE__) + len(SCREAMING_SNAKE_CASE__) :].strip() if tokens[:6] == r"<sep/>": # non-leaf nodes return [output] + self.tokenajson(tokens[6:] ,is_inner_value=SCREAMING_SNAKE_CASE__ ,added_vocab=SCREAMING_SNAKE_CASE__) if len(SCREAMING_SNAKE_CASE__): return [output] if is_inner_value else output else: return [] if is_inner_value else {"text_sequence": tokens} @property def lowerCAmelCase ( self : List[str]): warnings.warn( '`feature_extractor_class` is deprecated and will be removed in v5. Use `image_processor_class` instead.' ,SCREAMING_SNAKE_CASE__ ,) return self.image_processor_class @property def lowerCAmelCase ( self : List[Any]): warnings.warn( '`feature_extractor` is deprecated and will be removed in v5. Use `image_processor` instead.' ,SCREAMING_SNAKE_CASE__ ,) return self.image_processor
73
1
'''simple docstring''' def _UpperCamelCase ( UpperCamelCase__ ): return sum(i for i in range(1 , number // 2 + 1 ) if number % i == 0 ) == number if __name__ == "__main__": print('Program to check whether a number is a Perfect number or not...') __A =int(input('Enter number: ').strip()) print(f"""{number} is {'' if perfect(number) else 'not '}a Perfect Number.""")
283
'''simple docstring''' import numpy class _snake_case : def __init__( self , _lowerCamelCase , _lowerCamelCase): UpperCAmelCase__ : Dict = input_array # Random initial weights are assigned where first argument is the # number of nodes in previous layer and second argument is the # number of nodes in the next layer. # Random initial weights are assigned. # self.input_array.shape[1] is used to represent number of nodes in input layer. # First hidden layer consists of 4 nodes. UpperCAmelCase__ : Optional[Any] = numpy.random.rand( self.input_array.shape[1] , 4) # Random initial values for the first hidden layer. # First hidden layer has 4 nodes. # Second hidden layer has 3 nodes. UpperCAmelCase__ : Optional[int] = numpy.random.rand( 4 , 3) # Random initial values for the second hidden layer. # Second hidden layer has 3 nodes. # Output layer has 1 node. UpperCAmelCase__ : Any = numpy.random.rand(3 , 1) # Real output values provided. UpperCAmelCase__ : Tuple = output_array # Predicted output values by the neural network. # Predicted_output array initially consists of zeroes. UpperCAmelCase__ : Union[str, Any] = numpy.zeros(output_array.shape) def snake_case__ ( self): UpperCAmelCase__ : List[str] = sigmoid( numpy.dot(self.input_array , self.input_layer_and_first_hidden_layer_weights)) # layer_between_first_hidden_layer_and_second_hidden_layer is the layer # connecting the first hidden set of nodes with the second hidden set of nodes. UpperCAmelCase__ : Any = sigmoid( numpy.dot( self.layer_between_input_and_first_hidden_layer , self.first_hidden_layer_and_second_hidden_layer_weights , )) # layer_between_second_hidden_layer_and_output is the layer connecting # second hidden layer with the output node. UpperCAmelCase__ : Tuple = sigmoid( numpy.dot( self.layer_between_first_hidden_layer_and_second_hidden_layer , self.second_hidden_layer_and_output_layer_weights , )) return self.layer_between_second_hidden_layer_and_output def snake_case__ ( self): UpperCAmelCase__ : Optional[int] = numpy.dot( self.layer_between_first_hidden_layer_and_second_hidden_layer.T , 2 * (self.output_array - self.predicted_output) * sigmoid_derivative(self.predicted_output) , ) UpperCAmelCase__ : str = numpy.dot( self.layer_between_input_and_first_hidden_layer.T , numpy.dot( 2 * (self.output_array - self.predicted_output) * sigmoid_derivative(self.predicted_output) , self.second_hidden_layer_and_output_layer_weights.T , ) * sigmoid_derivative( self.layer_between_first_hidden_layer_and_second_hidden_layer) , ) UpperCAmelCase__ : Any = numpy.dot( self.input_array.T , numpy.dot( numpy.dot( 2 * (self.output_array - self.predicted_output) * sigmoid_derivative(self.predicted_output) , self.second_hidden_layer_and_output_layer_weights.T , ) * sigmoid_derivative( self.layer_between_first_hidden_layer_and_second_hidden_layer) , self.first_hidden_layer_and_second_hidden_layer_weights.T , ) * sigmoid_derivative(self.layer_between_input_and_first_hidden_layer) , ) self.input_layer_and_first_hidden_layer_weights += ( updated_input_layer_and_first_hidden_layer_weights ) self.first_hidden_layer_and_second_hidden_layer_weights += ( updated_first_hidden_layer_and_second_hidden_layer_weights ) self.second_hidden_layer_and_output_layer_weights += ( updated_second_hidden_layer_and_output_layer_weights ) def snake_case__ ( self , _lowerCamelCase , _lowerCamelCase , _lowerCamelCase): for iteration in range(1 , iterations + 1): UpperCAmelCase__ : Optional[Any] = self.feedforward() self.back_propagation() if give_loss: UpperCAmelCase__ : str = numpy.mean(numpy.square(output - self.feedforward())) print(f'''Iteration {iteration} Loss: {loss}''') def snake_case__ ( self , _lowerCamelCase): UpperCAmelCase__ : List[Any] = input_arr UpperCAmelCase__ : Tuple = sigmoid( numpy.dot(self.array , self.input_layer_and_first_hidden_layer_weights)) UpperCAmelCase__ : List[Any] = sigmoid( numpy.dot( self.layer_between_input_and_first_hidden_layer , self.first_hidden_layer_and_second_hidden_layer_weights , )) UpperCAmelCase__ : Optional[Any] = sigmoid( numpy.dot( self.layer_between_first_hidden_layer_and_second_hidden_layer , self.second_hidden_layer_and_output_layer_weights , )) return int(self.layer_between_second_hidden_layer_and_output > 0.6) def _UpperCamelCase ( UpperCamelCase__ ): return 1 / (1 + numpy.exp(-value )) def _UpperCamelCase ( UpperCamelCase__ ): return (value) * (1 - (value)) def _UpperCamelCase ( ): UpperCAmelCase__ : Union[str, Any] = numpy.array( ( [0, 0, 0], [0, 0, 1], [0, 1, 0], [0, 1, 1], [1, 0, 0], [1, 0, 1], [1, 1, 0], [1, 1, 1], ) , dtype=numpy.floataa , ) # True output values for the given input values. UpperCAmelCase__ : str = numpy.array(([0], [1], [1], [0], [1], [0], [0], [1]) , dtype=numpy.floataa ) # Calling neural network class. UpperCAmelCase__ : List[Any] = TwoHiddenLayerNeuralNetwork( input_array=UpperCamelCase__ , output_array=UpperCamelCase__ ) # Calling training function. # Set give_loss to True if you want to see loss in every iteration. neural_network.train(output=UpperCamelCase__ , iterations=1_0 , give_loss=UpperCamelCase__ ) return neural_network.predict(numpy.array(([1, 1, 1]) , dtype=numpy.floataa ) ) if __name__ == "__main__": example()
283
1
lowerCAmelCase : Union[str, Any] = {0: [2, 3], 1: [0], 2: [1], 3: [4], 4: []} lowerCAmelCase : str = {0: [1, 2, 3], 1: [2], 2: [0], 3: [4], 4: [5], 5: [3]} def A_ ( _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase ): SCREAMING_SNAKE_CASE_: Union[str, Any] = True SCREAMING_SNAKE_CASE_: Any = [] for neighbour in graph[vert]: if not visited[neighbour]: order += topology_sort(_UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase ) order.append(_UpperCAmelCase ) return order def A_ ( _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase ): SCREAMING_SNAKE_CASE_: int = True SCREAMING_SNAKE_CASE_: Any = [vert] for neighbour in reversed_graph[vert]: if not visited[neighbour]: component += find_components(_UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase ) return component def A_ ( _UpperCAmelCase ): SCREAMING_SNAKE_CASE_: Optional[Any] = len(_UpperCAmelCase ) * [False] SCREAMING_SNAKE_CASE_: dict[int, list[int]] = {vert: [] for vert in range(len(_UpperCAmelCase ) )} for vert, neighbours in graph.items(): for neighbour in neighbours: reversed_graph[neighbour].append(_UpperCAmelCase ) SCREAMING_SNAKE_CASE_: str = [] for i, was_visited in enumerate(_UpperCAmelCase ): if not was_visited: order += topology_sort(_UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase ) SCREAMING_SNAKE_CASE_: List[str] = [] SCREAMING_SNAKE_CASE_: List[Any] = len(_UpperCAmelCase ) * [False] for i in range(len(_UpperCAmelCase ) ): SCREAMING_SNAKE_CASE_: List[str] = order[len(_UpperCAmelCase ) - i - 1] if not visited[vert]: SCREAMING_SNAKE_CASE_: int = find_components(_UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase ) components_list.append(_UpperCAmelCase ) return components_list
13
'''simple docstring''' from copy import deepcopy import torch import torch.nn.functional as F from torch.optim import AdamW from torch.optim.lr_scheduler import LambdaLR from torch.utils.data import DataLoader from accelerate.accelerator import Accelerator from accelerate.state import GradientState from accelerate.test_utils import RegressionDataset, RegressionModel from accelerate.utils import DistributedType, is_torch_version, set_seed def _lowerCamelCase ( lowercase : Optional[Any] , lowercase : Optional[int] , lowercase : Optional[Any] , lowercase : Dict ) -> str: for param, grad_param in zip(model_a.parameters() , model_b.parameters() ): if not param.requires_grad: continue if not did_step: # Grads should not be in sync assert ( torch.allclose(param.grad , grad_param.grad ) is False ), F'Gradients in sync when they should not be at iteration {iteration}:\nmodel_a grad ({param.grad}) == model_b grad ({grad_param.grad})' else: # Grads should be in sync assert ( torch.allclose(param.grad , grad_param.grad ) is True ), F'Gradients not in sync when they should be at iteration {iteration}:\nmodel_a grad ({param.grad}) != model_b grad ({grad_param.grad})' def _lowerCamelCase ( lowercase : Optional[Any] , lowercase : int , lowercase : Tuple , lowercase : Optional[int] , lowercase : int=True ) -> Any: model.train() _a = model(lowercase ) _a = F.mse_loss(lowercase , target.to(output.device ) ) if not do_backward: loss /= accelerator.gradient_accumulation_steps loss.backward() else: accelerator.backward(lowercase ) def _lowerCamelCase ( lowercase : int , lowercase : Tuple=False ) -> List[str]: set_seed(42 ) _a = RegressionModel() _a = deepcopy(lowercase ) _a = RegressionDataset(length=80 ) _a = DataLoader(lowercase , batch_size=16 ) model.to(accelerator.device ) if sched: _a = AdamW(params=model.parameters() , lr=1E-3 ) _a = AdamW(params=ddp_model.parameters() , lr=1E-3 ) _a = LambdaLR(lowercase , lr_lambda=lambda lowercase : epoch**0.65 ) _a = LambdaLR(lowercase , lr_lambda=lambda lowercase : epoch**0.65 ) # Make a copy of `model` if sched: _a , _a , _a , _a = accelerator.prepare(lowercase , lowercase , lowercase , lowercase ) else: _a , _a = accelerator.prepare(lowercase , lowercase ) if sched: return (model, opt, sched, dataloader, ddp_model, ddp_opt, ddp_sched) return model, ddp_model, dataloader def _lowerCamelCase ( lowercase : Optional[Any] ) -> Optional[int]: # Test when on a single CPU or GPU that the context manager does nothing _a , _a , _a = get_training_setup(lowercase ) # Use a single batch _a , _a = next(iter(lowercase ) ).values() for iteration in range(3 ): # Gather the distributed inputs and targs for the base model _a , _a = accelerator.gather((ddp_input, ddp_target) ) _a , _a = input.to(accelerator.device ), target.to(accelerator.device ) # Perform our initial ground truth step in non "DDP" step_model(lowercase , lowercase , lowercase , lowercase ) # Do "gradient accumulation" (noop) if iteration % 2 == 0: # Accumulate grads locally with accelerator.no_sync(lowercase ): step_model(lowercase , lowercase , lowercase , lowercase ) else: # Sync grads step_model(lowercase , lowercase , lowercase , lowercase ) # Since `no_sync` is a noop, `ddp_model` and `model` grads should always be in sync check_model_parameters(lowercase , lowercase , lowercase , lowercase ) for param, ddp_param in zip(model.parameters() , ddp_model.parameters() ): if not param.requires_grad: continue assert torch.allclose( param.grad , ddp_param.grad ), F'Gradients not in sync when they should be:\nModel grad ({param.grad}) != DDP grad ({ddp_param.grad})' # Shuffle ddp_input on each iteration torch.manual_seed(1337 + iteration ) _a = ddp_input[torch.randperm(len(lowercase ) )] def _lowerCamelCase ( lowercase : Tuple ) -> Tuple: # Test on distributed setup that context manager behaves properly _a , _a , _a = get_training_setup(lowercase ) # Use a single batch _a , _a = next(iter(lowercase ) ).values() for iteration in range(3 ): # Gather the distributed inputs and targs for the base model _a , _a = accelerator.gather((ddp_input, ddp_target) ) _a , _a = input.to(accelerator.device ), target.to(accelerator.device ) # Perform our initial ground truth step in non "DDP" step_model(lowercase , lowercase , lowercase , lowercase ) # Do "gradient accumulation" (noop) if iteration % 2 == 0: # Accumulate grads locally with accelerator.no_sync(lowercase ): step_model(lowercase , lowercase , lowercase , lowercase ) else: # Sync grads step_model(lowercase , lowercase , lowercase , lowercase ) # DDP model and model should only be in sync when not (iteration % 2 == 0) for param, ddp_param in zip(model.parameters() , ddp_model.parameters() ): if not param.requires_grad: continue if iteration % 2 == 0: # Grads should not be in sync assert ( torch.allclose(param.grad , ddp_param.grad ) is False ), F'Gradients in sync when they should not be:\nModel grad ({param.grad}) == DDP grad ({ddp_param.grad})' else: # Grads should be in sync assert ( torch.allclose(param.grad , ddp_param.grad ) is True ), F'Gradients not in sync when they should be:\nModel grad ({param.grad}) != DDP grad ({ddp_param.grad})' # Shuffle ddp_input on each iteration torch.manual_seed(1337 + iteration ) _a = ddp_input[torch.randperm(len(lowercase ) )] def _lowerCamelCase ( lowercase : List[Any]=False , lowercase : Optional[int]=False ) -> Any: _a = Accelerator( split_batches=lowercase , dispatch_batches=lowercase , gradient_accumulation_steps=2 ) # Test that context manager behaves properly _a , _a , _a = get_training_setup(lowercase ) for iteration, batch in enumerate(lowercase ): _a , _a = batch.values() # Gather the distributed inputs and targs for the base model _a , _a = accelerator.gather((ddp_input, ddp_target) ) _a , _a = input.to(accelerator.device ), target.to(accelerator.device ) # Perform our initial ground truth step in non "DDP" step_model(lowercase , lowercase , lowercase , lowercase , lowercase ) # Do "gradient accumulation" (noop) with accelerator.accumulate(lowercase ): step_model(lowercase , lowercase , lowercase , lowercase ) # DDP model and model should only be in sync when not (iteration % 2 == 0) for param, ddp_param in zip(model.parameters() , ddp_model.parameters() ): if not param.requires_grad: continue if ((iteration + 1) % 2 == 0) or (iteration == len(lowercase ) - 1): # Grads should be in sync assert ( torch.allclose(param.grad , ddp_param.grad ) is True ), F'Gradients not in sync when they should be at iteration {iteration}:\nModel grad ({param.grad}) != DDP grad ({ddp_param.grad})' else: # Grads should not be in sync assert ( torch.allclose(param.grad , ddp_param.grad ) is False ), F'Gradients in sync when they should not be at iteration {iteration}:\nModel grad ({param.grad}) == DDP grad ({ddp_param.grad})' # Shuffle ddp_input on each iteration torch.manual_seed(1337 + iteration ) _a = ddp_input[torch.randperm(len(lowercase ) )] GradientState._reset_state() def _lowerCamelCase ( lowercase : int=False , lowercase : int=False ) -> Dict: _a = Accelerator( split_batches=lowercase , dispatch_batches=lowercase , gradient_accumulation_steps=2 ) # Test that context manager behaves properly _a , _a , _a , _a , _a , _a , _a = get_training_setup(lowercase , lowercase ) for iteration, batch in enumerate(lowercase ): _a , _a = batch.values() # Gather the distributed inputs and targs for the base model _a , _a = accelerator.gather((ddp_input, ddp_target) ) _a , _a = input.to(accelerator.device ), target.to(accelerator.device ) # Perform our initial ground truth step in non "DDP" model.train() ddp_model.train() step_model(lowercase , lowercase , lowercase , lowercase , lowercase ) opt.step() if ((iteration + 1) % 2 == 0) or ((iteration + 1) == len(lowercase )): if split_batches: sched.step() else: for _ in range(accelerator.num_processes ): sched.step() opt.zero_grad() # Perform gradient accumulation under wrapper with accelerator.accumulate(lowercase ): step_model(lowercase , lowercase , lowercase , lowercase ) ddp_opt.step() ddp_sched.step() ddp_opt.zero_grad() # Learning rates should be the same assert ( opt.param_groups[0]["lr"] == ddp_opt.param_groups[0]["lr"] ), F'Learning rates found in each optimizer did not align\nopt: {opt.param_groups[0]["lr"]}\nDDP opt: {ddp_opt.param_groups[0]["lr"]}\n' _a = (((iteration + 1) % 2) == 0) or ((iteration + 1) == len(lowercase )) if accelerator.num_processes > 1: check_model_parameters(lowercase , lowercase , lowercase , lowercase ) # Shuffle ddp_input on each iteration torch.manual_seed(1337 + iteration ) GradientState._reset_state() def _lowerCamelCase ( ) -> Any: _a = Accelerator() _a = RegressionDataset(length=80 ) _a = DataLoader(lowercase , batch_size=16 ) _a = RegressionDataset(length=96 ) _a = DataLoader(lowercase , batch_size=16 ) _a , _a = accelerator.prepare(lowercase , lowercase ) assert accelerator.gradient_state.active_dataloader is None for iteration, _ in enumerate(lowercase ): assert id(accelerator.gradient_state.active_dataloader ) == id(lowercase ) if iteration < len(lowercase ) - 1: assert not accelerator.gradient_state.end_of_dataloader if iteration == 1: for batch_num, _ in enumerate(lowercase ): assert id(accelerator.gradient_state.active_dataloader ) == id(lowercase ) if batch_num < len(lowercase ) - 1: assert not accelerator.gradient_state.end_of_dataloader else: assert accelerator.gradient_state.end_of_dataloader else: assert accelerator.gradient_state.end_of_dataloader assert accelerator.gradient_state.active_dataloader is None def _lowerCamelCase ( ) -> Optional[Any]: _a = Accelerator() _a = accelerator.state if state.local_process_index == 0: print("**Test `accumulate` gradient accumulation with dataloader break**" ) test_dataloader_break() if state.distributed_type == DistributedType.NO: if state.local_process_index == 0: print("**Test NOOP `no_sync` context manager**" ) test_noop_sync(lowercase ) if state.distributed_type in (DistributedType.MULTI_GPU, DistributedType.MULTI_CPU): if state.local_process_index == 0: print("**Test Distributed `no_sync` context manager**" ) test_distributed_sync(lowercase ) if state.distributed_type == DistributedType.MULTI_GPU: for split_batch in [True, False]: for dispatch_batches in [True, False]: if state.local_process_index == 0: print( "**Test `accumulate` gradient accumulation, " , F'`split_batches={split_batch}` and `dispatch_batches={dispatch_batches}`**' , ) test_gradient_accumulation(lowercase , lowercase ) # Currently will break on torch 2.0 +, need to investigate why if is_torch_version("<" , "2.0" ) or state.distributed_type == DistributedType.NO: if state.local_process_index == 0: print( "**Test `accumulate` gradient accumulation with optimizer and scheduler, " , "`split_batches=False`, `dispatch_batches=False`**" , ) test_gradient_accumulation_with_opt_and_scheduler() if state.distributed_type == DistributedType.MULTI_GPU: for split_batch in [True, False]: for dispatch_batches in [True, False]: if not split_batch and not dispatch_batches: continue if state.local_process_index == 0: print( "**Test `accumulate` gradient accumulation with optimizer and scheduler, " , F'`split_batches={split_batch}` and `dispatch_batches={dispatch_batches}`**' , ) test_gradient_accumulation_with_opt_and_scheduler(lowercase , lowercase ) def _lowerCamelCase ( lowercase : Any ) -> Tuple: # For xla_spawn (TPUs) main() if __name__ == "__main__": main()
63
0
'''simple docstring''' import warnings from ...utils import logging from .image_processing_mobilevit import MobileViTImageProcessor UpperCamelCase_ = logging.get_logger(__name__) class _a ( SCREAMING_SNAKE_CASE ): '''simple docstring''' def __init__( self, *A, **A ): '''simple docstring''' warnings.warn( 'The class MobileViTFeatureExtractor is deprecated and will be removed in version 5 of Transformers.' ' Please use MobileViTImageProcessor instead.', A, ) super().__init__(*A, **A )
246
'''simple docstring''' from ...configuration_utils import PretrainedConfig from ...utils import logging UpperCamelCase_ = logging.get_logger(__name__) UpperCamelCase_ = { "uclanlp/visualbert-vqa": "https://huggingface.co/uclanlp/visualbert-vqa/resolve/main/config.json", "uclanlp/visualbert-vqa-pre": "https://huggingface.co/uclanlp/visualbert-vqa-pre/resolve/main/config.json", "uclanlp/visualbert-vqa-coco-pre": ( "https://huggingface.co/uclanlp/visualbert-vqa-coco-pre/resolve/main/config.json" ), "uclanlp/visualbert-vcr": "https://huggingface.co/uclanlp/visualbert-vcr/resolve/main/config.json", "uclanlp/visualbert-vcr-pre": "https://huggingface.co/uclanlp/visualbert-vcr-pre/resolve/main/config.json", "uclanlp/visualbert-vcr-coco-pre": ( "https://huggingface.co/uclanlp/visualbert-vcr-coco-pre/resolve/main/config.json" ), "uclanlp/visualbert-nlvr2": "https://huggingface.co/uclanlp/visualbert-nlvr2/resolve/main/config.json", "uclanlp/visualbert-nlvr2-pre": "https://huggingface.co/uclanlp/visualbert-nlvr2-pre/resolve/main/config.json", "uclanlp/visualbert-nlvr2-coco-pre": ( "https://huggingface.co/uclanlp/visualbert-nlvr2-coco-pre/resolve/main/config.json" ) # See all VisualBERT models at https://huggingface.co/models?filter=visual_bert } class _a ( SCREAMING_SNAKE_CASE ): '''simple docstring''' A : Optional[int] = '''visual_bert''' def __init__( self, A=30_522, A=768, A=512, A=12, A=12, A=3_072, A="gelu", A=0.1, A=0.1, A=512, A=2, A=0.02, A=1E-12, A=False, A=True, A=1, A=0, A=2, **A, ): '''simple docstring''' super().__init__(pad_token_id=A, bos_token_id=A, eos_token_id=A, **A ) SCREAMING_SNAKE_CASE : Dict = vocab_size SCREAMING_SNAKE_CASE : Optional[Any] = max_position_embeddings SCREAMING_SNAKE_CASE : str = hidden_size SCREAMING_SNAKE_CASE : int = visual_embedding_dim SCREAMING_SNAKE_CASE : List[Any] = num_hidden_layers SCREAMING_SNAKE_CASE : Optional[int] = num_attention_heads SCREAMING_SNAKE_CASE : List[str] = intermediate_size SCREAMING_SNAKE_CASE : Tuple = hidden_act SCREAMING_SNAKE_CASE : Union[str, Any] = hidden_dropout_prob SCREAMING_SNAKE_CASE : List[str] = attention_probs_dropout_prob SCREAMING_SNAKE_CASE : Optional[int] = initializer_range SCREAMING_SNAKE_CASE : str = type_vocab_size SCREAMING_SNAKE_CASE : List[Any] = layer_norm_eps SCREAMING_SNAKE_CASE : str = bypass_transformer SCREAMING_SNAKE_CASE : Any = special_visual_initialize
246
1
a__ = { "km/h": 1.0, "m/s": 3.6, "mph": 1.609_344, "knot": 1.852, } a__ = { "km/h": 1.0, "m/s": 0.277_777_778, "mph": 0.621_371_192, "knot": 0.539_956_803, } def lowercase ( SCREAMING_SNAKE_CASE__ : float , SCREAMING_SNAKE_CASE__ : str , SCREAMING_SNAKE_CASE__ : str ) -> float: if unit_to not in speed_chart or unit_from not in speed_chart_inverse: _snake_case : List[Any] = ( F'''Incorrect \'from_type\' or \'to_type\' value: {unit_from!r}, {unit_to!r}\n''' F'''Valid values are: {', '.join(SCREAMING_SNAKE_CASE__ )}''' ) raise ValueError(SCREAMING_SNAKE_CASE__ ) return round(speed * speed_chart[unit_from] * speed_chart_inverse[unit_to] , 3 ) if __name__ == "__main__": import doctest doctest.testmod()
317
import unittest import numpy as np from transformers.testing_utils import require_torch, require_vision from transformers.utils import is_torch_available, is_vision_available from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs if is_torch_available(): import torch if is_vision_available(): from PIL import Image from transformers import MobileNetVaImageProcessor class snake_case ( unittest.TestCase ): '''simple docstring''' def __init__( self : Tuple , lowerCAmelCase : Tuple , lowerCAmelCase : Tuple=7 , lowerCAmelCase : List[Any]=3 , lowerCAmelCase : Optional[Any]=18 , lowerCAmelCase : Dict=30 , lowerCAmelCase : Optional[int]=400 , lowerCAmelCase : List[str]=True , lowerCAmelCase : int=None , lowerCAmelCase : Tuple=True , lowerCAmelCase : Dict=None , ) -> Union[str, Any]: """simple docstring""" _snake_case : Optional[Any] = size if size is not None else {"""shortest_edge""": 20} _snake_case : Any = crop_size if crop_size is not None else {"""height""": 18, """width""": 18} _snake_case : Optional[Any] = parent _snake_case : Tuple = batch_size _snake_case : int = num_channels _snake_case : List[Any] = image_size _snake_case : Dict = min_resolution _snake_case : List[Any] = max_resolution _snake_case : List[Any] = do_resize _snake_case : Any = size _snake_case : str = do_center_crop _snake_case : Union[str, Any] = crop_size def UpperCamelCase_ ( self : int) -> str: """simple docstring""" return { "do_resize": self.do_resize, "size": self.size, "do_center_crop": self.do_center_crop, "crop_size": self.crop_size, } @require_torch @require_vision class snake_case ( SCREAMING_SNAKE_CASE_ ,unittest.TestCase ): '''simple docstring''' snake_case_ : Tuple = MobileNetVaImageProcessor if is_vision_available() else None def UpperCamelCase_ ( self : Any) -> Optional[Any]: """simple docstring""" _snake_case : str = MobileNetVaImageProcessingTester(self) @property def UpperCamelCase_ ( self : int) -> Optional[int]: """simple docstring""" return self.image_processor_tester.prepare_image_processor_dict() def UpperCamelCase_ ( self : List[Any]) -> str: """simple docstring""" _snake_case : int = self.image_processing_class(**self.image_processor_dict) self.assertTrue(hasattr(lowerCAmelCase , """do_resize""")) self.assertTrue(hasattr(lowerCAmelCase , """size""")) self.assertTrue(hasattr(lowerCAmelCase , """do_center_crop""")) self.assertTrue(hasattr(lowerCAmelCase , """crop_size""")) def UpperCamelCase_ ( self : List[str]) -> List[Any]: """simple docstring""" _snake_case : List[Any] = self.image_processing_class.from_dict(self.image_processor_dict) self.assertEqual(image_processor.size , {"""shortest_edge""": 20}) self.assertEqual(image_processor.crop_size , {"""height""": 18, """width""": 18}) _snake_case : Tuple = self.image_processing_class.from_dict(self.image_processor_dict , size=42 , crop_size=84) self.assertEqual(image_processor.size , {"""shortest_edge""": 42}) self.assertEqual(image_processor.crop_size , {"""height""": 84, """width""": 84}) def UpperCamelCase_ ( self : List[str]) -> Optional[Any]: """simple docstring""" pass def UpperCamelCase_ ( self : Dict) -> str: """simple docstring""" _snake_case : Dict = self.image_processing_class(**self.image_processor_dict) # create random PIL images _snake_case : Any = prepare_image_inputs(self.image_processor_tester , equal_resolution=lowerCAmelCase) for image in image_inputs: self.assertIsInstance(lowerCAmelCase , Image.Image) # Test not batched input _snake_case : 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.crop_size["""height"""], self.image_processor_tester.crop_size["""width"""], ) , ) # Test batched _snake_case : Dict = image_processing(lowerCAmelCase , return_tensors="""pt""").pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size["""height"""], self.image_processor_tester.crop_size["""width"""], ) , ) def UpperCamelCase_ ( self : int) -> List[Any]: """simple docstring""" _snake_case : int = self.image_processing_class(**self.image_processor_dict) # create random numpy tensors _snake_case : List[Any] = prepare_image_inputs(self.image_processor_tester , equal_resolution=lowerCAmelCase , numpify=lowerCAmelCase) for image in image_inputs: self.assertIsInstance(lowerCAmelCase , np.ndarray) # Test not batched input _snake_case : 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.crop_size["""height"""], self.image_processor_tester.crop_size["""width"""], ) , ) # Test batched _snake_case : str = image_processing(lowerCAmelCase , return_tensors="""pt""").pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size["""height"""], self.image_processor_tester.crop_size["""width"""], ) , ) def UpperCamelCase_ ( self : str) -> List[str]: """simple docstring""" _snake_case : Union[str, Any] = self.image_processing_class(**self.image_processor_dict) # create random PyTorch tensors _snake_case : Any = prepare_image_inputs(self.image_processor_tester , equal_resolution=lowerCAmelCase , torchify=lowerCAmelCase) for image in image_inputs: self.assertIsInstance(lowerCAmelCase , torch.Tensor) # Test not batched input _snake_case : List[str] = image_processing(image_inputs[0] , return_tensors="""pt""").pixel_values self.assertEqual( encoded_images.shape , ( 1, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size["""height"""], self.image_processor_tester.crop_size["""width"""], ) , ) # Test batched _snake_case : int = image_processing(lowerCAmelCase , return_tensors="""pt""").pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size["""height"""], self.image_processor_tester.crop_size["""width"""], ) , )
317
1
import operator def lowerCamelCase__ ( a__ : list , a__ : bool = False , a__ : list | None = None ) -> list: UpperCamelCase_ = operator.lt if reverse else operator.gt UpperCamelCase_ = solution or [] if not arr: return solution UpperCamelCase_ = [arr.pop(0 )] for i, item in enumerate(a__ ): if _operator(a__ , sublist[-1] ): sublist.append(a__ ) arr.pop(a__ ) # merging sublist into solution list if not solution: solution.extend(a__ ) else: while sublist: UpperCamelCase_ = sublist.pop(0 ) for i, xx in enumerate(a__ ): if not _operator(a__ , a__ ): solution.insert(a__ , a__ ) break else: solution.append(a__ ) strand_sort(a__ , a__ , a__ ) return solution if __name__ == "__main__": assert strand_sort([4, 3, 5, 1, 2]) == [1, 2, 3, 4, 5] assert strand_sort([4, 3, 5, 1, 2], reverse=True) == [5, 4, 3, 2, 1]
261
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__ : Dataset , a__ : Dict[str, str] ) -> int: 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__ : List[str] , a__ : Any ): 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__ : str ) -> str: 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__ : Optional[int] ) -> Union[str, Any]: # 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__ : Optional[Any] ): 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__": _A = 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.''', ) _A = parser.parse_args() main(args)
261
1
import argparse import os import re import packaging.version _UpperCAmelCase : Optional[int] = """examples/""" _UpperCAmelCase : List[str] = { """examples""": (re.compile(R"""^check_min_version\(\"[^\"]+\"\)\s*$""", re.MULTILINE), """check_min_version(\"VERSION\")\n"""), """init""": (re.compile(R"""^__version__\s+=\s+\"([^\"]+)\"\s*$""", re.MULTILINE), """__version__ = \"VERSION\"\n"""), """setup""": (re.compile(R"""^(\s*)version\s*=\s*\"[^\"]+\",""", re.MULTILINE), R"""\1version=\"VERSION\","""), """doc""": (re.compile(R"""^(\s*)release\s*=\s*\"[^\"]+\"$""", re.MULTILINE), """release = \"VERSION\"\n"""), } _UpperCAmelCase : int = { """init""": """src/transformers/__init__.py""", """setup""": """setup.py""", } _UpperCAmelCase : List[str] = """README.md""" def SCREAMING_SNAKE_CASE ( _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase ) -> Dict: with open(_UpperCAmelCase , 'r' , encoding='utf-8' , newline='\n' ) as f: lowerCamelCase__ : Optional[Any] = f.read() lowerCamelCase__ , lowerCamelCase__ : str = REPLACE_PATTERNS[pattern] lowerCamelCase__ : str = replace.replace('VERSION' , _UpperCAmelCase ) lowerCamelCase__ : List[Any] = re_pattern.sub(_UpperCAmelCase , _UpperCAmelCase ) with open(_UpperCAmelCase , 'w' , encoding='utf-8' , newline='\n' ) as f: f.write(_UpperCAmelCase ) def SCREAMING_SNAKE_CASE ( _UpperCAmelCase ) -> List[str]: for folder, directories, fnames in os.walk(_UpperCAmelCase ): # Removing some of the folders with non-actively maintained examples from the walk if "research_projects" in directories: directories.remove('research_projects' ) if "legacy" in directories: directories.remove('legacy' ) for fname in fnames: if fname.endswith('.py' ): update_version_in_file(os.path.join(_UpperCAmelCase , _UpperCAmelCase ) , _UpperCAmelCase , pattern='examples' ) def SCREAMING_SNAKE_CASE ( _UpperCAmelCase , _UpperCAmelCase=False ) -> Optional[Any]: for pattern, fname in REPLACE_FILES.items(): update_version_in_file(_UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase ) if not patch: update_version_in_examples(_UpperCAmelCase ) def SCREAMING_SNAKE_CASE ( ) -> Optional[Any]: lowerCamelCase__ : List[str] = '🤗 Transformers currently provides the following architectures' lowerCamelCase__ : str = '1. Want to contribute a new model?' with open(_UpperCAmelCase , 'r' , encoding='utf-8' , newline='\n' ) as f: lowerCamelCase__ : str = f.readlines() # Find the start of the list. lowerCamelCase__ : Any = 0 while not lines[start_index].startswith(_start_prompt ): start_index += 1 start_index += 1 lowerCamelCase__ : Optional[Any] = start_index # Update the lines in the model list. while not lines[index].startswith(_end_prompt ): if lines[index].startswith('1.' ): lowerCamelCase__ : str = lines[index].replace( 'https://huggingface.co/docs/transformers/main/model_doc' , 'https://huggingface.co/docs/transformers/model_doc' , ) index += 1 with open(_UpperCAmelCase , 'w' , encoding='utf-8' , newline='\n' ) as f: f.writelines(_UpperCAmelCase ) def SCREAMING_SNAKE_CASE ( ) -> Optional[int]: with open(REPLACE_FILES['init'] , 'r' ) as f: lowerCamelCase__ : Any = f.read() lowerCamelCase__ : Union[str, Any] = REPLACE_PATTERNS['init'][0].search(_UpperCAmelCase ).groups()[0] return packaging.version.parse(_UpperCAmelCase ) def SCREAMING_SNAKE_CASE ( _UpperCAmelCase=False ) -> Optional[Any]: lowerCamelCase__ : Tuple = get_version() if patch and default_version.is_devrelease: raise ValueError('Can\'t create a patch version from the dev branch, checkout a released version!' ) if default_version.is_devrelease: lowerCamelCase__ : List[str] = default_version.base_version elif patch: lowerCamelCase__ : Union[str, Any] = F"""{default_version.major}.{default_version.minor}.{default_version.micro + 1}""" else: lowerCamelCase__ : Union[str, Any] = F"""{default_version.major}.{default_version.minor + 1}.0""" # Now let's ask nicely if that's the right one. lowerCamelCase__ : Optional[int] = input(F"""Which version are you releasing? [{default_version}]""" ) if len(_UpperCAmelCase ) == 0: lowerCamelCase__ : str = default_version print(F"""Updating version to {version}.""" ) global_version_update(_UpperCAmelCase , patch=_UpperCAmelCase ) if not patch: print('Cleaning main README, don\'t forget to run `make fix-copies`.' ) clean_main_ref_in_model_list() def SCREAMING_SNAKE_CASE ( ) -> Tuple: lowerCamelCase__ : Tuple = get_version() lowerCamelCase__ : List[Any] = F"""{current_version.major}.{current_version.minor + 1}.0.dev0""" lowerCamelCase__ : Union[str, Any] = current_version.base_version # Check with the user we got that right. lowerCamelCase__ : Any = input(F"""Which version are we developing now? [{dev_version}]""" ) if len(_UpperCAmelCase ) == 0: lowerCamelCase__ : Dict = dev_version print(F"""Updating version to {version}.""" ) global_version_update(_UpperCAmelCase ) print('Cleaning main README, don\'t forget to run `make fix-copies`.' ) clean_main_ref_in_model_list() if __name__ == "__main__": _UpperCAmelCase : Dict = argparse.ArgumentParser() parser.add_argument("""--post_release""", action="""store_true""", help="""Whether this is pre or post release.""") parser.add_argument("""--patch""", action="""store_true""", help="""Whether or not this is a patch release.""") _UpperCAmelCase : int = parser.parse_args() if not args.post_release: pre_release_work(patch=args.patch) elif args.patch: print("""Nothing to do after a patch :-)""") else: post_release_work()
50
"""simple docstring""" import inspect from typing import List, Optional, Tuple, Union import torch from ...models import UNetaDModel, VQModel from ...schedulers import DDIMScheduler from ...utils import randn_tensor from ..pipeline_utils import DiffusionPipeline, ImagePipelineOutput class UpperCAmelCase_ ( _lowercase): def __init__( self : List[Any] , __UpperCamelCase : VQModel , __UpperCamelCase : UNetaDModel , __UpperCamelCase : DDIMScheduler ) -> Optional[Any]: super().__init__() self.register_modules(vqvae=__UpperCamelCase , unet=__UpperCamelCase , scheduler=__UpperCamelCase ) @torch.no_grad() def __call__( self : List[Any] , __UpperCamelCase : int = 1 , __UpperCamelCase : Optional[Union[torch.Generator, List[torch.Generator]]] = None , __UpperCamelCase : float = 0.0 , __UpperCamelCase : int = 50 , __UpperCamelCase : Optional[str] = "pil" , __UpperCamelCase : bool = True , **__UpperCamelCase : Optional[int] , ) -> Union[Tuple, ImagePipelineOutput]: _UpperCamelCase = randn_tensor( (batch_size, self.unet.config.in_channels, self.unet.config.sample_size, self.unet.config.sample_size) , generator=__UpperCamelCase , ) _UpperCamelCase = latents.to(self.device ) # scale the initial noise by the standard deviation required by the scheduler _UpperCamelCase = latents * self.scheduler.init_noise_sigma self.scheduler.set_timesteps(__UpperCamelCase ) # prepare extra kwargs for the scheduler step, since not all schedulers have the same signature _UpperCamelCase = '''eta''' in set(inspect.signature(self.scheduler.step ).parameters.keys() ) _UpperCamelCase = {} if accepts_eta: _UpperCamelCase = eta for t in self.progress_bar(self.scheduler.timesteps ): _UpperCamelCase = self.scheduler.scale_model_input(__UpperCamelCase , __UpperCamelCase ) # predict the noise residual _UpperCamelCase = self.unet(__UpperCamelCase , __UpperCamelCase ).sample # compute the previous noisy sample x_t -> x_t-1 _UpperCamelCase = self.scheduler.step(__UpperCamelCase , __UpperCamelCase , __UpperCamelCase , **__UpperCamelCase ).prev_sample # decode the image latents with the VAE _UpperCamelCase = self.vqvae.decode(__UpperCamelCase ).sample _UpperCamelCase = (image / 2 + 0.5).clamp(0 , 1 ) _UpperCamelCase = image.cpu().permute(0 , 2 , 3 , 1 ).numpy() if output_type == "pil": _UpperCamelCase = self.numpy_to_pil(__UpperCamelCase ) if not return_dict: return (image,) return ImagePipelineOutput(images=__UpperCamelCase )
256
0
from __future__ import annotations def lowerCamelCase ( SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , ): '''simple docstring''' __UpperCamelCase :Optional[Any] = len(SCREAMING_SNAKE_CASE ) # If row is equal to the size of the board it means there are a queen in each row in # the current board (possible_board) if row == n: # We convert the variable possible_board that looks like this: [1, 3, 0, 2] to # this: ['. Q . . ', '. . . Q ', 'Q . . . ', '. . Q . '] boards.append(['''. ''' * i + '''Q ''' + '''. ''' * (n - 1 - i) for i in possible_board] ) return # We iterate each column in the row to find all possible results in each row for col in range(SCREAMING_SNAKE_CASE ): # We apply that we learned previously. First we check that in the current board # (possible_board) there are not other same value because if there is it means # that there are a collision in vertical. Then we apply the two formulas we # learned before: # # 45º: y - x = b or 45: row - col = b # 135º: y + x = b or row + col = b. # # And we verify if the results of this two formulas not exist in their variables # respectively. (diagonal_right_collisions, diagonal_left_collisions) # # If any or these are True it means there is a collision so we continue to the # next value in the for loop. if ( col in possible_board or row - col in diagonal_right_collisions or row + col in diagonal_left_collisions ): continue # If it is False we call dfs function again and we update the inputs depth_first_search( [*possible_board, col] , [*diagonal_right_collisions, row - col] , [*diagonal_left_collisions, row + col] , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , ) def lowerCamelCase ( SCREAMING_SNAKE_CASE ): '''simple docstring''' __UpperCamelCase :list[list[str]] = [] depth_first_search([] , [] , [] , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ) # Print all the boards for board in boards: for column in board: print(SCREAMING_SNAKE_CASE ) print('''''' ) print(len(SCREAMING_SNAKE_CASE ) , '''solutions were found.''' ) if __name__ == "__main__": import doctest doctest.testmod() n_queens_solution(4)
360
from collections import OrderedDict from typing import Any, List, Mapping, Optional from ... import PreTrainedTokenizer, TensorType, is_torch_available from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfigWithPast, PatchingSpec from ...utils import logging __lowercase = logging.get_logger(__name__) __lowercase = { '''EleutherAI/gpt-j-6B''': '''https://huggingface.co/EleutherAI/gpt-j-6B/resolve/main/config.json''', # See all GPT-J models at https://huggingface.co/models?filter=gpt_j } class lowerCamelCase_ ( UpperCAmelCase_ ): '''simple docstring''' a__ : Optional[Any] = """gptj""" a__ : Tuple = { """max_position_embeddings""": """n_positions""", """hidden_size""": """n_embd""", """num_attention_heads""": """n_head""", """num_hidden_layers""": """n_layer""", } def __init__( self , __lowercase=50_400 , __lowercase=2_048 , __lowercase=4_096 , __lowercase=28 , __lowercase=16 , __lowercase=64 , __lowercase=None , __lowercase="gelu_new" , __lowercase=0.0 , __lowercase=0.0 , __lowercase=0.0 , __lowercase=1E-5 , __lowercase=0.02 , __lowercase=True , __lowercase=50_256 , __lowercase=50_256 , __lowercase=False , **__lowercase , ) -> Tuple: __UpperCamelCase :Any = vocab_size __UpperCamelCase :Optional[int] = n_positions __UpperCamelCase :Tuple = n_embd __UpperCamelCase :int = n_layer __UpperCamelCase :Any = n_head __UpperCamelCase :Any = n_inner __UpperCamelCase :Dict = rotary_dim __UpperCamelCase :Tuple = activation_function __UpperCamelCase :Optional[Any] = resid_pdrop __UpperCamelCase :Any = embd_pdrop __UpperCamelCase :List[str] = attn_pdrop __UpperCamelCase :str = layer_norm_epsilon __UpperCamelCase :List[Any] = initializer_range __UpperCamelCase :Dict = use_cache __UpperCamelCase :List[Any] = bos_token_id __UpperCamelCase :Tuple = eos_token_id super().__init__( bos_token_id=__lowercase , eos_token_id=__lowercase , tie_word_embeddings=__lowercase , **__lowercase) class lowerCamelCase_ ( UpperCAmelCase_ ): '''simple docstring''' def __init__( self , __lowercase , __lowercase = "default" , __lowercase = None , __lowercase = False , ) -> Any: super().__init__(__lowercase , task=__lowercase , patching_specs=__lowercase , use_past=__lowercase) if not getattr(self._config , '''pad_token_id''' , __lowercase): # TODO: how to do that better? __UpperCamelCase :Tuple = 0 @property def UpperCamelCase__ ( self) -> Mapping[str, Mapping[int, str]]: __UpperCamelCase :Tuple = OrderedDict({'''input_ids''': {0: '''batch''', 1: '''sequence'''}}) if self.use_past: self.fill_with_past_key_values_(__lowercase , direction='''inputs''') __UpperCamelCase :str = {0: '''batch''', 1: '''past_sequence + sequence'''} else: __UpperCamelCase :Any = {0: '''batch''', 1: '''sequence'''} return common_inputs @property def UpperCamelCase__ ( self) -> int: return self._config.n_layer @property def UpperCamelCase__ ( self) -> int: return self._config.n_head def UpperCamelCase__ ( self , __lowercase , __lowercase = -1 , __lowercase = -1 , __lowercase = False , __lowercase = None , ) -> Mapping[str, Any]: __UpperCamelCase :Optional[int] = super(__lowercase , self).generate_dummy_inputs( __lowercase , batch_size=__lowercase , seq_length=__lowercase , is_pair=__lowercase , framework=__lowercase) # We need to order the input in the way they appears in the forward() __UpperCamelCase :int = OrderedDict({'''input_ids''': common_inputs['''input_ids''']}) # Need to add the past_keys if self.use_past: if not is_torch_available(): raise ValueError('''Cannot generate dummy past_keys inputs without PyTorch installed.''') else: import torch __UpperCamelCase , __UpperCamelCase :str = common_inputs['''input_ids'''].shape # Not using the same length for past_key_values __UpperCamelCase :List[str] = seqlen + 2 __UpperCamelCase :Union[str, Any] = ( batch, self.num_attention_heads, past_key_values_length, self._config.hidden_size // self.num_attention_heads, ) __UpperCamelCase :Tuple = [ (torch.zeros(__lowercase), torch.zeros(__lowercase)) for _ in range(self.num_layers) ] __UpperCamelCase :Tuple = common_inputs['''attention_mask'''] if self.use_past: __UpperCamelCase :Tuple = ordered_inputs['''attention_mask'''].dtype __UpperCamelCase :Optional[Any] = torch.cat( [ordered_inputs['''attention_mask'''], torch.ones(__lowercase , __lowercase , dtype=__lowercase)] , dim=1) return ordered_inputs @property def UpperCamelCase__ ( self) -> int: return 13
105
0
'''simple docstring''' from pickle import UnpicklingError import jax import jax.numpy as jnp import numpy as np from flax.serialization import from_bytes from flax.traverse_util import flatten_dict from ..utils import logging __a = logging.get_logger(__name__) def __snake_case( _lowerCAmelCase , _lowerCAmelCase ) -> Optional[int]: try: with open(_lowerCAmelCase , """rb""" ) as flax_state_f: snake_case__ : Any = from_bytes(_lowerCAmelCase , flax_state_f.read() ) except UnpicklingError as e: try: with open(_lowerCAmelCase ) as f: if f.read().startswith("""version""" ): raise OSError( """You seem to have cloned a repository without having git-lfs installed. Please""" """ install git-lfs and run `git lfs install` followed by `git lfs pull` in the""" """ folder you cloned.""" ) else: raise ValueError from e except (UnicodeDecodeError, ValueError): raise EnvironmentError(f"Unable to convert {model_file} to Flax deserializable object. " ) return load_flax_weights_in_pytorch_model(_lowerCAmelCase , _lowerCAmelCase ) def __snake_case( _lowerCAmelCase , _lowerCAmelCase ) -> int: try: import torch # noqa: F401 except ImportError: logger.error( """Loading Flax weights in PyTorch requires both PyTorch and Flax to be installed. Please see""" """ https://pytorch.org/ and https://flax.readthedocs.io/en/latest/installation.html for installation""" """ instructions.""" ) raise # check if we have bf16 weights snake_case__ : Optional[int] = flatten_dict(jax.tree_util.tree_map(lambda _lowerCAmelCase : x.dtype == jnp.bfloataa , _lowerCAmelCase ) ).values() if any(_lowerCAmelCase ): # convert all weights to fp32 if they are bf16 since torch.from_numpy can-not handle bf16 # and bf16 is not fully supported in PT yet. logger.warning( """Found ``bfloat16`` weights in Flax model. Casting all ``bfloat16`` weights to ``float32`` """ """before loading those in PyTorch model.""" ) snake_case__ : Optional[Any] = jax.tree_util.tree_map( lambda _lowerCAmelCase : params.astype(np.floataa ) if params.dtype == jnp.bfloataa else params , _lowerCAmelCase ) snake_case__ : Optional[int] = """""" snake_case__ : Any = flatten_dict(_lowerCAmelCase , sep=""".""" ) snake_case__ : Union[str, Any] = pt_model.state_dict() # keep track of unexpected & missing keys snake_case__ : Any = [] snake_case__ : List[Any] = set(pt_model_dict.keys() ) for flax_key_tuple, flax_tensor in flax_state_dict.items(): snake_case__ : str = flax_key_tuple.split(""".""" ) if flax_key_tuple_array[-1] == "kernel" and flax_tensor.ndim == 4: snake_case__ : Dict = flax_key_tuple_array[:-1] + ["""weight"""] snake_case__ : List[Any] = jnp.transpose(_lowerCAmelCase , (3, 2, 0, 1) ) elif flax_key_tuple_array[-1] == "kernel": snake_case__ : str = flax_key_tuple_array[:-1] + ["""weight"""] snake_case__ : Dict = flax_tensor.T elif flax_key_tuple_array[-1] == "scale": snake_case__ : Dict = flax_key_tuple_array[:-1] + ["""weight"""] if "time_embedding" not in flax_key_tuple_array: for i, flax_key_tuple_string in enumerate(_lowerCAmelCase ): snake_case__ : int = ( flax_key_tuple_string.replace("""_0""" , """.0""" ) .replace("""_1""" , """.1""" ) .replace("""_2""" , """.2""" ) .replace("""_3""" , """.3""" ) .replace("""_4""" , """.4""" ) .replace("""_5""" , """.5""" ) .replace("""_6""" , """.6""" ) .replace("""_7""" , """.7""" ) .replace("""_8""" , """.8""" ) .replace("""_9""" , """.9""" ) ) snake_case__ : List[Any] = """.""".join(_lowerCAmelCase ) if flax_key in pt_model_dict: if flax_tensor.shape != pt_model_dict[flax_key].shape: raise ValueError( f"Flax checkpoint seems to be incorrect. Weight {flax_key_tuple} was expected " f"to be of shape {pt_model_dict[flax_key].shape}, but is {flax_tensor.shape}." ) else: # add weight to pytorch dict snake_case__ : Tuple = np.asarray(_lowerCAmelCase ) if not isinstance(_lowerCAmelCase , np.ndarray ) else flax_tensor snake_case__ : Optional[int] = torch.from_numpy(_lowerCAmelCase ) # remove from missing keys missing_keys.remove(_lowerCAmelCase ) else: # weight is not expected by PyTorch model unexpected_keys.append(_lowerCAmelCase ) pt_model.load_state_dict(_lowerCAmelCase ) # re-transform missing_keys to list snake_case__ : Tuple = list(_lowerCAmelCase ) if len(_lowerCAmelCase ) > 0: logger.warning( """Some weights of the Flax model were not used when initializing the PyTorch model""" f" {pt_model.__class__.__name__}: {unexpected_keys}\n- This IS expected if you are initializing" f" {pt_model.__class__.__name__} from a Flax model trained on another task or with another architecture" """ (e.g. initializing a BertForSequenceClassification model from a FlaxBertForPreTraining model).\n- This""" f" IS NOT expected if you are initializing {pt_model.__class__.__name__} from a Flax model that you expect" """ to be exactly identical (e.g. initializing a BertForSequenceClassification model from a""" """ FlaxBertForSequenceClassification model).""" ) if len(_lowerCAmelCase ) > 0: logger.warning( f"Some weights of {pt_model.__class__.__name__} were not initialized from the Flax model and are newly" f" initialized: {missing_keys}\nYou should probably TRAIN this model on a down-stream task to be able to" """ use it for predictions and inference.""" ) return pt_model
35
'''simple docstring''' import os from datetime import datetime as dt from github import Github snake_case_ : Any = [ 'good first issue', 'good second issue', 'good difficult issue', 'enhancement', 'new pipeline/model', 'new scheduler', 'wip', ] def A__ ( ): _UpperCamelCase : Tuple = Github(os.environ['GITHUB_TOKEN'] ) _UpperCamelCase : List[Any] = g.get_repo('huggingface/diffusers' ) _UpperCamelCase : List[Any] = repo.get_issues(state='open' ) for issue in open_issues: _UpperCamelCase : Dict = sorted(issue.get_comments() , key=lambda UpperCAmelCase_ : i.created_at , reverse=UpperCAmelCase_ ) _UpperCamelCase : List[str] = comments[0] if len(UpperCAmelCase_ ) > 0 else None if ( last_comment is not None and last_comment.user.login == "github-actions[bot]" and (dt.utcnow() - issue.updated_at).days > 7 and (dt.utcnow() - issue.created_at).days >= 3_0 and not any(label.name.lower() in LABELS_TO_EXEMPT for label in issue.get_labels() ) ): # Closes the issue after 7 days of inactivity since the Stalebot notification. issue.edit(state='closed' ) elif ( "stale" in issue.get_labels() and last_comment is not None and last_comment.user.login != "github-actions[bot]" ): # Opens the issue if someone other than Stalebot commented. issue.edit(state='open' ) issue.remove_from_labels('stale' ) elif ( (dt.utcnow() - issue.updated_at).days > 2_3 and (dt.utcnow() - issue.created_at).days >= 3_0 and not any(label.name.lower() in LABELS_TO_EXEMPT for label in issue.get_labels() ) ): # Post a Stalebot notification after 23 days of inactivity. issue.create_comment( 'This issue has been automatically marked as stale because it has not had ' 'recent activity. If you think this still needs to be addressed ' 'please comment on this thread.\n\nPlease note that issues that do not follow the ' '[contributing guidelines](https://github.com/huggingface/diffusers/blob/main/CONTRIBUTING.md) ' 'are likely to be ignored.' ) issue.add_to_labels('stale' ) if __name__ == "__main__": main()
83
0
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tf_available, is_torch_available __lowerCAmelCase = {'''configuration_swin''': ['''SWIN_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''SwinConfig''', '''SwinOnnxConfig''']} try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __lowerCAmelCase = [ '''SWIN_PRETRAINED_MODEL_ARCHIVE_LIST''', '''SwinForImageClassification''', '''SwinForMaskedImageModeling''', '''SwinModel''', '''SwinPreTrainedModel''', '''SwinBackbone''', ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __lowerCAmelCase = [ '''TF_SWIN_PRETRAINED_MODEL_ARCHIVE_LIST''', '''TFSwinForImageClassification''', '''TFSwinForMaskedImageModeling''', '''TFSwinModel''', '''TFSwinPreTrainedModel''', ] if TYPE_CHECKING: from .configuration_swin import SWIN_PRETRAINED_CONFIG_ARCHIVE_MAP, SwinConfig, SwinOnnxConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_swin import ( SWIN_PRETRAINED_MODEL_ARCHIVE_LIST, SwinBackbone, SwinForImageClassification, SwinForMaskedImageModeling, SwinModel, SwinPreTrainedModel, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_swin import ( TF_SWIN_PRETRAINED_MODEL_ARCHIVE_LIST, TFSwinForImageClassification, TFSwinForMaskedImageModeling, TFSwinModel, TFSwinPreTrainedModel, ) else: import sys __lowerCAmelCase = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
365
import argparse import os import jax as jnp import numpy as onp import torch import torch.nn as nn from music_spectrogram_diffusion import inference from tax import checkpoints from diffusers import DDPMScheduler, OnnxRuntimeModel, SpectrogramDiffusionPipeline from diffusers.pipelines.spectrogram_diffusion import SpectrogramContEncoder, SpectrogramNotesEncoder, TaFilmDecoder __lowerCAmelCase = '''base_with_context''' def snake_case_ ( snake_case , snake_case ) -> int: lowercase__: Tuple = nn.Parameter(torch.FloatTensor(weights['token_embedder']['embedding'] ) ) lowercase__: Optional[int] = nn.Parameter( torch.FloatTensor(weights['Embed_0']['embedding'] ) , requires_grad=snake_case ) for lyr_num, lyr in enumerate(model.encoders ): lowercase__: List[str] = weights[f'layers_{lyr_num}'] lowercase__: List[Any] = nn.Parameter( torch.FloatTensor(ly_weight['pre_attention_layer_norm']['scale'] ) ) lowercase__: Any = ly_weight['attention'] lowercase__: int = nn.Parameter(torch.FloatTensor(attention_weights['query']['kernel'].T ) ) lowercase__: int = nn.Parameter(torch.FloatTensor(attention_weights['key']['kernel'].T ) ) lowercase__: List[str] = nn.Parameter(torch.FloatTensor(attention_weights['value']['kernel'].T ) ) lowercase__: Dict = nn.Parameter(torch.FloatTensor(attention_weights['out']['kernel'].T ) ) lowercase__: List[Any] = nn.Parameter(torch.FloatTensor(ly_weight['pre_mlp_layer_norm']['scale'] ) ) lowercase__: Optional[Any] = nn.Parameter(torch.FloatTensor(ly_weight['mlp']['wi_0']['kernel'].T ) ) lowercase__: Dict = nn.Parameter(torch.FloatTensor(ly_weight['mlp']['wi_1']['kernel'].T ) ) lowercase__: List[str] = nn.Parameter(torch.FloatTensor(ly_weight['mlp']['wo']['kernel'].T ) ) lowercase__: Any = nn.Parameter(torch.FloatTensor(weights['encoder_norm']['scale'] ) ) return model def snake_case_ ( snake_case , snake_case ) -> List[str]: lowercase__: str = nn.Parameter(torch.FloatTensor(weights['input_proj']['kernel'].T ) ) lowercase__: Dict = nn.Parameter( torch.FloatTensor(weights['Embed_0']['embedding'] ) , requires_grad=snake_case ) for lyr_num, lyr in enumerate(model.encoders ): lowercase__: str = weights[f'layers_{lyr_num}'] lowercase__: Optional[Any] = ly_weight['attention'] lowercase__: List[Any] = nn.Parameter(torch.FloatTensor(attention_weights['query']['kernel'].T ) ) lowercase__: Dict = nn.Parameter(torch.FloatTensor(attention_weights['key']['kernel'].T ) ) lowercase__: int = nn.Parameter(torch.FloatTensor(attention_weights['value']['kernel'].T ) ) lowercase__: Dict = nn.Parameter(torch.FloatTensor(attention_weights['out']['kernel'].T ) ) lowercase__: Union[str, Any] = nn.Parameter( torch.FloatTensor(ly_weight['pre_attention_layer_norm']['scale'] ) ) lowercase__: List[str] = nn.Parameter(torch.FloatTensor(ly_weight['mlp']['wi_0']['kernel'].T ) ) lowercase__: Dict = nn.Parameter(torch.FloatTensor(ly_weight['mlp']['wi_1']['kernel'].T ) ) lowercase__: Tuple = nn.Parameter(torch.FloatTensor(ly_weight['mlp']['wo']['kernel'].T ) ) lowercase__: Optional[int] = nn.Parameter(torch.FloatTensor(ly_weight['pre_mlp_layer_norm']['scale'] ) ) lowercase__: List[str] = nn.Parameter(torch.FloatTensor(weights['encoder_norm']['scale'] ) ) return model def snake_case_ ( snake_case , snake_case ) -> Any: lowercase__: int = nn.Parameter(torch.FloatTensor(weights['time_emb_dense0']['kernel'].T ) ) lowercase__: Any = nn.Parameter(torch.FloatTensor(weights['time_emb_dense1']['kernel'].T ) ) lowercase__: int = nn.Parameter( torch.FloatTensor(weights['Embed_0']['embedding'] ) , requires_grad=snake_case ) lowercase__: Dict = nn.Parameter( torch.FloatTensor(weights['continuous_inputs_projection']['kernel'].T ) ) for lyr_num, lyr in enumerate(model.decoders ): lowercase__: Optional[Any] = weights[f'layers_{lyr_num}'] lowercase__: Any = nn.Parameter( torch.FloatTensor(ly_weight['pre_self_attention_layer_norm']['scale'] ) ) lowercase__: int = nn.Parameter( torch.FloatTensor(ly_weight['FiLMLayer_0']['DenseGeneral_0']['kernel'].T ) ) lowercase__: List[str] = ly_weight['self_attention'] lowercase__: Optional[int] = nn.Parameter(torch.FloatTensor(attention_weights['query']['kernel'].T ) ) lowercase__: Any = nn.Parameter(torch.FloatTensor(attention_weights['key']['kernel'].T ) ) lowercase__: Tuple = nn.Parameter(torch.FloatTensor(attention_weights['value']['kernel'].T ) ) lowercase__: Union[str, Any] = nn.Parameter(torch.FloatTensor(attention_weights['out']['kernel'].T ) ) lowercase__: int = ly_weight['MultiHeadDotProductAttention_0'] lowercase__: List[Any] = nn.Parameter(torch.FloatTensor(attention_weights['query']['kernel'].T ) ) lowercase__: Dict = nn.Parameter(torch.FloatTensor(attention_weights['key']['kernel'].T ) ) lowercase__: str = nn.Parameter(torch.FloatTensor(attention_weights['value']['kernel'].T ) ) lowercase__: Any = nn.Parameter(torch.FloatTensor(attention_weights['out']['kernel'].T ) ) lowercase__: int = nn.Parameter( torch.FloatTensor(ly_weight['pre_cross_attention_layer_norm']['scale'] ) ) lowercase__: List[str] = nn.Parameter(torch.FloatTensor(ly_weight['pre_mlp_layer_norm']['scale'] ) ) lowercase__: Union[str, Any] = nn.Parameter( torch.FloatTensor(ly_weight['FiLMLayer_1']['DenseGeneral_0']['kernel'].T ) ) lowercase__: List[str] = nn.Parameter(torch.FloatTensor(ly_weight['mlp']['wi_0']['kernel'].T ) ) lowercase__: int = nn.Parameter(torch.FloatTensor(ly_weight['mlp']['wi_1']['kernel'].T ) ) lowercase__: str = nn.Parameter(torch.FloatTensor(ly_weight['mlp']['wo']['kernel'].T ) ) lowercase__: Optional[Any] = nn.Parameter(torch.FloatTensor(weights['decoder_norm']['scale'] ) ) lowercase__: Union[str, Any] = nn.Parameter(torch.FloatTensor(weights['spec_out_dense']['kernel'].T ) ) return model def snake_case_ ( snake_case ) -> Any: lowercase__: int = checkpoints.load_tax_checkpoint(args.checkpoint_path ) lowercase__: Tuple = jnp.tree_util.tree_map(onp.array , snake_case ) lowercase__: List[str] = [ 'from __gin__ import dynamic_registration', 'from music_spectrogram_diffusion.models.diffusion import diffusion_utils', 'diffusion_utils.ClassifierFreeGuidanceConfig.eval_condition_weight = 2.0', 'diffusion_utils.DiffusionConfig.classifier_free_guidance = @diffusion_utils.ClassifierFreeGuidanceConfig()', ] lowercase__: List[Any] = os.path.join(args.checkpoint_path , '..' , 'config.gin' ) lowercase__: Optional[Any] = inference.parse_training_gin_file(snake_case , snake_case ) lowercase__: str = inference.InferenceModel(args.checkpoint_path , snake_case ) lowercase__: Dict = DDPMScheduler(beta_schedule='squaredcos_cap_v2' , variance_type='fixed_large' ) lowercase__: List[Any] = SpectrogramNotesEncoder( max_length=synth_model.sequence_length['inputs'] , vocab_size=synth_model.model.module.config.vocab_size , d_model=synth_model.model.module.config.emb_dim , dropout_rate=synth_model.model.module.config.dropout_rate , num_layers=synth_model.model.module.config.num_encoder_layers , num_heads=synth_model.model.module.config.num_heads , d_kv=synth_model.model.module.config.head_dim , d_ff=synth_model.model.module.config.mlp_dim , feed_forward_proj='gated-gelu' , ) lowercase__: Dict = SpectrogramContEncoder( input_dims=synth_model.audio_codec.n_dims , targets_context_length=synth_model.sequence_length['targets_context'] , d_model=synth_model.model.module.config.emb_dim , dropout_rate=synth_model.model.module.config.dropout_rate , num_layers=synth_model.model.module.config.num_encoder_layers , num_heads=synth_model.model.module.config.num_heads , d_kv=synth_model.model.module.config.head_dim , d_ff=synth_model.model.module.config.mlp_dim , feed_forward_proj='gated-gelu' , ) lowercase__: Optional[Any] = TaFilmDecoder( input_dims=synth_model.audio_codec.n_dims , targets_length=synth_model.sequence_length['targets_context'] , max_decoder_noise_time=synth_model.model.module.config.max_decoder_noise_time , d_model=synth_model.model.module.config.emb_dim , num_layers=synth_model.model.module.config.num_decoder_layers , num_heads=synth_model.model.module.config.num_heads , d_kv=synth_model.model.module.config.head_dim , d_ff=synth_model.model.module.config.mlp_dim , dropout_rate=synth_model.model.module.config.dropout_rate , ) lowercase__: Dict = load_notes_encoder(ta_checkpoint['target']['token_encoder'] , snake_case ) lowercase__: int = load_continuous_encoder(ta_checkpoint['target']['continuous_encoder'] , snake_case ) lowercase__: Optional[int] = load_decoder(ta_checkpoint['target']['decoder'] , snake_case ) lowercase__: int = OnnxRuntimeModel.from_pretrained('kashif/soundstream_mel_decoder' ) lowercase__: List[Any] = SpectrogramDiffusionPipeline( notes_encoder=snake_case , continuous_encoder=snake_case , decoder=snake_case , scheduler=snake_case , melgan=snake_case , ) if args.save: pipe.save_pretrained(args.output_path ) if __name__ == "__main__": __lowerCAmelCase = argparse.ArgumentParser() parser.add_argument('''--output_path''', default=None, type=str, required=True, help='''Path to the converted model.''') parser.add_argument( '''--save''', default=True, type=bool, required=False, help='''Whether to save the converted model or not.''' ) parser.add_argument( '''--checkpoint_path''', default=F'''{MODEL}/checkpoint_500000''', type=str, required=False, help='''Path to the original jax model checkpoint.''', ) __lowerCAmelCase = parser.parse_args() main(args)
288
0
"""simple docstring""" import re from pathlib import Path from unittest import TestCase import pytest @pytest.mark.integration class UpperCamelCase_ ( UpperCamelCase): """simple docstring""" def UpperCAmelCase_ ( self : Optional[Any] , UpperCAmelCase__ : str ) -> Optional[int]: with open(UpperCAmelCase__ , encoding="utf-8" ) as input_file: __SCREAMING_SNAKE_CASE = re.compile(R"(?!.*\b(?:encoding|rb|w|wb|w+|wb+|ab|ab+)\b)(?<=\s)(open)\((.*)\)" ) __SCREAMING_SNAKE_CASE = input_file.read() __SCREAMING_SNAKE_CASE = regexp.search(UpperCAmelCase__ ) return match def UpperCAmelCase_ ( self : int , UpperCAmelCase__ : str ) -> Union[str, Any]: with open(UpperCAmelCase__ , encoding="utf-8" ) as input_file: __SCREAMING_SNAKE_CASE = re.compile(R"#[^\r\n]*print\(|\"[^\r\n]*print\(|\"\"\".*?print\(.*?\"\"\"|(print\()" , re.DOTALL ) __SCREAMING_SNAKE_CASE = input_file.read() # use `re.finditer` to handle the case where the ignored groups would be matched first by `re.search` __SCREAMING_SNAKE_CASE = regexp.finditer(UpperCAmelCase__ ) __SCREAMING_SNAKE_CASE = [match for match in matches if match is not None and match.group(1 ) is not None] return matches[0] if matches else None def UpperCAmelCase_ ( self : Dict ) -> Dict: __SCREAMING_SNAKE_CASE = Path("./datasets" ) __SCREAMING_SNAKE_CASE = list(dataset_paths.absolute().glob("**/*.py" ) ) for dataset in dataset_files: if self._no_encoding_on_file_open(str(UpperCAmelCase__ ) ): raise AssertionError(F"""open(...) must use utf-8 encoding in {dataset}""" ) def UpperCAmelCase_ ( self : str ) -> int: __SCREAMING_SNAKE_CASE = Path("./datasets" ) __SCREAMING_SNAKE_CASE = list(dataset_paths.absolute().glob("**/*.py" ) ) for dataset in dataset_files: if self._no_print_statements(str(UpperCAmelCase__ ) ): raise AssertionError(F"""print statement found in {dataset}. Use datasets.logger/logging instead.""" )
54
"""simple docstring""" import unittest import numpy as np import torch from diffusers import VersatileDiffusionImageVariationPipeline from diffusers.utils.testing_utils import load_image, require_torch_gpu, slow, torch_device a__ : Tuple = False class UpperCamelCase_ ( unittest.TestCase): """simple docstring""" pass @slow @require_torch_gpu class UpperCamelCase_ ( unittest.TestCase): """simple docstring""" def UpperCAmelCase_ ( self : int ) -> int: __SCREAMING_SNAKE_CASE = VersatileDiffusionImageVariationPipeline.from_pretrained("shi-labs/versatile-diffusion" ) pipe.to(UpperCAmelCase__ ) pipe.set_progress_bar_config(disable=UpperCAmelCase__ ) __SCREAMING_SNAKE_CASE = load_image( "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/versatile_diffusion/benz.jpg" ) __SCREAMING_SNAKE_CASE = torch.manual_seed(0 ) __SCREAMING_SNAKE_CASE = pipe( image=UpperCAmelCase__ , generator=UpperCAmelCase__ , guidance_scale=7.5 , num_inference_steps=5_0 , output_type="numpy" , ).images __SCREAMING_SNAKE_CASE = image[0, 2_5_3:2_5_6, 2_5_3:2_5_6, -1] assert image.shape == (1, 5_1_2, 5_1_2, 3) __SCREAMING_SNAKE_CASE = np.array([0.0_441, 0.0_469, 0.0_507, 0.0_575, 0.0_632, 0.0_650, 0.0_865, 0.0_909, 0.0_945] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-2
54
1
"""simple docstring""" import unittest from transformers import is_torch_available, is_vision_available from transformers.testing_utils import require_torch, require_vision, slow, torch_device if is_torch_available(): import torch from transformers import AutoModelForImageClassification if is_vision_available(): from transformers import AutoImageProcessor @require_torch @require_vision class UpperCamelCase_ ( unittest.TestCase ): @slow def UpperCamelCase_ ( self ) -> Any: """simple docstring""" UpperCAmelCase = AutoImageProcessor.from_pretrained("""microsoft/dit-base-finetuned-rvlcdip""" ) UpperCAmelCase = AutoModelForImageClassification.from_pretrained("""microsoft/dit-base-finetuned-rvlcdip""" ) model.to(snake_case__ ) from datasets import load_dataset UpperCAmelCase = load_dataset("""nielsr/rvlcdip-demo""" ) UpperCAmelCase = dataset["""train"""][0]["""image"""].convert("""RGB""" ) UpperCAmelCase = image_processor(snake_case__ , return_tensors="""pt""" ).to(snake_case__ ) # forward pass with torch.no_grad(): UpperCAmelCase = model(**snake_case__ ) UpperCAmelCase = outputs.logits UpperCAmelCase = torch.Size((1, 16) ) self.assertEqual(logits.shape , snake_case__ ) UpperCAmelCase = torch.tensor( [-0.4_158, -0.4_092, -0.4_347] , device=snake_case__ , dtype=torch.float , ) self.assertTrue(torch.allclose(logits[0, :3] , snake_case__ , atol=1e-4 ) )
248
"""simple docstring""" from __future__ import annotations import math def _lowerCAmelCase ( lowerCAmelCase ): '''simple docstring''' if 1 < number < 4: # 2 and 3 are primes return True elif number < 2 or number % 2 == 0 or number % 3 == 0: # Negatives, 0, 1, all even numbers, all multiples of 3 are not primes return False # All primes number are in format of 6k +/- 1 for i in range(5 , int(math.sqrt(lowerCAmelCase ) + 1 ) , 6 ): if number % i == 0 or number % (i + 2) == 0: return False return True def _lowerCAmelCase ( lowerCAmelCase ): '''simple docstring''' UpperCAmelCase = str(lowerCAmelCase ) UpperCAmelCase = [n] for i in range(1 , len(lowerCAmelCase ) ): list_nums.append(int(str_num[i:] ) ) list_nums.append(int(str_num[:-i] ) ) return list_nums def _lowerCAmelCase ( lowerCAmelCase ): '''simple docstring''' if len(str(lowerCAmelCase ) ) > 3: if not is_prime(int(str(lowerCAmelCase )[-3:] ) ) or not is_prime(int(str(lowerCAmelCase )[:3] ) ): return False return True def _lowerCAmelCase ( lowerCAmelCase = 11 ): '''simple docstring''' UpperCAmelCase = [] UpperCAmelCase = 13 while len(lowerCAmelCase ) != count: if validate(lowerCAmelCase ): UpperCAmelCase = list_truncated_nums(lowerCAmelCase ) if all(is_prime(lowerCAmelCase ) for i in list_nums ): list_truncated_primes.append(lowerCAmelCase ) num += 2 return list_truncated_primes def _lowerCAmelCase ( ): '''simple docstring''' return sum(compute_truncated_primes(11 ) ) if __name__ == "__main__": print(F'{sum(compute_truncated_primes(1_1)) = }')
248
1
import argparse import torch from datasets import load_dataset from donut import DonutModel from transformers import ( DonutImageProcessor, DonutProcessor, DonutSwinConfig, DonutSwinModel, MBartConfig, MBartForCausalLM, VisionEncoderDecoderModel, XLMRobertaTokenizerFast, ) def UpperCamelCase ( snake_case__ : Dict ) -> Dict: UpperCamelCase : List[str] = model.config UpperCamelCase : str = DonutSwinConfig( image_size=original_config.input_size , patch_size=4 , depths=original_config.encoder_layer , num_heads=[4, 8, 16, 32] , window_size=original_config.window_size , embed_dim=128 , ) UpperCamelCase : Tuple = MBartConfig( is_decoder=__lowerCamelCase , is_encoder_decoder=__lowerCamelCase , add_cross_attention=__lowerCamelCase , decoder_layers=original_config.decoder_layer , max_position_embeddings=original_config.max_position_embeddings , vocab_size=len( model.decoder.tokenizer ) , scale_embedding=__lowerCamelCase , add_final_layer_norm=__lowerCamelCase , ) return encoder_config, decoder_config def UpperCamelCase ( snake_case__ : Dict ) -> List[str]: if "encoder.model" in name: UpperCamelCase : Dict = name.replace('encoder.model' , 'encoder' ) if "decoder.model" in name: UpperCamelCase : Union[str, Any] = name.replace('decoder.model' , 'decoder' ) if "patch_embed.proj" in name: UpperCamelCase : Optional[int] = name.replace('patch_embed.proj' , 'embeddings.patch_embeddings.projection' ) if "patch_embed.norm" in name: UpperCamelCase : List[Any] = name.replace('patch_embed.norm' , 'embeddings.norm' ) if name.startswith('encoder' ): if "layers" in name: UpperCamelCase : int = 'encoder.' + name if "attn.proj" in name: UpperCamelCase : Tuple = name.replace('attn.proj' , 'attention.output.dense' ) if "attn" in name and "mask" not in name: UpperCamelCase : List[str] = name.replace('attn' , 'attention.self' ) if "norm1" in name: UpperCamelCase : int = name.replace('norm1' , 'layernorm_before' ) if "norm2" in name: UpperCamelCase : int = name.replace('norm2' , 'layernorm_after' ) if "mlp.fc1" in name: UpperCamelCase : Tuple = name.replace('mlp.fc1' , 'intermediate.dense' ) if "mlp.fc2" in name: UpperCamelCase : List[Any] = name.replace('mlp.fc2' , 'output.dense' ) if name == "encoder.norm.weight": UpperCamelCase : List[Any] = 'encoder.layernorm.weight' if name == "encoder.norm.bias": UpperCamelCase : Any = 'encoder.layernorm.bias' return name def UpperCamelCase ( snake_case__ : Optional[int] , snake_case__ : Dict ) -> Any: for key in orig_state_dict.copy().keys(): UpperCamelCase : Any = orig_state_dict.pop(__lowerCamelCase ) if "qkv" in key: UpperCamelCase : Optional[Any] = key.split('.' ) UpperCamelCase : Optional[int] = int(key_split[3] ) UpperCamelCase : Union[str, Any] = int(key_split[5] ) UpperCamelCase : Union[str, Any] = model.encoder.encoder.layers[layer_num].blocks[block_num].attention.self.all_head_size if "weight" in key: UpperCamelCase : Dict = val[:dim, :] UpperCamelCase : Optional[Any] = val[dim : dim * 2, :] UpperCamelCase : str = val[-dim:, :] else: UpperCamelCase : List[Any] = val[:dim] UpperCamelCase : Dict = val[dim : dim * 2] UpperCamelCase : Tuple = val[-dim:] elif "attn_mask" in key or key in ["encoder.model.norm.weight", "encoder.model.norm.bias"]: # HuggingFace implementation doesn't use attn_mask buffer # and model doesn't use final LayerNorms for the encoder pass else: UpperCamelCase : Any = val return orig_state_dict def UpperCamelCase ( snake_case__ : List[Any] , snake_case__ : Tuple=None , snake_case__ : Union[str, Any]=False ) -> List[str]: # load original model UpperCamelCase : Optional[int] = DonutModel.from_pretrained(__lowerCamelCase ).eval() # load HuggingFace model UpperCamelCase , UpperCamelCase : Optional[Any] = get_configs(__lowerCamelCase ) UpperCamelCase : Tuple = DonutSwinModel(__lowerCamelCase ) UpperCamelCase : Tuple = MBartForCausalLM(__lowerCamelCase ) UpperCamelCase : Any = VisionEncoderDecoderModel(encoder=__lowerCamelCase , decoder=__lowerCamelCase ) model.eval() UpperCamelCase : Optional[Any] = original_model.state_dict() UpperCamelCase : Optional[int] = convert_state_dict(__lowerCamelCase , __lowerCamelCase ) model.load_state_dict(__lowerCamelCase ) # verify results on scanned document UpperCamelCase : Tuple = load_dataset('hf-internal-testing/example-documents' ) UpperCamelCase : Tuple = dataset['test'][0]['image'].convert('RGB' ) UpperCamelCase : List[str] = XLMRobertaTokenizerFast.from_pretrained(__lowerCamelCase , from_slow=__lowerCamelCase ) UpperCamelCase : Tuple = DonutImageProcessor( do_align_long_axis=original_model.config.align_long_axis , size=original_model.config.input_size[::-1] ) UpperCamelCase : Optional[Any] = DonutProcessor(__lowerCamelCase , __lowerCamelCase ) UpperCamelCase : Any = processor(__lowerCamelCase , return_tensors='pt' ).pixel_values if model_name == "naver-clova-ix/donut-base-finetuned-docvqa": UpperCamelCase : Optional[int] = '<s_docvqa><s_question>{user_input}</s_question><s_answer>' UpperCamelCase : Any = 'When is the coffee break?' UpperCamelCase : Optional[Any] = task_prompt.replace('{user_input}' , __lowerCamelCase ) elif model_name == "naver-clova-ix/donut-base-finetuned-rvlcdip": UpperCamelCase : Union[str, Any] = '<s_rvlcdip>' elif model_name in [ "naver-clova-ix/donut-base-finetuned-cord-v1", "naver-clova-ix/donut-base-finetuned-cord-v1-2560", ]: UpperCamelCase : List[Any] = '<s_cord>' elif model_name == "naver-clova-ix/donut-base-finetuned-cord-v2": UpperCamelCase : List[Any] = 's_cord-v2>' elif model_name == "naver-clova-ix/donut-base-finetuned-zhtrainticket": UpperCamelCase : int = '<s_zhtrainticket>' elif model_name in ["naver-clova-ix/donut-proto", "naver-clova-ix/donut-base"]: # use a random prompt UpperCamelCase : str = 'hello world' else: raise ValueError('Model name not supported' ) UpperCamelCase : int = original_model.decoder.tokenizer(__lowerCamelCase , add_special_tokens=__lowerCamelCase , return_tensors='pt' )[ 'input_ids' ] UpperCamelCase : List[Any] = original_model.encoder.model.patch_embed(__lowerCamelCase ) UpperCamelCase , UpperCamelCase : List[str] = model.encoder.embeddings(__lowerCamelCase ) assert torch.allclose(__lowerCamelCase , __lowerCamelCase , atol=1E-3 ) # verify encoder hidden states UpperCamelCase : Any = original_model.encoder(__lowerCamelCase ) UpperCamelCase : List[Any] = model.encoder(__lowerCamelCase ).last_hidden_state assert torch.allclose(__lowerCamelCase , __lowerCamelCase , atol=1E-2 ) # verify decoder hidden states UpperCamelCase : Union[str, Any] = original_model(__lowerCamelCase , __lowerCamelCase , __lowerCamelCase ).logits UpperCamelCase : Optional[int] = model(__lowerCamelCase , decoder_input_ids=__lowerCamelCase ).logits assert torch.allclose(__lowerCamelCase , __lowerCamelCase , atol=1E-3 ) print('Looks ok!' ) if pytorch_dump_folder_path is not None: print(F"""Saving model and processor to {pytorch_dump_folder_path}""" ) model.save_pretrained(__lowerCamelCase ) processor.save_pretrained(__lowerCamelCase ) if push_to_hub: model.push_to_hub('nielsr/' + model_name.split('/' )[-1] , commit_message='Update model' ) processor.push_to_hub('nielsr/' + model_name.split('/' )[-1] , commit_message='Update model' ) if __name__ == "__main__": __UpperCAmelCase = argparse.ArgumentParser() # Required parameters parser.add_argument( '''--model_name''', default='''naver-clova-ix/donut-base-finetuned-docvqa''', required=False, type=str, help='''Name of the original model you\'d like to convert.''', ) parser.add_argument( '''--pytorch_dump_folder_path''', default=None, required=False, 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 and processor to the 🤗 hub.''', ) __UpperCAmelCase = parser.parse_args() convert_donut_checkpoint(args.model_name, args.pytorch_dump_folder_path, args.push_to_hub)
119
from __future__ import annotations import unittest from transformers import LEDConfig, is_tf_available from transformers.testing_utils import require_tf, slow from ...test_configuration_common import ConfigTester from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_tf_available(): import tensorflow as tf from transformers import TFLEDForConditionalGeneration, TFLEDModel @require_tf class __lowerCAmelCase : UpperCamelCase__ = LEDConfig UpperCamelCase__ = {} UpperCamelCase__ = '''gelu''' def __init__( self :Optional[int] , __magic_name__ :Dict , __magic_name__ :List[str]=13 , __magic_name__ :Union[str, Any]=7 , __magic_name__ :str=True , __magic_name__ :Union[str, Any]=False , __magic_name__ :Union[str, Any]=99 , __magic_name__ :List[Any]=32 , __magic_name__ :str=2 , __magic_name__ :List[str]=4 , __magic_name__ :str=37 , __magic_name__ :Any=0.1 , __magic_name__ :Dict=0.1 , __magic_name__ :List[str]=20 , __magic_name__ :Union[str, Any]=2 , __magic_name__ :List[Any]=1 , __magic_name__ :Optional[int]=0 , __magic_name__ :Optional[int]=4 , ): '''simple docstring''' a = parent a = batch_size a = seq_length a = is_training a = use_labels a = vocab_size a = hidden_size a = num_hidden_layers a = num_attention_heads a = intermediate_size a = hidden_dropout_prob a = attention_probs_dropout_prob a = max_position_embeddings a = eos_token_id a = pad_token_id a = bos_token_id a = attention_window # `ModelTesterMixin.test_attention_outputs` is expecting attention tensors to be of size # [num_attention_heads, encoder_seq_length, encoder_key_length], but TFLongformerSelfAttention # returns attention of shape [num_attention_heads, encoder_seq_length, self.attention_window + 1] # because its local attention only attends to `self.attention_window` and one before and one after a = self.attention_window + 2 # because of padding `encoder_seq_length`, is different from `seq_length`. Relevant for # the `test_attention_outputs` and `test_hidden_states_output` tests a = ( self.seq_length + (self.attention_window - self.seq_length % self.attention_window) % self.attention_window ) def lowerCamelCase__ ( self :Tuple ): '''simple docstring''' a = ids_tensor([self.batch_size, self.seq_length - 1] , self.vocab_size ) a = tf.expand_dims(tf.constant([self.eos_token_id] * self.batch_size ) , 1 ) a = tf.concat([input_ids, eos_tensor] , axis=1 ) a = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) a = 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 , attention_window=self.attention_window , **self.config_updates , ) a = prepare_led_inputs_dict(__magic_name__ , __magic_name__ , __magic_name__ ) a = tf.concat( [tf.zeros_like(__magic_name__ )[:, :-1], tf.ones_like(__magic_name__ )[:, -1:]] , axis=-1 , ) a = global_attention_mask return config, inputs_dict def lowerCamelCase__ ( self :Union[str, Any] , __magic_name__ :Optional[int] , __magic_name__ :List[Any] ): '''simple docstring''' a = TFLEDModel(config=__magic_name__ ).get_decoder() a = inputs_dict["""input_ids"""] a = input_ids[:1, :] a = inputs_dict["""attention_mask"""][:1, :] a = 1 # first forward pass a = model(__magic_name__ , attention_mask=__magic_name__ , use_cache=__magic_name__ ) a , a = outputs.to_tuple() # create hypothetical next token and extent to next_input_ids a = ids_tensor((self.batch_size, 3) , config.vocab_size ) a = tf.cast(ids_tensor((self.batch_size, 3) , 2 ) , tf.inta ) # append to next input_ids and a = tf.concat([input_ids, next_tokens] , axis=-1 ) a = tf.concat([attention_mask, next_attn_mask] , axis=-1 ) a = model(__magic_name__ , attention_mask=__magic_name__ )[0] a = model(__magic_name__ , attention_mask=__magic_name__ , past_key_values=__magic_name__ )[0] self.parent.assertEqual(next_tokens.shape[1] , output_from_past.shape[1] ) # select random slice a = int(ids_tensor((1,) , output_from_past.shape[-1] ) ) a = output_from_no_past[:, -3:, random_slice_idx] a = output_from_past[:, :, random_slice_idx] # test that outputs are equal for slice tf.debugging.assert_near(__magic_name__ , __magic_name__ , rtol=1E-3 ) def __A ( __lowerCamelCase , __lowerCamelCase , __lowerCamelCase , __lowerCamelCase=None , __lowerCamelCase=None , __lowerCamelCase=None , __lowerCamelCase=None , ) -> List[str]: if attention_mask is None: a = tf.cast(tf.math.not_equal(__lowerCamelCase , config.pad_token_id ) , tf.inta ) if decoder_attention_mask is None: a = 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: a = tf.ones((config.encoder_layers, config.encoder_attention_heads) ) if decoder_head_mask is None: a = tf.ones((config.decoder_layers, config.decoder_attention_heads) ) return { "input_ids": input_ids, "attention_mask": attention_mask, "decoder_input_ids": decoder_input_ids, "decoder_attention_mask": decoder_attention_mask, "head_mask": head_mask, "decoder_head_mask": decoder_head_mask, } @require_tf class __lowerCAmelCase ( __magic_name__ , __magic_name__ , unittest.TestCase ): UpperCamelCase__ = (TFLEDForConditionalGeneration, TFLEDModel) if is_tf_available() else () UpperCamelCase__ = (TFLEDForConditionalGeneration,) if is_tf_available() else () UpperCamelCase__ = ( { '''conversational''': TFLEDForConditionalGeneration, '''feature-extraction''': TFLEDModel, '''summarization''': TFLEDForConditionalGeneration, '''text2text-generation''': TFLEDForConditionalGeneration, '''translation''': TFLEDForConditionalGeneration, } if is_tf_available() else {} ) UpperCamelCase__ = True UpperCamelCase__ = False UpperCamelCase__ = False UpperCamelCase__ = False def lowerCamelCase__ ( self :Tuple ): '''simple docstring''' a = TFLEDModelTester(self ) a = ConfigTester(self , config_class=__magic_name__ ) def lowerCamelCase__ ( self :int ): '''simple docstring''' self.config_tester.run_common_tests() def lowerCamelCase__ ( self :str ): '''simple docstring''' a = self.model_tester.prepare_config_and_inputs_for_common() self.model_tester.check_decoder_model_past_large_inputs(*__magic_name__ ) def lowerCamelCase__ ( self :Optional[int] ): '''simple docstring''' a , a = self.model_tester.prepare_config_and_inputs_for_common() a = tf.zeros_like(inputs_dict["""attention_mask"""] ) a = 2 a = tf.where( tf.range(self.model_tester.seq_length )[None, :] < num_global_attn_indices , 1 , inputs_dict["""global_attention_mask"""] , ) a = True a = self.model_tester.seq_length a = self.model_tester.encoder_seq_length def check_decoder_attentions_output(__magic_name__ :int ): a = outputs.decoder_attentions self.assertEqual(len(__magic_name__ ) , self.model_tester.num_hidden_layers ) self.assertListEqual( list(decoder_attentions[0].shape[-3:] ) , [self.model_tester.num_attention_heads, seq_length, seq_length] , ) def check_encoder_attentions_output(__magic_name__ :Any ): a = [t.numpy() for t in outputs.encoder_attentions] a = [t.numpy() for t in outputs.encoder_global_attentions] self.assertEqual(len(__magic_name__ ) , self.model_tester.num_hidden_layers ) self.assertEqual(len(__magic_name__ ) , self.model_tester.num_hidden_layers ) self.assertListEqual( list(attentions[0].shape[-3:] ) , [self.model_tester.num_attention_heads, seq_length, seq_length] , ) self.assertListEqual( list(global_attentions[0].shape[-3:] ) , [self.model_tester.num_attention_heads, encoder_seq_length, num_global_attn_indices] , ) for model_class in self.all_model_classes: a = True a = False a = False a = model_class(__magic_name__ ) a = model(self._prepare_for_class(__magic_name__ , __magic_name__ ) ) a = len(__magic_name__ ) self.assertEqual(config.output_hidden_states , __magic_name__ ) check_encoder_attentions_output(__magic_name__ ) if self.is_encoder_decoder: a = model_class(__magic_name__ ) a = model(self._prepare_for_class(__magic_name__ , __magic_name__ ) ) self.assertEqual(config.output_hidden_states , __magic_name__ ) check_decoder_attentions_output(__magic_name__ ) # Check that output attentions can also be changed via the config del inputs_dict["output_attentions"] a = True a = model_class(__magic_name__ ) a = model(self._prepare_for_class(__magic_name__ , __magic_name__ ) ) self.assertEqual(config.output_hidden_states , __magic_name__ ) check_encoder_attentions_output(__magic_name__ ) # Check attention is always last and order is fine a = True a = True a = model_class(__magic_name__ ) a = model(self._prepare_for_class(__magic_name__ , __magic_name__ ) ) self.assertEqual(out_len + (2 if self.is_encoder_decoder else 1) , len(__magic_name__ ) ) self.assertEqual(model.config.output_hidden_states , __magic_name__ ) check_encoder_attentions_output(__magic_name__ ) @unittest.skip("""LED keeps using potentially symbolic tensors in conditionals and breaks tracing.""" ) def lowerCamelCase__ ( self :Union[str, Any] ): '''simple docstring''' pass def lowerCamelCase__ ( self :int ): '''simple docstring''' pass def __A ( __lowerCamelCase ) -> int: return tf.constant(__lowerCamelCase , dtype=tf.intaa ) __UpperCamelCase : int = 1E-4 @slow @require_tf class __lowerCAmelCase ( unittest.TestCase ): def lowerCamelCase__ ( self :Union[str, Any] ): '''simple docstring''' a = TFLEDForConditionalGeneration.from_pretrained("""allenai/led-base-16384""" ).led # change to intended input here a = _long_tensor([512 * [0, 3_1414, 232, 328, 740, 1140, 1_2695, 69]] ) a = _long_tensor([128 * [0, 3_1414, 232, 328, 740, 1140, 1_2695, 69]] ) a = prepare_led_inputs_dict(model.config , __magic_name__ , __magic_name__ ) a = model(**__magic_name__ )[0] a = (1, 1024, 768) self.assertEqual(output.shape , __magic_name__ ) # change to expected output here a = tf.convert_to_tensor( [[2.3050, 2.8279, 0.6531], [-1.8457, -0.1455, -3.5661], [-1.0186, 0.4586, -2.2043]] , ) tf.debugging.assert_near(output[:, :3, :3] , __magic_name__ , atol=1E-3 ) def lowerCamelCase__ ( self :str ): '''simple docstring''' a = TFLEDForConditionalGeneration.from_pretrained("""allenai/led-base-16384""" ) # change to intended input here a = _long_tensor([512 * [0, 3_1414, 232, 328, 740, 1140, 1_2695, 69]] ) a = _long_tensor([128 * [0, 3_1414, 232, 328, 740, 1140, 1_2695, 69]] ) a = prepare_led_inputs_dict(model.config , __magic_name__ , __magic_name__ ) a = model(**__magic_name__ )[0] a = (1, 1024, model.config.vocab_size) self.assertEqual(output.shape , __magic_name__ ) # change to expected output here a = tf.convert_to_tensor( [[33.6507, 6.4572, 16.8089], [5.8739, -2.4238, 11.2902], [-3.2139, -4.3149, 4.2783]] , ) tf.debugging.assert_near(output[:, :3, :3] , __magic_name__ , atol=1E-3 , rtol=1E-3 )
228
0
'''simple docstring''' from graphs.minimum_spanning_tree_kruskal import kruskal def lowerCamelCase (): __a : str = 9 __a : List[str] = [ [0, 1, 4], [0, 7, 8], [1, 2, 8], [7, 8, 7], [7, 6, 1], [2, 8, 2], [8, 6, 6], [2, 3, 7], [2, 5, 4], [6, 5, 2], [3, 5, 14], [3, 4, 9], [5, 4, 10], [1, 7, 11], ] __a : Tuple = kruskal(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) __a : Tuple = [ [7, 6, 1], [2, 8, 2], [6, 5, 2], [0, 1, 4], [2, 5, 4], [2, 3, 7], [0, 7, 8], [3, 4, 9], ] assert sorted(_SCREAMING_SNAKE_CASE ) == sorted(_SCREAMING_SNAKE_CASE )
356
'''simple docstring''' import os def lowerCamelCase (): with open(os.path.dirname(_SCREAMING_SNAKE_CASE ) + '/p022_names.txt' ) as file: __a : List[Any] = str(file.readlines()[0] ) __a : str = names.replace('"' , '' ).split(',' ) names.sort() __a : Union[str, Any] = 0 __a : Tuple = 0 for i, name in enumerate(_SCREAMING_SNAKE_CASE ): for letter in name: name_score += ord(_SCREAMING_SNAKE_CASE ) - 64 total_score += (i + 1) * name_score __a : Any = 0 return total_score if __name__ == "__main__": print(solution())
294
0
"""simple docstring""" def _SCREAMING_SNAKE_CASE ( _lowercase : str ) ->list: '''simple docstring''' if n_term == "": return [] a : list = [] for temp in range(int(_lowercase ) ): series.append(F"""1/{temp + 1}""" if series else "1" ) return series if __name__ == "__main__": a : Any = input('''Enter the last number (nth term) of the Harmonic Series''') print('''Formula of Harmonic Series => 1+1/2+1/3 ..... 1/n''') print(harmonic_series(nth_term))
105
"""simple docstring""" import argparse import logging import pickle import random import time import numpy as np from transformers import BertTokenizer, GPTaTokenizer, RobertaTokenizer logging.basicConfig( format='''%(asctime)s - %(levelname)s - %(name)s - %(message)s''', datefmt='''%m/%d/%Y %H:%M:%S''', level=logging.INFO ) a : Union[str, Any] = logging.getLogger(__name__) def _SCREAMING_SNAKE_CASE ( ) ->Tuple: '''simple docstring''' a : Dict = argparse.ArgumentParser( description="Preprocess the data to avoid re-doing it several times by (tokenization + token_to_ids)." ) parser.add_argument("--file_path" , type=_lowercase , default="data/dump.txt" , help="The path to the data." ) parser.add_argument("--tokenizer_type" , type=_lowercase , default="bert" , choices=["bert", "roberta", "gpt2"] ) parser.add_argument("--tokenizer_name" , type=_lowercase , default="bert-base-uncased" , help="The tokenizer to use." ) parser.add_argument("--dump_file" , type=_lowercase , default="data/dump" , help="The dump file prefix." ) a : Dict = parser.parse_args() logger.info(F"""Loading Tokenizer ({args.tokenizer_name})""" ) if args.tokenizer_type == "bert": a : Optional[Any] = BertTokenizer.from_pretrained(args.tokenizer_name ) a : str = tokenizer.special_tokens_map["cls_token"] # `[CLS]` a : List[str] = tokenizer.special_tokens_map["sep_token"] # `[SEP]` elif args.tokenizer_type == "roberta": a : Tuple = RobertaTokenizer.from_pretrained(args.tokenizer_name ) a : Union[str, Any] = tokenizer.special_tokens_map["cls_token"] # `<s>` a : str = tokenizer.special_tokens_map["sep_token"] # `</s>` elif args.tokenizer_type == "gpt2": a : List[Any] = GPTaTokenizer.from_pretrained(args.tokenizer_name ) a : Optional[int] = tokenizer.special_tokens_map["bos_token"] # `<|endoftext|>` a : List[Any] = tokenizer.special_tokens_map["eos_token"] # `<|endoftext|>` logger.info(F"""Loading text from {args.file_path}""" ) with open(args.file_path , "r" , encoding="utf8" ) as fp: a : List[Any] = fp.readlines() logger.info("Start encoding" ) logger.info(F"""{len(_lowercase )} examples to process.""" ) a : Optional[Any] = [] a : Optional[Any] = 0 a : int = 1_0000 a : Dict = time.time() for text in data: a : List[Any] = F"""{bos} {text.strip()} {sep}""" a : Optional[int] = tokenizer.encode(_lowercase , add_special_tokens=_lowercase ) rslt.append(_lowercase ) iter += 1 if iter % interval == 0: a : Optional[Any] = time.time() logger.info(F"""{iter} examples processed. - {(end-start):.2f}s/{interval}expl""" ) a : Optional[Any] = time.time() logger.info("Finished binarization" ) logger.info(F"""{len(_lowercase )} examples processed.""" ) a : Optional[int] = F"""{args.dump_file}.{args.tokenizer_name}.pickle""" a : Tuple = tokenizer.vocab_size if vocab_size < (1 << 16): a : Optional[int] = [np.uintaa(_lowercase ) for d in rslt] else: a : Optional[Any] = [np.intaa(_lowercase ) for d in rslt] random.shuffle(rslt_ ) logger.info(F"""Dump to {dp_file}""" ) with open(_lowercase , "wb" ) as handle: pickle.dump(rslt_ , _lowercase , protocol=pickle.HIGHEST_PROTOCOL ) if __name__ == "__main__": main()
105
1
import builtins import sys from ...utils.imports import _is_package_available from . import cursor, input from .helpers import Direction, clear_line, forceWrite, linebreak, move_cursor, reset_cursor, writeColor from .keymap import KEYMAP _UpperCAmelCase = False try: _UpperCAmelCase = _is_package_available("""google.colab""") except ModuleNotFoundError: pass @input.register class UpperCAmelCase : '''simple docstring''' def __init__( self , lowercase = None , lowercase = [] ): """simple docstring""" A_ : Optional[int] = 0 A_ : str = choices A_ : Union[str, Any] = prompt if sys.platform == "win32": A_ : Any = '*' else: A_ : Any = '➔ ' def lowerCAmelCase_ ( self , lowercase , lowercase = "" ): """simple docstring""" if sys.platform != "win32": writeColor(self.choices[index] , 3_2 , lowercase ) else: forceWrite(self.choices[index] , lowercase ) def lowerCAmelCase_ ( self , lowercase ): """simple docstring""" if index == self.position: forceWrite(F''' {self.arrow_char} ''' ) self.write_choice(lowercase ) else: forceWrite(F''' {self.choices[index]}''' ) reset_cursor() def lowerCAmelCase_ ( self , lowercase , lowercase = 1 ): """simple docstring""" A_ : Optional[Any] = self.position if direction == Direction.DOWN: if self.position + 1 >= len(self.choices ): return self.position += num_spaces else: if self.position - 1 < 0: return self.position -= num_spaces clear_line() self.print_choice(lowercase ) move_cursor(lowercase , direction.name ) self.print_choice(self.position ) @input.mark(KEYMAP['up'] ) def lowerCAmelCase_ ( self ): """simple docstring""" self.move_direction(Direction.UP ) @input.mark(KEYMAP['down'] ) def lowerCAmelCase_ ( self ): """simple docstring""" self.move_direction(Direction.DOWN ) @input.mark(KEYMAP['newline'] ) def lowerCAmelCase_ ( self ): """simple docstring""" move_cursor(len(self.choices ) - self.position , 'DOWN' ) return self.position @input.mark(KEYMAP['interrupt'] ) def lowerCAmelCase_ ( self ): """simple docstring""" move_cursor(len(self.choices ) - self.position , 'DOWN' ) raise KeyboardInterrupt @input.mark_multiple(*[KEYMAP[str(lowercase )] for number in range(1_0 )] ) def lowerCAmelCase_ ( self ): """simple docstring""" A_ : int = int(chr(self.current_selection ) ) A_ : List[str] = index - self.position if index == self.position: return if index < len(self.choices ): if self.position > index: self.move_direction(Direction.UP , -movement ) elif self.position < index: self.move_direction(Direction.DOWN , lowercase ) else: return else: return def lowerCAmelCase_ ( self , lowercase = 0 ): """simple docstring""" if self.prompt: linebreak() forceWrite(self.prompt , '\n' ) if in_colab: forceWrite('Please input a choice index (starting from 0), and press enter' , '\n' ) else: forceWrite('Please select a choice using the arrow or number keys, and selecting with enter' , '\n' ) A_ : Optional[int] = default_choice for i in range(len(self.choices ) ): self.print_choice(lowercase ) forceWrite('\n' ) move_cursor(len(self.choices ) - self.position , 'UP' ) with cursor.hide(): while True: if in_colab: try: A_ : int = int(builtins.input() ) except ValueError: A_ : int = default_choice else: A_ : Union[str, Any] = self.handle_input() if choice is not None: reset_cursor() for _ in range(len(self.choices ) + 1 ): move_cursor(1 , 'UP' ) clear_line() self.write_choice(lowercase , '\n' ) return choice
360
from typing import TYPE_CHECKING # rely on isort to merge the imports from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available _UpperCAmelCase = { """configuration_autoformer""": [ """AUTOFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP""", """AutoformerConfig""", ], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _UpperCAmelCase = [ """AUTOFORMER_PRETRAINED_MODEL_ARCHIVE_LIST""", """AutoformerForPrediction""", """AutoformerModel""", """AutoformerPreTrainedModel""", ] if TYPE_CHECKING: from .configuration_autoformer import ( AUTOFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP, AutoformerConfig, ) try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_autoformer import ( AUTOFORMER_PRETRAINED_MODEL_ARCHIVE_LIST, AutoformerForPrediction, AutoformerModel, AutoformerPreTrainedModel, ) else: import sys _UpperCAmelCase = _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)
192
0
"""simple docstring""" from __future__ import annotations import inspect import unittest from transformers import ViTConfig 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 TFViTForImageClassification, TFViTModel if is_vision_available(): from PIL import Image from transformers import ViTImageProcessor class A_ : """simple docstring""" def __init__( self :Union[str, Any] , lowerCamelCase_ :int , lowerCamelCase_ :int=13 , lowerCamelCase_ :Any=30 , lowerCamelCase_ :Union[str, Any]=2 , lowerCamelCase_ :str=3 , lowerCamelCase_ :Optional[int]=True , lowerCamelCase_ :Dict=True , lowerCamelCase_ :Tuple=32 , lowerCamelCase_ :Dict=2 , lowerCamelCase_ :Dict=4 , lowerCamelCase_ :Any=37 , lowerCamelCase_ :List[str]="gelu" , lowerCamelCase_ :Tuple=0.1 , lowerCamelCase_ :Tuple=0.1 , lowerCamelCase_ :int=10 , lowerCamelCase_ :int=0.02 , lowerCamelCase_ :List[str]=3 , lowerCamelCase_ :Union[str, Any]=None , ): """simple docstring""" lowerCamelCase__ : Dict =parent lowerCamelCase__ : int =batch_size lowerCamelCase__ : Tuple =image_size lowerCamelCase__ : List[Any] =patch_size lowerCamelCase__ : Dict =num_channels lowerCamelCase__ : List[str] =is_training lowerCamelCase__ : Tuple =use_labels lowerCamelCase__ : List[str] =hidden_size lowerCamelCase__ : Union[str, Any] =num_hidden_layers lowerCamelCase__ : List[Any] =num_attention_heads lowerCamelCase__ : int =intermediate_size lowerCamelCase__ : Optional[Any] =hidden_act lowerCamelCase__ : Tuple =hidden_dropout_prob lowerCamelCase__ : int =attention_probs_dropout_prob lowerCamelCase__ : int =type_sequence_label_size lowerCamelCase__ : int =initializer_range lowerCamelCase__ : Any =scope # in ViT, the seq length equals the number of patches + 1 (we add 1 for the [CLS] token) lowerCamelCase__ : Optional[Any] =(image_size // patch_size) ** 2 lowerCamelCase__ : Optional[Any] =num_patches + 1 def UpperCAmelCase__ ( self :Optional[int] ): """simple docstring""" lowerCamelCase__ : Optional[int] =floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) lowerCamelCase__ : str =None if self.use_labels: lowerCamelCase__ : Any =ids_tensor([self.batch_size] , self.type_sequence_label_size ) lowerCamelCase__ : Optional[int] =self.get_config() return config, pixel_values, labels def UpperCAmelCase__ ( self :str ): """simple docstring""" return ViTConfig( image_size=self.image_size , patch_size=self.patch_size , num_channels=self.num_channels , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , is_decoder=lowerCamelCase_ , initializer_range=self.initializer_range , ) def UpperCAmelCase__ ( self :Optional[int] , lowerCamelCase_ :Tuple , lowerCamelCase_ :Union[str, Any] , lowerCamelCase_ :Tuple ): """simple docstring""" lowerCamelCase__ : List[Any] =TFViTModel(config=lowerCamelCase_ ) lowerCamelCase__ : Any =model(lowerCamelCase_ , training=lowerCamelCase_ ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) # Test with an image with different size than the one specified in config. lowerCamelCase__ : Any =self.image_size // 2 lowerCamelCase__ : Dict =pixel_values[:, :, :image_size, :image_size] lowerCamelCase__ : Any =model(lowerCamelCase_ , interpolate_pos_encoding=lowerCamelCase_ , training=lowerCamelCase_ ) lowerCamelCase__ : Optional[int] =(image_size // self.patch_size) ** 2 + 1 self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, seq_length, self.hidden_size) ) def UpperCAmelCase__ ( self :Union[str, Any] , lowerCamelCase_ :Optional[Any] , lowerCamelCase_ :str , lowerCamelCase_ :str ): """simple docstring""" lowerCamelCase__ : List[str] =self.type_sequence_label_size lowerCamelCase__ : Union[str, Any] =TFViTForImageClassification(lowerCamelCase_ ) lowerCamelCase__ : str =model(lowerCamelCase_ , labels=lowerCamelCase_ , training=lowerCamelCase_ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.type_sequence_label_size) ) # Test with an image with different size than the one specified in config. lowerCamelCase__ : Tuple =self.image_size // 2 lowerCamelCase__ : Tuple =pixel_values[:, :, :image_size, :image_size] lowerCamelCase__ : Union[str, Any] =model(lowerCamelCase_ , interpolate_pos_encoding=lowerCamelCase_ , training=lowerCamelCase_ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.type_sequence_label_size) ) # test greyscale images lowerCamelCase__ : Tuple =1 lowerCamelCase__ : Union[str, Any] =TFViTForImageClassification(lowerCamelCase_ ) lowerCamelCase__ : Tuple =floats_tensor([self.batch_size, 1, self.image_size, self.image_size] ) lowerCamelCase__ : int =model(lowerCamelCase_ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.type_sequence_label_size) ) def UpperCAmelCase__ ( self :Union[str, Any] ): """simple docstring""" lowerCamelCase__ : Any =self.prepare_config_and_inputs() lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ : List[str] =config_and_inputs lowerCamelCase__ : List[Any] ={'pixel_values': pixel_values} return config, inputs_dict @require_tf class A_ ( A__ , A__ , unittest.TestCase ): """simple docstring""" SCREAMING_SNAKE_CASE_ = (TFViTModel, TFViTForImageClassification) if is_tf_available() else () SCREAMING_SNAKE_CASE_ = ( {"""feature-extraction""": TFViTModel, """image-classification""": TFViTForImageClassification} if is_tf_available() else {} ) SCREAMING_SNAKE_CASE_ = False SCREAMING_SNAKE_CASE_ = False SCREAMING_SNAKE_CASE_ = False def UpperCAmelCase__ ( self :Optional[int] ): """simple docstring""" lowerCamelCase__ : Optional[int] =TFViTModelTester(self ) lowerCamelCase__ : Optional[Any] =ConfigTester(self , config_class=lowerCamelCase_ , has_text_modality=lowerCamelCase_ , hidden_size=37 ) def UpperCAmelCase__ ( self :Optional[Any] ): """simple docstring""" self.config_tester.run_common_tests() @unittest.skip(reason='ViT does not use inputs_embeds' ) def UpperCAmelCase__ ( self :str ): """simple docstring""" pass @unittest.skip(reason='ViT does not use inputs_embeds' ) def UpperCAmelCase__ ( self :int ): """simple docstring""" pass def UpperCAmelCase__ ( self :Union[str, Any] ): """simple docstring""" lowerCamelCase__ , lowerCamelCase__ : Tuple =self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: lowerCamelCase__ : str =model_class(lowerCamelCase_ ) self.assertIsInstance(model.get_input_embeddings() , (tf.keras.layers.Layer) ) lowerCamelCase__ : List[str] =model.get_output_embeddings() self.assertTrue(x is None or isinstance(lowerCamelCase_ , tf.keras.layers.Layer ) ) def UpperCAmelCase__ ( self :int ): """simple docstring""" lowerCamelCase__ , lowerCamelCase__ : Any =self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: lowerCamelCase__ : Union[str, Any] =model_class(lowerCamelCase_ ) lowerCamelCase__ : Any =inspect.signature(model.call ) # signature.parameters is an OrderedDict => so arg_names order is deterministic lowerCamelCase__ : Tuple =[*signature.parameters.keys()] lowerCamelCase__ : str =['pixel_values'] self.assertListEqual(arg_names[:1] , lowerCamelCase_ ) def UpperCAmelCase__ ( self :Union[str, Any] ): """simple docstring""" lowerCamelCase__ : int =self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*lowerCamelCase_ ) def UpperCAmelCase__ ( self :int ): """simple docstring""" lowerCamelCase__ : Any =self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_image_classification(*lowerCamelCase_ ) @slow def UpperCAmelCase__ ( self :Tuple ): """simple docstring""" lowerCamelCase__ : str =TFViTModel.from_pretrained('google/vit-base-patch16-224' ) self.assertIsNotNone(lowerCamelCase_ ) def lowerCAmelCase_ ( ) ->Tuple: lowerCamelCase__ : List[Any] =Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png' ) return image @require_tf @require_vision class A_ ( unittest.TestCase ): """simple docstring""" @cached_property def UpperCAmelCase__ ( self :List[str] ): """simple docstring""" return ViTImageProcessor.from_pretrained('google/vit-base-patch16-224' ) if is_vision_available() else None @slow def UpperCAmelCase__ ( self :int ): """simple docstring""" lowerCamelCase__ : int =TFViTForImageClassification.from_pretrained('google/vit-base-patch16-224' ) lowerCamelCase__ : Union[str, Any] =self.default_image_processor lowerCamelCase__ : int =prepare_img() lowerCamelCase__ : Optional[Any] =image_processor(images=lowerCamelCase_ , return_tensors='tf' ) # forward pass lowerCamelCase__ : int =model(**lowerCamelCase_ ) # verify the logits lowerCamelCase__ : Union[str, Any] =tf.TensorShape((1, 1_000) ) self.assertEqual(outputs.logits.shape , lowerCamelCase_ ) lowerCamelCase__ : Optional[Any] =tf.constant([-0.27_44, 0.82_15, -0.08_36] ) tf.debugging.assert_near(outputs.logits[0, :3] , lowerCamelCase_ , atol=1e-4 )
126
"""simple docstring""" import math import os from copy import deepcopy import datasets import evaluate import torch import transformers from datasets import load_dataset from torch.utils.data import DataLoader from transformers import AutoModelForSequenceClassification, AutoTokenizer from accelerate import Accelerator from accelerate.test_utils import RegressionDataset, RegressionModel from accelerate.utils import is_tpu_available, set_seed lowerCAmelCase = """true""" def lowerCAmelCase_ ( snake_case_ : Dict , snake_case_ : int=8_2 , snake_case_ : Optional[Any]=1_6 ) ->Dict: set_seed(4_2 ) lowerCamelCase__ : List[Any] =RegressionModel() lowerCamelCase__ : List[Any] =deepcopy(snake_case_ ) lowerCamelCase__ : List[str] =RegressionDataset(length=snake_case_ ) lowerCamelCase__ : Any =DataLoader(snake_case_ , batch_size=snake_case_ ) model.to(accelerator.device ) lowerCamelCase__ , lowerCamelCase__ : Optional[Any] =accelerator.prepare(snake_case_ , snake_case_ ) return model, ddp_model, dataloader def lowerCAmelCase_ ( snake_case_ : Accelerator , snake_case_ : str=False ) ->List[str]: lowerCamelCase__ : int =AutoTokenizer.from_pretrained('hf-internal-testing/mrpc-bert-base-cased' ) lowerCamelCase__ : List[Any] =load_dataset('glue' , 'mrpc' , split='validation' ) def tokenize_function(snake_case_ : Optional[Any] ): lowerCamelCase__ : Optional[int] =tokenizer(examples['sentence1'] , examples['sentence2'] , truncation=snake_case_ , max_length=snake_case_ ) return outputs with accelerator.main_process_first(): lowerCamelCase__ : Tuple =dataset.map( snake_case_ , batched=snake_case_ , remove_columns=['idx', 'sentence1', 'sentence2'] , ) lowerCamelCase__ : List[Any] =tokenized_datasets.rename_column('label' , 'labels' ) def collate_fn(snake_case_ : Union[str, Any] ): if use_longest: return tokenizer.pad(snake_case_ , padding='longest' , return_tensors='pt' ) return tokenizer.pad(snake_case_ , padding='max_length' , max_length=1_2_8 , return_tensors='pt' ) return DataLoader(snake_case_ , shuffle=snake_case_ , collate_fn=snake_case_ , batch_size=1_6 ) def lowerCAmelCase_ ( snake_case_ : List[str] , snake_case_ : Tuple ) ->Any: lowerCamelCase__ : Optional[int] =Accelerator(dispatch_batches=snake_case_ , split_batches=snake_case_ ) lowerCamelCase__ : List[Any] =get_dataloader(snake_case_ , not dispatch_batches ) lowerCamelCase__ : Union[str, Any] =AutoModelForSequenceClassification.from_pretrained( 'hf-internal-testing/mrpc-bert-base-cased' , return_dict=snake_case_ ) lowerCamelCase__ , lowerCamelCase__ : Dict =accelerator.prepare(snake_case_ , snake_case_ ) return {"ddp": [ddp_model, ddp_dataloader, "cuda:0"], "no": [model, dataloader, accelerator.device]}, accelerator def lowerCAmelCase_ ( snake_case_ : Tuple , snake_case_ : List[Any] , snake_case_ : List[str] ) ->Dict: lowerCamelCase__ : Optional[Any] =[] for batch in dataloader: lowerCamelCase__ , lowerCamelCase__ : int =batch.values() with torch.no_grad(): lowerCamelCase__ : Optional[Any] =model(snake_case_ ) lowerCamelCase__ , lowerCamelCase__ : Optional[Any] =accelerator.gather_for_metrics((logit, target) ) logits_and_targets.append((logit, target) ) lowerCamelCase__ , lowerCamelCase__ : Optional[Any] =[], [] for logit, targ in logits_and_targets: logits.append(snake_case_ ) targs.append(snake_case_ ) lowerCamelCase__ , lowerCamelCase__ : Optional[Any] =torch.cat(snake_case_ ), torch.cat(snake_case_ ) return logits, targs def lowerCAmelCase_ ( snake_case_ : Accelerator , snake_case_ : Optional[int]=8_2 , snake_case_ : Any=False , snake_case_ : List[Any]=False , snake_case_ : Optional[int]=1_6 ) ->List[str]: lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ : Dict =get_basic_setup(snake_case_ , snake_case_ , snake_case_ ) lowerCamelCase__ , lowerCamelCase__ : Any =generate_predictions(snake_case_ , snake_case_ , snake_case_ ) assert ( len(snake_case_ ) == num_samples ), f"""Unexpected number of inputs:\n Expected: {num_samples}\n Actual: {len(snake_case_ )}""" def lowerCAmelCase_ ( snake_case_ : bool = False , snake_case_ : bool = False ) ->str: lowerCamelCase__ : Dict =evaluate.load('glue' , 'mrpc' ) lowerCamelCase__ , lowerCamelCase__ : Optional[Any] =get_mrpc_setup(snake_case_ , snake_case_ ) # First do baseline lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ : Dict =setup['no'] model.to(snake_case_ ) model.eval() for batch in dataloader: batch.to(snake_case_ ) with torch.inference_mode(): lowerCamelCase__ : Any =model(**snake_case_ ) lowerCamelCase__ : List[str] =outputs.logits.argmax(dim=-1 ) metric.add_batch(predictions=snake_case_ , references=batch['labels'] ) lowerCamelCase__ : Optional[Any] =metric.compute() # Then do distributed lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ : Optional[int] =setup['ddp'] model.eval() for batch in dataloader: with torch.inference_mode(): lowerCamelCase__ : List[Any] =model(**snake_case_ ) lowerCamelCase__ : str =outputs.logits.argmax(dim=-1 ) lowerCamelCase__ : int =batch['labels'] lowerCamelCase__ , lowerCamelCase__ : List[Any] =accelerator.gather_for_metrics((preds, references) ) metric.add_batch(predictions=snake_case_ , references=snake_case_ ) lowerCamelCase__ : List[str] =metric.compute() for key in "accuracy f1".split(): assert math.isclose( baseline[key] , distributed[key] ), f"""Baseline and Distributed are not the same for key {key}:\n\tBaseline: {baseline[key]}\n\tDistributed: {distributed[key]}\n""" def lowerCAmelCase_ ( ) ->str: lowerCamelCase__ : List[str] =Accelerator(split_batches=snake_case_ , dispatch_batches=snake_case_ ) if accelerator.is_local_main_process: datasets.utils.logging.set_verbosity_warning() transformers.utils.logging.set_verbosity_warning() else: datasets.utils.logging.set_verbosity_error() transformers.utils.logging.set_verbosity_error() # These are a bit slower so they should only be ran on the GPU or TPU if torch.cuda.is_available() or is_tpu_available(): if accelerator.is_local_main_process: print('**Testing gather_for_metrics**' ) for split_batches in [True, False]: for dispatch_batches in [True, False]: if accelerator.is_local_main_process: print(f"""With: `split_batches={split_batches}`, `dispatch_batches={dispatch_batches}`""" ) test_mrpc(snake_case_ , snake_case_ ) accelerator.state._reset_state() if accelerator.is_local_main_process: print('**Test torch metrics**' ) for split_batches in [True, False]: for dispatch_batches in [True, False]: lowerCamelCase__ : Dict =Accelerator(split_batches=snake_case_ , dispatch_batches=snake_case_ ) if accelerator.is_local_main_process: print(f"""With: `split_batches={split_batches}`, `dispatch_batches={dispatch_batches}`, length=99""" ) test_torch_metrics(snake_case_ , 9_9 ) accelerator.state._reset_state() if accelerator.is_local_main_process: print('**Test last batch is not dropped when perfectly divisible**' ) lowerCamelCase__ : List[Any] =Accelerator() test_torch_metrics(snake_case_ , 5_1_2 ) accelerator.state._reset_state() def lowerCAmelCase_ ( snake_case_ : List[Any] ) ->Dict: # For xla_spawn (TPUs) main() if __name__ == "__main__": main()
126
1
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_sentencepiece_available, is_tokenizers_available, is_torch_available, ) __snake_case = { '''configuration_llama''': ['''LLAMA_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''LlamaConfig'''], } try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __snake_case = ['''LlamaTokenizer'''] try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __snake_case = ['''LlamaTokenizerFast'''] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __snake_case = [ '''LlamaForCausalLM''', '''LlamaModel''', '''LlamaPreTrainedModel''', '''LlamaForSequenceClassification''', ] if TYPE_CHECKING: from .configuration_llama import LLAMA_PRETRAINED_CONFIG_ARCHIVE_MAP, LlamaConfig try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_llama import LlamaTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_llama_fast import LlamaTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_llama import LlamaForCausalLM, LlamaForSequenceClassification, LlamaModel, LlamaPreTrainedModel else: import sys __snake_case = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
357
from collections import defaultdict from pathlib import Path import pandas as pd from rouge_cli import calculate_rouge_path from utils import calculate_rouge __snake_case = [ '''Prosecutor: "No videos were used in the crash investigation" German papers say they saw a cell phone video of the''' ''' final seconds on board Flight 9525. The Germanwings co-pilot says he had a "previous episode of severe''' ''' depression\" German airline confirms it knew of Andreas Lubitz\'s depression years before he took control.''', '''The Palestinian Authority officially becomes the 123rd member of the International Criminal Court. The formal''' ''' accession was marked with a ceremony at The Hague, in the Netherlands. The Palestinians signed the ICC\'s''' ''' founding Rome Statute in January. Israel and the United States opposed the Palestinians\' efforts to join the''' ''' body.''', '''Amnesty International releases its annual report on the death penalty. The report catalogs the use of''' ''' state-sanctioned killing as a punitive measure across the globe. At least 607 people were executed around the''' ''' world in 2014, compared to 778 in 2013. The U.S. remains one of the worst offenders for imposing capital''' ''' punishment.''', ] __snake_case = [ '''Marseille prosecutor says "so far no videos were used in the crash investigation" despite media reports .''' ''' Journalists at Bild and Paris Match are "very confident" the video clip is real, an editor says . Andreas Lubitz''' ''' had informed his Lufthansa training school of an episode of severe depression, airline says .''', '''Membership gives the ICC jurisdiction over alleged crimes committed in Palestinian territories since last June .''' ''' Israel and the United States opposed the move, which could open the door to war crimes investigations against''' ''' Israelis .''', '''Amnesty\'s annual death penalty report catalogs encouraging signs, but setbacks in numbers of those sentenced to''' ''' death . Organization claims that governments around the world are using the threat of terrorism to advance''' ''' executions . The number of executions worldwide has gone down by almost 22% compared with 2013, but death''' ''' sentences up by 28% .''', ] def lowerCAmelCase_ ( )-> Optional[Any]: '''simple docstring''' UpperCAmelCase : Optional[int] =calculate_rouge(__lowerCAmelCase , __lowerCAmelCase , bootstrap_aggregation=__lowerCAmelCase , rouge_keys=['''rouge2''', '''rougeL'''] ) assert isinstance(__lowerCAmelCase , __lowerCAmelCase ) UpperCAmelCase : List[Any] =calculate_rouge(__lowerCAmelCase , __lowerCAmelCase , bootstrap_aggregation=__lowerCAmelCase , rouge_keys=['''rouge2'''] ) assert ( pd.DataFrame(no_aggregation['''rouge2'''] ).fmeasure.mean() == pd.DataFrame(no_aggregation_just_ra['''rouge2'''] ).fmeasure.mean() ) def lowerCAmelCase_ ( )-> Dict: '''simple docstring''' UpperCAmelCase : Any ='''rougeLsum''' UpperCAmelCase : Optional[Any] =calculate_rouge(__lowerCAmelCase , __lowerCAmelCase , newline_sep=__lowerCAmelCase , rouge_keys=[k] )[k] UpperCAmelCase : List[Any] =calculate_rouge(__lowerCAmelCase , __lowerCAmelCase , newline_sep=__lowerCAmelCase , rouge_keys=[k] )[k] assert score > score_no_sep def lowerCAmelCase_ ( )-> Any: '''simple docstring''' UpperCAmelCase : str =['''rouge1''', '''rouge2''', '''rougeL'''] UpperCAmelCase : int =calculate_rouge(__lowerCAmelCase , __lowerCAmelCase , newline_sep=__lowerCAmelCase , rouge_keys=__lowerCAmelCase ) UpperCAmelCase : Tuple =calculate_rouge(__lowerCAmelCase , __lowerCAmelCase , newline_sep=__lowerCAmelCase , rouge_keys=__lowerCAmelCase ) assert score_sep == score_no_sep def lowerCAmelCase_ ( )-> Dict: '''simple docstring''' UpperCAmelCase : int =[ '''Her older sister, Margot Frank, died in 1945, a month earlier than previously thought.''', '''Marseille prosecutor says "so far no videos were used in the crash investigation" despite media reports .''', ] UpperCAmelCase : Any =[ '''Margot Frank, died in 1945, a month earlier than previously thought.''', '''Prosecutor: "No videos were used in the crash investigation" German papers say they saw a cell phone video of''' ''' the final seconds on board Flight 9525.''', ] assert calculate_rouge(__lowerCAmelCase , __lowerCAmelCase , newline_sep=__lowerCAmelCase ) == calculate_rouge(__lowerCAmelCase , __lowerCAmelCase , newline_sep=__lowerCAmelCase ) def lowerCAmelCase_ ( )-> List[str]: '''simple docstring''' UpperCAmelCase : Union[str, Any] =[ '''" "a person who has such a video needs to immediately give it to the investigators," prosecutor says .<n> "it is a very disturbing scene," editor-in-chief of bild online tells "erin burnett: outfront" ''' ] UpperCAmelCase : Optional[Any] =[ ''' Marseille prosecutor says "so far no videos were used in the crash investigation" despite media reports . Journalists at Bild and Paris Match are "very confident" the video clip is real, an editor says . Andreas Lubitz had informed his Lufthansa training school of an episode of severe depression, airline says .''' ] UpperCAmelCase : Optional[int] =calculate_rouge(__lowerCAmelCase , __lowerCAmelCase , rouge_keys=['''rougeLsum'''] , newline_sep=__lowerCAmelCase )['''rougeLsum'''] UpperCAmelCase : int =calculate_rouge(__lowerCAmelCase , __lowerCAmelCase , rouge_keys=['''rougeLsum'''] )['''rougeLsum'''] assert new_score > prev_score def lowerCAmelCase_ ( )-> Optional[int]: '''simple docstring''' UpperCAmelCase : List[Any] =Path('''examples/seq2seq/test_data/wmt_en_ro''' ) UpperCAmelCase : Tuple =calculate_rouge_path(data_dir.joinpath('''test.source''' ) , data_dir.joinpath('''test.target''' ) ) assert isinstance(__lowerCAmelCase , __lowerCAmelCase ) UpperCAmelCase : Dict =calculate_rouge_path( data_dir.joinpath('''test.source''' ) , data_dir.joinpath('''test.target''' ) , bootstrap_aggregation=__lowerCAmelCase ) assert isinstance(__lowerCAmelCase , __lowerCAmelCase )
78
0
from dataclasses import dataclass, field from typing import Optional @dataclass class lowerCamelCase__: UpperCAmelCase__ : Optional[str] = field( default='codeparrot/codeparrot' , metadata={'help': 'Model name or path of model to be trained.'}) UpperCAmelCase__ : Optional[str] = field( default='./' , metadata={'help': 'Save dir where model repo is cloned and models updates are saved to.'}) UpperCAmelCase__ : Optional[str] = field( default='codeparrot/codeparrot-clean-train' , metadata={'help': 'Name or path of training dataset.'}) UpperCAmelCase__ : Optional[str] = field( default='codeparrot/codeparrot-clean-valid' , metadata={'help': 'Name or path of validation dataset.'}) UpperCAmelCase__ : Optional[int] = field(default=2 , metadata={'help': 'Batch size for training.'}) UpperCAmelCase__ : Optional[int] = field(default=2 , metadata={'help': 'Batch size for evaluation.'}) UpperCAmelCase__ : Optional[float] = field(default=0.1 , metadata={'help': 'Value of weight decay.'}) UpperCAmelCase__ : Optional[int] = field( default=1_0000 , metadata={'help': 'Size of buffer used to shuffle streaming dataset.'}) UpperCAmelCase__ : Optional[float] = field(default=2E-4 , metadata={'help': 'Learning rate fo training.'}) UpperCAmelCase__ : Optional[str] = field(default='cosine' , metadata={'help': 'Learning rate.'}) UpperCAmelCase__ : Optional[int] = field( default=750 , metadata={'help': 'Number of warmup steps in the learning rate schedule.'}) UpperCAmelCase__ : Optional[int] = field( default=16 , metadata={'help': 'Number of gradient accumulation steps.'}) UpperCAmelCase__ : Optional[bool] = field( default=__lowerCamelCase , metadata={'help': 'Use gradient checkpointing to reduce memory footprint.'}) UpperCAmelCase__ : Optional[int] = field(default=5_0000 , metadata={'help': 'Maximum number of training steps.'}) UpperCAmelCase__ : Optional[int] = field( default=-1 , metadata={'help': 'Maximum number of evaluation steps. If -1 the full dataset is evaluated.'}) UpperCAmelCase__ : Optional[int] = field(default=1024 , metadata={'help': 'Sequence lengths used for training.'}) UpperCAmelCase__ : Optional[int] = field(default=1 , metadata={'help': 'Training seed.'}) UpperCAmelCase__ : Optional[int] = field( default=1024 , metadata={'help': 'Interval to save checkpoints. Measured as number of forward passes not training steps.'} , ) UpperCAmelCase__ : Optional[str] = field( default=__lowerCamelCase , metadata={'help': 'States path if the training should continue from a checkpoint folder.'}) UpperCAmelCase__ : Optional[bool] = field(default=__lowerCamelCase , metadata={'help': 'If True the data is pretokenized.'}) @dataclass class lowerCamelCase__: UpperCAmelCase__ : Optional[str] = field( default='codeparrot/codeparrot' , metadata={'help': 'Model name or path of model to be evaluated.'}) UpperCAmelCase__ : Optional[str] = field( default='codeparrot/codeparrot-clean-valid' , metadata={'help': 'Name or path of validation dataset.'}) UpperCAmelCase__ : Optional[int] = field(default=2 , metadata={'help': 'Batch size used for evaluation.'}) UpperCAmelCase__ : Optional[int] = field( default=-1 , metadata={'help': 'Maximum number of evaluation steps. If -1 the full dataset is evaluated.'}) UpperCAmelCase__ : Optional[int] = field(default=1024 , metadata={'help': 'Length of sequences to be evaluated.'}) UpperCAmelCase__ : Optional[int] = field(default=1 , metadata={'help': 'Random seed used for evaluation.'}) @dataclass class lowerCamelCase__: UpperCAmelCase__ : Optional[str] = field( default='codeparrot/codeparrot' , metadata={'help': 'Model name or path of model to be evaluated.'}) UpperCAmelCase__ : Optional[int] = field(default=__lowerCamelCase , metadata={'help': 'Number of workers used for code evaluation.'}) UpperCAmelCase__ : Optional[int] = field( default=__lowerCamelCase , metadata={'help': 'The number of human-eval tasks to run. If not included all tasks are evaluated.'} , ) UpperCAmelCase__ : Optional[bool] = field( default=__lowerCamelCase , metadata={'help': 'Sample from the language model\'s output distribution.'}) UpperCAmelCase__ : Optional[float] = field(default=0.2 , metadata={'help': 'Sampling temperature used for generation.'}) UpperCAmelCase__ : Optional[int] = field(default=256 , metadata={'help': 'Maximum number of newly generated tokens.'}) UpperCAmelCase__ : Optional[int] = field(default=0 , metadata={'help': 'Top-k parameter used for generation.'}) UpperCAmelCase__ : Optional[float] = field(default=0.95 , metadata={'help': 'Top-p parameter used for nucleus sampling.'}) UpperCAmelCase__ : Optional[int] = field(default=10 , metadata={'help': 'Number of generations to run in parallel.'}) UpperCAmelCase__ : Optional[int] = field( default=200 , metadata={'help': 'Number of completions to generate for each sample.'}) UpperCAmelCase__ : Optional[int] = field(default=1 , metadata={'help': 'Random seed used for evaluation.'}) UpperCAmelCase__ : Optional[str] = field( default='eval_results.json' , metadata={'help': 'Random seed used for evaluation.'}) UpperCAmelCase__ : Optional[str] = field( default='0' , metadata={'help': 'Allow `code_eval` to execute Python code on machine'}) UpperCAmelCase__ : Optional[int] = field( default=-1 , metadata={ 'help': ( 'Determine which device to run the `text-generation` Pipeline on. -1 is CPU and any zero or positive' ' number corresponds to which GPU device id to run on.' ) } , ) @dataclass class lowerCamelCase__: UpperCAmelCase__ : Optional[int] = field( default=__lowerCamelCase , metadata={ 'help': 'The number of CPU cores to use for parallel preprocessing. Default uses the maximum available.' } , ) UpperCAmelCase__ : Optional[str] = field( default='transformersbook/codeparrot' , metadata={'help': 'Folder or name of dataset to process.'}) UpperCAmelCase__ : Optional[str] = field( default='codeparrot-clean' , metadata={'help': 'Folder to save processed processed dataset.'}) UpperCAmelCase__ : Optional[int] = field( default=10_0000 , metadata={'help': 'Number of files to save per JSON output file.'}) UpperCAmelCase__ : Optional[str] = field(default='content' , metadata={'help': 'Column containing text data to process.'}) UpperCAmelCase__ : Optional[float] = field( default=1000 , metadata={'help': 'Maximum line length in file, otherwise file is filtered.'}) UpperCAmelCase__ : Optional[float] = field( default=100 , metadata={'help': 'Maximum mean line length in file, otherwise file is filtered.'}) UpperCAmelCase__ : Optional[float] = field( default=0.25 , metadata={'help': 'Maximum fraction of non-alphanumeric characters, otherwise file is filtered.'}) UpperCAmelCase__ : Optional[float] = field( default=1.5 , metadata={'help': 'Minimum character token ratio for the file, otherwise file is filtered.'}) UpperCAmelCase__ : Optional[float] = field( default=0.7 , metadata={'help': 'Probability for filtering config, test and uncommon files.'}) UpperCAmelCase__ : Optional[str] = field( default='codeparrot/codeparrot' , metadata={'help': 'Name or path to the tokenizer.'} , ) UpperCAmelCase__ : Optional[bool] = field( default=__lowerCamelCase , metadata={'help': 'If True, near-duplicate samples are removed.'}) UpperCAmelCase__ : Optional[float] = field( default=0.85 , metadata={'help': 'Jaccard threshold for near-duplicate samples.'}) @dataclass class lowerCamelCase__: UpperCAmelCase__ : Optional[str] = field( default='gpt2' , metadata={'help': 'Base tokenizer to build new tokenizer from.'}) UpperCAmelCase__ : Optional[str] = field( default='transformersbook/codeparrot-train' , metadata={'help': 'Dataset to train tokenizer on.'}) UpperCAmelCase__ : Optional[str] = field(default='content' , metadata={'help': 'Column containing text data to process.'}) UpperCAmelCase__ : Optional[int] = field(default=20_0000 , metadata={'help': 'Number of examples to train tokenizer on.'}) UpperCAmelCase__ : Optional[int] = field( default=3_2768 , metadata={'help': 'Number of examples to train the tokenizer on.'}) UpperCAmelCase__ : Optional[str] = field(default='codeparrot' , metadata={'help': 'Name of new tokenizer.'}) UpperCAmelCase__ : Optional[bool] = field(default=__lowerCamelCase , metadata={'help': 'Push saved tokenizer to the hub.'}) @dataclass class lowerCamelCase__: UpperCAmelCase__ : Optional[str] = field( default='codeparrot/codeparrot' , metadata={'help': 'Name or path to the tokenizer.'}) UpperCAmelCase__ : Optional[str] = field( default='codeparrot/codeparrot-clean-train' , metadata={'help': 'Name or path to the dataset to pretokenize.'}) UpperCAmelCase__ : Optional[str] = field( default='tokenized-codeparrot-train' , metadata={'help': 'Repo name of the pretokenized data.'}) UpperCAmelCase__ : Optional[int] = field(default=__lowerCamelCase , metadata={'help': 'Number of workers used for code evaluation.'}) @dataclass class lowerCamelCase__: UpperCAmelCase__ : Optional[str] = field( default='gpt2-large' , metadata={'help': 'Configuration to use for model initialization.'}) UpperCAmelCase__ : Optional[str] = field( default='codeparrot/codeparrot' , metadata={'help': 'Tokenizer attached to model.'}) UpperCAmelCase__ : Optional[str] = field(default='codeparrot' , metadata={'help': 'Name of the created model.'}) UpperCAmelCase__ : Optional[bool] = field(default=__lowerCamelCase , metadata={'help': 'Push saved tokenizer to the hub.'})
12
from transformers import DistilBertTokenizer, DistilBertTokenizerFast from transformers.testing_utils import require_tokenizers, slow from ..bert.test_tokenization_bert import BertTokenizationTest @require_tokenizers class lowerCamelCase__( __lowerCamelCase): UpperCAmelCase__ : Dict = DistilBertTokenizer UpperCAmelCase__ : Dict = DistilBertTokenizerFast UpperCAmelCase__ : Tuple = True @slow def lowerCAmelCase__ ( self: Tuple ): __lowerCamelCase = DistilBertTokenizer.from_pretrained("""distilbert-base-uncased""" ) __lowerCamelCase = tokenizer.encode("""sequence builders""" , add_special_tokens=UpperCamelCase_ ) __lowerCamelCase = tokenizer.encode("""multi-sequence build""" , add_special_tokens=UpperCamelCase_ ) __lowerCamelCase = tokenizer.build_inputs_with_special_tokens(UpperCamelCase_ ) __lowerCamelCase = tokenizer.build_inputs_with_special_tokens(UpperCamelCase_ , UpperCamelCase_ ) assert encoded_sentence == [tokenizer.cls_token_id] + text + [tokenizer.sep_token_id] assert encoded_pair == [tokenizer.cls_token_id] + text + [tokenizer.sep_token_id] + text_a + [ tokenizer.sep_token_id ]
12
1
"""simple docstring""" import unittest from transformers.testing_utils import require_bsa from transformers.utils import is_bsa_available from ...test_feature_extraction_common import FeatureExtractionSavingTestMixin if is_bsa_available(): from transformers import MarkupLMFeatureExtractor class snake_case ( unittest.TestCase): def __init__( self : Optional[Any] , a__ : Any ) -> List[Any]: '''simple docstring''' _A = parent def a_ ( self : List[str] ) -> List[str]: '''simple docstring''' return {} def a__ ( ) -> str: _A = "<HTML>\n\n <HEAD>\n <TITLE>sample document</TITLE>\n </HEAD>\n\n <BODY BGCOLOR=\"FFFFFF\">\n <HR>\n <a href=\"http://google.com\">Goog</a>\n <H1>This is one header</H1>\n <H2>This is a another Header</H2>\n <P>Travel from\n <P>\n <B>SFO to JFK</B>\n <BR>\n <B><I>on May 2, 2015 at 2:00 pm. For details go to confirm.com </I></B>\n <HR>\n <div style=\"color:#0000FF\">\n <h3>Traveler <b> name </b> is\n <p> John Doe </p>\n </div>" _A = "\n <!DOCTYPE html>\n <html>\n <body>\n\n <h1>My First Heading</h1>\n <p>My first paragraph.</p>\n\n </body>\n </html>\n " return [html_string_a, html_string_a] @require_bsa class snake_case ( _UpperCamelCase , unittest.TestCase): __UpperCamelCase = MarkupLMFeatureExtractor if is_bsa_available() else None def a_ ( self : List[Any] ) -> Tuple: '''simple docstring''' _A = MarkupLMFeatureExtractionTester(self ) @property def a_ ( self : Optional[Any] ) -> Dict: '''simple docstring''' return self.feature_extract_tester.prepare_feat_extract_dict() def a_ ( self : Optional[int] ) -> List[str]: '''simple docstring''' _A = self.feature_extraction_class() # Test not batched input _A = get_html_strings()[0] _A = feature_extractor(a__ ) # fmt: off _A = [["sample document", "Goog", "This is one header", "This is a another Header", "Travel from", "SFO to JFK", "on May 2, 2015 at 2:00 pm. For details go to confirm.com", "Traveler", "name", "is", "John Doe"]] _A = [["/html/head/title", "/html/body/a", "/html/body/h1", "/html/body/h2", "/html/body/p", "/html/body/p/p/b[1]", "/html/body/p/p/b[2]/i", "/html/body/p/p/div/h3", "/html/body/p/p/div/h3/b", "/html/body/p/p/div/h3", "/html/body/p/p/div/h3/p"]] # fmt: on self.assertEqual(encoding.nodes , a__ ) self.assertEqual(encoding.xpaths , a__ ) # Test batched _A = get_html_strings() _A = feature_extractor(a__ ) # fmt: off _A = expected_nodes + [["My First Heading", "My first paragraph."]] _A = expected_xpaths + [["/html/body/h1", "/html/body/p"]] self.assertEqual(len(encoding.nodes ) , 2 ) self.assertEqual(len(encoding.xpaths ) , 2 ) self.assertEqual(encoding.nodes , a__ ) self.assertEqual(encoding.xpaths , a__ )
163
"""simple docstring""" from collections import namedtuple import requests from lxml import html # type: ignore a_ = namedtuple("covid_data", "cases deaths recovered") def a__ ( __lowercase = "https://www.worldometers.info/coronavirus/" ) -> covid_data: _A = "//div[@class = \"maincounter-number\"]/span/text()" return covid_data(*html.fromstring(requests.get(__lowercase ).content ).xpath(__lowercase ) ) a_ = "Total COVID-19 cases in the world: {}\nTotal deaths due to COVID-19 in the world: {}\nTotal COVID-19 patients recovered in the world: {}" print(fmt.format(*covid_stats()))
163
1
from ...configuration_utils import PretrainedConfig from ...utils import logging UpperCAmelCase_ : str = logging.get_logger(__name__) UpperCAmelCase_ : List[str] = { 'vinvino02/glpn-kitti': 'https://huggingface.co/vinvino02/glpn-kitti/resolve/main/config.json', # See all GLPN models at https://huggingface.co/models?filter=glpn } class SCREAMING_SNAKE_CASE__ ( lowercase__ ): snake_case__ : Dict = '''glpn''' def __init__( self : Tuple , SCREAMING_SNAKE_CASE__ : Dict=3 , SCREAMING_SNAKE_CASE__ : int=4 , SCREAMING_SNAKE_CASE__ : int=[2, 2, 2, 2] , SCREAMING_SNAKE_CASE__ : str=[8, 4, 2, 1] , SCREAMING_SNAKE_CASE__ : List[Any]=[3_2, 6_4, 1_6_0, 2_5_6] , SCREAMING_SNAKE_CASE__ : Optional[Any]=[7, 3, 3, 3] , SCREAMING_SNAKE_CASE__ : List[str]=[4, 2, 2, 2] , SCREAMING_SNAKE_CASE__ : Dict=[1, 2, 5, 8] , SCREAMING_SNAKE_CASE__ : List[str]=[4, 4, 4, 4] , SCREAMING_SNAKE_CASE__ : Union[str, Any]="gelu" , SCREAMING_SNAKE_CASE__ : int=0.0 , SCREAMING_SNAKE_CASE__ : List[str]=0.0 , SCREAMING_SNAKE_CASE__ : Tuple=0.02 , SCREAMING_SNAKE_CASE__ : str=0.1 , SCREAMING_SNAKE_CASE__ : str=1E-6 , SCREAMING_SNAKE_CASE__ : Optional[int]=6_4 , SCREAMING_SNAKE_CASE__ : str=1_0 , SCREAMING_SNAKE_CASE__ : Tuple=-1 , **SCREAMING_SNAKE_CASE__ : Union[str, Any] , ) -> int: super().__init__(**SCREAMING_SNAKE_CASE__ ) a_ : str = num_channels a_ : Tuple = num_encoder_blocks a_ : Union[str, Any] = depths a_ : Any = sr_ratios a_ : Optional[Any] = hidden_sizes a_ : Union[str, Any] = patch_sizes a_ : List[str] = strides a_ : List[Any] = mlp_ratios a_ : Optional[int] = num_attention_heads a_ : Optional[Any] = hidden_act a_ : List[Any] = hidden_dropout_prob a_ : Tuple = attention_probs_dropout_prob a_ : str = initializer_range a_ : Tuple = drop_path_rate a_ : Dict = layer_norm_eps a_ : Dict = decoder_hidden_size a_ : int = max_depth a_ : Union[str, Any] = head_in_index
32
"""simple docstring""" # Usage: # ./gen-card-facebook-wmt19.py import os from pathlib import Path def lowercase (SCREAMING_SNAKE_CASE_ : Tuple , SCREAMING_SNAKE_CASE_ : List[str] , SCREAMING_SNAKE_CASE_ : int ) -> List[str]: SCREAMING_SNAKE_CASE = { 'en': 'Machine learning is great, isn\'t it?', 'ru': 'Машинное обучение - это здорово, не так ли?', 'de': 'Maschinelles Lernen ist großartig, oder?', } # BLUE scores as follows: # "pair": [fairseq, transformers] SCREAMING_SNAKE_CASE = { 'ru-en': ['[41.3](http://matrix.statmt.org/matrix/output/1907?run_id=6937)', '39.20'], 'en-ru': ['[36.4](http://matrix.statmt.org/matrix/output/1914?run_id=6724)', '33.47'], 'en-de': ['[43.1](http://matrix.statmt.org/matrix/output/1909?run_id=6862)', '42.83'], 'de-en': ['[42.3](http://matrix.statmt.org/matrix/output/1902?run_id=6750)', '41.35'], } SCREAMING_SNAKE_CASE = F'{src_lang}-{tgt_lang}' SCREAMING_SNAKE_CASE = F'\n---\nlanguage: \n- {src_lang}\n- {tgt_lang}\nthumbnail:\ntags:\n- translation\n- wmt19\n- facebook\nlicense: apache-2.0\ndatasets:\n- wmt19\nmetrics:\n- bleu\n---\n\n# FSMT\n\n## Model description\n\nThis is a ported version of [fairseq wmt19 transformer](https://github.com/pytorch/fairseq/blob/master/examples/wmt19/README.md) for {src_lang}-{tgt_lang}.\n\nFor more details, please see, [Facebook FAIR\'s WMT19 News Translation Task Submission](https://arxiv.org/abs/1907.06616).\n\nThe abbreviation FSMT stands for FairSeqMachineTranslation\n\nAll four models are available:\n\n* [wmt19-en-ru](https://huggingface.co/facebook/wmt19-en-ru)\n* [wmt19-ru-en](https://huggingface.co/facebook/wmt19-ru-en)\n* [wmt19-en-de](https://huggingface.co/facebook/wmt19-en-de)\n* [wmt19-de-en](https://huggingface.co/facebook/wmt19-de-en)\n\n## Intended uses & limitations\n\n#### How to use\n\n```python\nfrom transformers import FSMTForConditionalGeneration, FSMTTokenizer\nmname = "facebook/wmt19-{src_lang}-{tgt_lang}"\ntokenizer = FSMTTokenizer.from_pretrained(mname)\nmodel = FSMTForConditionalGeneration.from_pretrained(mname)\n\ninput = "{texts[src_lang]}"\ninput_ids = tokenizer.encode(input, return_tensors="pt")\noutputs = model.generate(input_ids)\ndecoded = tokenizer.decode(outputs[0], skip_special_tokens=True)\nprint(decoded) # {texts[tgt_lang]}\n\n```\n\n#### Limitations and bias\n\n- The original (and this ported model) doesn\'t seem to handle well inputs with repeated sub-phrases, [content gets truncated](https://discuss.huggingface.co/t/issues-with-translating-inputs-containing-repeated-phrases/981)\n\n## Training data\n\nPretrained weights were left identical to the original model released by fairseq. For more details, please, see the [paper](https://arxiv.org/abs/1907.06616).\n\n## Eval results\n\npair | fairseq | transformers\n-------|---------|----------\n{pair} | {scores[pair][0]} | {scores[pair][1]}\n\nThe score is slightly below the score reported by `fairseq`, since `transformers`` currently doesn\'t support:\n- model ensemble, therefore the best performing checkpoint was ported (``model4.pt``).\n- re-ranking\n\nThe score was calculated using this code:\n\n```bash\ngit clone https://github.com/huggingface/transformers\ncd transformers\nexport PAIR={pair}\nexport DATA_DIR=data/$PAIR\nexport SAVE_DIR=data/$PAIR\nexport BS=8\nexport NUM_BEAMS=15\nmkdir -p $DATA_DIR\nsacrebleu -t wmt19 -l $PAIR --echo src > $DATA_DIR/val.source\nsacrebleu -t wmt19 -l $PAIR --echo ref > $DATA_DIR/val.target\necho $PAIR\nPYTHONPATH="src:examples/seq2seq" python examples/seq2seq/run_eval.py facebook/wmt19-$PAIR $DATA_DIR/val.source $SAVE_DIR/test_translations.txt --reference_path $DATA_DIR/val.target --score_path $SAVE_DIR/test_bleu.json --bs $BS --task translation --num_beams $NUM_BEAMS\n```\nnote: fairseq reports using a beam of 50, so you should get a slightly higher score if re-run with `--num_beams 50`.\n\n## Data Sources\n\n- [training, etc.](http://www.statmt.org/wmt19/)\n- [test set](http://matrix.statmt.org/test_sets/newstest2019.tgz?1556572561)\n\n\n### BibTeX entry and citation info\n\n```bibtex\n@inproceedings{{...,\n year={{2020}},\n title={{Facebook FAIR\'s WMT19 News Translation Task Submission}},\n author={{Ng, Nathan and Yee, Kyra and Baevski, Alexei and Ott, Myle and Auli, Michael and Edunov, Sergey}},\n booktitle={{Proc. of WMT}},\n}}\n```\n\n\n## TODO\n\n- port model ensemble (fairseq uses 4 model checkpoints)\n\n' os.makedirs(SCREAMING_SNAKE_CASE_ , exist_ok=SCREAMING_SNAKE_CASE_ ) SCREAMING_SNAKE_CASE = os.path.join(SCREAMING_SNAKE_CASE_ , 'README.md' ) print(F'Generating {path}' ) with open(SCREAMING_SNAKE_CASE_ , 'w' , encoding='utf-8' ) as f: f.write(SCREAMING_SNAKE_CASE_ ) # make sure we are under the root of the project __UpperCamelCase = Path(__file__).resolve().parent.parent.parent __UpperCamelCase = repo_dir / '''model_cards''' for model_name in ["wmt19-ru-en", "wmt19-en-ru", "wmt19-en-de", "wmt19-de-en"]: __UpperCamelCase,__UpperCamelCase,__UpperCamelCase = model_name.split('''-''') __UpperCamelCase = model_cards_dir / '''facebook''' / model_name write_model_card(model_card_dir, src_lang=src_lang, tgt_lang=tgt_lang)
113
0
import math import sys def SCREAMING_SNAKE_CASE ( __UpperCamelCase) -> int: if number != int(__UpperCamelCase): raise ValueError("the value of input must be a natural number") if number < 0: raise ValueError("the value of input must not be a negative number") if number == 0: return 1 a = [-1] * (number + 1) a = 0 for i in range(1 , number + 1): a = sys.maxsize a = int(math.sqrt(__UpperCamelCase)) for j in range(1 , root + 1): a = 1 + answers[i - (j**2)] a = min(__UpperCamelCase , __UpperCamelCase) a = answer return answers[number] if __name__ == "__main__": import doctest doctest.testmod()
350
import os from shutil import copyfile from typing import Any, Dict, List, Optional, Tuple import sentencepiece as spm from ...tokenization_utils import PreTrainedTokenizer from ...utils import logging lowercase__ : int = logging.get_logger(__name__) lowercase__ : Dict = "▁" lowercase__ : Union[str, Any] = {"vocab_file": "spiece.model"} lowercase__ : Union[str, Any] = { "vocab_file": { "google/reformer-crime-and-punishment": ( "https://huggingface.co/google/reformer-crime-and-punishment/resolve/main/spiece.model" ) } } lowercase__ : Tuple = { "google/reformer-crime-and-punishment": 524_288, } class a__ ( UpperCamelCase__ ): a : List[Any] = VOCAB_FILES_NAMES a : List[Any] = PRETRAINED_VOCAB_FILES_MAP a : Any = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES a : Dict = ["""input_ids""", """attention_mask"""] def __init__( self , A , A="</s>" , A="<unk>" , A=[] , A = None , **A , ) -> None: '''simple docstring''' a = {} if sp_model_kwargs is None else sp_model_kwargs super().__init__( eos_token=A , unk_token=A , additional_special_tokens=A , sp_model_kwargs=self.sp_model_kwargs , **A , ) a = vocab_file a = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(A ) @property def lowerCAmelCase_ ( self ) -> Optional[Any]: '''simple docstring''' return self.sp_model.get_piece_size() def lowerCAmelCase_ ( self ) -> Dict[str, int]: '''simple docstring''' a = {self.convert_ids_to_tokens(A ): i for i in range(self.vocab_size )} vocab.update(self.added_tokens_encoder ) return vocab def __getstate__( self ) -> Optional[int]: '''simple docstring''' a = self.__dict__.copy() a = None return state def __setstate__( self , A ) -> Union[str, Any]: '''simple docstring''' a = d # for backward compatibility if not hasattr(self , "sp_model_kwargs" ): a = {} a = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(self.vocab_file ) def lowerCAmelCase_ ( self , A ) -> List[str]: '''simple docstring''' return self.sp_model.encode(A , out_type=A ) def lowerCAmelCase_ ( self , A ) -> Union[str, Any]: '''simple docstring''' return self.sp_model.piece_to_id(A ) def lowerCAmelCase_ ( self , A ) -> Optional[int]: '''simple docstring''' if index < self.sp_model.get_piece_size(): a = self.sp_model.IdToPiece(A ) return token def lowerCAmelCase_ ( self , A ) -> Union[str, Any]: '''simple docstring''' a = [] a = "" for token in tokens: # make sure that special tokens are not decoded using sentencepiece model if token in self.all_special_tokens: out_string += self.sp_model.decode(A ) + token a = [] else: current_sub_tokens.append(A ) out_string += self.sp_model.decode(A ) return out_string.strip() def lowerCAmelCase_ ( self , A , A = None ) -> Tuple[str]: '''simple docstring''' if not os.path.isdir(A ): logger.error(F'''Vocabulary path ({save_directory}) should be a directory''' ) return a = os.path.join( A , (filename_prefix + "-" if filename_prefix else "") + VOCAB_FILES_NAMES["vocab_file"] ) if os.path.abspath(self.vocab_file ) != os.path.abspath(A ) and os.path.isfile(self.vocab_file ): copyfile(self.vocab_file , A ) elif not os.path.isfile(self.vocab_file ): with open(A , "wb" ) as fi: a = self.sp_model.serialized_model_proto() fi.write(A ) return (out_vocab_file,)
180
0
"""simple docstring""" from __future__ import annotations def UpperCamelCase ( _lowerCAmelCase : int, _lowerCAmelCase : int ) -> list[list[int]]: _UpperCAmelCase : list[list[int]] = [] create_all_state(1, _lowerCAmelCase, _lowerCAmelCase, [], _lowerCAmelCase ) return result def UpperCamelCase ( _lowerCAmelCase : int, _lowerCAmelCase : int, _lowerCAmelCase : int, _lowerCAmelCase : list[int], _lowerCAmelCase : list[list[int]], ) -> None: if level == 0: total_list.append(current_list[:] ) return for i in range(_lowerCAmelCase, total_number - level + 2 ): current_list.append(_lowerCAmelCase ) create_all_state(i + 1, _lowerCAmelCase, level - 1, _lowerCAmelCase, _lowerCAmelCase ) current_list.pop() def UpperCamelCase ( _lowerCAmelCase : list[list[int]] ) -> None: for i in total_list: print(*_lowerCAmelCase ) if __name__ == "__main__": lowerCamelCase__ : List[str] = 4 lowerCamelCase__ : Optional[int] = 2 lowerCamelCase__ : int = generate_all_combinations(n, k) print_all_state(total_list)
246
"""simple docstring""" import os import sys import tempfile import torch from .state import AcceleratorState from .utils import PrecisionType, PrepareForLaunch, is_mps_available, patch_environment def UpperCamelCase ( _lowerCAmelCase : Dict, _lowerCAmelCase : int=(), _lowerCAmelCase : Union[str, Any]=None, _lowerCAmelCase : Union[str, Any]="no", _lowerCAmelCase : Optional[int]="29500" ) -> Any: _UpperCAmelCase : Any = False _UpperCAmelCase : Dict = False if any(key.startswith("""KAGGLE""" ) for key in os.environ.keys() ): _UpperCAmelCase : Union[str, Any] = True elif "IPython" in sys.modules: _UpperCAmelCase : Dict = """google.colab""" in str(sys.modules["""IPython"""].get_ipython() ) try: _UpperCAmelCase : int = PrecisionType(mixed_precision.lower() ) except ValueError: raise ValueError( f'''Unknown mixed_precision mode: {args.mixed_precision.lower()}. Choose between {PrecisionType.list()}.''' ) if (in_colab or in_kaggle) and (os.environ.get("""TPU_NAME""", _lowerCAmelCase ) is not None): # TPU launch import torch_xla.distributed.xla_multiprocessing as xmp if len(AcceleratorState._shared_state ) > 0: raise ValueError( """To train on TPU in Colab or Kaggle Kernel, the `Accelerator` should only be initialized inside """ """your training function. Restart your notebook and make sure no cells initializes an """ """`Accelerator`.""" ) if num_processes is None: _UpperCAmelCase : List[Any] = 8 _UpperCAmelCase : int = PrepareForLaunch(_lowerCAmelCase, distributed_type="""TPU""" ) print(f'''Launching a training on {num_processes} TPU cores.''' ) xmp.spawn(_lowerCAmelCase, args=_lowerCAmelCase, nprocs=_lowerCAmelCase, start_method="""fork""" ) elif in_colab: # No need for a distributed launch otherwise as it's either CPU or one GPU. if torch.cuda.is_available(): print("""Launching training on one GPU.""" ) else: print("""Launching training on one CPU.""" ) function(*_lowerCAmelCase ) else: if num_processes is None: raise ValueError( """You have to specify the number of GPUs you would like to use, add `num_processes=...` to your call.""" ) if num_processes > 1: # Multi-GPU launch from torch.multiprocessing import start_processes from torch.multiprocessing.spawn import ProcessRaisedException if len(AcceleratorState._shared_state ) > 0: raise ValueError( """To launch a multi-GPU training from your notebook, the `Accelerator` should only be initialized """ """inside your training function. Restart your notebook and make sure no cells initializes an """ """`Accelerator`.""" ) if torch.cuda.is_initialized(): raise ValueError( """To launch a multi-GPU training from your notebook, you need to avoid running any instruction """ """using `torch.cuda` in any cell. Restart your notebook and make sure no cells use any CUDA """ """function.""" ) # torch.distributed will expect a few environment variable to be here. We set the ones common to each # process here (the other ones will be set be the launcher). with patch_environment( world_size=_lowerCAmelCase, master_addr="""127.0.01""", master_port=_lowerCAmelCase, mixed_precision=_lowerCAmelCase ): _UpperCAmelCase : Any = PrepareForLaunch(_lowerCAmelCase, distributed_type="""MULTI_GPU""" ) print(f'''Launching training on {num_processes} GPUs.''' ) try: start_processes(_lowerCAmelCase, args=_lowerCAmelCase, nprocs=_lowerCAmelCase, start_method="""fork""" ) except ProcessRaisedException as e: if "Cannot re-initialize CUDA in forked subprocess" in e.args[0]: raise RuntimeError( """CUDA has been initialized before the `notebook_launcher` could create a forked subprocess. """ """This likely stems from an outside import causing issues once the `notebook_launcher()` is called. """ """Please review your imports and test them when running the `notebook_launcher()` to identify """ """which one is problematic.""" ) from e else: # No need for a distributed launch otherwise as it's either CPU, GPU or MPS. if is_mps_available(): _UpperCAmelCase : Union[str, Any] = """1""" print("""Launching training on MPS.""" ) elif torch.cuda.is_available(): print("""Launching training on one GPU.""" ) else: print("""Launching training on CPU.""" ) function(*_lowerCAmelCase ) def UpperCamelCase ( _lowerCAmelCase : Optional[Any], _lowerCAmelCase : List[str]=(), _lowerCAmelCase : Optional[int]=2 ) -> Tuple: from torch.multiprocessing import start_processes with tempfile.NamedTemporaryFile() as tmp_file: # torch.distributed will expect a few environment variable to be here. We set the ones common to each # process here (the other ones will be set be the launcher). with patch_environment( world_size=_lowerCAmelCase, master_addr="""127.0.01""", master_port="""29500""", accelerate_mixed_precision="""no""", accelerate_debug_rdv_file=tmp_file.name, accelerate_use_cpu="""yes""", ): _UpperCAmelCase : Tuple = PrepareForLaunch(_lowerCAmelCase, debug=_lowerCAmelCase ) start_processes(_lowerCAmelCase, args=_lowerCAmelCase, nprocs=_lowerCAmelCase, start_method="""fork""" )
246
1
'''simple docstring''' _lowercase = """ # 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 """ _lowercase = [{"""type""": """code""", """content""": INSTALL_CONTENT}] _lowercase = { """{processor_class}""": """FakeProcessorClass""", """{model_class}""": """FakeModelClass""", """{object_class}""": """FakeObjectClass""", }
353
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_tf_available, is_torch_available, is_vision_available, ) _lowercase = {"""configuration_deit""": ["""DEIT_PRETRAINED_CONFIG_ARCHIVE_MAP""", """DeiTConfig""", """DeiTOnnxConfig"""]} try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _lowercase = ["""DeiTFeatureExtractor"""] _lowercase = ["""DeiTImageProcessor"""] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _lowercase = [ """DEIT_PRETRAINED_MODEL_ARCHIVE_LIST""", """DeiTForImageClassification""", """DeiTForImageClassificationWithTeacher""", """DeiTForMaskedImageModeling""", """DeiTModel""", """DeiTPreTrainedModel""", ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _lowercase = [ """TF_DEIT_PRETRAINED_MODEL_ARCHIVE_LIST""", """TFDeiTForImageClassification""", """TFDeiTForImageClassificationWithTeacher""", """TFDeiTForMaskedImageModeling""", """TFDeiTModel""", """TFDeiTPreTrainedModel""", ] if TYPE_CHECKING: from .configuration_deit import DEIT_PRETRAINED_CONFIG_ARCHIVE_MAP, DeiTConfig, DeiTOnnxConfig try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .feature_extraction_deit import DeiTFeatureExtractor from .image_processing_deit import DeiTImageProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_deit import ( DEIT_PRETRAINED_MODEL_ARCHIVE_LIST, DeiTForImageClassification, DeiTForImageClassificationWithTeacher, DeiTForMaskedImageModeling, DeiTModel, DeiTPreTrainedModel, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_deit import ( TF_DEIT_PRETRAINED_MODEL_ARCHIVE_LIST, TFDeiTForImageClassification, TFDeiTForImageClassificationWithTeacher, TFDeiTForMaskedImageModeling, TFDeiTModel, TFDeiTPreTrainedModel, ) else: import sys _lowercase = _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)
229
0
'''simple docstring''' import argparse import torch from torch import nn from transformers import MBartConfig, MBartForConditionalGeneration def __lowerCAmelCase ( UpperCamelCase__ ) -> Optional[int]: __lowerCamelCase = [ '''encoder.version''', '''decoder.version''', '''model.encoder.version''', '''model.decoder.version''', '''_float_tensor''', '''decoder.output_projection.weight''', ] for k in ignore_keys: state_dict.pop(UpperCamelCase__ , UpperCamelCase__ ) def __lowerCAmelCase ( UpperCamelCase__ ) -> Dict: __lowerCamelCase , __lowerCamelCase = emb.weight.shape __lowerCamelCase = nn.Linear(UpperCamelCase__ , UpperCamelCase__ , bias=UpperCamelCase__ ) __lowerCamelCase = emb.weight.data return lin_layer def __lowerCAmelCase ( UpperCamelCase__ , UpperCamelCase__="facebook/mbart-large-en-ro" , UpperCamelCase__=False , UpperCamelCase__=False ) -> List[str]: __lowerCamelCase = torch.load(UpperCamelCase__ , map_location='''cpu''' )['''model'''] remove_ignore_keys_(UpperCamelCase__ ) __lowerCamelCase = state_dict['''encoder.embed_tokens.weight'''].shape[0] __lowerCamelCase = MBartConfig.from_pretrained(UpperCamelCase__ , vocab_size=UpperCamelCase__ ) if mbart_aa and finetuned: __lowerCamelCase = '''relu''' __lowerCamelCase = state_dict['''decoder.embed_tokens.weight'''] __lowerCamelCase = MBartForConditionalGeneration(UpperCamelCase__ ) model.model.load_state_dict(UpperCamelCase__ ) if finetuned: __lowerCamelCase = make_linear_from_emb(model.model.shared ) return model if __name__ == "__main__": __UpperCAmelCase =argparse.ArgumentParser() # Required parameters parser.add_argument( "fairseq_path", type=str, help="bart.large, bart.large.cnn or a path to a model.pt on local filesystem." ) parser.add_argument("pytorch_dump_folder_path", default=None, type=str, help="Path to the output PyTorch model.") parser.add_argument( "--hf_config", default="facebook/mbart-large-cc25", type=str, help="Which huggingface architecture to use: mbart-large", ) parser.add_argument("--mbart_50", action="store_true", help="whether the model is mMART-50 checkpoint") parser.add_argument("--finetuned", action="store_true", help="whether the model is a fine-tuned checkpoint") __UpperCAmelCase =parser.parse_args() __UpperCAmelCase =convert_fairseq_mbart_checkpoint_from_disk( args.fairseq_path, hf_config_path=args.hf_config, finetuned=args.finetuned, mbart_aa=args.mbart_aa ) model.save_pretrained(args.pytorch_dump_folder_path)
67
from maths.prime_check import is_prime def UpperCAmelCase_( a__ ): """simple docstring""" if not isinstance(a__ , a__ ): SCREAMING_SNAKE_CASE : List[Any] = F"""Input value of [number={number}] must be an integer""" raise TypeError(a__ ) if is_prime(a__ ) and is_prime(number + 2 ): return number + 2 else: return -1 if __name__ == "__main__": import doctest doctest.testmod()
313
0
from __future__ import annotations import time from math import sqrt # 1 for manhattan, 0 for euclidean A_ :List[Any] = 0 A_ :Union[str, Any] = [ [0, 0, 0, 0, 0, 0, 0], [0, 1, 0, 0, 0, 0, 0], # 0 are free path whereas 1's are obstacles [0, 0, 0, 0, 0, 0, 0], [0, 0, 1, 0, 0, 0, 0], [1, 0, 1, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 1, 0, 0], ] A_ :Dict = [[-1, 0], [0, -1], [1, 0], [0, 1]] # up, left, down, right A_ :List[str] = tuple[int, int] class __A : """simple docstring""" def __init__( self , lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ , ): """simple docstring""" __UpperCamelCase : Union[str, Any] =pos_x __UpperCamelCase : int =pos_y __UpperCamelCase : Any =(pos_y, pos_x) __UpperCamelCase : Tuple =goal_x __UpperCamelCase : Optional[int] =goal_y __UpperCamelCase : Dict =g_cost __UpperCamelCase : str =parent __UpperCamelCase : Dict =self.calculate_heuristic() __UpperCamelCase : Optional[int] =self.g_cost + self.h_cost def __lowercase ( self ): """simple docstring""" __UpperCamelCase : Optional[Any] =self.pos_x - self.goal_x __UpperCamelCase : int =self.pos_y - self.goal_y if HEURISTIC == 1: return abs(UpperCAmelCase_ ) + abs(UpperCAmelCase_ ) else: return sqrt(dy**2 + dx**2 ) def __lt__( self , lowerCamelCase__ ): """simple docstring""" return self.f_cost < other.f_cost class __A : """simple docstring""" def __init__( self , lowerCamelCase__ , lowerCamelCase__ ): """simple docstring""" __UpperCamelCase : List[str] =Node(start[1] , start[0] , goal[1] , goal[0] , 0 , UpperCAmelCase_ ) __UpperCamelCase : Any =Node(goal[1] , goal[0] , goal[1] , goal[0] , 99999 , UpperCAmelCase_ ) __UpperCamelCase : str =[self.start] __UpperCamelCase : list[Node] =[] __UpperCamelCase : List[str] =False def __lowercase ( self ): """simple docstring""" while self.open_nodes: # Open Nodes are sorted using __lt__ self.open_nodes.sort() __UpperCamelCase : List[Any] =self.open_nodes.pop(0 ) if current_node.pos == self.target.pos: return self.retrace_path(UpperCAmelCase_ ) self.closed_nodes.append(UpperCAmelCase_ ) __UpperCamelCase : List[Any] =self.get_successors(UpperCAmelCase_ ) for child_node in successors: if child_node in self.closed_nodes: continue if child_node not in self.open_nodes: self.open_nodes.append(UpperCAmelCase_ ) else: # retrieve the best current path __UpperCamelCase : List[str] =self.open_nodes.pop(self.open_nodes.index(UpperCAmelCase_ ) ) if child_node.g_cost < better_node.g_cost: self.open_nodes.append(UpperCAmelCase_ ) else: self.open_nodes.append(UpperCAmelCase_ ) return [self.start.pos] def __lowercase ( self , lowerCamelCase__ ): """simple docstring""" __UpperCamelCase : int =[] for action in delta: __UpperCamelCase : Any =parent.pos_x + action[1] __UpperCamelCase : Dict =parent.pos_y + action[0] if not (0 <= pos_x <= len(grid[0] ) - 1 and 0 <= pos_y <= len(UpperCAmelCase_ ) - 1): continue if grid[pos_y][pos_x] != 0: continue successors.append( Node( UpperCAmelCase_ , UpperCAmelCase_ , self.target.pos_y , self.target.pos_x , parent.g_cost + 1 , UpperCAmelCase_ , ) ) return successors def __lowercase ( self , lowerCamelCase__ ): """simple docstring""" __UpperCamelCase : List[str] =node __UpperCamelCase : Dict =[] while current_node is not None: path.append((current_node.pos_y, current_node.pos_x) ) __UpperCamelCase : int =current_node.parent path.reverse() return path class __A : """simple docstring""" def __init__( self , lowerCamelCase__ , lowerCamelCase__ ): """simple docstring""" __UpperCamelCase : Dict =AStar(UpperCAmelCase_ , UpperCAmelCase_ ) __UpperCamelCase : str =AStar(UpperCAmelCase_ , UpperCAmelCase_ ) __UpperCamelCase : int =False def __lowercase ( self ): """simple docstring""" while self.fwd_astar.open_nodes or self.bwd_astar.open_nodes: self.fwd_astar.open_nodes.sort() self.bwd_astar.open_nodes.sort() __UpperCamelCase : List[str] =self.fwd_astar.open_nodes.pop(0 ) __UpperCamelCase : str =self.bwd_astar.open_nodes.pop(0 ) if current_bwd_node.pos == current_fwd_node.pos: return self.retrace_bidirectional_path( UpperCAmelCase_ , UpperCAmelCase_ ) self.fwd_astar.closed_nodes.append(UpperCAmelCase_ ) self.bwd_astar.closed_nodes.append(UpperCAmelCase_ ) __UpperCamelCase : Any =current_bwd_node __UpperCamelCase : Tuple =current_fwd_node __UpperCamelCase : List[str] ={ self.fwd_astar: self.fwd_astar.get_successors(UpperCAmelCase_ ), self.bwd_astar: self.bwd_astar.get_successors(UpperCAmelCase_ ), } for astar in [self.fwd_astar, self.bwd_astar]: for child_node in successors[astar]: if child_node in astar.closed_nodes: continue if child_node not in astar.open_nodes: astar.open_nodes.append(UpperCAmelCase_ ) else: # retrieve the best current path __UpperCamelCase : Optional[Any] =astar.open_nodes.pop( astar.open_nodes.index(UpperCAmelCase_ ) ) if child_node.g_cost < better_node.g_cost: astar.open_nodes.append(UpperCAmelCase_ ) else: astar.open_nodes.append(UpperCAmelCase_ ) return [self.fwd_astar.start.pos] def __lowercase ( self , lowerCamelCase__ , lowerCamelCase__ ): """simple docstring""" __UpperCamelCase : Optional[Any] =self.fwd_astar.retrace_path(UpperCAmelCase_ ) __UpperCamelCase : Tuple =self.bwd_astar.retrace_path(UpperCAmelCase_ ) bwd_path.pop() bwd_path.reverse() __UpperCamelCase : Optional[Any] =fwd_path + bwd_path return path if __name__ == "__main__": # all coordinates are given in format [y,x] A_ :Tuple = (0, 0) A_ :Union[str, Any] = (len(grid) - 1, len(grid[0]) - 1) for elem in grid: print(elem) A_ :Tuple = time.time() A_ :List[Any] = AStar(init, goal) A_ :int = a_star.search() A_ :List[Any] = time.time() - start_time print(f"AStar execution time = {end_time:f} seconds") A_ :List[str] = time.time() A_ :Tuple = BidirectionalAStar(init, goal) A_ :List[str] = time.time() - bd_start_time print(f"BidirectionalAStar execution time = {bd_end_time:f} seconds")
365
from math import pow, sqrt def A ( *a_ ) -> bool: __UpperCamelCase : Union[str, Any] =len(a_ ) > 0 and all(value > 0.0 for value in values ) return result def A ( a_ ,a_ ) -> float | ValueError: return ( round(sqrt(molar_mass_a / molar_mass_a ) ,6 ) if validate(a_ ,a_ ) else ValueError('Input Error: Molar mass values must greater than 0.' ) ) def A ( a_ ,a_ ,a_ ) -> float | ValueError: return ( round(effusion_rate * sqrt(molar_mass_a / molar_mass_a ) ,6 ) if validate(a_ ,a_ ,a_ ) else ValueError( 'Input Error: Molar mass and effusion rate values must greater than 0.' ) ) def A ( a_ ,a_ ,a_ ) -> float | ValueError: return ( round(effusion_rate / sqrt(molar_mass_a / molar_mass_a ) ,6 ) if validate(a_ ,a_ ,a_ ) else ValueError( 'Input Error: Molar mass and effusion rate values must greater than 0.' ) ) def A ( a_ ,a_ ,a_ ) -> float | ValueError: return ( round(molar_mass / pow(effusion_rate_a / effusion_rate_a ,2 ) ,6 ) if validate(a_ ,a_ ,a_ ) else ValueError( 'Input Error: Molar mass and effusion rate values must greater than 0.' ) ) def A ( a_ ,a_ ,a_ ) -> float | ValueError: return ( round(pow(effusion_rate_a / effusion_rate_a ,2 ) / molar_mass ,6 ) if validate(a_ ,a_ ,a_ ) else ValueError( 'Input Error: Molar mass and effusion rate values must greater than 0.' ) )
245
0
"""simple docstring""" import argparse import json import os import torch from transformers.file_utils import has_file from diffusers import UNetaDConditionModel, UNetaDModel _UpperCamelCase: Dict = False _UpperCamelCase: str = True _UpperCamelCase: Optional[int] = False if __name__ == "__main__": _UpperCamelCase: Tuple = argparse.ArgumentParser() parser.add_argument( '--repo_path', default=None, type=str, required=True, help='The config json file corresponding to the architecture.', ) parser.add_argument('--dump_path', default=None, type=str, required=True, help='Path to the output model.') _UpperCamelCase: Optional[int] = parser.parse_args() _UpperCamelCase: Tuple = { 'image_size': 'sample_size', 'num_res_blocks': 'layers_per_block', 'block_channels': 'block_out_channels', 'down_blocks': 'down_block_types', 'up_blocks': 'up_block_types', 'downscale_freq_shift': 'freq_shift', 'resnet_num_groups': 'norm_num_groups', 'resnet_act_fn': 'act_fn', 'resnet_eps': 'norm_eps', 'num_head_channels': 'attention_head_dim', } _UpperCamelCase: Dict = { 'time_steps': 'time_proj', 'mid': 'mid_block', 'downsample_blocks': 'down_blocks', 'upsample_blocks': 'up_blocks', } _UpperCamelCase: List[str] = '' if has_file(args.repo_path, 'config.json') else 'unet' with open(os.path.join(args.repo_path, subfolder, 'config.json'), 'r', encoding='utf-8') as reader: _UpperCamelCase: str = reader.read() _UpperCamelCase: int = json.loads(text) if do_only_config: for key in config_parameters_to_change.keys(): config.pop(key, None) if has_file(args.repo_path, 'config.json'): _UpperCamelCase: List[Any] = UNetaDModel(**config) else: _UpperCamelCase: Any = UNetaDConditionModel if 'ldm-text2im-large-256' in args.repo_path else UNetaDModel _UpperCamelCase: List[Any] = class_name(**config) if do_only_config: model.save_config(os.path.join(args.repo_path, subfolder)) _UpperCamelCase: Tuple = dict(model.config) if do_only_renaming: for key, value in config_parameters_to_change.items(): if key in config: _UpperCamelCase: str = config[key] del config[key] _UpperCamelCase: Union[str, Any] = [k.replace('UNetRes', '') for k in config['down_block_types']] _UpperCamelCase: str = [k.replace('UNetRes', '') for k in config['up_block_types']] if do_only_weights: _UpperCamelCase: Any = torch.load(os.path.join(args.repo_path, subfolder, 'diffusion_pytorch_model.bin')) _UpperCamelCase: int = {} for param_key, param_value in state_dict.items(): if param_key.endswith('.op.bias') or param_key.endswith('.op.weight'): continue _UpperCamelCase: List[str] = False for key, new_key in key_parameters_to_change.items(): if not has_changed and param_key.split('.')[0] == key: _UpperCamelCase: int = param_value _UpperCamelCase: Tuple = True if not has_changed: _UpperCamelCase: List[str] = param_value model.load_state_dict(new_state_dict) model.save_pretrained(os.path.join(args.repo_path, subfolder))
255
"""simple docstring""" from scipy.stats import pearsonr import datasets _UpperCamelCase: str = '\nPearson correlation coefficient and p-value for testing non-correlation.\nThe Pearson correlation coefficient measures the linear relationship between two datasets. The calculation of the p-value relies on the assumption that each dataset is normally distributed. Like other correlation coefficients, this one varies between -1 and +1 with 0 implying no correlation. Correlations of -1 or +1 imply an exact linear relationship. Positive correlations imply that as x increases, so does y. Negative correlations imply that as x increases, y decreases.\nThe p-value roughly indicates the probability of an uncorrelated system producing datasets that have a Pearson correlation at least as extreme as the one computed from these datasets.\n' _UpperCamelCase: Tuple = '\nArgs:\n predictions (`list` of `int`): Predicted class labels, as returned by a model.\n references (`list` of `int`): Ground truth labels.\n return_pvalue (`boolean`): If `True`, returns the p-value, along with the correlation coefficient. If `False`, returns only the correlation coefficient. Defaults to `False`.\n\nReturns:\n pearsonr (`float`): Pearson correlation coefficient. Minimum possible value is -1. Maximum possible value is 1. Values of 1 and -1 indicate exact linear positive and negative relationships, respectively. A value of 0 implies no correlation.\n p-value (`float`): P-value, which roughly indicates the probability of an The p-value roughly indicates the probability of an uncorrelated system producing datasets that have a Pearson correlation at least as extreme as the one computed from these datasets. Minimum possible value is 0. Maximum possible value is 1. Higher values indicate higher probabilities.\n\nExamples:\n\n Example 1-A simple example using only predictions and references.\n >>> pearsonr_metric = datasets.load_metric("pearsonr")\n >>> results = pearsonr_metric.compute(predictions=[10, 9, 2.5, 6, 4], references=[1, 2, 3, 4, 5])\n >>> print(round(results[\'pearsonr\'], 2))\n -0.74\n\n Example 2-The same as Example 1, but that also returns the `p-value`.\n >>> pearsonr_metric = datasets.load_metric("pearsonr")\n >>> results = pearsonr_metric.compute(predictions=[10, 9, 2.5, 6, 4], references=[1, 2, 3, 4, 5], return_pvalue=True)\n >>> print(sorted(list(results.keys())))\n [\'p-value\', \'pearsonr\']\n >>> print(round(results[\'pearsonr\'], 2))\n -0.74\n >>> print(round(results[\'p-value\'], 2))\n 0.15\n' _UpperCamelCase: Any = '\n@article{2020SciPy-NMeth,\nauthor = {Virtanen, Pauli and Gommers, Ralf and Oliphant, Travis E. and\n Haberland, Matt and Reddy, Tyler and Cournapeau, David and\n Burovski, Evgeni and Peterson, Pearu and Weckesser, Warren and\n Bright, Jonathan and {van der Walt}, St{\'e}fan J. and\n Brett, Matthew and Wilson, Joshua and Millman, K. Jarrod and\n Mayorov, Nikolay and Nelson, Andrew R. J. and Jones, Eric and\n Kern, Robert and Larson, Eric and Carey, C J and\n Polat, Ilhan and Feng, Yu and Moore, Eric W. and\n {VanderPlas}, Jake and Laxalde, Denis and Perktold, Josef and\n Cimrman, Robert and Henriksen, Ian and Quintero, E. A. and\n Harris, Charles R. and Archibald, Anne M. and\n Ribeiro, Antonio H. and Pedregosa, Fabian and\n {van Mulbregt}, Paul and {SciPy 1.0 Contributors}},\ntitle = {{{SciPy} 1.0: Fundamental Algorithms for Scientific\n Computing in Python}},\njournal = {Nature Methods},\nyear = {2020},\nvolume = {17},\npages = {261--272},\nadsurl = {https://rdcu.be/b08Wh},\ndoi = {10.1038/s41592-019-0686-2},\n}\n' @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION, _KWARGS_DESCRIPTION ) class a__ ( datasets.Metric ): def lowercase ( self : Optional[Any] ) -> List[Any]: return datasets.MetricInfo( description=_DESCRIPTION, citation=_CITATION, inputs_description=_KWARGS_DESCRIPTION, features=datasets.Features( { 'predictions': datasets.Value('float' ), 'references': datasets.Value('float' ), } ), reference_urls=['https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.pearsonr.html'], ) def lowercase ( self : Tuple, lowerCAmelCase : Any, lowerCAmelCase : str, lowerCAmelCase : List[str]=False ) -> int: if return_pvalue: lowercase : Optional[int] = pearsonr(lowerCAmelCase, lowerCAmelCase ) return {"pearsonr": results[0], "p-value": results[1]} else: return {"pearsonr": float(pearsonr(lowerCAmelCase, lowerCAmelCase )[0] )}
255
1
'''simple docstring''' import torch from torch import nn from transformers import CLIPPreTrainedModel, CLIPVisionModel from ...models.attention import BasicTransformerBlock from ...utils import logging lowerCAmelCase: Any = logging.get_logger(__name__) # pylint: disable=invalid-name class a__( lowerCamelCase__ ): def __init__( self : str , __snake_case : int , __snake_case : Optional[Any]=7_68 ): super().__init__(__snake_case ) a : List[str] = proj_size a : Tuple = CLIPVisionModel(__snake_case ) a : Any = PaintByExampleMapper(__snake_case ) a : List[str] = nn.LayerNorm(config.hidden_size ) a : Union[str, Any] = nn.Linear(config.hidden_size , self.proj_size ) # uncondition for scaling a : Optional[Any] = nn.Parameter(torch.randn((1, 1, self.proj_size) ) ) def lowercase_ ( self : str , __snake_case : int , __snake_case : Optional[int]=False ): a : Optional[int] = self.model(pixel_values=__snake_case ) a : List[Any] = clip_output.pooler_output a : Dict = self.mapper(latent_states[:, None] ) a : List[Any] = self.final_layer_norm(__snake_case ) a : Dict = self.proj_out(__snake_case ) if return_uncond_vector: return latent_states, self.uncond_vector return latent_states class a__( nn.Module ): def __init__( self : Optional[int] , __snake_case : List[str] ): super().__init__() a : int = (config.num_hidden_layers + 1) // 5 a : Optional[int] = config.hidden_size a : Union[str, Any] = 1 a : Union[str, Any] = nn.ModuleList( [ BasicTransformerBlock(__snake_case , __snake_case , __snake_case , activation_fn='gelu' , attention_bias=__snake_case ) for _ in range(__snake_case ) ] ) def lowercase_ ( self : Optional[Any] , __snake_case : List[str] ): for block in self.blocks: a : str = block(__snake_case ) return hidden_states
96
'''simple docstring''' import argparse import os import re import packaging.version lowerCAmelCase: List[str] = 'examples/' lowerCAmelCase: List[Any] = { 'examples': (re.compile(r'^check_min_version\("[^"]+"\)\s*$', re.MULTILINE), 'check_min_version("VERSION")\n'), 'init': (re.compile(r'^__version__\s+=\s+"([^"]+)"\s*$', re.MULTILINE), '__version__ = "VERSION"\n'), 'setup': (re.compile(r'^(\s*)version\s*=\s*"[^"]+",', re.MULTILINE), r'\1version="VERSION",'), 'doc': (re.compile(r'^(\s*)release\s*=\s*"[^"]+"$', re.MULTILINE), 'release = "VERSION"\n'), } lowerCAmelCase: str = { 'init': 'src/transformers/__init__.py', 'setup': 'setup.py', } lowerCAmelCase: str = 'README.md' def lowerCamelCase__ ( _A , _A , _A ): with open(_A , 'r' , encoding='utf-8' , newline='\n' ) as f: a : Tuple = f.read() a , a : Tuple = REPLACE_PATTERNS[pattern] a : Dict = replace.replace('VERSION' , _A ) a : Dict = re_pattern.sub(_A , _A ) with open(_A , 'w' , encoding='utf-8' , newline='\n' ) as f: f.write(_A ) def lowerCamelCase__ ( _A ): for folder, directories, fnames in os.walk(_A ): # Removing some of the folders with non-actively maintained examples from the walk if "research_projects" in directories: directories.remove('research_projects' ) if "legacy" in directories: directories.remove('legacy' ) for fname in fnames: if fname.endswith('.py' ): update_version_in_file(os.path.join(_A , _A ) , _A , pattern='examples' ) def lowerCamelCase__ ( _A , _A=False ): for pattern, fname in REPLACE_FILES.items(): update_version_in_file(_A , _A , _A ) if not patch: update_version_in_examples(_A ) def lowerCamelCase__ ( ): a : Tuple = '🤗 Transformers currently provides the following architectures' a : Any = '1. Want to contribute a new model?' with open(_A , 'r' , encoding='utf-8' , newline='\n' ) as f: a : Tuple = f.readlines() # Find the start of the list. a : Optional[int] = 0 while not lines[start_index].startswith(_start_prompt ): start_index += 1 start_index += 1 a : Optional[int] = start_index # Update the lines in the model list. while not lines[index].startswith(_end_prompt ): if lines[index].startswith('1.' ): a : List[Any] = lines[index].replace( 'https://huggingface.co/docs/transformers/main/model_doc' , 'https://huggingface.co/docs/transformers/model_doc' , ) index += 1 with open(_A , 'w' , encoding='utf-8' , newline='\n' ) as f: f.writelines(_A ) def lowerCamelCase__ ( ): with open(REPLACE_FILES['init'] , 'r' ) as f: a : Union[str, Any] = f.read() a : Tuple = REPLACE_PATTERNS['init'][0].search(_A ).groups()[0] return packaging.version.parse(_A ) def lowerCamelCase__ ( _A=False ): a : int = get_version() if patch and default_version.is_devrelease: raise ValueError('Can\'t create a patch version from the dev branch, checkout a released version!' ) if default_version.is_devrelease: a : Any = default_version.base_version elif patch: a : Dict = f"""{default_version.major}.{default_version.minor}.{default_version.micro + 1}""" else: a : Union[str, Any] = f"""{default_version.major}.{default_version.minor + 1}.0""" # Now let's ask nicely if that's the right one. a : List[Any] = input(f"""Which version are you releasing? [{default_version}]""" ) if len(_A ) == 0: a : Union[str, Any] = default_version print(f"""Updating version to {version}.""" ) global_version_update(_A , patch=_A ) if not patch: print('Cleaning main README, don\'t forget to run `make fix-copies`.' ) clean_main_ref_in_model_list() def lowerCamelCase__ ( ): a : int = get_version() a : Any = f"""{current_version.major}.{current_version.minor + 1}.0.dev0""" a : int = current_version.base_version # Check with the user we got that right. a : Tuple = input(f"""Which version are we developing now? [{dev_version}]""" ) if len(_A ) == 0: a : Optional[int] = dev_version print(f"""Updating version to {version}.""" ) global_version_update(_A ) print('Cleaning main README, don\'t forget to run `make fix-copies`.' ) clean_main_ref_in_model_list() if __name__ == "__main__": lowerCAmelCase: Tuple = argparse.ArgumentParser() parser.add_argument('--post_release', action='store_true', help='Whether this is pre or post release.') parser.add_argument('--patch', action='store_true', help='Whether or not this is a patch release.') lowerCAmelCase: Optional[Any] = parser.parse_args() if not args.post_release: pre_release_work(patch=args.patch) elif args.patch: print('Nothing to do after a patch :-)') else: post_release_work()
96
1
"""simple docstring""" def lowercase__ ( _UpperCAmelCase , _UpperCAmelCase ) -> bool: '''simple docstring''' lowercase : Optional[int] = len(_UpperCAmelCase ) + 1 lowercase : Any = len(_UpperCAmelCase ) + 1 # dp is a 2d matrix where dp[i][j] denotes whether prefix string of # length i of input_string matches with prefix string of length j of # given pattern. # "dp" stands for dynamic programming. lowercase : Tuple = [[0 for i in range(_UpperCAmelCase )] for j in range(_UpperCAmelCase )] # since string of zero length match pattern of zero length lowercase : List[Any] = 1 # since pattern of zero length will never match with string of non-zero length for i in range(1 , _UpperCAmelCase ): lowercase : Tuple = 0 # since string of zero length will match with pattern where there # is at least one * alternatively for j in range(1 , _UpperCAmelCase ): lowercase : Tuple = dp[0][j - 2] if pattern[j - 1] == '*' else 0 # now using bottom-up approach to find for all remaining lengths for i in range(1 , _UpperCAmelCase ): for j in range(1 , _UpperCAmelCase ): if input_string[i - 1] == pattern[j - 1] or pattern[j - 1] == ".": lowercase : List[str] = dp[i - 1][j - 1] elif pattern[j - 1] == "*": if dp[i][j - 2] == 1: lowercase : Any = 1 elif pattern[j - 2] in (input_string[i - 1], "."): lowercase : List[Any] = dp[i - 1][j] else: lowercase : Optional[int] = 0 else: lowercase : Dict = 0 return bool(dp[-1][-1] ) if __name__ == "__main__": import doctest doctest.testmod() # inputing the strings # input_string = input("input a string :") # pattern = input("input a pattern :") _UpperCamelCase: int = 'aab' _UpperCamelCase: Tuple = 'c*a*b' # using function to check whether given string matches the given pattern if match_pattern(input_string, pattern): print(f'''{input_string} matches the given pattern {pattern}''') else: print(f'''{input_string} does not match with the given pattern {pattern}''')
255
"""simple docstring""" import pickle import unittest import torch from accelerate import Accelerator from accelerate.state import AcceleratorState from accelerate.test_utils import require_cpu @require_cpu class a__ ( unittest.TestCase ): def lowercase ( self : Optional[Any] ) -> Any: lowercase : int = torch.nn.Linear(10, 10 ) lowercase : Optional[int] = torch.optim.SGD(model.parameters(), 0.1 ) lowercase : List[Any] = Accelerator() lowercase : Optional[Any] = accelerator.prepare(lowerCAmelCase ) try: pickle.loads(pickle.dumps(lowerCAmelCase ) ) except Exception as e: self.fail(f'''Accelerated optimizer pickling failed with {e}''' ) AcceleratorState._reset_state()
255
1
import os import time from dataclasses import dataclass, field from enum import Enum from typing import Dict, List, Optional, Union import torch from filelock import FileLock from torch.utils.data import Dataset from ...models.auto.modeling_auto import MODEL_FOR_QUESTION_ANSWERING_MAPPING from ...tokenization_utils import PreTrainedTokenizer from ...utils import logging from ..processors.squad import SquadFeatures, SquadVaProcessor, SquadVaProcessor, squad_convert_examples_to_features _lowerCamelCase : Tuple = logging.get_logger(__name__) _lowerCamelCase : List[str] = list(MODEL_FOR_QUESTION_ANSWERING_MAPPING.keys()) _lowerCamelCase : Union[str, Any] = tuple(conf.model_type for conf in MODEL_CONFIG_CLASSES) @dataclass class SCREAMING_SNAKE_CASE__ : '''simple docstring''' _UpperCAmelCase : str = field( default=UpperCAmelCase ,metadata={"help": "Model type selected in the list: " + ", ".join(UpperCAmelCase )} ) _UpperCAmelCase : str = field( default=UpperCAmelCase ,metadata={"help": "The input data dir. Should contain the .json files for the SQuAD task."} ) _UpperCAmelCase : int = field( default=1_2_8 ,metadata={ "help": ( "The maximum total input sequence length after tokenization. Sequences longer " "than this will be truncated, sequences shorter will be padded." ) } ,) _UpperCAmelCase : int = field( default=1_2_8 ,metadata={"help": "When splitting up a long document into chunks, how much stride to take between chunks."} ,) _UpperCAmelCase : int = field( default=6_4 ,metadata={ "help": ( "The maximum number of tokens for the question. Questions longer than this will " "be truncated to this length." ) } ,) _UpperCAmelCase : int = field( default=3_0 ,metadata={ "help": ( "The maximum length of an answer that can be generated. This is needed because the start " "and end predictions are not conditioned on one another." ) } ,) _UpperCAmelCase : bool = field( default=UpperCAmelCase ,metadata={"help": "Overwrite the cached training and evaluation sets"} ) _UpperCAmelCase : bool = field( default=UpperCAmelCase ,metadata={"help": "If true, the SQuAD examples contain some that do not have an answer."} ) _UpperCAmelCase : float = field( default=0.0 ,metadata={"help": "If null_score - best_non_null is greater than the threshold predict null."} ) _UpperCAmelCase : int = field( default=2_0 ,metadata={"help": "If null_score - best_non_null is greater than the threshold predict null."} ) _UpperCAmelCase : int = field( default=0 ,metadata={ "help": ( "language id of input for language-specific xlm models (see" " tokenization_xlm.PRETRAINED_INIT_CONFIGURATION)" ) } ,) _UpperCAmelCase : int = field(default=1 ,metadata={"help": "multiple threads for converting example to features"} ) class SCREAMING_SNAKE_CASE__ ( UpperCAmelCase ): '''simple docstring''' _UpperCAmelCase : List[Any] = "train" _UpperCAmelCase : Any = "dev" class SCREAMING_SNAKE_CASE__ ( UpperCAmelCase ): '''simple docstring''' _UpperCAmelCase : SquadDataTrainingArguments _UpperCAmelCase : List[SquadFeatures] _UpperCAmelCase : Split _UpperCAmelCase : bool def __init__( self : List[Any] , lowercase : SquadDataTrainingArguments , lowercase : PreTrainedTokenizer , lowercase : Optional[int] = None , lowercase : Union[str, Split] = Split.train , lowercase : Optional[bool] = False , lowercase : Optional[str] = None , lowercase : Optional[str] = "pt" , ): '''simple docstring''' _snake_case = args _snake_case = is_language_sensitive _snake_case = SquadVaProcessor() if args.version_2_with_negative else SquadVaProcessor() if isinstance(lowercase , lowercase ): try: _snake_case = Split[mode] except KeyError: raise KeyError('mode is not a valid split name' ) _snake_case = mode # Load data features from cache or dataset file _snake_case = 'v2' if args.version_2_with_negative else 'v1' _snake_case = os.path.join( cache_dir if cache_dir is not None else args.data_dir , f'''cached_{mode.value}_{tokenizer.__class__.__name__}_{args.max_seq_length}_{version_tag}''' , ) # Make sure only the first process in distributed training processes the dataset, # and the others will use the cache. _snake_case = cached_features_file + '.lock' with FileLock(lowercase ): if os.path.exists(lowercase ) and not args.overwrite_cache: _snake_case = time.time() _snake_case = torch.load(lowercase ) # Legacy cache files have only features, while new cache files # will have dataset and examples also. _snake_case = self.old_features['features'] _snake_case = self.old_features.get('dataset' , lowercase ) _snake_case = self.old_features.get('examples' , lowercase ) logger.info( f'''Loading features from cached file {cached_features_file} [took %.3f s]''' , time.time() - start ) if self.dataset is None or self.examples is None: logger.warning( f'''Deleting cached file {cached_features_file} will allow dataset and examples to be cached in''' ' future run' ) else: if mode == Split.dev: _snake_case = self.processor.get_dev_examples(args.data_dir ) else: _snake_case = self.processor.get_train_examples(args.data_dir ) _snake_case , _snake_case = squad_convert_examples_to_features( examples=self.examples , tokenizer=lowercase , max_seq_length=args.max_seq_length , doc_stride=args.doc_stride , max_query_length=args.max_query_length , is_training=mode == Split.train , threads=args.threads , return_dataset=lowercase , ) _snake_case = time.time() torch.save( {'features': self.features, 'dataset': self.dataset, 'examples': self.examples} , lowercase , ) # ^ This seems to take a lot of time so I want to investigate why and how we can improve. logger.info( f'''Saving features into cached file {cached_features_file} [took {time.time() - start:.3f} s]''' ) def __len__( self : Optional[Any] ): '''simple docstring''' return len(self.features ) def __getitem__( self : Union[str, Any] , lowercase : Optional[int] ): '''simple docstring''' _snake_case = self.features[i] _snake_case = torch.tensor(feature.input_ids , dtype=torch.long ) _snake_case = torch.tensor(feature.attention_mask , dtype=torch.long ) _snake_case = torch.tensor(feature.token_type_ids , dtype=torch.long ) _snake_case = torch.tensor(feature.cls_index , dtype=torch.long ) _snake_case = torch.tensor(feature.p_mask , dtype=torch.float ) _snake_case = torch.tensor(feature.is_impossible , dtype=torch.float ) _snake_case = { 'input_ids': input_ids, 'attention_mask': attention_mask, 'token_type_ids': token_type_ids, } if self.args.model_type in ["xlm", "roberta", "distilbert", "camembert"]: del inputs["token_type_ids"] if self.args.model_type in ["xlnet", "xlm"]: inputs.update({'cls_index': cls_index, 'p_mask': p_mask} ) if self.args.version_2_with_negative: inputs.update({'is_impossible': is_impossible} ) if self.is_language_sensitive: inputs.update({'langs': (torch.ones(input_ids.shape , dtype=torch.intaa ) * self.args.lang_id)} ) if self.mode == Split.train: _snake_case = torch.tensor(feature.start_position , dtype=torch.long ) _snake_case = torch.tensor(feature.end_position , dtype=torch.long ) inputs.update({'start_positions': start_positions, 'end_positions': end_positions} ) return inputs
130
import argparse import logging import pickle import random import time import numpy as np from transformers import BertTokenizer, GPTaTokenizer, RobertaTokenizer logging.basicConfig( format='''%(asctime)s - %(levelname)s - %(name)s - %(message)s''', datefmt='''%m/%d/%Y %H:%M:%S''', level=logging.INFO ) _lowerCamelCase : Tuple = logging.getLogger(__name__) def a_ ( ) -> Any: _snake_case = argparse.ArgumentParser( description='Preprocess the data to avoid re-doing it several times by (tokenization + token_to_ids).' ) parser.add_argument('--file_path' , type=__lowercase , default='data/dump.txt' , help='The path to the data.' ) parser.add_argument('--tokenizer_type' , type=__lowercase , default='bert' , choices=['bert', 'roberta', 'gpt2'] ) parser.add_argument('--tokenizer_name' , type=__lowercase , default='bert-base-uncased' , help='The tokenizer to use.' ) parser.add_argument('--dump_file' , type=__lowercase , default='data/dump' , help='The dump file prefix.' ) _snake_case = parser.parse_args() logger.info(f'''Loading Tokenizer ({args.tokenizer_name})''' ) if args.tokenizer_type == "bert": _snake_case = BertTokenizer.from_pretrained(args.tokenizer_name ) _snake_case = tokenizer.special_tokens_map['cls_token'] # `[CLS]` _snake_case = tokenizer.special_tokens_map['sep_token'] # `[SEP]` elif args.tokenizer_type == "roberta": _snake_case = RobertaTokenizer.from_pretrained(args.tokenizer_name ) _snake_case = tokenizer.special_tokens_map['cls_token'] # `<s>` _snake_case = tokenizer.special_tokens_map['sep_token'] # `</s>` elif args.tokenizer_type == "gpt2": _snake_case = GPTaTokenizer.from_pretrained(args.tokenizer_name ) _snake_case = tokenizer.special_tokens_map['bos_token'] # `<|endoftext|>` _snake_case = tokenizer.special_tokens_map['eos_token'] # `<|endoftext|>` logger.info(f'''Loading text from {args.file_path}''' ) with open(args.file_path , 'r' , encoding='utf8' ) as fp: _snake_case = fp.readlines() logger.info('Start encoding' ) logger.info(f'''{len(__lowercase )} examples to process.''' ) _snake_case = [] _snake_case = 0 _snake_case = 10_000 _snake_case = time.time() for text in data: _snake_case = f'''{bos} {text.strip()} {sep}''' _snake_case = tokenizer.encode(__lowercase , add_special_tokens=__lowercase ) rslt.append(__lowercase ) iter += 1 if iter % interval == 0: _snake_case = time.time() logger.info(f'''{iter} examples processed. - {(end-start):.2f}s/{interval}expl''' ) _snake_case = time.time() logger.info('Finished binarization' ) logger.info(f'''{len(__lowercase )} examples processed.''' ) _snake_case = f'''{args.dump_file}.{args.tokenizer_name}.pickle''' _snake_case = tokenizer.vocab_size if vocab_size < (1 << 16): _snake_case = [np.uintaa(__lowercase ) for d in rslt] else: _snake_case = [np.intaa(__lowercase ) for d in rslt] random.shuffle(rslt_ ) logger.info(f'''Dump to {dp_file}''' ) with open(__lowercase , 'wb' ) as handle: pickle.dump(rslt_ , __lowercase , protocol=pickle.HIGHEST_PROTOCOL ) if __name__ == "__main__": main()
130
1
def lowerCAmelCase_ ( __a ) -> list: """simple docstring""" if any(not isinstance(__a , __a ) or x < 0 for x in sequence ): raise TypeError("Sequence must be list of non-negative integers" ) for _ in range(len(__a ) ): for i, (rod_upper, rod_lower) in enumerate(zip(__a , sequence[1:] ) ): if rod_upper > rod_lower: sequence[i] -= rod_upper - rod_lower sequence[i + 1] += rod_upper - rod_lower return sequence if __name__ == "__main__": assert bead_sort([5, 4, 3, 2, 1]) == [1, 2, 3, 4, 5] assert bead_sort([7, 9, 4, 3, 5]) == [3, 4, 5, 7, 9]
10
'''simple docstring''' from __future__ import annotations def UpperCamelCase( UpperCAmelCase_ , UpperCAmelCase_ ): if partitions <= 0: raise ValueError('partitions must be a positive number!' ) if partitions > number_of_bytes: raise ValueError('partitions can not > number_of_bytes!' ) UpperCAmelCase : int = number_of_bytes // partitions UpperCAmelCase : List[str] = [] for i in range(UpperCAmelCase_ ): UpperCAmelCase : List[Any] = i * bytes_per_partition + 1 UpperCAmelCase : str = ( number_of_bytes if i == partitions - 1 else (i + 1) * bytes_per_partition ) allocation_list.append(F"""{start_bytes}-{end_bytes}""" ) return allocation_list if __name__ == "__main__": import doctest doctest.testmod()
151
0
"""simple docstring""" import inspect from typing import Optional, Union import numpy as np import PIL import torch from torch.nn import functional as F from torchvision import transforms from transformers import CLIPFeatureExtractor, CLIPModel, CLIPTextModel, CLIPTokenizer from diffusers import ( AutoencoderKL, DDIMScheduler, DiffusionPipeline, DPMSolverMultistepScheduler, LMSDiscreteScheduler, PNDMScheduler, UNetaDConditionModel, ) from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion import StableDiffusionPipelineOutput from diffusers.utils import ( PIL_INTERPOLATION, randn_tensor, ) def _a ( SCREAMING_SNAKE_CASE : Tuple , SCREAMING_SNAKE_CASE : Union[str, Any] , SCREAMING_SNAKE_CASE : List[Any] ): """simple docstring""" if isinstance(a_ , torch.Tensor ): return image elif isinstance(a_ , PIL.Image.Image ): UpperCamelCase__ : Any = [image] if isinstance(image[0] , PIL.Image.Image ): UpperCamelCase__ : Union[str, Any] = [np.array(i.resize((w, h) , resample=PIL_INTERPOLATION['''lanczos'''] ) )[None, :] for i in image] UpperCamelCase__ : List[Any] = np.concatenate(a_ , axis=0 ) UpperCamelCase__ : Tuple = np.array(a_ ).astype(np.floataa ) / 255.0 UpperCamelCase__ : List[str] = image.transpose(0 , 3 , 1 , 2 ) UpperCamelCase__ : List[str] = 2.0 * image - 1.0 UpperCamelCase__ : Optional[int] = torch.from_numpy(a_ ) elif isinstance(image[0] , torch.Tensor ): UpperCamelCase__ : int = torch.cat(a_ , dim=0 ) return image def _a ( SCREAMING_SNAKE_CASE : List[str] , SCREAMING_SNAKE_CASE : Any , SCREAMING_SNAKE_CASE : Union[str, Any] , SCREAMING_SNAKE_CASE : List[Any]=0.9995 ): """simple docstring""" if not isinstance(a_ , np.ndarray ): UpperCamelCase__ : List[Any] = True UpperCamelCase__ : List[str] = va.device UpperCamelCase__ : Dict = va.cpu().numpy() UpperCamelCase__ : Optional[Any] = va.cpu().numpy() UpperCamelCase__ : Any = np.sum(va * va / (np.linalg.norm(a_ ) * np.linalg.norm(a_ )) ) if np.abs(a_ ) > DOT_THRESHOLD: UpperCamelCase__ : Dict = (1 - t) * va + t * va else: UpperCamelCase__ : Any = np.arccos(a_ ) UpperCamelCase__ : Optional[Any] = np.sin(a_ ) UpperCamelCase__ : List[Any] = theta_a * t UpperCamelCase__ : int = np.sin(a_ ) UpperCamelCase__ : Tuple = np.sin(theta_a - theta_t ) / sin_theta_a UpperCamelCase__ : str = sin_theta_t / sin_theta_a UpperCamelCase__ : int = sa * va + sa * va if inputs_are_torch: UpperCamelCase__ : Optional[Any] = torch.from_numpy(a_ ).to(a_ ) return va def _a ( SCREAMING_SNAKE_CASE : List[Any] , SCREAMING_SNAKE_CASE : List[Any] ): """simple docstring""" UpperCamelCase__ : List[str] = F.normalize(a_ , dim=-1 ) UpperCamelCase__ : Dict = F.normalize(a_ , dim=-1 ) return (x - y).norm(dim=-1 ).div(2 ).arcsin().pow(2 ).mul(2 ) def _a ( SCREAMING_SNAKE_CASE : str , SCREAMING_SNAKE_CASE : str ): """simple docstring""" for param in model.parameters(): UpperCamelCase__ : Any = value class __magic_name__ ( __lowerCAmelCase): def __init__( self : Optional[Any] , lowerCamelCase__ : AutoencoderKL , lowerCamelCase__ : CLIPTextModel , lowerCamelCase__ : CLIPModel , lowerCamelCase__ : CLIPTokenizer , lowerCamelCase__ : UNetaDConditionModel , lowerCamelCase__ : Union[PNDMScheduler, LMSDiscreteScheduler, DDIMScheduler, DPMSolverMultistepScheduler] , lowerCamelCase__ : CLIPFeatureExtractor , lowerCamelCase__ : Dict=None , lowerCamelCase__ : int=None , lowerCamelCase__ : Any=None , ) -> int: '''simple docstring''' super().__init__() self.register_modules( vae=lowerCAmelCase__ , text_encoder=lowerCAmelCase__ , clip_model=lowerCAmelCase__ , tokenizer=lowerCAmelCase__ , unet=lowerCAmelCase__ , scheduler=lowerCAmelCase__ , feature_extractor=lowerCAmelCase__ , coca_model=lowerCAmelCase__ , coca_tokenizer=lowerCAmelCase__ , coca_transform=lowerCAmelCase__ , ) UpperCamelCase__ : List[Any] = ( feature_extractor.size if isinstance(feature_extractor.size , lowerCAmelCase__ ) else feature_extractor.size["shortest_edge"] ) UpperCamelCase__ : Union[str, Any] = transforms.Normalize(mean=feature_extractor.image_mean , std=feature_extractor.image_std ) set_requires_grad(self.text_encoder , lowerCAmelCase__ ) set_requires_grad(self.clip_model , lowerCAmelCase__ ) def UpperCAmelCase__ ( self : Union[str, Any] , lowerCamelCase__ : Optional[Union[str, int]] = "auto" ) -> Optional[int]: '''simple docstring''' if slice_size == "auto": # half the attention head size is usually a good trade-off between # speed and memory UpperCamelCase__ : Optional[int] = self.unet.config.attention_head_dim // 2 self.unet.set_attention_slice(lowerCAmelCase__ ) def UpperCAmelCase__ ( self : Tuple ) -> List[Any]: '''simple docstring''' self.enable_attention_slicing(lowerCAmelCase__ ) def UpperCAmelCase__ ( self : List[str] ) -> Union[str, Any]: '''simple docstring''' set_requires_grad(self.vae , lowerCAmelCase__ ) def UpperCAmelCase__ ( self : Dict ) -> Tuple: '''simple docstring''' set_requires_grad(self.vae , lowerCAmelCase__ ) def UpperCAmelCase__ ( self : Any ) -> List[str]: '''simple docstring''' set_requires_grad(self.unet , lowerCAmelCase__ ) def UpperCAmelCase__ ( self : Optional[int] ) -> Optional[int]: '''simple docstring''' set_requires_grad(self.unet , lowerCAmelCase__ ) def UpperCAmelCase__ ( self : Union[str, Any] , lowerCamelCase__ : Tuple , lowerCamelCase__ : Optional[Any] , lowerCamelCase__ : Union[str, Any] ) -> Union[str, Any]: '''simple docstring''' UpperCamelCase__ : int = min(int(num_inference_steps * strength ) , lowerCAmelCase__ ) UpperCamelCase__ : Optional[int] = max(num_inference_steps - init_timestep , 0 ) UpperCamelCase__ : str = self.scheduler.timesteps[t_start:] return timesteps, num_inference_steps - t_start def UpperCAmelCase__ ( self : str , lowerCamelCase__ : List[str] , lowerCamelCase__ : Optional[int] , lowerCamelCase__ : List[Any] , lowerCamelCase__ : List[Any] , lowerCamelCase__ : Union[str, Any] , lowerCamelCase__ : List[Any]=None ) -> str: '''simple docstring''' if not isinstance(lowerCAmelCase__ , torch.Tensor ): raise ValueError(F"`image` has to be of type `torch.Tensor` but is {type(lowerCAmelCase__ )}" ) UpperCamelCase__ : Tuple = image.to(device=lowerCAmelCase__ , dtype=lowerCAmelCase__ ) if isinstance(lowerCAmelCase__ , lowerCAmelCase__ ): UpperCamelCase__ : List[Any] = [ self.vae.encode(image[i : i + 1] ).latent_dist.sample(generator[i] ) for i in range(lowerCAmelCase__ ) ] UpperCamelCase__ : Optional[Any] = torch.cat(lowerCAmelCase__ , dim=0 ) else: UpperCamelCase__ : int = self.vae.encode(lowerCAmelCase__ ).latent_dist.sample(lowerCAmelCase__ ) # Hardcode 0.18215 because stable-diffusion-2-base has not self.vae.config.scaling_factor UpperCamelCase__ : Union[str, Any] = 0.1_8215 * init_latents UpperCamelCase__ : Any = init_latents.repeat_interleave(lowerCAmelCase__ , dim=0 ) UpperCamelCase__ : str = randn_tensor(init_latents.shape , generator=lowerCAmelCase__ , device=lowerCAmelCase__ , dtype=lowerCAmelCase__ ) # get latents UpperCamelCase__ : Dict = self.scheduler.add_noise(lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ ) UpperCamelCase__ : Union[str, Any] = init_latents return latents def UpperCAmelCase__ ( self : Optional[Any] , lowerCamelCase__ : Dict ) -> Optional[Any]: '''simple docstring''' UpperCamelCase__ : Tuple = self.coca_transform(lowerCAmelCase__ ).unsqueeze(0 ) with torch.no_grad(), torch.cuda.amp.autocast(): UpperCamelCase__ : int = self.coca_model.generate(transformed_image.to(device=self.device , dtype=self.coca_model.dtype ) ) UpperCamelCase__ : List[str] = self.coca_tokenizer.decode(generated[0].cpu().numpy() ) return generated.split('''<end_of_text>''' )[0].replace('''<start_of_text>''' , '''''' ).rstrip(''' .,''' ) def UpperCAmelCase__ ( self : str , lowerCamelCase__ : str , lowerCamelCase__ : Union[str, Any] ) -> List[str]: '''simple docstring''' UpperCamelCase__ : Dict = self.feature_extractor.preprocess(lowerCAmelCase__ ) UpperCamelCase__ : List[str] = torch.from_numpy(clip_image_input['''pixel_values'''][0] ).unsqueeze(0 ).to(self.device ).half() UpperCamelCase__ : List[str] = self.clip_model.get_image_features(lowerCAmelCase__ ) UpperCamelCase__ : int = image_embeddings_clip / image_embeddings_clip.norm(p=2 , dim=-1 , keepdim=lowerCAmelCase__ ) UpperCamelCase__ : Optional[int] = image_embeddings_clip.repeat_interleave(lowerCAmelCase__ , dim=0 ) return image_embeddings_clip @torch.enable_grad() def UpperCAmelCase__ ( self : str , lowerCamelCase__ : Optional[Any] , lowerCamelCase__ : Optional[Any] , lowerCamelCase__ : List[Any] , lowerCamelCase__ : List[str] , lowerCamelCase__ : Optional[Any] , lowerCamelCase__ : Union[str, Any] , lowerCamelCase__ : Union[str, Any] , ) -> str: '''simple docstring''' UpperCamelCase__ : Union[str, Any] = latents.detach().requires_grad_() UpperCamelCase__ : Tuple = self.scheduler.scale_model_input(lowerCAmelCase__ , lowerCAmelCase__ ) # predict the noise residual UpperCamelCase__ : Union[str, Any] = self.unet(lowerCAmelCase__ , lowerCAmelCase__ , encoder_hidden_states=lowerCAmelCase__ ).sample if isinstance(self.scheduler , (PNDMScheduler, DDIMScheduler, DPMSolverMultistepScheduler) ): UpperCamelCase__ : Union[str, Any] = self.scheduler.alphas_cumprod[timestep] UpperCamelCase__ : Tuple = 1 - alpha_prod_t # compute predicted original sample from predicted noise also called # "predicted x_0" of formula (12) from https://arxiv.org/pdf/2010.02502.pdf UpperCamelCase__ : int = (latents - beta_prod_t ** 0.5 * noise_pred) / alpha_prod_t ** 0.5 UpperCamelCase__ : Any = torch.sqrt(lowerCAmelCase__ ) UpperCamelCase__ : int = pred_original_sample * (fac) + latents * (1 - fac) elif isinstance(self.scheduler , lowerCAmelCase__ ): UpperCamelCase__ : Dict = self.scheduler.sigmas[index] UpperCamelCase__ : Optional[Any] = latents - sigma * noise_pred else: raise ValueError(F"scheduler type {type(self.scheduler )} not supported" ) # Hardcode 0.18215 because stable-diffusion-2-base has not self.vae.config.scaling_factor UpperCamelCase__ : Any = 1 / 0.1_8215 * sample UpperCamelCase__ : Any = self.vae.decode(lowerCAmelCase__ ).sample UpperCamelCase__ : str = (image / 2 + 0.5).clamp(0 , 1 ) UpperCamelCase__ : List[str] = transforms.Resize(self.feature_extractor_size )(lowerCAmelCase__ ) UpperCamelCase__ : List[Any] = self.normalize(lowerCAmelCase__ ).to(latents.dtype ) UpperCamelCase__ : Union[str, Any] = self.clip_model.get_image_features(lowerCAmelCase__ ) UpperCamelCase__ : Dict = image_embeddings_clip / image_embeddings_clip.norm(p=2 , dim=-1 , keepdim=lowerCAmelCase__ ) UpperCamelCase__ : str = spherical_dist_loss(lowerCAmelCase__ , lowerCAmelCase__ ).mean() * clip_guidance_scale UpperCamelCase__ : Any = -torch.autograd.grad(lowerCAmelCase__ , lowerCAmelCase__ )[0] if isinstance(self.scheduler , lowerCAmelCase__ ): UpperCamelCase__ : Tuple = latents.detach() + grads * (sigma**2) UpperCamelCase__ : Any = noise_pred_original else: UpperCamelCase__ : Optional[Any] = noise_pred_original - torch.sqrt(lowerCAmelCase__ ) * grads return noise_pred, latents @torch.no_grad() def __call__( self : List[str] , lowerCamelCase__ : Union[torch.FloatTensor, PIL.Image.Image] , lowerCamelCase__ : Union[torch.FloatTensor, PIL.Image.Image] , lowerCamelCase__ : Optional[str] = None , lowerCamelCase__ : Optional[str] = None , lowerCamelCase__ : Optional[int] = 512 , lowerCamelCase__ : Optional[int] = 512 , lowerCamelCase__ : float = 0.6 , lowerCamelCase__ : Optional[int] = 50 , lowerCamelCase__ : Optional[float] = 7.5 , lowerCamelCase__ : Optional[int] = 1 , lowerCamelCase__ : float = 0.0 , lowerCamelCase__ : Optional[float] = 100 , lowerCamelCase__ : Optional[torch.Generator] = None , lowerCamelCase__ : Optional[str] = "pil" , lowerCamelCase__ : bool = True , lowerCamelCase__ : float = 0.8 , lowerCamelCase__ : float = 0.1 , lowerCamelCase__ : float = 0.1 , ) -> List[str]: '''simple docstring''' if isinstance(lowerCAmelCase__ , lowerCAmelCase__ ) and len(lowerCAmelCase__ ) != batch_size: raise ValueError(F"You have passed {batch_size} batch_size, but only {len(lowerCAmelCase__ )} generators." ) if height % 8 != 0 or width % 8 != 0: raise ValueError(F"`height` and `width` have to be divisible by 8 but are {height} and {width}." ) if isinstance(lowerCAmelCase__ , torch.Generator ) and batch_size > 1: UpperCamelCase__ : Optional[Any] = [generator] + [None] * (batch_size - 1) UpperCamelCase__ : Optional[Any] = [ ("model", self.coca_model is None), ("tokenizer", self.coca_tokenizer is None), ("transform", self.coca_transform is None), ] UpperCamelCase__ : Tuple = [x[0] for x in coca_is_none if x[1]] UpperCamelCase__ : int = ", ".join(lowerCAmelCase__ ) # generate prompts with coca model if prompt is None if content_prompt is None: if len(lowerCAmelCase__ ): raise ValueError( F"Content prompt is None and CoCa [{coca_is_none_str}] is None." F"Set prompt or pass Coca [{coca_is_none_str}] to DiffusionPipeline." ) UpperCamelCase__ : str = self.get_image_description(lowerCAmelCase__ ) if style_prompt is None: if len(lowerCAmelCase__ ): raise ValueError( F"Style prompt is None and CoCa [{coca_is_none_str}] is None." F" Set prompt or pass Coca [{coca_is_none_str}] to DiffusionPipeline." ) UpperCamelCase__ : str = self.get_image_description(lowerCAmelCase__ ) # get prompt text embeddings for content and style UpperCamelCase__ : Tuple = self.tokenizer( lowerCAmelCase__ , padding='''max_length''' , max_length=self.tokenizer.model_max_length , truncation=lowerCAmelCase__ , return_tensors='''pt''' , ) UpperCamelCase__ : Dict = self.text_encoder(content_text_input.input_ids.to(self.device ) )[0] UpperCamelCase__ : Dict = self.tokenizer( lowerCAmelCase__ , padding='''max_length''' , max_length=self.tokenizer.model_max_length , truncation=lowerCAmelCase__ , return_tensors='''pt''' , ) UpperCamelCase__ : Optional[int] = self.text_encoder(style_text_input.input_ids.to(self.device ) )[0] UpperCamelCase__ : Any = slerp(lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ ) # duplicate text embeddings for each generation per prompt UpperCamelCase__ : str = text_embeddings.repeat_interleave(lowerCAmelCase__ , dim=0 ) # set timesteps UpperCamelCase__ : List[Any] = "offset" in set(inspect.signature(self.scheduler.set_timesteps ).parameters.keys() ) UpperCamelCase__ : Dict = {} if accepts_offset: UpperCamelCase__ : List[str] = 1 self.scheduler.set_timesteps(lowerCAmelCase__ , **lowerCAmelCase__ ) # Some schedulers like PNDM have timesteps as arrays # It's more optimized to move all timesteps to correct device beforehand self.scheduler.timesteps.to(self.device ) UpperCamelCase__ : Optional[int] = self.get_timesteps(lowerCAmelCase__ , lowerCAmelCase__ , self.device ) UpperCamelCase__ : Optional[Any] = timesteps[:1].repeat(lowerCAmelCase__ ) # Preprocess image UpperCamelCase__ : Dict = preprocess(lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ ) UpperCamelCase__ : str = self.prepare_latents( lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , text_embeddings.dtype , self.device , lowerCAmelCase__ ) UpperCamelCase__ : List[Any] = preprocess(lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ ) UpperCamelCase__ : List[str] = self.prepare_latents( lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , text_embeddings.dtype , self.device , lowerCAmelCase__ ) UpperCamelCase__ : int = slerp(lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ ) if clip_guidance_scale > 0: UpperCamelCase__ : int = self.get_clip_image_embeddings(lowerCAmelCase__ , lowerCAmelCase__ ) UpperCamelCase__ : Optional[Any] = self.get_clip_image_embeddings(lowerCAmelCase__ , lowerCAmelCase__ ) UpperCamelCase__ : Union[str, Any] = slerp( lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ ) # here `guidance_scale` is defined analog to the guidance weight `w` of equation (2) # of the Imagen paper: https://arxiv.org/pdf/2205.11487.pdf . `guidance_scale = 1` # corresponds to doing no classifier free guidance. UpperCamelCase__ : Any = guidance_scale > 1.0 # get unconditional embeddings for classifier free guidance if do_classifier_free_guidance: UpperCamelCase__ : Optional[int] = content_text_input.input_ids.shape[-1] UpperCamelCase__ : Any = self.tokenizer([''''''] , padding='''max_length''' , max_length=lowerCAmelCase__ , return_tensors='''pt''' ) UpperCamelCase__ : Optional[int] = self.text_encoder(uncond_input.input_ids.to(self.device ) )[0] # duplicate unconditional embeddings for each generation per prompt UpperCamelCase__ : Union[str, Any] = uncond_embeddings.repeat_interleave(lowerCAmelCase__ , dim=0 ) # For classifier free guidance, we need to do two forward passes. # Here we concatenate the unconditional and text embeddings into a single batch # to avoid doing two forward passes UpperCamelCase__ : Optional[int] = torch.cat([uncond_embeddings, text_embeddings] ) # get the initial random noise unless the user supplied it # Unlike in other pipelines, latents need to be generated in the target device # for 1-to-1 results reproducibility with the CompVis implementation. # However this currently doesn't work in `mps`. UpperCamelCase__ : str = (batch_size, self.unet.config.in_channels, height // 8, width // 8) UpperCamelCase__ : Union[str, Any] = text_embeddings.dtype if latents is None: if self.device.type == "mps": # randn does not work reproducibly on mps UpperCamelCase__ : Optional[Any] = torch.randn(lowerCAmelCase__ , generator=lowerCAmelCase__ , device='''cpu''' , dtype=lowerCAmelCase__ ).to( self.device ) else: UpperCamelCase__ : List[Any] = torch.randn(lowerCAmelCase__ , generator=lowerCAmelCase__ , device=self.device , dtype=lowerCAmelCase__ ) else: if latents.shape != latents_shape: raise ValueError(F"Unexpected latents shape, got {latents.shape}, expected {latents_shape}" ) UpperCamelCase__ : List[str] = latents.to(self.device ) # scale the initial noise by the standard deviation required by the scheduler UpperCamelCase__ : Any = latents * self.scheduler.init_noise_sigma # prepare extra kwargs for the scheduler step, since not all schedulers have the same signature # eta (η) is only used with the DDIMScheduler, it will be ignored for other schedulers. # eta corresponds to η in DDIM paper: https://arxiv.org/abs/2010.02502 # and should be between [0, 1] UpperCamelCase__ : Optional[Any] = "eta" in set(inspect.signature(self.scheduler.step ).parameters.keys() ) UpperCamelCase__ : List[Any] = {} if accepts_eta: UpperCamelCase__ : List[Any] = eta # check if the scheduler accepts generator UpperCamelCase__ : List[Any] = "generator" in set(inspect.signature(self.scheduler.step ).parameters.keys() ) if accepts_generator: UpperCamelCase__ : Any = generator with self.progress_bar(total=lowerCAmelCase__ ): for i, t in enumerate(lowerCAmelCase__ ): # expand the latents if we are doing classifier free guidance UpperCamelCase__ : str = torch.cat([latents] * 2 ) if do_classifier_free_guidance else latents UpperCamelCase__ : int = self.scheduler.scale_model_input(lowerCAmelCase__ , lowerCAmelCase__ ) # predict the noise residual UpperCamelCase__ : Tuple = self.unet(lowerCAmelCase__ , lowerCAmelCase__ , encoder_hidden_states=lowerCAmelCase__ ).sample # perform classifier free guidance if do_classifier_free_guidance: UpperCamelCase__ : Dict = noise_pred.chunk(2 ) UpperCamelCase__ : List[Any] = noise_pred_uncond + guidance_scale * (noise_pred_text - noise_pred_uncond) # perform clip guidance if clip_guidance_scale > 0: UpperCamelCase__ : Union[str, Any] = ( text_embeddings.chunk(2 )[1] if do_classifier_free_guidance else text_embeddings ) UpperCamelCase__ : Optional[int] = self.cond_fn( lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , ) # compute the previous noisy sample x_t -> x_t-1 UpperCamelCase__ : List[Any] = self.scheduler.step(lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , **lowerCAmelCase__ ).prev_sample # Hardcode 0.18215 because stable-diffusion-2-base has not self.vae.config.scaling_factor UpperCamelCase__ : Optional[Any] = 1 / 0.1_8215 * latents UpperCamelCase__ : Tuple = self.vae.decode(lowerCAmelCase__ ).sample UpperCamelCase__ : List[str] = (image / 2 + 0.5).clamp(0 , 1 ) UpperCamelCase__ : Optional[int] = image.cpu().permute(0 , 2 , 3 , 1 ).numpy() if output_type == "pil": UpperCamelCase__ : Union[str, Any] = self.numpy_to_pil(lowerCAmelCase__ ) if not return_dict: return (image, None) return StableDiffusionPipelineOutput(images=lowerCAmelCase__ , nsfw_content_detected=lowerCAmelCase__ )
367
import argparse import collections import torch from flax import traverse_util from tax import checkpoints from transformers import TaConfig, TaEncoderModel, TaForConditionalGeneration from transformers.utils import logging logging.set_verbosity_info() def _a ( SCREAMING_SNAKE_CASE : Optional[Any] , SCREAMING_SNAKE_CASE : int , SCREAMING_SNAKE_CASE : Optional[Any] , SCREAMING_SNAKE_CASE : Tuple="attention" ): """simple docstring""" UpperCamelCase__ : List[Any] = params[F"{prefix}/layers_{i}/{layer_name}/key/kernel"] UpperCamelCase__ : Optional[Any] = params[F"{prefix}/layers_{i}/{layer_name}/out/kernel"] UpperCamelCase__ : Union[str, Any] = params[F"{prefix}/layers_{i}/{layer_name}/query/kernel"] UpperCamelCase__ : int = params[F"{prefix}/layers_{i}/{layer_name}/value/kernel"] return k, o, q, v def _a ( SCREAMING_SNAKE_CASE : str , SCREAMING_SNAKE_CASE : Optional[Any] , SCREAMING_SNAKE_CASE : Union[str, Any] , SCREAMING_SNAKE_CASE : Any=False ): """simple docstring""" if split_mlp_wi: UpperCamelCase__ : Optional[int] = params[F"{prefix}/layers_{i}/mlp/wi_0/kernel"] UpperCamelCase__ : int = params[F"{prefix}/layers_{i}/mlp/wi_1/kernel"] UpperCamelCase__ : Any = (wi_a, wi_a) else: UpperCamelCase__ : Union[str, Any] = params[F"{prefix}/layers_{i}/mlp/wi/kernel"] UpperCamelCase__ : Any = params[F"{prefix}/layers_{i}/mlp/wo/kernel"] return wi, wo def _a ( SCREAMING_SNAKE_CASE : int , SCREAMING_SNAKE_CASE : List[str] , SCREAMING_SNAKE_CASE : Dict , SCREAMING_SNAKE_CASE : Union[str, Any] ): """simple docstring""" return params[F"{prefix}/layers_{i}/{layer_name}/scale"] def _a ( SCREAMING_SNAKE_CASE : dict , *, SCREAMING_SNAKE_CASE : int , SCREAMING_SNAKE_CASE : bool ): """simple docstring""" UpperCamelCase__ : List[Any] = traverse_util.flatten_dict(variables['''target'''] ) UpperCamelCase__ : List[str] = {'''/'''.join(SCREAMING_SNAKE_CASE ): v for k, v in old.items()} # v1.1 models have a gated GeLU with wi_0 and wi_1 instead of wi UpperCamelCase__ : List[Any] = '''encoder/layers_0/mlp/wi_0/kernel''' in old print('''Split MLP:''' , SCREAMING_SNAKE_CASE ) UpperCamelCase__ : Any = collections.OrderedDict() # Shared embeddings. UpperCamelCase__ : List[Any] = old['''token_embedder/embedding'''] # Encoder. for i in range(SCREAMING_SNAKE_CASE ): # Block i, layer 0 (Self Attention). UpperCamelCase__ : int = tax_layer_norm_lookup(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , '''encoder''' , '''pre_attention_layer_norm''' ) UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ : Optional[Any] = tax_attention_lookup(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , '''encoder''' , '''attention''' ) UpperCamelCase__ : Tuple = layer_norm UpperCamelCase__ : Optional[int] = k.T UpperCamelCase__ : Any = o.T UpperCamelCase__ : Dict = q.T UpperCamelCase__ : List[str] = v.T # Block i, layer 1 (MLP). UpperCamelCase__ : Tuple = tax_layer_norm_lookup(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , '''encoder''' , '''pre_mlp_layer_norm''' ) UpperCamelCase__ , UpperCamelCase__ : Dict = tax_mlp_lookup(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , '''encoder''' , SCREAMING_SNAKE_CASE ) UpperCamelCase__ : Union[str, Any] = layer_norm if split_mlp_wi: UpperCamelCase__ : Optional[int] = wi[0].T UpperCamelCase__ : Tuple = wi[1].T else: UpperCamelCase__ : List[Any] = wi.T UpperCamelCase__ : Optional[int] = wo.T UpperCamelCase__ : List[str] = old[ '''encoder/relpos_bias/rel_embedding''' ].T UpperCamelCase__ : str = old['''encoder/encoder_norm/scale'''] if not is_encoder_only: # Decoder. for i in range(SCREAMING_SNAKE_CASE ): # Block i, layer 0 (Self Attention). UpperCamelCase__ : List[Any] = tax_layer_norm_lookup(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , '''decoder''' , '''pre_self_attention_layer_norm''' ) UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ : Dict = tax_attention_lookup(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , '''decoder''' , '''self_attention''' ) UpperCamelCase__ : Dict = layer_norm UpperCamelCase__ : Optional[Any] = k.T UpperCamelCase__ : Tuple = o.T UpperCamelCase__ : Any = q.T UpperCamelCase__ : Optional[Any] = v.T # Block i, layer 1 (Cross Attention). UpperCamelCase__ : Optional[Any] = tax_layer_norm_lookup(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , '''decoder''' , '''pre_cross_attention_layer_norm''' ) UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ : Any = tax_attention_lookup(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , '''decoder''' , '''encoder_decoder_attention''' ) UpperCamelCase__ : Optional[int] = layer_norm UpperCamelCase__ : List[Any] = k.T UpperCamelCase__ : Optional[Any] = o.T UpperCamelCase__ : Dict = q.T UpperCamelCase__ : Any = v.T # Block i, layer 2 (MLP). UpperCamelCase__ : Union[str, Any] = tax_layer_norm_lookup(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , '''decoder''' , '''pre_mlp_layer_norm''' ) UpperCamelCase__ , UpperCamelCase__ : Union[str, Any] = tax_mlp_lookup(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , '''decoder''' , SCREAMING_SNAKE_CASE ) UpperCamelCase__ : Optional[Any] = layer_norm if split_mlp_wi: UpperCamelCase__ : str = wi[0].T UpperCamelCase__ : Any = wi[1].T else: UpperCamelCase__ : Tuple = wi.T UpperCamelCase__ : Tuple = wo.T UpperCamelCase__ : Optional[int] = old['''decoder/decoder_norm/scale'''] UpperCamelCase__ : Any = old[ '''decoder/relpos_bias/rel_embedding''' ].T # LM Head (only in v1.1 checkpoints, in v1.0 embeddings are used instead) if "decoder/logits_dense/kernel" in old: UpperCamelCase__ : Dict = old['''decoder/logits_dense/kernel'''].T return new def _a ( SCREAMING_SNAKE_CASE : Tuple , SCREAMING_SNAKE_CASE : bool ): """simple docstring""" UpperCamelCase__ : Optional[int] = collections.OrderedDict([(k, torch.from_numpy(v.copy() )) for (k, v) in converted_params.items()] ) # Add what is missing. if "encoder.embed_tokens.weight" not in state_dict: UpperCamelCase__ : Optional[Any] = state_dict['''shared.weight'''] if not is_encoder_only: if "decoder.embed_tokens.weight" not in state_dict: UpperCamelCase__ : List[Any] = state_dict['''shared.weight'''] if "lm_head.weight" not in state_dict: # For old 1.0 models. print('''Using shared word embeddings as lm_head.''' ) UpperCamelCase__ : List[str] = state_dict['''shared.weight'''] return state_dict def _a ( SCREAMING_SNAKE_CASE : Any , SCREAMING_SNAKE_CASE : List[str] , SCREAMING_SNAKE_CASE : Dict , SCREAMING_SNAKE_CASE : Tuple ): """simple docstring""" UpperCamelCase__ : Tuple = checkpoints.load_tax_checkpoint(SCREAMING_SNAKE_CASE ) UpperCamelCase__ : Dict = convert_tax_to_pytorch(SCREAMING_SNAKE_CASE , num_layers=config.num_layers , is_encoder_only=SCREAMING_SNAKE_CASE ) UpperCamelCase__ : List[str] = make_state_dict(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ) model.load_state_dict(SCREAMING_SNAKE_CASE , strict=SCREAMING_SNAKE_CASE ) def _a ( SCREAMING_SNAKE_CASE : List[str] , SCREAMING_SNAKE_CASE : Optional[int] , SCREAMING_SNAKE_CASE : List[Any] , SCREAMING_SNAKE_CASE : bool = False ): """simple docstring""" UpperCamelCase__ : Tuple = TaConfig.from_json_file(SCREAMING_SNAKE_CASE ) print(F"Building PyTorch model from configuration: {config}" ) # Non-v1.1 checkpoints could also use T5Model, but this works for all. # The v1.0 checkpoints will simply have an LM head that is the word embeddings. if is_encoder_only: UpperCamelCase__ : Any = TaEncoderModel(SCREAMING_SNAKE_CASE ) else: UpperCamelCase__ : Union[str, Any] = TaForConditionalGeneration(SCREAMING_SNAKE_CASE ) # Load weights from tf checkpoint load_tax_weights_in_ta(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ) # Save pytorch-model print(F"Save PyTorch model to {pytorch_dump_path}" ) model.save_pretrained(SCREAMING_SNAKE_CASE ) # Verify that we can load the checkpoint. model.from_pretrained(SCREAMING_SNAKE_CASE ) print('''Done''' ) if __name__ == "__main__": __UpperCamelCase : Union[str, Any] = argparse.ArgumentParser(description="Converts a native T5X checkpoint into a PyTorch checkpoint.") # Required parameters parser.add_argument( "--t5x_checkpoint_path", default=None, type=str, required=True, help="Path to the T5X checkpoint." ) parser.add_argument( "--config_file", default=None, type=str, required=True, help="The config json file corresponding to the pre-trained T5 model.\nThis specifies the model architecture.", ) parser.add_argument( "--pytorch_dump_path", default=None, type=str, required=True, help="Path to the output PyTorch model." ) parser.add_argument( "--is_encoder_only", action="store_true", help="Check if the model is encoder-decoder model", default=False ) __UpperCamelCase : int = parser.parse_args() convert_tax_checkpoint_to_pytorch( args.tax_checkpoint_path, args.config_file, args.pytorch_dump_path, args.is_encoder_only )
51
0
"""simple docstring""" import math_equivalence # From: git+https://github.com/hendrycks/math.git import datasets lowerCAmelCase__ : str = '\\n@article{hendrycksmath2021,\n title={Measuring Mathematical Problem Solving With the MATH Dataset},\n author={Dan Hendrycks\n and Collin Burns\n and Saurav Kadavath\n and Akul Arora\n and Steven Basart\n and Eric Tang\n and Dawn Song\n and Jacob Steinhardt},\n journal={arXiv preprint arXiv:2103.03874},\n year={2021}\n}\n' lowerCAmelCase__ : List[str] = '\\nThis metric is used to assess performance on the Mathematics Aptitude Test of Heuristics (MATH) dataset.\nIt first canonicalizes the inputs (e.g., converting "1/2" to "\\frac{1}{2}") and then computes accuracy.\n' lowerCAmelCase__ : Optional[Any] = r'\nCalculates accuracy after canonicalizing inputs.\n\nArgs:\n predictions: list of predictions to score. Each prediction\n is a string that contains natural language and LaTex.\n references: list of reference for each prediction. Each\n reference is a string that contains natural language\n and LaTex.\nReturns:\n accuracy: accuracy after canonicalizing inputs\n (e.g., converting "1/2" to "\\frac{1}{2}")\n\nExamples:\n >>> metric = datasets.load_metric("competition_math")\n >>> results = metric.compute(references=["\\frac{1}{2}"], predictions=["1/2"])\n >>> print(results)\n {\'accuracy\': 1.0}\n' @datasets.utils.file_utils.add_end_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class snake_case ( datasets.Metric ): """simple docstring""" def __lowerCAmelCase ( self : Any ): return datasets.MetricInfo( description=_DESCRIPTION ,citation=_CITATION ,inputs_description=_KWARGS_DESCRIPTION ,features=datasets.Features( { 'predictions': datasets.Value('string' ), 'references': datasets.Value('string' ), } ) ,homepage='https://github.com/hendrycks/math' ,codebase_urls=['https://github.com/hendrycks/math'] ,) def __lowerCAmelCase ( self : List[str] ,lowerCamelCase__ : Optional[int] ,lowerCamelCase__ : Any ): UpperCAmelCase__ = 0.0 for i, j in zip(lowerCamelCase__ ,lowerCamelCase__ ): n_correct += 1.0 if math_equivalence.is_equiv(lowerCamelCase__ ,lowerCamelCase__ ) else 0.0 UpperCAmelCase__ = n_correct / len(lowerCamelCase__ ) return { "accuracy": accuracy, }
98
"""simple docstring""" import math def a_ ( lowerCamelCase , lowerCamelCase ): if ( not isinstance(lowerCamelCase , (int, float) ) or power_factor < -1 or power_factor > 1 ): raise ValueError('power_factor must be a valid float value between -1 and 1.' ) return apparent_power * power_factor def a_ ( lowerCamelCase , lowerCamelCase ): if ( not isinstance(lowerCamelCase , (int, float) ) or power_factor < -1 or power_factor > 1 ): raise ValueError('power_factor must be a valid float value between -1 and 1.' ) return apparent_power * math.sqrt(1 - power_factor**2 ) if __name__ == "__main__": import doctest doctest.testmod()
98
1
"""simple docstring""" def __lowerCamelCase ( __UpperCamelCase ) -> list: """simple docstring""" if bit_count < 0: raise ValueError("The given input must be positive" ) # get the generated string sequence lowerCAmelCase_ : Optional[Any] = gray_code_sequence_string(__UpperCamelCase ) # # convert them to integers for i in range(len(__UpperCamelCase ) ): lowerCAmelCase_ : List[Any] = int(sequence[i] , 2 ) return sequence def __lowerCamelCase ( __UpperCamelCase ) -> list: """simple docstring""" if bit_count == 0: return ["0"] if bit_count == 1: return ["0", "1"] lowerCAmelCase_ : Dict = 1 << bit_count # defines the length of the sequence # 1<< n is equivalent to 2^n # recursive answer will generate answer for n-1 bits lowerCAmelCase_ : List[str] = gray_code_sequence_string(bit_count - 1 ) lowerCAmelCase_ : Tuple = [] # append 0 to first half of the smaller sequence generated for i in range(seq_len // 2 ): lowerCAmelCase_ : List[str] = "0" + smaller_sequence[i] sequence.append(__UpperCamelCase ) # append 1 to second half ... start from the end of the list for i in reversed(range(seq_len // 2 ) ): lowerCAmelCase_ : Optional[Any] = "1" + smaller_sequence[i] sequence.append(__UpperCamelCase ) return sequence if __name__ == "__main__": import doctest doctest.testmod()
353
"""simple docstring""" def __lowerCamelCase ( __UpperCamelCase = 50 ) -> int: """simple docstring""" lowerCAmelCase_ : int = [1] * (length + 1) for row_length in range(3 , length + 1 ): for block_length in range(3 , row_length + 1 ): for block_start in range(row_length - block_length ): ways_number[row_length] += ways_number[ row_length - block_start - block_length - 1 ] ways_number[row_length] += 1 return ways_number[length] if __name__ == "__main__": print(F"""{solution() = }""")
161
0
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available A__ : Optional[int] = { """configuration_upernet""": ["""UperNetConfig"""], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: A__ : Optional[Any] = [ """UperNetForSemanticSegmentation""", """UperNetPreTrainedModel""", ] if TYPE_CHECKING: from .configuration_upernet import UperNetConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_upernet import UperNetForSemanticSegmentation, UperNetPreTrainedModel else: import sys A__ : int = _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)
185
'''simple docstring''' A__ : Any = 8.3_1_4_4_6_2 # Unit - J mol-1 K-1 def UpperCAmelCase__ ( UpperCAmelCase_ : float , UpperCAmelCase_ : float , UpperCAmelCase_ : float ) -> float: if moles < 0 or kelvin < 0 or volume < 0: raise ValueError('Invalid inputs. Enter positive value.' ) return moles * kelvin * UNIVERSAL_GAS_CONSTANT / volume def UpperCAmelCase__ ( UpperCAmelCase_ : float , UpperCAmelCase_ : float , UpperCAmelCase_ : float ) -> float: if moles < 0 or kelvin < 0 or pressure < 0: raise ValueError('Invalid inputs. Enter positive value.' ) return moles * kelvin * UNIVERSAL_GAS_CONSTANT / pressure if __name__ == "__main__": from doctest import testmod testmod()
185
1
'''simple docstring''' from ...configuration_utils import PretrainedConfig from ...utils import logging _snake_case = logging.get_logger(__name__) _snake_case = { 'google/vivit-b-16x2-kinetics400': ( 'https://huggingface.co/google/vivit-b-16x2-kinetics400/resolve/main/config.json' ), # See all Vivit models at https://huggingface.co/models?filter=vivit } class a__ ( lowerCamelCase_ ): _SCREAMING_SNAKE_CASE : List[Any] = 'vivit' def __init__( self , _UpperCamelCase=224 , _UpperCamelCase=32 , _UpperCamelCase=[2, 16, 16] , _UpperCamelCase=3 , _UpperCamelCase=768 , _UpperCamelCase=12 , _UpperCamelCase=12 , _UpperCamelCase=3072 , _UpperCamelCase="gelu_fast" , _UpperCamelCase=0.0 , _UpperCamelCase=0.0 , _UpperCamelCase=0.0_2 , _UpperCamelCase=1E-0_6 , _UpperCamelCase=True , **_UpperCamelCase , ): """simple docstring""" _lowercase : List[str] = hidden_size _lowercase : List[Any] = num_hidden_layers _lowercase : Dict = num_attention_heads _lowercase : Any = intermediate_size _lowercase : List[Any] = hidden_act _lowercase : Optional[int] = hidden_dropout_prob _lowercase : Dict = attention_probs_dropout_prob _lowercase : str = initializer_range _lowercase : Optional[Any] = layer_norm_eps _lowercase : Optional[int] = image_size _lowercase : Tuple = num_frames _lowercase : Union[str, Any] = tubelet_size _lowercase : Optional[Any] = num_channels _lowercase : int = qkv_bias super().__init__(**_UpperCamelCase )
199
'''simple docstring''' import json import logging import os import re import sys from dataclasses import dataclass, field from typing import Any, Dict, List, Optional, Union import datasets import numpy as np import torch import torchaudio from packaging import version from torch import nn import transformers from transformers import ( HfArgumentParser, Trainer, TrainingArguments, WavaVecaCTCTokenizer, WavaVecaFeatureExtractor, WavaVecaForCTC, WavaVecaProcessor, is_apex_available, set_seed, ) from transformers.trainer_utils import get_last_checkpoint, is_main_process if is_apex_available(): from apex import amp if version.parse(version.parse(torch.__version__).base_version) >= version.parse('1.6'): _snake_case = True from torch.cuda.amp import autocast _snake_case = logging.getLogger(__name__) def _A ( snake_case=None , snake_case=None ) -> Any: return field(default_factory=lambda: default , metadata=snake_case ) @dataclass class a__ : _SCREAMING_SNAKE_CASE : str = field( metadata={'help': 'Path to pretrained model or model identifier from huggingface.co/models'} ) _SCREAMING_SNAKE_CASE : Optional[str] = field( default=lowerCamelCase_ , metadata={'help': 'Where do you want to store the pretrained models downloaded from huggingface.co'} , ) _SCREAMING_SNAKE_CASE : Optional[bool] = field( default=lowerCamelCase_ , metadata={'help': 'Whether to freeze the feature extractor layers of the model.'} ) _SCREAMING_SNAKE_CASE : Optional[float] = field( default=0.1 , metadata={'help': 'The dropout ratio for the attention probabilities.'} ) _SCREAMING_SNAKE_CASE : Optional[float] = field( default=0.1 , metadata={'help': 'The dropout ratio for activations inside the fully connected layer.'} ) _SCREAMING_SNAKE_CASE : Optional[float] = field( default=0.1 , metadata={ 'help': 'The dropout probabilitiy for all fully connected layers in the embeddings, encoder, and pooler.' } , ) _SCREAMING_SNAKE_CASE : Optional[float] = field( default=0.1 , metadata={'help': 'The dropout probabilitiy for all 1D convolutional layers in feature extractor.'} , ) _SCREAMING_SNAKE_CASE : Optional[float] = field( default=0.05 , metadata={ 'help': ( 'Propability of each feature vector along the time axis to be chosen as the start of the vector' 'span to be masked. Approximately ``mask_time_prob * sequence_length // mask_time_length`` feature' 'vectors will be masked along the time axis. This is only relevant if ``apply_spec_augment is True``.' ) } , ) _SCREAMING_SNAKE_CASE : Optional[float] = field(default=0.0 , metadata={'help': 'The LayerDrop probability.'} ) @dataclass class a__ : _SCREAMING_SNAKE_CASE : Optional[str] = field( default=lowerCamelCase_ , metadata={'help': 'The configuration name of the dataset to use (via the datasets library).'} ) _SCREAMING_SNAKE_CASE : Optional[str] = field( default='train+validation' , metadata={ 'help': 'The name of the training data set split to use (via the datasets library). Defaults to \'train\'' } , ) _SCREAMING_SNAKE_CASE : bool = field( default=lowerCamelCase_ , metadata={'help': 'Overwrite the cached preprocessed datasets or not.'} ) _SCREAMING_SNAKE_CASE : Optional[int] = field( default=lowerCamelCase_ , metadata={'help': 'The number of processes to use for the preprocessing.'} , ) _SCREAMING_SNAKE_CASE : Optional[int] = field( default=lowerCamelCase_ , metadata={ 'help': ( 'For debugging purposes or quicker training, truncate the number of training examples to this ' 'value if set.' ) } , ) _SCREAMING_SNAKE_CASE : Optional[int] = field( default=lowerCamelCase_ , metadata={ 'help': ( 'For debugging purposes or quicker training, truncate the number of validation examples to this ' 'value if set.' ) } , ) _SCREAMING_SNAKE_CASE : List[str] = list_field( default=[',', '?', '.', '!', '-', ';', ':', '""', '%', '\'', '"', '�'] , metadata={'help': 'A list of characters to remove from the transcripts.'} , ) @dataclass class a__ : _SCREAMING_SNAKE_CASE : WavaVecaProcessor _SCREAMING_SNAKE_CASE : Union[bool, str] = True _SCREAMING_SNAKE_CASE : Optional[int] = None _SCREAMING_SNAKE_CASE : Optional[int] = None _SCREAMING_SNAKE_CASE : Optional[int] = None _SCREAMING_SNAKE_CASE : Optional[int] = None def __call__( self , _UpperCamelCase ): """simple docstring""" _lowercase : List[str] = [{"input_values": feature["input_values"]} for feature in features] _lowercase : Dict = [{"input_ids": feature["labels"]} for feature in features] _lowercase : Any = self.processor.pad( _UpperCamelCase , padding=self.padding , max_length=self.max_length , pad_to_multiple_of=self.pad_to_multiple_of , return_tensors="pt" , ) _lowercase : Union[str, Any] = self.processor.pad( labels=_UpperCamelCase , padding=self.padding , max_length=self.max_length_labels , pad_to_multiple_of=self.pad_to_multiple_of_labels , return_tensors="pt" , ) # replace padding with -100 to ignore loss correctly _lowercase : List[str] = labels_batch["input_ids"].masked_fill(labels_batch.attention_mask.ne(1 ) , -100 ) _lowercase : Optional[Any] = labels return batch class a__ ( lowerCamelCase_ ): def _lowerCamelCase ( self , _UpperCamelCase , _UpperCamelCase ): """simple docstring""" model.train() _lowercase : str = self._prepare_inputs(_UpperCamelCase ) if self.use_amp: with autocast(): _lowercase : Optional[int] = self.compute_loss(_UpperCamelCase , _UpperCamelCase ) else: _lowercase : Tuple = self.compute_loss(_UpperCamelCase , _UpperCamelCase ) if self.args.n_gpu > 1: if model.module.config.ctc_loss_reduction == "mean": _lowercase : int = loss.mean() elif model.module.config.ctc_loss_reduction == "sum": _lowercase : Any = loss.sum() / (inputs["labels"] >= 0).sum() else: raise ValueError(f'''{model.config.ctc_loss_reduction} is not valid. Choose one of [\'mean\', \'sum\']''' ) if self.args.gradient_accumulation_steps > 1: _lowercase : Optional[int] = loss / self.args.gradient_accumulation_steps if self.use_amp: self.scaler.scale(_UpperCamelCase ).backward() elif self.use_apex: with amp.scale_loss(_UpperCamelCase , self.optimizer ) as scaled_loss: scaled_loss.backward() elif self.deepspeed: self.deepspeed.backward(_UpperCamelCase ) else: loss.backward() return loss.detach() def _A ( ) -> Optional[Any]: # 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. _lowercase : Optional[int] = HfArgumentParser((ModelArguments, DataTrainingArguments, TrainingArguments) ) if len(sys.argv ) == 2 and sys.argv[1].endswith(".json" ): # If we pass only one argument to the script and it's the path to a json file, # let's parse it to get our arguments. _lowercase , _lowercase , _lowercase : List[Any] = parser.parse_json_file(json_file=os.path.abspath(sys.argv[1] ) ) else: _lowercase , _lowercase , _lowercase : int = parser.parse_args_into_dataclasses() # Detecting last checkpoint. _lowercase : Union[str, Any] = None if os.path.isdir(training_args.output_dir ) and training_args.do_train and not training_args.overwrite_output_dir: _lowercase : Union[str, Any] = get_last_checkpoint(training_args.output_dir ) if last_checkpoint is None and len(os.listdir(training_args.output_dir ) ) > 0: raise ValueError( F'''Output directory ({training_args.output_dir}) already exists and is not empty. ''' "Use --overwrite_output_dir to overcome." ) elif last_checkpoint is not None: logger.info( F'''Checkpoint detected, resuming training at {last_checkpoint}. To avoid this behavior, change ''' "the `--output_dir` or add `--overwrite_output_dir` to train from scratch." ) # Setup logging logging.basicConfig( format="%(asctime)s - %(levelname)s - %(name)s - %(message)s" , datefmt="%m/%d/%Y %H:%M:%S" , handlers=[logging.StreamHandler(sys.stdout )] , ) logger.setLevel(logging.INFO if is_main_process(training_args.local_rank ) else logging.WARN ) # Log on each process the small summary: logger.warning( F'''Process rank: {training_args.local_rank}, device: {training_args.device}, n_gpu: {training_args.n_gpu}''' + F'''distributed training: {bool(training_args.local_rank != -1 )}, 16-bits training: {training_args.fpaa}''' ) # Set the verbosity to info of the Transformers logger (on main process only): if is_main_process(training_args.local_rank ): transformers.utils.logging.set_verbosity_info() logger.info("Training/evaluation parameters %s" , snake_case ) # Set seed before initializing model. set_seed(training_args.seed ) # Get the datasets: _lowercase : Tuple = datasets.load_dataset( "common_voice" , data_args.dataset_config_name , split=data_args.train_split_name ) _lowercase : Any = datasets.load_dataset("common_voice" , data_args.dataset_config_name , split="test" ) # Create and save tokenizer _lowercase : Dict = F'''[{''.join(data_args.chars_to_ignore )}]''' def remove_special_characters(snake_case ): _lowercase : List[str] = re.sub(snake_case , "" , batch["sentence"] ).lower() + " " return batch _lowercase : int = train_dataset.map(snake_case , remove_columns=["sentence"] ) _lowercase : int = eval_dataset.map(snake_case , remove_columns=["sentence"] ) def extract_all_chars(snake_case ): _lowercase : Optional[int] = " ".join(batch["text"] ) _lowercase : int = list(set(snake_case ) ) return {"vocab": [vocab], "all_text": [all_text]} _lowercase : Dict = train_dataset.map( snake_case , batched=snake_case , batch_size=-1 , keep_in_memory=snake_case , remove_columns=train_dataset.column_names , ) _lowercase : List[Any] = train_dataset.map( snake_case , batched=snake_case , batch_size=-1 , keep_in_memory=snake_case , remove_columns=eval_dataset.column_names , ) _lowercase : Dict = list(set(vocab_train["vocab"][0] ) | set(vocab_test["vocab"][0] ) ) _lowercase : List[str] = {v: k for k, v in enumerate(snake_case )} _lowercase : Union[str, Any] = vocab_dict[" "] del vocab_dict[" "] _lowercase : Dict = len(snake_case ) _lowercase : Dict = len(snake_case ) with open("vocab.json" , "w" ) as vocab_file: json.dump(snake_case , snake_case ) # Load pretrained model and tokenizer # # Distributed training: # The .from_pretrained methods guarantee that only one local process can concurrently # download model & vocab. _lowercase : Dict = WavaVecaCTCTokenizer( "vocab.json" , unk_token="[UNK]" , pad_token="[PAD]" , word_delimiter_token="|" , ) _lowercase : List[str] = WavaVecaFeatureExtractor( feature_size=1 , sampling_rate=1_60_00 , padding_value=0.0 , do_normalize=snake_case , return_attention_mask=snake_case ) _lowercase : Optional[int] = WavaVecaProcessor(feature_extractor=snake_case , tokenizer=snake_case ) _lowercase : int = WavaVecaForCTC.from_pretrained( model_args.model_name_or_path , cache_dir=model_args.cache_dir , activation_dropout=model_args.activation_dropout , attention_dropout=model_args.attention_dropout , hidden_dropout=model_args.hidden_dropout , feat_proj_dropout=model_args.feat_proj_dropout , mask_time_prob=model_args.mask_time_prob , gradient_checkpointing=training_args.gradient_checkpointing , layerdrop=model_args.layerdrop , ctc_loss_reduction="mean" , pad_token_id=processor.tokenizer.pad_token_id , vocab_size=len(processor.tokenizer ) , ) if data_args.max_train_samples is not None: _lowercase : Optional[Any] = min(len(snake_case ) , data_args.max_train_samples ) _lowercase : Any = train_dataset.select(range(snake_case ) ) if data_args.max_val_samples is not None: _lowercase : Any = eval_dataset.select(range(data_args.max_val_samples ) ) _lowercase : Dict = torchaudio.transforms.Resample(4_80_00 , 1_60_00 ) # Preprocessing the datasets. # We need to read the aduio files as arrays and tokenize the targets. def speech_file_to_array_fn(snake_case ): _lowercase , _lowercase : List[str] = torchaudio.load(batch["path"] ) _lowercase : List[Any] = resampler(snake_case ).squeeze().numpy() _lowercase : str = 1_60_00 _lowercase : Optional[int] = batch["text"] return batch _lowercase : Dict = train_dataset.map( snake_case , remove_columns=train_dataset.column_names , num_proc=data_args.preprocessing_num_workers , ) _lowercase : List[str] = eval_dataset.map( snake_case , remove_columns=eval_dataset.column_names , num_proc=data_args.preprocessing_num_workers , ) def prepare_dataset(snake_case ): # check that all files have the correct sampling rate assert ( len(set(batch["sampling_rate"] ) ) == 1 ), F'''Make sure all inputs have the same sampling rate of {processor.feature_extractor.sampling_rate}.''' _lowercase : str = processor( audio=batch["speech"] , text=batch["target_text"] , sampling_rate=batch["sampling_rate"][0] ) batch.update(snake_case ) return batch _lowercase : Any = train_dataset.map( snake_case , remove_columns=train_dataset.column_names , batch_size=training_args.per_device_train_batch_size , batched=snake_case , num_proc=data_args.preprocessing_num_workers , ) _lowercase : Dict = eval_dataset.map( snake_case , remove_columns=eval_dataset.column_names , batch_size=training_args.per_device_train_batch_size , batched=snake_case , num_proc=data_args.preprocessing_num_workers , ) # Metric _lowercase : Optional[int] = datasets.load_metric("wer" ) def compute_metrics(snake_case ): _lowercase : Dict = pred.predictions _lowercase : int = np.argmax(snake_case , axis=-1 ) _lowercase : str = processor.tokenizer.pad_token_id _lowercase : Optional[int] = processor.batch_decode(snake_case ) # we do not want to group tokens when computing the metrics _lowercase : int = processor.batch_decode(pred.label_ids , group_tokens=snake_case ) _lowercase : Optional[int] = wer_metric.compute(predictions=snake_case , references=snake_case ) return {"wer": wer} if model_args.freeze_feature_extractor: model.freeze_feature_extractor() # Data collator _lowercase : str = DataCollatorCTCWithPadding(processor=snake_case , padding=snake_case ) # Initialize our Trainer _lowercase : List[str] = CTCTrainer( model=snake_case , data_collator=snake_case , args=snake_case , compute_metrics=snake_case , train_dataset=train_dataset if training_args.do_train else None , eval_dataset=eval_dataset if training_args.do_eval else None , tokenizer=processor.feature_extractor , ) # Training if training_args.do_train: if last_checkpoint is not None: _lowercase : Any = last_checkpoint elif os.path.isdir(model_args.model_name_or_path ): _lowercase : Tuple = model_args.model_name_or_path else: _lowercase : Any = None # Save the feature_extractor and the tokenizer if is_main_process(training_args.local_rank ): processor.save_pretrained(training_args.output_dir ) _lowercase : str = trainer.train(resume_from_checkpoint=snake_case ) trainer.save_model() _lowercase : List[str] = train_result.metrics _lowercase : List[str] = ( data_args.max_train_samples if data_args.max_train_samples is not None else len(snake_case ) ) _lowercase : Any = min(snake_case , len(snake_case ) ) trainer.log_metrics("train" , snake_case ) trainer.save_metrics("train" , snake_case ) trainer.save_state() # Evaluation _lowercase : Dict = {} if training_args.do_eval: logger.info("*** Evaluate ***" ) _lowercase : Optional[Any] = trainer.evaluate() _lowercase : Optional[Any] = data_args.max_val_samples if data_args.max_val_samples is not None else len(snake_case ) _lowercase : Tuple = min(snake_case , len(snake_case ) ) trainer.log_metrics("eval" , snake_case ) trainer.save_metrics("eval" , snake_case ) return results if __name__ == "__main__": main()
199
1
'''simple docstring''' import warnings from typing import List, Optional, Tuple, Union import numpy as np import PIL import torch from ...models import UNetaDModel from ...schedulers import RePaintScheduler from ...utils import PIL_INTERPOLATION, logging, randn_tensor from ..pipeline_utils import DiffusionPipeline, ImagePipelineOutput UpperCAmelCase_ = logging.get_logger(__name__) # pylint: disable=invalid-name def _UpperCamelCase ( SCREAMING_SNAKE_CASE__ : Union[List, PIL.Image.Image, torch.Tensor] ): '''simple docstring''' warnings.warn( """The preprocess method is deprecated and will be removed in a future version. Please""" """ use VaeImageProcessor.preprocess instead""" , SCREAMING_SNAKE_CASE__ , ) if isinstance(SCREAMING_SNAKE_CASE__ , torch.Tensor ): return image elif isinstance(SCREAMING_SNAKE_CASE__ , PIL.Image.Image ): UpperCAmelCase__ = [image] if isinstance(image[0] , PIL.Image.Image ): UpperCAmelCase__ , UpperCAmelCase__ = image[0].size UpperCAmelCase__ , UpperCAmelCase__ = (x - x % 8 for x in (w, h)) # resize to integer multiple of 8 UpperCAmelCase__ = [np.array(i.resize((w, h) , resample=PIL_INTERPOLATION["""lanczos"""] ) )[None, :] for i in image] UpperCAmelCase__ = np.concatenate(SCREAMING_SNAKE_CASE__ , axis=0 ) UpperCAmelCase__ = np.array(SCREAMING_SNAKE_CASE__ ).astype(np.floataa ) / 2_55.0 UpperCAmelCase__ = image.transpose(0 , 3 , 1 , 2 ) UpperCAmelCase__ = 2.0 * image - 1.0 UpperCAmelCase__ = torch.from_numpy(SCREAMING_SNAKE_CASE__ ) elif isinstance(image[0] , torch.Tensor ): UpperCAmelCase__ = torch.cat(SCREAMING_SNAKE_CASE__ , dim=0 ) return image def _UpperCamelCase ( SCREAMING_SNAKE_CASE__ : Union[List, PIL.Image.Image, torch.Tensor] ): '''simple docstring''' if isinstance(SCREAMING_SNAKE_CASE__ , torch.Tensor ): return mask elif isinstance(SCREAMING_SNAKE_CASE__ , PIL.Image.Image ): UpperCAmelCase__ = [mask] if isinstance(mask[0] , PIL.Image.Image ): UpperCAmelCase__ , UpperCAmelCase__ = mask[0].size UpperCAmelCase__ , UpperCAmelCase__ = (x - x % 32 for x in (w, h)) # resize to integer multiple of 32 UpperCAmelCase__ = [np.array(m.convert("""L""" ).resize((w, h) , resample=PIL_INTERPOLATION["""nearest"""] ) )[None, :] for m in mask] UpperCAmelCase__ = np.concatenate(SCREAMING_SNAKE_CASE__ , axis=0 ) UpperCAmelCase__ = mask.astype(np.floataa ) / 2_55.0 UpperCAmelCase__ = 0 UpperCAmelCase__ = 1 UpperCAmelCase__ = torch.from_numpy(SCREAMING_SNAKE_CASE__ ) elif isinstance(mask[0] , torch.Tensor ): UpperCAmelCase__ = torch.cat(SCREAMING_SNAKE_CASE__ , dim=0 ) return mask class lowerCAmelCase_ ( lowerCamelCase_ ): '''simple docstring''' lowerCAmelCase_ : UNetaDModel lowerCAmelCase_ : RePaintScheduler def __init__( self : List[Any] , _UpperCAmelCase : str , _UpperCAmelCase : Dict ): """simple docstring""" super().__init__() self.register_modules(unet=_UpperCAmelCase , scheduler=_UpperCAmelCase ) @torch.no_grad() def __call__( self : int , _UpperCAmelCase : Union[torch.Tensor, PIL.Image.Image] , _UpperCAmelCase : Union[torch.Tensor, PIL.Image.Image] , _UpperCAmelCase : int = 2_50 , _UpperCAmelCase : float = 0.0 , _UpperCAmelCase : int = 10 , _UpperCAmelCase : int = 10 , _UpperCAmelCase : Optional[Union[torch.Generator, List[torch.Generator]]] = None , _UpperCAmelCase : Optional[str] = "pil" , _UpperCAmelCase : bool = True , ): """simple docstring""" UpperCAmelCase__ = image UpperCAmelCase__ = _preprocess_image(_UpperCAmelCase ) UpperCAmelCase__ = original_image.to(device=self.device , dtype=self.unet.dtype ) UpperCAmelCase__ = _preprocess_mask(_UpperCAmelCase ) UpperCAmelCase__ = mask_image.to(device=self.device , dtype=self.unet.dtype ) UpperCAmelCase__ = original_image.shape[0] # sample gaussian noise to begin the loop if isinstance(_UpperCAmelCase , _UpperCAmelCase ) and len(_UpperCAmelCase ) != batch_size: raise ValueError( f'''You have passed a list of generators of length {len(_UpperCAmelCase )}, but requested an effective batch''' f''' size of {batch_size}. Make sure the batch size matches the length of the generators.''' ) UpperCAmelCase__ = original_image.shape UpperCAmelCase__ = randn_tensor(_UpperCAmelCase , generator=_UpperCAmelCase , device=self.device , dtype=self.unet.dtype ) # set step values self.scheduler.set_timesteps(_UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , self.device ) UpperCAmelCase__ = eta UpperCAmelCase__ = self.scheduler.timesteps[0] + 1 UpperCAmelCase__ = generator[0] if isinstance(_UpperCAmelCase , _UpperCAmelCase ) else generator for i, t in enumerate(self.progress_bar(self.scheduler.timesteps ) ): if t < t_last: # predict the noise residual UpperCAmelCase__ = self.unet(_UpperCAmelCase , _UpperCAmelCase ).sample # compute previous image: x_t -> x_t-1 UpperCAmelCase__ = self.scheduler.step(_UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase ).prev_sample else: # compute the reverse: x_t-1 -> x_t UpperCAmelCase__ = self.scheduler.undo_step(_UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase ) UpperCAmelCase__ = t UpperCAmelCase__ = (image / 2 + 0.5).clamp(0 , 1 ) UpperCAmelCase__ = image.cpu().permute(0 , 2 , 3 , 1 ).numpy() if output_type == "pil": UpperCAmelCase__ = self.numpy_to_pil(_UpperCAmelCase ) if not return_dict: return (image,) return ImagePipelineOutput(images=_UpperCAmelCase )
346
'''simple docstring''' import itertools import os from collections import Counter, defaultdict from concurrent.futures import ThreadPoolExecutor, as_completed import numpy as np import datasets from .execute import check_correctness UpperCAmelCase_ = '\\n@misc{chen2021evaluating,\n title={Evaluating Large Language Models Trained on Code},\n author={Mark Chen and Jerry Tworek and Heewoo Jun and Qiming Yuan \\nand Henrique Ponde de Oliveira Pinto and Jared Kaplan and Harri Edwards \\nand Yuri Burda and Nicholas Joseph and Greg Brockman and Alex Ray \\nand Raul Puri and Gretchen Krueger and Michael Petrov and Heidy Khlaaf \\nand Girish Sastry and Pamela Mishkin and Brooke Chan and Scott Gray \\nand Nick Ryder and Mikhail Pavlov and Alethea Power and Lukasz Kaiser \\nand Mohammad Bavarian and Clemens Winter and Philippe Tillet \\nand Felipe Petroski Such and Dave Cummings and Matthias Plappert \\nand Fotios Chantzis and Elizabeth Barnes and Ariel Herbert-Voss \\nand William Hebgen Guss and Alex Nichol and Alex Paino and Nikolas Tezak \\nand Jie Tang and Igor Babuschkin and Suchir Balaji and Shantanu Jain \\nand William Saunders and Christopher Hesse and Andrew N. Carr \\nand Jan Leike and Josh Achiam and Vedant Misra and Evan Morikawa \\nand Alec Radford and Matthew Knight and Miles Brundage and Mira Murati \\nand Katie Mayer and Peter Welinder and Bob McGrew and Dario Amodei \\nand Sam McCandlish and Ilya Sutskever and Wojciech Zaremba},\n year={2021},\n eprint={2107.03374},\n archivePrefix={arXiv},\n primaryClass={cs.LG}\n}\n' UpperCAmelCase_ = '\\nThis metric implements the evaluation harness for the HumanEval problem solving dataset\ndescribed in the paper "Evaluating Large Language Models Trained on Code"\n(https://arxiv.org/abs/2107.03374).\n' UpperCAmelCase_ = '\nCalculates how good are predictions given some references, using certain scores\nArgs:\n predictions: list of candidates to evaluate. Each candidates should be a list\n of strings with several code candidates to solve the problem.\n references: a list with a test for each prediction. Each test should evaluate the\n correctness of a code candidate.\n k: number of code candidates to consider in the evaluation (Default: [1, 10, 100])\n num_workers: number of workers used to evaluate the canidate programs (Default: 4).\n timeout:\nReturns:\n pass_at_k: dict with pass rates for each k\n results: dict with granular results of each unittest\nExamples:\n >>> code_eval = datasets.load_metric("code_eval")\n >>> test_cases = ["assert add(2,3)==5"]\n >>> candidates = [["def add(a,b): return a*b", "def add(a, b): return a+b"]]\n >>> pass_at_k, results = code_eval.compute(references=test_cases, predictions=candidates, k=[1, 2])\n >>> print(pass_at_k)\n {\'pass@1\': 0.5, \'pass@2\': 1.0}\n' UpperCAmelCase_ = '\n################################################################################\n !!!WARNING!!!\n################################################################################\nThe "code_eval" metric executes untrusted model-generated code in Python.\nAlthough it is highly unlikely that model-generated code will do something\novertly malicious in response to this test suite, model-generated code may act\ndestructively due to a lack of model capability or alignment.\nUsers are strongly encouraged to sandbox this evaluation suite so that it\ndoes not perform destructive actions on their host or network. For more\ninformation on how OpenAI sandboxes its code, see the paper "Evaluating Large\nLanguage Models Trained on Code" (https://arxiv.org/abs/2107.03374).\n\nOnce you have read this disclaimer and taken appropriate precautions,\nset the environment variable HF_ALLOW_CODE_EVAL="1". Within Python you can to this\nwith:\n\n>>> import os\n>>> os.environ["HF_ALLOW_CODE_EVAL"] = "1"\n\n################################################################################\\n' UpperCAmelCase_ = 'The MIT License\n\nCopyright (c) OpenAI (https://openai.com)\n\nPermission is hereby granted, free of charge, to any person obtaining a copy\nof this software and associated documentation files (the "Software"), to deal\nin the Software without restriction, including without limitation the rights\nto use, copy, modify, merge, publish, distribute, sublicense, and/or sell\ncopies of the Software, and to permit persons to whom the Software is\nfurnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in\nall copies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\nAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\nOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN\nTHE SOFTWARE.' @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class lowerCAmelCase_ ( datasets.Metric ): '''simple docstring''' def SCREAMING_SNAKE_CASE__ ( self : Optional[Any] ): """simple docstring""" return datasets.MetricInfo( # This is the description that will appear on the metrics page. description=_DESCRIPTION , citation=_CITATION , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { """predictions""": datasets.Sequence(datasets.Value("""string""" ) ), """references""": datasets.Value("""string""" ), } ) , homepage="""https://github.com/openai/human-eval""" , codebase_urls=["""https://github.com/openai/human-eval"""] , reference_urls=["""https://github.com/openai/human-eval"""] , license=_LICENSE , ) def SCREAMING_SNAKE_CASE__ ( self : Optional[Any] , _UpperCAmelCase : Union[str, Any] , _UpperCAmelCase : Union[str, Any] , _UpperCAmelCase : List[str]=[1, 10, 1_00] , _UpperCAmelCase : Optional[Any]=4 , _UpperCAmelCase : Any=3.0 ): """simple docstring""" if os.getenv("""HF_ALLOW_CODE_EVAL""" , 0 ) != "1": raise ValueError(_WARNING ) if os.name == "nt": raise NotImplementedError("""This metric is currently not supported on Windows.""" ) with ThreadPoolExecutor(max_workers=_UpperCAmelCase ) as executor: UpperCAmelCase__ = [] UpperCAmelCase__ = Counter() UpperCAmelCase__ = 0 UpperCAmelCase__ = defaultdict(_UpperCAmelCase ) for task_id, (candidates, test_case) in enumerate(zip(_UpperCAmelCase , _UpperCAmelCase ) ): for candidate in candidates: UpperCAmelCase__ = candidate + """\n""" + test_case UpperCAmelCase__ = (test_program, timeout, task_id, completion_id[task_id]) UpperCAmelCase__ = executor.submit(_UpperCAmelCase , *_UpperCAmelCase ) futures.append(_UpperCAmelCase ) completion_id[task_id] += 1 n_samples += 1 for future in as_completed(_UpperCAmelCase ): UpperCAmelCase__ = future.result() results[result["task_id"]].append((result["""completion_id"""], result) ) UpperCAmelCase__ , UpperCAmelCase__ = [], [] for result in results.values(): result.sort() UpperCAmelCase__ = [r[1]["""passed"""] for r in result] total.append(len(_UpperCAmelCase ) ) correct.append(sum(_UpperCAmelCase ) ) UpperCAmelCase__ = np.array(_UpperCAmelCase ) UpperCAmelCase__ = np.array(_UpperCAmelCase ) UpperCAmelCase__ = k UpperCAmelCase__ = {f'''pass@{k}''': estimate_pass_at_k(_UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase ).mean() for k in ks if (total >= k).all()} return pass_at_k, results def _UpperCamelCase ( SCREAMING_SNAKE_CASE__ : List[Any] , SCREAMING_SNAKE_CASE__ : List[str] , SCREAMING_SNAKE_CASE__ : Optional[Any] ): '''simple docstring''' def estimator(SCREAMING_SNAKE_CASE__ : int , SCREAMING_SNAKE_CASE__ : int , SCREAMING_SNAKE_CASE__ : int ) -> float: if n - c < k: return 1.0 return 1.0 - np.prod(1.0 - k / np.arange(n - c + 1 , n + 1 ) ) if isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ): UpperCAmelCase__ = itertools.repeat(SCREAMING_SNAKE_CASE__ , len(SCREAMING_SNAKE_CASE__ ) ) else: assert len(SCREAMING_SNAKE_CASE__ ) == len(SCREAMING_SNAKE_CASE__ ) UpperCAmelCase__ = iter(SCREAMING_SNAKE_CASE__ ) return np.array([estimator(int(SCREAMING_SNAKE_CASE__ ) , int(SCREAMING_SNAKE_CASE__ ) , SCREAMING_SNAKE_CASE__ ) for n, c in zip(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ )] )
346
1
from ...configuration_utils import PretrainedConfig from ...utils import logging A_ :List[str] = logging.get_logger(__name__) A_ :Union[str, Any] = { '''studio-ousia/luke-base''': '''https://huggingface.co/studio-ousia/luke-base/resolve/main/config.json''', '''studio-ousia/luke-large''': '''https://huggingface.co/studio-ousia/luke-large/resolve/main/config.json''', } class __A ( a ): """simple docstring""" UpperCamelCase__ : Dict ="""luke""" def __init__( self , lowerCamelCase__=50267 , lowerCamelCase__=500000 , lowerCamelCase__=768 , lowerCamelCase__=256 , lowerCamelCase__=12 , lowerCamelCase__=12 , lowerCamelCase__=3072 , lowerCamelCase__="gelu" , lowerCamelCase__=0.1 , lowerCamelCase__=0.1 , lowerCamelCase__=512 , lowerCamelCase__=2 , lowerCamelCase__=0.02 , lowerCamelCase__=1E-12 , lowerCamelCase__=True , lowerCamelCase__=None , lowerCamelCase__=1 , lowerCamelCase__=0 , lowerCamelCase__=2 , **lowerCamelCase__ , ): """simple docstring""" super().__init__(pad_token_id=lowerCamelCase__ , bos_token_id=lowerCamelCase__ , eos_token_id=lowerCamelCase__ , **lowerCamelCase__ ) __UpperCamelCase : Tuple =vocab_size __UpperCamelCase : Optional[Any] =entity_vocab_size __UpperCamelCase : Dict =hidden_size __UpperCamelCase : Tuple =entity_emb_size __UpperCamelCase : Any =num_hidden_layers __UpperCamelCase : List[Any] =num_attention_heads __UpperCamelCase : Optional[Any] =hidden_act __UpperCamelCase : Any =intermediate_size __UpperCamelCase : Optional[int] =hidden_dropout_prob __UpperCamelCase : Optional[Any] =attention_probs_dropout_prob __UpperCamelCase : Union[str, Any] =max_position_embeddings __UpperCamelCase : List[Any] =type_vocab_size __UpperCamelCase : Optional[int] =initializer_range __UpperCamelCase : int =layer_norm_eps __UpperCamelCase : Optional[int] =use_entity_aware_attention __UpperCamelCase : Optional[Any] =classifier_dropout
245
def A ( a_ ) -> bool: return sum(i for i in range(1 ,number // 2 + 1 ) if number % i == 0 ) == number if __name__ == "__main__": print('''Program to check whether a number is a Perfect number or not...''') A_ :List[str] = int(input('''Enter number: ''').strip()) print(f"{number} is {'' if perfect(number) else 'not '}a Perfect Number.")
245
1
'''simple docstring''' from __future__ import annotations import time a_ : Optional[int] = list[tuple[int, int]] a_ : Any = [ [0, 0, 0, 0, 0, 0, 0], [0, 1, 0, 0, 0, 0, 0], # 0 are free path whereas 1's are obstacles [0, 0, 0, 0, 0, 0, 0], [0, 0, 1, 0, 0, 0, 0], [1, 0, 1, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 1, 0, 0], ] a_ : Dict = [[-1, 0], [0, -1], [1, 0], [0, 1]] # up, left, down, right class __UpperCamelCase : def __init__( self, lowerCAmelCase, lowerCAmelCase, lowerCAmelCase, lowerCAmelCase, lowerCAmelCase ): """simple docstring""" lowerCamelCase_ =pos_x lowerCamelCase_ =pos_y lowerCamelCase_ =(pos_y, pos_x) lowerCamelCase_ =goal_x lowerCamelCase_ =goal_y lowerCamelCase_ =parent class __UpperCamelCase : def __init__( self, lowerCAmelCase, lowerCAmelCase ): """simple docstring""" lowerCamelCase_ =Node(start[1], start[0], goal[1], goal[0], lowerCAmelCase ) lowerCamelCase_ =Node(goal[1], goal[0], goal[1], goal[0], lowerCAmelCase ) lowerCamelCase_ =[self.start] lowerCamelCase_ =False def lowercase__ ( self ): """simple docstring""" while self.node_queue: lowerCamelCase_ =self.node_queue.pop(0 ) if current_node.pos == self.target.pos: lowerCamelCase_ =True return self.retrace_path(lowerCAmelCase ) lowerCamelCase_ =self.get_successors(lowerCAmelCase ) for node in successors: self.node_queue.append(lowerCAmelCase ) if not self.reached: return [self.start.pos] return None def lowercase__ ( self, lowerCAmelCase ): """simple docstring""" lowerCamelCase_ =[] for action in delta: lowerCamelCase_ =parent.pos_x + action[1] lowerCamelCase_ =parent.pos_y + action[0] if not (0 <= pos_x <= len(grid[0] ) - 1 and 0 <= pos_y <= len(lowerCAmelCase ) - 1): continue if grid[pos_y][pos_x] != 0: continue successors.append( Node(lowerCAmelCase, lowerCAmelCase, self.target.pos_y, self.target.pos_x, lowerCAmelCase ) ) return successors def lowercase__ ( self, lowerCAmelCase ): """simple docstring""" lowerCamelCase_ =node lowerCamelCase_ =[] while current_node is not None: path.append((current_node.pos_y, current_node.pos_x) ) lowerCamelCase_ =current_node.parent path.reverse() return path class __UpperCamelCase : def __init__( self, lowerCAmelCase, lowerCAmelCase ): """simple docstring""" lowerCamelCase_ =BreadthFirstSearch(lowerCAmelCase, lowerCAmelCase ) lowerCamelCase_ =BreadthFirstSearch(lowerCAmelCase, lowerCAmelCase ) lowerCamelCase_ =False def lowercase__ ( self ): """simple docstring""" while self.fwd_bfs.node_queue or self.bwd_bfs.node_queue: lowerCamelCase_ =self.fwd_bfs.node_queue.pop(0 ) lowerCamelCase_ =self.bwd_bfs.node_queue.pop(0 ) if current_bwd_node.pos == current_fwd_node.pos: lowerCamelCase_ =True return self.retrace_bidirectional_path( lowerCAmelCase, lowerCAmelCase ) lowerCamelCase_ =current_bwd_node lowerCamelCase_ =current_fwd_node lowerCamelCase_ ={ self.fwd_bfs: self.fwd_bfs.get_successors(lowerCAmelCase ), self.bwd_bfs: self.bwd_bfs.get_successors(lowerCAmelCase ), } for bfs in [self.fwd_bfs, self.bwd_bfs]: for node in successors[bfs]: bfs.node_queue.append(lowerCAmelCase ) if not self.reached: return [self.fwd_bfs.start.pos] return None def lowercase__ ( self, lowerCAmelCase, lowerCAmelCase ): """simple docstring""" lowerCamelCase_ =self.fwd_bfs.retrace_path(lowerCAmelCase ) lowerCamelCase_ =self.bwd_bfs.retrace_path(lowerCAmelCase ) bwd_path.pop() bwd_path.reverse() lowerCamelCase_ =fwd_path + bwd_path return path if __name__ == "__main__": # all coordinates are given in format [y,x] import doctest doctest.testmod() a_ : Optional[int] = (0, 0) a_ : List[str] = (len(grid) - 1, len(grid[0]) - 1) for elem in grid: print(elem) a_ : int = time.time() a_ : List[Any] = BreadthFirstSearch(init, goal) a_ : Union[str, Any] = bfs.search() a_ : Union[str, Any] = time.time() - start_bfs_time print("""Unidirectional BFS computation time : """, bfs_time) a_ : Optional[Any] = time.time() a_ : Dict = BidirectionalBreadthFirstSearch(init, goal) a_ : Dict = bd_bfs.search() a_ : Optional[Any] = time.time() - start_bd_bfs_time print("""Bidirectional BFS computation time : """, bd_bfs_time)
75
'''simple docstring''' import operator def __magic_name__( lowerCamelCase, lowerCamelCase = False, lowerCamelCase = None): __lowerCAmelCase = operator.lt if reverse else operator.gt __lowerCAmelCase = solution or [] if not arr: return solution __lowerCAmelCase = [arr.pop(0)] for i, item in enumerate(lowerCamelCase): if _operator(lowerCamelCase, sublist[-1]): sublist.append(lowerCamelCase) arr.pop(lowerCamelCase) # merging sublist into solution list if not solution: solution.extend(lowerCamelCase) else: while sublist: __lowerCAmelCase = sublist.pop(0) for i, xx in enumerate(lowerCamelCase): if not _operator(lowerCamelCase, lowerCamelCase): solution.insert(lowerCamelCase, lowerCamelCase) break else: solution.append(lowerCamelCase) strand_sort(lowerCamelCase, lowerCamelCase, lowerCamelCase) return solution if __name__ == "__main__": assert strand_sort([4, 3, 5, 1, 2]) == [1, 2, 3, 4, 5] assert strand_sort([4, 3, 5, 1, 2], reverse=True) == [5, 4, 3, 2, 1]
174
0
'''simple docstring''' def SCREAMING_SNAKE_CASE__( _UpperCamelCase : int = 10_00 ) -> int: '''simple docstring''' UpperCamelCase__ = -1 UpperCamelCase__ = 0 for a in range(1 , n // 3 ): # Solving the two equations a**2+b**2=c**2 and a+b+c=N eliminating c UpperCamelCase__ = (n * n - 2 * a * n) // (2 * n - 2 * a) UpperCamelCase__ = n - a - b if c * c == (a * a + b * b): UpperCamelCase__ = a * b * c if candidate >= product: UpperCamelCase__ = candidate return product if __name__ == "__main__": print(F"""{solution() = }""")
361
'''simple docstring''' import argparse import json import subprocess def SCREAMING_SNAKE_CASE__( _UpperCamelCase : int , _UpperCamelCase : Tuple ) -> Union[str, Any]: '''simple docstring''' UpperCamelCase__ = [] UpperCamelCase__ = ( F'curl -H "Accept: application/vnd.github+json" -H "Authorization: Bearer {token}"' " https://api.github.com/repos/huggingface/transformers/actions/runners" ) UpperCamelCase__ = subprocess.run(_UpperCamelCase , shell=_UpperCamelCase , stdout=subprocess.PIPE ) UpperCamelCase__ = output.stdout.decode("utf-8" ) UpperCamelCase__ = json.loads(_UpperCamelCase ) UpperCamelCase__ = status["runners"] for runner in runners: if runner["name"] in target_runners: if runner["status"] == "offline": offline_runners.append(_UpperCamelCase ) # save the result so we can report them on Slack with open("offline_runners.txt" , "w" ) as fp: fp.write(json.dumps(_UpperCamelCase ) ) if len(_UpperCamelCase ) > 0: UpperCamelCase__ = "\n".join([x["name"] for x in offline_runners] ) raise ValueError(F'The following runners are offline:\n{failed}' ) if __name__ == "__main__": def SCREAMING_SNAKE_CASE__( _UpperCamelCase : Dict ) -> Optional[Any]: '''simple docstring''' return values.split("," ) __lowercase: str = argparse.ArgumentParser() # Required parameters parser.add_argument( "--target_runners", default=None, type=list_str, required=True, help="Comma-separated list of runners to check status.", ) parser.add_argument( "--token", default=None, type=str, required=True, help="A token that has actions:read permission." ) __lowercase: str = parser.parse_args() get_runner_status(args.target_runners, args.token)
31
0
import os import unicodedata from shutil import copyfile from typing import Any, Dict, List, Optional, Tuple import sentencepiece as spm from ...tokenization_utils import AddedToken, PreTrainedTokenizer from ...utils import SPIECE_UNDERLINE, logging A : str = logging.get_logger(__name__) A : Dict = {'vocab_file': 'spiece.model'} A : int = { 'vocab_file': { 'xlnet-base-cased': 'https://huggingface.co/xlnet-base-cased/resolve/main/spiece.model', 'xlnet-large-cased': 'https://huggingface.co/xlnet-large-cased/resolve/main/spiece.model', } } A : int = { 'xlnet-base-cased': None, 'xlnet-large-cased': None, } # Segments (not really needed) A : Optional[int] = 0 A : Union[str, Any] = 1 A : Tuple = 2 A : Dict = 3 A : str = 4 class __A( __lowerCAmelCase ): snake_case_ = VOCAB_FILES_NAMES snake_case_ = PRETRAINED_VOCAB_FILES_MAP snake_case_ = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES snake_case_ = '''left''' def __init__( self , _snake_case , _snake_case=False , _snake_case=True , _snake_case=False , _snake_case="<s>" , _snake_case="</s>" , _snake_case="<unk>" , _snake_case="<sep>" , _snake_case="<pad>" , _snake_case="<cls>" , _snake_case="<mask>" , _snake_case=["<eop>", "<eod>"] , _snake_case = None , **_snake_case , ) -> Tuple: '''simple docstring''' __a = AddedToken(lowerCamelCase_ , lstrip=lowerCamelCase_ , rstrip=lowerCamelCase_ ) if isinstance(lowerCamelCase_ , lowerCamelCase_ ) else mask_token __a = {} if sp_model_kwargs is None else sp_model_kwargs super().__init__( do_lower_case=lowerCamelCase_ , remove_space=lowerCamelCase_ , keep_accents=lowerCamelCase_ , bos_token=lowerCamelCase_ , eos_token=lowerCamelCase_ , unk_token=lowerCamelCase_ , sep_token=lowerCamelCase_ , pad_token=lowerCamelCase_ , cls_token=lowerCamelCase_ , mask_token=lowerCamelCase_ , additional_special_tokens=lowerCamelCase_ , sp_model_kwargs=self.sp_model_kwargs , **lowerCamelCase_ , ) __a = 3 __a = do_lower_case __a = remove_space __a = keep_accents __a = vocab_file __a = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(lowerCamelCase_ ) @property def SCREAMING_SNAKE_CASE_ ( self ) -> Any: '''simple docstring''' return len(self.sp_model ) def SCREAMING_SNAKE_CASE_ ( self ) -> List[Any]: '''simple docstring''' __a = {self.convert_ids_to_tokens(lowerCamelCase_ ): i for i in range(self.vocab_size )} vocab.update(self.added_tokens_encoder ) return vocab def __getstate__( self ) -> List[str]: '''simple docstring''' __a = self.__dict__.copy() __a = None return state def __setstate__( self , _snake_case ) -> Dict: '''simple docstring''' __a = d # for backward compatibility if not hasattr(self , '''sp_model_kwargs''' ): __a = {} __a = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(self.vocab_file ) def SCREAMING_SNAKE_CASE_ ( self , _snake_case ) -> Optional[int]: '''simple docstring''' if self.remove_space: __a = ''' '''.join(inputs.strip().split() ) else: __a = inputs __a = outputs.replace('''``''' , '''\"''' ).replace('''\'\'''' , '''\"''' ) if not self.keep_accents: __a = unicodedata.normalize('''NFKD''' , lowerCamelCase_ ) __a = ''''''.join([c for c in outputs if not unicodedata.combining(lowerCamelCase_ )] ) if self.do_lower_case: __a = outputs.lower() return outputs def SCREAMING_SNAKE_CASE_ ( self , _snake_case ) -> List[str]: '''simple docstring''' __a = self.preprocess_text(lowerCamelCase_ ) __a = self.sp_model.encode(lowerCamelCase_ , out_type=lowerCamelCase_ ) __a = [] for piece in pieces: if len(lowerCamelCase_ ) > 1 and piece[-1] == str(''',''' ) and piece[-2].isdigit(): __a = self.sp_model.EncodeAsPieces(piece[:-1].replace(lowerCamelCase_ , '''''' ) ) if piece[0] != SPIECE_UNDERLINE and cur_pieces[0][0] == SPIECE_UNDERLINE: if len(cur_pieces[0] ) == 1: __a = cur_pieces[1:] else: __a = cur_pieces[0][1:] cur_pieces.append(piece[-1] ) new_pieces.extend(lowerCamelCase_ ) else: new_pieces.append(lowerCamelCase_ ) return new_pieces def SCREAMING_SNAKE_CASE_ ( self , _snake_case ) -> List[str]: '''simple docstring''' return self.sp_model.PieceToId(lowerCamelCase_ ) def SCREAMING_SNAKE_CASE_ ( self , _snake_case ) -> Union[str, Any]: '''simple docstring''' return self.sp_model.IdToPiece(lowerCamelCase_ ) def SCREAMING_SNAKE_CASE_ ( self , _snake_case ) -> Union[str, Any]: '''simple docstring''' __a = ''''''.join(lowerCamelCase_ ).replace(lowerCamelCase_ , ''' ''' ).strip() return out_string def SCREAMING_SNAKE_CASE_ ( self , _snake_case , _snake_case = False , _snake_case = None , _snake_case = True , **_snake_case , ) -> Optional[Any]: '''simple docstring''' __a = kwargs.pop('''use_source_tokenizer''' , lowerCamelCase_ ) __a = self.convert_ids_to_tokens(lowerCamelCase_ , skip_special_tokens=lowerCamelCase_ ) # To avoid mixing byte-level and unicode for byte-level BPT # we need to build string separately for added tokens and byte-level tokens # cf. https://github.com/huggingface/transformers/issues/1133 __a = [] __a = [] for token in filtered_tokens: if skip_special_tokens and token in self.all_special_ids: continue if token in self.added_tokens_encoder: if current_sub_text: sub_texts.append(self.convert_tokens_to_string(lowerCamelCase_ ) ) __a = [] sub_texts.append(lowerCamelCase_ ) else: current_sub_text.append(lowerCamelCase_ ) if current_sub_text: sub_texts.append(self.convert_tokens_to_string(lowerCamelCase_ ) ) # Mimic the behavior of the Rust tokenizer: # By default, there are no spaces between special tokens __a = ''''''.join(lowerCamelCase_ ) __a = ( clean_up_tokenization_spaces if clean_up_tokenization_spaces is not None else self.clean_up_tokenization_spaces ) if clean_up_tokenization_spaces: __a = self.clean_up_tokenization(lowerCamelCase_ ) return clean_text else: return text def SCREAMING_SNAKE_CASE_ ( self , _snake_case , _snake_case = None ) -> int: '''simple docstring''' __a = [self.sep_token_id] __a = [self.cls_token_id] if token_ids_a is None: return token_ids_a + sep + cls return token_ids_a + sep + token_ids_a + sep + cls def SCREAMING_SNAKE_CASE_ ( self , _snake_case , _snake_case = None , _snake_case = False ) -> Optional[int]: '''simple docstring''' if already_has_special_tokens: return super().get_special_tokens_mask( token_ids_a=lowerCamelCase_ , token_ids_a=lowerCamelCase_ , already_has_special_tokens=lowerCamelCase_ ) if token_ids_a is not None: return ([0] * len(lowerCamelCase_ )) + [1] + ([0] * len(lowerCamelCase_ )) + [1, 1] return ([0] * len(lowerCamelCase_ )) + [1, 1] def SCREAMING_SNAKE_CASE_ ( self , _snake_case , _snake_case = None ) -> int: '''simple docstring''' __a = [self.sep_token_id] __a = [2] if token_ids_a is None: return len(token_ids_a + sep ) * [0] + cls_segment_id return len(token_ids_a + sep ) * [0] + len(token_ids_a + sep ) * [1] + cls_segment_id def SCREAMING_SNAKE_CASE_ ( self , _snake_case , _snake_case = None ) -> List[Any]: '''simple docstring''' if not os.path.isdir(lowerCamelCase_ ): logger.error(F"""Vocabulary path ({save_directory}) should be a directory""" ) return __a = os.path.join( lowerCamelCase_ , (filename_prefix + '''-''' if filename_prefix else '''''') + VOCAB_FILES_NAMES['''vocab_file'''] ) if os.path.abspath(self.vocab_file ) != os.path.abspath(lowerCamelCase_ ) and os.path.isfile(self.vocab_file ): copyfile(self.vocab_file , lowerCamelCase_ ) elif not os.path.isfile(self.vocab_file ): with open(lowerCamelCase_ , '''wb''' ) as fi: __a = self.sp_model.serialized_model_proto() fi.write(lowerCamelCase_ ) return (out_vocab_file,)
6
from __future__ import annotations from typing import Any class SCREAMING_SNAKE_CASE_ ( __lowerCAmelCase ): pass class SCREAMING_SNAKE_CASE_ : def __init__( self : List[Any] , lowerCamelCase_ : Any ): """simple docstring""" UpperCamelCase = data UpperCamelCase = None def __iter__( self : Optional[int] ): """simple docstring""" UpperCamelCase = self UpperCamelCase = [] while node: if node in visited: raise ContainsLoopError visited.append(lowerCamelCase_ ) yield node.data UpperCamelCase = node.next_node @property def lowerCamelCase_ ( self : List[Any] ): """simple docstring""" try: list(self ) return False except ContainsLoopError: return True if __name__ == "__main__": _SCREAMING_SNAKE_CASE = Node(1) _SCREAMING_SNAKE_CASE = Node(2) _SCREAMING_SNAKE_CASE = Node(3) _SCREAMING_SNAKE_CASE = Node(4) print(root_node.has_loop) # False _SCREAMING_SNAKE_CASE = root_node.next_node print(root_node.has_loop) # True _SCREAMING_SNAKE_CASE = Node(5) _SCREAMING_SNAKE_CASE = Node(6) _SCREAMING_SNAKE_CASE = Node(5) _SCREAMING_SNAKE_CASE = Node(6) print(root_node.has_loop) # False _SCREAMING_SNAKE_CASE = Node(1) print(root_node.has_loop) # False
343
0
'''simple docstring''' import argparse import os import evaluate import torch from datasets import load_dataset from torch.optim import AdamW from torch.utils.data import DataLoader from transformers import AutoModelForSequenceClassification, AutoTokenizer, get_linear_schedule_with_warmup, set_seed from accelerate import Accelerator, DistributedType from accelerate.local_sgd import LocalSGD ######################################################################## # This is a fully working simple example to use Accelerate # with LocalSGD, which is a method to synchronize model # parameters every K batches. It is different, but complementary # to gradient accumulation. # # This example trains a Bert base model on GLUE MRPC # in any of the following settings (with the same script): # - single CPU or single GPU # - multi GPUS (using PyTorch distributed mode) # - (multi) TPUs # - fp16 (mixed-precision) or fp32 (normal precision) # # To run it in each of these various modes, follow the instructions # in the readme for examples: # https://github.com/huggingface/accelerate/tree/main/examples # ######################################################################## _lowerCamelCase : Dict = 16 _lowerCamelCase : Any = 32 def __a ( UpperCAmelCase , UpperCAmelCase = 16 ) ->str: """simple docstring""" A = AutoTokenizer.from_pretrained("""bert-base-cased""" ) A = load_dataset("""glue""" , """mrpc""" ) def tokenize_function(UpperCAmelCase ): # max_length=None => use the model max length (it's actually the default) A = tokenizer(examples["""sentence1"""] , examples["""sentence2"""] , truncation=A__ , max_length=A__ ) return outputs # Apply the method we just defined to all the examples in all the splits of the dataset # starting with the main process first: with accelerator.main_process_first(): A = datasets.map( A__ , batched=A__ , remove_columns=["""idx""", """sentence1""", """sentence2"""] , ) # We also rename the 'label' column to 'labels' which is the expected name for labels by the models of the # transformers library A = tokenized_datasets.rename_column("""label""" , """labels""" ) def collate_fn(UpperCAmelCase ): # On TPU it's best to pad everything to the same length or training will be very slow. A = 128 if accelerator.distributed_type == DistributedType.TPU else None # When using mixed precision we want round multiples of 8/16 if accelerator.mixed_precision == "fp8": A = 16 elif accelerator.mixed_precision != "no": A = 8 else: A = None return tokenizer.pad( A__ , padding="""longest""" , max_length=A__ , pad_to_multiple_of=A__ , return_tensors="""pt""" , ) # Instantiate dataloaders. A = DataLoader( tokenized_datasets["""train"""] , shuffle=A__ , collate_fn=A__ , batch_size=A__ ) A = DataLoader( tokenized_datasets["""validation"""] , shuffle=A__ , collate_fn=A__ , batch_size=A__ ) return train_dataloader, eval_dataloader # For testing only if os.environ.get('TESTING_MOCKED_DATALOADERS', None) == "1": from accelerate.test_utils.training import mocked_dataloaders _lowerCamelCase : Union[str, Any] = mocked_dataloaders # noqa: F811 def __a ( UpperCAmelCase , UpperCAmelCase ) ->List[str]: """simple docstring""" if os.environ.get("""TESTING_MOCKED_DATALOADERS""" , A__ ) == "1": A = 2 # New Code # A = int(args.gradient_accumulation_steps ) A = int(args.local_sgd_steps ) # Initialize accelerator A = Accelerator( cpu=args.cpu , mixed_precision=args.mixed_precision , gradient_accumulation_steps=A__ ) if accelerator.distributed_type not in [DistributedType.NO, DistributedType.MULTI_CPU, DistributedType.MULTI_GPU]: raise NotImplementedError("""LocalSGD is supported only for CPUs and GPUs (no DeepSpeed or MegatronLM)""" ) # Sample hyper-parameters for learning rate, batch size, seed and a few other HPs A = config["""lr"""] A = int(config["""num_epochs"""] ) A = int(config["""seed"""] ) A = int(config["""batch_size"""] ) A = evaluate.load("""glue""" , """mrpc""" ) set_seed(A__ ) A , A = get_dataloaders(A__ , A__ ) # Instantiate the model (we build the model here so that the seed also control new weights initialization) A = AutoModelForSequenceClassification.from_pretrained("""bert-base-cased""" , return_dict=A__ ) # We could avoid this line since the accelerator is set with `device_placement=True` (default value). # Note that if you are placing tensors on devices manually, this line absolutely needs to be before the optimizer # creation otherwise training will not work on TPU (`accelerate` will kindly throw an error to make us aware of that). A = model.to(accelerator.device ) # Instantiate optimizer A = AdamW(params=model.parameters() , lr=A__ ) # Instantiate scheduler A = get_linear_schedule_with_warmup( optimizer=A__ , num_warmup_steps=100 , num_training_steps=(len(A__ ) * num_epochs) , ) # Prepare everything # There is no specific order to remember, we just need to unpack the objects in the same order we gave them to the # prepare method. A , A , A , A , A = accelerator.prepare( A__ , A__ , A__ , A__ , A__ ) # Now we train the model for epoch in range(A__ ): model.train() with LocalSGD( accelerator=A__ , model=A__ , local_sgd_steps=A__ , enabled=local_sgd_steps is not None ) as local_sgd: for step, batch in enumerate(A__ ): # We could avoid this line since we set the accelerator with `device_placement=True`. batch.to(accelerator.device ) # New code # # We use the new `accumulate` context manager to perform gradient accumulation # We also currently do not support TPUs nor advise it as bugs were found on the XLA side when running our tests. with accelerator.accumulate(A__ ): A = model(**A__ ) A = output.loss accelerator.backward(A__ ) optimizer.step() lr_scheduler.step() optimizer.zero_grad() # LocalSGD-specific line local_sgd.step() model.eval() for step, batch in enumerate(A__ ): # We could avoid this line since we set the accelerator with `device_placement=True`. batch.to(accelerator.device ) with torch.no_grad(): A = model(**A__ ) A = outputs.logits.argmax(dim=-1 ) A , A = accelerator.gather_for_metrics((predictions, batch["""labels"""]) ) metric.add_batch( predictions=A__ , references=A__ , ) A = metric.compute() # Use accelerator.print to print only on the main process. accelerator.print(f"""epoch {epoch}:""" , A__ ) def __a ( ) ->Tuple: """simple docstring""" A = argparse.ArgumentParser(description="""Simple example of training script.""" ) parser.add_argument( """--mixed_precision""" , type=A__ , default=A__ , choices=["""no""", """fp16""", """bf16""", """fp8"""] , help="""Whether to use mixed precision. Choose""" """between fp16 and bf16 (bfloat16). Bf16 requires PyTorch >= 1.10.""" """and an Nvidia Ampere GPU.""" , ) # New Code # parser.add_argument( """--gradient_accumulation_steps""" , type=A__ , default=1 , help="""The number of minibatches to be ran before gradients are accumulated.""" , ) parser.add_argument( """--local_sgd_steps""" , type=A__ , default=8 , help="""Number of local SGD steps or None to disable local SGD""" ) parser.add_argument("""--cpu""" , action="""store_true""" , help="""If passed, will train on the CPU.""" ) A = parser.parse_args() A = {"""lr""": 2E-5, """num_epochs""": 3, """seed""": 42, """batch_size""": 16} training_function(A__ , A__ ) if __name__ == "__main__": main()
358
'''simple docstring''' from binascii import hexlify from hashlib import shaaaa from os import urandom # RFC 3526 - More Modular Exponential (MODP) Diffie-Hellman groups for # Internet Key Exchange (IKE) https://tools.ietf.org/html/rfc3526 _lowerCamelCase : Any = { # 1536-bit 5: { 'prime': int( 'FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD1' + '29024E088A67CC74020BBEA63B139B22514A08798E3404DD' + 'EF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245' + 'E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7ED' + 'EE386BFB5A899FA5AE9F24117C4B1FE649286651ECE45B3D' + 'C2007CB8A163BF0598DA48361C55D39A69163FA8FD24CF5F' + '83655D23DCA3AD961C62F356208552BB9ED529077096966D' + '670C354E4ABC9804F1746C08CA237327FFFFFFFFFFFFFFFF', base=16, ), 'generator': 2, }, # 2048-bit 14: { 'prime': int( 'FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD1' + '29024E088A67CC74020BBEA63B139B22514A08798E3404DD' + 'EF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245' + 'E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7ED' + 'EE386BFB5A899FA5AE9F24117C4B1FE649286651ECE45B3D' + 'C2007CB8A163BF0598DA48361C55D39A69163FA8FD24CF5F' + '83655D23DCA3AD961C62F356208552BB9ED529077096966D' + '670C354E4ABC9804F1746C08CA18217C32905E462E36CE3B' + 'E39E772C180E86039B2783A2EC07A28FB5C55DF06F4C52C9' + 'DE2BCBF6955817183995497CEA956AE515D2261898FA0510' + '15728E5A8AACAA68FFFFFFFFFFFFFFFF', base=16, ), 'generator': 2, }, # 3072-bit 15: { 'prime': int( 'FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD1' + '29024E088A67CC74020BBEA63B139B22514A08798E3404DD' + 'EF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245' + 'E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7ED' + 'EE386BFB5A899FA5AE9F24117C4B1FE649286651ECE45B3D' + 'C2007CB8A163BF0598DA48361C55D39A69163FA8FD24CF5F' + '83655D23DCA3AD961C62F356208552BB9ED529077096966D' + '670C354E4ABC9804F1746C08CA18217C32905E462E36CE3B' + 'E39E772C180E86039B2783A2EC07A28FB5C55DF06F4C52C9' + 'DE2BCBF6955817183995497CEA956AE515D2261898FA0510' + '15728E5A8AAAC42DAD33170D04507A33A85521ABDF1CBA64' + 'ECFB850458DBEF0A8AEA71575D060C7DB3970F85A6E1E4C7' + 'ABF5AE8CDB0933D71E8C94E04A25619DCEE3D2261AD2EE6B' + 'F12FFA06D98A0864D87602733EC86A64521F2B18177B200C' + 'BBE117577A615D6C770988C0BAD946E208E24FA074E5AB31' + '43DB5BFCE0FD108E4B82D120A93AD2CAFFFFFFFFFFFFFFFF', base=16, ), 'generator': 2, }, # 4096-bit 16: { 'prime': int( 'FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD1' + '29024E088A67CC74020BBEA63B139B22514A08798E3404DD' + 'EF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245' + 'E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7ED' + 'EE386BFB5A899FA5AE9F24117C4B1FE649286651ECE45B3D' + 'C2007CB8A163BF0598DA48361C55D39A69163FA8FD24CF5F' + '83655D23DCA3AD961C62F356208552BB9ED529077096966D' + '670C354E4ABC9804F1746C08CA18217C32905E462E36CE3B' + 'E39E772C180E86039B2783A2EC07A28FB5C55DF06F4C52C9' + 'DE2BCBF6955817183995497CEA956AE515D2261898FA0510' + '15728E5A8AAAC42DAD33170D04507A33A85521ABDF1CBA64' + 'ECFB850458DBEF0A8AEA71575D060C7DB3970F85A6E1E4C7' + 'ABF5AE8CDB0933D71E8C94E04A25619DCEE3D2261AD2EE6B' + 'F12FFA06D98A0864D87602733EC86A64521F2B18177B200C' + 'BBE117577A615D6C770988C0BAD946E208E24FA074E5AB31' + '43DB5BFCE0FD108E4B82D120A92108011A723C12A787E6D7' + '88719A10BDBA5B2699C327186AF4E23C1A946834B6150BDA' + '2583E9CA2AD44CE8DBBBC2DB04DE8EF92E8EFC141FBECAA6' + '287C59474E6BC05D99B2964FA090C3A2233BA186515BE7ED' + '1F612970CEE2D7AFB81BDD762170481CD0069127D5B05AA9' + '93B4EA988D8FDDC186FFB7DC90A6C08F4DF435C934063199' + 'FFFFFFFFFFFFFFFF', base=16, ), 'generator': 2, }, # 6144-bit 17: { 'prime': int( 'FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD129024E08' + '8A67CC74020BBEA63B139B22514A08798E3404DDEF9519B3CD3A431B' + '302B0A6DF25F14374FE1356D6D51C245E485B576625E7EC6F44C42E9' + 'A637ED6B0BFF5CB6F406B7EDEE386BFB5A899FA5AE9F24117C4B1FE6' + '49286651ECE45B3DC2007CB8A163BF0598DA48361C55D39A69163FA8' + 'FD24CF5F83655D23DCA3AD961C62F356208552BB9ED529077096966D' + '670C354E4ABC9804F1746C08CA18217C32905E462E36CE3BE39E772C' + '180E86039B2783A2EC07A28FB5C55DF06F4C52C9DE2BCBF695581718' + '3995497CEA956AE515D2261898FA051015728E5A8AAAC42DAD33170D' + '04507A33A85521ABDF1CBA64ECFB850458DBEF0A8AEA71575D060C7D' + 'B3970F85A6E1E4C7ABF5AE8CDB0933D71E8C94E04A25619DCEE3D226' + '1AD2EE6BF12FFA06D98A0864D87602733EC86A64521F2B18177B200C' + 'BBE117577A615D6C770988C0BAD946E208E24FA074E5AB3143DB5BFC' + 'E0FD108E4B82D120A92108011A723C12A787E6D788719A10BDBA5B26' + '99C327186AF4E23C1A946834B6150BDA2583E9CA2AD44CE8DBBBC2DB' + '04DE8EF92E8EFC141FBECAA6287C59474E6BC05D99B2964FA090C3A2' + '233BA186515BE7ED1F612970CEE2D7AFB81BDD762170481CD0069127' + 'D5B05AA993B4EA988D8FDDC186FFB7DC90A6C08F4DF435C934028492' + '36C3FAB4D27C7026C1D4DCB2602646DEC9751E763DBA37BDF8FF9406' + 'AD9E530EE5DB382F413001AEB06A53ED9027D831179727B0865A8918' + 'DA3EDBEBCF9B14ED44CE6CBACED4BB1BDB7F1447E6CC254B33205151' + '2BD7AF426FB8F401378CD2BF5983CA01C64B92ECF032EA15D1721D03' + 'F482D7CE6E74FEF6D55E702F46980C82B5A84031900B1C9E59E7C97F' + 'BEC7E8F323A97A7E36CC88BE0F1D45B7FF585AC54BD407B22B4154AA' + 'CC8F6D7EBF48E1D814CC5ED20F8037E0A79715EEF29BE32806A1D58B' + 'B7C5DA76F550AA3D8A1FBFF0EB19CCB1A313D55CDA56C9EC2EF29632' + '387FE8D76E3C0468043E8F663F4860EE12BF2D5B0B7474D6E694F91E' + '6DCC4024FFFFFFFFFFFFFFFF', base=16, ), 'generator': 2, }, # 8192-bit 18: { 'prime': int( 'FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD1' + '29024E088A67CC74020BBEA63B139B22514A08798E3404DD' + 'EF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245' + 'E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7ED' + 'EE386BFB5A899FA5AE9F24117C4B1FE649286651ECE45B3D' + 'C2007CB8A163BF0598DA48361C55D39A69163FA8FD24CF5F' + '83655D23DCA3AD961C62F356208552BB9ED529077096966D' + '670C354E4ABC9804F1746C08CA18217C32905E462E36CE3B' + 'E39E772C180E86039B2783A2EC07A28FB5C55DF06F4C52C9' + 'DE2BCBF6955817183995497CEA956AE515D2261898FA0510' + '15728E5A8AAAC42DAD33170D04507A33A85521ABDF1CBA64' + 'ECFB850458DBEF0A8AEA71575D060C7DB3970F85A6E1E4C7' + 'ABF5AE8CDB0933D71E8C94E04A25619DCEE3D2261AD2EE6B' + 'F12FFA06D98A0864D87602733EC86A64521F2B18177B200C' + 'BBE117577A615D6C770988C0BAD946E208E24FA074E5AB31' + '43DB5BFCE0FD108E4B82D120A92108011A723C12A787E6D7' + '88719A10BDBA5B2699C327186AF4E23C1A946834B6150BDA' + '2583E9CA2AD44CE8DBBBC2DB04DE8EF92E8EFC141FBECAA6' + '287C59474E6BC05D99B2964FA090C3A2233BA186515BE7ED' + '1F612970CEE2D7AFB81BDD762170481CD0069127D5B05AA9' + '93B4EA988D8FDDC186FFB7DC90A6C08F4DF435C934028492' + '36C3FAB4D27C7026C1D4DCB2602646DEC9751E763DBA37BD' + 'F8FF9406AD9E530EE5DB382F413001AEB06A53ED9027D831' + '179727B0865A8918DA3EDBEBCF9B14ED44CE6CBACED4BB1B' + 'DB7F1447E6CC254B332051512BD7AF426FB8F401378CD2BF' + '5983CA01C64B92ECF032EA15D1721D03F482D7CE6E74FEF6' + 'D55E702F46980C82B5A84031900B1C9E59E7C97FBEC7E8F3' + '23A97A7E36CC88BE0F1D45B7FF585AC54BD407B22B4154AA' + 'CC8F6D7EBF48E1D814CC5ED20F8037E0A79715EEF29BE328' + '06A1D58BB7C5DA76F550AA3D8A1FBFF0EB19CCB1A313D55C' + 'DA56C9EC2EF29632387FE8D76E3C0468043E8F663F4860EE' + '12BF2D5B0B7474D6E694F91E6DBE115974A3926F12FEE5E4' + '38777CB6A932DF8CD8BEC4D073B931BA3BC832B68D9DD300' + '741FA7BF8AFC47ED2576F6936BA424663AAB639C5AE4F568' + '3423B4742BF1C978238F16CBE39D652DE3FDB8BEFC848AD9' + '22222E04A4037C0713EB57A81A23F0C73473FC646CEA306B' + '4BCBC8862F8385DDFA9D4B7FA2C087E879683303ED5BDD3A' + '062B3CF5B3A278A66D2A13F83F44F82DDF310EE074AB6A36' + '4597E899A0255DC164F31CC50846851DF9AB48195DED7EA1' + 'B1D510BD7EE74D73FAF36BC31ECFA268359046F4EB879F92' + '4009438B481C6CD7889A002ED5EE382BC9190DA6FC026E47' + '9558E4475677E9AA9E3050E2765694DFC81F56E880B96E71' + '60C980DD98EDD3DFFFFFFFFFFFFFFFFF', base=16, ), 'generator': 2, }, } class __UpperCAmelCase : '''simple docstring''' def __init__(self : int , _lowerCAmelCase : int = 14 ): if group not in primes: raise ValueError("""Unsupported Group""" ) A = primes[group]["""prime"""] A = primes[group]["""generator"""] A = int(hexlify(urandom(32 ) ) , base=16 ) def A (self : Optional[Any] ): return hex(self.__private_key )[2:] def A (self : Union[str, Any] ): A = pow(self.generator , self.__private_key , self.prime ) return hex(_lowerCAmelCase )[2:] def A (self : Any , _lowerCAmelCase : int ): # check if the other public key is valid based on NIST SP800-56 return ( 2 <= key <= self.prime - 2 and pow(_lowerCAmelCase , (self.prime - 1) // 2 , self.prime ) == 1 ) def A (self : List[str] , _lowerCAmelCase : str ): A = int(_lowerCAmelCase , base=16 ) if not self.is_valid_public_key(_lowerCAmelCase ): raise ValueError("""Invalid public key""" ) A = pow(_lowerCAmelCase , self.__private_key , self.prime ) return shaaaa(str(_lowerCAmelCase ).encode() ).hexdigest() @staticmethod def A (_lowerCAmelCase : int , _lowerCAmelCase : int ): # check if the other public key is valid based on NIST SP800-56 return ( 2 <= remote_public_key_str <= prime - 2 and pow(_lowerCAmelCase , (prime - 1) // 2 , _lowerCAmelCase ) == 1 ) @staticmethod def A (_lowerCAmelCase : str , _lowerCAmelCase : str , _lowerCAmelCase : int = 14 ): A = int(_lowerCAmelCase , base=16 ) A = int(_lowerCAmelCase , base=16 ) A = primes[group]["""prime"""] if not DiffieHellman.is_valid_public_key_static(_lowerCAmelCase , _lowerCAmelCase ): raise ValueError("""Invalid public key""" ) A = pow(_lowerCAmelCase , _lowerCAmelCase , _lowerCAmelCase ) return shaaaa(str(_lowerCAmelCase ).encode() ).hexdigest() if __name__ == "__main__": import doctest doctest.testmod()
337
0
import importlib import json import os import sys import tempfile import unittest from pathlib import Path import transformers import transformers.models.auto from transformers.models.auto.configuration_auto import CONFIG_MAPPING, AutoConfig from transformers.models.bert.configuration_bert import BertConfig from transformers.models.roberta.configuration_roberta import RobertaConfig from transformers.testing_utils import DUMMY_UNKNOWN_IDENTIFIER, get_tests_dir sys.path.append(str(Path(__file__).parent.parent.parent.parent / "utils")) from test_module.custom_configuration import CustomConfig # noqa E402 lowercase__ : Dict = get_tests_dir("fixtures/dummy-config.json") class SCREAMING_SNAKE_CASE__ ( unittest.TestCase ): """simple docstring""" def A__ ( self )-> Optional[Any]: '''simple docstring''' __UpperCamelCase = 0 def A__ ( self )-> Optional[int]: '''simple docstring''' self.assertIsNotNone(transformers.models.auto.__spec__ ) self.assertIsNotNone(importlib.util.find_spec('''transformers.models.auto''' ) ) def A__ ( self )-> Optional[int]: '''simple docstring''' __UpperCamelCase = AutoConfig.from_pretrained('''bert-base-uncased''' ) self.assertIsInstance(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) def A__ ( self )-> Tuple: '''simple docstring''' __UpperCamelCase = AutoConfig.from_pretrained(SCREAMING_SNAKE_CASE_ ) self.assertIsInstance(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) def A__ ( self )-> Any: '''simple docstring''' __UpperCamelCase = AutoConfig.from_pretrained(SCREAMING_SNAKE_CASE_ ) self.assertIsInstance(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) def A__ ( self )-> str: '''simple docstring''' __UpperCamelCase = AutoConfig.for_model('''roberta''' ) self.assertIsInstance(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) def A__ ( self )-> Optional[Any]: '''simple docstring''' with tempfile.TemporaryDirectory() as tmp_dir: # This model name contains bert and roberta, but roberta ends up being picked. __UpperCamelCase = os.path.join(SCREAMING_SNAKE_CASE_ , '''fake-roberta''' ) os.makedirs(SCREAMING_SNAKE_CASE_ , exist_ok=SCREAMING_SNAKE_CASE_ ) with open(os.path.join(SCREAMING_SNAKE_CASE_ , '''config.json''' ) , '''w''' ) as f: f.write(json.dumps({} ) ) __UpperCamelCase = AutoConfig.from_pretrained(SCREAMING_SNAKE_CASE_ ) self.assertEqual(type(SCREAMING_SNAKE_CASE_ ) , SCREAMING_SNAKE_CASE_ ) def A__ ( self )-> Any: '''simple docstring''' try: AutoConfig.register('''custom''' , SCREAMING_SNAKE_CASE_ ) # Wrong model type will raise an error with self.assertRaises(SCREAMING_SNAKE_CASE_ ): AutoConfig.register('''model''' , SCREAMING_SNAKE_CASE_ ) # Trying to register something existing in the Transformers library will raise an error with self.assertRaises(SCREAMING_SNAKE_CASE_ ): AutoConfig.register('''bert''' , SCREAMING_SNAKE_CASE_ ) # Now that the config is registered, it can be used as any other config with the auto-API __UpperCamelCase = CustomConfig() with tempfile.TemporaryDirectory() as tmp_dir: config.save_pretrained(SCREAMING_SNAKE_CASE_ ) __UpperCamelCase = AutoConfig.from_pretrained(SCREAMING_SNAKE_CASE_ ) self.assertIsInstance(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) finally: if "custom" in CONFIG_MAPPING._extra_content: del CONFIG_MAPPING._extra_content["custom"] def A__ ( self )-> Dict: '''simple docstring''' with self.assertRaisesRegex( SCREAMING_SNAKE_CASE_ , '''bert-base is not a local folder and is not a valid model identifier''' ): __UpperCamelCase = AutoConfig.from_pretrained('''bert-base''' ) def A__ ( self )-> Optional[int]: '''simple docstring''' with self.assertRaisesRegex( SCREAMING_SNAKE_CASE_ , r'''aaaaaa is not a valid git identifier \(branch name, tag name or commit id\)''' ): __UpperCamelCase = AutoConfig.from_pretrained(SCREAMING_SNAKE_CASE_ , revision='''aaaaaa''' ) def A__ ( self )-> Optional[Any]: '''simple docstring''' with self.assertRaisesRegex( SCREAMING_SNAKE_CASE_ , '''hf-internal-testing/no-config-test-repo does not appear to have a file named config.json.''' , ): __UpperCamelCase = AutoConfig.from_pretrained('''hf-internal-testing/no-config-test-repo''' ) def A__ ( self )-> Tuple: '''simple docstring''' with self.assertRaises(SCREAMING_SNAKE_CASE_ ): __UpperCamelCase = AutoConfig.from_pretrained('''hf-internal-testing/test_dynamic_model''' ) # If remote code is disabled, we can't load this config. with self.assertRaises(SCREAMING_SNAKE_CASE_ ): __UpperCamelCase = AutoConfig.from_pretrained('''hf-internal-testing/test_dynamic_model''' , trust_remote_code=SCREAMING_SNAKE_CASE_ ) __UpperCamelCase = AutoConfig.from_pretrained('''hf-internal-testing/test_dynamic_model''' , trust_remote_code=SCREAMING_SNAKE_CASE_ ) self.assertEqual(config.__class__.__name__ , '''NewModelConfig''' ) # Test config can be reloaded. with tempfile.TemporaryDirectory() as tmp_dir: config.save_pretrained(SCREAMING_SNAKE_CASE_ ) __UpperCamelCase = AutoConfig.from_pretrained(SCREAMING_SNAKE_CASE_ , trust_remote_code=SCREAMING_SNAKE_CASE_ ) self.assertEqual(reloaded_config.__class__.__name__ , '''NewModelConfig''' ) def A__ ( self )-> Any: '''simple docstring''' class SCREAMING_SNAKE_CASE__ ( SCREAMING_SNAKE_CASE_ ): """simple docstring""" _snake_case = 'new-model' try: AutoConfig.register('''new-model''' , SCREAMING_SNAKE_CASE_ ) # If remote code is not set, the default is to use local __UpperCamelCase = AutoConfig.from_pretrained('''hf-internal-testing/test_dynamic_model''' ) self.assertEqual(config.__class__.__name__ , '''NewModelConfigLocal''' ) # If remote code is disabled, we load the local one. __UpperCamelCase = AutoConfig.from_pretrained('''hf-internal-testing/test_dynamic_model''' , trust_remote_code=SCREAMING_SNAKE_CASE_ ) self.assertEqual(config.__class__.__name__ , '''NewModelConfigLocal''' ) # If remote is enabled, we load from the Hub __UpperCamelCase = AutoConfig.from_pretrained('''hf-internal-testing/test_dynamic_model''' , trust_remote_code=SCREAMING_SNAKE_CASE_ ) self.assertEqual(config.__class__.__name__ , '''NewModelConfig''' ) finally: if "new-model" in CONFIG_MAPPING._extra_content: del CONFIG_MAPPING._extra_content["new-model"]
328
from collections import OrderedDict from typing import TYPE_CHECKING, Any, Mapping, Optional, Union from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig, OnnxSeqaSeqConfigWithPast from ...utils import logging if TYPE_CHECKING: from ...feature_extraction_utils import FeatureExtractionMixin from ...tokenization_utils_base import PreTrainedTokenizerBase from ...utils import TensorType lowercase__ : str = logging.get_logger(__name__) lowercase__ : Union[str, Any] = { "openai/whisper-base": "https://huggingface.co/openai/whisper-base/resolve/main/config.json", } # fmt: off lowercase__ : str = [ 1, 2, 7, 8, 9, 1_0, 1_4, 2_5, 2_6, 2_7, 2_8, 2_9, 3_1, 5_8, 5_9, 6_0, 6_1, 6_2, 6_3, 9_0, 9_1, 9_2, 9_3, 3_5_7, 3_6_6, 4_3_8, 5_3_2, 6_8_5, 7_0_5, 7_9_6, 9_3_0, 1_0_5_8, 1_2_2_0, 1_2_6_7, 1_2_7_9, 1_3_0_3, 1_3_4_3, 1_3_7_7, 1_3_9_1, 1_6_3_5, 1_7_8_2, 1_8_7_5, 2_1_6_2, 2_3_6_1, 2_4_8_8, 3_4_6_7, 4_0_0_8, 4_2_1_1, 4_6_0_0, 4_8_0_8, 5_2_9_9, 5_8_5_5, 6_3_2_9, 7_2_0_3, 9_6_0_9, 9_9_5_9, 1_0_5_6_3, 1_0_7_8_6, 1_1_4_2_0, 1_1_7_0_9, 1_1_9_0_7, 1_3_1_6_3, 1_3_6_9_7, 1_3_7_0_0, 1_4_8_0_8, 1_5_3_0_6, 1_6_4_1_0, 1_6_7_9_1, 1_7_9_9_2, 1_9_2_0_3, 1_9_5_1_0, 2_0_7_2_4, 2_2_3_0_5, 2_2_9_3_5, 2_7_0_0_7, 3_0_1_0_9, 3_0_4_2_0, 3_3_4_0_9, 3_4_9_4_9, 4_0_2_8_3, 4_0_4_9_3, 4_0_5_4_9, 4_7_2_8_2, 4_9_1_4_6, 5_0_2_5_7, 5_0_3_5_9, 5_0_3_6_0, 5_0_3_6_1 ] lowercase__ : str = [ 1, 2, 7, 8, 9, 1_0, 1_4, 2_5, 2_6, 2_7, 2_8, 2_9, 3_1, 5_8, 5_9, 6_0, 6_1, 6_2, 6_3, 9_0, 9_1, 9_2, 9_3, 3_5_9, 5_0_3, 5_2_2, 5_4_2, 8_7_3, 8_9_3, 9_0_2, 9_1_8, 9_2_2, 9_3_1, 1_3_5_0, 1_8_5_3, 1_9_8_2, 2_4_6_0, 2_6_2_7, 3_2_4_6, 3_2_5_3, 3_2_6_8, 3_5_3_6, 3_8_4_6, 3_9_6_1, 4_1_8_3, 4_6_6_7, 6_5_8_5, 6_6_4_7, 7_2_7_3, 9_0_6_1, 9_3_8_3, 1_0_4_2_8, 1_0_9_2_9, 1_1_9_3_8, 1_2_0_3_3, 1_2_3_3_1, 1_2_5_6_2, 1_3_7_9_3, 1_4_1_5_7, 1_4_6_3_5, 1_5_2_6_5, 1_5_6_1_8, 1_6_5_5_3, 1_6_6_0_4, 1_8_3_6_2, 1_8_9_5_6, 2_0_0_7_5, 2_1_6_7_5, 2_2_5_2_0, 2_6_1_3_0, 2_6_1_6_1, 2_6_4_3_5, 2_8_2_7_9, 2_9_4_6_4, 3_1_6_5_0, 3_2_3_0_2, 3_2_4_7_0, 3_6_8_6_5, 4_2_8_6_3, 4_7_4_2_5, 4_9_8_7_0, 5_0_2_5_4, 5_0_2_5_8, 5_0_3_6_0, 5_0_3_6_1, 5_0_3_6_2 ] class SCREAMING_SNAKE_CASE__ ( SCREAMING_SNAKE_CASE_ ): """simple docstring""" _snake_case = 'whisper' _snake_case = ['past_key_values'] _snake_case = {'num_attention_heads': 'encoder_attention_heads', 'hidden_size': 'd_model'} def __init__( self , SCREAMING_SNAKE_CASE_=51865 , SCREAMING_SNAKE_CASE_=80 , SCREAMING_SNAKE_CASE_=6 , SCREAMING_SNAKE_CASE_=4 , SCREAMING_SNAKE_CASE_=6 , SCREAMING_SNAKE_CASE_=4 , SCREAMING_SNAKE_CASE_=1536 , SCREAMING_SNAKE_CASE_=1536 , SCREAMING_SNAKE_CASE_=0.0 , SCREAMING_SNAKE_CASE_=0.0 , SCREAMING_SNAKE_CASE_=50257 , SCREAMING_SNAKE_CASE_=True , SCREAMING_SNAKE_CASE_=True , SCREAMING_SNAKE_CASE_="gelu" , SCREAMING_SNAKE_CASE_=256 , SCREAMING_SNAKE_CASE_=0.0 , SCREAMING_SNAKE_CASE_=0.0 , SCREAMING_SNAKE_CASE_=0.0 , SCREAMING_SNAKE_CASE_=0.0_2 , SCREAMING_SNAKE_CASE_=False , SCREAMING_SNAKE_CASE_=1500 , SCREAMING_SNAKE_CASE_=448 , SCREAMING_SNAKE_CASE_=50256 , SCREAMING_SNAKE_CASE_=50256 , SCREAMING_SNAKE_CASE_=50256 , SCREAMING_SNAKE_CASE_=None , SCREAMING_SNAKE_CASE_=[220, 50256] , SCREAMING_SNAKE_CASE_=False , SCREAMING_SNAKE_CASE_=256 , SCREAMING_SNAKE_CASE_=False , SCREAMING_SNAKE_CASE_=0.0_5 , SCREAMING_SNAKE_CASE_=10 , SCREAMING_SNAKE_CASE_=2 , SCREAMING_SNAKE_CASE_=0.0 , SCREAMING_SNAKE_CASE_=10 , SCREAMING_SNAKE_CASE_=0 , SCREAMING_SNAKE_CASE_=7 , **SCREAMING_SNAKE_CASE_ , )-> Union[str, Any]: '''simple docstring''' __UpperCamelCase = vocab_size __UpperCamelCase = num_mel_bins __UpperCamelCase = d_model __UpperCamelCase = encoder_layers __UpperCamelCase = encoder_attention_heads __UpperCamelCase = decoder_layers __UpperCamelCase = decoder_attention_heads __UpperCamelCase = decoder_ffn_dim __UpperCamelCase = encoder_ffn_dim __UpperCamelCase = dropout __UpperCamelCase = attention_dropout __UpperCamelCase = activation_dropout __UpperCamelCase = activation_function __UpperCamelCase = init_std __UpperCamelCase = encoder_layerdrop __UpperCamelCase = decoder_layerdrop __UpperCamelCase = use_cache __UpperCamelCase = encoder_layers __UpperCamelCase = scale_embedding # scale factor will be sqrt(d_model) if True __UpperCamelCase = max_source_positions __UpperCamelCase = max_target_positions # Audio Classification-specific parameters. Feel free to ignore for other classes. __UpperCamelCase = classifier_proj_size __UpperCamelCase = use_weighted_layer_sum # fine-tuning config parameters for SpecAugment: https://arxiv.org/abs/1904.08779 __UpperCamelCase = apply_spec_augment __UpperCamelCase = mask_time_prob __UpperCamelCase = mask_time_length __UpperCamelCase = mask_time_min_masks __UpperCamelCase = mask_feature_prob __UpperCamelCase = mask_feature_length __UpperCamelCase = mask_feature_min_masks __UpperCamelCase = median_filter_width super().__init__( pad_token_id=SCREAMING_SNAKE_CASE_ , bos_token_id=SCREAMING_SNAKE_CASE_ , eos_token_id=SCREAMING_SNAKE_CASE_ , is_encoder_decoder=SCREAMING_SNAKE_CASE_ , decoder_start_token_id=SCREAMING_SNAKE_CASE_ , suppress_tokens=SCREAMING_SNAKE_CASE_ , begin_suppress_tokens=SCREAMING_SNAKE_CASE_ , **SCREAMING_SNAKE_CASE_ , ) class SCREAMING_SNAKE_CASE__ ( SCREAMING_SNAKE_CASE_ ): """simple docstring""" @property def A__ ( self )-> Mapping[str, Mapping[int, str]]: '''simple docstring''' __UpperCamelCase = OrderedDict( [ ('''input_features''', {0: '''batch''', 1: '''feature_size''', 2: '''encoder_sequence'''}), ] ) if self.use_past: __UpperCamelCase = {0: '''batch'''} else: __UpperCamelCase = {0: '''batch''', 1: '''decoder_sequence'''} if self.use_past: self.fill_with_past_key_values_(SCREAMING_SNAKE_CASE_ , direction='''inputs''' ) return common_inputs def A__ ( self , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = -1 , SCREAMING_SNAKE_CASE_ = -1 , SCREAMING_SNAKE_CASE_ = False , SCREAMING_SNAKE_CASE_ = None , SCREAMING_SNAKE_CASE_ = 22050 , SCREAMING_SNAKE_CASE_ = 5.0 , SCREAMING_SNAKE_CASE_ = 220 , )-> Mapping[str, Any]: '''simple docstring''' __UpperCamelCase = OrderedDict() __UpperCamelCase = OnnxConfig.generate_dummy_inputs( self , preprocessor=preprocessor.feature_extractor , batch_size=SCREAMING_SNAKE_CASE_ , framework=SCREAMING_SNAKE_CASE_ , sampling_rate=SCREAMING_SNAKE_CASE_ , time_duration=SCREAMING_SNAKE_CASE_ , frequency=SCREAMING_SNAKE_CASE_ , ) __UpperCamelCase = encoder_inputs['''input_features'''].shape[2] __UpperCamelCase = encoder_sequence_length // 2 if self.use_past else seq_length __UpperCamelCase = super().generate_dummy_inputs( preprocessor.tokenizer , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) __UpperCamelCase = encoder_inputs.pop('''input_features''' ) __UpperCamelCase = decoder_inputs.pop('''decoder_input_ids''' ) if "past_key_values" in decoder_inputs: __UpperCamelCase = decoder_inputs.pop('''past_key_values''' ) return dummy_inputs @property def A__ ( self )-> float: '''simple docstring''' return 1E-3
328
1
'''simple docstring''' def __UpperCamelCase ( _UpperCAmelCase = 1000 ): __UpperCAmelCase , __UpperCAmelCase : Optional[Any] = 1, 1 __UpperCAmelCase : Any = [] for i in range(1, n + 1 ): __UpperCAmelCase : Union[str, Any] = prev_numerator + 2 * prev_denominator __UpperCAmelCase : List[Any] = prev_numerator + prev_denominator if len(str(_UpperCAmelCase ) ) > len(str(_UpperCAmelCase ) ): result.append(_UpperCAmelCase ) __UpperCAmelCase : Union[str, Any] = numerator __UpperCAmelCase : Union[str, Any] = denominator return len(_UpperCAmelCase ) if __name__ == "__main__": print(f"{solution() = }")
37
'''simple docstring''' from dataclasses import dataclass from typing import Optional, Tuple, Union import torch import torch.nn as nn from ..configuration_utils import ConfigMixin, register_to_config from ..utils import BaseOutput from .embeddings import GaussianFourierProjection, TimestepEmbedding, Timesteps from .modeling_utils import ModelMixin from .unet_ad_blocks import get_down_block, get_mid_block, get_out_block, get_up_block @dataclass class SCREAMING_SNAKE_CASE__ ( snake_case__ ): """simple docstring""" SCREAMING_SNAKE_CASE = 42 class SCREAMING_SNAKE_CASE__ ( snake_case__ ,snake_case__ ): """simple docstring""" @register_to_config def __init__( self : Union[str, Any] , UpperCAmelCase_ : int = 65_536 , UpperCAmelCase_ : Optional[int] = None , UpperCAmelCase_ : int = 2 , UpperCAmelCase_ : int = 2 , UpperCAmelCase_ : int = 0 , UpperCAmelCase_ : str = "fourier" , UpperCAmelCase_ : bool = True , UpperCAmelCase_ : bool = False , UpperCAmelCase_ : float = 0.0 , UpperCAmelCase_ : Tuple[str] = ("DownBlock1DNoSkip", "DownBlock1D", "AttnDownBlock1D") , UpperCAmelCase_ : Tuple[str] = ("AttnUpBlock1D", "UpBlock1D", "UpBlock1DNoSkip") , UpperCAmelCase_ : Tuple[str] = "UNetMidBlock1D" , UpperCAmelCase_ : str = None , UpperCAmelCase_ : Tuple[int] = (32, 32, 64) , UpperCAmelCase_ : str = None , UpperCAmelCase_ : int = 8 , UpperCAmelCase_ : int = 1 , UpperCAmelCase_ : bool = False , ): """simple docstring""" super().__init__() __UpperCAmelCase : str = sample_size # time if time_embedding_type == "fourier": __UpperCAmelCase : int = GaussianFourierProjection( embedding_size=8 , set_W_to_weight=UpperCAmelCase_ , log=UpperCAmelCase_ , flip_sin_to_cos=UpperCAmelCase_ ) __UpperCAmelCase : str = 2 * block_out_channels[0] elif time_embedding_type == "positional": __UpperCAmelCase : str = Timesteps( block_out_channels[0] , flip_sin_to_cos=UpperCAmelCase_ , downscale_freq_shift=UpperCAmelCase_ ) __UpperCAmelCase : Dict = block_out_channels[0] if use_timestep_embedding: __UpperCAmelCase : Union[str, Any] = block_out_channels[0] * 4 __UpperCAmelCase : str = TimestepEmbedding( in_channels=UpperCAmelCase_ , time_embed_dim=UpperCAmelCase_ , act_fn=UpperCAmelCase_ , out_dim=block_out_channels[0] , ) __UpperCAmelCase : Tuple = nn.ModuleList([] ) __UpperCAmelCase : int = None __UpperCAmelCase : Optional[Any] = nn.ModuleList([] ) __UpperCAmelCase : Dict = None # down __UpperCAmelCase : str = in_channels for i, down_block_type in enumerate(UpperCAmelCase_ ): __UpperCAmelCase : Optional[Any] = output_channel __UpperCAmelCase : Optional[int] = block_out_channels[i] if i == 0: input_channel += extra_in_channels __UpperCAmelCase : Tuple = i == len(UpperCAmelCase_ ) - 1 __UpperCAmelCase : List[str] = get_down_block( UpperCAmelCase_ , num_layers=UpperCAmelCase_ , in_channels=UpperCAmelCase_ , out_channels=UpperCAmelCase_ , temb_channels=block_out_channels[0] , add_downsample=not is_final_block or downsample_each_block , ) self.down_blocks.append(UpperCAmelCase_ ) # mid __UpperCAmelCase : Optional[Any] = get_mid_block( UpperCAmelCase_ , in_channels=block_out_channels[-1] , mid_channels=block_out_channels[-1] , out_channels=block_out_channels[-1] , embed_dim=block_out_channels[0] , num_layers=UpperCAmelCase_ , add_downsample=UpperCAmelCase_ , ) # up __UpperCAmelCase : Tuple = list(reversed(UpperCAmelCase_ ) ) __UpperCAmelCase : Any = reversed_block_out_channels[0] if out_block_type is None: __UpperCAmelCase : Union[str, Any] = out_channels else: __UpperCAmelCase : Dict = block_out_channels[0] for i, up_block_type in enumerate(UpperCAmelCase_ ): __UpperCAmelCase : int = output_channel __UpperCAmelCase : str = ( reversed_block_out_channels[i + 1] if i < len(UpperCAmelCase_ ) - 1 else final_upsample_channels ) __UpperCAmelCase : Tuple = i == len(UpperCAmelCase_ ) - 1 __UpperCAmelCase : Dict = get_up_block( UpperCAmelCase_ , num_layers=UpperCAmelCase_ , in_channels=UpperCAmelCase_ , out_channels=UpperCAmelCase_ , temb_channels=block_out_channels[0] , add_upsample=not is_final_block , ) self.up_blocks.append(UpperCAmelCase_ ) __UpperCAmelCase : Union[str, Any] = output_channel # out __UpperCAmelCase : Optional[int] = norm_num_groups if norm_num_groups is not None else min(block_out_channels[0] // 4 , 32 ) __UpperCAmelCase : List[Any] = get_out_block( out_block_type=UpperCAmelCase_ , num_groups_out=UpperCAmelCase_ , embed_dim=block_out_channels[0] , out_channels=UpperCAmelCase_ , act_fn=UpperCAmelCase_ , fc_dim=block_out_channels[-1] // 4 , ) def lowerCamelCase_ ( self : Optional[int] , UpperCAmelCase_ : torch.FloatTensor , UpperCAmelCase_ : Union[torch.Tensor, float, int] , UpperCAmelCase_ : bool = True , ): """simple docstring""" __UpperCAmelCase : Dict = timestep if not torch.is_tensor(UpperCAmelCase_ ): __UpperCAmelCase : List[str] = torch.tensor([timesteps] , dtype=torch.long , device=sample.device ) elif torch.is_tensor(UpperCAmelCase_ ) and len(timesteps.shape ) == 0: __UpperCAmelCase : List[str] = timesteps[None].to(sample.device ) __UpperCAmelCase : List[str] = self.time_proj(UpperCAmelCase_ ) if self.config.use_timestep_embedding: __UpperCAmelCase : Any = self.time_mlp(UpperCAmelCase_ ) else: __UpperCAmelCase : Any = timestep_embed[..., None] __UpperCAmelCase : int = timestep_embed.repeat([1, 1, sample.shape[2]] ).to(sample.dtype ) __UpperCAmelCase : Dict = timestep_embed.broadcast_to((sample.shape[:1] + timestep_embed.shape[1:]) ) # 2. down __UpperCAmelCase : int = () for downsample_block in self.down_blocks: __UpperCAmelCase , __UpperCAmelCase : int = downsample_block(hidden_states=UpperCAmelCase_ , temb=UpperCAmelCase_ ) down_block_res_samples += res_samples # 3. mid if self.mid_block: __UpperCAmelCase : List[str] = self.mid_block(UpperCAmelCase_ , UpperCAmelCase_ ) # 4. up for i, upsample_block in enumerate(self.up_blocks ): __UpperCAmelCase : Any = down_block_res_samples[-1:] __UpperCAmelCase : List[Any] = down_block_res_samples[:-1] __UpperCAmelCase : str = upsample_block(UpperCAmelCase_ , res_hidden_states_tuple=UpperCAmelCase_ , temb=UpperCAmelCase_ ) # 5. post-process if self.out_block: __UpperCAmelCase : Tuple = self.out_block(UpperCAmelCase_ , UpperCAmelCase_ ) if not return_dict: return (sample,) return UNetaDOutput(sample=UpperCAmelCase_ )
37
1
from __future__ import annotations def SCREAMING_SNAKE_CASE_ ( __magic_name__ : list , __magic_name__ : int , __magic_name__ : int , __magic_name__ : int ) -> list: """simple docstring""" UpperCamelCase :List[Any] = [] UpperCamelCase , UpperCamelCase :Tuple = 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 ) ) UpperCamelCase :Dict = result + left + right return input_list def SCREAMING_SNAKE_CASE_ ( __magic_name__ : list ) -> list: """simple docstring""" if len(__magic_name__ ) <= 1: return input_list UpperCamelCase :List[Any] = list(__magic_name__ ) # iteration for two-way merging UpperCamelCase :Union[str, Any] = 2 while p <= len(__magic_name__ ): # getting low, high and middle value for merge-sort of single list for i in range(0 , len(__magic_name__ ) , __magic_name__ ): UpperCamelCase :Optional[int] = i UpperCamelCase :List[Any] = i + p - 1 UpperCamelCase :str = (low + high + 1) // 2 UpperCamelCase :int = merge(__magic_name__ , __magic_name__ , __magic_name__ , __magic_name__ ) # final merge of last two parts if p * 2 >= len(__magic_name__ ): UpperCamelCase :str = i UpperCamelCase :Tuple = merge(__magic_name__ , 0 , __magic_name__ , len(__magic_name__ ) - 1 ) break p *= 2 return input_list if __name__ == "__main__": UpperCAmelCase_ : int = input('''Enter numbers separated by a comma:\n''').strip() if user_input == "": UpperCAmelCase_ : str = [] else: UpperCAmelCase_ : Optional[Any] = [int(item.strip()) for item in user_input.split(''',''')] print(iter_merge_sort(unsorted))
38
import unittest from pathlib import Path from tempfile import TemporaryDirectory from transformers import AutoConfig, TFAutoModel, is_tensorflow_text_available, is_tf_available from transformers.models.bert.tokenization_bert import BertTokenizer from transformers.testing_utils import require_tensorflow_text, require_tf, slow if is_tf_available(): import tensorflow as tf if is_tensorflow_text_available(): from transformers.models.bert import TFBertTokenizer UpperCAmelCase_ : Optional[Any] = ['''bert-base-uncased''', '''bert-base-cased'''] UpperCAmelCase_ : List[str] = '''hf-internal-testing/tiny-bert-tf-only''' if is_tf_available(): class _SCREAMING_SNAKE_CASE ( tf.keras.Model ): def __init__( self : List[str] , __lowerCamelCase : Union[str, Any] ): super().__init__() UpperCamelCase :Any = tokenizer UpperCamelCase :List[str] = AutoConfig.from_pretrained(__lowerCamelCase ) UpperCamelCase :List[str] = TFAutoModel.from_config(__lowerCamelCase ) def _A ( self : Tuple , __lowerCamelCase : str ): UpperCamelCase :str = self.tokenizer(__lowerCamelCase ) UpperCamelCase :Any = self.bert(**__lowerCamelCase ) return out["pooler_output"] @require_tf @require_tensorflow_text class _SCREAMING_SNAKE_CASE ( unittest.TestCase ): def _A ( self : Dict ): super().setUp() UpperCamelCase :int = [ BertTokenizer.from_pretrained(__lowerCamelCase ) for checkpoint in (TOKENIZER_CHECKPOINTS * 2) ] # repeat for when fast_bert_tokenizer=false UpperCamelCase :Any = [TFBertTokenizer.from_pretrained(__lowerCamelCase ) for checkpoint in TOKENIZER_CHECKPOINTS] + [ TFBertTokenizer.from_pretrained(__lowerCamelCase , use_fast_bert_tokenizer=__lowerCamelCase ) for checkpoint in TOKENIZER_CHECKPOINTS ] assert len(self.tokenizers ) == len(self.tf_tokenizers ) UpperCamelCase :Any = [ """This is a straightforward English test sentence.""", """This one has some weird characters\rto\nsee\r\nif those\u00E9break things.""", """Now we're going to add some Chinese: 一 二 三 一二三""", """And some much more rare Chinese: 齉 堃 齉堃""", """Je vais aussi écrire en français pour tester les accents""", """Classical Irish also has some unusual characters, so in they go: Gaelaċ, ꝼ""", ] UpperCamelCase :Union[str, Any] = list(zip(self.test_sentences , self.test_sentences[::-1] ) ) def _A ( self : Optional[int] ): for tokenizer, tf_tokenizer in zip(self.tokenizers , self.tf_tokenizers ): for test_inputs in (self.test_sentences, self.paired_sentences): UpperCamelCase :Any = tokenizer(__lowerCamelCase , return_tensors="""tf""" , padding="""longest""" ) UpperCamelCase :str = tf_tokenizer(__lowerCamelCase ) for key in python_outputs.keys(): self.assertTrue(tf.reduce_all(python_outputs[key].shape == tf_outputs[key].shape ) ) self.assertTrue(tf.reduce_all(tf.cast(python_outputs[key] , tf.intaa ) == tf_outputs[key] ) ) @slow def _A ( self : Dict ): for tf_tokenizer in self.tf_tokenizers: UpperCamelCase :str = tf_tokenizer(self.paired_sentences ) UpperCamelCase :Any = tf_tokenizer( text=[sentence[0] for sentence in self.paired_sentences] , text_pair=[sentence[1] for sentence in self.paired_sentences] , ) for key in merged_outputs.keys(): self.assertTrue(tf.reduce_all(tf.cast(merged_outputs[key] , tf.intaa ) == separated_outputs[key] ) ) @slow def _A ( self : List[str] ): for tf_tokenizer in self.tf_tokenizers: UpperCamelCase :List[Any] = tf.function(__lowerCamelCase ) for test_inputs in (self.test_sentences, self.paired_sentences): UpperCamelCase :Any = tf.constant(__lowerCamelCase ) UpperCamelCase :List[str] = compiled_tokenizer(__lowerCamelCase ) UpperCamelCase :Optional[Any] = tf_tokenizer(__lowerCamelCase ) for key in eager_outputs.keys(): self.assertTrue(tf.reduce_all(eager_outputs[key] == compiled_outputs[key] ) ) @slow def _A ( self : Tuple ): for tf_tokenizer in self.tf_tokenizers: UpperCamelCase :List[str] = ModelToSave(tokenizer=__lowerCamelCase ) UpperCamelCase :Union[str, Any] = tf.convert_to_tensor(self.test_sentences ) UpperCamelCase :Union[str, Any] = model(__lowerCamelCase ) # Build model with some sample inputs with TemporaryDirectory() as tempdir: UpperCamelCase :List[str] = Path(__lowerCamelCase ) / """saved.model""" model.save(__lowerCamelCase ) UpperCamelCase :List[Any] = tf.keras.models.load_model(__lowerCamelCase ) UpperCamelCase :Dict = loaded_model(__lowerCamelCase ) # We may see small differences because the loaded model is compiled, so we need an epsilon for the test self.assertLessEqual(tf.reduce_max(tf.abs(out - loaded_output ) ) , 1E-5 )
38
1
import re from filelock import FileLock try: import nltk _A = True except (ImportError, ModuleNotFoundError): _A = False if NLTK_AVAILABLE: with FileLock('.lock') as lock: nltk.download('punkt', quiet=True) def _UpperCAmelCase ( SCREAMING_SNAKE_CASE__ : str ): re.sub('<n>' , '' , SCREAMING_SNAKE_CASE__ ) # remove pegasus newline char assert NLTK_AVAILABLE, "nltk must be installed to separate newlines between sentences. (pip install nltk)" return "\n".join(nltk.sent_tokenize(SCREAMING_SNAKE_CASE__ ) )
117
from __future__ import annotations from typing import Any def _UpperCAmelCase ( SCREAMING_SNAKE_CASE__ : list[Any] ): create_state_space_tree(SCREAMING_SNAKE_CASE__ , [] , 0 ) def _UpperCAmelCase ( SCREAMING_SNAKE_CASE__ : list[Any] , SCREAMING_SNAKE_CASE__ : list[Any] , SCREAMING_SNAKE_CASE__ : int ): if index == len(SCREAMING_SNAKE_CASE__ ): print(SCREAMING_SNAKE_CASE__ ) return create_state_space_tree(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , index + 1 ) current_subsequence.append(sequence[index] ) create_state_space_tree(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , index + 1 ) current_subsequence.pop() if __name__ == "__main__": _A = [3, 1, 2, 4] generate_all_subsequences(seq) seq.clear() seq.extend(['A', 'B', 'C']) generate_all_subsequences(seq)
117
1
"""simple docstring""" from .constants import ( MODEL_NAME, OPTIMIZER_NAME, RNG_STATE_NAME, SAFE_WEIGHTS_INDEX_NAME, SAFE_WEIGHTS_NAME, SCALER_NAME, SCHEDULER_NAME, TORCH_LAUNCH_PARAMS, WEIGHTS_INDEX_NAME, WEIGHTS_NAME, ) from .dataclasses import ( BnbQuantizationConfig, ComputeEnvironment, CustomDtype, DeepSpeedPlugin, DistributedDataParallelKwargs, DistributedType, DynamoBackend, FPaRecipeKwargs, FullyShardedDataParallelPlugin, GradientAccumulationPlugin, GradScalerKwargs, InitProcessGroupKwargs, KwargsHandler, LoggerType, MegatronLMPlugin, PrecisionType, ProjectConfiguration, RNGType, SageMakerDistributedType, TensorInformation, TorchDynamoPlugin, ) from .environment import get_int_from_env, parse_choice_from_env, parse_flag_from_env from .imports import ( get_ccl_version, is_abit_bnb_available, is_abit_bnb_available, is_aim_available, is_bfaa_available, is_bnb_available, is_botoa_available, is_ccl_available, is_comet_ml_available, is_datasets_available, is_deepspeed_available, is_fpa_available, is_ipex_available, is_megatron_lm_available, is_mlflow_available, is_mps_available, is_npu_available, is_rich_available, is_safetensors_available, is_sagemaker_available, is_tensorboard_available, is_tpu_available, is_transformers_available, is_wandb_available, is_xpu_available, ) from .modeling import ( check_device_map, check_tied_parameters_in_config, check_tied_parameters_on_same_device, compute_module_sizes, convert_file_size_to_int, dtype_byte_size, find_tied_parameters, get_balanced_memory, get_max_layer_size, get_max_memory, get_mixed_precision_context_manager, id_tensor_storage, infer_auto_device_map, load_checkpoint_in_model, load_offloaded_weights, load_state_dict, named_module_tensors, retie_parameters, set_module_tensor_to_device, shard_checkpoint, ) from .offload import ( OffloadedWeightsLoader, PrefixedDataset, extract_submodules_state_dict, load_offloaded_weight, offload_state_dict, offload_weight, save_offload_index, ) from .operations import ( broadcast, broadcast_object_list, concatenate, convert_outputs_to_fpaa, convert_to_fpaa, find_batch_size, find_device, gather, gather_object, get_data_structure, honor_type, initialize_tensors, is_namedtuple, is_tensor_information, is_torch_tensor, listify, pad_across_processes, recursively_apply, reduce, send_to_device, slice_tensors, ) from .versions import compare_versions, is_torch_version if is_deepspeed_available(): from .deepspeed import ( DeepSpeedEngineWrapper, DeepSpeedOptimizerWrapper, DeepSpeedSchedulerWrapper, DummyOptim, DummyScheduler, HfDeepSpeedConfig, ) from .bnb import has_abit_bnb_layers, load_and_quantize_model from .fsdp_utils import load_fsdp_model, load_fsdp_optimizer, save_fsdp_model, save_fsdp_optimizer from .launch import ( PrepareForLaunch, _filter_args, prepare_deepspeed_cmd_env, prepare_multi_gpu_env, prepare_sagemager_args_inputs, prepare_simple_launcher_cmd_env, prepare_tpu, ) from .megatron_lm import ( AbstractTrainStep, BertTrainStep, GPTTrainStep, MegatronEngine, MegatronLMDummyDataLoader, MegatronLMDummyScheduler, MegatronLMOptimizerWrapper, MegatronLMSchedulerWrapper, TaTrainStep, avg_losses_across_data_parallel_group, gather_across_data_parallel_groups, ) from .megatron_lm import initialize as megatron_lm_initialize from .megatron_lm import prepare_data_loader as megatron_lm_prepare_data_loader from .megatron_lm import prepare_model as megatron_lm_prepare_model from .megatron_lm import prepare_optimizer as megatron_lm_prepare_optimizer from .megatron_lm import prepare_scheduler as megatron_lm_prepare_scheduler from .memory import find_executable_batch_size, release_memory from .other import ( extract_model_from_parallel, get_pretty_name, is_port_in_use, merge_dicts, patch_environment, save, wait_for_everyone, write_basic_config, ) from .random import set_seed, synchronize_rng_state, synchronize_rng_states from .torch_xla import install_xla from .tqdm import tqdm from .transformer_engine import convert_model, has_transformer_engine_layers
171
"""simple docstring""" from typing import Dict, List, Optional, Union import numpy as np from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size_dict from ...image_transforms import convert_to_rgb, normalize, rescale, resize, to_channel_dimension_format from ...image_utils import ( OPENAI_CLIP_MEAN, OPENAI_CLIP_STD, ChannelDimension, ImageInput, PILImageResampling, make_list_of_images, to_numpy_array, valid_images, ) from ...utils import TensorType, is_vision_available, logging if is_vision_available(): import PIL _A = logging.get_logger(__name__) class lowerCamelCase ( lowerCAmelCase__ ): '''simple docstring''' SCREAMING_SNAKE_CASE = ['pixel_values'] def __init__(self , _lowerCamelCase = True , _lowerCamelCase = None , _lowerCamelCase = PILImageResampling.BICUBIC , _lowerCamelCase = True , _lowerCamelCase = 1 / 255 , _lowerCamelCase = True , _lowerCamelCase = None , _lowerCamelCase = None , _lowerCamelCase = True , **_lowerCamelCase , ): """simple docstring""" super().__init__(**_lowerCamelCase ) UpperCAmelCase__ : List[Any] = size if size is not None else {"""height""": 384, """width""": 384} UpperCAmelCase__ : Optional[int] = get_size_dict(_lowerCamelCase , default_to_square=_lowerCamelCase ) UpperCAmelCase__ : Union[str, Any] = do_resize UpperCAmelCase__ : Dict = size UpperCAmelCase__ : Optional[Any] = resample UpperCAmelCase__ : Optional[Any] = do_rescale UpperCAmelCase__ : List[str] = rescale_factor UpperCAmelCase__ : List[Any] = do_normalize UpperCAmelCase__ : Dict = image_mean if image_mean is not None else OPENAI_CLIP_MEAN UpperCAmelCase__ : Union[str, Any] = image_std if image_std is not None else OPENAI_CLIP_STD UpperCAmelCase__ : Optional[int] = do_convert_rgb def _a (self , _lowerCamelCase , _lowerCamelCase , _lowerCamelCase = PILImageResampling.BICUBIC , _lowerCamelCase = None , **_lowerCamelCase , ): """simple docstring""" UpperCAmelCase__ : Tuple = get_size_dict(_lowerCamelCase , default_to_square=_lowerCamelCase ) if "height" not in size or "width" not in size: raise ValueError(F"""The `size` dictionary must contain the keys `height` and `width`. Got {size.keys()}""" ) UpperCAmelCase__ : Dict = (size["""height"""], size["""width"""]) return resize(_lowerCamelCase , size=_lowerCamelCase , resample=_lowerCamelCase , data_format=_lowerCamelCase , **_lowerCamelCase ) def _a (self , _lowerCamelCase , _lowerCamelCase , _lowerCamelCase = None , **_lowerCamelCase , ): """simple docstring""" return rescale(_lowerCamelCase , scale=_lowerCamelCase , data_format=_lowerCamelCase , **_lowerCamelCase ) def _a (self , _lowerCamelCase , _lowerCamelCase , _lowerCamelCase , _lowerCamelCase = None , **_lowerCamelCase , ): """simple docstring""" return normalize(_lowerCamelCase , mean=_lowerCamelCase , std=_lowerCamelCase , data_format=_lowerCamelCase , **_lowerCamelCase ) def _a (self , _lowerCamelCase , _lowerCamelCase = None , _lowerCamelCase = None , _lowerCamelCase = None , _lowerCamelCase = None , _lowerCamelCase = None , _lowerCamelCase = None , _lowerCamelCase = None , _lowerCamelCase = None , _lowerCamelCase = None , _lowerCamelCase = None , _lowerCamelCase = ChannelDimension.FIRST , **_lowerCamelCase , ): """simple docstring""" UpperCAmelCase__ : Optional[int] = do_resize if do_resize is not None else self.do_resize UpperCAmelCase__ : int = resample if resample is not None else self.resample UpperCAmelCase__ : Union[str, Any] = do_rescale if do_rescale is not None else self.do_rescale UpperCAmelCase__ : str = rescale_factor if rescale_factor is not None else self.rescale_factor UpperCAmelCase__ : int = do_normalize if do_normalize is not None else self.do_normalize UpperCAmelCase__ : Any = image_mean if image_mean is not None else self.image_mean UpperCAmelCase__ : Tuple = image_std if image_std is not None else self.image_std UpperCAmelCase__ : List[str] = do_convert_rgb if do_convert_rgb is not None else self.do_convert_rgb UpperCAmelCase__ : Optional[int] = size if size is not None else self.size UpperCAmelCase__ : List[str] = get_size_dict(_lowerCamelCase , default_to_square=_lowerCamelCase ) UpperCAmelCase__ : Any = make_list_of_images(_lowerCamelCase ) if not valid_images(_lowerCamelCase ): raise ValueError( """Invalid image type. Must be of type PIL.Image.Image, numpy.ndarray, """ """torch.Tensor, tf.Tensor or jax.ndarray.""" ) if do_resize and size is None or resample is None: raise ValueError("""Size and resample must be specified if do_resize is True.""" ) if do_rescale and rescale_factor is None: raise ValueError("""Rescale factor must be specified if do_rescale is True.""" ) if do_normalize and (image_mean is None or image_std is None): raise ValueError("""Image mean and std must be specified if do_normalize is True.""" ) # PIL RGBA images are converted to RGB if do_convert_rgb: UpperCAmelCase__ : Dict = [convert_to_rgb(_lowerCamelCase ) for image in images] # All transformations expect numpy arrays. UpperCAmelCase__ : Optional[Any] = [to_numpy_array(_lowerCamelCase ) for image in images] if do_resize: UpperCAmelCase__ : Union[str, Any] = [self.resize(image=_lowerCamelCase , size=_lowerCamelCase , resample=_lowerCamelCase ) for image in images] if do_rescale: UpperCAmelCase__ : List[str] = [self.rescale(image=_lowerCamelCase , scale=_lowerCamelCase ) for image in images] if do_normalize: UpperCAmelCase__ : Union[str, Any] = [self.normalize(image=_lowerCamelCase , mean=_lowerCamelCase , std=_lowerCamelCase ) for image in images] UpperCAmelCase__ : List[str] = [to_channel_dimension_format(_lowerCamelCase , _lowerCamelCase ) for image in images] UpperCAmelCase__ : Optional[Any] = BatchFeature(data={"""pixel_values""": images} , tensor_type=_lowerCamelCase ) return encoded_outputs
171
1
'''simple docstring''' def __A ( lowerCAmelCase_ ): return credit_card_number.startswith(("""34""", """35""", """37""", """4""", """5""", """6""") ) def __A ( lowerCAmelCase_ ): _UpperCAmelCase : List[str] = credit_card_number _UpperCAmelCase : Optional[int] = 0 _UpperCAmelCase : Optional[int] = len(lowerCAmelCase_ ) - 2 for i in range(lowerCAmelCase_ , -1 , -2 ): # double the value of every second digit _UpperCAmelCase : List[str] = int(cc_number[i] ) digit *= 2 # If doubling of a number results in a two digit number # i.e greater than 9(e.g., 6 × 2 = 12), # then add the digits of the product (e.g., 12: 1 + 2 = 3, 15: 1 + 5 = 6), # to get a single digit number. if digit > 9: digit %= 10 digit += 1 _UpperCAmelCase : Any = cc_number[:i] + str(lowerCAmelCase_ ) + cc_number[i + 1 :] total += digit # Sum up the remaining digits for i in range(len(lowerCAmelCase_ ) - 1 , -1 , -2 ): total += int(cc_number[i] ) return total % 10 == 0 def __A ( lowerCAmelCase_ ): _UpperCAmelCase : Tuple = f"{credit_card_number} is an invalid credit card number because" if not credit_card_number.isdigit(): print(f"{error_message} it has nonnumerical characters." ) return False if not 13 <= len(lowerCAmelCase_ ) <= 16: print(f"{error_message} of its length." ) return False if not validate_initial_digits(lowerCAmelCase_ ): print(f"{error_message} of its first two digits." ) return False if not luhn_validation(lowerCAmelCase_ ): print(f"{error_message} it fails the Luhn check." ) return False print(f"{credit_card_number} is a valid credit card number." ) return True if __name__ == "__main__": import doctest doctest.testmod() validate_credit_card_number('''4111111111111111''') validate_credit_card_number('''32323''')
170
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_torch_available, ) lowerCAmelCase_ : int = { '''configuration_falcon''': ['''FALCON_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''FalconConfig'''], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase_ : Optional[Any] = [ '''FALCON_PRETRAINED_MODEL_ARCHIVE_LIST''', '''FalconForCausalLM''', '''FalconModel''', '''FalconPreTrainedModel''', '''FalconForSequenceClassification''', '''FalconForTokenClassification''', '''FalconForQuestionAnswering''', ] if TYPE_CHECKING: from .configuration_falcon import FALCON_PRETRAINED_CONFIG_ARCHIVE_MAP, FalconConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_falcon import ( FALCON_PRETRAINED_MODEL_ARCHIVE_LIST, FalconForCausalLM, FalconForQuestionAnswering, FalconForSequenceClassification, FalconForTokenClassification, FalconModel, FalconPreTrainedModel, ) else: import sys lowerCAmelCase_ : List[str] = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
170
1
'''simple docstring''' import unittest import numpy as np import torch from .utils_summarization import build_mask, compute_token_type_ids, process_story, truncate_or_pad class UpperCAmelCase ( unittest.TestCase ): '''simple docstring''' def _lowerCAmelCase( self ) -> str: lowercase__ : Tuple = 10 def _lowerCAmelCase( self ) -> Union[str, Any]: lowercase__ : Optional[Any] = [1, 2, 3, 4] lowercase__ : int = [1, 2, 3, 4, 0, 0, 0, 0, 0, 0] self.assertEqual(truncate_or_pad(__lowerCAmelCase , self.block_size , 0 ) , __lowerCAmelCase ) def _lowerCAmelCase( self ) -> int: lowercase__ : Tuple = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] lowercase__ : int = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] self.assertEqual(truncate_or_pad(__lowerCAmelCase , self.block_size , 0 ) , __lowerCAmelCase ) def _lowerCAmelCase( self ) -> Union[str, Any]: lowercase__ : Optional[int] = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13] lowercase__ : Optional[int] = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] self.assertEqual(truncate_or_pad(__lowerCAmelCase , self.block_size , 0 ) , __lowerCAmelCase ) def _lowerCAmelCase( self ) -> Dict: lowercase__ : Dict = 'It was the year of Our Lord one thousand seven hundred and\n seventy-five.\n\nSpiritual revelations were conceded to England at that\n favoured period, as at this.' lowercase__ : Any = process_story(__lowerCAmelCase ) self.assertEqual(__lowerCAmelCase , [] ) def _lowerCAmelCase( self ) -> List[Any]: lowercase__ : Optional[int] = '' lowercase__ : int = process_story(__lowerCAmelCase ) self.assertEqual(__lowerCAmelCase , [] ) self.assertEqual(__lowerCAmelCase , [] ) def _lowerCAmelCase( self ) -> Any: lowercase__ : str = ( 'It was the year of Our Lord one thousand seven hundred and ' 'seventy-five\n\nSpiritual revelations were conceded to England ' 'at that favoured period, as at this.\n@highlight\n\nIt was the best of times' ) lowercase__ : Optional[Any] = process_story(__lowerCAmelCase ) lowercase__ : List[Any] = [ 'It was the year of Our Lord one thousand seven hundred and seventy-five.', 'Spiritual revelations were conceded to England at that favoured period, as at this.', ] self.assertEqual(__lowerCAmelCase , __lowerCAmelCase ) lowercase__ : Optional[Any] = ['It was the best of times.'] self.assertEqual(__lowerCAmelCase , __lowerCAmelCase ) def _lowerCAmelCase( self ) -> List[str]: lowercase__ : int = torch.tensor([1, 2, 3, 4] ) lowercase__ : Optional[Any] = torch.tensor([1, 1, 1, 1] ) np.testing.assert_array_equal(build_mask(__lowerCAmelCase , 0 ).numpy() , expected.numpy() ) def _lowerCAmelCase( self ) -> Optional[int]: lowercase__ : str = torch.tensor([1, 2, 3, 4, 23, 23, 23] ) lowercase__ : str = torch.tensor([1, 1, 1, 1, 0, 0, 0] ) np.testing.assert_array_equal(build_mask(__lowerCAmelCase , 23 ).numpy() , expected.numpy() ) def _lowerCAmelCase( self ) -> List[str]: lowercase__ : Union[str, Any] = torch.tensor([8, 2, 3, 4, 1, 1, 1] ) lowercase__ : str = torch.tensor([1, 1, 1, 1, 0, 0, 0] ) np.testing.assert_array_equal(build_mask(__lowerCAmelCase , 1 ).numpy() , expected.numpy() ) def _lowerCAmelCase( self ) -> Tuple: lowercase__ : Optional[int] = 101 lowercase__ : List[str] = torch.tensor([[1, 2, 3, 4, 5, 6], [1, 2, 3, 101, 5, 6], [1, 101, 3, 4, 101, 6]] ) lowercase__ : List[str] = torch.tensor([[1, 1, 1, 1, 1, 1], [1, 1, 1, 0, 0, 0], [1, 0, 0, 0, 1, 1]] ) lowercase__ : Optional[int] = compute_token_type_ids(__lowerCAmelCase , __lowerCAmelCase ) np.testing.assert_array_equal(__lowerCAmelCase , __lowerCAmelCase )
198
def _a ( SCREAMING_SNAKE_CASE : int , SCREAMING_SNAKE_CASE : int , SCREAMING_SNAKE_CASE : list[list[int]] ) -> int: """simple docstring""" def update_area_of_max_square(SCREAMING_SNAKE_CASE : int , SCREAMING_SNAKE_CASE : int ) -> int: # BASE CASE if row >= rows or col >= cols: return 0 __lowerCAmelCase: Union[str, Any] = update_area_of_max_square(SCREAMING_SNAKE_CASE , col + 1 ) __lowerCAmelCase: Tuple = update_area_of_max_square(row + 1 , col + 1 ) __lowerCAmelCase: int = update_area_of_max_square(row + 1 , SCREAMING_SNAKE_CASE ) if mat[row][col]: __lowerCAmelCase: List[str] = 1 + min([right, diagonal, down] ) __lowerCAmelCase: List[str] = max(largest_square_area[0] , SCREAMING_SNAKE_CASE ) return sub_problem_sol else: return 0 __lowerCAmelCase: List[str] = [0] update_area_of_max_square(0 , 0 ) return largest_square_area[0] def _a ( SCREAMING_SNAKE_CASE : int , SCREAMING_SNAKE_CASE : int , SCREAMING_SNAKE_CASE : list[list[int]] ) -> int: """simple docstring""" def update_area_of_max_square_using_dp_array( SCREAMING_SNAKE_CASE : int , SCREAMING_SNAKE_CASE : int , SCREAMING_SNAKE_CASE : list[list[int]] ) -> int: if row >= rows or col >= cols: return 0 if dp_array[row][col] != -1: return dp_array[row][col] __lowerCAmelCase: List[Any] = update_area_of_max_square_using_dp_array(SCREAMING_SNAKE_CASE , col + 1 , SCREAMING_SNAKE_CASE ) __lowerCAmelCase: Union[str, Any] = update_area_of_max_square_using_dp_array(row + 1 , col + 1 , SCREAMING_SNAKE_CASE ) __lowerCAmelCase: Any = update_area_of_max_square_using_dp_array(row + 1 , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ) if mat[row][col]: __lowerCAmelCase: int = 1 + min([right, diagonal, down] ) __lowerCAmelCase: Union[str, Any] = max(largest_square_area[0] , SCREAMING_SNAKE_CASE ) __lowerCAmelCase: Optional[int] = sub_problem_sol return sub_problem_sol else: return 0 __lowerCAmelCase: int = [0] __lowerCAmelCase: int = [[-1] * cols for _ in range(SCREAMING_SNAKE_CASE )] update_area_of_max_square_using_dp_array(0 , 0 , SCREAMING_SNAKE_CASE ) return largest_square_area[0] def _a ( SCREAMING_SNAKE_CASE : int , SCREAMING_SNAKE_CASE : int , SCREAMING_SNAKE_CASE : list[list[int]] ) -> int: """simple docstring""" __lowerCAmelCase: int = [[0] * (cols + 1) for _ in range(rows + 1 )] __lowerCAmelCase: Optional[Any] = 0 for row in range(rows - 1 , -1 , -1 ): for col in range(cols - 1 , -1 , -1 ): __lowerCAmelCase: Union[str, Any] = dp_array[row][col + 1] __lowerCAmelCase: str = dp_array[row + 1][col + 1] __lowerCAmelCase: Optional[int] = dp_array[row + 1][col] if mat[row][col] == 1: __lowerCAmelCase: Optional[Any] = 1 + min(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ) __lowerCAmelCase: str = max(dp_array[row][col] , SCREAMING_SNAKE_CASE ) else: __lowerCAmelCase: Dict = 0 return largest_square_area def _a ( SCREAMING_SNAKE_CASE : int , SCREAMING_SNAKE_CASE : int , SCREAMING_SNAKE_CASE : list[list[int]] ) -> int: """simple docstring""" __lowerCAmelCase: Tuple = [0] * (cols + 1) __lowerCAmelCase: Optional[int] = [0] * (cols + 1) __lowerCAmelCase: str = 0 for row in range(rows - 1 , -1 , -1 ): for col in range(cols - 1 , -1 , -1 ): __lowerCAmelCase: int = current_row[col + 1] __lowerCAmelCase: Union[str, Any] = next_row[col + 1] __lowerCAmelCase: Any = next_row[col] if mat[row][col] == 1: __lowerCAmelCase: str = 1 + min(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ) __lowerCAmelCase: str = max(current_row[col] , SCREAMING_SNAKE_CASE ) else: __lowerCAmelCase: Optional[Any] = 0 __lowerCAmelCase: int = current_row return largest_square_area if __name__ == "__main__": import doctest doctest.testmod() print(largest_square_area_in_matrix_bottom_up(2, 2, [[1, 1], [1, 1]]))
322
0
from collections import OrderedDict from typing import Mapping from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging __SCREAMING_SNAKE_CASE : Any = logging.get_logger(__name__) __SCREAMING_SNAKE_CASE : List[Any] = { 'camembert-base': 'https://huggingface.co/camembert-base/resolve/main/config.json', 'umberto-commoncrawl-cased-v1': ( 'https://huggingface.co/Musixmatch/umberto-commoncrawl-cased-v1/resolve/main/config.json' ), 'umberto-wikipedia-uncased-v1': ( 'https://huggingface.co/Musixmatch/umberto-wikipedia-uncased-v1/resolve/main/config.json' ), } class lowercase_ ( __snake_case ): _lowerCamelCase = 'camembert' def __init__( self , lowercase_=30_522 , lowercase_=768 , lowercase_=12 , lowercase_=12 , lowercase_=3_072 , lowercase_="gelu" , lowercase_=0.1 , lowercase_=0.1 , lowercase_=512 , lowercase_=2 , lowercase_=0.02 , lowercase_=1e-12 , lowercase_=1 , lowercase_=0 , lowercase_=2 , lowercase_="absolute" , lowercase_=True , lowercase_=None , **lowercase_ , ): super().__init__(pad_token_id=lowercase_ , bos_token_id=lowercase_ , eos_token_id=lowercase_ , **lowercase_ ) _snake_case : str = vocab_size _snake_case : Any = hidden_size _snake_case : Optional[int] = num_hidden_layers _snake_case : Optional[int] = num_attention_heads _snake_case : int = hidden_act _snake_case : List[Any] = intermediate_size _snake_case : int = hidden_dropout_prob _snake_case : str = attention_probs_dropout_prob _snake_case : List[Any] = max_position_embeddings _snake_case : Dict = type_vocab_size _snake_case : Optional[int] = initializer_range _snake_case : Optional[Any] = layer_norm_eps _snake_case : Union[str, Any] = position_embedding_type _snake_case : Any = use_cache _snake_case : Union[str, Any] = classifier_dropout class lowercase_ ( __snake_case ): @property def UpperCamelCase ( self ): if self.task == "multiple-choice": _snake_case : Tuple = {0: "batch", 1: "choice", 2: "sequence"} else: _snake_case : List[Any] = {0: "batch", 1: "sequence"} return OrderedDict( [ ("input_ids", dynamic_axis), ("attention_mask", dynamic_axis), ] )
284
from typing import TYPE_CHECKING # rely on isort to merge the imports from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available __SCREAMING_SNAKE_CASE : Optional[Any] = { 'configuration_autoformer': [ 'AUTOFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP', 'AutoformerConfig', ], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __SCREAMING_SNAKE_CASE : Union[str, Any] = [ 'AUTOFORMER_PRETRAINED_MODEL_ARCHIVE_LIST', 'AutoformerForPrediction', 'AutoformerModel', 'AutoformerPreTrainedModel', ] if TYPE_CHECKING: from .configuration_autoformer import ( AUTOFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP, AutoformerConfig, ) try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_autoformer import ( AUTOFORMER_PRETRAINED_MODEL_ARCHIVE_LIST, AutoformerForPrediction, AutoformerModel, AutoformerPreTrainedModel, ) else: import sys __SCREAMING_SNAKE_CASE : str = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
284
1
"""simple docstring""" import warnings from typing import List, Optional, Union from ...processing_utils import ProcessorMixin from ...tokenization_utils_base import BatchEncoding, PaddingStrategy, PreTokenizedInput, TextInput, TruncationStrategy from ...utils import TensorType class __magic_name__ ( UpperCAmelCase__ ): '''simple docstring''' __UpperCamelCase = ["image_processor", "tokenizer"] __UpperCamelCase = "LayoutLMv2ImageProcessor" __UpperCamelCase = ("LayoutXLMTokenizer", "LayoutXLMTokenizerFast") def __init__( self , _a=None , _a=None , **_a ): """simple docstring""" if "feature_extractor" in kwargs: warnings.warn( """The `feature_extractor` argument is deprecated and will be removed in v5, use `image_processor`""" """ instead.""" , _a , ) lowerCamelCase = kwargs.pop("""feature_extractor""" ) lowerCamelCase = image_processor if image_processor is not None else feature_extractor if image_processor is None: raise ValueError("""You need to specify an `image_processor`.""" ) if tokenizer is None: raise ValueError("""You need to specify a `tokenizer`.""" ) super().__init__(_a , _a ) def __call__( self , _a , _a = None , _a = None , _a = None , _a = None , _a = True , _a = False , _a = None , _a = None , _a = 0 , _a = None , _a = None , _a = None , _a = False , _a = False , _a = False , _a = False , _a = True , _a = None , **_a , ): """simple docstring""" # verify input if self.image_processor.apply_ocr and (boxes is not None): raise ValueError( """You cannot provide bounding boxes """ """if you initialized the image processor with apply_ocr set to True.""" ) if self.image_processor.apply_ocr and (word_labels is not None): raise ValueError( """You cannot provide word labels if you initialized the image processor with apply_ocr set to True.""" ) if return_overflowing_tokens is True and return_offsets_mapping is False: raise ValueError("""You cannot return overflowing tokens without returning the offsets mapping.""" ) # first, apply the image processor lowerCamelCase = self.image_processor(images=_a , return_tensors=_a ) # second, apply the tokenizer if text is not None and self.image_processor.apply_ocr and text_pair is None: if isinstance(_a , _a ): lowerCamelCase = [text] # add batch dimension (as the image processor always adds a batch dimension) lowerCamelCase = features["""words"""] lowerCamelCase = self.tokenizer( text=text if text is not None else features["""words"""] , text_pair=text_pair if text_pair is not None else None , boxes=boxes if boxes is not None else features["""boxes"""] , word_labels=_a , add_special_tokens=_a , padding=_a , truncation=_a , max_length=_a , stride=_a , pad_to_multiple_of=_a , return_token_type_ids=_a , return_attention_mask=_a , return_overflowing_tokens=_a , return_special_tokens_mask=_a , return_offsets_mapping=_a , return_length=_a , verbose=_a , return_tensors=_a , **_a , ) # add pixel values lowerCamelCase = features.pop("""pixel_values""" ) if return_overflowing_tokens is True: lowerCamelCase = self.get_overflowing_images(_a , encoded_inputs["""overflow_to_sample_mapping"""] ) lowerCamelCase = images return encoded_inputs def _lowerCAmelCase ( self , _a , _a ): """simple docstring""" # in case there's an overflow, ensure each `input_ids` sample is mapped to its corresponding image lowerCamelCase = [] for sample_idx in overflow_to_sample_mapping: images_with_overflow.append(images[sample_idx] ) if len(_a ) != len(_a ): raise ValueError( """Expected length of images to be the same as the length of `overflow_to_sample_mapping`, but got""" f' {len(_a )} and {len(_a )}' ) return images_with_overflow def _lowerCAmelCase ( self , *_a , **_a ): """simple docstring""" return self.tokenizer.batch_decode(*_a , **_a ) def _lowerCAmelCase ( self , *_a , **_a ): """simple docstring""" return self.tokenizer.decode(*_a , **_a ) @property def _lowerCAmelCase ( self ): """simple docstring""" return ["input_ids", "bbox", "attention_mask", "image"] @property def _lowerCAmelCase ( self ): """simple docstring""" warnings.warn( """`feature_extractor_class` is deprecated and will be removed in v5. Use `image_processor_class` instead.""" , _a , ) return self.image_processor_class @property def _lowerCAmelCase ( self ): """simple docstring""" warnings.warn( """`feature_extractor` is deprecated and will be removed in v5. Use `image_processor` instead.""" , _a , ) return self.image_processor
291
"""simple docstring""" import functools import operator from ...configuration_utils import PretrainedConfig from ...utils import logging lowerCAmelCase : int = logging.get_logger(__name__) lowerCAmelCase : List[str] = { """asapp/sew-d-tiny-100k""": """https://huggingface.co/asapp/sew-d-tiny-100k/resolve/main/config.json""", # See all SEW-D models at https://huggingface.co/models?filter=sew-d } class __magic_name__ ( UpperCAmelCase__ ): '''simple docstring''' __UpperCamelCase = "sew-d" def __init__( self , _a=32 , _a=768 , _a=12 , _a=12 , _a=3_072 , _a=2 , _a=512 , _a=256 , _a=True , _a=True , _a=("p2c", "c2p") , _a="layer_norm" , _a="gelu_python" , _a=0.1 , _a=0.1 , _a=0.1 , _a=0.0 , _a=0.1 , _a=0.02 , _a=1e-7 , _a=1e-5 , _a="group" , _a="gelu" , _a=(64, 128, 128, 128, 128, 256, 256, 256, 256, 512, 512, 512, 512) , _a=(5, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1) , _a=(10, 3, 1, 3, 1, 3, 1, 3, 1, 2, 1, 2, 1) , _a=False , _a=128 , _a=16 , _a=True , _a=0.05 , _a=10 , _a=2 , _a=0.0 , _a=10 , _a=0 , _a="mean" , _a=False , _a=False , _a=256 , _a=0 , _a=1 , _a=2 , **_a , ): """simple docstring""" super().__init__(**_a , pad_token_id=_a , bos_token_id=_a , eos_token_id=_a ) lowerCamelCase = hidden_size lowerCamelCase = feat_extract_norm lowerCamelCase = feat_extract_activation lowerCamelCase = list(_a ) lowerCamelCase = list(_a ) lowerCamelCase = list(_a ) lowerCamelCase = conv_bias lowerCamelCase = num_conv_pos_embeddings lowerCamelCase = num_conv_pos_embedding_groups lowerCamelCase = len(self.conv_dim ) lowerCamelCase = num_hidden_layers lowerCamelCase = intermediate_size lowerCamelCase = squeeze_factor lowerCamelCase = max_position_embeddings lowerCamelCase = position_buckets lowerCamelCase = share_att_key lowerCamelCase = relative_attention lowerCamelCase = norm_rel_ebd lowerCamelCase = list(_a ) lowerCamelCase = hidden_act lowerCamelCase = num_attention_heads lowerCamelCase = hidden_dropout lowerCamelCase = attention_dropout lowerCamelCase = activation_dropout lowerCamelCase = feat_proj_dropout lowerCamelCase = final_dropout lowerCamelCase = layer_norm_eps lowerCamelCase = feature_layer_norm_eps lowerCamelCase = initializer_range lowerCamelCase = vocab_size if ( (len(self.conv_stride ) != self.num_feat_extract_layers) or (len(self.conv_kernel ) != self.num_feat_extract_layers) or (len(self.conv_dim ) != self.num_feat_extract_layers) ): raise ValueError( """Configuration for convolutional layers is incorrect.""" """It is required that `len(config.conv_dim)` == `len(config.conv_stride)` == `len(config.conv_kernel)`,""" f'but is `len(config.conv_dim) = {len(self.conv_dim )}`, `len(config.conv_stride)' f'= {len(self.conv_stride )}`, `len(config.conv_kernel) = {len(self.conv_kernel )}`.' ) # fine-tuning config parameters for SpecAugment: https://arxiv.org/abs/1904.08779 lowerCamelCase = apply_spec_augment lowerCamelCase = mask_time_prob lowerCamelCase = mask_time_length lowerCamelCase = mask_time_min_masks lowerCamelCase = mask_feature_prob lowerCamelCase = mask_feature_length lowerCamelCase = mask_feature_min_masks # ctc loss lowerCamelCase = ctc_loss_reduction lowerCamelCase = ctc_zero_infinity # sequence classification lowerCamelCase = use_weighted_layer_sum lowerCamelCase = classifier_proj_size @property def _lowerCAmelCase ( self ): """simple docstring""" return functools.reduce(operator.mul , self.conv_stride , 1 )
291
1
"""simple docstring""" import unittest import numpy as np from transformers.testing_utils import require_torch, require_vision from transformers.utils import is_torch_available, is_vision_available from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs if is_torch_available(): import torch if is_vision_available(): from PIL import Image from transformers import PoolFormerImageProcessor class lowercase( unittest.TestCase ): '''simple docstring''' def __init__( self: List[str], a_: Optional[Any], a_: Optional[int]=7, a_: List[str]=3, a_: int=30, a_: Tuple=400, a_: Optional[int]=True, a_: int=None, a_: Union[str, Any]=0.9, a_: Optional[int]=None, a_: Dict=True, a_: Tuple=[0.5, 0.5, 0.5], a_: List[Any]=[0.5, 0.5, 0.5], ): '''simple docstring''' _snake_case : Union[str, Any] = size if size is not None else {'shortest_edge': 30} _snake_case : Tuple = crop_size if crop_size is not None else {'height': 30, 'width': 30} _snake_case : List[str] = parent _snake_case : str = batch_size _snake_case : List[Any] = num_channels _snake_case : Union[str, Any] = min_resolution _snake_case : Union[str, Any] = max_resolution _snake_case : Optional[Any] = do_resize_and_center_crop _snake_case : Any = size _snake_case : int = crop_pct _snake_case : Any = crop_size _snake_case : str = do_normalize _snake_case : Dict = image_mean _snake_case : Any = image_std def UpperCamelCase_ ( self: Tuple ): '''simple docstring''' return { "size": self.size, "do_resize_and_center_crop": self.do_resize_and_center_crop, "crop_pct": self.crop_pct, "crop_size": self.crop_size, "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''' lowercase__ = PoolFormerImageProcessor if is_vision_available() else None def UpperCamelCase_ ( self: Union[str, Any] ): '''simple docstring''' _snake_case : str = PoolFormerImageProcessingTester(self ) @property def UpperCamelCase_ ( self: List[str] ): '''simple docstring''' return self.image_processor_tester.prepare_image_processor_dict() def UpperCamelCase_ ( self: Dict ): '''simple docstring''' _snake_case : str = self.image_processing_class(**self.image_processor_dict ) self.assertTrue(hasattr(a_, """do_resize_and_center_crop""" ) ) self.assertTrue(hasattr(a_, """size""" ) ) self.assertTrue(hasattr(a_, """crop_pct""" ) ) self.assertTrue(hasattr(a_, """do_normalize""" ) ) self.assertTrue(hasattr(a_, """image_mean""" ) ) self.assertTrue(hasattr(a_, """image_std""" ) ) def UpperCamelCase_ ( self: Any ): '''simple docstring''' _snake_case : Union[str, Any] = self.image_processing_class.from_dict(self.image_processor_dict ) self.assertEqual(image_processor.size, {"""shortest_edge""": 30} ) self.assertEqual(image_processor.crop_size, {"""height""": 30, """width""": 30} ) _snake_case : Optional[Any] = self.image_processing_class.from_dict(self.image_processor_dict, size=42, crop_size=84 ) self.assertEqual(image_processor.size, {"""shortest_edge""": 42} ) self.assertEqual(image_processor.crop_size, {"""height""": 84, """width""": 84} ) def UpperCamelCase_ ( self: List[str] ): '''simple docstring''' pass def UpperCamelCase_ ( self: Tuple ): '''simple docstring''' _snake_case : int = self.image_processing_class(**self.image_processor_dict ) # create random PIL images _snake_case : Tuple = prepare_image_inputs(self.image_processor_tester, equal_resolution=a_ ) for image in image_inputs: self.assertIsInstance(a_, Image.Image ) # Test not batched input _snake_case : Tuple = image_processing(image_inputs[0], return_tensors="""pt""" ).pixel_values self.assertEqual( encoded_images.shape, ( 1, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size["""height"""], self.image_processor_tester.crop_size["""width"""], ), ) # Test batched _snake_case : List[Any] = image_processing(a_, return_tensors="""pt""" ).pixel_values self.assertEqual( encoded_images.shape, ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size["""height"""], self.image_processor_tester.crop_size["""width"""], ), ) def UpperCamelCase_ ( self: Optional[Any] ): '''simple docstring''' _snake_case : Any = self.image_processing_class(**self.image_processor_dict ) # create random numpy tensors _snake_case : List[Any] = prepare_image_inputs(self.image_processor_tester, equal_resolution=a_, numpify=a_ ) for image in image_inputs: self.assertIsInstance(a_, np.ndarray ) # Test not batched input _snake_case : Union[str, Any] = image_processing(image_inputs[0], return_tensors="""pt""" ).pixel_values self.assertEqual( encoded_images.shape, ( 1, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size["""height"""], self.image_processor_tester.crop_size["""width"""], ), ) # Test batched _snake_case : Optional[Any] = image_processing(a_, return_tensors="""pt""" ).pixel_values self.assertEqual( encoded_images.shape, ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size["""height"""], self.image_processor_tester.crop_size["""width"""], ), ) def UpperCamelCase_ ( self: Dict ): '''simple docstring''' _snake_case : Any = self.image_processing_class(**self.image_processor_dict ) # create random PyTorch tensors _snake_case : Dict = prepare_image_inputs(self.image_processor_tester, equal_resolution=a_, torchify=a_ ) for image in image_inputs: self.assertIsInstance(a_, torch.Tensor ) # Test not batched input _snake_case : 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.crop_size["""height"""], self.image_processor_tester.crop_size["""width"""], ), ) # Test batched _snake_case : Optional[int] = image_processing(a_, return_tensors="""pt""" ).pixel_values self.assertEqual( encoded_images.shape, ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size["""height"""], self.image_processor_tester.crop_size["""width"""], ), )
351
"""simple docstring""" from collections.abc import Generator from math import sin def UpperCAmelCase__ (snake_case__ : bytes ): """simple docstring""" if len(snake_case__ ) != 32: raise ValueError("""Input must be of length 32""" ) _snake_case : Optional[int] = B"""""" for i in [3, 2, 1, 0]: little_endian += string_aa[8 * i : 8 * i + 8] return little_endian def UpperCAmelCase__ (snake_case__ : int ): """simple docstring""" if i < 0: raise ValueError("""Input must be non-negative""" ) _snake_case : Optional[Any] = format(snake_case__ , """08x""" )[-8:] _snake_case : Optional[Any] = B"""""" for i in [3, 2, 1, 0]: little_endian_hex += hex_rep[2 * i : 2 * i + 2].encode("""utf-8""" ) return little_endian_hex def UpperCAmelCase__ (snake_case__ : bytes ): """simple docstring""" _snake_case : Union[str, Any] = B"""""" for char in message: bit_string += format(snake_case__ , """08b""" ).encode("""utf-8""" ) _snake_case : List[Any] = format(len(snake_case__ ) , """064b""" ).encode("""utf-8""" ) # Pad bit_string to a multiple of 512 chars bit_string += b"1" while len(snake_case__ ) % 5_12 != 4_48: bit_string += b"0" bit_string += to_little_endian(start_len[32:] ) + to_little_endian(start_len[:32] ) return bit_string def UpperCAmelCase__ (snake_case__ : bytes ): """simple docstring""" if len(snake_case__ ) % 5_12 != 0: raise ValueError("""Input must have length that's a multiple of 512""" ) for pos in range(0 , len(snake_case__ ) , 5_12 ): _snake_case : List[str] = bit_string[pos : pos + 5_12] _snake_case : List[str] = [] for i in range(0 , 5_12 , 32 ): block_words.append(int(to_little_endian(block[i : i + 32] ) , 2 ) ) yield block_words def UpperCAmelCase__ (snake_case__ : int ): """simple docstring""" if i < 0: raise ValueError("""Input must be non-negative""" ) _snake_case : Optional[int] = format(snake_case__ , """032b""" ) _snake_case : str = """""" for c in i_str: new_str += "1" if c == "0" else "0" return int(snake_case__ , 2 ) def UpperCAmelCase__ (snake_case__ : int , snake_case__ : int ): """simple docstring""" return (a + b) % 2**32 def UpperCAmelCase__ (snake_case__ : int , snake_case__ : int ): """simple docstring""" if i < 0: raise ValueError("""Input must be non-negative""" ) if shift < 0: raise ValueError("""Shift must be non-negative""" ) return ((i << shift) ^ (i >> (32 - shift))) % 2**32 def UpperCAmelCase__ (snake_case__ : bytes ): """simple docstring""" _snake_case : Any = preprocess(snake_case__ ) _snake_case : Optional[Any] = [int(2**32 * abs(sin(i + 1 ) ) ) for i in range(64 )] # Starting states _snake_case : Union[str, Any] = 0x6745_2301 _snake_case : List[Any] = 0xEFCD_AB89 _snake_case : Optional[Any] = 0x98BA_DCFE _snake_case : Optional[int] = 0x1032_5476 _snake_case : Tuple = [ 7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22, 5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20, 4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23, 6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21, ] # Process bit string in chunks, each with 16 32-char words for block_words in get_block_words(snake_case__ ): _snake_case : Tuple = aa _snake_case : str = ba _snake_case : int = ca _snake_case : Dict = da # Hash current chunk for i in range(64 ): if i <= 15: # f = (b & c) | (not_32(b) & d) # Alternate definition for f _snake_case : int = d ^ (b & (c ^ d)) _snake_case : Dict = i elif i <= 31: # f = (d & b) | (not_32(d) & c) # Alternate definition for f _snake_case : Tuple = c ^ (d & (b ^ c)) _snake_case : int = (5 * i + 1) % 16 elif i <= 47: _snake_case : List[Any] = b ^ c ^ d _snake_case : Union[str, Any] = (3 * i + 5) % 16 else: _snake_case : Tuple = c ^ (b | not_aa(snake_case__ )) _snake_case : Optional[int] = (7 * i) % 16 _snake_case : Dict = (f + a + added_consts[i] + block_words[g]) % 2**32 _snake_case : List[str] = d _snake_case : List[Any] = c _snake_case : str = b _snake_case : List[str] = sum_aa(snake_case__ , left_rotate_aa(snake_case__ , shift_amounts[i] ) ) # Add hashed chunk to running total _snake_case : Union[str, Any] = sum_aa(snake_case__ , snake_case__ ) _snake_case : str = sum_aa(snake_case__ , snake_case__ ) _snake_case : Any = sum_aa(snake_case__ , snake_case__ ) _snake_case : List[str] = sum_aa(snake_case__ , snake_case__ ) _snake_case : Any = reformat_hex(snake_case__ ) + reformat_hex(snake_case__ ) + reformat_hex(snake_case__ ) + reformat_hex(snake_case__ ) return digest if __name__ == "__main__": import doctest doctest.testmod()
132
0
"""simple docstring""" import math from collections import defaultdict from typing import List, Optional, Tuple, Union import numpy as np import torch from ..configuration_utils import ConfigMixin, register_to_config from .scheduling_utils import KarrasDiffusionSchedulers, SchedulerMixin, SchedulerOutput def _snake_case ( snake_case__ : List[Any] , snake_case__ : Optional[int]=0.999 , snake_case__ : Union[str, Any]="cosine" , ): if alpha_transform_type == "cosine": def alpha_bar_fn(snake_case__ : Union[str, Any] ): return math.cos((t + 0.008) / 1.008 * math.pi / 2 ) ** 2 elif alpha_transform_type == "exp": def alpha_bar_fn(snake_case__ : Dict ): return math.exp(t * -12.0 ) else: raise ValueError(F'Unsupported alpha_tranform_type: {alpha_transform_type}' ) A = [] for i in range(snake_case__ ): A = i / num_diffusion_timesteps A = (i + 1) / num_diffusion_timesteps betas.append(min(1 - alpha_bar_fn(snake_case__ ) / alpha_bar_fn(snake_case__ ) , snake_case__ ) ) return torch.tensor(snake_case__ , dtype=torch.floataa ) class lowerCAmelCase_ ( _lowercase , _lowercase ): '''simple docstring''' _lowerCamelCase: Optional[int] = [e.name for e in KarrasDiffusionSchedulers] _lowerCamelCase: Optional[Any] = 2 @register_to_config def __init__( self : str ,A_ : int = 1000 ,A_ : float = 0.0_00_85 ,A_ : float = 0.0_12 ,A_ : str = "linear" ,A_ : Optional[Union[np.ndarray, List[float]]] = None ,A_ : str = "epsilon" ,A_ : Optional[bool] = False ,A_ : Optional[bool] = False ,A_ : float = 1.0 ,A_ : str = "linspace" ,A_ : int = 0 ,) -> List[str]: if trained_betas is not None: A = torch.tensor(A_ ,dtype=torch.floataa ) elif beta_schedule == "linear": A = torch.linspace(A_ ,A_ ,A_ ,dtype=torch.floataa ) elif beta_schedule == "scaled_linear": # this schedule is very specific to the latent diffusion model. A = ( torch.linspace(beta_start**0.5 ,beta_end**0.5 ,A_ ,dtype=torch.floataa ) ** 2 ) elif beta_schedule == "squaredcos_cap_v2": # Glide cosine schedule A = betas_for_alpha_bar(A_ ,alpha_transform_type='cosine' ) elif beta_schedule == "exp": A = betas_for_alpha_bar(A_ ,alpha_transform_type='exp' ) else: raise NotImplementedError(F'{beta_schedule} does is not implemented for {self.__class__}' ) A = 1.0 - self.betas A = torch.cumprod(self.alphas ,dim=0 ) # set all values self.set_timesteps(A_ ,A_ ,A_ ) A = use_karras_sigmas def _SCREAMING_SNAKE_CASE ( self : int ,A_ : Tuple ,A_ : Tuple=None ) -> Tuple: if schedule_timesteps is None: A = self.timesteps A = (schedule_timesteps == timestep).nonzero() # The sigma index that is taken for the **very** first `step` # is always the second index (or the last index if there is only 1) # This way we can ensure we don't accidentally skip a sigma in # case we start in the middle of the denoising schedule (e.g. for image-to-image) if len(self._index_counter ) == 0: A = 1 if len(A_ ) > 1 else 0 else: A = timestep.cpu().item() if torch.is_tensor(A_ ) else timestep A = self._index_counter[timestep_int] return indices[pos].item() @property def _SCREAMING_SNAKE_CASE ( self : Union[str, Any] ) -> List[Any]: # standard deviation of the initial noise distribution if self.config.timestep_spacing in ["linspace", "trailing"]: return self.sigmas.max() return (self.sigmas.max() ** 2 + 1) ** 0.5 def _SCREAMING_SNAKE_CASE ( self : List[Any] ,A_ : torch.FloatTensor ,A_ : Union[float, torch.FloatTensor] ,) -> torch.FloatTensor: A = self.index_for_timestep(A_ ) A = self.sigmas[step_index] A = sample / ((sigma**2 + 1) ** 0.5) return sample def _SCREAMING_SNAKE_CASE ( self : str ,A_ : int ,A_ : Union[str, torch.device] = None ,A_ : Optional[int] = None ,) -> Optional[Any]: A = num_inference_steps A = num_train_timesteps or self.config.num_train_timesteps # "linspace", "leading", "trailing" corresponds to annotation of Table 2. of https://arxiv.org/abs/2305.08891 if self.config.timestep_spacing == "linspace": A = np.linspace(0 ,num_train_timesteps - 1 ,A_ ,dtype=A_ )[::-1].copy() elif self.config.timestep_spacing == "leading": A = num_train_timesteps // self.num_inference_steps # creates integer timesteps by multiplying by ratio # casting to int to avoid issues when num_inference_step is power of 3 A = (np.arange(0 ,A_ ) * step_ratio).round()[::-1].copy().astype(A_ ) timesteps += self.config.steps_offset elif self.config.timestep_spacing == "trailing": A = num_train_timesteps / self.num_inference_steps # creates integer timesteps by multiplying by ratio # casting to int to avoid issues when num_inference_step is power of 3 A = (np.arange(A_ ,0 ,-step_ratio )).round().copy().astype(A_ ) timesteps -= 1 else: raise ValueError( F'{self.config.timestep_spacing} is not supported. Please make sure to choose one of \'linspace\', \'leading\' or \'trailing\'.' ) A = np.array(((1 - self.alphas_cumprod) / self.alphas_cumprod) ** 0.5 ) A = np.log(A_ ) A = np.interp(A_ ,np.arange(0 ,len(A_ ) ) ,A_ ) if self.config.use_karras_sigmas: A = self._convert_to_karras(in_sigmas=A_ ,num_inference_steps=self.num_inference_steps ) A = np.array([self._sigma_to_t(A_ ,A_ ) for sigma in sigmas] ) A = np.concatenate([sigmas, [0.0]] ).astype(np.floataa ) A = torch.from_numpy(A_ ).to(device=A_ ) A = torch.cat([sigmas[:1], sigmas[1:-1].repeat_interleave(2 ), sigmas[-1:]] ) A = torch.from_numpy(A_ ) A = torch.cat([timesteps[:1], timesteps[1:].repeat_interleave(2 )] ) if str(A_ ).startswith('mps' ): # mps does not support float64 A = timesteps.to(A_ ,dtype=torch.floataa ) else: A = timesteps.to(device=A_ ) # empty dt and derivative A = None A = None # for exp beta schedules, such as the one for `pipeline_shap_e.py` # we need an index counter A = defaultdict(A_ ) def _SCREAMING_SNAKE_CASE ( self : Any ,A_ : Optional[Any] ,A_ : List[str] ) -> Dict: # get log sigma A = np.log(A_ ) # get distribution A = log_sigma - log_sigmas[:, np.newaxis] # get sigmas range A = np.cumsum((dists >= 0) ,axis=0 ).argmax(axis=0 ).clip(max=log_sigmas.shape[0] - 2 ) A = low_idx + 1 A = log_sigmas[low_idx] A = log_sigmas[high_idx] # interpolate sigmas A = (low - log_sigma) / (low - high) A = np.clip(A_ ,0 ,1 ) # transform interpolation to time range A = (1 - w) * low_idx + w * high_idx A = t.reshape(sigma.shape ) return t def _SCREAMING_SNAKE_CASE ( self : List[str] ,A_ : torch.FloatTensor ,A_ : int ) -> torch.FloatTensor: A = in_sigmas[-1].item() A = in_sigmas[0].item() A = 7.0 # 7.0 is the value used in the paper A = np.linspace(0 ,1 ,A_ ) A = sigma_min ** (1 / rho) A = sigma_max ** (1 / rho) A = (max_inv_rho + ramp * (min_inv_rho - max_inv_rho)) ** rho return sigmas @property def _SCREAMING_SNAKE_CASE ( self : List[Any] ) -> Dict: return self.dt is None def _SCREAMING_SNAKE_CASE ( self : Tuple ,A_ : Union[torch.FloatTensor, np.ndarray] ,A_ : Union[float, torch.FloatTensor] ,A_ : Union[torch.FloatTensor, np.ndarray] ,A_ : bool = True ,) -> Union[SchedulerOutput, Tuple]: A = self.index_for_timestep(A_ ) # advance index counter by 1 A = timestep.cpu().item() if torch.is_tensor(A_ ) else timestep self._index_counter[timestep_int] += 1 if self.state_in_first_order: A = self.sigmas[step_index] A = self.sigmas[step_index + 1] else: # 2nd order / Heun's method A = self.sigmas[step_index - 1] A = self.sigmas[step_index] # currently only gamma=0 is supported. This usually works best anyways. # We can support gamma in the future but then need to scale the timestep before # passing it to the model which requires a change in API A = 0 A = sigma * (gamma + 1) # Note: sigma_hat == sigma for now # 1. compute predicted original sample (x_0) from sigma-scaled predicted noise if self.config.prediction_type == "epsilon": A = sigma_hat if self.state_in_first_order else sigma_next A = sample - sigma_input * model_output elif self.config.prediction_type == "v_prediction": A = sigma_hat if self.state_in_first_order else sigma_next A = model_output * (-sigma_input / (sigma_input**2 + 1) ** 0.5) + ( sample / (sigma_input**2 + 1) ) elif self.config.prediction_type == "sample": A = model_output else: raise ValueError( F'prediction_type given as {self.config.prediction_type} must be one of `epsilon`, or `v_prediction`' ) if self.config.clip_sample: A = pred_original_sample.clamp( -self.config.clip_sample_range ,self.config.clip_sample_range ) if self.state_in_first_order: # 2. Convert to an ODE derivative for 1st order A = (sample - pred_original_sample) / sigma_hat # 3. delta timestep A = sigma_next - sigma_hat # store for 2nd order step A = derivative A = dt A = sample else: # 2. 2nd order / Heun's method A = (sample - pred_original_sample) / sigma_next A = (self.prev_derivative + derivative) / 2 # 3. take prev timestep & sample A = self.dt A = self.sample # free dt and derivative # Note, this puts the scheduler in "first order mode" A = None A = None A = None A = sample + derivative * dt if not return_dict: return (prev_sample,) return SchedulerOutput(prev_sample=A_ ) def _SCREAMING_SNAKE_CASE ( self : int ,A_ : torch.FloatTensor ,A_ : torch.FloatTensor ,A_ : torch.FloatTensor ,) -> torch.FloatTensor: # Make sure sigmas and timesteps have the same device and dtype as original_samples A = self.sigmas.to(device=original_samples.device ,dtype=original_samples.dtype ) if original_samples.device.type == "mps" and torch.is_floating_point(A_ ): # mps does not support float64 A = self.timesteps.to(original_samples.device ,dtype=torch.floataa ) A = timesteps.to(original_samples.device ,dtype=torch.floataa ) else: A = self.timesteps.to(original_samples.device ) A = timesteps.to(original_samples.device ) A = [self.index_for_timestep(A_ ,A_ ) for t in timesteps] A = sigmas[step_indices].flatten() while len(sigma.shape ) < len(original_samples.shape ): A = sigma.unsqueeze(-1 ) A = original_samples + noise * sigma return noisy_samples def __len__( self : Dict ) -> int: return self.config.num_train_timesteps
74
"""simple docstring""" import json import os import unittest from transformers.models.biogpt.tokenization_biogpt import VOCAB_FILES_NAMES, BioGptTokenizer from transformers.testing_utils import slow from ...test_tokenization_common import TokenizerTesterMixin class _SCREAMING_SNAKE_CASE ( A__ , unittest.TestCase ): UpperCAmelCase_ :Any = BioGptTokenizer UpperCAmelCase_ :str = False def __lowerCAmelCase ( self ) -> List[Any]: super().setUp() # Adapted from Sennrich et al. 2015 and https://github.com/rsennrich/subword-nmt lowerCAmelCase_ :Optional[Any] = [ """l""", """o""", """w""", """e""", """r""", """s""", """t""", """i""", """d""", """n""", """w</w>""", """r</w>""", """t</w>""", """lo""", """low""", """er</w>""", """low</w>""", """lowest</w>""", """newer</w>""", """wider</w>""", """<unk>""", ] lowerCAmelCase_ :str = dict(zip(__A , range(len(__A ) ) ) ) lowerCAmelCase_ :int = ["""l o 123""", """lo w 1456""", """e r</w> 1789""", """"""] lowerCAmelCase_ :Optional[int] = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES["""vocab_file"""] ) lowerCAmelCase_ :Dict = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES["""merges_file"""] ) with open(self.vocab_file , """w""" ) as fp: fp.write(json.dumps(__A ) ) with open(self.merges_file , """w""" ) as fp: fp.write("""\n""".join(__A ) ) def __lowerCAmelCase ( self , __A ) -> Optional[int]: lowerCAmelCase_ :List[Any] = """lower newer""" lowerCAmelCase_ :Tuple = """lower newer""" return input_text, output_text def __lowerCAmelCase ( self ) -> str: lowerCAmelCase_ :List[str] = BioGptTokenizer(self.vocab_file , self.merges_file ) lowerCAmelCase_ :Union[str, Any] = """lower""" lowerCAmelCase_ :Any = ["""low""", """er</w>"""] lowerCAmelCase_ :Union[str, Any] = tokenizer.tokenize(__A ) self.assertListEqual(__A , __A ) lowerCAmelCase_ :Dict = tokens + ["""<unk>"""] lowerCAmelCase_ :List[str] = [14, 15, 20] self.assertListEqual(tokenizer.convert_tokens_to_ids(__A ) , __A ) @slow def __lowerCAmelCase ( self ) -> List[Any]: lowerCAmelCase_ :Optional[Any] = BioGptTokenizer.from_pretrained("""microsoft/biogpt""" ) lowerCAmelCase_ :List[Any] = tokenizer.encode("""sequence builders""" , add_special_tokens=__A ) lowerCAmelCase_ :List[str] = tokenizer.encode("""multi-sequence build""" , add_special_tokens=__A ) lowerCAmelCase_ :Optional[int] = tokenizer.build_inputs_with_special_tokens(__A ) lowerCAmelCase_ :List[str] = tokenizer.build_inputs_with_special_tokens(__A , __A ) self.assertTrue(encoded_sentence == [2] + text ) self.assertTrue(encoded_pair == [2] + text + [2] + text_a )
84
0
"""simple docstring""" import argparse import re import numpy as np import requests import torch from huggingface_hub import hf_hub_download from PIL import Image from transformers import ( SamConfig, SamImageProcessor, SamModel, SamProcessor, SamVisionConfig, ) __snake_case = { '''iou_prediction_head.layers.0''': '''iou_prediction_head.proj_in''', '''iou_prediction_head.layers.1''': '''iou_prediction_head.layers.0''', '''iou_prediction_head.layers.2''': '''iou_prediction_head.proj_out''', '''mask_decoder.output_upscaling.0''': '''mask_decoder.upscale_conv1''', '''mask_decoder.output_upscaling.1''': '''mask_decoder.upscale_layer_norm''', '''mask_decoder.output_upscaling.3''': '''mask_decoder.upscale_conv2''', '''mask_downscaling.0''': '''mask_embed.conv1''', '''mask_downscaling.1''': '''mask_embed.layer_norm1''', '''mask_downscaling.3''': '''mask_embed.conv2''', '''mask_downscaling.4''': '''mask_embed.layer_norm2''', '''mask_downscaling.6''': '''mask_embed.conv3''', '''point_embeddings''': '''point_embed''', '''pe_layer.positional_encoding_gaussian_matrix''': '''shared_embedding.positional_embedding''', '''image_encoder''': '''vision_encoder''', '''neck.0''': '''neck.conv1''', '''neck.1''': '''neck.layer_norm1''', '''neck.2''': '''neck.conv2''', '''neck.3''': '''neck.layer_norm2''', '''patch_embed.proj''': '''patch_embed.projection''', '''.norm''': '''.layer_norm''', '''blocks''': '''layers''', } def A_ ( _lowerCAmelCase : Any ): """simple docstring""" _a = {} state_dict.pop('''pixel_mean''', _lowerCAmelCase ) state_dict.pop('''pixel_std''', _lowerCAmelCase ) _a = R'''.*.output_hypernetworks_mlps.(\d+).layers.(\d+).*''' for key, value in state_dict.items(): for key_to_modify, new_key in KEYS_TO_MODIFY_MAPPING.items(): if key_to_modify in key: _a = key.replace(_lowerCAmelCase, _lowerCAmelCase ) if re.match(_lowerCAmelCase, _lowerCAmelCase ): _a = int(re.match(_lowerCAmelCase, _lowerCAmelCase ).group(2 ) ) if layer_nb == 0: _a = key.replace('''layers.0''', '''proj_in''' ) elif layer_nb == 1: _a = key.replace('''layers.1''', '''layers.0''' ) elif layer_nb == 2: _a = key.replace('''layers.2''', '''proj_out''' ) _a = value _a = model_state_dict[ '''prompt_encoder.shared_embedding.positional_embedding''' ] return model_state_dict def A_ ( _lowerCAmelCase : str, _lowerCAmelCase : Tuple, _lowerCAmelCase : List[Any], _lowerCAmelCase : str="ybelkada/segment-anything" ): """simple docstring""" _a = hf_hub_download(_lowerCAmelCase, f'checkpoints/{model_name}.pth' ) if "sam_vit_b" in model_name: _a = SamConfig() elif "sam_vit_l" in model_name: _a = SamVisionConfig( hidden_size=10_24, num_hidden_layers=24, num_attention_heads=16, global_attn_indexes=[5, 11, 17, 23], ) _a = SamConfig( vision_config=_lowerCAmelCase, ) elif "sam_vit_h" in model_name: _a = SamVisionConfig( hidden_size=12_80, num_hidden_layers=32, num_attention_heads=16, global_attn_indexes=[7, 15, 23, 31], ) _a = SamConfig( vision_config=_lowerCAmelCase, ) _a = torch.load(_lowerCAmelCase, map_location='''cpu''' ) _a = replace_keys(_lowerCAmelCase ) _a = SamImageProcessor() _a = SamProcessor(image_processor=_lowerCAmelCase ) _a = SamModel(_lowerCAmelCase ) hf_model.load_state_dict(_lowerCAmelCase ) _a = hf_model.to('''cuda''' ) _a = '''https://huggingface.co/ybelkada/segment-anything/resolve/main/assets/car.png''' _a = Image.open(requests.get(_lowerCAmelCase, stream=_lowerCAmelCase ).raw ).convert('''RGB''' ) _a = [[[4_00, 6_50]]] _a = [[1]] _a = processor(images=np.array(_lowerCAmelCase ), return_tensors='''pt''' ).to('''cuda''' ) with torch.no_grad(): _a = hf_model(**_lowerCAmelCase ) _a = output.iou_scores.squeeze() if model_name == "sam_vit_h_4b8939": assert scores[-1].item() == 0.5_7_9_8_9_0_2_5_1_1_5_9_6_6_8 _a = processor( images=np.array(_lowerCAmelCase ), input_points=_lowerCAmelCase, input_labels=_lowerCAmelCase, return_tensors='''pt''' ).to('''cuda''' ) with torch.no_grad(): _a = hf_model(**_lowerCAmelCase ) _a = output.iou_scores.squeeze() assert scores[-1].item() == 0.9_7_1_2_6_0_3_0_9_2_1_9_3_6_0_4 _a = ((75, 2_75, 17_25, 8_50),) _a = processor(images=np.array(_lowerCAmelCase ), input_boxes=_lowerCAmelCase, return_tensors='''pt''' ).to('''cuda''' ) with torch.no_grad(): _a = hf_model(**_lowerCAmelCase ) _a = output.iou_scores.squeeze() assert scores[-1].item() == 0.8_6_8_6_0_1_5_6_0_5_9_2_6_5_1_4 # Test with 2 points and 1 image. _a = [[[4_00, 6_50], [8_00, 6_50]]] _a = [[1, 1]] _a = processor( images=np.array(_lowerCAmelCase ), input_points=_lowerCAmelCase, input_labels=_lowerCAmelCase, return_tensors='''pt''' ).to('''cuda''' ) with torch.no_grad(): _a = hf_model(**_lowerCAmelCase ) _a = output.iou_scores.squeeze() assert scores[-1].item() == 0.9_9_3_6_0_4_7_7_9_2_4_3_4_6_9_2 if __name__ == "__main__": __snake_case = argparse.ArgumentParser() __snake_case = ['''sam_vit_b_01ec64''', '''sam_vit_h_4b8939''', '''sam_vit_l_0b3195'''] parser.add_argument( '''--model_name''', default='''sam_vit_h_4b8939''', choices=choices, type=str, help='''Path to hf config.json of model to convert''', ) parser.add_argument('''--pytorch_dump_folder_path''', default=None, type=str, help='''Path to the output PyTorch model.''') parser.add_argument( '''--push_to_hub''', action='''store_true''', help='''Whether to push the model and processor to the hub after converting''', ) parser.add_argument( '''--model_hub_id''', default='''ybelkada/segment-anything''', choices=choices, type=str, help='''Path to hf config.json of model to convert''', ) __snake_case = parser.parse_args() convert_sam_checkpoint(args.model_name, args.pytorch_dump_folder_path, args.push_to_hub, args.model_hub_id)
153
"""simple docstring""" from dataclasses import dataclass, field from typing import ClassVar, Dict from ..features import Features, Value from .base import TaskTemplate @dataclass(frozen=a__ ) class __lowerCamelCase ( a__ ): '''simple docstring''' # `task` is not a ClassVar since we want it to be part of the `asdict` output for JSON serialization A_ : str = field(default='summarization' , metadata={'include_in_asdict_even_if_is_default': True} ) A_ : ClassVar[Features] = Features({'text': Value('string' )} ) A_ : ClassVar[Features] = Features({'summary': Value('string' )} ) A_ : str = "text" A_ : str = "summary" @property def _UpperCAmelCase ( self ) -> Dict[str, str]: return {self.text_column: "text", self.summary_column: "summary"}
153
1
from ...configuration_utils import PretrainedConfig from ...utils import logging __a = logging.get_logger(__name__) __a = { # See all MEGATRON_BERT models at https://huggingface.co/models?filter=bert } class lowercase__( UpperCAmelCase ): """simple docstring""" a :Dict = 'megatron-bert' def __init__( self : Any , SCREAMING_SNAKE_CASE_ : Dict=2_9_0_5_6 , SCREAMING_SNAKE_CASE_ : Tuple=1_0_2_4 , SCREAMING_SNAKE_CASE_ : Optional[int]=2_4 , SCREAMING_SNAKE_CASE_ : Tuple=1_6 , SCREAMING_SNAKE_CASE_ : Any=4_0_9_6 , SCREAMING_SNAKE_CASE_ : Any="gelu" , SCREAMING_SNAKE_CASE_ : str=0.1 , SCREAMING_SNAKE_CASE_ : Tuple=0.1 , SCREAMING_SNAKE_CASE_ : int=5_1_2 , SCREAMING_SNAKE_CASE_ : Optional[Any]=2 , SCREAMING_SNAKE_CASE_ : Any=0.02 , SCREAMING_SNAKE_CASE_ : Any=1e-12 , SCREAMING_SNAKE_CASE_ : Union[str, Any]=0 , SCREAMING_SNAKE_CASE_ : int="absolute" , SCREAMING_SNAKE_CASE_ : int=True , **SCREAMING_SNAKE_CASE_ : Optional[int] , ) -> Any: super().__init__(pad_token_id=SCREAMING_SNAKE_CASE_ , **SCREAMING_SNAKE_CASE_ ) lowercase_ = vocab_size lowercase_ = hidden_size lowercase_ = num_hidden_layers lowercase_ = num_attention_heads lowercase_ = hidden_act lowercase_ = intermediate_size lowercase_ = hidden_dropout_prob lowercase_ = attention_probs_dropout_prob lowercase_ = max_position_embeddings lowercase_ = type_vocab_size lowercase_ = initializer_range lowercase_ = layer_norm_eps lowercase_ = position_embedding_type lowercase_ = use_cache
30
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_sentencepiece_available, is_tf_available, is_tokenizers_available, is_torch_available, ) lowerCAmelCase :str = {'''configuration_xglm''': ['''XGLM_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''XGLMConfig''']} try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase :Optional[Any] = ['''XGLMTokenizer'''] try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase :Dict = ['''XGLMTokenizerFast'''] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase :Tuple = [ '''XGLM_PRETRAINED_MODEL_ARCHIVE_LIST''', '''XGLMForCausalLM''', '''XGLMModel''', '''XGLMPreTrainedModel''', ] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase :int = [ '''FlaxXGLMForCausalLM''', '''FlaxXGLMModel''', '''FlaxXGLMPreTrainedModel''', ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase :Any = [ '''TF_XGLM_PRETRAINED_MODEL_ARCHIVE_LIST''', '''TFXGLMForCausalLM''', '''TFXGLMModel''', '''TFXGLMPreTrainedModel''', ] if TYPE_CHECKING: from .configuration_xglm import XGLM_PRETRAINED_CONFIG_ARCHIVE_MAP, XGLMConfig try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_xglm import XGLMTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_xglm_fast import XGLMTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_xglm import XGLM_PRETRAINED_MODEL_ARCHIVE_LIST, XGLMForCausalLM, XGLMModel, XGLMPreTrainedModel try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_flax_xglm import FlaxXGLMForCausalLM, FlaxXGLMModel, FlaxXGLMPreTrainedModel try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_xglm import ( TF_XGLM_PRETRAINED_MODEL_ARCHIVE_LIST, TFXGLMForCausalLM, TFXGLMModel, TFXGLMPreTrainedModel, ) else: import sys lowerCAmelCase :int = _LazyModule(__name__, globals()['''__file__'''], _import_structure)
331
0
"""simple docstring""" from scipy.stats import pearsonr import datasets lowerCamelCase_ = "\nPearson correlation coefficient and p-value for testing non-correlation.\nThe Pearson correlation coefficient measures the linear relationship between two datasets. The calculation of the p-value relies on the assumption that each dataset is normally distributed. Like other correlation coefficients, this one varies between -1 and +1 with 0 implying no correlation. Correlations of -1 or +1 imply an exact linear relationship. Positive correlations imply that as x increases, so does y. Negative correlations imply that as x increases, y decreases.\nThe p-value roughly indicates the probability of an uncorrelated system producing datasets that have a Pearson correlation at least as extreme as the one computed from these datasets.\n" lowerCamelCase_ = "\nArgs:\n predictions (`list` of `int`): Predicted class labels, as returned by a model.\n references (`list` of `int`): Ground truth labels.\n return_pvalue (`boolean`): If `True`, returns the p-value, along with the correlation coefficient. If `False`, returns only the correlation coefficient. Defaults to `False`.\n\nReturns:\n pearsonr (`float`): Pearson correlation coefficient. Minimum possible value is -1. Maximum possible value is 1. Values of 1 and -1 indicate exact linear positive and negative relationships, respectively. A value of 0 implies no correlation.\n p-value (`float`): P-value, which roughly indicates the probability of an The p-value roughly indicates the probability of an uncorrelated system producing datasets that have a Pearson correlation at least as extreme as the one computed from these datasets. Minimum possible value is 0. Maximum possible value is 1. Higher values indicate higher probabilities.\n\nExamples:\n\n Example 1-A simple example using only predictions and references.\n >>> pearsonr_metric = datasets.load_metric(\"pearsonr\")\n >>> results = pearsonr_metric.compute(predictions=[10, 9, 2.5, 6, 4], references=[1, 2, 3, 4, 5])\n >>> print(round(results['pearsonr'], 2))\n -0.74\n\n Example 2-The same as Example 1, but that also returns the `p-value`.\n >>> pearsonr_metric = datasets.load_metric(\"pearsonr\")\n >>> results = pearsonr_metric.compute(predictions=[10, 9, 2.5, 6, 4], references=[1, 2, 3, 4, 5], return_pvalue=True)\n >>> print(sorted(list(results.keys())))\n ['p-value', 'pearsonr']\n >>> print(round(results['pearsonr'], 2))\n -0.74\n >>> print(round(results['p-value'], 2))\n 0.15\n" lowerCamelCase_ = "\n@article{2020SciPy-NMeth,\nauthor = {Virtanen, Pauli and Gommers, Ralf and Oliphant, Travis E. and\n Haberland, Matt and Reddy, Tyler and Cournapeau, David and\n Burovski, Evgeni and Peterson, Pearu and Weckesser, Warren and\n Bright, Jonathan and {van der Walt}, St{\'e}fan J. and\n Brett, Matthew and Wilson, Joshua and Millman, K. Jarrod and\n Mayorov, Nikolay and Nelson, Andrew R. J. and Jones, Eric and\n Kern, Robert and Larson, Eric and Carey, C J and\n Polat, Ilhan and Feng, Yu and Moore, Eric W. and\n {VanderPlas}, Jake and Laxalde, Denis and Perktold, Josef and\n Cimrman, Robert and Henriksen, Ian and Quintero, E. A. and\n Harris, Charles R. and Archibald, Anne M. and\n Ribeiro, Antonio H. and Pedregosa, Fabian and\n {van Mulbregt}, Paul and {SciPy 1.0 Contributors}},\ntitle = {{{SciPy} 1.0: Fundamental Algorithms for Scientific\n Computing in Python}},\njournal = {Nature Methods},\nyear = {2020},\nvolume = {17},\npages = {261--272},\nadsurl = {https://rdcu.be/b08Wh},\ndoi = {10.1038/s41592-019-0686-2},\n}\n" @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class _SCREAMING_SNAKE_CASE( datasets.Metric ): def _UpperCamelCase ( self ) -> List[str]: """simple docstring""" return datasets.MetricInfo( description=_DESCRIPTION ,citation=_CITATION ,inputs_description=_KWARGS_DESCRIPTION ,features=datasets.Features( { '''predictions''': datasets.Value('''float''' ), '''references''': datasets.Value('''float''' ), } ) ,reference_urls=['''https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.pearsonr.html'''] ,) def _UpperCamelCase ( self ,SCREAMING_SNAKE_CASE__ ,SCREAMING_SNAKE_CASE__ ,SCREAMING_SNAKE_CASE__=False ) -> Optional[int]: """simple docstring""" if return_pvalue: __SCREAMING_SNAKE_CASE :Union[str, Any] = pearsonr(SCREAMING_SNAKE_CASE__ ,SCREAMING_SNAKE_CASE__ ) return {"pearsonr": results[0], "p-value": results[1]} else: return {"pearsonr": float(pearsonr(SCREAMING_SNAKE_CASE__ ,SCREAMING_SNAKE_CASE__ )[0] )}
239
"""simple docstring""" import warnings from collections import OrderedDict from typing import Any, Mapping, Optional from ... import PreTrainedTokenizer from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig, OnnxConfigWithPast, OnnxSeqaSeqConfigWithPast from ...onnx.utils import compute_effective_axis_dimension from ...utils import TensorType, is_torch_available, logging lowerCamelCase_ = logging.get_logger(__name__) lowerCamelCase_ = { "facebook/bart-large": "https://huggingface.co/facebook/bart-large/resolve/main/config.json", # See all BART models at https://huggingface.co/models?filter=bart } class _SCREAMING_SNAKE_CASE( A ): SCREAMING_SNAKE_CASE_ : str = '''bart''' SCREAMING_SNAKE_CASE_ : str = ['''past_key_values'''] SCREAMING_SNAKE_CASE_ : str = {'''num_attention_heads''': '''encoder_attention_heads''', '''hidden_size''': '''d_model'''} def __init__( self ,SCREAMING_SNAKE_CASE__=5_02_65 ,SCREAMING_SNAKE_CASE__=10_24 ,SCREAMING_SNAKE_CASE__=12 ,SCREAMING_SNAKE_CASE__=40_96 ,SCREAMING_SNAKE_CASE__=16 ,SCREAMING_SNAKE_CASE__=12 ,SCREAMING_SNAKE_CASE__=40_96 ,SCREAMING_SNAKE_CASE__=16 ,SCREAMING_SNAKE_CASE__=0.0 ,SCREAMING_SNAKE_CASE__=0.0 ,SCREAMING_SNAKE_CASE__="gelu" ,SCREAMING_SNAKE_CASE__=10_24 ,SCREAMING_SNAKE_CASE__=0.1 ,SCREAMING_SNAKE_CASE__=0.0 ,SCREAMING_SNAKE_CASE__=0.0 ,SCREAMING_SNAKE_CASE__=0.0_2 ,SCREAMING_SNAKE_CASE__=0.0 ,SCREAMING_SNAKE_CASE__=False ,SCREAMING_SNAKE_CASE__=True ,SCREAMING_SNAKE_CASE__=3 ,SCREAMING_SNAKE_CASE__=1 ,SCREAMING_SNAKE_CASE__=0 ,SCREAMING_SNAKE_CASE__=2 ,SCREAMING_SNAKE_CASE__=True ,SCREAMING_SNAKE_CASE__=2 ,SCREAMING_SNAKE_CASE__=2 ,**SCREAMING_SNAKE_CASE__ ,) -> int: """simple docstring""" __SCREAMING_SNAKE_CASE :str = vocab_size __SCREAMING_SNAKE_CASE :Union[str, Any] = max_position_embeddings __SCREAMING_SNAKE_CASE :Any = d_model __SCREAMING_SNAKE_CASE :Optional[int] = encoder_ffn_dim __SCREAMING_SNAKE_CASE :List[str] = encoder_layers __SCREAMING_SNAKE_CASE :Tuple = encoder_attention_heads __SCREAMING_SNAKE_CASE :List[Any] = decoder_ffn_dim __SCREAMING_SNAKE_CASE :Any = decoder_layers __SCREAMING_SNAKE_CASE :Optional[int] = decoder_attention_heads __SCREAMING_SNAKE_CASE :Optional[Any] = dropout __SCREAMING_SNAKE_CASE :Optional[Any] = attention_dropout __SCREAMING_SNAKE_CASE :Dict = activation_dropout __SCREAMING_SNAKE_CASE :Union[str, Any] = activation_function __SCREAMING_SNAKE_CASE :Union[str, Any] = init_std __SCREAMING_SNAKE_CASE :int = encoder_layerdrop __SCREAMING_SNAKE_CASE :Any = decoder_layerdrop __SCREAMING_SNAKE_CASE :str = classifier_dropout __SCREAMING_SNAKE_CASE :List[str] = use_cache __SCREAMING_SNAKE_CASE :List[str] = encoder_layers __SCREAMING_SNAKE_CASE :Tuple = scale_embedding # scale factor will be sqrt(d_model) if True super().__init__( num_labels=SCREAMING_SNAKE_CASE__ ,pad_token_id=SCREAMING_SNAKE_CASE__ ,bos_token_id=SCREAMING_SNAKE_CASE__ ,eos_token_id=SCREAMING_SNAKE_CASE__ ,is_encoder_decoder=SCREAMING_SNAKE_CASE__ ,decoder_start_token_id=SCREAMING_SNAKE_CASE__ ,forced_eos_token_id=SCREAMING_SNAKE_CASE__ ,**SCREAMING_SNAKE_CASE__ ,) # ensure backward compatibility for BART CNN models if self.forced_bos_token_id is None and kwargs.get('''force_bos_token_to_be_generated''' ,SCREAMING_SNAKE_CASE__ ): __SCREAMING_SNAKE_CASE :str = self.bos_token_id warnings.warn( f'''Please make sure the config includes `forced_bos_token_id={self.bos_token_id}` in future versions. ''' '''The config can simply be saved and uploaded again to be fixed.''' ) class _SCREAMING_SNAKE_CASE( A ): @property def _UpperCamelCase ( self ) -> Mapping[str, Mapping[int, str]]: """simple docstring""" if self.task in ["default", "seq2seq-lm"]: __SCREAMING_SNAKE_CASE :Optional[Any] = OrderedDict( [ ('''input_ids''', {0: '''batch''', 1: '''encoder_sequence'''}), ('''attention_mask''', {0: '''batch''', 1: '''encoder_sequence'''}), ] ) if self.use_past: __SCREAMING_SNAKE_CASE :int = {0: '''batch'''} __SCREAMING_SNAKE_CASE :int = {0: '''batch''', 1: '''past_decoder_sequence + sequence'''} else: __SCREAMING_SNAKE_CASE :Tuple = {0: '''batch''', 1: '''decoder_sequence'''} __SCREAMING_SNAKE_CASE :Any = {0: '''batch''', 1: '''decoder_sequence'''} if self.use_past: self.fill_with_past_key_values_(SCREAMING_SNAKE_CASE__ ,direction='''inputs''' ) elif self.task == "causal-lm": # TODO: figure this case out. __SCREAMING_SNAKE_CASE :Optional[int] = OrderedDict( [ ('''input_ids''', {0: '''batch''', 1: '''encoder_sequence'''}), ('''attention_mask''', {0: '''batch''', 1: '''encoder_sequence'''}), ] ) if self.use_past: __SCREAMING_SNAKE_CASE ,__SCREAMING_SNAKE_CASE :List[Any] = self.num_layers for i in range(SCREAMING_SNAKE_CASE__ ): __SCREAMING_SNAKE_CASE :List[str] = {0: '''batch''', 2: '''past_sequence + sequence'''} __SCREAMING_SNAKE_CASE :List[Any] = {0: '''batch''', 2: '''past_sequence + sequence'''} else: __SCREAMING_SNAKE_CASE :int = OrderedDict( [ ('''input_ids''', {0: '''batch''', 1: '''encoder_sequence'''}), ('''attention_mask''', {0: '''batch''', 1: '''encoder_sequence'''}), ('''decoder_input_ids''', {0: '''batch''', 1: '''decoder_sequence'''}), ('''decoder_attention_mask''', {0: '''batch''', 1: '''decoder_sequence'''}), ] ) return common_inputs @property def _UpperCamelCase ( self ) -> Mapping[str, Mapping[int, str]]: """simple docstring""" if self.task in ["default", "seq2seq-lm"]: __SCREAMING_SNAKE_CASE :str = super().outputs else: __SCREAMING_SNAKE_CASE :List[str] = super(SCREAMING_SNAKE_CASE__ ,self ).outputs if self.use_past: __SCREAMING_SNAKE_CASE ,__SCREAMING_SNAKE_CASE :Dict = self.num_layers for i in range(SCREAMING_SNAKE_CASE__ ): __SCREAMING_SNAKE_CASE :str = {0: '''batch''', 2: '''past_sequence + sequence'''} __SCREAMING_SNAKE_CASE :Any = {0: '''batch''', 2: '''past_sequence + sequence'''} return common_outputs def _UpperCamelCase ( self ,SCREAMING_SNAKE_CASE__ ,SCREAMING_SNAKE_CASE__ = -1 ,SCREAMING_SNAKE_CASE__ = -1 ,SCREAMING_SNAKE_CASE__ = False ,SCREAMING_SNAKE_CASE__ = None ,) -> Mapping[str, Any]: """simple docstring""" __SCREAMING_SNAKE_CASE :int = self._generate_dummy_inputs_for_sequence_classification_and_question_answering( SCREAMING_SNAKE_CASE__ ,SCREAMING_SNAKE_CASE__ ,SCREAMING_SNAKE_CASE__ ,SCREAMING_SNAKE_CASE__ ,SCREAMING_SNAKE_CASE__ ) # Generate decoder inputs __SCREAMING_SNAKE_CASE :Union[str, Any] = seq_length if not self.use_past else 1 __SCREAMING_SNAKE_CASE :Dict = self._generate_dummy_inputs_for_sequence_classification_and_question_answering( SCREAMING_SNAKE_CASE__ ,SCREAMING_SNAKE_CASE__ ,SCREAMING_SNAKE_CASE__ ,SCREAMING_SNAKE_CASE__ ,SCREAMING_SNAKE_CASE__ ) __SCREAMING_SNAKE_CASE :List[str] = {f'''decoder_{name}''': tensor for name, tensor in decoder_inputs.items()} __SCREAMING_SNAKE_CASE :Any = dict(**SCREAMING_SNAKE_CASE__ ,**SCREAMING_SNAKE_CASE__ ) if self.use_past: if not is_torch_available(): raise ValueError('''Cannot generate dummy past_keys inputs without PyTorch installed.''' ) else: import torch __SCREAMING_SNAKE_CASE ,__SCREAMING_SNAKE_CASE :Dict = common_inputs['''input_ids'''].shape __SCREAMING_SNAKE_CASE :Optional[Any] = common_inputs['''decoder_input_ids'''].shape[1] __SCREAMING_SNAKE_CASE ,__SCREAMING_SNAKE_CASE :Union[str, Any] = self.num_attention_heads __SCREAMING_SNAKE_CASE :Union[str, Any] = ( batch, num_encoder_attention_heads, encoder_seq_length, self._config.hidden_size // num_encoder_attention_heads, ) __SCREAMING_SNAKE_CASE :Optional[int] = decoder_seq_length + 3 __SCREAMING_SNAKE_CASE :Tuple = ( batch, num_decoder_attention_heads, decoder_past_length, self._config.hidden_size // num_decoder_attention_heads, ) __SCREAMING_SNAKE_CASE :Union[str, Any] = torch.cat( [common_inputs['''decoder_attention_mask'''], torch.ones(SCREAMING_SNAKE_CASE__ ,SCREAMING_SNAKE_CASE__ )] ,dim=1 ) __SCREAMING_SNAKE_CASE :Optional[Any] = [] # If the number of encoder and decoder layers are present in the model configuration, both are considered __SCREAMING_SNAKE_CASE ,__SCREAMING_SNAKE_CASE :Dict = self.num_layers __SCREAMING_SNAKE_CASE :int = min(SCREAMING_SNAKE_CASE__ ,SCREAMING_SNAKE_CASE__ ) __SCREAMING_SNAKE_CASE :Any = max(SCREAMING_SNAKE_CASE__ ,SCREAMING_SNAKE_CASE__ ) - min_num_layers __SCREAMING_SNAKE_CASE :int = '''encoder''' if num_encoder_layers > num_decoder_layers else '''decoder''' for _ in range(SCREAMING_SNAKE_CASE__ ): common_inputs["past_key_values"].append( ( torch.zeros(SCREAMING_SNAKE_CASE__ ), torch.zeros(SCREAMING_SNAKE_CASE__ ), torch.zeros(SCREAMING_SNAKE_CASE__ ), torch.zeros(SCREAMING_SNAKE_CASE__ ), ) ) # TODO: test this. __SCREAMING_SNAKE_CASE :Optional[int] = encoder_shape if remaining_side_name == '''encoder''' else decoder_shape for _ in range(SCREAMING_SNAKE_CASE__ ,SCREAMING_SNAKE_CASE__ ): common_inputs["past_key_values"].append((torch.zeros(SCREAMING_SNAKE_CASE__ ), torch.zeros(SCREAMING_SNAKE_CASE__ )) ) return common_inputs def _UpperCamelCase ( self ,SCREAMING_SNAKE_CASE__ ,SCREAMING_SNAKE_CASE__ = -1 ,SCREAMING_SNAKE_CASE__ = -1 ,SCREAMING_SNAKE_CASE__ = False ,SCREAMING_SNAKE_CASE__ = None ,) -> Mapping[str, Any]: """simple docstring""" __SCREAMING_SNAKE_CASE :Dict = self._generate_dummy_inputs_for_sequence_classification_and_question_answering( SCREAMING_SNAKE_CASE__ ,SCREAMING_SNAKE_CASE__ ,SCREAMING_SNAKE_CASE__ ,SCREAMING_SNAKE_CASE__ ,SCREAMING_SNAKE_CASE__ ) if self.use_past: if not is_torch_available(): raise ValueError('''Cannot generate dummy past_keys inputs without PyTorch installed.''' ) else: import torch __SCREAMING_SNAKE_CASE ,__SCREAMING_SNAKE_CASE :str = common_inputs['''input_ids'''].shape # Not using the same length for past_key_values __SCREAMING_SNAKE_CASE :List[str] = seqlen + 2 __SCREAMING_SNAKE_CASE ,__SCREAMING_SNAKE_CASE :Union[str, Any] = self.num_layers __SCREAMING_SNAKE_CASE ,__SCREAMING_SNAKE_CASE :int = self.num_attention_heads __SCREAMING_SNAKE_CASE :Tuple = ( batch, num_encoder_attention_heads, past_key_values_length, self._config.hidden_size // num_encoder_attention_heads, ) __SCREAMING_SNAKE_CASE :Tuple = common_inputs['''attention_mask'''].dtype __SCREAMING_SNAKE_CASE :Union[str, Any] = torch.cat( [common_inputs['''attention_mask'''], torch.ones(SCREAMING_SNAKE_CASE__ ,SCREAMING_SNAKE_CASE__ ,dtype=SCREAMING_SNAKE_CASE__ )] ,dim=1 ) __SCREAMING_SNAKE_CASE :str = [ (torch.zeros(SCREAMING_SNAKE_CASE__ ), torch.zeros(SCREAMING_SNAKE_CASE__ )) for _ in range(SCREAMING_SNAKE_CASE__ ) ] return common_inputs def _UpperCamelCase ( self ,SCREAMING_SNAKE_CASE__ ,SCREAMING_SNAKE_CASE__ = -1 ,SCREAMING_SNAKE_CASE__ = -1 ,SCREAMING_SNAKE_CASE__ = False ,SCREAMING_SNAKE_CASE__ = None ,) -> Mapping[str, Any]: """simple docstring""" __SCREAMING_SNAKE_CASE :List[Any] = compute_effective_axis_dimension( SCREAMING_SNAKE_CASE__ ,fixed_dimension=OnnxConfig.default_fixed_batch ,num_token_to_add=0 ) # If dynamic axis (-1) we forward with a fixed dimension of 8 tokens to avoid optimizations made by ONNX __SCREAMING_SNAKE_CASE :Optional[Any] = tokenizer.num_special_tokens_to_add(SCREAMING_SNAKE_CASE__ ) __SCREAMING_SNAKE_CASE :List[Any] = compute_effective_axis_dimension( SCREAMING_SNAKE_CASE__ ,fixed_dimension=OnnxConfig.default_fixed_sequence ,num_token_to_add=SCREAMING_SNAKE_CASE__ ) # Generate dummy inputs according to compute batch and sequence __SCREAMING_SNAKE_CASE :List[Any] = [''' '''.join([tokenizer.unk_token] ) * seq_length] * batch_size __SCREAMING_SNAKE_CASE :str = dict(tokenizer(SCREAMING_SNAKE_CASE__ ,return_tensors=SCREAMING_SNAKE_CASE__ ) ) return common_inputs def _UpperCamelCase ( self ,SCREAMING_SNAKE_CASE__ ,SCREAMING_SNAKE_CASE__ = -1 ,SCREAMING_SNAKE_CASE__ = -1 ,SCREAMING_SNAKE_CASE__ = False ,SCREAMING_SNAKE_CASE__ = None ,) -> Mapping[str, Any]: """simple docstring""" if self.task in ["default", "seq2seq-lm"]: __SCREAMING_SNAKE_CASE :str = self._generate_dummy_inputs_for_default_and_seqaseq_lm( SCREAMING_SNAKE_CASE__ ,batch_size=SCREAMING_SNAKE_CASE__ ,seq_length=SCREAMING_SNAKE_CASE__ ,is_pair=SCREAMING_SNAKE_CASE__ ,framework=SCREAMING_SNAKE_CASE__ ) elif self.task == "causal-lm": __SCREAMING_SNAKE_CASE :int = self._generate_dummy_inputs_for_causal_lm( SCREAMING_SNAKE_CASE__ ,batch_size=SCREAMING_SNAKE_CASE__ ,seq_length=SCREAMING_SNAKE_CASE__ ,is_pair=SCREAMING_SNAKE_CASE__ ,framework=SCREAMING_SNAKE_CASE__ ) else: __SCREAMING_SNAKE_CASE :int = self._generate_dummy_inputs_for_sequence_classification_and_question_answering( SCREAMING_SNAKE_CASE__ ,batch_size=SCREAMING_SNAKE_CASE__ ,seq_length=SCREAMING_SNAKE_CASE__ ,is_pair=SCREAMING_SNAKE_CASE__ ,framework=SCREAMING_SNAKE_CASE__ ) return common_inputs def _UpperCamelCase ( self ,SCREAMING_SNAKE_CASE__ ,SCREAMING_SNAKE_CASE__ ,SCREAMING_SNAKE_CASE__ ,SCREAMING_SNAKE_CASE__ ) -> str: """simple docstring""" if self.task in ["default", "seq2seq-lm"]: __SCREAMING_SNAKE_CASE :Dict = super()._flatten_past_key_values_(SCREAMING_SNAKE_CASE__ ,SCREAMING_SNAKE_CASE__ ,SCREAMING_SNAKE_CASE__ ,SCREAMING_SNAKE_CASE__ ) else: __SCREAMING_SNAKE_CASE :Dict = super(SCREAMING_SNAKE_CASE__ ,self )._flatten_past_key_values_( SCREAMING_SNAKE_CASE__ ,SCREAMING_SNAKE_CASE__ ,SCREAMING_SNAKE_CASE__ ,SCREAMING_SNAKE_CASE__ )
239
1
import os import unicodedata from shutil import copyfile from typing import Any, Dict, List, Optional, Tuple import sentencepiece as spm from ...tokenization_utils import AddedToken, PreTrainedTokenizer from ...utils import SPIECE_UNDERLINE, logging _lowerCamelCase =logging.get_logger(__name__) _lowerCamelCase ={"""vocab_file""": """spiece.model"""} _lowerCamelCase ={ """vocab_file""": { """xlnet-base-cased""": """https://huggingface.co/xlnet-base-cased/resolve/main/spiece.model""", """xlnet-large-cased""": """https://huggingface.co/xlnet-large-cased/resolve/main/spiece.model""", } } _lowerCamelCase ={ """xlnet-base-cased""": None, """xlnet-large-cased""": None, } # Segments (not really needed) _lowerCamelCase =0 _lowerCamelCase =1 _lowerCamelCase =2 _lowerCamelCase =3 _lowerCamelCase =4 class A__ ( __SCREAMING_SNAKE_CASE): _UpperCAmelCase : str = VOCAB_FILES_NAMES _UpperCAmelCase : Tuple = PRETRAINED_VOCAB_FILES_MAP _UpperCAmelCase : Optional[Any] = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES _UpperCAmelCase : Optional[Any] = """left""" def __init__( self , __magic_name__ , __magic_name__=False , __magic_name__=True , __magic_name__=False , __magic_name__="<s>" , __magic_name__="</s>" , __magic_name__="<unk>" , __magic_name__="<sep>" , __magic_name__="<pad>" , __magic_name__="<cls>" , __magic_name__="<mask>" , __magic_name__=["<eop>", "<eod>"] , __magic_name__ = None , **__magic_name__ , ): lowerCamelCase : Dict = AddedToken(_SCREAMING_SNAKE_CASE , lstrip=_SCREAMING_SNAKE_CASE , rstrip=_SCREAMING_SNAKE_CASE ) if isinstance(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) else mask_token lowerCamelCase : Tuple = {} if sp_model_kwargs is None else sp_model_kwargs super().__init__( do_lower_case=_SCREAMING_SNAKE_CASE , remove_space=_SCREAMING_SNAKE_CASE , keep_accents=_SCREAMING_SNAKE_CASE , bos_token=_SCREAMING_SNAKE_CASE , eos_token=_SCREAMING_SNAKE_CASE , unk_token=_SCREAMING_SNAKE_CASE , sep_token=_SCREAMING_SNAKE_CASE , pad_token=_SCREAMING_SNAKE_CASE , cls_token=_SCREAMING_SNAKE_CASE , mask_token=_SCREAMING_SNAKE_CASE , additional_special_tokens=_SCREAMING_SNAKE_CASE , sp_model_kwargs=self.sp_model_kwargs , **_SCREAMING_SNAKE_CASE , ) lowerCamelCase : Optional[Any] = 3 lowerCamelCase : Optional[int] = do_lower_case lowerCamelCase : Optional[int] = remove_space lowerCamelCase : Optional[Any] = keep_accents lowerCamelCase : int = vocab_file lowerCamelCase : str = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(_SCREAMING_SNAKE_CASE ) @property def UpperCamelCase__ ( self ): return len(self.sp_model ) def UpperCamelCase__ ( self ): lowerCamelCase : Dict = {self.convert_ids_to_tokens(_SCREAMING_SNAKE_CASE ): i for i in range(self.vocab_size )} vocab.update(self.added_tokens_encoder ) return vocab def __getstate__( self ): lowerCamelCase : Any = self.__dict__.copy() lowerCamelCase : Dict = None return state def __setstate__( self , __magic_name__ ): lowerCamelCase : List[Any] = d # for backward compatibility if not hasattr(self , """sp_model_kwargs""" ): lowerCamelCase : Tuple = {} lowerCamelCase : Any = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(self.vocab_file ) def UpperCamelCase__ ( self , __magic_name__ ): if self.remove_space: lowerCamelCase : List[Any] = """ """.join(inputs.strip().split() ) else: lowerCamelCase : Optional[Any] = inputs lowerCamelCase : Union[str, Any] = outputs.replace("""``""" , """\"""" ).replace("""''""" , """\"""" ) if not self.keep_accents: lowerCamelCase : List[Any] = unicodedata.normalize("""NFKD""" , _SCREAMING_SNAKE_CASE ) lowerCamelCase : Union[str, Any] = """""".join([c for c in outputs if not unicodedata.combining(_SCREAMING_SNAKE_CASE )] ) if self.do_lower_case: lowerCamelCase : Optional[Any] = outputs.lower() return outputs def UpperCamelCase__ ( self , __magic_name__ ): lowerCamelCase : Tuple = self.preprocess_text(_SCREAMING_SNAKE_CASE ) lowerCamelCase : List[Any] = self.sp_model.encode(_SCREAMING_SNAKE_CASE , out_type=_SCREAMING_SNAKE_CASE ) lowerCamelCase : Dict = [] for piece in pieces: if len(_SCREAMING_SNAKE_CASE ) > 1 and piece[-1] == str(""",""" ) and piece[-2].isdigit(): lowerCamelCase : Optional[Any] = self.sp_model.EncodeAsPieces(piece[:-1].replace(_SCREAMING_SNAKE_CASE , """""" ) ) if piece[0] != SPIECE_UNDERLINE and cur_pieces[0][0] == SPIECE_UNDERLINE: if len(cur_pieces[0] ) == 1: lowerCamelCase : Optional[Any] = cur_pieces[1:] else: lowerCamelCase : Optional[Any] = cur_pieces[0][1:] cur_pieces.append(piece[-1] ) new_pieces.extend(_SCREAMING_SNAKE_CASE ) else: new_pieces.append(_SCREAMING_SNAKE_CASE ) return new_pieces def UpperCamelCase__ ( self , __magic_name__ ): return self.sp_model.PieceToId(_SCREAMING_SNAKE_CASE ) def UpperCamelCase__ ( self , __magic_name__ ): return self.sp_model.IdToPiece(_SCREAMING_SNAKE_CASE ) def UpperCamelCase__ ( self , __magic_name__ ): lowerCamelCase : Optional[int] = """""".join(_SCREAMING_SNAKE_CASE ).replace(_SCREAMING_SNAKE_CASE , """ """ ).strip() return out_string def UpperCamelCase__ ( self , __magic_name__ , __magic_name__ = False , __magic_name__ = None , __magic_name__ = True , **__magic_name__ , ): lowerCamelCase : List[Any] = kwargs.pop("""use_source_tokenizer""" , _SCREAMING_SNAKE_CASE ) lowerCamelCase : Tuple = self.convert_ids_to_tokens(_SCREAMING_SNAKE_CASE , skip_special_tokens=_SCREAMING_SNAKE_CASE ) # To avoid mixing byte-level and unicode for byte-level BPT # we need to build string separately for added tokens and byte-level tokens # cf. https://github.com/huggingface/transformers/issues/1133 lowerCamelCase : Union[str, Any] = [] lowerCamelCase : Union[str, Any] = [] for token in filtered_tokens: if skip_special_tokens and token in self.all_special_ids: continue if token in self.added_tokens_encoder: if current_sub_text: sub_texts.append(self.convert_tokens_to_string(_SCREAMING_SNAKE_CASE ) ) lowerCamelCase : Optional[int] = [] sub_texts.append(_SCREAMING_SNAKE_CASE ) else: current_sub_text.append(_SCREAMING_SNAKE_CASE ) if current_sub_text: sub_texts.append(self.convert_tokens_to_string(_SCREAMING_SNAKE_CASE ) ) # Mimic the behavior of the Rust tokenizer: # By default, there are no spaces between special tokens lowerCamelCase : Optional[Any] = """""".join(_SCREAMING_SNAKE_CASE ) lowerCamelCase : Any = ( clean_up_tokenization_spaces if clean_up_tokenization_spaces is not None else self.clean_up_tokenization_spaces ) if clean_up_tokenization_spaces: lowerCamelCase : Dict = self.clean_up_tokenization(_SCREAMING_SNAKE_CASE ) return clean_text else: return text def UpperCamelCase__ ( self , __magic_name__ , __magic_name__ = None ): lowerCamelCase : Dict = [self.sep_token_id] lowerCamelCase : Dict = [self.cls_token_id] if token_ids_a is None: return token_ids_a + sep + cls return token_ids_a + sep + token_ids_a + sep + cls def UpperCamelCase__ ( self , __magic_name__ , __magic_name__ = None , __magic_name__ = False ): if already_has_special_tokens: return super().get_special_tokens_mask( token_ids_a=_SCREAMING_SNAKE_CASE , token_ids_a=_SCREAMING_SNAKE_CASE , already_has_special_tokens=_SCREAMING_SNAKE_CASE ) if token_ids_a is not None: return ([0] * len(_SCREAMING_SNAKE_CASE )) + [1] + ([0] * len(_SCREAMING_SNAKE_CASE )) + [1, 1] return ([0] * len(_SCREAMING_SNAKE_CASE )) + [1, 1] def UpperCamelCase__ ( self , __magic_name__ , __magic_name__ = None ): lowerCamelCase : Dict = [self.sep_token_id] lowerCamelCase : Union[str, Any] = [2] if token_ids_a is None: return len(token_ids_a + sep ) * [0] + cls_segment_id return len(token_ids_a + sep ) * [0] + len(token_ids_a + sep ) * [1] + cls_segment_id def UpperCamelCase__ ( self , __magic_name__ , __magic_name__ = None ): if not os.path.isdir(_SCREAMING_SNAKE_CASE ): logger.error(F'''Vocabulary path ({save_directory}) should be a directory''' ) return lowerCamelCase : Optional[int] = os.path.join( _SCREAMING_SNAKE_CASE , (filename_prefix + """-""" if filename_prefix else """""") + VOCAB_FILES_NAMES["""vocab_file"""] ) if os.path.abspath(self.vocab_file ) != os.path.abspath(_SCREAMING_SNAKE_CASE ) and os.path.isfile(self.vocab_file ): copyfile(self.vocab_file , _SCREAMING_SNAKE_CASE ) elif not os.path.isfile(self.vocab_file ): with open(_SCREAMING_SNAKE_CASE , """wb""" ) as fi: lowerCamelCase : str = self.sp_model.serialized_model_proto() fi.write(_SCREAMING_SNAKE_CASE ) return (out_vocab_file,)
287
'''simple docstring''' # Algorithm for the pigeonhole sorting def lowercase__ ( __UpperCamelCase )-> Union[str, Any]: UpperCamelCase = min(__UpperCamelCase ) # min() finds the minimum value UpperCamelCase = max(__UpperCamelCase ) # max() finds the maximum value UpperCamelCase = max_val - min_val + 1 # size is difference of max and min values plus one # list of pigeonholes of size equal to the variable size UpperCamelCase = [0] * size # Populate the pigeonholes. for x in a: assert isinstance(__UpperCamelCase , __UpperCamelCase ), "integers only please" holes[x - min_val] += 1 # Putting the elements back into the array in an order. UpperCamelCase = 0 for count in range(__UpperCamelCase ): while holes[count] > 0: holes[count] -= 1 UpperCamelCase = count + min_val i += 1 def lowercase__ ( )-> Any: UpperCamelCase = [8, 3, 2, 7, 4, 6, 8] pigeonhole_sort(__UpperCamelCase ) print("""Sorted order is:""" , """ """.join(__UpperCamelCase ) ) if __name__ == "__main__": main()
321
0
lowerCAmelCase__ :str = [ '''DownloadConfig''', '''DownloadManager''', '''DownloadMode''', '''StreamingDownloadManager''', ] from .download_config import DownloadConfig from .download_manager import DownloadManager, DownloadMode from .streaming_download_manager import StreamingDownloadManager
355
from __future__ import annotations import time from collections.abc import Sequence from random import randint from matplotlib import pyplot as plt def lowerCAmelCase__ ( a__: Sequence[float] , a__: int , a__: int ) -> tuple[int | None, int | None, float]: '''simple docstring''' if not arr: return None, None, 0 if low == high: return low, high, arr[low] _UpperCAmelCase = (low + high) // 2 _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase = max_subarray(a__ , a__ , a__ ) _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase = max_subarray(a__ , mid + 1 , a__ ) _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase = max_cross_sum(a__ , a__ , a__ , a__ ) if left_sum >= right_sum and left_sum >= cross_sum: return left_low, left_high, left_sum elif right_sum >= left_sum and right_sum >= cross_sum: return right_low, right_high, right_sum return cross_left, cross_right, cross_sum def lowerCAmelCase__ ( a__: Sequence[float] , a__: int , a__: int , a__: int ) -> tuple[int, int, float]: '''simple docstring''' _UpperCAmelCase , _UpperCAmelCase = float('-inf' ), -1 _UpperCAmelCase , _UpperCAmelCase = float('-inf' ), -1 _UpperCAmelCase = 0 for i in range(a__ , low - 1 , -1 ): summ += arr[i] if summ > left_sum: _UpperCAmelCase = summ _UpperCAmelCase = i _UpperCAmelCase = 0 for i in range(mid + 1 , high + 1 ): summ += arr[i] if summ > right_sum: _UpperCAmelCase = summ _UpperCAmelCase = i return max_left, max_right, (left_sum + right_sum) def lowerCAmelCase__ ( a__: int ) -> float: '''simple docstring''' _UpperCAmelCase = [randint(1 , a__ ) for _ in range(a__ )] _UpperCAmelCase = time.time() max_subarray(a__ , 0 , input_size - 1 ) _UpperCAmelCase = time.time() return end - start def lowerCAmelCase__ ( ) -> None: '''simple docstring''' _UpperCAmelCase = [1_0, 1_0_0, 1_0_0_0, 1_0_0_0_0, 5_0_0_0_0, 1_0_0_0_0_0, 2_0_0_0_0_0, 3_0_0_0_0_0, 4_0_0_0_0_0, 5_0_0_0_0_0] _UpperCAmelCase = [time_max_subarray(a__ ) for input_size in input_sizes] print('No of Inputs\t\tTime Taken' ) for input_size, runtime in zip(a__ , a__ ): print(a__ , '\t\t' , a__ ) plt.plot(a__ , a__ ) plt.xlabel('Number of Inputs' ) plt.ylabel('Time taken in seconds' ) plt.show() if __name__ == "__main__": from doctest import testmod testmod()
185
0
"""simple docstring""" import gc import math import unittest import torch from diffusers import UNetaDModel from diffusers.utils import floats_tensor, logging, slow, torch_all_close, torch_device from diffusers.utils.testing_utils import enable_full_determinism from .test_modeling_common import ModelTesterMixin, UNetTesterMixin A: str = logging.get_logger(__name__) enable_full_determinism() class SCREAMING_SNAKE_CASE__ ( UpperCAmelCase__ , UpperCAmelCase__ , unittest.TestCase ): __lowerCAmelCase : Optional[Any] = UNetaDModel __lowerCAmelCase : Tuple = 'sample' @property def SCREAMING_SNAKE_CASE ( self ) -> Union[str, Any]: '''simple docstring''' UpperCAmelCase : Optional[int] = 4 UpperCAmelCase : Dict = 3 UpperCAmelCase : Optional[Any] = (32, 32) UpperCAmelCase : Optional[int] = floats_tensor((batch_size, num_channels) + sizes ).to(_SCREAMING_SNAKE_CASE ) UpperCAmelCase : List[str] = torch.tensor([10] ).to(_SCREAMING_SNAKE_CASE ) return {"sample": noise, "timestep": time_step} @property def SCREAMING_SNAKE_CASE ( self ) -> Any: '''simple docstring''' return (3, 32, 32) @property def SCREAMING_SNAKE_CASE ( self ) -> Optional[Any]: '''simple docstring''' return (3, 32, 32) def SCREAMING_SNAKE_CASE ( self ) -> List[Any]: '''simple docstring''' UpperCAmelCase : int = { """block_out_channels""": (32, 64), """down_block_types""": ("""DownBlock2D""", """AttnDownBlock2D"""), """up_block_types""": ("""AttnUpBlock2D""", """UpBlock2D"""), """attention_head_dim""": 3, """out_channels""": 3, """in_channels""": 3, """layers_per_block""": 2, """sample_size""": 32, } UpperCAmelCase : List[str] = self.dummy_input return init_dict, inputs_dict class SCREAMING_SNAKE_CASE__ ( UpperCAmelCase__ , UpperCAmelCase__ , unittest.TestCase ): __lowerCAmelCase : int = UNetaDModel __lowerCAmelCase : List[str] = 'sample' @property def SCREAMING_SNAKE_CASE ( self ) -> str: '''simple docstring''' UpperCAmelCase : str = 4 UpperCAmelCase : str = 4 UpperCAmelCase : Any = (32, 32) UpperCAmelCase : int = floats_tensor((batch_size, num_channels) + sizes ).to(_SCREAMING_SNAKE_CASE ) UpperCAmelCase : str = torch.tensor([10] ).to(_SCREAMING_SNAKE_CASE ) return {"sample": noise, "timestep": time_step} @property def SCREAMING_SNAKE_CASE ( self ) -> Tuple: '''simple docstring''' return (4, 32, 32) @property def SCREAMING_SNAKE_CASE ( self ) -> Tuple: '''simple docstring''' return (4, 32, 32) def SCREAMING_SNAKE_CASE ( self ) -> Any: '''simple docstring''' UpperCAmelCase : int = { """sample_size""": 32, """in_channels""": 4, """out_channels""": 4, """layers_per_block""": 2, """block_out_channels""": (32, 64), """attention_head_dim""": 32, """down_block_types""": ("""DownBlock2D""", """DownBlock2D"""), """up_block_types""": ("""UpBlock2D""", """UpBlock2D"""), } UpperCAmelCase : Tuple = self.dummy_input return init_dict, inputs_dict def SCREAMING_SNAKE_CASE ( self ) -> Any: '''simple docstring''' UpperCAmelCase , UpperCAmelCase : List[Any] = UNetaDModel.from_pretrained("""fusing/unet-ldm-dummy-update""" , output_loading_info=_SCREAMING_SNAKE_CASE ) self.assertIsNotNone(_SCREAMING_SNAKE_CASE ) self.assertEqual(len(loading_info["""missing_keys"""] ) , 0 ) model.to(_SCREAMING_SNAKE_CASE ) UpperCAmelCase : Optional[Any] = model(**self.dummy_input ).sample assert image is not None, "Make sure output is not None" @unittest.skipIf(torch_device != """cuda""" , """This test is supposed to run on GPU""" ) def SCREAMING_SNAKE_CASE ( self ) -> str: '''simple docstring''' UpperCAmelCase , UpperCAmelCase : Dict = UNetaDModel.from_pretrained("""fusing/unet-ldm-dummy-update""" , output_loading_info=_SCREAMING_SNAKE_CASE ) model.to(_SCREAMING_SNAKE_CASE ) UpperCAmelCase : str = model(**self.dummy_input ).sample assert image is not None, "Make sure output is not None" @unittest.skipIf(torch_device != """cuda""" , """This test is supposed to run on GPU""" ) def SCREAMING_SNAKE_CASE ( self ) -> Tuple: '''simple docstring''' UpperCAmelCase , UpperCAmelCase : Optional[Any] = UNetaDModel.from_pretrained("""fusing/unet-ldm-dummy-update""" , output_loading_info=_SCREAMING_SNAKE_CASE ) model_accelerate.to(_SCREAMING_SNAKE_CASE ) model_accelerate.eval() UpperCAmelCase : Union[str, Any] = torch.randn( 1 , model_accelerate.config.in_channels , model_accelerate.config.sample_size , model_accelerate.config.sample_size , generator=torch.manual_seed(0 ) , ) UpperCAmelCase : Dict = noise.to(_SCREAMING_SNAKE_CASE ) UpperCAmelCase : Optional[int] = torch.tensor([10] * noise.shape[0] ).to(_SCREAMING_SNAKE_CASE ) UpperCAmelCase : int = model_accelerate(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE )["""sample"""] # two models don't need to stay in the device at the same time del model_accelerate torch.cuda.empty_cache() gc.collect() UpperCAmelCase , UpperCAmelCase : Union[str, Any] = UNetaDModel.from_pretrained( """fusing/unet-ldm-dummy-update""" , output_loading_info=_SCREAMING_SNAKE_CASE , low_cpu_mem_usage=_SCREAMING_SNAKE_CASE ) model_normal_load.to(_SCREAMING_SNAKE_CASE ) model_normal_load.eval() UpperCAmelCase : int = model_normal_load(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE )["""sample"""] assert torch_all_close(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , rtol=1E-3 ) def SCREAMING_SNAKE_CASE ( self ) -> Dict: '''simple docstring''' UpperCAmelCase : int = UNetaDModel.from_pretrained("""fusing/unet-ldm-dummy-update""" ) model.eval() model.to(_SCREAMING_SNAKE_CASE ) UpperCAmelCase : Tuple = torch.randn( 1 , model.config.in_channels , model.config.sample_size , model.config.sample_size , generator=torch.manual_seed(0 ) , ) UpperCAmelCase : Optional[Any] = noise.to(_SCREAMING_SNAKE_CASE ) UpperCAmelCase : Optional[Any] = torch.tensor([10] * noise.shape[0] ).to(_SCREAMING_SNAKE_CASE ) with torch.no_grad(): UpperCAmelCase : List[Any] = model(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ).sample UpperCAmelCase : Tuple = output[0, -1, -3:, -3:].flatten().cpu() # fmt: off UpperCAmelCase : List[Any] = torch.tensor([-13.3258, -20.1100, -15.9873, -17.6617, -23.0596, -17.9419, -13.3675, -16.1889, -12.3800] ) # fmt: on self.assertTrue(torch_all_close(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , rtol=1E-3 ) ) class SCREAMING_SNAKE_CASE__ ( UpperCAmelCase__ , UpperCAmelCase__ , unittest.TestCase ): __lowerCAmelCase : List[str] = UNetaDModel __lowerCAmelCase : List[str] = 'sample' @property def SCREAMING_SNAKE_CASE ( self , _SCREAMING_SNAKE_CASE=(32, 32) ) -> str: '''simple docstring''' UpperCAmelCase : Optional[Any] = 4 UpperCAmelCase : Tuple = 3 UpperCAmelCase : List[str] = floats_tensor((batch_size, num_channels) + sizes ).to(_SCREAMING_SNAKE_CASE ) UpperCAmelCase : Dict = torch.tensor(batch_size * [10] ).to(dtype=torch.intaa , device=_SCREAMING_SNAKE_CASE ) return {"sample": noise, "timestep": time_step} @property def SCREAMING_SNAKE_CASE ( self ) -> Any: '''simple docstring''' return (3, 32, 32) @property def SCREAMING_SNAKE_CASE ( self ) -> str: '''simple docstring''' return (3, 32, 32) def SCREAMING_SNAKE_CASE ( self ) -> int: '''simple docstring''' UpperCAmelCase : int = { """block_out_channels""": [32, 64, 64, 64], """in_channels""": 3, """layers_per_block""": 1, """out_channels""": 3, """time_embedding_type""": """fourier""", """norm_eps""": 1E-6, """mid_block_scale_factor""": math.sqrt(2.0 ), """norm_num_groups""": None, """down_block_types""": [ """SkipDownBlock2D""", """AttnSkipDownBlock2D""", """SkipDownBlock2D""", """SkipDownBlock2D""", ], """up_block_types""": [ """SkipUpBlock2D""", """SkipUpBlock2D""", """AttnSkipUpBlock2D""", """SkipUpBlock2D""", ], } UpperCAmelCase : Optional[Any] = self.dummy_input return init_dict, inputs_dict @slow def SCREAMING_SNAKE_CASE ( self ) -> Any: '''simple docstring''' UpperCAmelCase , UpperCAmelCase : List[Any] = UNetaDModel.from_pretrained("""google/ncsnpp-celebahq-256""" , output_loading_info=_SCREAMING_SNAKE_CASE ) self.assertIsNotNone(_SCREAMING_SNAKE_CASE ) self.assertEqual(len(loading_info["""missing_keys"""] ) , 0 ) model.to(_SCREAMING_SNAKE_CASE ) UpperCAmelCase : Dict = self.dummy_input UpperCAmelCase : int = floats_tensor((4, 3) + (256, 256) ).to(_SCREAMING_SNAKE_CASE ) UpperCAmelCase : Optional[Any] = noise UpperCAmelCase : Optional[int] = model(**_SCREAMING_SNAKE_CASE ) assert image is not None, "Make sure output is not None" @slow def SCREAMING_SNAKE_CASE ( self ) -> Tuple: '''simple docstring''' UpperCAmelCase : int = UNetaDModel.from_pretrained("""google/ncsnpp-celebahq-256""" ) model.to(_SCREAMING_SNAKE_CASE ) UpperCAmelCase : Optional[Any] = 4 UpperCAmelCase : Optional[Any] = 3 UpperCAmelCase : List[Any] = (256, 256) UpperCAmelCase : int = torch.ones((batch_size, num_channels) + sizes ).to(_SCREAMING_SNAKE_CASE ) UpperCAmelCase : Optional[Any] = torch.tensor(batch_size * [1E-4] ).to(_SCREAMING_SNAKE_CASE ) with torch.no_grad(): UpperCAmelCase : Dict = model(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ).sample UpperCAmelCase : str = output[0, -3:, -3:, -1].flatten().cpu() # fmt: off UpperCAmelCase : Optional[int] = torch.tensor([-4842.8691, -6499.6631, -3800.1953, -7978.2686, -1_0980.7129, -2_0028.8535, 8148.2822, 2342.2905, 567.7608] ) # fmt: on self.assertTrue(torch_all_close(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , rtol=1E-2 ) ) def SCREAMING_SNAKE_CASE ( self ) -> Dict: '''simple docstring''' UpperCAmelCase : int = UNetaDModel.from_pretrained("""fusing/ncsnpp-ffhq-ve-dummy-update""" ) model.to(_SCREAMING_SNAKE_CASE ) UpperCAmelCase : Dict = 4 UpperCAmelCase : Optional[int] = 3 UpperCAmelCase : Optional[Any] = (32, 32) UpperCAmelCase : List[Any] = torch.ones((batch_size, num_channels) + sizes ).to(_SCREAMING_SNAKE_CASE ) UpperCAmelCase : int = torch.tensor(batch_size * [1E-4] ).to(_SCREAMING_SNAKE_CASE ) with torch.no_grad(): UpperCAmelCase : int = model(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ).sample UpperCAmelCase : Any = output[0, -3:, -3:, -1].flatten().cpu() # fmt: off UpperCAmelCase : int = torch.tensor([-0.0325, -0.0900, -0.0869, -0.0332, -0.0725, -0.0270, -0.0101, 0.0227, 0.0256] ) # fmt: on self.assertTrue(torch_all_close(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , rtol=1E-2 ) ) def SCREAMING_SNAKE_CASE ( self ) -> Union[str, Any]: '''simple docstring''' pass
109
'''simple docstring''' import enum import warnings from .. import MODEL_FOR_CAUSAL_LM_MAPPING, TF_MODEL_FOR_CAUSAL_LM_MAPPING from ..utils import add_end_docstrings, is_tf_available from .base import PIPELINE_INIT_ARGS, Pipeline if is_tf_available(): import tensorflow as tf class lowercase ( enum.Enum ): """simple docstring""" UpperCAmelCase = 0 UpperCAmelCase = 1 UpperCAmelCase = 2 @add_end_docstrings(_lowerCamelCase ) class lowercase ( _lowerCamelCase ): """simple docstring""" UpperCAmelCase = """ In 1991, the remains of Russian Tsar Nicholas II and his family (except for Alexei and Maria) are discovered. The voice of Nicholas's young son, Tsarevich Alexei Nikolaevich, narrates the remainder of the story. 1883 Western Siberia, a young Grigori Rasputin is asked by his father and a group of men to perform magic. Rasputin has a vision and denounces one of the men as a horse thief. Although his father initially slaps him for making such an accusation, Rasputin watches as the man is chased outside and beaten. Twenty years later, Rasputin sees a vision of the Virgin Mary, prompting him to become a priest. Rasputin quickly becomes famous, with people, even a bishop, begging for his blessing. <eod> </s> <eos> """ def __init__( self ,*a_ ,**a_ ) -> Union[str, Any]: super().__init__(*a_ ,**a_ ) self.check_model_type( TF_MODEL_FOR_CAUSAL_LM_MAPPING if self.framework == """tf""" else MODEL_FOR_CAUSAL_LM_MAPPING ) if "prefix" not in self._preprocess_params: # This is very specific. The logic is quite complex and needs to be done # as a "default". # It also defines both some preprocess_kwargs and generate_kwargs # which is why we cannot put them in their respective methods. _UpperCAmelCase : List[str] = None if self.model.config.prefix is not None: _UpperCAmelCase : Any = self.model.config.prefix if prefix is None and self.model.__class__.__name__ in [ "XLNetLMHeadModel", "TransfoXLLMHeadModel", "TFXLNetLMHeadModel", "TFTransfoXLLMHeadModel", ]: # For XLNet and TransformerXL we add an article to the prompt to give more state to the model. _UpperCAmelCase : Union[str, Any] = self.XL_PREFIX if prefix is not None: # Recalculate some generate_kwargs linked to prefix. _UpperCAmelCase ,_UpperCAmelCase ,_UpperCAmelCase : List[Any] = self._sanitize_parameters(prefix=a_ ,**self._forward_params ) _UpperCAmelCase : Optional[int] = {**self._preprocess_params, **preprocess_params} _UpperCAmelCase : List[Any] = {**self._forward_params, **forward_params} def _snake_case ( self ,a_=None ,a_=None ,a_=None ,a_=None ,a_=None ,a_=None ,a_=None ,a_=None ,**a_ ,) -> Dict: _UpperCAmelCase : int = {} if prefix is not None: _UpperCAmelCase : Union[str, Any] = prefix if prefix: _UpperCAmelCase : Union[str, Any] = self.tokenizer( a_ ,padding=a_ ,add_special_tokens=a_ ,return_tensors=self.framework ) _UpperCAmelCase : Optional[int] = prefix_inputs["""input_ids"""].shape[-1] if handle_long_generation is not None: if handle_long_generation not in {"hole"}: raise ValueError( f'''{handle_long_generation} is not a valid value for `handle_long_generation` parameter expected''' """ [None, 'hole']""" ) _UpperCAmelCase : Optional[Any] = handle_long_generation preprocess_params.update(a_ ) _UpperCAmelCase : str = generate_kwargs _UpperCAmelCase : str = {} if return_full_text is not None and return_type is None: if return_text is not None: raise ValueError("""`return_text` is mutually exclusive with `return_full_text`""" ) if return_tensors is not None: raise ValueError("""`return_full_text` is mutually exclusive with `return_tensors`""" ) _UpperCAmelCase : Any = ReturnType.FULL_TEXT if return_full_text else ReturnType.NEW_TEXT if return_tensors is not None and return_type is None: if return_text is not None: raise ValueError("""`return_text` is mutually exclusive with `return_tensors`""" ) _UpperCAmelCase : Tuple = ReturnType.TENSORS if return_type is not None: _UpperCAmelCase : int = return_type if clean_up_tokenization_spaces is not None: _UpperCAmelCase : Tuple = clean_up_tokenization_spaces if stop_sequence is not None: _UpperCAmelCase : str = self.tokenizer.encode(a_ ,add_special_tokens=a_ ) if len(a_ ) > 1: warnings.warn( """Stopping on a multiple token sequence is not yet supported on transformers. The first token of""" """ the stop sequence will be used as the stop sequence string in the interim.""" ) _UpperCAmelCase : str = stop_sequence_ids[0] return preprocess_params, forward_params, postprocess_params def _snake_case ( self ,*a_ ,**a_ ) -> Dict: # Parse arguments if self.model.__class__.__name__ in ["TransfoXLLMHeadModel"]: kwargs.update({"""add_space_before_punct_symbol""": True} ) return super()._parse_and_tokenize(*a_ ,**a_ ) def __call__( self ,a_ ,**a_ ) -> str: return super().__call__(a_ ,**a_ ) def _snake_case ( self ,a_ ,a_="" ,a_=None ,**a_ ) -> Optional[Any]: _UpperCAmelCase : str = self.tokenizer( prefix + prompt_text ,padding=a_ ,add_special_tokens=a_ ,return_tensors=self.framework ) _UpperCAmelCase : Optional[Any] = prompt_text if handle_long_generation == "hole": _UpperCAmelCase : Dict = inputs["""input_ids"""].shape[-1] if "max_new_tokens" in generate_kwargs: _UpperCAmelCase : str = generate_kwargs["""max_new_tokens"""] else: _UpperCAmelCase : Optional[int] = generate_kwargs.get("""max_length""" ,self.model.config.max_length ) - cur_len if new_tokens < 0: raise ValueError("""We cannot infer how many new tokens are expected""" ) if cur_len + new_tokens > self.tokenizer.model_max_length: _UpperCAmelCase : str = self.tokenizer.model_max_length - new_tokens if keep_length <= 0: raise ValueError( """We cannot use `hole` to handle this generation the number of desired tokens exceeds the""" """ models max length""" ) _UpperCAmelCase : Optional[Any] = inputs["""input_ids"""][:, -keep_length:] if "attention_mask" in inputs: _UpperCAmelCase : Optional[int] = inputs["""attention_mask"""][:, -keep_length:] return inputs def _snake_case ( self ,a_ ,**a_ ) -> Union[str, Any]: _UpperCAmelCase : Optional[Any] = model_inputs["""input_ids"""] _UpperCAmelCase : List[str] = model_inputs.get("""attention_mask""" ,a_ ) # Allow empty prompts if input_ids.shape[1] == 0: _UpperCAmelCase : Optional[int] = None _UpperCAmelCase : List[Any] = None _UpperCAmelCase : int = 1 else: _UpperCAmelCase : List[str] = input_ids.shape[0] _UpperCAmelCase : Any = model_inputs.pop("""prompt_text""" ) # If there is a prefix, we may need to adjust the generation length. Do so without permanently modifying # generate_kwargs, as some of the parameterization may come from the initialization of the pipeline. _UpperCAmelCase : List[Any] = generate_kwargs.pop("""prefix_length""" ,0 ) if prefix_length > 0: _UpperCAmelCase : Optional[int] = """max_new_tokens""" in generate_kwargs or ( """generation_config""" in generate_kwargs and generate_kwargs["""generation_config"""].max_new_tokens is not None ) if not has_max_new_tokens: _UpperCAmelCase : Optional[int] = generate_kwargs.get("""max_length""" ) or self.model.config.max_length generate_kwargs["max_length"] += prefix_length _UpperCAmelCase : str = """min_new_tokens""" in generate_kwargs or ( """generation_config""" in generate_kwargs and generate_kwargs["""generation_config"""].min_new_tokens is not None ) if not has_min_new_tokens and "min_length" in generate_kwargs: generate_kwargs["min_length"] += prefix_length # BS x SL _UpperCAmelCase : Optional[int] = self.model.generate(input_ids=a_ ,attention_mask=a_ ,**a_ ) _UpperCAmelCase : Dict = generated_sequence.shape[0] if self.framework == "pt": _UpperCAmelCase : Optional[int] = generated_sequence.reshape(a_ ,out_b // in_b ,*generated_sequence.shape[1:] ) elif self.framework == "tf": _UpperCAmelCase : Union[str, Any] = tf.reshape(a_ ,(in_b, out_b // in_b, *generated_sequence.shape[1:]) ) return {"generated_sequence": generated_sequence, "input_ids": input_ids, "prompt_text": prompt_text} def _snake_case ( self ,a_ ,a_=ReturnType.FULL_TEXT ,a_=True ) -> List[str]: _UpperCAmelCase : Optional[Any] = model_outputs["""generated_sequence"""][0] _UpperCAmelCase : Optional[int] = model_outputs["""input_ids"""] _UpperCAmelCase : List[str] = model_outputs["""prompt_text"""] _UpperCAmelCase : Optional[Any] = generated_sequence.numpy().tolist() _UpperCAmelCase : Dict = [] for sequence in generated_sequence: if return_type == ReturnType.TENSORS: _UpperCAmelCase : str = {"""generated_token_ids""": sequence} elif return_type in {ReturnType.NEW_TEXT, ReturnType.FULL_TEXT}: # Decode text _UpperCAmelCase : Tuple = self.tokenizer.decode( a_ ,skip_special_tokens=a_ ,clean_up_tokenization_spaces=a_ ,) # Remove PADDING prompt of the sequence if XLNet or Transfo-XL model is used if input_ids is None: _UpperCAmelCase : Union[str, Any] = 0 else: _UpperCAmelCase : Tuple = len( self.tokenizer.decode( input_ids[0] ,skip_special_tokens=a_ ,clean_up_tokenization_spaces=a_ ,) ) if return_type == ReturnType.FULL_TEXT: _UpperCAmelCase : Any = prompt_text + text[prompt_length:] else: _UpperCAmelCase : Dict = text[prompt_length:] _UpperCAmelCase : Union[str, Any] = {"""generated_text""": all_text} records.append(a_ ) return records
215
0
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_tokenizers_available, is_torch_available, is_vision_available, ) UpperCAmelCase = { "configuration_perceiver": ["PERCEIVER_PRETRAINED_CONFIG_ARCHIVE_MAP", "PerceiverConfig", "PerceiverOnnxConfig"], "tokenization_perceiver": ["PerceiverTokenizer"], } try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCAmelCase = ["PerceiverFeatureExtractor"] UpperCAmelCase = ["PerceiverImageProcessor"] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCAmelCase = [ "PERCEIVER_PRETRAINED_MODEL_ARCHIVE_LIST", "PerceiverForImageClassificationConvProcessing", "PerceiverForImageClassificationFourier", "PerceiverForImageClassificationLearned", "PerceiverForMaskedLM", "PerceiverForMultimodalAutoencoding", "PerceiverForOpticalFlow", "PerceiverForSequenceClassification", "PerceiverLayer", "PerceiverModel", "PerceiverPreTrainedModel", ] if TYPE_CHECKING: from .configuration_perceiver import PERCEIVER_PRETRAINED_CONFIG_ARCHIVE_MAP, PerceiverConfig, PerceiverOnnxConfig from .tokenization_perceiver import PerceiverTokenizer try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .feature_extraction_perceiver import PerceiverFeatureExtractor from .image_processing_perceiver import PerceiverImageProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_perceiver import ( PERCEIVER_PRETRAINED_MODEL_ARCHIVE_LIST, PerceiverForImageClassificationConvProcessing, PerceiverForImageClassificationFourier, PerceiverForImageClassificationLearned, PerceiverForMaskedLM, PerceiverForMultimodalAutoencoding, PerceiverForOpticalFlow, PerceiverForSequenceClassification, PerceiverLayer, PerceiverModel, PerceiverPreTrainedModel, ) else: import sys UpperCAmelCase = _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)
360
import numpy as np import datasets UpperCAmelCase = """ Compute the Mahalanobis Distance Mahalonobis distance is the distance between a point and a distribution. And not between two distinct points. It is effectively a multivariate equivalent of the Euclidean distance. It was introduced by Prof. P. C. Mahalanobis in 1936 and has been used in various statistical applications ever since [source: https://www.machinelearningplus.com/statistics/mahalanobis-distance/] """ UpperCAmelCase = """\ @article{de2000mahalanobis, title={The mahalanobis distance}, author={De Maesschalck, Roy and Jouan-Rimbaud, Delphine and Massart, D{\'e}sir{\'e} L}, journal={Chemometrics and intelligent laboratory systems}, volume={50}, number={1}, pages={1--18}, year={2000}, publisher={Elsevier} } """ UpperCAmelCase = """ Args: X: List of datapoints to be compared with the `reference_distribution`. reference_distribution: List of datapoints from the reference distribution we want to compare to. Returns: mahalanobis: The Mahalonobis distance for each datapoint in `X`. Examples: >>> mahalanobis_metric = datasets.load_metric(\"mahalanobis\") >>> results = mahalanobis_metric.compute(reference_distribution=[[0, 1], [1, 0]], X=[[0, 1]]) >>> print(results) {'mahalanobis': array([0.5])} """ @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class lowerCAmelCase_ ( datasets.Metric ): '''simple docstring''' def UpperCamelCase__ ( self ): return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { '''X''': datasets.Sequence(datasets.Value('''float''' , id='''sequence''' ) , id='''X''' ), } ) , ) def UpperCamelCase__ ( self , _UpperCAmelCase , _UpperCAmelCase ): # convert to numpy arrays snake_case_ = np.array(_UpperCAmelCase ) snake_case_ = np.array(_UpperCAmelCase ) # Assert that arrays are 2D if len(X.shape ) != 2: raise ValueError('''Expected `X` to be a 2D vector''' ) if len(reference_distribution.shape ) != 2: raise ValueError('''Expected `reference_distribution` to be a 2D vector''' ) if reference_distribution.shape[0] < 2: raise ValueError( '''Expected `reference_distribution` to be a 2D vector with more than one element in the first dimension''' ) # Get mahalanobis distance for each prediction snake_case_ = X - np.mean(_UpperCAmelCase ) snake_case_ = np.cov(reference_distribution.T ) try: snake_case_ = np.linalg.inv(_UpperCAmelCase ) except np.linalg.LinAlgError: snake_case_ = np.linalg.pinv(_UpperCAmelCase ) snake_case_ = np.dot(_UpperCAmelCase , _UpperCAmelCase ) snake_case_ = np.dot(_UpperCAmelCase , X_minus_mu.T ).diagonal() return {"mahalanobis": mahal_dist}
267
0
import warnings from contextlib import contextmanager from ....processing_utils import ProcessorMixin class a ( __lowerCamelCase ): __lowerCAmelCase : List[Any] = """MCTCTFeatureExtractor""" __lowerCAmelCase : str = """AutoTokenizer""" def __init__( self :Optional[Any] ,__lowercase :Optional[Any] ,__lowercase :str ): super().__init__(__lowercase ,__lowercase ) snake_case__ : Dict = self.feature_extractor snake_case__ : Union[str, Any] = False def __call__( self :Tuple ,*__lowercase :str ,**__lowercase :Tuple ): # For backward compatibility if self._in_target_context_manager: return self.current_processor(*__lowercase ,**__lowercase ) if "raw_speech" in kwargs: warnings.warn('''Using `raw_speech` as a keyword argument is deprecated. Use `audio` instead.''' ) snake_case__ : Dict = kwargs.pop('''raw_speech''' ) else: snake_case__ : Optional[int] = kwargs.pop('''audio''' ,__lowercase ) snake_case__ : Optional[int] = kwargs.pop('''sampling_rate''' ,__lowercase ) snake_case__ : Optional[Any] = kwargs.pop('''text''' ,__lowercase ) if len(__lowercase ) > 0: snake_case__ : Tuple = args[0] snake_case__ : Tuple = args[1:] if audio is None and text is None: raise ValueError('''You need to specify either an `audio` or `text` input to process.''' ) if audio is not None: snake_case__ : Union[str, Any] = self.feature_extractor(__lowercase ,*__lowercase ,sampling_rate=__lowercase ,**__lowercase ) if text is not None: snake_case__ : List[str] = self.tokenizer(__lowercase ,**__lowercase ) if text is None: return inputs elif audio is None: return encodings else: snake_case__ : List[str] = encodings['''input_ids'''] return inputs def __lowerCamelCase ( self :Any ,*__lowercase :Tuple ,**__lowercase :Any ): return self.tokenizer.batch_decode(*__lowercase ,**__lowercase ) def __lowerCamelCase ( self :int ,*__lowercase :Optional[Any] ,**__lowercase :List[Any] ): # For backward compatibility if self._in_target_context_manager: return self.current_processor.pad(*__lowercase ,**__lowercase ) snake_case__ : Tuple = kwargs.pop('''input_features''' ,__lowercase ) snake_case__ : Union[str, Any] = kwargs.pop('''labels''' ,__lowercase ) if len(__lowercase ) > 0: snake_case__ : Dict = args[0] snake_case__ : int = args[1:] if input_features is not None: snake_case__ : Any = self.feature_extractor.pad(__lowercase ,*__lowercase ,**__lowercase ) if labels is not None: snake_case__ : List[Any] = self.tokenizer.pad(__lowercase ,**__lowercase ) if labels is None: return input_features elif input_features is None: return labels else: snake_case__ : Union[str, Any] = labels['''input_ids'''] return input_features def __lowerCamelCase ( self :str ,*__lowercase :Union[str, Any] ,**__lowercase :int ): return self.tokenizer.decode(*__lowercase ,**__lowercase ) @contextmanager def __lowerCamelCase ( self :Tuple ): warnings.warn( '''`as_target_processor` is deprecated and will be removed in v5 of Transformers. You can process your ''' '''labels by using the argument `text` of the regular `__call__` method (either in the same call as ''' '''your audio inputs, or in a separate call.''' ) snake_case__ : str = True snake_case__ : str = self.tokenizer yield snake_case__ : Optional[int] = self.feature_extractor snake_case__ : List[Any] = False
230
import json import os from pathlib import Path import pytest from datasets.download.download_config import DownloadConfig from datasets.download.download_manager import DownloadManager from datasets.utils.file_utils import hash_url_to_filename A__ = '''http://www.mocksite.com/file1.txt''' A__ = '''"text": ["foo", "foo"]''' A__ = '''6d8ce9aa78a471c7477201efbeabd3bb01ac2e7d100a6dc024ba1608361f90a8''' class a : __lowerCAmelCase : Optional[int] = 2_00 __lowerCAmelCase : List[str] = {"""Content-Length""": """100"""} __lowerCAmelCase : Dict = {} def __lowerCamelCase ( self :Dict ,**__lowercase :List[Any] ): return [bytes(__lowercase ,'''utf-8''' )] def _lowerCAmelCase ( *__lowerCAmelCase , **__lowerCAmelCase ) -> Optional[Any]: """simple docstring""" return MockResponse() @pytest.mark.parametrize('''urls_type''' , [str, list, dict] ) def _lowerCAmelCase ( __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase ) -> List[str]: """simple docstring""" import requests monkeypatch.setattr(__lowerCAmelCase , '''request''' , __lowerCAmelCase ) snake_case__ : Union[str, Any] = URL if issubclass(__lowerCAmelCase , __lowerCAmelCase ): snake_case__ : Optional[Any] = url elif issubclass(__lowerCAmelCase , __lowerCAmelCase ): snake_case__ : int = [url] elif issubclass(__lowerCAmelCase , __lowerCAmelCase ): snake_case__ : int = {'''train''': url} snake_case__ : Dict = '''dummy''' snake_case__ : Any = '''downloads''' snake_case__ : int = tmp_path snake_case__ : Any = DownloadConfig( cache_dir=os.path.join(__lowerCAmelCase , __lowerCAmelCase ) , use_etag=__lowerCAmelCase , ) snake_case__ : Tuple = DownloadManager(dataset_name=__lowerCAmelCase , download_config=__lowerCAmelCase ) snake_case__ : List[Any] = dl_manager.download(__lowerCAmelCase ) snake_case__ : Union[str, Any] = urls for downloaded_paths in [downloaded_paths]: if isinstance(__lowerCAmelCase , __lowerCAmelCase ): snake_case__ : Optional[int] = [downloaded_paths] snake_case__ : Dict = [urls] elif isinstance(__lowerCAmelCase , __lowerCAmelCase ): assert "train" in downloaded_paths.keys() snake_case__ : str = downloaded_paths.values() snake_case__ : List[str] = urls.values() assert downloaded_paths for downloaded_path, input_url in zip(__lowerCAmelCase , __lowerCAmelCase ): assert downloaded_path == dl_manager.downloaded_paths[input_url] snake_case__ : List[Any] = Path(__lowerCAmelCase ) snake_case__ : Any = downloaded_path.parts assert parts[-1] == HASH assert parts[-2] == cache_subdir assert downloaded_path.exists() snake_case__ : List[str] = downloaded_path.read_text() assert content == CONTENT snake_case__ : List[str] = downloaded_path.with_suffix('''.json''' ) assert metadata_downloaded_path.exists() snake_case__ : str = json.loads(metadata_downloaded_path.read_text() ) assert metadata_content == {"url": URL, "etag": None} @pytest.mark.parametrize('''paths_type''' , [str, list, dict] ) def _lowerCAmelCase ( __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase ) -> Any: """simple docstring""" snake_case__ : Any = str(__lowerCAmelCase ) if issubclass(__lowerCAmelCase , __lowerCAmelCase ): snake_case__ : Tuple = filename elif issubclass(__lowerCAmelCase , __lowerCAmelCase ): snake_case__ : Dict = [filename] elif issubclass(__lowerCAmelCase , __lowerCAmelCase ): snake_case__ : Dict = {'''train''': filename} snake_case__ : Any = '''dummy''' snake_case__ : Any = xz_file.parent snake_case__ : List[str] = '''extracted''' snake_case__ : Dict = DownloadConfig( cache_dir=__lowerCAmelCase , use_etag=__lowerCAmelCase , ) snake_case__ : Dict = DownloadManager(dataset_name=__lowerCAmelCase , download_config=__lowerCAmelCase ) snake_case__ : str = dl_manager.extract(__lowerCAmelCase ) snake_case__ : int = paths for extracted_paths in [extracted_paths]: if isinstance(__lowerCAmelCase , __lowerCAmelCase ): snake_case__ : Dict = [extracted_paths] snake_case__ : Optional[Any] = [paths] elif isinstance(__lowerCAmelCase , __lowerCAmelCase ): assert "train" in extracted_paths.keys() snake_case__ : int = extracted_paths.values() snake_case__ : int = paths.values() assert extracted_paths for extracted_path, input_path in zip(__lowerCAmelCase , __lowerCAmelCase ): assert extracted_path == dl_manager.extracted_paths[input_path] snake_case__ : Optional[int] = Path(__lowerCAmelCase ) snake_case__ : int = extracted_path.parts assert parts[-1] == hash_url_to_filename(__lowerCAmelCase , etag=__lowerCAmelCase ) assert parts[-2] == extracted_subdir assert extracted_path.exists() snake_case__ : List[Any] = extracted_path.read_text() snake_case__ : List[str] = text_file.read_text() assert extracted_file_content == expected_file_content def _lowerCAmelCase ( __lowerCAmelCase , __lowerCAmelCase ) -> List[Any]: """simple docstring""" assert path.endswith('''.jsonl''' ) for num_items, line in enumerate(__lowerCAmelCase , start=1 ): snake_case__ : Any = json.loads(line.decode('''utf-8''' ) ) assert item.keys() == {"col_1", "col_2", "col_3"} assert num_items == 4 @pytest.mark.parametrize('''archive_jsonl''' , ['''tar_jsonl_path''', '''zip_jsonl_path'''] ) def _lowerCAmelCase ( __lowerCAmelCase , __lowerCAmelCase ) -> Union[str, Any]: """simple docstring""" snake_case__ : Any = request.getfixturevalue(__lowerCAmelCase ) snake_case__ : Union[str, Any] = DownloadManager() for num_jsonl, (path, file) in enumerate(dl_manager.iter_archive(__lowerCAmelCase ) , start=1 ): _test_jsonl(__lowerCAmelCase , __lowerCAmelCase ) assert num_jsonl == 2 @pytest.mark.parametrize('''archive_nested_jsonl''' , ['''tar_nested_jsonl_path''', '''zip_nested_jsonl_path'''] ) def _lowerCAmelCase ( __lowerCAmelCase , __lowerCAmelCase ) -> List[str]: """simple docstring""" snake_case__ : Union[str, Any] = request.getfixturevalue(__lowerCAmelCase ) snake_case__ : Optional[int] = DownloadManager() for num_tar, (path, file) in enumerate(dl_manager.iter_archive(__lowerCAmelCase ) , start=1 ): for num_jsonl, (subpath, subfile) in enumerate(dl_manager.iter_archive(__lowerCAmelCase ) , start=1 ): _test_jsonl(__lowerCAmelCase , __lowerCAmelCase ) assert num_tar == 1 assert num_jsonl == 2 def _lowerCAmelCase ( __lowerCAmelCase ) -> str: """simple docstring""" snake_case__ : Any = DownloadManager() for num_file, file in enumerate(dl_manager.iter_files(__lowerCAmelCase ) , start=1 ): assert os.path.basename(__lowerCAmelCase ) == ("test.txt" if num_file == 1 else "train.txt") assert num_file == 2
230
1
from .testing import ( are_the_same_tensors, execute_subprocess_async, require_bnb, require_cpu, require_cuda, require_huggingface_suite, require_mps, require_multi_gpu, require_multi_xpu, require_safetensors, require_single_gpu, require_single_xpu, require_torch_min_version, require_tpu, require_xpu, skip, slow, ) from .training import RegressionDataset, RegressionModel, RegressionModelaXPU from .scripts import test_script, test_sync, test_ops # isort: skip
355
import math def SCREAMING_SNAKE_CASE__ ( _UpperCAmelCase = 100 ) -> int: '''simple docstring''' lowerCAmelCase : Any = sum(i * i for i in range(1, n + 1 ) ) lowerCAmelCase : str = int(math.pow(sum(range(1, n + 1 ) ), 2 ) ) return square_of_sum - sum_of_squares if __name__ == "__main__": print(F'{solution() = }')
323
0
import itertools import json import linecache import os import pickle import re import socket import string from collections import Counter from logging import getLogger from pathlib import Path from typing import Callable, Dict, Iterable, List import git import torch from torch.utils.data import Dataset from transformers import BartTokenizer, RagTokenizer, TaTokenizer def __snake_case ( _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase=True , _UpperCAmelCase="pt" ): __a = {'''add_prefix_space''': True} if isinstance(_UpperCAmelCase , _UpperCAmelCase ) and not line.startswith(''' ''' ) else {} __a = padding_side return tokenizer( [line] , max_length=_UpperCAmelCase , padding='''max_length''' if pad_to_max_length else None , truncation=_UpperCAmelCase , return_tensors=_UpperCAmelCase , add_special_tokens=_UpperCAmelCase , **_UpperCAmelCase , ) def __snake_case ( _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase=None , ): __a = input_ids.ne(_UpperCAmelCase ).any(dim=0 ) if attention_mask is None: return input_ids[:, keep_column_mask] else: return (input_ids[:, keep_column_mask], attention_mask[:, keep_column_mask]) class _A ( __UpperCAmelCase ): def __init__( self : Optional[int] , __SCREAMING_SNAKE_CASE : Optional[Any] , __SCREAMING_SNAKE_CASE : Optional[Any] , __SCREAMING_SNAKE_CASE : List[str] , __SCREAMING_SNAKE_CASE : Tuple , __SCREAMING_SNAKE_CASE : Any="train" , __SCREAMING_SNAKE_CASE : Any=None , __SCREAMING_SNAKE_CASE : int=None , __SCREAMING_SNAKE_CASE : Optional[Any]=None , __SCREAMING_SNAKE_CASE : Any="" , ): '''simple docstring''' super().__init__() __a = Path(__SCREAMING_SNAKE_CASE).joinpath(type_path + '''.source''') __a = Path(__SCREAMING_SNAKE_CASE).joinpath(type_path + '''.target''') __a = self.get_char_lens(self.src_file) __a = max_source_length __a = max_target_length assert min(self.src_lens) > 0, F'found empty line in {self.src_file}' __a = tokenizer __a = prefix if n_obs is not None: __a = self.src_lens[:n_obs] __a = src_lang __a = tgt_lang def __len__( self : Any): '''simple docstring''' return len(self.src_lens) def __getitem__( self : Tuple , __SCREAMING_SNAKE_CASE : Dict): '''simple docstring''' __a = index + 1 # linecache starts at 1 __a = self.prefix + linecache.getline(str(self.src_file) , __SCREAMING_SNAKE_CASE).rstrip('''\n''') __a = linecache.getline(str(self.tgt_file) , __SCREAMING_SNAKE_CASE).rstrip('''\n''') assert source_line, F'empty source line for index {index}' assert tgt_line, F'empty tgt line for index {index}' # Need to add eos token manually for T5 if isinstance(self.tokenizer , __SCREAMING_SNAKE_CASE): source_line += self.tokenizer.eos_token tgt_line += self.tokenizer.eos_token # Pad source and target to the right __a = ( self.tokenizer.question_encoder if isinstance(self.tokenizer , __SCREAMING_SNAKE_CASE) else self.tokenizer ) __a = self.tokenizer.generator if isinstance(self.tokenizer , __SCREAMING_SNAKE_CASE) else self.tokenizer __a = encode_line(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , self.max_source_length , '''right''') __a = encode_line(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , self.max_target_length , '''right''') __a = source_inputs['''input_ids'''].squeeze() __a = target_inputs['''input_ids'''].squeeze() __a = source_inputs['''attention_mask'''].squeeze() return { "input_ids": source_ids, "attention_mask": src_mask, "decoder_input_ids": target_ids, } @staticmethod def _lowerCamelCase ( __SCREAMING_SNAKE_CASE : Tuple): '''simple docstring''' return [len(__SCREAMING_SNAKE_CASE) for x in Path(__SCREAMING_SNAKE_CASE).open().readlines()] def _lowerCamelCase ( self : Optional[Any] , __SCREAMING_SNAKE_CASE : List[Any]): '''simple docstring''' __a = torch.stack([x['''input_ids'''] for x in batch]) __a = torch.stack([x['''attention_mask'''] for x in batch]) __a = torch.stack([x['''decoder_input_ids'''] for x in batch]) __a = ( self.tokenizer.generator.pad_token_id if isinstance(self.tokenizer , __SCREAMING_SNAKE_CASE) else self.tokenizer.pad_token_id ) __a = ( self.tokenizer.question_encoder.pad_token_id if isinstance(self.tokenizer , __SCREAMING_SNAKE_CASE) else self.tokenizer.pad_token_id ) __a = trim_batch(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE) __a , __a = trim_batch(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , attention_mask=__SCREAMING_SNAKE_CASE) __a = { '''input_ids''': source_ids, '''attention_mask''': source_mask, '''decoder_input_ids''': y, } return batch __snake_case :Optional[int] = getLogger(__name__) def __snake_case ( _UpperCAmelCase ): return list(itertools.chain.from_iterable(_UpperCAmelCase ) ) def __snake_case ( _UpperCAmelCase ): __a = get_git_info() save_json(_UpperCAmelCase , os.path.join(_UpperCAmelCase , '''git_log.json''' ) ) def __snake_case ( _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase=4 , **_UpperCAmelCase ): with open(_UpperCAmelCase , '''w''' ) as f: json.dump(_UpperCAmelCase , _UpperCAmelCase , indent=_UpperCAmelCase , **_UpperCAmelCase ) def __snake_case ( _UpperCAmelCase ): with open(_UpperCAmelCase ) as f: return json.load(_UpperCAmelCase ) def __snake_case ( ): __a = git.Repo(search_parent_directories=_UpperCAmelCase ) __a = { '''repo_id''': str(_UpperCAmelCase ), '''repo_sha''': str(repo.head.object.hexsha ), '''repo_branch''': str(repo.active_branch ), '''hostname''': str(socket.gethostname() ), } return repo_infos def __snake_case ( _UpperCAmelCase , _UpperCAmelCase ): return list(map(_UpperCAmelCase , _UpperCAmelCase ) ) def __snake_case ( _UpperCAmelCase , _UpperCAmelCase ): with open(_UpperCAmelCase , '''wb''' ) as f: return pickle.dump(_UpperCAmelCase , _UpperCAmelCase ) def __snake_case ( _UpperCAmelCase ): def remove_articles(_UpperCAmelCase ): return re.sub(R'''\b(a|an|the)\b''' , ''' ''' , _UpperCAmelCase ) def white_space_fix(_UpperCAmelCase ): return " ".join(text.split() ) def remove_punc(_UpperCAmelCase ): __a = set(string.punctuation ) return "".join(ch for ch in text if ch not in exclude ) def lower(_UpperCAmelCase ): return text.lower() return white_space_fix(remove_articles(remove_punc(lower(_UpperCAmelCase ) ) ) ) def __snake_case ( _UpperCAmelCase , _UpperCAmelCase ): __a = normalize_answer(_UpperCAmelCase ).split() __a = normalize_answer(_UpperCAmelCase ).split() __a = Counter(_UpperCAmelCase ) & Counter(_UpperCAmelCase ) __a = sum(common.values() ) if num_same == 0: return 0 __a = 1.0 * num_same / len(_UpperCAmelCase ) __a = 1.0 * num_same / len(_UpperCAmelCase ) __a = (2 * precision * recall) / (precision + recall) return fa def __snake_case ( _UpperCAmelCase , _UpperCAmelCase ): return normalize_answer(_UpperCAmelCase ) == normalize_answer(_UpperCAmelCase ) def __snake_case ( _UpperCAmelCase , _UpperCAmelCase ): assert len(_UpperCAmelCase ) == len(_UpperCAmelCase ) __a = 0 for hypo, pred in zip(_UpperCAmelCase , _UpperCAmelCase ): em += exact_match_score(_UpperCAmelCase , _UpperCAmelCase ) if len(_UpperCAmelCase ) > 0: em /= len(_UpperCAmelCase ) return {"em": em} def __snake_case ( _UpperCAmelCase ): return model_prefix.startswith('''rag''' ) def __snake_case ( _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase ): __a = {p: p for p in extra_params} # T5 models don't have `dropout` param, they have `dropout_rate` instead __a = '''dropout_rate''' for p in extra_params: if getattr(_UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase ): if not hasattr(_UpperCAmelCase , _UpperCAmelCase ) and not hasattr(_UpperCAmelCase , equivalent_param[p] ): logger.info('''config doesn\'t have a `{}` attribute'''.format(_UpperCAmelCase ) ) delattr(_UpperCAmelCase , _UpperCAmelCase ) continue __a = p if hasattr(_UpperCAmelCase , _UpperCAmelCase ) else equivalent_param[p] setattr(_UpperCAmelCase , _UpperCAmelCase , getattr(_UpperCAmelCase , _UpperCAmelCase ) ) delattr(_UpperCAmelCase , _UpperCAmelCase ) return hparams, config
49
import inspect import math import tempfile import unittest import numpy as np from transformers import ViTMAEConfig from transformers.testing_utils import require_torch, require_vision, slow, torch_device from transformers.utils import cached_property, is_torch_available, is_vision_available from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from torch import nn from transformers import ViTMAEForPreTraining, ViTMAEModel from transformers.models.vit.modeling_vit import VIT_PRETRAINED_MODEL_ARCHIVE_LIST if is_vision_available(): from PIL import Image from transformers import ViTImageProcessor class __magic_name__ : def __init__( self : Tuple , lowerCamelCase__ : Optional[Any] , lowerCamelCase__ : int=13 , lowerCamelCase__ : Union[str, Any]=30 , lowerCamelCase__ : Union[str, Any]=2 , lowerCamelCase__ : List[Any]=3 , lowerCamelCase__ : str=True , lowerCamelCase__ : str=True , lowerCamelCase__ : Dict=32 , lowerCamelCase__ : str=5 , lowerCamelCase__ : Dict=4 , lowerCamelCase__ : Any=37 , lowerCamelCase__ : Optional[Any]="gelu" , lowerCamelCase__ : List[Any]=0.1 , lowerCamelCase__ : Dict=0.1 , lowerCamelCase__ : Tuple=10 , lowerCamelCase__ : List[Any]=0.02 , lowerCamelCase__ : List[Any]=3 , lowerCamelCase__ : str=0.6 , lowerCamelCase__ : int=None , ) -> Dict: '''simple docstring''' UpperCamelCase__ : Any = parent UpperCamelCase__ : List[str] = batch_size UpperCamelCase__ : List[Any] = image_size UpperCamelCase__ : str = patch_size UpperCamelCase__ : List[str] = num_channels UpperCamelCase__ : int = is_training UpperCamelCase__ : Dict = use_labels UpperCamelCase__ : int = hidden_size UpperCamelCase__ : Union[str, Any] = num_hidden_layers UpperCamelCase__ : Tuple = num_attention_heads UpperCamelCase__ : Union[str, Any] = intermediate_size UpperCamelCase__ : Dict = hidden_act UpperCamelCase__ : str = hidden_dropout_prob UpperCamelCase__ : Tuple = attention_probs_dropout_prob UpperCamelCase__ : Union[str, Any] = type_sequence_label_size UpperCamelCase__ : str = initializer_range UpperCamelCase__ : str = mask_ratio UpperCamelCase__ : Tuple = scope # in ViTMAE, the expected sequence length = (num_patches + 1) * (1 - config.mask_ratio), rounded above # (we add 1 for the [CLS] token) UpperCamelCase__ : Optional[int] = (image_size // patch_size) ** 2 UpperCamelCase__ : Dict = int(math.ceil((1 - mask_ratio) * (num_patches + 1) ) ) def UpperCAmelCase__ ( self : Union[str, Any] ) -> Any: '''simple docstring''' UpperCamelCase__ : Union[str, Any] = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) UpperCamelCase__ : List[str] = None if self.use_labels: UpperCamelCase__ : Dict = ids_tensor([self.batch_size] , self.type_sequence_label_size ) UpperCamelCase__ : Any = self.get_config() return config, pixel_values, labels def UpperCAmelCase__ ( self : Dict ) -> List[str]: '''simple docstring''' return ViTMAEConfig( image_size=self.image_size , patch_size=self.patch_size , num_channels=self.num_channels , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , is_decoder=lowerCamelCase__ , initializer_range=self.initializer_range , mask_ratio=self.mask_ratio , ) def UpperCAmelCase__ ( self : Tuple , lowerCamelCase__ : Dict , lowerCamelCase__ : Union[str, Any] , lowerCamelCase__ : List[str] ) -> Union[str, Any]: '''simple docstring''' UpperCamelCase__ : List[Any] = ViTMAEModel(config=lowerCamelCase__ ) model.to(lowerCamelCase__ ) model.eval() UpperCamelCase__ : List[Any] = model(lowerCamelCase__ ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def UpperCAmelCase__ ( self : int , lowerCamelCase__ : str , lowerCamelCase__ : Optional[int] , lowerCamelCase__ : List[Any] ) -> List[Any]: '''simple docstring''' UpperCamelCase__ : Tuple = ViTMAEForPreTraining(lowerCamelCase__ ) model.to(lowerCamelCase__ ) model.eval() UpperCamelCase__ : List[str] = model(lowerCamelCase__ ) UpperCamelCase__ : int = (self.image_size // self.patch_size) ** 2 UpperCamelCase__ : Optional[int] = self.patch_size**2 * self.num_channels self.parent.assertEqual(result.logits.shape , (self.batch_size, num_patches, expected_num_channels) ) # test greyscale images UpperCamelCase__ : List[Any] = 1 UpperCamelCase__ : int = ViTMAEForPreTraining(lowerCamelCase__ ) model.to(lowerCamelCase__ ) model.eval() UpperCamelCase__ : Tuple = floats_tensor([self.batch_size, 1, self.image_size, self.image_size] ) UpperCamelCase__ : Any = model(lowerCamelCase__ ) UpperCamelCase__ : Optional[Any] = self.patch_size**2 self.parent.assertEqual(result.logits.shape , (self.batch_size, num_patches, expected_num_channels) ) def UpperCAmelCase__ ( self : Optional[Any] ) -> str: '''simple docstring''' UpperCamelCase__ : Any = self.prepare_config_and_inputs() UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ : Union[str, Any] = config_and_inputs UpperCamelCase__ : Optional[int] = {'''pixel_values''': pixel_values} return config, inputs_dict @require_torch class __magic_name__ ( __lowerCAmelCase , __lowerCAmelCase , unittest.TestCase): A: Optional[Any] = (ViTMAEModel, ViTMAEForPreTraining) if is_torch_available() else () A: Union[str, Any] = {"feature-extraction": ViTMAEModel} if is_torch_available() else {} A: Any = False A: str = False A: Optional[int] = False A: Any = False def UpperCAmelCase__ ( self : Optional[Any] ) -> int: '''simple docstring''' UpperCamelCase__ : Optional[int] = ViTMAEModelTester(self ) UpperCamelCase__ : Union[str, Any] = ConfigTester(self , config_class=lowerCamelCase__ , has_text_modality=lowerCamelCase__ , hidden_size=37 ) def UpperCAmelCase__ ( self : str ) -> Optional[int]: '''simple docstring''' self.config_tester.run_common_tests() @unittest.skip(reason='''ViTMAE does not use inputs_embeds''' ) def UpperCAmelCase__ ( self : Tuple ) -> str: '''simple docstring''' pass def UpperCAmelCase__ ( self : List[str] ) -> int: '''simple docstring''' UpperCamelCase__ , UpperCamelCase__ : Dict = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: UpperCamelCase__ : List[Any] = model_class(lowerCamelCase__ ) self.assertIsInstance(model.get_input_embeddings() , (nn.Module) ) UpperCamelCase__ : Optional[int] = model.get_output_embeddings() self.assertTrue(x is None or isinstance(lowerCamelCase__ , nn.Linear ) ) def UpperCAmelCase__ ( self : Optional[int] ) -> int: '''simple docstring''' UpperCamelCase__ , UpperCamelCase__ : Dict = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: UpperCamelCase__ : Optional[Any] = model_class(lowerCamelCase__ ) UpperCamelCase__ : List[Any] = inspect.signature(model.forward ) # signature.parameters is an OrderedDict => so arg_names order is deterministic UpperCamelCase__ : Optional[int] = [*signature.parameters.keys()] UpperCamelCase__ : List[str] = ['''pixel_values'''] self.assertListEqual(arg_names[:1] , lowerCamelCase__ ) def UpperCAmelCase__ ( self : Dict ) -> str: '''simple docstring''' UpperCamelCase__ : str = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*lowerCamelCase__ ) def UpperCAmelCase__ ( self : int ) -> List[str]: '''simple docstring''' UpperCamelCase__ : Dict = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_pretraining(*lowerCamelCase__ ) def UpperCAmelCase__ ( self : int , lowerCamelCase__ : Union[str, Any] , lowerCamelCase__ : Any , lowerCamelCase__ : List[str] ) -> Tuple: '''simple docstring''' np.random.seed(2 ) UpperCamelCase__ : List[str] = int((pt_model.config.image_size // pt_model.config.patch_size) ** 2 ) UpperCamelCase__ : Any = np.random.uniform(size=(self.model_tester.batch_size, num_patches) ) UpperCamelCase__ : Optional[Any] = torch.from_numpy(lowerCamelCase__ ) # Add `noise` argument. # PT inputs will be prepared in `super().check_pt_tf_models()` with this added `noise` argument UpperCamelCase__ : Union[str, Any] = pt_noise super().check_pt_tf_models(lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ ) def UpperCAmelCase__ ( self : Tuple ) -> List[str]: '''simple docstring''' UpperCamelCase__ , UpperCamelCase__ : int = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: UpperCamelCase__ : Tuple = model_class(lowerCamelCase__ ) model.to(lowerCamelCase__ ) model.eval() # make random mask reproducible torch.manual_seed(2 ) with torch.no_grad(): UpperCamelCase__ : Tuple = model(**self._prepare_for_class(lowerCamelCase__ , lowerCamelCase__ ) ) UpperCamelCase__ : int = outputs[0].cpu().numpy() UpperCamelCase__ : Dict = 0 with tempfile.TemporaryDirectory() as tmpdirname: model.save_pretrained(lowerCamelCase__ ) UpperCamelCase__ : Any = model_class.from_pretrained(lowerCamelCase__ ) model.to(lowerCamelCase__ ) # make random mask reproducible torch.manual_seed(2 ) with torch.no_grad(): UpperCamelCase__ : Optional[Any] = model(**self._prepare_for_class(lowerCamelCase__ , lowerCamelCase__ ) ) # Make sure we don't have nans UpperCamelCase__ : Union[str, Any] = after_outputs[0].cpu().numpy() UpperCamelCase__ : Optional[Any] = 0 UpperCamelCase__ : List[Any] = np.amax(np.abs(out_a - out_a ) ) self.assertLessEqual(lowerCamelCase__ , 1E-5 ) @unittest.skip( reason='''ViTMAE returns a random mask + ids_restore in each forward pass. See test_save_load to get deterministic results.''' ) def UpperCAmelCase__ ( self : Union[str, Any] ) -> List[str]: '''simple docstring''' pass @unittest.skip( reason='''ViTMAE returns a random mask + ids_restore in each forward pass. See test_save_load to get deterministic results.''' ) def UpperCAmelCase__ ( self : Tuple ) -> Optional[Any]: '''simple docstring''' pass @unittest.skip( reason='''ViTMAE returns a random mask + ids_restore in each forward pass. See test_save_load to get deterministic results.''' ) def UpperCAmelCase__ ( self : List[Any] ) -> List[Any]: '''simple docstring''' pass @unittest.skip(reason='''ViTMAE returns a random mask + ids_restore in each forward pass. See test_save_load''' ) def UpperCAmelCase__ ( self : int ) -> Optional[int]: '''simple docstring''' pass @unittest.skip('''Will be fixed soon by reducing the size of the model used for common tests.''' ) def UpperCAmelCase__ ( self : List[Any] ) -> Union[str, Any]: '''simple docstring''' pass @slow def UpperCAmelCase__ ( self : Tuple ) -> Tuple: '''simple docstring''' for model_name in VIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: UpperCamelCase__ : Dict = ViTMAEModel.from_pretrained(lowerCamelCase__ ) self.assertIsNotNone(lowerCamelCase__ ) def _a ( ): """simple docstring""" UpperCamelCase__ : Optional[Any] = Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png''' ) return image @require_torch @require_vision class __magic_name__ ( unittest.TestCase): @cached_property def UpperCAmelCase__ ( self : Optional[Any] ) -> str: '''simple docstring''' return ViTImageProcessor.from_pretrained('''facebook/vit-mae-base''' ) if is_vision_available() else None @slow def UpperCAmelCase__ ( self : str ) -> Any: '''simple docstring''' np.random.seed(2 ) UpperCamelCase__ : Dict = ViTMAEForPreTraining.from_pretrained('''facebook/vit-mae-base''' ).to(lowerCamelCase__ ) UpperCamelCase__ : Optional[Any] = self.default_image_processor UpperCamelCase__ : int = prepare_img() UpperCamelCase__ : str = image_processor(images=lowerCamelCase__ , return_tensors='''pt''' ).to(lowerCamelCase__ ) # prepare a noise vector that will be also used for testing the TF model # (this way we can ensure that the PT and TF models operate on the same inputs) UpperCamelCase__ : Tuple = ViTMAEConfig() UpperCamelCase__ : List[Any] = int((vit_mae_config.image_size // vit_mae_config.patch_size) ** 2 ) UpperCamelCase__ : Dict = np.random.uniform(size=(1, num_patches) ) # forward pass with torch.no_grad(): UpperCamelCase__ : List[Any] = model(**lowerCamelCase__ , noise=torch.from_numpy(lowerCamelCase__ ).to(device=lowerCamelCase__ ) ) # verify the logits UpperCamelCase__ : Optional[Any] = torch.Size((1, 196, 768) ) self.assertEqual(outputs.logits.shape , lowerCamelCase__ ) UpperCamelCase__ : Dict = torch.tensor( [[-0.0548, -1.7023, -0.9325], [0.3721, -0.5670, -0.2233], [0.8235, -1.3878, -0.3524]] ) self.assertTrue(torch.allclose(outputs.logits[0, :3, :3] , expected_slice.to(lowerCamelCase__ ) , atol=1E-4 ) )
146
0
from diffusers.utils.testing_utils import require_onnxruntime @require_onnxruntime class lowerCamelCase : pass
350
from operator import delitem, getitem, setitem import pytest from data_structures.hashing.hash_map import HashMap def lowercase_ ( A__ ) -> str: """simple docstring""" return getitem, k def lowercase_ ( A__ , A__ ) -> str: """simple docstring""" return setitem, k, v def lowercase_ ( A__ ) -> List[Any]: """simple docstring""" return delitem, k def lowercase_ ( A__ , A__ , *A__ ) -> str: """simple docstring""" try: return fun(A__ , *A__ ), None except Exception as e: return None, e _A = ( _set("key_a", "val_a"), _set("key_b", "val_b"), ) _A = [ _set("key_a", "val_a"), _set("key_a", "val_b"), ] _A = [ _set("key_a", "val_a"), _set("key_b", "val_b"), _del("key_a"), _del("key_b"), _set("key_a", "val_a"), _del("key_a"), ] _A = [ _get("key_a"), _del("key_a"), _set("key_a", "val_a"), _del("key_a"), _del("key_a"), _get("key_a"), ] _A = [ *[_set(x, x) for x in range(5)], # guaranteed upsize ] _A = [ *[_set(x, x) for x in range(5)], # guaranteed upsize *[_del(x) for x in range(5)], _set("key_a", "val_b"), ] @pytest.mark.parametrize( "operations" , ( pytest.param(_add_items , id="add items" ), pytest.param(_overwrite_items , id="overwrite items" ), pytest.param(_delete_items , id="delete items" ), pytest.param(_access_absent_items , id="access absent items" ), pytest.param(_add_with_resize_up , id="add with resize up" ), pytest.param(_add_with_resize_down , id="add with resize down" ), ) , ) def lowercase_ ( A__ ) -> List[Any]: """simple docstring""" snake_case = HashMap(initial_block_size=4 ) snake_case = {} for _, (fun, *args) in enumerate(A__ ): snake_case , snake_case = _run_operation(A__ , A__ , *A__ ) snake_case , snake_case = _run_operation(A__ , A__ , *A__ ) assert my_res == py_res assert str(A__ ) == str(A__ ) assert set(A__ ) == set(A__ ) assert len(A__ ) == len(A__ ) assert set(my.items() ) == set(py.items() ) def lowercase_ ( ) -> Optional[int]: """simple docstring""" def is_public(A__ ) -> bool: return not name.startswith("_" ) snake_case = {name for name in dir({} ) if is_public(A__ )} snake_case = {name for name in dir(HashMap() ) if is_public(A__ )} assert dict_public_names > hash_public_names
137
0
'''simple docstring''' from pathlib import Path import cva import numpy as np from matplotlib import pyplot as plt def __lowercase ( __lowercase , __lowercase , __lowercase , __lowercase , __lowercase ) -> np.ndarray: '''simple docstring''' _A = cva.getAffineTransform(_UpperCAmelCase , _UpperCAmelCase ) return cva.warpAffine(_UpperCAmelCase , _UpperCAmelCase , (rows, cols) ) if __name__ == "__main__": # read original image lowerCamelCase_ = cva.imread( str(Path(__file__).resolve().parent.parent / '''image_data''' / '''lena.jpg''') ) # turn image in gray scale value lowerCamelCase_ = cva.cvtColor(image, cva.COLOR_BGR2GRAY) # get image shape lowerCamelCase_ = gray_img.shape # set different points to rotate image lowerCamelCase_ = np.array([[50, 50], [2_00, 50], [50, 2_00]], np.floataa) lowerCamelCase_ = np.array([[10, 1_00], [2_00, 50], [1_00, 2_50]], np.floataa) lowerCamelCase_ = np.array([[50, 50], [1_50, 50], [1_20, 2_00]], np.floataa) lowerCamelCase_ = np.array([[10, 1_00], [80, 50], [1_80, 2_50]], np.floataa) # add all rotated images in a list lowerCamelCase_ = [ gray_img, get_rotation(gray_img, ptsa, ptsa, img_rows, img_cols), get_rotation(gray_img, ptsa, ptsa, img_rows, img_cols), get_rotation(gray_img, ptsa, ptsa, img_rows, img_cols), ] # plot different image rotations lowerCamelCase_ = plt.figure(1) lowerCamelCase_ = ['Original', 'Rotation 1', 'Rotation 2', 'Rotation 3'] for i, image in enumerate(images): plt.subplot(2, 2, i + 1), plt.imshow(image, '''gray''') plt.title(titles[i]) plt.axis('''off''') plt.subplots_adjust(left=0.0, bottom=0.05, right=1.0, top=0.95) plt.show()
79
"""simple docstring""" from dataclasses import dataclass from typing import Dict, Optional, Union import torch import torch.nn.functional as F from torch import nn from ..configuration_utils import ConfigMixin, register_to_config from ..utils import BaseOutput from .attention import BasicTransformerBlock from .attention_processor import AttentionProcessor, AttnProcessor from .embeddings import TimestepEmbedding, Timesteps from .modeling_utils import ModelMixin @dataclass class a__ ( SCREAMING_SNAKE_CASE__ ): _lowerCamelCase = 42 class a__ ( SCREAMING_SNAKE_CASE__, SCREAMING_SNAKE_CASE__ ): @register_to_config def __init__( self : Optional[int], lowerCAmelCase : int = 32, lowerCAmelCase : int = 64, lowerCAmelCase : int = 20, lowerCAmelCase : int = 768, lowerCAmelCase : Optional[Any]=77, lowerCAmelCase : Tuple=4, lowerCAmelCase : float = 0.0, lowerCAmelCase : str = "silu", lowerCAmelCase : Optional[str] = None, lowerCAmelCase : Optional[str] = None, lowerCAmelCase : Optional[str] = "linear", lowerCAmelCase : Optional[str] = "prd", lowerCAmelCase : Optional[int] = None, lowerCAmelCase : Optional[int] = None, lowerCAmelCase : Optional[int] = None, ) -> List[Any]: super().__init__() lowercase : List[Any] = num_attention_heads lowercase : int = attention_head_dim lowercase : List[Any] = num_attention_heads * attention_head_dim lowercase : Tuple = additional_embeddings lowercase : Dict = time_embed_dim or inner_dim lowercase : Optional[Any] = embedding_proj_dim or embedding_dim lowercase : int = clip_embed_dim or embedding_dim lowercase : List[str] = Timesteps(lowerCAmelCase, lowerCAmelCase, 0 ) lowercase : List[str] = TimestepEmbedding(lowerCAmelCase, lowerCAmelCase, out_dim=lowerCAmelCase, act_fn=lowerCAmelCase ) lowercase : List[str] = nn.Linear(lowerCAmelCase, lowerCAmelCase ) if embedding_proj_norm_type is None: lowercase : str = None elif embedding_proj_norm_type == "layer": lowercase : Tuple = nn.LayerNorm(lowerCAmelCase ) else: raise ValueError(f'''unsupported embedding_proj_norm_type: {embedding_proj_norm_type}''' ) lowercase : List[str] = nn.Linear(lowerCAmelCase, lowerCAmelCase ) if encoder_hid_proj_type is None: lowercase : Optional[int] = None elif encoder_hid_proj_type == "linear": lowercase : Dict = nn.Linear(lowerCAmelCase, lowerCAmelCase ) else: raise ValueError(f'''unsupported encoder_hid_proj_type: {encoder_hid_proj_type}''' ) lowercase : Dict = nn.Parameter(torch.zeros(1, num_embeddings + additional_embeddings, lowerCAmelCase ) ) if added_emb_type == "prd": lowercase : Union[str, Any] = nn.Parameter(torch.zeros(1, 1, lowerCAmelCase ) ) elif added_emb_type is None: lowercase : str = None else: raise ValueError( f'''`added_emb_type`: {added_emb_type} is not supported. Make sure to choose one of `\'prd\'` or `None`.''' ) lowercase : Dict = nn.ModuleList( [ BasicTransformerBlock( lowerCAmelCase, lowerCAmelCase, lowerCAmelCase, dropout=lowerCAmelCase, activation_fn='gelu', attention_bias=lowerCAmelCase, ) for d in range(lowerCAmelCase ) ] ) if norm_in_type == "layer": lowercase : str = nn.LayerNorm(lowerCAmelCase ) elif norm_in_type is None: lowercase : Optional[int] = None else: raise ValueError(f'''Unsupported norm_in_type: {norm_in_type}.''' ) lowercase : int = nn.LayerNorm(lowerCAmelCase ) lowercase : str = nn.Linear(lowerCAmelCase, lowerCAmelCase ) lowercase : Optional[Any] = torch.full( [num_embeddings + additional_embeddings, num_embeddings + additional_embeddings], -1_0000.0 ) causal_attention_mask.triu_(1 ) lowercase : List[str] = causal_attention_mask[None, ...] self.register_buffer('causal_attention_mask', lowerCAmelCase, persistent=lowerCAmelCase ) lowercase : Any = nn.Parameter(torch.zeros(1, lowerCAmelCase ) ) lowercase : Any = nn.Parameter(torch.zeros(1, lowerCAmelCase ) ) @property # Copied from diffusers.models.unet_2d_condition.UNet2DConditionModel.attn_processors def lowercase ( self : Tuple ) -> Dict[str, AttentionProcessor]: lowercase : Any = {} def fn_recursive_add_processors(lowerCAmelCase : str, lowerCAmelCase : torch.nn.Module, lowerCAmelCase : Dict[str, AttentionProcessor] ): if hasattr(lowerCAmelCase, 'set_processor' ): lowercase : List[str] = module.processor for sub_name, child in module.named_children(): fn_recursive_add_processors(f'''{name}.{sub_name}''', lowerCAmelCase, lowerCAmelCase ) return processors for name, module in self.named_children(): fn_recursive_add_processors(lowerCAmelCase, lowerCAmelCase, lowerCAmelCase ) return processors def lowercase ( self : Union[str, Any], lowerCAmelCase : Union[AttentionProcessor, Dict[str, AttentionProcessor]] ) -> Tuple: lowercase : str = len(self.attn_processors.keys() ) if isinstance(lowerCAmelCase, lowerCAmelCase ) and len(lowerCAmelCase ) != count: raise ValueError( f'''A dict of processors was passed, but the number of processors {len(lowerCAmelCase )} does not match the''' f''' number of attention layers: {count}. Please make sure to pass {count} processor classes.''' ) def fn_recursive_attn_processor(lowerCAmelCase : str, lowerCAmelCase : torch.nn.Module, lowerCAmelCase : Union[str, Any] ): if hasattr(lowerCAmelCase, 'set_processor' ): if not isinstance(lowerCAmelCase, lowerCAmelCase ): module.set_processor(lowerCAmelCase ) else: module.set_processor(processor.pop(f'''{name}.processor''' ) ) for sub_name, child in module.named_children(): fn_recursive_attn_processor(f'''{name}.{sub_name}''', lowerCAmelCase, lowerCAmelCase ) for name, module in self.named_children(): fn_recursive_attn_processor(lowerCAmelCase, lowerCAmelCase, lowerCAmelCase ) def lowercase ( self : Optional[Any] ) -> Optional[Any]: self.set_attn_processor(AttnProcessor() ) def lowercase ( self : Any, lowerCAmelCase : int, lowerCAmelCase : Union[torch.Tensor, float, int], lowerCAmelCase : torch.FloatTensor, lowerCAmelCase : Optional[torch.FloatTensor] = None, lowerCAmelCase : Optional[torch.BoolTensor] = None, lowerCAmelCase : bool = True, ) -> List[Any]: lowercase : Optional[Any] = hidden_states.shape[0] lowercase : Union[str, Any] = timestep if not torch.is_tensor(lowerCAmelCase ): lowercase : List[str] = torch.tensor([timesteps], dtype=torch.long, device=hidden_states.device ) elif torch.is_tensor(lowerCAmelCase ) and len(timesteps.shape ) == 0: lowercase : List[str] = timesteps[None].to(hidden_states.device ) # broadcast to batch dimension in a way that's compatible with ONNX/Core ML lowercase : Optional[int] = timesteps * torch.ones(lowerCAmelCase, dtype=timesteps.dtype, device=timesteps.device ) lowercase : Dict = self.time_proj(lowerCAmelCase ) # timesteps does not contain any weights and will always return f32 tensors # but time_embedding might be fp16, so we need to cast here. lowercase : Optional[int] = timesteps_projected.to(dtype=self.dtype ) lowercase : Any = self.time_embedding(lowerCAmelCase ) if self.embedding_proj_norm is not None: lowercase : Any = self.embedding_proj_norm(lowerCAmelCase ) lowercase : List[str] = self.embedding_proj(lowerCAmelCase ) if self.encoder_hidden_states_proj is not None and encoder_hidden_states is not None: lowercase : str = self.encoder_hidden_states_proj(lowerCAmelCase ) elif self.encoder_hidden_states_proj is not None and encoder_hidden_states is None: raise ValueError('`encoder_hidden_states_proj` requires `encoder_hidden_states` to be set' ) lowercase : Optional[Any] = self.proj_in(lowerCAmelCase ) lowercase : Optional[int] = self.positional_embedding.to(hidden_states.dtype ) lowercase : Dict = [] lowercase : Optional[int] = 0 if encoder_hidden_states is not None: additional_embeds.append(lowerCAmelCase ) additional_embeddings_len += encoder_hidden_states.shape[1] if len(proj_embeddings.shape ) == 2: lowercase : str = proj_embeddings[:, None, :] if len(hidden_states.shape ) == 2: lowercase : Union[str, Any] = hidden_states[:, None, :] lowercase : int = additional_embeds + [ proj_embeddings, time_embeddings[:, None, :], hidden_states, ] if self.prd_embedding is not None: lowercase : List[str] = self.prd_embedding.to(hidden_states.dtype ).expand(lowerCAmelCase, -1, -1 ) additional_embeds.append(lowerCAmelCase ) lowercase : Union[str, Any] = torch.cat( lowerCAmelCase, dim=1, ) # Allow positional_embedding to not include the `addtional_embeddings` and instead pad it with zeros for these additional tokens lowercase : Optional[int] = additional_embeddings_len + proj_embeddings.shape[1] + 1 if positional_embeddings.shape[1] < hidden_states.shape[1]: lowercase : List[Any] = F.pad( lowerCAmelCase, ( 0, 0, additional_embeddings_len, self.prd_embedding.shape[1] if self.prd_embedding is not None else 0, ), value=0.0, ) lowercase : str = hidden_states + positional_embeddings if attention_mask is not None: lowercase : Tuple = (1 - attention_mask.to(hidden_states.dtype )) * -1_0000.0 lowercase : List[Any] = F.pad(lowerCAmelCase, (0, self.additional_embeddings), value=0.0 ) lowercase : int = (attention_mask[:, None, :] + self.causal_attention_mask).to(hidden_states.dtype ) lowercase : Union[str, Any] = attention_mask.repeat_interleave(self.config.num_attention_heads, dim=0 ) if self.norm_in is not None: lowercase : List[Any] = self.norm_in(lowerCAmelCase ) for block in self.transformer_blocks: lowercase : Tuple = block(lowerCAmelCase, attention_mask=lowerCAmelCase ) lowercase : Optional[Any] = self.norm_out(lowerCAmelCase ) if self.prd_embedding is not None: lowercase : Optional[Any] = hidden_states[:, -1] else: lowercase : Any = hidden_states[:, additional_embeddings_len:] lowercase : Optional[int] = self.proj_to_clip_embeddings(lowerCAmelCase ) if not return_dict: return (predicted_image_embedding,) return PriorTransformerOutput(predicted_image_embedding=lowerCAmelCase ) def lowercase ( self : Any, lowerCAmelCase : Dict ) -> Dict: lowercase : int = (prior_latents * self.clip_std) + self.clip_mean return prior_latents
255
0
"""simple docstring""" def lowerCAmelCase_( lowercase_ : int , lowercase_ : int ) -> int: while b: _lowerCamelCase , _lowerCamelCase = b, a % b return a def lowerCAmelCase_( lowercase_ : int , lowercase_ : int ) -> int: return a if b == 0 else euclidean_gcd_recursive(lowercase_ , a % b ) def lowerCAmelCase_( ) -> Union[str, Any]: print(F"""euclidean_gcd(3, 5) = {euclidean_gcd(3 , 5 )}""" ) print(F"""euclidean_gcd(5, 3) = {euclidean_gcd(5 , 3 )}""" ) print(F"""euclidean_gcd(1, 3) = {euclidean_gcd(1 , 3 )}""" ) print(F"""euclidean_gcd(3, 6) = {euclidean_gcd(3 , 6 )}""" ) print(F"""euclidean_gcd(6, 3) = {euclidean_gcd(6 , 3 )}""" ) print(F"""euclidean_gcd_recursive(3, 5) = {euclidean_gcd_recursive(3 , 5 )}""" ) print(F"""euclidean_gcd_recursive(5, 3) = {euclidean_gcd_recursive(5 , 3 )}""" ) print(F"""euclidean_gcd_recursive(1, 3) = {euclidean_gcd_recursive(1 , 3 )}""" ) print(F"""euclidean_gcd_recursive(3, 6) = {euclidean_gcd_recursive(3 , 6 )}""" ) print(F"""euclidean_gcd_recursive(6, 3) = {euclidean_gcd_recursive(6 , 3 )}""" ) if __name__ == "__main__": main()
354
"""simple docstring""" import argparse import glob import importlib.util import os import re import black from doc_builder.style_doc import style_docstrings_in_code # All paths are set with the intent you should run this script from the root of the repo with the command # python utils/check_copies.py __SCREAMING_SNAKE_CASE : List[Any] = '''src/diffusers''' __SCREAMING_SNAKE_CASE : Union[str, Any] = '''.''' # This is to make sure the diffusers module imported is the one in the repo. __SCREAMING_SNAKE_CASE : Tuple = importlib.util.spec_from_file_location( '''diffusers''', os.path.join(DIFFUSERS_PATH, '''__init__.py'''), submodule_search_locations=[DIFFUSERS_PATH], ) __SCREAMING_SNAKE_CASE : Union[str, Any] = spec.loader.load_module() def lowerCAmelCase_( lowercase_ : str , lowercase_ : Tuple ) -> int: return line.startswith(lowercase_ ) or len(lowercase_ ) <= 1 or re.search(r'''^\s*\)(\s*->.*:|:)\s*$''' , lowercase_ ) is not None def lowerCAmelCase_( lowercase_ : Any ) -> Tuple: _lowerCamelCase = object_name.split('''.''' ) _lowerCamelCase = 0 # First let's find the module where our object lives. _lowerCamelCase = parts[i] while i < len(lowercase_ ) and not os.path.isfile(os.path.join(lowercase_ , F"""{module}.py""" ) ): i += 1 if i < len(lowercase_ ): _lowerCamelCase = os.path.join(lowercase_ , parts[i] ) if i >= len(lowercase_ ): raise ValueError(F"""`object_name` should begin with the name of a module of diffusers but got {object_name}.""" ) with open(os.path.join(lowercase_ , F"""{module}.py""" ) , '''r''' , encoding='''utf-8''' , newline='''\n''' ) as f: _lowerCamelCase = f.readlines() # Now let's find the class / func in the code! _lowerCamelCase = '''''' _lowerCamelCase = 0 for name in parts[i + 1 :]: while ( line_index < len(lowercase_ ) and re.search(rF"""^{indent}(class|def)\s+{name}(\(|\:)""" , lines[line_index] ) is None ): line_index += 1 indent += " " line_index += 1 if line_index >= len(lowercase_ ): raise ValueError(F""" {object_name} does not match any function or class in {module}.""" ) # We found the beginning of the class / func, now let's find the end (when the indent diminishes). _lowerCamelCase = line_index while line_index < len(lowercase_ ) and _should_continue(lines[line_index] , lowercase_ ): line_index += 1 # Clean up empty lines at the end (if any). while len(lines[line_index - 1] ) <= 1: line_index -= 1 _lowerCamelCase = lines[start_index:line_index] return "".join(lowercase_ ) __SCREAMING_SNAKE_CASE : str = re.compile(R'''^(\s*)#\s*Copied from\s+diffusers\.(\S+\.\S+)\s*($|\S.*$)''') __SCREAMING_SNAKE_CASE : List[Any] = re.compile(R'''^\s*(\S+)->(\S+)(\s+.*|$)''') __SCREAMING_SNAKE_CASE : List[str] = re.compile(R'''<FILL\s+[^>]*>''') def lowerCAmelCase_( lowercase_ : List[Any] ) -> str: _lowerCamelCase = code.split('''\n''' ) _lowerCamelCase = 0 while idx < len(lowercase_ ) and len(lines[idx] ) == 0: idx += 1 if idx < len(lowercase_ ): return re.search(r'''^(\s*)\S''' , lines[idx] ).groups()[0] return "" def lowerCAmelCase_( lowercase_ : List[Any] ) -> Union[str, Any]: _lowerCamelCase = len(get_indent(lowercase_ ) ) > 0 if has_indent: _lowerCamelCase = F"""class Bla:\n{code}""" _lowerCamelCase = black.Mode(target_versions={black.TargetVersion.PYaa} , line_length=1_19 , preview=lowercase_ ) _lowerCamelCase = black.format_str(lowercase_ , mode=lowercase_ ) _lowerCamelCase , _lowerCamelCase = style_docstrings_in_code(lowercase_ ) return result[len('''class Bla:\n''' ) :] if has_indent else result def lowerCAmelCase_( lowercase_ : Dict , lowercase_ : Union[str, Any]=False ) -> str: with open(lowercase_ , '''r''' , encoding='''utf-8''' , newline='''\n''' ) as f: _lowerCamelCase = f.readlines() _lowerCamelCase = [] _lowerCamelCase = 0 # Not a for loop cause `lines` is going to change (if `overwrite=True`). while line_index < len(lowercase_ ): _lowerCamelCase = _re_copy_warning.search(lines[line_index] ) if search is None: line_index += 1 continue # There is some copied code here, let's retrieve the original. _lowerCamelCase , _lowerCamelCase , _lowerCamelCase = search.groups() _lowerCamelCase = find_code_in_diffusers(lowercase_ ) _lowerCamelCase = get_indent(lowercase_ ) _lowerCamelCase = line_index + 1 if indent == theoretical_indent else line_index + 2 _lowerCamelCase = theoretical_indent _lowerCamelCase = start_index # Loop to check the observed code, stop when indentation diminishes or if we see a End copy comment. _lowerCamelCase = True while line_index < len(lowercase_ ) and should_continue: line_index += 1 if line_index >= len(lowercase_ ): break _lowerCamelCase = lines[line_index] _lowerCamelCase = _should_continue(lowercase_ , lowercase_ ) and re.search(F"""^{indent}# End copy""" , lowercase_ ) is None # Clean up empty lines at the end (if any). while len(lines[line_index - 1] ) <= 1: line_index -= 1 _lowerCamelCase = lines[start_index:line_index] _lowerCamelCase = ''''''.join(lowercase_ ) # Remove any nested `Copied from` comments to avoid circular copies _lowerCamelCase = [line for line in theoretical_code.split('''\n''' ) if _re_copy_warning.search(lowercase_ ) is None] _lowerCamelCase = '''\n'''.join(lowercase_ ) # Before comparing, use the `replace_pattern` on the original code. if len(lowercase_ ) > 0: _lowerCamelCase = replace_pattern.replace('''with''' , '''''' ).split(''',''' ) _lowerCamelCase = [_re_replace_pattern.search(lowercase_ ) for p in patterns] for pattern in patterns: if pattern is None: continue _lowerCamelCase , _lowerCamelCase , _lowerCamelCase = pattern.groups() _lowerCamelCase = re.sub(lowercase_ , lowercase_ , lowercase_ ) if option.strip() == "all-casing": _lowerCamelCase = re.sub(obja.lower() , obja.lower() , lowercase_ ) _lowerCamelCase = re.sub(obja.upper() , obja.upper() , lowercase_ ) # Blackify after replacement. To be able to do that, we need the header (class or function definition) # from the previous line _lowerCamelCase = blackify(lines[start_index - 1] + theoretical_code ) _lowerCamelCase = theoretical_code[len(lines[start_index - 1] ) :] # Test for a diff and act accordingly. if observed_code != theoretical_code: diffs.append([object_name, start_index] ) if overwrite: _lowerCamelCase = lines[:start_index] + [theoretical_code] + lines[line_index:] _lowerCamelCase = start_index + 1 if overwrite and len(lowercase_ ) > 0: # Warn the user a file has been modified. print(F"""Detected changes, rewriting {filename}.""" ) with open(lowercase_ , '''w''' , encoding='''utf-8''' , newline='''\n''' ) as f: f.writelines(lowercase_ ) return diffs def lowerCAmelCase_( lowercase_ : bool = False ) -> Union[str, Any]: _lowerCamelCase = glob.glob(os.path.join(lowercase_ , '''**/*.py''' ) , recursive=lowercase_ ) _lowerCamelCase = [] for filename in all_files: _lowerCamelCase = is_copy_consistent(lowercase_ , lowercase_ ) diffs += [F"""- {filename}: copy does not match {d[0]} at line {d[1]}""" for d in new_diffs] if not overwrite and len(lowercase_ ) > 0: _lowerCamelCase = '''\n'''.join(lowercase_ ) raise Exception( '''Found the following copy inconsistencies:\n''' + diff + '''\nRun `make fix-copies` or `python utils/check_copies.py --fix_and_overwrite` to fix them.''' ) if __name__ == "__main__": __SCREAMING_SNAKE_CASE : str = argparse.ArgumentParser() parser.add_argument('''--fix_and_overwrite''', action='''store_true''', help='''Whether to fix inconsistencies.''') __SCREAMING_SNAKE_CASE : str = parser.parse_args() check_copies(args.fix_and_overwrite)
73
0
'''simple docstring''' from ...configuration_utils import PretrainedConfig from ...utils import logging __a = logging.get_logger(__name__) __a = { "microsoft/swinv2-tiny-patch4-window8-256": ( "https://huggingface.co/microsoft/swinv2-tiny-patch4-window8-256/resolve/main/config.json" ), } class UpperCAmelCase_ ( _a ): """simple docstring""" lowercase = "swinv2" lowercase = { "num_attention_heads": "num_heads", "num_hidden_layers": "num_layers", } def __init__( self : Any , snake_case_ : int=224 , snake_case_ : List[Any]=4 , snake_case_ : List[Any]=3 , snake_case_ : Optional[Any]=96 , snake_case_ : str=[2, 2, 6, 2] , snake_case_ : Tuple=[3, 6, 12, 24] , snake_case_ : Optional[Any]=7 , snake_case_ : List[str]=4.0 , snake_case_ : Optional[int]=True , snake_case_ : Any=0.0 , snake_case_ : Tuple=0.0 , snake_case_ : Union[str, Any]=0.1 , snake_case_ : Any="gelu" , snake_case_ : Optional[Any]=False , snake_case_ : List[str]=0.02 , snake_case_ : Dict=1E-5 , snake_case_ : Optional[int]=32 , **snake_case_ : Dict , ): super().__init__(**snake_case_ ) snake_case__ : Optional[int] = image_size snake_case__ : Union[str, Any] = patch_size snake_case__ : Optional[int] = num_channels snake_case__ : str = embed_dim snake_case__ : List[str] = depths snake_case__ : int = len(snake_case_ ) snake_case__ : Union[str, Any] = num_heads snake_case__ : Tuple = window_size snake_case__ : str = mlp_ratio snake_case__ : Optional[Any] = qkv_bias snake_case__ : Union[str, Any] = hidden_dropout_prob snake_case__ : Dict = attention_probs_dropout_prob snake_case__ : Optional[Any] = drop_path_rate snake_case__ : Tuple = hidden_act snake_case__ : str = use_absolute_embeddings snake_case__ : List[str] = layer_norm_eps snake_case__ : Optional[int] = initializer_range snake_case__ : Dict = encoder_stride # we set the hidden_size attribute in order to make Swinv2 work with VisionEncoderDecoderModel # this indicates the channel dimension after the last stage of the model snake_case__ : List[str] = int(embed_dim * 2 ** (len(snake_case_ ) - 1) ) snake_case__ : Tuple = (0, 0, 0, 0)
35
'''simple docstring''' import argparse import json import requests import timm import torch from huggingface_hub import hf_hub_download from PIL import Image from transformers import AutoImageProcessor, SwinConfig, SwinForImageClassification def __snake_case( _lowerCAmelCase ) -> List[Any]: snake_case__ : Dict = SwinConfig() snake_case__ : Optional[Any] = swin_name.split("""_""" ) snake_case__ : Any = name_split[1] snake_case__ : List[Any] = int(name_split[4] ) snake_case__ : int = int(name_split[3][-1] ) if model_size == "tiny": snake_case__ : List[Any] = 96 snake_case__ : int = (2, 2, 6, 2) snake_case__ : int = (3, 6, 12, 24) elif model_size == "small": snake_case__ : Union[str, Any] = 96 snake_case__ : Optional[Any] = (2, 2, 18, 2) snake_case__ : str = (3, 6, 12, 24) elif model_size == "base": snake_case__ : Dict = 128 snake_case__ : str = (2, 2, 18, 2) snake_case__ : Dict = (4, 8, 16, 32) else: snake_case__ : List[str] = 192 snake_case__ : str = (2, 2, 18, 2) snake_case__ : List[Any] = (6, 12, 24, 48) if "in22k" in swin_name: snake_case__ : str = 21_841 else: snake_case__ : List[str] = 1_000 snake_case__ : int = """huggingface/label-files""" snake_case__ : Any = """imagenet-1k-id2label.json""" snake_case__ : List[Any] = json.load(open(hf_hub_download(_lowerCAmelCase , _lowerCAmelCase , repo_type="""dataset""" ) , """r""" ) ) snake_case__ : Dict = {int(_lowerCAmelCase ): v for k, v in idalabel.items()} snake_case__ : Optional[int] = idalabel snake_case__ : List[Any] = {v: k for k, v in idalabel.items()} snake_case__ : List[Any] = img_size snake_case__ : Dict = num_classes snake_case__ : Dict = embed_dim snake_case__ : Optional[int] = depths snake_case__ : int = num_heads snake_case__ : Optional[int] = window_size return config def __snake_case( _lowerCAmelCase ) -> Dict: if "patch_embed.proj" in name: snake_case__ : List[str] = name.replace("""patch_embed.proj""" , """embeddings.patch_embeddings.projection""" ) if "patch_embed.norm" in name: snake_case__ : int = name.replace("""patch_embed.norm""" , """embeddings.norm""" ) if "layers" in name: snake_case__ : str = """encoder.""" + name if "attn.proj" in name: snake_case__ : List[str] = name.replace("""attn.proj""" , """attention.output.dense""" ) if "attn" in name: snake_case__ : Tuple = name.replace("""attn""" , """attention.self""" ) if "norm1" in name: snake_case__ : List[str] = name.replace("""norm1""" , """layernorm_before""" ) if "norm2" in name: snake_case__ : Optional[Any] = name.replace("""norm2""" , """layernorm_after""" ) if "mlp.fc1" in name: snake_case__ : Union[str, Any] = name.replace("""mlp.fc1""" , """intermediate.dense""" ) if "mlp.fc2" in name: snake_case__ : Dict = name.replace("""mlp.fc2""" , """output.dense""" ) if name == "norm.weight": snake_case__ : Tuple = """layernorm.weight""" if name == "norm.bias": snake_case__ : Union[str, Any] = """layernorm.bias""" if "head" in name: snake_case__ : Optional[int] = name.replace("""head""" , """classifier""" ) else: snake_case__ : List[str] = """swin.""" + name return name def __snake_case( _lowerCAmelCase , _lowerCAmelCase ) -> Optional[Any]: for key in orig_state_dict.copy().keys(): snake_case__ : Optional[int] = orig_state_dict.pop(_lowerCAmelCase ) if "mask" in key: continue elif "qkv" in key: snake_case__ : Dict = key.split(""".""" ) snake_case__ : Optional[int] = int(key_split[1] ) snake_case__ : Union[str, Any] = int(key_split[3] ) snake_case__ : List[Any] = model.swin.encoder.layers[layer_num].blocks[block_num].attention.self.all_head_size if "weight" in key: snake_case__ : Optional[Any] = val[:dim, :] snake_case__ : Tuple = val[ dim : dim * 2, : ] snake_case__ : Dict = val[-dim:, :] else: snake_case__ : Tuple = val[ :dim ] snake_case__ : int = val[ dim : dim * 2 ] snake_case__ : int = val[ -dim: ] else: snake_case__ : Union[str, Any] = val return orig_state_dict def __snake_case( _lowerCAmelCase , _lowerCAmelCase ) -> int: snake_case__ : Optional[int] = timm.create_model(_lowerCAmelCase , pretrained=_lowerCAmelCase ) timm_model.eval() snake_case__ : Optional[int] = get_swin_config(_lowerCAmelCase ) snake_case__ : Optional[Any] = SwinForImageClassification(_lowerCAmelCase ) model.eval() snake_case__ : str = convert_state_dict(timm_model.state_dict() , _lowerCAmelCase ) model.load_state_dict(_lowerCAmelCase ) snake_case__ : int = """http://images.cocodataset.org/val2017/000000039769.jpg""" snake_case__ : Dict = AutoImageProcessor.from_pretrained("""microsoft/{}""".format(swin_name.replace("""_""" , """-""" ) ) ) snake_case__ : Dict = Image.open(requests.get(_lowerCAmelCase , stream=_lowerCAmelCase ).raw ) snake_case__ : Optional[int] = image_processor(images=_lowerCAmelCase , return_tensors="""pt""" ) snake_case__ : Optional[Any] = timm_model(inputs["""pixel_values"""] ) snake_case__ : str = model(**_lowerCAmelCase ).logits assert torch.allclose(_lowerCAmelCase , _lowerCAmelCase , atol=1e-3 ) print(f"Saving model {swin_name} to {pytorch_dump_folder_path}" ) model.save_pretrained(_lowerCAmelCase ) print(f"Saving image processor to {pytorch_dump_folder_path}" ) image_processor.save_pretrained(_lowerCAmelCase ) if __name__ == "__main__": __a = argparse.ArgumentParser() # Required parameters parser.add_argument( "--swin_name", default="swin_tiny_patch4_window7_224", type=str, help="Name of the Swin timm model you'd like to convert.", ) parser.add_argument( "--pytorch_dump_folder_path", default=None, type=str, help="Path to the output PyTorch model directory." ) __a = parser.parse_args() convert_swin_checkpoint(args.swin_name, args.pytorch_dump_folder_path)
35
1
'''simple docstring''' import inspect from typing import Callable, List, Optional, Union import torch from transformers import CLIPImageProcessor, CLIPTextModel, CLIPTokenizer from diffusers import DiffusionPipeline from diffusers.models import AutoencoderKL, UNetaDConditionModel from diffusers.pipelines.stable_diffusion import StableDiffusionPipelineOutput from diffusers.pipelines.stable_diffusion.safety_checker import StableDiffusionSafetyChecker from diffusers.schedulers import DDIMScheduler, LMSDiscreteScheduler, PNDMScheduler from diffusers.utils import logging lowerCAmelCase : List[str] = logging.get_logger(__name__) # pylint: disable=invalid-name class UpperCamelCase__ ( SCREAMING_SNAKE_CASE_ ): """simple docstring""" def __init__( self , snake_case__ , snake_case__ , snake_case__ , snake_case__ , snake_case__ , snake_case__ , snake_case__ , ): '''simple docstring''' super().__init__() self.register_modules( vae=snake_case__ , text_encoder=snake_case__ , tokenizer=snake_case__ , unet=snake_case__ , scheduler=snake_case__ , safety_checker=snake_case__ , feature_extractor=snake_case__ , ) def a ( self , snake_case__ = "auto" ): '''simple docstring''' if slice_size == "auto": # half the attention head size is usually a good trade-off between # speed and memory _lowerCAmelCase : Optional[Any] = self.unet.config.attention_head_dim // 2 self.unet.set_attention_slice(snake_case__ ) def a ( self ): '''simple docstring''' self.enable_attention_slicing(snake_case__ ) @torch.no_grad() def __call__( self , snake_case__ , snake_case__ = 512 , snake_case__ = 512 , snake_case__ = 50 , snake_case__ = 7.5 , snake_case__ = None , snake_case__ = 1 , snake_case__ = 0.0 , snake_case__ = None , snake_case__ = None , snake_case__ = "pil" , snake_case__ = True , snake_case__ = None , snake_case__ = 1 , snake_case__ = None , **snake_case__ , ): '''simple docstring''' if isinstance(snake_case__ , snake_case__ ): _lowerCAmelCase : Tuple = 1 elif isinstance(snake_case__ , snake_case__ ): _lowerCAmelCase : List[str] = len(snake_case__ ) else: raise ValueError(F'`prompt` has to be of type `str` or `list` but is {type(snake_case__ )}' ) if height % 8 != 0 or width % 8 != 0: raise ValueError(F'`height` and `width` have to be divisible by 8 but are {height} and {width}.' ) if (callback_steps is None) or ( callback_steps is not None and (not isinstance(snake_case__ , snake_case__ ) or callback_steps <= 0) ): raise ValueError( F'`callback_steps` has to be a positive integer but is {callback_steps} of type' F' {type(snake_case__ )}.' ) # get prompt text embeddings _lowerCAmelCase : Union[str, Any] = self.tokenizer( snake_case__ , padding='max_length' , max_length=self.tokenizer.model_max_length , return_tensors='pt' , ) _lowerCAmelCase : List[Any] = text_inputs.input_ids if text_input_ids.shape[-1] > self.tokenizer.model_max_length: _lowerCAmelCase : Union[str, Any] = self.tokenizer.batch_decode(text_input_ids[:, self.tokenizer.model_max_length :] ) logger.warning( 'The following part of your input was truncated because CLIP can only handle sequences up to' F' {self.tokenizer.model_max_length} tokens: {removed_text}' ) _lowerCAmelCase : List[str] = text_input_ids[:, : self.tokenizer.model_max_length] if text_embeddings is None: _lowerCAmelCase : Optional[Any] = self.text_encoder(text_input_ids.to(self.device ) )[0] # duplicate text embeddings for each generation per prompt, using mps friendly method _lowerCAmelCase , _lowerCAmelCase , _lowerCAmelCase : Any = text_embeddings.shape _lowerCAmelCase : int = text_embeddings.repeat(1 , snake_case__ , 1 ) _lowerCAmelCase : Union[str, Any] = text_embeddings.view(bs_embed * num_images_per_prompt , snake_case__ , -1 ) # here `guidance_scale` is defined analog to the guidance weight `w` of equation (2) # of the Imagen paper: https://arxiv.org/pdf/2205.11487.pdf . `guidance_scale = 1` # corresponds to doing no classifier free guidance. _lowerCAmelCase : Tuple = guidance_scale > 1.0 # get unconditional embeddings for classifier free guidance if do_classifier_free_guidance: _lowerCAmelCase : List[str] if negative_prompt is None: _lowerCAmelCase : Dict = [''] elif type(snake_case__ ) is not type(snake_case__ ): raise TypeError( F'`negative_prompt` should be the same type to `prompt`, but got {type(snake_case__ )} !=' F' {type(snake_case__ )}.' ) elif isinstance(snake_case__ , snake_case__ ): _lowerCAmelCase : List[str] = [negative_prompt] elif batch_size != len(snake_case__ ): raise ValueError( F'`negative_prompt`: {negative_prompt} has batch size {len(snake_case__ )}, but `prompt`:' F' {prompt} has batch size {batch_size}. Please make sure that passed `negative_prompt` matches' ' the batch size of `prompt`.' ) else: _lowerCAmelCase : Optional[Any] = negative_prompt _lowerCAmelCase : Tuple = text_input_ids.shape[-1] _lowerCAmelCase : Optional[Any] = self.tokenizer( snake_case__ , padding='max_length' , max_length=snake_case__ , truncation=snake_case__ , return_tensors='pt' , ) _lowerCAmelCase : Optional[int] = self.text_encoder(uncond_input.input_ids.to(self.device ) )[0] # duplicate unconditional embeddings for each generation per prompt, using mps friendly method _lowerCAmelCase : Dict = uncond_embeddings.shape[1] _lowerCAmelCase : Any = uncond_embeddings.repeat(snake_case__ , snake_case__ , 1 ) _lowerCAmelCase : List[Any] = uncond_embeddings.view(batch_size * num_images_per_prompt , snake_case__ , -1 ) # For classifier free guidance, we need to do two forward passes. # Here we concatenate the unconditional and text embeddings into a single batch # to avoid doing two forward passes _lowerCAmelCase : List[str] = torch.cat([uncond_embeddings, text_embeddings] ) # get the initial random noise unless the user supplied it # Unlike in other pipelines, latents need to be generated in the target device # for 1-to-1 results reproducibility with the CompVis implementation. # However this currently doesn't work in `mps`. _lowerCAmelCase : Tuple = (batch_size * num_images_per_prompt, self.unet.config.in_channels, height // 8, width // 8) _lowerCAmelCase : Any = (batch_size * num_images_per_prompt, self.unet.config.in_channels, 64, 64) _lowerCAmelCase : Optional[int] = text_embeddings.dtype if latents is None: if self.device.type == "mps": # randn does not exist on mps _lowerCAmelCase : Tuple = torch.randn( snake_case__ , generator=snake_case__ , device='cpu' , dtype=snake_case__ ).to(self.device ) _lowerCAmelCase : Optional[Any] = torch.randn(snake_case__ , generator=snake_case__ , device='cpu' , dtype=snake_case__ ).to( self.device ) else: _lowerCAmelCase : List[str] = torch.randn( snake_case__ , generator=snake_case__ , device=self.device , dtype=snake_case__ ) _lowerCAmelCase : List[str] = torch.randn(snake_case__ , generator=snake_case__ , device=self.device , dtype=snake_case__ ) else: if latents_reference.shape != latents_shape: raise ValueError(F'Unexpected latents shape, got {latents.shape}, expected {latents_shape}' ) _lowerCAmelCase : str = latents_reference.to(self.device ) _lowerCAmelCase : Dict = latents.to(self.device ) # This is the key part of the pipeline where we # try to ensure that the generated images w/ the same seed # but different sizes actually result in similar images _lowerCAmelCase : Dict = (latents_shape[3] - latents_shape_reference[3]) // 2 _lowerCAmelCase : Tuple = (latents_shape[2] - latents_shape_reference[2]) // 2 _lowerCAmelCase : Union[str, Any] = latents_shape_reference[3] if dx >= 0 else latents_shape_reference[3] + 2 * dx _lowerCAmelCase : Tuple = latents_shape_reference[2] if dy >= 0 else latents_shape_reference[2] + 2 * dy _lowerCAmelCase : int = 0 if dx < 0 else dx _lowerCAmelCase : str = 0 if dy < 0 else dy _lowerCAmelCase : Tuple = max(-dx , 0 ) _lowerCAmelCase : str = max(-dy , 0 ) # import pdb # pdb.set_trace() _lowerCAmelCase : Union[str, Any] = latents_reference[:, :, dy : dy + h, dx : dx + w] # set timesteps self.scheduler.set_timesteps(snake_case__ ) # Some schedulers like PNDM have timesteps as arrays # It's more optimized to move all timesteps to correct device beforehand _lowerCAmelCase : Dict = self.scheduler.timesteps.to(self.device ) # scale the initial noise by the standard deviation required by the scheduler _lowerCAmelCase : Dict = latents * self.scheduler.init_noise_sigma # prepare extra kwargs for the scheduler step, since not all schedulers have the same signature # eta (η) is only used with the DDIMScheduler, it will be ignored for other schedulers. # eta corresponds to η in DDIM paper: https://arxiv.org/abs/2010.02502 # and should be between [0, 1] _lowerCAmelCase : Optional[int] = 'eta' in set(inspect.signature(self.scheduler.step ).parameters.keys() ) _lowerCAmelCase : Optional[int] = {} if accepts_eta: _lowerCAmelCase : Optional[Any] = eta for i, t in enumerate(self.progress_bar(snake_case__ ) ): # expand the latents if we are doing classifier free guidance _lowerCAmelCase : List[str] = torch.cat([latents] * 2 ) if do_classifier_free_guidance else latents _lowerCAmelCase : List[str] = self.scheduler.scale_model_input(snake_case__ , snake_case__ ) # predict the noise residual _lowerCAmelCase : Union[str, Any] = self.unet(snake_case__ , snake_case__ , encoder_hidden_states=snake_case__ ).sample # perform guidance if do_classifier_free_guidance: _lowerCAmelCase , _lowerCAmelCase : List[Any] = noise_pred.chunk(2 ) _lowerCAmelCase : Any = noise_pred_uncond + guidance_scale * (noise_pred_text - noise_pred_uncond) # compute the previous noisy sample x_t -> x_t-1 _lowerCAmelCase : List[Any] = self.scheduler.step(snake_case__ , snake_case__ , snake_case__ , **snake_case__ ).prev_sample # call the callback, if provided if callback is not None and i % callback_steps == 0: callback(snake_case__ , snake_case__ , snake_case__ ) _lowerCAmelCase : Any = 1 / 0.1_8215 * latents _lowerCAmelCase : Optional[int] = self.vae.decode(snake_case__ ).sample _lowerCAmelCase : List[Any] = (image / 2 + 0.5).clamp(0 , 1 ) # we always cast to float32 as this does not cause significant overhead and is compatible with bfloat16 _lowerCAmelCase : Any = image.cpu().permute(0 , 2 , 3 , 1 ).float().numpy() if self.safety_checker is not None: _lowerCAmelCase : Union[str, Any] = self.feature_extractor(self.numpy_to_pil(snake_case__ ) , return_tensors='pt' ).to( self.device ) _lowerCAmelCase , _lowerCAmelCase : Dict = self.safety_checker( images=snake_case__ , clip_input=safety_checker_input.pixel_values.to(text_embeddings.dtype ) ) else: _lowerCAmelCase : Dict = None if output_type == "pil": _lowerCAmelCase : str = self.numpy_to_pil(snake_case__ ) if not return_dict: return (image, has_nsfw_concept) return StableDiffusionPipelineOutput(images=snake_case__ , nsfw_content_detected=snake_case__ )
25
'''simple docstring''' import shutil import tempfile import unittest from transformers import SPIECE_UNDERLINE, BatchEncoding, MBartaaTokenizer, MBartaaTokenizerFast, is_torch_available from transformers.testing_utils import ( get_tests_dir, nested_simplify, require_sentencepiece, require_tokenizers, require_torch, slow, ) from ...test_tokenization_common import TokenizerTesterMixin lowerCAmelCase : Tuple = get_tests_dir("""fixtures/test_sentencepiece.model""") if is_torch_available(): from transformers.models.mbart.modeling_mbart import shift_tokens_right lowerCAmelCase : Union[str, Any] = 25_00_04 lowerCAmelCase : int = 25_00_20 @require_sentencepiece @require_tokenizers class UpperCamelCase__ ( SCREAMING_SNAKE_CASE_ , unittest.TestCase ): """simple docstring""" __magic_name__ = MBartaaTokenizer __magic_name__ = MBartaaTokenizerFast __magic_name__ = True __magic_name__ = True def a ( self ): '''simple docstring''' super().setUp() # We have a SentencePiece fixture for testing _lowerCAmelCase : List[Any] = MBartaaTokenizer(snake_case__ , src_lang='en_XX' , tgt_lang='ro_RO' , keep_accents=snake_case__ ) tokenizer.save_pretrained(self.tmpdirname ) def a ( self ): '''simple docstring''' _lowerCAmelCase : Optional[int] = '<s>' _lowerCAmelCase : str = 0 self.assertEqual(self.get_tokenizer()._convert_token_to_id(snake_case__ ) , snake_case__ ) self.assertEqual(self.get_tokenizer()._convert_id_to_token(snake_case__ ) , snake_case__ ) def a ( self ): '''simple docstring''' _lowerCAmelCase : Dict = list(self.get_tokenizer().get_vocab().keys() ) self.assertEqual(vocab_keys[0] , '<s>' ) self.assertEqual(vocab_keys[1] , '<pad>' ) self.assertEqual(vocab_keys[-1] , '<mask>' ) self.assertEqual(len(snake_case__ ) , 1054 ) def a ( self ): '''simple docstring''' self.assertEqual(self.get_tokenizer().vocab_size , 1054 ) def a ( self ): '''simple docstring''' _lowerCAmelCase : Optional[Any] = MBartaaTokenizer(snake_case__ , src_lang='en_XX' , tgt_lang='ro_RO' , keep_accents=snake_case__ ) _lowerCAmelCase : Any = tokenizer.tokenize('This is a test' ) self.assertListEqual(snake_case__ , ['▁This', '▁is', '▁a', '▁t', 'est'] ) self.assertListEqual( tokenizer.convert_tokens_to_ids(snake_case__ ) , [value + tokenizer.fairseq_offset for value in [285, 46, 10, 170, 382]] , ) _lowerCAmelCase : Tuple = tokenizer.tokenize('I was born in 92000, and this is falsé.' ) self.assertListEqual( snake_case__ , [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 : Optional[int] = tokenizer.convert_tokens_to_ids(snake_case__ ) self.assertListEqual( snake_case__ , [ value + tokenizer.fairseq_offset for value in [8, 21, 84, 55, 24, 19, 7, 2, 602, 347, 347, 347, 3, 12, 66, 46, 72, 80, 6, 2, 4] ] , ) _lowerCAmelCase : Optional[Any] = tokenizer.convert_ids_to_tokens(snake_case__ ) self.assertListEqual( snake_case__ , [SPIECE_UNDERLINE + 'I', SPIECE_UNDERLINE + 'was', SPIECE_UNDERLINE + 'b', 'or', 'n', SPIECE_UNDERLINE + 'in', SPIECE_UNDERLINE + '', '<unk>', '2', '0', '0', '0', ',', SPIECE_UNDERLINE + 'and', SPIECE_UNDERLINE + 'this', SPIECE_UNDERLINE + 'is', SPIECE_UNDERLINE + 'f', 'al', 's', '<unk>', '.'] , ) @slow def a ( self ): '''simple docstring''' _lowerCAmelCase : Dict = {'input_ids': [[25_0004, 1_1062, 8_2772, 7, 15, 8_2772, 538, 5_1529, 237, 1_7198, 1290, 206, 9, 21_5175, 1314, 136, 1_7198, 1290, 206, 9, 5_6359, 42, 12_2009, 9, 1_6466, 16, 8_7344, 4537, 9, 4717, 7_8381, 6, 15_9958, 7, 15, 2_4480, 618, 4, 527, 2_2693, 5428, 4, 2777, 2_4480, 9874, 4, 4_3523, 594, 4, 803, 1_8392, 3_3189, 18, 4, 4_3523, 2_4447, 1_2399, 100, 2_4955, 8_3658, 9626, 14_4057, 15, 839, 2_2335, 16, 136, 2_4955, 8_3658, 8_3479, 15, 3_9102, 724, 16, 678, 645, 2789, 1328, 4589, 42, 12_2009, 11_5774, 23, 805, 1328, 4_6876, 7, 136, 5_3894, 1940, 4_2227, 4_1159, 1_7721, 823, 425, 4, 2_7512, 9_8722, 206, 136, 5531, 4970, 919, 1_7336, 5, 2], [25_0004, 2_0080, 618, 83, 8_2775, 47, 479, 9, 1517, 73, 5_3894, 333, 8_0581, 11_0117, 1_8811, 5256, 1295, 51, 15_2526, 297, 7986, 390, 12_4416, 538, 3_5431, 214, 98, 1_5044, 2_5737, 136, 7108, 4_3701, 23, 756, 13_5355, 7, 5, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [25_0004, 581, 6_3773, 11_9455, 6, 14_7797, 8_8203, 7, 645, 70, 21, 3285, 1_0269, 5, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]], 'attention_mask': [[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 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=snake_case__ , model_name='facebook/mbart-large-50' , revision='d3913889c59cd5c9e456b269c376325eabad57e2' , ) def a ( self ): '''simple docstring''' if not self.test_slow_tokenizer: # as we don't have a slow version, we can't compare the outputs between slow and fast versions return _lowerCAmelCase : Optional[int] = (self.rust_tokenizer_class, 'hf-internal-testing/tiny-random-mbart50', {}) for tokenizer, pretrained_name, kwargs in self.tokenizers_list: with self.subTest(F'{tokenizer.__class__.__name__} ({pretrained_name})' ): _lowerCAmelCase : Union[str, Any] = self.rust_tokenizer_class.from_pretrained(snake_case__ , **snake_case__ ) _lowerCAmelCase : Tuple = self.tokenizer_class.from_pretrained(snake_case__ , **snake_case__ ) _lowerCAmelCase : Optional[Any] = tempfile.mkdtemp() _lowerCAmelCase : Tuple = tokenizer_r.save_pretrained(snake_case__ ) _lowerCAmelCase : str = tokenizer_p.save_pretrained(snake_case__ ) # Checks it save with the same files + the tokenizer.json file for the fast one self.assertTrue(any('tokenizer.json' in f for f in tokenizer_r_files ) ) _lowerCAmelCase : Any = tuple(f for f in tokenizer_r_files if 'tokenizer.json' not in f ) self.assertSequenceEqual(snake_case__ , snake_case__ ) # Checks everything loads correctly in the same way _lowerCAmelCase : List[str] = tokenizer_r.from_pretrained(snake_case__ ) _lowerCAmelCase : Optional[int] = tokenizer_p.from_pretrained(snake_case__ ) # Check special tokens are set accordingly on Rust and Python for key in tokenizer_pp.special_tokens_map: self.assertTrue(hasattr(snake_case__ , snake_case__ ) ) # self.assertEqual(getattr(tokenizer_rp, key), getattr(tokenizer_pp, key)) # self.assertEqual(getattr(tokenizer_rp, key + "_id"), getattr(tokenizer_pp, key + "_id")) shutil.rmtree(snake_case__ ) # Save tokenizer rust, legacy_format=True _lowerCAmelCase : Union[str, Any] = tempfile.mkdtemp() _lowerCAmelCase : Dict = tokenizer_r.save_pretrained(snake_case__ , legacy_format=snake_case__ ) _lowerCAmelCase : Any = tokenizer_p.save_pretrained(snake_case__ ) # Checks it save with the same files self.assertSequenceEqual(snake_case__ , snake_case__ ) # Checks everything loads correctly in the same way _lowerCAmelCase : Dict = tokenizer_r.from_pretrained(snake_case__ ) _lowerCAmelCase : List[str] = tokenizer_p.from_pretrained(snake_case__ ) # Check special tokens are set accordingly on Rust and Python for key in tokenizer_pp.special_tokens_map: self.assertTrue(hasattr(snake_case__ , snake_case__ ) ) shutil.rmtree(snake_case__ ) # Save tokenizer rust, legacy_format=False _lowerCAmelCase : Optional[int] = tempfile.mkdtemp() _lowerCAmelCase : int = tokenizer_r.save_pretrained(snake_case__ , legacy_format=snake_case__ ) _lowerCAmelCase : Tuple = tokenizer_p.save_pretrained(snake_case__ ) # Checks it saved the tokenizer.json file self.assertTrue(any('tokenizer.json' in f for f in tokenizer_r_files ) ) # Checks everything loads correctly in the same way _lowerCAmelCase : int = tokenizer_r.from_pretrained(snake_case__ ) _lowerCAmelCase : str = tokenizer_p.from_pretrained(snake_case__ ) # Check special tokens are set accordingly on Rust and Python for key in tokenizer_pp.special_tokens_map: self.assertTrue(hasattr(snake_case__ , snake_case__ ) ) shutil.rmtree(snake_case__ ) @require_torch @require_sentencepiece @require_tokenizers class UpperCamelCase__ ( unittest.TestCase ): """simple docstring""" __magic_name__ = "facebook/mbart-large-50-one-to-many-mmt" __magic_name__ = [ " UN Chief Says There Is No Military Solution in Syria", " Secretary-General Ban Ki-moon says his response to Russia's stepped up military support for Syria is that \"there is no military solution\" to the nearly five-year conflict and more weapons will only worsen the violence and misery for millions of people.", ] __magic_name__ = [ "Şeful ONU declară că nu există o soluţie militară în Siria", "Secretarul General Ban Ki-moon declară că răspunsul său la intensificarea sprijinului militar al Rusiei" " pentru Siria este că \"nu există o soluţie militară\" la conflictul de aproape cinci ani şi că noi arme nu vor" " face decât să înrăutăţească violenţele şi mizeria pentru milioane de oameni.", ] __magic_name__ = [EN_CODE, 8_2_7_4, 1_2_7_8_7_3, 2_5_9_1_6, 7, 8_6_2_2, 2_0_7_1, 4_3_8, 6_7_4_8_5, 5_3, 1_8_7_8_9_5, 2_3, 5_1_7_1_2, 2] @classmethod def a ( cls ): '''simple docstring''' _lowerCAmelCase : MBartaaTokenizer = MBartaaTokenizer.from_pretrained( cls.checkpoint_name , src_lang='en_XX' , tgt_lang='ro_RO' ) _lowerCAmelCase : Dict = 1 return cls def a ( self ): '''simple docstring''' self.assertEqual(self.tokenizer.fairseq_tokens_to_ids['ar_AR'] , 25_0001 ) self.assertEqual(self.tokenizer.fairseq_tokens_to_ids['en_EN'] , 25_0004 ) self.assertEqual(self.tokenizer.fairseq_tokens_to_ids['ro_RO'] , 25_0020 ) self.assertEqual(self.tokenizer.fairseq_tokens_to_ids['mr_IN'] , 25_0038 ) def a ( self ): '''simple docstring''' _lowerCAmelCase : int = self.tokenizer.batch_encode_plus(self.src_text ).input_ids[0] self.assertListEqual(self.expected_src_tokens , snake_case__ ) def a ( self ): '''simple docstring''' self.assertIn(snake_case__ , self.tokenizer.all_special_ids ) _lowerCAmelCase : Union[str, Any] = [RO_CODE, 884, 9019, 96, 9, 916, 8_6792, 36, 1_8743, 1_5596, 5, 2] _lowerCAmelCase : List[str] = self.tokenizer.decode(snake_case__ , skip_special_tokens=snake_case__ ) _lowerCAmelCase : str = self.tokenizer.decode(generated_ids[1:] , skip_special_tokens=snake_case__ ) self.assertEqual(snake_case__ , snake_case__ ) self.assertNotIn(self.tokenizer.eos_token , snake_case__ ) def a ( self ): '''simple docstring''' _lowerCAmelCase : str = ['this is gunna be a long sentence ' * 20] assert isinstance(src_text[0] , snake_case__ ) _lowerCAmelCase : List[str] = 10 _lowerCAmelCase : Any = self.tokenizer(snake_case__ , max_length=snake_case__ , truncation=snake_case__ ).input_ids[0] self.assertEqual(ids[0] , snake_case__ ) self.assertEqual(ids[-1] , 2 ) self.assertEqual(len(snake_case__ ) , snake_case__ ) def a ( self ): '''simple docstring''' self.assertListEqual(self.tokenizer.convert_tokens_to_ids(['<mask>', 'ar_AR'] ) , [25_0053, 25_0001] ) def a ( self ): '''simple docstring''' _lowerCAmelCase : Optional[Any] = tempfile.mkdtemp() _lowerCAmelCase : Dict = self.tokenizer.fairseq_tokens_to_ids self.tokenizer.save_pretrained(snake_case__ ) _lowerCAmelCase : Tuple = MBartaaTokenizer.from_pretrained(snake_case__ ) self.assertDictEqual(new_tok.fairseq_tokens_to_ids , snake_case__ ) @require_torch def a ( self ): '''simple docstring''' _lowerCAmelCase : str = self.tokenizer(self.src_text , text_target=self.tgt_text , padding=snake_case__ , return_tensors='pt' ) _lowerCAmelCase : Optional[int] = shift_tokens_right(batch['labels'] , self.tokenizer.pad_token_id ) # fairseq batch: https://gist.github.com/sshleifer/cba08bc2109361a74ac3760a7e30e4f4 assert batch.input_ids[1][0] == EN_CODE assert batch.input_ids[1][-1] == 2 assert batch.labels[1][0] == RO_CODE assert batch.labels[1][-1] == 2 assert batch.decoder_input_ids[1][:2].tolist() == [2, RO_CODE] @require_torch def a ( self ): '''simple docstring''' _lowerCAmelCase : str = self.tokenizer( self.src_text , text_target=self.tgt_text , padding=snake_case__ , truncation=snake_case__ , max_length=len(self.expected_src_tokens ) , return_tensors='pt' , ) _lowerCAmelCase : int = shift_tokens_right(batch['labels'] , self.tokenizer.pad_token_id ) self.assertIsInstance(snake_case__ , snake_case__ ) self.assertEqual((2, 14) , batch.input_ids.shape ) self.assertEqual((2, 14) , batch.attention_mask.shape ) _lowerCAmelCase : Union[str, Any] = batch.input_ids.tolist()[0] self.assertListEqual(self.expected_src_tokens , snake_case__ ) self.assertEqual(2 , batch.decoder_input_ids[0, 0] ) # decoder_start_token_id # Test that special tokens are reset self.assertEqual(self.tokenizer.prefix_tokens , [EN_CODE] ) self.assertEqual(self.tokenizer.suffix_tokens , [self.tokenizer.eos_token_id] ) def a ( self ): '''simple docstring''' _lowerCAmelCase : Union[str, Any] = self.tokenizer(self.src_text , padding=snake_case__ , truncation=snake_case__ , max_length=3 , return_tensors='pt' ) _lowerCAmelCase : str = self.tokenizer( text_target=self.tgt_text , padding=snake_case__ , truncation=snake_case__ , max_length=10 , return_tensors='pt' ) _lowerCAmelCase : List[Any] = targets['input_ids'] _lowerCAmelCase : Any = shift_tokens_right(snake_case__ , 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 a ( self ): '''simple docstring''' _lowerCAmelCase : Optional[int] = self.tokenizer._build_translation_inputs( 'A test' , return_tensors='pt' , src_lang='en_XX' , tgt_lang='ar_AR' ) self.assertEqual( nested_simplify(snake_case__ ) , { # en_XX, A, test, EOS 'input_ids': [[25_0004, 62, 3034, 2]], 'attention_mask': [[1, 1, 1, 1]], # ar_AR 'forced_bos_token_id': 25_0001, } , )
25
1
"""simple docstring""" import argparse from diffusers.pipelines.stable_diffusion.convert_from_ckpt import download_controlnet_from_original_ckpt if __name__ == "__main__": UpperCAmelCase_ : str = argparse.ArgumentParser() parser.add_argument( """--checkpoint_path""", default=None, type=str, required=True, help="""Path to the checkpoint to convert.""" ) parser.add_argument( """--original_config_file""", type=str, required=True, help="""The YAML config file corresponding to the original architecture.""", ) parser.add_argument( """--num_in_channels""", default=None, type=int, help="""The number of input channels. If `None` number of input channels will be automatically inferred.""", ) parser.add_argument( """--image_size""", default=512, type=int, help=( """The image size that the model was trained on. Use 512 for Stable Diffusion v1.X and Stable Siffusion v2""" """ Base. Use 768 for Stable Diffusion v2.""" ), ) parser.add_argument( """--extract_ema""", action="""store_true""", help=( """Only relevant for checkpoints that have both EMA and non-EMA weights. Whether to extract the EMA weights""" """ or not. Defaults to `False`. Add `--extract_ema` to extract the EMA weights. EMA weights usually yield""" """ higher quality images for inference. Non-EMA weights are usually better to continue fine-tuning.""" ), ) parser.add_argument( """--upcast_attention""", action="""store_true""", help=( """Whether the attention computation should always be upcasted. This is necessary when running stable""" """ diffusion 2.1.""" ), ) parser.add_argument( """--from_safetensors""", action="""store_true""", help="""If `--checkpoint_path` is in `safetensors` format, load checkpoint with safetensors instead of PyTorch.""", ) parser.add_argument( """--to_safetensors""", action="""store_true""", help="""Whether to store pipeline in safetensors format or not.""", ) parser.add_argument("""--dump_path""", default=None, type=str, required=True, help="""Path to the output model.""") parser.add_argument("""--device""", type=str, help="""Device to use (e.g. cpu, cuda:0, cuda:1, etc.)""") def _A (__a ) -> Any: """simple docstring""" if string == "True": return True elif string == "False": return False else: raise ValueError(f'could not parse string as bool {string}' ) parser.add_argument( """--use_linear_projection""", help="""Override for use linear projection""", required=False, type=parse_bool ) parser.add_argument("""--cross_attention_dim""", help="""Override for cross attention_dim""", required=False, type=int) UpperCAmelCase_ : int = parser.parse_args() UpperCAmelCase_ : Union[str, Any] = download_controlnet_from_original_ckpt( checkpoint_path=args.checkpoint_path, original_config_file=args.original_config_file, image_size=args.image_size, extract_ema=args.extract_ema, num_in_channels=args.num_in_channels, upcast_attention=args.upcast_attention, from_safetensors=args.from_safetensors, device=args.device, use_linear_projection=args.use_linear_projection, cross_attention_dim=args.cross_attention_dim, ) controlnet.save_pretrained(args.dump_path, safe_serialization=args.to_safetensors)
91
"""simple docstring""" import unittest from diffusers.pipelines.pipeline_utils import is_safetensors_compatible class lowerCAmelCase__ ( unittest.TestCase ): '''simple docstring''' def _SCREAMING_SNAKE_CASE ( self : str): '''simple docstring''' SCREAMING_SNAKE_CASE_ : Optional[Any] = [ '''safety_checker/pytorch_model.bin''', '''safety_checker/model.safetensors''', '''vae/diffusion_pytorch_model.bin''', '''vae/diffusion_pytorch_model.safetensors''', '''text_encoder/pytorch_model.bin''', '''text_encoder/model.safetensors''', '''unet/diffusion_pytorch_model.bin''', '''unet/diffusion_pytorch_model.safetensors''', ] self.assertTrue(is_safetensors_compatible(lowercase_)) def _SCREAMING_SNAKE_CASE ( self : Optional[int]): '''simple docstring''' SCREAMING_SNAKE_CASE_ : Optional[Any] = [ '''unet/diffusion_pytorch_model.bin''', '''unet/diffusion_pytorch_model.safetensors''', ] self.assertTrue(is_safetensors_compatible(lowercase_)) def _SCREAMING_SNAKE_CASE ( self : List[str]): '''simple docstring''' SCREAMING_SNAKE_CASE_ : Optional[int] = [ '''safety_checker/pytorch_model.bin''', '''safety_checker/model.safetensors''', '''vae/diffusion_pytorch_model.bin''', '''vae/diffusion_pytorch_model.safetensors''', '''text_encoder/pytorch_model.bin''', '''text_encoder/model.safetensors''', '''unet/diffusion_pytorch_model.bin''', # Removed: 'unet/diffusion_pytorch_model.safetensors', ] self.assertFalse(is_safetensors_compatible(lowercase_)) def _SCREAMING_SNAKE_CASE ( self : int): '''simple docstring''' SCREAMING_SNAKE_CASE_ : List[Any] = [ '''text_encoder/pytorch_model.bin''', '''text_encoder/model.safetensors''', ] self.assertTrue(is_safetensors_compatible(lowercase_)) def _SCREAMING_SNAKE_CASE ( self : Optional[int]): '''simple docstring''' SCREAMING_SNAKE_CASE_ : Optional[int] = [ '''safety_checker/pytorch_model.bin''', '''safety_checker/model.safetensors''', '''vae/diffusion_pytorch_model.bin''', '''vae/diffusion_pytorch_model.safetensors''', '''text_encoder/pytorch_model.bin''', # Removed: 'text_encoder/model.safetensors', '''unet/diffusion_pytorch_model.bin''', '''unet/diffusion_pytorch_model.safetensors''', ] self.assertFalse(is_safetensors_compatible(lowercase_)) def _SCREAMING_SNAKE_CASE ( self : List[Any]): '''simple docstring''' SCREAMING_SNAKE_CASE_ : str = [ '''safety_checker/pytorch_model.fp16.bin''', '''safety_checker/model.fp16.safetensors''', '''vae/diffusion_pytorch_model.fp16.bin''', '''vae/diffusion_pytorch_model.fp16.safetensors''', '''text_encoder/pytorch_model.fp16.bin''', '''text_encoder/model.fp16.safetensors''', '''unet/diffusion_pytorch_model.fp16.bin''', '''unet/diffusion_pytorch_model.fp16.safetensors''', ] SCREAMING_SNAKE_CASE_ : Any = '''fp16''' self.assertTrue(is_safetensors_compatible(lowercase_ , variant=lowercase_)) def _SCREAMING_SNAKE_CASE ( self : str): '''simple docstring''' SCREAMING_SNAKE_CASE_ : Optional[int] = [ '''unet/diffusion_pytorch_model.fp16.bin''', '''unet/diffusion_pytorch_model.fp16.safetensors''', ] SCREAMING_SNAKE_CASE_ : Dict = '''fp16''' self.assertTrue(is_safetensors_compatible(lowercase_ , variant=lowercase_)) def _SCREAMING_SNAKE_CASE ( self : int): '''simple docstring''' SCREAMING_SNAKE_CASE_ : List[str] = [ '''unet/diffusion_pytorch_model.bin''', '''unet/diffusion_pytorch_model.safetensors''', ] SCREAMING_SNAKE_CASE_ : Any = '''fp16''' self.assertTrue(is_safetensors_compatible(lowercase_ , variant=lowercase_)) def _SCREAMING_SNAKE_CASE ( self : Union[str, Any]): '''simple docstring''' SCREAMING_SNAKE_CASE_ : Tuple = [ '''safety_checker/pytorch_model.fp16.bin''', '''safety_checker/model.fp16.safetensors''', '''vae/diffusion_pytorch_model.fp16.bin''', '''vae/diffusion_pytorch_model.fp16.safetensors''', '''text_encoder/pytorch_model.fp16.bin''', '''text_encoder/model.fp16.safetensors''', '''unet/diffusion_pytorch_model.fp16.bin''', # Removed: 'unet/diffusion_pytorch_model.fp16.safetensors', ] SCREAMING_SNAKE_CASE_ : List[Any] = '''fp16''' self.assertFalse(is_safetensors_compatible(lowercase_ , variant=lowercase_)) def _SCREAMING_SNAKE_CASE ( self : List[Any]): '''simple docstring''' SCREAMING_SNAKE_CASE_ : List[Any] = [ '''text_encoder/pytorch_model.fp16.bin''', '''text_encoder/model.fp16.safetensors''', ] SCREAMING_SNAKE_CASE_ : Any = '''fp16''' self.assertTrue(is_safetensors_compatible(lowercase_ , variant=lowercase_)) def _SCREAMING_SNAKE_CASE ( self : List[str]): '''simple docstring''' SCREAMING_SNAKE_CASE_ : Optional[Any] = [ '''text_encoder/pytorch_model.bin''', '''text_encoder/model.safetensors''', ] SCREAMING_SNAKE_CASE_ : List[Any] = '''fp16''' self.assertTrue(is_safetensors_compatible(lowercase_ , variant=lowercase_)) def _SCREAMING_SNAKE_CASE ( self : Optional[Any]): '''simple docstring''' SCREAMING_SNAKE_CASE_ : Optional[int] = [ '''safety_checker/pytorch_model.fp16.bin''', '''safety_checker/model.fp16.safetensors''', '''vae/diffusion_pytorch_model.fp16.bin''', '''vae/diffusion_pytorch_model.fp16.safetensors''', '''text_encoder/pytorch_model.fp16.bin''', # 'text_encoder/model.fp16.safetensors', '''unet/diffusion_pytorch_model.fp16.bin''', '''unet/diffusion_pytorch_model.fp16.safetensors''', ] SCREAMING_SNAKE_CASE_ : str = '''fp16''' self.assertFalse(is_safetensors_compatible(lowercase_ , variant=lowercase_))
91
1
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_sentencepiece_available, is_tokenizers_available, is_torch_available, ) __snake_case ={"""configuration_plbart""": ["""PLBART_PRETRAINED_CONFIG_ARCHIVE_MAP""", """PLBartConfig"""]} try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __snake_case =["""PLBartTokenizer"""] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __snake_case =[ """PLBART_PRETRAINED_MODEL_ARCHIVE_LIST""", """PLBartForCausalLM""", """PLBartForConditionalGeneration""", """PLBartForSequenceClassification""", """PLBartModel""", """PLBartPreTrainedModel""", ] if TYPE_CHECKING: from .configuration_plbart import PLBART_PRETRAINED_CONFIG_ARCHIVE_MAP, PLBartConfig try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_plbart import PLBartTokenizer try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_plbart import ( PLBART_PRETRAINED_MODEL_ARCHIVE_LIST, PLBartForCausalLM, PLBartForConditionalGeneration, PLBartForSequenceClassification, PLBartModel, PLBartPreTrainedModel, ) else: import sys __snake_case =_LazyModule(__name__, globals()["""__file__"""], _import_structure)
368
'''simple docstring''' import importlib import sys from argparse import REMAINDER, ArgumentParser from pathlib import Path import torch_xla.distributed.xla_multiprocessing as xmp def a_ ( ): lowerCAmelCase = ArgumentParser( description=( 'PyTorch TPU distributed training launch helper utility that will spawn up multiple distributed processes' ) ) # Optional arguments for the launch helper parser.add_argument('--num_cores' , type=lowerCamelCase , default=1 , help='Number of TPU cores to use (1 or 8).' ) # positional parser.add_argument( 'training_script' , type=lowerCamelCase , help=( 'The full path to the single TPU training ' 'program/script to be launched in parallel, ' 'followed by all the arguments for the ' 'training script' ) , ) # rest from the training program parser.add_argument('training_script_args' , nargs=lowerCamelCase ) return parser.parse_args() def a_ ( ): lowerCAmelCase = parse_args() # Import training_script as a module. lowerCAmelCase = Path(args.training_script ) sys.path.append(str(script_fpath.parent.resolve() ) ) lowerCAmelCase = script_fpath.stem lowerCAmelCase = importlib.import_module(lowerCamelCase ) # Patch sys.argv lowerCAmelCase = [args.training_script] + args.training_script_args + ['--tpu_num_cores', str(args.num_cores )] xmp.spawn(mod._mp_fn , args=() , nprocs=args.num_cores ) if __name__ == "__main__": main()
55
0
import math snake_case_ = 10 snake_case_ = 7 snake_case_ = BALLS_PER_COLOUR * NUM_COLOURS def lowerCamelCase__ ( snake_case_ : int = 20 ) -> str: __snake_case = math.comb(snake_case_ , snake_case_ ) __snake_case = math.comb(NUM_BALLS - BALLS_PER_COLOUR , snake_case_ ) __snake_case = NUM_COLOURS * (1 - missing_colour / total) return f"""{result:.9f}""" if __name__ == "__main__": print(solution(20))
24
from datasets.utils.patching import _PatchedModuleObj, patch_submodule from . import _test_patching def lowerCamelCase__ ( ) -> Any: import os as original_os from os import path as original_path from os import rename as original_rename from os.path import dirname as original_dirname from os.path import join as original_join assert _test_patching.os is original_os assert _test_patching.path is original_path assert _test_patching.join is original_join assert _test_patching.renamed_os is original_os assert _test_patching.renamed_path is original_path assert _test_patching.renamed_join is original_join __snake_case = '''__test_patch_submodule_mock__''' with patch_submodule(_test_patching , '''os.path.join''' , snake_case_ ): # Every way to access os.path.join must be patched, and the rest must stay untouched # check os.path.join assert isinstance(_test_patching.os , _PatchedModuleObj ) assert isinstance(_test_patching.os.path , _PatchedModuleObj ) assert _test_patching.os.path.join is mock # check path.join assert isinstance(_test_patching.path , _PatchedModuleObj ) assert _test_patching.path.join is mock # check join assert _test_patching.join is mock # check that the other attributes are untouched assert _test_patching.os.rename is original_rename assert _test_patching.path.dirname is original_dirname assert _test_patching.os.path.dirname is original_dirname # Even renamed modules or objects must be patched # check renamed_os.path.join assert isinstance(_test_patching.renamed_os , _PatchedModuleObj ) assert isinstance(_test_patching.renamed_os.path , _PatchedModuleObj ) assert _test_patching.renamed_os.path.join is mock # check renamed_path.join assert isinstance(_test_patching.renamed_path , _PatchedModuleObj ) assert _test_patching.renamed_path.join is mock # check renamed_join assert _test_patching.renamed_join is mock # check that the other attributes are untouched assert _test_patching.renamed_os.rename is original_rename assert _test_patching.renamed_path.dirname is original_dirname assert _test_patching.renamed_os.path.dirname is original_dirname # check that everthing is back to normal when the patch is over assert _test_patching.os is original_os assert _test_patching.path is original_path assert _test_patching.join is original_join assert _test_patching.renamed_os is original_os assert _test_patching.renamed_path is original_path assert _test_patching.renamed_join is original_join def lowerCamelCase__ ( ) -> Any: assert _test_patching.open is open __snake_case = '''__test_patch_submodule_builtin_mock__''' # _test_patching has "open" in its globals assert _test_patching.open is open with patch_submodule(_test_patching , '''open''' , snake_case_ ): assert _test_patching.open is mock # check that everthing is back to normal when the patch is over assert _test_patching.open is open def lowerCamelCase__ ( ) -> List[str]: # pandas.read_csv is not present in _test_patching __snake_case = '''__test_patch_submodule_missing_mock__''' with patch_submodule(_test_patching , '''pandas.read_csv''' , snake_case_ ): pass def lowerCamelCase__ ( ) -> Union[str, Any]: # builtin should always be mocked even if they're not in the globals # in case they're loaded at one point __snake_case = '''__test_patch_submodule_missing_builtin_mock__''' # _test_patching doesn't have "len" in its globals assert getattr(_test_patching , '''len''' , snake_case_ ) is None with patch_submodule(_test_patching , '''len''' , snake_case_ ): assert _test_patching.len is mock assert _test_patching.len is len def lowerCamelCase__ ( ) -> Union[str, Any]: __snake_case = '''__test_patch_submodule_start_and_stop_mock__''' __snake_case = patch_submodule(_test_patching , '''open''' , snake_case_ ) assert _test_patching.open is open patch.start() assert _test_patching.open is mock patch.stop() assert _test_patching.open is open def lowerCamelCase__ ( ) -> Optional[int]: from os import rename as original_rename from os.path import dirname as original_dirname from os.path import join as original_join __snake_case = '''__test_patch_submodule_successive_join__''' __snake_case = '''__test_patch_submodule_successive_dirname__''' __snake_case = '''__test_patch_submodule_successive_rename__''' assert _test_patching.os.path.join is original_join assert _test_patching.os.path.dirname is original_dirname assert _test_patching.os.rename is original_rename with patch_submodule(_test_patching , '''os.path.join''' , snake_case_ ): with patch_submodule(_test_patching , '''os.rename''' , snake_case_ ): with patch_submodule(_test_patching , '''os.path.dirname''' , snake_case_ ): assert _test_patching.os.path.join is mock_join assert _test_patching.os.path.dirname is mock_dirname assert _test_patching.os.rename is mock_rename # try another order with patch_submodule(_test_patching , '''os.rename''' , snake_case_ ): with patch_submodule(_test_patching , '''os.path.join''' , snake_case_ ): with patch_submodule(_test_patching , '''os.path.dirname''' , snake_case_ ): assert _test_patching.os.path.join is mock_join assert _test_patching.os.path.dirname is mock_dirname assert _test_patching.os.rename is mock_rename assert _test_patching.os.path.join is original_join assert _test_patching.os.path.dirname is original_dirname assert _test_patching.os.rename is original_rename def lowerCamelCase__ ( ) -> Tuple: __snake_case = '''__test_patch_submodule_doesnt_exist_mock__''' with patch_submodule(_test_patching , '''__module_that_doesn_exist__.__attribute_that_doesn_exist__''' , snake_case_ ): pass with patch_submodule(_test_patching , '''os.__attribute_that_doesn_exist__''' , snake_case_ ): pass
24
1
from __future__ import annotations lowercase : Optional[int] = tuple[int, int, int] lowercase : Any = tuple[str, str, str] # used alphabet -------------------------- # from string.ascii_uppercase lowercase : Any = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' # -------------------------- default selection -------------------------- # rotors -------------------------- lowercase : Any = 'EGZWVONAHDCLFQMSIPJBYUKXTR' lowercase : Optional[Any] = 'FOBHMDKEXQNRAULPGSJVTYICZW' lowercase : Any = 'ZJXESIUQLHAVRMDOYGTNFWPBKC' # reflector -------------------------- lowercase : Any = { 'A': 'N', 'N': 'A', 'B': 'O', 'O': 'B', 'C': 'P', 'P': 'C', 'D': 'Q', 'Q': 'D', 'E': 'R', 'R': 'E', 'F': 'S', 'S': 'F', 'G': 'T', 'T': 'G', 'H': 'U', 'U': 'H', 'I': 'V', 'V': 'I', 'J': 'W', 'W': 'J', 'K': 'X', 'X': 'K', 'L': 'Y', 'Y': 'L', 'M': 'Z', 'Z': 'M', } # -------------------------- extra rotors -------------------------- lowercase : Tuple = 'RMDJXFUWGISLHVTCQNKYPBEZOA' lowercase : List[Any] = 'SGLCPQWZHKXAREONTFBVIYJUDM' lowercase : Union[str, Any] = 'HVSICLTYKQUBXDWAJZOMFGPREN' lowercase : str = 'RZWQHFMVDBKICJLNTUXAGYPSOE' lowercase : Tuple = 'LFKIJODBEGAMQPXVUHYSTCZRWN' lowercase : List[Any] = 'KOAEGVDHXPQZMLFTYWJNBRCIUS' def _SCREAMING_SNAKE_CASE ( _lowerCamelCase : RotorPositionT , _lowerCamelCase : RotorSelectionT , _lowerCamelCase : str) -> tuple[RotorPositionT, RotorSelectionT, dict[str, str]]: '''simple docstring''' if (unique_rotsel := len(set(_lowerCamelCase))) < 3: __UpperCamelCase : Tuple = F'Please use 3 unique rotors (not {unique_rotsel})' raise Exception(_lowerCamelCase) # Checks if rotor positions are valid __UpperCamelCase , __UpperCamelCase , __UpperCamelCase : Optional[Any] = rotpos if not 0 < rotorposa <= len(_lowerCamelCase): __UpperCamelCase : Union[str, Any] = F'First rotor position is not within range of 1..26 ({rotorposa}' raise ValueError(_lowerCamelCase) if not 0 < rotorposa <= len(_lowerCamelCase): __UpperCamelCase : str = F'Second rotor position is not within range of 1..26 ({rotorposa})' raise ValueError(_lowerCamelCase) if not 0 < rotorposa <= len(_lowerCamelCase): __UpperCamelCase : Dict = F'Third rotor position is not within range of 1..26 ({rotorposa})' raise ValueError(_lowerCamelCase) # Validates string and returns dict __UpperCamelCase : List[str] = _plugboard(_lowerCamelCase) return rotpos, rotsel, pbdict def _SCREAMING_SNAKE_CASE ( _lowerCamelCase : str) -> dict[str, str]: '''simple docstring''' if not isinstance(_lowerCamelCase , _lowerCamelCase): __UpperCamelCase : Any = F'Plugboard setting isn\'t type string ({type(_lowerCamelCase)})' raise TypeError(_lowerCamelCase) elif len(_lowerCamelCase) % 2 != 0: __UpperCamelCase : Union[str, Any] = F'Odd number of symbols ({len(_lowerCamelCase)})' raise Exception(_lowerCamelCase) elif pbstring == "": return {} pbstring.replace(" " , "") # Checks if all characters are unique __UpperCamelCase : Dict = set() for i in pbstring: if i not in abc: __UpperCamelCase : Optional[Any] = F'\'{i}\' not in list of symbols' raise Exception(_lowerCamelCase) elif i in tmppbl: __UpperCamelCase : List[str] = F'Duplicate symbol ({i})' raise Exception(_lowerCamelCase) else: tmppbl.add(_lowerCamelCase) del tmppbl # Created the dictionary __UpperCamelCase : int = {} for j in range(0 , len(_lowerCamelCase) - 1 , 2): __UpperCamelCase : Any = pbstring[j + 1] __UpperCamelCase : Dict = pbstring[j] return pb def _SCREAMING_SNAKE_CASE ( _lowerCamelCase : str , _lowerCamelCase : RotorPositionT , _lowerCamelCase : RotorSelectionT = (rotora, rotora, rotora) , _lowerCamelCase : str = "" , ) -> str: '''simple docstring''' __UpperCamelCase : Optional[int] = text.upper() __UpperCamelCase , __UpperCamelCase , __UpperCamelCase : str = _validator( _lowerCamelCase , _lowerCamelCase , plugb.upper()) __UpperCamelCase , __UpperCamelCase , __UpperCamelCase : List[str] = rotor_position __UpperCamelCase , __UpperCamelCase , __UpperCamelCase : List[str] = rotor_selection rotorposa -= 1 rotorposa -= 1 rotorposa -= 1 __UpperCamelCase : Dict = [] # encryption/decryption process -------------------------- for symbol in text: if symbol in abc: # 1st plugboard -------------------------- if symbol in plugboard: __UpperCamelCase : Optional[int] = plugboard[symbol] # rotor ra -------------------------- __UpperCamelCase : str = abc.index(_lowerCamelCase) + rotorposa __UpperCamelCase : Optional[int] = rotora[index % len(_lowerCamelCase)] # rotor rb -------------------------- __UpperCamelCase : Any = abc.index(_lowerCamelCase) + rotorposa __UpperCamelCase : Dict = rotora[index % len(_lowerCamelCase)] # rotor rc -------------------------- __UpperCamelCase : Tuple = abc.index(_lowerCamelCase) + rotorposa __UpperCamelCase : int = rotora[index % len(_lowerCamelCase)] # reflector -------------------------- # this is the reason you don't need another machine to decipher __UpperCamelCase : Dict = reflector[symbol] # 2nd rotors __UpperCamelCase : List[str] = abc[rotora.index(_lowerCamelCase) - rotorposa] __UpperCamelCase : Optional[int] = abc[rotora.index(_lowerCamelCase) - rotorposa] __UpperCamelCase : Union[str, Any] = abc[rotora.index(_lowerCamelCase) - rotorposa] # 2nd plugboard if symbol in plugboard: __UpperCamelCase : Optional[int] = plugboard[symbol] # moves/resets rotor positions rotorposa += 1 if rotorposa >= len(_lowerCamelCase): __UpperCamelCase : Dict = 0 rotorposa += 1 if rotorposa >= len(_lowerCamelCase): __UpperCamelCase : Tuple = 0 rotorposa += 1 if rotorposa >= len(_lowerCamelCase): __UpperCamelCase : Tuple = 0 # else: # pass # Error could be also raised # raise ValueError( # 'Invalid symbol('+repr(symbol)+')') result.append(_lowerCamelCase) return "".join(_lowerCamelCase) if __name__ == "__main__": lowercase : Dict = 'This is my Python script that emulates the Enigma machine from WWII.' lowercase : Optional[Any] = (1, 1, 1) lowercase : Optional[Any] = 'pictures' lowercase : List[Any] = (rotora, rotora, rotora) lowercase : Dict = enigma(message, rotor_pos, rotor_sel, pb) print('Encrypted message:', en) print('Decrypted message:', enigma(en, rotor_pos, rotor_sel, pb))
151
from typing import TYPE_CHECKING # rely on isort to merge the imports from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available lowercase : Union[str, Any] = { 'configuration_informer': [ 'INFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP', 'InformerConfig', ], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase : List[Any] = [ 'INFORMER_PRETRAINED_MODEL_ARCHIVE_LIST', 'InformerForPrediction', 'InformerModel', 'InformerPreTrainedModel', ] if TYPE_CHECKING: from .configuration_informer import INFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP, InformerConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_informer import ( INFORMER_PRETRAINED_MODEL_ARCHIVE_LIST, InformerForPrediction, InformerModel, InformerPreTrainedModel, ) else: import sys lowercase : Optional[int] = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
151
1
'''simple docstring''' from __future__ import annotations from collections import namedtuple from dataclasses import dataclass @dataclass class __lowerCAmelCase : """simple docstring""" _snake_case : Any = 4_2 _snake_case : Optional[Any] = None _snake_case : Any = None lowercase__ : Union[str, Any] = namedtuple('CoinsDistribResult', 'moves excess') def a__ ( lowercase : str ) -> int: """simple docstring""" if root is None: return 0 # Validation def count_nodes(lowercase : Tuple ) -> int: if node is None: return 0 return count_nodes(node.left ) + count_nodes(node.right ) + 1 def count_coins(lowercase : Any ) -> int: if node is None: return 0 return count_coins(node.left ) + count_coins(node.right ) + node.data if count_nodes(UpperCamelCase__ ) != count_coins(UpperCamelCase__ ): raise ValueError('''The nodes number should be same as the number of coins''' ) # Main calculation def get_distrib(lowercase : str ) -> CoinsDistribResult: if node is None: return CoinsDistribResult(0, 1 ) _UpperCamelCase , _UpperCamelCase = get_distrib(node.left ) _UpperCamelCase , _UpperCamelCase = get_distrib(node.right ) _UpperCamelCase = 1 - left_distrib_excess _UpperCamelCase = 1 - right_distrib_excess _UpperCamelCase = ( left_distrib_moves + right_distrib_moves + abs(UpperCamelCase__ ) + abs(UpperCamelCase__ ) ) _UpperCamelCase = node.data - coins_to_left - coins_to_right return CoinsDistribResult(UpperCamelCase__, UpperCamelCase__ ) return get_distrib(UpperCamelCase__ )[0] if __name__ == "__main__": import doctest doctest.testmod()
324
from __future__ import annotations import unittest from transformers import AutoTokenizer, MBartConfig, 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, TFMBartForConditionalGeneration, TFMBartModel @require_tf class A_ : UpperCAmelCase__ = MBartConfig UpperCAmelCase__ = {} UpperCAmelCase__ = '''gelu''' def __init__( self , _A , _A=1_3 , _A=7 , _A=True , _A=False , _A=9_9 , _A=3_2 , _A=2 , _A=4 , _A=3_7 , _A=0.1 , _A=0.1 , _A=2_0 , _A=2 , _A=1 , _A=0 , ): '''simple docstring''' UpperCAmelCase = parent UpperCAmelCase = batch_size UpperCAmelCase = seq_length UpperCAmelCase = is_training UpperCAmelCase = use_labels UpperCAmelCase = vocab_size UpperCAmelCase = hidden_size UpperCAmelCase = num_hidden_layers UpperCAmelCase = num_attention_heads UpperCAmelCase = intermediate_size UpperCAmelCase = hidden_dropout_prob UpperCAmelCase = attention_probs_dropout_prob UpperCAmelCase = max_position_embeddings UpperCAmelCase = eos_token_id UpperCAmelCase = pad_token_id UpperCAmelCase = bos_token_id def _lowercase ( self ): '''simple docstring''' UpperCAmelCase = ids_tensor([self.batch_size, self.seq_length - 1] , self.vocab_size ) UpperCAmelCase = tf.expand_dims(tf.constant([self.eos_token_id] * self.batch_size ) , 1 ) UpperCAmelCase = tf.concat([input_ids, eos_tensor] , axis=1 ) UpperCAmelCase = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) UpperCAmelCase = 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 , ) UpperCAmelCase = prepare_mbart_inputs_dict(_A , _A , _A ) return config, inputs_dict def _lowercase ( self , _A , _A ): '''simple docstring''' UpperCAmelCase = TFMBartModel(config=_A ).get_decoder() UpperCAmelCase = inputs_dict['''input_ids'''] UpperCAmelCase = input_ids[:1, :] UpperCAmelCase = inputs_dict['''attention_mask'''][:1, :] UpperCAmelCase = inputs_dict['''head_mask'''] UpperCAmelCase = 1 # first forward pass UpperCAmelCase = model(_A , attention_mask=_A , head_mask=_A , use_cache=_A ) UpperCAmelCase , UpperCAmelCase = outputs.to_tuple() UpperCAmelCase = past_key_values[1] def __SCREAMING_SNAKE_CASE ( UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__=None , UpperCamelCase__=None , UpperCamelCase__=None , UpperCamelCase__=None , UpperCamelCase__=None , ) -> List[str]: '''simple docstring''' if attention_mask is None: UpperCAmelCase = tf.cast(tf.math.not_equal(UpperCamelCase__ , config.pad_token_id ) , tf.inta ) if decoder_attention_mask is None: UpperCAmelCase = 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: UpperCAmelCase = tf.ones((config.encoder_layers, config.encoder_attention_heads) ) if decoder_head_mask is None: UpperCAmelCase = tf.ones((config.decoder_layers, config.decoder_attention_heads) ) if cross_attn_head_mask is None: UpperCAmelCase = 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 A_ (a_ , a_ , unittest.TestCase ): UpperCAmelCase__ = (TFMBartForConditionalGeneration, TFMBartModel) if is_tf_available() else () UpperCAmelCase__ = (TFMBartForConditionalGeneration,) if is_tf_available() else () UpperCAmelCase__ = ( { '''conversational''': TFMBartForConditionalGeneration, '''feature-extraction''': TFMBartModel, '''summarization''': TFMBartForConditionalGeneration, '''text2text-generation''': TFMBartForConditionalGeneration, '''translation''': TFMBartForConditionalGeneration, } if is_tf_available() else {} ) UpperCAmelCase__ = True UpperCAmelCase__ = False UpperCAmelCase__ = False def _lowercase ( self , _A , _A , _A , _A , _A ): '''simple docstring''' if pipeline_test_casse_name != "FeatureExtractionPipelineTests": # Exception encountered when calling layer '...' return True return False def _lowercase ( self ): '''simple docstring''' UpperCAmelCase = TFMBartModelTester(self ) UpperCAmelCase = ConfigTester(self , config_class=_A ) def _lowercase ( self ): '''simple docstring''' self.config_tester.run_common_tests() def _lowercase ( self ): '''simple docstring''' UpperCAmelCase = self.model_tester.prepare_config_and_inputs_for_common() self.model_tester.check_decoder_model_past_large_inputs(*_A ) @require_sentencepiece @require_tokenizers @require_tf class A_ (unittest.TestCase ): UpperCAmelCase__ = [ ''' UN Chief Says There Is No Military Solution in Syria''', ] UpperCAmelCase__ = [ '''Şeful ONU declară că nu există o soluţie militară în Siria''', ] UpperCAmelCase__ = '''facebook/mbart-large-en-ro''' @cached_property def _lowercase ( self ): '''simple docstring''' return AutoTokenizer.from_pretrained(self.model_name ) @cached_property def _lowercase ( self ): '''simple docstring''' UpperCAmelCase = TFAutoModelForSeqaSeqLM.from_pretrained(self.model_name ) return model def _lowercase ( self , **_A ): '''simple docstring''' UpperCAmelCase = self.translate_src_text(**_A ) self.assertListEqual(self.expected_text , _A ) def _lowercase ( self , **_A ): '''simple docstring''' UpperCAmelCase = self.tokenizer(self.src_text , **_A , return_tensors='''tf''' ) UpperCAmelCase = self.model.generate( model_inputs.input_ids , attention_mask=model_inputs.attention_mask , num_beams=2 ) UpperCAmelCase = self.tokenizer.batch_decode(_A , skip_special_tokens=_A ) return generated_words @slow def _lowercase ( self ): '''simple docstring''' self._assert_generated_batch_equal_expected()
273
0
from math import pow, sqrt def UpperCAmelCase__ ( *lowerCamelCase ): lowercase :int = len(lowerCamelCase ) > 0 and all(value > 0.0 for value in values ) return result def UpperCAmelCase__ ( lowerCamelCase, lowerCamelCase ): return ( round(sqrt(molar_mass_a / molar_mass_a ), 6 ) if validate(lowerCamelCase, lowerCamelCase ) else ValueError("Input Error: Molar mass values must greater than 0." ) ) def UpperCAmelCase__ ( lowerCamelCase, lowerCamelCase, lowerCamelCase ): return ( round(effusion_rate * sqrt(molar_mass_a / molar_mass_a ), 6 ) if validate(lowerCamelCase, lowerCamelCase, lowerCamelCase ) else ValueError( "Input Error: Molar mass and effusion rate values must greater than 0." ) ) def UpperCAmelCase__ ( lowerCamelCase, lowerCamelCase, lowerCamelCase ): return ( round(effusion_rate / sqrt(molar_mass_a / molar_mass_a ), 6 ) if validate(lowerCamelCase, lowerCamelCase, lowerCamelCase ) else ValueError( "Input Error: Molar mass and effusion rate values must greater than 0." ) ) def UpperCAmelCase__ ( lowerCamelCase, lowerCamelCase, lowerCamelCase ): return ( round(molar_mass / pow(effusion_rate_a / effusion_rate_a, 2 ), 6 ) if validate(lowerCamelCase, lowerCamelCase, lowerCamelCase ) else ValueError( "Input Error: Molar mass and effusion rate values must greater than 0." ) ) def UpperCAmelCase__ ( lowerCamelCase, lowerCamelCase, lowerCamelCase ): return ( round(pow(effusion_rate_a / effusion_rate_a, 2 ) / molar_mass, 6 ) if validate(lowerCamelCase, lowerCamelCase, lowerCamelCase ) else ValueError( "Input Error: Molar mass and effusion rate values must greater than 0." ) )
158
from . import __version__ # Backward compatibility imports, to make sure all those objects can be found in file_utils from .utils import ( CLOUDFRONT_DISTRIB_PREFIX, CONFIG_NAME, DISABLE_TELEMETRY, DUMMY_INPUTS, DUMMY_MASK, ENV_VARS_TRUE_AND_AUTO_VALUES, ENV_VARS_TRUE_VALUES, FEATURE_EXTRACTOR_NAME, FLAX_WEIGHTS_NAME, HF_MODULES_CACHE, HUGGINGFACE_CO_PREFIX, HUGGINGFACE_CO_RESOLVE_ENDPOINT, MODEL_CARD_NAME, MULTIPLE_CHOICE_DUMMY_INPUTS, PYTORCH_PRETRAINED_BERT_CACHE, PYTORCH_TRANSFORMERS_CACHE, S3_BUCKET_PREFIX, SENTENCEPIECE_UNDERLINE, SPIECE_UNDERLINE, TF2_WEIGHTS_NAME, TF_WEIGHTS_NAME, TORCH_FX_REQUIRED_VERSION, TRANSFORMERS_CACHE, TRANSFORMERS_DYNAMIC_MODULE_NAME, USE_JAX, USE_TF, USE_TORCH, WEIGHTS_INDEX_NAME, WEIGHTS_NAME, ContextManagers, DummyObject, EntryNotFoundError, ExplicitEnum, ModelOutput, PaddingStrategy, PushToHubMixin, RepositoryNotFoundError, RevisionNotFoundError, TensorType, _LazyModule, add_code_sample_docstrings, add_end_docstrings, add_start_docstrings, add_start_docstrings_to_model_forward, cached_property, copy_func, default_cache_path, define_sagemaker_information, get_cached_models, get_file_from_repo, get_full_repo_name, get_torch_version, has_file, http_user_agent, is_apex_available, is_bsa_available, is_coloredlogs_available, is_datasets_available, is_detectrona_available, is_faiss_available, is_flax_available, is_ftfy_available, is_in_notebook, is_ipex_available, is_librosa_available, is_offline_mode, is_onnx_available, is_pandas_available, is_phonemizer_available, is_protobuf_available, is_psutil_available, is_pyanvml_available, is_pyctcdecode_available, is_pytesseract_available, is_pytorch_quantization_available, is_rjieba_available, is_sagemaker_dp_enabled, is_sagemaker_mp_enabled, is_scipy_available, is_sentencepiece_available, is_seqio_available, is_sklearn_available, is_soundfile_availble, is_spacy_available, is_speech_available, is_tensor, is_tensorflow_probability_available, is_tfaonnx_available, is_tf_available, is_timm_available, is_tokenizers_available, is_torch_available, is_torch_bfaa_available, is_torch_cuda_available, is_torch_fx_available, is_torch_fx_proxy, is_torch_mps_available, is_torch_tfaa_available, is_torch_tpu_available, is_torchaudio_available, is_training_run_on_sagemaker, is_vision_available, replace_return_docstrings, requires_backends, to_numpy, to_py_obj, torch_only_method, )
158
1
"""simple docstring""" def lowercase ( lowerCAmelCase__ : float ) -> float: return 10 - x * x def lowercase ( lowerCAmelCase__ : float , lowerCAmelCase__ : float ) -> float: # Bolzano theory in order to find if there is a root between a and b if equation(lowerCAmelCase__ ) * equation(lowerCAmelCase__ ) >= 0: raise ValueError('''Wrong space!''' ) __a = a while (b - a) >= 0.01: # Find middle point __a = (a + b) / 2 # Check if middle point is root if equation(lowerCAmelCase__ ) == 0.0: break # Decide the side to repeat the steps if equation(lowerCAmelCase__ ) * equation(lowerCAmelCase__ ) < 0: __a = c else: __a = c return c if __name__ == "__main__": import doctest doctest.testmod() print(bisection(-2, 5)) print(bisection(0, 6))
45
"""simple docstring""" from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_tf_available, is_tokenizers_available, is_torch_available, ) lowercase_ = { "configuration_roformer": ["ROFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP", "RoFormerConfig", "RoFormerOnnxConfig"], "tokenization_roformer": ["RoFormerTokenizer"], } try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase_ = ["RoFormerTokenizerFast"] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase_ = [ "ROFORMER_PRETRAINED_MODEL_ARCHIVE_LIST", "RoFormerForCausalLM", "RoFormerForMaskedLM", "RoFormerForMultipleChoice", "RoFormerForQuestionAnswering", "RoFormerForSequenceClassification", "RoFormerForTokenClassification", "RoFormerLayer", "RoFormerModel", "RoFormerPreTrainedModel", "load_tf_weights_in_roformer", ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase_ = [ "TF_ROFORMER_PRETRAINED_MODEL_ARCHIVE_LIST", "TFRoFormerForCausalLM", "TFRoFormerForMaskedLM", "TFRoFormerForMultipleChoice", "TFRoFormerForQuestionAnswering", "TFRoFormerForSequenceClassification", "TFRoFormerForTokenClassification", "TFRoFormerLayer", "TFRoFormerModel", "TFRoFormerPreTrainedModel", ] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase_ = [ "FLAX_ROFORMER_PRETRAINED_MODEL_ARCHIVE_LIST", "FlaxRoFormerForMaskedLM", "FlaxRoFormerForMultipleChoice", "FlaxRoFormerForQuestionAnswering", "FlaxRoFormerForSequenceClassification", "FlaxRoFormerForTokenClassification", "FlaxRoFormerModel", "FlaxRoFormerPreTrainedModel", ] if TYPE_CHECKING: from .configuration_roformer import ROFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP, RoFormerConfig, RoFormerOnnxConfig from .tokenization_roformer import RoFormerTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_roformer_fast import RoFormerTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_roformer import ( ROFORMER_PRETRAINED_MODEL_ARCHIVE_LIST, RoFormerForCausalLM, RoFormerForMaskedLM, RoFormerForMultipleChoice, RoFormerForQuestionAnswering, RoFormerForSequenceClassification, RoFormerForTokenClassification, RoFormerLayer, RoFormerModel, RoFormerPreTrainedModel, load_tf_weights_in_roformer, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_roformer import ( TF_ROFORMER_PRETRAINED_MODEL_ARCHIVE_LIST, TFRoFormerForCausalLM, TFRoFormerForMaskedLM, TFRoFormerForMultipleChoice, TFRoFormerForQuestionAnswering, TFRoFormerForSequenceClassification, TFRoFormerForTokenClassification, TFRoFormerLayer, TFRoFormerModel, TFRoFormerPreTrainedModel, ) try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_flax_roformer import ( FLAX_ROFORMER_PRETRAINED_MODEL_ARCHIVE_LIST, FlaxRoFormerForMaskedLM, FlaxRoFormerForMultipleChoice, FlaxRoFormerForQuestionAnswering, FlaxRoFormerForSequenceClassification, FlaxRoFormerForTokenClassification, FlaxRoFormerModel, FlaxRoFormerPreTrainedModel, ) else: import sys lowercase_ = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
45
1
from ....utils import logging UpperCamelCase__ = logging.get_logger(__name__) class __SCREAMING_SNAKE_CASE ( _UpperCAmelCase ): def __init__( self , __lowerCAmelCase , __lowerCAmelCase=None , __lowerCAmelCase=2048 ): UpperCamelCase__ = config.__dict__ UpperCamelCase__ = modal_hidden_size if num_labels: UpperCamelCase__ = num_labels
352
import warnings from ...configuration_utils import PretrainedConfig from ...utils import logging UpperCamelCase__ = logging.get_logger(__name__) UpperCamelCase__ = { "xlnet-base-cased": "https://huggingface.co/xlnet-base-cased/resolve/main/config.json", "xlnet-large-cased": "https://huggingface.co/xlnet-large-cased/resolve/main/config.json", } class __SCREAMING_SNAKE_CASE ( _a ): snake_case : Any = """xlnet""" snake_case : Optional[Any] = ["""mems"""] snake_case : Any = { """n_token""": """vocab_size""", # Backward compatibility """hidden_size""": """d_model""", """num_attention_heads""": """n_head""", """num_hidden_layers""": """n_layer""", } def __init__( self , __lowerCAmelCase=32000 , __lowerCAmelCase=1024 , __lowerCAmelCase=24 , __lowerCAmelCase=16 , __lowerCAmelCase=4096 , __lowerCAmelCase="gelu" , __lowerCAmelCase=True , __lowerCAmelCase="bi" , __lowerCAmelCase=0.02 , __lowerCAmelCase=1E-12 , __lowerCAmelCase=0.1 , __lowerCAmelCase=512 , __lowerCAmelCase=None , __lowerCAmelCase=True , __lowerCAmelCase=False , __lowerCAmelCase=False , __lowerCAmelCase=-1 , __lowerCAmelCase=False , __lowerCAmelCase="last" , __lowerCAmelCase=True , __lowerCAmelCase="tanh" , __lowerCAmelCase=0.1 , __lowerCAmelCase=5 , __lowerCAmelCase=5 , __lowerCAmelCase=5 , __lowerCAmelCase=1 , __lowerCAmelCase=2 , **__lowerCAmelCase , ): UpperCamelCase__ = vocab_size UpperCamelCase__ = d_model UpperCamelCase__ = n_layer UpperCamelCase__ = n_head if d_model % n_head != 0: raise ValueError(f"""'d_model % n_head' ({d_model % n_head}) should be equal to 0""" ) if "d_head" in kwargs: if kwargs["d_head"] != d_model // n_head: raise ValueError( f"""`d_head` ({kwargs["d_head"]}) should be equal to `d_model // n_head` ({d_model // n_head})""" ) UpperCamelCase__ = d_model // n_head UpperCamelCase__ = ff_activation UpperCamelCase__ = d_inner UpperCamelCase__ = untie_r UpperCamelCase__ = attn_type UpperCamelCase__ = initializer_range UpperCamelCase__ = layer_norm_eps UpperCamelCase__ = dropout UpperCamelCase__ = mem_len UpperCamelCase__ = reuse_len UpperCamelCase__ = bi_data UpperCamelCase__ = clamp_len UpperCamelCase__ = same_length UpperCamelCase__ = summary_type UpperCamelCase__ = summary_use_proj UpperCamelCase__ = summary_activation UpperCamelCase__ = summary_last_dropout UpperCamelCase__ = start_n_top UpperCamelCase__ = end_n_top UpperCamelCase__ = bos_token_id UpperCamelCase__ = pad_token_id UpperCamelCase__ = eos_token_id if "use_cache" in kwargs: warnings.warn( """The `use_cache` argument is deprecated and will be removed in a future version, use `use_mems_eval`""" """ instead.""" , __lowerCAmelCase , ) UpperCamelCase__ = kwargs["""use_cache"""] UpperCamelCase__ = use_mems_eval UpperCamelCase__ = use_mems_train super().__init__(pad_token_id=__lowerCAmelCase , bos_token_id=__lowerCAmelCase , eos_token_id=__lowerCAmelCase , **__lowerCAmelCase ) @property def _lowerCamelCase ( self ): 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 , __lowerCAmelCase ): # Message copied from Transformer-XL documentation raise NotImplementedError( f"""The model {self.model_type} is one of the few models that has no sequence length limit.""" )
87
0