code
stringlengths
82
54.1k
code_codestyle
int64
0
699
style_context
stringlengths
111
35.6k
style_context_codestyle
int64
0
699
label
int64
0
1
import unittest from transformers import ( MODEL_FOR_OBJECT_DETECTION_MAPPING, AutoFeatureExtractor, AutoModelForObjectDetection, ObjectDetectionPipeline, is_vision_available, pipeline, ) from transformers.testing_utils import ( is_pipeline_test, nested_simplify, require_pytesseract, require_tf, require_timm, require_torch, require_vision, slow, ) from .test_pipelines_common import ANY if is_vision_available(): from PIL import Image else: class UpperCamelCase : @staticmethod def UpperCamelCase_ ( *_lowerCAmelCase : Any ,**_lowerCAmelCase : str ): """simple docstring""" pass @is_pipeline_test @require_vision @require_timm @require_torch class UpperCamelCase ( unittest.TestCase ): __UpperCamelCase = MODEL_FOR_OBJECT_DETECTION_MAPPING def UpperCamelCase_ ( self : Dict ,_lowerCAmelCase : int ,_lowerCAmelCase : Tuple ,_lowerCAmelCase : Any ): """simple docstring""" __snake_case = ObjectDetectionPipeline(model=_a ,image_processor=_a ) return object_detector, ["./tests/fixtures/tests_samples/COCO/000000039769.png"] def UpperCamelCase_ ( self : Any ,_lowerCAmelCase : Optional[int] ,_lowerCAmelCase : Optional[Any] ): """simple docstring""" __snake_case = object_detector("./tests/fixtures/tests_samples/COCO/000000039769.png" ,threshold=0.0 ) self.assertGreater(len(_a ) ,0 ) for detected_object in outputs: self.assertEqual( _a ,{ "score": ANY(_a ), "label": ANY(_a ), "box": {"xmin": ANY(_a ), "ymin": ANY(_a ), "xmax": ANY(_a ), "ymax": ANY(_a )}, } ,) import datasets __snake_case = datasets.load_dataset("hf-internal-testing/fixtures_image_utils" ,"image" ,split="test" ) __snake_case = [ Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png" ), "http://images.cocodataset.org/val2017/000000039769.jpg", # RGBA dataset[0]["file"], # LA dataset[1]["file"], # L dataset[2]["file"], ] __snake_case = object_detector(_a ,threshold=0.0 ) self.assertEqual(len(_a ) ,len(_a ) ) for outputs in batch_outputs: self.assertGreater(len(_a ) ,0 ) for detected_object in outputs: self.assertEqual( _a ,{ "score": ANY(_a ), "label": ANY(_a ), "box": {"xmin": ANY(_a ), "ymin": ANY(_a ), "xmax": ANY(_a ), "ymax": ANY(_a )}, } ,) @require_tf @unittest.skip("Object detection not implemented in TF" ) def UpperCamelCase_ ( self : Optional[Any] ): """simple docstring""" pass @require_torch def UpperCamelCase_ ( self : Any ): """simple docstring""" __snake_case = "hf-internal-testing/tiny-detr-mobilenetsv3" __snake_case = AutoModelForObjectDetection.from_pretrained(_a ) __snake_case = AutoFeatureExtractor.from_pretrained(_a ) __snake_case = ObjectDetectionPipeline(model=_a ,feature_extractor=_a ) __snake_case = object_detector("http://images.cocodataset.org/val2017/000000039769.jpg" ,threshold=0.0 ) self.assertEqual( nested_simplify(_a ,decimals=4 ) ,[ {"score": 0.3_3_7_6, "label": "LABEL_0", "box": {"xmin": 159, "ymin": 120, "xmax": 480, "ymax": 359}}, {"score": 0.3_3_7_6, "label": "LABEL_0", "box": {"xmin": 159, "ymin": 120, "xmax": 480, "ymax": 359}}, ] ,) __snake_case = object_detector( [ "http://images.cocodataset.org/val2017/000000039769.jpg", "http://images.cocodataset.org/val2017/000000039769.jpg", ] ,threshold=0.0 ,) self.assertEqual( nested_simplify(_a ,decimals=4 ) ,[ [ {"score": 0.3_3_7_6, "label": "LABEL_0", "box": {"xmin": 159, "ymin": 120, "xmax": 480, "ymax": 359}}, {"score": 0.3_3_7_6, "label": "LABEL_0", "box": {"xmin": 159, "ymin": 120, "xmax": 480, "ymax": 359}}, ], [ {"score": 0.3_3_7_6, "label": "LABEL_0", "box": {"xmin": 159, "ymin": 120, "xmax": 480, "ymax": 359}}, {"score": 0.3_3_7_6, "label": "LABEL_0", "box": {"xmin": 159, "ymin": 120, "xmax": 480, "ymax": 359}}, ], ] ,) @require_torch @slow def UpperCamelCase_ ( self : List[Any] ): """simple docstring""" __snake_case = "facebook/detr-resnet-50" __snake_case = AutoModelForObjectDetection.from_pretrained(_a ) __snake_case = AutoFeatureExtractor.from_pretrained(_a ) __snake_case = ObjectDetectionPipeline(model=_a ,feature_extractor=_a ) __snake_case = object_detector("http://images.cocodataset.org/val2017/000000039769.jpg" ) self.assertEqual( nested_simplify(_a ,decimals=4 ) ,[ {"score": 0.9_9_8_2, "label": "remote", "box": {"xmin": 40, "ymin": 70, "xmax": 175, "ymax": 117}}, {"score": 0.9_9_6_0, "label": "remote", "box": {"xmin": 333, "ymin": 72, "xmax": 368, "ymax": 187}}, {"score": 0.9_9_5_5, "label": "couch", "box": {"xmin": 0, "ymin": 1, "xmax": 639, "ymax": 473}}, {"score": 0.9_9_8_8, "label": "cat", "box": {"xmin": 13, "ymin": 52, "xmax": 314, "ymax": 470}}, {"score": 0.9_9_8_7, "label": "cat", "box": {"xmin": 345, "ymin": 23, "xmax": 640, "ymax": 368}}, ] ,) __snake_case = object_detector( [ "http://images.cocodataset.org/val2017/000000039769.jpg", "http://images.cocodataset.org/val2017/000000039769.jpg", ] ) self.assertEqual( nested_simplify(_a ,decimals=4 ) ,[ [ {"score": 0.9_9_8_2, "label": "remote", "box": {"xmin": 40, "ymin": 70, "xmax": 175, "ymax": 117}}, {"score": 0.9_9_6_0, "label": "remote", "box": {"xmin": 333, "ymin": 72, "xmax": 368, "ymax": 187}}, {"score": 0.9_9_5_5, "label": "couch", "box": {"xmin": 0, "ymin": 1, "xmax": 639, "ymax": 473}}, {"score": 0.9_9_8_8, "label": "cat", "box": {"xmin": 13, "ymin": 52, "xmax": 314, "ymax": 470}}, {"score": 0.9_9_8_7, "label": "cat", "box": {"xmin": 345, "ymin": 23, "xmax": 640, "ymax": 368}}, ], [ {"score": 0.9_9_8_2, "label": "remote", "box": {"xmin": 40, "ymin": 70, "xmax": 175, "ymax": 117}}, {"score": 0.9_9_6_0, "label": "remote", "box": {"xmin": 333, "ymin": 72, "xmax": 368, "ymax": 187}}, {"score": 0.9_9_5_5, "label": "couch", "box": {"xmin": 0, "ymin": 1, "xmax": 639, "ymax": 473}}, {"score": 0.9_9_8_8, "label": "cat", "box": {"xmin": 13, "ymin": 52, "xmax": 314, "ymax": 470}}, {"score": 0.9_9_8_7, "label": "cat", "box": {"xmin": 345, "ymin": 23, "xmax": 640, "ymax": 368}}, ], ] ,) @require_torch @slow def UpperCamelCase_ ( self : Any ): """simple docstring""" __snake_case = "facebook/detr-resnet-50" __snake_case = pipeline("object-detection" ,model=_a ) __snake_case = object_detector("http://images.cocodataset.org/val2017/000000039769.jpg" ) self.assertEqual( nested_simplify(_a ,decimals=4 ) ,[ {"score": 0.9_9_8_2, "label": "remote", "box": {"xmin": 40, "ymin": 70, "xmax": 175, "ymax": 117}}, {"score": 0.9_9_6_0, "label": "remote", "box": {"xmin": 333, "ymin": 72, "xmax": 368, "ymax": 187}}, {"score": 0.9_9_5_5, "label": "couch", "box": {"xmin": 0, "ymin": 1, "xmax": 639, "ymax": 473}}, {"score": 0.9_9_8_8, "label": "cat", "box": {"xmin": 13, "ymin": 52, "xmax": 314, "ymax": 470}}, {"score": 0.9_9_8_7, "label": "cat", "box": {"xmin": 345, "ymin": 23, "xmax": 640, "ymax": 368}}, ] ,) __snake_case = object_detector( [ "http://images.cocodataset.org/val2017/000000039769.jpg", "http://images.cocodataset.org/val2017/000000039769.jpg", ] ) self.assertEqual( nested_simplify(_a ,decimals=4 ) ,[ [ {"score": 0.9_9_8_2, "label": "remote", "box": {"xmin": 40, "ymin": 70, "xmax": 175, "ymax": 117}}, {"score": 0.9_9_6_0, "label": "remote", "box": {"xmin": 333, "ymin": 72, "xmax": 368, "ymax": 187}}, {"score": 0.9_9_5_5, "label": "couch", "box": {"xmin": 0, "ymin": 1, "xmax": 639, "ymax": 473}}, {"score": 0.9_9_8_8, "label": "cat", "box": {"xmin": 13, "ymin": 52, "xmax": 314, "ymax": 470}}, {"score": 0.9_9_8_7, "label": "cat", "box": {"xmin": 345, "ymin": 23, "xmax": 640, "ymax": 368}}, ], [ {"score": 0.9_9_8_2, "label": "remote", "box": {"xmin": 40, "ymin": 70, "xmax": 175, "ymax": 117}}, {"score": 0.9_9_6_0, "label": "remote", "box": {"xmin": 333, "ymin": 72, "xmax": 368, "ymax": 187}}, {"score": 0.9_9_5_5, "label": "couch", "box": {"xmin": 0, "ymin": 1, "xmax": 639, "ymax": 473}}, {"score": 0.9_9_8_8, "label": "cat", "box": {"xmin": 13, "ymin": 52, "xmax": 314, "ymax": 470}}, {"score": 0.9_9_8_7, "label": "cat", "box": {"xmin": 345, "ymin": 23, "xmax": 640, "ymax": 368}}, ], ] ,) @require_torch @slow def UpperCamelCase_ ( self : Tuple ): """simple docstring""" __snake_case = 0.9_9_8_5 __snake_case = "facebook/detr-resnet-50" __snake_case = pipeline("object-detection" ,model=_a ) __snake_case = object_detector("http://images.cocodataset.org/val2017/000000039769.jpg" ,threshold=_a ) self.assertEqual( nested_simplify(_a ,decimals=4 ) ,[ {"score": 0.9_9_8_8, "label": "cat", "box": {"xmin": 13, "ymin": 52, "xmax": 314, "ymax": 470}}, {"score": 0.9_9_8_7, "label": "cat", "box": {"xmin": 345, "ymin": 23, "xmax": 640, "ymax": 368}}, ] ,) @require_torch @require_pytesseract @slow def UpperCamelCase_ ( self : Union[str, Any] ): """simple docstring""" __snake_case = "Narsil/layoutlmv3-finetuned-funsd" __snake_case = 0.9_9_9_3 __snake_case = pipeline("object-detection" ,model=_a ,threshold=_a ) __snake_case = object_detector( "https://huggingface.co/spaces/impira/docquery/resolve/2359223c1837a7587402bda0f2643382a6eefeab/invoice.png" ) self.assertEqual( nested_simplify(_a ,decimals=4 ) ,[ {"score": 0.9_9_9_3, "label": "I-ANSWER", "box": {"xmin": 294, "ymin": 254, "xmax": 343, "ymax": 264}}, {"score": 0.9_9_9_3, "label": "I-ANSWER", "box": {"xmin": 294, "ymin": 254, "xmax": 343, "ymax": 264}}, ] ,)
524
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 A__ ( UpperCamelCase__ ): def __UpperCamelCase ( self : Tuple ) -> Dict: """simple docstring""" _SCREAMING_SNAKE_CASE =tempfile.mkdtemp() _SCREAMING_SNAKE_CASE =8 # DPR tok _SCREAMING_SNAKE_CASE =[ '''[UNK]''', '''[CLS]''', '''[SEP]''', '''[PAD]''', '''[MASK]''', '''want''', '''##want''', '''##ed''', '''wa''', '''un''', '''runn''', '''##ing''', ''',''', '''low''', '''lowest''', ] _SCREAMING_SNAKE_CASE =os.path.join(self.tmpdirname , '''dpr_tokenizer''' ) os.makedirs(_a , exist_ok=_a ) _SCREAMING_SNAKE_CASE =os.path.join(_a , 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 _SCREAMING_SNAKE_CASE =[ '''l''', '''o''', '''w''', '''e''', '''r''', '''s''', '''t''', '''i''', '''d''', '''n''', '''\u0120''', '''\u0120l''', '''\u0120n''', '''\u0120lo''', '''\u0120low''', '''er''', '''\u0120lowest''', '''\u0120newer''', '''\u0120wider''', '''<unk>''', ] _SCREAMING_SNAKE_CASE =dict(zip(_a , range(len(_a ) ) ) ) _SCREAMING_SNAKE_CASE =['''#version: 0.2''', '''\u0120 l''', '''\u0120l o''', '''\u0120lo w''', '''e r''', ''''''] _SCREAMING_SNAKE_CASE ={'''unk_token''': '''<unk>'''} _SCREAMING_SNAKE_CASE =os.path.join(self.tmpdirname , '''bart_tokenizer''' ) os.makedirs(_a , exist_ok=_a ) _SCREAMING_SNAKE_CASE =os.path.join(_a , BART_VOCAB_FILES_NAMES['''vocab_file'''] ) _SCREAMING_SNAKE_CASE =os.path.join(_a , BART_VOCAB_FILES_NAMES['''merges_file'''] ) with open(self.vocab_file , '''w''' , encoding='''utf-8''' ) as fp: fp.write(json.dumps(_a ) + '''\n''' ) with open(self.merges_file , '''w''' , encoding='''utf-8''' ) as fp: fp.write('''\n'''.join(_a ) ) def __UpperCamelCase ( self : List[str] ) -> DPRQuestionEncoderTokenizer: """simple docstring""" return DPRQuestionEncoderTokenizer.from_pretrained(os.path.join(self.tmpdirname , '''dpr_tokenizer''' ) ) def __UpperCamelCase ( self : Dict ) -> DPRContextEncoderTokenizer: """simple docstring""" return DPRContextEncoderTokenizer.from_pretrained(os.path.join(self.tmpdirname , '''dpr_tokenizer''' ) ) def __UpperCamelCase ( self : Union[str, Any] ) -> BartTokenizer: """simple docstring""" return BartTokenizer.from_pretrained(os.path.join(self.tmpdirname , '''bart_tokenizer''' ) ) def __UpperCamelCase ( self : Union[str, Any] ) -> int: """simple docstring""" shutil.rmtree(self.tmpdirname ) def __UpperCamelCase ( self : Union[str, Any] ) -> int: """simple docstring""" _SCREAMING_SNAKE_CASE =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 __UpperCamelCase ( self : str ) -> List[str]: """simple docstring""" _SCREAMING_SNAKE_CASE =self.get_dummy_dataset() _SCREAMING_SNAKE_CASE =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: _SCREAMING_SNAKE_CASE =dataset _SCREAMING_SNAKE_CASE =RagRetriever( _a , question_encoder_tokenizer=self.get_dpr_tokenizer() , generator_tokenizer=self.get_bart_tokenizer() , ) return retriever def __UpperCamelCase ( self : Optional[int] , _a : bool ) -> Any: """simple docstring""" _SCREAMING_SNAKE_CASE =self.get_dummy_dataset() _SCREAMING_SNAKE_CASE =RagConfig( retrieval_vector_size=self.retrieval_vector_size , question_encoder=DPRConfig().to_dict() , generator=BartConfig().to_dict() , index_name='''custom''' , ) if from_disk: _SCREAMING_SNAKE_CASE =os.path.join(self.tmpdirname , '''dataset''' ) _SCREAMING_SNAKE_CASE =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 _SCREAMING_SNAKE_CASE =RagRetriever( _a , question_encoder_tokenizer=self.get_dpr_tokenizer() , generator_tokenizer=self.get_bart_tokenizer() , ) else: _SCREAMING_SNAKE_CASE =RagRetriever( _a , question_encoder_tokenizer=self.get_dpr_tokenizer() , generator_tokenizer=self.get_bart_tokenizer() , index=CustomHFIndex(config.retrieval_vector_size , _a ) , ) return retriever def __UpperCamelCase ( self : Optional[Any] ) -> Dict: """simple docstring""" _SCREAMING_SNAKE_CASE =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 ) _SCREAMING_SNAKE_CASE =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''' ) ) _SCREAMING_SNAKE_CASE =os.path.join(self.tmpdirname , '''psgs_w100.tsv.pkl''' ) _SCREAMING_SNAKE_CASE ={sample['''id''']: [sample['''text'''], sample['''title''']] for sample in dataset} pickle.dump(_a , open(_a , '''wb''' ) ) _SCREAMING_SNAKE_CASE =RagConfig( retrieval_vector_size=self.retrieval_vector_size , question_encoder=DPRConfig().to_dict() , generator=BartConfig().to_dict() , index_name='''legacy''' , index_path=self.tmpdirname , ) _SCREAMING_SNAKE_CASE =RagRetriever( _a , question_encoder_tokenizer=self.get_dpr_tokenizer() , generator_tokenizer=self.get_bart_tokenizer() ) return retriever def __UpperCamelCase ( self : Tuple ) -> Optional[Any]: """simple docstring""" _SCREAMING_SNAKE_CASE =1 _SCREAMING_SNAKE_CASE =self.get_dummy_canonical_hf_index_retriever() _SCREAMING_SNAKE_CASE =np.array( [np.ones(self.retrieval_vector_size ), -np.ones(self.retrieval_vector_size )] , dtype=np.floataa ) _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE =retriever.retrieve(_a , n_docs=_a ) self.assertEqual(retrieved_doc_embeds.shape , (2, n_docs, self.retrieval_vector_size) ) self.assertEqual(len(_a ) , 2 ) self.assertEqual(sorted(doc_dicts[0] ) , ['''embeddings''', '''id''', '''text''', '''title'''] ) self.assertEqual(len(doc_dicts[0]['''id'''] ) , _a ) 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 __UpperCamelCase ( self : Any ) -> str: """simple docstring""" _SCREAMING_SNAKE_CASE =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: _SCREAMING_SNAKE_CASE =self.get_dummy_dataset() retriever.save_pretrained(_a ) _SCREAMING_SNAKE_CASE =RagRetriever.from_pretrained(_a ) self.assertIsInstance(_a , _a ) _SCREAMING_SNAKE_CASE =np.array( [np.ones(self.retrieval_vector_size ), -np.ones(self.retrieval_vector_size )] , dtype=np.floataa ) _SCREAMING_SNAKE_CASE =retriever.retrieve(_a , n_docs=1 ) self.assertTrue(out is not None ) def __UpperCamelCase ( self : Dict ) -> Union[str, Any]: """simple docstring""" _SCREAMING_SNAKE_CASE =1 _SCREAMING_SNAKE_CASE =self.get_dummy_custom_hf_index_retriever(from_disk=_a ) _SCREAMING_SNAKE_CASE =np.array( [np.ones(self.retrieval_vector_size ), -np.ones(self.retrieval_vector_size )] , dtype=np.floataa ) _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE =retriever.retrieve(_a , n_docs=_a ) self.assertEqual(retrieved_doc_embeds.shape , (2, n_docs, self.retrieval_vector_size) ) self.assertEqual(len(_a ) , 2 ) self.assertEqual(sorted(doc_dicts[0] ) , ['''embeddings''', '''id''', '''text''', '''title'''] ) self.assertEqual(len(doc_dicts[0]['''id'''] ) , _a ) 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 __UpperCamelCase ( self : Optional[Any] ) -> Any: """simple docstring""" _SCREAMING_SNAKE_CASE =self.get_dummy_custom_hf_index_retriever(from_disk=_a ) with tempfile.TemporaryDirectory() as tmp_dirname: retriever.save_pretrained(_a ) _SCREAMING_SNAKE_CASE =RagRetriever.from_pretrained(_a ) self.assertIsInstance(_a , _a ) _SCREAMING_SNAKE_CASE =np.array( [np.ones(self.retrieval_vector_size ), -np.ones(self.retrieval_vector_size )] , dtype=np.floataa ) _SCREAMING_SNAKE_CASE =retriever.retrieve(_a , n_docs=1 ) self.assertTrue(out is not None ) def __UpperCamelCase ( self : Dict ) -> Optional[int]: """simple docstring""" _SCREAMING_SNAKE_CASE =1 _SCREAMING_SNAKE_CASE =self.get_dummy_custom_hf_index_retriever(from_disk=_a ) _SCREAMING_SNAKE_CASE =np.array( [np.ones(self.retrieval_vector_size ), -np.ones(self.retrieval_vector_size )] , dtype=np.floataa ) _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE =retriever.retrieve(_a , n_docs=_a ) self.assertEqual(retrieved_doc_embeds.shape , (2, n_docs, self.retrieval_vector_size) ) self.assertEqual(len(_a ) , 2 ) self.assertEqual(sorted(doc_dicts[0] ) , ['''embeddings''', '''id''', '''text''', '''title'''] ) self.assertEqual(len(doc_dicts[0]['''id'''] ) , _a ) 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 __UpperCamelCase ( self : Tuple ) -> Optional[Any]: """simple docstring""" _SCREAMING_SNAKE_CASE =self.get_dummy_custom_hf_index_retriever(from_disk=_a ) with tempfile.TemporaryDirectory() as tmp_dirname: retriever.save_pretrained(_a ) _SCREAMING_SNAKE_CASE =RagRetriever.from_pretrained(_a ) self.assertIsInstance(_a , _a ) _SCREAMING_SNAKE_CASE =np.array( [np.ones(self.retrieval_vector_size ), -np.ones(self.retrieval_vector_size )] , dtype=np.floataa ) _SCREAMING_SNAKE_CASE =retriever.retrieve(_a , n_docs=1 ) self.assertTrue(out is not None ) def __UpperCamelCase ( self : Optional[int] ) -> Dict: """simple docstring""" _SCREAMING_SNAKE_CASE =1 _SCREAMING_SNAKE_CASE =self.get_dummy_legacy_index_retriever() _SCREAMING_SNAKE_CASE =np.array( [np.ones(self.retrieval_vector_size ), -np.ones(self.retrieval_vector_size )] , dtype=np.floataa ) _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE =retriever.retrieve(_a , n_docs=_a ) self.assertEqual(retrieved_doc_embeds.shape , (2, n_docs, self.retrieval_vector_size) ) self.assertEqual(len(_a ) , 2 ) self.assertEqual(sorted(doc_dicts[0] ) , ['''text''', '''title'''] ) self.assertEqual(len(doc_dicts[0]['''text'''] ) , _a ) 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 __UpperCamelCase ( self : Dict ) -> Dict: """simple docstring""" _SCREAMING_SNAKE_CASE =self.get_dummy_legacy_index_retriever() with tempfile.TemporaryDirectory() as tmp_dirname: retriever.save_pretrained(_a ) _SCREAMING_SNAKE_CASE =RagRetriever.from_pretrained(_a ) self.assertIsInstance(_a , _a ) _SCREAMING_SNAKE_CASE =np.array( [np.ones(self.retrieval_vector_size ), -np.ones(self.retrieval_vector_size )] , dtype=np.floataa ) _SCREAMING_SNAKE_CASE =retriever.retrieve(_a , n_docs=1 ) self.assertTrue(out is not None ) @require_torch @require_tokenizers @require_sentencepiece def __UpperCamelCase ( self : Optional[int] ) -> int: """simple docstring""" import torch _SCREAMING_SNAKE_CASE =1 _SCREAMING_SNAKE_CASE =self.get_dummy_canonical_hf_index_retriever() _SCREAMING_SNAKE_CASE =[[5, 7], [10, 11]] _SCREAMING_SNAKE_CASE =np.array( [np.ones(self.retrieval_vector_size ), -np.ones(self.retrieval_vector_size )] , dtype=np.floataa ) _SCREAMING_SNAKE_CASE =retriever(_a , _a , prefix=retriever.config.generator.prefix , n_docs=_a ) _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE =( 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(_a , _a ) self.assertIsInstance(_a , _a ) self.assertIsInstance(_a , np.ndarray ) _SCREAMING_SNAKE_CASE =retriever( _a , _a , prefix=retriever.config.generator.prefix , n_docs=_a , return_tensors='''pt''' , ) _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE =( # 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(_a , torch.Tensor ) self.assertIsInstance(_a , torch.Tensor ) self.assertIsInstance(_a , torch.Tensor ) @require_torch @require_tokenizers @require_sentencepiece def __UpperCamelCase ( self : str ) -> Any: """simple docstring""" _SCREAMING_SNAKE_CASE =self.get_dpr_ctx_encoder_tokenizer() _SCREAMING_SNAKE_CASE =1 _SCREAMING_SNAKE_CASE =self.get_dummy_custom_hf_index_retriever(from_disk=_a ) retriever.set_ctx_encoder_tokenizer(_a ) _SCREAMING_SNAKE_CASE =[[5, 7], [10, 11]] _SCREAMING_SNAKE_CASE =np.array( [np.ones(self.retrieval_vector_size ), -np.ones(self.retrieval_vector_size )] , dtype=np.floataa ) _SCREAMING_SNAKE_CASE =retriever(_a , _a , prefix=retriever.config.generator.prefix , n_docs=_a ) self.assertEqual( len(_a ) , 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''') ) , _a ) # check for doc token related keys in dictionary.
691
0
def __lowerCamelCase ( UpperCamelCase__ ): '''simple docstring''' snake_case_ = [int(a__ ) for i in ip_va_address.split('.' ) if i.isdigit()] return len(a__ ) == 4 and all(0 <= int(a__ ) <= 254 for octet in octets ) if __name__ == "__main__": _UpperCAmelCase : List[Any] = input().strip() _UpperCAmelCase : Optional[Any] = '''valid''' if is_ip_va_address_valid(ip) else '''invalid''' print(F'''{ip} is a {valid_or_invalid} IP v4 address.''')
362
import gc import random import unittest import numpy as np import torch from PIL import Image from transformers import XLMRobertaTokenizerFast from diffusers import DDIMScheduler, KandinskyImgaImgPipeline, KandinskyPriorPipeline, UNetaDConditionModel, VQModel from diffusers.pipelines.kandinsky.text_encoder import MCLIPConfig, MultilingualCLIP from diffusers.utils import floats_tensor, load_image, load_numpy, slow, torch_device from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu from ..test_pipelines_common import PipelineTesterMixin, assert_mean_pixel_difference enable_full_determinism() class A__ ( UpperCamelCase__ , unittest.TestCase ): UpperCAmelCase = KandinskyImgaImgPipeline UpperCAmelCase = ["prompt", "image_embeds", "negative_image_embeds", "image"] UpperCAmelCase = [ "prompt", "negative_prompt", "image_embeds", "negative_image_embeds", "image", ] UpperCAmelCase = [ "generator", "height", "width", "strength", "guidance_scale", "negative_prompt", "num_inference_steps", "return_dict", "guidance_scale", "num_images_per_prompt", "output_type", "return_dict", ] UpperCAmelCase = False @property def __UpperCamelCase ( self : Union[str, Any] ) -> Union[str, Any]: """simple docstring""" return 32 @property def __UpperCamelCase ( self : Optional[int] ) -> List[str]: """simple docstring""" return 32 @property def __UpperCamelCase ( self : int ) -> Tuple: """simple docstring""" return self.time_input_dim @property def __UpperCamelCase ( self : Tuple ) -> List[Any]: """simple docstring""" return self.time_input_dim * 4 @property def __UpperCamelCase ( self : Any ) -> Optional[Any]: """simple docstring""" return 100 @property def __UpperCamelCase ( self : Dict ) -> int: """simple docstring""" _SCREAMING_SNAKE_CASE =XLMRobertaTokenizerFast.from_pretrained('''YiYiXu/tiny-random-mclip-base''' ) return tokenizer @property def __UpperCamelCase ( self : Dict ) -> Optional[int]: """simple docstring""" torch.manual_seed(0 ) _SCREAMING_SNAKE_CASE =MCLIPConfig( numDims=self.cross_attention_dim , transformerDimensions=self.text_embedder_hidden_size , hidden_size=self.text_embedder_hidden_size , intermediate_size=37 , num_attention_heads=4 , num_hidden_layers=5 , vocab_size=1005 , ) _SCREAMING_SNAKE_CASE =MultilingualCLIP(_a ) _SCREAMING_SNAKE_CASE =text_encoder.eval() return text_encoder @property def __UpperCamelCase ( self : List[Any] ) -> List[str]: """simple docstring""" torch.manual_seed(0 ) _SCREAMING_SNAKE_CASE ={ '''in_channels''': 4, # Out channels is double in channels because predicts mean and variance '''out_channels''': 8, '''addition_embed_type''': '''text_image''', '''down_block_types''': ('''ResnetDownsampleBlock2D''', '''SimpleCrossAttnDownBlock2D'''), '''up_block_types''': ('''SimpleCrossAttnUpBlock2D''', '''ResnetUpsampleBlock2D'''), '''mid_block_type''': '''UNetMidBlock2DSimpleCrossAttn''', '''block_out_channels''': (self.block_out_channels_a, self.block_out_channels_a * 2), '''layers_per_block''': 1, '''encoder_hid_dim''': self.text_embedder_hidden_size, '''encoder_hid_dim_type''': '''text_image_proj''', '''cross_attention_dim''': self.cross_attention_dim, '''attention_head_dim''': 4, '''resnet_time_scale_shift''': '''scale_shift''', '''class_embed_type''': None, } _SCREAMING_SNAKE_CASE =UNetaDConditionModel(**_a ) return model @property def __UpperCamelCase ( self : List[Any] ) -> Union[str, Any]: """simple docstring""" return { "block_out_channels": [32, 64], "down_block_types": ["DownEncoderBlock2D", "AttnDownEncoderBlock2D"], "in_channels": 3, "latent_channels": 4, "layers_per_block": 1, "norm_num_groups": 8, "norm_type": "spatial", "num_vq_embeddings": 12, "out_channels": 3, "up_block_types": [ "AttnUpDecoderBlock2D", "UpDecoderBlock2D", ], "vq_embed_dim": 4, } @property def __UpperCamelCase ( self : List[Any] ) -> Optional[Any]: """simple docstring""" torch.manual_seed(0 ) _SCREAMING_SNAKE_CASE =VQModel(**self.dummy_movq_kwargs ) return model def __UpperCamelCase ( self : str ) -> int: """simple docstring""" _SCREAMING_SNAKE_CASE =self.dummy_text_encoder _SCREAMING_SNAKE_CASE =self.dummy_tokenizer _SCREAMING_SNAKE_CASE =self.dummy_unet _SCREAMING_SNAKE_CASE =self.dummy_movq _SCREAMING_SNAKE_CASE ={ '''num_train_timesteps''': 1000, '''beta_schedule''': '''linear''', '''beta_start''': 0.0_00_85, '''beta_end''': 0.0_12, '''clip_sample''': False, '''set_alpha_to_one''': False, '''steps_offset''': 0, '''prediction_type''': '''epsilon''', '''thresholding''': False, } _SCREAMING_SNAKE_CASE =DDIMScheduler(**_a ) _SCREAMING_SNAKE_CASE ={ '''text_encoder''': text_encoder, '''tokenizer''': tokenizer, '''unet''': unet, '''scheduler''': scheduler, '''movq''': movq, } return components def __UpperCamelCase ( self : str , _a : int , _a : int=0 ) -> Any: """simple docstring""" _SCREAMING_SNAKE_CASE =floats_tensor((1, self.cross_attention_dim) , rng=random.Random(_a ) ).to(_a ) _SCREAMING_SNAKE_CASE =floats_tensor((1, self.cross_attention_dim) , rng=random.Random(seed + 1 ) ).to(_a ) # create init_image _SCREAMING_SNAKE_CASE =floats_tensor((1, 3, 64, 64) , rng=random.Random(_a ) ).to(_a ) _SCREAMING_SNAKE_CASE =image.cpu().permute(0 , 2 , 3 , 1 )[0] _SCREAMING_SNAKE_CASE =Image.fromarray(np.uinta(_a ) ).convert('''RGB''' ).resize((256, 256) ) if str(_a ).startswith('''mps''' ): _SCREAMING_SNAKE_CASE =torch.manual_seed(_a ) else: _SCREAMING_SNAKE_CASE =torch.Generator(device=_a ).manual_seed(_a ) _SCREAMING_SNAKE_CASE ={ '''prompt''': '''horse''', '''image''': init_image, '''image_embeds''': image_embeds, '''negative_image_embeds''': negative_image_embeds, '''generator''': generator, '''height''': 64, '''width''': 64, '''num_inference_steps''': 10, '''guidance_scale''': 7.0, '''strength''': 0.2, '''output_type''': '''np''', } return inputs def __UpperCamelCase ( self : Any ) -> Optional[int]: """simple docstring""" _SCREAMING_SNAKE_CASE ='''cpu''' _SCREAMING_SNAKE_CASE =self.get_dummy_components() _SCREAMING_SNAKE_CASE =self.pipeline_class(**_a ) _SCREAMING_SNAKE_CASE =pipe.to(_a ) pipe.set_progress_bar_config(disable=_a ) _SCREAMING_SNAKE_CASE =pipe(**self.get_dummy_inputs(_a ) ) _SCREAMING_SNAKE_CASE =output.images _SCREAMING_SNAKE_CASE =pipe( **self.get_dummy_inputs(_a ) , return_dict=_a , )[0] _SCREAMING_SNAKE_CASE =image[0, -3:, -3:, -1] _SCREAMING_SNAKE_CASE =image_from_tuple[0, -3:, -3:, -1] assert image.shape == (1, 64, 64, 3) _SCREAMING_SNAKE_CASE =np.array( [0.61_47_49_43, 0.6_07_35_39, 0.43_30_85_44, 0.5_92_82_69, 0.47_49_35_95, 0.46_75_59_73, 0.4_61_38_38, 0.45_36_87_97, 0.50_11_92_33] ) assert ( np.abs(image_slice.flatten() - expected_slice ).max() < 1E-2 ), f" expected_slice {expected_slice}, but got {image_slice.flatten()}" assert ( np.abs(image_from_tuple_slice.flatten() - expected_slice ).max() < 1E-2 ), f" expected_slice {expected_slice}, but got {image_from_tuple_slice.flatten()}" @slow @require_torch_gpu class A__ ( unittest.TestCase ): def __UpperCamelCase ( self : Optional[Any] ) -> List[Any]: """simple docstring""" super().tearDown() gc.collect() torch.cuda.empty_cache() def __UpperCamelCase ( self : Dict ) -> Any: """simple docstring""" _SCREAMING_SNAKE_CASE =load_numpy( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main''' '''/kandinsky/kandinsky_img2img_frog.npy''' ) _SCREAMING_SNAKE_CASE =load_image( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main''' '''/kandinsky/cat.png''' ) _SCREAMING_SNAKE_CASE ='''A red cartoon frog, 4k''' _SCREAMING_SNAKE_CASE =KandinskyPriorPipeline.from_pretrained( '''kandinsky-community/kandinsky-2-1-prior''' , torch_dtype=torch.floataa ) pipe_prior.to(_a ) _SCREAMING_SNAKE_CASE =KandinskyImgaImgPipeline.from_pretrained( '''kandinsky-community/kandinsky-2-1''' , torch_dtype=torch.floataa ) _SCREAMING_SNAKE_CASE =pipeline.to(_a ) pipeline.set_progress_bar_config(disable=_a ) _SCREAMING_SNAKE_CASE =torch.Generator(device='''cpu''' ).manual_seed(0 ) _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE =pipe_prior( _a , generator=_a , num_inference_steps=5 , negative_prompt='''''' , ).to_tuple() _SCREAMING_SNAKE_CASE =pipeline( _a , image=_a , image_embeds=_a , negative_image_embeds=_a , generator=_a , num_inference_steps=100 , height=768 , width=768 , strength=0.2 , output_type='''np''' , ) _SCREAMING_SNAKE_CASE =output.images[0] assert image.shape == (768, 768, 3) assert_mean_pixel_difference(_a , _a )
691
0
"""simple docstring""" from typing import TYPE_CHECKING # rely on isort to merge the imports from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available _A = { '''configuration_informer''': [ '''INFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''InformerConfig''', ], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _A = [ '''INFORMER_PRETRAINED_MODEL_ARCHIVE_LIST''', '''InformerForPrediction''', '''InformerModel''', '''InformerPreTrainedModel''', ] if TYPE_CHECKING: from .configuration_informer import INFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP, InformerConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_informer import ( INFORMER_PRETRAINED_MODEL_ARCHIVE_LIST, InformerForPrediction, InformerModel, InformerPreTrainedModel, ) else: import sys _A = _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)
182
import unittest import numpy as np from transformers.testing_utils import require_torch, require_vision from transformers.utils import is_torch_available, is_vision_available from ...test_image_processing_common import ImageProcessingSavingTestMixin if is_torch_available(): import torch if is_vision_available(): from PIL import Image from transformers import ChineseCLIPImageProcessor class A__ ( unittest.TestCase ): def __init__( self : List[str] , _a : Dict , _a : Dict=7 , _a : List[str]=3 , _a : str=18 , _a : Optional[int]=30 , _a : Tuple=400 , _a : Optional[Any]=True , _a : Dict=None , _a : str=True , _a : Tuple=None , _a : Any=True , _a : Any=[0.48_14_54_66, 0.4_57_82_75, 0.40_82_10_73] , _a : str=[0.26_86_29_54, 0.26_13_02_58, 0.27_57_77_11] , _a : List[Any]=True , ) -> Optional[int]: """simple docstring""" _SCREAMING_SNAKE_CASE =size if size is not None else {'''height''': 224, '''width''': 224} _SCREAMING_SNAKE_CASE =crop_size if crop_size is not None else {'''height''': 18, '''width''': 18} _SCREAMING_SNAKE_CASE =parent _SCREAMING_SNAKE_CASE =batch_size _SCREAMING_SNAKE_CASE =num_channels _SCREAMING_SNAKE_CASE =image_size _SCREAMING_SNAKE_CASE =min_resolution _SCREAMING_SNAKE_CASE =max_resolution _SCREAMING_SNAKE_CASE =do_resize _SCREAMING_SNAKE_CASE =size _SCREAMING_SNAKE_CASE =do_center_crop _SCREAMING_SNAKE_CASE =crop_size _SCREAMING_SNAKE_CASE =do_normalize _SCREAMING_SNAKE_CASE =image_mean _SCREAMING_SNAKE_CASE =image_std _SCREAMING_SNAKE_CASE =do_convert_rgb def __UpperCamelCase ( self : Any ) -> Tuple: """simple docstring""" return { "do_resize": self.do_resize, "size": self.size, "do_center_crop": self.do_center_crop, "crop_size": self.crop_size, "do_normalize": self.do_normalize, "image_mean": self.image_mean, "image_std": self.image_std, "do_convert_rgb": self.do_convert_rgb, } def __UpperCamelCase ( self : Tuple , _a : Optional[Any]=False , _a : str=False , _a : Dict=False ) -> Dict: """simple docstring""" assert not (numpify and torchify), "You cannot specify both numpy and PyTorch tensors at the same time" if equal_resolution: _SCREAMING_SNAKE_CASE =[] for i in range(self.batch_size ): image_inputs.append( np.random.randint( 255 , size=(self.num_channels, self.max_resolution, self.max_resolution) , dtype=np.uinta ) ) else: _SCREAMING_SNAKE_CASE =[] for i in range(self.batch_size ): _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE =np.random.choice(np.arange(self.min_resolution , self.max_resolution ) , 2 ) image_inputs.append(np.random.randint(255 , size=(self.num_channels, width, height) , dtype=np.uinta ) ) if not numpify and not torchify: # PIL expects the channel dimension as last dimension _SCREAMING_SNAKE_CASE =[Image.fromarray(np.moveaxis(_a , 0 , -1 ) ) for x in image_inputs] if torchify: _SCREAMING_SNAKE_CASE =[torch.from_numpy(_a ) for x in image_inputs] return image_inputs @require_torch @require_vision class A__ ( UpperCamelCase__ , unittest.TestCase ): UpperCAmelCase = ChineseCLIPImageProcessor if is_vision_available() else None def __UpperCamelCase ( self : Any ) -> Tuple: """simple docstring""" _SCREAMING_SNAKE_CASE =ChineseCLIPImageProcessingTester(self , do_center_crop=_a ) @property def __UpperCamelCase ( self : Union[str, Any] ) -> Tuple: """simple docstring""" return self.image_processor_tester.prepare_image_processor_dict() def __UpperCamelCase ( self : int ) -> Dict: """simple docstring""" _SCREAMING_SNAKE_CASE =self.image_processing_class(**self.image_processor_dict ) self.assertTrue(hasattr(_a , '''do_resize''' ) ) self.assertTrue(hasattr(_a , '''size''' ) ) self.assertTrue(hasattr(_a , '''do_center_crop''' ) ) self.assertTrue(hasattr(_a , '''center_crop''' ) ) self.assertTrue(hasattr(_a , '''do_normalize''' ) ) self.assertTrue(hasattr(_a , '''image_mean''' ) ) self.assertTrue(hasattr(_a , '''image_std''' ) ) self.assertTrue(hasattr(_a , '''do_convert_rgb''' ) ) def __UpperCamelCase ( self : List[str] ) -> str: """simple docstring""" _SCREAMING_SNAKE_CASE =self.image_processing_class.from_dict(self.image_processor_dict ) self.assertEqual(image_processor.size , {'''height''': 224, '''width''': 224} ) self.assertEqual(image_processor.crop_size , {'''height''': 18, '''width''': 18} ) _SCREAMING_SNAKE_CASE =self.image_processing_class.from_dict(self.image_processor_dict , size=42 , crop_size=84 ) self.assertEqual(image_processor.size , {'''shortest_edge''': 42} ) self.assertEqual(image_processor.crop_size , {'''height''': 84, '''width''': 84} ) def __UpperCamelCase ( self : Union[str, Any] ) -> int: """simple docstring""" pass def __UpperCamelCase ( self : str ) -> List[str]: """simple docstring""" _SCREAMING_SNAKE_CASE =self.image_processing_class(**self.image_processor_dict ) # create random PIL images _SCREAMING_SNAKE_CASE =self.image_processor_tester.prepare_inputs(equal_resolution=_a ) for image in image_inputs: self.assertIsInstance(_a , Image.Image ) # Test not batched input _SCREAMING_SNAKE_CASE =image_processing(image_inputs[0] , return_tensors='''pt''' ).pixel_values self.assertEqual( encoded_images.shape , ( 1, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size['''height'''], self.image_processor_tester.crop_size['''width'''], ) , ) # Test batched _SCREAMING_SNAKE_CASE =image_processing(_a , return_tensors='''pt''' ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size['''height'''], self.image_processor_tester.crop_size['''width'''], ) , ) def __UpperCamelCase ( self : Optional[Any] ) -> List[str]: """simple docstring""" _SCREAMING_SNAKE_CASE =self.image_processing_class(**self.image_processor_dict ) # create random numpy tensors _SCREAMING_SNAKE_CASE =self.image_processor_tester.prepare_inputs(equal_resolution=_a , numpify=_a ) for image in image_inputs: self.assertIsInstance(_a , np.ndarray ) # Test not batched input _SCREAMING_SNAKE_CASE =image_processing(image_inputs[0] , return_tensors='''pt''' ).pixel_values self.assertEqual( encoded_images.shape , ( 1, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size['''height'''], self.image_processor_tester.crop_size['''width'''], ) , ) # Test batched _SCREAMING_SNAKE_CASE =image_processing(_a , return_tensors='''pt''' ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size['''height'''], self.image_processor_tester.crop_size['''width'''], ) , ) def __UpperCamelCase ( self : Optional[int] ) -> Tuple: """simple docstring""" _SCREAMING_SNAKE_CASE =self.image_processing_class(**self.image_processor_dict ) # create random PyTorch tensors _SCREAMING_SNAKE_CASE =self.image_processor_tester.prepare_inputs(equal_resolution=_a , torchify=_a ) for image in image_inputs: self.assertIsInstance(_a , torch.Tensor ) # Test not batched input _SCREAMING_SNAKE_CASE =image_processing(image_inputs[0] , return_tensors='''pt''' ).pixel_values self.assertEqual( encoded_images.shape , ( 1, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size['''height'''], self.image_processor_tester.crop_size['''width'''], ) , ) # Test batched _SCREAMING_SNAKE_CASE =image_processing(_a , return_tensors='''pt''' ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size['''height'''], self.image_processor_tester.crop_size['''width'''], ) , ) @require_torch @require_vision class A__ ( UpperCamelCase__ , unittest.TestCase ): UpperCAmelCase = ChineseCLIPImageProcessor if is_vision_available() else None def __UpperCamelCase ( self : int ) -> str: """simple docstring""" _SCREAMING_SNAKE_CASE =ChineseCLIPImageProcessingTester(self , num_channels=4 , do_center_crop=_a ) _SCREAMING_SNAKE_CASE =3 @property def __UpperCamelCase ( self : Optional[int] ) -> Tuple: """simple docstring""" return self.image_processor_tester.prepare_image_processor_dict() def __UpperCamelCase ( self : int ) -> List[Any]: """simple docstring""" _SCREAMING_SNAKE_CASE =self.image_processing_class(**self.image_processor_dict ) self.assertTrue(hasattr(_a , '''do_resize''' ) ) self.assertTrue(hasattr(_a , '''size''' ) ) self.assertTrue(hasattr(_a , '''do_center_crop''' ) ) self.assertTrue(hasattr(_a , '''center_crop''' ) ) self.assertTrue(hasattr(_a , '''do_normalize''' ) ) self.assertTrue(hasattr(_a , '''image_mean''' ) ) self.assertTrue(hasattr(_a , '''image_std''' ) ) self.assertTrue(hasattr(_a , '''do_convert_rgb''' ) ) def __UpperCamelCase ( self : Any ) -> Union[str, Any]: """simple docstring""" pass def __UpperCamelCase ( self : Dict ) -> Any: """simple docstring""" _SCREAMING_SNAKE_CASE =self.image_processing_class(**self.image_processor_dict ) # create random PIL images _SCREAMING_SNAKE_CASE =self.image_processor_tester.prepare_inputs(equal_resolution=_a ) for image in image_inputs: self.assertIsInstance(_a , Image.Image ) # Test not batched input _SCREAMING_SNAKE_CASE =image_processing(image_inputs[0] , return_tensors='''pt''' ).pixel_values self.assertEqual( encoded_images.shape , ( 1, self.expected_encoded_image_num_channels, self.image_processor_tester.crop_size['''height'''], self.image_processor_tester.crop_size['''width'''], ) , ) # Test batched _SCREAMING_SNAKE_CASE =image_processing(_a , return_tensors='''pt''' ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.expected_encoded_image_num_channels, self.image_processor_tester.crop_size['''height'''], self.image_processor_tester.crop_size['''width'''], ) , )
691
0
"""simple docstring""" from __future__ import annotations from collections.abc import Iterator class lowerCamelCase__ : """simple docstring""" def __init__( self : Any , UpperCamelCase : int ): '''simple docstring''' __UpperCAmelCase : Union[str, Any] = value __UpperCAmelCase : Union[str, Any] = None __UpperCAmelCase : List[str] = None class lowerCamelCase__ : """simple docstring""" def __init__( self : List[Any] , UpperCamelCase : Node ): '''simple docstring''' __UpperCAmelCase : Tuple = tree def lowerCamelCase__ ( self : Union[str, Any] , UpperCamelCase : Node | None ): '''simple docstring''' if node is None: return 0 return node.value + ( self.depth_first_search(node.left ) + self.depth_first_search(node.right ) ) def __iter__( self : int ): '''simple docstring''' yield self.depth_first_search(self.tree ) if __name__ == "__main__": import doctest doctest.testmod()
139
def lowerCamelCase( a__ ,a__): return int((input_a, input_a).count(0) == 0) def lowerCamelCase( ): assert and_gate(0 ,0) == 0 assert and_gate(0 ,1) == 0 assert and_gate(1 ,0) == 0 assert and_gate(1 ,1) == 1 if __name__ == "__main__": test_and_gate() print(and_gate(1, 0)) print(and_gate(0, 0)) print(and_gate(0, 1)) print(and_gate(1, 1))
691
0
import inspect import unittest import warnings from transformers import DeiTConfig from transformers.models.auto import get_values from transformers.testing_utils import ( require_accelerate, require_torch, require_torch_gpu, require_vision, slow, torch_device, ) from transformers.utils import cached_property, is_torch_available, is_vision_available from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from torch import nn from transformers import ( MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING, MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING, MODEL_MAPPING, DeiTForImageClassification, DeiTForImageClassificationWithTeacher, DeiTForMaskedImageModeling, DeiTModel, ) from transformers.models.deit.modeling_deit import DEIT_PRETRAINED_MODEL_ARCHIVE_LIST if is_vision_available(): from PIL import Image from transformers import DeiTImageProcessor class _UpperCamelCase : '''simple docstring''' def __init__( self : List[str] , _lowerCamelCase : str , _lowerCamelCase : List[str]=1_3 , _lowerCamelCase : Tuple=3_0 , _lowerCamelCase : Any=2 , _lowerCamelCase : str=3 , _lowerCamelCase : Tuple=True , _lowerCamelCase : int=True , _lowerCamelCase : List[str]=3_2 , _lowerCamelCase : int=5 , _lowerCamelCase : List[str]=4 , _lowerCamelCase : Union[str, Any]=3_7 , _lowerCamelCase : Optional[int]="gelu" , _lowerCamelCase : str=0.1 , _lowerCamelCase : Optional[int]=0.1 , _lowerCamelCase : Any=1_0 , _lowerCamelCase : Dict=0.02 , _lowerCamelCase : List[Any]=3 , _lowerCamelCase : Union[str, Any]=None , _lowerCamelCase : List[Any]=2 , ): '''simple docstring''' __lowerCamelCase : int = parent __lowerCamelCase : Optional[Any] = batch_size __lowerCamelCase : List[Any] = image_size __lowerCamelCase : Any = patch_size __lowerCamelCase : Optional[int] = num_channels __lowerCamelCase : int = is_training __lowerCamelCase : Tuple = use_labels __lowerCamelCase : Optional[Any] = hidden_size __lowerCamelCase : Tuple = num_hidden_layers __lowerCamelCase : Union[str, Any] = num_attention_heads __lowerCamelCase : Any = intermediate_size __lowerCamelCase : Dict = hidden_act __lowerCamelCase : Tuple = hidden_dropout_prob __lowerCamelCase : List[str] = attention_probs_dropout_prob __lowerCamelCase : Any = type_sequence_label_size __lowerCamelCase : Optional[int] = initializer_range __lowerCamelCase : Union[str, Any] = scope __lowerCamelCase : Any = encoder_stride # in DeiT, the seq length equals the number of patches + 2 (we add 2 for the [CLS] and distilation tokens) __lowerCamelCase : List[str] = (image_size // patch_size) ** 2 __lowerCamelCase : List[Any] = num_patches + 2 def _snake_case ( self : Optional[Any] ): '''simple docstring''' __lowerCamelCase : Optional[int] = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) __lowerCamelCase : Optional[int] = None if self.use_labels: __lowerCamelCase : str = ids_tensor([self.batch_size] , self.type_sequence_label_size ) __lowerCamelCase : List[str] = self.get_config() return config, pixel_values, labels def _snake_case ( self : str ): '''simple docstring''' return DeiTConfig( image_size=self.image_size , patch_size=self.patch_size , num_channels=self.num_channels , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , is_decoder=_a , initializer_range=self.initializer_range , encoder_stride=self.encoder_stride , ) def _snake_case ( self : Optional[Any] , _lowerCamelCase : List[Any] , _lowerCamelCase : Optional[int] , _lowerCamelCase : Any ): '''simple docstring''' __lowerCamelCase : Optional[Any] = DeiTModel(config=_a ) model.to(_a ) model.eval() __lowerCamelCase : Any = model(_a ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def _snake_case ( self : str , _lowerCamelCase : List[Any] , _lowerCamelCase : int , _lowerCamelCase : List[str] ): '''simple docstring''' __lowerCamelCase : Union[str, Any] = DeiTForMaskedImageModeling(config=_a ) model.to(_a ) model.eval() __lowerCamelCase : List[Any] = model(_a ) self.parent.assertEqual( result.reconstruction.shape , (self.batch_size, self.num_channels, self.image_size, self.image_size) ) # test greyscale images __lowerCamelCase : Tuple = 1 __lowerCamelCase : Tuple = DeiTForMaskedImageModeling(_a ) model.to(_a ) model.eval() __lowerCamelCase : Union[str, Any] = floats_tensor([self.batch_size, 1, self.image_size, self.image_size] ) __lowerCamelCase : str = model(_a ) self.parent.assertEqual(result.reconstruction.shape , (self.batch_size, 1, self.image_size, self.image_size) ) def _snake_case ( self : Optional[Any] , _lowerCamelCase : List[Any] , _lowerCamelCase : int , _lowerCamelCase : str ): '''simple docstring''' __lowerCamelCase : Tuple = self.type_sequence_label_size __lowerCamelCase : Any = DeiTForImageClassification(_a ) model.to(_a ) model.eval() __lowerCamelCase : Union[str, Any] = model(_a , labels=_a ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.type_sequence_label_size) ) # test greyscale images __lowerCamelCase : Union[str, Any] = 1 __lowerCamelCase : str = DeiTForImageClassification(_a ) model.to(_a ) model.eval() __lowerCamelCase : List[Any] = floats_tensor([self.batch_size, 1, self.image_size, self.image_size] ) __lowerCamelCase : List[str] = model(_a , labels=_a ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.type_sequence_label_size) ) def _snake_case ( self : Optional[int] ): '''simple docstring''' __lowerCamelCase : str = self.prepare_config_and_inputs() ( ( __lowerCamelCase ) , ( __lowerCamelCase ) , ( __lowerCamelCase ) , ) : str = config_and_inputs __lowerCamelCase : Optional[int] = {"""pixel_values""": pixel_values} return config, inputs_dict @require_torch class _UpperCamelCase ( UpperCamelCase__,UpperCamelCase__,unittest.TestCase ): '''simple docstring''' a_ : str = ( ( DeiTModel, DeiTForImageClassification, DeiTForImageClassificationWithTeacher, DeiTForMaskedImageModeling, ) if is_torch_available() else () ) a_ : Optional[int] = ( { "feature-extraction": DeiTModel, "image-classification": (DeiTForImageClassification, DeiTForImageClassificationWithTeacher), } if is_torch_available() else {} ) a_ : str = False a_ : int = False a_ : List[Any] = False def _snake_case ( self : Any ): '''simple docstring''' __lowerCamelCase : Union[str, Any] = DeiTModelTester(self ) __lowerCamelCase : Optional[Any] = ConfigTester(self , config_class=_a , has_text_modality=_a , hidden_size=3_7 ) def _snake_case ( self : Any ): '''simple docstring''' self.config_tester.run_common_tests() @unittest.skip(reason="""DeiT does not use inputs_embeds""" ) def _snake_case ( self : List[str] ): '''simple docstring''' pass def _snake_case ( self : Dict ): '''simple docstring''' __lowerCamelCase , __lowerCamelCase : str = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: __lowerCamelCase : Union[str, Any] = model_class(_a ) self.assertIsInstance(model.get_input_embeddings() , (nn.Module) ) __lowerCamelCase : str = model.get_output_embeddings() self.assertTrue(x is None or isinstance(_a , nn.Linear ) ) def _snake_case ( self : Tuple ): '''simple docstring''' __lowerCamelCase , __lowerCamelCase : Optional[Any] = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: __lowerCamelCase : int = model_class(_a ) __lowerCamelCase : Optional[int] = inspect.signature(model.forward ) # signature.parameters is an OrderedDict => so arg_names order is deterministic __lowerCamelCase : List[str] = [*signature.parameters.keys()] __lowerCamelCase : List[str] = ["""pixel_values"""] self.assertListEqual(arg_names[:1] , _a ) def _snake_case ( self : List[str] ): '''simple docstring''' __lowerCamelCase : List[Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*_a ) def _snake_case ( self : Any ): '''simple docstring''' __lowerCamelCase : Tuple = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_masked_image_modeling(*_a ) def _snake_case ( self : int ): '''simple docstring''' __lowerCamelCase : Dict = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_image_classification(*_a ) def _snake_case ( self : Union[str, Any] , _lowerCamelCase : int , _lowerCamelCase : Optional[Any] , _lowerCamelCase : List[Any]=False ): '''simple docstring''' __lowerCamelCase : Optional[int] = super()._prepare_for_class(_a , _a , return_labels=_a ) if return_labels: if model_class.__name__ == "DeiTForImageClassificationWithTeacher": del inputs_dict["labels"] return inputs_dict def _snake_case ( self : Tuple ): '''simple docstring''' if not self.model_tester.is_training: return __lowerCamelCase , __lowerCamelCase : Optional[int] = self.model_tester.prepare_config_and_inputs_for_common() __lowerCamelCase : Optional[Any] = True for model_class in self.all_model_classes: # DeiTForImageClassificationWithTeacher supports inference-only if ( model_class in get_values(_a ) or model_class.__name__ == "DeiTForImageClassificationWithTeacher" ): continue __lowerCamelCase : Optional[Any] = model_class(_a ) model.to(_a ) model.train() __lowerCamelCase : Tuple = self._prepare_for_class(_a , _a , return_labels=_a ) __lowerCamelCase : Dict = model(**_a ).loss loss.backward() def _snake_case ( self : List[str] ): '''simple docstring''' __lowerCamelCase , __lowerCamelCase : int = self.model_tester.prepare_config_and_inputs_for_common() if not self.model_tester.is_training: return __lowerCamelCase : Optional[Any] = False __lowerCamelCase : Optional[Any] = True for model_class in self.all_model_classes: if model_class in get_values(_a ) or not model_class.supports_gradient_checkpointing: continue # DeiTForImageClassificationWithTeacher supports inference-only if model_class.__name__ == "DeiTForImageClassificationWithTeacher": continue __lowerCamelCase : Optional[Any] = model_class(_a ) model.gradient_checkpointing_enable() model.to(_a ) model.train() __lowerCamelCase : Any = self._prepare_for_class(_a , _a , return_labels=_a ) __lowerCamelCase : Optional[int] = model(**_a ).loss loss.backward() def _snake_case ( self : Optional[Any] ): '''simple docstring''' __lowerCamelCase , __lowerCamelCase : Optional[Any] = self.model_tester.prepare_config_and_inputs_for_common() __lowerCamelCase : Tuple = [ {"""title""": """multi_label_classification""", """num_labels""": 2, """dtype""": torch.float}, {"""title""": """single_label_classification""", """num_labels""": 1, """dtype""": torch.long}, {"""title""": """regression""", """num_labels""": 1, """dtype""": torch.float}, ] for model_class in self.all_model_classes: if ( model_class not in [ *get_values(_a ), *get_values(_a ), ] or model_class.__name__ == "DeiTForImageClassificationWithTeacher" ): continue for problem_type in problem_types: with self.subTest(msg=F"""Testing {model_class} with {problem_type["title"]}""" ): __lowerCamelCase : str = problem_type["""title"""] __lowerCamelCase : Tuple = problem_type["""num_labels"""] __lowerCamelCase : Optional[int] = model_class(_a ) model.to(_a ) model.train() __lowerCamelCase : Dict = self._prepare_for_class(_a , _a , return_labels=_a ) if problem_type["num_labels"] > 1: __lowerCamelCase : List[str] = inputs["""labels"""].unsqueeze(1 ).repeat(1 , problem_type["""num_labels"""] ) __lowerCamelCase : List[str] = inputs["""labels"""].to(problem_type["""dtype"""] ) # This tests that we do not trigger the warning form PyTorch "Using a target size that is different # to the input size. This will likely lead to incorrect results due to broadcasting. Please ensure # they have the same size." which is a symptom something in wrong for the regression problem. # See https://github.com/huggingface/transformers/issues/11780 with warnings.catch_warnings(record=_a ) as warning_list: __lowerCamelCase : str = model(**_a ).loss for w in warning_list: if "Using a target size that is different to the input size" in str(w.message ): raise ValueError( F"""Something is going wrong in the regression problem: intercepted {w.message}""" ) loss.backward() @slow def _snake_case ( self : int ): '''simple docstring''' for model_name in DEIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: __lowerCamelCase : Union[str, Any] = DeiTModel.from_pretrained(_a ) self.assertIsNotNone(_a ) def _UpperCAmelCase ( ): """simple docstring""" __lowerCamelCase : Any = Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png""" ) return image @require_torch @require_vision class _UpperCamelCase ( unittest.TestCase ): '''simple docstring''' @cached_property def _snake_case ( self : Tuple ): '''simple docstring''' return ( DeiTImageProcessor.from_pretrained("""facebook/deit-base-distilled-patch16-224""" ) if is_vision_available() else None ) @slow def _snake_case ( self : Any ): '''simple docstring''' __lowerCamelCase : Union[str, Any] = DeiTForImageClassificationWithTeacher.from_pretrained("""facebook/deit-base-distilled-patch16-224""" ).to( _a ) __lowerCamelCase : List[str] = self.default_image_processor __lowerCamelCase : Optional[int] = prepare_img() __lowerCamelCase : str = image_processor(images=_a , return_tensors="""pt""" ).to(_a ) # forward pass with torch.no_grad(): __lowerCamelCase : Any = model(**_a ) # verify the logits __lowerCamelCase : List[Any] = torch.Size((1, 1_0_0_0) ) self.assertEqual(outputs.logits.shape , _a ) __lowerCamelCase : Tuple = torch.tensor([-1.0_266, 0.1_912, -1.2_861] ).to(_a ) self.assertTrue(torch.allclose(outputs.logits[0, :3] , _a , atol=1E-4 ) ) @slow @require_accelerate @require_torch_gpu def _snake_case ( self : Dict ): '''simple docstring''' __lowerCamelCase : Union[str, Any] = DeiTModel.from_pretrained( """facebook/deit-base-distilled-patch16-224""" , torch_dtype=torch.floataa , device_map="""auto""" ) __lowerCamelCase : List[Any] = self.default_image_processor __lowerCamelCase : Optional[int] = prepare_img() __lowerCamelCase : List[Any] = image_processor(images=_a , return_tensors="""pt""" ) __lowerCamelCase : Any = inputs.pixel_values.to(_a ) # forward pass to make sure inference works in fp16 with torch.no_grad(): __lowerCamelCase : List[str] = model(_a )
519
import argparse import os import sys from unittest.mock import patch import pytorch_lightning as pl import timeout_decorator import torch from distillation import SummarizationDistiller, distill_main from finetune import SummarizationModule, main from transformers import MarianMTModel from transformers.file_utils import cached_path from transformers.testing_utils import TestCasePlus, require_torch_gpu, slow from utils import load_json snake_case_ : Optional[int] = '''sshleifer/mar_enro_6_3_student''' class A__ ( UpperCamelCase__ ): def __UpperCamelCase ( self : Any ) -> Any: """simple docstring""" super().setUp() _SCREAMING_SNAKE_CASE =cached_path( '''https://cdn-datasets.huggingface.co/translation/wmt_en_ro-tr40k-va0.5k-te0.5k.tar.gz''' , extract_compressed_file=_a , ) _SCREAMING_SNAKE_CASE =f"{data_cached}/wmt_en_ro-tr40k-va0.5k-te0.5k" @slow @require_torch_gpu def __UpperCamelCase ( self : Optional[Any] ) -> Union[str, Any]: """simple docstring""" MarianMTModel.from_pretrained(_a ) @slow @require_torch_gpu def __UpperCamelCase ( self : str ) -> Dict: """simple docstring""" _SCREAMING_SNAKE_CASE ={ '''$MAX_LEN''': 64, '''$BS''': 64, '''$GAS''': 1, '''$ENRO_DIR''': self.data_dir, '''facebook/mbart-large-cc25''': MARIAN_MODEL, # "val_check_interval=0.25": "val_check_interval=1.0", '''--learning_rate=3e-5''': '''--learning_rate 3e-4''', '''--num_train_epochs 6''': '''--num_train_epochs 1''', } # Clean up bash script _SCREAMING_SNAKE_CASE =(self.test_file_dir / '''train_mbart_cc25_enro.sh''').open().read().split('''finetune.py''' )[1].strip() _SCREAMING_SNAKE_CASE =bash_script.replace('''\\\n''' , '''''' ).strip().replace('''"$@"''' , '''''' ) for k, v in env_vars_to_replace.items(): _SCREAMING_SNAKE_CASE =bash_script.replace(_a , str(_a ) ) _SCREAMING_SNAKE_CASE =self.get_auto_remove_tmp_dir() # bash_script = bash_script.replace("--fp16 ", "") _SCREAMING_SNAKE_CASE =f"\n --output_dir {output_dir}\n --tokenizer_name Helsinki-NLP/opus-mt-en-ro\n --sortish_sampler\n --do_predict\n --gpus 1\n --freeze_encoder\n --n_train 40000\n --n_val 500\n --n_test 500\n --fp16_opt_level O1\n --num_sanity_val_steps 0\n --eval_beams 2\n ".split() # XXX: args.gpus > 1 : handle multi_gpu in the future _SCREAMING_SNAKE_CASE =['''finetune.py'''] + bash_script.split() + args with patch.object(_a , '''argv''' , _a ): _SCREAMING_SNAKE_CASE =argparse.ArgumentParser() _SCREAMING_SNAKE_CASE =pl.Trainer.add_argparse_args(_a ) _SCREAMING_SNAKE_CASE =SummarizationModule.add_model_specific_args(_a , os.getcwd() ) _SCREAMING_SNAKE_CASE =parser.parse_args() _SCREAMING_SNAKE_CASE =main(_a ) # Check metrics _SCREAMING_SNAKE_CASE =load_json(model.metrics_save_path ) _SCREAMING_SNAKE_CASE =metrics['''val'''][0] _SCREAMING_SNAKE_CASE =metrics['''val'''][-1] self.assertEqual(len(metrics['''val'''] ) , (args.max_epochs / args.val_check_interval) ) assert isinstance(last_step_stats[f"val_avg_{model.val_metric}"] , _a ) self.assertGreater(last_step_stats['''val_avg_gen_time'''] , 0.01 ) # model hanging on generate. Maybe bad config was saved. (XXX: old comment/assert?) self.assertLessEqual(last_step_stats['''val_avg_gen_time'''] , 1.0 ) # test learning requirements: # 1. BLEU improves over the course of training by more than 2 pts self.assertGreater(last_step_stats['''val_avg_bleu'''] - first_step_stats['''val_avg_bleu'''] , 2 ) # 2. BLEU finishes above 17 self.assertGreater(last_step_stats['''val_avg_bleu'''] , 17 ) # 3. test BLEU and val BLEU within ~1.1 pt. self.assertLess(abs(metrics['''val'''][-1]['''val_avg_bleu'''] - metrics['''test'''][-1]['''test_avg_bleu'''] ) , 1.1 ) # check lightning ckpt can be loaded and has a reasonable statedict _SCREAMING_SNAKE_CASE =os.listdir(_a ) _SCREAMING_SNAKE_CASE =[x for x in contents if x.endswith('''.ckpt''' )][0] _SCREAMING_SNAKE_CASE =os.path.join(args.output_dir , _a ) _SCREAMING_SNAKE_CASE =torch.load(_a , map_location='''cpu''' ) _SCREAMING_SNAKE_CASE ='''model.model.decoder.layers.0.encoder_attn_layer_norm.weight''' assert expected_key in ckpt["state_dict"] assert ckpt["state_dict"]["model.model.decoder.layers.0.encoder_attn_layer_norm.weight"].dtype == torch.floataa # TODO: turn on args.do_predict when PL bug fixed. if args.do_predict: _SCREAMING_SNAKE_CASE ={os.path.basename(_a ) for p in contents} assert "test_generations.txt" in contents assert "test_results.txt" in contents # assert len(metrics["val"]) == desired_n_evals assert len(metrics['''test'''] ) == 1 class A__ ( UpperCamelCase__ ): @timeout_decorator.timeout(600 ) @slow @require_torch_gpu def __UpperCamelCase ( self : Union[str, Any] ) -> str: """simple docstring""" _SCREAMING_SNAKE_CASE =f"{self.test_file_dir_str}/test_data/wmt_en_ro" _SCREAMING_SNAKE_CASE ={ '''--fp16_opt_level=O1''': '''''', '''$MAX_LEN''': 128, '''$BS''': 16, '''$GAS''': 1, '''$ENRO_DIR''': data_dir, '''$m''': '''sshleifer/student_marian_en_ro_6_1''', '''val_check_interval=0.25''': '''val_check_interval=1.0''', } # Clean up bash script _SCREAMING_SNAKE_CASE =( (self.test_file_dir / '''distil_marian_no_teacher.sh''').open().read().split('''distillation.py''' )[1].strip() ) _SCREAMING_SNAKE_CASE =bash_script.replace('''\\\n''' , '''''' ).strip().replace('''"$@"''' , '''''' ) _SCREAMING_SNAKE_CASE =bash_script.replace('''--fp16 ''' , ''' ''' ) for k, v in env_vars_to_replace.items(): _SCREAMING_SNAKE_CASE =bash_script.replace(_a , str(_a ) ) _SCREAMING_SNAKE_CASE =self.get_auto_remove_tmp_dir() _SCREAMING_SNAKE_CASE =bash_script.replace('''--fp16''' , '''''' ) _SCREAMING_SNAKE_CASE =6 _SCREAMING_SNAKE_CASE =( ['''distillation.py'''] + bash_script.split() + [ f"--output_dir={output_dir}", '''--gpus=1''', '''--learning_rate=1e-3''', f"--num_train_epochs={epochs}", '''--warmup_steps=10''', '''--val_check_interval=1.0''', '''--do_predict''', ] ) with patch.object(_a , '''argv''' , _a ): _SCREAMING_SNAKE_CASE =argparse.ArgumentParser() _SCREAMING_SNAKE_CASE =pl.Trainer.add_argparse_args(_a ) _SCREAMING_SNAKE_CASE =SummarizationDistiller.add_model_specific_args(_a , os.getcwd() ) _SCREAMING_SNAKE_CASE =parser.parse_args() # assert args.gpus == gpus THIS BREAKS for multi_gpu _SCREAMING_SNAKE_CASE =distill_main(_a ) # Check metrics _SCREAMING_SNAKE_CASE =load_json(model.metrics_save_path ) _SCREAMING_SNAKE_CASE =metrics['''val'''][0] _SCREAMING_SNAKE_CASE =metrics['''val'''][-1] assert len(metrics['''val'''] ) >= (args.max_epochs / args.val_check_interval) # +1 accounts for val_sanity_check assert last_step_stats["val_avg_gen_time"] >= 0.01 assert first_step_stats["val_avg_bleu"] < last_step_stats["val_avg_bleu"] # model learned nothing assert 1.0 >= last_step_stats["val_avg_gen_time"] # model hanging on generate. Maybe bad config was saved. assert isinstance(last_step_stats[f"val_avg_{model.val_metric}"] , _a ) # check lightning ckpt can be loaded and has a reasonable statedict _SCREAMING_SNAKE_CASE =os.listdir(_a ) _SCREAMING_SNAKE_CASE =[x for x in contents if x.endswith('''.ckpt''' )][0] _SCREAMING_SNAKE_CASE =os.path.join(args.output_dir , _a ) _SCREAMING_SNAKE_CASE =torch.load(_a , map_location='''cpu''' ) _SCREAMING_SNAKE_CASE ='''model.model.decoder.layers.0.encoder_attn_layer_norm.weight''' assert expected_key in ckpt["state_dict"] assert ckpt["state_dict"]["model.model.decoder.layers.0.encoder_attn_layer_norm.weight"].dtype == torch.floataa # TODO: turn on args.do_predict when PL bug fixed. if args.do_predict: _SCREAMING_SNAKE_CASE ={os.path.basename(_a ) for p in contents} assert "test_generations.txt" in contents assert "test_results.txt" in contents # assert len(metrics["val"]) == desired_n_evals assert len(metrics['''test'''] ) == 1
691
0
import argparse import os import sys from unittest.mock import patch import pytorch_lightning as pl import timeout_decorator import torch from distillation import SummarizationDistiller, distill_main from finetune import SummarizationModule, main from transformers import MarianMTModel from transformers.file_utils import cached_path from transformers.testing_utils import TestCasePlus, require_torch_gpu, slow from utils import load_json SCREAMING_SNAKE_CASE : Optional[int] = '''sshleifer/mar_enro_6_3_student''' class A_ ( UpperCamelCase__ ): def _UpperCAmelCase ( self : Any ): super().setUp() __a = cached_path( "https://cdn-datasets.huggingface.co/translation/wmt_en_ro-tr40k-va0.5k-te0.5k.tar.gz" , extract_compressed_file=_a , ) __a = f"""{data_cached}/wmt_en_ro-tr40k-va0.5k-te0.5k""" @slow @require_torch_gpu def _UpperCAmelCase ( self : Optional[Any] ): MarianMTModel.from_pretrained(_a ) @slow @require_torch_gpu def _UpperCAmelCase ( self : str ): __a = { "$MAX_LEN": 64, "$BS": 64, "$GAS": 1, "$ENRO_DIR": self.data_dir, "facebook/mbart-large-cc25": MARIAN_MODEL, # "val_check_interval=0.25": "val_check_interval=1.0", "--learning_rate=3e-5": "--learning_rate 3e-4", "--num_train_epochs 6": "--num_train_epochs 1", } # Clean up bash script __a = (self.test_file_dir / "train_mbart_cc25_enro.sh").open().read().split("finetune.py" )[1].strip() __a = bash_script.replace("\\\n" , "" ).strip().replace("\"$@\"" , "" ) for k, v in env_vars_to_replace.items(): __a = bash_script.replace(_a , str(_a ) ) __a = self.get_auto_remove_tmp_dir() # bash_script = bash_script.replace("--fp16 ", "") __a = f"""\n --output_dir {output_dir}\n --tokenizer_name Helsinki-NLP/opus-mt-en-ro\n --sortish_sampler\n --do_predict\n --gpus 1\n --freeze_encoder\n --n_train 40000\n --n_val 500\n --n_test 500\n --fp16_opt_level O1\n --num_sanity_val_steps 0\n --eval_beams 2\n """.split() # XXX: args.gpus > 1 : handle multi_gpu in the future __a = ["finetune.py"] + bash_script.split() + args with patch.object(_a , "argv" , _a ): __a = argparse.ArgumentParser() __a = pl.Trainer.add_argparse_args(_a ) __a = SummarizationModule.add_model_specific_args(_a , os.getcwd() ) __a = parser.parse_args() __a = main(_a ) # Check metrics __a = load_json(model.metrics_save_path ) __a = metrics["val"][0] __a = metrics["val"][-1] self.assertEqual(len(metrics["val"] ) , (args.max_epochs / args.val_check_interval) ) assert isinstance(last_step_stats[f"""val_avg_{model.val_metric}"""] , _a ) self.assertGreater(last_step_stats["val_avg_gen_time"] , 0.01 ) # model hanging on generate. Maybe bad config was saved. (XXX: old comment/assert?) self.assertLessEqual(last_step_stats["val_avg_gen_time"] , 1.0 ) # test learning requirements: # 1. BLEU improves over the course of training by more than 2 pts self.assertGreater(last_step_stats["val_avg_bleu"] - first_step_stats["val_avg_bleu"] , 2 ) # 2. BLEU finishes above 17 self.assertGreater(last_step_stats["val_avg_bleu"] , 17 ) # 3. test BLEU and val BLEU within ~1.1 pt. self.assertLess(abs(metrics["val"][-1]["val_avg_bleu"] - metrics["test"][-1]["test_avg_bleu"] ) , 1.1 ) # check lightning ckpt can be loaded and has a reasonable statedict __a = os.listdir(_a ) __a = [x for x in contents if x.endswith(".ckpt" )][0] __a = os.path.join(args.output_dir , _a ) __a = torch.load(_a , map_location="cpu" ) __a = "model.model.decoder.layers.0.encoder_attn_layer_norm.weight" assert expected_key in ckpt["state_dict"] assert ckpt["state_dict"]["model.model.decoder.layers.0.encoder_attn_layer_norm.weight"].dtype == torch.floataa # TODO: turn on args.do_predict when PL bug fixed. if args.do_predict: __a = {os.path.basename(_a ) for p in contents} assert "test_generations.txt" in contents assert "test_results.txt" in contents # assert len(metrics["val"]) == desired_n_evals assert len(metrics["test"] ) == 1 class A_ ( UpperCamelCase__ ): @timeout_decorator.timeout(6_00 ) @slow @require_torch_gpu def _UpperCAmelCase ( self : Union[str, Any] ): __a = f"""{self.test_file_dir_str}/test_data/wmt_en_ro""" __a = { "--fp16_opt_level=O1": "", "$MAX_LEN": 1_28, "$BS": 16, "$GAS": 1, "$ENRO_DIR": data_dir, "$m": "sshleifer/student_marian_en_ro_6_1", "val_check_interval=0.25": "val_check_interval=1.0", } # Clean up bash script __a = ( (self.test_file_dir / "distil_marian_no_teacher.sh").open().read().split("distillation.py" )[1].strip() ) __a = bash_script.replace("\\\n" , "" ).strip().replace("\"$@\"" , "" ) __a = bash_script.replace("--fp16 " , " " ) for k, v in env_vars_to_replace.items(): __a = bash_script.replace(_a , str(_a ) ) __a = self.get_auto_remove_tmp_dir() __a = bash_script.replace("--fp16" , "" ) __a = 6 __a = ( ["distillation.py"] + bash_script.split() + [ f"""--output_dir={output_dir}""", "--gpus=1", "--learning_rate=1e-3", f"""--num_train_epochs={epochs}""", "--warmup_steps=10", "--val_check_interval=1.0", "--do_predict", ] ) with patch.object(_a , "argv" , _a ): __a = argparse.ArgumentParser() __a = pl.Trainer.add_argparse_args(_a ) __a = SummarizationDistiller.add_model_specific_args(_a , os.getcwd() ) __a = parser.parse_args() # assert args.gpus == gpus THIS BREAKS for multi_gpu __a = distill_main(_a ) # Check metrics __a = load_json(model.metrics_save_path ) __a = metrics["val"][0] __a = metrics["val"][-1] assert len(metrics["val"] ) >= (args.max_epochs / args.val_check_interval) # +1 accounts for val_sanity_check assert last_step_stats["val_avg_gen_time"] >= 0.01 assert first_step_stats["val_avg_bleu"] < last_step_stats["val_avg_bleu"] # model learned nothing assert 1.0 >= last_step_stats["val_avg_gen_time"] # model hanging on generate. Maybe bad config was saved. assert isinstance(last_step_stats[f"""val_avg_{model.val_metric}"""] , _a ) # check lightning ckpt can be loaded and has a reasonable statedict __a = os.listdir(_a ) __a = [x for x in contents if x.endswith(".ckpt" )][0] __a = os.path.join(args.output_dir , _a ) __a = torch.load(_a , map_location="cpu" ) __a = "model.model.decoder.layers.0.encoder_attn_layer_norm.weight" assert expected_key in ckpt["state_dict"] assert ckpt["state_dict"]["model.model.decoder.layers.0.encoder_attn_layer_norm.weight"].dtype == torch.floataa # TODO: turn on args.do_predict when PL bug fixed. if args.do_predict: __a = {os.path.basename(_a ) for p in contents} assert "test_generations.txt" in contents assert "test_results.txt" in contents # assert len(metrics["val"]) == desired_n_evals assert len(metrics["test"] ) == 1
197
import inspect import os import unittest from dataclasses import dataclass import torch from accelerate import Accelerator, DistributedDataParallelKwargs, GradScalerKwargs from accelerate.state import AcceleratorState from accelerate.test_utils import execute_subprocess_async, require_cuda, require_multi_gpu from accelerate.utils import KwargsHandler @dataclass class A__ ( UpperCamelCase__ ): UpperCAmelCase = 0 UpperCAmelCase = False UpperCAmelCase = 3.0 class A__ ( unittest.TestCase ): def __UpperCamelCase ( self : Optional[Any] ) -> Optional[Any]: """simple docstring""" self.assertDictEqual(MockClass().to_kwargs() , {} ) self.assertDictEqual(MockClass(a=2 ).to_kwargs() , {'''a''': 2} ) self.assertDictEqual(MockClass(a=2 , b=_a ).to_kwargs() , {'''a''': 2, '''b''': True} ) self.assertDictEqual(MockClass(a=2 , c=2.25 ).to_kwargs() , {'''a''': 2, '''c''': 2.25} ) @require_cuda def __UpperCamelCase ( self : Optional[Any] ) -> Dict: """simple docstring""" _SCREAMING_SNAKE_CASE =GradScalerKwargs(init_scale=1024 , growth_factor=2 ) AcceleratorState._reset_state() _SCREAMING_SNAKE_CASE =Accelerator(mixed_precision='''fp16''' , kwargs_handlers=[scaler_handler] ) print(accelerator.use_fpaa ) _SCREAMING_SNAKE_CASE =accelerator.scaler # Check the kwargs have been applied self.assertEqual(scaler._init_scale , 10_24.0 ) self.assertEqual(scaler._growth_factor , 2.0 ) # Check the other values are at the default self.assertEqual(scaler._backoff_factor , 0.5 ) self.assertEqual(scaler._growth_interval , 2000 ) self.assertEqual(scaler._enabled , _a ) @require_multi_gpu def __UpperCamelCase ( self : str ) -> Tuple: """simple docstring""" _SCREAMING_SNAKE_CASE =['''torchrun''', f"--nproc_per_node={torch.cuda.device_count()}", inspect.getfile(self.__class__ )] execute_subprocess_async(_a , env=os.environ.copy() ) if __name__ == "__main__": snake_case_ : Optional[Any] = DistributedDataParallelKwargs(bucket_cap_mb=15, find_unused_parameters=True) snake_case_ : List[str] = Accelerator(kwargs_handlers=[ddp_scaler]) snake_case_ : Dict = torch.nn.Linear(1_00, 2_00) snake_case_ : List[Any] = accelerator.prepare(model) # Check the values changed in kwargs snake_case_ : Dict = '''''' snake_case_ : str = model.bucket_bytes_cap // (10_24 * 10_24) if observed_bucket_cap_map != 15: error_msg += f"Kwargs badly passed, should have `15` but found {observed_bucket_cap_map}.\n" if model.find_unused_parameters is not True: error_msg += f"Kwargs badly passed, should have `True` but found {model.find_unused_parameters}.\n" # Check the values of the defaults if model.dim != 0: error_msg += f"Default value not respected, should have `0` but found {model.dim}.\n" if model.broadcast_buffers is not True: error_msg += f"Default value not respected, should have `True` but found {model.broadcast_buffers}.\n" if model.gradient_as_bucket_view is not False: error_msg += f"Default value not respected, should have `False` but found {model.gradient_as_bucket_view}.\n" # Raise error at the end to make sure we don't stop at the first failure. if len(error_msg) > 0: raise ValueError(error_msg)
691
0
import enum import shutil import sys UpperCAmelCase__ = shutil.get_terminal_size() UpperCAmelCase__ = {'''UP''': '''A''', '''DOWN''': '''B''', '''RIGHT''': '''C''', '''LEFT''': '''D'''} class lowercase_ ( enum.Enum ): '''simple docstring''' __snake_case = 0 __snake_case = 1 def _a ( a :Optional[Any] , a :List[str]="" ) -> Any: sys.stdout.write(str(a__ ) + end ) sys.stdout.flush() def _a ( a :Union[str, Any] , a :int , a :Union[str, Any]="" ) -> List[Any]: forceWrite(F"""\u001b[{color}m{content}\u001b[0m""" , a__ ) def _a ( ) -> List[str]: forceWrite('''\r''' ) def _a ( a :Optional[Any] , a :Optional[int] ) -> List[str]: forceWrite(F"""\033[{num_lines}{CURSOR_TO_CHAR[direction.upper()]}""" ) def _a ( ) -> Dict: forceWrite(''' ''' * TERMINAL_WIDTH ) reset_cursor() def _a ( ) -> Any: reset_cursor() forceWrite('''-''' * TERMINAL_WIDTH )
117
class A__ : def __init__( self : List[str] ) -> List[str]: """simple docstring""" _SCREAMING_SNAKE_CASE =0 _SCREAMING_SNAKE_CASE =0 _SCREAMING_SNAKE_CASE ={} def __UpperCamelCase ( self : Any , _a : Union[str, Any] ) -> Optional[Any]: """simple docstring""" if vertex not in self.adjacency: _SCREAMING_SNAKE_CASE ={} self.num_vertices += 1 def __UpperCamelCase ( self : Optional[int] , _a : Tuple , _a : Tuple , _a : Dict ) -> Union[str, Any]: """simple docstring""" self.add_vertex(_a ) self.add_vertex(_a ) if head == tail: return _SCREAMING_SNAKE_CASE =weight _SCREAMING_SNAKE_CASE =weight def __UpperCamelCase ( self : Dict ) -> int: """simple docstring""" _SCREAMING_SNAKE_CASE =self.get_edges() for edge in edges: _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE =edge edges.remove((tail, head, weight) ) for i in range(len(_a ) ): _SCREAMING_SNAKE_CASE =list(edges[i] ) edges.sort(key=lambda _a : e[2] ) for i in range(len(_a ) - 1 ): if edges[i][2] >= edges[i + 1][2]: _SCREAMING_SNAKE_CASE =edges[i][2] + 1 for edge in edges: _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE =edge _SCREAMING_SNAKE_CASE =weight _SCREAMING_SNAKE_CASE =weight def __str__( self : str ) -> List[str]: """simple docstring""" _SCREAMING_SNAKE_CASE ='''''' for tail in self.adjacency: for head in self.adjacency[tail]: _SCREAMING_SNAKE_CASE =self.adjacency[head][tail] string += f"{head} -> {tail} == {weight}\n" return string.rstrip('''\n''' ) def __UpperCamelCase ( self : int ) -> Optional[Any]: """simple docstring""" _SCREAMING_SNAKE_CASE =[] for tail in self.adjacency: for head in self.adjacency[tail]: output.append((tail, head, self.adjacency[head][tail]) ) return output def __UpperCamelCase ( self : Any ) -> Any: """simple docstring""" return self.adjacency.keys() @staticmethod def __UpperCamelCase ( _a : List[str]=None , _a : Optional[int]=None ) -> Optional[Any]: """simple docstring""" _SCREAMING_SNAKE_CASE =Graph() if vertices is None: _SCREAMING_SNAKE_CASE =[] if edges is None: _SCREAMING_SNAKE_CASE =[] for vertex in vertices: g.add_vertex(_a ) for edge in edges: g.add_edge(*_a ) return g class A__ : def __init__( self : List[Any] ) -> Union[str, Any]: """simple docstring""" _SCREAMING_SNAKE_CASE ={} _SCREAMING_SNAKE_CASE ={} def __len__( self : Optional[int] ) -> Tuple: """simple docstring""" return len(self.parent ) def __UpperCamelCase ( self : Dict , _a : Optional[Any] ) -> int: """simple docstring""" if item in self.parent: return self.find(_a ) _SCREAMING_SNAKE_CASE =item _SCREAMING_SNAKE_CASE =0 return item def __UpperCamelCase ( self : str , _a : Tuple ) -> Union[str, Any]: """simple docstring""" if item not in self.parent: return self.make_set(_a ) if item != self.parent[item]: _SCREAMING_SNAKE_CASE =self.find(self.parent[item] ) return self.parent[item] def __UpperCamelCase ( self : Dict , _a : Optional[int] , _a : List[Any] ) -> List[str]: """simple docstring""" _SCREAMING_SNAKE_CASE =self.find(_a ) _SCREAMING_SNAKE_CASE =self.find(_a ) if roota == roota: return roota if self.rank[roota] > self.rank[roota]: _SCREAMING_SNAKE_CASE =roota return roota if self.rank[roota] < self.rank[roota]: _SCREAMING_SNAKE_CASE =roota return roota if self.rank[roota] == self.rank[roota]: self.rank[roota] += 1 _SCREAMING_SNAKE_CASE =roota return roota return None @staticmethod def __UpperCamelCase ( _a : int ) -> Union[str, Any]: """simple docstring""" _SCREAMING_SNAKE_CASE =graph.num_vertices _SCREAMING_SNAKE_CASE =Graph.UnionFind() _SCREAMING_SNAKE_CASE =[] while num_components > 1: _SCREAMING_SNAKE_CASE ={} for vertex in graph.get_vertices(): _SCREAMING_SNAKE_CASE =-1 _SCREAMING_SNAKE_CASE =graph.get_edges() for edge in edges: _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE =edge edges.remove((tail, head, weight) ) for edge in edges: _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE =edge _SCREAMING_SNAKE_CASE =union_find.find(_a ) _SCREAMING_SNAKE_CASE =union_find.find(_a ) if seta != seta: if cheap_edge[seta] == -1 or cheap_edge[seta][2] > weight: _SCREAMING_SNAKE_CASE =[head, tail, weight] if cheap_edge[seta] == -1 or cheap_edge[seta][2] > weight: _SCREAMING_SNAKE_CASE =[head, tail, weight] for vertex in cheap_edge: if cheap_edge[vertex] != -1: _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE =cheap_edge[vertex] if union_find.find(_a ) != union_find.find(_a ): union_find.union(_a , _a ) mst_edges.append(cheap_edge[vertex] ) _SCREAMING_SNAKE_CASE =num_components - 1 _SCREAMING_SNAKE_CASE =Graph.build(edges=_a ) return mst
691
0
import importlib import sys from argparse import REMAINDER, ArgumentParser from pathlib import Path import torch_xla.distributed.xla_multiprocessing as xmp def _lowerCAmelCase ( ) -> str: '''simple docstring''' __snake_case = ArgumentParser( description=( "PyTorch TPU distributed training launch helper utility that will spawn up multiple distributed processes" ) ) # Optional arguments for the launch helper parser.add_argument("--num_cores" , type=a__ , default=1 , help="Number of TPU cores to use (1 or 8)." ) # positional parser.add_argument( "training_script" , type=a__ , help=( "The full path to the single TPU training " "program/script to be launched in parallel, " "followed by all the arguments for the " "training script" ) , ) # rest from the training program parser.add_argument("training_script_args" , nargs=a__ ) return parser.parse_args() def _lowerCAmelCase ( ) -> List[Any]: '''simple docstring''' __snake_case = parse_args() # Import training_script as a module. __snake_case = Path(args.training_script ) sys.path.append(str(script_fpath.parent.resolve() ) ) __snake_case = script_fpath.stem __snake_case = importlib.import_module(a__ ) # Patch sys.argv __snake_case = [args.training_script] + args.training_script_args + ["--tpu_num_cores", str(args.num_cores )] xmp.spawn(mod._mp_fn , args=() , nprocs=args.num_cores ) if __name__ == "__main__": main()
371
import logging import os from dataclasses import dataclass, field from typing import Dict, Optional import numpy as np from utils_multiple_choice import MultipleChoiceDataset, Split, processors import transformers from transformers import ( AutoConfig, AutoModelForMultipleChoice, AutoTokenizer, DataCollatorWithPadding, EvalPrediction, HfArgumentParser, Trainer, TrainingArguments, set_seed, ) from transformers.trainer_utils import is_main_process snake_case_ : str = logging.getLogger(__name__) def lowerCamelCase( a__ ,a__): return (preds == labels).mean() @dataclass class A__ : UpperCAmelCase = field( metadata={"help": "Path to pretrained model or model identifier from huggingface.co/models"} ) UpperCAmelCase = field( default=UpperCamelCase__ , metadata={"help": "Pretrained config name or path if not the same as model_name"} ) UpperCAmelCase = field( default=UpperCamelCase__ , metadata={"help": "Pretrained tokenizer name or path if not the same as model_name"} ) UpperCAmelCase = field( default=UpperCamelCase__ , metadata={"help": "Where do you want to store the pretrained models downloaded from huggingface.co"} , ) @dataclass class A__ : UpperCAmelCase = field(metadata={"help": "The name of the task to train on: " + ", ".join(processors.keys() )} ) UpperCAmelCase = field(metadata={"help": "Should contain the data files for the task."} ) UpperCAmelCase = 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." ) } , ) UpperCAmelCase = field( default=UpperCamelCase__ , metadata={"help": "Overwrite the cached training and evaluation sets"} ) def lowerCamelCase( ): # See all possible arguments in src/transformers/training_args.py # or by passing the --help flag to this script. # We now keep distinct sets of args, for a cleaner separation of concerns. _SCREAMING_SNAKE_CASE =HfArgumentParser((ModelArguments, DataTrainingArguments, TrainingArguments)) _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE =parser.parse_args_into_dataclasses() if ( os.path.exists(training_args.output_dir) and os.listdir(training_args.output_dir) and training_args.do_train and not training_args.overwrite_output_dir ): raise ValueError( f"Output directory ({training_args.output_dir}) already exists and is not empty. Use" ''' --overwrite_output_dir to overcome.''') # Setup logging logging.basicConfig( format='''%(asctime)s - %(levelname)s - %(name)s - %(message)s''' ,datefmt='''%m/%d/%Y %H:%M:%S''' ,level=logging.INFO if training_args.local_rank in [-1, 0] else logging.WARN ,) logger.warning( '''Process rank: %s, device: %s, n_gpu: %s, distributed training: %s, 16-bits training: %s''' ,training_args.local_rank ,training_args.device ,training_args.n_gpu ,bool(training_args.local_rank != -1) ,training_args.fpaa ,) # Set the verbosity to info of the Transformers logger (on main process only): if is_main_process(training_args.local_rank): transformers.utils.logging.set_verbosity_info() transformers.utils.logging.enable_default_handler() transformers.utils.logging.enable_explicit_format() logger.info('''Training/evaluation parameters %s''' ,a__) # Set seed set_seed(training_args.seed) try: _SCREAMING_SNAKE_CASE =processors[data_args.task_name]() _SCREAMING_SNAKE_CASE =processor.get_labels() _SCREAMING_SNAKE_CASE =len(a__) except KeyError: raise ValueError('''Task not found: %s''' % (data_args.task_name)) # Load pretrained model and tokenizer # # Distributed training: # The .from_pretrained methods guarantee that only one local process can concurrently # download model & vocab. _SCREAMING_SNAKE_CASE =AutoConfig.from_pretrained( model_args.config_name if model_args.config_name else model_args.model_name_or_path ,num_labels=a__ ,finetuning_task=data_args.task_name ,cache_dir=model_args.cache_dir ,) _SCREAMING_SNAKE_CASE =AutoTokenizer.from_pretrained( model_args.tokenizer_name if model_args.tokenizer_name else model_args.model_name_or_path ,cache_dir=model_args.cache_dir ,) _SCREAMING_SNAKE_CASE =AutoModelForMultipleChoice.from_pretrained( model_args.model_name_or_path ,from_tf=bool('''.ckpt''' in model_args.model_name_or_path) ,config=a__ ,cache_dir=model_args.cache_dir ,) # Get datasets _SCREAMING_SNAKE_CASE =( MultipleChoiceDataset( data_dir=data_args.data_dir ,tokenizer=a__ ,task=data_args.task_name ,max_seq_length=data_args.max_seq_length ,overwrite_cache=data_args.overwrite_cache ,mode=Split.train ,) if training_args.do_train else None ) _SCREAMING_SNAKE_CASE =( MultipleChoiceDataset( data_dir=data_args.data_dir ,tokenizer=a__ ,task=data_args.task_name ,max_seq_length=data_args.max_seq_length ,overwrite_cache=data_args.overwrite_cache ,mode=Split.dev ,) if training_args.do_eval else None ) def compute_metrics(a__) -> Dict: _SCREAMING_SNAKE_CASE =np.argmax(p.predictions ,axis=1) return {"acc": simple_accuracy(a__ ,p.label_ids)} # Data collator _SCREAMING_SNAKE_CASE =DataCollatorWithPadding(a__ ,pad_to_multiple_of=8) if training_args.fpaa else None # Initialize our Trainer _SCREAMING_SNAKE_CASE =Trainer( model=a__ ,args=a__ ,train_dataset=a__ ,eval_dataset=a__ ,compute_metrics=a__ ,data_collator=a__ ,) # Training if training_args.do_train: trainer.train( model_path=model_args.model_name_or_path if os.path.isdir(model_args.model_name_or_path) else None) trainer.save_model() # For convenience, we also re-save the tokenizer to the same directory, # so that you can share your model easily on huggingface.co/models =) if trainer.is_world_master(): tokenizer.save_pretrained(training_args.output_dir) # Evaluation _SCREAMING_SNAKE_CASE ={} if training_args.do_eval: logger.info('''*** Evaluate ***''') _SCREAMING_SNAKE_CASE =trainer.evaluate() _SCREAMING_SNAKE_CASE =os.path.join(training_args.output_dir ,'''eval_results.txt''') if trainer.is_world_master(): with open(a__ ,'''w''') as writer: logger.info('''***** Eval results *****''') for key, value in result.items(): logger.info(''' %s = %s''' ,a__ ,a__) writer.write('''%s = %s\n''' % (key, value)) results.update(a__) return results def lowerCamelCase( a__): # For xla_spawn (TPUs) main() if __name__ == "__main__": main()
691
0
'''simple docstring''' import os import tempfile import unittest from transformers import DistilBertConfig, is_torch_available from transformers.testing_utils import require_torch, require_torch_gpu, slow, torch_device from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, ids_tensor, random_attention_mask from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from transformers import ( DISTILBERT_PRETRAINED_MODEL_ARCHIVE_LIST, DistilBertForMaskedLM, DistilBertForMultipleChoice, DistilBertForQuestionAnswering, DistilBertForSequenceClassification, DistilBertForTokenClassification, DistilBertModel, ) class __SCREAMING_SNAKE_CASE ( UpperCamelCase__ ): def __init__( self : List[Any] , UpperCAmelCase__ : Optional[Any] , UpperCAmelCase__ : Any=13 , UpperCAmelCase__ : Any=7 , UpperCAmelCase__ : Union[str, Any]=True , UpperCAmelCase__ : List[str]=True , UpperCAmelCase__ : List[Any]=False , UpperCAmelCase__ : str=True , UpperCAmelCase__ : str=99 , UpperCAmelCase__ : List[Any]=32 , UpperCAmelCase__ : List[Any]=5 , UpperCAmelCase__ : List[Any]=4 , UpperCAmelCase__ : Tuple=37 , UpperCAmelCase__ : Any="gelu" , UpperCAmelCase__ : Dict=0.1 , UpperCAmelCase__ : int=0.1 , UpperCAmelCase__ : str=512 , UpperCAmelCase__ : str=16 , UpperCAmelCase__ : Any=2 , UpperCAmelCase__ : int=0.02 , UpperCAmelCase__ : int=3 , UpperCAmelCase__ : Optional[int]=4 , UpperCAmelCase__ : int=None , ): '''simple docstring''' lowercase : List[Any] =parent lowercase : Tuple =batch_size lowercase : str =seq_length lowercase : Dict =is_training lowercase : str =use_input_mask lowercase : int =use_token_type_ids lowercase : Optional[int] =use_labels lowercase : str =vocab_size lowercase : Tuple =hidden_size lowercase : List[Any] =num_hidden_layers lowercase : Union[str, Any] =num_attention_heads lowercase : Dict =intermediate_size lowercase : Dict =hidden_act lowercase : Dict =hidden_dropout_prob lowercase : str =attention_probs_dropout_prob lowercase : Union[str, Any] =max_position_embeddings lowercase : int =type_vocab_size lowercase : int =type_sequence_label_size lowercase : List[str] =initializer_range lowercase : List[Any] =num_labels lowercase : Optional[Any] =num_choices lowercase : Tuple =scope def lowerCamelCase_ ( self : Union[str, Any] ): '''simple docstring''' lowercase : int =ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) lowercase : List[Any] =None if self.use_input_mask: lowercase : int =random_attention_mask([self.batch_size, self.seq_length] ) lowercase : Union[str, Any] =None lowercase : Optional[int] =None lowercase : Any =None if self.use_labels: lowercase : str =ids_tensor([self.batch_size] , self.type_sequence_label_size ) lowercase : Dict =ids_tensor([self.batch_size, self.seq_length] , self.num_labels ) lowercase : int =ids_tensor([self.batch_size] , self.num_choices ) lowercase : Tuple =self.get_config() return config, input_ids, input_mask, sequence_labels, token_labels, choice_labels def lowerCamelCase_ ( self : int ): '''simple docstring''' return DistilBertConfig( vocab_size=self.vocab_size , dim=self.hidden_size , n_layers=self.num_hidden_layers , n_heads=self.num_attention_heads , hidden_dim=self.intermediate_size , hidden_act=self.hidden_act , dropout=self.hidden_dropout_prob , attention_dropout=self.attention_probs_dropout_prob , max_position_embeddings=self.max_position_embeddings , initializer_range=self.initializer_range , ) def lowerCamelCase_ ( self : Tuple , UpperCAmelCase__ : Optional[Any] , UpperCAmelCase__ : Dict , UpperCAmelCase__ : Any , UpperCAmelCase__ : List[Any] , UpperCAmelCase__ : Optional[Any] , UpperCAmelCase__ : Tuple ): '''simple docstring''' lowercase : List[str] =DistilBertModel(config=_a ) model.to(_a ) model.eval() lowercase : Optional[int] =model(_a , _a ) lowercase : List[str] =model(_a ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def lowerCamelCase_ ( self : List[str] , UpperCAmelCase__ : List[Any] , UpperCAmelCase__ : Any , UpperCAmelCase__ : str , UpperCAmelCase__ : List[Any] , UpperCAmelCase__ : List[Any] , UpperCAmelCase__ : List[str] ): '''simple docstring''' lowercase : Dict =DistilBertForMaskedLM(config=_a ) model.to(_a ) model.eval() lowercase : int =model(_a , attention_mask=_a , labels=_a ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.vocab_size) ) def lowerCamelCase_ ( self : Dict , UpperCAmelCase__ : str , UpperCAmelCase__ : Tuple , UpperCAmelCase__ : List[Any] , UpperCAmelCase__ : List[str] , UpperCAmelCase__ : Optional[Any] , UpperCAmelCase__ : Optional[Any] ): '''simple docstring''' lowercase : List[str] =DistilBertForQuestionAnswering(config=_a ) model.to(_a ) model.eval() lowercase : Optional[int] =model( _a , attention_mask=_a , start_positions=_a , end_positions=_a ) self.parent.assertEqual(result.start_logits.shape , (self.batch_size, self.seq_length) ) self.parent.assertEqual(result.end_logits.shape , (self.batch_size, self.seq_length) ) def lowerCamelCase_ ( self : Tuple , UpperCAmelCase__ : Dict , UpperCAmelCase__ : Union[str, Any] , UpperCAmelCase__ : Tuple , UpperCAmelCase__ : List[Any] , UpperCAmelCase__ : Dict , UpperCAmelCase__ : Dict ): '''simple docstring''' lowercase : List[Any] =self.num_labels lowercase : int =DistilBertForSequenceClassification(_a ) model.to(_a ) model.eval() lowercase : int =model(_a , attention_mask=_a , labels=_a ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_labels) ) def lowerCamelCase_ ( self : int , UpperCAmelCase__ : Any , UpperCAmelCase__ : List[str] , UpperCAmelCase__ : List[Any] , UpperCAmelCase__ : Optional[Any] , UpperCAmelCase__ : int , UpperCAmelCase__ : List[str] ): '''simple docstring''' lowercase : Dict =self.num_labels lowercase : Union[str, Any] =DistilBertForTokenClassification(config=_a ) model.to(_a ) model.eval() lowercase : Dict =model(_a , attention_mask=_a , labels=_a ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.num_labels) ) def lowerCamelCase_ ( self : List[Any] , UpperCAmelCase__ : Any , UpperCAmelCase__ : Any , UpperCAmelCase__ : Optional[int] , UpperCAmelCase__ : List[Any] , UpperCAmelCase__ : Dict , UpperCAmelCase__ : Tuple ): '''simple docstring''' lowercase : Optional[Any] =self.num_choices lowercase : str =DistilBertForMultipleChoice(config=_a ) model.to(_a ) model.eval() lowercase : Union[str, Any] =input_ids.unsqueeze(1 ).expand(-1 , self.num_choices , -1 ).contiguous() lowercase : Optional[int] =input_mask.unsqueeze(1 ).expand(-1 , self.num_choices , -1 ).contiguous() lowercase : str =model( _a , attention_mask=_a , labels=_a , ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_choices) ) def lowerCamelCase_ ( self : List[str] ): '''simple docstring''' lowercase : Optional[Any] =self.prepare_config_and_inputs() ((lowercase) , (lowercase) , (lowercase) , (lowercase) , (lowercase) , (lowercase)) : Tuple =config_and_inputs lowercase : List[Any] ={'''input_ids''': input_ids, '''attention_mask''': input_mask} return config, inputs_dict @require_torch class __SCREAMING_SNAKE_CASE ( UpperCamelCase__ , UpperCamelCase__ , unittest.TestCase ): lowerCamelCase_ = ( ( DistilBertModel, DistilBertForMaskedLM, DistilBertForMultipleChoice, DistilBertForQuestionAnswering, DistilBertForSequenceClassification, DistilBertForTokenClassification, ) if is_torch_available() else None ) lowerCamelCase_ = ( { 'feature-extraction': DistilBertModel, 'fill-mask': DistilBertForMaskedLM, 'question-answering': DistilBertForQuestionAnswering, 'text-classification': DistilBertForSequenceClassification, 'token-classification': DistilBertForTokenClassification, 'zero-shot': DistilBertForSequenceClassification, } if is_torch_available() else {} ) lowerCamelCase_ = True lowerCamelCase_ = True lowerCamelCase_ = True lowerCamelCase_ = True def lowerCamelCase_ ( self : Optional[int] ): '''simple docstring''' lowercase : str =DistilBertModelTester(self ) lowercase : List[Any] =ConfigTester(self , config_class=_a , dim=37 ) def lowerCamelCase_ ( self : Tuple ): '''simple docstring''' self.config_tester.run_common_tests() def lowerCamelCase_ ( self : Optional[int] ): '''simple docstring''' lowercase : Union[str, Any] =self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_distilbert_model(*_a ) def lowerCamelCase_ ( self : str ): '''simple docstring''' lowercase : List[Any] =self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_distilbert_for_masked_lm(*_a ) def lowerCamelCase_ ( self : Optional[Any] ): '''simple docstring''' lowercase : Optional[Any] =self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_distilbert_for_question_answering(*_a ) def lowerCamelCase_ ( self : List[Any] ): '''simple docstring''' lowercase : Dict =self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_distilbert_for_sequence_classification(*_a ) def lowerCamelCase_ ( self : Optional[Any] ): '''simple docstring''' lowercase : Dict =self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_distilbert_for_token_classification(*_a ) def lowerCamelCase_ ( self : str ): '''simple docstring''' lowercase : Dict =self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_distilbert_for_multiple_choice(*_a ) @slow def lowerCamelCase_ ( self : Optional[Any] ): '''simple docstring''' for model_name in DISTILBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: lowercase : Union[str, Any] =DistilBertModel.from_pretrained(_a ) self.assertIsNotNone(_a ) @slow @require_torch_gpu def lowerCamelCase_ ( self : str ): '''simple docstring''' lowercase , lowercase : str =self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: # BertForMultipleChoice behaves incorrectly in JIT environments. if model_class == DistilBertForMultipleChoice: return lowercase : List[str] =True lowercase : Optional[int] =model_class(config=_a ) lowercase : Tuple =self._prepare_for_class(_a , _a ) lowercase : List[Any] =torch.jit.trace( _a , (inputs_dict['''input_ids'''].to('''cpu''' ), inputs_dict['''attention_mask'''].to('''cpu''' )) ) with tempfile.TemporaryDirectory() as tmp: torch.jit.save(_a , os.path.join(_a , '''traced_model.pt''' ) ) lowercase : Optional[int] =torch.jit.load(os.path.join(_a , '''traced_model.pt''' ) , map_location=_a ) loaded(inputs_dict['''input_ids'''].to(_a ) , inputs_dict['''attention_mask'''].to(_a ) ) @require_torch class __SCREAMING_SNAKE_CASE ( unittest.TestCase ): @slow def lowerCamelCase_ ( self : Any ): '''simple docstring''' lowercase : Optional[Any] =DistilBertModel.from_pretrained('''distilbert-base-uncased''' ) lowercase : Tuple =torch.tensor([[0, 345, 232, 328, 740, 140, 1695, 69, 6078, 1588, 2]] ) lowercase : str =torch.tensor([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]] ) with torch.no_grad(): lowercase : Any =model(_a , attention_mask=_a )[0] lowercase : Any =torch.Size((1, 11, 768) ) self.assertEqual(output.shape , _a ) lowercase : int =torch.tensor( [[[-0.16_39, 0.32_99, 0.16_48], [-0.17_46, 0.32_89, 0.17_10], [-0.18_84, 0.33_57, 0.18_10]]] ) self.assertTrue(torch.allclose(output[:, 1:4, 1:4] , _a , atol=1E-4 ) )
92
def lowerCamelCase( a__ ,a__ ,a__): if n == 0: return 1 elif n % 2 == 1: return (binary_exponentiation(a__ ,n - 1 ,a__) * a) % mod else: _SCREAMING_SNAKE_CASE =binary_exponentiation(a__ ,n / 2 ,a__) return (b * b) % mod # a prime number snake_case_ : Union[str, Any] = 7_01 snake_case_ : int = 10_00_00_00_00 snake_case_ : str = 10 # using binary exponentiation function, O(log(p)): print((a / b) % p == (a * binary_exponentiation(b, p - 2, p)) % p) print((a / b) % p == (a * b ** (p - 2)) % p)
691
0
'''simple docstring''' from math import ceil from typing import List, Optional, Union import numpy as np from ...audio_utils import mel_filter_bank, spectrogram, window_function from ...feature_extraction_sequence_utils import BatchFeature, SequenceFeatureExtractor from ...utils import TensorType, logging lowerCAmelCase_ : Dict = logging.get_logger(__name__) class lowerCamelCase_ ( UpperCamelCase__ ): _lowerCAmelCase : List[str] = ['audio_values', 'audio_mask'] def __init__( self : Optional[int] , lowerCAmelCase__ : Tuple=20_48 , lowerCAmelCase__ : Optional[Any]=1 , lowerCAmelCase__ : Union[str, Any]=[16, 16] , lowerCAmelCase__ : str=1_28 , lowerCAmelCase__ : int=4_41_00 , lowerCAmelCase__ : Any=86 , lowerCAmelCase__ : Union[str, Any]=20_48 , lowerCAmelCase__ : List[str]=0.0 , **lowerCAmelCase__ : Dict , ): """simple docstring""" super().__init__( feature_size=_a , sampling_rate=_a , padding_value=_a , **_a , ) SCREAMING_SNAKE_CASE : Optional[Any] = spectrogram_length SCREAMING_SNAKE_CASE : List[str] = num_channels SCREAMING_SNAKE_CASE : Optional[Any] = patch_size SCREAMING_SNAKE_CASE : Optional[int] = feature_size // self.patch_size[1] SCREAMING_SNAKE_CASE : Any = n_fft SCREAMING_SNAKE_CASE : Optional[Any] = sampling_rate // hop_length_to_sampling_rate SCREAMING_SNAKE_CASE : Union[str, Any] = sampling_rate SCREAMING_SNAKE_CASE : Optional[int] = padding_value SCREAMING_SNAKE_CASE : int = mel_filter_bank( num_frequency_bins=1 + n_fft // 2 , num_mel_filters=_a , min_frequency=0.0 , max_frequency=2_20_50.0 , sampling_rate=_a , norm='''slaney''' , mel_scale='''slaney''' , ).T def __lowercase ( self : Dict , lowerCAmelCase__ : np.array ): """simple docstring""" SCREAMING_SNAKE_CASE : Dict = spectrogram( _a , window_function(self.n_fft , '''hann''' ) , frame_length=self.n_fft , hop_length=self.hop_length , power=2.0 , mel_filters=self.mel_filters.T , log_mel='''dB''' , db_range=80.0 , ) SCREAMING_SNAKE_CASE : Union[str, Any] = log_spec[:, :-1] SCREAMING_SNAKE_CASE : str = log_spec - 20.0 SCREAMING_SNAKE_CASE : Optional[Any] = np.clip(log_spec / 40.0 , -2.0 , 0.0 ) + 1.0 return log_spec def __call__( self : Optional[int] , lowerCAmelCase__ : Union[np.ndarray, List[float], List[np.ndarray], List[List[float]]] , lowerCAmelCase__ : Optional[Union[str, TensorType]] = None , lowerCAmelCase__ : Optional[bool] = True , lowerCAmelCase__ : Optional[int] = None , lowerCAmelCase__ : bool = False , lowerCAmelCase__ : bool = False , **lowerCAmelCase__ : List[str] , ): """simple docstring""" if sampling_rate is not None: if sampling_rate != self.sampling_rate: raise ValueError( '''This feature extractor is set to support sampling rate''' F""" of {self.sampling_rate}. Please make sure that the provided `raw_speech` input was sampled""" F""" with {self.sampling_rate} and not {sampling_rate}.""" ) else: logger.warning( '''It is strongly recommended to pass the `sampling_rate` argument to this function. ''' '''Failing to do so can result in silent errors that might be hard to debug.''' ) SCREAMING_SNAKE_CASE : Optional[int] = isinstance(_a , np.ndarray ) and len(raw_speech.shape ) > 1 if is_batched_numpy and len(raw_speech.shape ) > 2: raise ValueError(F"""Only mono-channel audio is supported for input to {self}""" ) SCREAMING_SNAKE_CASE : str = is_batched_numpy or ( isinstance(_a , (list, tuple) ) and (isinstance(raw_speech[0] , (np.ndarray, tuple, list) )) ) if is_batched: SCREAMING_SNAKE_CASE : str = [np.asarray([speech] , dtype=np.floataa ).T for speech in raw_speech] elif not is_batched and not isinstance(_a , np.ndarray ): SCREAMING_SNAKE_CASE : List[Any] = np.asarray(_a , dtype=np.floataa ) elif isinstance(_a , np.ndarray ) and raw_speech.dtype is np.dtype(np.floataa ): SCREAMING_SNAKE_CASE : Tuple = raw_speech.astype(np.floataa ) # always return batch if not is_batched: SCREAMING_SNAKE_CASE : Tuple = [np.asarray([raw_speech] ).T] # Convert audio signals to log mel spectrograms, truncate by time axis SCREAMING_SNAKE_CASE : Any = [ self._np_extract_fbank_features(waveform.squeeze() ).T[: self.spectrogram_length] for waveform in raw_speech ] if isinstance(audio_features[0] , _a ): SCREAMING_SNAKE_CASE : Optional[int] = [np.asarray(_a , dtype=np.floataa ) for feature in audio_features] # Create audio attention mask SCREAMING_SNAKE_CASE : Optional[int] = max( [ceil(feature.shape[0] / self.patch_size[0] ) * self.freq_len for feature in audio_features] ) # The maximum number of audio patches in a batch if return_attention_mask: SCREAMING_SNAKE_CASE : List[Any] = [ (ceil(feature.shape[0] / self.patch_size[0] ) * self.freq_len) * [1] + (max_patch_len - ceil(feature.shape[0] / self.patch_size[0] ) * self.freq_len) * [0] for feature in audio_features ] SCREAMING_SNAKE_CASE : Any = np.array(_a ).astype(np.floataa ) # convert into correct format for padding SCREAMING_SNAKE_CASE : List[str] = max_patch_len // self.freq_len * self.patch_size[0] # The maximum audio size in a batch SCREAMING_SNAKE_CASE : Optional[Any] = np.ones([len(_a ), 1, max_time_len, self.feature_size] ).astype(np.floataa ) SCREAMING_SNAKE_CASE : Any = padded_audio_features * self.padding_value for i in range(len(_a ) ): SCREAMING_SNAKE_CASE : Any = audio_features[i] SCREAMING_SNAKE_CASE : List[Any] = feature # return as BatchFeature if return_attention_mask: SCREAMING_SNAKE_CASE : Optional[int] = {'''audio_values''': padded_audio_features, '''audio_mask''': audio_mask} else: SCREAMING_SNAKE_CASE : Dict = {'''audio_values''': padded_audio_features} SCREAMING_SNAKE_CASE : Tuple = BatchFeature(data=_a , tensor_type=_a ) return encoded_inputs
527
import inspect import unittest from transformers import BitConfig from transformers.testing_utils import require_torch, require_vision, slow, torch_device from transformers.utils import cached_property, is_torch_available, is_vision_available from ...test_backbone_common import BackboneTesterMixin from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from torch import nn from transformers import BitBackbone, BitForImageClassification, BitImageProcessor, BitModel from transformers.models.bit.modeling_bit import BIT_PRETRAINED_MODEL_ARCHIVE_LIST if is_vision_available(): from PIL import Image class A__ : def __init__( self : Optional[Any] , _a : int , _a : Optional[Any]=3 , _a : Tuple=32 , _a : Any=3 , _a : Union[str, Any]=10 , _a : Optional[int]=[8, 16, 32, 64] , _a : Union[str, Any]=[1, 1, 2, 1] , _a : Optional[Any]=True , _a : int=True , _a : Tuple="relu" , _a : Optional[Any]=3 , _a : str=None , _a : List[Any]=["stage2", "stage3", "stage4"] , _a : Union[str, Any]=[2, 3, 4] , _a : Dict=1 , ) -> Dict: """simple docstring""" _SCREAMING_SNAKE_CASE =parent _SCREAMING_SNAKE_CASE =batch_size _SCREAMING_SNAKE_CASE =image_size _SCREAMING_SNAKE_CASE =num_channels _SCREAMING_SNAKE_CASE =embeddings_size _SCREAMING_SNAKE_CASE =hidden_sizes _SCREAMING_SNAKE_CASE =depths _SCREAMING_SNAKE_CASE =is_training _SCREAMING_SNAKE_CASE =use_labels _SCREAMING_SNAKE_CASE =hidden_act _SCREAMING_SNAKE_CASE =num_labels _SCREAMING_SNAKE_CASE =scope _SCREAMING_SNAKE_CASE =len(_a ) _SCREAMING_SNAKE_CASE =out_features _SCREAMING_SNAKE_CASE =out_indices _SCREAMING_SNAKE_CASE =num_groups def __UpperCamelCase ( self : Optional[Any] ) -> str: """simple docstring""" _SCREAMING_SNAKE_CASE =floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) _SCREAMING_SNAKE_CASE =None if self.use_labels: _SCREAMING_SNAKE_CASE =ids_tensor([self.batch_size] , self.num_labels ) _SCREAMING_SNAKE_CASE =self.get_config() return config, pixel_values, labels def __UpperCamelCase ( self : Any ) -> Union[str, Any]: """simple docstring""" return BitConfig( num_channels=self.num_channels , embeddings_size=self.embeddings_size , hidden_sizes=self.hidden_sizes , depths=self.depths , hidden_act=self.hidden_act , num_labels=self.num_labels , out_features=self.out_features , out_indices=self.out_indices , num_groups=self.num_groups , ) def __UpperCamelCase ( self : Optional[Any] , _a : Dict , _a : str , _a : Dict ) -> List[Any]: """simple docstring""" _SCREAMING_SNAKE_CASE =BitModel(config=_a ) model.to(_a ) model.eval() _SCREAMING_SNAKE_CASE =model(_a ) self.parent.assertEqual( result.last_hidden_state.shape , (self.batch_size, self.hidden_sizes[-1], self.image_size // 32, self.image_size // 32) , ) def __UpperCamelCase ( self : Union[str, Any] , _a : Union[str, Any] , _a : Optional[Any] , _a : Optional[Any] ) -> Union[str, Any]: """simple docstring""" _SCREAMING_SNAKE_CASE =self.num_labels _SCREAMING_SNAKE_CASE =BitForImageClassification(_a ) model.to(_a ) model.eval() _SCREAMING_SNAKE_CASE =model(_a , labels=_a ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_labels) ) def __UpperCamelCase ( self : List[str] , _a : Any , _a : str , _a : List[str] ) -> Optional[int]: """simple docstring""" _SCREAMING_SNAKE_CASE =BitBackbone(config=_a ) model.to(_a ) model.eval() _SCREAMING_SNAKE_CASE =model(_a ) # verify feature maps self.parent.assertEqual(len(result.feature_maps ) , len(config.out_features ) ) self.parent.assertListEqual(list(result.feature_maps[0].shape ) , [self.batch_size, self.hidden_sizes[1], 4, 4] ) # verify channels self.parent.assertEqual(len(model.channels ) , len(config.out_features ) ) self.parent.assertListEqual(model.channels , config.hidden_sizes[1:] ) # verify backbone works with out_features=None _SCREAMING_SNAKE_CASE =None _SCREAMING_SNAKE_CASE =BitBackbone(config=_a ) model.to(_a ) model.eval() _SCREAMING_SNAKE_CASE =model(_a ) # verify feature maps self.parent.assertEqual(len(result.feature_maps ) , 1 ) self.parent.assertListEqual(list(result.feature_maps[0].shape ) , [self.batch_size, self.hidden_sizes[-1], 1, 1] ) # verify channels self.parent.assertEqual(len(model.channels ) , 1 ) self.parent.assertListEqual(model.channels , [config.hidden_sizes[-1]] ) def __UpperCamelCase ( self : Optional[Any] ) -> Dict: """simple docstring""" _SCREAMING_SNAKE_CASE =self.prepare_config_and_inputs() _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE =config_and_inputs _SCREAMING_SNAKE_CASE ={'''pixel_values''': pixel_values} return config, inputs_dict @require_torch class A__ ( UpperCamelCase__ , UpperCamelCase__ , unittest.TestCase ): UpperCAmelCase = (BitModel, BitForImageClassification, BitBackbone) if is_torch_available() else () UpperCAmelCase = ( {"feature-extraction": BitModel, "image-classification": BitForImageClassification} if is_torch_available() else {} ) UpperCAmelCase = False UpperCAmelCase = False UpperCAmelCase = False UpperCAmelCase = False UpperCAmelCase = False def __UpperCamelCase ( self : Union[str, Any] ) -> str: """simple docstring""" _SCREAMING_SNAKE_CASE =BitModelTester(self ) _SCREAMING_SNAKE_CASE =ConfigTester(self , config_class=_a , has_text_modality=_a ) def __UpperCamelCase ( self : Union[str, Any] ) -> int: """simple docstring""" self.create_and_test_config_common_properties() self.config_tester.create_and_test_config_to_json_string() self.config_tester.create_and_test_config_to_json_file() self.config_tester.create_and_test_config_from_and_save_pretrained() self.config_tester.create_and_test_config_with_num_labels() self.config_tester.check_config_can_be_init_without_params() self.config_tester.check_config_arguments_init() def __UpperCamelCase ( self : List[str] ) -> Optional[int]: """simple docstring""" return @unittest.skip(reason='''Bit does not output attentions''' ) def __UpperCamelCase ( self : Optional[int] ) -> List[str]: """simple docstring""" pass @unittest.skip(reason='''Bit does not use inputs_embeds''' ) def __UpperCamelCase ( self : str ) -> Optional[Any]: """simple docstring""" pass @unittest.skip(reason='''Bit does not support input and output embeddings''' ) def __UpperCamelCase ( self : Optional[int] ) -> Union[str, Any]: """simple docstring""" pass def __UpperCamelCase ( self : Tuple ) -> Optional[int]: """simple docstring""" _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE =self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: _SCREAMING_SNAKE_CASE =model_class(_a ) _SCREAMING_SNAKE_CASE =inspect.signature(model.forward ) # signature.parameters is an OrderedDict => so arg_names order is deterministic _SCREAMING_SNAKE_CASE =[*signature.parameters.keys()] _SCREAMING_SNAKE_CASE =['''pixel_values'''] self.assertListEqual(arg_names[:1] , _a ) def __UpperCamelCase ( self : int ) -> Optional[int]: """simple docstring""" _SCREAMING_SNAKE_CASE =self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*_a ) def __UpperCamelCase ( self : Optional[Any] ) -> Dict: """simple docstring""" _SCREAMING_SNAKE_CASE =self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_backbone(*_a ) def __UpperCamelCase ( self : Tuple ) -> Optional[int]: """simple docstring""" _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE =self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: _SCREAMING_SNAKE_CASE =model_class(config=_a ) for name, module in model.named_modules(): if isinstance(_a , (nn.BatchNormad, nn.GroupNorm) ): self.assertTrue( torch.all(module.weight == 1 ) , msg=f"Parameter {name} of model {model_class} seems not properly initialized" , ) self.assertTrue( torch.all(module.bias == 0 ) , msg=f"Parameter {name} of model {model_class} seems not properly initialized" , ) def __UpperCamelCase ( self : Tuple ) -> Optional[int]: """simple docstring""" def check_hidden_states_output(_a : Any , _a : Optional[int] , _a : Tuple ): _SCREAMING_SNAKE_CASE =model_class(_a ) model.to(_a ) model.eval() with torch.no_grad(): _SCREAMING_SNAKE_CASE =model(**self._prepare_for_class(_a , _a ) ) _SCREAMING_SNAKE_CASE =outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states _SCREAMING_SNAKE_CASE =self.model_tester.num_stages self.assertEqual(len(_a ) , expected_num_stages + 1 ) # Bit's feature maps are of shape (batch_size, num_channels, height, width) self.assertListEqual( list(hidden_states[0].shape[-2:] ) , [self.model_tester.image_size // 4, self.model_tester.image_size // 4] , ) _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE =self.model_tester.prepare_config_and_inputs_for_common() _SCREAMING_SNAKE_CASE =['''preactivation''', '''bottleneck'''] for model_class in self.all_model_classes: for layer_type in layers_type: _SCREAMING_SNAKE_CASE =layer_type _SCREAMING_SNAKE_CASE =True check_hidden_states_output(_a , _a , _a ) # check that output_hidden_states also work using config del inputs_dict["output_hidden_states"] _SCREAMING_SNAKE_CASE =True check_hidden_states_output(_a , _a , _a ) @unittest.skip(reason='''Bit does not use feedforward chunking''' ) def __UpperCamelCase ( self : Optional[int] ) -> Dict: """simple docstring""" pass def __UpperCamelCase ( self : Union[str, Any] ) -> Any: """simple docstring""" _SCREAMING_SNAKE_CASE =self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_image_classification(*_a ) @slow def __UpperCamelCase ( self : int ) -> Tuple: """simple docstring""" for model_name in BIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: _SCREAMING_SNAKE_CASE =BitModel.from_pretrained(_a ) self.assertIsNotNone(_a ) def lowerCamelCase( ): _SCREAMING_SNAKE_CASE =Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png''') return image @require_torch @require_vision class A__ ( unittest.TestCase ): @cached_property def __UpperCamelCase ( self : str ) -> Optional[Any]: """simple docstring""" return ( BitImageProcessor.from_pretrained(BIT_PRETRAINED_MODEL_ARCHIVE_LIST[0] ) if is_vision_available() else None ) @slow def __UpperCamelCase ( self : List[Any] ) -> int: """simple docstring""" _SCREAMING_SNAKE_CASE =BitForImageClassification.from_pretrained(BIT_PRETRAINED_MODEL_ARCHIVE_LIST[0] ).to(_a ) _SCREAMING_SNAKE_CASE =self.default_image_processor _SCREAMING_SNAKE_CASE =prepare_img() _SCREAMING_SNAKE_CASE =image_processor(images=_a , return_tensors='''pt''' ).to(_a ) # forward pass with torch.no_grad(): _SCREAMING_SNAKE_CASE =model(**_a ) # verify the logits _SCREAMING_SNAKE_CASE =torch.Size((1, 1000) ) self.assertEqual(outputs.logits.shape , _a ) _SCREAMING_SNAKE_CASE =torch.tensor([[-0.65_26, -0.52_63, -1.43_98]] ).to(_a ) self.assertTrue(torch.allclose(outputs.logits[0, :3] , _a , atol=1E-4 ) ) @require_torch class A__ ( UpperCamelCase__ , unittest.TestCase ): UpperCAmelCase = (BitBackbone,) if is_torch_available() else () UpperCAmelCase = BitConfig UpperCAmelCase = False def __UpperCamelCase ( self : List[str] ) -> Optional[Any]: """simple docstring""" _SCREAMING_SNAKE_CASE =BitModelTester(self )
691
0
from __future__ import annotations from cmath import sqrt def lowerCAmelCase__( lowercase : Optional[int] , lowercase : Optional[int] , lowercase : int ) -> Optional[Any]: if a == 0: raise ValueError("Coefficient \'a\' must not be zero." ) __snake_case : Tuple = b * b - 4 * a * c __snake_case : List[Any] = (-b + sqrt(a__ )) / (2 * a) __snake_case : List[str] = (-b - sqrt(a__ )) / (2 * a) return ( root_a.real if not root_a.imag else root_a, root_a.real if not root_a.imag else root_a, ) def lowerCAmelCase__( ) -> str: __snake_case , __snake_case : Dict = quadratic_roots(a=5 , b=6 , c=1 ) print(f"""The solutions are: {solutiona} and {solutiona}""" ) if __name__ == "__main__": main()
243
import copy from ...configuration_utils import PretrainedConfig from ...utils import add_start_docstrings snake_case_ : Optional[Any] = R''' [`RagConfig`] stores the configuration of a *RagModel*. Configuration objects inherit from [`PretrainedConfig`] and can be used to control the model outputs. Read the documentation from [`PretrainedConfig`] for more information. Args: title_sep (`str`, *optional*, defaults to `" / "`): Separator inserted between the title and the text of the retrieved document when calling [`RagRetriever`]. doc_sep (`str`, *optional*, defaults to `" // "`): Separator inserted between the text of the retrieved document and the original input when calling [`RagRetriever`]. n_docs (`int`, *optional*, defaults to 5): Number of documents to retrieve. max_combined_length (`int`, *optional*, defaults to 300): Max length of contextualized input returned by [`~RagRetriever.__call__`]. retrieval_vector_size (`int`, *optional*, defaults to 768): Dimensionality of the document embeddings indexed by [`RagRetriever`]. retrieval_batch_size (`int`, *optional*, defaults to 8): Retrieval batch size, defined as the number of queries issues concurrently to the faiss index encapsulated [`RagRetriever`]. dataset (`str`, *optional*, defaults to `"wiki_dpr"`): A dataset identifier of the indexed dataset in HuggingFace Datasets (list all available datasets and ids using `datasets.list_datasets()`). dataset_split (`str`, *optional*, defaults to `"train"`) Which split of the `dataset` to load. index_name (`str`, *optional*, defaults to `"compressed"`) The index name of the index associated with the `dataset`. One can choose between `"legacy"`, `"exact"` and `"compressed"`. index_path (`str`, *optional*) The path to the serialized faiss index on disk. passages_path (`str`, *optional*): A path to text passages compatible with the faiss index. Required if using [`~models.rag.retrieval_rag.LegacyIndex`] use_dummy_dataset (`bool`, *optional*, defaults to `False`) Whether to load a "dummy" variant of the dataset specified by `dataset`. label_smoothing (`float`, *optional*, defaults to 0.0): Only relevant if `return_loss` is set to `True`. Controls the `epsilon` parameter value for label smoothing in the loss calculation. If set to 0, no label smoothing is performed. do_marginalize (`bool`, *optional*, defaults to `False`): If `True`, the logits are marginalized over all documents by making use of `torch.nn.functional.log_softmax`. reduce_loss (`bool`, *optional*, defaults to `False`): Whether or not to reduce the NLL loss using the `torch.Tensor.sum` operation. do_deduplication (`bool`, *optional*, defaults to `True`): Whether or not to deduplicate the generations from different context documents for a given input. Has to be set to `False` if used while training with distributed backend. exclude_bos_score (`bool`, *optional*, defaults to `False`): Whether or not to disregard the BOS token when computing the loss. output_retrieved(`bool`, *optional*, defaults to `False`): If set to `True`, `retrieved_doc_embeds`, `retrieved_doc_ids`, `context_input_ids` and `context_attention_mask` are returned. See returned tensors for more detail. use_cache (`bool`, *optional*, defaults to `True`): Whether or not the model should return the last key/values attentions (not used by all models). forced_eos_token_id (`int`, *optional*): The id of the token to force as the last generated token when `max_length` is reached. Usually set to `eos_token_id`. ''' @add_start_docstrings(UpperCamelCase__ ) class A__ ( UpperCamelCase__ ): UpperCAmelCase = "rag" UpperCAmelCase = True def __init__( self : Tuple , _a : List[Any]=None , _a : Tuple=True , _a : Optional[Any]=None , _a : int=None , _a : List[str]=None , _a : int=None , _a : Optional[int]=None , _a : str=" / " , _a : Any=" // " , _a : Optional[Any]=5 , _a : int=300 , _a : Optional[Any]=768 , _a : Any=8 , _a : List[str]="wiki_dpr" , _a : Dict="train" , _a : Union[str, Any]="compressed" , _a : str=None , _a : Union[str, Any]=None , _a : int=False , _a : Any=False , _a : Any=0.0 , _a : Any=True , _a : List[str]=False , _a : Optional[int]=False , _a : int=False , _a : Union[str, Any]=True , _a : Optional[int]=None , **_a : List[str] , ) -> List[Any]: """simple docstring""" super().__init__( bos_token_id=_a , pad_token_id=_a , eos_token_id=_a , decoder_start_token_id=_a , forced_eos_token_id=_a , is_encoder_decoder=_a , prefix=_a , vocab_size=_a , **_a , ) assert ( "question_encoder" in kwargs and "generator" in kwargs ), "Config has to be initialized with question_encoder and generator config" _SCREAMING_SNAKE_CASE =kwargs.pop('''question_encoder''' ) _SCREAMING_SNAKE_CASE =question_encoder_config.pop('''model_type''' ) _SCREAMING_SNAKE_CASE =kwargs.pop('''generator''' ) _SCREAMING_SNAKE_CASE =decoder_config.pop('''model_type''' ) from ..auto.configuration_auto import AutoConfig _SCREAMING_SNAKE_CASE =AutoConfig.for_model(_a , **_a ) _SCREAMING_SNAKE_CASE =AutoConfig.for_model(_a , **_a ) _SCREAMING_SNAKE_CASE =reduce_loss _SCREAMING_SNAKE_CASE =label_smoothing _SCREAMING_SNAKE_CASE =exclude_bos_score _SCREAMING_SNAKE_CASE =do_marginalize _SCREAMING_SNAKE_CASE =title_sep _SCREAMING_SNAKE_CASE =doc_sep _SCREAMING_SNAKE_CASE =n_docs _SCREAMING_SNAKE_CASE =max_combined_length _SCREAMING_SNAKE_CASE =dataset _SCREAMING_SNAKE_CASE =dataset_split _SCREAMING_SNAKE_CASE =index_name _SCREAMING_SNAKE_CASE =retrieval_vector_size _SCREAMING_SNAKE_CASE =retrieval_batch_size _SCREAMING_SNAKE_CASE =passages_path _SCREAMING_SNAKE_CASE =index_path _SCREAMING_SNAKE_CASE =use_dummy_dataset _SCREAMING_SNAKE_CASE =output_retrieved _SCREAMING_SNAKE_CASE =do_deduplication _SCREAMING_SNAKE_CASE =use_cache if self.forced_eos_token_id is None: _SCREAMING_SNAKE_CASE =getattr(self.generator , '''forced_eos_token_id''' , _a ) @classmethod def __UpperCamelCase ( cls : Optional[int] , _a : PretrainedConfig , _a : PretrainedConfig , **_a : Dict ) -> PretrainedConfig: """simple docstring""" return cls(question_encoder=question_encoder_config.to_dict() , generator=generator_config.to_dict() , **_a ) def __UpperCamelCase ( self : Optional[Any] ) -> Any: """simple docstring""" _SCREAMING_SNAKE_CASE =copy.deepcopy(self.__dict__ ) _SCREAMING_SNAKE_CASE =self.question_encoder.to_dict() _SCREAMING_SNAKE_CASE =self.generator.to_dict() _SCREAMING_SNAKE_CASE =self.__class__.model_type return output
691
0
from __future__ import annotations import math class UpperCamelCase : def __init__( self : Any ,_lowerCAmelCase : int ): """simple docstring""" __snake_case = size # approximate the overall size of segment tree with given value __snake_case = [0 for i in range(0 ,4 * size )] # create array to store lazy update __snake_case = [0 for i in range(0 ,4 * size )] __snake_case = [0 for i in range(0 ,4 * size )] # flag for lazy update def UpperCamelCase_ ( self : str ,_lowerCAmelCase : int ): """simple docstring""" return idx * 2 def UpperCamelCase_ ( self : List[Any] ,_lowerCAmelCase : int ): """simple docstring""" return idx * 2 + 1 def UpperCamelCase_ ( self : Dict ,_lowerCAmelCase : int ,_lowerCAmelCase : int ,_lowerCAmelCase : int ,_lowerCAmelCase : list[int] ): """simple docstring""" if left_element == right_element: __snake_case = a[left_element - 1] else: __snake_case = (left_element + right_element) // 2 self.build(self.left(_a ) ,_a ,_a ,_a ) self.build(self.right(_a ) ,mid + 1 ,_a ,_a ) __snake_case = max( self.segment_tree[self.left(_a )] ,self.segment_tree[self.right(_a )] ) def UpperCamelCase_ ( self : int ,_lowerCAmelCase : int ,_lowerCAmelCase : int ,_lowerCAmelCase : int ,_lowerCAmelCase : int ,_lowerCAmelCase : int ,_lowerCAmelCase : int ): """simple docstring""" if self.flag[idx] is True: __snake_case = self.lazy[idx] __snake_case = False if left_element != right_element: __snake_case = self.lazy[idx] __snake_case = self.lazy[idx] __snake_case = True __snake_case = True if right_element < a or left_element > b: return True if left_element >= a and right_element <= b: __snake_case = val if left_element != right_element: __snake_case = val __snake_case = val __snake_case = True __snake_case = True return True __snake_case = (left_element + right_element) // 2 self.update(self.left(_a ) ,_a ,_a ,_a ,_a ,_a ) self.update(self.right(_a ) ,mid + 1 ,_a ,_a ,_a ,_a ) __snake_case = max( self.segment_tree[self.left(_a )] ,self.segment_tree[self.right(_a )] ) return True def UpperCamelCase_ ( self : Tuple ,_lowerCAmelCase : int ,_lowerCAmelCase : int ,_lowerCAmelCase : int ,_lowerCAmelCase : int ,_lowerCAmelCase : int ): """simple docstring""" if self.flag[idx] is True: __snake_case = self.lazy[idx] __snake_case = False if left_element != right_element: __snake_case = self.lazy[idx] __snake_case = self.lazy[idx] __snake_case = True __snake_case = True if right_element < a or left_element > b: return -math.inf if left_element >= a and right_element <= b: return self.segment_tree[idx] __snake_case = (left_element + right_element) // 2 __snake_case = self.query(self.left(_a ) ,_a ,_a ,_a ,_a ) __snake_case = self.query(self.right(_a ) ,mid + 1 ,_a ,_a ,_a ) return max(_a ,_a ) def __str__( self : Union[str, Any] ): """simple docstring""" return str([self.query(1 ,1 ,self.size ,_a ,_a ) for i in range(1 ,self.size + 1 )] ) if __name__ == "__main__": lowerCamelCase__ = [1, 2, -4, 7, 3, -5, 6, 11, -20, 9, 14, 15, 5, 2, -8] lowerCamelCase__ = 15 lowerCamelCase__ = SegmentTree(size) segt.build(1, 1, size, A) print(segt.query(1, 1, size, 4, 6)) print(segt.query(1, 1, size, 7, 11)) print(segt.query(1, 1, size, 7, 12)) segt.update(1, 1, size, 1, 3, 1_11) print(segt.query(1, 1, size, 1, 15)) segt.update(1, 1, size, 7, 8, 2_35) print(segt)
524
from manim import * class A__ ( UpperCamelCase__ ): def __UpperCamelCase ( self : Dict ) -> int: """simple docstring""" _SCREAMING_SNAKE_CASE =Rectangle(height=0.5 , width=0.5 ) _SCREAMING_SNAKE_CASE =Rectangle(height=0.25 , width=0.25 ) _SCREAMING_SNAKE_CASE =Rectangle(height=0.46 , width=0.46 ).set_stroke(width=0 ) _SCREAMING_SNAKE_CASE =[mem.copy() for i in range(6 )] _SCREAMING_SNAKE_CASE =[mem.copy() for i in range(6 )] _SCREAMING_SNAKE_CASE =VGroup(*_a ).arrange(_a , buff=0 ) _SCREAMING_SNAKE_CASE =VGroup(*_a ).arrange(_a , buff=0 ) _SCREAMING_SNAKE_CASE =VGroup(_a , _a ).arrange(_a , buff=0 ) _SCREAMING_SNAKE_CASE =Text('''CPU''' , font_size=24 ) _SCREAMING_SNAKE_CASE =Group(_a , _a ).arrange(_a , buff=0.5 , aligned_edge=_a ) cpu.move_to([-2.5, -0.5, 0] ) self.add(_a ) _SCREAMING_SNAKE_CASE =[mem.copy() for i in range(4 )] _SCREAMING_SNAKE_CASE =VGroup(*_a ).arrange(_a , buff=0 ) _SCREAMING_SNAKE_CASE =Text('''GPU''' , font_size=24 ) _SCREAMING_SNAKE_CASE =Group(_a , _a ).arrange(_a , buff=0.5 , aligned_edge=_a ) gpu.move_to([-1, -1, 0] ) self.add(_a ) _SCREAMING_SNAKE_CASE =[mem.copy() for i in range(6 )] _SCREAMING_SNAKE_CASE =VGroup(*_a ).arrange(_a , buff=0 ) _SCREAMING_SNAKE_CASE =Text('''Model''' , font_size=24 ) _SCREAMING_SNAKE_CASE =Group(_a , _a ).arrange(_a , buff=0.5 , aligned_edge=_a ) model.move_to([3, -1.0, 0] ) self.add(_a ) _SCREAMING_SNAKE_CASE =[] _SCREAMING_SNAKE_CASE =[] _SCREAMING_SNAKE_CASE =[] for i, rect in enumerate(_a ): rect.set_stroke(_a ) _SCREAMING_SNAKE_CASE =Rectangle(height=0.46 / 4 , width=0.46 / 3 ).set_stroke(width=0.0 ).set_fill(_a , opacity=0.7 ) if i == 0: cpu_target.next_to(cpu_left_col_base[0].get_corner(DOWN + LEFT ) , buff=0.02 , direction=_a ) cpu_target.set_x(cpu_target.get_x() + 0.1 ) elif i == 3: cpu_target.next_to(model_cpu_arr[0] , direction=_a , buff=0.0 ) else: cpu_target.next_to(model_cpu_arr[i - 1] , direction=_a , buff=0.0 ) self.add(_a ) model_cpu_arr.append(_a ) self.add(*_a , *_a , *_a ) _SCREAMING_SNAKE_CASE =[mem.copy() for i in range(6 )] _SCREAMING_SNAKE_CASE =VGroup(*_a ).arrange(_a , buff=0 ) _SCREAMING_SNAKE_CASE =Text('''Loaded Checkpoint''' , font_size=24 ) _SCREAMING_SNAKE_CASE =Group(_a , _a ).arrange(_a , buff=0.5 , aligned_edge=_a ) checkpoint.move_to([3, 0.5, 0] ) self.add(_a ) _SCREAMING_SNAKE_CASE =[] _SCREAMING_SNAKE_CASE =[] for i, rect in enumerate(_a ): _SCREAMING_SNAKE_CASE =fill.copy().set_fill(_a , opacity=0.7 ) target.move_to(_a ) ckpt_arr.append(_a ) _SCREAMING_SNAKE_CASE =target.copy() if i < 5: cpu_target.move_to(cpu_left_col_base[i + 1] ) else: cpu_target.move_to(cpu_right_col_base[i - 5] ) ckpt_cpu_arr.append(_a ) self.add(*_a , *_a ) _SCREAMING_SNAKE_CASE =Square(side_length=2.2 ) key.move_to([-5, 2, 0] ) _SCREAMING_SNAKE_CASE =MarkupText( f"<b>Key:</b>\n\n<span fgcolor='{YELLOW}'>โ—</span> Empty Model" , font_size=18 , ) key_text.move_to([-5, 2.4, 0] ) self.add(_a , _a ) _SCREAMING_SNAKE_CASE =MarkupText( f"<span fgcolor='{BLUE}'>โ—</span> Checkpoint" , font_size=18 , ) blue_text.next_to(_a , DOWN * 2.4 , aligned_edge=key_text.get_left() ) self.add(_a ) _SCREAMING_SNAKE_CASE =MarkupText( f"Based on the passed in configuration, weights are stored in\na variety of np.memmaps on disk or to a particular device." , font_size=24 , ) step_a.move_to([2, 2, 0] ) _SCREAMING_SNAKE_CASE =[meta_mem.copy() for i in range(6 )] _SCREAMING_SNAKE_CASE =[meta_mem.copy() for i in range(6 )] _SCREAMING_SNAKE_CASE =VGroup(*_a ).arrange(_a , buff=0 ) _SCREAMING_SNAKE_CASE =VGroup(*_a ).arrange(_a , buff=0 ) _SCREAMING_SNAKE_CASE =VGroup(_a , _a ).arrange(_a , buff=0 ) _SCREAMING_SNAKE_CASE =Text('''Disk''' , font_size=24 ) _SCREAMING_SNAKE_CASE =Group(_a , _a ).arrange(_a , buff=0.5 , aligned_edge=_a ) disk.move_to([-4.0, -1.25, 0] ) self.play(Write(_a , run_time=3 ) , Write(_a , run_time=1 ) , Create(_a , run_time=1 ) ) _SCREAMING_SNAKE_CASE =[] for i, rect in enumerate(_a ): _SCREAMING_SNAKE_CASE =rect.copy() target.generate_target() target.target.move_to(disk_left_col_base[i] ).scale(0.5 ) animations.append(MoveToTarget(_a , run_time=1.5 ) ) self.play(*_a ) self.play(FadeOut(_a ) ) _SCREAMING_SNAKE_CASE =MarkupText(f"Then, the checkpoint is removed from memory\nthrough garbage collection." , font_size=24 ) step_a.move_to([2, 2, 0] ) self.play(Write(_a , run_time=3 ) ) self.play( FadeOut(_a , _a , *_a , *_a ) , ) self.wait()
691
0
import argparse import requests import torch # pip3 install salesforce-lavis # I'm actually installing a slightly modified version: pip3 install git+https://github.com/nielsrogge/LAVIS.git@fix_lavis from lavis.models import load_model_and_preprocess from PIL import Image from transformers import ( AutoTokenizer, BlipaConfig, BlipaForConditionalGeneration, BlipaProcessor, BlipaVisionConfig, BlipImageProcessor, OPTConfig, TaConfig, ) from transformers.utils.constants import OPENAI_CLIP_MEAN, OPENAI_CLIP_STD def __lowerCamelCase ( ): '''simple docstring''' snake_case_ = 'https://storage.googleapis.com/sfr-vision-language-research/LAVIS/assets/merlion.png' snake_case_ = Image.open(requests.get(a__ , stream=a__ ).raw ).convert('RGB' ) return image def __lowerCamelCase ( UpperCamelCase__ ): '''simple docstring''' snake_case_ = [] # fmt: off # vision encoder rename_keys.append(('visual_encoder.cls_token', 'vision_model.embeddings.class_embedding') ) rename_keys.append(('visual_encoder.pos_embed', 'vision_model.embeddings.position_embedding') ) rename_keys.append(('visual_encoder.patch_embed.proj.weight', 'vision_model.embeddings.patch_embedding.weight') ) rename_keys.append(('visual_encoder.patch_embed.proj.bias', 'vision_model.embeddings.patch_embedding.bias') ) rename_keys.append(('ln_vision.weight', 'vision_model.post_layernorm.weight') ) rename_keys.append(('ln_vision.bias', 'vision_model.post_layernorm.bias') ) for i in range(config.vision_config.num_hidden_layers ): rename_keys.append((F'''visual_encoder.blocks.{i}.norm1.weight''', F'''vision_model.encoder.layers.{i}.layer_norm1.weight''') ) rename_keys.append((F'''visual_encoder.blocks.{i}.norm1.bias''', F'''vision_model.encoder.layers.{i}.layer_norm1.bias''') ) rename_keys.append((F'''visual_encoder.blocks.{i}.norm2.weight''', F'''vision_model.encoder.layers.{i}.layer_norm2.weight''') ) rename_keys.append((F'''visual_encoder.blocks.{i}.norm2.bias''', F'''vision_model.encoder.layers.{i}.layer_norm2.bias''') ) rename_keys.append((F'''visual_encoder.blocks.{i}.attn.qkv.weight''', F'''vision_model.encoder.layers.{i}.self_attn.qkv.weight''') ) rename_keys.append((F'''visual_encoder.blocks.{i}.attn.proj.weight''', F'''vision_model.encoder.layers.{i}.self_attn.projection.weight''',) ) rename_keys.append((F'''visual_encoder.blocks.{i}.attn.proj.bias''', F'''vision_model.encoder.layers.{i}.self_attn.projection.bias''') ) rename_keys.append((F'''visual_encoder.blocks.{i}.mlp.fc1.weight''', F'''vision_model.encoder.layers.{i}.mlp.fc1.weight''') ) rename_keys.append((F'''visual_encoder.blocks.{i}.mlp.fc1.bias''', F'''vision_model.encoder.layers.{i}.mlp.fc1.bias''') ) rename_keys.append((F'''visual_encoder.blocks.{i}.mlp.fc2.weight''', F'''vision_model.encoder.layers.{i}.mlp.fc2.weight''') ) rename_keys.append((F'''visual_encoder.blocks.{i}.mlp.fc2.bias''', F'''vision_model.encoder.layers.{i}.mlp.fc2.bias''') ) # QFormer rename_keys.append(('Qformer.bert.embeddings.LayerNorm.weight', 'qformer.layernorm.weight') ) rename_keys.append(('Qformer.bert.embeddings.LayerNorm.bias', 'qformer.layernorm.bias') ) # fmt: on return rename_keys def __lowerCamelCase ( UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ ): '''simple docstring''' snake_case_ = dct.pop(a__ ) snake_case_ = val def __lowerCamelCase ( UpperCamelCase__ , UpperCamelCase__ ): '''simple docstring''' for i in range(config.vision_config.num_hidden_layers ): # read in original q and v biases snake_case_ = state_dict.pop(F'''visual_encoder.blocks.{i}.attn.q_bias''' ) snake_case_ = state_dict.pop(F'''visual_encoder.blocks.{i}.attn.v_bias''' ) # next, set bias in the state dict snake_case_ = torch.cat((q_bias, torch.zeros_like(a__ , requires_grad=a__ ), v_bias) ) snake_case_ = qkv_bias def __lowerCamelCase ( UpperCamelCase__ , UpperCamelCase__ ): '''simple docstring''' snake_case_ = 364 if 'coco' in model_name else 224 snake_case_ = BlipaVisionConfig(image_size=a__ ).to_dict() # make sure the models have proper bos_token_id and eos_token_id set (important for generation) # seems like flan-T5 models don't have bos_token_id properly set? if "opt-2.7b" in model_name: snake_case_ = OPTConfig.from_pretrained('facebook/opt-2.7b' , eos_token_id=a__ ).to_dict() elif "opt-6.7b" in model_name: snake_case_ = OPTConfig.from_pretrained('facebook/opt-6.7b' , eos_token_id=a__ ).to_dict() elif "t5-xl" in model_name: snake_case_ = TaConfig.from_pretrained('google/flan-t5-xl' , dense_act_fn='gelu' , bos_token_id=1 ).to_dict() elif "t5-xxl" in model_name: snake_case_ = TaConfig.from_pretrained('google/flan-t5-xxl' , dense_act_fn='gelu' , bos_token_id=1 ).to_dict() snake_case_ = BlipaConfig(vision_config=a__ , text_config=a__ ) return config, image_size @torch.no_grad() def __lowerCamelCase ( UpperCamelCase__ , UpperCamelCase__=None , UpperCamelCase__=False ): '''simple docstring''' snake_case_ = ( AutoTokenizer.from_pretrained('facebook/opt-2.7b' ) if 'opt' in model_name else AutoTokenizer.from_pretrained('google/flan-t5-xl' ) ) snake_case_ = tokenizer('\n' , add_special_tokens=a__ ).input_ids[0] snake_case_ , snake_case_ = get_blipa_config(a__ , eos_token_id=a__ ) snake_case_ = BlipaForConditionalGeneration(a__ ).eval() snake_case_ = { 'blip2-opt-2.7b': ('blip2_opt', 'pretrain_opt2.7b'), 'blip2-opt-6.7b': ('blip2_opt', 'pretrain_opt6.7b'), 'blip2-opt-2.7b-coco': ('blip2_opt', 'caption_coco_opt2.7b'), 'blip2-opt-6.7b-coco': ('blip2_opt', 'caption_coco_opt6.7b'), 'blip2-flan-t5-xl': ('blip2_t5', 'pretrain_flant5xl'), 'blip2-flan-t5-xl-coco': ('blip2_t5', 'caption_coco_flant5xl'), 'blip2-flan-t5-xxl': ('blip2_t5', 'pretrain_flant5xxl'), } snake_case_ , snake_case_ = model_name_to_original[model_name] # load original model print('Loading original model...' ) snake_case_ = 'cuda' if torch.cuda.is_available() else 'cpu' snake_case_ , snake_case_ , snake_case_ = load_model_and_preprocess( name=a__ , model_type=a__ , is_eval=a__ , device=a__ ) original_model.eval() print('Done!' ) # update state dict keys snake_case_ = original_model.state_dict() snake_case_ = create_rename_keys(a__ ) for src, dest in rename_keys: rename_key(a__ , a__ , a__ ) # some keys can be renamed efficiently for key, val in state_dict.copy().items(): snake_case_ = state_dict.pop(a__ ) if key.startswith('Qformer.bert' ): snake_case_ = key.replace('Qformer.bert' , 'qformer' ) if "attention.self" in key: snake_case_ = key.replace('self' , 'attention' ) if "opt_proj" in key: snake_case_ = key.replace('opt_proj' , 'language_projection' ) if "t5_proj" in key: snake_case_ = key.replace('t5_proj' , 'language_projection' ) if key.startswith('opt' ): snake_case_ = key.replace('opt' , 'language' ) if key.startswith('t5' ): snake_case_ = key.replace('t5' , 'language' ) snake_case_ = val # read in qv biases read_in_q_v_bias(a__ , a__ ) snake_case_ , snake_case_ = hf_model.load_state_dict(a__ , strict=a__ ) assert len(a__ ) == 0 assert unexpected_keys == ["qformer.embeddings.position_ids"] snake_case_ = load_demo_image() snake_case_ = vis_processors['eval'](a__ ).unsqueeze(0 ).to(a__ ) snake_case_ = tokenizer(['\n'] , return_tensors='pt' ).input_ids.to(a__ ) # create processor snake_case_ = BlipImageProcessor( size={'height': image_size, 'width': image_size} , image_mean=a__ , image_std=a__ ) snake_case_ = BlipaProcessor(image_processor=a__ , tokenizer=a__ ) snake_case_ = processor(images=a__ , return_tensors='pt' ).pixel_values.to(a__ ) # make sure processor creates exact same pixel values assert torch.allclose(a__ , a__ ) original_model.to(a__ ) hf_model.to(a__ ) with torch.no_grad(): if "opt" in model_name: snake_case_ = original_model({'image': original_pixel_values, 'text_input': ['']} ).logits snake_case_ = hf_model(a__ , a__ ).logits else: snake_case_ = original_model( {'image': original_pixel_values, 'text_input': ['\n'], 'text_output': ['\n']} ).logits snake_case_ = input_ids.masked_fill(input_ids == tokenizer.pad_token_id , -100 ) snake_case_ = hf_model(a__ , a__ , labels=a__ ).logits assert original_logits.shape == logits.shape print('First values of original logits:' , original_logits[0, :3, :3] ) print('First values of HF logits:' , logits[0, :3, :3] ) # assert values if model_name == "blip2-flan-t5-xl": snake_case_ = torch.tensor( [[-41.58_50, -4.44_40, -8.99_22], [-47.43_22, -5.91_43, -1.73_40]] , device=a__ ) assert torch.allclose(logits[0, :3, :3] , a__ , atol=1E-4 ) elif model_name == "blip2-flan-t5-xl-coco": snake_case_ = torch.tensor( [[-57.01_09, -9.89_67, -12.62_80], [-68.65_78, -12.71_91, -10.50_65]] , device=a__ ) else: # cast to same type snake_case_ = logits.dtype assert torch.allclose(original_logits.to(a__ ) , a__ , atol=1E-2 ) print('Looks ok!' ) print('Generating a caption...' ) snake_case_ = '' snake_case_ = tokenizer(a__ , return_tensors='pt' ).input_ids.to(a__ ) snake_case_ = original_model.generate({'image': original_pixel_values} ) snake_case_ = hf_model.generate( a__ , a__ , do_sample=a__ , num_beams=5 , max_length=30 , min_length=1 , top_p=0.9 , repetition_penalty=1.0 , length_penalty=1.0 , temperature=1 , ) print('Original generation:' , a__ ) snake_case_ = input_ids.shape[1] snake_case_ = processor.batch_decode(outputs[:, prompt_length:] , skip_special_tokens=a__ ) snake_case_ = [text.strip() for text in output_text] print('HF generation:' , a__ ) if pytorch_dump_folder_path is not None: processor.save_pretrained(a__ ) hf_model.save_pretrained(a__ ) if push_to_hub: processor.push_to_hub(F'''nielsr/{model_name}''' ) hf_model.push_to_hub(F'''nielsr/{model_name}''' ) if __name__ == "__main__": _UpperCAmelCase : Optional[Any] = argparse.ArgumentParser() _UpperCAmelCase : List[Any] = [ '''blip2-opt-2.7b''', '''blip2-opt-6.7b''', '''blip2-opt-2.7b-coco''', '''blip2-opt-6.7b-coco''', '''blip2-flan-t5-xl''', '''blip2-flan-t5-xl-coco''', '''blip2-flan-t5-xxl''', ] parser.add_argument( """--model_name""", default="""blip2-opt-2.7b""", choices=choices, type=str, help="""Path to hf config.json of model to convert""", ) parser.add_argument("""--pytorch_dump_folder_path""", default=None, type=str, help="""Path to the output PyTorch model.""") parser.add_argument( """--push_to_hub""", action="""store_true""", help="""Whether to push the model and processor to the hub after converting""", ) _UpperCAmelCase : Optional[Any] = parser.parse_args() convert_blipa_checkpoint(args.model_name, args.pytorch_dump_folder_path, args.push_to_hub)
362
import json from typing import TYPE_CHECKING, List, Optional, Tuple from tokenizers import pre_tokenizers, processors from ...tokenization_utils_base import AddedToken, BatchEncoding from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import logging from .tokenization_blenderbot import BlenderbotTokenizer if TYPE_CHECKING: from transformers.pipelines.conversational import Conversation snake_case_ : str = logging.get_logger(__name__) snake_case_ : List[Any] = { '''vocab_file''': '''vocab.json''', '''merges_file''': '''merges.txt''', '''tokenizer_config_file''': '''tokenizer_config.json''', } snake_case_ : Any = { '''vocab_file''': {'''facebook/blenderbot-3B''': '''https://huggingface.co/facebook/blenderbot-3B/resolve/main/vocab.json'''}, '''merges_file''': {'''facebook/blenderbot-3B''': '''https://huggingface.co/facebook/blenderbot-3B/resolve/main/merges.txt'''}, '''tokenizer_config_file''': { '''facebook/blenderbot-3B''': '''https://huggingface.co/facebook/blenderbot-3B/resolve/main/tokenizer_config.json''' }, } snake_case_ : List[str] = {'''facebook/blenderbot-3B''': 1_28} class A__ ( UpperCamelCase__ ): UpperCAmelCase = VOCAB_FILES_NAMES UpperCAmelCase = PRETRAINED_VOCAB_FILES_MAP UpperCAmelCase = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES UpperCAmelCase = ["input_ids", "attention_mask"] UpperCAmelCase = BlenderbotTokenizer def __init__( self : Dict , _a : str=None , _a : Optional[int]=None , _a : List[str]=None , _a : int="replace" , _a : Dict="<s>" , _a : Optional[Any]="</s>" , _a : Any="</s>" , _a : int="<s>" , _a : int="<unk>" , _a : Optional[int]="<pad>" , _a : Tuple="<mask>" , _a : Tuple=False , _a : Union[str, Any]=True , **_a : List[str] , ) -> Optional[int]: """simple docstring""" super().__init__( _a , _a , tokenizer_file=_a , errors=_a , bos_token=_a , eos_token=_a , sep_token=_a , cls_token=_a , unk_token=_a , pad_token=_a , mask_token=_a , add_prefix_space=_a , trim_offsets=_a , **_a , ) _SCREAMING_SNAKE_CASE =json.loads(self.backend_tokenizer.pre_tokenizer.__getstate__() ) if pre_tok_state.get('''add_prefix_space''' , _a ) != add_prefix_space: _SCREAMING_SNAKE_CASE =getattr(_a , pre_tok_state.pop('''type''' ) ) _SCREAMING_SNAKE_CASE =add_prefix_space _SCREAMING_SNAKE_CASE =pre_tok_class(**_a ) _SCREAMING_SNAKE_CASE =add_prefix_space _SCREAMING_SNAKE_CASE ='''post_processor''' _SCREAMING_SNAKE_CASE =getattr(self.backend_tokenizer , _a , _a ) if tokenizer_component_instance: _SCREAMING_SNAKE_CASE =json.loads(tokenizer_component_instance.__getstate__() ) # The lists 'sep' and 'cls' must be cased in tuples for the object `post_processor_class` if "sep" in state: _SCREAMING_SNAKE_CASE =tuple(state['''sep'''] ) if "cls" in state: _SCREAMING_SNAKE_CASE =tuple(state['''cls'''] ) _SCREAMING_SNAKE_CASE =False if state.get('''add_prefix_space''' , _a ) != add_prefix_space: _SCREAMING_SNAKE_CASE =add_prefix_space _SCREAMING_SNAKE_CASE =True if state.get('''trim_offsets''' , _a ) != trim_offsets: _SCREAMING_SNAKE_CASE =trim_offsets _SCREAMING_SNAKE_CASE =True if changes_to_apply: _SCREAMING_SNAKE_CASE =getattr(_a , state.pop('''type''' ) ) _SCREAMING_SNAKE_CASE =component_class(**_a ) setattr(self.backend_tokenizer , _a , _a ) @property # Copied from transformers.models.roberta.tokenization_roberta_fast.RobertaTokenizerFast.mask_token with Roberta->Blenderbot, RoBERTa->Blenderbot def __UpperCamelCase ( self : Tuple ) -> str: """simple docstring""" if self._mask_token is None: if self.verbose: logger.error('''Using mask_token, but it is not set yet.''' ) return None return str(self._mask_token ) @mask_token.setter def __UpperCamelCase ( self : Optional[Any] , _a : str ) -> List[str]: """simple docstring""" _SCREAMING_SNAKE_CASE =AddedToken(_a , lstrip=_a , rstrip=_a ) if isinstance(_a , _a ) else value _SCREAMING_SNAKE_CASE =value def __UpperCamelCase ( self : Optional[Any] , *_a : str , **_a : int ) -> BatchEncoding: """simple docstring""" _SCREAMING_SNAKE_CASE =kwargs.get('''is_split_into_words''' , _a ) assert self.add_prefix_space or not is_split_into_words, ( f"You need to instantiate {self.__class__.__name__} with add_prefix_space=True " "to use it with pretokenized inputs." ) return super()._batch_encode_plus(*_a , **_a ) def __UpperCamelCase ( self : List[Any] , *_a : Optional[int] , **_a : Union[str, Any] ) -> BatchEncoding: """simple docstring""" _SCREAMING_SNAKE_CASE =kwargs.get('''is_split_into_words''' , _a ) assert self.add_prefix_space or not is_split_into_words, ( f"You need to instantiate {self.__class__.__name__} with add_prefix_space=True " "to use it with pretokenized inputs." ) return super()._encode_plus(*_a , **_a ) def __UpperCamelCase ( self : Dict , _a : str , _a : Optional[str] = None ) -> Tuple[str]: """simple docstring""" _SCREAMING_SNAKE_CASE =self._tokenizer.model.save(_a , name=_a ) return tuple(_a ) def __UpperCamelCase ( self : Tuple , _a : List[int] , _a : Optional[List[int]] = None ) -> List[int]: """simple docstring""" _SCREAMING_SNAKE_CASE =[self.sep_token_id] _SCREAMING_SNAKE_CASE =[self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep ) * [0] return len(cls + token_ids_a + sep + sep + token_ids_a + sep ) * [0] def __UpperCamelCase ( self : Tuple , _a : List[int] , _a : Optional[List[int]] = None ) -> Optional[Any]: """simple docstring""" return token_ids_a + [self.eos_token_id] def __UpperCamelCase ( self : Any , _a : "Conversation" ) -> List[int]: """simple docstring""" _SCREAMING_SNAKE_CASE =[] for is_user, text in conversation.iter_texts(): if is_user: # We need to space prefix as it's being done within blenderbot inputs.append(''' ''' + text ) else: # Generated responses should contain them already. inputs.append(_a ) _SCREAMING_SNAKE_CASE =''' '''.join(_a ) _SCREAMING_SNAKE_CASE =self.encode(_a ) if len(_a ) > self.model_max_length: _SCREAMING_SNAKE_CASE =input_ids[-self.model_max_length :] logger.warning(f"Trimmed input from conversation as it was longer than {self.model_max_length} tokens." ) return input_ids
691
0
"""simple docstring""" import time import warnings from abc import ABC from copy import deepcopy from typing import Optional import torch from ..utils import add_start_docstrings, logging _A = logging.get_logger(__name__) _A = R''' Args: input_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`): Indices of input sequence tokens in the vocabulary. Indices can be obtained using [`AutoTokenizer`]. See [`PreTrainedTokenizer.encode`] and [`PreTrainedTokenizer.__call__`] for details. [What are input IDs?](../glossary#input-ids) scores (`torch.FloatTensor` of shape `(batch_size, config.vocab_size)`): Prediction scores of a language modeling head. These can be scores for each vocabulary token before SoftMax or scores for each vocabulary token after SoftMax. kwargs (`Dict[str, Any]`, *optional*): Additional stopping criteria specific kwargs. Return: `bool`. `False` indicates we should continue, `True` indicates we should stop. ''' class lowerCamelCase ( UpperCamelCase__ ): '''simple docstring''' @add_start_docstrings(_a ) def __call__(self , _lowerCamelCase , _lowerCamelCase , **_lowerCamelCase ): """simple docstring""" raise NotImplementedError("""StoppingCriteria needs to be subclassed""" ) class lowerCamelCase ( UpperCamelCase__ ): '''simple docstring''' def __init__(self , _lowerCamelCase , _lowerCamelCase = None ): """simple docstring""" UpperCAmelCase__ : Any = max_length UpperCAmelCase__ : Union[str, Any] = max_position_embeddings @add_start_docstrings(_a ) def __call__(self , _lowerCamelCase , _lowerCamelCase , **_lowerCamelCase ): """simple docstring""" UpperCAmelCase__ : Tuple = input_ids.shape[-1] UpperCAmelCase__ : int = cur_len >= self.max_length if self.max_position_embeddings is not None and not is_done and cur_len >= self.max_position_embeddings: logger.warning_once( """This is a friendly reminder - the current text generation call will exceed the model\'s predefined """ F"""maximum length ({self.max_position_embeddings}). Depending on the model, you may observe """ """exceptions, performance degradation, or nothing at all.""" ) return is_done class lowerCamelCase ( UpperCamelCase__ ): '''simple docstring''' def __init__(self , _lowerCamelCase , _lowerCamelCase ): """simple docstring""" warnings.warn( """The class `MaxNewTokensCriteria` is deprecated. """ F"""Please use `MaxLengthCriteria(max_length={start_length + max_new_tokens})` """ """with `max_length = start_length + max_new_tokens` instead.""" , _a , ) UpperCAmelCase__ : Union[str, Any] = start_length UpperCAmelCase__ : int = max_new_tokens UpperCAmelCase__ : Dict = start_length + max_new_tokens @add_start_docstrings(_a ) def __call__(self , _lowerCamelCase , _lowerCamelCase , **_lowerCamelCase ): """simple docstring""" return input_ids.shape[-1] >= self.max_length class lowerCamelCase ( UpperCamelCase__ ): '''simple docstring''' def __init__(self , _lowerCamelCase , _lowerCamelCase = None ): """simple docstring""" UpperCAmelCase__ : int = max_time UpperCAmelCase__ : List[str] = time.time() if initial_timestamp is None else initial_timestamp @add_start_docstrings(_a ) def __call__(self , _lowerCamelCase , _lowerCamelCase , **_lowerCamelCase ): """simple docstring""" return time.time() - self.initial_timestamp > self.max_time class lowerCamelCase ( UpperCamelCase__ ): '''simple docstring''' @add_start_docstrings(_a ) def __call__(self , _lowerCamelCase , _lowerCamelCase , **_lowerCamelCase ): """simple docstring""" return any(criteria(_a , _a ) for criteria in self ) @property def _a (self ): """simple docstring""" for stopping_criterium in self: if isinstance(_a , _a ): return stopping_criterium.max_length elif isinstance(_a , _a ): return stopping_criterium.max_length return None def a__ ( lowerCAmelCase , lowerCAmelCase ) -> Optional[int]: UpperCAmelCase__ : Union[str, Any] = stopping_criteria.max_length UpperCAmelCase__ : int = deepcopy(a__ ) if stopping_max_length is not None and stopping_max_length != max_length: warnings.warn("""You set different `max_length` for stopping criteria and `max_length` parameter""" , a__ ) elif stopping_max_length is None: new_stopping_criteria.append(MaxLengthCriteria(max_length=a__ ) ) return new_stopping_criteria
182
import json import os import shutil import tempfile import unittest import numpy as np import pytest from transformers import BertTokenizer, BertTokenizerFast from transformers.models.bert.tokenization_bert import VOCAB_FILES_NAMES from transformers.testing_utils import require_vision from transformers.utils import FEATURE_EXTRACTOR_NAME, is_vision_available if is_vision_available(): from PIL import Image from transformers import ChineseCLIPImageProcessor, ChineseCLIPProcessor @require_vision class A__ ( unittest.TestCase ): def __UpperCamelCase ( self : int ) -> Any: """simple docstring""" _SCREAMING_SNAKE_CASE =tempfile.mkdtemp() _SCREAMING_SNAKE_CASE =[ '''[UNK]''', '''[CLS]''', '''[SEP]''', '''[PAD]''', '''[MASK]''', '''็š„''', '''ไปท''', '''ๆ ผ''', '''ๆ˜ฏ''', '''15''', '''ไพฟ''', '''alex''', '''##andra''', '''๏ผŒ''', '''ใ€‚''', '''-''', '''t''', '''shirt''', ] _SCREAMING_SNAKE_CASE =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] ) ) _SCREAMING_SNAKE_CASE ={ '''do_resize''': True, '''size''': {'''height''': 224, '''width''': 224}, '''do_center_crop''': True, '''crop_size''': {'''height''': 18, '''width''': 18}, '''do_normalize''': True, '''image_mean''': [0.48_14_54_66, 0.4_57_82_75, 0.40_82_10_73], '''image_std''': [0.26_86_29_54, 0.26_13_02_58, 0.27_57_77_11], '''do_convert_rgb''': True, } _SCREAMING_SNAKE_CASE =os.path.join(self.tmpdirname , _a ) with open(self.image_processor_file , '''w''' , encoding='''utf-8''' ) as fp: json.dump(_a , _a ) def __UpperCamelCase ( self : Optional[int] , **_a : str ) -> List[str]: """simple docstring""" return BertTokenizer.from_pretrained(self.tmpdirname , **_a ) def __UpperCamelCase ( self : List[Any] , **_a : Any ) -> Dict: """simple docstring""" return BertTokenizerFast.from_pretrained(self.tmpdirname , **_a ) def __UpperCamelCase ( self : int , **_a : Optional[Any] ) -> Any: """simple docstring""" return ChineseCLIPImageProcessor.from_pretrained(self.tmpdirname , **_a ) def __UpperCamelCase ( self : str ) -> Union[str, Any]: """simple docstring""" shutil.rmtree(self.tmpdirname ) def __UpperCamelCase ( self : int ) -> Optional[Any]: """simple docstring""" _SCREAMING_SNAKE_CASE =[np.random.randint(255 , size=(3, 30, 400) , dtype=np.uinta )] _SCREAMING_SNAKE_CASE =[Image.fromarray(np.moveaxis(_a , 0 , -1 ) ) for x in image_inputs] return image_inputs def __UpperCamelCase ( self : Any ) -> List[Any]: """simple docstring""" _SCREAMING_SNAKE_CASE =self.get_tokenizer() _SCREAMING_SNAKE_CASE =self.get_rust_tokenizer() _SCREAMING_SNAKE_CASE =self.get_image_processor() _SCREAMING_SNAKE_CASE =ChineseCLIPProcessor(tokenizer=_a , image_processor=_a ) processor_slow.save_pretrained(self.tmpdirname ) _SCREAMING_SNAKE_CASE =ChineseCLIPProcessor.from_pretrained(self.tmpdirname , use_fast=_a ) _SCREAMING_SNAKE_CASE =ChineseCLIPProcessor(tokenizer=_a , image_processor=_a ) processor_fast.save_pretrained(self.tmpdirname ) _SCREAMING_SNAKE_CASE =ChineseCLIPProcessor.from_pretrained(self.tmpdirname ) self.assertEqual(processor_slow.tokenizer.get_vocab() , tokenizer_slow.get_vocab() ) self.assertEqual(processor_fast.tokenizer.get_vocab() , tokenizer_fast.get_vocab() ) self.assertEqual(tokenizer_slow.get_vocab() , tokenizer_fast.get_vocab() ) self.assertIsInstance(processor_slow.tokenizer , _a ) self.assertIsInstance(processor_fast.tokenizer , _a ) self.assertEqual(processor_slow.image_processor.to_json_string() , image_processor.to_json_string() ) self.assertEqual(processor_fast.image_processor.to_json_string() , image_processor.to_json_string() ) self.assertIsInstance(processor_slow.image_processor , _a ) self.assertIsInstance(processor_fast.image_processor , _a ) def __UpperCamelCase ( self : str ) -> Union[str, Any]: """simple docstring""" _SCREAMING_SNAKE_CASE =ChineseCLIPProcessor(tokenizer=self.get_tokenizer() , image_processor=self.get_image_processor() ) processor.save_pretrained(self.tmpdirname ) _SCREAMING_SNAKE_CASE =self.get_tokenizer(cls_token='''(CLS)''' , sep_token='''(SEP)''' ) _SCREAMING_SNAKE_CASE =self.get_image_processor(do_normalize=_a ) _SCREAMING_SNAKE_CASE =ChineseCLIPProcessor.from_pretrained( self.tmpdirname , cls_token='''(CLS)''' , sep_token='''(SEP)''' , do_normalize=_a ) self.assertEqual(processor.tokenizer.get_vocab() , tokenizer_add_kwargs.get_vocab() ) self.assertIsInstance(processor.tokenizer , _a ) self.assertEqual(processor.image_processor.to_json_string() , image_processor_add_kwargs.to_json_string() ) self.assertIsInstance(processor.image_processor , _a ) def __UpperCamelCase ( self : List[Any] ) -> Tuple: """simple docstring""" _SCREAMING_SNAKE_CASE =self.get_image_processor() _SCREAMING_SNAKE_CASE =self.get_tokenizer() _SCREAMING_SNAKE_CASE =ChineseCLIPProcessor(tokenizer=_a , image_processor=_a ) _SCREAMING_SNAKE_CASE =self.prepare_image_inputs() _SCREAMING_SNAKE_CASE =image_processor(_a , return_tensors='''np''' ) _SCREAMING_SNAKE_CASE =processor(images=_a , return_tensors='''np''' ) for key in input_feat_extract.keys(): self.assertAlmostEqual(input_feat_extract[key].sum() , input_processor[key].sum() , delta=1E-2 ) def __UpperCamelCase ( self : Union[str, Any] ) -> Any: """simple docstring""" _SCREAMING_SNAKE_CASE =self.get_image_processor() _SCREAMING_SNAKE_CASE =self.get_tokenizer() _SCREAMING_SNAKE_CASE =ChineseCLIPProcessor(tokenizer=_a , image_processor=_a ) _SCREAMING_SNAKE_CASE ='''Alexandra๏ผŒT-shirt็š„ไปทๆ ผๆ˜ฏ15ไพฟๅฃซใ€‚''' _SCREAMING_SNAKE_CASE =processor(text=_a ) _SCREAMING_SNAKE_CASE =tokenizer(_a ) for key in encoded_tok.keys(): self.assertListEqual(encoded_tok[key] , encoded_processor[key] ) def __UpperCamelCase ( self : Tuple ) -> int: """simple docstring""" _SCREAMING_SNAKE_CASE =self.get_image_processor() _SCREAMING_SNAKE_CASE =self.get_tokenizer() _SCREAMING_SNAKE_CASE =ChineseCLIPProcessor(tokenizer=_a , image_processor=_a ) _SCREAMING_SNAKE_CASE ='''Alexandra๏ผŒT-shirt็š„ไปทๆ ผๆ˜ฏ15ไพฟๅฃซใ€‚''' _SCREAMING_SNAKE_CASE =self.prepare_image_inputs() _SCREAMING_SNAKE_CASE =processor(text=_a , images=_a ) self.assertListEqual(list(inputs.keys() ) , ['''input_ids''', '''token_type_ids''', '''attention_mask''', '''pixel_values'''] ) # test if it raises when no input is passed with pytest.raises(_a ): processor() def __UpperCamelCase ( self : Optional[Any] ) -> Optional[int]: """simple docstring""" _SCREAMING_SNAKE_CASE =self.get_image_processor() _SCREAMING_SNAKE_CASE =self.get_tokenizer() _SCREAMING_SNAKE_CASE =ChineseCLIPProcessor(tokenizer=_a , image_processor=_a ) _SCREAMING_SNAKE_CASE =[[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]] _SCREAMING_SNAKE_CASE =processor.batch_decode(_a ) _SCREAMING_SNAKE_CASE =tokenizer.batch_decode(_a ) self.assertListEqual(_a , _a ) def __UpperCamelCase ( self : Any ) -> List[str]: """simple docstring""" _SCREAMING_SNAKE_CASE =self.get_image_processor() _SCREAMING_SNAKE_CASE =self.get_tokenizer() _SCREAMING_SNAKE_CASE =ChineseCLIPProcessor(tokenizer=_a , image_processor=_a ) _SCREAMING_SNAKE_CASE ='''Alexandra๏ผŒT-shirt็š„ไปทๆ ผๆ˜ฏ15ไพฟๅฃซใ€‚''' _SCREAMING_SNAKE_CASE =self.prepare_image_inputs() _SCREAMING_SNAKE_CASE =processor(text=_a , images=_a ) self.assertListEqual(list(inputs.keys() ) , processor.model_input_names )
691
0
"""simple docstring""" from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_sentencepiece_available, is_speech_available, is_tf_available, is_torch_available, ) UpperCAmelCase : List[Any] = { '''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: UpperCAmelCase : Optional[int] = ['''Speech2TextTokenizer'''] try: if not is_speech_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCAmelCase : int = ['''Speech2TextFeatureExtractor'''] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCAmelCase : Dict = [ '''TF_SPEECH_TO_TEXT_PRETRAINED_MODEL_ARCHIVE_LIST''', '''TFSpeech2TextForConditionalGeneration''', '''TFSpeech2TextModel''', '''TFSpeech2TextPreTrainedModel''', ] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCAmelCase : Any = [ '''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 UpperCAmelCase : Optional[Any] = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
139
# Usage: # ./gen-card-allenai-wmt16.py import os from pathlib import Path def lowerCamelCase( a__ ,a__ ,a__ ,a__): _SCREAMING_SNAKE_CASE ={ '''en''': '''Machine learning is great, isn\'t it?''', '''ru''': '''ะœะฐัˆะธะฝะฝะพะต ะพะฑัƒั‡ะตะฝะธะต - ัั‚ะพ ะทะดะพั€ะพะฒะพ, ะฝะต ั‚ะฐะบ ะปะธ?''', '''de''': '''Maschinelles Lernen ist groรŸartig, nicht wahr?''', } # BLUE scores as follows: # "pair": [fairseq, transformers] _SCREAMING_SNAKE_CASE ={ '''wmt16-en-de-dist-12-1''': [28.3, 27.52], '''wmt16-en-de-dist-6-1''': [27.4, 27.11], '''wmt16-en-de-12-1''': [26.9, 25.75], } _SCREAMING_SNAKE_CASE =f"{src_lang}-{tgt_lang}" _SCREAMING_SNAKE_CASE =f"\n---\nlanguage:\n- {src_lang}\n- {tgt_lang}\nthumbnail:\ntags:\n- translation\n- wmt16\n- allenai\nlicense: apache-2.0\ndatasets:\n- wmt16\nmetrics:\n- bleu\n---\n\n# FSMT\n\n## Model description\n\nThis is a ported version of fairseq-based [wmt16 transformer](https://github.com/jungokasai/deep-shallow/) for {src_lang}-{tgt_lang}.\n\nFor more details, please, see [Deep Encoder, Shallow Decoder: Reevaluating the Speed-Quality Tradeoff in Machine Translation](https://arxiv.org/abs/2006.10369).\n\nAll 3 models are available:\n\n* [wmt16-en-de-dist-12-1](https://huggingface.co/allenai/wmt16-en-de-dist-12-1)\n* [wmt16-en-de-dist-6-1](https://huggingface.co/allenai/wmt16-en-de-dist-6-1)\n* [wmt16-en-de-12-1](https://huggingface.co/allenai/wmt16-en-de-12-1)\n\n\n## Intended uses & limitations\n\n#### How to use\n\n```python\nfrom transformers import FSMTForConditionalGeneration, FSMTTokenizer\nmname = \"allenai/{model_name}\"\ntokenizer = FSMTTokenizer.from_pretrained(mname)\nmodel = FSMTForConditionalGeneration.from_pretrained(mname)\n\ninput = \"{texts[src_lang]}\"\ninput_ids = tokenizer.encode(input, return_tensors=\"pt\")\noutputs = model.generate(input_ids)\ndecoded = tokenizer.decode(outputs[0], skip_special_tokens=True)\nprint(decoded) # {texts[tgt_lang]}\n\n```\n\n#### Limitations and bias\n\n\n## Training data\n\nPretrained weights were left identical to the original model released by allenai. For more details, please, see the [paper](https://arxiv.org/abs/2006.10369).\n\n## Eval results\n\nHere are the BLEU scores:\n\nmodel | fairseq | transformers\n-------|---------|----------\n{model_name} | {scores[model_name][0]} | {scores[model_name][1]}\n\nThe score is slightly below the score reported in the paper, as the researchers don't use `sacrebleu` and measure the score on tokenized outputs. `transformers` score was measured using `sacrebleu` on detokenized outputs.\n\nThe score was calculated using this code:\n\n```bash\ngit clone https://github.com/huggingface/transformers\ncd transformers\nexport PAIR={pair}\nexport DATA_DIR=data/$PAIR\nexport SAVE_DIR=data/$PAIR\nexport BS=8\nexport NUM_BEAMS=5\nmkdir -p $DATA_DIR\nsacrebleu -t wmt16 -l $PAIR --echo src > $DATA_DIR/val.source\nsacrebleu -t wmt16 -l $PAIR --echo ref > $DATA_DIR/val.target\necho $PAIR\nPYTHONPATH=\"src:examples/seq2seq\" python examples/seq2seq/run_eval.py allenai/{model_name} $DATA_DIR/val.source $SAVE_DIR/test_translations.txt --reference_path $DATA_DIR/val.target --score_path $SAVE_DIR/test_bleu.json --bs $BS --task translation --num_beams $NUM_BEAMS\n```\n\n## Data Sources\n\n- [training, etc.](http://www.statmt.org/wmt16/)\n- [test set](http://matrix.statmt.org/test_sets/newstest2016.tgz?1504722372)\n\n\n### BibTeX entry and citation info\n\n```\n@misc{{kasai2020deep,\n title={{Deep Encoder, Shallow Decoder: Reevaluating the Speed-Quality Tradeoff in Machine Translation}},\n author={{Jungo Kasai and Nikolaos Pappas and Hao Peng and James Cross and Noah A. Smith}},\n year={{2020}},\n eprint={{2006.10369}},\n archivePrefix={{arXiv}},\n primaryClass={{cs.CL}}\n}}\n```\n\n" model_card_dir.mkdir(parents=a__ ,exist_ok=a__) _SCREAMING_SNAKE_CASE =os.path.join(a__ ,'''README.md''') print(f"Generating {path}") with open(a__ ,'''w''' ,encoding='''utf-8''') as f: f.write(a__) # make sure we are under the root of the project snake_case_ : Any = Path(__file__).resolve().parent.parent.parent snake_case_ : Tuple = repo_dir / '''model_cards''' for model_name in ["wmt16-en-de-dist-12-1", "wmt16-en-de-dist-6-1", "wmt16-en-de-12-1"]: snake_case_ : Union[str, Any] = model_cards_dir / '''allenai''' / model_name write_model_card(model_card_dir, src_lang='''en''', tgt_lang='''de''', model_name=model_name)
691
0
from ..utils import DummyObject, requires_backends class _UpperCamelCase ( metaclass=UpperCamelCase__ ): '''simple docstring''' a_ : int = ["torch", "scipy"] def __init__( self : Optional[Any] , *_lowerCamelCase : Union[str, Any] , **_lowerCamelCase : Optional[Any] ): '''simple docstring''' requires_backends(self , ["""torch""", """scipy"""] ) @classmethod def _snake_case ( cls : str , *_lowerCamelCase : Any , **_lowerCamelCase : Any ): '''simple docstring''' requires_backends(cls , ["""torch""", """scipy"""] ) @classmethod def _snake_case ( cls : Tuple , *_lowerCamelCase : Any , **_lowerCamelCase : Dict ): '''simple docstring''' requires_backends(cls , ["""torch""", """scipy"""] )
519
from typing import TYPE_CHECKING from ....utils import _LazyModule snake_case_ : Dict = {'''tokenization_tapex''': ['''TapexTokenizer''']} if TYPE_CHECKING: from .tokenization_tapex import TapexTokenizer else: import sys snake_case_ : Union[str, Any] = _LazyModule(__name__, globals()['''__file__'''], _import_structure)
691
0
import math from collections import defaultdict from typing import List, Optional, Tuple, Union import numpy as np import torch from ..configuration_utils import ConfigMixin, register_to_config from .scheduling_utils import KarrasDiffusionSchedulers, SchedulerMixin, SchedulerOutput def __A ( _A , _A=0.999 , _A="cosine" , ): """simple docstring""" if alpha_transform_type == "cosine": def alpha_bar_fn(_A ): return math.cos((t + 0.008) / 1.008 * math.pi / 2 ) ** 2 elif alpha_transform_type == "exp": def alpha_bar_fn(_A ): return math.exp(t * -12.0 ) else: raise ValueError(f"""Unsupported alpha_tranform_type: {alpha_transform_type}""" ) __a = [] for i in range(a__ ): __a = i / num_diffusion_timesteps __a = (i + 1) / num_diffusion_timesteps betas.append(min(1 - alpha_bar_fn(a__ ) / alpha_bar_fn(a__ ) , a__ ) ) return torch.tensor(a__ , dtype=torch.floataa ) class A_ ( UpperCamelCase__ , UpperCamelCase__ ): _SCREAMING_SNAKE_CASE = [e.name for e in KarrasDiffusionSchedulers] _SCREAMING_SNAKE_CASE = 2 @register_to_config def __init__( self : int , __SCREAMING_SNAKE_CASE : int = 10_00 , __SCREAMING_SNAKE_CASE : float = 0.0_00_85 , __SCREAMING_SNAKE_CASE : float = 0.0_12 , __SCREAMING_SNAKE_CASE : str = "linear" , __SCREAMING_SNAKE_CASE : Optional[Union[np.ndarray, List[float]]] = None , __SCREAMING_SNAKE_CASE : str = "epsilon" , __SCREAMING_SNAKE_CASE : str = "linspace" , __SCREAMING_SNAKE_CASE : int = 0 , ): if trained_betas is not None: __a = torch.tensor(_a , dtype=torch.floataa ) elif beta_schedule == "linear": __a = torch.linspace(_a , _a , _a , dtype=torch.floataa ) elif beta_schedule == "scaled_linear": # this schedule is very specific to the latent diffusion model. __a = ( torch.linspace(beta_start**0.5 , beta_end**0.5 , _a , dtype=torch.floataa ) ** 2 ) elif beta_schedule == "squaredcos_cap_v2": # Glide cosine schedule __a = betas_for_alpha_bar(_a ) else: raise NotImplementedError(f"""{beta_schedule} does is not implemented for {self.__class__}""" ) __a = 1.0 - self.betas __a = torch.cumprod(self.alphas , dim=0 ) # set all values self.set_timesteps(_a , _a , _a ) def _UpperCAmelCase ( self : Any , __SCREAMING_SNAKE_CASE : Tuple , __SCREAMING_SNAKE_CASE : Optional[int]=None ): if schedule_timesteps is None: __a = self.timesteps __a = (schedule_timesteps == timestep).nonzero() # The sigma index that is taken for the **very** first `step` # is always the second index (or the last index if there is only 1) # This way we can ensure we don't accidentally skip a sigma in # case we start in the middle of the denoising schedule (e.g. for image-to-image) if len(self._index_counter ) == 0: __a = 1 if len(_a ) > 1 else 0 else: __a = timestep.cpu().item() if torch.is_tensor(_a ) else timestep __a = self._index_counter[timestep_int] return indices[pos].item() @property def _UpperCAmelCase ( self : int ): if self.config.timestep_spacing in ["linspace", "trailing"]: return self.sigmas.max() return (self.sigmas.max() ** 2 + 1) ** 0.5 def _UpperCAmelCase ( self : Any , __SCREAMING_SNAKE_CASE : torch.FloatTensor , __SCREAMING_SNAKE_CASE : Union[float, torch.FloatTensor] , ): __a = self.index_for_timestep(_a ) if self.state_in_first_order: __a = self.sigmas[step_index] else: __a = self.sigmas_interpol[step_index] __a = sample / ((sigma**2 + 1) ** 0.5) return sample def _UpperCAmelCase ( self : str , __SCREAMING_SNAKE_CASE : int , __SCREAMING_SNAKE_CASE : Union[str, torch.device] = None , __SCREAMING_SNAKE_CASE : Optional[int] = None , ): __a = num_inference_steps __a = num_train_timesteps or self.config.num_train_timesteps # "linspace", "leading", "trailing" corresponds to annotation of Table 2. of https://arxiv.org/abs/2305.08891 if self.config.timestep_spacing == "linspace": __a = np.linspace(0 , num_train_timesteps - 1 , _a , dtype=_a )[::-1].copy() elif self.config.timestep_spacing == "leading": __a = num_train_timesteps // self.num_inference_steps # creates integer timesteps by multiplying by ratio # casting to int to avoid issues when num_inference_step is power of 3 __a = (np.arange(0 , _a ) * step_ratio).round()[::-1].copy().astype(_a ) timesteps += self.config.steps_offset elif self.config.timestep_spacing == "trailing": __a = num_train_timesteps / self.num_inference_steps # creates integer timesteps by multiplying by ratio # casting to int to avoid issues when num_inference_step is power of 3 __a = (np.arange(_a , 0 , -step_ratio )).round().copy().astype(_a ) timesteps -= 1 else: raise ValueError( f"""{self.config.timestep_spacing} is not supported. Please make sure to choose one of 'linspace', 'leading' or 'trailing'.""" ) __a = np.array(((1 - self.alphas_cumprod) / self.alphas_cumprod) ** 0.5 ) __a = torch.from_numpy(np.log(_a ) ).to(_a ) __a = np.interp(_a , np.arange(0 , len(_a ) ) , _a ) __a = np.concatenate([sigmas, [0.0]] ).astype(np.floataa ) __a = torch.from_numpy(_a ).to(device=_a ) # interpolate sigmas __a = sigmas.log().lerp(sigmas.roll(1 ).log() , 0.5 ).exp() __a = torch.cat([sigmas[:1], sigmas[1:].repeat_interleave(2 ), sigmas[-1:]] ) __a = torch.cat( [sigmas_interpol[:1], sigmas_interpol[1:].repeat_interleave(2 ), sigmas_interpol[-1:]] ) if str(_a ).startswith("mps" ): # mps does not support float64 __a = torch.from_numpy(_a ).to(_a , dtype=torch.floataa ) else: __a = torch.from_numpy(_a ).to(_a ) # interpolate timesteps __a = self.sigma_to_t(_a ).to(_a , dtype=timesteps.dtype ) __a = torch.stack((timesteps_interpol[1:-1, None], timesteps[1:, None]) , dim=-1 ).flatten() __a = torch.cat([timesteps[:1], interleaved_timesteps] ) __a = None # for exp beta schedules, such as the one for `pipeline_shap_e.py` # we need an index counter __a = defaultdict(_a ) def _UpperCAmelCase ( self : Optional[Any] , __SCREAMING_SNAKE_CASE : List[Any] ): __a = sigma.log() # get distribution __a = log_sigma - self.log_sigmas[:, None] # get sigmas range __a = dists.ge(0 ).cumsum(dim=0 ).argmax(dim=0 ).clamp(max=self.log_sigmas.shape[0] - 2 ) __a = low_idx + 1 __a = self.log_sigmas[low_idx] __a = self.log_sigmas[high_idx] # interpolate sigmas __a = (low - log_sigma) / (low - high) __a = w.clamp(0 , 1 ) # transform interpolation to time range __a = (1 - w) * low_idx + w * high_idx __a = t.view(sigma.shape ) return t @property def _UpperCAmelCase ( self : List[Any] ): return self.sample is None def _UpperCAmelCase ( self : str , __SCREAMING_SNAKE_CASE : Union[torch.FloatTensor, np.ndarray] , __SCREAMING_SNAKE_CASE : Union[float, torch.FloatTensor] , __SCREAMING_SNAKE_CASE : Union[torch.FloatTensor, np.ndarray] , __SCREAMING_SNAKE_CASE : bool = True , ): __a = self.index_for_timestep(_a ) # advance index counter by 1 __a = timestep.cpu().item() if torch.is_tensor(_a ) else timestep self._index_counter[timestep_int] += 1 if self.state_in_first_order: __a = self.sigmas[step_index] __a = self.sigmas_interpol[step_index + 1] __a = self.sigmas[step_index + 1] else: # 2nd order / KDPM2's method __a = self.sigmas[step_index - 1] __a = self.sigmas_interpol[step_index] __a = self.sigmas[step_index] # currently only gamma=0 is supported. This usually works best anyways. # We can support gamma in the future but then need to scale the timestep before # passing it to the model which requires a change in API __a = 0 __a = sigma * (gamma + 1) # Note: sigma_hat == sigma for now # 1. compute predicted original sample (x_0) from sigma-scaled predicted noise if self.config.prediction_type == "epsilon": __a = sigma_hat if self.state_in_first_order else sigma_interpol __a = sample - sigma_input * model_output elif self.config.prediction_type == "v_prediction": __a = sigma_hat if self.state_in_first_order else sigma_interpol __a = model_output * (-sigma_input / (sigma_input**2 + 1) ** 0.5) + ( sample / (sigma_input**2 + 1) ) elif self.config.prediction_type == "sample": raise NotImplementedError("prediction_type not implemented yet: sample" ) else: raise ValueError( f"""prediction_type given as {self.config.prediction_type} must be one of `epsilon`, or `v_prediction`""" ) if self.state_in_first_order: # 2. Convert to an ODE derivative for 1st order __a = (sample - pred_original_sample) / sigma_hat # 3. delta timestep __a = sigma_interpol - sigma_hat # store for 2nd order step __a = sample else: # DPM-Solver-2 # 2. Convert to an ODE derivative for 2nd order __a = (sample - pred_original_sample) / sigma_interpol # 3. delta timestep __a = sigma_next - sigma_hat __a = self.sample __a = None __a = sample + derivative * dt if not return_dict: return (prev_sample,) return SchedulerOutput(prev_sample=_a ) def _UpperCAmelCase ( self : Optional[Any] , __SCREAMING_SNAKE_CASE : torch.FloatTensor , __SCREAMING_SNAKE_CASE : torch.FloatTensor , __SCREAMING_SNAKE_CASE : torch.FloatTensor , ): __a = self.sigmas.to(device=original_samples.device , dtype=original_samples.dtype ) if original_samples.device.type == "mps" and torch.is_floating_point(_a ): # mps does not support float64 __a = self.timesteps.to(original_samples.device , dtype=torch.floataa ) __a = timesteps.to(original_samples.device , dtype=torch.floataa ) else: __a = self.timesteps.to(original_samples.device ) __a = timesteps.to(original_samples.device ) __a = [self.index_for_timestep(_a , _a ) for t in timesteps] __a = sigmas[step_indices].flatten() while len(sigma.shape ) < len(original_samples.shape ): __a = sigma.unsqueeze(-1 ) __a = original_samples + noise * sigma return noisy_samples def __len__( self : List[str] ): return self.config.num_train_timesteps
197
import timeit import numpy as np import datasets from datasets.arrow_writer import ArrowWriter from datasets.features.features import _ArrayXD def lowerCamelCase( a__): def wrapper(*a__ ,**a__): _SCREAMING_SNAKE_CASE =timeit.default_timer() _SCREAMING_SNAKE_CASE =func(*a__ ,**a__) _SCREAMING_SNAKE_CASE =timeit.default_timer() - starttime return delta _SCREAMING_SNAKE_CASE =func.__name__ return wrapper def lowerCamelCase( a__ ,a__=100 ,a__=None): _SCREAMING_SNAKE_CASE =[] _SCREAMING_SNAKE_CASE =seq_shapes or {} for i in range(a__): _SCREAMING_SNAKE_CASE ={} for col_id, (k, v) in enumerate(features.items()): if isinstance(a__ ,_ArrayXD): _SCREAMING_SNAKE_CASE =np.random.rand(*v.shape).astype(v.dtype) elif isinstance(a__ ,datasets.Value): if v.dtype == "string": _SCREAMING_SNAKE_CASE ='''The small grey turtle was surprisingly fast when challenged.''' else: _SCREAMING_SNAKE_CASE =np.random.randint(10 ,size=1).astype(v.dtype).item() elif isinstance(a__ ,datasets.Sequence): while isinstance(a__ ,datasets.Sequence): _SCREAMING_SNAKE_CASE =v.feature _SCREAMING_SNAKE_CASE =seq_shapes[k] _SCREAMING_SNAKE_CASE =np.random.rand(*a__).astype(v.dtype) _SCREAMING_SNAKE_CASE =data dummy_data.append((i, example)) return dummy_data def lowerCamelCase( a__ ,a__ ,a__=100 ,a__=None): _SCREAMING_SNAKE_CASE =generate_examples(a__ ,num_examples=a__ ,seq_shapes=a__) with ArrowWriter(features=a__ ,path=a__) as writer: for key, record in dummy_data: _SCREAMING_SNAKE_CASE =features.encode_example(a__) writer.write(a__) _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE =writer.finalize() if not num_final_examples == num_examples: raise ValueError( f"Error writing the dataset, wrote {num_final_examples} examples but should have written {num_examples}.") _SCREAMING_SNAKE_CASE =datasets.Dataset.from_file(filename=a__ ,info=datasets.DatasetInfo(features=a__)) return dataset
691
0
from __future__ import annotations import inspect import unittest import numpy as np from transformers import DeiTConfig from transformers.testing_utils import require_tf, require_vision, slow from transformers.utils import cached_property, is_tf_available, is_vision_available from ...test_configuration_common import ConfigTester from ...test_modeling_tf_common import TFModelTesterMixin, floats_tensor, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_tf_available(): import tensorflow as tf from transformers import ( TFDeiTForImageClassification, TFDeiTForImageClassificationWithTeacher, TFDeiTForMaskedImageModeling, TFDeiTModel, ) from transformers.models.deit.modeling_tf_deit import TF_DEIT_PRETRAINED_MODEL_ARCHIVE_LIST if is_vision_available(): from PIL import Image from transformers import DeiTImageProcessor class lowercase_ : '''simple docstring''' def __init__( self : Optional[Any] , __UpperCAmelCase : str , __UpperCAmelCase : Any=13 , __UpperCAmelCase : Optional[Any]=30 , __UpperCAmelCase : int=2 , __UpperCAmelCase : List[str]=3 , __UpperCAmelCase : Dict=True , __UpperCAmelCase : Optional[int]=True , __UpperCAmelCase : Union[str, Any]=32 , __UpperCAmelCase : List[str]=2 , __UpperCAmelCase : List[Any]=4 , __UpperCAmelCase : Optional[Any]=37 , __UpperCAmelCase : Tuple="gelu" , __UpperCAmelCase : Optional[int]=0.1 , __UpperCAmelCase : Optional[Any]=0.1 , __UpperCAmelCase : Optional[Any]=10 , __UpperCAmelCase : str=0.02 , __UpperCAmelCase : List[str]=3 , __UpperCAmelCase : Tuple=None , __UpperCAmelCase : str=2 , ) ->int: """simple docstring""" a = parent a = batch_size a = image_size a = patch_size a = num_channels a = is_training a = use_labels a = hidden_size a = num_hidden_layers a = num_attention_heads a = intermediate_size a = hidden_act a = hidden_dropout_prob a = attention_probs_dropout_prob a = type_sequence_label_size a = initializer_range a = scope a = encoder_stride # in DeiT, the seq length equals the number of patches + 2 (we add 2 for the [CLS] and distilation tokens) a = (image_size // patch_size) ** 2 a = num_patches + 2 def __lowerCAmelCase ( self : int ) ->Dict: """simple docstring""" a = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) a = None if self.use_labels: a = ids_tensor([self.batch_size] , self.type_sequence_label_size ) a = self.get_config() return config, pixel_values, labels def __lowerCAmelCase ( self : Optional[int] ) ->str: """simple docstring""" return DeiTConfig( image_size=self.image_size , patch_size=self.patch_size , num_channels=self.num_channels , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , is_decoder=_a , initializer_range=self.initializer_range , encoder_stride=self.encoder_stride , ) def __lowerCAmelCase ( self : int , __UpperCAmelCase : str , __UpperCAmelCase : Union[str, Any] , __UpperCAmelCase : Tuple ) ->Union[str, Any]: """simple docstring""" a = TFDeiTModel(config=_a ) a = model(_a ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def __lowerCAmelCase ( self : Optional[int] , __UpperCAmelCase : str , __UpperCAmelCase : List[str] , __UpperCAmelCase : Optional[int] ) ->List[Any]: """simple docstring""" a = TFDeiTForMaskedImageModeling(config=_a ) a = model(_a ) self.parent.assertEqual( result.reconstruction.shape , (self.batch_size, self.num_channels, self.image_size, self.image_size) ) # test greyscale images a = 1 a = TFDeiTForMaskedImageModeling(_a ) a = floats_tensor([self.batch_size, 1, self.image_size, self.image_size] ) a = model(_a ) self.parent.assertEqual(result.reconstruction.shape , (self.batch_size, 1, self.image_size, self.image_size) ) def __lowerCAmelCase ( self : Tuple , __UpperCAmelCase : List[str] , __UpperCAmelCase : Union[str, Any] , __UpperCAmelCase : str ) ->Any: """simple docstring""" a = self.type_sequence_label_size a = TFDeiTForImageClassification(_a ) a = model(_a , labels=_a ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.type_sequence_label_size) ) # test greyscale images a = 1 a = TFDeiTForImageClassification(_a ) a = floats_tensor([self.batch_size, 1, self.image_size, self.image_size] ) a = model(_a , labels=_a ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.type_sequence_label_size) ) def __lowerCAmelCase ( self : List[str] ) ->Union[str, Any]: """simple docstring""" a = self.prepare_config_and_inputs() a , a , a = config_and_inputs a = {'''pixel_values''': pixel_values} return config, inputs_dict @require_tf class lowercase_ ( UpperCamelCase__ , UpperCamelCase__ , unittest.TestCase ): '''simple docstring''' __snake_case = ( ( TFDeiTModel, TFDeiTForImageClassification, TFDeiTForImageClassificationWithTeacher, TFDeiTForMaskedImageModeling, ) if is_tf_available() else () ) __snake_case = ( { '''feature-extraction''': TFDeiTModel, '''image-classification''': (TFDeiTForImageClassification, TFDeiTForImageClassificationWithTeacher), } if is_tf_available() else {} ) __snake_case = False __snake_case = False __snake_case = False __snake_case = False def __lowerCAmelCase ( self : List[str] ) ->str: """simple docstring""" a = TFDeiTModelTester(self ) a = ConfigTester(self , config_class=_a , has_text_modality=_a , hidden_size=37 ) def __lowerCAmelCase ( self : List[str] ) ->List[Any]: """simple docstring""" self.config_tester.run_common_tests() @unittest.skip(reason='''DeiT does not use inputs_embeds''' ) def __lowerCAmelCase ( self : str ) ->List[Any]: """simple docstring""" pass def __lowerCAmelCase ( self : str ) ->Dict: """simple docstring""" a , a = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: a = model_class(_a ) self.assertIsInstance(model.get_input_embeddings() , (tf.keras.layers.Layer) ) a = model.get_output_embeddings() self.assertTrue(x is None or isinstance(_a , tf.keras.layers.Dense ) ) def __lowerCAmelCase ( self : Optional[int] ) ->Tuple: """simple docstring""" a , a = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: a = model_class(_a ) a = inspect.signature(model.call ) # signature.parameters is an OrderedDict => so arg_names order is deterministic a = [*signature.parameters.keys()] a = ['''pixel_values'''] self.assertListEqual(arg_names[:1] , _a ) def __lowerCAmelCase ( self : Optional[int] ) ->List[Any]: """simple docstring""" a = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*_a ) def __lowerCAmelCase ( self : Optional[int] ) ->Union[str, Any]: """simple docstring""" a = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_masked_image_modeling(*_a ) def __lowerCAmelCase ( self : List[str] ) ->List[str]: """simple docstring""" a = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_image_classification(*_a ) def __lowerCAmelCase ( self : Any , __UpperCAmelCase : Dict , __UpperCAmelCase : int , __UpperCAmelCase : Tuple=False ) ->Union[str, Any]: """simple docstring""" a = super()._prepare_for_class(_a , _a , return_labels=_a ) if return_labels: if "labels" in inputs_dict and "labels" not in inspect.signature(model_class.call ).parameters: del inputs_dict["labels"] return inputs_dict @slow def __lowerCAmelCase ( self : Optional[int] ) ->Optional[int]: """simple docstring""" for model_name in TF_DEIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: a = TFDeiTModel.from_pretrained(_a ) self.assertIsNotNone(_a ) def _a ( ) -> Union[str, Any]: a = Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png''' ) return image @require_tf @require_vision class lowercase_ ( unittest.TestCase ): '''simple docstring''' @cached_property def __lowerCAmelCase ( self : str ) ->Any: """simple docstring""" return ( DeiTImageProcessor.from_pretrained('''facebook/deit-base-distilled-patch16-224''' ) if is_vision_available() else None ) @slow def __lowerCAmelCase ( self : List[str] ) ->Optional[int]: """simple docstring""" a = TFDeiTForImageClassificationWithTeacher.from_pretrained('''facebook/deit-base-distilled-patch16-224''' ) a = self.default_image_processor a = prepare_img() a = image_processor(images=_a , return_tensors='''tf''' ) # forward pass a = model(**_a ) # verify the logits a = tf.TensorShape((1, 1_000) ) self.assertEqual(outputs.logits.shape , _a ) a = tf.constant([-1.0266, 0.1912, -1.2861] ) self.assertTrue(np.allclose(outputs.logits[0, :3] , _a , atol=1e-4 ) )
117
import logging import torch from torch import nn from torch.nn import CrossEntropyLoss, MSELoss from transformers.file_utils import add_start_docstrings, add_start_docstrings_to_model_forward from transformers.models.bert.modeling_bert import ( BERT_INPUTS_DOCSTRING, BERT_START_DOCSTRING, BertEncoder, BertModel, BertPreTrainedModel, ) snake_case_ : Optional[Any] = logging.getLogger(__name__) class A__ ( UpperCamelCase__ ): def __UpperCamelCase ( self : Optional[int] , _a : Union[str, Any] , _a : List[str] , _a : List[Any]=None , _a : Optional[Any]=None ) -> Any: """simple docstring""" _SCREAMING_SNAKE_CASE =self.layer[current_layer](_a , _a , head_mask[current_layer] ) _SCREAMING_SNAKE_CASE =layer_outputs[0] return hidden_states @add_start_docstrings( "The bare Bert Model transformer with PABEE outputting raw hidden-states without any specific head on top." , UpperCamelCase__ , ) class A__ ( UpperCamelCase__ ): def __init__( self : List[str] , _a : Union[str, Any] ) -> Tuple: """simple docstring""" super().__init__(_a ) _SCREAMING_SNAKE_CASE =BertEncoderWithPabee(_a ) self.init_weights() _SCREAMING_SNAKE_CASE =0 _SCREAMING_SNAKE_CASE =0 _SCREAMING_SNAKE_CASE =0 _SCREAMING_SNAKE_CASE =0 def __UpperCamelCase ( self : List[str] , _a : Optional[int] ) -> List[str]: """simple docstring""" _SCREAMING_SNAKE_CASE =threshold def __UpperCamelCase ( self : Dict , _a : int ) -> Tuple: """simple docstring""" _SCREAMING_SNAKE_CASE =patience def __UpperCamelCase ( self : Optional[Any] ) -> int: """simple docstring""" _SCREAMING_SNAKE_CASE =0 _SCREAMING_SNAKE_CASE =0 def __UpperCamelCase ( self : Union[str, Any] ) -> Optional[int]: """simple docstring""" _SCREAMING_SNAKE_CASE =self.inference_layers_num / self.inference_instances_num _SCREAMING_SNAKE_CASE =( f"*** Patience = {self.patience} Avg. Inference Layers = {avg_inf_layers:.2f} Speed Up =" f" {1 - avg_inf_layers / self.config.num_hidden_layers:.2f} ***" ) print(_a ) @add_start_docstrings_to_model_forward(_a ) def __UpperCamelCase ( self : List[Any] , _a : Optional[Any]=None , _a : Optional[int]=None , _a : Any=None , _a : Union[str, Any]=None , _a : Union[str, Any]=None , _a : Union[str, Any]=None , _a : str=None , _a : Any=None , _a : str=None , _a : Optional[Any]=None , _a : Dict=False , ) -> Union[str, Any]: """simple docstring""" if input_ids is not None and inputs_embeds is not None: raise ValueError('''You cannot specify both input_ids and inputs_embeds at the same time''' ) elif input_ids is not None: _SCREAMING_SNAKE_CASE =input_ids.size() elif inputs_embeds is not None: _SCREAMING_SNAKE_CASE =inputs_embeds.size()[:-1] else: raise ValueError('''You have to specify either input_ids or inputs_embeds''' ) _SCREAMING_SNAKE_CASE =input_ids.device if input_ids is not None else inputs_embeds.device if attention_mask is None: _SCREAMING_SNAKE_CASE =torch.ones(_a , device=_a ) if token_type_ids is None: _SCREAMING_SNAKE_CASE =torch.zeros(_a , dtype=torch.long , device=_a ) # We can provide a self-attention mask of dimensions [batch_size, from_seq_length, to_seq_length] # ourselves in which case we just need to make it broadcastable to all heads. _SCREAMING_SNAKE_CASE =self.get_extended_attention_mask(_a , _a , _a ) # If a 2D ou 3D attention mask is provided for the cross-attention # we need to make broadcastable to [batch_size, num_heads, seq_length, seq_length] if self.config.is_decoder and encoder_hidden_states is not None: _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE =encoder_hidden_states.size() _SCREAMING_SNAKE_CASE =(encoder_batch_size, encoder_sequence_length) if encoder_attention_mask is None: _SCREAMING_SNAKE_CASE =torch.ones(_a , device=_a ) _SCREAMING_SNAKE_CASE =self.invert_attention_mask(_a ) else: _SCREAMING_SNAKE_CASE =None # Prepare head mask if needed # 1.0 in head_mask indicate we keep the head # attention_probs has shape bsz x n_heads x N x N # input head_mask has shape [num_heads] or [num_hidden_layers x num_heads] # and head_mask is converted to shape [num_hidden_layers x batch x num_heads x seq_length x seq_length] _SCREAMING_SNAKE_CASE =self.get_head_mask(_a , self.config.num_hidden_layers ) _SCREAMING_SNAKE_CASE =self.embeddings( input_ids=_a , position_ids=_a , token_type_ids=_a , inputs_embeds=_a ) _SCREAMING_SNAKE_CASE =embedding_output if self.training: _SCREAMING_SNAKE_CASE =[] for i in range(self.config.num_hidden_layers ): _SCREAMING_SNAKE_CASE =self.encoder.adaptive_forward( _a , current_layer=_a , attention_mask=_a , head_mask=_a ) _SCREAMING_SNAKE_CASE =self.pooler(_a ) _SCREAMING_SNAKE_CASE =output_layers[i](output_dropout(_a ) ) res.append(_a ) elif self.patience == 0: # Use all layers for inference _SCREAMING_SNAKE_CASE =self.encoder( _a , attention_mask=_a , head_mask=_a , encoder_hidden_states=_a , encoder_attention_mask=_a , ) _SCREAMING_SNAKE_CASE =self.pooler(encoder_outputs[0] ) _SCREAMING_SNAKE_CASE =[output_layers[self.config.num_hidden_layers - 1](_a )] else: _SCREAMING_SNAKE_CASE =0 _SCREAMING_SNAKE_CASE =None _SCREAMING_SNAKE_CASE =0 for i in range(self.config.num_hidden_layers ): calculated_layer_num += 1 _SCREAMING_SNAKE_CASE =self.encoder.adaptive_forward( _a , current_layer=_a , attention_mask=_a , head_mask=_a ) _SCREAMING_SNAKE_CASE =self.pooler(_a ) _SCREAMING_SNAKE_CASE =output_layers[i](_a ) if regression: _SCREAMING_SNAKE_CASE =logits.detach() if patient_result is not None: _SCREAMING_SNAKE_CASE =patient_result.detach() if (patient_result is not None) and torch.abs(patient_result - labels ) < self.regression_threshold: patient_counter += 1 else: _SCREAMING_SNAKE_CASE =0 else: _SCREAMING_SNAKE_CASE =logits.detach().argmax(dim=1 ) if patient_result is not None: _SCREAMING_SNAKE_CASE =patient_result.detach().argmax(dim=1 ) if (patient_result is not None) and torch.all(labels.eq(_a ) ): patient_counter += 1 else: _SCREAMING_SNAKE_CASE =0 _SCREAMING_SNAKE_CASE =logits if patient_counter == self.patience: break _SCREAMING_SNAKE_CASE =[patient_result] self.inference_layers_num += calculated_layer_num self.inference_instances_num += 1 return res @add_start_docstrings( "Bert Model transformer with PABEE and a sequence classification/regression head on top (a linear layer on top of\n the pooled output) e.g. for GLUE tasks. " , UpperCamelCase__ , ) class A__ ( UpperCamelCase__ ): def __init__( self : Optional[int] , _a : List[Any] ) -> Union[str, Any]: """simple docstring""" super().__init__(_a ) _SCREAMING_SNAKE_CASE =config.num_labels _SCREAMING_SNAKE_CASE =BertModelWithPabee(_a ) _SCREAMING_SNAKE_CASE =nn.Dropout(config.hidden_dropout_prob ) _SCREAMING_SNAKE_CASE =nn.ModuleList( [nn.Linear(config.hidden_size , self.config.num_labels ) for _ in range(config.num_hidden_layers )] ) self.init_weights() @add_start_docstrings_to_model_forward(_a ) def __UpperCamelCase ( self : List[str] , _a : Optional[Any]=None , _a : List[Any]=None , _a : Union[str, Any]=None , _a : List[str]=None , _a : Dict=None , _a : Optional[Any]=None , _a : Optional[Any]=None , ) -> str: """simple docstring""" _SCREAMING_SNAKE_CASE =self.bert( input_ids=_a , attention_mask=_a , token_type_ids=_a , position_ids=_a , head_mask=_a , inputs_embeds=_a , output_dropout=self.dropout , output_layers=self.classifiers , regression=self.num_labels == 1 , ) _SCREAMING_SNAKE_CASE =(logits[-1],) if labels is not None: _SCREAMING_SNAKE_CASE =None _SCREAMING_SNAKE_CASE =0 for ix, logits_item in enumerate(_a ): if self.num_labels == 1: # We are doing regression _SCREAMING_SNAKE_CASE =MSELoss() _SCREAMING_SNAKE_CASE =loss_fct(logits_item.view(-1 ) , labels.view(-1 ) ) else: _SCREAMING_SNAKE_CASE =CrossEntropyLoss() _SCREAMING_SNAKE_CASE =loss_fct(logits_item.view(-1 , self.num_labels ) , labels.view(-1 ) ) if total_loss is None: _SCREAMING_SNAKE_CASE =loss else: total_loss += loss * (ix + 1) total_weights += ix + 1 _SCREAMING_SNAKE_CASE =(total_loss / total_weights,) + outputs return outputs
691
0
import unittest from transformers import ( MODEL_FOR_CAUSAL_LM_MAPPING, TF_MODEL_FOR_CAUSAL_LM_MAPPING, TextGenerationPipeline, logging, pipeline, ) from transformers.testing_utils import ( CaptureLogger, is_pipeline_test, require_accelerate, require_tf, require_torch, require_torch_gpu, require_torch_or_tf, ) from .test_pipelines_common import ANY @is_pipeline_test @require_torch_or_tf class UpperCamelCase( unittest.TestCase ): snake_case_ : List[Any] = MODEL_FOR_CAUSAL_LM_MAPPING snake_case_ : str = TF_MODEL_FOR_CAUSAL_LM_MAPPING @require_torch def SCREAMING_SNAKE_CASE_ ( self : Optional[Any] ) -> Any: '''simple docstring''' __snake_case = pipeline(task="text-generation" , model="sshleifer/tiny-ctrl" , framework="pt" ) # Using `do_sample=False` to force deterministic output __snake_case = text_generator("This is a test" , do_sample=_a ) self.assertEqual( _a , [ { "generated_text": ( "This is a test โ˜ƒ โ˜ƒ segmental segmental segmental ่ฎฎ่ฎฎeski eski flutter flutter Lacy oscope." " oscope. FiliFili@@" ) } ] , ) __snake_case = text_generator(["This is a test", "This is a second test"] ) self.assertEqual( _a , [ [ { "generated_text": ( "This is a test โ˜ƒ โ˜ƒ segmental segmental segmental ่ฎฎ่ฎฎeski eski flutter flutter Lacy oscope." " oscope. FiliFili@@" ) } ], [ { "generated_text": ( "This is a second test โ˜ƒ segmental segmental segmental ่ฎฎ่ฎฎeski eski flutter flutter Lacy" " oscope. oscope. FiliFili@@" ) } ], ] , ) __snake_case = text_generator("This is a test" , do_sample=_a , num_return_sequences=2 , return_tensors=_a ) self.assertEqual( _a , [ {"generated_token_ids": ANY(_a )}, {"generated_token_ids": ANY(_a )}, ] , ) __snake_case = text_generator.model.config.eos_token_id __snake_case = "<pad>" __snake_case = text_generator( ["This is a test", "This is a second test"] , do_sample=_a , num_return_sequences=2 , batch_size=2 , return_tensors=_a , ) self.assertEqual( _a , [ [ {"generated_token_ids": ANY(_a )}, {"generated_token_ids": ANY(_a )}, ], [ {"generated_token_ids": ANY(_a )}, {"generated_token_ids": ANY(_a )}, ], ] , ) @require_tf def SCREAMING_SNAKE_CASE_ ( self : int ) -> Union[str, Any]: '''simple docstring''' __snake_case = pipeline(task="text-generation" , model="sshleifer/tiny-ctrl" , framework="tf" ) # Using `do_sample=False` to force deterministic output __snake_case = text_generator("This is a test" , do_sample=_a ) self.assertEqual( _a , [ { "generated_text": ( "This is a test FeyFeyFey(Croatis.), s.), Cannes Cannes Cannes ้–ฒ้–ฒCannes Cannes Cannes ๆ”ต" " please," ) } ] , ) __snake_case = text_generator(["This is a test", "This is a second test"] , do_sample=_a ) self.assertEqual( _a , [ [ { "generated_text": ( "This is a test FeyFeyFey(Croatis.), s.), Cannes Cannes Cannes ้–ฒ้–ฒCannes Cannes Cannes ๆ”ต" " please," ) } ], [ { "generated_text": ( "This is a second test Chieftain Chieftain prefecture prefecture prefecture Cannes Cannes" " Cannes ้–ฒ้–ฒCannes Cannes Cannes ๆ”ต please," ) } ], ] , ) def SCREAMING_SNAKE_CASE_ ( self : Union[str, Any] , SCREAMING_SNAKE_CASE : Tuple , SCREAMING_SNAKE_CASE : Optional[Any] , SCREAMING_SNAKE_CASE : List[str] ) -> int: '''simple docstring''' __snake_case = TextGenerationPipeline(model=_a , tokenizer=_a ) return text_generator, ["This is a test", "Another test"] def SCREAMING_SNAKE_CASE_ ( self : str ) -> Union[str, Any]: '''simple docstring''' __snake_case = "Hello I believe in" __snake_case = pipeline("text-generation" , model="hf-internal-testing/tiny-random-gpt2" ) __snake_case = text_generator(_a ) self.assertEqual( _a , [{"generated_text": "Hello I believe in fe fe fe fe fe fe fe fe fe fe fe fe"}] , ) __snake_case = text_generator(_a , stop_sequence=" fe" ) self.assertEqual(_a , [{"generated_text": "Hello I believe in fe"}] ) def SCREAMING_SNAKE_CASE_ ( self : Dict , SCREAMING_SNAKE_CASE : str , SCREAMING_SNAKE_CASE : Any ) -> Optional[int]: '''simple docstring''' __snake_case = text_generator.model __snake_case = text_generator.tokenizer __snake_case = text_generator("This is a test" ) self.assertEqual(_a , [{"generated_text": ANY(_a )}] ) self.assertTrue(outputs[0]["generated_text"].startswith("This is a test" ) ) __snake_case = text_generator("This is a test" , return_full_text=_a ) self.assertEqual(_a , [{"generated_text": ANY(_a )}] ) self.assertNotIn("This is a test" , outputs[0]["generated_text"] ) __snake_case = pipeline(task="text-generation" , model=_a , tokenizer=_a , return_full_text=_a ) __snake_case = text_generator("This is a test" ) self.assertEqual(_a , [{"generated_text": ANY(_a )}] ) self.assertNotIn("This is a test" , outputs[0]["generated_text"] ) __snake_case = text_generator("This is a test" , return_full_text=_a ) self.assertEqual(_a , [{"generated_text": ANY(_a )}] ) self.assertTrue(outputs[0]["generated_text"].startswith("This is a test" ) ) __snake_case = text_generator(["This is great !", "Something else"] , num_return_sequences=2 , do_sample=_a ) self.assertEqual( _a , [ [{"generated_text": ANY(_a )}, {"generated_text": ANY(_a )}], [{"generated_text": ANY(_a )}, {"generated_text": ANY(_a )}], ] , ) if text_generator.tokenizer.pad_token is not None: __snake_case = text_generator( ["This is great !", "Something else"] , num_return_sequences=2 , batch_size=2 , do_sample=_a ) self.assertEqual( _a , [ [{"generated_text": ANY(_a )}, {"generated_text": ANY(_a )}], [{"generated_text": ANY(_a )}, {"generated_text": ANY(_a )}], ] , ) with self.assertRaises(_a ): __snake_case = text_generator("test" , return_full_text=_a , return_text=_a ) with self.assertRaises(_a ): __snake_case = text_generator("test" , return_full_text=_a , return_tensors=_a ) with self.assertRaises(_a ): __snake_case = text_generator("test" , return_text=_a , return_tensors=_a ) # Empty prompt is slighly special # it requires BOS token to exist. # Special case for Pegasus which will always append EOS so will # work even without BOS. if ( text_generator.tokenizer.bos_token_id is not None or "Pegasus" in tokenizer.__class__.__name__ or "Git" in model.__class__.__name__ ): __snake_case = text_generator("" ) self.assertEqual(_a , [{"generated_text": ANY(_a )}] ) else: with self.assertRaises((ValueError, AssertionError) ): __snake_case = text_generator("" ) if text_generator.framework == "tf": # TF generation does not support max_new_tokens, and it's impossible # to control long generation with only max_length without # fancy calculation, dismissing tests for now. return # We don't care about infinite range models. # They already work. # Skip this test for XGLM, since it uses sinusoidal positional embeddings which are resized on-the-fly. __snake_case = ["RwkvForCausalLM", "XGLMForCausalLM", "GPTNeoXForCausalLM"] if ( tokenizer.model_max_length < 1_0_0_0_0 and text_generator.model.__class__.__name__ not in EXTRA_MODELS_CAN_HANDLE_LONG_INPUTS ): # Handling of large generations with self.assertRaises((RuntimeError, IndexError, ValueError, AssertionError) ): text_generator("This is a test" * 5_0_0 , max_new_tokens=2_0 ) __snake_case = text_generator("This is a test" * 5_0_0 , handle_long_generation="hole" , max_new_tokens=2_0 ) # Hole strategy cannot work with self.assertRaises(_a ): text_generator( "This is a test" * 5_0_0 , handle_long_generation="hole" , max_new_tokens=tokenizer.model_max_length + 1_0 , ) @require_torch @require_accelerate @require_torch_gpu def SCREAMING_SNAKE_CASE_ ( self : Optional[Any] ) -> str: '''simple docstring''' import torch # Classic `model_kwargs` __snake_case = pipeline( model="hf-internal-testing/tiny-random-bloom" , model_kwargs={"device_map": "auto", "torch_dtype": torch.bfloataa} , ) self.assertEqual(pipe.model.device , torch.device(0 ) ) self.assertEqual(pipe.model.lm_head.weight.dtype , torch.bfloataa ) __snake_case = pipe("This is a test" ) self.assertEqual( _a , [ { "generated_text": ( "This is a test test test test test test test test test test test test test test test test" " test" ) } ] , ) # Upgraded those two to real pipeline arguments (they just get sent for the model as they're unlikely to mean anything else.) __snake_case = pipeline(model="hf-internal-testing/tiny-random-bloom" , device_map="auto" , torch_dtype=torch.bfloataa ) self.assertEqual(pipe.model.device , torch.device(0 ) ) self.assertEqual(pipe.model.lm_head.weight.dtype , torch.bfloataa ) __snake_case = pipe("This is a test" ) self.assertEqual( _a , [ { "generated_text": ( "This is a test test test test test test test test test test test test test test test test" " test" ) } ] , ) # torch_dtype will be automatically set to float32 if not provided - check: https://github.com/huggingface/transformers/pull/20602 __snake_case = pipeline(model="hf-internal-testing/tiny-random-bloom" , device_map="auto" ) self.assertEqual(pipe.model.device , torch.device(0 ) ) self.assertEqual(pipe.model.lm_head.weight.dtype , torch.floataa ) __snake_case = pipe("This is a test" ) self.assertEqual( _a , [ { "generated_text": ( "This is a test test test test test test test test test test test test test test test test" " test" ) } ] , ) @require_torch @require_torch_gpu def SCREAMING_SNAKE_CASE_ ( self : Dict ) -> Any: '''simple docstring''' import torch __snake_case = pipeline(model="hf-internal-testing/tiny-random-bloom" , device=0 , torch_dtype=torch.floataa ) pipe("This is a test" ) @require_torch @require_accelerate @require_torch_gpu def SCREAMING_SNAKE_CASE_ ( self : Union[str, Any] ) -> Tuple: '''simple docstring''' import torch __snake_case = pipeline(model="hf-internal-testing/tiny-random-bloom" , device_map="auto" , torch_dtype=torch.floataa ) pipe("This is a test" , do_sample=_a , top_p=0.5 ) def SCREAMING_SNAKE_CASE_ ( self : int ) -> Any: '''simple docstring''' __snake_case = "Hello world" __snake_case = pipeline("text-generation" , model="hf-internal-testing/tiny-random-gpt2" ) if text_generator.model.framework == "tf": __snake_case = logging.get_logger("transformers.generation.tf_utils" ) else: __snake_case = logging.get_logger("transformers.generation.utils" ) __snake_case = "Both `max_new_tokens`" # The beggining of the message to be checked in this test # Both are set by the user -> log warning with CaptureLogger(_a ) as cl: __snake_case = text_generator(_a , max_length=1_0 , max_new_tokens=1 ) self.assertIn(_a , cl.out ) # The user only sets one -> no warning with CaptureLogger(_a ) as cl: __snake_case = text_generator(_a , max_new_tokens=1 ) self.assertNotIn(_a , cl.out ) with CaptureLogger(_a ) as cl: __snake_case = text_generator(_a , max_length=1_0 ) self.assertNotIn(_a , cl.out )
371
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available snake_case_ : str = { '''configuration_table_transformer''': [ '''TABLE_TRANSFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''TableTransformerConfig''', '''TableTransformerOnnxConfig''', ] } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: snake_case_ : str = [ '''TABLE_TRANSFORMER_PRETRAINED_MODEL_ARCHIVE_LIST''', '''TableTransformerForObjectDetection''', '''TableTransformerModel''', '''TableTransformerPreTrainedModel''', ] if TYPE_CHECKING: from .configuration_table_transformer import ( TABLE_TRANSFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP, TableTransformerConfig, TableTransformerOnnxConfig, ) try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_table_transformer import ( TABLE_TRANSFORMER_PRETRAINED_MODEL_ARCHIVE_LIST, TableTransformerForObjectDetection, TableTransformerModel, TableTransformerPreTrainedModel, ) else: import sys snake_case_ : Any = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
691
0
'''simple docstring''' import math import unittest def _lowerCAmelCase ( __magic_name__ : Any ) -> Optional[Any]: assert isinstance(a__ , a__ ) and ( number >= 0 ), "'number' must been an int and positive" 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(a__ ) + 1 ) , 6 ): if number % i == 0 or number % (i + 2) == 0: return False return True class __SCREAMING_SNAKE_CASE ( unittest.TestCase ): def lowerCamelCase_ ( self : Optional[int] ): '''simple docstring''' self.assertTrue(is_prime(2 ) ) self.assertTrue(is_prime(3 ) ) self.assertTrue(is_prime(5 ) ) self.assertTrue(is_prime(7 ) ) self.assertTrue(is_prime(11 ) ) self.assertTrue(is_prime(13 ) ) self.assertTrue(is_prime(17 ) ) self.assertTrue(is_prime(19 ) ) self.assertTrue(is_prime(23 ) ) self.assertTrue(is_prime(29 ) ) def lowerCamelCase_ ( self : str ): '''simple docstring''' with self.assertRaises(_a ): is_prime(-19 ) self.assertFalse( is_prime(0 ) , '''Zero doesn\'t have any positive factors, primes must have exactly two.''' , ) self.assertFalse( is_prime(1 ) , '''One only has 1 positive factor, primes must have exactly two.''' , ) self.assertFalse(is_prime(2 * 2 ) ) self.assertFalse(is_prime(2 * 3 ) ) self.assertFalse(is_prime(3 * 3 ) ) self.assertFalse(is_prime(3 * 5 ) ) self.assertFalse(is_prime(3 * 5 * 7 ) ) if __name__ == "__main__": unittest.main()
92
import json import os import shutil import tempfile import unittest import numpy as np import pytest from transformers import MgpstrTokenizer from transformers.models.mgp_str.tokenization_mgp_str import VOCAB_FILES_NAMES from transformers.testing_utils import require_torch, require_vision from transformers.utils import IMAGE_PROCESSOR_NAME, is_torch_available, is_vision_available if is_torch_available(): import torch if is_vision_available(): from PIL import Image from transformers import MgpstrProcessor, ViTImageProcessor @require_torch @require_vision class A__ ( unittest.TestCase ): UpperCAmelCase = ViTImageProcessor if is_vision_available() else None @property def __UpperCamelCase ( self : str ) -> Union[str, Any]: """simple docstring""" return self.image_processor_tester.prepare_image_processor_dict() def __UpperCamelCase ( self : str ) -> List[str]: """simple docstring""" _SCREAMING_SNAKE_CASE =(3, 32, 128) _SCREAMING_SNAKE_CASE =tempfile.mkdtemp() # fmt: off _SCREAMING_SNAKE_CASE =['''[GO]''', '''[s]''', '''0''', '''1''', '''2''', '''3''', '''4''', '''5''', '''6''', '''7''', '''8''', '''9''', '''a''', '''b''', '''c''', '''d''', '''e''', '''f''', '''g''', '''h''', '''i''', '''j''', '''k''', '''l''', '''m''', '''n''', '''o''', '''p''', '''q''', '''r''', '''s''', '''t''', '''u''', '''v''', '''w''', '''x''', '''y''', '''z'''] # fmt: on _SCREAMING_SNAKE_CASE =dict(zip(_a , range(len(_a ) ) ) ) _SCREAMING_SNAKE_CASE =os.path.join(self.tmpdirname , VOCAB_FILES_NAMES['''vocab_file'''] ) with open(self.vocab_file , '''w''' , encoding='''utf-8''' ) as fp: fp.write(json.dumps(_a ) + '''\n''' ) _SCREAMING_SNAKE_CASE ={ '''do_normalize''': False, '''do_resize''': True, '''image_processor_type''': '''ViTImageProcessor''', '''resample''': 3, '''size''': {'''height''': 32, '''width''': 128}, } _SCREAMING_SNAKE_CASE =os.path.join(self.tmpdirname , _a ) with open(self.image_processor_file , '''w''' , encoding='''utf-8''' ) as fp: json.dump(_a , _a ) def __UpperCamelCase ( self : Optional[Any] , **_a : str ) -> int: """simple docstring""" return MgpstrTokenizer.from_pretrained(self.tmpdirname , **_a ) def __UpperCamelCase ( self : Optional[int] , **_a : Tuple ) -> List[Any]: """simple docstring""" return ViTImageProcessor.from_pretrained(self.tmpdirname , **_a ) def __UpperCamelCase ( self : Tuple ) -> str: """simple docstring""" shutil.rmtree(self.tmpdirname ) def __UpperCamelCase ( self : List[Any] ) -> Any: """simple docstring""" _SCREAMING_SNAKE_CASE =np.random.randint(255 , size=(3, 30, 400) , dtype=np.uinta ) _SCREAMING_SNAKE_CASE =Image.fromarray(np.moveaxis(_a , 0 , -1 ) ) return image_input def __UpperCamelCase ( self : Union[str, Any] ) -> Dict: """simple docstring""" _SCREAMING_SNAKE_CASE =self.get_tokenizer() _SCREAMING_SNAKE_CASE =self.get_image_processor() _SCREAMING_SNAKE_CASE =MgpstrProcessor(tokenizer=_a , image_processor=_a ) processor.save_pretrained(self.tmpdirname ) _SCREAMING_SNAKE_CASE =MgpstrProcessor.from_pretrained(self.tmpdirname , use_fast=_a ) self.assertEqual(processor.char_tokenizer.get_vocab() , tokenizer.get_vocab() ) self.assertIsInstance(processor.char_tokenizer , _a ) self.assertEqual(processor.image_processor.to_json_string() , image_processor.to_json_string() ) self.assertIsInstance(processor.image_processor , _a ) def __UpperCamelCase ( self : Union[str, Any] ) -> List[Any]: """simple docstring""" _SCREAMING_SNAKE_CASE =self.get_tokenizer() _SCREAMING_SNAKE_CASE =self.get_image_processor() _SCREAMING_SNAKE_CASE =MgpstrProcessor(tokenizer=_a , image_processor=_a ) processor.save_pretrained(self.tmpdirname ) _SCREAMING_SNAKE_CASE =self.get_tokenizer(bos_token='''(BOS)''' , eos_token='''(EOS)''' ) _SCREAMING_SNAKE_CASE =self.get_image_processor(do_normalize=_a , padding_value=1.0 ) _SCREAMING_SNAKE_CASE =MgpstrProcessor.from_pretrained( self.tmpdirname , bos_token='''(BOS)''' , eos_token='''(EOS)''' , do_normalize=_a , padding_value=1.0 ) self.assertEqual(processor.char_tokenizer.get_vocab() , tokenizer_add_kwargs.get_vocab() ) self.assertIsInstance(processor.char_tokenizer , _a ) self.assertEqual(processor.image_processor.to_json_string() , image_processor_add_kwargs.to_json_string() ) self.assertIsInstance(processor.image_processor , _a ) def __UpperCamelCase ( self : Union[str, Any] ) -> str: """simple docstring""" _SCREAMING_SNAKE_CASE =self.get_image_processor() _SCREAMING_SNAKE_CASE =self.get_tokenizer() _SCREAMING_SNAKE_CASE =MgpstrProcessor(tokenizer=_a , image_processor=_a ) _SCREAMING_SNAKE_CASE =self.prepare_image_inputs() _SCREAMING_SNAKE_CASE =image_processor(_a , return_tensors='''np''' ) _SCREAMING_SNAKE_CASE =processor(images=_a , return_tensors='''np''' ) for key in input_image_proc.keys(): self.assertAlmostEqual(input_image_proc[key].sum() , input_processor[key].sum() , delta=1E-2 ) def __UpperCamelCase ( self : Optional[int] ) -> List[str]: """simple docstring""" _SCREAMING_SNAKE_CASE =self.get_image_processor() _SCREAMING_SNAKE_CASE =self.get_tokenizer() _SCREAMING_SNAKE_CASE =MgpstrProcessor(tokenizer=_a , image_processor=_a ) _SCREAMING_SNAKE_CASE ='''test''' _SCREAMING_SNAKE_CASE =processor(text=_a ) _SCREAMING_SNAKE_CASE =tokenizer(_a ) for key in encoded_tok.keys(): self.assertListEqual(encoded_tok[key] , encoded_processor[key] ) def __UpperCamelCase ( self : List[str] ) -> List[Any]: """simple docstring""" _SCREAMING_SNAKE_CASE =self.get_image_processor() _SCREAMING_SNAKE_CASE =self.get_tokenizer() _SCREAMING_SNAKE_CASE =MgpstrProcessor(tokenizer=_a , image_processor=_a ) _SCREAMING_SNAKE_CASE ='''test''' _SCREAMING_SNAKE_CASE =self.prepare_image_inputs() _SCREAMING_SNAKE_CASE =processor(text=_a , images=_a ) self.assertListEqual(list(inputs.keys() ) , ['''pixel_values''', '''labels'''] ) # test if it raises when no input is passed with pytest.raises(_a ): processor() def __UpperCamelCase ( self : List[Any] ) -> Any: """simple docstring""" _SCREAMING_SNAKE_CASE =self.get_image_processor() _SCREAMING_SNAKE_CASE =self.get_tokenizer() _SCREAMING_SNAKE_CASE =MgpstrProcessor(tokenizer=_a , image_processor=_a ) _SCREAMING_SNAKE_CASE =[[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9], [3, 4, 3, 1, 1, 8, 9]] _SCREAMING_SNAKE_CASE =processor.char_decode(_a ) _SCREAMING_SNAKE_CASE =tokenizer.batch_decode(_a ) _SCREAMING_SNAKE_CASE =[seq.replace(''' ''' , '''''' ) for seq in decoded_tok] self.assertListEqual(_a , _a ) def __UpperCamelCase ( self : Any ) -> List[Any]: """simple docstring""" _SCREAMING_SNAKE_CASE =self.get_image_processor() _SCREAMING_SNAKE_CASE =self.get_tokenizer() _SCREAMING_SNAKE_CASE =MgpstrProcessor(tokenizer=_a , image_processor=_a ) _SCREAMING_SNAKE_CASE =None _SCREAMING_SNAKE_CASE =self.prepare_image_inputs() _SCREAMING_SNAKE_CASE =processor(text=_a , images=_a ) self.assertListEqual(list(inputs.keys() ) , processor.model_input_names ) def __UpperCamelCase ( self : List[Any] ) -> Optional[int]: """simple docstring""" _SCREAMING_SNAKE_CASE =self.get_image_processor() _SCREAMING_SNAKE_CASE =self.get_tokenizer() _SCREAMING_SNAKE_CASE =MgpstrProcessor(tokenizer=_a , image_processor=_a ) _SCREAMING_SNAKE_CASE =torch.randn(1 , 27 , 38 ) _SCREAMING_SNAKE_CASE =torch.randn(1 , 27 , 5_0257 ) _SCREAMING_SNAKE_CASE =torch.randn(1 , 27 , 3_0522 ) _SCREAMING_SNAKE_CASE =processor.batch_decode([char_input, bpe_input, wp_input] ) self.assertListEqual(list(results.keys() ) , ['''generated_text''', '''scores''', '''char_preds''', '''bpe_preds''', '''wp_preds'''] )
691
0
'''simple docstring''' import unittest from transformers import is_vision_available from transformers.pipelines import pipeline from transformers.testing_utils import ( is_pipeline_test, nested_simplify, require_tf, require_torch, require_vision, slow, ) from .test_pipelines_common import ANY if is_vision_available(): from PIL import Image else: class lowerCamelCase_ : @staticmethod def __lowercase ( *lowerCAmelCase__ : Union[str, Any] , **lowerCAmelCase__ : str ): """simple docstring""" pass @is_pipeline_test @require_vision class lowerCamelCase_ ( unittest.TestCase ): @require_torch def __lowercase ( self : List[Any] ): """simple docstring""" SCREAMING_SNAKE_CASE : Optional[int] = pipeline( model='''hf-internal-testing/tiny-random-clip-zero-shot-image-classification''' , ) SCREAMING_SNAKE_CASE : List[str] = Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png''' ) SCREAMING_SNAKE_CASE : int = image_classifier(_a , candidate_labels=['''a''', '''b''', '''c'''] ) # The floating scores are so close, we enter floating error approximation and the order is not guaranteed across # python and torch versions. self.assertIn( nested_simplify(_a ) , [ [{'''score''': 0.333, '''label''': '''a'''}, {'''score''': 0.333, '''label''': '''b'''}, {'''score''': 0.333, '''label''': '''c'''}], [{'''score''': 0.333, '''label''': '''a'''}, {'''score''': 0.333, '''label''': '''c'''}, {'''score''': 0.333, '''label''': '''b'''}], ] , ) SCREAMING_SNAKE_CASE : Tuple = image_classifier([image] * 5 , candidate_labels=['''A''', '''B''', '''C'''] , batch_size=2 ) self.assertEqual( nested_simplify(_a ) , [ [ {'''score''': 0.333, '''label''': ANY(_a )}, {'''score''': 0.333, '''label''': ANY(_a )}, {'''score''': 0.333, '''label''': ANY(_a )}, ], [ {'''score''': 0.333, '''label''': ANY(_a )}, {'''score''': 0.333, '''label''': ANY(_a )}, {'''score''': 0.333, '''label''': ANY(_a )}, ], [ {'''score''': 0.333, '''label''': ANY(_a )}, {'''score''': 0.333, '''label''': ANY(_a )}, {'''score''': 0.333, '''label''': ANY(_a )}, ], [ {'''score''': 0.333, '''label''': ANY(_a )}, {'''score''': 0.333, '''label''': ANY(_a )}, {'''score''': 0.333, '''label''': ANY(_a )}, ], [ {'''score''': 0.333, '''label''': ANY(_a )}, {'''score''': 0.333, '''label''': ANY(_a )}, {'''score''': 0.333, '''label''': ANY(_a )}, ], ] , ) @require_tf def __lowercase ( self : str ): """simple docstring""" SCREAMING_SNAKE_CASE : int = pipeline( model='''hf-internal-testing/tiny-random-clip-zero-shot-image-classification''' , framework='''tf''' ) SCREAMING_SNAKE_CASE : str = Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png''' ) SCREAMING_SNAKE_CASE : Tuple = image_classifier(_a , candidate_labels=['''a''', '''b''', '''c'''] ) self.assertEqual( nested_simplify(_a ) , [{'''score''': 0.333, '''label''': '''a'''}, {'''score''': 0.333, '''label''': '''b'''}, {'''score''': 0.333, '''label''': '''c'''}] , ) SCREAMING_SNAKE_CASE : Optional[int] = image_classifier([image] * 5 , candidate_labels=['''A''', '''B''', '''C'''] , batch_size=2 ) self.assertEqual( nested_simplify(_a ) , [ [ {'''score''': 0.333, '''label''': ANY(_a )}, {'''score''': 0.333, '''label''': ANY(_a )}, {'''score''': 0.333, '''label''': ANY(_a )}, ], [ {'''score''': 0.333, '''label''': ANY(_a )}, {'''score''': 0.333, '''label''': ANY(_a )}, {'''score''': 0.333, '''label''': ANY(_a )}, ], [ {'''score''': 0.333, '''label''': ANY(_a )}, {'''score''': 0.333, '''label''': ANY(_a )}, {'''score''': 0.333, '''label''': ANY(_a )}, ], [ {'''score''': 0.333, '''label''': ANY(_a )}, {'''score''': 0.333, '''label''': ANY(_a )}, {'''score''': 0.333, '''label''': ANY(_a )}, ], [ {'''score''': 0.333, '''label''': ANY(_a )}, {'''score''': 0.333, '''label''': ANY(_a )}, {'''score''': 0.333, '''label''': ANY(_a )}, ], ] , ) @slow @require_torch def __lowercase ( self : str ): """simple docstring""" SCREAMING_SNAKE_CASE : Dict = pipeline( task='''zero-shot-image-classification''' , model='''openai/clip-vit-base-patch32''' , ) # This is an image of 2 cats with remotes and no planes SCREAMING_SNAKE_CASE : List[Any] = Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png''' ) SCREAMING_SNAKE_CASE : Tuple = image_classifier(_a , candidate_labels=['''cat''', '''plane''', '''remote'''] ) self.assertEqual( nested_simplify(_a ) , [ {'''score''': 0.511, '''label''': '''remote'''}, {'''score''': 0.485, '''label''': '''cat'''}, {'''score''': 0.004, '''label''': '''plane'''}, ] , ) SCREAMING_SNAKE_CASE : int = image_classifier([image] * 5 , candidate_labels=['''cat''', '''plane''', '''remote'''] , batch_size=2 ) self.assertEqual( nested_simplify(_a ) , [ [ {'''score''': 0.511, '''label''': '''remote'''}, {'''score''': 0.485, '''label''': '''cat'''}, {'''score''': 0.004, '''label''': '''plane'''}, ], ] * 5 , ) @slow @require_tf def __lowercase ( self : str ): """simple docstring""" SCREAMING_SNAKE_CASE : Any = pipeline( task='''zero-shot-image-classification''' , model='''openai/clip-vit-base-patch32''' , framework='''tf''' ) # This is an image of 2 cats with remotes and no planes SCREAMING_SNAKE_CASE : List[str] = Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png''' ) SCREAMING_SNAKE_CASE : Union[str, Any] = image_classifier(_a , candidate_labels=['''cat''', '''plane''', '''remote'''] ) self.assertEqual( nested_simplify(_a ) , [ {'''score''': 0.511, '''label''': '''remote'''}, {'''score''': 0.485, '''label''': '''cat'''}, {'''score''': 0.004, '''label''': '''plane'''}, ] , ) SCREAMING_SNAKE_CASE : Any = image_classifier([image] * 5 , candidate_labels=['''cat''', '''plane''', '''remote'''] , batch_size=2 ) self.assertEqual( nested_simplify(_a ) , [ [ {'''score''': 0.511, '''label''': '''remote'''}, {'''score''': 0.485, '''label''': '''cat'''}, {'''score''': 0.004, '''label''': '''plane'''}, ], ] * 5 , )
527
import requests from bsa import BeautifulSoup def lowerCamelCase( a__ = "https://www.worldometers.info/coronavirus"): _SCREAMING_SNAKE_CASE =BeautifulSoup(requests.get(a__).text ,'''html.parser''') _SCREAMING_SNAKE_CASE =soup.findAll('''h1''') _SCREAMING_SNAKE_CASE =soup.findAll('''div''' ,{'''class''': '''maincounter-number'''}) keys += soup.findAll('''span''' ,{'''class''': '''panel-title'''}) values += soup.findAll('''div''' ,{'''class''': '''number-table-main'''}) return {key.text.strip(): value.text.strip() for key, value in zip(a__ ,a__)} if __name__ == "__main__": print('''\033[1m''' + '''COVID-19 Status of the World''' + '''\033[0m\n''') for key, value in world_covidaa_stats().items(): print(f"""{key}\n{value}\n""")
691
0
import sys def lowerCAmelCase__( lowercase : Optional[Any] ) -> int: __snake_case : Tuple = len(a__ ) __snake_case : str = [[0 for x in range(a__ )] for x in range(a__ )] __snake_case : Tuple = [[0 for x in range(a__ )] for x in range(a__ )] for chain_length in range(2 , a__ ): for a in range(1 , n - chain_length + 1 ): __snake_case : List[str] = a + chain_length - 1 __snake_case : Tuple = sys.maxsize for c in range(a__ , a__ ): __snake_case : Tuple = ( matrix[a][c] + matrix[c + 1][b] + array[a - 1] * array[c] * array[b] ) if cost < matrix[a][b]: __snake_case : int = cost __snake_case : str = c return matrix, sol def lowerCAmelCase__( lowercase : Dict , lowercase : List[Any] , lowercase : Union[str, Any] ) -> Dict: if i == j: print("A" + str(a__ ) , end=" " ) else: print("(" , end=" " ) print_optiomal_solution(a__ , a__ , optimal_solution[i][j] ) print_optiomal_solution(a__ , optimal_solution[i][j] + 1 , a__ ) print(")" , end=" " ) def lowerCAmelCase__( ) -> Dict: __snake_case : Tuple = [30, 35, 15, 5, 10, 20, 25] __snake_case : Optional[int] = len(a__ ) # Size of matrix created from above array will be # 30*35 35*15 15*5 5*10 10*20 20*25 __snake_case , __snake_case : str = matrix_chain_order(a__ ) print("No. of Operation required: " + str(matrix[1][n - 1] ) ) print_optiomal_solution(a__ , 1 , n - 1 ) if __name__ == "__main__": main()
243
def lowerCamelCase( a__ ,a__): return number | (1 << position) def lowerCamelCase( a__ ,a__): return number & ~(1 << position) def lowerCamelCase( a__ ,a__): return number ^ (1 << position) def lowerCamelCase( a__ ,a__): return ((number >> position) & 1) == 1 def lowerCamelCase( a__ ,a__): return int((number & (1 << position)) != 0) if __name__ == "__main__": import doctest doctest.testmod()
691
0
import math def _lowerCamelCase( __snake_case , __snake_case ) -> Tuple: 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 > 360: 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(a__ ) ) ** 2) if __name__ == "__main__": import doctest doctest.testmod(name='malus_law')
524
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 A__ ( UpperCamelCase__ ): def __UpperCamelCase ( self : Tuple ) -> Dict: """simple docstring""" _SCREAMING_SNAKE_CASE =tempfile.mkdtemp() _SCREAMING_SNAKE_CASE =8 # DPR tok _SCREAMING_SNAKE_CASE =[ '''[UNK]''', '''[CLS]''', '''[SEP]''', '''[PAD]''', '''[MASK]''', '''want''', '''##want''', '''##ed''', '''wa''', '''un''', '''runn''', '''##ing''', ''',''', '''low''', '''lowest''', ] _SCREAMING_SNAKE_CASE =os.path.join(self.tmpdirname , '''dpr_tokenizer''' ) os.makedirs(_a , exist_ok=_a ) _SCREAMING_SNAKE_CASE =os.path.join(_a , 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 _SCREAMING_SNAKE_CASE =[ '''l''', '''o''', '''w''', '''e''', '''r''', '''s''', '''t''', '''i''', '''d''', '''n''', '''\u0120''', '''\u0120l''', '''\u0120n''', '''\u0120lo''', '''\u0120low''', '''er''', '''\u0120lowest''', '''\u0120newer''', '''\u0120wider''', '''<unk>''', ] _SCREAMING_SNAKE_CASE =dict(zip(_a , range(len(_a ) ) ) ) _SCREAMING_SNAKE_CASE =['''#version: 0.2''', '''\u0120 l''', '''\u0120l o''', '''\u0120lo w''', '''e r''', ''''''] _SCREAMING_SNAKE_CASE ={'''unk_token''': '''<unk>'''} _SCREAMING_SNAKE_CASE =os.path.join(self.tmpdirname , '''bart_tokenizer''' ) os.makedirs(_a , exist_ok=_a ) _SCREAMING_SNAKE_CASE =os.path.join(_a , BART_VOCAB_FILES_NAMES['''vocab_file'''] ) _SCREAMING_SNAKE_CASE =os.path.join(_a , BART_VOCAB_FILES_NAMES['''merges_file'''] ) with open(self.vocab_file , '''w''' , encoding='''utf-8''' ) as fp: fp.write(json.dumps(_a ) + '''\n''' ) with open(self.merges_file , '''w''' , encoding='''utf-8''' ) as fp: fp.write('''\n'''.join(_a ) ) def __UpperCamelCase ( self : List[str] ) -> DPRQuestionEncoderTokenizer: """simple docstring""" return DPRQuestionEncoderTokenizer.from_pretrained(os.path.join(self.tmpdirname , '''dpr_tokenizer''' ) ) def __UpperCamelCase ( self : Dict ) -> DPRContextEncoderTokenizer: """simple docstring""" return DPRContextEncoderTokenizer.from_pretrained(os.path.join(self.tmpdirname , '''dpr_tokenizer''' ) ) def __UpperCamelCase ( self : Union[str, Any] ) -> BartTokenizer: """simple docstring""" return BartTokenizer.from_pretrained(os.path.join(self.tmpdirname , '''bart_tokenizer''' ) ) def __UpperCamelCase ( self : Union[str, Any] ) -> int: """simple docstring""" shutil.rmtree(self.tmpdirname ) def __UpperCamelCase ( self : Union[str, Any] ) -> int: """simple docstring""" _SCREAMING_SNAKE_CASE =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 __UpperCamelCase ( self : str ) -> List[str]: """simple docstring""" _SCREAMING_SNAKE_CASE =self.get_dummy_dataset() _SCREAMING_SNAKE_CASE =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: _SCREAMING_SNAKE_CASE =dataset _SCREAMING_SNAKE_CASE =RagRetriever( _a , question_encoder_tokenizer=self.get_dpr_tokenizer() , generator_tokenizer=self.get_bart_tokenizer() , ) return retriever def __UpperCamelCase ( self : Optional[int] , _a : bool ) -> Any: """simple docstring""" _SCREAMING_SNAKE_CASE =self.get_dummy_dataset() _SCREAMING_SNAKE_CASE =RagConfig( retrieval_vector_size=self.retrieval_vector_size , question_encoder=DPRConfig().to_dict() , generator=BartConfig().to_dict() , index_name='''custom''' , ) if from_disk: _SCREAMING_SNAKE_CASE =os.path.join(self.tmpdirname , '''dataset''' ) _SCREAMING_SNAKE_CASE =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 _SCREAMING_SNAKE_CASE =RagRetriever( _a , question_encoder_tokenizer=self.get_dpr_tokenizer() , generator_tokenizer=self.get_bart_tokenizer() , ) else: _SCREAMING_SNAKE_CASE =RagRetriever( _a , question_encoder_tokenizer=self.get_dpr_tokenizer() , generator_tokenizer=self.get_bart_tokenizer() , index=CustomHFIndex(config.retrieval_vector_size , _a ) , ) return retriever def __UpperCamelCase ( self : Optional[Any] ) -> Dict: """simple docstring""" _SCREAMING_SNAKE_CASE =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 ) _SCREAMING_SNAKE_CASE =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''' ) ) _SCREAMING_SNAKE_CASE =os.path.join(self.tmpdirname , '''psgs_w100.tsv.pkl''' ) _SCREAMING_SNAKE_CASE ={sample['''id''']: [sample['''text'''], sample['''title''']] for sample in dataset} pickle.dump(_a , open(_a , '''wb''' ) ) _SCREAMING_SNAKE_CASE =RagConfig( retrieval_vector_size=self.retrieval_vector_size , question_encoder=DPRConfig().to_dict() , generator=BartConfig().to_dict() , index_name='''legacy''' , index_path=self.tmpdirname , ) _SCREAMING_SNAKE_CASE =RagRetriever( _a , question_encoder_tokenizer=self.get_dpr_tokenizer() , generator_tokenizer=self.get_bart_tokenizer() ) return retriever def __UpperCamelCase ( self : Tuple ) -> Optional[Any]: """simple docstring""" _SCREAMING_SNAKE_CASE =1 _SCREAMING_SNAKE_CASE =self.get_dummy_canonical_hf_index_retriever() _SCREAMING_SNAKE_CASE =np.array( [np.ones(self.retrieval_vector_size ), -np.ones(self.retrieval_vector_size )] , dtype=np.floataa ) _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE =retriever.retrieve(_a , n_docs=_a ) self.assertEqual(retrieved_doc_embeds.shape , (2, n_docs, self.retrieval_vector_size) ) self.assertEqual(len(_a ) , 2 ) self.assertEqual(sorted(doc_dicts[0] ) , ['''embeddings''', '''id''', '''text''', '''title'''] ) self.assertEqual(len(doc_dicts[0]['''id'''] ) , _a ) 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 __UpperCamelCase ( self : Any ) -> str: """simple docstring""" _SCREAMING_SNAKE_CASE =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: _SCREAMING_SNAKE_CASE =self.get_dummy_dataset() retriever.save_pretrained(_a ) _SCREAMING_SNAKE_CASE =RagRetriever.from_pretrained(_a ) self.assertIsInstance(_a , _a ) _SCREAMING_SNAKE_CASE =np.array( [np.ones(self.retrieval_vector_size ), -np.ones(self.retrieval_vector_size )] , dtype=np.floataa ) _SCREAMING_SNAKE_CASE =retriever.retrieve(_a , n_docs=1 ) self.assertTrue(out is not None ) def __UpperCamelCase ( self : Dict ) -> Union[str, Any]: """simple docstring""" _SCREAMING_SNAKE_CASE =1 _SCREAMING_SNAKE_CASE =self.get_dummy_custom_hf_index_retriever(from_disk=_a ) _SCREAMING_SNAKE_CASE =np.array( [np.ones(self.retrieval_vector_size ), -np.ones(self.retrieval_vector_size )] , dtype=np.floataa ) _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE =retriever.retrieve(_a , n_docs=_a ) self.assertEqual(retrieved_doc_embeds.shape , (2, n_docs, self.retrieval_vector_size) ) self.assertEqual(len(_a ) , 2 ) self.assertEqual(sorted(doc_dicts[0] ) , ['''embeddings''', '''id''', '''text''', '''title'''] ) self.assertEqual(len(doc_dicts[0]['''id'''] ) , _a ) 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 __UpperCamelCase ( self : Optional[Any] ) -> Any: """simple docstring""" _SCREAMING_SNAKE_CASE =self.get_dummy_custom_hf_index_retriever(from_disk=_a ) with tempfile.TemporaryDirectory() as tmp_dirname: retriever.save_pretrained(_a ) _SCREAMING_SNAKE_CASE =RagRetriever.from_pretrained(_a ) self.assertIsInstance(_a , _a ) _SCREAMING_SNAKE_CASE =np.array( [np.ones(self.retrieval_vector_size ), -np.ones(self.retrieval_vector_size )] , dtype=np.floataa ) _SCREAMING_SNAKE_CASE =retriever.retrieve(_a , n_docs=1 ) self.assertTrue(out is not None ) def __UpperCamelCase ( self : Dict ) -> Optional[int]: """simple docstring""" _SCREAMING_SNAKE_CASE =1 _SCREAMING_SNAKE_CASE =self.get_dummy_custom_hf_index_retriever(from_disk=_a ) _SCREAMING_SNAKE_CASE =np.array( [np.ones(self.retrieval_vector_size ), -np.ones(self.retrieval_vector_size )] , dtype=np.floataa ) _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE =retriever.retrieve(_a , n_docs=_a ) self.assertEqual(retrieved_doc_embeds.shape , (2, n_docs, self.retrieval_vector_size) ) self.assertEqual(len(_a ) , 2 ) self.assertEqual(sorted(doc_dicts[0] ) , ['''embeddings''', '''id''', '''text''', '''title'''] ) self.assertEqual(len(doc_dicts[0]['''id'''] ) , _a ) 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 __UpperCamelCase ( self : Tuple ) -> Optional[Any]: """simple docstring""" _SCREAMING_SNAKE_CASE =self.get_dummy_custom_hf_index_retriever(from_disk=_a ) with tempfile.TemporaryDirectory() as tmp_dirname: retriever.save_pretrained(_a ) _SCREAMING_SNAKE_CASE =RagRetriever.from_pretrained(_a ) self.assertIsInstance(_a , _a ) _SCREAMING_SNAKE_CASE =np.array( [np.ones(self.retrieval_vector_size ), -np.ones(self.retrieval_vector_size )] , dtype=np.floataa ) _SCREAMING_SNAKE_CASE =retriever.retrieve(_a , n_docs=1 ) self.assertTrue(out is not None ) def __UpperCamelCase ( self : Optional[int] ) -> Dict: """simple docstring""" _SCREAMING_SNAKE_CASE =1 _SCREAMING_SNAKE_CASE =self.get_dummy_legacy_index_retriever() _SCREAMING_SNAKE_CASE =np.array( [np.ones(self.retrieval_vector_size ), -np.ones(self.retrieval_vector_size )] , dtype=np.floataa ) _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE =retriever.retrieve(_a , n_docs=_a ) self.assertEqual(retrieved_doc_embeds.shape , (2, n_docs, self.retrieval_vector_size) ) self.assertEqual(len(_a ) , 2 ) self.assertEqual(sorted(doc_dicts[0] ) , ['''text''', '''title'''] ) self.assertEqual(len(doc_dicts[0]['''text'''] ) , _a ) 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 __UpperCamelCase ( self : Dict ) -> Dict: """simple docstring""" _SCREAMING_SNAKE_CASE =self.get_dummy_legacy_index_retriever() with tempfile.TemporaryDirectory() as tmp_dirname: retriever.save_pretrained(_a ) _SCREAMING_SNAKE_CASE =RagRetriever.from_pretrained(_a ) self.assertIsInstance(_a , _a ) _SCREAMING_SNAKE_CASE =np.array( [np.ones(self.retrieval_vector_size ), -np.ones(self.retrieval_vector_size )] , dtype=np.floataa ) _SCREAMING_SNAKE_CASE =retriever.retrieve(_a , n_docs=1 ) self.assertTrue(out is not None ) @require_torch @require_tokenizers @require_sentencepiece def __UpperCamelCase ( self : Optional[int] ) -> int: """simple docstring""" import torch _SCREAMING_SNAKE_CASE =1 _SCREAMING_SNAKE_CASE =self.get_dummy_canonical_hf_index_retriever() _SCREAMING_SNAKE_CASE =[[5, 7], [10, 11]] _SCREAMING_SNAKE_CASE =np.array( [np.ones(self.retrieval_vector_size ), -np.ones(self.retrieval_vector_size )] , dtype=np.floataa ) _SCREAMING_SNAKE_CASE =retriever(_a , _a , prefix=retriever.config.generator.prefix , n_docs=_a ) _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE =( 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(_a , _a ) self.assertIsInstance(_a , _a ) self.assertIsInstance(_a , np.ndarray ) _SCREAMING_SNAKE_CASE =retriever( _a , _a , prefix=retriever.config.generator.prefix , n_docs=_a , return_tensors='''pt''' , ) _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE =( # 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(_a , torch.Tensor ) self.assertIsInstance(_a , torch.Tensor ) self.assertIsInstance(_a , torch.Tensor ) @require_torch @require_tokenizers @require_sentencepiece def __UpperCamelCase ( self : str ) -> Any: """simple docstring""" _SCREAMING_SNAKE_CASE =self.get_dpr_ctx_encoder_tokenizer() _SCREAMING_SNAKE_CASE =1 _SCREAMING_SNAKE_CASE =self.get_dummy_custom_hf_index_retriever(from_disk=_a ) retriever.set_ctx_encoder_tokenizer(_a ) _SCREAMING_SNAKE_CASE =[[5, 7], [10, 11]] _SCREAMING_SNAKE_CASE =np.array( [np.ones(self.retrieval_vector_size ), -np.ones(self.retrieval_vector_size )] , dtype=np.floataa ) _SCREAMING_SNAKE_CASE =retriever(_a , _a , prefix=retriever.config.generator.prefix , n_docs=_a ) self.assertEqual( len(_a ) , 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''') ) , _a ) # check for doc token related keys in dictionary.
691
0
import argparse import json import os import evaluate import torch from datasets import load_dataset from torch.optim import AdamW from torch.utils.data import DataLoader from transformers import AutoModelForSequenceClassification, AutoTokenizer, get_linear_schedule_with_warmup, set_seed from accelerate import Accelerator, DistributedType from accelerate.utils.deepspeed import DummyOptim, DummyScheduler _UpperCAmelCase : List[Any] = 16 _UpperCAmelCase : Optional[Any] = 32 def __lowerCamelCase ( UpperCamelCase__ , UpperCamelCase__ = 16 , UpperCamelCase__ = "bert-base-cased" ): '''simple docstring''' snake_case_ = AutoTokenizer.from_pretrained(a__ ) snake_case_ = load_dataset('glue' , 'mrpc' ) def tokenize_function(UpperCamelCase__ ): # max_length=None => use the model max length (it's actually the default) snake_case_ = tokenizer(examples['sentence1'] , examples['sentence2'] , truncation=a__ , max_length=a__ ) return outputs # Apply the method we just defined to all the examples in all the splits of the dataset snake_case_ = datasets.map( a__ , batched=a__ , remove_columns=['idx', 'sentence1', 'sentence2'] , load_from_cache_file=a__ ) # We also rename the 'label' column to 'labels' which is the expected name for labels by the models of the # transformers library snake_case_ = tokenized_datasets.rename_column('label' , 'labels' ) def collate_fn(UpperCamelCase__ ): # On TPU it's best to pad everything to the same length or training will be very slow. if accelerator.distributed_type == DistributedType.TPU: return tokenizer.pad(a__ , padding='max_length' , max_length=128 , return_tensors='pt' ) return tokenizer.pad(a__ , padding='longest' , return_tensors='pt' ) # Instantiate dataloaders. snake_case_ = DataLoader( tokenized_datasets['train'] , shuffle=a__ , collate_fn=a__ , batch_size=a__ ) snake_case_ = DataLoader( tokenized_datasets['validation'] , shuffle=a__ , collate_fn=a__ , batch_size=a__ ) return train_dataloader, eval_dataloader def __lowerCamelCase ( UpperCamelCase__ , UpperCamelCase__ ): '''simple docstring''' snake_case_ = Accelerator() # Sample hyper-parameters for learning rate, batch size, seed and a few other HPs snake_case_ = config['lr'] snake_case_ = int(config['num_epochs'] ) snake_case_ = int(config['seed'] ) snake_case_ = int(config['batch_size'] ) snake_case_ = args.model_name_or_path set_seed(a__ ) snake_case_ , snake_case_ = get_dataloaders(a__ , a__ , a__ ) # Instantiate the model (we build the model here so that the seed also control new weights initialization) snake_case_ = AutoModelForSequenceClassification.from_pretrained(a__ , return_dict=a__ ) # Instantiate optimizer snake_case_ = ( AdamW if accelerator.state.deepspeed_plugin is None or 'optimizer' not in accelerator.state.deepspeed_plugin.deepspeed_config else DummyOptim ) snake_case_ = optimizer_cls(params=model.parameters() , lr=a__ ) if accelerator.state.deepspeed_plugin is not None: snake_case_ = accelerator.state.deepspeed_plugin.deepspeed_config[ 'gradient_accumulation_steps' ] else: snake_case_ = 1 snake_case_ = (len(a__ ) * num_epochs) // gradient_accumulation_steps # Instantiate scheduler if ( accelerator.state.deepspeed_plugin is None or "scheduler" not in accelerator.state.deepspeed_plugin.deepspeed_config ): snake_case_ = get_linear_schedule_with_warmup( optimizer=a__ , num_warmup_steps=0 , num_training_steps=a__ , ) else: snake_case_ = DummyScheduler(a__ , total_num_steps=a__ , warmup_num_steps=0 ) # Prepare everything # There is no specific order to remember, we just need to unpack the objects in the same order we gave them to the # prepare method. snake_case_ , snake_case_ , snake_case_ , snake_case_ , snake_case_ = accelerator.prepare( a__ , a__ , a__ , a__ , a__ ) # We need to keep track of how many total steps we have iterated over snake_case_ = 0 # We also need to keep track of the stating epoch so files are named properly snake_case_ = 0 # Now we train the model snake_case_ = evaluate.load('glue' , 'mrpc' ) snake_case_ = 0 snake_case_ = {} for epoch in range(a__ , a__ ): model.train() for step, batch in enumerate(a__ ): snake_case_ = model(**a__ ) snake_case_ = outputs.loss snake_case_ = loss / gradient_accumulation_steps accelerator.backward(a__ ) if step % gradient_accumulation_steps == 0: optimizer.step() lr_scheduler.step() optimizer.zero_grad() overall_step += 1 model.eval() snake_case_ = 0 for step, batch in enumerate(a__ ): # We could avoid this line since we set the accelerator with `device_placement=True`. batch.to(accelerator.device ) with torch.no_grad(): snake_case_ = model(**a__ ) snake_case_ = outputs.logits.argmax(dim=-1 ) # It is slightly faster to call this once, than multiple times snake_case_ , snake_case_ = accelerator.gather( (predictions, batch['labels']) ) # If we are in a multiprocess environment, the last batch has duplicates if accelerator.use_distributed: if step == len(a__ ) - 1: snake_case_ = predictions[: len(eval_dataloader.dataset ) - samples_seen] snake_case_ = references[: len(eval_dataloader.dataset ) - samples_seen] else: samples_seen += references.shape[0] metric.add_batch( predictions=a__ , references=a__ , ) snake_case_ = metric.compute() # Use accelerator.print to print only on the main process. accelerator.print(F'''epoch {epoch}:''' , a__ ) snake_case_ = eval_metric['accuracy'] if best_performance < eval_metric["accuracy"]: snake_case_ = eval_metric['accuracy'] if args.performance_lower_bound is not None: assert ( args.performance_lower_bound <= best_performance ), F'''Best performance metric {best_performance} is lower than the lower bound {args.performance_lower_bound}''' accelerator.wait_for_everyone() if accelerator.is_main_process: with open(os.path.join(args.output_dir , 'all_results.json' ) , 'w' ) as f: json.dump(a__ , a__ ) def __lowerCamelCase ( ): '''simple docstring''' snake_case_ = argparse.ArgumentParser(description='Simple example of training script tracking peak GPU memory usage.' ) parser.add_argument( '--model_name_or_path' , type=a__ , default='bert-base-cased' , help='Path to pretrained model or model identifier from huggingface.co/models.' , required=a__ , ) parser.add_argument( '--output_dir' , type=a__ , default='.' , help='Optional save directory where all checkpoint folders will be stored. Default is the current working directory.' , ) parser.add_argument( '--performance_lower_bound' , type=a__ , default=a__ , help='Optional lower bound for the performance metric. If set, the training will throw error when the performance metric drops below this value.' , ) parser.add_argument( '--num_epochs' , type=a__ , default=3 , help='Number of train epochs.' , ) snake_case_ = parser.parse_args() snake_case_ = {'lr': 2E-5, 'num_epochs': args.num_epochs, 'seed': 42, 'batch_size': 16} training_function(a__ , a__ ) if __name__ == "__main__": main()
362
import gc import random import unittest import numpy as np import torch from PIL import Image from transformers import XLMRobertaTokenizerFast from diffusers import DDIMScheduler, KandinskyImgaImgPipeline, KandinskyPriorPipeline, UNetaDConditionModel, VQModel from diffusers.pipelines.kandinsky.text_encoder import MCLIPConfig, MultilingualCLIP from diffusers.utils import floats_tensor, load_image, load_numpy, slow, torch_device from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu from ..test_pipelines_common import PipelineTesterMixin, assert_mean_pixel_difference enable_full_determinism() class A__ ( UpperCamelCase__ , unittest.TestCase ): UpperCAmelCase = KandinskyImgaImgPipeline UpperCAmelCase = ["prompt", "image_embeds", "negative_image_embeds", "image"] UpperCAmelCase = [ "prompt", "negative_prompt", "image_embeds", "negative_image_embeds", "image", ] UpperCAmelCase = [ "generator", "height", "width", "strength", "guidance_scale", "negative_prompt", "num_inference_steps", "return_dict", "guidance_scale", "num_images_per_prompt", "output_type", "return_dict", ] UpperCAmelCase = False @property def __UpperCamelCase ( self : Union[str, Any] ) -> Union[str, Any]: """simple docstring""" return 32 @property def __UpperCamelCase ( self : Optional[int] ) -> List[str]: """simple docstring""" return 32 @property def __UpperCamelCase ( self : int ) -> Tuple: """simple docstring""" return self.time_input_dim @property def __UpperCamelCase ( self : Tuple ) -> List[Any]: """simple docstring""" return self.time_input_dim * 4 @property def __UpperCamelCase ( self : Any ) -> Optional[Any]: """simple docstring""" return 100 @property def __UpperCamelCase ( self : Dict ) -> int: """simple docstring""" _SCREAMING_SNAKE_CASE =XLMRobertaTokenizerFast.from_pretrained('''YiYiXu/tiny-random-mclip-base''' ) return tokenizer @property def __UpperCamelCase ( self : Dict ) -> Optional[int]: """simple docstring""" torch.manual_seed(0 ) _SCREAMING_SNAKE_CASE =MCLIPConfig( numDims=self.cross_attention_dim , transformerDimensions=self.text_embedder_hidden_size , hidden_size=self.text_embedder_hidden_size , intermediate_size=37 , num_attention_heads=4 , num_hidden_layers=5 , vocab_size=1005 , ) _SCREAMING_SNAKE_CASE =MultilingualCLIP(_a ) _SCREAMING_SNAKE_CASE =text_encoder.eval() return text_encoder @property def __UpperCamelCase ( self : List[Any] ) -> List[str]: """simple docstring""" torch.manual_seed(0 ) _SCREAMING_SNAKE_CASE ={ '''in_channels''': 4, # Out channels is double in channels because predicts mean and variance '''out_channels''': 8, '''addition_embed_type''': '''text_image''', '''down_block_types''': ('''ResnetDownsampleBlock2D''', '''SimpleCrossAttnDownBlock2D'''), '''up_block_types''': ('''SimpleCrossAttnUpBlock2D''', '''ResnetUpsampleBlock2D'''), '''mid_block_type''': '''UNetMidBlock2DSimpleCrossAttn''', '''block_out_channels''': (self.block_out_channels_a, self.block_out_channels_a * 2), '''layers_per_block''': 1, '''encoder_hid_dim''': self.text_embedder_hidden_size, '''encoder_hid_dim_type''': '''text_image_proj''', '''cross_attention_dim''': self.cross_attention_dim, '''attention_head_dim''': 4, '''resnet_time_scale_shift''': '''scale_shift''', '''class_embed_type''': None, } _SCREAMING_SNAKE_CASE =UNetaDConditionModel(**_a ) return model @property def __UpperCamelCase ( self : List[Any] ) -> Union[str, Any]: """simple docstring""" return { "block_out_channels": [32, 64], "down_block_types": ["DownEncoderBlock2D", "AttnDownEncoderBlock2D"], "in_channels": 3, "latent_channels": 4, "layers_per_block": 1, "norm_num_groups": 8, "norm_type": "spatial", "num_vq_embeddings": 12, "out_channels": 3, "up_block_types": [ "AttnUpDecoderBlock2D", "UpDecoderBlock2D", ], "vq_embed_dim": 4, } @property def __UpperCamelCase ( self : List[Any] ) -> Optional[Any]: """simple docstring""" torch.manual_seed(0 ) _SCREAMING_SNAKE_CASE =VQModel(**self.dummy_movq_kwargs ) return model def __UpperCamelCase ( self : str ) -> int: """simple docstring""" _SCREAMING_SNAKE_CASE =self.dummy_text_encoder _SCREAMING_SNAKE_CASE =self.dummy_tokenizer _SCREAMING_SNAKE_CASE =self.dummy_unet _SCREAMING_SNAKE_CASE =self.dummy_movq _SCREAMING_SNAKE_CASE ={ '''num_train_timesteps''': 1000, '''beta_schedule''': '''linear''', '''beta_start''': 0.0_00_85, '''beta_end''': 0.0_12, '''clip_sample''': False, '''set_alpha_to_one''': False, '''steps_offset''': 0, '''prediction_type''': '''epsilon''', '''thresholding''': False, } _SCREAMING_SNAKE_CASE =DDIMScheduler(**_a ) _SCREAMING_SNAKE_CASE ={ '''text_encoder''': text_encoder, '''tokenizer''': tokenizer, '''unet''': unet, '''scheduler''': scheduler, '''movq''': movq, } return components def __UpperCamelCase ( self : str , _a : int , _a : int=0 ) -> Any: """simple docstring""" _SCREAMING_SNAKE_CASE =floats_tensor((1, self.cross_attention_dim) , rng=random.Random(_a ) ).to(_a ) _SCREAMING_SNAKE_CASE =floats_tensor((1, self.cross_attention_dim) , rng=random.Random(seed + 1 ) ).to(_a ) # create init_image _SCREAMING_SNAKE_CASE =floats_tensor((1, 3, 64, 64) , rng=random.Random(_a ) ).to(_a ) _SCREAMING_SNAKE_CASE =image.cpu().permute(0 , 2 , 3 , 1 )[0] _SCREAMING_SNAKE_CASE =Image.fromarray(np.uinta(_a ) ).convert('''RGB''' ).resize((256, 256) ) if str(_a ).startswith('''mps''' ): _SCREAMING_SNAKE_CASE =torch.manual_seed(_a ) else: _SCREAMING_SNAKE_CASE =torch.Generator(device=_a ).manual_seed(_a ) _SCREAMING_SNAKE_CASE ={ '''prompt''': '''horse''', '''image''': init_image, '''image_embeds''': image_embeds, '''negative_image_embeds''': negative_image_embeds, '''generator''': generator, '''height''': 64, '''width''': 64, '''num_inference_steps''': 10, '''guidance_scale''': 7.0, '''strength''': 0.2, '''output_type''': '''np''', } return inputs def __UpperCamelCase ( self : Any ) -> Optional[int]: """simple docstring""" _SCREAMING_SNAKE_CASE ='''cpu''' _SCREAMING_SNAKE_CASE =self.get_dummy_components() _SCREAMING_SNAKE_CASE =self.pipeline_class(**_a ) _SCREAMING_SNAKE_CASE =pipe.to(_a ) pipe.set_progress_bar_config(disable=_a ) _SCREAMING_SNAKE_CASE =pipe(**self.get_dummy_inputs(_a ) ) _SCREAMING_SNAKE_CASE =output.images _SCREAMING_SNAKE_CASE =pipe( **self.get_dummy_inputs(_a ) , return_dict=_a , )[0] _SCREAMING_SNAKE_CASE =image[0, -3:, -3:, -1] _SCREAMING_SNAKE_CASE =image_from_tuple[0, -3:, -3:, -1] assert image.shape == (1, 64, 64, 3) _SCREAMING_SNAKE_CASE =np.array( [0.61_47_49_43, 0.6_07_35_39, 0.43_30_85_44, 0.5_92_82_69, 0.47_49_35_95, 0.46_75_59_73, 0.4_61_38_38, 0.45_36_87_97, 0.50_11_92_33] ) assert ( np.abs(image_slice.flatten() - expected_slice ).max() < 1E-2 ), f" expected_slice {expected_slice}, but got {image_slice.flatten()}" assert ( np.abs(image_from_tuple_slice.flatten() - expected_slice ).max() < 1E-2 ), f" expected_slice {expected_slice}, but got {image_from_tuple_slice.flatten()}" @slow @require_torch_gpu class A__ ( unittest.TestCase ): def __UpperCamelCase ( self : Optional[Any] ) -> List[Any]: """simple docstring""" super().tearDown() gc.collect() torch.cuda.empty_cache() def __UpperCamelCase ( self : Dict ) -> Any: """simple docstring""" _SCREAMING_SNAKE_CASE =load_numpy( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main''' '''/kandinsky/kandinsky_img2img_frog.npy''' ) _SCREAMING_SNAKE_CASE =load_image( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main''' '''/kandinsky/cat.png''' ) _SCREAMING_SNAKE_CASE ='''A red cartoon frog, 4k''' _SCREAMING_SNAKE_CASE =KandinskyPriorPipeline.from_pretrained( '''kandinsky-community/kandinsky-2-1-prior''' , torch_dtype=torch.floataa ) pipe_prior.to(_a ) _SCREAMING_SNAKE_CASE =KandinskyImgaImgPipeline.from_pretrained( '''kandinsky-community/kandinsky-2-1''' , torch_dtype=torch.floataa ) _SCREAMING_SNAKE_CASE =pipeline.to(_a ) pipeline.set_progress_bar_config(disable=_a ) _SCREAMING_SNAKE_CASE =torch.Generator(device='''cpu''' ).manual_seed(0 ) _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE =pipe_prior( _a , generator=_a , num_inference_steps=5 , negative_prompt='''''' , ).to_tuple() _SCREAMING_SNAKE_CASE =pipeline( _a , image=_a , image_embeds=_a , negative_image_embeds=_a , generator=_a , num_inference_steps=100 , height=768 , width=768 , strength=0.2 , output_type='''np''' , ) _SCREAMING_SNAKE_CASE =output.images[0] assert image.shape == (768, 768, 3) assert_mean_pixel_difference(_a , _a )
691
0
"""simple docstring""" import os from math import logaa def a__ ( lowerCAmelCase = "base_exp.txt" ) -> List[str]: UpperCAmelCase__ : List[str] = 0 UpperCAmelCase__ : Union[str, Any] = 0 for i, line in enumerate(open(os.path.join(os.path.dirname(a__ ) , a__ ) ) ): UpperCAmelCase__ , UpperCAmelCase__ : Union[str, Any] = list(map(a__ , line.split(""",""" ) ) ) if x * logaa(a__ ) > largest: UpperCAmelCase__ : List[Any] = x * logaa(a__ ) UpperCAmelCase__ : Tuple = i + 1 return result if __name__ == "__main__": print(solution())
182
import unittest import numpy as np from transformers.testing_utils import require_torch, require_vision from transformers.utils import is_torch_available, is_vision_available from ...test_image_processing_common import ImageProcessingSavingTestMixin if is_torch_available(): import torch if is_vision_available(): from PIL import Image from transformers import ChineseCLIPImageProcessor class A__ ( unittest.TestCase ): def __init__( self : List[str] , _a : Dict , _a : Dict=7 , _a : List[str]=3 , _a : str=18 , _a : Optional[int]=30 , _a : Tuple=400 , _a : Optional[Any]=True , _a : Dict=None , _a : str=True , _a : Tuple=None , _a : Any=True , _a : Any=[0.48_14_54_66, 0.4_57_82_75, 0.40_82_10_73] , _a : str=[0.26_86_29_54, 0.26_13_02_58, 0.27_57_77_11] , _a : List[Any]=True , ) -> Optional[int]: """simple docstring""" _SCREAMING_SNAKE_CASE =size if size is not None else {'''height''': 224, '''width''': 224} _SCREAMING_SNAKE_CASE =crop_size if crop_size is not None else {'''height''': 18, '''width''': 18} _SCREAMING_SNAKE_CASE =parent _SCREAMING_SNAKE_CASE =batch_size _SCREAMING_SNAKE_CASE =num_channels _SCREAMING_SNAKE_CASE =image_size _SCREAMING_SNAKE_CASE =min_resolution _SCREAMING_SNAKE_CASE =max_resolution _SCREAMING_SNAKE_CASE =do_resize _SCREAMING_SNAKE_CASE =size _SCREAMING_SNAKE_CASE =do_center_crop _SCREAMING_SNAKE_CASE =crop_size _SCREAMING_SNAKE_CASE =do_normalize _SCREAMING_SNAKE_CASE =image_mean _SCREAMING_SNAKE_CASE =image_std _SCREAMING_SNAKE_CASE =do_convert_rgb def __UpperCamelCase ( self : Any ) -> Tuple: """simple docstring""" return { "do_resize": self.do_resize, "size": self.size, "do_center_crop": self.do_center_crop, "crop_size": self.crop_size, "do_normalize": self.do_normalize, "image_mean": self.image_mean, "image_std": self.image_std, "do_convert_rgb": self.do_convert_rgb, } def __UpperCamelCase ( self : Tuple , _a : Optional[Any]=False , _a : str=False , _a : Dict=False ) -> Dict: """simple docstring""" assert not (numpify and torchify), "You cannot specify both numpy and PyTorch tensors at the same time" if equal_resolution: _SCREAMING_SNAKE_CASE =[] for i in range(self.batch_size ): image_inputs.append( np.random.randint( 255 , size=(self.num_channels, self.max_resolution, self.max_resolution) , dtype=np.uinta ) ) else: _SCREAMING_SNAKE_CASE =[] for i in range(self.batch_size ): _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE =np.random.choice(np.arange(self.min_resolution , self.max_resolution ) , 2 ) image_inputs.append(np.random.randint(255 , size=(self.num_channels, width, height) , dtype=np.uinta ) ) if not numpify and not torchify: # PIL expects the channel dimension as last dimension _SCREAMING_SNAKE_CASE =[Image.fromarray(np.moveaxis(_a , 0 , -1 ) ) for x in image_inputs] if torchify: _SCREAMING_SNAKE_CASE =[torch.from_numpy(_a ) for x in image_inputs] return image_inputs @require_torch @require_vision class A__ ( UpperCamelCase__ , unittest.TestCase ): UpperCAmelCase = ChineseCLIPImageProcessor if is_vision_available() else None def __UpperCamelCase ( self : Any ) -> Tuple: """simple docstring""" _SCREAMING_SNAKE_CASE =ChineseCLIPImageProcessingTester(self , do_center_crop=_a ) @property def __UpperCamelCase ( self : Union[str, Any] ) -> Tuple: """simple docstring""" return self.image_processor_tester.prepare_image_processor_dict() def __UpperCamelCase ( self : int ) -> Dict: """simple docstring""" _SCREAMING_SNAKE_CASE =self.image_processing_class(**self.image_processor_dict ) self.assertTrue(hasattr(_a , '''do_resize''' ) ) self.assertTrue(hasattr(_a , '''size''' ) ) self.assertTrue(hasattr(_a , '''do_center_crop''' ) ) self.assertTrue(hasattr(_a , '''center_crop''' ) ) self.assertTrue(hasattr(_a , '''do_normalize''' ) ) self.assertTrue(hasattr(_a , '''image_mean''' ) ) self.assertTrue(hasattr(_a , '''image_std''' ) ) self.assertTrue(hasattr(_a , '''do_convert_rgb''' ) ) def __UpperCamelCase ( self : List[str] ) -> str: """simple docstring""" _SCREAMING_SNAKE_CASE =self.image_processing_class.from_dict(self.image_processor_dict ) self.assertEqual(image_processor.size , {'''height''': 224, '''width''': 224} ) self.assertEqual(image_processor.crop_size , {'''height''': 18, '''width''': 18} ) _SCREAMING_SNAKE_CASE =self.image_processing_class.from_dict(self.image_processor_dict , size=42 , crop_size=84 ) self.assertEqual(image_processor.size , {'''shortest_edge''': 42} ) self.assertEqual(image_processor.crop_size , {'''height''': 84, '''width''': 84} ) def __UpperCamelCase ( self : Union[str, Any] ) -> int: """simple docstring""" pass def __UpperCamelCase ( self : str ) -> List[str]: """simple docstring""" _SCREAMING_SNAKE_CASE =self.image_processing_class(**self.image_processor_dict ) # create random PIL images _SCREAMING_SNAKE_CASE =self.image_processor_tester.prepare_inputs(equal_resolution=_a ) for image in image_inputs: self.assertIsInstance(_a , Image.Image ) # Test not batched input _SCREAMING_SNAKE_CASE =image_processing(image_inputs[0] , return_tensors='''pt''' ).pixel_values self.assertEqual( encoded_images.shape , ( 1, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size['''height'''], self.image_processor_tester.crop_size['''width'''], ) , ) # Test batched _SCREAMING_SNAKE_CASE =image_processing(_a , return_tensors='''pt''' ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size['''height'''], self.image_processor_tester.crop_size['''width'''], ) , ) def __UpperCamelCase ( self : Optional[Any] ) -> List[str]: """simple docstring""" _SCREAMING_SNAKE_CASE =self.image_processing_class(**self.image_processor_dict ) # create random numpy tensors _SCREAMING_SNAKE_CASE =self.image_processor_tester.prepare_inputs(equal_resolution=_a , numpify=_a ) for image in image_inputs: self.assertIsInstance(_a , np.ndarray ) # Test not batched input _SCREAMING_SNAKE_CASE =image_processing(image_inputs[0] , return_tensors='''pt''' ).pixel_values self.assertEqual( encoded_images.shape , ( 1, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size['''height'''], self.image_processor_tester.crop_size['''width'''], ) , ) # Test batched _SCREAMING_SNAKE_CASE =image_processing(_a , return_tensors='''pt''' ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size['''height'''], self.image_processor_tester.crop_size['''width'''], ) , ) def __UpperCamelCase ( self : Optional[int] ) -> Tuple: """simple docstring""" _SCREAMING_SNAKE_CASE =self.image_processing_class(**self.image_processor_dict ) # create random PyTorch tensors _SCREAMING_SNAKE_CASE =self.image_processor_tester.prepare_inputs(equal_resolution=_a , torchify=_a ) for image in image_inputs: self.assertIsInstance(_a , torch.Tensor ) # Test not batched input _SCREAMING_SNAKE_CASE =image_processing(image_inputs[0] , return_tensors='''pt''' ).pixel_values self.assertEqual( encoded_images.shape , ( 1, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size['''height'''], self.image_processor_tester.crop_size['''width'''], ) , ) # Test batched _SCREAMING_SNAKE_CASE =image_processing(_a , return_tensors='''pt''' ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size['''height'''], self.image_processor_tester.crop_size['''width'''], ) , ) @require_torch @require_vision class A__ ( UpperCamelCase__ , unittest.TestCase ): UpperCAmelCase = ChineseCLIPImageProcessor if is_vision_available() else None def __UpperCamelCase ( self : int ) -> str: """simple docstring""" _SCREAMING_SNAKE_CASE =ChineseCLIPImageProcessingTester(self , num_channels=4 , do_center_crop=_a ) _SCREAMING_SNAKE_CASE =3 @property def __UpperCamelCase ( self : Optional[int] ) -> Tuple: """simple docstring""" return self.image_processor_tester.prepare_image_processor_dict() def __UpperCamelCase ( self : int ) -> List[Any]: """simple docstring""" _SCREAMING_SNAKE_CASE =self.image_processing_class(**self.image_processor_dict ) self.assertTrue(hasattr(_a , '''do_resize''' ) ) self.assertTrue(hasattr(_a , '''size''' ) ) self.assertTrue(hasattr(_a , '''do_center_crop''' ) ) self.assertTrue(hasattr(_a , '''center_crop''' ) ) self.assertTrue(hasattr(_a , '''do_normalize''' ) ) self.assertTrue(hasattr(_a , '''image_mean''' ) ) self.assertTrue(hasattr(_a , '''image_std''' ) ) self.assertTrue(hasattr(_a , '''do_convert_rgb''' ) ) def __UpperCamelCase ( self : Any ) -> Union[str, Any]: """simple docstring""" pass def __UpperCamelCase ( self : Dict ) -> Any: """simple docstring""" _SCREAMING_SNAKE_CASE =self.image_processing_class(**self.image_processor_dict ) # create random PIL images _SCREAMING_SNAKE_CASE =self.image_processor_tester.prepare_inputs(equal_resolution=_a ) for image in image_inputs: self.assertIsInstance(_a , Image.Image ) # Test not batched input _SCREAMING_SNAKE_CASE =image_processing(image_inputs[0] , return_tensors='''pt''' ).pixel_values self.assertEqual( encoded_images.shape , ( 1, self.expected_encoded_image_num_channels, self.image_processor_tester.crop_size['''height'''], self.image_processor_tester.crop_size['''width'''], ) , ) # Test batched _SCREAMING_SNAKE_CASE =image_processing(_a , return_tensors='''pt''' ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.expected_encoded_image_num_channels, self.image_processor_tester.crop_size['''height'''], self.image_processor_tester.crop_size['''width'''], ) , )
691
0
"""simple docstring""" import logging import os from typing import List, TextIO, Union from conllu import parse_incr from utils_ner import InputExample, Split, TokenClassificationTask UpperCAmelCase : List[str] = logging.getLogger(__name__) class lowerCamelCase__ ( UpperCamelCase__ ): """simple docstring""" def __init__( self : Dict , UpperCamelCase : Tuple=-1 ): '''simple docstring''' __UpperCAmelCase : Dict = label_idx def lowerCamelCase__ ( self : Optional[Any] , UpperCamelCase : Any , UpperCamelCase : Union[Split, str] ): '''simple docstring''' if isinstance(_a , _a ): __UpperCAmelCase : List[str] = mode.value __UpperCAmelCase : Union[str, Any] = os.path.join(_a , f'''{mode}.txt''' ) __UpperCAmelCase : str = 1 __UpperCAmelCase : str = [] with open(_a , encoding="""utf-8""" ) as f: __UpperCAmelCase : str = [] __UpperCAmelCase : Tuple = [] for line in f: if line.startswith("""-DOCSTART-""" ) or line == "" or line == "\n": if words: examples.append(InputExample(guid=f'''{mode}-{guid_index}''' , words=_a , labels=_a ) ) guid_index += 1 __UpperCAmelCase : Optional[Any] = [] __UpperCAmelCase : Tuple = [] else: __UpperCAmelCase : str = line.split(""" """ ) words.append(splits[0] ) if len(_a ) > 1: labels.append(splits[self.label_idx].replace("""\n""" , """""" ) ) else: # Examples could have no label for mode = "test" labels.append("""O""" ) if words: examples.append(InputExample(guid=f'''{mode}-{guid_index}''' , words=_a , labels=_a ) ) return examples def lowerCamelCase__ ( self : Dict , UpperCamelCase : TextIO , UpperCamelCase : TextIO , UpperCamelCase : List ): '''simple docstring''' __UpperCAmelCase : Any = 0 for line in test_input_reader: if line.startswith("""-DOCSTART-""" ) or line == "" or line == "\n": writer.write(_a ) if not preds_list[example_id]: example_id += 1 elif preds_list[example_id]: __UpperCAmelCase : str = line.split()[0] + """ """ + preds_list[example_id].pop(0 ) + """\n""" writer.write(_a ) else: logger.warning("""Maximum sequence length exceeded: No prediction for \'%s\'.""" , line.split()[0] ) def lowerCamelCase__ ( self : int , UpperCamelCase : str ): '''simple docstring''' if path: with open(_a , """r""" ) as f: __UpperCAmelCase : int = f.read().splitlines() if "O" not in labels: __UpperCAmelCase : int = ["""O"""] + labels return labels else: return ["O", "B-MISC", "I-MISC", "B-PER", "I-PER", "B-ORG", "I-ORG", "B-LOC", "I-LOC"] class lowerCamelCase__ ( UpperCamelCase__ ): """simple docstring""" def __init__( self : str ): '''simple docstring''' super().__init__(label_idx=-2 ) def lowerCamelCase__ ( self : Optional[Any] , UpperCamelCase : str ): '''simple docstring''' if path: with open(_a , """r""" ) as f: __UpperCAmelCase : Optional[Any] = f.read().splitlines() if "O" not in labels: __UpperCAmelCase : Tuple = ["""O"""] + labels return labels else: return [ "O", "B-ADVP", "B-INTJ", "B-LST", "B-PRT", "B-NP", "B-SBAR", "B-VP", "B-ADJP", "B-CONJP", "B-PP", "I-ADVP", "I-INTJ", "I-LST", "I-PRT", "I-NP", "I-SBAR", "I-VP", "I-ADJP", "I-CONJP", "I-PP", ] class lowerCamelCase__ ( UpperCamelCase__ ): """simple docstring""" def lowerCamelCase__ ( self : Optional[Any] , UpperCamelCase : Any , UpperCamelCase : Union[Split, str] ): '''simple docstring''' if isinstance(_a , _a ): __UpperCAmelCase : str = mode.value __UpperCAmelCase : Tuple = os.path.join(_a , f'''{mode}.txt''' ) __UpperCAmelCase : Dict = 1 __UpperCAmelCase : Tuple = [] with open(_a , encoding="""utf-8""" ) as f: for sentence in parse_incr(_a ): __UpperCAmelCase : Union[str, Any] = [] __UpperCAmelCase : Tuple = [] for token in sentence: words.append(token["""form"""] ) labels.append(token["""upos"""] ) assert len(_a ) == len(_a ) if words: examples.append(InputExample(guid=f'''{mode}-{guid_index}''' , words=_a , labels=_a ) ) guid_index += 1 return examples def lowerCamelCase__ ( self : List[str] , UpperCamelCase : TextIO , UpperCamelCase : TextIO , UpperCamelCase : List ): '''simple docstring''' __UpperCAmelCase : List[Any] = 0 for sentence in parse_incr(_a ): __UpperCAmelCase : Optional[Any] = preds_list[example_id] __UpperCAmelCase : Dict = """""" for token in sentence: out += f'''{token["form"]} ({token["upos"]}|{s_p.pop(0 )}) ''' out += "\n" writer.write(_a ) example_id += 1 def lowerCamelCase__ ( self : Optional[Any] , UpperCamelCase : str ): '''simple docstring''' if path: with open(_a , """r""" ) as f: return f.read().splitlines() else: return [ "ADJ", "ADP", "ADV", "AUX", "CCONJ", "DET", "INTJ", "NOUN", "NUM", "PART", "PRON", "PROPN", "PUNCT", "SCONJ", "SYM", "VERB", "X", ]
139
def lowerCamelCase( a__ ,a__): return int((input_a, input_a).count(0) == 0) def lowerCamelCase( ): assert and_gate(0 ,0) == 0 assert and_gate(0 ,1) == 0 assert and_gate(1 ,0) == 0 assert and_gate(1 ,1) == 1 if __name__ == "__main__": test_and_gate() print(and_gate(1, 0)) print(and_gate(0, 0)) print(and_gate(0, 1)) print(and_gate(1, 1))
691
0
import logging import os from dataclasses import dataclass, field from typing import Dict, Optional import numpy as np from utils_multiple_choice import MultipleChoiceDataset, Split, processors import transformers from transformers import ( AutoConfig, AutoModelForMultipleChoice, AutoTokenizer, DataCollatorWithPadding, EvalPrediction, HfArgumentParser, Trainer, TrainingArguments, set_seed, ) from transformers.trainer_utils import is_main_process __UpperCamelCase : str = logging.getLogger(__name__) def _UpperCAmelCase ( UpperCAmelCase : Union[str, Any] , UpperCAmelCase : List[Any] ): """simple docstring""" return (preds == labels).mean() @dataclass class _UpperCamelCase : '''simple docstring''' a_ : Any = field( metadata={"help": "Path to pretrained model or model identifier from huggingface.co/models"} ) a_ : Optional[int] = field( default=UpperCamelCase__,metadata={"help": "Pretrained config name or path if not the same as model_name"} ) a_ : Dict = field( default=UpperCamelCase__,metadata={"help": "Pretrained tokenizer name or path if not the same as model_name"} ) a_ : Tuple = field( default=UpperCamelCase__,metadata={"help": "Where do you want to store the pretrained models downloaded from huggingface.co"},) @dataclass class _UpperCamelCase : '''simple docstring''' a_ : Dict = field(metadata={"help": "The name of the task to train on: " + ", ".join(processors.keys() )} ) a_ : List[Any] = field(metadata={"help": "Should contain the data files for the task."} ) a_ : Tuple = field( default=128,metadata={ "help": ( "The maximum total input sequence length after tokenization. Sequences longer " "than this will be truncated, sequences shorter will be padded." ) },) a_ : Any = field( default=UpperCamelCase__,metadata={"help": "Overwrite the cached training and evaluation sets"} ) def _UpperCAmelCase ( ): """simple docstring""" __lowerCamelCase : List[str] = HfArgumentParser((ModelArguments, DataTrainingArguments, TrainingArguments) ) __lowerCamelCase , __lowerCamelCase , __lowerCamelCase : Optional[Any] = parser.parse_args_into_dataclasses() if ( os.path.exists(training_args.output_dir ) and os.listdir(training_args.output_dir ) and training_args.do_train and not training_args.overwrite_output_dir ): raise ValueError( f"""Output directory ({training_args.output_dir}) already exists and is not empty. Use""" """ --overwrite_output_dir to overcome.""" ) # Setup logging logging.basicConfig( format="""%(asctime)s - %(levelname)s - %(name)s - %(message)s""" , datefmt="""%m/%d/%Y %H:%M:%S""" , level=logging.INFO if training_args.local_rank in [-1, 0] else logging.WARN , ) logger.warning( """Process rank: %s, device: %s, n_gpu: %s, distributed training: %s, 16-bits training: %s""" , training_args.local_rank , training_args.device , training_args.n_gpu , bool(training_args.local_rank != -1 ) , training_args.fpaa , ) # Set the verbosity to info of the Transformers logger (on main process only): if is_main_process(training_args.local_rank ): transformers.utils.logging.set_verbosity_info() transformers.utils.logging.enable_default_handler() transformers.utils.logging.enable_explicit_format() logger.info("""Training/evaluation parameters %s""" , a__ ) # Set seed set_seed(training_args.seed ) try: __lowerCamelCase : Any = processors[data_args.task_name]() __lowerCamelCase : Any = processor.get_labels() __lowerCamelCase : Any = len(a__ ) except KeyError: raise ValueError("""Task not found: %s""" % (data_args.task_name) ) # Load pretrained model and tokenizer # # Distributed training: # The .from_pretrained methods guarantee that only one local process can concurrently # download model & vocab. __lowerCamelCase : Dict = AutoConfig.from_pretrained( model_args.config_name if model_args.config_name else model_args.model_name_or_path , num_labels=a__ , finetuning_task=data_args.task_name , cache_dir=model_args.cache_dir , ) __lowerCamelCase : List[Any] = AutoTokenizer.from_pretrained( model_args.tokenizer_name if model_args.tokenizer_name else model_args.model_name_or_path , cache_dir=model_args.cache_dir , ) __lowerCamelCase : int = AutoModelForMultipleChoice.from_pretrained( model_args.model_name_or_path , from_tf=bool(""".ckpt""" in model_args.model_name_or_path ) , config=a__ , cache_dir=model_args.cache_dir , ) # Get datasets __lowerCamelCase : Tuple = ( MultipleChoiceDataset( data_dir=data_args.data_dir , tokenizer=a__ , task=data_args.task_name , max_seq_length=data_args.max_seq_length , overwrite_cache=data_args.overwrite_cache , mode=Split.train , ) if training_args.do_train else None ) __lowerCamelCase : Optional[Any] = ( MultipleChoiceDataset( data_dir=data_args.data_dir , tokenizer=a__ , task=data_args.task_name , max_seq_length=data_args.max_seq_length , overwrite_cache=data_args.overwrite_cache , mode=Split.dev , ) if training_args.do_eval else None ) def compute_metrics(UpperCAmelCase : int ) -> Dict: __lowerCamelCase : List[str] = np.argmax(p.predictions , axis=1 ) return {"acc": simple_accuracy(a__ , p.label_ids )} # Data collator __lowerCamelCase : Tuple = DataCollatorWithPadding(a__ , pad_to_multiple_of=8 ) if training_args.fpaa else None # Initialize our Trainer __lowerCamelCase : List[Any] = Trainer( model=a__ , args=a__ , train_dataset=a__ , eval_dataset=a__ , compute_metrics=a__ , data_collator=a__ , ) # Training if training_args.do_train: trainer.train( model_path=model_args.model_name_or_path if os.path.isdir(model_args.model_name_or_path ) else None ) trainer.save_model() # For convenience, we also re-save the tokenizer to the same directory, # so that you can share your model easily on huggingface.co/models =) if trainer.is_world_master(): tokenizer.save_pretrained(training_args.output_dir ) # Evaluation __lowerCamelCase : Tuple = {} if training_args.do_eval: logger.info("""*** Evaluate ***""" ) __lowerCamelCase : Optional[int] = trainer.evaluate() __lowerCamelCase : List[str] = os.path.join(training_args.output_dir , """eval_results.txt""" ) if trainer.is_world_master(): with open(a__ , """w""" ) as writer: logger.info("""***** Eval results *****""" ) for key, value in result.items(): logger.info(""" %s = %s""" , a__ , a__ ) writer.write("""%s = %s\n""" % (key, value) ) results.update(a__ ) return results def _UpperCAmelCase ( UpperCAmelCase : str ): """simple docstring""" main() if __name__ == "__main__": main()
519
import argparse import os import sys from unittest.mock import patch import pytorch_lightning as pl import timeout_decorator import torch from distillation import SummarizationDistiller, distill_main from finetune import SummarizationModule, main from transformers import MarianMTModel from transformers.file_utils import cached_path from transformers.testing_utils import TestCasePlus, require_torch_gpu, slow from utils import load_json snake_case_ : Optional[int] = '''sshleifer/mar_enro_6_3_student''' class A__ ( UpperCamelCase__ ): def __UpperCamelCase ( self : Any ) -> Any: """simple docstring""" super().setUp() _SCREAMING_SNAKE_CASE =cached_path( '''https://cdn-datasets.huggingface.co/translation/wmt_en_ro-tr40k-va0.5k-te0.5k.tar.gz''' , extract_compressed_file=_a , ) _SCREAMING_SNAKE_CASE =f"{data_cached}/wmt_en_ro-tr40k-va0.5k-te0.5k" @slow @require_torch_gpu def __UpperCamelCase ( self : Optional[Any] ) -> Union[str, Any]: """simple docstring""" MarianMTModel.from_pretrained(_a ) @slow @require_torch_gpu def __UpperCamelCase ( self : str ) -> Dict: """simple docstring""" _SCREAMING_SNAKE_CASE ={ '''$MAX_LEN''': 64, '''$BS''': 64, '''$GAS''': 1, '''$ENRO_DIR''': self.data_dir, '''facebook/mbart-large-cc25''': MARIAN_MODEL, # "val_check_interval=0.25": "val_check_interval=1.0", '''--learning_rate=3e-5''': '''--learning_rate 3e-4''', '''--num_train_epochs 6''': '''--num_train_epochs 1''', } # Clean up bash script _SCREAMING_SNAKE_CASE =(self.test_file_dir / '''train_mbart_cc25_enro.sh''').open().read().split('''finetune.py''' )[1].strip() _SCREAMING_SNAKE_CASE =bash_script.replace('''\\\n''' , '''''' ).strip().replace('''"$@"''' , '''''' ) for k, v in env_vars_to_replace.items(): _SCREAMING_SNAKE_CASE =bash_script.replace(_a , str(_a ) ) _SCREAMING_SNAKE_CASE =self.get_auto_remove_tmp_dir() # bash_script = bash_script.replace("--fp16 ", "") _SCREAMING_SNAKE_CASE =f"\n --output_dir {output_dir}\n --tokenizer_name Helsinki-NLP/opus-mt-en-ro\n --sortish_sampler\n --do_predict\n --gpus 1\n --freeze_encoder\n --n_train 40000\n --n_val 500\n --n_test 500\n --fp16_opt_level O1\n --num_sanity_val_steps 0\n --eval_beams 2\n ".split() # XXX: args.gpus > 1 : handle multi_gpu in the future _SCREAMING_SNAKE_CASE =['''finetune.py'''] + bash_script.split() + args with patch.object(_a , '''argv''' , _a ): _SCREAMING_SNAKE_CASE =argparse.ArgumentParser() _SCREAMING_SNAKE_CASE =pl.Trainer.add_argparse_args(_a ) _SCREAMING_SNAKE_CASE =SummarizationModule.add_model_specific_args(_a , os.getcwd() ) _SCREAMING_SNAKE_CASE =parser.parse_args() _SCREAMING_SNAKE_CASE =main(_a ) # Check metrics _SCREAMING_SNAKE_CASE =load_json(model.metrics_save_path ) _SCREAMING_SNAKE_CASE =metrics['''val'''][0] _SCREAMING_SNAKE_CASE =metrics['''val'''][-1] self.assertEqual(len(metrics['''val'''] ) , (args.max_epochs / args.val_check_interval) ) assert isinstance(last_step_stats[f"val_avg_{model.val_metric}"] , _a ) self.assertGreater(last_step_stats['''val_avg_gen_time'''] , 0.01 ) # model hanging on generate. Maybe bad config was saved. (XXX: old comment/assert?) self.assertLessEqual(last_step_stats['''val_avg_gen_time'''] , 1.0 ) # test learning requirements: # 1. BLEU improves over the course of training by more than 2 pts self.assertGreater(last_step_stats['''val_avg_bleu'''] - first_step_stats['''val_avg_bleu'''] , 2 ) # 2. BLEU finishes above 17 self.assertGreater(last_step_stats['''val_avg_bleu'''] , 17 ) # 3. test BLEU and val BLEU within ~1.1 pt. self.assertLess(abs(metrics['''val'''][-1]['''val_avg_bleu'''] - metrics['''test'''][-1]['''test_avg_bleu'''] ) , 1.1 ) # check lightning ckpt can be loaded and has a reasonable statedict _SCREAMING_SNAKE_CASE =os.listdir(_a ) _SCREAMING_SNAKE_CASE =[x for x in contents if x.endswith('''.ckpt''' )][0] _SCREAMING_SNAKE_CASE =os.path.join(args.output_dir , _a ) _SCREAMING_SNAKE_CASE =torch.load(_a , map_location='''cpu''' ) _SCREAMING_SNAKE_CASE ='''model.model.decoder.layers.0.encoder_attn_layer_norm.weight''' assert expected_key in ckpt["state_dict"] assert ckpt["state_dict"]["model.model.decoder.layers.0.encoder_attn_layer_norm.weight"].dtype == torch.floataa # TODO: turn on args.do_predict when PL bug fixed. if args.do_predict: _SCREAMING_SNAKE_CASE ={os.path.basename(_a ) for p in contents} assert "test_generations.txt" in contents assert "test_results.txt" in contents # assert len(metrics["val"]) == desired_n_evals assert len(metrics['''test'''] ) == 1 class A__ ( UpperCamelCase__ ): @timeout_decorator.timeout(600 ) @slow @require_torch_gpu def __UpperCamelCase ( self : Union[str, Any] ) -> str: """simple docstring""" _SCREAMING_SNAKE_CASE =f"{self.test_file_dir_str}/test_data/wmt_en_ro" _SCREAMING_SNAKE_CASE ={ '''--fp16_opt_level=O1''': '''''', '''$MAX_LEN''': 128, '''$BS''': 16, '''$GAS''': 1, '''$ENRO_DIR''': data_dir, '''$m''': '''sshleifer/student_marian_en_ro_6_1''', '''val_check_interval=0.25''': '''val_check_interval=1.0''', } # Clean up bash script _SCREAMING_SNAKE_CASE =( (self.test_file_dir / '''distil_marian_no_teacher.sh''').open().read().split('''distillation.py''' )[1].strip() ) _SCREAMING_SNAKE_CASE =bash_script.replace('''\\\n''' , '''''' ).strip().replace('''"$@"''' , '''''' ) _SCREAMING_SNAKE_CASE =bash_script.replace('''--fp16 ''' , ''' ''' ) for k, v in env_vars_to_replace.items(): _SCREAMING_SNAKE_CASE =bash_script.replace(_a , str(_a ) ) _SCREAMING_SNAKE_CASE =self.get_auto_remove_tmp_dir() _SCREAMING_SNAKE_CASE =bash_script.replace('''--fp16''' , '''''' ) _SCREAMING_SNAKE_CASE =6 _SCREAMING_SNAKE_CASE =( ['''distillation.py'''] + bash_script.split() + [ f"--output_dir={output_dir}", '''--gpus=1''', '''--learning_rate=1e-3''', f"--num_train_epochs={epochs}", '''--warmup_steps=10''', '''--val_check_interval=1.0''', '''--do_predict''', ] ) with patch.object(_a , '''argv''' , _a ): _SCREAMING_SNAKE_CASE =argparse.ArgumentParser() _SCREAMING_SNAKE_CASE =pl.Trainer.add_argparse_args(_a ) _SCREAMING_SNAKE_CASE =SummarizationDistiller.add_model_specific_args(_a , os.getcwd() ) _SCREAMING_SNAKE_CASE =parser.parse_args() # assert args.gpus == gpus THIS BREAKS for multi_gpu _SCREAMING_SNAKE_CASE =distill_main(_a ) # Check metrics _SCREAMING_SNAKE_CASE =load_json(model.metrics_save_path ) _SCREAMING_SNAKE_CASE =metrics['''val'''][0] _SCREAMING_SNAKE_CASE =metrics['''val'''][-1] assert len(metrics['''val'''] ) >= (args.max_epochs / args.val_check_interval) # +1 accounts for val_sanity_check assert last_step_stats["val_avg_gen_time"] >= 0.01 assert first_step_stats["val_avg_bleu"] < last_step_stats["val_avg_bleu"] # model learned nothing assert 1.0 >= last_step_stats["val_avg_gen_time"] # model hanging on generate. Maybe bad config was saved. assert isinstance(last_step_stats[f"val_avg_{model.val_metric}"] , _a ) # check lightning ckpt can be loaded and has a reasonable statedict _SCREAMING_SNAKE_CASE =os.listdir(_a ) _SCREAMING_SNAKE_CASE =[x for x in contents if x.endswith('''.ckpt''' )][0] _SCREAMING_SNAKE_CASE =os.path.join(args.output_dir , _a ) _SCREAMING_SNAKE_CASE =torch.load(_a , map_location='''cpu''' ) _SCREAMING_SNAKE_CASE ='''model.model.decoder.layers.0.encoder_attn_layer_norm.weight''' assert expected_key in ckpt["state_dict"] assert ckpt["state_dict"]["model.model.decoder.layers.0.encoder_attn_layer_norm.weight"].dtype == torch.floataa # TODO: turn on args.do_predict when PL bug fixed. if args.do_predict: _SCREAMING_SNAKE_CASE ={os.path.basename(_a ) for p in contents} assert "test_generations.txt" in contents assert "test_results.txt" in contents # assert len(metrics["val"]) == desired_n_evals assert len(metrics['''test'''] ) == 1
691
0
def __A ( _A , _A ): """simple docstring""" return number | (1 << position) def __A ( _A , _A ): """simple docstring""" return number & ~(1 << position) def __A ( _A , _A ): """simple docstring""" return number ^ (1 << position) def __A ( _A , _A ): """simple docstring""" return ((number >> position) & 1) == 1 def __A ( _A , _A ): """simple docstring""" return int((number & (1 << position)) != 0 ) if __name__ == "__main__": import doctest doctest.testmod()
197
import inspect import os import unittest from dataclasses import dataclass import torch from accelerate import Accelerator, DistributedDataParallelKwargs, GradScalerKwargs from accelerate.state import AcceleratorState from accelerate.test_utils import execute_subprocess_async, require_cuda, require_multi_gpu from accelerate.utils import KwargsHandler @dataclass class A__ ( UpperCamelCase__ ): UpperCAmelCase = 0 UpperCAmelCase = False UpperCAmelCase = 3.0 class A__ ( unittest.TestCase ): def __UpperCamelCase ( self : Optional[Any] ) -> Optional[Any]: """simple docstring""" self.assertDictEqual(MockClass().to_kwargs() , {} ) self.assertDictEqual(MockClass(a=2 ).to_kwargs() , {'''a''': 2} ) self.assertDictEqual(MockClass(a=2 , b=_a ).to_kwargs() , {'''a''': 2, '''b''': True} ) self.assertDictEqual(MockClass(a=2 , c=2.25 ).to_kwargs() , {'''a''': 2, '''c''': 2.25} ) @require_cuda def __UpperCamelCase ( self : Optional[Any] ) -> Dict: """simple docstring""" _SCREAMING_SNAKE_CASE =GradScalerKwargs(init_scale=1024 , growth_factor=2 ) AcceleratorState._reset_state() _SCREAMING_SNAKE_CASE =Accelerator(mixed_precision='''fp16''' , kwargs_handlers=[scaler_handler] ) print(accelerator.use_fpaa ) _SCREAMING_SNAKE_CASE =accelerator.scaler # Check the kwargs have been applied self.assertEqual(scaler._init_scale , 10_24.0 ) self.assertEqual(scaler._growth_factor , 2.0 ) # Check the other values are at the default self.assertEqual(scaler._backoff_factor , 0.5 ) self.assertEqual(scaler._growth_interval , 2000 ) self.assertEqual(scaler._enabled , _a ) @require_multi_gpu def __UpperCamelCase ( self : str ) -> Tuple: """simple docstring""" _SCREAMING_SNAKE_CASE =['''torchrun''', f"--nproc_per_node={torch.cuda.device_count()}", inspect.getfile(self.__class__ )] execute_subprocess_async(_a , env=os.environ.copy() ) if __name__ == "__main__": snake_case_ : Optional[Any] = DistributedDataParallelKwargs(bucket_cap_mb=15, find_unused_parameters=True) snake_case_ : List[str] = Accelerator(kwargs_handlers=[ddp_scaler]) snake_case_ : Dict = torch.nn.Linear(1_00, 2_00) snake_case_ : List[Any] = accelerator.prepare(model) # Check the values changed in kwargs snake_case_ : Dict = '''''' snake_case_ : str = model.bucket_bytes_cap // (10_24 * 10_24) if observed_bucket_cap_map != 15: error_msg += f"Kwargs badly passed, should have `15` but found {observed_bucket_cap_map}.\n" if model.find_unused_parameters is not True: error_msg += f"Kwargs badly passed, should have `True` but found {model.find_unused_parameters}.\n" # Check the values of the defaults if model.dim != 0: error_msg += f"Default value not respected, should have `0` but found {model.dim}.\n" if model.broadcast_buffers is not True: error_msg += f"Default value not respected, should have `True` but found {model.broadcast_buffers}.\n" if model.gradient_as_bucket_view is not False: error_msg += f"Default value not respected, should have `False` but found {model.gradient_as_bucket_view}.\n" # Raise error at the end to make sure we don't stop at the first failure. if len(error_msg) > 0: raise ValueError(error_msg)
691
0
def _a ( a :str , a :List[str] ) -> List[Any]: return int((input_a, input_a).count(0 ) == 0 ) def _a ( ) -> Optional[Any]: assert and_gate(0 , 0 ) == 0 assert and_gate(0 , 1 ) == 0 assert and_gate(1 , 0 ) == 0 assert and_gate(1 , 1 ) == 1 if __name__ == "__main__": test_and_gate() print(and_gate(1, 0)) print(and_gate(0, 0)) print(and_gate(0, 1)) print(and_gate(1, 1))
117
class A__ : def __init__( self : List[str] ) -> List[str]: """simple docstring""" _SCREAMING_SNAKE_CASE =0 _SCREAMING_SNAKE_CASE =0 _SCREAMING_SNAKE_CASE ={} def __UpperCamelCase ( self : Any , _a : Union[str, Any] ) -> Optional[Any]: """simple docstring""" if vertex not in self.adjacency: _SCREAMING_SNAKE_CASE ={} self.num_vertices += 1 def __UpperCamelCase ( self : Optional[int] , _a : Tuple , _a : Tuple , _a : Dict ) -> Union[str, Any]: """simple docstring""" self.add_vertex(_a ) self.add_vertex(_a ) if head == tail: return _SCREAMING_SNAKE_CASE =weight _SCREAMING_SNAKE_CASE =weight def __UpperCamelCase ( self : Dict ) -> int: """simple docstring""" _SCREAMING_SNAKE_CASE =self.get_edges() for edge in edges: _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE =edge edges.remove((tail, head, weight) ) for i in range(len(_a ) ): _SCREAMING_SNAKE_CASE =list(edges[i] ) edges.sort(key=lambda _a : e[2] ) for i in range(len(_a ) - 1 ): if edges[i][2] >= edges[i + 1][2]: _SCREAMING_SNAKE_CASE =edges[i][2] + 1 for edge in edges: _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE =edge _SCREAMING_SNAKE_CASE =weight _SCREAMING_SNAKE_CASE =weight def __str__( self : str ) -> List[str]: """simple docstring""" _SCREAMING_SNAKE_CASE ='''''' for tail in self.adjacency: for head in self.adjacency[tail]: _SCREAMING_SNAKE_CASE =self.adjacency[head][tail] string += f"{head} -> {tail} == {weight}\n" return string.rstrip('''\n''' ) def __UpperCamelCase ( self : int ) -> Optional[Any]: """simple docstring""" _SCREAMING_SNAKE_CASE =[] for tail in self.adjacency: for head in self.adjacency[tail]: output.append((tail, head, self.adjacency[head][tail]) ) return output def __UpperCamelCase ( self : Any ) -> Any: """simple docstring""" return self.adjacency.keys() @staticmethod def __UpperCamelCase ( _a : List[str]=None , _a : Optional[int]=None ) -> Optional[Any]: """simple docstring""" _SCREAMING_SNAKE_CASE =Graph() if vertices is None: _SCREAMING_SNAKE_CASE =[] if edges is None: _SCREAMING_SNAKE_CASE =[] for vertex in vertices: g.add_vertex(_a ) for edge in edges: g.add_edge(*_a ) return g class A__ : def __init__( self : List[Any] ) -> Union[str, Any]: """simple docstring""" _SCREAMING_SNAKE_CASE ={} _SCREAMING_SNAKE_CASE ={} def __len__( self : Optional[int] ) -> Tuple: """simple docstring""" return len(self.parent ) def __UpperCamelCase ( self : Dict , _a : Optional[Any] ) -> int: """simple docstring""" if item in self.parent: return self.find(_a ) _SCREAMING_SNAKE_CASE =item _SCREAMING_SNAKE_CASE =0 return item def __UpperCamelCase ( self : str , _a : Tuple ) -> Union[str, Any]: """simple docstring""" if item not in self.parent: return self.make_set(_a ) if item != self.parent[item]: _SCREAMING_SNAKE_CASE =self.find(self.parent[item] ) return self.parent[item] def __UpperCamelCase ( self : Dict , _a : Optional[int] , _a : List[Any] ) -> List[str]: """simple docstring""" _SCREAMING_SNAKE_CASE =self.find(_a ) _SCREAMING_SNAKE_CASE =self.find(_a ) if roota == roota: return roota if self.rank[roota] > self.rank[roota]: _SCREAMING_SNAKE_CASE =roota return roota if self.rank[roota] < self.rank[roota]: _SCREAMING_SNAKE_CASE =roota return roota if self.rank[roota] == self.rank[roota]: self.rank[roota] += 1 _SCREAMING_SNAKE_CASE =roota return roota return None @staticmethod def __UpperCamelCase ( _a : int ) -> Union[str, Any]: """simple docstring""" _SCREAMING_SNAKE_CASE =graph.num_vertices _SCREAMING_SNAKE_CASE =Graph.UnionFind() _SCREAMING_SNAKE_CASE =[] while num_components > 1: _SCREAMING_SNAKE_CASE ={} for vertex in graph.get_vertices(): _SCREAMING_SNAKE_CASE =-1 _SCREAMING_SNAKE_CASE =graph.get_edges() for edge in edges: _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE =edge edges.remove((tail, head, weight) ) for edge in edges: _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE =edge _SCREAMING_SNAKE_CASE =union_find.find(_a ) _SCREAMING_SNAKE_CASE =union_find.find(_a ) if seta != seta: if cheap_edge[seta] == -1 or cheap_edge[seta][2] > weight: _SCREAMING_SNAKE_CASE =[head, tail, weight] if cheap_edge[seta] == -1 or cheap_edge[seta][2] > weight: _SCREAMING_SNAKE_CASE =[head, tail, weight] for vertex in cheap_edge: if cheap_edge[vertex] != -1: _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE =cheap_edge[vertex] if union_find.find(_a ) != union_find.find(_a ): union_find.union(_a , _a ) mst_edges.append(cheap_edge[vertex] ) _SCREAMING_SNAKE_CASE =num_components - 1 _SCREAMING_SNAKE_CASE =Graph.build(edges=_a ) return mst
691
0
import argparse from copy import deepcopy import numpy as np from datasets import ClassLabel, DatasetDict, load_dataset from evaluate import load from transformers import ( AutoModelForSequenceClassification, AutoTokenizer, DataCollatorWithPadding, Trainer, TrainerCallback, TrainingArguments, set_seed, ) def _lowerCAmelCase ( ) -> Optional[int]: '''simple docstring''' __snake_case = argparse.ArgumentParser() parser.add_argument("--model_ckpt" , type=a__ , default="microsoft/unixcoder-base-nine" ) parser.add_argument("--num_epochs" , type=a__ , default=5 ) parser.add_argument("--batch_size" , type=a__ , default=6 ) parser.add_argument("--gradient_accumulation_steps" , type=a__ , default=1 ) parser.add_argument("--freeze" , type=a__ , default=a__ ) parser.add_argument("--learning_rate" , type=a__ , default=5E-4 ) parser.add_argument("--seed" , type=a__ , default=0 ) parser.add_argument("--lr_scheduler_type" , type=a__ , default="cosine" ) parser.add_argument("--num_warmup_steps" , type=a__ , default=10 ) parser.add_argument("--weight_decay" , type=a__ , default=0.01 ) parser.add_argument("--output_dir" , type=a__ , default="./results" ) return parser.parse_args() A : int = load('accuracy') def _lowerCAmelCase ( _lowerCAmelCase ) -> Tuple: '''simple docstring''' __snake_case , __snake_case = eval_pred __snake_case = np.argmax(a__ , axis=1 ) return metric.compute(predictions=a__ , references=a__ ) class UpperCamelCase( UpperCamelCase__ ): def __init__( self : Tuple , SCREAMING_SNAKE_CASE : List[str] ) -> None: '''simple docstring''' super().__init__() __snake_case = trainer def SCREAMING_SNAKE_CASE_ ( self : Optional[Any] , SCREAMING_SNAKE_CASE : Dict , SCREAMING_SNAKE_CASE : int , SCREAMING_SNAKE_CASE : List[Any] , **SCREAMING_SNAKE_CASE : Union[str, Any] ) -> Optional[Any]: '''simple docstring''' if control.should_evaluate: __snake_case = deepcopy(_a ) self._trainer.evaluate(eval_dataset=self._trainer.train_dataset , metric_key_prefix="train" ) return control_copy def _lowerCAmelCase ( ) -> Optional[Any]: '''simple docstring''' __snake_case = get_args() set_seed(args.seed ) __snake_case = load_dataset("codeparrot/codecomplex" , split="train" ) __snake_case = dataset.train_test_split(test_size=0.2 ) __snake_case = train_test["test"].train_test_split(test_size=0.5 ) __snake_case = DatasetDict( { "train": train_test["train"], "test": test_validation["train"], "valid": test_validation["test"], } ) print("Loading tokenizer and model" ) __snake_case = AutoTokenizer.from_pretrained(args.model_ckpt ) __snake_case = tokenizer.eos_token __snake_case = AutoModelForSequenceClassification.from_pretrained(args.model_ckpt , num_labels=7 ) __snake_case = model.config.eos_token_id if args.freeze: for param in model.roberta.parameters(): __snake_case = False __snake_case = ClassLabel(num_classes=7 , names=list(set(train_test_validation["train"]["complexity"] ) ) ) def tokenize(_lowerCAmelCase ): __snake_case = tokenizer(example["src"] , truncation=a__ , max_length=1024 ) __snake_case = labels.straint(example["complexity"] ) return { "input_ids": inputs["input_ids"], "attention_mask": inputs["attention_mask"], "label": label, } __snake_case = train_test_validation.map( a__ , batched=a__ , remove_columns=train_test_validation["train"].column_names , ) __snake_case = DataCollatorWithPadding(tokenizer=a__ ) __snake_case = TrainingArguments( output_dir=args.output_dir , learning_rate=args.learning_rate , lr_scheduler_type=args.lr_scheduler_type , evaluation_strategy="epoch" , save_strategy="epoch" , logging_strategy="epoch" , per_device_train_batch_size=args.batch_size , per_device_eval_batch_size=args.batch_size , num_train_epochs=args.num_epochs , gradient_accumulation_steps=args.gradient_accumulation_steps , weight_decay=0.01 , metric_for_best_model="accuracy" , run_name="complexity-java" , report_to="wandb" , ) __snake_case = Trainer( model=a__ , args=a__ , train_dataset=tokenized_datasets["train"] , eval_dataset=tokenized_datasets["valid"] , tokenizer=a__ , data_collator=a__ , compute_metrics=a__ , ) print("Training..." ) trainer.add_callback(CustomCallback(a__ ) ) trainer.train() if __name__ == "__main__": main()
371
import logging import os from dataclasses import dataclass, field from typing import Dict, Optional import numpy as np from utils_multiple_choice import MultipleChoiceDataset, Split, processors import transformers from transformers import ( AutoConfig, AutoModelForMultipleChoice, AutoTokenizer, DataCollatorWithPadding, EvalPrediction, HfArgumentParser, Trainer, TrainingArguments, set_seed, ) from transformers.trainer_utils import is_main_process snake_case_ : str = logging.getLogger(__name__) def lowerCamelCase( a__ ,a__): return (preds == labels).mean() @dataclass class A__ : UpperCAmelCase = field( metadata={"help": "Path to pretrained model or model identifier from huggingface.co/models"} ) UpperCAmelCase = field( default=UpperCamelCase__ , metadata={"help": "Pretrained config name or path if not the same as model_name"} ) UpperCAmelCase = field( default=UpperCamelCase__ , metadata={"help": "Pretrained tokenizer name or path if not the same as model_name"} ) UpperCAmelCase = field( default=UpperCamelCase__ , metadata={"help": "Where do you want to store the pretrained models downloaded from huggingface.co"} , ) @dataclass class A__ : UpperCAmelCase = field(metadata={"help": "The name of the task to train on: " + ", ".join(processors.keys() )} ) UpperCAmelCase = field(metadata={"help": "Should contain the data files for the task."} ) UpperCAmelCase = 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." ) } , ) UpperCAmelCase = field( default=UpperCamelCase__ , metadata={"help": "Overwrite the cached training and evaluation sets"} ) def lowerCamelCase( ): # See all possible arguments in src/transformers/training_args.py # or by passing the --help flag to this script. # We now keep distinct sets of args, for a cleaner separation of concerns. _SCREAMING_SNAKE_CASE =HfArgumentParser((ModelArguments, DataTrainingArguments, TrainingArguments)) _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE =parser.parse_args_into_dataclasses() if ( os.path.exists(training_args.output_dir) and os.listdir(training_args.output_dir) and training_args.do_train and not training_args.overwrite_output_dir ): raise ValueError( f"Output directory ({training_args.output_dir}) already exists and is not empty. Use" ''' --overwrite_output_dir to overcome.''') # Setup logging logging.basicConfig( format='''%(asctime)s - %(levelname)s - %(name)s - %(message)s''' ,datefmt='''%m/%d/%Y %H:%M:%S''' ,level=logging.INFO if training_args.local_rank in [-1, 0] else logging.WARN ,) logger.warning( '''Process rank: %s, device: %s, n_gpu: %s, distributed training: %s, 16-bits training: %s''' ,training_args.local_rank ,training_args.device ,training_args.n_gpu ,bool(training_args.local_rank != -1) ,training_args.fpaa ,) # Set the verbosity to info of the Transformers logger (on main process only): if is_main_process(training_args.local_rank): transformers.utils.logging.set_verbosity_info() transformers.utils.logging.enable_default_handler() transformers.utils.logging.enable_explicit_format() logger.info('''Training/evaluation parameters %s''' ,a__) # Set seed set_seed(training_args.seed) try: _SCREAMING_SNAKE_CASE =processors[data_args.task_name]() _SCREAMING_SNAKE_CASE =processor.get_labels() _SCREAMING_SNAKE_CASE =len(a__) except KeyError: raise ValueError('''Task not found: %s''' % (data_args.task_name)) # Load pretrained model and tokenizer # # Distributed training: # The .from_pretrained methods guarantee that only one local process can concurrently # download model & vocab. _SCREAMING_SNAKE_CASE =AutoConfig.from_pretrained( model_args.config_name if model_args.config_name else model_args.model_name_or_path ,num_labels=a__ ,finetuning_task=data_args.task_name ,cache_dir=model_args.cache_dir ,) _SCREAMING_SNAKE_CASE =AutoTokenizer.from_pretrained( model_args.tokenizer_name if model_args.tokenizer_name else model_args.model_name_or_path ,cache_dir=model_args.cache_dir ,) _SCREAMING_SNAKE_CASE =AutoModelForMultipleChoice.from_pretrained( model_args.model_name_or_path ,from_tf=bool('''.ckpt''' in model_args.model_name_or_path) ,config=a__ ,cache_dir=model_args.cache_dir ,) # Get datasets _SCREAMING_SNAKE_CASE =( MultipleChoiceDataset( data_dir=data_args.data_dir ,tokenizer=a__ ,task=data_args.task_name ,max_seq_length=data_args.max_seq_length ,overwrite_cache=data_args.overwrite_cache ,mode=Split.train ,) if training_args.do_train else None ) _SCREAMING_SNAKE_CASE =( MultipleChoiceDataset( data_dir=data_args.data_dir ,tokenizer=a__ ,task=data_args.task_name ,max_seq_length=data_args.max_seq_length ,overwrite_cache=data_args.overwrite_cache ,mode=Split.dev ,) if training_args.do_eval else None ) def compute_metrics(a__) -> Dict: _SCREAMING_SNAKE_CASE =np.argmax(p.predictions ,axis=1) return {"acc": simple_accuracy(a__ ,p.label_ids)} # Data collator _SCREAMING_SNAKE_CASE =DataCollatorWithPadding(a__ ,pad_to_multiple_of=8) if training_args.fpaa else None # Initialize our Trainer _SCREAMING_SNAKE_CASE =Trainer( model=a__ ,args=a__ ,train_dataset=a__ ,eval_dataset=a__ ,compute_metrics=a__ ,data_collator=a__ ,) # Training if training_args.do_train: trainer.train( model_path=model_args.model_name_or_path if os.path.isdir(model_args.model_name_or_path) else None) trainer.save_model() # For convenience, we also re-save the tokenizer to the same directory, # so that you can share your model easily on huggingface.co/models =) if trainer.is_world_master(): tokenizer.save_pretrained(training_args.output_dir) # Evaluation _SCREAMING_SNAKE_CASE ={} if training_args.do_eval: logger.info('''*** Evaluate ***''') _SCREAMING_SNAKE_CASE =trainer.evaluate() _SCREAMING_SNAKE_CASE =os.path.join(training_args.output_dir ,'''eval_results.txt''') if trainer.is_world_master(): with open(a__ ,'''w''') as writer: logger.info('''***** Eval results *****''') for key, value in result.items(): logger.info(''' %s = %s''' ,a__ ,a__) writer.write('''%s = %s\n''' % (key, value)) results.update(a__) return results def lowerCamelCase( a__): # For xla_spawn (TPUs) main() if __name__ == "__main__": main()
691
0
'''simple docstring''' from ...utils import ( OptionalDependencyNotAvailable, is_flax_available, is_torch_available, is_transformers_available, ) try: if not (is_transformers_available() and is_torch_available()): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from ...utils.dummy_torch_and_transformers_objects import * # noqa F403 else: from .multicontrolnet import MultiControlNetModel from .pipeline_controlnet import StableDiffusionControlNetPipeline from .pipeline_controlnet_imgaimg import StableDiffusionControlNetImgaImgPipeline from .pipeline_controlnet_inpaint import StableDiffusionControlNetInpaintPipeline if is_transformers_available() and is_flax_available(): from .pipeline_flax_controlnet import FlaxStableDiffusionControlNetPipeline
92
def lowerCamelCase( a__ ,a__ ,a__): if n == 0: return 1 elif n % 2 == 1: return (binary_exponentiation(a__ ,n - 1 ,a__) * a) % mod else: _SCREAMING_SNAKE_CASE =binary_exponentiation(a__ ,n / 2 ,a__) return (b * b) % mod # a prime number snake_case_ : Union[str, Any] = 7_01 snake_case_ : int = 10_00_00_00_00 snake_case_ : str = 10 # using binary exponentiation function, O(log(p)): print((a / b) % p == (a * binary_exponentiation(b, p - 2, p)) % p) print((a / b) % p == (a * b ** (p - 2)) % p)
691
0
'''simple docstring''' import unittest import numpy as np import torch from .utils_summarization import build_mask, compute_token_type_ids, process_story, truncate_or_pad class lowerCamelCase_ ( unittest.TestCase ): def __lowercase ( self : List[str] ): """simple docstring""" SCREAMING_SNAKE_CASE : Tuple = 10 def __lowercase ( self : Any ): """simple docstring""" SCREAMING_SNAKE_CASE : List[Any] = [1, 2, 3, 4] SCREAMING_SNAKE_CASE : Tuple = [1, 2, 3, 4, 0, 0, 0, 0, 0, 0] self.assertEqual(truncate_or_pad(_a , self.block_size , 0 ) , _a ) def __lowercase ( self : Union[str, Any] ): """simple docstring""" SCREAMING_SNAKE_CASE : Optional[int] = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] SCREAMING_SNAKE_CASE : str = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] self.assertEqual(truncate_or_pad(_a , self.block_size , 0 ) , _a ) def __lowercase ( self : str ): """simple docstring""" SCREAMING_SNAKE_CASE : Union[str, Any] = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13] SCREAMING_SNAKE_CASE : int = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] self.assertEqual(truncate_or_pad(_a , self.block_size , 0 ) , _a ) def __lowercase ( self : Optional[Any] ): """simple docstring""" SCREAMING_SNAKE_CASE : Dict = '''It was the year of Our Lord one thousand seven hundred and seventy-five.\n\nSpiritual revelations were conceded to England at that favoured period, as at this.''' SCREAMING_SNAKE_CASE ,SCREAMING_SNAKE_CASE : List[Any] = process_story(_a ) self.assertEqual(_a , [] ) def __lowercase ( self : Dict ): """simple docstring""" SCREAMING_SNAKE_CASE : Any = '''''' SCREAMING_SNAKE_CASE ,SCREAMING_SNAKE_CASE : int = process_story(_a ) self.assertEqual(_a , [] ) self.assertEqual(_a , [] ) def __lowercase ( self : List[str] ): """simple docstring""" SCREAMING_SNAKE_CASE : int = ( '''It was the year of Our Lord one thousand seven hundred and ''' '''seventy-five\n\nSpiritual revelations were conceded to England ''' '''at that favoured period, as at this.\n@highlight\n\nIt was the best of times''' ) SCREAMING_SNAKE_CASE ,SCREAMING_SNAKE_CASE : List[str] = process_story(_a ) SCREAMING_SNAKE_CASE : Any = [ '''It was the year of Our Lord one thousand seven hundred and seventy-five.''', '''Spiritual revelations were conceded to England at that favoured period, as at this.''', ] self.assertEqual(_a , _a ) SCREAMING_SNAKE_CASE : str = ['''It was the best of times.'''] self.assertEqual(_a , _a ) def __lowercase ( self : Tuple ): """simple docstring""" SCREAMING_SNAKE_CASE : Any = torch.tensor([1, 2, 3, 4] ) SCREAMING_SNAKE_CASE : Tuple = torch.tensor([1, 1, 1, 1] ) np.testing.assert_array_equal(build_mask(_a , 0 ).numpy() , expected.numpy() ) def __lowercase ( self : List[str] ): """simple docstring""" SCREAMING_SNAKE_CASE : Tuple = torch.tensor([1, 2, 3, 4, 23, 23, 23] ) SCREAMING_SNAKE_CASE : Union[str, Any] = torch.tensor([1, 1, 1, 1, 0, 0, 0] ) np.testing.assert_array_equal(build_mask(_a , 23 ).numpy() , expected.numpy() ) def __lowercase ( self : Union[str, Any] ): """simple docstring""" SCREAMING_SNAKE_CASE : Union[str, Any] = torch.tensor([8, 2, 3, 4, 1, 1, 1] ) SCREAMING_SNAKE_CASE : Dict = torch.tensor([1, 1, 1, 1, 0, 0, 0] ) np.testing.assert_array_equal(build_mask(_a , 1 ).numpy() , expected.numpy() ) def __lowercase ( self : Any ): """simple docstring""" SCREAMING_SNAKE_CASE : List[Any] = 1_01 SCREAMING_SNAKE_CASE : str = torch.tensor([[1, 2, 3, 4, 5, 6], [1, 2, 3, 1_01, 5, 6], [1, 1_01, 3, 4, 1_01, 6]] ) SCREAMING_SNAKE_CASE : Any = torch.tensor([[1, 1, 1, 1, 1, 1], [1, 1, 1, 0, 0, 0], [1, 0, 0, 0, 1, 1]] ) SCREAMING_SNAKE_CASE : Dict = compute_token_type_ids(_a , _a ) np.testing.assert_array_equal(_a , _a )
527
import inspect import unittest from transformers import BitConfig from transformers.testing_utils import require_torch, require_vision, slow, torch_device from transformers.utils import cached_property, is_torch_available, is_vision_available from ...test_backbone_common import BackboneTesterMixin from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from torch import nn from transformers import BitBackbone, BitForImageClassification, BitImageProcessor, BitModel from transformers.models.bit.modeling_bit import BIT_PRETRAINED_MODEL_ARCHIVE_LIST if is_vision_available(): from PIL import Image class A__ : def __init__( self : Optional[Any] , _a : int , _a : Optional[Any]=3 , _a : Tuple=32 , _a : Any=3 , _a : Union[str, Any]=10 , _a : Optional[int]=[8, 16, 32, 64] , _a : Union[str, Any]=[1, 1, 2, 1] , _a : Optional[Any]=True , _a : int=True , _a : Tuple="relu" , _a : Optional[Any]=3 , _a : str=None , _a : List[Any]=["stage2", "stage3", "stage4"] , _a : Union[str, Any]=[2, 3, 4] , _a : Dict=1 , ) -> Dict: """simple docstring""" _SCREAMING_SNAKE_CASE =parent _SCREAMING_SNAKE_CASE =batch_size _SCREAMING_SNAKE_CASE =image_size _SCREAMING_SNAKE_CASE =num_channels _SCREAMING_SNAKE_CASE =embeddings_size _SCREAMING_SNAKE_CASE =hidden_sizes _SCREAMING_SNAKE_CASE =depths _SCREAMING_SNAKE_CASE =is_training _SCREAMING_SNAKE_CASE =use_labels _SCREAMING_SNAKE_CASE =hidden_act _SCREAMING_SNAKE_CASE =num_labels _SCREAMING_SNAKE_CASE =scope _SCREAMING_SNAKE_CASE =len(_a ) _SCREAMING_SNAKE_CASE =out_features _SCREAMING_SNAKE_CASE =out_indices _SCREAMING_SNAKE_CASE =num_groups def __UpperCamelCase ( self : Optional[Any] ) -> str: """simple docstring""" _SCREAMING_SNAKE_CASE =floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) _SCREAMING_SNAKE_CASE =None if self.use_labels: _SCREAMING_SNAKE_CASE =ids_tensor([self.batch_size] , self.num_labels ) _SCREAMING_SNAKE_CASE =self.get_config() return config, pixel_values, labels def __UpperCamelCase ( self : Any ) -> Union[str, Any]: """simple docstring""" return BitConfig( num_channels=self.num_channels , embeddings_size=self.embeddings_size , hidden_sizes=self.hidden_sizes , depths=self.depths , hidden_act=self.hidden_act , num_labels=self.num_labels , out_features=self.out_features , out_indices=self.out_indices , num_groups=self.num_groups , ) def __UpperCamelCase ( self : Optional[Any] , _a : Dict , _a : str , _a : Dict ) -> List[Any]: """simple docstring""" _SCREAMING_SNAKE_CASE =BitModel(config=_a ) model.to(_a ) model.eval() _SCREAMING_SNAKE_CASE =model(_a ) self.parent.assertEqual( result.last_hidden_state.shape , (self.batch_size, self.hidden_sizes[-1], self.image_size // 32, self.image_size // 32) , ) def __UpperCamelCase ( self : Union[str, Any] , _a : Union[str, Any] , _a : Optional[Any] , _a : Optional[Any] ) -> Union[str, Any]: """simple docstring""" _SCREAMING_SNAKE_CASE =self.num_labels _SCREAMING_SNAKE_CASE =BitForImageClassification(_a ) model.to(_a ) model.eval() _SCREAMING_SNAKE_CASE =model(_a , labels=_a ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_labels) ) def __UpperCamelCase ( self : List[str] , _a : Any , _a : str , _a : List[str] ) -> Optional[int]: """simple docstring""" _SCREAMING_SNAKE_CASE =BitBackbone(config=_a ) model.to(_a ) model.eval() _SCREAMING_SNAKE_CASE =model(_a ) # verify feature maps self.parent.assertEqual(len(result.feature_maps ) , len(config.out_features ) ) self.parent.assertListEqual(list(result.feature_maps[0].shape ) , [self.batch_size, self.hidden_sizes[1], 4, 4] ) # verify channels self.parent.assertEqual(len(model.channels ) , len(config.out_features ) ) self.parent.assertListEqual(model.channels , config.hidden_sizes[1:] ) # verify backbone works with out_features=None _SCREAMING_SNAKE_CASE =None _SCREAMING_SNAKE_CASE =BitBackbone(config=_a ) model.to(_a ) model.eval() _SCREAMING_SNAKE_CASE =model(_a ) # verify feature maps self.parent.assertEqual(len(result.feature_maps ) , 1 ) self.parent.assertListEqual(list(result.feature_maps[0].shape ) , [self.batch_size, self.hidden_sizes[-1], 1, 1] ) # verify channels self.parent.assertEqual(len(model.channels ) , 1 ) self.parent.assertListEqual(model.channels , [config.hidden_sizes[-1]] ) def __UpperCamelCase ( self : Optional[Any] ) -> Dict: """simple docstring""" _SCREAMING_SNAKE_CASE =self.prepare_config_and_inputs() _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE =config_and_inputs _SCREAMING_SNAKE_CASE ={'''pixel_values''': pixel_values} return config, inputs_dict @require_torch class A__ ( UpperCamelCase__ , UpperCamelCase__ , unittest.TestCase ): UpperCAmelCase = (BitModel, BitForImageClassification, BitBackbone) if is_torch_available() else () UpperCAmelCase = ( {"feature-extraction": BitModel, "image-classification": BitForImageClassification} if is_torch_available() else {} ) UpperCAmelCase = False UpperCAmelCase = False UpperCAmelCase = False UpperCAmelCase = False UpperCAmelCase = False def __UpperCamelCase ( self : Union[str, Any] ) -> str: """simple docstring""" _SCREAMING_SNAKE_CASE =BitModelTester(self ) _SCREAMING_SNAKE_CASE =ConfigTester(self , config_class=_a , has_text_modality=_a ) def __UpperCamelCase ( self : Union[str, Any] ) -> int: """simple docstring""" self.create_and_test_config_common_properties() self.config_tester.create_and_test_config_to_json_string() self.config_tester.create_and_test_config_to_json_file() self.config_tester.create_and_test_config_from_and_save_pretrained() self.config_tester.create_and_test_config_with_num_labels() self.config_tester.check_config_can_be_init_without_params() self.config_tester.check_config_arguments_init() def __UpperCamelCase ( self : List[str] ) -> Optional[int]: """simple docstring""" return @unittest.skip(reason='''Bit does not output attentions''' ) def __UpperCamelCase ( self : Optional[int] ) -> List[str]: """simple docstring""" pass @unittest.skip(reason='''Bit does not use inputs_embeds''' ) def __UpperCamelCase ( self : str ) -> Optional[Any]: """simple docstring""" pass @unittest.skip(reason='''Bit does not support input and output embeddings''' ) def __UpperCamelCase ( self : Optional[int] ) -> Union[str, Any]: """simple docstring""" pass def __UpperCamelCase ( self : Tuple ) -> Optional[int]: """simple docstring""" _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE =self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: _SCREAMING_SNAKE_CASE =model_class(_a ) _SCREAMING_SNAKE_CASE =inspect.signature(model.forward ) # signature.parameters is an OrderedDict => so arg_names order is deterministic _SCREAMING_SNAKE_CASE =[*signature.parameters.keys()] _SCREAMING_SNAKE_CASE =['''pixel_values'''] self.assertListEqual(arg_names[:1] , _a ) def __UpperCamelCase ( self : int ) -> Optional[int]: """simple docstring""" _SCREAMING_SNAKE_CASE =self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*_a ) def __UpperCamelCase ( self : Optional[Any] ) -> Dict: """simple docstring""" _SCREAMING_SNAKE_CASE =self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_backbone(*_a ) def __UpperCamelCase ( self : Tuple ) -> Optional[int]: """simple docstring""" _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE =self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: _SCREAMING_SNAKE_CASE =model_class(config=_a ) for name, module in model.named_modules(): if isinstance(_a , (nn.BatchNormad, nn.GroupNorm) ): self.assertTrue( torch.all(module.weight == 1 ) , msg=f"Parameter {name} of model {model_class} seems not properly initialized" , ) self.assertTrue( torch.all(module.bias == 0 ) , msg=f"Parameter {name} of model {model_class} seems not properly initialized" , ) def __UpperCamelCase ( self : Tuple ) -> Optional[int]: """simple docstring""" def check_hidden_states_output(_a : Any , _a : Optional[int] , _a : Tuple ): _SCREAMING_SNAKE_CASE =model_class(_a ) model.to(_a ) model.eval() with torch.no_grad(): _SCREAMING_SNAKE_CASE =model(**self._prepare_for_class(_a , _a ) ) _SCREAMING_SNAKE_CASE =outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states _SCREAMING_SNAKE_CASE =self.model_tester.num_stages self.assertEqual(len(_a ) , expected_num_stages + 1 ) # Bit's feature maps are of shape (batch_size, num_channels, height, width) self.assertListEqual( list(hidden_states[0].shape[-2:] ) , [self.model_tester.image_size // 4, self.model_tester.image_size // 4] , ) _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE =self.model_tester.prepare_config_and_inputs_for_common() _SCREAMING_SNAKE_CASE =['''preactivation''', '''bottleneck'''] for model_class in self.all_model_classes: for layer_type in layers_type: _SCREAMING_SNAKE_CASE =layer_type _SCREAMING_SNAKE_CASE =True check_hidden_states_output(_a , _a , _a ) # check that output_hidden_states also work using config del inputs_dict["output_hidden_states"] _SCREAMING_SNAKE_CASE =True check_hidden_states_output(_a , _a , _a ) @unittest.skip(reason='''Bit does not use feedforward chunking''' ) def __UpperCamelCase ( self : Optional[int] ) -> Dict: """simple docstring""" pass def __UpperCamelCase ( self : Union[str, Any] ) -> Any: """simple docstring""" _SCREAMING_SNAKE_CASE =self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_image_classification(*_a ) @slow def __UpperCamelCase ( self : int ) -> Tuple: """simple docstring""" for model_name in BIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: _SCREAMING_SNAKE_CASE =BitModel.from_pretrained(_a ) self.assertIsNotNone(_a ) def lowerCamelCase( ): _SCREAMING_SNAKE_CASE =Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png''') return image @require_torch @require_vision class A__ ( unittest.TestCase ): @cached_property def __UpperCamelCase ( self : str ) -> Optional[Any]: """simple docstring""" return ( BitImageProcessor.from_pretrained(BIT_PRETRAINED_MODEL_ARCHIVE_LIST[0] ) if is_vision_available() else None ) @slow def __UpperCamelCase ( self : List[Any] ) -> int: """simple docstring""" _SCREAMING_SNAKE_CASE =BitForImageClassification.from_pretrained(BIT_PRETRAINED_MODEL_ARCHIVE_LIST[0] ).to(_a ) _SCREAMING_SNAKE_CASE =self.default_image_processor _SCREAMING_SNAKE_CASE =prepare_img() _SCREAMING_SNAKE_CASE =image_processor(images=_a , return_tensors='''pt''' ).to(_a ) # forward pass with torch.no_grad(): _SCREAMING_SNAKE_CASE =model(**_a ) # verify the logits _SCREAMING_SNAKE_CASE =torch.Size((1, 1000) ) self.assertEqual(outputs.logits.shape , _a ) _SCREAMING_SNAKE_CASE =torch.tensor([[-0.65_26, -0.52_63, -1.43_98]] ).to(_a ) self.assertTrue(torch.allclose(outputs.logits[0, :3] , _a , atol=1E-4 ) ) @require_torch class A__ ( UpperCamelCase__ , unittest.TestCase ): UpperCAmelCase = (BitBackbone,) if is_torch_available() else () UpperCAmelCase = BitConfig UpperCAmelCase = False def __UpperCamelCase ( self : List[str] ) -> Optional[Any]: """simple docstring""" _SCREAMING_SNAKE_CASE =BitModelTester(self )
691
0
import time from dataclasses import dataclass from multiprocessing import Pool from unittest import TestCase from unittest.mock import patch import multiprocess import numpy as np import pytest from datasets.utils.py_utils import ( NestedDataStructure, asdict, iflatmap_unordered, map_nested, temp_seed, temporary_assignment, zip_dict, ) from .utils import require_tf, require_torch def lowerCAmelCase__( lowercase : Dict ) -> str: # picklable for multiprocessing return x.sum() def lowerCAmelCase__( lowercase : Tuple ) -> int: # picklable for multiprocessing return i + 1 @dataclass class _lowerCamelCase : """simple docstring""" UpperCAmelCase_ : Any =42 UpperCAmelCase_ : Dict =42 class _lowerCamelCase ( UpperCamelCase__ ): """simple docstring""" def UpperCAmelCase ( self ) -> str: '''simple docstring''' __snake_case : Any = {} __snake_case : Optional[Any] = [] __snake_case : Optional[Any] = 1 __snake_case : int = [1, 2] __snake_case : Dict = {"a": 1, "b": 2} __snake_case : Tuple = {"a": [1, 2], "b": [3, 4]} __snake_case : Optional[Any] = {"a": {"1": 1}, "b": 2} __snake_case : List[Any] = {"a": 1, "b": 2, "c": 3, "d": 4} __snake_case : List[str] = {} __snake_case : Dict = [] __snake_case : Any = 2 __snake_case : List[Any] = [2, 3] __snake_case : str = {"a": 2, "b": 3} __snake_case : Union[str, Any] = {"a": [2, 3], "b": [4, 5]} __snake_case : Any = {"a": {"1": 2}, "b": 3} __snake_case : Union[str, Any] = {"a": 2, "b": 3, "c": 4, "d": 5} self.assertEqual(map_nested(_a , _a ) , _a ) self.assertEqual(map_nested(_a , _a ) , _a ) self.assertEqual(map_nested(_a , _a ) , _a ) self.assertEqual(map_nested(_a , _a ) , _a ) self.assertEqual(map_nested(_a , _a ) , _a ) self.assertEqual(map_nested(_a , _a ) , _a ) self.assertEqual(map_nested(_a , _a ) , _a ) self.assertEqual(map_nested(_a , _a ) , _a ) __snake_case : Tuple = 2 self.assertEqual(map_nested(_a , _a , num_proc=_a ) , _a ) self.assertEqual(map_nested(_a , _a , num_proc=_a ) , _a ) self.assertEqual(map_nested(_a , _a , num_proc=_a ) , _a ) self.assertEqual(map_nested(_a , _a , num_proc=_a ) , _a ) self.assertEqual(map_nested(_a , _a , num_proc=_a ) , _a ) self.assertEqual(map_nested(_a , _a , num_proc=_a ) , _a ) self.assertEqual(map_nested(_a , _a , num_proc=_a ) , _a ) self.assertEqual(map_nested(_a , _a , num_proc=_a ) , _a ) __snake_case : int = {"a": np.eye(2 ), "b": np.zeros(3 ), "c": np.ones(2 )} __snake_case : Tuple = {"a": 2, "b": 0, "c": 2} __snake_case : str = { "a": np.eye(2 ).astype(_a ), "b": np.zeros(3 ).astype(_a ), "c": np.ones(2 ).astype(_a ), } self.assertEqual(map_nested(_a , _a , map_numpy=_a ) , _a ) self.assertEqual( {k: v.tolist() for k, v in map_nested(_a , _a , map_numpy=_a ).items()} , {k: v.tolist() for k, v in expected_map_nested_sna_int.items()} , ) self.assertEqual(map_nested(_a , _a , map_numpy=_a , num_proc=_a ) , _a ) self.assertEqual( {k: v.tolist() for k, v in map_nested(_a , _a , map_numpy=_a , num_proc=_a ).items()} , {k: v.tolist() for k, v in expected_map_nested_sna_int.items()} , ) with self.assertRaises(_a ): # can't pickle a local lambda map_nested(lambda UpperCAmelCase : x + 1 , _a , num_proc=_a ) def UpperCAmelCase ( self ) -> Optional[Any]: '''simple docstring''' __snake_case : str = {"a": 1, "b": 2} __snake_case : Tuple = {"a": 3, "b": 4} __snake_case : Union[str, Any] = {"a": 5, "b": 6} __snake_case : Any = sorted([("a", (1, 3, 5)), ("b", (2, 4, 6))] ) self.assertEqual(sorted(zip_dict(_a , _a , _a ) ) , _a ) def UpperCAmelCase ( self ) -> List[Any]: '''simple docstring''' class _lowerCamelCase : """simple docstring""" UpperCAmelCase_ : Optional[int] ="bar" __snake_case : Optional[int] = Foo() self.assertEqual(foo.my_attr , "bar" ) with temporary_assignment(_a , "my_attr" , "BAR" ): self.assertEqual(foo.my_attr , "BAR" ) self.assertEqual(foo.my_attr , "bar" ) @pytest.mark.parametrize( "iterable_length, num_proc, expected_num_proc" , [ (1, None, 1), (1, 1, 1), (2, None, 1), (2, 1, 1), (2, 2, 1), (2, 3, 1), (3, 2, 1), (16, 16, 16), (16, 17, 16), (17, 16, 16), ] , ) def lowerCAmelCase__( lowercase : Any , lowercase : Optional[int] , lowercase : Union[str, Any] ) -> int: with patch("datasets.utils.py_utils._single_map_nested" ) as mock_single_map_nested, patch( "datasets.parallel.parallel.Pool" ) as mock_multiprocessing_pool: __snake_case : Optional[Any] = {f"""{i}""": i for i in range(a__ )} __snake_case : Any = map_nested(lambda lowercase : x + 10 , a__ , num_proc=a__ , parallel_min_length=16 ) if expected_num_proc == 1: assert mock_single_map_nested.called assert not mock_multiprocessing_pool.called else: assert not mock_single_map_nested.called assert mock_multiprocessing_pool.called assert mock_multiprocessing_pool.call_args[0][0] == expected_num_proc class _lowerCamelCase ( UpperCamelCase__ ): """simple docstring""" @require_tf def UpperCAmelCase ( self ) -> Optional[int]: '''simple docstring''' import tensorflow as tf from tensorflow.keras import layers __snake_case : int = layers.Dense(2 ) def gen_random_output(): __snake_case : Optional[int] = tf.random.uniform((1, 3) ) return model(_a ).numpy() with temp_seed(42 , set_tensorflow=_a ): __snake_case : Dict = gen_random_output() with temp_seed(42 , set_tensorflow=_a ): __snake_case : List[Any] = gen_random_output() __snake_case : Union[str, Any] = gen_random_output() np.testing.assert_equal(_a , _a ) self.assertGreater(np.abs(outa - outa ).sum() , 0 ) @require_torch def UpperCAmelCase ( self ) -> Dict: '''simple docstring''' import torch def gen_random_output(): __snake_case : List[Any] = torch.nn.Linear(3 , 2 ) __snake_case : Any = torch.rand(1 , 3 ) return model(_a ).detach().numpy() with temp_seed(42 , set_pytorch=_a ): __snake_case : Optional[Any] = gen_random_output() with temp_seed(42 , set_pytorch=_a ): __snake_case : Dict = gen_random_output() __snake_case : Tuple = gen_random_output() np.testing.assert_equal(_a , _a ) self.assertGreater(np.abs(outa - outa ).sum() , 0 ) def UpperCAmelCase ( self ) -> str: '''simple docstring''' def gen_random_output(): return np.random.rand(1 , 3 ) with temp_seed(42 ): __snake_case : List[str] = gen_random_output() with temp_seed(42 ): __snake_case : Any = gen_random_output() __snake_case : Any = gen_random_output() np.testing.assert_equal(_a , _a ) self.assertGreater(np.abs(outa - outa ).sum() , 0 ) @pytest.mark.parametrize("input_data" , [{}] ) def lowerCAmelCase__( lowercase : Optional[Any] ) -> Optional[Any]: __snake_case : Dict = NestedDataStructure(a__ ).data assert output_data == input_data @pytest.mark.parametrize( "data, expected_output" , [ ({}, []), ([], []), ("foo", ["foo"]), (["foo", "bar"], ["foo", "bar"]), ([["foo", "bar"]], ["foo", "bar"]), ([[["foo"], ["bar"]]], ["foo", "bar"]), ([[["foo"], "bar"]], ["foo", "bar"]), ({"a": 1, "b": 2}, [1, 2]), ({"a": [1, 2], "b": [3, 4]}, [1, 2, 3, 4]), ({"a": [[1, 2]], "b": [[3, 4]]}, [1, 2, 3, 4]), ({"a": [[1, 2]], "b": [3, 4]}, [1, 2, 3, 4]), ({"a": [[[1], [2]]], "b": [[[3], [4]]]}, [1, 2, 3, 4]), ({"a": [[[1], [2]]], "b": [[3, 4]]}, [1, 2, 3, 4]), ({"a": [[[1], [2]]], "b": [3, 4]}, [1, 2, 3, 4]), ({"a": [[[1], [2]]], "b": [3, [4]]}, [1, 2, 3, 4]), ({"a": {"1": 1}, "b": 2}, [1, 2]), ({"a": {"1": [1]}, "b": 2}, [1, 2]), ({"a": {"1": [1]}, "b": [2]}, [1, 2]), ] , ) def lowerCAmelCase__( lowercase : Tuple , lowercase : Dict ) -> str: __snake_case : List[str] = NestedDataStructure(a__ ).flatten() assert output == expected_output def lowerCAmelCase__( ) -> Optional[Any]: __snake_case : Dict = A(x=1 , y="foobar" ) __snake_case : int = {"x": 1, "y": "foobar"} assert asdict(a__ ) == expected_output __snake_case : int = {"a": {"b": A(x=10 , y="foo" )}, "c": [A(x=20 , y="bar" )]} __snake_case : List[str] = {"a": {"b": {"x": 10, "y": "foo"}}, "c": [{"x": 20, "y": "bar"}]} assert asdict(a__ ) == expected_output with pytest.raises(a__ ): asdict([1, A(x=10 , y="foo" )] ) def lowerCAmelCase__( lowercase : Tuple ) -> Tuple: return text.split() def lowerCAmelCase__( lowercase : Dict ) -> Tuple: yield (time.time(), content) time.sleep(2 ) yield (time.time(), content) def lowerCAmelCase__( ) -> Any: with Pool(2 ) as pool: __snake_case : Union[str, Any] = list(iflatmap_unordered(a__ , _split_text , kwargs_iterable=[{"text": "hello there"}] * 10 ) ) assert out.count("hello" ) == 10 assert out.count("there" ) == 10 assert len(a__ ) == 20 # check multiprocess from pathos (uses dill for pickling) with multiprocess.Pool(2 ) as pool: __snake_case : Any = list(iflatmap_unordered(a__ , _split_text , kwargs_iterable=[{"text": "hello there"}] * 10 ) ) assert out.count("hello" ) == 10 assert out.count("there" ) == 10 assert len(a__ ) == 20 # check that we get items as fast as possible with Pool(2 ) as pool: __snake_case : str = [] for yield_time, content in iflatmap_unordered( a__ , _aseconds_generator_of_aitems_with_timing , kwargs_iterable=[{"content": "a"}, {"content": "b"}] ): assert yield_time < time.time() + 0.1, "we should each item directly after it was yielded" out.append(a__ ) assert out.count("a" ) == 2 assert out.count("b" ) == 2 assert len(a__ ) == 4
243
import copy from ...configuration_utils import PretrainedConfig from ...utils import add_start_docstrings snake_case_ : Optional[Any] = R''' [`RagConfig`] stores the configuration of a *RagModel*. Configuration objects inherit from [`PretrainedConfig`] and can be used to control the model outputs. Read the documentation from [`PretrainedConfig`] for more information. Args: title_sep (`str`, *optional*, defaults to `" / "`): Separator inserted between the title and the text of the retrieved document when calling [`RagRetriever`]. doc_sep (`str`, *optional*, defaults to `" // "`): Separator inserted between the text of the retrieved document and the original input when calling [`RagRetriever`]. n_docs (`int`, *optional*, defaults to 5): Number of documents to retrieve. max_combined_length (`int`, *optional*, defaults to 300): Max length of contextualized input returned by [`~RagRetriever.__call__`]. retrieval_vector_size (`int`, *optional*, defaults to 768): Dimensionality of the document embeddings indexed by [`RagRetriever`]. retrieval_batch_size (`int`, *optional*, defaults to 8): Retrieval batch size, defined as the number of queries issues concurrently to the faiss index encapsulated [`RagRetriever`]. dataset (`str`, *optional*, defaults to `"wiki_dpr"`): A dataset identifier of the indexed dataset in HuggingFace Datasets (list all available datasets and ids using `datasets.list_datasets()`). dataset_split (`str`, *optional*, defaults to `"train"`) Which split of the `dataset` to load. index_name (`str`, *optional*, defaults to `"compressed"`) The index name of the index associated with the `dataset`. One can choose between `"legacy"`, `"exact"` and `"compressed"`. index_path (`str`, *optional*) The path to the serialized faiss index on disk. passages_path (`str`, *optional*): A path to text passages compatible with the faiss index. Required if using [`~models.rag.retrieval_rag.LegacyIndex`] use_dummy_dataset (`bool`, *optional*, defaults to `False`) Whether to load a "dummy" variant of the dataset specified by `dataset`. label_smoothing (`float`, *optional*, defaults to 0.0): Only relevant if `return_loss` is set to `True`. Controls the `epsilon` parameter value for label smoothing in the loss calculation. If set to 0, no label smoothing is performed. do_marginalize (`bool`, *optional*, defaults to `False`): If `True`, the logits are marginalized over all documents by making use of `torch.nn.functional.log_softmax`. reduce_loss (`bool`, *optional*, defaults to `False`): Whether or not to reduce the NLL loss using the `torch.Tensor.sum` operation. do_deduplication (`bool`, *optional*, defaults to `True`): Whether or not to deduplicate the generations from different context documents for a given input. Has to be set to `False` if used while training with distributed backend. exclude_bos_score (`bool`, *optional*, defaults to `False`): Whether or not to disregard the BOS token when computing the loss. output_retrieved(`bool`, *optional*, defaults to `False`): If set to `True`, `retrieved_doc_embeds`, `retrieved_doc_ids`, `context_input_ids` and `context_attention_mask` are returned. See returned tensors for more detail. use_cache (`bool`, *optional*, defaults to `True`): Whether or not the model should return the last key/values attentions (not used by all models). forced_eos_token_id (`int`, *optional*): The id of the token to force as the last generated token when `max_length` is reached. Usually set to `eos_token_id`. ''' @add_start_docstrings(UpperCamelCase__ ) class A__ ( UpperCamelCase__ ): UpperCAmelCase = "rag" UpperCAmelCase = True def __init__( self : Tuple , _a : List[Any]=None , _a : Tuple=True , _a : Optional[Any]=None , _a : int=None , _a : List[str]=None , _a : int=None , _a : Optional[int]=None , _a : str=" / " , _a : Any=" // " , _a : Optional[Any]=5 , _a : int=300 , _a : Optional[Any]=768 , _a : Any=8 , _a : List[str]="wiki_dpr" , _a : Dict="train" , _a : Union[str, Any]="compressed" , _a : str=None , _a : Union[str, Any]=None , _a : int=False , _a : Any=False , _a : Any=0.0 , _a : Any=True , _a : List[str]=False , _a : Optional[int]=False , _a : int=False , _a : Union[str, Any]=True , _a : Optional[int]=None , **_a : List[str] , ) -> List[Any]: """simple docstring""" super().__init__( bos_token_id=_a , pad_token_id=_a , eos_token_id=_a , decoder_start_token_id=_a , forced_eos_token_id=_a , is_encoder_decoder=_a , prefix=_a , vocab_size=_a , **_a , ) assert ( "question_encoder" in kwargs and "generator" in kwargs ), "Config has to be initialized with question_encoder and generator config" _SCREAMING_SNAKE_CASE =kwargs.pop('''question_encoder''' ) _SCREAMING_SNAKE_CASE =question_encoder_config.pop('''model_type''' ) _SCREAMING_SNAKE_CASE =kwargs.pop('''generator''' ) _SCREAMING_SNAKE_CASE =decoder_config.pop('''model_type''' ) from ..auto.configuration_auto import AutoConfig _SCREAMING_SNAKE_CASE =AutoConfig.for_model(_a , **_a ) _SCREAMING_SNAKE_CASE =AutoConfig.for_model(_a , **_a ) _SCREAMING_SNAKE_CASE =reduce_loss _SCREAMING_SNAKE_CASE =label_smoothing _SCREAMING_SNAKE_CASE =exclude_bos_score _SCREAMING_SNAKE_CASE =do_marginalize _SCREAMING_SNAKE_CASE =title_sep _SCREAMING_SNAKE_CASE =doc_sep _SCREAMING_SNAKE_CASE =n_docs _SCREAMING_SNAKE_CASE =max_combined_length _SCREAMING_SNAKE_CASE =dataset _SCREAMING_SNAKE_CASE =dataset_split _SCREAMING_SNAKE_CASE =index_name _SCREAMING_SNAKE_CASE =retrieval_vector_size _SCREAMING_SNAKE_CASE =retrieval_batch_size _SCREAMING_SNAKE_CASE =passages_path _SCREAMING_SNAKE_CASE =index_path _SCREAMING_SNAKE_CASE =use_dummy_dataset _SCREAMING_SNAKE_CASE =output_retrieved _SCREAMING_SNAKE_CASE =do_deduplication _SCREAMING_SNAKE_CASE =use_cache if self.forced_eos_token_id is None: _SCREAMING_SNAKE_CASE =getattr(self.generator , '''forced_eos_token_id''' , _a ) @classmethod def __UpperCamelCase ( cls : Optional[int] , _a : PretrainedConfig , _a : PretrainedConfig , **_a : Dict ) -> PretrainedConfig: """simple docstring""" return cls(question_encoder=question_encoder_config.to_dict() , generator=generator_config.to_dict() , **_a ) def __UpperCamelCase ( self : Optional[Any] ) -> Any: """simple docstring""" _SCREAMING_SNAKE_CASE =copy.deepcopy(self.__dict__ ) _SCREAMING_SNAKE_CASE =self.question_encoder.to_dict() _SCREAMING_SNAKE_CASE =self.generator.to_dict() _SCREAMING_SNAKE_CASE =self.__class__.model_type return output
691
0
import unittest from transformers.testing_utils import CaptureStdout from transformers.tools.python_interpreter import evaluate def _lowerCamelCase( __snake_case ) -> List[str]: return x + 2 class UpperCamelCase ( unittest.TestCase ): def UpperCamelCase_ ( self : List[Any] ): """simple docstring""" __snake_case = "x = 3" __snake_case = {} __snake_case = evaluate(_a ,{} ,state=_a ) assert result == 3 self.assertDictEqual(_a ,{"x": 3} ) __snake_case = "x = y" __snake_case = {"y": 5} __snake_case = evaluate(_a ,{} ,state=_a ) # evaluate returns the value of the last assignment. assert result == 5 self.assertDictEqual(_a ,{"x": 5, "y": 5} ) def UpperCamelCase_ ( self : str ): """simple docstring""" __snake_case = "y = add_two(x)" __snake_case = {"x": 3} __snake_case = evaluate(_a ,{"add_two": add_two} ,state=_a ) assert result == 5 self.assertDictEqual(_a ,{"x": 3, "y": 5} ) # Won't work without the tool with CaptureStdout() as out: __snake_case = evaluate(_a ,{} ,state=_a ) assert result is None assert "tried to execute add_two" in out.out def UpperCamelCase_ ( self : Optional[int] ): """simple docstring""" __snake_case = "x = 3" __snake_case = {} __snake_case = evaluate(_a ,{} ,state=_a ) assert result == 3 self.assertDictEqual(_a ,{"x": 3} ) def UpperCamelCase_ ( self : Optional[int] ): """simple docstring""" __snake_case = "test_dict = {\'x\': x, \'y\': add_two(x)}" __snake_case = {"x": 3} __snake_case = evaluate(_a ,{"add_two": add_two} ,state=_a ) self.assertDictEqual(_a ,{"x": 3, "y": 5} ) self.assertDictEqual(_a ,{"x": 3, "test_dict": {"x": 3, "y": 5}} ) def UpperCamelCase_ ( self : Union[str, Any] ): """simple docstring""" __snake_case = "x = 3\ny = 5" __snake_case = {} __snake_case = evaluate(_a ,{} ,state=_a ) # evaluate returns the value of the last assignment. assert result == 5 self.assertDictEqual(_a ,{"x": 3, "y": 5} ) def UpperCamelCase_ ( self : Tuple ): """simple docstring""" __snake_case = "text = f\'This is x: {x}.\'" __snake_case = {"x": 3} __snake_case = evaluate(_a ,{} ,state=_a ) # evaluate returns the value of the last assignment. assert result == "This is x: 3." self.assertDictEqual(_a ,{"x": 3, "text": "This is x: 3."} ) def UpperCamelCase_ ( self : str ): """simple docstring""" __snake_case = "if x <= 3:\n y = 2\nelse:\n y = 5" __snake_case = {"x": 3} __snake_case = evaluate(_a ,{} ,state=_a ) # evaluate returns the value of the last assignment. assert result == 2 self.assertDictEqual(_a ,{"x": 3, "y": 2} ) __snake_case = {"x": 8} __snake_case = evaluate(_a ,{} ,state=_a ) # evaluate returns the value of the last assignment. assert result == 5 self.assertDictEqual(_a ,{"x": 8, "y": 5} ) def UpperCamelCase_ ( self : Any ): """simple docstring""" __snake_case = "test_list = [x, add_two(x)]" __snake_case = {"x": 3} __snake_case = evaluate(_a ,{"add_two": add_two} ,state=_a ) self.assertListEqual(_a ,[3, 5] ) self.assertDictEqual(_a ,{"x": 3, "test_list": [3, 5]} ) def UpperCamelCase_ ( self : List[Any] ): """simple docstring""" __snake_case = "y = x" __snake_case = {"x": 3} __snake_case = evaluate(_a ,{} ,state=_a ) assert result == 3 self.assertDictEqual(_a ,{"x": 3, "y": 3} ) def UpperCamelCase_ ( self : List[str] ): """simple docstring""" __snake_case = "test_list = [x, add_two(x)]\ntest_list[1]" __snake_case = {"x": 3} __snake_case = evaluate(_a ,{"add_two": add_two} ,state=_a ) assert result == 5 self.assertDictEqual(_a ,{"x": 3, "test_list": [3, 5]} ) __snake_case = "test_dict = {\'x\': x, \'y\': add_two(x)}\ntest_dict[\'y\']" __snake_case = {"x": 3} __snake_case = evaluate(_a ,{"add_two": add_two} ,state=_a ) assert result == 5 self.assertDictEqual(_a ,{"x": 3, "test_dict": {"x": 3, "y": 5}} ) def UpperCamelCase_ ( self : Union[str, Any] ): """simple docstring""" __snake_case = "x = 0\nfor i in range(3):\n x = i" __snake_case = {} __snake_case = evaluate(_a ,{"range": range} ,state=_a ) assert result == 2 self.assertDictEqual(_a ,{"x": 2, "i": 2} )
524
from manim import * class A__ ( UpperCamelCase__ ): def __UpperCamelCase ( self : Dict ) -> int: """simple docstring""" _SCREAMING_SNAKE_CASE =Rectangle(height=0.5 , width=0.5 ) _SCREAMING_SNAKE_CASE =Rectangle(height=0.25 , width=0.25 ) _SCREAMING_SNAKE_CASE =Rectangle(height=0.46 , width=0.46 ).set_stroke(width=0 ) _SCREAMING_SNAKE_CASE =[mem.copy() for i in range(6 )] _SCREAMING_SNAKE_CASE =[mem.copy() for i in range(6 )] _SCREAMING_SNAKE_CASE =VGroup(*_a ).arrange(_a , buff=0 ) _SCREAMING_SNAKE_CASE =VGroup(*_a ).arrange(_a , buff=0 ) _SCREAMING_SNAKE_CASE =VGroup(_a , _a ).arrange(_a , buff=0 ) _SCREAMING_SNAKE_CASE =Text('''CPU''' , font_size=24 ) _SCREAMING_SNAKE_CASE =Group(_a , _a ).arrange(_a , buff=0.5 , aligned_edge=_a ) cpu.move_to([-2.5, -0.5, 0] ) self.add(_a ) _SCREAMING_SNAKE_CASE =[mem.copy() for i in range(4 )] _SCREAMING_SNAKE_CASE =VGroup(*_a ).arrange(_a , buff=0 ) _SCREAMING_SNAKE_CASE =Text('''GPU''' , font_size=24 ) _SCREAMING_SNAKE_CASE =Group(_a , _a ).arrange(_a , buff=0.5 , aligned_edge=_a ) gpu.move_to([-1, -1, 0] ) self.add(_a ) _SCREAMING_SNAKE_CASE =[mem.copy() for i in range(6 )] _SCREAMING_SNAKE_CASE =VGroup(*_a ).arrange(_a , buff=0 ) _SCREAMING_SNAKE_CASE =Text('''Model''' , font_size=24 ) _SCREAMING_SNAKE_CASE =Group(_a , _a ).arrange(_a , buff=0.5 , aligned_edge=_a ) model.move_to([3, -1.0, 0] ) self.add(_a ) _SCREAMING_SNAKE_CASE =[] _SCREAMING_SNAKE_CASE =[] _SCREAMING_SNAKE_CASE =[] for i, rect in enumerate(_a ): rect.set_stroke(_a ) _SCREAMING_SNAKE_CASE =Rectangle(height=0.46 / 4 , width=0.46 / 3 ).set_stroke(width=0.0 ).set_fill(_a , opacity=0.7 ) if i == 0: cpu_target.next_to(cpu_left_col_base[0].get_corner(DOWN + LEFT ) , buff=0.02 , direction=_a ) cpu_target.set_x(cpu_target.get_x() + 0.1 ) elif i == 3: cpu_target.next_to(model_cpu_arr[0] , direction=_a , buff=0.0 ) else: cpu_target.next_to(model_cpu_arr[i - 1] , direction=_a , buff=0.0 ) self.add(_a ) model_cpu_arr.append(_a ) self.add(*_a , *_a , *_a ) _SCREAMING_SNAKE_CASE =[mem.copy() for i in range(6 )] _SCREAMING_SNAKE_CASE =VGroup(*_a ).arrange(_a , buff=0 ) _SCREAMING_SNAKE_CASE =Text('''Loaded Checkpoint''' , font_size=24 ) _SCREAMING_SNAKE_CASE =Group(_a , _a ).arrange(_a , buff=0.5 , aligned_edge=_a ) checkpoint.move_to([3, 0.5, 0] ) self.add(_a ) _SCREAMING_SNAKE_CASE =[] _SCREAMING_SNAKE_CASE =[] for i, rect in enumerate(_a ): _SCREAMING_SNAKE_CASE =fill.copy().set_fill(_a , opacity=0.7 ) target.move_to(_a ) ckpt_arr.append(_a ) _SCREAMING_SNAKE_CASE =target.copy() if i < 5: cpu_target.move_to(cpu_left_col_base[i + 1] ) else: cpu_target.move_to(cpu_right_col_base[i - 5] ) ckpt_cpu_arr.append(_a ) self.add(*_a , *_a ) _SCREAMING_SNAKE_CASE =Square(side_length=2.2 ) key.move_to([-5, 2, 0] ) _SCREAMING_SNAKE_CASE =MarkupText( f"<b>Key:</b>\n\n<span fgcolor='{YELLOW}'>โ—</span> Empty Model" , font_size=18 , ) key_text.move_to([-5, 2.4, 0] ) self.add(_a , _a ) _SCREAMING_SNAKE_CASE =MarkupText( f"<span fgcolor='{BLUE}'>โ—</span> Checkpoint" , font_size=18 , ) blue_text.next_to(_a , DOWN * 2.4 , aligned_edge=key_text.get_left() ) self.add(_a ) _SCREAMING_SNAKE_CASE =MarkupText( f"Based on the passed in configuration, weights are stored in\na variety of np.memmaps on disk or to a particular device." , font_size=24 , ) step_a.move_to([2, 2, 0] ) _SCREAMING_SNAKE_CASE =[meta_mem.copy() for i in range(6 )] _SCREAMING_SNAKE_CASE =[meta_mem.copy() for i in range(6 )] _SCREAMING_SNAKE_CASE =VGroup(*_a ).arrange(_a , buff=0 ) _SCREAMING_SNAKE_CASE =VGroup(*_a ).arrange(_a , buff=0 ) _SCREAMING_SNAKE_CASE =VGroup(_a , _a ).arrange(_a , buff=0 ) _SCREAMING_SNAKE_CASE =Text('''Disk''' , font_size=24 ) _SCREAMING_SNAKE_CASE =Group(_a , _a ).arrange(_a , buff=0.5 , aligned_edge=_a ) disk.move_to([-4.0, -1.25, 0] ) self.play(Write(_a , run_time=3 ) , Write(_a , run_time=1 ) , Create(_a , run_time=1 ) ) _SCREAMING_SNAKE_CASE =[] for i, rect in enumerate(_a ): _SCREAMING_SNAKE_CASE =rect.copy() target.generate_target() target.target.move_to(disk_left_col_base[i] ).scale(0.5 ) animations.append(MoveToTarget(_a , run_time=1.5 ) ) self.play(*_a ) self.play(FadeOut(_a ) ) _SCREAMING_SNAKE_CASE =MarkupText(f"Then, the checkpoint is removed from memory\nthrough garbage collection." , font_size=24 ) step_a.move_to([2, 2, 0] ) self.play(Write(_a , run_time=3 ) ) self.play( FadeOut(_a , _a , *_a , *_a ) , ) self.wait()
691
0
import os import re import warnings from shutil import copyfile from typing import List, Optional, Tuple from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import is_sentencepiece_available, logging if is_sentencepiece_available(): from .tokenization_ta import TaTokenizer else: _UpperCAmelCase : str = None _UpperCAmelCase : Union[str, Any] = logging.get_logger(__name__) _UpperCAmelCase : List[Any] = {'''vocab_file''': '''spiece.model''', '''tokenizer_file''': '''tokenizer.json'''} _UpperCAmelCase : Optional[int] = { '''vocab_file''': { '''t5-small''': '''https://huggingface.co/t5-small/resolve/main/spiece.model''', '''t5-base''': '''https://huggingface.co/t5-base/resolve/main/spiece.model''', '''t5-large''': '''https://huggingface.co/t5-large/resolve/main/spiece.model''', '''t5-3b''': '''https://huggingface.co/t5-3b/resolve/main/spiece.model''', '''t5-11b''': '''https://huggingface.co/t5-11b/resolve/main/spiece.model''', }, '''tokenizer_file''': { '''t5-small''': '''https://huggingface.co/t5-small/resolve/main/tokenizer.json''', '''t5-base''': '''https://huggingface.co/t5-base/resolve/main/tokenizer.json''', '''t5-large''': '''https://huggingface.co/t5-large/resolve/main/tokenizer.json''', '''t5-3b''': '''https://huggingface.co/t5-3b/resolve/main/tokenizer.json''', '''t5-11b''': '''https://huggingface.co/t5-11b/resolve/main/tokenizer.json''', }, } # TODO(PVP) - this should be removed in Transformers v5 _UpperCAmelCase : Any = { '''t5-small''': 512, '''t5-base''': 512, '''t5-large''': 512, '''t5-3b''': 512, '''t5-11b''': 512, } class lowercase ( UpperCamelCase__ ): __SCREAMING_SNAKE_CASE : Any = VOCAB_FILES_NAMES __SCREAMING_SNAKE_CASE : Union[str, Any] = PRETRAINED_VOCAB_FILES_MAP __SCREAMING_SNAKE_CASE : Optional[Any] = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES __SCREAMING_SNAKE_CASE : Optional[int] = ['''input_ids''', '''attention_mask'''] __SCREAMING_SNAKE_CASE : Optional[int] = TaTokenizer __SCREAMING_SNAKE_CASE : Any = [] def __init__( self , snake_case=None , snake_case=None , snake_case="</s>" , snake_case="<unk>" , snake_case="<pad>" , snake_case=100 , snake_case=None , **snake_case , ): if extra_ids > 0 and additional_special_tokens is None: snake_case_ = [F'''<extra_id_{i}>''' for i in range(_a )] elif extra_ids > 0 and additional_special_tokens is not None: # Check that we have the right number of extra special tokens snake_case_ = len(set(filter(lambda snake_case : bool('extra_id_' in str(_a ) ) , _a ) ) ) if extra_tokens != extra_ids: raise ValueError( F'''Both extra_ids ({extra_ids}) and additional_special_tokens ({additional_special_tokens}) are''' ' provided to T5Tokenizer. In this case the additional_special_tokens must include the extra_ids' ' tokens' ) super().__init__( _a , tokenizer_file=_a , eos_token=_a , unk_token=_a , pad_token=_a , extra_ids=_a , additional_special_tokens=_a , **_a , ) snake_case_ = vocab_file snake_case_ = False if not self.vocab_file else True snake_case_ = extra_ids @staticmethod def a ( snake_case , snake_case , snake_case ): if pretrained_model_name_or_path in TaTokenizerFast.max_model_input_sizes: snake_case_ = TaTokenizerFast.max_model_input_sizes[pretrained_model_name_or_path] if init_max_model_length is not None and init_max_model_length != max_model_length: return init_max_model_length elif init_max_model_length is None: warnings.warn( 'This tokenizer was incorrectly instantiated with a model max length of' F''' {deprecated_max_model_length} which will be corrected in Transformers v5.\nFor now, this''' ' behavior is kept to avoid breaking backwards compatibility when padding/encoding with' ' `truncation is True`.\n- Be aware that you SHOULD NOT rely on' F''' {pretrained_model_name_or_path} automatically truncating your input to''' F''' {deprecated_max_model_length} when padding/encoding.\n- If you want to encode/pad to sequences''' F''' longer than {deprecated_max_model_length} you can either instantiate this tokenizer with''' ' `model_max_length` or pass `max_length` when encoding/padding.\n- To avoid this warning, please' ' instantiate this tokenizer with `model_max_length` set to your preferred value.' , _a , ) return max_model_length def a ( self , snake_case , snake_case = None ): if not self.can_save_slow_tokenizer: raise ValueError( 'Your fast tokenizer does not have the necessary information to save the vocabulary for a slow ' 'tokenizer.' ) if not os.path.isdir(_a ): logger.error(F'''Vocabulary path ({save_directory}) should be a directory''' ) return snake_case_ = os.path.join( _a , (filename_prefix + '-' if filename_prefix else '') + VOCAB_FILES_NAMES['vocab_file'] ) if os.path.abspath(self.vocab_file ) != os.path.abspath(_a ): copyfile(self.vocab_file , _a ) logger.info(F'''Copy vocab file to {out_vocab_file}''' ) return (out_vocab_file,) def a ( self , snake_case , snake_case = None ): snake_case_ = token_ids_a + [self.eos_token_id] if token_ids_a is None: return self.prefix_tokens + token_ids_a else: snake_case_ = token_ids_a + [self.eos_token_id] return self.prefix_tokens + token_ids_a + token_ids_a def a ( self , snake_case , snake_case = None ): snake_case_ = [self.eos_token_id] if token_ids_a is None: return len(token_ids_a + eos ) * [0] return len(token_ids_a + eos + token_ids_a + eos ) * [0] def a ( self ): return list( set(filter(lambda snake_case : bool(re.search(R'<extra_id_\d+>' , _a ) ) is not None , self.additional_special_tokens ) ) ) def a ( self ): return [self.convert_tokens_to_ids(_a ) for token in self.get_sentinel_tokens()]
362
import json from typing import TYPE_CHECKING, List, Optional, Tuple from tokenizers import pre_tokenizers, processors from ...tokenization_utils_base import AddedToken, BatchEncoding from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import logging from .tokenization_blenderbot import BlenderbotTokenizer if TYPE_CHECKING: from transformers.pipelines.conversational import Conversation snake_case_ : str = logging.get_logger(__name__) snake_case_ : List[Any] = { '''vocab_file''': '''vocab.json''', '''merges_file''': '''merges.txt''', '''tokenizer_config_file''': '''tokenizer_config.json''', } snake_case_ : Any = { '''vocab_file''': {'''facebook/blenderbot-3B''': '''https://huggingface.co/facebook/blenderbot-3B/resolve/main/vocab.json'''}, '''merges_file''': {'''facebook/blenderbot-3B''': '''https://huggingface.co/facebook/blenderbot-3B/resolve/main/merges.txt'''}, '''tokenizer_config_file''': { '''facebook/blenderbot-3B''': '''https://huggingface.co/facebook/blenderbot-3B/resolve/main/tokenizer_config.json''' }, } snake_case_ : List[str] = {'''facebook/blenderbot-3B''': 1_28} class A__ ( UpperCamelCase__ ): UpperCAmelCase = VOCAB_FILES_NAMES UpperCAmelCase = PRETRAINED_VOCAB_FILES_MAP UpperCAmelCase = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES UpperCAmelCase = ["input_ids", "attention_mask"] UpperCAmelCase = BlenderbotTokenizer def __init__( self : Dict , _a : str=None , _a : Optional[int]=None , _a : List[str]=None , _a : int="replace" , _a : Dict="<s>" , _a : Optional[Any]="</s>" , _a : Any="</s>" , _a : int="<s>" , _a : int="<unk>" , _a : Optional[int]="<pad>" , _a : Tuple="<mask>" , _a : Tuple=False , _a : Union[str, Any]=True , **_a : List[str] , ) -> Optional[int]: """simple docstring""" super().__init__( _a , _a , tokenizer_file=_a , errors=_a , bos_token=_a , eos_token=_a , sep_token=_a , cls_token=_a , unk_token=_a , pad_token=_a , mask_token=_a , add_prefix_space=_a , trim_offsets=_a , **_a , ) _SCREAMING_SNAKE_CASE =json.loads(self.backend_tokenizer.pre_tokenizer.__getstate__() ) if pre_tok_state.get('''add_prefix_space''' , _a ) != add_prefix_space: _SCREAMING_SNAKE_CASE =getattr(_a , pre_tok_state.pop('''type''' ) ) _SCREAMING_SNAKE_CASE =add_prefix_space _SCREAMING_SNAKE_CASE =pre_tok_class(**_a ) _SCREAMING_SNAKE_CASE =add_prefix_space _SCREAMING_SNAKE_CASE ='''post_processor''' _SCREAMING_SNAKE_CASE =getattr(self.backend_tokenizer , _a , _a ) if tokenizer_component_instance: _SCREAMING_SNAKE_CASE =json.loads(tokenizer_component_instance.__getstate__() ) # The lists 'sep' and 'cls' must be cased in tuples for the object `post_processor_class` if "sep" in state: _SCREAMING_SNAKE_CASE =tuple(state['''sep'''] ) if "cls" in state: _SCREAMING_SNAKE_CASE =tuple(state['''cls'''] ) _SCREAMING_SNAKE_CASE =False if state.get('''add_prefix_space''' , _a ) != add_prefix_space: _SCREAMING_SNAKE_CASE =add_prefix_space _SCREAMING_SNAKE_CASE =True if state.get('''trim_offsets''' , _a ) != trim_offsets: _SCREAMING_SNAKE_CASE =trim_offsets _SCREAMING_SNAKE_CASE =True if changes_to_apply: _SCREAMING_SNAKE_CASE =getattr(_a , state.pop('''type''' ) ) _SCREAMING_SNAKE_CASE =component_class(**_a ) setattr(self.backend_tokenizer , _a , _a ) @property # Copied from transformers.models.roberta.tokenization_roberta_fast.RobertaTokenizerFast.mask_token with Roberta->Blenderbot, RoBERTa->Blenderbot def __UpperCamelCase ( self : Tuple ) -> str: """simple docstring""" if self._mask_token is None: if self.verbose: logger.error('''Using mask_token, but it is not set yet.''' ) return None return str(self._mask_token ) @mask_token.setter def __UpperCamelCase ( self : Optional[Any] , _a : str ) -> List[str]: """simple docstring""" _SCREAMING_SNAKE_CASE =AddedToken(_a , lstrip=_a , rstrip=_a ) if isinstance(_a , _a ) else value _SCREAMING_SNAKE_CASE =value def __UpperCamelCase ( self : Optional[Any] , *_a : str , **_a : int ) -> BatchEncoding: """simple docstring""" _SCREAMING_SNAKE_CASE =kwargs.get('''is_split_into_words''' , _a ) assert self.add_prefix_space or not is_split_into_words, ( f"You need to instantiate {self.__class__.__name__} with add_prefix_space=True " "to use it with pretokenized inputs." ) return super()._batch_encode_plus(*_a , **_a ) def __UpperCamelCase ( self : List[Any] , *_a : Optional[int] , **_a : Union[str, Any] ) -> BatchEncoding: """simple docstring""" _SCREAMING_SNAKE_CASE =kwargs.get('''is_split_into_words''' , _a ) assert self.add_prefix_space or not is_split_into_words, ( f"You need to instantiate {self.__class__.__name__} with add_prefix_space=True " "to use it with pretokenized inputs." ) return super()._encode_plus(*_a , **_a ) def __UpperCamelCase ( self : Dict , _a : str , _a : Optional[str] = None ) -> Tuple[str]: """simple docstring""" _SCREAMING_SNAKE_CASE =self._tokenizer.model.save(_a , name=_a ) return tuple(_a ) def __UpperCamelCase ( self : Tuple , _a : List[int] , _a : Optional[List[int]] = None ) -> List[int]: """simple docstring""" _SCREAMING_SNAKE_CASE =[self.sep_token_id] _SCREAMING_SNAKE_CASE =[self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep ) * [0] return len(cls + token_ids_a + sep + sep + token_ids_a + sep ) * [0] def __UpperCamelCase ( self : Tuple , _a : List[int] , _a : Optional[List[int]] = None ) -> Optional[Any]: """simple docstring""" return token_ids_a + [self.eos_token_id] def __UpperCamelCase ( self : Any , _a : "Conversation" ) -> List[int]: """simple docstring""" _SCREAMING_SNAKE_CASE =[] for is_user, text in conversation.iter_texts(): if is_user: # We need to space prefix as it's being done within blenderbot inputs.append(''' ''' + text ) else: # Generated responses should contain them already. inputs.append(_a ) _SCREAMING_SNAKE_CASE =''' '''.join(_a ) _SCREAMING_SNAKE_CASE =self.encode(_a ) if len(_a ) > self.model_max_length: _SCREAMING_SNAKE_CASE =input_ids[-self.model_max_length :] logger.warning(f"Trimmed input from conversation as it was longer than {self.model_max_length} tokens." ) return input_ids
691
0
"""simple docstring""" from __future__ import annotations class lowerCamelCase : '''simple docstring''' def __init__(self , _lowerCamelCase=None ): """simple docstring""" UpperCAmelCase__ : Any = data UpperCAmelCase__ : List[str] = None def __repr__(self ): """simple docstring""" UpperCAmelCase__ : Any = [] UpperCAmelCase__ : int = self while temp: string_rep.append(F"""{temp.data}""" ) UpperCAmelCase__ : List[Any] = temp.next return "->".join(_a ) def a__ ( lowerCAmelCase ) -> Union[str, Any]: if not elements_list: raise Exception("""The Elements List is empty""" ) UpperCAmelCase__ : Any = Node(elements_list[0] ) for i in range(1 , len(a__ ) ): UpperCAmelCase__ : Tuple = Node(elements_list[i] ) UpperCAmelCase__ : Any = current.next return head def a__ ( lowerCAmelCase ) -> str: if head_node is not None and isinstance(a__ , a__ ): print_reverse(head_node.next ) print(head_node.data ) def a__ ( ) -> str: from doctest import testmod testmod() UpperCAmelCase__ : Any = make_linked_list([14, 52, 14, 12, 43] ) print("""Linked List:""" ) print(a__ ) print("""Elements in Reverse:""" ) print_reverse(a__ ) if __name__ == "__main__": main()
182
import json import os import shutil import tempfile import unittest import numpy as np import pytest from transformers import BertTokenizer, BertTokenizerFast from transformers.models.bert.tokenization_bert import VOCAB_FILES_NAMES from transformers.testing_utils import require_vision from transformers.utils import FEATURE_EXTRACTOR_NAME, is_vision_available if is_vision_available(): from PIL import Image from transformers import ChineseCLIPImageProcessor, ChineseCLIPProcessor @require_vision class A__ ( unittest.TestCase ): def __UpperCamelCase ( self : int ) -> Any: """simple docstring""" _SCREAMING_SNAKE_CASE =tempfile.mkdtemp() _SCREAMING_SNAKE_CASE =[ '''[UNK]''', '''[CLS]''', '''[SEP]''', '''[PAD]''', '''[MASK]''', '''็š„''', '''ไปท''', '''ๆ ผ''', '''ๆ˜ฏ''', '''15''', '''ไพฟ''', '''alex''', '''##andra''', '''๏ผŒ''', '''ใ€‚''', '''-''', '''t''', '''shirt''', ] _SCREAMING_SNAKE_CASE =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] ) ) _SCREAMING_SNAKE_CASE ={ '''do_resize''': True, '''size''': {'''height''': 224, '''width''': 224}, '''do_center_crop''': True, '''crop_size''': {'''height''': 18, '''width''': 18}, '''do_normalize''': True, '''image_mean''': [0.48_14_54_66, 0.4_57_82_75, 0.40_82_10_73], '''image_std''': [0.26_86_29_54, 0.26_13_02_58, 0.27_57_77_11], '''do_convert_rgb''': True, } _SCREAMING_SNAKE_CASE =os.path.join(self.tmpdirname , _a ) with open(self.image_processor_file , '''w''' , encoding='''utf-8''' ) as fp: json.dump(_a , _a ) def __UpperCamelCase ( self : Optional[int] , **_a : str ) -> List[str]: """simple docstring""" return BertTokenizer.from_pretrained(self.tmpdirname , **_a ) def __UpperCamelCase ( self : List[Any] , **_a : Any ) -> Dict: """simple docstring""" return BertTokenizerFast.from_pretrained(self.tmpdirname , **_a ) def __UpperCamelCase ( self : int , **_a : Optional[Any] ) -> Any: """simple docstring""" return ChineseCLIPImageProcessor.from_pretrained(self.tmpdirname , **_a ) def __UpperCamelCase ( self : str ) -> Union[str, Any]: """simple docstring""" shutil.rmtree(self.tmpdirname ) def __UpperCamelCase ( self : int ) -> Optional[Any]: """simple docstring""" _SCREAMING_SNAKE_CASE =[np.random.randint(255 , size=(3, 30, 400) , dtype=np.uinta )] _SCREAMING_SNAKE_CASE =[Image.fromarray(np.moveaxis(_a , 0 , -1 ) ) for x in image_inputs] return image_inputs def __UpperCamelCase ( self : Any ) -> List[Any]: """simple docstring""" _SCREAMING_SNAKE_CASE =self.get_tokenizer() _SCREAMING_SNAKE_CASE =self.get_rust_tokenizer() _SCREAMING_SNAKE_CASE =self.get_image_processor() _SCREAMING_SNAKE_CASE =ChineseCLIPProcessor(tokenizer=_a , image_processor=_a ) processor_slow.save_pretrained(self.tmpdirname ) _SCREAMING_SNAKE_CASE =ChineseCLIPProcessor.from_pretrained(self.tmpdirname , use_fast=_a ) _SCREAMING_SNAKE_CASE =ChineseCLIPProcessor(tokenizer=_a , image_processor=_a ) processor_fast.save_pretrained(self.tmpdirname ) _SCREAMING_SNAKE_CASE =ChineseCLIPProcessor.from_pretrained(self.tmpdirname ) self.assertEqual(processor_slow.tokenizer.get_vocab() , tokenizer_slow.get_vocab() ) self.assertEqual(processor_fast.tokenizer.get_vocab() , tokenizer_fast.get_vocab() ) self.assertEqual(tokenizer_slow.get_vocab() , tokenizer_fast.get_vocab() ) self.assertIsInstance(processor_slow.tokenizer , _a ) self.assertIsInstance(processor_fast.tokenizer , _a ) self.assertEqual(processor_slow.image_processor.to_json_string() , image_processor.to_json_string() ) self.assertEqual(processor_fast.image_processor.to_json_string() , image_processor.to_json_string() ) self.assertIsInstance(processor_slow.image_processor , _a ) self.assertIsInstance(processor_fast.image_processor , _a ) def __UpperCamelCase ( self : str ) -> Union[str, Any]: """simple docstring""" _SCREAMING_SNAKE_CASE =ChineseCLIPProcessor(tokenizer=self.get_tokenizer() , image_processor=self.get_image_processor() ) processor.save_pretrained(self.tmpdirname ) _SCREAMING_SNAKE_CASE =self.get_tokenizer(cls_token='''(CLS)''' , sep_token='''(SEP)''' ) _SCREAMING_SNAKE_CASE =self.get_image_processor(do_normalize=_a ) _SCREAMING_SNAKE_CASE =ChineseCLIPProcessor.from_pretrained( self.tmpdirname , cls_token='''(CLS)''' , sep_token='''(SEP)''' , do_normalize=_a ) self.assertEqual(processor.tokenizer.get_vocab() , tokenizer_add_kwargs.get_vocab() ) self.assertIsInstance(processor.tokenizer , _a ) self.assertEqual(processor.image_processor.to_json_string() , image_processor_add_kwargs.to_json_string() ) self.assertIsInstance(processor.image_processor , _a ) def __UpperCamelCase ( self : List[Any] ) -> Tuple: """simple docstring""" _SCREAMING_SNAKE_CASE =self.get_image_processor() _SCREAMING_SNAKE_CASE =self.get_tokenizer() _SCREAMING_SNAKE_CASE =ChineseCLIPProcessor(tokenizer=_a , image_processor=_a ) _SCREAMING_SNAKE_CASE =self.prepare_image_inputs() _SCREAMING_SNAKE_CASE =image_processor(_a , return_tensors='''np''' ) _SCREAMING_SNAKE_CASE =processor(images=_a , return_tensors='''np''' ) for key in input_feat_extract.keys(): self.assertAlmostEqual(input_feat_extract[key].sum() , input_processor[key].sum() , delta=1E-2 ) def __UpperCamelCase ( self : Union[str, Any] ) -> Any: """simple docstring""" _SCREAMING_SNAKE_CASE =self.get_image_processor() _SCREAMING_SNAKE_CASE =self.get_tokenizer() _SCREAMING_SNAKE_CASE =ChineseCLIPProcessor(tokenizer=_a , image_processor=_a ) _SCREAMING_SNAKE_CASE ='''Alexandra๏ผŒT-shirt็š„ไปทๆ ผๆ˜ฏ15ไพฟๅฃซใ€‚''' _SCREAMING_SNAKE_CASE =processor(text=_a ) _SCREAMING_SNAKE_CASE =tokenizer(_a ) for key in encoded_tok.keys(): self.assertListEqual(encoded_tok[key] , encoded_processor[key] ) def __UpperCamelCase ( self : Tuple ) -> int: """simple docstring""" _SCREAMING_SNAKE_CASE =self.get_image_processor() _SCREAMING_SNAKE_CASE =self.get_tokenizer() _SCREAMING_SNAKE_CASE =ChineseCLIPProcessor(tokenizer=_a , image_processor=_a ) _SCREAMING_SNAKE_CASE ='''Alexandra๏ผŒT-shirt็š„ไปทๆ ผๆ˜ฏ15ไพฟๅฃซใ€‚''' _SCREAMING_SNAKE_CASE =self.prepare_image_inputs() _SCREAMING_SNAKE_CASE =processor(text=_a , images=_a ) self.assertListEqual(list(inputs.keys() ) , ['''input_ids''', '''token_type_ids''', '''attention_mask''', '''pixel_values'''] ) # test if it raises when no input is passed with pytest.raises(_a ): processor() def __UpperCamelCase ( self : Optional[Any] ) -> Optional[int]: """simple docstring""" _SCREAMING_SNAKE_CASE =self.get_image_processor() _SCREAMING_SNAKE_CASE =self.get_tokenizer() _SCREAMING_SNAKE_CASE =ChineseCLIPProcessor(tokenizer=_a , image_processor=_a ) _SCREAMING_SNAKE_CASE =[[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]] _SCREAMING_SNAKE_CASE =processor.batch_decode(_a ) _SCREAMING_SNAKE_CASE =tokenizer.batch_decode(_a ) self.assertListEqual(_a , _a ) def __UpperCamelCase ( self : Any ) -> List[str]: """simple docstring""" _SCREAMING_SNAKE_CASE =self.get_image_processor() _SCREAMING_SNAKE_CASE =self.get_tokenizer() _SCREAMING_SNAKE_CASE =ChineseCLIPProcessor(tokenizer=_a , image_processor=_a ) _SCREAMING_SNAKE_CASE ='''Alexandra๏ผŒT-shirt็š„ไปทๆ ผๆ˜ฏ15ไพฟๅฃซใ€‚''' _SCREAMING_SNAKE_CASE =self.prepare_image_inputs() _SCREAMING_SNAKE_CASE =processor(text=_a , images=_a ) self.assertListEqual(list(inputs.keys() ) , processor.model_input_names )
691
0
"""simple docstring""" from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tokenizers_available, is_torch_available UpperCAmelCase : List[str] = { '''configuration_bloom''': ['''BLOOM_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''BloomConfig''', '''BloomOnnxConfig'''], } try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCAmelCase : Union[str, Any] = ['''BloomTokenizerFast'''] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCAmelCase : Optional[Any] = [ '''BLOOM_PRETRAINED_MODEL_ARCHIVE_LIST''', '''BloomForCausalLM''', '''BloomModel''', '''BloomPreTrainedModel''', '''BloomForSequenceClassification''', '''BloomForTokenClassification''', '''BloomForQuestionAnswering''', ] if TYPE_CHECKING: from .configuration_bloom import BLOOM_PRETRAINED_CONFIG_ARCHIVE_MAP, BloomConfig, BloomOnnxConfig try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_bloom_fast import BloomTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_bloom import ( BLOOM_PRETRAINED_MODEL_ARCHIVE_LIST, BloomForCausalLM, BloomForQuestionAnswering, BloomForSequenceClassification, BloomForTokenClassification, BloomModel, BloomPreTrainedModel, ) else: import sys UpperCAmelCase : Optional[int] = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
139
# Usage: # ./gen-card-allenai-wmt16.py import os from pathlib import Path def lowerCamelCase( a__ ,a__ ,a__ ,a__): _SCREAMING_SNAKE_CASE ={ '''en''': '''Machine learning is great, isn\'t it?''', '''ru''': '''ะœะฐัˆะธะฝะฝะพะต ะพะฑัƒั‡ะตะฝะธะต - ัั‚ะพ ะทะดะพั€ะพะฒะพ, ะฝะต ั‚ะฐะบ ะปะธ?''', '''de''': '''Maschinelles Lernen ist groรŸartig, nicht wahr?''', } # BLUE scores as follows: # "pair": [fairseq, transformers] _SCREAMING_SNAKE_CASE ={ '''wmt16-en-de-dist-12-1''': [28.3, 27.52], '''wmt16-en-de-dist-6-1''': [27.4, 27.11], '''wmt16-en-de-12-1''': [26.9, 25.75], } _SCREAMING_SNAKE_CASE =f"{src_lang}-{tgt_lang}" _SCREAMING_SNAKE_CASE =f"\n---\nlanguage:\n- {src_lang}\n- {tgt_lang}\nthumbnail:\ntags:\n- translation\n- wmt16\n- allenai\nlicense: apache-2.0\ndatasets:\n- wmt16\nmetrics:\n- bleu\n---\n\n# FSMT\n\n## Model description\n\nThis is a ported version of fairseq-based [wmt16 transformer](https://github.com/jungokasai/deep-shallow/) for {src_lang}-{tgt_lang}.\n\nFor more details, please, see [Deep Encoder, Shallow Decoder: Reevaluating the Speed-Quality Tradeoff in Machine Translation](https://arxiv.org/abs/2006.10369).\n\nAll 3 models are available:\n\n* [wmt16-en-de-dist-12-1](https://huggingface.co/allenai/wmt16-en-de-dist-12-1)\n* [wmt16-en-de-dist-6-1](https://huggingface.co/allenai/wmt16-en-de-dist-6-1)\n* [wmt16-en-de-12-1](https://huggingface.co/allenai/wmt16-en-de-12-1)\n\n\n## Intended uses & limitations\n\n#### How to use\n\n```python\nfrom transformers import FSMTForConditionalGeneration, FSMTTokenizer\nmname = \"allenai/{model_name}\"\ntokenizer = FSMTTokenizer.from_pretrained(mname)\nmodel = FSMTForConditionalGeneration.from_pretrained(mname)\n\ninput = \"{texts[src_lang]}\"\ninput_ids = tokenizer.encode(input, return_tensors=\"pt\")\noutputs = model.generate(input_ids)\ndecoded = tokenizer.decode(outputs[0], skip_special_tokens=True)\nprint(decoded) # {texts[tgt_lang]}\n\n```\n\n#### Limitations and bias\n\n\n## Training data\n\nPretrained weights were left identical to the original model released by allenai. For more details, please, see the [paper](https://arxiv.org/abs/2006.10369).\n\n## Eval results\n\nHere are the BLEU scores:\n\nmodel | fairseq | transformers\n-------|---------|----------\n{model_name} | {scores[model_name][0]} | {scores[model_name][1]}\n\nThe score is slightly below the score reported in the paper, as the researchers don't use `sacrebleu` and measure the score on tokenized outputs. `transformers` score was measured using `sacrebleu` on detokenized outputs.\n\nThe score was calculated using this code:\n\n```bash\ngit clone https://github.com/huggingface/transformers\ncd transformers\nexport PAIR={pair}\nexport DATA_DIR=data/$PAIR\nexport SAVE_DIR=data/$PAIR\nexport BS=8\nexport NUM_BEAMS=5\nmkdir -p $DATA_DIR\nsacrebleu -t wmt16 -l $PAIR --echo src > $DATA_DIR/val.source\nsacrebleu -t wmt16 -l $PAIR --echo ref > $DATA_DIR/val.target\necho $PAIR\nPYTHONPATH=\"src:examples/seq2seq\" python examples/seq2seq/run_eval.py allenai/{model_name} $DATA_DIR/val.source $SAVE_DIR/test_translations.txt --reference_path $DATA_DIR/val.target --score_path $SAVE_DIR/test_bleu.json --bs $BS --task translation --num_beams $NUM_BEAMS\n```\n\n## Data Sources\n\n- [training, etc.](http://www.statmt.org/wmt16/)\n- [test set](http://matrix.statmt.org/test_sets/newstest2016.tgz?1504722372)\n\n\n### BibTeX entry and citation info\n\n```\n@misc{{kasai2020deep,\n title={{Deep Encoder, Shallow Decoder: Reevaluating the Speed-Quality Tradeoff in Machine Translation}},\n author={{Jungo Kasai and Nikolaos Pappas and Hao Peng and James Cross and Noah A. Smith}},\n year={{2020}},\n eprint={{2006.10369}},\n archivePrefix={{arXiv}},\n primaryClass={{cs.CL}}\n}}\n```\n\n" model_card_dir.mkdir(parents=a__ ,exist_ok=a__) _SCREAMING_SNAKE_CASE =os.path.join(a__ ,'''README.md''') print(f"Generating {path}") with open(a__ ,'''w''' ,encoding='''utf-8''') as f: f.write(a__) # make sure we are under the root of the project snake_case_ : Any = Path(__file__).resolve().parent.parent.parent snake_case_ : Tuple = repo_dir / '''model_cards''' for model_name in ["wmt16-en-de-dist-12-1", "wmt16-en-de-dist-6-1", "wmt16-en-de-12-1"]: snake_case_ : Union[str, Any] = model_cards_dir / '''allenai''' / model_name write_model_card(model_card_dir, src_lang='''en''', tgt_lang='''de''', model_name=model_name)
691
0
import argparse import torch from torch import nn from transformers import SpeechaTextConfig, SpeechaTextForConditionalGeneration def _UpperCAmelCase ( UpperCAmelCase : Dict ): """simple docstring""" __lowerCamelCase : Union[str, Any] = [ """encoder.version""", """decoder.version""", """model.encoder.version""", """model.decoder.version""", """decoder.output_projection.weight""", """_float_tensor""", """encoder.embed_positions._float_tensor""", """decoder.embed_positions._float_tensor""", ] for k in ignore_keys: state_dict.pop(a__ , a__ ) def _UpperCAmelCase ( UpperCAmelCase : Any ): """simple docstring""" __lowerCamelCase : Optional[Any] = list(s_dict.keys() ) for key in keys: if "transformer_layers" in key: __lowerCamelCase : List[str] = s_dict.pop(a__ ) elif "subsample" in key: __lowerCamelCase : int = s_dict.pop(a__ ) def _UpperCAmelCase ( UpperCAmelCase : Union[str, Any] ): """simple docstring""" __lowerCamelCase , __lowerCamelCase : str = emb.weight.shape __lowerCamelCase : int = nn.Linear(a__ , a__ , bias=a__ ) __lowerCamelCase : int = emb.weight.data return lin_layer def _UpperCAmelCase ( UpperCAmelCase : Tuple , UpperCAmelCase : int ): """simple docstring""" __lowerCamelCase : str = torch.load(a__ , map_location="""cpu""" ) __lowerCamelCase : Optional[Any] = mam_aaa["""args"""] __lowerCamelCase : List[str] = mam_aaa["""model"""] __lowerCamelCase : Any = state_dict["""decoder.output_projection.weight"""] remove_ignore_keys_(a__ ) rename_keys(a__ ) __lowerCamelCase : Optional[Any] = state_dict["""decoder.embed_tokens.weight"""].shape[0] __lowerCamelCase : Optional[Any] = args.share_decoder_input_output_embed __lowerCamelCase : Tuple = [int(a__ ) for i in args.conv_kernel_sizes.split(""",""" )] __lowerCamelCase : Tuple = SpeechaTextConfig( vocab_size=a__ , max_source_positions=args.max_source_positions , max_target_positions=args.max_target_positions , encoder_layers=args.encoder_layers , decoder_layers=args.decoder_layers , encoder_attention_heads=args.encoder_attention_heads , decoder_attention_heads=args.decoder_attention_heads , encoder_ffn_dim=args.encoder_ffn_embed_dim , decoder_ffn_dim=args.decoder_ffn_embed_dim , d_model=args.encoder_embed_dim , dropout=args.dropout , attention_dropout=args.attention_dropout , activation_dropout=args.activation_dropout , activation_function="""relu""" , num_conv_layers=len(a__ ) , conv_channels=args.conv_channels , conv_kernel_sizes=a__ , input_feat_per_channel=args.input_feat_per_channel , input_channels=args.input_channels , tie_word_embeddings=a__ , num_beams=5 , max_length=200 , use_cache=a__ , decoder_start_token_id=2 , early_stopping=a__ , ) __lowerCamelCase : int = SpeechaTextForConditionalGeneration(a__ ) __lowerCamelCase , __lowerCamelCase : Any = model.model.load_state_dict(a__ , strict=a__ ) if len(a__ ) > 0 and not set(a__ ) <= { "encoder.embed_positions.weights", "decoder.embed_positions.weights", }: raise ValueError( """Only `encoder.embed_positions.weights` and `decoder.embed_positions.weights` are allowed to be missing,""" f""" but all the following weights are missing {missing}""" ) if tie_embeds: __lowerCamelCase : Tuple = make_linear_from_emb(model.model.decoder.embed_tokens ) else: __lowerCamelCase : str = lm_head_weights model.save_pretrained(a__ ) if __name__ == "__main__": __UpperCamelCase : List[str] = argparse.ArgumentParser() # Required parameters parser.add_argument('--fairseq_path', type=str, help='Path to the fairseq model (.pt) file.') parser.add_argument('--pytorch_dump_folder_path', default=None, type=str, help='Path to the output PyTorch model.') __UpperCamelCase : Any = parser.parse_args() convert_fairseq_sat_checkpoint_to_tfms(args.fairseq_path, args.pytorch_dump_folder_path)
519
from typing import TYPE_CHECKING from ....utils import _LazyModule snake_case_ : Dict = {'''tokenization_tapex''': ['''TapexTokenizer''']} if TYPE_CHECKING: from .tokenization_tapex import TapexTokenizer else: import sys snake_case_ : Union[str, Any] = _LazyModule(__name__, globals()['''__file__'''], _import_structure)
691
0
import math import qiskit def __A ( _A = 1 , _A = 1 , _A = 1 ): """simple docstring""" if ( isinstance(a__ , a__ ) or isinstance(a__ , a__ ) or isinstance(a__ , a__ ) ): raise TypeError("inputs must be integers." ) if (input_a < 0) or (input_a < 0) or (carry_in < 0): raise ValueError("inputs must be positive." ) if ( (math.floor(a__ ) != input_a) or (math.floor(a__ ) != input_a) or (math.floor(a__ ) != carry_in) ): raise ValueError("inputs must be exact integers." ) if (input_a > 2) or (input_a > 2) or (carry_in > 2): raise ValueError("inputs must be less or equal to 2." ) # build registers __a = qiskit.QuantumRegister(4 , "qr" ) __a = qiskit.ClassicalRegister(2 , "cr" ) # list the entries __a = [input_a, input_a, carry_in] __a = qiskit.QuantumCircuit(a__ , a__ ) for i in range(0 , 3 ): if entry[i] == 2: quantum_circuit.h(a__ ) # for hadamard entries elif entry[i] == 1: quantum_circuit.x(a__ ) # for 1 entries elif entry[i] == 0: quantum_circuit.i(a__ ) # for 0 entries # build the circuit quantum_circuit.ccx(0 , 1 , 3 ) # ccx = toffoli gate quantum_circuit.cx(0 , 1 ) quantum_circuit.ccx(1 , 2 , 3 ) quantum_circuit.cx(1 , 2 ) quantum_circuit.cx(0 , 1 ) quantum_circuit.measure([2, 3] , a__ ) # measure the last two qbits __a = qiskit.Aer.get_backend("aer_simulator" ) __a = qiskit.execute(a__ , a__ , shots=1000 ) return job.result().get_counts(a__ ) if __name__ == "__main__": print(f'''Total sum count for state is: {quantum_full_adder(1, 1, 1)}''')
197
import timeit import numpy as np import datasets from datasets.arrow_writer import ArrowWriter from datasets.features.features import _ArrayXD def lowerCamelCase( a__): def wrapper(*a__ ,**a__): _SCREAMING_SNAKE_CASE =timeit.default_timer() _SCREAMING_SNAKE_CASE =func(*a__ ,**a__) _SCREAMING_SNAKE_CASE =timeit.default_timer() - starttime return delta _SCREAMING_SNAKE_CASE =func.__name__ return wrapper def lowerCamelCase( a__ ,a__=100 ,a__=None): _SCREAMING_SNAKE_CASE =[] _SCREAMING_SNAKE_CASE =seq_shapes or {} for i in range(a__): _SCREAMING_SNAKE_CASE ={} for col_id, (k, v) in enumerate(features.items()): if isinstance(a__ ,_ArrayXD): _SCREAMING_SNAKE_CASE =np.random.rand(*v.shape).astype(v.dtype) elif isinstance(a__ ,datasets.Value): if v.dtype == "string": _SCREAMING_SNAKE_CASE ='''The small grey turtle was surprisingly fast when challenged.''' else: _SCREAMING_SNAKE_CASE =np.random.randint(10 ,size=1).astype(v.dtype).item() elif isinstance(a__ ,datasets.Sequence): while isinstance(a__ ,datasets.Sequence): _SCREAMING_SNAKE_CASE =v.feature _SCREAMING_SNAKE_CASE =seq_shapes[k] _SCREAMING_SNAKE_CASE =np.random.rand(*a__).astype(v.dtype) _SCREAMING_SNAKE_CASE =data dummy_data.append((i, example)) return dummy_data def lowerCamelCase( a__ ,a__ ,a__=100 ,a__=None): _SCREAMING_SNAKE_CASE =generate_examples(a__ ,num_examples=a__ ,seq_shapes=a__) with ArrowWriter(features=a__ ,path=a__) as writer: for key, record in dummy_data: _SCREAMING_SNAKE_CASE =features.encode_example(a__) writer.write(a__) _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE =writer.finalize() if not num_final_examples == num_examples: raise ValueError( f"Error writing the dataset, wrote {num_final_examples} examples but should have written {num_examples}.") _SCREAMING_SNAKE_CASE =datasets.Dataset.from_file(filename=a__ ,info=datasets.DatasetInfo(features=a__)) return dataset
691
0
import unittest import numpy as np from transformers import RobertaConfig, is_flax_available from transformers.testing_utils import require_flax, slow from ...test_modeling_flax_common import FlaxModelTesterMixin, floats_tensor, ids_tensor, random_attention_mask if is_flax_available(): from transformers.models.roberta.modeling_flax_roberta import ( FlaxRobertaForCausalLM, FlaxRobertaForMaskedLM, FlaxRobertaForMultipleChoice, FlaxRobertaForQuestionAnswering, FlaxRobertaForSequenceClassification, FlaxRobertaForTokenClassification, FlaxRobertaModel, ) class lowercase_ ( unittest.TestCase ): '''simple docstring''' def __init__( self : Tuple , __UpperCAmelCase : Tuple , __UpperCAmelCase : Union[str, Any]=13 , __UpperCAmelCase : Union[str, Any]=7 , __UpperCAmelCase : Optional[int]=True , __UpperCAmelCase : Any=True , __UpperCAmelCase : List[str]=True , __UpperCAmelCase : Any=True , __UpperCAmelCase : List[str]=99 , __UpperCAmelCase : str=32 , __UpperCAmelCase : Dict=5 , __UpperCAmelCase : int=4 , __UpperCAmelCase : List[str]=37 , __UpperCAmelCase : Any="gelu" , __UpperCAmelCase : Tuple=0.1 , __UpperCAmelCase : Tuple=0.1 , __UpperCAmelCase : str=512 , __UpperCAmelCase : str=16 , __UpperCAmelCase : Optional[int]=2 , __UpperCAmelCase : Union[str, Any]=0.02 , __UpperCAmelCase : Optional[Any]=4 , ) ->int: """simple docstring""" a = parent a = batch_size a = seq_length a = is_training a = use_attention_mask a = use_token_type_ids a = use_labels a = vocab_size a = hidden_size a = num_hidden_layers a = num_attention_heads a = intermediate_size a = hidden_act a = hidden_dropout_prob a = attention_probs_dropout_prob a = max_position_embeddings a = type_vocab_size a = type_sequence_label_size a = initializer_range a = num_choices def __lowerCAmelCase ( self : List[str] ) ->List[Any]: """simple docstring""" a = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) a = None if self.use_attention_mask: a = random_attention_mask([self.batch_size, self.seq_length] ) a = None if self.use_token_type_ids: a = ids_tensor([self.batch_size, self.seq_length] , self.type_vocab_size ) a = RobertaConfig( vocab_size=self.vocab_size , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , max_position_embeddings=self.max_position_embeddings , type_vocab_size=self.type_vocab_size , is_decoder=_a , initializer_range=self.initializer_range , ) return config, input_ids, token_type_ids, attention_mask def __lowerCAmelCase ( self : Tuple ) ->List[str]: """simple docstring""" a = self.prepare_config_and_inputs() a , a , a , a = config_and_inputs a = {'''input_ids''': input_ids, '''token_type_ids''': token_type_ids, '''attention_mask''': attention_mask} return config, inputs_dict def __lowerCAmelCase ( self : List[Any] ) ->str: """simple docstring""" a = self.prepare_config_and_inputs() a , a , a , a = config_and_inputs a = True a = floats_tensor([self.batch_size, self.seq_length, self.hidden_size] ) a = ids_tensor([self.batch_size, self.seq_length] , vocab_size=2 ) return ( config, input_ids, token_type_ids, encoder_hidden_states, encoder_attention_mask, ) @require_flax class lowercase_ ( UpperCamelCase__ , unittest.TestCase ): '''simple docstring''' __snake_case = True __snake_case = ( ( FlaxRobertaModel, FlaxRobertaForCausalLM, FlaxRobertaForMaskedLM, FlaxRobertaForSequenceClassification, FlaxRobertaForTokenClassification, FlaxRobertaForMultipleChoice, FlaxRobertaForQuestionAnswering, ) if is_flax_available() else () ) def __lowerCAmelCase ( self : Optional[int] ) ->Union[str, Any]: """simple docstring""" a = FlaxRobertaModelTester(self ) @slow def __lowerCAmelCase ( self : int ) ->Dict: """simple docstring""" for model_class_name in self.all_model_classes: a = model_class_name.from_pretrained('''roberta-base''' , from_pt=_a ) a = model(np.ones((1, 1) ) ) self.assertIsNotNone(_a )
117
import logging import torch from torch import nn from torch.nn import CrossEntropyLoss, MSELoss from transformers.file_utils import add_start_docstrings, add_start_docstrings_to_model_forward from transformers.models.bert.modeling_bert import ( BERT_INPUTS_DOCSTRING, BERT_START_DOCSTRING, BertEncoder, BertModel, BertPreTrainedModel, ) snake_case_ : Optional[Any] = logging.getLogger(__name__) class A__ ( UpperCamelCase__ ): def __UpperCamelCase ( self : Optional[int] , _a : Union[str, Any] , _a : List[str] , _a : List[Any]=None , _a : Optional[Any]=None ) -> Any: """simple docstring""" _SCREAMING_SNAKE_CASE =self.layer[current_layer](_a , _a , head_mask[current_layer] ) _SCREAMING_SNAKE_CASE =layer_outputs[0] return hidden_states @add_start_docstrings( "The bare Bert Model transformer with PABEE outputting raw hidden-states without any specific head on top." , UpperCamelCase__ , ) class A__ ( UpperCamelCase__ ): def __init__( self : List[str] , _a : Union[str, Any] ) -> Tuple: """simple docstring""" super().__init__(_a ) _SCREAMING_SNAKE_CASE =BertEncoderWithPabee(_a ) self.init_weights() _SCREAMING_SNAKE_CASE =0 _SCREAMING_SNAKE_CASE =0 _SCREAMING_SNAKE_CASE =0 _SCREAMING_SNAKE_CASE =0 def __UpperCamelCase ( self : List[str] , _a : Optional[int] ) -> List[str]: """simple docstring""" _SCREAMING_SNAKE_CASE =threshold def __UpperCamelCase ( self : Dict , _a : int ) -> Tuple: """simple docstring""" _SCREAMING_SNAKE_CASE =patience def __UpperCamelCase ( self : Optional[Any] ) -> int: """simple docstring""" _SCREAMING_SNAKE_CASE =0 _SCREAMING_SNAKE_CASE =0 def __UpperCamelCase ( self : Union[str, Any] ) -> Optional[int]: """simple docstring""" _SCREAMING_SNAKE_CASE =self.inference_layers_num / self.inference_instances_num _SCREAMING_SNAKE_CASE =( f"*** Patience = {self.patience} Avg. Inference Layers = {avg_inf_layers:.2f} Speed Up =" f" {1 - avg_inf_layers / self.config.num_hidden_layers:.2f} ***" ) print(_a ) @add_start_docstrings_to_model_forward(_a ) def __UpperCamelCase ( self : List[Any] , _a : Optional[Any]=None , _a : Optional[int]=None , _a : Any=None , _a : Union[str, Any]=None , _a : Union[str, Any]=None , _a : Union[str, Any]=None , _a : str=None , _a : Any=None , _a : str=None , _a : Optional[Any]=None , _a : Dict=False , ) -> Union[str, Any]: """simple docstring""" if input_ids is not None and inputs_embeds is not None: raise ValueError('''You cannot specify both input_ids and inputs_embeds at the same time''' ) elif input_ids is not None: _SCREAMING_SNAKE_CASE =input_ids.size() elif inputs_embeds is not None: _SCREAMING_SNAKE_CASE =inputs_embeds.size()[:-1] else: raise ValueError('''You have to specify either input_ids or inputs_embeds''' ) _SCREAMING_SNAKE_CASE =input_ids.device if input_ids is not None else inputs_embeds.device if attention_mask is None: _SCREAMING_SNAKE_CASE =torch.ones(_a , device=_a ) if token_type_ids is None: _SCREAMING_SNAKE_CASE =torch.zeros(_a , dtype=torch.long , device=_a ) # We can provide a self-attention mask of dimensions [batch_size, from_seq_length, to_seq_length] # ourselves in which case we just need to make it broadcastable to all heads. _SCREAMING_SNAKE_CASE =self.get_extended_attention_mask(_a , _a , _a ) # If a 2D ou 3D attention mask is provided for the cross-attention # we need to make broadcastable to [batch_size, num_heads, seq_length, seq_length] if self.config.is_decoder and encoder_hidden_states is not None: _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE =encoder_hidden_states.size() _SCREAMING_SNAKE_CASE =(encoder_batch_size, encoder_sequence_length) if encoder_attention_mask is None: _SCREAMING_SNAKE_CASE =torch.ones(_a , device=_a ) _SCREAMING_SNAKE_CASE =self.invert_attention_mask(_a ) else: _SCREAMING_SNAKE_CASE =None # Prepare head mask if needed # 1.0 in head_mask indicate we keep the head # attention_probs has shape bsz x n_heads x N x N # input head_mask has shape [num_heads] or [num_hidden_layers x num_heads] # and head_mask is converted to shape [num_hidden_layers x batch x num_heads x seq_length x seq_length] _SCREAMING_SNAKE_CASE =self.get_head_mask(_a , self.config.num_hidden_layers ) _SCREAMING_SNAKE_CASE =self.embeddings( input_ids=_a , position_ids=_a , token_type_ids=_a , inputs_embeds=_a ) _SCREAMING_SNAKE_CASE =embedding_output if self.training: _SCREAMING_SNAKE_CASE =[] for i in range(self.config.num_hidden_layers ): _SCREAMING_SNAKE_CASE =self.encoder.adaptive_forward( _a , current_layer=_a , attention_mask=_a , head_mask=_a ) _SCREAMING_SNAKE_CASE =self.pooler(_a ) _SCREAMING_SNAKE_CASE =output_layers[i](output_dropout(_a ) ) res.append(_a ) elif self.patience == 0: # Use all layers for inference _SCREAMING_SNAKE_CASE =self.encoder( _a , attention_mask=_a , head_mask=_a , encoder_hidden_states=_a , encoder_attention_mask=_a , ) _SCREAMING_SNAKE_CASE =self.pooler(encoder_outputs[0] ) _SCREAMING_SNAKE_CASE =[output_layers[self.config.num_hidden_layers - 1](_a )] else: _SCREAMING_SNAKE_CASE =0 _SCREAMING_SNAKE_CASE =None _SCREAMING_SNAKE_CASE =0 for i in range(self.config.num_hidden_layers ): calculated_layer_num += 1 _SCREAMING_SNAKE_CASE =self.encoder.adaptive_forward( _a , current_layer=_a , attention_mask=_a , head_mask=_a ) _SCREAMING_SNAKE_CASE =self.pooler(_a ) _SCREAMING_SNAKE_CASE =output_layers[i](_a ) if regression: _SCREAMING_SNAKE_CASE =logits.detach() if patient_result is not None: _SCREAMING_SNAKE_CASE =patient_result.detach() if (patient_result is not None) and torch.abs(patient_result - labels ) < self.regression_threshold: patient_counter += 1 else: _SCREAMING_SNAKE_CASE =0 else: _SCREAMING_SNAKE_CASE =logits.detach().argmax(dim=1 ) if patient_result is not None: _SCREAMING_SNAKE_CASE =patient_result.detach().argmax(dim=1 ) if (patient_result is not None) and torch.all(labels.eq(_a ) ): patient_counter += 1 else: _SCREAMING_SNAKE_CASE =0 _SCREAMING_SNAKE_CASE =logits if patient_counter == self.patience: break _SCREAMING_SNAKE_CASE =[patient_result] self.inference_layers_num += calculated_layer_num self.inference_instances_num += 1 return res @add_start_docstrings( "Bert Model transformer with PABEE and a sequence classification/regression head on top (a linear layer on top of\n the pooled output) e.g. for GLUE tasks. " , UpperCamelCase__ , ) class A__ ( UpperCamelCase__ ): def __init__( self : Optional[int] , _a : List[Any] ) -> Union[str, Any]: """simple docstring""" super().__init__(_a ) _SCREAMING_SNAKE_CASE =config.num_labels _SCREAMING_SNAKE_CASE =BertModelWithPabee(_a ) _SCREAMING_SNAKE_CASE =nn.Dropout(config.hidden_dropout_prob ) _SCREAMING_SNAKE_CASE =nn.ModuleList( [nn.Linear(config.hidden_size , self.config.num_labels ) for _ in range(config.num_hidden_layers )] ) self.init_weights() @add_start_docstrings_to_model_forward(_a ) def __UpperCamelCase ( self : List[str] , _a : Optional[Any]=None , _a : List[Any]=None , _a : Union[str, Any]=None , _a : List[str]=None , _a : Dict=None , _a : Optional[Any]=None , _a : Optional[Any]=None , ) -> str: """simple docstring""" _SCREAMING_SNAKE_CASE =self.bert( input_ids=_a , attention_mask=_a , token_type_ids=_a , position_ids=_a , head_mask=_a , inputs_embeds=_a , output_dropout=self.dropout , output_layers=self.classifiers , regression=self.num_labels == 1 , ) _SCREAMING_SNAKE_CASE =(logits[-1],) if labels is not None: _SCREAMING_SNAKE_CASE =None _SCREAMING_SNAKE_CASE =0 for ix, logits_item in enumerate(_a ): if self.num_labels == 1: # We are doing regression _SCREAMING_SNAKE_CASE =MSELoss() _SCREAMING_SNAKE_CASE =loss_fct(logits_item.view(-1 ) , labels.view(-1 ) ) else: _SCREAMING_SNAKE_CASE =CrossEntropyLoss() _SCREAMING_SNAKE_CASE =loss_fct(logits_item.view(-1 , self.num_labels ) , labels.view(-1 ) ) if total_loss is None: _SCREAMING_SNAKE_CASE =loss else: total_loss += loss * (ix + 1) total_weights += ix + 1 _SCREAMING_SNAKE_CASE =(total_loss / total_weights,) + outputs return outputs
691
0
from typing import Dict, List, Optional, Union import numpy as np from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size_dict from ...image_transforms import ( center_crop, get_resize_output_image_size, normalize, rescale, resize, to_channel_dimension_format, ) from ...image_utils import ( IMAGENET_DEFAULT_MEAN, IMAGENET_DEFAULT_STD, ChannelDimension, ImageInput, PILImageResampling, make_list_of_images, to_numpy_array, valid_images, ) from ...utils import TensorType, is_vision_available, logging if is_vision_available(): import PIL A : str = logging.get_logger(__name__) class UpperCamelCase( UpperCamelCase__ ): snake_case_ : Tuple = ["""pixel_values"""] def __init__( self : int , SCREAMING_SNAKE_CASE : bool = True , SCREAMING_SNAKE_CASE : Dict[str, int] = None , SCREAMING_SNAKE_CASE : int = 0.9 , SCREAMING_SNAKE_CASE : PILImageResampling = PILImageResampling.BICUBIC , SCREAMING_SNAKE_CASE : bool = True , SCREAMING_SNAKE_CASE : Dict[str, int] = None , SCREAMING_SNAKE_CASE : Union[int, float] = 1 / 2_5_5 , SCREAMING_SNAKE_CASE : bool = True , 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 : Optional[Any] , ) -> None: '''simple docstring''' super().__init__(**_a ) __snake_case = size if size is not None else {"shortest_edge": 2_2_4} __snake_case = get_size_dict(_a , default_to_square=_a ) __snake_case = crop_size if crop_size is not None else {"height": 2_2_4, "width": 2_2_4} __snake_case = get_size_dict(_a , param_name="crop_size" ) __snake_case = do_resize __snake_case = size __snake_case = crop_pct __snake_case = resample __snake_case = do_center_crop __snake_case = crop_size __snake_case = do_rescale __snake_case = rescale_factor __snake_case = do_normalize __snake_case = image_mean if image_mean is not None else IMAGENET_DEFAULT_MEAN __snake_case = image_std if image_std is not None else IMAGENET_DEFAULT_STD def SCREAMING_SNAKE_CASE_ ( self : List[Any] , SCREAMING_SNAKE_CASE : np.ndarray , SCREAMING_SNAKE_CASE : Dict[str, int] , SCREAMING_SNAKE_CASE : Optional[float] = None , SCREAMING_SNAKE_CASE : PILImageResampling = PILImageResampling.BICUBIC , SCREAMING_SNAKE_CASE : Optional[Union[str, ChannelDimension]] = None , **SCREAMING_SNAKE_CASE : List[Any] , ) -> np.ndarray: '''simple docstring''' __snake_case = get_size_dict(_a , default_to_square=_a ) if "shortest_edge" not in size and ("height" not in size or "width" not in size): raise ValueError(f'''size must contain \'height\' and \'width\' or \'shortest_edge\' as keys. Got {size.keys()}''' ) if crop_pct is not None: if "shortest_edge" in size: __snake_case = int(size["shortest_edge"] / crop_pct ) elif "height" in size and "width" in size: if size["height"] == size["width"]: __snake_case = int(size["height"] / crop_pct ) else: __snake_case = (int(size["height"] / crop_pct ), int(size["width"] / crop_pct )) else: raise ValueError("Invalid size for resize: {}".format(_a ) ) __snake_case = get_resize_output_image_size(_a , size=_a , default_to_square=_a ) else: if "shortest_edge" in size: __snake_case = get_resize_output_image_size(_a , size=size["shortest_edge"] , default_to_square=_a ) elif "height" in size and "width" in size: __snake_case = (size["height"], size["width"]) else: raise ValueError("Invalid size for resize: {}".format(_a ) ) return resize(_a , size=_a , resample=_a , data_format=_a , **_a ) def SCREAMING_SNAKE_CASE_ ( self : Dict , SCREAMING_SNAKE_CASE : np.ndarray , SCREAMING_SNAKE_CASE : Dict[str, int] , SCREAMING_SNAKE_CASE : Optional[Union[str, ChannelDimension]] = None , **SCREAMING_SNAKE_CASE : Optional[Any] , ) -> np.ndarray: '''simple docstring''' __snake_case = get_size_dict(_a ) if "height" not in size or "width" not in size: raise ValueError(f'''size must contain \'height\' and \'width\' as keys. Got {size.keys()}''' ) return center_crop(_a , size=(size["height"], size["width"]) , data_format=_a , **_a ) def SCREAMING_SNAKE_CASE_ ( self : Union[str, Any] , SCREAMING_SNAKE_CASE : np.ndarray , SCREAMING_SNAKE_CASE : Union[int, float] , SCREAMING_SNAKE_CASE : Optional[Union[str, ChannelDimension]] = None , **SCREAMING_SNAKE_CASE : Tuple , ) -> Tuple: '''simple docstring''' return rescale(_a , scale=_a , data_format=_a , **_a ) def SCREAMING_SNAKE_CASE_ ( self : List[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 : Any , ) -> np.ndarray: '''simple docstring''' return normalize(_a , mean=_a , std=_a , data_format=_a , **_a ) def SCREAMING_SNAKE_CASE_ ( self : Tuple , SCREAMING_SNAKE_CASE : ImageInput , SCREAMING_SNAKE_CASE : bool = None , SCREAMING_SNAKE_CASE : Dict[str, int] = None , SCREAMING_SNAKE_CASE : int = None , SCREAMING_SNAKE_CASE : PILImageResampling = None , SCREAMING_SNAKE_CASE : bool = None , SCREAMING_SNAKE_CASE : Dict[str, 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 : Optional[Union[str, TensorType]] = None , SCREAMING_SNAKE_CASE : ChannelDimension = ChannelDimension.FIRST , **SCREAMING_SNAKE_CASE : str , ) -> PIL.Image.Image: '''simple docstring''' __snake_case = do_resize if do_resize is not None else self.do_resize __snake_case = crop_pct if crop_pct is not None else self.crop_pct __snake_case = resample if resample is not None else self.resample __snake_case = do_center_crop if do_center_crop is not None else self.do_center_crop __snake_case = do_rescale if do_rescale is not None else self.do_rescale __snake_case = rescale_factor if rescale_factor is not None else self.rescale_factor __snake_case = do_normalize if do_normalize is not None else self.do_normalize __snake_case = image_mean if image_mean is not None else self.image_mean __snake_case = image_std if image_std is not None else self.image_std __snake_case = size if size is not None else self.size __snake_case = get_size_dict(_a , default_to_square=_a ) __snake_case = crop_size if crop_size is not None else self.crop_size __snake_case = get_size_dict(_a , param_name="crop_size" ) __snake_case = make_list_of_images(_a ) if not valid_images(_a ): raise ValueError( "Invalid image type. Must be of type PIL.Image.Image, numpy.ndarray, " "torch.Tensor, tf.Tensor or jax.ndarray." ) if do_resize and size is None or resample is None: raise ValueError("Size and resample must be specified if do_resize is True." ) if do_center_crop and crop_pct is None: raise ValueError("Crop_pct must be specified if do_center_crop is True." ) if do_rescale and rescale_factor is None: raise ValueError("Rescale factor must be specified if do_rescale is True." ) if do_normalize and (image_mean is None or image_std is None): raise ValueError("Image mean and std must be specified if do_normalize is True." ) # All transformations expect numpy arrays. __snake_case = [to_numpy_array(_a ) for image in images] if do_resize: __snake_case = [self.resize(image=_a , size=_a , crop_pct=_a , resample=_a ) for image in images] if do_center_crop: __snake_case = [self.center_crop(image=_a , size=_a ) for image in images] if do_rescale: __snake_case = [self.rescale(image=_a , scale=_a ) for image in images] if do_normalize: __snake_case = [self.normalize(image=_a , mean=_a , std=_a ) for image in images] __snake_case = [to_channel_dimension_format(_a , _a ) for image in images] __snake_case = {"pixel_values": images} return BatchFeature(data=_a , tensor_type=_a )
371
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available snake_case_ : str = { '''configuration_table_transformer''': [ '''TABLE_TRANSFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''TableTransformerConfig''', '''TableTransformerOnnxConfig''', ] } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: snake_case_ : str = [ '''TABLE_TRANSFORMER_PRETRAINED_MODEL_ARCHIVE_LIST''', '''TableTransformerForObjectDetection''', '''TableTransformerModel''', '''TableTransformerPreTrainedModel''', ] if TYPE_CHECKING: from .configuration_table_transformer import ( TABLE_TRANSFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP, TableTransformerConfig, TableTransformerOnnxConfig, ) try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_table_transformer import ( TABLE_TRANSFORMER_PRETRAINED_MODEL_ARCHIVE_LIST, TableTransformerForObjectDetection, TableTransformerModel, TableTransformerPreTrainedModel, ) else: import sys snake_case_ : Any = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
691
0
'''simple docstring''' import math import os import re import sys import unittest from pathlib import Path from typing import Tuple from unittest.mock import patch from parameterized import parameterized from transformers.testing_utils import ( CaptureStderr, ExtendSysPath, TestCasePlus, execute_subprocess_async, get_gpu_count, get_torch_dist_unique_port, require_apex, require_bitsandbytes, require_fairscale, require_torch, require_torch_gpu, require_torch_multi_gpu, require_torch_non_multi_gpu, slow, ) from transformers.trainer_callback import TrainerState from transformers.trainer_utils import set_seed UpperCamelCase_ = os.path.abspath(os.path.dirname(__file__)) with ExtendSysPath(f'''{bindir}/../../examples/pytorch/translation'''): from run_translation import main # noqa set_seed(42) UpperCamelCase_ = '''sshleifer/student_marian_en_ro_6_1''' UpperCamelCase_ = '''sshleifer/tiny-mbart''' @require_torch class __SCREAMING_SNAKE_CASE ( UpperCamelCase__ ): def lowerCamelCase_ ( self : Any , UpperCAmelCase__ : List[Any]=False , UpperCAmelCase__ : List[str]=None , UpperCAmelCase__ : Optional[int]=True , UpperCAmelCase__ : str=True , UpperCAmelCase__ : Optional[int]=True , UpperCAmelCase__ : Optional[int]=True , ): '''simple docstring''' lowercase : str =self.run_trainer( eval_steps=1 , max_len=12 , model_name=_a , num_train_epochs=1 , distributed=_a , extra_args_str=_a , predict_with_generate=_a , do_train=_a , do_eval=_a , do_predict=_a , ) lowercase : int =TrainerState.load_from_json(os.path.join(_a , '''trainer_state.json''' ) ).log_history if not do_eval: return lowercase : Any =[log for log in logs if '''eval_loss''' in log.keys()] lowercase : List[Any] =eval_metrics[0] if predict_with_generate: assert "eval_bleu" in first_step_stats lowercase : Tuple =eval_metrics[-1] assert isinstance(last_step_stats['''eval_bleu'''] , _a ) assert not math.isnan(float(last_step_stats['''eval_loss'''] ) ), "eval_loss must not be `nan`" @require_torch_non_multi_gpu def lowerCamelCase_ ( self : Union[str, Any] ): '''simple docstring''' self.run_seqaseq_quick() @require_torch_multi_gpu def lowerCamelCase_ ( self : str ): '''simple docstring''' self.run_seqaseq_quick(distributed=_a ) @require_torch_multi_gpu def lowerCamelCase_ ( self : Optional[Any] ): '''simple docstring''' self.run_seqaseq_quick(distributed=_a ) @unittest.skip('''Requires an update of the env running those tests''' ) @require_torch_multi_gpu @require_fairscale def lowerCamelCase_ ( self : List[str] ): '''simple docstring''' self.run_seqaseq_quick(distributed=_a , extra_args_str='''--sharded_ddp simple''' ) @unittest.skip('''Requires an update of the env running those tests''' ) @require_torch_multi_gpu @require_fairscale def lowerCamelCase_ ( self : Union[str, Any] ): '''simple docstring''' self.run_seqaseq_quick(distributed=_a , extra_args_str='''--sharded_ddp simple --fp16''' ) @unittest.skip('''Requires an update of the env running those tests''' ) @require_torch_multi_gpu @require_fairscale def lowerCamelCase_ ( self : Optional[Any] ): '''simple docstring''' self.run_seqaseq_quick(distributed=_a , extra_args_str='''--sharded_ddp zero_dp_2''' , predict_with_generate=_a ) @unittest.skip('''Requires an update of the env running those tests''' ) @require_torch_multi_gpu @require_fairscale def lowerCamelCase_ ( self : List[str] ): '''simple docstring''' self.run_seqaseq_quick( distributed=_a , extra_args_str='''--sharded_ddp zero_dp_2 --fp16''' , predict_with_generate=_a ) @require_apex @require_torch_gpu def lowerCamelCase_ ( self : List[str] ): '''simple docstring''' self.run_seqaseq_quick(distributed=_a , extra_args_str='''--fp16 --fp16_backend=apex''' ) # test 2nd time - was getting eval_loss': nan' # to reproduce the problem set distributed=False self.run_seqaseq_quick(distributed=_a , extra_args_str='''--fp16 --fp16_backend=apex''' ) @parameterized.expand(['''base''', '''low''', '''high''', '''mixed'''] ) @require_torch_multi_gpu def lowerCamelCase_ ( self : str , UpperCAmelCase__ : List[str] ): '''simple docstring''' lowercase : Dict ={ # test with the default log_level - should be info and thus log info once '''base''': {'''extra_args_str''': '''''', '''n_matches''': 1}, # test with low log_level and log_level_replica - should be noisy on all processes # now the info string should appear twice on 2 processes '''low''': {'''extra_args_str''': '''--log_level debug --log_level_replica debug''', '''n_matches''': 2}, # test with high log_level and low log_level_replica # now the info string should appear once only on the replica '''high''': {'''extra_args_str''': '''--log_level error --log_level_replica debug''', '''n_matches''': 1}, # test with high log_level and log_level_replica - should be quiet on all processes '''mixed''': {'''extra_args_str''': '''--log_level error --log_level_replica error''', '''n_matches''': 0}, } lowercase : Any =experiments[experiment_id] lowercase : List[Any] ={'''distributed''': True, '''predict_with_generate''': False, '''do_eval''': False, '''do_predict''': False} lowercase : int ='''Running training''' with CaptureStderr() as cl: self.run_seqaseq_quick(**_a , extra_args_str=data['''extra_args_str'''] ) lowercase : List[str] =len(re.findall(_a , cl.err ) ) self.assertEqual(_a , data['''n_matches'''] ) @slow def lowerCamelCase_ ( self : List[str] ): '''simple docstring''' lowercase : Any =self.run_trainer( eval_steps=2 , max_len=128 , model_name=_a , learning_rate=3E-4 , num_train_epochs=10 , distributed=_a , ) # Check metrics lowercase : Optional[int] =TrainerState.load_from_json(os.path.join(_a , '''trainer_state.json''' ) ).log_history lowercase : Tuple =[log for log in logs if '''eval_loss''' in log.keys()] lowercase : Optional[int] =eval_metrics[0] lowercase : Optional[Any] =eval_metrics[-1] assert first_step_stats["eval_loss"] > last_step_stats["eval_loss"], "model learned nothing" assert isinstance(last_step_stats['''eval_bleu'''] , _a ) # test if do_predict saves generations and metrics lowercase : List[str] =os.listdir(_a ) lowercase : Any ={os.path.basename(_a ) for p in contents} assert "generated_predictions.txt" in contents assert "predict_results.json" in contents @slow @require_bitsandbytes def lowerCamelCase_ ( self : Union[str, Any] ): '''simple docstring''' from transformers.training_args import OptimizerNames def train_and_return_metrics(UpperCAmelCase__ : str ) -> Tuple[int, float]: lowercase : int ='''--skip_memory_metrics 0''' lowercase : int =self.run_trainer( max_len=128 , model_name=_a , learning_rate=3E-4 , num_train_epochs=1 , optim=_a , distributed=_a , extra_args_str=_a , do_eval=_a , do_predict=_a , n_gpus_to_use=1 , ) # Check metrics lowercase : List[str] =TrainerState.load_from_json(Path(_a , '''trainer_state.json''' ) ).log_history lowercase : Any =int(logs[0]['''train_mem_gpu_peaked_delta'''] / 2**20 ) lowercase : Optional[Any] =int(logs[0]['''train_mem_gpu_alloc_delta'''] / 2**20 ) lowercase : int =logs[0]['''train_loss'''] return gpu_peak_mem_mb, gpu_alloc_mem_mb, loss lowercase , lowercase , lowercase : Any =train_and_return_metrics(OptimizerNames.ADAMW_TORCH.value ) lowercase , lowercase , lowercase : Tuple =train_and_return_metrics(OptimizerNames.ADAMW_BNB.value ) lowercase : str =gpu_alloc_mem_orig - gpu_alloc_mem_bnb lowercase : str =gpu_peak_mem_orig + gpu_alloc_mem_orig lowercase : List[str] =gpu_peak_mem_bnb + gpu_alloc_mem_bnb lowercase : List[str] =gpu_total_mem_orig - gpu_total_mem_bnb # sshleifer/student_marian_en_ro_6_1 has 54M parameter, 29M of which is `nn.Embedding` which # doesn't get quantized and remains in fp32. Therefore we only have 25M parameters quantized # in 2 bytes and the diff in optim memory usage is derived as so: # # - normal 25*8=~200MB (8 bytes per param) # - bnb 25*2= ~50MB (2 bytes per param) # # Thus we should expect ~150MB total memory saved. # # Peak memory should be the same - the total should be different by about that same margin # # After leaving a small margin to accommodate for differences between gpus let's check # that we have at least 120MB in savings lowercase : str =120 # uncomment the following if this test starts failing - requires py38 for a new print feature # gpu_peak_mem_diff = gpu_peak_mem_orig - gpu_peak_mem_bnb # print(f"{gpu_alloc_mem_orig=}MB {gpu_peak_mem_orig=}MB {gpu_alloc_mem_orig+gpu_peak_mem_orig=}MB") # print(f" {gpu_alloc_mem_bnb=}MB {gpu_peak_mem_bnb=}MB {gpu_alloc_mem_bnb+gpu_peak_mem_bnb=}MB") # print(f"{gpu_alloc_mem_diff=}MB") # print(f"{gpu_peak_mem_diff=}MB") # print(f"{gpu_total_mem_orig=}MB, {gpu_total_mem_bnb=}MB") # print(f"{gpu_total_mem_diff=}MB, {gpu_total_mem_diff=}MB") self.assertGreater( _a , _a , '''should use ~150MB less alloc gpu memory with BNB, compared to without it for this model but got''' F''' a difference of {gpu_alloc_mem_diff}MB, with gpu_alloc_mem_orig={gpu_alloc_mem_orig}MB and''' F''' gpu_alloc_mem_bnb={gpu_alloc_mem_bnb}MB''' , ) self.assertGreater( _a , _a , '''should use ~150MB less total gpu memory with BNB, compared to without it for this model but got''' F''' a difference of {gpu_total_mem_diff}MB, with gpu_total_mem_orig={gpu_total_mem_orig}MB and''' F''' gpu_total_mem_bnb={gpu_total_mem_bnb}MB''' , ) self.assertEqual( _a , _a , F'''loss should be the same, but got loss_orig={loss_orig}, loss_bnb={loss_bnb}''' ) def lowerCamelCase_ ( self : Optional[int] , UpperCAmelCase__ : int , UpperCAmelCase__ : str , UpperCAmelCase__ : int , UpperCAmelCase__ : float = 3E-3 , UpperCAmelCase__ : str = "adafactor" , UpperCAmelCase__ : bool = False , UpperCAmelCase__ : str = None , UpperCAmelCase__ : int = 0 , UpperCAmelCase__ : bool = True , UpperCAmelCase__ : bool = True , UpperCAmelCase__ : bool = True , UpperCAmelCase__ : bool = True , UpperCAmelCase__ : int = None , ): '''simple docstring''' lowercase : Union[str, Any] =self.test_file_dir / '''../fixtures/tests_samples/wmt_en_ro''' lowercase : str =self.get_auto_remove_tmp_dir() lowercase : str =F'''\n --model_name_or_path {model_name}\n --train_file {data_dir}/train.json\n --validation_file {data_dir}/val.json\n --test_file {data_dir}/test.json\n --output_dir {output_dir}\n --overwrite_output_dir\n --max_train_samples 8\n --max_source_length {max_len}\n --max_target_length {max_len}\n --do_train\n --num_train_epochs {str(_a )}\n --per_device_train_batch_size 4\n --learning_rate {learning_rate}\n --warmup_steps 8\n --logging_steps 0\n --logging_strategy no\n --save_steps {str(_a )}\n --group_by_length\n --label_smoothing_factor 0.1\n --target_lang ro_RO\n --source_lang en_XX\n '''.split() lowercase : Optional[Any] =F'''\n --do_eval\n --per_device_eval_batch_size 4\n --max_eval_samples 8\n --val_max_target_length {max_len}\n --evaluation_strategy steps\n --eval_steps {str(_a )}\n '''.split() lowercase : Union[str, Any] =''' --do_predict '''.split() lowercase : Tuple =[] if do_train: args += args_train if do_eval: args += args_eval if do_predict: args += args_predict if predict_with_generate: args += "--predict_with_generate".split() if do_train: if optim == "adafactor": args += "--adafactor".split() else: args += F'''--optim {optim}'''.split() if extra_args_str is not None: args += extra_args_str.split() if distributed: if n_gpus_to_use is None: lowercase : Optional[int] =get_gpu_count() lowercase : str =get_torch_dist_unique_port() lowercase : List[str] =F'''\n -m torch.distributed.run\n --nproc_per_node={n_gpus_to_use}\n --master_port={master_port}\n {self.examples_dir_str}/pytorch/translation/run_translation.py\n '''.split() lowercase : str =[sys.executable] + distributed_args + args # keep for quick debug # print(" ".join([f"\nPYTHONPATH={self.src_dir_str}"] +cmd)); die execute_subprocess_async(_a , env=self.get_env() ) else: lowercase : Any =['''run_translation.py'''] + args with patch.object(_a , '''argv''' , _a ): main() return output_dir
92
import json import os import shutil import tempfile import unittest import numpy as np import pytest from transformers import MgpstrTokenizer from transformers.models.mgp_str.tokenization_mgp_str import VOCAB_FILES_NAMES from transformers.testing_utils import require_torch, require_vision from transformers.utils import IMAGE_PROCESSOR_NAME, is_torch_available, is_vision_available if is_torch_available(): import torch if is_vision_available(): from PIL import Image from transformers import MgpstrProcessor, ViTImageProcessor @require_torch @require_vision class A__ ( unittest.TestCase ): UpperCAmelCase = ViTImageProcessor if is_vision_available() else None @property def __UpperCamelCase ( self : str ) -> Union[str, Any]: """simple docstring""" return self.image_processor_tester.prepare_image_processor_dict() def __UpperCamelCase ( self : str ) -> List[str]: """simple docstring""" _SCREAMING_SNAKE_CASE =(3, 32, 128) _SCREAMING_SNAKE_CASE =tempfile.mkdtemp() # fmt: off _SCREAMING_SNAKE_CASE =['''[GO]''', '''[s]''', '''0''', '''1''', '''2''', '''3''', '''4''', '''5''', '''6''', '''7''', '''8''', '''9''', '''a''', '''b''', '''c''', '''d''', '''e''', '''f''', '''g''', '''h''', '''i''', '''j''', '''k''', '''l''', '''m''', '''n''', '''o''', '''p''', '''q''', '''r''', '''s''', '''t''', '''u''', '''v''', '''w''', '''x''', '''y''', '''z'''] # fmt: on _SCREAMING_SNAKE_CASE =dict(zip(_a , range(len(_a ) ) ) ) _SCREAMING_SNAKE_CASE =os.path.join(self.tmpdirname , VOCAB_FILES_NAMES['''vocab_file'''] ) with open(self.vocab_file , '''w''' , encoding='''utf-8''' ) as fp: fp.write(json.dumps(_a ) + '''\n''' ) _SCREAMING_SNAKE_CASE ={ '''do_normalize''': False, '''do_resize''': True, '''image_processor_type''': '''ViTImageProcessor''', '''resample''': 3, '''size''': {'''height''': 32, '''width''': 128}, } _SCREAMING_SNAKE_CASE =os.path.join(self.tmpdirname , _a ) with open(self.image_processor_file , '''w''' , encoding='''utf-8''' ) as fp: json.dump(_a , _a ) def __UpperCamelCase ( self : Optional[Any] , **_a : str ) -> int: """simple docstring""" return MgpstrTokenizer.from_pretrained(self.tmpdirname , **_a ) def __UpperCamelCase ( self : Optional[int] , **_a : Tuple ) -> List[Any]: """simple docstring""" return ViTImageProcessor.from_pretrained(self.tmpdirname , **_a ) def __UpperCamelCase ( self : Tuple ) -> str: """simple docstring""" shutil.rmtree(self.tmpdirname ) def __UpperCamelCase ( self : List[Any] ) -> Any: """simple docstring""" _SCREAMING_SNAKE_CASE =np.random.randint(255 , size=(3, 30, 400) , dtype=np.uinta ) _SCREAMING_SNAKE_CASE =Image.fromarray(np.moveaxis(_a , 0 , -1 ) ) return image_input def __UpperCamelCase ( self : Union[str, Any] ) -> Dict: """simple docstring""" _SCREAMING_SNAKE_CASE =self.get_tokenizer() _SCREAMING_SNAKE_CASE =self.get_image_processor() _SCREAMING_SNAKE_CASE =MgpstrProcessor(tokenizer=_a , image_processor=_a ) processor.save_pretrained(self.tmpdirname ) _SCREAMING_SNAKE_CASE =MgpstrProcessor.from_pretrained(self.tmpdirname , use_fast=_a ) self.assertEqual(processor.char_tokenizer.get_vocab() , tokenizer.get_vocab() ) self.assertIsInstance(processor.char_tokenizer , _a ) self.assertEqual(processor.image_processor.to_json_string() , image_processor.to_json_string() ) self.assertIsInstance(processor.image_processor , _a ) def __UpperCamelCase ( self : Union[str, Any] ) -> List[Any]: """simple docstring""" _SCREAMING_SNAKE_CASE =self.get_tokenizer() _SCREAMING_SNAKE_CASE =self.get_image_processor() _SCREAMING_SNAKE_CASE =MgpstrProcessor(tokenizer=_a , image_processor=_a ) processor.save_pretrained(self.tmpdirname ) _SCREAMING_SNAKE_CASE =self.get_tokenizer(bos_token='''(BOS)''' , eos_token='''(EOS)''' ) _SCREAMING_SNAKE_CASE =self.get_image_processor(do_normalize=_a , padding_value=1.0 ) _SCREAMING_SNAKE_CASE =MgpstrProcessor.from_pretrained( self.tmpdirname , bos_token='''(BOS)''' , eos_token='''(EOS)''' , do_normalize=_a , padding_value=1.0 ) self.assertEqual(processor.char_tokenizer.get_vocab() , tokenizer_add_kwargs.get_vocab() ) self.assertIsInstance(processor.char_tokenizer , _a ) self.assertEqual(processor.image_processor.to_json_string() , image_processor_add_kwargs.to_json_string() ) self.assertIsInstance(processor.image_processor , _a ) def __UpperCamelCase ( self : Union[str, Any] ) -> str: """simple docstring""" _SCREAMING_SNAKE_CASE =self.get_image_processor() _SCREAMING_SNAKE_CASE =self.get_tokenizer() _SCREAMING_SNAKE_CASE =MgpstrProcessor(tokenizer=_a , image_processor=_a ) _SCREAMING_SNAKE_CASE =self.prepare_image_inputs() _SCREAMING_SNAKE_CASE =image_processor(_a , return_tensors='''np''' ) _SCREAMING_SNAKE_CASE =processor(images=_a , return_tensors='''np''' ) for key in input_image_proc.keys(): self.assertAlmostEqual(input_image_proc[key].sum() , input_processor[key].sum() , delta=1E-2 ) def __UpperCamelCase ( self : Optional[int] ) -> List[str]: """simple docstring""" _SCREAMING_SNAKE_CASE =self.get_image_processor() _SCREAMING_SNAKE_CASE =self.get_tokenizer() _SCREAMING_SNAKE_CASE =MgpstrProcessor(tokenizer=_a , image_processor=_a ) _SCREAMING_SNAKE_CASE ='''test''' _SCREAMING_SNAKE_CASE =processor(text=_a ) _SCREAMING_SNAKE_CASE =tokenizer(_a ) for key in encoded_tok.keys(): self.assertListEqual(encoded_tok[key] , encoded_processor[key] ) def __UpperCamelCase ( self : List[str] ) -> List[Any]: """simple docstring""" _SCREAMING_SNAKE_CASE =self.get_image_processor() _SCREAMING_SNAKE_CASE =self.get_tokenizer() _SCREAMING_SNAKE_CASE =MgpstrProcessor(tokenizer=_a , image_processor=_a ) _SCREAMING_SNAKE_CASE ='''test''' _SCREAMING_SNAKE_CASE =self.prepare_image_inputs() _SCREAMING_SNAKE_CASE =processor(text=_a , images=_a ) self.assertListEqual(list(inputs.keys() ) , ['''pixel_values''', '''labels'''] ) # test if it raises when no input is passed with pytest.raises(_a ): processor() def __UpperCamelCase ( self : List[Any] ) -> Any: """simple docstring""" _SCREAMING_SNAKE_CASE =self.get_image_processor() _SCREAMING_SNAKE_CASE =self.get_tokenizer() _SCREAMING_SNAKE_CASE =MgpstrProcessor(tokenizer=_a , image_processor=_a ) _SCREAMING_SNAKE_CASE =[[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9], [3, 4, 3, 1, 1, 8, 9]] _SCREAMING_SNAKE_CASE =processor.char_decode(_a ) _SCREAMING_SNAKE_CASE =tokenizer.batch_decode(_a ) _SCREAMING_SNAKE_CASE =[seq.replace(''' ''' , '''''' ) for seq in decoded_tok] self.assertListEqual(_a , _a ) def __UpperCamelCase ( self : Any ) -> List[Any]: """simple docstring""" _SCREAMING_SNAKE_CASE =self.get_image_processor() _SCREAMING_SNAKE_CASE =self.get_tokenizer() _SCREAMING_SNAKE_CASE =MgpstrProcessor(tokenizer=_a , image_processor=_a ) _SCREAMING_SNAKE_CASE =None _SCREAMING_SNAKE_CASE =self.prepare_image_inputs() _SCREAMING_SNAKE_CASE =processor(text=_a , images=_a ) self.assertListEqual(list(inputs.keys() ) , processor.model_input_names ) def __UpperCamelCase ( self : List[Any] ) -> Optional[int]: """simple docstring""" _SCREAMING_SNAKE_CASE =self.get_image_processor() _SCREAMING_SNAKE_CASE =self.get_tokenizer() _SCREAMING_SNAKE_CASE =MgpstrProcessor(tokenizer=_a , image_processor=_a ) _SCREAMING_SNAKE_CASE =torch.randn(1 , 27 , 38 ) _SCREAMING_SNAKE_CASE =torch.randn(1 , 27 , 5_0257 ) _SCREAMING_SNAKE_CASE =torch.randn(1 , 27 , 3_0522 ) _SCREAMING_SNAKE_CASE =processor.batch_decode([char_input, bpe_input, wp_input] ) self.assertListEqual(list(results.keys() ) , ['''generated_text''', '''scores''', '''char_preds''', '''bpe_preds''', '''wp_preds'''] )
691
0
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_tf_available, is_torch_available, is_vision_available, ) lowerCAmelCase_ : int = { '''configuration_blip''': [ '''BLIP_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''BlipConfig''', '''BlipTextConfig''', '''BlipVisionConfig''', ], '''processing_blip''': ['''BlipProcessor'''], } try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase_ : List[str] = ['''BlipImageProcessor'''] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase_ : Any = [ '''BLIP_PRETRAINED_MODEL_ARCHIVE_LIST''', '''BlipModel''', '''BlipPreTrainedModel''', '''BlipForConditionalGeneration''', '''BlipForQuestionAnswering''', '''BlipVisionModel''', '''BlipTextModel''', '''BlipForImageTextRetrieval''', ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase_ : Optional[int] = [ '''TF_BLIP_PRETRAINED_MODEL_ARCHIVE_LIST''', '''TFBlipModel''', '''TFBlipPreTrainedModel''', '''TFBlipForConditionalGeneration''', '''TFBlipForQuestionAnswering''', '''TFBlipVisionModel''', '''TFBlipTextModel''', '''TFBlipForImageTextRetrieval''', ] if TYPE_CHECKING: from .configuration_blip import BLIP_PRETRAINED_CONFIG_ARCHIVE_MAP, BlipConfig, BlipTextConfig, BlipVisionConfig from .processing_blip import BlipProcessor try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .image_processing_blip import BlipImageProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_blip import ( BLIP_PRETRAINED_MODEL_ARCHIVE_LIST, BlipForConditionalGeneration, BlipForImageTextRetrieval, BlipForQuestionAnswering, BlipModel, BlipPreTrainedModel, BlipTextModel, BlipVisionModel, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_blip import ( TF_BLIP_PRETRAINED_MODEL_ARCHIVE_LIST, TFBlipForConditionalGeneration, TFBlipForImageTextRetrieval, TFBlipForQuestionAnswering, TFBlipModel, TFBlipPreTrainedModel, TFBlipTextModel, TFBlipVisionModel, ) else: import sys lowerCAmelCase_ : Any = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
527
import requests from bsa import BeautifulSoup def lowerCamelCase( a__ = "https://www.worldometers.info/coronavirus"): _SCREAMING_SNAKE_CASE =BeautifulSoup(requests.get(a__).text ,'''html.parser''') _SCREAMING_SNAKE_CASE =soup.findAll('''h1''') _SCREAMING_SNAKE_CASE =soup.findAll('''div''' ,{'''class''': '''maincounter-number'''}) keys += soup.findAll('''span''' ,{'''class''': '''panel-title'''}) values += soup.findAll('''div''' ,{'''class''': '''number-table-main'''}) return {key.text.strip(): value.text.strip() for key, value in zip(a__ ,a__)} if __name__ == "__main__": print('''\033[1m''' + '''COVID-19 Status of the World''' + '''\033[0m\n''') for key, value in world_covidaa_stats().items(): print(f"""{key}\n{value}\n""")
691
0
from typing import List import datasets from datasets.tasks import AudioClassification from ..folder_based_builder import folder_based_builder _UpperCamelCase = datasets.utils.logging.get_logger(__name__) class _lowerCamelCase ( folder_based_builder.FolderBasedBuilderConfig ): """simple docstring""" UpperCAmelCase_ : int =None UpperCAmelCase_ : Optional[int] =None class _lowerCamelCase ( folder_based_builder.FolderBasedBuilder ): """simple docstring""" UpperCAmelCase_ : str =datasets.Audio() UpperCAmelCase_ : Optional[Any] ="audio" UpperCAmelCase_ : Dict =AudioFolderConfig UpperCAmelCase_ : Tuple =42 # definition at the bottom of the script UpperCAmelCase_ : int =AudioClassification(audio_column="audio" , label_column="label" ) _UpperCamelCase = [ '''.aiff''', '''.au''', '''.avr''', '''.caf''', '''.flac''', '''.htk''', '''.svx''', '''.mat4''', '''.mat5''', '''.mpc2k''', '''.ogg''', '''.paf''', '''.pvf''', '''.raw''', '''.rf64''', '''.sd2''', '''.sds''', '''.ircam''', '''.voc''', '''.w64''', '''.wav''', '''.nist''', '''.wavex''', '''.wve''', '''.xi''', '''.mp3''', '''.opus''', ] _UpperCamelCase = AUDIO_EXTENSIONS
243
def lowerCamelCase( a__ ,a__): return number | (1 << position) def lowerCamelCase( a__ ,a__): return number & ~(1 << position) def lowerCamelCase( a__ ,a__): return number ^ (1 << position) def lowerCamelCase( a__ ,a__): return ((number >> position) & 1) == 1 def lowerCamelCase( a__ ,a__): return int((number & (1 << position)) != 0) if __name__ == "__main__": import doctest doctest.testmod()
691
0
import numpy # List of input, output pairs lowerCamelCase__ = ( ((5, 2, 3), 15), ((6, 5, 9), 25), ((11, 12, 13), 41), ((1, 1, 1), 8), ((11, 12, 13), 41), ) lowerCamelCase__ = (((5_15, 22, 13), 5_55), ((61, 35, 49), 1_50)) lowerCamelCase__ = [2, 4, 1, 5] lowerCamelCase__ = len(train_data) lowerCamelCase__ = 0.0_09 def _lowerCamelCase( __snake_case , __snake_case="train" ) -> Union[str, Any]: return calculate_hypothesis_value(a__ , a__ ) - output( a__ , a__ ) def _lowerCamelCase( __snake_case ) -> Union[str, Any]: __snake_case = 0 for i in range(len(a__ ) - 1 ): hyp_val += data_input_tuple[i] * parameter_vector[i + 1] hyp_val += parameter_vector[0] return hyp_val def _lowerCamelCase( __snake_case , __snake_case ) -> Union[str, Any]: if data_set == "train": return train_data[example_no][1] elif data_set == "test": return test_data[example_no][1] return None def _lowerCamelCase( __snake_case , __snake_case ) -> int: if data_set == "train": return _hypothesis_value(train_data[example_no][0] ) elif data_set == "test": return _hypothesis_value(test_data[example_no][0] ) return None def _lowerCamelCase( __snake_case , __snake_case=m ) -> Dict: __snake_case = 0 for i in range(a__ ): if index == -1: summation_value += _error(a__ ) else: summation_value += _error(a__ ) * train_data[i][0][index] return summation_value def _lowerCamelCase( __snake_case ) -> Optional[Any]: __snake_case = summation_of_cost_derivative(a__ , a__ ) / m return cost_derivative_value def _lowerCamelCase( ) -> Union[str, Any]: global parameter_vector # Tune these values to set a tolerance value for predicted output __snake_case = 0.0_0_0_0_0_2 __snake_case = 0 __snake_case = 0 while True: j += 1 __snake_case = [0, 0, 0, 0] for i in range(0 , len(a__ ) ): __snake_case = get_cost_derivative(i - 1 ) __snake_case = ( parameter_vector[i] - LEARNING_RATE * cost_derivative ) if numpy.allclose( a__ , a__ , atol=a__ , rtol=a__ , ): break __snake_case = temp_parameter_vector print(("Number of iterations:", j) ) def _lowerCamelCase( ) -> Optional[Any]: for i in range(len(a__ ) ): print(("Actual output value:", output(a__ , "test" )) ) print(("Hypothesis output:", calculate_hypothesis_value(a__ , "test" )) ) if __name__ == "__main__": run_gradient_descent() print('\nTesting gradient descent for a linear hypothesis function.\n') test_gradient_descent()
524
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 A__ ( UpperCamelCase__ ): def __UpperCamelCase ( self : Tuple ) -> Dict: """simple docstring""" _SCREAMING_SNAKE_CASE =tempfile.mkdtemp() _SCREAMING_SNAKE_CASE =8 # DPR tok _SCREAMING_SNAKE_CASE =[ '''[UNK]''', '''[CLS]''', '''[SEP]''', '''[PAD]''', '''[MASK]''', '''want''', '''##want''', '''##ed''', '''wa''', '''un''', '''runn''', '''##ing''', ''',''', '''low''', '''lowest''', ] _SCREAMING_SNAKE_CASE =os.path.join(self.tmpdirname , '''dpr_tokenizer''' ) os.makedirs(_a , exist_ok=_a ) _SCREAMING_SNAKE_CASE =os.path.join(_a , 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 _SCREAMING_SNAKE_CASE =[ '''l''', '''o''', '''w''', '''e''', '''r''', '''s''', '''t''', '''i''', '''d''', '''n''', '''\u0120''', '''\u0120l''', '''\u0120n''', '''\u0120lo''', '''\u0120low''', '''er''', '''\u0120lowest''', '''\u0120newer''', '''\u0120wider''', '''<unk>''', ] _SCREAMING_SNAKE_CASE =dict(zip(_a , range(len(_a ) ) ) ) _SCREAMING_SNAKE_CASE =['''#version: 0.2''', '''\u0120 l''', '''\u0120l o''', '''\u0120lo w''', '''e r''', ''''''] _SCREAMING_SNAKE_CASE ={'''unk_token''': '''<unk>'''} _SCREAMING_SNAKE_CASE =os.path.join(self.tmpdirname , '''bart_tokenizer''' ) os.makedirs(_a , exist_ok=_a ) _SCREAMING_SNAKE_CASE =os.path.join(_a , BART_VOCAB_FILES_NAMES['''vocab_file'''] ) _SCREAMING_SNAKE_CASE =os.path.join(_a , BART_VOCAB_FILES_NAMES['''merges_file'''] ) with open(self.vocab_file , '''w''' , encoding='''utf-8''' ) as fp: fp.write(json.dumps(_a ) + '''\n''' ) with open(self.merges_file , '''w''' , encoding='''utf-8''' ) as fp: fp.write('''\n'''.join(_a ) ) def __UpperCamelCase ( self : List[str] ) -> DPRQuestionEncoderTokenizer: """simple docstring""" return DPRQuestionEncoderTokenizer.from_pretrained(os.path.join(self.tmpdirname , '''dpr_tokenizer''' ) ) def __UpperCamelCase ( self : Dict ) -> DPRContextEncoderTokenizer: """simple docstring""" return DPRContextEncoderTokenizer.from_pretrained(os.path.join(self.tmpdirname , '''dpr_tokenizer''' ) ) def __UpperCamelCase ( self : Union[str, Any] ) -> BartTokenizer: """simple docstring""" return BartTokenizer.from_pretrained(os.path.join(self.tmpdirname , '''bart_tokenizer''' ) ) def __UpperCamelCase ( self : Union[str, Any] ) -> int: """simple docstring""" shutil.rmtree(self.tmpdirname ) def __UpperCamelCase ( self : Union[str, Any] ) -> int: """simple docstring""" _SCREAMING_SNAKE_CASE =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 __UpperCamelCase ( self : str ) -> List[str]: """simple docstring""" _SCREAMING_SNAKE_CASE =self.get_dummy_dataset() _SCREAMING_SNAKE_CASE =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: _SCREAMING_SNAKE_CASE =dataset _SCREAMING_SNAKE_CASE =RagRetriever( _a , question_encoder_tokenizer=self.get_dpr_tokenizer() , generator_tokenizer=self.get_bart_tokenizer() , ) return retriever def __UpperCamelCase ( self : Optional[int] , _a : bool ) -> Any: """simple docstring""" _SCREAMING_SNAKE_CASE =self.get_dummy_dataset() _SCREAMING_SNAKE_CASE =RagConfig( retrieval_vector_size=self.retrieval_vector_size , question_encoder=DPRConfig().to_dict() , generator=BartConfig().to_dict() , index_name='''custom''' , ) if from_disk: _SCREAMING_SNAKE_CASE =os.path.join(self.tmpdirname , '''dataset''' ) _SCREAMING_SNAKE_CASE =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 _SCREAMING_SNAKE_CASE =RagRetriever( _a , question_encoder_tokenizer=self.get_dpr_tokenizer() , generator_tokenizer=self.get_bart_tokenizer() , ) else: _SCREAMING_SNAKE_CASE =RagRetriever( _a , question_encoder_tokenizer=self.get_dpr_tokenizer() , generator_tokenizer=self.get_bart_tokenizer() , index=CustomHFIndex(config.retrieval_vector_size , _a ) , ) return retriever def __UpperCamelCase ( self : Optional[Any] ) -> Dict: """simple docstring""" _SCREAMING_SNAKE_CASE =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 ) _SCREAMING_SNAKE_CASE =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''' ) ) _SCREAMING_SNAKE_CASE =os.path.join(self.tmpdirname , '''psgs_w100.tsv.pkl''' ) _SCREAMING_SNAKE_CASE ={sample['''id''']: [sample['''text'''], sample['''title''']] for sample in dataset} pickle.dump(_a , open(_a , '''wb''' ) ) _SCREAMING_SNAKE_CASE =RagConfig( retrieval_vector_size=self.retrieval_vector_size , question_encoder=DPRConfig().to_dict() , generator=BartConfig().to_dict() , index_name='''legacy''' , index_path=self.tmpdirname , ) _SCREAMING_SNAKE_CASE =RagRetriever( _a , question_encoder_tokenizer=self.get_dpr_tokenizer() , generator_tokenizer=self.get_bart_tokenizer() ) return retriever def __UpperCamelCase ( self : Tuple ) -> Optional[Any]: """simple docstring""" _SCREAMING_SNAKE_CASE =1 _SCREAMING_SNAKE_CASE =self.get_dummy_canonical_hf_index_retriever() _SCREAMING_SNAKE_CASE =np.array( [np.ones(self.retrieval_vector_size ), -np.ones(self.retrieval_vector_size )] , dtype=np.floataa ) _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE =retriever.retrieve(_a , n_docs=_a ) self.assertEqual(retrieved_doc_embeds.shape , (2, n_docs, self.retrieval_vector_size) ) self.assertEqual(len(_a ) , 2 ) self.assertEqual(sorted(doc_dicts[0] ) , ['''embeddings''', '''id''', '''text''', '''title'''] ) self.assertEqual(len(doc_dicts[0]['''id'''] ) , _a ) 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 __UpperCamelCase ( self : Any ) -> str: """simple docstring""" _SCREAMING_SNAKE_CASE =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: _SCREAMING_SNAKE_CASE =self.get_dummy_dataset() retriever.save_pretrained(_a ) _SCREAMING_SNAKE_CASE =RagRetriever.from_pretrained(_a ) self.assertIsInstance(_a , _a ) _SCREAMING_SNAKE_CASE =np.array( [np.ones(self.retrieval_vector_size ), -np.ones(self.retrieval_vector_size )] , dtype=np.floataa ) _SCREAMING_SNAKE_CASE =retriever.retrieve(_a , n_docs=1 ) self.assertTrue(out is not None ) def __UpperCamelCase ( self : Dict ) -> Union[str, Any]: """simple docstring""" _SCREAMING_SNAKE_CASE =1 _SCREAMING_SNAKE_CASE =self.get_dummy_custom_hf_index_retriever(from_disk=_a ) _SCREAMING_SNAKE_CASE =np.array( [np.ones(self.retrieval_vector_size ), -np.ones(self.retrieval_vector_size )] , dtype=np.floataa ) _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE =retriever.retrieve(_a , n_docs=_a ) self.assertEqual(retrieved_doc_embeds.shape , (2, n_docs, self.retrieval_vector_size) ) self.assertEqual(len(_a ) , 2 ) self.assertEqual(sorted(doc_dicts[0] ) , ['''embeddings''', '''id''', '''text''', '''title'''] ) self.assertEqual(len(doc_dicts[0]['''id'''] ) , _a ) 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 __UpperCamelCase ( self : Optional[Any] ) -> Any: """simple docstring""" _SCREAMING_SNAKE_CASE =self.get_dummy_custom_hf_index_retriever(from_disk=_a ) with tempfile.TemporaryDirectory() as tmp_dirname: retriever.save_pretrained(_a ) _SCREAMING_SNAKE_CASE =RagRetriever.from_pretrained(_a ) self.assertIsInstance(_a , _a ) _SCREAMING_SNAKE_CASE =np.array( [np.ones(self.retrieval_vector_size ), -np.ones(self.retrieval_vector_size )] , dtype=np.floataa ) _SCREAMING_SNAKE_CASE =retriever.retrieve(_a , n_docs=1 ) self.assertTrue(out is not None ) def __UpperCamelCase ( self : Dict ) -> Optional[int]: """simple docstring""" _SCREAMING_SNAKE_CASE =1 _SCREAMING_SNAKE_CASE =self.get_dummy_custom_hf_index_retriever(from_disk=_a ) _SCREAMING_SNAKE_CASE =np.array( [np.ones(self.retrieval_vector_size ), -np.ones(self.retrieval_vector_size )] , dtype=np.floataa ) _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE =retriever.retrieve(_a , n_docs=_a ) self.assertEqual(retrieved_doc_embeds.shape , (2, n_docs, self.retrieval_vector_size) ) self.assertEqual(len(_a ) , 2 ) self.assertEqual(sorted(doc_dicts[0] ) , ['''embeddings''', '''id''', '''text''', '''title'''] ) self.assertEqual(len(doc_dicts[0]['''id'''] ) , _a ) 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 __UpperCamelCase ( self : Tuple ) -> Optional[Any]: """simple docstring""" _SCREAMING_SNAKE_CASE =self.get_dummy_custom_hf_index_retriever(from_disk=_a ) with tempfile.TemporaryDirectory() as tmp_dirname: retriever.save_pretrained(_a ) _SCREAMING_SNAKE_CASE =RagRetriever.from_pretrained(_a ) self.assertIsInstance(_a , _a ) _SCREAMING_SNAKE_CASE =np.array( [np.ones(self.retrieval_vector_size ), -np.ones(self.retrieval_vector_size )] , dtype=np.floataa ) _SCREAMING_SNAKE_CASE =retriever.retrieve(_a , n_docs=1 ) self.assertTrue(out is not None ) def __UpperCamelCase ( self : Optional[int] ) -> Dict: """simple docstring""" _SCREAMING_SNAKE_CASE =1 _SCREAMING_SNAKE_CASE =self.get_dummy_legacy_index_retriever() _SCREAMING_SNAKE_CASE =np.array( [np.ones(self.retrieval_vector_size ), -np.ones(self.retrieval_vector_size )] , dtype=np.floataa ) _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE =retriever.retrieve(_a , n_docs=_a ) self.assertEqual(retrieved_doc_embeds.shape , (2, n_docs, self.retrieval_vector_size) ) self.assertEqual(len(_a ) , 2 ) self.assertEqual(sorted(doc_dicts[0] ) , ['''text''', '''title'''] ) self.assertEqual(len(doc_dicts[0]['''text'''] ) , _a ) 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 __UpperCamelCase ( self : Dict ) -> Dict: """simple docstring""" _SCREAMING_SNAKE_CASE =self.get_dummy_legacy_index_retriever() with tempfile.TemporaryDirectory() as tmp_dirname: retriever.save_pretrained(_a ) _SCREAMING_SNAKE_CASE =RagRetriever.from_pretrained(_a ) self.assertIsInstance(_a , _a ) _SCREAMING_SNAKE_CASE =np.array( [np.ones(self.retrieval_vector_size ), -np.ones(self.retrieval_vector_size )] , dtype=np.floataa ) _SCREAMING_SNAKE_CASE =retriever.retrieve(_a , n_docs=1 ) self.assertTrue(out is not None ) @require_torch @require_tokenizers @require_sentencepiece def __UpperCamelCase ( self : Optional[int] ) -> int: """simple docstring""" import torch _SCREAMING_SNAKE_CASE =1 _SCREAMING_SNAKE_CASE =self.get_dummy_canonical_hf_index_retriever() _SCREAMING_SNAKE_CASE =[[5, 7], [10, 11]] _SCREAMING_SNAKE_CASE =np.array( [np.ones(self.retrieval_vector_size ), -np.ones(self.retrieval_vector_size )] , dtype=np.floataa ) _SCREAMING_SNAKE_CASE =retriever(_a , _a , prefix=retriever.config.generator.prefix , n_docs=_a ) _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE =( 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(_a , _a ) self.assertIsInstance(_a , _a ) self.assertIsInstance(_a , np.ndarray ) _SCREAMING_SNAKE_CASE =retriever( _a , _a , prefix=retriever.config.generator.prefix , n_docs=_a , return_tensors='''pt''' , ) _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE =( # 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(_a , torch.Tensor ) self.assertIsInstance(_a , torch.Tensor ) self.assertIsInstance(_a , torch.Tensor ) @require_torch @require_tokenizers @require_sentencepiece def __UpperCamelCase ( self : str ) -> Any: """simple docstring""" _SCREAMING_SNAKE_CASE =self.get_dpr_ctx_encoder_tokenizer() _SCREAMING_SNAKE_CASE =1 _SCREAMING_SNAKE_CASE =self.get_dummy_custom_hf_index_retriever(from_disk=_a ) retriever.set_ctx_encoder_tokenizer(_a ) _SCREAMING_SNAKE_CASE =[[5, 7], [10, 11]] _SCREAMING_SNAKE_CASE =np.array( [np.ones(self.retrieval_vector_size ), -np.ones(self.retrieval_vector_size )] , dtype=np.floataa ) _SCREAMING_SNAKE_CASE =retriever(_a , _a , prefix=retriever.config.generator.prefix , n_docs=_a ) self.assertEqual( len(_a ) , 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''') ) , _a ) # check for doc token related keys in dictionary.
691
0
import argparse import os import torch from transformers import ( XLNetConfig, XLNetForQuestionAnswering, XLNetForSequenceClassification, XLNetLMHeadModel, load_tf_weights_in_xlnet, ) from transformers.utils import CONFIG_NAME, WEIGHTS_NAME, logging _UpperCAmelCase : Dict = { '''cola''': 2, '''mnli''': 3, '''mrpc''': 2, '''sst-2''': 2, '''sts-b''': 1, '''qqp''': 2, '''qnli''': 2, '''rte''': 2, '''wnli''': 2, } logging.set_verbosity_info() def __lowerCamelCase ( UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__=None ): '''simple docstring''' snake_case_ = XLNetConfig.from_json_file(a__ ) snake_case_ = finetuning_task.lower() if finetuning_task is not None else '' if finetuning_task in GLUE_TASKS_NUM_LABELS: print(F'''Building PyTorch XLNetForSequenceClassification model from configuration: {config}''' ) snake_case_ = finetuning_task snake_case_ = GLUE_TASKS_NUM_LABELS[finetuning_task] snake_case_ = XLNetForSequenceClassification(a__ ) elif "squad" in finetuning_task: snake_case_ = finetuning_task snake_case_ = XLNetForQuestionAnswering(a__ ) else: snake_case_ = XLNetLMHeadModel(a__ ) # Load weights from tf checkpoint load_tf_weights_in_xlnet(a__ , a__ , a__ ) # Save pytorch-model snake_case_ = os.path.join(a__ , a__ ) snake_case_ = os.path.join(a__ , a__ ) print(F'''Save PyTorch model to {os.path.abspath(a__ )}''' ) torch.save(model.state_dict() , a__ ) print(F'''Save configuration file to {os.path.abspath(a__ )}''' ) with open(a__ , 'w' , encoding='utf-8' ) as f: f.write(config.to_json_string() ) if __name__ == "__main__": _UpperCAmelCase : Dict = argparse.ArgumentParser() # Required parameters parser.add_argument( """--tf_checkpoint_path""", default=None, type=str, required=True, help="""Path to the TensorFlow checkpoint path.""" ) parser.add_argument( """--xlnet_config_file""", default=None, type=str, required=True, help=( """The config json file corresponding to the pre-trained XLNet model. \n""" """This specifies the model architecture.""" ), ) parser.add_argument( """--pytorch_dump_folder_path""", default=None, type=str, required=True, help="""Path to the folder to store the PyTorch model or dataset/vocab.""", ) parser.add_argument( """--finetuning_task""", default=None, type=str, help="""Name of a task on which the XLNet TensorFlow model was fine-tuned""", ) _UpperCAmelCase : int = parser.parse_args() print(args) convert_xlnet_checkpoint_to_pytorch( args.tf_checkpoint_path, args.xlnet_config_file, args.pytorch_dump_folder_path, args.finetuning_task )
362
import gc import random import unittest import numpy as np import torch from PIL import Image from transformers import XLMRobertaTokenizerFast from diffusers import DDIMScheduler, KandinskyImgaImgPipeline, KandinskyPriorPipeline, UNetaDConditionModel, VQModel from diffusers.pipelines.kandinsky.text_encoder import MCLIPConfig, MultilingualCLIP from diffusers.utils import floats_tensor, load_image, load_numpy, slow, torch_device from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu from ..test_pipelines_common import PipelineTesterMixin, assert_mean_pixel_difference enable_full_determinism() class A__ ( UpperCamelCase__ , unittest.TestCase ): UpperCAmelCase = KandinskyImgaImgPipeline UpperCAmelCase = ["prompt", "image_embeds", "negative_image_embeds", "image"] UpperCAmelCase = [ "prompt", "negative_prompt", "image_embeds", "negative_image_embeds", "image", ] UpperCAmelCase = [ "generator", "height", "width", "strength", "guidance_scale", "negative_prompt", "num_inference_steps", "return_dict", "guidance_scale", "num_images_per_prompt", "output_type", "return_dict", ] UpperCAmelCase = False @property def __UpperCamelCase ( self : Union[str, Any] ) -> Union[str, Any]: """simple docstring""" return 32 @property def __UpperCamelCase ( self : Optional[int] ) -> List[str]: """simple docstring""" return 32 @property def __UpperCamelCase ( self : int ) -> Tuple: """simple docstring""" return self.time_input_dim @property def __UpperCamelCase ( self : Tuple ) -> List[Any]: """simple docstring""" return self.time_input_dim * 4 @property def __UpperCamelCase ( self : Any ) -> Optional[Any]: """simple docstring""" return 100 @property def __UpperCamelCase ( self : Dict ) -> int: """simple docstring""" _SCREAMING_SNAKE_CASE =XLMRobertaTokenizerFast.from_pretrained('''YiYiXu/tiny-random-mclip-base''' ) return tokenizer @property def __UpperCamelCase ( self : Dict ) -> Optional[int]: """simple docstring""" torch.manual_seed(0 ) _SCREAMING_SNAKE_CASE =MCLIPConfig( numDims=self.cross_attention_dim , transformerDimensions=self.text_embedder_hidden_size , hidden_size=self.text_embedder_hidden_size , intermediate_size=37 , num_attention_heads=4 , num_hidden_layers=5 , vocab_size=1005 , ) _SCREAMING_SNAKE_CASE =MultilingualCLIP(_a ) _SCREAMING_SNAKE_CASE =text_encoder.eval() return text_encoder @property def __UpperCamelCase ( self : List[Any] ) -> List[str]: """simple docstring""" torch.manual_seed(0 ) _SCREAMING_SNAKE_CASE ={ '''in_channels''': 4, # Out channels is double in channels because predicts mean and variance '''out_channels''': 8, '''addition_embed_type''': '''text_image''', '''down_block_types''': ('''ResnetDownsampleBlock2D''', '''SimpleCrossAttnDownBlock2D'''), '''up_block_types''': ('''SimpleCrossAttnUpBlock2D''', '''ResnetUpsampleBlock2D'''), '''mid_block_type''': '''UNetMidBlock2DSimpleCrossAttn''', '''block_out_channels''': (self.block_out_channels_a, self.block_out_channels_a * 2), '''layers_per_block''': 1, '''encoder_hid_dim''': self.text_embedder_hidden_size, '''encoder_hid_dim_type''': '''text_image_proj''', '''cross_attention_dim''': self.cross_attention_dim, '''attention_head_dim''': 4, '''resnet_time_scale_shift''': '''scale_shift''', '''class_embed_type''': None, } _SCREAMING_SNAKE_CASE =UNetaDConditionModel(**_a ) return model @property def __UpperCamelCase ( self : List[Any] ) -> Union[str, Any]: """simple docstring""" return { "block_out_channels": [32, 64], "down_block_types": ["DownEncoderBlock2D", "AttnDownEncoderBlock2D"], "in_channels": 3, "latent_channels": 4, "layers_per_block": 1, "norm_num_groups": 8, "norm_type": "spatial", "num_vq_embeddings": 12, "out_channels": 3, "up_block_types": [ "AttnUpDecoderBlock2D", "UpDecoderBlock2D", ], "vq_embed_dim": 4, } @property def __UpperCamelCase ( self : List[Any] ) -> Optional[Any]: """simple docstring""" torch.manual_seed(0 ) _SCREAMING_SNAKE_CASE =VQModel(**self.dummy_movq_kwargs ) return model def __UpperCamelCase ( self : str ) -> int: """simple docstring""" _SCREAMING_SNAKE_CASE =self.dummy_text_encoder _SCREAMING_SNAKE_CASE =self.dummy_tokenizer _SCREAMING_SNAKE_CASE =self.dummy_unet _SCREAMING_SNAKE_CASE =self.dummy_movq _SCREAMING_SNAKE_CASE ={ '''num_train_timesteps''': 1000, '''beta_schedule''': '''linear''', '''beta_start''': 0.0_00_85, '''beta_end''': 0.0_12, '''clip_sample''': False, '''set_alpha_to_one''': False, '''steps_offset''': 0, '''prediction_type''': '''epsilon''', '''thresholding''': False, } _SCREAMING_SNAKE_CASE =DDIMScheduler(**_a ) _SCREAMING_SNAKE_CASE ={ '''text_encoder''': text_encoder, '''tokenizer''': tokenizer, '''unet''': unet, '''scheduler''': scheduler, '''movq''': movq, } return components def __UpperCamelCase ( self : str , _a : int , _a : int=0 ) -> Any: """simple docstring""" _SCREAMING_SNAKE_CASE =floats_tensor((1, self.cross_attention_dim) , rng=random.Random(_a ) ).to(_a ) _SCREAMING_SNAKE_CASE =floats_tensor((1, self.cross_attention_dim) , rng=random.Random(seed + 1 ) ).to(_a ) # create init_image _SCREAMING_SNAKE_CASE =floats_tensor((1, 3, 64, 64) , rng=random.Random(_a ) ).to(_a ) _SCREAMING_SNAKE_CASE =image.cpu().permute(0 , 2 , 3 , 1 )[0] _SCREAMING_SNAKE_CASE =Image.fromarray(np.uinta(_a ) ).convert('''RGB''' ).resize((256, 256) ) if str(_a ).startswith('''mps''' ): _SCREAMING_SNAKE_CASE =torch.manual_seed(_a ) else: _SCREAMING_SNAKE_CASE =torch.Generator(device=_a ).manual_seed(_a ) _SCREAMING_SNAKE_CASE ={ '''prompt''': '''horse''', '''image''': init_image, '''image_embeds''': image_embeds, '''negative_image_embeds''': negative_image_embeds, '''generator''': generator, '''height''': 64, '''width''': 64, '''num_inference_steps''': 10, '''guidance_scale''': 7.0, '''strength''': 0.2, '''output_type''': '''np''', } return inputs def __UpperCamelCase ( self : Any ) -> Optional[int]: """simple docstring""" _SCREAMING_SNAKE_CASE ='''cpu''' _SCREAMING_SNAKE_CASE =self.get_dummy_components() _SCREAMING_SNAKE_CASE =self.pipeline_class(**_a ) _SCREAMING_SNAKE_CASE =pipe.to(_a ) pipe.set_progress_bar_config(disable=_a ) _SCREAMING_SNAKE_CASE =pipe(**self.get_dummy_inputs(_a ) ) _SCREAMING_SNAKE_CASE =output.images _SCREAMING_SNAKE_CASE =pipe( **self.get_dummy_inputs(_a ) , return_dict=_a , )[0] _SCREAMING_SNAKE_CASE =image[0, -3:, -3:, -1] _SCREAMING_SNAKE_CASE =image_from_tuple[0, -3:, -3:, -1] assert image.shape == (1, 64, 64, 3) _SCREAMING_SNAKE_CASE =np.array( [0.61_47_49_43, 0.6_07_35_39, 0.43_30_85_44, 0.5_92_82_69, 0.47_49_35_95, 0.46_75_59_73, 0.4_61_38_38, 0.45_36_87_97, 0.50_11_92_33] ) assert ( np.abs(image_slice.flatten() - expected_slice ).max() < 1E-2 ), f" expected_slice {expected_slice}, but got {image_slice.flatten()}" assert ( np.abs(image_from_tuple_slice.flatten() - expected_slice ).max() < 1E-2 ), f" expected_slice {expected_slice}, but got {image_from_tuple_slice.flatten()}" @slow @require_torch_gpu class A__ ( unittest.TestCase ): def __UpperCamelCase ( self : Optional[Any] ) -> List[Any]: """simple docstring""" super().tearDown() gc.collect() torch.cuda.empty_cache() def __UpperCamelCase ( self : Dict ) -> Any: """simple docstring""" _SCREAMING_SNAKE_CASE =load_numpy( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main''' '''/kandinsky/kandinsky_img2img_frog.npy''' ) _SCREAMING_SNAKE_CASE =load_image( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main''' '''/kandinsky/cat.png''' ) _SCREAMING_SNAKE_CASE ='''A red cartoon frog, 4k''' _SCREAMING_SNAKE_CASE =KandinskyPriorPipeline.from_pretrained( '''kandinsky-community/kandinsky-2-1-prior''' , torch_dtype=torch.floataa ) pipe_prior.to(_a ) _SCREAMING_SNAKE_CASE =KandinskyImgaImgPipeline.from_pretrained( '''kandinsky-community/kandinsky-2-1''' , torch_dtype=torch.floataa ) _SCREAMING_SNAKE_CASE =pipeline.to(_a ) pipeline.set_progress_bar_config(disable=_a ) _SCREAMING_SNAKE_CASE =torch.Generator(device='''cpu''' ).manual_seed(0 ) _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE =pipe_prior( _a , generator=_a , num_inference_steps=5 , negative_prompt='''''' , ).to_tuple() _SCREAMING_SNAKE_CASE =pipeline( _a , image=_a , image_embeds=_a , negative_image_embeds=_a , generator=_a , num_inference_steps=100 , height=768 , width=768 , strength=0.2 , output_type='''np''' , ) _SCREAMING_SNAKE_CASE =output.images[0] assert image.shape == (768, 768, 3) assert_mean_pixel_difference(_a , _a )
691
0
"""simple docstring""" from __future__ import annotations from collections.abc import Sequence from typing import Literal def a__ ( lowerCAmelCase , lowerCAmelCase ) -> List[str]: UpperCAmelCase__ : Optional[Any] = list(a__ ) UpperCAmelCase__ : Optional[int] = list(a__ ) UpperCAmelCase__ : Tuple = 0 for i in range(len(a__ ) ): if lista[i] != lista[i]: count += 1 UpperCAmelCase__ : Any = """_""" if count > 1: return False else: return "".join(a__ ) def a__ ( lowerCAmelCase ) -> Union[str, Any]: UpperCAmelCase__ : Tuple = [] while True: UpperCAmelCase__ : Optional[Any] = ["""$"""] * len(a__ ) UpperCAmelCase__ : Optional[int] = [] for i in range(len(a__ ) ): for j in range(i + 1 , len(a__ ) ): UpperCAmelCase__ : str = compare_string(binary[i] , binary[j] ) if k is False: UpperCAmelCase__ : int = """*""" UpperCAmelCase__ : Dict = """*""" temp.append("""X""" ) for i in range(len(a__ ) ): if checka[i] == "$": pi.append(binary[i] ) if len(a__ ) == 0: return pi UpperCAmelCase__ : Dict = list(set(a__ ) ) def a__ ( lowerCAmelCase , lowerCAmelCase ) -> int: UpperCAmelCase__ : int = [] for minterm in minterms: UpperCAmelCase__ : str = """""" for _ in range(a__ ): UpperCAmelCase__ : List[str] = str(minterm % 2 ) + string minterm //= 2 temp.append(a__ ) return temp def a__ ( lowerCAmelCase , lowerCAmelCase , lowerCAmelCase ) -> Optional[Any]: UpperCAmelCase__ : str = list(a__ ) UpperCAmelCase__ : int = list(a__ ) UpperCAmelCase__ : int = 0 for i in range(len(a__ ) ): if lista[i] != lista[i]: count_n += 1 return count_n == count def a__ ( lowerCAmelCase , lowerCAmelCase ) -> str: UpperCAmelCase__ : Dict = [] UpperCAmelCase__ : List[Any] = [0] * len(a__ ) for i in range(len(chart[0] ) ): UpperCAmelCase__ : int = 0 UpperCAmelCase__ : List[Any] = -1 for j in range(len(a__ ) ): if chart[j][i] == 1: count += 1 UpperCAmelCase__ : List[str] = j if count == 1: UpperCAmelCase__ : int = 1 for i in range(len(a__ ) ): if select[i] == 1: for j in range(len(chart[0] ) ): if chart[i][j] == 1: for k in range(len(a__ ) ): UpperCAmelCase__ : Union[str, Any] = 0 temp.append(prime_implicants[i] ) while True: UpperCAmelCase__ : List[str] = 0 UpperCAmelCase__ : Optional[Any] = -1 UpperCAmelCase__ : List[Any] = 0 for i in range(len(a__ ) ): UpperCAmelCase__ : Optional[int] = chart[i].count(1 ) if count_n > max_n: UpperCAmelCase__ : List[str] = count_n UpperCAmelCase__ : Optional[Any] = i if max_n == 0: return temp temp.append(prime_implicants[rem] ) for i in range(len(chart[0] ) ): if chart[rem][i] == 1: for j in range(len(a__ ) ): UpperCAmelCase__ : Union[str, Any] = 0 def a__ ( lowerCAmelCase , lowerCAmelCase ) -> Optional[int]: UpperCAmelCase__ : Union[str, Any] = [[0 for x in range(len(a__ ) )] for x in range(len(a__ ) )] for i in range(len(a__ ) ): UpperCAmelCase__ : int = prime_implicants[i].count("""_""" ) for j in range(len(a__ ) ): if is_for_table(prime_implicants[i] , binary[j] , a__ ): UpperCAmelCase__ : int = 1 return chart def a__ ( ) -> Any: UpperCAmelCase__ : Union[str, Any] = int(input("""Enter the no. of variables\n""" ) ) UpperCAmelCase__ : str = [ float(a__ ) for x in input( """Enter the decimal representation of Minterms \'Spaces Separated\'\n""" ).split() ] UpperCAmelCase__ : List[str] = decimal_to_binary(a__ , a__ ) UpperCAmelCase__ : Tuple = check(a__ ) print("""Prime Implicants are:""" ) print(a__ ) UpperCAmelCase__ : Optional[Any] = prime_implicant_chart(a__ , a__ ) UpperCAmelCase__ : Union[str, Any] = selection(a__ , a__ ) print("""Essential Prime Implicants are:""" ) print(a__ ) if __name__ == "__main__": import doctest doctest.testmod() main()
182
import unittest import numpy as np from transformers.testing_utils import require_torch, require_vision from transformers.utils import is_torch_available, is_vision_available from ...test_image_processing_common import ImageProcessingSavingTestMixin if is_torch_available(): import torch if is_vision_available(): from PIL import Image from transformers import ChineseCLIPImageProcessor class A__ ( unittest.TestCase ): def __init__( self : List[str] , _a : Dict , _a : Dict=7 , _a : List[str]=3 , _a : str=18 , _a : Optional[int]=30 , _a : Tuple=400 , _a : Optional[Any]=True , _a : Dict=None , _a : str=True , _a : Tuple=None , _a : Any=True , _a : Any=[0.48_14_54_66, 0.4_57_82_75, 0.40_82_10_73] , _a : str=[0.26_86_29_54, 0.26_13_02_58, 0.27_57_77_11] , _a : List[Any]=True , ) -> Optional[int]: """simple docstring""" _SCREAMING_SNAKE_CASE =size if size is not None else {'''height''': 224, '''width''': 224} _SCREAMING_SNAKE_CASE =crop_size if crop_size is not None else {'''height''': 18, '''width''': 18} _SCREAMING_SNAKE_CASE =parent _SCREAMING_SNAKE_CASE =batch_size _SCREAMING_SNAKE_CASE =num_channels _SCREAMING_SNAKE_CASE =image_size _SCREAMING_SNAKE_CASE =min_resolution _SCREAMING_SNAKE_CASE =max_resolution _SCREAMING_SNAKE_CASE =do_resize _SCREAMING_SNAKE_CASE =size _SCREAMING_SNAKE_CASE =do_center_crop _SCREAMING_SNAKE_CASE =crop_size _SCREAMING_SNAKE_CASE =do_normalize _SCREAMING_SNAKE_CASE =image_mean _SCREAMING_SNAKE_CASE =image_std _SCREAMING_SNAKE_CASE =do_convert_rgb def __UpperCamelCase ( self : Any ) -> Tuple: """simple docstring""" return { "do_resize": self.do_resize, "size": self.size, "do_center_crop": self.do_center_crop, "crop_size": self.crop_size, "do_normalize": self.do_normalize, "image_mean": self.image_mean, "image_std": self.image_std, "do_convert_rgb": self.do_convert_rgb, } def __UpperCamelCase ( self : Tuple , _a : Optional[Any]=False , _a : str=False , _a : Dict=False ) -> Dict: """simple docstring""" assert not (numpify and torchify), "You cannot specify both numpy and PyTorch tensors at the same time" if equal_resolution: _SCREAMING_SNAKE_CASE =[] for i in range(self.batch_size ): image_inputs.append( np.random.randint( 255 , size=(self.num_channels, self.max_resolution, self.max_resolution) , dtype=np.uinta ) ) else: _SCREAMING_SNAKE_CASE =[] for i in range(self.batch_size ): _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE =np.random.choice(np.arange(self.min_resolution , self.max_resolution ) , 2 ) image_inputs.append(np.random.randint(255 , size=(self.num_channels, width, height) , dtype=np.uinta ) ) if not numpify and not torchify: # PIL expects the channel dimension as last dimension _SCREAMING_SNAKE_CASE =[Image.fromarray(np.moveaxis(_a , 0 , -1 ) ) for x in image_inputs] if torchify: _SCREAMING_SNAKE_CASE =[torch.from_numpy(_a ) for x in image_inputs] return image_inputs @require_torch @require_vision class A__ ( UpperCamelCase__ , unittest.TestCase ): UpperCAmelCase = ChineseCLIPImageProcessor if is_vision_available() else None def __UpperCamelCase ( self : Any ) -> Tuple: """simple docstring""" _SCREAMING_SNAKE_CASE =ChineseCLIPImageProcessingTester(self , do_center_crop=_a ) @property def __UpperCamelCase ( self : Union[str, Any] ) -> Tuple: """simple docstring""" return self.image_processor_tester.prepare_image_processor_dict() def __UpperCamelCase ( self : int ) -> Dict: """simple docstring""" _SCREAMING_SNAKE_CASE =self.image_processing_class(**self.image_processor_dict ) self.assertTrue(hasattr(_a , '''do_resize''' ) ) self.assertTrue(hasattr(_a , '''size''' ) ) self.assertTrue(hasattr(_a , '''do_center_crop''' ) ) self.assertTrue(hasattr(_a , '''center_crop''' ) ) self.assertTrue(hasattr(_a , '''do_normalize''' ) ) self.assertTrue(hasattr(_a , '''image_mean''' ) ) self.assertTrue(hasattr(_a , '''image_std''' ) ) self.assertTrue(hasattr(_a , '''do_convert_rgb''' ) ) def __UpperCamelCase ( self : List[str] ) -> str: """simple docstring""" _SCREAMING_SNAKE_CASE =self.image_processing_class.from_dict(self.image_processor_dict ) self.assertEqual(image_processor.size , {'''height''': 224, '''width''': 224} ) self.assertEqual(image_processor.crop_size , {'''height''': 18, '''width''': 18} ) _SCREAMING_SNAKE_CASE =self.image_processing_class.from_dict(self.image_processor_dict , size=42 , crop_size=84 ) self.assertEqual(image_processor.size , {'''shortest_edge''': 42} ) self.assertEqual(image_processor.crop_size , {'''height''': 84, '''width''': 84} ) def __UpperCamelCase ( self : Union[str, Any] ) -> int: """simple docstring""" pass def __UpperCamelCase ( self : str ) -> List[str]: """simple docstring""" _SCREAMING_SNAKE_CASE =self.image_processing_class(**self.image_processor_dict ) # create random PIL images _SCREAMING_SNAKE_CASE =self.image_processor_tester.prepare_inputs(equal_resolution=_a ) for image in image_inputs: self.assertIsInstance(_a , Image.Image ) # Test not batched input _SCREAMING_SNAKE_CASE =image_processing(image_inputs[0] , return_tensors='''pt''' ).pixel_values self.assertEqual( encoded_images.shape , ( 1, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size['''height'''], self.image_processor_tester.crop_size['''width'''], ) , ) # Test batched _SCREAMING_SNAKE_CASE =image_processing(_a , return_tensors='''pt''' ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size['''height'''], self.image_processor_tester.crop_size['''width'''], ) , ) def __UpperCamelCase ( self : Optional[Any] ) -> List[str]: """simple docstring""" _SCREAMING_SNAKE_CASE =self.image_processing_class(**self.image_processor_dict ) # create random numpy tensors _SCREAMING_SNAKE_CASE =self.image_processor_tester.prepare_inputs(equal_resolution=_a , numpify=_a ) for image in image_inputs: self.assertIsInstance(_a , np.ndarray ) # Test not batched input _SCREAMING_SNAKE_CASE =image_processing(image_inputs[0] , return_tensors='''pt''' ).pixel_values self.assertEqual( encoded_images.shape , ( 1, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size['''height'''], self.image_processor_tester.crop_size['''width'''], ) , ) # Test batched _SCREAMING_SNAKE_CASE =image_processing(_a , return_tensors='''pt''' ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size['''height'''], self.image_processor_tester.crop_size['''width'''], ) , ) def __UpperCamelCase ( self : Optional[int] ) -> Tuple: """simple docstring""" _SCREAMING_SNAKE_CASE =self.image_processing_class(**self.image_processor_dict ) # create random PyTorch tensors _SCREAMING_SNAKE_CASE =self.image_processor_tester.prepare_inputs(equal_resolution=_a , torchify=_a ) for image in image_inputs: self.assertIsInstance(_a , torch.Tensor ) # Test not batched input _SCREAMING_SNAKE_CASE =image_processing(image_inputs[0] , return_tensors='''pt''' ).pixel_values self.assertEqual( encoded_images.shape , ( 1, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size['''height'''], self.image_processor_tester.crop_size['''width'''], ) , ) # Test batched _SCREAMING_SNAKE_CASE =image_processing(_a , return_tensors='''pt''' ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size['''height'''], self.image_processor_tester.crop_size['''width'''], ) , ) @require_torch @require_vision class A__ ( UpperCamelCase__ , unittest.TestCase ): UpperCAmelCase = ChineseCLIPImageProcessor if is_vision_available() else None def __UpperCamelCase ( self : int ) -> str: """simple docstring""" _SCREAMING_SNAKE_CASE =ChineseCLIPImageProcessingTester(self , num_channels=4 , do_center_crop=_a ) _SCREAMING_SNAKE_CASE =3 @property def __UpperCamelCase ( self : Optional[int] ) -> Tuple: """simple docstring""" return self.image_processor_tester.prepare_image_processor_dict() def __UpperCamelCase ( self : int ) -> List[Any]: """simple docstring""" _SCREAMING_SNAKE_CASE =self.image_processing_class(**self.image_processor_dict ) self.assertTrue(hasattr(_a , '''do_resize''' ) ) self.assertTrue(hasattr(_a , '''size''' ) ) self.assertTrue(hasattr(_a , '''do_center_crop''' ) ) self.assertTrue(hasattr(_a , '''center_crop''' ) ) self.assertTrue(hasattr(_a , '''do_normalize''' ) ) self.assertTrue(hasattr(_a , '''image_mean''' ) ) self.assertTrue(hasattr(_a , '''image_std''' ) ) self.assertTrue(hasattr(_a , '''do_convert_rgb''' ) ) def __UpperCamelCase ( self : Any ) -> Union[str, Any]: """simple docstring""" pass def __UpperCamelCase ( self : Dict ) -> Any: """simple docstring""" _SCREAMING_SNAKE_CASE =self.image_processing_class(**self.image_processor_dict ) # create random PIL images _SCREAMING_SNAKE_CASE =self.image_processor_tester.prepare_inputs(equal_resolution=_a ) for image in image_inputs: self.assertIsInstance(_a , Image.Image ) # Test not batched input _SCREAMING_SNAKE_CASE =image_processing(image_inputs[0] , return_tensors='''pt''' ).pixel_values self.assertEqual( encoded_images.shape , ( 1, self.expected_encoded_image_num_channels, self.image_processor_tester.crop_size['''height'''], self.image_processor_tester.crop_size['''width'''], ) , ) # Test batched _SCREAMING_SNAKE_CASE =image_processing(_a , return_tensors='''pt''' ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.expected_encoded_image_num_channels, self.image_processor_tester.crop_size['''height'''], self.image_processor_tester.crop_size['''width'''], ) , )
691
0
"""simple docstring""" import logging import torch from torch import nn from torch.nn import CrossEntropyLoss, MSELoss from transformers.file_utils import add_start_docstrings, add_start_docstrings_to_model_forward from transformers.models.bert.modeling_bert import ( BERT_INPUTS_DOCSTRING, BERT_START_DOCSTRING, BertEncoder, BertModel, BertPreTrainedModel, ) UpperCAmelCase : Optional[Any] = logging.getLogger(__name__) class lowerCamelCase__ ( UpperCamelCase__ ): """simple docstring""" def lowerCamelCase__ ( self : Optional[int] , UpperCamelCase : Union[str, Any] , UpperCamelCase : List[str] , UpperCamelCase : List[Any]=None , UpperCamelCase : Optional[Any]=None ): '''simple docstring''' __UpperCAmelCase : Union[str, Any] = self.layer[current_layer](_a , _a , head_mask[current_layer] ) __UpperCAmelCase : List[Any] = layer_outputs[0] return hidden_states @add_start_docstrings( """The bare Bert Model transformer with PABEE outputting raw hidden-states without any specific head on top.""" , UpperCamelCase__ , ) class lowerCamelCase__ ( UpperCamelCase__ ): """simple docstring""" def __init__( self : List[str] , UpperCamelCase : Union[str, Any] ): '''simple docstring''' super().__init__(_a ) __UpperCAmelCase : List[Any] = BertEncoderWithPabee(_a ) self.init_weights() __UpperCAmelCase : Any = 0 __UpperCAmelCase : Any = 0 __UpperCAmelCase : List[Any] = 0 __UpperCAmelCase : Dict = 0 def lowerCamelCase__ ( self : List[str] , UpperCamelCase : Optional[int] ): '''simple docstring''' __UpperCAmelCase : Tuple = threshold def lowerCamelCase__ ( self : Dict , UpperCamelCase : int ): '''simple docstring''' __UpperCAmelCase : Optional[int] = patience def lowerCamelCase__ ( self : Optional[Any] ): '''simple docstring''' __UpperCAmelCase : Optional[Any] = 0 __UpperCAmelCase : str = 0 def lowerCamelCase__ ( self : Union[str, Any] ): '''simple docstring''' __UpperCAmelCase : List[Any] = self.inference_layers_num / self.inference_instances_num __UpperCAmelCase : List[Any] = ( f'''*** Patience = {self.patience} Avg. Inference Layers = {avg_inf_layers:.2f} Speed Up =''' f''' {1 - avg_inf_layers / self.config.num_hidden_layers:.2f} ***''' ) print(_a ) @add_start_docstrings_to_model_forward(_a ) def lowerCamelCase__ ( self : List[Any] , UpperCamelCase : Optional[Any]=None , UpperCamelCase : Optional[int]=None , UpperCamelCase : Any=None , UpperCamelCase : Union[str, Any]=None , UpperCamelCase : Union[str, Any]=None , UpperCamelCase : Union[str, Any]=None , UpperCamelCase : str=None , UpperCamelCase : Any=None , UpperCamelCase : str=None , UpperCamelCase : Optional[Any]=None , UpperCamelCase : Dict=False , ): '''simple docstring''' if input_ids is not None and inputs_embeds is not None: raise ValueError("""You cannot specify both input_ids and inputs_embeds at the same time""" ) elif input_ids is not None: __UpperCAmelCase : str = input_ids.size() elif inputs_embeds is not None: __UpperCAmelCase : Any = inputs_embeds.size()[:-1] else: raise ValueError("""You have to specify either input_ids or inputs_embeds""" ) __UpperCAmelCase : Optional[int] = input_ids.device if input_ids is not None else inputs_embeds.device if attention_mask is None: __UpperCAmelCase : List[Any] = torch.ones(_a , device=_a ) if token_type_ids is None: __UpperCAmelCase : Any = torch.zeros(_a , dtype=torch.long , device=_a ) # We can provide a self-attention mask of dimensions [batch_size, from_seq_length, to_seq_length] # ourselves in which case we just need to make it broadcastable to all heads. __UpperCAmelCase : Any = self.get_extended_attention_mask(_a , _a , _a ) # If a 2D ou 3D attention mask is provided for the cross-attention # we need to make broadcastable to [batch_size, num_heads, seq_length, seq_length] if self.config.is_decoder and encoder_hidden_states is not None: __UpperCAmelCase ,__UpperCAmelCase ,__UpperCAmelCase : List[Any] = encoder_hidden_states.size() __UpperCAmelCase : List[Any] = (encoder_batch_size, encoder_sequence_length) if encoder_attention_mask is None: __UpperCAmelCase : Optional[int] = torch.ones(_a , device=_a ) __UpperCAmelCase : Optional[Any] = self.invert_attention_mask(_a ) else: __UpperCAmelCase : Union[str, Any] = None # Prepare head mask if needed # 1.0 in head_mask indicate we keep the head # attention_probs has shape bsz x n_heads x N x N # input head_mask has shape [num_heads] or [num_hidden_layers x num_heads] # and head_mask is converted to shape [num_hidden_layers x batch x num_heads x seq_length x seq_length] __UpperCAmelCase : int = self.get_head_mask(_a , self.config.num_hidden_layers ) __UpperCAmelCase : Dict = self.embeddings( input_ids=_a , position_ids=_a , token_type_ids=_a , inputs_embeds=_a ) __UpperCAmelCase : Dict = embedding_output if self.training: __UpperCAmelCase : List[Any] = [] for i in range(self.config.num_hidden_layers ): __UpperCAmelCase : Dict = self.encoder.adaptive_forward( _a , current_layer=_a , attention_mask=_a , head_mask=_a ) __UpperCAmelCase : Tuple = self.pooler(_a ) __UpperCAmelCase : Union[str, Any] = output_layers[i](output_dropout(_a ) ) res.append(_a ) elif self.patience == 0: # Use all layers for inference __UpperCAmelCase : List[Any] = self.encoder( _a , attention_mask=_a , head_mask=_a , encoder_hidden_states=_a , encoder_attention_mask=_a , ) __UpperCAmelCase : Tuple = self.pooler(encoder_outputs[0] ) __UpperCAmelCase : List[Any] = [output_layers[self.config.num_hidden_layers - 1](_a )] else: __UpperCAmelCase : List[Any] = 0 __UpperCAmelCase : str = None __UpperCAmelCase : Union[str, Any] = 0 for i in range(self.config.num_hidden_layers ): calculated_layer_num += 1 __UpperCAmelCase : Union[str, Any] = self.encoder.adaptive_forward( _a , current_layer=_a , attention_mask=_a , head_mask=_a ) __UpperCAmelCase : str = self.pooler(_a ) __UpperCAmelCase : Any = output_layers[i](_a ) if regression: __UpperCAmelCase : Optional[int] = logits.detach() if patient_result is not None: __UpperCAmelCase : Tuple = patient_result.detach() if (patient_result is not None) and torch.abs(patient_result - labels ) < self.regression_threshold: patient_counter += 1 else: __UpperCAmelCase : Union[str, Any] = 0 else: __UpperCAmelCase : Optional[Any] = logits.detach().argmax(dim=1 ) if patient_result is not None: __UpperCAmelCase : Optional[Any] = patient_result.detach().argmax(dim=1 ) if (patient_result is not None) and torch.all(labels.eq(_a ) ): patient_counter += 1 else: __UpperCAmelCase : Any = 0 __UpperCAmelCase : Union[str, Any] = logits if patient_counter == self.patience: break __UpperCAmelCase : Dict = [patient_result] self.inference_layers_num += calculated_layer_num self.inference_instances_num += 1 return res @add_start_docstrings( """Bert Model transformer with PABEE and a sequence classification/regression head on top (a linear layer on top of\n the pooled output) e.g. for GLUE tasks. """ , UpperCamelCase__ , ) class lowerCamelCase__ ( UpperCamelCase__ ): """simple docstring""" def __init__( self : Optional[int] , UpperCamelCase : List[Any] ): '''simple docstring''' super().__init__(_a ) __UpperCAmelCase : Dict = config.num_labels __UpperCAmelCase : List[Any] = BertModelWithPabee(_a ) __UpperCAmelCase : List[Any] = nn.Dropout(config.hidden_dropout_prob ) __UpperCAmelCase : Optional[Any] = nn.ModuleList( [nn.Linear(config.hidden_size , self.config.num_labels ) for _ in range(config.num_hidden_layers )] ) self.init_weights() @add_start_docstrings_to_model_forward(_a ) def lowerCamelCase__ ( self : List[str] , UpperCamelCase : Optional[Any]=None , UpperCamelCase : List[Any]=None , UpperCamelCase : Union[str, Any]=None , UpperCamelCase : List[str]=None , UpperCamelCase : Dict=None , UpperCamelCase : Optional[Any]=None , UpperCamelCase : Optional[Any]=None , ): '''simple docstring''' __UpperCAmelCase : Any = self.bert( input_ids=_a , attention_mask=_a , token_type_ids=_a , position_ids=_a , head_mask=_a , inputs_embeds=_a , output_dropout=self.dropout , output_layers=self.classifiers , regression=self.num_labels == 1 , ) __UpperCAmelCase : str = (logits[-1],) if labels is not None: __UpperCAmelCase : Dict = None __UpperCAmelCase : Any = 0 for ix, logits_item in enumerate(_a ): if self.num_labels == 1: # We are doing regression __UpperCAmelCase : Any = MSELoss() __UpperCAmelCase : List[Any] = loss_fct(logits_item.view(-1 ) , labels.view(-1 ) ) else: __UpperCAmelCase : List[Any] = CrossEntropyLoss() __UpperCAmelCase : Any = loss_fct(logits_item.view(-1 , self.num_labels ) , labels.view(-1 ) ) if total_loss is None: __UpperCAmelCase : Tuple = loss else: total_loss += loss * (ix + 1) total_weights += ix + 1 __UpperCAmelCase : str = (total_loss / total_weights,) + outputs return outputs
139
def lowerCamelCase( a__ ,a__): return int((input_a, input_a).count(0) == 0) def lowerCamelCase( ): assert and_gate(0 ,0) == 0 assert and_gate(0 ,1) == 0 assert and_gate(1 ,0) == 0 assert and_gate(1 ,1) == 1 if __name__ == "__main__": test_and_gate() print(and_gate(1, 0)) print(and_gate(0, 0)) print(and_gate(0, 1)) print(and_gate(1, 1))
691
0
import unittest import numpy as np from transformers import is_flax_available from transformers.testing_utils import require_flax from ..test_modeling_flax_common import ids_tensor if is_flax_available(): import jax import jax.numpy as jnp from transformers.generation import ( FlaxForcedBOSTokenLogitsProcessor, FlaxForcedEOSTokenLogitsProcessor, FlaxLogitsProcessorList, FlaxMinLengthLogitsProcessor, FlaxTemperatureLogitsWarper, FlaxTopKLogitsWarper, FlaxTopPLogitsWarper, ) @require_flax class _UpperCamelCase ( unittest.TestCase ): '''simple docstring''' def _snake_case ( self : Dict , _lowerCamelCase : int , _lowerCamelCase : int ): '''simple docstring''' __lowerCamelCase : Optional[int] = jnp.ones((batch_size, length) ) / length return scores def _snake_case ( self : Any ): '''simple docstring''' __lowerCamelCase : Union[str, Any] = None __lowerCamelCase : Dict = 2_0 __lowerCamelCase : str = self._get_uniform_logits(batch_size=2 , length=_a ) # tweak scores to not be uniform anymore __lowerCamelCase : Optional[int] = scores.at[1, 5].set((1 / length) + 0.1 ) # peak, 1st batch __lowerCamelCase : Any = scores.at[1, 1_0].set((1 / length) - 0.4 ) # valley, 1st batch # compute softmax __lowerCamelCase : Optional[int] = jax.nn.softmax(_a , axis=-1 ) __lowerCamelCase : int = FlaxTemperatureLogitsWarper(temperature=0.5 ) __lowerCamelCase : int = FlaxTemperatureLogitsWarper(temperature=1.3 ) __lowerCamelCase : str = jax.nn.softmax(temp_dist_warper_sharper(_a , scores.copy() , cur_len=_a ) , axis=-1 ) __lowerCamelCase : List[str] = jax.nn.softmax(temp_dist_warper_smoother(_a , scores.copy() , cur_len=_a ) , axis=-1 ) # uniform distribution stays uniform self.assertTrue(jnp.allclose(probs[0, :] , warped_prob_sharp[0, :] , atol=1E-3 ) ) self.assertTrue(jnp.allclose(probs[0, :] , warped_prob_smooth[0, :] , atol=1E-3 ) ) # sharp peaks get higher, valleys get lower self.assertLess(probs[1, :].max() , warped_prob_sharp[1, :].max() ) self.assertGreater(probs[1, :].min() , warped_prob_sharp[1, :].min() ) # smooth peaks get lower, valleys get higher self.assertGreater(probs[1, :].max() , warped_prob_smooth[1, :].max() ) self.assertLess(probs[1, :].min() , warped_prob_smooth[1, :].min() ) def _snake_case ( self : Union[str, Any] ): '''simple docstring''' __lowerCamelCase : List[str] = None __lowerCamelCase : Union[str, Any] = 1_0 __lowerCamelCase : List[str] = 2 # create ramp distribution __lowerCamelCase : List[Any] = np.broadcast_to(np.arange(_a )[None, :] , (batch_size, vocab_size) ).copy() __lowerCamelCase : Any = ramp_logits[1:, : vocab_size // 2] + vocab_size __lowerCamelCase : int = FlaxTopKLogitsWarper(3 ) __lowerCamelCase : List[Any] = top_k_warp(_a , _a , cur_len=_a ) # check that correct tokens are filtered self.assertListEqual(jnp.isinf(scores[0] ).tolist() , 7 * [True] + 3 * [False] ) self.assertListEqual(jnp.isinf(scores[1] ).tolist() , 2 * [True] + 3 * [False] + 5 * [True] ) # check special case __lowerCamelCase : List[Any] = 5 __lowerCamelCase : int = FlaxTopKLogitsWarper(top_k=1 , filter_value=0.0 , min_tokens_to_keep=3 ) __lowerCamelCase : Optional[int] = np.broadcast_to(np.arange(_a )[None, :] , (batch_size, length) ).copy() __lowerCamelCase : Dict = top_k_warp_safety_check(_a , _a , cur_len=_a ) # min_tokens overwrites k: 3 tokens are kept => 2 tokens are nullified self.assertListEqual((scores == 0.0).sum(axis=-1 ).tolist() , [2, 2] ) def _snake_case ( self : Dict ): '''simple docstring''' __lowerCamelCase : Any = None __lowerCamelCase : int = 1_0 __lowerCamelCase : List[str] = 2 # create distribution and take log (inverse to Softmax as taken in TopPLogitsWarper) __lowerCamelCase : Union[str, Any] = np.log(np.array([[0.3, 0.1, 0.1, 0.5], [0.15, 0.3, 0.3, 0.25]] ) ) __lowerCamelCase : List[str] = FlaxTopPLogitsWarper(0.8 ) __lowerCamelCase : Optional[int] = np.exp(top_p_warp(_a , _a , cur_len=_a ) ) # dist should be filtered to keep min num values so that sum is >= top_p # exp (-inf) => 0 __lowerCamelCase : List[str] = np.array([[0.3, 0.0, 0.0, 0.5], [0.0, 0.3, 0.3, 0.25]] ) self.assertTrue(np.allclose(_a , _a , atol=1E-3 ) ) # check edge cases with negative and extreme logits __lowerCamelCase : List[Any] = np.broadcast_to(np.arange(_a )[None, :] , (batch_size, vocab_size) ).copy() - ( vocab_size // 2 ) # make ramp_logits more extreme __lowerCamelCase : Tuple = ramp_logits[1] * 1_0_0.0 # make sure at least 2 tokens are kept __lowerCamelCase : Union[str, Any] = FlaxTopPLogitsWarper(0.9 , min_tokens_to_keep=2 , filter_value=0.0 ) __lowerCamelCase : Union[str, Any] = top_p_warp(_a , _a , cur_len=_a ) # first batch should keep three tokens, second batch would keep only 1, but due to `min_tokens_to_keep=2` keeps 2. self.assertListEqual((filtered_dist != 0.0).sum(axis=-1 ).tolist() , [3, 2] ) def _snake_case ( self : Tuple ): '''simple docstring''' __lowerCamelCase : List[Any] = 2_0 __lowerCamelCase : Tuple = 4 __lowerCamelCase : List[Any] = 0 __lowerCamelCase : List[str] = FlaxMinLengthLogitsProcessor(min_length=1_0 , eos_token_id=_a ) # check that min length is applied at length 5 __lowerCamelCase : str = ids_tensor((batch_size, 2_0) , vocab_size=2_0 ) __lowerCamelCase : str = 5 __lowerCamelCase : str = self._get_uniform_logits(_a , _a ) __lowerCamelCase : Optional[Any] = min_dist_processor(_a , _a , cur_len=_a ) self.assertListEqual(scores_before_min_length[:, eos_token_id].tolist() , 4 * [-float("""inf""" )] ) # check that min length is not applied anymore at length 15 __lowerCamelCase : int = self._get_uniform_logits(_a , _a ) __lowerCamelCase : int = 1_5 __lowerCamelCase : Optional[Any] = min_dist_processor(_a , _a , cur_len=_a ) self.assertFalse(jnp.isinf(_a ).any() ) def _snake_case ( self : int ): '''simple docstring''' __lowerCamelCase : List[str] = 2_0 __lowerCamelCase : List[str] = 4 __lowerCamelCase : int = 0 __lowerCamelCase : Tuple = FlaxForcedBOSTokenLogitsProcessor(bos_token_id=_a ) # check that all scores are -inf except the bos_token_id score __lowerCamelCase : Optional[Any] = ids_tensor((batch_size, 1) , vocab_size=2_0 ) __lowerCamelCase : int = 1 __lowerCamelCase : Any = self._get_uniform_logits(_a , _a ) __lowerCamelCase : str = logits_processor(_a , _a , cur_len=_a ) self.assertTrue(jnp.isneginf(scores[:, bos_token_id + 1 :] ).all() ) self.assertListEqual(scores[:, bos_token_id].tolist() , 4 * [0] ) # score for bos_token_id shold be zero # check that bos_token_id is not forced if current length is greater than 1 __lowerCamelCase : List[str] = 3 __lowerCamelCase : str = self._get_uniform_logits(_a , _a ) __lowerCamelCase : Any = logits_processor(_a , _a , cur_len=_a ) self.assertFalse(jnp.isinf(_a ).any() ) def _snake_case ( self : Dict ): '''simple docstring''' __lowerCamelCase : Union[str, Any] = 2_0 __lowerCamelCase : Union[str, Any] = 4 __lowerCamelCase : Optional[int] = 0 __lowerCamelCase : int = 5 __lowerCamelCase : str = FlaxForcedEOSTokenLogitsProcessor(max_length=_a , eos_token_id=_a ) # check that all scores are -inf except the eos_token_id when max_length is reached __lowerCamelCase : List[str] = ids_tensor((batch_size, 4) , vocab_size=2_0 ) __lowerCamelCase : int = 4 __lowerCamelCase : List[str] = self._get_uniform_logits(_a , _a ) __lowerCamelCase : List[str] = logits_processor(_a , _a , cur_len=_a ) self.assertTrue(jnp.isneginf(scores[:, eos_token_id + 1 :] ).all() ) self.assertListEqual(scores[:, eos_token_id].tolist() , 4 * [0] ) # score for eos_token_id should be zero # check that eos_token_id is not forced if max_length is not reached __lowerCamelCase : Tuple = 3 __lowerCamelCase : Tuple = self._get_uniform_logits(_a , _a ) __lowerCamelCase : Tuple = logits_processor(_a , _a , cur_len=_a ) self.assertFalse(jnp.isinf(_a ).any() ) def _snake_case ( self : Optional[Any] ): '''simple docstring''' __lowerCamelCase : Any = 4 __lowerCamelCase : Optional[int] = 1_0 __lowerCamelCase : Dict = 1_5 __lowerCamelCase : Union[str, Any] = 2 __lowerCamelCase : str = 1 __lowerCamelCase : Any = 1_5 # dummy input_ids and scores __lowerCamelCase : List[Any] = ids_tensor((batch_size, sequence_length) , _a ) __lowerCamelCase : Tuple = input_ids.copy() __lowerCamelCase : Any = self._get_uniform_logits(_a , _a ) __lowerCamelCase : int = scores.copy() # instantiate all dist processors __lowerCamelCase : Optional[int] = FlaxTemperatureLogitsWarper(temperature=0.5 ) __lowerCamelCase : Tuple = FlaxTopKLogitsWarper(3 ) __lowerCamelCase : Dict = FlaxTopPLogitsWarper(0.8 ) # instantiate all logits processors __lowerCamelCase : List[Any] = FlaxMinLengthLogitsProcessor(min_length=1_0 , eos_token_id=_a ) __lowerCamelCase : Union[str, Any] = FlaxForcedBOSTokenLogitsProcessor(bos_token_id=_a ) __lowerCamelCase : Optional[int] = FlaxForcedEOSTokenLogitsProcessor(max_length=_a , eos_token_id=_a ) __lowerCamelCase : Optional[int] = 1_0 # no processor list __lowerCamelCase : List[Any] = temp_dist_warp(_a , _a , cur_len=_a ) __lowerCamelCase : Tuple = top_k_warp(_a , _a , cur_len=_a ) __lowerCamelCase : List[Any] = top_p_warp(_a , _a , cur_len=_a ) __lowerCamelCase : List[str] = min_dist_proc(_a , _a , cur_len=_a ) __lowerCamelCase : Union[str, Any] = bos_dist_proc(_a , _a , cur_len=_a ) __lowerCamelCase : Optional[int] = eos_dist_proc(_a , _a , cur_len=_a ) # with processor list __lowerCamelCase : Tuple = FlaxLogitsProcessorList( [temp_dist_warp, top_k_warp, top_p_warp, min_dist_proc, bos_dist_proc, eos_dist_proc] ) __lowerCamelCase : Dict = processor(_a , _a , cur_len=_a ) # scores should be equal self.assertTrue(jnp.allclose(_a , _a , atol=1E-3 ) ) # input_ids should never be changed self.assertListEqual(input_ids.tolist() , input_ids_comp.tolist() ) def _snake_case ( self : int ): '''simple docstring''' __lowerCamelCase : List[Any] = 4 __lowerCamelCase : str = 1_0 __lowerCamelCase : int = 1_5 __lowerCamelCase : Optional[Any] = 2 __lowerCamelCase : Dict = 1 __lowerCamelCase : Optional[int] = 1_5 # dummy input_ids and scores __lowerCamelCase : Any = ids_tensor((batch_size, sequence_length) , _a ) __lowerCamelCase : Optional[Any] = input_ids.copy() __lowerCamelCase : List[str] = self._get_uniform_logits(_a , _a ) __lowerCamelCase : Dict = scores.copy() # instantiate all dist processors __lowerCamelCase : Optional[int] = FlaxTemperatureLogitsWarper(temperature=0.5 ) __lowerCamelCase : Tuple = FlaxTopKLogitsWarper(3 ) __lowerCamelCase : Union[str, Any] = FlaxTopPLogitsWarper(0.8 ) # instantiate all logits processors __lowerCamelCase : int = FlaxMinLengthLogitsProcessor(min_length=1_0 , eos_token_id=_a ) __lowerCamelCase : List[str] = FlaxForcedBOSTokenLogitsProcessor(bos_token_id=_a ) __lowerCamelCase : Dict = FlaxForcedEOSTokenLogitsProcessor(max_length=_a , eos_token_id=_a ) __lowerCamelCase : Union[str, Any] = 1_0 # no processor list def run_no_processor_list(_lowerCamelCase : Optional[int] , _lowerCamelCase : List[str] , _lowerCamelCase : str ): __lowerCamelCase : List[Any] = temp_dist_warp(_a , _a , cur_len=_a ) __lowerCamelCase : Optional[Any] = top_k_warp(_a , _a , cur_len=_a ) __lowerCamelCase : Union[str, Any] = top_p_warp(_a , _a , cur_len=_a ) __lowerCamelCase : int = min_dist_proc(_a , _a , cur_len=_a ) __lowerCamelCase : Any = bos_dist_proc(_a , _a , cur_len=_a ) __lowerCamelCase : List[str] = eos_dist_proc(_a , _a , cur_len=_a ) return scores # with processor list def run_processor_list(_lowerCamelCase : str , _lowerCamelCase : Tuple , _lowerCamelCase : Optional[Any] ): __lowerCamelCase : str = FlaxLogitsProcessorList( [temp_dist_warp, top_k_warp, top_p_warp, min_dist_proc, bos_dist_proc, eos_dist_proc] ) __lowerCamelCase : List[str] = processor(_a , _a , cur_len=_a ) return scores __lowerCamelCase : List[str] = jax.jit(_a ) __lowerCamelCase : Union[str, Any] = jax.jit(_a ) __lowerCamelCase : List[str] = jitted_run_no_processor_list(_a , _a , _a ) __lowerCamelCase : List[Any] = jitted_run_processor_list(_a , _a , _a ) # scores should be equal self.assertTrue(jnp.allclose(_a , _a , atol=1E-3 ) ) # input_ids should never be changed self.assertListEqual(input_ids.tolist() , input_ids_comp.tolist() )
519
import argparse import os import sys from unittest.mock import patch import pytorch_lightning as pl import timeout_decorator import torch from distillation import SummarizationDistiller, distill_main from finetune import SummarizationModule, main from transformers import MarianMTModel from transformers.file_utils import cached_path from transformers.testing_utils import TestCasePlus, require_torch_gpu, slow from utils import load_json snake_case_ : Optional[int] = '''sshleifer/mar_enro_6_3_student''' class A__ ( UpperCamelCase__ ): def __UpperCamelCase ( self : Any ) -> Any: """simple docstring""" super().setUp() _SCREAMING_SNAKE_CASE =cached_path( '''https://cdn-datasets.huggingface.co/translation/wmt_en_ro-tr40k-va0.5k-te0.5k.tar.gz''' , extract_compressed_file=_a , ) _SCREAMING_SNAKE_CASE =f"{data_cached}/wmt_en_ro-tr40k-va0.5k-te0.5k" @slow @require_torch_gpu def __UpperCamelCase ( self : Optional[Any] ) -> Union[str, Any]: """simple docstring""" MarianMTModel.from_pretrained(_a ) @slow @require_torch_gpu def __UpperCamelCase ( self : str ) -> Dict: """simple docstring""" _SCREAMING_SNAKE_CASE ={ '''$MAX_LEN''': 64, '''$BS''': 64, '''$GAS''': 1, '''$ENRO_DIR''': self.data_dir, '''facebook/mbart-large-cc25''': MARIAN_MODEL, # "val_check_interval=0.25": "val_check_interval=1.0", '''--learning_rate=3e-5''': '''--learning_rate 3e-4''', '''--num_train_epochs 6''': '''--num_train_epochs 1''', } # Clean up bash script _SCREAMING_SNAKE_CASE =(self.test_file_dir / '''train_mbart_cc25_enro.sh''').open().read().split('''finetune.py''' )[1].strip() _SCREAMING_SNAKE_CASE =bash_script.replace('''\\\n''' , '''''' ).strip().replace('''"$@"''' , '''''' ) for k, v in env_vars_to_replace.items(): _SCREAMING_SNAKE_CASE =bash_script.replace(_a , str(_a ) ) _SCREAMING_SNAKE_CASE =self.get_auto_remove_tmp_dir() # bash_script = bash_script.replace("--fp16 ", "") _SCREAMING_SNAKE_CASE =f"\n --output_dir {output_dir}\n --tokenizer_name Helsinki-NLP/opus-mt-en-ro\n --sortish_sampler\n --do_predict\n --gpus 1\n --freeze_encoder\n --n_train 40000\n --n_val 500\n --n_test 500\n --fp16_opt_level O1\n --num_sanity_val_steps 0\n --eval_beams 2\n ".split() # XXX: args.gpus > 1 : handle multi_gpu in the future _SCREAMING_SNAKE_CASE =['''finetune.py'''] + bash_script.split() + args with patch.object(_a , '''argv''' , _a ): _SCREAMING_SNAKE_CASE =argparse.ArgumentParser() _SCREAMING_SNAKE_CASE =pl.Trainer.add_argparse_args(_a ) _SCREAMING_SNAKE_CASE =SummarizationModule.add_model_specific_args(_a , os.getcwd() ) _SCREAMING_SNAKE_CASE =parser.parse_args() _SCREAMING_SNAKE_CASE =main(_a ) # Check metrics _SCREAMING_SNAKE_CASE =load_json(model.metrics_save_path ) _SCREAMING_SNAKE_CASE =metrics['''val'''][0] _SCREAMING_SNAKE_CASE =metrics['''val'''][-1] self.assertEqual(len(metrics['''val'''] ) , (args.max_epochs / args.val_check_interval) ) assert isinstance(last_step_stats[f"val_avg_{model.val_metric}"] , _a ) self.assertGreater(last_step_stats['''val_avg_gen_time'''] , 0.01 ) # model hanging on generate. Maybe bad config was saved. (XXX: old comment/assert?) self.assertLessEqual(last_step_stats['''val_avg_gen_time'''] , 1.0 ) # test learning requirements: # 1. BLEU improves over the course of training by more than 2 pts self.assertGreater(last_step_stats['''val_avg_bleu'''] - first_step_stats['''val_avg_bleu'''] , 2 ) # 2. BLEU finishes above 17 self.assertGreater(last_step_stats['''val_avg_bleu'''] , 17 ) # 3. test BLEU and val BLEU within ~1.1 pt. self.assertLess(abs(metrics['''val'''][-1]['''val_avg_bleu'''] - metrics['''test'''][-1]['''test_avg_bleu'''] ) , 1.1 ) # check lightning ckpt can be loaded and has a reasonable statedict _SCREAMING_SNAKE_CASE =os.listdir(_a ) _SCREAMING_SNAKE_CASE =[x for x in contents if x.endswith('''.ckpt''' )][0] _SCREAMING_SNAKE_CASE =os.path.join(args.output_dir , _a ) _SCREAMING_SNAKE_CASE =torch.load(_a , map_location='''cpu''' ) _SCREAMING_SNAKE_CASE ='''model.model.decoder.layers.0.encoder_attn_layer_norm.weight''' assert expected_key in ckpt["state_dict"] assert ckpt["state_dict"]["model.model.decoder.layers.0.encoder_attn_layer_norm.weight"].dtype == torch.floataa # TODO: turn on args.do_predict when PL bug fixed. if args.do_predict: _SCREAMING_SNAKE_CASE ={os.path.basename(_a ) for p in contents} assert "test_generations.txt" in contents assert "test_results.txt" in contents # assert len(metrics["val"]) == desired_n_evals assert len(metrics['''test'''] ) == 1 class A__ ( UpperCamelCase__ ): @timeout_decorator.timeout(600 ) @slow @require_torch_gpu def __UpperCamelCase ( self : Union[str, Any] ) -> str: """simple docstring""" _SCREAMING_SNAKE_CASE =f"{self.test_file_dir_str}/test_data/wmt_en_ro" _SCREAMING_SNAKE_CASE ={ '''--fp16_opt_level=O1''': '''''', '''$MAX_LEN''': 128, '''$BS''': 16, '''$GAS''': 1, '''$ENRO_DIR''': data_dir, '''$m''': '''sshleifer/student_marian_en_ro_6_1''', '''val_check_interval=0.25''': '''val_check_interval=1.0''', } # Clean up bash script _SCREAMING_SNAKE_CASE =( (self.test_file_dir / '''distil_marian_no_teacher.sh''').open().read().split('''distillation.py''' )[1].strip() ) _SCREAMING_SNAKE_CASE =bash_script.replace('''\\\n''' , '''''' ).strip().replace('''"$@"''' , '''''' ) _SCREAMING_SNAKE_CASE =bash_script.replace('''--fp16 ''' , ''' ''' ) for k, v in env_vars_to_replace.items(): _SCREAMING_SNAKE_CASE =bash_script.replace(_a , str(_a ) ) _SCREAMING_SNAKE_CASE =self.get_auto_remove_tmp_dir() _SCREAMING_SNAKE_CASE =bash_script.replace('''--fp16''' , '''''' ) _SCREAMING_SNAKE_CASE =6 _SCREAMING_SNAKE_CASE =( ['''distillation.py'''] + bash_script.split() + [ f"--output_dir={output_dir}", '''--gpus=1''', '''--learning_rate=1e-3''', f"--num_train_epochs={epochs}", '''--warmup_steps=10''', '''--val_check_interval=1.0''', '''--do_predict''', ] ) with patch.object(_a , '''argv''' , _a ): _SCREAMING_SNAKE_CASE =argparse.ArgumentParser() _SCREAMING_SNAKE_CASE =pl.Trainer.add_argparse_args(_a ) _SCREAMING_SNAKE_CASE =SummarizationDistiller.add_model_specific_args(_a , os.getcwd() ) _SCREAMING_SNAKE_CASE =parser.parse_args() # assert args.gpus == gpus THIS BREAKS for multi_gpu _SCREAMING_SNAKE_CASE =distill_main(_a ) # Check metrics _SCREAMING_SNAKE_CASE =load_json(model.metrics_save_path ) _SCREAMING_SNAKE_CASE =metrics['''val'''][0] _SCREAMING_SNAKE_CASE =metrics['''val'''][-1] assert len(metrics['''val'''] ) >= (args.max_epochs / args.val_check_interval) # +1 accounts for val_sanity_check assert last_step_stats["val_avg_gen_time"] >= 0.01 assert first_step_stats["val_avg_bleu"] < last_step_stats["val_avg_bleu"] # model learned nothing assert 1.0 >= last_step_stats["val_avg_gen_time"] # model hanging on generate. Maybe bad config was saved. assert isinstance(last_step_stats[f"val_avg_{model.val_metric}"] , _a ) # check lightning ckpt can be loaded and has a reasonable statedict _SCREAMING_SNAKE_CASE =os.listdir(_a ) _SCREAMING_SNAKE_CASE =[x for x in contents if x.endswith('''.ckpt''' )][0] _SCREAMING_SNAKE_CASE =os.path.join(args.output_dir , _a ) _SCREAMING_SNAKE_CASE =torch.load(_a , map_location='''cpu''' ) _SCREAMING_SNAKE_CASE ='''model.model.decoder.layers.0.encoder_attn_layer_norm.weight''' assert expected_key in ckpt["state_dict"] assert ckpt["state_dict"]["model.model.decoder.layers.0.encoder_attn_layer_norm.weight"].dtype == torch.floataa # TODO: turn on args.do_predict when PL bug fixed. if args.do_predict: _SCREAMING_SNAKE_CASE ={os.path.basename(_a ) for p in contents} assert "test_generations.txt" in contents assert "test_results.txt" in contents # assert len(metrics["val"]) == desired_n_evals assert len(metrics['''test'''] ) == 1
691
0
from typing import TYPE_CHECKING from ....utils import _LazyModule SCREAMING_SNAKE_CASE : Dict = {'''tokenization_tapex''': ['''TapexTokenizer''']} if TYPE_CHECKING: from .tokenization_tapex import TapexTokenizer else: import sys SCREAMING_SNAKE_CASE : Union[str, Any] = _LazyModule(__name__, globals()["""__file__"""], _import_structure)
197
import inspect import os import unittest from dataclasses import dataclass import torch from accelerate import Accelerator, DistributedDataParallelKwargs, GradScalerKwargs from accelerate.state import AcceleratorState from accelerate.test_utils import execute_subprocess_async, require_cuda, require_multi_gpu from accelerate.utils import KwargsHandler @dataclass class A__ ( UpperCamelCase__ ): UpperCAmelCase = 0 UpperCAmelCase = False UpperCAmelCase = 3.0 class A__ ( unittest.TestCase ): def __UpperCamelCase ( self : Optional[Any] ) -> Optional[Any]: """simple docstring""" self.assertDictEqual(MockClass().to_kwargs() , {} ) self.assertDictEqual(MockClass(a=2 ).to_kwargs() , {'''a''': 2} ) self.assertDictEqual(MockClass(a=2 , b=_a ).to_kwargs() , {'''a''': 2, '''b''': True} ) self.assertDictEqual(MockClass(a=2 , c=2.25 ).to_kwargs() , {'''a''': 2, '''c''': 2.25} ) @require_cuda def __UpperCamelCase ( self : Optional[Any] ) -> Dict: """simple docstring""" _SCREAMING_SNAKE_CASE =GradScalerKwargs(init_scale=1024 , growth_factor=2 ) AcceleratorState._reset_state() _SCREAMING_SNAKE_CASE =Accelerator(mixed_precision='''fp16''' , kwargs_handlers=[scaler_handler] ) print(accelerator.use_fpaa ) _SCREAMING_SNAKE_CASE =accelerator.scaler # Check the kwargs have been applied self.assertEqual(scaler._init_scale , 10_24.0 ) self.assertEqual(scaler._growth_factor , 2.0 ) # Check the other values are at the default self.assertEqual(scaler._backoff_factor , 0.5 ) self.assertEqual(scaler._growth_interval , 2000 ) self.assertEqual(scaler._enabled , _a ) @require_multi_gpu def __UpperCamelCase ( self : str ) -> Tuple: """simple docstring""" _SCREAMING_SNAKE_CASE =['''torchrun''', f"--nproc_per_node={torch.cuda.device_count()}", inspect.getfile(self.__class__ )] execute_subprocess_async(_a , env=os.environ.copy() ) if __name__ == "__main__": snake_case_ : Optional[Any] = DistributedDataParallelKwargs(bucket_cap_mb=15, find_unused_parameters=True) snake_case_ : List[str] = Accelerator(kwargs_handlers=[ddp_scaler]) snake_case_ : Dict = torch.nn.Linear(1_00, 2_00) snake_case_ : List[Any] = accelerator.prepare(model) # Check the values changed in kwargs snake_case_ : Dict = '''''' snake_case_ : str = model.bucket_bytes_cap // (10_24 * 10_24) if observed_bucket_cap_map != 15: error_msg += f"Kwargs badly passed, should have `15` but found {observed_bucket_cap_map}.\n" if model.find_unused_parameters is not True: error_msg += f"Kwargs badly passed, should have `True` but found {model.find_unused_parameters}.\n" # Check the values of the defaults if model.dim != 0: error_msg += f"Default value not respected, should have `0` but found {model.dim}.\n" if model.broadcast_buffers is not True: error_msg += f"Default value not respected, should have `True` but found {model.broadcast_buffers}.\n" if model.gradient_as_bucket_view is not False: error_msg += f"Default value not respected, should have `False` but found {model.gradient_as_bucket_view}.\n" # Raise error at the end to make sure we don't stop at the first failure. if len(error_msg) > 0: raise ValueError(error_msg)
691
0
from .glue import glue_convert_examples_to_features, glue_output_modes, glue_processors, glue_tasks_num_labels from .squad import SquadExample, SquadFeatures, SquadVaProcessor, SquadVaProcessor, squad_convert_examples_to_features from .utils import DataProcessor, InputExample, InputFeatures, SingleSentenceClassificationProcessor from .xnli import xnli_output_modes, xnli_processors, xnli_tasks_num_labels
117
class A__ : def __init__( self : List[str] ) -> List[str]: """simple docstring""" _SCREAMING_SNAKE_CASE =0 _SCREAMING_SNAKE_CASE =0 _SCREAMING_SNAKE_CASE ={} def __UpperCamelCase ( self : Any , _a : Union[str, Any] ) -> Optional[Any]: """simple docstring""" if vertex not in self.adjacency: _SCREAMING_SNAKE_CASE ={} self.num_vertices += 1 def __UpperCamelCase ( self : Optional[int] , _a : Tuple , _a : Tuple , _a : Dict ) -> Union[str, Any]: """simple docstring""" self.add_vertex(_a ) self.add_vertex(_a ) if head == tail: return _SCREAMING_SNAKE_CASE =weight _SCREAMING_SNAKE_CASE =weight def __UpperCamelCase ( self : Dict ) -> int: """simple docstring""" _SCREAMING_SNAKE_CASE =self.get_edges() for edge in edges: _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE =edge edges.remove((tail, head, weight) ) for i in range(len(_a ) ): _SCREAMING_SNAKE_CASE =list(edges[i] ) edges.sort(key=lambda _a : e[2] ) for i in range(len(_a ) - 1 ): if edges[i][2] >= edges[i + 1][2]: _SCREAMING_SNAKE_CASE =edges[i][2] + 1 for edge in edges: _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE =edge _SCREAMING_SNAKE_CASE =weight _SCREAMING_SNAKE_CASE =weight def __str__( self : str ) -> List[str]: """simple docstring""" _SCREAMING_SNAKE_CASE ='''''' for tail in self.adjacency: for head in self.adjacency[tail]: _SCREAMING_SNAKE_CASE =self.adjacency[head][tail] string += f"{head} -> {tail} == {weight}\n" return string.rstrip('''\n''' ) def __UpperCamelCase ( self : int ) -> Optional[Any]: """simple docstring""" _SCREAMING_SNAKE_CASE =[] for tail in self.adjacency: for head in self.adjacency[tail]: output.append((tail, head, self.adjacency[head][tail]) ) return output def __UpperCamelCase ( self : Any ) -> Any: """simple docstring""" return self.adjacency.keys() @staticmethod def __UpperCamelCase ( _a : List[str]=None , _a : Optional[int]=None ) -> Optional[Any]: """simple docstring""" _SCREAMING_SNAKE_CASE =Graph() if vertices is None: _SCREAMING_SNAKE_CASE =[] if edges is None: _SCREAMING_SNAKE_CASE =[] for vertex in vertices: g.add_vertex(_a ) for edge in edges: g.add_edge(*_a ) return g class A__ : def __init__( self : List[Any] ) -> Union[str, Any]: """simple docstring""" _SCREAMING_SNAKE_CASE ={} _SCREAMING_SNAKE_CASE ={} def __len__( self : Optional[int] ) -> Tuple: """simple docstring""" return len(self.parent ) def __UpperCamelCase ( self : Dict , _a : Optional[Any] ) -> int: """simple docstring""" if item in self.parent: return self.find(_a ) _SCREAMING_SNAKE_CASE =item _SCREAMING_SNAKE_CASE =0 return item def __UpperCamelCase ( self : str , _a : Tuple ) -> Union[str, Any]: """simple docstring""" if item not in self.parent: return self.make_set(_a ) if item != self.parent[item]: _SCREAMING_SNAKE_CASE =self.find(self.parent[item] ) return self.parent[item] def __UpperCamelCase ( self : Dict , _a : Optional[int] , _a : List[Any] ) -> List[str]: """simple docstring""" _SCREAMING_SNAKE_CASE =self.find(_a ) _SCREAMING_SNAKE_CASE =self.find(_a ) if roota == roota: return roota if self.rank[roota] > self.rank[roota]: _SCREAMING_SNAKE_CASE =roota return roota if self.rank[roota] < self.rank[roota]: _SCREAMING_SNAKE_CASE =roota return roota if self.rank[roota] == self.rank[roota]: self.rank[roota] += 1 _SCREAMING_SNAKE_CASE =roota return roota return None @staticmethod def __UpperCamelCase ( _a : int ) -> Union[str, Any]: """simple docstring""" _SCREAMING_SNAKE_CASE =graph.num_vertices _SCREAMING_SNAKE_CASE =Graph.UnionFind() _SCREAMING_SNAKE_CASE =[] while num_components > 1: _SCREAMING_SNAKE_CASE ={} for vertex in graph.get_vertices(): _SCREAMING_SNAKE_CASE =-1 _SCREAMING_SNAKE_CASE =graph.get_edges() for edge in edges: _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE =edge edges.remove((tail, head, weight) ) for edge in edges: _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE =edge _SCREAMING_SNAKE_CASE =union_find.find(_a ) _SCREAMING_SNAKE_CASE =union_find.find(_a ) if seta != seta: if cheap_edge[seta] == -1 or cheap_edge[seta][2] > weight: _SCREAMING_SNAKE_CASE =[head, tail, weight] if cheap_edge[seta] == -1 or cheap_edge[seta][2] > weight: _SCREAMING_SNAKE_CASE =[head, tail, weight] for vertex in cheap_edge: if cheap_edge[vertex] != -1: _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE =cheap_edge[vertex] if union_find.find(_a ) != union_find.find(_a ): union_find.union(_a , _a ) mst_edges.append(cheap_edge[vertex] ) _SCREAMING_SNAKE_CASE =num_components - 1 _SCREAMING_SNAKE_CASE =Graph.build(edges=_a ) return mst
691
0
import argparse import glob import logging import os import sys import time from collections import defaultdict from pathlib import Path from typing import Dict, List, Tuple import numpy as np import pytorch_lightning as pl import torch from callbacks import SeqaSeqLoggingCallback, get_checkpoint_callback, get_early_stopping_callback from torch import nn from torch.utils.data import DataLoader from transformers import MBartTokenizer, TaForConditionalGeneration from transformers.models.bart.modeling_bart import shift_tokens_right from utils import ( ROUGE_KEYS, LegacySeqaSeqDataset, SeqaSeqDataset, assert_all_frozen, calculate_bleu, calculate_rouge, check_output_dir, flatten_list, freeze_embeds, freeze_params, get_git_info, label_smoothed_nll_loss, lmap, pickle_save, save_git_info, save_json, use_task_specific_params, ) # need the parent dir module sys.path.insert(2, str(Path(__file__).resolve().parents[1])) from lightning_base import BaseTransformer, add_generic_args, generic_train # noqa A : Tuple = logging.getLogger(__name__) class UpperCamelCase( UpperCamelCase__ ): snake_case_ : Tuple = """summarization""" snake_case_ : Dict = ["""loss"""] snake_case_ : List[str] = ROUGE_KEYS snake_case_ : List[Any] = """rouge2""" def __init__( self : List[str] , SCREAMING_SNAKE_CASE : Any , **SCREAMING_SNAKE_CASE : Union[str, Any] ) -> Optional[int]: '''simple docstring''' if hparams.sortish_sampler and hparams.gpus > 1: __snake_case = False elif hparams.max_tokens_per_batch is not None: if hparams.gpus > 1: raise NotImplementedError("Dynamic Batch size does not work for multi-gpu training" ) if hparams.sortish_sampler: raise ValueError("--sortish_sampler and --max_tokens_per_batch may not be used simultaneously" ) super().__init__(_a , num_labels=_a , mode=self.mode , **_a ) use_task_specific_params(self.model , "summarization" ) save_git_info(self.hparams.output_dir ) __snake_case = Path(self.output_dir ) / "metrics.json" __snake_case = Path(self.output_dir ) / "hparams.pkl" pickle_save(self.hparams , self.hparams_save_path ) __snake_case = 0 __snake_case = defaultdict(_a ) __snake_case = self.config.model_type __snake_case = self.config.tgt_vocab_size if self.model_type == "fsmt" else self.config.vocab_size __snake_case = { "data_dir": self.hparams.data_dir, "max_source_length": self.hparams.max_source_length, "prefix": self.model.config.prefix or "", } __snake_case = { "train": self.hparams.n_train, "val": self.hparams.n_val, "test": self.hparams.n_test, } __snake_case = {k: v if v >= 0 else None for k, v in n_observations_per_split.items()} __snake_case = { "train": self.hparams.max_target_length, "val": self.hparams.val_max_target_length, "test": self.hparams.test_max_target_length, } assert self.target_lens["train"] <= self.target_lens["val"], f'''target_lens: {self.target_lens}''' assert self.target_lens["train"] <= self.target_lens["test"], f'''target_lens: {self.target_lens}''' if self.hparams.freeze_embeds: freeze_embeds(self.model ) if self.hparams.freeze_encoder: freeze_params(self.model.get_encoder() ) assert_all_frozen(self.model.get_encoder() ) __snake_case = get_git_info()["repo_sha"] __snake_case = hparams.num_workers __snake_case = None # default to config if self.model.config.decoder_start_token_id is None and isinstance(self.tokenizer , _a ): __snake_case = self.tokenizer.lang_code_to_id[hparams.tgt_lang] __snake_case = self.decoder_start_token_id __snake_case = ( SeqaSeqDataset if hasattr(self.tokenizer , "prepare_seq2seq_batch" ) else LegacySeqaSeqDataset ) __snake_case = False __snake_case = self.model.config.num_beams if self.hparams.eval_beams is None else self.hparams.eval_beams if self.hparams.eval_max_gen_length is not None: __snake_case = self.hparams.eval_max_gen_length else: __snake_case = self.model.config.max_length __snake_case = self.default_val_metric if self.hparams.val_metric is None else self.hparams.val_metric def SCREAMING_SNAKE_CASE_ ( self : Optional[int] , SCREAMING_SNAKE_CASE : Dict[str, torch.Tensor] ) -> Dict[str, List[str]]: '''simple docstring''' __snake_case = { k: self.tokenizer.batch_decode(v.tolist() ) if "mask" not in k else v.shape for k, v in batch.items() } save_json(_a , Path(self.output_dir ) / "text_batch.json" ) save_json({k: v.tolist() for k, v in batch.items()} , Path(self.output_dir ) / "tok_batch.json" ) __snake_case = True return readable_batch def SCREAMING_SNAKE_CASE_ ( self : Any , SCREAMING_SNAKE_CASE : Optional[int] , **SCREAMING_SNAKE_CASE : Optional[Any] ) -> int: '''simple docstring''' return self.model(_a , **_a ) def SCREAMING_SNAKE_CASE_ ( self : Optional[Any] , SCREAMING_SNAKE_CASE : List[int] ) -> Optional[Any]: '''simple docstring''' __snake_case = self.tokenizer.batch_decode( _a , skip_special_tokens=_a , clean_up_tokenization_spaces=_a ) return lmap(str.strip , _a ) def SCREAMING_SNAKE_CASE_ ( self : List[Any] , SCREAMING_SNAKE_CASE : dict ) -> Tuple: '''simple docstring''' __snake_case = self.tokenizer.pad_token_id __snake_case , __snake_case = batch["input_ids"], batch["attention_mask"] __snake_case = batch["labels"] if isinstance(self.model , _a ): __snake_case = self.model._shift_right(_a ) else: __snake_case = shift_tokens_right(_a , _a ) if not self.already_saved_batch: # This would be slightly better if it only happened on rank zero __snake_case = decoder_input_ids self.save_readable_batch(_a ) __snake_case = self(_a , attention_mask=_a , decoder_input_ids=_a , use_cache=_a ) __snake_case = outputs["logits"] if self.hparams.label_smoothing == 0: # Same behavior as modeling_bart.py, besides ignoring pad_token_id __snake_case = nn.CrossEntropyLoss(ignore_index=_a ) assert lm_logits.shape[-1] == self.vocab_size __snake_case = ce_loss_fct(lm_logits.view(-1 , lm_logits.shape[-1] ) , tgt_ids.view(-1 ) ) else: __snake_case = nn.functional.log_softmax(_a , dim=-1 ) __snake_case , __snake_case = label_smoothed_nll_loss( _a , _a , self.hparams.label_smoothing , ignore_index=_a ) return (loss,) @property def SCREAMING_SNAKE_CASE_ ( self : Optional[int] ) -> int: '''simple docstring''' return self.tokenizer.pad_token_id def SCREAMING_SNAKE_CASE_ ( self : List[Any] , SCREAMING_SNAKE_CASE : Tuple , SCREAMING_SNAKE_CASE : int ) -> Dict: '''simple docstring''' __snake_case = self._step(_a ) __snake_case = dict(zip(self.loss_names , _a ) ) # tokens per batch __snake_case = batch["input_ids"].ne(self.pad ).sum() + batch["labels"].ne(self.pad ).sum() __snake_case = batch["input_ids"].shape[0] __snake_case = batch["input_ids"].eq(self.pad ).sum() __snake_case = batch["input_ids"].eq(self.pad ).float().mean() # TODO(SS): make a wandb summary metric for this return {"loss": loss_tensors[0], "log": logs} def SCREAMING_SNAKE_CASE_ ( self : str , SCREAMING_SNAKE_CASE : Dict , SCREAMING_SNAKE_CASE : List[Any] ) -> Dict: '''simple docstring''' return self._generative_step(_a ) def SCREAMING_SNAKE_CASE_ ( self : Any , SCREAMING_SNAKE_CASE : Tuple , SCREAMING_SNAKE_CASE : List[str]="val" ) -> Dict: '''simple docstring''' self.step_count += 1 __snake_case = {k: torch.stack([x[k] for x in outputs] ).mean() for k in self.loss_names} __snake_case = losses["loss"] __snake_case = { k: np.array([x[k] for x in outputs] ).mean() for k in self.metric_names + ["gen_time", "gen_len"] } __snake_case = ( generative_metrics[self.val_metric] if self.val_metric in generative_metrics else losses[self.val_metric] ) __snake_case = torch.tensor(_a ).type_as(_a ) generative_metrics.update({k: v.item() for k, v in losses.items()} ) losses.update(_a ) __snake_case = {f'''{prefix}_avg_{k}''': x for k, x in losses.items()} __snake_case = self.step_count self.metrics[prefix].append(_a ) # callback writes this to self.metrics_save_path __snake_case = flatten_list([x["preds"] for x in outputs] ) return { "log": all_metrics, "preds": preds, f'''{prefix}_loss''': loss, f'''{prefix}_{self.val_metric}''': metric_tensor, } def SCREAMING_SNAKE_CASE_ ( self : Any , SCREAMING_SNAKE_CASE : Optional[Any] , SCREAMING_SNAKE_CASE : int ) -> Dict: '''simple docstring''' return calculate_rouge(_a , _a ) def SCREAMING_SNAKE_CASE_ ( self : Tuple , SCREAMING_SNAKE_CASE : dict ) -> dict: '''simple docstring''' __snake_case = time.time() # parser.add_argument('--eval_max_gen_length', type=int, default=None, help='never generate more than n tokens') __snake_case = self.model.generate( batch["input_ids"] , attention_mask=batch["attention_mask"] , use_cache=_a , decoder_start_token_id=self.decoder_start_token_id , num_beams=self.eval_beams , max_length=self.eval_max_length , ) __snake_case = (time.time() - ta) / batch["input_ids"].shape[0] __snake_case = self.ids_to_clean_text(_a ) __snake_case = self.ids_to_clean_text(batch["labels"] ) __snake_case = self._step(_a ) __snake_case = dict(zip(self.loss_names , _a ) ) __snake_case = self.calc_generative_metrics(_a , _a ) __snake_case = np.mean(lmap(_a , _a ) ) base_metrics.update(gen_time=_a , gen_len=_a , preds=_a , target=_a , **_a ) return base_metrics def SCREAMING_SNAKE_CASE_ ( self : str , SCREAMING_SNAKE_CASE : Any , SCREAMING_SNAKE_CASE : Tuple ) -> Any: '''simple docstring''' return self._generative_step(_a ) def SCREAMING_SNAKE_CASE_ ( self : Optional[int] , SCREAMING_SNAKE_CASE : Optional[Any] ) -> List[Any]: '''simple docstring''' return self.validation_epoch_end(_a , prefix="test" ) def SCREAMING_SNAKE_CASE_ ( self : str , SCREAMING_SNAKE_CASE : Optional[int] ) -> SeqaSeqDataset: '''simple docstring''' __snake_case = self.n_obs[type_path] __snake_case = self.target_lens[type_path] __snake_case = self.dataset_class( self.tokenizer , type_path=_a , n_obs=_a , max_target_length=_a , **self.dataset_kwargs , ) return dataset def SCREAMING_SNAKE_CASE_ ( self : Optional[Any] , SCREAMING_SNAKE_CASE : str , SCREAMING_SNAKE_CASE : int , SCREAMING_SNAKE_CASE : bool = False ) -> DataLoader: '''simple docstring''' __snake_case = self.get_dataset(_a ) if self.hparams.sortish_sampler and type_path != "test" and type_path != "val": __snake_case = dataset.make_sortish_sampler(_a , distributed=self.hparams.gpus > 1 ) return DataLoader( _a , batch_size=_a , collate_fn=dataset.collate_fn , shuffle=_a , num_workers=self.num_workers , sampler=_a , ) elif self.hparams.max_tokens_per_batch is not None and type_path != "test" and type_path != "val": __snake_case = dataset.make_dynamic_sampler( self.hparams.max_tokens_per_batch , distributed=self.hparams.gpus > 1 ) return DataLoader( _a , batch_sampler=_a , collate_fn=dataset.collate_fn , num_workers=self.num_workers , ) else: return DataLoader( _a , batch_size=_a , collate_fn=dataset.collate_fn , shuffle=_a , num_workers=self.num_workers , sampler=_a , ) def SCREAMING_SNAKE_CASE_ ( self : Union[str, Any] ) -> DataLoader: '''simple docstring''' __snake_case = self.get_dataloader("train" , batch_size=self.hparams.train_batch_size , shuffle=_a ) return dataloader def SCREAMING_SNAKE_CASE_ ( self : Dict ) -> DataLoader: '''simple docstring''' return self.get_dataloader("val" , batch_size=self.hparams.eval_batch_size ) def SCREAMING_SNAKE_CASE_ ( self : Union[str, Any] ) -> DataLoader: '''simple docstring''' return self.get_dataloader("test" , batch_size=self.hparams.eval_batch_size ) @staticmethod def SCREAMING_SNAKE_CASE_ ( SCREAMING_SNAKE_CASE : Any , SCREAMING_SNAKE_CASE : Any ) -> List[str]: '''simple docstring''' BaseTransformer.add_model_specific_args(_a , _a ) add_generic_args(_a , _a ) parser.add_argument( "--max_source_length" , default=1_0_2_4 , type=_a , help=( "The maximum total input sequence length after tokenization. Sequences longer " "than this will be truncated, sequences shorter will be padded." ) , ) parser.add_argument( "--max_target_length" , default=5_6 , type=_a , help=( "The maximum total input sequence length after tokenization. Sequences longer " "than this will be truncated, sequences shorter will be padded." ) , ) parser.add_argument( "--val_max_target_length" , default=1_4_2 , type=_a , help=( "The maximum total input sequence length after tokenization. Sequences longer " "than this will be truncated, sequences shorter will be padded." ) , ) parser.add_argument( "--test_max_target_length" , default=1_4_2 , type=_a , help=( "The maximum total input sequence length after tokenization. Sequences longer " "than this will be truncated, sequences shorter will be padded." ) , ) parser.add_argument("--freeze_encoder" , action="store_true" ) parser.add_argument("--freeze_embeds" , action="store_true" ) parser.add_argument("--sortish_sampler" , action="store_true" , default=_a ) parser.add_argument("--overwrite_output_dir" , action="store_true" , default=_a ) parser.add_argument("--max_tokens_per_batch" , type=_a , default=_a ) parser.add_argument("--logger_name" , type=_a , choices=["default", "wandb", "wandb_shared"] , default="default" ) parser.add_argument("--n_train" , type=_a , default=-1 , required=_a , help="# examples. -1 means use all." ) parser.add_argument("--n_val" , type=_a , default=5_0_0 , required=_a , help="# examples. -1 means use all." ) parser.add_argument("--n_test" , type=_a , default=-1 , required=_a , help="# examples. -1 means use all." ) parser.add_argument( "--task" , type=_a , default="summarization" , required=_a , help="# examples. -1 means use all." ) parser.add_argument("--label_smoothing" , type=_a , default=0.0 , required=_a ) parser.add_argument("--src_lang" , type=_a , default="" , required=_a ) parser.add_argument("--tgt_lang" , type=_a , default="" , required=_a ) parser.add_argument("--eval_beams" , type=_a , default=_a , required=_a ) parser.add_argument( "--val_metric" , type=_a , default=_a , required=_a , choices=["bleu", "rouge2", "loss", None] ) parser.add_argument("--eval_max_gen_length" , type=_a , default=_a , help="never generate more than n tokens" ) parser.add_argument("--save_top_k" , type=_a , default=1 , required=_a , help="How many checkpoints to save" ) parser.add_argument( "--early_stopping_patience" , type=_a , default=-1 , required=_a , help=( "-1 means never early stop. early_stopping_patience is measured in validation checks, not epochs. So" " val_check_interval will effect it." ) , ) return parser class UpperCamelCase( UpperCamelCase__ ): snake_case_ : Tuple = """translation""" snake_case_ : Union[str, Any] = ["""loss"""] snake_case_ : Optional[Any] = ["""bleu"""] snake_case_ : str = """bleu""" def __init__( self : Union[str, Any] , SCREAMING_SNAKE_CASE : List[Any] , **SCREAMING_SNAKE_CASE : List[Any] ) -> Optional[Any]: '''simple docstring''' super().__init__(_a , **_a ) __snake_case = hparams.src_lang __snake_case = hparams.tgt_lang def SCREAMING_SNAKE_CASE_ ( self : Optional[Any] , SCREAMING_SNAKE_CASE : int , SCREAMING_SNAKE_CASE : List[Any] ) -> dict: '''simple docstring''' return calculate_bleu(_a , _a ) def _lowerCAmelCase ( _lowerCAmelCase , _lowerCAmelCase=None ) -> Optional[int]: '''simple docstring''' Path(args.output_dir ).mkdir(exist_ok=a__ ) check_output_dir(a__ , expected_items=3 ) if model is None: if "summarization" in args.task: __snake_case = SummarizationModule(a__ ) else: __snake_case = TranslationModule(a__ ) __snake_case = Path(args.data_dir ).name if ( args.logger_name == "default" or args.fast_dev_run or str(args.output_dir ).startswith("/tmp" ) or str(args.output_dir ).startswith("/var" ) ): __snake_case = True # don't pollute wandb logs unnecessarily elif args.logger_name == "wandb": from pytorch_lightning.loggers import WandbLogger __snake_case = os.environ.get("WANDB_PROJECT" , a__ ) __snake_case = WandbLogger(name=model.output_dir.name , project=a__ ) elif args.logger_name == "wandb_shared": from pytorch_lightning.loggers import WandbLogger __snake_case = WandbLogger(name=model.output_dir.name , project=F'''hf_{dataset}''' ) if args.early_stopping_patience >= 0: __snake_case = get_early_stopping_callback(model.val_metric , args.early_stopping_patience ) else: __snake_case = False __snake_case = args.val_metric == "loss" __snake_case = generic_train( a__ , a__ , logging_callback=SeqaSeqLoggingCallback() , checkpoint_callback=get_checkpoint_callback( args.output_dir , model.val_metric , args.save_top_k , a__ ) , early_stopping_callback=a__ , logger=a__ , ) pickle_save(model.hparams , model.output_dir / "hparams.pkl" ) if not args.do_predict: return model __snake_case = "" __snake_case = sorted(glob.glob(os.path.join(args.output_dir , "*.ckpt" ) , recursive=a__ ) ) if checkpoints: __snake_case = checkpoints[-1] __snake_case = checkpoints[-1] trainer.logger.log_hyperparams(model.hparams ) # test() without a model tests using the best checkpoint automatically trainer.test() return model if __name__ == "__main__": A : Optional[Any] = argparse.ArgumentParser() A : Tuple = pl.Trainer.add_argparse_args(parser) A : str = SummarizationModule.add_model_specific_args(parser, os.getcwd()) A : Union[str, Any] = parser.parse_args() main(args)
371
import logging import os from dataclasses import dataclass, field from typing import Dict, Optional import numpy as np from utils_multiple_choice import MultipleChoiceDataset, Split, processors import transformers from transformers import ( AutoConfig, AutoModelForMultipleChoice, AutoTokenizer, DataCollatorWithPadding, EvalPrediction, HfArgumentParser, Trainer, TrainingArguments, set_seed, ) from transformers.trainer_utils import is_main_process snake_case_ : str = logging.getLogger(__name__) def lowerCamelCase( a__ ,a__): return (preds == labels).mean() @dataclass class A__ : UpperCAmelCase = field( metadata={"help": "Path to pretrained model or model identifier from huggingface.co/models"} ) UpperCAmelCase = field( default=UpperCamelCase__ , metadata={"help": "Pretrained config name or path if not the same as model_name"} ) UpperCAmelCase = field( default=UpperCamelCase__ , metadata={"help": "Pretrained tokenizer name or path if not the same as model_name"} ) UpperCAmelCase = field( default=UpperCamelCase__ , metadata={"help": "Where do you want to store the pretrained models downloaded from huggingface.co"} , ) @dataclass class A__ : UpperCAmelCase = field(metadata={"help": "The name of the task to train on: " + ", ".join(processors.keys() )} ) UpperCAmelCase = field(metadata={"help": "Should contain the data files for the task."} ) UpperCAmelCase = 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." ) } , ) UpperCAmelCase = field( default=UpperCamelCase__ , metadata={"help": "Overwrite the cached training and evaluation sets"} ) def lowerCamelCase( ): # See all possible arguments in src/transformers/training_args.py # or by passing the --help flag to this script. # We now keep distinct sets of args, for a cleaner separation of concerns. _SCREAMING_SNAKE_CASE =HfArgumentParser((ModelArguments, DataTrainingArguments, TrainingArguments)) _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE =parser.parse_args_into_dataclasses() if ( os.path.exists(training_args.output_dir) and os.listdir(training_args.output_dir) and training_args.do_train and not training_args.overwrite_output_dir ): raise ValueError( f"Output directory ({training_args.output_dir}) already exists and is not empty. Use" ''' --overwrite_output_dir to overcome.''') # Setup logging logging.basicConfig( format='''%(asctime)s - %(levelname)s - %(name)s - %(message)s''' ,datefmt='''%m/%d/%Y %H:%M:%S''' ,level=logging.INFO if training_args.local_rank in [-1, 0] else logging.WARN ,) logger.warning( '''Process rank: %s, device: %s, n_gpu: %s, distributed training: %s, 16-bits training: %s''' ,training_args.local_rank ,training_args.device ,training_args.n_gpu ,bool(training_args.local_rank != -1) ,training_args.fpaa ,) # Set the verbosity to info of the Transformers logger (on main process only): if is_main_process(training_args.local_rank): transformers.utils.logging.set_verbosity_info() transformers.utils.logging.enable_default_handler() transformers.utils.logging.enable_explicit_format() logger.info('''Training/evaluation parameters %s''' ,a__) # Set seed set_seed(training_args.seed) try: _SCREAMING_SNAKE_CASE =processors[data_args.task_name]() _SCREAMING_SNAKE_CASE =processor.get_labels() _SCREAMING_SNAKE_CASE =len(a__) except KeyError: raise ValueError('''Task not found: %s''' % (data_args.task_name)) # Load pretrained model and tokenizer # # Distributed training: # The .from_pretrained methods guarantee that only one local process can concurrently # download model & vocab. _SCREAMING_SNAKE_CASE =AutoConfig.from_pretrained( model_args.config_name if model_args.config_name else model_args.model_name_or_path ,num_labels=a__ ,finetuning_task=data_args.task_name ,cache_dir=model_args.cache_dir ,) _SCREAMING_SNAKE_CASE =AutoTokenizer.from_pretrained( model_args.tokenizer_name if model_args.tokenizer_name else model_args.model_name_or_path ,cache_dir=model_args.cache_dir ,) _SCREAMING_SNAKE_CASE =AutoModelForMultipleChoice.from_pretrained( model_args.model_name_or_path ,from_tf=bool('''.ckpt''' in model_args.model_name_or_path) ,config=a__ ,cache_dir=model_args.cache_dir ,) # Get datasets _SCREAMING_SNAKE_CASE =( MultipleChoiceDataset( data_dir=data_args.data_dir ,tokenizer=a__ ,task=data_args.task_name ,max_seq_length=data_args.max_seq_length ,overwrite_cache=data_args.overwrite_cache ,mode=Split.train ,) if training_args.do_train else None ) _SCREAMING_SNAKE_CASE =( MultipleChoiceDataset( data_dir=data_args.data_dir ,tokenizer=a__ ,task=data_args.task_name ,max_seq_length=data_args.max_seq_length ,overwrite_cache=data_args.overwrite_cache ,mode=Split.dev ,) if training_args.do_eval else None ) def compute_metrics(a__) -> Dict: _SCREAMING_SNAKE_CASE =np.argmax(p.predictions ,axis=1) return {"acc": simple_accuracy(a__ ,p.label_ids)} # Data collator _SCREAMING_SNAKE_CASE =DataCollatorWithPadding(a__ ,pad_to_multiple_of=8) if training_args.fpaa else None # Initialize our Trainer _SCREAMING_SNAKE_CASE =Trainer( model=a__ ,args=a__ ,train_dataset=a__ ,eval_dataset=a__ ,compute_metrics=a__ ,data_collator=a__ ,) # Training if training_args.do_train: trainer.train( model_path=model_args.model_name_or_path if os.path.isdir(model_args.model_name_or_path) else None) trainer.save_model() # For convenience, we also re-save the tokenizer to the same directory, # so that you can share your model easily on huggingface.co/models =) if trainer.is_world_master(): tokenizer.save_pretrained(training_args.output_dir) # Evaluation _SCREAMING_SNAKE_CASE ={} if training_args.do_eval: logger.info('''*** Evaluate ***''') _SCREAMING_SNAKE_CASE =trainer.evaluate() _SCREAMING_SNAKE_CASE =os.path.join(training_args.output_dir ,'''eval_results.txt''') if trainer.is_world_master(): with open(a__ ,'''w''') as writer: logger.info('''***** Eval results *****''') for key, value in result.items(): logger.info(''' %s = %s''' ,a__ ,a__) writer.write('''%s = %s\n''' % (key, value)) results.update(a__) return results def lowerCamelCase( a__): # For xla_spawn (TPUs) main() if __name__ == "__main__": main()
691
0
'''simple docstring''' import tempfile import unittest from pathlib import Path from shutil import copyfile from transformers import MaMaaaTokenizer, is_torch_available from transformers.testing_utils import ( get_tests_dir, nested_simplify, require_sentencepiece, require_tokenizers, require_torch, slow, ) from transformers.utils import is_sentencepiece_available if is_sentencepiece_available(): from transformers.models.mam_aaa.tokenization_mam_aaa import VOCAB_FILES_NAMES, save_json from ...test_tokenization_common import TokenizerTesterMixin if is_sentencepiece_available(): UpperCamelCase_ = get_tests_dir("""fixtures/test_sentencepiece.model""") if is_torch_available(): from transformers.models.mam_aaa.modeling_mam_aaa import shift_tokens_right UpperCamelCase_ = 128022 UpperCamelCase_ = 128028 @require_sentencepiece class __SCREAMING_SNAKE_CASE ( UpperCamelCase__ , unittest.TestCase ): lowerCamelCase_ = MaMaaaTokenizer lowerCamelCase_ = False lowerCamelCase_ = False lowerCamelCase_ = True def lowerCamelCase_ ( self : Dict ): '''simple docstring''' super().setUp() lowercase : Dict =['''</s>''', '''<unk>''', '''โ–This''', '''โ–is''', '''โ–a''', '''โ–t''', '''est''', '''\u0120''', '''<pad>'''] lowercase : Optional[int] =dict(zip(_a , range(len(_a ) ) ) ) lowercase : Optional[int] =Path(self.tmpdirname ) save_json(_a , save_dir / VOCAB_FILES_NAMES['''vocab_file'''] ) if not (save_dir / VOCAB_FILES_NAMES["spm_file"]).exists(): copyfile(_a , save_dir / VOCAB_FILES_NAMES['''spm_file'''] ) lowercase : Optional[int] =MaMaaaTokenizer.from_pretrained(self.tmpdirname ) tokenizer.save_pretrained(self.tmpdirname ) def lowerCamelCase_ ( self : int , **UpperCAmelCase__ : Tuple ): '''simple docstring''' return MaMaaaTokenizer.from_pretrained(self.tmpdirname , **_a ) def lowerCamelCase_ ( self : Any , UpperCAmelCase__ : List[Any] ): '''simple docstring''' return ( "This is a test", "This is a test", ) def lowerCamelCase_ ( self : Union[str, Any] ): '''simple docstring''' lowercase : str ='''</s>''' lowercase : Tuple =0 self.assertEqual(self.get_tokenizer()._convert_token_to_id(_a ) , _a ) self.assertEqual(self.get_tokenizer()._convert_id_to_token(_a ) , _a ) def lowerCamelCase_ ( self : Union[str, Any] ): '''simple docstring''' lowercase : Union[str, Any] =self.get_tokenizer() lowercase : Dict =list(tokenizer.get_vocab().keys() ) self.assertEqual(vocab_keys[0] , '''</s>''' ) self.assertEqual(vocab_keys[1] , '''<unk>''' ) self.assertEqual(vocab_keys[-1] , '''<s>''' ) self.assertEqual(len(_a ) , tokenizer.vocab_size + len(tokenizer.get_added_vocab() ) ) @unittest.skip('''Skip this test while all models are still to be uploaded.''' ) def lowerCamelCase_ ( self : Union[str, Any] ): '''simple docstring''' pass def lowerCamelCase_ ( self : Any ): '''simple docstring''' lowercase : str =self.get_tokenizer() lowercase : List[str] =tokenizer.tokenize('''This is a test''' ) self.assertListEqual(_a , ['''โ–This''', '''โ–is''', '''โ–a''', '''โ–t''', '''est'''] ) self.assertListEqual( tokenizer.convert_tokens_to_ids(_a ) , [2, 3, 4, 5, 6] , ) lowercase : Tuple =tokenizer.convert_ids_to_tokens([2, 3, 4, 5, 6] ) self.assertListEqual(_a , ['''โ–This''', '''โ–is''', '''โ–a''', '''โ–t''', '''est'''] ) lowercase : Optional[Any] =tokenizer.convert_tokens_to_string(_a ) self.assertEqual(_a , '''This is a test''' ) @slow def lowerCamelCase_ ( self : Union[str, Any] ): '''simple docstring''' lowercase : Any ={'''input_ids''': [[128022, 110108, 397, 11, 38272, 2247, 124811, 285, 18105, 1586, 207, 7, 39534, 4428, 397, 1019, 18105, 1586, 207, 7, 41337, 16786, 241, 7, 20214, 17, 125690, 10398, 7, 44378, 58069, 68342, 7798, 7343, 11, 299, 33310, 4, 158, 37350, 94077, 4569, 299, 33310, 90, 4, 52840, 290, 4, 31270, 112, 299, 682, 4, 52840, 39953, 14079, 193, 52519, 90894, 17894, 120697, 11, 40445, 551, 17, 1019, 52519, 90894, 17756, 963, 11, 40445, 480, 17, 9792, 1120, 5173, 1393, 6240, 16786, 241, 120996, 28, 1245, 1393, 118240, 11123, 1019, 93612, 2691, 10618, 98058, 120409, 1928, 279, 4, 40683, 367, 178, 207, 1019, 103, 103121, 506, 65296, 5, 2], [128022, 21217, 367, 117, 125450, 128, 719, 7, 7308, 40, 93612, 12669, 1116, 16704, 71, 17785, 3699, 15592, 35, 144, 9584, 241, 11943, 713, 950, 799, 2247, 88427, 150, 149, 118813, 120706, 1019, 106906, 81518, 28, 1224, 22799, 397, 5, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [128022, 1658, 123311, 5155, 5578, 4722, 279, 14947, 2366, 1120, 1197, 14, 1348, 9232, 5, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]], '''attention_mask''': [[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]} # noqa: E501 # fmt: on self.tokenizer_integration_test_util( expected_encoding=_a , model_name='''facebook/m2m100_418M''' , revision='''c168bae485c864188cf9aa0e4108b0b6934dc91e''' , ) @require_torch @require_sentencepiece @require_tokenizers class __SCREAMING_SNAKE_CASE ( unittest.TestCase ): lowerCamelCase_ = 'facebook/m2m100_418M' lowerCamelCase_ = [ 'In my opinion, there are two levels of response from the French government.', 'NSA Affair Emphasizes Complete Lack of Debate on Intelligence', ] lowerCamelCase_ = [ 'Selon moi, il y a deux niveaux de rรฉponse de la part du gouvernement franรงais.', 'L\'affaire NSA souligne l\'absence totale de dรฉbat sur le renseignement', ] # fmt: off lowerCamelCase_ = [EN_CODE, 5_93, 19_49, 11_57_81, 4, 7_15_86, 42_34, 6_06_33, 12_62_33, 4_32, 12_38_08, 1_55_92, 11_97, 11_71_32, 12_06_18, 5, 2] @classmethod def lowerCamelCase_ ( cls : Any ): '''simple docstring''' lowercase : str =MaMaaaTokenizer.from_pretrained( cls.checkpoint_name , src_lang='''en''' , tgt_lang='''fr''' ) lowercase : Optional[int] =1 return cls def lowerCamelCase_ ( self : int ): '''simple docstring''' self.assertEqual(self.tokenizer.get_lang_id('''ar''' ) , 128006 ) self.assertEqual(self.tokenizer.get_lang_id('''en''' ) , 128022 ) self.assertEqual(self.tokenizer.get_lang_id('''ro''' ) , 128076 ) self.assertEqual(self.tokenizer.get_lang_id('''mr''' ) , 128063 ) def lowerCamelCase_ ( self : str ): '''simple docstring''' lowercase : str =self.tokenizer.get_vocab() self.assertEqual(len(_a ) , self.tokenizer.vocab_size ) self.assertEqual(vocab['''<unk>'''] , 3 ) self.assertIn(self.tokenizer.get_lang_token('''en''' ) , _a ) def lowerCamelCase_ ( self : Optional[int] ): '''simple docstring''' lowercase : str ='''en''' lowercase : List[Any] =self.tokenizer.batch_encode_plus(self.src_text ).input_ids[0] self.assertListEqual(self.expected_src_tokens , _a ) def lowerCamelCase_ ( self : Any ): '''simple docstring''' self.assertIn(_a , self.tokenizer.all_special_ids ) # fmt: off lowercase : Optional[int] =[FR_CODE, 5364, 82, 8642, 4, 294, 47, 8, 14028, 136, 3286, 9706, 6, 90797, 6, 144012, 162, 88128, 30061, 5, 2] # fmt: on lowercase : Union[str, Any] =self.tokenizer.decode(_a , skip_special_tokens=_a ) lowercase : str =self.tokenizer.decode(generated_ids[1:] , skip_special_tokens=_a ) self.assertEqual(_a , _a ) self.assertNotIn(self.tokenizer.eos_token , _a ) def lowerCamelCase_ ( self : Optional[Any] ): '''simple docstring''' lowercase : List[Any] =tempfile.mkdtemp() lowercase : Any =self.tokenizer.lang_token_to_id self.tokenizer.save_pretrained(_a ) lowercase : Any =MaMaaaTokenizer.from_pretrained(_a ) self.assertDictEqual(new_tok.lang_token_to_id , _a ) @require_torch def lowerCamelCase_ ( self : Optional[Any] ): '''simple docstring''' lowercase : List[str] ='''en''' lowercase : Any ='''fr''' lowercase : Any =self.tokenizer(self.src_text , text_target=self.tgt_text , padding=_a , return_tensors='''pt''' ) lowercase : Tuple =shift_tokens_right( batch['''labels'''] , self.tokenizer.pad_token_id , self.tokenizer.eos_token_id ) for k in batch: lowercase : Union[str, Any] =batch[k].tolist() # batch = {k: v.tolist() for k,v in batch.items()} # fairseq batch: https://gist.github.com/sshleifer/cba08bc2109361a74ac3760a7e30e4f4 # batch.decoder_inputs_ids[0][0] == assert batch.input_ids[1][0] == EN_CODE assert batch.input_ids[1][-1] == 2 assert batch.labels[1][0] == FR_CODE assert batch.labels[1][-1] == 2 assert batch.decoder_input_ids[1][:2] == [2, FR_CODE] @require_torch def lowerCamelCase_ ( self : Optional[Any] ): '''simple docstring''' lowercase : List[str] ='''mr''' self.assertListEqual(self.tokenizer.prefix_tokens , [self.tokenizer.get_lang_id('''mr''' )] ) self.assertListEqual(self.tokenizer.suffix_tokens , [self.tokenizer.eos_token_id] ) lowercase : Tuple ='''zh''' self.assertListEqual(self.tokenizer.prefix_tokens , [self.tokenizer.get_lang_id('''zh''' )] ) self.assertListEqual(self.tokenizer.suffix_tokens , [self.tokenizer.eos_token_id] ) @require_torch def lowerCamelCase_ ( self : Any ): '''simple docstring''' lowercase : Tuple ='''mr''' self.tokenizer._switch_to_target_mode() self.assertListEqual(self.tokenizer.prefix_tokens , [self.tokenizer.get_lang_id('''mr''' )] ) self.assertListEqual(self.tokenizer.suffix_tokens , [self.tokenizer.eos_token_id] ) self.tokenizer._switch_to_input_mode() self.assertListEqual(self.tokenizer.prefix_tokens , [self.tokenizer.get_lang_id(self.tokenizer.src_lang )] ) lowercase : str ='''zh''' self.tokenizer._switch_to_target_mode() self.assertListEqual(self.tokenizer.prefix_tokens , [self.tokenizer.get_lang_id('''zh''' )] ) self.assertListEqual(self.tokenizer.suffix_tokens , [self.tokenizer.eos_token_id] ) self.tokenizer._switch_to_input_mode() self.assertListEqual(self.tokenizer.prefix_tokens , [self.tokenizer.get_lang_id(self.tokenizer.src_lang )] ) @require_torch def lowerCamelCase_ ( self : int ): '''simple docstring''' lowercase : int =self.tokenizer._build_translation_inputs('''A test''' , return_tensors='''pt''' , src_lang='''en''' , tgt_lang='''ar''' ) self.assertEqual( nested_simplify(_a ) , { # en_XX, A, test, EOS '''input_ids''': [[128022, 58, 4183, 2]], '''attention_mask''': [[1, 1, 1, 1]], # ar_AR '''forced_bos_token_id''': 128006, } , )
92
def lowerCamelCase( a__ ,a__ ,a__): if n == 0: return 1 elif n % 2 == 1: return (binary_exponentiation(a__ ,n - 1 ,a__) * a) % mod else: _SCREAMING_SNAKE_CASE =binary_exponentiation(a__ ,n / 2 ,a__) return (b * b) % mod # a prime number snake_case_ : Union[str, Any] = 7_01 snake_case_ : int = 10_00_00_00_00 snake_case_ : str = 10 # using binary exponentiation function, O(log(p)): print((a / b) % p == (a * binary_exponentiation(b, p - 2, p)) % p) print((a / b) % p == (a * b ** (p - 2)) % p)
691
0
'''simple docstring''' def UpperCAmelCase ( A : List[Any] = 3 , A : Optional[int] = 7 , A : Tuple = 1000000 ): SCREAMING_SNAKE_CASE : int = 0 SCREAMING_SNAKE_CASE : Optional[Any] = 1 for current_denominator in range(1 , limit + 1 ): SCREAMING_SNAKE_CASE : int = current_denominator * numerator // denominator if current_denominator % denominator == 0: current_numerator -= 1 if current_numerator * max_denominator > current_denominator * max_numerator: SCREAMING_SNAKE_CASE : List[Any] = current_numerator SCREAMING_SNAKE_CASE : List[str] = current_denominator return max_numerator if __name__ == "__main__": print(solution(numerator=3, denominator=7, limit=100_0000))
527
import inspect import unittest from transformers import BitConfig from transformers.testing_utils import require_torch, require_vision, slow, torch_device from transformers.utils import cached_property, is_torch_available, is_vision_available from ...test_backbone_common import BackboneTesterMixin from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from torch import nn from transformers import BitBackbone, BitForImageClassification, BitImageProcessor, BitModel from transformers.models.bit.modeling_bit import BIT_PRETRAINED_MODEL_ARCHIVE_LIST if is_vision_available(): from PIL import Image class A__ : def __init__( self : Optional[Any] , _a : int , _a : Optional[Any]=3 , _a : Tuple=32 , _a : Any=3 , _a : Union[str, Any]=10 , _a : Optional[int]=[8, 16, 32, 64] , _a : Union[str, Any]=[1, 1, 2, 1] , _a : Optional[Any]=True , _a : int=True , _a : Tuple="relu" , _a : Optional[Any]=3 , _a : str=None , _a : List[Any]=["stage2", "stage3", "stage4"] , _a : Union[str, Any]=[2, 3, 4] , _a : Dict=1 , ) -> Dict: """simple docstring""" _SCREAMING_SNAKE_CASE =parent _SCREAMING_SNAKE_CASE =batch_size _SCREAMING_SNAKE_CASE =image_size _SCREAMING_SNAKE_CASE =num_channels _SCREAMING_SNAKE_CASE =embeddings_size _SCREAMING_SNAKE_CASE =hidden_sizes _SCREAMING_SNAKE_CASE =depths _SCREAMING_SNAKE_CASE =is_training _SCREAMING_SNAKE_CASE =use_labels _SCREAMING_SNAKE_CASE =hidden_act _SCREAMING_SNAKE_CASE =num_labels _SCREAMING_SNAKE_CASE =scope _SCREAMING_SNAKE_CASE =len(_a ) _SCREAMING_SNAKE_CASE =out_features _SCREAMING_SNAKE_CASE =out_indices _SCREAMING_SNAKE_CASE =num_groups def __UpperCamelCase ( self : Optional[Any] ) -> str: """simple docstring""" _SCREAMING_SNAKE_CASE =floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) _SCREAMING_SNAKE_CASE =None if self.use_labels: _SCREAMING_SNAKE_CASE =ids_tensor([self.batch_size] , self.num_labels ) _SCREAMING_SNAKE_CASE =self.get_config() return config, pixel_values, labels def __UpperCamelCase ( self : Any ) -> Union[str, Any]: """simple docstring""" return BitConfig( num_channels=self.num_channels , embeddings_size=self.embeddings_size , hidden_sizes=self.hidden_sizes , depths=self.depths , hidden_act=self.hidden_act , num_labels=self.num_labels , out_features=self.out_features , out_indices=self.out_indices , num_groups=self.num_groups , ) def __UpperCamelCase ( self : Optional[Any] , _a : Dict , _a : str , _a : Dict ) -> List[Any]: """simple docstring""" _SCREAMING_SNAKE_CASE =BitModel(config=_a ) model.to(_a ) model.eval() _SCREAMING_SNAKE_CASE =model(_a ) self.parent.assertEqual( result.last_hidden_state.shape , (self.batch_size, self.hidden_sizes[-1], self.image_size // 32, self.image_size // 32) , ) def __UpperCamelCase ( self : Union[str, Any] , _a : Union[str, Any] , _a : Optional[Any] , _a : Optional[Any] ) -> Union[str, Any]: """simple docstring""" _SCREAMING_SNAKE_CASE =self.num_labels _SCREAMING_SNAKE_CASE =BitForImageClassification(_a ) model.to(_a ) model.eval() _SCREAMING_SNAKE_CASE =model(_a , labels=_a ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_labels) ) def __UpperCamelCase ( self : List[str] , _a : Any , _a : str , _a : List[str] ) -> Optional[int]: """simple docstring""" _SCREAMING_SNAKE_CASE =BitBackbone(config=_a ) model.to(_a ) model.eval() _SCREAMING_SNAKE_CASE =model(_a ) # verify feature maps self.parent.assertEqual(len(result.feature_maps ) , len(config.out_features ) ) self.parent.assertListEqual(list(result.feature_maps[0].shape ) , [self.batch_size, self.hidden_sizes[1], 4, 4] ) # verify channels self.parent.assertEqual(len(model.channels ) , len(config.out_features ) ) self.parent.assertListEqual(model.channels , config.hidden_sizes[1:] ) # verify backbone works with out_features=None _SCREAMING_SNAKE_CASE =None _SCREAMING_SNAKE_CASE =BitBackbone(config=_a ) model.to(_a ) model.eval() _SCREAMING_SNAKE_CASE =model(_a ) # verify feature maps self.parent.assertEqual(len(result.feature_maps ) , 1 ) self.parent.assertListEqual(list(result.feature_maps[0].shape ) , [self.batch_size, self.hidden_sizes[-1], 1, 1] ) # verify channels self.parent.assertEqual(len(model.channels ) , 1 ) self.parent.assertListEqual(model.channels , [config.hidden_sizes[-1]] ) def __UpperCamelCase ( self : Optional[Any] ) -> Dict: """simple docstring""" _SCREAMING_SNAKE_CASE =self.prepare_config_and_inputs() _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE =config_and_inputs _SCREAMING_SNAKE_CASE ={'''pixel_values''': pixel_values} return config, inputs_dict @require_torch class A__ ( UpperCamelCase__ , UpperCamelCase__ , unittest.TestCase ): UpperCAmelCase = (BitModel, BitForImageClassification, BitBackbone) if is_torch_available() else () UpperCAmelCase = ( {"feature-extraction": BitModel, "image-classification": BitForImageClassification} if is_torch_available() else {} ) UpperCAmelCase = False UpperCAmelCase = False UpperCAmelCase = False UpperCAmelCase = False UpperCAmelCase = False def __UpperCamelCase ( self : Union[str, Any] ) -> str: """simple docstring""" _SCREAMING_SNAKE_CASE =BitModelTester(self ) _SCREAMING_SNAKE_CASE =ConfigTester(self , config_class=_a , has_text_modality=_a ) def __UpperCamelCase ( self : Union[str, Any] ) -> int: """simple docstring""" self.create_and_test_config_common_properties() self.config_tester.create_and_test_config_to_json_string() self.config_tester.create_and_test_config_to_json_file() self.config_tester.create_and_test_config_from_and_save_pretrained() self.config_tester.create_and_test_config_with_num_labels() self.config_tester.check_config_can_be_init_without_params() self.config_tester.check_config_arguments_init() def __UpperCamelCase ( self : List[str] ) -> Optional[int]: """simple docstring""" return @unittest.skip(reason='''Bit does not output attentions''' ) def __UpperCamelCase ( self : Optional[int] ) -> List[str]: """simple docstring""" pass @unittest.skip(reason='''Bit does not use inputs_embeds''' ) def __UpperCamelCase ( self : str ) -> Optional[Any]: """simple docstring""" pass @unittest.skip(reason='''Bit does not support input and output embeddings''' ) def __UpperCamelCase ( self : Optional[int] ) -> Union[str, Any]: """simple docstring""" pass def __UpperCamelCase ( self : Tuple ) -> Optional[int]: """simple docstring""" _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE =self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: _SCREAMING_SNAKE_CASE =model_class(_a ) _SCREAMING_SNAKE_CASE =inspect.signature(model.forward ) # signature.parameters is an OrderedDict => so arg_names order is deterministic _SCREAMING_SNAKE_CASE =[*signature.parameters.keys()] _SCREAMING_SNAKE_CASE =['''pixel_values'''] self.assertListEqual(arg_names[:1] , _a ) def __UpperCamelCase ( self : int ) -> Optional[int]: """simple docstring""" _SCREAMING_SNAKE_CASE =self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*_a ) def __UpperCamelCase ( self : Optional[Any] ) -> Dict: """simple docstring""" _SCREAMING_SNAKE_CASE =self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_backbone(*_a ) def __UpperCamelCase ( self : Tuple ) -> Optional[int]: """simple docstring""" _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE =self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: _SCREAMING_SNAKE_CASE =model_class(config=_a ) for name, module in model.named_modules(): if isinstance(_a , (nn.BatchNormad, nn.GroupNorm) ): self.assertTrue( torch.all(module.weight == 1 ) , msg=f"Parameter {name} of model {model_class} seems not properly initialized" , ) self.assertTrue( torch.all(module.bias == 0 ) , msg=f"Parameter {name} of model {model_class} seems not properly initialized" , ) def __UpperCamelCase ( self : Tuple ) -> Optional[int]: """simple docstring""" def check_hidden_states_output(_a : Any , _a : Optional[int] , _a : Tuple ): _SCREAMING_SNAKE_CASE =model_class(_a ) model.to(_a ) model.eval() with torch.no_grad(): _SCREAMING_SNAKE_CASE =model(**self._prepare_for_class(_a , _a ) ) _SCREAMING_SNAKE_CASE =outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states _SCREAMING_SNAKE_CASE =self.model_tester.num_stages self.assertEqual(len(_a ) , expected_num_stages + 1 ) # Bit's feature maps are of shape (batch_size, num_channels, height, width) self.assertListEqual( list(hidden_states[0].shape[-2:] ) , [self.model_tester.image_size // 4, self.model_tester.image_size // 4] , ) _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE =self.model_tester.prepare_config_and_inputs_for_common() _SCREAMING_SNAKE_CASE =['''preactivation''', '''bottleneck'''] for model_class in self.all_model_classes: for layer_type in layers_type: _SCREAMING_SNAKE_CASE =layer_type _SCREAMING_SNAKE_CASE =True check_hidden_states_output(_a , _a , _a ) # check that output_hidden_states also work using config del inputs_dict["output_hidden_states"] _SCREAMING_SNAKE_CASE =True check_hidden_states_output(_a , _a , _a ) @unittest.skip(reason='''Bit does not use feedforward chunking''' ) def __UpperCamelCase ( self : Optional[int] ) -> Dict: """simple docstring""" pass def __UpperCamelCase ( self : Union[str, Any] ) -> Any: """simple docstring""" _SCREAMING_SNAKE_CASE =self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_image_classification(*_a ) @slow def __UpperCamelCase ( self : int ) -> Tuple: """simple docstring""" for model_name in BIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: _SCREAMING_SNAKE_CASE =BitModel.from_pretrained(_a ) self.assertIsNotNone(_a ) def lowerCamelCase( ): _SCREAMING_SNAKE_CASE =Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png''') return image @require_torch @require_vision class A__ ( unittest.TestCase ): @cached_property def __UpperCamelCase ( self : str ) -> Optional[Any]: """simple docstring""" return ( BitImageProcessor.from_pretrained(BIT_PRETRAINED_MODEL_ARCHIVE_LIST[0] ) if is_vision_available() else None ) @slow def __UpperCamelCase ( self : List[Any] ) -> int: """simple docstring""" _SCREAMING_SNAKE_CASE =BitForImageClassification.from_pretrained(BIT_PRETRAINED_MODEL_ARCHIVE_LIST[0] ).to(_a ) _SCREAMING_SNAKE_CASE =self.default_image_processor _SCREAMING_SNAKE_CASE =prepare_img() _SCREAMING_SNAKE_CASE =image_processor(images=_a , return_tensors='''pt''' ).to(_a ) # forward pass with torch.no_grad(): _SCREAMING_SNAKE_CASE =model(**_a ) # verify the logits _SCREAMING_SNAKE_CASE =torch.Size((1, 1000) ) self.assertEqual(outputs.logits.shape , _a ) _SCREAMING_SNAKE_CASE =torch.tensor([[-0.65_26, -0.52_63, -1.43_98]] ).to(_a ) self.assertTrue(torch.allclose(outputs.logits[0, :3] , _a , atol=1E-4 ) ) @require_torch class A__ ( UpperCamelCase__ , unittest.TestCase ): UpperCAmelCase = (BitBackbone,) if is_torch_available() else () UpperCAmelCase = BitConfig UpperCAmelCase = False def __UpperCamelCase ( self : List[str] ) -> Optional[Any]: """simple docstring""" _SCREAMING_SNAKE_CASE =BitModelTester(self )
691
0
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 lowerCAmelCase__( lowercase : List[Any] ) -> Tuple: assert "mock" in _fsspec_registry assert "bz2" in _fsspec_registry def lowerCAmelCase__( ) -> Dict: assert "mock" not in _fsspec_registry assert "bz2" in _fsspec_registry def lowerCAmelCase__( ) -> int: __snake_case : Optional[int] = "mock-s3-bucket" __snake_case : str = f"""s3://{mock_bucket}""" __snake_case : int = extract_path_from_uri(a__ ) assert dataset_path.startswith("s3://" ) is False __snake_case : int = "./local/path" __snake_case : Dict = extract_path_from_uri(a__ ) assert dataset_path == new_dataset_path def lowerCAmelCase__( lowercase : Union[str, Any] ) -> Union[str, Any]: __snake_case : Tuple = is_remote_filesystem(a__ ) assert is_remote is True __snake_case : List[Any] = fsspec.filesystem("file" ) __snake_case : Tuple = is_remote_filesystem(a__ ) assert is_remote is False @pytest.mark.parametrize("compression_fs_class" , a__ ) def lowerCAmelCase__( lowercase : Optional[Any] , lowercase : Union[str, Any] , lowercase : Dict , lowercase : str , lowercase : Dict , lowercase : Tuple , lowercase : Any ) -> Any: __snake_case : int = {"gzip": gz_file, "xz": xz_file, "zstd": zstd_file, "bz2": bza_file, "lz4": lza_file} __snake_case : List[Any] = input_paths[compression_fs_class.protocol] if input_path is None: __snake_case : Tuple = 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__ ) __snake_case : Optional[int] = fsspec.filesystem(compression_fs_class.protocol , fo=a__ ) assert isinstance(a__ , a__ ) __snake_case : Dict = os.path.basename(a__ ) __snake_case : Tuple = 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 lowerCAmelCase__( lowercase : int , lowercase : Any , lowercase : Union[str, Any] ) -> List[Any]: __snake_case : int = {"zip": zip_jsonl_path, "gzip": jsonl_gz_path} __snake_case : List[Any] = compressed_file_paths[protocol] __snake_case : Optional[int] = "dataset.jsonl" __snake_case : Tuple = f"""{protocol}://{member_file_path}::{compressed_file_path}""" __snake_case , *__snake_case : Union[str, Any] = fsspec.get_fs_token_paths(a__ ) assert fs.isfile(a__ ) assert not fs.isfile("non_existing_" + member_file_path ) @pytest.mark.integration def lowerCAmelCase__( lowercase : Dict , lowercase : Dict , lowercase : Optional[int] , lowercase : Optional[Any] ) -> Dict: __snake_case : int = hf_api.dataset_info(a__ , token=a__ ) __snake_case : Union[str, Any] = 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 lowerCAmelCase__( ) -> Dict: __snake_case : Optional[int] = "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.""" )
243
import copy from ...configuration_utils import PretrainedConfig from ...utils import add_start_docstrings snake_case_ : Optional[Any] = R''' [`RagConfig`] stores the configuration of a *RagModel*. Configuration objects inherit from [`PretrainedConfig`] and can be used to control the model outputs. Read the documentation from [`PretrainedConfig`] for more information. Args: title_sep (`str`, *optional*, defaults to `" / "`): Separator inserted between the title and the text of the retrieved document when calling [`RagRetriever`]. doc_sep (`str`, *optional*, defaults to `" // "`): Separator inserted between the text of the retrieved document and the original input when calling [`RagRetriever`]. n_docs (`int`, *optional*, defaults to 5): Number of documents to retrieve. max_combined_length (`int`, *optional*, defaults to 300): Max length of contextualized input returned by [`~RagRetriever.__call__`]. retrieval_vector_size (`int`, *optional*, defaults to 768): Dimensionality of the document embeddings indexed by [`RagRetriever`]. retrieval_batch_size (`int`, *optional*, defaults to 8): Retrieval batch size, defined as the number of queries issues concurrently to the faiss index encapsulated [`RagRetriever`]. dataset (`str`, *optional*, defaults to `"wiki_dpr"`): A dataset identifier of the indexed dataset in HuggingFace Datasets (list all available datasets and ids using `datasets.list_datasets()`). dataset_split (`str`, *optional*, defaults to `"train"`) Which split of the `dataset` to load. index_name (`str`, *optional*, defaults to `"compressed"`) The index name of the index associated with the `dataset`. One can choose between `"legacy"`, `"exact"` and `"compressed"`. index_path (`str`, *optional*) The path to the serialized faiss index on disk. passages_path (`str`, *optional*): A path to text passages compatible with the faiss index. Required if using [`~models.rag.retrieval_rag.LegacyIndex`] use_dummy_dataset (`bool`, *optional*, defaults to `False`) Whether to load a "dummy" variant of the dataset specified by `dataset`. label_smoothing (`float`, *optional*, defaults to 0.0): Only relevant if `return_loss` is set to `True`. Controls the `epsilon` parameter value for label smoothing in the loss calculation. If set to 0, no label smoothing is performed. do_marginalize (`bool`, *optional*, defaults to `False`): If `True`, the logits are marginalized over all documents by making use of `torch.nn.functional.log_softmax`. reduce_loss (`bool`, *optional*, defaults to `False`): Whether or not to reduce the NLL loss using the `torch.Tensor.sum` operation. do_deduplication (`bool`, *optional*, defaults to `True`): Whether or not to deduplicate the generations from different context documents for a given input. Has to be set to `False` if used while training with distributed backend. exclude_bos_score (`bool`, *optional*, defaults to `False`): Whether or not to disregard the BOS token when computing the loss. output_retrieved(`bool`, *optional*, defaults to `False`): If set to `True`, `retrieved_doc_embeds`, `retrieved_doc_ids`, `context_input_ids` and `context_attention_mask` are returned. See returned tensors for more detail. use_cache (`bool`, *optional*, defaults to `True`): Whether or not the model should return the last key/values attentions (not used by all models). forced_eos_token_id (`int`, *optional*): The id of the token to force as the last generated token when `max_length` is reached. Usually set to `eos_token_id`. ''' @add_start_docstrings(UpperCamelCase__ ) class A__ ( UpperCamelCase__ ): UpperCAmelCase = "rag" UpperCAmelCase = True def __init__( self : Tuple , _a : List[Any]=None , _a : Tuple=True , _a : Optional[Any]=None , _a : int=None , _a : List[str]=None , _a : int=None , _a : Optional[int]=None , _a : str=" / " , _a : Any=" // " , _a : Optional[Any]=5 , _a : int=300 , _a : Optional[Any]=768 , _a : Any=8 , _a : List[str]="wiki_dpr" , _a : Dict="train" , _a : Union[str, Any]="compressed" , _a : str=None , _a : Union[str, Any]=None , _a : int=False , _a : Any=False , _a : Any=0.0 , _a : Any=True , _a : List[str]=False , _a : Optional[int]=False , _a : int=False , _a : Union[str, Any]=True , _a : Optional[int]=None , **_a : List[str] , ) -> List[Any]: """simple docstring""" super().__init__( bos_token_id=_a , pad_token_id=_a , eos_token_id=_a , decoder_start_token_id=_a , forced_eos_token_id=_a , is_encoder_decoder=_a , prefix=_a , vocab_size=_a , **_a , ) assert ( "question_encoder" in kwargs and "generator" in kwargs ), "Config has to be initialized with question_encoder and generator config" _SCREAMING_SNAKE_CASE =kwargs.pop('''question_encoder''' ) _SCREAMING_SNAKE_CASE =question_encoder_config.pop('''model_type''' ) _SCREAMING_SNAKE_CASE =kwargs.pop('''generator''' ) _SCREAMING_SNAKE_CASE =decoder_config.pop('''model_type''' ) from ..auto.configuration_auto import AutoConfig _SCREAMING_SNAKE_CASE =AutoConfig.for_model(_a , **_a ) _SCREAMING_SNAKE_CASE =AutoConfig.for_model(_a , **_a ) _SCREAMING_SNAKE_CASE =reduce_loss _SCREAMING_SNAKE_CASE =label_smoothing _SCREAMING_SNAKE_CASE =exclude_bos_score _SCREAMING_SNAKE_CASE =do_marginalize _SCREAMING_SNAKE_CASE =title_sep _SCREAMING_SNAKE_CASE =doc_sep _SCREAMING_SNAKE_CASE =n_docs _SCREAMING_SNAKE_CASE =max_combined_length _SCREAMING_SNAKE_CASE =dataset _SCREAMING_SNAKE_CASE =dataset_split _SCREAMING_SNAKE_CASE =index_name _SCREAMING_SNAKE_CASE =retrieval_vector_size _SCREAMING_SNAKE_CASE =retrieval_batch_size _SCREAMING_SNAKE_CASE =passages_path _SCREAMING_SNAKE_CASE =index_path _SCREAMING_SNAKE_CASE =use_dummy_dataset _SCREAMING_SNAKE_CASE =output_retrieved _SCREAMING_SNAKE_CASE =do_deduplication _SCREAMING_SNAKE_CASE =use_cache if self.forced_eos_token_id is None: _SCREAMING_SNAKE_CASE =getattr(self.generator , '''forced_eos_token_id''' , _a ) @classmethod def __UpperCamelCase ( cls : Optional[int] , _a : PretrainedConfig , _a : PretrainedConfig , **_a : Dict ) -> PretrainedConfig: """simple docstring""" return cls(question_encoder=question_encoder_config.to_dict() , generator=generator_config.to_dict() , **_a ) def __UpperCamelCase ( self : Optional[Any] ) -> Any: """simple docstring""" _SCREAMING_SNAKE_CASE =copy.deepcopy(self.__dict__ ) _SCREAMING_SNAKE_CASE =self.question_encoder.to_dict() _SCREAMING_SNAKE_CASE =self.generator.to_dict() _SCREAMING_SNAKE_CASE =self.__class__.model_type return output
691
0
import inspect import unittest from transformers import BitConfig from transformers.testing_utils import require_torch, require_vision, slow, torch_device from transformers.utils import cached_property, is_torch_available, is_vision_available from ...test_backbone_common import BackboneTesterMixin from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from torch import nn from transformers import BitBackbone, BitForImageClassification, BitImageProcessor, BitModel from transformers.models.bit.modeling_bit import BIT_PRETRAINED_MODEL_ARCHIVE_LIST if is_vision_available(): from PIL import Image class UpperCamelCase : def __init__( self : Optional[Any] ,_lowerCAmelCase : int ,_lowerCAmelCase : Optional[Any]=3 ,_lowerCAmelCase : Tuple=32 ,_lowerCAmelCase : Any=3 ,_lowerCAmelCase : Union[str, Any]=10 ,_lowerCAmelCase : Optional[int]=[8, 16, 32, 64] ,_lowerCAmelCase : Union[str, Any]=[1, 1, 2, 1] ,_lowerCAmelCase : Optional[Any]=True ,_lowerCAmelCase : int=True ,_lowerCAmelCase : Tuple="relu" ,_lowerCAmelCase : Optional[Any]=3 ,_lowerCAmelCase : str=None ,_lowerCAmelCase : List[Any]=["stage2", "stage3", "stage4"] ,_lowerCAmelCase : Union[str, Any]=[2, 3, 4] ,_lowerCAmelCase : Dict=1 ,): """simple docstring""" __snake_case = parent __snake_case = batch_size __snake_case = image_size __snake_case = num_channels __snake_case = embeddings_size __snake_case = hidden_sizes __snake_case = depths __snake_case = is_training __snake_case = use_labels __snake_case = hidden_act __snake_case = num_labels __snake_case = scope __snake_case = len(_a ) __snake_case = out_features __snake_case = out_indices __snake_case = num_groups def UpperCamelCase_ ( self : Optional[Any] ): """simple docstring""" __snake_case = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) __snake_case = None if self.use_labels: __snake_case = ids_tensor([self.batch_size] ,self.num_labels ) __snake_case = self.get_config() return config, pixel_values, labels def UpperCamelCase_ ( self : Any ): """simple docstring""" return BitConfig( num_channels=self.num_channels ,embeddings_size=self.embeddings_size ,hidden_sizes=self.hidden_sizes ,depths=self.depths ,hidden_act=self.hidden_act ,num_labels=self.num_labels ,out_features=self.out_features ,out_indices=self.out_indices ,num_groups=self.num_groups ,) def UpperCamelCase_ ( self : Optional[Any] ,_lowerCAmelCase : Dict ,_lowerCAmelCase : str ,_lowerCAmelCase : Dict ): """simple docstring""" __snake_case = BitModel(config=_a ) model.to(_a ) model.eval() __snake_case = model(_a ) self.parent.assertEqual( result.last_hidden_state.shape ,(self.batch_size, self.hidden_sizes[-1], self.image_size // 32, self.image_size // 32) ,) def UpperCamelCase_ ( self : Union[str, Any] ,_lowerCAmelCase : Union[str, Any] ,_lowerCAmelCase : Optional[Any] ,_lowerCAmelCase : Optional[Any] ): """simple docstring""" __snake_case = self.num_labels __snake_case = BitForImageClassification(_a ) model.to(_a ) model.eval() __snake_case = model(_a ,labels=_a ) self.parent.assertEqual(result.logits.shape ,(self.batch_size, self.num_labels) ) def UpperCamelCase_ ( self : List[str] ,_lowerCAmelCase : Any ,_lowerCAmelCase : str ,_lowerCAmelCase : List[str] ): """simple docstring""" __snake_case = BitBackbone(config=_a ) model.to(_a ) model.eval() __snake_case = model(_a ) # verify feature maps self.parent.assertEqual(len(result.feature_maps ) ,len(config.out_features ) ) self.parent.assertListEqual(list(result.feature_maps[0].shape ) ,[self.batch_size, self.hidden_sizes[1], 4, 4] ) # verify channels self.parent.assertEqual(len(model.channels ) ,len(config.out_features ) ) self.parent.assertListEqual(model.channels ,config.hidden_sizes[1:] ) # verify backbone works with out_features=None __snake_case = None __snake_case = BitBackbone(config=_a ) model.to(_a ) model.eval() __snake_case = model(_a ) # verify feature maps self.parent.assertEqual(len(result.feature_maps ) ,1 ) self.parent.assertListEqual(list(result.feature_maps[0].shape ) ,[self.batch_size, self.hidden_sizes[-1], 1, 1] ) # verify channels self.parent.assertEqual(len(model.channels ) ,1 ) self.parent.assertListEqual(model.channels ,[config.hidden_sizes[-1]] ) def UpperCamelCase_ ( self : Optional[Any] ): """simple docstring""" __snake_case = self.prepare_config_and_inputs() __snake_case , __snake_case , __snake_case = config_and_inputs __snake_case = {"pixel_values": pixel_values} return config, inputs_dict @require_torch class UpperCamelCase ( UpperCamelCase__ , UpperCamelCase__ , unittest.TestCase ): __UpperCamelCase = (BitModel, BitForImageClassification, BitBackbone) if is_torch_available() else () __UpperCamelCase = ( {"""feature-extraction""": BitModel, """image-classification""": BitForImageClassification} if is_torch_available() else {} ) __UpperCamelCase = False __UpperCamelCase = False __UpperCamelCase = False __UpperCamelCase = False __UpperCamelCase = False def UpperCamelCase_ ( self : Union[str, Any] ): """simple docstring""" __snake_case = BitModelTester(self ) __snake_case = ConfigTester(self ,config_class=_a ,has_text_modality=_a ) def UpperCamelCase_ ( self : Union[str, Any] ): """simple docstring""" self.create_and_test_config_common_properties() self.config_tester.create_and_test_config_to_json_string() self.config_tester.create_and_test_config_to_json_file() self.config_tester.create_and_test_config_from_and_save_pretrained() self.config_tester.create_and_test_config_with_num_labels() self.config_tester.check_config_can_be_init_without_params() self.config_tester.check_config_arguments_init() def UpperCamelCase_ ( self : List[str] ): """simple docstring""" return @unittest.skip(reason="Bit does not output attentions" ) def UpperCamelCase_ ( self : Optional[int] ): """simple docstring""" pass @unittest.skip(reason="Bit does not use inputs_embeds" ) def UpperCamelCase_ ( self : str ): """simple docstring""" pass @unittest.skip(reason="Bit does not support input and output embeddings" ) def UpperCamelCase_ ( self : Optional[int] ): """simple docstring""" pass def UpperCamelCase_ ( self : Tuple ): """simple docstring""" __snake_case , __snake_case = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: __snake_case = model_class(_a ) __snake_case = inspect.signature(model.forward ) # signature.parameters is an OrderedDict => so arg_names order is deterministic __snake_case = [*signature.parameters.keys()] __snake_case = ["pixel_values"] self.assertListEqual(arg_names[:1] ,_a ) def UpperCamelCase_ ( self : int ): """simple docstring""" __snake_case = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*_a ) def UpperCamelCase_ ( self : Optional[Any] ): """simple docstring""" __snake_case = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_backbone(*_a ) def UpperCamelCase_ ( self : Tuple ): """simple docstring""" __snake_case , __snake_case = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: __snake_case = model_class(config=_a ) for name, module in model.named_modules(): if isinstance(_a ,(nn.BatchNormad, nn.GroupNorm) ): self.assertTrue( torch.all(module.weight == 1 ) ,msg=F"""Parameter {name} of model {model_class} seems not properly initialized""" ,) self.assertTrue( torch.all(module.bias == 0 ) ,msg=F"""Parameter {name} of model {model_class} seems not properly initialized""" ,) def UpperCamelCase_ ( self : Tuple ): """simple docstring""" def check_hidden_states_output(_lowerCAmelCase : Any ,_lowerCAmelCase : Optional[int] ,_lowerCAmelCase : Tuple ): __snake_case = model_class(_a ) model.to(_a ) model.eval() with torch.no_grad(): __snake_case = model(**self._prepare_for_class(_a ,_a ) ) __snake_case = outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states __snake_case = self.model_tester.num_stages self.assertEqual(len(_a ) ,expected_num_stages + 1 ) # Bit's feature maps are of shape (batch_size, num_channels, height, width) self.assertListEqual( list(hidden_states[0].shape[-2:] ) ,[self.model_tester.image_size // 4, self.model_tester.image_size // 4] ,) __snake_case , __snake_case = self.model_tester.prepare_config_and_inputs_for_common() __snake_case = ["preactivation", "bottleneck"] for model_class in self.all_model_classes: for layer_type in layers_type: __snake_case = layer_type __snake_case = True check_hidden_states_output(_a ,_a ,_a ) # check that output_hidden_states also work using config del inputs_dict["output_hidden_states"] __snake_case = True check_hidden_states_output(_a ,_a ,_a ) @unittest.skip(reason="Bit does not use feedforward chunking" ) def UpperCamelCase_ ( self : Optional[int] ): """simple docstring""" pass def UpperCamelCase_ ( self : Union[str, Any] ): """simple docstring""" __snake_case = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_image_classification(*_a ) @slow def UpperCamelCase_ ( self : int ): """simple docstring""" for model_name in BIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: __snake_case = BitModel.from_pretrained(_a ) self.assertIsNotNone(_a ) def _lowerCamelCase( ) -> Tuple: __snake_case = Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png" ) return image @require_torch @require_vision class UpperCamelCase ( unittest.TestCase ): @cached_property def UpperCamelCase_ ( self : str ): """simple docstring""" return ( BitImageProcessor.from_pretrained(BIT_PRETRAINED_MODEL_ARCHIVE_LIST[0] ) if is_vision_available() else None ) @slow def UpperCamelCase_ ( self : List[Any] ): """simple docstring""" __snake_case = BitForImageClassification.from_pretrained(BIT_PRETRAINED_MODEL_ARCHIVE_LIST[0] ).to(_a ) __snake_case = self.default_image_processor __snake_case = prepare_img() __snake_case = image_processor(images=_a ,return_tensors="pt" ).to(_a ) # forward pass with torch.no_grad(): __snake_case = model(**_a ) # verify the logits __snake_case = torch.Size((1, 1_000) ) self.assertEqual(outputs.logits.shape ,_a ) __snake_case = torch.tensor([[-0.6_5_2_6, -0.5_2_6_3, -1.4_3_9_8]] ).to(_a ) self.assertTrue(torch.allclose(outputs.logits[0, :3] ,_a ,atol=1E-4 ) ) @require_torch class UpperCamelCase ( UpperCamelCase__ , unittest.TestCase ): __UpperCamelCase = (BitBackbone,) if is_torch_available() else () __UpperCamelCase = BitConfig __UpperCamelCase = False def UpperCamelCase_ ( self : List[str] ): """simple docstring""" __snake_case = BitModelTester(self )
524
from manim import * class A__ ( UpperCamelCase__ ): def __UpperCamelCase ( self : Dict ) -> int: """simple docstring""" _SCREAMING_SNAKE_CASE =Rectangle(height=0.5 , width=0.5 ) _SCREAMING_SNAKE_CASE =Rectangle(height=0.25 , width=0.25 ) _SCREAMING_SNAKE_CASE =Rectangle(height=0.46 , width=0.46 ).set_stroke(width=0 ) _SCREAMING_SNAKE_CASE =[mem.copy() for i in range(6 )] _SCREAMING_SNAKE_CASE =[mem.copy() for i in range(6 )] _SCREAMING_SNAKE_CASE =VGroup(*_a ).arrange(_a , buff=0 ) _SCREAMING_SNAKE_CASE =VGroup(*_a ).arrange(_a , buff=0 ) _SCREAMING_SNAKE_CASE =VGroup(_a , _a ).arrange(_a , buff=0 ) _SCREAMING_SNAKE_CASE =Text('''CPU''' , font_size=24 ) _SCREAMING_SNAKE_CASE =Group(_a , _a ).arrange(_a , buff=0.5 , aligned_edge=_a ) cpu.move_to([-2.5, -0.5, 0] ) self.add(_a ) _SCREAMING_SNAKE_CASE =[mem.copy() for i in range(4 )] _SCREAMING_SNAKE_CASE =VGroup(*_a ).arrange(_a , buff=0 ) _SCREAMING_SNAKE_CASE =Text('''GPU''' , font_size=24 ) _SCREAMING_SNAKE_CASE =Group(_a , _a ).arrange(_a , buff=0.5 , aligned_edge=_a ) gpu.move_to([-1, -1, 0] ) self.add(_a ) _SCREAMING_SNAKE_CASE =[mem.copy() for i in range(6 )] _SCREAMING_SNAKE_CASE =VGroup(*_a ).arrange(_a , buff=0 ) _SCREAMING_SNAKE_CASE =Text('''Model''' , font_size=24 ) _SCREAMING_SNAKE_CASE =Group(_a , _a ).arrange(_a , buff=0.5 , aligned_edge=_a ) model.move_to([3, -1.0, 0] ) self.add(_a ) _SCREAMING_SNAKE_CASE =[] _SCREAMING_SNAKE_CASE =[] _SCREAMING_SNAKE_CASE =[] for i, rect in enumerate(_a ): rect.set_stroke(_a ) _SCREAMING_SNAKE_CASE =Rectangle(height=0.46 / 4 , width=0.46 / 3 ).set_stroke(width=0.0 ).set_fill(_a , opacity=0.7 ) if i == 0: cpu_target.next_to(cpu_left_col_base[0].get_corner(DOWN + LEFT ) , buff=0.02 , direction=_a ) cpu_target.set_x(cpu_target.get_x() + 0.1 ) elif i == 3: cpu_target.next_to(model_cpu_arr[0] , direction=_a , buff=0.0 ) else: cpu_target.next_to(model_cpu_arr[i - 1] , direction=_a , buff=0.0 ) self.add(_a ) model_cpu_arr.append(_a ) self.add(*_a , *_a , *_a ) _SCREAMING_SNAKE_CASE =[mem.copy() for i in range(6 )] _SCREAMING_SNAKE_CASE =VGroup(*_a ).arrange(_a , buff=0 ) _SCREAMING_SNAKE_CASE =Text('''Loaded Checkpoint''' , font_size=24 ) _SCREAMING_SNAKE_CASE =Group(_a , _a ).arrange(_a , buff=0.5 , aligned_edge=_a ) checkpoint.move_to([3, 0.5, 0] ) self.add(_a ) _SCREAMING_SNAKE_CASE =[] _SCREAMING_SNAKE_CASE =[] for i, rect in enumerate(_a ): _SCREAMING_SNAKE_CASE =fill.copy().set_fill(_a , opacity=0.7 ) target.move_to(_a ) ckpt_arr.append(_a ) _SCREAMING_SNAKE_CASE =target.copy() if i < 5: cpu_target.move_to(cpu_left_col_base[i + 1] ) else: cpu_target.move_to(cpu_right_col_base[i - 5] ) ckpt_cpu_arr.append(_a ) self.add(*_a , *_a ) _SCREAMING_SNAKE_CASE =Square(side_length=2.2 ) key.move_to([-5, 2, 0] ) _SCREAMING_SNAKE_CASE =MarkupText( f"<b>Key:</b>\n\n<span fgcolor='{YELLOW}'>โ—</span> Empty Model" , font_size=18 , ) key_text.move_to([-5, 2.4, 0] ) self.add(_a , _a ) _SCREAMING_SNAKE_CASE =MarkupText( f"<span fgcolor='{BLUE}'>โ—</span> Checkpoint" , font_size=18 , ) blue_text.next_to(_a , DOWN * 2.4 , aligned_edge=key_text.get_left() ) self.add(_a ) _SCREAMING_SNAKE_CASE =MarkupText( f"Based on the passed in configuration, weights are stored in\na variety of np.memmaps on disk or to a particular device." , font_size=24 , ) step_a.move_to([2, 2, 0] ) _SCREAMING_SNAKE_CASE =[meta_mem.copy() for i in range(6 )] _SCREAMING_SNAKE_CASE =[meta_mem.copy() for i in range(6 )] _SCREAMING_SNAKE_CASE =VGroup(*_a ).arrange(_a , buff=0 ) _SCREAMING_SNAKE_CASE =VGroup(*_a ).arrange(_a , buff=0 ) _SCREAMING_SNAKE_CASE =VGroup(_a , _a ).arrange(_a , buff=0 ) _SCREAMING_SNAKE_CASE =Text('''Disk''' , font_size=24 ) _SCREAMING_SNAKE_CASE =Group(_a , _a ).arrange(_a , buff=0.5 , aligned_edge=_a ) disk.move_to([-4.0, -1.25, 0] ) self.play(Write(_a , run_time=3 ) , Write(_a , run_time=1 ) , Create(_a , run_time=1 ) ) _SCREAMING_SNAKE_CASE =[] for i, rect in enumerate(_a ): _SCREAMING_SNAKE_CASE =rect.copy() target.generate_target() target.target.move_to(disk_left_col_base[i] ).scale(0.5 ) animations.append(MoveToTarget(_a , run_time=1.5 ) ) self.play(*_a ) self.play(FadeOut(_a ) ) _SCREAMING_SNAKE_CASE =MarkupText(f"Then, the checkpoint is removed from memory\nthrough garbage collection." , font_size=24 ) step_a.move_to([2, 2, 0] ) self.play(Write(_a , run_time=3 ) ) self.play( FadeOut(_a , _a , *_a , *_a ) , ) self.wait()
691
0
import argparse from typing import Dict import tensorflow as tf import torch from tqdm import tqdm from transformers import BigBirdPegasusConfig, BigBirdPegasusForConditionalGeneration _UpperCAmelCase : Optional[int] = [ # tf -> hf ('''/''', '''.'''), ('''layer_''', '''layers.'''), ('''kernel''', '''weight'''), ('''beta''', '''bias'''), ('''gamma''', '''weight'''), ('''pegasus''', '''model'''), ] _UpperCAmelCase : List[Any] = [ ('''.output.dense''', '''.fc2'''), ('''intermediate.LayerNorm''', '''final_layer_norm'''), ('''intermediate.dense''', '''fc1'''), ] _UpperCAmelCase : List[str] = ( INIT_COMMON + [ ('''attention.self.LayerNorm''', '''self_attn_layer_norm'''), ('''attention.output.dense''', '''self_attn.out_proj'''), ('''attention.self''', '''self_attn'''), ('''attention.encdec.LayerNorm''', '''encoder_attn_layer_norm'''), ('''attention.encdec_output.dense''', '''encoder_attn.out_proj'''), ('''attention.encdec''', '''encoder_attn'''), ('''key''', '''k_proj'''), ('''value''', '''v_proj'''), ('''query''', '''q_proj'''), ('''decoder.LayerNorm''', '''decoder.layernorm_embedding'''), ] + END_COMMON ) _UpperCAmelCase : Union[str, Any] = ( INIT_COMMON + [ ('''embeddings.word_embeddings''', '''shared.weight'''), ('''embeddings.position_embeddings''', '''embed_positions.weight'''), ('''attention.self.LayerNorm''', '''self_attn_layer_norm'''), ('''attention.output.dense''', '''self_attn.output'''), ('''attention.self''', '''self_attn.self'''), ('''encoder.LayerNorm''', '''encoder.layernorm_embedding'''), ] + END_COMMON ) _UpperCAmelCase : int = [ '''encdec/key/bias''', '''encdec/query/bias''', '''encdec/value/bias''', '''self/key/bias''', '''self/query/bias''', '''self/value/bias''', '''encdec_output/dense/bias''', '''attention/output/dense/bias''', ] def __lowerCamelCase ( UpperCamelCase__ , UpperCamelCase__ ): '''simple docstring''' for tf_name, hf_name in patterns: snake_case_ = k.replace(a__ , a__ ) return k def __lowerCamelCase ( UpperCamelCase__ , UpperCamelCase__ ): '''simple docstring''' snake_case_ = BigBirdPegasusConfig(**a__ ) snake_case_ = BigBirdPegasusForConditionalGeneration(a__ ) snake_case_ = torch_model.state_dict() snake_case_ = {} # separating decoder weights snake_case_ = {k: tf_weights[k] for k in tf_weights if k.startswith('pegasus/decoder' )} snake_case_ = {k: tf_weights[k] for k in tf_weights if not k.startswith('pegasus/decoder' )} for k, v in tqdm(decoder_weights.items() , 'tf -> hf conversion' ): snake_case_ = [k.endswith(a__ ) for ending in KEYS_TO_IGNORE] if any(a__ ): continue snake_case_ = DECODER_PATTERNS snake_case_ = rename_state_dict_key(a__ , a__ ) if new_k not in state_dict: raise ValueError(F'''could not find new key {new_k} in state dict. (converted from {k})''' ) if any(True if i in k else False for i in ['dense', 'query', 'key', 'value'] ): snake_case_ = v.T snake_case_ = torch.from_numpy(a__ ) assert v.shape == state_dict[new_k].shape, F'''{new_k}, {k}, {v.shape}, {state_dict[new_k].shape}''' for k, v in tqdm(remaining_weights.items() , 'tf -> hf conversion' ): snake_case_ = [k.endswith(a__ ) for ending in KEYS_TO_IGNORE] if any(a__ ): continue snake_case_ = REMAINING_PATTERNS snake_case_ = rename_state_dict_key(a__ , a__ ) if new_k not in state_dict and k != "pegasus/embeddings/position_embeddings": raise ValueError(F'''could not find new key {new_k} in state dict. (converted from {k})''' ) if any(True if i in k else False for i in ['dense', 'query', 'key', 'value'] ): snake_case_ = v.T snake_case_ = torch.from_numpy(a__ ) if k != "pegasus/embeddings/position_embeddings": assert v.shape == state_dict[new_k].shape, F'''{new_k}, {k}, {v.shape}, {state_dict[new_k].shape}''' snake_case_ = mapping['model.embed_positions.weight'] snake_case_ = mapping.pop('model.embed_positions.weight' ) snake_case_ , snake_case_ = torch_model.load_state_dict(a__ , strict=a__ ) snake_case_ = [ k for k in missing if k not in [ 'final_logits_bias', 'model.encoder.embed_tokens.weight', 'model.decoder.embed_tokens.weight', 'lm_head.weight', ] ] assert unexpected_missing == [], F'''no matches found for the following torch keys {unexpected_missing}''' assert extra == [], F'''no matches found for the following tf keys {extra}''' return torch_model def __lowerCamelCase ( UpperCamelCase__ ): '''simple docstring''' snake_case_ = tf.train.list_variables(a__ ) snake_case_ = {} snake_case_ = ['global_step'] for name, shape in tqdm(a__ , desc='converting tf checkpoint to dict' ): snake_case_ = any(pat in name for pat in ignore_name ) if skip_key: continue snake_case_ = tf.train.load_variable(a__ , a__ ) snake_case_ = array return tf_weights def __lowerCamelCase ( UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ ): '''simple docstring''' snake_case_ = get_tf_weights_as_numpy(a__ ) snake_case_ = convert_bigbird_pegasus(a__ , a__ ) torch_model.save_pretrained(a__ ) if __name__ == "__main__": _UpperCAmelCase : str = argparse.ArgumentParser() parser.add_argument("""--tf_ckpt_path""", type=str, help="""passed to tf.train.list_variables""") parser.add_argument("""--save_dir""", default=None, type=str, help="""Path to the output PyTorch model.""") _UpperCAmelCase : Any = parser.parse_args() _UpperCAmelCase : str = {} convert_bigbird_pegasus_ckpt_to_pytorch(args.tf_ckpt_path, args.save_dir, config_update=config_update)
362
import json from typing import TYPE_CHECKING, List, Optional, Tuple from tokenizers import pre_tokenizers, processors from ...tokenization_utils_base import AddedToken, BatchEncoding from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import logging from .tokenization_blenderbot import BlenderbotTokenizer if TYPE_CHECKING: from transformers.pipelines.conversational import Conversation snake_case_ : str = logging.get_logger(__name__) snake_case_ : List[Any] = { '''vocab_file''': '''vocab.json''', '''merges_file''': '''merges.txt''', '''tokenizer_config_file''': '''tokenizer_config.json''', } snake_case_ : Any = { '''vocab_file''': {'''facebook/blenderbot-3B''': '''https://huggingface.co/facebook/blenderbot-3B/resolve/main/vocab.json'''}, '''merges_file''': {'''facebook/blenderbot-3B''': '''https://huggingface.co/facebook/blenderbot-3B/resolve/main/merges.txt'''}, '''tokenizer_config_file''': { '''facebook/blenderbot-3B''': '''https://huggingface.co/facebook/blenderbot-3B/resolve/main/tokenizer_config.json''' }, } snake_case_ : List[str] = {'''facebook/blenderbot-3B''': 1_28} class A__ ( UpperCamelCase__ ): UpperCAmelCase = VOCAB_FILES_NAMES UpperCAmelCase = PRETRAINED_VOCAB_FILES_MAP UpperCAmelCase = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES UpperCAmelCase = ["input_ids", "attention_mask"] UpperCAmelCase = BlenderbotTokenizer def __init__( self : Dict , _a : str=None , _a : Optional[int]=None , _a : List[str]=None , _a : int="replace" , _a : Dict="<s>" , _a : Optional[Any]="</s>" , _a : Any="</s>" , _a : int="<s>" , _a : int="<unk>" , _a : Optional[int]="<pad>" , _a : Tuple="<mask>" , _a : Tuple=False , _a : Union[str, Any]=True , **_a : List[str] , ) -> Optional[int]: """simple docstring""" super().__init__( _a , _a , tokenizer_file=_a , errors=_a , bos_token=_a , eos_token=_a , sep_token=_a , cls_token=_a , unk_token=_a , pad_token=_a , mask_token=_a , add_prefix_space=_a , trim_offsets=_a , **_a , ) _SCREAMING_SNAKE_CASE =json.loads(self.backend_tokenizer.pre_tokenizer.__getstate__() ) if pre_tok_state.get('''add_prefix_space''' , _a ) != add_prefix_space: _SCREAMING_SNAKE_CASE =getattr(_a , pre_tok_state.pop('''type''' ) ) _SCREAMING_SNAKE_CASE =add_prefix_space _SCREAMING_SNAKE_CASE =pre_tok_class(**_a ) _SCREAMING_SNAKE_CASE =add_prefix_space _SCREAMING_SNAKE_CASE ='''post_processor''' _SCREAMING_SNAKE_CASE =getattr(self.backend_tokenizer , _a , _a ) if tokenizer_component_instance: _SCREAMING_SNAKE_CASE =json.loads(tokenizer_component_instance.__getstate__() ) # The lists 'sep' and 'cls' must be cased in tuples for the object `post_processor_class` if "sep" in state: _SCREAMING_SNAKE_CASE =tuple(state['''sep'''] ) if "cls" in state: _SCREAMING_SNAKE_CASE =tuple(state['''cls'''] ) _SCREAMING_SNAKE_CASE =False if state.get('''add_prefix_space''' , _a ) != add_prefix_space: _SCREAMING_SNAKE_CASE =add_prefix_space _SCREAMING_SNAKE_CASE =True if state.get('''trim_offsets''' , _a ) != trim_offsets: _SCREAMING_SNAKE_CASE =trim_offsets _SCREAMING_SNAKE_CASE =True if changes_to_apply: _SCREAMING_SNAKE_CASE =getattr(_a , state.pop('''type''' ) ) _SCREAMING_SNAKE_CASE =component_class(**_a ) setattr(self.backend_tokenizer , _a , _a ) @property # Copied from transformers.models.roberta.tokenization_roberta_fast.RobertaTokenizerFast.mask_token with Roberta->Blenderbot, RoBERTa->Blenderbot def __UpperCamelCase ( self : Tuple ) -> str: """simple docstring""" if self._mask_token is None: if self.verbose: logger.error('''Using mask_token, but it is not set yet.''' ) return None return str(self._mask_token ) @mask_token.setter def __UpperCamelCase ( self : Optional[Any] , _a : str ) -> List[str]: """simple docstring""" _SCREAMING_SNAKE_CASE =AddedToken(_a , lstrip=_a , rstrip=_a ) if isinstance(_a , _a ) else value _SCREAMING_SNAKE_CASE =value def __UpperCamelCase ( self : Optional[Any] , *_a : str , **_a : int ) -> BatchEncoding: """simple docstring""" _SCREAMING_SNAKE_CASE =kwargs.get('''is_split_into_words''' , _a ) assert self.add_prefix_space or not is_split_into_words, ( f"You need to instantiate {self.__class__.__name__} with add_prefix_space=True " "to use it with pretokenized inputs." ) return super()._batch_encode_plus(*_a , **_a ) def __UpperCamelCase ( self : List[Any] , *_a : Optional[int] , **_a : Union[str, Any] ) -> BatchEncoding: """simple docstring""" _SCREAMING_SNAKE_CASE =kwargs.get('''is_split_into_words''' , _a ) assert self.add_prefix_space or not is_split_into_words, ( f"You need to instantiate {self.__class__.__name__} with add_prefix_space=True " "to use it with pretokenized inputs." ) return super()._encode_plus(*_a , **_a ) def __UpperCamelCase ( self : Dict , _a : str , _a : Optional[str] = None ) -> Tuple[str]: """simple docstring""" _SCREAMING_SNAKE_CASE =self._tokenizer.model.save(_a , name=_a ) return tuple(_a ) def __UpperCamelCase ( self : Tuple , _a : List[int] , _a : Optional[List[int]] = None ) -> List[int]: """simple docstring""" _SCREAMING_SNAKE_CASE =[self.sep_token_id] _SCREAMING_SNAKE_CASE =[self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep ) * [0] return len(cls + token_ids_a + sep + sep + token_ids_a + sep ) * [0] def __UpperCamelCase ( self : Tuple , _a : List[int] , _a : Optional[List[int]] = None ) -> Optional[Any]: """simple docstring""" return token_ids_a + [self.eos_token_id] def __UpperCamelCase ( self : Any , _a : "Conversation" ) -> List[int]: """simple docstring""" _SCREAMING_SNAKE_CASE =[] for is_user, text in conversation.iter_texts(): if is_user: # We need to space prefix as it's being done within blenderbot inputs.append(''' ''' + text ) else: # Generated responses should contain them already. inputs.append(_a ) _SCREAMING_SNAKE_CASE =''' '''.join(_a ) _SCREAMING_SNAKE_CASE =self.encode(_a ) if len(_a ) > self.model_max_length: _SCREAMING_SNAKE_CASE =input_ids[-self.model_max_length :] logger.warning(f"Trimmed input from conversation as it was longer than {self.model_max_length} tokens." ) return input_ids
691
0
"""simple docstring""" import gc import random import unittest import numpy as np import torch from PIL import Image from transformers import XLMRobertaTokenizerFast from diffusers import DDIMScheduler, KandinskyImgaImgPipeline, KandinskyPriorPipeline, UNetaDConditionModel, VQModel from diffusers.pipelines.kandinsky.text_encoder import MCLIPConfig, MultilingualCLIP from diffusers.utils import floats_tensor, load_image, load_numpy, slow, torch_device from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu from ..test_pipelines_common import PipelineTesterMixin, assert_mean_pixel_difference enable_full_determinism() class lowerCamelCase ( UpperCamelCase__ , unittest.TestCase ): '''simple docstring''' SCREAMING_SNAKE_CASE = KandinskyImgaImgPipeline SCREAMING_SNAKE_CASE = ['prompt', 'image_embeds', 'negative_image_embeds', 'image'] SCREAMING_SNAKE_CASE = [ 'prompt', 'negative_prompt', 'image_embeds', 'negative_image_embeds', 'image', ] SCREAMING_SNAKE_CASE = [ 'generator', 'height', 'width', 'strength', 'guidance_scale', 'negative_prompt', 'num_inference_steps', 'return_dict', 'guidance_scale', 'num_images_per_prompt', 'output_type', 'return_dict', ] SCREAMING_SNAKE_CASE = False @property def _a (self ): """simple docstring""" return 32 @property def _a (self ): """simple docstring""" return 32 @property def _a (self ): """simple docstring""" return self.time_input_dim @property def _a (self ): """simple docstring""" return self.time_input_dim * 4 @property def _a (self ): """simple docstring""" return 100 @property def _a (self ): """simple docstring""" UpperCAmelCase__ : Optional[int] = XLMRobertaTokenizerFast.from_pretrained("""YiYiXu/tiny-random-mclip-base""" ) return tokenizer @property def _a (self ): """simple docstring""" torch.manual_seed(0 ) UpperCAmelCase__ : Optional[int] = MCLIPConfig( numDims=self.cross_attention_dim , transformerDimensions=self.text_embedder_hidden_size , hidden_size=self.text_embedder_hidden_size , intermediate_size=37 , num_attention_heads=4 , num_hidden_layers=5 , vocab_size=1005 , ) UpperCAmelCase__ : int = MultilingualCLIP(_a ) UpperCAmelCase__ : Tuple = text_encoder.eval() return text_encoder @property def _a (self ): """simple docstring""" torch.manual_seed(0 ) UpperCAmelCase__ : Tuple = { """in_channels""": 4, # Out channels is double in channels because predicts mean and variance """out_channels""": 8, """addition_embed_type""": """text_image""", """down_block_types""": ("""ResnetDownsampleBlock2D""", """SimpleCrossAttnDownBlock2D"""), """up_block_types""": ("""SimpleCrossAttnUpBlock2D""", """ResnetUpsampleBlock2D"""), """mid_block_type""": """UNetMidBlock2DSimpleCrossAttn""", """block_out_channels""": (self.block_out_channels_a, self.block_out_channels_a * 2), """layers_per_block""": 1, """encoder_hid_dim""": self.text_embedder_hidden_size, """encoder_hid_dim_type""": """text_image_proj""", """cross_attention_dim""": self.cross_attention_dim, """attention_head_dim""": 4, """resnet_time_scale_shift""": """scale_shift""", """class_embed_type""": None, } UpperCAmelCase__ : List[Any] = UNetaDConditionModel(**_a ) return model @property def _a (self ): """simple docstring""" return { "block_out_channels": [32, 64], "down_block_types": ["DownEncoderBlock2D", "AttnDownEncoderBlock2D"], "in_channels": 3, "latent_channels": 4, "layers_per_block": 1, "norm_num_groups": 8, "norm_type": "spatial", "num_vq_embeddings": 12, "out_channels": 3, "up_block_types": [ "AttnUpDecoderBlock2D", "UpDecoderBlock2D", ], "vq_embed_dim": 4, } @property def _a (self ): """simple docstring""" torch.manual_seed(0 ) UpperCAmelCase__ : List[str] = VQModel(**self.dummy_movq_kwargs ) return model def _a (self ): """simple docstring""" UpperCAmelCase__ : Tuple = self.dummy_text_encoder UpperCAmelCase__ : Any = self.dummy_tokenizer UpperCAmelCase__ : Dict = self.dummy_unet UpperCAmelCase__ : Optional[int] = self.dummy_movq UpperCAmelCase__ : Optional[int] = { """num_train_timesteps""": 1000, """beta_schedule""": """linear""", """beta_start""": 0.00_085, """beta_end""": 0.012, """clip_sample""": False, """set_alpha_to_one""": False, """steps_offset""": 0, """prediction_type""": """epsilon""", """thresholding""": False, } UpperCAmelCase__ : Any = DDIMScheduler(**_a ) UpperCAmelCase__ : str = { """text_encoder""": text_encoder, """tokenizer""": tokenizer, """unet""": unet, """scheduler""": scheduler, """movq""": movq, } return components def _a (self , _lowerCamelCase , _lowerCamelCase=0 ): """simple docstring""" UpperCAmelCase__ : List[str] = floats_tensor((1, self.cross_attention_dim) , rng=random.Random(_a ) ).to(_a ) UpperCAmelCase__ : List[str] = floats_tensor((1, self.cross_attention_dim) , rng=random.Random(seed + 1 ) ).to(_a ) # create init_image UpperCAmelCase__ : Optional[int] = floats_tensor((1, 3, 64, 64) , rng=random.Random(_a ) ).to(_a ) UpperCAmelCase__ : Any = image.cpu().permute(0 , 2 , 3 , 1 )[0] UpperCAmelCase__ : List[Any] = Image.fromarray(np.uinta(_a ) ).convert("""RGB""" ).resize((256, 256) ) if str(_a ).startswith("""mps""" ): UpperCAmelCase__ : List[Any] = torch.manual_seed(_a ) else: UpperCAmelCase__ : List[str] = torch.Generator(device=_a ).manual_seed(_a ) UpperCAmelCase__ : int = { """prompt""": """horse""", """image""": init_image, """image_embeds""": image_embeds, """negative_image_embeds""": negative_image_embeds, """generator""": generator, """height""": 64, """width""": 64, """num_inference_steps""": 10, """guidance_scale""": 7.0, """strength""": 0.2, """output_type""": """np""", } return inputs def _a (self ): """simple docstring""" UpperCAmelCase__ : Union[str, Any] = """cpu""" UpperCAmelCase__ : Optional[int] = self.get_dummy_components() UpperCAmelCase__ : Optional[int] = self.pipeline_class(**_a ) UpperCAmelCase__ : Optional[Any] = pipe.to(_a ) pipe.set_progress_bar_config(disable=_a ) UpperCAmelCase__ : Dict = pipe(**self.get_dummy_inputs(_a ) ) UpperCAmelCase__ : Optional[Any] = output.images UpperCAmelCase__ : str = pipe( **self.get_dummy_inputs(_a ) , return_dict=_a , )[0] UpperCAmelCase__ : List[Any] = image[0, -3:, -3:, -1] UpperCAmelCase__ : Any = image_from_tuple[0, -3:, -3:, -1] assert image.shape == (1, 64, 64, 3) UpperCAmelCase__ : List[Any] = np.array( [0.61_474_943, 0.6_073_539, 0.43_308_544, 0.5_928_269, 0.47_493_595, 0.46_755_973, 0.4_613_838, 0.45_368_797, 0.50_119_233] ) assert ( np.abs(image_slice.flatten() - expected_slice ).max() < 1e-2 ), F""" expected_slice {expected_slice}, but got {image_slice.flatten()}""" assert ( np.abs(image_from_tuple_slice.flatten() - expected_slice ).max() < 1e-2 ), F""" expected_slice {expected_slice}, but got {image_from_tuple_slice.flatten()}""" @slow @require_torch_gpu class lowerCamelCase ( unittest.TestCase ): '''simple docstring''' def _a (self ): """simple docstring""" super().tearDown() gc.collect() torch.cuda.empty_cache() def _a (self ): """simple docstring""" UpperCAmelCase__ : Optional[int] = load_numpy( """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main""" """/kandinsky/kandinsky_img2img_frog.npy""" ) UpperCAmelCase__ : Union[str, Any] = load_image( """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main""" """/kandinsky/cat.png""" ) UpperCAmelCase__ : List[str] = """A red cartoon frog, 4k""" UpperCAmelCase__ : List[Any] = KandinskyPriorPipeline.from_pretrained( """kandinsky-community/kandinsky-2-1-prior""" , torch_dtype=torch.floataa ) pipe_prior.to(_a ) UpperCAmelCase__ : Dict = KandinskyImgaImgPipeline.from_pretrained( """kandinsky-community/kandinsky-2-1""" , torch_dtype=torch.floataa ) UpperCAmelCase__ : Optional[int] = pipeline.to(_a ) pipeline.set_progress_bar_config(disable=_a ) UpperCAmelCase__ : Union[str, Any] = torch.Generator(device="""cpu""" ).manual_seed(0 ) UpperCAmelCase__ , UpperCAmelCase__ : Tuple = pipe_prior( _a , generator=_a , num_inference_steps=5 , negative_prompt="""""" , ).to_tuple() UpperCAmelCase__ : Optional[Any] = pipeline( _a , image=_a , image_embeds=_a , negative_image_embeds=_a , generator=_a , num_inference_steps=100 , height=768 , width=768 , strength=0.2 , output_type="""np""" , ) UpperCAmelCase__ : Any = output.images[0] assert image.shape == (768, 768, 3) assert_mean_pixel_difference(_a , _a )
182
import json import os import shutil import tempfile import unittest import numpy as np import pytest from transformers import BertTokenizer, BertTokenizerFast from transformers.models.bert.tokenization_bert import VOCAB_FILES_NAMES from transformers.testing_utils import require_vision from transformers.utils import FEATURE_EXTRACTOR_NAME, is_vision_available if is_vision_available(): from PIL import Image from transformers import ChineseCLIPImageProcessor, ChineseCLIPProcessor @require_vision class A__ ( unittest.TestCase ): def __UpperCamelCase ( self : int ) -> Any: """simple docstring""" _SCREAMING_SNAKE_CASE =tempfile.mkdtemp() _SCREAMING_SNAKE_CASE =[ '''[UNK]''', '''[CLS]''', '''[SEP]''', '''[PAD]''', '''[MASK]''', '''็š„''', '''ไปท''', '''ๆ ผ''', '''ๆ˜ฏ''', '''15''', '''ไพฟ''', '''alex''', '''##andra''', '''๏ผŒ''', '''ใ€‚''', '''-''', '''t''', '''shirt''', ] _SCREAMING_SNAKE_CASE =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] ) ) _SCREAMING_SNAKE_CASE ={ '''do_resize''': True, '''size''': {'''height''': 224, '''width''': 224}, '''do_center_crop''': True, '''crop_size''': {'''height''': 18, '''width''': 18}, '''do_normalize''': True, '''image_mean''': [0.48_14_54_66, 0.4_57_82_75, 0.40_82_10_73], '''image_std''': [0.26_86_29_54, 0.26_13_02_58, 0.27_57_77_11], '''do_convert_rgb''': True, } _SCREAMING_SNAKE_CASE =os.path.join(self.tmpdirname , _a ) with open(self.image_processor_file , '''w''' , encoding='''utf-8''' ) as fp: json.dump(_a , _a ) def __UpperCamelCase ( self : Optional[int] , **_a : str ) -> List[str]: """simple docstring""" return BertTokenizer.from_pretrained(self.tmpdirname , **_a ) def __UpperCamelCase ( self : List[Any] , **_a : Any ) -> Dict: """simple docstring""" return BertTokenizerFast.from_pretrained(self.tmpdirname , **_a ) def __UpperCamelCase ( self : int , **_a : Optional[Any] ) -> Any: """simple docstring""" return ChineseCLIPImageProcessor.from_pretrained(self.tmpdirname , **_a ) def __UpperCamelCase ( self : str ) -> Union[str, Any]: """simple docstring""" shutil.rmtree(self.tmpdirname ) def __UpperCamelCase ( self : int ) -> Optional[Any]: """simple docstring""" _SCREAMING_SNAKE_CASE =[np.random.randint(255 , size=(3, 30, 400) , dtype=np.uinta )] _SCREAMING_SNAKE_CASE =[Image.fromarray(np.moveaxis(_a , 0 , -1 ) ) for x in image_inputs] return image_inputs def __UpperCamelCase ( self : Any ) -> List[Any]: """simple docstring""" _SCREAMING_SNAKE_CASE =self.get_tokenizer() _SCREAMING_SNAKE_CASE =self.get_rust_tokenizer() _SCREAMING_SNAKE_CASE =self.get_image_processor() _SCREAMING_SNAKE_CASE =ChineseCLIPProcessor(tokenizer=_a , image_processor=_a ) processor_slow.save_pretrained(self.tmpdirname ) _SCREAMING_SNAKE_CASE =ChineseCLIPProcessor.from_pretrained(self.tmpdirname , use_fast=_a ) _SCREAMING_SNAKE_CASE =ChineseCLIPProcessor(tokenizer=_a , image_processor=_a ) processor_fast.save_pretrained(self.tmpdirname ) _SCREAMING_SNAKE_CASE =ChineseCLIPProcessor.from_pretrained(self.tmpdirname ) self.assertEqual(processor_slow.tokenizer.get_vocab() , tokenizer_slow.get_vocab() ) self.assertEqual(processor_fast.tokenizer.get_vocab() , tokenizer_fast.get_vocab() ) self.assertEqual(tokenizer_slow.get_vocab() , tokenizer_fast.get_vocab() ) self.assertIsInstance(processor_slow.tokenizer , _a ) self.assertIsInstance(processor_fast.tokenizer , _a ) self.assertEqual(processor_slow.image_processor.to_json_string() , image_processor.to_json_string() ) self.assertEqual(processor_fast.image_processor.to_json_string() , image_processor.to_json_string() ) self.assertIsInstance(processor_slow.image_processor , _a ) self.assertIsInstance(processor_fast.image_processor , _a ) def __UpperCamelCase ( self : str ) -> Union[str, Any]: """simple docstring""" _SCREAMING_SNAKE_CASE =ChineseCLIPProcessor(tokenizer=self.get_tokenizer() , image_processor=self.get_image_processor() ) processor.save_pretrained(self.tmpdirname ) _SCREAMING_SNAKE_CASE =self.get_tokenizer(cls_token='''(CLS)''' , sep_token='''(SEP)''' ) _SCREAMING_SNAKE_CASE =self.get_image_processor(do_normalize=_a ) _SCREAMING_SNAKE_CASE =ChineseCLIPProcessor.from_pretrained( self.tmpdirname , cls_token='''(CLS)''' , sep_token='''(SEP)''' , do_normalize=_a ) self.assertEqual(processor.tokenizer.get_vocab() , tokenizer_add_kwargs.get_vocab() ) self.assertIsInstance(processor.tokenizer , _a ) self.assertEqual(processor.image_processor.to_json_string() , image_processor_add_kwargs.to_json_string() ) self.assertIsInstance(processor.image_processor , _a ) def __UpperCamelCase ( self : List[Any] ) -> Tuple: """simple docstring""" _SCREAMING_SNAKE_CASE =self.get_image_processor() _SCREAMING_SNAKE_CASE =self.get_tokenizer() _SCREAMING_SNAKE_CASE =ChineseCLIPProcessor(tokenizer=_a , image_processor=_a ) _SCREAMING_SNAKE_CASE =self.prepare_image_inputs() _SCREAMING_SNAKE_CASE =image_processor(_a , return_tensors='''np''' ) _SCREAMING_SNAKE_CASE =processor(images=_a , return_tensors='''np''' ) for key in input_feat_extract.keys(): self.assertAlmostEqual(input_feat_extract[key].sum() , input_processor[key].sum() , delta=1E-2 ) def __UpperCamelCase ( self : Union[str, Any] ) -> Any: """simple docstring""" _SCREAMING_SNAKE_CASE =self.get_image_processor() _SCREAMING_SNAKE_CASE =self.get_tokenizer() _SCREAMING_SNAKE_CASE =ChineseCLIPProcessor(tokenizer=_a , image_processor=_a ) _SCREAMING_SNAKE_CASE ='''Alexandra๏ผŒT-shirt็š„ไปทๆ ผๆ˜ฏ15ไพฟๅฃซใ€‚''' _SCREAMING_SNAKE_CASE =processor(text=_a ) _SCREAMING_SNAKE_CASE =tokenizer(_a ) for key in encoded_tok.keys(): self.assertListEqual(encoded_tok[key] , encoded_processor[key] ) def __UpperCamelCase ( self : Tuple ) -> int: """simple docstring""" _SCREAMING_SNAKE_CASE =self.get_image_processor() _SCREAMING_SNAKE_CASE =self.get_tokenizer() _SCREAMING_SNAKE_CASE =ChineseCLIPProcessor(tokenizer=_a , image_processor=_a ) _SCREAMING_SNAKE_CASE ='''Alexandra๏ผŒT-shirt็š„ไปทๆ ผๆ˜ฏ15ไพฟๅฃซใ€‚''' _SCREAMING_SNAKE_CASE =self.prepare_image_inputs() _SCREAMING_SNAKE_CASE =processor(text=_a , images=_a ) self.assertListEqual(list(inputs.keys() ) , ['''input_ids''', '''token_type_ids''', '''attention_mask''', '''pixel_values'''] ) # test if it raises when no input is passed with pytest.raises(_a ): processor() def __UpperCamelCase ( self : Optional[Any] ) -> Optional[int]: """simple docstring""" _SCREAMING_SNAKE_CASE =self.get_image_processor() _SCREAMING_SNAKE_CASE =self.get_tokenizer() _SCREAMING_SNAKE_CASE =ChineseCLIPProcessor(tokenizer=_a , image_processor=_a ) _SCREAMING_SNAKE_CASE =[[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]] _SCREAMING_SNAKE_CASE =processor.batch_decode(_a ) _SCREAMING_SNAKE_CASE =tokenizer.batch_decode(_a ) self.assertListEqual(_a , _a ) def __UpperCamelCase ( self : Any ) -> List[str]: """simple docstring""" _SCREAMING_SNAKE_CASE =self.get_image_processor() _SCREAMING_SNAKE_CASE =self.get_tokenizer() _SCREAMING_SNAKE_CASE =ChineseCLIPProcessor(tokenizer=_a , image_processor=_a ) _SCREAMING_SNAKE_CASE ='''Alexandra๏ผŒT-shirt็š„ไปทๆ ผๆ˜ฏ15ไพฟๅฃซใ€‚''' _SCREAMING_SNAKE_CASE =self.prepare_image_inputs() _SCREAMING_SNAKE_CASE =processor(text=_a , images=_a ) self.assertListEqual(list(inputs.keys() ) , processor.model_input_names )
691
0
"""simple docstring""" UpperCAmelCase : int = range(2, 20 + 1) UpperCAmelCase : Any = [10**k for k in range(ks[-1] + 1)] UpperCAmelCase : dict[int, dict[int, list[list[int]]]] = {} def lowerCamelCase ( _UpperCamelCase : List[Any] , _UpperCamelCase : int , _UpperCamelCase : List[Any] , _UpperCamelCase : Union[str, Any] ) -> Optional[int]: '''simple docstring''' __UpperCAmelCase : Any = sum(a_i[j] for j in range(a__ , len(a__ ) ) ) __UpperCAmelCase : Tuple = sum(a_i[j] * base[j] for j in range(min(len(a__ ) , a__ ) ) ) __UpperCAmelCase ,__UpperCAmelCase : Dict = 0, 0 __UpperCAmelCase : Any = n - i __UpperCAmelCase : int = memo.get(a__ ) if sub_memo is not None: __UpperCAmelCase : Optional[Any] = sub_memo.get(a__ ) if jumps is not None and len(a__ ) > 0: # find and make the largest jump without going over __UpperCAmelCase : List[Any] = -1 for _k in range(len(a__ ) - 1 , -1 , -1 ): if jumps[_k][2] <= k and jumps[_k][1] <= max_dn: __UpperCAmelCase : List[Any] = _k break if max_jump >= 0: __UpperCAmelCase ,__UpperCAmelCase ,__UpperCAmelCase : Tuple = jumps[max_jump] # since the difference between jumps is cached, add c __UpperCAmelCase : List[str] = diff + c for j in range(min(a__ , len(a__ ) ) ): __UpperCAmelCase ,__UpperCAmelCase : Dict = divmod(a__ , 1_0 ) if new_c > 0: add(a__ , a__ , a__ ) else: __UpperCAmelCase : Optional[Any] = [] else: __UpperCAmelCase : Any = {c: []} __UpperCAmelCase : Optional[Any] = sub_memo if dn >= max_dn or c + diff >= base[k]: return diff, dn if k > ks[0]: while True: # keep doing smaller jumps __UpperCAmelCase ,__UpperCAmelCase : Dict = next_term(a__ , k - 1 , i + dn , a__ ) 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 __UpperCAmelCase ,__UpperCAmelCase : Union[str, Any] = compute(a__ , a__ , i + dn , a__ ) diff += _diff dn += terms_jumped __UpperCAmelCase : Optional[int] = sub_memo[c] # keep jumps sorted by # of terms skipped __UpperCAmelCase : Dict = 0 while j < len(a__ ): if jumps[j][1] > dn: break j += 1 # cache the jump for this value digitsum(b) and c sub_memo[c].insert(a__ , (diff, dn, k) ) return (diff, dn) def lowerCamelCase ( _UpperCamelCase : List[str] , _UpperCamelCase : Dict , _UpperCamelCase : List[str] , _UpperCamelCase : int ) -> Union[str, Any]: '''simple docstring''' if i >= n: return 0, i if k > len(a__ ): a_i.extend([0 for _ in range(k - len(a__ ) )] ) # note: a_i -> b * 10^k + c # ds_b -> digitsum(b) # ds_c -> digitsum(c) __UpperCAmelCase : Any = i __UpperCAmelCase ,__UpperCAmelCase ,__UpperCAmelCase : Optional[int] = 0, 0, 0 for j in range(len(a__ ) ): if j >= k: ds_b += a_i[j] else: ds_c += a_i[j] while i < n: i += 1 __UpperCAmelCase : List[str] = ds_c + ds_b diff += addend __UpperCAmelCase : int = 0 for j in range(a__ ): __UpperCAmelCase : List[str] = a_i[j] + addend __UpperCAmelCase ,__UpperCAmelCase : List[Any] = divmod(a__ , 1_0 ) ds_c += a_i[j] if addend > 0: break if addend > 0: add(a__ , a__ , a__ ) return diff, i - start_i def lowerCamelCase ( _UpperCamelCase : List[Any] , _UpperCamelCase : str , _UpperCamelCase : Optional[int] ) -> Optional[int]: '''simple docstring''' for j in range(a__ , len(a__ ) ): __UpperCAmelCase : Optional[int] = digits[j] + addend if s >= 1_0: __UpperCAmelCase ,__UpperCAmelCase : Union[str, Any] = divmod(a__ , 1_0 ) __UpperCAmelCase : Union[str, Any] = addend // 1_0 + quotient else: __UpperCAmelCase : List[str] = s __UpperCAmelCase : Optional[int] = addend // 1_0 if addend == 0: break while addend > 0: __UpperCAmelCase ,__UpperCAmelCase : Optional[int] = divmod(a__ , 1_0 ) digits.append(a__ ) def lowerCamelCase ( _UpperCamelCase : int = 1_0**1_5 ) -> Optional[Any]: '''simple docstring''' __UpperCAmelCase : Dict = [1] __UpperCAmelCase : Optional[int] = 1 __UpperCAmelCase : Optional[int] = 0 while True: __UpperCAmelCase ,__UpperCAmelCase : str = next_term(a__ , 2_0 , i + dn , a__ ) dn += terms_jumped if dn == n - i: break __UpperCAmelCase : Optional[int] = 0 for j in range(len(a__ ) ): a_n += digits[j] * 1_0**j return a_n if __name__ == "__main__": print(F"{solution() = }")
139
# Usage: # ./gen-card-allenai-wmt16.py import os from pathlib import Path def lowerCamelCase( a__ ,a__ ,a__ ,a__): _SCREAMING_SNAKE_CASE ={ '''en''': '''Machine learning is great, isn\'t it?''', '''ru''': '''ะœะฐัˆะธะฝะฝะพะต ะพะฑัƒั‡ะตะฝะธะต - ัั‚ะพ ะทะดะพั€ะพะฒะพ, ะฝะต ั‚ะฐะบ ะปะธ?''', '''de''': '''Maschinelles Lernen ist groรŸartig, nicht wahr?''', } # BLUE scores as follows: # "pair": [fairseq, transformers] _SCREAMING_SNAKE_CASE ={ '''wmt16-en-de-dist-12-1''': [28.3, 27.52], '''wmt16-en-de-dist-6-1''': [27.4, 27.11], '''wmt16-en-de-12-1''': [26.9, 25.75], } _SCREAMING_SNAKE_CASE =f"{src_lang}-{tgt_lang}" _SCREAMING_SNAKE_CASE =f"\n---\nlanguage:\n- {src_lang}\n- {tgt_lang}\nthumbnail:\ntags:\n- translation\n- wmt16\n- allenai\nlicense: apache-2.0\ndatasets:\n- wmt16\nmetrics:\n- bleu\n---\n\n# FSMT\n\n## Model description\n\nThis is a ported version of fairseq-based [wmt16 transformer](https://github.com/jungokasai/deep-shallow/) for {src_lang}-{tgt_lang}.\n\nFor more details, please, see [Deep Encoder, Shallow Decoder: Reevaluating the Speed-Quality Tradeoff in Machine Translation](https://arxiv.org/abs/2006.10369).\n\nAll 3 models are available:\n\n* [wmt16-en-de-dist-12-1](https://huggingface.co/allenai/wmt16-en-de-dist-12-1)\n* [wmt16-en-de-dist-6-1](https://huggingface.co/allenai/wmt16-en-de-dist-6-1)\n* [wmt16-en-de-12-1](https://huggingface.co/allenai/wmt16-en-de-12-1)\n\n\n## Intended uses & limitations\n\n#### How to use\n\n```python\nfrom transformers import FSMTForConditionalGeneration, FSMTTokenizer\nmname = \"allenai/{model_name}\"\ntokenizer = FSMTTokenizer.from_pretrained(mname)\nmodel = FSMTForConditionalGeneration.from_pretrained(mname)\n\ninput = \"{texts[src_lang]}\"\ninput_ids = tokenizer.encode(input, return_tensors=\"pt\")\noutputs = model.generate(input_ids)\ndecoded = tokenizer.decode(outputs[0], skip_special_tokens=True)\nprint(decoded) # {texts[tgt_lang]}\n\n```\n\n#### Limitations and bias\n\n\n## Training data\n\nPretrained weights were left identical to the original model released by allenai. For more details, please, see the [paper](https://arxiv.org/abs/2006.10369).\n\n## Eval results\n\nHere are the BLEU scores:\n\nmodel | fairseq | transformers\n-------|---------|----------\n{model_name} | {scores[model_name][0]} | {scores[model_name][1]}\n\nThe score is slightly below the score reported in the paper, as the researchers don't use `sacrebleu` and measure the score on tokenized outputs. `transformers` score was measured using `sacrebleu` on detokenized outputs.\n\nThe score was calculated using this code:\n\n```bash\ngit clone https://github.com/huggingface/transformers\ncd transformers\nexport PAIR={pair}\nexport DATA_DIR=data/$PAIR\nexport SAVE_DIR=data/$PAIR\nexport BS=8\nexport NUM_BEAMS=5\nmkdir -p $DATA_DIR\nsacrebleu -t wmt16 -l $PAIR --echo src > $DATA_DIR/val.source\nsacrebleu -t wmt16 -l $PAIR --echo ref > $DATA_DIR/val.target\necho $PAIR\nPYTHONPATH=\"src:examples/seq2seq\" python examples/seq2seq/run_eval.py allenai/{model_name} $DATA_DIR/val.source $SAVE_DIR/test_translations.txt --reference_path $DATA_DIR/val.target --score_path $SAVE_DIR/test_bleu.json --bs $BS --task translation --num_beams $NUM_BEAMS\n```\n\n## Data Sources\n\n- [training, etc.](http://www.statmt.org/wmt16/)\n- [test set](http://matrix.statmt.org/test_sets/newstest2016.tgz?1504722372)\n\n\n### BibTeX entry and citation info\n\n```\n@misc{{kasai2020deep,\n title={{Deep Encoder, Shallow Decoder: Reevaluating the Speed-Quality Tradeoff in Machine Translation}},\n author={{Jungo Kasai and Nikolaos Pappas and Hao Peng and James Cross and Noah A. Smith}},\n year={{2020}},\n eprint={{2006.10369}},\n archivePrefix={{arXiv}},\n primaryClass={{cs.CL}}\n}}\n```\n\n" model_card_dir.mkdir(parents=a__ ,exist_ok=a__) _SCREAMING_SNAKE_CASE =os.path.join(a__ ,'''README.md''') print(f"Generating {path}") with open(a__ ,'''w''' ,encoding='''utf-8''') as f: f.write(a__) # make sure we are under the root of the project snake_case_ : Any = Path(__file__).resolve().parent.parent.parent snake_case_ : Tuple = repo_dir / '''model_cards''' for model_name in ["wmt16-en-de-dist-12-1", "wmt16-en-de-dist-6-1", "wmt16-en-de-12-1"]: snake_case_ : Union[str, Any] = model_cards_dir / '''allenai''' / model_name write_model_card(model_card_dir, src_lang='''en''', tgt_lang='''de''', model_name=model_name)
691
0
from collections import OrderedDict from typing import Mapping from packaging import version from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging __UpperCamelCase : Optional[Any] = logging.get_logger(__name__) __UpperCamelCase : List[str] = { '''google/mobilenet_v1_1.0_224''': '''https://huggingface.co/google/mobilenet_v1_1.0_224/resolve/main/config.json''', '''google/mobilenet_v1_0.75_192''': '''https://huggingface.co/google/mobilenet_v1_0.75_192/resolve/main/config.json''', # See all MobileNetV1 models at https://huggingface.co/models?filter=mobilenet_v1 } class _UpperCamelCase ( UpperCamelCase__ ): '''simple docstring''' a_ : Union[str, Any] = "mobilenet_v1" def __init__( self : Dict , _lowerCamelCase : List[str]=3 , _lowerCamelCase : Optional[Any]=2_2_4 , _lowerCamelCase : Optional[int]=1.0 , _lowerCamelCase : List[Any]=8 , _lowerCamelCase : Any="relu6" , _lowerCamelCase : Union[str, Any]=True , _lowerCamelCase : List[str]=0.999 , _lowerCamelCase : Any=0.02 , _lowerCamelCase : List[Any]=0.001 , **_lowerCamelCase : List[Any] , ): '''simple docstring''' super().__init__(**_a ) if depth_multiplier <= 0: raise ValueError("""depth_multiplier must be greater than zero.""" ) __lowerCamelCase : str = num_channels __lowerCamelCase : Dict = image_size __lowerCamelCase : Dict = depth_multiplier __lowerCamelCase : int = min_depth __lowerCamelCase : Any = hidden_act __lowerCamelCase : List[str] = tf_padding __lowerCamelCase : Tuple = classifier_dropout_prob __lowerCamelCase : Optional[Any] = initializer_range __lowerCamelCase : List[Any] = layer_norm_eps class _UpperCamelCase ( UpperCamelCase__ ): '''simple docstring''' a_ : List[Any] = version.parse("1.11" ) @property def _snake_case ( self : Any ): '''simple docstring''' return OrderedDict([("""pixel_values""", {0: """batch"""})] ) @property def _snake_case ( self : Tuple ): '''simple docstring''' if self.task == "image-classification": return OrderedDict([("""logits""", {0: """batch"""})] ) else: return OrderedDict([("""last_hidden_state""", {0: """batch"""}), ("""pooler_output""", {0: """batch"""})] ) @property def _snake_case ( self : str ): '''simple docstring''' return 1E-4
519
from typing import TYPE_CHECKING from ....utils import _LazyModule snake_case_ : Dict = {'''tokenization_tapex''': ['''TapexTokenizer''']} if TYPE_CHECKING: from .tokenization_tapex import TapexTokenizer else: import sys snake_case_ : Union[str, Any] = _LazyModule(__name__, globals()['''__file__'''], _import_structure)
691
0
import argparse import json from pathlib import Path import requests import timm import torch from huggingface_hub import hf_hub_download from PIL import Image from timm.data import resolve_data_config from timm.data.transforms_factory import create_transform from transformers import ( BitConfig, ViTHybridConfig, ViTHybridForImageClassification, ViTHybridImageProcessor, ViTHybridModel, ) from transformers.image_utils import PILImageResampling from transformers.utils import logging logging.set_verbosity_info() SCREAMING_SNAKE_CASE : str = logging.get_logger(__name__) def __A ( _A , _A=False ): """simple docstring""" __a = [] # fmt: off # stem: rename_keys.append(("cls_token", "vit.embeddings.cls_token") ) rename_keys.append(("pos_embed", "vit.embeddings.position_embeddings") ) rename_keys.append(("patch_embed.proj.weight", "vit.embeddings.patch_embeddings.projection.weight") ) rename_keys.append(("patch_embed.proj.bias", "vit.embeddings.patch_embeddings.projection.bias") ) # backbone rename_keys.append(("patch_embed.backbone.stem.conv.weight", "vit.embeddings.patch_embeddings.backbone.bit.embedder.convolution.weight") ) rename_keys.append(("patch_embed.backbone.stem.norm.weight", "vit.embeddings.patch_embeddings.backbone.bit.embedder.norm.weight") ) rename_keys.append(("patch_embed.backbone.stem.norm.bias", "vit.embeddings.patch_embeddings.backbone.bit.embedder.norm.bias") ) for stage_idx in range(len(config.backbone_config.depths ) ): for layer_idx in range(config.backbone_config.depths[stage_idx] ): rename_keys.append((f"""patch_embed.backbone.stages.{stage_idx}.blocks.{layer_idx}.conv1.weight""", f"""vit.embeddings.patch_embeddings.backbone.bit.encoder.stages.{stage_idx}.layers.{layer_idx}.conv1.weight""") ) rename_keys.append((f"""patch_embed.backbone.stages.{stage_idx}.blocks.{layer_idx}.norm1.weight""", f"""vit.embeddings.patch_embeddings.backbone.bit.encoder.stages.{stage_idx}.layers.{layer_idx}.norm1.weight""") ) rename_keys.append((f"""patch_embed.backbone.stages.{stage_idx}.blocks.{layer_idx}.norm1.bias""", f"""vit.embeddings.patch_embeddings.backbone.bit.encoder.stages.{stage_idx}.layers.{layer_idx}.norm1.bias""") ) rename_keys.append((f"""patch_embed.backbone.stages.{stage_idx}.blocks.{layer_idx}.conv2.weight""", f"""vit.embeddings.patch_embeddings.backbone.bit.encoder.stages.{stage_idx}.layers.{layer_idx}.conv2.weight""") ) rename_keys.append((f"""patch_embed.backbone.stages.{stage_idx}.blocks.{layer_idx}.norm2.weight""", f"""vit.embeddings.patch_embeddings.backbone.bit.encoder.stages.{stage_idx}.layers.{layer_idx}.norm2.weight""") ) rename_keys.append((f"""patch_embed.backbone.stages.{stage_idx}.blocks.{layer_idx}.norm2.bias""", f"""vit.embeddings.patch_embeddings.backbone.bit.encoder.stages.{stage_idx}.layers.{layer_idx}.norm2.bias""") ) rename_keys.append((f"""patch_embed.backbone.stages.{stage_idx}.blocks.{layer_idx}.conv3.weight""", f"""vit.embeddings.patch_embeddings.backbone.bit.encoder.stages.{stage_idx}.layers.{layer_idx}.conv3.weight""") ) rename_keys.append((f"""patch_embed.backbone.stages.{stage_idx}.blocks.{layer_idx}.norm3.weight""", f"""vit.embeddings.patch_embeddings.backbone.bit.encoder.stages.{stage_idx}.layers.{layer_idx}.norm3.weight""") ) rename_keys.append((f"""patch_embed.backbone.stages.{stage_idx}.blocks.{layer_idx}.norm3.bias""", f"""vit.embeddings.patch_embeddings.backbone.bit.encoder.stages.{stage_idx}.layers.{layer_idx}.norm3.bias""") ) rename_keys.append((f"""patch_embed.backbone.stages.{stage_idx}.blocks.0.downsample.conv.weight""", f"""vit.embeddings.patch_embeddings.backbone.bit.encoder.stages.{stage_idx}.layers.0.downsample.conv.weight""") ) rename_keys.append((f"""patch_embed.backbone.stages.{stage_idx}.blocks.0.downsample.norm.weight""", f"""vit.embeddings.patch_embeddings.backbone.bit.encoder.stages.{stage_idx}.layers.0.downsample.norm.weight""") ) rename_keys.append((f"""patch_embed.backbone.stages.{stage_idx}.blocks.0.downsample.norm.bias""", f"""vit.embeddings.patch_embeddings.backbone.bit.encoder.stages.{stage_idx}.layers.0.downsample.norm.bias""") ) # transformer encoder for i in range(config.num_hidden_layers ): # encoder layers: output projection, 2 feedforward neural networks and 2 layernorms rename_keys.append((f"""blocks.{i}.norm1.weight""", f"""vit.encoder.layer.{i}.layernorm_before.weight""") ) rename_keys.append((f"""blocks.{i}.norm1.bias""", f"""vit.encoder.layer.{i}.layernorm_before.bias""") ) rename_keys.append((f"""blocks.{i}.attn.proj.weight""", f"""vit.encoder.layer.{i}.attention.output.dense.weight""") ) rename_keys.append((f"""blocks.{i}.attn.proj.bias""", f"""vit.encoder.layer.{i}.attention.output.dense.bias""") ) rename_keys.append((f"""blocks.{i}.norm2.weight""", f"""vit.encoder.layer.{i}.layernorm_after.weight""") ) rename_keys.append((f"""blocks.{i}.norm2.bias""", f"""vit.encoder.layer.{i}.layernorm_after.bias""") ) rename_keys.append((f"""blocks.{i}.mlp.fc1.weight""", f"""vit.encoder.layer.{i}.intermediate.dense.weight""") ) rename_keys.append((f"""blocks.{i}.mlp.fc1.bias""", f"""vit.encoder.layer.{i}.intermediate.dense.bias""") ) rename_keys.append((f"""blocks.{i}.mlp.fc2.weight""", f"""vit.encoder.layer.{i}.output.dense.weight""") ) rename_keys.append((f"""blocks.{i}.mlp.fc2.bias""", f"""vit.encoder.layer.{i}.output.dense.bias""") ) if base_model: # layernorm + pooler rename_keys.extend( [ ("norm.weight", "layernorm.weight"), ("norm.bias", "layernorm.bias"), ("pre_logits.fc.weight", "pooler.dense.weight"), ("pre_logits.fc.bias", "pooler.dense.bias"), ] ) # if just the base model, we should remove "vit" from all keys that start with "vit" __a = [(pair[0], pair[1][4:]) if pair[1].startswith("vit" ) else pair for pair in rename_keys] else: # layernorm + classification head rename_keys.extend( [ ("norm.weight", "vit.layernorm.weight"), ("norm.bias", "vit.layernorm.bias"), ("head.weight", "classifier.weight"), ("head.bias", "classifier.bias"), ] ) # fmt: on return rename_keys def __A ( _A , _A , _A=False ): """simple docstring""" for i in range(config.num_hidden_layers ): if base_model: __a = "" else: __a = "vit." # read in weights + bias of input projection layer (in timm, this is a single matrix + bias) __a = state_dict.pop(f"""blocks.{i}.attn.qkv.weight""" ) __a = state_dict.pop(f"""blocks.{i}.attn.qkv.bias""" ) # next, add query, keys and values (in that order) to the state dict __a = in_proj_weight[ : config.hidden_size, : ] __a = in_proj_bias[: config.hidden_size] __a = in_proj_weight[ config.hidden_size : config.hidden_size * 2, : ] __a = in_proj_bias[ config.hidden_size : config.hidden_size * 2 ] __a = in_proj_weight[ -config.hidden_size :, : ] __a = in_proj_bias[-config.hidden_size :] def __A ( _A ): """simple docstring""" __a = ["head.weight", "head.bias"] for k in ignore_keys: state_dict.pop(a__ , a__ ) def __A ( _A , _A , _A ): """simple docstring""" __a = dct.pop(a__ ) __a = val def __A ( ): """simple docstring""" __a = "http://images.cocodataset.org/val2017/000000039769.jpg" __a = Image.open(requests.get(a__ , stream=a__ ).raw ) return im @torch.no_grad() def __A ( _A , _A , _A=False ): """simple docstring""" __a = BitConfig( global_padding="same" , layer_type="bottleneck" , depths=(3, 4, 9) , out_features=["stage3"] , embedding_dynamic_padding=a__ , ) __a = ViTHybridConfig(backbone_config=a__ , image_size=384 , num_labels=1000 ) __a = False # load original model from timm __a = timm.create_model(a__ , pretrained=a__ ) timm_model.eval() # load state_dict of original model, remove and rename some keys __a = timm_model.state_dict() if base_model: remove_classification_head_(a__ ) __a = create_rename_keys(a__ , a__ ) for src, dest in rename_keys: rename_key(a__ , a__ , a__ ) read_in_q_k_v(a__ , a__ , a__ ) __a = "huggingface/label-files" __a = "imagenet-1k-id2label.json" __a = json.load(open(hf_hub_download(a__ , a__ , repo_type="dataset" ) , "r" ) ) __a = {int(a__ ): v for k, v in idalabel.items()} __a = idalabel __a = {v: k for k, v in idalabel.items()} # load HuggingFace model if vit_name[-5:] == "in21k": __a = ViTHybridModel(a__ ).eval() else: __a = ViTHybridForImageClassification(a__ ).eval() model.load_state_dict(a__ ) # create image processor __a = create_transform(**resolve_data_config({} , model=a__ ) ) __a = transform.transforms __a = { "bilinear": PILImageResampling.BILINEAR, "bicubic": PILImageResampling.BICUBIC, "nearest": PILImageResampling.NEAREST, } __a = ViTHybridImageProcessor( do_resize=a__ , size={"shortest_edge": timm_transforms[0].size} , resample=pillow_resamplings[timm_transforms[0].interpolation.value] , do_center_crop=a__ , crop_size={"height": timm_transforms[1].size[0], "width": timm_transforms[1].size[1]} , do_normalize=a__ , image_mean=timm_transforms[-1].mean.tolist() , image_std=timm_transforms[-1].std.tolist() , ) __a = prepare_img() __a = transform(a__ ).unsqueeze(0 ) __a = processor(a__ , return_tensors="pt" ).pixel_values # verify pixel values assert torch.allclose(a__ , a__ ) # verify logits with torch.no_grad(): __a = model(a__ ) __a = outputs.logits print("Predicted class:" , logits.argmax(-1 ).item() ) if base_model: __a = timm_model.forward_features(a__ ) assert timm_pooled_output.shape == outputs.pooler_output.shape assert torch.allclose(a__ , outputs.pooler_output , atol=1E-3 ) else: __a = timm_model(a__ ) assert timm_logits.shape == outputs.logits.shape assert torch.allclose(a__ , outputs.logits , atol=1E-3 ) print("Looks ok!" ) if pytorch_dump_folder_path is not None: Path(a__ ).mkdir(exist_ok=a__ ) print(f"""Saving model {vit_name} to {pytorch_dump_folder_path}""" ) model.save_pretrained(a__ ) print(f"""Saving processor to {pytorch_dump_folder_path}""" ) processor.save_pretrained(a__ ) if push_to_hub: print(f"""Pushing model and processor to the hub {vit_name}""" ) model.push_to_hub(f"""ybelkada/{vit_name}""" ) processor.push_to_hub(f"""ybelkada/{vit_name}""" ) if __name__ == "__main__": SCREAMING_SNAKE_CASE : Dict = argparse.ArgumentParser() # Required parameters parser.add_argument( """--vit_name""", default="""vit_base_r50_s16_384""", type=str, help="""Name of the hybrid ViT timm model you\'d like to convert.""", ) parser.add_argument( """--pytorch_dump_folder_path""", default=None, type=str, help="""Path to the output PyTorch model directory.""" ) parser.add_argument( """--push_to_hub""", action="""store_true""", help="""Whether to upload the model to the HuggingFace hub.""" ) SCREAMING_SNAKE_CASE : List[Any] = parser.parse_args() convert_vit_checkpoint(args.vit_name, args.pytorch_dump_folder_path, args.push_to_hub)
197
import timeit import numpy as np import datasets from datasets.arrow_writer import ArrowWriter from datasets.features.features import _ArrayXD def lowerCamelCase( a__): def wrapper(*a__ ,**a__): _SCREAMING_SNAKE_CASE =timeit.default_timer() _SCREAMING_SNAKE_CASE =func(*a__ ,**a__) _SCREAMING_SNAKE_CASE =timeit.default_timer() - starttime return delta _SCREAMING_SNAKE_CASE =func.__name__ return wrapper def lowerCamelCase( a__ ,a__=100 ,a__=None): _SCREAMING_SNAKE_CASE =[] _SCREAMING_SNAKE_CASE =seq_shapes or {} for i in range(a__): _SCREAMING_SNAKE_CASE ={} for col_id, (k, v) in enumerate(features.items()): if isinstance(a__ ,_ArrayXD): _SCREAMING_SNAKE_CASE =np.random.rand(*v.shape).astype(v.dtype) elif isinstance(a__ ,datasets.Value): if v.dtype == "string": _SCREAMING_SNAKE_CASE ='''The small grey turtle was surprisingly fast when challenged.''' else: _SCREAMING_SNAKE_CASE =np.random.randint(10 ,size=1).astype(v.dtype).item() elif isinstance(a__ ,datasets.Sequence): while isinstance(a__ ,datasets.Sequence): _SCREAMING_SNAKE_CASE =v.feature _SCREAMING_SNAKE_CASE =seq_shapes[k] _SCREAMING_SNAKE_CASE =np.random.rand(*a__).astype(v.dtype) _SCREAMING_SNAKE_CASE =data dummy_data.append((i, example)) return dummy_data def lowerCamelCase( a__ ,a__ ,a__=100 ,a__=None): _SCREAMING_SNAKE_CASE =generate_examples(a__ ,num_examples=a__ ,seq_shapes=a__) with ArrowWriter(features=a__ ,path=a__) as writer: for key, record in dummy_data: _SCREAMING_SNAKE_CASE =features.encode_example(a__) writer.write(a__) _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE =writer.finalize() if not num_final_examples == num_examples: raise ValueError( f"Error writing the dataset, wrote {num_final_examples} examples but should have written {num_examples}.") _SCREAMING_SNAKE_CASE =datasets.Dataset.from_file(filename=a__ ,info=datasets.DatasetInfo(features=a__)) return dataset
691
0
import torch from diffusers import DDPMParallelScheduler from .test_schedulers import SchedulerCommonTest class lowercase_ ( UpperCamelCase__ ): '''simple docstring''' __snake_case = (DDPMParallelScheduler,) def __lowerCAmelCase ( self : Dict , **__UpperCAmelCase : List[str] ) ->Any: """simple docstring""" a = { '''num_train_timesteps''': 1_000, '''beta_start''': 0.0001, '''beta_end''': 0.02, '''beta_schedule''': '''linear''', '''variance_type''': '''fixed_small''', '''clip_sample''': True, } config.update(**_a ) return config def __lowerCAmelCase ( self : Optional[int] ) ->List[str]: """simple docstring""" for timesteps in [1, 5, 100, 1_000]: self.check_over_configs(num_train_timesteps=_a ) def __lowerCAmelCase ( self : Union[str, Any] ) ->str: """simple docstring""" for beta_start, beta_end in zip([0.0001, 0.001, 0.01, 0.1] , [0.002, 0.02, 0.2, 2] ): self.check_over_configs(beta_start=_a , beta_end=_a ) def __lowerCAmelCase ( self : Optional[int] ) ->Any: """simple docstring""" for schedule in ["linear", "squaredcos_cap_v2"]: self.check_over_configs(beta_schedule=_a ) def __lowerCAmelCase ( self : Optional[Any] ) ->Any: """simple docstring""" for variance in ["fixed_small", "fixed_large", "other"]: self.check_over_configs(variance_type=_a ) def __lowerCAmelCase ( self : str ) ->List[Any]: """simple docstring""" for clip_sample in [True, False]: self.check_over_configs(clip_sample=_a ) def __lowerCAmelCase ( self : Dict ) ->Tuple: """simple docstring""" self.check_over_configs(thresholding=_a ) for threshold in [0.5, 1.0, 2.0]: for prediction_type in ["epsilon", "sample", "v_prediction"]: self.check_over_configs( thresholding=_a , prediction_type=_a , sample_max_value=_a , ) def __lowerCAmelCase ( self : Union[str, Any] ) ->Dict: """simple docstring""" for prediction_type in ["epsilon", "sample", "v_prediction"]: self.check_over_configs(prediction_type=_a ) def __lowerCAmelCase ( self : Tuple ) ->Tuple: """simple docstring""" for t in [0, 500, 999]: self.check_over_forward(time_step=_a ) def __lowerCAmelCase ( self : List[str] ) ->int: """simple docstring""" a = self.scheduler_classes[0] a = self.get_scheduler_config() a = scheduler_class(**_a ) assert torch.sum(torch.abs(scheduler._get_variance(0 ) - 0.0 ) ) < 1e-5 assert torch.sum(torch.abs(scheduler._get_variance(487 ) - 0.00979 ) ) < 1e-5 assert torch.sum(torch.abs(scheduler._get_variance(999 ) - 0.02 ) ) < 1e-5 def __lowerCAmelCase ( self : Dict ) ->List[Any]: """simple docstring""" a = self.scheduler_classes[0] a = self.get_scheduler_config() a = scheduler_class(**_a ) a = len(_a ) a = self.dummy_model() a = self.dummy_sample_deter a = self.dummy_sample_deter + 0.1 a = self.dummy_sample_deter - 0.1 a = samplea.shape[0] a = torch.stack([samplea, samplea, samplea] , dim=0 ) a = torch.arange(_a )[0:3, None].repeat(1 , _a ) a = model(samples.flatten(0 , 1 ) , timesteps.flatten(0 , 1 ) ) a = scheduler.batch_step_no_noise(_a , timesteps.flatten(0 , 1 ) , samples.flatten(0 , 1 ) ) a = torch.sum(torch.abs(_a ) ) a = torch.mean(torch.abs(_a ) ) assert abs(result_sum.item() - 1_153.1_833 ) < 1e-2 assert abs(result_mean.item() - 0.5005 ) < 1e-3 def __lowerCAmelCase ( self : List[Any] ) ->Tuple: """simple docstring""" a = self.scheduler_classes[0] a = self.get_scheduler_config() a = scheduler_class(**_a ) a = len(_a ) a = self.dummy_model() a = self.dummy_sample_deter a = torch.manual_seed(0 ) for t in reversed(range(_a ) ): # 1. predict noise residual a = model(_a , _a ) # 2. predict previous mean of sample x_t-1 a = scheduler.step(_a , _a , _a , generator=_a ).prev_sample a = pred_prev_sample a = torch.sum(torch.abs(_a ) ) a = torch.mean(torch.abs(_a ) ) assert abs(result_sum.item() - 258.9_606 ) < 1e-2 assert abs(result_mean.item() - 0.3372 ) < 1e-3 def __lowerCAmelCase ( self : Union[str, Any] ) ->Any: """simple docstring""" a = self.scheduler_classes[0] a = self.get_scheduler_config(prediction_type='''v_prediction''' ) a = scheduler_class(**_a ) a = len(_a ) a = self.dummy_model() a = self.dummy_sample_deter a = torch.manual_seed(0 ) for t in reversed(range(_a ) ): # 1. predict noise residual a = model(_a , _a ) # 2. predict previous mean of sample x_t-1 a = scheduler.step(_a , _a , _a , generator=_a ).prev_sample a = pred_prev_sample a = torch.sum(torch.abs(_a ) ) a = torch.mean(torch.abs(_a ) ) assert abs(result_sum.item() - 202.0_296 ) < 1e-2 assert abs(result_mean.item() - 0.2631 ) < 1e-3 def __lowerCAmelCase ( self : List[Any] ) ->str: """simple docstring""" a = self.scheduler_classes[0] a = self.get_scheduler_config() a = scheduler_class(**_a ) a = [100, 87, 50, 1, 0] scheduler.set_timesteps(timesteps=_a ) a = scheduler.timesteps for i, timestep in enumerate(_a ): if i == len(_a ) - 1: a = -1 else: a = timesteps[i + 1] a = scheduler.previous_timestep(_a ) a = prev_t.item() self.assertEqual(_a , _a ) def __lowerCAmelCase ( self : int ) ->Any: """simple docstring""" a = self.scheduler_classes[0] a = self.get_scheduler_config() a = scheduler_class(**_a ) a = [100, 87, 50, 51, 0] with self.assertRaises(_a , msg='''`custom_timesteps` must be in descending order.''' ): scheduler.set_timesteps(timesteps=_a ) def __lowerCAmelCase ( self : Tuple ) ->Optional[Any]: """simple docstring""" a = self.scheduler_classes[0] a = self.get_scheduler_config() a = scheduler_class(**_a ) a = [100, 87, 50, 1, 0] a = len(_a ) with self.assertRaises(_a , msg='''Can only pass one of `num_inference_steps` or `custom_timesteps`.''' ): scheduler.set_timesteps(num_inference_steps=_a , timesteps=_a ) def __lowerCAmelCase ( self : Tuple ) ->Union[str, Any]: """simple docstring""" a = self.scheduler_classes[0] a = self.get_scheduler_config() a = scheduler_class(**_a ) a = [scheduler.config.num_train_timesteps] with self.assertRaises( _a , msg='''`timesteps` must start before `self.config.train_timesteps`: {scheduler.config.num_train_timesteps}}''' , ): scheduler.set_timesteps(timesteps=_a )
117
import logging import torch from torch import nn from torch.nn import CrossEntropyLoss, MSELoss from transformers.file_utils import add_start_docstrings, add_start_docstrings_to_model_forward from transformers.models.bert.modeling_bert import ( BERT_INPUTS_DOCSTRING, BERT_START_DOCSTRING, BertEncoder, BertModel, BertPreTrainedModel, ) snake_case_ : Optional[Any] = logging.getLogger(__name__) class A__ ( UpperCamelCase__ ): def __UpperCamelCase ( self : Optional[int] , _a : Union[str, Any] , _a : List[str] , _a : List[Any]=None , _a : Optional[Any]=None ) -> Any: """simple docstring""" _SCREAMING_SNAKE_CASE =self.layer[current_layer](_a , _a , head_mask[current_layer] ) _SCREAMING_SNAKE_CASE =layer_outputs[0] return hidden_states @add_start_docstrings( "The bare Bert Model transformer with PABEE outputting raw hidden-states without any specific head on top." , UpperCamelCase__ , ) class A__ ( UpperCamelCase__ ): def __init__( self : List[str] , _a : Union[str, Any] ) -> Tuple: """simple docstring""" super().__init__(_a ) _SCREAMING_SNAKE_CASE =BertEncoderWithPabee(_a ) self.init_weights() _SCREAMING_SNAKE_CASE =0 _SCREAMING_SNAKE_CASE =0 _SCREAMING_SNAKE_CASE =0 _SCREAMING_SNAKE_CASE =0 def __UpperCamelCase ( self : List[str] , _a : Optional[int] ) -> List[str]: """simple docstring""" _SCREAMING_SNAKE_CASE =threshold def __UpperCamelCase ( self : Dict , _a : int ) -> Tuple: """simple docstring""" _SCREAMING_SNAKE_CASE =patience def __UpperCamelCase ( self : Optional[Any] ) -> int: """simple docstring""" _SCREAMING_SNAKE_CASE =0 _SCREAMING_SNAKE_CASE =0 def __UpperCamelCase ( self : Union[str, Any] ) -> Optional[int]: """simple docstring""" _SCREAMING_SNAKE_CASE =self.inference_layers_num / self.inference_instances_num _SCREAMING_SNAKE_CASE =( f"*** Patience = {self.patience} Avg. Inference Layers = {avg_inf_layers:.2f} Speed Up =" f" {1 - avg_inf_layers / self.config.num_hidden_layers:.2f} ***" ) print(_a ) @add_start_docstrings_to_model_forward(_a ) def __UpperCamelCase ( self : List[Any] , _a : Optional[Any]=None , _a : Optional[int]=None , _a : Any=None , _a : Union[str, Any]=None , _a : Union[str, Any]=None , _a : Union[str, Any]=None , _a : str=None , _a : Any=None , _a : str=None , _a : Optional[Any]=None , _a : Dict=False , ) -> Union[str, Any]: """simple docstring""" if input_ids is not None and inputs_embeds is not None: raise ValueError('''You cannot specify both input_ids and inputs_embeds at the same time''' ) elif input_ids is not None: _SCREAMING_SNAKE_CASE =input_ids.size() elif inputs_embeds is not None: _SCREAMING_SNAKE_CASE =inputs_embeds.size()[:-1] else: raise ValueError('''You have to specify either input_ids or inputs_embeds''' ) _SCREAMING_SNAKE_CASE =input_ids.device if input_ids is not None else inputs_embeds.device if attention_mask is None: _SCREAMING_SNAKE_CASE =torch.ones(_a , device=_a ) if token_type_ids is None: _SCREAMING_SNAKE_CASE =torch.zeros(_a , dtype=torch.long , device=_a ) # We can provide a self-attention mask of dimensions [batch_size, from_seq_length, to_seq_length] # ourselves in which case we just need to make it broadcastable to all heads. _SCREAMING_SNAKE_CASE =self.get_extended_attention_mask(_a , _a , _a ) # If a 2D ou 3D attention mask is provided for the cross-attention # we need to make broadcastable to [batch_size, num_heads, seq_length, seq_length] if self.config.is_decoder and encoder_hidden_states is not None: _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE =encoder_hidden_states.size() _SCREAMING_SNAKE_CASE =(encoder_batch_size, encoder_sequence_length) if encoder_attention_mask is None: _SCREAMING_SNAKE_CASE =torch.ones(_a , device=_a ) _SCREAMING_SNAKE_CASE =self.invert_attention_mask(_a ) else: _SCREAMING_SNAKE_CASE =None # Prepare head mask if needed # 1.0 in head_mask indicate we keep the head # attention_probs has shape bsz x n_heads x N x N # input head_mask has shape [num_heads] or [num_hidden_layers x num_heads] # and head_mask is converted to shape [num_hidden_layers x batch x num_heads x seq_length x seq_length] _SCREAMING_SNAKE_CASE =self.get_head_mask(_a , self.config.num_hidden_layers ) _SCREAMING_SNAKE_CASE =self.embeddings( input_ids=_a , position_ids=_a , token_type_ids=_a , inputs_embeds=_a ) _SCREAMING_SNAKE_CASE =embedding_output if self.training: _SCREAMING_SNAKE_CASE =[] for i in range(self.config.num_hidden_layers ): _SCREAMING_SNAKE_CASE =self.encoder.adaptive_forward( _a , current_layer=_a , attention_mask=_a , head_mask=_a ) _SCREAMING_SNAKE_CASE =self.pooler(_a ) _SCREAMING_SNAKE_CASE =output_layers[i](output_dropout(_a ) ) res.append(_a ) elif self.patience == 0: # Use all layers for inference _SCREAMING_SNAKE_CASE =self.encoder( _a , attention_mask=_a , head_mask=_a , encoder_hidden_states=_a , encoder_attention_mask=_a , ) _SCREAMING_SNAKE_CASE =self.pooler(encoder_outputs[0] ) _SCREAMING_SNAKE_CASE =[output_layers[self.config.num_hidden_layers - 1](_a )] else: _SCREAMING_SNAKE_CASE =0 _SCREAMING_SNAKE_CASE =None _SCREAMING_SNAKE_CASE =0 for i in range(self.config.num_hidden_layers ): calculated_layer_num += 1 _SCREAMING_SNAKE_CASE =self.encoder.adaptive_forward( _a , current_layer=_a , attention_mask=_a , head_mask=_a ) _SCREAMING_SNAKE_CASE =self.pooler(_a ) _SCREAMING_SNAKE_CASE =output_layers[i](_a ) if regression: _SCREAMING_SNAKE_CASE =logits.detach() if patient_result is not None: _SCREAMING_SNAKE_CASE =patient_result.detach() if (patient_result is not None) and torch.abs(patient_result - labels ) < self.regression_threshold: patient_counter += 1 else: _SCREAMING_SNAKE_CASE =0 else: _SCREAMING_SNAKE_CASE =logits.detach().argmax(dim=1 ) if patient_result is not None: _SCREAMING_SNAKE_CASE =patient_result.detach().argmax(dim=1 ) if (patient_result is not None) and torch.all(labels.eq(_a ) ): patient_counter += 1 else: _SCREAMING_SNAKE_CASE =0 _SCREAMING_SNAKE_CASE =logits if patient_counter == self.patience: break _SCREAMING_SNAKE_CASE =[patient_result] self.inference_layers_num += calculated_layer_num self.inference_instances_num += 1 return res @add_start_docstrings( "Bert Model transformer with PABEE and a sequence classification/regression head on top (a linear layer on top of\n the pooled output) e.g. for GLUE tasks. " , UpperCamelCase__ , ) class A__ ( UpperCamelCase__ ): def __init__( self : Optional[int] , _a : List[Any] ) -> Union[str, Any]: """simple docstring""" super().__init__(_a ) _SCREAMING_SNAKE_CASE =config.num_labels _SCREAMING_SNAKE_CASE =BertModelWithPabee(_a ) _SCREAMING_SNAKE_CASE =nn.Dropout(config.hidden_dropout_prob ) _SCREAMING_SNAKE_CASE =nn.ModuleList( [nn.Linear(config.hidden_size , self.config.num_labels ) for _ in range(config.num_hidden_layers )] ) self.init_weights() @add_start_docstrings_to_model_forward(_a ) def __UpperCamelCase ( self : List[str] , _a : Optional[Any]=None , _a : List[Any]=None , _a : Union[str, Any]=None , _a : List[str]=None , _a : Dict=None , _a : Optional[Any]=None , _a : Optional[Any]=None , ) -> str: """simple docstring""" _SCREAMING_SNAKE_CASE =self.bert( input_ids=_a , attention_mask=_a , token_type_ids=_a , position_ids=_a , head_mask=_a , inputs_embeds=_a , output_dropout=self.dropout , output_layers=self.classifiers , regression=self.num_labels == 1 , ) _SCREAMING_SNAKE_CASE =(logits[-1],) if labels is not None: _SCREAMING_SNAKE_CASE =None _SCREAMING_SNAKE_CASE =0 for ix, logits_item in enumerate(_a ): if self.num_labels == 1: # We are doing regression _SCREAMING_SNAKE_CASE =MSELoss() _SCREAMING_SNAKE_CASE =loss_fct(logits_item.view(-1 ) , labels.view(-1 ) ) else: _SCREAMING_SNAKE_CASE =CrossEntropyLoss() _SCREAMING_SNAKE_CASE =loss_fct(logits_item.view(-1 , self.num_labels ) , labels.view(-1 ) ) if total_loss is None: _SCREAMING_SNAKE_CASE =loss else: total_loss += loss * (ix + 1) total_weights += ix + 1 _SCREAMING_SNAKE_CASE =(total_loss / total_weights,) + outputs return outputs
691
0
import warnings from ...processing_utils import ProcessorMixin from ...tokenization_utils_base import BatchEncoding class UpperCamelCase( UpperCamelCase__ ): snake_case_ : str = ["""image_processor""", """tokenizer"""] snake_case_ : Dict = """ViTImageProcessor""" snake_case_ : List[str] = ("""CLIPTokenizer""", """CLIPTokenizerFast""") def __init__( self : Dict , SCREAMING_SNAKE_CASE : Optional[Any]=None , SCREAMING_SNAKE_CASE : List[str]=None , **SCREAMING_SNAKE_CASE : List[Any] ) -> Any: '''simple docstring''' __snake_case = None if "feature_extractor" in kwargs: warnings.warn( "The `feature_extractor` argument is deprecated and will be removed in v5, use `image_processor`" " instead." , _a , ) __snake_case = kwargs.pop("feature_extractor" ) __snake_case = image_processor if image_processor is not None else feature_extractor if image_processor is None: raise ValueError("You need to specify an `image_processor`." ) if tokenizer is None: raise ValueError("You need to specify a `tokenizer`." ) super().__init__(_a , _a ) def __call__( self : Tuple , SCREAMING_SNAKE_CASE : Optional[int]=None , SCREAMING_SNAKE_CASE : int=None , SCREAMING_SNAKE_CASE : str=None , SCREAMING_SNAKE_CASE : Optional[Any]=None , **SCREAMING_SNAKE_CASE : str ) -> Optional[Any]: '''simple docstring''' if text is None and visual_prompt is None and images is None: raise ValueError("You have to specify either text, visual prompt or images." ) if text is not None and visual_prompt is not None: raise ValueError("You have to specify exactly one type of prompt. Either text or visual prompt." ) if text is not None: __snake_case = self.tokenizer(_a , return_tensors=_a , **_a ) if visual_prompt is not None: __snake_case = self.image_processor(_a , return_tensors=_a , **_a ) if images is not None: __snake_case = self.image_processor(_a , return_tensors=_a , **_a ) if visual_prompt is not None and images is not None: __snake_case = { "pixel_values": image_features.pixel_values, "conditional_pixel_values": prompt_features.pixel_values, } return encoding elif text is not None and images is not None: __snake_case = image_features.pixel_values return encoding elif text is not None: return encoding elif visual_prompt is not None: __snake_case = { "conditional_pixel_values": prompt_features.pixel_values, } return encoding else: return BatchEncoding(data=dict(**_a ) , tensor_type=_a ) def SCREAMING_SNAKE_CASE_ ( self : Dict , *SCREAMING_SNAKE_CASE : int , **SCREAMING_SNAKE_CASE : Dict ) -> str: '''simple docstring''' return self.tokenizer.batch_decode(*_a , **_a ) def SCREAMING_SNAKE_CASE_ ( self : Tuple , *SCREAMING_SNAKE_CASE : List[str] , **SCREAMING_SNAKE_CASE : Dict ) -> Optional[int]: '''simple docstring''' return self.tokenizer.decode(*_a , **_a ) @property def SCREAMING_SNAKE_CASE_ ( self : str ) -> Tuple: '''simple docstring''' warnings.warn( "`feature_extractor_class` is deprecated and will be removed in v5. Use `image_processor_class` instead." , _a , ) return self.image_processor_class @property def SCREAMING_SNAKE_CASE_ ( self : Dict ) -> Any: '''simple docstring''' warnings.warn( "`feature_extractor` is deprecated and will be removed in v5. Use `image_processor` instead." , _a , ) return self.image_processor
371
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available snake_case_ : str = { '''configuration_table_transformer''': [ '''TABLE_TRANSFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''TableTransformerConfig''', '''TableTransformerOnnxConfig''', ] } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: snake_case_ : str = [ '''TABLE_TRANSFORMER_PRETRAINED_MODEL_ARCHIVE_LIST''', '''TableTransformerForObjectDetection''', '''TableTransformerModel''', '''TableTransformerPreTrainedModel''', ] if TYPE_CHECKING: from .configuration_table_transformer import ( TABLE_TRANSFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP, TableTransformerConfig, TableTransformerOnnxConfig, ) try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_table_transformer import ( TABLE_TRANSFORMER_PRETRAINED_MODEL_ARCHIVE_LIST, TableTransformerForObjectDetection, TableTransformerModel, TableTransformerPreTrainedModel, ) else: import sys snake_case_ : Any = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
691
0
'''simple docstring''' from collections import OrderedDict from ...utils import logging from .auto_factory import _BaseAutoModelClass, _LazyAutoMapping, auto_class_update from .configuration_auto import CONFIG_MAPPING_NAMES UpperCamelCase_ = logging.get_logger(__name__) UpperCamelCase_ = OrderedDict( [ # Base model mapping ("""albert""", """FlaxAlbertModel"""), ("""bart""", """FlaxBartModel"""), ("""beit""", """FlaxBeitModel"""), ("""bert""", """FlaxBertModel"""), ("""big_bird""", """FlaxBigBirdModel"""), ("""blenderbot""", """FlaxBlenderbotModel"""), ("""blenderbot-small""", """FlaxBlenderbotSmallModel"""), ("""clip""", """FlaxCLIPModel"""), ("""distilbert""", """FlaxDistilBertModel"""), ("""electra""", """FlaxElectraModel"""), ("""gpt-sw3""", """FlaxGPT2Model"""), ("""gpt2""", """FlaxGPT2Model"""), ("""gpt_neo""", """FlaxGPTNeoModel"""), ("""gptj""", """FlaxGPTJModel"""), ("""longt5""", """FlaxLongT5Model"""), ("""marian""", """FlaxMarianModel"""), ("""mbart""", """FlaxMBartModel"""), ("""mt5""", """FlaxMT5Model"""), ("""opt""", """FlaxOPTModel"""), ("""pegasus""", """FlaxPegasusModel"""), ("""regnet""", """FlaxRegNetModel"""), ("""resnet""", """FlaxResNetModel"""), ("""roberta""", """FlaxRobertaModel"""), ("""roberta-prelayernorm""", """FlaxRobertaPreLayerNormModel"""), ("""roformer""", """FlaxRoFormerModel"""), ("""t5""", """FlaxT5Model"""), ("""vision-text-dual-encoder""", """FlaxVisionTextDualEncoderModel"""), ("""vit""", """FlaxViTModel"""), ("""wav2vec2""", """FlaxWav2Vec2Model"""), ("""whisper""", """FlaxWhisperModel"""), ("""xglm""", """FlaxXGLMModel"""), ("""xlm-roberta""", """FlaxXLMRobertaModel"""), ] ) UpperCamelCase_ = OrderedDict( [ # Model for pre-training mapping ("""albert""", """FlaxAlbertForPreTraining"""), ("""bart""", """FlaxBartForConditionalGeneration"""), ("""bert""", """FlaxBertForPreTraining"""), ("""big_bird""", """FlaxBigBirdForPreTraining"""), ("""electra""", """FlaxElectraForPreTraining"""), ("""longt5""", """FlaxLongT5ForConditionalGeneration"""), ("""mbart""", """FlaxMBartForConditionalGeneration"""), ("""mt5""", """FlaxMT5ForConditionalGeneration"""), ("""roberta""", """FlaxRobertaForMaskedLM"""), ("""roberta-prelayernorm""", """FlaxRobertaPreLayerNormForMaskedLM"""), ("""roformer""", """FlaxRoFormerForMaskedLM"""), ("""t5""", """FlaxT5ForConditionalGeneration"""), ("""wav2vec2""", """FlaxWav2Vec2ForPreTraining"""), ("""whisper""", """FlaxWhisperForConditionalGeneration"""), ("""xlm-roberta""", """FlaxXLMRobertaForMaskedLM"""), ] ) UpperCamelCase_ = OrderedDict( [ # Model for Masked LM mapping ("""albert""", """FlaxAlbertForMaskedLM"""), ("""bart""", """FlaxBartForConditionalGeneration"""), ("""bert""", """FlaxBertForMaskedLM"""), ("""big_bird""", """FlaxBigBirdForMaskedLM"""), ("""distilbert""", """FlaxDistilBertForMaskedLM"""), ("""electra""", """FlaxElectraForMaskedLM"""), ("""mbart""", """FlaxMBartForConditionalGeneration"""), ("""roberta""", """FlaxRobertaForMaskedLM"""), ("""roberta-prelayernorm""", """FlaxRobertaPreLayerNormForMaskedLM"""), ("""roformer""", """FlaxRoFormerForMaskedLM"""), ("""xlm-roberta""", """FlaxXLMRobertaForMaskedLM"""), ] ) UpperCamelCase_ = OrderedDict( [ # Model for Seq2Seq Causal LM mapping ("""bart""", """FlaxBartForConditionalGeneration"""), ("""blenderbot""", """FlaxBlenderbotForConditionalGeneration"""), ("""blenderbot-small""", """FlaxBlenderbotSmallForConditionalGeneration"""), ("""encoder-decoder""", """FlaxEncoderDecoderModel"""), ("""longt5""", """FlaxLongT5ForConditionalGeneration"""), ("""marian""", """FlaxMarianMTModel"""), ("""mbart""", """FlaxMBartForConditionalGeneration"""), ("""mt5""", """FlaxMT5ForConditionalGeneration"""), ("""pegasus""", """FlaxPegasusForConditionalGeneration"""), ("""t5""", """FlaxT5ForConditionalGeneration"""), ] ) UpperCamelCase_ = OrderedDict( [ # Model for Image-classsification ("""beit""", """FlaxBeitForImageClassification"""), ("""regnet""", """FlaxRegNetForImageClassification"""), ("""resnet""", """FlaxResNetForImageClassification"""), ("""vit""", """FlaxViTForImageClassification"""), ] ) UpperCamelCase_ = OrderedDict( [ ("""vision-encoder-decoder""", """FlaxVisionEncoderDecoderModel"""), ] ) UpperCamelCase_ = OrderedDict( [ # Model for Causal LM mapping ("""bart""", """FlaxBartForCausalLM"""), ("""bert""", """FlaxBertForCausalLM"""), ("""big_bird""", """FlaxBigBirdForCausalLM"""), ("""electra""", """FlaxElectraForCausalLM"""), ("""gpt-sw3""", """FlaxGPT2LMHeadModel"""), ("""gpt2""", """FlaxGPT2LMHeadModel"""), ("""gpt_neo""", """FlaxGPTNeoForCausalLM"""), ("""gptj""", """FlaxGPTJForCausalLM"""), ("""opt""", """FlaxOPTForCausalLM"""), ("""roberta""", """FlaxRobertaForCausalLM"""), ("""roberta-prelayernorm""", """FlaxRobertaPreLayerNormForCausalLM"""), ("""xglm""", """FlaxXGLMForCausalLM"""), ("""xlm-roberta""", """FlaxXLMRobertaForCausalLM"""), ] ) UpperCamelCase_ = OrderedDict( [ # Model for Sequence Classification mapping ("""albert""", """FlaxAlbertForSequenceClassification"""), ("""bart""", """FlaxBartForSequenceClassification"""), ("""bert""", """FlaxBertForSequenceClassification"""), ("""big_bird""", """FlaxBigBirdForSequenceClassification"""), ("""distilbert""", """FlaxDistilBertForSequenceClassification"""), ("""electra""", """FlaxElectraForSequenceClassification"""), ("""mbart""", """FlaxMBartForSequenceClassification"""), ("""roberta""", """FlaxRobertaForSequenceClassification"""), ("""roberta-prelayernorm""", """FlaxRobertaPreLayerNormForSequenceClassification"""), ("""roformer""", """FlaxRoFormerForSequenceClassification"""), ("""xlm-roberta""", """FlaxXLMRobertaForSequenceClassification"""), ] ) UpperCamelCase_ = OrderedDict( [ # Model for Question Answering mapping ("""albert""", """FlaxAlbertForQuestionAnswering"""), ("""bart""", """FlaxBartForQuestionAnswering"""), ("""bert""", """FlaxBertForQuestionAnswering"""), ("""big_bird""", """FlaxBigBirdForQuestionAnswering"""), ("""distilbert""", """FlaxDistilBertForQuestionAnswering"""), ("""electra""", """FlaxElectraForQuestionAnswering"""), ("""mbart""", """FlaxMBartForQuestionAnswering"""), ("""roberta""", """FlaxRobertaForQuestionAnswering"""), ("""roberta-prelayernorm""", """FlaxRobertaPreLayerNormForQuestionAnswering"""), ("""roformer""", """FlaxRoFormerForQuestionAnswering"""), ("""xlm-roberta""", """FlaxXLMRobertaForQuestionAnswering"""), ] ) UpperCamelCase_ = OrderedDict( [ # Model for Token Classification mapping ("""albert""", """FlaxAlbertForTokenClassification"""), ("""bert""", """FlaxBertForTokenClassification"""), ("""big_bird""", """FlaxBigBirdForTokenClassification"""), ("""distilbert""", """FlaxDistilBertForTokenClassification"""), ("""electra""", """FlaxElectraForTokenClassification"""), ("""roberta""", """FlaxRobertaForTokenClassification"""), ("""roberta-prelayernorm""", """FlaxRobertaPreLayerNormForTokenClassification"""), ("""roformer""", """FlaxRoFormerForTokenClassification"""), ("""xlm-roberta""", """FlaxXLMRobertaForTokenClassification"""), ] ) UpperCamelCase_ = OrderedDict( [ # Model for Multiple Choice mapping ("""albert""", """FlaxAlbertForMultipleChoice"""), ("""bert""", """FlaxBertForMultipleChoice"""), ("""big_bird""", """FlaxBigBirdForMultipleChoice"""), ("""distilbert""", """FlaxDistilBertForMultipleChoice"""), ("""electra""", """FlaxElectraForMultipleChoice"""), ("""roberta""", """FlaxRobertaForMultipleChoice"""), ("""roberta-prelayernorm""", """FlaxRobertaPreLayerNormForMultipleChoice"""), ("""roformer""", """FlaxRoFormerForMultipleChoice"""), ("""xlm-roberta""", """FlaxXLMRobertaForMultipleChoice"""), ] ) UpperCamelCase_ = OrderedDict( [ ("""bert""", """FlaxBertForNextSentencePrediction"""), ] ) UpperCamelCase_ = OrderedDict( [ ("""speech-encoder-decoder""", """FlaxSpeechEncoderDecoderModel"""), ("""whisper""", """FlaxWhisperForConditionalGeneration"""), ] ) UpperCamelCase_ = OrderedDict( [ ("""whisper""", """FlaxWhisperForAudioClassification"""), ] ) UpperCamelCase_ = _LazyAutoMapping(CONFIG_MAPPING_NAMES, FLAX_MODEL_MAPPING_NAMES) UpperCamelCase_ = _LazyAutoMapping(CONFIG_MAPPING_NAMES, FLAX_MODEL_FOR_PRETRAINING_MAPPING_NAMES) UpperCamelCase_ = _LazyAutoMapping(CONFIG_MAPPING_NAMES, FLAX_MODEL_FOR_MASKED_LM_MAPPING_NAMES) UpperCamelCase_ = _LazyAutoMapping( CONFIG_MAPPING_NAMES, FLAX_MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING_NAMES ) UpperCamelCase_ = _LazyAutoMapping( CONFIG_MAPPING_NAMES, FLAX_MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING_NAMES ) UpperCamelCase_ = _LazyAutoMapping(CONFIG_MAPPING_NAMES, FLAX_MODEL_FOR_VISION_2_SEQ_MAPPING_NAMES) UpperCamelCase_ = _LazyAutoMapping(CONFIG_MAPPING_NAMES, FLAX_MODEL_FOR_CAUSAL_LM_MAPPING_NAMES) UpperCamelCase_ = _LazyAutoMapping( CONFIG_MAPPING_NAMES, FLAX_MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING_NAMES ) UpperCamelCase_ = _LazyAutoMapping( CONFIG_MAPPING_NAMES, FLAX_MODEL_FOR_QUESTION_ANSWERING_MAPPING_NAMES ) UpperCamelCase_ = _LazyAutoMapping( CONFIG_MAPPING_NAMES, FLAX_MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING_NAMES ) UpperCamelCase_ = _LazyAutoMapping( CONFIG_MAPPING_NAMES, FLAX_MODEL_FOR_MULTIPLE_CHOICE_MAPPING_NAMES ) UpperCamelCase_ = _LazyAutoMapping( CONFIG_MAPPING_NAMES, FLAX_MODEL_FOR_NEXT_SENTENCE_PREDICTION_MAPPING_NAMES ) UpperCamelCase_ = _LazyAutoMapping( CONFIG_MAPPING_NAMES, FLAX_MODEL_FOR_SPEECH_SEQ_2_SEQ_MAPPING_NAMES ) UpperCamelCase_ = _LazyAutoMapping( CONFIG_MAPPING_NAMES, FLAX_MODEL_FOR_AUDIO_CLASSIFICATION_MAPPING_NAMES ) class __SCREAMING_SNAKE_CASE ( _BaseAutoModelClass ): lowerCamelCase_ = FLAX_MODEL_MAPPING UpperCamelCase_ = auto_class_update(FlaxAutoModel) class __SCREAMING_SNAKE_CASE ( _BaseAutoModelClass ): lowerCamelCase_ = FLAX_MODEL_FOR_PRETRAINING_MAPPING UpperCamelCase_ = auto_class_update(FlaxAutoModelForPreTraining, head_doc="""pretraining""") class __SCREAMING_SNAKE_CASE ( _BaseAutoModelClass ): lowerCamelCase_ = FLAX_MODEL_FOR_CAUSAL_LM_MAPPING UpperCamelCase_ = auto_class_update(FlaxAutoModelForCausalLM, head_doc="""causal language modeling""") class __SCREAMING_SNAKE_CASE ( _BaseAutoModelClass ): lowerCamelCase_ = FLAX_MODEL_FOR_MASKED_LM_MAPPING UpperCamelCase_ = auto_class_update(FlaxAutoModelForMaskedLM, head_doc="""masked language modeling""") class __SCREAMING_SNAKE_CASE ( _BaseAutoModelClass ): lowerCamelCase_ = FLAX_MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING UpperCamelCase_ = auto_class_update( FlaxAutoModelForSeqaSeqLM, head_doc="""sequence-to-sequence language modeling""", checkpoint_for_example="""t5-base""" ) class __SCREAMING_SNAKE_CASE ( _BaseAutoModelClass ): lowerCamelCase_ = FLAX_MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING UpperCamelCase_ = auto_class_update( FlaxAutoModelForSequenceClassification, head_doc="""sequence classification""" ) class __SCREAMING_SNAKE_CASE ( _BaseAutoModelClass ): lowerCamelCase_ = FLAX_MODEL_FOR_QUESTION_ANSWERING_MAPPING UpperCamelCase_ = auto_class_update(FlaxAutoModelForQuestionAnswering, head_doc="""question answering""") class __SCREAMING_SNAKE_CASE ( _BaseAutoModelClass ): lowerCamelCase_ = FLAX_MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING UpperCamelCase_ = auto_class_update( FlaxAutoModelForTokenClassification, head_doc="""token classification""" ) class __SCREAMING_SNAKE_CASE ( _BaseAutoModelClass ): lowerCamelCase_ = FLAX_MODEL_FOR_MULTIPLE_CHOICE_MAPPING UpperCamelCase_ = auto_class_update(FlaxAutoModelForMultipleChoice, head_doc="""multiple choice""") class __SCREAMING_SNAKE_CASE ( _BaseAutoModelClass ): lowerCamelCase_ = FLAX_MODEL_FOR_NEXT_SENTENCE_PREDICTION_MAPPING UpperCamelCase_ = auto_class_update( FlaxAutoModelForNextSentencePrediction, head_doc="""next sentence prediction""" ) class __SCREAMING_SNAKE_CASE ( _BaseAutoModelClass ): lowerCamelCase_ = FLAX_MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING UpperCamelCase_ = auto_class_update( FlaxAutoModelForImageClassification, head_doc="""image classification""" ) class __SCREAMING_SNAKE_CASE ( _BaseAutoModelClass ): lowerCamelCase_ = FLAX_MODEL_FOR_VISION_2_SEQ_MAPPING UpperCamelCase_ = auto_class_update(FlaxAutoModelForVisionaSeq, head_doc="""vision-to-text modeling""") class __SCREAMING_SNAKE_CASE ( _BaseAutoModelClass ): lowerCamelCase_ = FLAX_MODEL_FOR_SPEECH_SEQ_2_SEQ_MAPPING UpperCamelCase_ = auto_class_update( FlaxAutoModelForSpeechSeqaSeq, head_doc="""sequence-to-sequence speech-to-text modeling""" )
92
import json import os import shutil import tempfile import unittest import numpy as np import pytest from transformers import MgpstrTokenizer from transformers.models.mgp_str.tokenization_mgp_str import VOCAB_FILES_NAMES from transformers.testing_utils import require_torch, require_vision from transformers.utils import IMAGE_PROCESSOR_NAME, is_torch_available, is_vision_available if is_torch_available(): import torch if is_vision_available(): from PIL import Image from transformers import MgpstrProcessor, ViTImageProcessor @require_torch @require_vision class A__ ( unittest.TestCase ): UpperCAmelCase = ViTImageProcessor if is_vision_available() else None @property def __UpperCamelCase ( self : str ) -> Union[str, Any]: """simple docstring""" return self.image_processor_tester.prepare_image_processor_dict() def __UpperCamelCase ( self : str ) -> List[str]: """simple docstring""" _SCREAMING_SNAKE_CASE =(3, 32, 128) _SCREAMING_SNAKE_CASE =tempfile.mkdtemp() # fmt: off _SCREAMING_SNAKE_CASE =['''[GO]''', '''[s]''', '''0''', '''1''', '''2''', '''3''', '''4''', '''5''', '''6''', '''7''', '''8''', '''9''', '''a''', '''b''', '''c''', '''d''', '''e''', '''f''', '''g''', '''h''', '''i''', '''j''', '''k''', '''l''', '''m''', '''n''', '''o''', '''p''', '''q''', '''r''', '''s''', '''t''', '''u''', '''v''', '''w''', '''x''', '''y''', '''z'''] # fmt: on _SCREAMING_SNAKE_CASE =dict(zip(_a , range(len(_a ) ) ) ) _SCREAMING_SNAKE_CASE =os.path.join(self.tmpdirname , VOCAB_FILES_NAMES['''vocab_file'''] ) with open(self.vocab_file , '''w''' , encoding='''utf-8''' ) as fp: fp.write(json.dumps(_a ) + '''\n''' ) _SCREAMING_SNAKE_CASE ={ '''do_normalize''': False, '''do_resize''': True, '''image_processor_type''': '''ViTImageProcessor''', '''resample''': 3, '''size''': {'''height''': 32, '''width''': 128}, } _SCREAMING_SNAKE_CASE =os.path.join(self.tmpdirname , _a ) with open(self.image_processor_file , '''w''' , encoding='''utf-8''' ) as fp: json.dump(_a , _a ) def __UpperCamelCase ( self : Optional[Any] , **_a : str ) -> int: """simple docstring""" return MgpstrTokenizer.from_pretrained(self.tmpdirname , **_a ) def __UpperCamelCase ( self : Optional[int] , **_a : Tuple ) -> List[Any]: """simple docstring""" return ViTImageProcessor.from_pretrained(self.tmpdirname , **_a ) def __UpperCamelCase ( self : Tuple ) -> str: """simple docstring""" shutil.rmtree(self.tmpdirname ) def __UpperCamelCase ( self : List[Any] ) -> Any: """simple docstring""" _SCREAMING_SNAKE_CASE =np.random.randint(255 , size=(3, 30, 400) , dtype=np.uinta ) _SCREAMING_SNAKE_CASE =Image.fromarray(np.moveaxis(_a , 0 , -1 ) ) return image_input def __UpperCamelCase ( self : Union[str, Any] ) -> Dict: """simple docstring""" _SCREAMING_SNAKE_CASE =self.get_tokenizer() _SCREAMING_SNAKE_CASE =self.get_image_processor() _SCREAMING_SNAKE_CASE =MgpstrProcessor(tokenizer=_a , image_processor=_a ) processor.save_pretrained(self.tmpdirname ) _SCREAMING_SNAKE_CASE =MgpstrProcessor.from_pretrained(self.tmpdirname , use_fast=_a ) self.assertEqual(processor.char_tokenizer.get_vocab() , tokenizer.get_vocab() ) self.assertIsInstance(processor.char_tokenizer , _a ) self.assertEqual(processor.image_processor.to_json_string() , image_processor.to_json_string() ) self.assertIsInstance(processor.image_processor , _a ) def __UpperCamelCase ( self : Union[str, Any] ) -> List[Any]: """simple docstring""" _SCREAMING_SNAKE_CASE =self.get_tokenizer() _SCREAMING_SNAKE_CASE =self.get_image_processor() _SCREAMING_SNAKE_CASE =MgpstrProcessor(tokenizer=_a , image_processor=_a ) processor.save_pretrained(self.tmpdirname ) _SCREAMING_SNAKE_CASE =self.get_tokenizer(bos_token='''(BOS)''' , eos_token='''(EOS)''' ) _SCREAMING_SNAKE_CASE =self.get_image_processor(do_normalize=_a , padding_value=1.0 ) _SCREAMING_SNAKE_CASE =MgpstrProcessor.from_pretrained( self.tmpdirname , bos_token='''(BOS)''' , eos_token='''(EOS)''' , do_normalize=_a , padding_value=1.0 ) self.assertEqual(processor.char_tokenizer.get_vocab() , tokenizer_add_kwargs.get_vocab() ) self.assertIsInstance(processor.char_tokenizer , _a ) self.assertEqual(processor.image_processor.to_json_string() , image_processor_add_kwargs.to_json_string() ) self.assertIsInstance(processor.image_processor , _a ) def __UpperCamelCase ( self : Union[str, Any] ) -> str: """simple docstring""" _SCREAMING_SNAKE_CASE =self.get_image_processor() _SCREAMING_SNAKE_CASE =self.get_tokenizer() _SCREAMING_SNAKE_CASE =MgpstrProcessor(tokenizer=_a , image_processor=_a ) _SCREAMING_SNAKE_CASE =self.prepare_image_inputs() _SCREAMING_SNAKE_CASE =image_processor(_a , return_tensors='''np''' ) _SCREAMING_SNAKE_CASE =processor(images=_a , return_tensors='''np''' ) for key in input_image_proc.keys(): self.assertAlmostEqual(input_image_proc[key].sum() , input_processor[key].sum() , delta=1E-2 ) def __UpperCamelCase ( self : Optional[int] ) -> List[str]: """simple docstring""" _SCREAMING_SNAKE_CASE =self.get_image_processor() _SCREAMING_SNAKE_CASE =self.get_tokenizer() _SCREAMING_SNAKE_CASE =MgpstrProcessor(tokenizer=_a , image_processor=_a ) _SCREAMING_SNAKE_CASE ='''test''' _SCREAMING_SNAKE_CASE =processor(text=_a ) _SCREAMING_SNAKE_CASE =tokenizer(_a ) for key in encoded_tok.keys(): self.assertListEqual(encoded_tok[key] , encoded_processor[key] ) def __UpperCamelCase ( self : List[str] ) -> List[Any]: """simple docstring""" _SCREAMING_SNAKE_CASE =self.get_image_processor() _SCREAMING_SNAKE_CASE =self.get_tokenizer() _SCREAMING_SNAKE_CASE =MgpstrProcessor(tokenizer=_a , image_processor=_a ) _SCREAMING_SNAKE_CASE ='''test''' _SCREAMING_SNAKE_CASE =self.prepare_image_inputs() _SCREAMING_SNAKE_CASE =processor(text=_a , images=_a ) self.assertListEqual(list(inputs.keys() ) , ['''pixel_values''', '''labels'''] ) # test if it raises when no input is passed with pytest.raises(_a ): processor() def __UpperCamelCase ( self : List[Any] ) -> Any: """simple docstring""" _SCREAMING_SNAKE_CASE =self.get_image_processor() _SCREAMING_SNAKE_CASE =self.get_tokenizer() _SCREAMING_SNAKE_CASE =MgpstrProcessor(tokenizer=_a , image_processor=_a ) _SCREAMING_SNAKE_CASE =[[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9], [3, 4, 3, 1, 1, 8, 9]] _SCREAMING_SNAKE_CASE =processor.char_decode(_a ) _SCREAMING_SNAKE_CASE =tokenizer.batch_decode(_a ) _SCREAMING_SNAKE_CASE =[seq.replace(''' ''' , '''''' ) for seq in decoded_tok] self.assertListEqual(_a , _a ) def __UpperCamelCase ( self : Any ) -> List[Any]: """simple docstring""" _SCREAMING_SNAKE_CASE =self.get_image_processor() _SCREAMING_SNAKE_CASE =self.get_tokenizer() _SCREAMING_SNAKE_CASE =MgpstrProcessor(tokenizer=_a , image_processor=_a ) _SCREAMING_SNAKE_CASE =None _SCREAMING_SNAKE_CASE =self.prepare_image_inputs() _SCREAMING_SNAKE_CASE =processor(text=_a , images=_a ) self.assertListEqual(list(inputs.keys() ) , processor.model_input_names ) def __UpperCamelCase ( self : List[Any] ) -> Optional[int]: """simple docstring""" _SCREAMING_SNAKE_CASE =self.get_image_processor() _SCREAMING_SNAKE_CASE =self.get_tokenizer() _SCREAMING_SNAKE_CASE =MgpstrProcessor(tokenizer=_a , image_processor=_a ) _SCREAMING_SNAKE_CASE =torch.randn(1 , 27 , 38 ) _SCREAMING_SNAKE_CASE =torch.randn(1 , 27 , 5_0257 ) _SCREAMING_SNAKE_CASE =torch.randn(1 , 27 , 3_0522 ) _SCREAMING_SNAKE_CASE =processor.batch_decode([char_input, bpe_input, wp_input] ) self.assertListEqual(list(results.keys() ) , ['''generated_text''', '''scores''', '''char_preds''', '''bpe_preds''', '''wp_preds'''] )
691
0
'''simple docstring''' import json from typing import TYPE_CHECKING, List, Optional, Tuple from tokenizers import pre_tokenizers, processors from ...tokenization_utils_base import AddedToken, BatchEncoding from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import logging from .tokenization_blenderbot import BlenderbotTokenizer if TYPE_CHECKING: from transformers.pipelines.conversational import Conversation lowerCAmelCase_ : str = logging.get_logger(__name__) lowerCAmelCase_ : List[Any] = { '''vocab_file''': '''vocab.json''', '''merges_file''': '''merges.txt''', '''tokenizer_config_file''': '''tokenizer_config.json''', } lowerCAmelCase_ : Any = { '''vocab_file''': {'''facebook/blenderbot-3B''': '''https://huggingface.co/facebook/blenderbot-3B/resolve/main/vocab.json'''}, '''merges_file''': {'''facebook/blenderbot-3B''': '''https://huggingface.co/facebook/blenderbot-3B/resolve/main/merges.txt'''}, '''tokenizer_config_file''': { '''facebook/blenderbot-3B''': '''https://huggingface.co/facebook/blenderbot-3B/resolve/main/tokenizer_config.json''' }, } lowerCAmelCase_ : List[str] = {'''facebook/blenderbot-3B''': 128} class lowerCamelCase_ ( UpperCamelCase__ ): _lowerCAmelCase : Tuple = VOCAB_FILES_NAMES _lowerCAmelCase : List[Any] = PRETRAINED_VOCAB_FILES_MAP _lowerCAmelCase : str = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES _lowerCAmelCase : int = ['input_ids', 'attention_mask'] _lowerCAmelCase : str = BlenderbotTokenizer def __init__( self : Dict , lowerCAmelCase__ : str=None , lowerCAmelCase__ : Optional[int]=None , lowerCAmelCase__ : List[str]=None , lowerCAmelCase__ : int="replace" , lowerCAmelCase__ : Dict="<s>" , lowerCAmelCase__ : Optional[Any]="</s>" , lowerCAmelCase__ : Any="</s>" , lowerCAmelCase__ : int="<s>" , lowerCAmelCase__ : int="<unk>" , lowerCAmelCase__ : Optional[int]="<pad>" , lowerCAmelCase__ : Tuple="<mask>" , lowerCAmelCase__ : Tuple=False , lowerCAmelCase__ : Union[str, Any]=True , **lowerCAmelCase__ : List[str] , ): """simple docstring""" super().__init__( _a , _a , tokenizer_file=_a , errors=_a , bos_token=_a , eos_token=_a , sep_token=_a , cls_token=_a , unk_token=_a , pad_token=_a , mask_token=_a , add_prefix_space=_a , trim_offsets=_a , **_a , ) SCREAMING_SNAKE_CASE : Any = json.loads(self.backend_tokenizer.pre_tokenizer.__getstate__() ) if pre_tok_state.get('''add_prefix_space''' , _a ) != add_prefix_space: SCREAMING_SNAKE_CASE : Optional[Any] = getattr(_a , pre_tok_state.pop('''type''' ) ) SCREAMING_SNAKE_CASE : Union[str, Any] = add_prefix_space SCREAMING_SNAKE_CASE : int = pre_tok_class(**_a ) SCREAMING_SNAKE_CASE : Union[str, Any] = add_prefix_space SCREAMING_SNAKE_CASE : Optional[int] = '''post_processor''' SCREAMING_SNAKE_CASE : Dict = getattr(self.backend_tokenizer , _a , _a ) if tokenizer_component_instance: SCREAMING_SNAKE_CASE : Optional[Any] = json.loads(tokenizer_component_instance.__getstate__() ) # The lists 'sep' and 'cls' must be cased in tuples for the object `post_processor_class` if "sep" in state: SCREAMING_SNAKE_CASE : Any = tuple(state['''sep'''] ) if "cls" in state: SCREAMING_SNAKE_CASE : Any = tuple(state['''cls'''] ) SCREAMING_SNAKE_CASE : List[Any] = False if state.get('''add_prefix_space''' , _a ) != add_prefix_space: SCREAMING_SNAKE_CASE : Dict = add_prefix_space SCREAMING_SNAKE_CASE : Union[str, Any] = True if state.get('''trim_offsets''' , _a ) != trim_offsets: SCREAMING_SNAKE_CASE : Any = trim_offsets SCREAMING_SNAKE_CASE : int = True if changes_to_apply: SCREAMING_SNAKE_CASE : List[Any] = getattr(_a , state.pop('''type''' ) ) SCREAMING_SNAKE_CASE : Any = component_class(**_a ) setattr(self.backend_tokenizer , _a , _a ) @property # Copied from transformers.models.roberta.tokenization_roberta_fast.RobertaTokenizerFast.mask_token with Roberta->Blenderbot, RoBERTa->Blenderbot def __lowercase ( self : Tuple ): """simple docstring""" if self._mask_token is None: if self.verbose: logger.error('''Using mask_token, but it is not set yet.''' ) return None return str(self._mask_token ) @mask_token.setter def __lowercase ( self : Optional[Any] , lowerCAmelCase__ : str ): """simple docstring""" SCREAMING_SNAKE_CASE : Tuple = AddedToken(_a , lstrip=_a , rstrip=_a ) if isinstance(_a , _a ) else value SCREAMING_SNAKE_CASE : List[str] = value def __lowercase ( self : Optional[Any] , *lowerCAmelCase__ : str , **lowerCAmelCase__ : int ): """simple docstring""" SCREAMING_SNAKE_CASE : Optional[Any] = kwargs.get('''is_split_into_words''' , _a ) assert self.add_prefix_space or not is_split_into_words, ( F"""You need to instantiate {self.__class__.__name__} with add_prefix_space=True """ "to use it with pretokenized inputs." ) return super()._batch_encode_plus(*_a , **_a ) def __lowercase ( self : List[Any] , *lowerCAmelCase__ : Optional[int] , **lowerCAmelCase__ : Union[str, Any] ): """simple docstring""" SCREAMING_SNAKE_CASE : List[Any] = kwargs.get('''is_split_into_words''' , _a ) assert self.add_prefix_space or not is_split_into_words, ( F"""You need to instantiate {self.__class__.__name__} with add_prefix_space=True """ "to use it with pretokenized inputs." ) return super()._encode_plus(*_a , **_a ) def __lowercase ( self : Dict , lowerCAmelCase__ : str , lowerCAmelCase__ : Optional[str] = None ): """simple docstring""" SCREAMING_SNAKE_CASE : List[Any] = self._tokenizer.model.save(_a , name=_a ) return tuple(_a ) def __lowercase ( self : Tuple , lowerCAmelCase__ : List[int] , lowerCAmelCase__ : Optional[List[int]] = None ): """simple docstring""" SCREAMING_SNAKE_CASE : List[Any] = [self.sep_token_id] SCREAMING_SNAKE_CASE : Dict = [self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep ) * [0] return len(cls + token_ids_a + sep + sep + token_ids_a + sep ) * [0] def __lowercase ( self : Tuple , lowerCAmelCase__ : List[int] , lowerCAmelCase__ : Optional[List[int]] = None ): """simple docstring""" return token_ids_a + [self.eos_token_id] def __lowercase ( self : Any , lowerCAmelCase__ : "Conversation" ): """simple docstring""" SCREAMING_SNAKE_CASE : Any = [] for is_user, text in conversation.iter_texts(): if is_user: # We need to space prefix as it's being done within blenderbot inputs.append(''' ''' + text ) else: # Generated responses should contain them already. inputs.append(_a ) SCREAMING_SNAKE_CASE : Union[str, Any] = ''' '''.join(_a ) SCREAMING_SNAKE_CASE : int = self.encode(_a ) if len(_a ) > self.model_max_length: SCREAMING_SNAKE_CASE : List[str] = input_ids[-self.model_max_length :] logger.warning(F"""Trimmed input from conversation as it was longer than {self.model_max_length} tokens.""" ) return input_ids
527
import requests from bsa import BeautifulSoup def lowerCamelCase( a__ = "https://www.worldometers.info/coronavirus"): _SCREAMING_SNAKE_CASE =BeautifulSoup(requests.get(a__).text ,'''html.parser''') _SCREAMING_SNAKE_CASE =soup.findAll('''h1''') _SCREAMING_SNAKE_CASE =soup.findAll('''div''' ,{'''class''': '''maincounter-number'''}) keys += soup.findAll('''span''' ,{'''class''': '''panel-title'''}) values += soup.findAll('''div''' ,{'''class''': '''number-table-main'''}) return {key.text.strip(): value.text.strip() for key, value in zip(a__ ,a__)} if __name__ == "__main__": print('''\033[1m''' + '''COVID-19 Status of the World''' + '''\033[0m\n''') for key, value in world_covidaa_stats().items(): print(f"""{key}\n{value}\n""")
691
0
import sys import webbrowser import requests from bsa import BeautifulSoup from fake_useragent import UserAgent if __name__ == "__main__": print('''Googling.....''') _UpperCamelCase = '''https://www.google.com/search?q=''' + ''' '''.join(sys.argv[1:]) _UpperCamelCase = 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(1_0000): out_file.write(data) _UpperCamelCase = BeautifulSoup(res.text, '''html.parser''') _UpperCamelCase = 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')}''')
243
def lowerCamelCase( a__ ,a__): return number | (1 << position) def lowerCamelCase( a__ ,a__): return number & ~(1 << position) def lowerCamelCase( a__ ,a__): return number ^ (1 << position) def lowerCamelCase( a__ ,a__): return ((number >> position) & 1) == 1 def lowerCamelCase( a__ ,a__): return int((number & (1 << position)) != 0) if __name__ == "__main__": import doctest doctest.testmod()
691
0
import functools import operator from ...configuration_utils import PretrainedConfig from ...utils import logging lowerCamelCase__ = logging.get_logger(__name__) lowerCamelCase__ = { '''facebook/wav2vec2-base-960h''': '''https://huggingface.co/facebook/wav2vec2-base-960h/resolve/main/config.json''', # See all Wav2Vec2 models at https://huggingface.co/models?filter=wav2vec2 } class UpperCamelCase ( UpperCamelCase__ ): __UpperCamelCase = """wav2vec2""" def __init__( self : Union[str, Any] ,_lowerCAmelCase : Tuple=32 ,_lowerCAmelCase : int=768 ,_lowerCAmelCase : Optional[int]=12 ,_lowerCAmelCase : Union[str, Any]=12 ,_lowerCAmelCase : Union[str, Any]=3_072 ,_lowerCAmelCase : Dict="gelu" ,_lowerCAmelCase : Dict=0.1 ,_lowerCAmelCase : Tuple=0.1 ,_lowerCAmelCase : Tuple=0.1 ,_lowerCAmelCase : Dict=0.0 ,_lowerCAmelCase : int=0.0 ,_lowerCAmelCase : Dict=0.1 ,_lowerCAmelCase : List[str]=0.1 ,_lowerCAmelCase : str=0.0_2 ,_lowerCAmelCase : Optional[int]=1E-5 ,_lowerCAmelCase : Union[str, Any]="group" ,_lowerCAmelCase : str="gelu" ,_lowerCAmelCase : Union[str, Any]=(512, 512, 512, 512, 512, 512, 512) ,_lowerCAmelCase : Dict=(5, 2, 2, 2, 2, 2, 2) ,_lowerCAmelCase : int=(10, 3, 3, 3, 3, 2, 2) ,_lowerCAmelCase : Any=False ,_lowerCAmelCase : Optional[int]=128 ,_lowerCAmelCase : str=16 ,_lowerCAmelCase : List[str]=False ,_lowerCAmelCase : Tuple=True ,_lowerCAmelCase : Dict=0.0_5 ,_lowerCAmelCase : Any=10 ,_lowerCAmelCase : str=2 ,_lowerCAmelCase : List[Any]=0.0 ,_lowerCAmelCase : Dict=10 ,_lowerCAmelCase : List[str]=0 ,_lowerCAmelCase : List[str]=320 ,_lowerCAmelCase : Optional[int]=2 ,_lowerCAmelCase : Optional[Any]=0.1 ,_lowerCAmelCase : str=100 ,_lowerCAmelCase : Any=256 ,_lowerCAmelCase : Optional[Any]=256 ,_lowerCAmelCase : Optional[Any]=0.1 ,_lowerCAmelCase : Optional[Any]="sum" ,_lowerCAmelCase : Tuple=False ,_lowerCAmelCase : Tuple=False ,_lowerCAmelCase : Optional[Any]=256 ,_lowerCAmelCase : Tuple=(512, 512, 512, 512, 1_500) ,_lowerCAmelCase : List[str]=(5, 3, 3, 1, 1) ,_lowerCAmelCase : int=(1, 2, 3, 1, 1) ,_lowerCAmelCase : Optional[int]=512 ,_lowerCAmelCase : str=0 ,_lowerCAmelCase : Tuple=1 ,_lowerCAmelCase : Any=2 ,_lowerCAmelCase : Union[str, Any]=False ,_lowerCAmelCase : Optional[Any]=3 ,_lowerCAmelCase : Union[str, Any]=2 ,_lowerCAmelCase : Tuple=3 ,_lowerCAmelCase : List[str]=None ,_lowerCAmelCase : Dict=None ,**_lowerCAmelCase : List[Any] ,): """simple docstring""" super().__init__(**_a ,pad_token_id=_a ,bos_token_id=_a ,eos_token_id=_a ) __snake_case = hidden_size __snake_case = feat_extract_norm __snake_case = feat_extract_activation __snake_case = list(_a ) __snake_case = list(_a ) __snake_case = list(_a ) __snake_case = conv_bias __snake_case = num_conv_pos_embeddings __snake_case = num_conv_pos_embedding_groups __snake_case = len(self.conv_dim ) __snake_case = num_hidden_layers __snake_case = intermediate_size __snake_case = hidden_act __snake_case = num_attention_heads __snake_case = hidden_dropout __snake_case = attention_dropout __snake_case = activation_dropout __snake_case = feat_proj_dropout __snake_case = final_dropout __snake_case = layerdrop __snake_case = layer_norm_eps __snake_case = initializer_range __snake_case = vocab_size __snake_case = do_stable_layer_norm __snake_case = use_weighted_layer_sum if ( (len(self.conv_stride ) != self.num_feat_extract_layers) or (len(self.conv_kernel ) != self.num_feat_extract_layers) or (len(self.conv_dim ) != self.num_feat_extract_layers) ): raise ValueError( "Configuration for convolutional layers is incorrect. It is required that `len(config.conv_dim)` ==" " `len(config.conv_stride)` == `len(config.conv_kernel)`, but is `len(config.conv_dim) =" F""" {len(self.conv_dim )}`, `len(config.conv_stride) = {len(self.conv_stride )}`,""" F""" `len(config.conv_kernel) = {len(self.conv_kernel )}`.""" ) # fine-tuning config parameters for SpecAugment: https://arxiv.org/abs/1904.08779 __snake_case = apply_spec_augment __snake_case = mask_time_prob __snake_case = mask_time_length __snake_case = mask_time_min_masks __snake_case = mask_feature_prob __snake_case = mask_feature_length __snake_case = mask_feature_min_masks # parameters for pretraining with codevector quantized representations __snake_case = num_codevectors_per_group __snake_case = num_codevector_groups __snake_case = contrastive_logits_temperature __snake_case = feat_quantizer_dropout __snake_case = num_negatives __snake_case = codevector_dim __snake_case = proj_codevector_dim __snake_case = diversity_loss_weight # ctc loss __snake_case = ctc_loss_reduction __snake_case = ctc_zero_infinity # adapter __snake_case = add_adapter __snake_case = adapter_kernel_size __snake_case = adapter_stride __snake_case = num_adapter_layers __snake_case = output_hidden_size or hidden_size __snake_case = adapter_attn_dim # SequenceClassification-specific parameter. Feel free to ignore for other classes. __snake_case = classifier_proj_size # XVector-specific parameters. Feel free to ignore for other classes. __snake_case = list(_a ) __snake_case = list(_a ) __snake_case = list(_a ) __snake_case = xvector_output_dim @property def UpperCamelCase_ ( self : Optional[int] ): """simple docstring""" return functools.reduce(operator.mul ,self.conv_stride ,1 )
524
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 A__ ( UpperCamelCase__ ): def __UpperCamelCase ( self : Tuple ) -> Dict: """simple docstring""" _SCREAMING_SNAKE_CASE =tempfile.mkdtemp() _SCREAMING_SNAKE_CASE =8 # DPR tok _SCREAMING_SNAKE_CASE =[ '''[UNK]''', '''[CLS]''', '''[SEP]''', '''[PAD]''', '''[MASK]''', '''want''', '''##want''', '''##ed''', '''wa''', '''un''', '''runn''', '''##ing''', ''',''', '''low''', '''lowest''', ] _SCREAMING_SNAKE_CASE =os.path.join(self.tmpdirname , '''dpr_tokenizer''' ) os.makedirs(_a , exist_ok=_a ) _SCREAMING_SNAKE_CASE =os.path.join(_a , 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 _SCREAMING_SNAKE_CASE =[ '''l''', '''o''', '''w''', '''e''', '''r''', '''s''', '''t''', '''i''', '''d''', '''n''', '''\u0120''', '''\u0120l''', '''\u0120n''', '''\u0120lo''', '''\u0120low''', '''er''', '''\u0120lowest''', '''\u0120newer''', '''\u0120wider''', '''<unk>''', ] _SCREAMING_SNAKE_CASE =dict(zip(_a , range(len(_a ) ) ) ) _SCREAMING_SNAKE_CASE =['''#version: 0.2''', '''\u0120 l''', '''\u0120l o''', '''\u0120lo w''', '''e r''', ''''''] _SCREAMING_SNAKE_CASE ={'''unk_token''': '''<unk>'''} _SCREAMING_SNAKE_CASE =os.path.join(self.tmpdirname , '''bart_tokenizer''' ) os.makedirs(_a , exist_ok=_a ) _SCREAMING_SNAKE_CASE =os.path.join(_a , BART_VOCAB_FILES_NAMES['''vocab_file'''] ) _SCREAMING_SNAKE_CASE =os.path.join(_a , BART_VOCAB_FILES_NAMES['''merges_file'''] ) with open(self.vocab_file , '''w''' , encoding='''utf-8''' ) as fp: fp.write(json.dumps(_a ) + '''\n''' ) with open(self.merges_file , '''w''' , encoding='''utf-8''' ) as fp: fp.write('''\n'''.join(_a ) ) def __UpperCamelCase ( self : List[str] ) -> DPRQuestionEncoderTokenizer: """simple docstring""" return DPRQuestionEncoderTokenizer.from_pretrained(os.path.join(self.tmpdirname , '''dpr_tokenizer''' ) ) def __UpperCamelCase ( self : Dict ) -> DPRContextEncoderTokenizer: """simple docstring""" return DPRContextEncoderTokenizer.from_pretrained(os.path.join(self.tmpdirname , '''dpr_tokenizer''' ) ) def __UpperCamelCase ( self : Union[str, Any] ) -> BartTokenizer: """simple docstring""" return BartTokenizer.from_pretrained(os.path.join(self.tmpdirname , '''bart_tokenizer''' ) ) def __UpperCamelCase ( self : Union[str, Any] ) -> int: """simple docstring""" shutil.rmtree(self.tmpdirname ) def __UpperCamelCase ( self : Union[str, Any] ) -> int: """simple docstring""" _SCREAMING_SNAKE_CASE =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 __UpperCamelCase ( self : str ) -> List[str]: """simple docstring""" _SCREAMING_SNAKE_CASE =self.get_dummy_dataset() _SCREAMING_SNAKE_CASE =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: _SCREAMING_SNAKE_CASE =dataset _SCREAMING_SNAKE_CASE =RagRetriever( _a , question_encoder_tokenizer=self.get_dpr_tokenizer() , generator_tokenizer=self.get_bart_tokenizer() , ) return retriever def __UpperCamelCase ( self : Optional[int] , _a : bool ) -> Any: """simple docstring""" _SCREAMING_SNAKE_CASE =self.get_dummy_dataset() _SCREAMING_SNAKE_CASE =RagConfig( retrieval_vector_size=self.retrieval_vector_size , question_encoder=DPRConfig().to_dict() , generator=BartConfig().to_dict() , index_name='''custom''' , ) if from_disk: _SCREAMING_SNAKE_CASE =os.path.join(self.tmpdirname , '''dataset''' ) _SCREAMING_SNAKE_CASE =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 _SCREAMING_SNAKE_CASE =RagRetriever( _a , question_encoder_tokenizer=self.get_dpr_tokenizer() , generator_tokenizer=self.get_bart_tokenizer() , ) else: _SCREAMING_SNAKE_CASE =RagRetriever( _a , question_encoder_tokenizer=self.get_dpr_tokenizer() , generator_tokenizer=self.get_bart_tokenizer() , index=CustomHFIndex(config.retrieval_vector_size , _a ) , ) return retriever def __UpperCamelCase ( self : Optional[Any] ) -> Dict: """simple docstring""" _SCREAMING_SNAKE_CASE =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 ) _SCREAMING_SNAKE_CASE =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''' ) ) _SCREAMING_SNAKE_CASE =os.path.join(self.tmpdirname , '''psgs_w100.tsv.pkl''' ) _SCREAMING_SNAKE_CASE ={sample['''id''']: [sample['''text'''], sample['''title''']] for sample in dataset} pickle.dump(_a , open(_a , '''wb''' ) ) _SCREAMING_SNAKE_CASE =RagConfig( retrieval_vector_size=self.retrieval_vector_size , question_encoder=DPRConfig().to_dict() , generator=BartConfig().to_dict() , index_name='''legacy''' , index_path=self.tmpdirname , ) _SCREAMING_SNAKE_CASE =RagRetriever( _a , question_encoder_tokenizer=self.get_dpr_tokenizer() , generator_tokenizer=self.get_bart_tokenizer() ) return retriever def __UpperCamelCase ( self : Tuple ) -> Optional[Any]: """simple docstring""" _SCREAMING_SNAKE_CASE =1 _SCREAMING_SNAKE_CASE =self.get_dummy_canonical_hf_index_retriever() _SCREAMING_SNAKE_CASE =np.array( [np.ones(self.retrieval_vector_size ), -np.ones(self.retrieval_vector_size )] , dtype=np.floataa ) _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE =retriever.retrieve(_a , n_docs=_a ) self.assertEqual(retrieved_doc_embeds.shape , (2, n_docs, self.retrieval_vector_size) ) self.assertEqual(len(_a ) , 2 ) self.assertEqual(sorted(doc_dicts[0] ) , ['''embeddings''', '''id''', '''text''', '''title'''] ) self.assertEqual(len(doc_dicts[0]['''id'''] ) , _a ) 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 __UpperCamelCase ( self : Any ) -> str: """simple docstring""" _SCREAMING_SNAKE_CASE =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: _SCREAMING_SNAKE_CASE =self.get_dummy_dataset() retriever.save_pretrained(_a ) _SCREAMING_SNAKE_CASE =RagRetriever.from_pretrained(_a ) self.assertIsInstance(_a , _a ) _SCREAMING_SNAKE_CASE =np.array( [np.ones(self.retrieval_vector_size ), -np.ones(self.retrieval_vector_size )] , dtype=np.floataa ) _SCREAMING_SNAKE_CASE =retriever.retrieve(_a , n_docs=1 ) self.assertTrue(out is not None ) def __UpperCamelCase ( self : Dict ) -> Union[str, Any]: """simple docstring""" _SCREAMING_SNAKE_CASE =1 _SCREAMING_SNAKE_CASE =self.get_dummy_custom_hf_index_retriever(from_disk=_a ) _SCREAMING_SNAKE_CASE =np.array( [np.ones(self.retrieval_vector_size ), -np.ones(self.retrieval_vector_size )] , dtype=np.floataa ) _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE =retriever.retrieve(_a , n_docs=_a ) self.assertEqual(retrieved_doc_embeds.shape , (2, n_docs, self.retrieval_vector_size) ) self.assertEqual(len(_a ) , 2 ) self.assertEqual(sorted(doc_dicts[0] ) , ['''embeddings''', '''id''', '''text''', '''title'''] ) self.assertEqual(len(doc_dicts[0]['''id'''] ) , _a ) 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 __UpperCamelCase ( self : Optional[Any] ) -> Any: """simple docstring""" _SCREAMING_SNAKE_CASE =self.get_dummy_custom_hf_index_retriever(from_disk=_a ) with tempfile.TemporaryDirectory() as tmp_dirname: retriever.save_pretrained(_a ) _SCREAMING_SNAKE_CASE =RagRetriever.from_pretrained(_a ) self.assertIsInstance(_a , _a ) _SCREAMING_SNAKE_CASE =np.array( [np.ones(self.retrieval_vector_size ), -np.ones(self.retrieval_vector_size )] , dtype=np.floataa ) _SCREAMING_SNAKE_CASE =retriever.retrieve(_a , n_docs=1 ) self.assertTrue(out is not None ) def __UpperCamelCase ( self : Dict ) -> Optional[int]: """simple docstring""" _SCREAMING_SNAKE_CASE =1 _SCREAMING_SNAKE_CASE =self.get_dummy_custom_hf_index_retriever(from_disk=_a ) _SCREAMING_SNAKE_CASE =np.array( [np.ones(self.retrieval_vector_size ), -np.ones(self.retrieval_vector_size )] , dtype=np.floataa ) _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE =retriever.retrieve(_a , n_docs=_a ) self.assertEqual(retrieved_doc_embeds.shape , (2, n_docs, self.retrieval_vector_size) ) self.assertEqual(len(_a ) , 2 ) self.assertEqual(sorted(doc_dicts[0] ) , ['''embeddings''', '''id''', '''text''', '''title'''] ) self.assertEqual(len(doc_dicts[0]['''id'''] ) , _a ) 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 __UpperCamelCase ( self : Tuple ) -> Optional[Any]: """simple docstring""" _SCREAMING_SNAKE_CASE =self.get_dummy_custom_hf_index_retriever(from_disk=_a ) with tempfile.TemporaryDirectory() as tmp_dirname: retriever.save_pretrained(_a ) _SCREAMING_SNAKE_CASE =RagRetriever.from_pretrained(_a ) self.assertIsInstance(_a , _a ) _SCREAMING_SNAKE_CASE =np.array( [np.ones(self.retrieval_vector_size ), -np.ones(self.retrieval_vector_size )] , dtype=np.floataa ) _SCREAMING_SNAKE_CASE =retriever.retrieve(_a , n_docs=1 ) self.assertTrue(out is not None ) def __UpperCamelCase ( self : Optional[int] ) -> Dict: """simple docstring""" _SCREAMING_SNAKE_CASE =1 _SCREAMING_SNAKE_CASE =self.get_dummy_legacy_index_retriever() _SCREAMING_SNAKE_CASE =np.array( [np.ones(self.retrieval_vector_size ), -np.ones(self.retrieval_vector_size )] , dtype=np.floataa ) _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE =retriever.retrieve(_a , n_docs=_a ) self.assertEqual(retrieved_doc_embeds.shape , (2, n_docs, self.retrieval_vector_size) ) self.assertEqual(len(_a ) , 2 ) self.assertEqual(sorted(doc_dicts[0] ) , ['''text''', '''title'''] ) self.assertEqual(len(doc_dicts[0]['''text'''] ) , _a ) 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 __UpperCamelCase ( self : Dict ) -> Dict: """simple docstring""" _SCREAMING_SNAKE_CASE =self.get_dummy_legacy_index_retriever() with tempfile.TemporaryDirectory() as tmp_dirname: retriever.save_pretrained(_a ) _SCREAMING_SNAKE_CASE =RagRetriever.from_pretrained(_a ) self.assertIsInstance(_a , _a ) _SCREAMING_SNAKE_CASE =np.array( [np.ones(self.retrieval_vector_size ), -np.ones(self.retrieval_vector_size )] , dtype=np.floataa ) _SCREAMING_SNAKE_CASE =retriever.retrieve(_a , n_docs=1 ) self.assertTrue(out is not None ) @require_torch @require_tokenizers @require_sentencepiece def __UpperCamelCase ( self : Optional[int] ) -> int: """simple docstring""" import torch _SCREAMING_SNAKE_CASE =1 _SCREAMING_SNAKE_CASE =self.get_dummy_canonical_hf_index_retriever() _SCREAMING_SNAKE_CASE =[[5, 7], [10, 11]] _SCREAMING_SNAKE_CASE =np.array( [np.ones(self.retrieval_vector_size ), -np.ones(self.retrieval_vector_size )] , dtype=np.floataa ) _SCREAMING_SNAKE_CASE =retriever(_a , _a , prefix=retriever.config.generator.prefix , n_docs=_a ) _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE =( 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(_a , _a ) self.assertIsInstance(_a , _a ) self.assertIsInstance(_a , np.ndarray ) _SCREAMING_SNAKE_CASE =retriever( _a , _a , prefix=retriever.config.generator.prefix , n_docs=_a , return_tensors='''pt''' , ) _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE =( # 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(_a , torch.Tensor ) self.assertIsInstance(_a , torch.Tensor ) self.assertIsInstance(_a , torch.Tensor ) @require_torch @require_tokenizers @require_sentencepiece def __UpperCamelCase ( self : str ) -> Any: """simple docstring""" _SCREAMING_SNAKE_CASE =self.get_dpr_ctx_encoder_tokenizer() _SCREAMING_SNAKE_CASE =1 _SCREAMING_SNAKE_CASE =self.get_dummy_custom_hf_index_retriever(from_disk=_a ) retriever.set_ctx_encoder_tokenizer(_a ) _SCREAMING_SNAKE_CASE =[[5, 7], [10, 11]] _SCREAMING_SNAKE_CASE =np.array( [np.ones(self.retrieval_vector_size ), -np.ones(self.retrieval_vector_size )] , dtype=np.floataa ) _SCREAMING_SNAKE_CASE =retriever(_a , _a , prefix=retriever.config.generator.prefix , n_docs=_a ) self.assertEqual( len(_a ) , 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''') ) , _a ) # check for doc token related keys in dictionary.
691
0
from ...configuration_utils import PretrainedConfig from ...utils import logging _UpperCAmelCase : Union[str, Any] = logging.get_logger(__name__) _UpperCAmelCase : List[Any] = { '''alibaba-damo/mgp-str-base''': '''https://huggingface.co/alibaba-damo/mgp-str-base/resolve/main/config.json''', } class lowercase ( UpperCamelCase__ ): __SCREAMING_SNAKE_CASE : Any = '''mgp-str''' def __init__( self , snake_case=[32, 128] , snake_case=4 , snake_case=3 , snake_case=27 , snake_case=38 , snake_case=5_0257 , snake_case=3_0522 , snake_case=768 , snake_case=12 , snake_case=12 , snake_case=4.0 , snake_case=True , snake_case=False , snake_case=1e-5 , snake_case=0.0 , snake_case=0.0 , snake_case=0.0 , snake_case=False , snake_case=0.02 , **snake_case , ): super().__init__(**_a ) snake_case_ = image_size snake_case_ = patch_size snake_case_ = num_channels snake_case_ = max_token_length snake_case_ = num_character_labels snake_case_ = num_bpe_labels snake_case_ = num_wordpiece_labels snake_case_ = hidden_size snake_case_ = num_hidden_layers snake_case_ = num_attention_heads snake_case_ = mlp_ratio snake_case_ = distilled snake_case_ = layer_norm_eps snake_case_ = drop_rate snake_case_ = qkv_bias snake_case_ = attn_drop_rate snake_case_ = drop_path_rate snake_case_ = output_aa_attentions snake_case_ = initializer_range
362
import gc import random import unittest import numpy as np import torch from PIL import Image from transformers import XLMRobertaTokenizerFast from diffusers import DDIMScheduler, KandinskyImgaImgPipeline, KandinskyPriorPipeline, UNetaDConditionModel, VQModel from diffusers.pipelines.kandinsky.text_encoder import MCLIPConfig, MultilingualCLIP from diffusers.utils import floats_tensor, load_image, load_numpy, slow, torch_device from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu from ..test_pipelines_common import PipelineTesterMixin, assert_mean_pixel_difference enable_full_determinism() class A__ ( UpperCamelCase__ , unittest.TestCase ): UpperCAmelCase = KandinskyImgaImgPipeline UpperCAmelCase = ["prompt", "image_embeds", "negative_image_embeds", "image"] UpperCAmelCase = [ "prompt", "negative_prompt", "image_embeds", "negative_image_embeds", "image", ] UpperCAmelCase = [ "generator", "height", "width", "strength", "guidance_scale", "negative_prompt", "num_inference_steps", "return_dict", "guidance_scale", "num_images_per_prompt", "output_type", "return_dict", ] UpperCAmelCase = False @property def __UpperCamelCase ( self : Union[str, Any] ) -> Union[str, Any]: """simple docstring""" return 32 @property def __UpperCamelCase ( self : Optional[int] ) -> List[str]: """simple docstring""" return 32 @property def __UpperCamelCase ( self : int ) -> Tuple: """simple docstring""" return self.time_input_dim @property def __UpperCamelCase ( self : Tuple ) -> List[Any]: """simple docstring""" return self.time_input_dim * 4 @property def __UpperCamelCase ( self : Any ) -> Optional[Any]: """simple docstring""" return 100 @property def __UpperCamelCase ( self : Dict ) -> int: """simple docstring""" _SCREAMING_SNAKE_CASE =XLMRobertaTokenizerFast.from_pretrained('''YiYiXu/tiny-random-mclip-base''' ) return tokenizer @property def __UpperCamelCase ( self : Dict ) -> Optional[int]: """simple docstring""" torch.manual_seed(0 ) _SCREAMING_SNAKE_CASE =MCLIPConfig( numDims=self.cross_attention_dim , transformerDimensions=self.text_embedder_hidden_size , hidden_size=self.text_embedder_hidden_size , intermediate_size=37 , num_attention_heads=4 , num_hidden_layers=5 , vocab_size=1005 , ) _SCREAMING_SNAKE_CASE =MultilingualCLIP(_a ) _SCREAMING_SNAKE_CASE =text_encoder.eval() return text_encoder @property def __UpperCamelCase ( self : List[Any] ) -> List[str]: """simple docstring""" torch.manual_seed(0 ) _SCREAMING_SNAKE_CASE ={ '''in_channels''': 4, # Out channels is double in channels because predicts mean and variance '''out_channels''': 8, '''addition_embed_type''': '''text_image''', '''down_block_types''': ('''ResnetDownsampleBlock2D''', '''SimpleCrossAttnDownBlock2D'''), '''up_block_types''': ('''SimpleCrossAttnUpBlock2D''', '''ResnetUpsampleBlock2D'''), '''mid_block_type''': '''UNetMidBlock2DSimpleCrossAttn''', '''block_out_channels''': (self.block_out_channels_a, self.block_out_channels_a * 2), '''layers_per_block''': 1, '''encoder_hid_dim''': self.text_embedder_hidden_size, '''encoder_hid_dim_type''': '''text_image_proj''', '''cross_attention_dim''': self.cross_attention_dim, '''attention_head_dim''': 4, '''resnet_time_scale_shift''': '''scale_shift''', '''class_embed_type''': None, } _SCREAMING_SNAKE_CASE =UNetaDConditionModel(**_a ) return model @property def __UpperCamelCase ( self : List[Any] ) -> Union[str, Any]: """simple docstring""" return { "block_out_channels": [32, 64], "down_block_types": ["DownEncoderBlock2D", "AttnDownEncoderBlock2D"], "in_channels": 3, "latent_channels": 4, "layers_per_block": 1, "norm_num_groups": 8, "norm_type": "spatial", "num_vq_embeddings": 12, "out_channels": 3, "up_block_types": [ "AttnUpDecoderBlock2D", "UpDecoderBlock2D", ], "vq_embed_dim": 4, } @property def __UpperCamelCase ( self : List[Any] ) -> Optional[Any]: """simple docstring""" torch.manual_seed(0 ) _SCREAMING_SNAKE_CASE =VQModel(**self.dummy_movq_kwargs ) return model def __UpperCamelCase ( self : str ) -> int: """simple docstring""" _SCREAMING_SNAKE_CASE =self.dummy_text_encoder _SCREAMING_SNAKE_CASE =self.dummy_tokenizer _SCREAMING_SNAKE_CASE =self.dummy_unet _SCREAMING_SNAKE_CASE =self.dummy_movq _SCREAMING_SNAKE_CASE ={ '''num_train_timesteps''': 1000, '''beta_schedule''': '''linear''', '''beta_start''': 0.0_00_85, '''beta_end''': 0.0_12, '''clip_sample''': False, '''set_alpha_to_one''': False, '''steps_offset''': 0, '''prediction_type''': '''epsilon''', '''thresholding''': False, } _SCREAMING_SNAKE_CASE =DDIMScheduler(**_a ) _SCREAMING_SNAKE_CASE ={ '''text_encoder''': text_encoder, '''tokenizer''': tokenizer, '''unet''': unet, '''scheduler''': scheduler, '''movq''': movq, } return components def __UpperCamelCase ( self : str , _a : int , _a : int=0 ) -> Any: """simple docstring""" _SCREAMING_SNAKE_CASE =floats_tensor((1, self.cross_attention_dim) , rng=random.Random(_a ) ).to(_a ) _SCREAMING_SNAKE_CASE =floats_tensor((1, self.cross_attention_dim) , rng=random.Random(seed + 1 ) ).to(_a ) # create init_image _SCREAMING_SNAKE_CASE =floats_tensor((1, 3, 64, 64) , rng=random.Random(_a ) ).to(_a ) _SCREAMING_SNAKE_CASE =image.cpu().permute(0 , 2 , 3 , 1 )[0] _SCREAMING_SNAKE_CASE =Image.fromarray(np.uinta(_a ) ).convert('''RGB''' ).resize((256, 256) ) if str(_a ).startswith('''mps''' ): _SCREAMING_SNAKE_CASE =torch.manual_seed(_a ) else: _SCREAMING_SNAKE_CASE =torch.Generator(device=_a ).manual_seed(_a ) _SCREAMING_SNAKE_CASE ={ '''prompt''': '''horse''', '''image''': init_image, '''image_embeds''': image_embeds, '''negative_image_embeds''': negative_image_embeds, '''generator''': generator, '''height''': 64, '''width''': 64, '''num_inference_steps''': 10, '''guidance_scale''': 7.0, '''strength''': 0.2, '''output_type''': '''np''', } return inputs def __UpperCamelCase ( self : Any ) -> Optional[int]: """simple docstring""" _SCREAMING_SNAKE_CASE ='''cpu''' _SCREAMING_SNAKE_CASE =self.get_dummy_components() _SCREAMING_SNAKE_CASE =self.pipeline_class(**_a ) _SCREAMING_SNAKE_CASE =pipe.to(_a ) pipe.set_progress_bar_config(disable=_a ) _SCREAMING_SNAKE_CASE =pipe(**self.get_dummy_inputs(_a ) ) _SCREAMING_SNAKE_CASE =output.images _SCREAMING_SNAKE_CASE =pipe( **self.get_dummy_inputs(_a ) , return_dict=_a , )[0] _SCREAMING_SNAKE_CASE =image[0, -3:, -3:, -1] _SCREAMING_SNAKE_CASE =image_from_tuple[0, -3:, -3:, -1] assert image.shape == (1, 64, 64, 3) _SCREAMING_SNAKE_CASE =np.array( [0.61_47_49_43, 0.6_07_35_39, 0.43_30_85_44, 0.5_92_82_69, 0.47_49_35_95, 0.46_75_59_73, 0.4_61_38_38, 0.45_36_87_97, 0.50_11_92_33] ) assert ( np.abs(image_slice.flatten() - expected_slice ).max() < 1E-2 ), f" expected_slice {expected_slice}, but got {image_slice.flatten()}" assert ( np.abs(image_from_tuple_slice.flatten() - expected_slice ).max() < 1E-2 ), f" expected_slice {expected_slice}, but got {image_from_tuple_slice.flatten()}" @slow @require_torch_gpu class A__ ( unittest.TestCase ): def __UpperCamelCase ( self : Optional[Any] ) -> List[Any]: """simple docstring""" super().tearDown() gc.collect() torch.cuda.empty_cache() def __UpperCamelCase ( self : Dict ) -> Any: """simple docstring""" _SCREAMING_SNAKE_CASE =load_numpy( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main''' '''/kandinsky/kandinsky_img2img_frog.npy''' ) _SCREAMING_SNAKE_CASE =load_image( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main''' '''/kandinsky/cat.png''' ) _SCREAMING_SNAKE_CASE ='''A red cartoon frog, 4k''' _SCREAMING_SNAKE_CASE =KandinskyPriorPipeline.from_pretrained( '''kandinsky-community/kandinsky-2-1-prior''' , torch_dtype=torch.floataa ) pipe_prior.to(_a ) _SCREAMING_SNAKE_CASE =KandinskyImgaImgPipeline.from_pretrained( '''kandinsky-community/kandinsky-2-1''' , torch_dtype=torch.floataa ) _SCREAMING_SNAKE_CASE =pipeline.to(_a ) pipeline.set_progress_bar_config(disable=_a ) _SCREAMING_SNAKE_CASE =torch.Generator(device='''cpu''' ).manual_seed(0 ) _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE =pipe_prior( _a , generator=_a , num_inference_steps=5 , negative_prompt='''''' , ).to_tuple() _SCREAMING_SNAKE_CASE =pipeline( _a , image=_a , image_embeds=_a , negative_image_embeds=_a , generator=_a , num_inference_steps=100 , height=768 , width=768 , strength=0.2 , output_type='''np''' , ) _SCREAMING_SNAKE_CASE =output.images[0] assert image.shape == (768, 768, 3) assert_mean_pixel_difference(_a , _a )
691
0
"""simple docstring""" from __future__ import annotations _A = [-10, -5, 0, 5, 5.1, 11, 13, 21, 3, 4, -21, -10, -5, -1, 0] _A = [-5, 0, 5, 5.1, 11, 13, 21, -1, 4, -1, -10, -5, -1, 0, -1] def a__ ( lowerCAmelCase ) -> Dict: UpperCAmelCase__ : Union[str, Any] = [] UpperCAmelCase__ : str = len(a__ ) for i in range(a__ ): UpperCAmelCase__ : List[Any] = -1 for j in range(i + 1 , a__ ): if arr[i] < arr[j]: UpperCAmelCase__ : List[str] = arr[j] break result.append(a__ ) return result def a__ ( lowerCAmelCase ) -> str: UpperCAmelCase__ : List[str] = [] for i, outer in enumerate(a__ ): UpperCAmelCase__ : int = -1 for inner in arr[i + 1 :]: if outer < inner: UpperCAmelCase__ : Any = inner break result.append(a__ ) return result def a__ ( lowerCAmelCase ) -> Dict: UpperCAmelCase__ : int = len(a__ ) UpperCAmelCase__ : Optional[int] = [] UpperCAmelCase__ : int = [-1] * arr_size for index in reversed(range(a__ ) ): if stack: while stack[-1] <= arr[index]: stack.pop() if not stack: break if stack: UpperCAmelCase__ : Union[str, Any] = stack[-1] stack.append(arr[index] ) return result if __name__ == "__main__": from doctest import testmod from timeit import timeit testmod() print(next_greatest_element_slow(arr)) print(next_greatest_element_fast(arr)) print(next_greatest_element(arr)) _A = ( '''from __main__ import arr, next_greatest_element_slow, ''' '''next_greatest_element_fast, next_greatest_element''' ) print( """next_greatest_element_slow():""", timeit("""next_greatest_element_slow(arr)""", setup=setup), ) print( """next_greatest_element_fast():""", timeit("""next_greatest_element_fast(arr)""", setup=setup), ) print( """ next_greatest_element():""", timeit("""next_greatest_element(arr)""", setup=setup), )
182
import unittest import numpy as np from transformers.testing_utils import require_torch, require_vision from transformers.utils import is_torch_available, is_vision_available from ...test_image_processing_common import ImageProcessingSavingTestMixin if is_torch_available(): import torch if is_vision_available(): from PIL import Image from transformers import ChineseCLIPImageProcessor class A__ ( unittest.TestCase ): def __init__( self : List[str] , _a : Dict , _a : Dict=7 , _a : List[str]=3 , _a : str=18 , _a : Optional[int]=30 , _a : Tuple=400 , _a : Optional[Any]=True , _a : Dict=None , _a : str=True , _a : Tuple=None , _a : Any=True , _a : Any=[0.48_14_54_66, 0.4_57_82_75, 0.40_82_10_73] , _a : str=[0.26_86_29_54, 0.26_13_02_58, 0.27_57_77_11] , _a : List[Any]=True , ) -> Optional[int]: """simple docstring""" _SCREAMING_SNAKE_CASE =size if size is not None else {'''height''': 224, '''width''': 224} _SCREAMING_SNAKE_CASE =crop_size if crop_size is not None else {'''height''': 18, '''width''': 18} _SCREAMING_SNAKE_CASE =parent _SCREAMING_SNAKE_CASE =batch_size _SCREAMING_SNAKE_CASE =num_channels _SCREAMING_SNAKE_CASE =image_size _SCREAMING_SNAKE_CASE =min_resolution _SCREAMING_SNAKE_CASE =max_resolution _SCREAMING_SNAKE_CASE =do_resize _SCREAMING_SNAKE_CASE =size _SCREAMING_SNAKE_CASE =do_center_crop _SCREAMING_SNAKE_CASE =crop_size _SCREAMING_SNAKE_CASE =do_normalize _SCREAMING_SNAKE_CASE =image_mean _SCREAMING_SNAKE_CASE =image_std _SCREAMING_SNAKE_CASE =do_convert_rgb def __UpperCamelCase ( self : Any ) -> Tuple: """simple docstring""" return { "do_resize": self.do_resize, "size": self.size, "do_center_crop": self.do_center_crop, "crop_size": self.crop_size, "do_normalize": self.do_normalize, "image_mean": self.image_mean, "image_std": self.image_std, "do_convert_rgb": self.do_convert_rgb, } def __UpperCamelCase ( self : Tuple , _a : Optional[Any]=False , _a : str=False , _a : Dict=False ) -> Dict: """simple docstring""" assert not (numpify and torchify), "You cannot specify both numpy and PyTorch tensors at the same time" if equal_resolution: _SCREAMING_SNAKE_CASE =[] for i in range(self.batch_size ): image_inputs.append( np.random.randint( 255 , size=(self.num_channels, self.max_resolution, self.max_resolution) , dtype=np.uinta ) ) else: _SCREAMING_SNAKE_CASE =[] for i in range(self.batch_size ): _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE =np.random.choice(np.arange(self.min_resolution , self.max_resolution ) , 2 ) image_inputs.append(np.random.randint(255 , size=(self.num_channels, width, height) , dtype=np.uinta ) ) if not numpify and not torchify: # PIL expects the channel dimension as last dimension _SCREAMING_SNAKE_CASE =[Image.fromarray(np.moveaxis(_a , 0 , -1 ) ) for x in image_inputs] if torchify: _SCREAMING_SNAKE_CASE =[torch.from_numpy(_a ) for x in image_inputs] return image_inputs @require_torch @require_vision class A__ ( UpperCamelCase__ , unittest.TestCase ): UpperCAmelCase = ChineseCLIPImageProcessor if is_vision_available() else None def __UpperCamelCase ( self : Any ) -> Tuple: """simple docstring""" _SCREAMING_SNAKE_CASE =ChineseCLIPImageProcessingTester(self , do_center_crop=_a ) @property def __UpperCamelCase ( self : Union[str, Any] ) -> Tuple: """simple docstring""" return self.image_processor_tester.prepare_image_processor_dict() def __UpperCamelCase ( self : int ) -> Dict: """simple docstring""" _SCREAMING_SNAKE_CASE =self.image_processing_class(**self.image_processor_dict ) self.assertTrue(hasattr(_a , '''do_resize''' ) ) self.assertTrue(hasattr(_a , '''size''' ) ) self.assertTrue(hasattr(_a , '''do_center_crop''' ) ) self.assertTrue(hasattr(_a , '''center_crop''' ) ) self.assertTrue(hasattr(_a , '''do_normalize''' ) ) self.assertTrue(hasattr(_a , '''image_mean''' ) ) self.assertTrue(hasattr(_a , '''image_std''' ) ) self.assertTrue(hasattr(_a , '''do_convert_rgb''' ) ) def __UpperCamelCase ( self : List[str] ) -> str: """simple docstring""" _SCREAMING_SNAKE_CASE =self.image_processing_class.from_dict(self.image_processor_dict ) self.assertEqual(image_processor.size , {'''height''': 224, '''width''': 224} ) self.assertEqual(image_processor.crop_size , {'''height''': 18, '''width''': 18} ) _SCREAMING_SNAKE_CASE =self.image_processing_class.from_dict(self.image_processor_dict , size=42 , crop_size=84 ) self.assertEqual(image_processor.size , {'''shortest_edge''': 42} ) self.assertEqual(image_processor.crop_size , {'''height''': 84, '''width''': 84} ) def __UpperCamelCase ( self : Union[str, Any] ) -> int: """simple docstring""" pass def __UpperCamelCase ( self : str ) -> List[str]: """simple docstring""" _SCREAMING_SNAKE_CASE =self.image_processing_class(**self.image_processor_dict ) # create random PIL images _SCREAMING_SNAKE_CASE =self.image_processor_tester.prepare_inputs(equal_resolution=_a ) for image in image_inputs: self.assertIsInstance(_a , Image.Image ) # Test not batched input _SCREAMING_SNAKE_CASE =image_processing(image_inputs[0] , return_tensors='''pt''' ).pixel_values self.assertEqual( encoded_images.shape , ( 1, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size['''height'''], self.image_processor_tester.crop_size['''width'''], ) , ) # Test batched _SCREAMING_SNAKE_CASE =image_processing(_a , return_tensors='''pt''' ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size['''height'''], self.image_processor_tester.crop_size['''width'''], ) , ) def __UpperCamelCase ( self : Optional[Any] ) -> List[str]: """simple docstring""" _SCREAMING_SNAKE_CASE =self.image_processing_class(**self.image_processor_dict ) # create random numpy tensors _SCREAMING_SNAKE_CASE =self.image_processor_tester.prepare_inputs(equal_resolution=_a , numpify=_a ) for image in image_inputs: self.assertIsInstance(_a , np.ndarray ) # Test not batched input _SCREAMING_SNAKE_CASE =image_processing(image_inputs[0] , return_tensors='''pt''' ).pixel_values self.assertEqual( encoded_images.shape , ( 1, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size['''height'''], self.image_processor_tester.crop_size['''width'''], ) , ) # Test batched _SCREAMING_SNAKE_CASE =image_processing(_a , return_tensors='''pt''' ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size['''height'''], self.image_processor_tester.crop_size['''width'''], ) , ) def __UpperCamelCase ( self : Optional[int] ) -> Tuple: """simple docstring""" _SCREAMING_SNAKE_CASE =self.image_processing_class(**self.image_processor_dict ) # create random PyTorch tensors _SCREAMING_SNAKE_CASE =self.image_processor_tester.prepare_inputs(equal_resolution=_a , torchify=_a ) for image in image_inputs: self.assertIsInstance(_a , torch.Tensor ) # Test not batched input _SCREAMING_SNAKE_CASE =image_processing(image_inputs[0] , return_tensors='''pt''' ).pixel_values self.assertEqual( encoded_images.shape , ( 1, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size['''height'''], self.image_processor_tester.crop_size['''width'''], ) , ) # Test batched _SCREAMING_SNAKE_CASE =image_processing(_a , return_tensors='''pt''' ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size['''height'''], self.image_processor_tester.crop_size['''width'''], ) , ) @require_torch @require_vision class A__ ( UpperCamelCase__ , unittest.TestCase ): UpperCAmelCase = ChineseCLIPImageProcessor if is_vision_available() else None def __UpperCamelCase ( self : int ) -> str: """simple docstring""" _SCREAMING_SNAKE_CASE =ChineseCLIPImageProcessingTester(self , num_channels=4 , do_center_crop=_a ) _SCREAMING_SNAKE_CASE =3 @property def __UpperCamelCase ( self : Optional[int] ) -> Tuple: """simple docstring""" return self.image_processor_tester.prepare_image_processor_dict() def __UpperCamelCase ( self : int ) -> List[Any]: """simple docstring""" _SCREAMING_SNAKE_CASE =self.image_processing_class(**self.image_processor_dict ) self.assertTrue(hasattr(_a , '''do_resize''' ) ) self.assertTrue(hasattr(_a , '''size''' ) ) self.assertTrue(hasattr(_a , '''do_center_crop''' ) ) self.assertTrue(hasattr(_a , '''center_crop''' ) ) self.assertTrue(hasattr(_a , '''do_normalize''' ) ) self.assertTrue(hasattr(_a , '''image_mean''' ) ) self.assertTrue(hasattr(_a , '''image_std''' ) ) self.assertTrue(hasattr(_a , '''do_convert_rgb''' ) ) def __UpperCamelCase ( self : Any ) -> Union[str, Any]: """simple docstring""" pass def __UpperCamelCase ( self : Dict ) -> Any: """simple docstring""" _SCREAMING_SNAKE_CASE =self.image_processing_class(**self.image_processor_dict ) # create random PIL images _SCREAMING_SNAKE_CASE =self.image_processor_tester.prepare_inputs(equal_resolution=_a ) for image in image_inputs: self.assertIsInstance(_a , Image.Image ) # Test not batched input _SCREAMING_SNAKE_CASE =image_processing(image_inputs[0] , return_tensors='''pt''' ).pixel_values self.assertEqual( encoded_images.shape , ( 1, self.expected_encoded_image_num_channels, self.image_processor_tester.crop_size['''height'''], self.image_processor_tester.crop_size['''width'''], ) , ) # Test batched _SCREAMING_SNAKE_CASE =image_processing(_a , return_tensors='''pt''' ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.expected_encoded_image_num_channels, self.image_processor_tester.crop_size['''height'''], self.image_processor_tester.crop_size['''width'''], ) , )
691
0
"""simple docstring""" import requests from bsa import BeautifulSoup def lowerCamelCase ( _UpperCamelCase : Optional[int] = "https://www.worldometers.info/coronavirus" ) -> str: '''simple docstring''' __UpperCAmelCase : Union[str, Any] = BeautifulSoup(requests.get(a__ ).text , """html.parser""" ) __UpperCAmelCase : Any = soup.findAll("""h1""" ) __UpperCAmelCase : Union[str, Any] = soup.findAll("""div""" , {"""class""": """maincounter-number"""} ) keys += soup.findAll("""span""" , {"""class""": """panel-title"""} ) values += soup.findAll("""div""" , {"""class""": """number-table-main"""} ) return {key.text.strip(): value.text.strip() for key, value in zip(a__ , a__ )} if __name__ == "__main__": print('\033[1m' + 'COVID-19 Status of the World' + '\033[0m\n') for key, value in world_covidaa_stats().items(): print(F"{key}\n{value}\n")
139
def lowerCamelCase( a__ ,a__): return int((input_a, input_a).count(0) == 0) def lowerCamelCase( ): assert and_gate(0 ,0) == 0 assert and_gate(0 ,1) == 0 assert and_gate(1 ,0) == 0 assert and_gate(1 ,1) == 1 if __name__ == "__main__": test_and_gate() print(and_gate(1, 0)) print(and_gate(0, 0)) print(and_gate(0, 1)) print(and_gate(1, 1))
691
0
import copy from ...configuration_utils import PretrainedConfig from ...utils import add_start_docstrings __UpperCamelCase : Optional[Any] = R''' [`RagConfig`] stores the configuration of a *RagModel*. Configuration objects inherit from [`PretrainedConfig`] and can be used to control the model outputs. Read the documentation from [`PretrainedConfig`] for more information. Args: title_sep (`str`, *optional*, defaults to `" / "`): Separator inserted between the title and the text of the retrieved document when calling [`RagRetriever`]. doc_sep (`str`, *optional*, defaults to `" // "`): Separator inserted between the text of the retrieved document and the original input when calling [`RagRetriever`]. n_docs (`int`, *optional*, defaults to 5): Number of documents to retrieve. max_combined_length (`int`, *optional*, defaults to 300): Max length of contextualized input returned by [`~RagRetriever.__call__`]. retrieval_vector_size (`int`, *optional*, defaults to 768): Dimensionality of the document embeddings indexed by [`RagRetriever`]. retrieval_batch_size (`int`, *optional*, defaults to 8): Retrieval batch size, defined as the number of queries issues concurrently to the faiss index encapsulated [`RagRetriever`]. dataset (`str`, *optional*, defaults to `"wiki_dpr"`): A dataset identifier of the indexed dataset in HuggingFace Datasets (list all available datasets and ids using `datasets.list_datasets()`). dataset_split (`str`, *optional*, defaults to `"train"`) Which split of the `dataset` to load. index_name (`str`, *optional*, defaults to `"compressed"`) The index name of the index associated with the `dataset`. One can choose between `"legacy"`, `"exact"` and `"compressed"`. index_path (`str`, *optional*) The path to the serialized faiss index on disk. passages_path (`str`, *optional*): A path to text passages compatible with the faiss index. Required if using [`~models.rag.retrieval_rag.LegacyIndex`] use_dummy_dataset (`bool`, *optional*, defaults to `False`) Whether to load a "dummy" variant of the dataset specified by `dataset`. label_smoothing (`float`, *optional*, defaults to 0.0): Only relevant if `return_loss` is set to `True`. Controls the `epsilon` parameter value for label smoothing in the loss calculation. If set to 0, no label smoothing is performed. do_marginalize (`bool`, *optional*, defaults to `False`): If `True`, the logits are marginalized over all documents by making use of `torch.nn.functional.log_softmax`. reduce_loss (`bool`, *optional*, defaults to `False`): Whether or not to reduce the NLL loss using the `torch.Tensor.sum` operation. do_deduplication (`bool`, *optional*, defaults to `True`): Whether or not to deduplicate the generations from different context documents for a given input. Has to be set to `False` if used while training with distributed backend. exclude_bos_score (`bool`, *optional*, defaults to `False`): Whether or not to disregard the BOS token when computing the loss. output_retrieved(`bool`, *optional*, defaults to `False`): If set to `True`, `retrieved_doc_embeds`, `retrieved_doc_ids`, `context_input_ids` and `context_attention_mask` are returned. See returned tensors for more detail. use_cache (`bool`, *optional*, defaults to `True`): Whether or not the model should return the last key/values attentions (not used by all models). forced_eos_token_id (`int`, *optional*): The id of the token to force as the last generated token when `max_length` is reached. Usually set to `eos_token_id`. ''' @add_start_docstrings(UpperCamelCase__ ) class _UpperCamelCase ( UpperCamelCase__ ): '''simple docstring''' a_ : Optional[int] = "rag" a_ : str = True def __init__( self : Tuple , _lowerCamelCase : List[Any]=None , _lowerCamelCase : Tuple=True , _lowerCamelCase : Optional[Any]=None , _lowerCamelCase : int=None , _lowerCamelCase : List[str]=None , _lowerCamelCase : int=None , _lowerCamelCase : Optional[int]=None , _lowerCamelCase : str=" / " , _lowerCamelCase : Any=" // " , _lowerCamelCase : Optional[Any]=5 , _lowerCamelCase : int=3_0_0 , _lowerCamelCase : Optional[Any]=7_6_8 , _lowerCamelCase : Any=8 , _lowerCamelCase : List[str]="wiki_dpr" , _lowerCamelCase : Dict="train" , _lowerCamelCase : Union[str, Any]="compressed" , _lowerCamelCase : str=None , _lowerCamelCase : Union[str, Any]=None , _lowerCamelCase : int=False , _lowerCamelCase : Any=False , _lowerCamelCase : Any=0.0 , _lowerCamelCase : Any=True , _lowerCamelCase : List[str]=False , _lowerCamelCase : Optional[int]=False , _lowerCamelCase : int=False , _lowerCamelCase : Union[str, Any]=True , _lowerCamelCase : Optional[int]=None , **_lowerCamelCase : List[str] , ): '''simple docstring''' super().__init__( bos_token_id=_a , pad_token_id=_a , eos_token_id=_a , decoder_start_token_id=_a , forced_eos_token_id=_a , is_encoder_decoder=_a , prefix=_a , vocab_size=_a , **_a , ) assert ( "question_encoder" in kwargs and "generator" in kwargs ), "Config has to be initialized with question_encoder and generator config" __lowerCamelCase : Dict = kwargs.pop("""question_encoder""" ) __lowerCamelCase : Optional[Any] = question_encoder_config.pop("""model_type""" ) __lowerCamelCase : Optional[Any] = kwargs.pop("""generator""" ) __lowerCamelCase : Dict = decoder_config.pop("""model_type""" ) from ..auto.configuration_auto import AutoConfig __lowerCamelCase : Optional[Any] = AutoConfig.for_model(_a , **_a ) __lowerCamelCase : Dict = AutoConfig.for_model(_a , **_a ) __lowerCamelCase : Tuple = reduce_loss __lowerCamelCase : Dict = label_smoothing __lowerCamelCase : Any = exclude_bos_score __lowerCamelCase : Optional[int] = do_marginalize __lowerCamelCase : List[str] = title_sep __lowerCamelCase : Tuple = doc_sep __lowerCamelCase : List[Any] = n_docs __lowerCamelCase : Union[str, Any] = max_combined_length __lowerCamelCase : Any = dataset __lowerCamelCase : Tuple = dataset_split __lowerCamelCase : Dict = index_name __lowerCamelCase : Any = retrieval_vector_size __lowerCamelCase : Optional[int] = retrieval_batch_size __lowerCamelCase : List[str] = passages_path __lowerCamelCase : Tuple = index_path __lowerCamelCase : Any = use_dummy_dataset __lowerCamelCase : Dict = output_retrieved __lowerCamelCase : Optional[Any] = do_deduplication __lowerCamelCase : Optional[Any] = use_cache if self.forced_eos_token_id is None: __lowerCamelCase : str = getattr(self.generator , """forced_eos_token_id""" , _a ) @classmethod def _snake_case ( cls : Optional[int] , _lowerCamelCase : PretrainedConfig , _lowerCamelCase : PretrainedConfig , **_lowerCamelCase : Dict ): '''simple docstring''' return cls(question_encoder=question_encoder_config.to_dict() , generator=generator_config.to_dict() , **_a ) def _snake_case ( self : Optional[Any] ): '''simple docstring''' __lowerCamelCase : int = copy.deepcopy(self.__dict__ ) __lowerCamelCase : List[Any] = self.question_encoder.to_dict() __lowerCamelCase : int = self.generator.to_dict() __lowerCamelCase : Union[str, Any] = self.__class__.model_type return output
519
import argparse import os import sys from unittest.mock import patch import pytorch_lightning as pl import timeout_decorator import torch from distillation import SummarizationDistiller, distill_main from finetune import SummarizationModule, main from transformers import MarianMTModel from transformers.file_utils import cached_path from transformers.testing_utils import TestCasePlus, require_torch_gpu, slow from utils import load_json snake_case_ : Optional[int] = '''sshleifer/mar_enro_6_3_student''' class A__ ( UpperCamelCase__ ): def __UpperCamelCase ( self : Any ) -> Any: """simple docstring""" super().setUp() _SCREAMING_SNAKE_CASE =cached_path( '''https://cdn-datasets.huggingface.co/translation/wmt_en_ro-tr40k-va0.5k-te0.5k.tar.gz''' , extract_compressed_file=_a , ) _SCREAMING_SNAKE_CASE =f"{data_cached}/wmt_en_ro-tr40k-va0.5k-te0.5k" @slow @require_torch_gpu def __UpperCamelCase ( self : Optional[Any] ) -> Union[str, Any]: """simple docstring""" MarianMTModel.from_pretrained(_a ) @slow @require_torch_gpu def __UpperCamelCase ( self : str ) -> Dict: """simple docstring""" _SCREAMING_SNAKE_CASE ={ '''$MAX_LEN''': 64, '''$BS''': 64, '''$GAS''': 1, '''$ENRO_DIR''': self.data_dir, '''facebook/mbart-large-cc25''': MARIAN_MODEL, # "val_check_interval=0.25": "val_check_interval=1.0", '''--learning_rate=3e-5''': '''--learning_rate 3e-4''', '''--num_train_epochs 6''': '''--num_train_epochs 1''', } # Clean up bash script _SCREAMING_SNAKE_CASE =(self.test_file_dir / '''train_mbart_cc25_enro.sh''').open().read().split('''finetune.py''' )[1].strip() _SCREAMING_SNAKE_CASE =bash_script.replace('''\\\n''' , '''''' ).strip().replace('''"$@"''' , '''''' ) for k, v in env_vars_to_replace.items(): _SCREAMING_SNAKE_CASE =bash_script.replace(_a , str(_a ) ) _SCREAMING_SNAKE_CASE =self.get_auto_remove_tmp_dir() # bash_script = bash_script.replace("--fp16 ", "") _SCREAMING_SNAKE_CASE =f"\n --output_dir {output_dir}\n --tokenizer_name Helsinki-NLP/opus-mt-en-ro\n --sortish_sampler\n --do_predict\n --gpus 1\n --freeze_encoder\n --n_train 40000\n --n_val 500\n --n_test 500\n --fp16_opt_level O1\n --num_sanity_val_steps 0\n --eval_beams 2\n ".split() # XXX: args.gpus > 1 : handle multi_gpu in the future _SCREAMING_SNAKE_CASE =['''finetune.py'''] + bash_script.split() + args with patch.object(_a , '''argv''' , _a ): _SCREAMING_SNAKE_CASE =argparse.ArgumentParser() _SCREAMING_SNAKE_CASE =pl.Trainer.add_argparse_args(_a ) _SCREAMING_SNAKE_CASE =SummarizationModule.add_model_specific_args(_a , os.getcwd() ) _SCREAMING_SNAKE_CASE =parser.parse_args() _SCREAMING_SNAKE_CASE =main(_a ) # Check metrics _SCREAMING_SNAKE_CASE =load_json(model.metrics_save_path ) _SCREAMING_SNAKE_CASE =metrics['''val'''][0] _SCREAMING_SNAKE_CASE =metrics['''val'''][-1] self.assertEqual(len(metrics['''val'''] ) , (args.max_epochs / args.val_check_interval) ) assert isinstance(last_step_stats[f"val_avg_{model.val_metric}"] , _a ) self.assertGreater(last_step_stats['''val_avg_gen_time'''] , 0.01 ) # model hanging on generate. Maybe bad config was saved. (XXX: old comment/assert?) self.assertLessEqual(last_step_stats['''val_avg_gen_time'''] , 1.0 ) # test learning requirements: # 1. BLEU improves over the course of training by more than 2 pts self.assertGreater(last_step_stats['''val_avg_bleu'''] - first_step_stats['''val_avg_bleu'''] , 2 ) # 2. BLEU finishes above 17 self.assertGreater(last_step_stats['''val_avg_bleu'''] , 17 ) # 3. test BLEU and val BLEU within ~1.1 pt. self.assertLess(abs(metrics['''val'''][-1]['''val_avg_bleu'''] - metrics['''test'''][-1]['''test_avg_bleu'''] ) , 1.1 ) # check lightning ckpt can be loaded and has a reasonable statedict _SCREAMING_SNAKE_CASE =os.listdir(_a ) _SCREAMING_SNAKE_CASE =[x for x in contents if x.endswith('''.ckpt''' )][0] _SCREAMING_SNAKE_CASE =os.path.join(args.output_dir , _a ) _SCREAMING_SNAKE_CASE =torch.load(_a , map_location='''cpu''' ) _SCREAMING_SNAKE_CASE ='''model.model.decoder.layers.0.encoder_attn_layer_norm.weight''' assert expected_key in ckpt["state_dict"] assert ckpt["state_dict"]["model.model.decoder.layers.0.encoder_attn_layer_norm.weight"].dtype == torch.floataa # TODO: turn on args.do_predict when PL bug fixed. if args.do_predict: _SCREAMING_SNAKE_CASE ={os.path.basename(_a ) for p in contents} assert "test_generations.txt" in contents assert "test_results.txt" in contents # assert len(metrics["val"]) == desired_n_evals assert len(metrics['''test'''] ) == 1 class A__ ( UpperCamelCase__ ): @timeout_decorator.timeout(600 ) @slow @require_torch_gpu def __UpperCamelCase ( self : Union[str, Any] ) -> str: """simple docstring""" _SCREAMING_SNAKE_CASE =f"{self.test_file_dir_str}/test_data/wmt_en_ro" _SCREAMING_SNAKE_CASE ={ '''--fp16_opt_level=O1''': '''''', '''$MAX_LEN''': 128, '''$BS''': 16, '''$GAS''': 1, '''$ENRO_DIR''': data_dir, '''$m''': '''sshleifer/student_marian_en_ro_6_1''', '''val_check_interval=0.25''': '''val_check_interval=1.0''', } # Clean up bash script _SCREAMING_SNAKE_CASE =( (self.test_file_dir / '''distil_marian_no_teacher.sh''').open().read().split('''distillation.py''' )[1].strip() ) _SCREAMING_SNAKE_CASE =bash_script.replace('''\\\n''' , '''''' ).strip().replace('''"$@"''' , '''''' ) _SCREAMING_SNAKE_CASE =bash_script.replace('''--fp16 ''' , ''' ''' ) for k, v in env_vars_to_replace.items(): _SCREAMING_SNAKE_CASE =bash_script.replace(_a , str(_a ) ) _SCREAMING_SNAKE_CASE =self.get_auto_remove_tmp_dir() _SCREAMING_SNAKE_CASE =bash_script.replace('''--fp16''' , '''''' ) _SCREAMING_SNAKE_CASE =6 _SCREAMING_SNAKE_CASE =( ['''distillation.py'''] + bash_script.split() + [ f"--output_dir={output_dir}", '''--gpus=1''', '''--learning_rate=1e-3''', f"--num_train_epochs={epochs}", '''--warmup_steps=10''', '''--val_check_interval=1.0''', '''--do_predict''', ] ) with patch.object(_a , '''argv''' , _a ): _SCREAMING_SNAKE_CASE =argparse.ArgumentParser() _SCREAMING_SNAKE_CASE =pl.Trainer.add_argparse_args(_a ) _SCREAMING_SNAKE_CASE =SummarizationDistiller.add_model_specific_args(_a , os.getcwd() ) _SCREAMING_SNAKE_CASE =parser.parse_args() # assert args.gpus == gpus THIS BREAKS for multi_gpu _SCREAMING_SNAKE_CASE =distill_main(_a ) # Check metrics _SCREAMING_SNAKE_CASE =load_json(model.metrics_save_path ) _SCREAMING_SNAKE_CASE =metrics['''val'''][0] _SCREAMING_SNAKE_CASE =metrics['''val'''][-1] assert len(metrics['''val'''] ) >= (args.max_epochs / args.val_check_interval) # +1 accounts for val_sanity_check assert last_step_stats["val_avg_gen_time"] >= 0.01 assert first_step_stats["val_avg_bleu"] < last_step_stats["val_avg_bleu"] # model learned nothing assert 1.0 >= last_step_stats["val_avg_gen_time"] # model hanging on generate. Maybe bad config was saved. assert isinstance(last_step_stats[f"val_avg_{model.val_metric}"] , _a ) # check lightning ckpt can be loaded and has a reasonable statedict _SCREAMING_SNAKE_CASE =os.listdir(_a ) _SCREAMING_SNAKE_CASE =[x for x in contents if x.endswith('''.ckpt''' )][0] _SCREAMING_SNAKE_CASE =os.path.join(args.output_dir , _a ) _SCREAMING_SNAKE_CASE =torch.load(_a , map_location='''cpu''' ) _SCREAMING_SNAKE_CASE ='''model.model.decoder.layers.0.encoder_attn_layer_norm.weight''' assert expected_key in ckpt["state_dict"] assert ckpt["state_dict"]["model.model.decoder.layers.0.encoder_attn_layer_norm.weight"].dtype == torch.floataa # TODO: turn on args.do_predict when PL bug fixed. if args.do_predict: _SCREAMING_SNAKE_CASE ={os.path.basename(_a ) for p in contents} assert "test_generations.txt" in contents assert "test_results.txt" in contents # assert len(metrics["val"]) == desired_n_evals assert len(metrics['''test'''] ) == 1
691
0
from collections import OrderedDict from typing import Mapping from packaging import version from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging from ...utils.backbone_utils import BackboneConfigMixin, get_aligned_output_features_output_indices SCREAMING_SNAKE_CASE : List[Any] = logging.get_logger(__name__) SCREAMING_SNAKE_CASE : str = { '''microsoft/swin-tiny-patch4-window7-224''': ( '''https://huggingface.co/microsoft/swin-tiny-patch4-window7-224/resolve/main/config.json''' ), # See all Swin models at https://huggingface.co/models?filter=swin } class A_ ( UpperCamelCase__ , UpperCamelCase__ ): _SCREAMING_SNAKE_CASE = """swin""" _SCREAMING_SNAKE_CASE = { """num_attention_heads""": """num_heads""", """num_hidden_layers""": """num_layers""", } def __init__( self : Dict , __SCREAMING_SNAKE_CASE : List[str]=2_24 , __SCREAMING_SNAKE_CASE : Optional[int]=4 , __SCREAMING_SNAKE_CASE : Dict=3 , __SCREAMING_SNAKE_CASE : str=96 , __SCREAMING_SNAKE_CASE : Union[str, Any]=[2, 2, 6, 2] , __SCREAMING_SNAKE_CASE : str=[3, 6, 12, 24] , __SCREAMING_SNAKE_CASE : int=7 , __SCREAMING_SNAKE_CASE : str=4.0 , __SCREAMING_SNAKE_CASE : int=True , __SCREAMING_SNAKE_CASE : int=0.0 , __SCREAMING_SNAKE_CASE : Union[str, Any]=0.0 , __SCREAMING_SNAKE_CASE : Dict=0.1 , __SCREAMING_SNAKE_CASE : List[Any]="gelu" , __SCREAMING_SNAKE_CASE : Any=False , __SCREAMING_SNAKE_CASE : List[str]=0.02 , __SCREAMING_SNAKE_CASE : Dict=1E-5 , __SCREAMING_SNAKE_CASE : Optional[int]=32 , __SCREAMING_SNAKE_CASE : Tuple=None , __SCREAMING_SNAKE_CASE : List[str]=None , **__SCREAMING_SNAKE_CASE : Any , ): super().__init__(**_a ) __a = image_size __a = patch_size __a = num_channels __a = embed_dim __a = depths __a = len(_a ) __a = num_heads __a = window_size __a = mlp_ratio __a = qkv_bias __a = hidden_dropout_prob __a = attention_probs_dropout_prob __a = drop_path_rate __a = hidden_act __a = use_absolute_embeddings __a = layer_norm_eps __a = initializer_range __a = encoder_stride # we set the hidden_size attribute in order to make Swin work with VisionEncoderDecoderModel # this indicates the channel dimension after the last stage of the model __a = int(embed_dim * 2 ** (len(_a ) - 1) ) __a = ["stem"] + [f"""stage{idx}""" for idx in range(1 , len(_a ) + 1 )] __a , __a = get_aligned_output_features_output_indices( out_features=_a , out_indices=_a , stage_names=self.stage_names ) class A_ ( UpperCamelCase__ ): _SCREAMING_SNAKE_CASE = version.parse("""1.11""" ) @property def _UpperCAmelCase ( self : Optional[Any] ): return OrderedDict( [ ("pixel_values", {0: "batch", 1: "num_channels", 2: "height", 3: "width"}), ] ) @property def _UpperCAmelCase ( self : Any ): return 1E-4
197
import inspect import os import unittest from dataclasses import dataclass import torch from accelerate import Accelerator, DistributedDataParallelKwargs, GradScalerKwargs from accelerate.state import AcceleratorState from accelerate.test_utils import execute_subprocess_async, require_cuda, require_multi_gpu from accelerate.utils import KwargsHandler @dataclass class A__ ( UpperCamelCase__ ): UpperCAmelCase = 0 UpperCAmelCase = False UpperCAmelCase = 3.0 class A__ ( unittest.TestCase ): def __UpperCamelCase ( self : Optional[Any] ) -> Optional[Any]: """simple docstring""" self.assertDictEqual(MockClass().to_kwargs() , {} ) self.assertDictEqual(MockClass(a=2 ).to_kwargs() , {'''a''': 2} ) self.assertDictEqual(MockClass(a=2 , b=_a ).to_kwargs() , {'''a''': 2, '''b''': True} ) self.assertDictEqual(MockClass(a=2 , c=2.25 ).to_kwargs() , {'''a''': 2, '''c''': 2.25} ) @require_cuda def __UpperCamelCase ( self : Optional[Any] ) -> Dict: """simple docstring""" _SCREAMING_SNAKE_CASE =GradScalerKwargs(init_scale=1024 , growth_factor=2 ) AcceleratorState._reset_state() _SCREAMING_SNAKE_CASE =Accelerator(mixed_precision='''fp16''' , kwargs_handlers=[scaler_handler] ) print(accelerator.use_fpaa ) _SCREAMING_SNAKE_CASE =accelerator.scaler # Check the kwargs have been applied self.assertEqual(scaler._init_scale , 10_24.0 ) self.assertEqual(scaler._growth_factor , 2.0 ) # Check the other values are at the default self.assertEqual(scaler._backoff_factor , 0.5 ) self.assertEqual(scaler._growth_interval , 2000 ) self.assertEqual(scaler._enabled , _a ) @require_multi_gpu def __UpperCamelCase ( self : str ) -> Tuple: """simple docstring""" _SCREAMING_SNAKE_CASE =['''torchrun''', f"--nproc_per_node={torch.cuda.device_count()}", inspect.getfile(self.__class__ )] execute_subprocess_async(_a , env=os.environ.copy() ) if __name__ == "__main__": snake_case_ : Optional[Any] = DistributedDataParallelKwargs(bucket_cap_mb=15, find_unused_parameters=True) snake_case_ : List[str] = Accelerator(kwargs_handlers=[ddp_scaler]) snake_case_ : Dict = torch.nn.Linear(1_00, 2_00) snake_case_ : List[Any] = accelerator.prepare(model) # Check the values changed in kwargs snake_case_ : Dict = '''''' snake_case_ : str = model.bucket_bytes_cap // (10_24 * 10_24) if observed_bucket_cap_map != 15: error_msg += f"Kwargs badly passed, should have `15` but found {observed_bucket_cap_map}.\n" if model.find_unused_parameters is not True: error_msg += f"Kwargs badly passed, should have `True` but found {model.find_unused_parameters}.\n" # Check the values of the defaults if model.dim != 0: error_msg += f"Default value not respected, should have `0` but found {model.dim}.\n" if model.broadcast_buffers is not True: error_msg += f"Default value not respected, should have `True` but found {model.broadcast_buffers}.\n" if model.gradient_as_bucket_view is not False: error_msg += f"Default value not respected, should have `False` but found {model.gradient_as_bucket_view}.\n" # Raise error at the end to make sure we don't stop at the first failure. if len(error_msg) > 0: raise ValueError(error_msg)
691
0
import unittest import numpy as np import torch from diffusers import PNDMPipeline, PNDMScheduler, UNetaDModel from diffusers.utils.testing_utils import enable_full_determinism, require_torch, slow, torch_device enable_full_determinism() class lowercase_ ( unittest.TestCase ): '''simple docstring''' @property def __lowerCAmelCase ( self : str ) ->Optional[int]: """simple docstring""" torch.manual_seed(0 ) a = 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''') , ) return model def __lowerCAmelCase ( self : Optional[Any] ) ->List[str]: """simple docstring""" a = self.dummy_uncond_unet a = PNDMScheduler() a = PNDMPipeline(unet=_a , scheduler=_a ) pndm.to(_a ) pndm.set_progress_bar_config(disable=_a ) a = torch.manual_seed(0 ) a = pndm(generator=_a , num_inference_steps=20 , output_type='''numpy''' ).images a = torch.manual_seed(0 ) a = pndm(generator=_a , num_inference_steps=20 , output_type='''numpy''' , return_dict=_a )[0] a = image[0, -3:, -3:, -1] a = image_from_tuple[0, -3:, -3:, -1] assert image.shape == (1, 32, 32, 3) a = np.array([1.0, 1.0, 0.0, 1.0, 0.0, 1.0, 0.0, 0.0, 0.0] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-2 assert np.abs(image_from_tuple_slice.flatten() - expected_slice ).max() < 1e-2 @slow @require_torch class lowercase_ ( unittest.TestCase ): '''simple docstring''' def __lowerCAmelCase ( self : Any ) ->List[str]: """simple docstring""" a = '''google/ddpm-cifar10-32''' a = UNetaDModel.from_pretrained(_a ) a = PNDMScheduler() a = PNDMPipeline(unet=_a , scheduler=_a ) pndm.to(_a ) pndm.set_progress_bar_config(disable=_a ) a = torch.manual_seed(0 ) a = pndm(generator=_a , output_type='''numpy''' ).images a = image[0, -3:, -3:, -1] assert image.shape == (1, 32, 32, 3) a = np.array([0.1564, 0.14645, 0.1406, 0.14715, 0.12425, 0.14045, 0.13115, 0.12175, 0.125] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-2
117
class A__ : def __init__( self : List[str] ) -> List[str]: """simple docstring""" _SCREAMING_SNAKE_CASE =0 _SCREAMING_SNAKE_CASE =0 _SCREAMING_SNAKE_CASE ={} def __UpperCamelCase ( self : Any , _a : Union[str, Any] ) -> Optional[Any]: """simple docstring""" if vertex not in self.adjacency: _SCREAMING_SNAKE_CASE ={} self.num_vertices += 1 def __UpperCamelCase ( self : Optional[int] , _a : Tuple , _a : Tuple , _a : Dict ) -> Union[str, Any]: """simple docstring""" self.add_vertex(_a ) self.add_vertex(_a ) if head == tail: return _SCREAMING_SNAKE_CASE =weight _SCREAMING_SNAKE_CASE =weight def __UpperCamelCase ( self : Dict ) -> int: """simple docstring""" _SCREAMING_SNAKE_CASE =self.get_edges() for edge in edges: _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE =edge edges.remove((tail, head, weight) ) for i in range(len(_a ) ): _SCREAMING_SNAKE_CASE =list(edges[i] ) edges.sort(key=lambda _a : e[2] ) for i in range(len(_a ) - 1 ): if edges[i][2] >= edges[i + 1][2]: _SCREAMING_SNAKE_CASE =edges[i][2] + 1 for edge in edges: _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE =edge _SCREAMING_SNAKE_CASE =weight _SCREAMING_SNAKE_CASE =weight def __str__( self : str ) -> List[str]: """simple docstring""" _SCREAMING_SNAKE_CASE ='''''' for tail in self.adjacency: for head in self.adjacency[tail]: _SCREAMING_SNAKE_CASE =self.adjacency[head][tail] string += f"{head} -> {tail} == {weight}\n" return string.rstrip('''\n''' ) def __UpperCamelCase ( self : int ) -> Optional[Any]: """simple docstring""" _SCREAMING_SNAKE_CASE =[] for tail in self.adjacency: for head in self.adjacency[tail]: output.append((tail, head, self.adjacency[head][tail]) ) return output def __UpperCamelCase ( self : Any ) -> Any: """simple docstring""" return self.adjacency.keys() @staticmethod def __UpperCamelCase ( _a : List[str]=None , _a : Optional[int]=None ) -> Optional[Any]: """simple docstring""" _SCREAMING_SNAKE_CASE =Graph() if vertices is None: _SCREAMING_SNAKE_CASE =[] if edges is None: _SCREAMING_SNAKE_CASE =[] for vertex in vertices: g.add_vertex(_a ) for edge in edges: g.add_edge(*_a ) return g class A__ : def __init__( self : List[Any] ) -> Union[str, Any]: """simple docstring""" _SCREAMING_SNAKE_CASE ={} _SCREAMING_SNAKE_CASE ={} def __len__( self : Optional[int] ) -> Tuple: """simple docstring""" return len(self.parent ) def __UpperCamelCase ( self : Dict , _a : Optional[Any] ) -> int: """simple docstring""" if item in self.parent: return self.find(_a ) _SCREAMING_SNAKE_CASE =item _SCREAMING_SNAKE_CASE =0 return item def __UpperCamelCase ( self : str , _a : Tuple ) -> Union[str, Any]: """simple docstring""" if item not in self.parent: return self.make_set(_a ) if item != self.parent[item]: _SCREAMING_SNAKE_CASE =self.find(self.parent[item] ) return self.parent[item] def __UpperCamelCase ( self : Dict , _a : Optional[int] , _a : List[Any] ) -> List[str]: """simple docstring""" _SCREAMING_SNAKE_CASE =self.find(_a ) _SCREAMING_SNAKE_CASE =self.find(_a ) if roota == roota: return roota if self.rank[roota] > self.rank[roota]: _SCREAMING_SNAKE_CASE =roota return roota if self.rank[roota] < self.rank[roota]: _SCREAMING_SNAKE_CASE =roota return roota if self.rank[roota] == self.rank[roota]: self.rank[roota] += 1 _SCREAMING_SNAKE_CASE =roota return roota return None @staticmethod def __UpperCamelCase ( _a : int ) -> Union[str, Any]: """simple docstring""" _SCREAMING_SNAKE_CASE =graph.num_vertices _SCREAMING_SNAKE_CASE =Graph.UnionFind() _SCREAMING_SNAKE_CASE =[] while num_components > 1: _SCREAMING_SNAKE_CASE ={} for vertex in graph.get_vertices(): _SCREAMING_SNAKE_CASE =-1 _SCREAMING_SNAKE_CASE =graph.get_edges() for edge in edges: _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE =edge edges.remove((tail, head, weight) ) for edge in edges: _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE =edge _SCREAMING_SNAKE_CASE =union_find.find(_a ) _SCREAMING_SNAKE_CASE =union_find.find(_a ) if seta != seta: if cheap_edge[seta] == -1 or cheap_edge[seta][2] > weight: _SCREAMING_SNAKE_CASE =[head, tail, weight] if cheap_edge[seta] == -1 or cheap_edge[seta][2] > weight: _SCREAMING_SNAKE_CASE =[head, tail, weight] for vertex in cheap_edge: if cheap_edge[vertex] != -1: _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE =cheap_edge[vertex] if union_find.find(_a ) != union_find.find(_a ): union_find.union(_a , _a ) mst_edges.append(cheap_edge[vertex] ) _SCREAMING_SNAKE_CASE =num_components - 1 _SCREAMING_SNAKE_CASE =Graph.build(edges=_a ) return mst
691
0
class UpperCamelCase: def __init__( self : List[str] ) -> List[str]: '''simple docstring''' __snake_case = 0 __snake_case = 0 __snake_case = {} def SCREAMING_SNAKE_CASE_ ( self : Any , SCREAMING_SNAKE_CASE : Union[str, Any] ) -> Optional[Any]: '''simple docstring''' if vertex not in self.adjacency: __snake_case = {} self.num_vertices += 1 def SCREAMING_SNAKE_CASE_ ( self : Optional[int] , SCREAMING_SNAKE_CASE : Tuple , SCREAMING_SNAKE_CASE : Tuple , SCREAMING_SNAKE_CASE : Dict ) -> Union[str, Any]: '''simple docstring''' self.add_vertex(_a ) self.add_vertex(_a ) if head == tail: return __snake_case = weight __snake_case = weight def SCREAMING_SNAKE_CASE_ ( self : Dict ) -> int: '''simple docstring''' __snake_case = self.get_edges() for edge in edges: __snake_case , __snake_case , __snake_case = edge edges.remove((tail, head, weight) ) for i in range(len(_a ) ): __snake_case = list(edges[i] ) edges.sort(key=lambda SCREAMING_SNAKE_CASE : e[2] ) for i in range(len(_a ) - 1 ): if edges[i][2] >= edges[i + 1][2]: __snake_case = edges[i][2] + 1 for edge in edges: __snake_case , __snake_case , __snake_case = edge __snake_case = weight __snake_case = weight def __str__( self : str ) -> List[str]: '''simple docstring''' __snake_case = "" for tail in self.adjacency: for head in self.adjacency[tail]: __snake_case = self.adjacency[head][tail] string += f'''{head} -> {tail} == {weight}\n''' return string.rstrip("\n" ) def SCREAMING_SNAKE_CASE_ ( self : int ) -> Optional[Any]: '''simple docstring''' __snake_case = [] for tail in self.adjacency: for head in self.adjacency[tail]: output.append((tail, head, self.adjacency[head][tail]) ) return output def SCREAMING_SNAKE_CASE_ ( self : Any ) -> Any: '''simple docstring''' return self.adjacency.keys() @staticmethod def SCREAMING_SNAKE_CASE_ ( SCREAMING_SNAKE_CASE : List[str]=None , SCREAMING_SNAKE_CASE : Optional[int]=None ) -> Optional[Any]: '''simple docstring''' __snake_case = Graph() if vertices is None: __snake_case = [] if edges is None: __snake_case = [] for vertex in vertices: g.add_vertex(_a ) for edge in edges: g.add_edge(*_a ) return g class UpperCamelCase: def __init__( self : List[Any] ) -> Union[str, Any]: '''simple docstring''' __snake_case = {} __snake_case = {} def __len__( self : Optional[int] ) -> Tuple: '''simple docstring''' return len(self.parent ) def SCREAMING_SNAKE_CASE_ ( self : Dict , SCREAMING_SNAKE_CASE : Optional[Any] ) -> int: '''simple docstring''' if item in self.parent: return self.find(_a ) __snake_case = item __snake_case = 0 return item def SCREAMING_SNAKE_CASE_ ( self : str , SCREAMING_SNAKE_CASE : Tuple ) -> Union[str, Any]: '''simple docstring''' if item not in self.parent: return self.make_set(_a ) if item != self.parent[item]: __snake_case = self.find(self.parent[item] ) return self.parent[item] def SCREAMING_SNAKE_CASE_ ( self : Dict , SCREAMING_SNAKE_CASE : Optional[int] , SCREAMING_SNAKE_CASE : List[Any] ) -> List[str]: '''simple docstring''' __snake_case = self.find(_a ) __snake_case = self.find(_a ) if roota == roota: return roota if self.rank[roota] > self.rank[roota]: __snake_case = roota return roota if self.rank[roota] < self.rank[roota]: __snake_case = roota return roota if self.rank[roota] == self.rank[roota]: self.rank[roota] += 1 __snake_case = roota return roota return None @staticmethod def SCREAMING_SNAKE_CASE_ ( SCREAMING_SNAKE_CASE : int ) -> Union[str, Any]: '''simple docstring''' __snake_case = graph.num_vertices __snake_case = Graph.UnionFind() __snake_case = [] while num_components > 1: __snake_case = {} for vertex in graph.get_vertices(): __snake_case = -1 __snake_case = graph.get_edges() for edge in edges: __snake_case , __snake_case , __snake_case = edge edges.remove((tail, head, weight) ) for edge in edges: __snake_case , __snake_case , __snake_case = edge __snake_case = union_find.find(_a ) __snake_case = union_find.find(_a ) if seta != seta: if cheap_edge[seta] == -1 or cheap_edge[seta][2] > weight: __snake_case = [head, tail, weight] if cheap_edge[seta] == -1 or cheap_edge[seta][2] > weight: __snake_case = [head, tail, weight] for vertex in cheap_edge: if cheap_edge[vertex] != -1: __snake_case , __snake_case , __snake_case = cheap_edge[vertex] if union_find.find(_a ) != union_find.find(_a ): union_find.union(_a , _a ) mst_edges.append(cheap_edge[vertex] ) __snake_case = num_components - 1 __snake_case = Graph.build(edges=_a ) return mst
371
import logging import os from dataclasses import dataclass, field from typing import Dict, Optional import numpy as np from utils_multiple_choice import MultipleChoiceDataset, Split, processors import transformers from transformers import ( AutoConfig, AutoModelForMultipleChoice, AutoTokenizer, DataCollatorWithPadding, EvalPrediction, HfArgumentParser, Trainer, TrainingArguments, set_seed, ) from transformers.trainer_utils import is_main_process snake_case_ : str = logging.getLogger(__name__) def lowerCamelCase( a__ ,a__): return (preds == labels).mean() @dataclass class A__ : UpperCAmelCase = field( metadata={"help": "Path to pretrained model or model identifier from huggingface.co/models"} ) UpperCAmelCase = field( default=UpperCamelCase__ , metadata={"help": "Pretrained config name or path if not the same as model_name"} ) UpperCAmelCase = field( default=UpperCamelCase__ , metadata={"help": "Pretrained tokenizer name or path if not the same as model_name"} ) UpperCAmelCase = field( default=UpperCamelCase__ , metadata={"help": "Where do you want to store the pretrained models downloaded from huggingface.co"} , ) @dataclass class A__ : UpperCAmelCase = field(metadata={"help": "The name of the task to train on: " + ", ".join(processors.keys() )} ) UpperCAmelCase = field(metadata={"help": "Should contain the data files for the task."} ) UpperCAmelCase = 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." ) } , ) UpperCAmelCase = field( default=UpperCamelCase__ , metadata={"help": "Overwrite the cached training and evaluation sets"} ) def lowerCamelCase( ): # See all possible arguments in src/transformers/training_args.py # or by passing the --help flag to this script. # We now keep distinct sets of args, for a cleaner separation of concerns. _SCREAMING_SNAKE_CASE =HfArgumentParser((ModelArguments, DataTrainingArguments, TrainingArguments)) _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE =parser.parse_args_into_dataclasses() if ( os.path.exists(training_args.output_dir) and os.listdir(training_args.output_dir) and training_args.do_train and not training_args.overwrite_output_dir ): raise ValueError( f"Output directory ({training_args.output_dir}) already exists and is not empty. Use" ''' --overwrite_output_dir to overcome.''') # Setup logging logging.basicConfig( format='''%(asctime)s - %(levelname)s - %(name)s - %(message)s''' ,datefmt='''%m/%d/%Y %H:%M:%S''' ,level=logging.INFO if training_args.local_rank in [-1, 0] else logging.WARN ,) logger.warning( '''Process rank: %s, device: %s, n_gpu: %s, distributed training: %s, 16-bits training: %s''' ,training_args.local_rank ,training_args.device ,training_args.n_gpu ,bool(training_args.local_rank != -1) ,training_args.fpaa ,) # Set the verbosity to info of the Transformers logger (on main process only): if is_main_process(training_args.local_rank): transformers.utils.logging.set_verbosity_info() transformers.utils.logging.enable_default_handler() transformers.utils.logging.enable_explicit_format() logger.info('''Training/evaluation parameters %s''' ,a__) # Set seed set_seed(training_args.seed) try: _SCREAMING_SNAKE_CASE =processors[data_args.task_name]() _SCREAMING_SNAKE_CASE =processor.get_labels() _SCREAMING_SNAKE_CASE =len(a__) except KeyError: raise ValueError('''Task not found: %s''' % (data_args.task_name)) # Load pretrained model and tokenizer # # Distributed training: # The .from_pretrained methods guarantee that only one local process can concurrently # download model & vocab. _SCREAMING_SNAKE_CASE =AutoConfig.from_pretrained( model_args.config_name if model_args.config_name else model_args.model_name_or_path ,num_labels=a__ ,finetuning_task=data_args.task_name ,cache_dir=model_args.cache_dir ,) _SCREAMING_SNAKE_CASE =AutoTokenizer.from_pretrained( model_args.tokenizer_name if model_args.tokenizer_name else model_args.model_name_or_path ,cache_dir=model_args.cache_dir ,) _SCREAMING_SNAKE_CASE =AutoModelForMultipleChoice.from_pretrained( model_args.model_name_or_path ,from_tf=bool('''.ckpt''' in model_args.model_name_or_path) ,config=a__ ,cache_dir=model_args.cache_dir ,) # Get datasets _SCREAMING_SNAKE_CASE =( MultipleChoiceDataset( data_dir=data_args.data_dir ,tokenizer=a__ ,task=data_args.task_name ,max_seq_length=data_args.max_seq_length ,overwrite_cache=data_args.overwrite_cache ,mode=Split.train ,) if training_args.do_train else None ) _SCREAMING_SNAKE_CASE =( MultipleChoiceDataset( data_dir=data_args.data_dir ,tokenizer=a__ ,task=data_args.task_name ,max_seq_length=data_args.max_seq_length ,overwrite_cache=data_args.overwrite_cache ,mode=Split.dev ,) if training_args.do_eval else None ) def compute_metrics(a__) -> Dict: _SCREAMING_SNAKE_CASE =np.argmax(p.predictions ,axis=1) return {"acc": simple_accuracy(a__ ,p.label_ids)} # Data collator _SCREAMING_SNAKE_CASE =DataCollatorWithPadding(a__ ,pad_to_multiple_of=8) if training_args.fpaa else None # Initialize our Trainer _SCREAMING_SNAKE_CASE =Trainer( model=a__ ,args=a__ ,train_dataset=a__ ,eval_dataset=a__ ,compute_metrics=a__ ,data_collator=a__ ,) # Training if training_args.do_train: trainer.train( model_path=model_args.model_name_or_path if os.path.isdir(model_args.model_name_or_path) else None) trainer.save_model() # For convenience, we also re-save the tokenizer to the same directory, # so that you can share your model easily on huggingface.co/models =) if trainer.is_world_master(): tokenizer.save_pretrained(training_args.output_dir) # Evaluation _SCREAMING_SNAKE_CASE ={} if training_args.do_eval: logger.info('''*** Evaluate ***''') _SCREAMING_SNAKE_CASE =trainer.evaluate() _SCREAMING_SNAKE_CASE =os.path.join(training_args.output_dir ,'''eval_results.txt''') if trainer.is_world_master(): with open(a__ ,'''w''') as writer: logger.info('''***** Eval results *****''') for key, value in result.items(): logger.info(''' %s = %s''' ,a__ ,a__) writer.write('''%s = %s\n''' % (key, value)) results.update(a__) return results def lowerCamelCase( a__): # For xla_spawn (TPUs) main() if __name__ == "__main__": main()
691
0
'''simple docstring''' from __future__ import annotations def _lowerCAmelCase ( __magic_name__ : List[Any] ) -> Any: create_state_space_tree(a__ , [] , 0 , [0 for i in range(len(a__ ) )] ) def _lowerCAmelCase ( __magic_name__ : Union[str, Any] , __magic_name__ : str , __magic_name__ : Tuple , __magic_name__ : Dict , ) -> Tuple: if index == len(a__ ): print(a__ ) return for i in range(len(a__ ) ): if not index_used[i]: current_sequence.append(sequence[i] ) lowercase : Any =True create_state_space_tree(a__ , a__ , index + 1 , a__ ) current_sequence.pop() lowercase : List[Any] =False UpperCamelCase_ = [3, 1, 2, 4] generate_all_permutations(sequence) UpperCamelCase_ = ["A", "B", "C"] generate_all_permutations(sequence_a)
92
def lowerCamelCase( a__ ,a__ ,a__): if n == 0: return 1 elif n % 2 == 1: return (binary_exponentiation(a__ ,n - 1 ,a__) * a) % mod else: _SCREAMING_SNAKE_CASE =binary_exponentiation(a__ ,n / 2 ,a__) return (b * b) % mod # a prime number snake_case_ : Union[str, Any] = 7_01 snake_case_ : int = 10_00_00_00_00 snake_case_ : str = 10 # using binary exponentiation function, O(log(p)): print((a / b) % p == (a * binary_exponentiation(b, p - 2, p)) % p) print((a / b) % p == (a * b ** (p - 2)) % p)
691
0
'''simple docstring''' import socket def UpperCAmelCase ( ): SCREAMING_SNAKE_CASE : Optional[int] = socket.socket(socket.AF_INET , socket.SOCK_STREAM ) SCREAMING_SNAKE_CASE : List[str] = socket.gethostname() SCREAMING_SNAKE_CASE : Any = 12312 sock.connect((host, port) ) sock.send(B'''Hello server!''' ) with open('''Received_file''' , '''wb''' ) as out_file: print('''File opened''' ) print('''Receiving data...''' ) while True: SCREAMING_SNAKE_CASE : str = sock.recv(1024 ) if not data: break out_file.write(a__ ) print('''Successfully received the file''' ) sock.close() print('''Connection closed''' ) if __name__ == "__main__": main()
527
import inspect import unittest from transformers import BitConfig from transformers.testing_utils import require_torch, require_vision, slow, torch_device from transformers.utils import cached_property, is_torch_available, is_vision_available from ...test_backbone_common import BackboneTesterMixin from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from torch import nn from transformers import BitBackbone, BitForImageClassification, BitImageProcessor, BitModel from transformers.models.bit.modeling_bit import BIT_PRETRAINED_MODEL_ARCHIVE_LIST if is_vision_available(): from PIL import Image class A__ : def __init__( self : Optional[Any] , _a : int , _a : Optional[Any]=3 , _a : Tuple=32 , _a : Any=3 , _a : Union[str, Any]=10 , _a : Optional[int]=[8, 16, 32, 64] , _a : Union[str, Any]=[1, 1, 2, 1] , _a : Optional[Any]=True , _a : int=True , _a : Tuple="relu" , _a : Optional[Any]=3 , _a : str=None , _a : List[Any]=["stage2", "stage3", "stage4"] , _a : Union[str, Any]=[2, 3, 4] , _a : Dict=1 , ) -> Dict: """simple docstring""" _SCREAMING_SNAKE_CASE =parent _SCREAMING_SNAKE_CASE =batch_size _SCREAMING_SNAKE_CASE =image_size _SCREAMING_SNAKE_CASE =num_channels _SCREAMING_SNAKE_CASE =embeddings_size _SCREAMING_SNAKE_CASE =hidden_sizes _SCREAMING_SNAKE_CASE =depths _SCREAMING_SNAKE_CASE =is_training _SCREAMING_SNAKE_CASE =use_labels _SCREAMING_SNAKE_CASE =hidden_act _SCREAMING_SNAKE_CASE =num_labels _SCREAMING_SNAKE_CASE =scope _SCREAMING_SNAKE_CASE =len(_a ) _SCREAMING_SNAKE_CASE =out_features _SCREAMING_SNAKE_CASE =out_indices _SCREAMING_SNAKE_CASE =num_groups def __UpperCamelCase ( self : Optional[Any] ) -> str: """simple docstring""" _SCREAMING_SNAKE_CASE =floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) _SCREAMING_SNAKE_CASE =None if self.use_labels: _SCREAMING_SNAKE_CASE =ids_tensor([self.batch_size] , self.num_labels ) _SCREAMING_SNAKE_CASE =self.get_config() return config, pixel_values, labels def __UpperCamelCase ( self : Any ) -> Union[str, Any]: """simple docstring""" return BitConfig( num_channels=self.num_channels , embeddings_size=self.embeddings_size , hidden_sizes=self.hidden_sizes , depths=self.depths , hidden_act=self.hidden_act , num_labels=self.num_labels , out_features=self.out_features , out_indices=self.out_indices , num_groups=self.num_groups , ) def __UpperCamelCase ( self : Optional[Any] , _a : Dict , _a : str , _a : Dict ) -> List[Any]: """simple docstring""" _SCREAMING_SNAKE_CASE =BitModel(config=_a ) model.to(_a ) model.eval() _SCREAMING_SNAKE_CASE =model(_a ) self.parent.assertEqual( result.last_hidden_state.shape , (self.batch_size, self.hidden_sizes[-1], self.image_size // 32, self.image_size // 32) , ) def __UpperCamelCase ( self : Union[str, Any] , _a : Union[str, Any] , _a : Optional[Any] , _a : Optional[Any] ) -> Union[str, Any]: """simple docstring""" _SCREAMING_SNAKE_CASE =self.num_labels _SCREAMING_SNAKE_CASE =BitForImageClassification(_a ) model.to(_a ) model.eval() _SCREAMING_SNAKE_CASE =model(_a , labels=_a ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_labels) ) def __UpperCamelCase ( self : List[str] , _a : Any , _a : str , _a : List[str] ) -> Optional[int]: """simple docstring""" _SCREAMING_SNAKE_CASE =BitBackbone(config=_a ) model.to(_a ) model.eval() _SCREAMING_SNAKE_CASE =model(_a ) # verify feature maps self.parent.assertEqual(len(result.feature_maps ) , len(config.out_features ) ) self.parent.assertListEqual(list(result.feature_maps[0].shape ) , [self.batch_size, self.hidden_sizes[1], 4, 4] ) # verify channels self.parent.assertEqual(len(model.channels ) , len(config.out_features ) ) self.parent.assertListEqual(model.channels , config.hidden_sizes[1:] ) # verify backbone works with out_features=None _SCREAMING_SNAKE_CASE =None _SCREAMING_SNAKE_CASE =BitBackbone(config=_a ) model.to(_a ) model.eval() _SCREAMING_SNAKE_CASE =model(_a ) # verify feature maps self.parent.assertEqual(len(result.feature_maps ) , 1 ) self.parent.assertListEqual(list(result.feature_maps[0].shape ) , [self.batch_size, self.hidden_sizes[-1], 1, 1] ) # verify channels self.parent.assertEqual(len(model.channels ) , 1 ) self.parent.assertListEqual(model.channels , [config.hidden_sizes[-1]] ) def __UpperCamelCase ( self : Optional[Any] ) -> Dict: """simple docstring""" _SCREAMING_SNAKE_CASE =self.prepare_config_and_inputs() _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE =config_and_inputs _SCREAMING_SNAKE_CASE ={'''pixel_values''': pixel_values} return config, inputs_dict @require_torch class A__ ( UpperCamelCase__ , UpperCamelCase__ , unittest.TestCase ): UpperCAmelCase = (BitModel, BitForImageClassification, BitBackbone) if is_torch_available() else () UpperCAmelCase = ( {"feature-extraction": BitModel, "image-classification": BitForImageClassification} if is_torch_available() else {} ) UpperCAmelCase = False UpperCAmelCase = False UpperCAmelCase = False UpperCAmelCase = False UpperCAmelCase = False def __UpperCamelCase ( self : Union[str, Any] ) -> str: """simple docstring""" _SCREAMING_SNAKE_CASE =BitModelTester(self ) _SCREAMING_SNAKE_CASE =ConfigTester(self , config_class=_a , has_text_modality=_a ) def __UpperCamelCase ( self : Union[str, Any] ) -> int: """simple docstring""" self.create_and_test_config_common_properties() self.config_tester.create_and_test_config_to_json_string() self.config_tester.create_and_test_config_to_json_file() self.config_tester.create_and_test_config_from_and_save_pretrained() self.config_tester.create_and_test_config_with_num_labels() self.config_tester.check_config_can_be_init_without_params() self.config_tester.check_config_arguments_init() def __UpperCamelCase ( self : List[str] ) -> Optional[int]: """simple docstring""" return @unittest.skip(reason='''Bit does not output attentions''' ) def __UpperCamelCase ( self : Optional[int] ) -> List[str]: """simple docstring""" pass @unittest.skip(reason='''Bit does not use inputs_embeds''' ) def __UpperCamelCase ( self : str ) -> Optional[Any]: """simple docstring""" pass @unittest.skip(reason='''Bit does not support input and output embeddings''' ) def __UpperCamelCase ( self : Optional[int] ) -> Union[str, Any]: """simple docstring""" pass def __UpperCamelCase ( self : Tuple ) -> Optional[int]: """simple docstring""" _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE =self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: _SCREAMING_SNAKE_CASE =model_class(_a ) _SCREAMING_SNAKE_CASE =inspect.signature(model.forward ) # signature.parameters is an OrderedDict => so arg_names order is deterministic _SCREAMING_SNAKE_CASE =[*signature.parameters.keys()] _SCREAMING_SNAKE_CASE =['''pixel_values'''] self.assertListEqual(arg_names[:1] , _a ) def __UpperCamelCase ( self : int ) -> Optional[int]: """simple docstring""" _SCREAMING_SNAKE_CASE =self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*_a ) def __UpperCamelCase ( self : Optional[Any] ) -> Dict: """simple docstring""" _SCREAMING_SNAKE_CASE =self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_backbone(*_a ) def __UpperCamelCase ( self : Tuple ) -> Optional[int]: """simple docstring""" _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE =self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: _SCREAMING_SNAKE_CASE =model_class(config=_a ) for name, module in model.named_modules(): if isinstance(_a , (nn.BatchNormad, nn.GroupNorm) ): self.assertTrue( torch.all(module.weight == 1 ) , msg=f"Parameter {name} of model {model_class} seems not properly initialized" , ) self.assertTrue( torch.all(module.bias == 0 ) , msg=f"Parameter {name} of model {model_class} seems not properly initialized" , ) def __UpperCamelCase ( self : Tuple ) -> Optional[int]: """simple docstring""" def check_hidden_states_output(_a : Any , _a : Optional[int] , _a : Tuple ): _SCREAMING_SNAKE_CASE =model_class(_a ) model.to(_a ) model.eval() with torch.no_grad(): _SCREAMING_SNAKE_CASE =model(**self._prepare_for_class(_a , _a ) ) _SCREAMING_SNAKE_CASE =outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states _SCREAMING_SNAKE_CASE =self.model_tester.num_stages self.assertEqual(len(_a ) , expected_num_stages + 1 ) # Bit's feature maps are of shape (batch_size, num_channels, height, width) self.assertListEqual( list(hidden_states[0].shape[-2:] ) , [self.model_tester.image_size // 4, self.model_tester.image_size // 4] , ) _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE =self.model_tester.prepare_config_and_inputs_for_common() _SCREAMING_SNAKE_CASE =['''preactivation''', '''bottleneck'''] for model_class in self.all_model_classes: for layer_type in layers_type: _SCREAMING_SNAKE_CASE =layer_type _SCREAMING_SNAKE_CASE =True check_hidden_states_output(_a , _a , _a ) # check that output_hidden_states also work using config del inputs_dict["output_hidden_states"] _SCREAMING_SNAKE_CASE =True check_hidden_states_output(_a , _a , _a ) @unittest.skip(reason='''Bit does not use feedforward chunking''' ) def __UpperCamelCase ( self : Optional[int] ) -> Dict: """simple docstring""" pass def __UpperCamelCase ( self : Union[str, Any] ) -> Any: """simple docstring""" _SCREAMING_SNAKE_CASE =self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_image_classification(*_a ) @slow def __UpperCamelCase ( self : int ) -> Tuple: """simple docstring""" for model_name in BIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: _SCREAMING_SNAKE_CASE =BitModel.from_pretrained(_a ) self.assertIsNotNone(_a ) def lowerCamelCase( ): _SCREAMING_SNAKE_CASE =Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png''') return image @require_torch @require_vision class A__ ( unittest.TestCase ): @cached_property def __UpperCamelCase ( self : str ) -> Optional[Any]: """simple docstring""" return ( BitImageProcessor.from_pretrained(BIT_PRETRAINED_MODEL_ARCHIVE_LIST[0] ) if is_vision_available() else None ) @slow def __UpperCamelCase ( self : List[Any] ) -> int: """simple docstring""" _SCREAMING_SNAKE_CASE =BitForImageClassification.from_pretrained(BIT_PRETRAINED_MODEL_ARCHIVE_LIST[0] ).to(_a ) _SCREAMING_SNAKE_CASE =self.default_image_processor _SCREAMING_SNAKE_CASE =prepare_img() _SCREAMING_SNAKE_CASE =image_processor(images=_a , return_tensors='''pt''' ).to(_a ) # forward pass with torch.no_grad(): _SCREAMING_SNAKE_CASE =model(**_a ) # verify the logits _SCREAMING_SNAKE_CASE =torch.Size((1, 1000) ) self.assertEqual(outputs.logits.shape , _a ) _SCREAMING_SNAKE_CASE =torch.tensor([[-0.65_26, -0.52_63, -1.43_98]] ).to(_a ) self.assertTrue(torch.allclose(outputs.logits[0, :3] , _a , atol=1E-4 ) ) @require_torch class A__ ( UpperCamelCase__ , unittest.TestCase ): UpperCAmelCase = (BitBackbone,) if is_torch_available() else () UpperCAmelCase = BitConfig UpperCAmelCase = False def __UpperCamelCase ( self : List[str] ) -> Optional[Any]: """simple docstring""" _SCREAMING_SNAKE_CASE =BitModelTester(self )
691
0
from __future__ import annotations import unittest from transformers import AutoTokenizer, PegasusConfig, is_tf_available from transformers.testing_utils import require_sentencepiece, require_tf, require_tokenizers, slow from transformers.utils import cached_property from ...test_configuration_common import ConfigTester from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_tf_available(): import tensorflow as tf from transformers import TFAutoModelForSeqaSeqLM, TFPegasusForConditionalGeneration, TFPegasusModel @require_tf class _lowerCamelCase : """simple docstring""" UpperCAmelCase_ : Optional[Any] =PegasusConfig UpperCAmelCase_ : Any ={} UpperCAmelCase_ : Union[str, Any] ="gelu" def __init__( self , UpperCAmelCase , UpperCAmelCase=13 , UpperCAmelCase=7 , UpperCAmelCase=True , UpperCAmelCase=False , UpperCAmelCase=99 , UpperCAmelCase=32 , UpperCAmelCase=2 , UpperCAmelCase=4 , UpperCAmelCase=37 , UpperCAmelCase=0.1 , UpperCAmelCase=0.1 , UpperCAmelCase=40 , UpperCAmelCase=2 , UpperCAmelCase=1 , UpperCAmelCase=0 , ) -> Any: '''simple docstring''' __snake_case : str = parent __snake_case : Union[str, Any] = batch_size __snake_case : List[str] = seq_length __snake_case : str = is_training __snake_case : Tuple = use_labels __snake_case : Union[str, Any] = vocab_size __snake_case : Dict = hidden_size __snake_case : Tuple = num_hidden_layers __snake_case : Optional[int] = num_attention_heads __snake_case : Optional[int] = intermediate_size __snake_case : str = hidden_dropout_prob __snake_case : Any = attention_probs_dropout_prob __snake_case : Optional[int] = max_position_embeddings __snake_case : Any = eos_token_id __snake_case : Union[str, Any] = pad_token_id __snake_case : str = bos_token_id def UpperCAmelCase ( self ) -> Dict: '''simple docstring''' __snake_case : Any = ids_tensor([self.batch_size, self.seq_length - 1] , self.vocab_size ) __snake_case : List[Any] = tf.expand_dims(tf.constant([self.eos_token_id] * self.batch_size ) , 1 ) __snake_case : Optional[Any] = tf.concat([input_ids, eos_tensor] , axis=1 ) __snake_case : Tuple = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) __snake_case : int = self.config_cls( vocab_size=self.vocab_size , d_model=self.hidden_size , encoder_layers=self.num_hidden_layers , decoder_layers=self.num_hidden_layers , encoder_attention_heads=self.num_attention_heads , decoder_attention_heads=self.num_attention_heads , encoder_ffn_dim=self.intermediate_size , decoder_ffn_dim=self.intermediate_size , dropout=self.hidden_dropout_prob , attention_dropout=self.attention_probs_dropout_prob , max_position_embeddings=self.max_position_embeddings , eos_token_ids=[2] , bos_token_id=self.bos_token_id , pad_token_id=self.pad_token_id , decoder_start_token_id=self.pad_token_id , **self.config_updates , ) __snake_case : Any = prepare_pegasus_inputs_dict(_a , _a , _a ) return config, inputs_dict def UpperCAmelCase ( self , UpperCAmelCase , UpperCAmelCase ) -> Tuple: '''simple docstring''' __snake_case : Optional[int] = TFPegasusModel(config=_a ).get_decoder() __snake_case : Union[str, Any] = inputs_dict["input_ids"] __snake_case : Dict = input_ids[:1, :] __snake_case : str = inputs_dict["attention_mask"][:1, :] __snake_case : Union[str, Any] = inputs_dict["head_mask"] __snake_case : int = 1 # first forward pass __snake_case : str = model(_a , attention_mask=_a , head_mask=_a , use_cache=_a ) __snake_case , __snake_case : Any = outputs.to_tuple() # create hypothetical next token and extent to next_input_ids __snake_case : Any = ids_tensor((self.batch_size, 3) , config.vocab_size ) __snake_case : Optional[Any] = tf.cast(ids_tensor((self.batch_size, 3) , 2 ) , tf.inta ) # append to next input_ids and __snake_case : Any = tf.concat([input_ids, next_tokens] , axis=-1 ) __snake_case : List[Any] = tf.concat([attention_mask, next_attn_mask] , axis=-1 ) __snake_case : Union[str, Any] = model(_a , attention_mask=_a )[0] __snake_case : List[Any] = model(_a , attention_mask=_a , past_key_values=_a )[0] self.parent.assertEqual(next_tokens.shape[1] , output_from_past.shape[1] ) # select random slice __snake_case : List[str] = int(ids_tensor((1,) , output_from_past.shape[-1] ) ) __snake_case : Union[str, Any] = output_from_no_past[:, -3:, random_slice_idx] __snake_case : Optional[int] = output_from_past[:, :, random_slice_idx] # test that outputs are equal for slice tf.debugging.assert_near(_a , _a , rtol=1E-3 ) def lowerCAmelCase__( lowercase : Any , lowercase : int , lowercase : Optional[int] , lowercase : Any=None , lowercase : str=None , lowercase : Union[str, Any]=None , lowercase : List[Any]=None , lowercase : Tuple=None , ) -> Dict: if attention_mask is None: __snake_case : Dict = tf.cast(tf.math.not_equal(a__ , config.pad_token_id ) , tf.inta ) if decoder_attention_mask is None: __snake_case : Tuple = tf.concat( [ tf.ones(decoder_input_ids[:, :1].shape , dtype=tf.inta ), tf.cast(tf.math.not_equal(decoder_input_ids[:, 1:] , config.pad_token_id ) , tf.inta ), ] , axis=-1 , ) if head_mask is None: __snake_case : Union[str, Any] = tf.ones((config.encoder_layers, config.encoder_attention_heads) ) if decoder_head_mask is None: __snake_case : Any = tf.ones((config.decoder_layers, config.decoder_attention_heads) ) if cross_attn_head_mask is None: __snake_case : Optional[int] = tf.ones((config.decoder_layers, config.decoder_attention_heads) ) return { "input_ids": input_ids, "decoder_input_ids": decoder_input_ids, "attention_mask": attention_mask, "decoder_attention_mask": decoder_attention_mask, "head_mask": head_mask, "decoder_head_mask": decoder_head_mask, "cross_attn_head_mask": cross_attn_head_mask, } @require_tf class _lowerCamelCase ( UpperCamelCase__ , UpperCamelCase__ , unittest.TestCase ): """simple docstring""" UpperCAmelCase_ : int =(TFPegasusForConditionalGeneration, TFPegasusModel) if is_tf_available() else () UpperCAmelCase_ : List[str] =(TFPegasusForConditionalGeneration,) if is_tf_available() else () UpperCAmelCase_ : Any =( { "conversational": TFPegasusForConditionalGeneration, "feature-extraction": TFPegasusModel, "summarization": TFPegasusForConditionalGeneration, "text2text-generation": TFPegasusForConditionalGeneration, "translation": TFPegasusForConditionalGeneration, } if is_tf_available() else {} ) UpperCAmelCase_ : List[str] =True UpperCAmelCase_ : Optional[Any] =False UpperCAmelCase_ : Dict =False def UpperCAmelCase ( self ) -> Union[str, Any]: '''simple docstring''' __snake_case : Optional[int] = TFPegasusModelTester(self ) __snake_case : Tuple = ConfigTester(self , config_class=_a ) def UpperCAmelCase ( self ) -> Union[str, Any]: '''simple docstring''' self.config_tester.run_common_tests() def UpperCAmelCase ( self ) -> str: '''simple docstring''' __snake_case : List[str] = self.model_tester.prepare_config_and_inputs_for_common() self.model_tester.check_decoder_model_past_large_inputs(*_a ) @require_sentencepiece @require_tokenizers @require_tf class _lowerCamelCase ( unittest.TestCase ): """simple docstring""" UpperCAmelCase_ : List[str] =[ " PG&E stated it scheduled the blackouts in response to forecasts for high winds amid dry conditions. The aim is to reduce the risk of wildfires. Nearly 800 thousand customers were scheduled to be affected by the shutoffs which were expected to last through at least midday tomorrow.", " The London trio are up for best UK act and best album, as well as getting two nominations in the best song category.\"We got told like this morning 'Oh I think you're nominated'\", said Dappy.\"And I was like 'Oh yeah, which one?' And now we've got nominated for four awards. I mean, wow!\"Bandmate Fazer added: \"We thought it's best of us to come down and mingle with everyone and say hello to the cameras. And now we find we've got four nominations.\"The band have two shots at the best song prize, getting the nod for their Tynchy Stryder collaboration Number One, and single Strong Again.Their album Uncle B will also go up against records by the likes of Beyonce and Kanye West.N-Dubz picked up the best newcomer Mobo in 2007, but female member Tulisa said they wouldn't be too disappointed if they didn't win this time around.\"At the end of the day we're grateful to be where we are in our careers.\"If it don't happen then it don't happen - live to fight another day and keep on making albums and hits for the fans.\"Dappy also revealed they could be performing live several times on the night.The group will be doing Number One and also a possible rendition of the War Child single, I Got Soul.The charity song is a re-working of The Killers' All These Things That I've Done and is set to feature artists like Chipmunk, Ironik and Pixie Lott.This year's Mobos will be held outside of London for the first time, in Glasgow on 30 September.N-Dubz said they were looking forward to performing for their Scottish fans and boasted about their recent shows north of the border.\"We just done Edinburgh the other day,\" said Dappy.\"We smashed up an N-Dubz show over there. We done Aberdeen about three or four months ago - we smashed up that show over there! Everywhere we go we smash it up!\" ", ] UpperCAmelCase_ : Optional[int] =[ "California's largest electricity provider has cut power to hundreds of thousands of customers in an effort to" " reduce the risk of wildfires.", "N-Dubz have revealed they\'re \"grateful\" to have been nominated for four Mobo Awards.", ] # differs slightly from pytorch, likely due to numerical differences in linear layers UpperCAmelCase_ : Any ="google/pegasus-xsum" @cached_property def UpperCAmelCase ( self ) -> Any: '''simple docstring''' return AutoTokenizer.from_pretrained(self.model_name ) @cached_property def UpperCAmelCase ( self ) -> Optional[Any]: '''simple docstring''' __snake_case : List[str] = TFAutoModelForSeqaSeqLM.from_pretrained(self.model_name ) return model def UpperCAmelCase ( self , **UpperCAmelCase ) -> List[str]: '''simple docstring''' __snake_case : Dict = self.translate_src_text(**_a ) assert self.expected_text == generated_words def UpperCAmelCase ( self , **UpperCAmelCase ) -> Dict: '''simple docstring''' __snake_case : List[Any] = self.tokenizer(self.src_text , **_a , padding=_a , return_tensors="tf" ) __snake_case : List[Any] = self.model.generate( model_inputs.input_ids , attention_mask=model_inputs.attention_mask , num_beams=2 , use_cache=_a , ) __snake_case : Dict = self.tokenizer.batch_decode(generated_ids.numpy() , skip_special_tokens=_a ) return generated_words @slow def UpperCAmelCase ( self ) -> Tuple: '''simple docstring''' self._assert_generated_batch_equal_expected()
243
import copy from ...configuration_utils import PretrainedConfig from ...utils import add_start_docstrings snake_case_ : Optional[Any] = R''' [`RagConfig`] stores the configuration of a *RagModel*. Configuration objects inherit from [`PretrainedConfig`] and can be used to control the model outputs. Read the documentation from [`PretrainedConfig`] for more information. Args: title_sep (`str`, *optional*, defaults to `" / "`): Separator inserted between the title and the text of the retrieved document when calling [`RagRetriever`]. doc_sep (`str`, *optional*, defaults to `" // "`): Separator inserted between the text of the retrieved document and the original input when calling [`RagRetriever`]. n_docs (`int`, *optional*, defaults to 5): Number of documents to retrieve. max_combined_length (`int`, *optional*, defaults to 300): Max length of contextualized input returned by [`~RagRetriever.__call__`]. retrieval_vector_size (`int`, *optional*, defaults to 768): Dimensionality of the document embeddings indexed by [`RagRetriever`]. retrieval_batch_size (`int`, *optional*, defaults to 8): Retrieval batch size, defined as the number of queries issues concurrently to the faiss index encapsulated [`RagRetriever`]. dataset (`str`, *optional*, defaults to `"wiki_dpr"`): A dataset identifier of the indexed dataset in HuggingFace Datasets (list all available datasets and ids using `datasets.list_datasets()`). dataset_split (`str`, *optional*, defaults to `"train"`) Which split of the `dataset` to load. index_name (`str`, *optional*, defaults to `"compressed"`) The index name of the index associated with the `dataset`. One can choose between `"legacy"`, `"exact"` and `"compressed"`. index_path (`str`, *optional*) The path to the serialized faiss index on disk. passages_path (`str`, *optional*): A path to text passages compatible with the faiss index. Required if using [`~models.rag.retrieval_rag.LegacyIndex`] use_dummy_dataset (`bool`, *optional*, defaults to `False`) Whether to load a "dummy" variant of the dataset specified by `dataset`. label_smoothing (`float`, *optional*, defaults to 0.0): Only relevant if `return_loss` is set to `True`. Controls the `epsilon` parameter value for label smoothing in the loss calculation. If set to 0, no label smoothing is performed. do_marginalize (`bool`, *optional*, defaults to `False`): If `True`, the logits are marginalized over all documents by making use of `torch.nn.functional.log_softmax`. reduce_loss (`bool`, *optional*, defaults to `False`): Whether or not to reduce the NLL loss using the `torch.Tensor.sum` operation. do_deduplication (`bool`, *optional*, defaults to `True`): Whether or not to deduplicate the generations from different context documents for a given input. Has to be set to `False` if used while training with distributed backend. exclude_bos_score (`bool`, *optional*, defaults to `False`): Whether or not to disregard the BOS token when computing the loss. output_retrieved(`bool`, *optional*, defaults to `False`): If set to `True`, `retrieved_doc_embeds`, `retrieved_doc_ids`, `context_input_ids` and `context_attention_mask` are returned. See returned tensors for more detail. use_cache (`bool`, *optional*, defaults to `True`): Whether or not the model should return the last key/values attentions (not used by all models). forced_eos_token_id (`int`, *optional*): The id of the token to force as the last generated token when `max_length` is reached. Usually set to `eos_token_id`. ''' @add_start_docstrings(UpperCamelCase__ ) class A__ ( UpperCamelCase__ ): UpperCAmelCase = "rag" UpperCAmelCase = True def __init__( self : Tuple , _a : List[Any]=None , _a : Tuple=True , _a : Optional[Any]=None , _a : int=None , _a : List[str]=None , _a : int=None , _a : Optional[int]=None , _a : str=" / " , _a : Any=" // " , _a : Optional[Any]=5 , _a : int=300 , _a : Optional[Any]=768 , _a : Any=8 , _a : List[str]="wiki_dpr" , _a : Dict="train" , _a : Union[str, Any]="compressed" , _a : str=None , _a : Union[str, Any]=None , _a : int=False , _a : Any=False , _a : Any=0.0 , _a : Any=True , _a : List[str]=False , _a : Optional[int]=False , _a : int=False , _a : Union[str, Any]=True , _a : Optional[int]=None , **_a : List[str] , ) -> List[Any]: """simple docstring""" super().__init__( bos_token_id=_a , pad_token_id=_a , eos_token_id=_a , decoder_start_token_id=_a , forced_eos_token_id=_a , is_encoder_decoder=_a , prefix=_a , vocab_size=_a , **_a , ) assert ( "question_encoder" in kwargs and "generator" in kwargs ), "Config has to be initialized with question_encoder and generator config" _SCREAMING_SNAKE_CASE =kwargs.pop('''question_encoder''' ) _SCREAMING_SNAKE_CASE =question_encoder_config.pop('''model_type''' ) _SCREAMING_SNAKE_CASE =kwargs.pop('''generator''' ) _SCREAMING_SNAKE_CASE =decoder_config.pop('''model_type''' ) from ..auto.configuration_auto import AutoConfig _SCREAMING_SNAKE_CASE =AutoConfig.for_model(_a , **_a ) _SCREAMING_SNAKE_CASE =AutoConfig.for_model(_a , **_a ) _SCREAMING_SNAKE_CASE =reduce_loss _SCREAMING_SNAKE_CASE =label_smoothing _SCREAMING_SNAKE_CASE =exclude_bos_score _SCREAMING_SNAKE_CASE =do_marginalize _SCREAMING_SNAKE_CASE =title_sep _SCREAMING_SNAKE_CASE =doc_sep _SCREAMING_SNAKE_CASE =n_docs _SCREAMING_SNAKE_CASE =max_combined_length _SCREAMING_SNAKE_CASE =dataset _SCREAMING_SNAKE_CASE =dataset_split _SCREAMING_SNAKE_CASE =index_name _SCREAMING_SNAKE_CASE =retrieval_vector_size _SCREAMING_SNAKE_CASE =retrieval_batch_size _SCREAMING_SNAKE_CASE =passages_path _SCREAMING_SNAKE_CASE =index_path _SCREAMING_SNAKE_CASE =use_dummy_dataset _SCREAMING_SNAKE_CASE =output_retrieved _SCREAMING_SNAKE_CASE =do_deduplication _SCREAMING_SNAKE_CASE =use_cache if self.forced_eos_token_id is None: _SCREAMING_SNAKE_CASE =getattr(self.generator , '''forced_eos_token_id''' , _a ) @classmethod def __UpperCamelCase ( cls : Optional[int] , _a : PretrainedConfig , _a : PretrainedConfig , **_a : Dict ) -> PretrainedConfig: """simple docstring""" return cls(question_encoder=question_encoder_config.to_dict() , generator=generator_config.to_dict() , **_a ) def __UpperCamelCase ( self : Optional[Any] ) -> Any: """simple docstring""" _SCREAMING_SNAKE_CASE =copy.deepcopy(self.__dict__ ) _SCREAMING_SNAKE_CASE =self.question_encoder.to_dict() _SCREAMING_SNAKE_CASE =self.generator.to_dict() _SCREAMING_SNAKE_CASE =self.__class__.model_type return output
691
0
# limitations under the License. # NOTE: This file is deprecated and will be removed in a future version. # It only exists so that temporarely `from diffusers.pipelines import DiffusionPipeline` works from .pipelines import DiffusionPipeline, ImagePipelineOutput # noqa: F401 from .utils import deprecate deprecate( 'pipelines_utils', '0.22.0', 'Importing `DiffusionPipeline` or `ImagePipelineOutput` from diffusers.pipeline_utils is deprecated. Please import from diffusers.pipelines.pipeline_utils instead.', standard_warn=False, stacklevel=3, )
524
from manim import * class A__ ( UpperCamelCase__ ): def __UpperCamelCase ( self : Dict ) -> int: """simple docstring""" _SCREAMING_SNAKE_CASE =Rectangle(height=0.5 , width=0.5 ) _SCREAMING_SNAKE_CASE =Rectangle(height=0.25 , width=0.25 ) _SCREAMING_SNAKE_CASE =Rectangle(height=0.46 , width=0.46 ).set_stroke(width=0 ) _SCREAMING_SNAKE_CASE =[mem.copy() for i in range(6 )] _SCREAMING_SNAKE_CASE =[mem.copy() for i in range(6 )] _SCREAMING_SNAKE_CASE =VGroup(*_a ).arrange(_a , buff=0 ) _SCREAMING_SNAKE_CASE =VGroup(*_a ).arrange(_a , buff=0 ) _SCREAMING_SNAKE_CASE =VGroup(_a , _a ).arrange(_a , buff=0 ) _SCREAMING_SNAKE_CASE =Text('''CPU''' , font_size=24 ) _SCREAMING_SNAKE_CASE =Group(_a , _a ).arrange(_a , buff=0.5 , aligned_edge=_a ) cpu.move_to([-2.5, -0.5, 0] ) self.add(_a ) _SCREAMING_SNAKE_CASE =[mem.copy() for i in range(4 )] _SCREAMING_SNAKE_CASE =VGroup(*_a ).arrange(_a , buff=0 ) _SCREAMING_SNAKE_CASE =Text('''GPU''' , font_size=24 ) _SCREAMING_SNAKE_CASE =Group(_a , _a ).arrange(_a , buff=0.5 , aligned_edge=_a ) gpu.move_to([-1, -1, 0] ) self.add(_a ) _SCREAMING_SNAKE_CASE =[mem.copy() for i in range(6 )] _SCREAMING_SNAKE_CASE =VGroup(*_a ).arrange(_a , buff=0 ) _SCREAMING_SNAKE_CASE =Text('''Model''' , font_size=24 ) _SCREAMING_SNAKE_CASE =Group(_a , _a ).arrange(_a , buff=0.5 , aligned_edge=_a ) model.move_to([3, -1.0, 0] ) self.add(_a ) _SCREAMING_SNAKE_CASE =[] _SCREAMING_SNAKE_CASE =[] _SCREAMING_SNAKE_CASE =[] for i, rect in enumerate(_a ): rect.set_stroke(_a ) _SCREAMING_SNAKE_CASE =Rectangle(height=0.46 / 4 , width=0.46 / 3 ).set_stroke(width=0.0 ).set_fill(_a , opacity=0.7 ) if i == 0: cpu_target.next_to(cpu_left_col_base[0].get_corner(DOWN + LEFT ) , buff=0.02 , direction=_a ) cpu_target.set_x(cpu_target.get_x() + 0.1 ) elif i == 3: cpu_target.next_to(model_cpu_arr[0] , direction=_a , buff=0.0 ) else: cpu_target.next_to(model_cpu_arr[i - 1] , direction=_a , buff=0.0 ) self.add(_a ) model_cpu_arr.append(_a ) self.add(*_a , *_a , *_a ) _SCREAMING_SNAKE_CASE =[mem.copy() for i in range(6 )] _SCREAMING_SNAKE_CASE =VGroup(*_a ).arrange(_a , buff=0 ) _SCREAMING_SNAKE_CASE =Text('''Loaded Checkpoint''' , font_size=24 ) _SCREAMING_SNAKE_CASE =Group(_a , _a ).arrange(_a , buff=0.5 , aligned_edge=_a ) checkpoint.move_to([3, 0.5, 0] ) self.add(_a ) _SCREAMING_SNAKE_CASE =[] _SCREAMING_SNAKE_CASE =[] for i, rect in enumerate(_a ): _SCREAMING_SNAKE_CASE =fill.copy().set_fill(_a , opacity=0.7 ) target.move_to(_a ) ckpt_arr.append(_a ) _SCREAMING_SNAKE_CASE =target.copy() if i < 5: cpu_target.move_to(cpu_left_col_base[i + 1] ) else: cpu_target.move_to(cpu_right_col_base[i - 5] ) ckpt_cpu_arr.append(_a ) self.add(*_a , *_a ) _SCREAMING_SNAKE_CASE =Square(side_length=2.2 ) key.move_to([-5, 2, 0] ) _SCREAMING_SNAKE_CASE =MarkupText( f"<b>Key:</b>\n\n<span fgcolor='{YELLOW}'>โ—</span> Empty Model" , font_size=18 , ) key_text.move_to([-5, 2.4, 0] ) self.add(_a , _a ) _SCREAMING_SNAKE_CASE =MarkupText( f"<span fgcolor='{BLUE}'>โ—</span> Checkpoint" , font_size=18 , ) blue_text.next_to(_a , DOWN * 2.4 , aligned_edge=key_text.get_left() ) self.add(_a ) _SCREAMING_SNAKE_CASE =MarkupText( f"Based on the passed in configuration, weights are stored in\na variety of np.memmaps on disk or to a particular device." , font_size=24 , ) step_a.move_to([2, 2, 0] ) _SCREAMING_SNAKE_CASE =[meta_mem.copy() for i in range(6 )] _SCREAMING_SNAKE_CASE =[meta_mem.copy() for i in range(6 )] _SCREAMING_SNAKE_CASE =VGroup(*_a ).arrange(_a , buff=0 ) _SCREAMING_SNAKE_CASE =VGroup(*_a ).arrange(_a , buff=0 ) _SCREAMING_SNAKE_CASE =VGroup(_a , _a ).arrange(_a , buff=0 ) _SCREAMING_SNAKE_CASE =Text('''Disk''' , font_size=24 ) _SCREAMING_SNAKE_CASE =Group(_a , _a ).arrange(_a , buff=0.5 , aligned_edge=_a ) disk.move_to([-4.0, -1.25, 0] ) self.play(Write(_a , run_time=3 ) , Write(_a , run_time=1 ) , Create(_a , run_time=1 ) ) _SCREAMING_SNAKE_CASE =[] for i, rect in enumerate(_a ): _SCREAMING_SNAKE_CASE =rect.copy() target.generate_target() target.target.move_to(disk_left_col_base[i] ).scale(0.5 ) animations.append(MoveToTarget(_a , run_time=1.5 ) ) self.play(*_a ) self.play(FadeOut(_a ) ) _SCREAMING_SNAKE_CASE =MarkupText(f"Then, the checkpoint is removed from memory\nthrough garbage collection." , font_size=24 ) step_a.move_to([2, 2, 0] ) self.play(Write(_a , run_time=3 ) ) self.play( FadeOut(_a , _a , *_a , *_a ) , ) self.wait()
691
0
import copy import os import tempfile from unittest import TestCase from unittest.mock import patch import numpy as np import pyarrow as pa import pyarrow.parquet as pq import pytest from datasets.arrow_writer import ArrowWriter, OptimizedTypedSequence, ParquetWriter, TypedSequence from datasets.features import ArrayaD, ClassLabel, Features, Image, Value from datasets.features.features import ArrayaDExtensionType, cast_to_python_objects from datasets.keyhash import DuplicatedKeysError, InvalidKeyError from .utils import require_pil class lowercase ( UpperCamelCase__ ): def a ( self ): snake_case_ = pa.array(TypedSequence([1, 2, 3] ) ) self.assertEqual(arr.type , pa.intaa() ) def a ( self ): with self.assertRaises(_a ): snake_case_ = pa.array(TypedSequence([1, 2, 3] ) , type=pa.intaa() ) def a ( self ): with self.assertRaises(_a ): snake_case_ = pa.array(TypedSequence([1, 2, 3] , try_type=Value('bool' ) , type=Value('int64' ) ) ) def a ( self ): snake_case_ = pa.array(TypedSequence([1, 2, 3] , type=Value('int32' ) ) ) self.assertEqual(arr.type , pa.intaa() ) def a ( self ): with self.assertRaises((TypeError, pa.lib.ArrowInvalid) ): snake_case_ = pa.array(TypedSequence(['foo', 'bar'] , type=Value('int64' ) ) ) def a ( self ): snake_case_ = pa.array(TypedSequence([1, 2, 3] , try_type=Value('int32' ) ) ) self.assertEqual(arr.type , pa.intaa() ) def a ( self ): snake_case_ = pa.array(TypedSequence(['foo', 'bar'] , try_type=Value('int64' ) ) ) self.assertEqual(arr.type , pa.string() ) def a ( self ): snake_case_ = pa.array(TypedSequence([[[1, 2, 3]]] , type=ArrayaD((1, 3) , 'int64' ) ) ) self.assertEqual(arr.type , ArrayaDExtensionType((1, 3) , 'int64' ) ) def a ( self ): with self.assertRaises((TypeError, pa.lib.ArrowInvalid) ): snake_case_ = pa.array(TypedSequence(['foo', 'bar'] , type=ArrayaD((1, 3) , 'int64' ) ) ) def a ( self ): snake_case_ = pa.array(TypedSequence([[[1, 2, 3]]] , try_type=ArrayaD((1, 3) , 'int64' ) ) ) self.assertEqual(arr.type , ArrayaDExtensionType((1, 3) , 'int64' ) ) def a ( self ): snake_case_ = pa.array(TypedSequence(['foo', 'bar'] , try_type=ArrayaD((1, 3) , 'int64' ) ) ) self.assertEqual(arr.type , pa.string() ) @require_pil def a ( self ): import PIL.Image snake_case_ = PIL.Image.fromarray(np.arange(10 , dtype=np.uinta ).reshape(2 , 5 ) ) with patch( 'datasets.arrow_writer.cast_to_python_objects' , side_effect=_a ) as mock_cast_to_python_objects: snake_case_ = pa.array(TypedSequence([{'path': None, 'bytes': b'image_bytes'}, pil_image] , type=Image() ) ) snake_case_ , snake_case_ = mock_cast_to_python_objects.call_args_list[-1] self.assertIn('optimize_list_casting' , _a ) self.assertFalse(kwargs['optimize_list_casting'] ) def __lowerCamelCase ( UpperCamelCase__ , UpperCamelCase__ ): '''simple docstring''' snake_case_ = pa.BufferReader(a__ ) if isinstance(a__ , pa.Buffer ) else pa.memory_map(a__ ) snake_case_ = pa.ipc.open_stream(a__ ) snake_case_ = f.read_all() assert len(pa_table.to_batches() ) == expected_num_chunks assert pa_table.to_pydict() == {"col_1": ["foo", "bar"], "col_2": [1, 2]} del pa_table @pytest.mark.parametrize('writer_batch_size' , [None, 1, 10] ) @pytest.mark.parametrize( 'fields' , [None, {'col_1': pa.string(), 'col_2': pa.intaa()}, {'col_1': pa.string(), 'col_2': pa.intaa()}] ) def __lowerCamelCase ( UpperCamelCase__ , UpperCamelCase__ ): '''simple docstring''' snake_case_ = pa.BufferOutputStream() snake_case_ = pa.schema(a__ ) if fields else None with ArrowWriter(stream=a__ , schema=a__ , writer_batch_size=a__ ) as writer: writer.write({'col_1': 'foo', 'col_2': 1} ) writer.write({'col_1': 'bar', 'col_2': 2} ) snake_case_ , snake_case_ = writer.finalize() assert num_examples == 2 assert num_bytes > 0 if not fields: snake_case_ = {'col_1': pa.string(), 'col_2': pa.intaa()} assert writer._schema == pa.schema(a__ , metadata=writer._schema.metadata ) _check_output(output.getvalue() , expected_num_chunks=num_examples if writer_batch_size == 1 else 1 ) def __lowerCamelCase ( ): '''simple docstring''' snake_case_ = pa.BufferOutputStream() snake_case_ = Features({'labels': ClassLabel(names=['neg', 'pos'] )} ) with ArrowWriter(stream=a__ , features=a__ ) as writer: writer.write({'labels': 0} ) writer.write({'labels': 1} ) snake_case_ , snake_case_ = writer.finalize() assert num_examples == 2 assert num_bytes > 0 assert writer._schema == features.arrow_schema assert writer._schema.metadata == features.arrow_schema.metadata snake_case_ = pa.BufferReader(output.getvalue() ) snake_case_ = pa.ipc.open_stream(a__ ) snake_case_ = f.read_all() snake_case_ = pa_table.schema assert pa_table.num_rows == 2 assert schema == features.arrow_schema assert schema.metadata == features.arrow_schema.metadata assert features == Features.from_arrow_schema(a__ ) @pytest.mark.parametrize('writer_batch_size' , [None, 1, 10] ) def __lowerCamelCase ( UpperCamelCase__ ): '''simple docstring''' snake_case_ = pa.BufferOutputStream() with ArrowWriter( stream=a__ , writer_batch_size=a__ , hash_salt='split_name' , check_duplicates=a__ , ) as writer: with pytest.raises(a__ ): writer.write({'col_1': 'foo', 'col_2': 1} , key=[1, 2] ) snake_case_ , snake_case_ = writer.finalize() @pytest.mark.parametrize('writer_batch_size' , [None, 2, 10] ) def __lowerCamelCase ( UpperCamelCase__ ): '''simple docstring''' snake_case_ = pa.BufferOutputStream() with ArrowWriter( stream=a__ , writer_batch_size=a__ , hash_salt='split_name' , check_duplicates=a__ , ) as writer: with pytest.raises(a__ ): writer.write({'col_1': 'foo', 'col_2': 1} , key=10 ) writer.write({'col_1': 'bar', 'col_2': 2} , key=10 ) snake_case_ , snake_case_ = writer.finalize() @pytest.mark.parametrize('writer_batch_size' , [None, 2, 10] ) def __lowerCamelCase ( UpperCamelCase__ ): '''simple docstring''' snake_case_ = pa.BufferOutputStream() with ArrowWriter( stream=a__ , writer_batch_size=a__ , hash_salt='split_name' , check_duplicates=a__ , ) as writer: writer.write({'col_1': 'foo', 'col_2': 1} , key=1 ) writer.write({'col_1': 'bar', 'col_2': 2} , key=2 ) snake_case_ , snake_case_ = writer.finalize() assert num_examples == 2 assert num_bytes > 0 _check_output(output.getvalue() , expected_num_chunks=num_examples if writer_batch_size == 1 else 1 ) @pytest.mark.parametrize('writer_batch_size' , [None, 1, 10] ) @pytest.mark.parametrize( 'fields' , [None, {'col_1': pa.string(), 'col_2': pa.intaa()}, {'col_1': pa.string(), 'col_2': pa.intaa()}] ) def __lowerCamelCase ( UpperCamelCase__ , UpperCamelCase__ ): '''simple docstring''' snake_case_ = pa.BufferOutputStream() snake_case_ = pa.schema(a__ ) if fields else None with ArrowWriter(stream=a__ , schema=a__ , writer_batch_size=a__ ) as writer: writer.write_batch({'col_1': ['foo', 'bar'], 'col_2': [1, 2]} ) writer.write_batch({'col_1': [], 'col_2': []} ) snake_case_ , snake_case_ = writer.finalize() assert num_examples == 2 assert num_bytes > 0 if not fields: snake_case_ = {'col_1': pa.string(), 'col_2': pa.intaa()} assert writer._schema == pa.schema(a__ , metadata=writer._schema.metadata ) _check_output(output.getvalue() , expected_num_chunks=num_examples if writer_batch_size == 1 else 1 ) @pytest.mark.parametrize('writer_batch_size' , [None, 1, 10] ) @pytest.mark.parametrize( 'fields' , [None, {'col_1': pa.string(), 'col_2': pa.intaa()}, {'col_1': pa.string(), 'col_2': pa.intaa()}] ) def __lowerCamelCase ( UpperCamelCase__ , UpperCamelCase__ ): '''simple docstring''' snake_case_ = pa.BufferOutputStream() snake_case_ = pa.schema(a__ ) if fields else None with ArrowWriter(stream=a__ , schema=a__ , writer_batch_size=a__ ) as writer: writer.write_table(pa.Table.from_pydict({'col_1': ['foo', 'bar'], 'col_2': [1, 2]} ) ) snake_case_ , snake_case_ = writer.finalize() assert num_examples == 2 assert num_bytes > 0 if not fields: snake_case_ = {'col_1': pa.string(), 'col_2': pa.intaa()} assert writer._schema == pa.schema(a__ , metadata=writer._schema.metadata ) _check_output(output.getvalue() , expected_num_chunks=num_examples if writer_batch_size == 1 else 1 ) @pytest.mark.parametrize('writer_batch_size' , [None, 1, 10] ) @pytest.mark.parametrize( 'fields' , [None, {'col_1': pa.string(), 'col_2': pa.intaa()}, {'col_1': pa.string(), 'col_2': pa.intaa()}] ) def __lowerCamelCase ( UpperCamelCase__ , UpperCamelCase__ ): '''simple docstring''' snake_case_ = pa.BufferOutputStream() snake_case_ = pa.schema(a__ ) if fields else None with ArrowWriter(stream=a__ , schema=a__ , writer_batch_size=a__ ) as writer: writer.write_row(pa.Table.from_pydict({'col_1': ['foo'], 'col_2': [1]} ) ) writer.write_row(pa.Table.from_pydict({'col_1': ['bar'], 'col_2': [2]} ) ) snake_case_ , snake_case_ = writer.finalize() assert num_examples == 2 assert num_bytes > 0 if not fields: snake_case_ = {'col_1': pa.string(), 'col_2': pa.intaa()} assert writer._schema == pa.schema(a__ , metadata=writer._schema.metadata ) _check_output(output.getvalue() , expected_num_chunks=num_examples if writer_batch_size == 1 else 1 ) def __lowerCamelCase ( ): '''simple docstring''' with tempfile.TemporaryDirectory() as tmp_dir: snake_case_ = {'col_1': pa.string(), 'col_2': pa.intaa()} snake_case_ = os.path.join(a__ , 'test.arrow' ) with ArrowWriter(path=a__ , schema=pa.schema(a__ ) ) as writer: writer.write_batch({'col_1': ['foo', 'bar'], 'col_2': [1, 2]} ) snake_case_ , snake_case_ = writer.finalize() assert num_examples == 2 assert num_bytes > 0 assert writer._schema == pa.schema(a__ , metadata=writer._schema.metadata ) _check_output(a__ , 1 ) def __lowerCamelCase ( UpperCamelCase__ ): '''simple docstring''' if pa.types.is_list(a__ ): return get_base_dtype(arr_type.value_type ) else: return arr_type def __lowerCamelCase ( UpperCamelCase__ , UpperCamelCase__ ): '''simple docstring''' if isinstance(lst[0] , a__ ): change_first_primitive_element_in_list(lst[0] , a__ ) else: snake_case_ = value @pytest.mark.parametrize('optimized_int_type, expected_dtype' , [(None, pa.intaa()), (Value('int32' ), pa.intaa())] ) @pytest.mark.parametrize('sequence' , [[1, 2, 3], [[1, 2, 3]], [[[1, 2, 3]]]] ) def __lowerCamelCase ( UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ ): '''simple docstring''' snake_case_ = pa.array(TypedSequence(a__ , optimized_int_type=a__ ) ) assert get_base_dtype(arr.type ) == expected_dtype @pytest.mark.parametrize( 'col, expected_dtype' , [ ('attention_mask', pa.inta()), ('special_tokens_mask', pa.inta()), ('token_type_ids', pa.inta()), ('input_ids', pa.intaa()), ('other', pa.intaa()), ] , ) @pytest.mark.parametrize('sequence' , [[1, 2, 3], [[1, 2, 3]], [[[1, 2, 3]]]] ) def __lowerCamelCase ( UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ ): '''simple docstring''' snake_case_ = pa.array(OptimizedTypedSequence(a__ , col=a__ ) ) assert get_base_dtype(arr.type ) == expected_dtype # not in range if col != "other": # avoids errors due to in-place modifications snake_case_ = copy.deepcopy(a__ ) snake_case_ = np.iinfo(expected_dtype.to_pandas_dtype() ).max + 1 change_first_primitive_element_in_list(a__ , a__ ) snake_case_ = pa.array(OptimizedTypedSequence(a__ , col=a__ ) ) assert get_base_dtype(arr.type ) == pa.intaa() @pytest.mark.parametrize('raise_exception' , [False, True] ) def __lowerCamelCase ( UpperCamelCase__ , UpperCamelCase__ ): '''simple docstring''' snake_case_ = str(tmp_path / 'dataset-train.arrow' ) try: with ArrowWriter(path=a__ ) as writer: if raise_exception: raise pa.lib.ArrowInvalid() else: writer.stream.close() except pa.lib.ArrowInvalid: pass finally: assert writer.stream.closed def __lowerCamelCase ( UpperCamelCase__ ): '''simple docstring''' snake_case_ = 'mock://dataset-train.arrow' with ArrowWriter(path=a__ , storage_options=mockfs.storage_options ) as writer: assert isinstance(writer._fs , type(a__ ) ) assert writer._fs.storage_options == mockfs.storage_options writer.write({'col_1': 'foo', 'col_2': 1} ) writer.write({'col_1': 'bar', 'col_2': 2} ) snake_case_ , snake_case_ = writer.finalize() assert num_examples == 2 assert num_bytes > 0 assert mockfs.exists(a__ ) def __lowerCamelCase ( ): '''simple docstring''' snake_case_ = pa.BufferOutputStream() with ParquetWriter(stream=a__ ) as writer: writer.write({'col_1': 'foo', 'col_2': 1} ) writer.write({'col_1': 'bar', 'col_2': 2} ) snake_case_ , snake_case_ = writer.finalize() assert num_examples == 2 assert num_bytes > 0 snake_case_ = pa.BufferReader(output.getvalue() ) snake_case_ = pq.read_table(a__ ) assert pa_table.to_pydict() == {"col_1": ["foo", "bar"], "col_2": [1, 2]} @require_pil @pytest.mark.parametrize('embed_local_files' , [False, True] ) def __lowerCamelCase ( UpperCamelCase__ , UpperCamelCase__ ): '''simple docstring''' import PIL.Image snake_case_ = str(tmp_path / 'test_image_rgb.jpg' ) PIL.Image.fromarray(np.zeros((5, 5) , dtype=np.uinta ) ).save(a__ , format='png' ) snake_case_ = pa.BufferOutputStream() with ParquetWriter( stream=a__ , features=Features({'image': Image()} ) , embed_local_files=a__ ) as writer: writer.write({'image': image_path} ) writer.finalize() snake_case_ = pa.BufferReader(output.getvalue() ) snake_case_ = pq.read_table(a__ ) snake_case_ = pa_table.to_pydict() if embed_local_files: assert isinstance(out['image'][0]['path'] , a__ ) with open(a__ , 'rb' ) as f: assert out["image"][0]["bytes"] == f.read() else: assert out["image"][0]["path"] == image_path assert out["image"][0]["bytes"] is None def __lowerCamelCase ( ): '''simple docstring''' snake_case_ = pa.schema([pa.field('col_1' , pa.string() , nullable=a__ )] ) snake_case_ = pa.BufferOutputStream() with ArrowWriter(stream=a__ ) as writer: writer._build_writer(inferred_schema=a__ ) assert writer._schema == pa.schema([pa.field('col_1' , pa.string() )] )
362
import json from typing import TYPE_CHECKING, List, Optional, Tuple from tokenizers import pre_tokenizers, processors from ...tokenization_utils_base import AddedToken, BatchEncoding from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import logging from .tokenization_blenderbot import BlenderbotTokenizer if TYPE_CHECKING: from transformers.pipelines.conversational import Conversation snake_case_ : str = logging.get_logger(__name__) snake_case_ : List[Any] = { '''vocab_file''': '''vocab.json''', '''merges_file''': '''merges.txt''', '''tokenizer_config_file''': '''tokenizer_config.json''', } snake_case_ : Any = { '''vocab_file''': {'''facebook/blenderbot-3B''': '''https://huggingface.co/facebook/blenderbot-3B/resolve/main/vocab.json'''}, '''merges_file''': {'''facebook/blenderbot-3B''': '''https://huggingface.co/facebook/blenderbot-3B/resolve/main/merges.txt'''}, '''tokenizer_config_file''': { '''facebook/blenderbot-3B''': '''https://huggingface.co/facebook/blenderbot-3B/resolve/main/tokenizer_config.json''' }, } snake_case_ : List[str] = {'''facebook/blenderbot-3B''': 1_28} class A__ ( UpperCamelCase__ ): UpperCAmelCase = VOCAB_FILES_NAMES UpperCAmelCase = PRETRAINED_VOCAB_FILES_MAP UpperCAmelCase = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES UpperCAmelCase = ["input_ids", "attention_mask"] UpperCAmelCase = BlenderbotTokenizer def __init__( self : Dict , _a : str=None , _a : Optional[int]=None , _a : List[str]=None , _a : int="replace" , _a : Dict="<s>" , _a : Optional[Any]="</s>" , _a : Any="</s>" , _a : int="<s>" , _a : int="<unk>" , _a : Optional[int]="<pad>" , _a : Tuple="<mask>" , _a : Tuple=False , _a : Union[str, Any]=True , **_a : List[str] , ) -> Optional[int]: """simple docstring""" super().__init__( _a , _a , tokenizer_file=_a , errors=_a , bos_token=_a , eos_token=_a , sep_token=_a , cls_token=_a , unk_token=_a , pad_token=_a , mask_token=_a , add_prefix_space=_a , trim_offsets=_a , **_a , ) _SCREAMING_SNAKE_CASE =json.loads(self.backend_tokenizer.pre_tokenizer.__getstate__() ) if pre_tok_state.get('''add_prefix_space''' , _a ) != add_prefix_space: _SCREAMING_SNAKE_CASE =getattr(_a , pre_tok_state.pop('''type''' ) ) _SCREAMING_SNAKE_CASE =add_prefix_space _SCREAMING_SNAKE_CASE =pre_tok_class(**_a ) _SCREAMING_SNAKE_CASE =add_prefix_space _SCREAMING_SNAKE_CASE ='''post_processor''' _SCREAMING_SNAKE_CASE =getattr(self.backend_tokenizer , _a , _a ) if tokenizer_component_instance: _SCREAMING_SNAKE_CASE =json.loads(tokenizer_component_instance.__getstate__() ) # The lists 'sep' and 'cls' must be cased in tuples for the object `post_processor_class` if "sep" in state: _SCREAMING_SNAKE_CASE =tuple(state['''sep'''] ) if "cls" in state: _SCREAMING_SNAKE_CASE =tuple(state['''cls'''] ) _SCREAMING_SNAKE_CASE =False if state.get('''add_prefix_space''' , _a ) != add_prefix_space: _SCREAMING_SNAKE_CASE =add_prefix_space _SCREAMING_SNAKE_CASE =True if state.get('''trim_offsets''' , _a ) != trim_offsets: _SCREAMING_SNAKE_CASE =trim_offsets _SCREAMING_SNAKE_CASE =True if changes_to_apply: _SCREAMING_SNAKE_CASE =getattr(_a , state.pop('''type''' ) ) _SCREAMING_SNAKE_CASE =component_class(**_a ) setattr(self.backend_tokenizer , _a , _a ) @property # Copied from transformers.models.roberta.tokenization_roberta_fast.RobertaTokenizerFast.mask_token with Roberta->Blenderbot, RoBERTa->Blenderbot def __UpperCamelCase ( self : Tuple ) -> str: """simple docstring""" if self._mask_token is None: if self.verbose: logger.error('''Using mask_token, but it is not set yet.''' ) return None return str(self._mask_token ) @mask_token.setter def __UpperCamelCase ( self : Optional[Any] , _a : str ) -> List[str]: """simple docstring""" _SCREAMING_SNAKE_CASE =AddedToken(_a , lstrip=_a , rstrip=_a ) if isinstance(_a , _a ) else value _SCREAMING_SNAKE_CASE =value def __UpperCamelCase ( self : Optional[Any] , *_a : str , **_a : int ) -> BatchEncoding: """simple docstring""" _SCREAMING_SNAKE_CASE =kwargs.get('''is_split_into_words''' , _a ) assert self.add_prefix_space or not is_split_into_words, ( f"You need to instantiate {self.__class__.__name__} with add_prefix_space=True " "to use it with pretokenized inputs." ) return super()._batch_encode_plus(*_a , **_a ) def __UpperCamelCase ( self : List[Any] , *_a : Optional[int] , **_a : Union[str, Any] ) -> BatchEncoding: """simple docstring""" _SCREAMING_SNAKE_CASE =kwargs.get('''is_split_into_words''' , _a ) assert self.add_prefix_space or not is_split_into_words, ( f"You need to instantiate {self.__class__.__name__} with add_prefix_space=True " "to use it with pretokenized inputs." ) return super()._encode_plus(*_a , **_a ) def __UpperCamelCase ( self : Dict , _a : str , _a : Optional[str] = None ) -> Tuple[str]: """simple docstring""" _SCREAMING_SNAKE_CASE =self._tokenizer.model.save(_a , name=_a ) return tuple(_a ) def __UpperCamelCase ( self : Tuple , _a : List[int] , _a : Optional[List[int]] = None ) -> List[int]: """simple docstring""" _SCREAMING_SNAKE_CASE =[self.sep_token_id] _SCREAMING_SNAKE_CASE =[self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep ) * [0] return len(cls + token_ids_a + sep + sep + token_ids_a + sep ) * [0] def __UpperCamelCase ( self : Tuple , _a : List[int] , _a : Optional[List[int]] = None ) -> Optional[Any]: """simple docstring""" return token_ids_a + [self.eos_token_id] def __UpperCamelCase ( self : Any , _a : "Conversation" ) -> List[int]: """simple docstring""" _SCREAMING_SNAKE_CASE =[] for is_user, text in conversation.iter_texts(): if is_user: # We need to space prefix as it's being done within blenderbot inputs.append(''' ''' + text ) else: # Generated responses should contain them already. inputs.append(_a ) _SCREAMING_SNAKE_CASE =''' '''.join(_a ) _SCREAMING_SNAKE_CASE =self.encode(_a ) if len(_a ) > self.model_max_length: _SCREAMING_SNAKE_CASE =input_ids[-self.model_max_length :] logger.warning(f"Trimmed input from conversation as it was longer than {self.model_max_length} tokens." ) return input_ids
691
0
"""simple docstring""" _A = { '''Pillow''': '''Pillow<10.0.0''', '''accelerate''': '''accelerate>=0.20.3''', '''av''': '''av==9.2.0''', '''beautifulsoup4''': '''beautifulsoup4''', '''black''': '''black~=23.1''', '''codecarbon''': '''codecarbon==1.2.0''', '''cookiecutter''': '''cookiecutter==1.7.3''', '''dataclasses''': '''dataclasses''', '''datasets''': '''datasets!=2.5.0''', '''decord''': '''decord==0.6.0''', '''deepspeed''': '''deepspeed>=0.9.3''', '''diffusers''': '''diffusers''', '''dill''': '''dill<0.3.5''', '''evaluate''': '''evaluate>=0.2.0''', '''fairscale''': '''fairscale>0.3''', '''faiss-cpu''': '''faiss-cpu''', '''fastapi''': '''fastapi''', '''filelock''': '''filelock''', '''flax''': '''flax>=0.4.1,<=0.7.0''', '''ftfy''': '''ftfy''', '''fugashi''': '''fugashi>=1.0''', '''GitPython''': '''GitPython<3.1.19''', '''hf-doc-builder''': '''hf-doc-builder>=0.3.0''', '''huggingface-hub''': '''huggingface-hub>=0.14.1,<1.0''', '''importlib_metadata''': '''importlib_metadata''', '''ipadic''': '''ipadic>=1.0.0,<2.0''', '''isort''': '''isort>=5.5.4''', '''jax''': '''jax>=0.2.8,!=0.3.2,<=0.4.13''', '''jaxlib''': '''jaxlib>=0.1.65,<=0.4.13''', '''jieba''': '''jieba''', '''kenlm''': '''kenlm''', '''keras-nlp''': '''keras-nlp>=0.3.1''', '''librosa''': '''librosa''', '''nltk''': '''nltk''', '''natten''': '''natten>=0.14.6''', '''numpy''': '''numpy>=1.17''', '''onnxconverter-common''': '''onnxconverter-common''', '''onnxruntime-tools''': '''onnxruntime-tools>=1.4.2''', '''onnxruntime''': '''onnxruntime>=1.4.0''', '''opencv-python''': '''opencv-python''', '''optuna''': '''optuna''', '''optax''': '''optax>=0.0.8,<=0.1.4''', '''packaging''': '''packaging>=20.0''', '''parameterized''': '''parameterized''', '''phonemizer''': '''phonemizer''', '''protobuf''': '''protobuf''', '''psutil''': '''psutil''', '''pyyaml''': '''pyyaml>=5.1''', '''pydantic''': '''pydantic<2''', '''pytest''': '''pytest>=7.2.0''', '''pytest-timeout''': '''pytest-timeout''', '''pytest-xdist''': '''pytest-xdist''', '''python''': '''python>=3.8.0''', '''ray[tune]''': '''ray[tune]''', '''regex''': '''regex!=2019.12.17''', '''requests''': '''requests''', '''rhoknp''': '''rhoknp>=1.1.0,<1.3.1''', '''rjieba''': '''rjieba''', '''rouge-score''': '''rouge-score!=0.0.7,!=0.0.8,!=0.1,!=0.1.1''', '''ruff''': '''ruff>=0.0.241,<=0.0.259''', '''sacrebleu''': '''sacrebleu>=1.4.12,<2.0.0''', '''sacremoses''': '''sacremoses''', '''safetensors''': '''safetensors>=0.3.1''', '''sagemaker''': '''sagemaker>=2.31.0''', '''scikit-learn''': '''scikit-learn''', '''sentencepiece''': '''sentencepiece>=0.1.91,!=0.1.92''', '''sigopt''': '''sigopt''', '''starlette''': '''starlette''', '''sudachipy''': '''sudachipy>=0.6.6''', '''sudachidict_core''': '''sudachidict_core>=20220729''', '''tensorflow-cpu''': '''tensorflow-cpu>=2.6,<2.14''', '''tensorflow''': '''tensorflow>=2.6,<2.14''', '''tensorflow-text''': '''tensorflow-text<2.14''', '''tf2onnx''': '''tf2onnx''', '''timeout-decorator''': '''timeout-decorator''', '''timm''': '''timm''', '''tokenizers''': '''tokenizers>=0.11.1,!=0.11.3,<0.14''', '''torch''': '''torch>=1.9,!=1.12.0''', '''torchaudio''': '''torchaudio''', '''torchvision''': '''torchvision''', '''pyctcdecode''': '''pyctcdecode>=0.4.0''', '''tqdm''': '''tqdm>=4.27''', '''unidic''': '''unidic>=1.0.2''', '''unidic_lite''': '''unidic_lite>=1.0.7''', '''urllib3''': '''urllib3<2.0.0''', '''uvicorn''': '''uvicorn''', }
182
import json import os import shutil import tempfile import unittest import numpy as np import pytest from transformers import BertTokenizer, BertTokenizerFast from transformers.models.bert.tokenization_bert import VOCAB_FILES_NAMES from transformers.testing_utils import require_vision from transformers.utils import FEATURE_EXTRACTOR_NAME, is_vision_available if is_vision_available(): from PIL import Image from transformers import ChineseCLIPImageProcessor, ChineseCLIPProcessor @require_vision class A__ ( unittest.TestCase ): def __UpperCamelCase ( self : int ) -> Any: """simple docstring""" _SCREAMING_SNAKE_CASE =tempfile.mkdtemp() _SCREAMING_SNAKE_CASE =[ '''[UNK]''', '''[CLS]''', '''[SEP]''', '''[PAD]''', '''[MASK]''', '''็š„''', '''ไปท''', '''ๆ ผ''', '''ๆ˜ฏ''', '''15''', '''ไพฟ''', '''alex''', '''##andra''', '''๏ผŒ''', '''ใ€‚''', '''-''', '''t''', '''shirt''', ] _SCREAMING_SNAKE_CASE =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] ) ) _SCREAMING_SNAKE_CASE ={ '''do_resize''': True, '''size''': {'''height''': 224, '''width''': 224}, '''do_center_crop''': True, '''crop_size''': {'''height''': 18, '''width''': 18}, '''do_normalize''': True, '''image_mean''': [0.48_14_54_66, 0.4_57_82_75, 0.40_82_10_73], '''image_std''': [0.26_86_29_54, 0.26_13_02_58, 0.27_57_77_11], '''do_convert_rgb''': True, } _SCREAMING_SNAKE_CASE =os.path.join(self.tmpdirname , _a ) with open(self.image_processor_file , '''w''' , encoding='''utf-8''' ) as fp: json.dump(_a , _a ) def __UpperCamelCase ( self : Optional[int] , **_a : str ) -> List[str]: """simple docstring""" return BertTokenizer.from_pretrained(self.tmpdirname , **_a ) def __UpperCamelCase ( self : List[Any] , **_a : Any ) -> Dict: """simple docstring""" return BertTokenizerFast.from_pretrained(self.tmpdirname , **_a ) def __UpperCamelCase ( self : int , **_a : Optional[Any] ) -> Any: """simple docstring""" return ChineseCLIPImageProcessor.from_pretrained(self.tmpdirname , **_a ) def __UpperCamelCase ( self : str ) -> Union[str, Any]: """simple docstring""" shutil.rmtree(self.tmpdirname ) def __UpperCamelCase ( self : int ) -> Optional[Any]: """simple docstring""" _SCREAMING_SNAKE_CASE =[np.random.randint(255 , size=(3, 30, 400) , dtype=np.uinta )] _SCREAMING_SNAKE_CASE =[Image.fromarray(np.moveaxis(_a , 0 , -1 ) ) for x in image_inputs] return image_inputs def __UpperCamelCase ( self : Any ) -> List[Any]: """simple docstring""" _SCREAMING_SNAKE_CASE =self.get_tokenizer() _SCREAMING_SNAKE_CASE =self.get_rust_tokenizer() _SCREAMING_SNAKE_CASE =self.get_image_processor() _SCREAMING_SNAKE_CASE =ChineseCLIPProcessor(tokenizer=_a , image_processor=_a ) processor_slow.save_pretrained(self.tmpdirname ) _SCREAMING_SNAKE_CASE =ChineseCLIPProcessor.from_pretrained(self.tmpdirname , use_fast=_a ) _SCREAMING_SNAKE_CASE =ChineseCLIPProcessor(tokenizer=_a , image_processor=_a ) processor_fast.save_pretrained(self.tmpdirname ) _SCREAMING_SNAKE_CASE =ChineseCLIPProcessor.from_pretrained(self.tmpdirname ) self.assertEqual(processor_slow.tokenizer.get_vocab() , tokenizer_slow.get_vocab() ) self.assertEqual(processor_fast.tokenizer.get_vocab() , tokenizer_fast.get_vocab() ) self.assertEqual(tokenizer_slow.get_vocab() , tokenizer_fast.get_vocab() ) self.assertIsInstance(processor_slow.tokenizer , _a ) self.assertIsInstance(processor_fast.tokenizer , _a ) self.assertEqual(processor_slow.image_processor.to_json_string() , image_processor.to_json_string() ) self.assertEqual(processor_fast.image_processor.to_json_string() , image_processor.to_json_string() ) self.assertIsInstance(processor_slow.image_processor , _a ) self.assertIsInstance(processor_fast.image_processor , _a ) def __UpperCamelCase ( self : str ) -> Union[str, Any]: """simple docstring""" _SCREAMING_SNAKE_CASE =ChineseCLIPProcessor(tokenizer=self.get_tokenizer() , image_processor=self.get_image_processor() ) processor.save_pretrained(self.tmpdirname ) _SCREAMING_SNAKE_CASE =self.get_tokenizer(cls_token='''(CLS)''' , sep_token='''(SEP)''' ) _SCREAMING_SNAKE_CASE =self.get_image_processor(do_normalize=_a ) _SCREAMING_SNAKE_CASE =ChineseCLIPProcessor.from_pretrained( self.tmpdirname , cls_token='''(CLS)''' , sep_token='''(SEP)''' , do_normalize=_a ) self.assertEqual(processor.tokenizer.get_vocab() , tokenizer_add_kwargs.get_vocab() ) self.assertIsInstance(processor.tokenizer , _a ) self.assertEqual(processor.image_processor.to_json_string() , image_processor_add_kwargs.to_json_string() ) self.assertIsInstance(processor.image_processor , _a ) def __UpperCamelCase ( self : List[Any] ) -> Tuple: """simple docstring""" _SCREAMING_SNAKE_CASE =self.get_image_processor() _SCREAMING_SNAKE_CASE =self.get_tokenizer() _SCREAMING_SNAKE_CASE =ChineseCLIPProcessor(tokenizer=_a , image_processor=_a ) _SCREAMING_SNAKE_CASE =self.prepare_image_inputs() _SCREAMING_SNAKE_CASE =image_processor(_a , return_tensors='''np''' ) _SCREAMING_SNAKE_CASE =processor(images=_a , return_tensors='''np''' ) for key in input_feat_extract.keys(): self.assertAlmostEqual(input_feat_extract[key].sum() , input_processor[key].sum() , delta=1E-2 ) def __UpperCamelCase ( self : Union[str, Any] ) -> Any: """simple docstring""" _SCREAMING_SNAKE_CASE =self.get_image_processor() _SCREAMING_SNAKE_CASE =self.get_tokenizer() _SCREAMING_SNAKE_CASE =ChineseCLIPProcessor(tokenizer=_a , image_processor=_a ) _SCREAMING_SNAKE_CASE ='''Alexandra๏ผŒT-shirt็š„ไปทๆ ผๆ˜ฏ15ไพฟๅฃซใ€‚''' _SCREAMING_SNAKE_CASE =processor(text=_a ) _SCREAMING_SNAKE_CASE =tokenizer(_a ) for key in encoded_tok.keys(): self.assertListEqual(encoded_tok[key] , encoded_processor[key] ) def __UpperCamelCase ( self : Tuple ) -> int: """simple docstring""" _SCREAMING_SNAKE_CASE =self.get_image_processor() _SCREAMING_SNAKE_CASE =self.get_tokenizer() _SCREAMING_SNAKE_CASE =ChineseCLIPProcessor(tokenizer=_a , image_processor=_a ) _SCREAMING_SNAKE_CASE ='''Alexandra๏ผŒT-shirt็š„ไปทๆ ผๆ˜ฏ15ไพฟๅฃซใ€‚''' _SCREAMING_SNAKE_CASE =self.prepare_image_inputs() _SCREAMING_SNAKE_CASE =processor(text=_a , images=_a ) self.assertListEqual(list(inputs.keys() ) , ['''input_ids''', '''token_type_ids''', '''attention_mask''', '''pixel_values'''] ) # test if it raises when no input is passed with pytest.raises(_a ): processor() def __UpperCamelCase ( self : Optional[Any] ) -> Optional[int]: """simple docstring""" _SCREAMING_SNAKE_CASE =self.get_image_processor() _SCREAMING_SNAKE_CASE =self.get_tokenizer() _SCREAMING_SNAKE_CASE =ChineseCLIPProcessor(tokenizer=_a , image_processor=_a ) _SCREAMING_SNAKE_CASE =[[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]] _SCREAMING_SNAKE_CASE =processor.batch_decode(_a ) _SCREAMING_SNAKE_CASE =tokenizer.batch_decode(_a ) self.assertListEqual(_a , _a ) def __UpperCamelCase ( self : Any ) -> List[str]: """simple docstring""" _SCREAMING_SNAKE_CASE =self.get_image_processor() _SCREAMING_SNAKE_CASE =self.get_tokenizer() _SCREAMING_SNAKE_CASE =ChineseCLIPProcessor(tokenizer=_a , image_processor=_a ) _SCREAMING_SNAKE_CASE ='''Alexandra๏ผŒT-shirt็š„ไปทๆ ผๆ˜ฏ15ไพฟๅฃซใ€‚''' _SCREAMING_SNAKE_CASE =self.prepare_image_inputs() _SCREAMING_SNAKE_CASE =processor(text=_a , images=_a ) self.assertListEqual(list(inputs.keys() ) , processor.model_input_names )
691
0
'''simple docstring''' def _lowerCamelCase ( lowercase : int , lowercase : int , lowercase : int ) -> float: _a = (num_of_terms / 2) * (2 * first_term + (num_of_terms - 1) * common_diff) # formula for sum of series return total def _lowerCamelCase ( ) -> Tuple: print(sum_of_series(1 , 1 , 10 ) ) if __name__ == "__main__": import doctest doctest.testmod()
692
'''simple docstring''' from ...configuration_utils import PretrainedConfig from ...utils import logging lowerCAmelCase_ : Optional[int] = logging.get_logger(__name__) lowerCAmelCase_ : str = { 'facebook/timesformer': 'https://huggingface.co/facebook/timesformer/resolve/main/config.json', } class __SCREAMING_SNAKE_CASE (lowerCamelCase_ ): """simple docstring""" __a ='timesformer' def __init__( self : Optional[int] , __a : Optional[int]=2_24 , __a : Tuple=16 , __a : int=3 , __a : Union[str, Any]=8 , __a : Union[str, Any]=7_68 , __a : List[str]=12 , __a : Union[str, Any]=12 , __a : Optional[Any]=30_72 , __a : Tuple="gelu" , __a : str=0.0 , __a : List[Any]=0.0 , __a : Any=0.02 , __a : List[str]=1e-6 , __a : Any=True , __a : Union[str, Any]="divided_space_time" , __a : str=0 , **__a : Tuple , ): super().__init__(**__a ) _a = image_size _a = patch_size _a = num_channels _a = num_frames _a = hidden_size _a = num_hidden_layers _a = num_attention_heads _a = intermediate_size _a = hidden_act _a = hidden_dropout_prob _a = attention_probs_dropout_prob _a = initializer_range _a = layer_norm_eps _a = qkv_bias _a = attention_type _a = drop_path_rate
692
1
'''simple docstring''' from datasets.utils.patching import _PatchedModuleObj, patch_submodule from . import _test_patching def _lowerCamelCase ( ) -> Optional[int]: import os as original_os from os import path as original_path from os import rename as original_rename from os.path import dirname as original_dirname from os.path import join as original_join assert _test_patching.os is original_os assert _test_patching.path is original_path assert _test_patching.join is original_join assert _test_patching.renamed_os is original_os assert _test_patching.renamed_path is original_path assert _test_patching.renamed_join is original_join _a = "__test_patch_submodule_mock__" with patch_submodule(_test_patching , "os.path.join" , lowercase ): # Every way to access os.path.join must be patched, and the rest must stay untouched # check os.path.join assert isinstance(_test_patching.os , _PatchedModuleObj ) assert isinstance(_test_patching.os.path , _PatchedModuleObj ) assert _test_patching.os.path.join is mock # check path.join assert isinstance(_test_patching.path , _PatchedModuleObj ) assert _test_patching.path.join is mock # check join assert _test_patching.join is mock # check that the other attributes are untouched assert _test_patching.os.rename is original_rename assert _test_patching.path.dirname is original_dirname assert _test_patching.os.path.dirname is original_dirname # Even renamed modules or objects must be patched # check renamed_os.path.join assert isinstance(_test_patching.renamed_os , _PatchedModuleObj ) assert isinstance(_test_patching.renamed_os.path , _PatchedModuleObj ) assert _test_patching.renamed_os.path.join is mock # check renamed_path.join assert isinstance(_test_patching.renamed_path , _PatchedModuleObj ) assert _test_patching.renamed_path.join is mock # check renamed_join assert _test_patching.renamed_join is mock # check that the other attributes are untouched assert _test_patching.renamed_os.rename is original_rename assert _test_patching.renamed_path.dirname is original_dirname assert _test_patching.renamed_os.path.dirname is original_dirname # check that everthing is back to normal when the patch is over assert _test_patching.os is original_os assert _test_patching.path is original_path assert _test_patching.join is original_join assert _test_patching.renamed_os is original_os assert _test_patching.renamed_path is original_path assert _test_patching.renamed_join is original_join def _lowerCamelCase ( ) -> Union[str, Any]: assert _test_patching.open is open _a = "__test_patch_submodule_builtin_mock__" # _test_patching has "open" in its globals assert _test_patching.open is open with patch_submodule(_test_patching , "open" , lowercase ): assert _test_patching.open is mock # check that everthing is back to normal when the patch is over assert _test_patching.open is open def _lowerCamelCase ( ) -> Optional[Any]: # pandas.read_csv is not present in _test_patching _a = "__test_patch_submodule_missing_mock__" with patch_submodule(_test_patching , "pandas.read_csv" , lowercase ): pass def _lowerCamelCase ( ) -> int: # builtin should always be mocked even if they're not in the globals # in case they're loaded at one point _a = "__test_patch_submodule_missing_builtin_mock__" # _test_patching doesn't have "len" in its globals assert getattr(_test_patching , "len" , lowercase ) is None with patch_submodule(_test_patching , "len" , lowercase ): assert _test_patching.len is mock assert _test_patching.len is len def _lowerCamelCase ( ) -> Dict: _a = "__test_patch_submodule_start_and_stop_mock__" _a = patch_submodule(_test_patching , "open" , lowercase ) assert _test_patching.open is open patch.start() assert _test_patching.open is mock patch.stop() assert _test_patching.open is open def _lowerCamelCase ( ) -> List[Any]: from os import rename as original_rename from os.path import dirname as original_dirname from os.path import join as original_join _a = "__test_patch_submodule_successive_join__" _a = "__test_patch_submodule_successive_dirname__" _a = "__test_patch_submodule_successive_rename__" assert _test_patching.os.path.join is original_join assert _test_patching.os.path.dirname is original_dirname assert _test_patching.os.rename is original_rename with patch_submodule(_test_patching , "os.path.join" , lowercase ): with patch_submodule(_test_patching , "os.rename" , lowercase ): with patch_submodule(_test_patching , "os.path.dirname" , lowercase ): assert _test_patching.os.path.join is mock_join assert _test_patching.os.path.dirname is mock_dirname assert _test_patching.os.rename is mock_rename # try another order with patch_submodule(_test_patching , "os.rename" , lowercase ): with patch_submodule(_test_patching , "os.path.join" , lowercase ): with patch_submodule(_test_patching , "os.path.dirname" , lowercase ): assert _test_patching.os.path.join is mock_join assert _test_patching.os.path.dirname is mock_dirname assert _test_patching.os.rename is mock_rename assert _test_patching.os.path.join is original_join assert _test_patching.os.path.dirname is original_dirname assert _test_patching.os.rename is original_rename def _lowerCamelCase ( ) -> Optional[int]: _a = "__test_patch_submodule_doesnt_exist_mock__" with patch_submodule(_test_patching , "__module_that_doesn_exist__.__attribute_that_doesn_exist__" , lowercase ): pass with patch_submodule(_test_patching , "os.__attribute_that_doesn_exist__" , lowercase ): pass
692
'''simple docstring''' import gc import random import unittest import numpy as np import torch from transformers import XLMRobertaTokenizer from diffusers import ( AltDiffusionImgaImgPipeline, AutoencoderKL, PNDMScheduler, UNetaDConditionModel, ) from diffusers.image_processor import VaeImageProcessor from diffusers.pipelines.alt_diffusion.modeling_roberta_series import ( RobertaSeriesConfig, RobertaSeriesModelWithTransformation, ) from diffusers.utils import floats_tensor, load_image, load_numpy, slow, torch_device from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu enable_full_determinism() class __SCREAMING_SNAKE_CASE (unittest.TestCase ): """simple docstring""" def UpperCamelCase__ ( self : Optional[int] ): # clean up the VRAM after each test super().tearDown() gc.collect() torch.cuda.empty_cache() @property def UpperCamelCase__ ( self : Dict ): _a = 1 _a = 3 _a = (32, 32) _a = floats_tensor((batch_size, num_channels) + sizes , rng=random.Random(0 ) ).to(__a ) return image @property def UpperCamelCase__ ( self : Dict ): torch.manual_seed(0 ) _a = UNetaDConditionModel( block_out_channels=(32, 64) , layers_per_block=2 , sample_size=32 , in_channels=4 , out_channels=4 , down_block_types=("DownBlock2D", "CrossAttnDownBlock2D") , up_block_types=("CrossAttnUpBlock2D", "UpBlock2D") , cross_attention_dim=32 , ) return model @property def UpperCamelCase__ ( self : Optional[int] ): torch.manual_seed(0 ) _a = AutoencoderKL( block_out_channels=[32, 64] , in_channels=3 , out_channels=3 , down_block_types=["DownEncoderBlock2D", "DownEncoderBlock2D"] , up_block_types=["UpDecoderBlock2D", "UpDecoderBlock2D"] , latent_channels=4 , ) return model @property def UpperCamelCase__ ( self : Optional[Any] ): torch.manual_seed(0 ) _a = RobertaSeriesConfig( hidden_size=32 , project_dim=32 , intermediate_size=37 , layer_norm_eps=1e-0_5 , num_attention_heads=4 , num_hidden_layers=5 , pad_token_id=1 , vocab_size=50_06 , ) return RobertaSeriesModelWithTransformation(__a ) @property def UpperCamelCase__ ( self : str ): def extract(*__a : Tuple , **__a : str ): class __SCREAMING_SNAKE_CASE : """simple docstring""" def __init__( self : Dict ): _a = torch.ones([0] ) def UpperCamelCase__ ( self : List[str] , __a : Dict ): self.pixel_values.to(__a ) return self return Out() return extract def UpperCamelCase__ ( self : Optional[int] ): _a = "cpu" # ensure determinism for the device-dependent torch.Generator _a = self.dummy_cond_unet _a = PNDMScheduler(skip_prk_steps=__a ) _a = self.dummy_vae _a = self.dummy_text_encoder _a = XLMRobertaTokenizer.from_pretrained("hf-internal-testing/tiny-xlm-roberta" ) _a = 77 _a = self.dummy_image.to(__a ) _a = init_image / 2 + 0.5 # make sure here that pndm scheduler skips prk _a = AltDiffusionImgaImgPipeline( unet=__a , scheduler=__a , vae=__a , text_encoder=__a , tokenizer=__a , safety_checker=__a , feature_extractor=self.dummy_extractor , ) _a = VaeImageProcessor(vae_scale_factor=alt_pipe.vae_scale_factor , do_normalize=__a ) _a = alt_pipe.to(__a ) alt_pipe.set_progress_bar_config(disable=__a ) _a = "A painting of a squirrel eating a burger" _a = torch.Generator(device=__a ).manual_seed(0 ) _a = alt_pipe( [prompt] , generator=__a , guidance_scale=6.0 , num_inference_steps=2 , output_type="np" , image=__a , ) _a = output.images _a = torch.Generator(device=__a ).manual_seed(0 ) _a = alt_pipe( [prompt] , generator=__a , guidance_scale=6.0 , num_inference_steps=2 , output_type="np" , image=__a , return_dict=__a , )[0] _a = image[0, -3:, -3:, -1] _a = image_from_tuple[0, -3:, -3:, -1] assert image.shape == (1, 32, 32, 3) _a = np.array([0.4427, 0.3731, 0.4249, 0.4941, 0.4546, 0.4148, 0.4193, 0.4666, 0.4499] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 5e-3 assert np.abs(image_from_tuple_slice.flatten() - expected_slice ).max() < 5e-3 @unittest.skipIf(torch_device != "cuda" , "This test requires a GPU" ) def UpperCamelCase__ ( self : Optional[int] ): _a = self.dummy_cond_unet _a = PNDMScheduler(skip_prk_steps=__a ) _a = self.dummy_vae _a = self.dummy_text_encoder _a = XLMRobertaTokenizer.from_pretrained("hf-internal-testing/tiny-xlm-roberta" ) _a = 77 _a = self.dummy_image.to(__a ) # put models in fp16 _a = unet.half() _a = vae.half() _a = bert.half() # make sure here that pndm scheduler skips prk _a = AltDiffusionImgaImgPipeline( unet=__a , scheduler=__a , vae=__a , text_encoder=__a , tokenizer=__a , safety_checker=__a , feature_extractor=self.dummy_extractor , ) _a = VaeImageProcessor(vae_scale_factor=alt_pipe.vae_scale_factor , do_normalize=__a ) _a = alt_pipe.to(__a ) alt_pipe.set_progress_bar_config(disable=__a ) _a = "A painting of a squirrel eating a burger" _a = torch.manual_seed(0 ) _a = alt_pipe( [prompt] , generator=__a , num_inference_steps=2 , output_type="np" , image=__a , ).images assert image.shape == (1, 32, 32, 3) @unittest.skipIf(torch_device != "cuda" , "This test requires a GPU" ) def UpperCamelCase__ ( self : Optional[Any] ): _a = load_image( "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main" "/img2img/sketch-mountains-input.jpg" ) # resize to resolution that is divisible by 8 but not 16 or 32 _a = init_image.resize((7_60, 5_04) ) _a = "BAAI/AltDiffusion" _a = AltDiffusionImgaImgPipeline.from_pretrained( __a , safety_checker=__a , ) pipe.to(__a ) pipe.set_progress_bar_config(disable=__a ) pipe.enable_attention_slicing() _a = "A fantasy landscape, trending on artstation" _a = torch.manual_seed(0 ) _a = pipe( prompt=__a , image=__a , strength=0.75 , guidance_scale=7.5 , generator=__a , output_type="np" , ) _a = output.images[0] _a = image[2_55:2_58, 3_83:3_86, -1] assert image.shape == (5_04, 7_60, 3) _a = np.array([0.9358, 0.9397, 0.9599, 0.9901, 1.0000, 1.0000, 0.9882, 1.0000, 1.0000] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-2 @slow @require_torch_gpu class __SCREAMING_SNAKE_CASE (unittest.TestCase ): """simple docstring""" def UpperCamelCase__ ( self : Dict ): # clean up the VRAM after each test super().tearDown() gc.collect() torch.cuda.empty_cache() def UpperCamelCase__ ( self : Union[str, Any] ): _a = load_image( "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main" "/img2img/sketch-mountains-input.jpg" ) _a = init_image.resize((7_68, 5_12) ) _a = load_numpy( "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/img2img/fantasy_landscape_alt.npy" ) _a = "BAAI/AltDiffusion" _a = AltDiffusionImgaImgPipeline.from_pretrained( __a , safety_checker=__a , ) pipe.to(__a ) pipe.set_progress_bar_config(disable=__a ) pipe.enable_attention_slicing() _a = "A fantasy landscape, trending on artstation" _a = torch.manual_seed(0 ) _a = pipe( prompt=__a , image=__a , strength=0.75 , guidance_scale=7.5 , generator=__a , output_type="np" , ) _a = output.images[0] assert image.shape == (5_12, 7_68, 3) # img2img is flaky across GPUs even in fp32, so using MAE here assert np.abs(expected_image - image ).max() < 1e-2
692
1
'''simple docstring''' import shutil import tempfile import unittest import numpy as np from transformers.testing_utils import ( is_pt_tf_cross_test, require_tf, require_torch, require_torchvision, require_vision, ) from transformers.utils import is_tf_available, is_torch_available, is_vision_available if is_vision_available(): from PIL import Image from transformers import AutoProcessor, SamImageProcessor, SamProcessor if is_torch_available(): import torch if is_tf_available(): import tensorflow as tf @require_vision @require_torchvision class __SCREAMING_SNAKE_CASE (unittest.TestCase ): """simple docstring""" def UpperCamelCase__ ( self : List[str] ): _a = tempfile.mkdtemp() _a = SamImageProcessor() _a = SamProcessor(__a ) processor.save_pretrained(self.tmpdirname ) def UpperCamelCase__ ( self : Union[str, Any] , **__a : Optional[Any] ): return AutoProcessor.from_pretrained(self.tmpdirname , **__a ).image_processor def UpperCamelCase__ ( self : Optional[Any] ): shutil.rmtree(self.tmpdirname ) def UpperCamelCase__ ( self : List[Any] ): _a = [np.random.randint(2_55 , size=(3, 30, 4_00) , dtype=np.uinta )] _a = [Image.fromarray(np.moveaxis(__a , 0 , -1 ) ) for x in image_inputs] return image_inputs def UpperCamelCase__ ( self : List[str] ): _a = SamProcessor(image_processor=self.get_image_processor() ) processor.save_pretrained(self.tmpdirname ) _a = self.get_image_processor(do_normalize=__a , padding_value=1.0 ) _a = SamProcessor.from_pretrained(self.tmpdirname , do_normalize=__a , padding_value=1.0 ) self.assertEqual(processor.image_processor.to_json_string() , image_processor_add_kwargs.to_json_string() ) self.assertIsInstance(processor.image_processor , __a ) def UpperCamelCase__ ( self : Any ): _a = self.get_image_processor() _a = SamProcessor(image_processor=__a ) _a = self.prepare_image_inputs() _a = image_processor(__a , return_tensors="np" ) _a = processor(images=__a , return_tensors="np" ) input_feat_extract.pop("original_sizes" ) # pop original_sizes as it is popped in the processor input_feat_extract.pop("reshaped_input_sizes" ) # pop original_sizes as it is popped in the processor for key in input_feat_extract.keys(): self.assertAlmostEqual(input_feat_extract[key].sum() , input_processor[key].sum() , delta=1e-2 ) @require_torch def UpperCamelCase__ ( self : Optional[Any] ): _a = self.get_image_processor() _a = SamProcessor(image_processor=__a ) _a = [torch.ones((1, 3, 5, 5) )] _a = [[17_64, 26_46]] _a = [[6_83, 10_24]] _a = processor.post_process_masks(__a , __a , __a ) self.assertEqual(masks[0].shape , (1, 3, 17_64, 26_46) ) _a = processor.post_process_masks( __a , torch.tensor(__a ) , torch.tensor(__a ) ) self.assertEqual(masks[0].shape , (1, 3, 17_64, 26_46) ) # should also work with np _a = [np.ones((1, 3, 5, 5) )] _a = processor.post_process_masks(__a , np.array(__a ) , np.array(__a ) ) self.assertEqual(masks[0].shape , (1, 3, 17_64, 26_46) ) _a = [[1, 0], [0, 1]] with self.assertRaises(__a ): _a = processor.post_process_masks(__a , np.array(__a ) , np.array(__a ) ) @require_vision @require_tf class __SCREAMING_SNAKE_CASE (unittest.TestCase ): """simple docstring""" def UpperCamelCase__ ( self : Optional[Any] ): _a = tempfile.mkdtemp() _a = SamImageProcessor() _a = SamProcessor(__a ) processor.save_pretrained(self.tmpdirname ) def UpperCamelCase__ ( self : Union[str, Any] , **__a : Optional[int] ): return AutoProcessor.from_pretrained(self.tmpdirname , **__a ).image_processor def UpperCamelCase__ ( self : List[str] ): shutil.rmtree(self.tmpdirname ) def UpperCamelCase__ ( self : List[str] ): _a = [np.random.randint(2_55 , size=(3, 30, 4_00) , dtype=np.uinta )] _a = [Image.fromarray(np.moveaxis(__a , 0 , -1 ) ) for x in image_inputs] return image_inputs def UpperCamelCase__ ( self : Optional[Any] ): _a = SamProcessor(image_processor=self.get_image_processor() ) processor.save_pretrained(self.tmpdirname ) _a = self.get_image_processor(do_normalize=__a , padding_value=1.0 ) _a = SamProcessor.from_pretrained(self.tmpdirname , do_normalize=__a , padding_value=1.0 ) self.assertEqual(processor.image_processor.to_json_string() , image_processor_add_kwargs.to_json_string() ) self.assertIsInstance(processor.image_processor , __a ) def UpperCamelCase__ ( self : List[Any] ): _a = self.get_image_processor() _a = SamProcessor(image_processor=__a ) _a = self.prepare_image_inputs() _a = image_processor(__a , return_tensors="np" ) _a = processor(images=__a , return_tensors="np" ) input_feat_extract.pop("original_sizes" ) # pop original_sizes as it is popped in the processor input_feat_extract.pop("reshaped_input_sizes" ) # pop reshaped_input_sizes as it is popped in the processor for key in input_feat_extract.keys(): self.assertAlmostEqual(input_feat_extract[key].sum() , input_processor[key].sum() , delta=1e-2 ) @require_tf def UpperCamelCase__ ( self : Optional[int] ): _a = self.get_image_processor() _a = SamProcessor(image_processor=__a ) _a = [tf.ones((1, 3, 5, 5) )] _a = [[17_64, 26_46]] _a = [[6_83, 10_24]] _a = processor.post_process_masks(__a , __a , __a , return_tensors="tf" ) self.assertEqual(masks[0].shape , (1, 3, 17_64, 26_46) ) _a = processor.post_process_masks( __a , tf.convert_to_tensor(__a ) , tf.convert_to_tensor(__a ) , return_tensors="tf" , ) self.assertEqual(masks[0].shape , (1, 3, 17_64, 26_46) ) # should also work with np _a = [np.ones((1, 3, 5, 5) )] _a = processor.post_process_masks( __a , np.array(__a ) , np.array(__a ) , return_tensors="tf" ) self.assertEqual(masks[0].shape , (1, 3, 17_64, 26_46) ) _a = [[1, 0], [0, 1]] with self.assertRaises(tf.errors.InvalidArgumentError ): _a = processor.post_process_masks( __a , np.array(__a ) , np.array(__a ) , return_tensors="tf" ) @require_vision @require_torchvision class __SCREAMING_SNAKE_CASE (unittest.TestCase ): """simple docstring""" def UpperCamelCase__ ( self : Optional[int] ): _a = tempfile.mkdtemp() _a = SamImageProcessor() _a = SamProcessor(__a ) processor.save_pretrained(self.tmpdirname ) def UpperCamelCase__ ( self : List[Any] , **__a : Dict ): return AutoProcessor.from_pretrained(self.tmpdirname , **__a ).image_processor def UpperCamelCase__ ( self : List[Any] ): shutil.rmtree(self.tmpdirname ) def UpperCamelCase__ ( self : Tuple ): _a = [np.random.randint(2_55 , size=(3, 30, 4_00) , dtype=np.uinta )] _a = [Image.fromarray(np.moveaxis(__a , 0 , -1 ) ) for x in image_inputs] return image_inputs @is_pt_tf_cross_test def UpperCamelCase__ ( self : Optional[Any] ): _a = self.get_image_processor() _a = SamProcessor(image_processor=__a ) _a = np.random.randint(0 , 2 , size=(1, 3, 5, 5) ).astype(np.floataa ) _a = [tf.convert_to_tensor(__a )] _a = [torch.tensor(__a )] _a = [[17_64, 26_46]] _a = [[6_83, 10_24]] _a = processor.post_process_masks( __a , __a , __a , return_tensors="tf" ) _a = processor.post_process_masks( __a , __a , __a , return_tensors="pt" ) self.assertTrue(np.all(tf_masks[0].numpy() == pt_masks[0].numpy() ) ) @is_pt_tf_cross_test def UpperCamelCase__ ( self : Dict ): _a = self.get_image_processor() _a = SamProcessor(image_processor=__a ) _a = self.prepare_image_inputs() _a = image_processor(__a , return_tensors="pt" )["pixel_values"].numpy() _a = processor(images=__a , return_tensors="pt" )["pixel_values"].numpy() _a = image_processor(__a , return_tensors="tf" )["pixel_values"].numpy() _a = processor(images=__a , return_tensors="tf" )["pixel_values"].numpy() self.assertTrue(np.allclose(__a , __a ) ) self.assertTrue(np.allclose(__a , __a ) ) self.assertTrue(np.allclose(__a , __a ) )
692
'''simple docstring''' import warnings from ...utils import logging from .image_processing_dpt import DPTImageProcessor lowerCAmelCase_ : Optional[int] = logging.get_logger(__name__) class __SCREAMING_SNAKE_CASE (lowerCamelCase_ ): """simple docstring""" def __init__( self : int , *__a : Tuple , **__a : Optional[Any] ): warnings.warn( "The class DPTFeatureExtractor is deprecated and will be removed in version 5 of Transformers. Please" " use DPTImageProcessor instead." , __a , ) super().__init__(*__a , **__a )
692
1
'''simple docstring''' import torch from transformers import CamembertForMaskedLM, CamembertTokenizer def _lowerCamelCase ( lowercase : Optional[Any] , lowercase : int , lowercase : Dict , lowercase : int=5 ) -> Dict: # Adapted from https://github.com/pytorch/fairseq/blob/master/fairseq/models/roberta/hub_interface.py assert masked_input.count("<mask>" ) == 1 _a = torch.tensor(tokenizer.encode(lowercase , add_special_tokens=lowercase ) ).unsqueeze(0 ) # Batch size 1 _a = model(lowercase )[0] # The last hidden-state is the first element of the output tuple _a = (input_ids.squeeze() == tokenizer.mask_token_id).nonzero().item() _a = logits[0, masked_index, :] _a = logits.softmax(dim=0 ) _a , _a = prob.topk(k=lowercase , dim=0 ) _a = " ".join( [tokenizer.convert_ids_to_tokens(indices[i].item() ) for i in range(len(lowercase ) )] ) _a = tokenizer.mask_token _a = [] for index, predicted_token_bpe in enumerate(topk_predicted_token_bpe.split(" " ) ): _a = predicted_token_bpe.replace("\u2581" , " " ) if " {0}".format(lowercase ) in masked_input: topk_filled_outputs.append( ( masked_input.replace(" {0}".format(lowercase ) , lowercase ), values[index].item(), predicted_token, ) ) else: topk_filled_outputs.append( ( masked_input.replace(lowercase , lowercase ), values[index].item(), predicted_token, ) ) return topk_filled_outputs lowerCAmelCase_ : int = CamembertTokenizer.from_pretrained('camembert-base') lowerCAmelCase_ : Optional[Any] = CamembertForMaskedLM.from_pretrained('camembert-base') model.eval() lowerCAmelCase_ : Dict = 'Le camembert est <mask> :)' print(fill_mask(masked_input, model, tokenizer, topk=3))
692
'''simple docstring''' import logging from pathlib import Path import numpy as np import pytorch_lightning as pl import torch from pytorch_lightning.callbacks import EarlyStopping, ModelCheckpoint from pytorch_lightning.utilities import rank_zero_only from utils_rag import save_json def _lowerCamelCase ( lowercase : Any ) -> Tuple: _a = filter(lambda lowercase : p.requires_grad , model.parameters() ) _a = sum([np.prod(p.size() ) for p in model_parameters] ) return params lowerCAmelCase_ : str = logging.getLogger(__name__) def _lowerCamelCase ( lowercase : Optional[Any] , lowercase : Union[str, Any] ) -> Dict: if metric == "rouge2": _a = "{val_avg_rouge2:.4f}-{step_count}" elif metric == "bleu": _a = "{val_avg_bleu:.4f}-{step_count}" elif metric == "em": _a = "{val_avg_em:.4f}-{step_count}" else: raise NotImplementedError( F'seq2seq callbacks only support rouge2 and bleu, got {metric}, You can make your own by adding to this' " function." ) _a = ModelCheckpoint( dirpath=lowercase , filename=lowercase , monitor=F'val_{metric}' , mode="max" , save_top_k=3 , every_n_epochs=1 , ) return checkpoint_callback def _lowerCamelCase ( lowercase : Optional[int] , lowercase : List[str] ) -> Dict: return EarlyStopping( monitor=F'val_{metric}' , mode="min" if "loss" in metric else "max" , patience=lowercase , verbose=lowercase , ) class __SCREAMING_SNAKE_CASE (pl.Callback ): """simple docstring""" def UpperCamelCase__ ( self : Optional[Any] , __a : Dict , __a : Optional[int] ): _a = {f'lr_group_{i}': param["lr"] for i, param in enumerate(pl_module.trainer.optimizers[0].param_groups )} pl_module.logger.log_metrics(__a ) @rank_zero_only def UpperCamelCase__ ( self : Any , __a : pl.Trainer , __a : pl.LightningModule , __a : str , __a : Optional[int]=True ): logger.info(f'***** {type_path} results at step {trainer.global_step:05d} *****' ) _a = trainer.callback_metrics trainer.logger.log_metrics({k: v for k, v in metrics.items() if k not in ["log", "progress_bar", "preds"]} ) # Log results _a = Path(pl_module.hparams.output_dir ) if type_path == "test": _a = od / "test_results.txt" _a = od / "test_generations.txt" else: # this never gets hit. I prefer not to save intermediate generations, and results are in metrics.json # If people want this it will be easy enough to add back. _a = od / f'{type_path}_results/{trainer.global_step:05d}.txt' _a = od / f'{type_path}_generations/{trainer.global_step:05d}.txt' results_file.parent.mkdir(exist_ok=__a ) generations_file.parent.mkdir(exist_ok=__a ) with open(__a , "a+" ) as writer: for key in sorted(__a ): if key in ["log", "progress_bar", "preds"]: continue _a = metrics[key] if isinstance(__a , torch.Tensor ): _a = val.item() _a = f'{key}: {val:.6f}\n' writer.write(__a ) if not save_generations: return if "preds" in metrics: _a = "\n".join(metrics["preds"] ) generations_file.open("w+" ).write(__a ) @rank_zero_only def UpperCamelCase__ ( self : List[str] , __a : Optional[Any] , __a : List[str] ): try: _a = pl_module.model.model.num_parameters() except AttributeError: _a = pl_module.model.num_parameters() _a = count_trainable_parameters(__a ) # mp stands for million parameters trainer.logger.log_metrics({"n_params": npars, "mp": npars / 1e6, "grad_mp": n_trainable_pars / 1e6} ) @rank_zero_only def UpperCamelCase__ ( self : Dict , __a : pl.Trainer , __a : pl.LightningModule ): save_json(pl_module.metrics , pl_module.metrics_save_path ) return self._write_logs(__a , __a , "test" ) @rank_zero_only def UpperCamelCase__ ( self : Any , __a : pl.Trainer , __a : Optional[int] ): save_json(pl_module.metrics , pl_module.metrics_save_path ) # Uncommenting this will save val generations # return self._write_logs(trainer, pl_module, "valid")
692
1
'''simple docstring''' from copy import deepcopy from typing import Optional, Union import numpy as np from ...processing_utils import ProcessorMixin from ...tokenization_utils_base import BatchEncoding from ...utils import TensorType, is_tf_available, is_torch_available if is_torch_available(): import torch if is_tf_available(): import tensorflow as tf class __SCREAMING_SNAKE_CASE (lowerCamelCase_ ): """simple docstring""" __a =['image_processor'] __a ='SamImageProcessor' def __init__( self : Any , __a : Tuple ): super().__init__(__a ) _a = self.image_processor _a = -10 _a = self.image_processor.size["longest_edge"] def __call__( self : Tuple , __a : Optional[int]=None , __a : List[str]=None , __a : Optional[int]=None , __a : Tuple=None , __a : Optional[Union[str, TensorType]] = None , **__a : Tuple , ): _a = self.image_processor( __a , return_tensors=__a , **__a , ) # pop arguments that are not used in the foward but used nevertheless _a = encoding_image_processor["original_sizes"] if hasattr(__a , "numpy" ): # Checks if Torch or TF tensor _a = original_sizes.numpy() _a , _a , _a = self._check_and_preprocess_points( input_points=__a , input_labels=__a , input_boxes=__a , ) _a = self._normalize_and_convert( __a , __a , input_points=__a , input_labels=__a , input_boxes=__a , return_tensors=__a , ) return encoding_image_processor def UpperCamelCase__ ( self : str , __a : Any , __a : Union[str, Any] , __a : List[str]=None , __a : int=None , __a : Any=None , __a : str="pt" , ): if input_points is not None: if len(__a ) != len(__a ): _a = [ self._normalize_coordinates(self.target_size , __a , original_sizes[0] ) for point in input_points ] else: _a = [ self._normalize_coordinates(self.target_size , __a , __a ) for point, original_size in zip(__a , __a ) ] # check that all arrays have the same shape if not all(point.shape == input_points[0].shape for point in input_points ): if input_labels is not None: _a , _a = self._pad_points_and_labels(__a , __a ) _a = np.array(__a ) if input_labels is not None: _a = np.array(__a ) if input_boxes is not None: if len(__a ) != len(__a ): _a = [ self._normalize_coordinates(self.target_size , __a , original_sizes[0] , is_bounding_box=__a ) for box in input_boxes ] else: _a = [ self._normalize_coordinates(self.target_size , __a , __a , is_bounding_box=__a ) for box, original_size in zip(__a , __a ) ] _a = np.array(__a ) if input_boxes is not None: if return_tensors == "pt": _a = torch.from_numpy(__a ) # boxes batch size of 1 by default _a = input_boxes.unsqueeze(1 ) if len(input_boxes.shape ) != 3 else input_boxes elif return_tensors == "tf": _a = tf.convert_to_tensor(__a ) # boxes batch size of 1 by default _a = tf.expand_dims(__a , 1 ) if len(input_boxes.shape ) != 3 else input_boxes encoding_image_processor.update({"input_boxes": input_boxes} ) if input_points is not None: if return_tensors == "pt": _a = torch.from_numpy(__a ) # point batch size of 1 by default _a = input_points.unsqueeze(1 ) if len(input_points.shape ) != 4 else input_points elif return_tensors == "tf": _a = tf.convert_to_tensor(__a ) # point batch size of 1 by default _a = tf.expand_dims(__a , 1 ) if len(input_points.shape ) != 4 else input_points encoding_image_processor.update({"input_points": input_points} ) if input_labels is not None: if return_tensors == "pt": _a = torch.from_numpy(__a ) # point batch size of 1 by default _a = input_labels.unsqueeze(1 ) if len(input_labels.shape ) != 3 else input_labels elif return_tensors == "tf": _a = tf.convert_to_tensor(__a ) # point batch size of 1 by default _a = tf.expand_dims(__a , 1 ) if len(input_labels.shape ) != 3 else input_labels encoding_image_processor.update({"input_labels": input_labels} ) return encoding_image_processor def UpperCamelCase__ ( self : str , __a : Tuple , __a : Any ): _a = max([point.shape[0] for point in input_points] ) _a = [] for i, point in enumerate(__a ): if point.shape[0] != expected_nb_points: _a = np.concatenate( [point, np.zeros((expected_nb_points - point.shape[0], 2) ) + self.point_pad_value] , axis=0 ) _a = np.append(input_labels[i] , [self.point_pad_value] ) processed_input_points.append(__a ) _a = processed_input_points return input_points, input_labels def UpperCamelCase__ ( self : List[str] , __a : int , __a : np.ndarray , __a : Optional[int] , __a : Optional[Any]=False ): _a , _a = original_size _a , _a = self.image_processor._get_preprocess_shape(__a , longest_edge=__a ) _a = deepcopy(__a ).astype(__a ) if is_bounding_box: _a = coords.reshape(-1 , 2 , 2 ) _a = coords[..., 0] * (new_w / old_w) _a = coords[..., 1] * (new_h / old_h) if is_bounding_box: _a = coords.reshape(-1 , 4 ) return coords def UpperCamelCase__ ( self : List[Any] , __a : List[Any]=None , __a : Optional[Any]=None , __a : Union[str, Any]=None , ): if input_points is not None: if hasattr(__a , "numpy" ): # Checks for TF or Torch tensor _a = input_points.numpy().tolist() if not isinstance(__a , __a ) or not isinstance(input_points[0] , __a ): raise ValueError("Input points must be a list of list of floating points." ) _a = [np.array(__a ) for input_point in input_points] else: _a = None if input_labels is not None: if hasattr(__a , "numpy" ): _a = input_labels.numpy().tolist() if not isinstance(__a , __a ) or not isinstance(input_labels[0] , __a ): raise ValueError("Input labels must be a list of list integers." ) _a = [np.array(__a ) for label in input_labels] else: _a = None if input_boxes is not None: if hasattr(__a , "numpy" ): _a = input_boxes.numpy().tolist() if ( not isinstance(__a , __a ) or not isinstance(input_boxes[0] , __a ) or not isinstance(input_boxes[0][0] , __a ) ): raise ValueError("Input boxes must be a list of list of list of floating points." ) _a = [np.array(__a ).astype(np.floataa ) for box in input_boxes] else: _a = None return input_points, input_labels, input_boxes @property def UpperCamelCase__ ( self : Dict ): _a = self.image_processor.model_input_names return list(dict.fromkeys(__a ) ) def UpperCamelCase__ ( self : List[Any] , *__a : List[Any] , **__a : List[str] ): return self.image_processor.post_process_masks(*__a , **__a )
692
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tokenizers_available, is_torch_available lowerCAmelCase_ : Any = { 'configuration_biogpt': ['BIOGPT_PRETRAINED_CONFIG_ARCHIVE_MAP', 'BioGptConfig'], 'tokenization_biogpt': ['BioGptTokenizer'], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase_ : List[str] = [ 'BIOGPT_PRETRAINED_MODEL_ARCHIVE_LIST', 'BioGptForCausalLM', 'BioGptForTokenClassification', 'BioGptForSequenceClassification', 'BioGptModel', 'BioGptPreTrainedModel', ] if TYPE_CHECKING: from .configuration_biogpt import BIOGPT_PRETRAINED_CONFIG_ARCHIVE_MAP, BioGptConfig from .tokenization_biogpt import BioGptTokenizer try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_biogpt import ( BIOGPT_PRETRAINED_MODEL_ARCHIVE_LIST, BioGptForCausalLM, BioGptForSequenceClassification, BioGptForTokenClassification, BioGptModel, BioGptPreTrainedModel, ) else: import sys lowerCAmelCase_ : str = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
692
1
'''simple docstring''' from __future__ import annotations def _lowerCamelCase ( lowercase : list[int] ) -> bool: return len(set(lowercase ) ) == len(lowercase ) if __name__ == "__main__": import doctest doctest.testmod()
692
'''simple docstring''' import gc import threading import time import psutil import torch class __SCREAMING_SNAKE_CASE : """simple docstring""" def __init__( self : List[Any] ): _a = psutil.Process() _a = False def UpperCamelCase__ ( self : Tuple ): _a = -1 while True: _a = max(self.process.memory_info().rss , self.cpu_memory_peak ) # can't sleep or will not catch the peak right (this comment is here on purpose) if not self.peak_monitoring: break def UpperCamelCase__ ( self : List[Any] ): _a = True _a = threading.Thread(target=self.peak_monitor ) _a = True self.thread.start() def UpperCamelCase__ ( self : Optional[int] ): _a = False self.thread.join() return self.cpu_memory_peak lowerCAmelCase_ : List[Any] = PeakCPUMemory() def _lowerCamelCase ( ) -> Tuple: # Time _a = {"time": time.time()} gc.collect() torch.cuda.empty_cache() # CPU mem _a = psutil.Process().memory_info().rss cpu_peak_tracker.start() # GPU mem for i in range(torch.cuda.device_count() ): _a = torch.cuda.memory_allocated(lowercase ) torch.cuda.reset_peak_memory_stats() return measures def _lowerCamelCase ( lowercase : Any ) -> int: # Time _a = {"time": time.time() - start_measures["time"]} gc.collect() torch.cuda.empty_cache() # CPU mem _a = (psutil.Process().memory_info().rss - start_measures["cpu"]) / 2**20 _a = (cpu_peak_tracker.stop() - start_measures["cpu"]) / 2**20 # GPU mem for i in range(torch.cuda.device_count() ): _a = (torch.cuda.memory_allocated(lowercase ) - start_measures[str(lowercase )]) / 2**20 _a = (torch.cuda.max_memory_allocated(lowercase ) - start_measures[str(lowercase )]) / 2**20 return measures def _lowerCamelCase ( lowercase : Optional[int] , lowercase : Dict ) -> str: print(F'{description}:' ) print(F'- Time: {measures["time"]:.2f}s' ) for i in range(torch.cuda.device_count() ): print(F'- GPU {i} allocated: {measures[str(lowercase )]:.2f}MiB' ) _a = measures[F'{i}-peak'] print(F'- GPU {i} peak: {peak:.2f}MiB' ) print(F'- CPU RAM allocated: {measures["cpu"]:.2f}MiB' ) print(F'- CPU RAM peak: {measures["cpu-peak"]:.2f}MiB' )
692
1
'''simple docstring''' class __SCREAMING_SNAKE_CASE : """simple docstring""" def __init__( self : Tuple ): _a = {} # Mapping from char to TrieNode _a = False def UpperCamelCase__ ( self : Tuple , __a : list[str] ): for word in words: self.insert(__a ) def UpperCamelCase__ ( self : Optional[Any] , __a : str ): _a = self for char in word: if char not in curr.nodes: _a = TrieNode() _a = curr.nodes[char] _a = True def UpperCamelCase__ ( self : str , __a : str ): _a = self for char in word: if char not in curr.nodes: return False _a = curr.nodes[char] return curr.is_leaf def UpperCamelCase__ ( self : Optional[Any] , __a : str ): def _delete(__a : TrieNode , __a : str , __a : int ) -> bool: if index == len(__a ): # If word does not exist if not curr.is_leaf: return False _a = False return len(curr.nodes ) == 0 _a = word[index] _a = curr.nodes.get(__a ) # If char not in current trie node if not char_node: return False # Flag to check if node can be deleted _a = _delete(__a , __a , index + 1 ) if delete_curr: del curr.nodes[char] return len(curr.nodes ) == 0 return delete_curr _delete(self , __a , 0 ) def _lowerCamelCase ( lowercase : TrieNode , lowercase : str ) -> None: if node.is_leaf: print(lowercase , end=" " ) for key, value in node.nodes.items(): print_words(lowercase , word + key ) def _lowerCamelCase ( ) -> bool: _a = "banana bananas bandana band apple all beast".split() _a = TrieNode() root.insert_many(lowercase ) # print_words(root, "") assert all(root.find(lowercase ) for word in words ) assert root.find("banana" ) assert not root.find("bandanas" ) assert not root.find("apps" ) assert root.find("apple" ) assert root.find("all" ) root.delete("all" ) assert not root.find("all" ) root.delete("banana" ) assert not root.find("banana" ) assert root.find("bananas" ) return True def _lowerCamelCase ( lowercase : str , lowercase : bool ) -> None: print(str(lowercase ) , "works!" if passes else "doesn't work :(" ) def _lowerCamelCase ( ) -> None: assert test_trie() def _lowerCamelCase ( ) -> None: print_results("Testing trie functionality" , test_trie() ) if __name__ == "__main__": main()
692
'''simple docstring''' import torch from diffusers import DDIMParallelScheduler from .test_schedulers import SchedulerCommonTest class __SCREAMING_SNAKE_CASE (lowerCamelCase_ ): """simple docstring""" __a =(DDIMParallelScheduler,) __a =(('eta', 0.0), ('num_inference_steps', 50)) def UpperCamelCase__ ( self : Optional[int] , **__a : Any ): _a = { "num_train_timesteps": 10_00, "beta_start": 0.0001, "beta_end": 0.02, "beta_schedule": "linear", "clip_sample": True, } config.update(**__a ) return config def UpperCamelCase__ ( self : List[str] , **__a : Optional[int] ): _a = self.scheduler_classes[0] _a = self.get_scheduler_config(**__a ) _a = scheduler_class(**__a ) _a , _a = 10, 0.0 _a = self.dummy_model() _a = self.dummy_sample_deter scheduler.set_timesteps(__a ) for t in scheduler.timesteps: _a = model(__a , __a ) _a = scheduler.step(__a , __a , __a , __a ).prev_sample return sample def UpperCamelCase__ ( self : str ): for timesteps in [1_00, 5_00, 10_00]: self.check_over_configs(num_train_timesteps=__a ) def UpperCamelCase__ ( self : Dict ): for steps_offset in [0, 1]: self.check_over_configs(steps_offset=__a ) _a = self.scheduler_classes[0] _a = self.get_scheduler_config(steps_offset=1 ) _a = scheduler_class(**__a ) scheduler.set_timesteps(5 ) assert torch.equal(scheduler.timesteps , torch.LongTensor([8_01, 6_01, 4_01, 2_01, 1] ) ) def UpperCamelCase__ ( self : Tuple ): for beta_start, beta_end in zip([0.0001, 0.001, 0.01, 0.1] , [0.002, 0.02, 0.2, 2] ): self.check_over_configs(beta_start=__a , beta_end=__a ) def UpperCamelCase__ ( self : Dict ): for schedule in ["linear", "squaredcos_cap_v2"]: self.check_over_configs(beta_schedule=__a ) def UpperCamelCase__ ( self : Tuple ): for prediction_type in ["epsilon", "v_prediction"]: self.check_over_configs(prediction_type=__a ) def UpperCamelCase__ ( self : Dict ): for clip_sample in [True, False]: self.check_over_configs(clip_sample=__a ) def UpperCamelCase__ ( self : Optional[int] ): for timestep_spacing in ["trailing", "leading"]: self.check_over_configs(timestep_spacing=__a ) def UpperCamelCase__ ( self : Optional[Any] ): for rescale_betas_zero_snr in [True, False]: self.check_over_configs(rescale_betas_zero_snr=__a ) def UpperCamelCase__ ( self : List[Any] ): self.check_over_configs(thresholding=__a ) for threshold in [0.5, 1.0, 2.0]: for prediction_type in ["epsilon", "v_prediction"]: self.check_over_configs( thresholding=__a , prediction_type=__a , sample_max_value=__a , ) def UpperCamelCase__ ( self : List[Any] ): for t in [1, 10, 49]: self.check_over_forward(time_step=__a ) def UpperCamelCase__ ( self : Union[str, Any] ): for t, num_inference_steps in zip([1, 10, 50] , [10, 50, 5_00] ): self.check_over_forward(time_step=__a , num_inference_steps=__a ) def UpperCamelCase__ ( self : Union[str, Any] ): for t, eta in zip([1, 10, 49] , [0.0, 0.5, 1.0] ): self.check_over_forward(time_step=__a , eta=__a ) def UpperCamelCase__ ( self : Optional[int] ): _a = self.scheduler_classes[0] _a = self.get_scheduler_config() _a = scheduler_class(**__a ) assert torch.sum(torch.abs(scheduler._get_variance(0 , 0 ) - 0.0 ) ) < 1e-5 assert torch.sum(torch.abs(scheduler._get_variance(4_20 , 4_00 ) - 0.14771 ) ) < 1e-5 assert torch.sum(torch.abs(scheduler._get_variance(9_80 , 9_60 ) - 0.32460 ) ) < 1e-5 assert torch.sum(torch.abs(scheduler._get_variance(0 , 0 ) - 0.0 ) ) < 1e-5 assert torch.sum(torch.abs(scheduler._get_variance(4_87 , 4_86 ) - 0.00979 ) ) < 1e-5 assert torch.sum(torch.abs(scheduler._get_variance(9_99 , 9_98 ) - 0.02 ) ) < 1e-5 def UpperCamelCase__ ( self : List[str] ): _a = self.scheduler_classes[0] _a = self.get_scheduler_config() _a = scheduler_class(**__a ) _a , _a = 10, 0.0 scheduler.set_timesteps(__a ) _a = self.dummy_model() _a = self.dummy_sample_deter _a = self.dummy_sample_deter + 0.1 _a = self.dummy_sample_deter - 0.1 _a = samplea.shape[0] _a = torch.stack([samplea, samplea, samplea] , dim=0 ) _a = torch.arange(__a )[0:3, None].repeat(1 , __a ) _a = model(samples.flatten(0 , 1 ) , timesteps.flatten(0 , 1 ) ) _a = scheduler.batch_step_no_noise(__a , timesteps.flatten(0 , 1 ) , samples.flatten(0 , 1 ) , __a ) _a = torch.sum(torch.abs(__a ) ) _a = torch.mean(torch.abs(__a ) ) assert abs(result_sum.item() - 1147.7904 ) < 1e-2 assert abs(result_mean.item() - 0.4982 ) < 1e-3 def UpperCamelCase__ ( self : List[str] ): _a = self.full_loop() _a = torch.sum(torch.abs(__a ) ) _a = torch.mean(torch.abs(__a ) ) assert abs(result_sum.item() - 172.0067 ) < 1e-2 assert abs(result_mean.item() - 0.223967 ) < 1e-3 def UpperCamelCase__ ( self : str ): _a = self.full_loop(prediction_type="v_prediction" ) _a = torch.sum(torch.abs(__a ) ) _a = torch.mean(torch.abs(__a ) ) assert abs(result_sum.item() - 52.5302 ) < 1e-2 assert abs(result_mean.item() - 0.0684 ) < 1e-3 def UpperCamelCase__ ( self : str ): # We specify different beta, so that the first alpha is 0.99 _a = self.full_loop(set_alpha_to_one=__a , beta_start=0.01 ) _a = torch.sum(torch.abs(__a ) ) _a = torch.mean(torch.abs(__a ) ) assert abs(result_sum.item() - 149.8295 ) < 1e-2 assert abs(result_mean.item() - 0.1951 ) < 1e-3 def UpperCamelCase__ ( self : str ): # We specify different beta, so that the first alpha is 0.99 _a = self.full_loop(set_alpha_to_one=__a , beta_start=0.01 ) _a = torch.sum(torch.abs(__a ) ) _a = torch.mean(torch.abs(__a ) ) assert abs(result_sum.item() - 149.0784 ) < 1e-2 assert abs(result_mean.item() - 0.1941 ) < 1e-3
692
1
'''simple docstring''' import os from typing import Any, Callable, Dict, List, Optional, Tuple, Union import torch from torch import nn from ...models.controlnet import ControlNetModel, ControlNetOutput from ...models.modeling_utils import ModelMixin from ...utils import logging lowerCAmelCase_ : Optional[int] = logging.get_logger(__name__) class __SCREAMING_SNAKE_CASE (lowerCamelCase_ ): """simple docstring""" def __init__( self : Optional[int] , __a : Union[List[ControlNetModel], Tuple[ControlNetModel]] ): super().__init__() _a = nn.ModuleList(__a ) def UpperCamelCase__ ( self : Dict , __a : torch.FloatTensor , __a : Union[torch.Tensor, float, int] , __a : torch.Tensor , __a : List[torch.tensor] , __a : List[float] , __a : Optional[torch.Tensor] = None , __a : Optional[torch.Tensor] = None , __a : Optional[torch.Tensor] = None , __a : Optional[Dict[str, Any]] = None , __a : bool = False , __a : bool = True , ): for i, (image, scale, controlnet) in enumerate(zip(__a , __a , self.nets ) ): _a , _a = controlnet( __a , __a , __a , __a , __a , __a , __a , __a , __a , __a , __a , ) # merge samples if i == 0: _a , _a = down_samples, mid_sample else: _a = [ samples_prev + samples_curr for samples_prev, samples_curr in zip(__a , __a ) ] mid_block_res_sample += mid_sample return down_block_res_samples, mid_block_res_sample def UpperCamelCase__ ( self : List[str] , __a : Union[str, os.PathLike] , __a : bool = True , __a : Callable = None , __a : bool = False , __a : Optional[str] = None , ): _a = 0 _a = save_directory for controlnet in self.nets: controlnet.save_pretrained( __a , is_main_process=__a , save_function=__a , safe_serialization=__a , variant=__a , ) idx += 1 _a = model_path_to_save + f'_{idx}' @classmethod def UpperCamelCase__ ( cls : Any , __a : Optional[Union[str, os.PathLike]] , **__a : Union[str, Any] ): _a = 0 _a = [] # load controlnet and append to list until no controlnet directory exists anymore # first controlnet has to be saved under `./mydirectory/controlnet` to be compliant with `DiffusionPipeline.from_prertained` # second, third, ... controlnets have to be saved under `./mydirectory/controlnet_1`, `./mydirectory/controlnet_2`, ... _a = pretrained_model_path while os.path.isdir(__a ): _a = ControlNetModel.from_pretrained(__a , **__a ) controlnets.append(__a ) idx += 1 _a = pretrained_model_path + f'_{idx}' logger.info(f'{len(__a )} controlnets loaded from {pretrained_model_path}.' ) if len(__a ) == 0: raise ValueError( f'No ControlNets found under {os.path.dirname(__a )}. Expected at least {pretrained_model_path + "_0"}.' ) return cls(__a )
692
'''simple docstring''' from operator import delitem, getitem, setitem import pytest from data_structures.hashing.hash_map import HashMap def _lowerCamelCase ( lowercase : Any ) -> List[str]: return getitem, k def _lowerCamelCase ( lowercase : Optional[Any] , lowercase : Union[str, Any] ) -> Any: return setitem, k, v def _lowerCamelCase ( lowercase : int ) -> Union[str, Any]: return delitem, k def _lowerCamelCase ( lowercase : Tuple , lowercase : Dict , *lowercase : Union[str, Any] ) -> int: try: return fun(lowercase , *lowercase ), None except Exception as e: return None, e lowerCAmelCase_ : Optional[Any] = ( _set('key_a', 'val_a'), _set('key_b', 'val_b'), ) lowerCAmelCase_ : Optional[int] = [ _set('key_a', 'val_a'), _set('key_a', 'val_b'), ] lowerCAmelCase_ : int = [ _set('key_a', 'val_a'), _set('key_b', 'val_b'), _del('key_a'), _del('key_b'), _set('key_a', 'val_a'), _del('key_a'), ] lowerCAmelCase_ : List[Any] = [ _get('key_a'), _del('key_a'), _set('key_a', 'val_a'), _del('key_a'), _del('key_a'), _get('key_a'), ] lowerCAmelCase_ : str = [ *[_set(x, x) for x in range(5)], # guaranteed upsize ] lowerCAmelCase_ : str = [ *[_set(x, x) for x in range(5)], # guaranteed upsize *[_del(x) for x in range(5)], _set('key_a', 'val_b'), ] @pytest.mark.parametrize( "operations" , ( pytest.param(_add_items , id="add items" ), pytest.param(_overwrite_items , id="overwrite items" ), pytest.param(_delete_items , id="delete items" ), pytest.param(_access_absent_items , id="access absent items" ), pytest.param(_add_with_resize_up , id="add with resize up" ), pytest.param(_add_with_resize_down , id="add with resize down" ), ) , ) def _lowerCamelCase ( lowercase : Optional[int] ) -> Optional[int]: _a = HashMap(initial_block_size=4 ) _a = {} for _, (fun, *args) in enumerate(lowercase ): _a , _a = _run_operation(lowercase , lowercase , *lowercase ) _a , _a = _run_operation(lowercase , lowercase , *lowercase ) assert my_res == py_res assert str(lowercase ) == str(lowercase ) assert set(lowercase ) == set(lowercase ) assert len(lowercase ) == len(lowercase ) assert set(my.items() ) == set(py.items() ) def _lowerCamelCase ( ) -> str: def is_public(lowercase : str ) -> bool: return not name.startswith("_" ) _a = {name for name in dir({} ) if is_public(lowercase )} _a = {name for name in dir(HashMap() ) if is_public(lowercase )} assert dict_public_names > hash_public_names
692
1
'''simple docstring''' import json import os from collections import Counter import torch import torchvision import torchvision.transforms as transforms from PIL import Image from torch import nn from torch.utils.data import Dataset lowerCAmelCase_ : List[str] = {1: (1, 1), 2: (2, 1), 3: (3, 1), 4: (2, 2), 5: (5, 1), 6: (3, 2), 7: (7, 1), 8: (4, 2), 9: (3, 3)} class __SCREAMING_SNAKE_CASE (nn.Module ): """simple docstring""" def __init__( self : Union[str, Any] , __a : Optional[Any] ): super().__init__() _a = torchvision.models.resnetaaa(pretrained=__a ) _a = list(model.children() )[:-2] _a = nn.Sequential(*__a ) _a = nn.AdaptiveAvgPoolad(POOLING_BREAKDOWN[args.num_image_embeds] ) def UpperCamelCase__ ( self : List[str] , __a : Optional[Any] ): # Bx3x224x224 -> Bx2048x7x7 -> Bx2048xN -> BxNx2048 _a = self.pool(self.model(__a ) ) _a = torch.flatten(__a , start_dim=2 ) _a = out.transpose(1 , 2 ).contiguous() return out # BxNx2048 class __SCREAMING_SNAKE_CASE (lowerCamelCase_ ): """simple docstring""" def __init__( self : Optional[int] , __a : Dict , __a : List[str] , __a : Optional[Any] , __a : Union[str, Any] , __a : List[Any] ): _a = [json.loads(__a ) for l in open(__a )] _a = os.path.dirname(__a ) _a = tokenizer _a = labels _a = len(__a ) _a = max_seq_length _a = transforms def __len__( self : Optional[int] ): return len(self.data ) def __getitem__( self : int , __a : Dict ): _a = torch.LongTensor(self.tokenizer.encode(self.data[index]["text"] , add_special_tokens=__a ) ) _a , _a , _a = sentence[0], sentence[1:-1], sentence[-1] _a = sentence[: self.max_seq_length] _a = torch.zeros(self.n_classes ) _a = 1 _a = Image.open(os.path.join(self.data_dir , self.data[index]["img"] ) ).convert("RGB" ) _a = self.transforms(__a ) return { "image_start_token": start_token, "image_end_token": end_token, "sentence": sentence, "image": image, "label": label, } def UpperCamelCase__ ( self : str ): _a = Counter() for row in self.data: label_freqs.update(row["label"] ) return label_freqs def _lowerCamelCase ( lowercase : Union[str, Any] ) -> Dict: _a = [len(row["sentence"] ) for row in batch] _a , _a = len(lowercase ), max(lowercase ) _a = torch.zeros(lowercase , lowercase , dtype=torch.long ) _a = torch.zeros(lowercase , lowercase , dtype=torch.long ) for i_batch, (input_row, length) in enumerate(zip(lowercase , lowercase ) ): _a = input_row["sentence"] _a = 1 _a = torch.stack([row["image"] for row in batch] ) _a = torch.stack([row["label"] for row in batch] ) _a = torch.stack([row["image_start_token"] for row in batch] ) _a = torch.stack([row["image_end_token"] for row in batch] ) return text_tensor, mask_tensor, img_tensor, img_start_token, img_end_token, tgt_tensor def _lowerCamelCase ( ) -> Tuple: return [ "Crime", "Drama", "Thriller", "Action", "Comedy", "Romance", "Documentary", "Short", "Mystery", "History", "Family", "Adventure", "Fantasy", "Sci-Fi", "Western", "Horror", "Sport", "War", "Music", "Musical", "Animation", "Biography", "Film-Noir", ] def _lowerCamelCase ( ) -> Optional[int]: return transforms.Compose( [ transforms.Resize(256 ), transforms.CenterCrop(224 ), transforms.ToTensor(), transforms.Normalize( mean=[0.46_77_70_44, 0.44_53_14_29, 0.40_66_10_17] , std=[0.12_22_19_94, 0.12_14_58_35, 0.14_38_04_69] , ), ] )
692
'''simple docstring''' import os import unittest from transformers.models.phobert.tokenization_phobert import VOCAB_FILES_NAMES, PhobertTokenizer from ...test_tokenization_common import TokenizerTesterMixin class __SCREAMING_SNAKE_CASE (lowerCamelCase_ , unittest.TestCase ): """simple docstring""" __a =PhobertTokenizer __a =False def UpperCamelCase__ ( self : int ): super().setUp() # Adapted from Sennrich et al. 2015 and https://github.com/rsennrich/subword-nmt _a = ["T@@", "i", "I", "R@@", "r", "e@@"] _a = dict(zip(__a , range(len(__a ) ) ) ) _a = ["#version: 0.2", "l ร </w>"] _a = {"unk_token": "<unk>"} _a = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES["vocab_file"] ) _a = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES["merges_file"] ) with open(self.vocab_file , "w" , encoding="utf-8" ) as fp: for token in vocab_tokens: fp.write(f'{token} {vocab_tokens[token]}\n' ) with open(self.merges_file , "w" , encoding="utf-8" ) as fp: fp.write("\n".join(__a ) ) def UpperCamelCase__ ( self : str , **__a : List[str] ): kwargs.update(self.special_tokens_map ) return PhobertTokenizer.from_pretrained(self.tmpdirname , **__a ) def UpperCamelCase__ ( self : Optional[Any] , __a : Optional[int] ): _a = "Tรดi lร  VinAI Research" _a = "T<unk> i <unk> <unk> <unk> <unk> <unk> <unk> I Re<unk> e<unk> <unk> <unk> <unk>" return input_text, output_text def UpperCamelCase__ ( self : Dict ): _a = PhobertTokenizer(self.vocab_file , self.merges_file , **self.special_tokens_map ) _a = "Tรดi lร  VinAI Research" _a = "T@@ รด@@ i l@@ ร  V@@ i@@ n@@ A@@ I R@@ e@@ s@@ e@@ a@@ r@@ c@@ h".split() _a = tokenizer.tokenize(__a ) print(__a ) self.assertListEqual(__a , __a ) _a = tokens + [tokenizer.unk_token] _a = [4, 3, 5, 3, 3, 3, 3, 3, 3, 6, 7, 9, 3, 9, 3, 3, 3, 3, 3] self.assertListEqual(tokenizer.convert_tokens_to_ids(__a ) , __a )
692
1
'''simple docstring''' class __SCREAMING_SNAKE_CASE : """simple docstring""" def __init__( self : List[Any] ): _a = 0 _a = 0 _a = {} def UpperCamelCase__ ( self : List[Any] , __a : Tuple ): if vertex not in self.adjacency: _a = {} self.num_vertices += 1 def UpperCamelCase__ ( self : Union[str, Any] , __a : Union[str, Any] , __a : Tuple , __a : Union[str, Any] ): self.add_vertex(__a ) self.add_vertex(__a ) if head == tail: return _a = weight _a = weight def UpperCamelCase__ ( self : Optional[Any] ): _a = self.get_edges() for edge in edges: _a , _a , _a = edge edges.remove((tail, head, weight) ) for i in range(len(__a ) ): _a = list(edges[i] ) edges.sort(key=lambda __a : e[2] ) for i in range(len(__a ) - 1 ): if edges[i][2] >= edges[i + 1][2]: _a = edges[i][2] + 1 for edge in edges: _a , _a , _a = edge _a = weight _a = weight def __str__( self : Any ): _a = "" for tail in self.adjacency: for head in self.adjacency[tail]: _a = self.adjacency[head][tail] string += f'{head} -> {tail} == {weight}\n' return string.rstrip("\n" ) def UpperCamelCase__ ( self : int ): _a = [] for tail in self.adjacency: for head in self.adjacency[tail]: output.append((tail, head, self.adjacency[head][tail]) ) return output def UpperCamelCase__ ( self : int ): return self.adjacency.keys() @staticmethod def UpperCamelCase__ ( __a : List[Any]=None , __a : Optional[int]=None ): _a = Graph() if vertices is None: _a = [] if edges is None: _a = [] for vertex in vertices: g.add_vertex(__a ) for edge in edges: g.add_edge(*__a ) return g class __SCREAMING_SNAKE_CASE : """simple docstring""" def __init__( self : List[Any] ): _a = {} _a = {} def __len__( self : Union[str, Any] ): return len(self.parent ) def UpperCamelCase__ ( self : Any , __a : List[str] ): if item in self.parent: return self.find(__a ) _a = item _a = 0 return item def UpperCamelCase__ ( self : Optional[Any] , __a : List[str] ): if item not in self.parent: return self.make_set(__a ) if item != self.parent[item]: _a = self.find(self.parent[item] ) return self.parent[item] def UpperCamelCase__ ( self : Optional[Any] , __a : Union[str, Any] , __a : Optional[int] ): _a = self.find(__a ) _a = self.find(__a ) if roota == roota: return roota if self.rank[roota] > self.rank[roota]: _a = roota return roota if self.rank[roota] < self.rank[roota]: _a = roota return roota if self.rank[roota] == self.rank[roota]: self.rank[roota] += 1 _a = roota return roota return None @staticmethod def UpperCamelCase__ ( __a : int ): _a = graph.num_vertices _a = Graph.UnionFind() _a = [] while num_components > 1: _a = {} for vertex in graph.get_vertices(): _a = -1 _a = graph.get_edges() for edge in edges: _a , _a , _a = edge edges.remove((tail, head, weight) ) for edge in edges: _a , _a , _a = edge _a = union_find.find(__a ) _a = union_find.find(__a ) if seta != seta: if cheap_edge[seta] == -1 or cheap_edge[seta][2] > weight: _a = [head, tail, weight] if cheap_edge[seta] == -1 or cheap_edge[seta][2] > weight: _a = [head, tail, weight] for vertex in cheap_edge: if cheap_edge[vertex] != -1: _a , _a , _a = cheap_edge[vertex] if union_find.find(__a ) != union_find.find(__a ): union_find.union(__a , __a ) mst_edges.append(cheap_edge[vertex] ) _a = num_components - 1 _a = Graph.build(edges=__a ) return mst
692
'''simple docstring''' import math import time from transformers import Trainer, is_torch_tpu_available from transformers.trainer_utils import PredictionOutput, speed_metrics if is_torch_tpu_available(check_device=False): import torch_xla.core.xla_model as xm import torch_xla.debug.metrics as met class __SCREAMING_SNAKE_CASE (lowerCamelCase_ ): """simple docstring""" def __init__( self : str , *__a : Any , __a : str=None , __a : Union[str, Any]=None , **__a : Any ): super().__init__(*__a , **__a ) _a = eval_examples _a = post_process_function def UpperCamelCase__ ( self : Optional[Any] , __a : Dict=None , __a : Any=None , __a : str=None , __a : str = "eval" ): _a = self.eval_dataset if eval_dataset is None else eval_dataset _a = self.get_eval_dataloader(__a ) _a = self.eval_examples if eval_examples is None else eval_examples # Temporarily disable metric computation, we will do it in the loop here. _a = self.compute_metrics _a = None _a = self.prediction_loop if self.args.use_legacy_prediction_loop else self.evaluation_loop _a = time.time() try: _a = eval_loop( __a , description="Evaluation" , prediction_loss_only=True if compute_metrics is None else None , ignore_keys=__a , metric_key_prefix=__a , ) finally: _a = compute_metrics _a = self.args.eval_batch_size * self.args.world_size if f'{metric_key_prefix}_jit_compilation_time' in output.metrics: start_time += output.metrics[f'{metric_key_prefix}_jit_compilation_time'] output.metrics.update( speed_metrics( __a , __a , num_samples=output.num_samples , num_steps=math.ceil(output.num_samples / total_batch_size ) , ) ) if self.post_process_function is not None and self.compute_metrics is not None and self.args.should_save: # Only the main node write the results by default _a = self.post_process_function(__a , __a , output.predictions ) _a = self.compute_metrics(__a ) # Prefix all keys with metric_key_prefix + '_' for key in list(metrics.keys() ): if not key.startswith(f'{metric_key_prefix}_' ): _a = metrics.pop(__a ) metrics.update(output.metrics ) else: _a = output.metrics if self.args.should_log: # Only the main node log the results by default self.log(__a ) if self.args.tpu_metrics_debug or self.args.debug: # tpu-comment: Logging debug metrics for PyTorch/XLA (compile, execute times, ops, etc.) xm.master_print(met.metrics_report() ) _a = self.callback_handler.on_evaluate(self.args , self.state , self.control , __a ) return metrics def UpperCamelCase__ ( self : Tuple , __a : Dict , __a : Optional[Any] , __a : Optional[Any]=None , __a : str = "test" ): _a = self.get_test_dataloader(__a ) # Temporarily disable metric computation, we will do it in the loop here. _a = self.compute_metrics _a = None _a = self.prediction_loop if self.args.use_legacy_prediction_loop else self.evaluation_loop _a = time.time() try: _a = eval_loop( __a , description="Prediction" , prediction_loss_only=True if compute_metrics is None else None , ignore_keys=__a , metric_key_prefix=__a , ) finally: _a = compute_metrics _a = self.args.eval_batch_size * self.args.world_size if f'{metric_key_prefix}_jit_compilation_time' in output.metrics: start_time += output.metrics[f'{metric_key_prefix}_jit_compilation_time'] output.metrics.update( speed_metrics( __a , __a , num_samples=output.num_samples , num_steps=math.ceil(output.num_samples / total_batch_size ) , ) ) if self.post_process_function is None or self.compute_metrics is None: return output _a = self.post_process_function(__a , __a , output.predictions , "predict" ) _a = self.compute_metrics(__a ) # Prefix all keys with metric_key_prefix + '_' for key in list(metrics.keys() ): if not key.startswith(f'{metric_key_prefix}_' ): _a = metrics.pop(__a ) metrics.update(output.metrics ) return PredictionOutput(predictions=predictions.predictions , label_ids=predictions.label_ids , metrics=__a )
692
1
'''simple docstring''' import os def _lowerCamelCase ( lowercase : str = "matrix.txt" ) -> int: with open(os.path.join(os.path.dirname(lowercase ) , lowercase ) ) as in_file: _a = in_file.read() _a = [[int(lowercase ) for cell in row.split("," )] for row in data.strip().splitlines()] _a = [[0 for cell in row] for row in grid] _a = len(grid[0] ) _a = [[0 for i in range(lowercase )] for j in range(lowercase )] _a = grid[0][0] for i in range(1 , lowercase ): _a = grid[0][i] + dp[0][i - 1] for i in range(1 , lowercase ): _a = grid[i][0] + dp[i - 1][0] for i in range(1 , lowercase ): for j in range(1 , lowercase ): _a = grid[i][j] + min(dp[i - 1][j] , dp[i][j - 1] ) return dp[-1][-1] if __name__ == "__main__": print(f"""{solution() = }""")
692
'''simple docstring''' import warnings from ...utils import logging from .image_processing_chinese_clip import ChineseCLIPImageProcessor lowerCAmelCase_ : Union[str, Any] = logging.get_logger(__name__) class __SCREAMING_SNAKE_CASE (lowerCamelCase_ ): """simple docstring""" def __init__( self : Optional[Any] , *__a : Dict , **__a : List[Any] ): warnings.warn( "The class ChineseCLIPFeatureExtractor is deprecated and will be removed in version 5 of Transformers." " Please use ChineseCLIPImageProcessor instead." , __a , ) super().__init__(*__a , **__a )
692
1
'''simple docstring''' from abc import ABC, abstractmethod from argparse import ArgumentParser class __SCREAMING_SNAKE_CASE (lowerCamelCase_ ): """simple docstring""" @staticmethod @abstractmethod def UpperCamelCase__ ( __a : ArgumentParser ): raise NotImplementedError() @abstractmethod def UpperCamelCase__ ( self : Optional[int] ): raise NotImplementedError()
692
'''simple docstring''' from typing import Any, Dict, Optional import torch import torch.nn.functional as F from torch import nn from ..utils import maybe_allow_in_graph from .activations import get_activation from .attention_processor import Attention from .embeddings import CombinedTimestepLabelEmbeddings @maybe_allow_in_graph class __SCREAMING_SNAKE_CASE (nn.Module ): """simple docstring""" def __init__( self : Optional[int] , __a : int , __a : int , __a : int , __a : str=0.0 , __a : Optional[int] = None , __a : str = "geglu" , __a : Optional[int] = None , __a : bool = False , __a : bool = False , __a : bool = False , __a : bool = False , __a : bool = True , __a : str = "layer_norm" , __a : bool = False , ): super().__init__() _a = only_cross_attention _a = (num_embeds_ada_norm is not None) and norm_type == "ada_norm_zero" _a = (num_embeds_ada_norm is not None) and norm_type == "ada_norm" if norm_type in ("ada_norm", "ada_norm_zero") and num_embeds_ada_norm is None: raise ValueError( f'`norm_type` is set to {norm_type}, but `num_embeds_ada_norm` is not defined. Please make sure to' f' define `num_embeds_ada_norm` if setting `norm_type` to {norm_type}.' ) # Define 3 blocks. Each block has its own normalization layer. # 1. Self-Attn if self.use_ada_layer_norm: _a = AdaLayerNorm(__a , __a ) elif self.use_ada_layer_norm_zero: _a = AdaLayerNormZero(__a , __a ) else: _a = nn.LayerNorm(__a , elementwise_affine=__a ) _a = Attention( query_dim=__a , heads=__a , dim_head=__a , dropout=__a , bias=__a , cross_attention_dim=cross_attention_dim if only_cross_attention else None , upcast_attention=__a , ) # 2. Cross-Attn if cross_attention_dim is not None or double_self_attention: # We currently only use AdaLayerNormZero for self attention where there will only be one attention block. # I.e. the number of returned modulation chunks from AdaLayerZero would not make sense if returned during # the second cross attention block. _a = ( AdaLayerNorm(__a , __a ) if self.use_ada_layer_norm else nn.LayerNorm(__a , elementwise_affine=__a ) ) _a = Attention( query_dim=__a , cross_attention_dim=cross_attention_dim if not double_self_attention else None , heads=__a , dim_head=__a , dropout=__a , bias=__a , upcast_attention=__a , ) # is self-attn if encoder_hidden_states is none else: _a = None _a = None # 3. Feed-forward _a = nn.LayerNorm(__a , elementwise_affine=__a ) _a = FeedForward(__a , dropout=__a , activation_fn=__a , final_dropout=__a ) # let chunk size default to None _a = None _a = 0 def UpperCamelCase__ ( self : int , __a : Optional[int] , __a : int ): # Sets chunk feed-forward _a = chunk_size _a = dim def UpperCamelCase__ ( self : List[str] , __a : torch.FloatTensor , __a : Optional[torch.FloatTensor] = None , __a : Optional[torch.FloatTensor] = None , __a : Optional[torch.FloatTensor] = None , __a : Optional[torch.LongTensor] = None , __a : Dict[str, Any] = None , __a : Optional[torch.LongTensor] = None , ): # Notice that normalization is always applied before the real computation in the following blocks. # 1. Self-Attention if self.use_ada_layer_norm: _a = self.norma(__a , __a ) elif self.use_ada_layer_norm_zero: _a , _a , _a , _a , _a = self.norma( __a , __a , __a , hidden_dtype=hidden_states.dtype ) else: _a = self.norma(__a ) _a = cross_attention_kwargs if cross_attention_kwargs is not None else {} _a = self.attna( __a , encoder_hidden_states=encoder_hidden_states if self.only_cross_attention else None , attention_mask=__a , **__a , ) if self.use_ada_layer_norm_zero: _a = gate_msa.unsqueeze(1 ) * attn_output _a = attn_output + hidden_states # 2. Cross-Attention if self.attna is not None: _a = ( self.norma(__a , __a ) if self.use_ada_layer_norm else self.norma(__a ) ) _a = self.attna( __a , encoder_hidden_states=__a , attention_mask=__a , **__a , ) _a = attn_output + hidden_states # 3. Feed-forward _a = self.norma(__a ) if self.use_ada_layer_norm_zero: _a = norm_hidden_states * (1 + scale_mlp[:, None]) + shift_mlp[:, None] if self._chunk_size is not None: # "feed_forward_chunk_size" can be used to save memory if norm_hidden_states.shape[self._chunk_dim] % self._chunk_size != 0: raise ValueError( f'`hidden_states` dimension to be chunked: {norm_hidden_states.shape[self._chunk_dim]} has to be divisible by chunk size: {self._chunk_size}. Make sure to set an appropriate `chunk_size` when calling `unet.enable_forward_chunking`.' ) _a = norm_hidden_states.shape[self._chunk_dim] // self._chunk_size _a = torch.cat( [self.ff(__a ) for hid_slice in norm_hidden_states.chunk(__a , dim=self._chunk_dim )] , dim=self._chunk_dim , ) else: _a = self.ff(__a ) if self.use_ada_layer_norm_zero: _a = gate_mlp.unsqueeze(1 ) * ff_output _a = ff_output + hidden_states return hidden_states class __SCREAMING_SNAKE_CASE (nn.Module ): """simple docstring""" def __init__( self : List[Any] , __a : int , __a : Optional[int] = None , __a : int = 4 , __a : float = 0.0 , __a : str = "geglu" , __a : bool = False , ): super().__init__() _a = int(dim * mult ) _a = dim_out if dim_out is not None else dim if activation_fn == "gelu": _a = GELU(__a , __a ) if activation_fn == "gelu-approximate": _a = GELU(__a , __a , approximate="tanh" ) elif activation_fn == "geglu": _a = GEGLU(__a , __a ) elif activation_fn == "geglu-approximate": _a = ApproximateGELU(__a , __a ) _a = nn.ModuleList([] ) # project in self.net.append(__a ) # project dropout self.net.append(nn.Dropout(__a ) ) # project out self.net.append(nn.Linear(__a , __a ) ) # FF as used in Vision Transformer, MLP-Mixer, etc. have a final dropout if final_dropout: self.net.append(nn.Dropout(__a ) ) def UpperCamelCase__ ( self : List[Any] , __a : Tuple ): for module in self.net: _a = module(__a ) return hidden_states class __SCREAMING_SNAKE_CASE (nn.Module ): """simple docstring""" def __init__( self : int , __a : int , __a : int , __a : str = "none" ): super().__init__() _a = nn.Linear(__a , __a ) _a = approximate def UpperCamelCase__ ( self : Union[str, Any] , __a : List[Any] ): if gate.device.type != "mps": return F.gelu(__a , approximate=self.approximate ) # mps: gelu is not implemented for float16 return F.gelu(gate.to(dtype=torch.floataa ) , approximate=self.approximate ).to(dtype=gate.dtype ) def UpperCamelCase__ ( self : str , __a : Optional[int] ): _a = self.proj(__a ) _a = self.gelu(__a ) return hidden_states class __SCREAMING_SNAKE_CASE (nn.Module ): """simple docstring""" def __init__( self : str , __a : int , __a : int ): super().__init__() _a = nn.Linear(__a , dim_out * 2 ) def UpperCamelCase__ ( self : List[Any] , __a : Optional[int] ): if gate.device.type != "mps": return F.gelu(__a ) # mps: gelu is not implemented for float16 return F.gelu(gate.to(dtype=torch.floataa ) ).to(dtype=gate.dtype ) def UpperCamelCase__ ( self : List[str] , __a : Any ): _a , _a = self.proj(__a ).chunk(2 , dim=-1 ) return hidden_states * self.gelu(__a ) class __SCREAMING_SNAKE_CASE (nn.Module ): """simple docstring""" def __init__( self : Optional[Any] , __a : int , __a : int ): super().__init__() _a = nn.Linear(__a , __a ) def UpperCamelCase__ ( self : Union[str, Any] , __a : Dict ): _a = self.proj(__a ) return x * torch.sigmoid(1.702 * x ) class __SCREAMING_SNAKE_CASE (nn.Module ): """simple docstring""" def __init__( self : int , __a : str , __a : str ): super().__init__() _a = nn.Embedding(__a , __a ) _a = nn.SiLU() _a = nn.Linear(__a , embedding_dim * 2 ) _a = nn.LayerNorm(__a , elementwise_affine=__a ) def UpperCamelCase__ ( self : Tuple , __a : Any , __a : Optional[Any] ): _a = self.linear(self.silu(self.emb(__a ) ) ) _a , _a = torch.chunk(__a , 2 ) _a = self.norm(__a ) * (1 + scale) + shift return x class __SCREAMING_SNAKE_CASE (nn.Module ): """simple docstring""" def __init__( self : List[Any] , __a : List[Any] , __a : Any ): super().__init__() _a = CombinedTimestepLabelEmbeddings(__a , __a ) _a = nn.SiLU() _a = nn.Linear(__a , 6 * embedding_dim , bias=__a ) _a = nn.LayerNorm(__a , elementwise_affine=__a , eps=1e-6 ) def UpperCamelCase__ ( self : Optional[Any] , __a : Dict , __a : List[Any] , __a : Union[str, Any] , __a : List[Any]=None ): _a = self.linear(self.silu(self.emb(__a , __a , hidden_dtype=__a ) ) ) _a , _a , _a , _a , _a , _a = emb.chunk(6 , dim=1 ) _a = self.norm(__a ) * (1 + scale_msa[:, None]) + shift_msa[:, None] return x, gate_msa, shift_mlp, scale_mlp, gate_mlp class __SCREAMING_SNAKE_CASE (nn.Module ): """simple docstring""" def __init__( self : Optional[int] , __a : int , __a : int , __a : int , __a : Optional[str] = None , __a : float = 1e-5 ): super().__init__() _a = num_groups _a = eps if act_fn is None: _a = None else: _a = get_activation(__a ) _a = nn.Linear(__a , out_dim * 2 ) def UpperCamelCase__ ( self : List[Any] , __a : Optional[Any] , __a : List[Any] ): if self.act: _a = self.act(__a ) _a = self.linear(__a ) _a = emb[:, :, None, None] _a , _a = emb.chunk(2 , dim=1 ) _a = F.group_norm(__a , self.num_groups , eps=self.eps ) _a = x * (1 + scale) + shift return x
692
1
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_tokenizers_available, is_torch_available, is_vision_available, ) lowerCAmelCase_ : str = { 'configuration_perceiver': ['PERCEIVER_PRETRAINED_CONFIG_ARCHIVE_MAP', 'PerceiverConfig', 'PerceiverOnnxConfig'], 'tokenization_perceiver': ['PerceiverTokenizer'], } try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase_ : List[str] = ['PerceiverFeatureExtractor'] lowerCAmelCase_ : Dict = ['PerceiverImageProcessor'] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase_ : Tuple = [ 'PERCEIVER_PRETRAINED_MODEL_ARCHIVE_LIST', 'PerceiverForImageClassificationConvProcessing', 'PerceiverForImageClassificationFourier', 'PerceiverForImageClassificationLearned', 'PerceiverForMaskedLM', 'PerceiverForMultimodalAutoencoding', 'PerceiverForOpticalFlow', 'PerceiverForSequenceClassification', 'PerceiverLayer', 'PerceiverModel', 'PerceiverPreTrainedModel', ] if TYPE_CHECKING: from .configuration_perceiver import PERCEIVER_PRETRAINED_CONFIG_ARCHIVE_MAP, PerceiverConfig, PerceiverOnnxConfig from .tokenization_perceiver import PerceiverTokenizer try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .feature_extraction_perceiver import PerceiverFeatureExtractor from .image_processing_perceiver import PerceiverImageProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_perceiver import ( PERCEIVER_PRETRAINED_MODEL_ARCHIVE_LIST, PerceiverForImageClassificationConvProcessing, PerceiverForImageClassificationFourier, PerceiverForImageClassificationLearned, PerceiverForMaskedLM, PerceiverForMultimodalAutoencoding, PerceiverForOpticalFlow, PerceiverForSequenceClassification, PerceiverLayer, PerceiverModel, PerceiverPreTrainedModel, ) else: import sys lowerCAmelCase_ : int = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
692
'''simple docstring''' from __future__ import annotations from collections import deque from collections.abc import Iterator from dataclasses import dataclass @dataclass class __SCREAMING_SNAKE_CASE : """simple docstring""" __a =42 __a =42 class __SCREAMING_SNAKE_CASE : """simple docstring""" def __init__( self : Union[str, Any] , __a : int ): _a = [[] for _ in range(__a )] _a = size def __getitem__( self : int , __a : int ): return iter(self._graph[vertex] ) @property def UpperCamelCase__ ( self : Dict ): return self._size def UpperCamelCase__ ( self : Union[str, Any] , __a : int , __a : int , __a : int ): if weight not in (0, 1): raise ValueError("Edge weight must be either 0 or 1." ) if to_vertex < 0 or to_vertex >= self.size: raise ValueError("Vertex indexes must be in [0; size)." ) self._graph[from_vertex].append(Edge(__a , __a ) ) def UpperCamelCase__ ( self : Tuple , __a : int , __a : int ): _a = deque([start_vertex] ) _a = [None] * self.size _a = 0 while queue: _a = queue.popleft() _a = distances[current_vertex] if current_distance is None: continue for edge in self[current_vertex]: _a = current_distance + edge.weight _a = distances[edge.destination_vertex] if ( isinstance(__a , __a ) and new_distance >= dest_vertex_distance ): continue _a = new_distance if edge.weight == 0: queue.appendleft(edge.destination_vertex ) else: queue.append(edge.destination_vertex ) if distances[finish_vertex] is None: raise ValueError("No path from start_vertex to finish_vertex." ) return distances[finish_vertex] if __name__ == "__main__": import doctest doctest.testmod()
692
1
'''simple docstring''' import os import time from dataclasses import dataclass, field from enum import Enum from typing import Dict, List, Optional, Union import torch from filelock import FileLock from torch.utils.data import Dataset from ...models.auto.modeling_auto import MODEL_FOR_QUESTION_ANSWERING_MAPPING from ...tokenization_utils import PreTrainedTokenizer from ...utils import logging from ..processors.squad import SquadFeatures, SquadVaProcessor, SquadVaProcessor, squad_convert_examples_to_features lowerCAmelCase_ : Tuple = logging.get_logger(__name__) lowerCAmelCase_ : List[str] = list(MODEL_FOR_QUESTION_ANSWERING_MAPPING.keys()) lowerCAmelCase_ : Dict = tuple(conf.model_type for conf in MODEL_CONFIG_CLASSES) @dataclass class __SCREAMING_SNAKE_CASE : """simple docstring""" __a =field( default=lowerCamelCase_ , metadata={'help': 'Model type selected in the list: ' + ', '.join(lowerCamelCase_ )} ) __a =field( default=lowerCamelCase_ , metadata={'help': 'The input data dir. Should contain the .json files for the SQuAD task.'} ) __a =field( default=128 , metadata={ 'help': ( 'The maximum total input sequence length after tokenization. Sequences longer ' 'than this will be truncated, sequences shorter will be padded.' ) } , ) __a =field( default=128 , metadata={'help': 'When splitting up a long document into chunks, how much stride to take between chunks.'} , ) __a =field( default=64 , metadata={ 'help': ( 'The maximum number of tokens for the question. Questions longer than this will ' 'be truncated to this length.' ) } , ) __a =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.' ) } , ) __a =field( default=lowerCamelCase_ , metadata={'help': 'Overwrite the cached training and evaluation sets'} ) __a =field( default=lowerCamelCase_ , metadata={'help': 'If true, the SQuAD examples contain some that do not have an answer.'} ) __a =field( default=0.0 , metadata={'help': 'If null_score - best_non_null is greater than the threshold predict null.'} ) __a =field( default=20 , metadata={'help': 'If null_score - best_non_null is greater than the threshold predict null.'} ) __a =field( default=0 , metadata={ 'help': ( 'language id of input for language-specific xlm models (see' ' tokenization_xlm.PRETRAINED_INIT_CONFIGURATION)' ) } , ) __a =field(default=1 , metadata={'help': 'multiple threads for converting example to features'} ) class __SCREAMING_SNAKE_CASE (lowerCamelCase_ ): """simple docstring""" __a ='train' __a ='dev' class __SCREAMING_SNAKE_CASE (lowerCamelCase_ ): """simple docstring""" __a =42 __a =42 __a =42 __a =42 def __init__( self : Tuple , __a : SquadDataTrainingArguments , __a : PreTrainedTokenizer , __a : Optional[int] = None , __a : Union[str, Split] = Split.train , __a : Optional[bool] = False , __a : Optional[str] = None , __a : Optional[str] = "pt" , ): _a = args _a = is_language_sensitive _a = SquadVaProcessor() if args.version_2_with_negative else SquadVaProcessor() if isinstance(__a , __a ): try: _a = Split[mode] except KeyError: raise KeyError("mode is not a valid split name" ) _a = mode # Load data features from cache or dataset file _a = "v2" if args.version_2_with_negative else "v1" _a = 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 = cached_features_file + ".lock" with FileLock(__a ): if os.path.exists(__a ) and not args.overwrite_cache: _a = time.time() _a = torch.load(__a ) # Legacy cache files have only features, while new cache files # will have dataset and examples also. _a = self.old_features["features"] _a = self.old_features.get("dataset" , __a ) _a = self.old_features.get("examples" , __a ) 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 = self.processor.get_dev_examples(args.data_dir ) else: _a = self.processor.get_train_examples(args.data_dir ) _a , _a = squad_convert_examples_to_features( examples=self.examples , tokenizer=__a , 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=__a , ) _a = time.time() torch.save( {"features": self.features, "dataset": self.dataset, "examples": self.examples} , __a , ) # ^ 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 : Any ): return len(self.features ) def __getitem__( self : Any , __a : Dict ): # Convert to Tensors and build dataset _a = self.features[i] _a = torch.tensor(feature.input_ids , dtype=torch.long ) _a = torch.tensor(feature.attention_mask , dtype=torch.long ) _a = torch.tensor(feature.token_type_ids , dtype=torch.long ) _a = torch.tensor(feature.cls_index , dtype=torch.long ) _a = torch.tensor(feature.p_mask , dtype=torch.float ) _a = torch.tensor(feature.is_impossible , dtype=torch.float ) _a = { "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 = torch.tensor(feature.start_position , dtype=torch.long ) _a = torch.tensor(feature.end_position , dtype=torch.long ) inputs.update({"start_positions": start_positions, "end_positions": end_positions} ) return inputs
692
'''simple docstring''' import unittest from diffusers import FlaxAutoencoderKL from diffusers.utils import is_flax_available from diffusers.utils.testing_utils import require_flax from .test_modeling_common_flax import FlaxModelTesterMixin if is_flax_available(): import jax @require_flax class __SCREAMING_SNAKE_CASE (lowerCamelCase_ , unittest.TestCase ): """simple docstring""" __a =FlaxAutoencoderKL @property def UpperCamelCase__ ( self : str ): _a = 4 _a = 3 _a = (32, 32) _a = jax.random.PRNGKey(0 ) _a = jax.random.uniform(__a , ((batch_size, num_channels) + sizes) ) return {"sample": image, "prng_key": prng_key} def UpperCamelCase__ ( self : List[Any] ): _a = { "block_out_channels": [32, 64], "in_channels": 3, "out_channels": 3, "down_block_types": ["DownEncoderBlock2D", "DownEncoderBlock2D"], "up_block_types": ["UpDecoderBlock2D", "UpDecoderBlock2D"], "latent_channels": 4, } _a = self.dummy_input return init_dict, inputs_dict
692
1
'''simple docstring''' import coval # From: git+https://github.com/ns-moosavi/coval.git # noqa: F401 from coval.conll import reader, util from coval.eval import evaluator import datasets lowerCAmelCase_ : Optional[Any] = datasets.logging.get_logger(__name__) lowerCAmelCase_ : Tuple = '\\n@InProceedings{moosavi2019minimum,\n author = { Nafise Sadat Moosavi, Leo Born, Massimo Poesio and Michael Strube},\n title = {Using Automatically Extracted Minimum Spans to Disentangle Coreference Evaluation from Boundary Detection},\n year = {2019},\n booktitle = {Proceedings of the 57th Annual Meeting of\n the Association for Computational Linguistics (Volume 1: Long Papers)},\n publisher = {Association for Computational Linguistics},\n address = {Florence, Italy},\n}\n\n@inproceedings{10.3115/1072399.1072405,\nauthor = {Vilain, Marc and Burger, John and Aberdeen, John and Connolly, Dennis and Hirschman, Lynette},\ntitle = {A Model-Theoretic Coreference Scoring Scheme},\nyear = {1995},\nisbn = {1558604022},\npublisher = {Association for Computational Linguistics},\naddress = {USA},\nurl = {https://doi.org/10.3115/1072399.1072405},\ndoi = {10.3115/1072399.1072405},\nbooktitle = {Proceedings of the 6th Conference on Message Understanding},\npages = {45โ€“52},\nnumpages = {8},\nlocation = {Columbia, Maryland},\nseries = {MUC6 โ€™95}\n}\n\n@INPROCEEDINGS{Bagga98algorithmsfor,\n author = {Amit Bagga and Breck Baldwin},\n title = {Algorithms for Scoring Coreference Chains},\n booktitle = {In The First International Conference on Language Resources and Evaluation Workshop on Linguistics Coreference},\n year = {1998},\n pages = {563--566}\n}\n\n@INPROCEEDINGS{Luo05oncoreference,\n author = {Xiaoqiang Luo},\n title = {On coreference resolution performance metrics},\n booktitle = {In Proc. of HLT/EMNLP},\n year = {2005},\n pages = {25--32},\n publisher = {URL}\n}\n\n@inproceedings{moosavi-strube-2016-coreference,\n title = "Which Coreference Evaluation Metric Do You Trust? A Proposal for a Link-based Entity Aware Metric",\n author = "Moosavi, Nafise Sadat and\n Strube, Michael",\n booktitle = "Proceedings of the 54th Annual Meeting of the Association for Computational Linguistics (Volume 1: Long Papers)",\n month = aug,\n year = "2016",\n address = "Berlin, Germany",\n publisher = "Association for Computational Linguistics",\n url = "https://www.aclweb.org/anthology/P16-1060",\n doi = "10.18653/v1/P16-1060",\n pages = "632--642",\n}\n\n' lowerCAmelCase_ : Union[str, Any] = '\\nCoVal is a coreference evaluation tool for the CoNLL and ARRAU datasets which\nimplements of the common evaluation metrics including MUC [Vilain et al, 1995],\nB-cubed [Bagga and Baldwin, 1998], CEAFe [Luo et al., 2005],\nLEA [Moosavi and Strube, 2016] and the averaged CoNLL score\n(the average of the F1 values of MUC, B-cubed and CEAFe)\n[Denis and Baldridge, 2009a; Pradhan et al., 2011].\n\nThis wrapper of CoVal currently only work with CoNLL line format:\nThe CoNLL format has one word per line with all the annotation for this word in column separated by spaces:\nColumn Type Description\n1 Document ID This is a variation on the document filename\n2 Part number Some files are divided into multiple parts numbered as 000, 001, 002, ... etc.\n3 Word number\n4 Word itself This is the token as segmented/tokenized in the Treebank. Initially the *_skel file contain the placeholder [WORD] which gets replaced by the actual token from the Treebank which is part of the OntoNotes release.\n5 Part-of-Speech\n6 Parse bit This is the bracketed structure broken before the first open parenthesis in the parse, and the word/part-of-speech leaf replaced with a *. The full parse can be created by substituting the asterix with the "([pos] [word])" string (or leaf) and concatenating the items in the rows of that column.\n7 Predicate lemma The predicate lemma is mentioned for the rows for which we have semantic role information. All other rows are marked with a "-"\n8 Predicate Frameset ID This is the PropBank frameset ID of the predicate in Column 7.\n9 Word sense This is the word sense of the word in Column 3.\n10 Speaker/Author This is the speaker or author name where available. Mostly in Broadcast Conversation and Web Log data.\n11 Named Entities These columns identifies the spans representing various named entities.\n12:N Predicate Arguments There is one column each of predicate argument structure information for the predicate mentioned in Column 7.\nN Coreference Coreference chain information encoded in a parenthesis structure.\nMore informations on the format can be found here (section "*_conll File Format"): http://www.conll.cemantix.org/2012/data.html\n\nDetails on the evaluation on CoNLL can be found here: https://github.com/ns-moosavi/coval/blob/master/conll/README.md\n\nCoVal code was written by @ns-moosavi.\nSome parts are borrowed from https://github.com/clarkkev/deep-coref/blob/master/evaluation.py\nThe test suite is taken from https://github.com/conll/reference-coreference-scorers/\nMention evaluation and the test suite are added by @andreasvc.\nParsing CoNLL files is developed by Leo Born.\n' lowerCAmelCase_ : Union[str, Any] = '\nCalculates coreference evaluation metrics.\nArgs:\n predictions: list of sentences. Each sentence is a list of word predictions to score in the CoNLL format.\n Each prediction is a word with its annotations as a string made of columns joined with spaces.\n Only columns 4, 5, 6 and the last column are used (word, POS, Pars and coreference annotation)\n See the details on the format in the description of the metric.\n references: list of sentences. Each sentence is a list of word reference to score in the CoNLL format.\n Each reference is a word with its annotations as a string made of columns joined with spaces.\n Only columns 4, 5, 6 and the last column are used (word, POS, Pars and coreference annotation)\n See the details on the format in the description of the metric.\n keep_singletons: After extracting all mentions of key or system files,\n mentions whose corresponding coreference chain is of size one,\n are considered as singletons. The default evaluation mode will include\n singletons in evaluations if they are included in the key or the system files.\n By setting \'keep_singletons=False\', all singletons in the key and system files\n will be excluded from the evaluation.\n NP_only: Most of the recent coreference resolvers only resolve NP mentions and\n leave out the resolution of VPs. By setting the \'NP_only\' option, the scorer will only evaluate the resolution of NPs.\n min_span: By setting \'min_span\', the scorer reports the results based on automatically detected minimum spans.\n Minimum spans are determined using the MINA algorithm.\n\nReturns:\n \'mentions\': mentions\n \'muc\': MUC metric [Vilain et al, 1995]\n \'bcub\': B-cubed [Bagga and Baldwin, 1998]\n \'ceafe\': CEAFe [Luo et al., 2005]\n \'lea\': LEA [Moosavi and Strube, 2016]\n \'conll_score\': averaged CoNLL score (the average of the F1 values of MUC, B-cubed and CEAFe)\n\nExamples:\n\n >>> coval = datasets.load_metric(\'coval\')\n >>> words = [\'bc/cctv/00/cctv_0005 0 0 Thank VBP (TOP(S(VP* thank 01 1 Xu_li * (V*) * -\',\n ... \'bc/cctv/00/cctv_0005 0 1 you PRP (NP*) - - - Xu_li * (ARG1*) (ARG0*) (116)\',\n ... \'bc/cctv/00/cctv_0005 0 2 everyone NN (NP*) - - - Xu_li * (ARGM-DIS*) * (116)\',\n ... \'bc/cctv/00/cctv_0005 0 3 for IN (PP* - - - Xu_li * (ARG2* * -\',\n ... \'bc/cctv/00/cctv_0005 0 4 watching VBG (S(VP*)))) watch 01 1 Xu_li * *) (V*) -\',\n ... \'bc/cctv/00/cctv_0005 0 5 . . *)) - - - Xu_li * * * -\']\n >>> references = [words]\n >>> predictions = [words]\n >>> results = coval.compute(predictions=predictions, references=references)\n >>> print(results) # doctest:+ELLIPSIS\n {\'mentions/recall\': 1.0,[...] \'conll_score\': 100.0}\n' def _lowerCamelCase ( lowercase : Tuple , lowercase : List[Any] , lowercase : Optional[int]=False , lowercase : Dict=False , lowercase : Optional[int]=True , lowercase : Union[str, Any]=False , lowercase : int="dummy_doc" ) -> Union[str, Any]: _a = {doc: key_lines} _a = {doc: sys_lines} _a = {} _a = 0 _a = 0 _a = 0 _a = 0 _a = 0 _a = 0 _a , _a = reader.get_doc_mentions(lowercase , key_doc_lines[doc] , lowercase ) key_singletons_num += singletons_num if NP_only or min_span: _a = reader.set_annotated_parse_trees(lowercase , key_doc_lines[doc] , lowercase , lowercase ) _a , _a = reader.get_doc_mentions(lowercase , sys_doc_lines[doc] , lowercase ) sys_singletons_num += singletons_num if NP_only or min_span: _a = reader.set_annotated_parse_trees(lowercase , key_doc_lines[doc] , lowercase , lowercase ) if remove_nested: _a , _a = reader.remove_nested_coref_mentions(lowercase , lowercase ) key_nested_coref_num += nested_mentions key_removed_nested_clusters += removed_clusters _a , _a = reader.remove_nested_coref_mentions(lowercase , lowercase ) sys_nested_coref_num += nested_mentions sys_removed_nested_clusters += removed_clusters _a = reader.get_mention_assignments(lowercase , lowercase ) _a = reader.get_mention_assignments(lowercase , lowercase ) _a = (key_clusters, sys_clusters, key_mention_sys_cluster, sys_mention_key_cluster) if remove_nested: logger.info( "Number of removed nested coreferring mentions in the key " F'annotation: {key_nested_coref_num}; and system annotation: {sys_nested_coref_num}' ) logger.info( "Number of resulting singleton clusters in the key " F'annotation: {key_removed_nested_clusters}; and system annotation: {sys_removed_nested_clusters}' ) if not keep_singletons: logger.info( F'{key_singletons_num:d} and {sys_singletons_num:d} singletons are removed from the key and system ' "files, respectively" ) return doc_coref_infos def _lowerCamelCase ( lowercase : List[Any] , lowercase : Any , lowercase : Optional[Any] , lowercase : Union[str, Any] , lowercase : Any , lowercase : List[str] , lowercase : Dict ) -> str: _a = get_coref_infos(lowercase , lowercase , lowercase , lowercase , lowercase , lowercase ) _a = {} _a = 0 _a = 0 for name, metric in metrics: _a , _a , _a = evaluator.evaluate_documents(lowercase , lowercase , beta=1 ) if name in ["muc", "bcub", "ceafe"]: conll += fa conll_subparts_num += 1 output_scores.update({F'{name}/recall': recall, F'{name}/precision': precision, F'{name}/f1': fa} ) logger.info( name.ljust(10 ) , F'Recall: {recall * 100:.2f}' , F' Precision: {precision * 100:.2f}' , F' F1: {fa * 100:.2f}' , ) if conll_subparts_num == 3: _a = (conll / 3) * 100 logger.info(F'CoNLL score: {conll:.2f}' ) output_scores.update({"conll_score": conll} ) return output_scores def _lowerCamelCase ( lowercase : Any ) -> str: _a = False for line in key_lines: if not line.startswith("#" ): if len(line.split() ) > 6: _a = line.split()[5] if not parse_col == "-": _a = True break else: break return has_gold_parse @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class __SCREAMING_SNAKE_CASE (datasets.Metric ): """simple docstring""" def UpperCamelCase__ ( self : str ): return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { "predictions": datasets.Sequence(datasets.Value("string" ) ), "references": datasets.Sequence(datasets.Value("string" ) ), } ) , codebase_urls=["https://github.com/ns-moosavi/coval"] , reference_urls=[ "https://github.com/ns-moosavi/coval", "https://www.aclweb.org/anthology/P16-1060", "http://www.conll.cemantix.org/2012/data.html", ] , ) def UpperCamelCase__ ( self : int , __a : Any , __a : int , __a : Optional[Any]=True , __a : Optional[Any]=False , __a : str=False , __a : List[str]=False ): _a = [ ("mentions", evaluator.mentions), ("muc", evaluator.muc), ("bcub", evaluator.b_cubed), ("ceafe", evaluator.ceafe), ("lea", evaluator.lea), ] if min_span: _a = util.check_gold_parse_annotation(__a ) if not has_gold_parse: raise NotImplementedError("References should have gold parse annotation to use 'min_span'." ) # util.parse_key_file(key_file) # key_file = key_file + ".parsed" _a = evaluate( key_lines=__a , sys_lines=__a , metrics=__a , NP_only=__a , remove_nested=__a , keep_singletons=__a , min_span=__a , ) return score
692
'''simple docstring''' import argparse from typing import Dict import tensorflow as tf import torch from tqdm import tqdm from transformers import BigBirdPegasusConfig, BigBirdPegasusForConditionalGeneration lowerCAmelCase_ : List[Any] = [ # tf -> hf ('/', '.'), ('layer_', 'layers.'), ('kernel', 'weight'), ('beta', 'bias'), ('gamma', 'weight'), ('pegasus', 'model'), ] lowerCAmelCase_ : Optional[int] = [ ('.output.dense', '.fc2'), ('intermediate.LayerNorm', 'final_layer_norm'), ('intermediate.dense', 'fc1'), ] lowerCAmelCase_ : Any = ( INIT_COMMON + [ ('attention.self.LayerNorm', 'self_attn_layer_norm'), ('attention.output.dense', 'self_attn.out_proj'), ('attention.self', 'self_attn'), ('attention.encdec.LayerNorm', 'encoder_attn_layer_norm'), ('attention.encdec_output.dense', 'encoder_attn.out_proj'), ('attention.encdec', 'encoder_attn'), ('key', 'k_proj'), ('value', 'v_proj'), ('query', 'q_proj'), ('decoder.LayerNorm', 'decoder.layernorm_embedding'), ] + END_COMMON ) lowerCAmelCase_ : Tuple = ( INIT_COMMON + [ ('embeddings.word_embeddings', 'shared.weight'), ('embeddings.position_embeddings', 'embed_positions.weight'), ('attention.self.LayerNorm', 'self_attn_layer_norm'), ('attention.output.dense', 'self_attn.output'), ('attention.self', 'self_attn.self'), ('encoder.LayerNorm', 'encoder.layernorm_embedding'), ] + END_COMMON ) lowerCAmelCase_ : Optional[int] = [ 'encdec/key/bias', 'encdec/query/bias', 'encdec/value/bias', 'self/key/bias', 'self/query/bias', 'self/value/bias', 'encdec_output/dense/bias', 'attention/output/dense/bias', ] def _lowerCamelCase ( lowercase : Any , lowercase : Any ) -> Optional[Any]: for tf_name, hf_name in patterns: _a = k.replace(lowercase , lowercase ) return k def _lowerCamelCase ( lowercase : dict , lowercase : dict ) -> BigBirdPegasusForConditionalGeneration: _a = BigBirdPegasusConfig(**lowercase ) _a = BigBirdPegasusForConditionalGeneration(lowercase ) _a = torch_model.state_dict() _a = {} # separating decoder weights _a = {k: tf_weights[k] for k in tf_weights if k.startswith("pegasus/decoder" )} _a = {k: tf_weights[k] for k in tf_weights if not k.startswith("pegasus/decoder" )} for k, v in tqdm(decoder_weights.items() , "tf -> hf conversion" ): _a = [k.endswith(lowercase ) for ending in KEYS_TO_IGNORE] if any(lowercase ): continue _a = DECODER_PATTERNS _a = rename_state_dict_key(lowercase , lowercase ) if new_k not in state_dict: raise ValueError(F'could not find new key {new_k} in state dict. (converted from {k})' ) if any(True if i in k else False for i in ["dense", "query", "key", "value"] ): _a = v.T _a = torch.from_numpy(lowercase ) assert v.shape == state_dict[new_k].shape, F'{new_k}, {k}, {v.shape}, {state_dict[new_k].shape}' for k, v in tqdm(remaining_weights.items() , "tf -> hf conversion" ): _a = [k.endswith(lowercase ) for ending in KEYS_TO_IGNORE] if any(lowercase ): continue _a = REMAINING_PATTERNS _a = rename_state_dict_key(lowercase , lowercase ) if new_k not in state_dict and k != "pegasus/embeddings/position_embeddings": raise ValueError(F'could not find new key {new_k} in state dict. (converted from {k})' ) if any(True if i in k else False for i in ["dense", "query", "key", "value"] ): _a = v.T _a = torch.from_numpy(lowercase ) if k != "pegasus/embeddings/position_embeddings": assert v.shape == state_dict[new_k].shape, F'{new_k}, {k}, {v.shape}, {state_dict[new_k].shape}' _a = mapping["model.embed_positions.weight"] _a = mapping.pop("model.embed_positions.weight" ) _a , _a = torch_model.load_state_dict(lowercase , strict=lowercase ) _a = [ k for k in missing if k not in [ "final_logits_bias", "model.encoder.embed_tokens.weight", "model.decoder.embed_tokens.weight", "lm_head.weight", ] ] assert unexpected_missing == [], F'no matches found for the following torch keys {unexpected_missing}' assert extra == [], F'no matches found for the following tf keys {extra}' return torch_model def _lowerCamelCase ( lowercase : List[Any] ) -> Dict: _a = tf.train.list_variables(lowercase ) _a = {} _a = ["global_step"] for name, shape in tqdm(lowercase , desc="converting tf checkpoint to dict" ): _a = any(pat in name for pat in ignore_name ) if skip_key: continue _a = tf.train.load_variable(lowercase , lowercase ) _a = array return tf_weights def _lowerCamelCase ( lowercase : str , lowercase : str , lowercase : dict ) -> Union[str, Any]: _a = get_tf_weights_as_numpy(lowercase ) _a = convert_bigbird_pegasus(lowercase , lowercase ) torch_model.save_pretrained(lowercase ) if __name__ == "__main__": lowerCAmelCase_ : str = argparse.ArgumentParser() parser.add_argument('--tf_ckpt_path', type=str, help='passed to tf.train.list_variables') parser.add_argument('--save_dir', default=None, type=str, help='Path to the output PyTorch model.') lowerCAmelCase_ : Optional[Any] = parser.parse_args() lowerCAmelCase_ : Optional[Any] = {} convert_bigbird_pegasus_ckpt_to_pytorch(args.tf_ckpt_path, args.save_dir, config_update=config_update)
692
1
'''simple docstring''' import os import sys import unittest lowerCAmelCase_ : str = os.path.abspath(os.path.dirname(os.path.dirname(os.path.dirname(__file__)))) sys.path.append(os.path.join(git_repo_path, 'utils')) import check_dummies # noqa: E402 from check_dummies import create_dummy_files, create_dummy_object, find_backend, read_init # noqa: E402 # Align TRANSFORMERS_PATH in check_dummies with the current path lowerCAmelCase_ : int = os.path.join(git_repo_path, 'src', 'transformers') lowerCAmelCase_ : Dict = '\n{0} = None\n' lowerCAmelCase_ : str = '\nclass {0}(metaclass=DummyObject):\n _backends = {1}\n\n def __init__(self, *args, **kwargs):\n requires_backends(self, {1})\n' lowerCAmelCase_ : Any = '\ndef {0}(*args, **kwargs):\n requires_backends({0}, {1})\n' class __SCREAMING_SNAKE_CASE (unittest.TestCase ): """simple docstring""" def UpperCamelCase__ ( self : Any ): _a = find_backend(" _import_structure[\"models.albert\"].append(\"AlbertTokenizerFast\")" ) self.assertIsNone(__a ) _a = find_backend(" if not is_tokenizers_available():" ) self.assertEqual(__a , "tokenizers" ) _a = find_backend(" if not is_tensorflow_text_available():" ) self.assertEqual(__a , "tensorflow_text" ) _a = find_backend(" if not (is_sentencepiece_available() and is_tokenizers_available()):" ) self.assertEqual(__a , "sentencepiece_and_tokenizers" ) _a = find_backend( " if not (is_sentencepiece_available() and is_tensorflow_text_available()):" ) self.assertEqual(__a , "sentencepiece_and_tensorflow_text" ) _a = find_backend( " if not (is_sentencepiece_available() and is_tokenizers_available() and is_vision_available()):" ) self.assertEqual(__a , "sentencepiece_and_tokenizers_and_vision" ) def UpperCamelCase__ ( self : List[Any] ): _a = read_init() # We don't assert on the exact list of keys to allow for smooth grow of backend-specific objects self.assertIn("torch" , __a ) self.assertIn("tensorflow_text" , __a ) self.assertIn("sentencepiece_and_tokenizers" , __a ) # Likewise, we can't assert on the exact content of a key self.assertIn("BertModel" , objects["torch"] ) self.assertIn("TFBertModel" , objects["tf"] ) self.assertIn("FlaxBertModel" , objects["flax"] ) self.assertIn("BertModel" , objects["torch"] ) self.assertIn("TFBertTokenizer" , objects["tensorflow_text"] ) self.assertIn("convert_slow_tokenizer" , objects["sentencepiece_and_tokenizers"] ) def UpperCamelCase__ ( self : List[str] ): _a = create_dummy_object("CONSTANT" , "'torch'" ) self.assertEqual(__a , "\nCONSTANT = None\n" ) _a = create_dummy_object("function" , "'torch'" ) self.assertEqual( __a , "\ndef function(*args, **kwargs):\n requires_backends(function, 'torch')\n" ) _a = "\nclass FakeClass(metaclass=DummyObject):\n _backends = 'torch'\n\n def __init__(self, *args, **kwargs):\n requires_backends(self, 'torch')\n" _a = create_dummy_object("FakeClass" , "'torch'" ) self.assertEqual(__a , __a ) def UpperCamelCase__ ( self : Dict ): _a = "# This file is autogenerated by the command `make fix-copies`, do not edit.\nfrom ..utils import DummyObject, requires_backends\n\n\nCONSTANT = None\n\n\ndef function(*args, **kwargs):\n requires_backends(function, [\"torch\"])\n\n\nclass FakeClass(metaclass=DummyObject):\n _backends = [\"torch\"]\n\n def __init__(self, *args, **kwargs):\n requires_backends(self, [\"torch\"])\n" _a = create_dummy_files({"torch": ["CONSTANT", "function", "FakeClass"]} ) self.assertEqual(dummy_files["torch"] , __a )
692
'''simple docstring''' def _lowerCamelCase ( lowercase : str , lowercase : list[str] ) -> str: _a = "" for word_or_phrase in separated: if not isinstance(lowercase , lowercase ): raise Exception("join() accepts only strings to be joined" ) joined += word_or_phrase + separator return joined.strip(lowercase ) if __name__ == "__main__": from doctest import testmod testmod()
692
1
'''simple docstring''' import torch from diffusers import KDPMaDiscreteScheduler from diffusers.utils import torch_device from .test_schedulers import SchedulerCommonTest class __SCREAMING_SNAKE_CASE (lowerCamelCase_ ): """simple docstring""" __a =(KDPMaDiscreteScheduler,) __a =10 def UpperCamelCase__ ( self : Optional[int] , **__a : Any ): _a = { "num_train_timesteps": 11_00, "beta_start": 0.0001, "beta_end": 0.02, "beta_schedule": "linear", } config.update(**__a ) return config def UpperCamelCase__ ( self : Any ): for timesteps in [10, 50, 1_00, 10_00]: self.check_over_configs(num_train_timesteps=__a ) def UpperCamelCase__ ( self : Any ): for beta_start, beta_end in zip([0.00001, 0.0001, 0.001] , [0.0002, 0.002, 0.02] ): self.check_over_configs(beta_start=__a , beta_end=__a ) def UpperCamelCase__ ( self : Union[str, Any] ): for schedule in ["linear", "scaled_linear"]: self.check_over_configs(beta_schedule=__a ) def UpperCamelCase__ ( self : List[str] ): for prediction_type in ["epsilon", "v_prediction"]: self.check_over_configs(prediction_type=__a ) def UpperCamelCase__ ( self : Union[str, Any] ): _a = self.scheduler_classes[0] _a = self.get_scheduler_config(prediction_type="v_prediction" ) _a = scheduler_class(**__a ) scheduler.set_timesteps(self.num_inference_steps ) _a = self.dummy_model() _a = self.dummy_sample_deter * scheduler.init_noise_sigma _a = sample.to(__a ) for i, t in enumerate(scheduler.timesteps ): _a = scheduler.scale_model_input(__a , __a ) _a = model(__a , __a ) _a = scheduler.step(__a , __a , __a ) _a = output.prev_sample _a = torch.sum(torch.abs(__a ) ) _a = torch.mean(torch.abs(__a ) ) if torch_device in ["cpu", "mps"]: assert abs(result_sum.item() - 4.6_9_3_4e-0_7 ) < 1e-2 assert abs(result_mean.item() - 6.1_1_1_2e-1_0 ) < 1e-3 else: # CUDA assert abs(result_sum.item() - 4.6_9_3_4_2_8_6_5_0_1_7_0_9_7_2e-0_7 ) < 1e-2 assert abs(result_mean.item() - 0.0002 ) < 1e-3 def UpperCamelCase__ ( self : List[str] ): if torch_device == "mps": return _a = self.scheduler_classes[0] _a = self.get_scheduler_config() _a = scheduler_class(**__a ) scheduler.set_timesteps(self.num_inference_steps ) _a = self.dummy_model() _a = self.dummy_sample_deter * scheduler.init_noise_sigma _a = sample.to(__a ) for i, t in enumerate(scheduler.timesteps ): _a = scheduler.scale_model_input(__a , __a ) _a = model(__a , __a ) _a = scheduler.step(__a , __a , __a ) _a = output.prev_sample _a = torch.sum(torch.abs(__a ) ) _a = torch.mean(torch.abs(__a ) ) if torch_device in ["cpu", "mps"]: assert abs(result_sum.item() - 20.4125 ) < 1e-2 assert abs(result_mean.item() - 0.0266 ) < 1e-3 else: # CUDA assert abs(result_sum.item() - 20.4125 ) < 1e-2 assert abs(result_mean.item() - 0.0266 ) < 1e-3 def UpperCamelCase__ ( self : Union[str, Any] ): if torch_device == "mps": return _a = self.scheduler_classes[0] _a = self.get_scheduler_config() _a = scheduler_class(**__a ) scheduler.set_timesteps(self.num_inference_steps , device=__a ) _a = self.dummy_model() _a = self.dummy_sample_deter.to(__a ) * scheduler.init_noise_sigma for t in scheduler.timesteps: _a = scheduler.scale_model_input(__a , __a ) _a = model(__a , __a ) _a = scheduler.step(__a , __a , __a ) _a = output.prev_sample _a = torch.sum(torch.abs(__a ) ) _a = torch.mean(torch.abs(__a ) ) if str(__a ).startswith("cpu" ): # The following sum varies between 148 and 156 on mps. Why? assert abs(result_sum.item() - 20.4125 ) < 1e-2 assert abs(result_mean.item() - 0.0266 ) < 1e-3 else: # CUDA assert abs(result_sum.item() - 20.4125 ) < 1e-2 assert abs(result_mean.item() - 0.0266 ) < 1e-3
692
'''simple docstring''' lowerCAmelCase_ : Optional[Any] = '\n# Transformers ์„ค์น˜ ๋ฐฉ๋ฒ•\n! pip install transformers datasets\n# ๋งˆ์ง€๋ง‰ ๋ฆด๋ฆฌ์Šค ๋Œ€์‹  ์†Œ์Šค์—์„œ ์„ค์น˜ํ•˜๋ ค๋ฉด, ์œ„ ๋ช…๋ น์„ ์ฃผ์„์œผ๋กœ ๋ฐ”๊พธ๊ณ  ์•„๋ž˜ ๋ช…๋ น์„ ํ•ด์ œํ•˜์„ธ์š”.\n# ! pip install git+https://github.com/huggingface/transformers.git\n' lowerCAmelCase_ : List[Any] = [{'type': 'code', 'content': INSTALL_CONTENT}] lowerCAmelCase_ : Dict = { '{processor_class}': 'FakeProcessorClass', '{model_class}': 'FakeModelClass', '{object_class}': 'FakeObjectClass', }
692
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_ : Tuple = logging.get_logger(__name__) class __SCREAMING_SNAKE_CASE : """simple docstring""" def __init__( self : Tuple , __a : Dict , __a : Union[str, Any] ): _a = question_encoder _a = generator _a = self.question_encoder def UpperCamelCase__ ( self : Optional[Any] , __a : Union[str, Any] ): if os.path.isfile(__a ): raise ValueError(f'Provided path ({save_directory}) should be a directory, not a file' ) os.makedirs(__a , exist_ok=__a ) _a = os.path.join(__a , "question_encoder_tokenizer" ) _a = os.path.join(__a , "generator_tokenizer" ) self.question_encoder.save_pretrained(__a ) self.generator.save_pretrained(__a ) @classmethod def UpperCamelCase__ ( cls : Any , __a : Optional[Any] , **__a : List[Any] ): # dynamically import AutoTokenizer from ..auto.tokenization_auto import AutoTokenizer _a = kwargs.pop("config" , __a ) if config is None: _a = RagConfig.from_pretrained(__a ) _a = AutoTokenizer.from_pretrained( __a , config=config.question_encoder , subfolder="question_encoder_tokenizer" ) _a = AutoTokenizer.from_pretrained( __a , config=config.generator , subfolder="generator_tokenizer" ) return cls(question_encoder=__a , generator=__a ) def __call__( self : Union[str, Any] , *__a : Dict , **__a : List[str] ): return self.current_tokenizer(*__a , **__a ) def UpperCamelCase__ ( self : Tuple , *__a : Optional[int] , **__a : List[Any] ): return self.generator.batch_decode(*__a , **__a ) def UpperCamelCase__ ( self : Union[str, Any] , *__a : Any , **__a : Optional[int] ): return self.generator.decode(*__a , **__a ) def UpperCamelCase__ ( self : str ): _a = self.question_encoder def UpperCamelCase__ ( self : Dict ): _a = self.generator def UpperCamelCase__ ( self : List[Any] , __a : List[str] , __a : Optional[List[str]] = None , __a : Optional[int] = None , __a : Optional[int] = None , __a : str = "longest" , __a : str = None , __a : bool = True , **__a : int , ): 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" , __a , ) if max_length is None: _a = self.current_tokenizer.model_max_length _a = self( __a , add_special_tokens=__a , return_tensors=__a , max_length=__a , padding=__a , truncation=__a , **__a , ) 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=__a , add_special_tokens=__a , return_tensors=__a , padding=__a , max_length=__a , truncation=__a , **__a , ) _a = labels["input_ids"] return model_inputs
692
'''simple docstring''' import sys import webbrowser import requests from bsa import BeautifulSoup from fake_useragent import UserAgent if __name__ == "__main__": print('Googling.....') lowerCAmelCase_ : Optional[Any] = 'https://www.google.com/search?q=' + ' '.join(sys.argv[1:]) lowerCAmelCase_ : Dict = 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(1_00_00): out_file.write(data) lowerCAmelCase_ : Dict = BeautifulSoup(res.text, 'html.parser') lowerCAmelCase_ : Optional[int] = 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')}""")
692
1
'''simple docstring''' import math def _lowerCamelCase ( lowercase : int ) -> int: if not isinstance(lowercase , lowercase ): _a = F'Input value of [number={number}] must be an integer' raise TypeError(lowercase ) if number < 1: _a = F'Input value of [number={number}] must be > 0' raise ValueError(lowercase ) elif number == 1: return 3 elif number == 2: return 5 else: _a = int(math.log(number // 3 , 2 ) ) + 2 _a = [3, 5] _a = 2 _a = 3 for block in range(1 , lowercase ): for _ in range(lowercase ): proth_list.append(2 ** (block + 1) + proth_list[proth_index - 1] ) proth_index += 1 increment *= 2 return proth_list[number - 1] if __name__ == "__main__": import doctest doctest.testmod() for number in range(11): lowerCAmelCase_ : str = 0 try: lowerCAmelCase_ : Tuple = proth(number) except ValueError: print(f"""ValueError: there is no {number}th Proth number""") continue print(f"""The {number}th Proth number: {value}""")
692
'''simple docstring''' import coval # From: git+https://github.com/ns-moosavi/coval.git # noqa: F401 from coval.conll import reader, util from coval.eval import evaluator import datasets lowerCAmelCase_ : Optional[Any] = datasets.logging.get_logger(__name__) lowerCAmelCase_ : Tuple = '\\n@InProceedings{moosavi2019minimum,\n author = { Nafise Sadat Moosavi, Leo Born, Massimo Poesio and Michael Strube},\n title = {Using Automatically Extracted Minimum Spans to Disentangle Coreference Evaluation from Boundary Detection},\n year = {2019},\n booktitle = {Proceedings of the 57th Annual Meeting of\n the Association for Computational Linguistics (Volume 1: Long Papers)},\n publisher = {Association for Computational Linguistics},\n address = {Florence, Italy},\n}\n\n@inproceedings{10.3115/1072399.1072405,\nauthor = {Vilain, Marc and Burger, John and Aberdeen, John and Connolly, Dennis and Hirschman, Lynette},\ntitle = {A Model-Theoretic Coreference Scoring Scheme},\nyear = {1995},\nisbn = {1558604022},\npublisher = {Association for Computational Linguistics},\naddress = {USA},\nurl = {https://doi.org/10.3115/1072399.1072405},\ndoi = {10.3115/1072399.1072405},\nbooktitle = {Proceedings of the 6th Conference on Message Understanding},\npages = {45โ€“52},\nnumpages = {8},\nlocation = {Columbia, Maryland},\nseries = {MUC6 โ€™95}\n}\n\n@INPROCEEDINGS{Bagga98algorithmsfor,\n author = {Amit Bagga and Breck Baldwin},\n title = {Algorithms for Scoring Coreference Chains},\n booktitle = {In The First International Conference on Language Resources and Evaluation Workshop on Linguistics Coreference},\n year = {1998},\n pages = {563--566}\n}\n\n@INPROCEEDINGS{Luo05oncoreference,\n author = {Xiaoqiang Luo},\n title = {On coreference resolution performance metrics},\n booktitle = {In Proc. of HLT/EMNLP},\n year = {2005},\n pages = {25--32},\n publisher = {URL}\n}\n\n@inproceedings{moosavi-strube-2016-coreference,\n title = "Which Coreference Evaluation Metric Do You Trust? A Proposal for a Link-based Entity Aware Metric",\n author = "Moosavi, Nafise Sadat and\n Strube, Michael",\n booktitle = "Proceedings of the 54th Annual Meeting of the Association for Computational Linguistics (Volume 1: Long Papers)",\n month = aug,\n year = "2016",\n address = "Berlin, Germany",\n publisher = "Association for Computational Linguistics",\n url = "https://www.aclweb.org/anthology/P16-1060",\n doi = "10.18653/v1/P16-1060",\n pages = "632--642",\n}\n\n' lowerCAmelCase_ : Union[str, Any] = '\\nCoVal is a coreference evaluation tool for the CoNLL and ARRAU datasets which\nimplements of the common evaluation metrics including MUC [Vilain et al, 1995],\nB-cubed [Bagga and Baldwin, 1998], CEAFe [Luo et al., 2005],\nLEA [Moosavi and Strube, 2016] and the averaged CoNLL score\n(the average of the F1 values of MUC, B-cubed and CEAFe)\n[Denis and Baldridge, 2009a; Pradhan et al., 2011].\n\nThis wrapper of CoVal currently only work with CoNLL line format:\nThe CoNLL format has one word per line with all the annotation for this word in column separated by spaces:\nColumn Type Description\n1 Document ID This is a variation on the document filename\n2 Part number Some files are divided into multiple parts numbered as 000, 001, 002, ... etc.\n3 Word number\n4 Word itself This is the token as segmented/tokenized in the Treebank. Initially the *_skel file contain the placeholder [WORD] which gets replaced by the actual token from the Treebank which is part of the OntoNotes release.\n5 Part-of-Speech\n6 Parse bit This is the bracketed structure broken before the first open parenthesis in the parse, and the word/part-of-speech leaf replaced with a *. The full parse can be created by substituting the asterix with the "([pos] [word])" string (or leaf) and concatenating the items in the rows of that column.\n7 Predicate lemma The predicate lemma is mentioned for the rows for which we have semantic role information. All other rows are marked with a "-"\n8 Predicate Frameset ID This is the PropBank frameset ID of the predicate in Column 7.\n9 Word sense This is the word sense of the word in Column 3.\n10 Speaker/Author This is the speaker or author name where available. Mostly in Broadcast Conversation and Web Log data.\n11 Named Entities These columns identifies the spans representing various named entities.\n12:N Predicate Arguments There is one column each of predicate argument structure information for the predicate mentioned in Column 7.\nN Coreference Coreference chain information encoded in a parenthesis structure.\nMore informations on the format can be found here (section "*_conll File Format"): http://www.conll.cemantix.org/2012/data.html\n\nDetails on the evaluation on CoNLL can be found here: https://github.com/ns-moosavi/coval/blob/master/conll/README.md\n\nCoVal code was written by @ns-moosavi.\nSome parts are borrowed from https://github.com/clarkkev/deep-coref/blob/master/evaluation.py\nThe test suite is taken from https://github.com/conll/reference-coreference-scorers/\nMention evaluation and the test suite are added by @andreasvc.\nParsing CoNLL files is developed by Leo Born.\n' lowerCAmelCase_ : Union[str, Any] = '\nCalculates coreference evaluation metrics.\nArgs:\n predictions: list of sentences. Each sentence is a list of word predictions to score in the CoNLL format.\n Each prediction is a word with its annotations as a string made of columns joined with spaces.\n Only columns 4, 5, 6 and the last column are used (word, POS, Pars and coreference annotation)\n See the details on the format in the description of the metric.\n references: list of sentences. Each sentence is a list of word reference to score in the CoNLL format.\n Each reference is a word with its annotations as a string made of columns joined with spaces.\n Only columns 4, 5, 6 and the last column are used (word, POS, Pars and coreference annotation)\n See the details on the format in the description of the metric.\n keep_singletons: After extracting all mentions of key or system files,\n mentions whose corresponding coreference chain is of size one,\n are considered as singletons. The default evaluation mode will include\n singletons in evaluations if they are included in the key or the system files.\n By setting \'keep_singletons=False\', all singletons in the key and system files\n will be excluded from the evaluation.\n NP_only: Most of the recent coreference resolvers only resolve NP mentions and\n leave out the resolution of VPs. By setting the \'NP_only\' option, the scorer will only evaluate the resolution of NPs.\n min_span: By setting \'min_span\', the scorer reports the results based on automatically detected minimum spans.\n Minimum spans are determined using the MINA algorithm.\n\nReturns:\n \'mentions\': mentions\n \'muc\': MUC metric [Vilain et al, 1995]\n \'bcub\': B-cubed [Bagga and Baldwin, 1998]\n \'ceafe\': CEAFe [Luo et al., 2005]\n \'lea\': LEA [Moosavi and Strube, 2016]\n \'conll_score\': averaged CoNLL score (the average of the F1 values of MUC, B-cubed and CEAFe)\n\nExamples:\n\n >>> coval = datasets.load_metric(\'coval\')\n >>> words = [\'bc/cctv/00/cctv_0005 0 0 Thank VBP (TOP(S(VP* thank 01 1 Xu_li * (V*) * -\',\n ... \'bc/cctv/00/cctv_0005 0 1 you PRP (NP*) - - - Xu_li * (ARG1*) (ARG0*) (116)\',\n ... \'bc/cctv/00/cctv_0005 0 2 everyone NN (NP*) - - - Xu_li * (ARGM-DIS*) * (116)\',\n ... \'bc/cctv/00/cctv_0005 0 3 for IN (PP* - - - Xu_li * (ARG2* * -\',\n ... \'bc/cctv/00/cctv_0005 0 4 watching VBG (S(VP*)))) watch 01 1 Xu_li * *) (V*) -\',\n ... \'bc/cctv/00/cctv_0005 0 5 . . *)) - - - Xu_li * * * -\']\n >>> references = [words]\n >>> predictions = [words]\n >>> results = coval.compute(predictions=predictions, references=references)\n >>> print(results) # doctest:+ELLIPSIS\n {\'mentions/recall\': 1.0,[...] \'conll_score\': 100.0}\n' def _lowerCamelCase ( lowercase : Tuple , lowercase : List[Any] , lowercase : Optional[int]=False , lowercase : Dict=False , lowercase : Optional[int]=True , lowercase : Union[str, Any]=False , lowercase : int="dummy_doc" ) -> Union[str, Any]: _a = {doc: key_lines} _a = {doc: sys_lines} _a = {} _a = 0 _a = 0 _a = 0 _a = 0 _a = 0 _a = 0 _a , _a = reader.get_doc_mentions(lowercase , key_doc_lines[doc] , lowercase ) key_singletons_num += singletons_num if NP_only or min_span: _a = reader.set_annotated_parse_trees(lowercase , key_doc_lines[doc] , lowercase , lowercase ) _a , _a = reader.get_doc_mentions(lowercase , sys_doc_lines[doc] , lowercase ) sys_singletons_num += singletons_num if NP_only or min_span: _a = reader.set_annotated_parse_trees(lowercase , key_doc_lines[doc] , lowercase , lowercase ) if remove_nested: _a , _a = reader.remove_nested_coref_mentions(lowercase , lowercase ) key_nested_coref_num += nested_mentions key_removed_nested_clusters += removed_clusters _a , _a = reader.remove_nested_coref_mentions(lowercase , lowercase ) sys_nested_coref_num += nested_mentions sys_removed_nested_clusters += removed_clusters _a = reader.get_mention_assignments(lowercase , lowercase ) _a = reader.get_mention_assignments(lowercase , lowercase ) _a = (key_clusters, sys_clusters, key_mention_sys_cluster, sys_mention_key_cluster) if remove_nested: logger.info( "Number of removed nested coreferring mentions in the key " F'annotation: {key_nested_coref_num}; and system annotation: {sys_nested_coref_num}' ) logger.info( "Number of resulting singleton clusters in the key " F'annotation: {key_removed_nested_clusters}; and system annotation: {sys_removed_nested_clusters}' ) if not keep_singletons: logger.info( F'{key_singletons_num:d} and {sys_singletons_num:d} singletons are removed from the key and system ' "files, respectively" ) return doc_coref_infos def _lowerCamelCase ( lowercase : List[Any] , lowercase : Any , lowercase : Optional[Any] , lowercase : Union[str, Any] , lowercase : Any , lowercase : List[str] , lowercase : Dict ) -> str: _a = get_coref_infos(lowercase , lowercase , lowercase , lowercase , lowercase , lowercase ) _a = {} _a = 0 _a = 0 for name, metric in metrics: _a , _a , _a = evaluator.evaluate_documents(lowercase , lowercase , beta=1 ) if name in ["muc", "bcub", "ceafe"]: conll += fa conll_subparts_num += 1 output_scores.update({F'{name}/recall': recall, F'{name}/precision': precision, F'{name}/f1': fa} ) logger.info( name.ljust(10 ) , F'Recall: {recall * 100:.2f}' , F' Precision: {precision * 100:.2f}' , F' F1: {fa * 100:.2f}' , ) if conll_subparts_num == 3: _a = (conll / 3) * 100 logger.info(F'CoNLL score: {conll:.2f}' ) output_scores.update({"conll_score": conll} ) return output_scores def _lowerCamelCase ( lowercase : Any ) -> str: _a = False for line in key_lines: if not line.startswith("#" ): if len(line.split() ) > 6: _a = line.split()[5] if not parse_col == "-": _a = True break else: break return has_gold_parse @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class __SCREAMING_SNAKE_CASE (datasets.Metric ): """simple docstring""" def UpperCamelCase__ ( self : str ): return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { "predictions": datasets.Sequence(datasets.Value("string" ) ), "references": datasets.Sequence(datasets.Value("string" ) ), } ) , codebase_urls=["https://github.com/ns-moosavi/coval"] , reference_urls=[ "https://github.com/ns-moosavi/coval", "https://www.aclweb.org/anthology/P16-1060", "http://www.conll.cemantix.org/2012/data.html", ] , ) def UpperCamelCase__ ( self : int , __a : Any , __a : int , __a : Optional[Any]=True , __a : Optional[Any]=False , __a : str=False , __a : List[str]=False ): _a = [ ("mentions", evaluator.mentions), ("muc", evaluator.muc), ("bcub", evaluator.b_cubed), ("ceafe", evaluator.ceafe), ("lea", evaluator.lea), ] if min_span: _a = util.check_gold_parse_annotation(__a ) if not has_gold_parse: raise NotImplementedError("References should have gold parse annotation to use 'min_span'." ) # util.parse_key_file(key_file) # key_file = key_file + ".parsed" _a = evaluate( key_lines=__a , sys_lines=__a , metrics=__a , NP_only=__a , remove_nested=__a , keep_singletons=__a , min_span=__a , ) return score
692
1
'''simple docstring''' from typing import Any class __SCREAMING_SNAKE_CASE : """simple docstring""" def __init__( self : str , __a : Any ): _a = data _a = None def __repr__( self : Dict ): return f'Node({self.data})' class __SCREAMING_SNAKE_CASE : """simple docstring""" def __init__( self : Optional[int] ): _a = None def __iter__( self : List[str] ): _a = self.head while node: yield node.data _a = node.next def __len__( self : Dict ): return sum(1 for _ in self ) def __repr__( self : List[Any] ): return "->".join([str(__a ) for item in self] ) def __getitem__( self : int , __a : int ): if not 0 <= index < len(self ): raise ValueError("list index out of range." ) for i, node in enumerate(self ): if i == index: return node return None def __setitem__( self : List[Any] , __a : int , __a : Any ): if not 0 <= index < len(self ): raise ValueError("list index out of range." ) _a = self.head for _ in range(__a ): _a = current.next _a = data def UpperCamelCase__ ( self : Tuple , __a : Any ): self.insert_nth(len(self ) , __a ) def UpperCamelCase__ ( self : Any , __a : Any ): self.insert_nth(0 , __a ) def UpperCamelCase__ ( self : str , __a : int , __a : Any ): if not 0 <= index <= len(self ): raise IndexError("list index out of range" ) _a = Node(__a ) if self.head is None: _a = new_node elif index == 0: _a = self.head # link new_node to head _a = new_node else: _a = self.head for _ in range(index - 1 ): _a = temp.next _a = temp.next _a = new_node def UpperCamelCase__ ( self : Any ): # print every node data print(self ) def UpperCamelCase__ ( self : int ): return self.delete_nth(0 ) def UpperCamelCase__ ( self : str ): # delete from tail return self.delete_nth(len(self ) - 1 ) def UpperCamelCase__ ( self : str , __a : int = 0 ): if not 0 <= index <= len(self ) - 1: # test if index is valid raise IndexError("List index out of range." ) _a = self.head # default first node if index == 0: _a = self.head.next else: _a = self.head for _ in range(index - 1 ): _a = temp.next _a = temp.next _a = temp.next.next return delete_node.data def UpperCamelCase__ ( self : Tuple ): return self.head is None def UpperCamelCase__ ( self : int ): _a = None _a = self.head while current: # Store the current node's next node. _a = current.next # Make the current node's next point backwards _a = prev # Make the previous node be the current node _a = current # Make the current node the next node (to progress iteration) _a = next_node # Return prev in order to put the head at the end _a = prev def _lowerCamelCase ( ) -> None: _a = LinkedList() assert linked_list.is_empty() is True assert str(lowercase ) == "" try: linked_list.delete_head() raise AssertionError # This should not happen. except IndexError: assert True # This should happen. try: linked_list.delete_tail() raise AssertionError # This should not happen. except IndexError: assert True # This should happen. for i in range(10 ): assert len(lowercase ) == i linked_list.insert_nth(lowercase , i + 1 ) assert str(lowercase ) == "->".join(str(lowercase ) for i in range(1 , 11 ) ) linked_list.insert_head(0 ) linked_list.insert_tail(11 ) assert str(lowercase ) == "->".join(str(lowercase ) for i in range(0 , 12 ) ) assert linked_list.delete_head() == 0 assert linked_list.delete_nth(9 ) == 10 assert linked_list.delete_tail() == 11 assert len(lowercase ) == 9 assert str(lowercase ) == "->".join(str(lowercase ) for i in range(1 , 10 ) ) assert all(linked_list[i] == i + 1 for i in range(0 , 9 ) ) is True for i in range(0 , 9 ): _a = -i assert all(linked_list[i] == -i for i in range(0 , 9 ) ) is True linked_list.reverse() assert str(lowercase ) == "->".join(str(lowercase ) for i in range(-8 , 1 ) ) def _lowerCamelCase ( ) -> None: _a = [ -9, 100, Node(7734_5112 ), "dlrow olleH", 7, 5555, 0, -1_92.5_55_55, "Hello, world!", 77.9, Node(10 ), None, None, 12.20, ] _a = LinkedList() for i in test_input: linked_list.insert_tail(lowercase ) # Check if it's empty or not assert linked_list.is_empty() is False assert ( str(lowercase ) == "-9->100->Node(77345112)->dlrow olleH->7->5555->0->" "-192.55555->Hello, world!->77.9->Node(10)->None->None->12.2" ) # Delete the head _a = linked_list.delete_head() assert result == -9 assert ( str(lowercase ) == "100->Node(77345112)->dlrow olleH->7->5555->0->-192.55555->" "Hello, world!->77.9->Node(10)->None->None->12.2" ) # Delete the tail _a = linked_list.delete_tail() assert result == 12.2 assert ( str(lowercase ) == "100->Node(77345112)->dlrow olleH->7->5555->0->-192.55555->" "Hello, world!->77.9->Node(10)->None->None" ) # Delete a node in specific location in linked list _a = linked_list.delete_nth(10 ) assert result is None assert ( str(lowercase ) == "100->Node(77345112)->dlrow olleH->7->5555->0->-192.55555->" "Hello, world!->77.9->Node(10)->None" ) # Add a Node instance to its head linked_list.insert_head(Node("Hello again, world!" ) ) assert ( str(lowercase ) == "Node(Hello again, world!)->100->Node(77345112)->dlrow olleH->" "7->5555->0->-192.55555->Hello, world!->77.9->Node(10)->None" ) # Add None to its tail linked_list.insert_tail(lowercase ) assert ( str(lowercase ) == "Node(Hello again, world!)->100->Node(77345112)->dlrow olleH->" "7->5555->0->-192.55555->Hello, world!->77.9->Node(10)->None->None" ) # Reverse the linked list linked_list.reverse() assert ( str(lowercase ) == "None->None->Node(10)->77.9->Hello, world!->-192.55555->0->5555->" "7->dlrow olleH->Node(77345112)->100->Node(Hello again, world!)" ) def _lowerCamelCase ( ) -> Optional[Any]: from doctest import testmod testmod() _a = LinkedList() linked_list.insert_head(input("Inserting 1st at head " ).strip() ) linked_list.insert_head(input("Inserting 2nd at head " ).strip() ) print("\nPrint list:" ) linked_list.print_list() linked_list.insert_tail(input("\nInserting 1st at tail " ).strip() ) linked_list.insert_tail(input("Inserting 2nd at tail " ).strip() ) print("\nPrint list:" ) linked_list.print_list() print("\nDelete head" ) linked_list.delete_head() print("Delete tail" ) linked_list.delete_tail() print("\nPrint list:" ) linked_list.print_list() print("\nReverse linked list" ) linked_list.reverse() print("\nPrint list:" ) linked_list.print_list() print("\nString representation of linked list:" ) print(lowercase ) print("\nReading/changing Node data using indexing:" ) print(F'Element at Position 1: {linked_list[1]}' ) _a = input("Enter New Value: " ).strip() print("New list:" ) print(lowercase ) print(F'length of linked_list is : {len(lowercase )}' ) if __name__ == "__main__": main()
692
'''simple docstring''' import math def _lowerCamelCase ( lowercase : int ) -> 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(lowercase ) + 1 ) , 6 ): if number % i == 0 or number % (i + 2) == 0: return False return True def _lowerCamelCase ( lowercase : float = 0.1 ) -> int: _a = 3 _a = 3 while primes / (2 * j - 1) >= ratio: for i in range(j * j + j + 1 , (j + 2) * (j + 2) , j + 1 ): primes += is_prime(lowercase ) j += 2 return j if __name__ == "__main__": import doctest doctest.testmod()
692
1