code
stringlengths
81
54k
code_codestyle
int64
0
721
style_context
stringlengths
91
41.9k
style_context_codestyle
int64
0
699
label
int64
0
1
'''simple docstring''' import json import os import unittest from transformers.models.gptsan_japanese.tokenization_gptsan_japanese import ( VOCAB_FILES_NAMES, GPTSanJapaneseTokenizer, ) from transformers.testing_utils import require_tokenizers, slow from ...test_tokenization_common import TokenizerTesterMixin @require_tokenizers class SCREAMING_SNAKE_CASE ( SCREAMING_SNAKE_CASE_ , unittest.TestCase ): snake_case__ = GPTSanJapaneseTokenizer snake_case__ = False snake_case__ = {"do_clean_text": False, "add_prefix_space": False} def SCREAMING_SNAKE_CASE ( self : Optional[Any] ) -> int: super().setUp() # fmt: off a_ : Union[str, Any] = ['''ใ“ใ‚“''', '''ใ“ใ‚“ใซ''', '''ใซใกใฏ''', '''ใฐใ‚“ใฏ''', '''ไธ–็•Œ,ใ”บ็•Œ''', '''ใ€''', '''ใ€‚''', '''<BR>''', '''<SP>''', '''<TAB>''', '''<URL>''', '''<EMAIL>''', '''<TEL>''', '''<DATE>''', '''<PRICE>''', '''<BLOCK>''', '''<KIGOU>''', '''<U2000U2BFF>''', '''<|emoji1|>''', '''<unk>''', '''<|bagoftoken|>''', '''<|endoftext|>'''] # fmt: on a_ : Any = {'''emoji''': {'''\ud83d\ude00''': '''<|emoji1|>'''}, '''emoji_inv''': {'''<|emoji1|>''': '''\ud83d\ude00'''}} # ๐Ÿ˜€ a_ : Dict = {'''unk_token''': '''<unk>'''} a_ : Union[str, Any] = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES['''vocab_file'''] ) a_ : List[str] = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES['''emoji_file'''] ) with open(self.vocab_file , '''w''' , encoding='''utf-8''' ) as vocab_writer: vocab_writer.write(''''''.join([x + '''\n''' for x in vocab_tokens] ) ) with open(self.emoji_file , '''w''' ) as emoji_writer: emoji_writer.write(json.dumps(__SCREAMING_SNAKE_CASE ) ) def SCREAMING_SNAKE_CASE ( self : Optional[int] , **__SCREAMING_SNAKE_CASE : Optional[Any] ) -> Tuple: kwargs.update(self.special_tokens_map ) return GPTSanJapaneseTokenizer.from_pretrained(self.tmpdirname , **__SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE ( self : Optional[Any] , __SCREAMING_SNAKE_CASE : Tuple ) -> Optional[Any]: a_ : Union[str, Any] = '''ใ“ใ‚“ใซใกใฏใ€ไธ–็•Œใ€‚ \nใ“ใ‚“ใฐใ‚“ใฏใ€ใ”บ็•Œใ€‚๐Ÿ˜€''' a_ : Dict = '''ใ“ใ‚“ใซใกใฏใ€ไธ–็•Œใ€‚ \nใ“ใ‚“ใฐใ‚“ใฏใ€ไธ–็•Œใ€‚๐Ÿ˜€''' return input_text, output_text def SCREAMING_SNAKE_CASE ( self : List[Any] , __SCREAMING_SNAKE_CASE : int ) -> Optional[int]: a_ : List[str] = self.get_input_output_texts(__SCREAMING_SNAKE_CASE ) a_ : List[str] = tokenizer.encode(__SCREAMING_SNAKE_CASE , add_special_tokens=__SCREAMING_SNAKE_CASE ) a_ : Optional[int] = tokenizer.decode(__SCREAMING_SNAKE_CASE , clean_up_tokenization_spaces=__SCREAMING_SNAKE_CASE ) return text, ids def SCREAMING_SNAKE_CASE ( self : List[str] ) -> Optional[int]: pass # TODO add if relevant def SCREAMING_SNAKE_CASE ( self : Dict ) -> int: pass # TODO add if relevant def SCREAMING_SNAKE_CASE ( self : Dict ) -> Tuple: pass # TODO add if relevant def SCREAMING_SNAKE_CASE ( self : Union[str, Any] ) -> str: a_ : List[str] = self.get_tokenizer() # Testing tokenization a_ : Tuple = '''ใ“ใ‚“ใซใกใฏใ€ไธ–็•Œใ€‚ใ€€ใ“ใ‚“ใฐใ‚“ใฏใ€ใ”บ็•Œใ€‚''' a_ : Any = ['''ใ“ใ‚“''', '''ใซใกใฏ''', '''ใ€''', '''ไธ–็•Œ''', '''ใ€‚''', '''<SP>''', '''ใ“ใ‚“''', '''ใฐใ‚“ใฏ''', '''ใ€''', '''ใ”บ็•Œ''', '''ใ€‚'''] a_ : Dict = tokenizer.tokenize(__SCREAMING_SNAKE_CASE ) self.assertListEqual(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) # Testing conversion to ids without special tokens a_ : Union[str, Any] = [0, 2, 5, 4, 6, 8, 0, 3, 5, 4, 6] a_ : Tuple = tokenizer.convert_tokens_to_ids(__SCREAMING_SNAKE_CASE ) self.assertListEqual(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) # Testing conversion to ids with special tokens a_ : Optional[int] = tokens + [tokenizer.unk_token] a_ : List[Any] = [0, 2, 5, 4, 6, 8, 0, 3, 5, 4, 6, 19] a_ : int = tokenizer.convert_tokens_to_ids(__SCREAMING_SNAKE_CASE ) self.assertListEqual(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE ( self : Tuple ) -> Union[str, Any]: a_ : Tuple = self.get_tokenizer() # Testing tokenization a_ : str = '''ใ“ใ‚“ใซใกใฏใ€<|bagoftoken|>ไธ–็•Œใ€‚ใ“ใ‚“ใฐใ‚“ใฏใ€<|bagoftoken|>ใ”บ็•Œใ€‚''' a_ : str = '''ใ“ใ‚“ใซใกใฏใ€ใ€ใ€ใ€ไธ–็•Œใ€‚ใ“ใ‚“ใฐใ‚“ใฏใ€ใ€ใ€ใ€ไธ–็•Œใ€‚''' a_ : Optional[int] = tokenizer.encode(__SCREAMING_SNAKE_CASE ) a_ : str = tokenizer.decode(__SCREAMING_SNAKE_CASE ) self.assertEqual(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) @slow def SCREAMING_SNAKE_CASE ( self : Optional[Any] ) -> List[str]: a_ : Tuple = self.tokenizer_class.from_pretrained('''Tanrei/GPTSAN-japanese''' ) # Testing tokenization a_ : Optional[Any] = '''ใ“ใ‚“ใซใกใฏใ€ไธ–็•Œใ€‚''' a_ : Union[str, Any] = '''ใ“ใ‚“ใฐใ‚“ใฏใ€ใ”บ็•Œใ€‚๐Ÿ˜€''' a_ : Union[str, Any] = '''ใ“ใ‚“ใซใกใฏใ€ไธ–็•Œใ€‚ใ“ใ‚“ใฐใ‚“ใฏใ€ไธ–็•Œใ€‚๐Ÿ˜€''' a_ : Union[str, Any] = tokenizer.encode(prefix_text + input_text ) a_ : Dict = tokenizer.encode('''''' , prefix_text=prefix_text + input_text ) a_ : Optional[Any] = tokenizer.encode(__SCREAMING_SNAKE_CASE , prefix_text=__SCREAMING_SNAKE_CASE ) a_ : Optional[Any] = tokenizer.decode(__SCREAMING_SNAKE_CASE ) a_ : Any = tokenizer.decode(__SCREAMING_SNAKE_CASE ) a_ : Optional[Any] = tokenizer.decode(__SCREAMING_SNAKE_CASE ) self.assertEqual(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) self.assertEqual(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) self.assertEqual(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) @slow def SCREAMING_SNAKE_CASE ( self : Tuple ) -> Optional[Any]: a_ : Any = self.tokenizer_class.from_pretrained('''Tanrei/GPTSAN-japanese''' ) # Testing tokenization a_ : Any = '''ใ“ใ‚“ใซใกใฏใ€ไธ–็•Œใ€‚''' a_ : Tuple = '''ใ“ใ‚“ใฐใ‚“ใฏใ€ใ”บ็•Œใ€‚๐Ÿ˜€''' a_ : Tuple = len(tokenizer.encode(__SCREAMING_SNAKE_CASE ) ) - 2 a_ : List[str] = len(tokenizer.encode(__SCREAMING_SNAKE_CASE ) ) - 2 a_ : List[str] = [1] + [0] * (len_prefix + len_text + 1) a_ : int = [1] * (len_prefix + len_text + 1) + [0] a_ : Optional[Any] = [1] + [1] * (len_prefix) + [0] * (len_text + 1) a_ : List[str] = tokenizer(prefix_text + input_text ).token_type_ids a_ : Union[str, Any] = tokenizer('''''' , prefix_text=prefix_text + input_text ).token_type_ids a_ : Tuple = tokenizer(__SCREAMING_SNAKE_CASE , prefix_text=__SCREAMING_SNAKE_CASE ).token_type_ids self.assertListEqual(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) self.assertListEqual(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) self.assertListEqual(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) @slow def SCREAMING_SNAKE_CASE ( self : str ) -> Dict: a_ : Tuple = self.tokenizer_class.from_pretrained('''Tanrei/GPTSAN-japanese''' ) a_ : int = tokenizer.encode('''ใ‚ใƒณใ„ใƒฏ''' ) a_ : List[Any] = tokenizer.encode('''''' , prefix_text='''ใ‚ใƒณใ„ใƒฏ''' ) a_ : str = tokenizer.encode('''ใ„ใƒฏ''' , prefix_text='''ใ‚ใƒณ''' ) self.assertEqual(tokenizer.decode(__SCREAMING_SNAKE_CASE ) , tokenizer.decode(__SCREAMING_SNAKE_CASE ) ) self.assertEqual(tokenizer.decode(__SCREAMING_SNAKE_CASE ) , tokenizer.decode(__SCREAMING_SNAKE_CASE ) ) self.assertNotEqual(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) self.assertNotEqual(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) self.assertEqual(x_token_a[1] , x_token_a[-1] ) # SEG token self.assertEqual(x_token_a[1] , x_token_a[3] ) # SEG token @slow def SCREAMING_SNAKE_CASE ( self : int ) -> Optional[Any]: a_ : Tuple = self.tokenizer_class.from_pretrained('''Tanrei/GPTSAN-japanese''' ) a_ : Optional[int] = [['''ๆญฆ็”ฐไฟก็Ž„''', '''ใฏใ€'''], ['''็น”็”ฐไฟก้•ท''', '''ใฎ้…ไธ‹ใฎใ€''']] a_ : Any = tokenizer(__SCREAMING_SNAKE_CASE , padding=__SCREAMING_SNAKE_CASE ) a_ : Optional[Any] = tokenizer.batch_encode_plus(__SCREAMING_SNAKE_CASE , padding=__SCREAMING_SNAKE_CASE ) # fmt: off a_ : List[Any] = [[3_5993, 8640, 2_5948, 3_5998, 3_0647, 3_5675, 3_5999, 3_5999], [3_5993, 1_0382, 9868, 3_5998, 3_0646, 9459, 3_0646, 3_5675]] a_ : Any = [[1, 1, 1, 0, 0, 0, 0, 0], [1, 1, 1, 0, 0, 0, 0, 0]] a_ : int = [[1, 1, 1, 1, 1, 1, 0, 0], [1, 1, 1, 1, 1, 1, 1, 1]] # fmt: on self.assertListEqual(x_token.input_ids , __SCREAMING_SNAKE_CASE ) self.assertListEqual(x_token.token_type_ids , __SCREAMING_SNAKE_CASE ) self.assertListEqual(x_token.attention_mask , __SCREAMING_SNAKE_CASE ) self.assertListEqual(x_token_a.input_ids , __SCREAMING_SNAKE_CASE ) self.assertListEqual(x_token_a.token_type_ids , __SCREAMING_SNAKE_CASE ) self.assertListEqual(x_token_a.attention_mask , __SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE ( self : Optional[Any] ) -> str: # Intentionally convert some words to accommodate character fluctuations unique to Japanese pass def SCREAMING_SNAKE_CASE ( self : Any ) -> Optional[int]: # tokenizer has no padding token pass
720
'''simple docstring''' import json import os import pickle import shutil import tempfile from unittest import TestCase from unittest.mock import patch import numpy as np from datasets import Dataset from transformers import is_faiss_available from transformers.models.bart.configuration_bart import BartConfig from transformers.models.bart.tokenization_bart import BartTokenizer from transformers.models.bert.tokenization_bert import VOCAB_FILES_NAMES as DPR_VOCAB_FILES_NAMES from transformers.models.dpr.configuration_dpr import DPRConfig from transformers.models.dpr.tokenization_dpr import DPRContextEncoderTokenizer, DPRQuestionEncoderTokenizer from transformers.models.rag.configuration_rag import RagConfig from transformers.models.rag.retrieval_rag import CustomHFIndex, RagRetriever from transformers.models.roberta.tokenization_roberta import VOCAB_FILES_NAMES as BART_VOCAB_FILES_NAMES from transformers.testing_utils import require_faiss, require_sentencepiece, require_tokenizers, require_torch if is_faiss_available(): import faiss @require_faiss class SCREAMING_SNAKE_CASE ( SCREAMING_SNAKE_CASE_ ): def SCREAMING_SNAKE_CASE ( self : Dict ) -> Dict: a_ : Union[str, Any] = tempfile.mkdtemp() a_ : Union[str, Any] = 8 # DPR tok a_ : Dict = [ '''[UNK]''', '''[CLS]''', '''[SEP]''', '''[PAD]''', '''[MASK]''', '''want''', '''##want''', '''##ed''', '''wa''', '''un''', '''runn''', '''##ing''', ''',''', '''low''', '''lowest''', ] a_ : str = os.path.join(self.tmpdirname , '''dpr_tokenizer''' ) os.makedirs(__SCREAMING_SNAKE_CASE , exist_ok=__SCREAMING_SNAKE_CASE ) a_ : Optional[int] = os.path.join(__SCREAMING_SNAKE_CASE , DPR_VOCAB_FILES_NAMES['''vocab_file'''] ) with open(self.vocab_file , '''w''' , encoding='''utf-8''' ) as vocab_writer: vocab_writer.write(''''''.join([x + '''\n''' for x in vocab_tokens] ) ) # BART tok a_ : Union[str, Any] = [ '''l''', '''o''', '''w''', '''e''', '''r''', '''s''', '''t''', '''i''', '''d''', '''n''', '''\u0120''', '''\u0120l''', '''\u0120n''', '''\u0120lo''', '''\u0120low''', '''er''', '''\u0120lowest''', '''\u0120newer''', '''\u0120wider''', '''<unk>''', ] a_ : int = dict(zip(__SCREAMING_SNAKE_CASE , range(len(__SCREAMING_SNAKE_CASE ) ) ) ) a_ : int = ['''#version: 0.2''', '''\u0120 l''', '''\u0120l o''', '''\u0120lo w''', '''e r''', ''''''] a_ : Optional[int] = {'''unk_token''': '''<unk>'''} a_ : List[str] = os.path.join(self.tmpdirname , '''bart_tokenizer''' ) os.makedirs(__SCREAMING_SNAKE_CASE , exist_ok=__SCREAMING_SNAKE_CASE ) a_ : Tuple = os.path.join(__SCREAMING_SNAKE_CASE , BART_VOCAB_FILES_NAMES['''vocab_file'''] ) a_ : int = os.path.join(__SCREAMING_SNAKE_CASE , BART_VOCAB_FILES_NAMES['''merges_file'''] ) with open(self.vocab_file , '''w''' , encoding='''utf-8''' ) as fp: fp.write(json.dumps(__SCREAMING_SNAKE_CASE ) + '''\n''' ) with open(self.merges_file , '''w''' , encoding='''utf-8''' ) as fp: fp.write('''\n'''.join(__SCREAMING_SNAKE_CASE ) ) def SCREAMING_SNAKE_CASE ( self : Tuple ) -> DPRQuestionEncoderTokenizer: return DPRQuestionEncoderTokenizer.from_pretrained(os.path.join(self.tmpdirname , '''dpr_tokenizer''' ) ) def SCREAMING_SNAKE_CASE ( self : str ) -> DPRContextEncoderTokenizer: return DPRContextEncoderTokenizer.from_pretrained(os.path.join(self.tmpdirname , '''dpr_tokenizer''' ) ) def SCREAMING_SNAKE_CASE ( self : Optional[int] ) -> BartTokenizer: return BartTokenizer.from_pretrained(os.path.join(self.tmpdirname , '''bart_tokenizer''' ) ) def SCREAMING_SNAKE_CASE ( self : List[Any] ) -> Union[str, Any]: shutil.rmtree(self.tmpdirname ) def SCREAMING_SNAKE_CASE ( self : Dict ) -> Union[str, Any]: a_ : str = Dataset.from_dict( { '''id''': ['''0''', '''1'''], '''text''': ['''foo''', '''bar'''], '''title''': ['''Foo''', '''Bar'''], '''embeddings''': [np.ones(self.retrieval_vector_size ), 2 * np.ones(self.retrieval_vector_size )], } ) dataset.add_faiss_index('''embeddings''' , string_factory='''Flat''' , metric_type=faiss.METRIC_INNER_PRODUCT ) return dataset def SCREAMING_SNAKE_CASE ( self : List[str] ) -> Tuple: a_ : List[str] = self.get_dummy_dataset() a_ : Tuple = RagConfig( retrieval_vector_size=self.retrieval_vector_size , question_encoder=DPRConfig().to_dict() , generator=BartConfig().to_dict() , ) with patch('''transformers.models.rag.retrieval_rag.load_dataset''' ) as mock_load_dataset: a_ : Tuple = dataset a_ : Any = RagRetriever( __SCREAMING_SNAKE_CASE , question_encoder_tokenizer=self.get_dpr_tokenizer() , generator_tokenizer=self.get_bart_tokenizer() , ) return retriever def SCREAMING_SNAKE_CASE ( self : Dict , __SCREAMING_SNAKE_CASE : bool ) -> Dict: a_ : Dict = self.get_dummy_dataset() a_ : Dict = RagConfig( retrieval_vector_size=self.retrieval_vector_size , question_encoder=DPRConfig().to_dict() , generator=BartConfig().to_dict() , index_name='''custom''' , ) if from_disk: a_ : Optional[int] = os.path.join(self.tmpdirname , '''dataset''' ) a_ : str = os.path.join(self.tmpdirname , '''index.faiss''' ) dataset.get_index('''embeddings''' ).save(os.path.join(self.tmpdirname , '''index.faiss''' ) ) dataset.drop_index('''embeddings''' ) dataset.save_to_disk(os.path.join(self.tmpdirname , '''dataset''' ) ) del dataset a_ : int = RagRetriever( __SCREAMING_SNAKE_CASE , question_encoder_tokenizer=self.get_dpr_tokenizer() , generator_tokenizer=self.get_bart_tokenizer() , ) else: a_ : Optional[Any] = RagRetriever( __SCREAMING_SNAKE_CASE , question_encoder_tokenizer=self.get_dpr_tokenizer() , generator_tokenizer=self.get_bart_tokenizer() , index=CustomHFIndex(config.retrieval_vector_size , __SCREAMING_SNAKE_CASE ) , ) return retriever def SCREAMING_SNAKE_CASE ( self : Optional[Any] ) -> Optional[int]: a_ : str = Dataset.from_dict( { '''id''': ['''0''', '''1'''], '''text''': ['''foo''', '''bar'''], '''title''': ['''Foo''', '''Bar'''], '''embeddings''': [np.ones(self.retrieval_vector_size + 1 ), 2 * np.ones(self.retrieval_vector_size + 1 )], } ) dataset.add_faiss_index('''embeddings''' , string_factory='''Flat''' , metric_type=faiss.METRIC_INNER_PRODUCT ) a_ : Optional[int] = os.path.join(self.tmpdirname , '''hf_bert_base.hnswSQ8_correct_phi_128.c_index''' ) dataset.save_faiss_index('''embeddings''' , index_file_name + '''.index.dpr''' ) pickle.dump(dataset['''id'''] , open(index_file_name + '''.index_meta.dpr''' , '''wb''' ) ) a_ : Union[str, Any] = os.path.join(self.tmpdirname , '''psgs_w100.tsv.pkl''' ) a_ : Dict = {sample['''id''']: [sample['''text'''], sample['''title''']] for sample in dataset} pickle.dump(__SCREAMING_SNAKE_CASE , open(__SCREAMING_SNAKE_CASE , '''wb''' ) ) a_ : Optional[Any] = RagConfig( retrieval_vector_size=self.retrieval_vector_size , question_encoder=DPRConfig().to_dict() , generator=BartConfig().to_dict() , index_name='''legacy''' , index_path=self.tmpdirname , ) a_ : int = RagRetriever( __SCREAMING_SNAKE_CASE , question_encoder_tokenizer=self.get_dpr_tokenizer() , generator_tokenizer=self.get_bart_tokenizer() ) return retriever def SCREAMING_SNAKE_CASE ( self : Tuple ) -> Dict: a_ : Optional[Any] = 1 a_ : Dict = self.get_dummy_canonical_hf_index_retriever() a_ : Tuple = np.array( [np.ones(self.retrieval_vector_size ), -np.ones(self.retrieval_vector_size )] , dtype=np.floataa ) a_ , a_ , a_ : str = retriever.retrieve(__SCREAMING_SNAKE_CASE , n_docs=__SCREAMING_SNAKE_CASE ) self.assertEqual(retrieved_doc_embeds.shape , (2, n_docs, self.retrieval_vector_size) ) self.assertEqual(len(__SCREAMING_SNAKE_CASE ) , 2 ) self.assertEqual(sorted(doc_dicts[0] ) , ['''embeddings''', '''id''', '''text''', '''title'''] ) self.assertEqual(len(doc_dicts[0]['''id'''] ) , __SCREAMING_SNAKE_CASE ) self.assertEqual(doc_dicts[0]['''id'''][0] , '''1''' ) # max inner product is reached with second doc self.assertEqual(doc_dicts[1]['''id'''][0] , '''0''' ) # max inner product is reached with first doc self.assertListEqual(doc_ids.tolist() , [[1], [0]] ) def SCREAMING_SNAKE_CASE ( self : Tuple ) -> Dict: a_ : str = self.get_dummy_canonical_hf_index_retriever() with tempfile.TemporaryDirectory() as tmp_dirname: with patch('''transformers.models.rag.retrieval_rag.load_dataset''' ) as mock_load_dataset: a_ : List[str] = self.get_dummy_dataset() retriever.save_pretrained(__SCREAMING_SNAKE_CASE ) a_ : Optional[Any] = RagRetriever.from_pretrained(__SCREAMING_SNAKE_CASE ) self.assertIsInstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) a_ : Union[str, Any] = np.array( [np.ones(self.retrieval_vector_size ), -np.ones(self.retrieval_vector_size )] , dtype=np.floataa ) a_ : List[str] = retriever.retrieve(__SCREAMING_SNAKE_CASE , n_docs=1 ) self.assertTrue(out is not None ) def SCREAMING_SNAKE_CASE ( self : Dict ) -> int: a_ : Union[str, Any] = 1 a_ : Optional[Any] = self.get_dummy_custom_hf_index_retriever(from_disk=__SCREAMING_SNAKE_CASE ) a_ : List[Any] = np.array( [np.ones(self.retrieval_vector_size ), -np.ones(self.retrieval_vector_size )] , dtype=np.floataa ) a_ , a_ , a_ : Any = retriever.retrieve(__SCREAMING_SNAKE_CASE , n_docs=__SCREAMING_SNAKE_CASE ) self.assertEqual(retrieved_doc_embeds.shape , (2, n_docs, self.retrieval_vector_size) ) self.assertEqual(len(__SCREAMING_SNAKE_CASE ) , 2 ) self.assertEqual(sorted(doc_dicts[0] ) , ['''embeddings''', '''id''', '''text''', '''title'''] ) self.assertEqual(len(doc_dicts[0]['''id'''] ) , __SCREAMING_SNAKE_CASE ) self.assertEqual(doc_dicts[0]['''id'''][0] , '''1''' ) # max inner product is reached with second doc self.assertEqual(doc_dicts[1]['''id'''][0] , '''0''' ) # max inner product is reached with first doc self.assertListEqual(doc_ids.tolist() , [[1], [0]] ) def SCREAMING_SNAKE_CASE ( self : str ) -> List[str]: a_ : Dict = self.get_dummy_custom_hf_index_retriever(from_disk=__SCREAMING_SNAKE_CASE ) with tempfile.TemporaryDirectory() as tmp_dirname: retriever.save_pretrained(__SCREAMING_SNAKE_CASE ) a_ : List[str] = RagRetriever.from_pretrained(__SCREAMING_SNAKE_CASE ) self.assertIsInstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) a_ : Tuple = np.array( [np.ones(self.retrieval_vector_size ), -np.ones(self.retrieval_vector_size )] , dtype=np.floataa ) a_ : Dict = retriever.retrieve(__SCREAMING_SNAKE_CASE , n_docs=1 ) self.assertTrue(out is not None ) def SCREAMING_SNAKE_CASE ( self : Tuple ) -> Optional[int]: a_ : Union[str, Any] = 1 a_ : str = self.get_dummy_custom_hf_index_retriever(from_disk=__SCREAMING_SNAKE_CASE ) a_ : Optional[int] = np.array( [np.ones(self.retrieval_vector_size ), -np.ones(self.retrieval_vector_size )] , dtype=np.floataa ) a_ , a_ , a_ : Tuple = retriever.retrieve(__SCREAMING_SNAKE_CASE , n_docs=__SCREAMING_SNAKE_CASE ) self.assertEqual(retrieved_doc_embeds.shape , (2, n_docs, self.retrieval_vector_size) ) self.assertEqual(len(__SCREAMING_SNAKE_CASE ) , 2 ) self.assertEqual(sorted(doc_dicts[0] ) , ['''embeddings''', '''id''', '''text''', '''title'''] ) self.assertEqual(len(doc_dicts[0]['''id'''] ) , __SCREAMING_SNAKE_CASE ) self.assertEqual(doc_dicts[0]['''id'''][0] , '''1''' ) # max inner product is reached with second doc self.assertEqual(doc_dicts[1]['''id'''][0] , '''0''' ) # max inner product is reached with first doc self.assertListEqual(doc_ids.tolist() , [[1], [0]] ) def SCREAMING_SNAKE_CASE ( self : int ) -> Dict: a_ : List[str] = self.get_dummy_custom_hf_index_retriever(from_disk=__SCREAMING_SNAKE_CASE ) with tempfile.TemporaryDirectory() as tmp_dirname: retriever.save_pretrained(__SCREAMING_SNAKE_CASE ) a_ : Any = RagRetriever.from_pretrained(__SCREAMING_SNAKE_CASE ) self.assertIsInstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) a_ : Tuple = np.array( [np.ones(self.retrieval_vector_size ), -np.ones(self.retrieval_vector_size )] , dtype=np.floataa ) a_ : Dict = retriever.retrieve(__SCREAMING_SNAKE_CASE , n_docs=1 ) self.assertTrue(out is not None ) def SCREAMING_SNAKE_CASE ( self : Optional[int] ) -> Any: a_ : str = 1 a_ : Tuple = self.get_dummy_legacy_index_retriever() a_ : Union[str, Any] = np.array( [np.ones(self.retrieval_vector_size ), -np.ones(self.retrieval_vector_size )] , dtype=np.floataa ) a_ , a_ , a_ : Any = retriever.retrieve(__SCREAMING_SNAKE_CASE , n_docs=__SCREAMING_SNAKE_CASE ) self.assertEqual(retrieved_doc_embeds.shape , (2, n_docs, self.retrieval_vector_size) ) self.assertEqual(len(__SCREAMING_SNAKE_CASE ) , 2 ) self.assertEqual(sorted(doc_dicts[0] ) , ['''text''', '''title'''] ) self.assertEqual(len(doc_dicts[0]['''text'''] ) , __SCREAMING_SNAKE_CASE ) self.assertEqual(doc_dicts[0]['''text'''][0] , '''bar''' ) # max inner product is reached with second doc self.assertEqual(doc_dicts[1]['''text'''][0] , '''foo''' ) # max inner product is reached with first doc self.assertListEqual(doc_ids.tolist() , [[1], [0]] ) def SCREAMING_SNAKE_CASE ( self : int ) -> Optional[int]: a_ : List[str] = self.get_dummy_legacy_index_retriever() with tempfile.TemporaryDirectory() as tmp_dirname: retriever.save_pretrained(__SCREAMING_SNAKE_CASE ) a_ : Any = RagRetriever.from_pretrained(__SCREAMING_SNAKE_CASE ) self.assertIsInstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) a_ : List[str] = np.array( [np.ones(self.retrieval_vector_size ), -np.ones(self.retrieval_vector_size )] , dtype=np.floataa ) a_ : Optional[Any] = retriever.retrieve(__SCREAMING_SNAKE_CASE , n_docs=1 ) self.assertTrue(out is not None ) @require_torch @require_tokenizers @require_sentencepiece def SCREAMING_SNAKE_CASE ( self : int ) -> List[str]: import torch a_ : Any = 1 a_ : List[Any] = self.get_dummy_canonical_hf_index_retriever() a_ : Union[str, Any] = [[5, 7], [10, 11]] a_ : Optional[int] = np.array( [np.ones(self.retrieval_vector_size ), -np.ones(self.retrieval_vector_size )] , dtype=np.floataa ) a_ : str = retriever(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , prefix=retriever.config.generator.prefix , n_docs=__SCREAMING_SNAKE_CASE ) a_ , a_ , a_ : List[str] = ( out['''context_input_ids'''], out['''context_attention_mask'''], out['''retrieved_doc_embeds'''], ) self.assertEqual(retrieved_doc_embeds.shape , (2, n_docs, self.retrieval_vector_size) ) self.assertIsInstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) self.assertIsInstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) self.assertIsInstance(__SCREAMING_SNAKE_CASE , np.ndarray ) a_ : Any = retriever( __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , prefix=retriever.config.generator.prefix , n_docs=__SCREAMING_SNAKE_CASE , return_tensors='''pt''' , ) a_ , a_ , a_ , a_ : str = ( # noqa: F841 out['''context_input_ids'''], out['''context_attention_mask'''], out['''retrieved_doc_embeds'''], out['''doc_ids'''], ) self.assertEqual(retrieved_doc_embeds.shape , (2, n_docs, self.retrieval_vector_size) ) self.assertIsInstance(__SCREAMING_SNAKE_CASE , torch.Tensor ) self.assertIsInstance(__SCREAMING_SNAKE_CASE , torch.Tensor ) self.assertIsInstance(__SCREAMING_SNAKE_CASE , torch.Tensor ) @require_torch @require_tokenizers @require_sentencepiece def SCREAMING_SNAKE_CASE ( self : Union[str, Any] ) -> Any: a_ : str = self.get_dpr_ctx_encoder_tokenizer() a_ : Tuple = 1 a_ : Any = self.get_dummy_custom_hf_index_retriever(from_disk=__SCREAMING_SNAKE_CASE ) retriever.set_ctx_encoder_tokenizer(__SCREAMING_SNAKE_CASE ) a_ : Dict = [[5, 7], [10, 11]] a_ : List[str] = np.array( [np.ones(self.retrieval_vector_size ), -np.ones(self.retrieval_vector_size )] , dtype=np.floataa ) a_ : List[Any] = retriever(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , prefix=retriever.config.generator.prefix , n_docs=__SCREAMING_SNAKE_CASE ) self.assertEqual( len(__SCREAMING_SNAKE_CASE ) , 6 ) # check whether the retriever output consist of 6 attributes including tokenized docs self.assertEqual( all(k in out for k in ('''tokenized_doc_ids''', '''tokenized_doc_attention_mask''') ) , __SCREAMING_SNAKE_CASE ) # check for doc token related keys in dictionary.
666
0
'''simple docstring''' import json import os import pickle import shutil import tempfile from unittest import TestCase from unittest.mock import patch import numpy as np from datasets import Dataset from transformers import is_faiss_available from transformers.models.bart.configuration_bart import BartConfig from transformers.models.bart.tokenization_bart import BartTokenizer from transformers.models.bert.tokenization_bert import VOCAB_FILES_NAMES as DPR_VOCAB_FILES_NAMES from transformers.models.dpr.configuration_dpr import DPRConfig from transformers.models.dpr.tokenization_dpr import DPRContextEncoderTokenizer, DPRQuestionEncoderTokenizer from transformers.models.rag.configuration_rag import RagConfig from transformers.models.rag.retrieval_rag import CustomHFIndex, RagRetriever from transformers.models.roberta.tokenization_roberta import VOCAB_FILES_NAMES as BART_VOCAB_FILES_NAMES from transformers.testing_utils import require_faiss, require_sentencepiece, require_tokenizers, require_torch if is_faiss_available(): import faiss @require_faiss class SCREAMING_SNAKE_CASE ( SCREAMING_SNAKE_CASE_ ): def SCREAMING_SNAKE_CASE ( self : Dict ) -> Dict: a_ : Union[str, Any] = tempfile.mkdtemp() a_ : Union[str, Any] = 8 # DPR tok a_ : Dict = [ '''[UNK]''', '''[CLS]''', '''[SEP]''', '''[PAD]''', '''[MASK]''', '''want''', '''##want''', '''##ed''', '''wa''', '''un''', '''runn''', '''##ing''', ''',''', '''low''', '''lowest''', ] a_ : str = os.path.join(self.tmpdirname , '''dpr_tokenizer''' ) os.makedirs(__SCREAMING_SNAKE_CASE , exist_ok=__SCREAMING_SNAKE_CASE ) a_ : Optional[int] = os.path.join(__SCREAMING_SNAKE_CASE , DPR_VOCAB_FILES_NAMES['''vocab_file'''] ) with open(self.vocab_file , '''w''' , encoding='''utf-8''' ) as vocab_writer: vocab_writer.write(''''''.join([x + '''\n''' for x in vocab_tokens] ) ) # BART tok a_ : Union[str, Any] = [ '''l''', '''o''', '''w''', '''e''', '''r''', '''s''', '''t''', '''i''', '''d''', '''n''', '''\u0120''', '''\u0120l''', '''\u0120n''', '''\u0120lo''', '''\u0120low''', '''er''', '''\u0120lowest''', '''\u0120newer''', '''\u0120wider''', '''<unk>''', ] a_ : int = dict(zip(__SCREAMING_SNAKE_CASE , range(len(__SCREAMING_SNAKE_CASE ) ) ) ) a_ : int = ['''#version: 0.2''', '''\u0120 l''', '''\u0120l o''', '''\u0120lo w''', '''e r''', ''''''] a_ : Optional[int] = {'''unk_token''': '''<unk>'''} a_ : List[str] = os.path.join(self.tmpdirname , '''bart_tokenizer''' ) os.makedirs(__SCREAMING_SNAKE_CASE , exist_ok=__SCREAMING_SNAKE_CASE ) a_ : Tuple = os.path.join(__SCREAMING_SNAKE_CASE , BART_VOCAB_FILES_NAMES['''vocab_file'''] ) a_ : int = os.path.join(__SCREAMING_SNAKE_CASE , BART_VOCAB_FILES_NAMES['''merges_file'''] ) with open(self.vocab_file , '''w''' , encoding='''utf-8''' ) as fp: fp.write(json.dumps(__SCREAMING_SNAKE_CASE ) + '''\n''' ) with open(self.merges_file , '''w''' , encoding='''utf-8''' ) as fp: fp.write('''\n'''.join(__SCREAMING_SNAKE_CASE ) ) def SCREAMING_SNAKE_CASE ( self : Tuple ) -> DPRQuestionEncoderTokenizer: return DPRQuestionEncoderTokenizer.from_pretrained(os.path.join(self.tmpdirname , '''dpr_tokenizer''' ) ) def SCREAMING_SNAKE_CASE ( self : str ) -> DPRContextEncoderTokenizer: return DPRContextEncoderTokenizer.from_pretrained(os.path.join(self.tmpdirname , '''dpr_tokenizer''' ) ) def SCREAMING_SNAKE_CASE ( self : Optional[int] ) -> BartTokenizer: return BartTokenizer.from_pretrained(os.path.join(self.tmpdirname , '''bart_tokenizer''' ) ) def SCREAMING_SNAKE_CASE ( self : List[Any] ) -> Union[str, Any]: shutil.rmtree(self.tmpdirname ) def SCREAMING_SNAKE_CASE ( self : Dict ) -> Union[str, Any]: a_ : str = Dataset.from_dict( { '''id''': ['''0''', '''1'''], '''text''': ['''foo''', '''bar'''], '''title''': ['''Foo''', '''Bar'''], '''embeddings''': [np.ones(self.retrieval_vector_size ), 2 * np.ones(self.retrieval_vector_size )], } ) dataset.add_faiss_index('''embeddings''' , string_factory='''Flat''' , metric_type=faiss.METRIC_INNER_PRODUCT ) return dataset def SCREAMING_SNAKE_CASE ( self : List[str] ) -> Tuple: a_ : List[str] = self.get_dummy_dataset() a_ : Tuple = RagConfig( retrieval_vector_size=self.retrieval_vector_size , question_encoder=DPRConfig().to_dict() , generator=BartConfig().to_dict() , ) with patch('''transformers.models.rag.retrieval_rag.load_dataset''' ) as mock_load_dataset: a_ : Tuple = dataset a_ : Any = RagRetriever( __SCREAMING_SNAKE_CASE , question_encoder_tokenizer=self.get_dpr_tokenizer() , generator_tokenizer=self.get_bart_tokenizer() , ) return retriever def SCREAMING_SNAKE_CASE ( self : Dict , __SCREAMING_SNAKE_CASE : bool ) -> Dict: a_ : Dict = self.get_dummy_dataset() a_ : Dict = RagConfig( retrieval_vector_size=self.retrieval_vector_size , question_encoder=DPRConfig().to_dict() , generator=BartConfig().to_dict() , index_name='''custom''' , ) if from_disk: a_ : Optional[int] = os.path.join(self.tmpdirname , '''dataset''' ) a_ : str = os.path.join(self.tmpdirname , '''index.faiss''' ) dataset.get_index('''embeddings''' ).save(os.path.join(self.tmpdirname , '''index.faiss''' ) ) dataset.drop_index('''embeddings''' ) dataset.save_to_disk(os.path.join(self.tmpdirname , '''dataset''' ) ) del dataset a_ : int = RagRetriever( __SCREAMING_SNAKE_CASE , question_encoder_tokenizer=self.get_dpr_tokenizer() , generator_tokenizer=self.get_bart_tokenizer() , ) else: a_ : Optional[Any] = RagRetriever( __SCREAMING_SNAKE_CASE , question_encoder_tokenizer=self.get_dpr_tokenizer() , generator_tokenizer=self.get_bart_tokenizer() , index=CustomHFIndex(config.retrieval_vector_size , __SCREAMING_SNAKE_CASE ) , ) return retriever def SCREAMING_SNAKE_CASE ( self : Optional[Any] ) -> Optional[int]: a_ : str = Dataset.from_dict( { '''id''': ['''0''', '''1'''], '''text''': ['''foo''', '''bar'''], '''title''': ['''Foo''', '''Bar'''], '''embeddings''': [np.ones(self.retrieval_vector_size + 1 ), 2 * np.ones(self.retrieval_vector_size + 1 )], } ) dataset.add_faiss_index('''embeddings''' , string_factory='''Flat''' , metric_type=faiss.METRIC_INNER_PRODUCT ) a_ : Optional[int] = os.path.join(self.tmpdirname , '''hf_bert_base.hnswSQ8_correct_phi_128.c_index''' ) dataset.save_faiss_index('''embeddings''' , index_file_name + '''.index.dpr''' ) pickle.dump(dataset['''id'''] , open(index_file_name + '''.index_meta.dpr''' , '''wb''' ) ) a_ : Union[str, Any] = os.path.join(self.tmpdirname , '''psgs_w100.tsv.pkl''' ) a_ : Dict = {sample['''id''']: [sample['''text'''], sample['''title''']] for sample in dataset} pickle.dump(__SCREAMING_SNAKE_CASE , open(__SCREAMING_SNAKE_CASE , '''wb''' ) ) a_ : Optional[Any] = RagConfig( retrieval_vector_size=self.retrieval_vector_size , question_encoder=DPRConfig().to_dict() , generator=BartConfig().to_dict() , index_name='''legacy''' , index_path=self.tmpdirname , ) a_ : int = RagRetriever( __SCREAMING_SNAKE_CASE , question_encoder_tokenizer=self.get_dpr_tokenizer() , generator_tokenizer=self.get_bart_tokenizer() ) return retriever def SCREAMING_SNAKE_CASE ( self : Tuple ) -> Dict: a_ : Optional[Any] = 1 a_ : Dict = self.get_dummy_canonical_hf_index_retriever() a_ : Tuple = np.array( [np.ones(self.retrieval_vector_size ), -np.ones(self.retrieval_vector_size )] , dtype=np.floataa ) a_ : str = retriever.retrieve(__SCREAMING_SNAKE_CASE , n_docs=__SCREAMING_SNAKE_CASE ) self.assertEqual(retrieved_doc_embeds.shape , (2, n_docs, self.retrieval_vector_size) ) self.assertEqual(len(__SCREAMING_SNAKE_CASE ) , 2 ) self.assertEqual(sorted(doc_dicts[0] ) , ['''embeddings''', '''id''', '''text''', '''title'''] ) self.assertEqual(len(doc_dicts[0]['''id'''] ) , __SCREAMING_SNAKE_CASE ) self.assertEqual(doc_dicts[0]['''id'''][0] , '''1''' ) # max inner product is reached with second doc self.assertEqual(doc_dicts[1]['''id'''][0] , '''0''' ) # max inner product is reached with first doc self.assertListEqual(doc_ids.tolist() , [[1], [0]] ) def SCREAMING_SNAKE_CASE ( self : Tuple ) -> Dict: a_ : str = self.get_dummy_canonical_hf_index_retriever() with tempfile.TemporaryDirectory() as tmp_dirname: with patch('''transformers.models.rag.retrieval_rag.load_dataset''' ) as mock_load_dataset: a_ : List[str] = self.get_dummy_dataset() retriever.save_pretrained(__SCREAMING_SNAKE_CASE ) a_ : Optional[Any] = RagRetriever.from_pretrained(__SCREAMING_SNAKE_CASE ) self.assertIsInstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) a_ : Union[str, Any] = np.array( [np.ones(self.retrieval_vector_size ), -np.ones(self.retrieval_vector_size )] , dtype=np.floataa ) a_ : List[str] = retriever.retrieve(__SCREAMING_SNAKE_CASE , n_docs=1 ) self.assertTrue(out is not None ) def SCREAMING_SNAKE_CASE ( self : Dict ) -> int: a_ : Union[str, Any] = 1 a_ : Optional[Any] = self.get_dummy_custom_hf_index_retriever(from_disk=__SCREAMING_SNAKE_CASE ) a_ : List[Any] = np.array( [np.ones(self.retrieval_vector_size ), -np.ones(self.retrieval_vector_size )] , dtype=np.floataa ) a_ : Any = retriever.retrieve(__SCREAMING_SNAKE_CASE , n_docs=__SCREAMING_SNAKE_CASE ) self.assertEqual(retrieved_doc_embeds.shape , (2, n_docs, self.retrieval_vector_size) ) self.assertEqual(len(__SCREAMING_SNAKE_CASE ) , 2 ) self.assertEqual(sorted(doc_dicts[0] ) , ['''embeddings''', '''id''', '''text''', '''title'''] ) self.assertEqual(len(doc_dicts[0]['''id'''] ) , __SCREAMING_SNAKE_CASE ) self.assertEqual(doc_dicts[0]['''id'''][0] , '''1''' ) # max inner product is reached with second doc self.assertEqual(doc_dicts[1]['''id'''][0] , '''0''' ) # max inner product is reached with first doc self.assertListEqual(doc_ids.tolist() , [[1], [0]] ) def SCREAMING_SNAKE_CASE ( self : str ) -> List[str]: a_ : Dict = self.get_dummy_custom_hf_index_retriever(from_disk=__SCREAMING_SNAKE_CASE ) with tempfile.TemporaryDirectory() as tmp_dirname: retriever.save_pretrained(__SCREAMING_SNAKE_CASE ) a_ : List[str] = RagRetriever.from_pretrained(__SCREAMING_SNAKE_CASE ) self.assertIsInstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) a_ : Tuple = np.array( [np.ones(self.retrieval_vector_size ), -np.ones(self.retrieval_vector_size )] , dtype=np.floataa ) a_ : Dict = retriever.retrieve(__SCREAMING_SNAKE_CASE , n_docs=1 ) self.assertTrue(out is not None ) def SCREAMING_SNAKE_CASE ( self : Tuple ) -> Optional[int]: a_ : Union[str, Any] = 1 a_ : str = self.get_dummy_custom_hf_index_retriever(from_disk=__SCREAMING_SNAKE_CASE ) a_ : Optional[int] = np.array( [np.ones(self.retrieval_vector_size ), -np.ones(self.retrieval_vector_size )] , dtype=np.floataa ) a_ : Tuple = retriever.retrieve(__SCREAMING_SNAKE_CASE , n_docs=__SCREAMING_SNAKE_CASE ) self.assertEqual(retrieved_doc_embeds.shape , (2, n_docs, self.retrieval_vector_size) ) self.assertEqual(len(__SCREAMING_SNAKE_CASE ) , 2 ) self.assertEqual(sorted(doc_dicts[0] ) , ['''embeddings''', '''id''', '''text''', '''title'''] ) self.assertEqual(len(doc_dicts[0]['''id'''] ) , __SCREAMING_SNAKE_CASE ) self.assertEqual(doc_dicts[0]['''id'''][0] , '''1''' ) # max inner product is reached with second doc self.assertEqual(doc_dicts[1]['''id'''][0] , '''0''' ) # max inner product is reached with first doc self.assertListEqual(doc_ids.tolist() , [[1], [0]] ) def SCREAMING_SNAKE_CASE ( self : int ) -> Dict: a_ : List[str] = self.get_dummy_custom_hf_index_retriever(from_disk=__SCREAMING_SNAKE_CASE ) with tempfile.TemporaryDirectory() as tmp_dirname: retriever.save_pretrained(__SCREAMING_SNAKE_CASE ) a_ : Any = RagRetriever.from_pretrained(__SCREAMING_SNAKE_CASE ) self.assertIsInstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) a_ : Tuple = np.array( [np.ones(self.retrieval_vector_size ), -np.ones(self.retrieval_vector_size )] , dtype=np.floataa ) a_ : Dict = retriever.retrieve(__SCREAMING_SNAKE_CASE , n_docs=1 ) self.assertTrue(out is not None ) def SCREAMING_SNAKE_CASE ( self : Optional[int] ) -> Any: a_ : str = 1 a_ : Tuple = self.get_dummy_legacy_index_retriever() a_ : Union[str, Any] = np.array( [np.ones(self.retrieval_vector_size ), -np.ones(self.retrieval_vector_size )] , dtype=np.floataa ) a_ : Any = retriever.retrieve(__SCREAMING_SNAKE_CASE , n_docs=__SCREAMING_SNAKE_CASE ) self.assertEqual(retrieved_doc_embeds.shape , (2, n_docs, self.retrieval_vector_size) ) self.assertEqual(len(__SCREAMING_SNAKE_CASE ) , 2 ) self.assertEqual(sorted(doc_dicts[0] ) , ['''text''', '''title'''] ) self.assertEqual(len(doc_dicts[0]['''text'''] ) , __SCREAMING_SNAKE_CASE ) self.assertEqual(doc_dicts[0]['''text'''][0] , '''bar''' ) # max inner product is reached with second doc self.assertEqual(doc_dicts[1]['''text'''][0] , '''foo''' ) # max inner product is reached with first doc self.assertListEqual(doc_ids.tolist() , [[1], [0]] ) def SCREAMING_SNAKE_CASE ( self : int ) -> Optional[int]: a_ : List[str] = self.get_dummy_legacy_index_retriever() with tempfile.TemporaryDirectory() as tmp_dirname: retriever.save_pretrained(__SCREAMING_SNAKE_CASE ) a_ : Any = RagRetriever.from_pretrained(__SCREAMING_SNAKE_CASE ) self.assertIsInstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) a_ : List[str] = np.array( [np.ones(self.retrieval_vector_size ), -np.ones(self.retrieval_vector_size )] , dtype=np.floataa ) a_ : Optional[Any] = retriever.retrieve(__SCREAMING_SNAKE_CASE , n_docs=1 ) self.assertTrue(out is not None ) @require_torch @require_tokenizers @require_sentencepiece def SCREAMING_SNAKE_CASE ( self : int ) -> List[str]: import torch a_ : Any = 1 a_ : List[Any] = self.get_dummy_canonical_hf_index_retriever() a_ : Union[str, Any] = [[5, 7], [10, 11]] a_ : Optional[int] = np.array( [np.ones(self.retrieval_vector_size ), -np.ones(self.retrieval_vector_size )] , dtype=np.floataa ) a_ : str = retriever(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , prefix=retriever.config.generator.prefix , n_docs=__SCREAMING_SNAKE_CASE ) a_ : List[str] = ( out['''context_input_ids'''], out['''context_attention_mask'''], out['''retrieved_doc_embeds'''], ) self.assertEqual(retrieved_doc_embeds.shape , (2, n_docs, self.retrieval_vector_size) ) self.assertIsInstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) self.assertIsInstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) self.assertIsInstance(__SCREAMING_SNAKE_CASE , np.ndarray ) a_ : Any = retriever( __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , prefix=retriever.config.generator.prefix , n_docs=__SCREAMING_SNAKE_CASE , return_tensors='''pt''' , ) a_ : str = ( # noqa: F841 out['''context_input_ids'''], out['''context_attention_mask'''], out['''retrieved_doc_embeds'''], out['''doc_ids'''], ) self.assertEqual(retrieved_doc_embeds.shape , (2, n_docs, self.retrieval_vector_size) ) self.assertIsInstance(__SCREAMING_SNAKE_CASE , torch.Tensor ) self.assertIsInstance(__SCREAMING_SNAKE_CASE , torch.Tensor ) self.assertIsInstance(__SCREAMING_SNAKE_CASE , torch.Tensor ) @require_torch @require_tokenizers @require_sentencepiece def SCREAMING_SNAKE_CASE ( self : Union[str, Any] ) -> Any: a_ : str = self.get_dpr_ctx_encoder_tokenizer() a_ : Tuple = 1 a_ : Any = self.get_dummy_custom_hf_index_retriever(from_disk=__SCREAMING_SNAKE_CASE ) retriever.set_ctx_encoder_tokenizer(__SCREAMING_SNAKE_CASE ) a_ : Dict = [[5, 7], [10, 11]] a_ : List[str] = np.array( [np.ones(self.retrieval_vector_size ), -np.ones(self.retrieval_vector_size )] , dtype=np.floataa ) a_ : List[Any] = retriever(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , prefix=retriever.config.generator.prefix , n_docs=__SCREAMING_SNAKE_CASE ) self.assertEqual( len(__SCREAMING_SNAKE_CASE ) , 6 ) # check whether the retriever output consist of 6 attributes including tokenized docs self.assertEqual( all(k in out for k in ('''tokenized_doc_ids''', '''tokenized_doc_attention_mask''') ) , __SCREAMING_SNAKE_CASE ) # check for doc token related keys in dictionary.
721
'''simple docstring''' from math import pi, sqrt, tan def _UpperCAmelCase ( __A : float ): if side_length < 0: raise ValueError('''surface_area_cube() only accepts non-negative values''' ) return 6 * side_length**2 def _UpperCAmelCase ( __A : float , __A : float , __A : float ): if length < 0 or breadth < 0 or height < 0: raise ValueError('''surface_area_cuboid() only accepts non-negative values''' ) return 2 * ((length * breadth) + (breadth * height) + (length * height)) def _UpperCAmelCase ( __A : float ): if radius < 0: raise ValueError('''surface_area_sphere() only accepts non-negative values''' ) return 4 * pi * radius**2 def _UpperCAmelCase ( __A : float ): if radius < 0: raise ValueError('''surface_area_hemisphere() only accepts non-negative values''' ) return 3 * pi * radius**2 def _UpperCAmelCase ( __A : float , __A : float ): if radius < 0 or height < 0: raise ValueError('''surface_area_cone() only accepts non-negative values''' ) return pi * radius * (radius + (height**2 + radius**2) ** 0.5) def _UpperCAmelCase ( __A : float , __A : float , __A : float ): if radius_a < 0 or radius_a < 0 or height < 0: raise ValueError( '''surface_area_conical_frustum() only accepts non-negative values''' ) a_ : Any = (height**2 + (radius_a - radius_a) ** 2) ** 0.5 return pi * ((slant_height * (radius_a + radius_a)) + radius_a**2 + radius_a**2) def _UpperCAmelCase ( __A : float , __A : float ): if radius < 0 or height < 0: raise ValueError('''surface_area_cylinder() only accepts non-negative values''' ) return 2 * pi * radius * (height + radius) def _UpperCAmelCase ( __A : float , __A : float ): if torus_radius < 0 or tube_radius < 0: raise ValueError('''surface_area_torus() only accepts non-negative values''' ) if torus_radius < tube_radius: raise ValueError( '''surface_area_torus() does not support spindle or self intersecting tori''' ) return 4 * pow(__A , 2 ) * torus_radius * tube_radius def _UpperCAmelCase ( __A : float , __A : float ): if length < 0 or width < 0: raise ValueError('''area_rectangle() only accepts non-negative values''' ) return length * width def _UpperCAmelCase ( __A : float ): if side_length < 0: raise ValueError('''area_square() only accepts non-negative values''' ) return side_length**2 def _UpperCAmelCase ( __A : float , __A : float ): if base < 0 or height < 0: raise ValueError('''area_triangle() only accepts non-negative values''' ) return (base * height) / 2 def _UpperCAmelCase ( __A : float , __A : float , __A : float ): if sidea < 0 or sidea < 0 or sidea < 0: raise ValueError('''area_triangle_three_sides() only accepts non-negative values''' ) elif sidea + sidea < sidea or sidea + sidea < sidea or sidea + sidea < sidea: raise ValueError('''Given three sides do not form a triangle''' ) a_ : int = (sidea + sidea + sidea) / 2 a_ : Optional[Any] = sqrt( semi_perimeter * (semi_perimeter - sidea) * (semi_perimeter - sidea) * (semi_perimeter - sidea) ) return area def _UpperCAmelCase ( __A : float , __A : float ): if base < 0 or height < 0: raise ValueError('''area_parallelogram() only accepts non-negative values''' ) return base * height def _UpperCAmelCase ( __A : float , __A : float , __A : float ): if basea < 0 or basea < 0 or height < 0: raise ValueError('''area_trapezium() only accepts non-negative values''' ) return 1 / 2 * (basea + basea) * height def _UpperCAmelCase ( __A : float ): if radius < 0: raise ValueError('''area_circle() only accepts non-negative values''' ) return pi * radius**2 def _UpperCAmelCase ( __A : float , __A : float ): if radius_x < 0 or radius_y < 0: raise ValueError('''area_ellipse() only accepts non-negative values''' ) return pi * radius_x * radius_y def _UpperCAmelCase ( __A : float , __A : float ): if diagonal_a < 0 or diagonal_a < 0: raise ValueError('''area_rhombus() only accepts non-negative values''' ) return 1 / 2 * diagonal_a * diagonal_a def _UpperCAmelCase ( __A : int , __A : float ): if not isinstance(__A , __A ) or sides < 3: raise ValueError( '''area_reg_polygon() only accepts integers greater than or \ equal to three as number of sides''' ) elif length < 0: raise ValueError( '''area_reg_polygon() only accepts non-negative values as \ length of a side''' ) return (sides * length**2) / (4 * tan(pi / sides )) return (sides * length**2) / (4 * tan(pi / sides )) if __name__ == "__main__": import doctest doctest.testmod(verbose=True) # verbose so we can see methods missing tests print('[DEMO] Areas of various geometric shapes: \n') print(F"""Rectangle: {area_rectangle(10, 20) = }""") print(F"""Square: {area_square(10) = }""") print(F"""Triangle: {area_triangle(10, 10) = }""") print(F"""Triangle: {area_triangle_three_sides(5, 12, 13) = }""") print(F"""Parallelogram: {area_parallelogram(10, 20) = }""") print(F"""Rhombus: {area_rhombus(10, 20) = }""") print(F"""Trapezium: {area_trapezium(10, 20, 30) = }""") print(F"""Circle: {area_circle(20) = }""") print(F"""Ellipse: {area_ellipse(10, 20) = }""") print('\nSurface Areas of various geometric shapes: \n') print(F"""Cube: {surface_area_cube(20) = }""") print(F"""Cuboid: {surface_area_cuboid(10, 20, 30) = }""") print(F"""Sphere: {surface_area_sphere(20) = }""") print(F"""Hemisphere: {surface_area_hemisphere(20) = }""") print(F"""Cone: {surface_area_cone(10, 20) = }""") print(F"""Conical Frustum: {surface_area_conical_frustum(10, 20, 30) = }""") print(F"""Cylinder: {surface_area_cylinder(10, 20) = }""") print(F"""Torus: {surface_area_torus(20, 10) = }""") print(F"""Equilateral Triangle: {area_reg_polygon(3, 10) = }""") print(F"""Square: {area_reg_polygon(4, 10) = }""") print(F"""Reqular Pentagon: {area_reg_polygon(5, 10) = }""")
666
0
'''simple docstring''' import copy import os from typing import Union from ...configuration_utils import PretrainedConfig from ...utils import logging __lowerCAmelCase = logging.get_logger(__name__) __lowerCAmelCase = { 'BridgeTower/bridgetower-base': 'https://huggingface.co/BridgeTower/bridgetower-base/blob/main/config.json', 'BridgeTower/bridgetower-base-itm-mlm': ( 'https://huggingface.co/BridgeTower/bridgetower-base-itm-mlm/blob/main/config.json' ), } class SCREAMING_SNAKE_CASE ( SCREAMING_SNAKE_CASE_ ): snake_case__ = "bridgetower_vision_model" def __init__( self : Optional[int] , __SCREAMING_SNAKE_CASE : List[Any]=768 , __SCREAMING_SNAKE_CASE : Tuple=12 , __SCREAMING_SNAKE_CASE : List[Any]=3 , __SCREAMING_SNAKE_CASE : Union[str, Any]=16 , __SCREAMING_SNAKE_CASE : List[str]=288 , __SCREAMING_SNAKE_CASE : Dict=1 , __SCREAMING_SNAKE_CASE : Tuple=1e-05 , __SCREAMING_SNAKE_CASE : List[str]=False , __SCREAMING_SNAKE_CASE : Union[str, Any]=True , __SCREAMING_SNAKE_CASE : Union[str, Any]=False , **__SCREAMING_SNAKE_CASE : List[Any] , ) -> int: super().__init__(**__SCREAMING_SNAKE_CASE ) a_ : str = hidden_size a_ : int = num_hidden_layers a_ : Optional[Any] = num_channels a_ : List[Any] = patch_size a_ : int = image_size a_ : Optional[Any] = initializer_factor a_ : Optional[int] = layer_norm_eps a_ : Optional[int] = stop_gradient a_ : Optional[int] = share_layernorm a_ : int = remove_last_layer @classmethod def SCREAMING_SNAKE_CASE ( cls : List[str] , __SCREAMING_SNAKE_CASE : Union[str, os.PathLike] , **__SCREAMING_SNAKE_CASE : str ) -> "PretrainedConfig": a_ : Tuple = cls.get_config_dict(__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) if config_dict.get('''model_type''' ) == "bridgetower": a_ : str = config_dict['''text_config'''] if "model_type" in config_dict and hasattr(cls , '''model_type''' ) and config_dict["model_type"] != cls.model_type: logger.warning( f'You are using a model of type {config_dict["model_type"]} to instantiate a model of type ' f'{cls.model_type}. This is not supported for all configurations of models and can yield errors.' ) return cls.from_dict(__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) class SCREAMING_SNAKE_CASE ( SCREAMING_SNAKE_CASE_ ): snake_case__ = "bridgetower_text_model" def __init__( self : List[Any] , __SCREAMING_SNAKE_CASE : Tuple=5_0265 , __SCREAMING_SNAKE_CASE : Any=768 , __SCREAMING_SNAKE_CASE : Optional[Any]=12 , __SCREAMING_SNAKE_CASE : int=12 , __SCREAMING_SNAKE_CASE : Optional[int]=1 , __SCREAMING_SNAKE_CASE : Optional[int]=3072 , __SCREAMING_SNAKE_CASE : List[Any]="gelu" , __SCREAMING_SNAKE_CASE : List[str]=0.1 , __SCREAMING_SNAKE_CASE : Optional[Any]=0.1 , __SCREAMING_SNAKE_CASE : Optional[Any]=514 , __SCREAMING_SNAKE_CASE : str=1 , __SCREAMING_SNAKE_CASE : Optional[Any]=1e-05 , __SCREAMING_SNAKE_CASE : Tuple=1 , __SCREAMING_SNAKE_CASE : Dict=0 , __SCREAMING_SNAKE_CASE : Tuple=2 , __SCREAMING_SNAKE_CASE : Optional[Any]="absolute" , __SCREAMING_SNAKE_CASE : Optional[Any]=True , **__SCREAMING_SNAKE_CASE : str , ) -> Optional[Any]: super().__init__(**__SCREAMING_SNAKE_CASE ) a_ : Union[str, Any] = vocab_size a_ : List[Any] = hidden_size a_ : int = num_hidden_layers a_ : Dict = num_attention_heads a_ : Union[str, Any] = hidden_act a_ : Union[str, Any] = initializer_factor a_ : Tuple = intermediate_size a_ : Tuple = hidden_dropout_prob a_ : Optional[int] = attention_probs_dropout_prob a_ : int = max_position_embeddings a_ : int = type_vocab_size a_ : Union[str, Any] = layer_norm_eps a_ : Optional[int] = position_embedding_type a_ : Union[str, Any] = use_cache a_ : Optional[int] = pad_token_id a_ : Optional[Any] = bos_token_id a_ : int = eos_token_id @classmethod def SCREAMING_SNAKE_CASE ( cls : List[Any] , __SCREAMING_SNAKE_CASE : Union[str, os.PathLike] , **__SCREAMING_SNAKE_CASE : List[Any] ) -> "PretrainedConfig": a_ : Optional[int] = cls.get_config_dict(__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) if config_dict.get('''model_type''' ) == "bridgetower": a_ : Dict = config_dict['''text_config'''] if "model_type" in config_dict and hasattr(cls , '''model_type''' ) and config_dict["model_type"] != cls.model_type: logger.warning( f'You are using a model of type {config_dict["model_type"]} to instantiate a model of type ' f'{cls.model_type}. This is not supported for all configurations of models and can yield errors.' ) return cls.from_dict(__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) class SCREAMING_SNAKE_CASE ( SCREAMING_SNAKE_CASE_ ): snake_case__ = "bridgetower" def __init__( self : int , __SCREAMING_SNAKE_CASE : Tuple=True , __SCREAMING_SNAKE_CASE : Optional[int]="gelu" , __SCREAMING_SNAKE_CASE : Union[str, Any]=768 , __SCREAMING_SNAKE_CASE : int=1 , __SCREAMING_SNAKE_CASE : List[Any]=1e-05 , __SCREAMING_SNAKE_CASE : int=False , __SCREAMING_SNAKE_CASE : Union[str, Any]="add" , __SCREAMING_SNAKE_CASE : List[Any]=12 , __SCREAMING_SNAKE_CASE : Optional[Any]=6 , __SCREAMING_SNAKE_CASE : Tuple=False , __SCREAMING_SNAKE_CASE : Optional[Any]=False , __SCREAMING_SNAKE_CASE : Optional[int]=None , __SCREAMING_SNAKE_CASE : Tuple=None , **__SCREAMING_SNAKE_CASE : int , ) -> Dict: # TODO: remove this once the Hub files are updated. a_ : List[Any] = kwargs.pop('''text_config_dict''' , __SCREAMING_SNAKE_CASE ) a_ : Dict = kwargs.pop('''vision_config_dict''' , __SCREAMING_SNAKE_CASE ) super().__init__(**__SCREAMING_SNAKE_CASE ) a_ : Union[str, Any] = share_cross_modal_transformer_layers a_ : Optional[int] = hidden_act a_ : Optional[int] = hidden_size a_ : Tuple = initializer_factor a_ : Dict = layer_norm_eps a_ : str = share_link_tower_layers a_ : List[Any] = link_tower_type a_ : Dict = num_attention_heads a_ : List[str] = num_hidden_layers a_ : Optional[Any] = tie_word_embeddings a_ : Tuple = init_layernorm_from_vision_encoder if text_config is None: a_ : Any = {} logger.info('''`text_config` is `None`. Initializing the `BridgeTowerTextConfig` with default values.''' ) if vision_config is None: a_ : Optional[Any] = {} logger.info('''`vision_config` is `None`. Initializing the `BridgeTowerVisionConfig` with default values.''' ) a_ : int = BridgeTowerTextConfig(**__SCREAMING_SNAKE_CASE ) a_ : int = BridgeTowerVisionConfig(**__SCREAMING_SNAKE_CASE ) @classmethod def SCREAMING_SNAKE_CASE ( cls : Dict , __SCREAMING_SNAKE_CASE : BridgeTowerTextConfig , __SCREAMING_SNAKE_CASE : BridgeTowerVisionConfig , **__SCREAMING_SNAKE_CASE : Optional[Any] ) -> int: return cls(text_config=text_config.to_dict() , vision_config=vision_config.to_dict() , **__SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE ( self : List[str] ) -> str: a_ : Optional[Any] = copy.deepcopy(self.__dict__ ) a_ : List[str] = self.text_config.to_dict() a_ : Optional[int] = self.vision_config.to_dict() a_ : Optional[int] = self.__class__.model_type return output
700
'''simple docstring''' import random import unittest import torch from diffusers import IFInpaintingSuperResolutionPipeline from diffusers.utils import floats_tensor from diffusers.utils.import_utils import is_xformers_available from diffusers.utils.testing_utils import skip_mps, torch_device from ..pipeline_params import ( TEXT_GUIDED_IMAGE_INPAINTING_BATCH_PARAMS, TEXT_GUIDED_IMAGE_INPAINTING_PARAMS, ) from ..test_pipelines_common import PipelineTesterMixin from . import IFPipelineTesterMixin @skip_mps class SCREAMING_SNAKE_CASE ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , unittest.TestCase ): snake_case__ = IFInpaintingSuperResolutionPipeline snake_case__ = TEXT_GUIDED_IMAGE_INPAINTING_PARAMS - {"width", "height"} snake_case__ = TEXT_GUIDED_IMAGE_INPAINTING_BATCH_PARAMS.union({"original_image"} ) snake_case__ = PipelineTesterMixin.required_optional_params - {"latents"} def SCREAMING_SNAKE_CASE ( self : str ) -> List[str]: return self._get_superresolution_dummy_components() def SCREAMING_SNAKE_CASE ( self : Union[str, Any] , __SCREAMING_SNAKE_CASE : Optional[Any] , __SCREAMING_SNAKE_CASE : Dict=0 ) -> List[Any]: if str(__SCREAMING_SNAKE_CASE ).startswith('''mps''' ): a_ : Optional[int] = torch.manual_seed(__SCREAMING_SNAKE_CASE ) else: a_ : str = torch.Generator(device=__SCREAMING_SNAKE_CASE ).manual_seed(__SCREAMING_SNAKE_CASE ) a_ : Dict = floats_tensor((1, 3, 16, 16) , rng=random.Random(__SCREAMING_SNAKE_CASE ) ).to(__SCREAMING_SNAKE_CASE ) a_ : Union[str, Any] = floats_tensor((1, 3, 32, 32) , rng=random.Random(__SCREAMING_SNAKE_CASE ) ).to(__SCREAMING_SNAKE_CASE ) a_ : Tuple = floats_tensor((1, 3, 32, 32) , rng=random.Random(__SCREAMING_SNAKE_CASE ) ).to(__SCREAMING_SNAKE_CASE ) a_ : Union[str, Any] = { '''prompt''': '''A painting of a squirrel eating a burger''', '''image''': image, '''original_image''': original_image, '''mask_image''': mask_image, '''generator''': generator, '''num_inference_steps''': 2, '''output_type''': '''numpy''', } return inputs @unittest.skipIf( torch_device != '''cuda''' or not is_xformers_available() , reason='''XFormers attention is only available with CUDA and `xformers` installed''' , ) def SCREAMING_SNAKE_CASE ( self : Optional[Any] ) -> List[str]: self._test_xformers_attention_forwardGenerator_pass(expected_max_diff=1e-3 ) def SCREAMING_SNAKE_CASE ( self : int ) -> int: self._test_save_load_optional_components() @unittest.skipIf(torch_device != '''cuda''' , reason='''float16 requires CUDA''' ) def SCREAMING_SNAKE_CASE ( self : str ) -> Tuple: # Due to non-determinism in save load of the hf-internal-testing/tiny-random-t5 text encoder super().test_save_load_floataa(expected_max_diff=1e-1 ) def SCREAMING_SNAKE_CASE ( self : List[str] ) -> Optional[int]: self._test_attention_slicing_forward_pass(expected_max_diff=1e-2 ) def SCREAMING_SNAKE_CASE ( self : Union[str, Any] ) -> Optional[Any]: self._test_save_load_local() def SCREAMING_SNAKE_CASE ( self : Dict ) -> Optional[Any]: self._test_inference_batch_single_identical( expected_max_diff=1e-2 , )
666
0
from __future__ import annotations import unittest from transformers import is_tf_available from transformers.testing_utils import require_tf, slow from ...test_configuration_common import ConfigTester from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor, random_attention_mask from ...test_pipeline_mixin import PipelineTesterMixin if is_tf_available(): import numpy import tensorflow as tf from transformers import ( TF_DPR_CONTEXT_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST, TF_DPR_QUESTION_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST, TF_DPR_READER_PRETRAINED_MODEL_ARCHIVE_LIST, BertConfig, DPRConfig, TFDPRContextEncoder, TFDPRQuestionEncoder, TFDPRReader, ) class SCREAMING_SNAKE_CASE : def __init__( self : Optional[Any] , __SCREAMING_SNAKE_CASE : List[str] , __SCREAMING_SNAKE_CASE : Optional[Any]=13 , __SCREAMING_SNAKE_CASE : Union[str, Any]=7 , __SCREAMING_SNAKE_CASE : Optional[Any]=True , __SCREAMING_SNAKE_CASE : Any=True , __SCREAMING_SNAKE_CASE : Dict=True , __SCREAMING_SNAKE_CASE : Union[str, Any]=True , __SCREAMING_SNAKE_CASE : str=99 , __SCREAMING_SNAKE_CASE : int=32 , __SCREAMING_SNAKE_CASE : Optional[Any]=2 , __SCREAMING_SNAKE_CASE : List[str]=4 , __SCREAMING_SNAKE_CASE : Optional[int]=37 , __SCREAMING_SNAKE_CASE : Tuple="gelu" , __SCREAMING_SNAKE_CASE : int=0.1 , __SCREAMING_SNAKE_CASE : Union[str, Any]=0.1 , __SCREAMING_SNAKE_CASE : List[str]=512 , __SCREAMING_SNAKE_CASE : Optional[Any]=16 , __SCREAMING_SNAKE_CASE : str=2 , __SCREAMING_SNAKE_CASE : str=0.02 , __SCREAMING_SNAKE_CASE : str=3 , __SCREAMING_SNAKE_CASE : int=4 , __SCREAMING_SNAKE_CASE : Union[str, Any]=None , __SCREAMING_SNAKE_CASE : str=0 , ) -> Optional[int]: a_ : Union[str, Any] = parent a_ : Tuple = batch_size a_ : str = seq_length a_ : List[str] = is_training a_ : List[str] = use_input_mask a_ : int = use_token_type_ids a_ : Optional[Any] = use_labels a_ : List[Any] = vocab_size a_ : Any = hidden_size a_ : str = num_hidden_layers a_ : List[Any] = num_attention_heads a_ : str = intermediate_size a_ : Tuple = hidden_act a_ : int = hidden_dropout_prob a_ : Any = attention_probs_dropout_prob a_ : Optional[Any] = max_position_embeddings a_ : Union[str, Any] = type_vocab_size a_ : List[Any] = type_sequence_label_size a_ : Optional[Any] = initializer_range a_ : Optional[int] = num_labels a_ : Optional[Any] = num_choices a_ : int = scope a_ : Tuple = projection_dim def SCREAMING_SNAKE_CASE ( self : Optional[int] ) -> Optional[Any]: a_ : str = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) a_ : Any = None if self.use_input_mask: # follow test_modeling_tf_ctrl.py a_ : List[Any] = 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_ : Tuple = None a_ : str = None a_ : Optional[Any] = None if self.use_labels: a_ : int = ids_tensor([self.batch_size] , self.type_sequence_label_size ) a_ : Tuple = ids_tensor([self.batch_size, self.seq_length] , self.num_labels ) a_ : Tuple = ids_tensor([self.batch_size] , self.num_choices ) a_ : str = BertConfig( vocab_size=self.vocab_size , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , max_position_embeddings=self.max_position_embeddings , type_vocab_size=self.type_vocab_size , is_decoder=__SCREAMING_SNAKE_CASE , initializer_range=self.initializer_range , ) a_ : List[Any] = DPRConfig(projection_dim=self.projection_dim , **config.to_dict() ) return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels def SCREAMING_SNAKE_CASE ( self : List[str] , __SCREAMING_SNAKE_CASE : Optional[Any] , __SCREAMING_SNAKE_CASE : Dict , __SCREAMING_SNAKE_CASE : Optional[Any] , __SCREAMING_SNAKE_CASE : int , __SCREAMING_SNAKE_CASE : str , __SCREAMING_SNAKE_CASE : Any , __SCREAMING_SNAKE_CASE : List[str] ) -> str: a_ : Optional[Any] = TFDPRContextEncoder(config=__SCREAMING_SNAKE_CASE ) a_ : Dict = model(__SCREAMING_SNAKE_CASE , attention_mask=__SCREAMING_SNAKE_CASE , token_type_ids=__SCREAMING_SNAKE_CASE ) a_ : Dict = model(__SCREAMING_SNAKE_CASE , token_type_ids=__SCREAMING_SNAKE_CASE ) a_ : Optional[int] = model(__SCREAMING_SNAKE_CASE ) self.parent.assertEqual(result.pooler_output.shape , (self.batch_size, self.projection_dim or self.hidden_size) ) def SCREAMING_SNAKE_CASE ( self : Optional[int] , __SCREAMING_SNAKE_CASE : List[Any] , __SCREAMING_SNAKE_CASE : Dict , __SCREAMING_SNAKE_CASE : str , __SCREAMING_SNAKE_CASE : List[Any] , __SCREAMING_SNAKE_CASE : str , __SCREAMING_SNAKE_CASE : List[str] , __SCREAMING_SNAKE_CASE : int ) -> List[str]: a_ : List[Any] = TFDPRQuestionEncoder(config=__SCREAMING_SNAKE_CASE ) a_ : Union[str, Any] = model(__SCREAMING_SNAKE_CASE , attention_mask=__SCREAMING_SNAKE_CASE , token_type_ids=__SCREAMING_SNAKE_CASE ) a_ : Dict = model(__SCREAMING_SNAKE_CASE , token_type_ids=__SCREAMING_SNAKE_CASE ) a_ : Union[str, Any] = model(__SCREAMING_SNAKE_CASE ) self.parent.assertEqual(result.pooler_output.shape , (self.batch_size, self.projection_dim or self.hidden_size) ) def SCREAMING_SNAKE_CASE ( self : Dict , __SCREAMING_SNAKE_CASE : List[Any] , __SCREAMING_SNAKE_CASE : Optional[int] , __SCREAMING_SNAKE_CASE : Tuple , __SCREAMING_SNAKE_CASE : Optional[int] , __SCREAMING_SNAKE_CASE : Any , __SCREAMING_SNAKE_CASE : Any , __SCREAMING_SNAKE_CASE : Tuple ) -> int: a_ : List[str] = TFDPRReader(config=__SCREAMING_SNAKE_CASE ) a_ : Dict = model(__SCREAMING_SNAKE_CASE , attention_mask=__SCREAMING_SNAKE_CASE ) self.parent.assertEqual(result.start_logits.shape , (self.batch_size, self.seq_length) ) self.parent.assertEqual(result.end_logits.shape , (self.batch_size, self.seq_length) ) self.parent.assertEqual(result.relevance_logits.shape , (self.batch_size,) ) def SCREAMING_SNAKE_CASE ( self : Union[str, Any] ) -> str: a_ : Optional[int] = self.prepare_config_and_inputs() ( a_ ) : List[str] = config_and_inputs a_ : int = {'''input_ids''': input_ids} return config, inputs_dict @require_tf class SCREAMING_SNAKE_CASE ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , unittest.TestCase ): snake_case__ = ( ( TFDPRContextEncoder, TFDPRQuestionEncoder, TFDPRReader, ) if is_tf_available() else () ) snake_case__ = {"feature-extraction": TFDPRQuestionEncoder} if is_tf_available() else {} snake_case__ = False snake_case__ = False snake_case__ = False snake_case__ = False snake_case__ = False def SCREAMING_SNAKE_CASE ( self : List[Any] ) -> Optional[Any]: a_ : Any = TFDPRModelTester(self ) a_ : int = ConfigTester(self , config_class=__SCREAMING_SNAKE_CASE , hidden_size=37 ) def SCREAMING_SNAKE_CASE ( self : Any ) -> str: self.config_tester.run_common_tests() def SCREAMING_SNAKE_CASE ( self : List[str] ) -> Dict: a_ : List[Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_dpr_context_encoder(*__SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE ( self : Dict ) -> Union[str, Any]: a_ : List[str] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_dpr_question_encoder(*__SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE ( self : List[str] ) -> List[Any]: a_ : Any = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_dpr_reader(*__SCREAMING_SNAKE_CASE ) @slow def SCREAMING_SNAKE_CASE ( self : List[str] ) -> Dict: for model_name in TF_DPR_CONTEXT_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: a_ : List[Any] = TFDPRContextEncoder.from_pretrained(__SCREAMING_SNAKE_CASE ) self.assertIsNotNone(__SCREAMING_SNAKE_CASE ) for model_name in TF_DPR_CONTEXT_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: a_ : Any = TFDPRContextEncoder.from_pretrained(__SCREAMING_SNAKE_CASE ) self.assertIsNotNone(__SCREAMING_SNAKE_CASE ) for model_name in TF_DPR_QUESTION_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: a_ : List[Any] = TFDPRQuestionEncoder.from_pretrained(__SCREAMING_SNAKE_CASE ) self.assertIsNotNone(__SCREAMING_SNAKE_CASE ) for model_name in TF_DPR_READER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: a_ : List[str] = TFDPRReader.from_pretrained(__SCREAMING_SNAKE_CASE ) self.assertIsNotNone(__SCREAMING_SNAKE_CASE ) @require_tf class SCREAMING_SNAKE_CASE ( unittest.TestCase ): @slow def SCREAMING_SNAKE_CASE ( self : int ) -> int: a_ : int = TFDPRQuestionEncoder.from_pretrained('''facebook/dpr-question_encoder-single-nq-base''' ) a_ : Any = tf.constant( [[101, 7592, 1010, 2003, 2026, 3899, 1_0140, 1029, 102]] ) # [CLS] hello, is my dog cute? [SEP] a_ : Optional[int] = model(__SCREAMING_SNAKE_CASE )[0] # embedding shape = (1, 768) # compare the actual values for a slice. a_ : Optional[int] = tf.constant( [ [ 0.0323_6253, 0.1275_3335, 0.1681_8509, 0.0027_9786, 0.389_6933, 0.2426_4945, 0.217_8971, -0.0233_5227, -0.0848_1959, -0.1432_4117, ] ] ) self.assertTrue(numpy.allclose(output[:, :10].numpy() , expected_slice.numpy() , atol=1e-4 ) )
701
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available __lowerCAmelCase = { 'configuration_git': ['GIT_PRETRAINED_CONFIG_ARCHIVE_MAP', 'GitConfig', 'GitVisionConfig'], 'processing_git': ['GitProcessor'], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __lowerCAmelCase = [ 'GIT_PRETRAINED_MODEL_ARCHIVE_LIST', 'GitForCausalLM', 'GitModel', 'GitPreTrainedModel', 'GitVisionModel', ] if TYPE_CHECKING: from .configuration_git import GIT_PRETRAINED_CONFIG_ARCHIVE_MAP, GitConfig, GitVisionConfig from .processing_git import GitProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_git import ( GIT_PRETRAINED_MODEL_ARCHIVE_LIST, GitForCausalLM, GitModel, GitPreTrainedModel, GitVisionModel, ) else: import sys __lowerCAmelCase = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
666
0
'''simple docstring''' import math def _UpperCAmelCase ( __A : int ): a_ : Dict = [] a_ : str = 2 a_ : int = int(math.sqrt(__A ) ) # Size of every segment a_ : List[str] = [True] * (end + 1) a_ : Optional[int] = [] while start <= end: if temp[start] is True: in_prime.append(__A ) for i in range(start * start , end + 1 , __A ): a_ : Dict = False start += 1 prime += in_prime a_ : int = end + 1 a_ : Tuple = min(2 * end , __A ) while low <= n: a_ : List[str] = [True] * (high - low + 1) for each in in_prime: a_ : Union[str, Any] = math.floor(low / each ) * each if t < low: t += each for j in range(__A , high + 1 , __A ): a_ : Tuple = False for j in range(len(__A ) ): if temp[j] is True: prime.append(j + low ) a_ : int = high + 1 a_ : int = min(high + end , __A ) return prime print(sieve(10**6))
702
'''simple docstring''' from unittest.mock import patch import pyspark from datasets.packaged_modules.spark.spark import ( Spark, SparkExamplesIterable, _generate_iterable_examples, ) from ..utils import ( require_dill_gt_0_3_2, require_not_windows, ) def _UpperCAmelCase ( __A : List[str] , __A : List[Any] ): a_ : Any = [] for part_id in partition_order: a_ : str = df.where(f'SPARK_PARTITION_ID() = {part_id}' ).collect() for row_idx, row in enumerate(__A ): expected_row_ids_and_row_dicts.append((f'{part_id}_{row_idx}', row.asDict()) ) return expected_row_ids_and_row_dicts @require_not_windows @require_dill_gt_0_3_2 def _UpperCAmelCase ( ): a_ : List[str] = pyspark.sql.SparkSession.builder.master('''local[*]''' ).appName('''pyspark''' ).getOrCreate() a_ : Union[str, Any] = spark.range(1_00 ).repartition(1 ) a_ : Any = Spark(__A ) # The id ints will be converted to Pyarrow int64s, so each row will be 8 bytes. Setting a max_shard_size of 16 means # that each partition can hold 2 rows. spark_builder._repartition_df_if_needed(max_shard_size=16 ) # Given that the dataframe has 100 rows and each partition has 2 rows, we expect 50 partitions. assert spark_builder.df.rdd.getNumPartitions() == 50 @require_not_windows @require_dill_gt_0_3_2 def _UpperCAmelCase ( ): a_ : List[Any] = pyspark.sql.SparkSession.builder.master('''local[*]''' ).appName('''pyspark''' ).getOrCreate() a_ : int = spark.range(10 ).repartition(2 ) a_ : Tuple = [1, 0] a_ : List[str] = _generate_iterable_examples(__A , __A ) # Reverse the partitions. a_ : int = _get_expected_row_ids_and_row_dicts_for_partition_order(__A , __A ) for i, (row_id, row_dict) in enumerate(generate_fn() ): a_ , a_ : List[Any] = expected_row_ids_and_row_dicts[i] assert row_id == expected_row_id assert row_dict == expected_row_dict @require_not_windows @require_dill_gt_0_3_2 def _UpperCAmelCase ( ): a_ : int = pyspark.sql.SparkSession.builder.master('''local[*]''' ).appName('''pyspark''' ).getOrCreate() a_ : str = spark.range(10 ).repartition(1 ) a_ : Tuple = SparkExamplesIterable(__A ) assert it.n_shards == 1 for i, (row_id, row_dict) in enumerate(__A ): assert row_id == f'0_{i}' assert row_dict == {"id": i} @require_not_windows @require_dill_gt_0_3_2 def _UpperCAmelCase ( ): a_ : Tuple = pyspark.sql.SparkSession.builder.master('''local[*]''' ).appName('''pyspark''' ).getOrCreate() a_ : str = spark.range(30 ).repartition(3 ) # Mock the generator so that shuffle reverses the partition indices. with patch('''numpy.random.Generator''' ) as generator_mock: a_ : Union[str, Any] = lambda __A : x.reverse() a_ : Any = _get_expected_row_ids_and_row_dicts_for_partition_order(__A , [2, 1, 0] ) a_ : str = SparkExamplesIterable(__A ).shuffle_data_sources(__A ) assert shuffled_it.n_shards == 3 for i, (row_id, row_dict) in enumerate(__A ): a_ , a_ : Optional[Any] = expected_row_ids_and_row_dicts[i] assert row_id == expected_row_id assert row_dict == expected_row_dict @require_not_windows @require_dill_gt_0_3_2 def _UpperCAmelCase ( ): a_ : int = pyspark.sql.SparkSession.builder.master('''local[*]''' ).appName('''pyspark''' ).getOrCreate() a_ : List[str] = spark.range(20 ).repartition(4 ) # Partitions 0 and 2 a_ : Dict = SparkExamplesIterable(__A ).shard_data_sources(worker_id=0 , num_workers=2 ) assert shard_it_a.n_shards == 2 a_ : Optional[Any] = _get_expected_row_ids_and_row_dicts_for_partition_order(__A , [0, 2] ) for i, (row_id, row_dict) in enumerate(__A ): a_ , a_ : Tuple = expected_row_ids_and_row_dicts_a[i] assert row_id == expected_row_id assert row_dict == expected_row_dict # Partitions 1 and 3 a_ : List[Any] = SparkExamplesIterable(__A ).shard_data_sources(worker_id=1 , num_workers=2 ) assert shard_it_a.n_shards == 2 a_ : Optional[int] = _get_expected_row_ids_and_row_dicts_for_partition_order(__A , [1, 3] ) for i, (row_id, row_dict) in enumerate(__A ): a_ , a_ : Any = expected_row_ids_and_row_dicts_a[i] assert row_id == expected_row_id assert row_dict == expected_row_dict @require_not_windows @require_dill_gt_0_3_2 def _UpperCAmelCase ( ): a_ : Any = pyspark.sql.SparkSession.builder.master('''local[*]''' ).appName('''pyspark''' ).getOrCreate() a_ : List[Any] = spark.range(1_00 ).repartition(1 ) a_ : Optional[Any] = Spark(__A ) # Choose a small max_shard_size for maximum partitioning. spark_builder._repartition_df_if_needed(max_shard_size=1 ) # The new number of partitions should not be greater than the number of rows. assert spark_builder.df.rdd.getNumPartitions() == 1_00
666
0
'''simple docstring''' def _UpperCAmelCase ( __A : int = 10_00 ): a_ : Union[str, Any] = 1, 1 a_ : List[Any] = 2 while True: a_ : Optional[Any] = 0 a_ : Optional[Any] = fa + fa a_ : Union[str, Any] = fa, f index += 1 for _ in str(__A ): i += 1 if i == n: break return index if __name__ == "__main__": print(solution(int(str(input()).strip())))
703
'''simple docstring''' from collections import OrderedDict from typing import TYPE_CHECKING, Any, List, Mapping, Optional from packaging import version if TYPE_CHECKING: from ... import PreTrainedTokenizer, TensorType from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfigWithPast, PatchingSpec from ...utils import is_torch_available, logging __lowerCAmelCase = logging.get_logger(__name__) __lowerCAmelCase = { 'bigscience/bloom': 'https://huggingface.co/bigscience/bloom/resolve/main/config.json', 'bigscience/bloom-560m': 'https://huggingface.co/bigscience/bloom-560m/blob/main/config.json', 'bigscience/bloom-1b1': 'https://huggingface.co/bigscience/bloom-1b1/blob/main/config.json', 'bigscience/bloom-1b7': 'https://huggingface.co/bigscience/bloom-1b7/blob/main/config.json', 'bigscience/bloom-3b': 'https://huggingface.co/bigscience/bloom-3b/blob/main/config.json', 'bigscience/bloom-7b1': 'https://huggingface.co/bigscience/bloom-7b1/blob/main/config.json', } class SCREAMING_SNAKE_CASE ( SCREAMING_SNAKE_CASE_ ): snake_case__ = "bloom" snake_case__ = ["past_key_values"] snake_case__ = { "num_hidden_layers": "n_layer", "num_attention_heads": "n_head", } def __init__( self : Union[str, Any] , __SCREAMING_SNAKE_CASE : int=25_0880 , __SCREAMING_SNAKE_CASE : Dict=64 , __SCREAMING_SNAKE_CASE : Tuple=2 , __SCREAMING_SNAKE_CASE : int=8 , __SCREAMING_SNAKE_CASE : Any=1e-5 , __SCREAMING_SNAKE_CASE : Optional[Any]=0.02 , __SCREAMING_SNAKE_CASE : Union[str, Any]=True , __SCREAMING_SNAKE_CASE : int=1 , __SCREAMING_SNAKE_CASE : Any=2 , __SCREAMING_SNAKE_CASE : Optional[Any]=False , __SCREAMING_SNAKE_CASE : Optional[Any]=0.0 , __SCREAMING_SNAKE_CASE : str=0.0 , __SCREAMING_SNAKE_CASE : List[Any]=1 , __SCREAMING_SNAKE_CASE : List[str]=False , **__SCREAMING_SNAKE_CASE : str , ) -> Any: a_ : Optional[int] = vocab_size # Backward compatibility with n_embed kwarg a_ : Any = kwargs.pop('''n_embed''' , __SCREAMING_SNAKE_CASE ) a_ : Optional[int] = hidden_size if n_embed is None else n_embed a_ : int = n_layer a_ : str = n_head a_ : Optional[int] = layer_norm_epsilon a_ : Dict = initializer_range a_ : List[str] = use_cache a_ : Dict = pretraining_tp a_ : Optional[Any] = apply_residual_connection_post_layernorm a_ : Optional[Any] = hidden_dropout a_ : List[str] = attention_dropout a_ : Dict = bos_token_id a_ : Optional[int] = eos_token_id a_ : Any = slow_but_exact super().__init__(bos_token_id=__SCREAMING_SNAKE_CASE , eos_token_id=__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) class SCREAMING_SNAKE_CASE ( SCREAMING_SNAKE_CASE_ ): snake_case__ = version.parse("1.12" ) def __init__( self : Tuple , __SCREAMING_SNAKE_CASE : PretrainedConfig , __SCREAMING_SNAKE_CASE : str = "default" , __SCREAMING_SNAKE_CASE : List[PatchingSpec] = None , __SCREAMING_SNAKE_CASE : bool = False , ) -> Optional[Any]: super().__init__(__SCREAMING_SNAKE_CASE , task=__SCREAMING_SNAKE_CASE , patching_specs=__SCREAMING_SNAKE_CASE , use_past=__SCREAMING_SNAKE_CASE ) if not getattr(self._config , '''pad_token_id''' , __SCREAMING_SNAKE_CASE ): # TODO: how to do that better? a_ : Tuple = 0 @property def SCREAMING_SNAKE_CASE ( self : Tuple ) -> Mapping[str, Mapping[int, str]]: a_ : Optional[Any] = OrderedDict({'''input_ids''': {0: '''batch''', 1: '''sequence'''}} ) if self.use_past: # BLOOM stores values on dynamic axis 2. For more details see: https://github.com/huggingface/transformers/pull/18344 self.fill_with_past_key_values_(__SCREAMING_SNAKE_CASE , direction='''inputs''' , inverted_values_shape=__SCREAMING_SNAKE_CASE ) a_ : Union[str, Any] = {0: '''batch''', 1: '''past_sequence + sequence'''} else: a_ : Union[str, Any] = {0: '''batch''', 1: '''sequence'''} return common_inputs @property def SCREAMING_SNAKE_CASE ( self : Any ) -> int: return self._config.n_layer @property def SCREAMING_SNAKE_CASE ( self : int ) -> int: return self._config.n_head @property def SCREAMING_SNAKE_CASE ( self : int ) -> float: return 1e-3 def SCREAMING_SNAKE_CASE ( self : Dict , __SCREAMING_SNAKE_CASE : "PreTrainedTokenizer" , __SCREAMING_SNAKE_CASE : int = -1 , __SCREAMING_SNAKE_CASE : int = -1 , __SCREAMING_SNAKE_CASE : bool = False , __SCREAMING_SNAKE_CASE : Optional["TensorType"] = None , ) -> Mapping[str, Any]: a_ : Dict = super(__SCREAMING_SNAKE_CASE , self ).generate_dummy_inputs( __SCREAMING_SNAKE_CASE , batch_size=__SCREAMING_SNAKE_CASE , seq_length=__SCREAMING_SNAKE_CASE , is_pair=__SCREAMING_SNAKE_CASE , framework=__SCREAMING_SNAKE_CASE ) # We need to order the input in the way they appears in the forward() a_ : Union[str, Any] = 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 a_ , a_ : Any = common_inputs['''input_ids'''].shape # Not using the same length for past_key_values a_ : str = seqlen + 2 a_ : Any = self._config.hidden_size // self.num_attention_heads a_ : Optional[int] = ( batch * self.num_attention_heads, head_dim, past_key_values_length, ) a_ : Any = ( batch * self.num_attention_heads, past_key_values_length, head_dim, ) a_ : List[str] = [ (torch.zeros(__SCREAMING_SNAKE_CASE ), torch.zeros(__SCREAMING_SNAKE_CASE )) for _ in range(self.num_layers ) ] a_ : Union[str, Any] = common_inputs['''attention_mask'''] if self.use_past: a_ : Optional[int] = ordered_inputs['''attention_mask'''].dtype a_ : List[Any] = torch.cat( [ordered_inputs['''attention_mask'''], torch.ones(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , dtype=__SCREAMING_SNAKE_CASE )] , dim=1 ) return ordered_inputs @property def SCREAMING_SNAKE_CASE ( self : Optional[int] ) -> int: return 13
666
0
'''simple docstring''' import argparse from torch import nn # transformers_old should correspond to branch `save_old_prophetnet_model_structure` here # original prophetnet_checkpoints are saved under `patrickvonplaten/..._old` respectively from transformers_old.modeling_prophetnet import ( ProphetNetForConditionalGeneration as ProphetNetForConditionalGenerationOld, ) from transformers_old.modeling_xlm_prophetnet import ( XLMProphetNetForConditionalGeneration as XLMProphetNetForConditionalGenerationOld, ) from transformers import ProphetNetForConditionalGeneration, XLMProphetNetForConditionalGeneration, logging __lowerCAmelCase = logging.get_logger(__name__) logging.set_verbosity_info() def _UpperCAmelCase ( __A : str , __A : str ): if "xprophetnet" in prophetnet_checkpoint_path: a_ : Tuple = XLMProphetNetForConditionalGenerationOld.from_pretrained(__A ) a_ : Optional[Any] = XLMProphetNetForConditionalGeneration.from_pretrained( __A , output_loading_info=__A ) else: a_ : List[Any] = ProphetNetForConditionalGenerationOld.from_pretrained(__A ) a_ : Any = ProphetNetForConditionalGeneration.from_pretrained( __A , output_loading_info=__A ) a_ : str = ['''key_proj''', '''value_proj''', '''query_proj'''] a_ : Tuple = { '''self_attn''': '''ngram_self_attn''', '''cross_attn''': '''encoder_attn''', '''cross_attn_layer_norm''': '''encoder_attn_layer_norm''', '''feed_forward_layer_norm''': '''final_layer_norm''', '''feed_forward''': '''''', '''intermediate''': '''fc1''', '''output''': '''fc2''', '''key_proj''': '''k_proj''', '''query_proj''': '''q_proj''', '''value_proj''': '''v_proj''', '''word_embeddings''': '''embed_tokens''', '''embeddings_layer_norm''': '''emb_layer_norm''', '''relative_pos_embeddings''': '''relative_linear''', '''ngram_embeddings''': '''ngram_input_embed''', '''position_embeddings''': '''embed_positions''', } for key in loading_info["missing_keys"]: a_ : List[str] = key.split('''.''' ) if attributes[0] == "lm_head": a_ : List[str] = prophet a_ : Dict = prophet_old else: a_ : str = prophet.prophetnet a_ : int = prophet_old.model a_ : str = False for attribute in attributes: if attribute in mapping: a_ : Dict = mapping[attribute] if not hasattr(__A , __A ) and len(__A ) > 0: a_ : List[str] = attribute elif hasattr(__A , __A ): a_ : Union[str, Any] = attribute if attribute == "weight": assert old_model.weight.shape == model.weight.shape, "Shapes have to match!" a_ : Tuple = old_model.weight logger.info(f'{attribute} is initialized.' ) a_ : Union[str, Any] = True break elif attribute == "bias": assert old_model.bias.shape == model.bias.shape, "Shapes have to match!" a_ : Union[str, Any] = old_model.bias logger.info(f'{attribute} is initialized' ) a_ : Dict = True break elif attribute in special_keys and hasattr(__A , '''in_proj_weight''' ): a_ : Tuple = old_model.in_proj_weight.shape[0] // 3 a_ : Any = getattr(__A , __A ) param.weight.shape == old_model.in_proj_weight[:embed_dim, :].shape, "Shapes have to match" param.bias.shape == old_model.in_proj_bias[:embed_dim].shape, "Shapes have to match" if attribute == "query_proj": a_ : Union[str, Any] = nn.Parameter(old_model.in_proj_weight[:embed_dim, :] ) a_ : Optional[Any] = nn.Parameter(old_model.in_proj_bias[:embed_dim] ) elif attribute == "key_proj": a_ : List[Any] = nn.Parameter(old_model.in_proj_weight[embed_dim : 2 * embed_dim, :] ) a_ : Optional[int] = nn.Parameter(old_model.in_proj_bias[embed_dim : 2 * embed_dim] ) elif attribute == "value_proj": a_ : Tuple = nn.Parameter(old_model.in_proj_weight[2 * embed_dim :, :] ) a_ : Any = nn.Parameter(old_model.in_proj_bias[2 * embed_dim :] ) a_ : Dict = True break elif attribute == "position_embeddings": assert ( model.position_embeddings.weight.shape[-1] == old_model.embed_positions.weight.shape[-1] ), "Hidden size has to match" assert model.position_embeddings.weight.shape[0] == 5_12, "We want 512 position_embeddings." a_ : Union[str, Any] = nn.Parameter(old_model.embed_positions.weight[:5_12, :] ) a_ : Optional[Any] = True break if attribute.isdigit(): a_ : Union[str, Any] = model[int(__A )] a_ : str = old_model[int(__A )] else: a_ : Tuple = getattr(__A , __A ) if old_attribute == "": a_ : List[str] = old_model else: if not hasattr(__A , __A ): raise ValueError(f'{old_model} does not have {old_attribute}' ) a_ : Optional[Any] = getattr(__A , __A ) if not is_key_init: raise ValueError(f'{key} was not correctly initialized!' ) print(f'Saving model to {pytorch_dump_folder_path}' ) prophet.save_pretrained(__A ) if __name__ == "__main__": __lowerCAmelCase = argparse.ArgumentParser() # Required parameters parser.add_argument( '--prophetnet_checkpoint_path', default=None, type=str, required=True, help='Path the official PyTorch dump.' ) parser.add_argument( '--pytorch_dump_folder_path', default=None, type=str, required=True, help='Path to the output PyTorch model.' ) __lowerCAmelCase = parser.parse_args() convert_prophetnet_checkpoint_to_pytorch(args.prophetnet_checkpoint_path, args.pytorch_dump_folder_path)
704
'''simple docstring''' import sys __lowerCAmelCase = ( '73167176531330624919225119674426574742355349194934' '96983520312774506326239578318016984801869478851843' '85861560789112949495459501737958331952853208805511' '12540698747158523863050715693290963295227443043557' '66896648950445244523161731856403098711121722383113' '62229893423380308135336276614282806444486645238749' '30358907296290491560440772390713810515859307960866' '70172427121883998797908792274921901699720888093776' '65727333001053367881220235421809751254540594752243' '52584907711670556013604839586446706324415722155397' '53697817977846174064955149290862569321978468622482' '83972241375657056057490261407972968652414535100474' '82166370484403199890008895243450658541227588666881' '16427171479924442928230863465674813919123162824586' '17866458359124566529476545682848912883142607690042' '24219022671055626321111109370544217506941658960408' '07198403850962455444362981230987879927244284909188' '84580156166097919133875499200524063689912560717606' '05886116467109405077541002256983155200055935729725' '71636269561882670428252483600823257530420752963450' ) def _UpperCAmelCase ( __A : str ): a_ : Tuple = 1 for digit in s: product *= int(__A ) return product def _UpperCAmelCase ( __A : str = N ): a_ : Dict = -sys.maxsize - 1 a_ : Optional[int] = n[:13] a_ : str = 13 while cur_index < len(__A ) - 13: if int(n[cur_index] ) >= int(substr[0] ): a_ : Tuple = substr[1:] + n[cur_index] cur_index += 1 else: a_ : Dict = max(__A , str_eval(__A ) ) a_ : List[str] = n[cur_index : cur_index + 13] cur_index += 13 return largest_product if __name__ == "__main__": print(F"""{solution() = }""")
666
0
'''simple docstring''' from ...configuration_utils import PretrainedConfig __lowerCAmelCase = { 'google/tapas-base-finetuned-sqa': ( 'https://huggingface.co/google/tapas-base-finetuned-sqa/resolve/main/config.json' ), 'google/tapas-base-finetuned-wtq': ( 'https://huggingface.co/google/tapas-base-finetuned-wtq/resolve/main/config.json' ), 'google/tapas-base-finetuned-wikisql-supervised': ( 'https://huggingface.co/google/tapas-base-finetuned-wikisql-supervised/resolve/main/config.json' ), 'google/tapas-base-finetuned-tabfact': ( 'https://huggingface.co/google/tapas-base-finetuned-tabfact/resolve/main/config.json' ), } class SCREAMING_SNAKE_CASE ( SCREAMING_SNAKE_CASE_ ): snake_case__ = "tapas" def __init__( self : List[str] , __SCREAMING_SNAKE_CASE : Any=3_0522 , __SCREAMING_SNAKE_CASE : Tuple=768 , __SCREAMING_SNAKE_CASE : str=12 , __SCREAMING_SNAKE_CASE : Dict=12 , __SCREAMING_SNAKE_CASE : int=3072 , __SCREAMING_SNAKE_CASE : Any="gelu" , __SCREAMING_SNAKE_CASE : Optional[int]=0.1 , __SCREAMING_SNAKE_CASE : Optional[int]=0.1 , __SCREAMING_SNAKE_CASE : Dict=1024 , __SCREAMING_SNAKE_CASE : Dict=[3, 256, 256, 2, 256, 256, 10] , __SCREAMING_SNAKE_CASE : Any=0.02 , __SCREAMING_SNAKE_CASE : Any=1e-12 , __SCREAMING_SNAKE_CASE : int=0 , __SCREAMING_SNAKE_CASE : int=10.0 , __SCREAMING_SNAKE_CASE : Optional[int]=0 , __SCREAMING_SNAKE_CASE : Any=1.0 , __SCREAMING_SNAKE_CASE : int=None , __SCREAMING_SNAKE_CASE : List[str]=1.0 , __SCREAMING_SNAKE_CASE : str=False , __SCREAMING_SNAKE_CASE : List[Any]=None , __SCREAMING_SNAKE_CASE : Tuple=1.0 , __SCREAMING_SNAKE_CASE : Optional[int]=1.0 , __SCREAMING_SNAKE_CASE : Union[str, Any]=False , __SCREAMING_SNAKE_CASE : Optional[Any]=False , __SCREAMING_SNAKE_CASE : List[Any]="ratio" , __SCREAMING_SNAKE_CASE : List[Any]=None , __SCREAMING_SNAKE_CASE : Any=None , __SCREAMING_SNAKE_CASE : Optional[int]=64 , __SCREAMING_SNAKE_CASE : str=32 , __SCREAMING_SNAKE_CASE : Tuple=False , __SCREAMING_SNAKE_CASE : Union[str, Any]=True , __SCREAMING_SNAKE_CASE : Tuple=False , __SCREAMING_SNAKE_CASE : Tuple=False , __SCREAMING_SNAKE_CASE : Dict=True , __SCREAMING_SNAKE_CASE : List[Any]=False , __SCREAMING_SNAKE_CASE : List[Any]=None , __SCREAMING_SNAKE_CASE : int=None , **__SCREAMING_SNAKE_CASE : List[str] , ) -> str: super().__init__(pad_token_id=__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) # BERT hyperparameters (with updated max_position_embeddings and type_vocab_sizes) a_ : Union[str, Any] = vocab_size a_ : Optional[Any] = hidden_size a_ : str = num_hidden_layers a_ : Dict = num_attention_heads a_ : Any = hidden_act a_ : Dict = intermediate_size a_ : Optional[int] = hidden_dropout_prob a_ : str = attention_probs_dropout_prob a_ : Tuple = max_position_embeddings a_ : int = type_vocab_sizes a_ : Tuple = initializer_range a_ : Any = layer_norm_eps # Fine-tuning task hyperparameters a_ : Any = positive_label_weight a_ : int = num_aggregation_labels a_ : Union[str, Any] = aggregation_loss_weight a_ : List[str] = use_answer_as_supervision a_ : Any = answer_loss_importance a_ : int = use_normalized_answer_loss a_ : Optional[int] = huber_loss_delta a_ : List[str] = temperature a_ : Union[str, Any] = aggregation_temperature a_ : str = use_gumbel_for_cells a_ : List[Any] = use_gumbel_for_aggregation a_ : Optional[Any] = average_approximation_function a_ : Dict = cell_selection_preference a_ : str = answer_loss_cutoff a_ : Union[str, Any] = max_num_rows a_ : Optional[int] = max_num_columns a_ : str = average_logits_per_cell a_ : Tuple = select_one_column a_ : List[str] = allow_empty_column_selection a_ : List[str] = init_cell_selection_weights_to_zero a_ : Union[str, Any] = reset_position_index_per_cell a_ : Dict = disable_per_token_loss # Aggregation hyperparameters a_ : List[str] = aggregation_labels a_ : Dict = no_aggregation_label_index if isinstance(self.aggregation_labels , __SCREAMING_SNAKE_CASE ): a_ : str = {int(__SCREAMING_SNAKE_CASE ): v for k, v in aggregation_labels.items()}
705
'''simple docstring''' from __future__ import annotations def _UpperCAmelCase ( __A : list[int] ): a_ : int = len(__A ) // 2 # choose the middle 3 elements a_ : Dict = lst[m - 1 : m + 2] # if middle element is peak if three[1] > three[0] and three[1] > three[2]: return three[1] # if increasing, recurse on right elif three[0] < three[2]: if len(lst[:m] ) == 2: m -= 1 return peak(lst[m:] ) # decreasing else: if len(lst[:m] ) == 2: m += 1 return peak(lst[:m] ) if __name__ == "__main__": import doctest doctest.testmod()
666
0
'''simple docstring''' import argparse from diffusers.pipelines.stable_diffusion.convert_from_ckpt import download_controlnet_from_original_ckpt if __name__ == "__main__": __lowerCAmelCase = 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 _UpperCAmelCase ( __A : Dict ): 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) __lowerCAmelCase = parser.parse_args() __lowerCAmelCase = 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)
706
'''simple docstring''' import itertools import json import os import unittest from transformers import AddedToken, LongformerTokenizer, LongformerTokenizerFast from transformers.models.longformer.tokenization_longformer import VOCAB_FILES_NAMES from transformers.testing_utils import require_tokenizers, slow from ...test_tokenization_common import TokenizerTesterMixin @require_tokenizers class SCREAMING_SNAKE_CASE ( SCREAMING_SNAKE_CASE_ , unittest.TestCase ): snake_case__ = LongformerTokenizer snake_case__ = True snake_case__ = LongformerTokenizerFast snake_case__ = True def SCREAMING_SNAKE_CASE ( self : Optional[Any] ) -> List[str]: super().setUp() # Adapted from Sennrich et al. 2015 and https://github.com/rsennrich/subword-nmt a_ : Tuple = [ '''l''', '''o''', '''w''', '''e''', '''r''', '''s''', '''t''', '''i''', '''d''', '''n''', '''\u0120''', '''\u0120l''', '''\u0120n''', '''\u0120lo''', '''\u0120low''', '''er''', '''\u0120lowest''', '''\u0120newer''', '''\u0120wider''', '''<unk>''', ] a_ : Optional[Any] = dict(zip(__SCREAMING_SNAKE_CASE , range(len(__SCREAMING_SNAKE_CASE ) ) ) ) a_ : Union[str, Any] = ['''#version: 0.2''', '''\u0120 l''', '''\u0120l o''', '''\u0120lo w''', '''e r''', ''''''] a_ : Any = {'''unk_token''': '''<unk>'''} a_ : Union[str, Any] = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES['''vocab_file'''] ) a_ : Optional[int] = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES['''merges_file'''] ) with open(self.vocab_file , '''w''' , encoding='''utf-8''' ) as fp: fp.write(json.dumps(__SCREAMING_SNAKE_CASE ) + '''\n''' ) with open(self.merges_file , '''w''' , encoding='''utf-8''' ) as fp: fp.write('''\n'''.join(__SCREAMING_SNAKE_CASE ) ) def SCREAMING_SNAKE_CASE ( self : Any , **__SCREAMING_SNAKE_CASE : Any ) -> int: kwargs.update(self.special_tokens_map ) return self.tokenizer_class.from_pretrained(self.tmpdirname , **__SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE ( self : Union[str, Any] , **__SCREAMING_SNAKE_CASE : List[Any] ) -> List[str]: kwargs.update(self.special_tokens_map ) return self.rust_tokenizer_class.from_pretrained(self.tmpdirname , **__SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE ( self : Dict , __SCREAMING_SNAKE_CASE : List[Any] ) -> Any: a_ : Union[str, Any] = '''lower newer''' a_ : List[Any] = '''lower newer''' return input_text, output_text def SCREAMING_SNAKE_CASE ( self : Optional[Any] ) -> List[str]: a_ : Optional[Any] = self.tokenizer_class(self.vocab_file , self.merges_file , **self.special_tokens_map ) a_ : List[str] = '''lower newer''' a_ : str = ['''l''', '''o''', '''w''', '''er''', '''\u0120''', '''n''', '''e''', '''w''', '''er'''] a_ : Optional[int] = tokenizer.tokenize(__SCREAMING_SNAKE_CASE ) # , add_prefix_space=True) self.assertListEqual(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) a_ : Dict = tokens + [tokenizer.unk_token] a_ : Any = [0, 1, 2, 15, 10, 9, 3, 2, 15, 19] self.assertListEqual(tokenizer.convert_tokens_to_ids(__SCREAMING_SNAKE_CASE ) , __SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE ( self : List[str] ) -> Optional[Any]: a_ : Union[str, Any] = self.get_tokenizer() self.assertListEqual(tokenizer.encode('''Hello world!''' , add_special_tokens=__SCREAMING_SNAKE_CASE ) , [0, 3_1414, 232, 328, 2] ) self.assertListEqual( tokenizer.encode('''Hello world! cรฉcรฉ herlolip 418''' , add_special_tokens=__SCREAMING_SNAKE_CASE ) , [0, 3_1414, 232, 328, 740, 1140, 1_2695, 69, 4_6078, 1588, 2] , ) @slow def SCREAMING_SNAKE_CASE ( self : int ) -> Optional[Any]: a_ : Dict = self.tokenizer_class.from_pretrained('''allenai/longformer-base-4096''' ) a_ : Tuple = tokenizer.encode('''sequence builders''' , add_special_tokens=__SCREAMING_SNAKE_CASE ) a_ : Union[str, Any] = tokenizer.encode('''multi-sequence build''' , add_special_tokens=__SCREAMING_SNAKE_CASE ) a_ : Any = tokenizer.encode( '''sequence builders''' , add_special_tokens=__SCREAMING_SNAKE_CASE , add_prefix_space=__SCREAMING_SNAKE_CASE ) a_ : Any = tokenizer.encode( '''sequence builders''' , '''multi-sequence build''' , add_special_tokens=__SCREAMING_SNAKE_CASE , add_prefix_space=__SCREAMING_SNAKE_CASE ) a_ : List[str] = tokenizer.build_inputs_with_special_tokens(__SCREAMING_SNAKE_CASE ) a_ : Union[str, Any] = tokenizer.build_inputs_with_special_tokens(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) assert encoded_sentence == encoded_text_from_decode assert encoded_pair == encoded_pair_from_decode def SCREAMING_SNAKE_CASE ( self : str ) -> int: a_ : str = self.get_tokenizer() a_ : int = '''Encode this sequence.''' a_ : List[str] = tokenizer.byte_encoder[''' '''.encode('''utf-8''' )[0]] # Testing encoder arguments a_ : Dict = tokenizer.encode(__SCREAMING_SNAKE_CASE , add_special_tokens=__SCREAMING_SNAKE_CASE , add_prefix_space=__SCREAMING_SNAKE_CASE ) a_ : Optional[Any] = tokenizer.convert_ids_to_tokens(encoded[0] )[0] self.assertNotEqual(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) a_ : Dict = tokenizer.encode(__SCREAMING_SNAKE_CASE , add_special_tokens=__SCREAMING_SNAKE_CASE , add_prefix_space=__SCREAMING_SNAKE_CASE ) a_ : Any = tokenizer.convert_ids_to_tokens(encoded[0] )[0] self.assertEqual(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) tokenizer.add_special_tokens({'''bos_token''': '''<s>'''} ) a_ : Dict = tokenizer.encode(__SCREAMING_SNAKE_CASE , add_special_tokens=__SCREAMING_SNAKE_CASE ) a_ : Dict = tokenizer.convert_ids_to_tokens(encoded[1] )[0] self.assertNotEqual(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) # Testing spaces after special tokens a_ : Optional[Any] = '''<mask>''' tokenizer.add_special_tokens( {'''mask_token''': AddedToken(__SCREAMING_SNAKE_CASE , lstrip=__SCREAMING_SNAKE_CASE , rstrip=__SCREAMING_SNAKE_CASE )} ) # mask token has a left space a_ : Optional[int] = tokenizer.convert_tokens_to_ids(__SCREAMING_SNAKE_CASE ) a_ : List[Any] = '''Encode <mask> sequence''' a_ : List[str] = '''Encode <mask>sequence''' a_ : int = tokenizer.encode(__SCREAMING_SNAKE_CASE ) a_ : Optional[int] = encoded.index(__SCREAMING_SNAKE_CASE ) a_ : Optional[int] = tokenizer.convert_ids_to_tokens(encoded[mask_loc + 1] )[0] self.assertEqual(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) a_ : Union[str, Any] = tokenizer.encode(__SCREAMING_SNAKE_CASE ) a_ : Optional[int] = encoded.index(__SCREAMING_SNAKE_CASE ) a_ : str = tokenizer.convert_ids_to_tokens(encoded[mask_loc + 1] )[0] self.assertNotEqual(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE ( self : Optional[int] ) -> Union[str, Any]: pass def SCREAMING_SNAKE_CASE ( self : Optional[Any] ) -> Any: for tokenizer, pretrained_name, kwargs in self.tokenizers_list: with self.subTest(f'{tokenizer.__class__.__name__} ({pretrained_name})' ): a_ : Any = self.rust_tokenizer_class.from_pretrained(__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) a_ : Any = self.tokenizer_class.from_pretrained(__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) a_ : str = '''A, <mask> AllenNLP sentence.''' a_ : List[Any] = tokenizer_r.encode_plus(__SCREAMING_SNAKE_CASE , add_special_tokens=__SCREAMING_SNAKE_CASE , return_token_type_ids=__SCREAMING_SNAKE_CASE ) a_ : Dict = tokenizer_p.encode_plus(__SCREAMING_SNAKE_CASE , add_special_tokens=__SCREAMING_SNAKE_CASE , return_token_type_ids=__SCREAMING_SNAKE_CASE ) # token_type_ids should put 0 everywhere self.assertEqual(sum(tokens_r['''token_type_ids'''] ) , sum(tokens_p['''token_type_ids'''] ) ) # attention_mask should put 1 everywhere, so sum over length should be 1 self.assertEqual( sum(tokens_r['''attention_mask'''] ) / len(tokens_r['''attention_mask'''] ) , sum(tokens_p['''attention_mask'''] ) / len(tokens_p['''attention_mask'''] ) , ) a_ : str = tokenizer_r.convert_ids_to_tokens(tokens_r['''input_ids'''] ) a_ : Tuple = tokenizer_p.convert_ids_to_tokens(tokens_p['''input_ids'''] ) # Rust correctly handles the space before the mask while python doesnt self.assertSequenceEqual(tokens_p['''input_ids'''] , [0, 250, 6, 5_0264, 3823, 487, 2_1992, 3645, 4, 2] ) self.assertSequenceEqual(tokens_r['''input_ids'''] , [0, 250, 6, 5_0264, 3823, 487, 2_1992, 3645, 4, 2] ) self.assertSequenceEqual( __SCREAMING_SNAKE_CASE , ['''<s>''', '''A''', ''',''', '''<mask>''', '''ฤ Allen''', '''N''', '''LP''', '''ฤ sentence''', '''.''', '''</s>'''] ) self.assertSequenceEqual( __SCREAMING_SNAKE_CASE , ['''<s>''', '''A''', ''',''', '''<mask>''', '''ฤ Allen''', '''N''', '''LP''', '''ฤ sentence''', '''.''', '''</s>'''] ) def SCREAMING_SNAKE_CASE ( self : Optional[Any] ) -> List[Any]: for trim_offsets, add_prefix_space in itertools.product([True, False] , repeat=2 ): a_ : Any = self.rust_tokenizer_class.from_pretrained( self.tmpdirname , use_fast=__SCREAMING_SNAKE_CASE , add_prefix_space=__SCREAMING_SNAKE_CASE , trim_offsets=__SCREAMING_SNAKE_CASE ) a_ : Union[str, Any] = json.loads(tokenizer_r.backend_tokenizer.pre_tokenizer.__getstate__() ) a_ : str = json.loads(tokenizer_r.backend_tokenizer.post_processor.__getstate__() ) self.assertEqual(pre_tokenizer_state['''add_prefix_space'''] , __SCREAMING_SNAKE_CASE ) self.assertEqual(post_processor_state['''add_prefix_space'''] , __SCREAMING_SNAKE_CASE ) self.assertEqual(post_processor_state['''trim_offsets'''] , __SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE ( self : int ) -> Optional[int]: # Test which aims to verify that the offsets are well adapted to the argument `add_prefix_space` and # `trim_offsets` for tokenizer, pretrained_name, kwargs in self.tokenizers_list: with self.subTest(f'{tokenizer.__class__.__name__} ({pretrained_name})' ): a_ : Dict = '''hello''' # `hello` is a token in the vocabulary of `pretrained_name` a_ : Union[str, Any] = f'{text_of_1_token} {text_of_1_token}' a_ : Any = self.rust_tokenizer_class.from_pretrained( __SCREAMING_SNAKE_CASE , use_fast=__SCREAMING_SNAKE_CASE , add_prefix_space=__SCREAMING_SNAKE_CASE , trim_offsets=__SCREAMING_SNAKE_CASE ) a_ : Optional[int] = tokenizer_r(__SCREAMING_SNAKE_CASE , return_offsets_mapping=__SCREAMING_SNAKE_CASE , add_special_tokens=__SCREAMING_SNAKE_CASE ) self.assertEqual(encoding.offset_mapping[0] , (0, len(__SCREAMING_SNAKE_CASE )) ) self.assertEqual( encoding.offset_mapping[1] , (len(__SCREAMING_SNAKE_CASE ) + 1, len(__SCREAMING_SNAKE_CASE ) + 1 + len(__SCREAMING_SNAKE_CASE )) , ) a_ : Any = self.rust_tokenizer_class.from_pretrained( __SCREAMING_SNAKE_CASE , use_fast=__SCREAMING_SNAKE_CASE , add_prefix_space=__SCREAMING_SNAKE_CASE , trim_offsets=__SCREAMING_SNAKE_CASE ) a_ : str = tokenizer_r(__SCREAMING_SNAKE_CASE , return_offsets_mapping=__SCREAMING_SNAKE_CASE , add_special_tokens=__SCREAMING_SNAKE_CASE ) self.assertEqual(encoding.offset_mapping[0] , (0, len(__SCREAMING_SNAKE_CASE )) ) self.assertEqual( encoding.offset_mapping[1] , (len(__SCREAMING_SNAKE_CASE ) + 1, len(__SCREAMING_SNAKE_CASE ) + 1 + len(__SCREAMING_SNAKE_CASE )) , ) a_ : int = self.rust_tokenizer_class.from_pretrained( __SCREAMING_SNAKE_CASE , use_fast=__SCREAMING_SNAKE_CASE , add_prefix_space=__SCREAMING_SNAKE_CASE , trim_offsets=__SCREAMING_SNAKE_CASE ) a_ : Union[str, Any] = tokenizer_r(__SCREAMING_SNAKE_CASE , return_offsets_mapping=__SCREAMING_SNAKE_CASE , add_special_tokens=__SCREAMING_SNAKE_CASE ) self.assertEqual(encoding.offset_mapping[0] , (0, len(__SCREAMING_SNAKE_CASE )) ) self.assertEqual( encoding.offset_mapping[1] , (len(__SCREAMING_SNAKE_CASE ), len(__SCREAMING_SNAKE_CASE ) + 1 + len(__SCREAMING_SNAKE_CASE )) , ) a_ : Tuple = self.rust_tokenizer_class.from_pretrained( __SCREAMING_SNAKE_CASE , use_fast=__SCREAMING_SNAKE_CASE , add_prefix_space=__SCREAMING_SNAKE_CASE , trim_offsets=__SCREAMING_SNAKE_CASE ) a_ : Any = tokenizer_r(__SCREAMING_SNAKE_CASE , return_offsets_mapping=__SCREAMING_SNAKE_CASE , add_special_tokens=__SCREAMING_SNAKE_CASE ) self.assertEqual(encoding.offset_mapping[0] , (0, len(__SCREAMING_SNAKE_CASE )) ) self.assertEqual( encoding.offset_mapping[1] , (len(__SCREAMING_SNAKE_CASE ), len(__SCREAMING_SNAKE_CASE ) + 1 + len(__SCREAMING_SNAKE_CASE )) , ) a_ : Union[str, Any] = f' {text}' # tokenizer_r = self.rust_tokenizer_class.from_pretrained( # pretrained_name, use_fast=True, add_prefix_space=True, trim_offsets=True # ) # encoding = tokenizer_r(text, return_offsets_mapping=True, add_special_tokens=False) # self.assertEqual(encoding.offset_mapping[0], (1, 1 + len(text_of_1_token))) # self.assertEqual( # encoding.offset_mapping[1], # (1 + len(text_of_1_token) + 1, 1 + len(text_of_1_token) + 1 + len(text_of_1_token)), # ) a_ : str = self.rust_tokenizer_class.from_pretrained( __SCREAMING_SNAKE_CASE , use_fast=__SCREAMING_SNAKE_CASE , add_prefix_space=__SCREAMING_SNAKE_CASE , trim_offsets=__SCREAMING_SNAKE_CASE ) a_ : int = tokenizer_r(__SCREAMING_SNAKE_CASE , return_offsets_mapping=__SCREAMING_SNAKE_CASE , add_special_tokens=__SCREAMING_SNAKE_CASE ) self.assertEqual(encoding.offset_mapping[0] , (1, 1 + len(__SCREAMING_SNAKE_CASE )) ) self.assertEqual( encoding.offset_mapping[1] , (1 + len(__SCREAMING_SNAKE_CASE ) + 1, 1 + len(__SCREAMING_SNAKE_CASE ) + 1 + len(__SCREAMING_SNAKE_CASE )) , ) a_ : int = self.rust_tokenizer_class.from_pretrained( __SCREAMING_SNAKE_CASE , use_fast=__SCREAMING_SNAKE_CASE , add_prefix_space=__SCREAMING_SNAKE_CASE , trim_offsets=__SCREAMING_SNAKE_CASE ) a_ : str = tokenizer_r(__SCREAMING_SNAKE_CASE , return_offsets_mapping=__SCREAMING_SNAKE_CASE , add_special_tokens=__SCREAMING_SNAKE_CASE ) self.assertEqual(encoding.offset_mapping[0] , (0, 1 + len(__SCREAMING_SNAKE_CASE )) ) self.assertEqual( encoding.offset_mapping[1] , (1 + len(__SCREAMING_SNAKE_CASE ), 1 + len(__SCREAMING_SNAKE_CASE ) + 1 + len(__SCREAMING_SNAKE_CASE )) , ) a_ : Optional[int] = self.rust_tokenizer_class.from_pretrained( __SCREAMING_SNAKE_CASE , use_fast=__SCREAMING_SNAKE_CASE , add_prefix_space=__SCREAMING_SNAKE_CASE , trim_offsets=__SCREAMING_SNAKE_CASE ) a_ : int = tokenizer_r(__SCREAMING_SNAKE_CASE , return_offsets_mapping=__SCREAMING_SNAKE_CASE , add_special_tokens=__SCREAMING_SNAKE_CASE ) self.assertEqual(encoding.offset_mapping[0] , (0, 1 + len(__SCREAMING_SNAKE_CASE )) ) self.assertEqual( encoding.offset_mapping[1] , (1 + len(__SCREAMING_SNAKE_CASE ), 1 + len(__SCREAMING_SNAKE_CASE ) + 1 + len(__SCREAMING_SNAKE_CASE )) , )
666
0
'''simple docstring''' __lowerCAmelCase = '\n# Transformers installation\n! pip install transformers datasets\n# To install from source instead of the last release, comment the command above and uncomment the following one.\n# ! pip install git+https://github.com/huggingface/transformers.git\n' __lowerCAmelCase = [{'type': 'code', 'content': INSTALL_CONTENT}] __lowerCAmelCase = { '{processor_class}': 'FakeProcessorClass', '{model_class}': 'FakeModelClass', '{object_class}': 'FakeObjectClass', }
707
'''simple docstring''' import os from shutil import copyfile from typing import Any, Dict, List, Optional, Tuple import sentencepiece as spm from ...tokenization_utils import AddedToken, PreTrainedTokenizer from ...utils import logging __lowerCAmelCase = logging.get_logger(__name__) __lowerCAmelCase = {'vocab_file': 'sentencepiece.bpe.model'} __lowerCAmelCase = { 'vocab_file': { 'moussaKam/mbarthez': 'https://huggingface.co/moussaKam/mbarthez/resolve/main/sentencepiece.bpe.model', 'moussaKam/barthez': 'https://huggingface.co/moussaKam/barthez/resolve/main/sentencepiece.bpe.model', 'moussaKam/barthez-orangesum-title': ( 'https://huggingface.co/moussaKam/barthez-orangesum-title/resolve/main/sentencepiece.bpe.model' ), }, } __lowerCAmelCase = { 'moussaKam/mbarthez': 1_024, 'moussaKam/barthez': 1_024, 'moussaKam/barthez-orangesum-title': 1_024, } __lowerCAmelCase = 'โ–' class SCREAMING_SNAKE_CASE ( SCREAMING_SNAKE_CASE_ ): snake_case__ = VOCAB_FILES_NAMES snake_case__ = PRETRAINED_VOCAB_FILES_MAP snake_case__ = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES snake_case__ = ["input_ids", "attention_mask"] def __init__( self : Dict , __SCREAMING_SNAKE_CASE : str , __SCREAMING_SNAKE_CASE : Dict="<s>" , __SCREAMING_SNAKE_CASE : List[Any]="</s>" , __SCREAMING_SNAKE_CASE : List[str]="</s>" , __SCREAMING_SNAKE_CASE : List[str]="<s>" , __SCREAMING_SNAKE_CASE : Dict="<unk>" , __SCREAMING_SNAKE_CASE : int="<pad>" , __SCREAMING_SNAKE_CASE : Tuple="<mask>" , __SCREAMING_SNAKE_CASE : Optional[Dict[str, Any]] = None , **__SCREAMING_SNAKE_CASE : Optional[Any] , ) -> None: # Mask token behave like a normal word, i.e. include the space before it a_ : Tuple = AddedToken(__SCREAMING_SNAKE_CASE , lstrip=__SCREAMING_SNAKE_CASE , rstrip=__SCREAMING_SNAKE_CASE ) if isinstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) else mask_token a_ : List[str] = {} if sp_model_kwargs is None else sp_model_kwargs super().__init__( bos_token=__SCREAMING_SNAKE_CASE , eos_token=__SCREAMING_SNAKE_CASE , unk_token=__SCREAMING_SNAKE_CASE , sep_token=__SCREAMING_SNAKE_CASE , cls_token=__SCREAMING_SNAKE_CASE , pad_token=__SCREAMING_SNAKE_CASE , mask_token=__SCREAMING_SNAKE_CASE , sp_model_kwargs=self.sp_model_kwargs , **__SCREAMING_SNAKE_CASE , ) a_ : Tuple = vocab_file a_ : Optional[Any] = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(str(__SCREAMING_SNAKE_CASE ) ) a_ : Optional[Any] = {'''<s>''': 0, '''<pad>''': 1, '''</s>''': 2, '''<unk>''': 3} a_ : Any = len(self.sp_model ) - 1 a_ : Optional[Any] = {v: k for k, v in self.fairseq_tokens_to_ids.items()} def SCREAMING_SNAKE_CASE ( self : str , __SCREAMING_SNAKE_CASE : List[int] , __SCREAMING_SNAKE_CASE : Optional[List[int]] = None ) -> List[int]: if token_ids_a is None: return [self.cls_token_id] + token_ids_a + [self.sep_token_id] a_ : List[str] = [self.cls_token_id] a_ : Optional[Any] = [self.sep_token_id] return cls + token_ids_a + sep + sep + token_ids_a + sep def SCREAMING_SNAKE_CASE ( self : Optional[int] , __SCREAMING_SNAKE_CASE : List[int] , __SCREAMING_SNAKE_CASE : Optional[List[int]] = None , __SCREAMING_SNAKE_CASE : bool = False ) -> List[int]: 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 None: return [1] + ([0] * len(__SCREAMING_SNAKE_CASE )) + [1] return [1] + ([0] * len(__SCREAMING_SNAKE_CASE )) + [1, 1] + ([0] * len(__SCREAMING_SNAKE_CASE )) + [1] def SCREAMING_SNAKE_CASE ( self : Union[str, Any] , __SCREAMING_SNAKE_CASE : List[int] , __SCREAMING_SNAKE_CASE : Optional[List[int]] = None ) -> List[int]: a_ : List[str] = [self.sep_token_id] a_ : Any = [self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep ) * [0] return len(cls + token_ids_a + sep + sep + token_ids_a + sep ) * [0] @property def SCREAMING_SNAKE_CASE ( self : Any ) -> Union[str, Any]: return len(self.sp_model ) def SCREAMING_SNAKE_CASE ( self : str ) -> Tuple: a_ : int = {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 SCREAMING_SNAKE_CASE ( self : Optional[Any] , __SCREAMING_SNAKE_CASE : str ) -> List[str]: return self.sp_model.encode(__SCREAMING_SNAKE_CASE , out_type=__SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE ( self : str , __SCREAMING_SNAKE_CASE : List[Any] ) -> Dict: if token in self.fairseq_tokens_to_ids: return self.fairseq_tokens_to_ids[token] a_ : Optional[int] = self.sp_model.PieceToId(__SCREAMING_SNAKE_CASE ) return spm_id if spm_id else self.unk_token_id def SCREAMING_SNAKE_CASE ( self : List[str] , __SCREAMING_SNAKE_CASE : int ) -> str: if index in self.fairseq_ids_to_tokens: return self.fairseq_ids_to_tokens[index] return self.sp_model.IdToPiece(__SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE ( self : str , __SCREAMING_SNAKE_CASE : Optional[Any] ) -> Optional[int]: a_ : Dict = [] a_ : List[Any] = '''''' a_ : Dict = False for token in tokens: # make sure that special tokens are not decoded using sentencepiece model if token in self.all_special_tokens: if not prev_is_special: out_string += " " out_string += self.sp_model.decode(__SCREAMING_SNAKE_CASE ) + token a_ : Dict = True a_ : Optional[Any] = [] else: current_sub_tokens.append(__SCREAMING_SNAKE_CASE ) a_ : Tuple = False out_string += self.sp_model.decode(__SCREAMING_SNAKE_CASE ) return out_string.strip() def __getstate__( self : Dict ) -> int: a_ : Dict = self.__dict__.copy() a_ : List[str] = None return state def __setstate__( self : Dict , __SCREAMING_SNAKE_CASE : Optional[Any] ) -> Any: a_ : Optional[Any] = d # for backward compatibility if not hasattr(self , '''sp_model_kwargs''' ): a_ : Union[str, Any] = {} a_ : int = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(self.vocab_file ) def SCREAMING_SNAKE_CASE ( self : str , __SCREAMING_SNAKE_CASE : str , __SCREAMING_SNAKE_CASE : Optional[str] = None ) -> Tuple[str]: if not os.path.isdir(__SCREAMING_SNAKE_CASE ): logger.error(f'Vocabulary path ({save_directory}) should be a directory' ) return a_ : Union[str, Any] = 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: a_ : Any = self.sp_model.serialized_model_proto() fi.write(__SCREAMING_SNAKE_CASE ) return (out_vocab_file,)
666
0
'''simple docstring''' import gc import random import unittest import numpy as np import torch from transformers import ( CLIPImageProcessor, CLIPTextConfig, CLIPTextModel, CLIPTokenizer, CLIPVisionConfig, CLIPVisionModelWithProjection, ) from diffusers import AutoencoderKL, DDIMScheduler, DDPMScheduler, StableUnCLIPImgaImgPipeline, UNetaDConditionModel from diffusers.pipelines.pipeline_utils import DiffusionPipeline from diffusers.pipelines.stable_diffusion.stable_unclip_image_normalizer import StableUnCLIPImageNormalizer from diffusers.utils.import_utils import is_xformers_available from diffusers.utils.testing_utils import ( enable_full_determinism, floats_tensor, load_image, load_numpy, require_torch_gpu, skip_mps, slow, torch_device, ) from ..pipeline_params import TEXT_GUIDED_IMAGE_VARIATION_BATCH_PARAMS, TEXT_GUIDED_IMAGE_VARIATION_PARAMS from ..test_pipelines_common import ( PipelineKarrasSchedulerTesterMixin, PipelineLatentTesterMixin, PipelineTesterMixin, assert_mean_pixel_difference, ) enable_full_determinism() class SCREAMING_SNAKE_CASE ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , unittest.TestCase ): snake_case__ = StableUnCLIPImgaImgPipeline snake_case__ = TEXT_GUIDED_IMAGE_VARIATION_PARAMS snake_case__ = TEXT_GUIDED_IMAGE_VARIATION_BATCH_PARAMS snake_case__ = frozenset( [] ) # TO-DO: update image_params once pipeline is refactored with VaeImageProcessor.preprocess snake_case__ = frozenset([] ) def SCREAMING_SNAKE_CASE ( self : List[str] ) -> Dict: a_ : List[Any] = 32 a_ : Optional[int] = embedder_hidden_size # image encoding components a_ : str = CLIPImageProcessor(crop_size=32 , size=32 ) torch.manual_seed(0 ) a_ : Dict = CLIPVisionModelWithProjection( CLIPVisionConfig( hidden_size=__SCREAMING_SNAKE_CASE , projection_dim=__SCREAMING_SNAKE_CASE , num_hidden_layers=5 , num_attention_heads=4 , image_size=32 , intermediate_size=37 , patch_size=1 , ) ) # regular denoising components torch.manual_seed(0 ) a_ : str = StableUnCLIPImageNormalizer(embedding_dim=__SCREAMING_SNAKE_CASE ) a_ : int = DDPMScheduler(beta_schedule='''squaredcos_cap_v2''' ) torch.manual_seed(0 ) a_ : Optional[int] = CLIPTokenizer.from_pretrained('''hf-internal-testing/tiny-random-clip''' ) torch.manual_seed(0 ) a_ : Optional[int] = CLIPTextModel( CLIPTextConfig( bos_token_id=0 , eos_token_id=2 , hidden_size=__SCREAMING_SNAKE_CASE , projection_dim=32 , intermediate_size=37 , layer_norm_eps=1e-05 , num_attention_heads=4 , num_hidden_layers=5 , pad_token_id=1 , vocab_size=1000 , ) ) torch.manual_seed(0 ) a_ : List[str] = UNetaDConditionModel( sample_size=32 , in_channels=4 , out_channels=4 , down_block_types=('''CrossAttnDownBlock2D''', '''DownBlock2D''') , up_block_types=('''UpBlock2D''', '''CrossAttnUpBlock2D''') , block_out_channels=(32, 64) , attention_head_dim=(2, 4) , class_embed_type='''projection''' , projection_class_embeddings_input_dim=embedder_projection_dim * 2 , cross_attention_dim=__SCREAMING_SNAKE_CASE , layers_per_block=1 , upcast_attention=__SCREAMING_SNAKE_CASE , use_linear_projection=__SCREAMING_SNAKE_CASE , ) torch.manual_seed(0 ) a_ : Tuple = DDIMScheduler( beta_schedule='''scaled_linear''' , beta_start=0.0_0085 , beta_end=0.012 , prediction_type='''v_prediction''' , set_alpha_to_one=__SCREAMING_SNAKE_CASE , steps_offset=1 , ) torch.manual_seed(0 ) a_ : Optional[Any] = AutoencoderKL() a_ : Tuple = { # image encoding components '''feature_extractor''': feature_extractor, '''image_encoder''': image_encoder.eval(), # image noising components '''image_normalizer''': image_normalizer.eval(), '''image_noising_scheduler''': image_noising_scheduler, # regular denoising components '''tokenizer''': tokenizer, '''text_encoder''': text_encoder.eval(), '''unet''': unet.eval(), '''scheduler''': scheduler, '''vae''': vae.eval(), } return components def SCREAMING_SNAKE_CASE ( self : str , __SCREAMING_SNAKE_CASE : int , __SCREAMING_SNAKE_CASE : Tuple=0 , __SCREAMING_SNAKE_CASE : Optional[int]=True ) -> List[str]: if str(__SCREAMING_SNAKE_CASE ).startswith('''mps''' ): a_ : List[str] = torch.manual_seed(__SCREAMING_SNAKE_CASE ) else: a_ : Tuple = torch.Generator(device=__SCREAMING_SNAKE_CASE ).manual_seed(__SCREAMING_SNAKE_CASE ) a_ : int = floats_tensor((1, 3, 32, 32) , rng=random.Random(__SCREAMING_SNAKE_CASE ) ).to(__SCREAMING_SNAKE_CASE ) if pil_image: a_ : List[str] = input_image * 0.5 + 0.5 a_ : List[Any] = input_image.clamp(0 , 1 ) a_ : str = input_image.cpu().permute(0 , 2 , 3 , 1 ).float().numpy() a_ : Optional[Any] = DiffusionPipeline.numpy_to_pil(__SCREAMING_SNAKE_CASE )[0] return { "prompt": "An anime racoon running a marathon", "image": input_image, "generator": generator, "num_inference_steps": 2, "output_type": "np", } @skip_mps def SCREAMING_SNAKE_CASE ( self : Optional[int] ) -> Union[str, Any]: a_ : List[str] = '''cpu''' # ensure determinism for the device-dependent torch.Generator a_ : List[Any] = self.get_dummy_components() a_ : Any = StableUnCLIPImgaImgPipeline(**__SCREAMING_SNAKE_CASE ) a_ : int = sd_pipe.to(__SCREAMING_SNAKE_CASE ) sd_pipe.set_progress_bar_config(disable=__SCREAMING_SNAKE_CASE ) a_ : List[Any] = self.get_dummy_inputs(__SCREAMING_SNAKE_CASE ) inputs.update({'''image_embeds''': None} ) a_ : List[Any] = sd_pipe(**__SCREAMING_SNAKE_CASE ).images a_ : Union[str, Any] = image[0, -3:, -3:, -1] assert image.shape == (1, 32, 32, 3) a_ : List[str] = np.array([0.3872, 0.7224, 0.5601, 0.4741, 0.6872, 0.5814, 0.4636, 0.3867, 0.5078] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-3 def SCREAMING_SNAKE_CASE ( self : Any ) -> Optional[int]: a_ : List[str] = torch_device in ['''cpu''', '''mps'''] self._test_attention_slicing_forward_pass(test_max_difference=__SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE ( self : Union[str, Any] ) -> Dict: a_ : int = torch_device in ['''cpu''', '''mps'''] self._test_inference_batch_single_identical(test_max_difference=__SCREAMING_SNAKE_CASE ) @unittest.skipIf( torch_device != '''cuda''' or not is_xformers_available() , reason='''XFormers attention is only available with CUDA and `xformers` installed''' , ) def SCREAMING_SNAKE_CASE ( self : Tuple ) -> int: self._test_xformers_attention_forwardGenerator_pass(test_max_difference=__SCREAMING_SNAKE_CASE ) @slow @require_torch_gpu class SCREAMING_SNAKE_CASE ( unittest.TestCase ): def SCREAMING_SNAKE_CASE ( self : str ) -> Any: # clean up the VRAM after each test super().tearDown() gc.collect() torch.cuda.empty_cache() def SCREAMING_SNAKE_CASE ( self : Dict ) -> Any: a_ : Union[str, Any] = load_image( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/stable_unclip/turtle.png''' ) a_ : Optional[int] = load_numpy( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/stable_unclip/stable_unclip_2_1_l_img2img_anime_turtle_fp16.npy''' ) a_ : List[Any] = StableUnCLIPImgaImgPipeline.from_pretrained( '''fusing/stable-unclip-2-1-l-img2img''' , torch_dtype=torch.floataa ) pipe.to(__SCREAMING_SNAKE_CASE ) pipe.set_progress_bar_config(disable=__SCREAMING_SNAKE_CASE ) # stable unclip will oom when integration tests are run on a V100, # so turn on memory savings pipe.enable_attention_slicing() pipe.enable_sequential_cpu_offload() a_ : Tuple = torch.Generator(device='''cpu''' ).manual_seed(0 ) a_ : Any = pipe(__SCREAMING_SNAKE_CASE , '''anime turle''' , generator=__SCREAMING_SNAKE_CASE , output_type='''np''' ) a_ : int = output.images[0] assert image.shape == (768, 768, 3) assert_mean_pixel_difference(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE ( self : Optional[Any] ) -> List[str]: a_ : Optional[Any] = load_image( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/stable_unclip/turtle.png''' ) a_ : int = load_numpy( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/stable_unclip/stable_unclip_2_1_h_img2img_anime_turtle_fp16.npy''' ) a_ : Optional[Any] = StableUnCLIPImgaImgPipeline.from_pretrained( '''fusing/stable-unclip-2-1-h-img2img''' , torch_dtype=torch.floataa ) pipe.to(__SCREAMING_SNAKE_CASE ) pipe.set_progress_bar_config(disable=__SCREAMING_SNAKE_CASE ) # stable unclip will oom when integration tests are run on a V100, # so turn on memory savings pipe.enable_attention_slicing() pipe.enable_sequential_cpu_offload() a_ : str = torch.Generator(device='''cpu''' ).manual_seed(0 ) a_ : Tuple = pipe(__SCREAMING_SNAKE_CASE , '''anime turle''' , generator=__SCREAMING_SNAKE_CASE , output_type='''np''' ) a_ : Tuple = output.images[0] assert image.shape == (768, 768, 3) assert_mean_pixel_difference(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE ( self : Any ) -> Any: a_ : int = load_image( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/stable_unclip/turtle.png''' ) torch.cuda.empty_cache() torch.cuda.reset_max_memory_allocated() torch.cuda.reset_peak_memory_stats() a_ : Any = StableUnCLIPImgaImgPipeline.from_pretrained( '''fusing/stable-unclip-2-1-h-img2img''' , torch_dtype=torch.floataa ) a_ : List[str] = pipe.to(__SCREAMING_SNAKE_CASE ) pipe.set_progress_bar_config(disable=__SCREAMING_SNAKE_CASE ) pipe.enable_attention_slicing() pipe.enable_sequential_cpu_offload() a_ : List[Any] = pipe( __SCREAMING_SNAKE_CASE , '''anime turtle''' , num_inference_steps=2 , output_type='''np''' , ) a_ : Tuple = torch.cuda.max_memory_allocated() # make sure that less than 7 GB is allocated assert mem_bytes < 7 * 10**9
708
'''simple docstring''' def _UpperCAmelCase ( __A : str , __A : str ): def get_matched_characters(__A : str , __A : str ) -> str: a_ : Union[str, Any] = [] a_ : int = min(len(_stra ) , len(_stra ) ) // 2 for i, l in enumerate(_stra ): a_ : Any = int(max(0 , i - limit ) ) a_ : Union[str, Any] = int(min(i + limit + 1 , len(_stra ) ) ) if l in _stra[left:right]: matched.append(__A ) a_ : Any = f'{_stra[0:_stra.index(__A )]} {_stra[_stra.index(__A ) + 1:]}' return "".join(__A ) # matching characters a_ : Optional[Any] = get_matched_characters(__A , __A ) a_ : int = get_matched_characters(__A , __A ) a_ : Any = len(__A ) # transposition a_ : List[Any] = ( len([(ca, ca) for ca, ca in zip(__A , __A ) if ca != ca] ) // 2 ) if not match_count: a_ : Dict = 0.0 else: a_ : Optional[int] = ( 1 / 3 * ( match_count / len(__A ) + match_count / len(__A ) + (match_count - transpositions) / match_count ) ) # common prefix up to 4 characters a_ : List[str] = 0 for ca, ca in zip(stra[:4] , stra[:4] ): if ca == ca: prefix_len += 1 else: break return jaro + 0.1 * prefix_len * (1 - jaro) if __name__ == "__main__": import doctest doctest.testmod() print(jaro_winkler('hello', 'world'))
666
0
'''simple docstring''' def _UpperCAmelCase ( __A : Tuple , __A : Any ): print('''\nThe shortest path matrix using Floyd Warshall algorithm\n''' ) for i in range(__A ): for j in range(__A ): if dist[i][j] != float('''inf''' ): print(int(dist[i][j] ) , end='''\t''' ) else: print('''INF''' , end='''\t''' ) print() def _UpperCAmelCase ( __A : Dict , __A : Optional[int] ): a_ : Dict = [[float('''inf''' ) for _ in range(__A )] for _ in range(__A )] for i in range(__A ): for j in range(__A ): a_ : Tuple = graph[i][j] # check vertex k against all other vertices (i, j) for k in range(__A ): # looping through rows of graph array for i in range(__A ): # looping through columns of graph array for j in range(__A ): if ( dist[i][k] != float('''inf''' ) and dist[k][j] != float('''inf''' ) and dist[i][k] + dist[k][j] < dist[i][j] ): a_ : Tuple = dist[i][k] + dist[k][j] _print_dist(__A , __A ) return dist, v if __name__ == "__main__": __lowerCAmelCase = int(input('Enter number of vertices: ')) __lowerCAmelCase = int(input('Enter number of edges: ')) __lowerCAmelCase = [[float('inf') for i in range(v)] for j in range(v)] for i in range(v): __lowerCAmelCase = 0.0 # src and dst are indices that must be within the array size graph[e][v] # failure to follow this will result in an error for i in range(e): print('\nEdge ', i + 1) __lowerCAmelCase = int(input('Enter source:')) __lowerCAmelCase = int(input('Enter destination:')) __lowerCAmelCase = float(input('Enter weight:')) __lowerCAmelCase = weight floyd_warshall(graph, v) # Example Input # Enter number of vertices: 3 # Enter number of edges: 2 # # generated graph from vertex and edge inputs # [[inf, inf, inf], [inf, inf, inf], [inf, inf, inf]] # [[0.0, inf, inf], [inf, 0.0, inf], [inf, inf, 0.0]] # specify source, destination and weight for edge #1 # Edge 1 # Enter source:1 # Enter destination:2 # Enter weight:2 # specify source, destination and weight for edge #2 # Edge 2 # Enter source:2 # Enter destination:1 # Enter weight:1 # # Expected Output from the vertice, edge and src, dst, weight inputs!! # 0 INF INF # INF 0 2 # INF 1 0
709
'''simple docstring''' import torch from transformers import AutoModel class SCREAMING_SNAKE_CASE ( torch.nn.Module ): def __init__( self : Optional[int] , __SCREAMING_SNAKE_CASE : int="sayef/fsner-bert-base-uncased" ) -> str: super(__SCREAMING_SNAKE_CASE , self ).__init__() a_ : str = AutoModel.from_pretrained(__SCREAMING_SNAKE_CASE , return_dict=__SCREAMING_SNAKE_CASE ) a_ : Optional[int] = torch.nn.CosineSimilarity(3 , 1e-08 ) a_ : Dict = torch.nn.Softmax(dim=1 ) def SCREAMING_SNAKE_CASE ( self : str , **__SCREAMING_SNAKE_CASE : int ) -> str: return self.bert(**__SCREAMING_SNAKE_CASE ).last_hidden_state def SCREAMING_SNAKE_CASE ( self : List[str] , __SCREAMING_SNAKE_CASE : List[Any] ) -> Optional[int]: return token_embeddings.sum(2 , keepdim=__SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE ( self : List[Any] , __SCREAMING_SNAKE_CASE : Optional[int] , __SCREAMING_SNAKE_CASE : Union[str, Any] , __SCREAMING_SNAKE_CASE : int=1 ) -> Dict: return self.softmax(T * self.cos(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) ) def SCREAMING_SNAKE_CASE ( self : str , __SCREAMING_SNAKE_CASE : Optional[Any] , __SCREAMING_SNAKE_CASE : Optional[int] ) -> Any: a_ : Dict = W_supports['''sizes'''].tolist() a_ : Tuple = W_supports['''start_token_id'''].item() a_ : List[Any] = W_supports['''end_token_id'''].item() del W_supports["sizes"] del W_supports["start_token_id"] del W_supports["end_token_id"] a_ : int = self.BERT(**__SCREAMING_SNAKE_CASE ) a_ : Any = self.BERT(**__SCREAMING_SNAKE_CASE ) a_ : Optional[Any] = None a_ : Tuple = None a_ : List[str] = W_supports['''input_ids'''] == start_token_id a_ : Dict = W_supports['''input_ids'''] == end_token_id for i, size in enumerate(__SCREAMING_SNAKE_CASE ): if i == 0: a_ : str = 0 else: a_ : str = support_sizes[i - 1] a_ : Union[str, Any] = S[s : s + size][start_token_masks[s : s + size]] a_ : Tuple = S[s : s + size][end_token_masks[s : s + size]] a_ : Tuple = torch.matmul(q[i] , s_start.T ).sum(1 ).softmax(0 ) a_ : Optional[Any] = torch.matmul(q[i] , s_end.T ).sum(1 ).softmax(0 ) if p_starts is not None: a_ : Any = torch.vstack((p_starts, p_start) ) a_ : Dict = torch.vstack((p_ends, p_end) ) else: a_ : Optional[int] = p_start a_ : List[Any] = p_end return p_starts, p_ends
666
0
'''simple docstring''' from abc import ABC, abstractmethod from typing import Optional, Union from .. import Dataset, DatasetDict, Features, IterableDataset, IterableDatasetDict, NamedSplit from ..utils.typing import NestedDataStructureLike, PathLike class SCREAMING_SNAKE_CASE ( SCREAMING_SNAKE_CASE_ ): def __init__( self : str , __SCREAMING_SNAKE_CASE : Optional[NestedDataStructureLike[PathLike]] = None , __SCREAMING_SNAKE_CASE : Optional[NamedSplit] = None , __SCREAMING_SNAKE_CASE : Optional[Features] = None , __SCREAMING_SNAKE_CASE : str = None , __SCREAMING_SNAKE_CASE : bool = False , __SCREAMING_SNAKE_CASE : bool = False , __SCREAMING_SNAKE_CASE : Optional[int] = None , **__SCREAMING_SNAKE_CASE : List[str] , ) -> Optional[Any]: a_ : List[str] = path_or_paths a_ : int = split if split or isinstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) else '''train''' a_ : Optional[int] = features a_ : List[Any] = cache_dir a_ : int = keep_in_memory a_ : int = streaming a_ : List[Any] = num_proc a_ : str = kwargs @abstractmethod def SCREAMING_SNAKE_CASE ( self : str ) -> Union[Dataset, DatasetDict, IterableDataset, IterableDatasetDict]: pass class SCREAMING_SNAKE_CASE ( SCREAMING_SNAKE_CASE_ ): def __init__( self : str , __SCREAMING_SNAKE_CASE : Optional[Features] = None , __SCREAMING_SNAKE_CASE : str = None , __SCREAMING_SNAKE_CASE : bool = False , __SCREAMING_SNAKE_CASE : bool = False , __SCREAMING_SNAKE_CASE : Optional[int] = None , **__SCREAMING_SNAKE_CASE : Any , ) -> Tuple: a_ : List[Any] = features a_ : Any = cache_dir a_ : Optional[Any] = keep_in_memory a_ : Tuple = streaming a_ : int = num_proc a_ : Dict = kwargs @abstractmethod def SCREAMING_SNAKE_CASE ( self : Optional[int] ) -> Union[Dataset, IterableDataset]: pass
710
'''simple docstring''' import copy from dataclasses import dataclass, field from typing import ClassVar, Dict from ..features import ClassLabel, Features, Image from .base import TaskTemplate @dataclass(frozen=SCREAMING_SNAKE_CASE_ ) class SCREAMING_SNAKE_CASE ( SCREAMING_SNAKE_CASE_ ): snake_case__ = field(default="image-classification" , metadata={"include_in_asdict_even_if_is_default": True} ) snake_case__ = Features({"image": Image()} ) snake_case__ = Features({"labels": ClassLabel} ) snake_case__ = "image" snake_case__ = "labels" def SCREAMING_SNAKE_CASE ( self : List[str] , __SCREAMING_SNAKE_CASE : List[Any] ) -> Any: if self.label_column not in features: raise ValueError(f'Column {self.label_column} is not present in features.' ) if not isinstance(features[self.label_column] , __SCREAMING_SNAKE_CASE ): raise ValueError(f'Column {self.label_column} is not a ClassLabel.' ) a_ : Optional[int] = copy.deepcopy(self ) a_ : int = self.label_schema.copy() a_ : Tuple = features[self.label_column] a_ : str = label_schema return task_template @property def SCREAMING_SNAKE_CASE ( self : Tuple ) -> Dict[str, str]: return { self.image_column: "image", self.label_column: "labels", }
666
0
'''simple docstring''' from unittest.mock import patch import pyspark from datasets.packaged_modules.spark.spark import ( Spark, SparkExamplesIterable, _generate_iterable_examples, ) from ..utils import ( require_dill_gt_0_3_2, require_not_windows, ) def _UpperCAmelCase ( __A : List[str] , __A : List[Any] ): a_ : Any = [] for part_id in partition_order: a_ : str = df.where(f'SPARK_PARTITION_ID() = {part_id}' ).collect() for row_idx, row in enumerate(__A ): expected_row_ids_and_row_dicts.append((f'{part_id}_{row_idx}', row.asDict()) ) return expected_row_ids_and_row_dicts @require_not_windows @require_dill_gt_0_3_2 def _UpperCAmelCase ( ): a_ : List[str] = pyspark.sql.SparkSession.builder.master('''local[*]''' ).appName('''pyspark''' ).getOrCreate() a_ : Union[str, Any] = spark.range(1_00 ).repartition(1 ) a_ : Any = Spark(__A ) # The id ints will be converted to Pyarrow int64s, so each row will be 8 bytes. Setting a max_shard_size of 16 means # that each partition can hold 2 rows. spark_builder._repartition_df_if_needed(max_shard_size=16 ) # Given that the dataframe has 100 rows and each partition has 2 rows, we expect 50 partitions. assert spark_builder.df.rdd.getNumPartitions() == 50 @require_not_windows @require_dill_gt_0_3_2 def _UpperCAmelCase ( ): a_ : List[Any] = pyspark.sql.SparkSession.builder.master('''local[*]''' ).appName('''pyspark''' ).getOrCreate() a_ : int = spark.range(10 ).repartition(2 ) a_ : Tuple = [1, 0] a_ : List[str] = _generate_iterable_examples(__A , __A ) # Reverse the partitions. a_ : int = _get_expected_row_ids_and_row_dicts_for_partition_order(__A , __A ) for i, (row_id, row_dict) in enumerate(generate_fn() ): a_ : List[Any] = expected_row_ids_and_row_dicts[i] assert row_id == expected_row_id assert row_dict == expected_row_dict @require_not_windows @require_dill_gt_0_3_2 def _UpperCAmelCase ( ): a_ : int = pyspark.sql.SparkSession.builder.master('''local[*]''' ).appName('''pyspark''' ).getOrCreate() a_ : str = spark.range(10 ).repartition(1 ) a_ : Tuple = SparkExamplesIterable(__A ) assert it.n_shards == 1 for i, (row_id, row_dict) in enumerate(__A ): assert row_id == f'0_{i}' assert row_dict == {"id": i} @require_not_windows @require_dill_gt_0_3_2 def _UpperCAmelCase ( ): a_ : Tuple = pyspark.sql.SparkSession.builder.master('''local[*]''' ).appName('''pyspark''' ).getOrCreate() a_ : str = spark.range(30 ).repartition(3 ) # Mock the generator so that shuffle reverses the partition indices. with patch('''numpy.random.Generator''' ) as generator_mock: a_ : Union[str, Any] = lambda __A : x.reverse() a_ : Any = _get_expected_row_ids_and_row_dicts_for_partition_order(__A , [2, 1, 0] ) a_ : str = SparkExamplesIterable(__A ).shuffle_data_sources(__A ) assert shuffled_it.n_shards == 3 for i, (row_id, row_dict) in enumerate(__A ): a_ : Optional[Any] = expected_row_ids_and_row_dicts[i] assert row_id == expected_row_id assert row_dict == expected_row_dict @require_not_windows @require_dill_gt_0_3_2 def _UpperCAmelCase ( ): a_ : int = pyspark.sql.SparkSession.builder.master('''local[*]''' ).appName('''pyspark''' ).getOrCreate() a_ : List[str] = spark.range(20 ).repartition(4 ) # Partitions 0 and 2 a_ : Dict = SparkExamplesIterable(__A ).shard_data_sources(worker_id=0 , num_workers=2 ) assert shard_it_a.n_shards == 2 a_ : Optional[Any] = _get_expected_row_ids_and_row_dicts_for_partition_order(__A , [0, 2] ) for i, (row_id, row_dict) in enumerate(__A ): a_ : Tuple = expected_row_ids_and_row_dicts_a[i] assert row_id == expected_row_id assert row_dict == expected_row_dict # Partitions 1 and 3 a_ : List[Any] = SparkExamplesIterable(__A ).shard_data_sources(worker_id=1 , num_workers=2 ) assert shard_it_a.n_shards == 2 a_ : Optional[int] = _get_expected_row_ids_and_row_dicts_for_partition_order(__A , [1, 3] ) for i, (row_id, row_dict) in enumerate(__A ): a_ : Any = expected_row_ids_and_row_dicts_a[i] assert row_id == expected_row_id assert row_dict == expected_row_dict @require_not_windows @require_dill_gt_0_3_2 def _UpperCAmelCase ( ): a_ : Any = pyspark.sql.SparkSession.builder.master('''local[*]''' ).appName('''pyspark''' ).getOrCreate() a_ : List[Any] = spark.range(1_00 ).repartition(1 ) a_ : Optional[Any] = Spark(__A ) # Choose a small max_shard_size for maximum partitioning. spark_builder._repartition_df_if_needed(max_shard_size=1 ) # The new number of partitions should not be greater than the number of rows. assert spark_builder.df.rdd.getNumPartitions() == 1_00
711
'''simple docstring''' from __future__ import annotations def _UpperCAmelCase ( __A : tuple[int, int] , __A : int ): a_ , a_ : List[str] = position a_ : Optional[int] = [ (y + 1, x + 2), (y - 1, x + 2), (y + 1, x - 2), (y - 1, x - 2), (y + 2, x + 1), (y + 2, x - 1), (y - 2, x + 1), (y - 2, x - 1), ] a_ : Any = [] for position in positions: a_ , a_ : Dict = position if 0 <= y_test < n and 0 <= x_test < n: permissible_positions.append(__A ) return permissible_positions def _UpperCAmelCase ( __A : list[list[int]] ): return not any(elem == 0 for row in board for elem in row ) def _UpperCAmelCase ( __A : list[list[int]] , __A : tuple[int, int] , __A : int ): if is_complete(__A ): return True for position in get_valid_pos(__A , len(__A ) ): a_ , a_ : Dict = position if board[y][x] == 0: a_ : Optional[Any] = curr + 1 if open_knight_tour_helper(__A , __A , curr + 1 ): return True a_ : Tuple = 0 return False def _UpperCAmelCase ( __A : int ): a_ : List[str] = [[0 for i in range(__A )] for j in range(__A )] for i in range(__A ): for j in range(__A ): a_ : Optional[Any] = 1 if open_knight_tour_helper(__A , (i, j) , 1 ): return board a_ : Union[str, Any] = 0 a_ : Dict = f'Open Kight Tour cannot be performed on a board of size {n}' raise ValueError(__A ) if __name__ == "__main__": import doctest doctest.testmod()
666
0
'''simple docstring''' from dataclasses import dataclass, field from typing import TYPE_CHECKING, Any, ClassVar, Dict, List, Optional, Union import pyarrow as pa if TYPE_CHECKING: from .features import FeatureType @dataclass class SCREAMING_SNAKE_CASE : snake_case__ = 42 snake_case__ = None # Automatically constructed snake_case__ = "dict" snake_case__ = None snake_case__ = field(default="Translation" , init=SCREAMING_SNAKE_CASE_ , repr=SCREAMING_SNAKE_CASE_ ) def __call__( self : Dict ) -> Tuple: return pa.struct({lang: pa.string() for lang in sorted(self.languages )} ) def SCREAMING_SNAKE_CASE ( self : Union[str, Any] ) -> Union["FeatureType", Dict[str, "FeatureType"]]: from .features import Value return {k: Value('''string''' ) for k in sorted(self.languages )} @dataclass class SCREAMING_SNAKE_CASE : snake_case__ = None snake_case__ = None snake_case__ = None # Automatically constructed snake_case__ = "dict" snake_case__ = None snake_case__ = field(default="TranslationVariableLanguages" , init=SCREAMING_SNAKE_CASE_ , repr=SCREAMING_SNAKE_CASE_ ) def SCREAMING_SNAKE_CASE ( self : Tuple ) -> Dict: a_ : List[str] = sorted(set(self.languages ) ) if self.languages else None a_ : Optional[Any] = len(self.languages ) if self.languages else None def __call__( self : Any ) -> Optional[Any]: return pa.struct({'''language''': pa.list_(pa.string() ), '''translation''': pa.list_(pa.string() )} ) def SCREAMING_SNAKE_CASE ( self : Optional[int] , __SCREAMING_SNAKE_CASE : Optional[Any] ) -> Optional[Any]: a_ : str = set(self.languages ) if self.languages and set(__SCREAMING_SNAKE_CASE ) - lang_set: raise ValueError( f'Some languages in example ({", ".join(sorted(set(__SCREAMING_SNAKE_CASE ) - lang_set ) )}) are not in valid set ({", ".join(__SCREAMING_SNAKE_CASE )}).' ) # Convert dictionary into tuples, splitting out cases where there are # multiple translations for a single language. a_ : int = [] for lang, text in translation_dict.items(): if isinstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): translation_tuples.append((lang, text) ) else: translation_tuples.extend([(lang, el) for el in text] ) # Ensure translations are in ascending order by language code. a_ : List[Any] = zip(*sorted(__SCREAMING_SNAKE_CASE ) ) return {"language": languages, "translation": translations} def SCREAMING_SNAKE_CASE ( self : Any ) -> Union["FeatureType", Dict[str, "FeatureType"]]: from .features import Sequence, Value return { "language": Sequence(Value('''string''' ) ), "translation": Sequence(Value('''string''' ) ), }
712
'''simple docstring''' import warnings warnings.warn( 'memory_utils has been reorganized to utils.memory. Import `find_executable_batchsize` from the main `__init__`: ' '`from accelerate import find_executable_batch_size` to avoid this warning.', FutureWarning, )
666
0
'''simple docstring''' def _UpperCAmelCase ( __A : int ): a_ : str = int(__A ) if decimal in (0, 1): # Exit cases for the recursion return str(__A ) a_ : Union[str, Any] = divmod(__A , 2 ) return binary_recursive(__A ) + str(__A ) def _UpperCAmelCase ( __A : str ): a_ : Optional[int] = str(__A ).strip() if not number: raise ValueError('''No input value was provided''' ) a_ : List[Any] = '''-''' if number.startswith('''-''' ) else '''''' a_ : Tuple = number.lstrip('''-''' ) if not number.isnumeric(): raise ValueError('''Input value is not an integer''' ) return f'{negative}0b{binary_recursive(int(__A ) )}' if __name__ == "__main__": from doctest import testmod testmod()
713
'''simple docstring''' import requests from bsa import BeautifulSoup def _UpperCAmelCase ( __A : str , __A : dict ): a_ : Tuple = BeautifulSoup(requests.get(__A , params=__A ).content , '''html.parser''' ) a_ : List[str] = soup.find('''div''' , attrs={'''class''': '''gs_ri'''} ) a_ : List[str] = div.find('''div''' , attrs={'''class''': '''gs_fl'''} ).find_all('''a''' ) return anchors[2].get_text() if __name__ == "__main__": __lowerCAmelCase = { 'title': ( 'Precisely geometry controlled microsupercapacitors for ultrahigh areal ' 'capacitance, volumetric capacitance, and energy density' ), 'journal': 'Chem. Mater.', 'volume': 30, 'pages': '3979-3990', 'year': 2_018, 'hl': 'en', } print(get_citation('https://scholar.google.com/scholar_lookup', params=params))
666
0
'''simple docstring''' import math from typing import Union import torch from ..configuration_utils import ConfigMixin, register_to_config from ..utils import randn_tensor from .scheduling_utils import SchedulerMixin class SCREAMING_SNAKE_CASE ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ): snake_case__ = 1 @register_to_config def __init__( self : Optional[int] , __SCREAMING_SNAKE_CASE : Dict=2000 , __SCREAMING_SNAKE_CASE : List[str]=0.1 , __SCREAMING_SNAKE_CASE : List[Any]=20 , __SCREAMING_SNAKE_CASE : List[Any]=1e-3 ) -> Dict: a_ : Any = None a_ : str = None a_ : Tuple = None def SCREAMING_SNAKE_CASE ( self : Optional[int] , __SCREAMING_SNAKE_CASE : Optional[int] , __SCREAMING_SNAKE_CASE : Union[str, torch.device] = None ) -> Union[str, Any]: a_ : Optional[Any] = torch.linspace(1 , self.config.sampling_eps , __SCREAMING_SNAKE_CASE , device=__SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE ( self : Union[str, Any] , __SCREAMING_SNAKE_CASE : Optional[Any] , __SCREAMING_SNAKE_CASE : List[str] , __SCREAMING_SNAKE_CASE : Union[str, Any] , __SCREAMING_SNAKE_CASE : List[Any]=None ) -> List[Any]: if self.timesteps is None: raise ValueError( '''`self.timesteps` is not set, you need to run \'set_timesteps\' after creating the scheduler''' ) # TODO(Patrick) better comments + non-PyTorch # postprocess model score a_ : Any = ( -0.25 * t**2 * (self.config.beta_max - self.config.beta_min) - 0.5 * t * self.config.beta_min ) a_ : List[str] = torch.sqrt(1.0 - torch.exp(2.0 * log_mean_coeff ) ) a_ : Union[str, Any] = std.flatten() while len(std.shape ) < len(score.shape ): a_ : Dict = std.unsqueeze(-1 ) a_ : Dict = -score / std # compute a_ : Any = -1.0 / len(self.timesteps ) a_ : int = self.config.beta_min + t * (self.config.beta_max - self.config.beta_min) a_ : int = beta_t.flatten() while len(beta_t.shape ) < len(x.shape ): a_ : Any = beta_t.unsqueeze(-1 ) a_ : Union[str, Any] = -0.5 * beta_t * x a_ : str = torch.sqrt(__SCREAMING_SNAKE_CASE ) a_ : List[str] = drift - diffusion**2 * score a_ : Dict = x + drift * dt # add noise a_ : Dict = randn_tensor(x.shape , layout=x.layout , generator=__SCREAMING_SNAKE_CASE , device=x.device , dtype=x.dtype ) a_ : Optional[Any] = x_mean + diffusion * math.sqrt(-dt ) * noise return x, x_mean def __len__( self : Optional[Any] ) -> List[str]: return self.config.num_train_timesteps
714
'''simple docstring''' import argparse from torch import nn # transformers_old should correspond to branch `save_old_prophetnet_model_structure` here # original prophetnet_checkpoints are saved under `patrickvonplaten/..._old` respectively from transformers_old.modeling_prophetnet import ( ProphetNetForConditionalGeneration as ProphetNetForConditionalGenerationOld, ) from transformers_old.modeling_xlm_prophetnet import ( XLMProphetNetForConditionalGeneration as XLMProphetNetForConditionalGenerationOld, ) from transformers import ProphetNetForConditionalGeneration, XLMProphetNetForConditionalGeneration, logging __lowerCAmelCase = logging.get_logger(__name__) logging.set_verbosity_info() def _UpperCAmelCase ( __A : str , __A : str ): if "xprophetnet" in prophetnet_checkpoint_path: a_ : Tuple = XLMProphetNetForConditionalGenerationOld.from_pretrained(__A ) a_ , a_ : Optional[Any] = XLMProphetNetForConditionalGeneration.from_pretrained( __A , output_loading_info=__A ) else: a_ : List[Any] = ProphetNetForConditionalGenerationOld.from_pretrained(__A ) a_ , a_ : Any = ProphetNetForConditionalGeneration.from_pretrained( __A , output_loading_info=__A ) a_ : str = ['''key_proj''', '''value_proj''', '''query_proj'''] a_ : Tuple = { '''self_attn''': '''ngram_self_attn''', '''cross_attn''': '''encoder_attn''', '''cross_attn_layer_norm''': '''encoder_attn_layer_norm''', '''feed_forward_layer_norm''': '''final_layer_norm''', '''feed_forward''': '''''', '''intermediate''': '''fc1''', '''output''': '''fc2''', '''key_proj''': '''k_proj''', '''query_proj''': '''q_proj''', '''value_proj''': '''v_proj''', '''word_embeddings''': '''embed_tokens''', '''embeddings_layer_norm''': '''emb_layer_norm''', '''relative_pos_embeddings''': '''relative_linear''', '''ngram_embeddings''': '''ngram_input_embed''', '''position_embeddings''': '''embed_positions''', } for key in loading_info["missing_keys"]: a_ : List[str] = key.split('''.''' ) if attributes[0] == "lm_head": a_ : List[str] = prophet a_ : Dict = prophet_old else: a_ : str = prophet.prophetnet a_ : int = prophet_old.model a_ : str = False for attribute in attributes: if attribute in mapping: a_ : Dict = mapping[attribute] if not hasattr(__A , __A ) and len(__A ) > 0: a_ : List[str] = attribute elif hasattr(__A , __A ): a_ : Union[str, Any] = attribute if attribute == "weight": assert old_model.weight.shape == model.weight.shape, "Shapes have to match!" a_ : Tuple = old_model.weight logger.info(f'{attribute} is initialized.' ) a_ : Union[str, Any] = True break elif attribute == "bias": assert old_model.bias.shape == model.bias.shape, "Shapes have to match!" a_ : Union[str, Any] = old_model.bias logger.info(f'{attribute} is initialized' ) a_ : Dict = True break elif attribute in special_keys and hasattr(__A , '''in_proj_weight''' ): a_ : Tuple = old_model.in_proj_weight.shape[0] // 3 a_ : Any = getattr(__A , __A ) param.weight.shape == old_model.in_proj_weight[:embed_dim, :].shape, "Shapes have to match" param.bias.shape == old_model.in_proj_bias[:embed_dim].shape, "Shapes have to match" if attribute == "query_proj": a_ : Union[str, Any] = nn.Parameter(old_model.in_proj_weight[:embed_dim, :] ) a_ : Optional[Any] = nn.Parameter(old_model.in_proj_bias[:embed_dim] ) elif attribute == "key_proj": a_ : List[Any] = nn.Parameter(old_model.in_proj_weight[embed_dim : 2 * embed_dim, :] ) a_ : Optional[int] = nn.Parameter(old_model.in_proj_bias[embed_dim : 2 * embed_dim] ) elif attribute == "value_proj": a_ : Tuple = nn.Parameter(old_model.in_proj_weight[2 * embed_dim :, :] ) a_ : Any = nn.Parameter(old_model.in_proj_bias[2 * embed_dim :] ) a_ : Dict = True break elif attribute == "position_embeddings": assert ( model.position_embeddings.weight.shape[-1] == old_model.embed_positions.weight.shape[-1] ), "Hidden size has to match" assert model.position_embeddings.weight.shape[0] == 5_12, "We want 512 position_embeddings." a_ : Union[str, Any] = nn.Parameter(old_model.embed_positions.weight[:5_12, :] ) a_ : Optional[Any] = True break if attribute.isdigit(): a_ : Union[str, Any] = model[int(__A )] a_ : str = old_model[int(__A )] else: a_ : Tuple = getattr(__A , __A ) if old_attribute == "": a_ : List[str] = old_model else: if not hasattr(__A , __A ): raise ValueError(f'{old_model} does not have {old_attribute}' ) a_ : Optional[Any] = getattr(__A , __A ) if not is_key_init: raise ValueError(f'{key} was not correctly initialized!' ) print(f'Saving model to {pytorch_dump_folder_path}' ) prophet.save_pretrained(__A ) if __name__ == "__main__": __lowerCAmelCase = argparse.ArgumentParser() # Required parameters parser.add_argument( '--prophetnet_checkpoint_path', default=None, type=str, required=True, help='Path the official PyTorch dump.' ) parser.add_argument( '--pytorch_dump_folder_path', default=None, type=str, required=True, help='Path to the output PyTorch model.' ) __lowerCAmelCase = parser.parse_args() convert_prophetnet_checkpoint_to_pytorch(args.prophetnet_checkpoint_path, args.pytorch_dump_folder_path)
666
0
import sys import webbrowser import requests from bsa import BeautifulSoup from fake_useragent import UserAgent if __name__ == "__main__": print('Googling.....') __lowerCAmelCase = 'https://www.google.com/search?q=' + ' '.join(sys.argv[1:]) __lowerCAmelCase = requests.get(url, headers={'UserAgent': UserAgent().random}) # res.raise_for_status() with open('project1a.html', 'wb') as out_file: # only for knowing the class for data in res.iter_content(10_000): out_file.write(data) __lowerCAmelCase = BeautifulSoup(res.text, 'html.parser') __lowerCAmelCase = list(soup.select('.eZt8xd'))[:5] print(len(links)) for link in links: if link.text == "Maps": webbrowser.open(link.get('href')) else: webbrowser.open(F"""https://google.com{link.get('href')}""")
715
'''simple docstring''' import re import string import numpy as np import datasets __lowerCAmelCase = '\nReturns the rate at which the input predicted strings exactly match their references, ignoring any strings input as part of the regexes_to_ignore list.\n' __lowerCAmelCase = '\nArgs:\n predictions: List of predicted texts.\n references: List of reference texts.\n regexes_to_ignore: List, defaults to None. Regex expressions of characters to\n ignore when calculating the exact matches. Note: these regexes are removed\n from the input data before the changes based on the options below (e.g. ignore_case,\n ignore_punctuation, ignore_numbers) are applied.\n ignore_case: Boolean, defaults to False. If true, turns everything\n to lowercase so that capitalization differences are ignored.\n ignore_punctuation: Boolean, defaults to False. If true, removes all punctuation before\n comparing predictions and references.\n ignore_numbers: Boolean, defaults to False. If true, removes all punctuation before\n comparing predictions and references.\nReturns:\n exact_match: Dictionary containing exact_match rate. Possible values are between 0.0 and 100.0, inclusive.\nExamples:\n >>> exact_match = datasets.load_metric("exact_match")\n >>> refs = ["the cat", "theater", "YELLING", "agent007"]\n >>> preds = ["cat?", "theater", "yelling", "agent"]\n >>> results = exact_match.compute(references=refs, predictions=preds)\n >>> print(round(results["exact_match"], 1))\n 25.0\n\n >>> exact_match = datasets.load_metric("exact_match")\n >>> refs = ["the cat", "theater", "YELLING", "agent007"]\n >>> preds = ["cat?", "theater", "yelling", "agent"]\n >>> results = exact_match.compute(references=refs, predictions=preds, regexes_to_ignore=["the ", "yell"], ignore_case=True, ignore_punctuation=True)\n >>> print(round(results["exact_match"], 1))\n 50.0\n\n\n >>> exact_match = datasets.load_metric("exact_match")\n >>> refs = ["the cat", "theater", "YELLING", "agent007"]\n >>> preds = ["cat?", "theater", "yelling", "agent"]\n >>> results = exact_match.compute(references=refs, predictions=preds, regexes_to_ignore=["the ", "yell", "YELL"], ignore_case=True, ignore_punctuation=True)\n >>> print(round(results["exact_match"], 1))\n 75.0\n\n >>> exact_match = datasets.load_metric("exact_match")\n >>> refs = ["the cat", "theater", "YELLING", "agent007"]\n >>> preds = ["cat?", "theater", "yelling", "agent"]\n >>> results = exact_match.compute(references=refs, predictions=preds, regexes_to_ignore=["the ", "yell", "YELL"], ignore_case=True, ignore_punctuation=True, ignore_numbers=True)\n >>> print(round(results["exact_match"], 1))\n 100.0\n\n >>> exact_match = datasets.load_metric("exact_match")\n >>> refs = ["The cat sat on the mat.", "Theaters are great.", "It\'s like comparing oranges and apples."]\n >>> preds = ["The cat sat on the mat?", "Theaters are great.", "It\'s like comparing apples and oranges."]\n >>> results = exact_match.compute(references=refs, predictions=preds)\n >>> print(round(results["exact_match"], 1))\n 33.3\n\n' __lowerCAmelCase = '\n' @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class SCREAMING_SNAKE_CASE ( datasets.Metric ): def SCREAMING_SNAKE_CASE ( self : Optional[Any] ) -> int: return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { '''predictions''': datasets.Value('''string''' , id='''sequence''' ), '''references''': datasets.Value('''string''' , id='''sequence''' ), } ) , reference_urls=[] , ) def SCREAMING_SNAKE_CASE ( self : str , __SCREAMING_SNAKE_CASE : List[Any] , __SCREAMING_SNAKE_CASE : Optional[int] , __SCREAMING_SNAKE_CASE : Dict=None , __SCREAMING_SNAKE_CASE : int=False , __SCREAMING_SNAKE_CASE : Optional[int]=False , __SCREAMING_SNAKE_CASE : Dict=False , ) -> str: if regexes_to_ignore is not None: for s in regexes_to_ignore: a_ : Optional[Any] = np.array([re.sub(__SCREAMING_SNAKE_CASE , '''''' , __SCREAMING_SNAKE_CASE ) for x in predictions] ) a_ : int = np.array([re.sub(__SCREAMING_SNAKE_CASE , '''''' , __SCREAMING_SNAKE_CASE ) for x in references] ) else: a_ : List[str] = np.asarray(__SCREAMING_SNAKE_CASE ) a_ : Any = np.asarray(__SCREAMING_SNAKE_CASE ) if ignore_case: a_ : List[str] = np.char.lower(__SCREAMING_SNAKE_CASE ) a_ : List[Any] = np.char.lower(__SCREAMING_SNAKE_CASE ) if ignore_punctuation: a_ : Any = string.punctuation.maketrans('''''' , '''''' , string.punctuation ) a_ : Union[str, Any] = np.char.translate(__SCREAMING_SNAKE_CASE , table=__SCREAMING_SNAKE_CASE ) a_ : int = np.char.translate(__SCREAMING_SNAKE_CASE , table=__SCREAMING_SNAKE_CASE ) if ignore_numbers: a_ : int = string.digits.maketrans('''''' , '''''' , string.digits ) a_ : Optional[int] = np.char.translate(__SCREAMING_SNAKE_CASE , table=__SCREAMING_SNAKE_CASE ) a_ : Dict = np.char.translate(__SCREAMING_SNAKE_CASE , table=__SCREAMING_SNAKE_CASE ) a_ : Optional[Any] = predictions == references return {"exact_match": np.mean(__SCREAMING_SNAKE_CASE ) * 100}
666
0
'''simple docstring''' import functools def _UpperCAmelCase ( __A : list[int] , __A : list[int] ): # Validation if not isinstance(__A , __A ) or not all(isinstance(__A , __A ) for day in days ): raise ValueError('''The parameter days should be a list of integers''' ) if len(__A ) != 3 or not all(isinstance(__A , __A ) for cost in costs ): raise ValueError('''The parameter costs should be a list of three integers''' ) if len(__A ) == 0: return 0 if min(__A ) <= 0: raise ValueError('''All days elements should be greater than 0''' ) if max(__A ) >= 3_66: raise ValueError('''All days elements should be less than 366''' ) a_ : int = set(__A ) @functools.cache def dynamic_programming(__A : int ) -> int: if index > 3_65: return 0 if index not in days_set: return dynamic_programming(index + 1 ) return min( costs[0] + dynamic_programming(index + 1 ) , costs[1] + dynamic_programming(index + 7 ) , costs[2] + dynamic_programming(index + 30 ) , ) return dynamic_programming(1 ) if __name__ == "__main__": import doctest doctest.testmod()
716
'''simple docstring''' import gc import unittest import numpy as np import torch from diffusers import ( AudioDiffusionPipeline, AutoencoderKL, DDIMScheduler, DDPMScheduler, DiffusionPipeline, Mel, UNetaDConditionModel, UNetaDModel, ) from diffusers.utils import slow, torch_device from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu enable_full_determinism() class SCREAMING_SNAKE_CASE ( unittest.TestCase ): def SCREAMING_SNAKE_CASE ( self : Dict ) -> List[Any]: # clean up the VRAM after each test super().tearDown() gc.collect() torch.cuda.empty_cache() @property def SCREAMING_SNAKE_CASE ( self : Tuple ) -> List[Any]: torch.manual_seed(0 ) a_ : Tuple = UNetaDModel( sample_size=(32, 64) , in_channels=1 , out_channels=1 , layers_per_block=2 , block_out_channels=(128, 128) , down_block_types=('''AttnDownBlock2D''', '''DownBlock2D''') , up_block_types=('''UpBlock2D''', '''AttnUpBlock2D''') , ) return model @property def SCREAMING_SNAKE_CASE ( self : int ) -> List[Any]: torch.manual_seed(0 ) a_ : Any = UNetaDConditionModel( sample_size=(64, 32) , in_channels=1 , out_channels=1 , layers_per_block=2 , block_out_channels=(128, 128) , down_block_types=('''CrossAttnDownBlock2D''', '''DownBlock2D''') , up_block_types=('''UpBlock2D''', '''CrossAttnUpBlock2D''') , cross_attention_dim=10 , ) return model @property def SCREAMING_SNAKE_CASE ( self : Optional[Any] ) -> List[str]: torch.manual_seed(0 ) a_ : List[Any] = AutoencoderKL( sample_size=(128, 64) , in_channels=1 , out_channels=1 , latent_channels=1 , layers_per_block=2 , block_out_channels=(128, 128) , down_block_types=('''DownEncoderBlock2D''', '''DownEncoderBlock2D''') , up_block_types=('''UpDecoderBlock2D''', '''UpDecoderBlock2D''') , ) a_ : List[Any] = UNetaDModel( sample_size=(64, 32) , in_channels=1 , out_channels=1 , layers_per_block=2 , block_out_channels=(128, 128) , down_block_types=('''AttnDownBlock2D''', '''DownBlock2D''') , up_block_types=('''UpBlock2D''', '''AttnUpBlock2D''') , ) return vqvae, unet @slow def SCREAMING_SNAKE_CASE ( self : Union[str, Any] ) -> Union[str, Any]: a_ : Dict = '''cpu''' # ensure determinism for the device-dependent torch.Generator a_ : Union[str, Any] = Mel( x_res=self.dummy_unet.config.sample_size[1] , y_res=self.dummy_unet.config.sample_size[0] , ) a_ : Any = DDPMScheduler() a_ : str = AudioDiffusionPipeline(vqvae=__SCREAMING_SNAKE_CASE , unet=self.dummy_unet , mel=__SCREAMING_SNAKE_CASE , scheduler=__SCREAMING_SNAKE_CASE ) a_ : List[Any] = pipe.to(__SCREAMING_SNAKE_CASE ) pipe.set_progress_bar_config(disable=__SCREAMING_SNAKE_CASE ) a_ : Optional[int] = torch.Generator(device=__SCREAMING_SNAKE_CASE ).manual_seed(42 ) a_ : List[str] = pipe(generator=__SCREAMING_SNAKE_CASE , steps=4 ) a_ : List[Any] = output.audios[0] a_ : Dict = output.images[0] a_ : Dict = torch.Generator(device=__SCREAMING_SNAKE_CASE ).manual_seed(42 ) a_ : Optional[Any] = pipe(generator=__SCREAMING_SNAKE_CASE , steps=4 , return_dict=__SCREAMING_SNAKE_CASE ) a_ : Union[str, Any] = output[0][0] assert audio.shape == (1, (self.dummy_unet.config.sample_size[1] - 1) * mel.hop_length) assert ( image.height == self.dummy_unet.config.sample_size[0] and image.width == self.dummy_unet.config.sample_size[1] ) a_ : Dict = np.frombuffer(image.tobytes() , dtype='''uint8''' )[:10] a_ : str = np.frombuffer(image_from_tuple.tobytes() , dtype='''uint8''' )[:10] a_ : List[str] = np.array([69, 255, 255, 255, 0, 0, 77, 181, 12, 127] ) assert np.abs(image_slice.flatten() - expected_slice ).max() == 0 assert np.abs(image_from_tuple_slice.flatten() - expected_slice ).max() == 0 a_ : str = Mel( x_res=self.dummy_vqvae_and_unet[0].config.sample_size[1] , y_res=self.dummy_vqvae_and_unet[0].config.sample_size[0] , ) a_ : int = DDIMScheduler() a_ : Dict = self.dummy_vqvae_and_unet a_ : List[str] = AudioDiffusionPipeline( vqvae=self.dummy_vqvae_and_unet[0] , unet=dummy_vqvae_and_unet[1] , mel=__SCREAMING_SNAKE_CASE , scheduler=__SCREAMING_SNAKE_CASE ) a_ : Any = pipe.to(__SCREAMING_SNAKE_CASE ) pipe.set_progress_bar_config(disable=__SCREAMING_SNAKE_CASE ) np.random.seed(0 ) a_ : List[str] = np.random.uniform(-1 , 1 , ((dummy_vqvae_and_unet[0].config.sample_size[1] - 1) * mel.hop_length,) ) a_ : int = torch.Generator(device=__SCREAMING_SNAKE_CASE ).manual_seed(42 ) a_ : int = pipe(raw_audio=__SCREAMING_SNAKE_CASE , generator=__SCREAMING_SNAKE_CASE , start_step=5 , steps=10 ) a_ : List[str] = output.images[0] assert ( image.height == self.dummy_vqvae_and_unet[0].config.sample_size[0] and image.width == self.dummy_vqvae_and_unet[0].config.sample_size[1] ) a_ : Optional[Any] = np.frombuffer(image.tobytes() , dtype='''uint8''' )[:10] a_ : List[str] = np.array([120, 117, 110, 109, 138, 167, 138, 148, 132, 121] ) assert np.abs(image_slice.flatten() - expected_slice ).max() == 0 a_ : List[str] = self.dummy_unet_condition a_ : Dict = AudioDiffusionPipeline( vqvae=self.dummy_vqvae_and_unet[0] , unet=__SCREAMING_SNAKE_CASE , mel=__SCREAMING_SNAKE_CASE , scheduler=__SCREAMING_SNAKE_CASE ) a_ : int = pipe.to(__SCREAMING_SNAKE_CASE ) pipe.set_progress_bar_config(disable=__SCREAMING_SNAKE_CASE ) np.random.seed(0 ) a_ : Any = torch.rand((1, 1, 10) ) a_ : Tuple = pipe(generator=__SCREAMING_SNAKE_CASE , encoding=__SCREAMING_SNAKE_CASE ) a_ : Union[str, Any] = output.images[0] a_ : Dict = np.frombuffer(image.tobytes() , dtype='''uint8''' )[:10] a_ : List[str] = np.array([107, 103, 120, 127, 142, 122, 113, 122, 97, 111] ) assert np.abs(image_slice.flatten() - expected_slice ).max() == 0 @slow @require_torch_gpu class SCREAMING_SNAKE_CASE ( unittest.TestCase ): def SCREAMING_SNAKE_CASE ( self : List[str] ) -> Any: # clean up the VRAM after each test super().tearDown() gc.collect() torch.cuda.empty_cache() def SCREAMING_SNAKE_CASE ( self : Optional[Any] ) -> Any: a_ : Any = torch_device a_ : Optional[int] = DiffusionPipeline.from_pretrained('''teticio/audio-diffusion-ddim-256''' ) a_ : Dict = pipe.to(__SCREAMING_SNAKE_CASE ) pipe.set_progress_bar_config(disable=__SCREAMING_SNAKE_CASE ) a_ : str = torch.Generator(device=__SCREAMING_SNAKE_CASE ).manual_seed(42 ) a_ : List[str] = pipe(generator=__SCREAMING_SNAKE_CASE ) a_ : Union[str, Any] = output.audios[0] a_ : Tuple = output.images[0] assert audio.shape == (1, (pipe.unet.config.sample_size[1] - 1) * pipe.mel.hop_length) assert image.height == pipe.unet.config.sample_size[0] and image.width == pipe.unet.config.sample_size[1] a_ : str = np.frombuffer(image.tobytes() , dtype='''uint8''' )[:10] a_ : Tuple = np.array([151, 167, 154, 144, 122, 134, 121, 105, 70, 26] ) assert np.abs(image_slice.flatten() - expected_slice ).max() == 0
666
0
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available __lowerCAmelCase = { 'configuration_timesformer': ['TIMESFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP', 'TimesformerConfig'], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __lowerCAmelCase = [ 'TIMESFORMER_PRETRAINED_MODEL_ARCHIVE_LIST', 'TimesformerModel', 'TimesformerForVideoClassification', 'TimesformerPreTrainedModel', ] if TYPE_CHECKING: from .configuration_timesformer import TIMESFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP, TimesformerConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_timesformer import ( TIMESFORMER_PRETRAINED_MODEL_ARCHIVE_LIST, TimesformerForVideoClassification, TimesformerModel, TimesformerPreTrainedModel, ) else: import sys __lowerCAmelCase = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
717
'''simple docstring''' import argparse import json from pathlib import Path import requests import torch from huggingface_hub import cached_download, hf_hub_download, hf_hub_url from PIL import Image from transformers import DetaConfig, DetaForObjectDetection, DetaImageProcessor, SwinConfig from transformers.utils import logging logging.set_verbosity_info() __lowerCAmelCase = logging.get_logger(__name__) def _UpperCAmelCase ( __A : Union[str, Any] ): a_ : Tuple = SwinConfig( embed_dim=1_92 , depths=(2, 2, 18, 2) , num_heads=(6, 12, 24, 48) , window_size=12 , out_features=['''stage2''', '''stage3''', '''stage4'''] , ) a_ : List[Any] = DetaConfig( backbone_config=__A , num_queries=9_00 , encoder_ffn_dim=20_48 , decoder_ffn_dim=20_48 , num_feature_levels=5 , assign_first_stage=__A , with_box_refine=__A , two_stage=__A , ) # set labels a_ : Optional[Any] = '''huggingface/label-files''' if "o365" in model_name: a_ : Optional[Any] = 3_66 a_ : Tuple = '''object365-id2label.json''' else: a_ : Any = 91 a_ : Union[str, Any] = '''coco-detection-id2label.json''' a_ : Tuple = num_labels a_ : str = json.load(open(cached_download(hf_hub_url(__A , __A , repo_type='''dataset''' ) ) , '''r''' ) ) a_ : Optional[int] = {int(__A ): v for k, v in idalabel.items()} a_ : int = idalabel a_ : Dict = {v: k for k, v in idalabel.items()} return config def _UpperCAmelCase ( __A : List[str] ): a_ : Tuple = [] # stem # fmt: off rename_keys.append(('''backbone.0.body.patch_embed.proj.weight''', '''model.backbone.model.embeddings.patch_embeddings.projection.weight''') ) rename_keys.append(('''backbone.0.body.patch_embed.proj.bias''', '''model.backbone.model.embeddings.patch_embeddings.projection.bias''') ) rename_keys.append(('''backbone.0.body.patch_embed.norm.weight''', '''model.backbone.model.embeddings.norm.weight''') ) rename_keys.append(('''backbone.0.body.patch_embed.norm.bias''', '''model.backbone.model.embeddings.norm.bias''') ) # stages for i in range(len(config.backbone_config.depths ) ): for j in range(config.backbone_config.depths[i] ): rename_keys.append((f'backbone.0.body.layers.{i}.blocks.{j}.norm1.weight', f'model.backbone.model.encoder.layers.{i}.blocks.{j}.layernorm_before.weight') ) rename_keys.append((f'backbone.0.body.layers.{i}.blocks.{j}.norm1.bias', f'model.backbone.model.encoder.layers.{i}.blocks.{j}.layernorm_before.bias') ) rename_keys.append((f'backbone.0.body.layers.{i}.blocks.{j}.attn.relative_position_bias_table', f'model.backbone.model.encoder.layers.{i}.blocks.{j}.attention.self.relative_position_bias_table') ) rename_keys.append((f'backbone.0.body.layers.{i}.blocks.{j}.attn.relative_position_index', f'model.backbone.model.encoder.layers.{i}.blocks.{j}.attention.self.relative_position_index') ) rename_keys.append((f'backbone.0.body.layers.{i}.blocks.{j}.attn.proj.weight', f'model.backbone.model.encoder.layers.{i}.blocks.{j}.attention.output.dense.weight') ) rename_keys.append((f'backbone.0.body.layers.{i}.blocks.{j}.attn.proj.bias', f'model.backbone.model.encoder.layers.{i}.blocks.{j}.attention.output.dense.bias') ) rename_keys.append((f'backbone.0.body.layers.{i}.blocks.{j}.norm2.weight', f'model.backbone.model.encoder.layers.{i}.blocks.{j}.layernorm_after.weight') ) rename_keys.append((f'backbone.0.body.layers.{i}.blocks.{j}.norm2.bias', f'model.backbone.model.encoder.layers.{i}.blocks.{j}.layernorm_after.bias') ) rename_keys.append((f'backbone.0.body.layers.{i}.blocks.{j}.mlp.fc1.weight', f'model.backbone.model.encoder.layers.{i}.blocks.{j}.intermediate.dense.weight') ) rename_keys.append((f'backbone.0.body.layers.{i}.blocks.{j}.mlp.fc1.bias', f'model.backbone.model.encoder.layers.{i}.blocks.{j}.intermediate.dense.bias') ) rename_keys.append((f'backbone.0.body.layers.{i}.blocks.{j}.mlp.fc2.weight', f'model.backbone.model.encoder.layers.{i}.blocks.{j}.output.dense.weight') ) rename_keys.append((f'backbone.0.body.layers.{i}.blocks.{j}.mlp.fc2.bias', f'model.backbone.model.encoder.layers.{i}.blocks.{j}.output.dense.bias') ) if i < 3: rename_keys.append((f'backbone.0.body.layers.{i}.downsample.reduction.weight', f'model.backbone.model.encoder.layers.{i}.downsample.reduction.weight') ) rename_keys.append((f'backbone.0.body.layers.{i}.downsample.norm.weight', f'model.backbone.model.encoder.layers.{i}.downsample.norm.weight') ) rename_keys.append((f'backbone.0.body.layers.{i}.downsample.norm.bias', f'model.backbone.model.encoder.layers.{i}.downsample.norm.bias') ) rename_keys.append(('''backbone.0.body.norm1.weight''', '''model.backbone.model.hidden_states_norms.stage2.weight''') ) rename_keys.append(('''backbone.0.body.norm1.bias''', '''model.backbone.model.hidden_states_norms.stage2.bias''') ) rename_keys.append(('''backbone.0.body.norm2.weight''', '''model.backbone.model.hidden_states_norms.stage3.weight''') ) rename_keys.append(('''backbone.0.body.norm2.bias''', '''model.backbone.model.hidden_states_norms.stage3.bias''') ) rename_keys.append(('''backbone.0.body.norm3.weight''', '''model.backbone.model.hidden_states_norms.stage4.weight''') ) rename_keys.append(('''backbone.0.body.norm3.bias''', '''model.backbone.model.hidden_states_norms.stage4.bias''') ) # transformer encoder for i in range(config.encoder_layers ): rename_keys.append((f'transformer.encoder.layers.{i}.self_attn.sampling_offsets.weight', f'model.encoder.layers.{i}.self_attn.sampling_offsets.weight') ) rename_keys.append((f'transformer.encoder.layers.{i}.self_attn.sampling_offsets.bias', f'model.encoder.layers.{i}.self_attn.sampling_offsets.bias') ) rename_keys.append((f'transformer.encoder.layers.{i}.self_attn.attention_weights.weight', f'model.encoder.layers.{i}.self_attn.attention_weights.weight') ) rename_keys.append((f'transformer.encoder.layers.{i}.self_attn.attention_weights.bias', f'model.encoder.layers.{i}.self_attn.attention_weights.bias') ) rename_keys.append((f'transformer.encoder.layers.{i}.self_attn.value_proj.weight', f'model.encoder.layers.{i}.self_attn.value_proj.weight') ) rename_keys.append((f'transformer.encoder.layers.{i}.self_attn.value_proj.bias', f'model.encoder.layers.{i}.self_attn.value_proj.bias') ) rename_keys.append((f'transformer.encoder.layers.{i}.self_attn.output_proj.weight', f'model.encoder.layers.{i}.self_attn.output_proj.weight') ) rename_keys.append((f'transformer.encoder.layers.{i}.self_attn.output_proj.bias', f'model.encoder.layers.{i}.self_attn.output_proj.bias') ) rename_keys.append((f'transformer.encoder.layers.{i}.norm1.weight', f'model.encoder.layers.{i}.self_attn_layer_norm.weight') ) rename_keys.append((f'transformer.encoder.layers.{i}.norm1.bias', f'model.encoder.layers.{i}.self_attn_layer_norm.bias') ) rename_keys.append((f'transformer.encoder.layers.{i}.linear1.weight', f'model.encoder.layers.{i}.fc1.weight') ) rename_keys.append((f'transformer.encoder.layers.{i}.linear1.bias', f'model.encoder.layers.{i}.fc1.bias') ) rename_keys.append((f'transformer.encoder.layers.{i}.linear2.weight', f'model.encoder.layers.{i}.fc2.weight') ) rename_keys.append((f'transformer.encoder.layers.{i}.linear2.bias', f'model.encoder.layers.{i}.fc2.bias') ) rename_keys.append((f'transformer.encoder.layers.{i}.norm2.weight', f'model.encoder.layers.{i}.final_layer_norm.weight') ) rename_keys.append((f'transformer.encoder.layers.{i}.norm2.bias', f'model.encoder.layers.{i}.final_layer_norm.bias') ) # transformer decoder for i in range(config.decoder_layers ): rename_keys.append((f'transformer.decoder.layers.{i}.cross_attn.sampling_offsets.weight', f'model.decoder.layers.{i}.encoder_attn.sampling_offsets.weight') ) rename_keys.append((f'transformer.decoder.layers.{i}.cross_attn.sampling_offsets.bias', f'model.decoder.layers.{i}.encoder_attn.sampling_offsets.bias') ) rename_keys.append((f'transformer.decoder.layers.{i}.cross_attn.attention_weights.weight', f'model.decoder.layers.{i}.encoder_attn.attention_weights.weight') ) rename_keys.append((f'transformer.decoder.layers.{i}.cross_attn.attention_weights.bias', f'model.decoder.layers.{i}.encoder_attn.attention_weights.bias') ) rename_keys.append((f'transformer.decoder.layers.{i}.cross_attn.value_proj.weight', f'model.decoder.layers.{i}.encoder_attn.value_proj.weight') ) rename_keys.append((f'transformer.decoder.layers.{i}.cross_attn.value_proj.bias', f'model.decoder.layers.{i}.encoder_attn.value_proj.bias') ) rename_keys.append((f'transformer.decoder.layers.{i}.cross_attn.output_proj.weight', f'model.decoder.layers.{i}.encoder_attn.output_proj.weight') ) rename_keys.append((f'transformer.decoder.layers.{i}.cross_attn.output_proj.bias', f'model.decoder.layers.{i}.encoder_attn.output_proj.bias') ) rename_keys.append((f'transformer.decoder.layers.{i}.norm1.weight', f'model.decoder.layers.{i}.encoder_attn_layer_norm.weight') ) rename_keys.append((f'transformer.decoder.layers.{i}.norm1.bias', f'model.decoder.layers.{i}.encoder_attn_layer_norm.bias') ) rename_keys.append((f'transformer.decoder.layers.{i}.self_attn.out_proj.weight', f'model.decoder.layers.{i}.self_attn.out_proj.weight') ) rename_keys.append((f'transformer.decoder.layers.{i}.self_attn.out_proj.bias', f'model.decoder.layers.{i}.self_attn.out_proj.bias') ) rename_keys.append((f'transformer.decoder.layers.{i}.norm2.weight', f'model.decoder.layers.{i}.self_attn_layer_norm.weight') ) rename_keys.append((f'transformer.decoder.layers.{i}.norm2.bias', f'model.decoder.layers.{i}.self_attn_layer_norm.bias') ) rename_keys.append((f'transformer.decoder.layers.{i}.linear1.weight', f'model.decoder.layers.{i}.fc1.weight') ) rename_keys.append((f'transformer.decoder.layers.{i}.linear1.bias', f'model.decoder.layers.{i}.fc1.bias') ) rename_keys.append((f'transformer.decoder.layers.{i}.linear2.weight', f'model.decoder.layers.{i}.fc2.weight') ) rename_keys.append((f'transformer.decoder.layers.{i}.linear2.bias', f'model.decoder.layers.{i}.fc2.bias') ) rename_keys.append((f'transformer.decoder.layers.{i}.norm3.weight', f'model.decoder.layers.{i}.final_layer_norm.weight') ) rename_keys.append((f'transformer.decoder.layers.{i}.norm3.bias', f'model.decoder.layers.{i}.final_layer_norm.bias') ) # fmt: on return rename_keys def _UpperCAmelCase ( __A : str , __A : int , __A : Tuple ): a_ : str = dct.pop(__A ) a_ : Dict = val def _UpperCAmelCase ( __A : List[str] , __A : Optional[int] ): a_ : str = [int(backbone_config.embed_dim * 2**i ) for i in range(len(backbone_config.depths ) )] for i in range(len(backbone_config.depths ) ): a_ : Tuple = num_features[i] for j in range(backbone_config.depths[i] ): # fmt: off # read in weights + bias of input projection layer (in original implementation, this is a single matrix + bias) a_ : List[str] = state_dict.pop(f'backbone.0.body.layers.{i}.blocks.{j}.attn.qkv.weight' ) a_ : str = state_dict.pop(f'backbone.0.body.layers.{i}.blocks.{j}.attn.qkv.bias' ) # next, add query, keys and values (in that order) to the state dict a_ : Optional[Any] = in_proj_weight[:dim, :] a_ : List[Any] = in_proj_bias[: dim] a_ : Optional[Any] = in_proj_weight[ dim : dim * 2, : ] a_ : Union[str, Any] = in_proj_bias[ dim : dim * 2 ] a_ : Optional[int] = in_proj_weight[ -dim :, : ] a_ : int = in_proj_bias[-dim :] # fmt: on def _UpperCAmelCase ( __A : Dict , __A : Dict ): # transformer decoder self-attention layers a_ : Any = config.d_model for i in range(config.decoder_layers ): # read in weights + bias of input projection layer of self-attention a_ : int = state_dict.pop(f'transformer.decoder.layers.{i}.self_attn.in_proj_weight' ) a_ : Any = state_dict.pop(f'transformer.decoder.layers.{i}.self_attn.in_proj_bias' ) # next, add query, keys and values (in that order) to the state dict a_ : Dict = in_proj_weight[:hidden_size, :] a_ : Tuple = in_proj_bias[:hidden_size] a_ : Any = in_proj_weight[ hidden_size : hidden_size * 2, : ] a_ : Tuple = in_proj_bias[hidden_size : hidden_size * 2] a_ : Optional[int] = in_proj_weight[-hidden_size:, :] a_ : int = in_proj_bias[-hidden_size:] def _UpperCAmelCase ( ): a_ : Union[str, Any] = '''http://images.cocodataset.org/val2017/000000039769.jpg''' a_ : List[str] = Image.open(requests.get(__A , stream=__A ).raw ) return im @torch.no_grad() def _UpperCAmelCase ( __A : int , __A : int , __A : Any ): a_ : Union[str, Any] = get_deta_config(__A ) # load original state dict if model_name == "deta-swin-large": a_ : Optional[Any] = hf_hub_download(repo_id='''nielsr/deta-checkpoints''' , filename='''adet_swin_ft.pth''' ) elif model_name == "deta-swin-large-o365": a_ : List[str] = hf_hub_download(repo_id='''jozhang97/deta-swin-l-o365''' , filename='''deta_swin_pt_o365.pth''' ) else: raise ValueError(f'Model name {model_name} not supported' ) a_ : List[Any] = torch.load(__A , map_location='''cpu''' )['''model'''] # original state dict for name, param in state_dict.items(): print(__A , param.shape ) # rename keys a_ : Union[str, Any] = create_rename_keys(__A ) for src, dest in rename_keys: rename_key(__A , __A , __A ) read_in_swin_q_k_v(__A , config.backbone_config ) read_in_decoder_q_k_v(__A , __A ) # fix some prefixes for key in state_dict.copy().keys(): if "transformer.decoder.class_embed" in key or "transformer.decoder.bbox_embed" in key: a_ : Optional[Any] = state_dict.pop(__A ) a_ : int = val if "input_proj" in key: a_ : str = state_dict.pop(__A ) a_ : Optional[Any] = val if "level_embed" in key or "pos_trans" in key or "pix_trans" in key or "enc_output" in key: a_ : List[str] = state_dict.pop(__A ) a_ : List[Any] = val # finally, create HuggingFace model and load state dict a_ : Dict = DetaForObjectDetection(__A ) model.load_state_dict(__A ) model.eval() a_ : int = '''cuda''' if torch.cuda.is_available() else '''cpu''' model.to(__A ) # load image processor a_ : List[Any] = DetaImageProcessor(format='''coco_detection''' ) # verify our conversion on image a_ : Dict = prepare_img() a_ : Optional[int] = processor(images=__A , return_tensors='''pt''' ) a_ : Any = encoding['''pixel_values'''] a_ : int = model(pixel_values.to(__A ) ) # verify logits print('''Logits:''' , outputs.logits[0, :3, :3] ) print('''Boxes:''' , outputs.pred_boxes[0, :3, :3] ) if model_name == "deta-swin-large": a_ : Optional[int] = torch.tensor( [[-7.6308, -2.8485, -5.3737], [-7.2037, -4.5505, -4.8027], [-7.2943, -4.2611, -4.6617]] ) a_ : Tuple = torch.tensor([[0.4987, 0.4969, 0.9999], [0.2549, 0.5498, 0.4805], [0.5498, 0.2757, 0.0569]] ) elif model_name == "deta-swin-large-o365": a_ : Union[str, Any] = torch.tensor( [[-8.0122, -3.5720, -4.9717], [-8.1547, -3.6886, -4.6389], [-7.6610, -3.6194, -5.0134]] ) a_ : Any = torch.tensor([[0.2523, 0.5549, 0.4881], [0.7715, 0.4149, 0.4601], [0.5503, 0.2753, 0.0575]] ) assert torch.allclose(outputs.logits[0, :3, :3] , expected_logits.to(__A ) , atol=1E-4 ) assert torch.allclose(outputs.pred_boxes[0, :3, :3] , expected_boxes.to(__A ) , atol=1E-4 ) print('''Everything ok!''' ) if pytorch_dump_folder_path: # Save model and processor logger.info(f'Saving PyTorch model and processor to {pytorch_dump_folder_path}...' ) Path(__A ).mkdir(exist_ok=__A ) model.save_pretrained(__A ) processor.save_pretrained(__A ) # Push to hub if push_to_hub: print('''Pushing model and processor to hub...''' ) model.push_to_hub(f'jozhang97/{model_name}' ) processor.push_to_hub(f'jozhang97/{model_name}' ) if __name__ == "__main__": __lowerCAmelCase = argparse.ArgumentParser() parser.add_argument( '--model_name', type=str, default='deta-swin-large', choices=['deta-swin-large', 'deta-swin-large-o365'], help='Name of the model you\'d like to convert.', ) parser.add_argument( '--pytorch_dump_folder_path', default=None, type=str, help='Path to the folder to output PyTorch model.', ) parser.add_argument( '--push_to_hub', action='store_true', help='Whether or not to push the converted model to the ๐Ÿค— hub.' ) __lowerCAmelCase = parser.parse_args() convert_deta_checkpoint(args.model_name, args.pytorch_dump_folder_path, args.push_to_hub)
666
0
'''simple docstring''' import numpy as np from matplotlib import pyplot as plt from sklearn.datasets import load_iris from sklearn.metrics import ConfusionMatrixDisplay from sklearn.model_selection import train_test_split from xgboost import XGBClassifier def _UpperCAmelCase ( __A : dict ): return (data["data"], data["target"]) def _UpperCAmelCase ( __A : np.ndarray , __A : np.ndarray ): a_ : Optional[int] = XGBClassifier() classifier.fit(__A , __A ) return classifier def _UpperCAmelCase ( ): a_ : Any = load_iris() a_ : List[str] = data_handling(__A ) a_ : Tuple = train_test_split( __A , __A , test_size=0.25 ) a_ : List[str] = iris['''target_names'''] # Create an XGBoost Classifier from the training data a_ : Optional[Any] = xgboost(__A , __A ) # Display the confusion matrix of the classifier with both training and test sets ConfusionMatrixDisplay.from_estimator( __A , __A , __A , display_labels=__A , cmap='''Blues''' , normalize='''true''' , ) plt.title('''Normalized Confusion Matrix - IRIS Dataset''' ) plt.show() if __name__ == "__main__": import doctest doctest.testmod(verbose=True) main()
718
'''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 SCREAMING_SNAKE_CASE ( SCREAMING_SNAKE_CASE_ , unittest.TestCase ): snake_case__ = DDIMPipeline snake_case__ = UNCONDITIONAL_IMAGE_GENERATION_PARAMS snake_case__ = PipelineTesterMixin.required_optional_params - { "num_images_per_prompt", "latents", "callback", "callback_steps", } snake_case__ = UNCONDITIONAL_IMAGE_GENERATION_BATCH_PARAMS snake_case__ = False def SCREAMING_SNAKE_CASE ( self : Any ) -> Optional[Any]: torch.manual_seed(0 ) a_ : int = 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''') , ) a_ : str = DDIMScheduler() a_ : Union[str, Any] = {'''unet''': unet, '''scheduler''': scheduler} return components def SCREAMING_SNAKE_CASE ( self : Dict , __SCREAMING_SNAKE_CASE : str , __SCREAMING_SNAKE_CASE : Tuple=0 ) -> str: if str(__SCREAMING_SNAKE_CASE ).startswith('''mps''' ): a_ : Dict = torch.manual_seed(__SCREAMING_SNAKE_CASE ) else: a_ : Union[str, Any] = torch.Generator(device=__SCREAMING_SNAKE_CASE ).manual_seed(__SCREAMING_SNAKE_CASE ) a_ : Union[str, Any] = { '''batch_size''': 1, '''generator''': generator, '''num_inference_steps''': 2, '''output_type''': '''numpy''', } return inputs def SCREAMING_SNAKE_CASE ( self : Union[str, Any] ) -> Union[str, Any]: a_ : Dict = '''cpu''' a_ : List[Any] = self.get_dummy_components() a_ : List[str] = self.pipeline_class(**__SCREAMING_SNAKE_CASE ) pipe.to(__SCREAMING_SNAKE_CASE ) pipe.set_progress_bar_config(disable=__SCREAMING_SNAKE_CASE ) a_ : Tuple = self.get_dummy_inputs(__SCREAMING_SNAKE_CASE ) a_ : Optional[Any] = pipe(**__SCREAMING_SNAKE_CASE ).images a_ : List[str] = image[0, -3:, -3:, -1] self.assertEqual(image.shape , (1, 32, 32, 3) ) a_ : int = np.array( [1.000e00, 5.717e-01, 4.717e-01, 1.000e00, 0.000e00, 1.000e00, 3.000e-04, 0.000e00, 9.000e-04] ) a_ : Union[str, Any] = np.abs(image_slice.flatten() - expected_slice ).max() self.assertLessEqual(__SCREAMING_SNAKE_CASE , 1e-3 ) def SCREAMING_SNAKE_CASE ( self : List[str] ) -> str: super().test_dict_tuple_outputs_equivalent(expected_max_difference=3e-3 ) def SCREAMING_SNAKE_CASE ( self : str ) -> Optional[int]: super().test_save_load_local(expected_max_difference=3e-3 ) def SCREAMING_SNAKE_CASE ( self : Any ) -> List[str]: super().test_save_load_optional_components(expected_max_difference=3e-3 ) def SCREAMING_SNAKE_CASE ( self : str ) -> Optional[int]: super().test_inference_batch_single_identical(expected_max_diff=3e-3 ) @slow @require_torch_gpu class SCREAMING_SNAKE_CASE ( unittest.TestCase ): def SCREAMING_SNAKE_CASE ( self : str ) -> Any: a_ : Optional[Any] = '''google/ddpm-cifar10-32''' a_ : Optional[Any] = UNetaDModel.from_pretrained(__SCREAMING_SNAKE_CASE ) a_ : Dict = DDIMScheduler() a_ : List[str] = DDIMPipeline(unet=__SCREAMING_SNAKE_CASE , scheduler=__SCREAMING_SNAKE_CASE ) ddim.to(__SCREAMING_SNAKE_CASE ) ddim.set_progress_bar_config(disable=__SCREAMING_SNAKE_CASE ) a_ : Tuple = torch.manual_seed(0 ) a_ : Tuple = ddim(generator=__SCREAMING_SNAKE_CASE , eta=0.0 , output_type='''numpy''' ).images a_ : List[Any] = image[0, -3:, -3:, -1] assert image.shape == (1, 32, 32, 3) a_ : List[str] = np.array([0.1723, 0.1617, 0.1600, 0.1626, 0.1497, 0.1513, 0.1505, 0.1442, 0.1453] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-2 def SCREAMING_SNAKE_CASE ( self : Union[str, Any] ) -> str: a_ : int = '''google/ddpm-ema-bedroom-256''' a_ : str = UNetaDModel.from_pretrained(__SCREAMING_SNAKE_CASE ) a_ : Tuple = DDIMScheduler.from_pretrained(__SCREAMING_SNAKE_CASE ) a_ : Any = DDIMPipeline(unet=__SCREAMING_SNAKE_CASE , scheduler=__SCREAMING_SNAKE_CASE ) ddpm.to(__SCREAMING_SNAKE_CASE ) ddpm.set_progress_bar_config(disable=__SCREAMING_SNAKE_CASE ) a_ : Tuple = torch.manual_seed(0 ) a_ : List[Any] = ddpm(generator=__SCREAMING_SNAKE_CASE , output_type='''numpy''' ).images a_ : Optional[int] = image[0, -3:, -3:, -1] assert image.shape == (1, 256, 256, 3) a_ : Optional[Any] = np.array([0.0060, 0.0201, 0.0344, 0.0024, 0.0018, 0.0002, 0.0022, 0.0000, 0.0069] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-2
666
0
'''simple docstring''' from ..models.whisper import WhisperForConditionalGeneration, WhisperProcessor from .base import PipelineTool class SCREAMING_SNAKE_CASE ( SCREAMING_SNAKE_CASE_ ): snake_case__ = "openai/whisper-base" snake_case__ = ( "This is a tool that transcribes an audio into text. It takes an input named `audio` and returns the " "transcribed text." ) snake_case__ = "transcriber" snake_case__ = WhisperProcessor snake_case__ = WhisperForConditionalGeneration snake_case__ = ["audio"] snake_case__ = ["text"] def SCREAMING_SNAKE_CASE ( self : Any , __SCREAMING_SNAKE_CASE : Any ) -> int: return self.pre_processor(__SCREAMING_SNAKE_CASE , return_tensors='''pt''' ).input_features def SCREAMING_SNAKE_CASE ( self : Tuple , __SCREAMING_SNAKE_CASE : Optional[Any] ) -> int: return self.model.generate(inputs=__SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE ( self : Union[str, Any] , __SCREAMING_SNAKE_CASE : Union[str, Any] ) -> str: return self.pre_processor.batch_decode(__SCREAMING_SNAKE_CASE , skip_special_tokens=__SCREAMING_SNAKE_CASE )[0]
719
'''simple docstring''' from dataclasses import dataclass, field from typing import TYPE_CHECKING, Any, ClassVar, Dict, List, Optional, Union import pyarrow as pa if TYPE_CHECKING: from .features import FeatureType @dataclass class SCREAMING_SNAKE_CASE : snake_case__ = 42 snake_case__ = None # Automatically constructed snake_case__ = "dict" snake_case__ = None snake_case__ = field(default="Translation" , init=SCREAMING_SNAKE_CASE_ , repr=SCREAMING_SNAKE_CASE_ ) def __call__( self : Dict ) -> Tuple: return pa.struct({lang: pa.string() for lang in sorted(self.languages )} ) def SCREAMING_SNAKE_CASE ( self : Union[str, Any] ) -> Union["FeatureType", Dict[str, "FeatureType"]]: from .features import Value return {k: Value('''string''' ) for k in sorted(self.languages )} @dataclass class SCREAMING_SNAKE_CASE : snake_case__ = None snake_case__ = None snake_case__ = None # Automatically constructed snake_case__ = "dict" snake_case__ = None snake_case__ = field(default="TranslationVariableLanguages" , init=SCREAMING_SNAKE_CASE_ , repr=SCREAMING_SNAKE_CASE_ ) def SCREAMING_SNAKE_CASE ( self : Tuple ) -> Dict: a_ : List[str] = sorted(set(self.languages ) ) if self.languages else None a_ : Optional[Any] = len(self.languages ) if self.languages else None def __call__( self : Any ) -> Optional[Any]: return pa.struct({'''language''': pa.list_(pa.string() ), '''translation''': pa.list_(pa.string() )} ) def SCREAMING_SNAKE_CASE ( self : Optional[int] , __SCREAMING_SNAKE_CASE : Optional[Any] ) -> Optional[Any]: a_ : str = set(self.languages ) if self.languages and set(__SCREAMING_SNAKE_CASE ) - lang_set: raise ValueError( f'Some languages in example ({", ".join(sorted(set(__SCREAMING_SNAKE_CASE ) - lang_set ) )}) are not in valid set ({", ".join(__SCREAMING_SNAKE_CASE )}).' ) # Convert dictionary into tuples, splitting out cases where there are # multiple translations for a single language. a_ : int = [] for lang, text in translation_dict.items(): if isinstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): translation_tuples.append((lang, text) ) else: translation_tuples.extend([(lang, el) for el in text] ) # Ensure translations are in ascending order by language code. a_ , a_ : List[Any] = zip(*sorted(__SCREAMING_SNAKE_CASE ) ) return {"language": languages, "translation": translations} def SCREAMING_SNAKE_CASE ( self : Any ) -> Union["FeatureType", Dict[str, "FeatureType"]]: from .features import Sequence, Value return { "language": Sequence(Value('''string''' ) ), "translation": Sequence(Value('''string''' ) ), }
666
0
'''simple docstring''' 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
720
'''simple docstring''' import json import os import pickle import shutil import tempfile from unittest import TestCase from unittest.mock import patch import numpy as np from datasets import Dataset from transformers import is_faiss_available from transformers.models.bart.configuration_bart import BartConfig from transformers.models.bart.tokenization_bart import BartTokenizer from transformers.models.bert.tokenization_bert import VOCAB_FILES_NAMES as DPR_VOCAB_FILES_NAMES from transformers.models.dpr.configuration_dpr import DPRConfig from transformers.models.dpr.tokenization_dpr import DPRContextEncoderTokenizer, DPRQuestionEncoderTokenizer from transformers.models.rag.configuration_rag import RagConfig from transformers.models.rag.retrieval_rag import CustomHFIndex, RagRetriever from transformers.models.roberta.tokenization_roberta import VOCAB_FILES_NAMES as BART_VOCAB_FILES_NAMES from transformers.testing_utils import require_faiss, require_sentencepiece, require_tokenizers, require_torch if is_faiss_available(): import faiss @require_faiss class SCREAMING_SNAKE_CASE ( SCREAMING_SNAKE_CASE_ ): def SCREAMING_SNAKE_CASE ( self : Dict ) -> Dict: a_ : Union[str, Any] = tempfile.mkdtemp() a_ : Union[str, Any] = 8 # DPR tok a_ : Dict = [ '''[UNK]''', '''[CLS]''', '''[SEP]''', '''[PAD]''', '''[MASK]''', '''want''', '''##want''', '''##ed''', '''wa''', '''un''', '''runn''', '''##ing''', ''',''', '''low''', '''lowest''', ] a_ : str = os.path.join(self.tmpdirname , '''dpr_tokenizer''' ) os.makedirs(__SCREAMING_SNAKE_CASE , exist_ok=__SCREAMING_SNAKE_CASE ) a_ : Optional[int] = os.path.join(__SCREAMING_SNAKE_CASE , DPR_VOCAB_FILES_NAMES['''vocab_file'''] ) with open(self.vocab_file , '''w''' , encoding='''utf-8''' ) as vocab_writer: vocab_writer.write(''''''.join([x + '''\n''' for x in vocab_tokens] ) ) # BART tok a_ : Union[str, Any] = [ '''l''', '''o''', '''w''', '''e''', '''r''', '''s''', '''t''', '''i''', '''d''', '''n''', '''\u0120''', '''\u0120l''', '''\u0120n''', '''\u0120lo''', '''\u0120low''', '''er''', '''\u0120lowest''', '''\u0120newer''', '''\u0120wider''', '''<unk>''', ] a_ : int = dict(zip(__SCREAMING_SNAKE_CASE , range(len(__SCREAMING_SNAKE_CASE ) ) ) ) a_ : int = ['''#version: 0.2''', '''\u0120 l''', '''\u0120l o''', '''\u0120lo w''', '''e r''', ''''''] a_ : Optional[int] = {'''unk_token''': '''<unk>'''} a_ : List[str] = os.path.join(self.tmpdirname , '''bart_tokenizer''' ) os.makedirs(__SCREAMING_SNAKE_CASE , exist_ok=__SCREAMING_SNAKE_CASE ) a_ : Tuple = os.path.join(__SCREAMING_SNAKE_CASE , BART_VOCAB_FILES_NAMES['''vocab_file'''] ) a_ : int = os.path.join(__SCREAMING_SNAKE_CASE , BART_VOCAB_FILES_NAMES['''merges_file'''] ) with open(self.vocab_file , '''w''' , encoding='''utf-8''' ) as fp: fp.write(json.dumps(__SCREAMING_SNAKE_CASE ) + '''\n''' ) with open(self.merges_file , '''w''' , encoding='''utf-8''' ) as fp: fp.write('''\n'''.join(__SCREAMING_SNAKE_CASE ) ) def SCREAMING_SNAKE_CASE ( self : Tuple ) -> DPRQuestionEncoderTokenizer: return DPRQuestionEncoderTokenizer.from_pretrained(os.path.join(self.tmpdirname , '''dpr_tokenizer''' ) ) def SCREAMING_SNAKE_CASE ( self : str ) -> DPRContextEncoderTokenizer: return DPRContextEncoderTokenizer.from_pretrained(os.path.join(self.tmpdirname , '''dpr_tokenizer''' ) ) def SCREAMING_SNAKE_CASE ( self : Optional[int] ) -> BartTokenizer: return BartTokenizer.from_pretrained(os.path.join(self.tmpdirname , '''bart_tokenizer''' ) ) def SCREAMING_SNAKE_CASE ( self : List[Any] ) -> Union[str, Any]: shutil.rmtree(self.tmpdirname ) def SCREAMING_SNAKE_CASE ( self : Dict ) -> Union[str, Any]: a_ : str = Dataset.from_dict( { '''id''': ['''0''', '''1'''], '''text''': ['''foo''', '''bar'''], '''title''': ['''Foo''', '''Bar'''], '''embeddings''': [np.ones(self.retrieval_vector_size ), 2 * np.ones(self.retrieval_vector_size )], } ) dataset.add_faiss_index('''embeddings''' , string_factory='''Flat''' , metric_type=faiss.METRIC_INNER_PRODUCT ) return dataset def SCREAMING_SNAKE_CASE ( self : List[str] ) -> Tuple: a_ : List[str] = self.get_dummy_dataset() a_ : Tuple = RagConfig( retrieval_vector_size=self.retrieval_vector_size , question_encoder=DPRConfig().to_dict() , generator=BartConfig().to_dict() , ) with patch('''transformers.models.rag.retrieval_rag.load_dataset''' ) as mock_load_dataset: a_ : Tuple = dataset a_ : Any = RagRetriever( __SCREAMING_SNAKE_CASE , question_encoder_tokenizer=self.get_dpr_tokenizer() , generator_tokenizer=self.get_bart_tokenizer() , ) return retriever def SCREAMING_SNAKE_CASE ( self : Dict , __SCREAMING_SNAKE_CASE : bool ) -> Dict: a_ : Dict = self.get_dummy_dataset() a_ : Dict = RagConfig( retrieval_vector_size=self.retrieval_vector_size , question_encoder=DPRConfig().to_dict() , generator=BartConfig().to_dict() , index_name='''custom''' , ) if from_disk: a_ : Optional[int] = os.path.join(self.tmpdirname , '''dataset''' ) a_ : str = os.path.join(self.tmpdirname , '''index.faiss''' ) dataset.get_index('''embeddings''' ).save(os.path.join(self.tmpdirname , '''index.faiss''' ) ) dataset.drop_index('''embeddings''' ) dataset.save_to_disk(os.path.join(self.tmpdirname , '''dataset''' ) ) del dataset a_ : int = RagRetriever( __SCREAMING_SNAKE_CASE , question_encoder_tokenizer=self.get_dpr_tokenizer() , generator_tokenizer=self.get_bart_tokenizer() , ) else: a_ : Optional[Any] = RagRetriever( __SCREAMING_SNAKE_CASE , question_encoder_tokenizer=self.get_dpr_tokenizer() , generator_tokenizer=self.get_bart_tokenizer() , index=CustomHFIndex(config.retrieval_vector_size , __SCREAMING_SNAKE_CASE ) , ) return retriever def SCREAMING_SNAKE_CASE ( self : Optional[Any] ) -> Optional[int]: a_ : str = Dataset.from_dict( { '''id''': ['''0''', '''1'''], '''text''': ['''foo''', '''bar'''], '''title''': ['''Foo''', '''Bar'''], '''embeddings''': [np.ones(self.retrieval_vector_size + 1 ), 2 * np.ones(self.retrieval_vector_size + 1 )], } ) dataset.add_faiss_index('''embeddings''' , string_factory='''Flat''' , metric_type=faiss.METRIC_INNER_PRODUCT ) a_ : Optional[int] = os.path.join(self.tmpdirname , '''hf_bert_base.hnswSQ8_correct_phi_128.c_index''' ) dataset.save_faiss_index('''embeddings''' , index_file_name + '''.index.dpr''' ) pickle.dump(dataset['''id'''] , open(index_file_name + '''.index_meta.dpr''' , '''wb''' ) ) a_ : Union[str, Any] = os.path.join(self.tmpdirname , '''psgs_w100.tsv.pkl''' ) a_ : Dict = {sample['''id''']: [sample['''text'''], sample['''title''']] for sample in dataset} pickle.dump(__SCREAMING_SNAKE_CASE , open(__SCREAMING_SNAKE_CASE , '''wb''' ) ) a_ : Optional[Any] = RagConfig( retrieval_vector_size=self.retrieval_vector_size , question_encoder=DPRConfig().to_dict() , generator=BartConfig().to_dict() , index_name='''legacy''' , index_path=self.tmpdirname , ) a_ : int = RagRetriever( __SCREAMING_SNAKE_CASE , question_encoder_tokenizer=self.get_dpr_tokenizer() , generator_tokenizer=self.get_bart_tokenizer() ) return retriever def SCREAMING_SNAKE_CASE ( self : Tuple ) -> Dict: a_ : Optional[Any] = 1 a_ : Dict = self.get_dummy_canonical_hf_index_retriever() a_ : Tuple = np.array( [np.ones(self.retrieval_vector_size ), -np.ones(self.retrieval_vector_size )] , dtype=np.floataa ) a_ , a_ , a_ : str = retriever.retrieve(__SCREAMING_SNAKE_CASE , n_docs=__SCREAMING_SNAKE_CASE ) self.assertEqual(retrieved_doc_embeds.shape , (2, n_docs, self.retrieval_vector_size) ) self.assertEqual(len(__SCREAMING_SNAKE_CASE ) , 2 ) self.assertEqual(sorted(doc_dicts[0] ) , ['''embeddings''', '''id''', '''text''', '''title'''] ) self.assertEqual(len(doc_dicts[0]['''id'''] ) , __SCREAMING_SNAKE_CASE ) self.assertEqual(doc_dicts[0]['''id'''][0] , '''1''' ) # max inner product is reached with second doc self.assertEqual(doc_dicts[1]['''id'''][0] , '''0''' ) # max inner product is reached with first doc self.assertListEqual(doc_ids.tolist() , [[1], [0]] ) def SCREAMING_SNAKE_CASE ( self : Tuple ) -> Dict: a_ : str = self.get_dummy_canonical_hf_index_retriever() with tempfile.TemporaryDirectory() as tmp_dirname: with patch('''transformers.models.rag.retrieval_rag.load_dataset''' ) as mock_load_dataset: a_ : List[str] = self.get_dummy_dataset() retriever.save_pretrained(__SCREAMING_SNAKE_CASE ) a_ : Optional[Any] = RagRetriever.from_pretrained(__SCREAMING_SNAKE_CASE ) self.assertIsInstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) a_ : Union[str, Any] = np.array( [np.ones(self.retrieval_vector_size ), -np.ones(self.retrieval_vector_size )] , dtype=np.floataa ) a_ : List[str] = retriever.retrieve(__SCREAMING_SNAKE_CASE , n_docs=1 ) self.assertTrue(out is not None ) def SCREAMING_SNAKE_CASE ( self : Dict ) -> int: a_ : Union[str, Any] = 1 a_ : Optional[Any] = self.get_dummy_custom_hf_index_retriever(from_disk=__SCREAMING_SNAKE_CASE ) a_ : List[Any] = np.array( [np.ones(self.retrieval_vector_size ), -np.ones(self.retrieval_vector_size )] , dtype=np.floataa ) a_ , a_ , a_ : Any = retriever.retrieve(__SCREAMING_SNAKE_CASE , n_docs=__SCREAMING_SNAKE_CASE ) self.assertEqual(retrieved_doc_embeds.shape , (2, n_docs, self.retrieval_vector_size) ) self.assertEqual(len(__SCREAMING_SNAKE_CASE ) , 2 ) self.assertEqual(sorted(doc_dicts[0] ) , ['''embeddings''', '''id''', '''text''', '''title'''] ) self.assertEqual(len(doc_dicts[0]['''id'''] ) , __SCREAMING_SNAKE_CASE ) self.assertEqual(doc_dicts[0]['''id'''][0] , '''1''' ) # max inner product is reached with second doc self.assertEqual(doc_dicts[1]['''id'''][0] , '''0''' ) # max inner product is reached with first doc self.assertListEqual(doc_ids.tolist() , [[1], [0]] ) def SCREAMING_SNAKE_CASE ( self : str ) -> List[str]: a_ : Dict = self.get_dummy_custom_hf_index_retriever(from_disk=__SCREAMING_SNAKE_CASE ) with tempfile.TemporaryDirectory() as tmp_dirname: retriever.save_pretrained(__SCREAMING_SNAKE_CASE ) a_ : List[str] = RagRetriever.from_pretrained(__SCREAMING_SNAKE_CASE ) self.assertIsInstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) a_ : Tuple = np.array( [np.ones(self.retrieval_vector_size ), -np.ones(self.retrieval_vector_size )] , dtype=np.floataa ) a_ : Dict = retriever.retrieve(__SCREAMING_SNAKE_CASE , n_docs=1 ) self.assertTrue(out is not None ) def SCREAMING_SNAKE_CASE ( self : Tuple ) -> Optional[int]: a_ : Union[str, Any] = 1 a_ : str = self.get_dummy_custom_hf_index_retriever(from_disk=__SCREAMING_SNAKE_CASE ) a_ : Optional[int] = np.array( [np.ones(self.retrieval_vector_size ), -np.ones(self.retrieval_vector_size )] , dtype=np.floataa ) a_ , a_ , a_ : Tuple = retriever.retrieve(__SCREAMING_SNAKE_CASE , n_docs=__SCREAMING_SNAKE_CASE ) self.assertEqual(retrieved_doc_embeds.shape , (2, n_docs, self.retrieval_vector_size) ) self.assertEqual(len(__SCREAMING_SNAKE_CASE ) , 2 ) self.assertEqual(sorted(doc_dicts[0] ) , ['''embeddings''', '''id''', '''text''', '''title'''] ) self.assertEqual(len(doc_dicts[0]['''id'''] ) , __SCREAMING_SNAKE_CASE ) self.assertEqual(doc_dicts[0]['''id'''][0] , '''1''' ) # max inner product is reached with second doc self.assertEqual(doc_dicts[1]['''id'''][0] , '''0''' ) # max inner product is reached with first doc self.assertListEqual(doc_ids.tolist() , [[1], [0]] ) def SCREAMING_SNAKE_CASE ( self : int ) -> Dict: a_ : List[str] = self.get_dummy_custom_hf_index_retriever(from_disk=__SCREAMING_SNAKE_CASE ) with tempfile.TemporaryDirectory() as tmp_dirname: retriever.save_pretrained(__SCREAMING_SNAKE_CASE ) a_ : Any = RagRetriever.from_pretrained(__SCREAMING_SNAKE_CASE ) self.assertIsInstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) a_ : Tuple = np.array( [np.ones(self.retrieval_vector_size ), -np.ones(self.retrieval_vector_size )] , dtype=np.floataa ) a_ : Dict = retriever.retrieve(__SCREAMING_SNAKE_CASE , n_docs=1 ) self.assertTrue(out is not None ) def SCREAMING_SNAKE_CASE ( self : Optional[int] ) -> Any: a_ : str = 1 a_ : Tuple = self.get_dummy_legacy_index_retriever() a_ : Union[str, Any] = np.array( [np.ones(self.retrieval_vector_size ), -np.ones(self.retrieval_vector_size )] , dtype=np.floataa ) a_ , a_ , a_ : Any = retriever.retrieve(__SCREAMING_SNAKE_CASE , n_docs=__SCREAMING_SNAKE_CASE ) self.assertEqual(retrieved_doc_embeds.shape , (2, n_docs, self.retrieval_vector_size) ) self.assertEqual(len(__SCREAMING_SNAKE_CASE ) , 2 ) self.assertEqual(sorted(doc_dicts[0] ) , ['''text''', '''title'''] ) self.assertEqual(len(doc_dicts[0]['''text'''] ) , __SCREAMING_SNAKE_CASE ) self.assertEqual(doc_dicts[0]['''text'''][0] , '''bar''' ) # max inner product is reached with second doc self.assertEqual(doc_dicts[1]['''text'''][0] , '''foo''' ) # max inner product is reached with first doc self.assertListEqual(doc_ids.tolist() , [[1], [0]] ) def SCREAMING_SNAKE_CASE ( self : int ) -> Optional[int]: a_ : List[str] = self.get_dummy_legacy_index_retriever() with tempfile.TemporaryDirectory() as tmp_dirname: retriever.save_pretrained(__SCREAMING_SNAKE_CASE ) a_ : Any = RagRetriever.from_pretrained(__SCREAMING_SNAKE_CASE ) self.assertIsInstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) a_ : List[str] = np.array( [np.ones(self.retrieval_vector_size ), -np.ones(self.retrieval_vector_size )] , dtype=np.floataa ) a_ : Optional[Any] = retriever.retrieve(__SCREAMING_SNAKE_CASE , n_docs=1 ) self.assertTrue(out is not None ) @require_torch @require_tokenizers @require_sentencepiece def SCREAMING_SNAKE_CASE ( self : int ) -> List[str]: import torch a_ : Any = 1 a_ : List[Any] = self.get_dummy_canonical_hf_index_retriever() a_ : Union[str, Any] = [[5, 7], [10, 11]] a_ : Optional[int] = np.array( [np.ones(self.retrieval_vector_size ), -np.ones(self.retrieval_vector_size )] , dtype=np.floataa ) a_ : str = retriever(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , prefix=retriever.config.generator.prefix , n_docs=__SCREAMING_SNAKE_CASE ) a_ , a_ , a_ : List[str] = ( out['''context_input_ids'''], out['''context_attention_mask'''], out['''retrieved_doc_embeds'''], ) self.assertEqual(retrieved_doc_embeds.shape , (2, n_docs, self.retrieval_vector_size) ) self.assertIsInstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) self.assertIsInstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) self.assertIsInstance(__SCREAMING_SNAKE_CASE , np.ndarray ) a_ : Any = retriever( __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , prefix=retriever.config.generator.prefix , n_docs=__SCREAMING_SNAKE_CASE , return_tensors='''pt''' , ) a_ , a_ , a_ , a_ : str = ( # noqa: F841 out['''context_input_ids'''], out['''context_attention_mask'''], out['''retrieved_doc_embeds'''], out['''doc_ids'''], ) self.assertEqual(retrieved_doc_embeds.shape , (2, n_docs, self.retrieval_vector_size) ) self.assertIsInstance(__SCREAMING_SNAKE_CASE , torch.Tensor ) self.assertIsInstance(__SCREAMING_SNAKE_CASE , torch.Tensor ) self.assertIsInstance(__SCREAMING_SNAKE_CASE , torch.Tensor ) @require_torch @require_tokenizers @require_sentencepiece def SCREAMING_SNAKE_CASE ( self : Union[str, Any] ) -> Any: a_ : str = self.get_dpr_ctx_encoder_tokenizer() a_ : Tuple = 1 a_ : Any = self.get_dummy_custom_hf_index_retriever(from_disk=__SCREAMING_SNAKE_CASE ) retriever.set_ctx_encoder_tokenizer(__SCREAMING_SNAKE_CASE ) a_ : Dict = [[5, 7], [10, 11]] a_ : List[str] = np.array( [np.ones(self.retrieval_vector_size ), -np.ones(self.retrieval_vector_size )] , dtype=np.floataa ) a_ : List[Any] = retriever(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , prefix=retriever.config.generator.prefix , n_docs=__SCREAMING_SNAKE_CASE ) self.assertEqual( len(__SCREAMING_SNAKE_CASE ) , 6 ) # check whether the retriever output consist of 6 attributes including tokenized docs self.assertEqual( all(k in out for k in ('''tokenized_doc_ids''', '''tokenized_doc_attention_mask''') ) , __SCREAMING_SNAKE_CASE ) # check for doc token related keys in dictionary.
666
0
'''simple docstring''' from maths.is_square_free import is_square_free from maths.prime_factors import prime_factors def _UpperCAmelCase ( __A : int ): a_ : Dict = prime_factors(__A ) if is_square_free(__A ): return -1 if len(__A ) % 2 else 1 return 0 if __name__ == "__main__": import doctest doctest.testmod()
721
'''simple docstring''' from math import pi, sqrt, tan def _UpperCAmelCase ( __A : float ): if side_length < 0: raise ValueError('''surface_area_cube() only accepts non-negative values''' ) return 6 * side_length**2 def _UpperCAmelCase ( __A : float , __A : float , __A : float ): if length < 0 or breadth < 0 or height < 0: raise ValueError('''surface_area_cuboid() only accepts non-negative values''' ) return 2 * ((length * breadth) + (breadth * height) + (length * height)) def _UpperCAmelCase ( __A : float ): if radius < 0: raise ValueError('''surface_area_sphere() only accepts non-negative values''' ) return 4 * pi * radius**2 def _UpperCAmelCase ( __A : float ): if radius < 0: raise ValueError('''surface_area_hemisphere() only accepts non-negative values''' ) return 3 * pi * radius**2 def _UpperCAmelCase ( __A : float , __A : float ): if radius < 0 or height < 0: raise ValueError('''surface_area_cone() only accepts non-negative values''' ) return pi * radius * (radius + (height**2 + radius**2) ** 0.5) def _UpperCAmelCase ( __A : float , __A : float , __A : float ): if radius_a < 0 or radius_a < 0 or height < 0: raise ValueError( '''surface_area_conical_frustum() only accepts non-negative values''' ) a_ : Any = (height**2 + (radius_a - radius_a) ** 2) ** 0.5 return pi * ((slant_height * (radius_a + radius_a)) + radius_a**2 + radius_a**2) def _UpperCAmelCase ( __A : float , __A : float ): if radius < 0 or height < 0: raise ValueError('''surface_area_cylinder() only accepts non-negative values''' ) return 2 * pi * radius * (height + radius) def _UpperCAmelCase ( __A : float , __A : float ): if torus_radius < 0 or tube_radius < 0: raise ValueError('''surface_area_torus() only accepts non-negative values''' ) if torus_radius < tube_radius: raise ValueError( '''surface_area_torus() does not support spindle or self intersecting tori''' ) return 4 * pow(__A , 2 ) * torus_radius * tube_radius def _UpperCAmelCase ( __A : float , __A : float ): if length < 0 or width < 0: raise ValueError('''area_rectangle() only accepts non-negative values''' ) return length * width def _UpperCAmelCase ( __A : float ): if side_length < 0: raise ValueError('''area_square() only accepts non-negative values''' ) return side_length**2 def _UpperCAmelCase ( __A : float , __A : float ): if base < 0 or height < 0: raise ValueError('''area_triangle() only accepts non-negative values''' ) return (base * height) / 2 def _UpperCAmelCase ( __A : float , __A : float , __A : float ): if sidea < 0 or sidea < 0 or sidea < 0: raise ValueError('''area_triangle_three_sides() only accepts non-negative values''' ) elif sidea + sidea < sidea or sidea + sidea < sidea or sidea + sidea < sidea: raise ValueError('''Given three sides do not form a triangle''' ) a_ : int = (sidea + sidea + sidea) / 2 a_ : Optional[Any] = sqrt( semi_perimeter * (semi_perimeter - sidea) * (semi_perimeter - sidea) * (semi_perimeter - sidea) ) return area def _UpperCAmelCase ( __A : float , __A : float ): if base < 0 or height < 0: raise ValueError('''area_parallelogram() only accepts non-negative values''' ) return base * height def _UpperCAmelCase ( __A : float , __A : float , __A : float ): if basea < 0 or basea < 0 or height < 0: raise ValueError('''area_trapezium() only accepts non-negative values''' ) return 1 / 2 * (basea + basea) * height def _UpperCAmelCase ( __A : float ): if radius < 0: raise ValueError('''area_circle() only accepts non-negative values''' ) return pi * radius**2 def _UpperCAmelCase ( __A : float , __A : float ): if radius_x < 0 or radius_y < 0: raise ValueError('''area_ellipse() only accepts non-negative values''' ) return pi * radius_x * radius_y def _UpperCAmelCase ( __A : float , __A : float ): if diagonal_a < 0 or diagonal_a < 0: raise ValueError('''area_rhombus() only accepts non-negative values''' ) return 1 / 2 * diagonal_a * diagonal_a def _UpperCAmelCase ( __A : int , __A : float ): if not isinstance(__A , __A ) or sides < 3: raise ValueError( '''area_reg_polygon() only accepts integers greater than or \ equal to three as number of sides''' ) elif length < 0: raise ValueError( '''area_reg_polygon() only accepts non-negative values as \ length of a side''' ) return (sides * length**2) / (4 * tan(pi / sides )) return (sides * length**2) / (4 * tan(pi / sides )) if __name__ == "__main__": import doctest doctest.testmod(verbose=True) # verbose so we can see methods missing tests print('[DEMO] Areas of various geometric shapes: \n') print(F"""Rectangle: {area_rectangle(10, 20) = }""") print(F"""Square: {area_square(10) = }""") print(F"""Triangle: {area_triangle(10, 10) = }""") print(F"""Triangle: {area_triangle_three_sides(5, 12, 13) = }""") print(F"""Parallelogram: {area_parallelogram(10, 20) = }""") print(F"""Rhombus: {area_rhombus(10, 20) = }""") print(F"""Trapezium: {area_trapezium(10, 20, 30) = }""") print(F"""Circle: {area_circle(20) = }""") print(F"""Ellipse: {area_ellipse(10, 20) = }""") print('\nSurface Areas of various geometric shapes: \n') print(F"""Cube: {surface_area_cube(20) = }""") print(F"""Cuboid: {surface_area_cuboid(10, 20, 30) = }""") print(F"""Sphere: {surface_area_sphere(20) = }""") print(F"""Hemisphere: {surface_area_hemisphere(20) = }""") print(F"""Cone: {surface_area_cone(10, 20) = }""") print(F"""Conical Frustum: {surface_area_conical_frustum(10, 20, 30) = }""") print(F"""Cylinder: {surface_area_cylinder(10, 20) = }""") print(F"""Torus: {surface_area_torus(20, 10) = }""") print(F"""Equilateral Triangle: {area_reg_polygon(3, 10) = }""") print(F"""Square: {area_reg_polygon(4, 10) = }""") print(F"""Reqular Pentagon: {area_reg_polygon(5, 10) = }""")
666
0
'''simple docstring''' import importlib import os import fsspec import pytest from fsspec import register_implementation from fsspec.registry import _registry as _fsspec_registry from datasets.filesystems import COMPRESSION_FILESYSTEMS, HfFileSystem, extract_path_from_uri, is_remote_filesystem from .utils import require_lza, require_zstandard def _UpperCAmelCase ( __A : Optional[Any] ): '''simple docstring''' assert "mock" in _fsspec_registry assert "bz2" in _fsspec_registry def _UpperCAmelCase ( ): '''simple docstring''' assert "mock" not in _fsspec_registry assert "bz2" in _fsspec_registry def _UpperCAmelCase ( ): '''simple docstring''' a_ : Any = '''mock-s3-bucket''' a_ : List[Any] = f's3://{mock_bucket}' a_ : List[Any] = extract_path_from_uri(__A ) assert dataset_path.startswith('''s3://''' ) is False a_ : int = '''./local/path''' a_ : Dict = extract_path_from_uri(__A ) assert dataset_path == new_dataset_path def _UpperCAmelCase ( __A : Optional[int] ): '''simple docstring''' a_ : List[str] = is_remote_filesystem(__A ) assert is_remote is True a_ : str = fsspec.filesystem('''file''' ) a_ : Union[str, Any] = is_remote_filesystem(__A ) assert is_remote is False @pytest.mark.parametrize('''compression_fs_class''' , __A ) def _UpperCAmelCase ( __A : List[Any] , __A : Any , __A : List[Any] , __A : Dict , __A : Optional[Any] , __A : int , __A : Union[str, Any] ): '''simple docstring''' a_ : str = {'''gzip''': gz_file, '''xz''': xz_file, '''zstd''': zstd_file, '''bz2''': bza_file, '''lz4''': lza_file} a_ : str = input_paths[compression_fs_class.protocol] if input_path is None: a_ : List[str] = f'for \'{compression_fs_class.protocol}\' compression protocol, ' if compression_fs_class.protocol == "lz4": reason += require_lza.kwargs["reason"] elif compression_fs_class.protocol == "zstd": reason += require_zstandard.kwargs["reason"] pytest.skip(__A ) a_ : Tuple = fsspec.filesystem(compression_fs_class.protocol , fo=__A ) assert isinstance(__A , __A ) a_ : List[str] = os.path.basename(__A ) a_ : Optional[int] = expected_filename[: expected_filename.rindex('''.''' )] assert fs.glob('''*''' ) == [expected_filename] with fs.open(__A , '''r''' , encoding='''utf-8''' ) as f, open(__A , encoding='''utf-8''' ) as expected_file: assert f.read() == expected_file.read() @pytest.mark.parametrize('''protocol''' , ['''zip''', '''gzip'''] ) def _UpperCAmelCase ( __A : List[str] , __A : Optional[Any] , __A : List[str] ): '''simple docstring''' a_ : int = {'''zip''': zip_jsonl_path, '''gzip''': jsonl_gz_path} a_ : List[Any] = compressed_file_paths[protocol] a_ : Dict = '''dataset.jsonl''' a_ : List[str] = f'{protocol}://{member_file_path}::{compressed_file_path}' a_ : Optional[Any] = fsspec.get_fs_token_paths(__A ) assert fs.isfile(__A ) assert not fs.isfile('''non_existing_''' + member_file_path ) @pytest.mark.integration def _UpperCAmelCase ( __A : int , __A : Optional[Any] , __A : str , __A : Optional[int] ): '''simple docstring''' a_ : Dict = hf_api.dataset_info(__A , token=__A ) a_ : int = HfFileSystem(repo_info=__A , token=__A ) assert sorted(hffs.glob('''*''' ) ) == [".gitattributes", "data"] assert hffs.isdir('''data''' ) assert hffs.isfile('''.gitattributes''' ) and hffs.isfile('''data/text_data.txt''' ) with open(__A ) as f: assert hffs.open('''data/text_data.txt''' , '''r''' ).read() == f.read() def _UpperCAmelCase ( ): '''simple docstring''' a_ : str = '''bz2''' # Import module import datasets.filesystems # Overwrite protocol and reload register_implementation(__A , __A , clobber=__A ) with pytest.warns(__A ) as warning_info: importlib.reload(datasets.filesystems ) assert len(__A ) == 1 assert ( str(warning_info[0].message ) == f'A filesystem protocol was already set for {protocol} and will be overwritten.' )
700
'''simple docstring''' import random import unittest import torch from diffusers import IFInpaintingSuperResolutionPipeline from diffusers.utils import floats_tensor from diffusers.utils.import_utils import is_xformers_available from diffusers.utils.testing_utils import skip_mps, torch_device from ..pipeline_params import ( TEXT_GUIDED_IMAGE_INPAINTING_BATCH_PARAMS, TEXT_GUIDED_IMAGE_INPAINTING_PARAMS, ) from ..test_pipelines_common import PipelineTesterMixin from . import IFPipelineTesterMixin @skip_mps class SCREAMING_SNAKE_CASE ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , unittest.TestCase ): snake_case__ = IFInpaintingSuperResolutionPipeline snake_case__ = TEXT_GUIDED_IMAGE_INPAINTING_PARAMS - {"width", "height"} snake_case__ = TEXT_GUIDED_IMAGE_INPAINTING_BATCH_PARAMS.union({"original_image"} ) snake_case__ = PipelineTesterMixin.required_optional_params - {"latents"} def SCREAMING_SNAKE_CASE ( self : str ) -> List[str]: return self._get_superresolution_dummy_components() def SCREAMING_SNAKE_CASE ( self : Union[str, Any] , __SCREAMING_SNAKE_CASE : Optional[Any] , __SCREAMING_SNAKE_CASE : Dict=0 ) -> List[Any]: if str(__SCREAMING_SNAKE_CASE ).startswith('''mps''' ): a_ : Optional[int] = torch.manual_seed(__SCREAMING_SNAKE_CASE ) else: a_ : str = torch.Generator(device=__SCREAMING_SNAKE_CASE ).manual_seed(__SCREAMING_SNAKE_CASE ) a_ : Dict = floats_tensor((1, 3, 16, 16) , rng=random.Random(__SCREAMING_SNAKE_CASE ) ).to(__SCREAMING_SNAKE_CASE ) a_ : Union[str, Any] = floats_tensor((1, 3, 32, 32) , rng=random.Random(__SCREAMING_SNAKE_CASE ) ).to(__SCREAMING_SNAKE_CASE ) a_ : Tuple = floats_tensor((1, 3, 32, 32) , rng=random.Random(__SCREAMING_SNAKE_CASE ) ).to(__SCREAMING_SNAKE_CASE ) a_ : Union[str, Any] = { '''prompt''': '''A painting of a squirrel eating a burger''', '''image''': image, '''original_image''': original_image, '''mask_image''': mask_image, '''generator''': generator, '''num_inference_steps''': 2, '''output_type''': '''numpy''', } return inputs @unittest.skipIf( torch_device != '''cuda''' or not is_xformers_available() , reason='''XFormers attention is only available with CUDA and `xformers` installed''' , ) def SCREAMING_SNAKE_CASE ( self : Optional[Any] ) -> List[str]: self._test_xformers_attention_forwardGenerator_pass(expected_max_diff=1e-3 ) def SCREAMING_SNAKE_CASE ( self : int ) -> int: self._test_save_load_optional_components() @unittest.skipIf(torch_device != '''cuda''' , reason='''float16 requires CUDA''' ) def SCREAMING_SNAKE_CASE ( self : str ) -> Tuple: # Due to non-determinism in save load of the hf-internal-testing/tiny-random-t5 text encoder super().test_save_load_floataa(expected_max_diff=1e-1 ) def SCREAMING_SNAKE_CASE ( self : List[str] ) -> Optional[int]: self._test_attention_slicing_forward_pass(expected_max_diff=1e-2 ) def SCREAMING_SNAKE_CASE ( self : Union[str, Any] ) -> Optional[Any]: self._test_save_load_local() def SCREAMING_SNAKE_CASE ( self : Dict ) -> Optional[Any]: self._test_inference_batch_single_identical( expected_max_diff=1e-2 , )
666
0
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 = logging.get_logger(__name__) __lowerCAmelCase = list(MODEL_FOR_QUESTION_ANSWERING_MAPPING.keys()) __lowerCAmelCase = tuple(conf.model_type for conf in MODEL_CONFIG_CLASSES) @dataclass class SCREAMING_SNAKE_CASE : snake_case__ = field( default=SCREAMING_SNAKE_CASE_ , metadata={"help": "Model type selected in the list: " + ", ".join(SCREAMING_SNAKE_CASE_ )} ) snake_case__ = field( default=SCREAMING_SNAKE_CASE_ , metadata={"help": "The input data dir. Should contain the .json files for the SQuAD task."} ) snake_case__ = field( default=128 , metadata={ "help": ( "The maximum total input sequence length after tokenization. Sequences longer " "than this will be truncated, sequences shorter will be padded." ) } , ) snake_case__ = field( default=128 , metadata={"help": "When splitting up a long document into chunks, how much stride to take between chunks."} , ) snake_case__ = field( default=64 , metadata={ "help": ( "The maximum number of tokens for the question. Questions longer than this will " "be truncated to this length." ) } , ) snake_case__ = field( default=30 , 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." ) } , ) snake_case__ = field( default=SCREAMING_SNAKE_CASE_ , metadata={"help": "Overwrite the cached training and evaluation sets"} ) snake_case__ = field( default=SCREAMING_SNAKE_CASE_ , metadata={"help": "If true, the SQuAD examples contain some that do not have an answer."} ) snake_case__ = field( default=0.0 , metadata={"help": "If null_score - best_non_null is greater than the threshold predict null."} ) snake_case__ = field( default=20 , metadata={"help": "If null_score - best_non_null is greater than the threshold predict null."} ) snake_case__ = field( default=0 , metadata={ "help": ( "language id of input for language-specific xlm models (see" " tokenization_xlm.PRETRAINED_INIT_CONFIGURATION)" ) } , ) snake_case__ = field(default=1 , metadata={"help": "multiple threads for converting example to features"} ) class SCREAMING_SNAKE_CASE ( SCREAMING_SNAKE_CASE_ ): snake_case__ = "train" snake_case__ = "dev" class SCREAMING_SNAKE_CASE ( SCREAMING_SNAKE_CASE_ ): snake_case__ = 42 snake_case__ = 42 snake_case__ = 42 snake_case__ = 42 def __init__( self : Tuple , __SCREAMING_SNAKE_CASE : SquadDataTrainingArguments , __SCREAMING_SNAKE_CASE : PreTrainedTokenizer , __SCREAMING_SNAKE_CASE : Optional[int] = None , __SCREAMING_SNAKE_CASE : Union[str, Split] = Split.train , __SCREAMING_SNAKE_CASE : Optional[bool] = False , __SCREAMING_SNAKE_CASE : Optional[str] = None , __SCREAMING_SNAKE_CASE : Optional[str] = "pt" , ) -> int: a_ : List[str] = args a_ : Optional[int] = is_language_sensitive a_ : Union[str, Any] = SquadVaProcessor() if args.version_2_with_negative else SquadVaProcessor() if isinstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): try: a_ : List[Any] = Split[mode] except KeyError: raise KeyError('''mode is not a valid split name''' ) a_ : Tuple = mode # Load data features from cache or dataset file a_ : List[str] = '''v2''' if args.version_2_with_negative else '''v1''' a_ : Dict = 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. a_ : Tuple = cached_features_file + '''.lock''' with FileLock(__SCREAMING_SNAKE_CASE ): if os.path.exists(__SCREAMING_SNAKE_CASE ) and not args.overwrite_cache: a_ : Tuple = time.time() a_ : str = torch.load(__SCREAMING_SNAKE_CASE ) # Legacy cache files have only features, while new cache files # will have dataset and examples also. a_ : Optional[int] = self.old_features['''features'''] a_ : Tuple = self.old_features.get('''dataset''' , __SCREAMING_SNAKE_CASE ) a_ : Tuple = self.old_features.get('''examples''' , __SCREAMING_SNAKE_CASE ) 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: a_ : Dict = self.processor.get_dev_examples(args.data_dir ) else: a_ : Tuple = self.processor.get_train_examples(args.data_dir ) a_ : Tuple = squad_convert_examples_to_features( examples=self.examples , tokenizer=__SCREAMING_SNAKE_CASE , 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=__SCREAMING_SNAKE_CASE , ) a_ : str = time.time() torch.save( {'''features''': self.features, '''dataset''': self.dataset, '''examples''': self.examples} , __SCREAMING_SNAKE_CASE , ) # ^ 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 : str ) -> str: return len(self.features ) def __getitem__( self : Union[str, Any] , __SCREAMING_SNAKE_CASE : int ) -> Dict[str, torch.Tensor]: # Convert to Tensors and build dataset a_ : str = self.features[i] a_ : List[str] = torch.tensor(feature.input_ids , dtype=torch.long ) a_ : Optional[Any] = torch.tensor(feature.attention_mask , dtype=torch.long ) a_ : Optional[Any] = torch.tensor(feature.token_type_ids , dtype=torch.long ) a_ : Tuple = torch.tensor(feature.cls_index , dtype=torch.long ) a_ : str = torch.tensor(feature.p_mask , dtype=torch.float ) a_ : List[str] = torch.tensor(feature.is_impossible , dtype=torch.float ) a_ : List[Any] = { '''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: a_ : Optional[int] = torch.tensor(feature.start_position , dtype=torch.long ) a_ : List[str] = torch.tensor(feature.end_position , dtype=torch.long ) inputs.update({'''start_positions''': start_positions, '''end_positions''': end_positions} ) return inputs
701
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available __lowerCAmelCase = { 'configuration_git': ['GIT_PRETRAINED_CONFIG_ARCHIVE_MAP', 'GitConfig', 'GitVisionConfig'], 'processing_git': ['GitProcessor'], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __lowerCAmelCase = [ 'GIT_PRETRAINED_MODEL_ARCHIVE_LIST', 'GitForCausalLM', 'GitModel', 'GitPreTrainedModel', 'GitVisionModel', ] if TYPE_CHECKING: from .configuration_git import GIT_PRETRAINED_CONFIG_ARCHIVE_MAP, GitConfig, GitVisionConfig from .processing_git import GitProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_git import ( GIT_PRETRAINED_MODEL_ARCHIVE_LIST, GitForCausalLM, GitModel, GitPreTrainedModel, GitVisionModel, ) else: import sys __lowerCAmelCase = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
666
0
'''simple docstring''' def _UpperCAmelCase ( __A : int ): if not isinstance(__A , __A ): raise TypeError('''Input value must be an \'int\' type''' ) a_ : Tuple = 0 while number: position += 1 number >>= 1 return position if __name__ == "__main__": import doctest doctest.testmod()
702
'''simple docstring''' from unittest.mock import patch import pyspark from datasets.packaged_modules.spark.spark import ( Spark, SparkExamplesIterable, _generate_iterable_examples, ) from ..utils import ( require_dill_gt_0_3_2, require_not_windows, ) def _UpperCAmelCase ( __A : List[str] , __A : List[Any] ): a_ : Any = [] for part_id in partition_order: a_ : str = df.where(f'SPARK_PARTITION_ID() = {part_id}' ).collect() for row_idx, row in enumerate(__A ): expected_row_ids_and_row_dicts.append((f'{part_id}_{row_idx}', row.asDict()) ) return expected_row_ids_and_row_dicts @require_not_windows @require_dill_gt_0_3_2 def _UpperCAmelCase ( ): a_ : List[str] = pyspark.sql.SparkSession.builder.master('''local[*]''' ).appName('''pyspark''' ).getOrCreate() a_ : Union[str, Any] = spark.range(1_00 ).repartition(1 ) a_ : Any = Spark(__A ) # The id ints will be converted to Pyarrow int64s, so each row will be 8 bytes. Setting a max_shard_size of 16 means # that each partition can hold 2 rows. spark_builder._repartition_df_if_needed(max_shard_size=16 ) # Given that the dataframe has 100 rows and each partition has 2 rows, we expect 50 partitions. assert spark_builder.df.rdd.getNumPartitions() == 50 @require_not_windows @require_dill_gt_0_3_2 def _UpperCAmelCase ( ): a_ : List[Any] = pyspark.sql.SparkSession.builder.master('''local[*]''' ).appName('''pyspark''' ).getOrCreate() a_ : int = spark.range(10 ).repartition(2 ) a_ : Tuple = [1, 0] a_ : List[str] = _generate_iterable_examples(__A , __A ) # Reverse the partitions. a_ : int = _get_expected_row_ids_and_row_dicts_for_partition_order(__A , __A ) for i, (row_id, row_dict) in enumerate(generate_fn() ): a_ , a_ : List[Any] = expected_row_ids_and_row_dicts[i] assert row_id == expected_row_id assert row_dict == expected_row_dict @require_not_windows @require_dill_gt_0_3_2 def _UpperCAmelCase ( ): a_ : int = pyspark.sql.SparkSession.builder.master('''local[*]''' ).appName('''pyspark''' ).getOrCreate() a_ : str = spark.range(10 ).repartition(1 ) a_ : Tuple = SparkExamplesIterable(__A ) assert it.n_shards == 1 for i, (row_id, row_dict) in enumerate(__A ): assert row_id == f'0_{i}' assert row_dict == {"id": i} @require_not_windows @require_dill_gt_0_3_2 def _UpperCAmelCase ( ): a_ : Tuple = pyspark.sql.SparkSession.builder.master('''local[*]''' ).appName('''pyspark''' ).getOrCreate() a_ : str = spark.range(30 ).repartition(3 ) # Mock the generator so that shuffle reverses the partition indices. with patch('''numpy.random.Generator''' ) as generator_mock: a_ : Union[str, Any] = lambda __A : x.reverse() a_ : Any = _get_expected_row_ids_and_row_dicts_for_partition_order(__A , [2, 1, 0] ) a_ : str = SparkExamplesIterable(__A ).shuffle_data_sources(__A ) assert shuffled_it.n_shards == 3 for i, (row_id, row_dict) in enumerate(__A ): a_ , a_ : Optional[Any] = expected_row_ids_and_row_dicts[i] assert row_id == expected_row_id assert row_dict == expected_row_dict @require_not_windows @require_dill_gt_0_3_2 def _UpperCAmelCase ( ): a_ : int = pyspark.sql.SparkSession.builder.master('''local[*]''' ).appName('''pyspark''' ).getOrCreate() a_ : List[str] = spark.range(20 ).repartition(4 ) # Partitions 0 and 2 a_ : Dict = SparkExamplesIterable(__A ).shard_data_sources(worker_id=0 , num_workers=2 ) assert shard_it_a.n_shards == 2 a_ : Optional[Any] = _get_expected_row_ids_and_row_dicts_for_partition_order(__A , [0, 2] ) for i, (row_id, row_dict) in enumerate(__A ): a_ , a_ : Tuple = expected_row_ids_and_row_dicts_a[i] assert row_id == expected_row_id assert row_dict == expected_row_dict # Partitions 1 and 3 a_ : List[Any] = SparkExamplesIterable(__A ).shard_data_sources(worker_id=1 , num_workers=2 ) assert shard_it_a.n_shards == 2 a_ : Optional[int] = _get_expected_row_ids_and_row_dicts_for_partition_order(__A , [1, 3] ) for i, (row_id, row_dict) in enumerate(__A ): a_ , a_ : Any = expected_row_ids_and_row_dicts_a[i] assert row_id == expected_row_id assert row_dict == expected_row_dict @require_not_windows @require_dill_gt_0_3_2 def _UpperCAmelCase ( ): a_ : Any = pyspark.sql.SparkSession.builder.master('''local[*]''' ).appName('''pyspark''' ).getOrCreate() a_ : List[Any] = spark.range(1_00 ).repartition(1 ) a_ : Optional[Any] = Spark(__A ) # Choose a small max_shard_size for maximum partitioning. spark_builder._repartition_df_if_needed(max_shard_size=1 ) # The new number of partitions should not be greater than the number of rows. assert spark_builder.df.rdd.getNumPartitions() == 1_00
666
0
'''simple docstring''' from typing import TYPE_CHECKING from ...file_utils import _LazyModule, is_torch_available from ...utils import OptionalDependencyNotAvailable __lowerCAmelCase = { 'configuration_gpt_neox_japanese': ['GPT_NEOX_JAPANESE_PRETRAINED_CONFIG_ARCHIVE_MAP', 'GPTNeoXJapaneseConfig'], 'tokenization_gpt_neox_japanese': ['GPTNeoXJapaneseTokenizer'], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __lowerCAmelCase = [ 'GPT_NEOX_JAPANESE_PRETRAINED_MODEL_ARCHIVE_LIST', 'GPTNeoXJapaneseForCausalLM', 'GPTNeoXJapaneseLayer', 'GPTNeoXJapaneseModel', 'GPTNeoXJapanesePreTrainedModel', ] if TYPE_CHECKING: from .configuration_gpt_neox_japanese import GPT_NEOX_JAPANESE_PRETRAINED_CONFIG_ARCHIVE_MAP, GPTNeoXJapaneseConfig from .tokenization_gpt_neox_japanese import GPTNeoXJapaneseTokenizer try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_gpt_neox_japanese import ( GPT_NEOX_JAPANESE_PRETRAINED_MODEL_ARCHIVE_LIST, GPTNeoXJapaneseForCausalLM, GPTNeoXJapaneseLayer, GPTNeoXJapaneseModel, GPTNeoXJapanesePreTrainedModel, ) else: import sys __lowerCAmelCase = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
703
'''simple docstring''' from collections import OrderedDict from typing import TYPE_CHECKING, Any, List, Mapping, Optional from packaging import version if TYPE_CHECKING: from ... import PreTrainedTokenizer, TensorType from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfigWithPast, PatchingSpec from ...utils import is_torch_available, logging __lowerCAmelCase = logging.get_logger(__name__) __lowerCAmelCase = { 'bigscience/bloom': 'https://huggingface.co/bigscience/bloom/resolve/main/config.json', 'bigscience/bloom-560m': 'https://huggingface.co/bigscience/bloom-560m/blob/main/config.json', 'bigscience/bloom-1b1': 'https://huggingface.co/bigscience/bloom-1b1/blob/main/config.json', 'bigscience/bloom-1b7': 'https://huggingface.co/bigscience/bloom-1b7/blob/main/config.json', 'bigscience/bloom-3b': 'https://huggingface.co/bigscience/bloom-3b/blob/main/config.json', 'bigscience/bloom-7b1': 'https://huggingface.co/bigscience/bloom-7b1/blob/main/config.json', } class SCREAMING_SNAKE_CASE ( SCREAMING_SNAKE_CASE_ ): snake_case__ = "bloom" snake_case__ = ["past_key_values"] snake_case__ = { "num_hidden_layers": "n_layer", "num_attention_heads": "n_head", } def __init__( self : Union[str, Any] , __SCREAMING_SNAKE_CASE : int=25_0880 , __SCREAMING_SNAKE_CASE : Dict=64 , __SCREAMING_SNAKE_CASE : Tuple=2 , __SCREAMING_SNAKE_CASE : int=8 , __SCREAMING_SNAKE_CASE : Any=1e-5 , __SCREAMING_SNAKE_CASE : Optional[Any]=0.02 , __SCREAMING_SNAKE_CASE : Union[str, Any]=True , __SCREAMING_SNAKE_CASE : int=1 , __SCREAMING_SNAKE_CASE : Any=2 , __SCREAMING_SNAKE_CASE : Optional[Any]=False , __SCREAMING_SNAKE_CASE : Optional[Any]=0.0 , __SCREAMING_SNAKE_CASE : str=0.0 , __SCREAMING_SNAKE_CASE : List[Any]=1 , __SCREAMING_SNAKE_CASE : List[str]=False , **__SCREAMING_SNAKE_CASE : str , ) -> Any: a_ : Optional[int] = vocab_size # Backward compatibility with n_embed kwarg a_ : Any = kwargs.pop('''n_embed''' , __SCREAMING_SNAKE_CASE ) a_ : Optional[int] = hidden_size if n_embed is None else n_embed a_ : int = n_layer a_ : str = n_head a_ : Optional[int] = layer_norm_epsilon a_ : Dict = initializer_range a_ : List[str] = use_cache a_ : Dict = pretraining_tp a_ : Optional[Any] = apply_residual_connection_post_layernorm a_ : Optional[Any] = hidden_dropout a_ : List[str] = attention_dropout a_ : Dict = bos_token_id a_ : Optional[int] = eos_token_id a_ : Any = slow_but_exact super().__init__(bos_token_id=__SCREAMING_SNAKE_CASE , eos_token_id=__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) class SCREAMING_SNAKE_CASE ( SCREAMING_SNAKE_CASE_ ): snake_case__ = version.parse("1.12" ) def __init__( self : Tuple , __SCREAMING_SNAKE_CASE : PretrainedConfig , __SCREAMING_SNAKE_CASE : str = "default" , __SCREAMING_SNAKE_CASE : List[PatchingSpec] = None , __SCREAMING_SNAKE_CASE : bool = False , ) -> Optional[Any]: super().__init__(__SCREAMING_SNAKE_CASE , task=__SCREAMING_SNAKE_CASE , patching_specs=__SCREAMING_SNAKE_CASE , use_past=__SCREAMING_SNAKE_CASE ) if not getattr(self._config , '''pad_token_id''' , __SCREAMING_SNAKE_CASE ): # TODO: how to do that better? a_ : Tuple = 0 @property def SCREAMING_SNAKE_CASE ( self : Tuple ) -> Mapping[str, Mapping[int, str]]: a_ : Optional[Any] = OrderedDict({'''input_ids''': {0: '''batch''', 1: '''sequence'''}} ) if self.use_past: # BLOOM stores values on dynamic axis 2. For more details see: https://github.com/huggingface/transformers/pull/18344 self.fill_with_past_key_values_(__SCREAMING_SNAKE_CASE , direction='''inputs''' , inverted_values_shape=__SCREAMING_SNAKE_CASE ) a_ : Union[str, Any] = {0: '''batch''', 1: '''past_sequence + sequence'''} else: a_ : Union[str, Any] = {0: '''batch''', 1: '''sequence'''} return common_inputs @property def SCREAMING_SNAKE_CASE ( self : Any ) -> int: return self._config.n_layer @property def SCREAMING_SNAKE_CASE ( self : int ) -> int: return self._config.n_head @property def SCREAMING_SNAKE_CASE ( self : int ) -> float: return 1e-3 def SCREAMING_SNAKE_CASE ( self : Dict , __SCREAMING_SNAKE_CASE : "PreTrainedTokenizer" , __SCREAMING_SNAKE_CASE : int = -1 , __SCREAMING_SNAKE_CASE : int = -1 , __SCREAMING_SNAKE_CASE : bool = False , __SCREAMING_SNAKE_CASE : Optional["TensorType"] = None , ) -> Mapping[str, Any]: a_ : Dict = super(__SCREAMING_SNAKE_CASE , self ).generate_dummy_inputs( __SCREAMING_SNAKE_CASE , batch_size=__SCREAMING_SNAKE_CASE , seq_length=__SCREAMING_SNAKE_CASE , is_pair=__SCREAMING_SNAKE_CASE , framework=__SCREAMING_SNAKE_CASE ) # We need to order the input in the way they appears in the forward() a_ : Union[str, Any] = 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 a_ , a_ : Any = common_inputs['''input_ids'''].shape # Not using the same length for past_key_values a_ : str = seqlen + 2 a_ : Any = self._config.hidden_size // self.num_attention_heads a_ : Optional[int] = ( batch * self.num_attention_heads, head_dim, past_key_values_length, ) a_ : Any = ( batch * self.num_attention_heads, past_key_values_length, head_dim, ) a_ : List[str] = [ (torch.zeros(__SCREAMING_SNAKE_CASE ), torch.zeros(__SCREAMING_SNAKE_CASE )) for _ in range(self.num_layers ) ] a_ : Union[str, Any] = common_inputs['''attention_mask'''] if self.use_past: a_ : Optional[int] = ordered_inputs['''attention_mask'''].dtype a_ : List[Any] = torch.cat( [ordered_inputs['''attention_mask'''], torch.ones(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , dtype=__SCREAMING_SNAKE_CASE )] , dim=1 ) return ordered_inputs @property def SCREAMING_SNAKE_CASE ( self : Optional[int] ) -> int: return 13
666
0
'''simple docstring''' def _UpperCAmelCase ( __A : int , __A : int ): a_ : List[Any] = 1 # To kept the Calculated Value # Since C(n, k) = C(n, n-k) if k > (n - k): a_ : List[Any] = n - k # Calculate C(n,k) for i in range(__A ): result *= n - i result //= i + 1 return result def _UpperCAmelCase ( __A : int ): return binomial_coefficient(2 * node_count , __A ) // (node_count + 1) def _UpperCAmelCase ( __A : int ): if n < 0: raise ValueError('''factorial() not defined for negative values''' ) a_ : Dict = 1 for i in range(1 , n + 1 ): result *= i return result def _UpperCAmelCase ( __A : int ): return catalan_number(__A ) * factorial(__A ) if __name__ == "__main__": __lowerCAmelCase = int(input('Enter the number of nodes: ').strip() or 0) if node_count <= 0: raise ValueError('We need some nodes to work with.') print( F"""Given {node_count} nodes, there are {binary_tree_count(node_count)} """ F"""binary trees and {catalan_number(node_count)} binary search trees.""" )
704
'''simple docstring''' import sys __lowerCAmelCase = ( '73167176531330624919225119674426574742355349194934' '96983520312774506326239578318016984801869478851843' '85861560789112949495459501737958331952853208805511' '12540698747158523863050715693290963295227443043557' '66896648950445244523161731856403098711121722383113' '62229893423380308135336276614282806444486645238749' '30358907296290491560440772390713810515859307960866' '70172427121883998797908792274921901699720888093776' '65727333001053367881220235421809751254540594752243' '52584907711670556013604839586446706324415722155397' '53697817977846174064955149290862569321978468622482' '83972241375657056057490261407972968652414535100474' '82166370484403199890008895243450658541227588666881' '16427171479924442928230863465674813919123162824586' '17866458359124566529476545682848912883142607690042' '24219022671055626321111109370544217506941658960408' '07198403850962455444362981230987879927244284909188' '84580156166097919133875499200524063689912560717606' '05886116467109405077541002256983155200055935729725' '71636269561882670428252483600823257530420752963450' ) def _UpperCAmelCase ( __A : str ): a_ : Tuple = 1 for digit in s: product *= int(__A ) return product def _UpperCAmelCase ( __A : str = N ): a_ : Dict = -sys.maxsize - 1 a_ : Optional[int] = n[:13] a_ : str = 13 while cur_index < len(__A ) - 13: if int(n[cur_index] ) >= int(substr[0] ): a_ : Tuple = substr[1:] + n[cur_index] cur_index += 1 else: a_ : Dict = max(__A , str_eval(__A ) ) a_ : List[str] = n[cur_index : cur_index + 13] cur_index += 13 return largest_product if __name__ == "__main__": print(F"""{solution() = }""")
666
0
'''simple docstring''' __lowerCAmelCase = '\n# Installazione di Transformers\n! pip install transformers datasets\n# Per installare dalla fonte invece dell\'ultima versione rilasciata, commenta il comando sopra e\n# rimuovi la modalitร  commento al comando seguente.\n# ! pip install git+https://github.com/huggingface/transformers.git\n' __lowerCAmelCase = [{'type': 'code', 'content': INSTALL_CONTENT}] __lowerCAmelCase = { '{processor_class}': 'FakeProcessorClass', '{model_class}': 'FakeModelClass', '{object_class}': 'FakeObjectClass', }
705
'''simple docstring''' from __future__ import annotations def _UpperCAmelCase ( __A : list[int] ): a_ : int = len(__A ) // 2 # choose the middle 3 elements a_ : Dict = lst[m - 1 : m + 2] # if middle element is peak if three[1] > three[0] and three[1] > three[2]: return three[1] # if increasing, recurse on right elif three[0] < three[2]: if len(lst[:m] ) == 2: m -= 1 return peak(lst[m:] ) # decreasing else: if len(lst[:m] ) == 2: m += 1 return peak(lst[:m] ) if __name__ == "__main__": import doctest doctest.testmod()
666
0
'''simple docstring''' import glob import os import random from string import ascii_lowercase, digits import cva __lowerCAmelCase = '''''' __lowerCAmelCase = '''''' __lowerCAmelCase = '''''' __lowerCAmelCase = 1 # (0 is vertical, 1 is horizontal) def _UpperCAmelCase ( ): a_ : Tuple = get_dataset(__A , __A ) print('''Processing...''' ) a_ : Optional[int] = update_image_and_anno(__A , __A , __A ) for index, image in enumerate(__A ): # Get random string code: '7b7ad245cdff75241935e4dd860f3bad' a_ : List[Any] = random_chars(32 ) a_ : Dict = paths[index].split(os.sep )[-1].rsplit('''.''' , 1 )[0] a_ : Dict = f'{OUTPUT_DIR}/{file_name}_FLIP_{letter_code}' cva.imwrite(f'/{file_root}.jpg' , __A , [cva.IMWRITE_JPEG_QUALITY, 85] ) print(f'Success {index+1}/{len(__A )} with {file_name}' ) a_ : List[str] = [] for anno in new_annos[index]: a_ : int = f'{anno[0]} {anno[1]} {anno[2]} {anno[3]} {anno[4]}' annos_list.append(__A ) with open(f'/{file_root}.txt' , '''w''' ) as outfile: outfile.write('''\n'''.join(line for line in annos_list ) ) def _UpperCAmelCase ( __A : str , __A : str ): a_ : List[str] = [] a_ : str = [] for label_file in glob.glob(os.path.join(__A , '''*.txt''' ) ): a_ : int = label_file.split(os.sep )[-1].rsplit('''.''' , 1 )[0] with open(__A ) as in_file: a_ : List[Any] = in_file.readlines() a_ : int = os.path.join(__A , f'{label_name}.jpg' ) a_ : Tuple = [] for obj_list in obj_lists: a_ : List[str] = obj_list.rstrip('''\n''' ).split(''' ''' ) boxes.append( [ int(obj[0] ), float(obj[1] ), float(obj[2] ), float(obj[3] ), float(obj[4] ), ] ) if not boxes: continue img_paths.append(__A ) labels.append(__A ) return img_paths, labels def _UpperCAmelCase ( __A : list , __A : list , __A : int = 1 ): a_ : Any = [] a_ : List[Any] = [] a_ : Optional[int] = [] for idx in range(len(__A ) ): a_ : Any = [] a_ : Optional[int] = img_list[idx] path_list.append(__A ) a_ : str = anno_list[idx] a_ : List[str] = cva.imread(__A ) if flip_type == 1: a_ : Optional[int] = cva.flip(__A , __A ) for bbox in img_annos: a_ : Any = 1 - bbox[1] new_annos.append([bbox[0], x_center_new, bbox[2], bbox[3], bbox[4]] ) elif flip_type == 0: a_ : Any = cva.flip(__A , __A ) for bbox in img_annos: a_ : str = 1 - bbox[2] new_annos.append([bbox[0], bbox[1], y_center_new, bbox[3], bbox[4]] ) new_annos_lists.append(__A ) new_imgs_list.append(__A ) return new_imgs_list, new_annos_lists, path_list def _UpperCAmelCase ( __A : int = 32 ): assert number_char > 1, "The number of character should greater than 1" a_ : List[str] = ascii_lowercase + digits return "".join(random.choice(__A ) for _ in range(__A ) ) if __name__ == "__main__": main() print('''DONE โœ…''')
706
'''simple docstring''' import itertools import json import os import unittest from transformers import AddedToken, LongformerTokenizer, LongformerTokenizerFast from transformers.models.longformer.tokenization_longformer import VOCAB_FILES_NAMES from transformers.testing_utils import require_tokenizers, slow from ...test_tokenization_common import TokenizerTesterMixin @require_tokenizers class SCREAMING_SNAKE_CASE ( SCREAMING_SNAKE_CASE_ , unittest.TestCase ): snake_case__ = LongformerTokenizer snake_case__ = True snake_case__ = LongformerTokenizerFast snake_case__ = True def SCREAMING_SNAKE_CASE ( self : Optional[Any] ) -> List[str]: super().setUp() # Adapted from Sennrich et al. 2015 and https://github.com/rsennrich/subword-nmt a_ : Tuple = [ '''l''', '''o''', '''w''', '''e''', '''r''', '''s''', '''t''', '''i''', '''d''', '''n''', '''\u0120''', '''\u0120l''', '''\u0120n''', '''\u0120lo''', '''\u0120low''', '''er''', '''\u0120lowest''', '''\u0120newer''', '''\u0120wider''', '''<unk>''', ] a_ : Optional[Any] = dict(zip(__SCREAMING_SNAKE_CASE , range(len(__SCREAMING_SNAKE_CASE ) ) ) ) a_ : Union[str, Any] = ['''#version: 0.2''', '''\u0120 l''', '''\u0120l o''', '''\u0120lo w''', '''e r''', ''''''] a_ : Any = {'''unk_token''': '''<unk>'''} a_ : Union[str, Any] = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES['''vocab_file'''] ) a_ : Optional[int] = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES['''merges_file'''] ) with open(self.vocab_file , '''w''' , encoding='''utf-8''' ) as fp: fp.write(json.dumps(__SCREAMING_SNAKE_CASE ) + '''\n''' ) with open(self.merges_file , '''w''' , encoding='''utf-8''' ) as fp: fp.write('''\n'''.join(__SCREAMING_SNAKE_CASE ) ) def SCREAMING_SNAKE_CASE ( self : Any , **__SCREAMING_SNAKE_CASE : Any ) -> int: kwargs.update(self.special_tokens_map ) return self.tokenizer_class.from_pretrained(self.tmpdirname , **__SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE ( self : Union[str, Any] , **__SCREAMING_SNAKE_CASE : List[Any] ) -> List[str]: kwargs.update(self.special_tokens_map ) return self.rust_tokenizer_class.from_pretrained(self.tmpdirname , **__SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE ( self : Dict , __SCREAMING_SNAKE_CASE : List[Any] ) -> Any: a_ : Union[str, Any] = '''lower newer''' a_ : List[Any] = '''lower newer''' return input_text, output_text def SCREAMING_SNAKE_CASE ( self : Optional[Any] ) -> List[str]: a_ : Optional[Any] = self.tokenizer_class(self.vocab_file , self.merges_file , **self.special_tokens_map ) a_ : List[str] = '''lower newer''' a_ : str = ['''l''', '''o''', '''w''', '''er''', '''\u0120''', '''n''', '''e''', '''w''', '''er'''] a_ : Optional[int] = tokenizer.tokenize(__SCREAMING_SNAKE_CASE ) # , add_prefix_space=True) self.assertListEqual(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) a_ : Dict = tokens + [tokenizer.unk_token] a_ : Any = [0, 1, 2, 15, 10, 9, 3, 2, 15, 19] self.assertListEqual(tokenizer.convert_tokens_to_ids(__SCREAMING_SNAKE_CASE ) , __SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE ( self : List[str] ) -> Optional[Any]: a_ : Union[str, Any] = self.get_tokenizer() self.assertListEqual(tokenizer.encode('''Hello world!''' , add_special_tokens=__SCREAMING_SNAKE_CASE ) , [0, 3_1414, 232, 328, 2] ) self.assertListEqual( tokenizer.encode('''Hello world! cรฉcรฉ herlolip 418''' , add_special_tokens=__SCREAMING_SNAKE_CASE ) , [0, 3_1414, 232, 328, 740, 1140, 1_2695, 69, 4_6078, 1588, 2] , ) @slow def SCREAMING_SNAKE_CASE ( self : int ) -> Optional[Any]: a_ : Dict = self.tokenizer_class.from_pretrained('''allenai/longformer-base-4096''' ) a_ : Tuple = tokenizer.encode('''sequence builders''' , add_special_tokens=__SCREAMING_SNAKE_CASE ) a_ : Union[str, Any] = tokenizer.encode('''multi-sequence build''' , add_special_tokens=__SCREAMING_SNAKE_CASE ) a_ : Any = tokenizer.encode( '''sequence builders''' , add_special_tokens=__SCREAMING_SNAKE_CASE , add_prefix_space=__SCREAMING_SNAKE_CASE ) a_ : Any = tokenizer.encode( '''sequence builders''' , '''multi-sequence build''' , add_special_tokens=__SCREAMING_SNAKE_CASE , add_prefix_space=__SCREAMING_SNAKE_CASE ) a_ : List[str] = tokenizer.build_inputs_with_special_tokens(__SCREAMING_SNAKE_CASE ) a_ : Union[str, Any] = tokenizer.build_inputs_with_special_tokens(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) assert encoded_sentence == encoded_text_from_decode assert encoded_pair == encoded_pair_from_decode def SCREAMING_SNAKE_CASE ( self : str ) -> int: a_ : str = self.get_tokenizer() a_ : int = '''Encode this sequence.''' a_ : List[str] = tokenizer.byte_encoder[''' '''.encode('''utf-8''' )[0]] # Testing encoder arguments a_ : Dict = tokenizer.encode(__SCREAMING_SNAKE_CASE , add_special_tokens=__SCREAMING_SNAKE_CASE , add_prefix_space=__SCREAMING_SNAKE_CASE ) a_ : Optional[Any] = tokenizer.convert_ids_to_tokens(encoded[0] )[0] self.assertNotEqual(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) a_ : Dict = tokenizer.encode(__SCREAMING_SNAKE_CASE , add_special_tokens=__SCREAMING_SNAKE_CASE , add_prefix_space=__SCREAMING_SNAKE_CASE ) a_ : Any = tokenizer.convert_ids_to_tokens(encoded[0] )[0] self.assertEqual(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) tokenizer.add_special_tokens({'''bos_token''': '''<s>'''} ) a_ : Dict = tokenizer.encode(__SCREAMING_SNAKE_CASE , add_special_tokens=__SCREAMING_SNAKE_CASE ) a_ : Dict = tokenizer.convert_ids_to_tokens(encoded[1] )[0] self.assertNotEqual(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) # Testing spaces after special tokens a_ : Optional[Any] = '''<mask>''' tokenizer.add_special_tokens( {'''mask_token''': AddedToken(__SCREAMING_SNAKE_CASE , lstrip=__SCREAMING_SNAKE_CASE , rstrip=__SCREAMING_SNAKE_CASE )} ) # mask token has a left space a_ : Optional[int] = tokenizer.convert_tokens_to_ids(__SCREAMING_SNAKE_CASE ) a_ : List[Any] = '''Encode <mask> sequence''' a_ : List[str] = '''Encode <mask>sequence''' a_ : int = tokenizer.encode(__SCREAMING_SNAKE_CASE ) a_ : Optional[int] = encoded.index(__SCREAMING_SNAKE_CASE ) a_ : Optional[int] = tokenizer.convert_ids_to_tokens(encoded[mask_loc + 1] )[0] self.assertEqual(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) a_ : Union[str, Any] = tokenizer.encode(__SCREAMING_SNAKE_CASE ) a_ : Optional[int] = encoded.index(__SCREAMING_SNAKE_CASE ) a_ : str = tokenizer.convert_ids_to_tokens(encoded[mask_loc + 1] )[0] self.assertNotEqual(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE ( self : Optional[int] ) -> Union[str, Any]: pass def SCREAMING_SNAKE_CASE ( self : Optional[Any] ) -> Any: for tokenizer, pretrained_name, kwargs in self.tokenizers_list: with self.subTest(f'{tokenizer.__class__.__name__} ({pretrained_name})' ): a_ : Any = self.rust_tokenizer_class.from_pretrained(__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) a_ : Any = self.tokenizer_class.from_pretrained(__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) a_ : str = '''A, <mask> AllenNLP sentence.''' a_ : List[Any] = tokenizer_r.encode_plus(__SCREAMING_SNAKE_CASE , add_special_tokens=__SCREAMING_SNAKE_CASE , return_token_type_ids=__SCREAMING_SNAKE_CASE ) a_ : Dict = tokenizer_p.encode_plus(__SCREAMING_SNAKE_CASE , add_special_tokens=__SCREAMING_SNAKE_CASE , return_token_type_ids=__SCREAMING_SNAKE_CASE ) # token_type_ids should put 0 everywhere self.assertEqual(sum(tokens_r['''token_type_ids'''] ) , sum(tokens_p['''token_type_ids'''] ) ) # attention_mask should put 1 everywhere, so sum over length should be 1 self.assertEqual( sum(tokens_r['''attention_mask'''] ) / len(tokens_r['''attention_mask'''] ) , sum(tokens_p['''attention_mask'''] ) / len(tokens_p['''attention_mask'''] ) , ) a_ : str = tokenizer_r.convert_ids_to_tokens(tokens_r['''input_ids'''] ) a_ : Tuple = tokenizer_p.convert_ids_to_tokens(tokens_p['''input_ids'''] ) # Rust correctly handles the space before the mask while python doesnt self.assertSequenceEqual(tokens_p['''input_ids'''] , [0, 250, 6, 5_0264, 3823, 487, 2_1992, 3645, 4, 2] ) self.assertSequenceEqual(tokens_r['''input_ids'''] , [0, 250, 6, 5_0264, 3823, 487, 2_1992, 3645, 4, 2] ) self.assertSequenceEqual( __SCREAMING_SNAKE_CASE , ['''<s>''', '''A''', ''',''', '''<mask>''', '''ฤ Allen''', '''N''', '''LP''', '''ฤ sentence''', '''.''', '''</s>'''] ) self.assertSequenceEqual( __SCREAMING_SNAKE_CASE , ['''<s>''', '''A''', ''',''', '''<mask>''', '''ฤ Allen''', '''N''', '''LP''', '''ฤ sentence''', '''.''', '''</s>'''] ) def SCREAMING_SNAKE_CASE ( self : Optional[Any] ) -> List[Any]: for trim_offsets, add_prefix_space in itertools.product([True, False] , repeat=2 ): a_ : Any = self.rust_tokenizer_class.from_pretrained( self.tmpdirname , use_fast=__SCREAMING_SNAKE_CASE , add_prefix_space=__SCREAMING_SNAKE_CASE , trim_offsets=__SCREAMING_SNAKE_CASE ) a_ : Union[str, Any] = json.loads(tokenizer_r.backend_tokenizer.pre_tokenizer.__getstate__() ) a_ : str = json.loads(tokenizer_r.backend_tokenizer.post_processor.__getstate__() ) self.assertEqual(pre_tokenizer_state['''add_prefix_space'''] , __SCREAMING_SNAKE_CASE ) self.assertEqual(post_processor_state['''add_prefix_space'''] , __SCREAMING_SNAKE_CASE ) self.assertEqual(post_processor_state['''trim_offsets'''] , __SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE ( self : int ) -> Optional[int]: # Test which aims to verify that the offsets are well adapted to the argument `add_prefix_space` and # `trim_offsets` for tokenizer, pretrained_name, kwargs in self.tokenizers_list: with self.subTest(f'{tokenizer.__class__.__name__} ({pretrained_name})' ): a_ : Dict = '''hello''' # `hello` is a token in the vocabulary of `pretrained_name` a_ : Union[str, Any] = f'{text_of_1_token} {text_of_1_token}' a_ : Any = self.rust_tokenizer_class.from_pretrained( __SCREAMING_SNAKE_CASE , use_fast=__SCREAMING_SNAKE_CASE , add_prefix_space=__SCREAMING_SNAKE_CASE , trim_offsets=__SCREAMING_SNAKE_CASE ) a_ : Optional[int] = tokenizer_r(__SCREAMING_SNAKE_CASE , return_offsets_mapping=__SCREAMING_SNAKE_CASE , add_special_tokens=__SCREAMING_SNAKE_CASE ) self.assertEqual(encoding.offset_mapping[0] , (0, len(__SCREAMING_SNAKE_CASE )) ) self.assertEqual( encoding.offset_mapping[1] , (len(__SCREAMING_SNAKE_CASE ) + 1, len(__SCREAMING_SNAKE_CASE ) + 1 + len(__SCREAMING_SNAKE_CASE )) , ) a_ : Any = self.rust_tokenizer_class.from_pretrained( __SCREAMING_SNAKE_CASE , use_fast=__SCREAMING_SNAKE_CASE , add_prefix_space=__SCREAMING_SNAKE_CASE , trim_offsets=__SCREAMING_SNAKE_CASE ) a_ : str = tokenizer_r(__SCREAMING_SNAKE_CASE , return_offsets_mapping=__SCREAMING_SNAKE_CASE , add_special_tokens=__SCREAMING_SNAKE_CASE ) self.assertEqual(encoding.offset_mapping[0] , (0, len(__SCREAMING_SNAKE_CASE )) ) self.assertEqual( encoding.offset_mapping[1] , (len(__SCREAMING_SNAKE_CASE ) + 1, len(__SCREAMING_SNAKE_CASE ) + 1 + len(__SCREAMING_SNAKE_CASE )) , ) a_ : int = self.rust_tokenizer_class.from_pretrained( __SCREAMING_SNAKE_CASE , use_fast=__SCREAMING_SNAKE_CASE , add_prefix_space=__SCREAMING_SNAKE_CASE , trim_offsets=__SCREAMING_SNAKE_CASE ) a_ : Union[str, Any] = tokenizer_r(__SCREAMING_SNAKE_CASE , return_offsets_mapping=__SCREAMING_SNAKE_CASE , add_special_tokens=__SCREAMING_SNAKE_CASE ) self.assertEqual(encoding.offset_mapping[0] , (0, len(__SCREAMING_SNAKE_CASE )) ) self.assertEqual( encoding.offset_mapping[1] , (len(__SCREAMING_SNAKE_CASE ), len(__SCREAMING_SNAKE_CASE ) + 1 + len(__SCREAMING_SNAKE_CASE )) , ) a_ : Tuple = self.rust_tokenizer_class.from_pretrained( __SCREAMING_SNAKE_CASE , use_fast=__SCREAMING_SNAKE_CASE , add_prefix_space=__SCREAMING_SNAKE_CASE , trim_offsets=__SCREAMING_SNAKE_CASE ) a_ : Any = tokenizer_r(__SCREAMING_SNAKE_CASE , return_offsets_mapping=__SCREAMING_SNAKE_CASE , add_special_tokens=__SCREAMING_SNAKE_CASE ) self.assertEqual(encoding.offset_mapping[0] , (0, len(__SCREAMING_SNAKE_CASE )) ) self.assertEqual( encoding.offset_mapping[1] , (len(__SCREAMING_SNAKE_CASE ), len(__SCREAMING_SNAKE_CASE ) + 1 + len(__SCREAMING_SNAKE_CASE )) , ) a_ : Union[str, Any] = f' {text}' # tokenizer_r = self.rust_tokenizer_class.from_pretrained( # pretrained_name, use_fast=True, add_prefix_space=True, trim_offsets=True # ) # encoding = tokenizer_r(text, return_offsets_mapping=True, add_special_tokens=False) # self.assertEqual(encoding.offset_mapping[0], (1, 1 + len(text_of_1_token))) # self.assertEqual( # encoding.offset_mapping[1], # (1 + len(text_of_1_token) + 1, 1 + len(text_of_1_token) + 1 + len(text_of_1_token)), # ) a_ : str = self.rust_tokenizer_class.from_pretrained( __SCREAMING_SNAKE_CASE , use_fast=__SCREAMING_SNAKE_CASE , add_prefix_space=__SCREAMING_SNAKE_CASE , trim_offsets=__SCREAMING_SNAKE_CASE ) a_ : int = tokenizer_r(__SCREAMING_SNAKE_CASE , return_offsets_mapping=__SCREAMING_SNAKE_CASE , add_special_tokens=__SCREAMING_SNAKE_CASE ) self.assertEqual(encoding.offset_mapping[0] , (1, 1 + len(__SCREAMING_SNAKE_CASE )) ) self.assertEqual( encoding.offset_mapping[1] , (1 + len(__SCREAMING_SNAKE_CASE ) + 1, 1 + len(__SCREAMING_SNAKE_CASE ) + 1 + len(__SCREAMING_SNAKE_CASE )) , ) a_ : int = self.rust_tokenizer_class.from_pretrained( __SCREAMING_SNAKE_CASE , use_fast=__SCREAMING_SNAKE_CASE , add_prefix_space=__SCREAMING_SNAKE_CASE , trim_offsets=__SCREAMING_SNAKE_CASE ) a_ : str = tokenizer_r(__SCREAMING_SNAKE_CASE , return_offsets_mapping=__SCREAMING_SNAKE_CASE , add_special_tokens=__SCREAMING_SNAKE_CASE ) self.assertEqual(encoding.offset_mapping[0] , (0, 1 + len(__SCREAMING_SNAKE_CASE )) ) self.assertEqual( encoding.offset_mapping[1] , (1 + len(__SCREAMING_SNAKE_CASE ), 1 + len(__SCREAMING_SNAKE_CASE ) + 1 + len(__SCREAMING_SNAKE_CASE )) , ) a_ : Optional[int] = self.rust_tokenizer_class.from_pretrained( __SCREAMING_SNAKE_CASE , use_fast=__SCREAMING_SNAKE_CASE , add_prefix_space=__SCREAMING_SNAKE_CASE , trim_offsets=__SCREAMING_SNAKE_CASE ) a_ : int = tokenizer_r(__SCREAMING_SNAKE_CASE , return_offsets_mapping=__SCREAMING_SNAKE_CASE , add_special_tokens=__SCREAMING_SNAKE_CASE ) self.assertEqual(encoding.offset_mapping[0] , (0, 1 + len(__SCREAMING_SNAKE_CASE )) ) self.assertEqual( encoding.offset_mapping[1] , (1 + len(__SCREAMING_SNAKE_CASE ), 1 + len(__SCREAMING_SNAKE_CASE ) + 1 + len(__SCREAMING_SNAKE_CASE )) , )
666
0
'''simple docstring''' def _UpperCAmelCase ( __A : int ): assert isinstance(__A , __A ), f'The input value of [n={number}] is not an integer' if number == 1: return 2 elif number < 1: a_ : str = f'The input value of [n={number}] has to be > 0' raise ValueError(__A ) else: a_ : List[str] = sylvester(number - 1 ) a_ : Optional[Any] = num - 1 a_ : Union[str, Any] = num return lower * upper + 1 if __name__ == "__main__": print(F"""The 8th number in Sylvester's sequence: {sylvester(8)}""")
707
'''simple docstring''' import os from shutil import copyfile from typing import Any, Dict, List, Optional, Tuple import sentencepiece as spm from ...tokenization_utils import AddedToken, PreTrainedTokenizer from ...utils import logging __lowerCAmelCase = logging.get_logger(__name__) __lowerCAmelCase = {'vocab_file': 'sentencepiece.bpe.model'} __lowerCAmelCase = { 'vocab_file': { 'moussaKam/mbarthez': 'https://huggingface.co/moussaKam/mbarthez/resolve/main/sentencepiece.bpe.model', 'moussaKam/barthez': 'https://huggingface.co/moussaKam/barthez/resolve/main/sentencepiece.bpe.model', 'moussaKam/barthez-orangesum-title': ( 'https://huggingface.co/moussaKam/barthez-orangesum-title/resolve/main/sentencepiece.bpe.model' ), }, } __lowerCAmelCase = { 'moussaKam/mbarthez': 1_024, 'moussaKam/barthez': 1_024, 'moussaKam/barthez-orangesum-title': 1_024, } __lowerCAmelCase = 'โ–' class SCREAMING_SNAKE_CASE ( SCREAMING_SNAKE_CASE_ ): snake_case__ = VOCAB_FILES_NAMES snake_case__ = PRETRAINED_VOCAB_FILES_MAP snake_case__ = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES snake_case__ = ["input_ids", "attention_mask"] def __init__( self : Dict , __SCREAMING_SNAKE_CASE : str , __SCREAMING_SNAKE_CASE : Dict="<s>" , __SCREAMING_SNAKE_CASE : List[Any]="</s>" , __SCREAMING_SNAKE_CASE : List[str]="</s>" , __SCREAMING_SNAKE_CASE : List[str]="<s>" , __SCREAMING_SNAKE_CASE : Dict="<unk>" , __SCREAMING_SNAKE_CASE : int="<pad>" , __SCREAMING_SNAKE_CASE : Tuple="<mask>" , __SCREAMING_SNAKE_CASE : Optional[Dict[str, Any]] = None , **__SCREAMING_SNAKE_CASE : Optional[Any] , ) -> None: # Mask token behave like a normal word, i.e. include the space before it a_ : Tuple = AddedToken(__SCREAMING_SNAKE_CASE , lstrip=__SCREAMING_SNAKE_CASE , rstrip=__SCREAMING_SNAKE_CASE ) if isinstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) else mask_token a_ : List[str] = {} if sp_model_kwargs is None else sp_model_kwargs super().__init__( bos_token=__SCREAMING_SNAKE_CASE , eos_token=__SCREAMING_SNAKE_CASE , unk_token=__SCREAMING_SNAKE_CASE , sep_token=__SCREAMING_SNAKE_CASE , cls_token=__SCREAMING_SNAKE_CASE , pad_token=__SCREAMING_SNAKE_CASE , mask_token=__SCREAMING_SNAKE_CASE , sp_model_kwargs=self.sp_model_kwargs , **__SCREAMING_SNAKE_CASE , ) a_ : Tuple = vocab_file a_ : Optional[Any] = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(str(__SCREAMING_SNAKE_CASE ) ) a_ : Optional[Any] = {'''<s>''': 0, '''<pad>''': 1, '''</s>''': 2, '''<unk>''': 3} a_ : Any = len(self.sp_model ) - 1 a_ : Optional[Any] = {v: k for k, v in self.fairseq_tokens_to_ids.items()} def SCREAMING_SNAKE_CASE ( self : str , __SCREAMING_SNAKE_CASE : List[int] , __SCREAMING_SNAKE_CASE : Optional[List[int]] = None ) -> List[int]: if token_ids_a is None: return [self.cls_token_id] + token_ids_a + [self.sep_token_id] a_ : List[str] = [self.cls_token_id] a_ : Optional[Any] = [self.sep_token_id] return cls + token_ids_a + sep + sep + token_ids_a + sep def SCREAMING_SNAKE_CASE ( self : Optional[int] , __SCREAMING_SNAKE_CASE : List[int] , __SCREAMING_SNAKE_CASE : Optional[List[int]] = None , __SCREAMING_SNAKE_CASE : bool = False ) -> List[int]: 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 None: return [1] + ([0] * len(__SCREAMING_SNAKE_CASE )) + [1] return [1] + ([0] * len(__SCREAMING_SNAKE_CASE )) + [1, 1] + ([0] * len(__SCREAMING_SNAKE_CASE )) + [1] def SCREAMING_SNAKE_CASE ( self : Union[str, Any] , __SCREAMING_SNAKE_CASE : List[int] , __SCREAMING_SNAKE_CASE : Optional[List[int]] = None ) -> List[int]: a_ : List[str] = [self.sep_token_id] a_ : Any = [self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep ) * [0] return len(cls + token_ids_a + sep + sep + token_ids_a + sep ) * [0] @property def SCREAMING_SNAKE_CASE ( self : Any ) -> Union[str, Any]: return len(self.sp_model ) def SCREAMING_SNAKE_CASE ( self : str ) -> Tuple: a_ : int = {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 SCREAMING_SNAKE_CASE ( self : Optional[Any] , __SCREAMING_SNAKE_CASE : str ) -> List[str]: return self.sp_model.encode(__SCREAMING_SNAKE_CASE , out_type=__SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE ( self : str , __SCREAMING_SNAKE_CASE : List[Any] ) -> Dict: if token in self.fairseq_tokens_to_ids: return self.fairseq_tokens_to_ids[token] a_ : Optional[int] = self.sp_model.PieceToId(__SCREAMING_SNAKE_CASE ) return spm_id if spm_id else self.unk_token_id def SCREAMING_SNAKE_CASE ( self : List[str] , __SCREAMING_SNAKE_CASE : int ) -> str: if index in self.fairseq_ids_to_tokens: return self.fairseq_ids_to_tokens[index] return self.sp_model.IdToPiece(__SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE ( self : str , __SCREAMING_SNAKE_CASE : Optional[Any] ) -> Optional[int]: a_ : Dict = [] a_ : List[Any] = '''''' a_ : Dict = False for token in tokens: # make sure that special tokens are not decoded using sentencepiece model if token in self.all_special_tokens: if not prev_is_special: out_string += " " out_string += self.sp_model.decode(__SCREAMING_SNAKE_CASE ) + token a_ : Dict = True a_ : Optional[Any] = [] else: current_sub_tokens.append(__SCREAMING_SNAKE_CASE ) a_ : Tuple = False out_string += self.sp_model.decode(__SCREAMING_SNAKE_CASE ) return out_string.strip() def __getstate__( self : Dict ) -> int: a_ : Dict = self.__dict__.copy() a_ : List[str] = None return state def __setstate__( self : Dict , __SCREAMING_SNAKE_CASE : Optional[Any] ) -> Any: a_ : Optional[Any] = d # for backward compatibility if not hasattr(self , '''sp_model_kwargs''' ): a_ : Union[str, Any] = {} a_ : int = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(self.vocab_file ) def SCREAMING_SNAKE_CASE ( self : str , __SCREAMING_SNAKE_CASE : str , __SCREAMING_SNAKE_CASE : Optional[str] = None ) -> Tuple[str]: if not os.path.isdir(__SCREAMING_SNAKE_CASE ): logger.error(f'Vocabulary path ({save_directory}) should be a directory' ) return a_ : Union[str, Any] = 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: a_ : Any = self.sp_model.serialized_model_proto() fi.write(__SCREAMING_SNAKE_CASE ) return (out_vocab_file,)
666
0
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_sentencepiece_available, is_tokenizers_available, is_torch_available, ) __lowerCAmelCase = {'configuration_reformer': ['REFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP', 'ReformerConfig']} try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __lowerCAmelCase = ['ReformerTokenizer'] try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __lowerCAmelCase = ['ReformerTokenizerFast'] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __lowerCAmelCase = [ 'REFORMER_PRETRAINED_MODEL_ARCHIVE_LIST', 'ReformerAttention', 'ReformerForMaskedLM', 'ReformerForQuestionAnswering', 'ReformerForSequenceClassification', 'ReformerLayer', 'ReformerModel', 'ReformerModelWithLMHead', 'ReformerPreTrainedModel', ] if TYPE_CHECKING: from .configuration_reformer import REFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP, ReformerConfig try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_reformer import ReformerTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_reformer_fast import ReformerTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_reformer import ( REFORMER_PRETRAINED_MODEL_ARCHIVE_LIST, ReformerAttention, ReformerForMaskedLM, ReformerForQuestionAnswering, ReformerForSequenceClassification, ReformerLayer, ReformerModel, ReformerModelWithLMHead, ReformerPreTrainedModel, ) else: import sys __lowerCAmelCase = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
708
'''simple docstring''' def _UpperCAmelCase ( __A : str , __A : str ): def get_matched_characters(__A : str , __A : str ) -> str: a_ : Union[str, Any] = [] a_ : int = min(len(_stra ) , len(_stra ) ) // 2 for i, l in enumerate(_stra ): a_ : Any = int(max(0 , i - limit ) ) a_ : Union[str, Any] = int(min(i + limit + 1 , len(_stra ) ) ) if l in _stra[left:right]: matched.append(__A ) a_ : Any = f'{_stra[0:_stra.index(__A )]} {_stra[_stra.index(__A ) + 1:]}' return "".join(__A ) # matching characters a_ : Optional[Any] = get_matched_characters(__A , __A ) a_ : int = get_matched_characters(__A , __A ) a_ : Any = len(__A ) # transposition a_ : List[Any] = ( len([(ca, ca) for ca, ca in zip(__A , __A ) if ca != ca] ) // 2 ) if not match_count: a_ : Dict = 0.0 else: a_ : Optional[int] = ( 1 / 3 * ( match_count / len(__A ) + match_count / len(__A ) + (match_count - transpositions) / match_count ) ) # common prefix up to 4 characters a_ : List[str] = 0 for ca, ca in zip(stra[:4] , stra[:4] ): if ca == ca: prefix_len += 1 else: break return jaro + 0.1 * prefix_len * (1 - jaro) if __name__ == "__main__": import doctest doctest.testmod() print(jaro_winkler('hello', 'world'))
666
0
'''simple docstring''' import os import re import shutil import sys import tempfile import unittest import black __lowerCAmelCase = os.path.abspath(os.path.dirname(os.path.dirname(os.path.dirname(__file__)))) sys.path.append(os.path.join(git_repo_path, 'utils')) import check_copies # noqa: E402 # This is the reference code that will be used in the tests. # If DDPMSchedulerOutput is changed in scheduling_ddpm.py, this code needs to be manually updated. __lowerCAmelCase = ' \"""\n Output class for the scheduler\'s step function output.\n\n Args:\n prev_sample (`torch.FloatTensor` of shape `(batch_size, num_channels, height, width)` for images):\n Computed sample (x_{t-1}) of previous timestep. `prev_sample` should be used as next model input in the\n denoising loop.\n pred_original_sample (`torch.FloatTensor` of shape `(batch_size, num_channels, height, width)` for images):\n The predicted denoised sample (x_{0}) based on the model output from the current timestep.\n `pred_original_sample` can be used to preview progress or for guidance.\n \"""\n\n prev_sample: torch.FloatTensor\n pred_original_sample: Optional[torch.FloatTensor] = None\n' class SCREAMING_SNAKE_CASE ( unittest.TestCase ): def SCREAMING_SNAKE_CASE ( self : List[str] ) -> Union[str, Any]: a_ : Dict = tempfile.mkdtemp() os.makedirs(os.path.join(self.diffusers_dir , '''schedulers/''' ) ) a_ : List[str] = self.diffusers_dir shutil.copy( os.path.join(__SCREAMING_SNAKE_CASE , '''src/diffusers/schedulers/scheduling_ddpm.py''' ) , os.path.join(self.diffusers_dir , '''schedulers/scheduling_ddpm.py''' ) , ) def SCREAMING_SNAKE_CASE ( self : Union[str, Any] ) -> Dict: a_ : str = '''src/diffusers''' shutil.rmtree(self.diffusers_dir ) def SCREAMING_SNAKE_CASE ( self : List[Any] , __SCREAMING_SNAKE_CASE : List[Any] , __SCREAMING_SNAKE_CASE : int , __SCREAMING_SNAKE_CASE : Tuple , __SCREAMING_SNAKE_CASE : int=None ) -> Optional[int]: a_ : str = comment + f'\nclass {class_name}(nn.Module):\n' + class_code if overwrite_result is not None: a_ : Tuple = comment + f'\nclass {class_name}(nn.Module):\n' + overwrite_result a_ : List[str] = black.Mode(target_versions={black.TargetVersion.PYaa} , line_length=119 ) a_ : List[Any] = black.format_str(__SCREAMING_SNAKE_CASE , mode=__SCREAMING_SNAKE_CASE ) a_ : Tuple = os.path.join(self.diffusers_dir , '''new_code.py''' ) with open(__SCREAMING_SNAKE_CASE , '''w''' , newline='''\n''' ) as f: f.write(__SCREAMING_SNAKE_CASE ) if overwrite_result is None: self.assertTrue(len(check_copies.is_copy_consistent(__SCREAMING_SNAKE_CASE ) ) == 0 ) else: check_copies.is_copy_consistent(f.name , overwrite=__SCREAMING_SNAKE_CASE ) with open(__SCREAMING_SNAKE_CASE , '''r''' ) as f: self.assertTrue(f.read() , __SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE ( self : Tuple ) -> int: a_ : List[str] = check_copies.find_code_in_diffusers('''schedulers.scheduling_ddpm.DDPMSchedulerOutput''' ) self.assertEqual(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE ( self : Optional[int] ) -> Optional[Any]: # Base copy consistency self.check_copy_consistency( '''# Copied from diffusers.schedulers.scheduling_ddpm.DDPMSchedulerOutput''' , '''DDPMSchedulerOutput''' , REFERENCE_CODE + '''\n''' , ) # With no empty line at the end self.check_copy_consistency( '''# Copied from diffusers.schedulers.scheduling_ddpm.DDPMSchedulerOutput''' , '''DDPMSchedulerOutput''' , __SCREAMING_SNAKE_CASE , ) # Copy consistency with rename self.check_copy_consistency( '''# Copied from diffusers.schedulers.scheduling_ddpm.DDPMSchedulerOutput with DDPM->Test''' , '''TestSchedulerOutput''' , re.sub('''DDPM''' , '''Test''' , __SCREAMING_SNAKE_CASE ) , ) # Copy consistency with a really long name a_ : List[Any] = '''TestClassWithAReallyLongNameBecauseSomePeopleLikeThatForSomeReason''' self.check_copy_consistency( f'# Copied from diffusers.schedulers.scheduling_ddpm.DDPMSchedulerOutput with DDPM->{long_class_name}' , f'{long_class_name}SchedulerOutput' , re.sub('''Bert''' , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) , ) # Copy consistency with overwrite self.check_copy_consistency( '''# Copied from diffusers.schedulers.scheduling_ddpm.DDPMSchedulerOutput with DDPM->Test''' , '''TestSchedulerOutput''' , __SCREAMING_SNAKE_CASE , overwrite_result=re.sub('''DDPM''' , '''Test''' , __SCREAMING_SNAKE_CASE ) , )
709
'''simple docstring''' import torch from transformers import AutoModel class SCREAMING_SNAKE_CASE ( torch.nn.Module ): def __init__( self : Optional[int] , __SCREAMING_SNAKE_CASE : int="sayef/fsner-bert-base-uncased" ) -> str: super(__SCREAMING_SNAKE_CASE , self ).__init__() a_ : str = AutoModel.from_pretrained(__SCREAMING_SNAKE_CASE , return_dict=__SCREAMING_SNAKE_CASE ) a_ : Optional[int] = torch.nn.CosineSimilarity(3 , 1e-08 ) a_ : Dict = torch.nn.Softmax(dim=1 ) def SCREAMING_SNAKE_CASE ( self : str , **__SCREAMING_SNAKE_CASE : int ) -> str: return self.bert(**__SCREAMING_SNAKE_CASE ).last_hidden_state def SCREAMING_SNAKE_CASE ( self : List[str] , __SCREAMING_SNAKE_CASE : List[Any] ) -> Optional[int]: return token_embeddings.sum(2 , keepdim=__SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE ( self : List[Any] , __SCREAMING_SNAKE_CASE : Optional[int] , __SCREAMING_SNAKE_CASE : Union[str, Any] , __SCREAMING_SNAKE_CASE : int=1 ) -> Dict: return self.softmax(T * self.cos(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) ) def SCREAMING_SNAKE_CASE ( self : str , __SCREAMING_SNAKE_CASE : Optional[Any] , __SCREAMING_SNAKE_CASE : Optional[int] ) -> Any: a_ : Dict = W_supports['''sizes'''].tolist() a_ : Tuple = W_supports['''start_token_id'''].item() a_ : List[Any] = W_supports['''end_token_id'''].item() del W_supports["sizes"] del W_supports["start_token_id"] del W_supports["end_token_id"] a_ : int = self.BERT(**__SCREAMING_SNAKE_CASE ) a_ : Any = self.BERT(**__SCREAMING_SNAKE_CASE ) a_ : Optional[Any] = None a_ : Tuple = None a_ : List[str] = W_supports['''input_ids'''] == start_token_id a_ : Dict = W_supports['''input_ids'''] == end_token_id for i, size in enumerate(__SCREAMING_SNAKE_CASE ): if i == 0: a_ : str = 0 else: a_ : str = support_sizes[i - 1] a_ : Union[str, Any] = S[s : s + size][start_token_masks[s : s + size]] a_ : Tuple = S[s : s + size][end_token_masks[s : s + size]] a_ : Tuple = torch.matmul(q[i] , s_start.T ).sum(1 ).softmax(0 ) a_ : Optional[Any] = torch.matmul(q[i] , s_end.T ).sum(1 ).softmax(0 ) if p_starts is not None: a_ : Any = torch.vstack((p_starts, p_start) ) a_ : Dict = torch.vstack((p_ends, p_end) ) else: a_ : Optional[int] = p_start a_ : List[Any] = p_end return p_starts, p_ends
666
0
'''simple docstring''' import math class SCREAMING_SNAKE_CASE : def __init__( self : Dict , __SCREAMING_SNAKE_CASE : Optional[int]=0 ) -> int: # a graph with Node 0,1,...,N-1 a_ : Union[str, Any] = n a_ : List[Any] = [ [math.inf for j in range(0 , __SCREAMING_SNAKE_CASE )] for i in range(0 , __SCREAMING_SNAKE_CASE ) ] # adjacency matrix for weight a_ : List[str] = [ [math.inf for j in range(0 , __SCREAMING_SNAKE_CASE )] for i in range(0 , __SCREAMING_SNAKE_CASE ) ] # dp[i][j] stores minimum distance from i to j def SCREAMING_SNAKE_CASE ( self : List[Any] , __SCREAMING_SNAKE_CASE : str , __SCREAMING_SNAKE_CASE : Dict , __SCREAMING_SNAKE_CASE : Tuple ) -> Optional[int]: a_ : str = w def SCREAMING_SNAKE_CASE ( self : int ) -> Tuple: for k in range(0 , self.n ): for i in range(0 , self.n ): for j in range(0 , self.n ): a_ : str = min(self.dp[i][j] , self.dp[i][k] + self.dp[k][j] ) def SCREAMING_SNAKE_CASE ( self : Optional[int] , __SCREAMING_SNAKE_CASE : Optional[Any] , __SCREAMING_SNAKE_CASE : List[Any] ) -> Optional[int]: return self.dp[u][v] if __name__ == "__main__": __lowerCAmelCase = Graph(5) graph.add_edge(0, 2, 9) graph.add_edge(0, 4, 10) graph.add_edge(1, 3, 5) graph.add_edge(2, 3, 7) graph.add_edge(3, 0, 10) graph.add_edge(3, 1, 2) graph.add_edge(3, 2, 1) graph.add_edge(3, 4, 6) graph.add_edge(4, 1, 3) graph.add_edge(4, 2, 4) graph.add_edge(4, 3, 9) graph.floyd_warshall() graph.show_min(1, 4) graph.show_min(0, 3)
710
'''simple docstring''' import copy from dataclasses import dataclass, field from typing import ClassVar, Dict from ..features import ClassLabel, Features, Image from .base import TaskTemplate @dataclass(frozen=SCREAMING_SNAKE_CASE_ ) class SCREAMING_SNAKE_CASE ( SCREAMING_SNAKE_CASE_ ): snake_case__ = field(default="image-classification" , metadata={"include_in_asdict_even_if_is_default": True} ) snake_case__ = Features({"image": Image()} ) snake_case__ = Features({"labels": ClassLabel} ) snake_case__ = "image" snake_case__ = "labels" def SCREAMING_SNAKE_CASE ( self : List[str] , __SCREAMING_SNAKE_CASE : List[Any] ) -> Any: if self.label_column not in features: raise ValueError(f'Column {self.label_column} is not present in features.' ) if not isinstance(features[self.label_column] , __SCREAMING_SNAKE_CASE ): raise ValueError(f'Column {self.label_column} is not a ClassLabel.' ) a_ : Optional[int] = copy.deepcopy(self ) a_ : int = self.label_schema.copy() a_ : Tuple = features[self.label_column] a_ : str = label_schema return task_template @property def SCREAMING_SNAKE_CASE ( self : Tuple ) -> Dict[str, str]: return { self.image_column: "image", self.label_column: "labels", }
666
0
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available, is_vision_available __lowerCAmelCase = { 'configuration_conditional_detr': [ 'CONDITIONAL_DETR_PRETRAINED_CONFIG_ARCHIVE_MAP', 'ConditionalDetrConfig', 'ConditionalDetrOnnxConfig', ] } try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __lowerCAmelCase = ['ConditionalDetrFeatureExtractor'] __lowerCAmelCase = ['ConditionalDetrImageProcessor'] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __lowerCAmelCase = [ 'CONDITIONAL_DETR_PRETRAINED_MODEL_ARCHIVE_LIST', 'ConditionalDetrForObjectDetection', 'ConditionalDetrForSegmentation', 'ConditionalDetrModel', 'ConditionalDetrPreTrainedModel', ] if TYPE_CHECKING: from .configuration_conditional_detr import ( CONDITIONAL_DETR_PRETRAINED_CONFIG_ARCHIVE_MAP, ConditionalDetrConfig, ConditionalDetrOnnxConfig, ) try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .feature_extraction_conditional_detr import ConditionalDetrFeatureExtractor from .image_processing_conditional_detr import ConditionalDetrImageProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_conditional_detr import ( CONDITIONAL_DETR_PRETRAINED_MODEL_ARCHIVE_LIST, ConditionalDetrForObjectDetection, ConditionalDetrForSegmentation, ConditionalDetrModel, ConditionalDetrPreTrainedModel, ) else: import sys __lowerCAmelCase = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
711
'''simple docstring''' from __future__ import annotations def _UpperCAmelCase ( __A : tuple[int, int] , __A : int ): a_ , a_ : List[str] = position a_ : Optional[int] = [ (y + 1, x + 2), (y - 1, x + 2), (y + 1, x - 2), (y - 1, x - 2), (y + 2, x + 1), (y + 2, x - 1), (y - 2, x + 1), (y - 2, x - 1), ] a_ : Any = [] for position in positions: a_ , a_ : Dict = position if 0 <= y_test < n and 0 <= x_test < n: permissible_positions.append(__A ) return permissible_positions def _UpperCAmelCase ( __A : list[list[int]] ): return not any(elem == 0 for row in board for elem in row ) def _UpperCAmelCase ( __A : list[list[int]] , __A : tuple[int, int] , __A : int ): if is_complete(__A ): return True for position in get_valid_pos(__A , len(__A ) ): a_ , a_ : Dict = position if board[y][x] == 0: a_ : Optional[Any] = curr + 1 if open_knight_tour_helper(__A , __A , curr + 1 ): return True a_ : Tuple = 0 return False def _UpperCAmelCase ( __A : int ): a_ : List[str] = [[0 for i in range(__A )] for j in range(__A )] for i in range(__A ): for j in range(__A ): a_ : Optional[Any] = 1 if open_knight_tour_helper(__A , (i, j) , 1 ): return board a_ : Union[str, Any] = 0 a_ : Dict = f'Open Kight Tour cannot be performed on a board of size {n}' raise ValueError(__A ) if __name__ == "__main__": import doctest doctest.testmod()
666
0
'''simple docstring''' from unittest.mock import Mock, patch from file_transfer.send_file import send_file @patch('''socket.socket''' ) @patch('''builtins.open''' ) def _UpperCAmelCase ( __A : List[Any] , __A : List[Any] ): # ===== initialization ===== a_ : str = Mock() a_ : List[str] = conn, Mock() a_ : Dict = iter([1, None] ) a_ : List[str] = lambda __A : next(__A ) # ===== invoke ===== send_file(filename='''mytext.txt''' , testing=__A ) # ===== ensurance ===== sock.assert_called_once() sock.return_value.bind.assert_called_once() sock.return_value.listen.assert_called_once() sock.return_value.accept.assert_called_once() conn.recv.assert_called_once() file.return_value.__enter__.assert_called_once() file.return_value.__enter__.return_value.read.assert_called() conn.send.assert_called_once() conn.close.assert_called_once() sock.return_value.shutdown.assert_called_once() sock.return_value.close.assert_called_once()
712
'''simple docstring''' import warnings warnings.warn( 'memory_utils has been reorganized to utils.memory. Import `find_executable_batchsize` from the main `__init__`: ' '`from accelerate import find_executable_batch_size` to avoid this warning.', FutureWarning, )
666
0
'''simple docstring''' class SCREAMING_SNAKE_CASE : def __init__( self : Union[str, Any] , __SCREAMING_SNAKE_CASE : Union[str, Any] , __SCREAMING_SNAKE_CASE : List[Any] , __SCREAMING_SNAKE_CASE : Tuple ) -> List[str]: a_ : int = None a_ : int = None a_ : List[str] = graph self._normalize_graph(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) a_ : Tuple = len(__SCREAMING_SNAKE_CASE ) a_ : int = None def SCREAMING_SNAKE_CASE ( self : List[Any] , __SCREAMING_SNAKE_CASE : int , __SCREAMING_SNAKE_CASE : int ) -> Optional[Any]: if sources is int: a_ : Optional[int] = [sources] if sinks is int: a_ : Optional[int] = [sinks] if len(__SCREAMING_SNAKE_CASE ) == 0 or len(__SCREAMING_SNAKE_CASE ) == 0: return a_ : List[Any] = sources[0] a_ : Dict = sinks[0] # make fake vertex if there are more # than one source or sink if len(__SCREAMING_SNAKE_CASE ) > 1 or len(__SCREAMING_SNAKE_CASE ) > 1: a_ : List[str] = 0 for i in sources: max_input_flow += sum(self.graph[i] ) a_ : Tuple = len(self.graph ) + 1 for room in self.graph: room.insert(0 , 0 ) self.graph.insert(0 , [0] * size ) for i in sources: a_ : Tuple = max_input_flow a_ : List[Any] = 0 a_ : int = len(self.graph ) + 1 for room in self.graph: room.append(0 ) self.graph.append([0] * size ) for i in sinks: a_ : Any = max_input_flow a_ : str = size - 1 def SCREAMING_SNAKE_CASE ( self : Optional[int] ) -> List[Any]: if self.maximum_flow_algorithm is None: raise Exception('''You need to set maximum flow algorithm before.''' ) if self.source_index is None or self.sink_index is None: return 0 self.maximum_flow_algorithm.execute() return self.maximum_flow_algorithm.getMaximumFlow() def SCREAMING_SNAKE_CASE ( self : Optional[int] , __SCREAMING_SNAKE_CASE : int ) -> Optional[Any]: a_ : Any = algorithm(self ) class SCREAMING_SNAKE_CASE : def __init__( self : List[Any] , __SCREAMING_SNAKE_CASE : Optional[Any] ) -> Union[str, Any]: a_ : Dict = flow_network a_ : Tuple = flow_network.verticesCount a_ : Tuple = flow_network.sourceIndex a_ : List[Any] = flow_network.sinkIndex # it's just a reference, so you shouldn't change # it in your algorithms, use deep copy before doing that a_ : Dict = flow_network.graph a_ : str = False def SCREAMING_SNAKE_CASE ( self : Any ) -> Tuple: if not self.executed: self._algorithm() a_ : Any = True def SCREAMING_SNAKE_CASE ( self : List[str] ) -> Tuple: pass class SCREAMING_SNAKE_CASE ( SCREAMING_SNAKE_CASE_ ): def __init__( self : str , __SCREAMING_SNAKE_CASE : Dict ) -> Dict: super().__init__(__SCREAMING_SNAKE_CASE ) # use this to save your result a_ : Dict = -1 def SCREAMING_SNAKE_CASE ( self : Any ) -> Tuple: if not self.executed: raise Exception('''You should execute algorithm before using its result!''' ) return self.maximum_flow class SCREAMING_SNAKE_CASE ( SCREAMING_SNAKE_CASE_ ): def __init__( self : List[Any] , __SCREAMING_SNAKE_CASE : List[str] ) -> List[Any]: super().__init__(__SCREAMING_SNAKE_CASE ) a_ : List[str] = [[0] * self.verticies_count for i in range(self.verticies_count )] a_ : Dict = [0] * self.verticies_count a_ : Any = [0] * self.verticies_count def SCREAMING_SNAKE_CASE ( self : Any ) -> Union[str, Any]: a_ : Dict = self.verticies_count # push some substance to graph for nextvertex_index, bandwidth in enumerate(self.graph[self.source_index] ): self.preflow[self.source_index][nextvertex_index] += bandwidth self.preflow[nextvertex_index][self.source_index] -= bandwidth self.excesses[nextvertex_index] += bandwidth # Relabel-to-front selection rule a_ : Optional[int] = [ i for i in range(self.verticies_count ) if i != self.source_index and i != self.sink_index ] # move through list a_ : List[Any] = 0 while i < len(__SCREAMING_SNAKE_CASE ): a_ : Optional[int] = vertices_list[i] a_ : Optional[Any] = self.heights[vertex_index] self.process_vertex(__SCREAMING_SNAKE_CASE ) if self.heights[vertex_index] > previous_height: # if it was relabeled, swap elements # and start from 0 index vertices_list.insert(0 , vertices_list.pop(__SCREAMING_SNAKE_CASE ) ) a_ : Dict = 0 else: i += 1 a_ : Dict = sum(self.preflow[self.source_index] ) def SCREAMING_SNAKE_CASE ( self : Optional[Any] , __SCREAMING_SNAKE_CASE : Optional[Any] ) -> Union[str, Any]: while self.excesses[vertex_index] > 0: for neighbour_index in range(self.verticies_count ): # if it's neighbour and current vertex is higher if ( self.graph[vertex_index][neighbour_index] - self.preflow[vertex_index][neighbour_index] > 0 and self.heights[vertex_index] > self.heights[neighbour_index] ): self.push(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) self.relabel(__SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE ( self : List[Any] , __SCREAMING_SNAKE_CASE : str , __SCREAMING_SNAKE_CASE : str ) -> Tuple: a_ : str = min( self.excesses[from_index] , self.graph[from_index][to_index] - self.preflow[from_index][to_index] , ) self.preflow[from_index][to_index] += preflow_delta self.preflow[to_index][from_index] -= preflow_delta self.excesses[from_index] -= preflow_delta self.excesses[to_index] += preflow_delta def SCREAMING_SNAKE_CASE ( self : Dict , __SCREAMING_SNAKE_CASE : Optional[Any] ) -> Dict: a_ : List[str] = None for to_index in range(self.verticies_count ): if ( self.graph[vertex_index][to_index] - self.preflow[vertex_index][to_index] > 0 ) and (min_height is None or self.heights[to_index] < min_height): a_ : Optional[Any] = self.heights[to_index] if min_height is not None: a_ : Union[str, Any] = min_height + 1 if __name__ == "__main__": __lowerCAmelCase = [0] __lowerCAmelCase = [3] # graph = [ # [0, 0, 4, 6, 0, 0], # [0, 0, 5, 2, 0, 0], # [0, 0, 0, 0, 4, 4], # [0, 0, 0, 0, 6, 6], # [0, 0, 0, 0, 0, 0], # [0, 0, 0, 0, 0, 0], # ] __lowerCAmelCase = [[0, 7, 0, 0], [0, 0, 6, 0], [0, 0, 0, 8], [9, 0, 0, 0]] # prepare our network __lowerCAmelCase = FlowNetwork(graph, entrances, exits) # set algorithm flow_network.set_maximum_flow_algorithm(PushRelabelExecutor) # and calculate __lowerCAmelCase = flow_network.find_maximum_flow() print(F"""maximum flow is {maximum_flow}""")
713
'''simple docstring''' import requests from bsa import BeautifulSoup def _UpperCAmelCase ( __A : str , __A : dict ): a_ : Tuple = BeautifulSoup(requests.get(__A , params=__A ).content , '''html.parser''' ) a_ : List[str] = soup.find('''div''' , attrs={'''class''': '''gs_ri'''} ) a_ : List[str] = div.find('''div''' , attrs={'''class''': '''gs_fl'''} ).find_all('''a''' ) return anchors[2].get_text() if __name__ == "__main__": __lowerCAmelCase = { 'title': ( 'Precisely geometry controlled microsupercapacitors for ultrahigh areal ' 'capacitance, volumetric capacitance, and energy density' ), 'journal': 'Chem. Mater.', 'volume': 30, 'pages': '3979-3990', 'year': 2_018, 'hl': 'en', } print(get_citation('https://scholar.google.com/scholar_lookup', params=params))
666
0
'''simple docstring''' import json import os import unittest from transformers.models.xlm.tokenization_xlm import VOCAB_FILES_NAMES, XLMTokenizer from transformers.testing_utils import slow from ...test_tokenization_common import TokenizerTesterMixin class SCREAMING_SNAKE_CASE ( SCREAMING_SNAKE_CASE_ , unittest.TestCase ): snake_case__ = XLMTokenizer snake_case__ = False def SCREAMING_SNAKE_CASE ( self : str ) -> str: super().setUp() # Adapted from Sennrich et al. 2015 and https://github.com/rsennrich/subword-nmt a_ : Union[str, 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>''', ] a_ : Any = dict(zip(__SCREAMING_SNAKE_CASE , range(len(__SCREAMING_SNAKE_CASE ) ) ) ) a_ : Dict = ['''l o 123''', '''lo w 1456''', '''e r</w> 1789''', ''''''] a_ : List[str] = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES['''vocab_file'''] ) a_ : Any = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES['''merges_file'''] ) with open(self.vocab_file , '''w''' ) as fp: fp.write(json.dumps(__SCREAMING_SNAKE_CASE ) ) with open(self.merges_file , '''w''' ) as fp: fp.write('''\n'''.join(__SCREAMING_SNAKE_CASE ) ) def SCREAMING_SNAKE_CASE ( self : str , __SCREAMING_SNAKE_CASE : Tuple ) -> Union[str, Any]: a_ : int = '''lower newer''' a_ : Optional[Any] = '''lower newer''' return input_text, output_text def SCREAMING_SNAKE_CASE ( self : Union[str, Any] ) -> Any: a_ : Dict = XLMTokenizer(self.vocab_file , self.merges_file ) a_ : str = '''lower''' a_ : List[str] = ['''low''', '''er</w>'''] a_ : Dict = tokenizer.tokenize(__SCREAMING_SNAKE_CASE ) self.assertListEqual(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) a_ : Optional[int] = tokens + ['''<unk>'''] a_ : Optional[Any] = [14, 15, 20] self.assertListEqual(tokenizer.convert_tokens_to_ids(__SCREAMING_SNAKE_CASE ) , __SCREAMING_SNAKE_CASE ) @slow def SCREAMING_SNAKE_CASE ( self : Optional[int] ) -> Optional[Any]: a_ : Optional[Any] = XLMTokenizer.from_pretrained('''xlm-mlm-en-2048''' ) a_ : str = tokenizer.encode('''sequence builders''' , add_special_tokens=__SCREAMING_SNAKE_CASE ) a_ : str = tokenizer.encode('''multi-sequence build''' , add_special_tokens=__SCREAMING_SNAKE_CASE ) a_ : Dict = tokenizer.build_inputs_with_special_tokens(__SCREAMING_SNAKE_CASE ) a_ : int = tokenizer.build_inputs_with_special_tokens(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) assert encoded_sentence == [0] + text + [1] assert encoded_pair == [0] + text + [1] + text_a + [1]
714
'''simple docstring''' import argparse from torch import nn # transformers_old should correspond to branch `save_old_prophetnet_model_structure` here # original prophetnet_checkpoints are saved under `patrickvonplaten/..._old` respectively from transformers_old.modeling_prophetnet import ( ProphetNetForConditionalGeneration as ProphetNetForConditionalGenerationOld, ) from transformers_old.modeling_xlm_prophetnet import ( XLMProphetNetForConditionalGeneration as XLMProphetNetForConditionalGenerationOld, ) from transformers import ProphetNetForConditionalGeneration, XLMProphetNetForConditionalGeneration, logging __lowerCAmelCase = logging.get_logger(__name__) logging.set_verbosity_info() def _UpperCAmelCase ( __A : str , __A : str ): if "xprophetnet" in prophetnet_checkpoint_path: a_ : Tuple = XLMProphetNetForConditionalGenerationOld.from_pretrained(__A ) a_ , a_ : Optional[Any] = XLMProphetNetForConditionalGeneration.from_pretrained( __A , output_loading_info=__A ) else: a_ : List[Any] = ProphetNetForConditionalGenerationOld.from_pretrained(__A ) a_ , a_ : Any = ProphetNetForConditionalGeneration.from_pretrained( __A , output_loading_info=__A ) a_ : str = ['''key_proj''', '''value_proj''', '''query_proj'''] a_ : Tuple = { '''self_attn''': '''ngram_self_attn''', '''cross_attn''': '''encoder_attn''', '''cross_attn_layer_norm''': '''encoder_attn_layer_norm''', '''feed_forward_layer_norm''': '''final_layer_norm''', '''feed_forward''': '''''', '''intermediate''': '''fc1''', '''output''': '''fc2''', '''key_proj''': '''k_proj''', '''query_proj''': '''q_proj''', '''value_proj''': '''v_proj''', '''word_embeddings''': '''embed_tokens''', '''embeddings_layer_norm''': '''emb_layer_norm''', '''relative_pos_embeddings''': '''relative_linear''', '''ngram_embeddings''': '''ngram_input_embed''', '''position_embeddings''': '''embed_positions''', } for key in loading_info["missing_keys"]: a_ : List[str] = key.split('''.''' ) if attributes[0] == "lm_head": a_ : List[str] = prophet a_ : Dict = prophet_old else: a_ : str = prophet.prophetnet a_ : int = prophet_old.model a_ : str = False for attribute in attributes: if attribute in mapping: a_ : Dict = mapping[attribute] if not hasattr(__A , __A ) and len(__A ) > 0: a_ : List[str] = attribute elif hasattr(__A , __A ): a_ : Union[str, Any] = attribute if attribute == "weight": assert old_model.weight.shape == model.weight.shape, "Shapes have to match!" a_ : Tuple = old_model.weight logger.info(f'{attribute} is initialized.' ) a_ : Union[str, Any] = True break elif attribute == "bias": assert old_model.bias.shape == model.bias.shape, "Shapes have to match!" a_ : Union[str, Any] = old_model.bias logger.info(f'{attribute} is initialized' ) a_ : Dict = True break elif attribute in special_keys and hasattr(__A , '''in_proj_weight''' ): a_ : Tuple = old_model.in_proj_weight.shape[0] // 3 a_ : Any = getattr(__A , __A ) param.weight.shape == old_model.in_proj_weight[:embed_dim, :].shape, "Shapes have to match" param.bias.shape == old_model.in_proj_bias[:embed_dim].shape, "Shapes have to match" if attribute == "query_proj": a_ : Union[str, Any] = nn.Parameter(old_model.in_proj_weight[:embed_dim, :] ) a_ : Optional[Any] = nn.Parameter(old_model.in_proj_bias[:embed_dim] ) elif attribute == "key_proj": a_ : List[Any] = nn.Parameter(old_model.in_proj_weight[embed_dim : 2 * embed_dim, :] ) a_ : Optional[int] = nn.Parameter(old_model.in_proj_bias[embed_dim : 2 * embed_dim] ) elif attribute == "value_proj": a_ : Tuple = nn.Parameter(old_model.in_proj_weight[2 * embed_dim :, :] ) a_ : Any = nn.Parameter(old_model.in_proj_bias[2 * embed_dim :] ) a_ : Dict = True break elif attribute == "position_embeddings": assert ( model.position_embeddings.weight.shape[-1] == old_model.embed_positions.weight.shape[-1] ), "Hidden size has to match" assert model.position_embeddings.weight.shape[0] == 5_12, "We want 512 position_embeddings." a_ : Union[str, Any] = nn.Parameter(old_model.embed_positions.weight[:5_12, :] ) a_ : Optional[Any] = True break if attribute.isdigit(): a_ : Union[str, Any] = model[int(__A )] a_ : str = old_model[int(__A )] else: a_ : Tuple = getattr(__A , __A ) if old_attribute == "": a_ : List[str] = old_model else: if not hasattr(__A , __A ): raise ValueError(f'{old_model} does not have {old_attribute}' ) a_ : Optional[Any] = getattr(__A , __A ) if not is_key_init: raise ValueError(f'{key} was not correctly initialized!' ) print(f'Saving model to {pytorch_dump_folder_path}' ) prophet.save_pretrained(__A ) if __name__ == "__main__": __lowerCAmelCase = argparse.ArgumentParser() # Required parameters parser.add_argument( '--prophetnet_checkpoint_path', default=None, type=str, required=True, help='Path the official PyTorch dump.' ) parser.add_argument( '--pytorch_dump_folder_path', default=None, type=str, required=True, help='Path to the output PyTorch model.' ) __lowerCAmelCase = parser.parse_args() convert_prophetnet_checkpoint_to_pytorch(args.prophetnet_checkpoint_path, args.pytorch_dump_folder_path)
666
0
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available __lowerCAmelCase = { 'configuration_megatron_bert': ['MEGATRON_BERT_PRETRAINED_CONFIG_ARCHIVE_MAP', 'MegatronBertConfig'], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __lowerCAmelCase = [ 'MEGATRON_BERT_PRETRAINED_MODEL_ARCHIVE_LIST', 'MegatronBertForCausalLM', 'MegatronBertForMaskedLM', 'MegatronBertForMultipleChoice', 'MegatronBertForNextSentencePrediction', 'MegatronBertForPreTraining', 'MegatronBertForQuestionAnswering', 'MegatronBertForSequenceClassification', 'MegatronBertForTokenClassification', 'MegatronBertModel', 'MegatronBertPreTrainedModel', ] if TYPE_CHECKING: from .configuration_megatron_bert import MEGATRON_BERT_PRETRAINED_CONFIG_ARCHIVE_MAP, MegatronBertConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_megatron_bert import ( MEGATRON_BERT_PRETRAINED_MODEL_ARCHIVE_LIST, MegatronBertForCausalLM, MegatronBertForMaskedLM, MegatronBertForMultipleChoice, MegatronBertForNextSentencePrediction, MegatronBertForPreTraining, MegatronBertForQuestionAnswering, MegatronBertForSequenceClassification, MegatronBertForTokenClassification, MegatronBertModel, MegatronBertPreTrainedModel, ) else: import sys __lowerCAmelCase = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
715
'''simple docstring''' import re import string import numpy as np import datasets __lowerCAmelCase = '\nReturns the rate at which the input predicted strings exactly match their references, ignoring any strings input as part of the regexes_to_ignore list.\n' __lowerCAmelCase = '\nArgs:\n predictions: List of predicted texts.\n references: List of reference texts.\n regexes_to_ignore: List, defaults to None. Regex expressions of characters to\n ignore when calculating the exact matches. Note: these regexes are removed\n from the input data before the changes based on the options below (e.g. ignore_case,\n ignore_punctuation, ignore_numbers) are applied.\n ignore_case: Boolean, defaults to False. If true, turns everything\n to lowercase so that capitalization differences are ignored.\n ignore_punctuation: Boolean, defaults to False. If true, removes all punctuation before\n comparing predictions and references.\n ignore_numbers: Boolean, defaults to False. If true, removes all punctuation before\n comparing predictions and references.\nReturns:\n exact_match: Dictionary containing exact_match rate. Possible values are between 0.0 and 100.0, inclusive.\nExamples:\n >>> exact_match = datasets.load_metric("exact_match")\n >>> refs = ["the cat", "theater", "YELLING", "agent007"]\n >>> preds = ["cat?", "theater", "yelling", "agent"]\n >>> results = exact_match.compute(references=refs, predictions=preds)\n >>> print(round(results["exact_match"], 1))\n 25.0\n\n >>> exact_match = datasets.load_metric("exact_match")\n >>> refs = ["the cat", "theater", "YELLING", "agent007"]\n >>> preds = ["cat?", "theater", "yelling", "agent"]\n >>> results = exact_match.compute(references=refs, predictions=preds, regexes_to_ignore=["the ", "yell"], ignore_case=True, ignore_punctuation=True)\n >>> print(round(results["exact_match"], 1))\n 50.0\n\n\n >>> exact_match = datasets.load_metric("exact_match")\n >>> refs = ["the cat", "theater", "YELLING", "agent007"]\n >>> preds = ["cat?", "theater", "yelling", "agent"]\n >>> results = exact_match.compute(references=refs, predictions=preds, regexes_to_ignore=["the ", "yell", "YELL"], ignore_case=True, ignore_punctuation=True)\n >>> print(round(results["exact_match"], 1))\n 75.0\n\n >>> exact_match = datasets.load_metric("exact_match")\n >>> refs = ["the cat", "theater", "YELLING", "agent007"]\n >>> preds = ["cat?", "theater", "yelling", "agent"]\n >>> results = exact_match.compute(references=refs, predictions=preds, regexes_to_ignore=["the ", "yell", "YELL"], ignore_case=True, ignore_punctuation=True, ignore_numbers=True)\n >>> print(round(results["exact_match"], 1))\n 100.0\n\n >>> exact_match = datasets.load_metric("exact_match")\n >>> refs = ["The cat sat on the mat.", "Theaters are great.", "It\'s like comparing oranges and apples."]\n >>> preds = ["The cat sat on the mat?", "Theaters are great.", "It\'s like comparing apples and oranges."]\n >>> results = exact_match.compute(references=refs, predictions=preds)\n >>> print(round(results["exact_match"], 1))\n 33.3\n\n' __lowerCAmelCase = '\n' @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class SCREAMING_SNAKE_CASE ( datasets.Metric ): def SCREAMING_SNAKE_CASE ( self : Optional[Any] ) -> int: return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { '''predictions''': datasets.Value('''string''' , id='''sequence''' ), '''references''': datasets.Value('''string''' , id='''sequence''' ), } ) , reference_urls=[] , ) def SCREAMING_SNAKE_CASE ( self : str , __SCREAMING_SNAKE_CASE : List[Any] , __SCREAMING_SNAKE_CASE : Optional[int] , __SCREAMING_SNAKE_CASE : Dict=None , __SCREAMING_SNAKE_CASE : int=False , __SCREAMING_SNAKE_CASE : Optional[int]=False , __SCREAMING_SNAKE_CASE : Dict=False , ) -> str: if regexes_to_ignore is not None: for s in regexes_to_ignore: a_ : Optional[Any] = np.array([re.sub(__SCREAMING_SNAKE_CASE , '''''' , __SCREAMING_SNAKE_CASE ) for x in predictions] ) a_ : int = np.array([re.sub(__SCREAMING_SNAKE_CASE , '''''' , __SCREAMING_SNAKE_CASE ) for x in references] ) else: a_ : List[str] = np.asarray(__SCREAMING_SNAKE_CASE ) a_ : Any = np.asarray(__SCREAMING_SNAKE_CASE ) if ignore_case: a_ : List[str] = np.char.lower(__SCREAMING_SNAKE_CASE ) a_ : List[Any] = np.char.lower(__SCREAMING_SNAKE_CASE ) if ignore_punctuation: a_ : Any = string.punctuation.maketrans('''''' , '''''' , string.punctuation ) a_ : Union[str, Any] = np.char.translate(__SCREAMING_SNAKE_CASE , table=__SCREAMING_SNAKE_CASE ) a_ : int = np.char.translate(__SCREAMING_SNAKE_CASE , table=__SCREAMING_SNAKE_CASE ) if ignore_numbers: a_ : int = string.digits.maketrans('''''' , '''''' , string.digits ) a_ : Optional[int] = np.char.translate(__SCREAMING_SNAKE_CASE , table=__SCREAMING_SNAKE_CASE ) a_ : Dict = np.char.translate(__SCREAMING_SNAKE_CASE , table=__SCREAMING_SNAKE_CASE ) a_ : Optional[Any] = predictions == references return {"exact_match": np.mean(__SCREAMING_SNAKE_CASE ) * 100}
666
0
'''simple docstring''' import argparse import logging import os import re import tensorflow as tf from transformers import ( AutoConfig, AutoTokenizer, DataCollatorForLanguageModeling, PushToHubCallback, TFAutoModelForMaskedLM, create_optimizer, ) __lowerCAmelCase = logging.getLogger(__name__) __lowerCAmelCase = tf.data.AUTOTUNE def _UpperCAmelCase ( ): a_ : Optional[Any] = argparse.ArgumentParser(description='''Train a masked language model on TPU.''' ) parser.add_argument( '''--pretrained_model_config''' , type=__A , default='''roberta-base''' , help='''The model config to use. Note that we don\'t copy the model\'s weights, only the config!''' , ) parser.add_argument( '''--tokenizer''' , type=__A , default='''unigram-tokenizer-wikitext''' , help='''The name of the tokenizer to load. We use the pretrained tokenizer to initialize the model\'s vocab size.''' , ) parser.add_argument( '''--per_replica_batch_size''' , type=__A , default=8 , help='''Batch size per TPU core.''' , ) parser.add_argument( '''--no_tpu''' , action='''store_true''' , help='''If set, run on CPU and don\'t try to initialize a TPU. Useful for debugging on non-TPU instances.''' , ) parser.add_argument( '''--tpu_name''' , type=__A , help='''Name of TPU resource to initialize. Should be blank on Colab, and \'local\' on TPU VMs.''' , default='''local''' , ) parser.add_argument( '''--tpu_zone''' , type=__A , help='''Google cloud zone that TPU resource is located in. Only used for non-Colab TPU nodes.''' , ) parser.add_argument( '''--gcp_project''' , type=__A , help='''Google cloud project name. Only used for non-Colab TPU nodes.''' ) parser.add_argument( '''--bfloat16''' , action='''store_true''' , help='''Use mixed-precision bfloat16 for training. This is the recommended lower-precision format for TPU.''' , ) parser.add_argument( '''--train_dataset''' , type=__A , help='''Path to training dataset to load. If the path begins with `gs://`''' ''' then the dataset will be loaded from a Google Cloud Storage bucket.''' , ) parser.add_argument( '''--shuffle_buffer_size''' , type=__A , default=2**18 , help='''Size of the shuffle buffer (in samples)''' , ) parser.add_argument( '''--eval_dataset''' , type=__A , help='''Path to evaluation dataset to load. If the path begins with `gs://`''' ''' then the dataset will be loaded from a Google Cloud Storage bucket.''' , ) parser.add_argument( '''--num_epochs''' , type=__A , default=1 , help='''Number of epochs to train for.''' , ) parser.add_argument( '''--learning_rate''' , type=__A , default=1E-4 , help='''Learning rate to use for training.''' , ) parser.add_argument( '''--weight_decay_rate''' , type=__A , default=1E-3 , help='''Weight decay rate to use for training.''' , ) parser.add_argument( '''--max_length''' , type=__A , default=5_12 , help='''Maximum length of tokenized sequences. Should match the setting used in prepare_tfrecord_shards.py''' , ) parser.add_argument( '''--mlm_probability''' , type=__A , default=0.15 , help='''Fraction of tokens to mask during training.''' , ) parser.add_argument('''--output_dir''' , type=__A , required=__A , help='''Path to save model checkpoints to.''' ) parser.add_argument('''--hub_model_id''' , type=__A , help='''Model ID to upload to on the Hugging Face Hub.''' ) a_ : Optional[Any] = parser.parse_args() return args def _UpperCAmelCase ( __A : Dict ): try: if args.tpu_name: a_ : Any = tf.distribute.cluster_resolver.TPUClusterResolver( args.tpu_name , zone=args.tpu_zone , project=args.gcp_project ) else: a_ : Tuple = tf.distribute.cluster_resolver.TPUClusterResolver() except ValueError: raise RuntimeError( '''Couldn\'t connect to TPU! Most likely you need to specify --tpu_name, --tpu_zone, or ''' '''--gcp_project. When running on a TPU VM, use --tpu_name local.''' ) tf.config.experimental_connect_to_cluster(__A ) tf.tpu.experimental.initialize_tpu_system(__A ) return tpu def _UpperCAmelCase ( __A : int ): a_ : Tuple = 0 for file in file_list: a_ : Optional[Any] = file.split('''/''' )[-1] a_ : Optional[int] = re.search(R'''-\d+-(\d+)\.tfrecord''' , __A ).group(1 ) a_ : Optional[Any] = int(__A ) num_samples += sample_count return num_samples def _UpperCAmelCase ( __A : Union[str, Any] , __A : Any , __A : List[Any] , __A : Tuple , __A : List[Any] , __A : Optional[Any]=None ): a_ : str = count_samples(__A ) a_ : Any = tf.data.Dataset.from_tensor_slices(__A ) if shuffle: a_ : Union[str, Any] = dataset.shuffle(len(__A ) ) a_ : str = tf.data.TFRecordDataset(__A , num_parallel_reads=__A ) # TF can't infer the total sample count because it doesn't read all the records yet, so we assert it here a_ : Tuple = dataset.apply(tf.data.experimental.assert_cardinality(__A ) ) a_ : str = dataset.map(__A , num_parallel_calls=__A ) if shuffle: assert shuffle_buffer_size is not None a_ : Optional[int] = dataset.shuffle(args.shuffle_buffer_size ) a_ : Any = dataset.batch(__A , drop_remainder=__A ) a_ : Optional[Any] = dataset.map(__A , num_parallel_calls=__A ) a_ : Optional[int] = dataset.prefetch(__A ) return dataset def _UpperCAmelCase ( __A : List[Any] ): if not args.no_tpu: a_ : Optional[int] = initialize_tpu(__A ) a_ : Dict = tf.distribute.TPUStrategy(__A ) else: a_ : Dict = tf.distribute.OneDeviceStrategy(device='''/gpu:0''' ) if args.bfloataa: tf.keras.mixed_precision.set_global_policy('''mixed_bfloat16''' ) a_ : List[Any] = AutoTokenizer.from_pretrained(args.tokenizer ) a_ : Any = AutoConfig.from_pretrained(args.pretrained_model_config ) a_ : Tuple = tokenizer.vocab_size a_ : Tuple = tf.io.gfile.glob(os.path.join(args.train_dataset , '''*.tfrecord''' ) ) if not training_records: raise ValueError(f'No .tfrecord files found in {args.train_dataset}.' ) a_ : Optional[int] = tf.io.gfile.glob(os.path.join(args.eval_dataset , '''*.tfrecord''' ) ) if not eval_records: raise ValueError(f'No .tfrecord files found in {args.eval_dataset}.' ) a_ : Any = count_samples(__A ) a_ : Optional[Any] = num_train_samples // (args.per_replica_batch_size * strategy.num_replicas_in_sync) a_ : Union[str, Any] = steps_per_epoch * args.num_epochs with strategy.scope(): a_ : Optional[Any] = TFAutoModelForMaskedLM.from_config(__A ) model(model.dummy_inputs ) # Pass some dummy inputs through the model to ensure all the weights are built a_ : int = create_optimizer( num_train_steps=__A , num_warmup_steps=total_train_steps // 20 , init_lr=args.learning_rate , weight_decay_rate=args.weight_decay_rate , ) # Transformers models compute the right loss for their task by default when labels are passed, and will # use this for training unless you specify your own loss function in compile(). model.compile(optimizer=__A , metrics=['''accuracy'''] ) def decode_fn(__A : Dict ): a_ : Tuple = { '''input_ids''': tf.io.FixedLenFeature(dtype=tf.intaa , shape=(args.max_length,) ), '''attention_mask''': tf.io.FixedLenFeature(dtype=tf.intaa , shape=(args.max_length,) ), } return tf.io.parse_single_example(__A , __A ) # Many of the data collators in Transformers are TF-compilable when return_tensors == "tf", so we can # use their methods in our data pipeline. a_ : Tuple = DataCollatorForLanguageModeling( tokenizer=__A , mlm_probability=args.mlm_probability , mlm=__A , return_tensors='''tf''' ) def mask_with_collator(__A : Dict ): # TF really needs an isin() function a_ : int = ( ~tf.cast(batch['''attention_mask'''] , tf.bool ) | (batch['''input_ids'''] == tokenizer.cls_token_id) | (batch['''input_ids'''] == tokenizer.sep_token_id) ) a_ : int = data_collator.tf_mask_tokens( batch['''input_ids'''] , vocab_size=len(__A ) , mask_token_id=tokenizer.mask_token_id , special_tokens_mask=__A , ) return batch a_ : List[str] = args.per_replica_batch_size * strategy.num_replicas_in_sync a_ : Union[str, Any] = prepare_dataset( __A , decode_fn=__A , mask_fn=__A , batch_size=__A , shuffle=__A , shuffle_buffer_size=args.shuffle_buffer_size , ) a_ : str = prepare_dataset( __A , decode_fn=__A , mask_fn=__A , batch_size=__A , shuffle=__A , ) a_ : Dict = [] if args.hub_model_id: callbacks.append( PushToHubCallback(output_dir=args.output_dir , hub_model_id=args.hub_model_id , tokenizer=__A ) ) model.fit( __A , validation_data=__A , epochs=args.num_epochs , callbacks=__A , ) model.save_pretrained(args.output_dir ) if __name__ == "__main__": __lowerCAmelCase = parse_args() main(args)
716
'''simple docstring''' import gc import unittest import numpy as np import torch from diffusers import ( AudioDiffusionPipeline, AutoencoderKL, DDIMScheduler, DDPMScheduler, DiffusionPipeline, Mel, UNetaDConditionModel, UNetaDModel, ) from diffusers.utils import slow, torch_device from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu enable_full_determinism() class SCREAMING_SNAKE_CASE ( unittest.TestCase ): def SCREAMING_SNAKE_CASE ( self : Dict ) -> List[Any]: # clean up the VRAM after each test super().tearDown() gc.collect() torch.cuda.empty_cache() @property def SCREAMING_SNAKE_CASE ( self : Tuple ) -> List[Any]: torch.manual_seed(0 ) a_ : Tuple = UNetaDModel( sample_size=(32, 64) , in_channels=1 , out_channels=1 , layers_per_block=2 , block_out_channels=(128, 128) , down_block_types=('''AttnDownBlock2D''', '''DownBlock2D''') , up_block_types=('''UpBlock2D''', '''AttnUpBlock2D''') , ) return model @property def SCREAMING_SNAKE_CASE ( self : int ) -> List[Any]: torch.manual_seed(0 ) a_ : Any = UNetaDConditionModel( sample_size=(64, 32) , in_channels=1 , out_channels=1 , layers_per_block=2 , block_out_channels=(128, 128) , down_block_types=('''CrossAttnDownBlock2D''', '''DownBlock2D''') , up_block_types=('''UpBlock2D''', '''CrossAttnUpBlock2D''') , cross_attention_dim=10 , ) return model @property def SCREAMING_SNAKE_CASE ( self : Optional[Any] ) -> List[str]: torch.manual_seed(0 ) a_ : List[Any] = AutoencoderKL( sample_size=(128, 64) , in_channels=1 , out_channels=1 , latent_channels=1 , layers_per_block=2 , block_out_channels=(128, 128) , down_block_types=('''DownEncoderBlock2D''', '''DownEncoderBlock2D''') , up_block_types=('''UpDecoderBlock2D''', '''UpDecoderBlock2D''') , ) a_ : List[Any] = UNetaDModel( sample_size=(64, 32) , in_channels=1 , out_channels=1 , layers_per_block=2 , block_out_channels=(128, 128) , down_block_types=('''AttnDownBlock2D''', '''DownBlock2D''') , up_block_types=('''UpBlock2D''', '''AttnUpBlock2D''') , ) return vqvae, unet @slow def SCREAMING_SNAKE_CASE ( self : Union[str, Any] ) -> Union[str, Any]: a_ : Dict = '''cpu''' # ensure determinism for the device-dependent torch.Generator a_ : Union[str, Any] = Mel( x_res=self.dummy_unet.config.sample_size[1] , y_res=self.dummy_unet.config.sample_size[0] , ) a_ : Any = DDPMScheduler() a_ : str = AudioDiffusionPipeline(vqvae=__SCREAMING_SNAKE_CASE , unet=self.dummy_unet , mel=__SCREAMING_SNAKE_CASE , scheduler=__SCREAMING_SNAKE_CASE ) a_ : List[Any] = pipe.to(__SCREAMING_SNAKE_CASE ) pipe.set_progress_bar_config(disable=__SCREAMING_SNAKE_CASE ) a_ : Optional[int] = torch.Generator(device=__SCREAMING_SNAKE_CASE ).manual_seed(42 ) a_ : List[str] = pipe(generator=__SCREAMING_SNAKE_CASE , steps=4 ) a_ : List[Any] = output.audios[0] a_ : Dict = output.images[0] a_ : Dict = torch.Generator(device=__SCREAMING_SNAKE_CASE ).manual_seed(42 ) a_ : Optional[Any] = pipe(generator=__SCREAMING_SNAKE_CASE , steps=4 , return_dict=__SCREAMING_SNAKE_CASE ) a_ : Union[str, Any] = output[0][0] assert audio.shape == (1, (self.dummy_unet.config.sample_size[1] - 1) * mel.hop_length) assert ( image.height == self.dummy_unet.config.sample_size[0] and image.width == self.dummy_unet.config.sample_size[1] ) a_ : Dict = np.frombuffer(image.tobytes() , dtype='''uint8''' )[:10] a_ : str = np.frombuffer(image_from_tuple.tobytes() , dtype='''uint8''' )[:10] a_ : List[str] = np.array([69, 255, 255, 255, 0, 0, 77, 181, 12, 127] ) assert np.abs(image_slice.flatten() - expected_slice ).max() == 0 assert np.abs(image_from_tuple_slice.flatten() - expected_slice ).max() == 0 a_ : str = Mel( x_res=self.dummy_vqvae_and_unet[0].config.sample_size[1] , y_res=self.dummy_vqvae_and_unet[0].config.sample_size[0] , ) a_ : int = DDIMScheduler() a_ : Dict = self.dummy_vqvae_and_unet a_ : List[str] = AudioDiffusionPipeline( vqvae=self.dummy_vqvae_and_unet[0] , unet=dummy_vqvae_and_unet[1] , mel=__SCREAMING_SNAKE_CASE , scheduler=__SCREAMING_SNAKE_CASE ) a_ : Any = pipe.to(__SCREAMING_SNAKE_CASE ) pipe.set_progress_bar_config(disable=__SCREAMING_SNAKE_CASE ) np.random.seed(0 ) a_ : List[str] = np.random.uniform(-1 , 1 , ((dummy_vqvae_and_unet[0].config.sample_size[1] - 1) * mel.hop_length,) ) a_ : int = torch.Generator(device=__SCREAMING_SNAKE_CASE ).manual_seed(42 ) a_ : int = pipe(raw_audio=__SCREAMING_SNAKE_CASE , generator=__SCREAMING_SNAKE_CASE , start_step=5 , steps=10 ) a_ : List[str] = output.images[0] assert ( image.height == self.dummy_vqvae_and_unet[0].config.sample_size[0] and image.width == self.dummy_vqvae_and_unet[0].config.sample_size[1] ) a_ : Optional[Any] = np.frombuffer(image.tobytes() , dtype='''uint8''' )[:10] a_ : List[str] = np.array([120, 117, 110, 109, 138, 167, 138, 148, 132, 121] ) assert np.abs(image_slice.flatten() - expected_slice ).max() == 0 a_ : List[str] = self.dummy_unet_condition a_ : Dict = AudioDiffusionPipeline( vqvae=self.dummy_vqvae_and_unet[0] , unet=__SCREAMING_SNAKE_CASE , mel=__SCREAMING_SNAKE_CASE , scheduler=__SCREAMING_SNAKE_CASE ) a_ : int = pipe.to(__SCREAMING_SNAKE_CASE ) pipe.set_progress_bar_config(disable=__SCREAMING_SNAKE_CASE ) np.random.seed(0 ) a_ : Any = torch.rand((1, 1, 10) ) a_ : Tuple = pipe(generator=__SCREAMING_SNAKE_CASE , encoding=__SCREAMING_SNAKE_CASE ) a_ : Union[str, Any] = output.images[0] a_ : Dict = np.frombuffer(image.tobytes() , dtype='''uint8''' )[:10] a_ : List[str] = np.array([107, 103, 120, 127, 142, 122, 113, 122, 97, 111] ) assert np.abs(image_slice.flatten() - expected_slice ).max() == 0 @slow @require_torch_gpu class SCREAMING_SNAKE_CASE ( unittest.TestCase ): def SCREAMING_SNAKE_CASE ( self : List[str] ) -> Any: # clean up the VRAM after each test super().tearDown() gc.collect() torch.cuda.empty_cache() def SCREAMING_SNAKE_CASE ( self : Optional[Any] ) -> Any: a_ : Any = torch_device a_ : Optional[int] = DiffusionPipeline.from_pretrained('''teticio/audio-diffusion-ddim-256''' ) a_ : Dict = pipe.to(__SCREAMING_SNAKE_CASE ) pipe.set_progress_bar_config(disable=__SCREAMING_SNAKE_CASE ) a_ : str = torch.Generator(device=__SCREAMING_SNAKE_CASE ).manual_seed(42 ) a_ : List[str] = pipe(generator=__SCREAMING_SNAKE_CASE ) a_ : Union[str, Any] = output.audios[0] a_ : Tuple = output.images[0] assert audio.shape == (1, (pipe.unet.config.sample_size[1] - 1) * pipe.mel.hop_length) assert image.height == pipe.unet.config.sample_size[0] and image.width == pipe.unet.config.sample_size[1] a_ : str = np.frombuffer(image.tobytes() , dtype='''uint8''' )[:10] a_ : Tuple = np.array([151, 167, 154, 144, 122, 134, 121, 105, 70, 26] ) assert np.abs(image_slice.flatten() - expected_slice ).max() == 0
666
0
from typing import TYPE_CHECKING from ....utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available, is_vision_available __lowerCAmelCase = {'configuration_van': ['VAN_PRETRAINED_CONFIG_ARCHIVE_MAP', 'VanConfig']} try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __lowerCAmelCase = [ 'VAN_PRETRAINED_MODEL_ARCHIVE_LIST', 'VanForImageClassification', 'VanModel', 'VanPreTrainedModel', ] if TYPE_CHECKING: from .configuration_van import VAN_PRETRAINED_CONFIG_ARCHIVE_MAP, VanConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_van import ( VAN_PRETRAINED_MODEL_ARCHIVE_LIST, VanForImageClassification, VanModel, VanPreTrainedModel, ) else: import sys __lowerCAmelCase = _LazyModule(__name__, globals()['__file__'], _import_structure)
717
'''simple docstring''' import argparse import json from pathlib import Path import requests import torch from huggingface_hub import cached_download, hf_hub_download, hf_hub_url from PIL import Image from transformers import DetaConfig, DetaForObjectDetection, DetaImageProcessor, SwinConfig from transformers.utils import logging logging.set_verbosity_info() __lowerCAmelCase = logging.get_logger(__name__) def _UpperCAmelCase ( __A : Union[str, Any] ): a_ : Tuple = SwinConfig( embed_dim=1_92 , depths=(2, 2, 18, 2) , num_heads=(6, 12, 24, 48) , window_size=12 , out_features=['''stage2''', '''stage3''', '''stage4'''] , ) a_ : List[Any] = DetaConfig( backbone_config=__A , num_queries=9_00 , encoder_ffn_dim=20_48 , decoder_ffn_dim=20_48 , num_feature_levels=5 , assign_first_stage=__A , with_box_refine=__A , two_stage=__A , ) # set labels a_ : Optional[Any] = '''huggingface/label-files''' if "o365" in model_name: a_ : Optional[Any] = 3_66 a_ : Tuple = '''object365-id2label.json''' else: a_ : Any = 91 a_ : Union[str, Any] = '''coco-detection-id2label.json''' a_ : Tuple = num_labels a_ : str = json.load(open(cached_download(hf_hub_url(__A , __A , repo_type='''dataset''' ) ) , '''r''' ) ) a_ : Optional[int] = {int(__A ): v for k, v in idalabel.items()} a_ : int = idalabel a_ : Dict = {v: k for k, v in idalabel.items()} return config def _UpperCAmelCase ( __A : List[str] ): a_ : Tuple = [] # stem # fmt: off rename_keys.append(('''backbone.0.body.patch_embed.proj.weight''', '''model.backbone.model.embeddings.patch_embeddings.projection.weight''') ) rename_keys.append(('''backbone.0.body.patch_embed.proj.bias''', '''model.backbone.model.embeddings.patch_embeddings.projection.bias''') ) rename_keys.append(('''backbone.0.body.patch_embed.norm.weight''', '''model.backbone.model.embeddings.norm.weight''') ) rename_keys.append(('''backbone.0.body.patch_embed.norm.bias''', '''model.backbone.model.embeddings.norm.bias''') ) # stages for i in range(len(config.backbone_config.depths ) ): for j in range(config.backbone_config.depths[i] ): rename_keys.append((f'backbone.0.body.layers.{i}.blocks.{j}.norm1.weight', f'model.backbone.model.encoder.layers.{i}.blocks.{j}.layernorm_before.weight') ) rename_keys.append((f'backbone.0.body.layers.{i}.blocks.{j}.norm1.bias', f'model.backbone.model.encoder.layers.{i}.blocks.{j}.layernorm_before.bias') ) rename_keys.append((f'backbone.0.body.layers.{i}.blocks.{j}.attn.relative_position_bias_table', f'model.backbone.model.encoder.layers.{i}.blocks.{j}.attention.self.relative_position_bias_table') ) rename_keys.append((f'backbone.0.body.layers.{i}.blocks.{j}.attn.relative_position_index', f'model.backbone.model.encoder.layers.{i}.blocks.{j}.attention.self.relative_position_index') ) rename_keys.append((f'backbone.0.body.layers.{i}.blocks.{j}.attn.proj.weight', f'model.backbone.model.encoder.layers.{i}.blocks.{j}.attention.output.dense.weight') ) rename_keys.append((f'backbone.0.body.layers.{i}.blocks.{j}.attn.proj.bias', f'model.backbone.model.encoder.layers.{i}.blocks.{j}.attention.output.dense.bias') ) rename_keys.append((f'backbone.0.body.layers.{i}.blocks.{j}.norm2.weight', f'model.backbone.model.encoder.layers.{i}.blocks.{j}.layernorm_after.weight') ) rename_keys.append((f'backbone.0.body.layers.{i}.blocks.{j}.norm2.bias', f'model.backbone.model.encoder.layers.{i}.blocks.{j}.layernorm_after.bias') ) rename_keys.append((f'backbone.0.body.layers.{i}.blocks.{j}.mlp.fc1.weight', f'model.backbone.model.encoder.layers.{i}.blocks.{j}.intermediate.dense.weight') ) rename_keys.append((f'backbone.0.body.layers.{i}.blocks.{j}.mlp.fc1.bias', f'model.backbone.model.encoder.layers.{i}.blocks.{j}.intermediate.dense.bias') ) rename_keys.append((f'backbone.0.body.layers.{i}.blocks.{j}.mlp.fc2.weight', f'model.backbone.model.encoder.layers.{i}.blocks.{j}.output.dense.weight') ) rename_keys.append((f'backbone.0.body.layers.{i}.blocks.{j}.mlp.fc2.bias', f'model.backbone.model.encoder.layers.{i}.blocks.{j}.output.dense.bias') ) if i < 3: rename_keys.append((f'backbone.0.body.layers.{i}.downsample.reduction.weight', f'model.backbone.model.encoder.layers.{i}.downsample.reduction.weight') ) rename_keys.append((f'backbone.0.body.layers.{i}.downsample.norm.weight', f'model.backbone.model.encoder.layers.{i}.downsample.norm.weight') ) rename_keys.append((f'backbone.0.body.layers.{i}.downsample.norm.bias', f'model.backbone.model.encoder.layers.{i}.downsample.norm.bias') ) rename_keys.append(('''backbone.0.body.norm1.weight''', '''model.backbone.model.hidden_states_norms.stage2.weight''') ) rename_keys.append(('''backbone.0.body.norm1.bias''', '''model.backbone.model.hidden_states_norms.stage2.bias''') ) rename_keys.append(('''backbone.0.body.norm2.weight''', '''model.backbone.model.hidden_states_norms.stage3.weight''') ) rename_keys.append(('''backbone.0.body.norm2.bias''', '''model.backbone.model.hidden_states_norms.stage3.bias''') ) rename_keys.append(('''backbone.0.body.norm3.weight''', '''model.backbone.model.hidden_states_norms.stage4.weight''') ) rename_keys.append(('''backbone.0.body.norm3.bias''', '''model.backbone.model.hidden_states_norms.stage4.bias''') ) # transformer encoder for i in range(config.encoder_layers ): rename_keys.append((f'transformer.encoder.layers.{i}.self_attn.sampling_offsets.weight', f'model.encoder.layers.{i}.self_attn.sampling_offsets.weight') ) rename_keys.append((f'transformer.encoder.layers.{i}.self_attn.sampling_offsets.bias', f'model.encoder.layers.{i}.self_attn.sampling_offsets.bias') ) rename_keys.append((f'transformer.encoder.layers.{i}.self_attn.attention_weights.weight', f'model.encoder.layers.{i}.self_attn.attention_weights.weight') ) rename_keys.append((f'transformer.encoder.layers.{i}.self_attn.attention_weights.bias', f'model.encoder.layers.{i}.self_attn.attention_weights.bias') ) rename_keys.append((f'transformer.encoder.layers.{i}.self_attn.value_proj.weight', f'model.encoder.layers.{i}.self_attn.value_proj.weight') ) rename_keys.append((f'transformer.encoder.layers.{i}.self_attn.value_proj.bias', f'model.encoder.layers.{i}.self_attn.value_proj.bias') ) rename_keys.append((f'transformer.encoder.layers.{i}.self_attn.output_proj.weight', f'model.encoder.layers.{i}.self_attn.output_proj.weight') ) rename_keys.append((f'transformer.encoder.layers.{i}.self_attn.output_proj.bias', f'model.encoder.layers.{i}.self_attn.output_proj.bias') ) rename_keys.append((f'transformer.encoder.layers.{i}.norm1.weight', f'model.encoder.layers.{i}.self_attn_layer_norm.weight') ) rename_keys.append((f'transformer.encoder.layers.{i}.norm1.bias', f'model.encoder.layers.{i}.self_attn_layer_norm.bias') ) rename_keys.append((f'transformer.encoder.layers.{i}.linear1.weight', f'model.encoder.layers.{i}.fc1.weight') ) rename_keys.append((f'transformer.encoder.layers.{i}.linear1.bias', f'model.encoder.layers.{i}.fc1.bias') ) rename_keys.append((f'transformer.encoder.layers.{i}.linear2.weight', f'model.encoder.layers.{i}.fc2.weight') ) rename_keys.append((f'transformer.encoder.layers.{i}.linear2.bias', f'model.encoder.layers.{i}.fc2.bias') ) rename_keys.append((f'transformer.encoder.layers.{i}.norm2.weight', f'model.encoder.layers.{i}.final_layer_norm.weight') ) rename_keys.append((f'transformer.encoder.layers.{i}.norm2.bias', f'model.encoder.layers.{i}.final_layer_norm.bias') ) # transformer decoder for i in range(config.decoder_layers ): rename_keys.append((f'transformer.decoder.layers.{i}.cross_attn.sampling_offsets.weight', f'model.decoder.layers.{i}.encoder_attn.sampling_offsets.weight') ) rename_keys.append((f'transformer.decoder.layers.{i}.cross_attn.sampling_offsets.bias', f'model.decoder.layers.{i}.encoder_attn.sampling_offsets.bias') ) rename_keys.append((f'transformer.decoder.layers.{i}.cross_attn.attention_weights.weight', f'model.decoder.layers.{i}.encoder_attn.attention_weights.weight') ) rename_keys.append((f'transformer.decoder.layers.{i}.cross_attn.attention_weights.bias', f'model.decoder.layers.{i}.encoder_attn.attention_weights.bias') ) rename_keys.append((f'transformer.decoder.layers.{i}.cross_attn.value_proj.weight', f'model.decoder.layers.{i}.encoder_attn.value_proj.weight') ) rename_keys.append((f'transformer.decoder.layers.{i}.cross_attn.value_proj.bias', f'model.decoder.layers.{i}.encoder_attn.value_proj.bias') ) rename_keys.append((f'transformer.decoder.layers.{i}.cross_attn.output_proj.weight', f'model.decoder.layers.{i}.encoder_attn.output_proj.weight') ) rename_keys.append((f'transformer.decoder.layers.{i}.cross_attn.output_proj.bias', f'model.decoder.layers.{i}.encoder_attn.output_proj.bias') ) rename_keys.append((f'transformer.decoder.layers.{i}.norm1.weight', f'model.decoder.layers.{i}.encoder_attn_layer_norm.weight') ) rename_keys.append((f'transformer.decoder.layers.{i}.norm1.bias', f'model.decoder.layers.{i}.encoder_attn_layer_norm.bias') ) rename_keys.append((f'transformer.decoder.layers.{i}.self_attn.out_proj.weight', f'model.decoder.layers.{i}.self_attn.out_proj.weight') ) rename_keys.append((f'transformer.decoder.layers.{i}.self_attn.out_proj.bias', f'model.decoder.layers.{i}.self_attn.out_proj.bias') ) rename_keys.append((f'transformer.decoder.layers.{i}.norm2.weight', f'model.decoder.layers.{i}.self_attn_layer_norm.weight') ) rename_keys.append((f'transformer.decoder.layers.{i}.norm2.bias', f'model.decoder.layers.{i}.self_attn_layer_norm.bias') ) rename_keys.append((f'transformer.decoder.layers.{i}.linear1.weight', f'model.decoder.layers.{i}.fc1.weight') ) rename_keys.append((f'transformer.decoder.layers.{i}.linear1.bias', f'model.decoder.layers.{i}.fc1.bias') ) rename_keys.append((f'transformer.decoder.layers.{i}.linear2.weight', f'model.decoder.layers.{i}.fc2.weight') ) rename_keys.append((f'transformer.decoder.layers.{i}.linear2.bias', f'model.decoder.layers.{i}.fc2.bias') ) rename_keys.append((f'transformer.decoder.layers.{i}.norm3.weight', f'model.decoder.layers.{i}.final_layer_norm.weight') ) rename_keys.append((f'transformer.decoder.layers.{i}.norm3.bias', f'model.decoder.layers.{i}.final_layer_norm.bias') ) # fmt: on return rename_keys def _UpperCAmelCase ( __A : str , __A : int , __A : Tuple ): a_ : str = dct.pop(__A ) a_ : Dict = val def _UpperCAmelCase ( __A : List[str] , __A : Optional[int] ): a_ : str = [int(backbone_config.embed_dim * 2**i ) for i in range(len(backbone_config.depths ) )] for i in range(len(backbone_config.depths ) ): a_ : Tuple = num_features[i] for j in range(backbone_config.depths[i] ): # fmt: off # read in weights + bias of input projection layer (in original implementation, this is a single matrix + bias) a_ : List[str] = state_dict.pop(f'backbone.0.body.layers.{i}.blocks.{j}.attn.qkv.weight' ) a_ : str = state_dict.pop(f'backbone.0.body.layers.{i}.blocks.{j}.attn.qkv.bias' ) # next, add query, keys and values (in that order) to the state dict a_ : Optional[Any] = in_proj_weight[:dim, :] a_ : List[Any] = in_proj_bias[: dim] a_ : Optional[Any] = in_proj_weight[ dim : dim * 2, : ] a_ : Union[str, Any] = in_proj_bias[ dim : dim * 2 ] a_ : Optional[int] = in_proj_weight[ -dim :, : ] a_ : int = in_proj_bias[-dim :] # fmt: on def _UpperCAmelCase ( __A : Dict , __A : Dict ): # transformer decoder self-attention layers a_ : Any = config.d_model for i in range(config.decoder_layers ): # read in weights + bias of input projection layer of self-attention a_ : int = state_dict.pop(f'transformer.decoder.layers.{i}.self_attn.in_proj_weight' ) a_ : Any = state_dict.pop(f'transformer.decoder.layers.{i}.self_attn.in_proj_bias' ) # next, add query, keys and values (in that order) to the state dict a_ : Dict = in_proj_weight[:hidden_size, :] a_ : Tuple = in_proj_bias[:hidden_size] a_ : Any = in_proj_weight[ hidden_size : hidden_size * 2, : ] a_ : Tuple = in_proj_bias[hidden_size : hidden_size * 2] a_ : Optional[int] = in_proj_weight[-hidden_size:, :] a_ : int = in_proj_bias[-hidden_size:] def _UpperCAmelCase ( ): a_ : Union[str, Any] = '''http://images.cocodataset.org/val2017/000000039769.jpg''' a_ : List[str] = Image.open(requests.get(__A , stream=__A ).raw ) return im @torch.no_grad() def _UpperCAmelCase ( __A : int , __A : int , __A : Any ): a_ : Union[str, Any] = get_deta_config(__A ) # load original state dict if model_name == "deta-swin-large": a_ : Optional[Any] = hf_hub_download(repo_id='''nielsr/deta-checkpoints''' , filename='''adet_swin_ft.pth''' ) elif model_name == "deta-swin-large-o365": a_ : List[str] = hf_hub_download(repo_id='''jozhang97/deta-swin-l-o365''' , filename='''deta_swin_pt_o365.pth''' ) else: raise ValueError(f'Model name {model_name} not supported' ) a_ : List[Any] = torch.load(__A , map_location='''cpu''' )['''model'''] # original state dict for name, param in state_dict.items(): print(__A , param.shape ) # rename keys a_ : Union[str, Any] = create_rename_keys(__A ) for src, dest in rename_keys: rename_key(__A , __A , __A ) read_in_swin_q_k_v(__A , config.backbone_config ) read_in_decoder_q_k_v(__A , __A ) # fix some prefixes for key in state_dict.copy().keys(): if "transformer.decoder.class_embed" in key or "transformer.decoder.bbox_embed" in key: a_ : Optional[Any] = state_dict.pop(__A ) a_ : int = val if "input_proj" in key: a_ : str = state_dict.pop(__A ) a_ : Optional[Any] = val if "level_embed" in key or "pos_trans" in key or "pix_trans" in key or "enc_output" in key: a_ : List[str] = state_dict.pop(__A ) a_ : List[Any] = val # finally, create HuggingFace model and load state dict a_ : Dict = DetaForObjectDetection(__A ) model.load_state_dict(__A ) model.eval() a_ : int = '''cuda''' if torch.cuda.is_available() else '''cpu''' model.to(__A ) # load image processor a_ : List[Any] = DetaImageProcessor(format='''coco_detection''' ) # verify our conversion on image a_ : Dict = prepare_img() a_ : Optional[int] = processor(images=__A , return_tensors='''pt''' ) a_ : Any = encoding['''pixel_values'''] a_ : int = model(pixel_values.to(__A ) ) # verify logits print('''Logits:''' , outputs.logits[0, :3, :3] ) print('''Boxes:''' , outputs.pred_boxes[0, :3, :3] ) if model_name == "deta-swin-large": a_ : Optional[int] = torch.tensor( [[-7.6308, -2.8485, -5.3737], [-7.2037, -4.5505, -4.8027], [-7.2943, -4.2611, -4.6617]] ) a_ : Tuple = torch.tensor([[0.4987, 0.4969, 0.9999], [0.2549, 0.5498, 0.4805], [0.5498, 0.2757, 0.0569]] ) elif model_name == "deta-swin-large-o365": a_ : Union[str, Any] = torch.tensor( [[-8.0122, -3.5720, -4.9717], [-8.1547, -3.6886, -4.6389], [-7.6610, -3.6194, -5.0134]] ) a_ : Any = torch.tensor([[0.2523, 0.5549, 0.4881], [0.7715, 0.4149, 0.4601], [0.5503, 0.2753, 0.0575]] ) assert torch.allclose(outputs.logits[0, :3, :3] , expected_logits.to(__A ) , atol=1E-4 ) assert torch.allclose(outputs.pred_boxes[0, :3, :3] , expected_boxes.to(__A ) , atol=1E-4 ) print('''Everything ok!''' ) if pytorch_dump_folder_path: # Save model and processor logger.info(f'Saving PyTorch model and processor to {pytorch_dump_folder_path}...' ) Path(__A ).mkdir(exist_ok=__A ) model.save_pretrained(__A ) processor.save_pretrained(__A ) # Push to hub if push_to_hub: print('''Pushing model and processor to hub...''' ) model.push_to_hub(f'jozhang97/{model_name}' ) processor.push_to_hub(f'jozhang97/{model_name}' ) if __name__ == "__main__": __lowerCAmelCase = argparse.ArgumentParser() parser.add_argument( '--model_name', type=str, default='deta-swin-large', choices=['deta-swin-large', 'deta-swin-large-o365'], help='Name of the model you\'d like to convert.', ) parser.add_argument( '--pytorch_dump_folder_path', default=None, type=str, help='Path to the folder to output PyTorch model.', ) parser.add_argument( '--push_to_hub', action='store_true', help='Whether or not to push the converted model to the ๐Ÿค— hub.' ) __lowerCAmelCase = parser.parse_args() convert_deta_checkpoint(args.model_name, args.pytorch_dump_folder_path, args.push_to_hub)
666
0
'''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 ( center_crop, convert_to_rgb, get_resize_output_image_size, 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 __lowerCAmelCase = logging.get_logger(__name__) if is_vision_available(): import PIL class SCREAMING_SNAKE_CASE ( SCREAMING_SNAKE_CASE_ ): snake_case__ = ["pixel_values"] def __init__( self : Optional[Any] , __SCREAMING_SNAKE_CASE : bool = True , __SCREAMING_SNAKE_CASE : Dict[str, int] = None , __SCREAMING_SNAKE_CASE : PILImageResampling = PILImageResampling.BICUBIC , __SCREAMING_SNAKE_CASE : bool = True , __SCREAMING_SNAKE_CASE : Dict[str, int] = None , __SCREAMING_SNAKE_CASE : bool = True , __SCREAMING_SNAKE_CASE : Union[int, float] = 1 / 255 , __SCREAMING_SNAKE_CASE : bool = True , __SCREAMING_SNAKE_CASE : Optional[Union[float, List[float]]] = None , __SCREAMING_SNAKE_CASE : Optional[Union[float, List[float]]] = None , __SCREAMING_SNAKE_CASE : bool = True , **__SCREAMING_SNAKE_CASE : List[Any] , ) -> None: super().__init__(**__SCREAMING_SNAKE_CASE ) a_ : str = size if size is not None else {'''shortest_edge''': 224} a_ : Optional[Any] = get_size_dict(__SCREAMING_SNAKE_CASE , default_to_square=__SCREAMING_SNAKE_CASE ) a_ : List[Any] = crop_size if crop_size is not None else {'''height''': 224, '''width''': 224} a_ : Dict = get_size_dict(__SCREAMING_SNAKE_CASE , default_to_square=__SCREAMING_SNAKE_CASE , param_name='''crop_size''' ) a_ : int = do_resize a_ : int = size a_ : Union[str, Any] = resample a_ : Any = do_center_crop a_ : Optional[Any] = crop_size a_ : Any = do_rescale a_ : Optional[Any] = rescale_factor a_ : Dict = do_normalize a_ : int = image_mean if image_mean is not None else OPENAI_CLIP_MEAN a_ : Any = image_std if image_std is not None else OPENAI_CLIP_STD a_ : int = do_convert_rgb def SCREAMING_SNAKE_CASE ( self : Any , __SCREAMING_SNAKE_CASE : np.ndarray , __SCREAMING_SNAKE_CASE : Dict[str, int] , __SCREAMING_SNAKE_CASE : PILImageResampling = PILImageResampling.BICUBIC , __SCREAMING_SNAKE_CASE : Optional[Union[str, ChannelDimension]] = None , **__SCREAMING_SNAKE_CASE : Any , ) -> np.ndarray: a_ : Optional[Any] = get_size_dict(__SCREAMING_SNAKE_CASE , default_to_square=__SCREAMING_SNAKE_CASE ) if "shortest_edge" not in size: raise ValueError(f'The `size` parameter must contain the key `shortest_edge`. Got {size.keys()}' ) a_ : Union[str, Any] = get_resize_output_image_size(__SCREAMING_SNAKE_CASE , size=size['''shortest_edge'''] , default_to_square=__SCREAMING_SNAKE_CASE ) return resize(__SCREAMING_SNAKE_CASE , size=__SCREAMING_SNAKE_CASE , resample=__SCREAMING_SNAKE_CASE , data_format=__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE ( self : List[Any] , __SCREAMING_SNAKE_CASE : np.ndarray , __SCREAMING_SNAKE_CASE : Dict[str, int] , __SCREAMING_SNAKE_CASE : Optional[Union[str, ChannelDimension]] = None , **__SCREAMING_SNAKE_CASE : Any , ) -> np.ndarray: a_ : int = get_size_dict(__SCREAMING_SNAKE_CASE ) if "height" not in size or "width" not in size: raise ValueError(f'The `size` parameter must contain the keys (height, width). Got {size.keys()}' ) return center_crop(__SCREAMING_SNAKE_CASE , size=(size['''height'''], size['''width''']) , data_format=__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE ( self : List[str] , __SCREAMING_SNAKE_CASE : np.ndarray , __SCREAMING_SNAKE_CASE : Union[int, float] , __SCREAMING_SNAKE_CASE : Optional[Union[str, ChannelDimension]] = None , **__SCREAMING_SNAKE_CASE : Tuple , ) -> Tuple: return rescale(__SCREAMING_SNAKE_CASE , scale=__SCREAMING_SNAKE_CASE , data_format=__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE ( self : Optional[Any] , __SCREAMING_SNAKE_CASE : np.ndarray , __SCREAMING_SNAKE_CASE : Union[float, List[float]] , __SCREAMING_SNAKE_CASE : Union[float, List[float]] , __SCREAMING_SNAKE_CASE : Optional[Union[str, ChannelDimension]] = None , **__SCREAMING_SNAKE_CASE : str , ) -> np.ndarray: return normalize(__SCREAMING_SNAKE_CASE , mean=__SCREAMING_SNAKE_CASE , std=__SCREAMING_SNAKE_CASE , data_format=__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE ( self : Any , __SCREAMING_SNAKE_CASE : ImageInput , __SCREAMING_SNAKE_CASE : bool = None , __SCREAMING_SNAKE_CASE : Dict[str, int] = None , __SCREAMING_SNAKE_CASE : PILImageResampling = None , __SCREAMING_SNAKE_CASE : bool = None , __SCREAMING_SNAKE_CASE : int = None , __SCREAMING_SNAKE_CASE : bool = None , __SCREAMING_SNAKE_CASE : float = None , __SCREAMING_SNAKE_CASE : bool = None , __SCREAMING_SNAKE_CASE : Optional[Union[float, List[float]]] = None , __SCREAMING_SNAKE_CASE : Optional[Union[float, List[float]]] = None , __SCREAMING_SNAKE_CASE : bool = None , __SCREAMING_SNAKE_CASE : Optional[Union[str, TensorType]] = None , __SCREAMING_SNAKE_CASE : Optional[ChannelDimension] = ChannelDimension.FIRST , **__SCREAMING_SNAKE_CASE : Any , ) -> PIL.Image.Image: a_ : Tuple = do_resize if do_resize is not None else self.do_resize a_ : str = size if size is not None else self.size a_ : Tuple = get_size_dict(__SCREAMING_SNAKE_CASE , param_name='''size''' , default_to_square=__SCREAMING_SNAKE_CASE ) a_ : Union[str, Any] = resample if resample is not None else self.resample a_ : Optional[int] = do_center_crop if do_center_crop is not None else self.do_center_crop a_ : Tuple = crop_size if crop_size is not None else self.crop_size a_ : Dict = get_size_dict(__SCREAMING_SNAKE_CASE , param_name='''crop_size''' , default_to_square=__SCREAMING_SNAKE_CASE ) a_ : Optional[int] = do_rescale if do_rescale is not None else self.do_rescale a_ : Union[str, Any] = rescale_factor if rescale_factor is not None else self.rescale_factor a_ : str = do_normalize if do_normalize is not None else self.do_normalize a_ : Optional[int] = image_mean if image_mean is not None else self.image_mean a_ : Any = image_std if image_std is not None else self.image_std a_ : Any = do_convert_rgb if do_convert_rgb is not None else self.do_convert_rgb a_ : List[str] = make_list_of_images(__SCREAMING_SNAKE_CASE ) if not valid_images(__SCREAMING_SNAKE_CASE ): raise ValueError( '''Invalid image type. Must be of type PIL.Image.Image, numpy.ndarray, ''' '''torch.Tensor, tf.Tensor or jax.ndarray.''' ) if do_resize and size is None: raise ValueError('''Size must be specified if do_resize is True.''' ) if do_center_crop and crop_size is None: raise ValueError('''Crop size must be specified if do_center_crop is True.''' ) if do_rescale and rescale_factor is None: raise ValueError('''Rescale factor must be specified if do_rescale is True.''' ) if do_normalize and (image_mean is None or image_std is None): raise ValueError('''Image mean and std must be specified if do_normalize is True.''' ) # PIL RGBA images are converted to RGB if do_convert_rgb: a_ : int = [convert_to_rgb(__SCREAMING_SNAKE_CASE ) for image in images] # All transformations expect numpy arrays. a_ : List[str] = [to_numpy_array(__SCREAMING_SNAKE_CASE ) for image in images] if do_resize: a_ : Union[str, Any] = [self.resize(image=__SCREAMING_SNAKE_CASE , size=__SCREAMING_SNAKE_CASE , resample=__SCREAMING_SNAKE_CASE ) for image in images] if do_center_crop: a_ : Dict = [self.center_crop(image=__SCREAMING_SNAKE_CASE , size=__SCREAMING_SNAKE_CASE ) for image in images] if do_rescale: a_ : Any = [self.rescale(image=__SCREAMING_SNAKE_CASE , scale=__SCREAMING_SNAKE_CASE ) for image in images] if do_normalize: a_ : int = [self.normalize(image=__SCREAMING_SNAKE_CASE , mean=__SCREAMING_SNAKE_CASE , std=__SCREAMING_SNAKE_CASE ) for image in images] a_ : Dict = [to_channel_dimension_format(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) for image in images] a_ : Dict = {'''pixel_values''': images} return BatchFeature(data=__SCREAMING_SNAKE_CASE , tensor_type=__SCREAMING_SNAKE_CASE )
718
'''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 SCREAMING_SNAKE_CASE ( SCREAMING_SNAKE_CASE_ , unittest.TestCase ): snake_case__ = DDIMPipeline snake_case__ = UNCONDITIONAL_IMAGE_GENERATION_PARAMS snake_case__ = PipelineTesterMixin.required_optional_params - { "num_images_per_prompt", "latents", "callback", "callback_steps", } snake_case__ = UNCONDITIONAL_IMAGE_GENERATION_BATCH_PARAMS snake_case__ = False def SCREAMING_SNAKE_CASE ( self : Any ) -> Optional[Any]: torch.manual_seed(0 ) a_ : int = 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''') , ) a_ : str = DDIMScheduler() a_ : Union[str, Any] = {'''unet''': unet, '''scheduler''': scheduler} return components def SCREAMING_SNAKE_CASE ( self : Dict , __SCREAMING_SNAKE_CASE : str , __SCREAMING_SNAKE_CASE : Tuple=0 ) -> str: if str(__SCREAMING_SNAKE_CASE ).startswith('''mps''' ): a_ : Dict = torch.manual_seed(__SCREAMING_SNAKE_CASE ) else: a_ : Union[str, Any] = torch.Generator(device=__SCREAMING_SNAKE_CASE ).manual_seed(__SCREAMING_SNAKE_CASE ) a_ : Union[str, Any] = { '''batch_size''': 1, '''generator''': generator, '''num_inference_steps''': 2, '''output_type''': '''numpy''', } return inputs def SCREAMING_SNAKE_CASE ( self : Union[str, Any] ) -> Union[str, Any]: a_ : Dict = '''cpu''' a_ : List[Any] = self.get_dummy_components() a_ : List[str] = self.pipeline_class(**__SCREAMING_SNAKE_CASE ) pipe.to(__SCREAMING_SNAKE_CASE ) pipe.set_progress_bar_config(disable=__SCREAMING_SNAKE_CASE ) a_ : Tuple = self.get_dummy_inputs(__SCREAMING_SNAKE_CASE ) a_ : Optional[Any] = pipe(**__SCREAMING_SNAKE_CASE ).images a_ : List[str] = image[0, -3:, -3:, -1] self.assertEqual(image.shape , (1, 32, 32, 3) ) a_ : int = np.array( [1.000e00, 5.717e-01, 4.717e-01, 1.000e00, 0.000e00, 1.000e00, 3.000e-04, 0.000e00, 9.000e-04] ) a_ : Union[str, Any] = np.abs(image_slice.flatten() - expected_slice ).max() self.assertLessEqual(__SCREAMING_SNAKE_CASE , 1e-3 ) def SCREAMING_SNAKE_CASE ( self : List[str] ) -> str: super().test_dict_tuple_outputs_equivalent(expected_max_difference=3e-3 ) def SCREAMING_SNAKE_CASE ( self : str ) -> Optional[int]: super().test_save_load_local(expected_max_difference=3e-3 ) def SCREAMING_SNAKE_CASE ( self : Any ) -> List[str]: super().test_save_load_optional_components(expected_max_difference=3e-3 ) def SCREAMING_SNAKE_CASE ( self : str ) -> Optional[int]: super().test_inference_batch_single_identical(expected_max_diff=3e-3 ) @slow @require_torch_gpu class SCREAMING_SNAKE_CASE ( unittest.TestCase ): def SCREAMING_SNAKE_CASE ( self : str ) -> Any: a_ : Optional[Any] = '''google/ddpm-cifar10-32''' a_ : Optional[Any] = UNetaDModel.from_pretrained(__SCREAMING_SNAKE_CASE ) a_ : Dict = DDIMScheduler() a_ : List[str] = DDIMPipeline(unet=__SCREAMING_SNAKE_CASE , scheduler=__SCREAMING_SNAKE_CASE ) ddim.to(__SCREAMING_SNAKE_CASE ) ddim.set_progress_bar_config(disable=__SCREAMING_SNAKE_CASE ) a_ : Tuple = torch.manual_seed(0 ) a_ : Tuple = ddim(generator=__SCREAMING_SNAKE_CASE , eta=0.0 , output_type='''numpy''' ).images a_ : List[Any] = image[0, -3:, -3:, -1] assert image.shape == (1, 32, 32, 3) a_ : List[str] = np.array([0.1723, 0.1617, 0.1600, 0.1626, 0.1497, 0.1513, 0.1505, 0.1442, 0.1453] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-2 def SCREAMING_SNAKE_CASE ( self : Union[str, Any] ) -> str: a_ : int = '''google/ddpm-ema-bedroom-256''' a_ : str = UNetaDModel.from_pretrained(__SCREAMING_SNAKE_CASE ) a_ : Tuple = DDIMScheduler.from_pretrained(__SCREAMING_SNAKE_CASE ) a_ : Any = DDIMPipeline(unet=__SCREAMING_SNAKE_CASE , scheduler=__SCREAMING_SNAKE_CASE ) ddpm.to(__SCREAMING_SNAKE_CASE ) ddpm.set_progress_bar_config(disable=__SCREAMING_SNAKE_CASE ) a_ : Tuple = torch.manual_seed(0 ) a_ : List[Any] = ddpm(generator=__SCREAMING_SNAKE_CASE , output_type='''numpy''' ).images a_ : Optional[int] = image[0, -3:, -3:, -1] assert image.shape == (1, 256, 256, 3) a_ : Optional[Any] = np.array([0.0060, 0.0201, 0.0344, 0.0024, 0.0018, 0.0002, 0.0022, 0.0000, 0.0069] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-2
666
0
'''simple docstring''' import random import unittest import torch from diffusers import IFInpaintingSuperResolutionPipeline from diffusers.utils import floats_tensor from diffusers.utils.import_utils import is_xformers_available from diffusers.utils.testing_utils import skip_mps, torch_device from ..pipeline_params import ( TEXT_GUIDED_IMAGE_INPAINTING_BATCH_PARAMS, TEXT_GUIDED_IMAGE_INPAINTING_PARAMS, ) from ..test_pipelines_common import PipelineTesterMixin from . import IFPipelineTesterMixin @skip_mps class SCREAMING_SNAKE_CASE ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , unittest.TestCase ): snake_case__ = IFInpaintingSuperResolutionPipeline snake_case__ = TEXT_GUIDED_IMAGE_INPAINTING_PARAMS - {"width", "height"} snake_case__ = TEXT_GUIDED_IMAGE_INPAINTING_BATCH_PARAMS.union({"original_image"} ) snake_case__ = PipelineTesterMixin.required_optional_params - {"latents"} def SCREAMING_SNAKE_CASE ( self : str ) -> List[str]: return self._get_superresolution_dummy_components() def SCREAMING_SNAKE_CASE ( self : Union[str, Any] , __SCREAMING_SNAKE_CASE : Optional[Any] , __SCREAMING_SNAKE_CASE : Dict=0 ) -> List[Any]: if str(__SCREAMING_SNAKE_CASE ).startswith('''mps''' ): a_ : Optional[int] = torch.manual_seed(__SCREAMING_SNAKE_CASE ) else: a_ : str = torch.Generator(device=__SCREAMING_SNAKE_CASE ).manual_seed(__SCREAMING_SNAKE_CASE ) a_ : Dict = floats_tensor((1, 3, 16, 16) , rng=random.Random(__SCREAMING_SNAKE_CASE ) ).to(__SCREAMING_SNAKE_CASE ) a_ : Union[str, Any] = floats_tensor((1, 3, 32, 32) , rng=random.Random(__SCREAMING_SNAKE_CASE ) ).to(__SCREAMING_SNAKE_CASE ) a_ : Tuple = floats_tensor((1, 3, 32, 32) , rng=random.Random(__SCREAMING_SNAKE_CASE ) ).to(__SCREAMING_SNAKE_CASE ) a_ : Union[str, Any] = { '''prompt''': '''A painting of a squirrel eating a burger''', '''image''': image, '''original_image''': original_image, '''mask_image''': mask_image, '''generator''': generator, '''num_inference_steps''': 2, '''output_type''': '''numpy''', } return inputs @unittest.skipIf( torch_device != '''cuda''' or not is_xformers_available() , reason='''XFormers attention is only available with CUDA and `xformers` installed''' , ) def SCREAMING_SNAKE_CASE ( self : Optional[Any] ) -> List[str]: self._test_xformers_attention_forwardGenerator_pass(expected_max_diff=1e-3 ) def SCREAMING_SNAKE_CASE ( self : int ) -> int: self._test_save_load_optional_components() @unittest.skipIf(torch_device != '''cuda''' , reason='''float16 requires CUDA''' ) def SCREAMING_SNAKE_CASE ( self : str ) -> Tuple: # Due to non-determinism in save load of the hf-internal-testing/tiny-random-t5 text encoder super().test_save_load_floataa(expected_max_diff=1e-1 ) def SCREAMING_SNAKE_CASE ( self : List[str] ) -> Optional[int]: self._test_attention_slicing_forward_pass(expected_max_diff=1e-2 ) def SCREAMING_SNAKE_CASE ( self : Union[str, Any] ) -> Optional[Any]: self._test_save_load_local() def SCREAMING_SNAKE_CASE ( self : Dict ) -> Optional[Any]: self._test_inference_batch_single_identical( expected_max_diff=1e-2 , )
719
'''simple docstring''' from dataclasses import dataclass, field from typing import TYPE_CHECKING, Any, ClassVar, Dict, List, Optional, Union import pyarrow as pa if TYPE_CHECKING: from .features import FeatureType @dataclass class SCREAMING_SNAKE_CASE : snake_case__ = 42 snake_case__ = None # Automatically constructed snake_case__ = "dict" snake_case__ = None snake_case__ = field(default="Translation" , init=SCREAMING_SNAKE_CASE_ , repr=SCREAMING_SNAKE_CASE_ ) def __call__( self : Dict ) -> Tuple: return pa.struct({lang: pa.string() for lang in sorted(self.languages )} ) def SCREAMING_SNAKE_CASE ( self : Union[str, Any] ) -> Union["FeatureType", Dict[str, "FeatureType"]]: from .features import Value return {k: Value('''string''' ) for k in sorted(self.languages )} @dataclass class SCREAMING_SNAKE_CASE : snake_case__ = None snake_case__ = None snake_case__ = None # Automatically constructed snake_case__ = "dict" snake_case__ = None snake_case__ = field(default="TranslationVariableLanguages" , init=SCREAMING_SNAKE_CASE_ , repr=SCREAMING_SNAKE_CASE_ ) def SCREAMING_SNAKE_CASE ( self : Tuple ) -> Dict: a_ : List[str] = sorted(set(self.languages ) ) if self.languages else None a_ : Optional[Any] = len(self.languages ) if self.languages else None def __call__( self : Any ) -> Optional[Any]: return pa.struct({'''language''': pa.list_(pa.string() ), '''translation''': pa.list_(pa.string() )} ) def SCREAMING_SNAKE_CASE ( self : Optional[int] , __SCREAMING_SNAKE_CASE : Optional[Any] ) -> Optional[Any]: a_ : str = set(self.languages ) if self.languages and set(__SCREAMING_SNAKE_CASE ) - lang_set: raise ValueError( f'Some languages in example ({", ".join(sorted(set(__SCREAMING_SNAKE_CASE ) - lang_set ) )}) are not in valid set ({", ".join(__SCREAMING_SNAKE_CASE )}).' ) # Convert dictionary into tuples, splitting out cases where there are # multiple translations for a single language. a_ : int = [] for lang, text in translation_dict.items(): if isinstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): translation_tuples.append((lang, text) ) else: translation_tuples.extend([(lang, el) for el in text] ) # Ensure translations are in ascending order by language code. a_ , a_ : List[Any] = zip(*sorted(__SCREAMING_SNAKE_CASE ) ) return {"language": languages, "translation": translations} def SCREAMING_SNAKE_CASE ( self : Any ) -> Union["FeatureType", Dict[str, "FeatureType"]]: from .features import Sequence, Value return { "language": Sequence(Value('''string''' ) ), "translation": Sequence(Value('''string''' ) ), }
666
0
'''simple docstring''' __lowerCAmelCase = {str(digit): digit**5 for digit in range(10)} def _UpperCAmelCase ( __A : int ): return sum(DIGITS_FIFTH_POWER[digit] for digit in str(__A ) ) def _UpperCAmelCase ( ): return sum( number for number in range(10_00 , 1_00_00_00 ) if number == digits_fifth_powers_sum(__A ) ) if __name__ == "__main__": print(solution())
720
'''simple docstring''' import json import os import pickle import shutil import tempfile from unittest import TestCase from unittest.mock import patch import numpy as np from datasets import Dataset from transformers import is_faiss_available from transformers.models.bart.configuration_bart import BartConfig from transformers.models.bart.tokenization_bart import BartTokenizer from transformers.models.bert.tokenization_bert import VOCAB_FILES_NAMES as DPR_VOCAB_FILES_NAMES from transformers.models.dpr.configuration_dpr import DPRConfig from transformers.models.dpr.tokenization_dpr import DPRContextEncoderTokenizer, DPRQuestionEncoderTokenizer from transformers.models.rag.configuration_rag import RagConfig from transformers.models.rag.retrieval_rag import CustomHFIndex, RagRetriever from transformers.models.roberta.tokenization_roberta import VOCAB_FILES_NAMES as BART_VOCAB_FILES_NAMES from transformers.testing_utils import require_faiss, require_sentencepiece, require_tokenizers, require_torch if is_faiss_available(): import faiss @require_faiss class SCREAMING_SNAKE_CASE ( SCREAMING_SNAKE_CASE_ ): def SCREAMING_SNAKE_CASE ( self : Dict ) -> Dict: a_ : Union[str, Any] = tempfile.mkdtemp() a_ : Union[str, Any] = 8 # DPR tok a_ : Dict = [ '''[UNK]''', '''[CLS]''', '''[SEP]''', '''[PAD]''', '''[MASK]''', '''want''', '''##want''', '''##ed''', '''wa''', '''un''', '''runn''', '''##ing''', ''',''', '''low''', '''lowest''', ] a_ : str = os.path.join(self.tmpdirname , '''dpr_tokenizer''' ) os.makedirs(__SCREAMING_SNAKE_CASE , exist_ok=__SCREAMING_SNAKE_CASE ) a_ : Optional[int] = os.path.join(__SCREAMING_SNAKE_CASE , DPR_VOCAB_FILES_NAMES['''vocab_file'''] ) with open(self.vocab_file , '''w''' , encoding='''utf-8''' ) as vocab_writer: vocab_writer.write(''''''.join([x + '''\n''' for x in vocab_tokens] ) ) # BART tok a_ : Union[str, Any] = [ '''l''', '''o''', '''w''', '''e''', '''r''', '''s''', '''t''', '''i''', '''d''', '''n''', '''\u0120''', '''\u0120l''', '''\u0120n''', '''\u0120lo''', '''\u0120low''', '''er''', '''\u0120lowest''', '''\u0120newer''', '''\u0120wider''', '''<unk>''', ] a_ : int = dict(zip(__SCREAMING_SNAKE_CASE , range(len(__SCREAMING_SNAKE_CASE ) ) ) ) a_ : int = ['''#version: 0.2''', '''\u0120 l''', '''\u0120l o''', '''\u0120lo w''', '''e r''', ''''''] a_ : Optional[int] = {'''unk_token''': '''<unk>'''} a_ : List[str] = os.path.join(self.tmpdirname , '''bart_tokenizer''' ) os.makedirs(__SCREAMING_SNAKE_CASE , exist_ok=__SCREAMING_SNAKE_CASE ) a_ : Tuple = os.path.join(__SCREAMING_SNAKE_CASE , BART_VOCAB_FILES_NAMES['''vocab_file'''] ) a_ : int = os.path.join(__SCREAMING_SNAKE_CASE , BART_VOCAB_FILES_NAMES['''merges_file'''] ) with open(self.vocab_file , '''w''' , encoding='''utf-8''' ) as fp: fp.write(json.dumps(__SCREAMING_SNAKE_CASE ) + '''\n''' ) with open(self.merges_file , '''w''' , encoding='''utf-8''' ) as fp: fp.write('''\n'''.join(__SCREAMING_SNAKE_CASE ) ) def SCREAMING_SNAKE_CASE ( self : Tuple ) -> DPRQuestionEncoderTokenizer: return DPRQuestionEncoderTokenizer.from_pretrained(os.path.join(self.tmpdirname , '''dpr_tokenizer''' ) ) def SCREAMING_SNAKE_CASE ( self : str ) -> DPRContextEncoderTokenizer: return DPRContextEncoderTokenizer.from_pretrained(os.path.join(self.tmpdirname , '''dpr_tokenizer''' ) ) def SCREAMING_SNAKE_CASE ( self : Optional[int] ) -> BartTokenizer: return BartTokenizer.from_pretrained(os.path.join(self.tmpdirname , '''bart_tokenizer''' ) ) def SCREAMING_SNAKE_CASE ( self : List[Any] ) -> Union[str, Any]: shutil.rmtree(self.tmpdirname ) def SCREAMING_SNAKE_CASE ( self : Dict ) -> Union[str, Any]: a_ : str = Dataset.from_dict( { '''id''': ['''0''', '''1'''], '''text''': ['''foo''', '''bar'''], '''title''': ['''Foo''', '''Bar'''], '''embeddings''': [np.ones(self.retrieval_vector_size ), 2 * np.ones(self.retrieval_vector_size )], } ) dataset.add_faiss_index('''embeddings''' , string_factory='''Flat''' , metric_type=faiss.METRIC_INNER_PRODUCT ) return dataset def SCREAMING_SNAKE_CASE ( self : List[str] ) -> Tuple: a_ : List[str] = self.get_dummy_dataset() a_ : Tuple = RagConfig( retrieval_vector_size=self.retrieval_vector_size , question_encoder=DPRConfig().to_dict() , generator=BartConfig().to_dict() , ) with patch('''transformers.models.rag.retrieval_rag.load_dataset''' ) as mock_load_dataset: a_ : Tuple = dataset a_ : Any = RagRetriever( __SCREAMING_SNAKE_CASE , question_encoder_tokenizer=self.get_dpr_tokenizer() , generator_tokenizer=self.get_bart_tokenizer() , ) return retriever def SCREAMING_SNAKE_CASE ( self : Dict , __SCREAMING_SNAKE_CASE : bool ) -> Dict: a_ : Dict = self.get_dummy_dataset() a_ : Dict = RagConfig( retrieval_vector_size=self.retrieval_vector_size , question_encoder=DPRConfig().to_dict() , generator=BartConfig().to_dict() , index_name='''custom''' , ) if from_disk: a_ : Optional[int] = os.path.join(self.tmpdirname , '''dataset''' ) a_ : str = os.path.join(self.tmpdirname , '''index.faiss''' ) dataset.get_index('''embeddings''' ).save(os.path.join(self.tmpdirname , '''index.faiss''' ) ) dataset.drop_index('''embeddings''' ) dataset.save_to_disk(os.path.join(self.tmpdirname , '''dataset''' ) ) del dataset a_ : int = RagRetriever( __SCREAMING_SNAKE_CASE , question_encoder_tokenizer=self.get_dpr_tokenizer() , generator_tokenizer=self.get_bart_tokenizer() , ) else: a_ : Optional[Any] = RagRetriever( __SCREAMING_SNAKE_CASE , question_encoder_tokenizer=self.get_dpr_tokenizer() , generator_tokenizer=self.get_bart_tokenizer() , index=CustomHFIndex(config.retrieval_vector_size , __SCREAMING_SNAKE_CASE ) , ) return retriever def SCREAMING_SNAKE_CASE ( self : Optional[Any] ) -> Optional[int]: a_ : str = Dataset.from_dict( { '''id''': ['''0''', '''1'''], '''text''': ['''foo''', '''bar'''], '''title''': ['''Foo''', '''Bar'''], '''embeddings''': [np.ones(self.retrieval_vector_size + 1 ), 2 * np.ones(self.retrieval_vector_size + 1 )], } ) dataset.add_faiss_index('''embeddings''' , string_factory='''Flat''' , metric_type=faiss.METRIC_INNER_PRODUCT ) a_ : Optional[int] = os.path.join(self.tmpdirname , '''hf_bert_base.hnswSQ8_correct_phi_128.c_index''' ) dataset.save_faiss_index('''embeddings''' , index_file_name + '''.index.dpr''' ) pickle.dump(dataset['''id'''] , open(index_file_name + '''.index_meta.dpr''' , '''wb''' ) ) a_ : Union[str, Any] = os.path.join(self.tmpdirname , '''psgs_w100.tsv.pkl''' ) a_ : Dict = {sample['''id''']: [sample['''text'''], sample['''title''']] for sample in dataset} pickle.dump(__SCREAMING_SNAKE_CASE , open(__SCREAMING_SNAKE_CASE , '''wb''' ) ) a_ : Optional[Any] = RagConfig( retrieval_vector_size=self.retrieval_vector_size , question_encoder=DPRConfig().to_dict() , generator=BartConfig().to_dict() , index_name='''legacy''' , index_path=self.tmpdirname , ) a_ : int = RagRetriever( __SCREAMING_SNAKE_CASE , question_encoder_tokenizer=self.get_dpr_tokenizer() , generator_tokenizer=self.get_bart_tokenizer() ) return retriever def SCREAMING_SNAKE_CASE ( self : Tuple ) -> Dict: a_ : Optional[Any] = 1 a_ : Dict = self.get_dummy_canonical_hf_index_retriever() a_ : Tuple = np.array( [np.ones(self.retrieval_vector_size ), -np.ones(self.retrieval_vector_size )] , dtype=np.floataa ) a_ , a_ , a_ : str = retriever.retrieve(__SCREAMING_SNAKE_CASE , n_docs=__SCREAMING_SNAKE_CASE ) self.assertEqual(retrieved_doc_embeds.shape , (2, n_docs, self.retrieval_vector_size) ) self.assertEqual(len(__SCREAMING_SNAKE_CASE ) , 2 ) self.assertEqual(sorted(doc_dicts[0] ) , ['''embeddings''', '''id''', '''text''', '''title'''] ) self.assertEqual(len(doc_dicts[0]['''id'''] ) , __SCREAMING_SNAKE_CASE ) self.assertEqual(doc_dicts[0]['''id'''][0] , '''1''' ) # max inner product is reached with second doc self.assertEqual(doc_dicts[1]['''id'''][0] , '''0''' ) # max inner product is reached with first doc self.assertListEqual(doc_ids.tolist() , [[1], [0]] ) def SCREAMING_SNAKE_CASE ( self : Tuple ) -> Dict: a_ : str = self.get_dummy_canonical_hf_index_retriever() with tempfile.TemporaryDirectory() as tmp_dirname: with patch('''transformers.models.rag.retrieval_rag.load_dataset''' ) as mock_load_dataset: a_ : List[str] = self.get_dummy_dataset() retriever.save_pretrained(__SCREAMING_SNAKE_CASE ) a_ : Optional[Any] = RagRetriever.from_pretrained(__SCREAMING_SNAKE_CASE ) self.assertIsInstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) a_ : Union[str, Any] = np.array( [np.ones(self.retrieval_vector_size ), -np.ones(self.retrieval_vector_size )] , dtype=np.floataa ) a_ : List[str] = retriever.retrieve(__SCREAMING_SNAKE_CASE , n_docs=1 ) self.assertTrue(out is not None ) def SCREAMING_SNAKE_CASE ( self : Dict ) -> int: a_ : Union[str, Any] = 1 a_ : Optional[Any] = self.get_dummy_custom_hf_index_retriever(from_disk=__SCREAMING_SNAKE_CASE ) a_ : List[Any] = np.array( [np.ones(self.retrieval_vector_size ), -np.ones(self.retrieval_vector_size )] , dtype=np.floataa ) a_ , a_ , a_ : Any = retriever.retrieve(__SCREAMING_SNAKE_CASE , n_docs=__SCREAMING_SNAKE_CASE ) self.assertEqual(retrieved_doc_embeds.shape , (2, n_docs, self.retrieval_vector_size) ) self.assertEqual(len(__SCREAMING_SNAKE_CASE ) , 2 ) self.assertEqual(sorted(doc_dicts[0] ) , ['''embeddings''', '''id''', '''text''', '''title'''] ) self.assertEqual(len(doc_dicts[0]['''id'''] ) , __SCREAMING_SNAKE_CASE ) self.assertEqual(doc_dicts[0]['''id'''][0] , '''1''' ) # max inner product is reached with second doc self.assertEqual(doc_dicts[1]['''id'''][0] , '''0''' ) # max inner product is reached with first doc self.assertListEqual(doc_ids.tolist() , [[1], [0]] ) def SCREAMING_SNAKE_CASE ( self : str ) -> List[str]: a_ : Dict = self.get_dummy_custom_hf_index_retriever(from_disk=__SCREAMING_SNAKE_CASE ) with tempfile.TemporaryDirectory() as tmp_dirname: retriever.save_pretrained(__SCREAMING_SNAKE_CASE ) a_ : List[str] = RagRetriever.from_pretrained(__SCREAMING_SNAKE_CASE ) self.assertIsInstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) a_ : Tuple = np.array( [np.ones(self.retrieval_vector_size ), -np.ones(self.retrieval_vector_size )] , dtype=np.floataa ) a_ : Dict = retriever.retrieve(__SCREAMING_SNAKE_CASE , n_docs=1 ) self.assertTrue(out is not None ) def SCREAMING_SNAKE_CASE ( self : Tuple ) -> Optional[int]: a_ : Union[str, Any] = 1 a_ : str = self.get_dummy_custom_hf_index_retriever(from_disk=__SCREAMING_SNAKE_CASE ) a_ : Optional[int] = np.array( [np.ones(self.retrieval_vector_size ), -np.ones(self.retrieval_vector_size )] , dtype=np.floataa ) a_ , a_ , a_ : Tuple = retriever.retrieve(__SCREAMING_SNAKE_CASE , n_docs=__SCREAMING_SNAKE_CASE ) self.assertEqual(retrieved_doc_embeds.shape , (2, n_docs, self.retrieval_vector_size) ) self.assertEqual(len(__SCREAMING_SNAKE_CASE ) , 2 ) self.assertEqual(sorted(doc_dicts[0] ) , ['''embeddings''', '''id''', '''text''', '''title'''] ) self.assertEqual(len(doc_dicts[0]['''id'''] ) , __SCREAMING_SNAKE_CASE ) self.assertEqual(doc_dicts[0]['''id'''][0] , '''1''' ) # max inner product is reached with second doc self.assertEqual(doc_dicts[1]['''id'''][0] , '''0''' ) # max inner product is reached with first doc self.assertListEqual(doc_ids.tolist() , [[1], [0]] ) def SCREAMING_SNAKE_CASE ( self : int ) -> Dict: a_ : List[str] = self.get_dummy_custom_hf_index_retriever(from_disk=__SCREAMING_SNAKE_CASE ) with tempfile.TemporaryDirectory() as tmp_dirname: retriever.save_pretrained(__SCREAMING_SNAKE_CASE ) a_ : Any = RagRetriever.from_pretrained(__SCREAMING_SNAKE_CASE ) self.assertIsInstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) a_ : Tuple = np.array( [np.ones(self.retrieval_vector_size ), -np.ones(self.retrieval_vector_size )] , dtype=np.floataa ) a_ : Dict = retriever.retrieve(__SCREAMING_SNAKE_CASE , n_docs=1 ) self.assertTrue(out is not None ) def SCREAMING_SNAKE_CASE ( self : Optional[int] ) -> Any: a_ : str = 1 a_ : Tuple = self.get_dummy_legacy_index_retriever() a_ : Union[str, Any] = np.array( [np.ones(self.retrieval_vector_size ), -np.ones(self.retrieval_vector_size )] , dtype=np.floataa ) a_ , a_ , a_ : Any = retriever.retrieve(__SCREAMING_SNAKE_CASE , n_docs=__SCREAMING_SNAKE_CASE ) self.assertEqual(retrieved_doc_embeds.shape , (2, n_docs, self.retrieval_vector_size) ) self.assertEqual(len(__SCREAMING_SNAKE_CASE ) , 2 ) self.assertEqual(sorted(doc_dicts[0] ) , ['''text''', '''title'''] ) self.assertEqual(len(doc_dicts[0]['''text'''] ) , __SCREAMING_SNAKE_CASE ) self.assertEqual(doc_dicts[0]['''text'''][0] , '''bar''' ) # max inner product is reached with second doc self.assertEqual(doc_dicts[1]['''text'''][0] , '''foo''' ) # max inner product is reached with first doc self.assertListEqual(doc_ids.tolist() , [[1], [0]] ) def SCREAMING_SNAKE_CASE ( self : int ) -> Optional[int]: a_ : List[str] = self.get_dummy_legacy_index_retriever() with tempfile.TemporaryDirectory() as tmp_dirname: retriever.save_pretrained(__SCREAMING_SNAKE_CASE ) a_ : Any = RagRetriever.from_pretrained(__SCREAMING_SNAKE_CASE ) self.assertIsInstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) a_ : List[str] = np.array( [np.ones(self.retrieval_vector_size ), -np.ones(self.retrieval_vector_size )] , dtype=np.floataa ) a_ : Optional[Any] = retriever.retrieve(__SCREAMING_SNAKE_CASE , n_docs=1 ) self.assertTrue(out is not None ) @require_torch @require_tokenizers @require_sentencepiece def SCREAMING_SNAKE_CASE ( self : int ) -> List[str]: import torch a_ : Any = 1 a_ : List[Any] = self.get_dummy_canonical_hf_index_retriever() a_ : Union[str, Any] = [[5, 7], [10, 11]] a_ : Optional[int] = np.array( [np.ones(self.retrieval_vector_size ), -np.ones(self.retrieval_vector_size )] , dtype=np.floataa ) a_ : str = retriever(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , prefix=retriever.config.generator.prefix , n_docs=__SCREAMING_SNAKE_CASE ) a_ , a_ , a_ : List[str] = ( out['''context_input_ids'''], out['''context_attention_mask'''], out['''retrieved_doc_embeds'''], ) self.assertEqual(retrieved_doc_embeds.shape , (2, n_docs, self.retrieval_vector_size) ) self.assertIsInstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) self.assertIsInstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) self.assertIsInstance(__SCREAMING_SNAKE_CASE , np.ndarray ) a_ : Any = retriever( __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , prefix=retriever.config.generator.prefix , n_docs=__SCREAMING_SNAKE_CASE , return_tensors='''pt''' , ) a_ , a_ , a_ , a_ : str = ( # noqa: F841 out['''context_input_ids'''], out['''context_attention_mask'''], out['''retrieved_doc_embeds'''], out['''doc_ids'''], ) self.assertEqual(retrieved_doc_embeds.shape , (2, n_docs, self.retrieval_vector_size) ) self.assertIsInstance(__SCREAMING_SNAKE_CASE , torch.Tensor ) self.assertIsInstance(__SCREAMING_SNAKE_CASE , torch.Tensor ) self.assertIsInstance(__SCREAMING_SNAKE_CASE , torch.Tensor ) @require_torch @require_tokenizers @require_sentencepiece def SCREAMING_SNAKE_CASE ( self : Union[str, Any] ) -> Any: a_ : str = self.get_dpr_ctx_encoder_tokenizer() a_ : Tuple = 1 a_ : Any = self.get_dummy_custom_hf_index_retriever(from_disk=__SCREAMING_SNAKE_CASE ) retriever.set_ctx_encoder_tokenizer(__SCREAMING_SNAKE_CASE ) a_ : Dict = [[5, 7], [10, 11]] a_ : List[str] = np.array( [np.ones(self.retrieval_vector_size ), -np.ones(self.retrieval_vector_size )] , dtype=np.floataa ) a_ : List[Any] = retriever(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , prefix=retriever.config.generator.prefix , n_docs=__SCREAMING_SNAKE_CASE ) self.assertEqual( len(__SCREAMING_SNAKE_CASE ) , 6 ) # check whether the retriever output consist of 6 attributes including tokenized docs self.assertEqual( all(k in out for k in ('''tokenized_doc_ids''', '''tokenized_doc_attention_mask''') ) , __SCREAMING_SNAKE_CASE ) # check for doc token related keys in dictionary.
666
0
'''simple docstring''' import copy from ...configuration_utils import PretrainedConfig from ...utils import add_start_docstrings __lowerCAmelCase = r'\n [`RagConfig`] stores the configuration of a *RagModel*. Configuration objects inherit from [`PretrainedConfig`] and\n can be used to control the model outputs. Read the documentation from [`PretrainedConfig`] for more information.\n\n Args:\n title_sep (`str`, *optional*, defaults to `" / "`):\n Separator inserted between the title and the text of the retrieved document when calling [`RagRetriever`].\n doc_sep (`str`, *optional*, defaults to `" // "`):\n Separator inserted between the text of the retrieved document and the original input when calling\n [`RagRetriever`].\n n_docs (`int`, *optional*, defaults to 5):\n Number of documents to retrieve.\n max_combined_length (`int`, *optional*, defaults to 300):\n Max length of contextualized input returned by [`~RagRetriever.__call__`].\n retrieval_vector_size (`int`, *optional*, defaults to 768):\n Dimensionality of the document embeddings indexed by [`RagRetriever`].\n retrieval_batch_size (`int`, *optional*, defaults to 8):\n Retrieval batch size, defined as the number of queries issues concurrently to the faiss index encapsulated\n [`RagRetriever`].\n dataset (`str`, *optional*, defaults to `"wiki_dpr"`):\n A dataset identifier of the indexed dataset in HuggingFace Datasets (list all available datasets and ids\n using `datasets.list_datasets()`).\n dataset_split (`str`, *optional*, defaults to `"train"`)\n Which split of the `dataset` to load.\n index_name (`str`, *optional*, defaults to `"compressed"`)\n The index name of the index associated with the `dataset`. One can choose between `"legacy"`, `"exact"` and\n `"compressed"`.\n index_path (`str`, *optional*)\n The path to the serialized faiss index on disk.\n passages_path (`str`, *optional*):\n A path to text passages compatible with the faiss index. Required if using\n [`~models.rag.retrieval_rag.LegacyIndex`]\n use_dummy_dataset (`bool`, *optional*, defaults to `False`)\n Whether to load a "dummy" variant of the dataset specified by `dataset`.\n label_smoothing (`float`, *optional*, defaults to 0.0):\n Only relevant if `return_loss` is set to `True`. Controls the `epsilon` parameter value for label smoothing\n in the loss calculation. If set to 0, no label smoothing is performed.\n do_marginalize (`bool`, *optional*, defaults to `False`):\n If `True`, the logits are marginalized over all documents by making use of\n `torch.nn.functional.log_softmax`.\n reduce_loss (`bool`, *optional*, defaults to `False`):\n Whether or not to reduce the NLL loss using the `torch.Tensor.sum` operation.\n do_deduplication (`bool`, *optional*, defaults to `True`):\n Whether or not to deduplicate the generations from different context documents for a given input. Has to be\n set to `False` if used while training with distributed backend.\n exclude_bos_score (`bool`, *optional*, defaults to `False`):\n Whether or not to disregard the BOS token when computing the loss.\n output_retrieved(`bool`, *optional*, defaults to `False`):\n If set to `True`, `retrieved_doc_embeds`, `retrieved_doc_ids`, `context_input_ids` and\n `context_attention_mask` are returned. See returned tensors for more detail.\n use_cache (`bool`, *optional*, defaults to `True`):\n Whether or not the model should return the last key/values attentions (not used by all models).\n forced_eos_token_id (`int`, *optional*):\n The id of the token to force as the last generated token when `max_length` is reached. Usually set to\n `eos_token_id`.\n' @add_start_docstrings(SCREAMING_SNAKE_CASE_ ) class SCREAMING_SNAKE_CASE ( SCREAMING_SNAKE_CASE_ ): snake_case__ = "rag" snake_case__ = True def __init__( self : Dict , __SCREAMING_SNAKE_CASE : Optional[Any]=None , __SCREAMING_SNAKE_CASE : Union[str, Any]=True , __SCREAMING_SNAKE_CASE : List[str]=None , __SCREAMING_SNAKE_CASE : Any=None , __SCREAMING_SNAKE_CASE : Any=None , __SCREAMING_SNAKE_CASE : int=None , __SCREAMING_SNAKE_CASE : str=None , __SCREAMING_SNAKE_CASE : Tuple=" / " , __SCREAMING_SNAKE_CASE : Any=" // " , __SCREAMING_SNAKE_CASE : List[str]=5 , __SCREAMING_SNAKE_CASE : Dict=300 , __SCREAMING_SNAKE_CASE : Dict=768 , __SCREAMING_SNAKE_CASE : Any=8 , __SCREAMING_SNAKE_CASE : int="wiki_dpr" , __SCREAMING_SNAKE_CASE : Optional[int]="train" , __SCREAMING_SNAKE_CASE : Union[str, Any]="compressed" , __SCREAMING_SNAKE_CASE : Optional[int]=None , __SCREAMING_SNAKE_CASE : Optional[Any]=None , __SCREAMING_SNAKE_CASE : int=False , __SCREAMING_SNAKE_CASE : Union[str, Any]=False , __SCREAMING_SNAKE_CASE : Union[str, Any]=0.0 , __SCREAMING_SNAKE_CASE : str=True , __SCREAMING_SNAKE_CASE : List[Any]=False , __SCREAMING_SNAKE_CASE : List[str]=False , __SCREAMING_SNAKE_CASE : str=False , __SCREAMING_SNAKE_CASE : Any=True , __SCREAMING_SNAKE_CASE : Union[str, Any]=None , **__SCREAMING_SNAKE_CASE : Optional[Any] , ) -> List[Any]: super().__init__( bos_token_id=__SCREAMING_SNAKE_CASE , pad_token_id=__SCREAMING_SNAKE_CASE , eos_token_id=__SCREAMING_SNAKE_CASE , decoder_start_token_id=__SCREAMING_SNAKE_CASE , forced_eos_token_id=__SCREAMING_SNAKE_CASE , is_encoder_decoder=__SCREAMING_SNAKE_CASE , prefix=__SCREAMING_SNAKE_CASE , vocab_size=__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE , ) assert ( "question_encoder" in kwargs and "generator" in kwargs ), "Config has to be initialized with question_encoder and generator config" a_ : Any = kwargs.pop('''question_encoder''' ) a_ : Dict = question_encoder_config.pop('''model_type''' ) a_ : Optional[int] = kwargs.pop('''generator''' ) a_ : str = decoder_config.pop('''model_type''' ) from ..auto.configuration_auto import AutoConfig a_ : List[str] = AutoConfig.for_model(__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) a_ : Tuple = AutoConfig.for_model(__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) a_ : List[str] = reduce_loss a_ : Any = label_smoothing a_ : Any = exclude_bos_score a_ : Union[str, Any] = do_marginalize a_ : Dict = title_sep a_ : Union[str, Any] = doc_sep a_ : Optional[Any] = n_docs a_ : Tuple = max_combined_length a_ : Dict = dataset a_ : List[str] = dataset_split a_ : int = index_name a_ : int = retrieval_vector_size a_ : int = retrieval_batch_size a_ : int = passages_path a_ : Optional[int] = index_path a_ : List[str] = use_dummy_dataset a_ : Optional[int] = output_retrieved a_ : Any = do_deduplication a_ : str = use_cache if self.forced_eos_token_id is None: a_ : Optional[Any] = getattr(self.generator , '''forced_eos_token_id''' , __SCREAMING_SNAKE_CASE ) @classmethod def SCREAMING_SNAKE_CASE ( cls : Tuple , __SCREAMING_SNAKE_CASE : PretrainedConfig , __SCREAMING_SNAKE_CASE : PretrainedConfig , **__SCREAMING_SNAKE_CASE : List[Any] ) -> PretrainedConfig: return cls(question_encoder=question_encoder_config.to_dict() , generator=generator_config.to_dict() , **__SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE ( self : Tuple ) -> Any: a_ : List[str] = copy.deepcopy(self.__dict__ ) a_ : List[str] = self.question_encoder.to_dict() a_ : str = self.generator.to_dict() a_ : Dict = self.__class__.model_type return output
721
'''simple docstring''' from math import pi, sqrt, tan def _UpperCAmelCase ( __A : float ): if side_length < 0: raise ValueError('''surface_area_cube() only accepts non-negative values''' ) return 6 * side_length**2 def _UpperCAmelCase ( __A : float , __A : float , __A : float ): if length < 0 or breadth < 0 or height < 0: raise ValueError('''surface_area_cuboid() only accepts non-negative values''' ) return 2 * ((length * breadth) + (breadth * height) + (length * height)) def _UpperCAmelCase ( __A : float ): if radius < 0: raise ValueError('''surface_area_sphere() only accepts non-negative values''' ) return 4 * pi * radius**2 def _UpperCAmelCase ( __A : float ): if radius < 0: raise ValueError('''surface_area_hemisphere() only accepts non-negative values''' ) return 3 * pi * radius**2 def _UpperCAmelCase ( __A : float , __A : float ): if radius < 0 or height < 0: raise ValueError('''surface_area_cone() only accepts non-negative values''' ) return pi * radius * (radius + (height**2 + radius**2) ** 0.5) def _UpperCAmelCase ( __A : float , __A : float , __A : float ): if radius_a < 0 or radius_a < 0 or height < 0: raise ValueError( '''surface_area_conical_frustum() only accepts non-negative values''' ) a_ : Any = (height**2 + (radius_a - radius_a) ** 2) ** 0.5 return pi * ((slant_height * (radius_a + radius_a)) + radius_a**2 + radius_a**2) def _UpperCAmelCase ( __A : float , __A : float ): if radius < 0 or height < 0: raise ValueError('''surface_area_cylinder() only accepts non-negative values''' ) return 2 * pi * radius * (height + radius) def _UpperCAmelCase ( __A : float , __A : float ): if torus_radius < 0 or tube_radius < 0: raise ValueError('''surface_area_torus() only accepts non-negative values''' ) if torus_radius < tube_radius: raise ValueError( '''surface_area_torus() does not support spindle or self intersecting tori''' ) return 4 * pow(__A , 2 ) * torus_radius * tube_radius def _UpperCAmelCase ( __A : float , __A : float ): if length < 0 or width < 0: raise ValueError('''area_rectangle() only accepts non-negative values''' ) return length * width def _UpperCAmelCase ( __A : float ): if side_length < 0: raise ValueError('''area_square() only accepts non-negative values''' ) return side_length**2 def _UpperCAmelCase ( __A : float , __A : float ): if base < 0 or height < 0: raise ValueError('''area_triangle() only accepts non-negative values''' ) return (base * height) / 2 def _UpperCAmelCase ( __A : float , __A : float , __A : float ): if sidea < 0 or sidea < 0 or sidea < 0: raise ValueError('''area_triangle_three_sides() only accepts non-negative values''' ) elif sidea + sidea < sidea or sidea + sidea < sidea or sidea + sidea < sidea: raise ValueError('''Given three sides do not form a triangle''' ) a_ : int = (sidea + sidea + sidea) / 2 a_ : Optional[Any] = sqrt( semi_perimeter * (semi_perimeter - sidea) * (semi_perimeter - sidea) * (semi_perimeter - sidea) ) return area def _UpperCAmelCase ( __A : float , __A : float ): if base < 0 or height < 0: raise ValueError('''area_parallelogram() only accepts non-negative values''' ) return base * height def _UpperCAmelCase ( __A : float , __A : float , __A : float ): if basea < 0 or basea < 0 or height < 0: raise ValueError('''area_trapezium() only accepts non-negative values''' ) return 1 / 2 * (basea + basea) * height def _UpperCAmelCase ( __A : float ): if radius < 0: raise ValueError('''area_circle() only accepts non-negative values''' ) return pi * radius**2 def _UpperCAmelCase ( __A : float , __A : float ): if radius_x < 0 or radius_y < 0: raise ValueError('''area_ellipse() only accepts non-negative values''' ) return pi * radius_x * radius_y def _UpperCAmelCase ( __A : float , __A : float ): if diagonal_a < 0 or diagonal_a < 0: raise ValueError('''area_rhombus() only accepts non-negative values''' ) return 1 / 2 * diagonal_a * diagonal_a def _UpperCAmelCase ( __A : int , __A : float ): if not isinstance(__A , __A ) or sides < 3: raise ValueError( '''area_reg_polygon() only accepts integers greater than or \ equal to three as number of sides''' ) elif length < 0: raise ValueError( '''area_reg_polygon() only accepts non-negative values as \ length of a side''' ) return (sides * length**2) / (4 * tan(pi / sides )) return (sides * length**2) / (4 * tan(pi / sides )) if __name__ == "__main__": import doctest doctest.testmod(verbose=True) # verbose so we can see methods missing tests print('[DEMO] Areas of various geometric shapes: \n') print(F"""Rectangle: {area_rectangle(10, 20) = }""") print(F"""Square: {area_square(10) = }""") print(F"""Triangle: {area_triangle(10, 10) = }""") print(F"""Triangle: {area_triangle_three_sides(5, 12, 13) = }""") print(F"""Parallelogram: {area_parallelogram(10, 20) = }""") print(F"""Rhombus: {area_rhombus(10, 20) = }""") print(F"""Trapezium: {area_trapezium(10, 20, 30) = }""") print(F"""Circle: {area_circle(20) = }""") print(F"""Ellipse: {area_ellipse(10, 20) = }""") print('\nSurface Areas of various geometric shapes: \n') print(F"""Cube: {surface_area_cube(20) = }""") print(F"""Cuboid: {surface_area_cuboid(10, 20, 30) = }""") print(F"""Sphere: {surface_area_sphere(20) = }""") print(F"""Hemisphere: {surface_area_hemisphere(20) = }""") print(F"""Cone: {surface_area_cone(10, 20) = }""") print(F"""Conical Frustum: {surface_area_conical_frustum(10, 20, 30) = }""") print(F"""Cylinder: {surface_area_cylinder(10, 20) = }""") print(F"""Torus: {surface_area_torus(20, 10) = }""") print(F"""Equilateral Triangle: {area_reg_polygon(3, 10) = }""") print(F"""Square: {area_reg_polygon(4, 10) = }""") print(F"""Reqular Pentagon: {area_reg_polygon(5, 10) = }""")
666
0
'''simple docstring''' import argparse import json from pathlib import Path import requests import torch from huggingface_hub import cached_download, hf_hub_download, hf_hub_url from PIL import Image from transformers import DetaConfig, DetaForObjectDetection, DetaImageProcessor, SwinConfig from transformers.utils import logging logging.set_verbosity_info() __lowerCAmelCase = logging.get_logger(__name__) def _UpperCAmelCase ( __A : Union[str, Any] ): '''simple docstring''' a_ : Tuple = SwinConfig( embed_dim=1_92 , depths=(2, 2, 18, 2) , num_heads=(6, 12, 24, 48) , window_size=12 , out_features=['''stage2''', '''stage3''', '''stage4'''] , ) a_ : List[Any] = DetaConfig( backbone_config=__A , num_queries=9_00 , encoder_ffn_dim=20_48 , decoder_ffn_dim=20_48 , num_feature_levels=5 , assign_first_stage=__A , with_box_refine=__A , two_stage=__A , ) # set labels a_ : Optional[Any] = '''huggingface/label-files''' if "o365" in model_name: a_ : Optional[Any] = 3_66 a_ : Tuple = '''object365-id2label.json''' else: a_ : Any = 91 a_ : Union[str, Any] = '''coco-detection-id2label.json''' a_ : Tuple = num_labels a_ : str = json.load(open(cached_download(hf_hub_url(__A , __A , repo_type='''dataset''' ) ) , '''r''' ) ) a_ : Optional[int] = {int(__A ): v for k, v in idalabel.items()} a_ : int = idalabel a_ : Dict = {v: k for k, v in idalabel.items()} return config def _UpperCAmelCase ( __A : List[str] ): '''simple docstring''' a_ : Tuple = [] # stem # fmt: off rename_keys.append(('''backbone.0.body.patch_embed.proj.weight''', '''model.backbone.model.embeddings.patch_embeddings.projection.weight''') ) rename_keys.append(('''backbone.0.body.patch_embed.proj.bias''', '''model.backbone.model.embeddings.patch_embeddings.projection.bias''') ) rename_keys.append(('''backbone.0.body.patch_embed.norm.weight''', '''model.backbone.model.embeddings.norm.weight''') ) rename_keys.append(('''backbone.0.body.patch_embed.norm.bias''', '''model.backbone.model.embeddings.norm.bias''') ) # stages for i in range(len(config.backbone_config.depths ) ): for j in range(config.backbone_config.depths[i] ): rename_keys.append((f'backbone.0.body.layers.{i}.blocks.{j}.norm1.weight', f'model.backbone.model.encoder.layers.{i}.blocks.{j}.layernorm_before.weight') ) rename_keys.append((f'backbone.0.body.layers.{i}.blocks.{j}.norm1.bias', f'model.backbone.model.encoder.layers.{i}.blocks.{j}.layernorm_before.bias') ) rename_keys.append((f'backbone.0.body.layers.{i}.blocks.{j}.attn.relative_position_bias_table', f'model.backbone.model.encoder.layers.{i}.blocks.{j}.attention.self.relative_position_bias_table') ) rename_keys.append((f'backbone.0.body.layers.{i}.blocks.{j}.attn.relative_position_index', f'model.backbone.model.encoder.layers.{i}.blocks.{j}.attention.self.relative_position_index') ) rename_keys.append((f'backbone.0.body.layers.{i}.blocks.{j}.attn.proj.weight', f'model.backbone.model.encoder.layers.{i}.blocks.{j}.attention.output.dense.weight') ) rename_keys.append((f'backbone.0.body.layers.{i}.blocks.{j}.attn.proj.bias', f'model.backbone.model.encoder.layers.{i}.blocks.{j}.attention.output.dense.bias') ) rename_keys.append((f'backbone.0.body.layers.{i}.blocks.{j}.norm2.weight', f'model.backbone.model.encoder.layers.{i}.blocks.{j}.layernorm_after.weight') ) rename_keys.append((f'backbone.0.body.layers.{i}.blocks.{j}.norm2.bias', f'model.backbone.model.encoder.layers.{i}.blocks.{j}.layernorm_after.bias') ) rename_keys.append((f'backbone.0.body.layers.{i}.blocks.{j}.mlp.fc1.weight', f'model.backbone.model.encoder.layers.{i}.blocks.{j}.intermediate.dense.weight') ) rename_keys.append((f'backbone.0.body.layers.{i}.blocks.{j}.mlp.fc1.bias', f'model.backbone.model.encoder.layers.{i}.blocks.{j}.intermediate.dense.bias') ) rename_keys.append((f'backbone.0.body.layers.{i}.blocks.{j}.mlp.fc2.weight', f'model.backbone.model.encoder.layers.{i}.blocks.{j}.output.dense.weight') ) rename_keys.append((f'backbone.0.body.layers.{i}.blocks.{j}.mlp.fc2.bias', f'model.backbone.model.encoder.layers.{i}.blocks.{j}.output.dense.bias') ) if i < 3: rename_keys.append((f'backbone.0.body.layers.{i}.downsample.reduction.weight', f'model.backbone.model.encoder.layers.{i}.downsample.reduction.weight') ) rename_keys.append((f'backbone.0.body.layers.{i}.downsample.norm.weight', f'model.backbone.model.encoder.layers.{i}.downsample.norm.weight') ) rename_keys.append((f'backbone.0.body.layers.{i}.downsample.norm.bias', f'model.backbone.model.encoder.layers.{i}.downsample.norm.bias') ) rename_keys.append(('''backbone.0.body.norm1.weight''', '''model.backbone.model.hidden_states_norms.stage2.weight''') ) rename_keys.append(('''backbone.0.body.norm1.bias''', '''model.backbone.model.hidden_states_norms.stage2.bias''') ) rename_keys.append(('''backbone.0.body.norm2.weight''', '''model.backbone.model.hidden_states_norms.stage3.weight''') ) rename_keys.append(('''backbone.0.body.norm2.bias''', '''model.backbone.model.hidden_states_norms.stage3.bias''') ) rename_keys.append(('''backbone.0.body.norm3.weight''', '''model.backbone.model.hidden_states_norms.stage4.weight''') ) rename_keys.append(('''backbone.0.body.norm3.bias''', '''model.backbone.model.hidden_states_norms.stage4.bias''') ) # transformer encoder for i in range(config.encoder_layers ): rename_keys.append((f'transformer.encoder.layers.{i}.self_attn.sampling_offsets.weight', f'model.encoder.layers.{i}.self_attn.sampling_offsets.weight') ) rename_keys.append((f'transformer.encoder.layers.{i}.self_attn.sampling_offsets.bias', f'model.encoder.layers.{i}.self_attn.sampling_offsets.bias') ) rename_keys.append((f'transformer.encoder.layers.{i}.self_attn.attention_weights.weight', f'model.encoder.layers.{i}.self_attn.attention_weights.weight') ) rename_keys.append((f'transformer.encoder.layers.{i}.self_attn.attention_weights.bias', f'model.encoder.layers.{i}.self_attn.attention_weights.bias') ) rename_keys.append((f'transformer.encoder.layers.{i}.self_attn.value_proj.weight', f'model.encoder.layers.{i}.self_attn.value_proj.weight') ) rename_keys.append((f'transformer.encoder.layers.{i}.self_attn.value_proj.bias', f'model.encoder.layers.{i}.self_attn.value_proj.bias') ) rename_keys.append((f'transformer.encoder.layers.{i}.self_attn.output_proj.weight', f'model.encoder.layers.{i}.self_attn.output_proj.weight') ) rename_keys.append((f'transformer.encoder.layers.{i}.self_attn.output_proj.bias', f'model.encoder.layers.{i}.self_attn.output_proj.bias') ) rename_keys.append((f'transformer.encoder.layers.{i}.norm1.weight', f'model.encoder.layers.{i}.self_attn_layer_norm.weight') ) rename_keys.append((f'transformer.encoder.layers.{i}.norm1.bias', f'model.encoder.layers.{i}.self_attn_layer_norm.bias') ) rename_keys.append((f'transformer.encoder.layers.{i}.linear1.weight', f'model.encoder.layers.{i}.fc1.weight') ) rename_keys.append((f'transformer.encoder.layers.{i}.linear1.bias', f'model.encoder.layers.{i}.fc1.bias') ) rename_keys.append((f'transformer.encoder.layers.{i}.linear2.weight', f'model.encoder.layers.{i}.fc2.weight') ) rename_keys.append((f'transformer.encoder.layers.{i}.linear2.bias', f'model.encoder.layers.{i}.fc2.bias') ) rename_keys.append((f'transformer.encoder.layers.{i}.norm2.weight', f'model.encoder.layers.{i}.final_layer_norm.weight') ) rename_keys.append((f'transformer.encoder.layers.{i}.norm2.bias', f'model.encoder.layers.{i}.final_layer_norm.bias') ) # transformer decoder for i in range(config.decoder_layers ): rename_keys.append((f'transformer.decoder.layers.{i}.cross_attn.sampling_offsets.weight', f'model.decoder.layers.{i}.encoder_attn.sampling_offsets.weight') ) rename_keys.append((f'transformer.decoder.layers.{i}.cross_attn.sampling_offsets.bias', f'model.decoder.layers.{i}.encoder_attn.sampling_offsets.bias') ) rename_keys.append((f'transformer.decoder.layers.{i}.cross_attn.attention_weights.weight', f'model.decoder.layers.{i}.encoder_attn.attention_weights.weight') ) rename_keys.append((f'transformer.decoder.layers.{i}.cross_attn.attention_weights.bias', f'model.decoder.layers.{i}.encoder_attn.attention_weights.bias') ) rename_keys.append((f'transformer.decoder.layers.{i}.cross_attn.value_proj.weight', f'model.decoder.layers.{i}.encoder_attn.value_proj.weight') ) rename_keys.append((f'transformer.decoder.layers.{i}.cross_attn.value_proj.bias', f'model.decoder.layers.{i}.encoder_attn.value_proj.bias') ) rename_keys.append((f'transformer.decoder.layers.{i}.cross_attn.output_proj.weight', f'model.decoder.layers.{i}.encoder_attn.output_proj.weight') ) rename_keys.append((f'transformer.decoder.layers.{i}.cross_attn.output_proj.bias', f'model.decoder.layers.{i}.encoder_attn.output_proj.bias') ) rename_keys.append((f'transformer.decoder.layers.{i}.norm1.weight', f'model.decoder.layers.{i}.encoder_attn_layer_norm.weight') ) rename_keys.append((f'transformer.decoder.layers.{i}.norm1.bias', f'model.decoder.layers.{i}.encoder_attn_layer_norm.bias') ) rename_keys.append((f'transformer.decoder.layers.{i}.self_attn.out_proj.weight', f'model.decoder.layers.{i}.self_attn.out_proj.weight') ) rename_keys.append((f'transformer.decoder.layers.{i}.self_attn.out_proj.bias', f'model.decoder.layers.{i}.self_attn.out_proj.bias') ) rename_keys.append((f'transformer.decoder.layers.{i}.norm2.weight', f'model.decoder.layers.{i}.self_attn_layer_norm.weight') ) rename_keys.append((f'transformer.decoder.layers.{i}.norm2.bias', f'model.decoder.layers.{i}.self_attn_layer_norm.bias') ) rename_keys.append((f'transformer.decoder.layers.{i}.linear1.weight', f'model.decoder.layers.{i}.fc1.weight') ) rename_keys.append((f'transformer.decoder.layers.{i}.linear1.bias', f'model.decoder.layers.{i}.fc1.bias') ) rename_keys.append((f'transformer.decoder.layers.{i}.linear2.weight', f'model.decoder.layers.{i}.fc2.weight') ) rename_keys.append((f'transformer.decoder.layers.{i}.linear2.bias', f'model.decoder.layers.{i}.fc2.bias') ) rename_keys.append((f'transformer.decoder.layers.{i}.norm3.weight', f'model.decoder.layers.{i}.final_layer_norm.weight') ) rename_keys.append((f'transformer.decoder.layers.{i}.norm3.bias', f'model.decoder.layers.{i}.final_layer_norm.bias') ) # fmt: on return rename_keys def _UpperCAmelCase ( __A : str , __A : int , __A : Tuple ): '''simple docstring''' a_ : str = dct.pop(__A ) a_ : Dict = val def _UpperCAmelCase ( __A : List[str] , __A : Optional[int] ): '''simple docstring''' a_ : str = [int(backbone_config.embed_dim * 2**i ) for i in range(len(backbone_config.depths ) )] for i in range(len(backbone_config.depths ) ): a_ : Tuple = num_features[i] for j in range(backbone_config.depths[i] ): # fmt: off # read in weights + bias of input projection layer (in original implementation, this is a single matrix + bias) a_ : List[str] = state_dict.pop(f'backbone.0.body.layers.{i}.blocks.{j}.attn.qkv.weight' ) a_ : str = state_dict.pop(f'backbone.0.body.layers.{i}.blocks.{j}.attn.qkv.bias' ) # next, add query, keys and values (in that order) to the state dict a_ : Optional[Any] = in_proj_weight[:dim, :] a_ : List[Any] = in_proj_bias[: dim] a_ : Optional[Any] = in_proj_weight[ dim : dim * 2, : ] a_ : Union[str, Any] = in_proj_bias[ dim : dim * 2 ] a_ : Optional[int] = in_proj_weight[ -dim :, : ] a_ : int = in_proj_bias[-dim :] # fmt: on def _UpperCAmelCase ( __A : Dict , __A : Dict ): '''simple docstring''' a_ : Any = config.d_model for i in range(config.decoder_layers ): # read in weights + bias of input projection layer of self-attention a_ : int = state_dict.pop(f'transformer.decoder.layers.{i}.self_attn.in_proj_weight' ) a_ : Any = state_dict.pop(f'transformer.decoder.layers.{i}.self_attn.in_proj_bias' ) # next, add query, keys and values (in that order) to the state dict a_ : Dict = in_proj_weight[:hidden_size, :] a_ : Tuple = in_proj_bias[:hidden_size] a_ : Any = in_proj_weight[ hidden_size : hidden_size * 2, : ] a_ : Tuple = in_proj_bias[hidden_size : hidden_size * 2] a_ : Optional[int] = in_proj_weight[-hidden_size:, :] a_ : int = in_proj_bias[-hidden_size:] def _UpperCAmelCase ( ): '''simple docstring''' a_ : Union[str, Any] = '''http://images.cocodataset.org/val2017/000000039769.jpg''' a_ : List[str] = Image.open(requests.get(__A , stream=__A ).raw ) return im @torch.no_grad() def _UpperCAmelCase ( __A : int , __A : int , __A : Any ): '''simple docstring''' a_ : Union[str, Any] = get_deta_config(__A ) # load original state dict if model_name == "deta-swin-large": a_ : Optional[Any] = hf_hub_download(repo_id='''nielsr/deta-checkpoints''' , filename='''adet_swin_ft.pth''' ) elif model_name == "deta-swin-large-o365": a_ : List[str] = hf_hub_download(repo_id='''jozhang97/deta-swin-l-o365''' , filename='''deta_swin_pt_o365.pth''' ) else: raise ValueError(f'Model name {model_name} not supported' ) a_ : List[Any] = torch.load(__A , map_location='''cpu''' )['''model'''] # original state dict for name, param in state_dict.items(): print(__A , param.shape ) # rename keys a_ : Union[str, Any] = create_rename_keys(__A ) for src, dest in rename_keys: rename_key(__A , __A , __A ) read_in_swin_q_k_v(__A , config.backbone_config ) read_in_decoder_q_k_v(__A , __A ) # fix some prefixes for key in state_dict.copy().keys(): if "transformer.decoder.class_embed" in key or "transformer.decoder.bbox_embed" in key: a_ : Optional[Any] = state_dict.pop(__A ) a_ : int = val if "input_proj" in key: a_ : str = state_dict.pop(__A ) a_ : Optional[Any] = val if "level_embed" in key or "pos_trans" in key or "pix_trans" in key or "enc_output" in key: a_ : List[str] = state_dict.pop(__A ) a_ : List[Any] = val # finally, create HuggingFace model and load state dict a_ : Dict = DetaForObjectDetection(__A ) model.load_state_dict(__A ) model.eval() a_ : int = '''cuda''' if torch.cuda.is_available() else '''cpu''' model.to(__A ) # load image processor a_ : List[Any] = DetaImageProcessor(format='''coco_detection''' ) # verify our conversion on image a_ : Dict = prepare_img() a_ : Optional[int] = processor(images=__A , return_tensors='''pt''' ) a_ : Any = encoding['''pixel_values'''] a_ : int = model(pixel_values.to(__A ) ) # verify logits print('''Logits:''' , outputs.logits[0, :3, :3] ) print('''Boxes:''' , outputs.pred_boxes[0, :3, :3] ) if model_name == "deta-swin-large": a_ : Optional[int] = torch.tensor( [[-7.6308, -2.8485, -5.3737], [-7.2037, -4.5505, -4.8027], [-7.2943, -4.2611, -4.6617]] ) a_ : Tuple = torch.tensor([[0.4987, 0.4969, 0.9999], [0.2549, 0.5498, 0.4805], [0.5498, 0.2757, 0.0569]] ) elif model_name == "deta-swin-large-o365": a_ : Union[str, Any] = torch.tensor( [[-8.0122, -3.5720, -4.9717], [-8.1547, -3.6886, -4.6389], [-7.6610, -3.6194, -5.0134]] ) a_ : Any = torch.tensor([[0.2523, 0.5549, 0.4881], [0.7715, 0.4149, 0.4601], [0.5503, 0.2753, 0.0575]] ) assert torch.allclose(outputs.logits[0, :3, :3] , expected_logits.to(__A ) , atol=1E-4 ) assert torch.allclose(outputs.pred_boxes[0, :3, :3] , expected_boxes.to(__A ) , atol=1E-4 ) print('''Everything ok!''' ) if pytorch_dump_folder_path: # Save model and processor logger.info(f'Saving PyTorch model and processor to {pytorch_dump_folder_path}...' ) Path(__A ).mkdir(exist_ok=__A ) model.save_pretrained(__A ) processor.save_pretrained(__A ) # Push to hub if push_to_hub: print('''Pushing model and processor to hub...''' ) model.push_to_hub(f'jozhang97/{model_name}' ) processor.push_to_hub(f'jozhang97/{model_name}' ) if __name__ == "__main__": __lowerCAmelCase = argparse.ArgumentParser() parser.add_argument( '--model_name', type=str, default='deta-swin-large', choices=['deta-swin-large', 'deta-swin-large-o365'], help='Name of the model you\'d like to convert.', ) parser.add_argument( '--pytorch_dump_folder_path', default=None, type=str, help='Path to the folder to output PyTorch model.', ) parser.add_argument( '--push_to_hub', action='store_true', help='Whether or not to push the converted model to the ๐Ÿค— hub.' ) __lowerCAmelCase = parser.parse_args() convert_deta_checkpoint(args.model_name, args.pytorch_dump_folder_path, args.push_to_hub)
700
'''simple docstring''' import random import unittest import torch from diffusers import IFInpaintingSuperResolutionPipeline from diffusers.utils import floats_tensor from diffusers.utils.import_utils import is_xformers_available from diffusers.utils.testing_utils import skip_mps, torch_device from ..pipeline_params import ( TEXT_GUIDED_IMAGE_INPAINTING_BATCH_PARAMS, TEXT_GUIDED_IMAGE_INPAINTING_PARAMS, ) from ..test_pipelines_common import PipelineTesterMixin from . import IFPipelineTesterMixin @skip_mps class SCREAMING_SNAKE_CASE ( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , unittest.TestCase ): snake_case__ = IFInpaintingSuperResolutionPipeline snake_case__ = TEXT_GUIDED_IMAGE_INPAINTING_PARAMS - {"width", "height"} snake_case__ = TEXT_GUIDED_IMAGE_INPAINTING_BATCH_PARAMS.union({"original_image"} ) snake_case__ = PipelineTesterMixin.required_optional_params - {"latents"} def SCREAMING_SNAKE_CASE ( self : str ) -> List[str]: return self._get_superresolution_dummy_components() def SCREAMING_SNAKE_CASE ( self : Union[str, Any] , __SCREAMING_SNAKE_CASE : Optional[Any] , __SCREAMING_SNAKE_CASE : Dict=0 ) -> List[Any]: if str(__SCREAMING_SNAKE_CASE ).startswith('''mps''' ): a_ : Optional[int] = torch.manual_seed(__SCREAMING_SNAKE_CASE ) else: a_ : str = torch.Generator(device=__SCREAMING_SNAKE_CASE ).manual_seed(__SCREAMING_SNAKE_CASE ) a_ : Dict = floats_tensor((1, 3, 16, 16) , rng=random.Random(__SCREAMING_SNAKE_CASE ) ).to(__SCREAMING_SNAKE_CASE ) a_ : Union[str, Any] = floats_tensor((1, 3, 32, 32) , rng=random.Random(__SCREAMING_SNAKE_CASE ) ).to(__SCREAMING_SNAKE_CASE ) a_ : Tuple = floats_tensor((1, 3, 32, 32) , rng=random.Random(__SCREAMING_SNAKE_CASE ) ).to(__SCREAMING_SNAKE_CASE ) a_ : Union[str, Any] = { '''prompt''': '''A painting of a squirrel eating a burger''', '''image''': image, '''original_image''': original_image, '''mask_image''': mask_image, '''generator''': generator, '''num_inference_steps''': 2, '''output_type''': '''numpy''', } return inputs @unittest.skipIf( torch_device != '''cuda''' or not is_xformers_available() , reason='''XFormers attention is only available with CUDA and `xformers` installed''' , ) def SCREAMING_SNAKE_CASE ( self : Optional[Any] ) -> List[str]: self._test_xformers_attention_forwardGenerator_pass(expected_max_diff=1e-3 ) def SCREAMING_SNAKE_CASE ( self : int ) -> int: self._test_save_load_optional_components() @unittest.skipIf(torch_device != '''cuda''' , reason='''float16 requires CUDA''' ) def SCREAMING_SNAKE_CASE ( self : str ) -> Tuple: # Due to non-determinism in save load of the hf-internal-testing/tiny-random-t5 text encoder super().test_save_load_floataa(expected_max_diff=1e-1 ) def SCREAMING_SNAKE_CASE ( self : List[str] ) -> Optional[int]: self._test_attention_slicing_forward_pass(expected_max_diff=1e-2 ) def SCREAMING_SNAKE_CASE ( self : Union[str, Any] ) -> Optional[Any]: self._test_save_load_local() def SCREAMING_SNAKE_CASE ( self : Dict ) -> Optional[Any]: self._test_inference_batch_single_identical( expected_max_diff=1e-2 , )
666
0
from __future__ import annotations import numpy as np from numpy import floataa from numpy.typing import NDArray def _UpperCAmelCase ( __A : NDArray[floataa] , __A : NDArray[floataa] , __A : list[int] , __A : int , ): a_ : int = coefficient_matrix.shape a_ : Optional[Any] = constant_matrix.shape if rowsa != colsa: a_ : Dict = f'Coefficient matrix dimensions must be nxn but received {rowsa}x{colsa}' raise ValueError(__A ) if colsa != 1: a_ : Tuple = f'Constant matrix must be nx1 but received {rowsa}x{colsa}' raise ValueError(__A ) if rowsa != rowsa: a_ : List[Any] = ( '''Coefficient and constant matrices dimensions must be nxn and nx1 but ''' f'received {rowsa}x{colsa} and {rowsa}x{colsa}' ) raise ValueError(__A ) if len(__A ) != rowsa: a_ : Tuple = ( '''Number of initial values must be equal to number of rows in coefficient ''' f'matrix but received {len(__A )} and {rowsa}' ) raise ValueError(__A ) if iterations <= 0: raise ValueError('''Iterations must be at least 1''' ) a_ : NDArray[floataa] = np.concatenate( (coefficient_matrix, constant_matrix) , axis=1 ) a_ : Optional[Any] = table.shape strictly_diagonally_dominant(__A ) # Iterates the whole matrix for given number of times for _ in range(__A ): a_ : List[str] = [] for row in range(__A ): a_ : Optional[Any] = 0 for col in range(__A ): if col == row: a_ : Union[str, Any] = table[row][col] elif col == cols - 1: a_ : Union[str, Any] = table[row][col] else: temp += (-1) * table[row][col] * init_val[col] a_ : Tuple = (temp + val) / denom new_val.append(__A ) a_ : str = new_val return [float(__A ) for i in new_val] def _UpperCAmelCase ( __A : NDArray[floataa] ): a_ : Dict = table.shape a_ : int = True for i in range(0 , __A ): a_ : Optional[Any] = 0 for j in range(0 , cols - 1 ): if i == j: continue else: total += table[i][j] if table[i][i] <= total: raise ValueError('''Coefficient matrix is not strictly diagonally dominant''' ) return is_diagonally_dominant # Test Cases if __name__ == "__main__": import doctest doctest.testmod()
701
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available __lowerCAmelCase = { 'configuration_git': ['GIT_PRETRAINED_CONFIG_ARCHIVE_MAP', 'GitConfig', 'GitVisionConfig'], 'processing_git': ['GitProcessor'], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __lowerCAmelCase = [ 'GIT_PRETRAINED_MODEL_ARCHIVE_LIST', 'GitForCausalLM', 'GitModel', 'GitPreTrainedModel', 'GitVisionModel', ] if TYPE_CHECKING: from .configuration_git import GIT_PRETRAINED_CONFIG_ARCHIVE_MAP, GitConfig, GitVisionConfig from .processing_git import GitProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_git import ( GIT_PRETRAINED_MODEL_ARCHIVE_LIST, GitForCausalLM, GitModel, GitPreTrainedModel, GitVisionModel, ) else: import sys __lowerCAmelCase = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
666
0
'''simple docstring''' import unittest import numpy as np import requests from transformers.testing_utils import require_torch, require_vision from transformers.utils import is_torch_available, is_vision_available from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs if is_torch_available(): import torch from transformers.pytorch_utils import is_torch_greater_or_equal_than_1_11 else: __lowerCAmelCase = False if is_vision_available(): from PIL import Image from transformers import PixaStructImageProcessor class SCREAMING_SNAKE_CASE ( unittest.TestCase ): def __init__( self : Tuple , __SCREAMING_SNAKE_CASE : Union[str, Any] , __SCREAMING_SNAKE_CASE : Any=7 , __SCREAMING_SNAKE_CASE : Tuple=3 , __SCREAMING_SNAKE_CASE : Union[str, Any]=18 , __SCREAMING_SNAKE_CASE : Union[str, Any]=30 , __SCREAMING_SNAKE_CASE : Union[str, Any]=400 , __SCREAMING_SNAKE_CASE : str=None , __SCREAMING_SNAKE_CASE : List[Any]=True , __SCREAMING_SNAKE_CASE : str=True , __SCREAMING_SNAKE_CASE : Dict=None , ) -> List[Any]: a_ : List[Any] = size if size is not None else {'''height''': 20, '''width''': 20} a_ : Optional[int] = parent a_ : List[str] = batch_size a_ : List[str] = num_channels a_ : str = image_size a_ : Any = min_resolution a_ : Dict = max_resolution a_ : List[str] = size a_ : str = do_normalize a_ : Optional[Any] = do_convert_rgb a_ : Any = [512, 1024, 2048, 4096] a_ : Dict = patch_size if patch_size is not None else {'''height''': 16, '''width''': 16} def SCREAMING_SNAKE_CASE ( self : int ) -> List[Any]: return {"do_normalize": self.do_normalize, "do_convert_rgb": self.do_convert_rgb} def SCREAMING_SNAKE_CASE ( self : Dict ) -> Any: a_ : Tuple = '''https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/tasks/australia.jpg''' a_ : List[str] = Image.open(requests.get(__SCREAMING_SNAKE_CASE , stream=__SCREAMING_SNAKE_CASE ).raw ).convert('''RGB''' ) return raw_image @unittest.skipIf( not is_torch_greater_or_equal_than_1_11 , reason="`Pix2StructImageProcessor` requires `torch>=1.11.0`." , ) @require_torch @require_vision class SCREAMING_SNAKE_CASE ( SCREAMING_SNAKE_CASE_ , unittest.TestCase ): snake_case__ = PixaStructImageProcessor if is_vision_available() else None def SCREAMING_SNAKE_CASE ( self : int ) -> Dict: a_ : List[str] = PixaStructImageProcessingTester(self ) @property def SCREAMING_SNAKE_CASE ( self : str ) -> Optional[Any]: return self.image_processor_tester.prepare_image_processor_dict() def SCREAMING_SNAKE_CASE ( self : List[Any] ) -> List[str]: a_ : Union[str, Any] = self.image_processing_class(**self.image_processor_dict ) self.assertTrue(hasattr(__SCREAMING_SNAKE_CASE , '''do_normalize''' ) ) self.assertTrue(hasattr(__SCREAMING_SNAKE_CASE , '''do_convert_rgb''' ) ) def SCREAMING_SNAKE_CASE ( self : Optional[int] ) -> Tuple: a_ : Union[str, Any] = self.image_processor_tester.prepare_dummy_image() a_ : Tuple = self.image_processing_class(**self.image_processor_dict ) a_ : int = 2048 a_ : Tuple = image_processor(__SCREAMING_SNAKE_CASE , return_tensors='''pt''' , max_patches=__SCREAMING_SNAKE_CASE ) self.assertTrue(torch.allclose(inputs.flattened_patches.mean() , torch.tensor(0.0606 ) , atol=1e-3 , rtol=1e-3 ) ) def SCREAMING_SNAKE_CASE ( self : Optional[int] ) -> Tuple: # Initialize image_processor a_ : str = self.image_processing_class(**self.image_processor_dict ) # create random PIL images a_ : Optional[Any] = prepare_image_inputs(self.image_processor_tester , equal_resolution=__SCREAMING_SNAKE_CASE ) for image in image_inputs: self.assertIsInstance(__SCREAMING_SNAKE_CASE , Image.Image ) # Test not batched input a_ : Dict = ( (self.image_processor_tester.patch_size['''height'''] * self.image_processor_tester.patch_size['''width''']) * self.image_processor_tester.num_channels ) + 2 for max_patch in self.image_processor_tester.max_patches: # Test not batched input a_ : int = image_processor( image_inputs[0] , return_tensors='''pt''' , max_patches=__SCREAMING_SNAKE_CASE ).flattened_patches self.assertEqual( encoded_images.shape , (1, max_patch, expected_hidden_dim) , ) # Test batched a_ : List[Any] = image_processor( __SCREAMING_SNAKE_CASE , return_tensors='''pt''' , max_patches=__SCREAMING_SNAKE_CASE ).flattened_patches self.assertEqual( encoded_images.shape , (self.image_processor_tester.batch_size, max_patch, expected_hidden_dim) , ) def SCREAMING_SNAKE_CASE ( self : Union[str, Any] ) -> str: # Initialize image_processor a_ : List[str] = self.image_processing_class(**self.image_processor_dict ) # create random PIL images a_ : Optional[int] = prepare_image_inputs(self.image_processor_tester , equal_resolution=__SCREAMING_SNAKE_CASE ) for image in image_inputs: self.assertIsInstance(__SCREAMING_SNAKE_CASE , Image.Image ) # Test not batched input a_ : List[str] = ( (self.image_processor_tester.patch_size['''height'''] * self.image_processor_tester.patch_size['''width''']) * self.image_processor_tester.num_channels ) + 2 a_ : Any = True for max_patch in self.image_processor_tester.max_patches: # Test not batched input with self.assertRaises(__SCREAMING_SNAKE_CASE ): a_ : str = image_processor( image_inputs[0] , return_tensors='''pt''' , max_patches=__SCREAMING_SNAKE_CASE ).flattened_patches a_ : str = '''Hello''' a_ : Optional[int] = image_processor( image_inputs[0] , return_tensors='''pt''' , max_patches=__SCREAMING_SNAKE_CASE , header_text=__SCREAMING_SNAKE_CASE ).flattened_patches self.assertEqual( encoded_images.shape , (1, max_patch, expected_hidden_dim) , ) # Test batched a_ : str = image_processor( __SCREAMING_SNAKE_CASE , return_tensors='''pt''' , max_patches=__SCREAMING_SNAKE_CASE , header_text=__SCREAMING_SNAKE_CASE ).flattened_patches self.assertEqual( encoded_images.shape , (self.image_processor_tester.batch_size, max_patch, expected_hidden_dim) , ) def SCREAMING_SNAKE_CASE ( self : Dict ) -> Optional[int]: # Initialize image_processor a_ : Optional[int] = self.image_processing_class(**self.image_processor_dict ) # create random numpy tensors a_ : List[Any] = prepare_image_inputs(self.image_processor_tester , equal_resolution=__SCREAMING_SNAKE_CASE , numpify=__SCREAMING_SNAKE_CASE ) for image in image_inputs: self.assertIsInstance(__SCREAMING_SNAKE_CASE , np.ndarray ) a_ : List[Any] = ( (self.image_processor_tester.patch_size['''height'''] * self.image_processor_tester.patch_size['''width''']) * self.image_processor_tester.num_channels ) + 2 for max_patch in self.image_processor_tester.max_patches: # Test not batched input a_ : Optional[Any] = image_processor( image_inputs[0] , return_tensors='''pt''' , max_patches=__SCREAMING_SNAKE_CASE ).flattened_patches self.assertEqual( encoded_images.shape , (1, max_patch, expected_hidden_dim) , ) # Test batched a_ : Union[str, Any] = image_processor( __SCREAMING_SNAKE_CASE , return_tensors='''pt''' , max_patches=__SCREAMING_SNAKE_CASE ).flattened_patches self.assertEqual( encoded_images.shape , (self.image_processor_tester.batch_size, max_patch, expected_hidden_dim) , ) def SCREAMING_SNAKE_CASE ( self : int ) -> List[str]: # Initialize image_processor a_ : Any = self.image_processing_class(**self.image_processor_dict ) # create random PyTorch tensors a_ : Optional[Any] = prepare_image_inputs(self.image_processor_tester , equal_resolution=__SCREAMING_SNAKE_CASE , torchify=__SCREAMING_SNAKE_CASE ) for image in image_inputs: self.assertIsInstance(__SCREAMING_SNAKE_CASE , torch.Tensor ) # Test not batched input a_ : Any = ( (self.image_processor_tester.patch_size['''height'''] * self.image_processor_tester.patch_size['''width''']) * self.image_processor_tester.num_channels ) + 2 for max_patch in self.image_processor_tester.max_patches: # Test not batched input a_ : Union[str, Any] = image_processor( image_inputs[0] , return_tensors='''pt''' , max_patches=__SCREAMING_SNAKE_CASE ).flattened_patches self.assertEqual( encoded_images.shape , (1, max_patch, expected_hidden_dim) , ) # Test batched a_ : Tuple = image_processor( __SCREAMING_SNAKE_CASE , return_tensors='''pt''' , max_patches=__SCREAMING_SNAKE_CASE ).flattened_patches self.assertEqual( encoded_images.shape , (self.image_processor_tester.batch_size, max_patch, expected_hidden_dim) , ) @unittest.skipIf( not is_torch_greater_or_equal_than_1_11 , reason="`Pix2StructImageProcessor` requires `torch>=1.11.0`." , ) @require_torch @require_vision class SCREAMING_SNAKE_CASE ( SCREAMING_SNAKE_CASE_ , unittest.TestCase ): snake_case__ = PixaStructImageProcessor if is_vision_available() else None def SCREAMING_SNAKE_CASE ( self : Tuple ) -> Any: a_ : Optional[Any] = PixaStructImageProcessingTester(self , num_channels=4 ) a_ : Tuple = 3 @property def SCREAMING_SNAKE_CASE ( self : Optional[int] ) -> Optional[int]: return self.image_processor_tester.prepare_image_processor_dict() def SCREAMING_SNAKE_CASE ( self : Optional[Any] ) -> Optional[int]: a_ : Optional[int] = self.image_processing_class(**self.image_processor_dict ) self.assertTrue(hasattr(__SCREAMING_SNAKE_CASE , '''do_normalize''' ) ) self.assertTrue(hasattr(__SCREAMING_SNAKE_CASE , '''do_convert_rgb''' ) ) def SCREAMING_SNAKE_CASE ( self : Optional[int] ) -> Optional[Any]: # Initialize image_processor a_ : Optional[int] = self.image_processing_class(**self.image_processor_dict ) # create random PIL images a_ : Dict = prepare_image_inputs(self.image_processor_tester , equal_resolution=__SCREAMING_SNAKE_CASE ) for image in image_inputs: self.assertIsInstance(__SCREAMING_SNAKE_CASE , Image.Image ) # Test not batched input a_ : int = ( (self.image_processor_tester.patch_size['''height'''] * self.image_processor_tester.patch_size['''width''']) * (self.image_processor_tester.num_channels - 1) ) + 2 for max_patch in self.image_processor_tester.max_patches: # Test not batched input a_ : Any = image_processor( image_inputs[0] , return_tensors='''pt''' , max_patches=__SCREAMING_SNAKE_CASE ).flattened_patches self.assertEqual( encoded_images.shape , (1, max_patch, expected_hidden_dim) , ) # Test batched a_ : Dict = image_processor( __SCREAMING_SNAKE_CASE , return_tensors='''pt''' , max_patches=__SCREAMING_SNAKE_CASE ).flattened_patches self.assertEqual( encoded_images.shape , (self.image_processor_tester.batch_size, max_patch, expected_hidden_dim) , )
702
'''simple docstring''' from unittest.mock import patch import pyspark from datasets.packaged_modules.spark.spark import ( Spark, SparkExamplesIterable, _generate_iterable_examples, ) from ..utils import ( require_dill_gt_0_3_2, require_not_windows, ) def _UpperCAmelCase ( __A : List[str] , __A : List[Any] ): a_ : Any = [] for part_id in partition_order: a_ : str = df.where(f'SPARK_PARTITION_ID() = {part_id}' ).collect() for row_idx, row in enumerate(__A ): expected_row_ids_and_row_dicts.append((f'{part_id}_{row_idx}', row.asDict()) ) return expected_row_ids_and_row_dicts @require_not_windows @require_dill_gt_0_3_2 def _UpperCAmelCase ( ): a_ : List[str] = pyspark.sql.SparkSession.builder.master('''local[*]''' ).appName('''pyspark''' ).getOrCreate() a_ : Union[str, Any] = spark.range(1_00 ).repartition(1 ) a_ : Any = Spark(__A ) # The id ints will be converted to Pyarrow int64s, so each row will be 8 bytes. Setting a max_shard_size of 16 means # that each partition can hold 2 rows. spark_builder._repartition_df_if_needed(max_shard_size=16 ) # Given that the dataframe has 100 rows and each partition has 2 rows, we expect 50 partitions. assert spark_builder.df.rdd.getNumPartitions() == 50 @require_not_windows @require_dill_gt_0_3_2 def _UpperCAmelCase ( ): a_ : List[Any] = pyspark.sql.SparkSession.builder.master('''local[*]''' ).appName('''pyspark''' ).getOrCreate() a_ : int = spark.range(10 ).repartition(2 ) a_ : Tuple = [1, 0] a_ : List[str] = _generate_iterable_examples(__A , __A ) # Reverse the partitions. a_ : int = _get_expected_row_ids_and_row_dicts_for_partition_order(__A , __A ) for i, (row_id, row_dict) in enumerate(generate_fn() ): a_ , a_ : List[Any] = expected_row_ids_and_row_dicts[i] assert row_id == expected_row_id assert row_dict == expected_row_dict @require_not_windows @require_dill_gt_0_3_2 def _UpperCAmelCase ( ): a_ : int = pyspark.sql.SparkSession.builder.master('''local[*]''' ).appName('''pyspark''' ).getOrCreate() a_ : str = spark.range(10 ).repartition(1 ) a_ : Tuple = SparkExamplesIterable(__A ) assert it.n_shards == 1 for i, (row_id, row_dict) in enumerate(__A ): assert row_id == f'0_{i}' assert row_dict == {"id": i} @require_not_windows @require_dill_gt_0_3_2 def _UpperCAmelCase ( ): a_ : Tuple = pyspark.sql.SparkSession.builder.master('''local[*]''' ).appName('''pyspark''' ).getOrCreate() a_ : str = spark.range(30 ).repartition(3 ) # Mock the generator so that shuffle reverses the partition indices. with patch('''numpy.random.Generator''' ) as generator_mock: a_ : Union[str, Any] = lambda __A : x.reverse() a_ : Any = _get_expected_row_ids_and_row_dicts_for_partition_order(__A , [2, 1, 0] ) a_ : str = SparkExamplesIterable(__A ).shuffle_data_sources(__A ) assert shuffled_it.n_shards == 3 for i, (row_id, row_dict) in enumerate(__A ): a_ , a_ : Optional[Any] = expected_row_ids_and_row_dicts[i] assert row_id == expected_row_id assert row_dict == expected_row_dict @require_not_windows @require_dill_gt_0_3_2 def _UpperCAmelCase ( ): a_ : int = pyspark.sql.SparkSession.builder.master('''local[*]''' ).appName('''pyspark''' ).getOrCreate() a_ : List[str] = spark.range(20 ).repartition(4 ) # Partitions 0 and 2 a_ : Dict = SparkExamplesIterable(__A ).shard_data_sources(worker_id=0 , num_workers=2 ) assert shard_it_a.n_shards == 2 a_ : Optional[Any] = _get_expected_row_ids_and_row_dicts_for_partition_order(__A , [0, 2] ) for i, (row_id, row_dict) in enumerate(__A ): a_ , a_ : Tuple = expected_row_ids_and_row_dicts_a[i] assert row_id == expected_row_id assert row_dict == expected_row_dict # Partitions 1 and 3 a_ : List[Any] = SparkExamplesIterable(__A ).shard_data_sources(worker_id=1 , num_workers=2 ) assert shard_it_a.n_shards == 2 a_ : Optional[int] = _get_expected_row_ids_and_row_dicts_for_partition_order(__A , [1, 3] ) for i, (row_id, row_dict) in enumerate(__A ): a_ , a_ : Any = expected_row_ids_and_row_dicts_a[i] assert row_id == expected_row_id assert row_dict == expected_row_dict @require_not_windows @require_dill_gt_0_3_2 def _UpperCAmelCase ( ): a_ : Any = pyspark.sql.SparkSession.builder.master('''local[*]''' ).appName('''pyspark''' ).getOrCreate() a_ : List[Any] = spark.range(1_00 ).repartition(1 ) a_ : Optional[Any] = Spark(__A ) # Choose a small max_shard_size for maximum partitioning. spark_builder._repartition_df_if_needed(max_shard_size=1 ) # The new number of partitions should not be greater than the number of rows. assert spark_builder.df.rdd.getNumPartitions() == 1_00
666
0
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available __lowerCAmelCase = { 'configuration_clipseg': [ 'CLIPSEG_PRETRAINED_CONFIG_ARCHIVE_MAP', 'CLIPSegConfig', 'CLIPSegTextConfig', 'CLIPSegVisionConfig', ], 'processing_clipseg': ['CLIPSegProcessor'], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __lowerCAmelCase = [ 'CLIPSEG_PRETRAINED_MODEL_ARCHIVE_LIST', 'CLIPSegModel', 'CLIPSegPreTrainedModel', 'CLIPSegTextModel', 'CLIPSegVisionModel', 'CLIPSegForImageSegmentation', ] if TYPE_CHECKING: from .configuration_clipseg import ( CLIPSEG_PRETRAINED_CONFIG_ARCHIVE_MAP, CLIPSegConfig, CLIPSegTextConfig, CLIPSegVisionConfig, ) from .processing_clipseg import CLIPSegProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_clipseg import ( CLIPSEG_PRETRAINED_MODEL_ARCHIVE_LIST, CLIPSegForImageSegmentation, CLIPSegModel, CLIPSegPreTrainedModel, CLIPSegTextModel, CLIPSegVisionModel, ) else: import sys __lowerCAmelCase = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
703
'''simple docstring''' from collections import OrderedDict from typing import TYPE_CHECKING, Any, List, Mapping, Optional from packaging import version if TYPE_CHECKING: from ... import PreTrainedTokenizer, TensorType from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfigWithPast, PatchingSpec from ...utils import is_torch_available, logging __lowerCAmelCase = logging.get_logger(__name__) __lowerCAmelCase = { 'bigscience/bloom': 'https://huggingface.co/bigscience/bloom/resolve/main/config.json', 'bigscience/bloom-560m': 'https://huggingface.co/bigscience/bloom-560m/blob/main/config.json', 'bigscience/bloom-1b1': 'https://huggingface.co/bigscience/bloom-1b1/blob/main/config.json', 'bigscience/bloom-1b7': 'https://huggingface.co/bigscience/bloom-1b7/blob/main/config.json', 'bigscience/bloom-3b': 'https://huggingface.co/bigscience/bloom-3b/blob/main/config.json', 'bigscience/bloom-7b1': 'https://huggingface.co/bigscience/bloom-7b1/blob/main/config.json', } class SCREAMING_SNAKE_CASE ( SCREAMING_SNAKE_CASE_ ): snake_case__ = "bloom" snake_case__ = ["past_key_values"] snake_case__ = { "num_hidden_layers": "n_layer", "num_attention_heads": "n_head", } def __init__( self : Union[str, Any] , __SCREAMING_SNAKE_CASE : int=25_0880 , __SCREAMING_SNAKE_CASE : Dict=64 , __SCREAMING_SNAKE_CASE : Tuple=2 , __SCREAMING_SNAKE_CASE : int=8 , __SCREAMING_SNAKE_CASE : Any=1e-5 , __SCREAMING_SNAKE_CASE : Optional[Any]=0.02 , __SCREAMING_SNAKE_CASE : Union[str, Any]=True , __SCREAMING_SNAKE_CASE : int=1 , __SCREAMING_SNAKE_CASE : Any=2 , __SCREAMING_SNAKE_CASE : Optional[Any]=False , __SCREAMING_SNAKE_CASE : Optional[Any]=0.0 , __SCREAMING_SNAKE_CASE : str=0.0 , __SCREAMING_SNAKE_CASE : List[Any]=1 , __SCREAMING_SNAKE_CASE : List[str]=False , **__SCREAMING_SNAKE_CASE : str , ) -> Any: a_ : Optional[int] = vocab_size # Backward compatibility with n_embed kwarg a_ : Any = kwargs.pop('''n_embed''' , __SCREAMING_SNAKE_CASE ) a_ : Optional[int] = hidden_size if n_embed is None else n_embed a_ : int = n_layer a_ : str = n_head a_ : Optional[int] = layer_norm_epsilon a_ : Dict = initializer_range a_ : List[str] = use_cache a_ : Dict = pretraining_tp a_ : Optional[Any] = apply_residual_connection_post_layernorm a_ : Optional[Any] = hidden_dropout a_ : List[str] = attention_dropout a_ : Dict = bos_token_id a_ : Optional[int] = eos_token_id a_ : Any = slow_but_exact super().__init__(bos_token_id=__SCREAMING_SNAKE_CASE , eos_token_id=__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) class SCREAMING_SNAKE_CASE ( SCREAMING_SNAKE_CASE_ ): snake_case__ = version.parse("1.12" ) def __init__( self : Tuple , __SCREAMING_SNAKE_CASE : PretrainedConfig , __SCREAMING_SNAKE_CASE : str = "default" , __SCREAMING_SNAKE_CASE : List[PatchingSpec] = None , __SCREAMING_SNAKE_CASE : bool = False , ) -> Optional[Any]: super().__init__(__SCREAMING_SNAKE_CASE , task=__SCREAMING_SNAKE_CASE , patching_specs=__SCREAMING_SNAKE_CASE , use_past=__SCREAMING_SNAKE_CASE ) if not getattr(self._config , '''pad_token_id''' , __SCREAMING_SNAKE_CASE ): # TODO: how to do that better? a_ : Tuple = 0 @property def SCREAMING_SNAKE_CASE ( self : Tuple ) -> Mapping[str, Mapping[int, str]]: a_ : Optional[Any] = OrderedDict({'''input_ids''': {0: '''batch''', 1: '''sequence'''}} ) if self.use_past: # BLOOM stores values on dynamic axis 2. For more details see: https://github.com/huggingface/transformers/pull/18344 self.fill_with_past_key_values_(__SCREAMING_SNAKE_CASE , direction='''inputs''' , inverted_values_shape=__SCREAMING_SNAKE_CASE ) a_ : Union[str, Any] = {0: '''batch''', 1: '''past_sequence + sequence'''} else: a_ : Union[str, Any] = {0: '''batch''', 1: '''sequence'''} return common_inputs @property def SCREAMING_SNAKE_CASE ( self : Any ) -> int: return self._config.n_layer @property def SCREAMING_SNAKE_CASE ( self : int ) -> int: return self._config.n_head @property def SCREAMING_SNAKE_CASE ( self : int ) -> float: return 1e-3 def SCREAMING_SNAKE_CASE ( self : Dict , __SCREAMING_SNAKE_CASE : "PreTrainedTokenizer" , __SCREAMING_SNAKE_CASE : int = -1 , __SCREAMING_SNAKE_CASE : int = -1 , __SCREAMING_SNAKE_CASE : bool = False , __SCREAMING_SNAKE_CASE : Optional["TensorType"] = None , ) -> Mapping[str, Any]: a_ : Dict = super(__SCREAMING_SNAKE_CASE , self ).generate_dummy_inputs( __SCREAMING_SNAKE_CASE , batch_size=__SCREAMING_SNAKE_CASE , seq_length=__SCREAMING_SNAKE_CASE , is_pair=__SCREAMING_SNAKE_CASE , framework=__SCREAMING_SNAKE_CASE ) # We need to order the input in the way they appears in the forward() a_ : Union[str, Any] = 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 a_ , a_ : Any = common_inputs['''input_ids'''].shape # Not using the same length for past_key_values a_ : str = seqlen + 2 a_ : Any = self._config.hidden_size // self.num_attention_heads a_ : Optional[int] = ( batch * self.num_attention_heads, head_dim, past_key_values_length, ) a_ : Any = ( batch * self.num_attention_heads, past_key_values_length, head_dim, ) a_ : List[str] = [ (torch.zeros(__SCREAMING_SNAKE_CASE ), torch.zeros(__SCREAMING_SNAKE_CASE )) for _ in range(self.num_layers ) ] a_ : Union[str, Any] = common_inputs['''attention_mask'''] if self.use_past: a_ : Optional[int] = ordered_inputs['''attention_mask'''].dtype a_ : List[Any] = torch.cat( [ordered_inputs['''attention_mask'''], torch.ones(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , dtype=__SCREAMING_SNAKE_CASE )] , dim=1 ) return ordered_inputs @property def SCREAMING_SNAKE_CASE ( self : Optional[int] ) -> int: return 13
666
0
'''simple docstring''' import os import tempfile from functools import partial from unittest import TestCase from unittest.mock import patch import datasets import datasets.config from .utils import require_beam class SCREAMING_SNAKE_CASE ( datasets.BeamBasedBuilder ): def SCREAMING_SNAKE_CASE ( self : int ) -> Dict: return datasets.DatasetInfo( features=datasets.Features({'''content''': datasets.Value('''string''' )} ) , supervised_keys=__SCREAMING_SNAKE_CASE , ) def SCREAMING_SNAKE_CASE ( self : Optional[int] , __SCREAMING_SNAKE_CASE : int , __SCREAMING_SNAKE_CASE : int ) -> Any: return [datasets.SplitGenerator(name=datasets.Split.TRAIN , gen_kwargs={'''examples''': get_test_dummy_examples()} )] def SCREAMING_SNAKE_CASE ( self : Dict , __SCREAMING_SNAKE_CASE : Dict , __SCREAMING_SNAKE_CASE : Any ) -> Optional[Any]: import apache_beam as beam return pipeline | "Load Examples" >> beam.Create(__SCREAMING_SNAKE_CASE ) class SCREAMING_SNAKE_CASE ( datasets.BeamBasedBuilder ): def SCREAMING_SNAKE_CASE ( self : Any ) -> int: return datasets.DatasetInfo( features=datasets.Features({'''a''': datasets.Sequence({'''b''': datasets.Value('''string''' )} )} ) , supervised_keys=__SCREAMING_SNAKE_CASE , ) def SCREAMING_SNAKE_CASE ( self : Tuple , __SCREAMING_SNAKE_CASE : Dict , __SCREAMING_SNAKE_CASE : Tuple ) -> int: return [ datasets.SplitGenerator(name=datasets.Split.TRAIN , gen_kwargs={'''examples''': get_test_nested_examples()} ) ] def SCREAMING_SNAKE_CASE ( self : Optional[int] , __SCREAMING_SNAKE_CASE : int , __SCREAMING_SNAKE_CASE : int ) -> Dict: import apache_beam as beam return pipeline | "Load Examples" >> beam.Create(__SCREAMING_SNAKE_CASE ) def _UpperCAmelCase ( ): return [(i, {"content": content}) for i, content in enumerate(['''foo''', '''bar''', '''foobar'''] )] def _UpperCAmelCase ( ): return [(i, {"a": {"b": [content]}}) for i, content in enumerate(['''foo''', '''bar''', '''foobar'''] )] class SCREAMING_SNAKE_CASE ( SCREAMING_SNAKE_CASE_ ): @require_beam def SCREAMING_SNAKE_CASE ( self : str ) -> Dict: a_ : Union[str, Any] = len(get_test_dummy_examples() ) with tempfile.TemporaryDirectory() as tmp_cache_dir: a_ : Any = DummyBeamDataset(cache_dir=__SCREAMING_SNAKE_CASE , beam_runner='''DirectRunner''' ) builder.download_and_prepare() self.assertTrue( os.path.exists( os.path.join(__SCREAMING_SNAKE_CASE , builder.name , '''default''' , '''0.0.0''' , f'{builder.name}-train.arrow' ) ) ) self.assertDictEqual(builder.info.features , datasets.Features({'''content''': datasets.Value('''string''' )} ) ) a_ : List[Any] = builder.as_dataset() self.assertEqual(dset['''train'''].num_rows , __SCREAMING_SNAKE_CASE ) self.assertEqual(dset['''train'''].info.splits['''train'''].num_examples , __SCREAMING_SNAKE_CASE ) self.assertDictEqual(dset['''train'''][0] , get_test_dummy_examples()[0][1] ) self.assertDictEqual( dset['''train'''][expected_num_examples - 1] , get_test_dummy_examples()[expected_num_examples - 1][1] ) self.assertTrue( os.path.exists(os.path.join(__SCREAMING_SNAKE_CASE , builder.name , '''default''' , '''0.0.0''' , '''dataset_info.json''' ) ) ) del dset @require_beam def SCREAMING_SNAKE_CASE ( self : Optional[Any] ) -> Any: import apache_beam as beam a_ : str = beam.io.parquetio.WriteToParquet a_ : Dict = len(get_test_dummy_examples() ) with tempfile.TemporaryDirectory() as tmp_cache_dir: a_ : Union[str, Any] = DummyBeamDataset(cache_dir=__SCREAMING_SNAKE_CASE , beam_runner='''DirectRunner''' ) with patch('''apache_beam.io.parquetio.WriteToParquet''' ) as write_parquet_mock: a_ : Optional[Any] = partial(__SCREAMING_SNAKE_CASE , num_shards=2 ) builder.download_and_prepare() self.assertTrue( os.path.exists( os.path.join( __SCREAMING_SNAKE_CASE , builder.name , '''default''' , '''0.0.0''' , f'{builder.name}-train-00000-of-00002.arrow' ) ) ) self.assertTrue( os.path.exists( os.path.join( __SCREAMING_SNAKE_CASE , builder.name , '''default''' , '''0.0.0''' , f'{builder.name}-train-00000-of-00002.arrow' ) ) ) self.assertDictEqual(builder.info.features , datasets.Features({'''content''': datasets.Value('''string''' )} ) ) a_ : Union[str, Any] = builder.as_dataset() self.assertEqual(dset['''train'''].num_rows , __SCREAMING_SNAKE_CASE ) self.assertEqual(dset['''train'''].info.splits['''train'''].num_examples , __SCREAMING_SNAKE_CASE ) # Order is not preserved when sharding, so we just check that all the elements are there self.assertListEqual(sorted(dset['''train''']['''content'''] ) , sorted(['''foo''', '''bar''', '''foobar'''] ) ) self.assertTrue( os.path.exists(os.path.join(__SCREAMING_SNAKE_CASE , builder.name , '''default''' , '''0.0.0''' , '''dataset_info.json''' ) ) ) del dset @require_beam def SCREAMING_SNAKE_CASE ( self : List[str] ) -> Dict: with tempfile.TemporaryDirectory() as tmp_cache_dir: a_ : int = DummyBeamDataset(cache_dir=__SCREAMING_SNAKE_CASE ) self.assertRaises(datasets.builder.MissingBeamOptions , builder.download_and_prepare ) @require_beam def SCREAMING_SNAKE_CASE ( self : List[str] ) -> Optional[int]: a_ : Union[str, Any] = len(get_test_nested_examples() ) with tempfile.TemporaryDirectory() as tmp_cache_dir: a_ : int = NestedBeamDataset(cache_dir=__SCREAMING_SNAKE_CASE , beam_runner='''DirectRunner''' ) builder.download_and_prepare() self.assertTrue( os.path.exists( os.path.join(__SCREAMING_SNAKE_CASE , builder.name , '''default''' , '''0.0.0''' , f'{builder.name}-train.arrow' ) ) ) self.assertDictEqual( builder.info.features , datasets.Features({'''a''': datasets.Sequence({'''b''': datasets.Value('''string''' )} )} ) ) a_ : List[str] = builder.as_dataset() self.assertEqual(dset['''train'''].num_rows , __SCREAMING_SNAKE_CASE ) self.assertEqual(dset['''train'''].info.splits['''train'''].num_examples , __SCREAMING_SNAKE_CASE ) self.assertDictEqual(dset['''train'''][0] , get_test_nested_examples()[0][1] ) self.assertDictEqual( dset['''train'''][expected_num_examples - 1] , get_test_nested_examples()[expected_num_examples - 1][1] ) self.assertTrue( os.path.exists(os.path.join(__SCREAMING_SNAKE_CASE , builder.name , '''default''' , '''0.0.0''' , '''dataset_info.json''' ) ) ) del dset
704
'''simple docstring''' import sys __lowerCAmelCase = ( '73167176531330624919225119674426574742355349194934' '96983520312774506326239578318016984801869478851843' '85861560789112949495459501737958331952853208805511' '12540698747158523863050715693290963295227443043557' '66896648950445244523161731856403098711121722383113' '62229893423380308135336276614282806444486645238749' '30358907296290491560440772390713810515859307960866' '70172427121883998797908792274921901699720888093776' '65727333001053367881220235421809751254540594752243' '52584907711670556013604839586446706324415722155397' '53697817977846174064955149290862569321978468622482' '83972241375657056057490261407972968652414535100474' '82166370484403199890008895243450658541227588666881' '16427171479924442928230863465674813919123162824586' '17866458359124566529476545682848912883142607690042' '24219022671055626321111109370544217506941658960408' '07198403850962455444362981230987879927244284909188' '84580156166097919133875499200524063689912560717606' '05886116467109405077541002256983155200055935729725' '71636269561882670428252483600823257530420752963450' ) def _UpperCAmelCase ( __A : str ): a_ : Tuple = 1 for digit in s: product *= int(__A ) return product def _UpperCAmelCase ( __A : str = N ): a_ : Dict = -sys.maxsize - 1 a_ : Optional[int] = n[:13] a_ : str = 13 while cur_index < len(__A ) - 13: if int(n[cur_index] ) >= int(substr[0] ): a_ : Tuple = substr[1:] + n[cur_index] cur_index += 1 else: a_ : Dict = max(__A , str_eval(__A ) ) a_ : List[str] = n[cur_index : cur_index + 13] cur_index += 13 return largest_product if __name__ == "__main__": print(F"""{solution() = }""")
666
0
'''simple docstring''' import warnings from ...utils import logging from .image_processing_layoutlmva import LayoutLMvaImageProcessor __lowerCAmelCase = logging.get_logger(__name__) class SCREAMING_SNAKE_CASE ( SCREAMING_SNAKE_CASE_ ): def __init__( self : Dict , *__SCREAMING_SNAKE_CASE : Optional[Any] , **__SCREAMING_SNAKE_CASE : int ) -> None: warnings.warn( '''The class LayoutLMv2FeatureExtractor is deprecated and will be removed in version 5 of Transformers.''' ''' Please use LayoutLMv2ImageProcessor instead.''' , __SCREAMING_SNAKE_CASE , ) super().__init__(*__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE )
705
'''simple docstring''' from __future__ import annotations def _UpperCAmelCase ( __A : list[int] ): a_ : int = len(__A ) // 2 # choose the middle 3 elements a_ : Dict = lst[m - 1 : m + 2] # if middle element is peak if three[1] > three[0] and three[1] > three[2]: return three[1] # if increasing, recurse on right elif three[0] < three[2]: if len(lst[:m] ) == 2: m -= 1 return peak(lst[m:] ) # decreasing else: if len(lst[:m] ) == 2: m += 1 return peak(lst[:m] ) if __name__ == "__main__": import doctest doctest.testmod()
666
0
'''simple docstring''' import argparse import json import requests import torch from huggingface_hub import hf_hub_download from PIL import Image from torchvision import transforms from transformers import BitImageProcessor, FocalNetConfig, FocalNetForImageClassification from transformers.image_utils import IMAGENET_DEFAULT_MEAN, IMAGENET_DEFAULT_STD, PILImageResampling def _UpperCAmelCase ( __A : int ): a_ : Any = [2, 2, 6, 2] if '''tiny''' in model_name else [2, 2, 18, 2] a_ : List[Any] = True if '''large''' in model_name or '''huge''' in model_name else False a_ : str = True if '''large''' in model_name or '''huge''' in model_name else False a_ : Optional[Any] = True if '''large''' in model_name or '''huge''' in model_name else False if "large" in model_name or "xlarge" in model_name or "huge" in model_name: if "fl3" in model_name: a_ : Any = [3, 3, 3, 3] a_ : Union[str, Any] = [5, 5, 5, 5] elif "fl4" in model_name: a_ : Optional[Any] = [4, 4, 4, 4] a_ : List[Any] = [3, 3, 3, 3] if "tiny" in model_name or "small" in model_name or "base" in model_name: a_ : List[Any] = [3, 3, 3, 3] if "lrf" in model_name: a_ : List[str] = [3, 3, 3, 3] else: a_ : int = [2, 2, 2, 2] if "tiny" in model_name: a_ : Any = 96 elif "small" in model_name: a_ : int = 96 elif "base" in model_name: a_ : Any = 1_28 elif "large" in model_name: a_ : Union[str, Any] = 1_92 elif "xlarge" in model_name: a_ : Optional[Any] = 2_56 elif "huge" in model_name: a_ : Optional[int] = 3_52 # set label information a_ : Any = '''huggingface/label-files''' if "large" in model_name or "huge" in model_name: a_ : Tuple = '''imagenet-22k-id2label.json''' else: a_ : Dict = '''imagenet-1k-id2label.json''' a_ : Optional[int] = json.load(open(hf_hub_download(__A , __A , repo_type='''dataset''' ) , '''r''' ) ) a_ : Optional[Any] = {int(__A ): v for k, v in idalabel.items()} a_ : Union[str, Any] = {v: k for k, v in idalabel.items()} a_ : List[str] = FocalNetConfig( embed_dim=__A , depths=__A , focal_levels=__A , focal_windows=__A , use_conv_embed=__A , idalabel=__A , labelaid=__A , use_post_layernorm=__A , use_layerscale=__A , ) return config def _UpperCAmelCase ( __A : Tuple ): if "patch_embed.proj" in name: a_ : Dict = name.replace('''patch_embed.proj''' , '''embeddings.patch_embeddings.projection''' ) if "patch_embed.norm" in name: a_ : Optional[int] = name.replace('''patch_embed.norm''' , '''embeddings.norm''' ) if "layers" in name: a_ : Tuple = '''encoder.''' + name if "encoder.layers" in name: a_ : str = name.replace('''encoder.layers''' , '''encoder.stages''' ) if "downsample.proj" in name: a_ : Optional[Any] = name.replace('''downsample.proj''' , '''downsample.projection''' ) if "blocks" in name: a_ : Optional[Any] = name.replace('''blocks''' , '''layers''' ) if "modulation.f.weight" in name or "modulation.f.bias" in name: a_ : Dict = name.replace('''modulation.f''' , '''modulation.projection_in''' ) if "modulation.h.weight" in name or "modulation.h.bias" in name: a_ : Optional[Any] = name.replace('''modulation.h''' , '''modulation.projection_context''' ) if "modulation.proj.weight" in name or "modulation.proj.bias" in name: a_ : int = name.replace('''modulation.proj''' , '''modulation.projection_out''' ) if name == "norm.weight": a_ : Dict = '''layernorm.weight''' if name == "norm.bias": a_ : List[str] = '''layernorm.bias''' if "head" in name: a_ : List[Any] = name.replace('''head''' , '''classifier''' ) else: a_ : Tuple = '''focalnet.''' + name return name def _UpperCAmelCase ( __A : List[Any] , __A : Optional[Any] , __A : str=False ): # fmt: off a_ : Tuple = { '''focalnet-tiny''': '''https://projects4jw.blob.core.windows.net/focalnet/release/classification/focalnet_tiny_srf.pth''', '''focalnet-tiny-lrf''': '''https://projects4jw.blob.core.windows.net/focalnet/release/classification/focalnet_tiny_lrf.pth''', '''focalnet-small''': '''https://projects4jw.blob.core.windows.net/focalnet/release/classification/focalnet_small_srf.pth''', '''focalnet-small-lrf''': '''https://projects4jw.blob.core.windows.net/focalnet/release/classification/focalnet_small_lrf.pth''', '''focalnet-base''': '''https://projects4jw.blob.core.windows.net/focalnet/release/classification/focalnet_base_srf.pth''', '''focalnet-base-lrf''': '''https://projects4jw.blob.core.windows.net/focalnet/release/classification/focalnet_base_lrf.pth''', '''focalnet-large-lrf-fl3''': '''https://projects4jw.blob.core.windows.net/focalnet/release/classification/focalnet_large_lrf_384.pth''', '''focalnet-large-lrf-fl4''': '''https://projects4jw.blob.core.windows.net/focalnet/release/classification/focalnet_large_lrf_384_fl4.pth''', '''focalnet-xlarge-lrf-fl3''': '''https://projects4jw.blob.core.windows.net/focalnet/release/classification/focalnet_xlarge_lrf_384.pth''', '''focalnet-xlarge-lrf-fl4''': '''https://projects4jw.blob.core.windows.net/focalnet/release/classification/focalnet_xlarge_lrf_384_fl4.pth''', } # fmt: on a_ : Any = model_name_to_url[model_name] print('''Checkpoint URL: ''' , __A ) a_ : Optional[Any] = torch.hub.load_state_dict_from_url(__A , map_location='''cpu''' )['''model'''] # rename keys for key in state_dict.copy().keys(): a_ : Union[str, Any] = state_dict.pop(__A ) a_ : Optional[int] = val a_ : Tuple = get_focalnet_config(__A ) a_ : str = FocalNetForImageClassification(__A ) model.eval() # load state dict model.load_state_dict(__A ) # verify conversion a_ : str = '''http://images.cocodataset.org/val2017/000000039769.jpg''' a_ : Any = BitImageProcessor( do_resize=__A , size={'''shortest_edge''': 2_56} , resample=PILImageResampling.BILINEAR , do_center_crop=__A , crop_size=2_24 , do_normalize=__A , image_mean=__A , image_std=__A , ) a_ : Optional[int] = Image.open(requests.get(__A , stream=__A ).raw ) a_ : Optional[Any] = processor(images=__A , return_tensors='''pt''' ) a_ : Union[str, Any] = transforms.Compose( [ transforms.Resize(2_56 ), transforms.CenterCrop(2_24 ), transforms.ToTensor(), transforms.Normalize(mean=[0.485, 0.456, 0.406] , std=[0.229, 0.224, 0.225] ), ] ) a_ : int = image_transforms(__A ).unsqueeze(0 ) # verify pixel_values assert torch.allclose(inputs.pixel_values , __A , atol=1E-4 ) a_ : List[str] = model(**__A ) a_ : Optional[Any] = outputs.logits.argmax(-1 ).item() print('''Predicted class:''' , model.config.idalabel[predicted_class_idx] ) print('''First values of logits:''' , outputs.logits[0, :3] ) if model_name == "focalnet-tiny": a_ : Union[str, Any] = torch.tensor([0.2166, -0.4368, 0.2191] ) elif model_name == "focalnet-tiny-lrf": a_ : List[Any] = torch.tensor([1.1669, 0.0125, -0.1695] ) elif model_name == "focalnet-small": a_ : List[Any] = torch.tensor([0.4917, -0.0430, 0.1341] ) elif model_name == "focalnet-small-lrf": a_ : Optional[Any] = torch.tensor([-0.2588, -0.5342, -0.2331] ) elif model_name == "focalnet-base": a_ : Any = torch.tensor([-0.1655, -0.4090, -0.1730] ) elif model_name == "focalnet-base-lrf": a_ : Any = torch.tensor([0.5306, -0.0483, -0.3928] ) assert torch.allclose(outputs.logits[0, :3] , __A , atol=1E-4 ) print('''Looks ok!''' ) if pytorch_dump_folder_path is not None: print(f'Saving model and processor of {model_name} to {pytorch_dump_folder_path}' ) model.save_pretrained(__A ) processor.save_pretrained(__A ) if push_to_hub: print(f'Pushing model and processor of {model_name} to the hub...' ) model.push_to_hub(f'{model_name}' ) processor.push_to_hub(f'{model_name}' ) if __name__ == "__main__": __lowerCAmelCase = argparse.ArgumentParser() # Required parameters parser.add_argument( '''--model_name''', default='''focalnet-tiny''', type=str, help='''Name of the FocalNet model you\'d like to convert.''', ) parser.add_argument( '''--pytorch_dump_folder_path''', default=None, type=str, help='''Path to the output PyTorch model directory.''' ) parser.add_argument( '''--push_to_hub''', action='''store_true''', help='''Whether to push the model and processor to the hub.''', ) __lowerCAmelCase = parser.parse_args() convert_focalnet_checkpoint(args.model_name, args.pytorch_dump_folder_path, args.push_to_hub)
706
'''simple docstring''' import itertools import json import os import unittest from transformers import AddedToken, LongformerTokenizer, LongformerTokenizerFast from transformers.models.longformer.tokenization_longformer import VOCAB_FILES_NAMES from transformers.testing_utils import require_tokenizers, slow from ...test_tokenization_common import TokenizerTesterMixin @require_tokenizers class SCREAMING_SNAKE_CASE ( SCREAMING_SNAKE_CASE_ , unittest.TestCase ): snake_case__ = LongformerTokenizer snake_case__ = True snake_case__ = LongformerTokenizerFast snake_case__ = True def SCREAMING_SNAKE_CASE ( self : Optional[Any] ) -> List[str]: super().setUp() # Adapted from Sennrich et al. 2015 and https://github.com/rsennrich/subword-nmt a_ : Tuple = [ '''l''', '''o''', '''w''', '''e''', '''r''', '''s''', '''t''', '''i''', '''d''', '''n''', '''\u0120''', '''\u0120l''', '''\u0120n''', '''\u0120lo''', '''\u0120low''', '''er''', '''\u0120lowest''', '''\u0120newer''', '''\u0120wider''', '''<unk>''', ] a_ : Optional[Any] = dict(zip(__SCREAMING_SNAKE_CASE , range(len(__SCREAMING_SNAKE_CASE ) ) ) ) a_ : Union[str, Any] = ['''#version: 0.2''', '''\u0120 l''', '''\u0120l o''', '''\u0120lo w''', '''e r''', ''''''] a_ : Any = {'''unk_token''': '''<unk>'''} a_ : Union[str, Any] = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES['''vocab_file'''] ) a_ : Optional[int] = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES['''merges_file'''] ) with open(self.vocab_file , '''w''' , encoding='''utf-8''' ) as fp: fp.write(json.dumps(__SCREAMING_SNAKE_CASE ) + '''\n''' ) with open(self.merges_file , '''w''' , encoding='''utf-8''' ) as fp: fp.write('''\n'''.join(__SCREAMING_SNAKE_CASE ) ) def SCREAMING_SNAKE_CASE ( self : Any , **__SCREAMING_SNAKE_CASE : Any ) -> int: kwargs.update(self.special_tokens_map ) return self.tokenizer_class.from_pretrained(self.tmpdirname , **__SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE ( self : Union[str, Any] , **__SCREAMING_SNAKE_CASE : List[Any] ) -> List[str]: kwargs.update(self.special_tokens_map ) return self.rust_tokenizer_class.from_pretrained(self.tmpdirname , **__SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE ( self : Dict , __SCREAMING_SNAKE_CASE : List[Any] ) -> Any: a_ : Union[str, Any] = '''lower newer''' a_ : List[Any] = '''lower newer''' return input_text, output_text def SCREAMING_SNAKE_CASE ( self : Optional[Any] ) -> List[str]: a_ : Optional[Any] = self.tokenizer_class(self.vocab_file , self.merges_file , **self.special_tokens_map ) a_ : List[str] = '''lower newer''' a_ : str = ['''l''', '''o''', '''w''', '''er''', '''\u0120''', '''n''', '''e''', '''w''', '''er'''] a_ : Optional[int] = tokenizer.tokenize(__SCREAMING_SNAKE_CASE ) # , add_prefix_space=True) self.assertListEqual(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) a_ : Dict = tokens + [tokenizer.unk_token] a_ : Any = [0, 1, 2, 15, 10, 9, 3, 2, 15, 19] self.assertListEqual(tokenizer.convert_tokens_to_ids(__SCREAMING_SNAKE_CASE ) , __SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE ( self : List[str] ) -> Optional[Any]: a_ : Union[str, Any] = self.get_tokenizer() self.assertListEqual(tokenizer.encode('''Hello world!''' , add_special_tokens=__SCREAMING_SNAKE_CASE ) , [0, 3_1414, 232, 328, 2] ) self.assertListEqual( tokenizer.encode('''Hello world! cรฉcรฉ herlolip 418''' , add_special_tokens=__SCREAMING_SNAKE_CASE ) , [0, 3_1414, 232, 328, 740, 1140, 1_2695, 69, 4_6078, 1588, 2] , ) @slow def SCREAMING_SNAKE_CASE ( self : int ) -> Optional[Any]: a_ : Dict = self.tokenizer_class.from_pretrained('''allenai/longformer-base-4096''' ) a_ : Tuple = tokenizer.encode('''sequence builders''' , add_special_tokens=__SCREAMING_SNAKE_CASE ) a_ : Union[str, Any] = tokenizer.encode('''multi-sequence build''' , add_special_tokens=__SCREAMING_SNAKE_CASE ) a_ : Any = tokenizer.encode( '''sequence builders''' , add_special_tokens=__SCREAMING_SNAKE_CASE , add_prefix_space=__SCREAMING_SNAKE_CASE ) a_ : Any = tokenizer.encode( '''sequence builders''' , '''multi-sequence build''' , add_special_tokens=__SCREAMING_SNAKE_CASE , add_prefix_space=__SCREAMING_SNAKE_CASE ) a_ : List[str] = tokenizer.build_inputs_with_special_tokens(__SCREAMING_SNAKE_CASE ) a_ : Union[str, Any] = tokenizer.build_inputs_with_special_tokens(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) assert encoded_sentence == encoded_text_from_decode assert encoded_pair == encoded_pair_from_decode def SCREAMING_SNAKE_CASE ( self : str ) -> int: a_ : str = self.get_tokenizer() a_ : int = '''Encode this sequence.''' a_ : List[str] = tokenizer.byte_encoder[''' '''.encode('''utf-8''' )[0]] # Testing encoder arguments a_ : Dict = tokenizer.encode(__SCREAMING_SNAKE_CASE , add_special_tokens=__SCREAMING_SNAKE_CASE , add_prefix_space=__SCREAMING_SNAKE_CASE ) a_ : Optional[Any] = tokenizer.convert_ids_to_tokens(encoded[0] )[0] self.assertNotEqual(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) a_ : Dict = tokenizer.encode(__SCREAMING_SNAKE_CASE , add_special_tokens=__SCREAMING_SNAKE_CASE , add_prefix_space=__SCREAMING_SNAKE_CASE ) a_ : Any = tokenizer.convert_ids_to_tokens(encoded[0] )[0] self.assertEqual(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) tokenizer.add_special_tokens({'''bos_token''': '''<s>'''} ) a_ : Dict = tokenizer.encode(__SCREAMING_SNAKE_CASE , add_special_tokens=__SCREAMING_SNAKE_CASE ) a_ : Dict = tokenizer.convert_ids_to_tokens(encoded[1] )[0] self.assertNotEqual(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) # Testing spaces after special tokens a_ : Optional[Any] = '''<mask>''' tokenizer.add_special_tokens( {'''mask_token''': AddedToken(__SCREAMING_SNAKE_CASE , lstrip=__SCREAMING_SNAKE_CASE , rstrip=__SCREAMING_SNAKE_CASE )} ) # mask token has a left space a_ : Optional[int] = tokenizer.convert_tokens_to_ids(__SCREAMING_SNAKE_CASE ) a_ : List[Any] = '''Encode <mask> sequence''' a_ : List[str] = '''Encode <mask>sequence''' a_ : int = tokenizer.encode(__SCREAMING_SNAKE_CASE ) a_ : Optional[int] = encoded.index(__SCREAMING_SNAKE_CASE ) a_ : Optional[int] = tokenizer.convert_ids_to_tokens(encoded[mask_loc + 1] )[0] self.assertEqual(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) a_ : Union[str, Any] = tokenizer.encode(__SCREAMING_SNAKE_CASE ) a_ : Optional[int] = encoded.index(__SCREAMING_SNAKE_CASE ) a_ : str = tokenizer.convert_ids_to_tokens(encoded[mask_loc + 1] )[0] self.assertNotEqual(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE ( self : Optional[int] ) -> Union[str, Any]: pass def SCREAMING_SNAKE_CASE ( self : Optional[Any] ) -> Any: for tokenizer, pretrained_name, kwargs in self.tokenizers_list: with self.subTest(f'{tokenizer.__class__.__name__} ({pretrained_name})' ): a_ : Any = self.rust_tokenizer_class.from_pretrained(__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) a_ : Any = self.tokenizer_class.from_pretrained(__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) a_ : str = '''A, <mask> AllenNLP sentence.''' a_ : List[Any] = tokenizer_r.encode_plus(__SCREAMING_SNAKE_CASE , add_special_tokens=__SCREAMING_SNAKE_CASE , return_token_type_ids=__SCREAMING_SNAKE_CASE ) a_ : Dict = tokenizer_p.encode_plus(__SCREAMING_SNAKE_CASE , add_special_tokens=__SCREAMING_SNAKE_CASE , return_token_type_ids=__SCREAMING_SNAKE_CASE ) # token_type_ids should put 0 everywhere self.assertEqual(sum(tokens_r['''token_type_ids'''] ) , sum(tokens_p['''token_type_ids'''] ) ) # attention_mask should put 1 everywhere, so sum over length should be 1 self.assertEqual( sum(tokens_r['''attention_mask'''] ) / len(tokens_r['''attention_mask'''] ) , sum(tokens_p['''attention_mask'''] ) / len(tokens_p['''attention_mask'''] ) , ) a_ : str = tokenizer_r.convert_ids_to_tokens(tokens_r['''input_ids'''] ) a_ : Tuple = tokenizer_p.convert_ids_to_tokens(tokens_p['''input_ids'''] ) # Rust correctly handles the space before the mask while python doesnt self.assertSequenceEqual(tokens_p['''input_ids'''] , [0, 250, 6, 5_0264, 3823, 487, 2_1992, 3645, 4, 2] ) self.assertSequenceEqual(tokens_r['''input_ids'''] , [0, 250, 6, 5_0264, 3823, 487, 2_1992, 3645, 4, 2] ) self.assertSequenceEqual( __SCREAMING_SNAKE_CASE , ['''<s>''', '''A''', ''',''', '''<mask>''', '''ฤ Allen''', '''N''', '''LP''', '''ฤ sentence''', '''.''', '''</s>'''] ) self.assertSequenceEqual( __SCREAMING_SNAKE_CASE , ['''<s>''', '''A''', ''',''', '''<mask>''', '''ฤ Allen''', '''N''', '''LP''', '''ฤ sentence''', '''.''', '''</s>'''] ) def SCREAMING_SNAKE_CASE ( self : Optional[Any] ) -> List[Any]: for trim_offsets, add_prefix_space in itertools.product([True, False] , repeat=2 ): a_ : Any = self.rust_tokenizer_class.from_pretrained( self.tmpdirname , use_fast=__SCREAMING_SNAKE_CASE , add_prefix_space=__SCREAMING_SNAKE_CASE , trim_offsets=__SCREAMING_SNAKE_CASE ) a_ : Union[str, Any] = json.loads(tokenizer_r.backend_tokenizer.pre_tokenizer.__getstate__() ) a_ : str = json.loads(tokenizer_r.backend_tokenizer.post_processor.__getstate__() ) self.assertEqual(pre_tokenizer_state['''add_prefix_space'''] , __SCREAMING_SNAKE_CASE ) self.assertEqual(post_processor_state['''add_prefix_space'''] , __SCREAMING_SNAKE_CASE ) self.assertEqual(post_processor_state['''trim_offsets'''] , __SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE ( self : int ) -> Optional[int]: # Test which aims to verify that the offsets are well adapted to the argument `add_prefix_space` and # `trim_offsets` for tokenizer, pretrained_name, kwargs in self.tokenizers_list: with self.subTest(f'{tokenizer.__class__.__name__} ({pretrained_name})' ): a_ : Dict = '''hello''' # `hello` is a token in the vocabulary of `pretrained_name` a_ : Union[str, Any] = f'{text_of_1_token} {text_of_1_token}' a_ : Any = self.rust_tokenizer_class.from_pretrained( __SCREAMING_SNAKE_CASE , use_fast=__SCREAMING_SNAKE_CASE , add_prefix_space=__SCREAMING_SNAKE_CASE , trim_offsets=__SCREAMING_SNAKE_CASE ) a_ : Optional[int] = tokenizer_r(__SCREAMING_SNAKE_CASE , return_offsets_mapping=__SCREAMING_SNAKE_CASE , add_special_tokens=__SCREAMING_SNAKE_CASE ) self.assertEqual(encoding.offset_mapping[0] , (0, len(__SCREAMING_SNAKE_CASE )) ) self.assertEqual( encoding.offset_mapping[1] , (len(__SCREAMING_SNAKE_CASE ) + 1, len(__SCREAMING_SNAKE_CASE ) + 1 + len(__SCREAMING_SNAKE_CASE )) , ) a_ : Any = self.rust_tokenizer_class.from_pretrained( __SCREAMING_SNAKE_CASE , use_fast=__SCREAMING_SNAKE_CASE , add_prefix_space=__SCREAMING_SNAKE_CASE , trim_offsets=__SCREAMING_SNAKE_CASE ) a_ : str = tokenizer_r(__SCREAMING_SNAKE_CASE , return_offsets_mapping=__SCREAMING_SNAKE_CASE , add_special_tokens=__SCREAMING_SNAKE_CASE ) self.assertEqual(encoding.offset_mapping[0] , (0, len(__SCREAMING_SNAKE_CASE )) ) self.assertEqual( encoding.offset_mapping[1] , (len(__SCREAMING_SNAKE_CASE ) + 1, len(__SCREAMING_SNAKE_CASE ) + 1 + len(__SCREAMING_SNAKE_CASE )) , ) a_ : int = self.rust_tokenizer_class.from_pretrained( __SCREAMING_SNAKE_CASE , use_fast=__SCREAMING_SNAKE_CASE , add_prefix_space=__SCREAMING_SNAKE_CASE , trim_offsets=__SCREAMING_SNAKE_CASE ) a_ : Union[str, Any] = tokenizer_r(__SCREAMING_SNAKE_CASE , return_offsets_mapping=__SCREAMING_SNAKE_CASE , add_special_tokens=__SCREAMING_SNAKE_CASE ) self.assertEqual(encoding.offset_mapping[0] , (0, len(__SCREAMING_SNAKE_CASE )) ) self.assertEqual( encoding.offset_mapping[1] , (len(__SCREAMING_SNAKE_CASE ), len(__SCREAMING_SNAKE_CASE ) + 1 + len(__SCREAMING_SNAKE_CASE )) , ) a_ : Tuple = self.rust_tokenizer_class.from_pretrained( __SCREAMING_SNAKE_CASE , use_fast=__SCREAMING_SNAKE_CASE , add_prefix_space=__SCREAMING_SNAKE_CASE , trim_offsets=__SCREAMING_SNAKE_CASE ) a_ : Any = tokenizer_r(__SCREAMING_SNAKE_CASE , return_offsets_mapping=__SCREAMING_SNAKE_CASE , add_special_tokens=__SCREAMING_SNAKE_CASE ) self.assertEqual(encoding.offset_mapping[0] , (0, len(__SCREAMING_SNAKE_CASE )) ) self.assertEqual( encoding.offset_mapping[1] , (len(__SCREAMING_SNAKE_CASE ), len(__SCREAMING_SNAKE_CASE ) + 1 + len(__SCREAMING_SNAKE_CASE )) , ) a_ : Union[str, Any] = f' {text}' # tokenizer_r = self.rust_tokenizer_class.from_pretrained( # pretrained_name, use_fast=True, add_prefix_space=True, trim_offsets=True # ) # encoding = tokenizer_r(text, return_offsets_mapping=True, add_special_tokens=False) # self.assertEqual(encoding.offset_mapping[0], (1, 1 + len(text_of_1_token))) # self.assertEqual( # encoding.offset_mapping[1], # (1 + len(text_of_1_token) + 1, 1 + len(text_of_1_token) + 1 + len(text_of_1_token)), # ) a_ : str = self.rust_tokenizer_class.from_pretrained( __SCREAMING_SNAKE_CASE , use_fast=__SCREAMING_SNAKE_CASE , add_prefix_space=__SCREAMING_SNAKE_CASE , trim_offsets=__SCREAMING_SNAKE_CASE ) a_ : int = tokenizer_r(__SCREAMING_SNAKE_CASE , return_offsets_mapping=__SCREAMING_SNAKE_CASE , add_special_tokens=__SCREAMING_SNAKE_CASE ) self.assertEqual(encoding.offset_mapping[0] , (1, 1 + len(__SCREAMING_SNAKE_CASE )) ) self.assertEqual( encoding.offset_mapping[1] , (1 + len(__SCREAMING_SNAKE_CASE ) + 1, 1 + len(__SCREAMING_SNAKE_CASE ) + 1 + len(__SCREAMING_SNAKE_CASE )) , ) a_ : int = self.rust_tokenizer_class.from_pretrained( __SCREAMING_SNAKE_CASE , use_fast=__SCREAMING_SNAKE_CASE , add_prefix_space=__SCREAMING_SNAKE_CASE , trim_offsets=__SCREAMING_SNAKE_CASE ) a_ : str = tokenizer_r(__SCREAMING_SNAKE_CASE , return_offsets_mapping=__SCREAMING_SNAKE_CASE , add_special_tokens=__SCREAMING_SNAKE_CASE ) self.assertEqual(encoding.offset_mapping[0] , (0, 1 + len(__SCREAMING_SNAKE_CASE )) ) self.assertEqual( encoding.offset_mapping[1] , (1 + len(__SCREAMING_SNAKE_CASE ), 1 + len(__SCREAMING_SNAKE_CASE ) + 1 + len(__SCREAMING_SNAKE_CASE )) , ) a_ : Optional[int] = self.rust_tokenizer_class.from_pretrained( __SCREAMING_SNAKE_CASE , use_fast=__SCREAMING_SNAKE_CASE , add_prefix_space=__SCREAMING_SNAKE_CASE , trim_offsets=__SCREAMING_SNAKE_CASE ) a_ : int = tokenizer_r(__SCREAMING_SNAKE_CASE , return_offsets_mapping=__SCREAMING_SNAKE_CASE , add_special_tokens=__SCREAMING_SNAKE_CASE ) self.assertEqual(encoding.offset_mapping[0] , (0, 1 + len(__SCREAMING_SNAKE_CASE )) ) self.assertEqual( encoding.offset_mapping[1] , (1 + len(__SCREAMING_SNAKE_CASE ), 1 + len(__SCREAMING_SNAKE_CASE ) + 1 + len(__SCREAMING_SNAKE_CASE )) , )
666
0
'''simple docstring''' import math def _UpperCAmelCase ( __A : list , __A : int = 0 , __A : int = 0 ): a_ : Dict = end or len(__A ) for i in range(__A , __A ): a_ : Union[str, Any] = i a_ : List[str] = array[i] while temp_index != start and temp_index_value < array[temp_index - 1]: a_ : Tuple = array[temp_index - 1] temp_index -= 1 a_ : Optional[Any] = temp_index_value return array def _UpperCAmelCase ( __A : list , __A : int , __A : int ): # Max Heap a_ : int = index a_ : str = 2 * index + 1 # Left Node a_ : str = 2 * index + 2 # Right Node if left_index < heap_size and array[largest] < array[left_index]: a_ : Optional[int] = left_index if right_index < heap_size and array[largest] < array[right_index]: a_ : Any = right_index if largest != index: a_ : Tuple = array[largest], array[index] heapify(__A , __A , __A ) def _UpperCAmelCase ( __A : list ): a_ : Union[str, Any] = len(__A ) for i in range(n // 2 , -1 , -1 ): heapify(__A , __A , __A ) for i in range(n - 1 , 0 , -1 ): a_ : Any = array[0], array[i] heapify(__A , 0 , __A ) return array def _UpperCAmelCase ( __A : list , __A : int , __A : int , __A : int ): if (array[first_index] > array[middle_index]) != ( array[first_index] > array[last_index] ): return array[first_index] elif (array[middle_index] > array[first_index]) != ( array[middle_index] > array[last_index] ): return array[middle_index] else: return array[last_index] def _UpperCAmelCase ( __A : list , __A : int , __A : int , __A : int ): a_ : str = low a_ : Tuple = high while True: while array[i] < pivot: i += 1 j -= 1 while pivot < array[j]: j -= 1 if i >= j: return i a_ : Union[str, Any] = array[j], array[i] i += 1 def _UpperCAmelCase ( __A : list ): if len(__A ) == 0: return array a_ : List[str] = 2 * math.ceil(math.loga(len(__A ) ) ) a_ : Optional[Any] = 16 return intro_sort(__A , 0 , len(__A ) , __A , __A ) def _UpperCAmelCase ( __A : list , __A : int , __A : int , __A : int , __A : int ): while end - start > size_threshold: if max_depth == 0: return heap_sort(__A ) max_depth -= 1 a_ : Optional[int] = median_of_a(__A , __A , start + ((end - start) // 2) + 1 , end - 1 ) a_ : List[str] = partition(__A , __A , __A , __A ) intro_sort(__A , __A , __A , __A , __A ) a_ : Optional[Any] = p return insertion_sort(__A , __A , __A ) if __name__ == "__main__": import doctest doctest.testmod() __lowerCAmelCase = input('Enter numbers separated by a comma : ').strip() __lowerCAmelCase = [float(item) for item in user_input.split(',')] print(sort(unsorted))
707
'''simple docstring''' import os from shutil import copyfile from typing import Any, Dict, List, Optional, Tuple import sentencepiece as spm from ...tokenization_utils import AddedToken, PreTrainedTokenizer from ...utils import logging __lowerCAmelCase = logging.get_logger(__name__) __lowerCAmelCase = {'vocab_file': 'sentencepiece.bpe.model'} __lowerCAmelCase = { 'vocab_file': { 'moussaKam/mbarthez': 'https://huggingface.co/moussaKam/mbarthez/resolve/main/sentencepiece.bpe.model', 'moussaKam/barthez': 'https://huggingface.co/moussaKam/barthez/resolve/main/sentencepiece.bpe.model', 'moussaKam/barthez-orangesum-title': ( 'https://huggingface.co/moussaKam/barthez-orangesum-title/resolve/main/sentencepiece.bpe.model' ), }, } __lowerCAmelCase = { 'moussaKam/mbarthez': 1_024, 'moussaKam/barthez': 1_024, 'moussaKam/barthez-orangesum-title': 1_024, } __lowerCAmelCase = 'โ–' class SCREAMING_SNAKE_CASE ( SCREAMING_SNAKE_CASE_ ): snake_case__ = VOCAB_FILES_NAMES snake_case__ = PRETRAINED_VOCAB_FILES_MAP snake_case__ = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES snake_case__ = ["input_ids", "attention_mask"] def __init__( self : Dict , __SCREAMING_SNAKE_CASE : str , __SCREAMING_SNAKE_CASE : Dict="<s>" , __SCREAMING_SNAKE_CASE : List[Any]="</s>" , __SCREAMING_SNAKE_CASE : List[str]="</s>" , __SCREAMING_SNAKE_CASE : List[str]="<s>" , __SCREAMING_SNAKE_CASE : Dict="<unk>" , __SCREAMING_SNAKE_CASE : int="<pad>" , __SCREAMING_SNAKE_CASE : Tuple="<mask>" , __SCREAMING_SNAKE_CASE : Optional[Dict[str, Any]] = None , **__SCREAMING_SNAKE_CASE : Optional[Any] , ) -> None: # Mask token behave like a normal word, i.e. include the space before it a_ : Tuple = AddedToken(__SCREAMING_SNAKE_CASE , lstrip=__SCREAMING_SNAKE_CASE , rstrip=__SCREAMING_SNAKE_CASE ) if isinstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) else mask_token a_ : List[str] = {} if sp_model_kwargs is None else sp_model_kwargs super().__init__( bos_token=__SCREAMING_SNAKE_CASE , eos_token=__SCREAMING_SNAKE_CASE , unk_token=__SCREAMING_SNAKE_CASE , sep_token=__SCREAMING_SNAKE_CASE , cls_token=__SCREAMING_SNAKE_CASE , pad_token=__SCREAMING_SNAKE_CASE , mask_token=__SCREAMING_SNAKE_CASE , sp_model_kwargs=self.sp_model_kwargs , **__SCREAMING_SNAKE_CASE , ) a_ : Tuple = vocab_file a_ : Optional[Any] = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(str(__SCREAMING_SNAKE_CASE ) ) a_ : Optional[Any] = {'''<s>''': 0, '''<pad>''': 1, '''</s>''': 2, '''<unk>''': 3} a_ : Any = len(self.sp_model ) - 1 a_ : Optional[Any] = {v: k for k, v in self.fairseq_tokens_to_ids.items()} def SCREAMING_SNAKE_CASE ( self : str , __SCREAMING_SNAKE_CASE : List[int] , __SCREAMING_SNAKE_CASE : Optional[List[int]] = None ) -> List[int]: if token_ids_a is None: return [self.cls_token_id] + token_ids_a + [self.sep_token_id] a_ : List[str] = [self.cls_token_id] a_ : Optional[Any] = [self.sep_token_id] return cls + token_ids_a + sep + sep + token_ids_a + sep def SCREAMING_SNAKE_CASE ( self : Optional[int] , __SCREAMING_SNAKE_CASE : List[int] , __SCREAMING_SNAKE_CASE : Optional[List[int]] = None , __SCREAMING_SNAKE_CASE : bool = False ) -> List[int]: 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 None: return [1] + ([0] * len(__SCREAMING_SNAKE_CASE )) + [1] return [1] + ([0] * len(__SCREAMING_SNAKE_CASE )) + [1, 1] + ([0] * len(__SCREAMING_SNAKE_CASE )) + [1] def SCREAMING_SNAKE_CASE ( self : Union[str, Any] , __SCREAMING_SNAKE_CASE : List[int] , __SCREAMING_SNAKE_CASE : Optional[List[int]] = None ) -> List[int]: a_ : List[str] = [self.sep_token_id] a_ : Any = [self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep ) * [0] return len(cls + token_ids_a + sep + sep + token_ids_a + sep ) * [0] @property def SCREAMING_SNAKE_CASE ( self : Any ) -> Union[str, Any]: return len(self.sp_model ) def SCREAMING_SNAKE_CASE ( self : str ) -> Tuple: a_ : int = {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 SCREAMING_SNAKE_CASE ( self : Optional[Any] , __SCREAMING_SNAKE_CASE : str ) -> List[str]: return self.sp_model.encode(__SCREAMING_SNAKE_CASE , out_type=__SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE ( self : str , __SCREAMING_SNAKE_CASE : List[Any] ) -> Dict: if token in self.fairseq_tokens_to_ids: return self.fairseq_tokens_to_ids[token] a_ : Optional[int] = self.sp_model.PieceToId(__SCREAMING_SNAKE_CASE ) return spm_id if spm_id else self.unk_token_id def SCREAMING_SNAKE_CASE ( self : List[str] , __SCREAMING_SNAKE_CASE : int ) -> str: if index in self.fairseq_ids_to_tokens: return self.fairseq_ids_to_tokens[index] return self.sp_model.IdToPiece(__SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE ( self : str , __SCREAMING_SNAKE_CASE : Optional[Any] ) -> Optional[int]: a_ : Dict = [] a_ : List[Any] = '''''' a_ : Dict = False for token in tokens: # make sure that special tokens are not decoded using sentencepiece model if token in self.all_special_tokens: if not prev_is_special: out_string += " " out_string += self.sp_model.decode(__SCREAMING_SNAKE_CASE ) + token a_ : Dict = True a_ : Optional[Any] = [] else: current_sub_tokens.append(__SCREAMING_SNAKE_CASE ) a_ : Tuple = False out_string += self.sp_model.decode(__SCREAMING_SNAKE_CASE ) return out_string.strip() def __getstate__( self : Dict ) -> int: a_ : Dict = self.__dict__.copy() a_ : List[str] = None return state def __setstate__( self : Dict , __SCREAMING_SNAKE_CASE : Optional[Any] ) -> Any: a_ : Optional[Any] = d # for backward compatibility if not hasattr(self , '''sp_model_kwargs''' ): a_ : Union[str, Any] = {} a_ : int = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(self.vocab_file ) def SCREAMING_SNAKE_CASE ( self : str , __SCREAMING_SNAKE_CASE : str , __SCREAMING_SNAKE_CASE : Optional[str] = None ) -> Tuple[str]: if not os.path.isdir(__SCREAMING_SNAKE_CASE ): logger.error(f'Vocabulary path ({save_directory}) should be a directory' ) return a_ : Union[str, Any] = 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: a_ : Any = self.sp_model.serialized_model_proto() fi.write(__SCREAMING_SNAKE_CASE ) return (out_vocab_file,)
666
0
'''simple docstring''' from typing import List, Optional, Union import numpy as np import torch import torchaudio.compliance.kaldi as ta_kaldi from ...feature_extraction_sequence_utils import SequenceFeatureExtractor from ...feature_extraction_utils import BatchFeature from ...utils import PaddingStrategy, TensorType, logging __lowerCAmelCase = logging.get_logger(__name__) class SCREAMING_SNAKE_CASE ( SCREAMING_SNAKE_CASE_ ): snake_case__ = ["input_features", "attention_mask"] def __init__( self : Union[str, Any] , __SCREAMING_SNAKE_CASE : Any=80 , __SCREAMING_SNAKE_CASE : Union[str, Any]=1_6000 , __SCREAMING_SNAKE_CASE : Dict=80 , __SCREAMING_SNAKE_CASE : Dict=0.0 , __SCREAMING_SNAKE_CASE : Optional[int]=True , __SCREAMING_SNAKE_CASE : Tuple=True , __SCREAMING_SNAKE_CASE : List[Any]=True , **__SCREAMING_SNAKE_CASE : Union[str, Any] , ) -> Tuple: super().__init__(feature_size=__SCREAMING_SNAKE_CASE , sampling_rate=__SCREAMING_SNAKE_CASE , padding_value=__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) a_ : Union[str, Any] = num_mel_bins a_ : str = do_ceptral_normalize a_ : int = normalize_means a_ : Optional[int] = normalize_vars a_ : Dict = True def SCREAMING_SNAKE_CASE ( self : str , __SCREAMING_SNAKE_CASE : np.ndarray , ) -> np.ndarray: a_ : str = waveform * (2**15) # Kaldi compliance: 16-bit signed integers a_ : Union[str, Any] = torch.from_numpy(__SCREAMING_SNAKE_CASE ).unsqueeze(0 ) a_ : List[Any] = ta_kaldi.fbank(__SCREAMING_SNAKE_CASE , num_mel_bins=self.num_mel_bins , sample_frequency=self.sampling_rate ) return features.numpy() @staticmethod def SCREAMING_SNAKE_CASE ( __SCREAMING_SNAKE_CASE : np.ndarray , __SCREAMING_SNAKE_CASE : int , __SCREAMING_SNAKE_CASE : Optional[bool] = True , __SCREAMING_SNAKE_CASE : Optional[bool] = True , __SCREAMING_SNAKE_CASE : float = 0.0 , ) -> np.ndarray: # make sure we normalize float32 arrays if normalize_means: a_ : Any = x[:input_length].mean(axis=0 ) a_ : Any = np.subtract(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) if normalize_vars: a_ : Dict = x[:input_length].std(axis=0 ) a_ : List[Any] = np.divide(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) if input_length < x.shape[0]: a_ : Any = padding_value # make sure array is in float32 a_ : Union[str, Any] = x.astype(np.floataa ) return x def SCREAMING_SNAKE_CASE ( self : Optional[int] , __SCREAMING_SNAKE_CASE : List[np.ndarray] , __SCREAMING_SNAKE_CASE : Optional[np.ndarray] = None ) -> List[np.ndarray]: a_ : Optional[int] = attention_mask.sum(-1 ) if attention_mask is not None else [x.shape[0] for x in input_features] return [ self.utterance_cmvn(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , self.normalize_means , self.normalize_vars , self.padding_value ) for x, n in zip(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) ] def __call__( self : str , __SCREAMING_SNAKE_CASE : Union[np.ndarray, List[float], List[np.ndarray], List[List[float]]] , __SCREAMING_SNAKE_CASE : Union[bool, str, PaddingStrategy] = False , __SCREAMING_SNAKE_CASE : Optional[int] = None , __SCREAMING_SNAKE_CASE : bool = False , __SCREAMING_SNAKE_CASE : Optional[int] = None , __SCREAMING_SNAKE_CASE : Optional[Union[str, TensorType]] = None , __SCREAMING_SNAKE_CASE : Optional[int] = None , __SCREAMING_SNAKE_CASE : Optional[bool] = None , **__SCREAMING_SNAKE_CASE : List[str] , ) -> BatchFeature: if sampling_rate is not None: if sampling_rate != self.sampling_rate: raise ValueError( f'The model corresponding to this feature extractor: {self} was trained using a sampling rate of' f' {self.sampling_rate}. Please make sure that the provided `raw_speech` input was sampled with' f' {self.sampling_rate} and not {sampling_rate}.' ) else: logger.warning( '''It is strongly recommended to pass the `sampling_rate` argument to this function. ''' '''Failing to do so can result in silent errors that might be hard to debug.''' ) a_ : Dict = isinstance(__SCREAMING_SNAKE_CASE , np.ndarray ) and len(raw_speech.shape ) > 1 if is_batched_numpy and len(raw_speech.shape ) > 2: raise ValueError(f'Only mono-channel audio is supported for input to {self}' ) a_ : int = is_batched_numpy or ( isinstance(__SCREAMING_SNAKE_CASE , (list, tuple) ) and (isinstance(raw_speech[0] , (np.ndarray, tuple, list) )) ) if is_batched: a_ : Dict = [np.asarray(__SCREAMING_SNAKE_CASE , dtype=np.floataa ) for speech in raw_speech] elif not is_batched and not isinstance(__SCREAMING_SNAKE_CASE , np.ndarray ): a_ : Optional[int] = np.asarray(__SCREAMING_SNAKE_CASE , dtype=np.floataa ) elif isinstance(__SCREAMING_SNAKE_CASE , np.ndarray ) and raw_speech.dtype is np.dtype(np.floataa ): a_ : Dict = raw_speech.astype(np.floataa ) # always return batch if not is_batched: a_ : List[Any] = [raw_speech] # extract fbank features a_ : int = [self._extract_fbank_features(__SCREAMING_SNAKE_CASE ) for waveform in raw_speech] # convert into correct format for padding a_ : Dict = BatchFeature({'''input_features''': features} ) a_ : int = self.pad( __SCREAMING_SNAKE_CASE , padding=__SCREAMING_SNAKE_CASE , max_length=__SCREAMING_SNAKE_CASE , truncation=__SCREAMING_SNAKE_CASE , pad_to_multiple_of=__SCREAMING_SNAKE_CASE , return_attention_mask=__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE , ) # make sure list is in array format a_ : Optional[Any] = padded_inputs.get('''input_features''' ) if isinstance(input_features[0] , __SCREAMING_SNAKE_CASE ): a_ : str = [np.asarray(__SCREAMING_SNAKE_CASE , dtype=np.floataa ) for feature in input_features] a_ : Union[str, Any] = padded_inputs.get('''attention_mask''' ) if attention_mask is not None: a_ : str = [np.asarray(__SCREAMING_SNAKE_CASE , dtype=np.intaa ) for array in attention_mask] # Utterance-level cepstral mean and variance normalization if self.do_ceptral_normalize: a_ : List[str] = ( np.array(__SCREAMING_SNAKE_CASE , dtype=np.intaa ) if self._get_padding_strategies(__SCREAMING_SNAKE_CASE , max_length=__SCREAMING_SNAKE_CASE ) is not PaddingStrategy.DO_NOT_PAD else None ) a_ : List[Any] = self.normalize( padded_inputs['''input_features'''] , attention_mask=__SCREAMING_SNAKE_CASE ) if return_tensors is not None: a_ : Optional[int] = padded_inputs.convert_to_tensors(__SCREAMING_SNAKE_CASE ) return padded_inputs
708
'''simple docstring''' def _UpperCAmelCase ( __A : str , __A : str ): def get_matched_characters(__A : str , __A : str ) -> str: a_ : Union[str, Any] = [] a_ : int = min(len(_stra ) , len(_stra ) ) // 2 for i, l in enumerate(_stra ): a_ : Any = int(max(0 , i - limit ) ) a_ : Union[str, Any] = int(min(i + limit + 1 , len(_stra ) ) ) if l in _stra[left:right]: matched.append(__A ) a_ : Any = f'{_stra[0:_stra.index(__A )]} {_stra[_stra.index(__A ) + 1:]}' return "".join(__A ) # matching characters a_ : Optional[Any] = get_matched_characters(__A , __A ) a_ : int = get_matched_characters(__A , __A ) a_ : Any = len(__A ) # transposition a_ : List[Any] = ( len([(ca, ca) for ca, ca in zip(__A , __A ) if ca != ca] ) // 2 ) if not match_count: a_ : Dict = 0.0 else: a_ : Optional[int] = ( 1 / 3 * ( match_count / len(__A ) + match_count / len(__A ) + (match_count - transpositions) / match_count ) ) # common prefix up to 4 characters a_ : List[str] = 0 for ca, ca in zip(stra[:4] , stra[:4] ): if ca == ca: prefix_len += 1 else: break return jaro + 0.1 * prefix_len * (1 - jaro) if __name__ == "__main__": import doctest doctest.testmod() print(jaro_winkler('hello', 'world'))
666
0
'''simple docstring''' import warnings from ...utils import logging from .image_processing_perceiver import PerceiverImageProcessor __lowerCAmelCase = logging.get_logger(__name__) class SCREAMING_SNAKE_CASE ( SCREAMING_SNAKE_CASE_ ): def __init__( self : str , *__SCREAMING_SNAKE_CASE : Optional[int] , **__SCREAMING_SNAKE_CASE : int ) -> None: warnings.warn( '''The class PerceiverFeatureExtractor is deprecated and will be removed in version 5 of Transformers.''' ''' Please use PerceiverImageProcessor instead.''' , __SCREAMING_SNAKE_CASE , ) super().__init__(*__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE )
709
'''simple docstring''' import torch from transformers import AutoModel class SCREAMING_SNAKE_CASE ( torch.nn.Module ): def __init__( self : Optional[int] , __SCREAMING_SNAKE_CASE : int="sayef/fsner-bert-base-uncased" ) -> str: super(__SCREAMING_SNAKE_CASE , self ).__init__() a_ : str = AutoModel.from_pretrained(__SCREAMING_SNAKE_CASE , return_dict=__SCREAMING_SNAKE_CASE ) a_ : Optional[int] = torch.nn.CosineSimilarity(3 , 1e-08 ) a_ : Dict = torch.nn.Softmax(dim=1 ) def SCREAMING_SNAKE_CASE ( self : str , **__SCREAMING_SNAKE_CASE : int ) -> str: return self.bert(**__SCREAMING_SNAKE_CASE ).last_hidden_state def SCREAMING_SNAKE_CASE ( self : List[str] , __SCREAMING_SNAKE_CASE : List[Any] ) -> Optional[int]: return token_embeddings.sum(2 , keepdim=__SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE ( self : List[Any] , __SCREAMING_SNAKE_CASE : Optional[int] , __SCREAMING_SNAKE_CASE : Union[str, Any] , __SCREAMING_SNAKE_CASE : int=1 ) -> Dict: return self.softmax(T * self.cos(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) ) def SCREAMING_SNAKE_CASE ( self : str , __SCREAMING_SNAKE_CASE : Optional[Any] , __SCREAMING_SNAKE_CASE : Optional[int] ) -> Any: a_ : Dict = W_supports['''sizes'''].tolist() a_ : Tuple = W_supports['''start_token_id'''].item() a_ : List[Any] = W_supports['''end_token_id'''].item() del W_supports["sizes"] del W_supports["start_token_id"] del W_supports["end_token_id"] a_ : int = self.BERT(**__SCREAMING_SNAKE_CASE ) a_ : Any = self.BERT(**__SCREAMING_SNAKE_CASE ) a_ : Optional[Any] = None a_ : Tuple = None a_ : List[str] = W_supports['''input_ids'''] == start_token_id a_ : Dict = W_supports['''input_ids'''] == end_token_id for i, size in enumerate(__SCREAMING_SNAKE_CASE ): if i == 0: a_ : str = 0 else: a_ : str = support_sizes[i - 1] a_ : Union[str, Any] = S[s : s + size][start_token_masks[s : s + size]] a_ : Tuple = S[s : s + size][end_token_masks[s : s + size]] a_ : Tuple = torch.matmul(q[i] , s_start.T ).sum(1 ).softmax(0 ) a_ : Optional[Any] = torch.matmul(q[i] , s_end.T ).sum(1 ).softmax(0 ) if p_starts is not None: a_ : Any = torch.vstack((p_starts, p_start) ) a_ : Dict = torch.vstack((p_ends, p_end) ) else: a_ : Optional[int] = p_start a_ : List[Any] = p_end return p_starts, p_ends
666
0
'''simple docstring''' import argparse from argparse import Namespace import torch from torch import nn from transformers import XGLMConfig, XGLMForCausalLM def _UpperCAmelCase ( __A : List[str] ): a_ : Optional[int] = [ '''decoder.version''', '''decoder.output_projection.weight''', '''_float_tensor''', '''decoder.embed_positions._float_tensor''', ] for k in ignore_keys: state_dict.pop(__A , __A ) def _UpperCAmelCase ( __A : str ): a_ : Dict = emb.weight.shape a_ : Optional[Any] = nn.Linear(__A , __A , bias=__A ) a_ : Dict = emb.weight.data return lin_layer def _UpperCAmelCase ( __A : Any ): a_ : List[str] = torch.load(__A , map_location='''cpu''' ) a_ : Optional[int] = Namespace(**checkpoint['''cfg''']['''model'''] ) a_ : Tuple = checkpoint['''model'''] remove_ignore_keys_(__A ) a_ : str = state_dict['''decoder.embed_tokens.weight'''].shape[0] a_ : str = {key.replace('''decoder''' , '''model''' ): val for key, val in state_dict.items()} a_ : List[str] = XGLMConfig( vocab_size=__A , max_position_embeddings=args.max_target_positions , num_layers=args.decoder_layers , attention_heads=args.decoder_attention_heads , ffn_dim=args.decoder_ffn_embed_dim , d_model=args.decoder_embed_dim , layerdrop=args.decoder_layerdrop , dropout=args.dropout , attention_dropout=args.attention_dropout , activation_dropout=args.activation_dropout , activation_function='''gelu''' , scale_embedding=not args.no_scale_embedding , tie_word_embeddings=args.share_decoder_input_output_embed , ) a_ : Tuple = XGLMForCausalLM(__A ) a_ : Any = model.load_state_dict(__A , strict=__A ) print(__A ) a_ : List[str] = make_linear_from_emb(model.model.embed_tokens ) return model if __name__ == "__main__": __lowerCAmelCase = argparse.ArgumentParser() # Required parameters parser.add_argument('fairseq_path', type=str, help='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.') __lowerCAmelCase = parser.parse_args() __lowerCAmelCase = convert_fairseq_xglm_checkpoint_from_disk(args.fairseq_path) model.save_pretrained(args.pytorch_dump_folder_path)
710
'''simple docstring''' import copy from dataclasses import dataclass, field from typing import ClassVar, Dict from ..features import ClassLabel, Features, Image from .base import TaskTemplate @dataclass(frozen=SCREAMING_SNAKE_CASE_ ) class SCREAMING_SNAKE_CASE ( SCREAMING_SNAKE_CASE_ ): snake_case__ = field(default="image-classification" , metadata={"include_in_asdict_even_if_is_default": True} ) snake_case__ = Features({"image": Image()} ) snake_case__ = Features({"labels": ClassLabel} ) snake_case__ = "image" snake_case__ = "labels" def SCREAMING_SNAKE_CASE ( self : List[str] , __SCREAMING_SNAKE_CASE : List[Any] ) -> Any: if self.label_column not in features: raise ValueError(f'Column {self.label_column} is not present in features.' ) if not isinstance(features[self.label_column] , __SCREAMING_SNAKE_CASE ): raise ValueError(f'Column {self.label_column} is not a ClassLabel.' ) a_ : Optional[int] = copy.deepcopy(self ) a_ : int = self.label_schema.copy() a_ : Tuple = features[self.label_column] a_ : str = label_schema return task_template @property def SCREAMING_SNAKE_CASE ( self : Tuple ) -> Dict[str, str]: return { self.image_column: "image", self.label_column: "labels", }
666
0
'''simple docstring''' import copy import tempfile import unittest from huggingface_hub import HfFolder, delete_repo from parameterized import parameterized from requests.exceptions import HTTPError from transformers import AutoConfig, GenerationConfig from transformers.testing_utils import TOKEN, USER, is_staging_test class SCREAMING_SNAKE_CASE ( unittest.TestCase ): @parameterized.expand([(None,), ('''foo.json''',)] ) def SCREAMING_SNAKE_CASE ( self : Dict , __SCREAMING_SNAKE_CASE : Optional[Any] ) -> List[Any]: a_ : List[Any] = GenerationConfig( do_sample=__SCREAMING_SNAKE_CASE , temperature=0.7 , length_penalty=1.0 , bad_words_ids=[[1, 2, 3], [4, 5]] , ) with tempfile.TemporaryDirectory() as tmp_dir: config.save_pretrained(__SCREAMING_SNAKE_CASE , config_name=__SCREAMING_SNAKE_CASE ) a_ : str = GenerationConfig.from_pretrained(__SCREAMING_SNAKE_CASE , config_name=__SCREAMING_SNAKE_CASE ) # Checks parameters that were specified self.assertEqual(loaded_config.do_sample , __SCREAMING_SNAKE_CASE ) self.assertEqual(loaded_config.temperature , 0.7 ) self.assertEqual(loaded_config.length_penalty , 1.0 ) self.assertEqual(loaded_config.bad_words_ids , [[1, 2, 3], [4, 5]] ) # Checks parameters that were not specified (defaults) self.assertEqual(loaded_config.top_k , 50 ) self.assertEqual(loaded_config.max_length , 20 ) self.assertEqual(loaded_config.max_time , __SCREAMING_SNAKE_CASE ) def SCREAMING_SNAKE_CASE ( self : List[Any] ) -> str: a_ : str = AutoConfig.from_pretrained('''gpt2''' ) a_ : str = GenerationConfig.from_model_config(__SCREAMING_SNAKE_CASE ) a_ : Optional[Any] = GenerationConfig() # The generation config has loaded a few non-default parameters from the model config self.assertNotEqual(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) # One of those parameters is eos_token_id -- check if it matches self.assertNotEqual(generation_config_from_model.eos_token_id , default_generation_config.eos_token_id ) self.assertEqual(generation_config_from_model.eos_token_id , model_config.eos_token_id ) def SCREAMING_SNAKE_CASE ( self : List[str] ) -> Union[str, Any]: a_ : Tuple = GenerationConfig() a_ : List[Any] = { '''max_new_tokens''': 1024, '''foo''': '''bar''', } a_ : Union[str, Any] = copy.deepcopy(__SCREAMING_SNAKE_CASE ) a_ : Any = generation_config.update(**__SCREAMING_SNAKE_CASE ) # update_kwargs was not modified (no side effects) self.assertEqual(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) # update_kwargs was used to update the config on valid attributes self.assertEqual(generation_config.max_new_tokens , 1024 ) # `.update()` returns a dictionary of unused kwargs self.assertEqual(__SCREAMING_SNAKE_CASE , {'''foo''': '''bar'''} ) def SCREAMING_SNAKE_CASE ( self : int ) -> Optional[int]: a_ : Optional[int] = GenerationConfig() a_ : List[str] = '''bar''' with tempfile.TemporaryDirectory('''test-generation-config''' ) as tmp_dir: generation_config.save_pretrained(__SCREAMING_SNAKE_CASE ) a_ : Optional[int] = GenerationConfig.from_pretrained(__SCREAMING_SNAKE_CASE ) # update_kwargs was used to update the config on valid attributes self.assertEqual(new_config.foo , '''bar''' ) a_ : Union[str, Any] = GenerationConfig.from_model_config(__SCREAMING_SNAKE_CASE ) assert not hasattr(__SCREAMING_SNAKE_CASE , '''foo''' ) # no new kwargs should be initialized if from config def SCREAMING_SNAKE_CASE ( self : Dict ) -> Optional[int]: a_ : Dict = GenerationConfig() self.assertEqual(default_config.temperature , 1.0 ) self.assertEqual(default_config.do_sample , __SCREAMING_SNAKE_CASE ) self.assertEqual(default_config.num_beams , 1 ) a_ : Optional[Any] = GenerationConfig( do_sample=__SCREAMING_SNAKE_CASE , temperature=0.7 , length_penalty=1.0 , bad_words_ids=[[1, 2, 3], [4, 5]] , ) self.assertEqual(config.temperature , 0.7 ) self.assertEqual(config.do_sample , __SCREAMING_SNAKE_CASE ) self.assertEqual(config.num_beams , 1 ) with tempfile.TemporaryDirectory() as tmp_dir: config.save_pretrained(__SCREAMING_SNAKE_CASE ) a_ : Union[str, Any] = GenerationConfig.from_pretrained(__SCREAMING_SNAKE_CASE , temperature=1.0 ) self.assertEqual(loaded_config.temperature , 1.0 ) self.assertEqual(loaded_config.do_sample , __SCREAMING_SNAKE_CASE ) self.assertEqual(loaded_config.num_beams , 1 ) # default value @is_staging_test class SCREAMING_SNAKE_CASE ( unittest.TestCase ): @classmethod def SCREAMING_SNAKE_CASE ( cls : Optional[int] ) -> Union[str, Any]: a_ : List[str] = TOKEN HfFolder.save_token(__SCREAMING_SNAKE_CASE ) @classmethod def SCREAMING_SNAKE_CASE ( cls : int ) -> Union[str, Any]: try: delete_repo(token=cls._token , repo_id='''test-generation-config''' ) except HTTPError: pass try: delete_repo(token=cls._token , repo_id='''valid_org/test-generation-config-org''' ) except HTTPError: pass def SCREAMING_SNAKE_CASE ( self : List[Any] ) -> Optional[int]: a_ : List[Any] = GenerationConfig( do_sample=__SCREAMING_SNAKE_CASE , temperature=0.7 , length_penalty=1.0 , ) config.push_to_hub('''test-generation-config''' , use_auth_token=self._token ) a_ : int = GenerationConfig.from_pretrained(f'{USER}/test-generation-config' ) for k, v in config.to_dict().items(): if k != "transformers_version": self.assertEqual(__SCREAMING_SNAKE_CASE , getattr(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) ) # Reset repo delete_repo(token=self._token , repo_id='''test-generation-config''' ) # Push to hub via save_pretrained with tempfile.TemporaryDirectory() as tmp_dir: config.save_pretrained( __SCREAMING_SNAKE_CASE , repo_id='''test-generation-config''' , push_to_hub=__SCREAMING_SNAKE_CASE , use_auth_token=self._token ) a_ : List[Any] = GenerationConfig.from_pretrained(f'{USER}/test-generation-config' ) for k, v in config.to_dict().items(): if k != "transformers_version": self.assertEqual(__SCREAMING_SNAKE_CASE , getattr(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) ) def SCREAMING_SNAKE_CASE ( self : Tuple ) -> Tuple: a_ : Optional[int] = GenerationConfig( do_sample=__SCREAMING_SNAKE_CASE , temperature=0.7 , length_penalty=1.0 , ) config.push_to_hub('''valid_org/test-generation-config-org''' , use_auth_token=self._token ) a_ : str = GenerationConfig.from_pretrained('''valid_org/test-generation-config-org''' ) for k, v in config.to_dict().items(): if k != "transformers_version": self.assertEqual(__SCREAMING_SNAKE_CASE , getattr(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) ) # Reset repo delete_repo(token=self._token , repo_id='''valid_org/test-generation-config-org''' ) # Push to hub via save_pretrained with tempfile.TemporaryDirectory() as tmp_dir: config.save_pretrained( __SCREAMING_SNAKE_CASE , repo_id='''valid_org/test-generation-config-org''' , push_to_hub=__SCREAMING_SNAKE_CASE , use_auth_token=self._token ) a_ : int = GenerationConfig.from_pretrained('''valid_org/test-generation-config-org''' ) for k, v in config.to_dict().items(): if k != "transformers_version": self.assertEqual(__SCREAMING_SNAKE_CASE , getattr(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) )
711
'''simple docstring''' from __future__ import annotations def _UpperCAmelCase ( __A : tuple[int, int] , __A : int ): a_ , a_ : List[str] = position a_ : Optional[int] = [ (y + 1, x + 2), (y - 1, x + 2), (y + 1, x - 2), (y - 1, x - 2), (y + 2, x + 1), (y + 2, x - 1), (y - 2, x + 1), (y - 2, x - 1), ] a_ : Any = [] for position in positions: a_ , a_ : Dict = position if 0 <= y_test < n and 0 <= x_test < n: permissible_positions.append(__A ) return permissible_positions def _UpperCAmelCase ( __A : list[list[int]] ): return not any(elem == 0 for row in board for elem in row ) def _UpperCAmelCase ( __A : list[list[int]] , __A : tuple[int, int] , __A : int ): if is_complete(__A ): return True for position in get_valid_pos(__A , len(__A ) ): a_ , a_ : Dict = position if board[y][x] == 0: a_ : Optional[Any] = curr + 1 if open_knight_tour_helper(__A , __A , curr + 1 ): return True a_ : Tuple = 0 return False def _UpperCAmelCase ( __A : int ): a_ : List[str] = [[0 for i in range(__A )] for j in range(__A )] for i in range(__A ): for j in range(__A ): a_ : Optional[Any] = 1 if open_knight_tour_helper(__A , (i, j) , 1 ): return board a_ : Union[str, Any] = 0 a_ : Dict = f'Open Kight Tour cannot be performed on a board of size {n}' raise ValueError(__A ) if __name__ == "__main__": import doctest doctest.testmod()
666
0
'''simple docstring''' from collections import OrderedDict from typing import Mapping from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging __lowerCAmelCase = logging.get_logger(__name__) __lowerCAmelCase = { 'xlm-roberta-base': 'https://huggingface.co/xlm-roberta-base/resolve/main/config.json', 'xlm-roberta-large': 'https://huggingface.co/xlm-roberta-large/resolve/main/config.json', 'xlm-roberta-large-finetuned-conll02-dutch': ( 'https://huggingface.co/xlm-roberta-large-finetuned-conll02-dutch/resolve/main/config.json' ), 'xlm-roberta-large-finetuned-conll02-spanish': ( 'https://huggingface.co/xlm-roberta-large-finetuned-conll02-spanish/resolve/main/config.json' ), 'xlm-roberta-large-finetuned-conll03-english': ( 'https://huggingface.co/xlm-roberta-large-finetuned-conll03-english/resolve/main/config.json' ), 'xlm-roberta-large-finetuned-conll03-german': ( 'https://huggingface.co/xlm-roberta-large-finetuned-conll03-german/resolve/main/config.json' ), } class SCREAMING_SNAKE_CASE ( SCREAMING_SNAKE_CASE_ ): snake_case__ = "xlm-roberta" def __init__( self : Tuple , __SCREAMING_SNAKE_CASE : Union[str, Any]=3_0522 , __SCREAMING_SNAKE_CASE : Any=768 , __SCREAMING_SNAKE_CASE : Dict=12 , __SCREAMING_SNAKE_CASE : Dict=12 , __SCREAMING_SNAKE_CASE : Any=3072 , __SCREAMING_SNAKE_CASE : Union[str, Any]="gelu" , __SCREAMING_SNAKE_CASE : int=0.1 , __SCREAMING_SNAKE_CASE : Optional[int]=0.1 , __SCREAMING_SNAKE_CASE : str=512 , __SCREAMING_SNAKE_CASE : Optional[int]=2 , __SCREAMING_SNAKE_CASE : List[str]=0.02 , __SCREAMING_SNAKE_CASE : Optional[Any]=1e-12 , __SCREAMING_SNAKE_CASE : Tuple=1 , __SCREAMING_SNAKE_CASE : Tuple=0 , __SCREAMING_SNAKE_CASE : List[Any]=2 , __SCREAMING_SNAKE_CASE : Optional[int]="absolute" , __SCREAMING_SNAKE_CASE : str=True , __SCREAMING_SNAKE_CASE : List[str]=None , **__SCREAMING_SNAKE_CASE : Union[str, Any] , ) -> List[Any]: super().__init__(pad_token_id=__SCREAMING_SNAKE_CASE , bos_token_id=__SCREAMING_SNAKE_CASE , eos_token_id=__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) a_ : int = vocab_size a_ : Optional[Any] = hidden_size a_ : Union[str, Any] = num_hidden_layers a_ : Optional[int] = num_attention_heads a_ : Tuple = hidden_act a_ : Optional[int] = intermediate_size a_ : Dict = hidden_dropout_prob a_ : Optional[int] = attention_probs_dropout_prob a_ : List[Any] = max_position_embeddings a_ : Union[str, Any] = type_vocab_size a_ : Tuple = initializer_range a_ : List[str] = layer_norm_eps a_ : Tuple = position_embedding_type a_ : Tuple = use_cache a_ : Optional[int] = classifier_dropout class SCREAMING_SNAKE_CASE ( SCREAMING_SNAKE_CASE_ ): @property def SCREAMING_SNAKE_CASE ( self : Any ) -> Mapping[str, Mapping[int, str]]: if self.task == "multiple-choice": a_ : List[str] = {0: '''batch''', 1: '''choice''', 2: '''sequence'''} else: a_ : str = {0: '''batch''', 1: '''sequence'''} return OrderedDict( [ ('''input_ids''', dynamic_axis), ('''attention_mask''', dynamic_axis), ] )
712
'''simple docstring''' import warnings warnings.warn( 'memory_utils has been reorganized to utils.memory. Import `find_executable_batchsize` from the main `__init__`: ' '`from accelerate import find_executable_batch_size` to avoid this warning.', FutureWarning, )
666
0
'''simple docstring''' __lowerCAmelCase = [ 'VerificationMode', 'Version', 'disable_progress_bar', 'enable_progress_bar', 'is_progress_bar_enabled', 'experimental', ] from .info_utils import VerificationMode from .logging import disable_progress_bar, enable_progress_bar, is_progress_bar_enabled from .version import Version from .experimental import experimental
713
'''simple docstring''' import requests from bsa import BeautifulSoup def _UpperCAmelCase ( __A : str , __A : dict ): a_ : Tuple = BeautifulSoup(requests.get(__A , params=__A ).content , '''html.parser''' ) a_ : List[str] = soup.find('''div''' , attrs={'''class''': '''gs_ri'''} ) a_ : List[str] = div.find('''div''' , attrs={'''class''': '''gs_fl'''} ).find_all('''a''' ) return anchors[2].get_text() if __name__ == "__main__": __lowerCAmelCase = { 'title': ( 'Precisely geometry controlled microsupercapacitors for ultrahigh areal ' 'capacitance, volumetric capacitance, and energy density' ), 'journal': 'Chem. Mater.', 'volume': 30, 'pages': '3979-3990', 'year': 2_018, 'hl': 'en', } print(get_citation('https://scholar.google.com/scholar_lookup', params=params))
666
0
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tokenizers_available, is_torch_available __lowerCAmelCase = { 'configuration_roc_bert': ['ROC_BERT_PRETRAINED_CONFIG_ARCHIVE_MAP', 'RoCBertConfig'], 'tokenization_roc_bert': ['RoCBertTokenizer'], } try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: pass try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __lowerCAmelCase = [ 'ROC_BERT_PRETRAINED_MODEL_ARCHIVE_LIST', 'RoCBertForCausalLM', 'RoCBertForMaskedLM', 'RoCBertForMultipleChoice', 'RoCBertForPreTraining', 'RoCBertForQuestionAnswering', 'RoCBertForSequenceClassification', 'RoCBertForTokenClassification', 'RoCBertLayer', 'RoCBertModel', 'RoCBertPreTrainedModel', 'load_tf_weights_in_roc_bert', ] if TYPE_CHECKING: from .configuration_roc_bert import ROC_BERT_PRETRAINED_CONFIG_ARCHIVE_MAP, RoCBertConfig from .tokenization_roc_bert import RoCBertTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: raise OptionalDependencyNotAvailable() try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_roc_bert import ( ROC_BERT_PRETRAINED_MODEL_ARCHIVE_LIST, RoCBertForCausalLM, RoCBertForMaskedLM, RoCBertForMultipleChoice, RoCBertForPreTraining, RoCBertForQuestionAnswering, RoCBertForSequenceClassification, RoCBertForTokenClassification, RoCBertLayer, RoCBertModel, RoCBertPreTrainedModel, load_tf_weights_in_roc_bert, ) else: import sys __lowerCAmelCase = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
714
'''simple docstring''' import argparse from torch import nn # transformers_old should correspond to branch `save_old_prophetnet_model_structure` here # original prophetnet_checkpoints are saved under `patrickvonplaten/..._old` respectively from transformers_old.modeling_prophetnet import ( ProphetNetForConditionalGeneration as ProphetNetForConditionalGenerationOld, ) from transformers_old.modeling_xlm_prophetnet import ( XLMProphetNetForConditionalGeneration as XLMProphetNetForConditionalGenerationOld, ) from transformers import ProphetNetForConditionalGeneration, XLMProphetNetForConditionalGeneration, logging __lowerCAmelCase = logging.get_logger(__name__) logging.set_verbosity_info() def _UpperCAmelCase ( __A : str , __A : str ): if "xprophetnet" in prophetnet_checkpoint_path: a_ : Tuple = XLMProphetNetForConditionalGenerationOld.from_pretrained(__A ) a_ , a_ : Optional[Any] = XLMProphetNetForConditionalGeneration.from_pretrained( __A , output_loading_info=__A ) else: a_ : List[Any] = ProphetNetForConditionalGenerationOld.from_pretrained(__A ) a_ , a_ : Any = ProphetNetForConditionalGeneration.from_pretrained( __A , output_loading_info=__A ) a_ : str = ['''key_proj''', '''value_proj''', '''query_proj'''] a_ : Tuple = { '''self_attn''': '''ngram_self_attn''', '''cross_attn''': '''encoder_attn''', '''cross_attn_layer_norm''': '''encoder_attn_layer_norm''', '''feed_forward_layer_norm''': '''final_layer_norm''', '''feed_forward''': '''''', '''intermediate''': '''fc1''', '''output''': '''fc2''', '''key_proj''': '''k_proj''', '''query_proj''': '''q_proj''', '''value_proj''': '''v_proj''', '''word_embeddings''': '''embed_tokens''', '''embeddings_layer_norm''': '''emb_layer_norm''', '''relative_pos_embeddings''': '''relative_linear''', '''ngram_embeddings''': '''ngram_input_embed''', '''position_embeddings''': '''embed_positions''', } for key in loading_info["missing_keys"]: a_ : List[str] = key.split('''.''' ) if attributes[0] == "lm_head": a_ : List[str] = prophet a_ : Dict = prophet_old else: a_ : str = prophet.prophetnet a_ : int = prophet_old.model a_ : str = False for attribute in attributes: if attribute in mapping: a_ : Dict = mapping[attribute] if not hasattr(__A , __A ) and len(__A ) > 0: a_ : List[str] = attribute elif hasattr(__A , __A ): a_ : Union[str, Any] = attribute if attribute == "weight": assert old_model.weight.shape == model.weight.shape, "Shapes have to match!" a_ : Tuple = old_model.weight logger.info(f'{attribute} is initialized.' ) a_ : Union[str, Any] = True break elif attribute == "bias": assert old_model.bias.shape == model.bias.shape, "Shapes have to match!" a_ : Union[str, Any] = old_model.bias logger.info(f'{attribute} is initialized' ) a_ : Dict = True break elif attribute in special_keys and hasattr(__A , '''in_proj_weight''' ): a_ : Tuple = old_model.in_proj_weight.shape[0] // 3 a_ : Any = getattr(__A , __A ) param.weight.shape == old_model.in_proj_weight[:embed_dim, :].shape, "Shapes have to match" param.bias.shape == old_model.in_proj_bias[:embed_dim].shape, "Shapes have to match" if attribute == "query_proj": a_ : Union[str, Any] = nn.Parameter(old_model.in_proj_weight[:embed_dim, :] ) a_ : Optional[Any] = nn.Parameter(old_model.in_proj_bias[:embed_dim] ) elif attribute == "key_proj": a_ : List[Any] = nn.Parameter(old_model.in_proj_weight[embed_dim : 2 * embed_dim, :] ) a_ : Optional[int] = nn.Parameter(old_model.in_proj_bias[embed_dim : 2 * embed_dim] ) elif attribute == "value_proj": a_ : Tuple = nn.Parameter(old_model.in_proj_weight[2 * embed_dim :, :] ) a_ : Any = nn.Parameter(old_model.in_proj_bias[2 * embed_dim :] ) a_ : Dict = True break elif attribute == "position_embeddings": assert ( model.position_embeddings.weight.shape[-1] == old_model.embed_positions.weight.shape[-1] ), "Hidden size has to match" assert model.position_embeddings.weight.shape[0] == 5_12, "We want 512 position_embeddings." a_ : Union[str, Any] = nn.Parameter(old_model.embed_positions.weight[:5_12, :] ) a_ : Optional[Any] = True break if attribute.isdigit(): a_ : Union[str, Any] = model[int(__A )] a_ : str = old_model[int(__A )] else: a_ : Tuple = getattr(__A , __A ) if old_attribute == "": a_ : List[str] = old_model else: if not hasattr(__A , __A ): raise ValueError(f'{old_model} does not have {old_attribute}' ) a_ : Optional[Any] = getattr(__A , __A ) if not is_key_init: raise ValueError(f'{key} was not correctly initialized!' ) print(f'Saving model to {pytorch_dump_folder_path}' ) prophet.save_pretrained(__A ) if __name__ == "__main__": __lowerCAmelCase = argparse.ArgumentParser() # Required parameters parser.add_argument( '--prophetnet_checkpoint_path', default=None, type=str, required=True, help='Path the official PyTorch dump.' ) parser.add_argument( '--pytorch_dump_folder_path', default=None, type=str, required=True, help='Path to the output PyTorch model.' ) __lowerCAmelCase = parser.parse_args() convert_prophetnet_checkpoint_to_pytorch(args.prophetnet_checkpoint_path, args.pytorch_dump_folder_path)
666
0
from __future__ import annotations import bisect def _UpperCAmelCase ( __A : list[int] , __A : int , __A : int = 0 , __A : int = -1 ): if hi < 0: a_ : Optional[Any] = len(__A ) while lo < hi: a_ : Optional[int] = lo + (hi - lo) // 2 if sorted_collection[mid] < item: a_ : List[str] = mid + 1 else: a_ : List[str] = mid return lo def _UpperCAmelCase ( __A : list[int] , __A : int , __A : int = 0 , __A : int = -1 ): if hi < 0: a_ : int = len(__A ) while lo < hi: a_ : int = lo + (hi - lo) // 2 if sorted_collection[mid] <= item: a_ : List[str] = mid + 1 else: a_ : Dict = mid return lo def _UpperCAmelCase ( __A : list[int] , __A : int , __A : int = 0 , __A : int = -1 ): sorted_collection.insert(bisect_left(__A , __A , __A , __A ) , __A ) def _UpperCAmelCase ( __A : list[int] , __A : int , __A : int = 0 , __A : int = -1 ): sorted_collection.insert(bisect_right(__A , __A , __A , __A ) , __A ) def _UpperCAmelCase ( __A : list[int] , __A : int ): a_ : Any = 0 a_ : int = len(__A ) - 1 while left <= right: a_ : Optional[Any] = left + (right - left) // 2 a_ : Union[str, Any] = sorted_collection[midpoint] if current_item == item: return midpoint elif item < current_item: a_ : str = midpoint - 1 else: a_ : str = midpoint + 1 return None def _UpperCAmelCase ( __A : list[int] , __A : int ): a_ : Optional[int] = bisect.bisect_left(__A , __A ) if index != len(__A ) and sorted_collection[index] == item: return index return None def _UpperCAmelCase ( __A : list[int] , __A : int , __A : int , __A : int ): if right < left: return None a_ : Any = left + (right - left) // 2 if sorted_collection[midpoint] == item: return midpoint elif sorted_collection[midpoint] > item: return binary_search_by_recursion(__A , __A , __A , midpoint - 1 ) else: return binary_search_by_recursion(__A , __A , midpoint + 1 , __A ) if __name__ == "__main__": __lowerCAmelCase = input('Enter numbers separated by comma:\n').strip() __lowerCAmelCase = sorted(int(item) for item in user_input.split(',')) __lowerCAmelCase = int(input('Enter a single number to be found in the list:\n')) __lowerCAmelCase = binary_search(collection, target) if result is None: print(F"""{target} was not found in {collection}.""") else: print(F"""{target} was found at position {result} in {collection}.""")
715
'''simple docstring''' import re import string import numpy as np import datasets __lowerCAmelCase = '\nReturns the rate at which the input predicted strings exactly match their references, ignoring any strings input as part of the regexes_to_ignore list.\n' __lowerCAmelCase = '\nArgs:\n predictions: List of predicted texts.\n references: List of reference texts.\n regexes_to_ignore: List, defaults to None. Regex expressions of characters to\n ignore when calculating the exact matches. Note: these regexes are removed\n from the input data before the changes based on the options below (e.g. ignore_case,\n ignore_punctuation, ignore_numbers) are applied.\n ignore_case: Boolean, defaults to False. If true, turns everything\n to lowercase so that capitalization differences are ignored.\n ignore_punctuation: Boolean, defaults to False. If true, removes all punctuation before\n comparing predictions and references.\n ignore_numbers: Boolean, defaults to False. If true, removes all punctuation before\n comparing predictions and references.\nReturns:\n exact_match: Dictionary containing exact_match rate. Possible values are between 0.0 and 100.0, inclusive.\nExamples:\n >>> exact_match = datasets.load_metric("exact_match")\n >>> refs = ["the cat", "theater", "YELLING", "agent007"]\n >>> preds = ["cat?", "theater", "yelling", "agent"]\n >>> results = exact_match.compute(references=refs, predictions=preds)\n >>> print(round(results["exact_match"], 1))\n 25.0\n\n >>> exact_match = datasets.load_metric("exact_match")\n >>> refs = ["the cat", "theater", "YELLING", "agent007"]\n >>> preds = ["cat?", "theater", "yelling", "agent"]\n >>> results = exact_match.compute(references=refs, predictions=preds, regexes_to_ignore=["the ", "yell"], ignore_case=True, ignore_punctuation=True)\n >>> print(round(results["exact_match"], 1))\n 50.0\n\n\n >>> exact_match = datasets.load_metric("exact_match")\n >>> refs = ["the cat", "theater", "YELLING", "agent007"]\n >>> preds = ["cat?", "theater", "yelling", "agent"]\n >>> results = exact_match.compute(references=refs, predictions=preds, regexes_to_ignore=["the ", "yell", "YELL"], ignore_case=True, ignore_punctuation=True)\n >>> print(round(results["exact_match"], 1))\n 75.0\n\n >>> exact_match = datasets.load_metric("exact_match")\n >>> refs = ["the cat", "theater", "YELLING", "agent007"]\n >>> preds = ["cat?", "theater", "yelling", "agent"]\n >>> results = exact_match.compute(references=refs, predictions=preds, regexes_to_ignore=["the ", "yell", "YELL"], ignore_case=True, ignore_punctuation=True, ignore_numbers=True)\n >>> print(round(results["exact_match"], 1))\n 100.0\n\n >>> exact_match = datasets.load_metric("exact_match")\n >>> refs = ["The cat sat on the mat.", "Theaters are great.", "It\'s like comparing oranges and apples."]\n >>> preds = ["The cat sat on the mat?", "Theaters are great.", "It\'s like comparing apples and oranges."]\n >>> results = exact_match.compute(references=refs, predictions=preds)\n >>> print(round(results["exact_match"], 1))\n 33.3\n\n' __lowerCAmelCase = '\n' @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class SCREAMING_SNAKE_CASE ( datasets.Metric ): def SCREAMING_SNAKE_CASE ( self : Optional[Any] ) -> int: return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { '''predictions''': datasets.Value('''string''' , id='''sequence''' ), '''references''': datasets.Value('''string''' , id='''sequence''' ), } ) , reference_urls=[] , ) def SCREAMING_SNAKE_CASE ( self : str , __SCREAMING_SNAKE_CASE : List[Any] , __SCREAMING_SNAKE_CASE : Optional[int] , __SCREAMING_SNAKE_CASE : Dict=None , __SCREAMING_SNAKE_CASE : int=False , __SCREAMING_SNAKE_CASE : Optional[int]=False , __SCREAMING_SNAKE_CASE : Dict=False , ) -> str: if regexes_to_ignore is not None: for s in regexes_to_ignore: a_ : Optional[Any] = np.array([re.sub(__SCREAMING_SNAKE_CASE , '''''' , __SCREAMING_SNAKE_CASE ) for x in predictions] ) a_ : int = np.array([re.sub(__SCREAMING_SNAKE_CASE , '''''' , __SCREAMING_SNAKE_CASE ) for x in references] ) else: a_ : List[str] = np.asarray(__SCREAMING_SNAKE_CASE ) a_ : Any = np.asarray(__SCREAMING_SNAKE_CASE ) if ignore_case: a_ : List[str] = np.char.lower(__SCREAMING_SNAKE_CASE ) a_ : List[Any] = np.char.lower(__SCREAMING_SNAKE_CASE ) if ignore_punctuation: a_ : Any = string.punctuation.maketrans('''''' , '''''' , string.punctuation ) a_ : Union[str, Any] = np.char.translate(__SCREAMING_SNAKE_CASE , table=__SCREAMING_SNAKE_CASE ) a_ : int = np.char.translate(__SCREAMING_SNAKE_CASE , table=__SCREAMING_SNAKE_CASE ) if ignore_numbers: a_ : int = string.digits.maketrans('''''' , '''''' , string.digits ) a_ : Optional[int] = np.char.translate(__SCREAMING_SNAKE_CASE , table=__SCREAMING_SNAKE_CASE ) a_ : Dict = np.char.translate(__SCREAMING_SNAKE_CASE , table=__SCREAMING_SNAKE_CASE ) a_ : Optional[Any] = predictions == references return {"exact_match": np.mean(__SCREAMING_SNAKE_CASE ) * 100}
666
0
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available __lowerCAmelCase = { 'configuration_upernet': ['UperNetConfig'], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __lowerCAmelCase = [ '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 __lowerCAmelCase = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
716
'''simple docstring''' import gc import unittest import numpy as np import torch from diffusers import ( AudioDiffusionPipeline, AutoencoderKL, DDIMScheduler, DDPMScheduler, DiffusionPipeline, Mel, UNetaDConditionModel, UNetaDModel, ) from diffusers.utils import slow, torch_device from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu enable_full_determinism() class SCREAMING_SNAKE_CASE ( unittest.TestCase ): def SCREAMING_SNAKE_CASE ( self : Dict ) -> List[Any]: # clean up the VRAM after each test super().tearDown() gc.collect() torch.cuda.empty_cache() @property def SCREAMING_SNAKE_CASE ( self : Tuple ) -> List[Any]: torch.manual_seed(0 ) a_ : Tuple = UNetaDModel( sample_size=(32, 64) , in_channels=1 , out_channels=1 , layers_per_block=2 , block_out_channels=(128, 128) , down_block_types=('''AttnDownBlock2D''', '''DownBlock2D''') , up_block_types=('''UpBlock2D''', '''AttnUpBlock2D''') , ) return model @property def SCREAMING_SNAKE_CASE ( self : int ) -> List[Any]: torch.manual_seed(0 ) a_ : Any = UNetaDConditionModel( sample_size=(64, 32) , in_channels=1 , out_channels=1 , layers_per_block=2 , block_out_channels=(128, 128) , down_block_types=('''CrossAttnDownBlock2D''', '''DownBlock2D''') , up_block_types=('''UpBlock2D''', '''CrossAttnUpBlock2D''') , cross_attention_dim=10 , ) return model @property def SCREAMING_SNAKE_CASE ( self : Optional[Any] ) -> List[str]: torch.manual_seed(0 ) a_ : List[Any] = AutoencoderKL( sample_size=(128, 64) , in_channels=1 , out_channels=1 , latent_channels=1 , layers_per_block=2 , block_out_channels=(128, 128) , down_block_types=('''DownEncoderBlock2D''', '''DownEncoderBlock2D''') , up_block_types=('''UpDecoderBlock2D''', '''UpDecoderBlock2D''') , ) a_ : List[Any] = UNetaDModel( sample_size=(64, 32) , in_channels=1 , out_channels=1 , layers_per_block=2 , block_out_channels=(128, 128) , down_block_types=('''AttnDownBlock2D''', '''DownBlock2D''') , up_block_types=('''UpBlock2D''', '''AttnUpBlock2D''') , ) return vqvae, unet @slow def SCREAMING_SNAKE_CASE ( self : Union[str, Any] ) -> Union[str, Any]: a_ : Dict = '''cpu''' # ensure determinism for the device-dependent torch.Generator a_ : Union[str, Any] = Mel( x_res=self.dummy_unet.config.sample_size[1] , y_res=self.dummy_unet.config.sample_size[0] , ) a_ : Any = DDPMScheduler() a_ : str = AudioDiffusionPipeline(vqvae=__SCREAMING_SNAKE_CASE , unet=self.dummy_unet , mel=__SCREAMING_SNAKE_CASE , scheduler=__SCREAMING_SNAKE_CASE ) a_ : List[Any] = pipe.to(__SCREAMING_SNAKE_CASE ) pipe.set_progress_bar_config(disable=__SCREAMING_SNAKE_CASE ) a_ : Optional[int] = torch.Generator(device=__SCREAMING_SNAKE_CASE ).manual_seed(42 ) a_ : List[str] = pipe(generator=__SCREAMING_SNAKE_CASE , steps=4 ) a_ : List[Any] = output.audios[0] a_ : Dict = output.images[0] a_ : Dict = torch.Generator(device=__SCREAMING_SNAKE_CASE ).manual_seed(42 ) a_ : Optional[Any] = pipe(generator=__SCREAMING_SNAKE_CASE , steps=4 , return_dict=__SCREAMING_SNAKE_CASE ) a_ : Union[str, Any] = output[0][0] assert audio.shape == (1, (self.dummy_unet.config.sample_size[1] - 1) * mel.hop_length) assert ( image.height == self.dummy_unet.config.sample_size[0] and image.width == self.dummy_unet.config.sample_size[1] ) a_ : Dict = np.frombuffer(image.tobytes() , dtype='''uint8''' )[:10] a_ : str = np.frombuffer(image_from_tuple.tobytes() , dtype='''uint8''' )[:10] a_ : List[str] = np.array([69, 255, 255, 255, 0, 0, 77, 181, 12, 127] ) assert np.abs(image_slice.flatten() - expected_slice ).max() == 0 assert np.abs(image_from_tuple_slice.flatten() - expected_slice ).max() == 0 a_ : str = Mel( x_res=self.dummy_vqvae_and_unet[0].config.sample_size[1] , y_res=self.dummy_vqvae_and_unet[0].config.sample_size[0] , ) a_ : int = DDIMScheduler() a_ : Dict = self.dummy_vqvae_and_unet a_ : List[str] = AudioDiffusionPipeline( vqvae=self.dummy_vqvae_and_unet[0] , unet=dummy_vqvae_and_unet[1] , mel=__SCREAMING_SNAKE_CASE , scheduler=__SCREAMING_SNAKE_CASE ) a_ : Any = pipe.to(__SCREAMING_SNAKE_CASE ) pipe.set_progress_bar_config(disable=__SCREAMING_SNAKE_CASE ) np.random.seed(0 ) a_ : List[str] = np.random.uniform(-1 , 1 , ((dummy_vqvae_and_unet[0].config.sample_size[1] - 1) * mel.hop_length,) ) a_ : int = torch.Generator(device=__SCREAMING_SNAKE_CASE ).manual_seed(42 ) a_ : int = pipe(raw_audio=__SCREAMING_SNAKE_CASE , generator=__SCREAMING_SNAKE_CASE , start_step=5 , steps=10 ) a_ : List[str] = output.images[0] assert ( image.height == self.dummy_vqvae_and_unet[0].config.sample_size[0] and image.width == self.dummy_vqvae_and_unet[0].config.sample_size[1] ) a_ : Optional[Any] = np.frombuffer(image.tobytes() , dtype='''uint8''' )[:10] a_ : List[str] = np.array([120, 117, 110, 109, 138, 167, 138, 148, 132, 121] ) assert np.abs(image_slice.flatten() - expected_slice ).max() == 0 a_ : List[str] = self.dummy_unet_condition a_ : Dict = AudioDiffusionPipeline( vqvae=self.dummy_vqvae_and_unet[0] , unet=__SCREAMING_SNAKE_CASE , mel=__SCREAMING_SNAKE_CASE , scheduler=__SCREAMING_SNAKE_CASE ) a_ : int = pipe.to(__SCREAMING_SNAKE_CASE ) pipe.set_progress_bar_config(disable=__SCREAMING_SNAKE_CASE ) np.random.seed(0 ) a_ : Any = torch.rand((1, 1, 10) ) a_ : Tuple = pipe(generator=__SCREAMING_SNAKE_CASE , encoding=__SCREAMING_SNAKE_CASE ) a_ : Union[str, Any] = output.images[0] a_ : Dict = np.frombuffer(image.tobytes() , dtype='''uint8''' )[:10] a_ : List[str] = np.array([107, 103, 120, 127, 142, 122, 113, 122, 97, 111] ) assert np.abs(image_slice.flatten() - expected_slice ).max() == 0 @slow @require_torch_gpu class SCREAMING_SNAKE_CASE ( unittest.TestCase ): def SCREAMING_SNAKE_CASE ( self : List[str] ) -> Any: # clean up the VRAM after each test super().tearDown() gc.collect() torch.cuda.empty_cache() def SCREAMING_SNAKE_CASE ( self : Optional[Any] ) -> Any: a_ : Any = torch_device a_ : Optional[int] = DiffusionPipeline.from_pretrained('''teticio/audio-diffusion-ddim-256''' ) a_ : Dict = pipe.to(__SCREAMING_SNAKE_CASE ) pipe.set_progress_bar_config(disable=__SCREAMING_SNAKE_CASE ) a_ : str = torch.Generator(device=__SCREAMING_SNAKE_CASE ).manual_seed(42 ) a_ : List[str] = pipe(generator=__SCREAMING_SNAKE_CASE ) a_ : Union[str, Any] = output.audios[0] a_ : Tuple = output.images[0] assert audio.shape == (1, (pipe.unet.config.sample_size[1] - 1) * pipe.mel.hop_length) assert image.height == pipe.unet.config.sample_size[0] and image.width == pipe.unet.config.sample_size[1] a_ : str = np.frombuffer(image.tobytes() , dtype='''uint8''' )[:10] a_ : Tuple = np.array([151, 167, 154, 144, 122, 134, 121, 105, 70, 26] ) assert np.abs(image_slice.flatten() - expected_slice ).max() == 0
666
0
import pytest from datasets.parallel import ParallelBackendConfig, parallel_backend from datasets.utils.py_utils import map_nested from .utils import require_dill_gt_0_3_2, require_joblibspark, require_not_windows def _UpperCAmelCase ( __A : Optional[int] ): # picklable for multiprocessing return i + 1 @require_dill_gt_0_3_2 @require_joblibspark @require_not_windows def _UpperCAmelCase ( ): with parallel_backend('''spark''' ): assert ParallelBackendConfig.backend_name == "spark" a_ : Optional[int] = [1, 2, 3] with pytest.raises(__A ): with parallel_backend('''unsupported backend''' ): map_nested(__A , __A , num_proc=2 ) with pytest.raises(__A ): with parallel_backend('''unsupported backend''' ): map_nested(__A , __A , num_proc=-1 ) @require_dill_gt_0_3_2 @require_joblibspark @require_not_windows @pytest.mark.parametrize('''num_proc''' , [2, -1] ) def _UpperCAmelCase ( __A : Optional[Any] ): a_ : List[Any] = [1, 2] a_ : List[Any] = {'''a''': 1, '''b''': 2} a_ : Union[str, Any] = {'''a''': [1, 2], '''b''': [3, 4]} a_ : Any = {'''a''': {'''1''': 1}, '''b''': 2} a_ : Union[str, Any] = {'''a''': 1, '''b''': 2, '''c''': 3, '''d''': 4} a_ : str = [2, 3] a_ : Optional[int] = {'''a''': 2, '''b''': 3} a_ : List[Any] = {'''a''': [2, 3], '''b''': [4, 5]} a_ : List[str] = {'''a''': {'''1''': 2}, '''b''': 3} a_ : Optional[int] = {'''a''': 2, '''b''': 3, '''c''': 4, '''d''': 5} with parallel_backend('''spark''' ): assert map_nested(__A , __A , num_proc=__A ) == expected_map_nested_sa assert map_nested(__A , __A , num_proc=__A ) == expected_map_nested_sa assert map_nested(__A , __A , num_proc=__A ) == expected_map_nested_sa assert map_nested(__A , __A , num_proc=__A ) == expected_map_nested_sa assert map_nested(__A , __A , num_proc=__A ) == expected_map_nested_sa
717
'''simple docstring''' import argparse import json from pathlib import Path import requests import torch from huggingface_hub import cached_download, hf_hub_download, hf_hub_url from PIL import Image from transformers import DetaConfig, DetaForObjectDetection, DetaImageProcessor, SwinConfig from transformers.utils import logging logging.set_verbosity_info() __lowerCAmelCase = logging.get_logger(__name__) def _UpperCAmelCase ( __A : Union[str, Any] ): a_ : Tuple = SwinConfig( embed_dim=1_92 , depths=(2, 2, 18, 2) , num_heads=(6, 12, 24, 48) , window_size=12 , out_features=['''stage2''', '''stage3''', '''stage4'''] , ) a_ : List[Any] = DetaConfig( backbone_config=__A , num_queries=9_00 , encoder_ffn_dim=20_48 , decoder_ffn_dim=20_48 , num_feature_levels=5 , assign_first_stage=__A , with_box_refine=__A , two_stage=__A , ) # set labels a_ : Optional[Any] = '''huggingface/label-files''' if "o365" in model_name: a_ : Optional[Any] = 3_66 a_ : Tuple = '''object365-id2label.json''' else: a_ : Any = 91 a_ : Union[str, Any] = '''coco-detection-id2label.json''' a_ : Tuple = num_labels a_ : str = json.load(open(cached_download(hf_hub_url(__A , __A , repo_type='''dataset''' ) ) , '''r''' ) ) a_ : Optional[int] = {int(__A ): v for k, v in idalabel.items()} a_ : int = idalabel a_ : Dict = {v: k for k, v in idalabel.items()} return config def _UpperCAmelCase ( __A : List[str] ): a_ : Tuple = [] # stem # fmt: off rename_keys.append(('''backbone.0.body.patch_embed.proj.weight''', '''model.backbone.model.embeddings.patch_embeddings.projection.weight''') ) rename_keys.append(('''backbone.0.body.patch_embed.proj.bias''', '''model.backbone.model.embeddings.patch_embeddings.projection.bias''') ) rename_keys.append(('''backbone.0.body.patch_embed.norm.weight''', '''model.backbone.model.embeddings.norm.weight''') ) rename_keys.append(('''backbone.0.body.patch_embed.norm.bias''', '''model.backbone.model.embeddings.norm.bias''') ) # stages for i in range(len(config.backbone_config.depths ) ): for j in range(config.backbone_config.depths[i] ): rename_keys.append((f'backbone.0.body.layers.{i}.blocks.{j}.norm1.weight', f'model.backbone.model.encoder.layers.{i}.blocks.{j}.layernorm_before.weight') ) rename_keys.append((f'backbone.0.body.layers.{i}.blocks.{j}.norm1.bias', f'model.backbone.model.encoder.layers.{i}.blocks.{j}.layernorm_before.bias') ) rename_keys.append((f'backbone.0.body.layers.{i}.blocks.{j}.attn.relative_position_bias_table', f'model.backbone.model.encoder.layers.{i}.blocks.{j}.attention.self.relative_position_bias_table') ) rename_keys.append((f'backbone.0.body.layers.{i}.blocks.{j}.attn.relative_position_index', f'model.backbone.model.encoder.layers.{i}.blocks.{j}.attention.self.relative_position_index') ) rename_keys.append((f'backbone.0.body.layers.{i}.blocks.{j}.attn.proj.weight', f'model.backbone.model.encoder.layers.{i}.blocks.{j}.attention.output.dense.weight') ) rename_keys.append((f'backbone.0.body.layers.{i}.blocks.{j}.attn.proj.bias', f'model.backbone.model.encoder.layers.{i}.blocks.{j}.attention.output.dense.bias') ) rename_keys.append((f'backbone.0.body.layers.{i}.blocks.{j}.norm2.weight', f'model.backbone.model.encoder.layers.{i}.blocks.{j}.layernorm_after.weight') ) rename_keys.append((f'backbone.0.body.layers.{i}.blocks.{j}.norm2.bias', f'model.backbone.model.encoder.layers.{i}.blocks.{j}.layernorm_after.bias') ) rename_keys.append((f'backbone.0.body.layers.{i}.blocks.{j}.mlp.fc1.weight', f'model.backbone.model.encoder.layers.{i}.blocks.{j}.intermediate.dense.weight') ) rename_keys.append((f'backbone.0.body.layers.{i}.blocks.{j}.mlp.fc1.bias', f'model.backbone.model.encoder.layers.{i}.blocks.{j}.intermediate.dense.bias') ) rename_keys.append((f'backbone.0.body.layers.{i}.blocks.{j}.mlp.fc2.weight', f'model.backbone.model.encoder.layers.{i}.blocks.{j}.output.dense.weight') ) rename_keys.append((f'backbone.0.body.layers.{i}.blocks.{j}.mlp.fc2.bias', f'model.backbone.model.encoder.layers.{i}.blocks.{j}.output.dense.bias') ) if i < 3: rename_keys.append((f'backbone.0.body.layers.{i}.downsample.reduction.weight', f'model.backbone.model.encoder.layers.{i}.downsample.reduction.weight') ) rename_keys.append((f'backbone.0.body.layers.{i}.downsample.norm.weight', f'model.backbone.model.encoder.layers.{i}.downsample.norm.weight') ) rename_keys.append((f'backbone.0.body.layers.{i}.downsample.norm.bias', f'model.backbone.model.encoder.layers.{i}.downsample.norm.bias') ) rename_keys.append(('''backbone.0.body.norm1.weight''', '''model.backbone.model.hidden_states_norms.stage2.weight''') ) rename_keys.append(('''backbone.0.body.norm1.bias''', '''model.backbone.model.hidden_states_norms.stage2.bias''') ) rename_keys.append(('''backbone.0.body.norm2.weight''', '''model.backbone.model.hidden_states_norms.stage3.weight''') ) rename_keys.append(('''backbone.0.body.norm2.bias''', '''model.backbone.model.hidden_states_norms.stage3.bias''') ) rename_keys.append(('''backbone.0.body.norm3.weight''', '''model.backbone.model.hidden_states_norms.stage4.weight''') ) rename_keys.append(('''backbone.0.body.norm3.bias''', '''model.backbone.model.hidden_states_norms.stage4.bias''') ) # transformer encoder for i in range(config.encoder_layers ): rename_keys.append((f'transformer.encoder.layers.{i}.self_attn.sampling_offsets.weight', f'model.encoder.layers.{i}.self_attn.sampling_offsets.weight') ) rename_keys.append((f'transformer.encoder.layers.{i}.self_attn.sampling_offsets.bias', f'model.encoder.layers.{i}.self_attn.sampling_offsets.bias') ) rename_keys.append((f'transformer.encoder.layers.{i}.self_attn.attention_weights.weight', f'model.encoder.layers.{i}.self_attn.attention_weights.weight') ) rename_keys.append((f'transformer.encoder.layers.{i}.self_attn.attention_weights.bias', f'model.encoder.layers.{i}.self_attn.attention_weights.bias') ) rename_keys.append((f'transformer.encoder.layers.{i}.self_attn.value_proj.weight', f'model.encoder.layers.{i}.self_attn.value_proj.weight') ) rename_keys.append((f'transformer.encoder.layers.{i}.self_attn.value_proj.bias', f'model.encoder.layers.{i}.self_attn.value_proj.bias') ) rename_keys.append((f'transformer.encoder.layers.{i}.self_attn.output_proj.weight', f'model.encoder.layers.{i}.self_attn.output_proj.weight') ) rename_keys.append((f'transformer.encoder.layers.{i}.self_attn.output_proj.bias', f'model.encoder.layers.{i}.self_attn.output_proj.bias') ) rename_keys.append((f'transformer.encoder.layers.{i}.norm1.weight', f'model.encoder.layers.{i}.self_attn_layer_norm.weight') ) rename_keys.append((f'transformer.encoder.layers.{i}.norm1.bias', f'model.encoder.layers.{i}.self_attn_layer_norm.bias') ) rename_keys.append((f'transformer.encoder.layers.{i}.linear1.weight', f'model.encoder.layers.{i}.fc1.weight') ) rename_keys.append((f'transformer.encoder.layers.{i}.linear1.bias', f'model.encoder.layers.{i}.fc1.bias') ) rename_keys.append((f'transformer.encoder.layers.{i}.linear2.weight', f'model.encoder.layers.{i}.fc2.weight') ) rename_keys.append((f'transformer.encoder.layers.{i}.linear2.bias', f'model.encoder.layers.{i}.fc2.bias') ) rename_keys.append((f'transformer.encoder.layers.{i}.norm2.weight', f'model.encoder.layers.{i}.final_layer_norm.weight') ) rename_keys.append((f'transformer.encoder.layers.{i}.norm2.bias', f'model.encoder.layers.{i}.final_layer_norm.bias') ) # transformer decoder for i in range(config.decoder_layers ): rename_keys.append((f'transformer.decoder.layers.{i}.cross_attn.sampling_offsets.weight', f'model.decoder.layers.{i}.encoder_attn.sampling_offsets.weight') ) rename_keys.append((f'transformer.decoder.layers.{i}.cross_attn.sampling_offsets.bias', f'model.decoder.layers.{i}.encoder_attn.sampling_offsets.bias') ) rename_keys.append((f'transformer.decoder.layers.{i}.cross_attn.attention_weights.weight', f'model.decoder.layers.{i}.encoder_attn.attention_weights.weight') ) rename_keys.append((f'transformer.decoder.layers.{i}.cross_attn.attention_weights.bias', f'model.decoder.layers.{i}.encoder_attn.attention_weights.bias') ) rename_keys.append((f'transformer.decoder.layers.{i}.cross_attn.value_proj.weight', f'model.decoder.layers.{i}.encoder_attn.value_proj.weight') ) rename_keys.append((f'transformer.decoder.layers.{i}.cross_attn.value_proj.bias', f'model.decoder.layers.{i}.encoder_attn.value_proj.bias') ) rename_keys.append((f'transformer.decoder.layers.{i}.cross_attn.output_proj.weight', f'model.decoder.layers.{i}.encoder_attn.output_proj.weight') ) rename_keys.append((f'transformer.decoder.layers.{i}.cross_attn.output_proj.bias', f'model.decoder.layers.{i}.encoder_attn.output_proj.bias') ) rename_keys.append((f'transformer.decoder.layers.{i}.norm1.weight', f'model.decoder.layers.{i}.encoder_attn_layer_norm.weight') ) rename_keys.append((f'transformer.decoder.layers.{i}.norm1.bias', f'model.decoder.layers.{i}.encoder_attn_layer_norm.bias') ) rename_keys.append((f'transformer.decoder.layers.{i}.self_attn.out_proj.weight', f'model.decoder.layers.{i}.self_attn.out_proj.weight') ) rename_keys.append((f'transformer.decoder.layers.{i}.self_attn.out_proj.bias', f'model.decoder.layers.{i}.self_attn.out_proj.bias') ) rename_keys.append((f'transformer.decoder.layers.{i}.norm2.weight', f'model.decoder.layers.{i}.self_attn_layer_norm.weight') ) rename_keys.append((f'transformer.decoder.layers.{i}.norm2.bias', f'model.decoder.layers.{i}.self_attn_layer_norm.bias') ) rename_keys.append((f'transformer.decoder.layers.{i}.linear1.weight', f'model.decoder.layers.{i}.fc1.weight') ) rename_keys.append((f'transformer.decoder.layers.{i}.linear1.bias', f'model.decoder.layers.{i}.fc1.bias') ) rename_keys.append((f'transformer.decoder.layers.{i}.linear2.weight', f'model.decoder.layers.{i}.fc2.weight') ) rename_keys.append((f'transformer.decoder.layers.{i}.linear2.bias', f'model.decoder.layers.{i}.fc2.bias') ) rename_keys.append((f'transformer.decoder.layers.{i}.norm3.weight', f'model.decoder.layers.{i}.final_layer_norm.weight') ) rename_keys.append((f'transformer.decoder.layers.{i}.norm3.bias', f'model.decoder.layers.{i}.final_layer_norm.bias') ) # fmt: on return rename_keys def _UpperCAmelCase ( __A : str , __A : int , __A : Tuple ): a_ : str = dct.pop(__A ) a_ : Dict = val def _UpperCAmelCase ( __A : List[str] , __A : Optional[int] ): a_ : str = [int(backbone_config.embed_dim * 2**i ) for i in range(len(backbone_config.depths ) )] for i in range(len(backbone_config.depths ) ): a_ : Tuple = num_features[i] for j in range(backbone_config.depths[i] ): # fmt: off # read in weights + bias of input projection layer (in original implementation, this is a single matrix + bias) a_ : List[str] = state_dict.pop(f'backbone.0.body.layers.{i}.blocks.{j}.attn.qkv.weight' ) a_ : str = state_dict.pop(f'backbone.0.body.layers.{i}.blocks.{j}.attn.qkv.bias' ) # next, add query, keys and values (in that order) to the state dict a_ : Optional[Any] = in_proj_weight[:dim, :] a_ : List[Any] = in_proj_bias[: dim] a_ : Optional[Any] = in_proj_weight[ dim : dim * 2, : ] a_ : Union[str, Any] = in_proj_bias[ dim : dim * 2 ] a_ : Optional[int] = in_proj_weight[ -dim :, : ] a_ : int = in_proj_bias[-dim :] # fmt: on def _UpperCAmelCase ( __A : Dict , __A : Dict ): # transformer decoder self-attention layers a_ : Any = config.d_model for i in range(config.decoder_layers ): # read in weights + bias of input projection layer of self-attention a_ : int = state_dict.pop(f'transformer.decoder.layers.{i}.self_attn.in_proj_weight' ) a_ : Any = state_dict.pop(f'transformer.decoder.layers.{i}.self_attn.in_proj_bias' ) # next, add query, keys and values (in that order) to the state dict a_ : Dict = in_proj_weight[:hidden_size, :] a_ : Tuple = in_proj_bias[:hidden_size] a_ : Any = in_proj_weight[ hidden_size : hidden_size * 2, : ] a_ : Tuple = in_proj_bias[hidden_size : hidden_size * 2] a_ : Optional[int] = in_proj_weight[-hidden_size:, :] a_ : int = in_proj_bias[-hidden_size:] def _UpperCAmelCase ( ): a_ : Union[str, Any] = '''http://images.cocodataset.org/val2017/000000039769.jpg''' a_ : List[str] = Image.open(requests.get(__A , stream=__A ).raw ) return im @torch.no_grad() def _UpperCAmelCase ( __A : int , __A : int , __A : Any ): a_ : Union[str, Any] = get_deta_config(__A ) # load original state dict if model_name == "deta-swin-large": a_ : Optional[Any] = hf_hub_download(repo_id='''nielsr/deta-checkpoints''' , filename='''adet_swin_ft.pth''' ) elif model_name == "deta-swin-large-o365": a_ : List[str] = hf_hub_download(repo_id='''jozhang97/deta-swin-l-o365''' , filename='''deta_swin_pt_o365.pth''' ) else: raise ValueError(f'Model name {model_name} not supported' ) a_ : List[Any] = torch.load(__A , map_location='''cpu''' )['''model'''] # original state dict for name, param in state_dict.items(): print(__A , param.shape ) # rename keys a_ : Union[str, Any] = create_rename_keys(__A ) for src, dest in rename_keys: rename_key(__A , __A , __A ) read_in_swin_q_k_v(__A , config.backbone_config ) read_in_decoder_q_k_v(__A , __A ) # fix some prefixes for key in state_dict.copy().keys(): if "transformer.decoder.class_embed" in key or "transformer.decoder.bbox_embed" in key: a_ : Optional[Any] = state_dict.pop(__A ) a_ : int = val if "input_proj" in key: a_ : str = state_dict.pop(__A ) a_ : Optional[Any] = val if "level_embed" in key or "pos_trans" in key or "pix_trans" in key or "enc_output" in key: a_ : List[str] = state_dict.pop(__A ) a_ : List[Any] = val # finally, create HuggingFace model and load state dict a_ : Dict = DetaForObjectDetection(__A ) model.load_state_dict(__A ) model.eval() a_ : int = '''cuda''' if torch.cuda.is_available() else '''cpu''' model.to(__A ) # load image processor a_ : List[Any] = DetaImageProcessor(format='''coco_detection''' ) # verify our conversion on image a_ : Dict = prepare_img() a_ : Optional[int] = processor(images=__A , return_tensors='''pt''' ) a_ : Any = encoding['''pixel_values'''] a_ : int = model(pixel_values.to(__A ) ) # verify logits print('''Logits:''' , outputs.logits[0, :3, :3] ) print('''Boxes:''' , outputs.pred_boxes[0, :3, :3] ) if model_name == "deta-swin-large": a_ : Optional[int] = torch.tensor( [[-7.6308, -2.8485, -5.3737], [-7.2037, -4.5505, -4.8027], [-7.2943, -4.2611, -4.6617]] ) a_ : Tuple = torch.tensor([[0.4987, 0.4969, 0.9999], [0.2549, 0.5498, 0.4805], [0.5498, 0.2757, 0.0569]] ) elif model_name == "deta-swin-large-o365": a_ : Union[str, Any] = torch.tensor( [[-8.0122, -3.5720, -4.9717], [-8.1547, -3.6886, -4.6389], [-7.6610, -3.6194, -5.0134]] ) a_ : Any = torch.tensor([[0.2523, 0.5549, 0.4881], [0.7715, 0.4149, 0.4601], [0.5503, 0.2753, 0.0575]] ) assert torch.allclose(outputs.logits[0, :3, :3] , expected_logits.to(__A ) , atol=1E-4 ) assert torch.allclose(outputs.pred_boxes[0, :3, :3] , expected_boxes.to(__A ) , atol=1E-4 ) print('''Everything ok!''' ) if pytorch_dump_folder_path: # Save model and processor logger.info(f'Saving PyTorch model and processor to {pytorch_dump_folder_path}...' ) Path(__A ).mkdir(exist_ok=__A ) model.save_pretrained(__A ) processor.save_pretrained(__A ) # Push to hub if push_to_hub: print('''Pushing model and processor to hub...''' ) model.push_to_hub(f'jozhang97/{model_name}' ) processor.push_to_hub(f'jozhang97/{model_name}' ) if __name__ == "__main__": __lowerCAmelCase = argparse.ArgumentParser() parser.add_argument( '--model_name', type=str, default='deta-swin-large', choices=['deta-swin-large', 'deta-swin-large-o365'], help='Name of the model you\'d like to convert.', ) parser.add_argument( '--pytorch_dump_folder_path', default=None, type=str, help='Path to the folder to output PyTorch model.', ) parser.add_argument( '--push_to_hub', action='store_true', help='Whether or not to push the converted model to the ๐Ÿค— hub.' ) __lowerCAmelCase = parser.parse_args() convert_deta_checkpoint(args.model_name, args.pytorch_dump_folder_path, args.push_to_hub)
666
0
'''simple docstring''' import argparse import json from tqdm import tqdm def _UpperCAmelCase ( ): a_ : Union[str, Any] = argparse.ArgumentParser() # Required parameters parser.add_argument( '''--src_path''' , type=__A , default='''biencoder-nq-dev.json''' , help='''Path to raw DPR training data''' , ) parser.add_argument( '''--evaluation_set''' , type=__A , help='''where to store parsed evaluation_set file''' , ) parser.add_argument( '''--gold_data_path''' , type=__A , help='''where to store parsed gold_data_path file''' , ) a_ : str = parser.parse_args() with open(args.src_path , '''r''' ) as src_file, open(args.evaluation_set , '''w''' ) as eval_file, open( args.gold_data_path , '''w''' ) as gold_file: a_ : str = json.load(__A ) for dpr_record in tqdm(__A ): a_ : str = dpr_record['''question'''] a_ : Tuple = [context['''title'''] for context in dpr_record['''positive_ctxs''']] eval_file.write(question + '''\n''' ) gold_file.write('''\t'''.join(__A ) + '''\n''' ) if __name__ == "__main__": main()
718
'''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 SCREAMING_SNAKE_CASE ( SCREAMING_SNAKE_CASE_ , unittest.TestCase ): snake_case__ = DDIMPipeline snake_case__ = UNCONDITIONAL_IMAGE_GENERATION_PARAMS snake_case__ = PipelineTesterMixin.required_optional_params - { "num_images_per_prompt", "latents", "callback", "callback_steps", } snake_case__ = UNCONDITIONAL_IMAGE_GENERATION_BATCH_PARAMS snake_case__ = False def SCREAMING_SNAKE_CASE ( self : Any ) -> Optional[Any]: torch.manual_seed(0 ) a_ : int = 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''') , ) a_ : str = DDIMScheduler() a_ : Union[str, Any] = {'''unet''': unet, '''scheduler''': scheduler} return components def SCREAMING_SNAKE_CASE ( self : Dict , __SCREAMING_SNAKE_CASE : str , __SCREAMING_SNAKE_CASE : Tuple=0 ) -> str: if str(__SCREAMING_SNAKE_CASE ).startswith('''mps''' ): a_ : Dict = torch.manual_seed(__SCREAMING_SNAKE_CASE ) else: a_ : Union[str, Any] = torch.Generator(device=__SCREAMING_SNAKE_CASE ).manual_seed(__SCREAMING_SNAKE_CASE ) a_ : Union[str, Any] = { '''batch_size''': 1, '''generator''': generator, '''num_inference_steps''': 2, '''output_type''': '''numpy''', } return inputs def SCREAMING_SNAKE_CASE ( self : Union[str, Any] ) -> Union[str, Any]: a_ : Dict = '''cpu''' a_ : List[Any] = self.get_dummy_components() a_ : List[str] = self.pipeline_class(**__SCREAMING_SNAKE_CASE ) pipe.to(__SCREAMING_SNAKE_CASE ) pipe.set_progress_bar_config(disable=__SCREAMING_SNAKE_CASE ) a_ : Tuple = self.get_dummy_inputs(__SCREAMING_SNAKE_CASE ) a_ : Optional[Any] = pipe(**__SCREAMING_SNAKE_CASE ).images a_ : List[str] = image[0, -3:, -3:, -1] self.assertEqual(image.shape , (1, 32, 32, 3) ) a_ : int = np.array( [1.000e00, 5.717e-01, 4.717e-01, 1.000e00, 0.000e00, 1.000e00, 3.000e-04, 0.000e00, 9.000e-04] ) a_ : Union[str, Any] = np.abs(image_slice.flatten() - expected_slice ).max() self.assertLessEqual(__SCREAMING_SNAKE_CASE , 1e-3 ) def SCREAMING_SNAKE_CASE ( self : List[str] ) -> str: super().test_dict_tuple_outputs_equivalent(expected_max_difference=3e-3 ) def SCREAMING_SNAKE_CASE ( self : str ) -> Optional[int]: super().test_save_load_local(expected_max_difference=3e-3 ) def SCREAMING_SNAKE_CASE ( self : Any ) -> List[str]: super().test_save_load_optional_components(expected_max_difference=3e-3 ) def SCREAMING_SNAKE_CASE ( self : str ) -> Optional[int]: super().test_inference_batch_single_identical(expected_max_diff=3e-3 ) @slow @require_torch_gpu class SCREAMING_SNAKE_CASE ( unittest.TestCase ): def SCREAMING_SNAKE_CASE ( self : str ) -> Any: a_ : Optional[Any] = '''google/ddpm-cifar10-32''' a_ : Optional[Any] = UNetaDModel.from_pretrained(__SCREAMING_SNAKE_CASE ) a_ : Dict = DDIMScheduler() a_ : List[str] = DDIMPipeline(unet=__SCREAMING_SNAKE_CASE , scheduler=__SCREAMING_SNAKE_CASE ) ddim.to(__SCREAMING_SNAKE_CASE ) ddim.set_progress_bar_config(disable=__SCREAMING_SNAKE_CASE ) a_ : Tuple = torch.manual_seed(0 ) a_ : Tuple = ddim(generator=__SCREAMING_SNAKE_CASE , eta=0.0 , output_type='''numpy''' ).images a_ : List[Any] = image[0, -3:, -3:, -1] assert image.shape == (1, 32, 32, 3) a_ : List[str] = np.array([0.1723, 0.1617, 0.1600, 0.1626, 0.1497, 0.1513, 0.1505, 0.1442, 0.1453] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-2 def SCREAMING_SNAKE_CASE ( self : Union[str, Any] ) -> str: a_ : int = '''google/ddpm-ema-bedroom-256''' a_ : str = UNetaDModel.from_pretrained(__SCREAMING_SNAKE_CASE ) a_ : Tuple = DDIMScheduler.from_pretrained(__SCREAMING_SNAKE_CASE ) a_ : Any = DDIMPipeline(unet=__SCREAMING_SNAKE_CASE , scheduler=__SCREAMING_SNAKE_CASE ) ddpm.to(__SCREAMING_SNAKE_CASE ) ddpm.set_progress_bar_config(disable=__SCREAMING_SNAKE_CASE ) a_ : Tuple = torch.manual_seed(0 ) a_ : List[Any] = ddpm(generator=__SCREAMING_SNAKE_CASE , output_type='''numpy''' ).images a_ : Optional[int] = image[0, -3:, -3:, -1] assert image.shape == (1, 256, 256, 3) a_ : Optional[Any] = np.array([0.0060, 0.0201, 0.0344, 0.0024, 0.0018, 0.0002, 0.0022, 0.0000, 0.0069] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-2
666
0
'''simple docstring''' import os from huggingface_hub.constants import HUGGINGFACE_HUB_CACHE, hf_cache_home __lowerCAmelCase = HUGGINGFACE_HUB_CACHE __lowerCAmelCase = 'config.json' __lowerCAmelCase = 'diffusion_pytorch_model.bin' __lowerCAmelCase = 'diffusion_flax_model.msgpack' __lowerCAmelCase = 'model.onnx' __lowerCAmelCase = 'diffusion_pytorch_model.safetensors' __lowerCAmelCase = 'weights.pb' __lowerCAmelCase = 'https://huggingface.co' __lowerCAmelCase = default_cache_path __lowerCAmelCase = 'diffusers_modules' __lowerCAmelCase = os.getenv('HF_MODULES_CACHE', os.path.join(hf_cache_home, 'modules')) __lowerCAmelCase = ['fp16', 'non-ema'] __lowerCAmelCase = '.self_attn'
719
'''simple docstring''' from dataclasses import dataclass, field from typing import TYPE_CHECKING, Any, ClassVar, Dict, List, Optional, Union import pyarrow as pa if TYPE_CHECKING: from .features import FeatureType @dataclass class SCREAMING_SNAKE_CASE : snake_case__ = 42 snake_case__ = None # Automatically constructed snake_case__ = "dict" snake_case__ = None snake_case__ = field(default="Translation" , init=SCREAMING_SNAKE_CASE_ , repr=SCREAMING_SNAKE_CASE_ ) def __call__( self : Dict ) -> Tuple: return pa.struct({lang: pa.string() for lang in sorted(self.languages )} ) def SCREAMING_SNAKE_CASE ( self : Union[str, Any] ) -> Union["FeatureType", Dict[str, "FeatureType"]]: from .features import Value return {k: Value('''string''' ) for k in sorted(self.languages )} @dataclass class SCREAMING_SNAKE_CASE : snake_case__ = None snake_case__ = None snake_case__ = None # Automatically constructed snake_case__ = "dict" snake_case__ = None snake_case__ = field(default="TranslationVariableLanguages" , init=SCREAMING_SNAKE_CASE_ , repr=SCREAMING_SNAKE_CASE_ ) def SCREAMING_SNAKE_CASE ( self : Tuple ) -> Dict: a_ : List[str] = sorted(set(self.languages ) ) if self.languages else None a_ : Optional[Any] = len(self.languages ) if self.languages else None def __call__( self : Any ) -> Optional[Any]: return pa.struct({'''language''': pa.list_(pa.string() ), '''translation''': pa.list_(pa.string() )} ) def SCREAMING_SNAKE_CASE ( self : Optional[int] , __SCREAMING_SNAKE_CASE : Optional[Any] ) -> Optional[Any]: a_ : str = set(self.languages ) if self.languages and set(__SCREAMING_SNAKE_CASE ) - lang_set: raise ValueError( f'Some languages in example ({", ".join(sorted(set(__SCREAMING_SNAKE_CASE ) - lang_set ) )}) are not in valid set ({", ".join(__SCREAMING_SNAKE_CASE )}).' ) # Convert dictionary into tuples, splitting out cases where there are # multiple translations for a single language. a_ : int = [] for lang, text in translation_dict.items(): if isinstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): translation_tuples.append((lang, text) ) else: translation_tuples.extend([(lang, el) for el in text] ) # Ensure translations are in ascending order by language code. a_ , a_ : List[Any] = zip(*sorted(__SCREAMING_SNAKE_CASE ) ) return {"language": languages, "translation": translations} def SCREAMING_SNAKE_CASE ( self : Any ) -> Union["FeatureType", Dict[str, "FeatureType"]]: from .features import Sequence, Value return { "language": Sequence(Value('''string''' ) ), "translation": Sequence(Value('''string''' ) ), }
666
0
'''simple docstring''' from ...configuration_utils import PretrainedConfig from ...utils import logging __lowerCAmelCase = logging.get_logger(__name__) __lowerCAmelCase = { 'transfo-xl-wt103': 'https://huggingface.co/transfo-xl-wt103/resolve/main/config.json', } class SCREAMING_SNAKE_CASE ( SCREAMING_SNAKE_CASE_ ): snake_case__ = "transfo-xl" snake_case__ = ["mems"] snake_case__ = { "n_token": "vocab_size", "hidden_size": "d_model", "num_attention_heads": "n_head", "num_hidden_layers": "n_layer", } def __init__( self : Optional[Any] , __SCREAMING_SNAKE_CASE : Optional[Any]=26_7735 , __SCREAMING_SNAKE_CASE : List[str]=[2_0000, 4_0000, 20_0000] , __SCREAMING_SNAKE_CASE : Any=1024 , __SCREAMING_SNAKE_CASE : int=1024 , __SCREAMING_SNAKE_CASE : str=16 , __SCREAMING_SNAKE_CASE : Tuple=64 , __SCREAMING_SNAKE_CASE : Any=4096 , __SCREAMING_SNAKE_CASE : List[Any]=4 , __SCREAMING_SNAKE_CASE : List[Any]=False , __SCREAMING_SNAKE_CASE : Any=18 , __SCREAMING_SNAKE_CASE : List[Any]=1600 , __SCREAMING_SNAKE_CASE : Any=1000 , __SCREAMING_SNAKE_CASE : Optional[int]=True , __SCREAMING_SNAKE_CASE : List[str]=True , __SCREAMING_SNAKE_CASE : Optional[int]=0 , __SCREAMING_SNAKE_CASE : Optional[int]=-1 , __SCREAMING_SNAKE_CASE : Optional[Any]=True , __SCREAMING_SNAKE_CASE : List[Any]=0.1 , __SCREAMING_SNAKE_CASE : List[str]=0.0 , __SCREAMING_SNAKE_CASE : List[str]=True , __SCREAMING_SNAKE_CASE : List[Any]="normal" , __SCREAMING_SNAKE_CASE : Optional[Any]=0.01 , __SCREAMING_SNAKE_CASE : Dict=0.01 , __SCREAMING_SNAKE_CASE : str=0.02 , __SCREAMING_SNAKE_CASE : Dict=1e-5 , __SCREAMING_SNAKE_CASE : List[str]=0 , **__SCREAMING_SNAKE_CASE : Union[str, Any] , ) -> Any: a_ : Union[str, Any] = vocab_size a_ : Dict = [] self.cutoffs.extend(__SCREAMING_SNAKE_CASE ) if proj_share_all_but_first: a_ : Tuple = [False] + [True] * len(self.cutoffs ) else: a_ : int = [False] + [False] * len(self.cutoffs ) a_ : Dict = d_model a_ : Tuple = d_embed a_ : Union[str, Any] = d_head a_ : Optional[int] = d_inner a_ : Union[str, Any] = div_val a_ : int = pre_lnorm a_ : List[Any] = n_layer a_ : List[Any] = n_head a_ : Dict = mem_len a_ : List[Any] = same_length a_ : Dict = attn_type a_ : Dict = clamp_len a_ : Union[str, Any] = sample_softmax a_ : Dict = adaptive a_ : Optional[Any] = dropout a_ : str = dropatt a_ : Optional[Any] = untie_r a_ : str = init a_ : Optional[Any] = init_range a_ : int = proj_init_std a_ : Union[str, Any] = init_std a_ : List[str] = layer_norm_epsilon super().__init__(eos_token_id=__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE ) @property def SCREAMING_SNAKE_CASE ( self : Any ) -> int: # Message copied from Transformer-XL documentation 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 SCREAMING_SNAKE_CASE ( self : Union[str, Any] , __SCREAMING_SNAKE_CASE : Optional[Any] ) -> Optional[Any]: # 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.' )
720
'''simple docstring''' import json import os import pickle import shutil import tempfile from unittest import TestCase from unittest.mock import patch import numpy as np from datasets import Dataset from transformers import is_faiss_available from transformers.models.bart.configuration_bart import BartConfig from transformers.models.bart.tokenization_bart import BartTokenizer from transformers.models.bert.tokenization_bert import VOCAB_FILES_NAMES as DPR_VOCAB_FILES_NAMES from transformers.models.dpr.configuration_dpr import DPRConfig from transformers.models.dpr.tokenization_dpr import DPRContextEncoderTokenizer, DPRQuestionEncoderTokenizer from transformers.models.rag.configuration_rag import RagConfig from transformers.models.rag.retrieval_rag import CustomHFIndex, RagRetriever from transformers.models.roberta.tokenization_roberta import VOCAB_FILES_NAMES as BART_VOCAB_FILES_NAMES from transformers.testing_utils import require_faiss, require_sentencepiece, require_tokenizers, require_torch if is_faiss_available(): import faiss @require_faiss class SCREAMING_SNAKE_CASE ( SCREAMING_SNAKE_CASE_ ): def SCREAMING_SNAKE_CASE ( self : Dict ) -> Dict: a_ : Union[str, Any] = tempfile.mkdtemp() a_ : Union[str, Any] = 8 # DPR tok a_ : Dict = [ '''[UNK]''', '''[CLS]''', '''[SEP]''', '''[PAD]''', '''[MASK]''', '''want''', '''##want''', '''##ed''', '''wa''', '''un''', '''runn''', '''##ing''', ''',''', '''low''', '''lowest''', ] a_ : str = os.path.join(self.tmpdirname , '''dpr_tokenizer''' ) os.makedirs(__SCREAMING_SNAKE_CASE , exist_ok=__SCREAMING_SNAKE_CASE ) a_ : Optional[int] = os.path.join(__SCREAMING_SNAKE_CASE , DPR_VOCAB_FILES_NAMES['''vocab_file'''] ) with open(self.vocab_file , '''w''' , encoding='''utf-8''' ) as vocab_writer: vocab_writer.write(''''''.join([x + '''\n''' for x in vocab_tokens] ) ) # BART tok a_ : Union[str, Any] = [ '''l''', '''o''', '''w''', '''e''', '''r''', '''s''', '''t''', '''i''', '''d''', '''n''', '''\u0120''', '''\u0120l''', '''\u0120n''', '''\u0120lo''', '''\u0120low''', '''er''', '''\u0120lowest''', '''\u0120newer''', '''\u0120wider''', '''<unk>''', ] a_ : int = dict(zip(__SCREAMING_SNAKE_CASE , range(len(__SCREAMING_SNAKE_CASE ) ) ) ) a_ : int = ['''#version: 0.2''', '''\u0120 l''', '''\u0120l o''', '''\u0120lo w''', '''e r''', ''''''] a_ : Optional[int] = {'''unk_token''': '''<unk>'''} a_ : List[str] = os.path.join(self.tmpdirname , '''bart_tokenizer''' ) os.makedirs(__SCREAMING_SNAKE_CASE , exist_ok=__SCREAMING_SNAKE_CASE ) a_ : Tuple = os.path.join(__SCREAMING_SNAKE_CASE , BART_VOCAB_FILES_NAMES['''vocab_file'''] ) a_ : int = os.path.join(__SCREAMING_SNAKE_CASE , BART_VOCAB_FILES_NAMES['''merges_file'''] ) with open(self.vocab_file , '''w''' , encoding='''utf-8''' ) as fp: fp.write(json.dumps(__SCREAMING_SNAKE_CASE ) + '''\n''' ) with open(self.merges_file , '''w''' , encoding='''utf-8''' ) as fp: fp.write('''\n'''.join(__SCREAMING_SNAKE_CASE ) ) def SCREAMING_SNAKE_CASE ( self : Tuple ) -> DPRQuestionEncoderTokenizer: return DPRQuestionEncoderTokenizer.from_pretrained(os.path.join(self.tmpdirname , '''dpr_tokenizer''' ) ) def SCREAMING_SNAKE_CASE ( self : str ) -> DPRContextEncoderTokenizer: return DPRContextEncoderTokenizer.from_pretrained(os.path.join(self.tmpdirname , '''dpr_tokenizer''' ) ) def SCREAMING_SNAKE_CASE ( self : Optional[int] ) -> BartTokenizer: return BartTokenizer.from_pretrained(os.path.join(self.tmpdirname , '''bart_tokenizer''' ) ) def SCREAMING_SNAKE_CASE ( self : List[Any] ) -> Union[str, Any]: shutil.rmtree(self.tmpdirname ) def SCREAMING_SNAKE_CASE ( self : Dict ) -> Union[str, Any]: a_ : str = Dataset.from_dict( { '''id''': ['''0''', '''1'''], '''text''': ['''foo''', '''bar'''], '''title''': ['''Foo''', '''Bar'''], '''embeddings''': [np.ones(self.retrieval_vector_size ), 2 * np.ones(self.retrieval_vector_size )], } ) dataset.add_faiss_index('''embeddings''' , string_factory='''Flat''' , metric_type=faiss.METRIC_INNER_PRODUCT ) return dataset def SCREAMING_SNAKE_CASE ( self : List[str] ) -> Tuple: a_ : List[str] = self.get_dummy_dataset() a_ : Tuple = RagConfig( retrieval_vector_size=self.retrieval_vector_size , question_encoder=DPRConfig().to_dict() , generator=BartConfig().to_dict() , ) with patch('''transformers.models.rag.retrieval_rag.load_dataset''' ) as mock_load_dataset: a_ : Tuple = dataset a_ : Any = RagRetriever( __SCREAMING_SNAKE_CASE , question_encoder_tokenizer=self.get_dpr_tokenizer() , generator_tokenizer=self.get_bart_tokenizer() , ) return retriever def SCREAMING_SNAKE_CASE ( self : Dict , __SCREAMING_SNAKE_CASE : bool ) -> Dict: a_ : Dict = self.get_dummy_dataset() a_ : Dict = RagConfig( retrieval_vector_size=self.retrieval_vector_size , question_encoder=DPRConfig().to_dict() , generator=BartConfig().to_dict() , index_name='''custom''' , ) if from_disk: a_ : Optional[int] = os.path.join(self.tmpdirname , '''dataset''' ) a_ : str = os.path.join(self.tmpdirname , '''index.faiss''' ) dataset.get_index('''embeddings''' ).save(os.path.join(self.tmpdirname , '''index.faiss''' ) ) dataset.drop_index('''embeddings''' ) dataset.save_to_disk(os.path.join(self.tmpdirname , '''dataset''' ) ) del dataset a_ : int = RagRetriever( __SCREAMING_SNAKE_CASE , question_encoder_tokenizer=self.get_dpr_tokenizer() , generator_tokenizer=self.get_bart_tokenizer() , ) else: a_ : Optional[Any] = RagRetriever( __SCREAMING_SNAKE_CASE , question_encoder_tokenizer=self.get_dpr_tokenizer() , generator_tokenizer=self.get_bart_tokenizer() , index=CustomHFIndex(config.retrieval_vector_size , __SCREAMING_SNAKE_CASE ) , ) return retriever def SCREAMING_SNAKE_CASE ( self : Optional[Any] ) -> Optional[int]: a_ : str = Dataset.from_dict( { '''id''': ['''0''', '''1'''], '''text''': ['''foo''', '''bar'''], '''title''': ['''Foo''', '''Bar'''], '''embeddings''': [np.ones(self.retrieval_vector_size + 1 ), 2 * np.ones(self.retrieval_vector_size + 1 )], } ) dataset.add_faiss_index('''embeddings''' , string_factory='''Flat''' , metric_type=faiss.METRIC_INNER_PRODUCT ) a_ : Optional[int] = os.path.join(self.tmpdirname , '''hf_bert_base.hnswSQ8_correct_phi_128.c_index''' ) dataset.save_faiss_index('''embeddings''' , index_file_name + '''.index.dpr''' ) pickle.dump(dataset['''id'''] , open(index_file_name + '''.index_meta.dpr''' , '''wb''' ) ) a_ : Union[str, Any] = os.path.join(self.tmpdirname , '''psgs_w100.tsv.pkl''' ) a_ : Dict = {sample['''id''']: [sample['''text'''], sample['''title''']] for sample in dataset} pickle.dump(__SCREAMING_SNAKE_CASE , open(__SCREAMING_SNAKE_CASE , '''wb''' ) ) a_ : Optional[Any] = RagConfig( retrieval_vector_size=self.retrieval_vector_size , question_encoder=DPRConfig().to_dict() , generator=BartConfig().to_dict() , index_name='''legacy''' , index_path=self.tmpdirname , ) a_ : int = RagRetriever( __SCREAMING_SNAKE_CASE , question_encoder_tokenizer=self.get_dpr_tokenizer() , generator_tokenizer=self.get_bart_tokenizer() ) return retriever def SCREAMING_SNAKE_CASE ( self : Tuple ) -> Dict: a_ : Optional[Any] = 1 a_ : Dict = self.get_dummy_canonical_hf_index_retriever() a_ : Tuple = np.array( [np.ones(self.retrieval_vector_size ), -np.ones(self.retrieval_vector_size )] , dtype=np.floataa ) a_ , a_ , a_ : str = retriever.retrieve(__SCREAMING_SNAKE_CASE , n_docs=__SCREAMING_SNAKE_CASE ) self.assertEqual(retrieved_doc_embeds.shape , (2, n_docs, self.retrieval_vector_size) ) self.assertEqual(len(__SCREAMING_SNAKE_CASE ) , 2 ) self.assertEqual(sorted(doc_dicts[0] ) , ['''embeddings''', '''id''', '''text''', '''title'''] ) self.assertEqual(len(doc_dicts[0]['''id'''] ) , __SCREAMING_SNAKE_CASE ) self.assertEqual(doc_dicts[0]['''id'''][0] , '''1''' ) # max inner product is reached with second doc self.assertEqual(doc_dicts[1]['''id'''][0] , '''0''' ) # max inner product is reached with first doc self.assertListEqual(doc_ids.tolist() , [[1], [0]] ) def SCREAMING_SNAKE_CASE ( self : Tuple ) -> Dict: a_ : str = self.get_dummy_canonical_hf_index_retriever() with tempfile.TemporaryDirectory() as tmp_dirname: with patch('''transformers.models.rag.retrieval_rag.load_dataset''' ) as mock_load_dataset: a_ : List[str] = self.get_dummy_dataset() retriever.save_pretrained(__SCREAMING_SNAKE_CASE ) a_ : Optional[Any] = RagRetriever.from_pretrained(__SCREAMING_SNAKE_CASE ) self.assertIsInstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) a_ : Union[str, Any] = np.array( [np.ones(self.retrieval_vector_size ), -np.ones(self.retrieval_vector_size )] , dtype=np.floataa ) a_ : List[str] = retriever.retrieve(__SCREAMING_SNAKE_CASE , n_docs=1 ) self.assertTrue(out is not None ) def SCREAMING_SNAKE_CASE ( self : Dict ) -> int: a_ : Union[str, Any] = 1 a_ : Optional[Any] = self.get_dummy_custom_hf_index_retriever(from_disk=__SCREAMING_SNAKE_CASE ) a_ : List[Any] = np.array( [np.ones(self.retrieval_vector_size ), -np.ones(self.retrieval_vector_size )] , dtype=np.floataa ) a_ , a_ , a_ : Any = retriever.retrieve(__SCREAMING_SNAKE_CASE , n_docs=__SCREAMING_SNAKE_CASE ) self.assertEqual(retrieved_doc_embeds.shape , (2, n_docs, self.retrieval_vector_size) ) self.assertEqual(len(__SCREAMING_SNAKE_CASE ) , 2 ) self.assertEqual(sorted(doc_dicts[0] ) , ['''embeddings''', '''id''', '''text''', '''title'''] ) self.assertEqual(len(doc_dicts[0]['''id'''] ) , __SCREAMING_SNAKE_CASE ) self.assertEqual(doc_dicts[0]['''id'''][0] , '''1''' ) # max inner product is reached with second doc self.assertEqual(doc_dicts[1]['''id'''][0] , '''0''' ) # max inner product is reached with first doc self.assertListEqual(doc_ids.tolist() , [[1], [0]] ) def SCREAMING_SNAKE_CASE ( self : str ) -> List[str]: a_ : Dict = self.get_dummy_custom_hf_index_retriever(from_disk=__SCREAMING_SNAKE_CASE ) with tempfile.TemporaryDirectory() as tmp_dirname: retriever.save_pretrained(__SCREAMING_SNAKE_CASE ) a_ : List[str] = RagRetriever.from_pretrained(__SCREAMING_SNAKE_CASE ) self.assertIsInstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) a_ : Tuple = np.array( [np.ones(self.retrieval_vector_size ), -np.ones(self.retrieval_vector_size )] , dtype=np.floataa ) a_ : Dict = retriever.retrieve(__SCREAMING_SNAKE_CASE , n_docs=1 ) self.assertTrue(out is not None ) def SCREAMING_SNAKE_CASE ( self : Tuple ) -> Optional[int]: a_ : Union[str, Any] = 1 a_ : str = self.get_dummy_custom_hf_index_retriever(from_disk=__SCREAMING_SNAKE_CASE ) a_ : Optional[int] = np.array( [np.ones(self.retrieval_vector_size ), -np.ones(self.retrieval_vector_size )] , dtype=np.floataa ) a_ , a_ , a_ : Tuple = retriever.retrieve(__SCREAMING_SNAKE_CASE , n_docs=__SCREAMING_SNAKE_CASE ) self.assertEqual(retrieved_doc_embeds.shape , (2, n_docs, self.retrieval_vector_size) ) self.assertEqual(len(__SCREAMING_SNAKE_CASE ) , 2 ) self.assertEqual(sorted(doc_dicts[0] ) , ['''embeddings''', '''id''', '''text''', '''title'''] ) self.assertEqual(len(doc_dicts[0]['''id'''] ) , __SCREAMING_SNAKE_CASE ) self.assertEqual(doc_dicts[0]['''id'''][0] , '''1''' ) # max inner product is reached with second doc self.assertEqual(doc_dicts[1]['''id'''][0] , '''0''' ) # max inner product is reached with first doc self.assertListEqual(doc_ids.tolist() , [[1], [0]] ) def SCREAMING_SNAKE_CASE ( self : int ) -> Dict: a_ : List[str] = self.get_dummy_custom_hf_index_retriever(from_disk=__SCREAMING_SNAKE_CASE ) with tempfile.TemporaryDirectory() as tmp_dirname: retriever.save_pretrained(__SCREAMING_SNAKE_CASE ) a_ : Any = RagRetriever.from_pretrained(__SCREAMING_SNAKE_CASE ) self.assertIsInstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) a_ : Tuple = np.array( [np.ones(self.retrieval_vector_size ), -np.ones(self.retrieval_vector_size )] , dtype=np.floataa ) a_ : Dict = retriever.retrieve(__SCREAMING_SNAKE_CASE , n_docs=1 ) self.assertTrue(out is not None ) def SCREAMING_SNAKE_CASE ( self : Optional[int] ) -> Any: a_ : str = 1 a_ : Tuple = self.get_dummy_legacy_index_retriever() a_ : Union[str, Any] = np.array( [np.ones(self.retrieval_vector_size ), -np.ones(self.retrieval_vector_size )] , dtype=np.floataa ) a_ , a_ , a_ : Any = retriever.retrieve(__SCREAMING_SNAKE_CASE , n_docs=__SCREAMING_SNAKE_CASE ) self.assertEqual(retrieved_doc_embeds.shape , (2, n_docs, self.retrieval_vector_size) ) self.assertEqual(len(__SCREAMING_SNAKE_CASE ) , 2 ) self.assertEqual(sorted(doc_dicts[0] ) , ['''text''', '''title'''] ) self.assertEqual(len(doc_dicts[0]['''text'''] ) , __SCREAMING_SNAKE_CASE ) self.assertEqual(doc_dicts[0]['''text'''][0] , '''bar''' ) # max inner product is reached with second doc self.assertEqual(doc_dicts[1]['''text'''][0] , '''foo''' ) # max inner product is reached with first doc self.assertListEqual(doc_ids.tolist() , [[1], [0]] ) def SCREAMING_SNAKE_CASE ( self : int ) -> Optional[int]: a_ : List[str] = self.get_dummy_legacy_index_retriever() with tempfile.TemporaryDirectory() as tmp_dirname: retriever.save_pretrained(__SCREAMING_SNAKE_CASE ) a_ : Any = RagRetriever.from_pretrained(__SCREAMING_SNAKE_CASE ) self.assertIsInstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) a_ : List[str] = np.array( [np.ones(self.retrieval_vector_size ), -np.ones(self.retrieval_vector_size )] , dtype=np.floataa ) a_ : Optional[Any] = retriever.retrieve(__SCREAMING_SNAKE_CASE , n_docs=1 ) self.assertTrue(out is not None ) @require_torch @require_tokenizers @require_sentencepiece def SCREAMING_SNAKE_CASE ( self : int ) -> List[str]: import torch a_ : Any = 1 a_ : List[Any] = self.get_dummy_canonical_hf_index_retriever() a_ : Union[str, Any] = [[5, 7], [10, 11]] a_ : Optional[int] = np.array( [np.ones(self.retrieval_vector_size ), -np.ones(self.retrieval_vector_size )] , dtype=np.floataa ) a_ : str = retriever(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , prefix=retriever.config.generator.prefix , n_docs=__SCREAMING_SNAKE_CASE ) a_ , a_ , a_ : List[str] = ( out['''context_input_ids'''], out['''context_attention_mask'''], out['''retrieved_doc_embeds'''], ) self.assertEqual(retrieved_doc_embeds.shape , (2, n_docs, self.retrieval_vector_size) ) self.assertIsInstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) self.assertIsInstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) self.assertIsInstance(__SCREAMING_SNAKE_CASE , np.ndarray ) a_ : Any = retriever( __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , prefix=retriever.config.generator.prefix , n_docs=__SCREAMING_SNAKE_CASE , return_tensors='''pt''' , ) a_ , a_ , a_ , a_ : str = ( # noqa: F841 out['''context_input_ids'''], out['''context_attention_mask'''], out['''retrieved_doc_embeds'''], out['''doc_ids'''], ) self.assertEqual(retrieved_doc_embeds.shape , (2, n_docs, self.retrieval_vector_size) ) self.assertIsInstance(__SCREAMING_SNAKE_CASE , torch.Tensor ) self.assertIsInstance(__SCREAMING_SNAKE_CASE , torch.Tensor ) self.assertIsInstance(__SCREAMING_SNAKE_CASE , torch.Tensor ) @require_torch @require_tokenizers @require_sentencepiece def SCREAMING_SNAKE_CASE ( self : Union[str, Any] ) -> Any: a_ : str = self.get_dpr_ctx_encoder_tokenizer() a_ : Tuple = 1 a_ : Any = self.get_dummy_custom_hf_index_retriever(from_disk=__SCREAMING_SNAKE_CASE ) retriever.set_ctx_encoder_tokenizer(__SCREAMING_SNAKE_CASE ) a_ : Dict = [[5, 7], [10, 11]] a_ : List[str] = np.array( [np.ones(self.retrieval_vector_size ), -np.ones(self.retrieval_vector_size )] , dtype=np.floataa ) a_ : List[Any] = retriever(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , prefix=retriever.config.generator.prefix , n_docs=__SCREAMING_SNAKE_CASE ) self.assertEqual( len(__SCREAMING_SNAKE_CASE ) , 6 ) # check whether the retriever output consist of 6 attributes including tokenized docs self.assertEqual( all(k in out for k in ('''tokenized_doc_ids''', '''tokenized_doc_attention_mask''') ) , __SCREAMING_SNAKE_CASE ) # check for doc token related keys in dictionary.
666
0
'''simple docstring''' from __future__ import annotations import unittest from transformers import is_tf_available from transformers.testing_utils import require_sentencepiece, require_tf, require_tokenizers, slow if is_tf_available(): import numpy as np import tensorflow as tf from transformers import TFXLMRobertaModel @require_tf @require_sentencepiece @require_tokenizers class SCREAMING_SNAKE_CASE ( unittest.TestCase ): @slow def SCREAMING_SNAKE_CASE ( self : Union[str, Any] ) -> Optional[Any]: a_ : Union[str, Any] = TFXLMRobertaModel.from_pretrained('''jplu/tf-xlm-roberta-base''' ) a_ : str = { '''input_ids''': tf.convert_to_tensor([[0, 2646, 1_0269, 83, 9_9942, 2]] , dtype=tf.intaa ), # "My dog is cute" '''attention_mask''': tf.convert_to_tensor([[1, 1, 1, 1, 1, 1]] , dtype=tf.intaa ), } a_ : str = model(__SCREAMING_SNAKE_CASE )['''last_hidden_state'''] a_ : Optional[int] = tf.TensorShape((1, 6, 768) ) self.assertEqual(output.shape , __SCREAMING_SNAKE_CASE ) # compare the actual values for a slice. a_ : Tuple = tf.convert_to_tensor( [ [ [0.068_1762, 0.1089_4451, 0.0677_2504], [-0.0642_3668, 0.0236_6615, 0.0432_9344], [-0.0605_7295, 0.0997_4135, -0.0007_0584], ] ] , dtype=tf.floataa , ) self.assertTrue(np.allclose(output[:, :3, :3].numpy() , expected_slice.numpy() , atol=1e-4 ) )
721
'''simple docstring''' from math import pi, sqrt, tan def _UpperCAmelCase ( __A : float ): if side_length < 0: raise ValueError('''surface_area_cube() only accepts non-negative values''' ) return 6 * side_length**2 def _UpperCAmelCase ( __A : float , __A : float , __A : float ): if length < 0 or breadth < 0 or height < 0: raise ValueError('''surface_area_cuboid() only accepts non-negative values''' ) return 2 * ((length * breadth) + (breadth * height) + (length * height)) def _UpperCAmelCase ( __A : float ): if radius < 0: raise ValueError('''surface_area_sphere() only accepts non-negative values''' ) return 4 * pi * radius**2 def _UpperCAmelCase ( __A : float ): if radius < 0: raise ValueError('''surface_area_hemisphere() only accepts non-negative values''' ) return 3 * pi * radius**2 def _UpperCAmelCase ( __A : float , __A : float ): if radius < 0 or height < 0: raise ValueError('''surface_area_cone() only accepts non-negative values''' ) return pi * radius * (radius + (height**2 + radius**2) ** 0.5) def _UpperCAmelCase ( __A : float , __A : float , __A : float ): if radius_a < 0 or radius_a < 0 or height < 0: raise ValueError( '''surface_area_conical_frustum() only accepts non-negative values''' ) a_ : Any = (height**2 + (radius_a - radius_a) ** 2) ** 0.5 return pi * ((slant_height * (radius_a + radius_a)) + radius_a**2 + radius_a**2) def _UpperCAmelCase ( __A : float , __A : float ): if radius < 0 or height < 0: raise ValueError('''surface_area_cylinder() only accepts non-negative values''' ) return 2 * pi * radius * (height + radius) def _UpperCAmelCase ( __A : float , __A : float ): if torus_radius < 0 or tube_radius < 0: raise ValueError('''surface_area_torus() only accepts non-negative values''' ) if torus_radius < tube_radius: raise ValueError( '''surface_area_torus() does not support spindle or self intersecting tori''' ) return 4 * pow(__A , 2 ) * torus_radius * tube_radius def _UpperCAmelCase ( __A : float , __A : float ): if length < 0 or width < 0: raise ValueError('''area_rectangle() only accepts non-negative values''' ) return length * width def _UpperCAmelCase ( __A : float ): if side_length < 0: raise ValueError('''area_square() only accepts non-negative values''' ) return side_length**2 def _UpperCAmelCase ( __A : float , __A : float ): if base < 0 or height < 0: raise ValueError('''area_triangle() only accepts non-negative values''' ) return (base * height) / 2 def _UpperCAmelCase ( __A : float , __A : float , __A : float ): if sidea < 0 or sidea < 0 or sidea < 0: raise ValueError('''area_triangle_three_sides() only accepts non-negative values''' ) elif sidea + sidea < sidea or sidea + sidea < sidea or sidea + sidea < sidea: raise ValueError('''Given three sides do not form a triangle''' ) a_ : int = (sidea + sidea + sidea) / 2 a_ : Optional[Any] = sqrt( semi_perimeter * (semi_perimeter - sidea) * (semi_perimeter - sidea) * (semi_perimeter - sidea) ) return area def _UpperCAmelCase ( __A : float , __A : float ): if base < 0 or height < 0: raise ValueError('''area_parallelogram() only accepts non-negative values''' ) return base * height def _UpperCAmelCase ( __A : float , __A : float , __A : float ): if basea < 0 or basea < 0 or height < 0: raise ValueError('''area_trapezium() only accepts non-negative values''' ) return 1 / 2 * (basea + basea) * height def _UpperCAmelCase ( __A : float ): if radius < 0: raise ValueError('''area_circle() only accepts non-negative values''' ) return pi * radius**2 def _UpperCAmelCase ( __A : float , __A : float ): if radius_x < 0 or radius_y < 0: raise ValueError('''area_ellipse() only accepts non-negative values''' ) return pi * radius_x * radius_y def _UpperCAmelCase ( __A : float , __A : float ): if diagonal_a < 0 or diagonal_a < 0: raise ValueError('''area_rhombus() only accepts non-negative values''' ) return 1 / 2 * diagonal_a * diagonal_a def _UpperCAmelCase ( __A : int , __A : float ): if not isinstance(__A , __A ) or sides < 3: raise ValueError( '''area_reg_polygon() only accepts integers greater than or \ equal to three as number of sides''' ) elif length < 0: raise ValueError( '''area_reg_polygon() only accepts non-negative values as \ length of a side''' ) return (sides * length**2) / (4 * tan(pi / sides )) return (sides * length**2) / (4 * tan(pi / sides )) if __name__ == "__main__": import doctest doctest.testmod(verbose=True) # verbose so we can see methods missing tests print('[DEMO] Areas of various geometric shapes: \n') print(F"""Rectangle: {area_rectangle(10, 20) = }""") print(F"""Square: {area_square(10) = }""") print(F"""Triangle: {area_triangle(10, 10) = }""") print(F"""Triangle: {area_triangle_three_sides(5, 12, 13) = }""") print(F"""Parallelogram: {area_parallelogram(10, 20) = }""") print(F"""Rhombus: {area_rhombus(10, 20) = }""") print(F"""Trapezium: {area_trapezium(10, 20, 30) = }""") print(F"""Circle: {area_circle(20) = }""") print(F"""Ellipse: {area_ellipse(10, 20) = }""") print('\nSurface Areas of various geometric shapes: \n') print(F"""Cube: {surface_area_cube(20) = }""") print(F"""Cuboid: {surface_area_cuboid(10, 20, 30) = }""") print(F"""Sphere: {surface_area_sphere(20) = }""") print(F"""Hemisphere: {surface_area_hemisphere(20) = }""") print(F"""Cone: {surface_area_cone(10, 20) = }""") print(F"""Conical Frustum: {surface_area_conical_frustum(10, 20, 30) = }""") print(F"""Cylinder: {surface_area_cylinder(10, 20) = }""") print(F"""Torus: {surface_area_torus(20, 10) = }""") print(F"""Equilateral Triangle: {area_reg_polygon(3, 10) = }""") print(F"""Square: {area_reg_polygon(4, 10) = }""") print(F"""Reqular Pentagon: {area_reg_polygon(5, 10) = }""")
666
0
'''simple docstring''' import re from typing import Callable, List, Optional, Union import tensorflow as tf try: from tensorflow.keras.optimizers.legacy import Adam except ImportError: from tensorflow.keras.optimizers import Adam class A__ ( tf.keras.optimizers.schedules.LearningRateSchedule ): def __init__( self , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ = 1.0 , UpperCamelCase__ = None , ) -> List[str]: '''simple docstring''' super().__init__() A_ = initial_learning_rate A_ = warmup_steps A_ = power A_ = decay_schedule_fn A_ = name def __call__( self , UpperCamelCase__ ) -> Union[str, Any]: '''simple docstring''' with tf.name_scope(self.name or """WarmUp""" ) as name: # Implements polynomial warmup. i.e., if global_step < warmup_steps, the # learning rate will be `global_step/num_warmup_steps * init_lr`. A_ = tf.cast(UpperCamelCase__ , tf.floataa ) A_ = tf.cast(self.warmup_steps , tf.floataa ) A_ = global_step_float / warmup_steps_float A_ = self.initial_learning_rate * tf.math.pow(UpperCamelCase__ , self.power ) return tf.cond( global_step_float < warmup_steps_float , lambda: warmup_learning_rate , lambda: self.decay_schedule_fn(step - self.warmup_steps ) , name=UpperCamelCase__ , ) def snake_case_ ( self ) -> List[str]: '''simple docstring''' return { "initial_learning_rate": self.initial_learning_rate, "decay_schedule_fn": self.decay_schedule_fn, "warmup_steps": self.warmup_steps, "power": self.power, "name": self.name, } def UpperCAmelCase__ ( UpperCAmelCase__, UpperCAmelCase__, UpperCAmelCase__, UpperCAmelCase__ = 0.0, UpperCAmelCase__ = 0.9, UpperCAmelCase__ = 0.999, UpperCAmelCase__ = 1e-8, UpperCAmelCase__ = None, UpperCAmelCase__ = None, UpperCAmelCase__ = 0.0, UpperCAmelCase__ = 1.0, UpperCAmelCase__ = None, ) -> int: A_ = tf.keras.optimizers.schedules.PolynomialDecay( initial_learning_rate=UpperCAmelCase__, decay_steps=num_train_steps - num_warmup_steps, end_learning_rate=init_lr * min_lr_ratio, power=UpperCAmelCase__, ) if num_warmup_steps: A_ = WarmUp( initial_learning_rate=UpperCAmelCase__, decay_schedule_fn=UpperCAmelCase__, warmup_steps=UpperCAmelCase__, ) if weight_decay_rate > 0.0: A_ = AdamWeightDecay( learning_rate=UpperCAmelCase__, weight_decay_rate=UpperCAmelCase__, beta_a=UpperCAmelCase__, beta_a=UpperCAmelCase__, epsilon=UpperCAmelCase__, clipnorm=UpperCAmelCase__, global_clipnorm=UpperCAmelCase__, exclude_from_weight_decay=["""LayerNorm""", """layer_norm""", """bias"""], include_in_weight_decay=UpperCAmelCase__, ) else: A_ = tf.keras.optimizers.Adam( learning_rate=UpperCAmelCase__, beta_a=UpperCAmelCase__, beta_a=UpperCAmelCase__, epsilon=UpperCAmelCase__, clipnorm=UpperCAmelCase__, global_clipnorm=UpperCAmelCase__, ) # We return the optimizer and the LR scheduler in order to better track the # evolution of the LR independently of the optimizer. return optimizer, lr_schedule class A__ ( _snake_case ): def __init__( self , UpperCamelCase__ = 0.001 , UpperCamelCase__ = 0.9 , UpperCamelCase__ = 0.999 , UpperCamelCase__ = 1e-7 , UpperCamelCase__ = False , UpperCamelCase__ = 0.0 , UpperCamelCase__ = None , UpperCamelCase__ = None , UpperCamelCase__ = "AdamWeightDecay" , **UpperCamelCase__ , ) -> Optional[Any]: '''simple docstring''' super().__init__(UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , **UpperCamelCase__ ) A_ = weight_decay_rate A_ = include_in_weight_decay A_ = exclude_from_weight_decay @classmethod def snake_case_ ( cls , UpperCamelCase__ ) -> Union[str, Any]: '''simple docstring''' A_ = {"""WarmUp""": WarmUp} return super(UpperCamelCase__ , cls ).from_config(UpperCamelCase__ , custom_objects=UpperCamelCase__ ) def snake_case_ ( self , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ ) -> Any: '''simple docstring''' super(UpperCamelCase__ , self )._prepare_local(UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ ) A_ = tf.constant( self.weight_decay_rate , name="""adam_weight_decay_rate""" ) def snake_case_ ( self , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ ) -> List[Any]: '''simple docstring''' A_ = self._do_use_weight_decay(var.name ) if do_decay: return var.assign_sub( learning_rate * var * apply_state[(var.device, var.dtype.base_dtype)]["""weight_decay_rate"""] , use_locking=self._use_locking , ) return tf.no_op() def snake_case_ ( self , UpperCamelCase__ , UpperCamelCase__=None , **UpperCamelCase__ ) -> int: '''simple docstring''' A_ , A_ = list(zip(*UpperCamelCase__ ) ) return super(UpperCamelCase__ , self ).apply_gradients(zip(UpperCamelCase__ , UpperCamelCase__ ) , name=UpperCamelCase__ , **UpperCamelCase__ ) def snake_case_ ( self , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ ) -> Union[str, Any]: '''simple docstring''' if apply_state is None: return self._decayed_lr_t[var_dtype], {} A_ = apply_state or {} A_ = apply_state.get((var_device, var_dtype) ) if coefficients is None: A_ = self._fallback_apply_state(UpperCamelCase__ , UpperCamelCase__ ) A_ = coefficients return coefficients["lr_t"], {"apply_state": apply_state} def snake_case_ ( self , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__=None ) -> List[Any]: '''simple docstring''' A_ , A_ = self._get_lr(var.device , var.dtype.base_dtype , UpperCamelCase__ ) A_ = self._decay_weights_op(UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ ) with tf.control_dependencies([decay] ): return super(UpperCamelCase__ , self )._resource_apply_dense(UpperCamelCase__ , UpperCamelCase__ , **UpperCamelCase__ ) def snake_case_ ( self , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__=None ) -> Any: '''simple docstring''' A_ , A_ = self._get_lr(var.device , var.dtype.base_dtype , UpperCamelCase__ ) A_ = self._decay_weights_op(UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ ) with tf.control_dependencies([decay] ): return super(UpperCamelCase__ , self )._resource_apply_sparse(UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , **UpperCamelCase__ ) def snake_case_ ( self ) -> Optional[int]: '''simple docstring''' A_ = super().get_config() config.update({"""weight_decay_rate""": self.weight_decay_rate} ) return config def snake_case_ ( self , UpperCamelCase__ ) -> str: '''simple docstring''' if self.weight_decay_rate == 0: return False if self._include_in_weight_decay: for r in self._include_in_weight_decay: if re.search(UpperCamelCase__ , UpperCamelCase__ ) is not None: return True if self._exclude_from_weight_decay: for r in self._exclude_from_weight_decay: if re.search(UpperCamelCase__ , UpperCamelCase__ ) is not None: return False return True class A__ ( _snake_case ): def __init__( self ) -> List[Any]: '''simple docstring''' A_ = [] A_ = None @property def snake_case_ ( self ) -> List[str]: '''simple docstring''' if self._accum_steps is None: A_ = tf.Variable( tf.constant(0 , dtype=tf.intaa ) , trainable=UpperCamelCase__ , synchronization=tf.VariableSynchronization.ON_READ , aggregation=tf.VariableAggregation.ONLY_FIRST_REPLICA , ) return self._accum_steps.value() @property def snake_case_ ( self ) -> int: '''simple docstring''' if not self._gradients: raise ValueError("""The accumulator should be called first to initialize the gradients""" ) return [gradient.value() if gradient is not None else gradient for gradient in self._gradients] def __call__( self , UpperCamelCase__ ) -> int: '''simple docstring''' if not self._gradients: A_ = self.step # Create the step variable. self._gradients.extend( [ tf.Variable( tf.zeros_like(UpperCamelCase__ ) , trainable=UpperCamelCase__ , synchronization=tf.VariableSynchronization.ON_READ , aggregation=tf.VariableAggregation.ONLY_FIRST_REPLICA , ) if gradient is not None else gradient for gradient in gradients ] ) if len(UpperCamelCase__ ) != len(self._gradients ): raise ValueError(f'''Expected {len(self._gradients )} gradients, but got {len(UpperCamelCase__ )}''' ) for accum_gradient, gradient in zip(self._gradients , UpperCamelCase__ ): if accum_gradient is not None and gradient is not None: accum_gradient.assign_add(UpperCamelCase__ ) self._accum_steps.assign_add(1 ) def snake_case_ ( self ) -> Dict: '''simple docstring''' if not self._gradients: return self._accum_steps.assign(0 ) for gradient in self._gradients: if gradient is not None: gradient.assign(tf.zeros_like(UpperCamelCase__ ) )
667
'''simple docstring''' import tensorflow as tf from ...tf_utils import shape_list class A__ ( tf.keras.layers.Layer ): def __init__( self , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__=1 , UpperCamelCase__=False , **UpperCamelCase__ ) -> Union[str, Any]: '''simple docstring''' super().__init__(**UpperCamelCase__ ) A_ = vocab_size A_ = d_embed A_ = d_proj A_ = cutoffs + [vocab_size] A_ = [0] + self.cutoffs A_ = div_val A_ = self.cutoffs[0] A_ = len(self.cutoffs ) - 1 A_ = self.shortlist_size + self.n_clusters A_ = keep_order A_ = [] A_ = [] def snake_case_ ( self , UpperCamelCase__ ) -> Union[str, Any]: '''simple docstring''' if self.n_clusters > 0: A_ = self.add_weight( shape=(self.n_clusters, self.d_embed) , initializer="""zeros""" , trainable=UpperCamelCase__ , name="""cluster_weight""" ) A_ = self.add_weight( shape=(self.n_clusters,) , initializer="""zeros""" , trainable=UpperCamelCase__ , name="""cluster_bias""" ) if self.div_val == 1: for i in range(len(self.cutoffs ) ): if self.d_proj != self.d_embed: A_ = self.add_weight( shape=(self.d_embed, self.d_proj) , initializer="""zeros""" , trainable=UpperCamelCase__ , name=f'''out_projs_._{i}''' , ) self.out_projs.append(UpperCamelCase__ ) else: self.out_projs.append(UpperCamelCase__ ) A_ = self.add_weight( shape=(self.vocab_size, self.d_embed) , initializer="""zeros""" , trainable=UpperCamelCase__ , name=f'''out_layers_._{i}_._weight''' , ) A_ = self.add_weight( shape=(self.vocab_size,) , initializer="""zeros""" , trainable=UpperCamelCase__ , name=f'''out_layers_._{i}_._bias''' , ) self.out_layers.append((weight, bias) ) else: for i in range(len(self.cutoffs ) ): A_ , A_ = self.cutoff_ends[i], self.cutoff_ends[i + 1] A_ = self.d_embed // (self.div_val**i) A_ = self.add_weight( shape=(d_emb_i, self.d_proj) , initializer="""zeros""" , trainable=UpperCamelCase__ , name=f'''out_projs_._{i}''' ) self.out_projs.append(UpperCamelCase__ ) A_ = self.add_weight( shape=(r_idx - l_idx, d_emb_i) , initializer="""zeros""" , trainable=UpperCamelCase__ , name=f'''out_layers_._{i}_._weight''' , ) A_ = self.add_weight( shape=(r_idx - l_idx,) , initializer="""zeros""" , trainable=UpperCamelCase__ , name=f'''out_layers_._{i}_._bias''' , ) self.out_layers.append((weight, bias) ) super().build(UpperCamelCase__ ) @staticmethod def snake_case_ ( UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__=None ) -> List[Any]: '''simple docstring''' A_ = x if proj is not None: A_ = tf.einsum("""ibd,ed->ibe""" , UpperCamelCase__ , UpperCamelCase__ ) return tf.einsum("""ibd,nd->ibn""" , UpperCamelCase__ , UpperCamelCase__ ) + b @staticmethod def snake_case_ ( UpperCamelCase__ , UpperCamelCase__ ) -> List[str]: '''simple docstring''' A_ = shape_list(UpperCamelCase__ ) A_ = tf.range(lp_size[0] , dtype=target.dtype ) A_ = tf.stack([r, target] , 1 ) return tf.gather_nd(UpperCamelCase__ , UpperCamelCase__ ) def snake_case_ ( self , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__=True , UpperCamelCase__=False ) -> Optional[int]: '''simple docstring''' A_ = 0 if self.n_clusters == 0: A_ = self._logit(UpperCamelCase__ , self.out_layers[0][0] , self.out_layers[0][1] , self.out_projs[0] ) if target is not None: A_ = tf.nn.sparse_softmax_cross_entropy_with_logits(labels=UpperCamelCase__ , logits=UpperCamelCase__ ) A_ = tf.nn.log_softmax(UpperCamelCase__ , axis=-1 ) else: A_ = shape_list(UpperCamelCase__ ) A_ = [] A_ = tf.zeros(hidden_sizes[:2] ) for i in range(len(self.cutoffs ) ): A_ , A_ = self.cutoff_ends[i], self.cutoff_ends[i + 1] if target is not None: A_ = (target >= l_idx) & (target < r_idx) A_ = tf.where(UpperCamelCase__ ) A_ = tf.boolean_mask(UpperCamelCase__ , UpperCamelCase__ ) - l_idx if self.div_val == 1: A_ = self.out_layers[0][0][l_idx:r_idx] A_ = self.out_layers[0][1][l_idx:r_idx] else: A_ = self.out_layers[i][0] A_ = self.out_layers[i][1] if i == 0: A_ = tf.concat([cur_W, self.cluster_weight] , 0 ) A_ = tf.concat([cur_b, self.cluster_bias] , 0 ) A_ = self._logit(UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , self.out_projs[0] ) A_ = tf.nn.log_softmax(UpperCamelCase__ ) out.append(head_logprob[..., : self.cutoffs[0]] ) if target is not None: A_ = tf.boolean_mask(UpperCamelCase__ , UpperCamelCase__ ) A_ = self._gather_logprob(UpperCamelCase__ , UpperCamelCase__ ) else: A_ = self._logit(UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , self.out_projs[i] ) A_ = tf.nn.log_softmax(UpperCamelCase__ ) A_ = self.cutoffs[0] + i - 1 # No probability for the head cluster A_ = head_logprob[..., cluster_prob_idx, None] + tail_logprob out.append(UpperCamelCase__ ) if target is not None: A_ = tf.boolean_mask(UpperCamelCase__ , UpperCamelCase__ ) A_ = tf.boolean_mask(UpperCamelCase__ , UpperCamelCase__ ) A_ = self._gather_logprob(UpperCamelCase__ , UpperCamelCase__ ) cur_logprob += cur_head_logprob[:, self.cutoff_ends[1] + i - 1] if target is not None: loss += tf.scatter_nd(UpperCamelCase__ , -cur_logprob , shape_list(UpperCamelCase__ ) ) A_ = tf.concat(UpperCamelCase__ , axis=-1 ) if target is not None: if return_mean: A_ = tf.reduce_mean(UpperCamelCase__ ) # Add the training-time loss value to the layer using `self.add_loss()`. self.add_loss(UpperCamelCase__ ) # Log the loss as a metric (we could log arbitrary metrics, # including different metrics for training and inference. self.add_metric(UpperCamelCase__ , name=self.name , aggregation="""mean""" if return_mean else """""" ) return out
667
1
'''simple docstring''' def UpperCAmelCase__ ( UpperCAmelCase__ ) -> list: return [ txt[:a] + txt[a].upper() + txt[a + 1 :] for a in range(len(UpperCAmelCase__ ) ) if txt[a].isalpha() ] if __name__ == "__main__": __import__('''doctest''').testmod()
667
'''simple docstring''' from queue import PriorityQueue from typing import Any import numpy as np def UpperCAmelCase__ ( UpperCAmelCase__, UpperCAmelCase__, UpperCAmelCase__, UpperCAmelCase__, UpperCAmelCase__, UpperCAmelCase__, UpperCAmelCase__, UpperCAmelCase__, UpperCAmelCase__, ) -> float | int: for nxt, d in graph[v]: if nxt in visited_forward: continue A_ = cst_fwd.get(UpperCAmelCase__, np.inf ) A_ = cst_fwd[v] + d if new_cost_f < old_cost_f: queue.put((new_cost_f, nxt) ) A_ = new_cost_f A_ = v if nxt in visited_backward: if cst_fwd[v] + d + cst_bwd[nxt] < shortest_distance: A_ = cst_fwd[v] + d + cst_bwd[nxt] return shortest_distance def UpperCAmelCase__ ( UpperCAmelCase__, UpperCAmelCase__, UpperCAmelCase__, UpperCAmelCase__ ) -> int: A_ = -1 A_ = set() A_ = set() A_ = {source: 0} A_ = {destination: 0} A_ = {source: None} A_ = {destination: None} A_ = PriorityQueue() A_ = PriorityQueue() A_ = np.inf queue_forward.put((0, source) ) queue_backward.put((0, destination) ) if source == destination: return 0 while not queue_forward.empty() and not queue_backward.empty(): A_ , A_ = queue_forward.get() visited_forward.add(UpperCAmelCase__ ) A_ , A_ = queue_backward.get() visited_backward.add(UpperCAmelCase__ ) A_ = pass_and_relaxation( UpperCAmelCase__, UpperCAmelCase__, UpperCAmelCase__, UpperCAmelCase__, UpperCAmelCase__, UpperCAmelCase__, UpperCAmelCase__, UpperCAmelCase__, UpperCAmelCase__, ) A_ = pass_and_relaxation( UpperCAmelCase__, UpperCAmelCase__, UpperCAmelCase__, UpperCAmelCase__, UpperCAmelCase__, UpperCAmelCase__, UpperCAmelCase__, UpperCAmelCase__, UpperCAmelCase__, ) if cst_fwd[v_fwd] + cst_bwd[v_bwd] >= shortest_distance: break if shortest_distance != np.inf: A_ = shortest_distance return shortest_path_distance __lowerCamelCase = { '''B''': [['''C''', 1]], '''C''': [['''D''', 1]], '''D''': [['''F''', 1]], '''E''': [['''B''', 1], ['''G''', 2]], '''F''': [], '''G''': [['''F''', 1]], } __lowerCamelCase = { '''B''': [['''E''', 1]], '''C''': [['''B''', 1]], '''D''': [['''C''', 1]], '''F''': [['''D''', 1], ['''G''', 1]], '''E''': [[None, np.inf]], '''G''': [['''E''', 2]], } if __name__ == "__main__": import doctest doctest.testmod()
667
1
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_tf_available, is_torch_available, is_vision_available, ) __lowerCamelCase = { '''configuration_mobilevit''': ['''MOBILEVIT_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''MobileViTConfig''', '''MobileViTOnnxConfig'''], } try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __lowerCamelCase = ['''MobileViTFeatureExtractor'''] __lowerCamelCase = ['''MobileViTImageProcessor'''] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __lowerCamelCase = [ '''MOBILEVIT_PRETRAINED_MODEL_ARCHIVE_LIST''', '''MobileViTForImageClassification''', '''MobileViTForSemanticSegmentation''', '''MobileViTModel''', '''MobileViTPreTrainedModel''', ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __lowerCamelCase = [ '''TF_MOBILEVIT_PRETRAINED_MODEL_ARCHIVE_LIST''', '''TFMobileViTForImageClassification''', '''TFMobileViTForSemanticSegmentation''', '''TFMobileViTModel''', '''TFMobileViTPreTrainedModel''', ] if TYPE_CHECKING: from .configuration_mobilevit import MOBILEVIT_PRETRAINED_CONFIG_ARCHIVE_MAP, MobileViTConfig, MobileViTOnnxConfig try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .feature_extraction_mobilevit import MobileViTFeatureExtractor from .image_processing_mobilevit import MobileViTImageProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_mobilevit import ( MOBILEVIT_PRETRAINED_MODEL_ARCHIVE_LIST, MobileViTForImageClassification, MobileViTForSemanticSegmentation, MobileViTModel, MobileViTPreTrainedModel, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_mobilevit import ( TF_MOBILEVIT_PRETRAINED_MODEL_ARCHIVE_LIST, TFMobileViTForImageClassification, TFMobileViTForSemanticSegmentation, TFMobileViTModel, TFMobileViTPreTrainedModel, ) else: import sys __lowerCamelCase = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
667
'''simple docstring''' import os __lowerCamelCase = {'''I''': 1, '''V''': 5, '''X''': 10, '''L''': 50, '''C''': 100, '''D''': 500, '''M''': 1000} def UpperCAmelCase__ ( UpperCAmelCase__ ) -> int: A_ = 0 A_ = 0 while index < len(UpperCAmelCase__ ) - 1: A_ = SYMBOLS[numerals[index]] A_ = SYMBOLS[numerals[index + 1]] if current_value < next_value: total_value -= current_value else: total_value += current_value index += 1 total_value += SYMBOLS[numerals[index]] return total_value def UpperCAmelCase__ ( UpperCAmelCase__ ) -> str: A_ = """""" A_ = num // 10_00 numerals += m_count * "M" num %= 10_00 A_ = num // 1_00 if c_count == 9: numerals += "CM" c_count -= 9 elif c_count == 4: numerals += "CD" c_count -= 4 if c_count >= 5: numerals += "D" c_count -= 5 numerals += c_count * "C" num %= 1_00 A_ = num // 10 if x_count == 9: numerals += "XC" x_count -= 9 elif x_count == 4: numerals += "XL" x_count -= 4 if x_count >= 5: numerals += "L" x_count -= 5 numerals += x_count * "X" num %= 10 if num == 9: numerals += "IX" num -= 9 elif num == 4: numerals += "IV" num -= 4 if num >= 5: numerals += "V" num -= 5 numerals += num * "I" return numerals def UpperCAmelCase__ ( UpperCAmelCase__ = "/p089_roman.txt" ) -> int: A_ = 0 with open(os.path.dirname(UpperCAmelCase__ ) + roman_numerals_filename ) as filea: A_ = filea.readlines() for line in lines: A_ = line.strip() A_ = parse_roman_numerals(UpperCAmelCase__ ) A_ = generate_roman_numerals(UpperCAmelCase__ ) savings += len(UpperCAmelCase__ ) - len(UpperCAmelCase__ ) return savings if __name__ == "__main__": print(f"""{solution() = }""")
667
1
'''simple docstring''' from ..utils import DummyObject, requires_backends class A__ ( metaclass=_snake_case ): lowercase = ["torch"] def __init__( self , *UpperCamelCase__ , **UpperCamelCase__ ) -> Tuple: '''simple docstring''' requires_backends(self , ["""torch"""] ) @classmethod def snake_case_ ( cls , *UpperCamelCase__ , **UpperCamelCase__ ) -> int: '''simple docstring''' requires_backends(cls , ["""torch"""] ) @classmethod def snake_case_ ( cls , *UpperCamelCase__ , **UpperCamelCase__ ) -> Tuple: '''simple docstring''' requires_backends(cls , ["""torch"""] ) class A__ ( metaclass=_snake_case ): lowercase = ["torch"] def __init__( self , *UpperCamelCase__ , **UpperCamelCase__ ) -> Tuple: '''simple docstring''' requires_backends(self , ["""torch"""] ) @classmethod def snake_case_ ( cls , *UpperCamelCase__ , **UpperCamelCase__ ) -> Optional[int]: '''simple docstring''' requires_backends(cls , ["""torch"""] ) @classmethod def snake_case_ ( cls , *UpperCamelCase__ , **UpperCamelCase__ ) -> Dict: '''simple docstring''' requires_backends(cls , ["""torch"""] ) class A__ ( metaclass=_snake_case ): lowercase = ["torch"] def __init__( self , *UpperCamelCase__ , **UpperCamelCase__ ) -> List[str]: '''simple docstring''' requires_backends(self , ["""torch"""] ) @classmethod def snake_case_ ( cls , *UpperCamelCase__ , **UpperCamelCase__ ) -> List[str]: '''simple docstring''' requires_backends(cls , ["""torch"""] ) @classmethod def snake_case_ ( cls , *UpperCamelCase__ , **UpperCamelCase__ ) -> Any: '''simple docstring''' requires_backends(cls , ["""torch"""] ) class A__ ( metaclass=_snake_case ): lowercase = ["torch"] def __init__( self , *UpperCamelCase__ , **UpperCamelCase__ ) -> str: '''simple docstring''' requires_backends(self , ["""torch"""] ) @classmethod def snake_case_ ( cls , *UpperCamelCase__ , **UpperCamelCase__ ) -> int: '''simple docstring''' requires_backends(cls , ["""torch"""] ) @classmethod def snake_case_ ( cls , *UpperCamelCase__ , **UpperCamelCase__ ) -> str: '''simple docstring''' requires_backends(cls , ["""torch"""] ) class A__ ( metaclass=_snake_case ): lowercase = ["torch"] def __init__( self , *UpperCamelCase__ , **UpperCamelCase__ ) -> Tuple: '''simple docstring''' requires_backends(self , ["""torch"""] ) @classmethod def snake_case_ ( cls , *UpperCamelCase__ , **UpperCamelCase__ ) -> str: '''simple docstring''' requires_backends(cls , ["""torch"""] ) @classmethod def snake_case_ ( cls , *UpperCamelCase__ , **UpperCamelCase__ ) -> Optional[int]: '''simple docstring''' requires_backends(cls , ["""torch"""] ) class A__ ( metaclass=_snake_case ): lowercase = ["torch"] def __init__( self , *UpperCamelCase__ , **UpperCamelCase__ ) -> str: '''simple docstring''' requires_backends(self , ["""torch"""] ) @classmethod def snake_case_ ( cls , *UpperCamelCase__ , **UpperCamelCase__ ) -> str: '''simple docstring''' requires_backends(cls , ["""torch"""] ) @classmethod def snake_case_ ( cls , *UpperCamelCase__ , **UpperCamelCase__ ) -> Tuple: '''simple docstring''' requires_backends(cls , ["""torch"""] ) class A__ ( metaclass=_snake_case ): lowercase = ["torch"] def __init__( self , *UpperCamelCase__ , **UpperCamelCase__ ) -> int: '''simple docstring''' requires_backends(self , ["""torch"""] ) @classmethod def snake_case_ ( cls , *UpperCamelCase__ , **UpperCamelCase__ ) -> Union[str, Any]: '''simple docstring''' requires_backends(cls , ["""torch"""] ) @classmethod def snake_case_ ( cls , *UpperCamelCase__ , **UpperCamelCase__ ) -> Union[str, Any]: '''simple docstring''' requires_backends(cls , ["""torch"""] ) class A__ ( metaclass=_snake_case ): lowercase = ["torch"] def __init__( self , *UpperCamelCase__ , **UpperCamelCase__ ) -> int: '''simple docstring''' requires_backends(self , ["""torch"""] ) @classmethod def snake_case_ ( cls , *UpperCamelCase__ , **UpperCamelCase__ ) -> List[Any]: '''simple docstring''' requires_backends(cls , ["""torch"""] ) @classmethod def snake_case_ ( cls , *UpperCamelCase__ , **UpperCamelCase__ ) -> Tuple: '''simple docstring''' requires_backends(cls , ["""torch"""] ) class A__ ( metaclass=_snake_case ): lowercase = ["torch"] def __init__( self , *UpperCamelCase__ , **UpperCamelCase__ ) -> List[str]: '''simple docstring''' requires_backends(self , ["""torch"""] ) @classmethod def snake_case_ ( cls , *UpperCamelCase__ , **UpperCamelCase__ ) -> List[Any]: '''simple docstring''' requires_backends(cls , ["""torch"""] ) @classmethod def snake_case_ ( cls , *UpperCamelCase__ , **UpperCamelCase__ ) -> int: '''simple docstring''' requires_backends(cls , ["""torch"""] ) class A__ ( metaclass=_snake_case ): lowercase = ["torch"] def __init__( self , *UpperCamelCase__ , **UpperCamelCase__ ) -> Dict: '''simple docstring''' requires_backends(self , ["""torch"""] ) @classmethod def snake_case_ ( cls , *UpperCamelCase__ , **UpperCamelCase__ ) -> List[Any]: '''simple docstring''' requires_backends(cls , ["""torch"""] ) @classmethod def snake_case_ ( cls , *UpperCamelCase__ , **UpperCamelCase__ ) -> Optional[int]: '''simple docstring''' requires_backends(cls , ["""torch"""] ) class A__ ( metaclass=_snake_case ): lowercase = ["torch"] def __init__( self , *UpperCamelCase__ , **UpperCamelCase__ ) -> Dict: '''simple docstring''' requires_backends(self , ["""torch"""] ) @classmethod def snake_case_ ( cls , *UpperCamelCase__ , **UpperCamelCase__ ) -> Dict: '''simple docstring''' requires_backends(cls , ["""torch"""] ) @classmethod def snake_case_ ( cls , *UpperCamelCase__ , **UpperCamelCase__ ) -> Optional[Any]: '''simple docstring''' requires_backends(cls , ["""torch"""] ) def UpperCAmelCase__ ( *UpperCAmelCase__, **UpperCAmelCase__ ) -> int: requires_backends(UpperCAmelCase__, ["""torch"""] ) def UpperCAmelCase__ ( *UpperCAmelCase__, **UpperCAmelCase__ ) -> List[str]: requires_backends(UpperCAmelCase__, ["""torch"""] ) def UpperCAmelCase__ ( *UpperCAmelCase__, **UpperCAmelCase__ ) -> int: requires_backends(UpperCAmelCase__, ["""torch"""] ) def UpperCAmelCase__ ( *UpperCAmelCase__, **UpperCAmelCase__ ) -> str: requires_backends(UpperCAmelCase__, ["""torch"""] ) def UpperCAmelCase__ ( *UpperCAmelCase__, **UpperCAmelCase__ ) -> int: requires_backends(UpperCAmelCase__, ["""torch"""] ) def UpperCAmelCase__ ( *UpperCAmelCase__, **UpperCAmelCase__ ) -> Any: requires_backends(UpperCAmelCase__, ["""torch"""] ) def UpperCAmelCase__ ( *UpperCAmelCase__, **UpperCAmelCase__ ) -> Any: requires_backends(UpperCAmelCase__, ["""torch"""] ) class A__ ( metaclass=_snake_case ): lowercase = ["torch"] def __init__( self , *UpperCamelCase__ , **UpperCamelCase__ ) -> Union[str, Any]: '''simple docstring''' requires_backends(self , ["""torch"""] ) @classmethod def snake_case_ ( cls , *UpperCamelCase__ , **UpperCamelCase__ ) -> Union[str, Any]: '''simple docstring''' requires_backends(cls , ["""torch"""] ) @classmethod def snake_case_ ( cls , *UpperCamelCase__ , **UpperCamelCase__ ) -> Optional[int]: '''simple docstring''' requires_backends(cls , ["""torch"""] ) class A__ ( metaclass=_snake_case ): lowercase = ["torch"] def __init__( self , *UpperCamelCase__ , **UpperCamelCase__ ) -> List[str]: '''simple docstring''' requires_backends(self , ["""torch"""] ) @classmethod def snake_case_ ( cls , *UpperCamelCase__ , **UpperCamelCase__ ) -> str: '''simple docstring''' requires_backends(cls , ["""torch"""] ) @classmethod def snake_case_ ( cls , *UpperCamelCase__ , **UpperCamelCase__ ) -> int: '''simple docstring''' requires_backends(cls , ["""torch"""] ) class A__ ( metaclass=_snake_case ): lowercase = ["torch"] def __init__( self , *UpperCamelCase__ , **UpperCamelCase__ ) -> List[Any]: '''simple docstring''' requires_backends(self , ["""torch"""] ) @classmethod def snake_case_ ( cls , *UpperCamelCase__ , **UpperCamelCase__ ) -> Union[str, Any]: '''simple docstring''' requires_backends(cls , ["""torch"""] ) @classmethod def snake_case_ ( cls , *UpperCamelCase__ , **UpperCamelCase__ ) -> List[Any]: '''simple docstring''' requires_backends(cls , ["""torch"""] ) class A__ ( metaclass=_snake_case ): lowercase = ["torch"] def __init__( self , *UpperCamelCase__ , **UpperCamelCase__ ) -> List[Any]: '''simple docstring''' requires_backends(self , ["""torch"""] ) @classmethod def snake_case_ ( cls , *UpperCamelCase__ , **UpperCamelCase__ ) -> Optional[Any]: '''simple docstring''' requires_backends(cls , ["""torch"""] ) @classmethod def snake_case_ ( cls , *UpperCamelCase__ , **UpperCamelCase__ ) -> List[str]: '''simple docstring''' requires_backends(cls , ["""torch"""] ) class A__ ( metaclass=_snake_case ): lowercase = ["torch"] def __init__( self , *UpperCamelCase__ , **UpperCamelCase__ ) -> Tuple: '''simple docstring''' requires_backends(self , ["""torch"""] ) @classmethod def snake_case_ ( cls , *UpperCamelCase__ , **UpperCamelCase__ ) -> Union[str, Any]: '''simple docstring''' requires_backends(cls , ["""torch"""] ) @classmethod def snake_case_ ( cls , *UpperCamelCase__ , **UpperCamelCase__ ) -> Tuple: '''simple docstring''' requires_backends(cls , ["""torch"""] ) class A__ ( metaclass=_snake_case ): lowercase = ["torch"] def __init__( self , *UpperCamelCase__ , **UpperCamelCase__ ) -> int: '''simple docstring''' requires_backends(self , ["""torch"""] ) @classmethod def snake_case_ ( cls , *UpperCamelCase__ , **UpperCamelCase__ ) -> List[Any]: '''simple docstring''' requires_backends(cls , ["""torch"""] ) @classmethod def snake_case_ ( cls , *UpperCamelCase__ , **UpperCamelCase__ ) -> Optional[int]: '''simple docstring''' requires_backends(cls , ["""torch"""] ) class A__ ( metaclass=_snake_case ): lowercase = ["torch"] def __init__( self , *UpperCamelCase__ , **UpperCamelCase__ ) -> List[str]: '''simple docstring''' requires_backends(self , ["""torch"""] ) @classmethod def snake_case_ ( cls , *UpperCamelCase__ , **UpperCamelCase__ ) -> Union[str, Any]: '''simple docstring''' requires_backends(cls , ["""torch"""] ) @classmethod def snake_case_ ( cls , *UpperCamelCase__ , **UpperCamelCase__ ) -> Union[str, Any]: '''simple docstring''' requires_backends(cls , ["""torch"""] ) class A__ ( metaclass=_snake_case ): lowercase = ["torch"] def __init__( self , *UpperCamelCase__ , **UpperCamelCase__ ) -> Dict: '''simple docstring''' requires_backends(self , ["""torch"""] ) @classmethod def snake_case_ ( cls , *UpperCamelCase__ , **UpperCamelCase__ ) -> int: '''simple docstring''' requires_backends(cls , ["""torch"""] ) @classmethod def snake_case_ ( cls , *UpperCamelCase__ , **UpperCamelCase__ ) -> Optional[int]: '''simple docstring''' requires_backends(cls , ["""torch"""] ) class A__ ( metaclass=_snake_case ): lowercase = ["torch"] def __init__( self , *UpperCamelCase__ , **UpperCamelCase__ ) -> str: '''simple docstring''' requires_backends(self , ["""torch"""] ) @classmethod def snake_case_ ( cls , *UpperCamelCase__ , **UpperCamelCase__ ) -> Optional[int]: '''simple docstring''' requires_backends(cls , ["""torch"""] ) @classmethod def snake_case_ ( cls , *UpperCamelCase__ , **UpperCamelCase__ ) -> str: '''simple docstring''' requires_backends(cls , ["""torch"""] ) class A__ ( metaclass=_snake_case ): lowercase = ["torch"] def __init__( self , *UpperCamelCase__ , **UpperCamelCase__ ) -> str: '''simple docstring''' requires_backends(self , ["""torch"""] ) @classmethod def snake_case_ ( cls , *UpperCamelCase__ , **UpperCamelCase__ ) -> Any: '''simple docstring''' requires_backends(cls , ["""torch"""] ) @classmethod def snake_case_ ( cls , *UpperCamelCase__ , **UpperCamelCase__ ) -> Tuple: '''simple docstring''' requires_backends(cls , ["""torch"""] ) class A__ ( metaclass=_snake_case ): lowercase = ["torch"] def __init__( self , *UpperCamelCase__ , **UpperCamelCase__ ) -> Tuple: '''simple docstring''' requires_backends(self , ["""torch"""] ) @classmethod def snake_case_ ( cls , *UpperCamelCase__ , **UpperCamelCase__ ) -> List[Any]: '''simple docstring''' requires_backends(cls , ["""torch"""] ) @classmethod def snake_case_ ( cls , *UpperCamelCase__ , **UpperCamelCase__ ) -> Optional[int]: '''simple docstring''' requires_backends(cls , ["""torch"""] ) class A__ ( metaclass=_snake_case ): lowercase = ["torch"] def __init__( self , *UpperCamelCase__ , **UpperCamelCase__ ) -> Union[str, Any]: '''simple docstring''' requires_backends(self , ["""torch"""] ) @classmethod def snake_case_ ( cls , *UpperCamelCase__ , **UpperCamelCase__ ) -> Optional[Any]: '''simple docstring''' requires_backends(cls , ["""torch"""] ) @classmethod def snake_case_ ( cls , *UpperCamelCase__ , **UpperCamelCase__ ) -> Union[str, Any]: '''simple docstring''' requires_backends(cls , ["""torch"""] ) class A__ ( metaclass=_snake_case ): lowercase = ["torch"] def __init__( self , *UpperCamelCase__ , **UpperCamelCase__ ) -> Optional[Any]: '''simple docstring''' requires_backends(self , ["""torch"""] ) @classmethod def snake_case_ ( cls , *UpperCamelCase__ , **UpperCamelCase__ ) -> Any: '''simple docstring''' requires_backends(cls , ["""torch"""] ) @classmethod def snake_case_ ( cls , *UpperCamelCase__ , **UpperCamelCase__ ) -> List[str]: '''simple docstring''' requires_backends(cls , ["""torch"""] ) class A__ ( metaclass=_snake_case ): lowercase = ["torch"] def __init__( self , *UpperCamelCase__ , **UpperCamelCase__ ) -> str: '''simple docstring''' requires_backends(self , ["""torch"""] ) @classmethod def snake_case_ ( cls , *UpperCamelCase__ , **UpperCamelCase__ ) -> Tuple: '''simple docstring''' requires_backends(cls , ["""torch"""] ) @classmethod def snake_case_ ( cls , *UpperCamelCase__ , **UpperCamelCase__ ) -> List[str]: '''simple docstring''' requires_backends(cls , ["""torch"""] ) class A__ ( metaclass=_snake_case ): lowercase = ["torch"] def __init__( self , *UpperCamelCase__ , **UpperCamelCase__ ) -> int: '''simple docstring''' requires_backends(self , ["""torch"""] ) @classmethod def snake_case_ ( cls , *UpperCamelCase__ , **UpperCamelCase__ ) -> List[Any]: '''simple docstring''' requires_backends(cls , ["""torch"""] ) @classmethod def snake_case_ ( cls , *UpperCamelCase__ , **UpperCamelCase__ ) -> int: '''simple docstring''' requires_backends(cls , ["""torch"""] ) class A__ ( metaclass=_snake_case ): lowercase = ["torch"] def __init__( self , *UpperCamelCase__ , **UpperCamelCase__ ) -> List[str]: '''simple docstring''' requires_backends(self , ["""torch"""] ) @classmethod def snake_case_ ( cls , *UpperCamelCase__ , **UpperCamelCase__ ) -> Optional[int]: '''simple docstring''' requires_backends(cls , ["""torch"""] ) @classmethod def snake_case_ ( cls , *UpperCamelCase__ , **UpperCamelCase__ ) -> Optional[int]: '''simple docstring''' requires_backends(cls , ["""torch"""] ) class A__ ( metaclass=_snake_case ): lowercase = ["torch"] def __init__( self , *UpperCamelCase__ , **UpperCamelCase__ ) -> Any: '''simple docstring''' requires_backends(self , ["""torch"""] ) @classmethod def snake_case_ ( cls , *UpperCamelCase__ , **UpperCamelCase__ ) -> Union[str, Any]: '''simple docstring''' requires_backends(cls , ["""torch"""] ) @classmethod def snake_case_ ( cls , *UpperCamelCase__ , **UpperCamelCase__ ) -> Tuple: '''simple docstring''' requires_backends(cls , ["""torch"""] ) class A__ ( metaclass=_snake_case ): lowercase = ["torch"] def __init__( self , *UpperCamelCase__ , **UpperCamelCase__ ) -> Tuple: '''simple docstring''' requires_backends(self , ["""torch"""] ) @classmethod def snake_case_ ( cls , *UpperCamelCase__ , **UpperCamelCase__ ) -> List[Any]: '''simple docstring''' requires_backends(cls , ["""torch"""] ) @classmethod def snake_case_ ( cls , *UpperCamelCase__ , **UpperCamelCase__ ) -> Union[str, Any]: '''simple docstring''' requires_backends(cls , ["""torch"""] ) class A__ ( metaclass=_snake_case ): lowercase = ["torch"] def __init__( self , *UpperCamelCase__ , **UpperCamelCase__ ) -> List[str]: '''simple docstring''' requires_backends(self , ["""torch"""] ) @classmethod def snake_case_ ( cls , *UpperCamelCase__ , **UpperCamelCase__ ) -> int: '''simple docstring''' requires_backends(cls , ["""torch"""] ) @classmethod def snake_case_ ( cls , *UpperCamelCase__ , **UpperCamelCase__ ) -> str: '''simple docstring''' requires_backends(cls , ["""torch"""] ) class A__ ( metaclass=_snake_case ): lowercase = ["torch"] def __init__( self , *UpperCamelCase__ , **UpperCamelCase__ ) -> List[Any]: '''simple docstring''' requires_backends(self , ["""torch"""] ) @classmethod def snake_case_ ( cls , *UpperCamelCase__ , **UpperCamelCase__ ) -> Tuple: '''simple docstring''' requires_backends(cls , ["""torch"""] ) @classmethod def snake_case_ ( cls , *UpperCamelCase__ , **UpperCamelCase__ ) -> List[str]: '''simple docstring''' requires_backends(cls , ["""torch"""] ) class A__ ( metaclass=_snake_case ): lowercase = ["torch"] def __init__( self , *UpperCamelCase__ , **UpperCamelCase__ ) -> Any: '''simple docstring''' requires_backends(self , ["""torch"""] ) @classmethod def snake_case_ ( cls , *UpperCamelCase__ , **UpperCamelCase__ ) -> List[Any]: '''simple docstring''' requires_backends(cls , ["""torch"""] ) @classmethod def snake_case_ ( cls , *UpperCamelCase__ , **UpperCamelCase__ ) -> Dict: '''simple docstring''' requires_backends(cls , ["""torch"""] ) class A__ ( metaclass=_snake_case ): lowercase = ["torch"] def __init__( self , *UpperCamelCase__ , **UpperCamelCase__ ) -> Tuple: '''simple docstring''' requires_backends(self , ["""torch"""] ) @classmethod def snake_case_ ( cls , *UpperCamelCase__ , **UpperCamelCase__ ) -> List[Any]: '''simple docstring''' requires_backends(cls , ["""torch"""] ) @classmethod def snake_case_ ( cls , *UpperCamelCase__ , **UpperCamelCase__ ) -> Any: '''simple docstring''' requires_backends(cls , ["""torch"""] ) class A__ ( metaclass=_snake_case ): lowercase = ["torch"] def __init__( self , *UpperCamelCase__ , **UpperCamelCase__ ) -> Union[str, Any]: '''simple docstring''' requires_backends(self , ["""torch"""] ) @classmethod def snake_case_ ( cls , *UpperCamelCase__ , **UpperCamelCase__ ) -> str: '''simple docstring''' requires_backends(cls , ["""torch"""] ) @classmethod def snake_case_ ( cls , *UpperCamelCase__ , **UpperCamelCase__ ) -> Dict: '''simple docstring''' requires_backends(cls , ["""torch"""] ) class A__ ( metaclass=_snake_case ): lowercase = ["torch"] def __init__( self , *UpperCamelCase__ , **UpperCamelCase__ ) -> List[str]: '''simple docstring''' requires_backends(self , ["""torch"""] ) @classmethod def snake_case_ ( cls , *UpperCamelCase__ , **UpperCamelCase__ ) -> int: '''simple docstring''' requires_backends(cls , ["""torch"""] ) @classmethod def snake_case_ ( cls , *UpperCamelCase__ , **UpperCamelCase__ ) -> Union[str, Any]: '''simple docstring''' requires_backends(cls , ["""torch"""] ) class A__ ( metaclass=_snake_case ): lowercase = ["torch"] def __init__( self , *UpperCamelCase__ , **UpperCamelCase__ ) -> Tuple: '''simple docstring''' requires_backends(self , ["""torch"""] ) @classmethod def snake_case_ ( cls , *UpperCamelCase__ , **UpperCamelCase__ ) -> Tuple: '''simple docstring''' requires_backends(cls , ["""torch"""] ) @classmethod def snake_case_ ( cls , *UpperCamelCase__ , **UpperCamelCase__ ) -> int: '''simple docstring''' requires_backends(cls , ["""torch"""] ) class A__ ( metaclass=_snake_case ): lowercase = ["torch"] def __init__( self , *UpperCamelCase__ , **UpperCamelCase__ ) -> Optional[int]: '''simple docstring''' requires_backends(self , ["""torch"""] ) @classmethod def snake_case_ ( cls , *UpperCamelCase__ , **UpperCamelCase__ ) -> List[str]: '''simple docstring''' requires_backends(cls , ["""torch"""] ) @classmethod def snake_case_ ( cls , *UpperCamelCase__ , **UpperCamelCase__ ) -> Dict: '''simple docstring''' requires_backends(cls , ["""torch"""] ) class A__ ( metaclass=_snake_case ): lowercase = ["torch"] def __init__( self , *UpperCamelCase__ , **UpperCamelCase__ ) -> int: '''simple docstring''' requires_backends(self , ["""torch"""] ) @classmethod def snake_case_ ( cls , *UpperCamelCase__ , **UpperCamelCase__ ) -> int: '''simple docstring''' requires_backends(cls , ["""torch"""] ) @classmethod def snake_case_ ( cls , *UpperCamelCase__ , **UpperCamelCase__ ) -> Any: '''simple docstring''' requires_backends(cls , ["""torch"""] ) class A__ ( metaclass=_snake_case ): lowercase = ["torch"] def __init__( self , *UpperCamelCase__ , **UpperCamelCase__ ) -> Dict: '''simple docstring''' requires_backends(self , ["""torch"""] ) @classmethod def snake_case_ ( cls , *UpperCamelCase__ , **UpperCamelCase__ ) -> List[str]: '''simple docstring''' requires_backends(cls , ["""torch"""] ) @classmethod def snake_case_ ( cls , *UpperCamelCase__ , **UpperCamelCase__ ) -> Any: '''simple docstring''' requires_backends(cls , ["""torch"""] ) class A__ ( metaclass=_snake_case ): lowercase = ["torch"] def __init__( self , *UpperCamelCase__ , **UpperCamelCase__ ) -> int: '''simple docstring''' requires_backends(self , ["""torch"""] ) @classmethod def snake_case_ ( cls , *UpperCamelCase__ , **UpperCamelCase__ ) -> int: '''simple docstring''' requires_backends(cls , ["""torch"""] ) @classmethod def snake_case_ ( cls , *UpperCamelCase__ , **UpperCamelCase__ ) -> Union[str, Any]: '''simple docstring''' requires_backends(cls , ["""torch"""] ) class A__ ( metaclass=_snake_case ): lowercase = ["torch"] def __init__( self , *UpperCamelCase__ , **UpperCamelCase__ ) -> Tuple: '''simple docstring''' requires_backends(self , ["""torch"""] ) @classmethod def snake_case_ ( cls , *UpperCamelCase__ , **UpperCamelCase__ ) -> int: '''simple docstring''' requires_backends(cls , ["""torch"""] ) @classmethod def snake_case_ ( cls , *UpperCamelCase__ , **UpperCamelCase__ ) -> List[Any]: '''simple docstring''' requires_backends(cls , ["""torch"""] ) class A__ ( metaclass=_snake_case ): lowercase = ["torch"] def __init__( self , *UpperCamelCase__ , **UpperCamelCase__ ) -> Dict: '''simple docstring''' requires_backends(self , ["""torch"""] ) @classmethod def snake_case_ ( cls , *UpperCamelCase__ , **UpperCamelCase__ ) -> Dict: '''simple docstring''' requires_backends(cls , ["""torch"""] ) @classmethod def snake_case_ ( cls , *UpperCamelCase__ , **UpperCamelCase__ ) -> List[Any]: '''simple docstring''' requires_backends(cls , ["""torch"""] ) class A__ ( metaclass=_snake_case ): lowercase = ["torch"] def __init__( self , *UpperCamelCase__ , **UpperCamelCase__ ) -> Optional[int]: '''simple docstring''' requires_backends(self , ["""torch"""] ) @classmethod def snake_case_ ( cls , *UpperCamelCase__ , **UpperCamelCase__ ) -> List[Any]: '''simple docstring''' requires_backends(cls , ["""torch"""] ) @classmethod def snake_case_ ( cls , *UpperCamelCase__ , **UpperCamelCase__ ) -> str: '''simple docstring''' requires_backends(cls , ["""torch"""] ) class A__ ( metaclass=_snake_case ): lowercase = ["torch"] def __init__( self , *UpperCamelCase__ , **UpperCamelCase__ ) -> Tuple: '''simple docstring''' requires_backends(self , ["""torch"""] ) @classmethod def snake_case_ ( cls , *UpperCamelCase__ , **UpperCamelCase__ ) -> List[str]: '''simple docstring''' requires_backends(cls , ["""torch"""] ) @classmethod def snake_case_ ( cls , *UpperCamelCase__ , **UpperCamelCase__ ) -> List[str]: '''simple docstring''' requires_backends(cls , ["""torch"""] ) class A__ ( metaclass=_snake_case ): lowercase = ["torch"] def __init__( self , *UpperCamelCase__ , **UpperCamelCase__ ) -> Optional[int]: '''simple docstring''' requires_backends(self , ["""torch"""] ) @classmethod def snake_case_ ( cls , *UpperCamelCase__ , **UpperCamelCase__ ) -> Tuple: '''simple docstring''' requires_backends(cls , ["""torch"""] ) @classmethod def snake_case_ ( cls , *UpperCamelCase__ , **UpperCamelCase__ ) -> Tuple: '''simple docstring''' requires_backends(cls , ["""torch"""] ) class A__ ( metaclass=_snake_case ): lowercase = ["torch"] def __init__( self , *UpperCamelCase__ , **UpperCamelCase__ ) -> List[Any]: '''simple docstring''' requires_backends(self , ["""torch"""] ) @classmethod def snake_case_ ( cls , *UpperCamelCase__ , **UpperCamelCase__ ) -> Union[str, Any]: '''simple docstring''' requires_backends(cls , ["""torch"""] ) @classmethod def snake_case_ ( cls , *UpperCamelCase__ , **UpperCamelCase__ ) -> Dict: '''simple docstring''' requires_backends(cls , ["""torch"""] ) class A__ ( metaclass=_snake_case ): lowercase = ["torch"] def __init__( self , *UpperCamelCase__ , **UpperCamelCase__ ) -> List[str]: '''simple docstring''' requires_backends(self , ["""torch"""] ) @classmethod def snake_case_ ( cls , *UpperCamelCase__ , **UpperCamelCase__ ) -> str: '''simple docstring''' requires_backends(cls , ["""torch"""] ) @classmethod def snake_case_ ( cls , *UpperCamelCase__ , **UpperCamelCase__ ) -> Tuple: '''simple docstring''' requires_backends(cls , ["""torch"""] ) class A__ ( metaclass=_snake_case ): lowercase = ["torch"] def __init__( self , *UpperCamelCase__ , **UpperCamelCase__ ) -> Any: '''simple docstring''' requires_backends(self , ["""torch"""] ) @classmethod def snake_case_ ( cls , *UpperCamelCase__ , **UpperCamelCase__ ) -> int: '''simple docstring''' requires_backends(cls , ["""torch"""] ) @classmethod def snake_case_ ( cls , *UpperCamelCase__ , **UpperCamelCase__ ) -> Any: '''simple docstring''' requires_backends(cls , ["""torch"""] ) class A__ ( metaclass=_snake_case ): lowercase = ["torch"] def __init__( self , *UpperCamelCase__ , **UpperCamelCase__ ) -> List[str]: '''simple docstring''' requires_backends(self , ["""torch"""] ) @classmethod def snake_case_ ( cls , *UpperCamelCase__ , **UpperCamelCase__ ) -> List[Any]: '''simple docstring''' requires_backends(cls , ["""torch"""] ) @classmethod def snake_case_ ( cls , *UpperCamelCase__ , **UpperCamelCase__ ) -> List[str]: '''simple docstring''' requires_backends(cls , ["""torch"""] ) class A__ ( metaclass=_snake_case ): lowercase = ["torch"] def __init__( self , *UpperCamelCase__ , **UpperCamelCase__ ) -> Optional[int]: '''simple docstring''' requires_backends(self , ["""torch"""] ) @classmethod def snake_case_ ( cls , *UpperCamelCase__ , **UpperCamelCase__ ) -> Dict: '''simple docstring''' requires_backends(cls , ["""torch"""] ) @classmethod def snake_case_ ( cls , *UpperCamelCase__ , **UpperCamelCase__ ) -> int: '''simple docstring''' requires_backends(cls , ["""torch"""] )
667
'''simple docstring''' import warnings from diffusers import StableDiffusionImgaImgPipeline # noqa F401 warnings.warn( '''The `image_to_image.py` script is outdated. Please use directly `from diffusers import''' ''' StableDiffusionImg2ImgPipeline` instead.''' )
667
1
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_sentencepiece_available, is_speech_available, is_tf_available, is_torch_available, ) __lowerCamelCase = { '''configuration_speech_to_text''': ['''SPEECH_TO_TEXT_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''Speech2TextConfig'''], '''processing_speech_to_text''': ['''Speech2TextProcessor'''], } try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __lowerCamelCase = ['''Speech2TextTokenizer'''] try: if not is_speech_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __lowerCamelCase = ['''Speech2TextFeatureExtractor'''] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __lowerCamelCase = [ '''TF_SPEECH_TO_TEXT_PRETRAINED_MODEL_ARCHIVE_LIST''', '''TFSpeech2TextForConditionalGeneration''', '''TFSpeech2TextModel''', '''TFSpeech2TextPreTrainedModel''', ] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __lowerCamelCase = [ '''SPEECH_TO_TEXT_PRETRAINED_MODEL_ARCHIVE_LIST''', '''Speech2TextForConditionalGeneration''', '''Speech2TextModel''', '''Speech2TextPreTrainedModel''', ] if TYPE_CHECKING: from .configuration_speech_to_text import SPEECH_TO_TEXT_PRETRAINED_CONFIG_ARCHIVE_MAP, SpeechaTextConfig from .processing_speech_to_text import SpeechaTextProcessor try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_speech_to_text import SpeechaTextTokenizer try: if not is_speech_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .feature_extraction_speech_to_text import SpeechaTextFeatureExtractor try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_speech_to_text import ( TF_SPEECH_TO_TEXT_PRETRAINED_MODEL_ARCHIVE_LIST, TFSpeechaTextForConditionalGeneration, TFSpeechaTextModel, TFSpeechaTextPreTrainedModel, ) try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_speech_to_text import ( SPEECH_TO_TEXT_PRETRAINED_MODEL_ARCHIVE_LIST, SpeechaTextForConditionalGeneration, SpeechaTextModel, SpeechaTextPreTrainedModel, ) else: import sys __lowerCamelCase = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
667
'''simple docstring''' import importlib.util import os import platform from argparse import ArgumentParser import huggingface_hub from .. import __version__ as version from ..utils import ( is_accelerate_available, is_flax_available, is_safetensors_available, is_tf_available, is_torch_available, ) from . import BaseTransformersCLICommand def UpperCAmelCase__ ( UpperCAmelCase__ ) -> Tuple: return EnvironmentCommand() def UpperCAmelCase__ ( UpperCAmelCase__ ) -> int: return EnvironmentCommand(args.accelerate_config_file ) class A__ ( _snake_case ): @staticmethod def snake_case_ ( UpperCamelCase__ ) -> Union[str, Any]: '''simple docstring''' A_ = parser.add_parser("""env""" ) download_parser.set_defaults(func=UpperCamelCase__ ) download_parser.add_argument( """--accelerate-config_file""" , default=UpperCamelCase__ , help="""The accelerate config file to use for the default values in the launching script.""" , ) download_parser.set_defaults(func=UpperCamelCase__ ) def __init__( self , UpperCamelCase__ , *UpperCamelCase__ ) -> None: '''simple docstring''' A_ = accelerate_config_file def snake_case_ ( self ) -> List[str]: '''simple docstring''' A_ = """not installed""" if is_safetensors_available(): import safetensors A_ = safetensors.__version__ elif importlib.util.find_spec("""safetensors""" ) is not None: import safetensors A_ = f'''{safetensors.__version__} but is ignored because of PyTorch version too old.''' A_ = """not installed""" A_ = A_ = """not found""" if is_accelerate_available(): import accelerate from accelerate.commands.config import default_config_file, load_config_from_file A_ = accelerate.__version__ # Get the default from the config file. if self._accelerate_config_file is not None or os.path.isfile(UpperCamelCase__ ): A_ = load_config_from_file(self._accelerate_config_file ).to_dict() A_ = ( """\n""".join([f'''\t- {prop}: {val}''' for prop, val in accelerate_config.items()] ) if isinstance(UpperCamelCase__ , UpperCamelCase__ ) else f'''\t{accelerate_config}''' ) A_ = """not installed""" A_ = """NA""" if is_torch_available(): import torch A_ = torch.__version__ A_ = torch.cuda.is_available() A_ = """not installed""" A_ = """NA""" if is_tf_available(): import tensorflow as tf A_ = tf.__version__ try: # deprecated in v2.1 A_ = tf.test.is_gpu_available() except AttributeError: # returns list of devices, convert to bool A_ = bool(tf.config.list_physical_devices("""GPU""" ) ) A_ = """not installed""" A_ = """not installed""" A_ = """not installed""" A_ = """NA""" if is_flax_available(): import flax import jax import jaxlib A_ = flax.__version__ A_ = jax.__version__ A_ = jaxlib.__version__ A_ = jax.lib.xla_bridge.get_backend().platform A_ = { """`transformers` version""": version, """Platform""": platform.platform(), """Python version""": platform.python_version(), """Huggingface_hub version""": huggingface_hub.__version__, """Safetensors version""": f'''{safetensors_version}''', """Accelerate version""": f'''{accelerate_version}''', """Accelerate config""": f'''{accelerate_config_str}''', """PyTorch version (GPU?)""": f'''{pt_version} ({pt_cuda_available})''', """Tensorflow version (GPU?)""": f'''{tf_version} ({tf_cuda_available})''', """Flax version (CPU?/GPU?/TPU?)""": f'''{flax_version} ({jax_backend})''', """Jax version""": f'''{jax_version}''', """JaxLib version""": f'''{jaxlib_version}''', """Using GPU in script?""": """<fill in>""", """Using distributed or parallel set-up in script?""": """<fill in>""", } print("""\nCopy-and-paste the text below in your GitHub issue and FILL OUT the two last points.\n""" ) print(self.format_dict(UpperCamelCase__ ) ) return info @staticmethod def snake_case_ ( UpperCamelCase__ ) -> List[str]: '''simple docstring''' return "\n".join([f'''- {prop}: {val}''' for prop, val in d.items()] ) + "\n"
667
1
'''simple docstring''' from typing import Optional, Tuple, Union import torch from einops import rearrange, reduce from diffusers import DDIMScheduler, DDPMScheduler, DiffusionPipeline, ImagePipelineOutput, UNetaDConditionModel from diffusers.schedulers.scheduling_ddim import DDIMSchedulerOutput from diffusers.schedulers.scheduling_ddpm import DDPMSchedulerOutput __lowerCamelCase = 8 def UpperCAmelCase__ ( UpperCAmelCase__, UpperCAmelCase__=BITS ) -> Any: A_ = x.device A_ = (x * 2_55).int().clamp(0, 2_55 ) A_ = 2 ** torch.arange(bits - 1, -1, -1, device=UpperCAmelCase__ ) A_ = rearrange(UpperCAmelCase__, """d -> d 1 1""" ) A_ = rearrange(UpperCAmelCase__, """b c h w -> b c 1 h w""" ) A_ = ((x & mask) != 0).float() A_ = rearrange(UpperCAmelCase__, """b c d h w -> b (c d) h w""" ) A_ = bits * 2 - 1 return bits def UpperCAmelCase__ ( UpperCAmelCase__, UpperCAmelCase__=BITS ) -> List[str]: A_ = x.device A_ = (x > 0).int() A_ = 2 ** torch.arange(bits - 1, -1, -1, device=UpperCAmelCase__, dtype=torch.intaa ) A_ = rearrange(UpperCAmelCase__, """d -> d 1 1""" ) A_ = rearrange(UpperCAmelCase__, """b (c d) h w -> b c d h w""", d=8 ) A_ = reduce(x * mask, """b c d h w -> b c h w""", """sum""" ) return (dec / 2_55).clamp(0.0, 1.0 ) def UpperCAmelCase__ ( self, UpperCAmelCase__, UpperCAmelCase__, UpperCAmelCase__, UpperCAmelCase__ = 0.0, UpperCAmelCase__ = True, UpperCAmelCase__=None, UpperCAmelCase__ = True, ) -> Union[DDIMSchedulerOutput, Tuple]: if self.num_inference_steps is None: raise ValueError( """Number of inference steps is 'None', you need to run 'set_timesteps' after creating the scheduler""" ) # See formulas (12) and (16) of DDIM paper https://arxiv.org/pdf/2010.02502.pdf # Ideally, read DDIM paper in-detail understanding # Notation (<variable name> -> <name in paper> # - pred_noise_t -> e_theta(x_t, t) # - pred_original_sample -> f_theta(x_t, t) or x_0 # - std_dev_t -> sigma_t # - eta -> ฮท # - pred_sample_direction -> "direction pointing to x_t" # - pred_prev_sample -> "x_t-1" # 1. get previous step value (=t-1) A_ = timestep - self.config.num_train_timesteps // self.num_inference_steps # 2. compute alphas, betas A_ = self.alphas_cumprod[timestep] A_ = self.alphas_cumprod[prev_timestep] if prev_timestep >= 0 else self.final_alpha_cumprod A_ = 1 - alpha_prod_t # 3. compute predicted original sample from predicted noise also called # "predicted x_0" of formula (12) from https://arxiv.org/pdf/2010.02502.pdf A_ = (sample - beta_prod_t ** 0.5 * model_output) / alpha_prod_t ** 0.5 # 4. Clip "predicted x_0" A_ = self.bit_scale if self.config.clip_sample: A_ = torch.clamp(UpperCAmelCase__, -scale, UpperCAmelCase__ ) # 5. compute variance: "sigma_t(ฮท)" -> see formula (16) # ฯƒ_t = sqrt((1 โˆ’ ฮฑ_tโˆ’1)/(1 โˆ’ ฮฑ_t)) * sqrt(1 โˆ’ ฮฑ_t/ฮฑ_tโˆ’1) A_ = self._get_variance(UpperCAmelCase__, UpperCAmelCase__ ) A_ = eta * variance ** 0.5 if use_clipped_model_output: # the model_output is always re-derived from the clipped x_0 in Glide A_ = (sample - alpha_prod_t ** 0.5 * pred_original_sample) / beta_prod_t ** 0.5 # 6. compute "direction pointing to x_t" of formula (12) from https://arxiv.org/pdf/2010.02502.pdf A_ = (1 - alpha_prod_t_prev - std_dev_t**2) ** 0.5 * model_output # 7. compute x_t without "random noise" of formula (12) from https://arxiv.org/pdf/2010.02502.pdf A_ = alpha_prod_t_prev ** 0.5 * pred_original_sample + pred_sample_direction if eta > 0: # randn_like does not support generator https://github.com/pytorch/pytorch/issues/27072 A_ = model_output.device if torch.is_tensor(UpperCAmelCase__ ) else """cpu""" A_ = torch.randn(model_output.shape, dtype=model_output.dtype, generator=UpperCAmelCase__ ).to(UpperCAmelCase__ ) A_ = self._get_variance(UpperCAmelCase__, UpperCAmelCase__ ) ** 0.5 * eta * noise A_ = prev_sample + variance if not return_dict: return (prev_sample,) return DDIMSchedulerOutput(prev_sample=UpperCAmelCase__, pred_original_sample=UpperCAmelCase__ ) def UpperCAmelCase__ ( self, UpperCAmelCase__, UpperCAmelCase__, UpperCAmelCase__, UpperCAmelCase__="epsilon", UpperCAmelCase__=None, UpperCAmelCase__ = True, ) -> Union[DDPMSchedulerOutput, Tuple]: A_ = timestep if model_output.shape[1] == sample.shape[1] * 2 and self.variance_type in ["learned", "learned_range"]: A_ , A_ = torch.split(UpperCAmelCase__, sample.shape[1], dim=1 ) else: A_ = None # 1. compute alphas, betas A_ = self.alphas_cumprod[t] A_ = self.alphas_cumprod[t - 1] if t > 0 else self.one A_ = 1 - alpha_prod_t A_ = 1 - alpha_prod_t_prev # 2. compute predicted original sample from predicted noise also called # "predicted x_0" of formula (15) from https://arxiv.org/pdf/2006.11239.pdf if prediction_type == "epsilon": A_ = (sample - beta_prod_t ** 0.5 * model_output) / alpha_prod_t ** 0.5 elif prediction_type == "sample": A_ = model_output else: raise ValueError(F'''Unsupported prediction_type {prediction_type}.''' ) # 3. Clip "predicted x_0" A_ = self.bit_scale if self.config.clip_sample: A_ = torch.clamp(UpperCAmelCase__, -scale, UpperCAmelCase__ ) # 4. Compute coefficients for pred_original_sample x_0 and current sample x_t # See formula (7) from https://arxiv.org/pdf/2006.11239.pdf A_ = (alpha_prod_t_prev ** 0.5 * self.betas[t]) / beta_prod_t A_ = self.alphas[t] ** 0.5 * beta_prod_t_prev / beta_prod_t # 5. Compute predicted previous sample ยต_t # See formula (7) from https://arxiv.org/pdf/2006.11239.pdf A_ = pred_original_sample_coeff * pred_original_sample + current_sample_coeff * sample # 6. Add noise A_ = 0 if t > 0: A_ = torch.randn( model_output.size(), dtype=model_output.dtype, layout=model_output.layout, generator=UpperCAmelCase__ ).to(model_output.device ) A_ = (self._get_variance(UpperCAmelCase__, predicted_variance=UpperCAmelCase__ ) ** 0.5) * noise A_ = pred_prev_sample + variance if not return_dict: return (pred_prev_sample,) return DDPMSchedulerOutput(prev_sample=UpperCAmelCase__, pred_original_sample=UpperCAmelCase__ ) class A__ ( _snake_case ): def __init__( self , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ = 1.0 , ) -> str: '''simple docstring''' super().__init__() A_ = bit_scale A_ = ( ddim_bit_scheduler_step if isinstance(UpperCamelCase__ , UpperCamelCase__ ) else ddpm_bit_scheduler_step ) self.register_modules(unet=UpperCamelCase__ , scheduler=UpperCamelCase__ ) @torch.no_grad() def __call__( self , UpperCamelCase__ = 256 , UpperCamelCase__ = 256 , UpperCamelCase__ = 50 , UpperCamelCase__ = None , UpperCamelCase__ = 1 , UpperCamelCase__ = "pil" , UpperCamelCase__ = True , **UpperCamelCase__ , ) -> Union[Tuple, ImagePipelineOutput]: '''simple docstring''' A_ = torch.randn( (batch_size, self.unet.config.in_channels, height, width) , generator=UpperCamelCase__ , ) A_ = decimal_to_bits(UpperCamelCase__ ) * self.bit_scale A_ = latents.to(self.device ) self.scheduler.set_timesteps(UpperCamelCase__ ) for t in self.progress_bar(self.scheduler.timesteps ): # predict the noise residual A_ = self.unet(UpperCamelCase__ , UpperCamelCase__ ).sample # compute the previous noisy sample x_t -> x_t-1 A_ = self.scheduler.step(UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ ).prev_sample A_ = bits_to_decimal(UpperCamelCase__ ) if output_type == "pil": A_ = self.numpy_to_pil(UpperCamelCase__ ) if not return_dict: return (image,) return ImagePipelineOutput(images=UpperCamelCase__ )
667
'''simple docstring''' import unittest import numpy as np import torch from torch import nn from transformers import ( CLIPImageProcessor, CLIPTextConfig, CLIPTextModelWithProjection, CLIPTokenizer, CLIPVisionConfig, CLIPVisionModelWithProjection, ) from diffusers import KandinskyVaaPriorPipeline, PriorTransformer, UnCLIPScheduler from diffusers.utils import torch_device from diffusers.utils.testing_utils import enable_full_determinism, skip_mps from ..test_pipelines_common import PipelineTesterMixin enable_full_determinism() class A__ ( _snake_case , unittest.TestCase ): lowercase = KandinskyVaaPriorPipeline lowercase = ["prompt"] lowercase = ["prompt", "negative_prompt"] lowercase = [ "num_images_per_prompt", "generator", "num_inference_steps", "latents", "negative_prompt", "guidance_scale", "output_type", "return_dict", ] lowercase = False @property def snake_case_ ( self ) -> Any: '''simple docstring''' return 32 @property def snake_case_ ( self ) -> Optional[int]: '''simple docstring''' return 32 @property def snake_case_ ( self ) -> Optional[Any]: '''simple docstring''' return self.time_input_dim @property def snake_case_ ( self ) -> str: '''simple docstring''' return self.time_input_dim * 4 @property def snake_case_ ( self ) -> int: '''simple docstring''' return 100 @property def snake_case_ ( self ) -> Optional[Any]: '''simple docstring''' A_ = CLIPTokenizer.from_pretrained("""hf-internal-testing/tiny-random-clip""" ) return tokenizer @property def snake_case_ ( self ) -> Union[str, Any]: '''simple docstring''' torch.manual_seed(0 ) A_ = CLIPTextConfig( bos_token_id=0 , eos_token_id=2 , hidden_size=self.text_embedder_hidden_size , projection_dim=self.text_embedder_hidden_size , intermediate_size=37 , layer_norm_eps=1e-0_5 , num_attention_heads=4 , num_hidden_layers=5 , pad_token_id=1 , vocab_size=1000 , ) return CLIPTextModelWithProjection(UpperCamelCase__ ) @property def snake_case_ ( self ) -> Tuple: '''simple docstring''' torch.manual_seed(0 ) A_ = { """num_attention_heads""": 2, """attention_head_dim""": 12, """embedding_dim""": self.text_embedder_hidden_size, """num_layers""": 1, } A_ = PriorTransformer(**UpperCamelCase__ ) # clip_std and clip_mean is initialized to be 0 so PriorTransformer.post_process_latents will always return 0 - set clip_std to be 1 so it won't return 0 A_ = nn.Parameter(torch.ones(model.clip_std.shape ) ) return model @property def snake_case_ ( self ) -> str: '''simple docstring''' torch.manual_seed(0 ) A_ = CLIPVisionConfig( hidden_size=self.text_embedder_hidden_size , image_size=224 , projection_dim=self.text_embedder_hidden_size , intermediate_size=37 , num_attention_heads=4 , num_channels=3 , num_hidden_layers=5 , patch_size=14 , ) A_ = CLIPVisionModelWithProjection(UpperCamelCase__ ) return model @property def snake_case_ ( self ) -> str: '''simple docstring''' A_ = CLIPImageProcessor( crop_size=224 , do_center_crop=UpperCamelCase__ , do_normalize=UpperCamelCase__ , do_resize=UpperCamelCase__ , image_mean=[0.48145466, 0.4578275, 0.40821073] , image_std=[0.26862954, 0.26130258, 0.27577711] , resample=3 , size=224 , ) return image_processor def snake_case_ ( self ) -> Union[str, Any]: '''simple docstring''' A_ = self.dummy_prior A_ = self.dummy_image_encoder A_ = self.dummy_text_encoder A_ = self.dummy_tokenizer A_ = self.dummy_image_processor A_ = UnCLIPScheduler( variance_type="""fixed_small_log""" , prediction_type="""sample""" , num_train_timesteps=1000 , clip_sample=UpperCamelCase__ , clip_sample_range=10.0 , ) A_ = { """prior""": prior, """image_encoder""": image_encoder, """text_encoder""": text_encoder, """tokenizer""": tokenizer, """scheduler""": scheduler, """image_processor""": image_processor, } return components def snake_case_ ( self , UpperCamelCase__ , UpperCamelCase__=0 ) -> Optional[int]: '''simple docstring''' if str(UpperCamelCase__ ).startswith("""mps""" ): A_ = torch.manual_seed(UpperCamelCase__ ) else: A_ = torch.Generator(device=UpperCamelCase__ ).manual_seed(UpperCamelCase__ ) A_ = { """prompt""": """horse""", """generator""": generator, """guidance_scale""": 4.0, """num_inference_steps""": 2, """output_type""": """np""", } return inputs def snake_case_ ( self ) -> List[str]: '''simple docstring''' A_ = """cpu""" A_ = self.get_dummy_components() A_ = self.pipeline_class(**UpperCamelCase__ ) A_ = pipe.to(UpperCamelCase__ ) pipe.set_progress_bar_config(disable=UpperCamelCase__ ) A_ = pipe(**self.get_dummy_inputs(UpperCamelCase__ ) ) A_ = output.image_embeds A_ = pipe( **self.get_dummy_inputs(UpperCamelCase__ ) , return_dict=UpperCamelCase__ , )[0] A_ = image[0, -10:] A_ = image_from_tuple[0, -10:] assert image.shape == (1, 32) A_ = np.array( [-0.0532, 1.7120, 0.3656, -1.0852, -0.8946, -1.1756, 0.4348, 0.2482, 0.5146, -0.1156] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-2 assert np.abs(image_from_tuple_slice.flatten() - expected_slice ).max() < 1e-2 @skip_mps def snake_case_ ( self ) -> int: '''simple docstring''' A_ = torch_device == """cpu""" A_ = True A_ = False self._test_inference_batch_single_identical( test_max_difference=UpperCamelCase__ , relax_max_difference=UpperCamelCase__ , test_mean_pixel_difference=UpperCamelCase__ , ) @skip_mps def snake_case_ ( self ) -> Union[str, Any]: '''simple docstring''' A_ = torch_device == """cpu""" A_ = False self._test_attention_slicing_forward_pass( test_max_difference=UpperCamelCase__ , test_mean_pixel_difference=UpperCamelCase__ , )
667
1
'''simple docstring''' import logging from transformers import PretrainedConfig __lowerCamelCase = logging.getLogger(__name__) __lowerCamelCase = { '''bertabs-finetuned-cnndm''': '''https://huggingface.co/remi/bertabs-finetuned-cnndm-extractive-abstractive-summarization/resolve/main/config.json''', } class A__ ( _snake_case ): lowercase = "bertabs" def __init__( self , UpperCamelCase__=30522 , UpperCamelCase__=512 , UpperCamelCase__=6 , UpperCamelCase__=512 , UpperCamelCase__=8 , UpperCamelCase__=512 , UpperCamelCase__=0.2 , UpperCamelCase__=6 , UpperCamelCase__=768 , UpperCamelCase__=8 , UpperCamelCase__=2048 , UpperCamelCase__=0.2 , **UpperCamelCase__ , ) -> Optional[int]: '''simple docstring''' super().__init__(**UpperCamelCase__ ) A_ = vocab_size A_ = max_pos A_ = enc_layers A_ = enc_hidden_size A_ = enc_heads A_ = enc_ff_size A_ = enc_dropout A_ = dec_layers A_ = dec_hidden_size A_ = dec_heads A_ = dec_ff_size A_ = dec_dropout
667
'''simple docstring''' import tempfile import torch from diffusers import IPNDMScheduler from .test_schedulers import SchedulerCommonTest class A__ ( _snake_case ): lowercase = (IPNDMScheduler,) lowercase = (("num_inference_steps", 50),) def snake_case_ ( self , **UpperCamelCase__ ) -> Optional[Any]: '''simple docstring''' A_ = {"""num_train_timesteps""": 1000} config.update(**UpperCamelCase__ ) return config def snake_case_ ( self , UpperCamelCase__=0 , **UpperCamelCase__ ) -> str: '''simple docstring''' A_ = dict(self.forward_default_kwargs ) A_ = kwargs.pop("""num_inference_steps""" , UpperCamelCase__ ) A_ = self.dummy_sample A_ = 0.1 * sample A_ = [residual + 0.2, residual + 0.15, residual + 0.1, residual + 0.05] for scheduler_class in self.scheduler_classes: A_ = self.get_scheduler_config(**UpperCamelCase__ ) A_ = scheduler_class(**UpperCamelCase__ ) scheduler.set_timesteps(UpperCamelCase__ ) # copy over dummy past residuals A_ = dummy_past_residuals[:] if time_step is None: A_ = scheduler.timesteps[len(scheduler.timesteps ) // 2] with tempfile.TemporaryDirectory() as tmpdirname: scheduler.save_config(UpperCamelCase__ ) A_ = scheduler_class.from_pretrained(UpperCamelCase__ ) new_scheduler.set_timesteps(UpperCamelCase__ ) # copy over dummy past residuals A_ = dummy_past_residuals[:] A_ = scheduler.step(UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , **UpperCamelCase__ ).prev_sample A_ = new_scheduler.step(UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , **UpperCamelCase__ ).prev_sample assert torch.sum(torch.abs(output - new_output ) ) < 1e-5, "Scheduler outputs are not identical" A_ = scheduler.step(UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , **UpperCamelCase__ ).prev_sample A_ = new_scheduler.step(UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , **UpperCamelCase__ ).prev_sample assert torch.sum(torch.abs(output - new_output ) ) < 1e-5, "Scheduler outputs are not identical" def snake_case_ ( self ) -> Optional[int]: '''simple docstring''' pass def snake_case_ ( self , UpperCamelCase__=0 , **UpperCamelCase__ ) -> str: '''simple docstring''' A_ = dict(self.forward_default_kwargs ) A_ = kwargs.pop("""num_inference_steps""" , UpperCamelCase__ ) A_ = self.dummy_sample A_ = 0.1 * sample A_ = [residual + 0.2, residual + 0.15, residual + 0.1, residual + 0.05] for scheduler_class in self.scheduler_classes: A_ = self.get_scheduler_config() A_ = scheduler_class(**UpperCamelCase__ ) scheduler.set_timesteps(UpperCamelCase__ ) # copy over dummy past residuals (must be after setting timesteps) A_ = dummy_past_residuals[:] if time_step is None: A_ = scheduler.timesteps[len(scheduler.timesteps ) // 2] with tempfile.TemporaryDirectory() as tmpdirname: scheduler.save_config(UpperCamelCase__ ) A_ = scheduler_class.from_pretrained(UpperCamelCase__ ) # copy over dummy past residuals new_scheduler.set_timesteps(UpperCamelCase__ ) # copy over dummy past residual (must be after setting timesteps) A_ = dummy_past_residuals[:] A_ = scheduler.step(UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , **UpperCamelCase__ ).prev_sample A_ = new_scheduler.step(UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , **UpperCamelCase__ ).prev_sample assert torch.sum(torch.abs(output - new_output ) ) < 1e-5, "Scheduler outputs are not identical" A_ = scheduler.step(UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , **UpperCamelCase__ ).prev_sample A_ = new_scheduler.step(UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , **UpperCamelCase__ ).prev_sample assert torch.sum(torch.abs(output - new_output ) ) < 1e-5, "Scheduler outputs are not identical" def snake_case_ ( self , **UpperCamelCase__ ) -> Tuple: '''simple docstring''' A_ = self.scheduler_classes[0] A_ = self.get_scheduler_config(**UpperCamelCase__ ) A_ = scheduler_class(**UpperCamelCase__ ) A_ = 10 A_ = self.dummy_model() A_ = self.dummy_sample_deter scheduler.set_timesteps(UpperCamelCase__ ) for i, t in enumerate(scheduler.timesteps ): A_ = model(UpperCamelCase__ , UpperCamelCase__ ) A_ = scheduler.step(UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ ).prev_sample for i, t in enumerate(scheduler.timesteps ): A_ = model(UpperCamelCase__ , UpperCamelCase__ ) A_ = scheduler.step(UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ ).prev_sample return sample def snake_case_ ( self ) -> Union[str, Any]: '''simple docstring''' A_ = dict(self.forward_default_kwargs ) A_ = kwargs.pop("""num_inference_steps""" , UpperCamelCase__ ) for scheduler_class in self.scheduler_classes: A_ = self.get_scheduler_config() A_ = scheduler_class(**UpperCamelCase__ ) A_ = self.dummy_sample A_ = 0.1 * sample if num_inference_steps is not None and hasattr(UpperCamelCase__ , """set_timesteps""" ): scheduler.set_timesteps(UpperCamelCase__ ) elif num_inference_steps is not None and not hasattr(UpperCamelCase__ , """set_timesteps""" ): A_ = num_inference_steps # copy over dummy past residuals (must be done after set_timesteps) A_ = [residual + 0.2, residual + 0.15, residual + 0.1, residual + 0.05] A_ = dummy_past_residuals[:] A_ = scheduler.timesteps[5] A_ = scheduler.timesteps[6] A_ = scheduler.step(UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , **UpperCamelCase__ ).prev_sample A_ = scheduler.step(UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , **UpperCamelCase__ ).prev_sample self.assertEqual(output_a.shape , sample.shape ) self.assertEqual(output_a.shape , output_a.shape ) A_ = scheduler.step(UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , **UpperCamelCase__ ).prev_sample A_ = scheduler.step(UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , **UpperCamelCase__ ).prev_sample self.assertEqual(output_a.shape , sample.shape ) self.assertEqual(output_a.shape , output_a.shape ) def snake_case_ ( self ) -> Any: '''simple docstring''' for timesteps in [100, 1000]: self.check_over_configs(num_train_timesteps=UpperCamelCase__ , time_step=UpperCamelCase__ ) def snake_case_ ( self ) -> Any: '''simple docstring''' for t, num_inference_steps in zip([1, 5, 10] , [10, 50, 100] ): self.check_over_forward(num_inference_steps=UpperCamelCase__ , time_step=UpperCamelCase__ ) def snake_case_ ( self ) -> Dict: '''simple docstring''' A_ = self.full_loop() A_ = torch.mean(torch.abs(UpperCamelCase__ ) ) assert abs(result_mean.item() - 2540529 ) < 10
667
1
'''simple docstring''' import contextlib import os import sqlitea import pytest from datasets import Dataset, Features, Value from datasets.io.sql import SqlDatasetReader, SqlDatasetWriter from ..utils import assert_arrow_memory_doesnt_increase, assert_arrow_memory_increases, require_sqlalchemy def UpperCAmelCase__ ( UpperCAmelCase__, UpperCAmelCase__ ) -> str: assert isinstance(UpperCAmelCase__, UpperCAmelCase__ ) assert dataset.num_rows == 4 assert dataset.num_columns == 3 assert dataset.column_names == ["col_1", "col_2", "col_3"] for feature, expected_dtype in expected_features.items(): assert dataset.features[feature].dtype == expected_dtype @require_sqlalchemy @pytest.mark.parametrize("""keep_in_memory""", [False, True] ) def UpperCAmelCase__ ( UpperCAmelCase__, UpperCAmelCase__, UpperCAmelCase__, UpperCAmelCase__ ) -> List[Any]: A_ = tmp_path / """cache""" A_ = {"""col_1""": """string""", """col_2""": """int64""", """col_3""": """float64"""} with assert_arrow_memory_increases() if keep_in_memory else assert_arrow_memory_doesnt_increase(): A_ = SqlDatasetReader( """dataset""", """sqlite:///""" + sqlite_path, cache_dir=UpperCAmelCase__, keep_in_memory=UpperCAmelCase__ ).read() _check_sql_dataset(UpperCAmelCase__, UpperCAmelCase__ ) @require_sqlalchemy @pytest.mark.parametrize( """features""", [ None, {"""col_1""": """string""", """col_2""": """int64""", """col_3""": """float64"""}, {"""col_1""": """string""", """col_2""": """string""", """col_3""": """string"""}, {"""col_1""": """int32""", """col_2""": """int32""", """col_3""": """int32"""}, {"""col_1""": """float32""", """col_2""": """float32""", """col_3""": """float32"""}, ], ) def UpperCAmelCase__ ( UpperCAmelCase__, UpperCAmelCase__, UpperCAmelCase__, UpperCAmelCase__ ) -> List[str]: A_ = tmp_path / """cache""" A_ = {"""col_1""": """string""", """col_2""": """int64""", """col_3""": """float64"""} A_ = features.copy() if features else default_expected_features A_ = ( Features({feature: Value(UpperCAmelCase__ ) for feature, dtype in features.items()} ) if features is not None else None ) A_ = SqlDatasetReader("""dataset""", """sqlite:///""" + sqlite_path, features=UpperCAmelCase__, cache_dir=UpperCAmelCase__ ).read() _check_sql_dataset(UpperCAmelCase__, UpperCAmelCase__ ) def UpperCAmelCase__ ( UpperCAmelCase__ ) -> Union[str, Any]: with contextlib.closing(sqlitea.connect(UpperCAmelCase__ ) ) as con: A_ = con.cursor() cur.execute("""SELECT * FROM dataset""" ) for row in cur: yield row @require_sqlalchemy def UpperCAmelCase__ ( UpperCAmelCase__, UpperCAmelCase__, UpperCAmelCase__ ) -> str: A_ = tmp_path / """cache""" A_ = os.path.join(UpperCAmelCase__, """tmp.sql""" ) A_ = SqlDatasetReader("""dataset""", """sqlite:///""" + sqlite_path, cache_dir=UpperCAmelCase__ ).read() SqlDatasetWriter(UpperCAmelCase__, """dataset""", """sqlite:///""" + output_sqlite_path, num_proc=1 ).write() A_ = iter_sql_file(UpperCAmelCase__ ) A_ = iter_sql_file(UpperCAmelCase__ ) for rowa, rowa in zip(UpperCAmelCase__, UpperCAmelCase__ ): assert rowa == rowa @require_sqlalchemy def UpperCAmelCase__ ( UpperCAmelCase__, UpperCAmelCase__, UpperCAmelCase__ ) -> List[Any]: A_ = tmp_path / """cache""" A_ = os.path.join(UpperCAmelCase__, """tmp.sql""" ) A_ = SqlDatasetReader("""dataset""", """sqlite:///""" + sqlite_path, cache_dir=UpperCAmelCase__ ).read() SqlDatasetWriter(UpperCAmelCase__, """dataset""", """sqlite:///""" + output_sqlite_path, num_proc=2 ).write() A_ = iter_sql_file(UpperCAmelCase__ ) A_ = iter_sql_file(UpperCAmelCase__ ) for rowa, rowa in zip(UpperCAmelCase__, UpperCAmelCase__ ): assert rowa == rowa @require_sqlalchemy def UpperCAmelCase__ ( UpperCAmelCase__, UpperCAmelCase__, UpperCAmelCase__ ) -> Any: A_ = tmp_path / """cache""" A_ = os.path.join(UpperCAmelCase__, """tmp.sql""" ) A_ = SqlDatasetReader("""dataset""", """sqlite:///""" + sqlite_path, cache_dir=UpperCAmelCase__ ).read() with pytest.raises(UpperCAmelCase__ ): SqlDatasetWriter(UpperCAmelCase__, """dataset""", """sqlite:///""" + output_sqlite_path, num_proc=0 ).write()
667
'''simple docstring''' import argparse import re from flax.traverse_util import flatten_dict, unflatten_dict from tax import checkpoints from transformers import SwitchTransformersConfig, SwitchTransformersForConditionalGeneration from transformers.modeling_flax_pytorch_utils import load_flax_weights_in_pytorch_model from transformers.utils import logging logging.set_verbosity_info() # should not include what is already done by the `from_pt` argument __lowerCamelCase = { '''/attention/''': '''/0/SelfAttention/''', '''/self_attention/''': '''/0/SelfAttention/''', '''/encoder_decoder_attention/''': '''/1/EncDecAttention/''', '''value''': '''v''', '''query''': '''q''', '''key''': '''k''', '''out''': '''o''', '''pre_self_attention_layer_norm''': '''0/layer_norm''', '''pre_cross_attention_layer_norm''': '''1/layer_norm''', '''pre_attention_layer_norm''': '''0/layer_norm''', # previously 1, but seems wrong '''token_embedder''': '''shared''', '''encoder_norm''': '''final_layer_norm''', '''decoder_norm''': '''final_layer_norm''', '''relpos_bias/rel_embedding''': '''block/0/layer/0/SelfAttention/relative_attention_bias/weight''', '''router/router_weights/w/''': '''router/classifier/''', '''roer/roer_weights/w/''': '''router/classifier/''', '''logits_dense''': '''lm_head''', } def UpperCAmelCase__ ( UpperCAmelCase__ ) -> Tuple: # 1. in HF T5, we have block.{x}.layer.{y}. which corresponds to layer.{x} in # the original model A_ = list(s_dict.keys() ) for key in keys: A_ = r""".*/layers_(\d+)""" A_ = key if re.match(UpperCAmelCase__, UpperCAmelCase__ ): A_ = re.sub(r"""layers_(\d+)""", r"""block/\1/layer""", UpperCAmelCase__ ) A_ = r"""(encoder|decoder)\/""" if re.match(UpperCAmelCase__, UpperCAmelCase__ ): A_ = re.match(UpperCAmelCase__, UpperCAmelCase__ ).groups() if groups[0] == "encoder": A_ = re.sub(r"""/mlp/""", r"""/1/mlp/""", UpperCAmelCase__ ) A_ = re.sub(r"""/pre_mlp_layer_norm/""", r"""/1/layer_norm/""", UpperCAmelCase__ ) elif groups[0] == "decoder": A_ = re.sub(r"""/mlp/""", r"""/2/mlp/""", UpperCAmelCase__ ) A_ = re.sub(r"""/pre_mlp_layer_norm/""", r"""/2/layer_norm/""", UpperCAmelCase__ ) # 2. Convert other classic mappings for old_key, temp_key in MOE_LAYER_NAME_MAPPING.items(): if old_key in new_key: A_ = new_key.replace(UpperCAmelCase__, UpperCAmelCase__ ) print(F'''{key} -> {new_key}''' ) A_ = s_dict.pop(UpperCAmelCase__ ) if "encoder/block/0/layer/0/SelfAttention/relative_attention_bias/weight" in s_dict: A_ = s_dict[ """encoder/block/0/layer/0/SelfAttention/relative_attention_bias/weight""" ].T if "decoder/block/0/layer/0/SelfAttention/relative_attention_bias/weight" in s_dict: A_ = s_dict[ """decoder/block/0/layer/0/SelfAttention/relative_attention_bias/weight""" ].T # 3. Take extra care of the EXPERTS layer for key in list(s_dict.keys() ): if "expert" in key: A_ = s_dict[key].shape[0] A_ = s_dict[key] for idx in range(UpperCAmelCase__ ): A_ = expert_weihts[idx] print(F'''{key} -> {key.replace("expert/", "nested fstring" )}''' ) s_dict.pop(UpperCAmelCase__ ) return s_dict __lowerCamelCase = { '''NUM_ENCODER_LAYERS''': '''num_layers''', '''NUM_DECODER_LAYERS''': '''num_decoder_layers''', '''NUM_HEADS''': '''num_heads''', '''HEAD_DIM''': '''d_kv''', '''EMBED_DIM''': '''d_model''', '''MLP_DIM''': '''d_ff''', '''NUM_SELECTED_EXPERTS''': '''num_selected_experts''', '''NUM_ENCODER_SPARSE_LAYERS''': '''num_sparse_encoder_layers''', '''NUM_DECODER_SPARSE_LAYERS''': '''num_sparse_decoder_layers''', '''dense.MlpBlock.activations''': '''feed_forward_proj''', } def UpperCAmelCase__ ( UpperCAmelCase__, UpperCAmelCase__ ) -> List[Any]: # Convert a google style config to the hugging face fromat import regex as re with open(UpperCAmelCase__, """r""" ) as f: A_ = f.read() A_ = re.findall(r"""(.*) = ([0-9.]*)""", UpperCAmelCase__ ) A_ = {} for param, value in regex_match: if param in GIN_TO_CONFIG_MAPPING and value != "": A_ = float(UpperCAmelCase__ ) if """.""" in value else int(UpperCAmelCase__ ) A_ = re.findall(r"""(.*activations) = \(\'(.*)\',\)""", UpperCAmelCase__ )[0] A_ = str(activation[1] ) A_ = num_experts A_ = SwitchTransformersConfig(**UpperCAmelCase__ ) return config def UpperCAmelCase__ ( UpperCAmelCase__, UpperCAmelCase__, UpperCAmelCase__=None, UpperCAmelCase__="./", UpperCAmelCase__=8 ) -> List[str]: # Initialise PyTorch model print(F'''Loading flax weights from : {flax_checkpoint_path}''' ) A_ = checkpoints.load_tax_checkpoint(UpperCAmelCase__ ) if gin_file is not None: A_ = convert_gin_to_config(UpperCAmelCase__, UpperCAmelCase__ ) else: A_ = SwitchTransformersConfig.from_pretrained(UpperCAmelCase__ ) A_ = SwitchTransformersForConditionalGeneration(UpperCAmelCase__ ) A_ = flax_params["""target"""] A_ = flatten_dict(UpperCAmelCase__, sep="""/""" ) A_ = rename_keys(UpperCAmelCase__ ) A_ = unflatten_dict(UpperCAmelCase__, sep="""/""" ) # Load the flax params in the PT model load_flax_weights_in_pytorch_model(UpperCAmelCase__, UpperCAmelCase__ ) print(F'''Save PyTorch model to {pytorch_dump_path}''' ) pt_model.save_pretrained(UpperCAmelCase__ ) if __name__ == "__main__": __lowerCamelCase = argparse.ArgumentParser() # Required parameters parser.add_argument( '''--switch_t5x_checkpoint_path''', default=None, type=str, required=True, help=( '''The config json file corresponding to the pre-trained SwitchTransformers model. \nThis specifies the''' ''' model architecture. If not provided, a `gin_file` has to be provided.''' ), ) parser.add_argument( '''--gin_file''', default=None, type=str, required=False, help='''Path to the gin config file. If not provided, a `config_file` has to be passed ''', ) parser.add_argument( '''--config_name''', default=None, type=str, required=False, help='''Config name of SwitchTransformers model.''' ) parser.add_argument( '''--pytorch_dump_folder_path''', default=None, type=str, required=True, help='''Path to the output pytorch model.''' ) parser.add_argument('''--num_experts''', default=8, type=int, required=False, help='''Number of experts''') __lowerCamelCase = parser.parse_args() convert_flax_checkpoint_to_pytorch( args.switch_tax_checkpoint_path, args.config_name, args.gin_file, args.pytorch_dump_folder_path, args.num_experts, )
667
1
'''simple docstring''' import json import os import shutil import tempfile import unittest import numpy as np from transformers import BertTokenizerFast from transformers.models.bert.tokenization_bert import VOCAB_FILES_NAMES, BertTokenizer from transformers.testing_utils import require_tokenizers, require_vision from transformers.utils import IMAGE_PROCESSOR_NAME, is_vision_available if is_vision_available(): from PIL import Image from transformers import VisionTextDualEncoderProcessor, ViTImageProcessor @require_tokenizers @require_vision class A__ ( unittest.TestCase ): def snake_case_ ( self ) -> Optional[Any]: '''simple docstring''' A_ = tempfile.mkdtemp() # fmt: off A_ = ["""[UNK]""", """[CLS]""", """[SEP]""", """[PAD]""", """[MASK]""", """want""", """##want""", """##ed""", """wa""", """un""", """runn""", """##ing""", """,""", """low""", """lowest"""] # fmt: on A_ = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES["""vocab_file"""] ) with open(self.vocab_file , """w""" , encoding="""utf-8""" ) as vocab_writer: vocab_writer.write("""""".join([x + """\n""" for x in vocab_tokens] ) ) A_ = { """do_resize""": True, """size""": {"""height""": 18, """width""": 18}, """do_normalize""": True, """image_mean""": [0.5, 0.5, 0.5], """image_std""": [0.5, 0.5, 0.5], } A_ = os.path.join(self.tmpdirname , UpperCamelCase__ ) with open(self.image_processor_file , """w""" , encoding="""utf-8""" ) as fp: json.dump(UpperCamelCase__ , UpperCamelCase__ ) def snake_case_ ( self , **UpperCamelCase__ ) -> Tuple: '''simple docstring''' return BertTokenizer.from_pretrained(self.tmpdirname , **UpperCamelCase__ ) def snake_case_ ( self , **UpperCamelCase__ ) -> Optional[Any]: '''simple docstring''' return ViTImageProcessor.from_pretrained(self.tmpdirname , **UpperCamelCase__ ) def snake_case_ ( self ) -> Any: '''simple docstring''' shutil.rmtree(self.tmpdirname ) def snake_case_ ( self ) -> Optional[Any]: '''simple docstring''' A_ = [np.random.randint(255 , size=(3, 30, 400) , dtype=np.uinta )] A_ = [Image.fromarray(np.moveaxis(UpperCamelCase__ , 0 , -1 ) ) for x in image_inputs] return image_inputs def snake_case_ ( self ) -> Dict: '''simple docstring''' A_ = self.get_tokenizer() A_ = self.get_image_processor() A_ = VisionTextDualEncoderProcessor(tokenizer=UpperCamelCase__ , image_processor=UpperCamelCase__ ) processor.save_pretrained(self.tmpdirname ) A_ = VisionTextDualEncoderProcessor.from_pretrained(self.tmpdirname ) self.assertEqual(processor.tokenizer.get_vocab() , tokenizer.get_vocab() ) self.assertIsInstance(processor.tokenizer , (BertTokenizer, BertTokenizerFast) ) self.assertEqual(processor.image_processor.to_json_string() , image_processor.to_json_string() ) self.assertIsInstance(processor.image_processor , UpperCamelCase__ ) def snake_case_ ( self ) -> Tuple: '''simple docstring''' A_ = VisionTextDualEncoderProcessor( tokenizer=self.get_tokenizer() , image_processor=self.get_image_processor() ) processor.save_pretrained(self.tmpdirname ) A_ = self.get_tokenizer(bos_token="""(BOS)""" , eos_token="""(EOS)""" ) A_ = self.get_image_processor(do_normalize=UpperCamelCase__ , padding_value=1.0 ) A_ = VisionTextDualEncoderProcessor.from_pretrained( self.tmpdirname , bos_token="""(BOS)""" , eos_token="""(EOS)""" , do_normalize=UpperCamelCase__ , padding_value=1.0 ) self.assertEqual(processor.tokenizer.get_vocab() , tokenizer_add_kwargs.get_vocab() ) self.assertIsInstance(processor.tokenizer , (BertTokenizer, BertTokenizerFast) ) self.assertEqual(processor.image_processor.to_json_string() , image_processor_add_kwargs.to_json_string() ) self.assertIsInstance(processor.image_processor , UpperCamelCase__ ) def snake_case_ ( self ) -> int: '''simple docstring''' A_ = self.get_image_processor() A_ = self.get_tokenizer() A_ = VisionTextDualEncoderProcessor(tokenizer=UpperCamelCase__ , image_processor=UpperCamelCase__ ) A_ = self.prepare_image_inputs() A_ = image_processor(UpperCamelCase__ , return_tensors="""np""" ) A_ = processor(images=UpperCamelCase__ , return_tensors="""np""" ) for key in input_feat_extract.keys(): self.assertAlmostEqual(input_feat_extract[key].sum() , input_processor[key].sum() , delta=1e-2 ) def snake_case_ ( self ) -> Union[str, Any]: '''simple docstring''' A_ = self.get_image_processor() A_ = self.get_tokenizer() A_ = VisionTextDualEncoderProcessor(tokenizer=UpperCamelCase__ , image_processor=UpperCamelCase__ ) A_ = """lower newer""" A_ = processor(text=UpperCamelCase__ ) A_ = tokenizer(UpperCamelCase__ ) for key in encoded_tok.keys(): self.assertListEqual(encoded_tok[key] , encoded_processor[key] ) def snake_case_ ( self ) -> int: '''simple docstring''' A_ = self.get_image_processor() A_ = self.get_tokenizer() A_ = VisionTextDualEncoderProcessor(tokenizer=UpperCamelCase__ , image_processor=UpperCamelCase__ ) A_ = """lower newer""" A_ = self.prepare_image_inputs() A_ = processor(text=UpperCamelCase__ , images=UpperCamelCase__ ) self.assertListEqual(list(inputs.keys() ) , ["""input_ids""", """token_type_ids""", """attention_mask""", """pixel_values"""] ) # test if it raises when no input is passed with self.assertRaises(UpperCamelCase__ ): processor() def snake_case_ ( self ) -> List[Any]: '''simple docstring''' A_ = self.get_image_processor() A_ = self.get_tokenizer() A_ = VisionTextDualEncoderProcessor(tokenizer=UpperCamelCase__ , image_processor=UpperCamelCase__ ) A_ = [[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]] A_ = processor.batch_decode(UpperCamelCase__ ) A_ = tokenizer.batch_decode(UpperCamelCase__ ) self.assertListEqual(UpperCamelCase__ , UpperCamelCase__ ) def snake_case_ ( self ) -> int: '''simple docstring''' A_ = self.get_image_processor() A_ = self.get_tokenizer() A_ = VisionTextDualEncoderProcessor(tokenizer=UpperCamelCase__ , image_processor=UpperCamelCase__ ) A_ = """lower newer""" A_ = self.prepare_image_inputs() A_ = processor(text=UpperCamelCase__ , images=UpperCamelCase__ ) self.assertListEqual(list(inputs.keys() ) , processor.model_input_names )
667
'''simple docstring''' def UpperCAmelCase__ ( UpperCAmelCase__ ) -> int: assert ( isinstance(UpperCAmelCase__, UpperCAmelCase__ ) and number_of_steps > 0 ), F'''number_of_steps needs to be positive integer, your input {number_of_steps}''' if number_of_steps == 1: return 1 A_ , A_ = 1, 1 for _ in range(number_of_steps - 1 ): A_ , A_ = current + previous, current return current if __name__ == "__main__": import doctest doctest.testmod()
667
1
'''simple docstring''' from collections import UserDict from typing import List, Union from ..utils import ( add_end_docstrings, is_tf_available, is_torch_available, is_vision_available, logging, requires_backends, ) from .base import PIPELINE_INIT_ARGS, Pipeline if is_vision_available(): from PIL import Image from ..image_utils import load_image if is_torch_available(): from ..models.auto.modeling_auto import MODEL_FOR_ZERO_SHOT_IMAGE_CLASSIFICATION_MAPPING if is_tf_available(): from ..models.auto.modeling_tf_auto import TF_MODEL_FOR_ZERO_SHOT_IMAGE_CLASSIFICATION_MAPPING from ..tf_utils import stable_softmax __lowerCamelCase = logging.get_logger(__name__) @add_end_docstrings(_snake_case ) class A__ ( _snake_case ): def __init__( self , **UpperCamelCase__ ) -> List[str]: '''simple docstring''' super().__init__(**UpperCamelCase__ ) requires_backends(self , """vision""" ) self.check_model_type( TF_MODEL_FOR_ZERO_SHOT_IMAGE_CLASSIFICATION_MAPPING if self.framework == """tf""" else MODEL_FOR_ZERO_SHOT_IMAGE_CLASSIFICATION_MAPPING ) def __call__( self , UpperCamelCase__ , **UpperCamelCase__ ) -> Optional[int]: '''simple docstring''' return super().__call__(UpperCamelCase__ , **UpperCamelCase__ ) def snake_case_ ( self , **UpperCamelCase__ ) -> str: '''simple docstring''' A_ = {} if "candidate_labels" in kwargs: A_ = kwargs["""candidate_labels"""] if "hypothesis_template" in kwargs: A_ = kwargs["""hypothesis_template"""] return preprocess_params, {}, {} def snake_case_ ( self , UpperCamelCase__ , UpperCamelCase__=None , UpperCamelCase__="This is a photo of {}." ) -> Union[str, Any]: '''simple docstring''' A_ = load_image(UpperCamelCase__ ) A_ = self.image_processor(images=[image] , return_tensors=self.framework ) A_ = candidate_labels A_ = [hypothesis_template.format(UpperCamelCase__ ) for x in candidate_labels] A_ = self.tokenizer(UpperCamelCase__ , return_tensors=self.framework , padding=UpperCamelCase__ ) A_ = [text_inputs] return inputs def snake_case_ ( self , UpperCamelCase__ ) -> Optional[int]: '''simple docstring''' A_ = model_inputs.pop("""candidate_labels""" ) A_ = model_inputs.pop("""text_inputs""" ) if isinstance(text_inputs[0] , UpperCamelCase__ ): A_ = text_inputs[0] else: # Batching case. A_ = text_inputs[0][0] A_ = self.model(**UpperCamelCase__ , **UpperCamelCase__ ) A_ = { """candidate_labels""": candidate_labels, """logits""": outputs.logits_per_image, } return model_outputs def snake_case_ ( self , UpperCamelCase__ ) -> List[Any]: '''simple docstring''' A_ = model_outputs.pop("""candidate_labels""" ) A_ = model_outputs["""logits"""][0] if self.framework == "pt": A_ = logits.softmax(dim=-1 ).squeeze(-1 ) A_ = probs.tolist() if not isinstance(UpperCamelCase__ , UpperCamelCase__ ): A_ = [scores] elif self.framework == "tf": A_ = stable_softmax(UpperCamelCase__ , axis=-1 ) A_ = probs.numpy().tolist() else: raise ValueError(f'''Unsupported framework: {self.framework}''' ) A_ = [ {"""score""": score, """label""": candidate_label} for score, candidate_label in sorted(zip(UpperCamelCase__ , UpperCamelCase__ ) , key=lambda UpperCamelCase__ : -x[0] ) ] return result
667
'''simple docstring''' def UpperCAmelCase__ ( UpperCAmelCase__ ) -> bool: return str(UpperCAmelCase__ ) == str(UpperCAmelCase__ )[::-1] def UpperCAmelCase__ ( UpperCAmelCase__ ) -> int: return int(UpperCAmelCase__ ) + int(str(UpperCAmelCase__ )[::-1] ) def UpperCAmelCase__ ( UpperCAmelCase__ = 1_00_00 ) -> int: A_ = [] for num in range(1, UpperCAmelCase__ ): A_ = 0 A_ = num while iterations < 50: A_ = sum_reverse(UpperCAmelCase__ ) iterations += 1 if is_palindrome(UpperCAmelCase__ ): break else: lychrel_nums.append(UpperCAmelCase__ ) return len(UpperCAmelCase__ ) if __name__ == "__main__": print(f"""{solution() = }""")
667
1
'''simple docstring''' from collections import defaultdict def UpperCAmelCase__ ( UpperCAmelCase__, UpperCAmelCase__ ) -> bool: A_ = first_str.lower().strip() A_ = second_str.lower().strip() # Remove whitespace A_ = first_str.replace(""" """, """""" ) A_ = second_str.replace(""" """, """""" ) # Strings of different lengths are not anagrams if len(UpperCAmelCase__ ) != len(UpperCAmelCase__ ): return False # Default values for count should be 0 A_ = defaultdict(UpperCAmelCase__ ) # For each character in input strings, # increment count in the corresponding for i in range(len(UpperCAmelCase__ ) ): count[first_str[i]] += 1 count[second_str[i]] -= 1 return all(_count == 0 for _count in count.values() ) if __name__ == "__main__": from doctest import testmod testmod() __lowerCamelCase = input('''Enter the first string ''').strip() __lowerCamelCase = input('''Enter the second string ''').strip() __lowerCamelCase = check_anagrams(input_a, input_b) print(f"""{input_a} and {input_b} are {'' if status else 'not '}anagrams.""")
667
'''simple docstring''' import argparse import json from typing import List from ltp import LTP from transformers.models.bert.tokenization_bert import BertTokenizer def UpperCAmelCase__ ( UpperCAmelCase__ ) -> Optional[int]: # This defines a "chinese character" as anything in the CJK Unicode block: # https://en.wikipedia.org/wiki/CJK_Unified_Ideographs_(Unicode_block) # # Note that the CJK Unicode block is NOT all Japanese and Korean characters, # despite its name. The modern Korean Hangul alphabet is a different block, # as is Japanese Hiragana and Katakana. Those alphabets are used to write # space-separated words, so they are not treated specially and handled # like the all of the other languages. if ( (cp >= 0X4E_00 and cp <= 0X9F_FF) or (cp >= 0X34_00 and cp <= 0X4D_BF) # or (cp >= 0X2_00_00 and cp <= 0X2_A6_DF) # or (cp >= 0X2_A7_00 and cp <= 0X2_B7_3F) # or (cp >= 0X2_B7_40 and cp <= 0X2_B8_1F) # or (cp >= 0X2_B8_20 and cp <= 0X2_CE_AF) # or (cp >= 0XF9_00 and cp <= 0XFA_FF) or (cp >= 0X2_F8_00 and cp <= 0X2_FA_1F) # ): # return True return False def UpperCAmelCase__ ( UpperCAmelCase__ ) -> List[Any]: # word like '180' or '่บซ้ซ˜' or '็ฅž' for char in word: A_ = ord(UpperCAmelCase__ ) if not _is_chinese_char(UpperCAmelCase__ ): return 0 return 1 def UpperCAmelCase__ ( UpperCAmelCase__ ) -> int: A_ = set() for token in tokens: A_ = len(UpperCAmelCase__ ) > 1 and is_chinese(UpperCAmelCase__ ) if chinese_word: word_set.add(UpperCAmelCase__ ) A_ = list(UpperCAmelCase__ ) return word_list def UpperCAmelCase__ ( UpperCAmelCase__, UpperCAmelCase__ ) -> Dict: if not chinese_word_set: return bert_tokens A_ = max([len(UpperCAmelCase__ ) for w in chinese_word_set] ) A_ = bert_tokens A_ , A_ = 0, len(UpperCAmelCase__ ) while start < end: A_ = True if is_chinese(bert_word[start] ): A_ = min(end - start, UpperCAmelCase__ ) for i in range(UpperCAmelCase__, 1, -1 ): A_ = """""".join(bert_word[start : start + i] ) if whole_word in chinese_word_set: for j in range(start + 1, start + i ): A_ = """##""" + bert_word[j] A_ = start + i A_ = False break if single_word: start += 1 return bert_word def UpperCAmelCase__ ( UpperCAmelCase__, UpperCAmelCase__, UpperCAmelCase__ ) -> Dict: A_ = [] for i in range(0, len(UpperCAmelCase__ ), 1_00 ): A_ = ltp_tokenizer.pipeline(lines[i : i + 1_00], tasks=["""cws"""] ).cws A_ = [get_chinese_word(UpperCAmelCase__ ) for r in res] ltp_res.extend(UpperCAmelCase__ ) assert len(UpperCAmelCase__ ) == len(UpperCAmelCase__ ) A_ = [] for i in range(0, len(UpperCAmelCase__ ), 1_00 ): A_ = bert_tokenizer(lines[i : i + 1_00], add_special_tokens=UpperCAmelCase__, truncation=UpperCAmelCase__, max_length=5_12 ) bert_res.extend(res["""input_ids"""] ) assert len(UpperCAmelCase__ ) == len(UpperCAmelCase__ ) A_ = [] for input_ids, chinese_word in zip(UpperCAmelCase__, UpperCAmelCase__ ): A_ = [] for id in input_ids: A_ = bert_tokenizer._convert_id_to_token(UpperCAmelCase__ ) input_tokens.append(UpperCAmelCase__ ) A_ = add_sub_symbol(UpperCAmelCase__, UpperCAmelCase__ ) A_ = [] # We only save pos of chinese subwords start with ##, which mean is part of a whole word. for i, token in enumerate(UpperCAmelCase__ ): if token[:2] == "##": A_ = token[2:] # save chinese tokens' pos if len(UpperCAmelCase__ ) == 1 and _is_chinese_char(ord(UpperCAmelCase__ ) ): ref_id.append(UpperCAmelCase__ ) ref_ids.append(UpperCAmelCase__ ) assert len(UpperCAmelCase__ ) == len(UpperCAmelCase__ ) return ref_ids def UpperCAmelCase__ ( UpperCAmelCase__ ) -> Optional[Any]: # For Chinese (Ro)Bert, the best result is from : RoBERTa-wwm-ext (https://github.com/ymcui/Chinese-BERT-wwm) # If we want to fine-tune these model, we have to use same tokenizer : LTP (https://github.com/HIT-SCIR/ltp) with open(args.file_name, """r""", encoding="""utf-8""" ) as f: A_ = f.readlines() A_ = [line.strip() for line in data if len(UpperCAmelCase__ ) > 0 and not line.isspace()] # avoid delimiter like '\u2029' A_ = LTP(args.ltp ) # faster in GPU device A_ = BertTokenizer.from_pretrained(args.bert ) A_ = prepare_ref(UpperCAmelCase__, UpperCAmelCase__, UpperCAmelCase__ ) with open(args.save_path, """w""", encoding="""utf-8""" ) as f: A_ = [json.dumps(UpperCAmelCase__ ) + """\n""" for ref in ref_ids] f.writelines(UpperCAmelCase__ ) if __name__ == "__main__": __lowerCamelCase = argparse.ArgumentParser(description='''prepare_chinese_ref''') parser.add_argument( '''--file_name''', required=False, type=str, default='''./resources/chinese-demo.txt''', help='''file need process, same as training data in lm''', ) parser.add_argument( '''--ltp''', required=False, type=str, default='''./resources/ltp''', help='''resources for LTP tokenizer, usually a path''', ) parser.add_argument( '''--bert''', required=False, type=str, default='''./resources/robert''', help='''resources for Bert tokenizer''', ) parser.add_argument( '''--save_path''', required=False, type=str, default='''./resources/ref.txt''', help='''path to save res''', ) __lowerCamelCase = parser.parse_args() main(args)
667
1
'''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 __lowerCamelCase = pytest.mark.integration __lowerCamelCase = {'''comet'''} __lowerCamelCase = importlib.util.find_spec('''fairseq''') is not None __lowerCamelCase = {'''code_eval'''} __lowerCamelCase = os.name == '''nt''' __lowerCamelCase = {'''bertscore''', '''frugalscore''', '''perplexity'''} __lowerCamelCase = importlib.util.find_spec('''transformers''') is not None def UpperCAmelCase__ ( UpperCAmelCase__ ) -> Any: @wraps(UpperCAmelCase__ ) def wrapper(self, UpperCAmelCase__ ): if not _has_fairseq and metric_name in REQUIRE_FAIRSEQ: self.skipTest("""\"test requires Fairseq\"""" ) else: test_case(self, UpperCAmelCase__ ) return wrapper def UpperCAmelCase__ ( UpperCAmelCase__ ) -> str: @wraps(UpperCAmelCase__ ) def wrapper(self, UpperCAmelCase__ ): if not _has_transformers and metric_name in REQUIRE_TRANSFORMERS: self.skipTest("""\"test requires transformers\"""" ) else: test_case(self, UpperCAmelCase__ ) return wrapper def UpperCAmelCase__ ( UpperCAmelCase__ ) -> Union[str, Any]: @wraps(UpperCAmelCase__ ) def wrapper(self, UpperCAmelCase__ ): 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 UpperCAmelCase__ ( ) -> List[str]: A_ = [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( _snake_case , _snake_case , _snake_case ) @local class A__ ( parameterized.TestCase ): lowercase = {} lowercase = None @pytest.mark.filterwarnings("""ignore:metric_module_factory is deprecated:FutureWarning""" ) @pytest.mark.filterwarnings("""ignore:load_metric is deprecated:FutureWarning""" ) def snake_case_ ( self , UpperCamelCase__ ) -> List[Any]: '''simple docstring''' A_ = """[...]""" A_ = importlib.import_module( datasets.load.metric_module_factory(os.path.join("""metrics""" , UpperCamelCase__ ) ).module_path ) A_ = datasets.load.import_main_class(metric_module.__name__ , dataset=UpperCamelCase__ ) # check parameters A_ = 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: A_ = 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 snake_case_ ( self , UpperCamelCase__ ) -> Union[str, Any]: '''simple docstring''' A_ = """[...]""" A_ = importlib.import_module( datasets.load.metric_module_factory(os.path.join("""metrics""" , UpperCamelCase__ ) ).module_path ) # run doctest with self.use_local_metrics(): A_ = doctest.testmod(UpperCamelCase__ , verbose=UpperCamelCase__ , raise_on_error=UpperCamelCase__ ) self.assertEqual(results.failed , 0 ) self.assertGreater(results.attempted , 1 ) @contextmanager def snake_case_ ( self , UpperCamelCase__ , UpperCamelCase__ ) -> Optional[int]: '''simple docstring''' if metric_name in self.INTENSIVE_CALLS_PATCHER: with self.INTENSIVE_CALLS_PATCHER[metric_name](UpperCamelCase__ ): yield else: yield @contextmanager def snake_case_ ( self ) -> Tuple: '''simple docstring''' def load_local_metric(UpperCamelCase__ , *UpperCamelCase__ , **UpperCamelCase__ ): return load_metric(os.path.join("""metrics""" , UpperCamelCase__ ) , *UpperCamelCase__ , **UpperCamelCase__ ) with patch("""datasets.load_metric""" ) as mock_load_metric: A_ = load_local_metric yield @classmethod def snake_case_ ( cls , UpperCamelCase__ ) -> Tuple: '''simple docstring''' def wrapper(UpperCamelCase__ ): A_ = contextmanager(UpperCamelCase__ ) A_ = patcher return patcher return wrapper @LocalMetricTest.register_intensive_calls_patcher("""bleurt""" ) def UpperCAmelCase__ ( UpperCAmelCase__ ) -> Union[str, Any]: import tensorflow.compat.va as tf from bleurt.score import Predictor tf.flags.DEFINE_string("""sv""", """""", """""" ) # handle pytest cli flags class A__ ( _snake_case ): def snake_case_ ( self , UpperCamelCase__ ) -> Union[str, Any]: '''simple docstring''' 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: A_ = MockedPredictor() yield @LocalMetricTest.register_intensive_calls_patcher("""bertscore""" ) def UpperCAmelCase__ ( UpperCAmelCase__ ) -> Dict: import torch def bert_cos_score_idf(UpperCAmelCase__, UpperCAmelCase__, *UpperCAmelCase__, **UpperCAmelCase__ ): 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: A_ = bert_cos_score_idf yield @LocalMetricTest.register_intensive_calls_patcher("""comet""" ) def UpperCAmelCase__ ( UpperCAmelCase__ ) -> Dict: def load_from_checkpoint(UpperCAmelCase__ ): class A__ : def snake_case_ ( self , UpperCamelCase__ , *UpperCamelCase__ , **UpperCamelCase__ ) -> Union[str, Any]: '''simple docstring''' assert len(UpperCamelCase__ ) == 2 A_ = [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: A_ = None with patch("""comet.load_from_checkpoint""" ) as mock_load_from_checkpoint: A_ = load_from_checkpoint yield def UpperCAmelCase__ ( ) -> Dict: A_ = load_metric(os.path.join("""metrics""", """seqeval""" ) ) A_ = """ERROR""" A_ = 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__ )
667
'''simple docstring''' from __future__ import annotations import math def UpperCAmelCase__ ( UpperCAmelCase__ ) -> bool: if 1 < number < 4: # 2 and 3 are primes return True elif number < 2 or number % 2 == 0 or number % 3 == 0: # Negatives, 0, 1, all even numbers, all multiples of 3 are not primes return False # All primes number are in format of 6k +/- 1 for i in range(5, int(math.sqrt(UpperCAmelCase__ ) + 1 ), 6 ): if number % i == 0 or number % (i + 2) == 0: return False return True __lowerCamelCase = [num for num in range(3, 10_0001, 2) if not is_prime(num)] def UpperCAmelCase__ ( UpperCAmelCase__ ) -> list[int]: if not isinstance(UpperCAmelCase__, UpperCAmelCase__ ): raise ValueError("""n must be an integer""" ) if n <= 0: raise ValueError("""n must be >= 0""" ) A_ = [] for num in range(len(UpperCAmelCase__ ) ): A_ = 0 while 2 * i * i <= odd_composites[num]: A_ = odd_composites[num] - 2 * i * i if is_prime(UpperCAmelCase__ ): break i += 1 else: list_nums.append(odd_composites[num] ) if len(UpperCAmelCase__ ) == n: return list_nums return [] def UpperCAmelCase__ ( ) -> int: return compute_nums(1 )[0] if __name__ == "__main__": print(f"""{solution() = }""")
667
1
'''simple docstring''' import io import os import unicodedata from typing import Any, Dict, List, Optional, Tuple import sentencepiece as spm from ...tokenization_utils import PreTrainedTokenizer from ...utils import logging __lowerCamelCase = logging.get_logger(__name__) __lowerCamelCase = '''โ–''' __lowerCamelCase = {'''vocab_file''': '''vocab.txt''', '''sentencepiece_model_ckpt''': '''sentencepiece.bpe.model'''} __lowerCamelCase = { '''sentencepiece_model_file''': '''sentencepiece.bpe.model''', '''vocab_file''': '''vocab.txt''', } __lowerCamelCase = { '''vocab_file''': { '''ernie-m-base''': '''https://huggingface.co/susnato/ernie-m-base_pytorch/blob/main/vocab.txt''', '''ernie-m-large''': '''https://huggingface.co/susnato/ernie-m-base_pytorch/blob/main/vocab.txt''', }, '''sentencepiece_model_file''': { '''ernie-m-base''': '''https://huggingface.co/susnato/ernie-m-base_pytorch/blob/main/sentencepiece.bpe.model''', '''ernie-m-large''': '''https://huggingface.co/susnato/ernie-m-base_pytorch/blob/main/sentencepiece.bpe.model''', }, } __lowerCamelCase = { '''ernie-m-base''': 514, '''ernie-m-large''': 514, } __lowerCamelCase = { '''ernie-m-base''': {'''do_lower_case''': False}, '''ernie-m-large''': {'''do_lower_case''': False}, } class A__ ( _snake_case ): lowercase = ["input_ids"] lowercase = VOCAB_FILES_NAMES lowercase = PRETRAINED_INIT_CONFIGURATION lowercase = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES lowercase = PRETRAINED_VOCAB_FILES_MAP lowercase = RESOURCE_FILES_NAMES def __init__( self , UpperCamelCase__ , UpperCamelCase__=None , UpperCamelCase__=False , UpperCamelCase__="utf8" , UpperCamelCase__="[UNK]" , UpperCamelCase__="[SEP]" , UpperCamelCase__="[PAD]" , UpperCamelCase__="[CLS]" , UpperCamelCase__="[MASK]" , UpperCamelCase__ = None , **UpperCamelCase__ , ) -> None: '''simple docstring''' # Mask token behave like a normal word, i.e. include the space before it and # is included in the raw text, there should be a match in a non-normalized sentence. A_ = {} if sp_model_kwargs is None else sp_model_kwargs super().__init__( do_lower_case=UpperCamelCase__ , unk_token=UpperCamelCase__ , sep_token=UpperCamelCase__ , pad_token=UpperCamelCase__ , cls_token=UpperCamelCase__ , mask_token=UpperCamelCase__ , vocab_file=UpperCamelCase__ , encoding=UpperCamelCase__ , sp_model_kwargs=self.sp_model_kwargs , **UpperCamelCase__ , ) A_ = do_lower_case A_ = sentencepiece_model_ckpt A_ = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(UpperCamelCase__ ) # to mimic paddlenlp.transformers.ernie_m.tokenizer.ErnieMTokenizer functioning if vocab_file is not None: A_ = self.load_vocab(filepath=UpperCamelCase__ ) else: A_ = {self.sp_model.id_to_piece(UpperCamelCase__ ): id for id in range(self.sp_model.get_piece_size() )} A_ = {v: k for k, v in self.vocab.items()} def snake_case_ ( self , UpperCamelCase__ ) -> Union[str, Any]: '''simple docstring''' if text is None: return None A_ = self.tokenize(UpperCamelCase__ ) A_ , A_ = """""", [] for i, ch in enumerate(UpperCamelCase__ ): if ch in self.SP_CHAR_MAPPING: A_ = self.SP_CHAR_MAPPING.get(UpperCamelCase__ ) else: A_ = unicodedata.normalize("""NFKC""" , UpperCamelCase__ ) if self.is_whitespace(UpperCamelCase__ ): continue normalized_text += ch char_mapping.extend([i] * len(UpperCamelCase__ ) ) A_ , A_ , A_ = normalized_text, [], 0 if self.do_lower_case: A_ = text.lower() for token in split_tokens: if token[:1] == "โ–": A_ = token[1:] A_ = text[offset:].index(UpperCamelCase__ ) + offset A_ = start + len(UpperCamelCase__ ) token_mapping.append((char_mapping[start], char_mapping[end - 1] + 1) ) A_ = end return token_mapping @property def snake_case_ ( self ) -> Optional[int]: '''simple docstring''' return len(self.vocab ) def snake_case_ ( self ) -> int: '''simple docstring''' return dict(self.vocab , **self.added_tokens_encoder ) def __getstate__( self ) -> List[str]: '''simple docstring''' A_ = self.__dict__.copy() A_ = None return state def __setstate__( self , UpperCamelCase__ ) -> 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.sentencepiece_model_ckpt ) def snake_case_ ( self , UpperCamelCase__ ) -> Union[str, Any]: '''simple docstring''' return "".join((self.SP_CHAR_MAPPING.get(UpperCamelCase__ , UpperCamelCase__ ) for c in text) ) def snake_case_ ( self , UpperCamelCase__ , UpperCamelCase__=False , UpperCamelCase__=64 , UpperCamelCase__=0.1 ) -> List[Any]: '''simple docstring''' if self.sp_model_kwargs.get("""enable_sampling""" ) is True: A_ = True if self.sp_model_kwargs.get("""alpha""" ) is not None: A_ = self.sp_model_kwargs.get("""alpha""" ) if self.sp_model_kwargs.get("""nbest_size""" ) is not None: A_ = self.sp_model_kwargs.get("""nbest_size""" ) if not enable_sampling: A_ = self.sp_model.EncodeAsPieces(UpperCamelCase__ ) else: A_ = self.sp_model.SampleEncodeAsPieces(UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ ) A_ = [] for pi, piece in enumerate(UpperCamelCase__ ): if piece == SPIECE_UNDERLINE: if not pieces[pi + 1].startswith(UpperCamelCase__ ) and pi != 0: new_pieces.append(UpperCamelCase__ ) continue else: continue A_ = 0 for i, chunk in enumerate(UpperCamelCase__ ): if chunk == SPIECE_UNDERLINE: continue if self.is_ch_char(UpperCamelCase__ ) or self.is_punct(UpperCamelCase__ ): if i > lst_i and piece[lst_i:i] != SPIECE_UNDERLINE: new_pieces.append(piece[lst_i:i] ) new_pieces.append(UpperCamelCase__ ) A_ = i + 1 elif chunk.isdigit() and i > 0 and not piece[i - 1].isdigit(): if i > lst_i and piece[lst_i:i] != SPIECE_UNDERLINE: new_pieces.append(piece[lst_i:i] ) A_ = i elif not chunk.isdigit() and i > 0 and piece[i - 1].isdigit(): if i > lst_i and piece[lst_i:i] != SPIECE_UNDERLINE: new_pieces.append(piece[lst_i:i] ) A_ = i if len(UpperCamelCase__ ) > lst_i: new_pieces.append(piece[lst_i:] ) return new_pieces def snake_case_ ( self , UpperCamelCase__ ) -> Optional[Any]: '''simple docstring''' A_ = """""".join(UpperCamelCase__ ).replace(UpperCamelCase__ , """ """ ).strip() return out_string def snake_case_ ( self , UpperCamelCase__ ) -> List[str]: '''simple docstring''' A_ = self.convert_ids_to_tokens(UpperCamelCase__ ) A_ = """""".join(UpperCamelCase__ ).replace(UpperCamelCase__ , """ """ ).strip() return out_string def snake_case_ ( self , UpperCamelCase__ ) -> Tuple: '''simple docstring''' return self.vocab.get(UpperCamelCase__ , self.vocab.get(self.unk_token ) ) def snake_case_ ( self , UpperCamelCase__ ) -> Optional[Any]: '''simple docstring''' return self.reverse_vocab.get(UpperCamelCase__ , self.unk_token ) def snake_case_ ( self , UpperCamelCase__ , UpperCamelCase__=None ) -> Optional[Any]: '''simple docstring''' if token_ids_a is None: return [self.cls_token_id] + token_ids_a + [self.sep_token_id] A_ = [self.cls_token_id] A_ = [self.sep_token_id] return _cls + token_ids_a + _sep + _sep + token_ids_a + _sep def snake_case_ ( self , UpperCamelCase__ , UpperCamelCase__=None ) -> Optional[Any]: '''simple docstring''' if offset_mapping_a is None: return [(0, 0)] + offset_mapping_a + [(0, 0)] return [(0, 0)] + offset_mapping_a + [(0, 0), (0, 0)] + offset_mapping_a + [(0, 0)] def snake_case_ ( self , UpperCamelCase__ , UpperCamelCase__=None , UpperCamelCase__=False ) -> Optional[int]: '''simple docstring''' if already_has_special_tokens: if token_ids_a is not None: raise ValueError( """You should not supply a second sequence if the provided sequence of """ """ids is already formatted with special tokens for the model.""" ) return [1 if x in [self.sep_token_id, self.cls_token_id] else 0 for x in token_ids_a] if token_ids_a is not None: return [1] + ([0] * len(UpperCamelCase__ )) + [1, 1] + ([0] * len(UpperCamelCase__ )) + [1] return [1] + ([0] * len(UpperCamelCase__ )) + [1] def snake_case_ ( self , UpperCamelCase__ , UpperCamelCase__ = None ) -> List[int]: '''simple docstring''' # called when `add_special_tokens` is True, so align with `build_inputs_with_special_tokens` method if token_ids_a is None: # [CLS] X [SEP] return (len(UpperCamelCase__ ) + 2) * [0] # [CLS] A [SEP] [SEP] B [SEP] return [0] * (len(UpperCamelCase__ ) + 1) + [1] * (len(UpperCamelCase__ ) + 3) def snake_case_ ( self , UpperCamelCase__ ) -> Union[str, Any]: '''simple docstring''' if "\u4e00" <= char <= "\u9fff": return True return False def snake_case_ ( self , UpperCamelCase__ ) -> Dict: '''simple docstring''' if ("a" <= char <= "z") or ("A" <= char <= "Z"): return True return False def snake_case_ ( self , UpperCamelCase__ ) -> Any: '''simple docstring''' if char in ",;:.?!~๏ผŒ๏ผ›๏ผšใ€‚๏ผŸ๏ผใ€Šใ€‹ใ€ใ€‘": return True return False def snake_case_ ( self , UpperCamelCase__ ) -> Any: '''simple docstring''' if char == " " or char == "\t" or char == "\n" or char == "\r": return True if len(UpperCamelCase__ ) == 1: A_ = unicodedata.category(UpperCamelCase__ ) if cat == "Zs": return True return False def snake_case_ ( self , UpperCamelCase__ ) -> Any: '''simple docstring''' A_ = {} with io.open(UpperCamelCase__ , """r""" , encoding="""utf-8""" ) as f: for index, line in enumerate(UpperCamelCase__ ): A_ = line.rstrip("""\n""" ) A_ = int(UpperCamelCase__ ) return token_to_idx def snake_case_ ( self , UpperCamelCase__ , UpperCamelCase__ = None ) -> Tuple[str]: '''simple docstring''' A_ = 0 if os.path.isdir(UpperCamelCase__ ): A_ = os.path.join( UpperCamelCase__ , (filename_prefix + """-""" if filename_prefix else """""") + VOCAB_FILES_NAMES["""vocab_file"""] ) else: A_ = (filename_prefix + """-""" if filename_prefix else """""") + save_directory with open(UpperCamelCase__ , """w""" , encoding="""utf-8""" ) as writer: for token, token_index in sorted(self.vocab.items() , key=lambda UpperCamelCase__ : kv[1] ): if index != token_index: logger.warning( f'''Saving vocabulary to {vocab_file}: vocabulary indices are not consecutive.''' """ Please check that the vocabulary is not corrupted!""" ) A_ = token_index writer.write(token + """\n""" ) index += 1 A_ = os.path.join(UpperCamelCase__ , """sentencepiece.bpe.model""" ) with open(UpperCamelCase__ , """wb""" ) as fi: A_ = self.sp_model.serialized_model_proto() fi.write(UpperCamelCase__ ) return (vocab_file,)
667
'''simple docstring''' def UpperCAmelCase__ ( UpperCAmelCase__, UpperCAmelCase__, UpperCAmelCase__ = 0, UpperCAmelCase__ = 0 ) -> int: A_ = right or len(UpperCAmelCase__ ) - 1 if left > right: return -1 elif list_data[left] == key: return left elif list_data[right] == key: return right else: return search(UpperCAmelCase__, UpperCAmelCase__, left + 1, right - 1 ) if __name__ == "__main__": import doctest doctest.testmod()
667
1
'''simple docstring''' from math import loga def UpperCAmelCase__ ( UpperCAmelCase__ ) -> int: if a < 0: raise ValueError("""Input value must be a positive integer""" ) elif isinstance(UpperCAmelCase__, UpperCAmelCase__ ): raise TypeError("""Input value must be a 'int' type""" ) return 0 if (a == 0) else int(loga(a & -a ) ) if __name__ == "__main__": import doctest doctest.testmod()
667
'''simple docstring''' import os import time import pytest from datasets.utils.filelock import FileLock, Timeout def UpperCAmelCase__ ( UpperCAmelCase__ ) -> int: A_ = FileLock(str(tmpdir / """foo.lock""" ) ) A_ = FileLock(str(tmpdir / """foo.lock""" ) ) A_ = 0.01 with locka.acquire(): with pytest.raises(UpperCAmelCase__ ): A_ = time.time() locka.acquire(UpperCAmelCase__ ) assert time.time() - _start > timeout def UpperCAmelCase__ ( UpperCAmelCase__ ) -> Dict: A_ = """a""" * 10_00 + """.lock""" A_ = FileLock(str(tmpdir / filename ) ) assert locka._lock_file.endswith(""".lock""" ) assert not locka._lock_file.endswith(UpperCAmelCase__ ) assert len(os.path.basename(locka._lock_file ) ) <= 2_55 A_ = FileLock(tmpdir / filename ) with locka.acquire(): with pytest.raises(UpperCAmelCase__ ): locka.acquire(0 )
667
1
'''simple docstring''' from __future__ import annotations from collections.abc import Iterable, Iterator from dataclasses import dataclass __lowerCamelCase = (3, 9, -11, 0, 7, 5, 1, -1) __lowerCamelCase = (4, 6, 2, 0, 8, 10, 3, -2) @dataclass class A__ : lowercase = 42 lowercase = 42 class A__ : def __init__( self , UpperCamelCase__ ) -> None: '''simple docstring''' A_ = None for i in sorted(UpperCamelCase__ , reverse=UpperCamelCase__ ): A_ = Node(UpperCamelCase__ , self.head ) def __iter__( self ) -> Iterator[int]: '''simple docstring''' A_ = self.head while node: yield node.data A_ = node.next_node def __len__( self ) -> int: '''simple docstring''' return sum(1 for _ in self ) def __str__( self ) -> str: '''simple docstring''' return " -> ".join([str(UpperCamelCase__ ) for node in self] ) def UpperCAmelCase__ ( UpperCAmelCase__, UpperCAmelCase__ ) -> SortedLinkedList: return SortedLinkedList(list(UpperCAmelCase__ ) + list(UpperCAmelCase__ ) ) if __name__ == "__main__": import doctest doctest.testmod() __lowerCamelCase = SortedLinkedList print(merge_lists(SSL(test_data_odd), SSL(test_data_even)))
667
'''simple docstring''' from dataclasses import dataclass from typing import Optional import numpy as np import torch import torch.nn as nn from ..utils import BaseOutput, is_torch_version, randn_tensor from .attention_processor import SpatialNorm from .unet_ad_blocks import UNetMidBlockaD, get_down_block, get_up_block @dataclass class A__ ( _snake_case ): lowercase = 42 class A__ ( nn.Module ): def __init__( self , UpperCamelCase__=3 , UpperCamelCase__=3 , UpperCamelCase__=("DownEncoderBlock2D",) , UpperCamelCase__=(64,) , UpperCamelCase__=2 , UpperCamelCase__=32 , UpperCamelCase__="silu" , UpperCamelCase__=True , ) -> Union[str, Any]: '''simple docstring''' super().__init__() A_ = layers_per_block A_ = torch.nn.Convad( UpperCamelCase__ , block_out_channels[0] , kernel_size=3 , stride=1 , padding=1 , ) A_ = None A_ = nn.ModuleList([] ) # down A_ = block_out_channels[0] for i, down_block_type in enumerate(UpperCamelCase__ ): A_ = output_channel A_ = block_out_channels[i] A_ = i == len(UpperCamelCase__ ) - 1 A_ = get_down_block( UpperCamelCase__ , num_layers=self.layers_per_block , in_channels=UpperCamelCase__ , out_channels=UpperCamelCase__ , add_downsample=not is_final_block , resnet_eps=1e-6 , downsample_padding=0 , resnet_act_fn=UpperCamelCase__ , resnet_groups=UpperCamelCase__ , attention_head_dim=UpperCamelCase__ , temb_channels=UpperCamelCase__ , ) self.down_blocks.append(UpperCamelCase__ ) # mid A_ = UNetMidBlockaD( in_channels=block_out_channels[-1] , resnet_eps=1e-6 , resnet_act_fn=UpperCamelCase__ , output_scale_factor=1 , resnet_time_scale_shift="""default""" , attention_head_dim=block_out_channels[-1] , resnet_groups=UpperCamelCase__ , temb_channels=UpperCamelCase__ , ) # out A_ = nn.GroupNorm(num_channels=block_out_channels[-1] , num_groups=UpperCamelCase__ , eps=1e-6 ) A_ = nn.SiLU() A_ = 2 * out_channels if double_z else out_channels A_ = nn.Convad(block_out_channels[-1] , UpperCamelCase__ , 3 , padding=1 ) A_ = False def snake_case_ ( self , UpperCamelCase__ ) -> str: '''simple docstring''' A_ = x A_ = self.conv_in(UpperCamelCase__ ) if self.training and self.gradient_checkpointing: def create_custom_forward(UpperCamelCase__ ): def custom_forward(*UpperCamelCase__ ): return module(*UpperCamelCase__ ) return custom_forward # down if is_torch_version(""">=""" , """1.11.0""" ): for down_block in self.down_blocks: A_ = torch.utils.checkpoint.checkpoint( create_custom_forward(UpperCamelCase__ ) , UpperCamelCase__ , use_reentrant=UpperCamelCase__ ) # middle A_ = torch.utils.checkpoint.checkpoint( create_custom_forward(self.mid_block ) , UpperCamelCase__ , use_reentrant=UpperCamelCase__ ) else: for down_block in self.down_blocks: A_ = torch.utils.checkpoint.checkpoint(create_custom_forward(UpperCamelCase__ ) , UpperCamelCase__ ) # middle A_ = torch.utils.checkpoint.checkpoint(create_custom_forward(self.mid_block ) , UpperCamelCase__ ) else: # down for down_block in self.down_blocks: A_ = down_block(UpperCamelCase__ ) # middle A_ = self.mid_block(UpperCamelCase__ ) # post-process A_ = self.conv_norm_out(UpperCamelCase__ ) A_ = self.conv_act(UpperCamelCase__ ) A_ = self.conv_out(UpperCamelCase__ ) return sample class A__ ( nn.Module ): def __init__( self , UpperCamelCase__=3 , UpperCamelCase__=3 , UpperCamelCase__=("UpDecoderBlock2D",) , UpperCamelCase__=(64,) , UpperCamelCase__=2 , UpperCamelCase__=32 , UpperCamelCase__="silu" , UpperCamelCase__="group" , ) -> List[Any]: '''simple docstring''' super().__init__() A_ = layers_per_block A_ = nn.Convad( UpperCamelCase__ , block_out_channels[-1] , kernel_size=3 , stride=1 , padding=1 , ) A_ = None A_ = nn.ModuleList([] ) A_ = in_channels if norm_type == """spatial""" else None # mid A_ = UNetMidBlockaD( in_channels=block_out_channels[-1] , resnet_eps=1e-6 , resnet_act_fn=UpperCamelCase__ , output_scale_factor=1 , resnet_time_scale_shift="""default""" if norm_type == """group""" else norm_type , attention_head_dim=block_out_channels[-1] , resnet_groups=UpperCamelCase__ , temb_channels=UpperCamelCase__ , ) # up A_ = list(reversed(UpperCamelCase__ ) ) A_ = reversed_block_out_channels[0] for i, up_block_type in enumerate(UpperCamelCase__ ): A_ = output_channel A_ = reversed_block_out_channels[i] A_ = i == len(UpperCamelCase__ ) - 1 A_ = get_up_block( UpperCamelCase__ , num_layers=self.layers_per_block + 1 , in_channels=UpperCamelCase__ , out_channels=UpperCamelCase__ , prev_output_channel=UpperCamelCase__ , add_upsample=not is_final_block , resnet_eps=1e-6 , resnet_act_fn=UpperCamelCase__ , resnet_groups=UpperCamelCase__ , attention_head_dim=UpperCamelCase__ , temb_channels=UpperCamelCase__ , resnet_time_scale_shift=UpperCamelCase__ , ) self.up_blocks.append(UpperCamelCase__ ) A_ = output_channel # out if norm_type == "spatial": A_ = SpatialNorm(block_out_channels[0] , UpperCamelCase__ ) else: A_ = nn.GroupNorm(num_channels=block_out_channels[0] , num_groups=UpperCamelCase__ , eps=1e-6 ) A_ = nn.SiLU() A_ = nn.Convad(block_out_channels[0] , UpperCamelCase__ , 3 , padding=1 ) A_ = False def snake_case_ ( self , UpperCamelCase__ , UpperCamelCase__=None ) -> Optional[Any]: '''simple docstring''' A_ = z A_ = self.conv_in(UpperCamelCase__ ) A_ = next(iter(self.up_blocks.parameters() ) ).dtype if self.training and self.gradient_checkpointing: def create_custom_forward(UpperCamelCase__ ): def custom_forward(*UpperCamelCase__ ): return module(*UpperCamelCase__ ) return custom_forward if is_torch_version(""">=""" , """1.11.0""" ): # middle A_ = torch.utils.checkpoint.checkpoint( create_custom_forward(self.mid_block ) , UpperCamelCase__ , UpperCamelCase__ , use_reentrant=UpperCamelCase__ ) A_ = sample.to(UpperCamelCase__ ) # up for up_block in self.up_blocks: A_ = torch.utils.checkpoint.checkpoint( create_custom_forward(UpperCamelCase__ ) , UpperCamelCase__ , UpperCamelCase__ , use_reentrant=UpperCamelCase__ ) else: # middle A_ = torch.utils.checkpoint.checkpoint( create_custom_forward(self.mid_block ) , UpperCamelCase__ , UpperCamelCase__ ) A_ = sample.to(UpperCamelCase__ ) # up for up_block in self.up_blocks: A_ = torch.utils.checkpoint.checkpoint(create_custom_forward(UpperCamelCase__ ) , UpperCamelCase__ , UpperCamelCase__ ) else: # middle A_ = self.mid_block(UpperCamelCase__ , UpperCamelCase__ ) A_ = sample.to(UpperCamelCase__ ) # up for up_block in self.up_blocks: A_ = up_block(UpperCamelCase__ , UpperCamelCase__ ) # post-process if latent_embeds is None: A_ = self.conv_norm_out(UpperCamelCase__ ) else: A_ = self.conv_norm_out(UpperCamelCase__ , UpperCamelCase__ ) A_ = self.conv_act(UpperCamelCase__ ) A_ = self.conv_out(UpperCamelCase__ ) return sample class A__ ( nn.Module ): def __init__( self , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__=None , UpperCamelCase__="random" , UpperCamelCase__=False , UpperCamelCase__=True ) -> str: '''simple docstring''' super().__init__() A_ = n_e A_ = vq_embed_dim A_ = beta A_ = legacy A_ = nn.Embedding(self.n_e , self.vq_embed_dim ) self.embedding.weight.data.uniform_(-1.0 / self.n_e , 1.0 / self.n_e ) A_ = remap if self.remap is not None: self.register_buffer("""used""" , torch.tensor(np.load(self.remap ) ) ) A_ = self.used.shape[0] A_ = unknown_index # "random" or "extra" or integer if self.unknown_index == "extra": A_ = self.re_embed A_ = self.re_embed + 1 print( f'''Remapping {self.n_e} indices to {self.re_embed} indices. ''' f'''Using {self.unknown_index} for unknown indices.''' ) else: A_ = n_e A_ = sane_index_shape def snake_case_ ( self , UpperCamelCase__ ) -> Optional[Any]: '''simple docstring''' A_ = inds.shape assert len(UpperCamelCase__ ) > 1 A_ = inds.reshape(ishape[0] , -1 ) A_ = self.used.to(UpperCamelCase__ ) A_ = (inds[:, :, None] == used[None, None, ...]).long() A_ = match.argmax(-1 ) A_ = match.sum(2 ) < 1 if self.unknown_index == "random": A_ = torch.randint(0 , self.re_embed , size=new[unknown].shape ).to(device=new.device ) else: A_ = self.unknown_index return new.reshape(UpperCamelCase__ ) def snake_case_ ( self , UpperCamelCase__ ) -> Dict: '''simple docstring''' A_ = inds.shape assert len(UpperCamelCase__ ) > 1 A_ = inds.reshape(ishape[0] , -1 ) A_ = self.used.to(UpperCamelCase__ ) if self.re_embed > self.used.shape[0]: # extra token A_ = 0 # simply set to zero A_ = torch.gather(used[None, :][inds.shape[0] * [0], :] , 1 , UpperCamelCase__ ) return back.reshape(UpperCamelCase__ ) def snake_case_ ( self , UpperCamelCase__ ) -> str: '''simple docstring''' # reshape z -> (batch, height, width, channel) and flatten A_ = z.permute(0 , 2 , 3 , 1 ).contiguous() A_ = z.view(-1 , self.vq_embed_dim ) # distances from z to embeddings e_j (z - e)^2 = z^2 + e^2 - 2 e * z A_ = torch.argmin(torch.cdist(UpperCamelCase__ , self.embedding.weight ) , dim=1 ) A_ = self.embedding(UpperCamelCase__ ).view(z.shape ) A_ = None A_ = None # compute loss for embedding if not self.legacy: A_ = self.beta * torch.mean((z_q.detach() - z) ** 2 ) + torch.mean((z_q - z.detach()) ** 2 ) else: A_ = torch.mean((z_q.detach() - z) ** 2 ) + self.beta * torch.mean((z_q - z.detach()) ** 2 ) # preserve gradients A_ = z + (z_q - z).detach() # reshape back to match original input shape A_ = z_q.permute(0 , 3 , 1 , 2 ).contiguous() if self.remap is not None: A_ = min_encoding_indices.reshape(z.shape[0] , -1 ) # add batch axis A_ = self.remap_to_used(UpperCamelCase__ ) A_ = min_encoding_indices.reshape(-1 , 1 ) # flatten if self.sane_index_shape: A_ = min_encoding_indices.reshape(z_q.shape[0] , z_q.shape[2] , z_q.shape[3] ) return z_q, loss, (perplexity, min_encodings, min_encoding_indices) def snake_case_ ( self , UpperCamelCase__ , UpperCamelCase__ ) -> Optional[int]: '''simple docstring''' # shape specifying (batch, height, width, channel) if self.remap is not None: A_ = indices.reshape(shape[0] , -1 ) # add batch axis A_ = self.unmap_to_all(UpperCamelCase__ ) A_ = indices.reshape(-1 ) # flatten again # get quantized latent vectors A_ = self.embedding(UpperCamelCase__ ) if shape is not None: A_ = z_q.view(UpperCamelCase__ ) # reshape back to match original input shape A_ = z_q.permute(0 , 3 , 1 , 2 ).contiguous() return z_q class A__ ( _snake_case ): def __init__( self , UpperCamelCase__ , UpperCamelCase__=False ) -> Dict: '''simple docstring''' A_ = parameters A_ , A_ = torch.chunk(UpperCamelCase__ , 2 , dim=1 ) A_ = torch.clamp(self.logvar , -30.0 , 20.0 ) A_ = deterministic A_ = torch.exp(0.5 * self.logvar ) A_ = torch.exp(self.logvar ) if self.deterministic: A_ = A_ = torch.zeros_like( self.mean , device=self.parameters.device , dtype=self.parameters.dtype ) def snake_case_ ( self , UpperCamelCase__ = None ) -> torch.FloatTensor: '''simple docstring''' # make sure sample is on the same device as the parameters and has same dtype A_ = randn_tensor( self.mean.shape , generator=UpperCamelCase__ , device=self.parameters.device , dtype=self.parameters.dtype ) A_ = self.mean + self.std * sample return x def snake_case_ ( self , UpperCamelCase__=None ) -> int: '''simple docstring''' if self.deterministic: return torch.Tensor([0.0] ) else: if other is None: return 0.5 * torch.sum(torch.pow(self.mean , 2 ) + self.var - 1.0 - self.logvar , dim=[1, 2, 3] ) else: return 0.5 * torch.sum( torch.pow(self.mean - other.mean , 2 ) / other.var + self.var / other.var - 1.0 - self.logvar + other.logvar , dim=[1, 2, 3] , ) def snake_case_ ( self , UpperCamelCase__ , UpperCamelCase__=[1, 2, 3] ) -> Optional[Any]: '''simple docstring''' if self.deterministic: return torch.Tensor([0.0] ) A_ = np.log(2.0 * np.pi ) return 0.5 * torch.sum(logtwopi + self.logvar + torch.pow(sample - self.mean , 2 ) / self.var , dim=UpperCamelCase__ ) def snake_case_ ( self ) -> List[Any]: '''simple docstring''' return self.mean
667
1
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_torch_available __lowerCamelCase = { '''configuration_longt5''': ['''LONGT5_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''LongT5Config''', '''LongT5OnnxConfig'''], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __lowerCamelCase = [ '''LONGT5_PRETRAINED_MODEL_ARCHIVE_LIST''', '''LongT5EncoderModel''', '''LongT5ForConditionalGeneration''', '''LongT5Model''', '''LongT5PreTrainedModel''', ] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __lowerCamelCase = [ '''FlaxLongT5ForConditionalGeneration''', '''FlaxLongT5Model''', '''FlaxLongT5PreTrainedModel''', ] if TYPE_CHECKING: from .configuration_longta import LONGT5_PRETRAINED_CONFIG_ARCHIVE_MAP, LongTaConfig, LongTaOnnxConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_longta import ( LONGT5_PRETRAINED_MODEL_ARCHIVE_LIST, LongTaEncoderModel, LongTaForConditionalGeneration, LongTaModel, LongTaPreTrainedModel, ) try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_flax_longta import ( FlaxLongTaForConditionalGeneration, FlaxLongTaModel, FlaxLongTaPreTrainedModel, ) else: import sys __lowerCamelCase = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
667
'''simple docstring''' import argparse import json import os from collections import OrderedDict import torch from transformers import LukeConfig, LukeForMaskedLM, MLukeTokenizer, XLMRobertaTokenizer from transformers.tokenization_utils_base import AddedToken @torch.no_grad() def UpperCAmelCase__ ( UpperCAmelCase__, UpperCAmelCase__, UpperCAmelCase__, UpperCAmelCase__, UpperCAmelCase__ ) -> List[Any]: # Load configuration defined in the metadata file with open(UpperCAmelCase__ ) as metadata_file: A_ = json.load(UpperCAmelCase__ ) A_ = LukeConfig(use_entity_aware_attention=UpperCAmelCase__, **metadata["""model_config"""] ) # Load in the weights from the checkpoint_path A_ = torch.load(UpperCAmelCase__, map_location="""cpu""" )["""module"""] # Load the entity vocab file A_ = load_original_entity_vocab(UpperCAmelCase__ ) # add an entry for [MASK2] A_ = max(entity_vocab.values() ) + 1 config.entity_vocab_size += 1 A_ = XLMRobertaTokenizer.from_pretrained(metadata["""model_config"""]["""bert_model_name"""] ) # Add special tokens to the token vocabulary for downstream tasks A_ = AddedToken("""<ent>""", lstrip=UpperCAmelCase__, rstrip=UpperCAmelCase__ ) A_ = AddedToken("""<ent2>""", lstrip=UpperCAmelCase__, rstrip=UpperCAmelCase__ ) tokenizer.add_special_tokens({"""additional_special_tokens""": [entity_token_a, entity_token_a]} ) config.vocab_size += 2 print(F'''Saving tokenizer to {pytorch_dump_folder_path}''' ) tokenizer.save_pretrained(UpperCAmelCase__ ) with open(os.path.join(UpperCAmelCase__, """tokenizer_config.json""" ), """r""" ) as f: A_ = json.load(UpperCAmelCase__ ) A_ = """MLukeTokenizer""" with open(os.path.join(UpperCAmelCase__, """tokenizer_config.json""" ), """w""" ) as f: json.dump(UpperCAmelCase__, UpperCAmelCase__ ) with open(os.path.join(UpperCAmelCase__, MLukeTokenizer.vocab_files_names["""entity_vocab_file"""] ), """w""" ) as f: json.dump(UpperCAmelCase__, UpperCAmelCase__ ) A_ = MLukeTokenizer.from_pretrained(UpperCAmelCase__ ) # Initialize the embeddings of the special tokens A_ = tokenizer.convert_tokens_to_ids(["""@"""] )[0] A_ = tokenizer.convert_tokens_to_ids(["""#"""] )[0] A_ = state_dict["""embeddings.word_embeddings.weight"""] A_ = word_emb[ent_init_index].unsqueeze(0 ) A_ = word_emb[enta_init_index].unsqueeze(0 ) A_ = torch.cat([word_emb, ent_emb, enta_emb] ) # add special tokens for 'entity_predictions.bias' for bias_name in ["lm_head.decoder.bias", "lm_head.bias"]: A_ = state_dict[bias_name] A_ = decoder_bias[ent_init_index].unsqueeze(0 ) A_ = decoder_bias[enta_init_index].unsqueeze(0 ) A_ = torch.cat([decoder_bias, ent_decoder_bias, enta_decoder_bias] ) # Initialize the query layers of the entity-aware self-attention mechanism for layer_index in range(config.num_hidden_layers ): for matrix_name in ["query.weight", "query.bias"]: A_ = F'''encoder.layer.{layer_index}.attention.self.''' A_ = state_dict[prefix + matrix_name] A_ = state_dict[prefix + matrix_name] A_ = state_dict[prefix + matrix_name] # Initialize the embedding of the [MASK2] entity using that of the [MASK] entity for downstream tasks A_ = state_dict["""entity_embeddings.entity_embeddings.weight"""] A_ = entity_emb[entity_vocab["""[MASK]"""]].unsqueeze(0 ) A_ = torch.cat([entity_emb, entity_mask_emb] ) # add [MASK2] for 'entity_predictions.bias' A_ = state_dict["""entity_predictions.bias"""] A_ = entity_prediction_bias[entity_vocab["""[MASK]"""]].unsqueeze(0 ) A_ = torch.cat([entity_prediction_bias, entity_mask_bias] ) A_ = LukeForMaskedLM(config=UpperCAmelCase__ ).eval() state_dict.pop("""entity_predictions.decoder.weight""" ) state_dict.pop("""lm_head.decoder.weight""" ) state_dict.pop("""lm_head.decoder.bias""" ) A_ = OrderedDict() for key, value in state_dict.items(): if not (key.startswith("""lm_head""" ) or key.startswith("""entity_predictions""" )): A_ = state_dict[key] else: A_ = state_dict[key] A_ , A_ = model.load_state_dict(UpperCAmelCase__, strict=UpperCAmelCase__ ) if set(UpperCAmelCase__ ) != {"luke.embeddings.position_ids"}: raise ValueError(F'''Unexpected unexpected_keys: {unexpected_keys}''' ) if set(UpperCAmelCase__ ) != { "lm_head.decoder.weight", "lm_head.decoder.bias", "entity_predictions.decoder.weight", }: raise ValueError(F'''Unexpected missing_keys: {missing_keys}''' ) model.tie_weights() assert (model.luke.embeddings.word_embeddings.weight == model.lm_head.decoder.weight).all() assert (model.luke.entity_embeddings.entity_embeddings.weight == model.entity_predictions.decoder.weight).all() # Check outputs A_ = MLukeTokenizer.from_pretrained(UpperCAmelCase__, task="""entity_classification""" ) A_ = """ISO 639-3 uses the code fas for the dialects spoken across Iran and ใ‚ขใƒ•ใ‚ฌใƒ‹ใ‚นใ‚ฟใƒณ (Afghanistan).""" A_ = (0, 9) A_ = tokenizer(UpperCAmelCase__, entity_spans=[span], return_tensors="""pt""" ) A_ = model(**UpperCAmelCase__ ) # Verify word hidden states if model_size == "large": raise NotImplementedError else: # base A_ = torch.Size((1, 33, 7_68) ) A_ = torch.tensor([[0.0_892, 0.0_596, -0.2_819], [0.0_134, 0.1_199, 0.0_573], [-0.0_169, 0.0_927, 0.0_644]] ) if not (outputs.last_hidden_state.shape == expected_shape): raise ValueError( F'''Outputs.last_hidden_state.shape is {outputs.last_hidden_state.shape}, Expected shape is {expected_shape}''' ) if not torch.allclose(outputs.last_hidden_state[0, :3, :3], UpperCAmelCase__, atol=1e-4 ): raise ValueError # Verify entity hidden states if model_size == "large": raise NotImplementedError else: # base A_ = torch.Size((1, 1, 7_68) ) A_ = torch.tensor([[-0.1_482, 0.0_609, 0.0_322]] ) if not (outputs.entity_last_hidden_state.shape == expected_shape): raise ValueError( F'''Outputs.entity_last_hidden_state.shape is {outputs.entity_last_hidden_state.shape}, Expected shape is''' F''' {expected_shape}''' ) if not torch.allclose(outputs.entity_last_hidden_state[0, :3, :3], UpperCAmelCase__, atol=1e-4 ): raise ValueError # Verify masked word/entity prediction A_ = MLukeTokenizer.from_pretrained(UpperCAmelCase__ ) A_ = """Tokyo is the capital of <mask>.""" A_ = (24, 30) A_ = tokenizer(UpperCAmelCase__, entity_spans=[span], return_tensors="""pt""" ) A_ = model(**UpperCAmelCase__ ) A_ = encoding["""input_ids"""][0].tolist() A_ = input_ids.index(tokenizer.convert_tokens_to_ids("""<mask>""" ) ) A_ = outputs.logits[0][mask_position_id].argmax(dim=-1 ) assert "Japan" == tokenizer.decode(UpperCAmelCase__ ) A_ = outputs.entity_logits[0][0].argmax().item() A_ = [ entity for entity, entity_id in tokenizer.entity_vocab.items() if entity_id == predicted_entity_id ] assert [e for e in multilingual_predicted_entities if e.startswith("""en:""" )][0] == "en:Japan" # Finally, save our PyTorch model and tokenizer print("""Saving PyTorch model to {}""".format(UpperCAmelCase__ ) ) model.save_pretrained(UpperCAmelCase__ ) def UpperCAmelCase__ ( UpperCAmelCase__ ) -> int: A_ = ["""[MASK]""", """[PAD]""", """[UNK]"""] A_ = [json.loads(UpperCAmelCase__ ) for line in open(UpperCAmelCase__ )] A_ = {} for entry in data: A_ = entry["""id"""] for entity_name, language in entry["entities"]: if entity_name in SPECIAL_TOKENS: A_ = entity_id break A_ = F'''{language}:{entity_name}''' A_ = entity_id return new_mapping if __name__ == "__main__": __lowerCamelCase = argparse.ArgumentParser() # Required parameters parser.add_argument('''--checkpoint_path''', type=str, help='''Path to a pytorch_model.bin file.''') parser.add_argument( '''--metadata_path''', default=None, type=str, help='''Path to a metadata.json file, defining the configuration.''' ) parser.add_argument( '''--entity_vocab_path''', default=None, type=str, help='''Path to an entity_vocab.tsv file, containing the entity vocabulary.''', ) parser.add_argument( '''--pytorch_dump_folder_path''', default=None, type=str, help='''Path to where to dump the output PyTorch model.''' ) parser.add_argument( '''--model_size''', default='''base''', type=str, choices=['''base''', '''large'''], help='''Size of the model to be converted.''' ) __lowerCamelCase = parser.parse_args() convert_luke_checkpoint( args.checkpoint_path, args.metadata_path, args.entity_vocab_path, args.pytorch_dump_folder_path, args.model_size, )
667
1
'''simple docstring''' def UpperCAmelCase__ ( UpperCAmelCase__, UpperCAmelCase__ ) -> str: print("""\nThe shortest path matrix using Floyd Warshall algorithm\n""" ) for i in range(UpperCAmelCase__ ): for j in range(UpperCAmelCase__ ): if dist[i][j] != float("""inf""" ): print(int(dist[i][j] ), end="""\t""" ) else: print("""INF""", end="""\t""" ) print() def UpperCAmelCase__ ( UpperCAmelCase__, UpperCAmelCase__ ) -> Optional[int]: A_ = [[float("""inf""" ) for _ in range(UpperCAmelCase__ )] for _ in range(UpperCAmelCase__ )] for i in range(UpperCAmelCase__ ): for j in range(UpperCAmelCase__ ): A_ = graph[i][j] # check vertex k against all other vertices (i, j) for k in range(UpperCAmelCase__ ): # looping through rows of graph array for i in range(UpperCAmelCase__ ): # looping through columns of graph array for j in range(UpperCAmelCase__ ): if ( dist[i][k] != float("""inf""" ) and dist[k][j] != float("""inf""" ) and dist[i][k] + dist[k][j] < dist[i][j] ): A_ = dist[i][k] + dist[k][j] _print_dist(UpperCAmelCase__, UpperCAmelCase__ ) return dist, v if __name__ == "__main__": __lowerCamelCase = int(input('''Enter number of vertices: ''')) __lowerCamelCase = int(input('''Enter number of edges: ''')) __lowerCamelCase = [[float('''inf''') for i in range(v)] for j in range(v)] for i in range(v): __lowerCamelCase = 0.0 # src and dst are indices that must be within the array size graph[e][v] # failure to follow this will result in an error for i in range(e): print('''\nEdge ''', i + 1) __lowerCamelCase = int(input('''Enter source:''')) __lowerCamelCase = int(input('''Enter destination:''')) __lowerCamelCase = float(input('''Enter weight:''')) __lowerCamelCase = weight floyd_warshall(graph, v) # Example Input # Enter number of vertices: 3 # Enter number of edges: 2 # # generated graph from vertex and edge inputs # [[inf, inf, inf], [inf, inf, inf], [inf, inf, inf]] # [[0.0, inf, inf], [inf, 0.0, inf], [inf, inf, 0.0]] # specify source, destination and weight for edge #1 # Edge 1 # Enter source:1 # Enter destination:2 # Enter weight:2 # specify source, destination and weight for edge #2 # Edge 2 # Enter source:2 # Enter destination:1 # Enter weight:1 # # Expected Output from the vertice, edge and src, dst, weight inputs!! # 0 INF INF # INF 0 2 # INF 1 0
667
'''simple docstring''' from ...processing_utils import ProcessorMixin from ...tokenization_utils_base import BatchEncoding class A__ ( _snake_case ): lowercase = "ClapFeatureExtractor" lowercase = ("RobertaTokenizer", "RobertaTokenizerFast") def __init__( self , UpperCamelCase__ , UpperCamelCase__ ) -> Tuple: '''simple docstring''' super().__init__(UpperCamelCase__ , UpperCamelCase__ ) def __call__( self , UpperCamelCase__=None , UpperCamelCase__=None , UpperCamelCase__=None , **UpperCamelCase__ ) -> Tuple: '''simple docstring''' A_ = kwargs.pop("""sampling_rate""" , UpperCamelCase__ ) if text is None and audios is None: raise ValueError("""You have to specify either text or audios. Both cannot be none.""" ) if text is not None: A_ = self.tokenizer(UpperCamelCase__ , return_tensors=UpperCamelCase__ , **UpperCamelCase__ ) if audios is not None: A_ = self.feature_extractor( UpperCamelCase__ , sampling_rate=UpperCamelCase__ , return_tensors=UpperCamelCase__ , **UpperCamelCase__ ) if text is not None and audios is not None: A_ = audio_features.input_features return encoding elif text is not None: return encoding else: return BatchEncoding(data=dict(**UpperCamelCase__ ) , tensor_type=UpperCamelCase__ ) def snake_case_ ( self , *UpperCamelCase__ , **UpperCamelCase__ ) -> Optional[int]: '''simple docstring''' return self.tokenizer.batch_decode(*UpperCamelCase__ , **UpperCamelCase__ ) def snake_case_ ( self , *UpperCamelCase__ , **UpperCamelCase__ ) -> str: '''simple docstring''' return self.tokenizer.decode(*UpperCamelCase__ , **UpperCamelCase__ ) @property def snake_case_ ( self ) -> int: '''simple docstring''' A_ = self.tokenizer.model_input_names A_ = self.feature_extractor.model_input_names return list(dict.fromkeys(tokenizer_input_names + feature_extractor_input_names ) )
667
1
'''simple docstring''' import math from ...configuration_utils import PretrainedConfig from ...utils import logging __lowerCamelCase = logging.get_logger(__name__) __lowerCamelCase = { '''facebook/data2vec-base-960h''': '''https://huggingface.co/facebook/data2vec-audio-base-960h/resolve/main/config.json''', # See all Data2VecAudio models at https://huggingface.co/models?filter=data2vec-audio } class A__ ( _snake_case ): lowercase = "data2vec-audio" def __init__( self , UpperCamelCase__=32 , UpperCamelCase__=768 , UpperCamelCase__=12 , UpperCamelCase__=12 , UpperCamelCase__=3072 , UpperCamelCase__="gelu" , UpperCamelCase__=0.1 , UpperCamelCase__=0.1 , UpperCamelCase__=0.1 , UpperCamelCase__=0.0 , UpperCamelCase__=0.1 , UpperCamelCase__=0.1 , UpperCamelCase__=0.02 , UpperCamelCase__=1e-5 , UpperCamelCase__="gelu" , UpperCamelCase__=(512, 512, 512, 512, 512, 512, 512) , UpperCamelCase__=(5, 2, 2, 2, 2, 2, 2) , UpperCamelCase__=(10, 3, 3, 3, 3, 2, 2) , UpperCamelCase__=False , UpperCamelCase__=16 , UpperCamelCase__=19 , UpperCamelCase__=5 , UpperCamelCase__=0.05 , UpperCamelCase__=10 , UpperCamelCase__=2 , UpperCamelCase__=0.0 , UpperCamelCase__=10 , UpperCamelCase__=0 , UpperCamelCase__="sum" , UpperCamelCase__=False , UpperCamelCase__=False , UpperCamelCase__=256 , UpperCamelCase__=(512, 512, 512, 512, 1500) , UpperCamelCase__=(5, 3, 3, 1, 1) , UpperCamelCase__=(1, 2, 3, 1, 1) , UpperCamelCase__=512 , UpperCamelCase__=0 , UpperCamelCase__=1 , UpperCamelCase__=2 , UpperCamelCase__=False , UpperCamelCase__=3 , UpperCamelCase__=2 , UpperCamelCase__=3 , UpperCamelCase__=None , **UpperCamelCase__ , ) -> Tuple: '''simple docstring''' super().__init__(**UpperCamelCase__ , pad_token_id=UpperCamelCase__ , bos_token_id=UpperCamelCase__ , eos_token_id=UpperCamelCase__ ) A_ = hidden_size A_ = feat_extract_activation A_ = list(UpperCamelCase__ ) A_ = list(UpperCamelCase__ ) A_ = list(UpperCamelCase__ ) A_ = conv_bias A_ = num_conv_pos_embeddings A_ = num_conv_pos_embedding_groups A_ = conv_pos_kernel_size A_ = len(self.conv_dim ) A_ = num_hidden_layers A_ = intermediate_size A_ = hidden_act A_ = num_attention_heads A_ = hidden_dropout A_ = attention_dropout A_ = activation_dropout A_ = feat_proj_dropout A_ = final_dropout A_ = layerdrop A_ = layer_norm_eps A_ = initializer_range A_ = vocab_size A_ = use_weighted_layer_sum if ( (len(self.conv_stride ) != self.num_feat_extract_layers) or (len(self.conv_kernel ) != self.num_feat_extract_layers) or (len(self.conv_dim ) != self.num_feat_extract_layers) ): raise ValueError( """Configuration for convolutional layers is incorrect. It is required that `len(config.conv_dim)` ==""" """ `len(config.conv_stride)` == `len(config.conv_kernel)`, but is `len(config.conv_dim) =""" f''' {len(self.conv_dim )}`, `len(config.conv_stride) = {len(self.conv_stride )}`,''' f''' `len(config.conv_kernel) = {len(self.conv_kernel )}`.''' ) # fine-tuning config parameters for SpecAugment: https://arxiv.org/abs/1904.08779 A_ = mask_time_prob A_ = mask_time_length A_ = mask_time_min_masks A_ = mask_feature_prob A_ = mask_feature_length A_ = mask_feature_min_masks # ctc loss A_ = ctc_loss_reduction A_ = ctc_zero_infinity # adapter A_ = add_adapter A_ = adapter_kernel_size A_ = adapter_stride A_ = num_adapter_layers A_ = output_hidden_size or hidden_size # SequenceClassification-specific parameter. Feel free to ignore for other classes. A_ = classifier_proj_size # XVector-specific parameters. Feel free to ignore for other classes. A_ = list(UpperCamelCase__ ) A_ = list(UpperCamelCase__ ) A_ = list(UpperCamelCase__ ) A_ = xvector_output_dim @property def snake_case_ ( self ) -> Optional[int]: '''simple docstring''' return math.prod(self.conv_stride )
667
'''simple docstring''' import numpy as np from cva import COLOR_BGR2GRAY, cvtColor, imread from numpy import array, uinta from PIL import Image from digital_image_processing import change_contrast as cc from digital_image_processing import convert_to_negative as cn from digital_image_processing import sepia as sp from digital_image_processing.dithering import burkes as bs from digital_image_processing.edge_detection import canny from digital_image_processing.filters import convolve as conv from digital_image_processing.filters import gaussian_filter as gg from digital_image_processing.filters import local_binary_pattern as lbp from digital_image_processing.filters import median_filter as med from digital_image_processing.filters import sobel_filter as sob from digital_image_processing.resize import resize as rs __lowerCamelCase = imread(r'''digital_image_processing/image_data/lena_small.jpg''') __lowerCamelCase = cvtColor(img, COLOR_BGR2GRAY) def UpperCAmelCase__ ( ) -> Dict: A_ = cn.convert_to_negative(UpperCAmelCase__ ) # assert negative_img array for at least one True assert negative_img.any() def UpperCAmelCase__ ( ) -> List[Any]: with Image.open("""digital_image_processing/image_data/lena_small.jpg""" ) as img: # Work around assertion for response assert str(cc.change_contrast(UpperCAmelCase__, 1_10 ) ).startswith( """<PIL.Image.Image image mode=RGB size=100x100 at""" ) def UpperCAmelCase__ ( ) -> str: A_ = canny.gen_gaussian_kernel(9, sigma=1.4 ) # Assert ambiguous array assert resp.all() def UpperCAmelCase__ ( ) -> Union[str, Any]: A_ = imread("""digital_image_processing/image_data/lena_small.jpg""", 0 ) # assert ambiguous array for all == True assert canny_img.all() A_ = canny.canny(UpperCAmelCase__ ) # assert canny array for at least one True assert canny_array.any() def UpperCAmelCase__ ( ) -> Dict: assert gg.gaussian_filter(UpperCAmelCase__, 5, sigma=0.9 ).all() def UpperCAmelCase__ ( ) -> int: # laplace diagonals A_ = array([[0.25, 0.5, 0.25], [0.5, -3, 0.5], [0.25, 0.5, 0.25]] ) A_ = conv.img_convolve(UpperCAmelCase__, UpperCAmelCase__ ).astype(UpperCAmelCase__ ) assert res.any() def UpperCAmelCase__ ( ) -> List[Any]: assert med.median_filter(UpperCAmelCase__, 3 ).any() def UpperCAmelCase__ ( ) -> List[Any]: A_ , A_ = sob.sobel_filter(UpperCAmelCase__ ) assert grad.any() and theta.any() def UpperCAmelCase__ ( ) -> List[str]: A_ = sp.make_sepia(UpperCAmelCase__, 20 ) assert sepia.all() def UpperCAmelCase__ ( UpperCAmelCase__ = "digital_image_processing/image_data/lena_small.jpg" ) -> List[Any]: A_ = bs.Burkes(imread(UpperCAmelCase__, 1 ), 1_20 ) burkes.process() assert burkes.output_img.any() def UpperCAmelCase__ ( UpperCAmelCase__ = "digital_image_processing/image_data/lena_small.jpg", ) -> Optional[int]: A_ = rs.NearestNeighbour(imread(UpperCAmelCase__, 1 ), 4_00, 2_00 ) nn.process() assert nn.output.any() def UpperCAmelCase__ ( ) -> Optional[int]: A_ = """digital_image_processing/image_data/lena.jpg""" # Reading the image and converting it to grayscale. A_ = imread(UpperCAmelCase__, 0 ) # Test for get_neighbors_pixel function() return not None A_ = 0 A_ = 0 A_ = image[x_coordinate][y_coordinate] A_ = lbp.get_neighbors_pixel( UpperCAmelCase__, UpperCAmelCase__, UpperCAmelCase__, UpperCAmelCase__ ) assert neighbors_pixels is not None # Test for local_binary_pattern function() # Create a numpy array as the same height and width of read image A_ = np.zeros((image.shape[0], image.shape[1]) ) # Iterating through the image and calculating the local binary pattern value # for each pixel. for i in range(0, image.shape[0] ): for j in range(0, image.shape[1] ): A_ = lbp.local_binary_value(UpperCAmelCase__, UpperCAmelCase__, UpperCAmelCase__ ) assert lbp_image.any()
667
1
'''simple docstring''' import math import os import sys def UpperCAmelCase__ ( UpperCAmelCase__ ) -> str: A_ = """""" try: with open(UpperCAmelCase__, """rb""" ) as binary_file: A_ = binary_file.read() for dat in data: A_ = F'''{dat:08b}''' result += curr_byte return result except OSError: print("""File not accessible""" ) sys.exit() def UpperCAmelCase__ ( UpperCAmelCase__, UpperCAmelCase__, UpperCAmelCase__, UpperCAmelCase__ ) -> None: lexicon.pop(UpperCAmelCase__ ) A_ = last_match_id if math.loga(UpperCAmelCase__ ).is_integer(): for curr_key in lexicon: A_ = """0""" + lexicon[curr_key] A_ = bin(UpperCAmelCase__ )[2:] def UpperCAmelCase__ ( UpperCAmelCase__ ) -> str: A_ = {"""0""": """0""", """1""": """1"""} A_ , A_ = """""", """""" A_ = len(UpperCAmelCase__ ) for i in range(len(UpperCAmelCase__ ) ): curr_string += data_bits[i] if curr_string not in lexicon: continue A_ = lexicon[curr_string] result += last_match_id add_key_to_lexicon(UpperCAmelCase__, UpperCAmelCase__, UpperCAmelCase__, UpperCAmelCase__ ) index += 1 A_ = """""" while curr_string != "" and curr_string not in lexicon: curr_string += "0" if curr_string != "": A_ = lexicon[curr_string] result += last_match_id return result def UpperCAmelCase__ ( UpperCAmelCase__, UpperCAmelCase__ ) -> str: A_ = os.path.getsize(UpperCAmelCase__ ) A_ = bin(UpperCAmelCase__ )[2:] A_ = len(UpperCAmelCase__ ) return "0" * (length_length - 1) + file_length_binary + compressed def UpperCAmelCase__ ( UpperCAmelCase__, UpperCAmelCase__ ) -> None: A_ = 8 try: with open(UpperCAmelCase__, """wb""" ) as opened_file: A_ = [ to_write[i : i + byte_length] for i in range(0, len(UpperCAmelCase__ ), UpperCAmelCase__ ) ] if len(result_byte_array[-1] ) % byte_length == 0: result_byte_array.append("""10000000""" ) else: result_byte_array[-1] += "1" + "0" * ( byte_length - len(result_byte_array[-1] ) - 1 ) for elem in result_byte_array: opened_file.write(int(UpperCAmelCase__, 2 ).to_bytes(1, byteorder="""big""" ) ) except OSError: print("""File not accessible""" ) sys.exit() def UpperCAmelCase__ ( UpperCAmelCase__, UpperCAmelCase__ ) -> None: A_ = read_file_binary(UpperCAmelCase__ ) A_ = compress_data(UpperCAmelCase__ ) A_ = add_file_length(UpperCAmelCase__, UpperCAmelCase__ ) write_file_binary(UpperCAmelCase__, UpperCAmelCase__ ) if __name__ == "__main__": compress(sys.argv[1], sys.argv[2])
667
'''simple docstring''' def UpperCAmelCase__ ( UpperCAmelCase__, UpperCAmelCase__ ) -> float: _validate_point(UpperCAmelCase__ ) _validate_point(UpperCAmelCase__ ) if len(UpperCAmelCase__ ) != len(UpperCAmelCase__ ): raise ValueError("""Both points must be in the same n-dimensional space""" ) return float(sum(abs(a - b ) for a, b in zip(UpperCAmelCase__, UpperCAmelCase__ ) ) ) def UpperCAmelCase__ ( UpperCAmelCase__ ) -> None: if point: if isinstance(UpperCAmelCase__, UpperCAmelCase__ ): for item in point: if not isinstance(UpperCAmelCase__, (int, float) ): A_ = ( """Expected a list of numbers as input, found """ F'''{type(UpperCAmelCase__ ).__name__}''' ) raise TypeError(UpperCAmelCase__ ) else: A_ = F'''Expected a list of numbers as input, found {type(UpperCAmelCase__ ).__name__}''' raise TypeError(UpperCAmelCase__ ) else: raise ValueError("""Missing an input""" ) def UpperCAmelCase__ ( UpperCAmelCase__, UpperCAmelCase__ ) -> float: _validate_point(UpperCAmelCase__ ) _validate_point(UpperCAmelCase__ ) if len(UpperCAmelCase__ ) != len(UpperCAmelCase__ ): raise ValueError("""Both points must be in the same n-dimensional space""" ) return float(sum(abs(x - y ) for x, y in zip(UpperCAmelCase__, UpperCAmelCase__ ) ) ) if __name__ == "__main__": import doctest doctest.testmod()
667
1
'''simple docstring''' def UpperCAmelCase__ ( UpperCAmelCase__, UpperCAmelCase__ ) -> float: _validate_point(UpperCAmelCase__ ) _validate_point(UpperCAmelCase__ ) if len(UpperCAmelCase__ ) != len(UpperCAmelCase__ ): raise ValueError("""Both points must be in the same n-dimensional space""" ) return float(sum(abs(a - b ) for a, b in zip(UpperCAmelCase__, UpperCAmelCase__ ) ) ) def UpperCAmelCase__ ( UpperCAmelCase__ ) -> None: if point: if isinstance(UpperCAmelCase__, UpperCAmelCase__ ): for item in point: if not isinstance(UpperCAmelCase__, (int, float) ): A_ = ( """Expected a list of numbers as input, found """ F'''{type(UpperCAmelCase__ ).__name__}''' ) raise TypeError(UpperCAmelCase__ ) else: A_ = F'''Expected a list of numbers as input, found {type(UpperCAmelCase__ ).__name__}''' raise TypeError(UpperCAmelCase__ ) else: raise ValueError("""Missing an input""" ) def UpperCAmelCase__ ( UpperCAmelCase__, UpperCAmelCase__ ) -> float: _validate_point(UpperCAmelCase__ ) _validate_point(UpperCAmelCase__ ) if len(UpperCAmelCase__ ) != len(UpperCAmelCase__ ): raise ValueError("""Both points must be in the same n-dimensional space""" ) return float(sum(abs(x - y ) for x, y in zip(UpperCAmelCase__, UpperCAmelCase__ ) ) ) if __name__ == "__main__": import doctest doctest.testmod()
667
'''simple docstring''' import warnings from ...utils import logging from .image_processing_beit import BeitImageProcessor __lowerCamelCase = logging.get_logger(__name__) class A__ ( _snake_case ): def __init__( self , *UpperCamelCase__ , **UpperCamelCase__ ) -> None: '''simple docstring''' warnings.warn( """The class BeitFeatureExtractor is deprecated and will be removed in version 5 of Transformers. Please""" """ use BeitImageProcessor instead.""" , UpperCamelCase__ , ) super().__init__(*UpperCamelCase__ , **UpperCamelCase__ )
667
1
'''simple docstring''' from typing import TYPE_CHECKING from ....utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available __lowerCamelCase = { '''configuration_mctct''': ['''MCTCT_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''MCTCTConfig'''], '''feature_extraction_mctct''': ['''MCTCTFeatureExtractor'''], '''processing_mctct''': ['''MCTCTProcessor'''], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __lowerCamelCase = [ '''MCTCT_PRETRAINED_MODEL_ARCHIVE_LIST''', '''MCTCTForCTC''', '''MCTCTModel''', '''MCTCTPreTrainedModel''', ] if TYPE_CHECKING: from .configuration_mctct import MCTCT_PRETRAINED_CONFIG_ARCHIVE_MAP, MCTCTConfig from .feature_extraction_mctct import MCTCTFeatureExtractor from .processing_mctct import MCTCTProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_mctct import MCTCT_PRETRAINED_MODEL_ARCHIVE_LIST, MCTCTForCTC, MCTCTModel, MCTCTPreTrainedModel else: import sys __lowerCamelCase = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
667
'''simple docstring''' def UpperCAmelCase__ ( UpperCAmelCase__ ) -> bool: if num < 0: return False A_ = num A_ = 0 while num > 0: A_ = rev_num * 10 + (num % 10) num //= 10 return num_copy == rev_num if __name__ == "__main__": import doctest doctest.testmod()
667
1
'''simple docstring''' from collections import Counter from pathlib import Path from typing import Optional, Tuple import yaml class A__ ( yaml.SafeLoader ): def snake_case_ ( self , UpperCamelCase__ ) -> List[Any]: '''simple docstring''' A_ = [self.constructed_objects[key_node] for key_node, _ in node.value] A_ = [tuple(UpperCamelCase__ ) if isinstance(UpperCamelCase__ , UpperCamelCase__ ) else key for key in keys] A_ = Counter(UpperCamelCase__ ) A_ = [key for key in counter if counter[key] > 1] if duplicate_keys: raise TypeError(f'''Got duplicate yaml keys: {duplicate_keys}''' ) def snake_case_ ( self , UpperCamelCase__ , UpperCamelCase__=False ) -> Tuple: '''simple docstring''' A_ = super().construct_mapping(UpperCamelCase__ , deep=UpperCamelCase__ ) self._check_no_duplicates_on_constructed_node(UpperCamelCase__ ) return mapping def UpperCAmelCase__ ( UpperCAmelCase__ ) -> Tuple[Optional[str], str]: A_ = list(readme_content.splitlines() ) if full_content and full_content[0] == "---" and "---" in full_content[1:]: A_ = full_content[1:].index("""---""" ) + 1 A_ = """\n""".join(full_content[1:sep_idx] ) return yamlblock, "\n".join(full_content[sep_idx + 1 :] ) return None, "\n".join(UpperCAmelCase__ ) class A__ ( _snake_case ): # class attributes lowercase = {"train_eval_index"} # train-eval-index in the YAML metadata @classmethod def snake_case_ ( cls , UpperCamelCase__ ) -> "DatasetMetadata": '''simple docstring''' with open(UpperCamelCase__ , encoding="""utf-8""" ) as readme_file: A_ , A_ = _split_yaml_from_readme(readme_file.read() ) if yaml_string is not None: return cls.from_yaml_string(UpperCamelCase__ ) else: return cls() def snake_case_ ( self , UpperCamelCase__ ) -> List[str]: '''simple docstring''' if path.exists(): with open(UpperCamelCase__ , encoding="""utf-8""" ) as readme_file: A_ = readme_file.read() else: A_ = None A_ = self._to_readme(UpperCamelCase__ ) with open(UpperCamelCase__ , """w""" , encoding="""utf-8""" ) as readme_file: readme_file.write(UpperCamelCase__ ) def snake_case_ ( self , UpperCamelCase__ = None ) -> str: '''simple docstring''' if readme_content is not None: A_ , A_ = _split_yaml_from_readme(UpperCamelCase__ ) A_ = """---\n""" + self.to_yaml_string() + """---\n""" + content else: A_ = """---\n""" + self.to_yaml_string() + """---\n""" return full_content @classmethod def snake_case_ ( cls , UpperCamelCase__ ) -> "DatasetMetadata": '''simple docstring''' A_ = yaml.load(UpperCamelCase__ , Loader=_NoDuplicateSafeLoader ) or {} # Convert the YAML keys to DatasetMetadata fields A_ = { (key.replace("""-""" , """_""" ) if key.replace("""-""" , """_""" ) in cls._FIELDS_WITH_DASHES else key): value for key, value in metadata_dict.items() } return cls(**UpperCamelCase__ ) def snake_case_ ( self ) -> str: '''simple docstring''' return yaml.safe_dump( { (key.replace("""_""" , """-""" ) if key in self._FIELDS_WITH_DASHES else key): value for key, value in self.items() } , sort_keys=UpperCamelCase__ , allow_unicode=UpperCamelCase__ , encoding="""utf-8""" , ).decode("""utf-8""" ) __lowerCamelCase = { '''image-classification''': [], '''translation''': [], '''image-segmentation''': [], '''fill-mask''': [], '''automatic-speech-recognition''': [], '''token-classification''': [], '''sentence-similarity''': [], '''audio-classification''': [], '''question-answering''': [], '''summarization''': [], '''zero-shot-classification''': [], '''table-to-text''': [], '''feature-extraction''': [], '''other''': [], '''multiple-choice''': [], '''text-classification''': [], '''text-to-image''': [], '''text2text-generation''': [], '''zero-shot-image-classification''': [], '''tabular-classification''': [], '''tabular-regression''': [], '''image-to-image''': [], '''tabular-to-text''': [], '''unconditional-image-generation''': [], '''text-retrieval''': [], '''text-to-speech''': [], '''object-detection''': [], '''audio-to-audio''': [], '''text-generation''': [], '''conversational''': [], '''table-question-answering''': [], '''visual-question-answering''': [], '''image-to-text''': [], '''reinforcement-learning''': [], '''voice-activity-detection''': [], '''time-series-forecasting''': [], '''document-question-answering''': [], } if __name__ == "__main__": from argparse import ArgumentParser __lowerCamelCase = ArgumentParser(usage='''Validate the yaml metadata block of a README.md file.''') ap.add_argument('''readme_filepath''') __lowerCamelCase = ap.parse_args() __lowerCamelCase = Path(args.readme_filepath) __lowerCamelCase = DatasetMetadata.from_readme(readme_filepath) print(dataset_metadata) dataset_metadata.to_readme(readme_filepath)
667
'''simple docstring''' __lowerCamelCase = range(2, 20 + 1) __lowerCamelCase = [10**k for k in range(ks[-1] + 1)] __lowerCamelCase = {} def UpperCAmelCase__ ( UpperCAmelCase__, UpperCAmelCase__, UpperCAmelCase__, UpperCAmelCase__ ) -> Tuple: A_ = sum(a_i[j] for j in range(UpperCAmelCase__, len(UpperCAmelCase__ ) ) ) A_ = sum(a_i[j] * base[j] for j in range(min(len(UpperCAmelCase__ ), UpperCAmelCase__ ) ) ) A_ , A_ = 0, 0 A_ = n - i A_ = memo.get(UpperCAmelCase__ ) if sub_memo is not None: A_ = sub_memo.get(UpperCAmelCase__ ) if jumps is not None and len(UpperCAmelCase__ ) > 0: # find and make the largest jump without going over A_ = -1 for _k in range(len(UpperCAmelCase__ ) - 1, -1, -1 ): if jumps[_k][2] <= k and jumps[_k][1] <= max_dn: A_ = _k break if max_jump >= 0: A_ , A_ , A_ = jumps[max_jump] # since the difference between jumps is cached, add c A_ = diff + c for j in range(min(UpperCAmelCase__, len(UpperCAmelCase__ ) ) ): A_ , A_ = divmod(UpperCAmelCase__, 10 ) if new_c > 0: add(UpperCAmelCase__, UpperCAmelCase__, UpperCAmelCase__ ) else: A_ = [] else: A_ = {c: []} A_ = sub_memo if dn >= max_dn or c + diff >= base[k]: return diff, dn if k > ks[0]: while True: # keep doing smaller jumps A_ , A_ = next_term(UpperCAmelCase__, k - 1, i + dn, UpperCAmelCase__ ) diff += _diff dn += terms_jumped if dn >= max_dn or c + diff >= base[k]: break else: # would be too small a jump, just compute sequential terms instead A_ , A_ = compute(UpperCAmelCase__, UpperCAmelCase__, i + dn, UpperCAmelCase__ ) diff += _diff dn += terms_jumped A_ = sub_memo[c] # keep jumps sorted by # of terms skipped A_ = 0 while j < len(UpperCAmelCase__ ): if jumps[j][1] > dn: break j += 1 # cache the jump for this value digitsum(b) and c sub_memo[c].insert(UpperCAmelCase__, (diff, dn, k) ) return (diff, dn) def UpperCAmelCase__ ( UpperCAmelCase__, UpperCAmelCase__, UpperCAmelCase__, UpperCAmelCase__ ) -> int: if i >= n: return 0, i if k > len(UpperCAmelCase__ ): a_i.extend([0 for _ in range(k - len(UpperCAmelCase__ ) )] ) # note: a_i -> b * 10^k + c # ds_b -> digitsum(b) # ds_c -> digitsum(c) A_ = i A_ , A_ , A_ = 0, 0, 0 for j in range(len(UpperCAmelCase__ ) ): if j >= k: ds_b += a_i[j] else: ds_c += a_i[j] while i < n: i += 1 A_ = ds_c + ds_b diff += addend A_ = 0 for j in range(UpperCAmelCase__ ): A_ = a_i[j] + addend A_ , A_ = divmod(UpperCAmelCase__, 10 ) ds_c += a_i[j] if addend > 0: break if addend > 0: add(UpperCAmelCase__, UpperCAmelCase__, UpperCAmelCase__ ) return diff, i - start_i def UpperCAmelCase__ ( UpperCAmelCase__, UpperCAmelCase__, UpperCAmelCase__ ) -> str: for j in range(UpperCAmelCase__, len(UpperCAmelCase__ ) ): A_ = digits[j] + addend if s >= 10: A_ , A_ = divmod(UpperCAmelCase__, 10 ) A_ = addend // 10 + quotient else: A_ = s A_ = addend // 10 if addend == 0: break while addend > 0: A_ , A_ = divmod(UpperCAmelCase__, 10 ) digits.append(UpperCAmelCase__ ) def UpperCAmelCase__ ( UpperCAmelCase__ = 10**15 ) -> int: A_ = [1] A_ = 1 A_ = 0 while True: A_ , A_ = next_term(UpperCAmelCase__, 20, i + dn, UpperCAmelCase__ ) dn += terms_jumped if dn == n - i: break A_ = 0 for j in range(len(UpperCAmelCase__ ) ): a_n += digits[j] * 10**j return a_n if __name__ == "__main__": print(f"""{solution() = }""")
667
1
'''simple docstring''' import argparse import copy def UpperCAmelCase__ ( UpperCAmelCase__ ) -> int: A_ = {} with open(UpperCAmelCase__ ) as f: for line in f: if line.split()[0] not in dict_of_neighbours: A_ = [] _list.append([line.split()[1], line.split()[2]] ) A_ = _list else: dict_of_neighbours[line.split()[0]].append( [line.split()[1], line.split()[2]] ) if line.split()[1] not in dict_of_neighbours: A_ = [] _list.append([line.split()[0], line.split()[2]] ) A_ = _list else: dict_of_neighbours[line.split()[1]].append( [line.split()[0], line.split()[2]] ) return dict_of_neighbours def UpperCAmelCase__ ( UpperCAmelCase__, UpperCAmelCase__ ) -> Tuple: with open(UpperCAmelCase__ ) as f: A_ = f.read(1 ) A_ = start_node A_ = [] A_ = start_node A_ = 0 while visiting not in first_solution: A_ = 1_00_00 for k in dict_of_neighbours[visiting]: if int(k[1] ) < int(UpperCAmelCase__ ) and k[0] not in first_solution: A_ = k[1] A_ = k[0] first_solution.append(UpperCAmelCase__ ) A_ = distance_of_first_solution + int(UpperCAmelCase__ ) A_ = best_node first_solution.append(UpperCAmelCase__ ) A_ = 0 for k in dict_of_neighbours[first_solution[-2]]: if k[0] == start_node: break position += 1 A_ = ( distance_of_first_solution + int(dict_of_neighbours[first_solution[-2]][position][1] ) - 1_00_00 ) return first_solution, distance_of_first_solution def UpperCAmelCase__ ( UpperCAmelCase__, UpperCAmelCase__ ) -> Dict: A_ = [] for n in solution[1:-1]: A_ = solution.index(UpperCAmelCase__ ) for kn in solution[1:-1]: A_ = solution.index(UpperCAmelCase__ ) if n == kn: continue A_ = copy.deepcopy(UpperCAmelCase__ ) A_ = kn A_ = n A_ = 0 for k in _tmp[:-1]: A_ = _tmp[_tmp.index(UpperCAmelCase__ ) + 1] for i in dict_of_neighbours[k]: if i[0] == next_node: A_ = distance + int(i[1] ) _tmp.append(UpperCAmelCase__ ) if _tmp not in neighborhood_of_solution: neighborhood_of_solution.append(_tmp ) A_ = len(neighborhood_of_solution[0] ) - 1 neighborhood_of_solution.sort(key=lambda UpperCAmelCase__ : x[index_of_last_item_in_the_list] ) return neighborhood_of_solution def UpperCAmelCase__ ( UpperCAmelCase__, UpperCAmelCase__, UpperCAmelCase__, UpperCAmelCase__, UpperCAmelCase__ ) -> Optional[Any]: A_ = 1 A_ = first_solution A_ = [] A_ = distance_of_first_solution A_ = solution while count <= iters: A_ = find_neighborhood(UpperCAmelCase__, UpperCAmelCase__ ) A_ = 0 A_ = neighborhood[index_of_best_solution] A_ = len(UpperCAmelCase__ ) - 1 A_ = False while not found: A_ = 0 while i < len(UpperCAmelCase__ ): if best_solution[i] != solution[i]: A_ = best_solution[i] A_ = solution[i] break A_ = i + 1 if [first_exchange_node, second_exchange_node] not in tabu_list and [ second_exchange_node, first_exchange_node, ] not in tabu_list: tabu_list.append([first_exchange_node, second_exchange_node] ) A_ = True A_ = best_solution[:-1] A_ = neighborhood[index_of_best_solution][best_cost_index] if cost < best_cost: A_ = cost A_ = solution else: A_ = index_of_best_solution + 1 A_ = neighborhood[index_of_best_solution] if len(UpperCAmelCase__ ) >= size: tabu_list.pop(0 ) A_ = count + 1 return best_solution_ever, best_cost def UpperCAmelCase__ ( UpperCAmelCase__=None ) -> Dict: A_ = generate_neighbours(args.File ) A_ , A_ = generate_first_solution( args.File, UpperCAmelCase__ ) A_ , A_ = tabu_search( UpperCAmelCase__, UpperCAmelCase__, UpperCAmelCase__, args.Iterations, args.Size, ) print(F'''Best solution: {best_sol}, with total distance: {best_cost}.''' ) if __name__ == "__main__": __lowerCamelCase = argparse.ArgumentParser(description='''Tabu Search''') parser.add_argument( '''-f''', '''--File''', type=str, help='''Path to the file containing the data''', required=True, ) parser.add_argument( '''-i''', '''--Iterations''', type=int, help='''How many iterations the algorithm should perform''', required=True, ) parser.add_argument( '''-s''', '''--Size''', type=int, help='''Size of the tabu list''', required=True ) # Pass the arguments to main method main(parser.parse_args())
667
'''simple docstring''' import tensorflow as tf from ...tf_utils import shape_list class A__ ( tf.keras.layers.Layer ): def __init__( self , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__=1 , UpperCamelCase__=False , **UpperCamelCase__ ) -> Union[str, Any]: '''simple docstring''' super().__init__(**UpperCamelCase__ ) A_ = vocab_size A_ = d_embed A_ = d_proj A_ = cutoffs + [vocab_size] A_ = [0] + self.cutoffs A_ = div_val A_ = self.cutoffs[0] A_ = len(self.cutoffs ) - 1 A_ = self.shortlist_size + self.n_clusters A_ = keep_order A_ = [] A_ = [] def snake_case_ ( self , UpperCamelCase__ ) -> Union[str, Any]: '''simple docstring''' if self.n_clusters > 0: A_ = self.add_weight( shape=(self.n_clusters, self.d_embed) , initializer="""zeros""" , trainable=UpperCamelCase__ , name="""cluster_weight""" ) A_ = self.add_weight( shape=(self.n_clusters,) , initializer="""zeros""" , trainable=UpperCamelCase__ , name="""cluster_bias""" ) if self.div_val == 1: for i in range(len(self.cutoffs ) ): if self.d_proj != self.d_embed: A_ = self.add_weight( shape=(self.d_embed, self.d_proj) , initializer="""zeros""" , trainable=UpperCamelCase__ , name=f'''out_projs_._{i}''' , ) self.out_projs.append(UpperCamelCase__ ) else: self.out_projs.append(UpperCamelCase__ ) A_ = self.add_weight( shape=(self.vocab_size, self.d_embed) , initializer="""zeros""" , trainable=UpperCamelCase__ , name=f'''out_layers_._{i}_._weight''' , ) A_ = self.add_weight( shape=(self.vocab_size,) , initializer="""zeros""" , trainable=UpperCamelCase__ , name=f'''out_layers_._{i}_._bias''' , ) self.out_layers.append((weight, bias) ) else: for i in range(len(self.cutoffs ) ): A_ , A_ = self.cutoff_ends[i], self.cutoff_ends[i + 1] A_ = self.d_embed // (self.div_val**i) A_ = self.add_weight( shape=(d_emb_i, self.d_proj) , initializer="""zeros""" , trainable=UpperCamelCase__ , name=f'''out_projs_._{i}''' ) self.out_projs.append(UpperCamelCase__ ) A_ = self.add_weight( shape=(r_idx - l_idx, d_emb_i) , initializer="""zeros""" , trainable=UpperCamelCase__ , name=f'''out_layers_._{i}_._weight''' , ) A_ = self.add_weight( shape=(r_idx - l_idx,) , initializer="""zeros""" , trainable=UpperCamelCase__ , name=f'''out_layers_._{i}_._bias''' , ) self.out_layers.append((weight, bias) ) super().build(UpperCamelCase__ ) @staticmethod def snake_case_ ( UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__=None ) -> List[Any]: '''simple docstring''' A_ = x if proj is not None: A_ = tf.einsum("""ibd,ed->ibe""" , UpperCamelCase__ , UpperCamelCase__ ) return tf.einsum("""ibd,nd->ibn""" , UpperCamelCase__ , UpperCamelCase__ ) + b @staticmethod def snake_case_ ( UpperCamelCase__ , UpperCamelCase__ ) -> List[str]: '''simple docstring''' A_ = shape_list(UpperCamelCase__ ) A_ = tf.range(lp_size[0] , dtype=target.dtype ) A_ = tf.stack([r, target] , 1 ) return tf.gather_nd(UpperCamelCase__ , UpperCamelCase__ ) def snake_case_ ( self , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__=True , UpperCamelCase__=False ) -> Optional[int]: '''simple docstring''' A_ = 0 if self.n_clusters == 0: A_ = self._logit(UpperCamelCase__ , self.out_layers[0][0] , self.out_layers[0][1] , self.out_projs[0] ) if target is not None: A_ = tf.nn.sparse_softmax_cross_entropy_with_logits(labels=UpperCamelCase__ , logits=UpperCamelCase__ ) A_ = tf.nn.log_softmax(UpperCamelCase__ , axis=-1 ) else: A_ = shape_list(UpperCamelCase__ ) A_ = [] A_ = tf.zeros(hidden_sizes[:2] ) for i in range(len(self.cutoffs ) ): A_ , A_ = self.cutoff_ends[i], self.cutoff_ends[i + 1] if target is not None: A_ = (target >= l_idx) & (target < r_idx) A_ = tf.where(UpperCamelCase__ ) A_ = tf.boolean_mask(UpperCamelCase__ , UpperCamelCase__ ) - l_idx if self.div_val == 1: A_ = self.out_layers[0][0][l_idx:r_idx] A_ = self.out_layers[0][1][l_idx:r_idx] else: A_ = self.out_layers[i][0] A_ = self.out_layers[i][1] if i == 0: A_ = tf.concat([cur_W, self.cluster_weight] , 0 ) A_ = tf.concat([cur_b, self.cluster_bias] , 0 ) A_ = self._logit(UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , self.out_projs[0] ) A_ = tf.nn.log_softmax(UpperCamelCase__ ) out.append(head_logprob[..., : self.cutoffs[0]] ) if target is not None: A_ = tf.boolean_mask(UpperCamelCase__ , UpperCamelCase__ ) A_ = self._gather_logprob(UpperCamelCase__ , UpperCamelCase__ ) else: A_ = self._logit(UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , self.out_projs[i] ) A_ = tf.nn.log_softmax(UpperCamelCase__ ) A_ = self.cutoffs[0] + i - 1 # No probability for the head cluster A_ = head_logprob[..., cluster_prob_idx, None] + tail_logprob out.append(UpperCamelCase__ ) if target is not None: A_ = tf.boolean_mask(UpperCamelCase__ , UpperCamelCase__ ) A_ = tf.boolean_mask(UpperCamelCase__ , UpperCamelCase__ ) A_ = self._gather_logprob(UpperCamelCase__ , UpperCamelCase__ ) cur_logprob += cur_head_logprob[:, self.cutoff_ends[1] + i - 1] if target is not None: loss += tf.scatter_nd(UpperCamelCase__ , -cur_logprob , shape_list(UpperCamelCase__ ) ) A_ = tf.concat(UpperCamelCase__ , axis=-1 ) if target is not None: if return_mean: A_ = tf.reduce_mean(UpperCamelCase__ ) # Add the training-time loss value to the layer using `self.add_loss()`. self.add_loss(UpperCamelCase__ ) # Log the loss as a metric (we could log arbitrary metrics, # including different metrics for training and inference. self.add_metric(UpperCamelCase__ , name=self.name , aggregation="""mean""" if return_mean else """""" ) return out
667
1
'''simple docstring''' import math from collections.abc import Callable def UpperCAmelCase__ ( UpperCAmelCase__, UpperCAmelCase__, UpperCAmelCase__ ) -> float: A_ = xa A_ = xa while True: if x_n == x_na or function(UpperCAmelCase__ ) == function(UpperCAmelCase__ ): raise ZeroDivisionError("""float division by zero, could not find root""" ) A_ = x_na - ( function(UpperCAmelCase__ ) / ((function(UpperCAmelCase__ ) - function(UpperCAmelCase__ )) / (x_na - x_n)) ) if abs(x_na - x_na ) < 10**-5: return x_na A_ = x_na A_ = x_na def UpperCAmelCase__ ( UpperCAmelCase__ ) -> float: return math.pow(UpperCAmelCase__, 3 ) - (2 * x) - 5 if __name__ == "__main__": print(intersection(f, 3, 3.5))
667
'''simple docstring''' from queue import PriorityQueue from typing import Any import numpy as np def UpperCAmelCase__ ( UpperCAmelCase__, UpperCAmelCase__, UpperCAmelCase__, UpperCAmelCase__, UpperCAmelCase__, UpperCAmelCase__, UpperCAmelCase__, UpperCAmelCase__, UpperCAmelCase__, ) -> float | int: for nxt, d in graph[v]: if nxt in visited_forward: continue A_ = cst_fwd.get(UpperCAmelCase__, np.inf ) A_ = cst_fwd[v] + d if new_cost_f < old_cost_f: queue.put((new_cost_f, nxt) ) A_ = new_cost_f A_ = v if nxt in visited_backward: if cst_fwd[v] + d + cst_bwd[nxt] < shortest_distance: A_ = cst_fwd[v] + d + cst_bwd[nxt] return shortest_distance def UpperCAmelCase__ ( UpperCAmelCase__, UpperCAmelCase__, UpperCAmelCase__, UpperCAmelCase__ ) -> int: A_ = -1 A_ = set() A_ = set() A_ = {source: 0} A_ = {destination: 0} A_ = {source: None} A_ = {destination: None} A_ = PriorityQueue() A_ = PriorityQueue() A_ = np.inf queue_forward.put((0, source) ) queue_backward.put((0, destination) ) if source == destination: return 0 while not queue_forward.empty() and not queue_backward.empty(): A_ , A_ = queue_forward.get() visited_forward.add(UpperCAmelCase__ ) A_ , A_ = queue_backward.get() visited_backward.add(UpperCAmelCase__ ) A_ = pass_and_relaxation( UpperCAmelCase__, UpperCAmelCase__, UpperCAmelCase__, UpperCAmelCase__, UpperCAmelCase__, UpperCAmelCase__, UpperCAmelCase__, UpperCAmelCase__, UpperCAmelCase__, ) A_ = pass_and_relaxation( UpperCAmelCase__, UpperCAmelCase__, UpperCAmelCase__, UpperCAmelCase__, UpperCAmelCase__, UpperCAmelCase__, UpperCAmelCase__, UpperCAmelCase__, UpperCAmelCase__, ) if cst_fwd[v_fwd] + cst_bwd[v_bwd] >= shortest_distance: break if shortest_distance != np.inf: A_ = shortest_distance return shortest_path_distance __lowerCamelCase = { '''B''': [['''C''', 1]], '''C''': [['''D''', 1]], '''D''': [['''F''', 1]], '''E''': [['''B''', 1], ['''G''', 2]], '''F''': [], '''G''': [['''F''', 1]], } __lowerCamelCase = { '''B''': [['''E''', 1]], '''C''': [['''B''', 1]], '''D''': [['''C''', 1]], '''F''': [['''D''', 1], ['''G''', 1]], '''E''': [[None, np.inf]], '''G''': [['''E''', 2]], } if __name__ == "__main__": import doctest doctest.testmod()
667
1
'''simple docstring''' from collections import OrderedDict from typing import Mapping from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging __lowerCamelCase = logging.get_logger(__name__) __lowerCamelCase = { '''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 A__ ( _snake_case ): lowercase = "camembert" def __init__( self , UpperCamelCase__=30522 , UpperCamelCase__=768 , UpperCamelCase__=12 , UpperCamelCase__=12 , UpperCamelCase__=3072 , UpperCamelCase__="gelu" , UpperCamelCase__=0.1 , UpperCamelCase__=0.1 , UpperCamelCase__=512 , UpperCamelCase__=2 , UpperCamelCase__=0.02 , UpperCamelCase__=1e-1_2 , UpperCamelCase__=1 , UpperCamelCase__=0 , UpperCamelCase__=2 , UpperCamelCase__="absolute" , UpperCamelCase__=True , UpperCamelCase__=None , **UpperCamelCase__ , ) -> List[str]: '''simple docstring''' super().__init__(pad_token_id=UpperCamelCase__ , bos_token_id=UpperCamelCase__ , eos_token_id=UpperCamelCase__ , **UpperCamelCase__ ) A_ = vocab_size A_ = hidden_size A_ = num_hidden_layers A_ = num_attention_heads A_ = hidden_act A_ = intermediate_size A_ = hidden_dropout_prob A_ = attention_probs_dropout_prob A_ = max_position_embeddings A_ = type_vocab_size A_ = initializer_range A_ = layer_norm_eps A_ = position_embedding_type A_ = use_cache A_ = classifier_dropout class A__ ( _snake_case ): @property def snake_case_ ( self ) -> Mapping[str, Mapping[int, str]]: '''simple docstring''' if self.task == "multiple-choice": A_ = {0: """batch""", 1: """choice""", 2: """sequence"""} else: A_ = {0: """batch""", 1: """sequence"""} return OrderedDict( [ ("""input_ids""", dynamic_axis), ("""attention_mask""", dynamic_axis), ] )
667
'''simple docstring''' import os __lowerCamelCase = {'''I''': 1, '''V''': 5, '''X''': 10, '''L''': 50, '''C''': 100, '''D''': 500, '''M''': 1000} def UpperCAmelCase__ ( UpperCAmelCase__ ) -> int: A_ = 0 A_ = 0 while index < len(UpperCAmelCase__ ) - 1: A_ = SYMBOLS[numerals[index]] A_ = SYMBOLS[numerals[index + 1]] if current_value < next_value: total_value -= current_value else: total_value += current_value index += 1 total_value += SYMBOLS[numerals[index]] return total_value def UpperCAmelCase__ ( UpperCAmelCase__ ) -> str: A_ = """""" A_ = num // 10_00 numerals += m_count * "M" num %= 10_00 A_ = num // 1_00 if c_count == 9: numerals += "CM" c_count -= 9 elif c_count == 4: numerals += "CD" c_count -= 4 if c_count >= 5: numerals += "D" c_count -= 5 numerals += c_count * "C" num %= 1_00 A_ = num // 10 if x_count == 9: numerals += "XC" x_count -= 9 elif x_count == 4: numerals += "XL" x_count -= 4 if x_count >= 5: numerals += "L" x_count -= 5 numerals += x_count * "X" num %= 10 if num == 9: numerals += "IX" num -= 9 elif num == 4: numerals += "IV" num -= 4 if num >= 5: numerals += "V" num -= 5 numerals += num * "I" return numerals def UpperCAmelCase__ ( UpperCAmelCase__ = "/p089_roman.txt" ) -> int: A_ = 0 with open(os.path.dirname(UpperCAmelCase__ ) + roman_numerals_filename ) as filea: A_ = filea.readlines() for line in lines: A_ = line.strip() A_ = parse_roman_numerals(UpperCAmelCase__ ) A_ = generate_roman_numerals(UpperCAmelCase__ ) savings += len(UpperCAmelCase__ ) - len(UpperCAmelCase__ ) return savings if __name__ == "__main__": print(f"""{solution() = }""")
667
1
'''simple docstring''' from __future__ import annotations def UpperCAmelCase__ ( UpperCAmelCase__, UpperCAmelCase__ ) -> list[list[int]]: A_ = [] create_all_state(1, UpperCAmelCase__, UpperCAmelCase__, [], UpperCAmelCase__ ) return result def UpperCAmelCase__ ( UpperCAmelCase__, UpperCAmelCase__, UpperCAmelCase__, UpperCAmelCase__, UpperCAmelCase__, ) -> None: if level == 0: total_list.append(current_list[:] ) return for i in range(UpperCAmelCase__, total_number - level + 2 ): current_list.append(UpperCAmelCase__ ) create_all_state(i + 1, UpperCAmelCase__, level - 1, UpperCAmelCase__, UpperCAmelCase__ ) current_list.pop() def UpperCAmelCase__ ( UpperCAmelCase__ ) -> None: for i in total_list: print(*UpperCAmelCase__ ) if __name__ == "__main__": __lowerCamelCase = 4 __lowerCamelCase = 2 __lowerCamelCase = generate_all_combinations(n, k) print_all_state(total_list)
667
'''simple docstring''' import warnings from diffusers import StableDiffusionImgaImgPipeline # noqa F401 warnings.warn( '''The `image_to_image.py` script is outdated. Please use directly `from diffusers import''' ''' StableDiffusionImg2ImgPipeline` instead.''' )
667
1
'''simple docstring''' from ...processing_utils import ProcessorMixin from ...tokenization_utils_base import BatchEncoding class A__ ( _snake_case ): lowercase = "ClapFeatureExtractor" lowercase = ("RobertaTokenizer", "RobertaTokenizerFast") def __init__( self , UpperCamelCase__ , UpperCamelCase__ ) -> Tuple: '''simple docstring''' super().__init__(UpperCamelCase__ , UpperCamelCase__ ) def __call__( self , UpperCamelCase__=None , UpperCamelCase__=None , UpperCamelCase__=None , **UpperCamelCase__ ) -> Tuple: '''simple docstring''' A_ = kwargs.pop("""sampling_rate""" , UpperCamelCase__ ) if text is None and audios is None: raise ValueError("""You have to specify either text or audios. Both cannot be none.""" ) if text is not None: A_ = self.tokenizer(UpperCamelCase__ , return_tensors=UpperCamelCase__ , **UpperCamelCase__ ) if audios is not None: A_ = self.feature_extractor( UpperCamelCase__ , sampling_rate=UpperCamelCase__ , return_tensors=UpperCamelCase__ , **UpperCamelCase__ ) if text is not None and audios is not None: A_ = audio_features.input_features return encoding elif text is not None: return encoding else: return BatchEncoding(data=dict(**UpperCamelCase__ ) , tensor_type=UpperCamelCase__ ) def snake_case_ ( self , *UpperCamelCase__ , **UpperCamelCase__ ) -> Optional[int]: '''simple docstring''' return self.tokenizer.batch_decode(*UpperCamelCase__ , **UpperCamelCase__ ) def snake_case_ ( self , *UpperCamelCase__ , **UpperCamelCase__ ) -> str: '''simple docstring''' return self.tokenizer.decode(*UpperCamelCase__ , **UpperCamelCase__ ) @property def snake_case_ ( self ) -> int: '''simple docstring''' A_ = self.tokenizer.model_input_names A_ = self.feature_extractor.model_input_names return list(dict.fromkeys(tokenizer_input_names + feature_extractor_input_names ) )
667
'''simple docstring''' import importlib.util import os import platform from argparse import ArgumentParser import huggingface_hub from .. import __version__ as version from ..utils import ( is_accelerate_available, is_flax_available, is_safetensors_available, is_tf_available, is_torch_available, ) from . import BaseTransformersCLICommand def UpperCAmelCase__ ( UpperCAmelCase__ ) -> Tuple: return EnvironmentCommand() def UpperCAmelCase__ ( UpperCAmelCase__ ) -> int: return EnvironmentCommand(args.accelerate_config_file ) class A__ ( _snake_case ): @staticmethod def snake_case_ ( UpperCamelCase__ ) -> Union[str, Any]: '''simple docstring''' A_ = parser.add_parser("""env""" ) download_parser.set_defaults(func=UpperCamelCase__ ) download_parser.add_argument( """--accelerate-config_file""" , default=UpperCamelCase__ , help="""The accelerate config file to use for the default values in the launching script.""" , ) download_parser.set_defaults(func=UpperCamelCase__ ) def __init__( self , UpperCamelCase__ , *UpperCamelCase__ ) -> None: '''simple docstring''' A_ = accelerate_config_file def snake_case_ ( self ) -> List[str]: '''simple docstring''' A_ = """not installed""" if is_safetensors_available(): import safetensors A_ = safetensors.__version__ elif importlib.util.find_spec("""safetensors""" ) is not None: import safetensors A_ = f'''{safetensors.__version__} but is ignored because of PyTorch version too old.''' A_ = """not installed""" A_ = A_ = """not found""" if is_accelerate_available(): import accelerate from accelerate.commands.config import default_config_file, load_config_from_file A_ = accelerate.__version__ # Get the default from the config file. if self._accelerate_config_file is not None or os.path.isfile(UpperCamelCase__ ): A_ = load_config_from_file(self._accelerate_config_file ).to_dict() A_ = ( """\n""".join([f'''\t- {prop}: {val}''' for prop, val in accelerate_config.items()] ) if isinstance(UpperCamelCase__ , UpperCamelCase__ ) else f'''\t{accelerate_config}''' ) A_ = """not installed""" A_ = """NA""" if is_torch_available(): import torch A_ = torch.__version__ A_ = torch.cuda.is_available() A_ = """not installed""" A_ = """NA""" if is_tf_available(): import tensorflow as tf A_ = tf.__version__ try: # deprecated in v2.1 A_ = tf.test.is_gpu_available() except AttributeError: # returns list of devices, convert to bool A_ = bool(tf.config.list_physical_devices("""GPU""" ) ) A_ = """not installed""" A_ = """not installed""" A_ = """not installed""" A_ = """NA""" if is_flax_available(): import flax import jax import jaxlib A_ = flax.__version__ A_ = jax.__version__ A_ = jaxlib.__version__ A_ = jax.lib.xla_bridge.get_backend().platform A_ = { """`transformers` version""": version, """Platform""": platform.platform(), """Python version""": platform.python_version(), """Huggingface_hub version""": huggingface_hub.__version__, """Safetensors version""": f'''{safetensors_version}''', """Accelerate version""": f'''{accelerate_version}''', """Accelerate config""": f'''{accelerate_config_str}''', """PyTorch version (GPU?)""": f'''{pt_version} ({pt_cuda_available})''', """Tensorflow version (GPU?)""": f'''{tf_version} ({tf_cuda_available})''', """Flax version (CPU?/GPU?/TPU?)""": f'''{flax_version} ({jax_backend})''', """Jax version""": f'''{jax_version}''', """JaxLib version""": f'''{jaxlib_version}''', """Using GPU in script?""": """<fill in>""", """Using distributed or parallel set-up in script?""": """<fill in>""", } print("""\nCopy-and-paste the text below in your GitHub issue and FILL OUT the two last points.\n""" ) print(self.format_dict(UpperCamelCase__ ) ) return info @staticmethod def snake_case_ ( UpperCamelCase__ ) -> List[str]: '''simple docstring''' return "\n".join([f'''- {prop}: {val}''' for prop, val in d.items()] ) + "\n"
667
1
'''simple docstring''' def UpperCAmelCase__ ( UpperCAmelCase__ ) -> bool: return credit_card_number.startswith(("""34""", """35""", """37""", """4""", """5""", """6""") ) def UpperCAmelCase__ ( UpperCAmelCase__ ) -> bool: A_ = credit_card_number A_ = 0 A_ = len(UpperCAmelCase__ ) - 2 for i in range(UpperCAmelCase__, -1, -2 ): # double the value of every second digit A_ = 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 A_ = cc_number[:i] + str(UpperCAmelCase__ ) + cc_number[i + 1 :] total += digit # Sum up the remaining digits for i in range(len(UpperCAmelCase__ ) - 1, -1, -2 ): total += int(cc_number[i] ) return total % 10 == 0 def UpperCAmelCase__ ( UpperCAmelCase__ ) -> bool: A_ = 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(UpperCAmelCase__ ) <= 16: print(F'''{error_message} of its length.''' ) return False if not validate_initial_digits(UpperCAmelCase__ ): print(F'''{error_message} of its first two digits.''' ) return False if not luhn_validation(UpperCAmelCase__ ): 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''')
667
'''simple docstring''' import unittest import numpy as np import torch from torch import nn from transformers import ( CLIPImageProcessor, CLIPTextConfig, CLIPTextModelWithProjection, CLIPTokenizer, CLIPVisionConfig, CLIPVisionModelWithProjection, ) from diffusers import KandinskyVaaPriorPipeline, PriorTransformer, UnCLIPScheduler from diffusers.utils import torch_device from diffusers.utils.testing_utils import enable_full_determinism, skip_mps from ..test_pipelines_common import PipelineTesterMixin enable_full_determinism() class A__ ( _snake_case , unittest.TestCase ): lowercase = KandinskyVaaPriorPipeline lowercase = ["prompt"] lowercase = ["prompt", "negative_prompt"] lowercase = [ "num_images_per_prompt", "generator", "num_inference_steps", "latents", "negative_prompt", "guidance_scale", "output_type", "return_dict", ] lowercase = False @property def snake_case_ ( self ) -> Any: '''simple docstring''' return 32 @property def snake_case_ ( self ) -> Optional[int]: '''simple docstring''' return 32 @property def snake_case_ ( self ) -> Optional[Any]: '''simple docstring''' return self.time_input_dim @property def snake_case_ ( self ) -> str: '''simple docstring''' return self.time_input_dim * 4 @property def snake_case_ ( self ) -> int: '''simple docstring''' return 100 @property def snake_case_ ( self ) -> Optional[Any]: '''simple docstring''' A_ = CLIPTokenizer.from_pretrained("""hf-internal-testing/tiny-random-clip""" ) return tokenizer @property def snake_case_ ( self ) -> Union[str, Any]: '''simple docstring''' torch.manual_seed(0 ) A_ = CLIPTextConfig( bos_token_id=0 , eos_token_id=2 , hidden_size=self.text_embedder_hidden_size , projection_dim=self.text_embedder_hidden_size , intermediate_size=37 , layer_norm_eps=1e-0_5 , num_attention_heads=4 , num_hidden_layers=5 , pad_token_id=1 , vocab_size=1000 , ) return CLIPTextModelWithProjection(UpperCamelCase__ ) @property def snake_case_ ( self ) -> Tuple: '''simple docstring''' torch.manual_seed(0 ) A_ = { """num_attention_heads""": 2, """attention_head_dim""": 12, """embedding_dim""": self.text_embedder_hidden_size, """num_layers""": 1, } A_ = PriorTransformer(**UpperCamelCase__ ) # clip_std and clip_mean is initialized to be 0 so PriorTransformer.post_process_latents will always return 0 - set clip_std to be 1 so it won't return 0 A_ = nn.Parameter(torch.ones(model.clip_std.shape ) ) return model @property def snake_case_ ( self ) -> str: '''simple docstring''' torch.manual_seed(0 ) A_ = CLIPVisionConfig( hidden_size=self.text_embedder_hidden_size , image_size=224 , projection_dim=self.text_embedder_hidden_size , intermediate_size=37 , num_attention_heads=4 , num_channels=3 , num_hidden_layers=5 , patch_size=14 , ) A_ = CLIPVisionModelWithProjection(UpperCamelCase__ ) return model @property def snake_case_ ( self ) -> str: '''simple docstring''' A_ = CLIPImageProcessor( crop_size=224 , do_center_crop=UpperCamelCase__ , do_normalize=UpperCamelCase__ , do_resize=UpperCamelCase__ , image_mean=[0.48145466, 0.4578275, 0.40821073] , image_std=[0.26862954, 0.26130258, 0.27577711] , resample=3 , size=224 , ) return image_processor def snake_case_ ( self ) -> Union[str, Any]: '''simple docstring''' A_ = self.dummy_prior A_ = self.dummy_image_encoder A_ = self.dummy_text_encoder A_ = self.dummy_tokenizer A_ = self.dummy_image_processor A_ = UnCLIPScheduler( variance_type="""fixed_small_log""" , prediction_type="""sample""" , num_train_timesteps=1000 , clip_sample=UpperCamelCase__ , clip_sample_range=10.0 , ) A_ = { """prior""": prior, """image_encoder""": image_encoder, """text_encoder""": text_encoder, """tokenizer""": tokenizer, """scheduler""": scheduler, """image_processor""": image_processor, } return components def snake_case_ ( self , UpperCamelCase__ , UpperCamelCase__=0 ) -> Optional[int]: '''simple docstring''' if str(UpperCamelCase__ ).startswith("""mps""" ): A_ = torch.manual_seed(UpperCamelCase__ ) else: A_ = torch.Generator(device=UpperCamelCase__ ).manual_seed(UpperCamelCase__ ) A_ = { """prompt""": """horse""", """generator""": generator, """guidance_scale""": 4.0, """num_inference_steps""": 2, """output_type""": """np""", } return inputs def snake_case_ ( self ) -> List[str]: '''simple docstring''' A_ = """cpu""" A_ = self.get_dummy_components() A_ = self.pipeline_class(**UpperCamelCase__ ) A_ = pipe.to(UpperCamelCase__ ) pipe.set_progress_bar_config(disable=UpperCamelCase__ ) A_ = pipe(**self.get_dummy_inputs(UpperCamelCase__ ) ) A_ = output.image_embeds A_ = pipe( **self.get_dummy_inputs(UpperCamelCase__ ) , return_dict=UpperCamelCase__ , )[0] A_ = image[0, -10:] A_ = image_from_tuple[0, -10:] assert image.shape == (1, 32) A_ = np.array( [-0.0532, 1.7120, 0.3656, -1.0852, -0.8946, -1.1756, 0.4348, 0.2482, 0.5146, -0.1156] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-2 assert np.abs(image_from_tuple_slice.flatten() - expected_slice ).max() < 1e-2 @skip_mps def snake_case_ ( self ) -> int: '''simple docstring''' A_ = torch_device == """cpu""" A_ = True A_ = False self._test_inference_batch_single_identical( test_max_difference=UpperCamelCase__ , relax_max_difference=UpperCamelCase__ , test_mean_pixel_difference=UpperCamelCase__ , ) @skip_mps def snake_case_ ( self ) -> Union[str, Any]: '''simple docstring''' A_ = torch_device == """cpu""" A_ = False self._test_attention_slicing_forward_pass( test_max_difference=UpperCamelCase__ , test_mean_pixel_difference=UpperCamelCase__ , )
667
1
'''simple docstring''' import math def UpperCAmelCase__ ( UpperCAmelCase__, UpperCAmelCase__ ) -> float: if initial_intensity < 0: raise ValueError("""The value of intensity cannot be negative""" ) # handling of negative values of initial intensity if angle < 0 or angle > 3_60: raise ValueError("""In Malus Law, the angle is in the range 0-360 degrees""" ) # handling of values out of allowed range return initial_intensity * (math.cos(math.radians(UpperCAmelCase__ ) ) ** 2) if __name__ == "__main__": import doctest doctest.testmod(name='''malus_law''')
667
'''simple docstring''' import tempfile import torch from diffusers import IPNDMScheduler from .test_schedulers import SchedulerCommonTest class A__ ( _snake_case ): lowercase = (IPNDMScheduler,) lowercase = (("num_inference_steps", 50),) def snake_case_ ( self , **UpperCamelCase__ ) -> Optional[Any]: '''simple docstring''' A_ = {"""num_train_timesteps""": 1000} config.update(**UpperCamelCase__ ) return config def snake_case_ ( self , UpperCamelCase__=0 , **UpperCamelCase__ ) -> str: '''simple docstring''' A_ = dict(self.forward_default_kwargs ) A_ = kwargs.pop("""num_inference_steps""" , UpperCamelCase__ ) A_ = self.dummy_sample A_ = 0.1 * sample A_ = [residual + 0.2, residual + 0.15, residual + 0.1, residual + 0.05] for scheduler_class in self.scheduler_classes: A_ = self.get_scheduler_config(**UpperCamelCase__ ) A_ = scheduler_class(**UpperCamelCase__ ) scheduler.set_timesteps(UpperCamelCase__ ) # copy over dummy past residuals A_ = dummy_past_residuals[:] if time_step is None: A_ = scheduler.timesteps[len(scheduler.timesteps ) // 2] with tempfile.TemporaryDirectory() as tmpdirname: scheduler.save_config(UpperCamelCase__ ) A_ = scheduler_class.from_pretrained(UpperCamelCase__ ) new_scheduler.set_timesteps(UpperCamelCase__ ) # copy over dummy past residuals A_ = dummy_past_residuals[:] A_ = scheduler.step(UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , **UpperCamelCase__ ).prev_sample A_ = new_scheduler.step(UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , **UpperCamelCase__ ).prev_sample assert torch.sum(torch.abs(output - new_output ) ) < 1e-5, "Scheduler outputs are not identical" A_ = scheduler.step(UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , **UpperCamelCase__ ).prev_sample A_ = new_scheduler.step(UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , **UpperCamelCase__ ).prev_sample assert torch.sum(torch.abs(output - new_output ) ) < 1e-5, "Scheduler outputs are not identical" def snake_case_ ( self ) -> Optional[int]: '''simple docstring''' pass def snake_case_ ( self , UpperCamelCase__=0 , **UpperCamelCase__ ) -> str: '''simple docstring''' A_ = dict(self.forward_default_kwargs ) A_ = kwargs.pop("""num_inference_steps""" , UpperCamelCase__ ) A_ = self.dummy_sample A_ = 0.1 * sample A_ = [residual + 0.2, residual + 0.15, residual + 0.1, residual + 0.05] for scheduler_class in self.scheduler_classes: A_ = self.get_scheduler_config() A_ = scheduler_class(**UpperCamelCase__ ) scheduler.set_timesteps(UpperCamelCase__ ) # copy over dummy past residuals (must be after setting timesteps) A_ = dummy_past_residuals[:] if time_step is None: A_ = scheduler.timesteps[len(scheduler.timesteps ) // 2] with tempfile.TemporaryDirectory() as tmpdirname: scheduler.save_config(UpperCamelCase__ ) A_ = scheduler_class.from_pretrained(UpperCamelCase__ ) # copy over dummy past residuals new_scheduler.set_timesteps(UpperCamelCase__ ) # copy over dummy past residual (must be after setting timesteps) A_ = dummy_past_residuals[:] A_ = scheduler.step(UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , **UpperCamelCase__ ).prev_sample A_ = new_scheduler.step(UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , **UpperCamelCase__ ).prev_sample assert torch.sum(torch.abs(output - new_output ) ) < 1e-5, "Scheduler outputs are not identical" A_ = scheduler.step(UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , **UpperCamelCase__ ).prev_sample A_ = new_scheduler.step(UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , **UpperCamelCase__ ).prev_sample assert torch.sum(torch.abs(output - new_output ) ) < 1e-5, "Scheduler outputs are not identical" def snake_case_ ( self , **UpperCamelCase__ ) -> Tuple: '''simple docstring''' A_ = self.scheduler_classes[0] A_ = self.get_scheduler_config(**UpperCamelCase__ ) A_ = scheduler_class(**UpperCamelCase__ ) A_ = 10 A_ = self.dummy_model() A_ = self.dummy_sample_deter scheduler.set_timesteps(UpperCamelCase__ ) for i, t in enumerate(scheduler.timesteps ): A_ = model(UpperCamelCase__ , UpperCamelCase__ ) A_ = scheduler.step(UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ ).prev_sample for i, t in enumerate(scheduler.timesteps ): A_ = model(UpperCamelCase__ , UpperCamelCase__ ) A_ = scheduler.step(UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ ).prev_sample return sample def snake_case_ ( self ) -> Union[str, Any]: '''simple docstring''' A_ = dict(self.forward_default_kwargs ) A_ = kwargs.pop("""num_inference_steps""" , UpperCamelCase__ ) for scheduler_class in self.scheduler_classes: A_ = self.get_scheduler_config() A_ = scheduler_class(**UpperCamelCase__ ) A_ = self.dummy_sample A_ = 0.1 * sample if num_inference_steps is not None and hasattr(UpperCamelCase__ , """set_timesteps""" ): scheduler.set_timesteps(UpperCamelCase__ ) elif num_inference_steps is not None and not hasattr(UpperCamelCase__ , """set_timesteps""" ): A_ = num_inference_steps # copy over dummy past residuals (must be done after set_timesteps) A_ = [residual + 0.2, residual + 0.15, residual + 0.1, residual + 0.05] A_ = dummy_past_residuals[:] A_ = scheduler.timesteps[5] A_ = scheduler.timesteps[6] A_ = scheduler.step(UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , **UpperCamelCase__ ).prev_sample A_ = scheduler.step(UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , **UpperCamelCase__ ).prev_sample self.assertEqual(output_a.shape , sample.shape ) self.assertEqual(output_a.shape , output_a.shape ) A_ = scheduler.step(UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , **UpperCamelCase__ ).prev_sample A_ = scheduler.step(UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , **UpperCamelCase__ ).prev_sample self.assertEqual(output_a.shape , sample.shape ) self.assertEqual(output_a.shape , output_a.shape ) def snake_case_ ( self ) -> Any: '''simple docstring''' for timesteps in [100, 1000]: self.check_over_configs(num_train_timesteps=UpperCamelCase__ , time_step=UpperCamelCase__ ) def snake_case_ ( self ) -> Any: '''simple docstring''' for t, num_inference_steps in zip([1, 5, 10] , [10, 50, 100] ): self.check_over_forward(num_inference_steps=UpperCamelCase__ , time_step=UpperCamelCase__ ) def snake_case_ ( self ) -> Dict: '''simple docstring''' A_ = self.full_loop() A_ = torch.mean(torch.abs(UpperCamelCase__ ) ) assert abs(result_mean.item() - 2540529 ) < 10
667
1
'''simple docstring''' import argparse import requests import torch from PIL import Image from transformers import ViTMAEConfig, ViTMAEForPreTraining, ViTMAEImageProcessor def UpperCAmelCase__ ( UpperCAmelCase__ ) -> Any: if "cls_token" in name: A_ = name.replace("""cls_token""", """vit.embeddings.cls_token""" ) if "mask_token" in name: A_ = name.replace("""mask_token""", """decoder.mask_token""" ) if "decoder_pos_embed" in name: A_ = name.replace("""decoder_pos_embed""", """decoder.decoder_pos_embed""" ) if "pos_embed" in name and "decoder" not in name: A_ = name.replace("""pos_embed""", """vit.embeddings.position_embeddings""" ) if "patch_embed.proj" in name: A_ = name.replace("""patch_embed.proj""", """vit.embeddings.patch_embeddings.projection""" ) if "patch_embed.norm" in name: A_ = name.replace("""patch_embed.norm""", """vit.embeddings.norm""" ) if "decoder_blocks" in name: A_ = name.replace("""decoder_blocks""", """decoder.decoder_layers""" ) if "blocks" in name: A_ = name.replace("""blocks""", """vit.encoder.layer""" ) if "attn.proj" in name: A_ = name.replace("""attn.proj""", """attention.output.dense""" ) if "attn" in name: A_ = name.replace("""attn""", """attention.self""" ) if "norm1" in name: A_ = name.replace("""norm1""", """layernorm_before""" ) if "norm2" in name: A_ = name.replace("""norm2""", """layernorm_after""" ) if "mlp.fc1" in name: A_ = name.replace("""mlp.fc1""", """intermediate.dense""" ) if "mlp.fc2" in name: A_ = name.replace("""mlp.fc2""", """output.dense""" ) if "decoder_embed" in name: A_ = name.replace("""decoder_embed""", """decoder.decoder_embed""" ) if "decoder_norm" in name: A_ = name.replace("""decoder_norm""", """decoder.decoder_norm""" ) if "decoder_pred" in name: A_ = name.replace("""decoder_pred""", """decoder.decoder_pred""" ) if "norm.weight" in name and "decoder" not in name: A_ = name.replace("""norm.weight""", """vit.layernorm.weight""" ) if "norm.bias" in name and "decoder" not in name: A_ = name.replace("""norm.bias""", """vit.layernorm.bias""" ) return name def UpperCAmelCase__ ( UpperCAmelCase__, UpperCAmelCase__ ) -> Union[str, Any]: for key in orig_state_dict.copy().keys(): A_ = orig_state_dict.pop(UpperCAmelCase__ ) if "qkv" in key: A_ = key.split(""".""" ) A_ = int(key_split[1] ) if "decoder_blocks" in key: A_ = config.decoder_hidden_size A_ = """decoder.decoder_layers.""" if "weight" in key: A_ = val[:dim, :] A_ = val[dim : dim * 2, :] A_ = val[-dim:, :] elif "bias" in key: A_ = val[:dim] A_ = val[dim : dim * 2] A_ = val[-dim:] else: A_ = config.hidden_size A_ = """vit.encoder.layer.""" if "weight" in key: A_ = val[:dim, :] A_ = val[dim : dim * 2, :] A_ = val[-dim:, :] elif "bias" in key: A_ = val[:dim] A_ = val[dim : dim * 2] A_ = val[-dim:] else: A_ = val return orig_state_dict def UpperCAmelCase__ ( UpperCAmelCase__, UpperCAmelCase__ ) -> Optional[Any]: A_ = ViTMAEConfig() if "large" in checkpoint_url: A_ = 10_24 A_ = 40_96 A_ = 24 A_ = 16 elif "huge" in checkpoint_url: A_ = 14 A_ = 12_80 A_ = 51_20 A_ = 32 A_ = 16 A_ = ViTMAEForPreTraining(UpperCAmelCase__ ) A_ = torch.hub.load_state_dict_from_url(UpperCAmelCase__, map_location="""cpu""" )["""model"""] A_ = ViTMAEImageProcessor(size=config.image_size ) A_ = convert_state_dict(UpperCAmelCase__, UpperCAmelCase__ ) model.load_state_dict(UpperCAmelCase__ ) model.eval() A_ = """https://user-images.githubusercontent.com/11435359/147738734-196fd92f-9260-48d5-ba7e-bf103d29364d.jpg""" A_ = Image.open(requests.get(UpperCAmelCase__, stream=UpperCAmelCase__ ).raw ) A_ = ViTMAEImageProcessor(size=config.image_size ) A_ = image_processor(images=UpperCAmelCase__, return_tensors="""pt""" ) # forward pass torch.manual_seed(2 ) A_ = model(**UpperCAmelCase__ ) A_ = outputs.logits if "large" in checkpoint_url: A_ = torch.tensor( [[-0.7_309, -0.7_128, -1.0_169], [-1.0_161, -0.9_058, -1.1_878], [-1.0_478, -0.9_411, -1.1_911]] ) elif "huge" in checkpoint_url: A_ = torch.tensor( [[-1.1_599, -0.9_199, -1.2_221], [-1.1_952, -0.9_269, -1.2_307], [-1.2_143, -0.9_337, -1.2_262]] ) else: A_ = torch.tensor( [[-0.9_192, -0.8_481, -1.1_259], [-1.1_349, -1.0_034, -1.2_599], [-1.1_757, -1.0_429, -1.2_726]] ) # verify logits assert torch.allclose(logits[0, :3, :3], UpperCAmelCase__, atol=1e-4 ) print(F'''Saving model to {pytorch_dump_folder_path}''' ) model.save_pretrained(UpperCAmelCase__ ) print(F'''Saving image processor to {pytorch_dump_folder_path}''' ) image_processor.save_pretrained(UpperCAmelCase__ ) if __name__ == "__main__": __lowerCamelCase = argparse.ArgumentParser() # Required parameters parser.add_argument( '''--checkpoint_url''', default='''https://dl.fbaipublicfiles.com/mae/visualize/mae_visualize_vit_base.pth''', type=str, help='''URL of the checkpoint you\'d like to convert.''', ) parser.add_argument( '''--pytorch_dump_folder_path''', default=None, type=str, help='''Path to the output PyTorch model directory.''' ) __lowerCamelCase = parser.parse_args() convert_vit_mae_checkpoint(args.checkpoint_url, args.pytorch_dump_folder_path)
667
'''simple docstring''' import argparse import re from flax.traverse_util import flatten_dict, unflatten_dict from tax import checkpoints from transformers import SwitchTransformersConfig, SwitchTransformersForConditionalGeneration from transformers.modeling_flax_pytorch_utils import load_flax_weights_in_pytorch_model from transformers.utils import logging logging.set_verbosity_info() # should not include what is already done by the `from_pt` argument __lowerCamelCase = { '''/attention/''': '''/0/SelfAttention/''', '''/self_attention/''': '''/0/SelfAttention/''', '''/encoder_decoder_attention/''': '''/1/EncDecAttention/''', '''value''': '''v''', '''query''': '''q''', '''key''': '''k''', '''out''': '''o''', '''pre_self_attention_layer_norm''': '''0/layer_norm''', '''pre_cross_attention_layer_norm''': '''1/layer_norm''', '''pre_attention_layer_norm''': '''0/layer_norm''', # previously 1, but seems wrong '''token_embedder''': '''shared''', '''encoder_norm''': '''final_layer_norm''', '''decoder_norm''': '''final_layer_norm''', '''relpos_bias/rel_embedding''': '''block/0/layer/0/SelfAttention/relative_attention_bias/weight''', '''router/router_weights/w/''': '''router/classifier/''', '''roer/roer_weights/w/''': '''router/classifier/''', '''logits_dense''': '''lm_head''', } def UpperCAmelCase__ ( UpperCAmelCase__ ) -> Tuple: # 1. in HF T5, we have block.{x}.layer.{y}. which corresponds to layer.{x} in # the original model A_ = list(s_dict.keys() ) for key in keys: A_ = r""".*/layers_(\d+)""" A_ = key if re.match(UpperCAmelCase__, UpperCAmelCase__ ): A_ = re.sub(r"""layers_(\d+)""", r"""block/\1/layer""", UpperCAmelCase__ ) A_ = r"""(encoder|decoder)\/""" if re.match(UpperCAmelCase__, UpperCAmelCase__ ): A_ = re.match(UpperCAmelCase__, UpperCAmelCase__ ).groups() if groups[0] == "encoder": A_ = re.sub(r"""/mlp/""", r"""/1/mlp/""", UpperCAmelCase__ ) A_ = re.sub(r"""/pre_mlp_layer_norm/""", r"""/1/layer_norm/""", UpperCAmelCase__ ) elif groups[0] == "decoder": A_ = re.sub(r"""/mlp/""", r"""/2/mlp/""", UpperCAmelCase__ ) A_ = re.sub(r"""/pre_mlp_layer_norm/""", r"""/2/layer_norm/""", UpperCAmelCase__ ) # 2. Convert other classic mappings for old_key, temp_key in MOE_LAYER_NAME_MAPPING.items(): if old_key in new_key: A_ = new_key.replace(UpperCAmelCase__, UpperCAmelCase__ ) print(F'''{key} -> {new_key}''' ) A_ = s_dict.pop(UpperCAmelCase__ ) if "encoder/block/0/layer/0/SelfAttention/relative_attention_bias/weight" in s_dict: A_ = s_dict[ """encoder/block/0/layer/0/SelfAttention/relative_attention_bias/weight""" ].T if "decoder/block/0/layer/0/SelfAttention/relative_attention_bias/weight" in s_dict: A_ = s_dict[ """decoder/block/0/layer/0/SelfAttention/relative_attention_bias/weight""" ].T # 3. Take extra care of the EXPERTS layer for key in list(s_dict.keys() ): if "expert" in key: A_ = s_dict[key].shape[0] A_ = s_dict[key] for idx in range(UpperCAmelCase__ ): A_ = expert_weihts[idx] print(F'''{key} -> {key.replace("expert/", "nested fstring" )}''' ) s_dict.pop(UpperCAmelCase__ ) return s_dict __lowerCamelCase = { '''NUM_ENCODER_LAYERS''': '''num_layers''', '''NUM_DECODER_LAYERS''': '''num_decoder_layers''', '''NUM_HEADS''': '''num_heads''', '''HEAD_DIM''': '''d_kv''', '''EMBED_DIM''': '''d_model''', '''MLP_DIM''': '''d_ff''', '''NUM_SELECTED_EXPERTS''': '''num_selected_experts''', '''NUM_ENCODER_SPARSE_LAYERS''': '''num_sparse_encoder_layers''', '''NUM_DECODER_SPARSE_LAYERS''': '''num_sparse_decoder_layers''', '''dense.MlpBlock.activations''': '''feed_forward_proj''', } def UpperCAmelCase__ ( UpperCAmelCase__, UpperCAmelCase__ ) -> List[Any]: # Convert a google style config to the hugging face fromat import regex as re with open(UpperCAmelCase__, """r""" ) as f: A_ = f.read() A_ = re.findall(r"""(.*) = ([0-9.]*)""", UpperCAmelCase__ ) A_ = {} for param, value in regex_match: if param in GIN_TO_CONFIG_MAPPING and value != "": A_ = float(UpperCAmelCase__ ) if """.""" in value else int(UpperCAmelCase__ ) A_ = re.findall(r"""(.*activations) = \(\'(.*)\',\)""", UpperCAmelCase__ )[0] A_ = str(activation[1] ) A_ = num_experts A_ = SwitchTransformersConfig(**UpperCAmelCase__ ) return config def UpperCAmelCase__ ( UpperCAmelCase__, UpperCAmelCase__, UpperCAmelCase__=None, UpperCAmelCase__="./", UpperCAmelCase__=8 ) -> List[str]: # Initialise PyTorch model print(F'''Loading flax weights from : {flax_checkpoint_path}''' ) A_ = checkpoints.load_tax_checkpoint(UpperCAmelCase__ ) if gin_file is not None: A_ = convert_gin_to_config(UpperCAmelCase__, UpperCAmelCase__ ) else: A_ = SwitchTransformersConfig.from_pretrained(UpperCAmelCase__ ) A_ = SwitchTransformersForConditionalGeneration(UpperCAmelCase__ ) A_ = flax_params["""target"""] A_ = flatten_dict(UpperCAmelCase__, sep="""/""" ) A_ = rename_keys(UpperCAmelCase__ ) A_ = unflatten_dict(UpperCAmelCase__, sep="""/""" ) # Load the flax params in the PT model load_flax_weights_in_pytorch_model(UpperCAmelCase__, UpperCAmelCase__ ) print(F'''Save PyTorch model to {pytorch_dump_path}''' ) pt_model.save_pretrained(UpperCAmelCase__ ) if __name__ == "__main__": __lowerCamelCase = argparse.ArgumentParser() # Required parameters parser.add_argument( '''--switch_t5x_checkpoint_path''', default=None, type=str, required=True, help=( '''The config json file corresponding to the pre-trained SwitchTransformers model. \nThis specifies the''' ''' model architecture. If not provided, a `gin_file` has to be provided.''' ), ) parser.add_argument( '''--gin_file''', default=None, type=str, required=False, help='''Path to the gin config file. If not provided, a `config_file` has to be passed ''', ) parser.add_argument( '''--config_name''', default=None, type=str, required=False, help='''Config name of SwitchTransformers model.''' ) parser.add_argument( '''--pytorch_dump_folder_path''', default=None, type=str, required=True, help='''Path to the output pytorch model.''' ) parser.add_argument('''--num_experts''', default=8, type=int, required=False, help='''Number of experts''') __lowerCamelCase = parser.parse_args() convert_flax_checkpoint_to_pytorch( args.switch_tax_checkpoint_path, args.config_name, args.gin_file, args.pytorch_dump_folder_path, args.num_experts, )
667
1
'''simple docstring''' import math import tensorflow as tf from packaging import version def UpperCAmelCase__ ( UpperCAmelCase__ ) -> Dict: A_ = tf.convert_to_tensor(UpperCAmelCase__ ) A_ = 0.5 * (1.0 + tf.math.erf(x / tf.cast(tf.sqrt(2.0 ), x.dtype ) )) return x * cdf def UpperCAmelCase__ ( UpperCAmelCase__ ) -> Tuple: A_ = tf.convert_to_tensor(UpperCAmelCase__ ) A_ = tf.cast(math.pi, x.dtype ) A_ = tf.cast(0.044_715, x.dtype ) A_ = 0.5 * (1.0 + tf.tanh(tf.sqrt(2.0 / pi ) * (x + coeff * tf.pow(UpperCAmelCase__, 3 )) )) return x * cdf def UpperCAmelCase__ ( UpperCAmelCase__ ) -> List[Any]: A_ = tf.convert_to_tensor(UpperCAmelCase__ ) return x * tf.tanh(tf.math.softplus(UpperCAmelCase__ ) ) def UpperCAmelCase__ ( UpperCAmelCase__ ) -> List[Any]: A_ = tf.convert_to_tensor(UpperCAmelCase__ ) A_ = tf.cast(0.044_715, x.dtype ) A_ = tf.cast(0.7_978_845_608, x.dtype ) return 0.5 * x * (1.0 + tf.tanh(x * coeffa * (1.0 + coeffa * x * x) )) def UpperCAmelCase__ ( UpperCAmelCase__ ) -> Dict: A_ = tf.convert_to_tensor(UpperCAmelCase__ ) A_ = tf.cast(1.702, x.dtype ) return x * tf.math.sigmoid(coeff * x ) def UpperCAmelCase__ ( UpperCAmelCase__ ) -> Optional[Any]: return tf.clip_by_value(_gelu(UpperCAmelCase__ ), -10, 10 ) def UpperCAmelCase__ ( UpperCAmelCase__, UpperCAmelCase__=-1 ) -> Optional[int]: A_ , A_ = tf.split(UpperCAmelCase__, 2, axis=UpperCAmelCase__ ) return a * tf.math.sigmoid(UpperCAmelCase__ ) if version.parse(tf.version.VERSION) >= version.parse('''2.4'''): def UpperCAmelCase__ ( UpperCAmelCase__ ) -> Tuple: return tf.keras.activations.gelu(UpperCAmelCase__, approximate=UpperCAmelCase__ ) __lowerCamelCase = tf.keras.activations.gelu __lowerCamelCase = approximate_gelu_wrap else: __lowerCamelCase = _gelu __lowerCamelCase = _gelu_new __lowerCamelCase = { '''gelu''': gelu, '''gelu_10''': gelu_aa, '''gelu_fast''': gelu_fast, '''gelu_new''': gelu_new, '''glu''': glu, '''mish''': mish, '''quick_gelu''': quick_gelu, '''relu''': tf.keras.activations.relu, '''sigmoid''': tf.keras.activations.sigmoid, '''silu''': tf.keras.activations.swish, '''swish''': tf.keras.activations.swish, '''tanh''': tf.keras.activations.tanh, } def UpperCAmelCase__ ( UpperCAmelCase__ ) -> Optional[int]: if activation_string in ACTaFN: return ACTaFN[activation_string] else: raise KeyError(F'''function {activation_string} not found in ACT2FN mapping {list(ACTaFN.keys() )}''' )
667
'''simple docstring''' def UpperCAmelCase__ ( UpperCAmelCase__ ) -> int: assert ( isinstance(UpperCAmelCase__, UpperCAmelCase__ ) and number_of_steps > 0 ), F'''number_of_steps needs to be positive integer, your input {number_of_steps}''' if number_of_steps == 1: return 1 A_ , A_ = 1, 1 for _ in range(number_of_steps - 1 ): A_ , A_ = current + previous, current return current if __name__ == "__main__": import doctest doctest.testmod()
667
1
'''simple docstring''' import os import warnings from typing import List, Optional from ...tokenization_utils_base import BatchEncoding from ...utils import logging from .configuration_rag import RagConfig __lowerCamelCase = logging.get_logger(__name__) class A__ : def __init__( self , UpperCamelCase__ , UpperCamelCase__ ) -> Optional[int]: '''simple docstring''' A_ = question_encoder A_ = generator A_ = self.question_encoder def snake_case_ ( self , UpperCamelCase__ ) -> Any: '''simple docstring''' if os.path.isfile(UpperCamelCase__ ): raise ValueError(f'''Provided path ({save_directory}) should be a directory, not a file''' ) os.makedirs(UpperCamelCase__ , exist_ok=UpperCamelCase__ ) A_ = os.path.join(UpperCamelCase__ , """question_encoder_tokenizer""" ) A_ = os.path.join(UpperCamelCase__ , """generator_tokenizer""" ) self.question_encoder.save_pretrained(UpperCamelCase__ ) self.generator.save_pretrained(UpperCamelCase__ ) @classmethod def snake_case_ ( cls , UpperCamelCase__ , **UpperCamelCase__ ) -> Tuple: '''simple docstring''' # dynamically import AutoTokenizer from ..auto.tokenization_auto import AutoTokenizer A_ = kwargs.pop("""config""" , UpperCamelCase__ ) if config is None: A_ = RagConfig.from_pretrained(UpperCamelCase__ ) A_ = AutoTokenizer.from_pretrained( UpperCamelCase__ , config=config.question_encoder , subfolder="""question_encoder_tokenizer""" ) A_ = AutoTokenizer.from_pretrained( UpperCamelCase__ , config=config.generator , subfolder="""generator_tokenizer""" ) return cls(question_encoder=UpperCamelCase__ , generator=UpperCamelCase__ ) def __call__( self , *UpperCamelCase__ , **UpperCamelCase__ ) -> List[Any]: '''simple docstring''' return self.current_tokenizer(*UpperCamelCase__ , **UpperCamelCase__ ) def snake_case_ ( self , *UpperCamelCase__ , **UpperCamelCase__ ) -> List[Any]: '''simple docstring''' return self.generator.batch_decode(*UpperCamelCase__ , **UpperCamelCase__ ) def snake_case_ ( self , *UpperCamelCase__ , **UpperCamelCase__ ) -> Any: '''simple docstring''' return self.generator.decode(*UpperCamelCase__ , **UpperCamelCase__ ) def snake_case_ ( self ) -> str: '''simple docstring''' A_ = self.question_encoder def snake_case_ ( self ) -> Union[str, Any]: '''simple docstring''' A_ = self.generator def snake_case_ ( self , UpperCamelCase__ , UpperCamelCase__ = None , UpperCamelCase__ = None , UpperCamelCase__ = None , UpperCamelCase__ = "longest" , UpperCamelCase__ = None , UpperCamelCase__ = True , **UpperCamelCase__ , ) -> BatchEncoding: '''simple docstring''' warnings.warn( """`prepare_seq2seq_batch` is deprecated and will be removed in version 5 of ๐Ÿค— Transformers. Use the """ """regular `__call__` method to prepare your inputs and the tokenizer under the `with_target_tokenizer` """ """context manager to prepare your targets. See the documentation of your specific tokenizer for more """ """details""" , UpperCamelCase__ , ) if max_length is None: A_ = self.current_tokenizer.model_max_length A_ = self( UpperCamelCase__ , add_special_tokens=UpperCamelCase__ , return_tensors=UpperCamelCase__ , max_length=UpperCamelCase__ , padding=UpperCamelCase__ , truncation=UpperCamelCase__ , **UpperCamelCase__ , ) if tgt_texts is None: return model_inputs # Process tgt_texts if max_target_length is None: A_ = self.current_tokenizer.model_max_length A_ = self( text_target=UpperCamelCase__ , add_special_tokens=UpperCamelCase__ , return_tensors=UpperCamelCase__ , padding=UpperCamelCase__ , max_length=UpperCamelCase__ , truncation=UpperCamelCase__ , **UpperCamelCase__ , ) A_ = labels["""input_ids"""] return model_inputs
667
'''simple docstring''' def UpperCAmelCase__ ( UpperCAmelCase__ ) -> bool: return str(UpperCAmelCase__ ) == str(UpperCAmelCase__ )[::-1] def UpperCAmelCase__ ( UpperCAmelCase__ ) -> int: return int(UpperCAmelCase__ ) + int(str(UpperCAmelCase__ )[::-1] ) def UpperCAmelCase__ ( UpperCAmelCase__ = 1_00_00 ) -> int: A_ = [] for num in range(1, UpperCAmelCase__ ): A_ = 0 A_ = num while iterations < 50: A_ = sum_reverse(UpperCAmelCase__ ) iterations += 1 if is_palindrome(UpperCAmelCase__ ): break else: lychrel_nums.append(UpperCAmelCase__ ) return len(UpperCAmelCase__ ) if __name__ == "__main__": print(f"""{solution() = }""")
667
1