code
stringlengths
86
54.5k
code_codestyle
int64
0
371
style_context
stringlengths
87
49.2k
style_context_codestyle
int64
0
349
label
int64
0
1
"""simple docstring""" from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tokenizers_available, is_torch_available __A = { '''configuration_canine''': ['''CANINE_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''CanineConfig'''], '''tokenization_canine''': ['''CanineTokenizer'''], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __A = [ '''CANINE_PRETRAINED_MODEL_ARCHIVE_LIST''', '''CanineForMultipleChoice''', '''CanineForQuestionAnswering''', '''CanineForSequenceClassification''', '''CanineForTokenClassification''', '''CanineLayer''', '''CanineModel''', '''CaninePreTrainedModel''', '''load_tf_weights_in_canine''', ] if TYPE_CHECKING: from .configuration_canine import CANINE_PRETRAINED_CONFIG_ARCHIVE_MAP, CanineConfig from .tokenization_canine import CanineTokenizer try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_canine import ( CANINE_PRETRAINED_MODEL_ARCHIVE_LIST, CanineForMultipleChoice, CanineForQuestionAnswering, CanineForSequenceClassification, CanineForTokenClassification, CanineLayer, CanineModel, CaninePreTrainedModel, load_tf_weights_in_canine, ) else: import sys __A = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
217
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 ( A__ ): def __lowercase ( self : Any ): '''simple docstring''' UpperCamelCase__ : List[str] = tempfile.mkdtemp() UpperCamelCase__ : Any = 8 # DPR tok UpperCamelCase__ : List[Any] = [ "[UNK]", "[CLS]", "[SEP]", "[PAD]", "[MASK]", "want", "##want", "##ed", "wa", "un", "runn", "##ing", ",", "low", "lowest", ] UpperCamelCase__ : Any = os.path.join(self.tmpdirname , "dpr_tokenizer" ) os.makedirs(SCREAMING_SNAKE_CASE , exist_ok=SCREAMING_SNAKE_CASE ) UpperCamelCase__ : Optional[int] = os.path.join(SCREAMING_SNAKE_CASE , DPR_VOCAB_FILES_NAMES["vocab_file"] ) with open(self.vocab_file , "w" , encoding="utf-8" ) as vocab_writer: vocab_writer.write("".join([x + "\n" for x in vocab_tokens] ) ) # BART tok UpperCamelCase__ : str = [ "l", "o", "w", "e", "r", "s", "t", "i", "d", "n", "\u0120", "\u0120l", "\u0120n", "\u0120lo", "\u0120low", "er", "\u0120lowest", "\u0120newer", "\u0120wider", "<unk>", ] UpperCamelCase__ : int = dict(zip(SCREAMING_SNAKE_CASE , range(len(SCREAMING_SNAKE_CASE ) ) ) ) UpperCamelCase__ : Optional[Any] = ["#version: 0.2", "\u0120 l", "\u0120l o", "\u0120lo w", "e r", ""] UpperCamelCase__ : Union[str, Any] = {"unk_token": "<unk>"} UpperCamelCase__ : Dict = os.path.join(self.tmpdirname , "bart_tokenizer" ) os.makedirs(SCREAMING_SNAKE_CASE , exist_ok=SCREAMING_SNAKE_CASE ) UpperCamelCase__ : List[str] = os.path.join(SCREAMING_SNAKE_CASE , BART_VOCAB_FILES_NAMES["vocab_file"] ) UpperCamelCase__ : str = os.path.join(SCREAMING_SNAKE_CASE , BART_VOCAB_FILES_NAMES["merges_file"] ) with open(self.vocab_file , "w" , encoding="utf-8" ) as fp: fp.write(json.dumps(SCREAMING_SNAKE_CASE ) + "\n" ) with open(self.merges_file , "w" , encoding="utf-8" ) as fp: fp.write("\n".join(SCREAMING_SNAKE_CASE ) ) def __lowercase ( self : int ): '''simple docstring''' return DPRQuestionEncoderTokenizer.from_pretrained(os.path.join(self.tmpdirname , "dpr_tokenizer" ) ) def __lowercase ( self : Tuple ): '''simple docstring''' return DPRContextEncoderTokenizer.from_pretrained(os.path.join(self.tmpdirname , "dpr_tokenizer" ) ) def __lowercase ( self : Optional[int] ): '''simple docstring''' return BartTokenizer.from_pretrained(os.path.join(self.tmpdirname , "bart_tokenizer" ) ) def __lowercase ( self : Optional[Any] ): '''simple docstring''' shutil.rmtree(self.tmpdirname ) def __lowercase ( self : Tuple ): '''simple docstring''' UpperCamelCase__ : Tuple = 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 __lowercase ( self : Optional[int] ): '''simple docstring''' UpperCamelCase__ : int = self.get_dummy_dataset() UpperCamelCase__ : List[Any] = 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: UpperCamelCase__ : str = dataset UpperCamelCase__ : Optional[int] = RagRetriever( SCREAMING_SNAKE_CASE , question_encoder_tokenizer=self.get_dpr_tokenizer() , generator_tokenizer=self.get_bart_tokenizer() , ) return retriever def __lowercase ( self : int , SCREAMING_SNAKE_CASE : bool ): '''simple docstring''' UpperCamelCase__ : Dict = self.get_dummy_dataset() UpperCamelCase__ : Tuple = RagConfig( retrieval_vector_size=self.retrieval_vector_size , question_encoder=DPRConfig().to_dict() , generator=BartConfig().to_dict() , index_name="custom" , ) if from_disk: UpperCamelCase__ : Optional[int] = os.path.join(self.tmpdirname , "dataset" ) UpperCamelCase__ : List[str] = os.path.join(self.tmpdirname , "index.faiss" ) dataset.get_index("embeddings" ).save(os.path.join(self.tmpdirname , "index.faiss" ) ) dataset.drop_index("embeddings" ) dataset.save_to_disk(os.path.join(self.tmpdirname , "dataset" ) ) del dataset UpperCamelCase__ : str = RagRetriever( SCREAMING_SNAKE_CASE , question_encoder_tokenizer=self.get_dpr_tokenizer() , generator_tokenizer=self.get_bart_tokenizer() , ) else: UpperCamelCase__ : List[Any] = RagRetriever( SCREAMING_SNAKE_CASE , question_encoder_tokenizer=self.get_dpr_tokenizer() , generator_tokenizer=self.get_bart_tokenizer() , index=CustomHFIndex(config.retrieval_vector_size , SCREAMING_SNAKE_CASE ) , ) return retriever def __lowercase ( self : int ): '''simple docstring''' UpperCamelCase__ : int = 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 ) UpperCamelCase__ : List[str] = 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" ) ) UpperCamelCase__ : Optional[int] = os.path.join(self.tmpdirname , "psgs_w100.tsv.pkl" ) UpperCamelCase__ : Tuple = {sample["id"]: [sample["text"], sample["title"]] for sample in dataset} pickle.dump(SCREAMING_SNAKE_CASE , open(SCREAMING_SNAKE_CASE , "wb" ) ) UpperCamelCase__ : List[str] = RagConfig( retrieval_vector_size=self.retrieval_vector_size , question_encoder=DPRConfig().to_dict() , generator=BartConfig().to_dict() , index_name="legacy" , index_path=self.tmpdirname , ) UpperCamelCase__ : List[str] = RagRetriever( SCREAMING_SNAKE_CASE , question_encoder_tokenizer=self.get_dpr_tokenizer() , generator_tokenizer=self.get_bart_tokenizer() ) return retriever def __lowercase ( self : Optional[Any] ): '''simple docstring''' UpperCamelCase__ : Union[str, Any] = 1 UpperCamelCase__ : Tuple = self.get_dummy_canonical_hf_index_retriever() UpperCamelCase__ : str = np.array( [np.ones(self.retrieval_vector_size ), -np.ones(self.retrieval_vector_size )] , dtype=np.floataa ) UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ : List[str] = retriever.retrieve(SCREAMING_SNAKE_CASE , n_docs=SCREAMING_SNAKE_CASE ) self.assertEqual(retrieved_doc_embeds.shape , (2, n_docs, self.retrieval_vector_size) ) self.assertEqual(len(SCREAMING_SNAKE_CASE ) , 2 ) self.assertEqual(sorted(doc_dicts[0] ) , ["embeddings", "id", "text", "title"] ) self.assertEqual(len(doc_dicts[0]["id"] ) , SCREAMING_SNAKE_CASE ) self.assertEqual(doc_dicts[0]["id"][0] , "1" ) # max inner product is reached with second doc self.assertEqual(doc_dicts[1]["id"][0] , "0" ) # max inner product is reached with first doc self.assertListEqual(doc_ids.tolist() , [[1], [0]] ) def __lowercase ( self : Tuple ): '''simple docstring''' UpperCamelCase__ : Optional[int] = 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: UpperCamelCase__ : Optional[int] = self.get_dummy_dataset() retriever.save_pretrained(SCREAMING_SNAKE_CASE ) UpperCamelCase__ : Any = RagRetriever.from_pretrained(SCREAMING_SNAKE_CASE ) self.assertIsInstance(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ) UpperCamelCase__ : str = np.array( [np.ones(self.retrieval_vector_size ), -np.ones(self.retrieval_vector_size )] , dtype=np.floataa ) UpperCamelCase__ : Any = retriever.retrieve(SCREAMING_SNAKE_CASE , n_docs=1 ) self.assertTrue(out is not None ) def __lowercase ( self : Tuple ): '''simple docstring''' UpperCamelCase__ : int = 1 UpperCamelCase__ : Dict = self.get_dummy_custom_hf_index_retriever(from_disk=SCREAMING_SNAKE_CASE ) UpperCamelCase__ : Tuple = np.array( [np.ones(self.retrieval_vector_size ), -np.ones(self.retrieval_vector_size )] , dtype=np.floataa ) UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ : List[str] = retriever.retrieve(SCREAMING_SNAKE_CASE , n_docs=SCREAMING_SNAKE_CASE ) self.assertEqual(retrieved_doc_embeds.shape , (2, n_docs, self.retrieval_vector_size) ) self.assertEqual(len(SCREAMING_SNAKE_CASE ) , 2 ) self.assertEqual(sorted(doc_dicts[0] ) , ["embeddings", "id", "text", "title"] ) self.assertEqual(len(doc_dicts[0]["id"] ) , SCREAMING_SNAKE_CASE ) self.assertEqual(doc_dicts[0]["id"][0] , "1" ) # max inner product is reached with second doc self.assertEqual(doc_dicts[1]["id"][0] , "0" ) # max inner product is reached with first doc self.assertListEqual(doc_ids.tolist() , [[1], [0]] ) def __lowercase ( self : str ): '''simple docstring''' UpperCamelCase__ : Any = self.get_dummy_custom_hf_index_retriever(from_disk=SCREAMING_SNAKE_CASE ) with tempfile.TemporaryDirectory() as tmp_dirname: retriever.save_pretrained(SCREAMING_SNAKE_CASE ) UpperCamelCase__ : Tuple = RagRetriever.from_pretrained(SCREAMING_SNAKE_CASE ) self.assertIsInstance(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ) UpperCamelCase__ : Optional[int] = np.array( [np.ones(self.retrieval_vector_size ), -np.ones(self.retrieval_vector_size )] , dtype=np.floataa ) UpperCamelCase__ : Any = retriever.retrieve(SCREAMING_SNAKE_CASE , n_docs=1 ) self.assertTrue(out is not None ) def __lowercase ( self : List[str] ): '''simple docstring''' UpperCamelCase__ : int = 1 UpperCamelCase__ : List[str] = self.get_dummy_custom_hf_index_retriever(from_disk=SCREAMING_SNAKE_CASE ) UpperCamelCase__ : Dict = np.array( [np.ones(self.retrieval_vector_size ), -np.ones(self.retrieval_vector_size )] , dtype=np.floataa ) UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ : List[str] = retriever.retrieve(SCREAMING_SNAKE_CASE , n_docs=SCREAMING_SNAKE_CASE ) self.assertEqual(retrieved_doc_embeds.shape , (2, n_docs, self.retrieval_vector_size) ) self.assertEqual(len(SCREAMING_SNAKE_CASE ) , 2 ) self.assertEqual(sorted(doc_dicts[0] ) , ["embeddings", "id", "text", "title"] ) self.assertEqual(len(doc_dicts[0]["id"] ) , SCREAMING_SNAKE_CASE ) self.assertEqual(doc_dicts[0]["id"][0] , "1" ) # max inner product is reached with second doc self.assertEqual(doc_dicts[1]["id"][0] , "0" ) # max inner product is reached with first doc self.assertListEqual(doc_ids.tolist() , [[1], [0]] ) def __lowercase ( self : Union[str, Any] ): '''simple docstring''' UpperCamelCase__ : List[str] = self.get_dummy_custom_hf_index_retriever(from_disk=SCREAMING_SNAKE_CASE ) with tempfile.TemporaryDirectory() as tmp_dirname: retriever.save_pretrained(SCREAMING_SNAKE_CASE ) UpperCamelCase__ : Optional[int] = RagRetriever.from_pretrained(SCREAMING_SNAKE_CASE ) self.assertIsInstance(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ) UpperCamelCase__ : Dict = np.array( [np.ones(self.retrieval_vector_size ), -np.ones(self.retrieval_vector_size )] , dtype=np.floataa ) UpperCamelCase__ : Union[str, Any] = retriever.retrieve(SCREAMING_SNAKE_CASE , n_docs=1 ) self.assertTrue(out is not None ) def __lowercase ( self : Tuple ): '''simple docstring''' UpperCamelCase__ : List[str] = 1 UpperCamelCase__ : Any = self.get_dummy_legacy_index_retriever() UpperCamelCase__ : Optional[int] = np.array( [np.ones(self.retrieval_vector_size ), -np.ones(self.retrieval_vector_size )] , dtype=np.floataa ) UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ : Tuple = retriever.retrieve(SCREAMING_SNAKE_CASE , n_docs=SCREAMING_SNAKE_CASE ) self.assertEqual(retrieved_doc_embeds.shape , (2, n_docs, self.retrieval_vector_size) ) self.assertEqual(len(SCREAMING_SNAKE_CASE ) , 2 ) self.assertEqual(sorted(doc_dicts[0] ) , ["text", "title"] ) self.assertEqual(len(doc_dicts[0]["text"] ) , SCREAMING_SNAKE_CASE ) self.assertEqual(doc_dicts[0]["text"][0] , "bar" ) # max inner product is reached with second doc self.assertEqual(doc_dicts[1]["text"][0] , "foo" ) # max inner product is reached with first doc self.assertListEqual(doc_ids.tolist() , [[1], [0]] ) def __lowercase ( self : List[Any] ): '''simple docstring''' UpperCamelCase__ : Union[str, Any] = self.get_dummy_legacy_index_retriever() with tempfile.TemporaryDirectory() as tmp_dirname: retriever.save_pretrained(SCREAMING_SNAKE_CASE ) UpperCamelCase__ : str = RagRetriever.from_pretrained(SCREAMING_SNAKE_CASE ) self.assertIsInstance(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ) UpperCamelCase__ : int = np.array( [np.ones(self.retrieval_vector_size ), -np.ones(self.retrieval_vector_size )] , dtype=np.floataa ) UpperCamelCase__ : int = retriever.retrieve(SCREAMING_SNAKE_CASE , n_docs=1 ) self.assertTrue(out is not None ) @require_torch @require_tokenizers @require_sentencepiece def __lowercase ( self : int ): '''simple docstring''' import torch UpperCamelCase__ : Optional[Any] = 1 UpperCamelCase__ : Optional[int] = self.get_dummy_canonical_hf_index_retriever() UpperCamelCase__ : Optional[Any] = [[5, 7], [10, 11]] UpperCamelCase__ : List[str] = np.array( [np.ones(self.retrieval_vector_size ), -np.ones(self.retrieval_vector_size )] , dtype=np.floataa ) UpperCamelCase__ : int = retriever(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , prefix=retriever.config.generator.prefix , n_docs=SCREAMING_SNAKE_CASE ) UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ : List[str] = ( out["context_input_ids"], out["context_attention_mask"], out["retrieved_doc_embeds"], ) self.assertEqual(retrieved_doc_embeds.shape , (2, n_docs, self.retrieval_vector_size) ) self.assertIsInstance(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ) self.assertIsInstance(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ) self.assertIsInstance(SCREAMING_SNAKE_CASE , np.ndarray ) UpperCamelCase__ : List[Any] = retriever( SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , prefix=retriever.config.generator.prefix , n_docs=SCREAMING_SNAKE_CASE , return_tensors="pt" , ) UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ : List[Any] = ( # noqa: F841 out["context_input_ids"], out["context_attention_mask"], out["retrieved_doc_embeds"], out["doc_ids"], ) self.assertEqual(retrieved_doc_embeds.shape , (2, n_docs, self.retrieval_vector_size) ) self.assertIsInstance(SCREAMING_SNAKE_CASE , torch.Tensor ) self.assertIsInstance(SCREAMING_SNAKE_CASE , torch.Tensor ) self.assertIsInstance(SCREAMING_SNAKE_CASE , torch.Tensor ) @require_torch @require_tokenizers @require_sentencepiece def __lowercase ( self : Union[str, Any] ): '''simple docstring''' UpperCamelCase__ : Optional[int] = self.get_dpr_ctx_encoder_tokenizer() UpperCamelCase__ : Union[str, Any] = 1 UpperCamelCase__ : Tuple = self.get_dummy_custom_hf_index_retriever(from_disk=SCREAMING_SNAKE_CASE ) retriever.set_ctx_encoder_tokenizer(SCREAMING_SNAKE_CASE ) UpperCamelCase__ : Tuple = [[5, 7], [10, 11]] UpperCamelCase__ : List[str] = np.array( [np.ones(self.retrieval_vector_size ), -np.ones(self.retrieval_vector_size )] , dtype=np.floataa ) UpperCamelCase__ : Optional[int] = retriever(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , prefix=retriever.config.generator.prefix , n_docs=SCREAMING_SNAKE_CASE ) self.assertEqual( len(SCREAMING_SNAKE_CASE ) , 6 ) # check whether the retriever output consist of 6 attributes including tokenized docs self.assertEqual( all(k in out for k in ("tokenized_doc_ids", "tokenized_doc_attention_mask") ) , SCREAMING_SNAKE_CASE ) # check for doc token related keys in dictionary.
189
0
def __SCREAMING_SNAKE_CASE ( __UpperCamelCase : int , __UpperCamelCase : int ) -> bool: """simple docstring""" return numa ^ numa < 0 if __name__ == "__main__": import doctest doctest.testmod()
362
from __future__ import annotations import copy import inspect import unittest import numpy as np from transformers import is_tf_available, is_vision_available from transformers.models.auto import get_values from transformers.testing_utils import require_tf, slow from transformers.utils import cached_property from ...test_configuration_common import ConfigTester from ...test_modeling_tf_common import TFModelTesterMixin, floats_tensor, ids_tensor, random_attention_mask from ...test_pipeline_mixin import PipelineTesterMixin if is_tf_available(): import tensorflow as tf from transformers import ( TF_LAYOUTLMV3_PRETRAINED_MODEL_ARCHIVE_LIST, TF_MODEL_FOR_MULTIPLE_CHOICE_MAPPING, TF_MODEL_FOR_QUESTION_ANSWERING_MAPPING, TF_MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING, TF_MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING, LayoutLMvaConfig, TFLayoutLMvaForQuestionAnswering, TFLayoutLMvaForSequenceClassification, TFLayoutLMvaForTokenClassification, TFLayoutLMvaModel, ) if is_vision_available(): from PIL import Image from transformers import LayoutLMvaImageProcessor class __snake_case : def __init__( self : Any , _lowercase : Tuple , _lowercase : str=2 , _lowercase : List[Any]=3 , _lowercase : Optional[Any]=4 , _lowercase : Optional[Any]=2 , _lowercase : str=7 , _lowercase : Dict=True , _lowercase : List[str]=True , _lowercase : Union[str, Any]=True , _lowercase : Optional[int]=True , _lowercase : Dict=99 , _lowercase : Dict=36 , _lowercase : Tuple=2 , _lowercase : Optional[int]=4 , _lowercase : int=37 , _lowercase : Tuple="gelu" , _lowercase : Optional[Any]=0.1 , _lowercase : Tuple=0.1 , _lowercase : str=5_12 , _lowercase : Dict=16 , _lowercase : int=2 , _lowercase : int=0.02 , _lowercase : Any=6 , _lowercase : List[Any]=6 , _lowercase : List[Any]=3 , _lowercase : List[Any]=4 , _lowercase : int=None , _lowercase : Optional[int]=10_00 , ): """simple docstring""" SCREAMING_SNAKE_CASE__ = parent SCREAMING_SNAKE_CASE__ = batch_size SCREAMING_SNAKE_CASE__ = num_channels SCREAMING_SNAKE_CASE__ = image_size SCREAMING_SNAKE_CASE__ = patch_size SCREAMING_SNAKE_CASE__ = is_training SCREAMING_SNAKE_CASE__ = use_input_mask SCREAMING_SNAKE_CASE__ = use_token_type_ids SCREAMING_SNAKE_CASE__ = use_labels SCREAMING_SNAKE_CASE__ = vocab_size SCREAMING_SNAKE_CASE__ = hidden_size SCREAMING_SNAKE_CASE__ = num_hidden_layers SCREAMING_SNAKE_CASE__ = num_attention_heads SCREAMING_SNAKE_CASE__ = intermediate_size SCREAMING_SNAKE_CASE__ = hidden_act SCREAMING_SNAKE_CASE__ = hidden_dropout_prob SCREAMING_SNAKE_CASE__ = attention_probs_dropout_prob SCREAMING_SNAKE_CASE__ = max_position_embeddings SCREAMING_SNAKE_CASE__ = type_vocab_size SCREAMING_SNAKE_CASE__ = type_sequence_label_size SCREAMING_SNAKE_CASE__ = initializer_range SCREAMING_SNAKE_CASE__ = coordinate_size SCREAMING_SNAKE_CASE__ = shape_size SCREAMING_SNAKE_CASE__ = num_labels SCREAMING_SNAKE_CASE__ = num_choices SCREAMING_SNAKE_CASE__ = scope SCREAMING_SNAKE_CASE__ = range_bbox # LayoutLMv3's sequence length equals the number of text tokens + number of patches + 1 (we add 1 for the CLS token) SCREAMING_SNAKE_CASE__ = text_seq_length SCREAMING_SNAKE_CASE__ = (image_size // patch_size) ** 2 + 1 SCREAMING_SNAKE_CASE__ = self.text_seq_length + self.image_seq_length def __a ( self : List[str] ): """simple docstring""" SCREAMING_SNAKE_CASE__ = ids_tensor([self.batch_size, self.text_seq_length] , self.vocab_size ) SCREAMING_SNAKE_CASE__ = ids_tensor([self.batch_size, self.text_seq_length, 4] , self.range_bbox ) SCREAMING_SNAKE_CASE__ = bbox.numpy() # Ensure that bbox is legal for i in range(bbox.shape[0] ): for j in range(bbox.shape[1] ): if bbox[i, j, 3] < bbox[i, j, 1]: SCREAMING_SNAKE_CASE__ = bbox[i, j, 3] SCREAMING_SNAKE_CASE__ = bbox[i, j, 1] SCREAMING_SNAKE_CASE__ = tmp_coordinate if bbox[i, j, 2] < bbox[i, j, 0]: SCREAMING_SNAKE_CASE__ = bbox[i, j, 2] SCREAMING_SNAKE_CASE__ = bbox[i, j, 0] SCREAMING_SNAKE_CASE__ = tmp_coordinate SCREAMING_SNAKE_CASE__ = tf.constant(_lowercase ) SCREAMING_SNAKE_CASE__ = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) SCREAMING_SNAKE_CASE__ = None if self.use_input_mask: SCREAMING_SNAKE_CASE__ = random_attention_mask([self.batch_size, self.text_seq_length] ) SCREAMING_SNAKE_CASE__ = None if self.use_token_type_ids: SCREAMING_SNAKE_CASE__ = ids_tensor([self.batch_size, self.text_seq_length] , self.type_vocab_size ) SCREAMING_SNAKE_CASE__ = None SCREAMING_SNAKE_CASE__ = None if self.use_labels: SCREAMING_SNAKE_CASE__ = ids_tensor([self.batch_size] , self.type_sequence_label_size ) SCREAMING_SNAKE_CASE__ = ids_tensor([self.batch_size, self.text_seq_length] , self.num_labels ) SCREAMING_SNAKE_CASE__ = LayoutLMvaConfig( 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 , initializer_range=self.initializer_range , coordinate_size=self.coordinate_size , shape_size=self.shape_size , input_size=self.image_size , patch_size=self.patch_size , ) return config, input_ids, bbox, pixel_values, token_type_ids, input_mask, sequence_labels, token_labels def __a ( self : List[str] , _lowercase : Dict , _lowercase : List[Any] , _lowercase : str , _lowercase : Optional[int] , _lowercase : Union[str, Any] , _lowercase : Tuple ): """simple docstring""" SCREAMING_SNAKE_CASE__ = TFLayoutLMvaModel(config=_lowercase ) # text + image SCREAMING_SNAKE_CASE__ = model(_lowercase , pixel_values=_lowercase , training=_lowercase ) SCREAMING_SNAKE_CASE__ = model( _lowercase , bbox=_lowercase , pixel_values=_lowercase , attention_mask=_lowercase , token_type_ids=_lowercase , training=_lowercase , ) SCREAMING_SNAKE_CASE__ = model(_lowercase , bbox=_lowercase , pixel_values=_lowercase , training=_lowercase ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) # text only SCREAMING_SNAKE_CASE__ = model(_lowercase , training=_lowercase ) self.parent.assertEqual( result.last_hidden_state.shape , (self.batch_size, self.text_seq_length, self.hidden_size) ) # image only SCREAMING_SNAKE_CASE__ = model({"""pixel_values""": pixel_values} , training=_lowercase ) self.parent.assertEqual( result.last_hidden_state.shape , (self.batch_size, self.image_seq_length, self.hidden_size) ) def __a ( self : int , _lowercase : int , _lowercase : Optional[int] , _lowercase : Optional[Any] , _lowercase : Optional[int] , _lowercase : Tuple , _lowercase : List[Any] , _lowercase : Tuple ): """simple docstring""" SCREAMING_SNAKE_CASE__ = self.num_labels SCREAMING_SNAKE_CASE__ = TFLayoutLMvaForSequenceClassification(config=_lowercase ) SCREAMING_SNAKE_CASE__ = model( _lowercase , bbox=_lowercase , pixel_values=_lowercase , attention_mask=_lowercase , token_type_ids=_lowercase , labels=_lowercase , training=_lowercase , ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_labels) ) def __a ( self : Any , _lowercase : Dict , _lowercase : Tuple , _lowercase : int , _lowercase : Optional[int] , _lowercase : Optional[int] , _lowercase : Union[str, Any] , _lowercase : Tuple ): """simple docstring""" SCREAMING_SNAKE_CASE__ = self.num_labels SCREAMING_SNAKE_CASE__ = TFLayoutLMvaForTokenClassification(config=_lowercase ) SCREAMING_SNAKE_CASE__ = model( _lowercase , bbox=_lowercase , pixel_values=_lowercase , attention_mask=_lowercase , token_type_ids=_lowercase , labels=_lowercase , training=_lowercase , ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.text_seq_length, self.num_labels) ) def __a ( self : str , _lowercase : int , _lowercase : List[str] , _lowercase : str , _lowercase : str , _lowercase : List[str] , _lowercase : List[str] , _lowercase : Union[str, Any] ): """simple docstring""" SCREAMING_SNAKE_CASE__ = 2 SCREAMING_SNAKE_CASE__ = TFLayoutLMvaForQuestionAnswering(config=_lowercase ) SCREAMING_SNAKE_CASE__ = model( _lowercase , bbox=_lowercase , pixel_values=_lowercase , attention_mask=_lowercase , token_type_ids=_lowercase , start_positions=_lowercase , end_positions=_lowercase , training=_lowercase , ) 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 __a ( self : List[Any] ): """simple docstring""" SCREAMING_SNAKE_CASE__ = self.prepare_config_and_inputs() ((SCREAMING_SNAKE_CASE__) , (SCREAMING_SNAKE_CASE__) , (SCREAMING_SNAKE_CASE__) , (SCREAMING_SNAKE_CASE__) , (SCREAMING_SNAKE_CASE__) , (SCREAMING_SNAKE_CASE__) , (SCREAMING_SNAKE_CASE__) , (SCREAMING_SNAKE_CASE__)) = config_and_inputs SCREAMING_SNAKE_CASE__ = { """input_ids""": input_ids, """bbox""": bbox, """pixel_values""": pixel_values, """token_type_ids""": token_type_ids, """attention_mask""": input_mask, } return config, inputs_dict @require_tf class __snake_case ( lowerCamelCase_ , lowerCamelCase_ , unittest.TestCase ): lowerCAmelCase_ = ( ( TFLayoutLMvaModel, TFLayoutLMvaForQuestionAnswering, TFLayoutLMvaForSequenceClassification, TFLayoutLMvaForTokenClassification, ) if is_tf_available() else () ) lowerCAmelCase_ = ( {"document-question-answering": TFLayoutLMvaForQuestionAnswering, "feature-extraction": TFLayoutLMvaModel} if is_tf_available() else {} ) lowerCAmelCase_ = False lowerCAmelCase_ = False lowerCAmelCase_ = False def __a ( self : Union[str, Any] , _lowercase : Optional[int] , _lowercase : List[str] , _lowercase : Optional[Any] , _lowercase : Optional[int] , _lowercase : List[Any] ): """simple docstring""" return True def __a ( self : List[str] , _lowercase : List[Any] , _lowercase : str , _lowercase : str=False ): """simple docstring""" SCREAMING_SNAKE_CASE__ = copy.deepcopy(_lowercase ) if model_class in get_values(_lowercase ): SCREAMING_SNAKE_CASE__ = { k: tf.tile(tf.expand_dims(_lowercase , 1 ) , (1, self.model_tester.num_choices) + (1,) * (v.ndim - 1) ) if isinstance(_lowercase , tf.Tensor ) and v.ndim > 0 else v for k, v in inputs_dict.items() } if return_labels: if model_class in get_values(_lowercase ): SCREAMING_SNAKE_CASE__ = tf.ones(self.model_tester.batch_size , dtype=tf.intaa ) elif model_class in get_values(_lowercase ): SCREAMING_SNAKE_CASE__ = tf.zeros(self.model_tester.batch_size , dtype=tf.intaa ) SCREAMING_SNAKE_CASE__ = tf.zeros(self.model_tester.batch_size , dtype=tf.intaa ) elif model_class in get_values(_lowercase ): SCREAMING_SNAKE_CASE__ = tf.zeros(self.model_tester.batch_size , dtype=tf.intaa ) elif model_class in get_values(_lowercase ): SCREAMING_SNAKE_CASE__ = tf.zeros( (self.model_tester.batch_size, self.model_tester.text_seq_length) , dtype=tf.intaa ) return inputs_dict def __a ( self : Tuple ): """simple docstring""" SCREAMING_SNAKE_CASE__ = TFLayoutLMvaModelTester(self ) SCREAMING_SNAKE_CASE__ = ConfigTester(self , config_class=_lowercase , hidden_size=37 ) def __a ( self : Any ): """simple docstring""" self.config_tester.run_common_tests() def __a ( self : Dict ): """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(_lowercase ) if getattr(_lowercase , """hf_compute_loss""" , _lowercase ): # The number of elements in the loss should be the same as the number of elements in the label SCREAMING_SNAKE_CASE__ = self._prepare_for_class(inputs_dict.copy() , _lowercase , return_labels=_lowercase ) SCREAMING_SNAKE_CASE__ = prepared_for_class[ sorted(prepared_for_class.keys() - inputs_dict.keys() , reverse=_lowercase )[0] ] SCREAMING_SNAKE_CASE__ = added_label.shape.as_list()[:1] # Test that model correctly compute the loss with kwargs SCREAMING_SNAKE_CASE__ = self._prepare_for_class(inputs_dict.copy() , _lowercase , return_labels=_lowercase ) SCREAMING_SNAKE_CASE__ = prepared_for_class.pop("""input_ids""" ) SCREAMING_SNAKE_CASE__ = model(_lowercase , **_lowercase )[0] self.assertTrue(loss.shape.as_list() == expected_loss_size or loss.shape.as_list() == [1] ) # Test that model correctly compute the loss when we mask some positions SCREAMING_SNAKE_CASE__ = self._prepare_for_class(inputs_dict.copy() , _lowercase , return_labels=_lowercase ) SCREAMING_SNAKE_CASE__ = prepared_for_class.pop("""input_ids""" ) if "labels" in prepared_for_class: SCREAMING_SNAKE_CASE__ = prepared_for_class["""labels"""].numpy() if len(labels.shape ) > 1 and labels.shape[1] != 1: SCREAMING_SNAKE_CASE__ = -1_00 SCREAMING_SNAKE_CASE__ = tf.convert_to_tensor(_lowercase ) SCREAMING_SNAKE_CASE__ = model(_lowercase , **_lowercase )[0] self.assertTrue(loss.shape.as_list() == expected_loss_size or loss.shape.as_list() == [1] ) self.assertTrue(not np.any(np.isnan(loss.numpy() ) ) ) # Test that model correctly compute the loss with a dict SCREAMING_SNAKE_CASE__ = self._prepare_for_class(inputs_dict.copy() , _lowercase , return_labels=_lowercase ) SCREAMING_SNAKE_CASE__ = model(_lowercase )[0] self.assertTrue(loss.shape.as_list() == expected_loss_size or loss.shape.as_list() == [1] ) # Test that model correctly compute the loss with a tuple SCREAMING_SNAKE_CASE__ = self._prepare_for_class(inputs_dict.copy() , _lowercase , return_labels=_lowercase ) # Get keys that were added with the _prepare_for_class function SCREAMING_SNAKE_CASE__ = prepared_for_class.keys() - inputs_dict.keys() SCREAMING_SNAKE_CASE__ = inspect.signature(model.call ).parameters SCREAMING_SNAKE_CASE__ = list(signature.keys() ) # Create a dictionary holding the location of the tensors in the tuple SCREAMING_SNAKE_CASE__ = {0: """input_ids"""} for label_key in label_keys: SCREAMING_SNAKE_CASE__ = signature_names.index(_lowercase ) SCREAMING_SNAKE_CASE__ = label_key SCREAMING_SNAKE_CASE__ = sorted(tuple_index_mapping.items() ) # Initialize a list with their default values, update the values and convert to a tuple SCREAMING_SNAKE_CASE__ = [] for name in signature_names: if name != "kwargs": list_input.append(signature[name].default ) for index, value in sorted_tuple_index_mapping: SCREAMING_SNAKE_CASE__ = prepared_for_class[value] SCREAMING_SNAKE_CASE__ = tuple(_lowercase ) # Send to model SCREAMING_SNAKE_CASE__ = model(tuple_input[:-1] )[0] self.assertTrue(loss.shape.as_list() == expected_loss_size or loss.shape.as_list() == [1] ) def __a ( self : Optional[int] ): """simple docstring""" ( ( SCREAMING_SNAKE_CASE__ ) , ( SCREAMING_SNAKE_CASE__ ) , ( SCREAMING_SNAKE_CASE__ ) , ( SCREAMING_SNAKE_CASE__ ) , ( SCREAMING_SNAKE_CASE__ ) , ( SCREAMING_SNAKE_CASE__ ) , ( SCREAMING_SNAKE_CASE__ ) , ( SCREAMING_SNAKE_CASE__ ) , ) = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(_lowercase , _lowercase , _lowercase , _lowercase , _lowercase , _lowercase ) def __a ( self : int ): """simple docstring""" ( ( SCREAMING_SNAKE_CASE__ ) , ( SCREAMING_SNAKE_CASE__ ) , ( SCREAMING_SNAKE_CASE__ ) , ( SCREAMING_SNAKE_CASE__ ) , ( SCREAMING_SNAKE_CASE__ ) , ( SCREAMING_SNAKE_CASE__ ) , ( SCREAMING_SNAKE_CASE__ ) , ( SCREAMING_SNAKE_CASE__ ) , ) = self.model_tester.prepare_config_and_inputs() for type in ["absolute", "relative_key", "relative_key_query"]: SCREAMING_SNAKE_CASE__ = type self.model_tester.create_and_check_model(_lowercase , _lowercase , _lowercase , _lowercase , _lowercase , _lowercase ) def __a ( self : int ): """simple docstring""" ( ( SCREAMING_SNAKE_CASE__ ) , ( SCREAMING_SNAKE_CASE__ ) , ( SCREAMING_SNAKE_CASE__ ) , ( SCREAMING_SNAKE_CASE__ ) , ( SCREAMING_SNAKE_CASE__ ) , ( SCREAMING_SNAKE_CASE__ ) , ( SCREAMING_SNAKE_CASE__ ) , ( SCREAMING_SNAKE_CASE__ ) , ) = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_sequence_classification( _lowercase , _lowercase , _lowercase , _lowercase , _lowercase , _lowercase , _lowercase ) def __a ( self : str ): """simple docstring""" ( ( SCREAMING_SNAKE_CASE__ ) , ( SCREAMING_SNAKE_CASE__ ) , ( SCREAMING_SNAKE_CASE__ ) , ( SCREAMING_SNAKE_CASE__ ) , ( SCREAMING_SNAKE_CASE__ ) , ( SCREAMING_SNAKE_CASE__ ) , ( SCREAMING_SNAKE_CASE__ ) , ( SCREAMING_SNAKE_CASE__ ) , ) = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_token_classification( _lowercase , _lowercase , _lowercase , _lowercase , _lowercase , _lowercase , _lowercase ) def __a ( self : List[str] ): """simple docstring""" ( ( SCREAMING_SNAKE_CASE__ ) , ( SCREAMING_SNAKE_CASE__ ) , ( SCREAMING_SNAKE_CASE__ ) , ( SCREAMING_SNAKE_CASE__ ) , ( SCREAMING_SNAKE_CASE__ ) , ( SCREAMING_SNAKE_CASE__ ) , ( SCREAMING_SNAKE_CASE__ ) , ( SCREAMING_SNAKE_CASE__ ) , ) = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_question_answering( _lowercase , _lowercase , _lowercase , _lowercase , _lowercase , _lowercase , _lowercase ) @slow def __a ( self : Tuple ): """simple docstring""" for model_name in TF_LAYOUTLMV3_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: SCREAMING_SNAKE_CASE__ = TFLayoutLMvaModel.from_pretrained(_lowercase ) self.assertIsNotNone(_lowercase ) def __SCREAMING_SNAKE_CASE ( ) -> List[Any]: """simple docstring""" SCREAMING_SNAKE_CASE__ = Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png""" ) return image @require_tf class __snake_case ( unittest.TestCase ): @cached_property def __a ( self : Optional[int] ): """simple docstring""" return LayoutLMvaImageProcessor(apply_ocr=_lowercase ) if is_vision_available() else None @slow def __a ( self : Tuple ): """simple docstring""" SCREAMING_SNAKE_CASE__ = TFLayoutLMvaModel.from_pretrained("""microsoft/layoutlmv3-base""" ) SCREAMING_SNAKE_CASE__ = self.default_image_processor SCREAMING_SNAKE_CASE__ = prepare_img() SCREAMING_SNAKE_CASE__ = image_processor(images=_lowercase , return_tensors="""tf""" ).pixel_values SCREAMING_SNAKE_CASE__ = tf.constant([[1, 2]] ) SCREAMING_SNAKE_CASE__ = tf.expand_dims(tf.constant([[1, 2, 3, 4], [5, 6, 7, 8]] ) , axis=0 ) # forward pass SCREAMING_SNAKE_CASE__ = model(input_ids=_lowercase , bbox=_lowercase , pixel_values=_lowercase , training=_lowercase ) # verify the logits SCREAMING_SNAKE_CASE__ = (1, 1_99, 7_68) self.assertEqual(outputs.last_hidden_state.shape , _lowercase ) SCREAMING_SNAKE_CASE__ = tf.constant( [[-0.05_29, 0.36_18, 0.16_32], [-0.15_87, -0.16_67, -0.04_00], [-0.15_57, -0.16_71, -0.05_05]] ) self.assertTrue(np.allclose(outputs.last_hidden_state[0, :3, :3] , _lowercase , atol=1E-4 ) )
204
0
from collections import OrderedDict from typing import Mapping from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging a_ = logging.get_logger(__name__) a_ = { '''xlm-roberta-base''': '''https://huggingface.co/xlm-roberta-base/resolve/main/config.json''', '''xlm-roberta-large''': '''https://huggingface.co/xlm-roberta-large/resolve/main/config.json''', '''xlm-roberta-large-finetuned-conll02-dutch''': ( '''https://huggingface.co/xlm-roberta-large-finetuned-conll02-dutch/resolve/main/config.json''' ), '''xlm-roberta-large-finetuned-conll02-spanish''': ( '''https://huggingface.co/xlm-roberta-large-finetuned-conll02-spanish/resolve/main/config.json''' ), '''xlm-roberta-large-finetuned-conll03-english''': ( '''https://huggingface.co/xlm-roberta-large-finetuned-conll03-english/resolve/main/config.json''' ), '''xlm-roberta-large-finetuned-conll03-german''': ( '''https://huggingface.co/xlm-roberta-large-finetuned-conll03-german/resolve/main/config.json''' ), } class lowercase__ ( _UpperCAmelCase ): a_ ="""xlm-roberta""" def __init__( self , __UpperCAmelCase=30522 , __UpperCAmelCase=768 , __UpperCAmelCase=12 , __UpperCAmelCase=12 , __UpperCAmelCase=3072 , __UpperCAmelCase="gelu" , __UpperCAmelCase=0.1 , __UpperCAmelCase=0.1 , __UpperCAmelCase=512 , __UpperCAmelCase=2 , __UpperCAmelCase=0.02 , __UpperCAmelCase=1E-1_2 , __UpperCAmelCase=1 , __UpperCAmelCase=0 , __UpperCAmelCase=2 , __UpperCAmelCase="absolute" , __UpperCAmelCase=True , __UpperCAmelCase=None , **__UpperCAmelCase , )-> Dict: '''simple docstring''' super().__init__(pad_token_id=__UpperCAmelCase , bos_token_id=__UpperCAmelCase , eos_token_id=__UpperCAmelCase , **__UpperCAmelCase ) lowerCAmelCase__ = vocab_size lowerCAmelCase__ = hidden_size lowerCAmelCase__ = num_hidden_layers lowerCAmelCase__ = num_attention_heads lowerCAmelCase__ = hidden_act lowerCAmelCase__ = intermediate_size lowerCAmelCase__ = hidden_dropout_prob lowerCAmelCase__ = attention_probs_dropout_prob lowerCAmelCase__ = max_position_embeddings lowerCAmelCase__ = type_vocab_size lowerCAmelCase__ = initializer_range lowerCAmelCase__ = layer_norm_eps lowerCAmelCase__ = position_embedding_type lowerCAmelCase__ = use_cache lowerCAmelCase__ = classifier_dropout class lowercase__ ( _UpperCAmelCase ): @property def UpperCAmelCase ( self )-> Mapping[str, Mapping[int, str]]: '''simple docstring''' if self.task == "multiple-choice": lowerCAmelCase__ = {0: "batch", 1: "choice", 2: "sequence"} else: lowerCAmelCase__ = {0: "batch", 1: "sequence"} return OrderedDict( [ ("input_ids", dynamic_axis), ("attention_mask", dynamic_axis), ] )
340
from __future__ import annotations from collections.abc import Iterable, Iterator from dataclasses import dataclass a_ = (3, 9, -11, 0, 7, 5, 1, -1) a_ = (4, 6, 2, 0, 8, 10, 3, -2) @dataclass class lowercase__ : a_ =42 a_ =42 class lowercase__ : def __init__( self , __UpperCAmelCase )-> None: '''simple docstring''' lowerCAmelCase__ = None for i in sorted(__UpperCAmelCase , reverse=__UpperCAmelCase ): lowerCAmelCase__ = Node(__UpperCAmelCase , self.head ) def __iter__( self )-> Iterator[int]: '''simple docstring''' lowerCAmelCase__ = self.head while node: yield node.data lowerCAmelCase__ = node.next_node def __len__( self )-> int: '''simple docstring''' return sum(1 for _ in self ) def __str__( self )-> str: '''simple docstring''' return " -> ".join([str(__UpperCAmelCase ) for node in self] ) def _a ( UpperCamelCase_ : SortedLinkedList , UpperCamelCase_ : SortedLinkedList ) -> SortedLinkedList: """simple docstring""" return SortedLinkedList(list(UpperCamelCase_ ) + list(UpperCamelCase_ ) ) if __name__ == "__main__": import doctest doctest.testmod() a_ = SortedLinkedList print(merge_lists(SSL(test_data_odd), SSL(test_data_even)))
340
1
'''simple docstring''' from ...utils import deprecate from ..controlnet.pipeline_flax_controlnet import FlaxStableDiffusionControlNetPipeline # noqa: F401 deprecate( '''stable diffusion controlnet''', '''0.22.0''', '''Importing `FlaxStableDiffusionControlNetPipeline` from diffusers.pipelines.stable_diffusion.flax_pipeline_stable_diffusion_controlnet is deprecated. Please import `from diffusers import FlaxStableDiffusionControlNetPipeline` instead.''', standard_warn=False, stacklevel=3, )
355
'''simple docstring''' import numpy as np def _lowerCAmelCase ( lowerCamelCase_ : np.array ): return 1 / (1 + np.exp(-vector )) def _lowerCAmelCase ( lowerCamelCase_ : np.array ): return vector * sigmoid(1.7_02 * vector ) if __name__ == "__main__": import doctest doctest.testmod()
217
0
def lowercase_( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ): '''simple docstring''' lowerCamelCase : Any = """""" for word_or_phrase in separated: if not isinstance(lowerCAmelCase_ , lowerCAmelCase_ ): raise Exception("join() accepts only strings to be joined" ) joined += word_or_phrase + separator return joined.strip(lowerCAmelCase_ ) if __name__ == "__main__": from doctest import testmod testmod()
283
'''simple docstring''' import argparse import torch from transformers import RemBertConfig, RemBertModel, load_tf_weights_in_rembert from transformers.utils import logging logging.set_verbosity_info() def snake_case_ ( lowerCAmelCase_ , lowerCAmelCase_ , lowerCAmelCase_ )-> Union[str, Any]: '''simple docstring''' _UpperCAmelCase : int = RemBertConfig.from_json_file(lowerCAmelCase_ ) print("""Building PyTorch model from configuration: {}""".format(str(lowerCAmelCase_ ) ) ) _UpperCAmelCase : Any = RemBertModel(lowerCAmelCase_ ) # Load weights from tf checkpoint load_tf_weights_in_rembert(lowerCAmelCase_ , lowerCAmelCase_ , lowerCAmelCase_ ) # Save pytorch-model print("""Save PyTorch model to {}""".format(lowerCAmelCase_ ) ) torch.save(model.state_dict() , lowerCAmelCase_ ) if __name__ == "__main__": A_ : Optional[Any] = 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( """--rembert_config_file""", default=None, type=str, required=True, help=( """The config json file corresponding to the pre-trained RemBERT model. \n""" """This specifies the model architecture.""" ), ) parser.add_argument( """--pytorch_dump_path""", default=None, type=str, required=True, help="""Path to the output PyTorch model.""" ) A_ : Any = parser.parse_args() convert_rembert_tf_checkpoint_to_pytorch(args.tf_checkpoint_path, args.rembert_config_file, args.pytorch_dump_path)
215
0
"""simple docstring""" import gc import random import unittest import numpy as np import torch from transformers import CLIPTextConfig, CLIPTextModel, CLIPTokenizer from diffusers import AutoencoderKL, CycleDiffusionPipeline, DDIMScheduler, UNetaDConditionModel 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, skip_mps from ..pipeline_params import ( IMAGE_TO_IMAGE_IMAGE_PARAMS, TEXT_GUIDED_IMAGE_VARIATION_BATCH_PARAMS, TEXT_GUIDED_IMAGE_VARIATION_PARAMS, ) from ..test_pipelines_common import PipelineLatentTesterMixin, PipelineTesterMixin enable_full_determinism() class UpperCAmelCase_ ( _lowercase , _lowercase , unittest.TestCase): snake_case__ = CycleDiffusionPipeline snake_case__ = TEXT_GUIDED_IMAGE_VARIATION_PARAMS - { '''negative_prompt''', '''height''', '''width''', '''negative_prompt_embeds''', } snake_case__ = PipelineTesterMixin.required_optional_params - {'''latents'''} snake_case__ = TEXT_GUIDED_IMAGE_VARIATION_BATCH_PARAMS.union({'''source_prompt'''}) snake_case__ = IMAGE_TO_IMAGE_IMAGE_PARAMS snake_case__ = IMAGE_TO_IMAGE_IMAGE_PARAMS def _UpperCamelCase ( self : List[str] ) -> Union[str, Any]: torch.manual_seed(0 ) _UpperCamelCase = 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 , ) _UpperCamelCase = DDIMScheduler( beta_start=0.0_0_0_8_5 , beta_end=0.0_1_2 , beta_schedule='''scaled_linear''' , num_train_timesteps=1000 , clip_sample=__UpperCamelCase , set_alpha_to_one=__UpperCamelCase , ) torch.manual_seed(0 ) _UpperCamelCase = 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 , ) torch.manual_seed(0 ) _UpperCamelCase = CLIPTextConfig( bos_token_id=0 , eos_token_id=2 , hidden_size=32 , intermediate_size=37 , layer_norm_eps=1E-05 , num_attention_heads=4 , num_hidden_layers=5 , pad_token_id=1 , vocab_size=1000 , ) _UpperCamelCase = CLIPTextModel(__UpperCamelCase ) _UpperCamelCase = CLIPTokenizer.from_pretrained('''hf-internal-testing/tiny-random-clip''' ) _UpperCamelCase = { '''unet''': unet, '''scheduler''': scheduler, '''vae''': vae, '''text_encoder''': text_encoder, '''tokenizer''': tokenizer, '''safety_checker''': None, '''feature_extractor''': None, } return components def _UpperCamelCase ( self : Tuple , __UpperCamelCase : Union[str, Any] , __UpperCamelCase : int=0 ) -> str: _UpperCamelCase = floats_tensor((1, 3, 32, 32) , rng=random.Random(__UpperCamelCase ) ).to(__UpperCamelCase ) _UpperCamelCase = image / 2 + 0.5 if str(__UpperCamelCase ).startswith('''mps''' ): _UpperCamelCase = torch.manual_seed(__UpperCamelCase ) else: _UpperCamelCase = torch.Generator(device=__UpperCamelCase ).manual_seed(__UpperCamelCase ) _UpperCamelCase = { '''prompt''': '''An astronaut riding an elephant''', '''source_prompt''': '''An astronaut riding a horse''', '''image''': image, '''generator''': generator, '''num_inference_steps''': 2, '''eta''': 0.1, '''strength''': 0.8, '''guidance_scale''': 3, '''source_guidance_scale''': 1, '''output_type''': '''numpy''', } return inputs def _UpperCamelCase ( self : Tuple ) -> Any: _UpperCamelCase = '''cpu''' # ensure determinism for the device-dependent torch.Generator _UpperCamelCase = self.get_dummy_components() _UpperCamelCase = CycleDiffusionPipeline(**__UpperCamelCase ) _UpperCamelCase = pipe.to(__UpperCamelCase ) pipe.set_progress_bar_config(disable=__UpperCamelCase ) _UpperCamelCase = self.get_dummy_inputs(__UpperCamelCase ) _UpperCamelCase = pipe(**__UpperCamelCase ) _UpperCamelCase = output.images _UpperCamelCase = images[0, -3:, -3:, -1] assert images.shape == (1, 32, 32, 3) _UpperCamelCase = np.array([0.4_4_5_9, 0.4_9_4_3, 0.4_5_4_4, 0.6_6_4_3, 0.5_4_7_4, 0.4_3_2_7, 0.5_7_0_1, 0.5_9_5_9, 0.5_1_7_9] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-2 @unittest.skipIf(torch_device != '''cuda''' , '''This test requires a GPU''' ) def _UpperCamelCase ( self : List[Any] ) -> Union[str, Any]: _UpperCamelCase = self.get_dummy_components() for name, module in components.items(): if hasattr(__UpperCamelCase , '''half''' ): _UpperCamelCase = module.half() _UpperCamelCase = CycleDiffusionPipeline(**__UpperCamelCase ) _UpperCamelCase = pipe.to(__UpperCamelCase ) pipe.set_progress_bar_config(disable=__UpperCamelCase ) _UpperCamelCase = self.get_dummy_inputs(__UpperCamelCase ) _UpperCamelCase = pipe(**__UpperCamelCase ) _UpperCamelCase = output.images _UpperCamelCase = images[0, -3:, -3:, -1] assert images.shape == (1, 32, 32, 3) _UpperCamelCase = np.array([0.3_5_0_6, 0.4_5_4_3, 0.4_4_6, 0.4_5_7_5, 0.5_1_9_5, 0.4_1_5_5, 0.5_2_7_3, 0.5_1_8, 0.4_1_1_6] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-2 @skip_mps def _UpperCamelCase ( self : Any ) -> Dict: return super().test_save_load_local() @unittest.skip('''non-deterministic pipeline''' ) def _UpperCamelCase ( self : List[str] ) -> Optional[int]: return super().test_inference_batch_single_identical() @skip_mps def _UpperCamelCase ( self : str ) -> Union[str, Any]: return super().test_dict_tuple_outputs_equivalent() @skip_mps def _UpperCamelCase ( self : Tuple ) -> Optional[int]: return super().test_save_load_optional_components() @skip_mps def _UpperCamelCase ( self : Any ) -> Optional[int]: return super().test_attention_slicing_forward_pass() @slow @require_torch_gpu class UpperCAmelCase_ ( unittest.TestCase): def _UpperCamelCase ( self : Dict ) -> Any: # clean up the VRAM after each test super().tearDown() gc.collect() torch.cuda.empty_cache() def _UpperCamelCase ( self : Optional[int] ) -> str: _UpperCamelCase = load_image( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main''' '''/cycle-diffusion/black_colored_car.png''' ) _UpperCamelCase = load_numpy( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/cycle-diffusion/blue_colored_car_fp16.npy''' ) _UpperCamelCase = init_image.resize((512, 512) ) _UpperCamelCase = '''CompVis/stable-diffusion-v1-4''' _UpperCamelCase = DDIMScheduler.from_pretrained(__UpperCamelCase , subfolder='''scheduler''' ) _UpperCamelCase = CycleDiffusionPipeline.from_pretrained( __UpperCamelCase , scheduler=__UpperCamelCase , safety_checker=__UpperCamelCase , torch_dtype=torch.floataa , revision='''fp16''' ) pipe.to(__UpperCamelCase ) pipe.set_progress_bar_config(disable=__UpperCamelCase ) pipe.enable_attention_slicing() _UpperCamelCase = '''A black colored car''' _UpperCamelCase = '''A blue colored car''' _UpperCamelCase = torch.manual_seed(0 ) _UpperCamelCase = pipe( prompt=__UpperCamelCase , source_prompt=__UpperCamelCase , image=__UpperCamelCase , num_inference_steps=100 , eta=0.1 , strength=0.8_5 , guidance_scale=3 , source_guidance_scale=1 , generator=__UpperCamelCase , output_type='''np''' , ) _UpperCamelCase = output.images # the values aren't exactly equal, but the images look the same visually assert np.abs(image - expected_image ).max() < 5E-1 def _UpperCamelCase ( self : int ) -> Union[str, Any]: _UpperCamelCase = load_image( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main''' '''/cycle-diffusion/black_colored_car.png''' ) _UpperCamelCase = load_numpy( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/cycle-diffusion/blue_colored_car.npy''' ) _UpperCamelCase = init_image.resize((512, 512) ) _UpperCamelCase = '''CompVis/stable-diffusion-v1-4''' _UpperCamelCase = DDIMScheduler.from_pretrained(__UpperCamelCase , subfolder='''scheduler''' ) _UpperCamelCase = CycleDiffusionPipeline.from_pretrained(__UpperCamelCase , scheduler=__UpperCamelCase , safety_checker=__UpperCamelCase ) pipe.to(__UpperCamelCase ) pipe.set_progress_bar_config(disable=__UpperCamelCase ) pipe.enable_attention_slicing() _UpperCamelCase = '''A black colored car''' _UpperCamelCase = '''A blue colored car''' _UpperCamelCase = torch.manual_seed(0 ) _UpperCamelCase = pipe( prompt=__UpperCamelCase , source_prompt=__UpperCamelCase , image=__UpperCamelCase , num_inference_steps=100 , eta=0.1 , strength=0.8_5 , guidance_scale=3 , source_guidance_scale=1 , generator=__UpperCamelCase , output_type='''np''' , ) _UpperCamelCase = output.images assert np.abs(image - expected_image ).max() < 2E-2
54
"""simple docstring""" from __future__ import annotations UpperCAmelCase = 8.988E9 # units = N * m^s * C^-2 def lowercase ( a__ : float , a__ : float , a__ : float , a__ : float ) -> dict[str, float]: _UpperCamelCase = abs(chargea * chargea ) if (force, chargea, chargea, distance).count(0 ) != 1: raise ValueError('''One and only one argument must be 0''' ) if distance < 0: raise ValueError('''Distance cannot be negative''' ) if force == 0: _UpperCamelCase = COULOMBS_CONSTANT * charge_product / (distance**2) return {"force": force} elif chargea == 0: _UpperCamelCase = abs(a__ ) * (distance**2) / (COULOMBS_CONSTANT * chargea) return {"charge1": chargea} elif chargea == 0: _UpperCamelCase = abs(a__ ) * (distance**2) / (COULOMBS_CONSTANT * chargea) return {"charge2": chargea} elif distance == 0: _UpperCamelCase = (COULOMBS_CONSTANT * charge_product / abs(a__ )) ** 0.5 return {"distance": distance} raise ValueError('''Exactly one argument must be 0''' ) if __name__ == "__main__": import doctest doctest.testmod()
54
1
"""simple docstring""" import argparse import os import sys from unittest.mock import patch import pytorch_lightning as pl import timeout_decorator import torch from distillation import SummarizationDistiller, distill_main from finetune import SummarizationModule, main from transformers import MarianMTModel from transformers.file_utils import cached_path from transformers.testing_utils import TestCasePlus, require_torch_gpu, slow from utils import load_json __snake_case : Any = 'sshleifer/mar_enro_6_3_student' class A__ ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' def _SCREAMING_SNAKE_CASE ( self: Union[str, Any]) -> Any: """simple docstring""" super().setUp() __lowerCAmelCase : str = cached_path( "https://cdn-datasets.huggingface.co/translation/wmt_en_ro-tr40k-va0.5k-te0.5k.tar.gz" , extract_compressed_file=_SCREAMING_SNAKE_CASE , ) __lowerCAmelCase : str = F"""{data_cached}/wmt_en_ro-tr40k-va0.5k-te0.5k""" @slow @require_torch_gpu def _SCREAMING_SNAKE_CASE ( self: str) -> Optional[int]: """simple docstring""" MarianMTModel.from_pretrained(_SCREAMING_SNAKE_CASE) @slow @require_torch_gpu def _SCREAMING_SNAKE_CASE ( self: Dict) -> List[Any]: """simple docstring""" __lowerCAmelCase : Tuple = { "$MAX_LEN": 64, "$BS": 64, "$GAS": 1, "$ENRO_DIR": self.data_dir, "facebook/mbart-large-cc25": MARIAN_MODEL, # "val_check_interval=0.25": "val_check_interval=1.0", "--learning_rate=3e-5": "--learning_rate 3e-4", "--num_train_epochs 6": "--num_train_epochs 1", } # Clean up bash script __lowerCAmelCase : List[str] = (self.test_file_dir / "train_mbart_cc25_enro.sh").open().read().split("finetune.py")[1].strip() __lowerCAmelCase : List[Any] = bash_script.replace("\\\n" , "").strip().replace("\"$@\"" , "") for k, v in env_vars_to_replace.items(): __lowerCAmelCase : Optional[int] = bash_script.replace(_SCREAMING_SNAKE_CASE , str(_SCREAMING_SNAKE_CASE)) __lowerCAmelCase : int = self.get_auto_remove_tmp_dir() # bash_script = bash_script.replace("--fp16 ", "") __lowerCAmelCase : Optional[Any] = F""" --output_dir {output_dir} --tokenizer_name Helsinki-NLP/opus-mt-en-ro --sortish_sampler --do_predict --gpus 1 --freeze_encoder --n_train 40000 --n_val 500 --n_test 500 --fp16_opt_level O1 --num_sanity_val_steps 0 --eval_beams 2 """.split() # XXX: args.gpus > 1 : handle multi_gpu in the future __lowerCAmelCase : str = ["finetune.py"] + bash_script.split() + args with patch.object(_SCREAMING_SNAKE_CASE , "argv" , _SCREAMING_SNAKE_CASE): __lowerCAmelCase : int = argparse.ArgumentParser() __lowerCAmelCase : List[str] = pl.Trainer.add_argparse_args(_SCREAMING_SNAKE_CASE) __lowerCAmelCase : Dict = SummarizationModule.add_model_specific_args(_SCREAMING_SNAKE_CASE , os.getcwd()) __lowerCAmelCase : int = parser.parse_args() __lowerCAmelCase : Any = main(_SCREAMING_SNAKE_CASE) # Check metrics __lowerCAmelCase : Any = load_json(model.metrics_save_path) __lowerCAmelCase : Optional[Any] = metrics["val"][0] __lowerCAmelCase : str = 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}"""] , _SCREAMING_SNAKE_CASE) self.assertGreater(last_step_stats["val_avg_gen_time"] , 0.01) # model hanging on generate. Maybe bad config was saved. (XXX: old comment/assert?) self.assertLessEqual(last_step_stats["val_avg_gen_time"] , 1.0) # test learning requirements: # 1. BLEU improves over the course of training by more than 2 pts self.assertGreater(last_step_stats["val_avg_bleu"] - first_step_stats["val_avg_bleu"] , 2) # 2. BLEU finishes above 17 self.assertGreater(last_step_stats["val_avg_bleu"] , 17) # 3. test BLEU and val BLEU within ~1.1 pt. self.assertLess(abs(metrics["val"][-1]["val_avg_bleu"] - metrics["test"][-1]["test_avg_bleu"]) , 1.1) # check lightning ckpt can be loaded and has a reasonable statedict __lowerCAmelCase : str = os.listdir(_SCREAMING_SNAKE_CASE) __lowerCAmelCase : Union[str, Any] = [x for x in contents if x.endswith(".ckpt")][0] __lowerCAmelCase : List[Any] = os.path.join(args.output_dir , _SCREAMING_SNAKE_CASE) __lowerCAmelCase : Tuple = torch.load(_SCREAMING_SNAKE_CASE , map_location="cpu") __lowerCAmelCase : Dict = "model.model.decoder.layers.0.encoder_attn_layer_norm.weight" assert expected_key in ckpt["state_dict"] assert ckpt["state_dict"]["model.model.decoder.layers.0.encoder_attn_layer_norm.weight"].dtype == torch.floataa # TODO: turn on args.do_predict when PL bug fixed. if args.do_predict: __lowerCAmelCase : int = {os.path.basename(_SCREAMING_SNAKE_CASE) 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__ ( __SCREAMING_SNAKE_CASE ): '''simple docstring''' @timeout_decorator.timeout(600) @slow @require_torch_gpu def _SCREAMING_SNAKE_CASE ( self: Any) -> Tuple: """simple docstring""" __lowerCAmelCase : List[str] = F"""{self.test_file_dir_str}/test_data/wmt_en_ro""" __lowerCAmelCase : Any = { "--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 __lowerCAmelCase : List[Any] = ( (self.test_file_dir / "distil_marian_no_teacher.sh").open().read().split("distillation.py")[1].strip() ) __lowerCAmelCase : str = bash_script.replace("\\\n" , "").strip().replace("\"$@\"" , "") __lowerCAmelCase : Union[str, Any] = bash_script.replace("--fp16 " , " ") for k, v in env_vars_to_replace.items(): __lowerCAmelCase : Tuple = bash_script.replace(_SCREAMING_SNAKE_CASE , str(_SCREAMING_SNAKE_CASE)) __lowerCAmelCase : int = self.get_auto_remove_tmp_dir() __lowerCAmelCase : int = bash_script.replace("--fp16" , "") __lowerCAmelCase : List[Any] = 6 __lowerCAmelCase : int = ( ["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(_SCREAMING_SNAKE_CASE , "argv" , _SCREAMING_SNAKE_CASE): __lowerCAmelCase : Optional[Any] = argparse.ArgumentParser() __lowerCAmelCase : Optional[int] = pl.Trainer.add_argparse_args(_SCREAMING_SNAKE_CASE) __lowerCAmelCase : List[Any] = SummarizationDistiller.add_model_specific_args(_SCREAMING_SNAKE_CASE , os.getcwd()) __lowerCAmelCase : List[Any] = parser.parse_args() # assert args.gpus == gpus THIS BREAKS for multi_gpu __lowerCAmelCase : List[str] = distill_main(_SCREAMING_SNAKE_CASE) # Check metrics __lowerCAmelCase : Union[str, Any] = load_json(model.metrics_save_path) __lowerCAmelCase : Any = metrics["val"][0] __lowerCAmelCase : Union[str, Any] = 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}"""] , _SCREAMING_SNAKE_CASE) # check lightning ckpt can be loaded and has a reasonable statedict __lowerCAmelCase : Any = os.listdir(_SCREAMING_SNAKE_CASE) __lowerCAmelCase : Any = [x for x in contents if x.endswith(".ckpt")][0] __lowerCAmelCase : Optional[Any] = os.path.join(args.output_dir , _SCREAMING_SNAKE_CASE) __lowerCAmelCase : List[str] = torch.load(_SCREAMING_SNAKE_CASE , map_location="cpu") __lowerCAmelCase : Dict = "model.model.decoder.layers.0.encoder_attn_layer_norm.weight" assert expected_key in ckpt["state_dict"] assert ckpt["state_dict"]["model.model.decoder.layers.0.encoder_attn_layer_norm.weight"].dtype == torch.floataa # TODO: turn on args.do_predict when PL bug fixed. if args.do_predict: __lowerCAmelCase : Dict = {os.path.basename(_SCREAMING_SNAKE_CASE) 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
269
"""simple docstring""" import collections import tempfile import unittest import numpy as np from transformers.testing_utils import ( is_pt_flax_cross_test, require_flax, require_torch, require_vision, slow, torch_device, ) from transformers.utils import is_flax_available, is_torch_available, is_vision_available from ...test_modeling_flax_common import floats_tensor, ids_tensor, random_attention_mask from ..bert.test_modeling_flax_bert import FlaxBertModelTester from ..clip.test_modeling_flax_clip import FlaxCLIPVisionModelTester from ..vit.test_modeling_flax_vit import FlaxViTModelTester if is_flax_available(): from transformers import ( FlaxBertModel, FlaxCLIPVisionModel, FlaxVisionTextDualEncoderModel, FlaxViTModel, VisionTextDualEncoderConfig, VisionTextDualEncoderProcessor, ) from transformers.modeling_flax_pytorch_utils import ( convert_pytorch_state_dict_to_flax, load_flax_weights_in_pytorch_model, ) if is_torch_available(): import torch from transformers import VisionTextDualEncoderModel if is_vision_available(): from PIL import Image def _lowercase ( __snake_case ) -> List[str]: if isinstance(__snake_case ,collections.abc.Iterable ): return x return (x, x) @require_flax class A__ : '''simple docstring''' def _SCREAMING_SNAKE_CASE ( self: Optional[Any] , _SCREAMING_SNAKE_CASE: Dict , _SCREAMING_SNAKE_CASE: Optional[Any]) -> str: """simple docstring""" pass def _SCREAMING_SNAKE_CASE ( self: str) -> int: """simple docstring""" pass def _SCREAMING_SNAKE_CASE ( self: List[str]) -> Tuple: """simple docstring""" pass def _SCREAMING_SNAKE_CASE ( self: Any , _SCREAMING_SNAKE_CASE: np.ndarray , _SCREAMING_SNAKE_CASE: np.ndarray , _SCREAMING_SNAKE_CASE: float) -> List[Any]: """simple docstring""" __lowerCAmelCase : str = np.abs((a - b)).max() self.assertLessEqual(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , F"""Difference between torch and flax is {diff} (>= {tol}).""") def _SCREAMING_SNAKE_CASE ( self: str , _SCREAMING_SNAKE_CASE: List[str] , _SCREAMING_SNAKE_CASE: Dict , _SCREAMING_SNAKE_CASE: Any , _SCREAMING_SNAKE_CASE: List[Any] , _SCREAMING_SNAKE_CASE: Any=None , **_SCREAMING_SNAKE_CASE: Tuple) -> List[str]: """simple docstring""" __lowerCAmelCase : Tuple = VisionTextDualEncoderConfig.from_vision_text_configs(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE) __lowerCAmelCase : List[str] = FlaxVisionTextDualEncoderModel(_SCREAMING_SNAKE_CASE) __lowerCAmelCase : Optional[int] = model(input_ids=_SCREAMING_SNAKE_CASE , pixel_values=_SCREAMING_SNAKE_CASE , attention_mask=_SCREAMING_SNAKE_CASE) self.assertEqual(output["text_embeds"].shape , (input_ids.shape[0], config.projection_dim)) self.assertEqual(output["image_embeds"].shape , (pixel_values.shape[0], config.projection_dim)) def _SCREAMING_SNAKE_CASE ( self: List[Any] , _SCREAMING_SNAKE_CASE: str , _SCREAMING_SNAKE_CASE: Dict , _SCREAMING_SNAKE_CASE: Dict , _SCREAMING_SNAKE_CASE: List[Any] , _SCREAMING_SNAKE_CASE: Optional[Any]=None , **_SCREAMING_SNAKE_CASE: List[str]) -> Dict: """simple docstring""" __lowerCAmelCase , __lowerCAmelCase : List[Any] = self.get_vision_text_model(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE) __lowerCAmelCase : Union[str, Any] = {"vision_model": vision_model, "text_model": text_model} __lowerCAmelCase : Optional[Any] = FlaxVisionTextDualEncoderModel.from_vision_text_pretrained(**_SCREAMING_SNAKE_CASE) __lowerCAmelCase : Union[str, Any] = model(input_ids=_SCREAMING_SNAKE_CASE , pixel_values=_SCREAMING_SNAKE_CASE , attention_mask=_SCREAMING_SNAKE_CASE) self.assertEqual(output["text_embeds"].shape , (input_ids.shape[0], model.config.projection_dim)) self.assertEqual(output["image_embeds"].shape , (pixel_values.shape[0], model.config.projection_dim)) def _SCREAMING_SNAKE_CASE ( self: Dict , _SCREAMING_SNAKE_CASE: List[Any] , _SCREAMING_SNAKE_CASE: List[str] , _SCREAMING_SNAKE_CASE: List[Any] , _SCREAMING_SNAKE_CASE: Optional[int] , _SCREAMING_SNAKE_CASE: List[Any]=None , **_SCREAMING_SNAKE_CASE: Tuple) -> str: """simple docstring""" __lowerCAmelCase , __lowerCAmelCase : Optional[Any] = self.get_vision_text_model(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE) __lowerCAmelCase : Optional[Any] = {"vision_model": vision_model, "text_model": text_model} __lowerCAmelCase : Any = FlaxVisionTextDualEncoderModel.from_vision_text_pretrained(**_SCREAMING_SNAKE_CASE) __lowerCAmelCase : Optional[Any] = model(input_ids=_SCREAMING_SNAKE_CASE , pixel_values=_SCREAMING_SNAKE_CASE , attention_mask=_SCREAMING_SNAKE_CASE) __lowerCAmelCase : Any = output[0] with tempfile.TemporaryDirectory() as tmpdirname: model.save_pretrained(_SCREAMING_SNAKE_CASE) __lowerCAmelCase : Union[str, Any] = FlaxVisionTextDualEncoderModel.from_pretrained(_SCREAMING_SNAKE_CASE) __lowerCAmelCase : int = model(input_ids=_SCREAMING_SNAKE_CASE , pixel_values=_SCREAMING_SNAKE_CASE , attention_mask=_SCREAMING_SNAKE_CASE) __lowerCAmelCase : Union[str, Any] = after_output[0] __lowerCAmelCase : Any = np.amax(np.abs(out_a - out_a)) self.assertLessEqual(_SCREAMING_SNAKE_CASE , 1e-3) def _SCREAMING_SNAKE_CASE ( self: Tuple , _SCREAMING_SNAKE_CASE: Optional[int] , _SCREAMING_SNAKE_CASE: Optional[Any] , _SCREAMING_SNAKE_CASE: int , _SCREAMING_SNAKE_CASE: Optional[int] , _SCREAMING_SNAKE_CASE: Any=None , **_SCREAMING_SNAKE_CASE: List[str]) -> Optional[int]: """simple docstring""" __lowerCAmelCase , __lowerCAmelCase : Any = self.get_vision_text_model(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE) __lowerCAmelCase : Union[str, Any] = {"vision_model": vision_model, "text_model": text_model} __lowerCAmelCase : Optional[Any] = FlaxVisionTextDualEncoderModel.from_vision_text_pretrained(**_SCREAMING_SNAKE_CASE) __lowerCAmelCase : Optional[Any] = model( input_ids=_SCREAMING_SNAKE_CASE , pixel_values=_SCREAMING_SNAKE_CASE , attention_mask=_SCREAMING_SNAKE_CASE , output_attentions=_SCREAMING_SNAKE_CASE) __lowerCAmelCase : Optional[Any] = output.vision_model_output.attentions self.assertEqual(len(_SCREAMING_SNAKE_CASE) , vision_config.num_hidden_layers) # in ViT, the seq_len equals the number of patches + 1 (we add 1 for the [CLS] token) __lowerCAmelCase : List[str] = to_atuple(vision_model.config.image_size) __lowerCAmelCase : Any = to_atuple(vision_model.config.patch_size) __lowerCAmelCase : Union[str, Any] = (image_size[1] // patch_size[1]) * (image_size[0] // patch_size[0]) __lowerCAmelCase : Tuple = num_patches + 1 self.assertEqual(vision_attentions[0].shape[-3:] , (vision_config.num_attention_heads, seq_len, seq_len)) __lowerCAmelCase : Union[str, Any] = output.text_model_output.attentions self.assertEqual(len(_SCREAMING_SNAKE_CASE) , text_config.num_hidden_layers) self.assertEqual( text_attentions[0].shape[-3:] , (text_config.num_attention_heads, input_ids.shape[-1], input_ids.shape[-1]) , ) def _SCREAMING_SNAKE_CASE ( self: List[str] , _SCREAMING_SNAKE_CASE: Union[str, Any] , _SCREAMING_SNAKE_CASE: Optional[int] , _SCREAMING_SNAKE_CASE: int) -> str: """simple docstring""" pt_model.to(_SCREAMING_SNAKE_CASE) pt_model.eval() # prepare inputs __lowerCAmelCase : Union[str, Any] = inputs_dict __lowerCAmelCase : Union[str, Any] = {k: torch.tensor(v.tolist()) for k, v in flax_inputs.items()} with torch.no_grad(): __lowerCAmelCase : Any = pt_model(**_SCREAMING_SNAKE_CASE).to_tuple() __lowerCAmelCase : List[Any] = fx_model(**_SCREAMING_SNAKE_CASE).to_tuple() self.assertEqual(len(_SCREAMING_SNAKE_CASE) , len(_SCREAMING_SNAKE_CASE) , "Output lengths differ between Flax and PyTorch") for fx_output, pt_output in zip(fx_outputs[:4] , pt_outputs[:4]): self.assert_almost_equals(_SCREAMING_SNAKE_CASE , pt_output.numpy() , 4e-2) # PT -> Flax with tempfile.TemporaryDirectory() as tmpdirname: pt_model.save_pretrained(_SCREAMING_SNAKE_CASE) __lowerCAmelCase : str = FlaxVisionTextDualEncoderModel.from_pretrained(_SCREAMING_SNAKE_CASE , from_pt=_SCREAMING_SNAKE_CASE) __lowerCAmelCase : int = fx_model_loaded(**_SCREAMING_SNAKE_CASE).to_tuple() self.assertEqual(len(_SCREAMING_SNAKE_CASE) , len(_SCREAMING_SNAKE_CASE) , "Output lengths differ between Flax and PyTorch") for fx_output_loaded, pt_output in zip(fx_outputs_loaded[:4] , pt_outputs[:4]): self.assert_almost_equals(_SCREAMING_SNAKE_CASE , pt_output.numpy() , 4e-2) # Flax -> PT with tempfile.TemporaryDirectory() as tmpdirname: fx_model.save_pretrained(_SCREAMING_SNAKE_CASE) __lowerCAmelCase : Union[str, Any] = VisionTextDualEncoderModel.from_pretrained(_SCREAMING_SNAKE_CASE , from_flax=_SCREAMING_SNAKE_CASE) pt_model_loaded.to(_SCREAMING_SNAKE_CASE) pt_model_loaded.eval() with torch.no_grad(): __lowerCAmelCase : Optional[Any] = pt_model_loaded(**_SCREAMING_SNAKE_CASE).to_tuple() self.assertEqual(len(_SCREAMING_SNAKE_CASE) , len(_SCREAMING_SNAKE_CASE) , "Output lengths differ between Flax and PyTorch") for fx_output, pt_output_loaded in zip(fx_outputs[:4] , pt_outputs_loaded[:4]): self.assert_almost_equals(_SCREAMING_SNAKE_CASE , pt_output_loaded.numpy() , 4e-2) def _SCREAMING_SNAKE_CASE ( self: Union[str, Any] , _SCREAMING_SNAKE_CASE: Optional[Any] , _SCREAMING_SNAKE_CASE: Optional[int] , _SCREAMING_SNAKE_CASE: Dict) -> Optional[Any]: """simple docstring""" __lowerCAmelCase : Union[str, Any] = VisionTextDualEncoderConfig.from_vision_text_configs(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE) __lowerCAmelCase : Dict = VisionTextDualEncoderModel(_SCREAMING_SNAKE_CASE) __lowerCAmelCase : str = FlaxVisionTextDualEncoderModel(_SCREAMING_SNAKE_CASE) __lowerCAmelCase : str = convert_pytorch_state_dict_to_flax(pt_model.state_dict() , _SCREAMING_SNAKE_CASE) __lowerCAmelCase : Optional[Any] = fx_state self.check_pt_flax_equivalence(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE) def _SCREAMING_SNAKE_CASE ( self: List[Any] , _SCREAMING_SNAKE_CASE: Optional[int] , _SCREAMING_SNAKE_CASE: Tuple , _SCREAMING_SNAKE_CASE: Dict) -> str: """simple docstring""" __lowerCAmelCase : Optional[int] = VisionTextDualEncoderConfig.from_vision_text_configs(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE) __lowerCAmelCase : Dict = VisionTextDualEncoderModel(_SCREAMING_SNAKE_CASE) __lowerCAmelCase : Tuple = FlaxVisionTextDualEncoderModel(_SCREAMING_SNAKE_CASE) __lowerCAmelCase : int = load_flax_weights_in_pytorch_model(_SCREAMING_SNAKE_CASE , fx_model.params) self.check_pt_flax_equivalence(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE) def _SCREAMING_SNAKE_CASE ( self: List[Any]) -> str: """simple docstring""" __lowerCAmelCase : List[str] = self.prepare_config_and_inputs() self.check_model_from_pretrained_configs(**_SCREAMING_SNAKE_CASE) def _SCREAMING_SNAKE_CASE ( self: Dict) -> int: """simple docstring""" __lowerCAmelCase : List[Any] = self.prepare_config_and_inputs() self.check_vision_text_dual_encoder_from_pretrained(**_SCREAMING_SNAKE_CASE) def _SCREAMING_SNAKE_CASE ( self: List[Any]) -> List[Any]: """simple docstring""" __lowerCAmelCase : Dict = self.prepare_config_and_inputs() self.check_save_load(**_SCREAMING_SNAKE_CASE) def _SCREAMING_SNAKE_CASE ( self: int) -> Dict: """simple docstring""" __lowerCAmelCase : List[str] = self.prepare_config_and_inputs() self.check_vision_text_output_attention(**_SCREAMING_SNAKE_CASE) @is_pt_flax_cross_test def _SCREAMING_SNAKE_CASE ( self: Union[str, Any]) -> Any: """simple docstring""" __lowerCAmelCase : Dict = self.prepare_config_and_inputs() __lowerCAmelCase : List[Any] = config_inputs_dict.pop("vision_config") __lowerCAmelCase : str = config_inputs_dict.pop("text_config") __lowerCAmelCase : Union[str, Any] = config_inputs_dict self.check_equivalence_pt_to_flax(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE) self.check_equivalence_flax_to_pt(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE) @slow def _SCREAMING_SNAKE_CASE ( self: str) -> Dict: """simple docstring""" __lowerCAmelCase , __lowerCAmelCase : Dict = self.get_pretrained_model_and_inputs() __lowerCAmelCase : Union[str, Any] = model_a(**_SCREAMING_SNAKE_CASE) __lowerCAmelCase : List[str] = outputs[0] with tempfile.TemporaryDirectory() as tmp_dirname: model_a.save_pretrained(_SCREAMING_SNAKE_CASE) __lowerCAmelCase : List[Any] = FlaxVisionTextDualEncoderModel.from_pretrained(_SCREAMING_SNAKE_CASE) __lowerCAmelCase : Union[str, Any] = model_a(**_SCREAMING_SNAKE_CASE) __lowerCAmelCase : List[str] = after_outputs[0] __lowerCAmelCase : List[Any] = np.amax(np.abs(out_a - out_a)) self.assertLessEqual(_SCREAMING_SNAKE_CASE , 1e-5) @require_flax class A__ ( __SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' def _SCREAMING_SNAKE_CASE ( self: List[str]) -> List[Any]: """simple docstring""" __lowerCAmelCase : Any = FlaxVisionTextDualEncoderModel.from_vision_text_pretrained( "hf-internal-testing/tiny-random-vit" , "hf-internal-testing/tiny-bert" , vision_from_pt=_SCREAMING_SNAKE_CASE , text_from_pt=_SCREAMING_SNAKE_CASE , ) __lowerCAmelCase : Union[str, Any] = 13 __lowerCAmelCase : Optional[int] = floats_tensor( [ batch_size, model.config.vision_config.num_channels, model.config.vision_config.image_size, model.config.vision_config.image_size, ]) __lowerCAmelCase : List[Any] = ids_tensor([batch_size, 4] , model.config.text_config.vocab_size) __lowerCAmelCase : List[Any] = random_attention_mask([batch_size, 4]) __lowerCAmelCase : str = {"pixel_values": pixel_values, "input_ids": input_ids, "attention_mask": attention_mask} return model, inputs def _SCREAMING_SNAKE_CASE ( self: int , _SCREAMING_SNAKE_CASE: Union[str, Any] , _SCREAMING_SNAKE_CASE: Union[str, Any]) -> Optional[int]: """simple docstring""" __lowerCAmelCase : List[str] = FlaxViTModel(_SCREAMING_SNAKE_CASE) __lowerCAmelCase : Union[str, Any] = FlaxBertModel(_SCREAMING_SNAKE_CASE) return vision_model, text_model def _SCREAMING_SNAKE_CASE ( self: Optional[int]) -> int: """simple docstring""" __lowerCAmelCase : List[Any] = FlaxViTModelTester(self) __lowerCAmelCase : Optional[Any] = FlaxBertModelTester(self) __lowerCAmelCase : int = vit_model_tester.prepare_config_and_inputs() __lowerCAmelCase : List[str] = bert_model_tester.prepare_config_and_inputs() __lowerCAmelCase , __lowerCAmelCase : Tuple = vision_config_and_inputs __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase : Union[str, Any] = text_config_and_inputs # make sure that cross attention layers are added return { "text_config": text_config, "vision_config": vision_config, "pixel_values": pixel_values, "attention_mask": attention_mask, "input_ids": input_ids, "token_type_ids": token_type_ids, } @require_torch class A__ ( __SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' def _SCREAMING_SNAKE_CASE ( self: Dict) -> List[str]: """simple docstring""" __lowerCAmelCase : Optional[int] = FlaxVisionTextDualEncoderModel.from_vision_text_pretrained( "hf-internal-testing/tiny-random-clip" , "hf-internal-testing/tiny-bert" , vision_from_pt=_SCREAMING_SNAKE_CASE , text_from_pt=_SCREAMING_SNAKE_CASE , ) __lowerCAmelCase : Optional[int] = 13 __lowerCAmelCase : List[str] = floats_tensor( [ batch_size, model.config.vision_config.num_channels, model.config.vision_config.image_size, model.config.vision_config.image_size, ]) __lowerCAmelCase : Any = ids_tensor([batch_size, 4] , model.config.text_config.vocab_size) __lowerCAmelCase : str = random_attention_mask([batch_size, 4]) __lowerCAmelCase : Optional[int] = {"pixel_values": pixel_values, "input_ids": input_ids, "attention_mask": attention_mask} return model, inputs def _SCREAMING_SNAKE_CASE ( self: Optional[Any] , _SCREAMING_SNAKE_CASE: int , _SCREAMING_SNAKE_CASE: Union[str, Any]) -> int: """simple docstring""" __lowerCAmelCase : int = FlaxCLIPVisionModel(_SCREAMING_SNAKE_CASE) __lowerCAmelCase : List[str] = FlaxBertModel(_SCREAMING_SNAKE_CASE) return vision_model, text_model def _SCREAMING_SNAKE_CASE ( self: Union[str, Any]) -> Optional[Any]: """simple docstring""" __lowerCAmelCase : List[Any] = FlaxCLIPVisionModelTester(self) __lowerCAmelCase : str = FlaxBertModelTester(self) __lowerCAmelCase : Optional[Any] = clip_model_tester.prepare_config_and_inputs() __lowerCAmelCase : Dict = bert_model_tester.prepare_config_and_inputs() __lowerCAmelCase , __lowerCAmelCase : Any = vision_config_and_inputs __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase : List[Any] = text_config_and_inputs # make sure that cross attention layers are added return { "text_config": text_config, "vision_config": vision_config, "pixel_values": pixel_values, "attention_mask": attention_mask, "input_ids": input_ids, "token_type_ids": token_type_ids, } @require_flax @require_vision class A__ ( unittest.TestCase ): '''simple docstring''' @slow def _SCREAMING_SNAKE_CASE ( self: List[str]) -> List[str]: """simple docstring""" __lowerCAmelCase : Dict = FlaxVisionTextDualEncoderModel.from_pretrained("clip-italian/clip-italian" , logit_scale_init_value=1.0) __lowerCAmelCase : str = VisionTextDualEncoderProcessor.from_pretrained("clip-italian/clip-italian") __lowerCAmelCase : Tuple = Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png") __lowerCAmelCase : Optional[int] = processor( text=["una foto di un gatto", "una foto di un cane"] , images=_SCREAMING_SNAKE_CASE , padding=_SCREAMING_SNAKE_CASE , return_tensors="np") __lowerCAmelCase : List[str] = model(**_SCREAMING_SNAKE_CASE) # verify the logits self.assertEqual(outputs.logits_per_image.shape , (inputs.pixel_values.shape[0], inputs.input_ids.shape[0])) self.assertEqual( outputs.logits_per_text.shape , (inputs.input_ids.shape[0], inputs.pixel_values.shape[0]) , ) __lowerCAmelCase : List[str] = np.array([[1.228_4727, 0.310_4122]]) self.assertTrue(np.allclose(outputs.logits_per_image , _SCREAMING_SNAKE_CASE , atol=1e-3))
269
1
def SCREAMING_SNAKE_CASE ( snake_case_ : Union[str, Any] ): snake_case__ : str = len(__lowerCAmelCase ) while cur > 1: # Find the maximum number in arr snake_case__ : str = arr.index(max(arr[0:cur] ) ) # Reverse from 0 to mi snake_case__ : int = arr[mi::-1] + arr[mi + 1 : len(__lowerCAmelCase )] # Reverse whole list snake_case__ : Dict = arr[cur - 1 :: -1] + arr[cur : len(__lowerCAmelCase )] cur -= 1 return arr if __name__ == "__main__": __lowerCamelCase : Tuple = input("""Enter numbers separated by a comma:\n""").strip() __lowerCamelCase : Optional[int] = [int(item) for item in user_input.split(""",""")] print(pancake_sort(unsorted))
350
import gc import random import unittest import numpy as np import torch from PIL import Image from transformers import CLIPTextConfig, CLIPTextModel, CLIPTokenizer from diffusers import ( AutoencoderKL, DDIMScheduler, EulerAncestralDiscreteScheduler, LMSDiscreteScheduler, PNDMScheduler, StableDiffusionInstructPixaPixPipeline, UNetaDConditionModel, ) from diffusers.image_processor import VaeImageProcessor from diffusers.utils import floats_tensor, load_image, slow, torch_device from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu from ..pipeline_params import ( IMAGE_TO_IMAGE_IMAGE_PARAMS, TEXT_GUIDED_IMAGE_INPAINTING_BATCH_PARAMS, TEXT_GUIDED_IMAGE_VARIATION_PARAMS, ) from ..test_pipelines_common import PipelineKarrasSchedulerTesterMixin, PipelineLatentTesterMixin, PipelineTesterMixin enable_full_determinism() class SCREAMING_SNAKE_CASE__ ( UpperCamelCase_ , UpperCamelCase_ , UpperCamelCase_ , unittest.TestCase ): """simple docstring""" a_ = StableDiffusionInstructPixaPixPipeline a_ = TEXT_GUIDED_IMAGE_VARIATION_PARAMS - {"height", "width", "cross_attention_kwargs"} a_ = TEXT_GUIDED_IMAGE_INPAINTING_BATCH_PARAMS a_ = IMAGE_TO_IMAGE_IMAGE_PARAMS a_ = IMAGE_TO_IMAGE_IMAGE_PARAMS def _lowercase ( self : List[str] ): torch.manual_seed(0 ) snake_case__ : Any = UNetaDConditionModel( block_out_channels=(3_2, 6_4) , layers_per_block=2 , sample_size=3_2 , in_channels=8 , out_channels=4 , down_block_types=("DownBlock2D", "CrossAttnDownBlock2D") , up_block_types=("CrossAttnUpBlock2D", "UpBlock2D") , cross_attention_dim=3_2 , ) snake_case__ : int = PNDMScheduler(skip_prk_steps=__A ) torch.manual_seed(0 ) snake_case__ : Union[str, Any] = AutoencoderKL( block_out_channels=[3_2, 6_4] , in_channels=3 , out_channels=3 , down_block_types=["DownEncoderBlock2D", "DownEncoderBlock2D"] , up_block_types=["UpDecoderBlock2D", "UpDecoderBlock2D"] , latent_channels=4 , ) torch.manual_seed(0 ) snake_case__ : int = CLIPTextConfig( bos_token_id=0 , eos_token_id=2 , hidden_size=3_2 , intermediate_size=3_7 , layer_norm_eps=1e-0_5 , num_attention_heads=4 , num_hidden_layers=5 , pad_token_id=1 , vocab_size=1_0_0_0 , ) snake_case__ : Union[str, Any] = CLIPTextModel(__A ) snake_case__ : List[Any] = CLIPTokenizer.from_pretrained("hf-internal-testing/tiny-random-clip" ) snake_case__ : str = { "unet": unet, "scheduler": scheduler, "vae": vae, "text_encoder": text_encoder, "tokenizer": tokenizer, "safety_checker": None, "feature_extractor": None, } return components def _lowercase ( self : List[Any] , __A : int , __A : Any=0 ): snake_case__ : Optional[int] = floats_tensor((1, 3, 3_2, 3_2) , rng=random.Random(__A ) ).to(__A ) snake_case__ : int = image.cpu().permute(0 , 2 , 3 , 1 )[0] snake_case__ : Union[str, Any] = Image.fromarray(np.uinta(__A ) ).convert("RGB" ) if str(__A ).startswith("mps" ): snake_case__ : List[Any] = torch.manual_seed(__A ) else: snake_case__ : Optional[int] = torch.Generator(device=__A ).manual_seed(__A ) snake_case__ : Optional[int] = { "prompt": "A painting of a squirrel eating a burger", "image": image, "generator": generator, "num_inference_steps": 2, "guidance_scale": 6.0, "image_guidance_scale": 1, "output_type": "numpy", } return inputs def _lowercase ( self : int ): snake_case__ : Tuple = "cpu" # ensure determinism for the device-dependent torch.Generator snake_case__ : int = self.get_dummy_components() snake_case__ : List[Any] = StableDiffusionInstructPixaPixPipeline(**__A ) snake_case__ : List[Any] = sd_pipe.to(__A ) sd_pipe.set_progress_bar_config(disable=__A ) snake_case__ : Tuple = self.get_dummy_inputs(__A ) snake_case__ : List[str] = sd_pipe(**__A ).images snake_case__ : str = image[0, -3:, -3:, -1] assert image.shape == (1, 3_2, 3_2, 3) snake_case__ : List[Any] = np.array([0.7_5_2_6, 0.3_7_5_0, 0.4_5_4_7, 0.6_1_1_7, 0.5_8_6_6, 0.5_0_1_6, 0.4_3_2_7, 0.5_6_4_2, 0.4_8_1_5] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-3 def _lowercase ( self : Union[str, Any] ): snake_case__ : Dict = "cpu" # ensure determinism for the device-dependent torch.Generator snake_case__ : List[Any] = self.get_dummy_components() snake_case__ : List[Any] = StableDiffusionInstructPixaPixPipeline(**__A ) snake_case__ : str = sd_pipe.to(__A ) sd_pipe.set_progress_bar_config(disable=__A ) snake_case__ : str = self.get_dummy_inputs(__A ) snake_case__ : List[Any] = "french fries" snake_case__ : str = sd_pipe(**__A , negative_prompt=__A ) snake_case__ : Any = output.images snake_case__ : Any = image[0, -3:, -3:, -1] assert image.shape == (1, 3_2, 3_2, 3) snake_case__ : Union[str, Any] = np.array([0.7_5_1_1, 0.3_6_4_2, 0.4_5_5_3, 0.6_2_3_6, 0.5_7_9_7, 0.5_0_1_3, 0.4_3_4_3, 0.5_6_1_1, 0.4_8_3_1] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-3 def _lowercase ( self : Optional[int] ): snake_case__ : Tuple = "cpu" # ensure determinism for the device-dependent torch.Generator snake_case__ : List[Any] = self.get_dummy_components() snake_case__ : str = StableDiffusionInstructPixaPixPipeline(**__A ) snake_case__ : List[str] = sd_pipe.to(__A ) sd_pipe.set_progress_bar_config(disable=__A ) snake_case__ : Any = self.get_dummy_inputs(__A ) snake_case__ : Tuple = [inputs["prompt"]] * 2 snake_case__ : Any = np.array(inputs["image"] ).astype(np.floataa ) / 2_5_5.0 snake_case__ : List[str] = torch.from_numpy(__A ).unsqueeze(0 ).to(__A ) snake_case__ : Union[str, Any] = image / 2 + 0.5 snake_case__ : str = image.permute(0 , 3 , 1 , 2 ) snake_case__ : int = image.repeat(2 , 1 , 1 , 1 ) snake_case__ : str = sd_pipe(**__A ).images snake_case__ : Any = image[-1, -3:, -3:, -1] assert image.shape == (2, 3_2, 3_2, 3) snake_case__ : int = np.array([0.5_8_1_2, 0.5_7_4_8, 0.5_2_2_2, 0.5_9_0_8, 0.5_6_9_5, 0.7_1_7_4, 0.6_8_0_4, 0.5_5_2_3, 0.5_5_7_9] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-3 def _lowercase ( self : Union[str, Any] ): snake_case__ : Union[str, Any] = "cpu" # ensure determinism for the device-dependent torch.Generator snake_case__ : int = self.get_dummy_components() snake_case__ : Dict = EulerAncestralDiscreteScheduler( beta_start=0.0_0_0_8_5 , beta_end=0.0_1_2 , beta_schedule="scaled_linear" ) snake_case__ : Tuple = StableDiffusionInstructPixaPixPipeline(**__A ) snake_case__ : str = sd_pipe.to(__A ) sd_pipe.set_progress_bar_config(disable=__A ) snake_case__ : str = self.get_dummy_inputs(__A ) snake_case__ : Optional[Any] = sd_pipe(**__A ).images snake_case__ : Dict = image[0, -3:, -3:, -1] snake_case__ : Union[str, Any] = [round(__A , 4 ) for x in image_slice.flatten().tolist()] print(",".join([str(__A ) for x in slice] ) ) assert image.shape == (1, 3_2, 3_2, 3) snake_case__ : str = np.array([0.7_4_1_7, 0.3_8_4_2, 0.4_7_3_2, 0.5_7_7_6, 0.5_8_9_1, 0.5_1_3_9, 0.4_0_5_2, 0.5_6_7_3, 0.4_9_8_6] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-3 def _lowercase ( self : List[str] ): super().test_inference_batch_single_identical(expected_max_diff=3e-3 ) def _lowercase ( self : List[Any] ): snake_case__ : Tuple = self.get_dummy_components() snake_case__ : Tuple = StableDiffusionInstructPixaPixPipeline(**__A ) snake_case__ : int = VaeImageProcessor(do_resize=__A , do_normalize=__A ) snake_case__ : Any = pipe.to(__A ) pipe.set_progress_bar_config(disable=__A ) snake_case__ : Dict = pipe(**self.get_dummy_inputs_by_type(__A , input_image_type="pt" ) )[0] snake_case__ : int = components["vae"] snake_case__ : Union[str, Any] = self.get_dummy_inputs_by_type(__A , input_image_type="pt" ) for image_param in self.image_latents_params: if image_param in inputs.keys(): snake_case__ : Optional[int] = vae.encode(inputs[image_param] ).latent_dist.mode() snake_case__ : str = pipe(**__A )[0] snake_case__ : Dict = np.abs(out - out_latents_inputs ).max() self.assertLess(__A , 1e-4 , "passing latents as image input generate different result from passing image" ) @slow @require_torch_gpu class SCREAMING_SNAKE_CASE__ ( unittest.TestCase ): """simple docstring""" def _lowercase ( self : Optional[int] ): super().tearDown() gc.collect() torch.cuda.empty_cache() def _lowercase ( self : str , __A : Dict=0 ): snake_case__ : Optional[int] = torch.manual_seed(__A ) snake_case__ : Tuple = load_image( "https://huggingface.co/datasets/diffusers/test-arrays/resolve/main/stable_diffusion_pix2pix/example.jpg" ) snake_case__ : Optional[Any] = { "prompt": "turn him into a cyborg", "image": image, "generator": generator, "num_inference_steps": 3, "guidance_scale": 7.5, "image_guidance_scale": 1.0, "output_type": "numpy", } return inputs def _lowercase ( self : int ): snake_case__ : Union[str, Any] = StableDiffusionInstructPixaPixPipeline.from_pretrained( "timbrooks/instruct-pix2pix" , safety_checker=__A ) pipe.to(__A ) pipe.set_progress_bar_config(disable=__A ) pipe.enable_attention_slicing() snake_case__ : Union[str, Any] = self.get_inputs() snake_case__ : Union[str, Any] = pipe(**__A ).images snake_case__ : Dict = image[0, -3:, -3:, -1].flatten() assert image.shape == (1, 5_1_2, 5_1_2, 3) snake_case__ : Any = np.array([0.5_9_0_2, 0.6_0_1_5, 0.6_0_2_7, 0.5_9_8_3, 0.6_0_9_2, 0.6_0_6_1, 0.5_7_6_5, 0.5_7_8_5, 0.5_5_5_5] ) assert np.abs(expected_slice - image_slice ).max() < 1e-3 def _lowercase ( self : str ): snake_case__ : int = StableDiffusionInstructPixaPixPipeline.from_pretrained( "timbrooks/instruct-pix2pix" , safety_checker=__A ) snake_case__ : Dict = LMSDiscreteScheduler.from_config(pipe.scheduler.config ) pipe.to(__A ) pipe.set_progress_bar_config(disable=__A ) pipe.enable_attention_slicing() snake_case__ : List[str] = self.get_inputs() snake_case__ : Any = pipe(**__A ).images snake_case__ : Union[str, Any] = image[0, -3:, -3:, -1].flatten() assert image.shape == (1, 5_1_2, 5_1_2, 3) snake_case__ : Optional[Any] = np.array([0.6_5_7_8, 0.6_8_1_7, 0.6_9_7_2, 0.6_7_6_1, 0.6_8_5_6, 0.6_9_1_6, 0.6_4_2_8, 0.6_5_1_6, 0.6_3_0_1] ) assert np.abs(expected_slice - image_slice ).max() < 1e-3 def _lowercase ( self : Dict ): snake_case__ : List[str] = StableDiffusionInstructPixaPixPipeline.from_pretrained( "timbrooks/instruct-pix2pix" , safety_checker=__A ) snake_case__ : List[str] = DDIMScheduler.from_config(pipe.scheduler.config ) pipe.to(__A ) pipe.set_progress_bar_config(disable=__A ) pipe.enable_attention_slicing() snake_case__ : int = self.get_inputs() snake_case__ : Union[str, Any] = pipe(**__A ).images snake_case__ : Tuple = image[0, -3:, -3:, -1].flatten() assert image.shape == (1, 5_1_2, 5_1_2, 3) snake_case__ : Union[str, Any] = np.array([0.3_8_2_8, 0.3_8_3_4, 0.3_8_1_8, 0.3_7_9_2, 0.3_8_6_5, 0.3_7_5_2, 0.3_7_9_2, 0.3_8_4_7, 0.3_7_5_3] ) assert np.abs(expected_slice - image_slice ).max() < 1e-3 def _lowercase ( self : List[Any] ): snake_case__ : Optional[Any] = 0 def callback_fn(__A : int , __A : int , __A : torch.FloatTensor ) -> None: snake_case__ : Union[str, Any] = True nonlocal number_of_steps number_of_steps += 1 if step == 1: snake_case__ : Optional[Any] = latents.detach().cpu().numpy() assert latents.shape == (1, 4, 6_4, 6_4) snake_case__ : int = latents[0, -3:, -3:, -1] snake_case__ : Optional[int] = np.array([-0.2_4_6_3, -0.4_6_4_4, -0.9_7_5_6, 1.5_1_7_6, 1.4_4_1_4, 0.7_8_6_6, 0.9_8_9_7, 0.8_5_2_1, 0.7_9_8_3] ) assert np.abs(latents_slice.flatten() - expected_slice ).max() < 5e-2 elif step == 2: snake_case__ : int = latents.detach().cpu().numpy() assert latents.shape == (1, 4, 6_4, 6_4) snake_case__ : Any = latents[0, -3:, -3:, -1] snake_case__ : Dict = np.array([-0.2_6_4_4, -0.4_6_2_6, -0.9_6_5_3, 1.5_1_7_6, 1.4_5_5_1, 0.7_6_8_6, 0.9_8_0_5, 0.8_4_5_2, 0.8_1_1_5] ) assert np.abs(latents_slice.flatten() - expected_slice ).max() < 5e-2 snake_case__ : Any = False snake_case__ : Union[str, Any] = StableDiffusionInstructPixaPixPipeline.from_pretrained( "timbrooks/instruct-pix2pix" , safety_checker=__A , torch_dtype=torch.floataa ) snake_case__ : int = pipe.to(__A ) pipe.set_progress_bar_config(disable=__A ) pipe.enable_attention_slicing() snake_case__ : Optional[Any] = self.get_inputs() pipe(**__A , callback=__A , callback_steps=1 ) assert callback_fn.has_been_called assert number_of_steps == 3 def _lowercase ( self : List[Any] ): torch.cuda.empty_cache() torch.cuda.reset_max_memory_allocated() torch.cuda.reset_peak_memory_stats() snake_case__ : Dict = StableDiffusionInstructPixaPixPipeline.from_pretrained( "timbrooks/instruct-pix2pix" , safety_checker=__A , torch_dtype=torch.floataa ) snake_case__ : Tuple = pipe.to(__A ) pipe.set_progress_bar_config(disable=__A ) pipe.enable_attention_slicing(1 ) pipe.enable_sequential_cpu_offload() snake_case__ : Dict = self.get_inputs() snake_case__ : List[Any] = pipe(**__A ) snake_case__ : List[str] = torch.cuda.max_memory_allocated() # make sure that less than 2.2 GB is allocated assert mem_bytes < 2.2 * 1_0**9 def _lowercase ( self : Tuple ): snake_case__ : int = self.get_inputs() # resize to resolution that is divisible by 8 but not 16 or 32 snake_case__ : Union[str, Any] = inputs["image"].resize((5_0_4, 5_0_4) ) snake_case__ : Optional[Any] = "timbrooks/instruct-pix2pix" snake_case__ : Union[str, Any] = StableDiffusionInstructPixaPixPipeline.from_pretrained( __A , safety_checker=__A , ) pipe.to(__A ) pipe.set_progress_bar_config(disable=__A ) pipe.enable_attention_slicing() snake_case__ : Union[str, Any] = pipe(**__A ) snake_case__ : Tuple = output.images[0] snake_case__ : List[Any] = image[2_5_5:2_5_8, 3_8_3:3_8_6, -1] assert image.shape == (5_0_4, 5_0_4, 3) snake_case__ : int = np.array([0.2_7_2_6, 0.2_5_2_9, 0.2_6_6_4, 0.2_6_5_5, 0.2_6_4_1, 0.2_6_4_2, 0.2_5_9_1, 0.2_6_4_9, 0.2_5_9_0] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 5e-3
286
0
'''simple docstring''' import unittest import numpy as np from transformers.testing_utils import require_torch, require_vision from transformers.utils import is_torch_available, is_vision_available from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_video_inputs if is_torch_available(): import torch if is_vision_available(): from PIL import Image from transformers import VivitImageProcessor class SCREAMING_SNAKE_CASE (unittest.TestCase ): def __init__( self , _UpperCAmelCase , _UpperCAmelCase=7 , _UpperCAmelCase=3 , _UpperCAmelCase=10 , _UpperCAmelCase=18 , _UpperCAmelCase=30 , _UpperCAmelCase=400 , _UpperCAmelCase=True , _UpperCAmelCase=None , _UpperCAmelCase=True , _UpperCAmelCase=[0.5, 0.5, 0.5] , _UpperCAmelCase=[0.5, 0.5, 0.5] , _UpperCAmelCase=None , ): '''simple docstring''' __A : List[str] = size if size is not None else {'shortest_edge': 18} __A : int = crop_size if crop_size is not None else {'height': 18, 'width': 18} __A : List[str] = parent __A : str = batch_size __A : int = num_channels __A : List[str] = num_frames __A : Union[str, Any] = image_size __A : Optional[int] = min_resolution __A : str = max_resolution __A : Optional[int] = do_resize __A : Optional[int] = size __A : Dict = do_normalize __A : List[Any] = image_mean __A : int = image_std __A : Union[str, Any] = crop_size def SCREAMING_SNAKE_CASE ( self): '''simple docstring''' return { "image_mean": self.image_mean, "image_std": self.image_std, "do_normalize": self.do_normalize, "do_resize": self.do_resize, "size": self.size, "crop_size": self.crop_size, } @require_torch @require_vision class SCREAMING_SNAKE_CASE (a__ , unittest.TestCase ): lowerCAmelCase = VivitImageProcessor if is_vision_available() else None def SCREAMING_SNAKE_CASE ( self): '''simple docstring''' __A : List[str] = VivitImageProcessingTester(self) @property def SCREAMING_SNAKE_CASE ( self): '''simple docstring''' return self.image_processor_tester.prepare_image_processor_dict() def SCREAMING_SNAKE_CASE ( self): '''simple docstring''' __A : Optional[int] = self.image_processing_class(**self.image_processor_dict) self.assertTrue(hasattr(A_ , 'image_mean')) self.assertTrue(hasattr(A_ , 'image_std')) self.assertTrue(hasattr(A_ , 'do_normalize')) self.assertTrue(hasattr(A_ , 'do_resize')) self.assertTrue(hasattr(A_ , 'do_center_crop')) self.assertTrue(hasattr(A_ , 'size')) def SCREAMING_SNAKE_CASE ( self): '''simple docstring''' __A : int = self.image_processing_class.from_dict(self.image_processor_dict) self.assertEqual(image_processor.size , {'shortest_edge': 18}) self.assertEqual(image_processor.crop_size , {'height': 18, 'width': 18}) __A : Dict = 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 SCREAMING_SNAKE_CASE ( self): '''simple docstring''' __A : List[str] = self.image_processing_class(**self.image_processor_dict) # create random PIL videos __A : Optional[Any] = prepare_video_inputs(self.image_processor_tester , equal_resolution=A_) for video in video_inputs: self.assertIsInstance(A_ , A_) self.assertIsInstance(video[0] , Image.Image) # Test not batched input __A : Optional[Any] = image_processing(video_inputs[0] , return_tensors='pt').pixel_values self.assertEqual( encoded_videos.shape , ( 1, self.image_processor_tester.num_frames, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size['height'], self.image_processor_tester.crop_size['width'], ) , ) # Test batched __A : Dict = image_processing(A_ , return_tensors='pt').pixel_values self.assertEqual( encoded_videos.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_frames, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size['height'], self.image_processor_tester.crop_size['width'], ) , ) def SCREAMING_SNAKE_CASE ( self): '''simple docstring''' __A : Dict = self.image_processing_class(**self.image_processor_dict) # create random numpy tensors __A : Dict = prepare_video_inputs(self.image_processor_tester , equal_resolution=A_ , numpify=A_) for video in video_inputs: self.assertIsInstance(A_ , A_) self.assertIsInstance(video[0] , np.ndarray) # Test not batched input __A : List[str] = image_processing(video_inputs[0] , return_tensors='pt').pixel_values self.assertEqual( encoded_videos.shape , ( 1, self.image_processor_tester.num_frames, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size['height'], self.image_processor_tester.crop_size['width'], ) , ) # Test batched __A : int = image_processing(A_ , return_tensors='pt').pixel_values self.assertEqual( encoded_videos.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_frames, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size['height'], self.image_processor_tester.crop_size['width'], ) , ) def SCREAMING_SNAKE_CASE ( self): '''simple docstring''' __A : int = self.image_processing_class(**self.image_processor_dict) # create random PyTorch tensors __A : Optional[Any] = prepare_video_inputs(self.image_processor_tester , equal_resolution=A_ , torchify=A_) for video in video_inputs: self.assertIsInstance(A_ , A_) self.assertIsInstance(video[0] , torch.Tensor) # Test not batched input __A : int = image_processing(video_inputs[0] , return_tensors='pt').pixel_values self.assertEqual( encoded_videos.shape , ( 1, self.image_processor_tester.num_frames, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size['height'], self.image_processor_tester.crop_size['width'], ) , ) # Test batched __A : Dict = image_processing(A_ , return_tensors='pt').pixel_values self.assertEqual( encoded_videos.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_frames, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size['height'], self.image_processor_tester.crop_size['width'], ) , )
190
def _SCREAMING_SNAKE_CASE ( lowercase : int = 10_00 ): '''simple docstring''' lowerCamelCase_ = 2**power lowerCamelCase_ = 0 while n: lowerCamelCase_ , lowerCamelCase_ = r + n % 10, n // 10 return r if __name__ == "__main__": print(solution(int(str(input()).strip())))
204
0
'''simple docstring''' from collections import OrderedDict from typing import Mapping from packaging import version from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging from ...utils.backbone_utils import BackboneConfigMixin, get_aligned_output_features_output_indices __lowercase: Any = logging.get_logger(__name__) __lowercase: List[Any] = { "microsoft/resnet-50": "https://huggingface.co/microsoft/resnet-50/blob/main/config.json", } class UpperCAmelCase ( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__): _lowerCamelCase : List[str] = 'resnet' _lowerCamelCase : int = ['basic', 'bottleneck'] def __init__( self : Any, a_ : int=3, a_ : str=64, a_ : Optional[Any]=[256, 512, 1024, 2048], a_ : List[str]=[3, 4, 6, 3], a_ : Optional[int]="bottleneck", a_ : int="relu", a_ : Tuple=False, a_ : Dict=None, a_ : List[str]=None, **a_ : Optional[int], ): """simple docstring""" super().__init__(**a_ ) if layer_type not in self.layer_types: raise ValueError(f'layer_type={layer_type} is not one of {",".join(self.layer_types )}' ) UpperCamelCase__ = num_channels UpperCamelCase__ = embedding_size UpperCamelCase__ = hidden_sizes UpperCamelCase__ = depths UpperCamelCase__ = layer_type UpperCamelCase__ = hidden_act UpperCamelCase__ = downsample_in_first_stage UpperCamelCase__ = ["stem"] + [f'stage{idx}' for idx in range(1, len(a_ ) + 1 )] UpperCamelCase__ , UpperCamelCase__ = get_aligned_output_features_output_indices( out_features=a_, out_indices=a_, stage_names=self.stage_names ) class UpperCAmelCase ( SCREAMING_SNAKE_CASE__): _lowerCamelCase : List[str] = version.parse('1.11') @property def lowercase_ ( self : List[Any] ): """simple docstring""" return OrderedDict( [ ("pixel_values", {0: "batch", 1: "num_channels", 2: "height", 3: "width"}), ] ) @property def lowercase_ ( self : Any ): """simple docstring""" return 1e-3
31
'''simple docstring''' import itertools import random import unittest import numpy as np from transformers import is_speech_available from transformers.testing_utils import require_torch, require_torchaudio from ...test_sequence_feature_extraction_common import SequenceFeatureExtractionTestMixin if is_speech_available(): from transformers import SpeechaTextFeatureExtractor __lowercase: str = random.Random() def SCREAMING_SNAKE_CASE__( _UpperCamelCase : List[str] , _UpperCamelCase : Optional[int]=1.0 , _UpperCamelCase : Dict=None , _UpperCamelCase : List[str]=None ) -> Union[str, Any]: '''simple docstring''' if rng is None: UpperCamelCase__ = global_rng UpperCamelCase__ = [] for batch_idx in range(shape[0] ): values.append([] ) for _ in range(shape[1] ): values[-1].append(rng.random() * scale ) return values @require_torch @require_torchaudio class UpperCAmelCase ( unittest.TestCase): def __init__( self : List[Any], a_ : List[str], a_ : Any=7, a_ : Dict=400, a_ : str=2000, a_ : List[Any]=24, a_ : int=24, a_ : int=0.0, a_ : Union[str, Any]=1_6000, a_ : Union[str, Any]=True, a_ : Optional[Any]=True, ): """simple docstring""" UpperCamelCase__ = parent UpperCamelCase__ = batch_size UpperCamelCase__ = min_seq_length UpperCamelCase__ = max_seq_length UpperCamelCase__ = (self.max_seq_length - self.min_seq_length) // (self.batch_size - 1) UpperCamelCase__ = feature_size UpperCamelCase__ = num_mel_bins UpperCamelCase__ = padding_value UpperCamelCase__ = sampling_rate UpperCamelCase__ = return_attention_mask UpperCamelCase__ = do_normalize def lowercase_ ( self : Tuple ): """simple docstring""" return { "feature_size": self.feature_size, "num_mel_bins": self.num_mel_bins, "padding_value": self.padding_value, "sampling_rate": self.sampling_rate, "return_attention_mask": self.return_attention_mask, "do_normalize": self.do_normalize, } def lowercase_ ( self : Optional[Any], a_ : Union[str, Any]=False, a_ : Optional[int]=False ): """simple docstring""" def _flatten(a_ : Dict ): return list(itertools.chain(*a_ ) ) if equal_length: UpperCamelCase__ = [floats_list((self.max_seq_length, self.feature_size) ) for _ in range(self.batch_size )] else: # make sure that inputs increase in size UpperCamelCase__ = [ floats_list((x, self.feature_size) ) for x in range(self.min_seq_length, self.max_seq_length, self.seq_length_diff ) ] if numpify: UpperCamelCase__ = [np.asarray(a_ ) for x in speech_inputs] return speech_inputs @require_torch @require_torchaudio class UpperCAmelCase ( SCREAMING_SNAKE_CASE__ , unittest.TestCase): _lowerCamelCase : Dict = SpeechaTextFeatureExtractor if is_speech_available() else None def lowercase_ ( self : Any ): """simple docstring""" UpperCamelCase__ = SpeechaTextFeatureExtractionTester(self ) def lowercase_ ( self : Optional[int], a_ : Tuple ): """simple docstring""" self.assertTrue(np.all(np.mean(a_, axis=0 ) < 1e-3 ) ) self.assertTrue(np.all(np.abs(np.var(a_, axis=0 ) - 1 ) < 1e-3 ) ) def lowercase_ ( self : Any ): """simple docstring""" UpperCamelCase__ = self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() ) # create three inputs of length 800, 1000, and 1200 UpperCamelCase__ = [floats_list((1, x) )[0] for x in range(800, 1400, 200 )] UpperCamelCase__ = [np.asarray(a_ ) for speech_input in speech_inputs] # Test feature size UpperCamelCase__ = feature_extractor(a_, padding=a_, return_tensors="np" ).input_features self.assertTrue(input_features.ndim == 3 ) self.assertTrue(input_features.shape[-1] == feature_extractor.feature_size ) # Test not batched input UpperCamelCase__ = feature_extractor(speech_inputs[0], return_tensors="np" ).input_features UpperCamelCase__ = feature_extractor(np_speech_inputs[0], return_tensors="np" ).input_features self.assertTrue(np.allclose(a_, a_, atol=1e-3 ) ) # Test batched UpperCamelCase__ = feature_extractor(a_, return_tensors="np" ).input_features UpperCamelCase__ = feature_extractor(a_, return_tensors="np" ).input_features for enc_seq_a, enc_seq_a in zip(a_, a_ ): self.assertTrue(np.allclose(a_, a_, atol=1e-3 ) ) # Test 2-D numpy arrays are batched. UpperCamelCase__ = [floats_list((1, x) )[0] for x in (800, 800, 800)] UpperCamelCase__ = np.asarray(a_ ) UpperCamelCase__ = feature_extractor(a_, return_tensors="np" ).input_features UpperCamelCase__ = feature_extractor(a_, return_tensors="np" ).input_features for enc_seq_a, enc_seq_a in zip(a_, a_ ): self.assertTrue(np.allclose(a_, a_, atol=1e-3 ) ) def lowercase_ ( self : List[str] ): """simple docstring""" UpperCamelCase__ = self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() ) UpperCamelCase__ = [floats_list((1, x) )[0] for x in range(800, 1400, 200 )] UpperCamelCase__ = ["longest", "max_length", "do_not_pad"] UpperCamelCase__ = [None, 16, None] for max_length, padding in zip(a_, a_ ): UpperCamelCase__ = feature_extractor( a_, padding=a_, max_length=a_, return_attention_mask=a_ ) UpperCamelCase__ = inputs.input_features UpperCamelCase__ = inputs.attention_mask UpperCamelCase__ = [np.sum(a_ ) for x in attention_mask] self._check_zero_mean_unit_variance(input_features[0][: fbank_feat_lengths[0]] ) self._check_zero_mean_unit_variance(input_features[1][: fbank_feat_lengths[1]] ) self._check_zero_mean_unit_variance(input_features[2][: fbank_feat_lengths[2]] ) def lowercase_ ( self : Any ): """simple docstring""" UpperCamelCase__ = self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() ) UpperCamelCase__ = [floats_list((1, x) )[0] for x in range(800, 1400, 200 )] UpperCamelCase__ = ["longest", "max_length", "do_not_pad"] UpperCamelCase__ = [None, 16, None] for max_length, padding in zip(a_, a_ ): UpperCamelCase__ = feature_extractor( a_, max_length=a_, padding=a_, return_tensors="np", return_attention_mask=a_ ) UpperCamelCase__ = inputs.input_features UpperCamelCase__ = inputs.attention_mask UpperCamelCase__ = [np.sum(a_ ) for x in attention_mask] self._check_zero_mean_unit_variance(input_features[0][: fbank_feat_lengths[0]] ) self.assertTrue(input_features[0][fbank_feat_lengths[0] :].sum() < 1e-6 ) self._check_zero_mean_unit_variance(input_features[1][: fbank_feat_lengths[1]] ) self.assertTrue(input_features[0][fbank_feat_lengths[1] :].sum() < 1e-6 ) self._check_zero_mean_unit_variance(input_features[2][: fbank_feat_lengths[2]] ) def lowercase_ ( self : str ): """simple docstring""" UpperCamelCase__ = self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() ) UpperCamelCase__ = [floats_list((1, x) )[0] for x in range(800, 1400, 200 )] UpperCamelCase__ = feature_extractor( a_, padding="max_length", max_length=4, truncation=a_, return_tensors="np", return_attention_mask=a_, ) UpperCamelCase__ = inputs.input_features UpperCamelCase__ = inputs.attention_mask UpperCamelCase__ = np.sum(attention_mask == 1, axis=1 ) self._check_zero_mean_unit_variance(input_features[0, : fbank_feat_lengths[0]] ) self._check_zero_mean_unit_variance(input_features[1] ) self._check_zero_mean_unit_variance(input_features[2] ) def lowercase_ ( self : Any ): """simple docstring""" UpperCamelCase__ = self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() ) UpperCamelCase__ = [floats_list((1, x) )[0] for x in range(800, 1400, 200 )] UpperCamelCase__ = feature_extractor( a_, padding="longest", max_length=4, truncation=a_, return_tensors="np", return_attention_mask=a_, ) UpperCamelCase__ = inputs.input_features UpperCamelCase__ = inputs.attention_mask UpperCamelCase__ = np.sum(attention_mask == 1, axis=1 ) self._check_zero_mean_unit_variance(input_features[0, : fbank_feat_lengths[0]] ) self._check_zero_mean_unit_variance(input_features[1, : fbank_feat_lengths[1]] ) self._check_zero_mean_unit_variance(input_features[2] ) # make sure that if max_length < longest -> then pad to max_length self.assertEqual(input_features.shape, (3, 4, 24) ) UpperCamelCase__ = [floats_list((1, x) )[0] for x in range(800, 1400, 200 )] UpperCamelCase__ = feature_extractor( a_, padding="longest", max_length=16, truncation=a_, return_tensors="np", return_attention_mask=a_, ) UpperCamelCase__ = inputs.input_features UpperCamelCase__ = inputs.attention_mask UpperCamelCase__ = np.sum(attention_mask == 1, axis=1 ) self._check_zero_mean_unit_variance(input_features[0, : fbank_feat_lengths[0]] ) self._check_zero_mean_unit_variance(input_features[1, : fbank_feat_lengths[1]] ) self._check_zero_mean_unit_variance(input_features[2] ) # make sure that if max_length < longest -> then pad to max_length self.assertEqual(input_features.shape, (3, 6, 24) ) def lowercase_ ( self : Optional[Any] ): """simple docstring""" import torch UpperCamelCase__ = self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() ) UpperCamelCase__ = np.random.rand(100, 32 ).astype(np.floataa ) UpperCamelCase__ = np_speech_inputs.tolist() for inputs in [py_speech_inputs, np_speech_inputs]: UpperCamelCase__ = feature_extractor.pad([{"input_features": inputs}], return_tensors="np" ) self.assertTrue(np_processed.input_features.dtype == np.floataa ) UpperCamelCase__ = feature_extractor.pad([{"input_features": inputs}], return_tensors="pt" ) self.assertTrue(pt_processed.input_features.dtype == torch.floataa ) def lowercase_ ( self : List[str], a_ : int ): """simple docstring""" from datasets import load_dataset UpperCamelCase__ = load_dataset("hf-internal-testing/librispeech_asr_dummy", "clean", split="validation" ) # automatic decoding with librispeech UpperCamelCase__ = ds.sort("id" ).select(range(a_ ) )[:num_samples]["audio"] return [x["array"] for x in speech_samples] def lowercase_ ( self : int ): """simple docstring""" UpperCamelCase__ = np.array([ -1.5_745, -1.7_713, -1.7_020, -1.6_069, -1.2_250, -1.1_105, -0.9_072, -0.8_241, -1.2_310, -0.8_098, -0.3_320, -0.4_101, -0.7_985, -0.4_996, -0.8_213, -0.9_128, -1.0_420, -1.1_286, -1.0_440, -0.7_999, -0.8_405, -1.2_275, -1.5_443, -1.4_625, ] ) # fmt: on UpperCamelCase__ = self._load_datasamples(1 ) UpperCamelCase__ = self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() ) UpperCamelCase__ = feature_extractor(a_, return_tensors="pt" ).input_features self.assertEquals(input_features.shape, (1, 584, 24) ) self.assertTrue(np.allclose(input_features[0, 0, :30], a_, atol=1e-4 ) )
31
1
import os from shutil import copyfile from typing import List, Optional, Tuple from ...tokenization_utils import AddedToken from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import is_sentencepiece_available, logging if is_sentencepiece_available(): from .tokenization_rembert import RemBertTokenizer else: lowerCamelCase : List[Any] = None lowerCamelCase : List[str] = logging.get_logger(__name__) lowerCamelCase : List[Any] = {'vocab_file': 'sentencepiece.model', 'tokenizer_file': 'tokenizer.json'} lowerCamelCase : List[Any] = { 'vocab_file': { 'google/rembert': 'https://huggingface.co/google/rembert/resolve/main/sentencepiece.model', }, 'tokenizer_file': { 'google/rembert': 'https://huggingface.co/google/rembert/resolve/main/tokenizer.json', }, } lowerCamelCase : Optional[int] = { 'google/rembert': 2_5_6, } lowerCamelCase : Optional[Any] = '▁' class __lowercase (__snake_case ): """simple docstring""" _snake_case = VOCAB_FILES_NAMES _snake_case = PRETRAINED_VOCAB_FILES_MAP _snake_case = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES _snake_case = RemBertTokenizer def __init__( self , A=None , A=None , A=True , A=True , A=False , A="[CLS]" , A="[SEP]" , A="<unk>" , A="[SEP]" , A="<pad>" , A="[CLS]" , A="[MASK]" , **A , ) -> List[Any]: snake_case : int = AddedToken(UpperCamelCase__ , lstrip=UpperCamelCase__ , rstrip=UpperCamelCase__ ) if isinstance(UpperCamelCase__ , UpperCamelCase__ ) else mask_token super().__init__( UpperCamelCase__ , tokenizer_file=UpperCamelCase__ , do_lower_case=UpperCamelCase__ , remove_space=UpperCamelCase__ , keep_accents=UpperCamelCase__ , bos_token=UpperCamelCase__ , eos_token=UpperCamelCase__ , unk_token=UpperCamelCase__ , sep_token=UpperCamelCase__ , pad_token=UpperCamelCase__ , cls_token=UpperCamelCase__ , mask_token=UpperCamelCase__ , **UpperCamelCase__ , ) snake_case : Optional[int] = do_lower_case snake_case : int = remove_space snake_case : int = keep_accents snake_case : str = vocab_file snake_case : Tuple = False if not self.vocab_file else True def UpperCAmelCase ( self , A , A = None ) -> List[int]: snake_case : Optional[int] = [self.sep_token_id] snake_case : Any = [self.cls_token_id] if token_ids_a is None: return cls + token_ids_a + sep return cls + token_ids_a + sep + token_ids_a + sep def UpperCAmelCase ( self , A , A = None , A = False ) -> List[int]: if already_has_special_tokens: if token_ids_a is not None: raise ValueError( """You should not supply a second sequence if the provided sequence of """ """ids is already formatted with special tokens for the model.""" ) return [1 if x in [self.sep_token_id, self.cls_token_id] else 0 for x in token_ids_a] if token_ids_a is not None: return [1] + ([0] * len(UpperCamelCase__ )) + [1] + ([0] * len(UpperCamelCase__ )) + [1] return [1] + ([0] * len(UpperCamelCase__ )) + [1] def UpperCAmelCase ( self , A , A = None ) -> List[int]: snake_case : Optional[int] = [self.sep_token_id] 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 ) * [0] + len(token_ids_a + sep ) * [1] def UpperCAmelCase ( self , A , A = None ) -> Tuple[str]: if not os.path.isdir(UpperCamelCase__ ): logger.error("""Vocabulary path ({}) should be a directory""".format(UpperCamelCase__ ) ) return snake_case : Optional[Any] = os.path.join( UpperCamelCase__ , (filename_prefix + """-""" if filename_prefix else """""") + VOCAB_FILES_NAMES["""vocab_file"""] ) if os.path.abspath(self.vocab_file ) != os.path.abspath(UpperCamelCase__ ): copyfile(self.vocab_file , UpperCamelCase__ ) return (out_vocab_file,)
124
"""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 __A = logging.get_logger(__name__) __A = { "vocab_file": "vocab.json", "merges_file": "merges.txt", "tokenizer_config_file": "tokenizer_config.json", } __A = { "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" }, } __A = {"facebook/blenderbot-3B": 128} class snake_case ( __snake_case ): SCREAMING_SNAKE_CASE_ : Dict = VOCAB_FILES_NAMES SCREAMING_SNAKE_CASE_ : str = 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_ : List[Any] = BlenderbotTokenizer def __init__( self : Any , UpperCamelCase__ : str=None , UpperCamelCase__ : Tuple=None , UpperCamelCase__ : List[Any]=None , UpperCamelCase__ : Optional[Any]="replace" , UpperCamelCase__ : int="<s>" , UpperCamelCase__ : str="</s>" , UpperCamelCase__ : List[Any]="</s>" , UpperCamelCase__ : Union[str, Any]="<s>" , UpperCamelCase__ : List[str]="<unk>" , UpperCamelCase__ : Dict="<pad>" , UpperCamelCase__ : List[Any]="<mask>" , UpperCamelCase__ : Any=False , UpperCamelCase__ : int=True , **UpperCamelCase__ : Union[str, Any] , )-> int: '''simple docstring''' super().__init__( UpperCamelCase__ , UpperCamelCase__ , tokenizer_file=UpperCamelCase__ , errors=UpperCamelCase__ , bos_token=UpperCamelCase__ , eos_token=UpperCamelCase__ , sep_token=UpperCamelCase__ , cls_token=UpperCamelCase__ , unk_token=UpperCamelCase__ , pad_token=UpperCamelCase__ , mask_token=UpperCamelCase__ , add_prefix_space=UpperCamelCase__ , trim_offsets=UpperCamelCase__ , **UpperCamelCase__ , ) __lowerCAmelCase: Any = json.loads(self.backend_tokenizer.pre_tokenizer.__getstate__()) if pre_tok_state.get("add_prefix_space" , UpperCamelCase__) != add_prefix_space: __lowerCAmelCase: Dict = getattr(UpperCamelCase__ , pre_tok_state.pop("type")) __lowerCAmelCase: int = add_prefix_space __lowerCAmelCase: str = pre_tok_class(**UpperCamelCase__) __lowerCAmelCase: List[Any] = add_prefix_space __lowerCAmelCase: Union[str, Any] = "post_processor" __lowerCAmelCase: Tuple = getattr(self.backend_tokenizer , UpperCamelCase__ , UpperCamelCase__) if tokenizer_component_instance: __lowerCAmelCase: List[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: __lowerCAmelCase: str = tuple(state["sep"]) if "cls" in state: __lowerCAmelCase: Any = tuple(state["cls"]) __lowerCAmelCase: int = False if state.get("add_prefix_space" , UpperCamelCase__) != add_prefix_space: __lowerCAmelCase: Dict = add_prefix_space __lowerCAmelCase: Optional[Any] = True if state.get("trim_offsets" , UpperCamelCase__) != trim_offsets: __lowerCAmelCase: Dict = trim_offsets __lowerCAmelCase: Tuple = True if changes_to_apply: __lowerCAmelCase: Dict = getattr(UpperCamelCase__ , state.pop("type")) __lowerCAmelCase: str = component_class(**UpperCamelCase__) setattr(self.backend_tokenizer , UpperCamelCase__ , UpperCamelCase__) @property # Copied from transformers.models.roberta.tokenization_roberta_fast.RobertaTokenizerFast.mask_token with Roberta->Blenderbot, RoBERTa->Blenderbot def lowercase_ ( self : Optional[Any])-> 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 lowercase_ ( self : str , UpperCamelCase__ : Union[str, Any])-> Tuple: '''simple docstring''' __lowerCAmelCase: int = AddedToken(UpperCamelCase__ , lstrip=UpperCamelCase__ , rstrip=UpperCamelCase__) if isinstance(UpperCamelCase__ , UpperCamelCase__) else value __lowerCAmelCase: Any = value def lowercase_ ( self : Union[str, Any] , *UpperCamelCase__ : List[str] , **UpperCamelCase__ : Optional[int])-> BatchEncoding: '''simple docstring''' __lowerCAmelCase: List[str] = kwargs.get("is_split_into_words" , UpperCamelCase__) 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(*UpperCamelCase__ , **UpperCamelCase__) def lowercase_ ( self : Any , *UpperCamelCase__ : Optional[int] , **UpperCamelCase__ : int)-> BatchEncoding: '''simple docstring''' __lowerCAmelCase: List[Any] = kwargs.get("is_split_into_words" , UpperCamelCase__) 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(*UpperCamelCase__ , **UpperCamelCase__) def lowercase_ ( self : List[str] , UpperCamelCase__ : str , UpperCamelCase__ : Optional[str] = None)-> Tuple[str]: '''simple docstring''' __lowerCAmelCase: Optional[int] = self._tokenizer.model.save(UpperCamelCase__ , name=UpperCamelCase__) return tuple(UpperCamelCase__) def lowercase_ ( self : str , UpperCamelCase__ : List[int] , UpperCamelCase__ : Optional[List[int]] = None)-> List[int]: '''simple docstring''' __lowerCAmelCase: Tuple = [self.sep_token_id] __lowerCAmelCase: Any = [self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep) * [0] return len(cls + token_ids_a + sep + sep + token_ids_a + sep) * [0] def lowercase_ ( self : Union[str, Any] , UpperCamelCase__ : List[int] , UpperCamelCase__ : Optional[List[int]] = None)-> List[Any]: '''simple docstring''' return token_ids_a + [self.eos_token_id] def lowercase_ ( self : Dict , UpperCamelCase__ : "Conversation")-> List[int]: '''simple docstring''' __lowerCAmelCase: str = [] 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(UpperCamelCase__) __lowerCAmelCase: Optional[int] = " ".join(UpperCamelCase__) __lowerCAmelCase: Tuple = self.encode(UpperCamelCase__) if len(UpperCamelCase__) > self.model_max_length: __lowerCAmelCase: int = 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
217
0
from manim import * class A ( _a ): def __lowerCAmelCase ( self : Dict ) -> Dict: """simple docstring""" _a = Rectangle(height=0.5 , width=0.5 ) _a = Rectangle(height=0.2_5 , width=0.2_5 ) _a = Rectangle(height=0.4_6 , width=0.4_6 ).set_stroke(width=0 ) _a = [mem.copy() for i in range(6 )] _a = [mem.copy() for i in range(6 )] _a = VGroup(*lowerCAmelCase__ ).arrange(lowerCAmelCase__ , buff=0 ) _a = VGroup(*lowerCAmelCase__ ).arrange(lowerCAmelCase__ , buff=0 ) _a = VGroup(lowerCAmelCase__ , lowerCAmelCase__ ).arrange(lowerCAmelCase__ , buff=0 ) _a = Text('''CPU''' , font_size=24 ) _a = Group(lowerCAmelCase__ , lowerCAmelCase__ ).arrange(lowerCAmelCase__ , buff=0.5 , aligned_edge=lowerCAmelCase__ ) cpu.move_to([-2.5, -0.5, 0] ) self.add(lowerCAmelCase__ ) _a = [mem.copy() for i in range(4 )] _a = VGroup(*lowerCAmelCase__ ).arrange(lowerCAmelCase__ , buff=0 ) _a = Text('''GPU''' , font_size=24 ) _a = Group(lowerCAmelCase__ , lowerCAmelCase__ ).arrange(lowerCAmelCase__ , buff=0.5 , aligned_edge=lowerCAmelCase__ ) gpu.move_to([-1, -1, 0] ) self.add(lowerCAmelCase__ ) _a = [mem.copy() for i in range(6 )] _a = VGroup(*lowerCAmelCase__ ).arrange(lowerCAmelCase__ , buff=0 ) _a = Text('''Model''' , font_size=24 ) _a = Group(lowerCAmelCase__ , lowerCAmelCase__ ).arrange(lowerCAmelCase__ , buff=0.5 , aligned_edge=lowerCAmelCase__ ) model.move_to([3, -1.0, 0] ) self.add(lowerCAmelCase__ ) _a = [] _a = [] _a = [] for i, rect in enumerate(lowerCAmelCase__ ): rect.set_stroke(lowerCAmelCase__ ) _a = Rectangle(height=0.4_6 / 4 , width=0.4_6 / 3 ).set_stroke(width=0.0 ).set_fill(lowerCAmelCase__ , opacity=0.7 ) if i == 0: cpu_target.next_to(cpu_left_col_base[0].get_corner(DOWN + LEFT ) , buff=0.0_2 , direction=lowerCAmelCase__ ) cpu_target.set_x(cpu_target.get_x() + 0.1 ) elif i == 3: cpu_target.next_to(model_cpu_arr[0] , direction=lowerCAmelCase__ , buff=0.0 ) else: cpu_target.next_to(model_cpu_arr[i - 1] , direction=lowerCAmelCase__ , buff=0.0 ) self.add(lowerCAmelCase__ ) model_cpu_arr.append(lowerCAmelCase__ ) self.add(*lowerCAmelCase__ , *lowerCAmelCase__ , *lowerCAmelCase__ ) _a = [mem.copy() for i in range(6 )] _a = VGroup(*lowerCAmelCase__ ).arrange(lowerCAmelCase__ , buff=0 ) _a = Text('''Loaded Checkpoint''' , font_size=24 ) _a = Group(lowerCAmelCase__ , lowerCAmelCase__ ).arrange(lowerCAmelCase__ , buff=0.5 , aligned_edge=lowerCAmelCase__ ) checkpoint.move_to([3, 0.5, 0] ) self.add(lowerCAmelCase__ ) _a = [] _a = [] for i, rect in enumerate(lowerCAmelCase__ ): _a = fill.copy().set_fill(lowerCAmelCase__ , opacity=0.7 ) target.move_to(lowerCAmelCase__ ) ckpt_arr.append(lowerCAmelCase__ ) _a = 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(lowerCAmelCase__ ) self.add(*lowerCAmelCase__ , *lowerCAmelCase__ ) _a = Square(side_length=2.2 ) key.move_to([-5, 2, 0] ) _a = 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(lowerCAmelCase__ , lowerCAmelCase__ ) _a = MarkupText( F'<span fgcolor=\'{BLUE}\'>●</span> Checkpoint' , font_size=18 , ) blue_text.next_to(lowerCAmelCase__ , DOWN * 2.4 , aligned_edge=key_text.get_left() ) self.add(lowerCAmelCase__ ) _a = 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] ) _a = [meta_mem.copy() for i in range(6 )] _a = [meta_mem.copy() for i in range(6 )] _a = VGroup(*lowerCAmelCase__ ).arrange(lowerCAmelCase__ , buff=0 ) _a = VGroup(*lowerCAmelCase__ ).arrange(lowerCAmelCase__ , buff=0 ) _a = VGroup(lowerCAmelCase__ , lowerCAmelCase__ ).arrange(lowerCAmelCase__ , buff=0 ) _a = Text('''Disk''' , font_size=24 ) _a = Group(lowerCAmelCase__ , lowerCAmelCase__ ).arrange(lowerCAmelCase__ , buff=0.5 , aligned_edge=lowerCAmelCase__ ) disk.move_to([-4.0, -1.2_5, 0] ) self.play(Write(lowerCAmelCase__ , run_time=3 ) , Write(lowerCAmelCase__ , run_time=1 ) , Create(lowerCAmelCase__ , run_time=1 ) ) _a = [] for i, rect in enumerate(lowerCAmelCase__ ): _a = rect.copy() target.generate_target() target.target.move_to(disk_left_col_base[i] ).scale(0.5 ) animations.append(MoveToTarget(lowerCAmelCase__ , run_time=1.5 ) ) self.play(*lowerCAmelCase__ ) self.play(FadeOut(lowerCAmelCase__ ) ) _a = 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(lowerCAmelCase__ , run_time=3 ) ) self.play( FadeOut(lowerCAmelCase__ , lowerCAmelCase__ , *lowerCAmelCase__ , *lowerCAmelCase__ ) , ) self.wait()
367
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_sentencepiece_available, is_tf_available, is_tokenizers_available, is_torch_available, ) _snake_case : Dict = { 'configuration_albert': ['ALBERT_PRETRAINED_CONFIG_ARCHIVE_MAP', 'AlbertConfig', 'AlbertOnnxConfig'], } try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _snake_case : Optional[int] = ['AlbertTokenizer'] try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _snake_case : str = ['AlbertTokenizerFast'] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _snake_case : str = [ 'ALBERT_PRETRAINED_MODEL_ARCHIVE_LIST', 'AlbertForMaskedLM', 'AlbertForMultipleChoice', 'AlbertForPreTraining', 'AlbertForQuestionAnswering', 'AlbertForSequenceClassification', 'AlbertForTokenClassification', 'AlbertModel', 'AlbertPreTrainedModel', 'load_tf_weights_in_albert', ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _snake_case : Any = [ 'TF_ALBERT_PRETRAINED_MODEL_ARCHIVE_LIST', 'TFAlbertForMaskedLM', 'TFAlbertForMultipleChoice', 'TFAlbertForPreTraining', 'TFAlbertForQuestionAnswering', 'TFAlbertForSequenceClassification', 'TFAlbertForTokenClassification', 'TFAlbertMainLayer', 'TFAlbertModel', 'TFAlbertPreTrainedModel', ] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _snake_case : Any = [ 'FlaxAlbertForMaskedLM', 'FlaxAlbertForMultipleChoice', 'FlaxAlbertForPreTraining', 'FlaxAlbertForQuestionAnswering', 'FlaxAlbertForSequenceClassification', 'FlaxAlbertForTokenClassification', 'FlaxAlbertModel', 'FlaxAlbertPreTrainedModel', ] if TYPE_CHECKING: from .configuration_albert import ALBERT_PRETRAINED_CONFIG_ARCHIVE_MAP, AlbertConfig, AlbertOnnxConfig try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_albert import AlbertTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_albert_fast import AlbertTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_albert import ( ALBERT_PRETRAINED_MODEL_ARCHIVE_LIST, AlbertForMaskedLM, AlbertForMultipleChoice, AlbertForPreTraining, AlbertForQuestionAnswering, AlbertForSequenceClassification, AlbertForTokenClassification, AlbertModel, AlbertPreTrainedModel, load_tf_weights_in_albert, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_albert import ( TF_ALBERT_PRETRAINED_MODEL_ARCHIVE_LIST, TFAlbertForMaskedLM, TFAlbertForMultipleChoice, TFAlbertForPreTraining, TFAlbertForQuestionAnswering, TFAlbertForSequenceClassification, TFAlbertForTokenClassification, TFAlbertMainLayer, TFAlbertModel, TFAlbertPreTrainedModel, ) try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_flax_albert import ( FlaxAlbertForMaskedLM, FlaxAlbertForMultipleChoice, FlaxAlbertForPreTraining, FlaxAlbertForQuestionAnswering, FlaxAlbertForSequenceClassification, FlaxAlbertForTokenClassification, FlaxAlbertModel, FlaxAlbertPreTrainedModel, ) else: import sys _snake_case : Union[str, Any] = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
179
0
"""simple docstring""" from __future__ import annotations def UpperCAmelCase__ (lowerCAmelCase_ ): '''simple docstring''' __SCREAMING_SNAKE_CASE = str(lowerCAmelCase_ ) return n == n[::-1] def UpperCAmelCase__ (lowerCAmelCase_ = 100_0000 ): '''simple docstring''' __SCREAMING_SNAKE_CASE = 0 for i in range(1 , lowerCAmelCase_ ): if is_palindrome(lowerCAmelCase_ ) and is_palindrome(bin(lowerCAmelCase_ ).split("b" )[1] ): total += i return total if __name__ == "__main__": print(solution(int(str(input().strip()))))
54
"""simple docstring""" import unittest from transformers import PegasusConfig, PegasusTokenizer, is_flax_available from transformers.testing_utils import require_flax, slow from ...test_configuration_common import ConfigTester from ...test_modeling_flax_common import FlaxModelTesterMixin, ids_tensor if is_flax_available(): import os # The slow tests are often failing with OOM error on GPU # This makes JAX allocate exactly what is needed on demand, and deallocate memory that is no longer needed # but will be slower as stated here https://jax.readthedocs.io/en/latest/gpu_memory_allocation.html a__ : List[str] = '''platform''' import jax import jax.numpy as jnp import numpy as np from transformers import FlaxPegasusForConditionalGeneration, FlaxPegasusModel @require_flax class UpperCamelCase_ : """simple docstring""" snake_case__ : Dict = PegasusConfig snake_case__ : Union[str, Any] = {} snake_case__ : Any = "gelu" def __init__( self : str , UpperCAmelCase__ : Union[str, Any] , UpperCAmelCase__ : int=1_3 , UpperCAmelCase__ : Optional[int]=7 , UpperCAmelCase__ : Union[str, Any]=True , UpperCAmelCase__ : Any=False , UpperCAmelCase__ : List[Any]=9_9 , UpperCAmelCase__ : int=3_2 , UpperCAmelCase__ : Dict=5 , UpperCAmelCase__ : Optional[int]=4 , UpperCAmelCase__ : List[Any]=3_7 , UpperCAmelCase__ : int=0.1 , UpperCAmelCase__ : int=0.1 , UpperCAmelCase__ : List[Any]=2_0 , UpperCAmelCase__ : int=2 , UpperCAmelCase__ : List[Any]=1 , UpperCAmelCase__ : Optional[Any]=0 , ) -> Any: __SCREAMING_SNAKE_CASE = parent __SCREAMING_SNAKE_CASE = batch_size __SCREAMING_SNAKE_CASE = seq_length __SCREAMING_SNAKE_CASE = is_training __SCREAMING_SNAKE_CASE = use_labels __SCREAMING_SNAKE_CASE = vocab_size __SCREAMING_SNAKE_CASE = hidden_size __SCREAMING_SNAKE_CASE = num_hidden_layers __SCREAMING_SNAKE_CASE = num_attention_heads __SCREAMING_SNAKE_CASE = intermediate_size __SCREAMING_SNAKE_CASE = hidden_dropout_prob __SCREAMING_SNAKE_CASE = attention_probs_dropout_prob __SCREAMING_SNAKE_CASE = max_position_embeddings __SCREAMING_SNAKE_CASE = eos_token_id __SCREAMING_SNAKE_CASE = pad_token_id __SCREAMING_SNAKE_CASE = bos_token_id def UpperCAmelCase_ ( self : Dict ) -> Dict: __SCREAMING_SNAKE_CASE = ids_tensor([self.batch_size, self.seq_length - 1] , self.vocab_size ).clip(3 , self.vocab_size ) __SCREAMING_SNAKE_CASE = np.expand_dims(np.array([self.eos_token_id] * self.batch_size ) , 1 ) __SCREAMING_SNAKE_CASE = np.concatenate([input_ids, eos_tensor] , axis=1 ) __SCREAMING_SNAKE_CASE = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) __SCREAMING_SNAKE_CASE = 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 , ) __SCREAMING_SNAKE_CASE = prepare_pegasus_inputs_dict(UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__ ) return config, inputs_dict def UpperCAmelCase_ ( self : List[Any] , UpperCAmelCase__ : str , UpperCAmelCase__ : Tuple , UpperCAmelCase__ : List[Any] ) -> str: __SCREAMING_SNAKE_CASE = 2_0 __SCREAMING_SNAKE_CASE = model_class_name(UpperCAmelCase__ ) __SCREAMING_SNAKE_CASE = model.encode(inputs_dict["input_ids"] ) __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = ( inputs_dict["decoder_input_ids"], inputs_dict["decoder_attention_mask"], ) __SCREAMING_SNAKE_CASE = model.init_cache(decoder_input_ids.shape[0] , UpperCAmelCase__ , UpperCAmelCase__ ) __SCREAMING_SNAKE_CASE = jnp.ones((decoder_input_ids.shape[0], max_decoder_length) , dtype="i4" ) __SCREAMING_SNAKE_CASE = jnp.broadcast_to( jnp.arange(decoder_input_ids.shape[-1] - 1 )[None, :] , (decoder_input_ids.shape[0], decoder_input_ids.shape[-1] - 1) , ) __SCREAMING_SNAKE_CASE = model.decode( decoder_input_ids[:, :-1] , UpperCAmelCase__ , decoder_attention_mask=UpperCAmelCase__ , past_key_values=UpperCAmelCase__ , decoder_position_ids=UpperCAmelCase__ , ) __SCREAMING_SNAKE_CASE = jnp.array(decoder_input_ids.shape[0] * [[decoder_input_ids.shape[-1] - 1]] , dtype="i4" ) __SCREAMING_SNAKE_CASE = model.decode( decoder_input_ids[:, -1:] , UpperCAmelCase__ , decoder_attention_mask=UpperCAmelCase__ , past_key_values=outputs_cache.past_key_values , decoder_position_ids=UpperCAmelCase__ , ) __SCREAMING_SNAKE_CASE = model.decode(UpperCAmelCase__ , UpperCAmelCase__ ) __SCREAMING_SNAKE_CASE = np.max(np.abs((outputs_cache_next[0][:, -1, :5] - outputs[0][:, -1, :5]) ) ) self.parent.assertTrue(diff < 1E-3 , msg=F"""Max diff is {diff}""" ) def UpperCAmelCase_ ( self : int , UpperCAmelCase__ : Dict , UpperCAmelCase__ : List[str] , UpperCAmelCase__ : Union[str, Any] ) -> Union[str, Any]: __SCREAMING_SNAKE_CASE = 2_0 __SCREAMING_SNAKE_CASE = model_class_name(UpperCAmelCase__ ) __SCREAMING_SNAKE_CASE = model.encode(inputs_dict["input_ids"] ) __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = ( inputs_dict["decoder_input_ids"], inputs_dict["decoder_attention_mask"], ) __SCREAMING_SNAKE_CASE = jnp.concatenate( [ decoder_attention_mask, jnp.zeros((decoder_attention_mask.shape[0], max_decoder_length - decoder_attention_mask.shape[1]) ), ] , axis=-1 , ) __SCREAMING_SNAKE_CASE = model.init_cache(decoder_input_ids.shape[0] , UpperCAmelCase__ , UpperCAmelCase__ ) __SCREAMING_SNAKE_CASE = jnp.broadcast_to( jnp.arange(decoder_input_ids.shape[-1] - 1 )[None, :] , (decoder_input_ids.shape[0], decoder_input_ids.shape[-1] - 1) , ) __SCREAMING_SNAKE_CASE = model.decode( decoder_input_ids[:, :-1] , UpperCAmelCase__ , decoder_attention_mask=UpperCAmelCase__ , past_key_values=UpperCAmelCase__ , decoder_position_ids=UpperCAmelCase__ , ) __SCREAMING_SNAKE_CASE = jnp.array(decoder_input_ids.shape[0] * [[decoder_input_ids.shape[-1] - 1]] , dtype="i4" ) __SCREAMING_SNAKE_CASE = model.decode( decoder_input_ids[:, -1:] , UpperCAmelCase__ , past_key_values=outputs_cache.past_key_values , decoder_attention_mask=UpperCAmelCase__ , decoder_position_ids=UpperCAmelCase__ , ) __SCREAMING_SNAKE_CASE = model.decode(UpperCAmelCase__ , UpperCAmelCase__ , decoder_attention_mask=UpperCAmelCase__ ) __SCREAMING_SNAKE_CASE = np.max(np.abs((outputs_cache_next[0][:, -1, :5] - outputs[0][:, -1, :5]) ) ) self.parent.assertTrue(diff < 1E-3 , msg=F"""Max diff is {diff}""" ) def UpperCAmelCase__ (lowerCAmelCase_ , lowerCAmelCase_ , lowerCAmelCase_ , lowerCAmelCase_=None , lowerCAmelCase_=None , ): '''simple docstring''' if attention_mask is None: __SCREAMING_SNAKE_CASE = np.not_equal(lowerCAmelCase_ , config.pad_token_id ).astype(np.inta ) if decoder_attention_mask is None: __SCREAMING_SNAKE_CASE = np.concatenate( [ np.ones(decoder_input_ids[:, :1].shape , dtype=np.inta ), np.not_equal(decoder_input_ids[:, 1:] , config.pad_token_id ).astype(np.inta ), ] , axis=-1 , ) return { "input_ids": input_ids, "decoder_input_ids": decoder_input_ids, "attention_mask": attention_mask, "decoder_attention_mask": decoder_attention_mask, } @require_flax class UpperCamelCase_ ( UpperCamelCase , unittest.TestCase): """simple docstring""" snake_case__ : Tuple = ( ( FlaxPegasusForConditionalGeneration, FlaxPegasusModel, ) if is_flax_available() else () ) snake_case__ : Union[str, Any] = (FlaxPegasusForConditionalGeneration,) if is_flax_available() else () snake_case__ : Tuple = True snake_case__ : Union[str, Any] = False snake_case__ : int = False snake_case__ : List[Any] = False def UpperCAmelCase_ ( self : int ) -> Union[str, Any]: __SCREAMING_SNAKE_CASE = FlaxPegasusModelTester(self ) __SCREAMING_SNAKE_CASE = ConfigTester(self , config_class=UpperCAmelCase__ ) def UpperCAmelCase_ ( self : Union[str, Any] ) -> Optional[int]: self.config_tester.run_common_tests() def UpperCAmelCase_ ( self : Tuple ) -> Optional[int]: __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: self.model_tester.check_use_cache_forward(UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__ ) def UpperCAmelCase_ ( self : Tuple ) -> Tuple: __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: self.model_tester.check_use_cache_forward_with_attn_mask(UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__ ) def UpperCAmelCase_ ( self : List[str] ) -> List[str]: __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: with self.subTest(model_class.__name__ ): __SCREAMING_SNAKE_CASE = self._prepare_for_class(UpperCAmelCase__ , UpperCAmelCase__ ) __SCREAMING_SNAKE_CASE = model_class(UpperCAmelCase__ ) @jax.jit def encode_jitted(UpperCAmelCase__ : Optional[Any] , UpperCAmelCase__ : int=None , **UpperCAmelCase__ : int ): return model.encode(input_ids=UpperCAmelCase__ , attention_mask=UpperCAmelCase__ ) with self.subTest("JIT Enabled" ): __SCREAMING_SNAKE_CASE = encode_jitted(**UpperCAmelCase__ ).to_tuple() with self.subTest("JIT Disabled" ): with jax.disable_jit(): __SCREAMING_SNAKE_CASE = encode_jitted(**UpperCAmelCase__ ).to_tuple() self.assertEqual(len(UpperCAmelCase__ ) , len(UpperCAmelCase__ ) ) for jitted_output, output in zip(UpperCAmelCase__ , UpperCAmelCase__ ): self.assertEqual(jitted_output.shape , output.shape ) def UpperCAmelCase_ ( self : Tuple ) -> Any: __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: with self.subTest(model_class.__name__ ): __SCREAMING_SNAKE_CASE = model_class(UpperCAmelCase__ ) __SCREAMING_SNAKE_CASE = model.encode(inputs_dict["input_ids"] , inputs_dict["attention_mask"] ) __SCREAMING_SNAKE_CASE = { "decoder_input_ids": inputs_dict["decoder_input_ids"], "decoder_attention_mask": inputs_dict["decoder_attention_mask"], "encoder_outputs": encoder_outputs, } @jax.jit def decode_jitted(UpperCAmelCase__ : Union[str, Any] , UpperCAmelCase__ : Tuple , UpperCAmelCase__ : List[Any] ): return model.decode( decoder_input_ids=UpperCAmelCase__ , decoder_attention_mask=UpperCAmelCase__ , encoder_outputs=UpperCAmelCase__ , ) with self.subTest("JIT Enabled" ): __SCREAMING_SNAKE_CASE = decode_jitted(**UpperCAmelCase__ ).to_tuple() with self.subTest("JIT Disabled" ): with jax.disable_jit(): __SCREAMING_SNAKE_CASE = decode_jitted(**UpperCAmelCase__ ).to_tuple() self.assertEqual(len(UpperCAmelCase__ ) , len(UpperCAmelCase__ ) ) for jitted_output, output in zip(UpperCAmelCase__ , UpperCAmelCase__ ): self.assertEqual(jitted_output.shape , output.shape ) @slow def UpperCAmelCase_ ( self : Dict ) -> Tuple: for model_class_name in self.all_model_classes: __SCREAMING_SNAKE_CASE = model_class_name.from_pretrained("google/pegasus-large" , from_pt=UpperCAmelCase__ ) __SCREAMING_SNAKE_CASE = np.ones((1, 1) ) __SCREAMING_SNAKE_CASE = model(UpperCAmelCase__ ) self.assertIsNotNone(UpperCAmelCase__ ) @slow def UpperCAmelCase_ ( self : Optional[int] ) -> List[Any]: __SCREAMING_SNAKE_CASE = FlaxPegasusForConditionalGeneration.from_pretrained("google/pegasus-xsum" ) __SCREAMING_SNAKE_CASE = PegasusTokenizer.from_pretrained("google/pegasus-xsum" ) __SCREAMING_SNAKE_CASE = [ " 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!\" ", ] __SCREAMING_SNAKE_CASE = [ "California's largest electricity provider has turned off power to hundreds of thousands of customers.", "Pop group N-Dubz have revealed they were surprised to get four nominations for this year's Mobo Awards.", ] __SCREAMING_SNAKE_CASE = tokenizer(UpperCAmelCase__ , return_tensors="np" , truncation=UpperCAmelCase__ , max_length=5_1_2 , padding=UpperCAmelCase__ ) __SCREAMING_SNAKE_CASE = model.generate(**UpperCAmelCase__ , num_beams=2 ).sequences __SCREAMING_SNAKE_CASE = tokenizer.batch_decode(UpperCAmelCase__ , skip_special_tokens=UpperCAmelCase__ ) assert tgt_text == decoded
54
1
"""simple docstring""" import argparse import os from transformers.utils import direct_transformers_import # All paths are set with the intent you should run this script from the root of the repo with the command # python utils/check_task_guides.py __UpperCamelCase : Any = '''src/transformers''' __UpperCamelCase : Tuple = '''docs/source/en/tasks''' def _SCREAMING_SNAKE_CASE (_UpperCAmelCase : Optional[Any] , _UpperCAmelCase : Tuple , _UpperCAmelCase : List[Any] ): with open(_UpperCAmelCase , 'r' , encoding='utf-8' , newline='\n' ) as f: lowerCAmelCase = f.readlines() # Find the start prompt. lowerCAmelCase = 0 while not lines[start_index].startswith(_UpperCAmelCase ): start_index += 1 start_index += 1 lowerCAmelCase = start_index while not lines[end_index].startswith(_UpperCAmelCase ): end_index += 1 end_index -= 1 while len(lines[start_index] ) <= 1: start_index += 1 while len(lines[end_index] ) <= 1: end_index -= 1 end_index += 1 return "".join(lines[start_index:end_index] ), start_index, end_index, lines # This is to make sure the transformers module imported is the one in the repo. __UpperCamelCase : Tuple = direct_transformers_import(TRANSFORMERS_PATH) __UpperCamelCase : Any = { '''asr.md''': transformers_module.models.auto.modeling_auto.MODEL_FOR_CTC_MAPPING_NAMES, '''audio_classification.md''': transformers_module.models.auto.modeling_auto.MODEL_FOR_AUDIO_CLASSIFICATION_MAPPING_NAMES, '''language_modeling.md''': transformers_module.models.auto.modeling_auto.MODEL_FOR_CAUSAL_LM_MAPPING_NAMES, '''image_classification.md''': transformers_module.models.auto.modeling_auto.MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING_NAMES, '''masked_language_modeling.md''': transformers_module.models.auto.modeling_auto.MODEL_FOR_MASKED_LM_MAPPING_NAMES, '''multiple_choice.md''': transformers_module.models.auto.modeling_auto.MODEL_FOR_MULTIPLE_CHOICE_MAPPING_NAMES, '''object_detection.md''': transformers_module.models.auto.modeling_auto.MODEL_FOR_OBJECT_DETECTION_MAPPING_NAMES, '''question_answering.md''': transformers_module.models.auto.modeling_auto.MODEL_FOR_QUESTION_ANSWERING_MAPPING_NAMES, '''semantic_segmentation.md''': transformers_module.models.auto.modeling_auto.MODEL_FOR_SEMANTIC_SEGMENTATION_MAPPING_NAMES, '''sequence_classification.md''': transformers_module.models.auto.modeling_auto.MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING_NAMES, '''summarization.md''': transformers_module.models.auto.modeling_auto.MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING_NAMES, '''token_classification.md''': transformers_module.models.auto.modeling_auto.MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING_NAMES, '''translation.md''': transformers_module.models.auto.modeling_auto.MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING_NAMES, '''video_classification.md''': transformers_module.models.auto.modeling_auto.MODEL_FOR_VIDEO_CLASSIFICATION_MAPPING_NAMES, '''document_question_answering.md''': transformers_module.models.auto.modeling_auto.MODEL_FOR_DOCUMENT_QUESTION_ANSWERING_MAPPING_NAMES, '''monocular_depth_estimation.md''': transformers_module.models.auto.modeling_auto.MODEL_FOR_DEPTH_ESTIMATION_MAPPING_NAMES, } # This list contains model types used in some task guides that are not in `CONFIG_MAPPING_NAMES` (therefore not in any # `MODEL_MAPPING_NAMES` or any `MODEL_FOR_XXX_MAPPING_NAMES`). __UpperCamelCase : str = { '''summarization.md''': ('''nllb''',), '''translation.md''': ('''nllb''',), } def _SCREAMING_SNAKE_CASE (_UpperCAmelCase : int ): lowerCAmelCase = TASK_GUIDE_TO_MODELS[task_guide] lowerCAmelCase = SPECIAL_TASK_GUIDE_TO_MODEL_TYPES.get(_UpperCAmelCase , set() ) lowerCAmelCase = { code: name for code, name in transformers_module.MODEL_NAMES_MAPPING.items() if (code in model_maping_names or code in special_model_types) } return ", ".join([F'[{name}](../model_doc/{code})' for code, name in model_names.items()] ) + "\n" def _SCREAMING_SNAKE_CASE (_UpperCAmelCase : Optional[int] , _UpperCAmelCase : Union[str, Any]=False ): lowerCAmelCase ,lowerCAmelCase ,lowerCAmelCase ,lowerCAmelCase = _find_text_in_file( filename=os.path.join(_UpperCAmelCase , _UpperCAmelCase ) , start_prompt='<!--This tip is automatically generated by `make fix-copies`, do not fill manually!-->' , end_prompt='<!--End of the generated tip-->' , ) lowerCAmelCase = get_model_list_for_task(_UpperCAmelCase ) if current_list != new_list: if overwrite: with open(os.path.join(_UpperCAmelCase , _UpperCAmelCase ) , 'w' , encoding='utf-8' , newline='\n' ) as f: f.writelines(lines[:start_index] + [new_list] + lines[end_index:] ) else: raise ValueError( F'The list of models that can be used in the {task_guide} guide needs an update. Run `make fix-copies`' ' to fix this.' ) if __name__ == "__main__": __UpperCamelCase : int = argparse.ArgumentParser() parser.add_argument('''--fix_and_overwrite''', action='''store_true''', help='''Whether to fix inconsistencies.''') __UpperCamelCase : List[Any] = parser.parse_args() for task_guide in TASK_GUIDE_TO_MODELS.keys(): check_model_list_for_task(task_guide, args.fix_and_overwrite)
351
"""simple docstring""" import re def _SCREAMING_SNAKE_CASE (_UpperCAmelCase : str ): if len(re.findall('[ATCG]' , _UpperCAmelCase ) ) != len(_UpperCAmelCase ): raise ValueError('Invalid Strand' ) return dna.translate(dna.maketrans('ATCG' , 'TAGC' ) ) if __name__ == "__main__": import doctest doctest.testmod()
309
0
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_tf_available, is_torch_available, is_vision_available, ) _lowerCAmelCase = { '''configuration_convnext''': ['''CONVNEXT_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''ConvNextConfig''', '''ConvNextOnnxConfig'''] } try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _lowerCAmelCase = ['''ConvNextFeatureExtractor'''] _lowerCAmelCase = ['''ConvNextImageProcessor'''] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _lowerCAmelCase = [ '''CONVNEXT_PRETRAINED_MODEL_ARCHIVE_LIST''', '''ConvNextForImageClassification''', '''ConvNextModel''', '''ConvNextPreTrainedModel''', '''ConvNextBackbone''', ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _lowerCAmelCase = [ '''TFConvNextForImageClassification''', '''TFConvNextModel''', '''TFConvNextPreTrainedModel''', ] if TYPE_CHECKING: from .configuration_convnext import CONVNEXT_PRETRAINED_CONFIG_ARCHIVE_MAP, ConvNextConfig, ConvNextOnnxConfig try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .feature_extraction_convnext import ConvNextFeatureExtractor from .image_processing_convnext import ConvNextImageProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_convnext import ( CONVNEXT_PRETRAINED_MODEL_ARCHIVE_LIST, ConvNextBackbone, ConvNextForImageClassification, ConvNextModel, ConvNextPreTrainedModel, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_convnext import TFConvNextForImageClassification, TFConvNextModel, TFConvNextPreTrainedModel else: import sys _lowerCAmelCase = _LazyModule(__name__, globals()['''__file__'''], _import_structure)
37
"""simple docstring""" from typing import Optional, Union import torch from torch import nn from ...configuration_utils import ConfigMixin, register_to_config from ...models.modeling_utils import ModelMixin class _UpperCAmelCase ( UpperCAmelCase__ , UpperCAmelCase__ ): '''simple docstring''' @register_to_config def __init__( self , snake_case_ = 7_6_8 , ): """simple docstring""" super().__init__() A_ : Optional[int] = nn.Parameter(torch.zeros(1 , snake_case_ ) ) A_ : Optional[int] = nn.Parameter(torch.ones(1 , snake_case_ ) ) def lowerCamelCase_ ( self , snake_case_ = None , snake_case_ = None , ): """simple docstring""" A_ : str = nn.Parameter(self.mean.to(snake_case_ ).to(snake_case_ ) ) A_ : Optional[int] = nn.Parameter(self.std.to(snake_case_ ).to(snake_case_ ) ) return self def lowerCamelCase_ ( self , snake_case_ ): """simple docstring""" A_ : Tuple = (embeds - self.mean) * 1.0 / self.std return embeds def lowerCamelCase_ ( self , snake_case_ ): """simple docstring""" A_ : List[str] = (embeds * self.std) + self.mean return embeds
286
0
'''simple docstring''' import warnings from ...utils import logging from .image_processing_flava import FlavaImageProcessor __a: int = logging.get_logger(__name__) class UpperCAmelCase ( a__ ): '''simple docstring''' def __init__( self , *__lowerCAmelCase , **__lowerCAmelCase ) -> None: warnings.warn( '''The class FlavaFeatureExtractor is deprecated and will be removed in version 5 of Transformers. Please''' ''' use FlavaImageProcessor instead.''' , __lowerCAmelCase , ) super().__init__(*__lowerCAmelCase , **__lowerCAmelCase )
358
'''simple docstring''' from manim import * class UpperCAmelCase ( a__ ): '''simple docstring''' def _lowerCAmelCase( self ) -> List[Any]: lowercase__ : int = Rectangle(height=0.5 , width=0.5 ) lowercase__ : Optional[int] = Rectangle(height=0.2_5 , width=0.2_5 ) lowercase__ : Tuple = Rectangle(height=0.4_6 , width=0.4_6 ).set_stroke(width=0 ) lowercase__ : str = [mem.copy() for i in range(6 )] lowercase__ : Dict = [mem.copy() for i in range(6 )] lowercase__ : Tuple = VGroup(*__lowerCAmelCase ).arrange(__lowerCAmelCase , buff=0 ) lowercase__ : List[str] = VGroup(*__lowerCAmelCase ).arrange(__lowerCAmelCase , buff=0 ) lowercase__ : str = VGroup(__lowerCAmelCase , __lowerCAmelCase ).arrange(__lowerCAmelCase , buff=0 ) lowercase__ : str = Text('''CPU''' , font_size=24 ) lowercase__ : List[Any] = Group(__lowerCAmelCase , __lowerCAmelCase ).arrange(__lowerCAmelCase , buff=0.5 , aligned_edge=__lowerCAmelCase ) cpu.move_to([-2.5, -0.5, 0] ) self.add(__lowerCAmelCase ) lowercase__ : Any = [mem.copy() for i in range(4 )] lowercase__ : int = VGroup(*__lowerCAmelCase ).arrange(__lowerCAmelCase , buff=0 ) lowercase__ : Optional[int] = Text('''GPU''' , font_size=24 ) lowercase__ : Tuple = Group(__lowerCAmelCase , __lowerCAmelCase ).arrange(__lowerCAmelCase , buff=0.5 , aligned_edge=__lowerCAmelCase ) gpu.move_to([-1, -1, 0] ) self.add(__lowerCAmelCase ) lowercase__ : int = [mem.copy() for i in range(6 )] lowercase__ : Optional[int] = VGroup(*__lowerCAmelCase ).arrange(__lowerCAmelCase , buff=0 ) lowercase__ : Any = Text('''Model''' , font_size=24 ) lowercase__ : Optional[Any] = Group(__lowerCAmelCase , __lowerCAmelCase ).arrange(__lowerCAmelCase , buff=0.5 , aligned_edge=__lowerCAmelCase ) model.move_to([3, -1.0, 0] ) self.add(__lowerCAmelCase ) lowercase__ : int = [] lowercase__ : int = [] lowercase__ : Any = [] for i, rect in enumerate(__lowerCAmelCase ): rect.set_stroke(__lowerCAmelCase ) lowercase__ : Optional[int] = Rectangle(height=0.4_6 / 4 , width=0.4_6 / 3 ).set_stroke(width=0.0 ).set_fill(__lowerCAmelCase , opacity=0.7 ) if i == 0: cpu_target.next_to(cpu_left_col_base[0].get_corner(DOWN + LEFT ) , buff=0.0_2 , direction=__lowerCAmelCase ) cpu_target.set_x(cpu_target.get_x() + 0.1 ) elif i == 3: cpu_target.next_to(model_cpu_arr[0] , direction=__lowerCAmelCase , buff=0.0 ) else: cpu_target.next_to(model_cpu_arr[i - 1] , direction=__lowerCAmelCase , buff=0.0 ) self.add(__lowerCAmelCase ) model_cpu_arr.append(__lowerCAmelCase ) self.add(*__lowerCAmelCase , *__lowerCAmelCase , *__lowerCAmelCase ) lowercase__ : Optional[int] = [mem.copy() for i in range(6 )] lowercase__ : List[Any] = VGroup(*__lowerCAmelCase ).arrange(__lowerCAmelCase , buff=0 ) lowercase__ : Optional[Any] = Text('''Loaded Checkpoint''' , font_size=24 ) lowercase__ : Dict = Group(__lowerCAmelCase , __lowerCAmelCase ).arrange(__lowerCAmelCase , buff=0.5 , aligned_edge=__lowerCAmelCase ) checkpoint.move_to([3, 0.5, 0] ) self.add(__lowerCAmelCase ) lowercase__ : str = [] lowercase__ : List[str] = [] for i, rect in enumerate(__lowerCAmelCase ): lowercase__ : List[str] = fill.copy().set_fill(__lowerCAmelCase , opacity=0.7 ) target.move_to(__lowerCAmelCase ) ckpt_arr.append(__lowerCAmelCase ) lowercase__ : Any = 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(__lowerCAmelCase ) self.add(*__lowerCAmelCase , *__lowerCAmelCase ) lowercase__ : Optional[int] = Square(side_length=2.2 ) key.move_to([-5, 2, 0] ) lowercase__ : Optional[Any] = 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(__lowerCAmelCase , __lowerCAmelCase ) lowercase__ : str = MarkupText( F"""<span fgcolor='{BLUE}'>●</span> Checkpoint""" , font_size=18 , ) blue_text.next_to(__lowerCAmelCase , DOWN * 2.4 , aligned_edge=key_text.get_left() ) self.add(__lowerCAmelCase ) lowercase__ : Union[str, Any] = 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] ) lowercase__ : Tuple = [meta_mem.copy() for i in range(6 )] lowercase__ : Any = [meta_mem.copy() for i in range(6 )] lowercase__ : str = VGroup(*__lowerCAmelCase ).arrange(__lowerCAmelCase , buff=0 ) lowercase__ : Any = VGroup(*__lowerCAmelCase ).arrange(__lowerCAmelCase , buff=0 ) lowercase__ : Union[str, Any] = VGroup(__lowerCAmelCase , __lowerCAmelCase ).arrange(__lowerCAmelCase , buff=0 ) lowercase__ : Union[str, Any] = Text('''Disk''' , font_size=24 ) lowercase__ : Tuple = Group(__lowerCAmelCase , __lowerCAmelCase ).arrange(__lowerCAmelCase , buff=0.5 , aligned_edge=__lowerCAmelCase ) disk.move_to([-4.0, -1.2_5, 0] ) self.play(Write(__lowerCAmelCase , run_time=3 ) , Write(__lowerCAmelCase , run_time=1 ) , Create(__lowerCAmelCase , run_time=1 ) ) lowercase__ : Tuple = [] for i, rect in enumerate(__lowerCAmelCase ): lowercase__ : Dict = rect.copy() target.generate_target() target.target.move_to(disk_left_col_base[i] ).scale(0.5 ) animations.append(MoveToTarget(__lowerCAmelCase , run_time=1.5 ) ) self.play(*__lowerCAmelCase ) self.play(FadeOut(__lowerCAmelCase ) ) lowercase__ : Dict = 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(__lowerCAmelCase , run_time=3 ) ) self.play( FadeOut(__lowerCAmelCase , __lowerCAmelCase , *__lowerCAmelCase , *__lowerCAmelCase ) , ) self.wait()
214
0
'''simple docstring''' import argparse import torch from transformers import OpenAIGPTConfig, OpenAIGPTModel, load_tf_weights_in_openai_gpt from transformers.utils import CONFIG_NAME, WEIGHTS_NAME, logging logging.set_verbosity_info() def UpperCamelCase_ ( _UpperCAmelCase : int , _UpperCAmelCase : List[Any] , _UpperCAmelCase : Tuple ) -> Optional[int]: """simple docstring""" if openai_config_file == "": _UpperCAmelCase : Tuple = OpenAIGPTConfig() else: _UpperCAmelCase : List[str] = OpenAIGPTConfig.from_json_file(_UpperCAmelCase ) _UpperCAmelCase : int = OpenAIGPTModel(_UpperCAmelCase ) # Load weights from numpy load_tf_weights_in_openai_gpt(_UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase ) # Save pytorch-model _UpperCAmelCase : Any = pytorch_dump_folder_path + "/" + WEIGHTS_NAME _UpperCAmelCase : Dict = pytorch_dump_folder_path + "/" + CONFIG_NAME print(F"""Save PyTorch model to {pytorch_weights_dump_path}""" ) torch.save(model.state_dict() , _UpperCAmelCase ) print(F"""Save configuration file to {pytorch_config_dump_path}""" ) with open(_UpperCAmelCase , "w" , encoding="utf-8" ) as f: f.write(config.to_json_string() ) if __name__ == "__main__": __SCREAMING_SNAKE_CASE : Optional[int] = argparse.ArgumentParser() # Required parameters parser.add_argument( """--openai_checkpoint_folder_path""", default=None, type=str, required=True, help="""Path to the TensorFlow checkpoint path.""", ) parser.add_argument( """--pytorch_dump_folder_path""", default=None, type=str, required=True, help="""Path to the output PyTorch model.""" ) parser.add_argument( """--openai_config_file""", default="""""", type=str, help=( """An optional config json file corresponding to the pre-trained OpenAI model. \n""" """This specifies the model architecture.""" ), ) __SCREAMING_SNAKE_CASE : Any = parser.parse_args() convert_openai_checkpoint_to_pytorch( args.openai_checkpoint_folder_path, args.openai_config_file, args.pytorch_dump_folder_path )
31
'''simple docstring''' __SCREAMING_SNAKE_CASE : Dict = 8.3_1_4_4_6_2 # Unit - J mol-1 K-1 def UpperCamelCase_ ( _UpperCAmelCase : float , _UpperCAmelCase : float , _UpperCAmelCase : float ) -> float: """simple docstring""" if moles < 0 or kelvin < 0 or volume < 0: raise ValueError("Invalid inputs. Enter positive value." ) return moles * kelvin * UNIVERSAL_GAS_CONSTANT / volume def UpperCamelCase_ ( _UpperCAmelCase : float , _UpperCAmelCase : float , _UpperCAmelCase : float ) -> float: """simple docstring""" if moles < 0 or kelvin < 0 or pressure < 0: raise ValueError("Invalid inputs. Enter positive value." ) return moles * kelvin * UNIVERSAL_GAS_CONSTANT / pressure if __name__ == "__main__": from doctest import testmod testmod()
31
1
"""simple docstring""" from __future__ import annotations from scipy.special import comb # type: ignore class snake_case : def __init__( self : str , a__ : List[str] ) -> Optional[Any]: '''simple docstring''' _A = list_of_points # Degree determines the flexibility of the curve. # Degree = 1 will produce a straight line. _A = len(_SCREAMING_SNAKE_CASE ) - 1 def a_ ( self : Optional[Any] , a__ : Tuple ) -> list[float]: '''simple docstring''' assert 0 <= t <= 1, "Time t must be between 0 and 1." _A = [] for i in range(len(self.list_of_points ) ): # basis function for each i output_values.append( comb(self.degree , _SCREAMING_SNAKE_CASE ) * ((1 - t) ** (self.degree - i)) * (t**i) ) # the basis must sum up to 1 for it to produce a valid Bezier curve. assert round(sum(_SCREAMING_SNAKE_CASE ) , 5 ) == 1 return output_values def a_ ( self : str , a__ : List[str] ) -> tuple[float, float]: '''simple docstring''' assert 0 <= t <= 1, "Time t must be between 0 and 1." _A = self.basis_function(_SCREAMING_SNAKE_CASE ) _A = 0.0 _A = 0.0 for i in range(len(self.list_of_points ) ): # For all points, sum up the product of i-th basis function and i-th point. x += basis_function[i] * self.list_of_points[i][0] y += basis_function[i] * self.list_of_points[i][1] return (x, y) def a_ ( self : Dict , a__ : Dict = 0.0_1 ) -> int: '''simple docstring''' from matplotlib import pyplot as plt # type: ignore _A = [] # x coordinates of points to plot _A = [] # y coordinates of points to plot _A = 0.0 while t <= 1: _A = self.bezier_curve_function(_SCREAMING_SNAKE_CASE ) to_plot_x.append(value[0] ) to_plot_y.append(value[1] ) t += step_size _A = [i[0] for i in self.list_of_points] _A = [i[1] for i in self.list_of_points] plt.plot( _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , color="blue" , label="Curve of Degree " + str(self.degree ) , ) plt.scatter(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , color="red" , label="Control Points" ) plt.legend() plt.show() if __name__ == "__main__": import doctest doctest.testmod() BezierCurve([(1, 2), (3, 5)]).plot_curve() # degree 1 BezierCurve([(0, 0), (5, 5), (5, 0)]).plot_curve() # degree 2 BezierCurve([(0, 0), (5, 5), (5, 0), (2.5, -2.5)]).plot_curve() # degree 3
371
"""simple docstring""" import pytest from datasets.utils.sharding import _distribute_shards, _number_of_shards_in_gen_kwargs, _split_gen_kwargs @pytest.mark.parametrize( "kwargs, expected" , [ ({"num_shards": 0, "max_num_jobs": 1}, []), ({"num_shards": 10, "max_num_jobs": 1}, [range(10 )]), ({"num_shards": 10, "max_num_jobs": 10}, [range(__lowercase , i + 1 ) for i in range(10 )]), ({"num_shards": 1, "max_num_jobs": 10}, [range(1 )]), ({"num_shards": 10, "max_num_jobs": 3}, [range(0 , 4 ), range(4 , 7 ), range(7 , 10 )]), ({"num_shards": 3, "max_num_jobs": 10}, [range(0 , 1 ), range(1 , 2 ), range(2 , 3 )]), ] , ) def a__ ( __lowercase , __lowercase ) -> Optional[Any]: _A = _distribute_shards(**__lowercase ) assert out == expected @pytest.mark.parametrize( "gen_kwargs, max_num_jobs, expected" , [ ({"foo": 0}, 10, [{"foo": 0}]), ({"shards": [0, 1, 2, 3]}, 1, [{"shards": [0, 1, 2, 3]}]), ({"shards": [0, 1, 2, 3]}, 4, [{"shards": [0]}, {"shards": [1]}, {"shards": [2]}, {"shards": [3]}]), ({"shards": [0, 1]}, 4, [{"shards": [0]}, {"shards": [1]}]), ({"shards": [0, 1, 2, 3]}, 2, [{"shards": [0, 1]}, {"shards": [2, 3]}]), ] , ) def a__ ( __lowercase , __lowercase , __lowercase ) -> List[str]: _A = _split_gen_kwargs(__lowercase , __lowercase ) assert out == expected @pytest.mark.parametrize( "gen_kwargs, expected" , [ ({"foo": 0}, 1), ({"shards": [0]}, 1), ({"shards": [0, 1, 2, 3]}, 4), ({"shards": [0, 1, 2, 3], "foo": 0}, 4), ({"shards": [0, 1, 2, 3], "other": (0, 1)}, 4), ({"shards": [0, 1, 2, 3], "shards2": [0, 1]}, RuntimeError), ] , ) def a__ ( __lowercase , __lowercase ) -> List[Any]: if expected is RuntimeError: with pytest.raises(__lowercase ): _number_of_shards_in_gen_kwargs(__lowercase ) else: _A = _number_of_shards_in_gen_kwargs(__lowercase ) assert out == expected
163
0
import pytest import datasets.config from datasets.utils.info_utils import is_small_dataset @pytest.mark.parametrize("dataset_size" , [None, 4_00 * 2**20, 6_00 * 2**20] ) @pytest.mark.parametrize("input_in_memory_max_size" , ["default", 0, 1_00 * 2**20, 9_00 * 2**20] ) def A_ ( _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase ): if input_in_memory_max_size != "default": monkeypatch.setattr(datasets.config , "IN_MEMORY_MAX_SIZE" , _UpperCAmelCase ) SCREAMING_SNAKE_CASE_: Any = datasets.config.IN_MEMORY_MAX_SIZE if input_in_memory_max_size == "default": assert in_memory_max_size == 0 else: assert in_memory_max_size == input_in_memory_max_size if dataset_size and in_memory_max_size: SCREAMING_SNAKE_CASE_: Optional[Any] = dataset_size < in_memory_max_size else: SCREAMING_SNAKE_CASE_: Tuple = False SCREAMING_SNAKE_CASE_: str = is_small_dataset(_UpperCAmelCase ) assert result == expected
13
"""simple docstring""" import logging from transformers.configuration_utils import PretrainedConfig a_ = logging.getLogger(__name__) class __snake_case ( SCREAMING_SNAKE_CASE__ ): """simple docstring""" _lowerCamelCase = """masked_bert""" def __init__( self , __lowerCamelCase=3_0522 , __lowerCamelCase=768 , __lowerCamelCase=12 , __lowerCamelCase=12 , __lowerCamelCase=3072 , __lowerCamelCase="gelu" , __lowerCamelCase=0.1 , __lowerCamelCase=0.1 , __lowerCamelCase=512 , __lowerCamelCase=2 , __lowerCamelCase=0.0_2 , __lowerCamelCase=1e-1_2 , __lowerCamelCase=0 , __lowerCamelCase="topK" , __lowerCamelCase="constant" , __lowerCamelCase=0.0 , **__lowerCamelCase , ): '''simple docstring''' super().__init__(pad_token_id=__lowerCamelCase , **__lowerCamelCase ) __A : Dict = vocab_size __A : Union[str, Any] = hidden_size __A : Tuple = num_hidden_layers __A : Tuple = num_attention_heads __A : Optional[Any] = hidden_act __A : List[str] = intermediate_size __A : Any = hidden_dropout_prob __A : Optional[Any] = attention_probs_dropout_prob __A : Any = max_position_embeddings __A : str = type_vocab_size __A : List[Any] = initializer_range __A : str = layer_norm_eps __A : Optional[int] = pruning_method __A : str = mask_init __A : Any = mask_scale
179
0
import os import tempfile import unittest import uuid from pathlib import Path from transformers.testing_utils import get_tests_dir, require_soundfile, require_torch, require_vision from transformers.tools.agent_types import AgentAudio, AgentImage, AgentText from transformers.utils import is_soundfile_availble, is_torch_available, is_vision_available if is_torch_available(): import torch if is_soundfile_availble(): import soundfile as sf if is_vision_available(): from PIL import Image def lowerCamelCase__ ( lowercase="" ): """simple docstring""" SCREAMING_SNAKE_CASE : Optional[Any] = tempfile.mkdtemp() return os.path.join(lowercase , str(uuid.uuida() ) + suffix ) @require_soundfile @require_torch class SCREAMING_SNAKE_CASE ( unittest.TestCase ): '''simple docstring''' def _A ( self : int ): SCREAMING_SNAKE_CASE : Optional[int] = torch.rand(12 , dtype=torch.floataa ) - 0.5 SCREAMING_SNAKE_CASE : List[Any] = AgentAudio(UpperCAmelCase_ ) SCREAMING_SNAKE_CASE : Optional[int] = str(agent_type.to_string() ) # Ensure that the tensor and the agent_type's tensor are the same self.assertTrue(torch.allclose(UpperCAmelCase_ , agent_type.to_raw() , atol=1E-4 ) ) del agent_type # Ensure the path remains even after the object deletion self.assertTrue(os.path.exists(UpperCAmelCase_ ) ) # Ensure that the file contains the same value as the original tensor SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE : Dict = sf.read(UpperCAmelCase_ ) self.assertTrue(torch.allclose(UpperCAmelCase_ , torch.tensor(UpperCAmelCase_ ) , atol=1E-4 ) ) def _A ( self : Union[str, Any] ): SCREAMING_SNAKE_CASE : Any = torch.rand(12 , dtype=torch.floataa ) - 0.5 SCREAMING_SNAKE_CASE : int = get_new_path(suffix=".wav" ) sf.write(UpperCAmelCase_ , UpperCAmelCase_ , 1_6000 ) SCREAMING_SNAKE_CASE : Union[str, Any] = AgentAudio(UpperCAmelCase_ ) self.assertTrue(torch.allclose(UpperCAmelCase_ , agent_type.to_raw() , atol=1E-4 ) ) self.assertEqual(agent_type.to_string() , UpperCAmelCase_ ) @require_vision @require_torch class SCREAMING_SNAKE_CASE ( unittest.TestCase ): '''simple docstring''' def _A ( self : int ): SCREAMING_SNAKE_CASE : Optional[int] = torch.randint(0 , 256 , (64, 64, 3) ) SCREAMING_SNAKE_CASE : str = AgentImage(UpperCAmelCase_ ) SCREAMING_SNAKE_CASE : str = str(agent_type.to_string() ) # Ensure that the tensor and the agent_type's tensor are the same self.assertTrue(torch.allclose(UpperCAmelCase_ , agent_type._tensor , atol=1E-4 ) ) self.assertIsInstance(agent_type.to_raw() , Image.Image ) # Ensure the path remains even after the object deletion del agent_type self.assertTrue(os.path.exists(UpperCAmelCase_ ) ) def _A ( self : Tuple ): SCREAMING_SNAKE_CASE : List[Any] = Path(get_tests_dir("fixtures/tests_samples/COCO" ) ) / "000000039769.png" SCREAMING_SNAKE_CASE : List[str] = Image.open(UpperCAmelCase_ ) SCREAMING_SNAKE_CASE : int = AgentImage(UpperCAmelCase_ ) self.assertTrue(path.samefile(agent_type.to_string() ) ) self.assertTrue(image == agent_type.to_raw() ) # Ensure the path remains even after the object deletion del agent_type self.assertTrue(os.path.exists(UpperCAmelCase_ ) ) def _A ( self : List[str] ): SCREAMING_SNAKE_CASE : Dict = Path(get_tests_dir("fixtures/tests_samples/COCO" ) ) / "000000039769.png" SCREAMING_SNAKE_CASE : Dict = Image.open(UpperCAmelCase_ ) SCREAMING_SNAKE_CASE : int = AgentImage(UpperCAmelCase_ ) self.assertFalse(path.samefile(agent_type.to_string() ) ) self.assertTrue(image == agent_type.to_raw() ) # Ensure the path remains even after the object deletion del agent_type self.assertTrue(os.path.exists(UpperCAmelCase_ ) ) class SCREAMING_SNAKE_CASE ( unittest.TestCase ): '''simple docstring''' def _A ( self : int ): SCREAMING_SNAKE_CASE : Union[str, Any] = "Hey!" SCREAMING_SNAKE_CASE : Optional[Any] = AgentText(UpperCAmelCase_ ) self.assertEqual(UpperCAmelCase_ , agent_type.to_string() ) self.assertEqual(UpperCAmelCase_ , agent_type.to_raw() ) self.assertEqual(UpperCAmelCase_ , UpperCAmelCase_ )
319
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_sentencepiece_available snake_case = {} try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: snake_case = ["""MLukeTokenizer"""] if TYPE_CHECKING: try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_mluke import MLukeTokenizer else: import sys snake_case = _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)
319
1
import argparse import gc import json import os 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 _snake_case = 16 _snake_case = 32 def lowerCAmelCase_ ( snake_case_ ): return int(x / 2**20 ) class lowercase : def __enter__( self ) -> List[Any]: gc.collect() torch.cuda.empty_cache() torch.cuda.reset_max_memory_allocated() # reset the peak gauge to zero _A : Any = torch.cuda.memory_allocated() return self def __exit__( self , *_a ) -> Optional[Any]: gc.collect() torch.cuda.empty_cache() _A : str = torch.cuda.memory_allocated() _A : Dict = torch.cuda.max_memory_allocated() _A : Union[str, Any] = bamb(self.end - self.begin ) _A : Optional[int] = bamb(self.peak - self.begin ) # print(f"delta used/peak {self.used:4d}/{self.peaked:4d}") def lowerCAmelCase_ ( snake_case_,snake_case_ = 16,snake_case_ = "bert-base-cased",snake_case_ = 320,snake_case_ = 160,): _A : Union[str, Any] = AutoTokenizer.from_pretrained(_lowerCamelCase ) _A : Any = load_dataset( """glue""","""mrpc""",split={"""train""": f'''train[:{n_train}]''', """validation""": f'''validation[:{n_val}]'''} ) def tokenize_function(snake_case_ ): # max_length=None => use the model max length (it's actually the default) _A : List[str] = tokenizer(examples["""sentence1"""],examples["""sentence2"""],truncation=_lowerCamelCase,max_length=_lowerCamelCase ) return outputs # Apply the method we just defined to all the examples in all the splits of the dataset _A : Optional[int] = datasets.map( _lowerCamelCase,batched=_lowerCamelCase,remove_columns=["""idx""", """sentence1""", """sentence2"""],load_from_cache_file=_lowerCamelCase ) # We also rename the 'label' column to 'labels' which is the expected name for labels by the models of the # transformers library _A : Tuple = tokenized_datasets.rename_column("""label""","""labels""" ) def collate_fn(snake_case_ ): # 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(_lowerCamelCase,padding="""max_length""",max_length=128,return_tensors="""pt""" ) return tokenizer.pad(_lowerCamelCase,padding="""longest""",return_tensors="""pt""" ) # Instantiate dataloaders. _A : int = DataLoader( tokenized_datasets["""train"""],shuffle=_lowerCamelCase,collate_fn=_lowerCamelCase,batch_size=_lowerCamelCase ) _A : Any = DataLoader( tokenized_datasets["""validation"""],shuffle=_lowerCamelCase,collate_fn=_lowerCamelCase,batch_size=_lowerCamelCase ) return train_dataloader, eval_dataloader def lowerCAmelCase_ ( snake_case_,snake_case_ ): # Initialize accelerator _A : Union[str, Any] = Accelerator() # Sample hyper-parameters for learning rate, batch size, seed and a few other HPs _A : str = config["""lr"""] _A : List[str] = int(config["""num_epochs"""] ) _A : Optional[Any] = int(config["""seed"""] ) _A : List[str] = int(config["""batch_size"""] ) _A : Optional[int] = args.model_name_or_path set_seed(_lowerCamelCase ) _A : int = get_dataloaders(_lowerCamelCase,_lowerCamelCase,_lowerCamelCase,args.n_train,args.n_val ) # Instantiate the model (we build the model here so that the seed also control new weights initialization) _A : int = AutoModelForSequenceClassification.from_pretrained(_lowerCamelCase,return_dict=_lowerCamelCase ) # Instantiate optimizer _A : Dict = ( AdamW if accelerator.state.deepspeed_plugin is None or """optimizer""" not in accelerator.state.deepspeed_plugin.deepspeed_config else DummyOptim ) _A : Any = optimizer_cls(params=model.parameters(),lr=_lowerCamelCase ) if accelerator.state.deepspeed_plugin is not None: _A : Union[str, Any] = accelerator.state.deepspeed_plugin.deepspeed_config[ """gradient_accumulation_steps""" ] else: _A : Optional[int] = 1 _A : int = (len(_lowerCamelCase ) * num_epochs) // gradient_accumulation_steps # Instantiate scheduler if ( accelerator.state.deepspeed_plugin is None or "scheduler" not in accelerator.state.deepspeed_plugin.deepspeed_config ): _A : List[Any] = get_linear_schedule_with_warmup( optimizer=_lowerCamelCase,num_warmup_steps=0,num_training_steps=_lowerCamelCase,) else: _A : Any = DummyScheduler(_lowerCamelCase,total_num_steps=_lowerCamelCase,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. _A : Any = accelerator.prepare( _lowerCamelCase,_lowerCamelCase,_lowerCamelCase,_lowerCamelCase,_lowerCamelCase ) # We need to keep track of how many total steps we have iterated over _A : List[str] = 0 # We also need to keep track of the stating epoch so files are named properly _A : Dict = 0 # Now we train the model _A : Any = {} for epoch in range(_lowerCamelCase,_lowerCamelCase ): with TorchTracemalloc() as tracemalloc: model.train() for step, batch in enumerate(_lowerCamelCase ): _A : Optional[Any] = model(**_lowerCamelCase ) _A : Optional[Any] = outputs.loss _A : Optional[Any] = loss / gradient_accumulation_steps accelerator.backward(_lowerCamelCase ) if step % gradient_accumulation_steps == 0: optimizer.step() lr_scheduler.step() optimizer.zero_grad() overall_step += 1 # Printing the GPU memory usage details such as allocated memory, peak memory, and total memory usage accelerator.print("""Memory before entering the train : {}""".format(bamb(tracemalloc.begin ) ) ) accelerator.print("""Memory consumed at the end of the train (end-begin): {}""".format(tracemalloc.used ) ) accelerator.print("""Peak Memory consumed during the train (max-begin): {}""".format(tracemalloc.peaked ) ) accelerator.print( """Total Peak Memory consumed during the train (max): {}""".format( tracemalloc.peaked + bamb(tracemalloc.begin ) ) ) _A : str = tracemalloc.peaked + bamb(tracemalloc.begin ) if args.peak_memory_upper_bound is not None: assert ( train_total_peak_memory[f'''epoch-{epoch}'''] <= args.peak_memory_upper_bound ), "Peak memory usage exceeded the upper bound" accelerator.wait_for_everyone() if accelerator.is_main_process: with open(os.path.join(args.output_dir,"""peak_memory_utilization.json""" ),"""w""" ) as f: json.dump(_lowerCamelCase,_lowerCamelCase ) def lowerCAmelCase_ ( ): _A : Any = argparse.ArgumentParser(description="""Simple example of training script tracking peak GPU memory usage.""" ) parser.add_argument( """--model_name_or_path""",type=_lowerCamelCase,default="""bert-base-cased""",help="""Path to pretrained model or model identifier from huggingface.co/models.""",required=_lowerCamelCase,) parser.add_argument( """--output_dir""",type=_lowerCamelCase,default=""".""",help="""Optional save directory where all checkpoint folders will be stored. Default is the current working directory.""",) parser.add_argument( """--peak_memory_upper_bound""",type=_lowerCamelCase,default=_lowerCamelCase,help="""The upper bound of peak memory usage in MB. If set, the training will throw an error if the peak memory usage exceeds this value.""",) parser.add_argument( """--n_train""",type=_lowerCamelCase,default=320,help="""Number of training examples to use.""",) parser.add_argument( """--n_val""",type=_lowerCamelCase,default=160,help="""Number of validation examples to use.""",) parser.add_argument( """--num_epochs""",type=_lowerCamelCase,default=1,help="""Number of train epochs.""",) _A : Tuple = parser.parse_args() _A : Union[str, Any] = {"""lr""": 2e-5, """num_epochs""": args.num_epochs, """seed""": 42, """batch_size""": 16} training_function(_lowerCamelCase,_lowerCamelCase ) if __name__ == "__main__": main()
26
'''simple docstring''' from typing import TYPE_CHECKING from ...file_utils import _LazyModule, is_tokenizers_available, is_torch_available, is_vision_available from ...utils import OptionalDependencyNotAvailable UpperCamelCase_ = {"""configuration_dpt""": ["""DPT_PRETRAINED_CONFIG_ARCHIVE_MAP""", """DPTConfig"""]} try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase_ = ["""DPTFeatureExtractor"""] UpperCamelCase_ = ["""DPTImageProcessor"""] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase_ = [ """DPT_PRETRAINED_MODEL_ARCHIVE_LIST""", """DPTForDepthEstimation""", """DPTForSemanticSegmentation""", """DPTModel""", """DPTPreTrainedModel""", ] if TYPE_CHECKING: from .configuration_dpt import DPT_PRETRAINED_CONFIG_ARCHIVE_MAP, DPTConfig try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .feature_extraction_dpt import DPTFeatureExtractor from .image_processing_dpt import DPTImageProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_dpt import ( DPT_PRETRAINED_MODEL_ARCHIVE_LIST, DPTForDepthEstimation, DPTForSemanticSegmentation, DPTModel, DPTPreTrainedModel, ) else: import sys UpperCamelCase_ = _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)
309
0
import warnings from ...utils import logging from .image_processing_chinese_clip import ChineseCLIPImageProcessor UpperCAmelCase = logging.get_logger(__name__) class lowerCAmelCase_ ( lowerCamelCase__ ): '''simple docstring''' def __init__( self , *_UpperCAmelCase , **_UpperCAmelCase ): warnings.warn( '''The class ChineseCLIPFeatureExtractor is deprecated and will be removed in version 5 of Transformers.''' ''' Please use ChineseCLIPImageProcessor instead.''' , _UpperCAmelCase , ) super().__init__(*_UpperCAmelCase , **_UpperCAmelCase )
362
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available UpperCAmelCase = {"""configuration_vit_msn""": ["""VIT_MSN_PRETRAINED_CONFIG_ARCHIVE_MAP""", """ViTMSNConfig"""]} try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCAmelCase = [ """VIT_MSN_PRETRAINED_MODEL_ARCHIVE_LIST""", """ViTMSNModel""", """ViTMSNForImageClassification""", """ViTMSNPreTrainedModel""", ] if TYPE_CHECKING: from .configuration_vit_msn import VIT_MSN_PRETRAINED_CONFIG_ARCHIVE_MAP, ViTMSNConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_vit_msn import ( VIT_MSN_PRETRAINED_MODEL_ARCHIVE_LIST, ViTMSNForImageClassification, ViTMSNModel, ViTMSNPreTrainedModel, ) else: import sys UpperCAmelCase = _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)
267
0
"""simple docstring""" import os import unittest from transformers import FunnelTokenizer, FunnelTokenizerFast from transformers.models.funnel.tokenization_funnel import VOCAB_FILES_NAMES from transformers.testing_utils import require_tokenizers from ...test_tokenization_common import TokenizerTesterMixin @require_tokenizers class a ( __snake_case, unittest.TestCase ): UpperCAmelCase_ : List[str] =FunnelTokenizer UpperCAmelCase_ : Optional[Any] =FunnelTokenizerFast UpperCAmelCase_ : List[Any] =True UpperCAmelCase_ : int =True def UpperCamelCase_ ( self ): super().setUp() lowercase = [ '<unk>', '<cls>', '<sep>', 'want', '##want', '##ed', 'wa', 'un', 'runn', '##ing', ',', 'low', 'lowest', ] lowercase = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES['vocab_file'] ) with open(self.vocab_file , 'w' , encoding='utf-8' ) as vocab_writer: vocab_writer.write(''.join([x + '\n' for x in vocab_tokens] ) ) def UpperCamelCase_ ( self , **_lowerCamelCase ): return FunnelTokenizer.from_pretrained(self.tmpdirname , **_lowerCamelCase ) def UpperCamelCase_ ( self , **_lowerCamelCase ): return FunnelTokenizerFast.from_pretrained(self.tmpdirname , **_lowerCamelCase ) def UpperCamelCase_ ( self , _lowerCamelCase ): lowercase = 'UNwant\u00E9d,running' lowercase = 'unwanted, running' return input_text, output_text def UpperCamelCase_ ( self ): lowercase = self.tokenizer_class(self.vocab_file ) lowercase = tokenizer.tokenize('UNwant\u00E9d,running' ) self.assertListEqual(_lowerCamelCase , ['un', '##want', '##ed', ',', 'runn', '##ing'] ) self.assertListEqual(tokenizer.convert_tokens_to_ids(_lowerCamelCase ) , [7, 4, 5, 1_0, 8, 9] ) def UpperCamelCase_ ( self ): lowercase = self.get_tokenizers(do_lower_case=_lowerCamelCase ) for tokenizer in tokenizers: lowercase = tokenizer('UNwant\u00E9d,running' ) lowercase = len(inputs['input_ids'] ) - 1 self.assertListEqual(inputs['token_type_ids'] , [2] + [0] * sentence_len ) lowercase = tokenizer('UNwant\u00E9d,running' , 'UNwant\u00E9d,running' ) self.assertListEqual(inputs['token_type_ids'] , [2] + [0] * sentence_len + [1] * sentence_len )
220
def snake_case__ ( SCREAMING_SNAKE_CASE_ : str ): '''simple docstring''' if not all(x.isalpha() for x in string ): raise ValueError('String must only contain alphabetic characters.' ) lowercase__ : Optional[int] = sorted(string.lower() ) return len(SCREAMING_SNAKE_CASE_ ) == len(set(SCREAMING_SNAKE_CASE_ ) ) if __name__ == "__main__": snake_case_ = input('''Enter a string ''').strip() snake_case_ = is_isogram(input_str) print(F'''{input_str} is {'an' if isogram else 'not an'} isogram.''')
214
0
import datasets lowerCamelCase__ = '''\ @InProceedings{conneau2018xnli, author = "Conneau, Alexis and Rinott, Ruty and Lample, Guillaume and Williams, Adina and Bowman, Samuel R. and Schwenk, Holger and Stoyanov, Veselin", title = "XNLI: Evaluating Cross-lingual Sentence Representations", booktitle = "Proceedings of the 2018 Conference on Empirical Methods in Natural Language Processing", year = "2018", publisher = "Association for Computational Linguistics", location = "Brussels, Belgium", } ''' lowerCamelCase__ = '''\ XNLI is a subset of a few thousand examples from MNLI which has been translated into a 14 different languages (some low-ish resource). As with MNLI, the goal is to predict textual entailment (does sentence A imply/contradict/neither sentence B) and is a classification task (given two sentences, predict one of three labels). ''' lowerCamelCase__ = ''' Computes XNLI score which is just simple accuracy. Args: predictions: Predicted labels. references: Ground truth labels. Returns: \'accuracy\': accuracy Examples: >>> predictions = [0, 1] >>> references = [0, 1] >>> xnli_metric = datasets.load_metric("xnli") >>> results = xnli_metric.compute(predictions=predictions, references=references) >>> print(results) {\'accuracy\': 1.0} ''' def A(__a: Dict , __a: Union[str, Any] ): return (preds == labels).mean() @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class __magic_name__ (datasets.Metric ): def __a ( self ) -> Tuple: return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { "predictions": datasets.Value("int64" if self.config_name != "sts-b" else "float32" ), "references": datasets.Value("int64" if self.config_name != "sts-b" else "float32" ), } ) , codebase_urls=[] , reference_urls=[] , format="numpy" , ) def __a ( self , _a , _a ) -> List[str]: return {"accuracy": simple_accuracy(_a , _a )}
22
import string from math import logaa def A(__a: str , __a: str ): lowerCAmelCase_ = document.translate( str.maketrans("" , "" , string.punctuation ) ).replace("\n" , "" ) lowerCAmelCase_ = document_without_punctuation.split(" " ) # word tokenization return len([word for word in tokenize_document if word.lower() == term.lower()] ) def A(__a: str , __a: str ): lowerCAmelCase_ = corpus.lower().translate( str.maketrans("" , "" , string.punctuation ) ) # strip all punctuation and replace it with '' lowerCAmelCase_ = corpus_without_punctuation.split("\n" ) lowerCAmelCase_ = term.lower() return (len([doc for doc in docs if term in doc] ), len(__a )) def A(__a: int , __a: int , __a: List[Any]=False ): if smoothing: if n == 0: raise ValueError("log10(0) is undefined." ) return round(1 + logaa(n / (1 + df) ) , 3 ) if df == 0: raise ZeroDivisionError("df must be > 0" ) elif n == 0: raise ValueError("log10(0) is undefined." ) return round(logaa(n / df ) , 3 ) def A(__a: int , __a: int ): return round(tf * idf , 3 )
22
1
from __future__ import annotations import queue class UpperCAmelCase__ : """simple docstring""" def __init__( self , A_ ) -> Optional[int]: __UpperCamelCase =data __UpperCamelCase =None __UpperCamelCase =None def _UpperCAmelCase ( ): print('\n********Press N to stop entering at any point of time********\n' ) __UpperCamelCase =input('Enter the value of the root node: ' ).strip().lower() __UpperCamelCase =queue.Queue() __UpperCamelCase =TreeNode(int(SCREAMING_SNAKE_CASE__ ) ) q.put(SCREAMING_SNAKE_CASE__ ) while not q.empty(): __UpperCamelCase =q.get() __UpperCamelCase =F'Enter the left node of {node_found.data}: ' __UpperCamelCase =input(SCREAMING_SNAKE_CASE__ ).strip().lower() or 'n' if check == "n": return tree_node __UpperCamelCase =TreeNode(int(SCREAMING_SNAKE_CASE__ ) ) __UpperCamelCase =left_node q.put(SCREAMING_SNAKE_CASE__ ) __UpperCamelCase =F'Enter the right node of {node_found.data}: ' __UpperCamelCase =input(SCREAMING_SNAKE_CASE__ ).strip().lower() or 'n' if check == "n": return tree_node __UpperCamelCase =TreeNode(int(SCREAMING_SNAKE_CASE__ ) ) __UpperCamelCase =right_node q.put(SCREAMING_SNAKE_CASE__ ) raise def _UpperCAmelCase ( SCREAMING_SNAKE_CASE__ : TreeNode ): if not isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) or not node: return print(node.data , end=',' ) pre_order(node.left ) pre_order(node.right ) def _UpperCAmelCase ( SCREAMING_SNAKE_CASE__ : TreeNode ): if not isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) or not node: return in_order(node.left ) print(node.data , end=',' ) in_order(node.right ) def _UpperCAmelCase ( SCREAMING_SNAKE_CASE__ : TreeNode ): if not isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) or not node: return post_order(node.left ) post_order(node.right ) print(node.data , end=',' ) def _UpperCAmelCase ( SCREAMING_SNAKE_CASE__ : TreeNode ): if not isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) or not node: return __UpperCamelCase =queue.Queue() q.put(SCREAMING_SNAKE_CASE__ ) while not q.empty(): __UpperCamelCase =q.get() print(node_dequeued.data , end=',' ) if node_dequeued.left: q.put(node_dequeued.left ) if node_dequeued.right: q.put(node_dequeued.right ) def _UpperCAmelCase ( SCREAMING_SNAKE_CASE__ : TreeNode ): if not isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) or not node: return __UpperCamelCase =queue.Queue() q.put(SCREAMING_SNAKE_CASE__ ) while not q.empty(): __UpperCamelCase =[] while not q.empty(): __UpperCamelCase =q.get() print(node_dequeued.data , end=',' ) if node_dequeued.left: list_.append(node_dequeued.left ) if node_dequeued.right: list_.append(node_dequeued.right ) print() for node in list_: q.put(SCREAMING_SNAKE_CASE__ ) def _UpperCAmelCase ( SCREAMING_SNAKE_CASE__ : TreeNode ): if not isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) or not node: return __UpperCamelCase =[] __UpperCamelCase =node while n or stack: while n: # start from root node, find its left child print(n.data , end=',' ) stack.append(SCREAMING_SNAKE_CASE__ ) __UpperCamelCase =n.left # end of while means current node doesn't have left child __UpperCamelCase =stack.pop() # start to traverse its right child __UpperCamelCase =n.right def _UpperCAmelCase ( SCREAMING_SNAKE_CASE__ : TreeNode ): if not isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) or not node: return __UpperCamelCase =[] __UpperCamelCase =node while n or stack: while n: stack.append(SCREAMING_SNAKE_CASE__ ) __UpperCamelCase =n.left __UpperCamelCase =stack.pop() print(n.data , end=',' ) __UpperCamelCase =n.right def _UpperCAmelCase ( SCREAMING_SNAKE_CASE__ : TreeNode ): if not isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) or not node: return __UpperCamelCase , __UpperCamelCase =[], [] __UpperCamelCase =node stacka.append(SCREAMING_SNAKE_CASE__ ) while stacka: # to find the reversed order of post order, store it in stack2 __UpperCamelCase =stacka.pop() if n.left: stacka.append(n.left ) if n.right: stacka.append(n.right ) stacka.append(SCREAMING_SNAKE_CASE__ ) while stacka: # pop up from stack2 will be the post order print(stacka.pop().data , end=',' ) def _UpperCAmelCase ( SCREAMING_SNAKE_CASE__ : str = "" , SCREAMING_SNAKE_CASE__ : Optional[int]=50 , SCREAMING_SNAKE_CASE__ : Tuple="*" ): if not s: return "\n" + width * char __UpperCamelCase , __UpperCamelCase =divmod(width - len(SCREAMING_SNAKE_CASE__ ) - 2 , 2 ) return F'{left * char} {s} {(left + extra) * char}' if __name__ == "__main__": import doctest doctest.testmod() print(prompt('Binary Tree Traversals')) _A = build_tree() print(prompt('Pre Order Traversal')) pre_order(node) print(prompt() + '\n') print(prompt('In Order Traversal')) in_order(node) print(prompt() + '\n') print(prompt('Post Order Traversal')) post_order(node) print(prompt() + '\n') print(prompt('Level Order Traversal')) level_order(node) print(prompt() + '\n') print(prompt('Actual Level Order Traversal')) level_order_actual(node) print('*' * 50 + '\n') print(prompt('Pre Order Traversal - Iteration Version')) pre_order_iter(node) print(prompt() + '\n') print(prompt('In Order Traversal - Iteration Version')) in_order_iter(node) print(prompt() + '\n') print(prompt('Post Order Traversal - Iteration Version')) post_order_iter(node) print(prompt())
62
'''simple docstring''' from typing import List import datasets from datasets.tasks import AudioClassification from ..folder_based_builder import folder_based_builder __A =datasets.utils.logging.get_logger(__name__) class _snake_case ( folder_based_builder.FolderBasedBuilderConfig ): lowerCAmelCase :bool = None lowerCAmelCase :bool = None class _snake_case ( folder_based_builder.FolderBasedBuilder ): lowerCAmelCase :Optional[Any] = datasets.Audio() lowerCAmelCase :Tuple = '''audio''' lowerCAmelCase :Optional[Any] = AudioFolderConfig lowerCAmelCase :List[str] # definition at the bottom of the script lowerCAmelCase :Union[str, Any] = AudioClassification(audio_column='''audio''' , label_column='''label''' ) __A =[ '.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', ] __A =AUDIO_EXTENSIONS
163
0
import argparse from collections import OrderedDict from pathlib import Path import torch from transformers import ( VisualBertConfig, VisualBertForMultipleChoice, VisualBertForPreTraining, VisualBertForQuestionAnswering, VisualBertForVisualReasoning, ) from transformers.utils import logging logging.set_verbosity_info() _lowerCAmelCase : str = logging.get_logger(__name__) _lowerCAmelCase : Union[str, Any] = [ ("bert.bert", "visual_bert"), ("bert.cls", "cls"), ("bert.classifier", "cls"), ("token_type_embeddings_visual", "visual_token_type_embeddings"), ("position_embeddings_visual", "visual_position_embeddings"), ("projection", "visual_projection"), ] _lowerCAmelCase : int = [ "nlvr2_coco_pre_trained.th", "nlvr2_fine_tuned.th", "nlvr2_pre_trained.th", "vcr_coco_pre_train.th", "vcr_fine_tune.th", "vcr_pre_train.th", "vqa_coco_pre_trained.th", "vqa_fine_tuned.th", "vqa_pre_trained.th", ] def UpperCamelCase_( _snake_case : int ): """simple docstring""" __a =torch.load(_snake_case , map_location='cpu' ) return sd def UpperCamelCase_( _snake_case : Optional[int] , _snake_case : List[str] , _snake_case : int=rename_keys_prefix ): """simple docstring""" __a =OrderedDict() __a =torch.arange(config.max_position_embeddings ).expand((1, -1) ) # detector_d = OrderedDict() for key in d: if "detector" in key: # detector_d[key.replace('detector.','')] = d[key] continue __a =key for name_pair in rename_keys_prefix: __a =new_key.replace(name_pair[0] , name_pair[1] ) __a =d[key] if key == "bert.cls.predictions.decoder.weight": # Old bert code didn't have `decoder.bias`, but was added separately __a =new_d['cls.predictions.bias'] return new_d @torch.no_grad() def UpperCamelCase_( _snake_case : List[Any] , _snake_case : int ): """simple docstring""" assert ( checkpoint_path.split('/' )[-1] in ACCEPTABLE_CHECKPOINTS ), F'The checkpoint provided must be in {ACCEPTABLE_CHECKPOINTS}.' # Get Config if "pre" in checkpoint_path: __a ='pretraining' if "vcr" in checkpoint_path: __a ={'visual_embedding_dim': 512} elif "vqa_advanced" in checkpoint_path: __a ={'visual_embedding_dim': 2048} elif "vqa" in checkpoint_path: __a ={'visual_embedding_dim': 2048} elif "nlvr" in checkpoint_path: __a ={'visual_embedding_dim': 1024} else: raise NotImplementedError(F'No implementation found for `{checkpoint_path}`.' ) else: if "vcr" in checkpoint_path: __a ={'visual_embedding_dim': 512} __a ='multichoice' elif "vqa_advanced" in checkpoint_path: __a ={'visual_embedding_dim': 2048} __a ='vqa_advanced' elif "vqa" in checkpoint_path: __a ={'visual_embedding_dim': 2048, 'num_labels': 3129} __a ='vqa' elif "nlvr" in checkpoint_path: __a ={ 'visual_embedding_dim': 1024, 'num_labels': 2, } __a ='nlvr' __a =VisualBertConfig(**_snake_case ) # Load State Dict __a =load_state_dict(_snake_case ) __a =get_new_dict(_snake_case , _snake_case ) if model_type == "pretraining": __a =VisualBertForPreTraining(_snake_case ) elif model_type == "vqa": __a =VisualBertForQuestionAnswering(_snake_case ) elif model_type == "nlvr": __a =VisualBertForVisualReasoning(_snake_case ) elif model_type == "multichoice": __a =VisualBertForMultipleChoice(_snake_case ) model.load_state_dict(_snake_case ) # Save Checkpoints Path(_snake_case ).mkdir(exist_ok=_snake_case ) model.save_pretrained(_snake_case ) if __name__ == "__main__": _lowerCAmelCase : Tuple = argparse.ArgumentParser() # Required parameters parser.add_argument("orig_checkpoint_path", type=str, help="A path to .th on local filesystem.") parser.add_argument("pytorch_dump_folder_path", type=str, help="Path to the output PyTorch model.") _lowerCAmelCase : Any = parser.parse_args() convert_visual_bert_checkpoint(args.orig_checkpoint_path, args.pytorch_dump_folder_path)
308
import argparse import dataclasses import json import logging import os import shutil from typing import List, Optional import datasets from accelerate import Accelerator from datasets import load_dataset from finetuning import finetune from tqdm.auto import tqdm import transformers from transformers import AutoConfig, set_seed from transformers.trainer_utils import IntervalStrategy _lowerCAmelCase : Optional[int] = logging.getLogger(__name__) _lowerCAmelCase : Any = "pytorch_model.bin" @dataclasses.dataclass class __magic_name__ : SCREAMING_SNAKE_CASE = dataclasses.field( metadata={'help': 'Path to pretrained model or model identifier from huggingface.co/models.'} ) SCREAMING_SNAKE_CASE = dataclasses.field( default=lowerCAmelCase_ , metadata={'help': 'Where do you want to store the pretrained models downloaded from huggingface.co.'} , ) @dataclasses.dataclass class __magic_name__ : SCREAMING_SNAKE_CASE = dataclasses.field(metadata={'help': 'A csv or a json file containing the training data.'} ) SCREAMING_SNAKE_CASE = dataclasses.field(metadata={'help': 'A csv or a json file containing the data to predict on.'} ) SCREAMING_SNAKE_CASE = dataclasses.field( default=lowerCAmelCase_ , metadata={'help': 'A csv or a json file containing the validation data.'} ) SCREAMING_SNAKE_CASE = dataclasses.field( default=lowerCAmelCase_ , metadata={'help': 'The name of the task to train on.'} , ) SCREAMING_SNAKE_CASE = dataclasses.field( default=lowerCAmelCase_ , metadata={'help': 'The list of labels for the task.'} ) @dataclasses.dataclass class __magic_name__ : SCREAMING_SNAKE_CASE = dataclasses.field( metadata={'help': 'The output directory where the model predictions and checkpoints will be written.'} ) SCREAMING_SNAKE_CASE = dataclasses.field( default='accuracy' , metadata={'help': 'The evaluation metric used for the task.'} ) SCREAMING_SNAKE_CASE = dataclasses.field( default='no' , metadata={ 'help': 'The evaluation strategy to adopt during training. Possible values are: ["no", "step", "epoch]' } , ) SCREAMING_SNAKE_CASE = dataclasses.field( default=1_0 , metadata={'help': 'Number of evaluation calls with no improvement after which training will be stopped.'} , ) SCREAMING_SNAKE_CASE = dataclasses.field( default=0.0 , metadata={ 'help': 'How much the specified evaluation metric must improve to satisfy early stopping conditions.' } , ) SCREAMING_SNAKE_CASE = dataclasses.field( default=lowerCAmelCase_ , metadata={'help': 'Whether to filter the pseudo-labeled data based on the confidence score.'} , ) SCREAMING_SNAKE_CASE = dataclasses.field( default=lowerCAmelCase_ , metadata={'help': 'Whether to filter the pseudo-labeled data based on the validation performance.'} , ) SCREAMING_SNAKE_CASE = dataclasses.field( default=lowerCAmelCase_ , metadata={'help': 'Whether to fine-tune on labeled data after pseudo training.'} , ) SCREAMING_SNAKE_CASE = dataclasses.field( default=0.0 , metadata={'help': 'Confidence threshold for pseudo-labeled data filtering.'} , ) SCREAMING_SNAKE_CASE = dataclasses.field( default=1_0_0 , metadata={'help': 'Number of evaluation calls with no improvement after which training will be stopped.'} , ) SCREAMING_SNAKE_CASE = dataclasses.field( default=lowerCAmelCase_ , metadata={'help': 'Random seed for initialization.'} , ) def UpperCamelCase_( _snake_case : int , _snake_case : str , _snake_case : Optional[int] , _snake_case : str , _snake_case : Union[str, Any] , _snake_case : List[Any] ): """simple docstring""" __a =datasets.concatenate_datasets([infer_input, infer_output] , axis=1 ) if args.do_filter_by_confidence: __a =dataset.filter(lambda _snake_case : example["probability"] > args.confidence_threshold ) if args.do_filter_by_val_performance: assert eval_result >= 0.0 and eval_result <= 1.0 __a =int(eval_result * len(_snake_case ) ) print(_snake_case ) __a =dataset.sort('probability' , reverse=_snake_case ) __a =dataset.select(range(_snake_case ) ) __a =dataset.remove_columns(['label', 'probability'] ) __a =dataset.rename_column('prediction' , 'label' ) __a =dataset.map(lambda _snake_case : {"label": idalabel[example["label"]]} ) __a =dataset.shuffle(seed=args.seed ) __a =os.path.join(_snake_case , F'train_pseudo.{args.data_file_extension}' ) if args.data_file_extension == "csv": dataset.to_csv(_snake_case , index=_snake_case ) else: dataset.to_json(_snake_case ) def UpperCamelCase_( _snake_case : List[Any] , _snake_case : str , _snake_case : int , _snake_case : Optional[int] , **_snake_case : List[str] ): """simple docstring""" __a =Accelerator() # Make one log on every process with the configuration for debugging. logging.basicConfig( format='%(asctime)s - %(levelname)s - %(name)s - %(message)s' , datefmt='%m/%d/%Y %H:%M:%S' , level=logging.INFO , ) logger.info(accelerator.state ) # Setup logging, we only want one process per machine to log things on the # screen. accelerator.is_local_main_process is only True for one process per # machine. logger.setLevel(logging.INFO if accelerator.is_local_main_process else logging.ERROR ) if accelerator.is_local_main_process: datasets.utils.logging.set_verbosity_warning() transformers.utils.logging.set_verbosity_info() else: datasets.utils.logging.set_verbosity_error() transformers.utils.logging.set_verbosity_error() __a =STModelArguments(model_name_or_path=_snake_case ) __a =STDataArguments(train_file=_snake_case , infer_file=_snake_case ) __a =STTrainingArguments(output_dir=_snake_case ) __a =argparse.Namespace() for arg_class in (model_args, data_args, training_args): for key, value in vars(_snake_case ).items(): setattr(_snake_case , _snake_case , _snake_case ) for key, value in kwargs.items(): if hasattr(_snake_case , _snake_case ): setattr(_snake_case , _snake_case , _snake_case ) # Sanity checks __a ={} __a =None # You need to provide the training data and the data to predict on assert args.train_file is not None assert args.infer_file is not None __a =args.train_file __a =args.infer_file if args.evaluation_strategy != IntervalStrategy.NO.value: assert args.eval_file is not None __a =args.eval_file for key in data_files: __a =data_files[key].split('.' )[-1] assert extension in ["csv", "json"], F'`{key}_file` should be a csv or a json file.' if args.data_file_extension is None: __a =extension else: assert extension == args.data_file_extension, F'`{key}_file` should be a {args.data_file_extension} file`.' assert ( args.eval_metric in datasets.list_metrics() ), F'{args.eval_metric} not in the list of supported metrics {datasets.list_metrics()}.' # If passed along, set the training seed now. if args.seed is not None: set_seed(args.seed ) logger.info('Creating the initial data directory for self-training...' ) __a =F'{args.output_dir}/self-train_iter-{{}}'.format __a =data_dir_format(0 ) if accelerator.is_main_process: if args.output_dir is not None: os.makedirs(args.output_dir , exist_ok=_snake_case ) os.makedirs(_snake_case , exist_ok=_snake_case ) accelerator.wait_for_everyone() __a =None __a =None __a =0 __a =False # Show the progress bar __a =tqdm(range(args.max_selftrain_iterations ) , disable=not accelerator.is_local_main_process ) # Self-train for iteration in range(0 , int(args.max_selftrain_iterations ) ): __a =data_dir_format(_snake_case ) assert os.path.exists(_snake_case ) # Stage 1: initial fine-tuning for iteration = 0 or pseudo-training for # iteration > 0 __a =os.path.join(_snake_case , 'stage-1' ) __a ={ 'accelerator': accelerator, 'model_name_or_path': args.model_name_or_path, 'cache_dir': args.cache_dir, 'do_train': True, 'train_file': data_files['train'] if iteration == 0 else data_files['train_pseudo'], 'do_eval': True if args.eval_file is not None else False, 'eval_file': data_files['eval'], 'do_predict': True, 'infer_file': data_files['infer'], 'task_name': args.task_name, 'label_list': args.label_list, 'output_dir': current_output_dir, 'eval_metric': args.eval_metric, 'evaluation_strategy': args.evaluation_strategy, 'early_stopping_patience': args.early_stopping_patience, 'early_stopping_threshold': args.early_stopping_threshold, 'seed': args.seed, } # Add additional training arguments for key, value in kwargs.items(): if key not in arguments_dict and not hasattr(_snake_case , _snake_case ): arguments_dict.update({key: value} ) __a =os.path.join(_snake_case , 'best-checkpoint' , _snake_case ) if os.path.exists(_snake_case ): logger.info( 'Found existing model checkpoint at %s. Skipping self-training: iteration: %d, stage: 1.' , _snake_case , _snake_case , ) else: logger.info('***** Running self-training: iteration: %d, stage: 1 *****' , _snake_case ) finetune(**_snake_case ) accelerator.wait_for_everyone() assert os.path.exists(_snake_case ) logger.info('Self-training job completed: iteration: %d, stage: 1.' , _snake_case ) if iteration > 0 and args.finetune_on_labeled_data: # Stage 2 (optional): fine-tuning on the original labeled data __a =os.path.join(_snake_case , 'best-checkpoint' ) __a =os.path.join(_snake_case , 'stage-2' ) # Update arguments_dict __a =model_path __a =data_files['train'] __a =current_output_dir __a =os.path.join(_snake_case , 'best-checkpoint' , _snake_case ) if os.path.exists(_snake_case ): logger.info( 'Found existing model checkpoint at %s. Skipping self-training: iteration: %d, stage: 2.' , _snake_case , _snake_case , ) else: logger.info('***** Running self-training: iteration: %d, stage: 2 *****' , _snake_case ) finetune(**_snake_case ) accelerator.wait_for_everyone() assert os.path.exists(_snake_case ) logger.info('Self-training job completed: iteration: %d, stage: 2.' , _snake_case ) __a =iteration __a =data_dir_format(iteration + 1 ) __a =AutoConfig.from_pretrained(os.path.join(_snake_case , 'best-checkpoint' ) ) __a =config.idalabel __a =os.path.join(_snake_case , 'eval_results_best-checkpoint.json' ) __a =os.path.join(_snake_case , 'test_results_best-checkpoint.json' ) assert os.path.exists(_snake_case ) with open(_snake_case , 'r' ) as f: __a =float(json.load(_snake_case )[args.eval_metric] ) __a =os.path.join(_snake_case , 'infer_output_best-checkpoint.csv' ) assert os.path.exists(_snake_case ) # Loading the dataset from local csv or json files. __a =load_dataset(args.data_file_extension , data_files={'data': data_files['infer']} )['data'] __a =load_dataset('csv' , data_files={'data': infer_output_file} )['data'] if accelerator.is_main_process: os.makedirs(_snake_case , exist_ok=_snake_case ) shutil.copy(_snake_case , os.path.join(_snake_case , F'eval_results_iter-{iteration}.json' ) ) if os.path.exists(_snake_case ): shutil.copy(_snake_case , os.path.join(_snake_case , F'test_results_iter-{iteration}.json' ) ) create_pseudo_labeled_data(_snake_case , _snake_case , _snake_case , _snake_case , _snake_case , _snake_case ) accelerator.wait_for_everyone() __a =os.path.join(_snake_case , F'train_pseudo.{args.data_file_extension}' ) if args.evaluation_strategy != IntervalStrategy.NO.value: __a =eval_result if best_iteration is None: __a =new_iteration __a =new_eval_result else: if new_eval_result - best_eval_result > args.early_stopping_threshold: __a =new_iteration __a =new_eval_result __a =0 else: if new_eval_result == best_eval_result: __a =new_iteration __a =new_eval_result early_stopping_patience_counter += 1 if early_stopping_patience_counter >= args.early_stopping_patience: __a =True progress_bar.update(1 ) if should_training_stop: break if best_iteration is not None: # Save the best iteration logger.info('Best iteration: %d' , _snake_case ) logger.info('Best evaluation result: %s = %f' , args.eval_metric , _snake_case ) accelerator.wait_for_everyone() if accelerator.is_main_process: shutil.copy( os.path.join(_snake_case , F'eval_results_iter-{iteration}.json' ) , os.path.join(_snake_case , 'eval_results_best-iteration.json' ) , ) else: # Assume that the last iteration is the best logger.info('Best iteration: %d' , args.max_selftrain_iterations - 1 ) logger.info('Best evaluation result: %s = %f' , args.eval_metric , _snake_case ) accelerator.wait_for_everyone() if accelerator.is_main_process: shutil.copy( os.path.join(_snake_case , F'eval_results_iter-{args.max_selftrain_iterations - 1}.json' ) , os.path.join(_snake_case , 'eval_results_best-iteration.json' ) , )
308
1
'''simple docstring''' # flake8: noqa # Lint as: python3 from typing import Dict, List, Optional, Type from .. import config from ..utils import logging from .formatting import ( ArrowFormatter, CustomFormatter, Formatter, PandasFormatter, PythonFormatter, TensorFormatter, format_table, query_table, ) from .np_formatter import NumpyFormatter UpperCamelCase = logging.get_logger(__name__) UpperCamelCase = {} UpperCamelCase = {} UpperCamelCase = {} def SCREAMING_SNAKE_CASE( __lowercase , __lowercase , __lowercase = None , ) -> str: A: Union[str, Any] = aliases if aliases is not None else [] if format_type in _FORMAT_TYPES: logger.warning( F"""Overwriting format type '{format_type}' ({_FORMAT_TYPES[format_type].__name__} -> {formatter_cls.__name__})""" ) A: Union[str, Any] = formatter_cls for alias in set(aliases + [format_type] ): if alias in _FORMAT_TYPES_ALIASES: logger.warning( F"""Overwriting format type alias '{alias}' ({_FORMAT_TYPES_ALIASES[alias]} -> {format_type})""" ) A: Optional[int] = format_type def SCREAMING_SNAKE_CASE( __lowercase , __lowercase , __lowercase = None ) -> Optional[Any]: A: List[Any] = aliases if aliases is not None else [] for alias in set(aliases + [format_type] ): A: List[str] = unavailable_error # Here we define all the available formatting functions that can be used by `Dataset.set_format` _register_formatter(PythonFormatter, None, aliases=['''python''']) _register_formatter(ArrowFormatter, '''arrow''', aliases=['''pa''', '''pyarrow''']) _register_formatter(NumpyFormatter, '''numpy''', aliases=['''np''']) _register_formatter(PandasFormatter, '''pandas''', aliases=['''pd''']) _register_formatter(CustomFormatter, '''custom''') if config.TORCH_AVAILABLE: from .torch_formatter import TorchFormatter _register_formatter(TorchFormatter, '''torch''', aliases=['''pt''', '''pytorch''']) else: UpperCamelCase = ValueError('''PyTorch needs to be installed to be able to return PyTorch tensors.''') _register_unavailable_formatter(_torch_error, '''torch''', aliases=['''pt''', '''pytorch''']) if config.TF_AVAILABLE: from .tf_formatter import TFFormatter _register_formatter(TFFormatter, '''tensorflow''', aliases=['''tf''']) else: UpperCamelCase = ValueError('''Tensorflow needs to be installed to be able to return Tensorflow tensors.''') _register_unavailable_formatter(_tf_error, '''tensorflow''', aliases=['''tf''']) if config.JAX_AVAILABLE: from .jax_formatter import JaxFormatter _register_formatter(JaxFormatter, '''jax''', aliases=[]) else: UpperCamelCase = ValueError('''JAX needs to be installed to be able to return JAX arrays.''') _register_unavailable_formatter(_jax_error, '''jax''', aliases=[]) def SCREAMING_SNAKE_CASE( __lowercase ) -> Optional[str]: if format_type in _FORMAT_TYPES_ALIASES: return _FORMAT_TYPES_ALIASES[format_type] else: return format_type def SCREAMING_SNAKE_CASE( __lowercase , **__lowercase ) -> Formatter: A: int = get_format_type_from_alias(__lowercase ) if format_type in _FORMAT_TYPES: return _FORMAT_TYPES[format_type](**__lowercase ) if format_type in _FORMAT_TYPES_ALIASES_UNAVAILABLE: raise _FORMAT_TYPES_ALIASES_UNAVAILABLE[format_type] else: raise ValueError( F"""Return type should be None or selected in {list(type for type in _FORMAT_TYPES.keys() if type != None )}, but got '{format_type}'""" )
319
'''simple docstring''' import heapq import sys import numpy as np UpperCamelCase = tuple[int, int] class lowerCAmelCase_ : '''simple docstring''' def __init__( self : List[Any] ) -> str: '''simple docstring''' A: Any = [] A: int = set() def _snake_case ( self : Optional[Any] ) -> int: '''simple docstring''' if not self.empty(): return self.elements[0][0] else: return float('''inf''' ) def _snake_case ( self : List[str] ) -> List[Any]: '''simple docstring''' return len(self.elements ) == 0 def _snake_case ( self : Optional[int] , SCREAMING_SNAKE_CASE_ : int , SCREAMING_SNAKE_CASE_ : Any ) -> List[Any]: '''simple docstring''' if item not in self.set: heapq.heappush(self.elements , (priority, item) ) self.set.add(SCREAMING_SNAKE_CASE_ ) else: # update # print("update", item) A: Optional[int] = [] ((A) , (A)): str = heapq.heappop(self.elements ) while x != item: temp.append((pri, x) ) ((A) , (A)): int = heapq.heappop(self.elements ) temp.append((priority, item) ) for pro, xxx in temp: heapq.heappush(self.elements , (pro, xxx) ) def _snake_case ( self : Optional[int] , SCREAMING_SNAKE_CASE_ : str ) -> Any: '''simple docstring''' if item in self.set: self.set.remove(SCREAMING_SNAKE_CASE_ ) A: str = [] ((A) , (A)): List[str] = heapq.heappop(self.elements ) while x != item: temp.append((pro, x) ) ((A) , (A)): Any = heapq.heappop(self.elements ) for prito, yyy in temp: heapq.heappush(self.elements , (prito, yyy) ) def _snake_case ( self : List[Any] ) -> Optional[int]: '''simple docstring''' return self.elements[0][1] def _snake_case ( self : int ) -> Union[str, Any]: '''simple docstring''' ((A) , (A)): Dict = heapq.heappop(self.elements ) self.set.remove(SCREAMING_SNAKE_CASE_ ) return (priority, item) def SCREAMING_SNAKE_CASE( __lowercase , __lowercase ) -> Union[str, Any]: # euclidean distance A: List[str] = np.array(__lowercase ) A: Optional[int] = np.array(__lowercase ) return np.linalg.norm(a - b ) def SCREAMING_SNAKE_CASE( __lowercase , __lowercase ) -> int: # integer division by time variable return consistent_heuristic(__lowercase , __lowercase ) // t def SCREAMING_SNAKE_CASE( __lowercase , __lowercase ) -> Optional[Any]: # manhattan distance return abs(p[0] - goal[0] ) + abs(p[1] - goal[1] ) def SCREAMING_SNAKE_CASE( __lowercase , __lowercase , __lowercase , __lowercase ) -> List[Any]: A: int = g_function[start] + Wa * heuristics[i](__lowercase , __lowercase ) return ans def SCREAMING_SNAKE_CASE( __lowercase , __lowercase , __lowercase ) -> Optional[int]: A: Union[str, Any] = np.chararray((n, n) ) for i in range(__lowercase ): for j in range(__lowercase ): A: Union[str, Any] = '''*''' for i in range(__lowercase ): for j in range(__lowercase ): if (j, (n - 1) - i) in blocks: A: Optional[Any] = '''#''' A: Tuple = '''-''' A: List[str] = back_pointer[goal] while x != start: ((A) , (A)): Tuple = x # print(x) A: List[str] = '''-''' A: str = back_pointer[x] A: Dict = '''-''' for i in range(__lowercase ): for j in range(__lowercase ): if (i, j) == (0, n - 1): print(grid[i][j] , end=''' ''' ) print('''<-- End position''' , end=''' ''' ) else: print(grid[i][j] , end=''' ''' ) print() print('''^''' ) print('''Start position''' ) print() print('''# is an obstacle''' ) print('''- is the path taken by algorithm''' ) print('''PATH TAKEN BY THE ALGORITHM IS:-''' ) A: List[str] = back_pointer[goal] while x != start: print(__lowercase , end=''' ''' ) A: Optional[int] = back_pointer[x] print(__lowercase ) sys.exit() def SCREAMING_SNAKE_CASE( __lowercase ) -> Optional[Any]: if p[0] < 0 or p[0] > n - 1: return False if p[1] < 0 or p[1] > n - 1: return False return True def SCREAMING_SNAKE_CASE( __lowercase , __lowercase , __lowercase , __lowercase , __lowercase , __lowercase , __lowercase , __lowercase , ) -> Union[str, Any]: for itera in range(__lowercase ): open_list[itera].remove_element(__lowercase ) # print("s", s) # print("j", j) ((A) , (A)): Tuple = s A: Optional[Any] = (x - 1, y) A: str = (x + 1, y) A: List[Any] = (x, y + 1) A: int = (x, y - 1) for neighbours in [left, right, up, down]: if neighbours not in blocks: if valid(__lowercase ) and neighbours not in visited: # print("neighbour", neighbours) visited.add(__lowercase ) A: int = -1 A: int = float('''inf''' ) if valid(__lowercase ) and g_function[neighbours] > g_function[s] + 1: A: List[str] = g_function[s] + 1 A: List[str] = s if neighbours not in close_list_anchor: open_list[0].put(__lowercase , key(__lowercase , 0 , __lowercase , __lowercase ) ) if neighbours not in close_list_inad: for var in range(1 , __lowercase ): if key(__lowercase , __lowercase , __lowercase , __lowercase ) <= Wa * key( __lowercase , 0 , __lowercase , __lowercase ): open_list[j].put( __lowercase , key(__lowercase , __lowercase , __lowercase , __lowercase ) ) def SCREAMING_SNAKE_CASE( ) -> Tuple: A: str = [] for x in range(1 , 5 ): for y in range(1 , 6 ): some_list.append((x, y) ) for x in range(1_5 , 2_0 ): some_list.append((x, 1_7) ) for x in range(1_0 , 1_9 ): for y in range(1 , 1_5 ): some_list.append((x, y) ) # L block for x in range(1 , 4 ): for y in range(1_2 , 1_9 ): some_list.append((x, y) ) for x in range(3 , 1_3 ): for y in range(1_6 , 1_9 ): some_list.append((x, y) ) return some_list UpperCamelCase = {0: consistent_heuristic, 1: heuristic_a, 2: heuristic_a} UpperCamelCase = [ (0, 1), (1, 1), (2, 1), (3, 1), (4, 1), (5, 1), (6, 1), (7, 1), (8, 1), (9, 1), (10, 1), (11, 1), (12, 1), (13, 1), (14, 1), (15, 1), (16, 1), (17, 1), (18, 1), (19, 1), ] UpperCamelCase = make_common_ground() UpperCamelCase = blocks_blk # hyper parameters UpperCamelCase = 1 UpperCamelCase = 1 UpperCamelCase = 20 UpperCamelCase = 3 # one consistent and two other inconsistent # start and end destination UpperCamelCase = (0, 0) UpperCamelCase = (n - 1, n - 1) UpperCamelCase = 1 def SCREAMING_SNAKE_CASE( __lowercase , __lowercase , __lowercase ) -> int: A: int = {start: 0, goal: float('''inf''' )} A: Union[str, Any] = {start: -1, goal: -1} A: List[Any] = [] A: Union[str, Any] = set() for i in range(__lowercase ): open_list.append(PriorityQueue() ) open_list[i].put(__lowercase , key(__lowercase , __lowercase , __lowercase , __lowercase ) ) A: list[int] = [] A: list[int] = [] while open_list[0].minkey() < float('''inf''' ): for i in range(1 , __lowercase ): # print(open_list[0].minkey(), open_list[i].minkey()) if open_list[i].minkey() <= Wa * open_list[0].minkey(): global t t += 1 if g_function[goal] <= open_list[i].minkey(): if g_function[goal] < float('''inf''' ): do_something(__lowercase , __lowercase , __lowercase ) else: A , A: Union[str, Any] = open_list[i].top_show() visited.add(__lowercase ) expand_state( __lowercase , __lowercase , __lowercase , __lowercase , __lowercase , __lowercase , __lowercase , __lowercase , ) close_list_inad.append(__lowercase ) else: if g_function[goal] <= open_list[0].minkey(): if g_function[goal] < float('''inf''' ): do_something(__lowercase , __lowercase , __lowercase ) else: A: Union[str, Any] = open_list[0].top_show() visited.add(__lowercase ) expand_state( __lowercase , 0 , __lowercase , __lowercase , __lowercase , __lowercase , __lowercase , __lowercase , ) close_list_anchor.append(__lowercase ) print('''No path found to goal''' ) print() for i in range(n - 1 , -1 , -1 ): for j in range(__lowercase ): if (j, i) in blocks: print('''#''' , end=''' ''' ) elif (j, i) in back_pointer: if (j, i) == (n - 1, n - 1): print('''*''' , end=''' ''' ) else: print('''-''' , end=''' ''' ) else: print('''*''' , end=''' ''' ) if (j, i) == (n - 1, n - 1): print('''<-- End position''' , end=''' ''' ) print() print('''^''' ) print('''Start position''' ) print() print('''# is an obstacle''' ) print('''- is the path taken by algorithm''' ) if __name__ == "__main__": multi_a_star(start, goal, n_heuristic)
319
1
import argparse import json import os from collections import OrderedDict import numpy as np import tensorflow as tf import torch def _UpperCAmelCase ( SCREAMING_SNAKE_CASE__ : Union[str, Any] ): __UpperCamelCase =os.path.join(args.tf_model_dir , 'parameters.json' ) __UpperCamelCase =json.loads(open(SCREAMING_SNAKE_CASE__ ).read() ) if not params: raise ValueError( F'It seems that the json file at {parameter_file} is empty. Make sure you have a correct json file.' ) if not args.output.endswith('.pt' ): __UpperCamelCase =args.output + '''.pt''' __UpperCamelCase =OrderedDict() with tf.device('/CPU:0' ): __UpperCamelCase =tf.train.load_checkpoint(args.tf_model_dir ) __UpperCamelCase =reader.get_variable_to_shape_map() for key_name in shapes.keys(): __UpperCamelCase =reader.get_tensor(SCREAMING_SNAKE_CASE__ ).astype(np.floataa ) if key_name.endswith('/adam_m' ) or key_name.endswith('/adam_v' ): continue if key_name.startswith('pasts/' ): if key_name.startswith('pasts/mlp' ): __UpperCamelCase =int(key_name[9] ) elif key_name.startswith('pasts/out' ): __UpperCamelCase =8 __UpperCamelCase ='''model.sqout.%d.weight''' % (player * 2) # enter to nn.Sequencial with Tanh, so 2 at a time __UpperCamelCase =vnp.transpose([1, 0] ).copy() # Mesh-Tensorflow is a diagonal matrix __UpperCamelCase =torch.tensor(SCREAMING_SNAKE_CASE__ ) elif key_name.startswith('model/moe' ): __UpperCamelCase =int(key_name[9:].split('/' )[0] ) if key_name.endswith('/switch_gating/kernel' ): __UpperCamelCase ='''model.blocks.%d.feed_forward.mlp.router.classifier.weight''' % player __UpperCamelCase =vnp.transpose([1, 0] ).copy() # Mesh-Tensorflow is a diagonal matrix __UpperCamelCase =torch.tensor(SCREAMING_SNAKE_CASE__ ) elif key_name.endswith('/softmlp/kernel' ): __UpperCamelCase ='''model.blocks.%d.feed_forward.soft_bypass_mlp.weight''' % player __UpperCamelCase =vnp.transpose([1, 0] ).copy() # Mesh-Tensorflow is a diagonal matrix __UpperCamelCase =torch.tensor(SCREAMING_SNAKE_CASE__ ) elif key_name.endswith('/wo/kernel' ) or key_name.endswith('/wi/kernel' ): __UpperCamelCase =key_name[-9:-7] for i in range(16 ): __UpperCamelCase ='''model.blocks.%d.feed_forward.mlp.experts.expert_%d.%s.weight''' % (player, i, nlayer) __UpperCamelCase =( vnp[i].transpose([1, 0] ).copy() ) # In Mesh-Tensorflow, it is one array, so it is divided __UpperCamelCase =torch.tensor(SCREAMING_SNAKE_CASE__ ) elif key_name.startswith('model/mlp' ): __UpperCamelCase =int(key_name[9:].split('/' )[0] ) if key_name.endswith('/p1/kernel' ): __UpperCamelCase ='''model.blocks.%d.feed_forward.mlp.wi.weight''' % player __UpperCamelCase =vnp.transpose([1, 0] ).copy() # Mesh-Tensorflow is a diagonal matrix __UpperCamelCase =torch.tensor(SCREAMING_SNAKE_CASE__ ) elif key_name.endswith('/p1/bias' ): __UpperCamelCase ='''model.blocks.%d.feed_forward.mlp.wi.bias''' % player __UpperCamelCase =vnp.copy() # same because it is one dimensional __UpperCamelCase =torch.tensor(SCREAMING_SNAKE_CASE__ ) elif key_name.endswith('/p2/kernel' ): __UpperCamelCase ='''model.blocks.%d.feed_forward.mlp.wo.weight''' % player __UpperCamelCase =vnp.transpose([1, 0] ).copy() # Mesh-Tensorflow is a diagonal matrix __UpperCamelCase =torch.tensor(SCREAMING_SNAKE_CASE__ ) elif key_name.endswith('/p2/bias' ): __UpperCamelCase ='''model.blocks.%d.feed_forward.mlp.wo.bias''' % player __UpperCamelCase =vnp.copy() # same because it is one dimensional __UpperCamelCase =torch.tensor(SCREAMING_SNAKE_CASE__ ) elif key_name.startswith('model/ln' ): __UpperCamelCase =int(key_name[8:].split('/' )[0] ) if key_name.endswith('/b' ): __UpperCamelCase ='''model.blocks.%d.feed_forward.norm.bias''' % player __UpperCamelCase =vnp.copy() # same because it is one dimensional __UpperCamelCase =torch.tensor(SCREAMING_SNAKE_CASE__ ) elif key_name.endswith('/g' ): __UpperCamelCase ='''model.blocks.%d.feed_forward.norm.weight''' % player __UpperCamelCase =vnp.copy() # same because it is one dimensional __UpperCamelCase =torch.tensor(SCREAMING_SNAKE_CASE__ ) elif key_name.startswith('model/att' ): __UpperCamelCase =int(key_name[9:].split('/' )[0] ) if key_name.endswith('/qkv/kernel' ): __UpperCamelCase =vnp.copy() # Compute same dimension as Mesh-tensorflow using einsum __UpperCamelCase =state[:, 0, :, :] __UpperCamelCase =state[:, 1, :, :] __UpperCamelCase =state[:, 2, :, :] __UpperCamelCase =( state_q.reshape([state_q.shape[0], state_q.shape[1] * state_q.shape[2]] ) .transpose([1, 0] ) .copy() ) # Mesh-Tensorflow is a diagonal matrix __UpperCamelCase =( state_k.reshape([state_k.shape[0], state_k.shape[1] * state_k.shape[2]] ) .transpose([1, 0] ) .copy() ) # Mesh-Tensorflow is a diagonal matrix __UpperCamelCase =( state_v.reshape([state_v.shape[0], state_v.shape[1] * state_v.shape[2]] ) .transpose([1, 0] ) .copy() ) # Mesh-Tensorflow is a diagonal matrix __UpperCamelCase ='''model.blocks.%d.self_attn.self_attn.q_proj.weight''' % player __UpperCamelCase =torch.tensor(SCREAMING_SNAKE_CASE__ ) __UpperCamelCase ='''model.blocks.%d.self_attn.self_attn.k_proj.weight''' % player __UpperCamelCase =torch.tensor(SCREAMING_SNAKE_CASE__ ) __UpperCamelCase ='''model.blocks.%d.self_attn.self_attn.v_proj.weight''' % player __UpperCamelCase =torch.tensor(SCREAMING_SNAKE_CASE__ ) elif key_name.endswith('/o/kernel' ): __UpperCamelCase ='''model.blocks.%d.self_attn.self_attn.out_proj.weight''' % player __UpperCamelCase =( vnp.reshape([vnp.shape[0] * vnp.shape[1], vnp.shape[2]] ).transpose([1, 0] ).copy() ) # Mesh-Tensorflow is a diagonal matrix __UpperCamelCase =torch.tensor(SCREAMING_SNAKE_CASE__ ) elif key_name.startswith('model/an' ): __UpperCamelCase =int(key_name[8:].split('/' )[0] ) if key_name.endswith('/b' ): __UpperCamelCase ='''model.blocks.%d.self_attn.norm.bias''' % player __UpperCamelCase =vnp.copy() # same because it is one dimensional __UpperCamelCase =torch.tensor(SCREAMING_SNAKE_CASE__ ) elif key_name.endswith('/g' ): __UpperCamelCase ='''model.blocks.%d.self_attn.norm.weight''' % player __UpperCamelCase =vnp.copy() # same because it is one dimensional __UpperCamelCase =torch.tensor(SCREAMING_SNAKE_CASE__ ) elif ( key_name.startswith('model/wte' ) or key_name.startswith('model/wpe' ) or key_name.startswith('model/ete' ) ): __UpperCamelCase ={'''wte''': '''embed_tokens''', '''wpe''': '''position_embeddings''', '''ete''': '''extra_position_embeddings'''}[ key_name[-3:] ] __UpperCamelCase ='''model.%s.weight''' % nlayer __UpperCamelCase =vnp.copy() # same in embedded __UpperCamelCase =torch.tensor(SCREAMING_SNAKE_CASE__ ) if key_name.startswith('model/wte' ): __UpperCamelCase ='''lm_head.weight''' __UpperCamelCase =vnp.copy() # same in embedded __UpperCamelCase =torch.tensor(SCREAMING_SNAKE_CASE__ ) elif key_name.startswith('model/wob' ): __UpperCamelCase ='''final_logits_bias''' __UpperCamelCase =vnp.copy() # same in embedded __UpperCamelCase =state.reshape((1, -1) ) __UpperCamelCase =torch.tensor(SCREAMING_SNAKE_CASE__ ) elif key_name == "model/dense/kernel": __UpperCamelCase ='''model.last_project.weight''' __UpperCamelCase =vnp.transpose([1, 0] ).copy() # Mesh-Tensorflow is a diagonal matrix __UpperCamelCase =torch.tensor(SCREAMING_SNAKE_CASE__ ) elif key_name == "model/dense_1/bias": __UpperCamelCase ='''model.last_project.bias''' __UpperCamelCase =vnp.copy() # same because it is one dimensional __UpperCamelCase =torch.tensor(SCREAMING_SNAKE_CASE__ ) torch.save(SCREAMING_SNAKE_CASE__ , args.output ) if __name__ == "__main__": _A = argparse.ArgumentParser( description='model converter.', formatter_class=argparse.ArgumentDefaultsHelpFormatter ) parser.add_argument('--tf_model_dir', metavar='PATH', type=str, required=True, help='import model') parser.add_argument('--output', metavar='PATH', type=str, required=True, help='output model') _A = parser.parse_args() convert_tf_gptsan_to_pt(args)
367
from ..utils import DummyObject, requires_backends class UpperCAmelCase__ ( metaclass=A_ ): """simple docstring""" UpperCAmelCase__ : Any = ["speech"] def __init__( self , *A_ , **A_ ) -> Any: requires_backends(self , ['speech'] ) class UpperCAmelCase__ ( metaclass=A_ ): """simple docstring""" UpperCAmelCase__ : Union[str, Any] = ["speech"] def __init__( self , *A_ , **A_ ) -> Union[str, Any]: requires_backends(self , ['speech'] )
117
0
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_sentencepiece_available, is_tf_available, is_tokenizers_available, is_torch_available, ) lowercase : Dict = {'configuration_xglm': ['XGLM_PRETRAINED_CONFIG_ARCHIVE_MAP', 'XGLMConfig']} try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase : Tuple = ['XGLMTokenizer'] try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase : Optional[Any] = ['XGLMTokenizerFast'] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase : Dict = [ 'XGLM_PRETRAINED_MODEL_ARCHIVE_LIST', 'XGLMForCausalLM', 'XGLMModel', 'XGLMPreTrainedModel', ] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase : List[str] = [ 'FlaxXGLMForCausalLM', 'FlaxXGLMModel', 'FlaxXGLMPreTrainedModel', ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase : Union[str, Any] = [ 'TF_XGLM_PRETRAINED_MODEL_ARCHIVE_LIST', 'TFXGLMForCausalLM', 'TFXGLMModel', 'TFXGLMPreTrainedModel', ] if TYPE_CHECKING: from .configuration_xglm import XGLM_PRETRAINED_CONFIG_ARCHIVE_MAP, XGLMConfig try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_xglm import XGLMTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_xglm_fast import XGLMTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_xglm import XGLM_PRETRAINED_MODEL_ARCHIVE_LIST, XGLMForCausalLM, XGLMModel, XGLMPreTrainedModel try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_flax_xglm import FlaxXGLMForCausalLM, FlaxXGLMModel, FlaxXGLMPreTrainedModel try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_xglm import ( TF_XGLM_PRETRAINED_MODEL_ARCHIVE_LIST, TFXGLMForCausalLM, TFXGLMModel, TFXGLMPreTrainedModel, ) else: import sys lowercase : int = _LazyModule(__name__, globals()['__file__'], _import_structure)
3
'''simple docstring''' import argparse import os from . import ( ALBERT_PRETRAINED_CONFIG_ARCHIVE_MAP, BART_PRETRAINED_MODEL_ARCHIVE_LIST, BERT_PRETRAINED_CONFIG_ARCHIVE_MAP, CAMEMBERT_PRETRAINED_CONFIG_ARCHIVE_MAP, CTRL_PRETRAINED_CONFIG_ARCHIVE_MAP, DISTILBERT_PRETRAINED_CONFIG_ARCHIVE_MAP, DPR_CONTEXT_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST, DPR_QUESTION_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST, DPR_READER_PRETRAINED_MODEL_ARCHIVE_LIST, ELECTRA_PRETRAINED_CONFIG_ARCHIVE_MAP, FLAUBERT_PRETRAINED_CONFIG_ARCHIVE_MAP, GPT2_PRETRAINED_CONFIG_ARCHIVE_MAP, LAYOUTLM_PRETRAINED_MODEL_ARCHIVE_LIST, LXMERT_PRETRAINED_CONFIG_ARCHIVE_MAP, OPENAI_GPT_PRETRAINED_CONFIG_ARCHIVE_MAP, ROBERTA_PRETRAINED_CONFIG_ARCHIVE_MAP, T5_PRETRAINED_CONFIG_ARCHIVE_MAP, TRANSFO_XL_PRETRAINED_CONFIG_ARCHIVE_MAP, WAV_2_VEC_2_PRETRAINED_CONFIG_ARCHIVE_MAP, XLM_PRETRAINED_CONFIG_ARCHIVE_MAP, XLM_ROBERTA_PRETRAINED_CONFIG_ARCHIVE_MAP, XLNET_PRETRAINED_CONFIG_ARCHIVE_MAP, AlbertConfig, BartConfig, BertConfig, CamembertConfig, CTRLConfig, DistilBertConfig, DPRConfig, ElectraConfig, FlaubertConfig, GPTaConfig, LayoutLMConfig, LxmertConfig, OpenAIGPTConfig, RobertaConfig, TaConfig, TFAlbertForPreTraining, TFBartForConditionalGeneration, TFBartForSequenceClassification, TFBertForPreTraining, TFBertForQuestionAnswering, TFBertForSequenceClassification, TFCamembertForMaskedLM, TFCTRLLMHeadModel, TFDistilBertForMaskedLM, TFDistilBertForQuestionAnswering, TFDPRContextEncoder, TFDPRQuestionEncoder, TFDPRReader, TFElectraForPreTraining, TFFlaubertWithLMHeadModel, TFGPTaLMHeadModel, TFLayoutLMForMaskedLM, TFLxmertForPreTraining, TFLxmertVisualFeatureEncoder, TFOpenAIGPTLMHeadModel, TFRobertaForCausalLM, TFRobertaForMaskedLM, TFRobertaForSequenceClassification, TFTaForConditionalGeneration, TFTransfoXLLMHeadModel, TFWavaVecaModel, TFXLMRobertaForMaskedLM, TFXLMWithLMHeadModel, TFXLNetLMHeadModel, TransfoXLConfig, WavaVecaConfig, WavaVecaModel, XLMConfig, XLMRobertaConfig, XLNetConfig, is_torch_available, load_pytorch_checkpoint_in_tfa_model, ) from .utils import CONFIG_NAME, WEIGHTS_NAME, cached_file, logging if is_torch_available(): import numpy as np import torch from . import ( AlbertForPreTraining, BartForConditionalGeneration, BertForPreTraining, BertForQuestionAnswering, BertForSequenceClassification, CamembertForMaskedLM, CTRLLMHeadModel, DistilBertForMaskedLM, DistilBertForQuestionAnswering, DPRContextEncoder, DPRQuestionEncoder, DPRReader, ElectraForPreTraining, FlaubertWithLMHeadModel, GPTaLMHeadModel, LayoutLMForMaskedLM, LxmertForPreTraining, LxmertVisualFeatureEncoder, OpenAIGPTLMHeadModel, RobertaForMaskedLM, RobertaForSequenceClassification, TaForConditionalGeneration, TransfoXLLMHeadModel, XLMRobertaForMaskedLM, XLMWithLMHeadModel, XLNetLMHeadModel, ) logging.set_verbosity_info() UpperCAmelCase : Tuple = { 'bart': ( BartConfig, TFBartForConditionalGeneration, TFBartForSequenceClassification, BartForConditionalGeneration, BART_PRETRAINED_MODEL_ARCHIVE_LIST, ), 'bert': ( BertConfig, TFBertForPreTraining, BertForPreTraining, BERT_PRETRAINED_CONFIG_ARCHIVE_MAP, ), 'bert-large-uncased-whole-word-masking-finetuned-squad': ( BertConfig, TFBertForQuestionAnswering, BertForQuestionAnswering, BERT_PRETRAINED_CONFIG_ARCHIVE_MAP, ), 'bert-large-cased-whole-word-masking-finetuned-squad': ( BertConfig, TFBertForQuestionAnswering, BertForQuestionAnswering, BERT_PRETRAINED_CONFIG_ARCHIVE_MAP, ), 'bert-base-cased-finetuned-mrpc': ( BertConfig, TFBertForSequenceClassification, BertForSequenceClassification, BERT_PRETRAINED_CONFIG_ARCHIVE_MAP, ), 'dpr': ( DPRConfig, TFDPRQuestionEncoder, TFDPRContextEncoder, TFDPRReader, DPRQuestionEncoder, DPRContextEncoder, DPRReader, DPR_CONTEXT_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST, DPR_QUESTION_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST, DPR_READER_PRETRAINED_MODEL_ARCHIVE_LIST, ), 'gpt2': ( GPTaConfig, TFGPTaLMHeadModel, GPTaLMHeadModel, GPT2_PRETRAINED_CONFIG_ARCHIVE_MAP, ), 'xlnet': ( XLNetConfig, TFXLNetLMHeadModel, XLNetLMHeadModel, XLNET_PRETRAINED_CONFIG_ARCHIVE_MAP, ), 'xlm': ( XLMConfig, TFXLMWithLMHeadModel, XLMWithLMHeadModel, XLM_PRETRAINED_CONFIG_ARCHIVE_MAP, ), 'xlm-roberta': ( XLMRobertaConfig, TFXLMRobertaForMaskedLM, XLMRobertaForMaskedLM, XLM_ROBERTA_PRETRAINED_CONFIG_ARCHIVE_MAP, ), 'transfo-xl': ( TransfoXLConfig, TFTransfoXLLMHeadModel, TransfoXLLMHeadModel, TRANSFO_XL_PRETRAINED_CONFIG_ARCHIVE_MAP, ), 'openai-gpt': ( OpenAIGPTConfig, TFOpenAIGPTLMHeadModel, OpenAIGPTLMHeadModel, OPENAI_GPT_PRETRAINED_CONFIG_ARCHIVE_MAP, ), 'roberta': ( RobertaConfig, TFRobertaForCausalLM, TFRobertaForMaskedLM, RobertaForMaskedLM, ROBERTA_PRETRAINED_CONFIG_ARCHIVE_MAP, ), 'layoutlm': ( LayoutLMConfig, TFLayoutLMForMaskedLM, LayoutLMForMaskedLM, LAYOUTLM_PRETRAINED_MODEL_ARCHIVE_LIST, ), 'roberta-large-mnli': ( RobertaConfig, TFRobertaForSequenceClassification, RobertaForSequenceClassification, ROBERTA_PRETRAINED_CONFIG_ARCHIVE_MAP, ), 'camembert': ( CamembertConfig, TFCamembertForMaskedLM, CamembertForMaskedLM, CAMEMBERT_PRETRAINED_CONFIG_ARCHIVE_MAP, ), 'flaubert': ( FlaubertConfig, TFFlaubertWithLMHeadModel, FlaubertWithLMHeadModel, FLAUBERT_PRETRAINED_CONFIG_ARCHIVE_MAP, ), 'distilbert': ( DistilBertConfig, TFDistilBertForMaskedLM, DistilBertForMaskedLM, DISTILBERT_PRETRAINED_CONFIG_ARCHIVE_MAP, ), 'distilbert-base-distilled-squad': ( DistilBertConfig, TFDistilBertForQuestionAnswering, DistilBertForQuestionAnswering, DISTILBERT_PRETRAINED_CONFIG_ARCHIVE_MAP, ), 'lxmert': ( LxmertConfig, TFLxmertForPreTraining, LxmertForPreTraining, LXMERT_PRETRAINED_CONFIG_ARCHIVE_MAP, ), 'lxmert-visual-feature-encoder': ( LxmertConfig, TFLxmertVisualFeatureEncoder, LxmertVisualFeatureEncoder, LXMERT_PRETRAINED_CONFIG_ARCHIVE_MAP, ), 'ctrl': ( CTRLConfig, TFCTRLLMHeadModel, CTRLLMHeadModel, CTRL_PRETRAINED_CONFIG_ARCHIVE_MAP, ), 'albert': ( AlbertConfig, TFAlbertForPreTraining, AlbertForPreTraining, ALBERT_PRETRAINED_CONFIG_ARCHIVE_MAP, ), 't5': ( TaConfig, TFTaForConditionalGeneration, TaForConditionalGeneration, T5_PRETRAINED_CONFIG_ARCHIVE_MAP, ), 'electra': ( ElectraConfig, TFElectraForPreTraining, ElectraForPreTraining, ELECTRA_PRETRAINED_CONFIG_ARCHIVE_MAP, ), 'wav2vec2': ( WavaVecaConfig, TFWavaVecaModel, WavaVecaModel, WAV_2_VEC_2_PRETRAINED_CONFIG_ARCHIVE_MAP, ), } def a__ ( a__ , a__ , a__ , a__ , a__=False , a__=True ): """simple docstring""" if model_type not in MODEL_CLASSES: raise ValueError(F'Unrecognized model type, should be one of {list(MODEL_CLASSES.keys() )}.' ) __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = MODEL_CLASSES[model_type] # Initialise TF model if config_file in aws_config_map: __SCREAMING_SNAKE_CASE = cached_file(a__ , a__ , force_download=not use_cached_models ) __SCREAMING_SNAKE_CASE = config_class.from_json_file(a__ ) __SCREAMING_SNAKE_CASE = True __SCREAMING_SNAKE_CASE = True print(F'Building TensorFlow model from configuration: {config}' ) __SCREAMING_SNAKE_CASE = model_class(a__ ) # Load weights from tf checkpoint if pytorch_checkpoint_path in aws_config_map.keys(): __SCREAMING_SNAKE_CASE = cached_file( a__ , a__ , force_download=not use_cached_models ) # Load PyTorch checkpoint in tf2 model: __SCREAMING_SNAKE_CASE = load_pytorch_checkpoint_in_tfa_model(a__ , a__ ) if compare_with_pt_model: __SCREAMING_SNAKE_CASE = tf_model(tf_model.dummy_inputs , training=a__ ) # build the network __SCREAMING_SNAKE_CASE = torch.load(a__ , map_location="""cpu""" ) __SCREAMING_SNAKE_CASE = pt_model_class.from_pretrained( pretrained_model_name_or_path=a__ , config=a__ , state_dict=a__ ) with torch.no_grad(): __SCREAMING_SNAKE_CASE = pt_model(**pt_model.dummy_inputs ) __SCREAMING_SNAKE_CASE = pto[0].numpy() __SCREAMING_SNAKE_CASE = tfo[0].numpy() __SCREAMING_SNAKE_CASE = np.amax(np.abs(np_pt - np_tf ) ) print(F'Max absolute difference between models outputs {diff}' ) assert diff <= 2E-2, F'Error, model absolute difference is >2e-2: {diff}' # Save pytorch-model print(F'Save TensorFlow model to {tf_dump_path}' ) tf_model.save_weights(a__ , save_format="""h5""" ) def a__ ( a__ , a__ , a__=None , a__=None , a__=False , a__=False , a__=False , a__=False , ): """simple docstring""" if args_model_type is None: __SCREAMING_SNAKE_CASE = list(MODEL_CLASSES.keys() ) else: __SCREAMING_SNAKE_CASE = [args_model_type] for j, model_type in enumerate(a__ , start=1 ): print("""=""" * 1_00 ) print(F' Converting model type {j}/{len(a__ )}: {model_type}' ) print("""=""" * 1_00 ) if model_type not in MODEL_CLASSES: raise ValueError(F'Unrecognized model type {model_type}, should be one of {list(MODEL_CLASSES.keys() )}.' ) __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = MODEL_CLASSES[model_type] if model_shortcut_names_or_path is None: __SCREAMING_SNAKE_CASE = list(aws_model_maps.keys() ) if config_shortcut_names_or_path is None: __SCREAMING_SNAKE_CASE = model_shortcut_names_or_path for i, (model_shortcut_name, config_shortcut_name) in enumerate( zip(a__ , a__ ) , start=1 ): print("""-""" * 1_00 ) if "-squad" in model_shortcut_name or "-mrpc" in model_shortcut_name or "-mnli" in model_shortcut_name: if not only_convert_finetuned_models: print(F' Skipping finetuned checkpoint {model_shortcut_name}' ) continue __SCREAMING_SNAKE_CASE = model_shortcut_name elif only_convert_finetuned_models: print(F' Skipping not finetuned checkpoint {model_shortcut_name}' ) continue print( F' Converting checkpoint {i}/{len(a__ )}: {model_shortcut_name} - model_type {model_type}' ) print("""-""" * 1_00 ) if config_shortcut_name in aws_config_map: __SCREAMING_SNAKE_CASE = cached_file(a__ , a__ , force_download=not use_cached_models ) else: __SCREAMING_SNAKE_CASE = config_shortcut_name if model_shortcut_name in aws_model_maps: __SCREAMING_SNAKE_CASE = cached_file(a__ , a__ , force_download=not use_cached_models ) else: __SCREAMING_SNAKE_CASE = model_shortcut_name if os.path.isfile(a__ ): __SCREAMING_SNAKE_CASE = """converted_model""" convert_pt_checkpoint_to_tf( model_type=a__ , pytorch_checkpoint_path=a__ , config_file=a__ , tf_dump_path=os.path.join(a__ , model_shortcut_name + """-tf_model.h5""" ) , compare_with_pt_model=a__ , ) if remove_cached_files: os.remove(a__ ) os.remove(a__ ) if __name__ == "__main__": UpperCAmelCase : List[str] = argparse.ArgumentParser() # Required parameters parser.add_argument( '--tf_dump_path', default=None, type=str, required=True, help='Path to the output Tensorflow dump file.' ) parser.add_argument( '--model_type', default=None, type=str, help=( f"""Model type selected in the list of {list(MODEL_CLASSES.keys())}. If not given, will download and """ 'convert all the models from AWS.' ), ) parser.add_argument( '--pytorch_checkpoint_path', default=None, type=str, help=( 'Path to the PyTorch checkpoint path or shortcut name to download from AWS. ' 'If not given, will download and convert all the checkpoints from AWS.' ), ) parser.add_argument( '--config_file', default=None, type=str, help=( 'The config json file corresponding to the pre-trained model. \n' 'This specifies the model architecture. If not given and ' '--pytorch_checkpoint_path is not given or is a shortcut name ' 'use the configuration associated to the shortcut name on the AWS' ), ) parser.add_argument( '--compare_with_pt_model', action='store_true', help='Compare Tensorflow and PyTorch model predictions.' ) parser.add_argument( '--use_cached_models', action='store_true', help='Use cached models if possible instead of updating to latest checkpoint versions.', ) parser.add_argument( '--remove_cached_files', action='store_true', help='Remove pytorch models after conversion (save memory when converting in batches).', ) parser.add_argument('--only_convert_finetuned_models', action='store_true', help='Only convert finetuned models.') UpperCAmelCase : List[Any] = parser.parse_args() # if args.pytorch_checkpoint_path is not None: # convert_pt_checkpoint_to_tf(args.model_type.lower(), # args.pytorch_checkpoint_path, # args.config_file if args.config_file is not None else args.pytorch_checkpoint_path, # args.tf_dump_path, # compare_with_pt_model=args.compare_with_pt_model, # use_cached_models=args.use_cached_models) # else: convert_all_pt_checkpoints_to_tf( args.model_type.lower() if args.model_type is not None else None, args.tf_dump_path, model_shortcut_names_or_path=[args.pytorch_checkpoint_path] if args.pytorch_checkpoint_path is not None else None, config_shortcut_names_or_path=[args.config_file] if args.config_file is not None else None, compare_with_pt_model=args.compare_with_pt_model, use_cached_models=args.use_cached_models, remove_cached_files=args.remove_cached_files, only_convert_finetuned_models=args.only_convert_finetuned_models, )
267
0
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available lowerCAmelCase__ : Tuple ={'''configuration_sew''': ['''SEW_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''SEWConfig''']} try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase__ : Tuple =[ '''SEW_PRETRAINED_MODEL_ARCHIVE_LIST''', '''SEWForCTC''', '''SEWForSequenceClassification''', '''SEWModel''', '''SEWPreTrainedModel''', ] if TYPE_CHECKING: from .configuration_sew import SEW_PRETRAINED_CONFIG_ARCHIVE_MAP, SEWConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_sew import ( SEW_PRETRAINED_MODEL_ARCHIVE_LIST, SEWForCTC, SEWForSequenceClassification, SEWModel, SEWPreTrainedModel, ) else: import sys lowerCAmelCase__ : Optional[Any] =_LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
361
from __future__ import annotations from collections.abc import Generator def __lowercase ( ) -> Generator[int, None, None]: __SCREAMING_SNAKE_CASE = {} __SCREAMING_SNAKE_CASE = 2 while True: __SCREAMING_SNAKE_CASE = factor_map.pop(a__ , a__ ) if factor: __SCREAMING_SNAKE_CASE = factor + prime while x in factor_map: x += factor __SCREAMING_SNAKE_CASE = factor else: __SCREAMING_SNAKE_CASE = prime yield prime prime += 1 def __lowercase ( a__ = 1E10 ) -> int: __SCREAMING_SNAKE_CASE = sieve() __SCREAMING_SNAKE_CASE = 1 while True: __SCREAMING_SNAKE_CASE = next(a__ ) if (2 * prime * n) > limit: return n # Ignore the next prime as the reminder will be 2. next(a__ ) n += 2 if __name__ == "__main__": print(solution())
118
0
'''simple docstring''' from unittest import TestCase from datasets import Dataset from minhash_deduplication import deduplicate_dataset, make_duplicate_clusters def UpperCAmelCase_ ( ) -> Any: '''simple docstring''' _UpperCAmelCase = { "repo_name": ["test_repo1", "test_repo2", "test_repo3"], "path": ["test_1.py", "test_2.py", "unit_test.py"], "content": ["a " * 20, "a " * 30, "b " * 7], } _UpperCAmelCase = Dataset.from_dict(__lowercase ) return dataset class A_ ( lowerCAmelCase_ ): def lowercase ( self : Union[str, Any] ): _UpperCAmelCase = get_dataset() _UpperCAmelCase = make_duplicate_clusters(snake_case_ , 0.8_5 ) self.assertEqual(len(duplicate_clusters[0] ) , 2 ) def lowercase ( self : int ): _UpperCAmelCase = get_dataset() _UpperCAmelCase , _UpperCAmelCase = deduplicate_dataset(snake_case_ ) self.assertEqual(len(snake_case_ ) , 2 ) print(snake_case_ ) self.assertEqual(duplicate_clusters[0][0]["copies"] , 2 ) self.assertEqual(duplicate_clusters[0][0]["is_extreme"] , snake_case_ )
22
'''simple docstring''' import warnings from ...utils import is_sklearn_available, requires_backends if is_sklearn_available(): from scipy.stats import pearsonr, spearmanr from sklearn.metrics import fa_score, matthews_corrcoef __SCREAMING_SNAKE_CASE :List[str] = ( '''This metric will be removed from the library soon, metrics should be handled with the 🤗 Evaluate ''' '''library. You can have a look at this example script for pointers: ''' '''https://github.com/huggingface/transformers/blob/main/examples/pytorch/text-classification/run_glue.py''' ) def UpperCAmelCase_ ( __lowercase : Any , __lowercase : Tuple ) -> int: '''simple docstring''' warnings.warn(__lowercase , __lowercase ) requires_backends(__lowercase , "sklearn" ) return (preds == labels).mean() def UpperCAmelCase_ ( __lowercase : int , __lowercase : str ) -> Optional[Any]: '''simple docstring''' warnings.warn(__lowercase , __lowercase ) requires_backends(__lowercase , "sklearn" ) _UpperCAmelCase = simple_accuracy(__lowercase , __lowercase ) _UpperCAmelCase = fa_score(y_true=__lowercase , y_pred=__lowercase ) return { "acc": acc, "f1": fa, "acc_and_f1": (acc + fa) / 2, } def UpperCAmelCase_ ( __lowercase : Optional[int] , __lowercase : List[str] ) -> List[Any]: '''simple docstring''' warnings.warn(__lowercase , __lowercase ) requires_backends(__lowercase , "sklearn" ) _UpperCAmelCase = pearsonr(__lowercase , __lowercase )[0] _UpperCAmelCase = spearmanr(__lowercase , __lowercase )[0] return { "pearson": pearson_corr, "spearmanr": spearman_corr, "corr": (pearson_corr + spearman_corr) / 2, } def UpperCAmelCase_ ( __lowercase : Optional[Any] , __lowercase : str , __lowercase : str ) -> Tuple: '''simple docstring''' warnings.warn(__lowercase , __lowercase ) requires_backends(__lowercase , "sklearn" ) assert len(__lowercase ) == len(__lowercase ), f'Predictions and labels have mismatched lengths {len(__lowercase )} and {len(__lowercase )}' if task_name == "cola": return {"mcc": matthews_corrcoef(__lowercase , __lowercase )} elif task_name == "sst-2": return {"acc": simple_accuracy(__lowercase , __lowercase )} elif task_name == "mrpc": return acc_and_fa(__lowercase , __lowercase ) elif task_name == "sts-b": return pearson_and_spearman(__lowercase , __lowercase ) elif task_name == "qqp": return acc_and_fa(__lowercase , __lowercase ) elif task_name == "mnli": return {"mnli/acc": simple_accuracy(__lowercase , __lowercase )} elif task_name == "mnli-mm": return {"mnli-mm/acc": simple_accuracy(__lowercase , __lowercase )} elif task_name == "qnli": return {"acc": simple_accuracy(__lowercase , __lowercase )} elif task_name == "rte": return {"acc": simple_accuracy(__lowercase , __lowercase )} elif task_name == "wnli": return {"acc": simple_accuracy(__lowercase , __lowercase )} elif task_name == "hans": return {"acc": simple_accuracy(__lowercase , __lowercase )} else: raise KeyError(__lowercase ) def UpperCAmelCase_ ( __lowercase : List[Any] , __lowercase : Dict , __lowercase : str ) -> Union[str, Any]: '''simple docstring''' warnings.warn(__lowercase , __lowercase ) requires_backends(__lowercase , "sklearn" ) if len(__lowercase ) != len(__lowercase ): raise ValueError(f'Predictions and labels have mismatched lengths {len(__lowercase )} and {len(__lowercase )}' ) if task_name == "xnli": return {"acc": simple_accuracy(__lowercase , __lowercase )} else: raise KeyError(__lowercase )
22
1
'''simple docstring''' import itertools import os from collections import Counter, defaultdict from concurrent.futures import ThreadPoolExecutor, as_completed import numpy as np import datasets from .execute import check_correctness lowercase = """\ @misc{chen2021evaluating, title={Evaluating Large Language Models Trained on Code}, author={Mark Chen and Jerry Tworek and Heewoo Jun and Qiming Yuan \ and Henrique Ponde de Oliveira Pinto and Jared Kaplan and Harri Edwards \ and Yuri Burda and Nicholas Joseph and Greg Brockman and Alex Ray \ and Raul Puri and Gretchen Krueger and Michael Petrov and Heidy Khlaaf \ and Girish Sastry and Pamela Mishkin and Brooke Chan and Scott Gray \ and Nick Ryder and Mikhail Pavlov and Alethea Power and Lukasz Kaiser \ and Mohammad Bavarian and Clemens Winter and Philippe Tillet \ and Felipe Petroski Such and Dave Cummings and Matthias Plappert \ and Fotios Chantzis and Elizabeth Barnes and Ariel Herbert-Voss \ and William Hebgen Guss and Alex Nichol and Alex Paino and Nikolas Tezak \ and Jie Tang and Igor Babuschkin and Suchir Balaji and Shantanu Jain \ and William Saunders and Christopher Hesse and Andrew N. Carr \ and Jan Leike and Josh Achiam and Vedant Misra and Evan Morikawa \ and Alec Radford and Matthew Knight and Miles Brundage and Mira Murati \ and Katie Mayer and Peter Welinder and Bob McGrew and Dario Amodei \ and Sam McCandlish and Ilya Sutskever and Wojciech Zaremba}, year={2021}, eprint={2107.03374}, archivePrefix={arXiv}, primaryClass={cs.LG} } """ lowercase = """\ This metric implements the evaluation harness for the HumanEval problem solving dataset described in the paper \"Evaluating Large Language Models Trained on Code\" (https://arxiv.org/abs/2107.03374). """ lowercase = """ Calculates how good are predictions given some references, using certain scores Args: predictions: list of candidates to evaluate. Each candidates should be a list of strings with several code candidates to solve the problem. references: a list with a test for each prediction. Each test should evaluate the correctness of a code candidate. k: number of code candidates to consider in the evaluation (Default: [1, 10, 100]) num_workers: number of workers used to evaluate the canidate programs (Default: 4). timeout: Returns: pass_at_k: dict with pass rates for each k results: dict with granular results of each unittest Examples: >>> code_eval = datasets.load_metric(\"code_eval\") >>> test_cases = [\"assert add(2,3)==5\"] >>> candidates = [[\"def add(a,b): return a*b\", \"def add(a, b): return a+b\"]] >>> pass_at_k, results = code_eval.compute(references=test_cases, predictions=candidates, k=[1, 2]) >>> print(pass_at_k) {'pass@1': 0.5, 'pass@2': 1.0} """ lowercase = """ ################################################################################ !!!WARNING!!! ################################################################################ The \"code_eval\" metric executes untrusted model-generated code in Python. Although it is highly unlikely that model-generated code will do something overtly malicious in response to this test suite, model-generated code may act destructively due to a lack of model capability or alignment. Users are strongly encouraged to sandbox this evaluation suite so that it does not perform destructive actions on their host or network. For more information on how OpenAI sandboxes its code, see the paper \"Evaluating Large Language Models Trained on Code\" (https://arxiv.org/abs/2107.03374). Once you have read this disclaimer and taken appropriate precautions, set the environment variable HF_ALLOW_CODE_EVAL=\"1\". Within Python you can to this with: >>> import os >>> os.environ[\"HF_ALLOW_CODE_EVAL\"] = \"1\" ################################################################################\ """ lowercase = """The MIT License Copyright (c) OpenAI (https://openai.com) Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the \"Software\"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.""" @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION ,_KWARGS_DESCRIPTION ) class __magic_name__ ( datasets.Metric ): def lowerCAmelCase ( self) -> List[str]: '''simple docstring''' return datasets.MetricInfo( # This is the description that will appear on the metrics page. description=_DESCRIPTION , citation=_CITATION , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { 'predictions': datasets.Sequence(datasets.Value('string')), 'references': datasets.Value('string'), }) , homepage='https://github.com/openai/human-eval' , codebase_urls=['https://github.com/openai/human-eval'] , reference_urls=['https://github.com/openai/human-eval'] , license=_LICENSE , ) def lowerCAmelCase ( self , snake_case , snake_case , snake_case=[1, 1_0, 1_0_0] , snake_case=4 , snake_case=3.0) -> Tuple: '''simple docstring''' if os.getenv('HF_ALLOW_CODE_EVAL' , 0) != "1": raise ValueError(_WARNING) if os.name == "nt": raise NotImplementedError('This metric is currently not supported on Windows.') with ThreadPoolExecutor(max_workers=__lowerCAmelCase) as executor: _UpperCAmelCase : Optional[int] =[] _UpperCAmelCase : Dict =Counter() _UpperCAmelCase : Optional[Any] =0 _UpperCAmelCase : List[str] =defaultdict(__lowerCAmelCase) for task_id, (candidates, test_case) in enumerate(zip(__lowerCAmelCase , __lowerCAmelCase)): for candidate in candidates: _UpperCAmelCase : str =candidate + '\n' + test_case _UpperCAmelCase : Tuple =(test_program, timeout, task_id, completion_id[task_id]) _UpperCAmelCase : Union[str, Any] =executor.submit(__lowerCAmelCase , *__lowerCAmelCase) futures.append(__lowerCAmelCase) completion_id[task_id] += 1 n_samples += 1 for future in as_completed(__lowerCAmelCase): _UpperCAmelCase : Optional[Any] =future.result() results[result["task_id"]].append((result['completion_id'], result)) _UpperCAmelCase , _UpperCAmelCase : Tuple =[], [] for result in results.values(): result.sort() _UpperCAmelCase : Dict =[r[1]['passed'] for r in result] total.append(len(__lowerCAmelCase)) correct.append(sum(__lowerCAmelCase)) _UpperCAmelCase : Optional[int] =np.array(__lowerCAmelCase) _UpperCAmelCase : Tuple =np.array(__lowerCAmelCase) _UpperCAmelCase : str =k _UpperCAmelCase : Union[str, Any] ={f"pass@{k}": estimate_pass_at_k(__lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase).mean() for k in ks if (total >= k).all()} return pass_at_k, results def lowerCamelCase__ ( __lowerCamelCase : Optional[int] , __lowerCamelCase : Dict , __lowerCamelCase : Union[str, Any] ): '''simple docstring''' def estimator(__lowerCamelCase : int , __lowerCamelCase : int , __lowerCamelCase : int ) -> float: if n - c < k: return 1.0 return 1.0 - np.prod(1.0 - k / np.arange(n - c + 1 , n + 1 ) ) if isinstance(lowerCAmelCase__ , lowerCAmelCase__ ): _UpperCAmelCase : str =itertools.repeat(lowerCAmelCase__ , len(lowerCAmelCase__ ) ) else: assert len(lowerCAmelCase__ ) == len(lowerCAmelCase__ ) _UpperCAmelCase : Optional[int] =iter(lowerCAmelCase__ ) return np.array([estimator(int(lowerCAmelCase__ ) , int(lowerCAmelCase__ ) , lowerCAmelCase__ ) for n, c in zip(lowerCAmelCase__ , lowerCAmelCase__ )] )
360
'''simple docstring''' lowercase =[0, 2, 4, 6, 8] lowercase =[1, 3, 5, 7, 9] def lowerCamelCase__ ( __lowerCamelCase : int , __lowerCamelCase : int , __lowerCamelCase : list[int] , __lowerCamelCase : int ): '''simple docstring''' if remaining_length == 0: if digits[0] == 0 or digits[-1] == 0: return 0 for i in range(length // 2 - 1 , -1 , -1 ): remainder += digits[i] + digits[length - i - 1] if remainder % 2 == 0: return 0 remainder //= 1_0 return 1 if remaining_length == 1: if remainder % 2 == 0: return 0 _UpperCAmelCase : Union[str, Any] =0 for digit in range(1_0 ): _UpperCAmelCase : str =digit result += reversible_numbers( 0 , (remainder + 2 * digit) // 1_0 , __lowerCamelCase , __lowerCamelCase ) return result _UpperCAmelCase : Optional[Any] =0 for digita in range(1_0 ): _UpperCAmelCase : Any =digita if (remainder + digita) % 2 == 0: _UpperCAmelCase : Optional[int] =ODD_DIGITS else: _UpperCAmelCase : Union[str, Any] =EVEN_DIGITS for digita in other_parity_digits: _UpperCAmelCase : int =digita result += reversible_numbers( remaining_length - 2 , (remainder + digita + digita) // 1_0 , __lowerCamelCase , __lowerCamelCase , ) return result def lowerCamelCase__ ( __lowerCamelCase : int = 9 ): '''simple docstring''' _UpperCAmelCase : Optional[int] =0 for length in range(1 , max_power + 1 ): result += reversible_numbers(__lowerCamelCase , 0 , [0] * length , __lowerCamelCase ) return result if __name__ == "__main__": print(F"""{solution() = }""")
242
0
import os import numpy import onnx def snake_case( __magic_name__ , __magic_name__ ) -> List[Any]: '''simple docstring''' lowercase : List[str] = a.name lowercase : Any = b.name lowercase : str = '''''' lowercase : int = '''''' lowercase : Optional[Any] = a == b lowercase : Optional[Any] = name_a lowercase : List[Any] = name_b return res def snake_case( __magic_name__ , __magic_name__ , __magic_name__ ) -> Tuple: '''simple docstring''' for i, input_name in enumerate(node_proto.input ): if input_name == name: node_proto.input.insert(__magic_name__ , __magic_name__ ) node_proto.input.pop(i + 1 ) if node_proto.op_type == "If": _graph_replace_input_with(node_proto.attribute[0].g , __magic_name__ , __magic_name__ ) _graph_replace_input_with(node_proto.attribute[1].g , __magic_name__ , __magic_name__ ) if node_proto.op_type == "Loop": _graph_replace_input_with(node_proto.attribute[0].g , __magic_name__ , __magic_name__ ) def snake_case( __magic_name__ , __magic_name__ , __magic_name__ ) -> Union[str, Any]: '''simple docstring''' for n in graph_proto.node: _node_replace_input_with(__magic_name__ , __magic_name__ , __magic_name__ ) def snake_case( __magic_name__ , __magic_name__ , __magic_name__ ) -> Tuple: '''simple docstring''' lowercase : Dict = list(model.graph.initializer ) lowercase : Optional[Any] = list(model_without_ext.graph.initializer ) for i, ref_i in ind_to_replace: assert inits_with_data[i].name == inits[i].name assert inits_with_data[ref_i].name == inits[ref_i].name assert i > ref_i lowercase : List[str] = inits[i].name lowercase : List[Any] = inits[ref_i].name model_without_ext.graph.initializer.remove(inits[i] ) # for n in model.graph.node: _graph_replace_input_with(model_without_ext.graph , __magic_name__ , __magic_name__ ) def snake_case( __magic_name__ ) -> Tuple: '''simple docstring''' lowercase : Union[str, Any] = os.path.dirname(__magic_name__ ) lowercase : Union[str, Any] = os.path.basename(__magic_name__ ) lowercase : int = onnx.load(os.path.join(__magic_name__ , __magic_name__ ) ) lowercase : Tuple = list(model.graph.initializer ) lowercase : Tuple = set() lowercase : Any = {} lowercase : List[Any] = [] lowercase : str = 0 for i in range(len(__magic_name__ ) ): if i in dup_set: continue for j in range(i + 1 , len(__magic_name__ ) ): if j in dup_set: continue if _is_equal_tensor_proto(inits[i] , inits[j] ): dup_set.add(__magic_name__ ) dup_set.add(__magic_name__ ) lowercase : Union[str, Any] = inits[j].data_type lowercase : Optional[int] = numpy.prod(inits[j].dims ) if dtype == 1: mem_size *= 4 elif dtype == 6: mem_size *= 4 elif dtype == 7 or dtype == 11: mem_size *= 8 else: print('''unexpected data type: ''' , __magic_name__ ) total_reduced_size += mem_size lowercase : str = inits[i].name lowercase : Optional[int] = inits[j].name if name_i in dup_map: dup_map[name_i].append(__magic_name__ ) else: lowercase : int = [name_j] ind_to_replace.append((j, i) ) print('''total reduced size: ''' , total_reduced_size / 10_24 / 10_24 / 10_24 , '''GB''' ) lowercase : str = sorted(__magic_name__ ) _remove_dup_initializers_from_model(__magic_name__ , __magic_name__ , __magic_name__ ) lowercase : int = '''optimized_''' + model_file_name lowercase : Union[str, Any] = os.path.join(__magic_name__ , __magic_name__ ) onnx.save(__magic_name__ , __magic_name__ ) return new_model
308
from __future__ import annotations from typing import Any def snake_case( __magic_name__ ) -> None: '''simple docstring''' create_state_space_tree(__magic_name__ , [] , 0 ) def snake_case( __magic_name__ , __magic_name__ , __magic_name__ ) -> None: '''simple docstring''' if index == len(__magic_name__ ): print(__magic_name__ ) return create_state_space_tree(__magic_name__ , __magic_name__ , index + 1 ) current_subsequence.append(sequence[index] ) create_state_space_tree(__magic_name__ , __magic_name__ , index + 1 ) current_subsequence.pop() if __name__ == "__main__": lowerCAmelCase_ = [3, 1, 2, 4] generate_all_subsequences(seq) seq.clear() seq.extend(['A', 'B', 'C']) generate_all_subsequences(seq)
308
1
"""simple docstring""" from __future__ import annotations SCREAMING_SNAKE_CASE_ : int = '''#''' class a : def __init__( self ): """simple docstring""" lowerCAmelCase = {} def UpperCamelCase__ ( self , _snake_case ): """simple docstring""" lowerCAmelCase = self._trie for char in text: if char not in trie: lowerCAmelCase = {} lowerCAmelCase = trie[char] lowerCAmelCase = True def UpperCamelCase__ ( self , _snake_case ): """simple docstring""" lowerCAmelCase = self._trie for char in prefix: if char in trie: lowerCAmelCase = trie[char] else: return [] return self._elements(_snake_case ) def UpperCamelCase__ ( self , _snake_case ): """simple docstring""" lowerCAmelCase = [] for c, v in d.items(): lowerCAmelCase = [' '] if c == END else [(c + s) for s in self._elements(_snake_case )] result.extend(_snake_case ) return tuple(_snake_case ) SCREAMING_SNAKE_CASE_ : Dict = Trie() SCREAMING_SNAKE_CASE_ : List[str] = ('''depart''', '''detergent''', '''daring''', '''dog''', '''deer''', '''deal''') for word in words: trie.insert_word(word) def _SCREAMING_SNAKE_CASE (_UpperCAmelCase : str ): lowerCAmelCase = trie.find_word(_UpperCAmelCase ) return tuple(string + word for word in suffixes ) def _SCREAMING_SNAKE_CASE (): print(autocomplete_using_trie('de' ) ) if __name__ == "__main__": import doctest doctest.testmod() main()
357
"""simple docstring""" __UpperCamelCase : Dict = {0: [2, 3], 1: [0], 2: [1], 3: [4], 4: []} __UpperCamelCase : str = {0: [1, 2, 3], 1: [2], 2: [0], 3: [4], 4: [5], 5: [3]} def _SCREAMING_SNAKE_CASE (_UpperCAmelCase : dict[int, list[int]] , _UpperCAmelCase : int , _UpperCAmelCase : list[bool] ): lowerCAmelCase = True lowerCAmelCase = [] for neighbour in graph[vert]: if not visited[neighbour]: order += topology_sort(_UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase ) order.append(_UpperCAmelCase ) return order def _SCREAMING_SNAKE_CASE (_UpperCAmelCase : dict[int, list[int]] , _UpperCAmelCase : int , _UpperCAmelCase : list[bool] ): lowerCAmelCase = True lowerCAmelCase = [vert] for neighbour in reversed_graph[vert]: if not visited[neighbour]: component += find_components(_UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase ) return component def _SCREAMING_SNAKE_CASE (_UpperCAmelCase : dict[int, list[int]] ): lowerCAmelCase = len(_UpperCAmelCase ) * [False] lowerCAmelCase = {vert: [] for vert in range(len(_UpperCAmelCase ) )} for vert, neighbours in graph.items(): for neighbour in neighbours: reversed_graph[neighbour].append(_UpperCAmelCase ) lowerCAmelCase = [] for i, was_visited in enumerate(_UpperCAmelCase ): if not was_visited: order += topology_sort(_UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase ) lowerCAmelCase = [] lowerCAmelCase = len(_UpperCAmelCase ) * [False] for i in range(len(_UpperCAmelCase ) ): lowerCAmelCase = order[len(_UpperCAmelCase ) - i - 1] if not visited[vert]: lowerCAmelCase = find_components(_UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase ) components_list.append(_UpperCAmelCase ) return components_list
309
0
"""simple docstring""" import tempfile import unittest import numpy as np import transformers from transformers import GPTaTokenizer, GPTJConfig, is_flax_available, is_torch_available from transformers.testing_utils import is_pt_flax_cross_test, require_flax, tooslow from ...generation.test_flax_utils import FlaxGenerationTesterMixin from ...test_modeling_flax_common import FlaxModelTesterMixin, ids_tensor, random_attention_mask if is_flax_available(): import jax import jax.numpy as jnp from transformers.modeling_flax_pytorch_utils import ( convert_pytorch_state_dict_to_flax, load_flax_weights_in_pytorch_model, ) from transformers.models.gptj.modeling_flax_gptj import FlaxGPTJForCausalLM, FlaxGPTJModel if is_torch_available(): import torch class _UpperCAmelCase : '''simple docstring''' def __init__(self , a_ , a_=14 , a_=7 , a_=True , a_=True , a_=False , a_=True , a_=99 , a_=32 , a_=4 , a_=4 , a_=4 , a_=37 , a_="gelu" , a_=0.1 , a_=0.1 , a_=5_12 , a_=0.02 , ): '''simple docstring''' __snake_case : Any = parent __snake_case : List[str] = batch_size __snake_case : Union[str, Any] = seq_length __snake_case : Tuple = is_training __snake_case : List[Any] = use_input_mask __snake_case : List[Any] = use_token_type_ids __snake_case : Union[str, Any] = use_labels __snake_case : Union[str, Any] = vocab_size __snake_case : Dict = hidden_size __snake_case : int = rotary_dim __snake_case : Any = num_hidden_layers __snake_case : Union[str, Any] = num_attention_heads __snake_case : Dict = intermediate_size __snake_case : Union[str, Any] = hidden_act __snake_case : List[str] = hidden_dropout_prob __snake_case : Any = attention_probs_dropout_prob __snake_case : Optional[int] = max_position_embeddings __snake_case : Tuple = initializer_range __snake_case : int = None __snake_case : Optional[Any] = vocab_size - 1 __snake_case : List[str] = vocab_size - 1 __snake_case : Tuple = vocab_size - 1 def SCREAMING_SNAKE_CASE (self ): '''simple docstring''' __snake_case : Dict = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) __snake_case : int = None if self.use_input_mask: __snake_case : Union[str, Any] = random_attention_mask([self.batch_size, self.seq_length] ) __snake_case : int = GPTJConfig( vocab_size=self.vocab_size , n_embd=self.hidden_size , n_layer=self.num_hidden_layers , n_head=self.num_attention_heads , n_positions=self.max_position_embeddings , use_cache=a_ , bos_token_id=self.bos_token_id , eos_token_id=self.eos_token_id , pad_token_id=self.pad_token_id , rotary_dim=self.rotary_dim , ) return (config, input_ids, input_mask) def SCREAMING_SNAKE_CASE (self ): '''simple docstring''' __snake_case : Optional[Any] = self.prepare_config_and_inputs() __snake_case , __snake_case , __snake_case : int = config_and_inputs __snake_case : Tuple = {'''input_ids''': input_ids, '''attention_mask''': attention_mask} return config, inputs_dict def SCREAMING_SNAKE_CASE (self , a_ , a_ , a_ , a_ ): '''simple docstring''' __snake_case : Any = 20 __snake_case : int = model_class_name(a_ ) __snake_case : Tuple = model.init_cache(input_ids.shape[0] , a_ ) __snake_case : Dict = jnp.ones((input_ids.shape[0], max_decoder_length) , dtype='''i4''' ) __snake_case : List[Any] = jnp.broadcast_to( jnp.arange(input_ids.shape[-1] - 1 )[None, :] , (input_ids.shape[0], input_ids.shape[-1] - 1) ) __snake_case : Optional[Any] = model( input_ids[:, :-1] , attention_mask=a_ , past_key_values=a_ , position_ids=a_ , ) __snake_case : List[str] = jnp.array(input_ids.shape[0] * [[input_ids.shape[-1] - 1]] , dtype='''i4''' ) __snake_case : List[str] = model( input_ids[:, -1:] , attention_mask=a_ , past_key_values=outputs_cache.past_key_values , position_ids=a_ , ) __snake_case : Union[str, Any] = model(a_ ) __snake_case : Any = np.max(np.abs((outputs_cache_next[0][:, -1, :5] - outputs[0][:, -1, :5]) ) ) self.parent.assertTrue(diff < 1E-3 , msg=f"""Max diff is {diff}""" ) def SCREAMING_SNAKE_CASE (self , a_ , a_ , a_ , a_ ): '''simple docstring''' __snake_case : List[str] = 20 __snake_case : str = model_class_name(a_ ) __snake_case : Tuple = jnp.concatenate( [attention_mask, jnp.zeros((attention_mask.shape[0], max_decoder_length - attention_mask.shape[1]) )] , axis=-1 , ) __snake_case : Union[str, Any] = model.init_cache(input_ids.shape[0] , a_ ) __snake_case : Any = jnp.broadcast_to( jnp.arange(input_ids.shape[-1] - 1 )[None, :] , (input_ids.shape[0], input_ids.shape[-1] - 1) ) __snake_case : List[str] = model( input_ids[:, :-1] , attention_mask=a_ , past_key_values=a_ , position_ids=a_ , ) __snake_case : Union[str, Any] = jnp.array(input_ids.shape[0] * [[input_ids.shape[-1] - 1]] , dtype='''i4''' ) __snake_case : Optional[int] = model( input_ids[:, -1:] , past_key_values=outputs_cache.past_key_values , attention_mask=a_ , position_ids=a_ , ) __snake_case : Optional[int] = model(a_ , attention_mask=a_ ) __snake_case : List[Any] = np.max(np.abs((outputs_cache_next[0][:, -1, :5] - outputs[0][:, -1, :5]) ) ) self.parent.assertTrue(diff < 1E-3 , msg=f"""Max diff is {diff}""" ) @require_flax class _UpperCAmelCase ( __snake_case, __snake_case, unittest.TestCase ): '''simple docstring''' lowerCamelCase__ =(FlaxGPTJModel, FlaxGPTJForCausalLM) if is_flax_available() else () lowerCamelCase__ =(FlaxGPTJForCausalLM,) if is_flax_available() else () def SCREAMING_SNAKE_CASE (self ): '''simple docstring''' __snake_case : str = FlaxGPTJModelTester(self ) def SCREAMING_SNAKE_CASE (self ): '''simple docstring''' for model_class_name in self.all_model_classes: __snake_case , __snake_case , __snake_case : Union[str, Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.check_use_cache_forward(a_ , a_ , a_ , a_ ) def SCREAMING_SNAKE_CASE (self ): '''simple docstring''' for model_class_name in self.all_model_classes: __snake_case , __snake_case , __snake_case : List[Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.check_use_cache_forward_with_attn_mask( a_ , a_ , a_ , a_ ) @tooslow def SCREAMING_SNAKE_CASE (self ): '''simple docstring''' __snake_case : Tuple = GPTaTokenizer.from_pretrained('''gpt2''' , pad_token='''<|endoftext|>''' , padding_side='''left''' ) __snake_case : str = tokenizer(['''Hello this is a long string''', '''Hey'''] , return_tensors='''np''' , padding=a_ , truncation=a_ ) __snake_case : Optional[int] = FlaxGPTJForCausalLM.from_pretrained('''EleutherAI/gpt-j-6B''' ) __snake_case : Any = False __snake_case : List[str] = model.config.eos_token_id __snake_case : Dict = jax.jit(model.generate ) __snake_case : Dict = jit_generate( inputs['''input_ids'''] , attention_mask=inputs['''attention_mask'''] , pad_token_id=tokenizer.pad_token_id ).sequences __snake_case : Union[str, Any] = tokenizer.batch_decode(a_ , skip_special_tokens=a_ ) __snake_case : int = [ '''Hello this is a long string of text.\n\nI\'m trying to get the text of the''', '''Hey, I\'m a little late to the party. I\'m going to''', ] self.assertListEqual(a_ , a_ ) @is_pt_flax_cross_test def SCREAMING_SNAKE_CASE (self ): '''simple docstring''' __snake_case , __snake_case : Tuple = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: with self.subTest(model_class.__name__ ): # prepare inputs __snake_case : Tuple = self._prepare_for_class(a_ , a_ ) __snake_case : str = {k: torch.tensor(v.tolist() ) for k, v in prepared_inputs_dict.items()} # load corresponding PyTorch class __snake_case : int = model_class.__name__[4:] # Skip the "Flax" at the beginning __snake_case : List[Any] = getattr(a_ , a_ ) __snake_case , __snake_case : Dict = pt_inputs['''input_ids'''].shape __snake_case : Optional[Any] = np.random.randint(0 , seq_length - 1 , size=(batch_size,) ) for batch_idx, start_index in enumerate(a_ ): __snake_case : List[str] = 0 __snake_case : Tuple = 1 __snake_case : int = 0 __snake_case : Optional[Any] = 1 __snake_case : Any = pt_model_class(a_ ).eval() __snake_case : int = model_class(a_ , dtype=jnp.floataa ) __snake_case : Dict = convert_pytorch_state_dict_to_flax(pt_model.state_dict() , a_ ) __snake_case : str = fx_state with torch.no_grad(): __snake_case : int = pt_model(**a_ ).to_tuple() __snake_case : str = fx_model(**a_ ).to_tuple() self.assertEqual(len(a_ ) , len(a_ ) , '''Output lengths differ between Flax and PyTorch''' ) for fx_output, pt_output in zip(a_ , a_ ): self.assert_almost_equals(fx_output[:, -1] , pt_output[:, -1].numpy() , 4E-2 ) with tempfile.TemporaryDirectory() as tmpdirname: pt_model.save_pretrained(a_ ) __snake_case : List[str] = model_class.from_pretrained(a_ , from_pt=a_ ) __snake_case : int = fx_model_loaded(**a_ ).to_tuple() self.assertEqual( len(a_ ) , len(a_ ) , '''Output lengths differ between Flax and PyTorch''' ) for fx_output_loaded, pt_output in zip(a_ , a_ ): self.assert_almost_equals(fx_output_loaded[:, -1] , pt_output[:, -1].numpy() , 4E-2 ) @is_pt_flax_cross_test def SCREAMING_SNAKE_CASE (self ): '''simple docstring''' __snake_case , __snake_case : int = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: with self.subTest(model_class.__name__ ): # prepare inputs __snake_case : List[Any] = self._prepare_for_class(a_ , a_ ) __snake_case : Dict = {k: torch.tensor(v.tolist() ) for k, v in prepared_inputs_dict.items()} # load corresponding PyTorch class __snake_case : str = model_class.__name__[4:] # Skip the "Flax" at the beginning __snake_case : List[Any] = getattr(a_ , a_ ) __snake_case : List[str] = pt_model_class(a_ ).eval() __snake_case : int = model_class(a_ , dtype=jnp.floataa ) __snake_case : Dict = load_flax_weights_in_pytorch_model(a_ , fx_model.params ) __snake_case , __snake_case : Union[str, Any] = pt_inputs['''input_ids'''].shape __snake_case : Optional[Any] = np.random.randint(0 , seq_length - 1 , size=(batch_size,) ) for batch_idx, start_index in enumerate(a_ ): __snake_case : Any = 0 __snake_case : int = 1 __snake_case : int = 0 __snake_case : int = 1 # make sure weights are tied in PyTorch pt_model.tie_weights() with torch.no_grad(): __snake_case : List[Any] = pt_model(**a_ ).to_tuple() __snake_case : List[str] = fx_model(**a_ ).to_tuple() self.assertEqual(len(a_ ) , len(a_ ) , '''Output lengths differ between Flax and PyTorch''' ) for fx_output, pt_output in zip(a_ , a_ ): self.assert_almost_equals(fx_output[:, -1] , pt_output[:, -1].numpy() , 4E-2 ) with tempfile.TemporaryDirectory() as tmpdirname: fx_model.save_pretrained(a_ ) __snake_case : Any = pt_model_class.from_pretrained(a_ , from_flax=a_ ) with torch.no_grad(): __snake_case : Union[str, Any] = pt_model_loaded(**a_ ).to_tuple() self.assertEqual( len(a_ ) , len(a_ ) , '''Output lengths differ between Flax and PyTorch''' ) for fx_output, pt_output in zip(a_ , a_ ): self.assert_almost_equals(fx_output[:, -1] , pt_output[:, -1].numpy() , 4E-2 ) @tooslow def SCREAMING_SNAKE_CASE (self ): '''simple docstring''' for model_class_name in self.all_model_classes: __snake_case : List[str] = model_class_name.from_pretrained('''EleutherAI/gpt-j-6B''' ) __snake_case : int = model(np.ones((1, 1) ) ) self.assertIsNotNone(a_ )
102
def _a ( lowerCamelCase: int = 2_00 ) -> int: '''simple docstring''' __A = [1, 2, 5, 10, 20, 50, 1_00, 2_00] __A = [0] * (pence + 1) __A = 1 # base case: 1 way to make 0 pence for coin in coins: for i in range(lowerCamelCase , pence + 1 , 1 ): number_of_ways[i] += number_of_ways[i - coin] return number_of_ways[pence] if __name__ == "__main__": assert solution(200) == 73682
117
0
from collections import defaultdict class __lowerCAmelCase : def __init__( self : Dict , A : int , A : List[Any]) -> Any: """simple docstring""" _UpperCAmelCase = total # total no of tasks (N) # DP table will have a dimension of (2^M)*N # initially all values are set to -1 _UpperCAmelCase = [ [-1 for i in range(total + 1)] for j in range(2 ** len(__A)) ] _UpperCAmelCase = defaultdict(__A) # stores the list of persons for each task # final_mask is used to check if all persons are included by setting all bits # to 1 _UpperCAmelCase = (1 << len(__A)) - 1 def _lowerCamelCase ( self : Tuple , A : List[Any] , A : List[Any]) -> List[str]: """simple docstring""" if mask == self.final_mask: return 1 # if not everyone gets the task and no more tasks are available, return 0 if task_no > self.total_tasks: return 0 # if case already considered if self.dp[mask][task_no] != -1: return self.dp[mask][task_no] # Number of ways when we don't this task in the arrangement _UpperCAmelCase = self.count_ways_until(__A , task_no + 1) # now assign the tasks one by one to all possible persons and recursively # assign for the remaining tasks. if task_no in self.task: for p in self.task[task_no]: # if p is already given a task if mask & (1 << p): continue # assign this task to p and change the mask value. And recursively # assign tasks with the new mask value. total_ways_util += self.count_ways_until(mask | (1 << p) , task_no + 1) # save the value. _UpperCAmelCase = total_ways_util return self.dp[mask][task_no] def _lowerCamelCase ( self : Tuple , A : Union[str, Any]) -> List[str]: """simple docstring""" for i in range(len(__A)): for j in task_performed[i]: self.task[j].append(__A) # call the function to fill the DP table, final answer is stored in dp[0][1] return self.count_ways_until(0 , 1) if __name__ == "__main__": UpperCAmelCase__ = 5 # total no of tasks (the value of N) # the list of tasks that can be done by M persons. UpperCAmelCase__ = [[1, 3, 4], [1, 2, 5], [3, 4]] print( AssignmentUsingBitmask(task_performed, total_tasks).count_no_of_ways( task_performed ) )
361
from __future__ import annotations from itertools import permutations from random import randint from timeit import repeat def A ( ) -> tuple[list[int], int]: '''simple docstring''' _UpperCAmelCase = [randint(-1_000 , 1_000 ) for i in range(10 )] _UpperCAmelCase = randint(-5_000 , 5_000 ) return (arr, r) UpperCAmelCase__ = make_dataset() def A ( _UpperCAmelCase : list[int] , _UpperCAmelCase : int ) -> tuple[int, ...]: '''simple docstring''' for triplet in permutations(_UpperCAmelCase , 3 ): if sum(_UpperCAmelCase ) == target: return tuple(sorted(_UpperCAmelCase ) ) return (0, 0, 0) def A ( _UpperCAmelCase : list[int] , _UpperCAmelCase : int ) -> tuple[int, int, int]: '''simple docstring''' arr.sort() _UpperCAmelCase = len(_UpperCAmelCase ) for i in range(n - 1 ): _UpperCAmelCase , _UpperCAmelCase = i + 1, n - 1 while left < right: if arr[i] + arr[left] + arr[right] == target: return (arr[i], arr[left], arr[right]) elif arr[i] + arr[left] + arr[right] < target: left += 1 elif arr[i] + arr[left] + arr[right] > target: right -= 1 return (0, 0, 0) def A ( ) -> tuple[float, float]: '''simple docstring''' _UpperCAmelCase = '\nfrom __main__ import dataset, triplet_sum1, triplet_sum2\n' _UpperCAmelCase = '\ntriplet_sum1(*dataset)\n' _UpperCAmelCase = '\ntriplet_sum2(*dataset)\n' _UpperCAmelCase = repeat(setup=_UpperCAmelCase , stmt=_UpperCAmelCase , repeat=5 , number=10_000 ) _UpperCAmelCase = repeat(setup=_UpperCAmelCase , stmt=_UpperCAmelCase , repeat=5 , number=10_000 ) return (min(_UpperCAmelCase ), min(_UpperCAmelCase )) if __name__ == "__main__": from doctest import testmod testmod() UpperCAmelCase__ = solution_times() print(f"""The time for naive implementation is {times[0]}.""") print(f"""The time for optimized implementation is {times[1]}.""")
290
0
from __future__ import annotations from collections.abc import Generator def UpperCAmelCase__ ( ): lowercase :Dict = {} lowercase :int = 2 while True: lowercase :int = factor_map.pop(__UpperCamelCase, __UpperCamelCase ) if factor: lowercase :List[Any] = factor + prime while x in factor_map: x += factor lowercase :List[Any] = factor else: lowercase :Union[str, Any] = prime yield prime prime += 1 def UpperCAmelCase__ ( lowerCamelCase = 1e10 ): lowercase :List[Any] = sieve() lowercase :Optional[Any] = 1 while True: lowercase :Dict = next(__UpperCamelCase ) if (2 * prime * n) > limit: return n # Ignore the next prime as the reminder will be 2. next(__UpperCamelCase ) n += 2 if __name__ == "__main__": print(solution())
236
def a__ ( __UpperCamelCase = 1_0_0_0 ): SCREAMING_SNAKE_CASE_ = -1 SCREAMING_SNAKE_CASE_ = 0 for a in range(1 , n // 3 ): # Solving the two equations a**2+b**2=c**2 and a+b+c=N eliminating c SCREAMING_SNAKE_CASE_ = (n * n - 2 * a * n) // (2 * n - 2 * a) SCREAMING_SNAKE_CASE_ = n - a - b if c * c == (a * a + b * b): SCREAMING_SNAKE_CASE_ = a * b * c if candidate >= product: SCREAMING_SNAKE_CASE_ = candidate return product if __name__ == "__main__": print(f"{solution() = }")
118
0
import unittest import torch from torch import nn from accelerate.test_utils import require_cuda from accelerate.utils.memory import find_executable_batch_size, release_memory def _a ( ): """simple docstring""" raise RuntimeError('''CUDA out of memory.''' ) class __magic_name__ ( nn.Module): def __init__( self : Optional[Any] ) -> Optional[int]: '''simple docstring''' super().__init__() UpperCamelCase__ : Optional[Any] = nn.Linear(3 , 4 ) UpperCamelCase__ : Union[str, Any] = nn.BatchNormad(4 ) UpperCamelCase__ : str = nn.Linear(4 , 5 ) def UpperCAmelCase__ ( self : Dict , lowerCamelCase__ : int ) -> List[Any]: '''simple docstring''' return self.lineara(self.batchnorm(self.lineara(_a ) ) ) class __magic_name__ ( unittest.TestCase): def UpperCAmelCase__ ( self : List[Any] ) -> Dict: '''simple docstring''' UpperCamelCase__ : Optional[Any] = [] @find_executable_batch_size(starting_batch_size=128 ) def mock_training_loop_function(lowerCamelCase__ : Any ): nonlocal batch_sizes batch_sizes.append(_a ) if batch_size != 8: raise_fake_out_of_memory() mock_training_loop_function() self.assertListEqual(_a , [128, 64, 32, 16, 8] ) def UpperCAmelCase__ ( self : int ) -> Optional[Any]: '''simple docstring''' UpperCamelCase__ : List[str] = [] @find_executable_batch_size(starting_batch_size=128 ) def mock_training_loop_function(lowerCamelCase__ : str , lowerCamelCase__ : Optional[int] ): nonlocal batch_sizes batch_sizes.append(_a ) if batch_size != 8: raise_fake_out_of_memory() return batch_size, arga UpperCamelCase__ , UpperCamelCase__ : str = mock_training_loop_function('''hello''' ) self.assertListEqual(_a , [128, 64, 32, 16, 8] ) self.assertListEqual([bs, arga] , [8, '''hello'''] ) def UpperCAmelCase__ ( self : Any ) -> List[Any]: '''simple docstring''' @find_executable_batch_size(starting_batch_size=0 ) def mock_training_loop_function(lowerCamelCase__ : List[Any] ): pass with self.assertRaises(_a ) as cm: mock_training_loop_function() self.assertIn('''No executable batch size found, reached zero.''' , cm.exception.args[0] ) def UpperCAmelCase__ ( self : int ) -> Tuple: '''simple docstring''' @find_executable_batch_size(starting_batch_size=16 ) def mock_training_loop_function(lowerCamelCase__ : Dict ): if batch_size > 0: raise_fake_out_of_memory() pass with self.assertRaises(_a ) as cm: mock_training_loop_function() self.assertIn('''No executable batch size found, reached zero.''' , cm.exception.args[0] ) def UpperCAmelCase__ ( self : Union[str, Any] ) -> Optional[Any]: '''simple docstring''' @find_executable_batch_size(starting_batch_size=128 ) def mock_training_loop_function(lowerCamelCase__ : Dict , lowerCamelCase__ : Optional[Any] , lowerCamelCase__ : str ): if batch_size != 8: raise raise_fake_out_of_memory() with self.assertRaises(_a ) as cm: mock_training_loop_function(128 , '''hello''' , '''world''' ) self.assertIn('''Batch size was passed into `f`''' , cm.exception.args[0] ) self.assertIn('''`f(arg1=\'hello\', arg2=\'world\')''' , cm.exception.args[0] ) def UpperCAmelCase__ ( self : int ) -> Union[str, Any]: '''simple docstring''' @find_executable_batch_size(starting_batch_size=16 ) def mock_training_loop_function(lowerCamelCase__ : Tuple ): raise ValueError('''Oops, we had an error!''' ) with self.assertRaises(_a ) as cm: mock_training_loop_function() self.assertIn('''Oops, we had an error!''' , cm.exception.args[0] ) @require_cuda def UpperCAmelCase__ ( self : Union[str, Any] ) -> Optional[Any]: '''simple docstring''' UpperCamelCase__ : Optional[int] = torch.cuda.memory_allocated() UpperCamelCase__ : Any = ModelForTest() model.cuda() self.assertGreater(torch.cuda.memory_allocated() , _a ) UpperCamelCase__ : Dict = release_memory(_a ) self.assertEqual(torch.cuda.memory_allocated() , _a )
363
import os from pathlib import Path import numpy as np import pytest from pack_dataset import pack_data_dir from parameterized import parameterized from save_len_file import save_len_file from torch.utils.data import DataLoader from transformers import AutoTokenizer from transformers.models.mbart.modeling_mbart import shift_tokens_right from transformers.testing_utils import TestCasePlus, slow from utils import FAIRSEQ_AVAILABLE, DistributedSortishSampler, LegacySeqaSeqDataset, SeqaSeqDataset __UpperCamelCase : Union[str, Any] = "bert-base-cased" __UpperCamelCase : Tuple = "google/pegasus-xsum" __UpperCamelCase : Union[str, Any] = [" Sam ate lunch today.", "Sams lunch ingredients."] __UpperCamelCase : Union[str, Any] = ["A very interesting story about what I ate for lunch.", "Avocado, celery, turkey, coffee"] __UpperCamelCase : Any = "patrickvonplaten/t5-tiny-random" __UpperCamelCase : List[Any] = "sshleifer/bart-tiny-random" __UpperCamelCase : Any = "sshleifer/tiny-mbart" __UpperCamelCase : Optional[Any] = "sshleifer/tiny-marian-en-de" def _a ( SCREAMING_SNAKE_CASE : Path , SCREAMING_SNAKE_CASE : list ): """simple docstring""" UpperCamelCase__ : Optional[Any] = '''\n'''.join(SCREAMING_SNAKE_CASE ) Path(SCREAMING_SNAKE_CASE ).open('''w''' ).writelines(SCREAMING_SNAKE_CASE ) def _a ( SCREAMING_SNAKE_CASE : Dict ): """simple docstring""" for split in ["train", "val", "test"]: _dump_articles(os.path.join(SCREAMING_SNAKE_CASE , F"{split}.source" ) , SCREAMING_SNAKE_CASE ) _dump_articles(os.path.join(SCREAMING_SNAKE_CASE , F"{split}.target" ) , SCREAMING_SNAKE_CASE ) return tmp_dir class __magic_name__ ( __lowerCAmelCase): @parameterized.expand( [ MBART_TINY, MARIAN_TINY, T5_TINY, BART_TINY, PEGASUS_XSUM, ] , ) @slow def UpperCAmelCase__ ( self : Tuple , lowerCamelCase__ : Dict ) -> Optional[Any]: '''simple docstring''' UpperCamelCase__ : Dict = AutoTokenizer.from_pretrained(lowerCamelCase__ ) UpperCamelCase__ : Optional[int] = make_test_data_dir(tmp_dir=self.get_auto_remove_tmp_dir() ) UpperCamelCase__ : Tuple = max(len(tokenizer.encode(lowerCamelCase__ ) ) for a in ARTICLES ) UpperCamelCase__ : List[str] = max(len(tokenizer.encode(lowerCamelCase__ ) ) for a in SUMMARIES ) UpperCamelCase__ : int = 4 UpperCamelCase__ : Union[str, Any] = 8 assert max_len_target > max_src_len # Will be truncated assert max_len_source > max_src_len # Will be truncated UpperCamelCase__ , UpperCamelCase__ : Union[str, Any] = '''ro_RO''', '''de_DE''' # ignored for all but mbart, but never causes error. UpperCamelCase__ : List[str] = SeqaSeqDataset( lowerCamelCase__ , data_dir=lowerCamelCase__ , type_path='''train''' , max_source_length=lowerCamelCase__ , max_target_length=lowerCamelCase__ , src_lang=lowerCamelCase__ , tgt_lang=lowerCamelCase__ , ) UpperCamelCase__ : Dict = DataLoader(lowerCamelCase__ , batch_size=2 , collate_fn=train_dataset.collate_fn ) for batch in dataloader: assert isinstance(lowerCamelCase__ , lowerCamelCase__ ) assert batch["attention_mask"].shape == batch["input_ids"].shape # show that articles were trimmed. assert batch["input_ids"].shape[1] == max_src_len # show that targets are the same len assert batch["labels"].shape[1] == max_tgt_len if tok_name != MBART_TINY: continue # check language codes in correct place UpperCamelCase__ : Dict = shift_tokens_right(batch['''labels'''] , tokenizer.pad_token_id ) assert batch["decoder_input_ids"][0, 0].item() == tokenizer.lang_code_to_id[tgt_lang] assert batch["decoder_input_ids"][0, -1].item() == tokenizer.eos_token_id assert batch["input_ids"][0, -2].item() == tokenizer.eos_token_id assert batch["input_ids"][0, -1].item() == tokenizer.lang_code_to_id[src_lang] break # No need to test every batch @parameterized.expand([BART_TINY, BERT_BASE_CASED] ) def UpperCAmelCase__ ( self : int , lowerCamelCase__ : Tuple ) -> List[Any]: '''simple docstring''' UpperCamelCase__ : Dict = AutoTokenizer.from_pretrained(lowerCamelCase__ ) UpperCamelCase__ : str = make_test_data_dir(tmp_dir=self.get_auto_remove_tmp_dir() ) UpperCamelCase__ : Union[str, Any] = max(len(tokenizer.encode(lowerCamelCase__ ) ) for a in ARTICLES ) UpperCamelCase__ : str = max(len(tokenizer.encode(lowerCamelCase__ ) ) for a in SUMMARIES ) UpperCamelCase__ : Union[str, Any] = 4 UpperCamelCase__ : Optional[int] = LegacySeqaSeqDataset( lowerCamelCase__ , data_dir=lowerCamelCase__ , type_path='''train''' , max_source_length=20 , max_target_length=lowerCamelCase__ , ) UpperCamelCase__ : List[str] = DataLoader(lowerCamelCase__ , batch_size=2 , collate_fn=train_dataset.collate_fn ) for batch in dataloader: assert batch["attention_mask"].shape == batch["input_ids"].shape # show that articles were trimmed. assert batch["input_ids"].shape[1] == max_len_source assert 20 >= batch["input_ids"].shape[1] # trimmed significantly # show that targets were truncated assert batch["labels"].shape[1] == trunc_target # Truncated assert max_len_target > trunc_target # Truncated break # No need to test every batch def UpperCAmelCase__ ( self : List[str] ) -> Dict: '''simple docstring''' UpperCamelCase__ : Dict = AutoTokenizer.from_pretrained('''facebook/mbart-large-cc25''' ) UpperCamelCase__ : List[str] = Path(make_test_data_dir(tmp_dir=self.get_auto_remove_tmp_dir() ) ) UpperCamelCase__ : int = tmp_dir.joinpath('''train.source''' ).open().readlines() UpperCamelCase__ : Tuple = Path(make_test_data_dir(tmp_dir=self.get_auto_remove_tmp_dir() ) ) pack_data_dir(lowerCamelCase__ , lowerCamelCase__ , 128 , lowerCamelCase__ ) UpperCamelCase__ : str = {x.name for x in tmp_dir.iterdir()} UpperCamelCase__ : Optional[int] = {x.name for x in save_dir.iterdir()} UpperCamelCase__ : Dict = save_dir.joinpath('''train.source''' ).open().readlines() # orig: [' Sam ate lunch today.\n', 'Sams lunch ingredients.'] # desired_packed: [' Sam ate lunch today.\n Sams lunch ingredients.'] assert len(lowerCamelCase__ ) < len(lowerCamelCase__ ) assert len(lowerCamelCase__ ) == 1 assert len(packed_examples[0] ) == sum(len(lowerCamelCase__ ) for x in orig_examples ) assert orig_paths == new_paths @pytest.mark.skipif(not FAIRSEQ_AVAILABLE , reason='''This test requires fairseq''' ) def UpperCAmelCase__ ( self : str ) -> Optional[Any]: '''simple docstring''' if not FAIRSEQ_AVAILABLE: return UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ : Tuple = self._get_dataset(max_len=64 ) UpperCamelCase__ : List[str] = 64 UpperCamelCase__ : Optional[int] = ds.make_dynamic_sampler(lowerCamelCase__ , required_batch_size_multiple=lowerCamelCase__ ) UpperCamelCase__ : Union[str, Any] = [len(lowerCamelCase__ ) for x in batch_sampler] assert len(set(lowerCamelCase__ ) ) > 1 # it's not dynamic batch size if every batch is the same length assert sum(lowerCamelCase__ ) == len(lowerCamelCase__ ) # no dropped or added examples UpperCamelCase__ : Any = DataLoader(lowerCamelCase__ , batch_sampler=lowerCamelCase__ , collate_fn=ds.collate_fn , num_workers=2 ) UpperCamelCase__ : int = [] UpperCamelCase__ : Tuple = [] for batch in data_loader: UpperCamelCase__ : int = batch['''input_ids'''].shape UpperCamelCase__ : Any = src_shape[0] assert bs % required_batch_size_multiple == 0 or bs < required_batch_size_multiple UpperCamelCase__ : Tuple = np.product(batch['''input_ids'''].shape ) num_src_per_batch.append(lowerCamelCase__ ) if num_src_tokens > (max_tokens * 1.1): failures.append(lowerCamelCase__ ) assert num_src_per_batch[0] == max(lowerCamelCase__ ) if failures: raise AssertionError(F"too many tokens in {len(lowerCamelCase__ )} batches" ) def UpperCAmelCase__ ( self : str ) -> Optional[Any]: '''simple docstring''' UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ : Tuple = self._get_dataset(max_len=512 ) UpperCamelCase__ : Union[str, Any] = 2 UpperCamelCase__ : Optional[int] = ds.make_sortish_sampler(lowerCamelCase__ , shuffle=lowerCamelCase__ ) UpperCamelCase__ : List[Any] = DataLoader(lowerCamelCase__ , batch_size=lowerCamelCase__ , collate_fn=ds.collate_fn , num_workers=2 ) UpperCamelCase__ : str = DataLoader(lowerCamelCase__ , batch_size=lowerCamelCase__ , collate_fn=ds.collate_fn , num_workers=2 , sampler=lowerCamelCase__ ) UpperCamelCase__ : Optional[Any] = tokenizer.pad_token_id def count_pad_tokens(lowerCamelCase__ : int , lowerCamelCase__ : List[Any]="input_ids" ): return [batch[k].eq(lowerCamelCase__ ).sum().item() for batch in data_loader] assert sum(count_pad_tokens(lowerCamelCase__ , k='''labels''' ) ) < sum(count_pad_tokens(lowerCamelCase__ , k='''labels''' ) ) assert sum(count_pad_tokens(lowerCamelCase__ ) ) < sum(count_pad_tokens(lowerCamelCase__ ) ) assert len(lowerCamelCase__ ) == len(lowerCamelCase__ ) def UpperCAmelCase__ ( self : int , lowerCamelCase__ : List[Any]=1000 , lowerCamelCase__ : Tuple=128 ) -> str: '''simple docstring''' if os.getenv('''USE_REAL_DATA''' , lowerCamelCase__ ): UpperCamelCase__ : List[str] = '''examples/seq2seq/wmt_en_ro''' UpperCamelCase__ : int = max_len * 2 * 64 if not Path(lowerCamelCase__ ).joinpath('''train.len''' ).exists(): save_len_file(lowerCamelCase__ , lowerCamelCase__ ) else: UpperCamelCase__ : Optional[Any] = '''examples/seq2seq/test_data/wmt_en_ro''' UpperCamelCase__ : Optional[Any] = max_len * 4 save_len_file(lowerCamelCase__ , lowerCamelCase__ ) UpperCamelCase__ : List[str] = AutoTokenizer.from_pretrained(lowerCamelCase__ ) UpperCamelCase__ : Any = SeqaSeqDataset( lowerCamelCase__ , data_dir=lowerCamelCase__ , type_path='''train''' , max_source_length=lowerCamelCase__ , max_target_length=lowerCamelCase__ , n_obs=lowerCamelCase__ , ) return ds, max_tokens, tokenizer def UpperCAmelCase__ ( self : int ) -> str: '''simple docstring''' UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ : List[Any] = self._get_dataset() UpperCamelCase__ : Any = set(DistributedSortishSampler(lowerCamelCase__ , 256 , num_replicas=2 , rank=0 , add_extra_examples=lowerCamelCase__ ) ) UpperCamelCase__ : str = set(DistributedSortishSampler(lowerCamelCase__ , 256 , num_replicas=2 , rank=1 , add_extra_examples=lowerCamelCase__ ) ) assert idsa.intersection(lowerCamelCase__ ) == set() @parameterized.expand( [ MBART_TINY, MARIAN_TINY, T5_TINY, BART_TINY, PEGASUS_XSUM, ] , ) def UpperCAmelCase__ ( self : Any , lowerCamelCase__ : Tuple ) -> str: '''simple docstring''' UpperCamelCase__ : Union[str, Any] = AutoTokenizer.from_pretrained(lowerCamelCase__ , use_fast=lowerCamelCase__ ) if tok_name == MBART_TINY: UpperCamelCase__ : Dict = SeqaSeqDataset( lowerCamelCase__ , data_dir=make_test_data_dir(tmp_dir=self.get_auto_remove_tmp_dir() ) , type_path='''train''' , max_source_length=4 , max_target_length=8 , src_lang='''EN''' , tgt_lang='''FR''' , ) UpperCamelCase__ : List[Any] = train_dataset.dataset_kwargs assert "src_lang" in kwargs and "tgt_lang" in kwargs else: UpperCamelCase__ : List[Any] = SeqaSeqDataset( lowerCamelCase__ , data_dir=make_test_data_dir(tmp_dir=self.get_auto_remove_tmp_dir() ) , type_path='''train''' , max_source_length=4 , max_target_length=8 , ) UpperCamelCase__ : Optional[Any] = train_dataset.dataset_kwargs assert "add_prefix_space" not in kwargs if tok_name != BART_TINY else "add_prefix_space" in kwargs assert len(lowerCamelCase__ ) == 1 if tok_name == BART_TINY else len(lowerCamelCase__ ) == 0
51
0
import requests def lowerCAmelCase_ ( snake_case_,snake_case_ ): _A : List[str] = {"""Content-Type""": """application/json"""} _A : str = requests.post(snake_case_,json={"""text""": message_body},headers=snake_case_ ) if response.status_code != 200: _A : Union[str, Any] = ( """Request to slack returned an error """ f'''{response.status_code}, the response is:\n{response.text}''' ) raise ValueError(snake_case_ ) if __name__ == "__main__": # Set the slack url to the one provided by Slack when you create the webhook at # https://my.slack.com/services/new/incoming-webhook/ send_slack_message("<YOUR MESSAGE BODY>", "<SLACK CHANNEL URL>")
26
"""simple docstring""" from typing import Dict, List, Optional, Union import numpy as np from transformers.utils import is_vision_available from transformers.utils.generic import TensorType from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size_dict from ...image_transforms import ( center_crop, get_resize_output_image_size, normalize, rescale, resize, to_channel_dimension_format, ) from ...image_utils import ( IMAGENET_STANDARD_MEAN, IMAGENET_STANDARD_STD, ChannelDimension, ImageInput, PILImageResampling, is_valid_image, to_numpy_array, valid_images, ) from ...utils import logging if is_vision_available(): import PIL _A = logging.get_logger(__name__) def lowercase_ ( __UpperCAmelCase ) -> List[List[ImageInput]]: if isinstance(__UpperCAmelCase , (list, tuple) ) and isinstance(videos[0] , (list, tuple) ) and is_valid_image(videos[0][0] ): return videos elif isinstance(__UpperCAmelCase , (list, tuple) ) and is_valid_image(videos[0] ): return [videos] elif is_valid_image(__UpperCAmelCase ): return [[videos]] raise ValueError(f"""Could not make batched video from {videos}""" ) class _lowerCamelCase ( a_ ): _lowerCamelCase :Optional[int] = ["pixel_values"] def __init__( self : Dict , UpperCamelCase : bool = True , UpperCamelCase : Dict[str, int] = None , UpperCamelCase : PILImageResampling = PILImageResampling.BILINEAR , UpperCamelCase : bool = True , UpperCamelCase : Dict[str, int] = None , UpperCamelCase : bool = True , UpperCamelCase : Union[int, float] = 1 / 2_55 , UpperCamelCase : bool = True , UpperCamelCase : bool = True , UpperCamelCase : Optional[Union[float, List[float]]] = None , UpperCamelCase : Optional[Union[float, List[float]]] = None , **UpperCamelCase : Dict , ) -> None: """simple docstring""" super().__init__(**UpperCamelCase ) lowerCAmelCase__ : int = size if size is not None else {"""shortest_edge""": 2_56} lowerCAmelCase__ : Union[str, Any] = get_size_dict(UpperCamelCase , default_to_square=UpperCamelCase ) lowerCAmelCase__ : Union[str, Any] = crop_size if crop_size is not None else {"""height""": 2_24, """width""": 2_24} lowerCAmelCase__ : List[str] = get_size_dict(UpperCamelCase , param_name="""crop_size""" ) lowerCAmelCase__ : Union[str, Any] = do_resize lowerCAmelCase__ : str = size lowerCAmelCase__ : str = do_center_crop lowerCAmelCase__ : Tuple = crop_size lowerCAmelCase__ : Union[str, Any] = resample lowerCAmelCase__ : Any = do_rescale lowerCAmelCase__ : Union[str, Any] = rescale_factor lowerCAmelCase__ : Dict = offset lowerCAmelCase__ : Optional[int] = do_normalize lowerCAmelCase__ : Optional[int] = image_mean if image_mean is not None else IMAGENET_STANDARD_MEAN lowerCAmelCase__ : Tuple = image_std if image_std is not None else IMAGENET_STANDARD_STD def _lowerCAmelCase ( self : List[str] , UpperCamelCase : np.ndarray , UpperCamelCase : Dict[str, int] , UpperCamelCase : PILImageResampling = PILImageResampling.BILINEAR , UpperCamelCase : Optional[Union[str, ChannelDimension]] = None , **UpperCamelCase : Optional[Any] , ) -> np.ndarray: """simple docstring""" lowerCAmelCase__ : List[Any] = get_size_dict(UpperCamelCase , default_to_square=UpperCamelCase ) if "shortest_edge" in size: lowerCAmelCase__ : int = get_resize_output_image_size(UpperCamelCase , size["""shortest_edge"""] , default_to_square=UpperCamelCase ) elif "height" in size and "width" in size: lowerCAmelCase__ : Union[str, Any] = (size["""height"""], size["""width"""]) else: raise ValueError(f"""Size must have 'height' and 'width' or 'shortest_edge' as keys. Got {size.keys()}""" ) return resize(UpperCamelCase , size=UpperCamelCase , resample=UpperCamelCase , data_format=UpperCamelCase , **UpperCamelCase ) def _lowerCAmelCase ( self : Tuple , UpperCamelCase : np.ndarray , UpperCamelCase : Dict[str, int] , UpperCamelCase : Optional[Union[str, ChannelDimension]] = None , **UpperCamelCase : Optional[int] , ) -> np.ndarray: """simple docstring""" lowerCAmelCase__ : int = get_size_dict(UpperCamelCase ) if "height" not in size or "width" not in size: raise ValueError(f"""Size must have 'height' and 'width' as keys. Got {size.keys()}""" ) return center_crop(UpperCamelCase , size=(size["""height"""], size["""width"""]) , data_format=UpperCamelCase , **UpperCamelCase ) def _lowerCAmelCase ( self : Dict , UpperCamelCase : np.ndarray , UpperCamelCase : Union[int, float] , UpperCamelCase : bool = True , UpperCamelCase : Optional[Union[str, ChannelDimension]] = None , **UpperCamelCase : Union[str, Any] , ) -> List[Any]: """simple docstring""" lowerCAmelCase__ : Union[str, Any] = image.astype(np.floataa ) if offset: lowerCAmelCase__ : Tuple = image - (scale / 2) return rescale(UpperCamelCase , scale=UpperCamelCase , data_format=UpperCamelCase , **UpperCamelCase ) def _lowerCAmelCase ( self : Dict , UpperCamelCase : np.ndarray , UpperCamelCase : Union[float, List[float]] , UpperCamelCase : Union[float, List[float]] , UpperCamelCase : Optional[Union[str, ChannelDimension]] = None , **UpperCamelCase : Optional[int] , ) -> np.ndarray: """simple docstring""" return normalize(UpperCamelCase , mean=UpperCamelCase , std=UpperCamelCase , data_format=UpperCamelCase , **UpperCamelCase ) def _lowerCAmelCase ( self : Optional[int] , UpperCamelCase : ImageInput , UpperCamelCase : bool = None , UpperCamelCase : Dict[str, int] = None , UpperCamelCase : PILImageResampling = None , UpperCamelCase : bool = None , UpperCamelCase : Dict[str, int] = None , UpperCamelCase : bool = None , UpperCamelCase : float = None , UpperCamelCase : bool = None , UpperCamelCase : bool = None , UpperCamelCase : Optional[Union[float, List[float]]] = None , UpperCamelCase : Optional[Union[float, List[float]]] = None , UpperCamelCase : Optional[ChannelDimension] = ChannelDimension.FIRST , ) -> np.ndarray: """simple docstring""" 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_size is None: raise ValueError("""Crop size must be specified if do_center_crop is True.""" ) if do_rescale and rescale_factor is None: raise ValueError("""Rescale factor must be specified if do_rescale is True.""" ) if do_normalize and (image_mean is None or image_std is None): raise ValueError("""Image mean and std must be specified if do_normalize is True.""" ) if offset and not do_rescale: raise ValueError("""For offset, do_rescale must also be set to True.""" ) # All transformations expect numpy arrays. lowerCAmelCase__ : Optional[Any] = to_numpy_array(UpperCamelCase ) if do_resize: lowerCAmelCase__ : List[str] = self.resize(image=UpperCamelCase , size=UpperCamelCase , resample=UpperCamelCase ) if do_center_crop: lowerCAmelCase__ : List[str] = self.center_crop(UpperCamelCase , size=UpperCamelCase ) if do_rescale: lowerCAmelCase__ : Optional[int] = self.rescale(image=UpperCamelCase , scale=UpperCamelCase , offset=UpperCamelCase ) if do_normalize: lowerCAmelCase__ : Tuple = self.normalize(image=UpperCamelCase , mean=UpperCamelCase , std=UpperCamelCase ) lowerCAmelCase__ : List[str] = to_channel_dimension_format(UpperCamelCase , UpperCamelCase ) return image def _lowerCAmelCase ( self : Optional[int] , UpperCamelCase : ImageInput , UpperCamelCase : bool = None , UpperCamelCase : Dict[str, int] = None , UpperCamelCase : PILImageResampling = None , UpperCamelCase : bool = None , UpperCamelCase : Dict[str, int] = None , UpperCamelCase : bool = None , UpperCamelCase : float = None , UpperCamelCase : bool = None , UpperCamelCase : bool = None , UpperCamelCase : Optional[Union[float, List[float]]] = None , UpperCamelCase : Optional[Union[float, List[float]]] = None , UpperCamelCase : Optional[Union[str, TensorType]] = None , UpperCamelCase : ChannelDimension = ChannelDimension.FIRST , **UpperCamelCase : Dict , ) -> PIL.Image.Image: """simple docstring""" lowerCAmelCase__ : List[str] = do_resize if do_resize is not None else self.do_resize lowerCAmelCase__ : Union[str, Any] = resample if resample is not None else self.resample lowerCAmelCase__ : Union[str, Any] = do_center_crop if do_center_crop is not None else self.do_center_crop lowerCAmelCase__ : int = do_rescale if do_rescale is not None else self.do_rescale lowerCAmelCase__ : Dict = rescale_factor if rescale_factor is not None else self.rescale_factor lowerCAmelCase__ : Dict = offset if offset is not None else self.offset lowerCAmelCase__ : str = do_normalize if do_normalize is not None else self.do_normalize lowerCAmelCase__ : List[Any] = image_mean if image_mean is not None else self.image_mean lowerCAmelCase__ : Union[str, Any] = image_std if image_std is not None else self.image_std lowerCAmelCase__ : List[Any] = size if size is not None else self.size lowerCAmelCase__ : Tuple = get_size_dict(UpperCamelCase , default_to_square=UpperCamelCase ) lowerCAmelCase__ : Optional[int] = crop_size if crop_size is not None else self.crop_size lowerCAmelCase__ : Optional[Any] = get_size_dict(UpperCamelCase , param_name="""crop_size""" ) if not valid_images(UpperCamelCase ): raise ValueError( """Invalid image type. Must be of type PIL.Image.Image, numpy.ndarray, """ """torch.Tensor, tf.Tensor or jax.ndarray.""" ) lowerCAmelCase__ : int = make_batched(UpperCamelCase ) lowerCAmelCase__ : str = [ [ self._preprocess_image( image=UpperCamelCase , do_resize=UpperCamelCase , size=UpperCamelCase , resample=UpperCamelCase , do_center_crop=UpperCamelCase , crop_size=UpperCamelCase , do_rescale=UpperCamelCase , rescale_factor=UpperCamelCase , offset=UpperCamelCase , do_normalize=UpperCamelCase , image_mean=UpperCamelCase , image_std=UpperCamelCase , data_format=UpperCamelCase , ) for img in video ] for video in videos ] lowerCAmelCase__ : Dict = {"""pixel_values""": videos} return BatchFeature(data=UpperCamelCase , tensor_type=UpperCamelCase )
242
0
from datetime import datetime import matplotlib.pyplot as plt import torch def __snake_case ( _UpperCAmelCase ): for param in module.parameters(): __a = False def __snake_case ( ): __a = '''cuda''' if torch.cuda.is_available() else '''cpu''' if torch.backends.mps.is_available() and torch.backends.mps.is_built(): __a = '''mps''' if device == "mps": print( '''WARNING: MPS currently doesn\'t seem to work, and messes up backpropagation without any visible torch''' ''' errors. I recommend using CUDA on a colab notebook or CPU instead if you\'re facing inexplicable issues''' ''' with generations.''' ) return device def __snake_case ( _UpperCAmelCase ): __a = plt.imshow(_UpperCAmelCase ) fig.axes.get_xaxis().set_visible(_UpperCAmelCase ) fig.axes.get_yaxis().set_visible(_UpperCAmelCase ) plt.show() def __snake_case ( ): __a = datetime.now() __a = current_time.strftime('''%H:%M:%S''' ) return timestamp
131
from typing import Any, Dict, List, Union from ..utils import add_end_docstrings, is_torch_available, is_vision_available, logging, requires_backends from .base import PIPELINE_INIT_ARGS, ChunkPipeline if is_vision_available(): from PIL import Image from ..image_utils import load_image if is_torch_available(): import torch from transformers.modeling_outputs import BaseModelOutput from ..models.auto.modeling_auto import MODEL_FOR_ZERO_SHOT_OBJECT_DETECTION_MAPPING __snake_case :Tuple = logging.get_logger(__name__) @add_end_docstrings(__UpperCAmelCase ) class _A ( __UpperCAmelCase ): def __init__( self : Optional[int] , **__SCREAMING_SNAKE_CASE : Optional[Any]): '''simple docstring''' super().__init__(**__SCREAMING_SNAKE_CASE) if self.framework == "tf": raise ValueError(F'The {self.__class__} is only available in PyTorch.') requires_backends(self , '''vision''') self.check_model_type(__SCREAMING_SNAKE_CASE) def __call__( self : List[Any] , __SCREAMING_SNAKE_CASE : Union[str, "Image.Image", List[Dict[str, Any]]] , __SCREAMING_SNAKE_CASE : Union[str, List[str]] = None , **__SCREAMING_SNAKE_CASE : Union[str, Any] , ): '''simple docstring''' if "text_queries" in kwargs: __a = kwargs.pop('''text_queries''') if isinstance(__SCREAMING_SNAKE_CASE , (str, Image.Image)): __a = {'''image''': image, '''candidate_labels''': candidate_labels} else: __a = image __a = super().__call__(__SCREAMING_SNAKE_CASE , **__SCREAMING_SNAKE_CASE) return results def _lowerCamelCase ( self : str , **__SCREAMING_SNAKE_CASE : Union[str, Any]): '''simple docstring''' __a = {} if "threshold" in kwargs: __a = kwargs['''threshold'''] if "top_k" in kwargs: __a = kwargs['''top_k'''] return {}, {}, postprocess_params def _lowerCamelCase ( self : List[str] , __SCREAMING_SNAKE_CASE : Tuple): '''simple docstring''' __a = load_image(inputs['''image''']) __a = inputs['''candidate_labels'''] if isinstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE): __a = candidate_labels.split(''',''') __a = torch.tensor([[image.height, image.width]] , dtype=torch.intaa) for i, candidate_label in enumerate(__SCREAMING_SNAKE_CASE): __a = self.tokenizer(__SCREAMING_SNAKE_CASE , return_tensors=self.framework) __a = self.image_processor(__SCREAMING_SNAKE_CASE , return_tensors=self.framework) yield { "is_last": i == len(__SCREAMING_SNAKE_CASE) - 1, "target_size": target_size, "candidate_label": candidate_label, **text_inputs, **image_features, } def _lowerCamelCase ( self : int , __SCREAMING_SNAKE_CASE : Tuple): '''simple docstring''' __a = model_inputs.pop('''target_size''') __a = model_inputs.pop('''candidate_label''') __a = model_inputs.pop('''is_last''') __a = self.model(**__SCREAMING_SNAKE_CASE) __a = {'''target_size''': target_size, '''candidate_label''': candidate_label, '''is_last''': is_last, **outputs} return model_outputs def _lowerCamelCase ( self : Optional[Any] , __SCREAMING_SNAKE_CASE : Optional[int] , __SCREAMING_SNAKE_CASE : Tuple=0.1 , __SCREAMING_SNAKE_CASE : Union[str, Any]=None): '''simple docstring''' __a = [] for model_output in model_outputs: __a = model_output['''candidate_label'''] __a = BaseModelOutput(__SCREAMING_SNAKE_CASE) __a = self.image_processor.post_process_object_detection( outputs=__SCREAMING_SNAKE_CASE , threshold=__SCREAMING_SNAKE_CASE , target_sizes=model_output['''target_size'''])[0] for index in outputs["scores"].nonzero(): __a = outputs['''scores'''][index].item() __a = self._get_bounding_box(outputs['''boxes'''][index][0]) __a = {'''score''': score, '''label''': label, '''box''': box} results.append(__SCREAMING_SNAKE_CASE) __a = sorted(__SCREAMING_SNAKE_CASE , key=lambda __SCREAMING_SNAKE_CASE: x["score"] , reverse=__SCREAMING_SNAKE_CASE) if top_k: __a = results[:top_k] return results def _lowerCamelCase ( self : Optional[Any] , __SCREAMING_SNAKE_CASE : "torch.Tensor"): '''simple docstring''' if self.framework != "pt": raise ValueError('''The ZeroShotObjectDetectionPipeline is only available in PyTorch.''') __a , __a , __a , __a = box.int().tolist() __a = { '''xmin''': xmin, '''ymin''': ymin, '''xmax''': xmax, '''ymax''': ymax, } return bbox
131
1
'''simple docstring''' import argparse import torch from transformers import BertConfig, BertForPreTraining, load_tf_weights_in_bert from transformers.utils import logging logging.set_verbosity_info() def __lowerCamelCase ( __snake_case : int, __snake_case : Tuple, __snake_case : str ) -> List[Any]: """simple docstring""" A__ : Optional[int] =BertConfig.from_json_file(_lowerCamelCase ) print(f"Building PyTorch model from configuration: {config}" ) A__ : Union[str, Any] =BertForPreTraining(_lowerCamelCase ) # Load weights from tf checkpoint load_tf_weights_in_bert(_lowerCamelCase, _lowerCamelCase, _lowerCamelCase ) # Save pytorch-model print(f"Save PyTorch model to {pytorch_dump_path}" ) torch.save(model.state_dict(), _lowerCamelCase ) if __name__ == "__main__": __snake_case : Optional[Any] = 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( '--bert_config_file', default=None, type=str, required=True, help=( 'The config json file corresponding to the pre-trained BERT model. \n' 'This specifies the model architecture.' ), ) parser.add_argument( '--pytorch_dump_path', default=None, type=str, required=True, help='Path to the output PyTorch model.' ) __snake_case : Optional[int] = parser.parse_args() convert_tf_checkpoint_to_pytorch(args.tf_checkpoint_path, args.bert_config_file, args.pytorch_dump_path)
134
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tf_available, is_torch_available UpperCamelCase_ = { """configuration_groupvit""": [ """GROUPVIT_PRETRAINED_CONFIG_ARCHIVE_MAP""", """GroupViTConfig""", """GroupViTOnnxConfig""", """GroupViTTextConfig""", """GroupViTVisionConfig""", ], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase_ = [ """GROUPVIT_PRETRAINED_MODEL_ARCHIVE_LIST""", """GroupViTModel""", """GroupViTPreTrainedModel""", """GroupViTTextModel""", """GroupViTVisionModel""", ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCamelCase_ = [ """TF_GROUPVIT_PRETRAINED_MODEL_ARCHIVE_LIST""", """TFGroupViTModel""", """TFGroupViTPreTrainedModel""", """TFGroupViTTextModel""", """TFGroupViTVisionModel""", ] if TYPE_CHECKING: from .configuration_groupvit import ( GROUPVIT_PRETRAINED_CONFIG_ARCHIVE_MAP, GroupViTConfig, GroupViTOnnxConfig, GroupViTTextConfig, GroupViTVisionConfig, ) try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_groupvit import ( GROUPVIT_PRETRAINED_MODEL_ARCHIVE_LIST, GroupViTModel, GroupViTPreTrainedModel, GroupViTTextModel, GroupViTVisionModel, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_groupvit import ( TF_GROUPVIT_PRETRAINED_MODEL_ARCHIVE_LIST, TFGroupViTModel, TFGroupViTPreTrainedModel, TFGroupViTTextModel, TFGroupViTVisionModel, ) else: import sys UpperCamelCase_ = _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)
309
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 a__: List[Any] = 16 a__: Optional[Any] = 32 def UpperCamelCase__( UpperCamelCase__ : int , UpperCamelCase__ : Dict = 16 , UpperCamelCase__ : Union[str, Any] = "bert-base-cased" )->Any: A__ = AutoTokenizer.from_pretrained(_lowerCAmelCase ) A__ = load_dataset('''glue''' , '''mrpc''' ) def tokenize_function(UpperCamelCase__ : Tuple ): # max_length=None => use the model max length (it's actually the default) A__ = tokenizer(examples['''sentence1'''] , examples['''sentence2'''] , truncation=_lowerCAmelCase , max_length=_lowerCAmelCase ) return outputs # Apply the method we just defined to all the examples in all the splits of the dataset A__ = datasets.map( _lowerCAmelCase , batched=_lowerCAmelCase , remove_columns=['''idx''', '''sentence1''', '''sentence2'''] , load_from_cache_file=_lowerCAmelCase ) # We also rename the 'label' column to 'labels' which is the expected name for labels by the models of the # transformers library A__ = tokenized_datasets.rename_column('''label''' , '''labels''' ) def collate_fn(UpperCamelCase__ : Any ): # 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(_lowerCAmelCase , padding='''max_length''' , max_length=1_28 , return_tensors='''pt''' ) return tokenizer.pad(_lowerCAmelCase , padding='''longest''' , return_tensors='''pt''' ) # Instantiate dataloaders. A__ = DataLoader( tokenized_datasets['''train'''] , shuffle=_lowerCAmelCase , collate_fn=_lowerCAmelCase , batch_size=_lowerCAmelCase ) A__ = DataLoader( tokenized_datasets['''validation'''] , shuffle=_lowerCAmelCase , collate_fn=_lowerCAmelCase , batch_size=_lowerCAmelCase ) return train_dataloader, eval_dataloader def UpperCamelCase__( UpperCamelCase__ : Tuple , UpperCamelCase__ : Any )->int: # Initialize accelerator A__ = Accelerator() # Sample hyper-parameters for learning rate, batch size, seed and a few other HPs A__ = config["""lr"""] A__ = int(config['''num_epochs'''] ) A__ = int(config['''seed'''] ) A__ = int(config['''batch_size'''] ) A__ = args.model_name_or_path set_seed(_lowerCAmelCase ) A__ = get_dataloaders(_lowerCAmelCase , _lowerCAmelCase , _lowerCAmelCase ) # Instantiate the model (we build the model here so that the seed also control new weights initialization) A__ = AutoModelForSequenceClassification.from_pretrained(_lowerCAmelCase , return_dict=_lowerCAmelCase ) # Instantiate optimizer A__ = ( AdamW if accelerator.state.deepspeed_plugin is None or """optimizer""" not in accelerator.state.deepspeed_plugin.deepspeed_config else DummyOptim ) A__ = optimizer_cls(params=model.parameters() , lr=_lowerCAmelCase ) if accelerator.state.deepspeed_plugin is not None: A__ = accelerator.state.deepspeed_plugin.deepspeed_config[ """gradient_accumulation_steps""" ] else: A__ = 1 A__ = (len(_lowerCAmelCase ) * num_epochs) // gradient_accumulation_steps # Instantiate scheduler if ( accelerator.state.deepspeed_plugin is None or "scheduler" not in accelerator.state.deepspeed_plugin.deepspeed_config ): A__ = get_linear_schedule_with_warmup( optimizer=_lowerCAmelCase , num_warmup_steps=0 , num_training_steps=_lowerCAmelCase , ) else: A__ = DummyScheduler(_lowerCAmelCase , total_num_steps=_lowerCAmelCase , 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. A__ = accelerator.prepare( _lowerCAmelCase , _lowerCAmelCase , _lowerCAmelCase , _lowerCAmelCase , _lowerCAmelCase ) # We need to keep track of how many total steps we have iterated over A__ = 0 # We also need to keep track of the stating epoch so files are named properly A__ = 0 # Now we train the model A__ = evaluate.load('''glue''' , '''mrpc''' ) A__ = 0 A__ = {} for epoch in range(_lowerCAmelCase , _lowerCAmelCase ): model.train() for step, batch in enumerate(_lowerCAmelCase ): A__ = model(**_lowerCAmelCase ) A__ = outputs.loss A__ = loss / gradient_accumulation_steps accelerator.backward(_lowerCAmelCase ) if step % gradient_accumulation_steps == 0: optimizer.step() lr_scheduler.step() optimizer.zero_grad() overall_step += 1 model.eval() A__ = 0 for step, batch in enumerate(_lowerCAmelCase ): # We could avoid this line since we set the accelerator with `device_placement=True`. batch.to(accelerator.device ) with torch.no_grad(): A__ = model(**_lowerCAmelCase ) A__ = outputs.logits.argmax(dim=-1 ) # It is slightly faster to call this once, than multiple times A__ = accelerator.gather( (predictions, batch['''labels''']) ) # If we are in a multiprocess environment, the last batch has duplicates if accelerator.use_distributed: if step == len(_lowerCAmelCase ) - 1: A__ = predictions[: len(eval_dataloader.dataset ) - samples_seen] A__ = references[: len(eval_dataloader.dataset ) - samples_seen] else: samples_seen += references.shape[0] metric.add_batch( predictions=_lowerCAmelCase , references=_lowerCAmelCase , ) A__ = metric.compute() # Use accelerator.print to print only on the main process. accelerator.print(f"epoch {epoch}:" , _lowerCAmelCase ) A__ = eval_metric["""accuracy"""] if best_performance < eval_metric["accuracy"]: A__ = 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(_lowerCAmelCase , _lowerCAmelCase ) def UpperCamelCase__( )->List[str]: A__ = argparse.ArgumentParser(description='''Simple example of training script tracking peak GPU memory usage.''' ) parser.add_argument( '''--model_name_or_path''' , type=_lowerCAmelCase , default='''bert-base-cased''' , help='''Path to pretrained model or model identifier from huggingface.co/models.''' , required=_lowerCAmelCase , ) parser.add_argument( '''--output_dir''' , type=_lowerCAmelCase , 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=_lowerCAmelCase , default=_lowerCAmelCase , 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=_lowerCAmelCase , default=3 , help='''Number of train epochs.''' , ) A__ = parser.parse_args() A__ = {"""lr""": 2e-5, """num_epochs""": args.num_epochs, """seed""": 42, """batch_size""": 16} training_function(_lowerCAmelCase , _lowerCAmelCase ) if __name__ == "__main__": main()
370
from __future__ import annotations import time import numpy as np a__: Optional[Any] = [8, 5, 9, 7] a__: Dict = [ [2, 0, 1, 1], [0, 1, 2, 1], [4, 0, 0, 3], [0, 2, 1, 0], [1, 0, 3, 0], ] a__: List[Any] = [ [3, 2, 1, 4], [0, 2, 5, 2], [5, 1, 0, 5], [1, 5, 3, 0], [3, 0, 3, 3], ] class SCREAMING_SNAKE_CASE__ : def __init__( self,__lowerCamelCase,__lowerCamelCase,__lowerCamelCase,): A__ = claim_vector A__ = allocated_resources_table A__ = maximum_claim_table def UpperCamelCase ( self ): return [ sum(p_item[i] for p_item in self.__allocated_resources_table ) for i in range(len(self.__allocated_resources_table[0] ) ) ] def UpperCamelCase ( self ): return np.array(self.__claim_vector ) - np.array( self.__processes_resource_summation() ) def UpperCamelCase ( self ): return [ list(np.array(self.__maximum_claim_table[i] ) - np.array(__lowerCamelCase ) ) for i, allocated_resource in enumerate(self.__allocated_resources_table ) ] def UpperCamelCase ( self ): return {self.__need().index(__lowerCamelCase ): i for i in self.__need()} def UpperCamelCase ( self,**__lowerCamelCase ): A__ = self.__need() A__ = self.__allocated_resources_table A__ = self.__available_resources() A__ = self.__need_index_manager() for kw, val in kwargs.items(): if kw and val is True: self.__pretty_data() print('''_''' * 50 + '''\n''' ) while need_list: A__ = False for each_need in need_list: A__ = True for index, need in enumerate(__lowerCamelCase ): if need > available_resources[index]: A__ = False break if execution: A__ = True # get the original index of the process from ind_ctrl db for original_need_index, need_clone in need_index_manager.items(): if each_need == need_clone: A__ = original_need_index print(f"Process {process_number + 1} is executing." ) # remove the process run from stack need_list.remove(__lowerCamelCase ) # update available/freed resources stack A__ = np.array(__lowerCamelCase ) + np.array( alloc_resources_table[process_number] ) print( '''Updated available resource stack for processes: ''' + ''' '''.join([str(__lowerCamelCase ) for x in available_resources] ) ) break if safe: print('''The process is in a safe state.\n''' ) else: print('''System in unsafe state. Aborting...\n''' ) break def UpperCamelCase ( self ): print(''' ''' * 9 + '''Allocated Resource Table''' ) for item in self.__allocated_resources_table: print( f"P{self.__allocated_resources_table.index(__lowerCamelCase ) + 1}" + ''' '''.join(f"{it:>8}" for it in item ) + '''\n''' ) print(''' ''' * 9 + '''System Resource Table''' ) for item in self.__maximum_claim_table: print( f"P{self.__maximum_claim_table.index(__lowerCamelCase ) + 1}" + ''' '''.join(f"{it:>8}" for it in item ) + '''\n''' ) print( '''Current Usage by Active Processes: ''' + ''' '''.join(str(__lowerCamelCase ) for x in self.__claim_vector ) ) print( '''Initial Available Resources: ''' + ''' '''.join(str(__lowerCamelCase ) for x in self.__available_resources() ) ) time.sleep(1 ) if __name__ == "__main__": import doctest doctest.testmod()
39
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 _a ( ): __lowerCAmelCase = "https://storage.googleapis.com/sfr-vision-language-research/LAVIS/assets/merlion.png" __lowerCAmelCase = Image.open(requests.get(SCREAMING_SNAKE_CASE_ , stream=SCREAMING_SNAKE_CASE_ ).raw ).convert("RGB" ) return image def _a ( SCREAMING_SNAKE_CASE_ : List[Any] ): __lowerCAmelCase = [] # 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 _a ( SCREAMING_SNAKE_CASE_ : Dict , SCREAMING_SNAKE_CASE_ : Tuple , SCREAMING_SNAKE_CASE_ : Dict ): __lowerCAmelCase = dct.pop(SCREAMING_SNAKE_CASE_ ) __lowerCAmelCase = val def _a ( SCREAMING_SNAKE_CASE_ : Union[str, Any] , SCREAMING_SNAKE_CASE_ : Union[str, Any] ): for i in range(config.vision_config.num_hidden_layers ): # read in original q and v biases __lowerCAmelCase = state_dict.pop(F"""visual_encoder.blocks.{i}.attn.q_bias""" ) __lowerCAmelCase = state_dict.pop(F"""visual_encoder.blocks.{i}.attn.v_bias""" ) # next, set bias in the state dict __lowerCAmelCase = torch.cat((q_bias, torch.zeros_like(SCREAMING_SNAKE_CASE_ , requires_grad=SCREAMING_SNAKE_CASE_ ), v_bias) ) __lowerCAmelCase = qkv_bias def _a ( SCREAMING_SNAKE_CASE_ : Any , SCREAMING_SNAKE_CASE_ : Any ): __lowerCAmelCase = 3_64 if "coco" in model_name else 2_24 __lowerCAmelCase = BlipaVisionConfig(image_size=SCREAMING_SNAKE_CASE_ ).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: __lowerCAmelCase = OPTConfig.from_pretrained("facebook/opt-2.7b" , eos_token_id=SCREAMING_SNAKE_CASE_ ).to_dict() elif "opt-6.7b" in model_name: __lowerCAmelCase = OPTConfig.from_pretrained("facebook/opt-6.7b" , eos_token_id=SCREAMING_SNAKE_CASE_ ).to_dict() elif "t5-xl" in model_name: __lowerCAmelCase = TaConfig.from_pretrained("google/flan-t5-xl" , dense_act_fn="gelu" , bos_token_id=1 ).to_dict() elif "t5-xxl" in model_name: __lowerCAmelCase = TaConfig.from_pretrained("google/flan-t5-xxl" , dense_act_fn="gelu" , bos_token_id=1 ).to_dict() __lowerCAmelCase = BlipaConfig(vision_config=SCREAMING_SNAKE_CASE_ , text_config=SCREAMING_SNAKE_CASE_ ) return config, image_size @torch.no_grad() def _a ( SCREAMING_SNAKE_CASE_ : Any , SCREAMING_SNAKE_CASE_ : Dict=None , SCREAMING_SNAKE_CASE_ : Union[str, Any]=False ): __lowerCAmelCase = ( AutoTokenizer.from_pretrained("facebook/opt-2.7b" ) if "opt" in model_name else AutoTokenizer.from_pretrained("google/flan-t5-xl" ) ) __lowerCAmelCase = tokenizer("\n" , add_special_tokens=SCREAMING_SNAKE_CASE_ ).input_ids[0] __lowerCAmelCase , __lowerCAmelCase = get_blipa_config(SCREAMING_SNAKE_CASE_ , eos_token_id=SCREAMING_SNAKE_CASE_ ) __lowerCAmelCase = BlipaForConditionalGeneration(SCREAMING_SNAKE_CASE_ ).eval() __lowerCAmelCase = { "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"), } __lowerCAmelCase , __lowerCAmelCase = model_name_to_original[model_name] # load original model print("Loading original model..." ) __lowerCAmelCase = "cuda" if torch.cuda.is_available() else "cpu" __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase = load_model_and_preprocess( name=SCREAMING_SNAKE_CASE_ , model_type=SCREAMING_SNAKE_CASE_ , is_eval=SCREAMING_SNAKE_CASE_ , device=SCREAMING_SNAKE_CASE_ ) original_model.eval() print("Done!" ) # update state dict keys __lowerCAmelCase = original_model.state_dict() __lowerCAmelCase = create_rename_keys(SCREAMING_SNAKE_CASE_ ) for src, dest in rename_keys: rename_key(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) # some keys can be renamed efficiently for key, val in state_dict.copy().items(): __lowerCAmelCase = state_dict.pop(SCREAMING_SNAKE_CASE_ ) if key.startswith("Qformer.bert" ): __lowerCAmelCase = key.replace("Qformer.bert" , "qformer" ) if "attention.self" in key: __lowerCAmelCase = key.replace("self" , "attention" ) if "opt_proj" in key: __lowerCAmelCase = key.replace("opt_proj" , "language_projection" ) if "t5_proj" in key: __lowerCAmelCase = key.replace("t5_proj" , "language_projection" ) if key.startswith("opt" ): __lowerCAmelCase = key.replace("opt" , "language" ) if key.startswith("t5" ): __lowerCAmelCase = key.replace("t5" , "language" ) __lowerCAmelCase = val # read in qv biases read_in_q_v_bias(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) __lowerCAmelCase , __lowerCAmelCase = hf_model.load_state_dict(SCREAMING_SNAKE_CASE_ , strict=SCREAMING_SNAKE_CASE_ ) assert len(SCREAMING_SNAKE_CASE_ ) == 0 assert unexpected_keys == ["qformer.embeddings.position_ids"] __lowerCAmelCase = load_demo_image() __lowerCAmelCase = vis_processors["eval"](SCREAMING_SNAKE_CASE_ ).unsqueeze(0 ).to(SCREAMING_SNAKE_CASE_ ) __lowerCAmelCase = tokenizer(["\n"] , return_tensors="pt" ).input_ids.to(SCREAMING_SNAKE_CASE_ ) # create processor __lowerCAmelCase = BlipImageProcessor( size={"height": image_size, "width": image_size} , image_mean=SCREAMING_SNAKE_CASE_ , image_std=SCREAMING_SNAKE_CASE_ ) __lowerCAmelCase = BlipaProcessor(image_processor=SCREAMING_SNAKE_CASE_ , tokenizer=SCREAMING_SNAKE_CASE_ ) __lowerCAmelCase = processor(images=SCREAMING_SNAKE_CASE_ , return_tensors="pt" ).pixel_values.to(SCREAMING_SNAKE_CASE_ ) # make sure processor creates exact same pixel values assert torch.allclose(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) original_model.to(SCREAMING_SNAKE_CASE_ ) hf_model.to(SCREAMING_SNAKE_CASE_ ) with torch.no_grad(): if "opt" in model_name: __lowerCAmelCase = original_model({"image": original_pixel_values, "text_input": [""]} ).logits __lowerCAmelCase = hf_model(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ).logits else: __lowerCAmelCase = original_model( {"image": original_pixel_values, "text_input": ["\n"], "text_output": ["\n"]} ).logits __lowerCAmelCase = input_ids.masked_fill(input_ids == tokenizer.pad_token_id , -1_00 ) __lowerCAmelCase = hf_model(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , labels=SCREAMING_SNAKE_CASE_ ).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": __lowerCAmelCase = torch.tensor( [[-41.58_50, -4.44_40, -8.99_22], [-47.43_22, -5.91_43, -1.73_40]] , device=SCREAMING_SNAKE_CASE_ ) assert torch.allclose(logits[0, :3, :3] , SCREAMING_SNAKE_CASE_ , atol=1E-4 ) elif model_name == "blip2-flan-t5-xl-coco": __lowerCAmelCase = torch.tensor( [[-57.01_09, -9.89_67, -12.62_80], [-68.65_78, -12.71_91, -10.50_65]] , device=SCREAMING_SNAKE_CASE_ ) else: # cast to same type __lowerCAmelCase = logits.dtype assert torch.allclose(original_logits.to(SCREAMING_SNAKE_CASE_ ) , SCREAMING_SNAKE_CASE_ , atol=1E-2 ) print("Looks ok!" ) print("Generating a caption..." ) __lowerCAmelCase = "" __lowerCAmelCase = tokenizer(SCREAMING_SNAKE_CASE_ , return_tensors="pt" ).input_ids.to(SCREAMING_SNAKE_CASE_ ) __lowerCAmelCase = original_model.generate({"image": original_pixel_values} ) __lowerCAmelCase = hf_model.generate( SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , do_sample=SCREAMING_SNAKE_CASE_ , 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:" , SCREAMING_SNAKE_CASE_ ) __lowerCAmelCase = input_ids.shape[1] __lowerCAmelCase = processor.batch_decode(outputs[:, prompt_length:] , skip_special_tokens=SCREAMING_SNAKE_CASE_ ) __lowerCAmelCase = [text.strip() for text in output_text] print("HF generation:" , SCREAMING_SNAKE_CASE_ ) if pytorch_dump_folder_path is not None: processor.save_pretrained(SCREAMING_SNAKE_CASE_ ) hf_model.save_pretrained(SCREAMING_SNAKE_CASE_ ) 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__ = argparse.ArgumentParser() UpperCamelCase__ = [ """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__ = parser.parse_args() convert_blipa_checkpoint(args.model_name, args.pytorch_dump_folder_path, args.push_to_hub)
92
"""simple docstring""" from collections import OrderedDict from typing import Mapping from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging lowercase__ = logging.get_logger(__name__) lowercase__ = { 'andreasmadsen/efficient_mlm_m0.40': ( 'https://huggingface.co/andreasmadsen/efficient_mlm_m0.40/resolve/main/config.json' ), } class __snake_case ( __lowerCAmelCase ): a__ = """roberta-prelayernorm""" def __init__( self , lowercase=5_02_65 , lowercase=7_68 , lowercase=12 , lowercase=12 , lowercase=30_72 , lowercase="gelu" , lowercase=0.1 , lowercase=0.1 , lowercase=5_12 , lowercase=2 , lowercase=0.02 , lowercase=1e-12 , lowercase=1 , lowercase=0 , lowercase=2 , lowercase="absolute" , lowercase=True , lowercase=None , **lowercase , ) -> Any: '''simple docstring''' super().__init__(pad_token_id=lowercase , bos_token_id=lowercase , eos_token_id=lowercase , **lowercase) a__: Union[str, Any] = vocab_size a__: str = hidden_size a__: Tuple = num_hidden_layers a__: List[str] = num_attention_heads a__: Dict = hidden_act a__: int = intermediate_size a__: Tuple = hidden_dropout_prob a__: str = attention_probs_dropout_prob a__: Tuple = max_position_embeddings a__: Tuple = type_vocab_size a__: Optional[Any] = initializer_range a__: Tuple = layer_norm_eps a__: Optional[int] = position_embedding_type a__: Any = use_cache a__: Dict = classifier_dropout class __snake_case ( __lowerCAmelCase ): @property def lowerCamelCase_ ( self) -> Mapping[str, Mapping[int, str]]: '''simple docstring''' if self.task == "multiple-choice": a__: str = {0: 'batch', 1: 'choice', 2: 'sequence'} else: a__: Union[str, Any] = {0: 'batch', 1: 'sequence'} return OrderedDict( [ ('input_ids', dynamic_axis), ('attention_mask', dynamic_axis), ])
290
0
'''simple docstring''' from __future__ import annotations __a: Dict = list[list[int]] # assigning initial values to the grid __a: Matrix = [ [3, 0, 6, 5, 0, 8, 4, 0, 0], [5, 2, 0, 0, 0, 0, 0, 0, 0], [0, 8, 7, 0, 0, 0, 0, 3, 1], [0, 0, 3, 0, 1, 0, 0, 8, 0], [9, 0, 0, 8, 6, 3, 0, 0, 5], [0, 5, 0, 0, 9, 0, 6, 0, 0], [1, 3, 0, 0, 0, 0, 2, 5, 0], [0, 0, 0, 0, 0, 0, 0, 7, 4], [0, 0, 5, 2, 0, 6, 3, 0, 0], ] # a grid with no solution __a: Matrix = [ [5, 0, 6, 5, 0, 8, 4, 0, 3], [5, 2, 0, 0, 0, 0, 0, 0, 2], [1, 8, 7, 0, 0, 0, 0, 3, 1], [0, 0, 3, 0, 1, 0, 0, 8, 0], [9, 0, 0, 8, 6, 3, 0, 0, 5], [0, 5, 0, 0, 9, 0, 6, 0, 0], [1, 3, 0, 0, 0, 0, 2, 5, 0], [0, 0, 0, 0, 0, 0, 0, 7, 4], [0, 0, 5, 2, 0, 6, 3, 0, 0], ] def __UpperCamelCase ( UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase ): for i in range(9 ): if grid[row][i] == n or grid[i][column] == n: return False for i in range(3 ): for j in range(3 ): if grid[(row - row % 3) + i][(column - column % 3) + j] == n: return False return True def __UpperCamelCase ( UpperCAmelCase ): for i in range(9 ): for j in range(9 ): if grid[i][j] == 0: return i, j return None def __UpperCamelCase ( UpperCAmelCase ): if location := find_empty_location(UpperCamelCase__ ): lowercase__ : int = location else: # If the location is ``None``, then the grid is solved. return grid for digit in range(1 , 10 ): if is_safe(UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ ): lowercase__ : List[Any] = digit if sudoku(UpperCamelCase__ ) is not None: return grid lowercase__ : Optional[Any] = 0 return None def __UpperCamelCase ( UpperCAmelCase ): for row in grid: for cell in row: print(UpperCamelCase__ , end=''' ''' ) print() if __name__ == "__main__": # make a copy of grid so that you can compare with the unmodified grid for example_grid in (initial_grid, no_solution): print("""\nExample grid:\n""" + """=""" * 20) print_solution(example_grid) print("""\nExample grid solution:""") __a: str = sudoku(example_grid) if solution is not None: print_solution(solution) else: print("""Cannot find a solution.""")
352
'''simple docstring''' from collections import UserDict from typing import List, Union from ..utils import ( add_end_docstrings, is_tf_available, is_torch_available, is_vision_available, logging, requires_backends, ) from .base import PIPELINE_INIT_ARGS, Pipeline if is_vision_available(): from PIL import Image from ..image_utils import load_image if is_torch_available(): from ..models.auto.modeling_auto import MODEL_FOR_ZERO_SHOT_IMAGE_CLASSIFICATION_MAPPING if is_tf_available(): from ..models.auto.modeling_tf_auto import TF_MODEL_FOR_ZERO_SHOT_IMAGE_CLASSIFICATION_MAPPING from ..tf_utils import stable_softmax __a: Optional[Any] = logging.get_logger(__name__) @add_end_docstrings(a__ ) class UpperCAmelCase ( a__ ): '''simple docstring''' def __init__( self , **__lowerCAmelCase ) -> int: super().__init__(**__lowerCAmelCase ) requires_backends(self , '''vision''' ) self.check_model_type( TF_MODEL_FOR_ZERO_SHOT_IMAGE_CLASSIFICATION_MAPPING if self.framework == '''tf''' else MODEL_FOR_ZERO_SHOT_IMAGE_CLASSIFICATION_MAPPING ) def __call__( self , __lowerCAmelCase , **__lowerCAmelCase ) -> List[str]: return super().__call__(__lowerCAmelCase , **__lowerCAmelCase ) def _lowerCAmelCase( self , **__lowerCAmelCase ) -> Optional[Any]: lowercase__ : str = {} if "candidate_labels" in kwargs: lowercase__ : str = kwargs['''candidate_labels'''] if "hypothesis_template" in kwargs: lowercase__ : str = kwargs['''hypothesis_template'''] return preprocess_params, {}, {} def _lowerCAmelCase( self , __lowerCAmelCase , __lowerCAmelCase=None , __lowerCAmelCase="This is a photo of {}." ) -> Any: lowercase__ : Union[str, Any] = load_image(__lowerCAmelCase ) lowercase__ : List[str] = self.image_processor(images=[image] , return_tensors=self.framework ) lowercase__ : Union[str, Any] = candidate_labels lowercase__ : int = [hypothesis_template.format(__lowerCAmelCase ) for x in candidate_labels] lowercase__ : Any = self.tokenizer(__lowerCAmelCase , return_tensors=self.framework , padding=__lowerCAmelCase ) lowercase__ : Any = [text_inputs] return inputs def _lowerCAmelCase( self , __lowerCAmelCase ) -> Optional[Any]: lowercase__ : Any = model_inputs.pop('''candidate_labels''' ) lowercase__ : int = model_inputs.pop('''text_inputs''' ) if isinstance(text_inputs[0] , __lowerCAmelCase ): lowercase__ : Union[str, Any] = text_inputs[0] else: # Batching case. lowercase__ : Optional[Any] = text_inputs[0][0] lowercase__ : Any = self.model(**__lowerCAmelCase , **__lowerCAmelCase ) lowercase__ : Any = { '''candidate_labels''': candidate_labels, '''logits''': outputs.logits_per_image, } return model_outputs def _lowerCAmelCase( self , __lowerCAmelCase ) -> List[str]: lowercase__ : Union[str, Any] = model_outputs.pop('''candidate_labels''' ) lowercase__ : Optional[int] = model_outputs['''logits'''][0] if self.framework == "pt": lowercase__ : Optional[int] = logits.softmax(dim=-1 ).squeeze(-1 ) lowercase__ : Any = probs.tolist() if not isinstance(__lowerCAmelCase , __lowerCAmelCase ): lowercase__ : Dict = [scores] elif self.framework == "tf": lowercase__ : List[Any] = stable_softmax(__lowerCAmelCase , axis=-1 ) lowercase__ : str = probs.numpy().tolist() else: raise ValueError(F"""Unsupported framework: {self.framework}""" ) lowercase__ : Optional[int] = [ {'''score''': score, '''label''': candidate_label} for score, candidate_label in sorted(zip(__lowerCAmelCase , __lowerCAmelCase ) , key=lambda __lowerCAmelCase : -x[0] ) ] return result
214
0
"""simple docstring""" import warnings from transformers import AutoTokenizer from transformers.utils import is_torch_available from transformers.utils.generic import ExplicitEnum from ...processing_utils import ProcessorMixin if is_torch_available(): import torch class _UpperCAmelCase ( _A ): SCREAMING_SNAKE_CASE_ : str = "char" SCREAMING_SNAKE_CASE_ : Any = "bpe" SCREAMING_SNAKE_CASE_ : Optional[int] = "wp" __A : int = (DecodeType.CHARACTER, DecodeType.BPE, DecodeType.WORDPIECE) class _UpperCAmelCase ( _A ): SCREAMING_SNAKE_CASE_ : Any = ["image_processor", "char_tokenizer"] SCREAMING_SNAKE_CASE_ : Any = "ViTImageProcessor" SCREAMING_SNAKE_CASE_ : Optional[int] = "MgpstrTokenizer" def __init__( self : List[str] , A : Union[str, Any]=None , A : str=None , **A : Optional[Any] ) -> str: lowercase_ : Optional[Any] = 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 , ) lowercase_ : List[Any] = kwargs.pop('''feature_extractor''' ) lowercase_ : str = 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`.''' ) lowercase_ : List[str] = tokenizer lowercase_ : Union[str, Any] = AutoTokenizer.from_pretrained('''gpt2''' ) lowercase_ : List[Any] = AutoTokenizer.from_pretrained('''bert-base-uncased''' ) super().__init__(A , A ) def __call__( self : List[Any] , A : int=None , A : Tuple=None , A : List[Any]=None , **A : Union[str, Any] ) -> int: if images is None and text is None: raise ValueError('''You need to specify either an `images` or `text` input to process.''' ) if images is not None: lowercase_ : Optional[Any] = self.image_processor(A , return_tensors=A , **A ) if text is not None: lowercase_ : Union[str, Any] = self.char_tokenizer(A , return_tensors=A , **A ) if text is None: return inputs elif images is None: return encodings else: lowercase_ : List[str] = encodings['''input_ids'''] return inputs def A ( self : Optional[Any] , A : int ) -> Optional[int]: lowercase_ , lowercase_ , lowercase_ : Any = sequences lowercase_ : Tuple = char_preds.size(0 ) lowercase_ , lowercase_ : Optional[int] = self._decode_helper(A , '''char''' ) lowercase_ , lowercase_ : Dict = self._decode_helper(A , '''bpe''' ) lowercase_ , lowercase_ : Optional[int] = self._decode_helper(A , '''wp''' ) lowercase_ : int = [] lowercase_ : Optional[int] = [] for i in range(A ): lowercase_ : Dict = [char_scores[i], bpe_scores[i], wp_scores[i]] lowercase_ : List[Any] = [char_strs[i], bpe_strs[i], wp_strs[i]] lowercase_ : Dict = scores.index(max(A ) ) final_strs.append(strs[max_score_index] ) final_scores.append(scores[max_score_index] ) lowercase_ : List[Any] = {} lowercase_ : Any = final_strs lowercase_ : List[Any] = final_scores lowercase_ : Optional[Any] = char_strs lowercase_ : Union[str, Any] = bpe_strs lowercase_ : Dict = wp_strs return out def A ( self : Dict , A : List[Any] , A : Dict ) -> Optional[Any]: if format == DecodeType.CHARACTER: lowercase_ : List[str] = self.char_decode lowercase_ : str = 1 lowercase_ : str = '''[s]''' elif format == DecodeType.BPE: lowercase_ : Optional[int] = self.bpe_decode lowercase_ : List[Any] = 2 lowercase_ : Any = '''#''' elif format == DecodeType.WORDPIECE: lowercase_ : Optional[Any] = self.wp_decode lowercase_ : Optional[int] = 1_02 lowercase_ : str = '''[SEP]''' else: raise ValueError(F'''Format {format} is not supported.''' ) lowercase_ , lowercase_ : Union[str, Any] = [], [] lowercase_ : Dict = pred_logits.size(0 ) lowercase_ : Dict = pred_logits.size(1 ) lowercase_ , lowercase_ : Dict = pred_logits.topk(1 , dim=-1 , largest=A , sorted=A ) lowercase_ : List[str] = preds_index.view(-1 , A )[:, 1:] lowercase_ : Dict = decoder(A ) lowercase_ , lowercase_ : Dict = torch.nn.functional.softmax(A , dim=2 ).max(dim=2 ) lowercase_ : Optional[int] = preds_max_prob[:, 1:] for index in range(A ): lowercase_ : Union[str, Any] = preds_str[index].find(A ) lowercase_ : Union[str, Any] = preds_str[index][:pred_eos] lowercase_ : Dict = preds_index[index].cpu().tolist() lowercase_ : int = pred_index.index(A ) if eos_token in pred_index else -1 lowercase_ : List[str] = preds_max_prob[index][: pred_eos_index + 1] lowercase_ : Optional[int] = pred_max_prob.cumprod(dim=0 )[-1] if pred_max_prob.nelement() != 0 else 0.0 dec_strs.append(A ) conf_scores.append(A ) return dec_strs, conf_scores def A ( self : str , A : Optional[int] ) -> Any: lowercase_ : Tuple = [seq.replace(''' ''' , '''''' ) for seq in self.char_tokenizer.batch_decode(A )] return decode_strs def A ( self : Tuple , A : Union[str, Any] ) -> Optional[int]: return self.bpe_tokenizer.batch_decode(A ) def A ( self : Dict , A : int ) -> str: lowercase_ : str = [seq.replace(''' ''' , '''''' ) for seq in self.wp_tokenizer.batch_decode(A )] return decode_strs
33
import argparse import pickle import numpy as np import torch from torch import nn from transformers import ReformerConfig, ReformerModelWithLMHead from transformers.utils import logging logging.set_verbosity_info() def A (__A : Optional[int] , __A : int , __A : str=None ) -> List[Any]: """simple docstring""" assert torch_layer.weight.shape == weight.shape, F"""{torch_layer} layer.weight does not match""" UpperCAmelCase_ = nn.Parameter(__A ) if bias is not None: assert torch_layer.bias.shape == bias.shape, F"""{torch_layer} layer.bias does not match""" UpperCAmelCase_ = nn.Parameter(__A ) def A (__A : Tuple , __A : Dict , __A : str ) -> Tuple: """simple docstring""" UpperCAmelCase_ = np.asarray(weights[0] ) UpperCAmelCase_ = np.asarray(weights[1] ) UpperCAmelCase_ = np.asarray(weights[2] ) set_param( torch_layer.self_attention.query_key , torch.tensor(__A ).transpose(1 , 2 ).contiguous().view(-1 , __A ) , ) set_param( torch_layer.self_attention.value , torch.tensor(__A ).transpose(1 , 2 ).contiguous().view(-1 , __A ) , ) set_param( torch_layer.output.dense , torch.tensor(__A ).view(-1 , __A ).contiguous().transpose(0 , 1 ) , ) def A (__A : Optional[Any] , __A : Any , __A : List[Any] ) -> int: """simple docstring""" UpperCAmelCase_ = np.asarray(weights[0] ) UpperCAmelCase_ = np.asarray(weights[1] ) UpperCAmelCase_ = np.asarray(weights[2] ) UpperCAmelCase_ = np.asarray(weights[3] ) set_param( torch_layer.self_attention.query , torch.tensor(__A ).transpose(1 , 2 ).contiguous().view(-1 , __A ) , ) set_param( torch_layer.self_attention.key , torch.tensor(__A ).transpose(1 , 2 ).contiguous().view(-1 , __A ) , ) set_param( torch_layer.self_attention.value , torch.tensor(__A ).transpose(1 , 2 ).contiguous().view(-1 , __A ) , ) set_param( torch_layer.output.dense , torch.tensor(__A ).view(-1 , __A ).contiguous().transpose(0 , 1 ) , ) def A (__A : int , __A : Union[str, Any] , __A : List[str] ) -> List[Any]: """simple docstring""" UpperCAmelCase_ = weights[0][0][0] UpperCAmelCase_ = np.asarray(layer_norm_a[0] ) UpperCAmelCase_ = np.asarray(layer_norm_a[1] ) set_param( torch_block.attention.layer_norm , torch.tensor(__A ) , torch.tensor(__A ) , ) # lsh weights + output UpperCAmelCase_ = weights[0][1] if len(__A ) < 4: set_layer_weights_in_torch_lsh(__A , torch_block.attention , __A ) else: set_layer_weights_in_torch_local(__A , torch_block.attention , __A ) # intermediate weighs UpperCAmelCase_ = weights[2][0][1][2] # Chunked Feed Forward if len(__A ) == 4: UpperCAmelCase_ = intermediate_weights[2] # layernorm 2 UpperCAmelCase_ = np.asarray(intermediate_weights[0][0] ) UpperCAmelCase_ = np.asarray(intermediate_weights[0][1] ) set_param( torch_block.feed_forward.layer_norm , torch.tensor(__A ) , torch.tensor(__A ) , ) # intermediate dense UpperCAmelCase_ = np.asarray(intermediate_weights[1][0] ) UpperCAmelCase_ = np.asarray(intermediate_weights[1][1] ) set_param( torch_block.feed_forward.dense.dense , torch.tensor(__A ).transpose(0 , 1 ).contiguous() , torch.tensor(__A ) , ) # intermediate out UpperCAmelCase_ = np.asarray(intermediate_weights[4][0] ) UpperCAmelCase_ = np.asarray(intermediate_weights[4][1] ) set_param( torch_block.feed_forward.output.dense , torch.tensor(__A ).transpose(0 , 1 ).contiguous() , torch.tensor(__A ) , ) def A (__A : Optional[int] , __A : Tuple , __A : Any ) -> Tuple: """simple docstring""" UpperCAmelCase_ = torch_model.reformer # word embeds UpperCAmelCase_ = np.asarray(weights[1] ) set_param( torch_model_reformer.embeddings.word_embeddings , torch.tensor(__A ) , ) if isinstance(weights[3] , __A ): UpperCAmelCase_ = torch_model_reformer.embeddings.position_embeddings for emb_idx in range(len(position_embeddings.weights ) ): UpperCAmelCase_ = np.asarray(weights[3][emb_idx][0] ) assert ( position_embeddings.weights[emb_idx].shape == emb_weights.shape ), F"""{position_embeddings[emb_idx]} emb does not match""" UpperCAmelCase_ = nn.Parameter(torch.tensor(__A ) ) UpperCAmelCase_ = weights[5] assert len(torch_model_reformer.encoder.layers ) * 4 == len( __A ), "HF and trax model do not have the same number of layers" for layer_idx, layer in enumerate(torch_model_reformer.encoder.layers ): UpperCAmelCase_ = trax_layer_weights[4 * layer_idx : 4 * (layer_idx + 1)] set_block_weights_in_torch(__A , __A , __A ) # output layer norm UpperCAmelCase_ = np.asarray(weights[7][0] ) UpperCAmelCase_ = np.asarray(weights[7][1] ) set_param( torch_model_reformer.encoder.layer_norm , torch.tensor(__A ) , torch.tensor(__A ) , ) # output embeddings UpperCAmelCase_ = np.asarray(weights[9][0] ) UpperCAmelCase_ = np.asarray(weights[9][1] ) set_param( torch_model.lm_head.decoder , torch.tensor(__A ).transpose(0 , 1 ).contiguous() , torch.tensor(__A ) , ) def A (__A : Tuple , __A : int , __A : str ) -> Union[str, Any]: """simple docstring""" UpperCAmelCase_ = ReformerConfig.from_json_file(__A ) print(F"""Building PyTorch model from configuration: {config}""" ) UpperCAmelCase_ = ReformerModelWithLMHead(__A ) with open(__A , '''rb''' ) as f: UpperCAmelCase_ = pickle.load(__A )['''weights'''] set_model_weights_in_torch(__A , __A , config.hidden_size ) # Save pytorch-model print(F"""Save PyTorch model to {pytorch_dump_path}""" ) torch.save(model.state_dict() , __A ) if __name__ == "__main__": snake_case_ : Optional[Any] = argparse.ArgumentParser() # Required parameters parser.add_argument( "--trax_model_pkl_path", default=None, type=str, required=True, help="Path to the TensorFlow checkpoint path." ) parser.add_argument( "--config_file", default=None, type=str, required=True, help=( "The config json file corresponding to the pre-trained Reformer model. \n" "This specifies the model architecture." ), ) parser.add_argument( "--pytorch_dump_path", default=None, type=str, required=True, help="Path to the output PyTorch model." ) snake_case_ : List[Any] = parser.parse_args() convert_trax_checkpoint_to_pytorch(args.trax_model_pkl_path, args.config_file, args.pytorch_dump_path)
51
0
"""simple docstring""" def _A ( _a : str , _a : str ): """simple docstring""" A = len(_lowerCAmelCase ) + 1 A = len(_lowerCAmelCase ) + 1 # dp is a 2d matrix where dp[i][j] denotes whether prefix string of # length i of input_string matches with prefix string of length j of # given pattern. # "dp" stands for dynamic programming. A = [[0 for i in range(_lowerCAmelCase )] for j in range(_lowerCAmelCase )] # since string of zero length match pattern of zero length A = 1 # since pattern of zero length will never match with string of non-zero length for i in range(1 , _lowerCAmelCase ): A = 0 # since string of zero length will match with pattern where there # is at least one * alternatively for j in range(1 , _lowerCAmelCase ): A = dp[0][j - 2] if pattern[j - 1] == "*" else 0 # now using bottom-up approach to find for all remaining lengths for i in range(1 , _lowerCAmelCase ): for j in range(1 , _lowerCAmelCase ): if input_string[i - 1] == pattern[j - 1] or pattern[j - 1] == ".": A = dp[i - 1][j - 1] elif pattern[j - 1] == "*": if dp[i][j - 2] == 1: A = 1 elif pattern[j - 2] in (input_string[i - 1], "."): A = dp[i - 1][j] else: A = 0 else: A = 0 return bool(dp[-1][-1] ) if __name__ == "__main__": import doctest doctest.testmod() # inputing the strings # input_string = input("input a string :") # pattern = input("input a pattern :") UpperCAmelCase ='''aab''' UpperCAmelCase ='''c*a*b''' # using function to check whether given string matches the given pattern if match_pattern(input_string, pattern): print(f"""{input_string} matches the given pattern {pattern}""") else: print(f"""{input_string} does not match with the given pattern {pattern}""")
354
"""simple docstring""" from math import factorial def _A ( _a : int = 1_0_0 ): """simple docstring""" return sum(map(_a , str(factorial(_a ) ) ) ) if __name__ == "__main__": print(solution(int(input("Enter the Number: ").strip())))
77
0
from collections import OrderedDict from typing import Mapping from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig lowerCamelCase = { '''albert-base-v1''': '''https://huggingface.co/albert-base-v1/resolve/main/config.json''', '''albert-large-v1''': '''https://huggingface.co/albert-large-v1/resolve/main/config.json''', '''albert-xlarge-v1''': '''https://huggingface.co/albert-xlarge-v1/resolve/main/config.json''', '''albert-xxlarge-v1''': '''https://huggingface.co/albert-xxlarge-v1/resolve/main/config.json''', '''albert-base-v2''': '''https://huggingface.co/albert-base-v2/resolve/main/config.json''', '''albert-large-v2''': '''https://huggingface.co/albert-large-v2/resolve/main/config.json''', '''albert-xlarge-v2''': '''https://huggingface.co/albert-xlarge-v2/resolve/main/config.json''', '''albert-xxlarge-v2''': '''https://huggingface.co/albert-xxlarge-v2/resolve/main/config.json''', } class _a ( _lowercase): _a : Any = '''albert''' def __init__( self : Optional[Any] , _SCREAMING_SNAKE_CASE : List[Any]=3_0000 , _SCREAMING_SNAKE_CASE : Union[str, Any]=128 , _SCREAMING_SNAKE_CASE : Dict=4096 , _SCREAMING_SNAKE_CASE : Union[str, Any]=12 , _SCREAMING_SNAKE_CASE : List[str]=1 , _SCREAMING_SNAKE_CASE : Union[str, Any]=64 , _SCREAMING_SNAKE_CASE : Tuple=1_6384 , _SCREAMING_SNAKE_CASE : Tuple=1 , _SCREAMING_SNAKE_CASE : Any="gelu_new" , _SCREAMING_SNAKE_CASE : Optional[int]=0 , _SCREAMING_SNAKE_CASE : Dict=0 , _SCREAMING_SNAKE_CASE : Any=512 , _SCREAMING_SNAKE_CASE : Optional[int]=2 , _SCREAMING_SNAKE_CASE : Any=0.02 , _SCREAMING_SNAKE_CASE : List[Any]=1E-12 , _SCREAMING_SNAKE_CASE : Any=0.1 , _SCREAMING_SNAKE_CASE : Optional[int]="absolute" , _SCREAMING_SNAKE_CASE : Optional[Any]=0 , _SCREAMING_SNAKE_CASE : Any=2 , _SCREAMING_SNAKE_CASE : int=3 , **_SCREAMING_SNAKE_CASE : List[str] , )-> Union[str, Any]: super().__init__(pad_token_id=_SCREAMING_SNAKE_CASE , bos_token_id=_SCREAMING_SNAKE_CASE , eos_token_id=_SCREAMING_SNAKE_CASE , **_SCREAMING_SNAKE_CASE ) lowerCAmelCase__ : List[str] = vocab_size lowerCAmelCase__ : str = embedding_size lowerCAmelCase__ : str = hidden_size lowerCAmelCase__ : Optional[int] = num_hidden_layers lowerCAmelCase__ : List[str] = num_hidden_groups lowerCAmelCase__ : int = num_attention_heads lowerCAmelCase__ : Dict = inner_group_num lowerCAmelCase__ : Dict = hidden_act lowerCAmelCase__ : Union[str, Any] = intermediate_size lowerCAmelCase__ : Dict = hidden_dropout_prob lowerCAmelCase__ : str = attention_probs_dropout_prob lowerCAmelCase__ : List[str] = max_position_embeddings lowerCAmelCase__ : str = type_vocab_size lowerCAmelCase__ : str = initializer_range lowerCAmelCase__ : List[Any] = layer_norm_eps lowerCAmelCase__ : int = classifier_dropout_prob lowerCAmelCase__ : Any = position_embedding_type class _a ( _lowercase): @property def UpperCAmelCase__( self : Optional[int] )-> Mapping[str, Mapping[int, str]]: if self.task == "multiple-choice": lowerCAmelCase__ : Dict = {0: '''batch''', 1: '''choice''', 2: '''sequence'''} else: lowerCAmelCase__ : str = {0: '''batch''', 1: '''sequence'''} return OrderedDict( [ ('''input_ids''', dynamic_axis), ('''attention_mask''', dynamic_axis), ('''token_type_ids''', dynamic_axis), ] )
131
from typing import List, Optional from tokenizers import ByteLevelBPETokenizer from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import logging from .tokenization_blenderbot_small import BlenderbotSmallTokenizer lowerCamelCase = logging.get_logger(__name__) lowerCamelCase = { '''vocab_file''': '''vocab.json''', '''merges_file''': '''merges.txt''', '''tokenizer_config_file''': '''tokenizer_config.json''', } lowerCamelCase = { '''vocab_file''': { '''facebook/blenderbot_small-90M''': '''https://huggingface.co/facebook/blenderbot_small-90M/resolve/main/vocab.json''' }, '''merges_file''': { '''facebook/blenderbot_small-90M''': '''https://huggingface.co/facebook/blenderbot_small-90M/resolve/main/merges.txt''' }, '''tokenizer_config_file''': { '''facebook/blenderbot_small-90M''': ( '''https://huggingface.co/facebook/blenderbot_small-90M/resolve/main/tokenizer_config.json''' ) }, } lowerCamelCase = { '''facebook/blenderbot_small-90M''': 512, } class _a ( _lowercase): _a : Dict = VOCAB_FILES_NAMES _a : Optional[int] = PRETRAINED_VOCAB_FILES_MAP _a : Any = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES _a : Dict = BlenderbotSmallTokenizer def __init__( self : Tuple , _SCREAMING_SNAKE_CASE : int=None , _SCREAMING_SNAKE_CASE : Any=None , _SCREAMING_SNAKE_CASE : Tuple="<|endoftext|>" , _SCREAMING_SNAKE_CASE : Any="<|endoftext|>" , _SCREAMING_SNAKE_CASE : Union[str, Any]="<|endoftext|>" , _SCREAMING_SNAKE_CASE : Tuple=False , _SCREAMING_SNAKE_CASE : List[Any]=True , **_SCREAMING_SNAKE_CASE : Optional[Any] , )-> Union[str, Any]: super().__init__( ByteLevelBPETokenizer( vocab=_SCREAMING_SNAKE_CASE , merges=_SCREAMING_SNAKE_CASE , add_prefix_space=_SCREAMING_SNAKE_CASE , trim_offsets=_SCREAMING_SNAKE_CASE , ) , bos_token=_SCREAMING_SNAKE_CASE , eos_token=_SCREAMING_SNAKE_CASE , unk_token=_SCREAMING_SNAKE_CASE , **_SCREAMING_SNAKE_CASE , ) lowerCAmelCase__ : List[str] = add_prefix_space def UpperCAmelCase__( self : Tuple , _SCREAMING_SNAKE_CASE : Dict , _SCREAMING_SNAKE_CASE : Any=None )-> Optional[int]: lowerCAmelCase__ : str = [self.bos_token_id] + token_ids_a + [self.eos_token_id] if token_ids_a is None: return output return output + [self.eos_token_id] + token_ids_a + [self.eos_token_id] def UpperCAmelCase__( self : int , _SCREAMING_SNAKE_CASE : List[int] , _SCREAMING_SNAKE_CASE : Optional[List[int]] = None )-> List[int]: lowerCAmelCase__ : List[str] = [self.sep_token_id] lowerCAmelCase__ : Optional[Any] = [self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep ) * [0] return len(cls + token_ids_a + sep + sep + token_ids_a + sep ) * [0]
131
1
'''simple docstring''' from transformers import DistilBertTokenizer, DistilBertTokenizerFast from transformers.testing_utils import require_tokenizers, slow from ..bert.test_tokenization_bert import BertTokenizationTest @require_tokenizers class __UpperCamelCase ( _lowerCamelCase ): lowercase : str = DistilBertTokenizer lowercase : Tuple = DistilBertTokenizerFast lowercase : List[str] = True @slow def a__ ( self :Union[str, Any] ): snake_case_ : str = DistilBertTokenizer.from_pretrained("""distilbert-base-uncased""" ) snake_case_ : Dict = tokenizer.encode("""sequence builders""" ,add_special_tokens=lowercase_ ) snake_case_ : List[Any] = tokenizer.encode("""multi-sequence build""" ,add_special_tokens=lowercase_ ) snake_case_ : str = tokenizer.build_inputs_with_special_tokens(lowercase_ ) snake_case_ : List[str] = tokenizer.build_inputs_with_special_tokens(lowercase_ ,lowercase_ ) assert encoded_sentence == [tokenizer.cls_token_id] + text + [tokenizer.sep_token_id] assert encoded_pair == [tokenizer.cls_token_id] + text + [tokenizer.sep_token_id] + text_a + [ tokenizer.sep_token_id ]
355
'''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 __UpperCamelCase ( nn.Module ): def __init__( self :Any ,_UpperCamelCase :int ,_UpperCamelCase :int ,_UpperCamelCase :int ,_UpperCamelCase :int=0.0 ,_UpperCamelCase :Optional[int] = None ,_UpperCamelCase :str = "geglu" ,_UpperCamelCase :Optional[int] = None ,_UpperCamelCase :bool = False ,_UpperCamelCase :bool = False ,_UpperCamelCase :bool = False ,_UpperCamelCase :bool = False ,_UpperCamelCase :bool = True ,_UpperCamelCase :str = "layer_norm" ,_UpperCamelCase :bool = False ,): super().__init__() snake_case_ : Any = only_cross_attention snake_case_ : Union[str, Any] = (num_embeds_ada_norm is not None) and norm_type == """ada_norm_zero""" snake_case_ : Any = (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: snake_case_ : Dict = AdaLayerNorm(_UpperCamelCase ,_UpperCamelCase ) elif self.use_ada_layer_norm_zero: snake_case_ : str = AdaLayerNormZero(_UpperCamelCase ,_UpperCamelCase ) else: snake_case_ : List[Any] = nn.LayerNorm(_UpperCamelCase ,elementwise_affine=_UpperCamelCase ) snake_case_ : List[str] = Attention( query_dim=_UpperCamelCase ,heads=_UpperCamelCase ,dim_head=_UpperCamelCase ,dropout=_UpperCamelCase ,bias=_UpperCamelCase ,cross_attention_dim=cross_attention_dim if only_cross_attention else None ,upcast_attention=_UpperCamelCase ,) # 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. snake_case_ : str = ( AdaLayerNorm(_UpperCamelCase ,_UpperCamelCase ) if self.use_ada_layer_norm else nn.LayerNorm(_UpperCamelCase ,elementwise_affine=_UpperCamelCase ) ) snake_case_ : List[str] = Attention( query_dim=_UpperCamelCase ,cross_attention_dim=cross_attention_dim if not double_self_attention else None ,heads=_UpperCamelCase ,dim_head=_UpperCamelCase ,dropout=_UpperCamelCase ,bias=_UpperCamelCase ,upcast_attention=_UpperCamelCase ,) # is self-attn if encoder_hidden_states is none else: snake_case_ : Any = None snake_case_ : Optional[Any] = None # 3. Feed-forward snake_case_ : List[str] = nn.LayerNorm(_UpperCamelCase ,elementwise_affine=_UpperCamelCase ) snake_case_ : Union[str, Any] = FeedForward(_UpperCamelCase ,dropout=_UpperCamelCase ,activation_fn=_UpperCamelCase ,final_dropout=_UpperCamelCase ) # let chunk size default to None snake_case_ : Optional[int] = None snake_case_ : Dict = 0 def a__ ( self :List[Any] ,_UpperCamelCase :Optional[int] ,_UpperCamelCase :int ): # Sets chunk feed-forward snake_case_ : Optional[Any] = chunk_size snake_case_ : Optional[Any] = dim def a__ ( self :List[str] ,_UpperCamelCase :torch.FloatTensor ,_UpperCamelCase :Optional[torch.FloatTensor] = None ,_UpperCamelCase :Optional[torch.FloatTensor] = None ,_UpperCamelCase :Optional[torch.FloatTensor] = None ,_UpperCamelCase :Optional[torch.LongTensor] = None ,_UpperCamelCase :Dict[str, Any] = None ,_UpperCamelCase :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: snake_case_ : Optional[Any] = self.norma(_UpperCamelCase ,_UpperCamelCase ) elif self.use_ada_layer_norm_zero: snake_case_ , snake_case_ , snake_case_ , snake_case_ , snake_case_ : Union[str, Any] = self.norma( _UpperCamelCase ,_UpperCamelCase ,_UpperCamelCase ,hidden_dtype=hidden_states.dtype ) else: snake_case_ : Optional[int] = self.norma(_UpperCamelCase ) snake_case_ : int = cross_attention_kwargs if cross_attention_kwargs is not None else {} snake_case_ : Union[str, Any] = self.attna( _UpperCamelCase ,encoder_hidden_states=encoder_hidden_states if self.only_cross_attention else None ,attention_mask=_UpperCamelCase ,**_UpperCamelCase ,) if self.use_ada_layer_norm_zero: snake_case_ : Union[str, Any] = gate_msa.unsqueeze(1 ) * attn_output snake_case_ : Union[str, Any] = attn_output + hidden_states # 2. Cross-Attention if self.attna is not None: snake_case_ : Any = ( self.norma(_UpperCamelCase ,_UpperCamelCase ) if self.use_ada_layer_norm else self.norma(_UpperCamelCase ) ) snake_case_ : List[Any] = self.attna( _UpperCamelCase ,encoder_hidden_states=_UpperCamelCase ,attention_mask=_UpperCamelCase ,**_UpperCamelCase ,) snake_case_ : Tuple = attn_output + hidden_states # 3. Feed-forward snake_case_ : Optional[Any] = self.norma(_UpperCamelCase ) if self.use_ada_layer_norm_zero: snake_case_ : Dict = 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`.''' ) snake_case_ : Union[str, Any] = norm_hidden_states.shape[self._chunk_dim] // self._chunk_size snake_case_ : int = torch.cat( [self.ff(_UpperCamelCase ) for hid_slice in norm_hidden_states.chunk(_UpperCamelCase ,dim=self._chunk_dim )] ,dim=self._chunk_dim ,) else: snake_case_ : List[str] = self.ff(_UpperCamelCase ) if self.use_ada_layer_norm_zero: snake_case_ : Union[str, Any] = gate_mlp.unsqueeze(1 ) * ff_output snake_case_ : Any = ff_output + hidden_states return hidden_states class __UpperCamelCase ( nn.Module ): def __init__( self :Dict ,_UpperCamelCase :int ,_UpperCamelCase :Optional[int] = None ,_UpperCamelCase :int = 4 ,_UpperCamelCase :float = 0.0 ,_UpperCamelCase :str = "geglu" ,_UpperCamelCase :bool = False ,): super().__init__() snake_case_ : Tuple = int(dim * mult ) snake_case_ : Optional[int] = dim_out if dim_out is not None else dim if activation_fn == "gelu": snake_case_ : Any = GELU(_UpperCamelCase ,_UpperCamelCase ) if activation_fn == "gelu-approximate": snake_case_ : Tuple = GELU(_UpperCamelCase ,_UpperCamelCase ,approximate="""tanh""" ) elif activation_fn == "geglu": snake_case_ : Dict = GEGLU(_UpperCamelCase ,_UpperCamelCase ) elif activation_fn == "geglu-approximate": snake_case_ : Optional[Any] = ApproximateGELU(_UpperCamelCase ,_UpperCamelCase ) snake_case_ : Dict = nn.ModuleList([] ) # project in self.net.append(_UpperCamelCase ) # project dropout self.net.append(nn.Dropout(_UpperCamelCase ) ) # project out self.net.append(nn.Linear(_UpperCamelCase ,_UpperCamelCase ) ) # FF as used in Vision Transformer, MLP-Mixer, etc. have a final dropout if final_dropout: self.net.append(nn.Dropout(_UpperCamelCase ) ) def a__ ( self :Tuple ,_UpperCamelCase :Union[str, Any] ): for module in self.net: snake_case_ : Tuple = module(_UpperCamelCase ) return hidden_states class __UpperCamelCase ( nn.Module ): def __init__( self :Optional[Any] ,_UpperCamelCase :int ,_UpperCamelCase :int ,_UpperCamelCase :str = "none" ): super().__init__() snake_case_ : Union[str, Any] = nn.Linear(_UpperCamelCase ,_UpperCamelCase ) snake_case_ : Optional[Any] = approximate def a__ ( self :str ,_UpperCamelCase :int ): if gate.device.type != "mps": return F.gelu(_UpperCamelCase ,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 a__ ( self :Optional[int] ,_UpperCamelCase :Optional[Any] ): snake_case_ : Optional[Any] = self.proj(_UpperCamelCase ) snake_case_ : int = self.gelu(_UpperCamelCase ) return hidden_states class __UpperCamelCase ( nn.Module ): def __init__( self :List[Any] ,_UpperCamelCase :int ,_UpperCamelCase :int ): super().__init__() snake_case_ : str = nn.Linear(_UpperCamelCase ,dim_out * 2 ) def a__ ( self :Dict ,_UpperCamelCase :List[str] ): if gate.device.type != "mps": return F.gelu(_UpperCamelCase ) # mps: gelu is not implemented for float16 return F.gelu(gate.to(dtype=torch.floataa ) ).to(dtype=gate.dtype ) def a__ ( self :Optional[Any] ,_UpperCamelCase :Optional[int] ): snake_case_ , snake_case_ : Dict = self.proj(_UpperCamelCase ).chunk(2 ,dim=-1 ) return hidden_states * self.gelu(_UpperCamelCase ) class __UpperCamelCase ( nn.Module ): def __init__( self :List[str] ,_UpperCamelCase :int ,_UpperCamelCase :int ): super().__init__() snake_case_ : int = nn.Linear(_UpperCamelCase ,_UpperCamelCase ) def a__ ( self :Optional[int] ,_UpperCamelCase :Optional[int] ): snake_case_ : int = self.proj(_UpperCamelCase ) return x * torch.sigmoid(1.7_02 * x ) class __UpperCamelCase ( nn.Module ): def __init__( self :int ,_UpperCamelCase :str ,_UpperCamelCase :List[Any] ): super().__init__() snake_case_ : int = nn.Embedding(_UpperCamelCase ,_UpperCamelCase ) snake_case_ : Union[str, Any] = nn.SiLU() snake_case_ : Any = nn.Linear(_UpperCamelCase ,embedding_dim * 2 ) snake_case_ : Dict = nn.LayerNorm(_UpperCamelCase ,elementwise_affine=_UpperCamelCase ) def a__ ( self :int ,_UpperCamelCase :List[str] ,_UpperCamelCase :int ): snake_case_ : Union[str, Any] = self.linear(self.silu(self.emb(_UpperCamelCase ) ) ) snake_case_ , snake_case_ : Tuple = torch.chunk(_UpperCamelCase ,2 ) snake_case_ : Tuple = self.norm(_UpperCamelCase ) * (1 + scale) + shift return x class __UpperCamelCase ( nn.Module ): def __init__( self :List[str] ,_UpperCamelCase :Tuple ,_UpperCamelCase :int ): super().__init__() snake_case_ : int = CombinedTimestepLabelEmbeddings(_UpperCamelCase ,_UpperCamelCase ) snake_case_ : int = nn.SiLU() snake_case_ : List[str] = nn.Linear(_UpperCamelCase ,6 * embedding_dim ,bias=_UpperCamelCase ) snake_case_ : str = nn.LayerNorm(_UpperCamelCase ,elementwise_affine=_UpperCamelCase ,eps=1E-6 ) def a__ ( self :Union[str, Any] ,_UpperCamelCase :Any ,_UpperCamelCase :Tuple ,_UpperCamelCase :Optional[Any] ,_UpperCamelCase :str=None ): snake_case_ : Union[str, Any] = self.linear(self.silu(self.emb(_UpperCamelCase ,_UpperCamelCase ,hidden_dtype=_UpperCamelCase ) ) ) snake_case_ , snake_case_ , snake_case_ , snake_case_ , snake_case_ , snake_case_ : Any = emb.chunk(6 ,dim=1 ) snake_case_ : str = self.norm(_UpperCamelCase ) * (1 + scale_msa[:, None]) + shift_msa[:, None] return x, gate_msa, shift_mlp, scale_mlp, gate_mlp class __UpperCamelCase ( nn.Module ): def __init__( self :Optional[int] ,_UpperCamelCase :int ,_UpperCamelCase :int ,_UpperCamelCase :int ,_UpperCamelCase :Optional[str] = None ,_UpperCamelCase :float = 1E-5 ): super().__init__() snake_case_ : Optional[int] = num_groups snake_case_ : List[Any] = eps if act_fn is None: snake_case_ : int = None else: snake_case_ : Dict = get_activation(_UpperCamelCase ) snake_case_ : Optional[int] = nn.Linear(_UpperCamelCase ,out_dim * 2 ) def a__ ( self :List[Any] ,_UpperCamelCase :Optional[Any] ,_UpperCamelCase :List[str] ): if self.act: snake_case_ : Any = self.act(_UpperCamelCase ) snake_case_ : Optional[int] = self.linear(_UpperCamelCase ) snake_case_ : Dict = emb[:, :, None, None] snake_case_ , snake_case_ : str = emb.chunk(2 ,dim=1 ) snake_case_ : str = F.group_norm(_UpperCamelCase ,self.num_groups ,eps=self.eps ) snake_case_ : List[str] = x * (1 + scale) + shift return x
8
0
'''simple docstring''' from __future__ import annotations from math import pi from typing import Protocol import matplotlib.pyplot as plt import numpy as np class __UpperCAmelCase ( _lowerCamelCase ): def lowerCamelCase ( self , lowerCAmelCase_ ): """simple docstring""" return 0.0 def SCREAMING_SNAKE_CASE__ ( __A , __A ) -> tuple[int | float, int | float]: _snake_case = min([-20, np.min(fft_results[1 : samplerate // 2 - 1] )] ) _snake_case = max([20, np.max(fft_results[1 : samplerate // 2 - 1] )] ) return lowest, highest def SCREAMING_SNAKE_CASE__ ( __A , __A ) -> None: _snake_case = 512 _snake_case = [1] + [0] * (size - 1) _snake_case = [filter_type.process(__A ) for item in inputs] _snake_case = [0] * (samplerate - size) # zero-padding outputs += filler _snake_case = np.abs(np.fft.fft(__A ) ) _snake_case = 20 * np.logaa(__A ) # Frequencies on log scale from 24 to nyquist frequency plt.xlim(24 , samplerate / 2 - 1 ) plt.xlabel('Frequency (Hz)' ) plt.xscale('log' ) # Display within reasonable bounds _snake_case = get_bounds(__A , __A ) plt.ylim(max([-80, bounds[0]] ) , min([80, bounds[1]] ) ) plt.ylabel('Gain (dB)' ) plt.plot(__A ) plt.show() def SCREAMING_SNAKE_CASE__ ( __A , __A ) -> None: _snake_case = 512 _snake_case = [1] + [0] * (size - 1) _snake_case = [filter_type.process(__A ) for item in inputs] _snake_case = [0] * (samplerate - size) # zero-padding outputs += filler _snake_case = np.angle(np.fft.fft(__A ) ) # Frequencies on log scale from 24 to nyquist frequency plt.xlim(24 , samplerate / 2 - 1 ) plt.xlabel('Frequency (Hz)' ) plt.xscale('log' ) plt.ylim(-2 * pi , 2 * pi ) plt.ylabel('Phase shift (Radians)' ) plt.plot(np.unwrap(__A , -2 * pi ) ) plt.show()
42
from typing import List, Optional, Union from ...processing_utils import ProcessorMixin from ...tokenization_utils_base import BatchEncoding, PaddingStrategy, PreTokenizedInput, TextInput, TruncationStrategy from ...utils import TensorType class __lowerCamelCase ( snake_case__): """simple docstring""" UpperCamelCase__ = ["image_processor", "tokenizer"] UpperCamelCase__ = "Pix2StructImageProcessor" UpperCamelCase__ = ("T5Tokenizer", "T5TokenizerFast") def __init__( self , UpperCAmelCase , UpperCAmelCase ): """simple docstring""" _UpperCAmelCase = False super().__init__(UpperCAmelCase , UpperCAmelCase ) def __call__( self , UpperCAmelCase=None , UpperCAmelCase = None , UpperCAmelCase = True , UpperCAmelCase = False , UpperCAmelCase = None , UpperCAmelCase = None , UpperCAmelCase = 2048 , UpperCAmelCase = 0 , UpperCAmelCase = None , UpperCAmelCase = None , UpperCAmelCase = False , UpperCAmelCase = False , UpperCAmelCase = False , UpperCAmelCase = False , UpperCAmelCase = False , UpperCAmelCase = True , UpperCAmelCase = None , **UpperCAmelCase , ): """simple docstring""" if images is None and text is None: raise ValueError('You have to specify either images or text.' ) # Get only text if images is None and not self.image_processor.is_vqa: _UpperCAmelCase = self.tokenizer _UpperCAmelCase = self.tokenizer( text=UpperCAmelCase , add_special_tokens=UpperCAmelCase , padding=UpperCAmelCase , truncation=UpperCAmelCase , max_length=UpperCAmelCase , stride=UpperCAmelCase , pad_to_multiple_of=UpperCAmelCase , return_attention_mask=UpperCAmelCase , return_overflowing_tokens=UpperCAmelCase , return_special_tokens_mask=UpperCAmelCase , return_offsets_mapping=UpperCAmelCase , return_token_type_ids=UpperCAmelCase , return_length=UpperCAmelCase , verbose=UpperCAmelCase , return_tensors=UpperCAmelCase , **UpperCAmelCase , ) return text_encoding if not self.image_processor.is_vqa: # add pixel_values _UpperCAmelCase = self.image_processor( UpperCAmelCase , return_tensors=UpperCAmelCase , max_patches=UpperCAmelCase , **UpperCAmelCase ) else: # add pixel_values and bbox _UpperCAmelCase = self.image_processor( UpperCAmelCase , return_tensors=UpperCAmelCase , max_patches=UpperCAmelCase , header_text=UpperCAmelCase , **UpperCAmelCase ) if text is not None and not self.image_processor.is_vqa: _UpperCAmelCase = self.tokenizer( text=UpperCAmelCase , add_special_tokens=UpperCAmelCase , padding=UpperCAmelCase , truncation=UpperCAmelCase , max_length=UpperCAmelCase , stride=UpperCAmelCase , pad_to_multiple_of=UpperCAmelCase , return_attention_mask=UpperCAmelCase , return_overflowing_tokens=UpperCAmelCase , return_special_tokens_mask=UpperCAmelCase , return_offsets_mapping=UpperCAmelCase , return_token_type_ids=UpperCAmelCase , return_length=UpperCAmelCase , verbose=UpperCAmelCase , return_tensors=UpperCAmelCase , **UpperCAmelCase , ) if "attention_mask" in text_encoding: _UpperCAmelCase = text_encoding.pop('attention_mask' ) if "input_ids" in text_encoding: _UpperCAmelCase = text_encoding.pop('input_ids' ) else: _UpperCAmelCase = None if text_encoding is not None: encoding_image_processor.update(UpperCAmelCase ) return encoding_image_processor def UpperCamelCase ( self , *UpperCAmelCase , **UpperCAmelCase ): """simple docstring""" return self.tokenizer.batch_decode(*UpperCAmelCase , **UpperCAmelCase ) def UpperCamelCase ( self , *UpperCAmelCase , **UpperCAmelCase ): """simple docstring""" return self.tokenizer.decode(*UpperCAmelCase , **UpperCAmelCase ) @property def UpperCamelCase ( self ): """simple docstring""" _UpperCAmelCase = self.tokenizer.model_input_names _UpperCAmelCase = self.image_processor.model_input_names return list(dict.fromkeys(tokenizer_input_names + image_processor_input_names ) )
39
0
'''simple docstring''' from maths.prime_factors import prime_factors def SCREAMING_SNAKE_CASE( __lowercase ) -> int: if not isinstance(__lowercase , __lowercase ): A: Any = F"""Input value of [number={number}] must be an integer""" raise TypeError(__lowercase ) if number < 1: raise ValueError('''Input must be a positive integer''' ) return -1 if len(prime_factors(__lowercase ) ) % 2 else 1 if __name__ == "__main__": import doctest doctest.testmod()
334
'''simple docstring''' import os from distutils.util import strtobool def SCREAMING_SNAKE_CASE( __lowercase , __lowercase ) -> List[Any]: for e in env_keys: A: Dict = int(os.environ.get(__lowercase , -1 ) ) if val >= 0: return val return default def SCREAMING_SNAKE_CASE( __lowercase , __lowercase=False ) -> List[str]: A: str = os.environ.get(__lowercase , str(__lowercase ) ) return strtobool(__lowercase ) == 1 # As its name indicates `strtobool` actually returns an int... def SCREAMING_SNAKE_CASE( __lowercase , __lowercase="no" ) -> str: A: Optional[int] = os.environ.get(__lowercase , str(__lowercase ) ) return value
334
1
'''simple docstring''' def _lowerCamelCase ( lowercase : float , lowercase : int ) -> float: if digit_amount > 0: return round(number - int(lowercase ) , lowercase ) return number - int(lowercase ) if __name__ == "__main__": print(decimal_isolate(1.53, 0)) print(decimal_isolate(35.345, 1)) print(decimal_isolate(35.345, 2)) print(decimal_isolate(35.345, 3)) print(decimal_isolate(-14.789, 3)) print(decimal_isolate(0, 2)) print(decimal_isolate(-14.123, 1)) print(decimal_isolate(-14.123, 2)) print(decimal_isolate(-14.123, 3))
63
import math def snake_case__ ( SCREAMING_SNAKE_CASE_ : int ): '''simple docstring''' lowercase__ : Optional[Any] = [] lowercase__ : str = 2 lowercase__ : Optional[Any] = int(math.sqrt(SCREAMING_SNAKE_CASE_ ) ) # Size of every segment lowercase__ : Dict = [True] * (end + 1) lowercase__ : Union[str, Any] = [] while start <= end: if temp[start] is True: in_prime.append(SCREAMING_SNAKE_CASE_ ) for i in range(start * start , end + 1 , SCREAMING_SNAKE_CASE_ ): lowercase__ : int = False start += 1 prime += in_prime lowercase__ : Optional[int] = end + 1 lowercase__ : List[str] = min(2 * end , SCREAMING_SNAKE_CASE_ ) while low <= n: lowercase__ : str = [True] * (high - low + 1) for each in in_prime: lowercase__ : str = math.floor(low / each ) * each if t < low: t += each for j in range(SCREAMING_SNAKE_CASE_ , high + 1 , SCREAMING_SNAKE_CASE_ ): lowercase__ : Optional[Any] = False for j in range(len(SCREAMING_SNAKE_CASE_ ) ): if temp[j] is True: prime.append(j + low ) lowercase__ : Optional[Any] = high + 1 lowercase__ : Optional[int] = min(high + end , SCREAMING_SNAKE_CASE_ ) return prime print(sieve(10**6))
214
0
'''simple docstring''' import json import os from pathlib import Path from shutil import copyfile from typing import Any, Dict, List, Optional, Tuple, Union import sentencepiece from ...tokenization_utils import PreTrainedTokenizer from ...utils import logging _UpperCamelCase = logging.get_logger(__name__) _UpperCamelCase = '''▁''' _UpperCamelCase = { '''vocab_file''': '''vocab.json''', '''spm_file''': '''sentencepiece.bpe.model''', } _UpperCamelCase = { '''vocab_file''': { '''facebook/s2t-small-librispeech-asr''': ( '''https://huggingface.co/facebook/s2t-small-librispeech-asr/resolve/main/vocab.json''' ), }, '''spm_file''': { '''facebook/s2t-small-librispeech-asr''': ( '''https://huggingface.co/facebook/s2t-small-librispeech-asr/resolve/main/sentencepiece.bpe.model''' ) }, } _UpperCamelCase = { '''facebook/s2t-small-librispeech-asr''': 1024, } _UpperCamelCase = ['''pt''', '''fr''', '''ru''', '''nl''', '''ro''', '''it''', '''es''', '''de'''] _UpperCamelCase = {'''mustc''': MUSTC_LANGS} class _A ( __SCREAMING_SNAKE_CASE ): _SCREAMING_SNAKE_CASE : Tuple = VOCAB_FILES_NAMES _SCREAMING_SNAKE_CASE : Any = PRETRAINED_VOCAB_FILES_MAP _SCREAMING_SNAKE_CASE : List[Any] = MAX_MODEL_INPUT_SIZES _SCREAMING_SNAKE_CASE : str = ["input_ids", "attention_mask"] _SCREAMING_SNAKE_CASE : List[int] = [] def __init__( self , __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase="<s>" , __UpperCAmelCase="</s>" , __UpperCAmelCase="<pad>" , __UpperCAmelCase="<unk>" , __UpperCAmelCase=False , __UpperCAmelCase=False , __UpperCAmelCase=None , __UpperCAmelCase=None , __UpperCAmelCase = None , **__UpperCAmelCase , ) -> None: '''simple docstring''' __UpperCAmelCase : Optional[Any] = {} if sp_model_kwargs is None else sp_model_kwargs super().__init__( bos_token=__UpperCAmelCase , eos_token=__UpperCAmelCase , unk_token=__UpperCAmelCase , pad_token=__UpperCAmelCase , do_upper_case=__UpperCAmelCase , do_lower_case=__UpperCAmelCase , tgt_lang=__UpperCAmelCase , lang_codes=__UpperCAmelCase , sp_model_kwargs=self.sp_model_kwargs , **__UpperCAmelCase , ) __UpperCAmelCase : Dict = do_upper_case __UpperCAmelCase : List[Any] = do_lower_case __UpperCAmelCase : List[str] = load_json(__UpperCAmelCase ) __UpperCAmelCase : Optional[Any] = {v: k for k, v in self.encoder.items()} __UpperCAmelCase : int = spm_file __UpperCAmelCase : str = load_spm(__UpperCAmelCase , self.sp_model_kwargs ) if lang_codes is not None: __UpperCAmelCase : Dict = lang_codes __UpperCAmelCase : Dict = LANGUAGES[lang_codes] __UpperCAmelCase : List[Any] = [f'<lang:{lang}>' for lang in self.langs] __UpperCAmelCase : Union[str, Any] = {lang: self.sp_model.PieceToId(f'<lang:{lang}>' ) for lang in self.langs} __UpperCAmelCase : Any = self.lang_tokens __UpperCAmelCase : int = tgt_lang if tgt_lang is not None else self.langs[0] self.set_tgt_lang_special_tokens(self._tgt_lang ) else: __UpperCAmelCase : int = {} @property def __A ( self ) -> int: '''simple docstring''' return len(self.encoder ) @property def __A ( self ) -> str: '''simple docstring''' return self._tgt_lang @tgt_lang.setter def __A ( self , __UpperCAmelCase ) -> None: '''simple docstring''' __UpperCAmelCase : Union[str, Any] = new_tgt_lang self.set_tgt_lang_special_tokens(__UpperCAmelCase ) def __A ( self , __UpperCAmelCase ) -> None: '''simple docstring''' __UpperCAmelCase : int = self.lang_code_to_id[tgt_lang] __UpperCAmelCase : str = [lang_code_id] def __A ( self , __UpperCAmelCase ) -> List[str]: '''simple docstring''' return self.sp_model.encode(__UpperCAmelCase , out_type=__UpperCAmelCase ) def __A ( self , __UpperCAmelCase ) -> Any: '''simple docstring''' return self.encoder.get(__UpperCAmelCase , self.encoder[self.unk_token] ) def __A ( self , __UpperCAmelCase ) -> str: '''simple docstring''' return self.decoder.get(__UpperCAmelCase , self.unk_token ) def __A ( self , __UpperCAmelCase ) -> str: '''simple docstring''' __UpperCAmelCase : List[str] = [] __UpperCAmelCase : Any = """""" for token in tokens: # make sure that special tokens are not decoded using sentencepiece model if token in self.all_special_tokens: __UpperCAmelCase : int = self.sp_model.decode(__UpperCAmelCase ) out_string += (decoded.upper() if self.do_upper_case else decoded) + token + " " __UpperCAmelCase : Tuple = [] else: current_sub_tokens.append(__UpperCAmelCase ) __UpperCAmelCase : Optional[int] = self.sp_model.decode(__UpperCAmelCase ) out_string += decoded.upper() if self.do_upper_case else decoded return out_string.strip() def __A ( self , __UpperCAmelCase , __UpperCAmelCase=None ) -> List[int]: '''simple docstring''' if token_ids_a is None: return self.prefix_tokens + token_ids_a + [self.eos_token_id] # We don't expect to process pairs, but leave the pair logic for API consistency return self.prefix_tokens + token_ids_a + token_ids_a + [self.eos_token_id] def __A ( self , __UpperCAmelCase , __UpperCAmelCase = None , __UpperCAmelCase = False ) -> List[int]: '''simple docstring''' if already_has_special_tokens: return super().get_special_tokens_mask( token_ids_a=__UpperCAmelCase , token_ids_a=__UpperCAmelCase , already_has_special_tokens=__UpperCAmelCase ) __UpperCAmelCase : Optional[Any] = [1] * len(self.prefix_tokens ) __UpperCAmelCase : Optional[int] = [1] if token_ids_a is None: return prefix_ones + ([0] * len(__UpperCAmelCase )) + suffix_ones return prefix_ones + ([0] * len(__UpperCAmelCase )) + ([0] * len(__UpperCAmelCase )) + suffix_ones def __A ( self ) -> Dict: '''simple docstring''' __UpperCAmelCase : Optional[Any] = self.encoder.copy() vocab.update(self.added_tokens_encoder ) return vocab def __getstate__( self ) -> Dict: '''simple docstring''' __UpperCAmelCase : Dict = self.__dict__.copy() __UpperCAmelCase : Dict = None return state def __setstate__( self , __UpperCAmelCase ) -> None: '''simple docstring''' __UpperCAmelCase : Any = d # for backward compatibility if not hasattr(self , """sp_model_kwargs""" ): __UpperCAmelCase : List[Any] = {} __UpperCAmelCase : Any = load_spm(self.spm_file , self.sp_model_kwargs ) def __A ( self , __UpperCAmelCase , __UpperCAmelCase = None ) -> Tuple[str]: '''simple docstring''' __UpperCAmelCase : List[str] = Path(__UpperCAmelCase ) assert save_dir.is_dir(), f'{save_directory} should be a directory' __UpperCAmelCase : List[Any] = save_dir / ( (filename_prefix + """-""" if filename_prefix else """""") + self.vocab_files_names["""vocab_file"""] ) __UpperCAmelCase : Union[str, Any] = save_dir / ( (filename_prefix + """-""" if filename_prefix else """""") + self.vocab_files_names["""spm_file"""] ) save_json(self.encoder , __UpperCAmelCase ) if os.path.abspath(self.spm_file ) != os.path.abspath(__UpperCAmelCase ) and os.path.isfile(self.spm_file ): copyfile(self.spm_file , __UpperCAmelCase ) elif not os.path.isfile(self.spm_file ): with open(__UpperCAmelCase , """wb""" ) as fi: __UpperCAmelCase : Any = self.sp_model.serialized_model_proto() fi.write(__UpperCAmelCase ) return (str(__UpperCAmelCase ), str(__UpperCAmelCase )) def lowercase_ ( lowerCAmelCase__ : str , lowerCAmelCase__ : Dict[str, Any] ): """simple docstring""" __UpperCAmelCase : int = sentencepiece.SentencePieceProcessor(**lowerCAmelCase__ ) spm.Load(str(lowerCAmelCase__ ) ) return spm def lowercase_ ( lowerCAmelCase__ : str ): """simple docstring""" with open(lowerCAmelCase__ , """r""" ) as f: return json.load(lowerCAmelCase__ ) def lowercase_ ( lowerCAmelCase__ : Dict , lowerCAmelCase__ : str ): """simple docstring""" with open(lowerCAmelCase__ , """w""" ) as f: json.dump(lowerCAmelCase__ , lowerCAmelCase__ , indent=2 )
16
'''simple docstring''' import numpy as np import torch from torch.utils.data import DataLoader from accelerate.utils.dataclasses import DistributedType class _A : def __init__( self , __UpperCAmelCase=2 , __UpperCAmelCase=3 , __UpperCAmelCase=64 , __UpperCAmelCase=None ) -> Optional[Any]: '''simple docstring''' __UpperCAmelCase : str = np.random.default_rng(__UpperCAmelCase ) __UpperCAmelCase : List[str] = length __UpperCAmelCase : List[Any] = rng.normal(size=(length,) ).astype(np.floataa ) __UpperCAmelCase : Union[str, Any] = a * self.x + b + rng.normal(scale=0.1 , size=(length,) ).astype(np.floataa ) def __len__( self ) -> Dict: '''simple docstring''' return self.length def __getitem__( self , __UpperCAmelCase ) -> List[str]: '''simple docstring''' return {"x": self.x[i], "y": self.y[i]} class _A ( torch.nn.Module ): def __init__( self , __UpperCAmelCase=0 , __UpperCAmelCase=0 , __UpperCAmelCase=False ) -> int: '''simple docstring''' super().__init__() __UpperCAmelCase : List[Any] = torch.nn.Parameter(torch.tensor([2, 3] ).float() ) __UpperCAmelCase : Optional[Any] = torch.nn.Parameter(torch.tensor([2, 3] ).float() ) __UpperCAmelCase : Any = True def __A ( self , __UpperCAmelCase=None ) -> str: '''simple docstring''' if self.first_batch: print(f'Model dtype: {self.a.dtype}, {self.b.dtype}. Input dtype: {x.dtype}' ) __UpperCAmelCase : Optional[int] = False return x * self.a[0] + self.b[0] class _A ( torch.nn.Module ): def __init__( self , __UpperCAmelCase=0 , __UpperCAmelCase=0 , __UpperCAmelCase=False ) -> Optional[Any]: '''simple docstring''' super().__init__() __UpperCAmelCase : Tuple = torch.nn.Parameter(torch.tensor(__UpperCAmelCase ).float() ) __UpperCAmelCase : List[str] = torch.nn.Parameter(torch.tensor(__UpperCAmelCase ).float() ) __UpperCAmelCase : str = True def __A ( self , __UpperCAmelCase=None ) -> Tuple: '''simple docstring''' if self.first_batch: print(f'Model dtype: {self.a.dtype}, {self.b.dtype}. Input dtype: {x.dtype}' ) __UpperCAmelCase : int = False return x * self.a + self.b def lowercase_ ( lowerCAmelCase__ : Optional[int] , lowerCAmelCase__ : int = 16 ): """simple docstring""" from datasets import load_dataset from transformers import AutoTokenizer __UpperCAmelCase : int = AutoTokenizer.from_pretrained("""bert-base-cased""" ) __UpperCAmelCase : List[str] = {"""train""": """tests/test_samples/MRPC/train.csv""", """validation""": """tests/test_samples/MRPC/dev.csv"""} __UpperCAmelCase : Tuple = load_dataset("""csv""" , data_files=lowerCAmelCase__ ) __UpperCAmelCase : Optional[Any] = datasets["""train"""].unique("""label""" ) __UpperCAmelCase : str = {v: i for i, v in enumerate(lowerCAmelCase__ )} def tokenize_function(lowerCAmelCase__ : Optional[Any] ): # max_length=None => use the model max length (it's actually the default) __UpperCAmelCase : List[Any] = tokenizer( examples["""sentence1"""] , examples["""sentence2"""] , truncation=lowerCAmelCase__ , max_length=lowerCAmelCase__ , padding="""max_length""" ) if "label" in examples: __UpperCAmelCase : Optional[Any] = [label_to_id[l] for l in examples["""label"""]] return outputs # Apply the method we just defined to all the examples in all the splits of the dataset __UpperCAmelCase : Tuple = datasets.map( lowerCAmelCase__ , batched=lowerCAmelCase__ , remove_columns=["""sentence1""", """sentence2""", """label"""] , ) def collate_fn(lowerCAmelCase__ : Any ): # 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(lowerCAmelCase__ , padding="""max_length""" , max_length=128 , return_tensors="""pt""" ) return tokenizer.pad(lowerCAmelCase__ , padding="""longest""" , return_tensors="""pt""" ) # Instantiate dataloaders. __UpperCAmelCase : Optional[Any] = DataLoader(tokenized_datasets["""train"""] , shuffle=lowerCAmelCase__ , collate_fn=lowerCAmelCase__ , batch_size=2 ) __UpperCAmelCase : List[Any] = DataLoader(tokenized_datasets["""validation"""] , shuffle=lowerCAmelCase__ , collate_fn=lowerCAmelCase__ , batch_size=1 ) return train_dataloader, eval_dataloader
16
1
import os import numpy import onnx def lowerCAmelCase__ ( SCREAMING_SNAKE_CASE_: str , SCREAMING_SNAKE_CASE_: int ) -> Optional[int]: '''simple docstring''' A__ = a.name A__ = b.name A__ = "" A__ = "" A__ = a == b A__ = name_a A__ = name_b return res def lowerCAmelCase__ ( SCREAMING_SNAKE_CASE_: Union[str, Any] , SCREAMING_SNAKE_CASE_: List[Any] , SCREAMING_SNAKE_CASE_: Optional[Any] ) -> Tuple: '''simple docstring''' for i, input_name in enumerate(node_proto.input ): if input_name == name: node_proto.input.insert(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) node_proto.input.pop(i + 1 ) if node_proto.op_type == "If": _graph_replace_input_with(node_proto.attribute[0].g , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) _graph_replace_input_with(node_proto.attribute[1].g , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) if node_proto.op_type == "Loop": _graph_replace_input_with(node_proto.attribute[0].g , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) def lowerCAmelCase__ ( SCREAMING_SNAKE_CASE_: Optional[Any] , SCREAMING_SNAKE_CASE_: Tuple , SCREAMING_SNAKE_CASE_: Union[str, Any] ) -> List[Any]: '''simple docstring''' for n in graph_proto.node: _node_replace_input_with(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) def lowerCAmelCase__ ( SCREAMING_SNAKE_CASE_: str , SCREAMING_SNAKE_CASE_: Optional[Any] , SCREAMING_SNAKE_CASE_: str ) -> Dict: '''simple docstring''' A__ = list(model.graph.initializer ) A__ = list(model_without_ext.graph.initializer ) for i, ref_i in ind_to_replace: assert inits_with_data[i].name == inits[i].name assert inits_with_data[ref_i].name == inits[ref_i].name assert i > ref_i A__ = inits[i].name A__ = inits[ref_i].name model_without_ext.graph.initializer.remove(inits[i] ) # for n in model.graph.node: _graph_replace_input_with(model_without_ext.graph , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) def lowerCAmelCase__ ( SCREAMING_SNAKE_CASE_: List[str] ) -> Any: '''simple docstring''' A__ = os.path.dirname(SCREAMING_SNAKE_CASE_ ) A__ = os.path.basename(SCREAMING_SNAKE_CASE_ ) A__ = onnx.load(os.path.join(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) ) A__ = list(model.graph.initializer ) A__ = set() A__ = {} A__ = [] A__ = 0 for i in range(len(SCREAMING_SNAKE_CASE_ ) ): if i in dup_set: continue for j in range(i + 1 , len(SCREAMING_SNAKE_CASE_ ) ): if j in dup_set: continue if _is_equal_tensor_proto(inits[i] , inits[j] ): dup_set.add(SCREAMING_SNAKE_CASE_ ) dup_set.add(SCREAMING_SNAKE_CASE_ ) A__ = inits[j].data_type A__ = numpy.prod(inits[j].dims ) if dtype == 1: mem_size *= 4 elif dtype == 6: mem_size *= 4 elif dtype == 7 or dtype == 1_1: mem_size *= 8 else: print("unexpected data type: " , SCREAMING_SNAKE_CASE_ ) total_reduced_size += mem_size A__ = inits[i].name A__ = inits[j].name if name_i in dup_map: dup_map[name_i].append(SCREAMING_SNAKE_CASE_ ) else: A__ = [name_j] ind_to_replace.append((j, i) ) print("total reduced size: " , total_reduced_size / 1_0_2_4 / 1_0_2_4 / 1_0_2_4 , "GB" ) A__ = sorted(SCREAMING_SNAKE_CASE_ ) _remove_dup_initializers_from_model(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) A__ = "optimized_" + model_file_name A__ = os.path.join(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) onnx.save(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) return new_model
68
"""simple docstring""" from collections.abc import Generator def a_ ( ): '''simple docstring''' lowercase__ , lowercase__ : List[str] = 0, 1 while True: lowercase__ , lowercase__ : Optional[int] = b, a + b yield b def a_ ( _lowerCAmelCase : int = 1000 ): '''simple docstring''' lowercase__ : List[Any] = 1 lowercase__ : Any = fibonacci_generator() while len(str(next(_lowerCAmelCase ) ) ) < n: answer += 1 return answer + 1 if __name__ == "__main__": print(solution(int(str(input()).strip())))
77
0
def A ( lowercase ) -> list[int]: '''simple docstring''' if num <= 0: raise ValueError('Input must be a positive integer' ) UpperCamelCase = [True] * (num + 1) UpperCamelCase = 2 while p * p <= num: if primes[p]: for i in range(p * p , num + 1 , lowercase ): UpperCamelCase = False p += 1 return [prime for prime in range(2 , num + 1 ) if primes[prime]] if __name__ == "__main__": import doctest doctest.testmod() _UpperCAmelCase : str = int(input("Enter a positive integer: ").strip()) print(prime_sieve_eratosthenes(user_num))
110
from ...utils import ( OptionalDependencyNotAvailable, is_torch_available, is_transformers_available, is_transformers_version, ) try: if not (is_transformers_available() and is_torch_available() and is_transformers_version(">=", "4.25.0")): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from ...utils.dummy_torch_and_transformers_objects import UnCLIPImageVariationPipeline, UnCLIPPipeline else: from .pipeline_unclip import UnCLIPPipeline from .pipeline_unclip_image_variation import UnCLIPImageVariationPipeline from .text_proj import UnCLIPTextProjModel
110
1
"""simple docstring""" def snake_case_ ( A_ : list[list[float]] ): '''simple docstring''' _lowerCamelCase : list[list[float]] = [] for data in source_data: for i, el in enumerate(A_ ): if len(A_ ) < i + 1: data_lists.append([] ) data_lists[i].append(float(A_ ) ) return data_lists def snake_case_ ( A_ : list[list[float]], A_ : list[int] ): '''simple docstring''' _lowerCamelCase : list[list[float]] = [] for dlist, weight in zip(A_, A_ ): _lowerCamelCase : Any = min(A_ ) _lowerCamelCase : Optional[Any] = max(A_ ) _lowerCamelCase : list[float] = [] # for weight 0 score is 1 - actual score if weight == 0: for item in dlist: try: score.append(1 - ((item - mind) / (maxd - mind)) ) except ZeroDivisionError: score.append(1 ) elif weight == 1: for item in dlist: try: score.append((item - mind) / (maxd - mind) ) except ZeroDivisionError: score.append(0 ) # weight not 0 or 1 else: _lowerCamelCase : str = F'''Invalid weight of {weight:f} provided''' raise ValueError(A_ ) score_lists.append(A_ ) return score_lists def snake_case_ ( A_ : list[list[float]] ): '''simple docstring''' _lowerCamelCase : list[float] = [0 for i in range(len(score_lists[0] ) )] for slist in score_lists: for j, ele in enumerate(A_ ): _lowerCamelCase : List[str] = final_scores[j] + ele return final_scores def snake_case_ ( A_ : list[list[float]], A_ : list[int] ): '''simple docstring''' _lowerCamelCase : Tuple = get_data(A_ ) _lowerCamelCase : Optional[Any] = calculate_each_score(A_, A_ ) _lowerCamelCase : str = generate_final_scores(A_ ) # append scores to source data for i, ele in enumerate(A_ ): source_data[i].append(A_ ) return source_data
72
from sklearn.metrics import mean_squared_error import datasets lowerCAmelCase_ = '''\ @article{scikit-learn, title={Scikit-learn: Machine Learning in {P}ython}, author={Pedregosa, F. and Varoquaux, G. and Gramfort, A. and Michel, V. and Thirion, B. and Grisel, O. and Blondel, M. and Prettenhofer, P. and Weiss, R. and Dubourg, V. and Vanderplas, J. and Passos, A. and Cournapeau, D. and Brucher, M. and Perrot, M. and Duchesnay, E.}, journal={Journal of Machine Learning Research}, volume={12}, pages={2825--2830}, year={2011} } ''' lowerCAmelCase_ = '''\ Mean Squared Error(MSE) is the average of the square of difference between the predicted and actual values. ''' lowerCAmelCase_ = ''' Args: predictions: array-like of shape (n_samples,) or (n_samples, n_outputs) Estimated target values. references: array-like of shape (n_samples,) or (n_samples, n_outputs) Ground truth (correct) target values. sample_weight: array-like of shape (n_samples,), default=None Sample weights. multioutput: {"raw_values", "uniform_average"} or array-like of shape (n_outputs,), default="uniform_average" Defines aggregating of multiple output values. Array-like value defines weights used to average errors. "raw_values" : Returns a full set of errors in case of multioutput input. "uniform_average" : Errors of all outputs are averaged with uniform weight. squared : bool, default=True If True returns MSE value, if False returns RMSE (Root Mean Squared Error) value. Returns: mse : mean squared error. Examples: >>> mse_metric = datasets.load_metric("mse") >>> predictions = [2.5, 0.0, 2, 8] >>> references = [3, -0.5, 2, 7] >>> results = mse_metric.compute(predictions=predictions, references=references) >>> print(results) {\'mse\': 0.375} >>> rmse_result = mse_metric.compute(predictions=predictions, references=references, squared=False) >>> print(rmse_result) {\'mse\': 0.6123724356957945} If you\'re using multi-dimensional lists, then set the config as follows : >>> mse_metric = datasets.load_metric("mse", "multilist") >>> predictions = [[0.5, 1], [-1, 1], [7, -6]] >>> references = [[0, 2], [-1, 2], [8, -5]] >>> results = mse_metric.compute(predictions=predictions, references=references) >>> print(results) {\'mse\': 0.7083333333333334} >>> results = mse_metric.compute(predictions=predictions, references=references, multioutput=\'raw_values\') >>> print(results) # doctest: +NORMALIZE_WHITESPACE {\'mse\': array([0.41666667, 1. ])} ''' @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class snake_case_ ( datasets.Metric ): '''simple docstring''' def snake_case__( self : Optional[int] ) ->List[Any]: return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features(self._get_feature_types() ) , reference_urls=[ '''https://scikit-learn.org/stable/modules/generated/sklearn.metrics.mean_squared_error.html''' ] , ) def snake_case__( self : List[Any] ) ->Optional[int]: if self.config_name == "multilist": return { "predictions": datasets.Sequence(datasets.Value('''float''' ) ), "references": datasets.Sequence(datasets.Value('''float''' ) ), } else: return { "predictions": datasets.Value('''float''' ), "references": datasets.Value('''float''' ), } def snake_case__( self : int , _UpperCamelCase : int , _UpperCamelCase : Union[str, Any] , _UpperCamelCase : Any=None , _UpperCamelCase : Optional[int]="uniform_average" , _UpperCamelCase : Tuple=True ) ->Tuple: snake_case_ = mean_squared_error( _UpperCamelCase , _UpperCamelCase , sample_weight=_UpperCamelCase , multioutput=_UpperCamelCase , squared=_UpperCamelCase ) return {"mse": mse}
8
0
'''simple docstring''' def a__ ( _SCREAMING_SNAKE_CASE : list ) -> list: """simple docstring""" UpperCAmelCase_ : str = len(_SCREAMING_SNAKE_CASE ) for _ in range(_SCREAMING_SNAKE_CASE ): for i in range(_ % 2 , arr_size - 1 , 2 ): if arr[i + 1] < arr[i]: UpperCAmelCase_ : List[str] = arr[i + 1], arr[i] return arr if __name__ == "__main__": _lowerCamelCase = list(range(10, 0, -1)) print(f"""Original: {arr}. Sorted: {odd_even_transposition(arr)}""")
351
'''simple docstring''' import argparse import os import evaluate import torch from datasets import load_dataset from torch.optim import AdamW from torch.utils.data import DataLoader from transformers import AutoModelForSequenceClassification, AutoTokenizer, get_linear_schedule_with_warmup, set_seed from accelerate import Accelerator, DistributedType ######################################################################## # This is a fully working simple example to use Accelerate, # specifically showcasing the experiment tracking capability, # and builds off the `nlp_example.py` script. # # This example trains a Bert base model on GLUE MRPC # in any of the following settings (with the same script): # - single CPU or single GPU # - multi GPUS (using PyTorch distributed mode) # - (multi) TPUs # - fp16 (mixed-precision) or fp32 (normal precision) # # To help focus on the differences in the code, building `DataLoaders` # was refactored into its own function. # New additions from the base script can be found quickly by # looking for the # New Code # tags # # To run it in each of these various modes, follow the instructions # in the readme for examples: # https://github.com/huggingface/accelerate/tree/main/examples # ######################################################################## _lowerCamelCase = 16 _lowerCamelCase = 32 def a__ ( _SCREAMING_SNAKE_CASE : Accelerator , _SCREAMING_SNAKE_CASE : int = 16 ) -> List[str]: """simple docstring""" UpperCAmelCase_ : Union[str, Any] = AutoTokenizer.from_pretrained("bert-base-cased" ) UpperCAmelCase_ : Optional[Any] = load_dataset("glue" , "mrpc" ) def tokenize_function(_SCREAMING_SNAKE_CASE : int ): # max_length=None => use the model max length (it's actually the default) UpperCAmelCase_ : str = tokenizer(examples["sentence1"] , examples["sentence2"] , truncation=_SCREAMING_SNAKE_CASE , max_length=_SCREAMING_SNAKE_CASE ) return outputs # Apply the method we just defined to all the examples in all the splits of the dataset # starting with the main process first: with accelerator.main_process_first(): UpperCAmelCase_ : Optional[Any] = datasets.map( _SCREAMING_SNAKE_CASE , batched=_SCREAMING_SNAKE_CASE , remove_columns=["idx", "sentence1", "sentence2"] , ) # We also rename the 'label' column to 'labels' which is the expected name for labels by the models of the # transformers library UpperCAmelCase_ : Any = tokenized_datasets.rename_column("label" , "labels" ) def collate_fn(_SCREAMING_SNAKE_CASE : Any ): # On TPU it's best to pad everything to the same length or training will be very slow. UpperCAmelCase_ : Optional[Any] = 1_28 if accelerator.distributed_type == DistributedType.TPU else None # When using mixed precision we want round multiples of 8/16 if accelerator.mixed_precision == "fp8": UpperCAmelCase_ : Optional[Any] = 16 elif accelerator.mixed_precision != "no": UpperCAmelCase_ : Union[str, Any] = 8 else: UpperCAmelCase_ : List[str] = None return tokenizer.pad( _SCREAMING_SNAKE_CASE , padding="longest" , max_length=_SCREAMING_SNAKE_CASE , pad_to_multiple_of=_SCREAMING_SNAKE_CASE , return_tensors="pt" , ) # Instantiate dataloaders. UpperCAmelCase_ : Union[str, Any] = DataLoader( tokenized_datasets["train"] , shuffle=_SCREAMING_SNAKE_CASE , collate_fn=_SCREAMING_SNAKE_CASE , batch_size=_SCREAMING_SNAKE_CASE ) UpperCAmelCase_ : List[Any] = DataLoader( tokenized_datasets["validation"] , shuffle=_SCREAMING_SNAKE_CASE , collate_fn=_SCREAMING_SNAKE_CASE , batch_size=_SCREAMING_SNAKE_CASE ) return train_dataloader, eval_dataloader # For testing only if os.environ.get("""TESTING_MOCKED_DATALOADERS""", None) == "1": from accelerate.test_utils.training import mocked_dataloaders _lowerCamelCase = mocked_dataloaders # noqa: F811 def a__ ( _SCREAMING_SNAKE_CASE : int , _SCREAMING_SNAKE_CASE : Tuple ) -> str: """simple docstring""" if os.environ.get("TESTING_MOCKED_DATALOADERS" , _SCREAMING_SNAKE_CASE ) == "1": UpperCAmelCase_ : Tuple = 2 # Initialize Accelerator # New Code # # We pass in "all" to `log_with` to grab all available trackers in the environment # Note: If using a custom `Tracker` class, should be passed in here such as: # >>> log_with = ["all", MyCustomTrackerClassInstance()] if args.with_tracking: UpperCAmelCase_ : Optional[Any] = Accelerator( cpu=args.cpu , mixed_precision=args.mixed_precision , log_with="all" , project_dir=args.project_dir ) else: UpperCAmelCase_ : List[Any] = Accelerator(cpu=args.cpu , mixed_precision=args.mixed_precision ) # Sample hyper-parameters for learning rate, batch size, seed and a few other HPs UpperCAmelCase_ : Optional[Any] = config["lr"] UpperCAmelCase_ : Union[str, Any] = int(config["num_epochs"] ) UpperCAmelCase_ : str = int(config["seed"] ) UpperCAmelCase_ : Tuple = int(config["batch_size"] ) set_seed(_SCREAMING_SNAKE_CASE ) UpperCAmelCase_ , UpperCAmelCase_ : List[Any] = get_dataloaders(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) UpperCAmelCase_ : Optional[int] = evaluate.load("glue" , "mrpc" ) # If the batch size is too big we use gradient accumulation UpperCAmelCase_ : List[Any] = 1 if batch_size > MAX_GPU_BATCH_SIZE and accelerator.distributed_type != DistributedType.TPU: UpperCAmelCase_ : Tuple = batch_size // MAX_GPU_BATCH_SIZE UpperCAmelCase_ : Tuple = MAX_GPU_BATCH_SIZE # Instantiate the model (we build the model here so that the seed also control new weights initialization) UpperCAmelCase_ : Tuple = AutoModelForSequenceClassification.from_pretrained("bert-base-cased" , return_dict=_SCREAMING_SNAKE_CASE ) # We could avoid this line since the accelerator is set with `device_placement=True` (default value). # Note that if you are placing tensors on devices manually, this line absolutely needs to be before the optimizer # creation otherwise training will not work on TPU (`accelerate` will kindly throw an error to make us aware of that). UpperCAmelCase_ : Union[str, Any] = model.to(accelerator.device ) # Instantiate optimizer UpperCAmelCase_ : int = AdamW(params=model.parameters() , lr=_SCREAMING_SNAKE_CASE ) # Instantiate scheduler UpperCAmelCase_ : Optional[int] = get_linear_schedule_with_warmup( optimizer=_SCREAMING_SNAKE_CASE , num_warmup_steps=1_00 , num_training_steps=(len(_SCREAMING_SNAKE_CASE ) * num_epochs) // gradient_accumulation_steps , ) # Prepare everything # There is no specific order to remember, we just need to unpack the objects in the same order we gave them to the # prepare method. UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ : int = accelerator.prepare( _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) # New Code # # We need to initialize the trackers we use. Overall configurations can also be stored if args.with_tracking: UpperCAmelCase_ : List[str] = os.path.split(_SCREAMING_SNAKE_CASE )[-1].split("." )[0] accelerator.init_trackers(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) # Now we train the model for epoch in range(_SCREAMING_SNAKE_CASE ): model.train() # New Code # # For our tracking example, we will log the total loss of each epoch if args.with_tracking: UpperCAmelCase_ : Dict = 0 for step, batch in enumerate(_SCREAMING_SNAKE_CASE ): # We could avoid this line since we set the accelerator with `device_placement=True`. batch.to(accelerator.device ) UpperCAmelCase_ : Union[str, Any] = model(**_SCREAMING_SNAKE_CASE ) UpperCAmelCase_ : int = outputs.loss # New Code # if args.with_tracking: total_loss += loss.detach().float() UpperCAmelCase_ : List[str] = loss / gradient_accumulation_steps accelerator.backward(_SCREAMING_SNAKE_CASE ) if step % gradient_accumulation_steps == 0: optimizer.step() lr_scheduler.step() optimizer.zero_grad() model.eval() for step, batch in enumerate(_SCREAMING_SNAKE_CASE ): # We could avoid this line since we set the accelerator with `device_placement=True` (the default). batch.to(accelerator.device ) with torch.no_grad(): UpperCAmelCase_ : Optional[int] = model(**_SCREAMING_SNAKE_CASE ) UpperCAmelCase_ : Tuple = outputs.logits.argmax(dim=-1 ) UpperCAmelCase_ , UpperCAmelCase_ : List[str] = accelerator.gather_for_metrics((predictions, batch["labels"]) ) metric.add_batch( predictions=_SCREAMING_SNAKE_CASE , references=_SCREAMING_SNAKE_CASE , ) UpperCAmelCase_ : Dict = metric.compute() # Use accelerator.print to print only on the main process. accelerator.print(F'''epoch {epoch}:''' , _SCREAMING_SNAKE_CASE ) # New Code # # To actually log, we call `Accelerator.log` # The values passed can be of `str`, `int`, `float` or `dict` of `str` to `float`/`int` if args.with_tracking: accelerator.log( { "accuracy": eval_metric["accuracy"], "f1": eval_metric["f1"], "train_loss": total_loss.item() / len(_SCREAMING_SNAKE_CASE ), "epoch": epoch, } , step=_SCREAMING_SNAKE_CASE , ) # New Code # # When a run is finished, you should call `accelerator.end_training()` # to close all of the open trackers if args.with_tracking: accelerator.end_training() def a__ ( ) -> List[str]: """simple docstring""" UpperCAmelCase_ : int = argparse.ArgumentParser(description="Simple example of training script." ) parser.add_argument( "--mixed_precision" , type=_SCREAMING_SNAKE_CASE , default=_SCREAMING_SNAKE_CASE , choices=["no", "fp16", "bf16", "fp8"] , help="Whether to use mixed precision. Choose" "between fp16 and bf16 (bfloat16). Bf16 requires PyTorch >= 1.10." "and an Nvidia Ampere GPU." , ) parser.add_argument("--cpu" , action="store_true" , help="If passed, will train on the CPU." ) parser.add_argument( "--with_tracking" , action="store_true" , help="Whether to load in all available experiment trackers from the environment and use them for logging." , ) parser.add_argument( "--project_dir" , type=_SCREAMING_SNAKE_CASE , default="logs" , help="Location on where to store experiment tracking logs` and relevent project information" , ) UpperCAmelCase_ : List[Any] = parser.parse_args() UpperCAmelCase_ : Dict = {"lr": 2E-5, "num_epochs": 3, "seed": 42, "batch_size": 16} training_function(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) if __name__ == "__main__": main()
67
0
from maths.prime_factors import prime_factors def snake_case__ ( lowerCAmelCase_ ): """simple docstring""" if not isinstance(lowerCAmelCase_, lowerCAmelCase_ ): SCREAMING_SNAKE_CASE =F'Input value of [number={number}] must be an integer' raise TypeError(lowerCAmelCase_ ) if number < 1: raise ValueError('Input must be a positive integer' ) return -1 if len(prime_factors(lowerCAmelCase_ ) ) % 2 else 1 if __name__ == "__main__": import doctest doctest.testmod()
334
import argparse import os # New Code # 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 import find_executable_batch_size ######################################################################## # This is a fully working simple example to use Accelerate, # specifically showcasing how to ensure out-of-memory errors never # interrupt training, and builds off the `nlp_example.py` script. # # This example trains a Bert base model on GLUE MRPC # in any of the following settings (with the same script): # - single CPU or single GPU # - multi GPUS (using PyTorch distributed mode) # - (multi) TPUs # - fp16 (mixed-precision) or fp32 (normal precision) # # New additions from the base script can be found quickly by # looking for the # New Code # tags # # To run it in each of these various modes, follow the instructions # in the readme for examples: # https://github.com/huggingface/accelerate/tree/main/examples # ######################################################################## _lowerCamelCase =16 _lowerCamelCase =32 def snake_case__ ( lowerCAmelCase_, lowerCAmelCase_ = 16 ): """simple docstring""" SCREAMING_SNAKE_CASE =AutoTokenizer.from_pretrained('bert-base-cased' ) SCREAMING_SNAKE_CASE =load_dataset('glue', 'mrpc' ) def tokenize_function(lowerCAmelCase_ ): # max_length=None => use the model max length (it's actually the default) SCREAMING_SNAKE_CASE =tokenizer(examples['sentence1'], examples['sentence2'], truncation=lowerCAmelCase_, max_length=lowerCAmelCase_ ) return outputs # Apply the method we just defined to all the examples in all the splits of the dataset # starting with the main process first: with accelerator.main_process_first(): SCREAMING_SNAKE_CASE =datasets.map( lowerCAmelCase_, batched=lowerCAmelCase_, remove_columns=['idx', 'sentence1', 'sentence2'], ) # We also rename the 'label' column to 'labels' which is the expected name for labels by the models of the # transformers library SCREAMING_SNAKE_CASE =tokenized_datasets.rename_column('label', 'labels' ) def collate_fn(lowerCAmelCase_ ): # On TPU it's best to pad everything to the same length or training will be very slow. SCREAMING_SNAKE_CASE =128 if accelerator.distributed_type == DistributedType.TPU else None # When using mixed precision we want round multiples of 8/16 if accelerator.mixed_precision == "fp8": SCREAMING_SNAKE_CASE =16 elif accelerator.mixed_precision != "no": SCREAMING_SNAKE_CASE =8 else: SCREAMING_SNAKE_CASE =None return tokenizer.pad( lowerCAmelCase_, padding='longest', max_length=lowerCAmelCase_, pad_to_multiple_of=lowerCAmelCase_, return_tensors='pt', ) # Instantiate dataloaders. SCREAMING_SNAKE_CASE =DataLoader( tokenized_datasets['train'], shuffle=lowerCAmelCase_, collate_fn=lowerCAmelCase_, batch_size=lowerCAmelCase_ ) SCREAMING_SNAKE_CASE =DataLoader( tokenized_datasets['validation'], shuffle=lowerCAmelCase_, collate_fn=lowerCAmelCase_, batch_size=lowerCAmelCase_ ) return train_dataloader, eval_dataloader # For testing only if os.environ.get("TESTING_MOCKED_DATALOADERS", None) == "1": from accelerate.test_utils.training import mocked_dataloaders _lowerCamelCase =mocked_dataloaders # noqa: F811 def snake_case__ ( lowerCAmelCase_, lowerCAmelCase_ ): """simple docstring""" if os.environ.get('TESTING_MOCKED_DATALOADERS', lowerCAmelCase_ ) == "1": SCREAMING_SNAKE_CASE =2 # Initialize accelerator SCREAMING_SNAKE_CASE =Accelerator(cpu=args.cpu, mixed_precision=args.mixed_precision ) # Sample hyper-parameters for learning rate, batch size, seed and a few other HPs SCREAMING_SNAKE_CASE =config['lr'] SCREAMING_SNAKE_CASE =int(config['num_epochs'] ) SCREAMING_SNAKE_CASE =int(config['seed'] ) SCREAMING_SNAKE_CASE =int(config['batch_size'] ) SCREAMING_SNAKE_CASE =evaluate.load('glue', 'mrpc' ) # New Code # # We now can define an inner training loop function. It should take a batch size as the only parameter, # and build the dataloaders in there. # It also gets our decorator @find_executable_batch_size(starting_batch_size=lowerCAmelCase_ ) def inner_training_loop(lowerCAmelCase_ ): # And now just move everything below under this function # We need to bring in the Accelerator object from earlier nonlocal accelerator # And reset all of its attributes that could hold onto any memory: accelerator.free_memory() # Then we can declare the model, optimizer, and everything else: set_seed(lowerCAmelCase_ ) # Instantiate the model (we build the model here so that the seed also control new weights initialization) SCREAMING_SNAKE_CASE =AutoModelForSequenceClassification.from_pretrained('bert-base-cased', return_dict=lowerCAmelCase_ ) # We could avoid this line since the accelerator is set with `device_placement=True` (default value). # Note that if you are placing tensors on devices manually, this line absolutely needs to be before the optimizer # creation otherwise training will not work on TPU (`accelerate` will kindly throw an error to make us aware of that). SCREAMING_SNAKE_CASE =model.to(accelerator.device ) # Instantiate optimizer SCREAMING_SNAKE_CASE =AdamW(params=model.parameters(), lr=lowerCAmelCase_ ) SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE =get_dataloaders(lowerCAmelCase_, lowerCAmelCase_ ) # Instantiate scheduler SCREAMING_SNAKE_CASE =get_linear_schedule_with_warmup( optimizer=lowerCAmelCase_, num_warmup_steps=100, num_training_steps=(len(lowerCAmelCase_ ) * num_epochs), ) # Prepare everything # There is no specific order to remember, we just need to unpack the objects in the same order we gave them to the # prepare method. SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE =accelerator.prepare( lowerCAmelCase_, lowerCAmelCase_, lowerCAmelCase_, lowerCAmelCase_, lowerCAmelCase_ ) # Now we train the model for epoch in range(lowerCAmelCase_ ): model.train() for step, batch in enumerate(lowerCAmelCase_ ): # We could avoid this line since we set the accelerator with `device_placement=True`. batch.to(accelerator.device ) SCREAMING_SNAKE_CASE =model(**lowerCAmelCase_ ) SCREAMING_SNAKE_CASE =outputs.loss accelerator.backward(lowerCAmelCase_ ) optimizer.step() lr_scheduler.step() optimizer.zero_grad() model.eval() for step, batch in enumerate(lowerCAmelCase_ ): # We could avoid this line since we set the accelerator with `device_placement=True`. batch.to(accelerator.device ) with torch.no_grad(): SCREAMING_SNAKE_CASE =model(**lowerCAmelCase_ ) SCREAMING_SNAKE_CASE =outputs.logits.argmax(dim=-1 ) SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE =accelerator.gather_for_metrics((predictions, batch['labels']) ) metric.add_batch( predictions=lowerCAmelCase_, references=lowerCAmelCase_, ) SCREAMING_SNAKE_CASE =metric.compute() # Use accelerator.print to print only on the main process. accelerator.print(F'epoch {epoch}:', lowerCAmelCase_ ) # New Code # # And call it at the end with no arguments # Note: You could also refactor this outside of your training loop function inner_training_loop() def snake_case__ ( ): """simple docstring""" SCREAMING_SNAKE_CASE =argparse.ArgumentParser(description='Simple example of training script.' ) parser.add_argument( '--mixed_precision', type=lowerCAmelCase_, default=lowerCAmelCase_, choices=['no', 'fp16', 'bf16', 'fp8'], help='Whether to use mixed precision. Choose' 'between fp16 and bf16 (bfloat16). Bf16 requires PyTorch >= 1.10.' 'and an Nvidia Ampere GPU.', ) parser.add_argument('--cpu', action='store_true', help='If passed, will train on the CPU.' ) SCREAMING_SNAKE_CASE =parser.parse_args() SCREAMING_SNAKE_CASE ={'lr': 2e-5, 'num_epochs': 3, 'seed': 42, 'batch_size': 16} training_function(lowerCAmelCase_, lowerCAmelCase_ ) if __name__ == "__main__": main()
334
1
import warnings from collections import OrderedDict from typing import Mapping from packaging import version from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging _A = logging.get_logger(__name__) _A = { "nvidia/segformer-b0-finetuned-ade-512-512": ( "https://huggingface.co/nvidia/segformer-b0-finetuned-ade-512-512/resolve/main/config.json" ), # See all SegFormer models at https://huggingface.co/models?filter=segformer } class lowerCamelCase ( A_ ): UpperCAmelCase__ : List[Any] = "segformer" def __init__(self : List[Any] , _A : int=3 , _A : List[Any]=4 , _A : Any=[2, 2, 2, 2] , _A : Dict=[8, 4, 2, 1] , _A : List[Any]=[3_2, 6_4, 1_6_0, 2_5_6] , _A : Tuple=[7, 3, 3, 3] , _A : Optional[int]=[4, 2, 2, 2] , _A : Dict=[1, 2, 5, 8] , _A : int=[4, 4, 4, 4] , _A : Dict="gelu" , _A : Tuple=0.0 , _A : Optional[Any]=0.0 , _A : List[Any]=0.1 , _A : Union[str, Any]=0.02 , _A : Dict=0.1 , _A : List[Any]=1E-6 , _A : List[str]=2_5_6 , _A : Optional[Any]=2_5_5 , **_A : str , ) -> Tuple: super().__init__(**_A ) if "reshape_last_stage" in kwargs and kwargs["reshape_last_stage"] is False: warnings.warn( "Reshape_last_stage is set to False in this config. This argument is deprecated and will soon be" " removed, as the behaviour will default to that of reshape_last_stage = True." , _A , ) snake_case = num_channels snake_case = num_encoder_blocks snake_case = depths snake_case = sr_ratios snake_case = hidden_sizes snake_case = patch_sizes snake_case = strides snake_case = mlp_ratios snake_case = num_attention_heads snake_case = hidden_act snake_case = hidden_dropout_prob snake_case = attention_probs_dropout_prob snake_case = classifier_dropout_prob snake_case = initializer_range snake_case = drop_path_rate snake_case = layer_norm_eps snake_case = decoder_hidden_size snake_case = kwargs.get("reshape_last_stage" , _A ) snake_case = semantic_loss_ignore_index class lowerCamelCase ( A_ ): UpperCAmelCase__ : Optional[Any] = version.parse("1.11" ) @property def UpperCAmelCase(self : Tuple ) -> Mapping[str, Mapping[int, str]]: return OrderedDict( [ ("pixel_values", {0: "batch", 1: "num_channels", 2: "height", 3: "width"}), ] ) @property def UpperCAmelCase(self : Tuple ) -> float: return 1E-4 @property def UpperCAmelCase(self : List[str] ) -> int: return 1_2
137
import importlib.metadata import warnings from copy import deepcopy from packaging import version from ..utils import logging from .import_utils import is_accelerate_available, is_bitsandbytes_available if is_bitsandbytes_available(): import bitsandbytes as bnb import torch import torch.nn as nn from ..pytorch_utils import ConvaD if is_accelerate_available(): from accelerate import init_empty_weights from accelerate.utils import find_tied_parameters _A = logging.get_logger(__name__) def lowercase_ ( A__ , A__ , A__ , A__=None , A__=None ) -> str: """simple docstring""" if "." in tensor_name: snake_case = tensor_name.split("." ) for split in splits[:-1]: snake_case = getattr(A__ , A__ ) if new_module is None: raise ValueError(F'{module} has no attribute {split}.' ) snake_case = new_module snake_case = splits[-1] if tensor_name not in module._parameters and tensor_name not in module._buffers: raise ValueError(F'{module} does not have a parameter or a buffer named {tensor_name}.' ) snake_case = tensor_name in module._buffers snake_case = getattr(A__ , A__ ) if old_value.device == torch.device("meta" ) and device not in ["meta", torch.device("meta" )] and value is None: raise ValueError(F'{tensor_name} is on the meta device, we need a `value` to put in on {device}.' ) snake_case = False snake_case = False if is_buffer or not is_bitsandbytes_available(): snake_case = False snake_case = False else: snake_case = hasattr(bnb.nn , "Params4bit" ) and isinstance(module._parameters[tensor_name] , bnb.nn.Paramsabit ) snake_case = isinstance(module._parameters[tensor_name] , bnb.nn.IntaParams ) if is_abit or is_abit: snake_case = module._parameters[tensor_name] if param.device.type != "cuda": if value is None: snake_case = old_value.to(A__ ) elif isinstance(A__ , torch.Tensor ): snake_case = value.to("cpu" ) if value.dtype == torch.inta: snake_case = version.parse(importlib.metadata.version("bitsandbytes" ) ) > version.parse( "0.37.2" ) if not is_abit_serializable: raise ValueError( "Detected int8 weights but the version of bitsandbytes is not compatible with int8 serialization. " "Make sure to download the latest `bitsandbytes` version. `pip install --upgrade bitsandbytes`." ) else: snake_case = torch.tensor(A__ , device="cpu" ) # Support models using `Conv1D` in place of `nn.Linear` (e.g. gpt2) by transposing the weight matrix prior to quantization. # Since weights are saved in the correct "orientation", we skip transposing when loading. if issubclass(module.source_cls , A__ ) and fpaa_statistics is None: snake_case = new_value.T snake_case = old_value.__dict__ if is_abit: snake_case = bnb.nn.IntaParams(A__ , requires_grad=A__ , **A__ ).to(A__ ) elif is_abit: snake_case = bnb.nn.Paramsabit(A__ , requires_grad=A__ , **A__ ).to(A__ ) snake_case = new_value if fpaa_statistics is not None: setattr(module.weight , "SCB" , fpaa_statistics.to(A__ ) ) else: if value is None: snake_case = old_value.to(A__ ) elif isinstance(A__ , torch.Tensor ): snake_case = value.to(A__ ) else: snake_case = torch.tensor(A__ , device=A__ ) if is_buffer: snake_case = new_value else: snake_case = nn.Parameter(A__ , requires_grad=old_value.requires_grad ) snake_case = new_value def lowercase_ ( A__ , A__=None , A__=None , A__=None , A__=False ) -> Optional[Any]: """simple docstring""" for name, module in model.named_children(): if current_key_name is None: snake_case = [] current_key_name.append(A__ ) if (isinstance(A__ , nn.Linear ) or isinstance(A__ , A__ )) and name not in modules_to_not_convert: # Check if the current key is not in the `modules_to_not_convert` if not any(key in ".".join(A__ ) for key in modules_to_not_convert ): with init_empty_weights(): if isinstance(A__ , A__ ): snake_case , snake_case = module.weight.shape else: snake_case = module.in_features snake_case = module.out_features if quantization_config.quantization_method() == "llm_int8": snake_case = bnb.nn.LinearabitLt( A__ , A__ , module.bias is not None , has_fpaa_weights=quantization_config.llm_inta_has_fpaa_weight , threshold=quantization_config.llm_inta_threshold , ) snake_case = True else: if ( quantization_config.llm_inta_skip_modules is not None and name in quantization_config.llm_inta_skip_modules ): pass else: snake_case = bnb.nn.Linearabit( A__ , A__ , module.bias is not None , quantization_config.bnb_abit_compute_dtype , compress_statistics=quantization_config.bnb_abit_use_double_quant , quant_type=quantization_config.bnb_abit_quant_type , ) snake_case = True # Store the module class in case we need to transpose the weight later snake_case = type(A__ ) # Force requires grad to False to avoid unexpected errors model._modules[name].requires_grad_(A__ ) if len(list(module.children() ) ) > 0: snake_case , snake_case = _replace_with_bnb_linear( A__ , A__ , A__ , A__ , has_been_replaced=A__ , ) # Remove the last key for recursion current_key_name.pop(-1 ) return model, has_been_replaced def lowercase_ ( A__ , A__=None , A__=None , A__=None ) -> List[str]: """simple docstring""" snake_case = ["lm_head"] if modules_to_not_convert is None else modules_to_not_convert snake_case , snake_case = _replace_with_bnb_linear( A__ , A__ , A__ , A__ ) if not has_been_replaced: logger.warning( "You are loading your model in 8bit or 4bit but no linear modules were found in your model." " Please double check your model architecture, or submit an issue on github if you think this is" " a bug." ) return model def lowercase_ ( *A__ , **A__ ) -> Optional[int]: """simple docstring""" warnings.warn( "`replace_8bit_linear` will be deprecated in a future version, please use `replace_with_bnb_linear` instead" , A__ , ) return replace_with_bnb_linear(*A__ , **A__ ) def lowercase_ ( *A__ , **A__ ) -> Any: """simple docstring""" warnings.warn( "`set_module_8bit_tensor_to_device` will be deprecated in a future version, please use `set_module_quantized_tensor_to_device` instead" , A__ , ) return set_module_quantized_tensor_to_device(*A__ , **A__ ) def lowercase_ ( A__ ) -> Union[str, Any]: """simple docstring""" snake_case = deepcopy(A__ ) # this has 0 cost since it is done inside `init_empty_weights` context manager` tied_model.tie_weights() snake_case = find_tied_parameters(A__ ) # For compatibility with Accelerate < 0.18 if isinstance(A__ , A__ ): snake_case = sum(list(tied_params.values() ) , [] ) + list(tied_params.keys() ) else: snake_case = sum(A__ , [] ) snake_case = len(A__ ) > 0 # Check if it is a base model snake_case = not hasattr(A__ , model.base_model_prefix ) # Ignore this for base models (BertModel, GPT2Model, etc.) if (not has_tied_params) and is_base_model: return [] # otherwise they have an attached head snake_case = list(model.named_children() ) snake_case = [list_modules[-1][0]] # add last module together with tied weights snake_case = set(A__ ) - set(A__ ) snake_case = list(set(A__ ) ) + list(A__ ) # remove ".weight" from the keys snake_case = [".weight", ".bias"] snake_case = [] for name in list_untouched: for name_to_remove in names_to_remove: if name_to_remove in name: snake_case = name.replace(A__ , "" ) filtered_module_names.append(A__ ) return filtered_module_names
137
1
"""simple docstring""" import json import os from pathlib import Path from shutil import copyfile from typing import Any, Dict, List, Optional, Tuple, Union import sentencepiece from ...tokenization_utils import PreTrainedTokenizer from ...utils import logging lowerCAmelCase_ = logging.get_logger(__name__) lowerCAmelCase_ = '▁' lowerCAmelCase_ = { 'vocab_file': 'vocab.json', 'spm_file': 'sentencepiece.bpe.model', } lowerCAmelCase_ = { 'vocab_file': { 'facebook/s2t-small-librispeech-asr': ( 'https://huggingface.co/facebook/s2t-small-librispeech-asr/resolve/main/vocab.json' ), }, 'spm_file': { 'facebook/s2t-small-librispeech-asr': ( 'https://huggingface.co/facebook/s2t-small-librispeech-asr/resolve/main/sentencepiece.bpe.model' ) }, } lowerCAmelCase_ = { 'facebook/s2t-small-librispeech-asr': 1_024, } lowerCAmelCase_ = ['pt', 'fr', 'ru', 'nl', 'ro', 'it', 'es', 'de'] lowerCAmelCase_ = {'mustc': MUSTC_LANGS} class __A ( A_ ): '''simple docstring''' lowerCAmelCase : Union[str, Any] = VOCAB_FILES_NAMES lowerCAmelCase : Union[str, Any] = PRETRAINED_VOCAB_FILES_MAP lowerCAmelCase : int = MAX_MODEL_INPUT_SIZES lowerCAmelCase : Optional[int] = ["input_ids", "attention_mask"] lowerCAmelCase : List[int] = [] def __init__( self : Optional[Any] ,_snake_case : int ,_snake_case : Optional[int] ,_snake_case : Optional[int]="<s>" ,_snake_case : Union[str, Any]="</s>" ,_snake_case : Any="<pad>" ,_snake_case : Dict="<unk>" ,_snake_case : Optional[Any]=False ,_snake_case : int=False ,_snake_case : Optional[int]=None ,_snake_case : Dict=None ,_snake_case : Optional[Dict[str, Any]] = None ,**_snake_case : int ,) -> None: """simple docstring""" lowercase__ : Union[str, Any] = {} if sp_model_kwargs is None else sp_model_kwargs super().__init__( bos_token=_snake_case ,eos_token=_snake_case ,unk_token=_snake_case ,pad_token=_snake_case ,do_upper_case=_snake_case ,do_lower_case=_snake_case ,tgt_lang=_snake_case ,lang_codes=_snake_case ,sp_model_kwargs=self.sp_model_kwargs ,**_snake_case ,) lowercase__ : str = do_upper_case lowercase__ : List[str] = do_lower_case lowercase__ : Any = load_json(_snake_case ) lowercase__ : Dict = {v: k for k, v in self.encoder.items()} lowercase__ : Dict = spm_file lowercase__ : List[str] = load_spm(_snake_case ,self.sp_model_kwargs ) if lang_codes is not None: lowercase__ : Optional[Any] = lang_codes lowercase__ : Any = LANGUAGES[lang_codes] lowercase__ : Optional[int] = [f"""<lang:{lang}>""" for lang in self.langs] lowercase__ : Union[str, Any] = {lang: self.sp_model.PieceToId(f"""<lang:{lang}>""" ) for lang in self.langs} lowercase__ : str = self.lang_tokens lowercase__ : Optional[int] = tgt_lang if tgt_lang is not None else self.langs[0] self.set_tgt_lang_special_tokens(self._tgt_lang ) else: lowercase__ : Dict = {} @property def UpperCAmelCase ( self : Tuple ) -> int: """simple docstring""" return len(self.encoder ) @property def UpperCAmelCase ( self : str ) -> str: """simple docstring""" return self._tgt_lang @tgt_lang.setter def UpperCAmelCase ( self : Optional[int] ,_snake_case : List[Any] ) -> None: """simple docstring""" lowercase__ : Optional[Any] = new_tgt_lang self.set_tgt_lang_special_tokens(_snake_case ) def UpperCAmelCase ( self : Optional[int] ,_snake_case : str ) -> None: """simple docstring""" lowercase__ : List[Any] = self.lang_code_to_id[tgt_lang] lowercase__ : str = [lang_code_id] def UpperCAmelCase ( self : Optional[Any] ,_snake_case : str ) -> List[str]: """simple docstring""" return self.sp_model.encode(_snake_case ,out_type=_snake_case ) def UpperCAmelCase ( self : Dict ,_snake_case : Union[str, Any] ) -> Tuple: """simple docstring""" return self.encoder.get(_snake_case ,self.encoder[self.unk_token] ) def UpperCAmelCase ( self : List[str] ,_snake_case : int ) -> str: """simple docstring""" return self.decoder.get(_snake_case ,self.unk_token ) def UpperCAmelCase ( self : List[Any] ,_snake_case : List[str] ) -> str: """simple docstring""" lowercase__ : str = [] lowercase__ : List[str] = '''''' for token in tokens: # make sure that special tokens are not decoded using sentencepiece model if token in self.all_special_tokens: lowercase__ : int = self.sp_model.decode(_snake_case ) out_string += (decoded.upper() if self.do_upper_case else decoded) + token + " " lowercase__ : str = [] else: current_sub_tokens.append(_snake_case ) lowercase__ : Tuple = self.sp_model.decode(_snake_case ) out_string += decoded.upper() if self.do_upper_case else decoded return out_string.strip() def UpperCAmelCase ( self : str ,_snake_case : Tuple ,_snake_case : Any=None ) -> List[int]: """simple docstring""" if token_ids_a is None: return self.prefix_tokens + token_ids_a + [self.eos_token_id] # We don't expect to process pairs, but leave the pair logic for API consistency return self.prefix_tokens + token_ids_a + token_ids_a + [self.eos_token_id] def UpperCAmelCase ( self : Dict ,_snake_case : List[int] ,_snake_case : Optional[List[int]] = None ,_snake_case : bool = False ) -> List[int]: """simple docstring""" if already_has_special_tokens: return super().get_special_tokens_mask( token_ids_a=_snake_case ,token_ids_a=_snake_case ,already_has_special_tokens=_snake_case ) lowercase__ : List[Any] = [1] * len(self.prefix_tokens ) lowercase__ : Tuple = [1] if token_ids_a is None: return prefix_ones + ([0] * len(_snake_case )) + suffix_ones return prefix_ones + ([0] * len(_snake_case )) + ([0] * len(_snake_case )) + suffix_ones def UpperCAmelCase ( self : Dict ) -> Dict: """simple docstring""" lowercase__ : Any = self.encoder.copy() vocab.update(self.added_tokens_encoder ) return vocab def __getstate__( self : int ) -> Dict: """simple docstring""" lowercase__ : int = self.__dict__.copy() lowercase__ : Dict = None return state def __setstate__( self : Union[str, Any] ,_snake_case : Dict ) -> None: """simple docstring""" lowercase__ : Optional[int] = d # for backward compatibility if not hasattr(self ,'''sp_model_kwargs''' ): lowercase__ : int = {} lowercase__ : List[Any] = load_spm(self.spm_file ,self.sp_model_kwargs ) def UpperCAmelCase ( self : Optional[int] ,_snake_case : str ,_snake_case : Optional[str] = None ) -> Tuple[str]: """simple docstring""" lowercase__ : Tuple = Path(_snake_case ) assert save_dir.is_dir(), f"""{save_directory} should be a directory""" lowercase__ : List[Any] = save_dir / ( (filename_prefix + '''-''' if filename_prefix else '''''') + self.vocab_files_names['''vocab_file'''] ) lowercase__ : Optional[Any] = save_dir / ( (filename_prefix + '''-''' if filename_prefix else '''''') + self.vocab_files_names['''spm_file'''] ) save_json(self.encoder ,_snake_case ) if os.path.abspath(self.spm_file ) != os.path.abspath(_snake_case ) and os.path.isfile(self.spm_file ): copyfile(self.spm_file ,_snake_case ) elif not os.path.isfile(self.spm_file ): with open(_snake_case ,'''wb''' ) as fi: lowercase__ : Dict = self.sp_model.serialized_model_proto() fi.write(_snake_case ) return (str(_snake_case ), str(_snake_case )) def __UpperCAmelCase ( __lowerCamelCase , __lowerCamelCase ) -> sentencepiece.SentencePieceProcessor: lowercase__ : Any = sentencepiece.SentencePieceProcessor(**__lowerCamelCase ) spm.Load(str(__lowerCamelCase ) ) return spm def __UpperCAmelCase ( __lowerCamelCase ) -> Union[Dict, List]: with open(__lowerCamelCase , '''r''' ) as f: return json.load(__lowerCamelCase ) def __UpperCAmelCase ( __lowerCamelCase , __lowerCamelCase ) -> None: with open(__lowerCamelCase , '''w''' ) as f: json.dump(__lowerCamelCase , __lowerCamelCase , indent=2 )
16
"""simple docstring""" from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_sentencepiece_available, is_torch_available, ) lowerCAmelCase_ = { 'configuration_speecht5': [ 'SPEECHT5_PRETRAINED_CONFIG_ARCHIVE_MAP', 'SPEECHT5_PRETRAINED_HIFIGAN_CONFIG_ARCHIVE_MAP', 'SpeechT5Config', 'SpeechT5HifiGanConfig', ], 'feature_extraction_speecht5': ['SpeechT5FeatureExtractor'], 'processing_speecht5': ['SpeechT5Processor'], } try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase_ = ['SpeechT5Tokenizer'] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase_ = [ 'SPEECHT5_PRETRAINED_MODEL_ARCHIVE_LIST', 'SpeechT5ForSpeechToText', 'SpeechT5ForSpeechToSpeech', 'SpeechT5ForTextToSpeech', 'SpeechT5Model', 'SpeechT5PreTrainedModel', 'SpeechT5HifiGan', ] if TYPE_CHECKING: from .configuration_speechta import ( SPEECHT5_PRETRAINED_CONFIG_ARCHIVE_MAP, SPEECHT5_PRETRAINED_HIFIGAN_CONFIG_ARCHIVE_MAP, SpeechTaConfig, SpeechTaHifiGanConfig, ) from .feature_extraction_speechta import SpeechTaFeatureExtractor from .processing_speechta import SpeechTaProcessor try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_speechta import SpeechTaTokenizer try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_speechta import ( SPEECHT5_PRETRAINED_MODEL_ARCHIVE_LIST, SpeechTaForSpeechToSpeech, SpeechTaForSpeechToText, SpeechTaForTextToSpeech, SpeechTaHifiGan, SpeechTaModel, SpeechTaPreTrainedModel, ) else: import sys lowerCAmelCase_ = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
16
1
import gc import random import unittest import numpy as np import torch from PIL import Image from transformers import CLIPTextConfig, CLIPTextModel, CLIPTokenizer from diffusers import ( AutoencoderKL, DDIMScheduler, EulerAncestralDiscreteScheduler, LMSDiscreteScheduler, PNDMScheduler, StableDiffusionInstructPixaPixPipeline, UNetaDConditionModel, ) from diffusers.image_processor import VaeImageProcessor from diffusers.utils import floats_tensor, load_image, slow, torch_device from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu from ..pipeline_params import ( IMAGE_TO_IMAGE_IMAGE_PARAMS, TEXT_GUIDED_IMAGE_INPAINTING_BATCH_PARAMS, TEXT_GUIDED_IMAGE_VARIATION_PARAMS, ) from ..test_pipelines_common import PipelineKarrasSchedulerTesterMixin, PipelineLatentTesterMixin, PipelineTesterMixin enable_full_determinism() class __magic_name__ ( lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ , unittest.TestCase ): """simple docstring""" __UpperCamelCase = StableDiffusionInstructPixaPixPipeline __UpperCamelCase = TEXT_GUIDED_IMAGE_VARIATION_PARAMS - {'''height''', '''width''', '''cross_attention_kwargs'''} __UpperCamelCase = TEXT_GUIDED_IMAGE_INPAINTING_BATCH_PARAMS __UpperCamelCase = IMAGE_TO_IMAGE_IMAGE_PARAMS __UpperCamelCase = IMAGE_TO_IMAGE_IMAGE_PARAMS def SCREAMING_SNAKE_CASE ( self :str ): '''simple docstring''' torch.manual_seed(0 ) A_ : Tuple = UNetaDConditionModel( block_out_channels=(32, 64) , layers_per_block=2 , sample_size=32 , in_channels=8 , out_channels=4 , down_block_types=("DownBlock2D", "CrossAttnDownBlock2D") , up_block_types=("CrossAttnUpBlock2D", "UpBlock2D") , cross_attention_dim=32 , ) A_ : Union[str, Any] = PNDMScheduler(skip_prk_steps=__A ) torch.manual_seed(0 ) A_ : str = 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 , ) torch.manual_seed(0 ) A_ : Any = CLIPTextConfig( bos_token_id=0 , eos_token_id=2 , hidden_size=32 , intermediate_size=37 , layer_norm_eps=1e-05 , num_attention_heads=4 , num_hidden_layers=5 , pad_token_id=1 , vocab_size=1_000 , ) A_ : Dict = CLIPTextModel(__A ) A_ : List[str] = CLIPTokenizer.from_pretrained("hf-internal-testing/tiny-random-clip" ) A_ : Optional[int] = { "unet": unet, "scheduler": scheduler, "vae": vae, "text_encoder": text_encoder, "tokenizer": tokenizer, "safety_checker": None, "feature_extractor": None, } return components def SCREAMING_SNAKE_CASE ( self :List[str] , snake_case :List[str] , snake_case :Any=0 ): '''simple docstring''' A_ : Union[str, Any] = floats_tensor((1, 3, 32, 32) , rng=random.Random(__A ) ).to(__A ) A_ : Union[str, Any] = image.cpu().permute(0 , 2 , 3 , 1 )[0] A_ : Optional[int] = Image.fromarray(np.uinta(__A ) ).convert("RGB" ) if str(__A ).startswith("mps" ): A_ : str = torch.manual_seed(__A ) else: A_ : Optional[Any] = torch.Generator(device=__A ).manual_seed(__A ) A_ : List[Any] = { "prompt": "A painting of a squirrel eating a burger", "image": image, "generator": generator, "num_inference_steps": 2, "guidance_scale": 6.0, "image_guidance_scale": 1, "output_type": "numpy", } return inputs def SCREAMING_SNAKE_CASE ( self :Any ): '''simple docstring''' A_ : Dict = "cpu" # ensure determinism for the device-dependent torch.Generator A_ : Optional[Any] = self.get_dummy_components() A_ : str = StableDiffusionInstructPixaPixPipeline(**__A ) A_ : Any = sd_pipe.to(__A ) sd_pipe.set_progress_bar_config(disable=__A ) A_ : List[Any] = self.get_dummy_inputs(__A ) A_ : List[Any] = sd_pipe(**__A ).images A_ : int = image[0, -3:, -3:, -1] assert image.shape == (1, 32, 32, 3) A_ : Union[str, Any] = np.array([0.7526, 0.3750, 0.4547, 0.6117, 0.5866, 0.5016, 0.4327, 0.5642, 0.4815] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-3 def SCREAMING_SNAKE_CASE ( self :int ): '''simple docstring''' A_ : List[Any] = "cpu" # ensure determinism for the device-dependent torch.Generator A_ : int = self.get_dummy_components() A_ : Union[str, Any] = StableDiffusionInstructPixaPixPipeline(**__A ) A_ : Union[str, Any] = sd_pipe.to(__A ) sd_pipe.set_progress_bar_config(disable=__A ) A_ : List[str] = self.get_dummy_inputs(__A ) A_ : int = "french fries" A_ : List[str] = sd_pipe(**__A , negative_prompt=__A ) A_ : List[str] = output.images A_ : Dict = image[0, -3:, -3:, -1] assert image.shape == (1, 32, 32, 3) A_ : List[str] = np.array([0.7511, 0.3642, 0.4553, 0.6236, 0.5797, 0.5013, 0.4343, 0.5611, 0.4831] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-3 def SCREAMING_SNAKE_CASE ( self :List[str] ): '''simple docstring''' A_ : List[str] = "cpu" # ensure determinism for the device-dependent torch.Generator A_ : Optional[Any] = self.get_dummy_components() A_ : Dict = StableDiffusionInstructPixaPixPipeline(**__A ) A_ : int = sd_pipe.to(__A ) sd_pipe.set_progress_bar_config(disable=__A ) A_ : str = self.get_dummy_inputs(__A ) A_ : str = [inputs["prompt"]] * 2 A_ : Union[str, Any] = np.array(inputs["image"] ).astype(np.floataa ) / 255.0 A_ : Union[str, Any] = torch.from_numpy(__A ).unsqueeze(0 ).to(__A ) A_ : Optional[Any] = image / 2 + 0.5 A_ : List[str] = image.permute(0 , 3 , 1 , 2 ) A_ : Dict = image.repeat(2 , 1 , 1 , 1 ) A_ : Optional[Any] = sd_pipe(**__A ).images A_ : Union[str, Any] = image[-1, -3:, -3:, -1] assert image.shape == (2, 32, 32, 3) A_ : str = np.array([0.5812, 0.5748, 0.5222, 0.5908, 0.5695, 0.7174, 0.6804, 0.5523, 0.5579] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-3 def SCREAMING_SNAKE_CASE ( self :Any ): '''simple docstring''' A_ : Dict = "cpu" # ensure determinism for the device-dependent torch.Generator A_ : int = self.get_dummy_components() A_ : int = EulerAncestralDiscreteScheduler( beta_start=0.00085 , beta_end=0.012 , beta_schedule="scaled_linear" ) A_ : List[Any] = StableDiffusionInstructPixaPixPipeline(**__A ) A_ : Dict = sd_pipe.to(__A ) sd_pipe.set_progress_bar_config(disable=__A ) A_ : str = self.get_dummy_inputs(__A ) A_ : Any = sd_pipe(**__A ).images A_ : Optional[int] = image[0, -3:, -3:, -1] A_ : Optional[Any] = [round(__A , 4 ) for x in image_slice.flatten().tolist()] print(",".join([str(__A ) for x in slice] ) ) assert image.shape == (1, 32, 32, 3) A_ : str = np.array([0.7417, 0.3842, 0.4732, 0.5776, 0.5891, 0.5139, 0.4052, 0.5673, 0.4986] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-3 def SCREAMING_SNAKE_CASE ( self :str ): '''simple docstring''' super().test_inference_batch_single_identical(expected_max_diff=3e-3 ) def SCREAMING_SNAKE_CASE ( self :Optional[int] ): '''simple docstring''' A_ : Tuple = self.get_dummy_components() A_ : List[Any] = StableDiffusionInstructPixaPixPipeline(**__A ) A_ : Dict = VaeImageProcessor(do_resize=__A , do_normalize=__A ) A_ : Dict = pipe.to(__A ) pipe.set_progress_bar_config(disable=__A ) A_ : int = pipe(**self.get_dummy_inputs_by_type(__A , input_image_type="pt" ) )[0] A_ : Optional[Any] = components["vae"] A_ : List[Any] = self.get_dummy_inputs_by_type(__A , input_image_type="pt" ) for image_param in self.image_latents_params: if image_param in inputs.keys(): A_ : int = vae.encode(inputs[image_param] ).latent_dist.mode() A_ : Dict = pipe(**__A )[0] A_ : int = np.abs(out - out_latents_inputs ).max() self.assertLess(__A , 1e-4 , "passing latents as image input generate different result from passing image" ) @slow @require_torch_gpu class __magic_name__ ( unittest.TestCase ): """simple docstring""" def SCREAMING_SNAKE_CASE ( self :str ): '''simple docstring''' super().tearDown() gc.collect() torch.cuda.empty_cache() def SCREAMING_SNAKE_CASE ( self :Dict , snake_case :Any=0 ): '''simple docstring''' A_ : int = torch.manual_seed(__A ) A_ : List[Any] = load_image( "https://huggingface.co/datasets/diffusers/test-arrays/resolve/main/stable_diffusion_pix2pix/example.jpg" ) A_ : Union[str, Any] = { "prompt": "turn him into a cyborg", "image": image, "generator": generator, "num_inference_steps": 3, "guidance_scale": 7.5, "image_guidance_scale": 1.0, "output_type": "numpy", } return inputs def SCREAMING_SNAKE_CASE ( self :str ): '''simple docstring''' A_ : List[Any] = StableDiffusionInstructPixaPixPipeline.from_pretrained( "timbrooks/instruct-pix2pix" , safety_checker=__A ) pipe.to(__A ) pipe.set_progress_bar_config(disable=__A ) pipe.enable_attention_slicing() A_ : str = self.get_inputs() A_ : Tuple = pipe(**__A ).images A_ : Dict = image[0, -3:, -3:, -1].flatten() assert image.shape == (1, 512, 512, 3) A_ : Union[str, Any] = np.array([0.5902, 0.6015, 0.6027, 0.5983, 0.6092, 0.6061, 0.5765, 0.5785, 0.5555] ) assert np.abs(expected_slice - image_slice ).max() < 1e-3 def SCREAMING_SNAKE_CASE ( self :Optional[Any] ): '''simple docstring''' A_ : int = StableDiffusionInstructPixaPixPipeline.from_pretrained( "timbrooks/instruct-pix2pix" , safety_checker=__A ) A_ : Optional[int] = LMSDiscreteScheduler.from_config(pipe.scheduler.config ) pipe.to(__A ) pipe.set_progress_bar_config(disable=__A ) pipe.enable_attention_slicing() A_ : str = self.get_inputs() A_ : Tuple = pipe(**__A ).images A_ : Optional[Any] = image[0, -3:, -3:, -1].flatten() assert image.shape == (1, 512, 512, 3) A_ : Optional[int] = np.array([0.6578, 0.6817, 0.6972, 0.6761, 0.6856, 0.6916, 0.6428, 0.6516, 0.6301] ) assert np.abs(expected_slice - image_slice ).max() < 1e-3 def SCREAMING_SNAKE_CASE ( self :List[str] ): '''simple docstring''' A_ : Union[str, Any] = StableDiffusionInstructPixaPixPipeline.from_pretrained( "timbrooks/instruct-pix2pix" , safety_checker=__A ) A_ : List[Any] = DDIMScheduler.from_config(pipe.scheduler.config ) pipe.to(__A ) pipe.set_progress_bar_config(disable=__A ) pipe.enable_attention_slicing() A_ : Dict = self.get_inputs() A_ : str = pipe(**__A ).images A_ : Union[str, Any] = image[0, -3:, -3:, -1].flatten() assert image.shape == (1, 512, 512, 3) A_ : List[Any] = np.array([0.3828, 0.3834, 0.3818, 0.3792, 0.3865, 0.3752, 0.3792, 0.3847, 0.3753] ) assert np.abs(expected_slice - image_slice ).max() < 1e-3 def SCREAMING_SNAKE_CASE ( self :List[Any] ): '''simple docstring''' A_ : List[str] = 0 def callback_fn(snake_case :int , snake_case :int , snake_case :torch.FloatTensor ) -> None: A_ : Union[str, Any] = True nonlocal number_of_steps number_of_steps += 1 if step == 1: A_ : List[Any] = latents.detach().cpu().numpy() assert latents.shape == (1, 4, 64, 64) A_ : List[str] = latents[0, -3:, -3:, -1] A_ : Dict = np.array([-0.2463, -0.4644, -0.9756, 1.5176, 1.4414, 0.7866, 0.9897, 0.8521, 0.7983] ) assert np.abs(latents_slice.flatten() - expected_slice ).max() < 5e-2 elif step == 2: A_ : List[Any] = latents.detach().cpu().numpy() assert latents.shape == (1, 4, 64, 64) A_ : Any = latents[0, -3:, -3:, -1] A_ : List[str] = np.array([-0.2644, -0.4626, -0.9653, 1.5176, 1.4551, 0.7686, 0.9805, 0.8452, 0.8115] ) assert np.abs(latents_slice.flatten() - expected_slice ).max() < 5e-2 A_ : Any = False A_ : List[Any] = StableDiffusionInstructPixaPixPipeline.from_pretrained( "timbrooks/instruct-pix2pix" , safety_checker=__A , torch_dtype=torch.floataa ) A_ : Optional[Any] = pipe.to(__A ) pipe.set_progress_bar_config(disable=__A ) pipe.enable_attention_slicing() A_ : Union[str, Any] = self.get_inputs() pipe(**__A , callback=__A , callback_steps=1 ) assert callback_fn.has_been_called assert number_of_steps == 3 def SCREAMING_SNAKE_CASE ( self :str ): '''simple docstring''' torch.cuda.empty_cache() torch.cuda.reset_max_memory_allocated() torch.cuda.reset_peak_memory_stats() A_ : Dict = StableDiffusionInstructPixaPixPipeline.from_pretrained( "timbrooks/instruct-pix2pix" , safety_checker=__A , torch_dtype=torch.floataa ) A_ : List[str] = pipe.to(__A ) pipe.set_progress_bar_config(disable=__A ) pipe.enable_attention_slicing(1 ) pipe.enable_sequential_cpu_offload() A_ : int = self.get_inputs() A_ : List[str] = pipe(**__A ) A_ : Dict = torch.cuda.max_memory_allocated() # make sure that less than 2.2 GB is allocated assert mem_bytes < 2.2 * 10**9 def SCREAMING_SNAKE_CASE ( self :int ): '''simple docstring''' A_ : str = self.get_inputs() # resize to resolution that is divisible by 8 but not 16 or 32 A_ : List[str] = inputs["image"].resize((504, 504) ) A_ : Tuple = "timbrooks/instruct-pix2pix" A_ : Optional[int] = StableDiffusionInstructPixaPixPipeline.from_pretrained( __A , safety_checker=__A , ) pipe.to(__A ) pipe.set_progress_bar_config(disable=__A ) pipe.enable_attention_slicing() A_ : Any = pipe(**__A ) A_ : List[Any] = output.images[0] A_ : List[Any] = image[255:258, 383:386, -1] assert image.shape == (504, 504, 3) A_ : List[Any] = np.array([0.2726, 0.2529, 0.2664, 0.2655, 0.2641, 0.2642, 0.2591, 0.2649, 0.2590] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 5e-3
358
from __future__ import annotations from itertools import permutations from random import randint from timeit import repeat def __snake_case ( ) -> tuple[list[int], int]: A_ : Dict = [randint(-1000 , 1000 ) for i in range(10 )] A_ : List[str] = randint(-5000 , 5000 ) return (arr, r) _lowerCAmelCase : List[Any] = make_dataset() def __snake_case ( _lowerCAmelCase : list[int] , _lowerCAmelCase : int ) -> tuple[int, ...]: for triplet in permutations(_lowerCAmelCase , 3 ): if sum(_lowerCAmelCase ) == target: return tuple(sorted(_lowerCAmelCase ) ) return (0, 0, 0) def __snake_case ( _lowerCAmelCase : list[int] , _lowerCAmelCase : int ) -> tuple[int, int, int]: arr.sort() A_ : Tuple = len(_lowerCAmelCase ) for i in range(n - 1 ): A_ , A_ : int = i + 1, n - 1 while left < right: if arr[i] + arr[left] + arr[right] == target: return (arr[i], arr[left], arr[right]) elif arr[i] + arr[left] + arr[right] < target: left += 1 elif arr[i] + arr[left] + arr[right] > target: right -= 1 return (0, 0, 0) def __snake_case ( ) -> tuple[float, float]: A_ : Union[str, Any] = "\nfrom __main__ import dataset, triplet_sum1, triplet_sum2\n" A_ : Tuple = "\ntriplet_sum1(*dataset)\n" A_ : Optional[Any] = "\ntriplet_sum2(*dataset)\n" A_ : List[str] = repeat(setup=_lowerCAmelCase , stmt=_lowerCAmelCase , repeat=5 , number=10000 ) A_ : Tuple = repeat(setup=_lowerCAmelCase , stmt=_lowerCAmelCase , repeat=5 , number=10000 ) return (min(_lowerCAmelCase ), min(_lowerCAmelCase )) if __name__ == "__main__": from doctest import testmod testmod() _lowerCAmelCase : Optional[Any] = solution_times() print(F'''The time for naive implementation is {times[0]}.''') print(F'''The time for optimized implementation is {times[1]}.''')
70
0
import os from pathlib import Path from unittest.mock import patch import pytest import zstandard as zstd from datasets.download.download_config import DownloadConfig from datasets.utils.file_utils import ( OfflineModeIsEnabled, cached_path, fsspec_get, fsspec_head, ftp_get, ftp_head, get_from_cache, http_get, http_head, ) lowerCAmelCase = '\\n Text data.\n Second line of data.' lowerCAmelCase = 'file' @pytest.fixture(scope='''session''' ) def _a ( SCREAMING_SNAKE_CASE ): """simple docstring""" lowercase__ = tmp_path_factory.mktemp('''data''' ) / (FILE_PATH + '''.zstd''') lowercase__ = bytes(SCREAMING_SNAKE_CASE , '''utf-8''' ) with zstd.open(SCREAMING_SNAKE_CASE , '''wb''' ) as f: f.write(SCREAMING_SNAKE_CASE ) return path @pytest.fixture def _a ( SCREAMING_SNAKE_CASE ): """simple docstring""" with open(os.path.join(tmpfs.local_root_dir , SCREAMING_SNAKE_CASE ) , '''w''' ) as f: f.write(SCREAMING_SNAKE_CASE ) return FILE_PATH @pytest.mark.parametrize('''compression_format''' , ['''gzip''', '''xz''', '''zstd'''] ) def _a ( SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ): """simple docstring""" lowercase__ = {'''gzip''': gz_file, '''xz''': xz_file, '''zstd''': zstd_path} lowercase__ = input_paths[compression_format] lowercase__ = tmp_path / '''cache''' lowercase__ = DownloadConfig(cache_dir=SCREAMING_SNAKE_CASE , extract_compressed_file=SCREAMING_SNAKE_CASE ) lowercase__ = cached_path(SCREAMING_SNAKE_CASE , download_config=SCREAMING_SNAKE_CASE ) with open(SCREAMING_SNAKE_CASE ) as f: lowercase__ = f.read() with open(SCREAMING_SNAKE_CASE ) as f: lowercase__ = f.read() assert extracted_file_content == expected_file_content @pytest.mark.parametrize('''default_extracted''' , [True, False] ) @pytest.mark.parametrize('''default_cache_dir''' , [True, False] ) def _a ( SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ): """simple docstring""" lowercase__ = '''custom_cache''' lowercase__ = '''custom_extracted_dir''' lowercase__ = tmp_path / '''custom_extracted_path''' if default_extracted: lowercase__ = ('''downloads''' if default_cache_dir else custom_cache_dir, '''extracted''') else: monkeypatch.setattr('''datasets.config.EXTRACTED_DATASETS_DIR''' , SCREAMING_SNAKE_CASE ) monkeypatch.setattr('''datasets.config.EXTRACTED_DATASETS_PATH''' , str(SCREAMING_SNAKE_CASE ) ) lowercase__ = custom_extracted_path.parts[-2:] if default_cache_dir else (custom_cache_dir, custom_extracted_dir) lowercase__ = xz_file lowercase__ = ( DownloadConfig(extract_compressed_file=SCREAMING_SNAKE_CASE ) if default_cache_dir else DownloadConfig(cache_dir=tmp_path / custom_cache_dir , extract_compressed_file=SCREAMING_SNAKE_CASE ) ) lowercase__ = cached_path(SCREAMING_SNAKE_CASE , download_config=SCREAMING_SNAKE_CASE ) assert Path(SCREAMING_SNAKE_CASE ).parent.parts[-2:] == expected def _a ( SCREAMING_SNAKE_CASE ): """simple docstring""" lowercase__ = str(Path(SCREAMING_SNAKE_CASE ).resolve() ) assert cached_path(SCREAMING_SNAKE_CASE ) == text_file # relative path lowercase__ = str(Path(SCREAMING_SNAKE_CASE ).resolve().relative_to(Path(os.getcwd() ) ) ) assert cached_path(SCREAMING_SNAKE_CASE ) == text_file def _a ( SCREAMING_SNAKE_CASE ): """simple docstring""" lowercase__ = str(tmp_path.resolve() / '''__missing_file__.txt''' ) with pytest.raises(SCREAMING_SNAKE_CASE ): cached_path(SCREAMING_SNAKE_CASE ) # relative path lowercase__ = '''./__missing_file__.txt''' with pytest.raises(SCREAMING_SNAKE_CASE ): cached_path(SCREAMING_SNAKE_CASE ) def _a ( SCREAMING_SNAKE_CASE ): """simple docstring""" lowercase__ = get_from_cache(f'tmp://{tmpfs_file}' ) with open(SCREAMING_SNAKE_CASE ) as f: lowercase__ = f.read() assert output_file_content == FILE_CONTENT @patch('''datasets.config.HF_DATASETS_OFFLINE''' , SCREAMING_SNAKE_CASE ) def _a ( ): """simple docstring""" with pytest.raises(SCREAMING_SNAKE_CASE ): cached_path('''https://huggingface.co''' ) @patch('''datasets.config.HF_DATASETS_OFFLINE''' , SCREAMING_SNAKE_CASE ) def _a ( SCREAMING_SNAKE_CASE ): """simple docstring""" lowercase__ = tmp_path_factory.mktemp('''data''' ) / '''file.html''' with pytest.raises(SCREAMING_SNAKE_CASE ): http_get('''https://huggingface.co''' , temp_file=SCREAMING_SNAKE_CASE ) with pytest.raises(SCREAMING_SNAKE_CASE ): http_head('''https://huggingface.co''' ) @patch('''datasets.config.HF_DATASETS_OFFLINE''' , SCREAMING_SNAKE_CASE ) def _a ( SCREAMING_SNAKE_CASE ): """simple docstring""" lowercase__ = tmp_path_factory.mktemp('''data''' ) / '''file.html''' with pytest.raises(SCREAMING_SNAKE_CASE ): ftp_get('''ftp://huggingface.co''' , temp_file=SCREAMING_SNAKE_CASE ) with pytest.raises(SCREAMING_SNAKE_CASE ): ftp_head('''ftp://huggingface.co''' ) @patch('''datasets.config.HF_DATASETS_OFFLINE''' , SCREAMING_SNAKE_CASE ) def _a ( SCREAMING_SNAKE_CASE ): """simple docstring""" lowercase__ = tmp_path_factory.mktemp('''data''' ) / '''file.html''' with pytest.raises(SCREAMING_SNAKE_CASE ): fsspec_get('''s3://huggingface.co''' , temp_file=SCREAMING_SNAKE_CASE ) with pytest.raises(SCREAMING_SNAKE_CASE ): fsspec_head('''s3://huggingface.co''' )
110
import unittest import numpy as np from transformers.testing_utils import require_torch, require_vision from transformers.utils import is_torch_available, is_vision_available from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs if is_torch_available(): import torch if is_vision_available(): from PIL import Image from transformers import LevitImageProcessor class _a ( unittest.TestCase ): def __init__( self: Dict , UpperCamelCase_: Any , UpperCamelCase_: Dict=7 , UpperCamelCase_: Optional[int]=3 , UpperCamelCase_: List[Any]=18 , UpperCamelCase_: Dict=30 , UpperCamelCase_: Optional[int]=400 , UpperCamelCase_: Optional[int]=True , UpperCamelCase_: Union[str, Any]=None , UpperCamelCase_: List[str]=True , UpperCamelCase_: Dict=None , UpperCamelCase_: Optional[int]=True , UpperCamelCase_: Tuple=[0.5, 0.5, 0.5] , UpperCamelCase_: List[str]=[0.5, 0.5, 0.5] , ) -> int: """simple docstring""" lowercase__ = size if size is not None else {'''shortest_edge''': 18} lowercase__ = crop_size if crop_size is not None else {'''height''': 18, '''width''': 18} lowercase__ = parent lowercase__ = batch_size lowercase__ = num_channels lowercase__ = image_size lowercase__ = min_resolution lowercase__ = max_resolution lowercase__ = do_resize lowercase__ = size lowercase__ = do_center_crop lowercase__ = crop_size lowercase__ = do_normalize lowercase__ = image_mean lowercase__ = image_std def lowerCamelCase_ ( self: List[str] ) -> Optional[Any]: """simple docstring""" return { "image_mean": self.image_mean, "image_std": self.image_std, "do_normalize": self.do_normalize, "do_resize": self.do_resize, "do_center_crop": self.do_center_crop, "size": self.size, "crop_size": self.crop_size, } @require_torch @require_vision class _a ( UpperCamelCase__ , unittest.TestCase ): _lowercase : List[str] = LevitImageProcessor if is_vision_available() else None def lowerCamelCase_ ( self: Optional[Any] ) -> List[str]: """simple docstring""" lowercase__ = LevitImageProcessingTester(self ) @property def lowerCamelCase_ ( self: List[Any] ) -> Any: """simple docstring""" return self.image_processor_tester.prepare_image_processor_dict() def lowerCamelCase_ ( self: Union[str, Any] ) -> str: """simple docstring""" lowercase__ = self.image_processing_class(**self.image_processor_dict ) self.assertTrue(hasattr(UpperCamelCase_ , '''image_mean''' ) ) self.assertTrue(hasattr(UpperCamelCase_ , '''image_std''' ) ) self.assertTrue(hasattr(UpperCamelCase_ , '''do_normalize''' ) ) self.assertTrue(hasattr(UpperCamelCase_ , '''do_resize''' ) ) self.assertTrue(hasattr(UpperCamelCase_ , '''do_center_crop''' ) ) self.assertTrue(hasattr(UpperCamelCase_ , '''size''' ) ) def lowerCamelCase_ ( self: str ) -> Tuple: """simple docstring""" lowercase__ = self.image_processing_class.from_dict(self.image_processor_dict ) self.assertEqual(image_processor.size , {'''shortest_edge''': 18} ) self.assertEqual(image_processor.crop_size , {'''height''': 18, '''width''': 18} ) lowercase__ = self.image_processing_class.from_dict(self.image_processor_dict , size=42 , crop_size=84 ) self.assertEqual(image_processor.size , {'''shortest_edge''': 42} ) self.assertEqual(image_processor.crop_size , {'''height''': 84, '''width''': 84} ) def lowerCamelCase_ ( self: List[str] ) -> Dict: """simple docstring""" pass def lowerCamelCase_ ( self: List[str] ) -> int: """simple docstring""" lowercase__ = self.image_processing_class(**self.image_processor_dict ) # create random PIL images lowercase__ = prepare_image_inputs(self.image_processor_tester , equal_resolution=UpperCamelCase_ ) for image in image_inputs: self.assertIsInstance(UpperCamelCase_ , Image.Image ) # Test not batched input lowercase__ = 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 lowercase__ = image_processing(UpperCamelCase_ , return_tensors='''pt''' ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size['''height'''], self.image_processor_tester.crop_size['''width'''], ) , ) def lowerCamelCase_ ( self: Union[str, Any] ) -> Optional[int]: """simple docstring""" lowercase__ = self.image_processing_class(**self.image_processor_dict ) # create random numpy tensors lowercase__ = prepare_image_inputs(self.image_processor_tester , equal_resolution=UpperCamelCase_ , numpify=UpperCamelCase_ ) for image in image_inputs: self.assertIsInstance(UpperCamelCase_ , np.ndarray ) # Test not batched input lowercase__ = 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 lowercase__ = image_processing(UpperCamelCase_ , return_tensors='''pt''' ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size['''height'''], self.image_processor_tester.crop_size['''width'''], ) , ) def lowerCamelCase_ ( self: Any ) -> Optional[Any]: """simple docstring""" lowercase__ = self.image_processing_class(**self.image_processor_dict ) # create random PyTorch tensors lowercase__ = prepare_image_inputs(self.image_processor_tester , equal_resolution=UpperCamelCase_ , torchify=UpperCamelCase_ ) for image in image_inputs: self.assertIsInstance(UpperCamelCase_ , torch.Tensor ) # Test not batched input lowercase__ = 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 lowercase__ = image_processing(UpperCamelCase_ , return_tensors='''pt''' ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size['''height'''], self.image_processor_tester.crop_size['''width'''], ) , )
110
1
import gc import random import tempfile import unittest import numpy as np import torch from transformers import CLIPTextConfig, CLIPTextModel, CLIPTokenizer from diffusers import AutoencoderKL, DDIMScheduler, LMSDiscreteScheduler, PNDMScheduler, UNetaDConditionModel from diffusers.pipelines.stable_diffusion_safe import StableDiffusionPipelineSafe as StableDiffusionPipeline from diffusers.utils import floats_tensor, nightly, torch_device from diffusers.utils.testing_utils import require_torch_gpu class __A( unittest.TestCase ): """simple docstring""" def UpperCAmelCase_ (self ): # clean up the VRAM after each test super().tearDown() gc.collect() torch.cuda.empty_cache() @property def UpperCAmelCase_ (self ): UpperCamelCase__ = 1 UpperCamelCase__ = 3 UpperCamelCase__ = (32, 32) UpperCamelCase__ = floats_tensor((batch_size, num_channels) + sizes , rng=random.Random(0 ) ).to(_a ) return image @property def UpperCAmelCase_ (self ): torch.manual_seed(0 ) UpperCamelCase__ = 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 ): torch.manual_seed(0 ) UpperCamelCase__ = 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 ): torch.manual_seed(0 ) UpperCamelCase__ = CLIPTextConfig( bos_token_id=0 , eos_token_id=2 , hidden_size=32 , intermediate_size=37 , layer_norm_eps=1E-05 , num_attention_heads=4 , num_hidden_layers=5 , pad_token_id=1 , vocab_size=10_00 , ) return CLIPTextModel(_a ) @property def UpperCAmelCase_ (self ): def extract(*SCREAMING_SNAKE_CASE_ , **SCREAMING_SNAKE_CASE_ ): class __A: """simple docstring""" def __init__(self ): UpperCamelCase__ = torch.ones([0] ) def UpperCAmelCase_ (self , SCREAMING_SNAKE_CASE_ ): self.pixel_values.to(_a ) return self return Out() return extract def UpperCAmelCase_ (self ): UpperCamelCase__ = '''cpu''' # ensure determinism for the device-dependent torch.Generator UpperCamelCase__ = self.dummy_cond_unet UpperCamelCase__ = DDIMScheduler( beta_start=0.0_0085 , beta_end=0.012 , beta_schedule="""scaled_linear""" , clip_sample=_a , set_alpha_to_one=_a , ) UpperCamelCase__ = self.dummy_vae UpperCamelCase__ = self.dummy_text_encoder UpperCamelCase__ = CLIPTokenizer.from_pretrained("""hf-internal-testing/tiny-random-clip""" ) # make sure here that pndm scheduler skips prk UpperCamelCase__ = StableDiffusionPipeline( unet=_a , scheduler=_a , vae=_a , text_encoder=_a , tokenizer=_a , safety_checker=_a , feature_extractor=self.dummy_extractor , ) UpperCamelCase__ = sd_pipe.to(_a ) sd_pipe.set_progress_bar_config(disable=_a ) UpperCamelCase__ = '''A painting of a squirrel eating a burger''' UpperCamelCase__ = torch.Generator(device=_a ).manual_seed(0 ) UpperCamelCase__ = sd_pipe([prompt] , generator=_a , guidance_scale=6.0 , num_inference_steps=2 , output_type="""np""" ) UpperCamelCase__ = output.images UpperCamelCase__ = torch.Generator(device=_a ).manual_seed(0 ) UpperCamelCase__ = sd_pipe( [prompt] , generator=_a , guidance_scale=6.0 , num_inference_steps=2 , output_type="""np""" , return_dict=_a , )[0] UpperCamelCase__ = image[0, -3:, -3:, -1] UpperCamelCase__ = image_from_tuple[0, -3:, -3:, -1] assert image.shape == (1, 64, 64, 3) UpperCamelCase__ = np.array([0.5756, 0.6118, 0.5005, 0.5041, 0.5471, 0.4726, 0.4976, 0.4865, 0.4864] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-2 assert np.abs(image_from_tuple_slice.flatten() - expected_slice ).max() < 1E-2 def UpperCAmelCase_ (self ): UpperCamelCase__ = '''cpu''' # ensure determinism for the device-dependent torch.Generator UpperCamelCase__ = self.dummy_cond_unet UpperCamelCase__ = PNDMScheduler(skip_prk_steps=_a ) UpperCamelCase__ = self.dummy_vae UpperCamelCase__ = self.dummy_text_encoder UpperCamelCase__ = CLIPTokenizer.from_pretrained("""hf-internal-testing/tiny-random-clip""" ) # make sure here that pndm scheduler skips prk UpperCamelCase__ = StableDiffusionPipeline( unet=_a , scheduler=_a , vae=_a , text_encoder=_a , tokenizer=_a , safety_checker=_a , feature_extractor=self.dummy_extractor , ) UpperCamelCase__ = sd_pipe.to(_a ) sd_pipe.set_progress_bar_config(disable=_a ) UpperCamelCase__ = '''A painting of a squirrel eating a burger''' UpperCamelCase__ = torch.Generator(device=_a ).manual_seed(0 ) UpperCamelCase__ = sd_pipe([prompt] , generator=_a , guidance_scale=6.0 , num_inference_steps=2 , output_type="""np""" ) UpperCamelCase__ = output.images UpperCamelCase__ = torch.Generator(device=_a ).manual_seed(0 ) UpperCamelCase__ = sd_pipe( [prompt] , generator=_a , guidance_scale=6.0 , num_inference_steps=2 , output_type="""np""" , return_dict=_a , )[0] UpperCamelCase__ = image[0, -3:, -3:, -1] UpperCamelCase__ = image_from_tuple[0, -3:, -3:, -1] assert image.shape == (1, 64, 64, 3) UpperCamelCase__ = np.array([0.5125, 0.5716, 0.4828, 0.5060, 0.5650, 0.4768, 0.5185, 0.4895, 0.4993] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-2 assert np.abs(image_from_tuple_slice.flatten() - expected_slice ).max() < 1E-2 def UpperCAmelCase_ (self ): UpperCamelCase__ = StableDiffusionPipeline.from_pretrained( """hf-internal-testing/tiny-stable-diffusion-lms-pipe""" , safety_checker=_a ) assert isinstance(_a , _a ) assert isinstance(pipe.scheduler , _a ) assert pipe.safety_checker is None UpperCamelCase__ = pipe("""example prompt""" , num_inference_steps=2 ).images[0] assert image is not None # check that there's no error when saving a pipeline with one of the models being None with tempfile.TemporaryDirectory() as tmpdirname: pipe.save_pretrained(_a ) UpperCamelCase__ = StableDiffusionPipeline.from_pretrained(_a ) # sanity check that the pipeline still works assert pipe.safety_checker is None UpperCamelCase__ = pipe("""example prompt""" , num_inference_steps=2 ).images[0] assert image is not None @unittest.skipIf(torch_device != """cuda""" , """This test requires a GPU""" ) def UpperCAmelCase_ (self ): UpperCamelCase__ = self.dummy_cond_unet UpperCamelCase__ = PNDMScheduler(skip_prk_steps=_a ) UpperCamelCase__ = self.dummy_vae UpperCamelCase__ = self.dummy_text_encoder UpperCamelCase__ = CLIPTokenizer.from_pretrained("""hf-internal-testing/tiny-random-clip""" ) # put models in fp16 UpperCamelCase__ = unet.half() UpperCamelCase__ = vae.half() UpperCamelCase__ = bert.half() # make sure here that pndm scheduler skips prk UpperCamelCase__ = StableDiffusionPipeline( unet=_a , scheduler=_a , vae=_a , text_encoder=_a , tokenizer=_a , safety_checker=_a , feature_extractor=self.dummy_extractor , ) UpperCamelCase__ = sd_pipe.to(_a ) sd_pipe.set_progress_bar_config(disable=_a ) UpperCamelCase__ = '''A painting of a squirrel eating a burger''' UpperCamelCase__ = sd_pipe([prompt] , num_inference_steps=2 , output_type="""np""" ).images assert image.shape == (1, 64, 64, 3) @nightly @require_torch_gpu class __A( unittest.TestCase ): """simple docstring""" def UpperCAmelCase_ (self ): # clean up the VRAM after each test super().tearDown() gc.collect() torch.cuda.empty_cache() def UpperCAmelCase_ (self ): UpperCamelCase__ = StableDiffusionPipeline.from_pretrained("""runwayml/stable-diffusion-v1-5""" , safety_checker=_a ) UpperCamelCase__ = LMSDiscreteScheduler.from_config(sd_pipe.scheduler.config ) UpperCamelCase__ = sd_pipe.to(_a ) sd_pipe.set_progress_bar_config(disable=_a ) UpperCamelCase__ = ( '''portrait of girl with smokey eyes makeup in abandoned hotel, grange clothes, redshift, wide high angle''' ''' coloured polaroid photograph with flash, kodak film, hyper real, stunning moody cinematography, with''' ''' anamorphic lenses, by maripol, fallen angels by wong kar - wai, style of suspiria and neon demon and''' ''' children from bahnhof zoo, detailed ''' ) UpperCamelCase__ = 40_03_66_03_46 UpperCamelCase__ = 7 # without safety guidance (sld_guidance_scale = 0) UpperCamelCase__ = torch.manual_seed(_a ) UpperCamelCase__ = sd_pipe( [prompt] , generator=_a , guidance_scale=_a , num_inference_steps=50 , output_type="""np""" , width=5_12 , height=5_12 , sld_guidance_scale=0 , ) UpperCamelCase__ = output.images UpperCamelCase__ = image[0, -3:, -3:, -1] UpperCamelCase__ = [0.2278, 0.2231, 0.2249, 0.2333, 0.2303, 0.1885, 0.2273, 0.2144, 0.2176] assert image.shape == (1, 5_12, 5_12, 3) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-2 # without safety guidance (strong configuration) UpperCamelCase__ = torch.manual_seed(_a ) UpperCamelCase__ = sd_pipe( [prompt] , generator=_a , guidance_scale=_a , num_inference_steps=50 , output_type="""np""" , width=5_12 , height=5_12 , sld_guidance_scale=20_00 , sld_warmup_steps=7 , sld_threshold=0.025 , sld_momentum_scale=0.5 , sld_mom_beta=0.7 , ) UpperCamelCase__ = output.images UpperCamelCase__ = image[0, -3:, -3:, -1] UpperCamelCase__ = [0.2383, 0.2276, 0.236, 0.2192, 0.2186, 0.2053, 0.1971, 0.1901, 0.1719] assert image.shape == (1, 5_12, 5_12, 3) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-2 def UpperCAmelCase_ (self ): UpperCamelCase__ = StableDiffusionPipeline.from_pretrained("""runwayml/stable-diffusion-v1-5""" , safety_checker=_a ) UpperCamelCase__ = LMSDiscreteScheduler.from_config(sd_pipe.scheduler.config ) UpperCamelCase__ = sd_pipe.to(_a ) sd_pipe.set_progress_bar_config(disable=_a ) UpperCamelCase__ = '''padme amidala taking a bath artwork, safe for work, no nudity''' UpperCamelCase__ = 27_34_97_17_55 UpperCamelCase__ = 7 UpperCamelCase__ = torch.manual_seed(_a ) UpperCamelCase__ = sd_pipe( [prompt] , generator=_a , guidance_scale=_a , num_inference_steps=50 , output_type="""np""" , width=5_12 , height=5_12 , sld_guidance_scale=0 , ) UpperCamelCase__ = output.images UpperCamelCase__ = image[0, -3:, -3:, -1] UpperCamelCase__ = [0.3502, 0.3622, 0.3396, 0.3642, 0.3478, 0.3318, 0.35, 0.3348, 0.3297] assert image.shape == (1, 5_12, 5_12, 3) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-2 UpperCamelCase__ = torch.manual_seed(_a ) UpperCamelCase__ = sd_pipe( [prompt] , generator=_a , guidance_scale=_a , num_inference_steps=50 , output_type="""np""" , width=5_12 , height=5_12 , sld_guidance_scale=20_00 , sld_warmup_steps=7 , sld_threshold=0.025 , sld_momentum_scale=0.5 , sld_mom_beta=0.7 , ) UpperCamelCase__ = output.images UpperCamelCase__ = image[0, -3:, -3:, -1] UpperCamelCase__ = [0.5531, 0.5206, 0.4895, 0.5156, 0.5182, 0.4751, 0.4802, 0.4803, 0.4443] assert image.shape == (1, 5_12, 5_12, 3) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-2 def UpperCAmelCase_ (self ): UpperCamelCase__ = StableDiffusionPipeline.from_pretrained("""runwayml/stable-diffusion-v1-5""" ) UpperCamelCase__ = sd_pipe.to(_a ) sd_pipe.set_progress_bar_config(disable=_a ) UpperCamelCase__ = ( '''the four horsewomen of the apocalypse, painting by tom of finland, gaston bussiere, craig mullins, j. c.''' ''' leyendecker''' ) UpperCamelCase__ = 10_44_35_52_34 UpperCamelCase__ = 12 UpperCamelCase__ = torch.manual_seed(_a ) UpperCamelCase__ = sd_pipe( [prompt] , generator=_a , guidance_scale=_a , num_inference_steps=50 , output_type="""np""" , width=5_12 , height=5_12 , sld_guidance_scale=0 , ) UpperCamelCase__ = output.images UpperCamelCase__ = image[0, -3:, -3:, -1] UpperCamelCase__ = np.array([0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0] ) assert image.shape == (1, 5_12, 5_12, 3) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-7 UpperCamelCase__ = torch.manual_seed(_a ) UpperCamelCase__ = sd_pipe( [prompt] , generator=_a , guidance_scale=_a , num_inference_steps=50 , output_type="""np""" , width=5_12 , height=5_12 , sld_guidance_scale=20_00 , sld_warmup_steps=7 , sld_threshold=0.025 , sld_momentum_scale=0.5 , sld_mom_beta=0.7 , ) UpperCamelCase__ = output.images UpperCamelCase__ = image[0, -3:, -3:, -1] UpperCamelCase__ = np.array([0.5818, 0.6285, 0.6835, 0.6019, 0.625, 0.6754, 0.6096, 0.6334, 0.6561] ) assert image.shape == (1, 5_12, 5_12, 3) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-2
365
import shutil import tempfile import unittest import numpy as np import pytest from transformers.testing_utils import require_vision from transformers.utils import is_vision_available if is_vision_available(): from PIL import Image from transformers import ( AutoProcessor, BertTokenizerFast, BlipImageProcessor, GPTaTokenizer, InstructBlipProcessor, PreTrainedTokenizerFast, ) @require_vision class __A( unittest.TestCase ): """simple docstring""" def UpperCAmelCase_ (self ): UpperCamelCase__ = tempfile.mkdtemp() UpperCamelCase__ = BlipImageProcessor() UpperCamelCase__ = GPTaTokenizer.from_pretrained("""hf-internal-testing/tiny-random-GPT2Model""" ) UpperCamelCase__ = BertTokenizerFast.from_pretrained("""hf-internal-testing/tiny-random-bert""" ) UpperCamelCase__ = InstructBlipProcessor(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) processor.save_pretrained(self.tmpdirname ) def UpperCAmelCase_ (self , **SCREAMING_SNAKE_CASE_ ): return AutoProcessor.from_pretrained(self.tmpdirname , **SCREAMING_SNAKE_CASE_ ).tokenizer def UpperCAmelCase_ (self , **SCREAMING_SNAKE_CASE_ ): return AutoProcessor.from_pretrained(self.tmpdirname , **SCREAMING_SNAKE_CASE_ ).image_processor def UpperCAmelCase_ (self , **SCREAMING_SNAKE_CASE_ ): return AutoProcessor.from_pretrained(self.tmpdirname , **SCREAMING_SNAKE_CASE_ ).qformer_tokenizer def UpperCAmelCase_ (self ): shutil.rmtree(self.tmpdirname ) def UpperCAmelCase_ (self ): UpperCamelCase__ = [np.random.randint(2_55 , size=(3, 30, 4_00) , dtype=np.uinta )] UpperCamelCase__ = [Image.fromarray(np.moveaxis(SCREAMING_SNAKE_CASE_ , 0 , -1 ) ) for x in image_inputs] return image_inputs def UpperCAmelCase_ (self ): UpperCamelCase__ = InstructBlipProcessor( tokenizer=self.get_tokenizer() , image_processor=self.get_image_processor() , qformer_tokenizer=self.get_qformer_tokenizer() , ) processor.save_pretrained(self.tmpdirname ) UpperCamelCase__ = self.get_tokenizer(bos_token="""(BOS)""" , eos_token="""(EOS)""" ) UpperCamelCase__ = self.get_image_processor(do_normalize=SCREAMING_SNAKE_CASE_ , padding_value=1.0 ) UpperCamelCase__ = InstructBlipProcessor.from_pretrained( self.tmpdirname , bos_token="""(BOS)""" , eos_token="""(EOS)""" , do_normalize=SCREAMING_SNAKE_CASE_ , padding_value=1.0 ) self.assertEqual(processor.tokenizer.get_vocab() , tokenizer_add_kwargs.get_vocab() ) self.assertIsInstance(processor.tokenizer , SCREAMING_SNAKE_CASE_ ) self.assertEqual(processor.image_processor.to_json_string() , image_processor_add_kwargs.to_json_string() ) self.assertIsInstance(processor.image_processor , SCREAMING_SNAKE_CASE_ ) self.assertIsInstance(processor.qformer_tokenizer , SCREAMING_SNAKE_CASE_ ) def UpperCAmelCase_ (self ): UpperCamelCase__ = self.get_image_processor() UpperCamelCase__ = self.get_tokenizer() UpperCamelCase__ = self.get_qformer_tokenizer() UpperCamelCase__ = InstructBlipProcessor( tokenizer=SCREAMING_SNAKE_CASE_ , image_processor=SCREAMING_SNAKE_CASE_ , qformer_tokenizer=SCREAMING_SNAKE_CASE_ ) UpperCamelCase__ = self.prepare_image_inputs() UpperCamelCase__ = image_processor(SCREAMING_SNAKE_CASE_ , return_tensors="""np""" ) UpperCamelCase__ = processor(images=SCREAMING_SNAKE_CASE_ , 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 ): UpperCamelCase__ = self.get_image_processor() UpperCamelCase__ = self.get_tokenizer() UpperCamelCase__ = self.get_qformer_tokenizer() UpperCamelCase__ = InstructBlipProcessor( tokenizer=SCREAMING_SNAKE_CASE_ , image_processor=SCREAMING_SNAKE_CASE_ , qformer_tokenizer=SCREAMING_SNAKE_CASE_ ) UpperCamelCase__ = """lower newer""" UpperCamelCase__ = processor(text=SCREAMING_SNAKE_CASE_ ) UpperCamelCase__ = tokenizer(SCREAMING_SNAKE_CASE_ , return_token_type_ids=SCREAMING_SNAKE_CASE_ ) UpperCamelCase__ = qformer_tokenizer(SCREAMING_SNAKE_CASE_ , return_token_type_ids=SCREAMING_SNAKE_CASE_ ) for key in encoded_tokens.keys(): self.assertListEqual(encoded_tokens[key] , encoded_processor[key] ) for key in encoded_tokens_qformer.keys(): self.assertListEqual(encoded_tokens_qformer[key] , encoded_processor["""qformer_""" + key] ) def UpperCAmelCase_ (self ): UpperCamelCase__ = self.get_image_processor() UpperCamelCase__ = self.get_tokenizer() UpperCamelCase__ = self.get_qformer_tokenizer() UpperCamelCase__ = InstructBlipProcessor( tokenizer=SCREAMING_SNAKE_CASE_ , image_processor=SCREAMING_SNAKE_CASE_ , qformer_tokenizer=SCREAMING_SNAKE_CASE_ ) UpperCamelCase__ = """lower newer""" UpperCamelCase__ = self.prepare_image_inputs() UpperCamelCase__ = processor(text=SCREAMING_SNAKE_CASE_ , images=SCREAMING_SNAKE_CASE_ ) self.assertListEqual( list(inputs.keys() ) , ["""input_ids""", """attention_mask""", """qformer_input_ids""", """qformer_attention_mask""", """pixel_values"""] , ) # test if it raises when no input is passed with pytest.raises(SCREAMING_SNAKE_CASE_ ): processor() def UpperCAmelCase_ (self ): UpperCamelCase__ = self.get_image_processor() UpperCamelCase__ = self.get_tokenizer() UpperCamelCase__ = self.get_qformer_tokenizer() UpperCamelCase__ = InstructBlipProcessor( tokenizer=SCREAMING_SNAKE_CASE_ , image_processor=SCREAMING_SNAKE_CASE_ , qformer_tokenizer=SCREAMING_SNAKE_CASE_ ) UpperCamelCase__ = [[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]] UpperCamelCase__ = processor.batch_decode(SCREAMING_SNAKE_CASE_ ) UpperCamelCase__ = tokenizer.batch_decode(SCREAMING_SNAKE_CASE_ ) self.assertListEqual(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) def UpperCAmelCase_ (self ): UpperCamelCase__ = self.get_image_processor() UpperCamelCase__ = self.get_tokenizer() UpperCamelCase__ = self.get_qformer_tokenizer() UpperCamelCase__ = InstructBlipProcessor( tokenizer=SCREAMING_SNAKE_CASE_ , image_processor=SCREAMING_SNAKE_CASE_ , qformer_tokenizer=SCREAMING_SNAKE_CASE_ ) UpperCamelCase__ = """lower newer""" UpperCamelCase__ = self.prepare_image_inputs() UpperCamelCase__ = processor(text=SCREAMING_SNAKE_CASE_ , images=SCREAMING_SNAKE_CASE_ ) self.assertListEqual( list(inputs.keys() ) , ["""input_ids""", """attention_mask""", """qformer_input_ids""", """qformer_attention_mask""", """pixel_values"""] , )
178
0
import argparse import os import re import tensorflow as tf import torch from transformers import BertConfig, BertModel from transformers.utils import logging logging.set_verbosity_info() lowerCAmelCase = logging.get_logger(__name__) def _a ( SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ): """simple docstring""" lowercase__ = os.path.abspath(SCREAMING_SNAKE_CASE ) logger.info(f'Converting TensorFlow checkpoint from {tf_path}' ) # Load weights from TF model lowercase__ = tf.train.list_variables(SCREAMING_SNAKE_CASE ) lowercase__ = [] lowercase__ = [] lowercase__ = [] for full_name, shape in init_vars: # logger.info(f"Loading TF weight {name} with shape {shape}") lowercase__ = full_name.split('''/''' ) if full_name == "_CHECKPOINTABLE_OBJECT_GRAPH" or name[0] in ["global_step", "save_counter"]: logger.info(f'Skipping non-model layer {full_name}' ) continue if "optimizer" in full_name: logger.info(f'Skipping optimization layer {full_name}' ) continue if name[0] == "model": # ignore initial 'model' lowercase__ = name[1:] # figure out how many levels deep the name is lowercase__ = 0 for _name in name: if _name.startswith('''layer_with_weights''' ): depth += 1 else: break layer_depth.append(SCREAMING_SNAKE_CASE ) # read data lowercase__ = tf.train.load_variable(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ) names.append('''/'''.join(SCREAMING_SNAKE_CASE ) ) arrays.append(SCREAMING_SNAKE_CASE ) logger.info(f'Read a total of {len(SCREAMING_SNAKE_CASE ):,} layers' ) # Sanity check if len(set(SCREAMING_SNAKE_CASE ) ) != 1: raise ValueError(f'Found layer names with different depths (layer depth {list(set(SCREAMING_SNAKE_CASE ) )})' ) lowercase__ = list(set(SCREAMING_SNAKE_CASE ) )[0] if layer_depth != 1: raise ValueError( '''The model contains more than just the embedding/encoder layers. This script does not handle MLM/NSP''' ''' heads.''' ) # convert layers logger.info('''Converting weights...''' ) for full_name, array in zip(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ): lowercase__ = full_name.split('''/''' ) lowercase__ = model lowercase__ = [] for i, m_name in enumerate(SCREAMING_SNAKE_CASE ): if m_name == ".ATTRIBUTES": # variable names end with .ATTRIBUTES/VARIABLE_VALUE break if m_name.startswith('''layer_with_weights''' ): lowercase__ = int(m_name.split('''-''' )[-1] ) if layer_num <= 2: # embedding layers # layer_num 0: word_embeddings # layer_num 1: position_embeddings # layer_num 2: token_type_embeddings continue elif layer_num == 3: # embedding LayerNorm trace.extend(['''embeddings''', '''LayerNorm'''] ) lowercase__ = getattr(SCREAMING_SNAKE_CASE , '''embeddings''' ) lowercase__ = getattr(SCREAMING_SNAKE_CASE , '''LayerNorm''' ) elif layer_num > 3 and layer_num < config.num_hidden_layers + 4: # encoder layers trace.extend(['''encoder''', '''layer''', str(layer_num - 4 )] ) lowercase__ = getattr(SCREAMING_SNAKE_CASE , '''encoder''' ) lowercase__ = getattr(SCREAMING_SNAKE_CASE , '''layer''' ) lowercase__ = pointer[layer_num - 4] elif layer_num == config.num_hidden_layers + 4: # pooler layer trace.extend(['''pooler''', '''dense'''] ) lowercase__ = getattr(SCREAMING_SNAKE_CASE , '''pooler''' ) lowercase__ = getattr(SCREAMING_SNAKE_CASE , '''dense''' ) elif m_name == "embeddings": trace.append('''embeddings''' ) lowercase__ = getattr(SCREAMING_SNAKE_CASE , '''embeddings''' ) if layer_num == 0: trace.append('''word_embeddings''' ) lowercase__ = getattr(SCREAMING_SNAKE_CASE , '''word_embeddings''' ) elif layer_num == 1: trace.append('''position_embeddings''' ) lowercase__ = getattr(SCREAMING_SNAKE_CASE , '''position_embeddings''' ) elif layer_num == 2: trace.append('''token_type_embeddings''' ) lowercase__ = getattr(SCREAMING_SNAKE_CASE , '''token_type_embeddings''' ) else: raise ValueError(f'Unknown embedding layer with name {full_name}' ) trace.append('''weight''' ) lowercase__ = getattr(SCREAMING_SNAKE_CASE , '''weight''' ) elif m_name == "_attention_layer": # self-attention layer trace.extend(['''attention''', '''self'''] ) lowercase__ = getattr(SCREAMING_SNAKE_CASE , '''attention''' ) lowercase__ = getattr(SCREAMING_SNAKE_CASE , '''self''' ) elif m_name == "_attention_layer_norm": # output attention norm trace.extend(['''attention''', '''output''', '''LayerNorm'''] ) lowercase__ = getattr(SCREAMING_SNAKE_CASE , '''attention''' ) lowercase__ = getattr(SCREAMING_SNAKE_CASE , '''output''' ) lowercase__ = getattr(SCREAMING_SNAKE_CASE , '''LayerNorm''' ) elif m_name == "_attention_output_dense": # output attention dense trace.extend(['''attention''', '''output''', '''dense'''] ) lowercase__ = getattr(SCREAMING_SNAKE_CASE , '''attention''' ) lowercase__ = getattr(SCREAMING_SNAKE_CASE , '''output''' ) lowercase__ = getattr(SCREAMING_SNAKE_CASE , '''dense''' ) elif m_name == "_output_dense": # output dense trace.extend(['''output''', '''dense'''] ) lowercase__ = getattr(SCREAMING_SNAKE_CASE , '''output''' ) lowercase__ = getattr(SCREAMING_SNAKE_CASE , '''dense''' ) elif m_name == "_output_layer_norm": # output dense trace.extend(['''output''', '''LayerNorm'''] ) lowercase__ = getattr(SCREAMING_SNAKE_CASE , '''output''' ) lowercase__ = getattr(SCREAMING_SNAKE_CASE , '''LayerNorm''' ) elif m_name == "_key_dense": # attention key trace.append('''key''' ) lowercase__ = getattr(SCREAMING_SNAKE_CASE , '''key''' ) elif m_name == "_query_dense": # attention query trace.append('''query''' ) lowercase__ = getattr(SCREAMING_SNAKE_CASE , '''query''' ) elif m_name == "_value_dense": # attention value trace.append('''value''' ) lowercase__ = getattr(SCREAMING_SNAKE_CASE , '''value''' ) elif m_name == "_intermediate_dense": # attention intermediate dense trace.extend(['''intermediate''', '''dense'''] ) lowercase__ = getattr(SCREAMING_SNAKE_CASE , '''intermediate''' ) lowercase__ = getattr(SCREAMING_SNAKE_CASE , '''dense''' ) elif m_name == "_output_layer_norm": # output layer norm trace.append('''output''' ) lowercase__ = getattr(SCREAMING_SNAKE_CASE , '''output''' ) # weights & biases elif m_name in ["bias", "beta"]: trace.append('''bias''' ) lowercase__ = getattr(SCREAMING_SNAKE_CASE , '''bias''' ) elif m_name in ["kernel", "gamma"]: trace.append('''weight''' ) lowercase__ = getattr(SCREAMING_SNAKE_CASE , '''weight''' ) else: logger.warning(f'Ignored {m_name}' ) # for certain layers reshape is necessary lowercase__ = '''.'''.join(SCREAMING_SNAKE_CASE ) if re.match(R'''(\S+)\.attention\.self\.(key|value|query)\.(bias|weight)''' , SCREAMING_SNAKE_CASE ) or re.match( R'''(\S+)\.attention\.output\.dense\.weight''' , SCREAMING_SNAKE_CASE ): lowercase__ = array.reshape(pointer.data.shape ) if "kernel" in full_name: lowercase__ = array.transpose() if pointer.shape == array.shape: lowercase__ = torch.from_numpy(SCREAMING_SNAKE_CASE ) else: raise ValueError( f'Shape mismatch in layer {full_name}: Model expects shape {pointer.shape} but layer contains shape:' f' {array.shape}' ) logger.info(f'Successfully set variable {full_name} to PyTorch layer {trace}' ) return model def _a ( SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ): """simple docstring""" logger.info(f'Loading model based on config from {config_path}...' ) lowercase__ = BertConfig.from_json_file(SCREAMING_SNAKE_CASE ) lowercase__ = BertModel(SCREAMING_SNAKE_CASE ) # Load weights from checkpoint logger.info(f'Loading weights from checkpoint {tf_checkpoint_path}...' ) load_tfa_weights_in_bert(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ) # Save pytorch-model logger.info(f'Saving PyTorch model to {pytorch_dump_path}...' ) torch.save(model.state_dict() , SCREAMING_SNAKE_CASE ) if __name__ == "__main__": lowerCAmelCase = argparse.ArgumentParser() parser.add_argument( '--tf_checkpoint_path', type=str, required=True, help='Path to the TensorFlow 2.x checkpoint path.' ) parser.add_argument( '--bert_config_file', type=str, required=True, help='The config json file corresponding to the BERT model. This specifies the model architecture.', ) parser.add_argument( '--pytorch_dump_path', type=str, required=True, help='Path to the output PyTorch model (must include filename).', ) lowerCAmelCase = parser.parse_args() convert_tfa_checkpoint_to_pytorch(args.tf_checkpoint_path, args.bert_config_file, args.pytorch_dump_path)
110
'''simple docstring''' import logging import os from .state import PartialState class a__ ( logging.LoggerAdapter ): @staticmethod def SCREAMING_SNAKE_CASE__ ( a : Optional[Any] ): """simple docstring""" __lowerCamelCase = PartialState() return not main_process_only or (main_process_only and state.is_main_process) def SCREAMING_SNAKE_CASE__ ( self : int , a : Optional[int] , a : str , *a : Optional[int] , **a : List[Any] ): """simple docstring""" if PartialState._shared_state == {}: raise RuntimeError( '''You must initialize the accelerate state by calling either `PartialState()` or `Accelerator()` before using the logging utility.''' ) __lowerCamelCase = kwargs.pop('''main_process_only''' , a ) __lowerCamelCase = kwargs.pop('''in_order''' , a ) if self.isEnabledFor(a ): if self._should_log(a ): __lowerCamelCase , __lowerCamelCase = self.process(a , a ) self.logger.log(a , a , *a , **a ) elif in_order: __lowerCamelCase = PartialState() for i in range(state.num_processes ): if i == state.process_index: __lowerCamelCase , __lowerCamelCase = self.process(a , a ) self.logger.log(a , a , *a , **a ) state.wait_for_everyone() def __lowerCAmelCase ( UpperCamelCase__ , UpperCamelCase__ = None ) -> Optional[int]: if log_level is None: __lowerCamelCase = os.environ.get('''ACCELERATE_LOG_LEVEL''' , UpperCamelCase__ ) __lowerCamelCase = logging.getLogger(UpperCamelCase__ ) if log_level is not None: logger.setLevel(log_level.upper() ) logger.root.setLevel(log_level.upper() ) return MultiProcessAdapter(UpperCamelCase__ , {} )
67
0
'''simple docstring''' import argparse import requests import torch from PIL import Image from transformers import SwinConfig, SwinForMaskedImageModeling, ViTImageProcessor def lowerCamelCase (_SCREAMING_SNAKE_CASE : str ): __a : Tuple = SwinConfig(image_size=192 ) if "base" in model_name: __a : Optional[int] = 6 __a : Optional[Any] = 128 __a : Union[str, Any] = (2, 2, 18, 2) __a : Tuple = (4, 8, 16, 32) elif "large" in model_name: __a : List[Any] = 12 __a : str = 192 __a : List[str] = (2, 2, 18, 2) __a : Optional[int] = (6, 12, 24, 48) else: raise ValueError('Model not supported, only supports base and large variants' ) __a : Tuple = window_size __a : Optional[int] = embed_dim __a : str = depths __a : Dict = num_heads return config def lowerCamelCase (_SCREAMING_SNAKE_CASE : Tuple ): if "encoder.mask_token" in name: __a : List[Any] = name.replace('encoder.mask_token' , 'embeddings.mask_token' ) if "encoder.patch_embed.proj" in name: __a : int = name.replace('encoder.patch_embed.proj' , 'embeddings.patch_embeddings.projection' ) if "encoder.patch_embed.norm" in name: __a : List[str] = name.replace('encoder.patch_embed.norm' , 'embeddings.norm' ) if "attn.proj" in name: __a : List[Any] = name.replace('attn.proj' , 'attention.output.dense' ) if "attn" in name: __a : Union[str, Any] = name.replace('attn' , 'attention.self' ) if "norm1" in name: __a : Any = name.replace('norm1' , 'layernorm_before' ) if "norm2" in name: __a : Any = name.replace('norm2' , 'layernorm_after' ) if "mlp.fc1" in name: __a : Dict = name.replace('mlp.fc1' , 'intermediate.dense' ) if "mlp.fc2" in name: __a : Dict = name.replace('mlp.fc2' , 'output.dense' ) if name == "encoder.norm.weight": __a : List[str] = 'layernorm.weight' if name == "encoder.norm.bias": __a : Optional[Any] = 'layernorm.bias' if "decoder" in name: pass else: __a : Optional[Any] = 'swin.' + name return name def lowerCamelCase (_SCREAMING_SNAKE_CASE : int , _SCREAMING_SNAKE_CASE : List[Any] ): for key in orig_state_dict.copy().keys(): __a : int = orig_state_dict.pop(_SCREAMING_SNAKE_CASE ) if "attn_mask" in key: pass elif "qkv" in key: __a : List[str] = key.split('.' ) __a : str = int(key_split[2] ) __a : List[str] = int(key_split[4] ) __a : Optional[int] = model.swin.encoder.layers[layer_num].blocks[block_num].attention.self.all_head_size if "weight" in key: __a : List[Any] = val[:dim, :] __a : Optional[int] = val[ dim : dim * 2, : ] __a : Dict = val[-dim:, :] else: __a : int = val[ :dim ] __a : Optional[Any] = val[ dim : dim * 2 ] __a : List[Any] = val[ -dim: ] else: __a : Any = val return orig_state_dict def lowerCamelCase (_SCREAMING_SNAKE_CASE : Any , _SCREAMING_SNAKE_CASE : int , _SCREAMING_SNAKE_CASE : str , _SCREAMING_SNAKE_CASE : Optional[int] ): __a : int = torch.load(_SCREAMING_SNAKE_CASE , map_location='cpu' )['model'] __a : Any = get_swin_config(_SCREAMING_SNAKE_CASE ) __a : List[str] = SwinForMaskedImageModeling(_SCREAMING_SNAKE_CASE ) model.eval() __a : List[Any] = convert_state_dict(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) model.load_state_dict(_SCREAMING_SNAKE_CASE ) __a : Dict = 'http://images.cocodataset.org/val2017/000000039769.jpg' __a : Any = ViTImageProcessor(size={'height': 192, 'width': 192} ) __a : Optional[int] = Image.open(requests.get(_SCREAMING_SNAKE_CASE , stream=_SCREAMING_SNAKE_CASE ).raw ) __a : List[str] = image_processor(images=_SCREAMING_SNAKE_CASE , return_tensors='pt' ) with torch.no_grad(): __a : Optional[Any] = model(**_SCREAMING_SNAKE_CASE ).logits print(outputs.keys() ) print('Looks ok!' ) if pytorch_dump_folder_path is not None: print(F"""Saving model {model_name} to {pytorch_dump_folder_path}""" ) model.save_pretrained(_SCREAMING_SNAKE_CASE ) print(F"""Saving image processor to {pytorch_dump_folder_path}""" ) image_processor.save_pretrained(_SCREAMING_SNAKE_CASE ) if push_to_hub: print(F"""Pushing model and image processor for {model_name} to hub""" ) model.push_to_hub(F"""microsoft/{model_name}""" ) image_processor.push_to_hub(F"""microsoft/{model_name}""" ) if __name__ == "__main__": __lowercase : Any = argparse.ArgumentParser() # Required parameters parser.add_argument( '--model_name', default='swin-base-simmim-window6-192', type=str, choices=['swin-base-simmim-window6-192', 'swin-large-simmim-window12-192'], help='Name of the Swin SimMIM model you\'d like to convert.', ) parser.add_argument( '--checkpoint_path', default='/Users/nielsrogge/Documents/SwinSimMIM/simmim_pretrain__swin_base__img192_window6__100ep.pth', type=str, help='Path to the original PyTorch checkpoint (.pth file).', ) parser.add_argument( '--pytorch_dump_folder_path', default=None, type=str, help='Path to the output PyTorch model directory.' ) parser.add_argument( '--push_to_hub', action='store_true', help='Whether or not to push the converted model to the 🤗 hub.' ) __lowercase : Tuple = parser.parse_args() convert_swin_checkpoint(args.model_name, args.checkpoint_path, args.pytorch_dump_folder_path, args.push_to_hub)
294
'''simple docstring''' import re from filelock import FileLock try: import nltk __lowercase : Optional[Any] = True except (ImportError, ModuleNotFoundError): __lowercase : Dict = False if NLTK_AVAILABLE: with FileLock('.lock') as lock: nltk.download('punkt', quiet=True) def lowerCamelCase (_SCREAMING_SNAKE_CASE : str ): re.sub('<n>' , '' , _SCREAMING_SNAKE_CASE ) # remove pegasus newline char assert NLTK_AVAILABLE, "nltk must be installed to separate newlines between sentences. (pip install nltk)" return "\n".join(nltk.sent_tokenize(_SCREAMING_SNAKE_CASE ) )
294
1
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_ : Union[str, Any] = logging.get_logger(__name__) class _snake_case ( A__ ): _lowercase : Optional[Any] = ['''pixel_values'''] def __init__( self , a = True , a = None , a = 0.9 , a = PILImageResampling.BICUBIC , a = True , a = None , a = 1 / 255 , a = True , a = True , a = None , a = None , **a , ) -> None: super().__init__(**a) SCREAMING_SNAKE_CASE = size if size is not None else {'shortest_edge': 224} SCREAMING_SNAKE_CASE = get_size_dict(a , default_to_square=a) SCREAMING_SNAKE_CASE = crop_size if crop_size is not None else {'height': 224, 'width': 224} SCREAMING_SNAKE_CASE = get_size_dict(a , param_name='crop_size') SCREAMING_SNAKE_CASE = do_resize SCREAMING_SNAKE_CASE = size SCREAMING_SNAKE_CASE = crop_pct SCREAMING_SNAKE_CASE = resample SCREAMING_SNAKE_CASE = do_center_crop SCREAMING_SNAKE_CASE = crop_size SCREAMING_SNAKE_CASE = do_rescale SCREAMING_SNAKE_CASE = rescale_factor SCREAMING_SNAKE_CASE = do_normalize SCREAMING_SNAKE_CASE = image_mean if image_mean is not None else IMAGENET_DEFAULT_MEAN SCREAMING_SNAKE_CASE = image_std if image_std is not None else IMAGENET_DEFAULT_STD def SCREAMING_SNAKE_CASE__ ( self , a , a , a = None , a = PILImageResampling.BICUBIC , a = None , **a , ) -> np.ndarray: SCREAMING_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: SCREAMING_SNAKE_CASE = int(size['shortest_edge'] / crop_pct) elif "height" in size and "width" in size: if size["height"] == size["width"]: SCREAMING_SNAKE_CASE = int(size['height'] / crop_pct) else: SCREAMING_SNAKE_CASE = (int(size['height'] / crop_pct), int(size['width'] / crop_pct)) else: raise ValueError('Invalid size for resize: {}'.format(a)) SCREAMING_SNAKE_CASE = get_resize_output_image_size(a , size=a , default_to_square=a) else: if "shortest_edge" in size: SCREAMING_SNAKE_CASE = get_resize_output_image_size(a , size=size['shortest_edge'] , default_to_square=a) elif "height" in size and "width" in size: SCREAMING_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 , a , a , a = None , **a , ) -> np.ndarray: SCREAMING_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 , a , a , a = None , **a , ) -> Optional[int]: return rescale(a , scale=a , data_format=a , **a) def SCREAMING_SNAKE_CASE__ ( self , a , a , a , a = None , **a , ) -> np.ndarray: return normalize(a , mean=a , std=a , data_format=a , **a) def SCREAMING_SNAKE_CASE__ ( self , a , a = None , a = None , a = None , a = None , a = None , a = None , a = None , a = None , a = None , a = None , a = None , a = None , a = ChannelDimension.FIRST , **a , ) -> PIL.Image.Image: SCREAMING_SNAKE_CASE = do_resize if do_resize is not None else self.do_resize SCREAMING_SNAKE_CASE = crop_pct if crop_pct is not None else self.crop_pct SCREAMING_SNAKE_CASE = resample if resample is not None else self.resample SCREAMING_SNAKE_CASE = do_center_crop if do_center_crop is not None else self.do_center_crop SCREAMING_SNAKE_CASE = do_rescale if do_rescale is not None else self.do_rescale SCREAMING_SNAKE_CASE = rescale_factor if rescale_factor is not None else self.rescale_factor SCREAMING_SNAKE_CASE = do_normalize if do_normalize is not None else self.do_normalize SCREAMING_SNAKE_CASE = image_mean if image_mean is not None else self.image_mean SCREAMING_SNAKE_CASE = image_std if image_std is not None else self.image_std SCREAMING_SNAKE_CASE = size if size is not None else self.size SCREAMING_SNAKE_CASE = get_size_dict(a , default_to_square=a) SCREAMING_SNAKE_CASE = crop_size if crop_size is not None else self.crop_size SCREAMING_SNAKE_CASE = get_size_dict(a , param_name='crop_size') SCREAMING_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. SCREAMING_SNAKE_CASE = [to_numpy_array(a) for image in images] if do_resize: SCREAMING_SNAKE_CASE = [self.resize(image=a , size=a , crop_pct=a , resample=a) for image in images] if do_center_crop: SCREAMING_SNAKE_CASE = [self.center_crop(image=a , size=a) for image in images] if do_rescale: SCREAMING_SNAKE_CASE = [self.rescale(image=a , scale=a) for image in images] if do_normalize: SCREAMING_SNAKE_CASE = [self.normalize(image=a , mean=a , std=a) for image in images] SCREAMING_SNAKE_CASE = [to_channel_dimension_format(a , a) for image in images] SCREAMING_SNAKE_CASE = {'pixel_values': images} return BatchFeature(data=a , tensor_type=a)
137
import os import posixpath import uuid from dataclasses import dataclass from typing import TYPE_CHECKING, Iterable, List, Optional, Tuple, Union import numpy as np import pyarrow as pa import datasets from datasets.arrow_writer import ArrowWriter, ParquetWriter from datasets.config import MAX_SHARD_SIZE from datasets.filesystems import ( is_remote_filesystem, rename, ) from datasets.iterable_dataset import _BaseExamplesIterable from datasets.utils.py_utils import convert_file_size_to_int a_ : Optional[int] = datasets.utils.logging.get_logger(__name__) if TYPE_CHECKING: import pyspark @dataclass class _snake_case ( datasets.BuilderConfig ): _lowercase : Optional[datasets.Features] = None def lowerCamelCase__ (_UpperCAmelCase , _UpperCAmelCase , ): import pyspark def generate_fn(): SCREAMING_SNAKE_CASE = df.select('*' , pyspark.sql.functions.spark_partition_id().alias('part_id')) for partition_id in partition_order: SCREAMING_SNAKE_CASE = df_with_partition_id.select('*').where(F'''part_id = {partition_id}''').drop('part_id') SCREAMING_SNAKE_CASE = partition_df.collect() SCREAMING_SNAKE_CASE = 0 for row in rows: yield F'''{partition_id}_{row_id}''', row.asDict() row_id += 1 return generate_fn class _snake_case ( _BaseExamplesIterable ): def __init__( self , a , a=None , ) -> Tuple: SCREAMING_SNAKE_CASE = df SCREAMING_SNAKE_CASE = partition_order or range(self.df.rdd.getNumPartitions()) SCREAMING_SNAKE_CASE = _generate_iterable_examples(self.df , self.partition_order) def __iter__( self) -> Dict: yield from self.generate_examples_fn() def SCREAMING_SNAKE_CASE__ ( self , a) -> "SparkExamplesIterable": SCREAMING_SNAKE_CASE = list(range(self.df.rdd.getNumPartitions())) generator.shuffle(a) return SparkExamplesIterable(self.df , partition_order=a) def SCREAMING_SNAKE_CASE__ ( self , a , a) -> "SparkExamplesIterable": SCREAMING_SNAKE_CASE = self.split_shard_indices_by_worker(a , a) return SparkExamplesIterable(self.df , partition_order=a) @property def SCREAMING_SNAKE_CASE__ ( self) -> int: return len(self.partition_order) class _snake_case ( datasets.DatasetBuilder ): _lowercase : int = SparkConfig def __init__( self , a , a = None , a = None , **a , ) -> List[str]: import pyspark SCREAMING_SNAKE_CASE = pyspark.sql.SparkSession.builder.getOrCreate() SCREAMING_SNAKE_CASE = df SCREAMING_SNAKE_CASE = working_dir super().__init__( cache_dir=a , config_name=str(self.df.semanticHash()) , **a , ) def SCREAMING_SNAKE_CASE__ ( self) -> Dict: # Returns the path of the created file. def create_cache_and_write_probe(a): # makedirs with exist_ok will recursively create the directory. It will not throw an error if directories # already exist. os.makedirs(self._cache_dir , exist_ok=a) SCREAMING_SNAKE_CASE = os.path.join(self._cache_dir , 'fs_test' + uuid.uuida().hex) # Opening the file in append mode will create a new file unless it already exists, in which case it will not # change the file contents. open(a , 'a') return [probe_file] if self._spark.conf.get('spark.master' , '').startswith('local'): return # If the cluster is multi-node, make sure that the user provided a cache_dir and that it is on an NFS # accessible to the driver. # TODO: Stream batches to the driver using ArrowCollectSerializer instead of throwing an error. if self._cache_dir: SCREAMING_SNAKE_CASE = ( self._spark.sparkContext.parallelize(range(1) , 1).mapPartitions(a).collect() ) if os.path.isfile(probe[0]): return raise ValueError( 'When using Dataset.from_spark on a multi-node cluster, the driver and all workers should be able to access cache_dir') def SCREAMING_SNAKE_CASE__ ( self) -> List[str]: return datasets.DatasetInfo(features=self.config.features) def SCREAMING_SNAKE_CASE__ ( self , a) -> Dict: return [datasets.SplitGenerator(name=datasets.Split.TRAIN)] def SCREAMING_SNAKE_CASE__ ( self , a) -> Optional[int]: import pyspark def get_arrow_batch_size(a): for batch in it: yield pa.RecordBatch.from_pydict({'batch_bytes': [batch.nbytes]}) SCREAMING_SNAKE_CASE = self.df.count() SCREAMING_SNAKE_CASE = df_num_rows if df_num_rows <= 100 else 100 # Approximate the size of each row (in Arrow format) by averaging over a max-100-row sample. SCREAMING_SNAKE_CASE = ( self.df.limit(a) .repartition(1) .mapInArrow(a , 'batch_bytes: long') .agg(pyspark.sql.functions.sum('batch_bytes').alias('sample_bytes')) .collect()[0] .sample_bytes / sample_num_rows ) SCREAMING_SNAKE_CASE = approx_bytes_per_row * df_num_rows if approx_total_size > max_shard_size: # Make sure there is at least one row per partition. SCREAMING_SNAKE_CASE = min(a , int(approx_total_size / max_shard_size)) SCREAMING_SNAKE_CASE = self.df.repartition(a) def SCREAMING_SNAKE_CASE__ ( self , a , a , a , ) -> Iterable[Tuple[int, bool, Union[int, tuple]]]: import pyspark SCREAMING_SNAKE_CASE = ParquetWriter if file_format == 'parquet' else ArrowWriter SCREAMING_SNAKE_CASE = os.path.join(self._working_dir , os.path.basename(a)) if self._working_dir else fpath SCREAMING_SNAKE_CASE = file_format == 'parquet' # Define these so that we don't reference self in write_arrow, which will result in a pickling error due to # pickling the SparkContext. SCREAMING_SNAKE_CASE = self.config.features SCREAMING_SNAKE_CASE = self._writer_batch_size SCREAMING_SNAKE_CASE = self._fs.storage_options def write_arrow(a): # Within the same SparkContext, no two task attempts will share the same attempt ID. SCREAMING_SNAKE_CASE = pyspark.TaskContext().taskAttemptId() SCREAMING_SNAKE_CASE = next(a , a) if first_batch is None: # Some partitions might not receive any data. return pa.RecordBatch.from_arrays( [[task_id], [0], [0]] , names=['task_id', 'num_examples', 'num_bytes'] , ) SCREAMING_SNAKE_CASE = 0 SCREAMING_SNAKE_CASE = writer_class( features=a , path=working_fpath.replace('SSSSS' , f'''{shard_id:05d}''').replace('TTTTT' , f'''{task_id:05d}''') , writer_batch_size=a , storage_options=a , embed_local_files=a , ) SCREAMING_SNAKE_CASE = pa.Table.from_batches([first_batch]) writer.write_table(a) for batch in it: if max_shard_size is not None and writer._num_bytes >= max_shard_size: SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = writer.finalize() writer.close() yield pa.RecordBatch.from_arrays( [[task_id], [num_examples], [num_bytes]] , names=['task_id', 'num_examples', 'num_bytes'] , ) shard_id += 1 SCREAMING_SNAKE_CASE = writer_class( features=writer._features , path=working_fpath.replace('SSSSS' , f'''{shard_id:05d}''').replace('TTTTT' , f'''{task_id:05d}''') , writer_batch_size=a , storage_options=a , embed_local_files=a , ) SCREAMING_SNAKE_CASE = pa.Table.from_batches([batch]) writer.write_table(a) if writer._num_bytes > 0: SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = writer.finalize() writer.close() yield pa.RecordBatch.from_arrays( [[task_id], [num_examples], [num_bytes]] , names=['task_id', 'num_examples', 'num_bytes'] , ) if working_fpath != fpath: for file in os.listdir(os.path.dirname(a)): SCREAMING_SNAKE_CASE = os.path.join(os.path.dirname(a) , os.path.basename(a)) shutil.move(a , a) SCREAMING_SNAKE_CASE = ( self.df.mapInArrow(a , 'task_id: long, num_examples: long, num_bytes: long') .groupBy('task_id') .agg( pyspark.sql.functions.sum('num_examples').alias('total_num_examples') , pyspark.sql.functions.sum('num_bytes').alias('total_num_bytes') , pyspark.sql.functions.count('num_bytes').alias('num_shards') , pyspark.sql.functions.collect_list('num_examples').alias('shard_lengths') , ) .collect() ) for row in stats: yield row.task_id, (row.total_num_examples, row.total_num_bytes, row.num_shards, row.shard_lengths) def SCREAMING_SNAKE_CASE__ ( self , a , a = "arrow" , a = None , a = None , **a , ) -> List[Any]: self._validate_cache_dir() SCREAMING_SNAKE_CASE = convert_file_size_to_int(max_shard_size or MAX_SHARD_SIZE) self._repartition_df_if_needed(a) SCREAMING_SNAKE_CASE = not is_remote_filesystem(self._fs) SCREAMING_SNAKE_CASE = os.path.join if is_local else posixpath.join SCREAMING_SNAKE_CASE = '-TTTTT-SSSSS-of-NNNNN' SCREAMING_SNAKE_CASE = f'''{self.name}-{split_generator.name}{SUFFIX}.{file_format}''' SCREAMING_SNAKE_CASE = path_join(self._output_dir , a) SCREAMING_SNAKE_CASE = 0 SCREAMING_SNAKE_CASE = 0 SCREAMING_SNAKE_CASE = 0 SCREAMING_SNAKE_CASE = [] SCREAMING_SNAKE_CASE = [] for task_id, content in self._prepare_split_single(a , a , a): ( ( SCREAMING_SNAKE_CASE ) , ( SCREAMING_SNAKE_CASE ) , ( SCREAMING_SNAKE_CASE ) , ( SCREAMING_SNAKE_CASE ) , ) = content if num_bytes > 0: total_num_examples += num_examples total_num_bytes += num_bytes total_shards += num_shards task_id_and_num_shards.append((task_id, num_shards)) all_shard_lengths.extend(a) SCREAMING_SNAKE_CASE = total_num_examples SCREAMING_SNAKE_CASE = total_num_bytes # should rename everything at the end logger.debug(f'''Renaming {total_shards} shards.''') if total_shards > 1: SCREAMING_SNAKE_CASE = all_shard_lengths # Define fs outside of _rename_shard so that we don't reference self in the function, which will result in a # pickling error due to pickling the SparkContext. SCREAMING_SNAKE_CASE = self._fs # use the -SSSSS-of-NNNNN pattern def _rename_shard( a , a , a , ): rename( a , fpath.replace('SSSSS' , f'''{shard_id:05d}''').replace('TTTTT' , f'''{task_id:05d}''') , fpath.replace('TTTTT-SSSSS' , f'''{global_shard_id:05d}''').replace('NNNNN' , f'''{total_shards:05d}''') , ) SCREAMING_SNAKE_CASE = [] SCREAMING_SNAKE_CASE = 0 for i in range(len(a)): SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE = task_id_and_num_shards[i] for shard_id in range(a): args.append([task_id, shard_id, global_shard_id]) global_shard_id += 1 self._spark.sparkContext.parallelize(a , len(a)).map(lambda a: _rename_shard(*a)).collect() else: # don't use any pattern SCREAMING_SNAKE_CASE = 0 SCREAMING_SNAKE_CASE = task_id_and_num_shards[0][0] self._rename( fpath.replace('SSSSS' , f'''{shard_id:05d}''').replace('TTTTT' , f'''{task_id:05d}''') , fpath.replace(a , '') , ) def SCREAMING_SNAKE_CASE__ ( self , a , ) -> SparkExamplesIterable: return SparkExamplesIterable(self.df)
137
1
from ...configuration_utils import PretrainedConfig from ...utils import logging from ...utils.backbone_utils import BackboneConfigMixin, get_aligned_output_features_output_indices lowerCAmelCase_ = logging.get_logger(__name__) class lowerCamelCase ( __lowerCAmelCase , __lowerCAmelCase ): snake_case_ = '''maskformer-swin''' snake_case_ = { '''num_attention_heads''': '''num_heads''', '''num_hidden_layers''': '''num_layers''', } def __init__( self, lowercase_=224, lowercase_=4, lowercase_=3, lowercase_=96, lowercase_=[2, 2, 6, 2], lowercase_=[3, 6, 12, 24], lowercase_=7, lowercase_=4.0, lowercase_=True, lowercase_=0.0, lowercase_=0.0, lowercase_=0.1, lowercase_="gelu", lowercase_=False, lowercase_=0.02, lowercase_=1E-5, lowercase_=None, lowercase_=None, **lowercase_, ) -> Any: super().__init__(**lowercase_ ) snake_case = image_size snake_case = patch_size snake_case = num_channels snake_case = embed_dim snake_case = depths snake_case = len(lowercase_ ) snake_case = num_heads snake_case = window_size snake_case = mlp_ratio snake_case = qkv_bias snake_case = hidden_dropout_prob snake_case = attention_probs_dropout_prob snake_case = drop_path_rate snake_case = hidden_act snake_case = use_absolute_embeddings snake_case = layer_norm_eps snake_case = initializer_range # 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 snake_case = int(embed_dim * 2 ** (len(lowercase_ ) - 1) ) snake_case = ['stem'] + [F'''stage{idx}''' for idx in range(1, len(lowercase_ ) + 1 )] snake_case , snake_case = get_aligned_output_features_output_indices( out_features=lowercase_, out_indices=lowercase_, stage_names=self.stage_names )
355
'''simple docstring''' import argparse import torch from transformers import BertConfig, BertForPreTraining, load_tf_weights_in_bert from transformers.utils import logging logging.set_verbosity_info() def __magic_name__ ( A , A , A ) -> Any: # Initialise PyTorch model snake_case = BertConfig.from_json_file(A ) print(F'''Building PyTorch model from configuration: {config}''' ) snake_case = BertForPreTraining(A ) # Load weights from tf checkpoint load_tf_weights_in_bert(A , A , A ) # Save pytorch-model print(F'''Save PyTorch model to {pytorch_dump_path}''' ) torch.save(model.state_dict() , A ) if __name__ == "__main__": lowerCAmelCase_ = 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( "--bert_config_file", default=None, type=str, required=True, help=( "The config json file corresponding to the pre-trained BERT model. \n" "This specifies the model architecture." ), ) parser.add_argument( "--pytorch_dump_path", default=None, type=str, required=True, help="Path to the output PyTorch model." ) lowerCAmelCase_ = parser.parse_args() convert_tf_checkpoint_to_pytorch(args.tf_checkpoint_path, args.bert_config_file, args.pytorch_dump_path)
332
0
def SCREAMING_SNAKE_CASE__ ( lowerCamelCase__ , lowerCamelCase__ ) -> Any: __lowerCamelCase : Optional[int] = 0 while b > 0: if b & 1: res += a a += a b >>= 1 return res def SCREAMING_SNAKE_CASE__ ( lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ ) -> Tuple: __lowerCamelCase : str = 0 while b > 0: if b & 1: __lowerCamelCase : Tuple = ((res % c) + (a % c)) % c a += a b >>= 1 return res
73
'''simple docstring''' def UpperCamelCase__ ( lowerCAmelCase ): """simple docstring""" _lowerCAmelCase = len(lowerCAmelCase ) for i in range(length - 1 ): _lowerCAmelCase = i for k in range(i + 1 , lowerCAmelCase ): if collection[k] < collection[least]: _lowerCAmelCase = k if least != i: _lowerCAmelCase , _lowerCAmelCase = (collection[i], collection[least]) return collection if __name__ == "__main__": A__ : str =input('''Enter numbers separated by a comma:\n''').strip() A__ : Optional[int] =[int(item) for item in user_input.split(''',''')] print(selection_sort(unsorted))
70
0
import torch from diffusers import DPMSolverSDEScheduler from diffusers.utils import torch_device from diffusers.utils.testing_utils import require_torchsde from .test_schedulers import SchedulerCommonTest @require_torchsde class __lowercase (__SCREAMING_SNAKE_CASE ): """simple docstring""" _UpperCAmelCase = (DPMSolverSDEScheduler,) _UpperCAmelCase = 1_0 def UpperCamelCase__ ( self , **lowerCAmelCase__ ): """simple docstring""" SCREAMING_SNAKE_CASE_ : List[Any] = { 'num_train_timesteps': 1_1_0_0, 'beta_start': 0.0_001, 'beta_end': 0.02, 'beta_schedule': 'linear', 'noise_sampler_seed': 0, } config.update(**lowerCAmelCase__ ) return config def UpperCamelCase__ ( self ): """simple docstring""" for timesteps in [1_0, 5_0, 1_0_0, 1_0_0_0]: self.check_over_configs(num_train_timesteps=lowerCAmelCase__ ) def UpperCamelCase__ ( self ): """simple docstring""" for beta_start, beta_end in zip([0.00_001, 0.0_001, 0.001] , [0.0_002, 0.002, 0.02] ): self.check_over_configs(beta_start=lowerCAmelCase__ , beta_end=lowerCAmelCase__ ) def UpperCamelCase__ ( self ): """simple docstring""" for schedule in ["linear", "scaled_linear"]: self.check_over_configs(beta_schedule=lowerCAmelCase__ ) def UpperCamelCase__ ( self ): """simple docstring""" for prediction_type in ["epsilon", "v_prediction"]: self.check_over_configs(prediction_type=lowerCAmelCase__ ) def UpperCamelCase__ ( self ): """simple docstring""" SCREAMING_SNAKE_CASE_ : List[str] = self.scheduler_classes[0] SCREAMING_SNAKE_CASE_ : Optional[Any] = self.get_scheduler_config() SCREAMING_SNAKE_CASE_ : Tuple = scheduler_class(**lowerCAmelCase__ ) scheduler.set_timesteps(self.num_inference_steps ) SCREAMING_SNAKE_CASE_ : Tuple = self.dummy_model() SCREAMING_SNAKE_CASE_ : List[Any] = self.dummy_sample_deter * scheduler.init_noise_sigma SCREAMING_SNAKE_CASE_ : Any = sample.to(lowerCAmelCase__ ) for i, t in enumerate(scheduler.timesteps ): SCREAMING_SNAKE_CASE_ : List[str] = scheduler.scale_model_input(lowerCAmelCase__ , lowerCAmelCase__ ) SCREAMING_SNAKE_CASE_ : List[Any] = model(lowerCAmelCase__ , lowerCAmelCase__ ) SCREAMING_SNAKE_CASE_ : Optional[int] = scheduler.step(lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ ) SCREAMING_SNAKE_CASE_ : Tuple = output.prev_sample SCREAMING_SNAKE_CASE_ : List[Any] = torch.sum(torch.abs(lowerCAmelCase__ ) ) SCREAMING_SNAKE_CASE_ : Dict = torch.mean(torch.abs(lowerCAmelCase__ ) ) if torch_device in ["mps"]: assert abs(result_sum.item() - 167.47_821_044_921_875 ) < 1E-2 assert abs(result_mean.item() - 0.2_178_705_964_565_277 ) < 1E-3 elif torch_device in ["cuda"]: assert abs(result_sum.item() - 171.59_352_111_816_406 ) < 1E-2 assert abs(result_mean.item() - 0.22_342_906_892_299_652 ) < 1E-3 else: assert abs(result_sum.item() - 162.52_383_422_851_562 ) < 1E-2 assert abs(result_mean.item() - 0.211_619_570_851_326 ) < 1E-3 def UpperCamelCase__ ( self ): """simple docstring""" SCREAMING_SNAKE_CASE_ : Optional[int] = self.scheduler_classes[0] SCREAMING_SNAKE_CASE_ : Optional[Any] = self.get_scheduler_config(prediction_type='v_prediction' ) SCREAMING_SNAKE_CASE_ : List[str] = scheduler_class(**lowerCAmelCase__ ) scheduler.set_timesteps(self.num_inference_steps ) SCREAMING_SNAKE_CASE_ : List[Any] = self.dummy_model() SCREAMING_SNAKE_CASE_ : str = self.dummy_sample_deter * scheduler.init_noise_sigma SCREAMING_SNAKE_CASE_ : Dict = sample.to(lowerCAmelCase__ ) for i, t in enumerate(scheduler.timesteps ): SCREAMING_SNAKE_CASE_ : str = scheduler.scale_model_input(lowerCAmelCase__ , lowerCAmelCase__ ) SCREAMING_SNAKE_CASE_ : Optional[Any] = model(lowerCAmelCase__ , lowerCAmelCase__ ) SCREAMING_SNAKE_CASE_ : int = scheduler.step(lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ ) SCREAMING_SNAKE_CASE_ : int = output.prev_sample SCREAMING_SNAKE_CASE_ : List[Any] = torch.sum(torch.abs(lowerCAmelCase__ ) ) SCREAMING_SNAKE_CASE_ : List[Any] = torch.mean(torch.abs(lowerCAmelCase__ ) ) if torch_device in ["mps"]: assert abs(result_sum.item() - 124.77_149_200_439_453 ) < 1E-2 assert abs(result_mean.item() - 0.16_226_289_014_816_284 ) < 1E-3 elif torch_device in ["cuda"]: assert abs(result_sum.item() - 128.1_663_360_595_703 ) < 1E-2 assert abs(result_mean.item() - 0.16_688_326_001_167_297 ) < 1E-3 else: assert abs(result_sum.item() - 119.8_487_548_828_125 ) < 1E-2 assert abs(result_mean.item() - 0.1_560_530_662_536_621 ) < 1E-3 def UpperCamelCase__ ( self ): """simple docstring""" SCREAMING_SNAKE_CASE_ : List[str] = self.scheduler_classes[0] SCREAMING_SNAKE_CASE_ : Dict = self.get_scheduler_config() SCREAMING_SNAKE_CASE_ : Optional[Any] = scheduler_class(**lowerCAmelCase__ ) scheduler.set_timesteps(self.num_inference_steps , device=lowerCAmelCase__ ) SCREAMING_SNAKE_CASE_ : List[Any] = self.dummy_model() SCREAMING_SNAKE_CASE_ : int = self.dummy_sample_deter.to(lowerCAmelCase__ ) * scheduler.init_noise_sigma for t in scheduler.timesteps: SCREAMING_SNAKE_CASE_ : Union[str, Any] = scheduler.scale_model_input(lowerCAmelCase__ , lowerCAmelCase__ ) SCREAMING_SNAKE_CASE_ : str = model(lowerCAmelCase__ , lowerCAmelCase__ ) SCREAMING_SNAKE_CASE_ : Tuple = scheduler.step(lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ ) SCREAMING_SNAKE_CASE_ : Union[str, Any] = output.prev_sample SCREAMING_SNAKE_CASE_ : Tuple = torch.sum(torch.abs(lowerCAmelCase__ ) ) SCREAMING_SNAKE_CASE_ : str = torch.mean(torch.abs(lowerCAmelCase__ ) ) if torch_device in ["mps"]: assert abs(result_sum.item() - 167.46_957_397_460_938 ) < 1E-2 assert abs(result_mean.item() - 0.21_805_934_607_982_635 ) < 1E-3 elif torch_device in ["cuda"]: assert abs(result_sum.item() - 171.59_353_637_695_312 ) < 1E-2 assert abs(result_mean.item() - 0.22_342_908_382_415_771 ) < 1E-3 else: assert abs(result_sum.item() - 162.52_383_422_851_562 ) < 1E-2 assert abs(result_mean.item() - 0.211_619_570_851_326 ) < 1E-3 def UpperCamelCase__ ( self ): """simple docstring""" SCREAMING_SNAKE_CASE_ : Dict = self.scheduler_classes[0] SCREAMING_SNAKE_CASE_ : Any = self.get_scheduler_config() SCREAMING_SNAKE_CASE_ : Dict = scheduler_class(**lowerCAmelCase__ , use_karras_sigmas=lowerCAmelCase__ ) scheduler.set_timesteps(self.num_inference_steps , device=lowerCAmelCase__ ) SCREAMING_SNAKE_CASE_ : Dict = self.dummy_model() SCREAMING_SNAKE_CASE_ : Dict = self.dummy_sample_deter.to(lowerCAmelCase__ ) * scheduler.init_noise_sigma SCREAMING_SNAKE_CASE_ : Optional[Any] = sample.to(lowerCAmelCase__ ) for t in scheduler.timesteps: SCREAMING_SNAKE_CASE_ : List[Any] = scheduler.scale_model_input(lowerCAmelCase__ , lowerCAmelCase__ ) SCREAMING_SNAKE_CASE_ : str = model(lowerCAmelCase__ , lowerCAmelCase__ ) SCREAMING_SNAKE_CASE_ : Tuple = scheduler.step(lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ ) SCREAMING_SNAKE_CASE_ : Union[str, Any] = output.prev_sample SCREAMING_SNAKE_CASE_ : int = torch.sum(torch.abs(lowerCAmelCase__ ) ) SCREAMING_SNAKE_CASE_ : Union[str, Any] = torch.mean(torch.abs(lowerCAmelCase__ ) ) if torch_device in ["mps"]: assert abs(result_sum.item() - 176.66_974_135_742_188 ) < 1E-2 assert abs(result_mean.item() - 0.23_003_872_730_981_811 ) < 1E-2 elif torch_device in ["cuda"]: assert abs(result_sum.item() - 177.63_653_564_453_125 ) < 1E-2 assert abs(result_mean.item() - 0.23_003_872_730_981_811 ) < 1E-2 else: assert abs(result_sum.item() - 170.3_135_223_388_672 ) < 1E-2 assert abs(result_mean.item() - 0.23_003_872_730_981_811 ) < 1E-2
162
import unittest import torch from torch import nn from accelerate.test_utils import require_cuda from accelerate.utils.memory import find_executable_batch_size, release_memory def a__ ( ): raise RuntimeError('CUDA out of memory.' ) class __lowercase (nn.Module ): """simple docstring""" def __init__( self ): """simple docstring""" super().__init__() SCREAMING_SNAKE_CASE_ : int = nn.Linear(3 , 4 ) SCREAMING_SNAKE_CASE_ : Tuple = nn.BatchNormad(4 ) SCREAMING_SNAKE_CASE_ : str = nn.Linear(4 , 5 ) def UpperCamelCase__ ( self , lowerCAmelCase__ ): """simple docstring""" return self.lineara(self.batchnorm(self.lineara(lowerCAmelCase__ ) ) ) class __lowercase (unittest.TestCase ): """simple docstring""" def UpperCamelCase__ ( self ): """simple docstring""" SCREAMING_SNAKE_CASE_ : List[Any] = [] @find_executable_batch_size(starting_batch_size=1_2_8 ) def mock_training_loop_function(lowerCAmelCase__ ): nonlocal batch_sizes batch_sizes.append(lowerCAmelCase__ ) if batch_size != 8: raise_fake_out_of_memory() mock_training_loop_function() self.assertListEqual(lowerCAmelCase__ , [1_2_8, 6_4, 3_2, 1_6, 8] ) def UpperCamelCase__ ( self ): """simple docstring""" SCREAMING_SNAKE_CASE_ : Tuple = [] @find_executable_batch_size(starting_batch_size=1_2_8 ) def mock_training_loop_function(lowerCAmelCase__ , lowerCAmelCase__ ): nonlocal batch_sizes batch_sizes.append(lowerCAmelCase__ ) if batch_size != 8: raise_fake_out_of_memory() return batch_size, arga SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ : Dict = mock_training_loop_function('hello' ) self.assertListEqual(lowerCAmelCase__ , [1_2_8, 6_4, 3_2, 1_6, 8] ) self.assertListEqual([bs, arga] , [8, 'hello'] ) def UpperCamelCase__ ( self ): """simple docstring""" @find_executable_batch_size(starting_batch_size=0 ) def mock_training_loop_function(lowerCAmelCase__ ): pass with self.assertRaises(lowerCAmelCase__ ) as cm: mock_training_loop_function() self.assertIn('No executable batch size found, reached zero.' , cm.exception.args[0] ) def UpperCamelCase__ ( self ): """simple docstring""" @find_executable_batch_size(starting_batch_size=1_6 ) def mock_training_loop_function(lowerCAmelCase__ ): if batch_size > 0: raise_fake_out_of_memory() pass with self.assertRaises(lowerCAmelCase__ ) as cm: mock_training_loop_function() self.assertIn('No executable batch size found, reached zero.' , cm.exception.args[0] ) def UpperCamelCase__ ( self ): """simple docstring""" @find_executable_batch_size(starting_batch_size=1_2_8 ) def mock_training_loop_function(lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ ): if batch_size != 8: raise raise_fake_out_of_memory() with self.assertRaises(lowerCAmelCase__ ) as cm: mock_training_loop_function(1_2_8 , 'hello' , 'world' ) self.assertIn('Batch size was passed into `f`' , cm.exception.args[0] ) self.assertIn('`f(arg1=\'hello\', arg2=\'world\')' , cm.exception.args[0] ) def UpperCamelCase__ ( self ): """simple docstring""" @find_executable_batch_size(starting_batch_size=1_6 ) def mock_training_loop_function(lowerCAmelCase__ ): raise ValueError('Oops, we had an error!' ) with self.assertRaises(lowerCAmelCase__ ) as cm: mock_training_loop_function() self.assertIn('Oops, we had an error!' , cm.exception.args[0] ) @require_cuda def UpperCamelCase__ ( self ): """simple docstring""" SCREAMING_SNAKE_CASE_ : Union[str, Any] = torch.cuda.memory_allocated() SCREAMING_SNAKE_CASE_ : Optional[int] = ModelForTest() model.cuda() self.assertGreater(torch.cuda.memory_allocated() , lowerCAmelCase__ ) SCREAMING_SNAKE_CASE_ : List[Any] = release_memory(lowerCAmelCase__ ) self.assertEqual(torch.cuda.memory_allocated() , lowerCAmelCase__ )
162
1
from __future__ import annotations import queue class lowerCamelCase__: def __init__( self: Any , UpperCamelCase_: str ): __lowerCamelCase = data __lowerCamelCase = None __lowerCamelCase = None def lowerCamelCase__ ( ): '''simple docstring''' print("""\n********Press N to stop entering at any point of time********\n""" ) __lowerCamelCase = input("""Enter the value of the root node: """ ).strip().lower() __lowerCamelCase = queue.Queue() __lowerCamelCase = TreeNode(int(A__ ) ) q.put(A__ ) while not q.empty(): __lowerCamelCase = q.get() __lowerCamelCase = f'Enter the left node of {node_found.data}: ' __lowerCamelCase = input(A__ ).strip().lower() or """n""" if check == "n": return tree_node __lowerCamelCase = TreeNode(int(A__ ) ) __lowerCamelCase = left_node q.put(A__ ) __lowerCamelCase = f'Enter the right node of {node_found.data}: ' __lowerCamelCase = input(A__ ).strip().lower() or """n""" if check == "n": return tree_node __lowerCamelCase = TreeNode(int(A__ ) ) __lowerCamelCase = right_node q.put(A__ ) raise def lowerCamelCase__ ( A__ : TreeNode ): '''simple docstring''' if not isinstance(A__ , A__ ) or not node: return print(node.data , end=""",""" ) pre_order(node.left ) pre_order(node.right ) def lowerCamelCase__ ( A__ : TreeNode ): '''simple docstring''' if not isinstance(A__ , A__ ) or not node: return in_order(node.left ) print(node.data , end=""",""" ) in_order(node.right ) def lowerCamelCase__ ( A__ : TreeNode ): '''simple docstring''' if not isinstance(A__ , A__ ) or not node: return post_order(node.left ) post_order(node.right ) print(node.data , end=""",""" ) def lowerCamelCase__ ( A__ : TreeNode ): '''simple docstring''' if not isinstance(A__ , A__ ) or not node: return __lowerCamelCase = queue.Queue() q.put(A__ ) while not q.empty(): __lowerCamelCase = q.get() print(node_dequeued.data , end=""",""" ) if node_dequeued.left: q.put(node_dequeued.left ) if node_dequeued.right: q.put(node_dequeued.right ) def lowerCamelCase__ ( A__ : TreeNode ): '''simple docstring''' if not isinstance(A__ , A__ ) or not node: return __lowerCamelCase = queue.Queue() q.put(A__ ) while not q.empty(): __lowerCamelCase = [] while not q.empty(): __lowerCamelCase = q.get() print(node_dequeued.data , end=""",""" ) if node_dequeued.left: list_.append(node_dequeued.left ) if node_dequeued.right: list_.append(node_dequeued.right ) print() for node in list_: q.put(A__ ) def lowerCamelCase__ ( A__ : TreeNode ): '''simple docstring''' if not isinstance(A__ , A__ ) or not node: return __lowerCamelCase = [] __lowerCamelCase = node while n or stack: while n: # start from root node, find its left child print(n.data , end=""",""" ) stack.append(A__ ) __lowerCamelCase = n.left # end of while means current node doesn't have left child __lowerCamelCase = stack.pop() # start to traverse its right child __lowerCamelCase = n.right def lowerCamelCase__ ( A__ : TreeNode ): '''simple docstring''' if not isinstance(A__ , A__ ) or not node: return __lowerCamelCase = [] __lowerCamelCase = node while n or stack: while n: stack.append(A__ ) __lowerCamelCase = n.left __lowerCamelCase = stack.pop() print(n.data , end=""",""" ) __lowerCamelCase = n.right def lowerCamelCase__ ( A__ : TreeNode ): '''simple docstring''' if not isinstance(A__ , A__ ) or not node: return __lowerCamelCase, __lowerCamelCase = [], [] __lowerCamelCase = node stacka.append(A__ ) while stacka: # to find the reversed order of post order, store it in stack2 __lowerCamelCase = stacka.pop() if n.left: stacka.append(n.left ) if n.right: stacka.append(n.right ) stacka.append(A__ ) while stacka: # pop up from stack2 will be the post order print(stacka.pop().data , end=""",""" ) def lowerCamelCase__ ( A__ : str = "" , A__ : Tuple=50 , A__ : Optional[int]="*" ): '''simple docstring''' if not s: return "\n" + width * char __lowerCamelCase, __lowerCamelCase = divmod(width - len(A__ ) - 2 , 2 ) return f'{left * char} {s} {(left + extra) * char}' if __name__ == "__main__": import doctest doctest.testmod() print(prompt('Binary Tree Traversals')) UpperCAmelCase_ = build_tree() print(prompt('Pre Order Traversal')) pre_order(node) print(prompt() + '\n') print(prompt('In Order Traversal')) in_order(node) print(prompt() + '\n') print(prompt('Post Order Traversal')) post_order(node) print(prompt() + '\n') print(prompt('Level Order Traversal')) level_order(node) print(prompt() + '\n') print(prompt('Actual Level Order Traversal')) level_order_actual(node) print('*' * 50 + '\n') print(prompt('Pre Order Traversal - Iteration Version')) pre_order_iter(node) print(prompt() + '\n') print(prompt('In Order Traversal - Iteration Version')) in_order_iter(node) print(prompt() + '\n') print(prompt('Post Order Traversal - Iteration Version')) post_order_iter(node) print(prompt())
12
import unittest from transformers import GPTNeoXJapaneseConfig, is_torch_available from transformers.models.gpt_neox_japanese.tokenization_gpt_neox_japanese import GPTNeoXJapaneseTokenizer from transformers.testing_utils import require_torch, slow, torch_device from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, ids_tensor, random_attention_mask from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from transformers import GPTNeoXJapaneseForCausalLM, GPTNeoXJapaneseModel class UpperCamelCase_ : '''simple docstring''' def __init__( self , a , a=13 , a=7 , a=True , a=True , a=True , a=True , a=99 , a=32 , a=5 , a=4 , a=4 , a="gelu" , a=0.0 , a=0.1 , a=True , a=5_12 , a=16 , a=2 , a=0.02 , a=3 , a=4 , a=None , ) -> Optional[Any]: snake_case_ = parent snake_case_ = batch_size snake_case_ = seq_length snake_case_ = is_training snake_case_ = use_input_mask snake_case_ = use_token_type_ids snake_case_ = use_labels snake_case_ = vocab_size snake_case_ = hidden_size snake_case_ = num_hidden_layers snake_case_ = num_attention_heads snake_case_ = intermediate_multiple_size snake_case_ = hidden_act snake_case_ = hidden_dropout snake_case_ = attention_dropout snake_case_ = weight_tying snake_case_ = max_position_embeddings snake_case_ = type_vocab_size snake_case_ = type_sequence_label_size snake_case_ = initializer_range snake_case_ = num_labels snake_case_ = num_choices snake_case_ = scope def _UpperCamelCase ( self ) -> List[Any]: snake_case_ = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) snake_case_ = None if self.use_input_mask: snake_case_ = random_attention_mask([self.batch_size, self.seq_length] ) snake_case_ = None if self.use_labels: snake_case_ = ids_tensor([self.batch_size, self.seq_length] , self.num_labels ) snake_case_ = self.get_config() return config, input_ids, input_mask, token_labels def _UpperCamelCase ( self ) -> Dict: return GPTNeoXJapaneseConfig( 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_multiple_size=self.intermediate_multiple_size , hidden_act=self.hidden_act , hidden_dropout=self.hidden_dropout , attention_dropout=self.attention_dropout , weight_tying=self.weight_tying , max_position_embeddings=self.max_position_embeddings , type_vocab_size=self.type_vocab_size , is_decoder=a , initializer_range=self.initializer_range , ) def _UpperCamelCase ( self ) -> int: snake_case_ , snake_case_ , snake_case_ , snake_case_ = self.prepare_config_and_inputs() snake_case_ = True return config, input_ids, input_mask, token_labels def _UpperCamelCase ( self , a , a , a ) -> Any: snake_case_ = GPTNeoXJapaneseModel(config=a ) model.to(a ) model.eval() snake_case_ = model(a , attention_mask=a ) snake_case_ = model(a ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def _UpperCamelCase ( self , a , a , a ) -> Union[str, Any]: snake_case_ = True snake_case_ = GPTNeoXJapaneseModel(a ) model.to(a ) model.eval() snake_case_ = model(a , attention_mask=a ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def _UpperCamelCase ( self , a , a , a , a ) -> int: snake_case_ = GPTNeoXJapaneseForCausalLM(config=a ) model.to(a ) model.eval() snake_case_ = model(a , attention_mask=a , labels=a ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.vocab_size) ) def _UpperCamelCase ( self , a , a , a ) -> Tuple: snake_case_ = True snake_case_ = GPTNeoXJapaneseForCausalLM(config=a ) model.to(a ) model.eval() # first forward pass snake_case_ = model(a , attention_mask=a , use_cache=a ) snake_case_ = outputs.past_key_values # create hypothetical multiple next token and extent to next_input_ids snake_case_ = ids_tensor((self.batch_size, 3) , config.vocab_size ) snake_case_ = ids_tensor((self.batch_size, 3) , vocab_size=2 ) # append to next input_ids and snake_case_ = torch.cat([input_ids, next_tokens] , dim=-1 ) snake_case_ = torch.cat([input_mask, next_mask] , dim=-1 ) snake_case_ = model(a , attention_mask=a , output_hidden_states=a ) snake_case_ = output_from_no_past['hidden_states'][0] snake_case_ = model( a , attention_mask=a , past_key_values=a , output_hidden_states=a , )['hidden_states'][0] # select random slice snake_case_ = ids_tensor((1,) , output_from_past.shape[-1] ).item() snake_case_ = output_from_no_past[:, -3:, random_slice_idx].detach() snake_case_ = output_from_past[:, :, random_slice_idx].detach() self.parent.assertTrue(output_from_past_slice.shape[1] == next_tokens.shape[1] ) # test that outputs are equal for slice self.parent.assertTrue(torch.allclose(a , a , atol=1E-3 ) ) def _UpperCamelCase ( self ) -> Dict: snake_case_ = self.prepare_config_and_inputs() snake_case_ , snake_case_ , snake_case_ , snake_case_ = config_and_inputs snake_case_ = {'input_ids': input_ids, 'attention_mask': input_mask} return config, inputs_dict @require_torch class UpperCamelCase_ ( snake_case_ , snake_case_ , unittest.TestCase ): '''simple docstring''' lowerCAmelCase = (GPTNeoXJapaneseModel, GPTNeoXJapaneseForCausalLM) if is_torch_available() else () lowerCAmelCase = (GPTNeoXJapaneseForCausalLM,) if is_torch_available() else () lowerCAmelCase = ( {'''feature-extraction''': GPTNeoXJapaneseModel, '''text-generation''': GPTNeoXJapaneseForCausalLM} if is_torch_available() else {} ) lowerCAmelCase = False lowerCAmelCase = False lowerCAmelCase = False lowerCAmelCase = False def _UpperCamelCase ( self ) -> List[Any]: snake_case_ = GPTNeoXJapaneseModelTester(self ) snake_case_ = ConfigTester(self , config_class=a , hidden_size=37 ) def _UpperCamelCase ( self ) -> str: self.config_tester.run_common_tests() def _UpperCamelCase ( self ) -> Optional[Any]: snake_case_ , snake_case_ , snake_case_ , snake_case_ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(a , a , a ) def _UpperCamelCase ( self ) -> Union[str, Any]: snake_case_ , snake_case_ , snake_case_ , snake_case_ = self.model_tester.prepare_config_and_inputs_for_decoder() self.model_tester.create_and_check_model_as_decoder(a , a , a ) def _UpperCamelCase ( self ) -> Optional[int]: # This regression test was failing with PyTorch < 1.3 snake_case_ , snake_case_ , snake_case_ , snake_case_ = self.model_tester.prepare_config_and_inputs_for_decoder() snake_case_ = None self.model_tester.create_and_check_model_as_decoder(a , a , a ) def _UpperCamelCase ( self ) -> Dict: snake_case_ , snake_case_ , snake_case_ , snake_case_ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_decoder_model_past_large_inputs(a , a , a ) def _UpperCamelCase ( self ) -> List[Any]: snake_case_ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_causal_lm(*a ) @slow def _UpperCamelCase ( self ) -> Any: snake_case_ = 'abeja/gpt-neox-japanese-2.7b' snake_case_ = ['データサイエンティストとは、', '100年後に必要とされる会社は、', 'フルリモートの環境で働くために必要なことは、', '国境の長いトンネルを抜けると', '美味しい日本食といえば、'] snake_case_ = [ 'データサイエンティストとは、データを分析し、ビジネスに役立つ知見を導き出す専門家のことです。', '100年後に必要とされる会社は、「人」が中心の会社です。', 'フルリモートの環境で働くために必要なことは、「自分の時間をコントロールする」ことです。', '国境の長いトンネルを抜けると、そこは雪国だった。', '美味しい日本食といえば、やっぱりお寿司ですよね。', ] snake_case_ = GPTNeoXJapaneseTokenizer.from_pretrained(a ) snake_case_ = GPTNeoXJapaneseForCausalLM.from_pretrained(a ) snake_case_ = [] for prompt in prompts: snake_case_ = tokenizer(a , return_tensors='pt' ).input_ids snake_case_ = model.generate(a , max_length=50 ) snake_case_ = tokenizer.batch_decode(a , skip_special_tokens=a ) predicted_outputs += generated_string self.assertListEqual(a , a )
178
0
import warnings from contextlib import contextmanager from ...processing_utils import ProcessorMixin from .feature_extraction_wavaveca import WavaVecaFeatureExtractor from .tokenization_wavaveca import WavaVecaCTCTokenizer class __snake_case ( lowerCAmelCase ): _a : int= "Wav2Vec2FeatureExtractor" _a : Tuple= "AutoTokenizer" def __init__( self ,snake_case ,snake_case ): '''simple docstring''' super().__init__(snake_case ,snake_case ) lowercase : int = self.feature_extractor lowercase : Union[str, Any] = False @classmethod def _SCREAMING_SNAKE_CASE ( cls ,snake_case ,**snake_case ): '''simple docstring''' try: return super().from_pretrained(snake_case ,**snake_case ) except OSError: warnings.warn( f"Loading a tokenizer inside {cls.__name__} from a config that does not" """ include a `tokenizer_class` attribute is deprecated and will be """ """removed in v5. Please add `'tokenizer_class': 'Wav2Vec2CTCTokenizer'`""" """ attribute to either your `config.json` or `tokenizer_config.json` """ """file to suppress this warning: """ ,snake_case ,) lowercase : Optional[Any] = WavaVecaFeatureExtractor.from_pretrained(snake_case ,**snake_case ) lowercase : Any = WavaVecaCTCTokenizer.from_pretrained(snake_case ,**snake_case ) return cls(feature_extractor=snake_case ,tokenizer=snake_case ) def __call__( self ,*snake_case ,**snake_case ): '''simple docstring''' if self._in_target_context_manager: return self.current_processor(*snake_case ,**snake_case ) if "raw_speech" in kwargs: warnings.warn("""Using `raw_speech` as a keyword argument is deprecated. Use `audio` instead.""" ) lowercase : Union[str, Any] = kwargs.pop("""raw_speech""" ) else: lowercase : Optional[Any] = kwargs.pop("""audio""" ,snake_case ) lowercase : int = kwargs.pop("""sampling_rate""" ,snake_case ) lowercase : List[str] = kwargs.pop("""text""" ,snake_case ) if len(snake_case ) > 0: lowercase : Dict = args[0] lowercase : Dict = args[1:] if audio is None and text is None: raise ValueError("""You need to specify either an `audio` or `text` input to process.""" ) if audio is not None: lowercase : Optional[int] = self.feature_extractor(snake_case ,*snake_case ,sampling_rate=snake_case ,**snake_case ) if text is not None: lowercase : int = self.tokenizer(snake_case ,**snake_case ) if text is None: return inputs elif audio is None: return encodings else: lowercase : Dict = encodings["""input_ids"""] return inputs def _SCREAMING_SNAKE_CASE ( self ,*snake_case ,**snake_case ): '''simple docstring''' if self._in_target_context_manager: return self.current_processor.pad(*snake_case ,**snake_case ) lowercase : List[str] = kwargs.pop("""input_features""" ,snake_case ) lowercase : List[Any] = kwargs.pop("""labels""" ,snake_case ) if len(snake_case ) > 0: lowercase : int = args[0] lowercase : int = args[1:] if input_features is not None: lowercase : Any = self.feature_extractor.pad(snake_case ,*snake_case ,**snake_case ) if labels is not None: lowercase : Optional[Any] = self.tokenizer.pad(snake_case ,**snake_case ) if labels is None: return input_features elif input_features is None: return labels else: lowercase : Dict = labels["""input_ids"""] return input_features def _SCREAMING_SNAKE_CASE ( self ,*snake_case ,**snake_case ): '''simple docstring''' return self.tokenizer.batch_decode(*snake_case ,**snake_case ) def _SCREAMING_SNAKE_CASE ( self ,*snake_case ,**snake_case ): '''simple docstring''' return self.tokenizer.decode(*snake_case ,**snake_case ) @contextmanager def _SCREAMING_SNAKE_CASE ( self ): '''simple docstring''' warnings.warn( """`as_target_processor` is deprecated and will be removed in v5 of Transformers. You can process your """ """labels by using the argument `text` of the regular `__call__` method (either in the same call as """ """your audio inputs, or in a separate call.""" ) lowercase : str = True lowercase : List[str] = self.tokenizer yield lowercase : Union[str, Any] = self.feature_extractor lowercase : Any = False
285
import math from datetime import datetime, timedelta def _snake_case( SCREAMING_SNAKE_CASE__ ) -> datetime: lowercase : Any = year % 19 lowercase : Optional[int] = year % 4 lowercase : Any = year % 7 lowercase : str = math.floor(year / 100 ) lowercase : List[str] = math.floor((13 + 8 * leap_day_inhibits) / 25 ) lowercase : Tuple = leap_day_inhibits / 4 lowercase : Any = ( 15 - lunar_orbit_correction + leap_day_inhibits - leap_day_reinstall_number ) % 30 lowercase : Union[str, Any] = (4 + leap_day_inhibits - leap_day_reinstall_number) % 7 # days to be added to March 21 lowercase : str = (19 * metonic_cycle + secular_moon_shift) % 30 # PHM -> Paschal Full Moon lowercase : List[Any] = ( 2 * julian_leap_year + 4 * non_leap_year + 6 * days_to_add + century_starting_point ) % 7 if days_to_add == 29 and days_from_phm_to_sunday == 6: return datetime(SCREAMING_SNAKE_CASE__ , 4 , 19 ) elif days_to_add == 28 and days_from_phm_to_sunday == 6: return datetime(SCREAMING_SNAKE_CASE__ , 4 , 18 ) else: return datetime(SCREAMING_SNAKE_CASE__ , 3 , 22 ) + timedelta( days=int(days_to_add + days_from_phm_to_sunday ) ) if __name__ == "__main__": for year in (1994, 2000, 2010, 2021, 2023): lowercase : List[str] = """will be""" if year > datetime.now().year else """was""" print(F'''Easter in {year} {tense} {gauss_easter(year)}''')
285
1
"""simple docstring""" from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_tokenizers_available, is_torch_available, is_vision_available, ) _snake_case = { 'configuration_layoutlmv2': ['LAYOUTLMV2_PRETRAINED_CONFIG_ARCHIVE_MAP', 'LayoutLMv2Config'], 'processing_layoutlmv2': ['LayoutLMv2Processor'], 'tokenization_layoutlmv2': ['LayoutLMv2Tokenizer'], } try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _snake_case = ['LayoutLMv2TokenizerFast'] try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _snake_case = ['LayoutLMv2FeatureExtractor'] _snake_case = ['LayoutLMv2ImageProcessor'] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _snake_case = [ 'LAYOUTLMV2_PRETRAINED_MODEL_ARCHIVE_LIST', 'LayoutLMv2ForQuestionAnswering', 'LayoutLMv2ForSequenceClassification', 'LayoutLMv2ForTokenClassification', 'LayoutLMv2Layer', 'LayoutLMv2Model', 'LayoutLMv2PreTrainedModel', ] if TYPE_CHECKING: from .configuration_layoutlmva import LAYOUTLMV2_PRETRAINED_CONFIG_ARCHIVE_MAP, LayoutLMvaConfig from .processing_layoutlmva import LayoutLMvaProcessor from .tokenization_layoutlmva import LayoutLMvaTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_layoutlmva_fast import LayoutLMvaTokenizerFast try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .feature_extraction_layoutlmva import LayoutLMvaFeatureExtractor, LayoutLMvaImageProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_layoutlmva import ( LAYOUTLMV2_PRETRAINED_MODEL_ARCHIVE_LIST, LayoutLMvaForQuestionAnswering, LayoutLMvaForSequenceClassification, LayoutLMvaForTokenClassification, LayoutLMvaLayer, LayoutLMvaModel, LayoutLMvaPreTrainedModel, ) else: import sys _snake_case = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
294
"""simple docstring""" _snake_case = '\n# Transformers installation\n! pip install transformers datasets\n# To install from source instead of the last release, comment the command above and uncomment the following one.\n# ! pip install git+https://github.com/huggingface/transformers.git\n' _snake_case = [{'type': 'code', 'content': INSTALL_CONTENT}] _snake_case = { '{processor_class}': 'FakeProcessorClass', '{model_class}': 'FakeModelClass', '{object_class}': 'FakeObjectClass', }
294
1
'''simple docstring''' from json import JSONDecodeError # Workaround for requests.exceptions.JSONDecodeError import requests def UpperCAmelCase ( lowerCamelCase_ :str = "isbn/0140328726" ): '''simple docstring''' snake_case_ : Tuple = olid.strip().strip("""/""" ) # Remove leading/trailing whitespace & slashes if new_olid.count("""/""" ) != 1: snake_case_ : int = F'''{olid} is not a valid Open Library olid''' raise ValueError(lowerCamelCase_ ) return requests.get(F'''https://openlibrary.org/{new_olid}.json''' ).json() def UpperCAmelCase ( lowerCamelCase_ :dict ): '''simple docstring''' snake_case_ : List[str] = { """title""": """Title""", """publish_date""": """Publish date""", """authors""": """Authors""", """number_of_pages""": """Number of pages:""", """first_sentence""": """First sentence""", """isbn_10""": """ISBN (10)""", """isbn_13""": """ISBN (13)""", } snake_case_ : Optional[int] = {better_key: ol_book_data[key] for key, better_key in desired_keys.items()} snake_case_ : List[Any] = [ get_openlibrary_data(author["""key"""] )["""name"""] for author in data["""Authors"""] ] snake_case_ : Optional[int] = data["""First sentence"""]["""value"""] for key, value in data.items(): if isinstance(lowerCamelCase_ , lowerCamelCase_ ): snake_case_ : str = """, """.join(lowerCamelCase_ ) return data if __name__ == "__main__": import doctest doctest.testmod() while True: __A : Any = input('\nEnter the ISBN code to search (or \'quit\' to stop): ').strip() if isbn.lower() in ("", "q", "quit", "exit", "stop"): break if len(isbn) not in (10, 13) or not isbn.isdigit(): print(F'Sorry, {isbn} is not a valid ISBN. Please, input a valid ISBN.') continue print(F'\nSearching Open Library for ISBN: {isbn}...\n') try: __A : Any = summarize_book(get_openlibrary_data(F'isbn/{isbn}')) print('\n'.join(F'{key}: {value}' for key, value in book_summary.items())) except JSONDecodeError: # Workaround for requests.exceptions.RequestException: print(F'Sorry, there are no results for ISBN: {isbn}.')
8
'''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 __UpperCamelCase ( nn.Module ): def __init__( self :Any ,_UpperCamelCase :int ,_UpperCamelCase :int ,_UpperCamelCase :int ,_UpperCamelCase :int=0.0 ,_UpperCamelCase :Optional[int] = None ,_UpperCamelCase :str = "geglu" ,_UpperCamelCase :Optional[int] = None ,_UpperCamelCase :bool = False ,_UpperCamelCase :bool = False ,_UpperCamelCase :bool = False ,_UpperCamelCase :bool = False ,_UpperCamelCase :bool = True ,_UpperCamelCase :str = "layer_norm" ,_UpperCamelCase :bool = False ,): super().__init__() snake_case_ : Any = only_cross_attention snake_case_ : Union[str, Any] = (num_embeds_ada_norm is not None) and norm_type == """ada_norm_zero""" snake_case_ : Any = (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: snake_case_ : Dict = AdaLayerNorm(_UpperCamelCase ,_UpperCamelCase ) elif self.use_ada_layer_norm_zero: snake_case_ : str = AdaLayerNormZero(_UpperCamelCase ,_UpperCamelCase ) else: snake_case_ : List[Any] = nn.LayerNorm(_UpperCamelCase ,elementwise_affine=_UpperCamelCase ) snake_case_ : List[str] = Attention( query_dim=_UpperCamelCase ,heads=_UpperCamelCase ,dim_head=_UpperCamelCase ,dropout=_UpperCamelCase ,bias=_UpperCamelCase ,cross_attention_dim=cross_attention_dim if only_cross_attention else None ,upcast_attention=_UpperCamelCase ,) # 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. snake_case_ : str = ( AdaLayerNorm(_UpperCamelCase ,_UpperCamelCase ) if self.use_ada_layer_norm else nn.LayerNorm(_UpperCamelCase ,elementwise_affine=_UpperCamelCase ) ) snake_case_ : List[str] = Attention( query_dim=_UpperCamelCase ,cross_attention_dim=cross_attention_dim if not double_self_attention else None ,heads=_UpperCamelCase ,dim_head=_UpperCamelCase ,dropout=_UpperCamelCase ,bias=_UpperCamelCase ,upcast_attention=_UpperCamelCase ,) # is self-attn if encoder_hidden_states is none else: snake_case_ : Any = None snake_case_ : Optional[Any] = None # 3. Feed-forward snake_case_ : List[str] = nn.LayerNorm(_UpperCamelCase ,elementwise_affine=_UpperCamelCase ) snake_case_ : Union[str, Any] = FeedForward(_UpperCamelCase ,dropout=_UpperCamelCase ,activation_fn=_UpperCamelCase ,final_dropout=_UpperCamelCase ) # let chunk size default to None snake_case_ : Optional[int] = None snake_case_ : Dict = 0 def a__ ( self :List[Any] ,_UpperCamelCase :Optional[int] ,_UpperCamelCase :int ): # Sets chunk feed-forward snake_case_ : Optional[Any] = chunk_size snake_case_ : Optional[Any] = dim def a__ ( self :List[str] ,_UpperCamelCase :torch.FloatTensor ,_UpperCamelCase :Optional[torch.FloatTensor] = None ,_UpperCamelCase :Optional[torch.FloatTensor] = None ,_UpperCamelCase :Optional[torch.FloatTensor] = None ,_UpperCamelCase :Optional[torch.LongTensor] = None ,_UpperCamelCase :Dict[str, Any] = None ,_UpperCamelCase :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: snake_case_ : Optional[Any] = self.norma(_UpperCamelCase ,_UpperCamelCase ) elif self.use_ada_layer_norm_zero: snake_case_ , snake_case_ , snake_case_ , snake_case_ , snake_case_ : Union[str, Any] = self.norma( _UpperCamelCase ,_UpperCamelCase ,_UpperCamelCase ,hidden_dtype=hidden_states.dtype ) else: snake_case_ : Optional[int] = self.norma(_UpperCamelCase ) snake_case_ : int = cross_attention_kwargs if cross_attention_kwargs is not None else {} snake_case_ : Union[str, Any] = self.attna( _UpperCamelCase ,encoder_hidden_states=encoder_hidden_states if self.only_cross_attention else None ,attention_mask=_UpperCamelCase ,**_UpperCamelCase ,) if self.use_ada_layer_norm_zero: snake_case_ : Union[str, Any] = gate_msa.unsqueeze(1 ) * attn_output snake_case_ : Union[str, Any] = attn_output + hidden_states # 2. Cross-Attention if self.attna is not None: snake_case_ : Any = ( self.norma(_UpperCamelCase ,_UpperCamelCase ) if self.use_ada_layer_norm else self.norma(_UpperCamelCase ) ) snake_case_ : List[Any] = self.attna( _UpperCamelCase ,encoder_hidden_states=_UpperCamelCase ,attention_mask=_UpperCamelCase ,**_UpperCamelCase ,) snake_case_ : Tuple = attn_output + hidden_states # 3. Feed-forward snake_case_ : Optional[Any] = self.norma(_UpperCamelCase ) if self.use_ada_layer_norm_zero: snake_case_ : Dict = 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`.''' ) snake_case_ : Union[str, Any] = norm_hidden_states.shape[self._chunk_dim] // self._chunk_size snake_case_ : int = torch.cat( [self.ff(_UpperCamelCase ) for hid_slice in norm_hidden_states.chunk(_UpperCamelCase ,dim=self._chunk_dim )] ,dim=self._chunk_dim ,) else: snake_case_ : List[str] = self.ff(_UpperCamelCase ) if self.use_ada_layer_norm_zero: snake_case_ : Union[str, Any] = gate_mlp.unsqueeze(1 ) * ff_output snake_case_ : Any = ff_output + hidden_states return hidden_states class __UpperCamelCase ( nn.Module ): def __init__( self :Dict ,_UpperCamelCase :int ,_UpperCamelCase :Optional[int] = None ,_UpperCamelCase :int = 4 ,_UpperCamelCase :float = 0.0 ,_UpperCamelCase :str = "geglu" ,_UpperCamelCase :bool = False ,): super().__init__() snake_case_ : Tuple = int(dim * mult ) snake_case_ : Optional[int] = dim_out if dim_out is not None else dim if activation_fn == "gelu": snake_case_ : Any = GELU(_UpperCamelCase ,_UpperCamelCase ) if activation_fn == "gelu-approximate": snake_case_ : Tuple = GELU(_UpperCamelCase ,_UpperCamelCase ,approximate="""tanh""" ) elif activation_fn == "geglu": snake_case_ : Dict = GEGLU(_UpperCamelCase ,_UpperCamelCase ) elif activation_fn == "geglu-approximate": snake_case_ : Optional[Any] = ApproximateGELU(_UpperCamelCase ,_UpperCamelCase ) snake_case_ : Dict = nn.ModuleList([] ) # project in self.net.append(_UpperCamelCase ) # project dropout self.net.append(nn.Dropout(_UpperCamelCase ) ) # project out self.net.append(nn.Linear(_UpperCamelCase ,_UpperCamelCase ) ) # FF as used in Vision Transformer, MLP-Mixer, etc. have a final dropout if final_dropout: self.net.append(nn.Dropout(_UpperCamelCase ) ) def a__ ( self :Tuple ,_UpperCamelCase :Union[str, Any] ): for module in self.net: snake_case_ : Tuple = module(_UpperCamelCase ) return hidden_states class __UpperCamelCase ( nn.Module ): def __init__( self :Optional[Any] ,_UpperCamelCase :int ,_UpperCamelCase :int ,_UpperCamelCase :str = "none" ): super().__init__() snake_case_ : Union[str, Any] = nn.Linear(_UpperCamelCase ,_UpperCamelCase ) snake_case_ : Optional[Any] = approximate def a__ ( self :str ,_UpperCamelCase :int ): if gate.device.type != "mps": return F.gelu(_UpperCamelCase ,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 a__ ( self :Optional[int] ,_UpperCamelCase :Optional[Any] ): snake_case_ : Optional[Any] = self.proj(_UpperCamelCase ) snake_case_ : int = self.gelu(_UpperCamelCase ) return hidden_states class __UpperCamelCase ( nn.Module ): def __init__( self :List[Any] ,_UpperCamelCase :int ,_UpperCamelCase :int ): super().__init__() snake_case_ : str = nn.Linear(_UpperCamelCase ,dim_out * 2 ) def a__ ( self :Dict ,_UpperCamelCase :List[str] ): if gate.device.type != "mps": return F.gelu(_UpperCamelCase ) # mps: gelu is not implemented for float16 return F.gelu(gate.to(dtype=torch.floataa ) ).to(dtype=gate.dtype ) def a__ ( self :Optional[Any] ,_UpperCamelCase :Optional[int] ): snake_case_ , snake_case_ : Dict = self.proj(_UpperCamelCase ).chunk(2 ,dim=-1 ) return hidden_states * self.gelu(_UpperCamelCase ) class __UpperCamelCase ( nn.Module ): def __init__( self :List[str] ,_UpperCamelCase :int ,_UpperCamelCase :int ): super().__init__() snake_case_ : int = nn.Linear(_UpperCamelCase ,_UpperCamelCase ) def a__ ( self :Optional[int] ,_UpperCamelCase :Optional[int] ): snake_case_ : int = self.proj(_UpperCamelCase ) return x * torch.sigmoid(1.7_02 * x ) class __UpperCamelCase ( nn.Module ): def __init__( self :int ,_UpperCamelCase :str ,_UpperCamelCase :List[Any] ): super().__init__() snake_case_ : int = nn.Embedding(_UpperCamelCase ,_UpperCamelCase ) snake_case_ : Union[str, Any] = nn.SiLU() snake_case_ : Any = nn.Linear(_UpperCamelCase ,embedding_dim * 2 ) snake_case_ : Dict = nn.LayerNorm(_UpperCamelCase ,elementwise_affine=_UpperCamelCase ) def a__ ( self :int ,_UpperCamelCase :List[str] ,_UpperCamelCase :int ): snake_case_ : Union[str, Any] = self.linear(self.silu(self.emb(_UpperCamelCase ) ) ) snake_case_ , snake_case_ : Tuple = torch.chunk(_UpperCamelCase ,2 ) snake_case_ : Tuple = self.norm(_UpperCamelCase ) * (1 + scale) + shift return x class __UpperCamelCase ( nn.Module ): def __init__( self :List[str] ,_UpperCamelCase :Tuple ,_UpperCamelCase :int ): super().__init__() snake_case_ : int = CombinedTimestepLabelEmbeddings(_UpperCamelCase ,_UpperCamelCase ) snake_case_ : int = nn.SiLU() snake_case_ : List[str] = nn.Linear(_UpperCamelCase ,6 * embedding_dim ,bias=_UpperCamelCase ) snake_case_ : str = nn.LayerNorm(_UpperCamelCase ,elementwise_affine=_UpperCamelCase ,eps=1E-6 ) def a__ ( self :Union[str, Any] ,_UpperCamelCase :Any ,_UpperCamelCase :Tuple ,_UpperCamelCase :Optional[Any] ,_UpperCamelCase :str=None ): snake_case_ : Union[str, Any] = self.linear(self.silu(self.emb(_UpperCamelCase ,_UpperCamelCase ,hidden_dtype=_UpperCamelCase ) ) ) snake_case_ , snake_case_ , snake_case_ , snake_case_ , snake_case_ , snake_case_ : Any = emb.chunk(6 ,dim=1 ) snake_case_ : str = self.norm(_UpperCamelCase ) * (1 + scale_msa[:, None]) + shift_msa[:, None] return x, gate_msa, shift_mlp, scale_mlp, gate_mlp class __UpperCamelCase ( nn.Module ): def __init__( self :Optional[int] ,_UpperCamelCase :int ,_UpperCamelCase :int ,_UpperCamelCase :int ,_UpperCamelCase :Optional[str] = None ,_UpperCamelCase :float = 1E-5 ): super().__init__() snake_case_ : Optional[int] = num_groups snake_case_ : List[Any] = eps if act_fn is None: snake_case_ : int = None else: snake_case_ : Dict = get_activation(_UpperCamelCase ) snake_case_ : Optional[int] = nn.Linear(_UpperCamelCase ,out_dim * 2 ) def a__ ( self :List[Any] ,_UpperCamelCase :Optional[Any] ,_UpperCamelCase :List[str] ): if self.act: snake_case_ : Any = self.act(_UpperCamelCase ) snake_case_ : Optional[int] = self.linear(_UpperCamelCase ) snake_case_ : Dict = emb[:, :, None, None] snake_case_ , snake_case_ : str = emb.chunk(2 ,dim=1 ) snake_case_ : str = F.group_norm(_UpperCamelCase ,self.num_groups ,eps=self.eps ) snake_case_ : List[str] = x * (1 + scale) + shift return x
8
1
"""simple docstring""" from typing import Optional, Union import torch from torch import nn from ...configuration_utils import ConfigMixin, register_to_config from ...models.modeling_utils import ModelMixin class SCREAMING_SNAKE_CASE__ ( _lowerCAmelCase , _lowerCAmelCase ): @register_to_config def __init__( self : Tuple , lowerCAmelCase : int = 768 , ): super().__init__() lowerCAmelCase = nn.Parameter(torch.zeros(1 , _lowercase ) ) lowerCAmelCase = nn.Parameter(torch.ones(1 , _lowercase ) ) def __lowercase ( self : Any , lowerCAmelCase : Optional[Union[str, torch.device]] = None , lowerCAmelCase : Optional[torch.dtype] = None , ): lowerCAmelCase = nn.Parameter(self.mean.to(_lowercase ).to(_lowercase ) ) lowerCAmelCase = nn.Parameter(self.std.to(_lowercase ).to(_lowercase ) ) return self def __lowercase ( self : Optional[int] , lowerCAmelCase : List[Any] ): lowerCAmelCase = (embeds - self.mean) * 1.0 / self.std return embeds def __lowercase ( self : List[str] , lowerCAmelCase : Optional[int] ): lowerCAmelCase = (embeds * self.std) + self.mean return embeds
155
"""simple docstring""" from typing import Dict from .base import GenericTensor, Pipeline class _UpperCAmelCase ( _lowerCAmelCase ): def a ( self : Tuple , _lowercase : Dict=None , _lowercase : str=None , _lowercase : Union[str, Any]=None , **_lowercase : Tuple ): if tokenize_kwargs is None: __UpperCAmelCase = {} if truncation is not None: if "truncation" in tokenize_kwargs: raise ValueError( '''truncation parameter defined twice (given as keyword argument as well as in tokenize_kwargs)''' ) __UpperCAmelCase = truncation __UpperCAmelCase = tokenize_kwargs __UpperCAmelCase = {} if return_tensors is not None: __UpperCAmelCase = return_tensors return preprocess_params, {}, postprocess_params def a ( self : int , _lowercase : Optional[Any] , **_lowercase : Union[str, Any] ): __UpperCAmelCase = self.framework __UpperCAmelCase = self.tokenizer(_lowercase , return_tensors=_lowercase , **_lowercase ) return model_inputs def a ( self : List[str] , _lowercase : Tuple ): __UpperCAmelCase = self.model(**_lowercase ) return model_outputs def a ( self : int , _lowercase : Tuple , _lowercase : str=False ): # [0] is the first available tensor, logits or last_hidden_state. if return_tensors: return model_outputs[0] if self.framework == "pt": return model_outputs[0].tolist() elif self.framework == "tf": return model_outputs[0].numpy().tolist() def __call__( self : List[Any] , *_lowercase : Optional[Any] , **_lowercase : Union[str, Any] ): return super().__call__(*_lowercase , **_lowercase )
332
0
'''simple docstring''' import argparse import json import os import fairseq import torch from fairseq.data import Dictionary from transformers import ( UniSpeechConfig, UniSpeechForCTC, UniSpeechForPreTraining, WavaVecaFeatureExtractor, WavaVecaPhonemeCTCTokenizer, WavaVecaProcessor, logging, ) logging.set_verbosity_info() _lowerCamelCase : Any = logging.get_logger(__name__) _lowerCamelCase : List[str] = { 'post_extract_proj': 'feature_projection.projection', 'encoder.pos_conv.0': 'encoder.pos_conv_embed.conv', 'self_attn.k_proj': 'encoder.layers.*.attention.k_proj', 'self_attn.v_proj': 'encoder.layers.*.attention.v_proj', 'self_attn.q_proj': 'encoder.layers.*.attention.q_proj', 'self_attn.out_proj': 'encoder.layers.*.attention.out_proj', 'self_attn_layer_norm': 'encoder.layers.*.layer_norm', 'fc1': 'encoder.layers.*.feed_forward.intermediate_dense', 'fc2': 'encoder.layers.*.feed_forward.output_dense', 'final_layer_norm': 'encoder.layers.*.final_layer_norm', 'encoder.layer_norm': 'encoder.layer_norm', 'w2v_model.layer_norm': 'feature_projection.layer_norm', 'quantizer.weight_proj': 'quantizer.weight_proj', 'quantizer.vars': 'quantizer.codevectors', 'project_q': 'project_q', 'final_proj': 'project_hid', 'w2v_encoder.proj': 'ctc_proj', 'mask_emb': 'masked_spec_embed', } _lowerCamelCase : Any = [ 'ctc_proj', 'quantizer.weight_proj', 'quantizer.codevectors', 'project_q', 'project_hid', ] def __a ( UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase ) ->Union[str, Any]: """simple docstring""" for attribute in key.split(""".""" ): if is_finetuned: if attribute in ["quantizer", "project_q", "project_hid"]: # those layers are only relevant for pretraining and should be dropped return if attribute == "ctc_proj": # we should rename `ctc_proj` to `lm_head` for fine-tuned phoneme models A = """lm_head""" A = getattr(UpperCAmelCase , UpperCAmelCase ) if weight_type is not None: A = getattr(UpperCAmelCase , UpperCAmelCase ).shape else: A = hf_pointer.shape assert hf_shape == value.shape, ( f"""Shape of hf {key + "." + weight_type if weight_type is not None else ""} is {hf_shape}, but should be""" f""" {value.shape} for {full_name}""" ) if weight_type == "weight": A = value elif weight_type == "weight_g": A = value elif weight_type == "weight_v": A = value elif weight_type == "bias": A = value else: A = value logger.info(f"""{key + "." + weight_type if weight_type is not None else ""} was initialized from {full_name}.""" ) def __a ( UpperCAmelCase , UpperCAmelCase , UpperCAmelCase ) ->Any: """simple docstring""" A = [] A = fairseq_model.state_dict() A = hf_model.unispeech.feature_extractor for name, value in fairseq_dict.items(): A = False if "conv_layers" in name: load_conv_layer( UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , hf_model.config.feat_extract_norm == """group""" , ) A = True else: for key, mapped_key in MAPPING.items(): A = """unispeech.""" + mapped_key if mapped_key not in TOP_LEVEL_KEYS else mapped_key if key in name or key.split("""w2v_model.""" )[-1] == name.split(""".""" )[0]: A = True if "*" in mapped_key: A = name.split(UpperCAmelCase )[0].split(""".""" )[-2] A = mapped_key.replace("""*""" , UpperCAmelCase ) if "weight_g" in name: A = """weight_g""" elif "weight_v" in name: A = """weight_v""" elif "bias" in name: A = """bias""" elif "weight" in name: # TODO: don't match quantizer.weight_proj A = """weight""" else: A = None set_recursively(UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase ) continue if not is_used: unused_weights.append(UpperCAmelCase ) logger.warning(f"""Unused weights: {unused_weights}""" ) def __a ( UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase ) ->List[str]: """simple docstring""" A = full_name.split("""conv_layers.""" )[-1] A = name.split(""".""" ) A = int(items[0] ) A = int(items[1] ) if type_id == 0: if "bias" in name: assert value.shape == feature_extractor.conv_layers[layer_id].conv.bias.data.shape, ( f"""{full_name} has size {value.shape}, but""" f""" {feature_extractor.conv_layers[layer_id].conv.bias.data.shape} was found.""" ) A = value logger.info(f"""Feat extract conv layer {layer_id} was initialized from {full_name}.""" ) elif "weight" in name: assert value.shape == feature_extractor.conv_layers[layer_id].conv.weight.data.shape, ( f"""{full_name} has size {value.shape}, but""" f""" {feature_extractor.conv_layers[layer_id].conv.weight.data.shape} was found.""" ) A = value logger.info(f"""Feat extract conv layer {layer_id} was initialized from {full_name}.""" ) elif (type_id == 2 and not use_group_norm) or (type_id == 2 and layer_id == 0 and use_group_norm): if "bias" in name: assert value.shape == feature_extractor.conv_layers[layer_id].layer_norm.bias.data.shape, ( f"""{full_name} has size {value.shape}, but {feature_extractor[layer_id].layer_norm.bias.data.shape} was""" " found." ) A = value logger.info(f"""Feat extract layer norm weight of layer {layer_id} was initialized from {full_name}.""" ) elif "weight" in name: assert value.shape == feature_extractor.conv_layers[layer_id].layer_norm.weight.data.shape, ( f"""{full_name} has size {value.shape}, but""" f""" {feature_extractor[layer_id].layer_norm.weight.data.shape} was found.""" ) A = value logger.info(f"""Feat extract layer norm weight of layer {layer_id} was initialized from {full_name}.""" ) else: unused_weights.append(UpperCAmelCase ) @torch.no_grad() def __a ( UpperCAmelCase , UpperCAmelCase , UpperCAmelCase=None , UpperCAmelCase=None , UpperCAmelCase=True ) ->Optional[Any]: """simple docstring""" if config_path is not None: A = UniSpeechConfig.from_pretrained(UpperCAmelCase ) else: A = UniSpeechConfig() if is_finetuned: if dict_path: A = Dictionary.load_from_json(UpperCAmelCase ) # important change bos & pad token id since CTC symbol is <pad> and # not <s> as in fairseq A = target_dict.pad_index A = target_dict.bos_index A = target_dict.eos_index A = len(target_dict.symbols ) A = os.path.join(UpperCAmelCase , """vocab.json""" ) if not os.path.isdir(UpperCAmelCase ): logger.error("""--pytorch_dump_folder_path ({}) should be a directory""".format(UpperCAmelCase ) ) return os.makedirs(UpperCAmelCase , exist_ok=UpperCAmelCase ) A = target_dict.indices # fairseq has the <pad> and <s> switched A = 42 A = 43 with open(UpperCAmelCase , """w""" , encoding="""utf-8""" ) as vocab_handle: json.dump(UpperCAmelCase , UpperCAmelCase ) A = WavaVecaPhonemeCTCTokenizer( UpperCAmelCase , unk_token=target_dict.unk_word , pad_token=target_dict.pad_word , bos_token=target_dict.bos_word , eos_token=target_dict.eos_word , word_delimiter_token="""|""" , do_lower_case=UpperCAmelCase , ) A = True if config.feat_extract_norm == """layer""" else False A = WavaVecaFeatureExtractor( feature_size=1 , sampling_rate=16000 , padding_value=0 , do_normalize=UpperCAmelCase , return_attention_mask=UpperCAmelCase , ) A = WavaVecaProcessor(feature_extractor=UpperCAmelCase , tokenizer=UpperCAmelCase ) processor.save_pretrained(UpperCAmelCase ) A = UniSpeechForCTC(UpperCAmelCase ) else: A = UniSpeechForPreTraining(UpperCAmelCase ) if is_finetuned: A , A , A = fairseq.checkpoint_utils.load_model_ensemble_and_task( [checkpoint_path] , arg_overrides={"""data""": """/""".join(dict_path.split("""/""" )[:-1] ), """w2v_path""": checkpoint_path} ) else: A , A , A = fairseq.checkpoint_utils.load_model_ensemble_and_task([checkpoint_path] ) A = model[0].eval() recursively_load_weights(UpperCAmelCase , UpperCAmelCase , UpperCAmelCase ) hf_unispeech.save_pretrained(UpperCAmelCase ) if __name__ == "__main__": _lowerCamelCase : str = argparse.ArgumentParser() parser.add_argument('--pytorch_dump_folder_path', default=None, type=str, help='Path to the output PyTorch model.') parser.add_argument('--checkpoint_path', default=None, type=str, help='Path to fairseq checkpoint') parser.add_argument('--dict_path', default=None, type=str, help='Path to dict of fine-tuned model') parser.add_argument('--config_path', default=None, type=str, help='Path to hf config.json of model to convert') parser.add_argument( '--not_finetuned', action='store_true', help='Whether the model to convert is a fine-tuned model or not' ) _lowerCamelCase : List[Any] = parser.parse_args() convert_unispeech_checkpoint( args.checkpoint_path, args.pytorch_dump_folder_path, args.config_path, args.dict_path, not args.not_finetuned )
364
'''simple docstring''' def __a ( UpperCAmelCase ) ->bool: """simple docstring""" return credit_card_number.startswith(("""34""", """35""", """37""", """4""", """5""", """6""") ) def __a ( UpperCAmelCase ) ->bool: """simple docstring""" A = credit_card_number A = 0 A = len(UpperCAmelCase ) - 2 for i in range(UpperCAmelCase , -1 , -2 ): # double the value of every second digit A = int(cc_number[i] ) digit *= 2 # If doubling of a number results in a two digit number # i.e greater than 9(e.g., 6 × 2 = 12), # then add the digits of the product (e.g., 12: 1 + 2 = 3, 15: 1 + 5 = 6), # to get a single digit number. if digit > 9: digit %= 10 digit += 1 A = cc_number[:i] + str(UpperCAmelCase ) + cc_number[i + 1 :] total += digit # Sum up the remaining digits for i in range(len(UpperCAmelCase ) - 1 , -1 , -2 ): total += int(cc_number[i] ) return total % 10 == 0 def __a ( UpperCAmelCase ) ->bool: """simple docstring""" A = f"""{credit_card_number} is an invalid credit card number because""" if not credit_card_number.isdigit(): print(f"""{error_message} it has nonnumerical characters.""" ) return False if not 13 <= len(UpperCAmelCase ) <= 16: print(f"""{error_message} of its length.""" ) return False if not validate_initial_digits(UpperCAmelCase ): print(f"""{error_message} of its first two digits.""" ) return False if not luhn_validation(UpperCAmelCase ): print(f"""{error_message} it fails the Luhn check.""" ) return False print(f"""{credit_card_number} is a valid credit card number.""" ) return True if __name__ == "__main__": import doctest doctest.testmod() validate_credit_card_number('4111111111111111') validate_credit_card_number('32323')
337
0
'''simple docstring''' import math import os import sys def UpperCAmelCase__ ( UpperCAmelCase__ ) -> str: A_ = """""" try: with open(UpperCAmelCase__, """rb""" ) as binary_file: A_ = binary_file.read() for dat in data: A_ = F'''{dat:08b}''' result += curr_byte return result except OSError: print("""File not accessible""" ) sys.exit() def UpperCAmelCase__ ( UpperCAmelCase__, UpperCAmelCase__, UpperCAmelCase__, UpperCAmelCase__ ) -> None: lexicon.pop(UpperCAmelCase__ ) A_ = last_match_id if math.loga(UpperCAmelCase__ ).is_integer(): for curr_key in lexicon: A_ = """0""" + lexicon[curr_key] A_ = bin(UpperCAmelCase__ )[2:] def UpperCAmelCase__ ( UpperCAmelCase__ ) -> str: A_ = {"""0""": """0""", """1""": """1"""} A_ , A_ = """""", """""" A_ = len(UpperCAmelCase__ ) for i in range(len(UpperCAmelCase__ ) ): curr_string += data_bits[i] if curr_string not in lexicon: continue A_ = lexicon[curr_string] result += last_match_id add_key_to_lexicon(UpperCAmelCase__, UpperCAmelCase__, UpperCAmelCase__, UpperCAmelCase__ ) index += 1 A_ = """""" while curr_string != "" and curr_string not in lexicon: curr_string += "0" if curr_string != "": A_ = lexicon[curr_string] result += last_match_id return result def UpperCAmelCase__ ( UpperCAmelCase__, UpperCAmelCase__ ) -> str: A_ = os.path.getsize(UpperCAmelCase__ ) A_ = bin(UpperCAmelCase__ )[2:] A_ = len(UpperCAmelCase__ ) return "0" * (length_length - 1) + file_length_binary + compressed def UpperCAmelCase__ ( UpperCAmelCase__, UpperCAmelCase__ ) -> None: A_ = 8 try: with open(UpperCAmelCase__, """wb""" ) as opened_file: A_ = [ to_write[i : i + byte_length] for i in range(0, len(UpperCAmelCase__ ), UpperCAmelCase__ ) ] if len(result_byte_array[-1] ) % byte_length == 0: result_byte_array.append("""10000000""" ) else: result_byte_array[-1] += "1" + "0" * ( byte_length - len(result_byte_array[-1] ) - 1 ) for elem in result_byte_array: opened_file.write(int(UpperCAmelCase__, 2 ).to_bytes(1, byteorder="""big""" ) ) except OSError: print("""File not accessible""" ) sys.exit() def UpperCAmelCase__ ( UpperCAmelCase__, UpperCAmelCase__ ) -> None: A_ = read_file_binary(UpperCAmelCase__ ) A_ = compress_data(UpperCAmelCase__ ) A_ = add_file_length(UpperCAmelCase__, UpperCAmelCase__ ) write_file_binary(UpperCAmelCase__, UpperCAmelCase__ ) if __name__ == "__main__": compress(sys.argv[1], sys.argv[2])
162
'''simple docstring''' from __future__ import annotations def UpperCAmelCase__ ( UpperCAmelCase__ ) -> bool: A_ = len(UpperCAmelCase__ ) # We need to create solution object to save path. A_ = [[0 for _ in range(UpperCAmelCase__ )] for _ in range(UpperCAmelCase__ )] A_ = run_maze(UpperCAmelCase__, 0, 0, UpperCAmelCase__ ) if solved: print("""\n""".join(str(UpperCAmelCase__ ) for row in solutions ) ) else: print("""No solution exists!""" ) return solved def UpperCAmelCase__ ( UpperCAmelCase__, UpperCAmelCase__, UpperCAmelCase__, UpperCAmelCase__ ) -> bool: A_ = len(UpperCAmelCase__ ) # Final check point. if i == j == (size - 1): A_ = 1 return True A_ = (not i < 0) and (not j < 0) # Check lower bounds A_ = (i < size) and (j < size) # Check upper bounds if lower_flag and upper_flag: # check for already visited and block points. A_ = (not solutions[i][j]) and (not maze[i][j]) if block_flag: # check visited A_ = 1 # check for directions if ( run_maze(UpperCAmelCase__, i + 1, UpperCAmelCase__, UpperCAmelCase__ ) or run_maze(UpperCAmelCase__, UpperCAmelCase__, j + 1, UpperCAmelCase__ ) or run_maze(UpperCAmelCase__, i - 1, UpperCAmelCase__, UpperCAmelCase__ ) or run_maze(UpperCAmelCase__, UpperCAmelCase__, j - 1, UpperCAmelCase__ ) ): return True A_ = 0 return False return False if __name__ == "__main__": import doctest doctest.testmod()
162
1
import json from typing import List, Optional, Tuple from tokenizers import normalizers from ...tokenization_utils_fast import PreTrainedTokenizerFast from .tokenization_electra import ElectraTokenizer __UpperCAmelCase = {'vocab_file': 'vocab.txt', 'tokenizer_file': 'tokenizer.json'} __UpperCAmelCase = { 'vocab_file': { 'google/electra-small-generator': ( 'https://huggingface.co/google/electra-small-generator/resolve/main/vocab.txt' ), 'google/electra-base-generator': 'https://huggingface.co/google/electra-base-generator/resolve/main/vocab.txt', 'google/electra-large-generator': ( 'https://huggingface.co/google/electra-large-generator/resolve/main/vocab.txt' ), 'google/electra-small-discriminator': ( 'https://huggingface.co/google/electra-small-discriminator/resolve/main/vocab.txt' ), 'google/electra-base-discriminator': ( 'https://huggingface.co/google/electra-base-discriminator/resolve/main/vocab.txt' ), 'google/electra-large-discriminator': ( 'https://huggingface.co/google/electra-large-discriminator/resolve/main/vocab.txt' ), }, 'tokenizer_file': { 'google/electra-small-generator': ( 'https://huggingface.co/google/electra-small-generator/resolve/main/tokenizer.json' ), 'google/electra-base-generator': ( 'https://huggingface.co/google/electra-base-generator/resolve/main/tokenizer.json' ), 'google/electra-large-generator': ( 'https://huggingface.co/google/electra-large-generator/resolve/main/tokenizer.json' ), 'google/electra-small-discriminator': ( 'https://huggingface.co/google/electra-small-discriminator/resolve/main/tokenizer.json' ), 'google/electra-base-discriminator': ( 'https://huggingface.co/google/electra-base-discriminator/resolve/main/tokenizer.json' ), 'google/electra-large-discriminator': ( 'https://huggingface.co/google/electra-large-discriminator/resolve/main/tokenizer.json' ), }, } __UpperCAmelCase = { 'google/electra-small-generator': 5_12, 'google/electra-base-generator': 5_12, 'google/electra-large-generator': 5_12, 'google/electra-small-discriminator': 5_12, 'google/electra-base-discriminator': 5_12, 'google/electra-large-discriminator': 5_12, } __UpperCAmelCase = { 'google/electra-small-generator': {'do_lower_case': True}, 'google/electra-base-generator': {'do_lower_case': True}, 'google/electra-large-generator': {'do_lower_case': True}, 'google/electra-small-discriminator': {'do_lower_case': True}, 'google/electra-base-discriminator': {'do_lower_case': True}, 'google/electra-large-discriminator': {'do_lower_case': True}, } class __a ( __UpperCamelCase ): __snake_case : List[Any] = VOCAB_FILES_NAMES __snake_case : List[str] = PRETRAINED_VOCAB_FILES_MAP __snake_case : Dict = PRETRAINED_INIT_CONFIGURATION __snake_case : Any = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES __snake_case : str = ElectraTokenizer def __init__( self : List[Any] , UpperCAmelCase : Any=None , UpperCAmelCase : List[str]=None , UpperCAmelCase : Optional[int]=True , UpperCAmelCase : Dict="[UNK]" , UpperCAmelCase : Any="[SEP]" , UpperCAmelCase : Any="[PAD]" , UpperCAmelCase : Union[str, Any]="[CLS]" , UpperCAmelCase : Optional[Any]="[MASK]" , UpperCAmelCase : Optional[int]=True , UpperCAmelCase : Union[str, Any]=None , **UpperCAmelCase : Optional[Any] , ): super().__init__( UpperCAmelCase , tokenizer_file=UpperCAmelCase , do_lower_case=UpperCAmelCase , unk_token=UpperCAmelCase , sep_token=UpperCAmelCase , pad_token=UpperCAmelCase , cls_token=UpperCAmelCase , mask_token=UpperCAmelCase , tokenize_chinese_chars=UpperCAmelCase , strip_accents=UpperCAmelCase , **UpperCAmelCase , ) lowerCAmelCase_ : Optional[int] = json.loads(self.backend_tokenizer.normalizer.__getstate__() ) if ( normalizer_state.get("""lowercase""" , UpperCAmelCase ) != do_lower_case or normalizer_state.get("""strip_accents""" , UpperCAmelCase ) != strip_accents or normalizer_state.get("""handle_chinese_chars""" , UpperCAmelCase ) != tokenize_chinese_chars ): lowerCAmelCase_ : Optional[Any] = getattr(UpperCAmelCase , normalizer_state.pop("""type""" ) ) lowerCAmelCase_ : List[Any] = do_lower_case lowerCAmelCase_ : Tuple = strip_accents lowerCAmelCase_ : Union[str, Any] = tokenize_chinese_chars lowerCAmelCase_ : int = normalizer_class(**UpperCAmelCase ) lowerCAmelCase_ : str = do_lower_case def A ( self : Optional[int] , UpperCAmelCase : List[Any] , UpperCAmelCase : Union[str, Any]=None ): lowerCAmelCase_ : str = [self.cls_token_id] + token_ids_a + [self.sep_token_id] if token_ids_a: output += token_ids_a + [self.sep_token_id] return output def A ( self : List[Any] , UpperCAmelCase : List[int] , UpperCAmelCase : Optional[List[int]] = None ): lowerCAmelCase_ : str = [self.sep_token_id] lowerCAmelCase_ : Any = [self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep ) * [0] return len(cls + token_ids_a + sep ) * [0] + len(token_ids_a + sep ) * [1] def A ( self : Optional[int] , UpperCAmelCase : str , UpperCAmelCase : Optional[str] = None ): lowerCAmelCase_ : Union[str, Any] = self._tokenizer.model.save(UpperCAmelCase , name=UpperCAmelCase ) return tuple(UpperCAmelCase )
370
from ...configuration_utils import PretrainedConfig from ...utils import logging __UpperCAmelCase = logging.get_logger(__name__) __UpperCAmelCase = { 'uw-madison/mra-base-512-4': 'https://huggingface.co/uw-madison/mra-base-512-4/resolve/main/config.json', } class __a ( __UpperCamelCase ): __snake_case : Optional[Any] = """mra""" def __init__( self : List[str] , UpperCAmelCase : Tuple=5_02_65 , UpperCAmelCase : str=7_68 , UpperCAmelCase : int=12 , UpperCAmelCase : Dict=12 , UpperCAmelCase : Tuple=30_72 , UpperCAmelCase : str="gelu" , UpperCAmelCase : Optional[Any]=0.1 , UpperCAmelCase : Optional[int]=0.1 , UpperCAmelCase : List[str]=5_12 , UpperCAmelCase : Optional[Any]=1 , UpperCAmelCase : Tuple=0.02 , UpperCAmelCase : int=1e-5 , UpperCAmelCase : Optional[int]="absolute" , UpperCAmelCase : Optional[Any]=4 , UpperCAmelCase : Any="full" , UpperCAmelCase : Optional[Any]=0 , UpperCAmelCase : List[str]=0 , UpperCAmelCase : Any=1 , UpperCAmelCase : int=0 , UpperCAmelCase : int=2 , **UpperCAmelCase : Tuple , ): super().__init__(pad_token_id=UpperCAmelCase , bos_token_id=UpperCAmelCase , eos_token_id=UpperCAmelCase , **UpperCAmelCase ) lowerCAmelCase_ : Union[str, Any] = vocab_size lowerCAmelCase_ : Optional[int] = max_position_embeddings lowerCAmelCase_ : Any = hidden_size lowerCAmelCase_ : List[Any] = num_hidden_layers lowerCAmelCase_ : Tuple = num_attention_heads lowerCAmelCase_ : List[Any] = intermediate_size lowerCAmelCase_ : Dict = hidden_act lowerCAmelCase_ : Optional[Any] = hidden_dropout_prob lowerCAmelCase_ : Any = attention_probs_dropout_prob lowerCAmelCase_ : str = initializer_range lowerCAmelCase_ : str = type_vocab_size lowerCAmelCase_ : str = layer_norm_eps lowerCAmelCase_ : Optional[int] = position_embedding_type lowerCAmelCase_ : Any = block_per_row lowerCAmelCase_ : int = approx_mode lowerCAmelCase_ : Union[str, Any] = initial_prior_first_n_blocks lowerCAmelCase_ : Dict = initial_prior_diagonal_n_blocks
28
0
import json import os import shutil import tempfile import unittest import numpy as np import pytest from transformers import CLIPTokenizer, CLIPTokenizerFast from transformers.models.clip.tokenization_clip import VOCAB_FILES_NAMES from transformers.testing_utils import require_vision from transformers.utils import IMAGE_PROCESSOR_NAME, is_vision_available if is_vision_available(): from PIL import Image from transformers import OwlViTImageProcessor, OwlViTProcessor @require_vision class lowercase ( unittest.TestCase ): def a ( self ): snake_case_ = tempfile.mkdtemp() # fmt: off snake_case_ = ['', 'l', 'o', 'w', 'e', 'r', 's', 't', 'i', 'd', 'n', 'lo', 'l</w>', 'w</w>', 'r</w>', 't</w>', 'low</w>', 'er</w>', 'lowest</w>', 'newer</w>', 'wider', '<unk>', '<|startoftext|>', '<|endoftext|>'] # fmt: on snake_case_ = dict(zip(snake_case , range(len(snake_case ) ) ) ) snake_case_ = ['#version: 0.2', 'l o', 'lo w</w>', 'e r</w>', ''] snake_case_ = {'unk_token': '<unk>'} snake_case_ = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES['vocab_file'] ) snake_case_ = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES['merges_file'] ) with open(self.vocab_file , 'w' , encoding='utf-8' ) as fp: fp.write(json.dumps(snake_case ) + '\n' ) with open(self.merges_file , 'w' , encoding='utf-8' ) as fp: fp.write('\n'.join(snake_case ) ) snake_case_ = { 'do_resize': True, 'size': 20, 'do_center_crop': True, 'crop_size': 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], } snake_case_ = os.path.join(self.tmpdirname , snake_case ) with open(self.image_processor_file , 'w' , encoding='utf-8' ) as fp: json.dump(snake_case , snake_case ) def a ( self , **snake_case ): return CLIPTokenizer.from_pretrained(self.tmpdirname , pad_token='!' , **snake_case ) def a ( self , **snake_case ): return CLIPTokenizerFast.from_pretrained(self.tmpdirname , pad_token='!' , **snake_case ) def a ( self , **snake_case ): return OwlViTImageProcessor.from_pretrained(self.tmpdirname , **snake_case ) def a ( self ): shutil.rmtree(self.tmpdirname ) def a ( self ): snake_case_ = [np.random.randint(255 , size=(3, 30, 400) , dtype=np.uinta )] snake_case_ = [Image.fromarray(np.moveaxis(snake_case , 0 , -1 ) ) for x in image_inputs] return image_inputs def a ( self ): snake_case_ = self.get_tokenizer() snake_case_ = self.get_rust_tokenizer() snake_case_ = self.get_image_processor() snake_case_ = OwlViTProcessor(tokenizer=snake_case , image_processor=snake_case ) processor_slow.save_pretrained(self.tmpdirname ) snake_case_ = OwlViTProcessor.from_pretrained(self.tmpdirname , use_fast=snake_case ) snake_case_ = OwlViTProcessor(tokenizer=snake_case , image_processor=snake_case ) processor_fast.save_pretrained(self.tmpdirname ) snake_case_ = OwlViTProcessor.from_pretrained(self.tmpdirname ) self.assertEqual(processor_slow.tokenizer.get_vocab() , tokenizer_slow.get_vocab() ) self.assertEqual(processor_fast.tokenizer.get_vocab() , tokenizer_fast.get_vocab() ) self.assertEqual(tokenizer_slow.get_vocab() , tokenizer_fast.get_vocab() ) self.assertIsInstance(processor_slow.tokenizer , snake_case ) self.assertIsInstance(processor_fast.tokenizer , snake_case ) 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 , snake_case ) self.assertIsInstance(processor_fast.image_processor , snake_case ) def a ( self ): snake_case_ = OwlViTProcessor(tokenizer=self.get_tokenizer() , image_processor=self.get_image_processor() ) processor.save_pretrained(self.tmpdirname ) snake_case_ = self.get_tokenizer(bos_token='(BOS)' , eos_token='(EOS)' ) snake_case_ = self.get_image_processor(do_normalize=snake_case ) snake_case_ = OwlViTProcessor.from_pretrained( self.tmpdirname , bos_token='(BOS)' , eos_token='(EOS)' , do_normalize=snake_case ) self.assertEqual(processor.tokenizer.get_vocab() , tokenizer_add_kwargs.get_vocab() ) self.assertIsInstance(processor.tokenizer , snake_case ) self.assertEqual(processor.image_processor.to_json_string() , image_processor_add_kwargs.to_json_string() ) self.assertIsInstance(processor.image_processor , snake_case ) def a ( self ): snake_case_ = self.get_image_processor() snake_case_ = self.get_tokenizer() snake_case_ = OwlViTProcessor(tokenizer=snake_case , image_processor=snake_case ) snake_case_ = self.prepare_image_inputs() snake_case_ = image_processor(snake_case , return_tensors='np' ) snake_case_ = processor(images=snake_case , 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 a ( self ): snake_case_ = self.get_image_processor() snake_case_ = self.get_tokenizer() snake_case_ = OwlViTProcessor(tokenizer=snake_case , image_processor=snake_case ) snake_case_ = 'lower newer' snake_case_ = processor(text=snake_case , return_tensors='np' ) snake_case_ = tokenizer(snake_case , return_tensors='np' ) for key in encoded_tok.keys(): self.assertListEqual(encoded_tok[key][0].tolist() , encoded_processor[key][0].tolist() ) def a ( self ): snake_case_ = self.get_image_processor() snake_case_ = self.get_tokenizer() snake_case_ = OwlViTProcessor(tokenizer=snake_case , image_processor=snake_case ) snake_case_ = 'lower newer' snake_case_ = self.prepare_image_inputs() snake_case_ = processor(text=snake_case , images=snake_case ) self.assertListEqual(list(inputs.keys() ) , ['input_ids', 'attention_mask', 'pixel_values'] ) # test if it raises when no input is passed with pytest.raises(snake_case ): processor() def a ( self ): snake_case_ = 'google/owlvit-base-patch32' snake_case_ = OwlViTProcessor.from_pretrained(snake_case ) snake_case_ = ['cat', 'nasa badge'] snake_case_ = processor(text=snake_case ) snake_case_ = 16 self.assertListEqual(list(inputs.keys() ) , ['input_ids', 'attention_mask'] ) self.assertEqual(inputs['input_ids'].shape , (2, seq_length) ) # test if it raises when no input is passed with pytest.raises(snake_case ): processor() def a ( self ): snake_case_ = 'google/owlvit-base-patch32' snake_case_ = OwlViTProcessor.from_pretrained(snake_case ) snake_case_ = [['cat', 'nasa badge'], ['person']] snake_case_ = processor(text=snake_case ) snake_case_ = 16 snake_case_ = len(snake_case ) snake_case_ = max([len(snake_case ) for texts in input_texts] ) self.assertListEqual(list(inputs.keys() ) , ['input_ids', 'attention_mask'] ) self.assertEqual(inputs['input_ids'].shape , (batch_size * num_max_text_queries, seq_length) ) # test if it raises when no input is passed with pytest.raises(snake_case ): processor() def a ( self ): snake_case_ = 'google/owlvit-base-patch32' snake_case_ = OwlViTProcessor.from_pretrained(snake_case ) snake_case_ = ['cat', 'nasa badge'] snake_case_ = processor(text=snake_case ) snake_case_ = 16 snake_case_ = inputs['input_ids'] snake_case_ = [ [4_9406, 2368, 4_9407, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [4_9406, 6841, 1_1301, 4_9407, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], ] self.assertListEqual(list(inputs.keys() ) , ['input_ids', 'attention_mask'] ) self.assertEqual(inputs['input_ids'].shape , (2, seq_length) ) self.assertListEqual(list(input_ids[0] ) , predicted_ids[0] ) self.assertListEqual(list(input_ids[1] ) , predicted_ids[1] ) def a ( self ): snake_case_ = self.get_image_processor() snake_case_ = self.get_tokenizer() snake_case_ = OwlViTProcessor(tokenizer=snake_case , image_processor=snake_case ) snake_case_ = self.prepare_image_inputs() snake_case_ = self.prepare_image_inputs() snake_case_ = processor(images=snake_case , query_images=snake_case ) self.assertListEqual(list(inputs.keys() ) , ['query_pixel_values', 'pixel_values'] ) # test if it raises when no input is passed with pytest.raises(snake_case ): processor() def a ( self ): snake_case_ = self.get_image_processor() snake_case_ = self.get_tokenizer() snake_case_ = OwlViTProcessor(tokenizer=snake_case , image_processor=snake_case ) snake_case_ = [[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]] snake_case_ = processor.batch_decode(snake_case ) snake_case_ = tokenizer.batch_decode(snake_case ) self.assertListEqual(snake_case , snake_case )
285
from typing import Dict, List, Optional from ...tokenization_utils import AddedToken, PreTrainedTokenizer from ...utils import logging _UpperCAmelCase : Any = logging.get_logger(__name__) _UpperCAmelCase : Dict = { """nielsr/canine-s""": 2048, } # Unicode defines 1,114,112 total “codepoints” _UpperCAmelCase : Tuple = 111_4112 # Below: Constants defining canonical codepoints for special, pseudo-characters. # Copied from https://github.com/google-research/language/blob/master/language/canine/special_codepoints.py _UpperCAmelCase : List[str] = 0 _UpperCAmelCase : Any = 0xE000 _UpperCAmelCase : Dict = 0xE001 _UpperCAmelCase : Optional[int] = 0xE002 _UpperCAmelCase : Tuple = 0xE003 _UpperCAmelCase : Tuple = 0xE004 # Maps special codepoints to human-readable names. _UpperCAmelCase : Dict[int, str] = { # Special symbols are represented using codepoints values that are valid, # but designated as "Private Use", meaning that they will never be assigned # characters by the Unicode Consortium, and are thus safe for use here. # # NOTE: Do *NOT* add any sort of [UNK_CHAR] here. They are explicitly # excluded and should fail with a hard error. CLS: "[CLS]", SEP: "[SEP]", BOS: "[BOS]", MASK: "[MASK]", PAD: "[PAD]", RESERVED: "[RESERVED]", } # Maps special codepoint human-readable names to their codepoint values. _UpperCAmelCase : Dict[str, int] = {name: codepoint for codepoint, name in SPECIAL_CODEPOINTS.items()} class lowercase ( lowercase_ ): __SCREAMING_SNAKE_CASE : str = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES def __init__( self , snake_case=chr(snake_case ) , snake_case=chr(snake_case ) , snake_case=chr(snake_case ) , snake_case=chr(snake_case ) , snake_case=chr(snake_case ) , snake_case=chr(snake_case ) , snake_case=False , snake_case=2048 , **snake_case , ): snake_case_ = AddedToken(snake_case , lstrip=snake_case , rstrip=snake_case ) if isinstance(snake_case , snake_case ) else bos_token snake_case_ = AddedToken(snake_case , lstrip=snake_case , rstrip=snake_case ) if isinstance(snake_case , snake_case ) else eos_token snake_case_ = AddedToken(snake_case , lstrip=snake_case , rstrip=snake_case ) if isinstance(snake_case , snake_case ) else sep_token snake_case_ = AddedToken(snake_case , lstrip=snake_case , rstrip=snake_case ) if isinstance(snake_case , snake_case ) else cls_token snake_case_ = AddedToken(snake_case , lstrip=snake_case , rstrip=snake_case ) if isinstance(snake_case , snake_case ) else pad_token # Mask token behave like a normal word, i.e. include the space before it snake_case_ = AddedToken(snake_case , lstrip=snake_case , rstrip=snake_case ) if isinstance(snake_case , snake_case ) else mask_token super().__init__( bos_token=snake_case , eos_token=snake_case , sep_token=snake_case , cls_token=snake_case , pad_token=snake_case , mask_token=snake_case , add_prefix_space=snake_case , model_max_length=snake_case , **snake_case , ) # Creates a mapping for looking up the IDs of special symbols. snake_case_ = {} for codepoint, name in SPECIAL_CODEPOINTS.items(): snake_case_ = codepoint # Creates a mapping for looking up the string forms of special symbol IDs. snake_case_ = { codepoint: name for name, codepoint in self._special_codepoints.items() } snake_case_ = UNICODE_VOCAB_SIZE snake_case_ = len(self._special_codepoints ) @property def a ( self ): return self._unicode_vocab_size def a ( self , snake_case ): return list(snake_case ) def a ( self , snake_case ): try: return ord(snake_case ) except TypeError: raise ValueError(F'''invalid token: \'{token}\'''' ) def a ( self , snake_case ): try: if index in SPECIAL_CODEPOINTS: return SPECIAL_CODEPOINTS[index] return chr(snake_case ) except TypeError: raise ValueError(F'''invalid id: {index}''' ) def a ( self , snake_case ): return "".join(snake_case ) def a ( self , snake_case , snake_case = None ): snake_case_ = [self.sep_token_id] snake_case_ = [self.cls_token_id] snake_case_ = cls + token_ids_a + sep if token_ids_a is not None: result += token_ids_a + sep return result def a ( self , snake_case , snake_case = None , snake_case = False ): if already_has_special_tokens: return super().get_special_tokens_mask( token_ids_a=snake_case , token_ids_a=snake_case , already_has_special_tokens=snake_case ) snake_case_ = [1] + ([0] * len(snake_case )) + [1] if token_ids_a is not None: result += ([0] * len(snake_case )) + [1] return result def a ( self , snake_case , snake_case = None ): snake_case_ = [self.sep_token_id] snake_case_ = [self.cls_token_id] snake_case_ = len(cls + token_ids_a + sep ) * [0] if token_ids_a is not None: result += len(token_ids_a + sep ) * [1] return result def a ( self , snake_case , snake_case = None ): return ()
285
1
"""simple docstring""" from itertools import product def _lowerCAmelCase ( UpperCAmelCase : int , UpperCAmelCase : int ): '''simple docstring''' UpperCamelCase__ : List[str] =sides_number UpperCamelCase__ : Union[str, Any] =max_face_number * dice_number UpperCamelCase__ : int =[0] * (max_total + 1) UpperCamelCase__ : Dict =1 UpperCamelCase__ : int =range(UpperCAmelCase , max_face_number + 1 ) for dice_numbers in product(UpperCAmelCase , repeat=UpperCAmelCase ): UpperCamelCase__ : Dict =sum(UpperCAmelCase ) totals_frequencies[total] += 1 return totals_frequencies def _lowerCAmelCase ( ): '''simple docstring''' UpperCamelCase__ : str =total_frequency_distribution( sides_number=4 , dice_number=9 ) UpperCamelCase__ : Union[str, Any] =total_frequency_distribution( sides_number=6 , dice_number=6 ) UpperCamelCase__ : Union[str, Any] =0 UpperCamelCase__ : Union[str, Any] =9 UpperCamelCase__ : List[Any] =4 * 9 UpperCamelCase__ : Optional[Any] =6 for peter_total in range(UpperCAmelCase , max_peter_total + 1 ): peter_wins_count += peter_totals_frequencies[peter_total] * sum( colin_totals_frequencies[min_colin_total:peter_total] ) UpperCamelCase__ : Union[str, Any] =(4**9) * (6**6) UpperCamelCase__ : int =peter_wins_count / total_games_number UpperCamelCase__ : Tuple =round(UpperCAmelCase , ndigits=7 ) return rounded_peter_win_probability if __name__ == "__main__": print(F'''{solution() = }''')
350
"""simple docstring""" # Copyright 2022 The HuggingFace Team and The OpenBMB Team. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. from typing import TYPE_CHECKING # rely on isort to merge the imports from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tokenizers_available, is_torch_available _SCREAMING_SNAKE_CASE : Any = { """configuration_cpmant""": ["""CPMANT_PRETRAINED_CONFIG_ARCHIVE_MAP""", """CpmAntConfig"""], """tokenization_cpmant""": ["""CpmAntTokenizer"""], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _SCREAMING_SNAKE_CASE : Any = [ """CPMANT_PRETRAINED_MODEL_ARCHIVE_LIST""", """CpmAntForCausalLM""", """CpmAntModel""", """CpmAntPreTrainedModel""", ] if TYPE_CHECKING: from .configuration_cpmant import CPMANT_PRETRAINED_CONFIG_ARCHIVE_MAP, CpmAntConfig from .tokenization_cpmant import CpmAntTokenizer try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_cpmant import ( CPMANT_PRETRAINED_MODEL_ARCHIVE_LIST, CpmAntForCausalLM, CpmAntModel, CpmAntPreTrainedModel, ) else: import sys _SCREAMING_SNAKE_CASE : Dict = _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)
157
0
from json import JSONDecodeError # Workaround for requests.exceptions.JSONDecodeError import requests def __SCREAMING_SNAKE_CASE (SCREAMING_SNAKE_CASE__ = "isbn/0140328726" ): snake_case_ = olid.strip().strip('''/''' ) # Remove leading/trailing whitespace & slashes if new_olid.count('''/''' ) != 1: snake_case_ = F'''{olid} is not a valid Open Library olid''' raise ValueError(SCREAMING_SNAKE_CASE__ ) return requests.get(F'''https://openlibrary.org/{new_olid}.json''' ).json() def __SCREAMING_SNAKE_CASE (SCREAMING_SNAKE_CASE__ ): snake_case_ = { '''title''': '''Title''', '''publish_date''': '''Publish date''', '''authors''': '''Authors''', '''number_of_pages''': '''Number of pages:''', '''first_sentence''': '''First sentence''', '''isbn_10''': '''ISBN (10)''', '''isbn_13''': '''ISBN (13)''', } snake_case_ = {better_key: ol_book_data[key] for key, better_key in desired_keys.items()} snake_case_ = [ get_openlibrary_data(author['''key'''] )['''name'''] for author in data['''Authors'''] ] snake_case_ = data['''First sentence''']['''value'''] for key, value in data.items(): if isinstance(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ): snake_case_ = ''', '''.join(SCREAMING_SNAKE_CASE__ ) return data if __name__ == "__main__": import doctest doctest.testmod() while True: lowerCAmelCase_ = input('''\nEnter the ISBN code to search (or \'quit\' to stop): ''').strip() if isbn.lower() in ("", "q", "quit", "exit", "stop"): break if len(isbn) not in (10, 13) or not isbn.isdigit(): print(f"""Sorry, {isbn} is not a valid ISBN. Please, input a valid ISBN.""") continue print(f"""\nSearching Open Library for ISBN: {isbn}...\n""") try: lowerCAmelCase_ = summarize_book(get_openlibrary_data(f"""isbn/{isbn}""")) print('''\n'''.join(f"""{key}: {value}""" for key, value in book_summary.items())) except JSONDecodeError: # Workaround for requests.exceptions.RequestException: print(f"""Sorry, there are no results for ISBN: {isbn}.""")
8
from sklearn.metrics import mean_squared_error import datasets lowerCAmelCase_ = '''\ @article{scikit-learn, title={Scikit-learn: Machine Learning in {P}ython}, author={Pedregosa, F. and Varoquaux, G. and Gramfort, A. and Michel, V. and Thirion, B. and Grisel, O. and Blondel, M. and Prettenhofer, P. and Weiss, R. and Dubourg, V. and Vanderplas, J. and Passos, A. and Cournapeau, D. and Brucher, M. and Perrot, M. and Duchesnay, E.}, journal={Journal of Machine Learning Research}, volume={12}, pages={2825--2830}, year={2011} } ''' lowerCAmelCase_ = '''\ Mean Squared Error(MSE) is the average of the square of difference between the predicted and actual values. ''' lowerCAmelCase_ = ''' Args: predictions: array-like of shape (n_samples,) or (n_samples, n_outputs) Estimated target values. references: array-like of shape (n_samples,) or (n_samples, n_outputs) Ground truth (correct) target values. sample_weight: array-like of shape (n_samples,), default=None Sample weights. multioutput: {"raw_values", "uniform_average"} or array-like of shape (n_outputs,), default="uniform_average" Defines aggregating of multiple output values. Array-like value defines weights used to average errors. "raw_values" : Returns a full set of errors in case of multioutput input. "uniform_average" : Errors of all outputs are averaged with uniform weight. squared : bool, default=True If True returns MSE value, if False returns RMSE (Root Mean Squared Error) value. Returns: mse : mean squared error. Examples: >>> mse_metric = datasets.load_metric("mse") >>> predictions = [2.5, 0.0, 2, 8] >>> references = [3, -0.5, 2, 7] >>> results = mse_metric.compute(predictions=predictions, references=references) >>> print(results) {\'mse\': 0.375} >>> rmse_result = mse_metric.compute(predictions=predictions, references=references, squared=False) >>> print(rmse_result) {\'mse\': 0.6123724356957945} If you\'re using multi-dimensional lists, then set the config as follows : >>> mse_metric = datasets.load_metric("mse", "multilist") >>> predictions = [[0.5, 1], [-1, 1], [7, -6]] >>> references = [[0, 2], [-1, 2], [8, -5]] >>> results = mse_metric.compute(predictions=predictions, references=references) >>> print(results) {\'mse\': 0.7083333333333334} >>> results = mse_metric.compute(predictions=predictions, references=references, multioutput=\'raw_values\') >>> print(results) # doctest: +NORMALIZE_WHITESPACE {\'mse\': array([0.41666667, 1. ])} ''' @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class snake_case_ ( datasets.Metric ): '''simple docstring''' def snake_case__( self : Optional[int] ) ->List[Any]: return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features(self._get_feature_types() ) , reference_urls=[ '''https://scikit-learn.org/stable/modules/generated/sklearn.metrics.mean_squared_error.html''' ] , ) def snake_case__( self : List[Any] ) ->Optional[int]: if self.config_name == "multilist": return { "predictions": datasets.Sequence(datasets.Value('''float''' ) ), "references": datasets.Sequence(datasets.Value('''float''' ) ), } else: return { "predictions": datasets.Value('''float''' ), "references": datasets.Value('''float''' ), } def snake_case__( self : int , _UpperCamelCase : int , _UpperCamelCase : Union[str, Any] , _UpperCamelCase : Any=None , _UpperCamelCase : Optional[int]="uniform_average" , _UpperCamelCase : Tuple=True ) ->Tuple: snake_case_ = mean_squared_error( _UpperCamelCase , _UpperCamelCase , sample_weight=_UpperCamelCase , multioutput=_UpperCamelCase , squared=_UpperCamelCase ) return {"mse": mse}
8
1
import warnings from ...utils import logging from .image_processing_donut import DonutImageProcessor __UpperCAmelCase = logging.get_logger(__name__) class SCREAMING_SNAKE_CASE ( a_ ): """simple docstring""" def __init__( self : Optional[int] , *lowerCAmelCase : Optional[Any] , **lowerCAmelCase : Optional[int] ) -> None: """simple docstring""" warnings.warn( """The class DonutFeatureExtractor is deprecated and will be removed in version 5 of Transformers. Please""" """ use DonutImageProcessor instead.""" , lowerCAmelCase , ) super().__init__(*lowerCAmelCase , **lowerCAmelCase )
354
import argparse import json import requests import timm import torch from huggingface_hub import hf_hub_download from PIL import Image from transformers import AutoImageProcessor, SwinConfig, SwinForImageClassification def snake_case_ (__A : Optional[Any] ) -> Tuple: __lowerCAmelCase : Optional[int] = SwinConfig() __lowerCAmelCase : List[Any] = swin_name.split("""_""" ) __lowerCAmelCase : Dict = name_split[1] __lowerCAmelCase : Optional[Any] = int(name_split[4] ) __lowerCAmelCase : List[Any] = int(name_split[3][-1] ) if model_size == "tiny": __lowerCAmelCase : List[Any] = 9_6 __lowerCAmelCase : List[Any] = (2, 2, 6, 2) __lowerCAmelCase : Optional[Any] = (3, 6, 1_2, 2_4) elif model_size == "small": __lowerCAmelCase : List[Any] = 9_6 __lowerCAmelCase : Optional[int] = (2, 2, 1_8, 2) __lowerCAmelCase : Optional[int] = (3, 6, 1_2, 2_4) elif model_size == "base": __lowerCAmelCase : List[Any] = 1_2_8 __lowerCAmelCase : Tuple = (2, 2, 1_8, 2) __lowerCAmelCase : int = (4, 8, 1_6, 3_2) else: __lowerCAmelCase : List[Any] = 1_9_2 __lowerCAmelCase : List[str] = (2, 2, 1_8, 2) __lowerCAmelCase : int = (6, 1_2, 2_4, 4_8) if "in22k" in swin_name: __lowerCAmelCase : Dict = 2_1_8_4_1 else: __lowerCAmelCase : Optional[Any] = 1_0_0_0 __lowerCAmelCase : Union[str, Any] = """huggingface/label-files""" __lowerCAmelCase : Any = """imagenet-1k-id2label.json""" __lowerCAmelCase : Any = json.load(open(hf_hub_download(__A , __A , repo_type="""dataset""" ) , """r""" ) ) __lowerCAmelCase : int = {int(__A ): v for k, v in idalabel.items()} __lowerCAmelCase : str = idalabel __lowerCAmelCase : int = {v: k for k, v in idalabel.items()} __lowerCAmelCase : Optional[Any] = img_size __lowerCAmelCase : Optional[Any] = num_classes __lowerCAmelCase : Tuple = embed_dim __lowerCAmelCase : Union[str, Any] = depths __lowerCAmelCase : Optional[Any] = num_heads __lowerCAmelCase : Tuple = window_size return config def snake_case_ (__A : int ) -> Optional[Any]: if "patch_embed.proj" in name: __lowerCAmelCase : Optional[Any] = name.replace("""patch_embed.proj""" , """embeddings.patch_embeddings.projection""" ) if "patch_embed.norm" in name: __lowerCAmelCase : List[Any] = name.replace("""patch_embed.norm""" , """embeddings.norm""" ) if "layers" in name: __lowerCAmelCase : int = """encoder.""" + name if "attn.proj" in name: __lowerCAmelCase : Tuple = name.replace("""attn.proj""" , """attention.output.dense""" ) if "attn" in name: __lowerCAmelCase : Optional[Any] = name.replace("""attn""" , """attention.self""" ) if "norm1" in name: __lowerCAmelCase : Dict = name.replace("""norm1""" , """layernorm_before""" ) if "norm2" in name: __lowerCAmelCase : Dict = name.replace("""norm2""" , """layernorm_after""" ) if "mlp.fc1" in name: __lowerCAmelCase : List[str] = name.replace("""mlp.fc1""" , """intermediate.dense""" ) if "mlp.fc2" in name: __lowerCAmelCase : str = name.replace("""mlp.fc2""" , """output.dense""" ) if name == "norm.weight": __lowerCAmelCase : Dict = """layernorm.weight""" if name == "norm.bias": __lowerCAmelCase : Optional[int] = """layernorm.bias""" if "head" in name: __lowerCAmelCase : int = name.replace("""head""" , """classifier""" ) else: __lowerCAmelCase : List[str] = """swin.""" + name return name def snake_case_ (__A : List[Any] , __A : str ) -> int: for key in orig_state_dict.copy().keys(): __lowerCAmelCase : Tuple = orig_state_dict.pop(__A ) if "mask" in key: continue elif "qkv" in key: __lowerCAmelCase : Any = key.split(""".""" ) __lowerCAmelCase : Union[str, Any] = int(key_split[1] ) __lowerCAmelCase : Optional[Any] = int(key_split[3] ) __lowerCAmelCase : Optional[int] = model.swin.encoder.layers[layer_num].blocks[block_num].attention.self.all_head_size if "weight" in key: __lowerCAmelCase : List[str] = val[:dim, :] __lowerCAmelCase : List[Any] = val[ dim : dim * 2, : ] __lowerCAmelCase : str = val[-dim:, :] else: __lowerCAmelCase : str = val[ :dim ] __lowerCAmelCase : int = val[ dim : dim * 2 ] __lowerCAmelCase : int = val[ -dim: ] else: __lowerCAmelCase : Tuple = val return orig_state_dict def snake_case_ (__A : Union[str, Any] , __A : int ) -> Any: __lowerCAmelCase : List[Any] = timm.create_model(__A , pretrained=__A ) timm_model.eval() __lowerCAmelCase : str = get_swin_config(__A ) __lowerCAmelCase : Any = SwinForImageClassification(__A ) model.eval() __lowerCAmelCase : str = convert_state_dict(timm_model.state_dict() , __A ) model.load_state_dict(__A ) __lowerCAmelCase : str = """http://images.cocodataset.org/val2017/000000039769.jpg""" __lowerCAmelCase : Any = AutoImageProcessor.from_pretrained("""microsoft/{}""".format(swin_name.replace("""_""" , """-""" ) ) ) __lowerCAmelCase : List[Any] = Image.open(requests.get(__A , stream=__A ).raw ) __lowerCAmelCase : List[str] = image_processor(images=__A , return_tensors="""pt""" ) __lowerCAmelCase : Tuple = timm_model(inputs["""pixel_values"""] ) __lowerCAmelCase : Dict = model(**__A ).logits assert torch.allclose(__A , __A , atol=1e-3 ) print(f'''Saving model {swin_name} to {pytorch_dump_folder_path}''' ) model.save_pretrained(__A ) print(f'''Saving image processor to {pytorch_dump_folder_path}''' ) image_processor.save_pretrained(__A ) if __name__ == "__main__": __UpperCAmelCase = argparse.ArgumentParser() # Required parameters parser.add_argument( """--swin_name""", default="""swin_tiny_patch4_window7_224""", type=str, help="""Name of the Swin timm model you'd like to convert.""", ) parser.add_argument( """--pytorch_dump_folder_path""", default=None, type=str, help="""Path to the output PyTorch model directory.""" ) __UpperCAmelCase = parser.parse_args() convert_swin_checkpoint(args.swin_name, args.pytorch_dump_folder_path)
139
0
def UpperCAmelCase ( a_ ) -> str: """simple docstring""" return " ".join(input_str.split()[::-1] ) if __name__ == "__main__": import doctest doctest.testmod()
15
from __future__ import annotations import unittest from transformers import BlenderbotConfig, BlenderbotTokenizer, is_tf_available from transformers.testing_utils import 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, TFBlenderbotForConditionalGeneration, TFBlenderbotModel @require_tf class __snake_case : __lowerCamelCase : str = BlenderbotConfig __lowerCamelCase : Optional[Any] = {} __lowerCamelCase : Optional[int] = """gelu""" def __init__( self , snake_case__ , snake_case__=13 , snake_case__=7 , snake_case__=True , snake_case__=False , snake_case__=99 , snake_case__=32 , snake_case__=2 , snake_case__=4 , snake_case__=37 , snake_case__=0.1 , snake_case__=0.1 , snake_case__=20 , snake_case__=2 , snake_case__=1 , snake_case__=0 , ) -> Optional[Any]: '''simple docstring''' UpperCAmelCase : Union[str, Any] =parent UpperCAmelCase : Optional[int] =batch_size UpperCAmelCase : Dict =seq_length UpperCAmelCase : Optional[Any] =is_training UpperCAmelCase : List[str] =use_labels UpperCAmelCase : List[Any] =vocab_size UpperCAmelCase : Optional[int] =hidden_size UpperCAmelCase : Tuple =num_hidden_layers UpperCAmelCase : Any =num_attention_heads UpperCAmelCase : Optional[int] =intermediate_size UpperCAmelCase : str =hidden_dropout_prob UpperCAmelCase : Optional[int] =attention_probs_dropout_prob UpperCAmelCase : str =max_position_embeddings UpperCAmelCase : List[Any] =eos_token_id UpperCAmelCase : Optional[int] =pad_token_id UpperCAmelCase : Tuple =bos_token_id def UpperCAmelCase__ ( self ) -> Optional[Any]: '''simple docstring''' UpperCAmelCase : List[Any] =ids_tensor([self.batch_size, self.seq_length - 1] , self.vocab_size ) UpperCAmelCase : List[Any] =tf.expand_dims(tf.constant([self.eos_token_id] * self.batch_size ) , 1 ) UpperCAmelCase : Tuple =tf.concat([input_ids, eos_tensor] , axis=1 ) UpperCAmelCase : str =ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) UpperCAmelCase : Optional[Any] =self.config_cls( vocab_size=self.vocab_size , d_model=self.hidden_size , encoder_layers=self.num_hidden_layers , decoder_layers=self.num_hidden_layers , encoder_attention_heads=self.num_attention_heads , decoder_attention_heads=self.num_attention_heads , encoder_ffn_dim=self.intermediate_size , decoder_ffn_dim=self.intermediate_size , dropout=self.hidden_dropout_prob , attention_dropout=self.attention_probs_dropout_prob , max_position_embeddings=self.max_position_embeddings , eos_token_ids=[2] , bos_token_id=self.bos_token_id , pad_token_id=self.pad_token_id , decoder_start_token_id=self.pad_token_id , **self.config_updates , ) UpperCAmelCase : List[str] =prepare_blenderbot_inputs_dict(snake_case__ , snake_case__ , snake_case__ ) return config, inputs_dict def UpperCAmelCase__ ( self , snake_case__ , snake_case__ ) -> int: '''simple docstring''' UpperCAmelCase : Union[str, Any] =TFBlenderbotModel(config=snake_case__ ).get_decoder() UpperCAmelCase : Any =inputs_dict['''input_ids'''] UpperCAmelCase : str =input_ids[:1, :] UpperCAmelCase : Tuple =inputs_dict['''attention_mask'''][:1, :] UpperCAmelCase : Tuple =inputs_dict['''head_mask'''] UpperCAmelCase : List[Any] =1 # first forward pass UpperCAmelCase : List[str] =model(snake_case__ , attention_mask=snake_case__ , head_mask=snake_case__ , use_cache=snake_case__ ) UpperCAmelCase , UpperCAmelCase : str =outputs.to_tuple() # create hypothetical next token and extent to next_input_ids UpperCAmelCase : Union[str, Any] =ids_tensor((self.batch_size, 3) , config.vocab_size ) UpperCAmelCase : List[Any] =tf.cast(ids_tensor((self.batch_size, 3) , 2 ) , tf.inta ) # append to next input_ids and UpperCAmelCase : Tuple =tf.concat([input_ids, next_tokens] , axis=-1 ) UpperCAmelCase : int =tf.concat([attention_mask, next_attn_mask] , axis=-1 ) UpperCAmelCase : Optional[int] =model(snake_case__ , attention_mask=snake_case__ )[0] UpperCAmelCase : str =model(snake_case__ , attention_mask=snake_case__ , past_key_values=snake_case__ )[0] self.parent.assertEqual(next_tokens.shape[1] , output_from_past.shape[1] ) # select random slice UpperCAmelCase : List[Any] =int(ids_tensor((1,) , output_from_past.shape[-1] ) ) UpperCAmelCase : List[Any] =output_from_no_past[:, -3:, random_slice_idx] UpperCAmelCase : Dict =output_from_past[:, :, random_slice_idx] # test that outputs are equal for slice tf.debugging.assert_near(snake_case__ , snake_case__ , rtol=1e-3 ) def lowerCAmelCase_ ( __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase=None , __lowerCAmelCase=None , __lowerCAmelCase=None , __lowerCAmelCase=None , __lowerCAmelCase=None , )-> str: '''simple docstring''' if attention_mask is None: UpperCAmelCase : int =tf.cast(tf.math.not_equal(__lowerCAmelCase , config.pad_token_id ) , tf.inta ) if decoder_attention_mask is None: UpperCAmelCase : 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: UpperCAmelCase : str =tf.ones((config.encoder_layers, config.encoder_attention_heads) ) if decoder_head_mask is None: UpperCAmelCase : Union[str, Any] =tf.ones((config.decoder_layers, config.decoder_attention_heads) ) if cross_attn_head_mask is None: UpperCAmelCase : 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 __snake_case ( lowerCamelCase__ , lowerCamelCase__ , unittest.TestCase ): __lowerCamelCase : List[str] = (TFBlenderbotForConditionalGeneration, TFBlenderbotModel) if is_tf_available() else () __lowerCamelCase : Dict = (TFBlenderbotForConditionalGeneration,) if is_tf_available() else () __lowerCamelCase : Dict = ( { """conversational""": TFBlenderbotForConditionalGeneration, """feature-extraction""": TFBlenderbotModel, """summarization""": TFBlenderbotForConditionalGeneration, """text2text-generation""": TFBlenderbotForConditionalGeneration, """translation""": TFBlenderbotForConditionalGeneration, } if is_tf_available() else {} ) __lowerCamelCase : Union[str, Any] = True __lowerCamelCase : Union[str, Any] = False __lowerCamelCase : Union[str, Any] = False def UpperCAmelCase__ ( self ) -> Dict: '''simple docstring''' UpperCAmelCase : List[str] =TFBlenderbotModelTester(self ) UpperCAmelCase : List[Any] =ConfigTester(self , config_class=snake_case__ ) def UpperCAmelCase__ ( self ) -> Optional[Any]: '''simple docstring''' self.config_tester.run_common_tests() def UpperCAmelCase__ ( self ) -> List[Any]: '''simple docstring''' UpperCAmelCase : int =self.model_tester.prepare_config_and_inputs_for_common() self.model_tester.check_decoder_model_past_large_inputs(*snake_case__ ) @require_tokenizers @require_tf class __snake_case ( unittest.TestCase ): __lowerCamelCase : List[str] = ["""My friends are cool but they eat too many carbs."""] __lowerCamelCase : Dict = """facebook/blenderbot-400M-distill""" @cached_property def UpperCAmelCase__ ( self ) -> int: '''simple docstring''' return BlenderbotTokenizer.from_pretrained(self.model_name ) @cached_property def UpperCAmelCase__ ( self ) -> int: '''simple docstring''' UpperCAmelCase : int =TFAutoModelForSeqaSeqLM.from_pretrained(self.model_name ) return model @slow def UpperCAmelCase__ ( self ) -> Any: '''simple docstring''' UpperCAmelCase : Optional[int] =self.tokenizer(self.src_text , return_tensors='''tf''' ) UpperCAmelCase : Optional[int] =self.model.generate( model_inputs.input_ids , ) UpperCAmelCase : str =self.tokenizer.batch_decode(generated_ids.numpy() , skip_special_tokens=snake_case__ )[0] assert ( generated_words == " That's unfortunate. Are they trying to lose weight or are they just trying to be healthier?" )
348
0
"""simple docstring""" import numpy as np from matplotlib import pyplot as plt from sklearn.datasets import load_iris from sklearn.metrics import ConfusionMatrixDisplay from sklearn.model_selection import train_test_split from xgboost import XGBClassifier def _SCREAMING_SNAKE_CASE (__lowerCAmelCase ) -> tuple: '''simple docstring''' return (data["data"], data["target"]) def _SCREAMING_SNAKE_CASE (__lowerCAmelCase , __lowerCAmelCase ) -> XGBClassifier: '''simple docstring''' lowercase_ = XGBClassifier() classifier.fit(__lowerCAmelCase , __lowerCAmelCase ) return classifier def _SCREAMING_SNAKE_CASE () -> None: '''simple docstring''' lowercase_ = load_iris() lowercase_ , lowercase_ = data_handling(__lowerCAmelCase ) lowercase_ , lowercase_ , lowercase_ , lowercase_ = train_test_split( __lowerCAmelCase , __lowerCAmelCase , test_size=0.25 ) lowercase_ = iris["""target_names"""] # Create an XGBoost Classifier from the training data lowercase_ = xgboost(__lowerCAmelCase , __lowerCAmelCase ) # Display the confusion matrix of the classifier with both training and test sets ConfusionMatrixDisplay.from_estimator( __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , display_labels=__lowerCAmelCase , cmap="""Blues""" , normalize="""true""" , ) plt.title("""Normalized Confusion Matrix - IRIS Dataset""" ) plt.show() if __name__ == "__main__": import doctest doctest.testmod(verbose=True) main()
368
"""simple docstring""" from random import randint from tempfile import TemporaryFile import numpy as np def _SCREAMING_SNAKE_CASE (__lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase ) -> Tuple: '''simple docstring''' lowercase_ = 0 if start < end: lowercase_ = randint(__lowerCAmelCase , __lowerCAmelCase ) lowercase_ = a[end] lowercase_ = a[pivot] lowercase_ = temp lowercase_ , lowercase_ = _in_place_partition(__lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase ) count += _in_place_quick_sort(__lowerCAmelCase , __lowerCAmelCase , p - 1 ) count += _in_place_quick_sort(__lowerCAmelCase , p + 1 , __lowerCAmelCase ) return count def _SCREAMING_SNAKE_CASE (__lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase ) -> int: '''simple docstring''' lowercase_ = 0 lowercase_ = randint(__lowerCAmelCase , __lowerCAmelCase ) lowercase_ = a[end] lowercase_ = a[pivot] lowercase_ = temp lowercase_ = start - 1 for index in range(__lowerCAmelCase , __lowerCAmelCase ): count += 1 if a[index] < a[end]: # check if current val is less than pivot value lowercase_ = new_pivot_index + 1 lowercase_ = a[new_pivot_index] lowercase_ = a[index] lowercase_ = temp lowercase_ = a[new_pivot_index + 1] lowercase_ = a[end] lowercase_ = temp return new_pivot_index + 1, count UpperCAmelCase : Union[str, Any] = TemporaryFile() UpperCAmelCase : Optional[int] = 100 # 1000 elements are to be sorted UpperCAmelCase , UpperCAmelCase : List[str] = 0, 1 # mean and standard deviation UpperCAmelCase : Optional[Any] = np.random.normal(mu, sigma, p) np.save(outfile, X) print("The array is") print(X) outfile.seek(0) # using the same array UpperCAmelCase : List[str] = np.load(outfile) UpperCAmelCase : List[Any] = len(M) - 1 UpperCAmelCase : Optional[int] = _in_place_quick_sort(M, 0, r) print( "No of Comparisons for 100 elements selected from a standard normal distribution" "is :" ) print(z)
313
0
from abc import ABC, abstractmethod from argparse import ArgumentParser class A__(_a ): """simple docstring""" @staticmethod @abstractmethod def UpperCamelCase__ ( _lowercase ) -> Optional[int]: raise NotImplementedError() @abstractmethod def UpperCamelCase__ ( self ) -> List[Any]: raise NotImplementedError()
248
'''simple docstring''' from . import ( albert, align, altclip, audio_spectrogram_transformer, auto, autoformer, bark, bart, barthez, bartpho, beit, bert, bert_generation, bert_japanese, bertweet, big_bird, bigbird_pegasus, biogpt, bit, blenderbot, blenderbot_small, blip, blip_a, bloom, bridgetower, byta, camembert, canine, chinese_clip, clap, clip, clipseg, codegen, conditional_detr, convbert, convnext, convnextva, cpm, cpmant, ctrl, cvt, dataavec, deberta, deberta_va, decision_transformer, deformable_detr, deit, deprecated, deta, detr, dialogpt, dinat, distilbert, dit, donut, dpr, dpt, efficientformer, efficientnet, electra, encodec, encoder_decoder, ernie, ernie_m, esm, falcon, flaubert, flava, fnet, focalnet, fsmt, funnel, git, glpn, gpta, gpt_bigcode, gpt_neo, gpt_neox, gpt_neox_japanese, gpt_swa, gptj, gptsan_japanese, graphormer, groupvit, herbert, hubert, ibert, imagegpt, informer, instructblip, jukebox, layoutlm, layoutlmva, layoutlmva, layoutxlm, led, levit, lilt, llama, longformer, longta, luke, lxmert, mam_aaa, marian, markuplm, maskaformer, maskformer, mbart, mbartaa, mega, megatron_bert, megatron_gpta, mgp_str, mluke, mobilebert, mobilenet_va, mobilenet_va, mobilevit, mobilevitva, mpnet, mra, mta, musicgen, mvp, nat, nezha, nllb, nllb_moe, nystromformer, oneformer, open_llama, openai, opt, owlvit, pegasus, pegasus_x, perceiver, phobert, pixastruct, plbart, poolformer, prophetnet, qdqbert, rag, realm, reformer, regnet, rembert, resnet, roberta, roberta_prelayernorm, roc_bert, roformer, rwkv, sam, segformer, sew, sew_d, speech_encoder_decoder, speech_to_text, speech_to_text_a, speechta, splinter, squeezebert, swiftformer, swin, swinasr, swinva, switch_transformers, ta, table_transformer, tapas, time_series_transformer, timesformer, timm_backbone, transfo_xl, trocr, tvlt, umta, unispeech, unispeech_sat, upernet, videomae, vilt, vision_encoder_decoder, vision_text_dual_encoder, visual_bert, vit, vit_hybrid, vit_mae, vit_msn, vivit, wavaveca, wavaveca_conformer, wavaveca_phoneme, wavaveca_with_lm, wavlm, whisper, x_clip, xglm, xlm, xlm_prophetnet, xlm_roberta, xlm_roberta_xl, xlnet, xmod, yolos, yoso, )
28
0
"""simple docstring""" from __future__ import annotations import inspect import unittest import numpy as np from transformers import ResNetConfig from transformers.testing_utils import require_tf, require_vision, slow from transformers.utils import cached_property, is_tf_available, is_vision_available from ...test_configuration_common import ConfigTester from ...test_modeling_tf_common import TFModelTesterMixin, floats_tensor, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_tf_available(): import tensorflow as tf from transformers import TFResNetForImageClassification, TFResNetModel from transformers.models.resnet.modeling_tf_resnet import TF_RESNET_PRETRAINED_MODEL_ARCHIVE_LIST if is_vision_available(): from PIL import Image from transformers import AutoImageProcessor class __SCREAMING_SNAKE_CASE : def __init__( self , _UpperCamelCase , _UpperCamelCase=3 , _UpperCamelCase=32 , _UpperCamelCase=3 , _UpperCamelCase=10 , _UpperCamelCase=[10, 20, 30, 40] , _UpperCamelCase=[1, 1, 2, 1] , _UpperCamelCase=True , _UpperCamelCase=True , _UpperCamelCase="relu" , _UpperCamelCase=3 , _UpperCamelCase=None , ): """simple docstring""" lowerCAmelCase__ = parent lowerCAmelCase__ = batch_size lowerCAmelCase__ = image_size lowerCAmelCase__ = num_channels lowerCAmelCase__ = embeddings_size lowerCAmelCase__ = hidden_sizes lowerCAmelCase__ = depths lowerCAmelCase__ = is_training lowerCAmelCase__ = use_labels lowerCAmelCase__ = hidden_act lowerCAmelCase__ = num_labels lowerCAmelCase__ = scope lowerCAmelCase__ = len(_UpperCamelCase ) def UpperCamelCase__ ( self ): """simple docstring""" lowerCAmelCase__ = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) lowerCAmelCase__ = None if self.use_labels: lowerCAmelCase__ = ids_tensor([self.batch_size] , self.num_labels ) lowerCAmelCase__ = self.get_config() return config, pixel_values, labels def UpperCamelCase__ ( self ): """simple docstring""" return ResNetConfig( num_channels=self.num_channels , embeddings_size=self.embeddings_size , hidden_sizes=self.hidden_sizes , depths=self.depths , hidden_act=self.hidden_act , num_labels=self.num_labels , image_size=self.image_size , ) def UpperCamelCase__ ( self , _UpperCamelCase , _UpperCamelCase , _UpperCamelCase ): """simple docstring""" lowerCAmelCase__ = TFResNetModel(config=_UpperCamelCase ) lowerCAmelCase__ = model(_UpperCamelCase ) # expected last hidden states: B, C, H // 32, W // 32 self.parent.assertEqual( result.last_hidden_state.shape , (self.batch_size, self.hidden_sizes[-1], self.image_size // 32, self.image_size // 32) , ) def UpperCamelCase__ ( self , _UpperCamelCase , _UpperCamelCase , _UpperCamelCase ): """simple docstring""" lowerCAmelCase__ = self.num_labels lowerCAmelCase__ = TFResNetForImageClassification(_UpperCamelCase ) lowerCAmelCase__ = model(_UpperCamelCase , labels=_UpperCamelCase ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_labels) ) def UpperCamelCase__ ( self ): """simple docstring""" lowerCAmelCase__ = self.prepare_config_and_inputs() lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ = config_and_inputs lowerCAmelCase__ = {'pixel_values': pixel_values} return config, inputs_dict @require_tf class __SCREAMING_SNAKE_CASE ( __lowercase , __lowercase , unittest.TestCase): _SCREAMING_SNAKE_CASE : Any = (TFResNetModel, TFResNetForImageClassification) if is_tf_available() else () _SCREAMING_SNAKE_CASE : List[str] = ( {'''feature-extraction''': TFResNetModel, '''image-classification''': TFResNetForImageClassification} if is_tf_available() else {} ) _SCREAMING_SNAKE_CASE : Union[str, Any] = False _SCREAMING_SNAKE_CASE : int = False _SCREAMING_SNAKE_CASE : Optional[int] = False _SCREAMING_SNAKE_CASE : List[str] = False _SCREAMING_SNAKE_CASE : Optional[Any] = False def UpperCamelCase__ ( self ): """simple docstring""" lowerCAmelCase__ = TFResNetModelTester(self ) lowerCAmelCase__ = ConfigTester(self , config_class=_UpperCamelCase , has_text_modality=_UpperCamelCase ) def UpperCamelCase__ ( self ): """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 ): """simple docstring""" return @unittest.skip(reason='ResNet does not use inputs_embeds' ) def UpperCamelCase__ ( self ): """simple docstring""" pass @unittest.skip(reason='ResNet does not support input and output embeddings' ) def UpperCamelCase__ ( self ): """simple docstring""" pass def UpperCamelCase__ ( self ): """simple docstring""" lowerCAmelCase__ , lowerCAmelCase__ = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: lowerCAmelCase__ = model_class(_UpperCamelCase ) lowerCAmelCase__ = inspect.signature(model.call ) # signature.parameters is an OrderedDict => so arg_names order is deterministic lowerCAmelCase__ = [*signature.parameters.keys()] lowerCAmelCase__ = ['pixel_values'] self.assertListEqual(arg_names[:1] , _UpperCamelCase ) def UpperCamelCase__ ( self ): """simple docstring""" lowerCAmelCase__ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*_UpperCamelCase ) def UpperCamelCase__ ( self ): """simple docstring""" def check_hidden_states_output(_UpperCamelCase , _UpperCamelCase , _UpperCamelCase ): lowerCAmelCase__ = model_class(_UpperCamelCase ) lowerCAmelCase__ = model(**self._prepare_for_class(_UpperCamelCase , _UpperCamelCase ) ) lowerCAmelCase__ = outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states lowerCAmelCase__ = self.model_tester.num_stages self.assertEqual(len(_UpperCamelCase ) , expected_num_stages + 1 ) # ResNet's feature maps are of shape (batch_size, num_channels, height, width) self.assertListEqual( list(hidden_states[0].shape[-2:] ) , [self.model_tester.image_size // 4, self.model_tester.image_size // 4] , ) lowerCAmelCase__ , lowerCAmelCase__ = self.model_tester.prepare_config_and_inputs_for_common() lowerCAmelCase__ = ['basic', 'bottleneck'] for model_class in self.all_model_classes: for layer_type in layers_type: lowerCAmelCase__ = layer_type lowerCAmelCase__ = True check_hidden_states_output(_UpperCamelCase , _UpperCamelCase , _UpperCamelCase ) # check that output_hidden_states also work using config del inputs_dict["output_hidden_states"] lowerCAmelCase__ = True check_hidden_states_output(_UpperCamelCase , _UpperCamelCase , _UpperCamelCase ) def UpperCamelCase__ ( self ): """simple docstring""" lowerCAmelCase__ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_image_classification(*_UpperCamelCase ) @slow def UpperCamelCase__ ( self ): """simple docstring""" for model_name in TF_RESNET_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: lowerCAmelCase__ = TFResNetModel.from_pretrained(_UpperCamelCase ) self.assertIsNotNone(_UpperCamelCase ) def _UpperCamelCase ( ) -> List[str]: """simple docstring""" lowerCAmelCase__ = Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png' ) return image @require_tf @require_vision class __SCREAMING_SNAKE_CASE ( unittest.TestCase): @cached_property def UpperCamelCase__ ( self ): """simple docstring""" return ( AutoImageProcessor.from_pretrained(TF_RESNET_PRETRAINED_MODEL_ARCHIVE_LIST[0] ) if is_vision_available() else None ) @slow def UpperCamelCase__ ( self ): """simple docstring""" lowerCAmelCase__ = TFResNetForImageClassification.from_pretrained(TF_RESNET_PRETRAINED_MODEL_ARCHIVE_LIST[0] ) lowerCAmelCase__ = self.default_image_processor lowerCAmelCase__ = prepare_img() lowerCAmelCase__ = image_processor(images=_UpperCamelCase , return_tensors='tf' ) # forward pass lowerCAmelCase__ = model(**_UpperCamelCase ) # verify the logits lowerCAmelCase__ = tf.TensorShape((1, 10_00) ) self.assertEqual(outputs.logits.shape , _UpperCamelCase ) lowerCAmelCase__ = tf.constant([-11.10_69, -9.78_77, -8.37_77] ) self.assertTrue(np.allclose(outputs.logits[0, :3].numpy() , _UpperCamelCase , atol=1E-4 ) )
357
from typing import TYPE_CHECKING # rely on isort to merge the imports from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available __snake_case : Dict = {"""configuration_focalnet""": ["""FOCALNET_PRETRAINED_CONFIG_ARCHIVE_MAP""", """FocalNetConfig"""]} try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __snake_case : str = [ """FOCALNET_PRETRAINED_MODEL_ARCHIVE_LIST""", """FocalNetForImageClassification""", """FocalNetForMaskedImageModeling""", """FocalNetBackbone""", """FocalNetModel""", """FocalNetPreTrainedModel""", ] if TYPE_CHECKING: from .configuration_focalnet import FOCALNET_PRETRAINED_CONFIG_ARCHIVE_MAP, FocalNetConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_focalnet import ( FOCALNET_PRETRAINED_MODEL_ARCHIVE_LIST, FocalNetBackbone, FocalNetForImageClassification, FocalNetForMaskedImageModeling, FocalNetModel, FocalNetPreTrainedModel, ) else: import sys __snake_case : List[Any] = _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)
122
0
from collections import OrderedDict from typing import Mapping from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging __lowerCAmelCase : Optional[int] = logging.get_logger(__name__) __lowerCAmelCase : Tuple = { 'distilbert-base-uncased': 'https://huggingface.co/distilbert-base-uncased/resolve/main/config.json', 'distilbert-base-uncased-distilled-squad': ( 'https://huggingface.co/distilbert-base-uncased-distilled-squad/resolve/main/config.json' ), 'distilbert-base-cased': 'https://huggingface.co/distilbert-base-cased/resolve/main/config.json', 'distilbert-base-cased-distilled-squad': ( 'https://huggingface.co/distilbert-base-cased-distilled-squad/resolve/main/config.json' ), 'distilbert-base-german-cased': 'https://huggingface.co/distilbert-base-german-cased/resolve/main/config.json', 'distilbert-base-multilingual-cased': ( 'https://huggingface.co/distilbert-base-multilingual-cased/resolve/main/config.json' ), 'distilbert-base-uncased-finetuned-sst-2-english': ( 'https://huggingface.co/distilbert-base-uncased-finetuned-sst-2-english/resolve/main/config.json' ), } class snake_case__ (_UpperCamelCase ): """simple docstring""" SCREAMING_SNAKE_CASE_ : Optional[int] = """distilbert""" SCREAMING_SNAKE_CASE_ : Optional[Any] = { """hidden_size""": """dim""", """num_attention_heads""": """n_heads""", """num_hidden_layers""": """n_layers""", } def __init__( self : int , __lowerCamelCase : Tuple=3_05_22 , __lowerCamelCase : Any=5_12 , __lowerCamelCase : List[str]=False , __lowerCamelCase : List[Any]=6 , __lowerCamelCase : List[str]=12 , __lowerCamelCase : Optional[Any]=7_68 , __lowerCamelCase : Any=4 * 7_68 , __lowerCamelCase : Tuple=0.1 , __lowerCamelCase : str=0.1 , __lowerCamelCase : str="gelu" , __lowerCamelCase : str=0.02 , __lowerCamelCase : str=0.1 , __lowerCamelCase : Any=0.2 , __lowerCamelCase : Any=0 , **__lowerCamelCase : Dict , ) -> int: a = vocab_size a = max_position_embeddings a = sinusoidal_pos_embds a = n_layers a = n_heads a = dim a = hidden_dim a = dropout a = attention_dropout a = activation a = initializer_range a = qa_dropout a = seq_classif_dropout super().__init__(**__lowerCamelCase , pad_token_id=__lowerCamelCase ) class snake_case__ (_UpperCamelCase ): """simple docstring""" @property def __UpperCAmelCase ( self : Optional[Any] ) -> Mapping[str, Mapping[int, str]]: if self.task == "multiple-choice": a = {0: "batch", 1: "choice", 2: "sequence"} else: a = {0: "batch", 1: "sequence"} return OrderedDict( [ ("input_ids", dynamic_axis), ("attention_mask", dynamic_axis), ] )
107
def _UpperCamelCase ( snake_case__ ) -> int: __UpperCAmelCase : list[list[int]] = [[0 for _ in range(snake_case__ )] for _ in range(m + 1 )] for i in range(m + 1 ): __UpperCAmelCase : Optional[int] = 1 for n in range(m + 1 ): for k in range(1, snake_case__ ): memo[n][k] += memo[n][k - 1] if n - k > 0: memo[n][k] += memo[n - k - 1][k] return memo[m][m - 1] if __name__ == "__main__": import sys if len(sys.argv) == 1: try: _snake_case = int(input('''Enter a number: ''').strip()) print(partition(n)) except ValueError: print('''Please enter a number.''') else: try: _snake_case = int(sys.argv[1]) print(partition(n)) except ValueError: print('''Please pass a number.''')
157
0
'''simple docstring''' from math import pow, sqrt def lowercase__ ( *__UpperCamelCase )-> bool: UpperCamelCase = len(__UpperCamelCase ) > 0 and all(value > 0.0 for value in values ) return result def lowercase__ ( __UpperCamelCase , __UpperCamelCase )-> float | ValueError: return ( round(sqrt(molar_mass_a / molar_mass_a ) , 6 ) if validate(__UpperCamelCase , __UpperCamelCase ) else ValueError("""Input Error: Molar mass values must greater than 0.""" ) ) def lowercase__ ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase )-> float | ValueError: return ( round(effusion_rate * sqrt(molar_mass_a / molar_mass_a ) , 6 ) if validate(__UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) else ValueError( """Input Error: Molar mass and effusion rate values must greater than 0.""" ) ) def lowercase__ ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase )-> float | ValueError: return ( round(effusion_rate / sqrt(molar_mass_a / molar_mass_a ) , 6 ) if validate(__UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) else ValueError( """Input Error: Molar mass and effusion rate values must greater than 0.""" ) ) def lowercase__ ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase )-> float | ValueError: return ( round(molar_mass / pow(effusion_rate_a / effusion_rate_a , 2 ) , 6 ) if validate(__UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) else ValueError( """Input Error: Molar mass and effusion rate values must greater than 0.""" ) ) def lowercase__ ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase )-> float | ValueError: return ( round(pow(effusion_rate_a / effusion_rate_a , 2 ) / molar_mass , 6 ) if validate(__UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) else ValueError( """Input Error: Molar mass and effusion rate values must greater than 0.""" ) )
183
'''simple docstring''' import re import string import numpy as np import datasets SCREAMING_SNAKE_CASE__ = '\nReturns the rate at which the input predicted strings exactly match their references, ignoring any strings input as part of the regexes_to_ignore list.\n' SCREAMING_SNAKE_CASE__ = '\nArgs:\n predictions: List of predicted texts.\n references: List of reference texts.\n regexes_to_ignore: List, defaults to None. Regex expressions of characters to\n ignore when calculating the exact matches. Note: these regexes are removed\n from the input data before the changes based on the options below (e.g. ignore_case,\n ignore_punctuation, ignore_numbers) are applied.\n ignore_case: Boolean, defaults to False. If true, turns everything\n to lowercase so that capitalization differences are ignored.\n ignore_punctuation: Boolean, defaults to False. If true, removes all punctuation before\n comparing predictions and references.\n ignore_numbers: Boolean, defaults to False. If true, removes all punctuation before\n comparing predictions and references.\nReturns:\n exact_match: Dictionary containing exact_match rate. Possible values are between 0.0 and 100.0, inclusive.\nExamples:\n >>> exact_match = datasets.load_metric("exact_match")\n >>> refs = ["the cat", "theater", "YELLING", "agent007"]\n >>> preds = ["cat?", "theater", "yelling", "agent"]\n >>> results = exact_match.compute(references=refs, predictions=preds)\n >>> print(round(results["exact_match"], 1))\n 25.0\n\n >>> exact_match = datasets.load_metric("exact_match")\n >>> refs = ["the cat", "theater", "YELLING", "agent007"]\n >>> preds = ["cat?", "theater", "yelling", "agent"]\n >>> results = exact_match.compute(references=refs, predictions=preds, regexes_to_ignore=["the ", "yell"], ignore_case=True, ignore_punctuation=True)\n >>> print(round(results["exact_match"], 1))\n 50.0\n\n\n >>> exact_match = datasets.load_metric("exact_match")\n >>> refs = ["the cat", "theater", "YELLING", "agent007"]\n >>> preds = ["cat?", "theater", "yelling", "agent"]\n >>> results = exact_match.compute(references=refs, predictions=preds, regexes_to_ignore=["the ", "yell", "YELL"], ignore_case=True, ignore_punctuation=True)\n >>> print(round(results["exact_match"], 1))\n 75.0\n\n >>> exact_match = datasets.load_metric("exact_match")\n >>> refs = ["the cat", "theater", "YELLING", "agent007"]\n >>> preds = ["cat?", "theater", "yelling", "agent"]\n >>> results = exact_match.compute(references=refs, predictions=preds, regexes_to_ignore=["the ", "yell", "YELL"], ignore_case=True, ignore_punctuation=True, ignore_numbers=True)\n >>> print(round(results["exact_match"], 1))\n 100.0\n\n >>> exact_match = datasets.load_metric("exact_match")\n >>> refs = ["The cat sat on the mat.", "Theaters are great.", "It\'s like comparing oranges and apples."]\n >>> preds = ["The cat sat on the mat?", "Theaters are great.", "It\'s like comparing apples and oranges."]\n >>> results = exact_match.compute(references=refs, predictions=preds)\n >>> print(round(results["exact_match"], 1))\n 33.3\n\n' SCREAMING_SNAKE_CASE__ = '\n' @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class a_ ( datasets.Metric ): def A__ ( self ) -> Optional[Any]: """simple docstring""" return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { """predictions""": datasets.Value("""string""" , id="""sequence""" ), """references""": datasets.Value("""string""" , id="""sequence""" ), } ) , reference_urls=[] , ) def A__ ( self , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE=None , _SCREAMING_SNAKE_CASE=False , _SCREAMING_SNAKE_CASE=False , _SCREAMING_SNAKE_CASE=False , ) -> List[Any]: """simple docstring""" if regexes_to_ignore is not None: for s in regexes_to_ignore: UpperCamelCase = np.array([re.sub(_SCREAMING_SNAKE_CASE , """""" , _SCREAMING_SNAKE_CASE ) for x in predictions] ) UpperCamelCase = np.array([re.sub(_SCREAMING_SNAKE_CASE , """""" , _SCREAMING_SNAKE_CASE ) for x in references] ) else: UpperCamelCase = np.asarray(_SCREAMING_SNAKE_CASE ) UpperCamelCase = np.asarray(_SCREAMING_SNAKE_CASE ) if ignore_case: UpperCamelCase = np.char.lower(_SCREAMING_SNAKE_CASE ) UpperCamelCase = np.char.lower(_SCREAMING_SNAKE_CASE ) if ignore_punctuation: UpperCamelCase = string.punctuation.maketrans("""""" , """""" , string.punctuation ) UpperCamelCase = np.char.translate(_SCREAMING_SNAKE_CASE , table=_SCREAMING_SNAKE_CASE ) UpperCamelCase = np.char.translate(_SCREAMING_SNAKE_CASE , table=_SCREAMING_SNAKE_CASE ) if ignore_numbers: UpperCamelCase = string.digits.maketrans("""""" , """""" , string.digits ) UpperCamelCase = np.char.translate(_SCREAMING_SNAKE_CASE , table=_SCREAMING_SNAKE_CASE ) UpperCamelCase = np.char.translate(_SCREAMING_SNAKE_CASE , table=_SCREAMING_SNAKE_CASE ) UpperCamelCase = predictions == references return {"exact_match": np.mean(_SCREAMING_SNAKE_CASE ) * 100}
183
1
import unittest from transformers import AlbertConfig, is_torch_available from transformers.models.auto import get_values from transformers.testing_utils import require_torch, slow, torch_device from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, ids_tensor, random_attention_mask from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from transformers import ( MODEL_FOR_PRETRAINING_MAPPING, AlbertForMaskedLM, AlbertForMultipleChoice, AlbertForPreTraining, AlbertForQuestionAnswering, AlbertForSequenceClassification, AlbertForTokenClassification, AlbertModel, ) from transformers.models.albert.modeling_albert import ALBERT_PRETRAINED_MODEL_ARCHIVE_LIST class _a : def __init__(self, SCREAMING_SNAKE_CASE_, SCREAMING_SNAKE_CASE_=13, SCREAMING_SNAKE_CASE_=7, SCREAMING_SNAKE_CASE_=True, SCREAMING_SNAKE_CASE_=True, SCREAMING_SNAKE_CASE_=True, SCREAMING_SNAKE_CASE_=True, SCREAMING_SNAKE_CASE_=99, SCREAMING_SNAKE_CASE_=16, SCREAMING_SNAKE_CASE_=36, SCREAMING_SNAKE_CASE_=6, SCREAMING_SNAKE_CASE_=6, SCREAMING_SNAKE_CASE_=6, SCREAMING_SNAKE_CASE_=37, SCREAMING_SNAKE_CASE_="gelu", SCREAMING_SNAKE_CASE_=0.1, SCREAMING_SNAKE_CASE_=0.1, SCREAMING_SNAKE_CASE_=512, SCREAMING_SNAKE_CASE_=16, SCREAMING_SNAKE_CASE_=2, SCREAMING_SNAKE_CASE_=0.0_2, SCREAMING_SNAKE_CASE_=3, SCREAMING_SNAKE_CASE_=4, SCREAMING_SNAKE_CASE_=None, ) -> str: UpperCAmelCase_: Optional[Any] = parent UpperCAmelCase_: Optional[int] = batch_size UpperCAmelCase_: Dict = seq_length UpperCAmelCase_: Dict = is_training UpperCAmelCase_: Optional[Any] = use_input_mask UpperCAmelCase_: Optional[int] = use_token_type_ids UpperCAmelCase_: Any = use_labels UpperCAmelCase_: str = vocab_size UpperCAmelCase_: Dict = embedding_size UpperCAmelCase_: Any = hidden_size UpperCAmelCase_: List[str] = num_hidden_layers UpperCAmelCase_: str = num_hidden_groups UpperCAmelCase_: Optional[Any] = num_attention_heads UpperCAmelCase_: Union[str, Any] = intermediate_size UpperCAmelCase_: Optional[int] = hidden_act UpperCAmelCase_: List[Any] = hidden_dropout_prob UpperCAmelCase_: Union[str, Any] = attention_probs_dropout_prob UpperCAmelCase_: Union[str, Any] = max_position_embeddings UpperCAmelCase_: int = type_vocab_size UpperCAmelCase_: Tuple = type_sequence_label_size UpperCAmelCase_: int = initializer_range UpperCAmelCase_: Any = num_labels UpperCAmelCase_: Optional[int] = num_choices UpperCAmelCase_: Union[str, Any] = scope def __snake_case (self ) -> List[str]: UpperCAmelCase_: str = ids_tensor([self.batch_size, self.seq_length], self.vocab_size ) UpperCAmelCase_: Optional[int] = None if self.use_input_mask: UpperCAmelCase_: str = random_attention_mask([self.batch_size, self.seq_length] ) UpperCAmelCase_: Any = None if self.use_token_type_ids: UpperCAmelCase_: int = ids_tensor([self.batch_size, self.seq_length], self.type_vocab_size ) UpperCAmelCase_: List[Any] = None UpperCAmelCase_: str = None UpperCAmelCase_: Optional[Any] = None if self.use_labels: UpperCAmelCase_: str = ids_tensor([self.batch_size], self.type_sequence_label_size ) UpperCAmelCase_: Optional[Any] = ids_tensor([self.batch_size, self.seq_length], self.num_labels ) UpperCAmelCase_: List[str] = ids_tensor([self.batch_size], self.num_choices ) UpperCAmelCase_: int = self.get_config() return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels def __snake_case (self ) -> Optional[int]: return AlbertConfig( vocab_size=self.vocab_size, hidden_size=self.hidden_size, num_hidden_layers=self.num_hidden_layers, num_attention_heads=self.num_attention_heads, intermediate_size=self.intermediate_size, hidden_act=self.hidden_act, hidden_dropout_prob=self.hidden_dropout_prob, attention_probs_dropout_prob=self.attention_probs_dropout_prob, max_position_embeddings=self.max_position_embeddings, type_vocab_size=self.type_vocab_size, initializer_range=self.initializer_range, num_hidden_groups=self.num_hidden_groups, ) def __snake_case (self, SCREAMING_SNAKE_CASE_, SCREAMING_SNAKE_CASE_, SCREAMING_SNAKE_CASE_, SCREAMING_SNAKE_CASE_, SCREAMING_SNAKE_CASE_, SCREAMING_SNAKE_CASE_, SCREAMING_SNAKE_CASE_ ) -> int: UpperCAmelCase_: Tuple = AlbertModel(config=SCREAMING_SNAKE_CASE__ ) model.to(SCREAMING_SNAKE_CASE__ ) model.eval() UpperCAmelCase_: Any = model(SCREAMING_SNAKE_CASE__, attention_mask=SCREAMING_SNAKE_CASE__, token_type_ids=SCREAMING_SNAKE_CASE__ ) UpperCAmelCase_: str = model(SCREAMING_SNAKE_CASE__, token_type_ids=SCREAMING_SNAKE_CASE__ ) UpperCAmelCase_: str = model(SCREAMING_SNAKE_CASE__ ) self.parent.assertEqual(result.last_hidden_state.shape, (self.batch_size, self.seq_length, self.hidden_size) ) self.parent.assertEqual(result.pooler_output.shape, (self.batch_size, self.hidden_size) ) def __snake_case (self, SCREAMING_SNAKE_CASE_, SCREAMING_SNAKE_CASE_, SCREAMING_SNAKE_CASE_, SCREAMING_SNAKE_CASE_, SCREAMING_SNAKE_CASE_, SCREAMING_SNAKE_CASE_, SCREAMING_SNAKE_CASE_ ) -> Dict: UpperCAmelCase_: Dict = AlbertForPreTraining(config=SCREAMING_SNAKE_CASE__ ) model.to(SCREAMING_SNAKE_CASE__ ) model.eval() UpperCAmelCase_: Optional[int] = model( SCREAMING_SNAKE_CASE__, attention_mask=SCREAMING_SNAKE_CASE__, token_type_ids=SCREAMING_SNAKE_CASE__, labels=SCREAMING_SNAKE_CASE__, sentence_order_label=SCREAMING_SNAKE_CASE__, ) self.parent.assertEqual(result.prediction_logits.shape, (self.batch_size, self.seq_length, self.vocab_size) ) self.parent.assertEqual(result.sop_logits.shape, (self.batch_size, config.num_labels) ) def __snake_case (self, SCREAMING_SNAKE_CASE_, SCREAMING_SNAKE_CASE_, SCREAMING_SNAKE_CASE_, SCREAMING_SNAKE_CASE_, SCREAMING_SNAKE_CASE_, SCREAMING_SNAKE_CASE_, SCREAMING_SNAKE_CASE_ ) -> Tuple: UpperCAmelCase_: List[Any] = AlbertForMaskedLM(config=SCREAMING_SNAKE_CASE__ ) model.to(SCREAMING_SNAKE_CASE__ ) model.eval() UpperCAmelCase_: Any = model(SCREAMING_SNAKE_CASE__, attention_mask=SCREAMING_SNAKE_CASE__, token_type_ids=SCREAMING_SNAKE_CASE__, labels=SCREAMING_SNAKE_CASE__ ) self.parent.assertEqual(result.logits.shape, (self.batch_size, self.seq_length, self.vocab_size) ) def __snake_case (self, SCREAMING_SNAKE_CASE_, SCREAMING_SNAKE_CASE_, SCREAMING_SNAKE_CASE_, SCREAMING_SNAKE_CASE_, SCREAMING_SNAKE_CASE_, SCREAMING_SNAKE_CASE_, SCREAMING_SNAKE_CASE_ ) -> Optional[Any]: UpperCAmelCase_: Optional[Any] = AlbertForQuestionAnswering(config=SCREAMING_SNAKE_CASE__ ) model.to(SCREAMING_SNAKE_CASE__ ) model.eval() UpperCAmelCase_: Any = model( SCREAMING_SNAKE_CASE__, attention_mask=SCREAMING_SNAKE_CASE__, token_type_ids=SCREAMING_SNAKE_CASE__, start_positions=SCREAMING_SNAKE_CASE__, end_positions=SCREAMING_SNAKE_CASE__, ) self.parent.assertEqual(result.start_logits.shape, (self.batch_size, self.seq_length) ) self.parent.assertEqual(result.end_logits.shape, (self.batch_size, self.seq_length) ) def __snake_case (self, SCREAMING_SNAKE_CASE_, SCREAMING_SNAKE_CASE_, SCREAMING_SNAKE_CASE_, SCREAMING_SNAKE_CASE_, SCREAMING_SNAKE_CASE_, SCREAMING_SNAKE_CASE_, SCREAMING_SNAKE_CASE_ ) -> List[str]: UpperCAmelCase_: Union[str, Any] = self.num_labels UpperCAmelCase_: Dict = AlbertForSequenceClassification(SCREAMING_SNAKE_CASE__ ) model.to(SCREAMING_SNAKE_CASE__ ) model.eval() UpperCAmelCase_: Optional[int] = model(SCREAMING_SNAKE_CASE__, attention_mask=SCREAMING_SNAKE_CASE__, token_type_ids=SCREAMING_SNAKE_CASE__, labels=SCREAMING_SNAKE_CASE__ ) self.parent.assertEqual(result.logits.shape, (self.batch_size, self.num_labels) ) def __snake_case (self, SCREAMING_SNAKE_CASE_, SCREAMING_SNAKE_CASE_, SCREAMING_SNAKE_CASE_, SCREAMING_SNAKE_CASE_, SCREAMING_SNAKE_CASE_, SCREAMING_SNAKE_CASE_, SCREAMING_SNAKE_CASE_ ) -> Dict: UpperCAmelCase_: List[str] = self.num_labels UpperCAmelCase_: Tuple = AlbertForTokenClassification(config=SCREAMING_SNAKE_CASE__ ) model.to(SCREAMING_SNAKE_CASE__ ) model.eval() UpperCAmelCase_: Dict = model(SCREAMING_SNAKE_CASE__, attention_mask=SCREAMING_SNAKE_CASE__, token_type_ids=SCREAMING_SNAKE_CASE__, labels=SCREAMING_SNAKE_CASE__ ) self.parent.assertEqual(result.logits.shape, (self.batch_size, self.seq_length, self.num_labels) ) def __snake_case (self, SCREAMING_SNAKE_CASE_, SCREAMING_SNAKE_CASE_, SCREAMING_SNAKE_CASE_, SCREAMING_SNAKE_CASE_, SCREAMING_SNAKE_CASE_, SCREAMING_SNAKE_CASE_, SCREAMING_SNAKE_CASE_ ) -> List[Any]: UpperCAmelCase_: Tuple = self.num_choices UpperCAmelCase_: List[Any] = AlbertForMultipleChoice(config=SCREAMING_SNAKE_CASE__ ) model.to(SCREAMING_SNAKE_CASE__ ) model.eval() UpperCAmelCase_: Optional[int] = input_ids.unsqueeze(1 ).expand(-1, self.num_choices, -1 ).contiguous() UpperCAmelCase_: Dict = token_type_ids.unsqueeze(1 ).expand(-1, self.num_choices, -1 ).contiguous() UpperCAmelCase_: Optional[int] = input_mask.unsqueeze(1 ).expand(-1, self.num_choices, -1 ).contiguous() UpperCAmelCase_: Any = model( SCREAMING_SNAKE_CASE__, attention_mask=SCREAMING_SNAKE_CASE__, token_type_ids=SCREAMING_SNAKE_CASE__, labels=SCREAMING_SNAKE_CASE__, ) self.parent.assertEqual(result.logits.shape, (self.batch_size, self.num_choices) ) def __snake_case (self ) -> Dict: UpperCAmelCase_: Union[str, Any] = self.prepare_config_and_inputs() ( UpperCAmelCase_ ): List[Any] = config_and_inputs UpperCAmelCase_: Dict = {"input_ids": input_ids, "token_type_ids": token_type_ids, "attention_mask": input_mask} return config, inputs_dict @require_torch class _a ( _a , _a , unittest.TestCase ): A = ( ( AlbertModel, AlbertForPreTraining, AlbertForMaskedLM, AlbertForMultipleChoice, AlbertForSequenceClassification, AlbertForTokenClassification, AlbertForQuestionAnswering, ) if is_torch_available() else () ) A = ( { '''feature-extraction''': AlbertModel, '''fill-mask''': AlbertForMaskedLM, '''question-answering''': AlbertForQuestionAnswering, '''text-classification''': AlbertForSequenceClassification, '''token-classification''': AlbertForTokenClassification, '''zero-shot''': AlbertForSequenceClassification, } if is_torch_available() else {} ) A = True def __snake_case (self, SCREAMING_SNAKE_CASE_, SCREAMING_SNAKE_CASE_, SCREAMING_SNAKE_CASE_=False ) -> Any: UpperCAmelCase_: int = super()._prepare_for_class(SCREAMING_SNAKE_CASE__, SCREAMING_SNAKE_CASE__, return_labels=SCREAMING_SNAKE_CASE__ ) if return_labels: if model_class in get_values(SCREAMING_SNAKE_CASE__ ): UpperCAmelCase_: Optional[Any] = torch.zeros( (self.model_tester.batch_size, self.model_tester.seq_length), dtype=torch.long, device=SCREAMING_SNAKE_CASE__ ) UpperCAmelCase_: Union[str, Any] = torch.zeros( self.model_tester.batch_size, dtype=torch.long, device=SCREAMING_SNAKE_CASE__ ) return inputs_dict def __snake_case (self ) -> Optional[Any]: UpperCAmelCase_: int = AlbertModelTester(self ) UpperCAmelCase_: Any = ConfigTester(self, config_class=SCREAMING_SNAKE_CASE__, hidden_size=37 ) def __snake_case (self ) -> Union[str, Any]: self.config_tester.run_common_tests() def __snake_case (self ) -> Union[str, Any]: UpperCAmelCase_: List[Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*SCREAMING_SNAKE_CASE__ ) def __snake_case (self ) -> Union[str, Any]: UpperCAmelCase_: Optional[Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_pretraining(*SCREAMING_SNAKE_CASE__ ) def __snake_case (self ) -> Optional[int]: UpperCAmelCase_: Optional[int] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_masked_lm(*SCREAMING_SNAKE_CASE__ ) def __snake_case (self ) -> Tuple: UpperCAmelCase_: Union[str, Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_multiple_choice(*SCREAMING_SNAKE_CASE__ ) def __snake_case (self ) -> Union[str, Any]: UpperCAmelCase_: Tuple = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_question_answering(*SCREAMING_SNAKE_CASE__ ) def __snake_case (self ) -> Any: UpperCAmelCase_: Optional[int] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_sequence_classification(*SCREAMING_SNAKE_CASE__ ) def __snake_case (self ) -> Dict: UpperCAmelCase_: Tuple = self.model_tester.prepare_config_and_inputs() for type in ["absolute", "relative_key", "relative_key_query"]: UpperCAmelCase_: List[Any] = type self.model_tester.create_and_check_model(*SCREAMING_SNAKE_CASE__ ) @slow def __snake_case (self ) -> str: for model_name in ALBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: UpperCAmelCase_: Dict = AlbertModel.from_pretrained(SCREAMING_SNAKE_CASE__ ) self.assertIsNotNone(SCREAMING_SNAKE_CASE__ ) @require_torch class _a ( unittest.TestCase ): @slow def __snake_case (self ) -> Tuple: UpperCAmelCase_: Union[str, Any] = AlbertModel.from_pretrained("""albert-base-v2""" ) UpperCAmelCase_: List[str] = torch.tensor([[0, 345, 232, 328, 740, 140, 1695, 69, 6078, 1588, 2]] ) UpperCAmelCase_: Optional[Any] = torch.tensor([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]] ) with torch.no_grad(): UpperCAmelCase_: List[Any] = model(SCREAMING_SNAKE_CASE__, attention_mask=SCREAMING_SNAKE_CASE__ )[0] UpperCAmelCase_: int = torch.Size((1, 11, 768) ) self.assertEqual(output.shape, SCREAMING_SNAKE_CASE__ ) UpperCAmelCase_: List[Any] = torch.tensor( [[[-0.6_5_1_3, 1.5_0_3_5, -0.2_7_6_6], [-0.6_5_1_5, 1.5_0_4_6, -0.2_7_8_0], [-0.6_5_1_2, 1.5_0_4_9, -0.2_7_8_4]]] ) self.assertTrue(torch.allclose(output[:, 1:4, 1:4], SCREAMING_SNAKE_CASE__, atol=1E-4 ) )
147
'''simple docstring''' from math import factorial, pi def A_ ( snake_case , snake_case = 30 ): if not isinstance(snake_case , (int, float) ): raise ValueError("maclaurin_sin() requires either an int or float for theta" ) if not isinstance(snake_case , snake_case ) or accuracy <= 0: raise ValueError("maclaurin_sin() requires a positive int for accuracy" ) SCREAMING_SNAKE_CASE:Optional[int] = float(snake_case ) SCREAMING_SNAKE_CASE:Optional[Any] = theta // (2 * pi) theta -= 2 * div * pi return sum( (-1) ** r * theta ** (2 * r + 1) / factorial(2 * r + 1 ) for r in range(snake_case ) ) def A_ ( snake_case , snake_case = 30 ): if not isinstance(snake_case , (int, float) ): raise ValueError("maclaurin_cos() requires either an int or float for theta" ) if not isinstance(snake_case , snake_case ) or accuracy <= 0: raise ValueError("maclaurin_cos() requires a positive int for accuracy" ) SCREAMING_SNAKE_CASE:Optional[Any] = float(snake_case ) SCREAMING_SNAKE_CASE:List[str] = theta // (2 * pi) theta -= 2 * div * pi return sum((-1) ** r * theta ** (2 * r) / factorial(2 * r ) for r in range(snake_case ) ) if __name__ == "__main__": import doctest doctest.testmod() print(maclaurin_sin(10)) print(maclaurin_sin(-10)) print(maclaurin_sin(10, 15)) print(maclaurin_sin(-10, 15)) print(maclaurin_cos(5)) print(maclaurin_cos(-5)) print(maclaurin_cos(10, 15)) print(maclaurin_cos(-10, 15))
139
0
'''simple docstring''' def a__ ( a__ ): """simple docstring""" for i in range(len(__snake_case ) - 1 , 0 , -1 ): __SCREAMING_SNAKE_CASE = False for j in range(__snake_case , 0 , -1 ): if unsorted[j] < unsorted[j - 1]: __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = unsorted[j - 1], unsorted[j] __SCREAMING_SNAKE_CASE = True for j in range(__snake_case ): if unsorted[j] > unsorted[j + 1]: __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = unsorted[j + 1], unsorted[j] __SCREAMING_SNAKE_CASE = True if not swapped: break return unsorted if __name__ == "__main__": import doctest doctest.testmod() UpperCAmelCase : Union[str, Any] = input('Enter numbers separated by a comma:\n').strip() UpperCAmelCase : List[Any] = [int(item) for item in user_input.split(',')] print(f"""{cocktail_shaker_sort(unsorted) = }""")
362
'''simple docstring''' import gc import unittest import numpy as np import torch from transformers import CLIPTextConfig, CLIPTextModel, XLMRobertaTokenizer from diffusers import AltDiffusionPipeline, AutoencoderKL, DDIMScheduler, PNDMScheduler, UNetaDConditionModel from diffusers.pipelines.alt_diffusion.modeling_roberta_series import ( RobertaSeriesConfig, RobertaSeriesModelWithTransformation, ) from diffusers.utils import slow, torch_device from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu from ..pipeline_params import TEXT_TO_IMAGE_BATCH_PARAMS, TEXT_TO_IMAGE_IMAGE_PARAMS, TEXT_TO_IMAGE_PARAMS from ..test_pipelines_common import PipelineKarrasSchedulerTesterMixin, PipelineLatentTesterMixin, PipelineTesterMixin enable_full_determinism() class lowerCAmelCase__ ( a , a , a , unittest.TestCase ): """simple docstring""" lowerCAmelCase__ = AltDiffusionPipeline lowerCAmelCase__ = TEXT_TO_IMAGE_PARAMS lowerCAmelCase__ = TEXT_TO_IMAGE_BATCH_PARAMS lowerCAmelCase__ = TEXT_TO_IMAGE_IMAGE_PARAMS lowerCAmelCase__ = TEXT_TO_IMAGE_IMAGE_PARAMS def UpperCAmelCase__ ( self : List[str] ) -> Optional[int]: """simple docstring""" torch.manual_seed(0 ) __SCREAMING_SNAKE_CASE = 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 , ) __SCREAMING_SNAKE_CASE = DDIMScheduler( beta_start=0.00085 , beta_end=0.012 , beta_schedule="""scaled_linear""" , clip_sample=__SCREAMING_SNAKE_CASE , set_alpha_to_one=__SCREAMING_SNAKE_CASE , ) torch.manual_seed(0 ) __SCREAMING_SNAKE_CASE = 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 , ) # TODO: address the non-deterministic text encoder (fails for save-load tests) # torch.manual_seed(0) # text_encoder_config = RobertaSeriesConfig( # hidden_size=32, # project_dim=32, # intermediate_size=37, # layer_norm_eps=1e-05, # num_attention_heads=4, # num_hidden_layers=5, # vocab_size=5002, # ) # text_encoder = RobertaSeriesModelWithTransformation(text_encoder_config) torch.manual_seed(0 ) __SCREAMING_SNAKE_CASE = CLIPTextConfig( bos_token_id=0 , eos_token_id=2 , hidden_size=32 , projection_dim=32 , intermediate_size=37 , layer_norm_eps=1E-05 , num_attention_heads=4 , num_hidden_layers=5 , pad_token_id=1 , vocab_size=5_002 , ) __SCREAMING_SNAKE_CASE = CLIPTextModel(__SCREAMING_SNAKE_CASE ) __SCREAMING_SNAKE_CASE = XLMRobertaTokenizer.from_pretrained("""hf-internal-testing/tiny-xlm-roberta""" ) __SCREAMING_SNAKE_CASE = 77 __SCREAMING_SNAKE_CASE = { """unet""": unet, """scheduler""": scheduler, """vae""": vae, """text_encoder""": text_encoder, """tokenizer""": tokenizer, """safety_checker""": None, """feature_extractor""": None, } return components def UpperCAmelCase__ ( self : Tuple , __SCREAMING_SNAKE_CASE : Dict , __SCREAMING_SNAKE_CASE : Dict=0 ) -> List[str]: """simple docstring""" if str(__SCREAMING_SNAKE_CASE ).startswith("""mps""" ): __SCREAMING_SNAKE_CASE = torch.manual_seed(__SCREAMING_SNAKE_CASE ) else: __SCREAMING_SNAKE_CASE = torch.Generator(device=__SCREAMING_SNAKE_CASE ).manual_seed(__SCREAMING_SNAKE_CASE ) __SCREAMING_SNAKE_CASE = { """prompt""": """A painting of a squirrel eating a burger""", """generator""": generator, """num_inference_steps""": 2, """guidance_scale""": 6.0, """output_type""": """numpy""", } return inputs def UpperCAmelCase__ ( self : Any ) -> Tuple: """simple docstring""" super().test_attention_slicing_forward_pass(expected_max_diff=3E-3 ) def UpperCAmelCase__ ( self : Tuple ) -> str: """simple docstring""" super().test_inference_batch_single_identical(expected_max_diff=3E-3 ) def UpperCAmelCase__ ( self : Optional[int] ) -> Tuple: """simple docstring""" __SCREAMING_SNAKE_CASE = """cpu""" # ensure determinism for the device-dependent torch.Generator __SCREAMING_SNAKE_CASE = self.get_dummy_components() torch.manual_seed(0 ) __SCREAMING_SNAKE_CASE = RobertaSeriesConfig( hidden_size=32 , project_dim=32 , intermediate_size=37 , layer_norm_eps=1E-05 , num_attention_heads=4 , num_hidden_layers=5 , vocab_size=5_002 , ) # TODO: remove after fixing the non-deterministic text encoder __SCREAMING_SNAKE_CASE = RobertaSeriesModelWithTransformation(__SCREAMING_SNAKE_CASE ) __SCREAMING_SNAKE_CASE = text_encoder __SCREAMING_SNAKE_CASE = AltDiffusionPipeline(**__SCREAMING_SNAKE_CASE ) __SCREAMING_SNAKE_CASE = alt_pipe.to(__SCREAMING_SNAKE_CASE ) alt_pipe.set_progress_bar_config(disable=__SCREAMING_SNAKE_CASE ) __SCREAMING_SNAKE_CASE = self.get_dummy_inputs(__SCREAMING_SNAKE_CASE ) __SCREAMING_SNAKE_CASE = """A photo of an astronaut""" __SCREAMING_SNAKE_CASE = alt_pipe(**__SCREAMING_SNAKE_CASE ) __SCREAMING_SNAKE_CASE = output.images __SCREAMING_SNAKE_CASE = image[0, -3:, -3:, -1] assert image.shape == (1, 64, 64, 3) __SCREAMING_SNAKE_CASE = np.array( [0.5748162, 0.60447145, 0.48821217, 0.50100636, 0.5431185, 0.45763683, 0.49657696, 0.48132733, 0.47573093] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-2 def UpperCAmelCase__ ( self : int ) -> Union[str, Any]: """simple docstring""" __SCREAMING_SNAKE_CASE = """cpu""" # ensure determinism for the device-dependent torch.Generator __SCREAMING_SNAKE_CASE = self.get_dummy_components() __SCREAMING_SNAKE_CASE = PNDMScheduler(skip_prk_steps=__SCREAMING_SNAKE_CASE ) torch.manual_seed(0 ) __SCREAMING_SNAKE_CASE = RobertaSeriesConfig( hidden_size=32 , project_dim=32 , intermediate_size=37 , layer_norm_eps=1E-05 , num_attention_heads=4 , num_hidden_layers=5 , vocab_size=5_002 , ) # TODO: remove after fixing the non-deterministic text encoder __SCREAMING_SNAKE_CASE = RobertaSeriesModelWithTransformation(__SCREAMING_SNAKE_CASE ) __SCREAMING_SNAKE_CASE = text_encoder __SCREAMING_SNAKE_CASE = AltDiffusionPipeline(**__SCREAMING_SNAKE_CASE ) __SCREAMING_SNAKE_CASE = alt_pipe.to(__SCREAMING_SNAKE_CASE ) alt_pipe.set_progress_bar_config(disable=__SCREAMING_SNAKE_CASE ) __SCREAMING_SNAKE_CASE = self.get_dummy_inputs(__SCREAMING_SNAKE_CASE ) __SCREAMING_SNAKE_CASE = alt_pipe(**__SCREAMING_SNAKE_CASE ) __SCREAMING_SNAKE_CASE = output.images __SCREAMING_SNAKE_CASE = image[0, -3:, -3:, -1] assert image.shape == (1, 64, 64, 3) __SCREAMING_SNAKE_CASE = np.array( [0.51605093, 0.5707241, 0.47365507, 0.50578886, 0.5633877, 0.4642503, 0.5182081, 0.48763484, 0.49084237] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-2 @slow @require_torch_gpu class lowerCAmelCase__ ( unittest.TestCase ): """simple docstring""" def UpperCAmelCase__ ( self : Any ) -> Optional[Any]: """simple docstring""" super().tearDown() gc.collect() torch.cuda.empty_cache() def UpperCAmelCase__ ( self : Union[str, Any] ) -> Tuple: """simple docstring""" __SCREAMING_SNAKE_CASE = AltDiffusionPipeline.from_pretrained("""BAAI/AltDiffusion""" , safety_checker=__SCREAMING_SNAKE_CASE ) __SCREAMING_SNAKE_CASE = alt_pipe.to(__SCREAMING_SNAKE_CASE ) alt_pipe.set_progress_bar_config(disable=__SCREAMING_SNAKE_CASE ) __SCREAMING_SNAKE_CASE = """A painting of a squirrel eating a burger""" __SCREAMING_SNAKE_CASE = torch.manual_seed(0 ) __SCREAMING_SNAKE_CASE = alt_pipe([prompt] , generator=__SCREAMING_SNAKE_CASE , guidance_scale=6.0 , num_inference_steps=20 , output_type="""np""" ) __SCREAMING_SNAKE_CASE = output.images __SCREAMING_SNAKE_CASE = image[0, -3:, -3:, -1] assert image.shape == (1, 512, 512, 3) __SCREAMING_SNAKE_CASE = np.array([0.1010, 0.0800, 0.0794, 0.0885, 0.0843, 0.0762, 0.0769, 0.0729, 0.0586] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-2 def UpperCAmelCase__ ( self : List[Any] ) -> Any: """simple docstring""" __SCREAMING_SNAKE_CASE = DDIMScheduler.from_pretrained("""BAAI/AltDiffusion""" , subfolder="""scheduler""" ) __SCREAMING_SNAKE_CASE = AltDiffusionPipeline.from_pretrained("""BAAI/AltDiffusion""" , scheduler=__SCREAMING_SNAKE_CASE , safety_checker=__SCREAMING_SNAKE_CASE ) __SCREAMING_SNAKE_CASE = alt_pipe.to(__SCREAMING_SNAKE_CASE ) alt_pipe.set_progress_bar_config(disable=__SCREAMING_SNAKE_CASE ) __SCREAMING_SNAKE_CASE = """A painting of a squirrel eating a burger""" __SCREAMING_SNAKE_CASE = torch.manual_seed(0 ) __SCREAMING_SNAKE_CASE = alt_pipe([prompt] , generator=__SCREAMING_SNAKE_CASE , num_inference_steps=2 , output_type="""numpy""" ) __SCREAMING_SNAKE_CASE = output.images __SCREAMING_SNAKE_CASE = image[0, -3:, -3:, -1] assert image.shape == (1, 512, 512, 3) __SCREAMING_SNAKE_CASE = np.array([0.4019, 0.4052, 0.3810, 0.4119, 0.3916, 0.3982, 0.4651, 0.4195, 0.5323] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-2
331
0
import copy from typing import Any, Dict, List, Optional, Union import numpy as np from ...audio_utils import mel_filter_bank, spectrogram, window_function from ...feature_extraction_sequence_utils import SequenceFeatureExtractor from ...feature_extraction_utils import BatchFeature from ...utils import TensorType, logging __UpperCAmelCase = logging.get_logger(__name__) class UpperCamelCase__ ( a__ ): """simple docstring""" UpperCAmelCase_ =['input_features'] def __init__( self , _A=80 , _A=16000 , _A=160 , _A=30 , _A=400 , _A=0.0 , _A=False , **_A , ) -> List[str]: super().__init__( feature_size=_lowerCamelCase , sampling_rate=_lowerCamelCase , padding_value=_lowerCamelCase , return_attention_mask=_lowerCamelCase , **_lowerCamelCase , ) SCREAMING_SNAKE_CASE_ = n_fft SCREAMING_SNAKE_CASE_ = hop_length SCREAMING_SNAKE_CASE_ = chunk_length SCREAMING_SNAKE_CASE_ = chunk_length * sampling_rate SCREAMING_SNAKE_CASE_ = self.n_samples // hop_length SCREAMING_SNAKE_CASE_ = sampling_rate SCREAMING_SNAKE_CASE_ = mel_filter_bank( num_frequency_bins=1 + n_fft // 2 , num_mel_filters=_lowerCamelCase , min_frequency=0.0 , max_frequency=8000.0 , sampling_rate=_lowerCamelCase , norm='''slaney''' , mel_scale='''slaney''' , ) def _UpperCamelCase ( self , _A ) -> np.ndarray: SCREAMING_SNAKE_CASE_ = spectrogram( _lowerCamelCase , window_function(self.n_fft , '''hann''' ) , frame_length=self.n_fft , hop_length=self.hop_length , power=2.0 , mel_filters=self.mel_filters , log_mel='''log10''' , ) SCREAMING_SNAKE_CASE_ = log_spec[:, :-1] SCREAMING_SNAKE_CASE_ = np.maximum(_lowerCamelCase , log_spec.max() - 8.0 ) SCREAMING_SNAKE_CASE_ = (log_spec + 4.0) / 4.0 return log_spec @staticmethod # Copied from transformers.models.wav2vec2.feature_extraction_wav2vec2.Wav2Vec2FeatureExtractor.zero_mean_unit_var_norm def _UpperCamelCase ( _A , _A , _A = 0.0 ) -> List[np.ndarray]: if attention_mask is not None: SCREAMING_SNAKE_CASE_ = np.array(_lowerCamelCase , np.intaa ) SCREAMING_SNAKE_CASE_ = [] for vector, length in zip(_lowerCamelCase , attention_mask.sum(-1 ) ): SCREAMING_SNAKE_CASE_ = (vector - vector[:length].mean()) / np.sqrt(vector[:length].var() + 1E-7 ) if length < normed_slice.shape[0]: SCREAMING_SNAKE_CASE_ = padding_value normed_input_values.append(_lowerCamelCase ) else: SCREAMING_SNAKE_CASE_ = [(x - x.mean()) / np.sqrt(x.var() + 1E-7 ) for x in input_values] return normed_input_values def __call__( self , _A , _A = True , _A = None , _A = None , _A = None , _A = "max_length" , _A = None , _A = None , _A = None , **_A , ) -> BatchFeature: if sampling_rate is not None: if sampling_rate != self.sampling_rate: raise ValueError( F'''The model corresponding to this feature extractor: {self.__class__.__name__} was trained using a''' F''' sampling rate of {self.sampling_rate}. Please make sure that the provided `raw_speech` input''' F''' was sampled with {self.sampling_rate} and not {sampling_rate}.''' ) else: logger.warning( '''It is strongly recommended to pass the `sampling_rate` argument to this function. ''' '''Failing to do so can result in silent errors that might be hard to debug.''' ) SCREAMING_SNAKE_CASE_ = isinstance(_lowerCamelCase , 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_ = is_batched_numpy or ( isinstance(_lowerCamelCase , (list, tuple) ) and (isinstance(raw_speech[0] , (np.ndarray, tuple, list) )) ) if is_batched: SCREAMING_SNAKE_CASE_ = [np.asarray([speech] , dtype=np.floataa ).T for speech in raw_speech] elif not is_batched and not isinstance(_lowerCamelCase , np.ndarray ): SCREAMING_SNAKE_CASE_ = np.asarray(_lowerCamelCase , dtype=np.floataa ) elif isinstance(_lowerCamelCase , np.ndarray ) and raw_speech.dtype is np.dtype(np.floataa ): SCREAMING_SNAKE_CASE_ = raw_speech.astype(np.floataa ) # always return batch if not is_batched: SCREAMING_SNAKE_CASE_ = [np.asarray([raw_speech] ).T] SCREAMING_SNAKE_CASE_ = BatchFeature({'''input_features''': raw_speech} ) # convert into correct format for padding SCREAMING_SNAKE_CASE_ = self.pad( _lowerCamelCase , padding=_lowerCamelCase , max_length=max_length if max_length else self.n_samples , truncation=_lowerCamelCase , pad_to_multiple_of=_lowerCamelCase , return_attention_mask=return_attention_mask or do_normalize , ) # zero-mean and unit-variance normalization if do_normalize: SCREAMING_SNAKE_CASE_ = self.zero_mean_unit_var_norm( padded_inputs['''input_features'''] , attention_mask=padded_inputs['''attention_mask'''] , padding_value=self.padding_value , ) SCREAMING_SNAKE_CASE_ = np.stack(padded_inputs['''input_features'''] , axis=0 ) # make sure list is in array format SCREAMING_SNAKE_CASE_ = padded_inputs.get('''input_features''' ).transpose(2 , 0 , 1 ) SCREAMING_SNAKE_CASE_ = [self._np_extract_fbank_features(_lowerCamelCase ) for waveform in input_features[0]] if isinstance(input_features[0] , _lowerCamelCase ): SCREAMING_SNAKE_CASE_ = [np.asarray(_lowerCamelCase , dtype=np.floataa ) for feature in input_features] else: SCREAMING_SNAKE_CASE_ = input_features if return_attention_mask: # rescale from sample (48000) to feature (3000) SCREAMING_SNAKE_CASE_ = padded_inputs['''attention_mask'''][:, :: self.hop_length] if return_tensors is not None: SCREAMING_SNAKE_CASE_ = padded_inputs.convert_to_tensors(_lowerCamelCase ) return padded_inputs def _UpperCamelCase ( self ) -> Dict[str, Any]: SCREAMING_SNAKE_CASE_ = copy.deepcopy(self.__dict__ ) SCREAMING_SNAKE_CASE_ = self.__class__.__name__ if "mel_filters" in output: del output["mel_filters"] return output
299
import datetime import platform import subprocess from typing import Optional, Tuple, Union import numpy as np def UpperCAmelCase_( a__ , a__ ): """simple docstring""" SCREAMING_SNAKE_CASE : Optional[Any] = F"""{sampling_rate}""" SCREAMING_SNAKE_CASE : Tuple = '''1''' SCREAMING_SNAKE_CASE : Union[str, Any] = '''f32le''' SCREAMING_SNAKE_CASE : List[Any] = [ '''ffmpeg''', '''-i''', '''pipe:0''', '''-ac''', ac, '''-ar''', ar, '''-f''', format_for_conversion, '''-hide_banner''', '''-loglevel''', '''quiet''', '''pipe:1''', ] try: with subprocess.Popen(a__ , stdin=subprocess.PIPE , stdout=subprocess.PIPE ) as ffmpeg_process: SCREAMING_SNAKE_CASE : Tuple = ffmpeg_process.communicate(a__ ) except FileNotFoundError as error: raise ValueError('''ffmpeg was not found but is required to load audio files from filename''' ) from error SCREAMING_SNAKE_CASE : Optional[Any] = output_stream[0] SCREAMING_SNAKE_CASE : Any = np.frombuffer(a__ , np.floataa ) if audio.shape[0] == 0: raise ValueError('''Malformed soundfile''' ) return audio def UpperCAmelCase_( a__ , a__ , a__ = "f32le" , ): """simple docstring""" SCREAMING_SNAKE_CASE : Union[str, Any] = F"""{sampling_rate}""" SCREAMING_SNAKE_CASE : Dict = '''1''' if format_for_conversion == "s16le": SCREAMING_SNAKE_CASE : List[Any] = 2 elif format_for_conversion == "f32le": SCREAMING_SNAKE_CASE : Dict = 4 else: raise ValueError(F"""Unhandled format `{format_for_conversion}`. Please use `s16le` or `f32le`""" ) SCREAMING_SNAKE_CASE : Union[str, Any] = platform.system() if system == "Linux": SCREAMING_SNAKE_CASE : Dict = '''alsa''' SCREAMING_SNAKE_CASE : Any = '''default''' elif system == "Darwin": SCREAMING_SNAKE_CASE : Union[str, Any] = '''avfoundation''' SCREAMING_SNAKE_CASE : Optional[int] = ''':0''' elif system == "Windows": SCREAMING_SNAKE_CASE : int = '''dshow''' SCREAMING_SNAKE_CASE : Any = '''default''' SCREAMING_SNAKE_CASE : Union[str, Any] = [ '''ffmpeg''', '''-f''', format_, '''-i''', input_, '''-ac''', ac, '''-ar''', ar, '''-f''', format_for_conversion, '''-fflags''', '''nobuffer''', '''-hide_banner''', '''-loglevel''', '''quiet''', '''pipe:1''', ] SCREAMING_SNAKE_CASE : List[str] = int(round(sampling_rate * chunk_length_s ) ) * size_of_sample SCREAMING_SNAKE_CASE : List[Any] = _ffmpeg_stream(a__ , a__ ) for item in iterator: yield item def UpperCAmelCase_( a__ , a__ , a__ = None , a__ = None , a__ = "f32le" , ): """simple docstring""" if stream_chunk_s is not None: SCREAMING_SNAKE_CASE : Tuple = stream_chunk_s else: SCREAMING_SNAKE_CASE : List[str] = chunk_length_s SCREAMING_SNAKE_CASE : Union[str, Any] = ffmpeg_microphone(a__ , a__ , format_for_conversion=a__ ) if format_for_conversion == "s16le": SCREAMING_SNAKE_CASE : Optional[int] = np.intaa SCREAMING_SNAKE_CASE : List[Any] = 2 elif format_for_conversion == "f32le": SCREAMING_SNAKE_CASE : Any = np.floataa SCREAMING_SNAKE_CASE : Union[str, Any] = 4 else: raise ValueError(F"""Unhandled format `{format_for_conversion}`. Please use `s16le` or `f32le`""" ) if stride_length_s is None: SCREAMING_SNAKE_CASE : Optional[Any] = chunk_length_s / 6 SCREAMING_SNAKE_CASE : Dict = int(round(sampling_rate * chunk_length_s ) ) * size_of_sample if isinstance(a__ , (int, float) ): SCREAMING_SNAKE_CASE : List[Any] = [stride_length_s, stride_length_s] SCREAMING_SNAKE_CASE : Any = int(round(sampling_rate * stride_length_s[0] ) ) * size_of_sample SCREAMING_SNAKE_CASE : int = int(round(sampling_rate * stride_length_s[1] ) ) * size_of_sample SCREAMING_SNAKE_CASE : Union[str, Any] = datetime.datetime.now() SCREAMING_SNAKE_CASE : Dict = datetime.timedelta(seconds=a__ ) for item in chunk_bytes_iter(a__ , a__ , stride=(stride_left, stride_right) , stream=a__ ): # Put everything back in numpy scale SCREAMING_SNAKE_CASE : Dict = np.frombuffer(item['''raw'''] , dtype=a__ ) SCREAMING_SNAKE_CASE : Optional[Any] = ( item['''stride'''][0] // size_of_sample, item['''stride'''][1] // size_of_sample, ) SCREAMING_SNAKE_CASE : Any = sampling_rate audio_time += delta if datetime.datetime.now() > audio_time + 10 * delta: # We're late !! SKIP continue yield item def UpperCAmelCase_( a__ , a__ , a__ , a__ = False ): """simple docstring""" SCREAMING_SNAKE_CASE : List[str] = b'''''' SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE : Union[str, Any] = stride if stride_left + stride_right >= chunk_len: raise ValueError( F"""Stride needs to be strictly smaller than chunk_len: ({stride_left}, {stride_right}) vs {chunk_len}""" ) SCREAMING_SNAKE_CASE : Union[str, Any] = 0 for raw in iterator: acc += raw if stream and len(a__ ) < chunk_len: SCREAMING_SNAKE_CASE : List[str] = (_stride_left, 0) yield {"raw": acc[:chunk_len], "stride": stride, "partial": True} else: while len(a__ ) >= chunk_len: # We are flushing the accumulator SCREAMING_SNAKE_CASE : str = (_stride_left, stride_right) SCREAMING_SNAKE_CASE : List[str] = {'''raw''': acc[:chunk_len], '''stride''': stride} if stream: SCREAMING_SNAKE_CASE : List[str] = False yield item SCREAMING_SNAKE_CASE : Dict = stride_left SCREAMING_SNAKE_CASE : int = acc[chunk_len - stride_left - stride_right :] # Last chunk if len(a__ ) > stride_left: SCREAMING_SNAKE_CASE : Optional[Any] = {'''raw''': acc, '''stride''': (_stride_left, 0)} if stream: SCREAMING_SNAKE_CASE : Union[str, Any] = False yield item def UpperCAmelCase_( a__ , a__ ): """simple docstring""" SCREAMING_SNAKE_CASE : str = 2**24 # 16Mo try: with subprocess.Popen(a__ , stdout=subprocess.PIPE , bufsize=a__ ) as ffmpeg_process: while True: SCREAMING_SNAKE_CASE : str = ffmpeg_process.stdout.read(a__ ) if raw == b"": break yield raw except FileNotFoundError as error: raise ValueError('''ffmpeg was not found but is required to stream audio files from filename''' ) from error
313
0
"""simple docstring""" from pathlib import Path import numpy as np from PIL import Image def a__ ( _SCREAMING_SNAKE_CASE ): """simple docstring""" UpperCamelCase , UpperCamelCase , UpperCamelCase = rgb[:, :, 0], rgb[:, :, 1], rgb[:, :, 2] return 0.29_89 * r + 0.58_70 * g + 0.11_40 * b def a__ ( _SCREAMING_SNAKE_CASE ): """simple docstring""" return (gray > 127) & (gray <= 255) def a__ ( _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ): """simple docstring""" UpperCamelCase = np.zeros_like(__snake_case ) UpperCamelCase = np.zeros( (image.shape[0] + kernel.shape[0] - 1, image.shape[1] + kernel.shape[1] - 1) ) # Copy image to padded image UpperCamelCase = image # Iterate over image & apply kernel for x in range(image.shape[1] ): for y in range(image.shape[0] ): UpperCamelCase = ( kernel * image_padded[y : y + kernel.shape[0], x : x + kernel.shape[1]] ).sum() UpperCamelCase = int(summation > 0 ) return output if __name__ == "__main__": # read original image lowerCAmelCase__ = Path(__file__).resolve().parent / """image_data""" / """lena.jpg""" lowerCAmelCase__ = np.array(Image.open(lena_path)) # kernel to be applied lowerCAmelCase__ = np.array([[0, 1, 0], [1, 1, 1], [0, 1, 0]]) lowerCAmelCase__ = dilation(gray_to_binary(rgb_to_gray(lena)), structuring_element) # Save the output image lowerCAmelCase__ = Image.fromarray(output).convert('''RGB''') pil_img.save('''result_dilation.png''')
356
"""simple docstring""" import math def a__ ( _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ): """simple docstring""" UpperCamelCase = len(_SCREAMING_SNAKE_CASE ) UpperCamelCase = int(math.floor(math.sqrt(_SCREAMING_SNAKE_CASE ) ) ) UpperCamelCase = 0 while arr[min(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) - 1] < x: UpperCamelCase = step step += int(math.floor(math.sqrt(_SCREAMING_SNAKE_CASE ) ) ) if prev >= n: return -1 while arr[prev] < x: UpperCamelCase = prev + 1 if prev == min(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ): return -1 if arr[prev] == x: return prev return -1 if __name__ == "__main__": lowerCAmelCase__ = input('''Enter numbers separated by a comma:\n''').strip() lowerCAmelCase__ = [int(item) for item in user_input.split(''',''')] lowerCAmelCase__ = int(input('''Enter the number to be searched:\n''')) lowerCAmelCase__ = jump_search(arr, x) if res == -1: print('''Number not found!''') else: print(f'''Number {x} is at index {res}''')
244
0
'''simple docstring''' import copy import os from typing import Union from ...configuration_utils import PretrainedConfig from ...models.auto.modeling_auto import MODEL_FOR_CAUSAL_LM_MAPPING_NAMES from ...utils import logging from ..auto import CONFIG_MAPPING _lowerCAmelCase = logging.get_logger(__name__) _lowerCAmelCase = { '''Salesforce/instruct-blip-flan-t5''': '''https://huggingface.co/Salesforce/instruct-blip-flan-t5/resolve/main/config.json''', } class lowerCAmelCase_( __SCREAMING_SNAKE_CASE ): '''simple docstring''' __lowercase : Dict = """instructblip_vision_model""" def __init__( self ,__UpperCAmelCase=1408 ,__UpperCAmelCase=6144 ,__UpperCAmelCase=39 ,__UpperCAmelCase=16 ,__UpperCAmelCase=224 ,__UpperCAmelCase=14 ,__UpperCAmelCase="gelu" ,__UpperCAmelCase=1E-6 ,__UpperCAmelCase=0.0 ,__UpperCAmelCase=1E-10 ,__UpperCAmelCase=True ,**__UpperCAmelCase ,) -> Optional[int]: super().__init__(**__UpperCamelCase ) lowerCAmelCase__ : Any = hidden_size lowerCAmelCase__ : Dict = intermediate_size lowerCAmelCase__ : int = num_hidden_layers lowerCAmelCase__ : List[Any] = num_attention_heads lowerCAmelCase__ : Optional[Any] = patch_size lowerCAmelCase__ : List[Any] = image_size lowerCAmelCase__ : Optional[int] = initializer_range lowerCAmelCase__ : Optional[Any] = attention_dropout lowerCAmelCase__ : Optional[Any] = layer_norm_eps lowerCAmelCase__ : List[Any] = hidden_act lowerCAmelCase__ : Union[str, Any] = qkv_bias @classmethod def UpperCAmelCase_ ( cls ,__UpperCAmelCase ,**__UpperCAmelCase ) -> str: cls._set_token_in_kwargs(__UpperCamelCase ) lowerCAmelCase__ , lowerCAmelCase__ : Union[str, Any] = cls.get_config_dict(__UpperCamelCase ,**__UpperCamelCase ) # get the vision config dict if we are loading from InstructBlipConfig if config_dict.get("""model_type""" ) == "instructblip": lowerCAmelCase__ : str = config_dict["""vision_config"""] if "model_type" in config_dict and hasattr(cls ,"""model_type""" ) and config_dict["model_type"] != cls.model_type: logger.warning( F"""You are using a model of type {config_dict['model_type']} to instantiate a model of type """ F"""{cls.model_type}. This is not supported for all configurations of models and can yield errors.""" ) return cls.from_dict(__UpperCamelCase ,**__UpperCamelCase ) class lowerCAmelCase_( __SCREAMING_SNAKE_CASE ): '''simple docstring''' __lowercase : Union[str, Any] = """instructblip_qformer""" def __init__( self ,__UpperCAmelCase=3_0522 ,__UpperCAmelCase=768 ,__UpperCAmelCase=12 ,__UpperCAmelCase=12 ,__UpperCAmelCase=3072 ,__UpperCAmelCase="gelu" ,__UpperCAmelCase=0.1 ,__UpperCAmelCase=0.1 ,__UpperCAmelCase=512 ,__UpperCAmelCase=0.0_2 ,__UpperCAmelCase=1E-12 ,__UpperCAmelCase=0 ,__UpperCAmelCase="absolute" ,__UpperCAmelCase=2 ,__UpperCAmelCase=1408 ,**__UpperCAmelCase ,) -> Optional[Any]: super().__init__(pad_token_id=__UpperCamelCase ,**__UpperCamelCase ) lowerCAmelCase__ : Tuple = vocab_size lowerCAmelCase__ : str = hidden_size lowerCAmelCase__ : Any = num_hidden_layers lowerCAmelCase__ : Union[str, Any] = num_attention_heads lowerCAmelCase__ : List[Any] = hidden_act lowerCAmelCase__ : Dict = intermediate_size lowerCAmelCase__ : Union[str, Any] = hidden_dropout_prob lowerCAmelCase__ : Dict = attention_probs_dropout_prob lowerCAmelCase__ : Any = max_position_embeddings lowerCAmelCase__ : Optional[int] = initializer_range lowerCAmelCase__ : Tuple = layer_norm_eps lowerCAmelCase__ : Any = position_embedding_type lowerCAmelCase__ : int = cross_attention_frequency lowerCAmelCase__ : List[str] = encoder_hidden_size @classmethod def UpperCAmelCase_ ( cls ,__UpperCAmelCase ,**__UpperCAmelCase ) -> Tuple: cls._set_token_in_kwargs(__UpperCamelCase ) lowerCAmelCase__ , lowerCAmelCase__ : str = cls.get_config_dict(__UpperCamelCase ,**__UpperCamelCase ) # get the qformer config dict if we are loading from InstructBlipConfig if config_dict.get("""model_type""" ) == "instructblip": lowerCAmelCase__ : Optional[Any] = config_dict["""qformer_config"""] if "model_type" in config_dict and hasattr(cls ,"""model_type""" ) and config_dict["model_type"] != cls.model_type: logger.warning( F"""You are using a model of type {config_dict['model_type']} to instantiate a model of type """ F"""{cls.model_type}. This is not supported for all configurations of models and can yield errors.""" ) return cls.from_dict(__UpperCamelCase ,**__UpperCamelCase ) class lowerCAmelCase_( __SCREAMING_SNAKE_CASE ): '''simple docstring''' __lowercase : List[Any] = """instructblip""" __lowercase : Dict = True def __init__( self ,__UpperCAmelCase=None ,__UpperCAmelCase=None ,__UpperCAmelCase=None ,__UpperCAmelCase=32 ,**__UpperCAmelCase ) -> Union[str, Any]: super().__init__(**__UpperCamelCase ) if vision_config is None: lowerCAmelCase__ : List[str] = {} logger.info("""vision_config is None. initializing the InstructBlipVisionConfig with default values.""" ) if qformer_config is None: lowerCAmelCase__ : Dict = {} logger.info("""qformer_config is None. Initializing the InstructBlipQFormerConfig with default values.""" ) if text_config is None: lowerCAmelCase__ : List[Any] = {} logger.info("""text_config is None. Initializing the text config with default values (`OPTConfig`).""" ) lowerCAmelCase__ : List[Any] = InstructBlipVisionConfig(**__UpperCamelCase ) lowerCAmelCase__ : Optional[Any] = InstructBlipQFormerConfig(**__UpperCamelCase ) lowerCAmelCase__ : str = text_config["""model_type"""] if """model_type""" in text_config else """opt""" lowerCAmelCase__ : Optional[Any] = CONFIG_MAPPING[text_model_type](**__UpperCamelCase ) lowerCAmelCase__ : Any = self.text_config.tie_word_embeddings lowerCAmelCase__ : int = self.text_config.is_encoder_decoder lowerCAmelCase__ : int = num_query_tokens lowerCAmelCase__ : List[Any] = self.vision_config.hidden_size lowerCAmelCase__ : Dict = self.text_config.model_type in MODEL_FOR_CAUSAL_LM_MAPPING_NAMES lowerCAmelCase__ : Tuple = 1.0 lowerCAmelCase__ : Union[str, Any] = 0.0_2 @classmethod def UpperCAmelCase_ ( cls ,__UpperCAmelCase ,__UpperCAmelCase ,__UpperCAmelCase ,**__UpperCAmelCase ,) -> Union[str, Any]: return cls( vision_config=vision_config.to_dict() ,qformer_config=qformer_config.to_dict() ,text_config=text_config.to_dict() ,**__UpperCamelCase ,) def UpperCAmelCase_ ( self ) -> List[Any]: lowerCAmelCase__ : List[str] = copy.deepcopy(self.__dict__ ) lowerCAmelCase__ : Dict = self.vision_config.to_dict() lowerCAmelCase__ : Tuple = self.qformer_config.to_dict() lowerCAmelCase__ : List[Any] = self.text_config.to_dict() lowerCAmelCase__ : Optional[int] = self.__class__.model_type return output
37
_A = [0, 2, 4, 6, 8] _A = [1, 3, 5, 7, 9] def lowerCamelCase__ ( a__ : int , a__ : int , a__ : list[int] , a__ : int ) -> int: if remaining_length == 0: if digits[0] == 0 or digits[-1] == 0: return 0 for i in range(length // 2 - 1 , -1 , -1 ): remainder += digits[i] + digits[length - i - 1] if remainder % 2 == 0: return 0 remainder //= 10 return 1 if remaining_length == 1: if remainder % 2 == 0: return 0 UpperCamelCase_ = 0 for digit in range(10 ): UpperCamelCase_ = digit result += reversible_numbers( 0 , (remainder + 2 * digit) // 10 , a__ , a__ ) return result UpperCamelCase_ = 0 for digita in range(10 ): UpperCamelCase_ = digita if (remainder + digita) % 2 == 0: UpperCamelCase_ = ODD_DIGITS else: UpperCamelCase_ = EVEN_DIGITS for digita in other_parity_digits: UpperCamelCase_ = digita result += reversible_numbers( remaining_length - 2 , (remainder + digita + digita) // 10 , a__ , a__ , ) return result def lowerCamelCase__ ( a__ : int = 9 ) -> int: UpperCamelCase_ = 0 for length in range(1 , max_power + 1 ): result += reversible_numbers(a__ , 0 , [0] * length , a__ ) return result if __name__ == "__main__": print(F'''{solution() = }''')
122
0
'''simple docstring''' from math import factorial def lowerCamelCase__ ( __lowerCamelCase : int , __lowerCamelCase : int ): '''simple docstring''' if n < k or k < 0: raise ValueError('Please enter positive integers for n and k where n >= k' ) return factorial(_SCREAMING_SNAKE_CASE ) // (factorial(_SCREAMING_SNAKE_CASE ) * factorial(n - k )) if __name__ == "__main__": print( 'The number of five-card hands possible from a standard', F"""fifty-two card deck is: {combinations(52, 5)}\n""", ) print( 'If a class of 40 students must be arranged into groups of', F"""4 for group projects, there are {combinations(40, 4)} ways""", 'to arrange them.\n', ) print( 'If 10 teams are competing in a Formula One race, there', F"""are {combinations(10, 3)} ways that first, second and""", 'third place can be awarded.', )
351
'''simple docstring''' from typing import Any def lowerCamelCase__ ( __lowerCamelCase : list , __lowerCamelCase : list , __lowerCamelCase : dict , __lowerCamelCase : dict , __lowerCamelCase : dict , ): '''simple docstring''' _validation( __lowerCamelCase , __lowerCamelCase , __lowerCamelCase , __lowerCamelCase , __lowerCamelCase , ) # Creates data structures and fill initial step _UpperCAmelCase : dict ={} _UpperCAmelCase : dict ={} for state in states_space: _UpperCAmelCase : int =observations_space[0] _UpperCAmelCase : int =( initial_probabilities[state] * emission_probabilities[state][observation] ) _UpperCAmelCase : int =None # Fills the data structure with the probabilities of # different transitions and pointers to previous states for o in range(1 , len(__lowerCamelCase ) ): _UpperCAmelCase : List[Any] =observations_space[o] _UpperCAmelCase : Optional[int] =observations_space[o - 1] for state in states_space: # Calculates the argmax for probability function _UpperCAmelCase : List[str] ='' _UpperCAmelCase : Dict =-1 for k_state in states_space: _UpperCAmelCase : List[str] =( probabilities[(k_state, prior_observation)] * transition_probabilities[k_state][state] * emission_probabilities[state][observation] ) if probability > max_probability: _UpperCAmelCase : int =probability _UpperCAmelCase : List[Any] =k_state # Update probabilities and pointers dicts _UpperCAmelCase : str =( probabilities[(arg_max, prior_observation)] * transition_probabilities[arg_max][state] * emission_probabilities[state][observation] ) _UpperCAmelCase : List[Any] =arg_max # The final observation _UpperCAmelCase : int =observations_space[len(__lowerCamelCase ) - 1] # argmax for given final observation _UpperCAmelCase : Any ='' _UpperCAmelCase : Union[str, Any] =-1 for k_state in states_space: _UpperCAmelCase : Optional[int] =probabilities[(k_state, final_observation)] if probability > max_probability: _UpperCAmelCase : Union[str, Any] =probability _UpperCAmelCase : int =k_state _UpperCAmelCase : int =arg_max # Process pointers backwards _UpperCAmelCase : List[str] =last_state _UpperCAmelCase : Optional[int] =[] for o in range(len(__lowerCamelCase ) - 1 , -1 , -1 ): result.append(__lowerCamelCase ) _UpperCAmelCase : Optional[Any] =pointers[previous, observations_space[o]] result.reverse() return result def lowerCamelCase__ ( __lowerCamelCase : Any , __lowerCamelCase : Any , __lowerCamelCase : Any , __lowerCamelCase : Any , __lowerCamelCase : Any , ): '''simple docstring''' _validate_not_empty( __lowerCamelCase , __lowerCamelCase , __lowerCamelCase , __lowerCamelCase , __lowerCamelCase , ) _validate_lists(__lowerCamelCase , __lowerCamelCase ) _validate_dicts( __lowerCamelCase , __lowerCamelCase , __lowerCamelCase ) def lowerCamelCase__ ( __lowerCamelCase : Any , __lowerCamelCase : Any , __lowerCamelCase : Any , __lowerCamelCase : Any , __lowerCamelCase : Any , ): '''simple docstring''' if not all( [ observations_space, states_space, initial_probabilities, transition_probabilities, emission_probabilities, ] ): raise ValueError('There\'s an empty parameter' ) def lowerCamelCase__ ( __lowerCamelCase : Any , __lowerCamelCase : Any ): '''simple docstring''' _validate_list(__lowerCamelCase , 'observations_space' ) _validate_list(__lowerCamelCase , 'states_space' ) def lowerCamelCase__ ( __lowerCamelCase : Any , __lowerCamelCase : str ): '''simple docstring''' if not isinstance(_object , __lowerCamelCase ): _UpperCAmelCase : Any =f"{var_name} must be a list" raise ValueError(__lowerCamelCase ) else: for x in _object: if not isinstance(__lowerCamelCase , __lowerCamelCase ): _UpperCAmelCase : Optional[int] =f"{var_name} must be a list of strings" raise ValueError(__lowerCamelCase ) def lowerCamelCase__ ( __lowerCamelCase : Any , __lowerCamelCase : Any , __lowerCamelCase : Any , ): '''simple docstring''' _validate_dict(__lowerCamelCase , 'initial_probabilities' , __lowerCamelCase ) _validate_nested_dict(__lowerCamelCase , 'transition_probabilities' ) _validate_nested_dict(__lowerCamelCase , 'emission_probabilities' ) def lowerCamelCase__ ( __lowerCamelCase : Any , __lowerCamelCase : str ): '''simple docstring''' _validate_dict(_object , __lowerCamelCase , __lowerCamelCase ) for x in _object.values(): _validate_dict(__lowerCamelCase , __lowerCamelCase , __lowerCamelCase , __lowerCamelCase ) def lowerCamelCase__ ( __lowerCamelCase : Any , __lowerCamelCase : str , __lowerCamelCase : type , __lowerCamelCase : bool = False ): '''simple docstring''' if not isinstance(_object , __lowerCamelCase ): _UpperCAmelCase : List[str] =f"{var_name} must be a dict" raise ValueError(__lowerCamelCase ) if not all(isinstance(__lowerCamelCase , __lowerCamelCase ) for x in _object ): _UpperCAmelCase : str =f"{var_name} all keys must be strings" raise ValueError(__lowerCamelCase ) if not all(isinstance(__lowerCamelCase , __lowerCamelCase ) for x in _object.values() ): _UpperCAmelCase : int ='nested dictionary ' if nested else '' _UpperCAmelCase : Optional[int] =f"{var_name} {nested_text}all values must be {value_type.__name__}" raise ValueError(__lowerCamelCase ) if __name__ == "__main__": from doctest import testmod testmod()
242
0
"""simple docstring""" # Copyright 2023 The HuggingFace Team. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available _SCREAMING_SNAKE_CASE : Optional[Any] = { '''configuration_xmod''': [ '''XMOD_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''XmodConfig''', '''XmodOnnxConfig''', ], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _SCREAMING_SNAKE_CASE : Tuple = [ '''XMOD_PRETRAINED_MODEL_ARCHIVE_LIST''', '''XmodForCausalLM''', '''XmodForMaskedLM''', '''XmodForMultipleChoice''', '''XmodForQuestionAnswering''', '''XmodForSequenceClassification''', '''XmodForTokenClassification''', '''XmodModel''', '''XmodPreTrainedModel''', ] if TYPE_CHECKING: from .configuration_xmod import XMOD_PRETRAINED_CONFIG_ARCHIVE_MAP, XmodConfig, XmodOnnxConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_xmod import ( XMOD_PRETRAINED_MODEL_ARCHIVE_LIST, XmodForCausalLM, XmodForMaskedLM, XmodForMultipleChoice, XmodForQuestionAnswering, XmodForSequenceClassification, XmodForTokenClassification, XmodModel, XmodPreTrainedModel, ) else: import sys _SCREAMING_SNAKE_CASE : Dict = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
183
"""simple docstring""" import importlib import shutil import threading import warnings from typing import List import fsspec import fsspec.asyn from . import compression from .hffilesystem import HfFileSystem _SCREAMING_SNAKE_CASE : Any = importlib.util.find_spec('''s3fs''') is not None if _has_safs: from .safilesystem import SaFileSystem # noqa: F401 _SCREAMING_SNAKE_CASE : List[compression.BaseCompressedFileFileSystem] = [ compression.BzaFileSystem, compression.GzipFileSystem, compression.LzaFileSystem, compression.XzFileSystem, compression.ZstdFileSystem, ] # Register custom filesystems for fs_class in COMPRESSION_FILESYSTEMS + [HfFileSystem]: if fs_class.protocol in fsspec.registry and fsspec.registry[fs_class.protocol] is not fs_class: warnings.warn(F'''A filesystem protocol was already set for {fs_class.protocol} and will be overwritten.''') fsspec.register_implementation(fs_class.protocol, fs_class, clobber=True) def lowerCamelCase__ ( _lowerCamelCase : str ) -> str: if "://" in dataset_path: lowerCamelCase_ = dataset_path.split('://' )[1] return dataset_path def lowerCamelCase__ ( _lowerCamelCase : fsspec.AbstractFileSystem ) -> bool: if fs is not None and fs.protocol != "file": return True else: return False def lowerCamelCase__ ( _lowerCamelCase : fsspec.AbstractFileSystem , _lowerCamelCase : str , _lowerCamelCase : str ) -> int: lowerCamelCase_ = not is_remote_filesystem(_lowerCamelCase ) if is_local: # LocalFileSystem.mv does copy + rm, it is more efficient to simply move a local directory shutil.move(fs._strip_protocol(_lowerCamelCase ) , fs._strip_protocol(_lowerCamelCase ) ) else: fs.mv(_lowerCamelCase , _lowerCamelCase , recursive=_lowerCamelCase ) def lowerCamelCase__ ( ) -> None: if hasattr(fsspec.asyn , 'reset_lock' ): # for future fsspec>2022.05.0 fsspec.asyn.reset_lock() else: lowerCamelCase_ = None lowerCamelCase_ = None lowerCamelCase_ = threading.Lock()
183
1
'''simple docstring''' def lowerCAmelCase_ ( snake_case__ , snake_case__ ): '''simple docstring''' return abs(snake_case__ ) if a == 0 else greatest_common_divisor(b % a , snake_case__ ) def lowerCAmelCase_ ( snake_case__ , snake_case__ ): '''simple docstring''' while y: # --> when y=0 then loop will terminate and return x as final GCD. A, A : Any = y, x % y return abs(snake_case__ ) def lowerCAmelCase_ ( ): '''simple docstring''' try: A : List[Any] = input('''Enter two integers separated by comma (,): ''' ).split(''',''' ) A : Union[str, Any] = int(nums[0] ) A : str = int(nums[1] ) print( F'greatest_common_divisor({num_a}, {num_a}) = ' F'{greatest_common_divisor(snake_case__ , snake_case__ )}' ) print(F'By iterative gcd({num_a}, {num_a}) = {gcd_by_iterative(snake_case__ , snake_case__ )}' ) except (IndexError, UnboundLocalError, ValueError): print('''Wrong input''' ) if __name__ == "__main__": main()
360
'''simple docstring''' import colorsys from PIL import Image # type: ignore def lowerCAmelCase_ ( snake_case__ , snake_case__ , snake_case__ ): '''simple docstring''' A : Optional[int] = x A : str = y for step in range(snake_case__ ): # noqa: B007 A : str = a * a - b * b + x A : List[str] = 2 * a * b + y A : str = a_new # divergence happens for all complex number with an absolute value # greater than 4 if a * a + b * b > 4: break return step / (max_step - 1) def lowerCAmelCase_ ( snake_case__ ): '''simple docstring''' if distance == 1: return (0, 0, 0) else: return (255, 255, 255) def lowerCAmelCase_ ( snake_case__ ): '''simple docstring''' if distance == 1: return (0, 0, 0) else: return tuple(round(i * 255 ) for i in colorsys.hsv_to_rgb(snake_case__ , 1 , 1 ) ) def lowerCAmelCase_ ( snake_case__ = 800 , snake_case__ = 600 , snake_case__ = -0.6 , snake_case__ = 0 , snake_case__ = 3.2 , snake_case__ = 50 , snake_case__ = True , ): '''simple docstring''' A : List[Any] = Image.new('''RGB''' , (image_width, image_height) ) A : Tuple = img.load() # loop through the image-coordinates for image_x in range(snake_case__ ): for image_y in range(snake_case__ ): # determine the figure-coordinates based on the image-coordinates A : Optional[int] = figure_width / image_width * image_height A : Tuple = figure_center_x + (image_x / image_width - 0.5) * figure_width A : List[str] = figure_center_y + (image_y / image_height - 0.5) * figure_height A : str = get_distance(snake_case__ , snake_case__ , snake_case__ ) # color the corresponding pixel based on the selected coloring-function if use_distance_color_coding: A : str = get_color_coded_rgb(snake_case__ ) else: A : List[Any] = get_black_and_white_rgb(snake_case__ ) return img if __name__ == "__main__": import doctest doctest.testmod() # colored version, full figure lowercase : Optional[Any] = get_image() # uncomment for colored version, different section, zoomed in # img = get_image(figure_center_x = -0.6, figure_center_y = -0.4, # figure_width = 0.8) # uncomment for black and white version, full figure # img = get_image(use_distance_color_coding = False) # uncomment to save the image # img.save("mandelbrot.png") img.show()
311
0
"""simple docstring""" from ...configuration_utils import PretrainedConfig from ...utils import logging __magic_name__ = logging.get_logger(__name__) __magic_name__ = { "facebook/dpr-ctx_encoder-single-nq-base": ( "https://huggingface.co/facebook/dpr-ctx_encoder-single-nq-base/resolve/main/config.json" ), "facebook/dpr-question_encoder-single-nq-base": ( "https://huggingface.co/facebook/dpr-question_encoder-single-nq-base/resolve/main/config.json" ), "facebook/dpr-reader-single-nq-base": ( "https://huggingface.co/facebook/dpr-reader-single-nq-base/resolve/main/config.json" ), "facebook/dpr-ctx_encoder-multiset-base": ( "https://huggingface.co/facebook/dpr-ctx_encoder-multiset-base/resolve/main/config.json" ), "facebook/dpr-question_encoder-multiset-base": ( "https://huggingface.co/facebook/dpr-question_encoder-multiset-base/resolve/main/config.json" ), "facebook/dpr-reader-multiset-base": ( "https://huggingface.co/facebook/dpr-reader-multiset-base/resolve/main/config.json" ), } class SCREAMING_SNAKE_CASE_ ( __a ): """simple docstring""" __lowercase : Optional[int] = '''dpr''' def __init__( self , lowerCAmelCase__=3_0_5_2_2 , lowerCAmelCase__=7_6_8 , lowerCAmelCase__=1_2 , lowerCAmelCase__=1_2 , lowerCAmelCase__=3_0_7_2 , lowerCAmelCase__="gelu" , lowerCAmelCase__=0.1 , lowerCAmelCase__=0.1 , lowerCAmelCase__=5_1_2 , lowerCAmelCase__=2 , lowerCAmelCase__=0.02 , lowerCAmelCase__=1E-12 , lowerCAmelCase__=0 , lowerCAmelCase__="absolute" , lowerCAmelCase__ = 0 , **lowerCAmelCase__ , ): super().__init__(pad_token_id=lowerCAmelCase__ , **lowerCAmelCase__) __SCREAMING_SNAKE_CASE = vocab_size __SCREAMING_SNAKE_CASE = hidden_size __SCREAMING_SNAKE_CASE = num_hidden_layers __SCREAMING_SNAKE_CASE = num_attention_heads __SCREAMING_SNAKE_CASE = hidden_act __SCREAMING_SNAKE_CASE = intermediate_size __SCREAMING_SNAKE_CASE = hidden_dropout_prob __SCREAMING_SNAKE_CASE = attention_probs_dropout_prob __SCREAMING_SNAKE_CASE = max_position_embeddings __SCREAMING_SNAKE_CASE = type_vocab_size __SCREAMING_SNAKE_CASE = initializer_range __SCREAMING_SNAKE_CASE = layer_norm_eps __SCREAMING_SNAKE_CASE = projection_dim __SCREAMING_SNAKE_CASE = position_embedding_type
100
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_tf_available, is_torch_available, ) lowerCAmelCase :Union[str, Any] = { '''configuration_vision_encoder_decoder''': ['''VisionEncoderDecoderConfig''', '''VisionEncoderDecoderOnnxConfig'''] } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase :str = ['''VisionEncoderDecoderModel'''] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase :Optional[int] = ['''TFVisionEncoderDecoderModel'''] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase :Union[str, Any] = ['''FlaxVisionEncoderDecoderModel'''] if TYPE_CHECKING: from .configuration_vision_encoder_decoder import VisionEncoderDecoderConfig, VisionEncoderDecoderOnnxConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_vision_encoder_decoder import VisionEncoderDecoderModel try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_vision_encoder_decoder import TFVisionEncoderDecoderModel try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_flax_vision_encoder_decoder import FlaxVisionEncoderDecoderModel else: import sys lowerCAmelCase :int = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
331
0
import importlib import shutil import threading import warnings from typing import List import fsspec import fsspec.asyn from . import compression from .hffilesystem import HfFileSystem __SCREAMING_SNAKE_CASE = importlib.util.find_spec("""s3fs""") is not None if _has_safs: from .safilesystem import SaFileSystem # noqa: F401 __SCREAMING_SNAKE_CASE = [ compression.BzaFileSystem, compression.GzipFileSystem, compression.LzaFileSystem, compression.XzFileSystem, compression.ZstdFileSystem, ] # Register custom filesystems for fs_class in COMPRESSION_FILESYSTEMS + [HfFileSystem]: if fs_class.protocol in fsspec.registry and fsspec.registry[fs_class.protocol] is not fs_class: warnings.warn(F"""A filesystem protocol was already set for {fs_class.protocol} and will be overwritten.""") fsspec.register_implementation(fs_class.protocol, fs_class, clobber=True) def UpperCAmelCase ( _lowerCamelCase ): if "://" in dataset_path: A : Dict = dataset_path.split("://" )[1] return dataset_path def UpperCAmelCase ( _lowerCamelCase ): if fs is not None and fs.protocol != "file": return True else: return False def UpperCAmelCase ( _lowerCamelCase , _lowerCamelCase , _lowerCamelCase ): A : Any = not is_remote_filesystem(_lowerCamelCase ) if is_local: # LocalFileSystem.mv does copy + rm, it is more efficient to simply move a local directory shutil.move(fs._strip_protocol(_lowerCamelCase ) , fs._strip_protocol(_lowerCamelCase ) ) else: fs.mv(_lowerCamelCase , _lowerCamelCase , recursive=_lowerCamelCase ) def UpperCAmelCase ( ): if hasattr(fsspec.asyn , "reset_lock" ): # for future fsspec>2022.05.0 fsspec.asyn.reset_lock() else: A : Any = None A : Optional[Any] = None A : Optional[Any] = threading.Lock()
256
import json from typing import List, Optional, Tuple from tokenizers import normalizers from ...tokenization_utils_fast import PreTrainedTokenizerFast from .tokenization_electra import ElectraTokenizer __SCREAMING_SNAKE_CASE = {"""vocab_file""": """vocab.txt""", """tokenizer_file""": """tokenizer.json"""} __SCREAMING_SNAKE_CASE = { """vocab_file""": { """google/electra-small-generator""": ( """https://huggingface.co/google/electra-small-generator/resolve/main/vocab.txt""" ), """google/electra-base-generator""": """https://huggingface.co/google/electra-base-generator/resolve/main/vocab.txt""", """google/electra-large-generator""": ( """https://huggingface.co/google/electra-large-generator/resolve/main/vocab.txt""" ), """google/electra-small-discriminator""": ( """https://huggingface.co/google/electra-small-discriminator/resolve/main/vocab.txt""" ), """google/electra-base-discriminator""": ( """https://huggingface.co/google/electra-base-discriminator/resolve/main/vocab.txt""" ), """google/electra-large-discriminator""": ( """https://huggingface.co/google/electra-large-discriminator/resolve/main/vocab.txt""" ), }, """tokenizer_file""": { """google/electra-small-generator""": ( """https://huggingface.co/google/electra-small-generator/resolve/main/tokenizer.json""" ), """google/electra-base-generator""": ( """https://huggingface.co/google/electra-base-generator/resolve/main/tokenizer.json""" ), """google/electra-large-generator""": ( """https://huggingface.co/google/electra-large-generator/resolve/main/tokenizer.json""" ), """google/electra-small-discriminator""": ( """https://huggingface.co/google/electra-small-discriminator/resolve/main/tokenizer.json""" ), """google/electra-base-discriminator""": ( """https://huggingface.co/google/electra-base-discriminator/resolve/main/tokenizer.json""" ), """google/electra-large-discriminator""": ( """https://huggingface.co/google/electra-large-discriminator/resolve/main/tokenizer.json""" ), }, } __SCREAMING_SNAKE_CASE = { """google/electra-small-generator""": 512, """google/electra-base-generator""": 512, """google/electra-large-generator""": 512, """google/electra-small-discriminator""": 512, """google/electra-base-discriminator""": 512, """google/electra-large-discriminator""": 512, } __SCREAMING_SNAKE_CASE = { """google/electra-small-generator""": {"""do_lower_case""": True}, """google/electra-base-generator""": {"""do_lower_case""": True}, """google/electra-large-generator""": {"""do_lower_case""": True}, """google/electra-small-discriminator""": {"""do_lower_case""": True}, """google/electra-base-discriminator""": {"""do_lower_case""": True}, """google/electra-large-discriminator""": {"""do_lower_case""": True}, } class lowerCamelCase_ ( _A ): '''simple docstring''' a__ = VOCAB_FILES_NAMES a__ = PRETRAINED_VOCAB_FILES_MAP a__ = PRETRAINED_INIT_CONFIGURATION a__ = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES a__ = ElectraTokenizer def __init__( self : int , __lowerCamelCase : str=None , __lowerCamelCase : Optional[int]=None , __lowerCamelCase : Tuple=True , __lowerCamelCase : int="[UNK]" , __lowerCamelCase : Any="[SEP]" , __lowerCamelCase : Union[str, Any]="[PAD]" , __lowerCamelCase : str="[CLS]" , __lowerCamelCase : Tuple="[MASK]" , __lowerCamelCase : Union[str, Any]=True , __lowerCamelCase : str=None , **__lowerCamelCase : str , ) -> List[str]: super().__init__( __lowerCamelCase , tokenizer_file=__lowerCamelCase , do_lower_case=__lowerCamelCase , unk_token=__lowerCamelCase , sep_token=__lowerCamelCase , pad_token=__lowerCamelCase , cls_token=__lowerCamelCase , mask_token=__lowerCamelCase , tokenize_chinese_chars=__lowerCamelCase , strip_accents=__lowerCamelCase , **__lowerCamelCase , ) A : Dict = json.loads(self.backend_tokenizer.normalizer.__getstate__() ) if ( normalizer_state.get("lowercase" , __lowerCamelCase ) != do_lower_case or normalizer_state.get("strip_accents" , __lowerCamelCase ) != strip_accents or normalizer_state.get("handle_chinese_chars" , __lowerCamelCase ) != tokenize_chinese_chars ): A : Union[str, Any] = getattr(__lowerCamelCase , normalizer_state.pop("type" ) ) A : List[Any] = do_lower_case A : Tuple = strip_accents A : Any = tokenize_chinese_chars A : Tuple = normalizer_class(**__lowerCamelCase ) A : Optional[Any] = do_lower_case def SCREAMING_SNAKE_CASE__ ( self : Optional[int] , __lowerCamelCase : Optional[Any] , __lowerCamelCase : Dict=None ) -> List[Any]: A : Tuple = [self.cls_token_id] + token_ids_a + [self.sep_token_id] if token_ids_a: output += token_ids_a + [self.sep_token_id] return output def SCREAMING_SNAKE_CASE__ ( self : Tuple , __lowerCamelCase : List[int] , __lowerCamelCase : Optional[List[int]] = None ) -> List[int]: A : int = [self.sep_token_id] A : Optional[int] = [self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep ) * [0] return len(cls + token_ids_a + sep ) * [0] + len(token_ids_a + sep ) * [1] def SCREAMING_SNAKE_CASE__ ( self : Any , __lowerCamelCase : str , __lowerCamelCase : Optional[str] = None ) -> Tuple[str]: A : List[Any] = self._tokenizer.model.save(__lowerCamelCase , name=__lowerCamelCase ) return tuple(__lowerCamelCase )
256
1
def _a ( a :int ) -> list[int]: if num <= 0: raise ValueError('''Input must be a positive integer''' ) a = [True] * (num + 1) a = 2 while p * p <= num: if primes[p]: for i in range(p * p , num + 1 , a ): a = False p += 1 return [prime for prime in range(2 , num + 1 ) if primes[prime]] if __name__ == "__main__": import doctest doctest.testmod() UpperCAmelCase__ = int(input("Enter a positive integer: ").strip()) print(prime_sieve_eratosthenes(user_num))
0
import fire from torch.utils.data import DataLoader from tqdm import tqdm from transformers import AutoTokenizer from utils import SeqaSeqDataset, pickle_save def __magic_name__ ( __a : Optional[int] , __a : Union[str, Any] , __a : Union[str, Any]=1_024 , __a : str=1_024 , __a : Optional[Any]=False , **__a : Tuple ): '''simple docstring''' UpperCamelCase__ = AutoTokenizer.from_pretrained(__a ) UpperCamelCase__ = SeqaSeqDataset(__a , __a , __a , __a , type_path="""train""" , **__a ) UpperCamelCase__ = tok.pad_token_id def get_lens(__a : Optional[int] ): UpperCamelCase__ = tqdm( DataLoader(__a , batch_size=512 , num_workers=8 , shuffle=__a , collate_fn=ds.collate_fn ) , desc=str(ds.len_file ) , ) UpperCamelCase__ = [] for batch in dl: UpperCamelCase__ = batch["""input_ids"""].ne(__a ).sum(1 ).tolist() UpperCamelCase__ = batch["""labels"""].ne(__a ).sum(1 ).tolist() if consider_target: for src, tgt in zip(__a , __a ): max_lens.append(max(__a , __a ) ) else: max_lens.extend(__a ) return max_lens UpperCamelCase__ = get_lens(__a ) UpperCamelCase__ = SeqaSeqDataset(__a , __a , __a , __a , type_path="""val""" , **__a ) UpperCamelCase__ = get_lens(__a ) pickle_save(__a , train_ds.len_file ) pickle_save(__a , val_ds.len_file ) if __name__ == "__main__": fire.Fire(save_len_file)
244
0
"""simple docstring""" import unittest import numpy as np from datasets import load_dataset from transformers.testing_utils import require_torch, require_vision from transformers.utils import is_torch_available, is_vision_available from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs if is_torch_available(): import torch if is_vision_available(): from PIL import Image from transformers import BeitImageProcessor class UpperCamelCase ( unittest.TestCase ): def __init__( self : Optional[Any] , UpperCAmelCase__ : Dict , UpperCAmelCase__ : Tuple=7 , UpperCAmelCase__ : Tuple=3 , UpperCAmelCase__ : Any=18 , UpperCAmelCase__ : List[str]=30 , UpperCAmelCase__ : Tuple=400 , UpperCAmelCase__ : int=True , UpperCAmelCase__ : str=None , UpperCAmelCase__ : Optional[Any]=True , UpperCAmelCase__ : Optional[Any]=None , UpperCAmelCase__ : str=True , UpperCAmelCase__ : Union[str, Any]=[0.5, 0.5, 0.5] , UpperCAmelCase__ : List[str]=[0.5, 0.5, 0.5] , UpperCAmelCase__ : List[Any]=False , ) -> Optional[Any]: _a : Union[str, Any] = size if size is not None else {"""height""": 20, """width""": 20} _a : int = crop_size if crop_size is not None else {"""height""": 18, """width""": 18} _a : Optional[int] = parent _a : int = batch_size _a : List[Any] = num_channels _a : Dict = image_size _a : Optional[Any] = min_resolution _a : Any = max_resolution _a : Dict = do_resize _a : Union[str, Any] = size _a : str = do_center_crop _a : Tuple = crop_size _a : int = do_normalize _a : Optional[Any] = image_mean _a : List[str] = image_std _a : int = do_reduce_labels def _lowercase ( self : Dict ) -> Any: 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_reduce_labels": self.do_reduce_labels, } def lowerCAmelCase__ ( ): '''simple docstring''' _a : Dict = load_dataset("""hf-internal-testing/fixtures_ade20k""" , split="""test""" ) _a : Optional[int] = Image.open(dataset[0]["""file"""] ) _a : Tuple = Image.open(dataset[1]["""file"""] ) return image, map def lowerCAmelCase__ ( ): '''simple docstring''' _a : List[Any] = load_dataset("""hf-internal-testing/fixtures_ade20k""" , split="""test""" ) _a : List[Any] = Image.open(ds[0]["""file"""] ) _a : Tuple = Image.open(ds[1]["""file"""] ) _a : List[Any] = Image.open(ds[2]["""file"""] ) _a : List[Any] = Image.open(ds[3]["""file"""] ) return [imagea, imagea], [mapa, mapa] @require_torch @require_vision class UpperCamelCase ( snake_case_ , unittest.TestCase ): UpperCamelCase : List[str] = BeitImageProcessor if is_vision_available() else None def _lowercase ( self : Tuple ) -> Optional[Any]: _a : Union[str, Any] = BeitImageProcessingTester(self ) @property def _lowercase ( self : Optional[Any] ) -> Union[str, Any]: return self.image_processor_tester.prepare_image_processor_dict() def _lowercase ( self : Dict ) -> Tuple: _a : Any = self.image_processing_class(**self.image_processor_dict ) self.assertTrue(hasattr(UpperCAmelCase__ , """do_resize""" ) ) self.assertTrue(hasattr(UpperCAmelCase__ , """size""" ) ) self.assertTrue(hasattr(UpperCAmelCase__ , """do_center_crop""" ) ) self.assertTrue(hasattr(UpperCAmelCase__ , """center_crop""" ) ) self.assertTrue(hasattr(UpperCAmelCase__ , """do_normalize""" ) ) self.assertTrue(hasattr(UpperCAmelCase__ , """image_mean""" ) ) self.assertTrue(hasattr(UpperCAmelCase__ , """image_std""" ) ) def _lowercase ( self : Any ) -> Any: _a : List[str] = self.image_processing_class.from_dict(self.image_processor_dict ) self.assertEqual(image_processor.size , {"""height""": 20, """width""": 20} ) self.assertEqual(image_processor.crop_size , {"""height""": 18, """width""": 18} ) self.assertEqual(image_processor.do_reduce_labels , UpperCAmelCase__ ) _a : List[str] = self.image_processing_class.from_dict( self.image_processor_dict , size=42 , crop_size=84 , reduce_labels=UpperCAmelCase__ ) self.assertEqual(image_processor.size , {"""height""": 42, """width""": 42} ) self.assertEqual(image_processor.crop_size , {"""height""": 84, """width""": 84} ) self.assertEqual(image_processor.do_reduce_labels , UpperCAmelCase__ ) def _lowercase ( self : List[str] ) -> int: pass def _lowercase ( self : Any ) -> Dict: # Initialize image_processing _a : List[Any] = self.image_processing_class(**self.image_processor_dict ) # create random PIL images _a : str = prepare_image_inputs(self.image_processor_tester , equal_resolution=UpperCAmelCase__ ) for image in image_inputs: self.assertIsInstance(UpperCAmelCase__ , Image.Image ) # Test not batched input _a : int = image_processing(image_inputs[0] , return_tensors="""pt""" ).pixel_values self.assertEqual( encoded_images.shape , ( 1, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size["""height"""], self.image_processor_tester.crop_size["""width"""], ) , ) # Test batched _a : Dict = image_processing(UpperCAmelCase__ , return_tensors="""pt""" ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size["""height"""], self.image_processor_tester.crop_size["""width"""], ) , ) def _lowercase ( self : Union[str, Any] ) -> Tuple: # Initialize image_processing _a : List[Any] = self.image_processing_class(**self.image_processor_dict ) # create random numpy tensors _a : Dict = prepare_image_inputs(self.image_processor_tester , equal_resolution=UpperCAmelCase__ , numpify=UpperCAmelCase__ ) for image in image_inputs: self.assertIsInstance(UpperCAmelCase__ , np.ndarray ) # Test not batched input _a : str = image_processing(image_inputs[0] , return_tensors="""pt""" ).pixel_values self.assertEqual( encoded_images.shape , ( 1, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size["""height"""], self.image_processor_tester.crop_size["""width"""], ) , ) # Test batched _a : int = image_processing(UpperCAmelCase__ , return_tensors="""pt""" ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size["""height"""], self.image_processor_tester.crop_size["""width"""], ) , ) def _lowercase ( self : Optional[Any] ) -> List[str]: # Initialize image_processing _a : int = self.image_processing_class(**self.image_processor_dict ) # create random PyTorch tensors _a : int = prepare_image_inputs(self.image_processor_tester , equal_resolution=UpperCAmelCase__ , torchify=UpperCAmelCase__ ) for image in image_inputs: self.assertIsInstance(UpperCAmelCase__ , torch.Tensor ) # Test not batched input _a : Optional[int] = image_processing(image_inputs[0] , return_tensors="""pt""" ).pixel_values self.assertEqual( encoded_images.shape , ( 1, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size["""height"""], self.image_processor_tester.crop_size["""width"""], ) , ) # Test batched _a : Dict = image_processing(UpperCAmelCase__ , return_tensors="""pt""" ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size["""height"""], self.image_processor_tester.crop_size["""width"""], ) , ) def _lowercase ( self : int ) -> Union[str, Any]: # Initialize image_processing _a : Optional[int] = self.image_processing_class(**self.image_processor_dict ) # create random PyTorch tensors _a : Optional[Any] = prepare_image_inputs(self.image_processor_tester , equal_resolution=UpperCAmelCase__ , torchify=UpperCAmelCase__ ) _a : List[Any] = [] for image in image_inputs: self.assertIsInstance(UpperCAmelCase__ , torch.Tensor ) maps.append(torch.zeros(image.shape[-2:] ).long() ) # Test not batched input _a : int = image_processing(image_inputs[0] , maps[0] , return_tensors="""pt""" ) self.assertEqual( encoding["""pixel_values"""].shape , ( 1, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size["""height"""], self.image_processor_tester.crop_size["""width"""], ) , ) self.assertEqual( encoding["""labels"""].shape , ( 1, self.image_processor_tester.crop_size["""height"""], self.image_processor_tester.crop_size["""width"""], ) , ) self.assertEqual(encoding["""labels"""].dtype , torch.long ) self.assertTrue(encoding["""labels"""].min().item() >= 0 ) self.assertTrue(encoding["""labels"""].max().item() <= 255 ) # Test batched _a : List[Any] = image_processing(UpperCAmelCase__ , UpperCAmelCase__ , return_tensors="""pt""" ) self.assertEqual( encoding["""pixel_values"""].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"""], ) , ) self.assertEqual( encoding["""labels"""].shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.crop_size["""height"""], self.image_processor_tester.crop_size["""width"""], ) , ) self.assertEqual(encoding["""labels"""].dtype , torch.long ) self.assertTrue(encoding["""labels"""].min().item() >= 0 ) self.assertTrue(encoding["""labels"""].max().item() <= 255 ) # Test not batched input (PIL images) _a : Optional[int] = prepare_semantic_single_inputs() _a : List[str] = image_processing(UpperCAmelCase__ , UpperCAmelCase__ , return_tensors="""pt""" ) self.assertEqual( encoding["""pixel_values"""].shape , ( 1, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size["""height"""], self.image_processor_tester.crop_size["""width"""], ) , ) self.assertEqual( encoding["""labels"""].shape , ( 1, self.image_processor_tester.crop_size["""height"""], self.image_processor_tester.crop_size["""width"""], ) , ) self.assertEqual(encoding["""labels"""].dtype , torch.long ) self.assertTrue(encoding["""labels"""].min().item() >= 0 ) self.assertTrue(encoding["""labels"""].max().item() <= 255 ) # Test batched input (PIL images) _a : Any = prepare_semantic_batch_inputs() _a : int = image_processing(UpperCAmelCase__ , UpperCAmelCase__ , return_tensors="""pt""" ) self.assertEqual( encoding["""pixel_values"""].shape , ( 2, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size["""height"""], self.image_processor_tester.crop_size["""width"""], ) , ) self.assertEqual( encoding["""labels"""].shape , ( 2, self.image_processor_tester.crop_size["""height"""], self.image_processor_tester.crop_size["""width"""], ) , ) self.assertEqual(encoding["""labels"""].dtype , torch.long ) self.assertTrue(encoding["""labels"""].min().item() >= 0 ) self.assertTrue(encoding["""labels"""].max().item() <= 255 ) def _lowercase ( self : Optional[int] ) -> Dict: # Initialize image_processing _a : int = self.image_processing_class(**self.image_processor_dict ) # ADE20k has 150 classes, and the background is included, so labels should be between 0 and 150 _a : Union[str, Any] = prepare_semantic_single_inputs() _a : Union[str, Any] = image_processing(UpperCAmelCase__ , UpperCAmelCase__ , return_tensors="""pt""" ) self.assertTrue(encoding["""labels"""].min().item() >= 0 ) self.assertTrue(encoding["""labels"""].max().item() <= 150 ) _a : List[str] = True _a : Union[str, Any] = image_processing(UpperCAmelCase__ , UpperCAmelCase__ , return_tensors="""pt""" ) self.assertTrue(encoding["""labels"""].min().item() >= 0 ) self.assertTrue(encoding["""labels"""].max().item() <= 255 )
364
"""simple docstring""" def lowerCAmelCase__ ( UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ ): '''simple docstring''' _a , _a : Dict = len(UpperCamelCase__ ), len(grid[0] ) if ( min(UpperCamelCase__ , UpperCamelCase__ ) < 0 or row == row_length or col == col_length or (row, col) in visit or grid[row][col] == 1 ): return 0 if row == row_length - 1 and col == col_length - 1: return 1 visit.add((row, col) ) _a : Any = 0 count += depth_first_search(UpperCamelCase__ , row + 1 , UpperCamelCase__ , UpperCamelCase__ ) count += depth_first_search(UpperCamelCase__ , row - 1 , UpperCamelCase__ , UpperCamelCase__ ) count += depth_first_search(UpperCamelCase__ , UpperCamelCase__ , col + 1 , UpperCamelCase__ ) count += depth_first_search(UpperCamelCase__ , UpperCamelCase__ , col - 1 , UpperCamelCase__ ) visit.remove((row, col) ) return count if __name__ == "__main__": import doctest doctest.testmod()
324
0
"""simple docstring""" from typing import Dict, List, Optional from ...tokenization_utils import AddedToken, PreTrainedTokenizer from ...utils import logging _a = logging.get_logger(__name__) _a = { 'nielsr/canine-s': 2_048, } # Unicode defines 1,114,112 total “codepoints” _a = 1_114_112 # Below: Constants defining canonical codepoints for special, pseudo-characters. # Copied from https://github.com/google-research/language/blob/master/language/canine/special_codepoints.py _a = 0 _a = 0xe000 _a = 0xe001 _a = 0xe002 _a = 0xe003 _a = 0xe004 # Maps special codepoints to human-readable names. _a = { # Special symbols are represented using codepoints values that are valid, # but designated as "Private Use", meaning that they will never be assigned # characters by the Unicode Consortium, and are thus safe for use here. # # NOTE: Do *NOT* add any sort of [UNK_CHAR] here. They are explicitly # excluded and should fail with a hard error. CLS: "[CLS]", SEP: "[SEP]", BOS: "[BOS]", MASK: "[MASK]", PAD: "[PAD]", RESERVED: "[RESERVED]", } # Maps special codepoint human-readable names to their codepoint values. _a = {name: codepoint for codepoint, name in SPECIAL_CODEPOINTS.items()} class A_ (lowercase__ ): '''simple docstring''' SCREAMING_SNAKE_CASE__ : List[str] = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES def __init__( self , lowercase_=chr(lowercase_ ) , lowercase_=chr(lowercase_ ) , lowercase_=chr(lowercase_ ) , lowercase_=chr(lowercase_ ) , lowercase_=chr(lowercase_ ) , lowercase_=chr(lowercase_ ) , lowercase_=False , lowercase_=2048 , **lowercase_ , ): """simple docstring""" UpperCAmelCase_ : Tuple = AddedToken(lowercase_ , lstrip=lowercase_ , rstrip=lowercase_ ) if isinstance(lowercase_ , lowercase_ ) else bos_token UpperCAmelCase_ : Union[str, Any] = AddedToken(lowercase_ , lstrip=lowercase_ , rstrip=lowercase_ ) if isinstance(lowercase_ , lowercase_ ) else eos_token UpperCAmelCase_ : int = AddedToken(lowercase_ , lstrip=lowercase_ , rstrip=lowercase_ ) if isinstance(lowercase_ , lowercase_ ) else sep_token UpperCAmelCase_ : Any = AddedToken(lowercase_ , lstrip=lowercase_ , rstrip=lowercase_ ) if isinstance(lowercase_ , lowercase_ ) else cls_token UpperCAmelCase_ : Optional[Any] = AddedToken(lowercase_ , lstrip=lowercase_ , rstrip=lowercase_ ) if isinstance(lowercase_ , lowercase_ ) else pad_token # Mask token behave like a normal word, i.e. include the space before it UpperCAmelCase_ : Optional[Any] = AddedToken(lowercase_ , lstrip=lowercase_ , rstrip=lowercase_ ) if isinstance(lowercase_ , lowercase_ ) else mask_token super().__init__( bos_token=lowercase_ , eos_token=lowercase_ , sep_token=lowercase_ , cls_token=lowercase_ , pad_token=lowercase_ , mask_token=lowercase_ , add_prefix_space=lowercase_ , model_max_length=lowercase_ , **lowercase_ , ) # Creates a mapping for looking up the IDs of special symbols. UpperCAmelCase_ : Dict[str, int] = {} for codepoint, name in SPECIAL_CODEPOINTS.items(): UpperCAmelCase_ : Union[str, Any] = codepoint # Creates a mapping for looking up the string forms of special symbol IDs. UpperCAmelCase_ : Dict[int, str] = { codepoint: name for name, codepoint in self._special_codepoints.items() } UpperCAmelCase_ : str = UNICODE_VOCAB_SIZE UpperCAmelCase_ : Optional[int] = len(self._special_codepoints ) @property def UpperCamelCase__ ( self ): """simple docstring""" return self._unicode_vocab_size def UpperCamelCase__ ( self , lowercase_ ): """simple docstring""" return list(lowercase_ ) def UpperCamelCase__ ( self , lowercase_ ): """simple docstring""" try: return ord(lowercase_ ) except TypeError: raise ValueError(F"""invalid token: '{token}'""" ) def UpperCamelCase__ ( self , lowercase_ ): """simple docstring""" try: if index in SPECIAL_CODEPOINTS: return SPECIAL_CODEPOINTS[index] return chr(lowercase_ ) except TypeError: raise ValueError(F"""invalid id: {index}""" ) def UpperCamelCase__ ( self , lowercase_ ): """simple docstring""" return "".join(lowercase_ ) def UpperCamelCase__ ( self , lowercase_ , lowercase_ = None ): """simple docstring""" UpperCAmelCase_ : Optional[Any] = [self.sep_token_id] UpperCAmelCase_ : int = [self.cls_token_id] UpperCAmelCase_ : List[Any] = cls + token_ids_a + sep if token_ids_a is not None: result += token_ids_a + sep return result def UpperCamelCase__ ( self , lowercase_ , lowercase_ = None , lowercase_ = False ): """simple docstring""" if already_has_special_tokens: return super().get_special_tokens_mask( token_ids_a=lowercase_ , token_ids_a=lowercase_ , already_has_special_tokens=lowercase_ ) UpperCAmelCase_ : Any = [1] + ([0] * len(lowercase_ )) + [1] if token_ids_a is not None: result += ([0] * len(lowercase_ )) + [1] return result def UpperCamelCase__ ( self , lowercase_ , lowercase_ = None ): """simple docstring""" UpperCAmelCase_ : Tuple = [self.sep_token_id] UpperCAmelCase_ : int = [self.cls_token_id] UpperCAmelCase_ : Union[str, Any] = len(cls + token_ids_a + sep ) * [0] if token_ids_a is not None: result += len(token_ids_a + sep ) * [1] return result def UpperCamelCase__ ( self , lowercase_ , lowercase_ = None ): """simple docstring""" return ()
61
"""simple docstring""" import copy from ...configuration_utils import PretrainedConfig from ...utils import logging from ..auto import CONFIG_MAPPING _A = logging.get_logger(__name__) _A = { """SenseTime/deformable-detr""": """https://huggingface.co/sensetime/deformable-detr/resolve/main/config.json""", # See all Deformable DETR models at https://huggingface.co/models?filter=deformable-detr } class _lowerCamelCase ( a_ ): _lowerCamelCase :Any = "deformable_detr" _lowerCamelCase :Union[str, Any] = { "hidden_size": "d_model", "num_attention_heads": "encoder_attention_heads", } def __init__( self : int , UpperCamelCase : Optional[Any]=True , UpperCamelCase : str=None , UpperCamelCase : int=3 , UpperCamelCase : Dict=3_00 , UpperCamelCase : int=10_24 , UpperCamelCase : List[str]=6 , UpperCamelCase : Optional[Any]=10_24 , UpperCamelCase : Any=8 , UpperCamelCase : List[str]=6 , UpperCamelCase : Dict=10_24 , UpperCamelCase : Optional[Any]=8 , UpperCamelCase : Tuple=0.0 , UpperCamelCase : Optional[Any]=True , UpperCamelCase : int="relu" , UpperCamelCase : List[Any]=2_56 , UpperCamelCase : Union[str, Any]=0.1 , UpperCamelCase : Dict=0.0 , UpperCamelCase : Optional[Any]=0.0 , UpperCamelCase : Any=0.02 , UpperCamelCase : int=1.0 , UpperCamelCase : Dict=True , UpperCamelCase : Dict=False , UpperCamelCase : Any="sine" , UpperCamelCase : int="resnet50" , UpperCamelCase : str=True , UpperCamelCase : str=False , UpperCamelCase : Any=4 , UpperCamelCase : List[str]=4 , UpperCamelCase : Dict=4 , UpperCamelCase : List[str]=False , UpperCamelCase : Tuple=3_00 , UpperCamelCase : int=False , UpperCamelCase : List[str]=1 , UpperCamelCase : List[str]=5 , UpperCamelCase : str=2 , UpperCamelCase : List[Any]=1 , UpperCamelCase : Tuple=1 , UpperCamelCase : str=5 , UpperCamelCase : List[str]=2 , UpperCamelCase : Any=0.1 , UpperCamelCase : Union[str, Any]=0.25 , UpperCamelCase : Any=False , **UpperCamelCase : int , ) -> Dict: """simple docstring""" if backbone_config is not None and use_timm_backbone: raise ValueError("""You can't specify both `backbone_config` and `use_timm_backbone`.""" ) if not use_timm_backbone: if backbone_config is None: logger.info("""`backbone_config` is `None`. Initializing the config with the default `ResNet` backbone.""" ) lowerCAmelCase__ : List[str] = CONFIG_MAPPING["""resnet"""](out_features=["""stage4"""] ) elif isinstance(UpperCamelCase , UpperCamelCase ): lowerCAmelCase__ : Dict = backbone_config.get("""model_type""" ) lowerCAmelCase__ : Dict = CONFIG_MAPPING[backbone_model_type] lowerCAmelCase__ : Any = config_class.from_dict(UpperCamelCase ) lowerCAmelCase__ : List[str] = use_timm_backbone lowerCAmelCase__ : List[str] = backbone_config lowerCAmelCase__ : Dict = num_channels lowerCAmelCase__ : int = num_queries lowerCAmelCase__ : Any = max_position_embeddings lowerCAmelCase__ : List[Any] = d_model lowerCAmelCase__ : Tuple = encoder_ffn_dim lowerCAmelCase__ : Tuple = encoder_layers lowerCAmelCase__ : List[Any] = encoder_attention_heads lowerCAmelCase__ : Optional[int] = decoder_ffn_dim lowerCAmelCase__ : Dict = decoder_layers lowerCAmelCase__ : Any = decoder_attention_heads lowerCAmelCase__ : int = dropout lowerCAmelCase__ : List[str] = attention_dropout lowerCAmelCase__ : str = activation_dropout lowerCAmelCase__ : Optional[int] = activation_function lowerCAmelCase__ : Optional[Any] = init_std lowerCAmelCase__ : Optional[int] = init_xavier_std lowerCAmelCase__ : Optional[int] = encoder_layerdrop lowerCAmelCase__ : Any = auxiliary_loss lowerCAmelCase__ : List[str] = position_embedding_type lowerCAmelCase__ : Optional[Any] = backbone lowerCAmelCase__ : Tuple = use_pretrained_backbone lowerCAmelCase__ : Union[str, Any] = dilation # deformable attributes lowerCAmelCase__ : Optional[int] = num_feature_levels lowerCAmelCase__ : int = encoder_n_points lowerCAmelCase__ : Optional[int] = decoder_n_points lowerCAmelCase__ : Tuple = two_stage lowerCAmelCase__ : Any = two_stage_num_proposals lowerCAmelCase__ : Optional[int] = with_box_refine if two_stage is True and with_box_refine is False: raise ValueError("""If two_stage is True, with_box_refine must be True.""" ) # Hungarian matcher lowerCAmelCase__ : Union[str, Any] = class_cost lowerCAmelCase__ : Optional[int] = bbox_cost lowerCAmelCase__ : str = giou_cost # Loss coefficients lowerCAmelCase__ : Optional[Any] = mask_loss_coefficient lowerCAmelCase__ : Tuple = dice_loss_coefficient lowerCAmelCase__ : Optional[Any] = bbox_loss_coefficient lowerCAmelCase__ : Optional[int] = giou_loss_coefficient lowerCAmelCase__ : Optional[Any] = eos_coefficient lowerCAmelCase__ : Tuple = focal_alpha lowerCAmelCase__ : Tuple = disable_custom_kernels super().__init__(is_encoder_decoder=UpperCamelCase , **UpperCamelCase ) @property def _lowerCAmelCase ( self : str ) -> int: """simple docstring""" return self.encoder_attention_heads @property def _lowerCAmelCase ( self : Optional[int] ) -> int: """simple docstring""" return self.d_model def _lowerCAmelCase ( self : str ) -> List[str]: """simple docstring""" lowerCAmelCase__ : Dict = copy.deepcopy(self.__dict__ ) if self.backbone_config is not None: lowerCAmelCase__ : Union[str, Any] = self.backbone_config.to_dict() lowerCAmelCase__ : Union[str, Any] = self.__class__.model_type return output
242
0
"""simple docstring""" def SCREAMING_SNAKE_CASE__ ( snake_case : Optional[Any] )-> set: '''simple docstring''' UpperCAmelCase__ : Union[str, Any] = set() # edges = list of graph's edges UpperCAmelCase__ : List[str] = get_edges(__lowerCAmelCase ) # While there are still elements in edges list, take an arbitrary edge # (from_node, to_node) and add his extremity to chosen_vertices and then # remove all arcs adjacent to the from_node and to_node while edges: UpperCAmelCase__ : List[str] = edges.pop() chosen_vertices.add(__lowerCAmelCase ) chosen_vertices.add(__lowerCAmelCase ) for edge in edges.copy(): if from_node in edge or to_node in edge: edges.discard(__lowerCAmelCase ) return chosen_vertices def SCREAMING_SNAKE_CASE__ ( snake_case : Optional[Any] )-> set: '''simple docstring''' UpperCAmelCase__ : Tuple = set() for from_node, to_nodes in graph.items(): for to_node in to_nodes: edges.add((from_node, to_node) ) return edges if __name__ == "__main__": import doctest doctest.testmod() # graph = {0: [1, 3], 1: [0, 3], 2: [0, 3, 4], 3: [0, 1, 2], 4: [2, 3]} # print(f"Matching vertex cover:\n{matching_min_vertex_cover(graph)}")
362
"""simple docstring""" import unittest from transformers import AlbertConfig, is_torch_available from transformers.models.auto import get_values from transformers.testing_utils import require_torch, slow, torch_device from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, ids_tensor, random_attention_mask from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from transformers import ( MODEL_FOR_PRETRAINING_MAPPING, AlbertForMaskedLM, AlbertForMultipleChoice, AlbertForPreTraining, AlbertForQuestionAnswering, AlbertForSequenceClassification, AlbertForTokenClassification, AlbertModel, ) from transformers.models.albert.modeling_albert import ALBERT_PRETRAINED_MODEL_ARCHIVE_LIST class lowerCAmelCase__ : def __init__( self : Optional[Any] , snake_case__ : Optional[Any] , snake_case__ : Dict=1_3 , snake_case__ : List[str]=7 , snake_case__ : Union[str, Any]=True , snake_case__ : Tuple=True , snake_case__ : Optional[int]=True , snake_case__ : Any=True , snake_case__ : Any=9_9 , snake_case__ : List[Any]=1_6 , snake_case__ : Any=3_6 , snake_case__ : Union[str, Any]=6 , snake_case__ : Tuple=6 , snake_case__ : List[str]=6 , snake_case__ : List[str]=3_7 , snake_case__ : Dict="gelu" , snake_case__ : int=0.1 , snake_case__ : List[Any]=0.1 , snake_case__ : List[str]=5_1_2 , snake_case__ : Dict=1_6 , snake_case__ : str=2 , snake_case__ : Optional[Any]=0.02 , snake_case__ : List[str]=3 , snake_case__ : Any=4 , snake_case__ : int=None , ): '''simple docstring''' UpperCAmelCase__ : Tuple = parent UpperCAmelCase__ : int = batch_size UpperCAmelCase__ : int = seq_length UpperCAmelCase__ : List[str] = is_training UpperCAmelCase__ : Union[str, Any] = use_input_mask UpperCAmelCase__ : Optional[Any] = use_token_type_ids UpperCAmelCase__ : Any = use_labels UpperCAmelCase__ : List[Any] = vocab_size UpperCAmelCase__ : Any = embedding_size UpperCAmelCase__ : List[str] = hidden_size UpperCAmelCase__ : List[Any] = num_hidden_layers UpperCAmelCase__ : int = num_hidden_groups UpperCAmelCase__ : Union[str, Any] = num_attention_heads UpperCAmelCase__ : List[str] = intermediate_size UpperCAmelCase__ : Optional[Any] = hidden_act UpperCAmelCase__ : List[Any] = hidden_dropout_prob UpperCAmelCase__ : Tuple = attention_probs_dropout_prob UpperCAmelCase__ : str = max_position_embeddings UpperCAmelCase__ : Any = type_vocab_size UpperCAmelCase__ : Union[str, Any] = type_sequence_label_size UpperCAmelCase__ : Union[str, Any] = initializer_range UpperCAmelCase__ : Tuple = num_labels UpperCAmelCase__ : List[str] = num_choices UpperCAmelCase__ : Union[str, Any] = scope def __a ( self : int ): '''simple docstring''' UpperCAmelCase__ : List[Any] = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) UpperCAmelCase__ : Optional[int] = None if self.use_input_mask: UpperCAmelCase__ : Optional[Any] = random_attention_mask([self.batch_size, self.seq_length] ) UpperCAmelCase__ : Optional[int] = None if self.use_token_type_ids: UpperCAmelCase__ : List[str] = ids_tensor([self.batch_size, self.seq_length] , self.type_vocab_size ) UpperCAmelCase__ : List[Any] = None UpperCAmelCase__ : Optional[int] = None UpperCAmelCase__ : Any = None if self.use_labels: UpperCAmelCase__ : int = ids_tensor([self.batch_size] , self.type_sequence_label_size ) UpperCAmelCase__ : Optional[int] = ids_tensor([self.batch_size, self.seq_length] , self.num_labels ) UpperCAmelCase__ : Dict = ids_tensor([self.batch_size] , self.num_choices ) UpperCAmelCase__ : int = self.get_config() return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels def __a ( self : Any ): '''simple docstring''' return AlbertConfig( vocab_size=self.vocab_size , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , max_position_embeddings=self.max_position_embeddings , type_vocab_size=self.type_vocab_size , initializer_range=self.initializer_range , num_hidden_groups=self.num_hidden_groups , ) def __a ( self : List[Any] , snake_case__ : Optional[Any] , snake_case__ : List[Any] , snake_case__ : Optional[Any] , snake_case__ : Tuple , snake_case__ : List[str] , snake_case__ : List[Any] , snake_case__ : List[str] ): '''simple docstring''' UpperCAmelCase__ : str = AlbertModel(config=snake_case__ ) model.to(snake_case__ ) model.eval() UpperCAmelCase__ : Union[str, Any] = model(snake_case__ , attention_mask=snake_case__ , token_type_ids=snake_case__ ) UpperCAmelCase__ : Optional[Any] = model(snake_case__ , token_type_ids=snake_case__ ) UpperCAmelCase__ : Optional[int] = model(snake_case__ ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) self.parent.assertEqual(result.pooler_output.shape , (self.batch_size, self.hidden_size) ) def __a ( self : Dict , snake_case__ : List[Any] , snake_case__ : Union[str, Any] , snake_case__ : List[str] , snake_case__ : int , snake_case__ : List[Any] , snake_case__ : str , snake_case__ : int ): '''simple docstring''' UpperCAmelCase__ : List[str] = AlbertForPreTraining(config=snake_case__ ) model.to(snake_case__ ) model.eval() UpperCAmelCase__ : Dict = model( snake_case__ , attention_mask=snake_case__ , token_type_ids=snake_case__ , labels=snake_case__ , sentence_order_label=snake_case__ , ) self.parent.assertEqual(result.prediction_logits.shape , (self.batch_size, self.seq_length, self.vocab_size) ) self.parent.assertEqual(result.sop_logits.shape , (self.batch_size, config.num_labels) ) def __a ( self : Union[str, Any] , snake_case__ : Any , snake_case__ : List[str] , snake_case__ : List[str] , snake_case__ : Union[str, Any] , snake_case__ : List[Any] , snake_case__ : Optional[Any] , snake_case__ : List[Any] ): '''simple docstring''' UpperCAmelCase__ : Optional[int] = AlbertForMaskedLM(config=snake_case__ ) model.to(snake_case__ ) model.eval() UpperCAmelCase__ : Union[str, Any] = model(snake_case__ , attention_mask=snake_case__ , token_type_ids=snake_case__ , labels=snake_case__ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.vocab_size) ) def __a ( self : Optional[int] , snake_case__ : Optional[int] , snake_case__ : Optional[Any] , snake_case__ : List[str] , snake_case__ : Any , snake_case__ : Optional[Any] , snake_case__ : List[str] , snake_case__ : Union[str, Any] ): '''simple docstring''' UpperCAmelCase__ : List[str] = AlbertForQuestionAnswering(config=snake_case__ ) model.to(snake_case__ ) model.eval() UpperCAmelCase__ : List[str] = model( snake_case__ , attention_mask=snake_case__ , token_type_ids=snake_case__ , start_positions=snake_case__ , end_positions=snake_case__ , ) self.parent.assertEqual(result.start_logits.shape , (self.batch_size, self.seq_length) ) self.parent.assertEqual(result.end_logits.shape , (self.batch_size, self.seq_length) ) def __a ( self : Dict , snake_case__ : Optional[Any] , snake_case__ : List[str] , snake_case__ : Optional[int] , snake_case__ : Any , snake_case__ : Any , snake_case__ : List[str] , snake_case__ : Union[str, Any] ): '''simple docstring''' UpperCAmelCase__ : Union[str, Any] = self.num_labels UpperCAmelCase__ : int = AlbertForSequenceClassification(snake_case__ ) model.to(snake_case__ ) model.eval() UpperCAmelCase__ : int = model(snake_case__ , attention_mask=snake_case__ , token_type_ids=snake_case__ , labels=snake_case__ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_labels) ) def __a ( self : str , snake_case__ : Optional[Any] , snake_case__ : str , snake_case__ : Optional[Any] , snake_case__ : Union[str, Any] , snake_case__ : Optional[int] , snake_case__ : Dict , snake_case__ : Tuple ): '''simple docstring''' UpperCAmelCase__ : str = self.num_labels UpperCAmelCase__ : Any = AlbertForTokenClassification(config=snake_case__ ) model.to(snake_case__ ) model.eval() UpperCAmelCase__ : List[str] = model(snake_case__ , attention_mask=snake_case__ , token_type_ids=snake_case__ , labels=snake_case__ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.num_labels) ) def __a ( self : Any , snake_case__ : Optional[Any] , snake_case__ : Dict , snake_case__ : Dict , snake_case__ : int , snake_case__ : List[str] , snake_case__ : Union[str, Any] , snake_case__ : Dict ): '''simple docstring''' UpperCAmelCase__ : Union[str, Any] = self.num_choices UpperCAmelCase__ : Optional[Any] = AlbertForMultipleChoice(config=snake_case__ ) model.to(snake_case__ ) model.eval() UpperCAmelCase__ : int = input_ids.unsqueeze(1 ).expand(-1 , self.num_choices , -1 ).contiguous() UpperCAmelCase__ : Any = token_type_ids.unsqueeze(1 ).expand(-1 , self.num_choices , -1 ).contiguous() UpperCAmelCase__ : Optional[Any] = input_mask.unsqueeze(1 ).expand(-1 , self.num_choices , -1 ).contiguous() UpperCAmelCase__ : Tuple = model( snake_case__ , attention_mask=snake_case__ , token_type_ids=snake_case__ , labels=snake_case__ , ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_choices) ) def __a ( self : Union[str, Any] ): '''simple docstring''' UpperCAmelCase__ : Optional[Any] = self.prepare_config_and_inputs() ( ( UpperCAmelCase__ ) , ( UpperCAmelCase__ ) , ( UpperCAmelCase__ ) , ( UpperCAmelCase__ ) , ( UpperCAmelCase__ ) , ( UpperCAmelCase__ ) , ( UpperCAmelCase__ ) , ) : Optional[Any] = config_and_inputs UpperCAmelCase__ : Optional[int] = {"input_ids": input_ids, "token_type_ids": token_type_ids, "attention_mask": input_mask} return config, inputs_dict @require_torch class lowerCAmelCase__ ( __magic_name__ , __magic_name__ , unittest.TestCase ): SCREAMING_SNAKE_CASE_ =( ( AlbertModel, AlbertForPreTraining, AlbertForMaskedLM, AlbertForMultipleChoice, AlbertForSequenceClassification, AlbertForTokenClassification, AlbertForQuestionAnswering, ) if is_torch_available() else () ) SCREAMING_SNAKE_CASE_ =( { '''feature-extraction''': AlbertModel, '''fill-mask''': AlbertForMaskedLM, '''question-answering''': AlbertForQuestionAnswering, '''text-classification''': AlbertForSequenceClassification, '''token-classification''': AlbertForTokenClassification, '''zero-shot''': AlbertForSequenceClassification, } if is_torch_available() else {} ) SCREAMING_SNAKE_CASE_ =True def __a ( self : Tuple , snake_case__ : int , snake_case__ : Tuple , snake_case__ : Optional[int]=False ): '''simple docstring''' UpperCAmelCase__ : List[str] = super()._prepare_for_class(snake_case__ , snake_case__ , return_labels=snake_case__ ) if return_labels: if model_class in get_values(snake_case__ ): UpperCAmelCase__ : List[Any] = torch.zeros( (self.model_tester.batch_size, self.model_tester.seq_length) , dtype=torch.long , device=snake_case__ ) UpperCAmelCase__ : int = torch.zeros( self.model_tester.batch_size , dtype=torch.long , device=snake_case__ ) return inputs_dict def __a ( self : Tuple ): '''simple docstring''' UpperCAmelCase__ : Dict = AlbertModelTester(self ) UpperCAmelCase__ : Any = ConfigTester(self , config_class=snake_case__ , hidden_size=3_7 ) def __a ( self : Dict ): '''simple docstring''' self.config_tester.run_common_tests() def __a ( self : str ): '''simple docstring''' UpperCAmelCase__ : Optional[Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*snake_case__ ) def __a ( self : Dict ): '''simple docstring''' UpperCAmelCase__ : Optional[int] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_pretraining(*snake_case__ ) def __a ( self : str ): '''simple docstring''' UpperCAmelCase__ : Dict = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_masked_lm(*snake_case__ ) def __a ( self : Tuple ): '''simple docstring''' UpperCAmelCase__ : Union[str, Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_multiple_choice(*snake_case__ ) def __a ( self : Dict ): '''simple docstring''' UpperCAmelCase__ : Dict = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_question_answering(*snake_case__ ) def __a ( self : Optional[Any] ): '''simple docstring''' UpperCAmelCase__ : Union[str, Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_sequence_classification(*snake_case__ ) def __a ( self : List[Any] ): '''simple docstring''' UpperCAmelCase__ : str = self.model_tester.prepare_config_and_inputs() for type in ["absolute", "relative_key", "relative_key_query"]: UpperCAmelCase__ : Dict = type self.model_tester.create_and_check_model(*snake_case__ ) @slow def __a ( self : Union[str, Any] ): '''simple docstring''' for model_name in ALBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: UpperCAmelCase__ : List[str] = AlbertModel.from_pretrained(snake_case__ ) self.assertIsNotNone(snake_case__ ) @require_torch class lowerCAmelCase__ ( unittest.TestCase ): @slow def __a ( self : List[str] ): '''simple docstring''' UpperCAmelCase__ : List[str] = AlbertModel.from_pretrained("albert-base-v2" ) UpperCAmelCase__ : Dict = torch.tensor([[0, 3_4_5, 2_3_2, 3_2_8, 7_4_0, 1_4_0, 1_6_9_5, 6_9, 6_0_7_8, 1_5_8_8, 2]] ) UpperCAmelCase__ : List[str] = torch.tensor([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]] ) with torch.no_grad(): UpperCAmelCase__ : int = model(snake_case__ , attention_mask=snake_case__ )[0] UpperCAmelCase__ : Dict = torch.Size((1, 1_1, 7_6_8) ) self.assertEqual(output.shape , snake_case__ ) UpperCAmelCase__ : Union[str, Any] = torch.tensor( [[[-0.6513, 1.5035, -0.2766], [-0.6515, 1.5046, -0.2780], [-0.6512, 1.5049, -0.2784]]] ) self.assertTrue(torch.allclose(output[:, 1:4, 1:4] , snake_case__ , atol=1e-4 ) )
298
0