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
import contextlib import importlib import io import unittest import transformers # Try to import everything from transformers to ensure every object can be loaded. from transformers import * # noqa F406 from transformers.testing_utils import DUMMY_UNKNOWN_IDENTIFIER, require_flax, require_tf, require_torch from transformers.utils import ContextManagers, find_labels, is_flax_available, is_tf_available, is_torch_available if is_torch_available(): from transformers import BertForPreTraining, BertForQuestionAnswering, BertForSequenceClassification if is_tf_available(): from transformers import TFBertForPreTraining, TFBertForQuestionAnswering, TFBertForSequenceClassification if is_flax_available(): from transformers import FlaxBertForPreTraining, FlaxBertForQuestionAnswering, FlaxBertForSequenceClassification A : Optional[int] = DUMMY_UNKNOWN_IDENTIFIER # An actual model hosted on huggingface.co A : List[str] = "main" # Default branch name A : Union[str, Any] = "f2c752cfc5c0ab6f4bdec59acea69eefbee381c2" # One particular commit (not the top of `main`) A : Dict = "aaaaaaa" # This commit does not exist, so we should 404. A : Optional[int] = "d9e9f15bc825e4b2c9249e9578f884bbcb5e3684" # Sha-1 of config.json on the top of `main`, for checking purposes A : Optional[Any] = "4b243c475af8d0a7754e87d7d096c92e5199ec2fe168a2ee7998e3b8e9bcb1d3" @contextlib.contextmanager def a__ ( ): print("Welcome!" ) yield print("Bye!" ) @contextlib.contextmanager def a__ ( ): print("Bonjour!" ) yield print("Au revoir!" ) class lowerCamelCase (unittest.TestCase ): """simple docstring""" def __A ( self : Dict ) -> Tuple: # If the spec is missing, importlib would not be able to import the module dynamically. assert transformers.__spec__ is not None assert importlib.util.find_spec("transformers" ) is not None class lowerCamelCase (unittest.TestCase ): """simple docstring""" @unittest.mock.patch("sys.stdout" , new_callable=io.StringIO ) def __A ( self : Tuple , __magic_name__ : Union[str, Any] ) -> Optional[int]: with ContextManagers([] ): print("Transformers are awesome!" ) # The print statement adds a new line at the end of the output self.assertEqual(mock_stdout.getvalue() , "Transformers are awesome!\n" ) @unittest.mock.patch("sys.stdout" , new_callable=io.StringIO ) def __A ( self : List[Any] , __magic_name__ : Union[str, Any] ) -> List[str]: with ContextManagers([context_en()] ): print("Transformers are awesome!" ) # The output should be wrapped with an English welcome and goodbye self.assertEqual(mock_stdout.getvalue() , "Welcome!\nTransformers are awesome!\nBye!\n" ) @unittest.mock.patch("sys.stdout" , new_callable=io.StringIO ) def __A ( self : Any , __magic_name__ : Union[str, Any] ) -> Any: with ContextManagers([context_fr(), context_en()] ): print("Transformers are awesome!" ) # The output should be wrapped with an English and French welcome and goodbye self.assertEqual(mock_stdout.getvalue() , "Bonjour!\nWelcome!\nTransformers are awesome!\nBye!\nAu revoir!\n" ) @require_torch def __A ( self : List[str] ) -> int: self.assertEqual(find_labels(__magic_name__ ) , ["labels"] ) self.assertEqual(find_labels(__magic_name__ ) , ["labels", "next_sentence_label"] ) self.assertEqual(find_labels(__magic_name__ ) , ["start_positions", "end_positions"] ) class lowerCamelCase (SCREAMING_SNAKE_CASE__ ): """simple docstring""" pass self.assertEqual(find_labels(__magic_name__ ) , ["labels"] ) @require_tf def __A ( self : Optional[int] ) -> List[str]: self.assertEqual(find_labels(__magic_name__ ) , ["labels"] ) self.assertEqual(find_labels(__magic_name__ ) , ["labels", "next_sentence_label"] ) self.assertEqual(find_labels(__magic_name__ ) , ["start_positions", "end_positions"] ) class lowerCamelCase (SCREAMING_SNAKE_CASE__ ): """simple docstring""" pass self.assertEqual(find_labels(__magic_name__ ) , ["labels"] ) @require_flax def __A ( self : Union[str, Any] ) -> Dict: # Flax models don't have labels self.assertEqual(find_labels(__magic_name__ ) , [] ) self.assertEqual(find_labels(__magic_name__ ) , [] ) self.assertEqual(find_labels(__magic_name__ ) , [] ) class lowerCamelCase (SCREAMING_SNAKE_CASE__ ): """simple docstring""" pass self.assertEqual(find_labels(__magic_name__ ) , [] )
305
from __future__ import annotations import collections import pprint from pathlib import Path def a__ ( __UpperCamelCase ): return "".join(sorted(__UpperCamelCase ) ) def a__ ( __UpperCamelCase ): return word_by_signature[signature(__UpperCamelCase )] A : str = Path(__file__).parent.joinpath("words.txt").read_text(encoding="utf-8") A : int = sorted({word.strip().lower() for word in data.splitlines()}) A : Tuple = collections.defaultdict(list) for word in word_list: word_by_signature[signature(word)].append(word) if __name__ == "__main__": A : Union[str, Any] = {word: anagram(word) for word in word_list if len(anagram(word)) > 1} with open("anagrams.txt", "w") as file: file.write("all_anagrams = \n ") file.write(pprint.pformat(all_anagrams))
305
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_camembert import CamembertTokenizer else: A : Dict = None A : Dict = logging.get_logger(__name__) A : List[str] = {"vocab_file": "sentencepiece.bpe.model", "tokenizer_file": "tokenizer.json"} A : Dict = { "vocab_file": { "camembert-base": "https://huggingface.co/camembert-base/resolve/main/sentencepiece.bpe.model", }, "tokenizer_file": { "camembert-base": "https://huggingface.co/camembert-base/resolve/main/tokenizer.json", }, } A : str = { "camembert-base": 5_12, } A : Any = "▁" class lowerCamelCase (SCREAMING_SNAKE_CASE__ ): """simple docstring""" lowerCamelCase__ = VOCAB_FILES_NAMES lowerCamelCase__ = PRETRAINED_VOCAB_FILES_MAP lowerCamelCase__ = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES lowerCamelCase__ = ['''input_ids''', '''attention_mask'''] lowerCamelCase__ = CamembertTokenizer def __init__( self : str , __magic_name__ : Union[str, Any]=None , __magic_name__ : List[str]=None , __magic_name__ : Union[str, Any]="<s>" , __magic_name__ : str="</s>" , __magic_name__ : Union[str, Any]="</s>" , __magic_name__ : Union[str, Any]="<s>" , __magic_name__ : Optional[Any]="<unk>" , __magic_name__ : List[Any]="<pad>" , __magic_name__ : Tuple="<mask>" , __magic_name__ : List[Any]=["<s>NOTUSED", "</s>NOTUSED"] , **__magic_name__ : str , ) -> List[Any]: # Mask token behave like a normal word, i.e. include the space before it SCREAMING_SNAKE_CASE_ = AddedToken(__magic_name__ , lstrip=__magic_name__ , rstrip=__magic_name__ ) if isinstance(__magic_name__ , __magic_name__ ) else mask_token super().__init__( __magic_name__ , tokenizer_file=__magic_name__ , bos_token=__magic_name__ , eos_token=__magic_name__ , sep_token=__magic_name__ , cls_token=__magic_name__ , unk_token=__magic_name__ , pad_token=__magic_name__ , mask_token=__magic_name__ , additional_special_tokens=__magic_name__ , **__magic_name__ , ) SCREAMING_SNAKE_CASE_ = vocab_file SCREAMING_SNAKE_CASE_ = False if not self.vocab_file else True def __A ( self : Union[str, Any] , __magic_name__ : List[int] , __magic_name__ : Optional[List[int]] = None ) -> List[int]: if token_ids_a is None: return [self.cls_token_id] + token_ids_a + [self.sep_token_id] SCREAMING_SNAKE_CASE_ = [self.cls_token_id] SCREAMING_SNAKE_CASE_ = [self.sep_token_id] return cls + token_ids_a + sep + sep + token_ids_a + sep def __A ( self : str , __magic_name__ : List[int] , __magic_name__ : Optional[List[int]] = None ) -> List[int]: SCREAMING_SNAKE_CASE_ = [self.sep_token_id] SCREAMING_SNAKE_CASE_ = [self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep ) * [0] return len(cls + token_ids_a + sep + sep + token_ids_a + sep ) * [0] def __A ( self : str , __magic_name__ : str , __magic_name__ : Optional[str] = None ) -> Tuple[str]: if not self.can_save_slow_tokenizer: raise ValueError( "Your fast tokenizer does not have the necessary information to save the vocabulary for a slow " "tokenizer." ) if not os.path.isdir(__magic_name__ ): logger.error(F'''Vocabulary path ({save_directory}) should be a directory''' ) return SCREAMING_SNAKE_CASE_ = os.path.join( __magic_name__ , (filename_prefix + "-" if filename_prefix else "") + VOCAB_FILES_NAMES["vocab_file"] ) if os.path.abspath(self.vocab_file ) != os.path.abspath(__magic_name__ ): copyfile(self.vocab_file , __magic_name__ ) return (out_vocab_file,)
305
import copy import os from typing import TYPE_CHECKING, List, Union if TYPE_CHECKING: pass from ...configuration_utils import PretrainedConfig from ...utils import logging A : int = logging.get_logger(__name__) A : str = { "kakaobrain/align-base": "https://huggingface.co/kakaobrain/align-base/resolve/main/config.json", } class lowerCamelCase (SCREAMING_SNAKE_CASE__ ): """simple docstring""" lowerCamelCase__ = '''align_text_model''' def __init__( self : Optional[Any] , __magic_name__ : Union[str, Any]=30_522 , __magic_name__ : Tuple=768 , __magic_name__ : List[str]=12 , __magic_name__ : Optional[Any]=12 , __magic_name__ : str=3_072 , __magic_name__ : Dict="gelu" , __magic_name__ : Any=0.1 , __magic_name__ : Optional[int]=0.1 , __magic_name__ : List[str]=512 , __magic_name__ : Any=2 , __magic_name__ : Optional[Any]=0.02 , __magic_name__ : int=1e-12 , __magic_name__ : str=0 , __magic_name__ : Optional[Any]="absolute" , __magic_name__ : Optional[Any]=True , **__magic_name__ : Tuple , ) -> Union[str, Any]: super().__init__(**__magic_name__ ) 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_ = position_embedding_type SCREAMING_SNAKE_CASE_ = use_cache SCREAMING_SNAKE_CASE_ = pad_token_id @classmethod def __A ( cls : Any , __magic_name__ : Union[str, os.PathLike] , **__magic_name__ : Optional[Any] ) -> "PretrainedConfig": cls._set_token_in_kwargs(__magic_name__ ) SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = cls.get_config_dict(__magic_name__ , **__magic_name__ ) # get the text config dict if we are loading from AlignConfig if config_dict.get("model_type" ) == "align": SCREAMING_SNAKE_CASE_ = config_dict["text_config"] if "model_type" in config_dict and hasattr(cls , "model_type" ) and config_dict["model_type"] != cls.model_type: logger.warning( F'''You are using a model of type {config_dict["model_type"]} to instantiate a model of type ''' F'''{cls.model_type}. This is not supported for all configurations of models and can yield errors.''' ) return cls.from_dict(__magic_name__ , **__magic_name__ ) class lowerCamelCase (SCREAMING_SNAKE_CASE__ ): """simple docstring""" lowerCamelCase__ = '''align_vision_model''' def __init__( self : List[str] , __magic_name__ : int = 3 , __magic_name__ : int = 600 , __magic_name__ : float = 2.0 , __magic_name__ : float = 3.1 , __magic_name__ : int = 8 , __magic_name__ : List[int] = [3, 3, 5, 3, 5, 5, 3] , __magic_name__ : List[int] = [32, 16, 24, 40, 80, 112, 192] , __magic_name__ : List[int] = [16, 24, 40, 80, 112, 192, 320] , __magic_name__ : List[int] = [] , __magic_name__ : List[int] = [1, 2, 2, 2, 1, 2, 1] , __magic_name__ : List[int] = [1, 2, 2, 3, 3, 4, 1] , __magic_name__ : List[int] = [1, 6, 6, 6, 6, 6, 6] , __magic_name__ : float = 0.25 , __magic_name__ : str = "swish" , __magic_name__ : int = 2_560 , __magic_name__ : str = "mean" , __magic_name__ : float = 0.02 , __magic_name__ : float = 0.001 , __magic_name__ : float = 0.99 , __magic_name__ : float = 0.2 , **__magic_name__ : List[Any] , ) -> Tuple: super().__init__(**__magic_name__ ) SCREAMING_SNAKE_CASE_ = num_channels SCREAMING_SNAKE_CASE_ = image_size SCREAMING_SNAKE_CASE_ = width_coefficient SCREAMING_SNAKE_CASE_ = depth_coefficient SCREAMING_SNAKE_CASE_ = depth_divisor SCREAMING_SNAKE_CASE_ = kernel_sizes SCREAMING_SNAKE_CASE_ = in_channels SCREAMING_SNAKE_CASE_ = out_channels SCREAMING_SNAKE_CASE_ = depthwise_padding SCREAMING_SNAKE_CASE_ = strides SCREAMING_SNAKE_CASE_ = num_block_repeats SCREAMING_SNAKE_CASE_ = expand_ratios SCREAMING_SNAKE_CASE_ = squeeze_expansion_ratio SCREAMING_SNAKE_CASE_ = hidden_act SCREAMING_SNAKE_CASE_ = hidden_dim SCREAMING_SNAKE_CASE_ = pooling_type SCREAMING_SNAKE_CASE_ = initializer_range SCREAMING_SNAKE_CASE_ = batch_norm_eps SCREAMING_SNAKE_CASE_ = batch_norm_momentum SCREAMING_SNAKE_CASE_ = drop_connect_rate SCREAMING_SNAKE_CASE_ = sum(__magic_name__ ) * 4 @classmethod def __A ( cls : List[str] , __magic_name__ : Union[str, os.PathLike] , **__magic_name__ : Dict ) -> "PretrainedConfig": cls._set_token_in_kwargs(__magic_name__ ) SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = cls.get_config_dict(__magic_name__ , **__magic_name__ ) # get the vision config dict if we are loading from AlignConfig if config_dict.get("model_type" ) == "align": SCREAMING_SNAKE_CASE_ = 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(__magic_name__ , **__magic_name__ ) class lowerCamelCase (SCREAMING_SNAKE_CASE__ ): """simple docstring""" lowerCamelCase__ = '''align''' lowerCamelCase__ = True def __init__( self : Optional[Any] , __magic_name__ : Dict=None , __magic_name__ : List[Any]=None , __magic_name__ : str=640 , __magic_name__ : Any=1.0 , __magic_name__ : Dict=0.02 , **__magic_name__ : Union[str, Any] , ) -> int: super().__init__(**__magic_name__ ) if text_config is None: SCREAMING_SNAKE_CASE_ = {} logger.info("text_config is None. Initializing the AlignTextConfig with default values." ) if vision_config is None: SCREAMING_SNAKE_CASE_ = {} logger.info("vision_config is None. Initializing the AlignVisionConfig with default values." ) SCREAMING_SNAKE_CASE_ = AlignTextConfig(**__magic_name__ ) SCREAMING_SNAKE_CASE_ = AlignVisionConfig(**__magic_name__ ) SCREAMING_SNAKE_CASE_ = projection_dim SCREAMING_SNAKE_CASE_ = temperature_init_value SCREAMING_SNAKE_CASE_ = initializer_range @classmethod def __A ( cls : List[str] , __magic_name__ : AlignTextConfig , __magic_name__ : AlignVisionConfig , **__magic_name__ : Tuple ) -> Any: return cls(text_config=text_config.to_dict() , vision_config=vision_config.to_dict() , **__magic_name__ ) def __A ( self : int ) -> Union[str, Any]: SCREAMING_SNAKE_CASE_ = copy.deepcopy(self.__dict__ ) SCREAMING_SNAKE_CASE_ = self.text_config.to_dict() SCREAMING_SNAKE_CASE_ = self.vision_config.to_dict() SCREAMING_SNAKE_CASE_ = self.__class__.model_type return output
305
1
import json import sys import tempfile import unittest from pathlib import Path import transformers from transformers import ( CONFIG_MAPPING, IMAGE_PROCESSOR_MAPPING, AutoConfig, AutoImageProcessor, CLIPConfig, CLIPImageProcessor, ) from transformers.testing_utils import DUMMY_UNKNOWN_IDENTIFIER sys.path.append(str(Path(__file__).parent.parent.parent.parent / "utils")) from test_module.custom_configuration import CustomConfig # noqa E402 from test_module.custom_image_processing import CustomImageProcessor # noqa E402 class lowerCamelCase (unittest.TestCase ): """simple docstring""" def __A ( self : Tuple ) -> Optional[Any]: SCREAMING_SNAKE_CASE_ = 0 def __A ( self : List[Any] ) -> List[Any]: SCREAMING_SNAKE_CASE_ = AutoImageProcessor.from_pretrained("openai/clip-vit-base-patch32" ) self.assertIsInstance(__magic_name__ , __magic_name__ ) def __A ( self : Any ) -> Optional[Any]: with tempfile.TemporaryDirectory() as tmpdirname: SCREAMING_SNAKE_CASE_ = Path(__magic_name__ ) / "preprocessor_config.json" SCREAMING_SNAKE_CASE_ = Path(__magic_name__ ) / "config.json" json.dump( {"image_processor_type": "CLIPImageProcessor", "processor_class": "CLIPProcessor"} , open(__magic_name__ , "w" ) , ) json.dump({"model_type": "clip"} , open(__magic_name__ , "w" ) ) SCREAMING_SNAKE_CASE_ = AutoImageProcessor.from_pretrained(__magic_name__ ) self.assertIsInstance(__magic_name__ , __magic_name__ ) def __A ( self : Optional[int] ) -> Tuple: # Ensure we can load the image processor from the feature extractor config with tempfile.TemporaryDirectory() as tmpdirname: SCREAMING_SNAKE_CASE_ = Path(__magic_name__ ) / "preprocessor_config.json" SCREAMING_SNAKE_CASE_ = Path(__magic_name__ ) / "config.json" json.dump( {"feature_extractor_type": "CLIPFeatureExtractor", "processor_class": "CLIPProcessor"} , open(__magic_name__ , "w" ) , ) json.dump({"model_type": "clip"} , open(__magic_name__ , "w" ) ) SCREAMING_SNAKE_CASE_ = AutoImageProcessor.from_pretrained(__magic_name__ ) self.assertIsInstance(__magic_name__ , __magic_name__ ) def __A ( self : List[str] ) -> Union[str, Any]: with tempfile.TemporaryDirectory() as tmpdirname: SCREAMING_SNAKE_CASE_ = CLIPConfig() # Create a dummy config file with image_proceesor_type SCREAMING_SNAKE_CASE_ = Path(__magic_name__ ) / "preprocessor_config.json" SCREAMING_SNAKE_CASE_ = Path(__magic_name__ ) / "config.json" json.dump( {"image_processor_type": "CLIPImageProcessor", "processor_class": "CLIPProcessor"} , open(__magic_name__ , "w" ) , ) json.dump({"model_type": "clip"} , open(__magic_name__ , "w" ) ) # remove image_processor_type to make sure config.json alone is enough to load image processor locally SCREAMING_SNAKE_CASE_ = AutoImageProcessor.from_pretrained(__magic_name__ ).to_dict() config_dict.pop("image_processor_type" ) SCREAMING_SNAKE_CASE_ = CLIPImageProcessor(**__magic_name__ ) # save in new folder model_config.save_pretrained(__magic_name__ ) config.save_pretrained(__magic_name__ ) SCREAMING_SNAKE_CASE_ = AutoImageProcessor.from_pretrained(__magic_name__ ) # make sure private variable is not incorrectly saved SCREAMING_SNAKE_CASE_ = json.loads(config.to_json_string() ) self.assertTrue("_processor_class" not in dict_as_saved ) self.assertIsInstance(__magic_name__ , __magic_name__ ) def __A ( self : List[Any] ) -> Dict: with tempfile.TemporaryDirectory() as tmpdirname: SCREAMING_SNAKE_CASE_ = Path(__magic_name__ ) / "preprocessor_config.json" json.dump( {"image_processor_type": "CLIPImageProcessor", "processor_class": "CLIPProcessor"} , open(__magic_name__ , "w" ) , ) SCREAMING_SNAKE_CASE_ = AutoImageProcessor.from_pretrained(__magic_name__ ) self.assertIsInstance(__magic_name__ , __magic_name__ ) def __A ( self : List[Any] ) -> Any: with self.assertRaisesRegex( __magic_name__ , "clip-base is not a local folder and is not a valid model identifier" ): SCREAMING_SNAKE_CASE_ = AutoImageProcessor.from_pretrained("clip-base" ) def __A ( self : List[Any] ) -> Dict: with self.assertRaisesRegex( __magic_name__ , r"aaaaaa is not a valid git identifier \(branch name, tag name or commit id\)" ): SCREAMING_SNAKE_CASE_ = AutoImageProcessor.from_pretrained(__magic_name__ , revision="aaaaaa" ) def __A ( self : int ) -> Any: with self.assertRaisesRegex( __magic_name__ , "hf-internal-testing/config-no-model does not appear to have a file named preprocessor_config.json." , ): SCREAMING_SNAKE_CASE_ = AutoImageProcessor.from_pretrained("hf-internal-testing/config-no-model" ) def __A ( self : str ) -> List[Any]: # If remote code is not set, we will time out when asking whether to load the model. with self.assertRaises(__magic_name__ ): SCREAMING_SNAKE_CASE_ = AutoImageProcessor.from_pretrained("hf-internal-testing/test_dynamic_image_processor" ) # If remote code is disabled, we can't load this config. with self.assertRaises(__magic_name__ ): SCREAMING_SNAKE_CASE_ = AutoImageProcessor.from_pretrained( "hf-internal-testing/test_dynamic_image_processor" , trust_remote_code=__magic_name__ ) SCREAMING_SNAKE_CASE_ = AutoImageProcessor.from_pretrained( "hf-internal-testing/test_dynamic_image_processor" , trust_remote_code=__magic_name__ ) self.assertEqual(image_processor.__class__.__name__ , "NewImageProcessor" ) # Test image processor can be reloaded. with tempfile.TemporaryDirectory() as tmp_dir: image_processor.save_pretrained(__magic_name__ ) SCREAMING_SNAKE_CASE_ = AutoImageProcessor.from_pretrained(__magic_name__ , trust_remote_code=__magic_name__ ) self.assertEqual(reloaded_image_processor.__class__.__name__ , "NewImageProcessor" ) def __A ( self : Any ) -> Tuple: try: AutoConfig.register("custom" , __magic_name__ ) AutoImageProcessor.register(__magic_name__ , __magic_name__ ) # Trying to register something existing in the Transformers library will raise an error with self.assertRaises(__magic_name__ ): AutoImageProcessor.register(__magic_name__ , __magic_name__ ) with tempfile.TemporaryDirectory() as tmpdirname: SCREAMING_SNAKE_CASE_ = Path(__magic_name__ ) / "preprocessor_config.json" SCREAMING_SNAKE_CASE_ = Path(__magic_name__ ) / "config.json" json.dump( {"feature_extractor_type": "CLIPFeatureExtractor", "processor_class": "CLIPProcessor"} , open(__magic_name__ , "w" ) , ) json.dump({"model_type": "clip"} , open(__magic_name__ , "w" ) ) SCREAMING_SNAKE_CASE_ = CustomImageProcessor.from_pretrained(__magic_name__ ) # Now that the config is registered, it can be used as any other config with the auto-API with tempfile.TemporaryDirectory() as tmp_dir: image_processor.save_pretrained(__magic_name__ ) SCREAMING_SNAKE_CASE_ = AutoImageProcessor.from_pretrained(__magic_name__ ) self.assertIsInstance(__magic_name__ , __magic_name__ ) finally: if "custom" in CONFIG_MAPPING._extra_content: del CONFIG_MAPPING._extra_content["custom"] if CustomConfig in IMAGE_PROCESSOR_MAPPING._extra_content: del IMAGE_PROCESSOR_MAPPING._extra_content[CustomConfig] def __A ( self : Optional[Any] ) -> Optional[int]: class lowerCamelCase (SCREAMING_SNAKE_CASE__ ): """simple docstring""" lowerCamelCase__ = True try: AutoConfig.register("custom" , __magic_name__ ) AutoImageProcessor.register(__magic_name__ , __magic_name__ ) # If remote code is not set, the default is to use local SCREAMING_SNAKE_CASE_ = AutoImageProcessor.from_pretrained("hf-internal-testing/test_dynamic_image_processor" ) self.assertEqual(image_processor.__class__.__name__ , "NewImageProcessor" ) self.assertTrue(image_processor.is_local ) # If remote code is disabled, we load the local one. SCREAMING_SNAKE_CASE_ = AutoImageProcessor.from_pretrained( "hf-internal-testing/test_dynamic_image_processor" , trust_remote_code=__magic_name__ ) self.assertEqual(image_processor.__class__.__name__ , "NewImageProcessor" ) self.assertTrue(image_processor.is_local ) # If remote is enabled, we load from the Hub SCREAMING_SNAKE_CASE_ = AutoImageProcessor.from_pretrained( "hf-internal-testing/test_dynamic_image_processor" , trust_remote_code=__magic_name__ ) self.assertEqual(image_processor.__class__.__name__ , "NewImageProcessor" ) self.assertTrue(not hasattr(__magic_name__ , "is_local" ) ) finally: if "custom" in CONFIG_MAPPING._extra_content: del CONFIG_MAPPING._extra_content["custom"] if CustomConfig in IMAGE_PROCESSOR_MAPPING._extra_content: del IMAGE_PROCESSOR_MAPPING._extra_content[CustomConfig]
305
import unittest from transformers.testing_utils import CaptureStdout from transformers.tools.python_interpreter import evaluate def a__ ( __UpperCamelCase ): return x + 2 class lowerCamelCase (unittest.TestCase ): """simple docstring""" def __A ( self : List[Any] ) -> int: SCREAMING_SNAKE_CASE_ = "x = 3" SCREAMING_SNAKE_CASE_ = {} SCREAMING_SNAKE_CASE_ = evaluate(__magic_name__ , {} , state=__magic_name__ ) assert result == 3 self.assertDictEqual(__magic_name__ , {"x": 3} ) SCREAMING_SNAKE_CASE_ = "x = y" SCREAMING_SNAKE_CASE_ = {"y": 5} SCREAMING_SNAKE_CASE_ = evaluate(__magic_name__ , {} , state=__magic_name__ ) # evaluate returns the value of the last assignment. assert result == 5 self.assertDictEqual(__magic_name__ , {"x": 5, "y": 5} ) def __A ( self : Union[str, Any] ) -> str: SCREAMING_SNAKE_CASE_ = "y = add_two(x)" SCREAMING_SNAKE_CASE_ = {"x": 3} SCREAMING_SNAKE_CASE_ = evaluate(__magic_name__ , {"add_two": add_two} , state=__magic_name__ ) assert result == 5 self.assertDictEqual(__magic_name__ , {"x": 3, "y": 5} ) # Won't work without the tool with CaptureStdout() as out: SCREAMING_SNAKE_CASE_ = evaluate(__magic_name__ , {} , state=__magic_name__ ) assert result is None assert "tried to execute add_two" in out.out def __A ( self : List[str] ) -> int: SCREAMING_SNAKE_CASE_ = "x = 3" SCREAMING_SNAKE_CASE_ = {} SCREAMING_SNAKE_CASE_ = evaluate(__magic_name__ , {} , state=__magic_name__ ) assert result == 3 self.assertDictEqual(__magic_name__ , {"x": 3} ) def __A ( self : Optional[Any] ) -> str: SCREAMING_SNAKE_CASE_ = "test_dict = {'x': x, 'y': add_two(x)}" SCREAMING_SNAKE_CASE_ = {"x": 3} SCREAMING_SNAKE_CASE_ = evaluate(__magic_name__ , {"add_two": add_two} , state=__magic_name__ ) self.assertDictEqual(__magic_name__ , {"x": 3, "y": 5} ) self.assertDictEqual(__magic_name__ , {"x": 3, "test_dict": {"x": 3, "y": 5}} ) def __A ( self : Optional[int] ) -> List[str]: SCREAMING_SNAKE_CASE_ = "x = 3\ny = 5" SCREAMING_SNAKE_CASE_ = {} SCREAMING_SNAKE_CASE_ = evaluate(__magic_name__ , {} , state=__magic_name__ ) # evaluate returns the value of the last assignment. assert result == 5 self.assertDictEqual(__magic_name__ , {"x": 3, "y": 5} ) def __A ( self : Any ) -> List[str]: SCREAMING_SNAKE_CASE_ = "text = f'This is x: {x}.'" SCREAMING_SNAKE_CASE_ = {"x": 3} SCREAMING_SNAKE_CASE_ = evaluate(__magic_name__ , {} , state=__magic_name__ ) # evaluate returns the value of the last assignment. assert result == "This is x: 3." self.assertDictEqual(__magic_name__ , {"x": 3, "text": "This is x: 3."} ) def __A ( self : int ) -> Tuple: SCREAMING_SNAKE_CASE_ = "if x <= 3:\n y = 2\nelse:\n y = 5" SCREAMING_SNAKE_CASE_ = {"x": 3} SCREAMING_SNAKE_CASE_ = evaluate(__magic_name__ , {} , state=__magic_name__ ) # evaluate returns the value of the last assignment. assert result == 2 self.assertDictEqual(__magic_name__ , {"x": 3, "y": 2} ) SCREAMING_SNAKE_CASE_ = {"x": 8} SCREAMING_SNAKE_CASE_ = evaluate(__magic_name__ , {} , state=__magic_name__ ) # evaluate returns the value of the last assignment. assert result == 5 self.assertDictEqual(__magic_name__ , {"x": 8, "y": 5} ) def __A ( self : str ) -> str: SCREAMING_SNAKE_CASE_ = "test_list = [x, add_two(x)]" SCREAMING_SNAKE_CASE_ = {"x": 3} SCREAMING_SNAKE_CASE_ = evaluate(__magic_name__ , {"add_two": add_two} , state=__magic_name__ ) self.assertListEqual(__magic_name__ , [3, 5] ) self.assertDictEqual(__magic_name__ , {"x": 3, "test_list": [3, 5]} ) def __A ( self : Union[str, Any] ) -> List[Any]: SCREAMING_SNAKE_CASE_ = "y = x" SCREAMING_SNAKE_CASE_ = {"x": 3} SCREAMING_SNAKE_CASE_ = evaluate(__magic_name__ , {} , state=__magic_name__ ) assert result == 3 self.assertDictEqual(__magic_name__ , {"x": 3, "y": 3} ) def __A ( self : Tuple ) -> List[Any]: SCREAMING_SNAKE_CASE_ = "test_list = [x, add_two(x)]\ntest_list[1]" SCREAMING_SNAKE_CASE_ = {"x": 3} SCREAMING_SNAKE_CASE_ = evaluate(__magic_name__ , {"add_two": add_two} , state=__magic_name__ ) assert result == 5 self.assertDictEqual(__magic_name__ , {"x": 3, "test_list": [3, 5]} ) SCREAMING_SNAKE_CASE_ = "test_dict = {'x': x, 'y': add_two(x)}\ntest_dict['y']" SCREAMING_SNAKE_CASE_ = {"x": 3} SCREAMING_SNAKE_CASE_ = evaluate(__magic_name__ , {"add_two": add_two} , state=__magic_name__ ) assert result == 5 self.assertDictEqual(__magic_name__ , {"x": 3, "test_dict": {"x": 3, "y": 5}} ) def __A ( self : Tuple ) -> Any: SCREAMING_SNAKE_CASE_ = "x = 0\nfor i in range(3):\n x = i" SCREAMING_SNAKE_CASE_ = {} SCREAMING_SNAKE_CASE_ = evaluate(__magic_name__ , {"range": range} , state=__magic_name__ ) assert result == 2 self.assertDictEqual(__magic_name__ , {"x": 2, "i": 2} )
305
1
from __future__ import annotations A : List[str] = { "A": ["B", "C", "E"], "B": ["A", "D", "E"], "C": ["A", "F", "G"], "D": ["B"], "E": ["A", "B", "D"], "F": ["C"], "G": ["C"], } class lowerCamelCase : """simple docstring""" def __init__( self : Union[str, Any] , __magic_name__ : dict[str, list[str]] , __magic_name__ : str ) -> None: SCREAMING_SNAKE_CASE_ = graph # mapping node to its parent in resulting breadth first tree SCREAMING_SNAKE_CASE_ = {} SCREAMING_SNAKE_CASE_ = source_vertex def __A ( self : Optional[Any] ) -> None: SCREAMING_SNAKE_CASE_ = {self.source_vertex} SCREAMING_SNAKE_CASE_ = None SCREAMING_SNAKE_CASE_ = [self.source_vertex] # first in first out queue while queue: SCREAMING_SNAKE_CASE_ = queue.pop(0 ) for adjacent_vertex in self.graph[vertex]: if adjacent_vertex not in visited: visited.add(__magic_name__ ) SCREAMING_SNAKE_CASE_ = vertex queue.append(__magic_name__ ) def __A ( self : Union[str, Any] , __magic_name__ : str ) -> str: if target_vertex == self.source_vertex: return self.source_vertex SCREAMING_SNAKE_CASE_ = self.parent.get(__magic_name__ ) if target_vertex_parent is None: SCREAMING_SNAKE_CASE_ = ( F'''No path from vertex: {self.source_vertex} to vertex: {target_vertex}''' ) raise ValueError(__magic_name__ ) return self.shortest_path(__magic_name__ ) + F'''->{target_vertex}''' if __name__ == "__main__": A : List[str] = Graph(graph, "G") g.breath_first_search() print(g.shortest_path("D")) print(g.shortest_path("G")) print(g.shortest_path("Foo"))
305
import numpy as np import pandas as pd from sklearn.preprocessing import Normalizer from sklearn.svm import SVR from statsmodels.tsa.statespace.sarimax import SARIMAX def a__ ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase ): SCREAMING_SNAKE_CASE_ = np.array([[1, item, train_mtch[i]] for i, item in enumerate(__UpperCamelCase )] ) SCREAMING_SNAKE_CASE_ = np.array(__UpperCamelCase ) SCREAMING_SNAKE_CASE_ = np.dot(np.dot(np.linalg.inv(np.dot(x.transpose() , __UpperCamelCase ) ) , x.transpose() ) , __UpperCamelCase ) return abs(beta[0] + test_dt[0] * beta[1] + test_mtch[0] + beta[2] ) def a__ ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase ): SCREAMING_SNAKE_CASE_ = (1, 2, 1) SCREAMING_SNAKE_CASE_ = (1, 1, 0, 7) SCREAMING_SNAKE_CASE_ = SARIMAX( __UpperCamelCase , exog=__UpperCamelCase , order=__UpperCamelCase , seasonal_order=__UpperCamelCase ) SCREAMING_SNAKE_CASE_ = model.fit(disp=__UpperCamelCase , maxiter=6_0_0 , method="nm" ) SCREAMING_SNAKE_CASE_ = model_fit.predict(1 , len(__UpperCamelCase ) , exog=[test_match] ) return result[0] def a__ ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase ): SCREAMING_SNAKE_CASE_ = SVR(kernel="rbf" , C=1 , gamma=0.1 , epsilon=0.1 ) regressor.fit(__UpperCamelCase , __UpperCamelCase ) SCREAMING_SNAKE_CASE_ = regressor.predict(__UpperCamelCase ) return y_pred[0] def a__ ( __UpperCamelCase ): train_user.sort() SCREAMING_SNAKE_CASE_ = np.percentile(__UpperCamelCase , 2_5 ) SCREAMING_SNAKE_CASE_ = np.percentile(__UpperCamelCase , 7_5 ) SCREAMING_SNAKE_CASE_ = qa - qa SCREAMING_SNAKE_CASE_ = qa - (iqr * 0.1) return low_lim def a__ ( __UpperCamelCase , __UpperCamelCase ): SCREAMING_SNAKE_CASE_ = 0 SCREAMING_SNAKE_CASE_ = 0 for i in list_vote: if i > actual_result: SCREAMING_SNAKE_CASE_ = not_safe + 1 else: if abs(abs(__UpperCamelCase ) - abs(__UpperCamelCase ) ) <= 0.1: safe += 1 else: not_safe += 1 return safe > not_safe if __name__ == "__main__": # data_input_df = pd.read_csv("ex_data.csv", header=None) A : Dict = [[1_82_31, 0.0, 1], [2_26_21, 1.0, 2], [1_56_75, 0.0, 3], [2_35_83, 1.0, 4]] A : Optional[Any] = pd.DataFrame( data_input, columns=["total_user", "total_even", "days"] ) A : Union[str, Any] = Normalizer().fit_transform(data_input_df.values) # split data A : Optional[int] = normalize_df[:, 2].tolist() A : List[str] = normalize_df[:, 0].tolist() A : int = normalize_df[:, 1].tolist() # for svr (input variable = total date and total match) A : int = normalize_df[:, [1, 2]].tolist() A : Tuple = x[: len(x) - 1] A : str = x[len(x) - 1 :] # for linear regression & sarimax A : Tuple = total_date[: len(total_date) - 1] A : Optional[int] = total_user[: len(total_user) - 1] A : str = total_match[: len(total_match) - 1] A : List[Any] = total_date[len(total_date) - 1 :] A : List[Any] = total_user[len(total_user) - 1 :] A : Optional[Any] = total_match[len(total_match) - 1 :] # voting system with forecasting A : Optional[int] = [ linear_regression_prediction( trn_date, trn_user, trn_match, tst_date, tst_match ), sarimax_predictor(trn_user, trn_match, tst_match), support_vector_regressor(x_train, x_test, trn_user), ] # check the safety of today's data A : str = "" if data_safety_checker(res_vote, tst_user) else "not " print("Today's data is {not_str}safe.")
305
1
def a__ ( __UpperCamelCase ): SCREAMING_SNAKE_CASE_ = generate_pascal_triangle(__UpperCamelCase ) for row_idx in range(__UpperCamelCase ): # Print left spaces for _ in range(num_rows - row_idx - 1 ): print(end=" " ) # Print row values for col_idx in range(row_idx + 1 ): if col_idx != row_idx: print(triangle[row_idx][col_idx] , end=" " ) else: print(triangle[row_idx][col_idx] , end="" ) print() def a__ ( __UpperCamelCase ): if not isinstance(__UpperCamelCase , __UpperCamelCase ): raise TypeError("The input value of 'num_rows' should be 'int'" ) if num_rows == 0: return [] elif num_rows < 0: raise ValueError( "The input value of 'num_rows' should be greater than or equal to 0" ) SCREAMING_SNAKE_CASE_ = [] for current_row_idx in range(__UpperCamelCase ): SCREAMING_SNAKE_CASE_ = populate_current_row(__UpperCamelCase , __UpperCamelCase ) triangle.append(__UpperCamelCase ) return triangle def a__ ( __UpperCamelCase , __UpperCamelCase ): SCREAMING_SNAKE_CASE_ = [-1] * (current_row_idx + 1) # first and last elements of current row are equal to 1 SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = 1, 1 for current_col_idx in range(1 , __UpperCamelCase ): calculate_current_element( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) return current_row def a__ ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , ): SCREAMING_SNAKE_CASE_ = triangle[current_row_idx - 1][current_col_idx - 1] SCREAMING_SNAKE_CASE_ = triangle[current_row_idx - 1][current_col_idx] SCREAMING_SNAKE_CASE_ = above_to_left_elt + above_to_right_elt def a__ ( __UpperCamelCase ): if not isinstance(__UpperCamelCase , __UpperCamelCase ): raise TypeError("The input value of 'num_rows' should be 'int'" ) if num_rows == 0: return [] elif num_rows < 0: raise ValueError( "The input value of 'num_rows' should be greater than or equal to 0" ) SCREAMING_SNAKE_CASE_ = [[1]] for row_index in range(1 , __UpperCamelCase ): SCREAMING_SNAKE_CASE_ = [0] + result[-1] + [0] SCREAMING_SNAKE_CASE_ = row_index + 1 # Calculate the number of distinct elements in a row SCREAMING_SNAKE_CASE_ = sum(divmod(__UpperCamelCase , 2 ) ) SCREAMING_SNAKE_CASE_ = [ temp_row[i - 1] + temp_row[i] for i in range(1 , distinct_elements + 1 ) ] SCREAMING_SNAKE_CASE_ = row_first_half[: (row_index + 1) // 2] row_second_half.reverse() SCREAMING_SNAKE_CASE_ = row_first_half + row_second_half result.append(__UpperCamelCase ) return result def a__ ( ): from collections.abc import Callable from timeit import timeit def benchmark_a_function(__UpperCamelCase , __UpperCamelCase ) -> None: SCREAMING_SNAKE_CASE_ = F'''{func.__name__}({value})''' SCREAMING_SNAKE_CASE_ = timeit(F'''__main__.{call}''' , setup="import __main__" ) # print(f"{call:38} = {func(value)} -- {timing:.4f} seconds") print(F'''{call:38} -- {timing:.4f} seconds''' ) for value in range(1_5 ): # (1, 7, 14): for func in (generate_pascal_triangle, generate_pascal_triangle_optimized): benchmark_a_function(__UpperCamelCase , __UpperCamelCase ) print() if __name__ == "__main__": import doctest doctest.testmod() benchmark()
305
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tf_available, is_torch_available A : List[str] = {"configuration_swin": ["SWIN_PRETRAINED_CONFIG_ARCHIVE_MAP", "SwinConfig", "SwinOnnxConfig"]} try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: A : Any = [ "SWIN_PRETRAINED_MODEL_ARCHIVE_LIST", "SwinForImageClassification", "SwinForMaskedImageModeling", "SwinModel", "SwinPreTrainedModel", "SwinBackbone", ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: A : str = [ "TF_SWIN_PRETRAINED_MODEL_ARCHIVE_LIST", "TFSwinForImageClassification", "TFSwinForMaskedImageModeling", "TFSwinModel", "TFSwinPreTrainedModel", ] if TYPE_CHECKING: from .configuration_swin import SWIN_PRETRAINED_CONFIG_ARCHIVE_MAP, SwinConfig, SwinOnnxConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_swin import ( SWIN_PRETRAINED_MODEL_ARCHIVE_LIST, SwinBackbone, SwinForImageClassification, SwinForMaskedImageModeling, SwinModel, SwinPreTrainedModel, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_swin import ( TF_SWIN_PRETRAINED_MODEL_ARCHIVE_LIST, TFSwinForImageClassification, TFSwinForMaskedImageModeling, TFSwinModel, TFSwinPreTrainedModel, ) else: import sys A : Tuple = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
305
1
import warnings from typing import List import numpy as np from ...processing_utils import ProcessorMixin from ...tokenization_utils_base import BatchEncoding from ...utils import is_flax_available, is_tf_available, is_torch_available class lowerCamelCase (SCREAMING_SNAKE_CASE__ ): """simple docstring""" lowerCamelCase__ = ['''image_processor''', '''tokenizer'''] lowerCamelCase__ = '''OwlViTImageProcessor''' lowerCamelCase__ = ('''CLIPTokenizer''', '''CLIPTokenizerFast''') def __init__( self : Any , __magic_name__ : Union[str, Any]=None , __magic_name__ : Any=None , **__magic_name__ : List[Any] ) -> Optional[Any]: SCREAMING_SNAKE_CASE_ = None if "feature_extractor" in kwargs: warnings.warn( "The `feature_extractor` argument is deprecated and will be removed in v5, use `image_processor`" " instead." , __magic_name__ , ) SCREAMING_SNAKE_CASE_ = kwargs.pop("feature_extractor" ) SCREAMING_SNAKE_CASE_ = image_processor if image_processor is not None else feature_extractor if image_processor is None: raise ValueError("You need to specify an `image_processor`." ) if tokenizer is None: raise ValueError("You need to specify a `tokenizer`." ) super().__init__(__magic_name__ , __magic_name__ ) def __call__( self : str , __magic_name__ : List[Any]=None , __magic_name__ : Optional[int]=None , __magic_name__ : int=None , __magic_name__ : List[str]="max_length" , __magic_name__ : Optional[Any]="np" , **__magic_name__ : Optional[int] ) -> int: if text is None and query_images is None and images is None: raise ValueError( "You have to specify at least one text or query image or image. All three cannot be none." ) if text is not None: if isinstance(__magic_name__ , __magic_name__ ) or (isinstance(__magic_name__ , __magic_name__ ) and not isinstance(text[0] , __magic_name__ )): SCREAMING_SNAKE_CASE_ = [self.tokenizer(__magic_name__ , padding=__magic_name__ , return_tensors=__magic_name__ , **__magic_name__ )] elif isinstance(__magic_name__ , __magic_name__ ) and isinstance(text[0] , __magic_name__ ): SCREAMING_SNAKE_CASE_ = [] # Maximum number of queries across batch SCREAMING_SNAKE_CASE_ = max([len(__magic_name__ ) for t in text] ) # Pad all batch samples to max number of text queries for t in text: if len(__magic_name__ ) != max_num_queries: SCREAMING_SNAKE_CASE_ = t + [" "] * (max_num_queries - len(__magic_name__ )) SCREAMING_SNAKE_CASE_ = self.tokenizer(__magic_name__ , padding=__magic_name__ , return_tensors=__magic_name__ , **__magic_name__ ) encodings.append(__magic_name__ ) else: raise TypeError("Input text should be a string, a list of strings or a nested list of strings" ) if return_tensors == "np": SCREAMING_SNAKE_CASE_ = np.concatenate([encoding["input_ids"] for encoding in encodings] , axis=0 ) SCREAMING_SNAKE_CASE_ = np.concatenate([encoding["attention_mask"] for encoding in encodings] , axis=0 ) elif return_tensors == "jax" and is_flax_available(): import jax.numpy as jnp SCREAMING_SNAKE_CASE_ = jnp.concatenate([encoding["input_ids"] for encoding in encodings] , axis=0 ) SCREAMING_SNAKE_CASE_ = jnp.concatenate([encoding["attention_mask"] for encoding in encodings] , axis=0 ) elif return_tensors == "pt" and is_torch_available(): import torch SCREAMING_SNAKE_CASE_ = torch.cat([encoding["input_ids"] for encoding in encodings] , dim=0 ) SCREAMING_SNAKE_CASE_ = torch.cat([encoding["attention_mask"] for encoding in encodings] , dim=0 ) elif return_tensors == "tf" and is_tf_available(): import tensorflow as tf SCREAMING_SNAKE_CASE_ = tf.stack([encoding["input_ids"] for encoding in encodings] , axis=0 ) SCREAMING_SNAKE_CASE_ = tf.stack([encoding["attention_mask"] for encoding in encodings] , axis=0 ) else: raise ValueError("Target return tensor type could not be returned" ) SCREAMING_SNAKE_CASE_ = BatchEncoding() SCREAMING_SNAKE_CASE_ = input_ids SCREAMING_SNAKE_CASE_ = attention_mask if query_images is not None: SCREAMING_SNAKE_CASE_ = BatchEncoding() SCREAMING_SNAKE_CASE_ = self.image_processor( __magic_name__ , return_tensors=__magic_name__ , **__magic_name__ ).pixel_values SCREAMING_SNAKE_CASE_ = query_pixel_values if images is not None: SCREAMING_SNAKE_CASE_ = self.image_processor(__magic_name__ , return_tensors=__magic_name__ , **__magic_name__ ) if text is not None and images is not None: SCREAMING_SNAKE_CASE_ = image_features.pixel_values return encoding elif query_images is not None and images is not None: SCREAMING_SNAKE_CASE_ = image_features.pixel_values return encoding elif text is not None or query_images is not None: return encoding else: return BatchEncoding(data=dict(**__magic_name__ ) , tensor_type=__magic_name__ ) def __A ( self : Union[str, Any] , *__magic_name__ : Union[str, Any] , **__magic_name__ : Union[str, Any] ) -> Optional[Any]: return self.image_processor.post_process(*__magic_name__ , **__magic_name__ ) def __A ( self : List[Any] , *__magic_name__ : str , **__magic_name__ : Dict ) -> str: return self.image_processor.post_process_object_detection(*__magic_name__ , **__magic_name__ ) def __A ( self : int , *__magic_name__ : Any , **__magic_name__ : Tuple ) -> List[Any]: return self.image_processor.post_process_image_guided_detection(*__magic_name__ , **__magic_name__ ) def __A ( self : Dict , *__magic_name__ : List[Any] , **__magic_name__ : Tuple ) -> Dict: return self.tokenizer.batch_decode(*__magic_name__ , **__magic_name__ ) def __A ( self : Union[str, Any] , *__magic_name__ : Optional[Any] , **__magic_name__ : List[str] ) -> Dict: return self.tokenizer.decode(*__magic_name__ , **__magic_name__ ) @property def __A ( self : Optional[int] ) -> Union[str, Any]: warnings.warn( "`feature_extractor_class` is deprecated and will be removed in v5. Use `image_processor_class` instead." , __magic_name__ , ) return self.image_processor_class @property def __A ( self : Any ) -> List[str]: warnings.warn( "`feature_extractor` is deprecated and will be removed in v5. Use `image_processor` instead." , __magic_name__ , ) return self.image_processor
305
import tempfile import unittest import numpy as np from huggingface_hub import HfFolder, delete_repo from requests.exceptions import HTTPError from transformers import BertConfig, is_flax_available from transformers.testing_utils import TOKEN, USER, is_staging_test, require_flax if is_flax_available(): import os from flax.core.frozen_dict import unfreeze from flax.traverse_util import flatten_dict from transformers import FlaxBertModel A : Union[str, Any] = "0.12" # assumed parallelism: 8 @require_flax @is_staging_test class lowerCamelCase (unittest.TestCase ): """simple docstring""" @classmethod def __A ( cls : Any ) -> Dict: SCREAMING_SNAKE_CASE_ = TOKEN HfFolder.save_token(__magic_name__ ) @classmethod def __A ( cls : Optional[int] ) -> Tuple: try: delete_repo(token=cls._token , repo_id="test-model-flax" ) except HTTPError: pass try: delete_repo(token=cls._token , repo_id="valid_org/test-model-flax-org" ) except HTTPError: pass def __A ( self : str ) -> str: SCREAMING_SNAKE_CASE_ = BertConfig( vocab_size=99 , hidden_size=32 , num_hidden_layers=5 , num_attention_heads=4 , intermediate_size=37 ) SCREAMING_SNAKE_CASE_ = FlaxBertModel(__magic_name__ ) model.push_to_hub("test-model-flax" , use_auth_token=self._token ) SCREAMING_SNAKE_CASE_ = FlaxBertModel.from_pretrained(F'''{USER}/test-model-flax''' ) SCREAMING_SNAKE_CASE_ = flatten_dict(unfreeze(model.params ) ) SCREAMING_SNAKE_CASE_ = flatten_dict(unfreeze(new_model.params ) ) for key in base_params.keys(): SCREAMING_SNAKE_CASE_ = (base_params[key] - new_params[key]).sum().item() self.assertLessEqual(__magic_name__ , 1e-3 , msg=F'''{key} not identical''' ) # Reset repo delete_repo(token=self._token , repo_id="test-model-flax" ) # Push to hub via save_pretrained with tempfile.TemporaryDirectory() as tmp_dir: model.save_pretrained(__magic_name__ , repo_id="test-model-flax" , push_to_hub=__magic_name__ , use_auth_token=self._token ) SCREAMING_SNAKE_CASE_ = FlaxBertModel.from_pretrained(F'''{USER}/test-model-flax''' ) SCREAMING_SNAKE_CASE_ = flatten_dict(unfreeze(model.params ) ) SCREAMING_SNAKE_CASE_ = flatten_dict(unfreeze(new_model.params ) ) for key in base_params.keys(): SCREAMING_SNAKE_CASE_ = (base_params[key] - new_params[key]).sum().item() self.assertLessEqual(__magic_name__ , 1e-3 , msg=F'''{key} not identical''' ) def __A ( self : int ) -> Tuple: SCREAMING_SNAKE_CASE_ = BertConfig( vocab_size=99 , hidden_size=32 , num_hidden_layers=5 , num_attention_heads=4 , intermediate_size=37 ) SCREAMING_SNAKE_CASE_ = FlaxBertModel(__magic_name__ ) model.push_to_hub("valid_org/test-model-flax-org" , use_auth_token=self._token ) SCREAMING_SNAKE_CASE_ = FlaxBertModel.from_pretrained("valid_org/test-model-flax-org" ) SCREAMING_SNAKE_CASE_ = flatten_dict(unfreeze(model.params ) ) SCREAMING_SNAKE_CASE_ = flatten_dict(unfreeze(new_model.params ) ) for key in base_params.keys(): SCREAMING_SNAKE_CASE_ = (base_params[key] - new_params[key]).sum().item() self.assertLessEqual(__magic_name__ , 1e-3 , msg=F'''{key} not identical''' ) # Reset repo delete_repo(token=self._token , repo_id="valid_org/test-model-flax-org" ) # Push to hub via save_pretrained with tempfile.TemporaryDirectory() as tmp_dir: model.save_pretrained( __magic_name__ , repo_id="valid_org/test-model-flax-org" , push_to_hub=__magic_name__ , use_auth_token=self._token ) SCREAMING_SNAKE_CASE_ = FlaxBertModel.from_pretrained("valid_org/test-model-flax-org" ) SCREAMING_SNAKE_CASE_ = flatten_dict(unfreeze(model.params ) ) SCREAMING_SNAKE_CASE_ = flatten_dict(unfreeze(new_model.params ) ) for key in base_params.keys(): SCREAMING_SNAKE_CASE_ = (base_params[key] - new_params[key]).sum().item() self.assertLessEqual(__magic_name__ , 1e-3 , msg=F'''{key} not identical''' ) def a__ ( __UpperCamelCase , __UpperCamelCase ): SCREAMING_SNAKE_CASE_ = True SCREAMING_SNAKE_CASE_ = flatten_dict(modela.params ) SCREAMING_SNAKE_CASE_ = flatten_dict(modela.params ) for key in flat_params_a.keys(): if np.sum(np.abs(flat_params_a[key] - flat_params_a[key] ) ) > 1E-4: SCREAMING_SNAKE_CASE_ = False return models_are_equal @require_flax class lowerCamelCase (unittest.TestCase ): """simple docstring""" def __A ( self : str ) -> Dict: SCREAMING_SNAKE_CASE_ = BertConfig.from_pretrained("hf-internal-testing/tiny-bert-flax-only" ) SCREAMING_SNAKE_CASE_ = FlaxBertModel(__magic_name__ ) SCREAMING_SNAKE_CASE_ = "bert" with tempfile.TemporaryDirectory() as tmp_dir: model.save_pretrained(os.path.join(__magic_name__ , __magic_name__ ) ) with self.assertRaises(__magic_name__ ): SCREAMING_SNAKE_CASE_ = FlaxBertModel.from_pretrained(__magic_name__ ) SCREAMING_SNAKE_CASE_ = FlaxBertModel.from_pretrained(__magic_name__ , subfolder=__magic_name__ ) self.assertTrue(check_models_equal(__magic_name__ , __magic_name__ ) ) def __A ( self : Optional[Any] ) -> Tuple: SCREAMING_SNAKE_CASE_ = BertConfig.from_pretrained("hf-internal-testing/tiny-bert-flax-only" ) SCREAMING_SNAKE_CASE_ = FlaxBertModel(__magic_name__ ) SCREAMING_SNAKE_CASE_ = "bert" with tempfile.TemporaryDirectory() as tmp_dir: model.save_pretrained(os.path.join(__magic_name__ , __magic_name__ ) , max_shard_size="10KB" ) with self.assertRaises(__magic_name__ ): SCREAMING_SNAKE_CASE_ = FlaxBertModel.from_pretrained(__magic_name__ ) SCREAMING_SNAKE_CASE_ = FlaxBertModel.from_pretrained(__magic_name__ , subfolder=__magic_name__ ) self.assertTrue(check_models_equal(__magic_name__ , __magic_name__ ) ) def __A ( self : Optional[int] ) -> Dict: SCREAMING_SNAKE_CASE_ = "bert" SCREAMING_SNAKE_CASE_ = "hf-internal-testing/tiny-random-bert-subfolder" with self.assertRaises(__magic_name__ ): SCREAMING_SNAKE_CASE_ = FlaxBertModel.from_pretrained(__magic_name__ ) SCREAMING_SNAKE_CASE_ = FlaxBertModel.from_pretrained(__magic_name__ , subfolder=__magic_name__ ) self.assertIsNotNone(__magic_name__ ) def __A ( self : List[str] ) -> Dict: SCREAMING_SNAKE_CASE_ = "bert" SCREAMING_SNAKE_CASE_ = "hf-internal-testing/tiny-random-bert-sharded-subfolder" with self.assertRaises(__magic_name__ ): SCREAMING_SNAKE_CASE_ = FlaxBertModel.from_pretrained(__magic_name__ ) SCREAMING_SNAKE_CASE_ = FlaxBertModel.from_pretrained(__magic_name__ , subfolder=__magic_name__ ) self.assertIsNotNone(__magic_name__ )
305
1
from typing import Callable, Dict, Optional, Tuple import torch from torch import nn from torch.distributions import ( AffineTransform, Distribution, Independent, NegativeBinomial, Normal, StudentT, TransformedDistribution, ) class lowerCamelCase (SCREAMING_SNAKE_CASE__ ): """simple docstring""" def __init__( self : List[str] , __magic_name__ : Distribution , __magic_name__ : Tuple=None , __magic_name__ : Optional[Any]=None , __magic_name__ : Optional[int]=0 ) -> Optional[Any]: SCREAMING_SNAKE_CASE_ = 1.0 if scale is None else scale SCREAMING_SNAKE_CASE_ = 0.0 if loc is None else loc super().__init__(__magic_name__ , [AffineTransform(loc=self.loc , scale=self.scale , event_dim=__magic_name__ )] ) @property def __A ( self : Dict ) -> Any: return self.base_dist.mean * self.scale + self.loc @property def __A ( self : List[Any] ) -> Dict: return self.base_dist.variance * self.scale**2 @property def __A ( self : str ) -> Any: return self.variance.sqrt() class lowerCamelCase (nn.Module ): """simple docstring""" def __init__( self : Dict , __magic_name__ : int , __magic_name__ : Dict[str, int] , __magic_name__ : Callable[..., Tuple[torch.Tensor]] , **__magic_name__ : int ) -> None: super().__init__(**__magic_name__ ) SCREAMING_SNAKE_CASE_ = args_dim SCREAMING_SNAKE_CASE_ = nn.ModuleList([nn.Linear(__magic_name__ , __magic_name__ ) for dim in args_dim.values()] ) SCREAMING_SNAKE_CASE_ = domain_map def __A ( self : Any , __magic_name__ : torch.Tensor ) -> Tuple[torch.Tensor]: SCREAMING_SNAKE_CASE_ = [proj(__magic_name__ ) for proj in self.proj] return self.domain_map(*__magic_name__ ) class lowerCamelCase (nn.Module ): """simple docstring""" def __init__( self : str , __magic_name__ : List[Any] ) -> str: super().__init__() SCREAMING_SNAKE_CASE_ = function def __A ( self : Union[str, Any] , __magic_name__ : Tuple , *__magic_name__ : Tuple ) -> List[Any]: return self.function(__magic_name__ , *__magic_name__ ) class lowerCamelCase : """simple docstring""" lowerCamelCase__ = 42 lowerCamelCase__ = 42 lowerCamelCase__ = 42 def __init__( self : List[Any] , __magic_name__ : int = 1 ) -> None: SCREAMING_SNAKE_CASE_ = dim SCREAMING_SNAKE_CASE_ = {k: dim * self.args_dim[k] for k in self.args_dim} def __A ( self : int , __magic_name__ : Optional[int] ) -> Dict: if self.dim == 1: return self.distribution_class(*__magic_name__ ) else: return Independent(self.distribution_class(*__magic_name__ ) , 1 ) def __A ( self : Optional[Any] , __magic_name__ : str , __magic_name__ : Optional[torch.Tensor] = None , __magic_name__ : Optional[torch.Tensor] = None , ) -> Distribution: SCREAMING_SNAKE_CASE_ = self._base_distribution(__magic_name__ ) if loc is None and scale is None: return distr else: return AffineTransformed(__magic_name__ , loc=__magic_name__ , scale=__magic_name__ , event_dim=self.event_dim ) @property def __A ( self : str ) -> Tuple: return () if self.dim == 1 else (self.dim,) @property def __A ( self : str ) -> int: return len(self.event_shape ) @property def __A ( self : Union[str, Any] ) -> float: return 0.0 def __A ( self : Union[str, Any] , __magic_name__ : int ) -> nn.Module: return ParameterProjection( in_features=__magic_name__ , args_dim=self.args_dim , domain_map=LambdaLayer(self.domain_map ) , ) def __A ( self : Tuple , *__magic_name__ : torch.Tensor ) -> Union[str, Any]: raise NotImplementedError() @staticmethod def __A ( __magic_name__ : torch.Tensor ) -> torch.Tensor: return (x + torch.sqrt(torch.square(__magic_name__ ) + 4.0 )) / 2.0 class lowerCamelCase (SCREAMING_SNAKE_CASE__ ): """simple docstring""" lowerCamelCase__ = {"df": 1, "loc": 1, "scale": 1} lowerCamelCase__ = StudentT @classmethod def __A ( cls : Any , __magic_name__ : torch.Tensor , __magic_name__ : torch.Tensor , __magic_name__ : torch.Tensor ) -> Tuple: SCREAMING_SNAKE_CASE_ = cls.squareplus(__magic_name__ ).clamp_min(torch.finfo(scale.dtype ).eps ) SCREAMING_SNAKE_CASE_ = 2.0 + cls.squareplus(__magic_name__ ) return df.squeeze(-1 ), loc.squeeze(-1 ), scale.squeeze(-1 ) class lowerCamelCase (SCREAMING_SNAKE_CASE__ ): """simple docstring""" lowerCamelCase__ = {"loc": 1, "scale": 1} lowerCamelCase__ = Normal @classmethod def __A ( cls : Union[str, Any] , __magic_name__ : torch.Tensor , __magic_name__ : torch.Tensor ) -> Tuple: SCREAMING_SNAKE_CASE_ = cls.squareplus(__magic_name__ ).clamp_min(torch.finfo(scale.dtype ).eps ) return loc.squeeze(-1 ), scale.squeeze(-1 ) class lowerCamelCase (SCREAMING_SNAKE_CASE__ ): """simple docstring""" lowerCamelCase__ = {"total_count": 1, "logits": 1} lowerCamelCase__ = NegativeBinomial @classmethod def __A ( cls : Any , __magic_name__ : torch.Tensor , __magic_name__ : torch.Tensor ) -> str: SCREAMING_SNAKE_CASE_ = cls.squareplus(__magic_name__ ) return total_count.squeeze(-1 ), logits.squeeze(-1 ) def __A ( self : Tuple , __magic_name__ : Union[str, Any] ) -> Distribution: SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = distr_args if self.dim == 1: return self.distribution_class(total_count=__magic_name__ , logits=__magic_name__ ) else: return Independent(self.distribution_class(total_count=__magic_name__ , logits=__magic_name__ ) , 1 ) def __A ( self : int , __magic_name__ : List[str] , __magic_name__ : Optional[torch.Tensor] = None , __magic_name__ : Optional[torch.Tensor] = None ) -> Distribution: SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = distr_args if scale is not None: # See scaling property of Gamma. logits += scale.log() return self._base_distribution((total_count, logits) )
305
# Copyright 2021 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. import argparse import os from accelerate.utils import ComputeEnvironment from .cluster import get_cluster_input from .config_args import cache_dir, default_config_file, default_yaml_config_file, load_config_from_file # noqa: F401 from .config_utils import _ask_field, _ask_options, _convert_compute_environment # noqa: F401 from .sagemaker import get_sagemaker_input A : Union[str, Any] = "Launches a series of prompts to create and save a `default_config.yaml` configuration file for your training system. Should always be ran first on your machine" def a__ ( ): SCREAMING_SNAKE_CASE_ = _ask_options( "In which compute environment are you running?" , ["This machine", "AWS (Amazon SageMaker)"] , _convert_compute_environment , ) if compute_environment == ComputeEnvironment.AMAZON_SAGEMAKER: SCREAMING_SNAKE_CASE_ = get_sagemaker_input() else: SCREAMING_SNAKE_CASE_ = get_cluster_input() return config def a__ ( __UpperCamelCase=None ): if subparsers is not None: SCREAMING_SNAKE_CASE_ = subparsers.add_parser("config" , description=__UpperCamelCase ) else: SCREAMING_SNAKE_CASE_ = argparse.ArgumentParser("Accelerate config command" , description=__UpperCamelCase ) parser.add_argument( "--config_file" , default=__UpperCamelCase , help=( "The path to use to store the config file. Will default to a file named default_config.yaml in the cache " "location, which is the content of the environment `HF_HOME` suffixed with 'accelerate', or if you don't have " "such an environment variable, your cache directory ('~/.cache' or the content of `XDG_CACHE_HOME`) suffixed " "with 'huggingface'." ) , ) if subparsers is not None: parser.set_defaults(func=__UpperCamelCase ) return parser def a__ ( __UpperCamelCase ): SCREAMING_SNAKE_CASE_ = get_user_input() if args.config_file is not None: SCREAMING_SNAKE_CASE_ = args.config_file else: if not os.path.isdir(__UpperCamelCase ): os.makedirs(__UpperCamelCase ) SCREAMING_SNAKE_CASE_ = default_yaml_config_file if config_file.endswith(".json" ): config.to_json_file(__UpperCamelCase ) else: config.to_yaml_file(__UpperCamelCase ) print(F'''accelerate configuration saved at {config_file}''' ) def a__ ( ): SCREAMING_SNAKE_CASE_ = config_command_parser() SCREAMING_SNAKE_CASE_ = parser.parse_args() config_command(__UpperCamelCase ) if __name__ == "__main__": main()
305
1
import torch from torch import nn class lowerCamelCase (nn.Module ): """simple docstring""" def __init__( self : str , __magic_name__ : Union[str, Any] , __magic_name__ : Union[str, Any] , __magic_name__ : Tuple , __magic_name__ : int , __magic_name__ : int=1 , __magic_name__ : List[Any]=False ) -> List[Any]: super().__init__() SCREAMING_SNAKE_CASE_ = n_token SCREAMING_SNAKE_CASE_ = d_embed SCREAMING_SNAKE_CASE_ = d_proj SCREAMING_SNAKE_CASE_ = cutoffs + [n_token] SCREAMING_SNAKE_CASE_ = [0] + self.cutoffs SCREAMING_SNAKE_CASE_ = div_val SCREAMING_SNAKE_CASE_ = self.cutoffs[0] SCREAMING_SNAKE_CASE_ = len(self.cutoffs ) - 1 SCREAMING_SNAKE_CASE_ = self.shortlist_size + self.n_clusters if self.n_clusters > 0: SCREAMING_SNAKE_CASE_ = nn.Parameter(torch.zeros(self.n_clusters , self.d_embed ) ) SCREAMING_SNAKE_CASE_ = nn.Parameter(torch.zeros(self.n_clusters ) ) SCREAMING_SNAKE_CASE_ = nn.ModuleList() SCREAMING_SNAKE_CASE_ = nn.ParameterList() if div_val == 1: for i in range(len(self.cutoffs ) ): if d_proj != d_embed: self.out_projs.append(nn.Parameter(torch.FloatTensor(__magic_name__ , __magic_name__ ) ) ) else: self.out_projs.append(__magic_name__ ) self.out_layers.append(nn.Linear(__magic_name__ , __magic_name__ ) ) else: for i in range(len(self.cutoffs ) ): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = self.cutoff_ends[i], self.cutoff_ends[i + 1] SCREAMING_SNAKE_CASE_ = d_embed // (div_val**i) self.out_projs.append(nn.Parameter(torch.FloatTensor(__magic_name__ , __magic_name__ ) ) ) self.out_layers.append(nn.Linear(__magic_name__ , r_idx - l_idx ) ) SCREAMING_SNAKE_CASE_ = keep_order def __A ( self : Optional[int] , __magic_name__ : List[Any] , __magic_name__ : Tuple , __magic_name__ : int , __magic_name__ : List[Any] ) -> Any: if proj is None: SCREAMING_SNAKE_CASE_ = nn.functional.linear(__magic_name__ , __magic_name__ , bias=__magic_name__ ) else: # if CUDA_MAJOR <= 9 and CUDA_MINOR <= 1: SCREAMING_SNAKE_CASE_ = nn.functional.linear(__magic_name__ , proj.t().contiguous() ) SCREAMING_SNAKE_CASE_ = nn.functional.linear(__magic_name__ , __magic_name__ , bias=__magic_name__ ) # else: # logit = torch.einsum('bd,de,ev->bv', (hidden, proj, weight.t())) # if bias is not None: # logit = logit + bias return logit def __A ( self : List[Any] , __magic_name__ : Dict , __magic_name__ : List[str]=None , __magic_name__ : List[Any]=False ) -> Tuple: if labels is not None: # Shift so that tokens < n predict n SCREAMING_SNAKE_CASE_ = hidden[..., :-1, :].contiguous() SCREAMING_SNAKE_CASE_ = labels[..., 1:].contiguous() SCREAMING_SNAKE_CASE_ = hidden.view(-1 , hidden.size(-1 ) ) SCREAMING_SNAKE_CASE_ = labels.view(-1 ) if hidden.size(0 ) != labels.size(0 ): raise RuntimeError("Input and labels should have the same size in the batch dimension." ) else: SCREAMING_SNAKE_CASE_ = hidden.view(-1 , hidden.size(-1 ) ) if self.n_clusters == 0: SCREAMING_SNAKE_CASE_ = self._compute_logit(__magic_name__ , self.out_layers[0].weight , self.out_layers[0].bias , self.out_projs[0] ) if labels is not None: SCREAMING_SNAKE_CASE_ = labels != -100 SCREAMING_SNAKE_CASE_ = torch.zeros_like(__magic_name__ , dtype=hidden.dtype , device=hidden.device ) SCREAMING_SNAKE_CASE_ = ( -nn.functional.log_softmax(__magic_name__ , dim=-1 )[mask].gather(1 , labels[mask].unsqueeze(1 ) ).squeeze(1 ) ) else: SCREAMING_SNAKE_CASE_ = nn.functional.log_softmax(__magic_name__ , dim=-1 ) else: # construct weights and biases SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = [], [] for i in range(len(self.cutoffs ) ): if self.div_val == 1: SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = self.cutoff_ends[i], self.cutoff_ends[i + 1] SCREAMING_SNAKE_CASE_ = self.out_layers[0].weight[l_idx:r_idx] SCREAMING_SNAKE_CASE_ = self.out_layers[0].bias[l_idx:r_idx] else: SCREAMING_SNAKE_CASE_ = self.out_layers[i].weight SCREAMING_SNAKE_CASE_ = self.out_layers[i].bias if i == 0: SCREAMING_SNAKE_CASE_ = torch.cat([weight_i, self.cluster_weight] , dim=0 ) SCREAMING_SNAKE_CASE_ = torch.cat([bias_i, self.cluster_bias] , dim=0 ) weights.append(__magic_name__ ) biases.append(__magic_name__ ) SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = weights[0], biases[0], self.out_projs[0] SCREAMING_SNAKE_CASE_ = self._compute_logit(__magic_name__ , __magic_name__ , __magic_name__ , __magic_name__ ) SCREAMING_SNAKE_CASE_ = nn.functional.log_softmax(__magic_name__ , dim=1 ) if labels is None: SCREAMING_SNAKE_CASE_ = hidden.new_empty((head_logit.size(0 ), self.n_token) ) else: SCREAMING_SNAKE_CASE_ = torch.zeros_like(__magic_name__ , dtype=hidden.dtype , device=hidden.device ) SCREAMING_SNAKE_CASE_ = 0 SCREAMING_SNAKE_CASE_ = [0] + self.cutoffs for i in range(len(__magic_name__ ) - 1 ): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = cutoff_values[i], cutoff_values[i + 1] if labels is not None: SCREAMING_SNAKE_CASE_ = (labels >= l_idx) & (labels < r_idx) SCREAMING_SNAKE_CASE_ = mask_i.nonzero().squeeze() if indices_i.numel() == 0: continue SCREAMING_SNAKE_CASE_ = labels.index_select(0 , __magic_name__ ) - l_idx SCREAMING_SNAKE_CASE_ = head_logprob.index_select(0 , __magic_name__ ) SCREAMING_SNAKE_CASE_ = hidden.index_select(0 , __magic_name__ ) else: SCREAMING_SNAKE_CASE_ = hidden if i == 0: if labels is not None: SCREAMING_SNAKE_CASE_ = head_logprob_i.gather(1 , target_i[:, None] ).squeeze(1 ) else: SCREAMING_SNAKE_CASE_ = head_logprob[:, : self.cutoffs[0]] else: SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = weights[i], biases[i], self.out_projs[i] SCREAMING_SNAKE_CASE_ = self._compute_logit(__magic_name__ , __magic_name__ , __magic_name__ , __magic_name__ ) SCREAMING_SNAKE_CASE_ = nn.functional.log_softmax(__magic_name__ , dim=1 ) SCREAMING_SNAKE_CASE_ = self.cutoffs[0] + i - 1 # No probability for the head cluster if labels is not None: SCREAMING_SNAKE_CASE_ = head_logprob_i[:, cluster_prob_idx] + tail_logprob_i.gather( 1 , target_i[:, None] ).squeeze(1 ) else: SCREAMING_SNAKE_CASE_ = head_logprob[:, cluster_prob_idx, None] + tail_logprob_i SCREAMING_SNAKE_CASE_ = logprob_i if labels is not None: if (hasattr(self , "keep_order" ) and self.keep_order) or keep_order: out.index_copy_(0 , __magic_name__ , -logprob_i ) else: out[offset : offset + logprob_i.size(0 )].copy_(-logprob_i ) offset += logprob_i.size(0 ) return out def __A ( self : Dict , __magic_name__ : Optional[Any] ) -> List[Any]: if self.n_clusters == 0: SCREAMING_SNAKE_CASE_ = self._compute_logit(__magic_name__ , self.out_layers[0].weight , self.out_layers[0].bias , self.out_projs[0] ) return nn.functional.log_softmax(__magic_name__ , dim=-1 ) else: # construct weights and biases SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = [], [] for i in range(len(self.cutoffs ) ): if self.div_val == 1: SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = self.cutoff_ends[i], self.cutoff_ends[i + 1] SCREAMING_SNAKE_CASE_ = self.out_layers[0].weight[l_idx:r_idx] SCREAMING_SNAKE_CASE_ = self.out_layers[0].bias[l_idx:r_idx] else: SCREAMING_SNAKE_CASE_ = self.out_layers[i].weight SCREAMING_SNAKE_CASE_ = self.out_layers[i].bias if i == 0: SCREAMING_SNAKE_CASE_ = torch.cat([weight_i, self.cluster_weight] , dim=0 ) SCREAMING_SNAKE_CASE_ = torch.cat([bias_i, self.cluster_bias] , dim=0 ) weights.append(__magic_name__ ) biases.append(__magic_name__ ) SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = weights[0], biases[0], self.out_projs[0] SCREAMING_SNAKE_CASE_ = self._compute_logit(__magic_name__ , __magic_name__ , __magic_name__ , __magic_name__ ) SCREAMING_SNAKE_CASE_ = hidden.new_empty((head_logit.size(0 ), self.n_token) ) SCREAMING_SNAKE_CASE_ = nn.functional.log_softmax(__magic_name__ , dim=1 ) SCREAMING_SNAKE_CASE_ = [0] + self.cutoffs for i in range(len(__magic_name__ ) - 1 ): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = cutoff_values[i], cutoff_values[i + 1] if i == 0: SCREAMING_SNAKE_CASE_ = head_logprob[:, : self.cutoffs[0]] else: SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = weights[i], biases[i], self.out_projs[i] SCREAMING_SNAKE_CASE_ = self._compute_logit(__magic_name__ , __magic_name__ , __magic_name__ , __magic_name__ ) SCREAMING_SNAKE_CASE_ = nn.functional.log_softmax(__magic_name__ , dim=1 ) SCREAMING_SNAKE_CASE_ = head_logprob[:, -i] + tail_logprob_i SCREAMING_SNAKE_CASE_ = logprob_i return out
305
from dataclasses import dataclass, field from typing import ClassVar, Dict from ..features import Features, Value from .base import TaskTemplate @dataclass(frozen=SCREAMING_SNAKE_CASE__ ) class lowerCamelCase (SCREAMING_SNAKE_CASE__ ): """simple docstring""" lowerCamelCase__ = field(default='''summarization''' , metadata={'''include_in_asdict_even_if_is_default''': True} ) lowerCamelCase__ = Features({'''text''': Value('''string''' )} ) lowerCamelCase__ = Features({'''summary''': Value('''string''' )} ) lowerCamelCase__ = "text" lowerCamelCase__ = "summary" @property def __A ( self : Dict ) -> Dict[str, str]: return {self.text_column: "text", self.summary_column: "summary"}
305
1
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tokenizers_available, is_torch_available A : Tuple = { "configuration_bloom": ["BLOOM_PRETRAINED_CONFIG_ARCHIVE_MAP", "BloomConfig", "BloomOnnxConfig"], } try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: A : Optional[int] = ["BloomTokenizerFast"] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: A : Dict = [ "BLOOM_PRETRAINED_MODEL_ARCHIVE_LIST", "BloomForCausalLM", "BloomModel", "BloomPreTrainedModel", "BloomForSequenceClassification", "BloomForTokenClassification", "BloomForQuestionAnswering", ] if TYPE_CHECKING: from .configuration_bloom import BLOOM_PRETRAINED_CONFIG_ARCHIVE_MAP, BloomConfig, BloomOnnxConfig try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_bloom_fast import BloomTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_bloom import ( BLOOM_PRETRAINED_MODEL_ARCHIVE_LIST, BloomForCausalLM, BloomForQuestionAnswering, BloomForSequenceClassification, BloomForTokenClassification, BloomModel, BloomPreTrainedModel, ) else: import sys A : int = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
305
from ....utils import logging A : List[str] = logging.get_logger(__name__) class lowerCamelCase (SCREAMING_SNAKE_CASE__ ): """simple docstring""" def __init__( self : List[str] , __magic_name__ : Optional[Any] , __magic_name__ : Any=None , __magic_name__ : List[str]=2_048 ) -> List[Any]: SCREAMING_SNAKE_CASE_ = config.__dict__ SCREAMING_SNAKE_CASE_ = modal_hidden_size if num_labels: SCREAMING_SNAKE_CASE_ = num_labels
305
1
import importlib import json import os from collections import OrderedDict from typing import Dict, Optional, Union # Build the list of all image processors from ...configuration_utils import PretrainedConfig from ...dynamic_module_utils import get_class_from_dynamic_module, resolve_trust_remote_code from ...image_processing_utils import ImageProcessingMixin from ...utils import CONFIG_NAME, IMAGE_PROCESSOR_NAME, get_file_from_repo, logging from .auto_factory import _LazyAutoMapping from .configuration_auto import ( CONFIG_MAPPING_NAMES, AutoConfig, model_type_to_module_name, replace_list_option_in_docstrings, ) A : str = logging.get_logger(__name__) A : str = OrderedDict( [ ("align", "EfficientNetImageProcessor"), ("beit", "BeitImageProcessor"), ("bit", "BitImageProcessor"), ("blip", "BlipImageProcessor"), ("blip-2", "BlipImageProcessor"), ("bridgetower", "BridgeTowerImageProcessor"), ("chinese_clip", "ChineseCLIPImageProcessor"), ("clip", "CLIPImageProcessor"), ("clipseg", "ViTImageProcessor"), ("conditional_detr", "ConditionalDetrImageProcessor"), ("convnext", "ConvNextImageProcessor"), ("convnextv2", "ConvNextImageProcessor"), ("cvt", "ConvNextImageProcessor"), ("data2vec-vision", "BeitImageProcessor"), ("deformable_detr", "DeformableDetrImageProcessor"), ("deit", "DeiTImageProcessor"), ("deta", "DetaImageProcessor"), ("detr", "DetrImageProcessor"), ("dinat", "ViTImageProcessor"), ("donut-swin", "DonutImageProcessor"), ("dpt", "DPTImageProcessor"), ("efficientformer", "EfficientFormerImageProcessor"), ("efficientnet", "EfficientNetImageProcessor"), ("flava", "FlavaImageProcessor"), ("focalnet", "BitImageProcessor"), ("git", "CLIPImageProcessor"), ("glpn", "GLPNImageProcessor"), ("groupvit", "CLIPImageProcessor"), ("imagegpt", "ImageGPTImageProcessor"), ("instructblip", "BlipImageProcessor"), ("layoutlmv2", "LayoutLMv2ImageProcessor"), ("layoutlmv3", "LayoutLMv3ImageProcessor"), ("levit", "LevitImageProcessor"), ("mask2former", "Mask2FormerImageProcessor"), ("maskformer", "MaskFormerImageProcessor"), ("mgp-str", "ViTImageProcessor"), ("mobilenet_v1", "MobileNetV1ImageProcessor"), ("mobilenet_v2", "MobileNetV2ImageProcessor"), ("mobilevit", "MobileViTImageProcessor"), ("mobilevit", "MobileViTImageProcessor"), ("mobilevitv2", "MobileViTImageProcessor"), ("nat", "ViTImageProcessor"), ("oneformer", "OneFormerImageProcessor"), ("owlvit", "OwlViTImageProcessor"), ("perceiver", "PerceiverImageProcessor"), ("pix2struct", "Pix2StructImageProcessor"), ("poolformer", "PoolFormerImageProcessor"), ("regnet", "ConvNextImageProcessor"), ("resnet", "ConvNextImageProcessor"), ("sam", "SamImageProcessor"), ("segformer", "SegformerImageProcessor"), ("swiftformer", "ViTImageProcessor"), ("swin", "ViTImageProcessor"), ("swin2sr", "Swin2SRImageProcessor"), ("swinv2", "ViTImageProcessor"), ("table-transformer", "DetrImageProcessor"), ("timesformer", "VideoMAEImageProcessor"), ("tvlt", "TvltImageProcessor"), ("upernet", "SegformerImageProcessor"), ("van", "ConvNextImageProcessor"), ("videomae", "VideoMAEImageProcessor"), ("vilt", "ViltImageProcessor"), ("vit", "ViTImageProcessor"), ("vit_hybrid", "ViTHybridImageProcessor"), ("vit_mae", "ViTImageProcessor"), ("vit_msn", "ViTImageProcessor"), ("xclip", "CLIPImageProcessor"), ("yolos", "YolosImageProcessor"), ] ) A : Optional[int] = _LazyAutoMapping(CONFIG_MAPPING_NAMES, IMAGE_PROCESSOR_MAPPING_NAMES) def a__ ( __UpperCamelCase ): for module_name, extractors in IMAGE_PROCESSOR_MAPPING_NAMES.items(): if class_name in extractors: SCREAMING_SNAKE_CASE_ = model_type_to_module_name(__UpperCamelCase ) SCREAMING_SNAKE_CASE_ = importlib.import_module(F'''.{module_name}''' , "transformers.models" ) try: return getattr(__UpperCamelCase , __UpperCamelCase ) except AttributeError: continue for _, extractor in IMAGE_PROCESSOR_MAPPING._extra_content.items(): if getattr(__UpperCamelCase , "__name__" , __UpperCamelCase ) == class_name: return extractor # We did not fine the class, but maybe it's because a dep is missing. In that case, the class will be in the main # init and we return the proper dummy to get an appropriate error message. SCREAMING_SNAKE_CASE_ = importlib.import_module("transformers" ) if hasattr(__UpperCamelCase , __UpperCamelCase ): return getattr(__UpperCamelCase , __UpperCamelCase ) return None def a__ ( __UpperCamelCase , __UpperCamelCase = None , __UpperCamelCase = False , __UpperCamelCase = False , __UpperCamelCase = None , __UpperCamelCase = None , __UpperCamelCase = None , __UpperCamelCase = False , **__UpperCamelCase , ): SCREAMING_SNAKE_CASE_ = get_file_from_repo( __UpperCamelCase , __UpperCamelCase , cache_dir=__UpperCamelCase , force_download=__UpperCamelCase , resume_download=__UpperCamelCase , proxies=__UpperCamelCase , use_auth_token=__UpperCamelCase , revision=__UpperCamelCase , local_files_only=__UpperCamelCase , ) if resolved_config_file is None: logger.info( "Could not locate the image processor configuration file, will try to use the model config instead." ) return {} with open(__UpperCamelCase , encoding="utf-8" ) as reader: return json.load(__UpperCamelCase ) class lowerCamelCase : """simple docstring""" def __init__( self : int ) -> List[Any]: raise EnvironmentError( "AutoImageProcessor is designed to be instantiated " "using the `AutoImageProcessor.from_pretrained(pretrained_model_name_or_path)` method." ) @classmethod @replace_list_option_in_docstrings(__magic_name__ ) def __A ( cls : List[str] , __magic_name__ : Dict , **__magic_name__ : Dict ) -> List[Any]: SCREAMING_SNAKE_CASE_ = kwargs.pop("config" , __magic_name__ ) SCREAMING_SNAKE_CASE_ = kwargs.pop("trust_remote_code" , __magic_name__ ) SCREAMING_SNAKE_CASE_ = True SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = ImageProcessingMixin.get_image_processor_dict(__magic_name__ , **__magic_name__ ) SCREAMING_SNAKE_CASE_ = config_dict.get("image_processor_type" , __magic_name__ ) SCREAMING_SNAKE_CASE_ = None if "AutoImageProcessor" in config_dict.get("auto_map" , {} ): SCREAMING_SNAKE_CASE_ = config_dict["auto_map"]["AutoImageProcessor"] # If we still don't have the image processor class, check if we're loading from a previous feature extractor config # and if so, infer the image processor class from there. if image_processor_class is None and image_processor_auto_map is None: SCREAMING_SNAKE_CASE_ = config_dict.pop("feature_extractor_type" , __magic_name__ ) if feature_extractor_class is not None: logger.warning( "Could not find image processor class in the image processor config or the model config. Loading" " based on pattern matching with the model's feature extractor configuration." ) SCREAMING_SNAKE_CASE_ = feature_extractor_class.replace("FeatureExtractor" , "ImageProcessor" ) if "AutoFeatureExtractor" in config_dict.get("auto_map" , {} ): SCREAMING_SNAKE_CASE_ = config_dict["auto_map"]["AutoFeatureExtractor"] SCREAMING_SNAKE_CASE_ = feature_extractor_auto_map.replace("FeatureExtractor" , "ImageProcessor" ) logger.warning( "Could not find image processor auto map in the image processor config or the model config." " Loading based on pattern matching with the model's feature extractor configuration." ) # If we don't find the image processor class in the image processor config, let's try the model config. if image_processor_class is None and image_processor_auto_map is None: if not isinstance(__magic_name__ , __magic_name__ ): SCREAMING_SNAKE_CASE_ = AutoConfig.from_pretrained(__magic_name__ , **__magic_name__ ) # It could be in `config.image_processor_type`` SCREAMING_SNAKE_CASE_ = getattr(__magic_name__ , "image_processor_type" , __magic_name__ ) if hasattr(__magic_name__ , "auto_map" ) and "AutoImageProcessor" in config.auto_map: SCREAMING_SNAKE_CASE_ = config.auto_map["AutoImageProcessor"] if image_processor_class is not None: SCREAMING_SNAKE_CASE_ = image_processor_class_from_name(__magic_name__ ) SCREAMING_SNAKE_CASE_ = image_processor_auto_map is not None SCREAMING_SNAKE_CASE_ = image_processor_class is not None or type(__magic_name__ ) in IMAGE_PROCESSOR_MAPPING SCREAMING_SNAKE_CASE_ = resolve_trust_remote_code( __magic_name__ , __magic_name__ , __magic_name__ , __magic_name__ ) if has_remote_code and trust_remote_code: SCREAMING_SNAKE_CASE_ = get_class_from_dynamic_module( __magic_name__ , __magic_name__ , **__magic_name__ ) SCREAMING_SNAKE_CASE_ = kwargs.pop("code_revision" , __magic_name__ ) if os.path.isdir(__magic_name__ ): image_processor_class.register_for_auto_class() return image_processor_class.from_dict(__magic_name__ , **__magic_name__ ) elif image_processor_class is not None: return image_processor_class.from_dict(__magic_name__ , **__magic_name__ ) # Last try: we use the IMAGE_PROCESSOR_MAPPING. elif type(__magic_name__ ) in IMAGE_PROCESSOR_MAPPING: SCREAMING_SNAKE_CASE_ = IMAGE_PROCESSOR_MAPPING[type(__magic_name__ )] return image_processor_class.from_dict(__magic_name__ , **__magic_name__ ) raise ValueError( F'''Unrecognized image processor in {pretrained_model_name_or_path}. Should have a ''' F'''`image_processor_type` key in its {IMAGE_PROCESSOR_NAME} of {CONFIG_NAME}, or one of the following ''' F'''`model_type` keys in its {CONFIG_NAME}: {", ".join(c for c in IMAGE_PROCESSOR_MAPPING_NAMES.keys() )}''' ) @staticmethod def __A ( __magic_name__ : List[str] , __magic_name__ : Union[str, Any] ) -> List[str]: IMAGE_PROCESSOR_MAPPING.register(__magic_name__ , __magic_name__ )
305
import warnings from typing import List, Optional, Union from ...processing_utils import ProcessorMixin from ...tokenization_utils_base import BatchEncoding, PaddingStrategy, PreTokenizedInput, TextInput, TruncationStrategy from ...utils import TensorType class lowerCamelCase (SCREAMING_SNAKE_CASE__ ): """simple docstring""" lowerCamelCase__ = ['''image_processor''', '''tokenizer'''] lowerCamelCase__ = '''ViltImageProcessor''' lowerCamelCase__ = ('''BertTokenizer''', '''BertTokenizerFast''') def __init__( self : Optional[int] , __magic_name__ : str=None , __magic_name__ : List[str]=None , **__magic_name__ : Any ) -> str: SCREAMING_SNAKE_CASE_ = None if "feature_extractor" in kwargs: warnings.warn( "The `feature_extractor` argument is deprecated and will be removed in v5, use `image_processor`" " instead." , __magic_name__ , ) SCREAMING_SNAKE_CASE_ = kwargs.pop("feature_extractor" ) SCREAMING_SNAKE_CASE_ = image_processor if image_processor is not None else feature_extractor if image_processor is None: raise ValueError("You need to specify an `image_processor`." ) if tokenizer is None: raise ValueError("You need to specify a `tokenizer`." ) super().__init__(__magic_name__ , __magic_name__ ) SCREAMING_SNAKE_CASE_ = self.image_processor def __call__( self : List[str] , __magic_name__ : List[str] , __magic_name__ : Union[TextInput, PreTokenizedInput, List[TextInput], List[PreTokenizedInput]] = None , __magic_name__ : bool = True , __magic_name__ : Union[bool, str, PaddingStrategy] = False , __magic_name__ : Union[bool, str, TruncationStrategy] = None , __magic_name__ : Optional[int] = None , __magic_name__ : int = 0 , __magic_name__ : Optional[int] = None , __magic_name__ : Optional[bool] = None , __magic_name__ : Optional[bool] = None , __magic_name__ : bool = False , __magic_name__ : bool = False , __magic_name__ : bool = False , __magic_name__ : bool = False , __magic_name__ : bool = True , __magic_name__ : Optional[Union[str, TensorType]] = None , **__magic_name__ : str , ) -> BatchEncoding: SCREAMING_SNAKE_CASE_ = self.tokenizer( text=__magic_name__ , add_special_tokens=__magic_name__ , padding=__magic_name__ , truncation=__magic_name__ , max_length=__magic_name__ , stride=__magic_name__ , pad_to_multiple_of=__magic_name__ , return_token_type_ids=__magic_name__ , return_attention_mask=__magic_name__ , return_overflowing_tokens=__magic_name__ , return_special_tokens_mask=__magic_name__ , return_offsets_mapping=__magic_name__ , return_length=__magic_name__ , verbose=__magic_name__ , return_tensors=__magic_name__ , **__magic_name__ , ) # add pixel_values + pixel_mask SCREAMING_SNAKE_CASE_ = self.image_processor(__magic_name__ , return_tensors=__magic_name__ ) encoding.update(__magic_name__ ) return encoding def __A ( self : Optional[int] , *__magic_name__ : List[Any] , **__magic_name__ : Optional[Any] ) -> Any: return self.tokenizer.batch_decode(*__magic_name__ , **__magic_name__ ) def __A ( self : Dict , *__magic_name__ : List[Any] , **__magic_name__ : Union[str, Any] ) -> str: return self.tokenizer.decode(*__magic_name__ , **__magic_name__ ) @property def __A ( self : Optional[int] ) -> Union[str, Any]: SCREAMING_SNAKE_CASE_ = self.tokenizer.model_input_names SCREAMING_SNAKE_CASE_ = self.image_processor.model_input_names return list(dict.fromkeys(tokenizer_input_names + image_processor_input_names ) ) @property def __A ( self : Dict ) -> List[Any]: warnings.warn( "`feature_extractor_class` is deprecated and will be removed in v5. Use `image_processor_class` instead." , __magic_name__ , ) return self.image_processor_class @property def __A ( self : int ) -> List[Any]: warnings.warn( "`feature_extractor` is deprecated and will be removed in v5. Use `image_processor` instead." , __magic_name__ , ) return self.image_processor
305
1
import json from typing import List, Optional, Tuple from tokenizers import normalizers from tokenizers.pre_tokenizers import BertPreTokenizer, PreTokenizer from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import logging from .tokenization_roformer import RoFormerTokenizer from .tokenization_utils import JiebaPreTokenizer A : Optional[int] = logging.get_logger(__name__) A : str = {"vocab_file": "vocab.txt", "tokenizer_file": "tokenizer.json"} A : Union[str, Any] = { "vocab_file": { "junnyu/roformer_chinese_small": "https://huggingface.co/junnyu/roformer_chinese_small/resolve/main/vocab.txt", "junnyu/roformer_chinese_base": "https://huggingface.co/junnyu/roformer_chinese_base/resolve/main/vocab.txt", "junnyu/roformer_chinese_char_small": ( "https://huggingface.co/junnyu/roformer_chinese_char_small/resolve/main/vocab.txt" ), "junnyu/roformer_chinese_char_base": ( "https://huggingface.co/junnyu/roformer_chinese_char_base/resolve/main/vocab.txt" ), "junnyu/roformer_small_discriminator": ( "https://huggingface.co/junnyu/roformer_small_discriminator/resolve/main/vocab.txt" ), "junnyu/roformer_small_generator": ( "https://huggingface.co/junnyu/roformer_small_generator/resolve/main/vocab.txt" ), } } A : Tuple = { "junnyu/roformer_chinese_small": 15_36, "junnyu/roformer_chinese_base": 15_36, "junnyu/roformer_chinese_char_small": 5_12, "junnyu/roformer_chinese_char_base": 5_12, "junnyu/roformer_small_discriminator": 1_28, "junnyu/roformer_small_generator": 1_28, } A : Union[str, Any] = { "junnyu/roformer_chinese_small": {"do_lower_case": True}, "junnyu/roformer_chinese_base": {"do_lower_case": True}, "junnyu/roformer_chinese_char_small": {"do_lower_case": True}, "junnyu/roformer_chinese_char_base": {"do_lower_case": True}, "junnyu/roformer_small_discriminator": {"do_lower_case": True}, "junnyu/roformer_small_generator": {"do_lower_case": True}, } class lowerCamelCase (SCREAMING_SNAKE_CASE__ ): """simple docstring""" lowerCamelCase__ = VOCAB_FILES_NAMES lowerCamelCase__ = PRETRAINED_VOCAB_FILES_MAP lowerCamelCase__ = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES lowerCamelCase__ = PRETRAINED_INIT_CONFIGURATION lowerCamelCase__ = RoFormerTokenizer def __init__( self : Union[str, Any] , __magic_name__ : Any=None , __magic_name__ : Tuple=None , __magic_name__ : List[Any]=True , __magic_name__ : Optional[Any]="[UNK]" , __magic_name__ : Dict="[SEP]" , __magic_name__ : Optional[Any]="[PAD]" , __magic_name__ : Dict="[CLS]" , __magic_name__ : Optional[int]="[MASK]" , __magic_name__ : Tuple=True , __magic_name__ : List[Any]=None , **__magic_name__ : Optional[Any] , ) -> List[Any]: super().__init__( __magic_name__ , tokenizer_file=__magic_name__ , do_lower_case=__magic_name__ , unk_token=__magic_name__ , sep_token=__magic_name__ , pad_token=__magic_name__ , cls_token=__magic_name__ , mask_token=__magic_name__ , tokenize_chinese_chars=__magic_name__ , strip_accents=__magic_name__ , **__magic_name__ , ) SCREAMING_SNAKE_CASE_ = json.loads(self.backend_tokenizer.normalizer.__getstate__() ) if ( pre_tok_state.get("lowercase" , __magic_name__ ) != do_lower_case or pre_tok_state.get("strip_accents" , __magic_name__ ) != strip_accents ): SCREAMING_SNAKE_CASE_ = getattr(__magic_name__ , pre_tok_state.pop("type" ) ) SCREAMING_SNAKE_CASE_ = do_lower_case SCREAMING_SNAKE_CASE_ = strip_accents SCREAMING_SNAKE_CASE_ = pre_tok_class(**__magic_name__ ) SCREAMING_SNAKE_CASE_ = do_lower_case def __getstate__( self : List[Any] ) -> Dict: SCREAMING_SNAKE_CASE_ = self.__dict__.copy() SCREAMING_SNAKE_CASE_ = BertPreTokenizer() return state def __setstate__( self : Optional[int] , __magic_name__ : int ) -> int: SCREAMING_SNAKE_CASE_ = d SCREAMING_SNAKE_CASE_ = self.__dict__["_tokenizer"].get_vocab() SCREAMING_SNAKE_CASE_ = PreTokenizer.custom(JiebaPreTokenizer(__magic_name__ ) ) def __A ( self : str , __magic_name__ : str , __magic_name__ : Dict=None ) -> Union[str, Any]: SCREAMING_SNAKE_CASE_ = [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 : Any , __magic_name__ : List[int] , __magic_name__ : Optional[List[int]] = None ) -> List[int]: SCREAMING_SNAKE_CASE_ = [self.sep_token_id] SCREAMING_SNAKE_CASE_ = [self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep ) * [0] return len(cls + token_ids_a + sep ) * [0] + len(token_ids_a + sep ) * [1] def __A ( self : Any , __magic_name__ : str , __magic_name__ : Optional[str] = None ) -> Tuple[str]: SCREAMING_SNAKE_CASE_ = self._tokenizer.model.save(__magic_name__ , name=__magic_name__ ) return tuple(__magic_name__ ) def __A ( self : str , __magic_name__ : Any , __magic_name__ : Tuple=None , __magic_name__ : List[Any]=None , __magic_name__ : List[Any]=False , **__magic_name__ : Optional[int] , ) -> Optional[Any]: SCREAMING_SNAKE_CASE_ = BertPreTokenizer() return super().save_pretrained(__magic_name__ , __magic_name__ , __magic_name__ , __magic_name__ , **__magic_name__ )
305
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 ..auto import CONFIG_MAPPING A : str = logging.get_logger(__name__) A : Optional[int] = { "microsoft/table-transformer-detection": ( "https://huggingface.co/microsoft/table-transformer-detection/resolve/main/config.json" ), } class lowerCamelCase (SCREAMING_SNAKE_CASE__ ): """simple docstring""" lowerCamelCase__ = '''table-transformer''' lowerCamelCase__ = ['''past_key_values'''] lowerCamelCase__ = { '''hidden_size''': '''d_model''', '''num_attention_heads''': '''encoder_attention_heads''', } def __init__( self : List[Any] , __magic_name__ : Optional[Any]=True , __magic_name__ : Dict=None , __magic_name__ : Any=3 , __magic_name__ : List[str]=100 , __magic_name__ : Union[str, Any]=6 , __magic_name__ : Dict=2_048 , __magic_name__ : str=8 , __magic_name__ : int=6 , __magic_name__ : List[Any]=2_048 , __magic_name__ : Optional[int]=8 , __magic_name__ : Optional[int]=0.0 , __magic_name__ : List[Any]=0.0 , __magic_name__ : Optional[Any]=True , __magic_name__ : List[Any]="relu" , __magic_name__ : List[str]=256 , __magic_name__ : List[str]=0.1 , __magic_name__ : int=0.0 , __magic_name__ : Optional[Any]=0.0 , __magic_name__ : Tuple=0.02 , __magic_name__ : str=1.0 , __magic_name__ : int=False , __magic_name__ : Dict="sine" , __magic_name__ : Union[str, Any]="resnet50" , __magic_name__ : Optional[Any]=True , __magic_name__ : str=False , __magic_name__ : List[str]=1 , __magic_name__ : int=5 , __magic_name__ : Union[str, Any]=2 , __magic_name__ : Tuple=1 , __magic_name__ : Optional[int]=1 , __magic_name__ : Optional[Any]=5 , __magic_name__ : Optional[int]=2 , __magic_name__ : Union[str, Any]=0.1 , **__magic_name__ : Tuple , ) -> str: 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." ) SCREAMING_SNAKE_CASE_ = CONFIG_MAPPING["resnet"](out_features=["stage4"] ) elif isinstance(__magic_name__ , __magic_name__ ): SCREAMING_SNAKE_CASE_ = backbone_config.get("model_type" ) SCREAMING_SNAKE_CASE_ = CONFIG_MAPPING[backbone_model_type] SCREAMING_SNAKE_CASE_ = config_class.from_dict(__magic_name__ ) # set timm attributes to None SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = None, None, None SCREAMING_SNAKE_CASE_ = use_timm_backbone SCREAMING_SNAKE_CASE_ = backbone_config SCREAMING_SNAKE_CASE_ = num_channels SCREAMING_SNAKE_CASE_ = num_queries SCREAMING_SNAKE_CASE_ = d_model SCREAMING_SNAKE_CASE_ = encoder_ffn_dim SCREAMING_SNAKE_CASE_ = encoder_layers SCREAMING_SNAKE_CASE_ = encoder_attention_heads SCREAMING_SNAKE_CASE_ = decoder_ffn_dim SCREAMING_SNAKE_CASE_ = decoder_layers SCREAMING_SNAKE_CASE_ = decoder_attention_heads SCREAMING_SNAKE_CASE_ = dropout SCREAMING_SNAKE_CASE_ = attention_dropout SCREAMING_SNAKE_CASE_ = activation_dropout SCREAMING_SNAKE_CASE_ = activation_function SCREAMING_SNAKE_CASE_ = init_std SCREAMING_SNAKE_CASE_ = init_xavier_std SCREAMING_SNAKE_CASE_ = encoder_layerdrop SCREAMING_SNAKE_CASE_ = decoder_layerdrop SCREAMING_SNAKE_CASE_ = encoder_layers SCREAMING_SNAKE_CASE_ = auxiliary_loss SCREAMING_SNAKE_CASE_ = position_embedding_type SCREAMING_SNAKE_CASE_ = backbone SCREAMING_SNAKE_CASE_ = use_pretrained_backbone SCREAMING_SNAKE_CASE_ = dilation # Hungarian matcher SCREAMING_SNAKE_CASE_ = class_cost SCREAMING_SNAKE_CASE_ = bbox_cost SCREAMING_SNAKE_CASE_ = giou_cost # Loss coefficients SCREAMING_SNAKE_CASE_ = mask_loss_coefficient SCREAMING_SNAKE_CASE_ = dice_loss_coefficient SCREAMING_SNAKE_CASE_ = bbox_loss_coefficient SCREAMING_SNAKE_CASE_ = giou_loss_coefficient SCREAMING_SNAKE_CASE_ = eos_coefficient super().__init__(is_encoder_decoder=__magic_name__ , **__magic_name__ ) @property def __A ( self : Union[str, Any] ) -> int: return self.encoder_attention_heads @property def __A ( self : Any ) -> int: return self.d_model class lowerCamelCase (SCREAMING_SNAKE_CASE__ ): """simple docstring""" lowerCamelCase__ = version.parse('''1.11''' ) @property def __A ( self : Tuple ) -> Mapping[str, Mapping[int, str]]: return OrderedDict( [ ("pixel_values", {0: "batch", 1: "num_channels", 2: "height", 3: "width"}), ("pixel_mask", {0: "batch"}), ] ) @property def __A ( self : Any ) -> float: return 1e-5 @property def __A ( self : int ) -> int: return 12
305
1
import argparse import json from pathlib import Path import requests import timm import torch from huggingface_hub import hf_hub_download from PIL import Image from transformers import AutoImageProcessor, SwinvaConfig, SwinvaForImageClassification def a__ ( __UpperCamelCase ): SCREAMING_SNAKE_CASE_ = SwinvaConfig() SCREAMING_SNAKE_CASE_ = swinva_name.split("_" ) SCREAMING_SNAKE_CASE_ = name_split[1] if "to" in name_split[3]: SCREAMING_SNAKE_CASE_ = int(name_split[3][-3:] ) else: SCREAMING_SNAKE_CASE_ = int(name_split[3] ) if "to" in name_split[2]: SCREAMING_SNAKE_CASE_ = int(name_split[2][-2:] ) else: SCREAMING_SNAKE_CASE_ = int(name_split[2][6:] ) if model_size == "tiny": SCREAMING_SNAKE_CASE_ = 9_6 SCREAMING_SNAKE_CASE_ = (2, 2, 6, 2) SCREAMING_SNAKE_CASE_ = (3, 6, 1_2, 2_4) elif model_size == "small": SCREAMING_SNAKE_CASE_ = 9_6 SCREAMING_SNAKE_CASE_ = (2, 2, 1_8, 2) SCREAMING_SNAKE_CASE_ = (3, 6, 1_2, 2_4) elif model_size == "base": SCREAMING_SNAKE_CASE_ = 1_2_8 SCREAMING_SNAKE_CASE_ = (2, 2, 1_8, 2) SCREAMING_SNAKE_CASE_ = (4, 8, 1_6, 3_2) else: SCREAMING_SNAKE_CASE_ = 1_9_2 SCREAMING_SNAKE_CASE_ = (2, 2, 1_8, 2) SCREAMING_SNAKE_CASE_ = (6, 1_2, 2_4, 4_8) if "to" in swinva_name: SCREAMING_SNAKE_CASE_ = (1_2, 1_2, 1_2, 6) if ("22k" in swinva_name) and ("to" not in swinva_name): SCREAMING_SNAKE_CASE_ = 2_1_8_4_1 SCREAMING_SNAKE_CASE_ = "huggingface/label-files" SCREAMING_SNAKE_CASE_ = "imagenet-22k-id2label.json" SCREAMING_SNAKE_CASE_ = json.load(open(hf_hub_download(__UpperCamelCase , __UpperCamelCase , repo_type="dataset" ) , "r" ) ) SCREAMING_SNAKE_CASE_ = {int(__UpperCamelCase ): v for k, v in idalabel.items()} SCREAMING_SNAKE_CASE_ = idalabel SCREAMING_SNAKE_CASE_ = {v: k for k, v in idalabel.items()} else: SCREAMING_SNAKE_CASE_ = 1_0_0_0 SCREAMING_SNAKE_CASE_ = "huggingface/label-files" SCREAMING_SNAKE_CASE_ = "imagenet-1k-id2label.json" SCREAMING_SNAKE_CASE_ = json.load(open(hf_hub_download(__UpperCamelCase , __UpperCamelCase , repo_type="dataset" ) , "r" ) ) SCREAMING_SNAKE_CASE_ = {int(__UpperCamelCase ): v for k, v in idalabel.items()} SCREAMING_SNAKE_CASE_ = idalabel SCREAMING_SNAKE_CASE_ = {v: k for k, v in idalabel.items()} SCREAMING_SNAKE_CASE_ = img_size SCREAMING_SNAKE_CASE_ = num_classes SCREAMING_SNAKE_CASE_ = embed_dim SCREAMING_SNAKE_CASE_ = depths SCREAMING_SNAKE_CASE_ = num_heads SCREAMING_SNAKE_CASE_ = window_size return config def a__ ( __UpperCamelCase ): if "patch_embed.proj" in name: SCREAMING_SNAKE_CASE_ = name.replace("patch_embed.proj" , "embeddings.patch_embeddings.projection" ) if "patch_embed.norm" in name: SCREAMING_SNAKE_CASE_ = name.replace("patch_embed.norm" , "embeddings.norm" ) if "layers" in name: SCREAMING_SNAKE_CASE_ = "encoder." + name if "attn.proj" in name: SCREAMING_SNAKE_CASE_ = name.replace("attn.proj" , "attention.output.dense" ) if "attn" in name: SCREAMING_SNAKE_CASE_ = name.replace("attn" , "attention.self" ) if "norm1" in name: SCREAMING_SNAKE_CASE_ = name.replace("norm1" , "layernorm_before" ) if "norm2" in name: SCREAMING_SNAKE_CASE_ = name.replace("norm2" , "layernorm_after" ) if "mlp.fc1" in name: SCREAMING_SNAKE_CASE_ = name.replace("mlp.fc1" , "intermediate.dense" ) if "mlp.fc2" in name: SCREAMING_SNAKE_CASE_ = name.replace("mlp.fc2" , "output.dense" ) if "q_bias" in name: SCREAMING_SNAKE_CASE_ = name.replace("q_bias" , "query.bias" ) if "k_bias" in name: SCREAMING_SNAKE_CASE_ = name.replace("k_bias" , "key.bias" ) if "v_bias" in name: SCREAMING_SNAKE_CASE_ = name.replace("v_bias" , "value.bias" ) if "cpb_mlp" in name: SCREAMING_SNAKE_CASE_ = name.replace("cpb_mlp" , "continuous_position_bias_mlp" ) if name == "norm.weight": SCREAMING_SNAKE_CASE_ = "layernorm.weight" if name == "norm.bias": SCREAMING_SNAKE_CASE_ = "layernorm.bias" if "head" in name: SCREAMING_SNAKE_CASE_ = name.replace("head" , "classifier" ) else: SCREAMING_SNAKE_CASE_ = "swinv2." + name return name def a__ ( __UpperCamelCase , __UpperCamelCase ): for key in orig_state_dict.copy().keys(): SCREAMING_SNAKE_CASE_ = orig_state_dict.pop(__UpperCamelCase ) if "mask" in key: continue elif "qkv" in key: SCREAMING_SNAKE_CASE_ = key.split("." ) SCREAMING_SNAKE_CASE_ = int(key_split[1] ) SCREAMING_SNAKE_CASE_ = int(key_split[3] ) SCREAMING_SNAKE_CASE_ = model.swinva.encoder.layers[layer_num].blocks[block_num].attention.self.all_head_size if "weight" in key: SCREAMING_SNAKE_CASE_ = val[:dim, :] SCREAMING_SNAKE_CASE_ = val[dim : dim * 2, :] SCREAMING_SNAKE_CASE_ = val[-dim:, :] else: SCREAMING_SNAKE_CASE_ = val[:dim] SCREAMING_SNAKE_CASE_ = val[ dim : dim * 2 ] SCREAMING_SNAKE_CASE_ = val[-dim:] else: SCREAMING_SNAKE_CASE_ = val return orig_state_dict def a__ ( __UpperCamelCase , __UpperCamelCase ): SCREAMING_SNAKE_CASE_ = timm.create_model(__UpperCamelCase , pretrained=__UpperCamelCase ) timm_model.eval() SCREAMING_SNAKE_CASE_ = get_swinva_config(__UpperCamelCase ) SCREAMING_SNAKE_CASE_ = SwinvaForImageClassification(__UpperCamelCase ) model.eval() SCREAMING_SNAKE_CASE_ = convert_state_dict(timm_model.state_dict() , __UpperCamelCase ) model.load_state_dict(__UpperCamelCase ) SCREAMING_SNAKE_CASE_ = "http://images.cocodataset.org/val2017/000000039769.jpg" SCREAMING_SNAKE_CASE_ = AutoImageProcessor.from_pretrained("microsoft/{}".format(swinva_name.replace("_" , "-" ) ) ) SCREAMING_SNAKE_CASE_ = Image.open(requests.get(__UpperCamelCase , stream=__UpperCamelCase ).raw ) SCREAMING_SNAKE_CASE_ = image_processor(images=__UpperCamelCase , return_tensors="pt" ) SCREAMING_SNAKE_CASE_ = timm_model(inputs["pixel_values"] ) SCREAMING_SNAKE_CASE_ = model(**__UpperCamelCase ).logits assert torch.allclose(__UpperCamelCase , __UpperCamelCase , atol=1E-3 ) print(F'''Saving model {swinva_name} to {pytorch_dump_folder_path}''' ) model.save_pretrained(__UpperCamelCase ) print(F'''Saving image processor to {pytorch_dump_folder_path}''' ) image_processor.save_pretrained(__UpperCamelCase ) model.push_to_hub( repo_path_or_name=Path(__UpperCamelCase , __UpperCamelCase ) , organization="nandwalritik" , commit_message="Add model" , ) if __name__ == "__main__": A : int = argparse.ArgumentParser() # Required parameters parser.add_argument( "--swinv2_name", default="swinv2_tiny_patch4_window8_256", type=str, help="Name of the Swinv2 timm model you'd like to convert.", ) parser.add_argument( "--pytorch_dump_folder_path", default=None, type=str, help="Path to the output PyTorch model directory." ) A : Any = parser.parse_args() convert_swinva_checkpoint(args.swinva_name, args.pytorch_dump_folder_path)
305
import warnings from diffusers import StableDiffusionImgaImgPipeline # noqa F401 warnings.warn( "The `image_to_image.py` script is outdated. Please use directly `from diffusers import" " StableDiffusionImg2ImgPipeline` instead." )
305
1
"""simple docstring""" from ...configuration_utils import PretrainedConfig from ...utils import logging A : Union[str, Any] = logging.get_logger(__name__) class lowerCamelCase (SCREAMING_SNAKE_CASE_ ): """simple docstring""" lowerCamelCase__ = '''timm_backbone''' def __init__( self : Optional[Any] , __magic_name__ : List[str]=None , __magic_name__ : int=3 , __magic_name__ : Tuple=True , __magic_name__ : Tuple=True , __magic_name__ : Tuple=None , **__magic_name__ : Tuple , ) -> Optional[Any]: super().__init__(**__a ) SCREAMING_SNAKE_CASE_ = backbone SCREAMING_SNAKE_CASE_ = num_channels SCREAMING_SNAKE_CASE_ = features_only SCREAMING_SNAKE_CASE_ = use_pretrained_backbone SCREAMING_SNAKE_CASE_ = True SCREAMING_SNAKE_CASE_ = out_indices if out_indices is not None else (-1,)
350
from __future__ import annotations import numpy as np def a__ ( __UpperCamelCase ): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = np.shape(__UpperCamelCase ) if rows != columns: SCREAMING_SNAKE_CASE_ = ( "'table' has to be of square shaped array but got a " F'''{rows}x{columns} array:\n{table}''' ) raise ValueError(__UpperCamelCase ) SCREAMING_SNAKE_CASE_ = np.zeros((rows, columns) ) SCREAMING_SNAKE_CASE_ = np.zeros((rows, columns) ) for i in range(__UpperCamelCase ): for j in range(__UpperCamelCase ): SCREAMING_SNAKE_CASE_ = sum(lower[i][k] * upper[k][j] for k in range(__UpperCamelCase ) ) if upper[j][j] == 0: raise ArithmeticError("No LU decomposition exists" ) SCREAMING_SNAKE_CASE_ = (table[i][j] - total) / upper[j][j] SCREAMING_SNAKE_CASE_ = 1 for j in range(__UpperCamelCase , __UpperCamelCase ): SCREAMING_SNAKE_CASE_ = sum(lower[i][k] * upper[k][j] for k in range(__UpperCamelCase ) ) SCREAMING_SNAKE_CASE_ = table[i][j] - total return lower, upper if __name__ == "__main__": import doctest doctest.testmod()
305
0
"""simple docstring""" from __future__ import annotations import string from itertools import cycle, product from pathlib import Path A : Optional[Any] = ( string.ascii_letters + string.digits + string.punctuation + string.whitespace ) A : Tuple = [ord(letter) for letter in string.ascii_lowercase] A : Dict = {ord(char) for char in VALID_CHARS} A : List[str] = ["the", "be", "to", "of", "and", "in", "that", "have"] def a__ ( __UpperCamelCase , __UpperCamelCase ): SCREAMING_SNAKE_CASE_ = "" SCREAMING_SNAKE_CASE_ = 42 SCREAMING_SNAKE_CASE_ = 42 SCREAMING_SNAKE_CASE_ = 42 for keychar, cipherchar in zip(cycle(lowercase__ ) , lowercase__ ): SCREAMING_SNAKE_CASE_ = cipherchar ^ keychar if decodedchar not in VALID_INTS: return None decoded += chr(lowercase__ ) return decoded def a__ ( __UpperCamelCase ): SCREAMING_SNAKE_CASE_ = [] for key in product(lowercase__ , repeat=3 ): SCREAMING_SNAKE_CASE_ = try_key(lowercase__ , lowercase__ ) if encoded is not None: possibles.append(lowercase__ ) return possibles def a__ ( __UpperCamelCase , __UpperCamelCase ): return [possible for possible in possibles if common_word in possible.lower()] def a__ ( __UpperCamelCase = "p059_cipher.txt" ): SCREAMING_SNAKE_CASE_ = 42 SCREAMING_SNAKE_CASE_ = 42 SCREAMING_SNAKE_CASE_ = 42 SCREAMING_SNAKE_CASE_ = 42 SCREAMING_SNAKE_CASE_ = Path(lowercase__ ).parent.joinpath(lowercase__ ).read_text(encoding="utf-8" ) SCREAMING_SNAKE_CASE_ = [int(lowercase__ ) for number in data.strip().split("," )] SCREAMING_SNAKE_CASE_ = filter_valid_chars(lowercase__ ) for common_word in COMMON_WORDS: SCREAMING_SNAKE_CASE_ = filter_common_word(lowercase__ , lowercase__ ) if len(lowercase__ ) == 1: break SCREAMING_SNAKE_CASE_ = possibles[0] return sum(ord(lowercase__ ) for char in decoded_text ) if __name__ == "__main__": print(f"{solution() = }")
351
from math import pi, sqrt, tan def a__ ( __UpperCamelCase ): if side_length < 0: raise ValueError("surface_area_cube() only accepts non-negative values" ) return 6 * side_length**2 def a__ ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase ): if length < 0 or breadth < 0 or height < 0: raise ValueError("surface_area_cuboid() only accepts non-negative values" ) return 2 * ((length * breadth) + (breadth * height) + (length * height)) def a__ ( __UpperCamelCase ): if radius < 0: raise ValueError("surface_area_sphere() only accepts non-negative values" ) return 4 * pi * radius**2 def a__ ( __UpperCamelCase ): if radius < 0: raise ValueError("surface_area_hemisphere() only accepts non-negative values" ) return 3 * pi * radius**2 def a__ ( __UpperCamelCase , __UpperCamelCase ): if radius < 0 or height < 0: raise ValueError("surface_area_cone() only accepts non-negative values" ) return pi * radius * (radius + (height**2 + radius**2) ** 0.5) def a__ ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase ): if radius_a < 0 or radius_a < 0 or height < 0: raise ValueError( "surface_area_conical_frustum() only accepts non-negative values" ) SCREAMING_SNAKE_CASE_ = (height**2 + (radius_a - radius_a) ** 2) ** 0.5 return pi * ((slant_height * (radius_a + radius_a)) + radius_a**2 + radius_a**2) def a__ ( __UpperCamelCase , __UpperCamelCase ): if radius < 0 or height < 0: raise ValueError("surface_area_cylinder() only accepts non-negative values" ) return 2 * pi * radius * (height + radius) def a__ ( __UpperCamelCase , __UpperCamelCase ): if torus_radius < 0 or tube_radius < 0: raise ValueError("surface_area_torus() only accepts non-negative values" ) if torus_radius < tube_radius: raise ValueError( "surface_area_torus() does not support spindle or self intersecting tori" ) return 4 * pow(__UpperCamelCase , 2 ) * torus_radius * tube_radius def a__ ( __UpperCamelCase , __UpperCamelCase ): if length < 0 or width < 0: raise ValueError("area_rectangle() only accepts non-negative values" ) return length * width def a__ ( __UpperCamelCase ): if side_length < 0: raise ValueError("area_square() only accepts non-negative values" ) return side_length**2 def a__ ( __UpperCamelCase , __UpperCamelCase ): if base < 0 or height < 0: raise ValueError("area_triangle() only accepts non-negative values" ) return (base * height) / 2 def a__ ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase ): if sidea < 0 or sidea < 0 or sidea < 0: raise ValueError("area_triangle_three_sides() only accepts non-negative values" ) elif sidea + sidea < sidea or sidea + sidea < sidea or sidea + sidea < sidea: raise ValueError("Given three sides do not form a triangle" ) SCREAMING_SNAKE_CASE_ = (sidea + sidea + sidea) / 2 SCREAMING_SNAKE_CASE_ = sqrt( semi_perimeter * (semi_perimeter - sidea) * (semi_perimeter - sidea) * (semi_perimeter - sidea) ) return area def a__ ( __UpperCamelCase , __UpperCamelCase ): if base < 0 or height < 0: raise ValueError("area_parallelogram() only accepts non-negative values" ) return base * height def a__ ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase ): if basea < 0 or basea < 0 or height < 0: raise ValueError("area_trapezium() only accepts non-negative values" ) return 1 / 2 * (basea + basea) * height def a__ ( __UpperCamelCase ): if radius < 0: raise ValueError("area_circle() only accepts non-negative values" ) return pi * radius**2 def a__ ( __UpperCamelCase , __UpperCamelCase ): if radius_x < 0 or radius_y < 0: raise ValueError("area_ellipse() only accepts non-negative values" ) return pi * radius_x * radius_y def a__ ( __UpperCamelCase , __UpperCamelCase ): if diagonal_a < 0 or diagonal_a < 0: raise ValueError("area_rhombus() only accepts non-negative values" ) return 1 / 2 * diagonal_a * diagonal_a def a__ ( __UpperCamelCase , __UpperCamelCase ): if not isinstance(__UpperCamelCase , __UpperCamelCase ) or sides < 3: raise ValueError( "area_reg_polygon() only accepts integers greater than or \ equal to three as number of sides" ) elif length < 0: raise ValueError( "area_reg_polygon() only accepts non-negative values as \ length of a side" ) return (sides * length**2) / (4 * tan(pi / sides )) return (sides * length**2) / (4 * tan(pi / sides )) if __name__ == "__main__": import doctest doctest.testmod(verbose=True) # verbose so we can see methods missing tests print("[DEMO] Areas of various geometric shapes: \n") print(f"Rectangle: {area_rectangle(10, 20) = }") print(f"Square: {area_square(10) = }") print(f"Triangle: {area_triangle(10, 10) = }") print(f"Triangle: {area_triangle_three_sides(5, 12, 13) = }") print(f"Parallelogram: {area_parallelogram(10, 20) = }") print(f"Rhombus: {area_rhombus(10, 20) = }") print(f"Trapezium: {area_trapezium(10, 20, 30) = }") print(f"Circle: {area_circle(20) = }") print(f"Ellipse: {area_ellipse(10, 20) = }") print("\nSurface Areas of various geometric shapes: \n") print(f"Cube: {surface_area_cube(20) = }") print(f"Cuboid: {surface_area_cuboid(10, 20, 30) = }") print(f"Sphere: {surface_area_sphere(20) = }") print(f"Hemisphere: {surface_area_hemisphere(20) = }") print(f"Cone: {surface_area_cone(10, 20) = }") print(f"Conical Frustum: {surface_area_conical_frustum(10, 20, 30) = }") print(f"Cylinder: {surface_area_cylinder(10, 20) = }") print(f"Torus: {surface_area_torus(20, 10) = }") print(f"Equilateral Triangle: {area_reg_polygon(3, 10) = }") print(f"Square: {area_reg_polygon(4, 10) = }") print(f"Reqular Pentagon: {area_reg_polygon(5, 10) = }")
305
0
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available A : int = { "configuration_pegasus_x": ["PEGASUS_X_PRETRAINED_CONFIG_ARCHIVE_MAP", "PegasusXConfig"], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: A : List[str] = [ "PEGASUS_X_PRETRAINED_MODEL_ARCHIVE_LIST", "PegasusXForConditionalGeneration", "PegasusXModel", "PegasusXPreTrainedModel", ] if TYPE_CHECKING: from .configuration_pegasus_x import PEGASUS_X_PRETRAINED_CONFIG_ARCHIVE_MAP, PegasusXConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_pegasus_x import ( PEGASUS_X_PRETRAINED_MODEL_ARCHIVE_LIST, PegasusXForConditionalGeneration, PegasusXModel, PegasusXPreTrainedModel, ) else: import sys A : List[Any] = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
352
from collections import OrderedDict from typing import Any, Mapping, Optional from ... import PreTrainedTokenizer from ...configuration_utils import PretrainedConfig from ...file_utils import TensorType, is_torch_available from ...onnx import OnnxConfig, OnnxConfigWithPast, OnnxSeqaSeqConfigWithPast from ...onnx.utils import compute_effective_axis_dimension from ...utils import logging A : List[str] = logging.get_logger(__name__) A : int = { "facebook/blenderbot_small-90M": "https://huggingface.co/facebook/blenderbot_small-90M/resolve/main/config.json", # See all BlenderbotSmall models at https://huggingface.co/models?filter=blenderbot_small } class lowerCamelCase (SCREAMING_SNAKE_CASE__ ): """simple docstring""" lowerCamelCase__ = '''blenderbot-small''' lowerCamelCase__ = ['''past_key_values'''] lowerCamelCase__ = {'''num_attention_heads''': '''encoder_attention_heads''', '''hidden_size''': '''d_model'''} def __init__( self : Dict , __magic_name__ : Dict=50_265 , __magic_name__ : str=512 , __magic_name__ : List[Any]=8 , __magic_name__ : Any=2_048 , __magic_name__ : Dict=16 , __magic_name__ : Any=8 , __magic_name__ : Optional[int]=2_048 , __magic_name__ : Dict=16 , __magic_name__ : Tuple=0.0 , __magic_name__ : Dict=0.0 , __magic_name__ : Optional[int]=True , __magic_name__ : Any=True , __magic_name__ : Dict="gelu" , __magic_name__ : Tuple=512 , __magic_name__ : List[str]=0.1 , __magic_name__ : List[Any]=0.0 , __magic_name__ : List[Any]=0.0 , __magic_name__ : Tuple=0.02 , __magic_name__ : Union[str, Any]=1 , __magic_name__ : List[Any]=False , __magic_name__ : str=0 , __magic_name__ : Dict=1 , __magic_name__ : str=2 , __magic_name__ : Union[str, Any]=2 , **__magic_name__ : Optional[Any] , ) -> Optional[Any]: SCREAMING_SNAKE_CASE_ = vocab_size SCREAMING_SNAKE_CASE_ = max_position_embeddings SCREAMING_SNAKE_CASE_ = d_model SCREAMING_SNAKE_CASE_ = encoder_ffn_dim SCREAMING_SNAKE_CASE_ = encoder_layers SCREAMING_SNAKE_CASE_ = encoder_attention_heads SCREAMING_SNAKE_CASE_ = decoder_ffn_dim SCREAMING_SNAKE_CASE_ = decoder_layers SCREAMING_SNAKE_CASE_ = decoder_attention_heads SCREAMING_SNAKE_CASE_ = dropout SCREAMING_SNAKE_CASE_ = attention_dropout SCREAMING_SNAKE_CASE_ = activation_dropout SCREAMING_SNAKE_CASE_ = activation_function SCREAMING_SNAKE_CASE_ = init_std SCREAMING_SNAKE_CASE_ = encoder_layerdrop SCREAMING_SNAKE_CASE_ = decoder_layerdrop SCREAMING_SNAKE_CASE_ = use_cache SCREAMING_SNAKE_CASE_ = encoder_layers SCREAMING_SNAKE_CASE_ = scale_embedding # scale factor will be sqrt(d_model) if True super().__init__( pad_token_id=__magic_name__ , bos_token_id=__magic_name__ , eos_token_id=__magic_name__ , is_encoder_decoder=__magic_name__ , decoder_start_token_id=__magic_name__ , forced_eos_token_id=__magic_name__ , **__magic_name__ , ) class lowerCamelCase (SCREAMING_SNAKE_CASE__ ): """simple docstring""" @property def __A ( self : str ) -> Mapping[str, Mapping[int, str]]: if self.task in ["default", "seq2seq-lm"]: SCREAMING_SNAKE_CASE_ = OrderedDict( [ ("input_ids", {0: "batch", 1: "encoder_sequence"}), ("attention_mask", {0: "batch", 1: "encoder_sequence"}), ] ) if self.use_past: SCREAMING_SNAKE_CASE_ = {0: "batch"} SCREAMING_SNAKE_CASE_ = {0: "batch", 1: "past_decoder_sequence + sequence"} else: SCREAMING_SNAKE_CASE_ = {0: "batch", 1: "decoder_sequence"} SCREAMING_SNAKE_CASE_ = {0: "batch", 1: "decoder_sequence"} if self.use_past: self.fill_with_past_key_values_(__magic_name__ , direction="inputs" ) elif self.task == "causal-lm": # TODO: figure this case out. SCREAMING_SNAKE_CASE_ = OrderedDict( [ ("input_ids", {0: "batch", 1: "encoder_sequence"}), ("attention_mask", {0: "batch", 1: "encoder_sequence"}), ] ) if self.use_past: SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = self.num_layers for i in range(__magic_name__ ): SCREAMING_SNAKE_CASE_ = {0: "batch", 2: "past_sequence + sequence"} SCREAMING_SNAKE_CASE_ = {0: "batch", 2: "past_sequence + sequence"} else: SCREAMING_SNAKE_CASE_ = OrderedDict( [ ("input_ids", {0: "batch", 1: "encoder_sequence"}), ("attention_mask", {0: "batch", 1: "encoder_sequence"}), ("decoder_input_ids", {0: "batch", 1: "decoder_sequence"}), ("decoder_attention_mask", {0: "batch", 1: "decoder_sequence"}), ] ) return common_inputs @property def __A ( self : Tuple ) -> Mapping[str, Mapping[int, str]]: if self.task in ["default", "seq2seq-lm"]: SCREAMING_SNAKE_CASE_ = super().outputs else: SCREAMING_SNAKE_CASE_ = super(__magic_name__ , self ).outputs if self.use_past: SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = self.num_layers for i in range(__magic_name__ ): SCREAMING_SNAKE_CASE_ = {0: "batch", 2: "past_sequence + sequence"} SCREAMING_SNAKE_CASE_ = {0: "batch", 2: "past_sequence + sequence"} return common_outputs def __A ( self : int , __magic_name__ : PreTrainedTokenizer , __magic_name__ : int = -1 , __magic_name__ : int = -1 , __magic_name__ : bool = False , __magic_name__ : Optional[TensorType] = None , ) -> Mapping[str, Any]: SCREAMING_SNAKE_CASE_ = self._generate_dummy_inputs_for_sequence_classification_and_question_answering( __magic_name__ , __magic_name__ , __magic_name__ , __magic_name__ , __magic_name__ ) # Generate decoder inputs SCREAMING_SNAKE_CASE_ = seq_length if not self.use_past else 1 SCREAMING_SNAKE_CASE_ = self._generate_dummy_inputs_for_sequence_classification_and_question_answering( __magic_name__ , __magic_name__ , __magic_name__ , __magic_name__ , __magic_name__ ) SCREAMING_SNAKE_CASE_ = {F'''decoder_{name}''': tensor for name, tensor in decoder_inputs.items()} SCREAMING_SNAKE_CASE_ = dict(**__magic_name__ , **__magic_name__ ) if self.use_past: if not is_torch_available(): raise ValueError("Cannot generate dummy past_keys inputs without PyTorch installed." ) else: import torch SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = common_inputs["input_ids"].shape SCREAMING_SNAKE_CASE_ = common_inputs["decoder_input_ids"].shape[1] SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = self.num_attention_heads SCREAMING_SNAKE_CASE_ = ( batch, num_encoder_attention_heads, encoder_seq_length, self._config.hidden_size // num_encoder_attention_heads, ) SCREAMING_SNAKE_CASE_ = decoder_seq_length + 3 SCREAMING_SNAKE_CASE_ = ( batch, num_decoder_attention_heads, decoder_past_length, self._config.hidden_size // num_decoder_attention_heads, ) SCREAMING_SNAKE_CASE_ = torch.cat( [common_inputs["decoder_attention_mask"], torch.ones(__magic_name__ , __magic_name__ )] , dim=1 ) SCREAMING_SNAKE_CASE_ = [] # If the number of encoder and decoder layers are present in the model configuration, both are considered SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = self.num_layers SCREAMING_SNAKE_CASE_ = min(__magic_name__ , __magic_name__ ) SCREAMING_SNAKE_CASE_ = max(__magic_name__ , __magic_name__ ) - min_num_layers SCREAMING_SNAKE_CASE_ = "encoder" if num_encoder_layers > num_decoder_layers else "decoder" for _ in range(__magic_name__ ): common_inputs["past_key_values"].append( ( torch.zeros(__magic_name__ ), torch.zeros(__magic_name__ ), torch.zeros(__magic_name__ ), torch.zeros(__magic_name__ ), ) ) # TODO: test this. SCREAMING_SNAKE_CASE_ = encoder_shape if remaining_side_name == "encoder" else decoder_shape for _ in range(__magic_name__ , __magic_name__ ): common_inputs["past_key_values"].append((torch.zeros(__magic_name__ ), torch.zeros(__magic_name__ )) ) return common_inputs def __A ( self : Union[str, Any] , __magic_name__ : PreTrainedTokenizer , __magic_name__ : int = -1 , __magic_name__ : int = -1 , __magic_name__ : bool = False , __magic_name__ : Optional[TensorType] = None , ) -> Mapping[str, Any]: SCREAMING_SNAKE_CASE_ = self._generate_dummy_inputs_for_sequence_classification_and_question_answering( __magic_name__ , __magic_name__ , __magic_name__ , __magic_name__ , __magic_name__ ) if self.use_past: if not is_torch_available(): raise ValueError("Cannot generate dummy past_keys inputs without PyTorch installed." ) else: import torch SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = common_inputs["input_ids"].shape # Not using the same length for past_key_values SCREAMING_SNAKE_CASE_ = seqlen + 2 SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = self.num_layers SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = self.num_attention_heads SCREAMING_SNAKE_CASE_ = ( batch, num_encoder_attention_heads, past_key_values_length, self._config.hidden_size // num_encoder_attention_heads, ) SCREAMING_SNAKE_CASE_ = common_inputs["attention_mask"].dtype SCREAMING_SNAKE_CASE_ = torch.cat( [common_inputs["attention_mask"], torch.ones(__magic_name__ , __magic_name__ , dtype=__magic_name__ )] , dim=1 ) SCREAMING_SNAKE_CASE_ = [ (torch.zeros(__magic_name__ ), torch.zeros(__magic_name__ )) for _ in range(__magic_name__ ) ] return common_inputs def __A ( self : Dict , __magic_name__ : PreTrainedTokenizer , __magic_name__ : int = -1 , __magic_name__ : int = -1 , __magic_name__ : bool = False , __magic_name__ : Optional[TensorType] = None , ) -> Mapping[str, Any]: # Copied from OnnxConfig.generate_dummy_inputs # Did not use super(OnnxConfigWithPast, self).generate_dummy_inputs for code clarity. # If dynamic axis (-1) we forward with a fixed dimension of 2 samples to avoid optimizations made by ONNX SCREAMING_SNAKE_CASE_ = compute_effective_axis_dimension( __magic_name__ , fixed_dimension=OnnxConfig.default_fixed_batch , num_token_to_add=0 ) # If dynamic axis (-1) we forward with a fixed dimension of 8 tokens to avoid optimizations made by ONNX SCREAMING_SNAKE_CASE_ = tokenizer.num_special_tokens_to_add(__magic_name__ ) SCREAMING_SNAKE_CASE_ = compute_effective_axis_dimension( __magic_name__ , fixed_dimension=OnnxConfig.default_fixed_sequence , num_token_to_add=__magic_name__ ) # Generate dummy inputs according to compute batch and sequence SCREAMING_SNAKE_CASE_ = [" ".join([tokenizer.unk_token] ) * seq_length] * batch_size SCREAMING_SNAKE_CASE_ = dict(tokenizer(__magic_name__ , return_tensors=__magic_name__ ) ) return common_inputs def __A ( self : Optional[Any] , __magic_name__ : PreTrainedTokenizer , __magic_name__ : int = -1 , __magic_name__ : int = -1 , __magic_name__ : bool = False , __magic_name__ : Optional[TensorType] = None , ) -> Mapping[str, Any]: if self.task in ["default", "seq2seq-lm"]: SCREAMING_SNAKE_CASE_ = self._generate_dummy_inputs_for_default_and_seqaseq_lm( __magic_name__ , batch_size=__magic_name__ , seq_length=__magic_name__ , is_pair=__magic_name__ , framework=__magic_name__ ) elif self.task == "causal-lm": SCREAMING_SNAKE_CASE_ = self._generate_dummy_inputs_for_causal_lm( __magic_name__ , batch_size=__magic_name__ , seq_length=__magic_name__ , is_pair=__magic_name__ , framework=__magic_name__ ) else: SCREAMING_SNAKE_CASE_ = self._generate_dummy_inputs_for_sequence_classification_and_question_answering( __magic_name__ , batch_size=__magic_name__ , seq_length=__magic_name__ , is_pair=__magic_name__ , framework=__magic_name__ ) return common_inputs def __A ( self : Optional[Any] , __magic_name__ : str , __magic_name__ : List[Any] , __magic_name__ : str , __magic_name__ : List[str] ) -> List[str]: if self.task in ["default", "seq2seq-lm"]: SCREAMING_SNAKE_CASE_ = super()._flatten_past_key_values_(__magic_name__ , __magic_name__ , __magic_name__ , __magic_name__ ) else: SCREAMING_SNAKE_CASE_ = super(__magic_name__ , self )._flatten_past_key_values_( __magic_name__ , __magic_name__ , __magic_name__ , __magic_name__ )
305
0
A : Optional[int] = "0.18.2" from .configuration_utils import ConfigMixin from .utils import ( OptionalDependencyNotAvailable, is_flax_available, is_inflect_available, is_invisible_watermark_available, is_k_diffusion_available, is_k_diffusion_version, is_librosa_available, is_note_seq_available, is_onnx_available, is_scipy_available, is_torch_available, is_torchsde_available, is_transformers_available, is_transformers_version, is_unidecode_available, logging, ) try: if not is_onnx_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from .utils.dummy_onnx_objects import * # noqa F403 else: from .pipelines import OnnxRuntimeModel try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from .utils.dummy_pt_objects import * # noqa F403 else: from .models import ( AutoencoderKL, ControlNetModel, ModelMixin, PriorTransformer, TaFilmDecoder, TransformeraDModel, UNetaDModel, UNetaDConditionModel, UNetaDModel, UNetaDConditionModel, VQModel, ) from .optimization import ( get_constant_schedule, get_constant_schedule_with_warmup, get_cosine_schedule_with_warmup, get_cosine_with_hard_restarts_schedule_with_warmup, get_linear_schedule_with_warmup, get_polynomial_decay_schedule_with_warmup, get_scheduler, ) from .pipelines import ( AudioPipelineOutput, ConsistencyModelPipeline, DanceDiffusionPipeline, DDIMPipeline, DDPMPipeline, DiffusionPipeline, DiTPipeline, ImagePipelineOutput, KarrasVePipeline, LDMPipeline, LDMSuperResolutionPipeline, PNDMPipeline, RePaintPipeline, ScoreSdeVePipeline, ) from .schedulers import ( CMStochasticIterativeScheduler, DDIMInverseScheduler, DDIMParallelScheduler, DDIMScheduler, DDPMParallelScheduler, DDPMScheduler, DEISMultistepScheduler, DPMSolverMultistepInverseScheduler, DPMSolverMultistepScheduler, DPMSolverSinglestepScheduler, EulerAncestralDiscreteScheduler, EulerDiscreteScheduler, HeunDiscreteScheduler, IPNDMScheduler, KarrasVeScheduler, KDPMaAncestralDiscreteScheduler, KDPMaDiscreteScheduler, PNDMScheduler, RePaintScheduler, SchedulerMixin, ScoreSdeVeScheduler, UnCLIPScheduler, UniPCMultistepScheduler, VQDiffusionScheduler, ) from .training_utils import EMAModel try: if not (is_torch_available() and is_scipy_available()): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from .utils.dummy_torch_and_scipy_objects import * # noqa F403 else: from .schedulers import LMSDiscreteScheduler try: if not (is_torch_available() and is_torchsde_available()): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from .utils.dummy_torch_and_torchsde_objects import * # noqa F403 else: from .schedulers import DPMSolverSDEScheduler try: if not (is_torch_available() and is_transformers_available()): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from .utils.dummy_torch_and_transformers_objects import * # noqa F403 else: from .pipelines import ( AltDiffusionImgaImgPipeline, AltDiffusionPipeline, AudioLDMPipeline, CycleDiffusionPipeline, IFImgaImgPipeline, IFImgaImgSuperResolutionPipeline, IFInpaintingPipeline, IFInpaintingSuperResolutionPipeline, IFPipeline, IFSuperResolutionPipeline, ImageTextPipelineOutput, KandinskyImgaImgPipeline, KandinskyInpaintPipeline, KandinskyPipeline, KandinskyPriorPipeline, KandinskyVaaControlnetImgaImgPipeline, KandinskyVaaControlnetPipeline, KandinskyVaaImgaImgPipeline, KandinskyVaaInpaintPipeline, KandinskyVaaPipeline, KandinskyVaaPriorEmbaEmbPipeline, KandinskyVaaPriorPipeline, LDMTextToImagePipeline, PaintByExamplePipeline, SemanticStableDiffusionPipeline, ShapEImgaImgPipeline, ShapEPipeline, StableDiffusionAttendAndExcitePipeline, StableDiffusionControlNetImgaImgPipeline, StableDiffusionControlNetInpaintPipeline, StableDiffusionControlNetPipeline, StableDiffusionDepthaImgPipeline, StableDiffusionDiffEditPipeline, StableDiffusionImageVariationPipeline, StableDiffusionImgaImgPipeline, StableDiffusionInpaintPipeline, StableDiffusionInpaintPipelineLegacy, StableDiffusionInstructPixaPixPipeline, StableDiffusionLatentUpscalePipeline, StableDiffusionLDMaDPipeline, StableDiffusionModelEditingPipeline, StableDiffusionPanoramaPipeline, StableDiffusionParadigmsPipeline, StableDiffusionPipeline, StableDiffusionPipelineSafe, StableDiffusionPixaPixZeroPipeline, StableDiffusionSAGPipeline, StableDiffusionUpscalePipeline, StableUnCLIPImgaImgPipeline, StableUnCLIPPipeline, TextToVideoSDPipeline, TextToVideoZeroPipeline, UnCLIPImageVariationPipeline, UnCLIPPipeline, UniDiffuserModel, UniDiffuserPipeline, UniDiffuserTextDecoder, VersatileDiffusionDualGuidedPipeline, VersatileDiffusionImageVariationPipeline, VersatileDiffusionPipeline, VersatileDiffusionTextToImagePipeline, VideoToVideoSDPipeline, VQDiffusionPipeline, ) try: if not (is_torch_available() and is_transformers_available() and is_invisible_watermark_available()): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from .utils.dummy_torch_and_transformers_and_invisible_watermark_objects import * # noqa F403 else: from .pipelines import StableDiffusionXLImgaImgPipeline, StableDiffusionXLPipeline try: if not (is_torch_available() and is_transformers_available() and is_k_diffusion_available()): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from .utils.dummy_torch_and_transformers_and_k_diffusion_objects import * # noqa F403 else: from .pipelines import StableDiffusionKDiffusionPipeline try: if not (is_torch_available() and is_transformers_available() and is_onnx_available()): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from .utils.dummy_torch_and_transformers_and_onnx_objects import * # noqa F403 else: from .pipelines import ( OnnxStableDiffusionImgaImgPipeline, OnnxStableDiffusionInpaintPipeline, OnnxStableDiffusionInpaintPipelineLegacy, OnnxStableDiffusionPipeline, OnnxStableDiffusionUpscalePipeline, StableDiffusionOnnxPipeline, ) try: if not (is_torch_available() and is_librosa_available()): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from .utils.dummy_torch_and_librosa_objects import * # noqa F403 else: from .pipelines import AudioDiffusionPipeline, Mel try: if not (is_transformers_available() and is_torch_available() and is_note_seq_available()): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from .utils.dummy_transformers_and_torch_and_note_seq_objects import * # noqa F403 else: from .pipelines import SpectrogramDiffusionPipeline try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from .utils.dummy_flax_objects import * # noqa F403 else: from .models.controlnet_flax import FlaxControlNetModel from .models.modeling_flax_utils import FlaxModelMixin from .models.unet_ad_condition_flax import FlaxUNetaDConditionModel from .models.vae_flax import FlaxAutoencoderKL from .pipelines import FlaxDiffusionPipeline from .schedulers import ( FlaxDDIMScheduler, FlaxDDPMScheduler, FlaxDPMSolverMultistepScheduler, FlaxKarrasVeScheduler, FlaxLMSDiscreteScheduler, FlaxPNDMScheduler, FlaxSchedulerMixin, FlaxScoreSdeVeScheduler, ) try: if not (is_flax_available() and is_transformers_available()): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from .utils.dummy_flax_and_transformers_objects import * # noqa F403 else: from .pipelines import ( FlaxStableDiffusionControlNetPipeline, FlaxStableDiffusionImgaImgPipeline, FlaxStableDiffusionInpaintPipeline, FlaxStableDiffusionPipeline, ) try: if not (is_note_seq_available()): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from .utils.dummy_note_seq_objects import * # noqa F403 else: from .pipelines import MidiProcessor
353
import inspect import unittest from datasets import load_dataset from packaging import version from transformers import BeitConfig from transformers.models.auto import get_values from transformers.testing_utils import require_torch, require_torch_multi_gpu, require_vision, slow, torch_device from transformers.utils import cached_property, is_torch_available, is_vision_available from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, _config_zero_init, floats_tensor, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from torch import nn from transformers import ( MODEL_MAPPING, BeitForImageClassification, BeitForMaskedImageModeling, BeitForSemanticSegmentation, BeitModel, ) from transformers.models.beit.modeling_beit import BEIT_PRETRAINED_MODEL_ARCHIVE_LIST if is_vision_available(): import PIL from PIL import Image from transformers import BeitImageProcessor class lowerCamelCase : """simple docstring""" def __init__( self : List[Any] , __magic_name__ : List[str] , __magic_name__ : int=100 , __magic_name__ : Optional[Any]=13 , __magic_name__ : Dict=30 , __magic_name__ : Tuple=2 , __magic_name__ : str=3 , __magic_name__ : str=True , __magic_name__ : Optional[int]=True , __magic_name__ : Union[str, Any]=32 , __magic_name__ : Optional[int]=4 , __magic_name__ : Dict=4 , __magic_name__ : Tuple=37 , __magic_name__ : Any="gelu" , __magic_name__ : int=0.1 , __magic_name__ : List[str]=0.1 , __magic_name__ : Optional[int]=10 , __magic_name__ : Tuple=0.02 , __magic_name__ : Optional[int]=3 , __magic_name__ : List[str]=None , __magic_name__ : Tuple=[0, 1, 2, 3] , ) -> List[str]: SCREAMING_SNAKE_CASE_ = parent SCREAMING_SNAKE_CASE_ = 100 SCREAMING_SNAKE_CASE_ = batch_size SCREAMING_SNAKE_CASE_ = image_size SCREAMING_SNAKE_CASE_ = patch_size SCREAMING_SNAKE_CASE_ = num_channels SCREAMING_SNAKE_CASE_ = is_training SCREAMING_SNAKE_CASE_ = use_labels 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_ = type_sequence_label_size SCREAMING_SNAKE_CASE_ = initializer_range SCREAMING_SNAKE_CASE_ = scope SCREAMING_SNAKE_CASE_ = out_indices SCREAMING_SNAKE_CASE_ = num_labels # in BeiT, the seq length equals the number of patches + 1 (we add 1 for the [CLS] token) SCREAMING_SNAKE_CASE_ = (image_size // patch_size) ** 2 SCREAMING_SNAKE_CASE_ = num_patches + 1 def __A ( self : Any ) -> int: SCREAMING_SNAKE_CASE_ = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_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.image_size, self.image_size] , self.num_labels ) SCREAMING_SNAKE_CASE_ = self.get_config() return config, pixel_values, labels, pixel_labels def __A ( self : Dict ) -> Optional[int]: return BeitConfig( vocab_size=self.vocab_size , image_size=self.image_size , patch_size=self.patch_size , num_channels=self.num_channels , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , is_decoder=__magic_name__ , initializer_range=self.initializer_range , out_indices=self.out_indices , ) def __A ( self : Optional[int] , __magic_name__ : List[str] , __magic_name__ : Optional[int] , __magic_name__ : Tuple , __magic_name__ : Tuple ) -> Optional[int]: SCREAMING_SNAKE_CASE_ = BeitModel(config=__magic_name__ ) model.to(__magic_name__ ) model.eval() SCREAMING_SNAKE_CASE_ = model(__magic_name__ ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def __A ( self : Union[str, Any] , __magic_name__ : List[str] , __magic_name__ : int , __magic_name__ : int , __magic_name__ : str ) -> int: SCREAMING_SNAKE_CASE_ = BeitForMaskedImageModeling(config=__magic_name__ ) model.to(__magic_name__ ) model.eval() SCREAMING_SNAKE_CASE_ = model(__magic_name__ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length - 1, self.vocab_size) ) def __A ( self : Dict , __magic_name__ : List[Any] , __magic_name__ : int , __magic_name__ : Tuple , __magic_name__ : Optional[Any] ) -> int: SCREAMING_SNAKE_CASE_ = self.type_sequence_label_size SCREAMING_SNAKE_CASE_ = BeitForImageClassification(__magic_name__ ) model.to(__magic_name__ ) model.eval() SCREAMING_SNAKE_CASE_ = model(__magic_name__ , labels=__magic_name__ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.type_sequence_label_size) ) # test greyscale images SCREAMING_SNAKE_CASE_ = 1 SCREAMING_SNAKE_CASE_ = BeitForImageClassification(__magic_name__ ) model.to(__magic_name__ ) model.eval() SCREAMING_SNAKE_CASE_ = floats_tensor([self.batch_size, 1, self.image_size, self.image_size] ) SCREAMING_SNAKE_CASE_ = model(__magic_name__ , labels=__magic_name__ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.type_sequence_label_size) ) def __A ( self : Tuple , __magic_name__ : Any , __magic_name__ : Optional[Any] , __magic_name__ : Tuple , __magic_name__ : int ) -> int: SCREAMING_SNAKE_CASE_ = self.num_labels SCREAMING_SNAKE_CASE_ = BeitForSemanticSegmentation(__magic_name__ ) model.to(__magic_name__ ) model.eval() SCREAMING_SNAKE_CASE_ = model(__magic_name__ ) self.parent.assertEqual( result.logits.shape , (self.batch_size, self.num_labels, self.image_size * 2, self.image_size * 2) ) SCREAMING_SNAKE_CASE_ = model(__magic_name__ , labels=__magic_name__ ) self.parent.assertEqual( result.logits.shape , (self.batch_size, self.num_labels, self.image_size * 2, self.image_size * 2) ) def __A ( self : str ) -> List[Any]: SCREAMING_SNAKE_CASE_ = self.prepare_config_and_inputs() SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = config_and_inputs SCREAMING_SNAKE_CASE_ = {"pixel_values": pixel_values} return config, inputs_dict @require_torch class lowerCamelCase (SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , unittest.TestCase ): """simple docstring""" lowerCamelCase__ = ( (BeitModel, BeitForImageClassification, BeitForMaskedImageModeling, BeitForSemanticSegmentation) if is_torch_available() else () ) lowerCamelCase__ = ( { '''feature-extraction''': BeitModel, '''image-classification''': BeitForImageClassification, '''image-segmentation''': BeitForSemanticSegmentation, } if is_torch_available() else {} ) lowerCamelCase__ = False lowerCamelCase__ = False lowerCamelCase__ = False def __A ( self : Tuple ) -> Any: SCREAMING_SNAKE_CASE_ = BeitModelTester(self ) SCREAMING_SNAKE_CASE_ = ConfigTester(self , config_class=__magic_name__ , has_text_modality=__magic_name__ , hidden_size=37 ) def __A ( self : Dict ) -> List[Any]: self.config_tester.run_common_tests() @unittest.skip(reason="BEiT does not use inputs_embeds" ) def __A ( self : List[str] ) -> Optional[Any]: pass @require_torch_multi_gpu @unittest.skip(reason="BEiT has some layers using `add_module` which doesn't work well with `nn.DataParallel`" ) def __A ( self : str ) -> List[str]: pass def __A ( self : List[Any] ) -> 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: SCREAMING_SNAKE_CASE_ = model_class(__magic_name__ ) self.assertIsInstance(model.get_input_embeddings() , (nn.Module) ) SCREAMING_SNAKE_CASE_ = model.get_output_embeddings() self.assertTrue(x is None or isinstance(__magic_name__ , nn.Linear ) ) def __A ( self : Union[str, Any] ) -> int: 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(__magic_name__ ) SCREAMING_SNAKE_CASE_ = inspect.signature(model.forward ) # signature.parameters is an OrderedDict => so arg_names order is deterministic SCREAMING_SNAKE_CASE_ = [*signature.parameters.keys()] SCREAMING_SNAKE_CASE_ = ["pixel_values"] self.assertListEqual(arg_names[:1] , __magic_name__ ) def __A ( self : Tuple ) -> Union[str, Any]: SCREAMING_SNAKE_CASE_ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*__magic_name__ ) def __A ( self : Dict ) -> Union[str, Any]: SCREAMING_SNAKE_CASE_ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_masked_lm(*__magic_name__ ) def __A ( self : Optional[int] ) -> List[Any]: SCREAMING_SNAKE_CASE_ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_image_classification(*__magic_name__ ) def __A ( self : Optional[Any] ) -> List[str]: SCREAMING_SNAKE_CASE_ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_semantic_segmentation(*__magic_name__ ) def __A ( self : int ) -> Optional[int]: if not self.model_tester.is_training: return SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = self.model_tester.prepare_config_and_inputs_for_common() SCREAMING_SNAKE_CASE_ = True for model_class in self.all_model_classes: # we don't test BeitForMaskedImageModeling if model_class in [*get_values(__magic_name__ ), BeitForMaskedImageModeling]: continue SCREAMING_SNAKE_CASE_ = model_class(__magic_name__ ) model.to(__magic_name__ ) model.train() SCREAMING_SNAKE_CASE_ = self._prepare_for_class(__magic_name__ , __magic_name__ , return_labels=__magic_name__ ) SCREAMING_SNAKE_CASE_ = model(**__magic_name__ ).loss loss.backward() def __A ( self : Any ) -> Tuple: SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = self.model_tester.prepare_config_and_inputs_for_common() if not self.model_tester.is_training: return SCREAMING_SNAKE_CASE_ = False SCREAMING_SNAKE_CASE_ = True for model_class in self.all_model_classes: # we don't test BeitForMaskedImageModeling if ( model_class in [*get_values(__magic_name__ ), BeitForMaskedImageModeling] or not model_class.supports_gradient_checkpointing ): continue SCREAMING_SNAKE_CASE_ = model_class(__magic_name__ ) model.gradient_checkpointing_enable() model.to(__magic_name__ ) model.train() SCREAMING_SNAKE_CASE_ = self._prepare_for_class(__magic_name__ , __magic_name__ , return_labels=__magic_name__ ) SCREAMING_SNAKE_CASE_ = model(**__magic_name__ ).loss loss.backward() def __A ( self : List[str] ) -> str: SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = self.model_tester.prepare_config_and_inputs_for_common() SCREAMING_SNAKE_CASE_ = _config_zero_init(__magic_name__ ) for model_class in self.all_model_classes: SCREAMING_SNAKE_CASE_ = model_class(config=__magic_name__ ) for name, param in model.named_parameters(): # we skip lambda parameters as these require special initial values # determined by config.layer_scale_init_value if "lambda" in name: continue if param.requires_grad: self.assertIn( ((param.data.mean() * 1e9).round() / 1e9).item() , [0.0, 1.0] , msg=F'''Parameter {name} of model {model_class} seems not properly initialized''' , ) @slow def __A ( self : int ) -> Optional[int]: for model_name in BEIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: SCREAMING_SNAKE_CASE_ = BeitModel.from_pretrained(__magic_name__ ) self.assertIsNotNone(__magic_name__ ) def a__ ( ): SCREAMING_SNAKE_CASE_ = Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png" ) return image @require_torch @require_vision class lowerCamelCase (unittest.TestCase ): """simple docstring""" @cached_property def __A ( self : List[Any] ) -> str: return BeitImageProcessor.from_pretrained("microsoft/beit-base-patch16-224" ) if is_vision_available() else None @slow def __A ( self : List[str] ) -> Union[str, Any]: SCREAMING_SNAKE_CASE_ = BeitForMaskedImageModeling.from_pretrained("microsoft/beit-base-patch16-224-pt22k" ).to(__magic_name__ ) SCREAMING_SNAKE_CASE_ = self.default_image_processor SCREAMING_SNAKE_CASE_ = prepare_img() SCREAMING_SNAKE_CASE_ = image_processor(images=__magic_name__ , return_tensors="pt" ).pixel_values.to(__magic_name__ ) # prepare bool_masked_pos SCREAMING_SNAKE_CASE_ = torch.ones((1, 196) , dtype=torch.bool ).to(__magic_name__ ) # forward pass with torch.no_grad(): SCREAMING_SNAKE_CASE_ = model(pixel_values=__magic_name__ , bool_masked_pos=__magic_name__ ) SCREAMING_SNAKE_CASE_ = outputs.logits # verify the logits SCREAMING_SNAKE_CASE_ = torch.Size((1, 196, 8_192) ) self.assertEqual(logits.shape , __magic_name__ ) SCREAMING_SNAKE_CASE_ = torch.tensor( [[-3.2437, 0.5072, -13.9174], [-3.2456, 0.4948, -13.9401], [-3.2033, 0.5121, -13.8550]] ).to(__magic_name__ ) self.assertTrue(torch.allclose(logits[bool_masked_pos][:3, :3] , __magic_name__ , atol=1e-2 ) ) @slow def __A ( self : Tuple ) -> int: SCREAMING_SNAKE_CASE_ = BeitForImageClassification.from_pretrained("microsoft/beit-base-patch16-224" ).to(__magic_name__ ) SCREAMING_SNAKE_CASE_ = self.default_image_processor SCREAMING_SNAKE_CASE_ = prepare_img() SCREAMING_SNAKE_CASE_ = image_processor(images=__magic_name__ , return_tensors="pt" ).to(__magic_name__ ) # forward pass with torch.no_grad(): SCREAMING_SNAKE_CASE_ = model(**__magic_name__ ) SCREAMING_SNAKE_CASE_ = outputs.logits # verify the logits SCREAMING_SNAKE_CASE_ = torch.Size((1, 1_000) ) self.assertEqual(logits.shape , __magic_name__ ) SCREAMING_SNAKE_CASE_ = torch.tensor([-1.2385, -1.0987, -1.0108] ).to(__magic_name__ ) self.assertTrue(torch.allclose(logits[0, :3] , __magic_name__ , atol=1e-4 ) ) SCREAMING_SNAKE_CASE_ = 281 self.assertEqual(logits.argmax(-1 ).item() , __magic_name__ ) @slow def __A ( self : Optional[Any] ) -> int: SCREAMING_SNAKE_CASE_ = BeitForImageClassification.from_pretrained("microsoft/beit-large-patch16-224-pt22k-ft22k" ).to( __magic_name__ ) SCREAMING_SNAKE_CASE_ = self.default_image_processor SCREAMING_SNAKE_CASE_ = prepare_img() SCREAMING_SNAKE_CASE_ = image_processor(images=__magic_name__ , return_tensors="pt" ).to(__magic_name__ ) # forward pass with torch.no_grad(): SCREAMING_SNAKE_CASE_ = model(**__magic_name__ ) SCREAMING_SNAKE_CASE_ = outputs.logits # verify the logits SCREAMING_SNAKE_CASE_ = torch.Size((1, 21_841) ) self.assertEqual(logits.shape , __magic_name__ ) SCREAMING_SNAKE_CASE_ = torch.tensor([1.6881, -0.2787, 0.5901] ).to(__magic_name__ ) self.assertTrue(torch.allclose(logits[0, :3] , __magic_name__ , atol=1e-4 ) ) SCREAMING_SNAKE_CASE_ = 2_396 self.assertEqual(logits.argmax(-1 ).item() , __magic_name__ ) @slow def __A ( self : Tuple ) -> Tuple: SCREAMING_SNAKE_CASE_ = BeitForSemanticSegmentation.from_pretrained("microsoft/beit-base-finetuned-ade-640-640" ) SCREAMING_SNAKE_CASE_ = model.to(__magic_name__ ) SCREAMING_SNAKE_CASE_ = BeitImageProcessor(do_resize=__magic_name__ , size=640 , do_center_crop=__magic_name__ ) SCREAMING_SNAKE_CASE_ = load_dataset("hf-internal-testing/fixtures_ade20k" , split="test" ) SCREAMING_SNAKE_CASE_ = Image.open(ds[0]["file"] ) SCREAMING_SNAKE_CASE_ = image_processor(images=__magic_name__ , return_tensors="pt" ).to(__magic_name__ ) # forward pass with torch.no_grad(): SCREAMING_SNAKE_CASE_ = model(**__magic_name__ ) SCREAMING_SNAKE_CASE_ = outputs.logits # verify the logits SCREAMING_SNAKE_CASE_ = torch.Size((1, 150, 160, 160) ) self.assertEqual(logits.shape , __magic_name__ ) SCREAMING_SNAKE_CASE_ = version.parse(PIL.__version__ ) < version.parse("9.0.0" ) if is_pillow_less_than_a: SCREAMING_SNAKE_CASE_ = torch.tensor( [ [[-4.9225, -2.3954, -3.0522], [-2.8822, -1.0046, -1.7561], [-2.9549, -1.3228, -2.1347]], [[-5.8168, -3.4129, -4.0778], [-3.8651, -2.2214, -3.0277], [-3.8356, -2.4643, -3.3535]], [[-0.0078, 3.9952, 4.0754], [2.9856, 4.6944, 5.0035], [3.2413, 4.7813, 4.9969]], ] , device=__magic_name__ , ) else: SCREAMING_SNAKE_CASE_ = torch.tensor( [ [[-4.8960, -2.3688, -3.0355], [-2.8478, -0.9836, -1.7418], [-2.9449, -1.3332, -2.1456]], [[-5.8081, -3.4124, -4.1006], [-3.8561, -2.2081, -3.0323], [-3.8365, -2.4601, -3.3669]], [[-0.0309, 3.9868, 4.0540], [2.9640, 4.6877, 4.9976], [3.2081, 4.7690, 4.9942]], ] , device=__magic_name__ , ) self.assertTrue(torch.allclose(logits[0, :3, :3, :3] , __magic_name__ , atol=1e-4 ) ) @slow def __A ( self : List[str] ) -> Tuple: SCREAMING_SNAKE_CASE_ = BeitForSemanticSegmentation.from_pretrained("microsoft/beit-base-finetuned-ade-640-640" ) SCREAMING_SNAKE_CASE_ = model.to(__magic_name__ ) SCREAMING_SNAKE_CASE_ = BeitImageProcessor(do_resize=__magic_name__ , size=640 , do_center_crop=__magic_name__ ) SCREAMING_SNAKE_CASE_ = load_dataset("hf-internal-testing/fixtures_ade20k" , split="test" ) SCREAMING_SNAKE_CASE_ = Image.open(ds[0]["file"] ) SCREAMING_SNAKE_CASE_ = image_processor(images=__magic_name__ , return_tensors="pt" ).to(__magic_name__ ) # forward pass with torch.no_grad(): SCREAMING_SNAKE_CASE_ = model(**__magic_name__ ) SCREAMING_SNAKE_CASE_ = outputs.logits.detach().cpu() SCREAMING_SNAKE_CASE_ = image_processor.post_process_semantic_segmentation(outputs=__magic_name__ , target_sizes=[(500, 300)] ) SCREAMING_SNAKE_CASE_ = torch.Size((500, 300) ) self.assertEqual(segmentation[0].shape , __magic_name__ ) SCREAMING_SNAKE_CASE_ = image_processor.post_process_semantic_segmentation(outputs=__magic_name__ ) SCREAMING_SNAKE_CASE_ = torch.Size((160, 160) ) self.assertEqual(segmentation[0].shape , __magic_name__ )
305
0
"""simple docstring""" def a__ ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase ): return not any( neighbour == 1 and colored_vertices[i] == color for i, neighbour in enumerate(lowerCamelCase__ ) ) def a__ ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase ): # Base Case if index == len(lowerCamelCase__ ): return True # Recursive Step for i in range(lowerCamelCase__ ): if valid_coloring(graph[index] , lowerCamelCase__ , lowerCamelCase__ ): # Color current vertex SCREAMING_SNAKE_CASE_ = i # Validate coloring if util_color(lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ , index + 1 ): return True # Backtrack SCREAMING_SNAKE_CASE_ = -1 return False def a__ ( __UpperCamelCase , __UpperCamelCase ): SCREAMING_SNAKE_CASE_ = [-1] * len(lowerCamelCase__ ) if util_color(lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ , 0 ): return colored_vertices return []
354
from __future__ import annotations A : Dict = "#" class lowerCamelCase : """simple docstring""" def __init__( self : Dict ) -> None: SCREAMING_SNAKE_CASE_ = {} def __A ( self : List[Any] , __magic_name__ : str ) -> None: SCREAMING_SNAKE_CASE_ = self._trie for char in text: if char not in trie: SCREAMING_SNAKE_CASE_ = {} SCREAMING_SNAKE_CASE_ = trie[char] SCREAMING_SNAKE_CASE_ = True def __A ( self : Union[str, Any] , __magic_name__ : str ) -> tuple | list: SCREAMING_SNAKE_CASE_ = self._trie for char in prefix: if char in trie: SCREAMING_SNAKE_CASE_ = trie[char] else: return [] return self._elements(__magic_name__ ) def __A ( self : int , __magic_name__ : dict ) -> tuple: SCREAMING_SNAKE_CASE_ = [] for c, v in d.items(): SCREAMING_SNAKE_CASE_ = [" "] if c == END else [(c + s) for s in self._elements(__magic_name__ )] result.extend(__magic_name__ ) return tuple(__magic_name__ ) A : Union[str, Any] = Trie() A : Optional[int] = ("depart", "detergent", "daring", "dog", "deer", "deal") for word in words: trie.insert_word(word) def a__ ( __UpperCamelCase ): SCREAMING_SNAKE_CASE_ = trie.find_word(__UpperCamelCase ) return tuple(string + word for word in suffixes ) def a__ ( ): print(autocomplete_using_trie("de" ) ) if __name__ == "__main__": import doctest doctest.testmod() main()
305
0
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_tf_available, is_tokenizers_available, is_torch_available, ) A : Optional[Any] = { "configuration_mobilebert": [ "MOBILEBERT_PRETRAINED_CONFIG_ARCHIVE_MAP", "MobileBertConfig", "MobileBertOnnxConfig", ], "tokenization_mobilebert": ["MobileBertTokenizer"], } try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: A : str = ["MobileBertTokenizerFast"] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: A : List[Any] = [ "MOBILEBERT_PRETRAINED_MODEL_ARCHIVE_LIST", "MobileBertForMaskedLM", "MobileBertForMultipleChoice", "MobileBertForNextSentencePrediction", "MobileBertForPreTraining", "MobileBertForQuestionAnswering", "MobileBertForSequenceClassification", "MobileBertForTokenClassification", "MobileBertLayer", "MobileBertModel", "MobileBertPreTrainedModel", "load_tf_weights_in_mobilebert", ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: A : Any = [ "TF_MOBILEBERT_PRETRAINED_MODEL_ARCHIVE_LIST", "TFMobileBertForMaskedLM", "TFMobileBertForMultipleChoice", "TFMobileBertForNextSentencePrediction", "TFMobileBertForPreTraining", "TFMobileBertForQuestionAnswering", "TFMobileBertForSequenceClassification", "TFMobileBertForTokenClassification", "TFMobileBertMainLayer", "TFMobileBertModel", "TFMobileBertPreTrainedModel", ] if TYPE_CHECKING: from .configuration_mobilebert import ( MOBILEBERT_PRETRAINED_CONFIG_ARCHIVE_MAP, MobileBertConfig, MobileBertOnnxConfig, ) from .tokenization_mobilebert import MobileBertTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_mobilebert_fast import MobileBertTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_mobilebert import ( MOBILEBERT_PRETRAINED_MODEL_ARCHIVE_LIST, MobileBertForMaskedLM, MobileBertForMultipleChoice, MobileBertForNextSentencePrediction, MobileBertForPreTraining, MobileBertForQuestionAnswering, MobileBertForSequenceClassification, MobileBertForTokenClassification, MobileBertLayer, MobileBertModel, MobileBertPreTrainedModel, load_tf_weights_in_mobilebert, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_mobilebert import ( TF_MOBILEBERT_PRETRAINED_MODEL_ARCHIVE_LIST, TFMobileBertForMaskedLM, TFMobileBertForMultipleChoice, TFMobileBertForNextSentencePrediction, TFMobileBertForPreTraining, TFMobileBertForQuestionAnswering, TFMobileBertForSequenceClassification, TFMobileBertForTokenClassification, TFMobileBertMainLayer, TFMobileBertModel, TFMobileBertPreTrainedModel, ) else: import sys A : Optional[int] = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
355
from collections import deque class lowerCamelCase : """simple docstring""" def __init__( self : str , __magic_name__ : str , __magic_name__ : int , __magic_name__ : int ) -> None: SCREAMING_SNAKE_CASE_ = process_name # process name SCREAMING_SNAKE_CASE_ = arrival_time # arrival time of the process # completion time of finished process or last interrupted time SCREAMING_SNAKE_CASE_ = arrival_time SCREAMING_SNAKE_CASE_ = burst_time # remaining burst time SCREAMING_SNAKE_CASE_ = 0 # total time of the process wait in ready queue SCREAMING_SNAKE_CASE_ = 0 # time from arrival time to completion time class lowerCamelCase : """simple docstring""" def __init__( self : Tuple , __magic_name__ : int , __magic_name__ : list[int] , __magic_name__ : deque[Process] , __magic_name__ : int , ) -> None: # total number of mlfq's queues SCREAMING_SNAKE_CASE_ = number_of_queues # time slice of queues that round robin algorithm applied SCREAMING_SNAKE_CASE_ = time_slices # unfinished process is in this ready_queue SCREAMING_SNAKE_CASE_ = queue # current time SCREAMING_SNAKE_CASE_ = current_time # finished process is in this sequence queue SCREAMING_SNAKE_CASE_ = deque() def __A ( self : Dict ) -> list[str]: SCREAMING_SNAKE_CASE_ = [] for i in range(len(self.finish_queue ) ): sequence.append(self.finish_queue[i].process_name ) return sequence def __A ( self : List[str] , __magic_name__ : list[Process] ) -> list[int]: SCREAMING_SNAKE_CASE_ = [] for i in range(len(__magic_name__ ) ): waiting_times.append(queue[i].waiting_time ) return waiting_times def __A ( self : List[str] , __magic_name__ : list[Process] ) -> list[int]: SCREAMING_SNAKE_CASE_ = [] for i in range(len(__magic_name__ ) ): turnaround_times.append(queue[i].turnaround_time ) return turnaround_times def __A ( self : Tuple , __magic_name__ : list[Process] ) -> list[int]: SCREAMING_SNAKE_CASE_ = [] for i in range(len(__magic_name__ ) ): completion_times.append(queue[i].stop_time ) return completion_times def __A ( self : str , __magic_name__ : deque[Process] ) -> list[int]: return [q.burst_time for q in queue] def __A ( self : Optional[Any] , __magic_name__ : Process ) -> int: process.waiting_time += self.current_time - process.stop_time return process.waiting_time def __A ( self : Optional[Any] , __magic_name__ : deque[Process] ) -> deque[Process]: SCREAMING_SNAKE_CASE_ = deque() # sequence deque of finished process while len(__magic_name__ ) != 0: SCREAMING_SNAKE_CASE_ = ready_queue.popleft() # current process # if process's arrival time is later than current time, update current time if self.current_time < cp.arrival_time: self.current_time += cp.arrival_time # update waiting time of current process self.update_waiting_time(__magic_name__ ) # update current time self.current_time += cp.burst_time # finish the process and set the process's burst-time 0 SCREAMING_SNAKE_CASE_ = 0 # set the process's turnaround time because it is finished SCREAMING_SNAKE_CASE_ = self.current_time - cp.arrival_time # set the completion time SCREAMING_SNAKE_CASE_ = self.current_time # add the process to queue that has finished queue finished.append(__magic_name__ ) self.finish_queue.extend(__magic_name__ ) # add finished process to finish queue # FCFS will finish all remaining processes return finished def __A ( self : Any , __magic_name__ : deque[Process] , __magic_name__ : int ) -> tuple[deque[Process], deque[Process]]: SCREAMING_SNAKE_CASE_ = deque() # sequence deque of terminated process # just for 1 cycle and unfinished processes will go back to queue for _ in range(len(__magic_name__ ) ): SCREAMING_SNAKE_CASE_ = ready_queue.popleft() # current process # if process's arrival time is later than current time, update current time if self.current_time < cp.arrival_time: self.current_time += cp.arrival_time # update waiting time of unfinished processes self.update_waiting_time(__magic_name__ ) # if the burst time of process is bigger than time-slice if cp.burst_time > time_slice: # use CPU for only time-slice self.current_time += time_slice # update remaining burst time cp.burst_time -= time_slice # update end point time SCREAMING_SNAKE_CASE_ = self.current_time # locate the process behind the queue because it is not finished ready_queue.append(__magic_name__ ) else: # use CPU for remaining burst time self.current_time += cp.burst_time # set burst time 0 because the process is finished SCREAMING_SNAKE_CASE_ = 0 # set the finish time SCREAMING_SNAKE_CASE_ = self.current_time # update the process' turnaround time because it is finished SCREAMING_SNAKE_CASE_ = self.current_time - cp.arrival_time # add the process to queue that has finished queue finished.append(__magic_name__ ) self.finish_queue.extend(__magic_name__ ) # add finished process to finish queue # return finished processes queue and remaining processes queue return finished, ready_queue def __A ( self : Any ) -> deque[Process]: # all queues except last one have round_robin algorithm for i in range(self.number_of_queues - 1 ): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = self.round_robin( self.ready_queue , self.time_slices[i] ) # the last queue has first_come_first_served algorithm self.first_come_first_served(self.ready_queue ) return self.finish_queue if __name__ == "__main__": import doctest A : Dict = Process("P1", 0, 53) A : str = Process("P2", 0, 17) A : List[Any] = Process("P3", 0, 68) A : List[str] = Process("P4", 0, 24) A : Dict = 3 A : Any = [17, 25] A : Dict = deque([Pa, Pa, Pa, Pa]) if len(time_slices) != number_of_queues - 1: raise SystemExit(0) doctest.testmod(extraglobs={"queue": deque([Pa, Pa, Pa, Pa])}) A : Union[str, Any] = Process("P1", 0, 53) A : Any = Process("P2", 0, 17) A : Dict = Process("P3", 0, 68) A : List[str] = Process("P4", 0, 24) A : Optional[int] = 3 A : int = [17, 25] A : Union[str, Any] = deque([Pa, Pa, Pa, Pa]) A : Tuple = MLFQ(number_of_queues, time_slices, queue, 0) A : Tuple = mlfq.multi_level_feedback_queue() # print total waiting times of processes(P1, P2, P3, P4) print( f"waiting time:\ \t\t\t{MLFQ.calculate_waiting_time(mlfq, [Pa, Pa, Pa, Pa])}" ) # print completion times of processes(P1, P2, P3, P4) print( f"completion time:\ \t\t{MLFQ.calculate_completion_time(mlfq, [Pa, Pa, Pa, Pa])}" ) # print total turnaround times of processes(P1, P2, P3, P4) print( f"turnaround time:\ \t\t{MLFQ.calculate_turnaround_time(mlfq, [Pa, Pa, Pa, Pa])}" ) # print sequence of finished processes print( f"sequence of finished processes:\ {mlfq.calculate_sequence_of_finish_queue()}" )
305
0
"""simple docstring""" from __future__ import annotations import numpy as np def a__ ( __UpperCamelCase ): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = np.shape(__snake_case ) if rows != columns: SCREAMING_SNAKE_CASE_ = ( "\'table\' has to be of square shaped array but got a " F'''{rows}x{columns} array:\n{table}''' ) raise ValueError(__snake_case ) SCREAMING_SNAKE_CASE_ = np.zeros((rows, columns) ) SCREAMING_SNAKE_CASE_ = np.zeros((rows, columns) ) for i in range(__snake_case ): for j in range(__snake_case ): SCREAMING_SNAKE_CASE_ = sum(lower[i][k] * upper[k][j] for k in range(__snake_case ) ) if upper[j][j] == 0: raise ArithmeticError("No LU decomposition exists" ) SCREAMING_SNAKE_CASE_ = (table[i][j] - total) / upper[j][j] SCREAMING_SNAKE_CASE_ = 1 for j in range(__snake_case , __snake_case ): SCREAMING_SNAKE_CASE_ = sum(lower[i][k] * upper[k][j] for k in range(__snake_case ) ) SCREAMING_SNAKE_CASE_ = table[i][j] - total return lower, upper if __name__ == "__main__": import doctest doctest.testmod()
356
import torch def a__ ( ): if torch.cuda.is_available(): SCREAMING_SNAKE_CASE_ = torch.cuda.device_count() else: SCREAMING_SNAKE_CASE_ = 0 print(F'''Successfully ran on {num_gpus} GPUs''' ) if __name__ == "__main__": main()
305
0
import os from itertools import chain from random import randrange, shuffle import pytest from .sola import PokerHand A : Optional[int] = ( "4S 3H 2C 7S 5H", "9D 8H 2C 6S 7H", "2D 6D 9D TH 7D", "TC 8C 2S JH 6C", "JH 8S TH AH QH", "TS KS 5S 9S AC", "KD 6S 9D TH AD", "KS 8D 4D 9S 4S", # pair "8C 4S KH JS 4D", # pair "QH 8H KD JH 8S", # pair "KC 4H KS 2H 8D", # pair "KD 4S KC 3H 8S", # pair "AH 8S AS KC JH", # pair "3H 4C 4H 3S 2H", # 2 pairs "5S 5D 2C KH KH", # 2 pairs "3C KH 5D 5S KH", # 2 pairs "AS 3C KH AD KH", # 2 pairs "7C 7S 3S 7H 5S", # 3 of a kind "7C 7S KH 2H 7H", # 3 of a kind "AC KH QH AH AS", # 3 of a kind "2H 4D 3C AS 5S", # straight (low ace) "3C 5C 4C 2C 6H", # straight "6S 8S 7S 5H 9H", # straight "JS QS 9H TS KH", # straight "QC KH TS JS AH", # straight (high ace) "8C 9C 5C 3C TC", # flush "3S 8S 9S 5S KS", # flush "4C 5C 9C 8C KC", # flush "JH 8H AH KH QH", # flush "3D 2H 3H 2C 2D", # full house "2H 2C 3S 3H 3D", # full house "KH KC 3S 3H 3D", # full house "JC 6H JS JD JH", # 4 of a kind "JC 7H JS JD JH", # 4 of a kind "JC KH JS JD JH", # 4 of a kind "2S AS 4S 5S 3S", # straight flush (low ace) "2D 6D 3D 4D 5D", # straight flush "5C 6C 3C 7C 4C", # straight flush "JH 9H TH KH QH", # straight flush "JH AH TH KH QH", # royal flush (high ace straight flush) ) A : Optional[Any] = ( ("2H 3H 4H 5H 6H", "KS AS TS QS JS", "Loss"), ("2H 3H 4H 5H 6H", "AS AD AC AH JD", "Win"), ("AS AH 2H AD AC", "JS JD JC JH 3D", "Win"), ("2S AH 2H AS AC", "JS JD JC JH AD", "Loss"), ("2S AH 2H AS AC", "2H 3H 5H 6H 7H", "Win"), ("AS 3S 4S 8S 2S", "2H 3H 5H 6H 7H", "Win"), ("2H 3H 5H 6H 7H", "2S 3H 4H 5S 6C", "Win"), ("2S 3H 4H 5S 6C", "3D 4C 5H 6H 2S", "Tie"), ("2S 3H 4H 5S 6C", "AH AC 5H 6H AS", "Win"), ("2S 2H 4H 5S 4C", "AH AC 5H 6H AS", "Loss"), ("2S 2H 4H 5S 4C", "AH AC 5H 6H 7S", "Win"), ("6S AD 7H 4S AS", "AH AC 5H 6H 7S", "Loss"), ("2S AH 4H 5S KC", "AH AC 5H 6H 7S", "Loss"), ("2S 3H 6H 7S 9C", "7H 3C TH 6H 9S", "Loss"), ("4S 5H 6H TS AC", "3S 5H 6H TS AC", "Win"), ("2S AH 4H 5S 6C", "AD 4C 5H 6H 2C", "Tie"), ("AS AH 3H AD AC", "AS AH 2H AD AC", "Win"), ("AH AC 5H 5C QS", "AH AC 5H 5C KS", "Loss"), ("AH AC 5H 5C QS", "KH KC 5H 5C QS", "Win"), ("7C 7S KH 2H 7H", "3C 3S AH 2H 3H", "Win"), ("3C 3S AH 2H 3H", "7C 7S KH 2H 7H", "Loss"), ("6H 5H 4H 3H 2H", "5H 4H 3H 2H AH", "Win"), ("5H 4H 3H 2H AH", "5H 4H 3H 2H AH", "Tie"), ("5H 4H 3H 2H AH", "6H 5H 4H 3H 2H", "Loss"), ("AH AD KS KC AC", "AH KD KH AC KC", "Win"), ("2H 4D 3C AS 5S", "2H 4D 3C 6S 5S", "Loss"), ("2H 3S 3C 3H 2S", "3S 3C 2S 2H 2D", "Win"), ("4D 6D 5D 2D JH", "3S 8S 3H TC KH", "Loss"), ("4S 6C 8S 3S 7S", "AD KS 2D 7D 7C", "Loss"), ("6S 4C 7H 8C 3H", "5H JC AH 9D 9C", "Loss"), ("9D 9H JH TC QH", "3C 2S JS 5C 7H", "Win"), ("2H TC 8S AD 9S", "4H TS 7H 2C 5C", "Win"), ("9D 3S 2C 7S 7C", "JC TD 3C TC 9H", "Loss"), ) A : Dict = ( ("2H 3H 4H 5H 6H", True), ("AS AH 2H AD AC", False), ("2H 3H 5H 6H 7H", True), ("KS AS TS QS JS", True), ("8H 9H QS JS TH", False), ("AS 3S 4S 8S 2S", True), ) A : Optional[Any] = ( ("2H 3H 4H 5H 6H", True), ("AS AH 2H AD AC", False), ("2H 3H 5H 6H 7H", False), ("KS AS TS QS JS", True), ("8H 9H QS JS TH", True), ) A : int = ( ("2H 4D 3C AS 5S", True, [5, 4, 3, 2, 14]), ("2H 5D 3C AS 5S", False, [14, 5, 5, 3, 2]), ("JH QD KC AS TS", False, [14, 13, 12, 11, 10]), ("9D 3S 2C 7S 7C", False, [9, 7, 7, 3, 2]), ) A : List[str] = ( ("JH AH TH KH QH", 0), ("JH 9H TH KH QH", 0), ("JC KH JS JD JH", 7), ("KH KC 3S 3H 3D", 6), ("8C 9C 5C 3C TC", 0), ("JS QS 9H TS KH", 0), ("7C 7S KH 2H 7H", 3), ("3C KH 5D 5S KH", 2), ("QH 8H KD JH 8S", 1), ("2D 6D 9D TH 7D", 0), ) A : List[str] = ( ("JH AH TH KH QH", 23), ("JH 9H TH KH QH", 22), ("JC KH JS JD JH", 21), ("KH KC 3S 3H 3D", 20), ("8C 9C 5C 3C TC", 19), ("JS QS 9H TS KH", 18), ("7C 7S KH 2H 7H", 17), ("3C KH 5D 5S KH", 16), ("QH 8H KD JH 8S", 15), ("2D 6D 9D TH 7D", 14), ) def a__ ( ): """simple docstring""" SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = randrange(len(__A ) ), randrange(len(__A ) ) SCREAMING_SNAKE_CASE_ = ["Loss", "Tie", "Win"][(play >= oppo) + (play > oppo)] SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = SORTED_HANDS[play], SORTED_HANDS[oppo] return hand, other, expected def a__ ( __UpperCamelCase = 1_0_0 ): """simple docstring""" return (generate_random_hand() for _ in range(__A )) @pytest.mark.parametrize("hand, expected" , __A ) def a__ ( __UpperCamelCase , __UpperCamelCase ): """simple docstring""" assert PokerHand(__A )._is_flush() == expected @pytest.mark.parametrize("hand, expected" , __A ) def a__ ( __UpperCamelCase , __UpperCamelCase ): """simple docstring""" assert PokerHand(__A )._is_straight() == expected @pytest.mark.parametrize("hand, expected, card_values" , __A ) def a__ ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase ): """simple docstring""" SCREAMING_SNAKE_CASE_ = PokerHand(__A ) assert player._is_five_high_straight() == expected assert player._card_values == card_values @pytest.mark.parametrize("hand, expected" , __A ) def a__ ( __UpperCamelCase , __UpperCamelCase ): """simple docstring""" assert PokerHand(__A )._is_same_kind() == expected @pytest.mark.parametrize("hand, expected" , __A ) def a__ ( __UpperCamelCase , __UpperCamelCase ): """simple docstring""" assert PokerHand(__A )._hand_type == expected @pytest.mark.parametrize("hand, other, expected" , __A ) def a__ ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase ): """simple docstring""" assert PokerHand(__A ).compare_with(PokerHand(__A ) ) == expected @pytest.mark.parametrize("hand, other, expected" , generate_random_hands() ) def a__ ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase ): """simple docstring""" assert PokerHand(__A ).compare_with(PokerHand(__A ) ) == expected def a__ ( ): """simple docstring""" SCREAMING_SNAKE_CASE_ = [PokerHand(__A ) for hand in SORTED_HANDS] SCREAMING_SNAKE_CASE_ = poker_hands.copy() shuffle(__A ) SCREAMING_SNAKE_CASE_ = chain(sorted(__A ) ) for index, hand in enumerate(__A ): assert hand == poker_hands[index] def a__ ( ): """simple docstring""" SCREAMING_SNAKE_CASE_ = [PokerHand("2D AC 3H 4H 5S" ), PokerHand("2S 3H 4H 5S 6C" )] pokerhands.sort(reverse=__A ) assert pokerhands[0].__str__() == "2S 3H 4H 5S 6C" def a__ ( ): """simple docstring""" SCREAMING_SNAKE_CASE_ = PokerHand("2C 4S AS 3D 5C" ) SCREAMING_SNAKE_CASE_ = True SCREAMING_SNAKE_CASE_ = [5, 4, 3, 2, 1_4] for _ in range(1_0 ): assert pokerhand._is_five_high_straight() == expected assert pokerhand._card_values == expected_card_values def a__ ( ): """simple docstring""" SCREAMING_SNAKE_CASE_ = 0 SCREAMING_SNAKE_CASE_ = os.path.abspath(os.path.dirname(__A ) ) SCREAMING_SNAKE_CASE_ = os.path.join(__A , "poker_hands.txt" ) with open(__A ) as file_hand: for line in file_hand: SCREAMING_SNAKE_CASE_ = line[:1_4].strip() SCREAMING_SNAKE_CASE_ = line[1_5:].strip() SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = PokerHand(__A ), PokerHand(__A ) SCREAMING_SNAKE_CASE_ = player.compare_with(__A ) if output == "Win": answer += 1 assert answer == 3_7_6
357
from collections.abc import Generator from math import sin def a__ ( __UpperCamelCase ): if len(__UpperCamelCase ) != 3_2: raise ValueError("Input must be of length 32" ) SCREAMING_SNAKE_CASE_ = b"" for i in [3, 2, 1, 0]: little_endian += string_aa[8 * i : 8 * i + 8] return little_endian def a__ ( __UpperCamelCase ): if i < 0: raise ValueError("Input must be non-negative" ) SCREAMING_SNAKE_CASE_ = format(__UpperCamelCase , "08x" )[-8:] SCREAMING_SNAKE_CASE_ = b"" for i in [3, 2, 1, 0]: little_endian_hex += hex_rep[2 * i : 2 * i + 2].encode("utf-8" ) return little_endian_hex def a__ ( __UpperCamelCase ): SCREAMING_SNAKE_CASE_ = b"" for char in message: bit_string += format(__UpperCamelCase , "08b" ).encode("utf-8" ) SCREAMING_SNAKE_CASE_ = format(len(__UpperCamelCase ) , "064b" ).encode("utf-8" ) # Pad bit_string to a multiple of 512 chars bit_string += b"1" while len(__UpperCamelCase ) % 5_1_2 != 4_4_8: bit_string += b"0" bit_string += to_little_endian(start_len[3_2:] ) + to_little_endian(start_len[:3_2] ) return bit_string def a__ ( __UpperCamelCase ): if len(__UpperCamelCase ) % 5_1_2 != 0: raise ValueError("Input must have length that's a multiple of 512" ) for pos in range(0 , len(__UpperCamelCase ) , 5_1_2 ): SCREAMING_SNAKE_CASE_ = bit_string[pos : pos + 5_1_2] SCREAMING_SNAKE_CASE_ = [] for i in range(0 , 5_1_2 , 3_2 ): block_words.append(int(to_little_endian(block[i : i + 3_2] ) , 2 ) ) yield block_words def a__ ( __UpperCamelCase ): if i < 0: raise ValueError("Input must be non-negative" ) SCREAMING_SNAKE_CASE_ = format(__UpperCamelCase , "032b" ) SCREAMING_SNAKE_CASE_ = "" for c in i_str: new_str += "1" if c == "0" else "0" return int(__UpperCamelCase , 2 ) def a__ ( __UpperCamelCase , __UpperCamelCase ): return (a + b) % 2**3_2 def a__ ( __UpperCamelCase , __UpperCamelCase ): if i < 0: raise ValueError("Input must be non-negative" ) if shift < 0: raise ValueError("Shift must be non-negative" ) return ((i << shift) ^ (i >> (3_2 - shift))) % 2**3_2 def a__ ( __UpperCamelCase ): SCREAMING_SNAKE_CASE_ = preprocess(__UpperCamelCase ) SCREAMING_SNAKE_CASE_ = [int(2**3_2 * abs(sin(i + 1 ) ) ) for i in range(6_4 )] # Starting states SCREAMING_SNAKE_CASE_ = 0X67452301 SCREAMING_SNAKE_CASE_ = 0Xefcdab89 SCREAMING_SNAKE_CASE_ = 0X98badcfe SCREAMING_SNAKE_CASE_ = 0X10325476 SCREAMING_SNAKE_CASE_ = [ 7, 1_2, 1_7, 2_2, 7, 1_2, 1_7, 2_2, 7, 1_2, 1_7, 2_2, 7, 1_2, 1_7, 2_2, 5, 9, 1_4, 2_0, 5, 9, 1_4, 2_0, 5, 9, 1_4, 2_0, 5, 9, 1_4, 2_0, 4, 1_1, 1_6, 2_3, 4, 1_1, 1_6, 2_3, 4, 1_1, 1_6, 2_3, 4, 1_1, 1_6, 2_3, 6, 1_0, 1_5, 2_1, 6, 1_0, 1_5, 2_1, 6, 1_0, 1_5, 2_1, 6, 1_0, 1_5, 2_1, ] # Process bit string in chunks, each with 16 32-char words for block_words in get_block_words(__UpperCamelCase ): SCREAMING_SNAKE_CASE_ = aa SCREAMING_SNAKE_CASE_ = ba SCREAMING_SNAKE_CASE_ = ca SCREAMING_SNAKE_CASE_ = da # Hash current chunk for i in range(6_4 ): if i <= 1_5: # f = (b & c) | (not_32(b) & d) # Alternate definition for f SCREAMING_SNAKE_CASE_ = d ^ (b & (c ^ d)) SCREAMING_SNAKE_CASE_ = i elif i <= 3_1: # f = (d & b) | (not_32(d) & c) # Alternate definition for f SCREAMING_SNAKE_CASE_ = c ^ (d & (b ^ c)) SCREAMING_SNAKE_CASE_ = (5 * i + 1) % 1_6 elif i <= 4_7: SCREAMING_SNAKE_CASE_ = b ^ c ^ d SCREAMING_SNAKE_CASE_ = (3 * i + 5) % 1_6 else: SCREAMING_SNAKE_CASE_ = c ^ (b | not_aa(__UpperCamelCase )) SCREAMING_SNAKE_CASE_ = (7 * i) % 1_6 SCREAMING_SNAKE_CASE_ = (f + a + added_consts[i] + block_words[g]) % 2**3_2 SCREAMING_SNAKE_CASE_ = d SCREAMING_SNAKE_CASE_ = c SCREAMING_SNAKE_CASE_ = b SCREAMING_SNAKE_CASE_ = sum_aa(__UpperCamelCase , left_rotate_aa(__UpperCamelCase , shift_amounts[i] ) ) # Add hashed chunk to running total SCREAMING_SNAKE_CASE_ = sum_aa(__UpperCamelCase , __UpperCamelCase ) SCREAMING_SNAKE_CASE_ = sum_aa(__UpperCamelCase , __UpperCamelCase ) SCREAMING_SNAKE_CASE_ = sum_aa(__UpperCamelCase , __UpperCamelCase ) SCREAMING_SNAKE_CASE_ = sum_aa(__UpperCamelCase , __UpperCamelCase ) SCREAMING_SNAKE_CASE_ = reformat_hex(__UpperCamelCase ) + reformat_hex(__UpperCamelCase ) + reformat_hex(__UpperCamelCase ) + reformat_hex(__UpperCamelCase ) return digest if __name__ == "__main__": import doctest doctest.testmod()
305
0
import itertools import json import linecache import os import pickle import re import socket import string from collections import Counter from logging import getLogger from pathlib import Path from typing import Callable, Dict, Iterable, List import git import torch from torch.utils.data import Dataset from transformers import BartTokenizer, RagTokenizer, TaTokenizer def a__ ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase=True , __UpperCamelCase="pt" ): SCREAMING_SNAKE_CASE_ = {"""add_prefix_space""": True} if isinstance(lowerCamelCase_ , lowerCamelCase_ ) and not line.startswith(" " ) else {} SCREAMING_SNAKE_CASE_ = padding_side return tokenizer( [line] , max_length=lowerCamelCase_ , padding="max_length" if pad_to_max_length else None , truncation=lowerCamelCase_ , return_tensors=lowerCamelCase_ , add_special_tokens=lowerCamelCase_ , **lowerCamelCase_ , ) def a__ ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase=None , ): SCREAMING_SNAKE_CASE_ = input_ids.ne(lowerCamelCase_ ).any(dim=0 ) if attention_mask is None: return input_ids[:, keep_column_mask] else: return (input_ids[:, keep_column_mask], attention_mask[:, keep_column_mask]) class lowerCamelCase (lowercase__ ): """simple docstring""" def __init__( self : List[Any] , __magic_name__ : List[Any] , __magic_name__ : Any , __magic_name__ : int , __magic_name__ : Optional[Any] , __magic_name__ : Any="train" , __magic_name__ : Optional[int]=None , __magic_name__ : int=None , __magic_name__ : List[Any]=None , __magic_name__ : Optional[int]="" , ) -> Dict: super().__init__() SCREAMING_SNAKE_CASE_ = Path(_UpperCamelCase ).joinpath(type_path + ".source" ) SCREAMING_SNAKE_CASE_ = Path(_UpperCamelCase ).joinpath(type_path + ".target" ) SCREAMING_SNAKE_CASE_ = self.get_char_lens(self.src_file ) SCREAMING_SNAKE_CASE_ = max_source_length SCREAMING_SNAKE_CASE_ = max_target_length assert min(self.src_lens ) > 0, F'''found empty line in {self.src_file}''' SCREAMING_SNAKE_CASE_ = tokenizer SCREAMING_SNAKE_CASE_ = prefix if n_obs is not None: SCREAMING_SNAKE_CASE_ = self.src_lens[:n_obs] SCREAMING_SNAKE_CASE_ = src_lang SCREAMING_SNAKE_CASE_ = tgt_lang def __len__( self : Any ) -> List[Any]: return len(self.src_lens ) def __getitem__( self : List[str] , __magic_name__ : Union[str, Any] ) -> int: SCREAMING_SNAKE_CASE_ = index + 1 # linecache starts at 1 SCREAMING_SNAKE_CASE_ = self.prefix + linecache.getline(str(self.src_file ) , _UpperCamelCase ).rstrip("\n" ) SCREAMING_SNAKE_CASE_ = linecache.getline(str(self.tgt_file ) , _UpperCamelCase ).rstrip("\n" ) assert source_line, F'''empty source line for index {index}''' assert tgt_line, F'''empty tgt line for index {index}''' # Need to add eos token manually for T5 if isinstance(self.tokenizer , _UpperCamelCase ): source_line += self.tokenizer.eos_token tgt_line += self.tokenizer.eos_token # Pad source and target to the right SCREAMING_SNAKE_CASE_ = ( self.tokenizer.question_encoder if isinstance(self.tokenizer , _UpperCamelCase ) else self.tokenizer ) SCREAMING_SNAKE_CASE_ = self.tokenizer.generator if isinstance(self.tokenizer , _UpperCamelCase ) else self.tokenizer SCREAMING_SNAKE_CASE_ = encode_line(_UpperCamelCase , _UpperCamelCase , self.max_source_length , "right" ) SCREAMING_SNAKE_CASE_ = encode_line(_UpperCamelCase , _UpperCamelCase , self.max_target_length , "right" ) SCREAMING_SNAKE_CASE_ = source_inputs["""input_ids"""].squeeze() SCREAMING_SNAKE_CASE_ = target_inputs["""input_ids"""].squeeze() SCREAMING_SNAKE_CASE_ = source_inputs["""attention_mask"""].squeeze() return { "input_ids": source_ids, "attention_mask": src_mask, "decoder_input_ids": target_ids, } @staticmethod def __A ( __magic_name__ : str ) -> List[str]: return [len(_UpperCamelCase ) for x in Path(_UpperCamelCase ).open().readlines()] def __A ( self : Optional[int] , __magic_name__ : List[str] ) -> Dict: SCREAMING_SNAKE_CASE_ = torch.stack([x["input_ids"] for x in batch] ) SCREAMING_SNAKE_CASE_ = torch.stack([x["attention_mask"] for x in batch] ) SCREAMING_SNAKE_CASE_ = torch.stack([x["decoder_input_ids"] for x in batch] ) SCREAMING_SNAKE_CASE_ = ( self.tokenizer.generator.pad_token_id if isinstance(self.tokenizer , _UpperCamelCase ) else self.tokenizer.pad_token_id ) SCREAMING_SNAKE_CASE_ = ( self.tokenizer.question_encoder.pad_token_id if isinstance(self.tokenizer , _UpperCamelCase ) else self.tokenizer.pad_token_id ) SCREAMING_SNAKE_CASE_ = trim_batch(_UpperCamelCase , _UpperCamelCase ) SCREAMING_SNAKE_CASE_ = trim_batch(_UpperCamelCase , _UpperCamelCase , attention_mask=_UpperCamelCase ) SCREAMING_SNAKE_CASE_ = { """input_ids""": source_ids, """attention_mask""": source_mask, """decoder_input_ids""": y, } return batch A : List[Any] = getLogger(__name__) def a__ ( __UpperCamelCase ): return list(itertools.chain.from_iterable(lowerCamelCase_ ) ) def a__ ( __UpperCamelCase ): SCREAMING_SNAKE_CASE_ = get_git_info() save_json(lowerCamelCase_ , os.path.join(lowerCamelCase_ , "git_log.json" ) ) def a__ ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase=4 , **__UpperCamelCase ): with open(lowerCamelCase_ , "w" ) as f: json.dump(lowerCamelCase_ , lowerCamelCase_ , indent=lowerCamelCase_ , **lowerCamelCase_ ) def a__ ( __UpperCamelCase ): with open(lowerCamelCase_ ) as f: return json.load(lowerCamelCase_ ) def a__ ( ): SCREAMING_SNAKE_CASE_ = git.Repo(search_parent_directories=lowerCamelCase_ ) SCREAMING_SNAKE_CASE_ = { """repo_id""": str(lowerCamelCase_ ), """repo_sha""": str(repo.head.object.hexsha ), """repo_branch""": str(repo.active_branch ), """hostname""": str(socket.gethostname() ), } return repo_infos def a__ ( __UpperCamelCase , __UpperCamelCase ): return list(map(lowerCamelCase_ , lowerCamelCase_ ) ) def a__ ( __UpperCamelCase , __UpperCamelCase ): with open(lowerCamelCase_ , "wb" ) as f: return pickle.dump(lowerCamelCase_ , lowerCamelCase_ ) def a__ ( __UpperCamelCase ): def remove_articles(__UpperCamelCase ): return re.sub(r"\b(a|an|the)\b" , " " , lowerCamelCase_ ) def white_space_fix(__UpperCamelCase ): return " ".join(text.split() ) def remove_punc(__UpperCamelCase ): SCREAMING_SNAKE_CASE_ = set(string.punctuation ) return "".join(ch for ch in text if ch not in exclude ) def lower(__UpperCamelCase ): return text.lower() return white_space_fix(remove_articles(remove_punc(lower(lowerCamelCase_ ) ) ) ) def a__ ( __UpperCamelCase , __UpperCamelCase ): SCREAMING_SNAKE_CASE_ = normalize_answer(lowerCamelCase_ ).split() SCREAMING_SNAKE_CASE_ = normalize_answer(lowerCamelCase_ ).split() SCREAMING_SNAKE_CASE_ = Counter(lowerCamelCase_ ) & Counter(lowerCamelCase_ ) SCREAMING_SNAKE_CASE_ = sum(common.values() ) if num_same == 0: return 0 SCREAMING_SNAKE_CASE_ = 1.0 * num_same / len(lowerCamelCase_ ) SCREAMING_SNAKE_CASE_ = 1.0 * num_same / len(lowerCamelCase_ ) SCREAMING_SNAKE_CASE_ = (2 * precision * recall) / (precision + recall) return fa def a__ ( __UpperCamelCase , __UpperCamelCase ): return normalize_answer(lowerCamelCase_ ) == normalize_answer(lowerCamelCase_ ) def a__ ( __UpperCamelCase , __UpperCamelCase ): assert len(lowerCamelCase_ ) == len(lowerCamelCase_ ) SCREAMING_SNAKE_CASE_ = 0 for hypo, pred in zip(lowerCamelCase_ , lowerCamelCase_ ): em += exact_match_score(lowerCamelCase_ , lowerCamelCase_ ) if len(lowerCamelCase_ ) > 0: em /= len(lowerCamelCase_ ) return {"em": em} def a__ ( __UpperCamelCase ): return model_prefix.startswith("rag" ) def a__ ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase ): SCREAMING_SNAKE_CASE_ = {p: p for p in extra_params} # T5 models don't have `dropout` param, they have `dropout_rate` instead SCREAMING_SNAKE_CASE_ = """dropout_rate""" for p in extra_params: if getattr(lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ ): if not hasattr(lowerCamelCase_ , lowerCamelCase_ ) and not hasattr(lowerCamelCase_ , equivalent_param[p] ): logger.info("config doesn't have a `{}` attribute".format(lowerCamelCase_ ) ) delattr(lowerCamelCase_ , lowerCamelCase_ ) continue SCREAMING_SNAKE_CASE_ = p if hasattr(lowerCamelCase_ , lowerCamelCase_ ) else equivalent_param[p] setattr(lowerCamelCase_ , lowerCamelCase_ , getattr(lowerCamelCase_ , lowerCamelCase_ ) ) delattr(lowerCamelCase_ , lowerCamelCase_ ) return hparams, config
358
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, BlipaProcessor, BlipImageProcessor, GPTaTokenizer, PreTrainedTokenizerFast @require_vision class lowerCamelCase (unittest.TestCase ): """simple docstring""" def __A ( self : int ) -> Any: SCREAMING_SNAKE_CASE_ = tempfile.mkdtemp() SCREAMING_SNAKE_CASE_ = BlipImageProcessor() SCREAMING_SNAKE_CASE_ = GPTaTokenizer.from_pretrained("hf-internal-testing/tiny-random-GPT2Model" ) SCREAMING_SNAKE_CASE_ = BlipaProcessor(__magic_name__ , __magic_name__ ) processor.save_pretrained(self.tmpdirname ) def __A ( self : str , **__magic_name__ : int ) -> Union[str, Any]: return AutoProcessor.from_pretrained(self.tmpdirname , **__magic_name__ ).tokenizer def __A ( self : Dict , **__magic_name__ : List[Any] ) -> int: return AutoProcessor.from_pretrained(self.tmpdirname , **__magic_name__ ).image_processor def __A ( self : int ) -> Any: shutil.rmtree(self.tmpdirname ) def __A ( self : Dict ) -> Dict: SCREAMING_SNAKE_CASE_ = [np.random.randint(255 , size=(3, 30, 400) , dtype=np.uinta )] SCREAMING_SNAKE_CASE_ = [Image.fromarray(np.moveaxis(__magic_name__ , 0 , -1 ) ) for x in image_inputs] return image_inputs def __A ( self : List[Any] ) -> Union[str, Any]: SCREAMING_SNAKE_CASE_ = BlipaProcessor(tokenizer=self.get_tokenizer() , image_processor=self.get_image_processor() ) processor.save_pretrained(self.tmpdirname ) SCREAMING_SNAKE_CASE_ = self.get_tokenizer(bos_token="(BOS)" , eos_token="(EOS)" ) SCREAMING_SNAKE_CASE_ = self.get_image_processor(do_normalize=__magic_name__ , padding_value=1.0 ) SCREAMING_SNAKE_CASE_ = BlipaProcessor.from_pretrained( self.tmpdirname , bos_token="(BOS)" , eos_token="(EOS)" , do_normalize=__magic_name__ , padding_value=1.0 ) self.assertEqual(processor.tokenizer.get_vocab() , tokenizer_add_kwargs.get_vocab() ) self.assertIsInstance(processor.tokenizer , __magic_name__ ) self.assertEqual(processor.image_processor.to_json_string() , image_processor_add_kwargs.to_json_string() ) self.assertIsInstance(processor.image_processor , __magic_name__ ) def __A ( self : Tuple ) -> int: SCREAMING_SNAKE_CASE_ = self.get_image_processor() SCREAMING_SNAKE_CASE_ = self.get_tokenizer() SCREAMING_SNAKE_CASE_ = BlipaProcessor(tokenizer=__magic_name__ , image_processor=__magic_name__ ) SCREAMING_SNAKE_CASE_ = self.prepare_image_inputs() SCREAMING_SNAKE_CASE_ = image_processor(__magic_name__ , return_tensors="np" ) SCREAMING_SNAKE_CASE_ = processor(images=__magic_name__ , 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 __A ( self : str ) -> Tuple: SCREAMING_SNAKE_CASE_ = self.get_image_processor() SCREAMING_SNAKE_CASE_ = self.get_tokenizer() SCREAMING_SNAKE_CASE_ = BlipaProcessor(tokenizer=__magic_name__ , image_processor=__magic_name__ ) SCREAMING_SNAKE_CASE_ = "lower newer" SCREAMING_SNAKE_CASE_ = processor(text=__magic_name__ ) SCREAMING_SNAKE_CASE_ = tokenizer(__magic_name__ , return_token_type_ids=__magic_name__ ) for key in encoded_tok.keys(): self.assertListEqual(encoded_tok[key] , encoded_processor[key] ) def __A ( self : Dict ) -> Union[str, Any]: SCREAMING_SNAKE_CASE_ = self.get_image_processor() SCREAMING_SNAKE_CASE_ = self.get_tokenizer() SCREAMING_SNAKE_CASE_ = BlipaProcessor(tokenizer=__magic_name__ , image_processor=__magic_name__ ) SCREAMING_SNAKE_CASE_ = "lower newer" SCREAMING_SNAKE_CASE_ = self.prepare_image_inputs() SCREAMING_SNAKE_CASE_ = processor(text=__magic_name__ , images=__magic_name__ ) self.assertListEqual(list(inputs.keys() ) , ["pixel_values", "input_ids", "attention_mask"] ) # test if it raises when no input is passed with pytest.raises(__magic_name__ ): processor() def __A ( self : Dict ) -> Tuple: SCREAMING_SNAKE_CASE_ = self.get_image_processor() SCREAMING_SNAKE_CASE_ = self.get_tokenizer() SCREAMING_SNAKE_CASE_ = BlipaProcessor(tokenizer=__magic_name__ , image_processor=__magic_name__ ) SCREAMING_SNAKE_CASE_ = [[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]] SCREAMING_SNAKE_CASE_ = processor.batch_decode(__magic_name__ ) SCREAMING_SNAKE_CASE_ = tokenizer.batch_decode(__magic_name__ ) self.assertListEqual(__magic_name__ , __magic_name__ ) def __A ( self : List[str] ) -> int: SCREAMING_SNAKE_CASE_ = self.get_image_processor() SCREAMING_SNAKE_CASE_ = self.get_tokenizer() SCREAMING_SNAKE_CASE_ = BlipaProcessor(tokenizer=__magic_name__ , image_processor=__magic_name__ ) SCREAMING_SNAKE_CASE_ = "lower newer" SCREAMING_SNAKE_CASE_ = self.prepare_image_inputs() SCREAMING_SNAKE_CASE_ = processor(text=__magic_name__ , images=__magic_name__ ) # For now the processor supports only ['pixel_values', 'input_ids', 'attention_mask'] self.assertListEqual(list(inputs.keys() ) , ["pixel_values", "input_ids", "attention_mask"] )
305
0
def a__ ( __UpperCamelCase , __UpperCamelCase ): if a < 0 or b < 0: raise ValueError("the value of both inputs must be positive" ) SCREAMING_SNAKE_CASE_ = str(bin(__UpperCamelCase ) )[2:] # remove the leading "0b" SCREAMING_SNAKE_CASE_ = str(bin(__UpperCamelCase ) )[2:] # remove the leading "0b" SCREAMING_SNAKE_CASE_ = max(len(__UpperCamelCase ) , len(__UpperCamelCase ) ) return "0b" + "".join( str(int(char_a == "1" and char_b == "1" ) ) for char_a, char_b in zip(a_binary.zfill(__UpperCamelCase ) , b_binary.zfill(__UpperCamelCase ) ) ) if __name__ == "__main__": import doctest doctest.testmod()
359
from typing import TYPE_CHECKING from ...file_utils import _LazyModule, is_tokenizers_available, is_torch_available, is_vision_available from ...utils import OptionalDependencyNotAvailable A : List[Any] = {"configuration_dpt": ["DPT_PRETRAINED_CONFIG_ARCHIVE_MAP", "DPTConfig"]} try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: A : Optional[Any] = ["DPTFeatureExtractor"] A : str = ["DPTImageProcessor"] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: A : Optional[Any] = [ "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 A : Optional[int] = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
305
0
def a__ ( __UpperCamelCase ): if not isinstance(__lowerCamelCase , __lowerCamelCase ): raise TypeError("only integers accepted as input" ) else: SCREAMING_SNAKE_CASE_ = str(abs(__lowerCamelCase ) ) SCREAMING_SNAKE_CASE_ = [list(__lowerCamelCase ) for char in range(len(__lowerCamelCase ) )] for index in range(len(__lowerCamelCase ) ): num_transpositions[index].pop(__lowerCamelCase ) return max( int("".join(list(__lowerCamelCase ) ) ) for transposition in num_transpositions ) if __name__ == "__main__": __import__("doctest").testmod()
360
from __future__ import annotations import collections import pprint from pathlib import Path def a__ ( __UpperCamelCase ): return "".join(sorted(__UpperCamelCase ) ) def a__ ( __UpperCamelCase ): return word_by_signature[signature(__UpperCamelCase )] A : str = Path(__file__).parent.joinpath("words.txt").read_text(encoding="utf-8") A : int = sorted({word.strip().lower() for word in data.splitlines()}) A : Tuple = collections.defaultdict(list) for word in word_list: word_by_signature[signature(word)].append(word) if __name__ == "__main__": A : Union[str, Any] = {word: anagram(word) for word in word_list if len(anagram(word)) > 1} with open("anagrams.txt", "w") as file: file.write("all_anagrams = \n ") file.write(pprint.pformat(all_anagrams))
305
0
from ...configuration_utils import PretrainedConfig from ...utils import logging A : List[str] = logging.get_logger(__name__) A : Union[str, Any] = { "RWKV/rwkv-4-169m-pile": "https://huggingface.co/RWKV/rwkv-4-169m-pile/resolve/main/config.json", "RWKV/rwkv-4-430m-pile": "https://huggingface.co/RWKV/rwkv-4-430m-pile/resolve/main/config.json", "RWKV/rwkv-4-1b5-pile": "https://huggingface.co/RWKV/rwkv-4-1b5-pile/resolve/main/config.json", "RWKV/rwkv-4-3b-pile": "https://huggingface.co/RWKV/rwkv-4-3b-pile/resolve/main/config.json", "RWKV/rwkv-4-7b-pile": "https://huggingface.co/RWKV/rwkv-4-7b-pile/resolve/main/config.json", "RWKV/rwkv-4-14b-pile": "https://huggingface.co/RWKV/rwkv-4-14b-pile/resolve/main/config.json", "RWKV/rwkv-raven-1b5": "https://huggingface.co/RWKV/rwkv-raven-1b5/resolve/main/config.json", "RWKV/rwkv-raven-3b": "https://huggingface.co/RWKV/rwkv-raven-3b/resolve/main/config.json", "RWKV/rwkv-raven-7b": "https://huggingface.co/RWKV/rwkv-raven-7b/resolve/main/config.json", "RWKV/rwkv-raven-14b": "https://huggingface.co/RWKV/rwkv-raven-14b/resolve/main/config.json", } class lowerCamelCase (__SCREAMING_SNAKE_CASE ): """simple docstring""" lowerCamelCase__ = 'rwkv' lowerCamelCase__ = {'max_position_embeddings': 'context_length'} def __init__( self : str , __magic_name__ : str=50_277 , __magic_name__ : Dict=1_024 , __magic_name__ : List[str]=4_096 , __magic_name__ : Tuple=32 , __magic_name__ : Any=None , __magic_name__ : int=None , __magic_name__ : Dict=1e-5 , __magic_name__ : List[str]=0 , __magic_name__ : List[str]=0 , __magic_name__ : Dict=6 , __magic_name__ : Optional[int]=False , __magic_name__ : List[Any]=True , **__magic_name__ : str , ) -> Any: """simple docstring""" SCREAMING_SNAKE_CASE_ = vocab_size SCREAMING_SNAKE_CASE_ = context_length SCREAMING_SNAKE_CASE_ = hidden_size SCREAMING_SNAKE_CASE_ = num_hidden_layers SCREAMING_SNAKE_CASE_ = attention_hidden_size if attention_hidden_size is not None else hidden_size SCREAMING_SNAKE_CASE_ = intermediate_size if intermediate_size is not None else 4 * hidden_size SCREAMING_SNAKE_CASE_ = layer_norm_epsilon SCREAMING_SNAKE_CASE_ = rescale_every SCREAMING_SNAKE_CASE_ = use_cache SCREAMING_SNAKE_CASE_ = bos_token_id SCREAMING_SNAKE_CASE_ = eos_token_id super().__init__( tie_word_embeddings=_a , bos_token_id=_a , eos_token_id=_a , **_a )
361
import copy import os from typing import TYPE_CHECKING, List, Union if TYPE_CHECKING: pass from ...configuration_utils import PretrainedConfig from ...utils import logging A : int = logging.get_logger(__name__) A : str = { "kakaobrain/align-base": "https://huggingface.co/kakaobrain/align-base/resolve/main/config.json", } class lowerCamelCase (SCREAMING_SNAKE_CASE__ ): """simple docstring""" lowerCamelCase__ = '''align_text_model''' def __init__( self : Optional[Any] , __magic_name__ : Union[str, Any]=30_522 , __magic_name__ : Tuple=768 , __magic_name__ : List[str]=12 , __magic_name__ : Optional[Any]=12 , __magic_name__ : str=3_072 , __magic_name__ : Dict="gelu" , __magic_name__ : Any=0.1 , __magic_name__ : Optional[int]=0.1 , __magic_name__ : List[str]=512 , __magic_name__ : Any=2 , __magic_name__ : Optional[Any]=0.02 , __magic_name__ : int=1e-12 , __magic_name__ : str=0 , __magic_name__ : Optional[Any]="absolute" , __magic_name__ : Optional[Any]=True , **__magic_name__ : Tuple , ) -> Union[str, Any]: super().__init__(**__magic_name__ ) 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_ = position_embedding_type SCREAMING_SNAKE_CASE_ = use_cache SCREAMING_SNAKE_CASE_ = pad_token_id @classmethod def __A ( cls : Any , __magic_name__ : Union[str, os.PathLike] , **__magic_name__ : Optional[Any] ) -> "PretrainedConfig": cls._set_token_in_kwargs(__magic_name__ ) SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = cls.get_config_dict(__magic_name__ , **__magic_name__ ) # get the text config dict if we are loading from AlignConfig if config_dict.get("model_type" ) == "align": SCREAMING_SNAKE_CASE_ = config_dict["text_config"] if "model_type" in config_dict and hasattr(cls , "model_type" ) and config_dict["model_type"] != cls.model_type: logger.warning( F'''You are using a model of type {config_dict["model_type"]} to instantiate a model of type ''' F'''{cls.model_type}. This is not supported for all configurations of models and can yield errors.''' ) return cls.from_dict(__magic_name__ , **__magic_name__ ) class lowerCamelCase (SCREAMING_SNAKE_CASE__ ): """simple docstring""" lowerCamelCase__ = '''align_vision_model''' def __init__( self : List[str] , __magic_name__ : int = 3 , __magic_name__ : int = 600 , __magic_name__ : float = 2.0 , __magic_name__ : float = 3.1 , __magic_name__ : int = 8 , __magic_name__ : List[int] = [3, 3, 5, 3, 5, 5, 3] , __magic_name__ : List[int] = [32, 16, 24, 40, 80, 112, 192] , __magic_name__ : List[int] = [16, 24, 40, 80, 112, 192, 320] , __magic_name__ : List[int] = [] , __magic_name__ : List[int] = [1, 2, 2, 2, 1, 2, 1] , __magic_name__ : List[int] = [1, 2, 2, 3, 3, 4, 1] , __magic_name__ : List[int] = [1, 6, 6, 6, 6, 6, 6] , __magic_name__ : float = 0.25 , __magic_name__ : str = "swish" , __magic_name__ : int = 2_560 , __magic_name__ : str = "mean" , __magic_name__ : float = 0.02 , __magic_name__ : float = 0.001 , __magic_name__ : float = 0.99 , __magic_name__ : float = 0.2 , **__magic_name__ : List[Any] , ) -> Tuple: super().__init__(**__magic_name__ ) SCREAMING_SNAKE_CASE_ = num_channels SCREAMING_SNAKE_CASE_ = image_size SCREAMING_SNAKE_CASE_ = width_coefficient SCREAMING_SNAKE_CASE_ = depth_coefficient SCREAMING_SNAKE_CASE_ = depth_divisor SCREAMING_SNAKE_CASE_ = kernel_sizes SCREAMING_SNAKE_CASE_ = in_channels SCREAMING_SNAKE_CASE_ = out_channels SCREAMING_SNAKE_CASE_ = depthwise_padding SCREAMING_SNAKE_CASE_ = strides SCREAMING_SNAKE_CASE_ = num_block_repeats SCREAMING_SNAKE_CASE_ = expand_ratios SCREAMING_SNAKE_CASE_ = squeeze_expansion_ratio SCREAMING_SNAKE_CASE_ = hidden_act SCREAMING_SNAKE_CASE_ = hidden_dim SCREAMING_SNAKE_CASE_ = pooling_type SCREAMING_SNAKE_CASE_ = initializer_range SCREAMING_SNAKE_CASE_ = batch_norm_eps SCREAMING_SNAKE_CASE_ = batch_norm_momentum SCREAMING_SNAKE_CASE_ = drop_connect_rate SCREAMING_SNAKE_CASE_ = sum(__magic_name__ ) * 4 @classmethod def __A ( cls : List[str] , __magic_name__ : Union[str, os.PathLike] , **__magic_name__ : Dict ) -> "PretrainedConfig": cls._set_token_in_kwargs(__magic_name__ ) SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = cls.get_config_dict(__magic_name__ , **__magic_name__ ) # get the vision config dict if we are loading from AlignConfig if config_dict.get("model_type" ) == "align": SCREAMING_SNAKE_CASE_ = 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(__magic_name__ , **__magic_name__ ) class lowerCamelCase (SCREAMING_SNAKE_CASE__ ): """simple docstring""" lowerCamelCase__ = '''align''' lowerCamelCase__ = True def __init__( self : Optional[Any] , __magic_name__ : Dict=None , __magic_name__ : List[Any]=None , __magic_name__ : str=640 , __magic_name__ : Any=1.0 , __magic_name__ : Dict=0.02 , **__magic_name__ : Union[str, Any] , ) -> int: super().__init__(**__magic_name__ ) if text_config is None: SCREAMING_SNAKE_CASE_ = {} logger.info("text_config is None. Initializing the AlignTextConfig with default values." ) if vision_config is None: SCREAMING_SNAKE_CASE_ = {} logger.info("vision_config is None. Initializing the AlignVisionConfig with default values." ) SCREAMING_SNAKE_CASE_ = AlignTextConfig(**__magic_name__ ) SCREAMING_SNAKE_CASE_ = AlignVisionConfig(**__magic_name__ ) SCREAMING_SNAKE_CASE_ = projection_dim SCREAMING_SNAKE_CASE_ = temperature_init_value SCREAMING_SNAKE_CASE_ = initializer_range @classmethod def __A ( cls : List[str] , __magic_name__ : AlignTextConfig , __magic_name__ : AlignVisionConfig , **__magic_name__ : Tuple ) -> Any: return cls(text_config=text_config.to_dict() , vision_config=vision_config.to_dict() , **__magic_name__ ) def __A ( self : int ) -> Union[str, Any]: SCREAMING_SNAKE_CASE_ = copy.deepcopy(self.__dict__ ) SCREAMING_SNAKE_CASE_ = self.text_config.to_dict() SCREAMING_SNAKE_CASE_ = self.vision_config.to_dict() SCREAMING_SNAKE_CASE_ = self.__class__.model_type return output
305
0
"""simple docstring""" from typing import Callable, List, Optional, Tuple, Union import torch from transformers import CLIPTextModel, CLIPTokenizer from ...configuration_utils import ConfigMixin, register_to_config from ...models import ModelMixin, TransformeraDModel, VQModel from ...schedulers import VQDiffusionScheduler from ...utils import logging from ..pipeline_utils import DiffusionPipeline, ImagePipelineOutput A : Any = logging.get_logger(__name__) # pylint: disable=invalid-name class lowerCamelCase (SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ): """simple docstring""" @register_to_config def __init__( self : Any , __magic_name__ : int , __magic_name__ : Dict = None , __magic_name__ : Dict = None ) -> int: super().__init__() SCREAMING_SNAKE_CASE_ = learnable if self.learnable: assert hidden_size is not None, "learnable=True requires `hidden_size` to be set" assert length is not None, "learnable=True requires `length` to be set" SCREAMING_SNAKE_CASE_ = torch.zeros(__magic_name__ , __magic_name__ ) else: SCREAMING_SNAKE_CASE_ = None SCREAMING_SNAKE_CASE_ = torch.nn.Parameter(__magic_name__ ) class lowerCamelCase (SCREAMING_SNAKE_CASE__ ): """simple docstring""" lowerCamelCase__ = 4_2 lowerCamelCase__ = 4_2 lowerCamelCase__ = 4_2 lowerCamelCase__ = 4_2 lowerCamelCase__ = 4_2 lowerCamelCase__ = 4_2 def __init__( self : Optional[int] , __magic_name__ : Optional[int] , __magic_name__ : str , __magic_name__ : List[Any] , __magic_name__ : Tuple , __magic_name__ : List[str] , __magic_name__ : List[Any] , ) -> str: super().__init__() self.register_modules( vqvae=__magic_name__ , transformer=__magic_name__ , text_encoder=__magic_name__ , tokenizer=__magic_name__ , scheduler=__magic_name__ , learned_classifier_free_sampling_embeddings=__magic_name__ , ) def __A ( self : Union[str, Any] , __magic_name__ : int , __magic_name__ : Dict , __magic_name__ : Union[str, Any] ) -> Tuple: SCREAMING_SNAKE_CASE_ = len(__magic_name__ ) if isinstance(__magic_name__ , __magic_name__ ) else 1 # get prompt text embeddings SCREAMING_SNAKE_CASE_ = self.tokenizer( __magic_name__ , padding="max_length" , max_length=self.tokenizer.model_max_length , return_tensors="pt" , ) SCREAMING_SNAKE_CASE_ = text_inputs.input_ids if text_input_ids.shape[-1] > self.tokenizer.model_max_length: SCREAMING_SNAKE_CASE_ = self.tokenizer.batch_decode(text_input_ids[:, self.tokenizer.model_max_length :] ) logger.warning( "The following part of your input was truncated because CLIP can only handle sequences up to" F''' {self.tokenizer.model_max_length} tokens: {removed_text}''' ) SCREAMING_SNAKE_CASE_ = text_input_ids[:, : self.tokenizer.model_max_length] SCREAMING_SNAKE_CASE_ = self.text_encoder(text_input_ids.to(self.device ) )[0] # NOTE: This additional step of normalizing the text embeddings is from VQ-Diffusion. # While CLIP does normalize the pooled output of the text transformer when combining # the image and text embeddings, CLIP does not directly normalize the last hidden state. # # CLIP normalizing the pooled output. # https://github.com/huggingface/transformers/blob/d92e22d1f28324f513f3080e5c47c071a3916721/src/transformers/models/clip/modeling_clip.py#L1052-L1053 SCREAMING_SNAKE_CASE_ = prompt_embeds / prompt_embeds.norm(dim=-1 , keepdim=__magic_name__ ) # duplicate text embeddings for each generation per prompt SCREAMING_SNAKE_CASE_ = prompt_embeds.repeat_interleave(__magic_name__ , dim=0 ) if do_classifier_free_guidance: if self.learned_classifier_free_sampling_embeddings.learnable: SCREAMING_SNAKE_CASE_ = self.learned_classifier_free_sampling_embeddings.embeddings SCREAMING_SNAKE_CASE_ = negative_prompt_embeds.unsqueeze(0 ).repeat(__magic_name__ , 1 , 1 ) else: SCREAMING_SNAKE_CASE_ = [''] * batch_size SCREAMING_SNAKE_CASE_ = text_input_ids.shape[-1] SCREAMING_SNAKE_CASE_ = self.tokenizer( __magic_name__ , padding="max_length" , max_length=__magic_name__ , truncation=__magic_name__ , return_tensors="pt" , ) SCREAMING_SNAKE_CASE_ = self.text_encoder(uncond_input.input_ids.to(self.device ) )[0] # See comment for normalizing text embeddings SCREAMING_SNAKE_CASE_ = negative_prompt_embeds / negative_prompt_embeds.norm(dim=-1 , keepdim=__magic_name__ ) # duplicate unconditional embeddings for each generation per prompt, using mps friendly method SCREAMING_SNAKE_CASE_ = negative_prompt_embeds.shape[1] SCREAMING_SNAKE_CASE_ = negative_prompt_embeds.repeat(1 , __magic_name__ , 1 ) SCREAMING_SNAKE_CASE_ = negative_prompt_embeds.view(batch_size * num_images_per_prompt , __magic_name__ , -1 ) # For classifier free guidance, we need to do two forward passes. # Here we concatenate the unconditional and text embeddings into a single batch # to avoid doing two forward passes SCREAMING_SNAKE_CASE_ = torch.cat([negative_prompt_embeds, prompt_embeds] ) return prompt_embeds @torch.no_grad() def __call__( self : Any , __magic_name__ : int , __magic_name__ : Optional[int] = 100 , __magic_name__ : Union[str, Any] = 5.0 , __magic_name__ : Any = 1.0 , __magic_name__ : str = 1 , __magic_name__ : int = None , __magic_name__ : Optional[Any] = None , __magic_name__ : Any = "pil" , __magic_name__ : List[str] = True , __magic_name__ : List[Any] = None , __magic_name__ : Optional[int] = 1 , ) -> Union[str, Any]: if isinstance(__magic_name__ , __magic_name__ ): SCREAMING_SNAKE_CASE_ = 1 elif isinstance(__magic_name__ , __magic_name__ ): SCREAMING_SNAKE_CASE_ = len(__magic_name__ ) else: raise ValueError(F'''`prompt` has to be of type `str` or `list` but is {type(__magic_name__ )}''' ) SCREAMING_SNAKE_CASE_ = batch_size * num_images_per_prompt SCREAMING_SNAKE_CASE_ = guidance_scale > 1.0 SCREAMING_SNAKE_CASE_ = self._encode_prompt(__magic_name__ , __magic_name__ , __magic_name__ ) if (callback_steps is None) or ( callback_steps is not None and (not isinstance(__magic_name__ , __magic_name__ ) or callback_steps <= 0) ): raise ValueError( F'''`callback_steps` has to be a positive integer but is {callback_steps} of type''' F''' {type(__magic_name__ )}.''' ) # get the initial completely masked latents unless the user supplied it SCREAMING_SNAKE_CASE_ = (batch_size, self.transformer.num_latent_pixels) if latents is None: SCREAMING_SNAKE_CASE_ = self.transformer.num_vector_embeds - 1 SCREAMING_SNAKE_CASE_ = torch.full(__magic_name__ , __magic_name__ ).to(self.device ) else: if latents.shape != latents_shape: raise ValueError(F'''Unexpected latents shape, got {latents.shape}, expected {latents_shape}''' ) if (latents < 0).any() or (latents >= self.transformer.num_vector_embeds).any(): raise ValueError( "Unexpected latents value(s). All latents be valid embedding indices i.e. in the range 0," F''' {self.transformer.num_vector_embeds - 1} (inclusive).''' ) SCREAMING_SNAKE_CASE_ = latents.to(self.device ) # set timesteps self.scheduler.set_timesteps(__magic_name__ , device=self.device ) SCREAMING_SNAKE_CASE_ = self.scheduler.timesteps.to(self.device ) SCREAMING_SNAKE_CASE_ = latents for i, t in enumerate(self.progress_bar(__magic_name__ ) ): # expand the sample if we are doing classifier free guidance SCREAMING_SNAKE_CASE_ = torch.cat([sample] * 2 ) if do_classifier_free_guidance else sample # predict the un-noised image # model_output == `log_p_x_0` SCREAMING_SNAKE_CASE_ = self.transformer(__magic_name__ , encoder_hidden_states=__magic_name__ , timestep=__magic_name__ ).sample if do_classifier_free_guidance: SCREAMING_SNAKE_CASE_ = model_output.chunk(2 ) SCREAMING_SNAKE_CASE_ = model_output_uncond + guidance_scale * (model_output_text - model_output_uncond) model_output -= torch.logsumexp(__magic_name__ , dim=1 , keepdim=__magic_name__ ) SCREAMING_SNAKE_CASE_ = self.truncate(__magic_name__ , __magic_name__ ) # remove `log(0)`'s (`-inf`s) SCREAMING_SNAKE_CASE_ = model_output.clamp(-70 ) # compute the previous noisy sample x_t -> x_t-1 SCREAMING_SNAKE_CASE_ = self.scheduler.step(__magic_name__ , timestep=__magic_name__ , sample=__magic_name__ , generator=__magic_name__ ).prev_sample # call the callback, if provided if callback is not None and i % callback_steps == 0: callback(__magic_name__ , __magic_name__ , __magic_name__ ) SCREAMING_SNAKE_CASE_ = self.vqvae.config.vq_embed_dim SCREAMING_SNAKE_CASE_ = (batch_size, self.transformer.height, self.transformer.width, embedding_channels) SCREAMING_SNAKE_CASE_ = self.vqvae.quantize.get_codebook_entry(__magic_name__ , shape=__magic_name__ ) SCREAMING_SNAKE_CASE_ = self.vqvae.decode(__magic_name__ , force_not_quantize=__magic_name__ ).sample SCREAMING_SNAKE_CASE_ = (image / 2 + 0.5).clamp(0 , 1 ) SCREAMING_SNAKE_CASE_ = image.cpu().permute(0 , 2 , 3 , 1 ).numpy() if output_type == "pil": SCREAMING_SNAKE_CASE_ = self.numpy_to_pil(__magic_name__ ) if not return_dict: return (image,) return ImagePipelineOutput(images=__magic_name__ ) def __A ( self : List[Any] , __magic_name__ : Optional[int] , __magic_name__ : Dict ) -> List[str]: SCREAMING_SNAKE_CASE_ = torch.sort(__magic_name__ , 1 , descending=__magic_name__ ) SCREAMING_SNAKE_CASE_ = torch.exp(__magic_name__ ) SCREAMING_SNAKE_CASE_ = sorted_p_x_0.cumsum(dim=1 ) < truncation_rate # Ensure that at least the largest probability is not zeroed out SCREAMING_SNAKE_CASE_ = torch.full_like(keep_mask[:, 0:1, :] , __magic_name__ ) SCREAMING_SNAKE_CASE_ = torch.cat((all_true, keep_mask) , dim=1 ) SCREAMING_SNAKE_CASE_ = keep_mask[:, :-1, :] SCREAMING_SNAKE_CASE_ = keep_mask.gather(1 , indices.argsort(1 ) ) SCREAMING_SNAKE_CASE_ = log_p_x_0.clone() SCREAMING_SNAKE_CASE_ = -torch.inf # -inf = log(0) return rv
362
import unittest from transformers.testing_utils import CaptureStdout from transformers.tools.python_interpreter import evaluate def a__ ( __UpperCamelCase ): return x + 2 class lowerCamelCase (unittest.TestCase ): """simple docstring""" def __A ( self : List[Any] ) -> int: SCREAMING_SNAKE_CASE_ = "x = 3" SCREAMING_SNAKE_CASE_ = {} SCREAMING_SNAKE_CASE_ = evaluate(__magic_name__ , {} , state=__magic_name__ ) assert result == 3 self.assertDictEqual(__magic_name__ , {"x": 3} ) SCREAMING_SNAKE_CASE_ = "x = y" SCREAMING_SNAKE_CASE_ = {"y": 5} SCREAMING_SNAKE_CASE_ = evaluate(__magic_name__ , {} , state=__magic_name__ ) # evaluate returns the value of the last assignment. assert result == 5 self.assertDictEqual(__magic_name__ , {"x": 5, "y": 5} ) def __A ( self : Union[str, Any] ) -> str: SCREAMING_SNAKE_CASE_ = "y = add_two(x)" SCREAMING_SNAKE_CASE_ = {"x": 3} SCREAMING_SNAKE_CASE_ = evaluate(__magic_name__ , {"add_two": add_two} , state=__magic_name__ ) assert result == 5 self.assertDictEqual(__magic_name__ , {"x": 3, "y": 5} ) # Won't work without the tool with CaptureStdout() as out: SCREAMING_SNAKE_CASE_ = evaluate(__magic_name__ , {} , state=__magic_name__ ) assert result is None assert "tried to execute add_two" in out.out def __A ( self : List[str] ) -> int: SCREAMING_SNAKE_CASE_ = "x = 3" SCREAMING_SNAKE_CASE_ = {} SCREAMING_SNAKE_CASE_ = evaluate(__magic_name__ , {} , state=__magic_name__ ) assert result == 3 self.assertDictEqual(__magic_name__ , {"x": 3} ) def __A ( self : Optional[Any] ) -> str: SCREAMING_SNAKE_CASE_ = "test_dict = {'x': x, 'y': add_two(x)}" SCREAMING_SNAKE_CASE_ = {"x": 3} SCREAMING_SNAKE_CASE_ = evaluate(__magic_name__ , {"add_two": add_two} , state=__magic_name__ ) self.assertDictEqual(__magic_name__ , {"x": 3, "y": 5} ) self.assertDictEqual(__magic_name__ , {"x": 3, "test_dict": {"x": 3, "y": 5}} ) def __A ( self : Optional[int] ) -> List[str]: SCREAMING_SNAKE_CASE_ = "x = 3\ny = 5" SCREAMING_SNAKE_CASE_ = {} SCREAMING_SNAKE_CASE_ = evaluate(__magic_name__ , {} , state=__magic_name__ ) # evaluate returns the value of the last assignment. assert result == 5 self.assertDictEqual(__magic_name__ , {"x": 3, "y": 5} ) def __A ( self : Any ) -> List[str]: SCREAMING_SNAKE_CASE_ = "text = f'This is x: {x}.'" SCREAMING_SNAKE_CASE_ = {"x": 3} SCREAMING_SNAKE_CASE_ = evaluate(__magic_name__ , {} , state=__magic_name__ ) # evaluate returns the value of the last assignment. assert result == "This is x: 3." self.assertDictEqual(__magic_name__ , {"x": 3, "text": "This is x: 3."} ) def __A ( self : int ) -> Tuple: SCREAMING_SNAKE_CASE_ = "if x <= 3:\n y = 2\nelse:\n y = 5" SCREAMING_SNAKE_CASE_ = {"x": 3} SCREAMING_SNAKE_CASE_ = evaluate(__magic_name__ , {} , state=__magic_name__ ) # evaluate returns the value of the last assignment. assert result == 2 self.assertDictEqual(__magic_name__ , {"x": 3, "y": 2} ) SCREAMING_SNAKE_CASE_ = {"x": 8} SCREAMING_SNAKE_CASE_ = evaluate(__magic_name__ , {} , state=__magic_name__ ) # evaluate returns the value of the last assignment. assert result == 5 self.assertDictEqual(__magic_name__ , {"x": 8, "y": 5} ) def __A ( self : str ) -> str: SCREAMING_SNAKE_CASE_ = "test_list = [x, add_two(x)]" SCREAMING_SNAKE_CASE_ = {"x": 3} SCREAMING_SNAKE_CASE_ = evaluate(__magic_name__ , {"add_two": add_two} , state=__magic_name__ ) self.assertListEqual(__magic_name__ , [3, 5] ) self.assertDictEqual(__magic_name__ , {"x": 3, "test_list": [3, 5]} ) def __A ( self : Union[str, Any] ) -> List[Any]: SCREAMING_SNAKE_CASE_ = "y = x" SCREAMING_SNAKE_CASE_ = {"x": 3} SCREAMING_SNAKE_CASE_ = evaluate(__magic_name__ , {} , state=__magic_name__ ) assert result == 3 self.assertDictEqual(__magic_name__ , {"x": 3, "y": 3} ) def __A ( self : Tuple ) -> List[Any]: SCREAMING_SNAKE_CASE_ = "test_list = [x, add_two(x)]\ntest_list[1]" SCREAMING_SNAKE_CASE_ = {"x": 3} SCREAMING_SNAKE_CASE_ = evaluate(__magic_name__ , {"add_two": add_two} , state=__magic_name__ ) assert result == 5 self.assertDictEqual(__magic_name__ , {"x": 3, "test_list": [3, 5]} ) SCREAMING_SNAKE_CASE_ = "test_dict = {'x': x, 'y': add_two(x)}\ntest_dict['y']" SCREAMING_SNAKE_CASE_ = {"x": 3} SCREAMING_SNAKE_CASE_ = evaluate(__magic_name__ , {"add_two": add_two} , state=__magic_name__ ) assert result == 5 self.assertDictEqual(__magic_name__ , {"x": 3, "test_dict": {"x": 3, "y": 5}} ) def __A ( self : Tuple ) -> Any: SCREAMING_SNAKE_CASE_ = "x = 0\nfor i in range(3):\n x = i" SCREAMING_SNAKE_CASE_ = {} SCREAMING_SNAKE_CASE_ = evaluate(__magic_name__ , {"range": range} , state=__magic_name__ ) assert result == 2 self.assertDictEqual(__magic_name__ , {"x": 2, "i": 2} )
305
0
import pickle import shutil import tempfile import unittest from transformers import SPIECE_UNDERLINE, XGLMTokenizer, XGLMTokenizerFast from transformers.testing_utils import get_tests_dir, require_sentencepiece, require_tokenizers, slow from transformers.utils import cached_property from ...test_tokenization_common import TokenizerTesterMixin A : str = get_tests_dir("fixtures/test_sentencepiece.model") @require_sentencepiece @require_tokenizers class lowerCamelCase (__lowercase , unittest.TestCase ): """simple docstring""" lowerCamelCase__ = XGLMTokenizer lowerCamelCase__ = XGLMTokenizerFast lowerCamelCase__ = True lowerCamelCase__ = True def __A ( self : List[str] ) -> Union[str, Any]: super().setUp() # We have a SentencePiece fixture for testing SCREAMING_SNAKE_CASE_ = XGLMTokenizer(__magic_name__ , keep_accents=__magic_name__ ) tokenizer.save_pretrained(self.tmpdirname ) def __A ( self : Union[str, Any] ) -> List[Any]: SCREAMING_SNAKE_CASE_ = """<pad>""" SCREAMING_SNAKE_CASE_ = 1 self.assertEqual(self.get_tokenizer()._convert_token_to_id(__magic_name__ ) , __magic_name__ ) self.assertEqual(self.get_tokenizer()._convert_id_to_token(__magic_name__ ) , __magic_name__ ) def __A ( self : Any ) -> Tuple: SCREAMING_SNAKE_CASE_ = list(self.get_tokenizer().get_vocab().keys() ) self.assertEqual(vocab_keys[0] , "<s>" ) self.assertEqual(vocab_keys[1] , "<pad>" ) self.assertEqual(len(__magic_name__ ) , 1_008 ) def __A ( self : Optional[int] ) -> Optional[int]: self.assertEqual(self.get_tokenizer().vocab_size , 1_008 ) def __A ( self : int ) -> List[str]: SCREAMING_SNAKE_CASE_ = XGLMTokenizer(__magic_name__ , keep_accents=__magic_name__ ) SCREAMING_SNAKE_CASE_ = tokenizer.tokenize("This is a test" ) self.assertListEqual(__magic_name__ , ["▁This", "▁is", "▁a", "▁t", "est"] ) self.assertListEqual( tokenizer.convert_tokens_to_ids(__magic_name__ ) , [value + tokenizer.fairseq_offset for value in [285, 46, 10, 170, 382]] , ) SCREAMING_SNAKE_CASE_ = tokenizer.tokenize("I was born in 92000, and this is falsé." ) self.assertListEqual( __magic_name__ , [ SPIECE_UNDERLINE + "I", SPIECE_UNDERLINE + "was", SPIECE_UNDERLINE + "b", "or", "n", SPIECE_UNDERLINE + "in", SPIECE_UNDERLINE + "", "9", "2", "0", "0", "0", ",", SPIECE_UNDERLINE + "and", SPIECE_UNDERLINE + "this", SPIECE_UNDERLINE + "is", SPIECE_UNDERLINE + "f", "al", "s", "é", ".", ] , ) SCREAMING_SNAKE_CASE_ = tokenizer.convert_tokens_to_ids(__magic_name__ ) self.assertListEqual( __magic_name__ , [ value + tokenizer.fairseq_offset for value in [8, 21, 84, 55, 24, 19, 7, 2, 602, 347, 347, 347, 3, 12, 66, 46, 72, 80, 6, 2, 4] ] , ) SCREAMING_SNAKE_CASE_ = tokenizer.convert_ids_to_tokens(__magic_name__ ) self.assertListEqual( __magic_name__ , [ SPIECE_UNDERLINE + "I", SPIECE_UNDERLINE + "was", SPIECE_UNDERLINE + "b", "or", "n", SPIECE_UNDERLINE + "in", SPIECE_UNDERLINE + "", "<unk>", "2", "0", "0", "0", ",", SPIECE_UNDERLINE + "and", SPIECE_UNDERLINE + "this", SPIECE_UNDERLINE + "is", SPIECE_UNDERLINE + "f", "al", "s", "<unk>", ".", ] , ) @cached_property def __A ( self : str ) -> Any: return XGLMTokenizer.from_pretrained("facebook/xglm-564M" ) def __A ( self : List[str] ) -> Any: with tempfile.NamedTemporaryFile() as f: shutil.copyfile(__magic_name__ , f.name ) SCREAMING_SNAKE_CASE_ = XGLMTokenizer(f.name , keep_accents=__magic_name__ ) SCREAMING_SNAKE_CASE_ = pickle.dumps(__magic_name__ ) pickle.loads(__magic_name__ ) def __A ( self : Optional[int] ) -> str: if not self.test_rust_tokenizer: return SCREAMING_SNAKE_CASE_ = self.get_tokenizer() SCREAMING_SNAKE_CASE_ = self.get_rust_tokenizer() SCREAMING_SNAKE_CASE_ = """I was born in 92000, and this is falsé.""" SCREAMING_SNAKE_CASE_ = tokenizer.tokenize(__magic_name__ ) SCREAMING_SNAKE_CASE_ = rust_tokenizer.tokenize(__magic_name__ ) self.assertListEqual(__magic_name__ , __magic_name__ ) SCREAMING_SNAKE_CASE_ = tokenizer.encode(__magic_name__ , add_special_tokens=__magic_name__ ) SCREAMING_SNAKE_CASE_ = rust_tokenizer.encode(__magic_name__ , add_special_tokens=__magic_name__ ) self.assertListEqual(__magic_name__ , __magic_name__ ) SCREAMING_SNAKE_CASE_ = self.get_rust_tokenizer() SCREAMING_SNAKE_CASE_ = tokenizer.encode(__magic_name__ ) SCREAMING_SNAKE_CASE_ = rust_tokenizer.encode(__magic_name__ ) self.assertListEqual(__magic_name__ , __magic_name__ ) @slow def __A ( self : List[Any] ) -> List[str]: SCREAMING_SNAKE_CASE_ = """Hello World!""" SCREAMING_SNAKE_CASE_ = [2, 31_227, 4_447, 35] self.assertListEqual(__magic_name__ , self.big_tokenizer.encode(__magic_name__ ) ) @slow def __A ( self : str ) -> Dict: SCREAMING_SNAKE_CASE_ = ( """This is a very long text with a lot of weird characters, such as: . , ~ ? ( ) \" [ ] ! : - . Also we will""" """ add words that should not exsist and be tokenized to unk, such as saoneuhaoesuth""" ) # fmt: off SCREAMING_SNAKE_CASE_ = [2, 1_018, 67, 11, 1_988, 2_617, 5_631, 278, 11, 3_407, 48, 71_630, 28_085, 4, 3_234, 157, 13, 6, 5, 6, 4, 3_526, 768, 15, 659, 57, 298, 3_983, 864, 129, 21, 6, 5, 13_675, 377, 652, 7_580, 10_341, 155, 2_817, 422, 1_666, 7, 1_674, 53, 113, 202_277, 17_892, 33, 60, 87, 4, 3_234, 157, 61, 2_667, 52_376, 19, 88, 23, 735] # fmt: on self.assertListEqual(__magic_name__ , self.big_tokenizer.encode(__magic_name__ ) ) @slow def __A ( self : Union[str, Any] ) -> Dict: SCREAMING_SNAKE_CASE_ = { """input_ids""": [[2, 108_825, 1_163, 15, 88_010, 473, 15_898, 157, 13_672, 1_857, 312, 8, 238_021, 1_163, 53, 13_672, 1_857, 312, 8, 53_283, 182_396, 8, 18_566, 16, 36_733, 4_101, 8, 230, 244_017, 122_553, 7, 15, 132_597, 4, 293, 12_511, 7_610, 4, 3_414, 132_597, 9, 4, 32_361, 362, 4, 734, 28_512, 32_569, 18, 4, 32_361, 26_096, 14_982, 73, 18_715, 21_433, 235_261, 15, 492, 12_427, 16, 53, 18_715, 21_433, 65_454, 15, 23_659, 563, 16, 278, 597, 2_843, 595, 7_931, 182_396, 64_186, 22, 886, 595, 132_981, 53, 25_540, 3_449, 43_982, 39_901, 5_951, 878, 330, 4, 27_694, 80_269, 312, 53, 6_517, 11_780, 611, 20_408, 5], [2, 6, 132_597, 67, 42_897, 33, 592, 8, 163_729, 25_540, 361, 136_997, 109_514, 173_230, 7, 501, 60, 102_913, 196, 5_631, 235, 63_243, 473, 6, 231_757, 74, 5_277, 7_905, 53, 3_095, 37_317, 22, 454, 183_874, 5], [2, 268, 31_298, 46_530, 6, 132_935, 43_831, 7, 597, 32, 24, 3_688, 9_865, 5]], """attention_mask""": [[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]] } # noqa: E501 # fmt: on self.tokenizer_integration_test_util( expected_encoding=__magic_name__ , model_name="facebook/xglm-564M" , padding=__magic_name__ , )
363
import numpy as np import pandas as pd from sklearn.preprocessing import Normalizer from sklearn.svm import SVR from statsmodels.tsa.statespace.sarimax import SARIMAX def a__ ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase ): SCREAMING_SNAKE_CASE_ = np.array([[1, item, train_mtch[i]] for i, item in enumerate(__UpperCamelCase )] ) SCREAMING_SNAKE_CASE_ = np.array(__UpperCamelCase ) SCREAMING_SNAKE_CASE_ = np.dot(np.dot(np.linalg.inv(np.dot(x.transpose() , __UpperCamelCase ) ) , x.transpose() ) , __UpperCamelCase ) return abs(beta[0] + test_dt[0] * beta[1] + test_mtch[0] + beta[2] ) def a__ ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase ): SCREAMING_SNAKE_CASE_ = (1, 2, 1) SCREAMING_SNAKE_CASE_ = (1, 1, 0, 7) SCREAMING_SNAKE_CASE_ = SARIMAX( __UpperCamelCase , exog=__UpperCamelCase , order=__UpperCamelCase , seasonal_order=__UpperCamelCase ) SCREAMING_SNAKE_CASE_ = model.fit(disp=__UpperCamelCase , maxiter=6_0_0 , method="nm" ) SCREAMING_SNAKE_CASE_ = model_fit.predict(1 , len(__UpperCamelCase ) , exog=[test_match] ) return result[0] def a__ ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase ): SCREAMING_SNAKE_CASE_ = SVR(kernel="rbf" , C=1 , gamma=0.1 , epsilon=0.1 ) regressor.fit(__UpperCamelCase , __UpperCamelCase ) SCREAMING_SNAKE_CASE_ = regressor.predict(__UpperCamelCase ) return y_pred[0] def a__ ( __UpperCamelCase ): train_user.sort() SCREAMING_SNAKE_CASE_ = np.percentile(__UpperCamelCase , 2_5 ) SCREAMING_SNAKE_CASE_ = np.percentile(__UpperCamelCase , 7_5 ) SCREAMING_SNAKE_CASE_ = qa - qa SCREAMING_SNAKE_CASE_ = qa - (iqr * 0.1) return low_lim def a__ ( __UpperCamelCase , __UpperCamelCase ): SCREAMING_SNAKE_CASE_ = 0 SCREAMING_SNAKE_CASE_ = 0 for i in list_vote: if i > actual_result: SCREAMING_SNAKE_CASE_ = not_safe + 1 else: if abs(abs(__UpperCamelCase ) - abs(__UpperCamelCase ) ) <= 0.1: safe += 1 else: not_safe += 1 return safe > not_safe if __name__ == "__main__": # data_input_df = pd.read_csv("ex_data.csv", header=None) A : Dict = [[1_82_31, 0.0, 1], [2_26_21, 1.0, 2], [1_56_75, 0.0, 3], [2_35_83, 1.0, 4]] A : Optional[Any] = pd.DataFrame( data_input, columns=["total_user", "total_even", "days"] ) A : Union[str, Any] = Normalizer().fit_transform(data_input_df.values) # split data A : Optional[int] = normalize_df[:, 2].tolist() A : List[str] = normalize_df[:, 0].tolist() A : int = normalize_df[:, 1].tolist() # for svr (input variable = total date and total match) A : int = normalize_df[:, [1, 2]].tolist() A : Tuple = x[: len(x) - 1] A : str = x[len(x) - 1 :] # for linear regression & sarimax A : Tuple = total_date[: len(total_date) - 1] A : Optional[int] = total_user[: len(total_user) - 1] A : str = total_match[: len(total_match) - 1] A : List[Any] = total_date[len(total_date) - 1 :] A : List[Any] = total_user[len(total_user) - 1 :] A : Optional[Any] = total_match[len(total_match) - 1 :] # voting system with forecasting A : Optional[int] = [ linear_regression_prediction( trn_date, trn_user, trn_match, tst_date, tst_match ), sarimax_predictor(trn_user, trn_match, tst_match), support_vector_regressor(x_train, x_test, trn_user), ] # check the safety of today's data A : str = "" if data_safety_checker(res_vote, tst_user) else "not " print("Today's data is {not_str}safe.")
305
0
import argparse import torch from safetensors.torch import load_file from diffusers import StableDiffusionPipeline def a__ ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase ): # load base model SCREAMING_SNAKE_CASE_ = StableDiffusionPipeline.from_pretrained(a_ , torch_dtype=torch.floataa ) # load LoRA weight from .safetensors SCREAMING_SNAKE_CASE_ = load_file(a_ ) SCREAMING_SNAKE_CASE_ = [] # directly update weight in diffusers model for key in state_dict: # it is suggested to print out the key, it usually will be something like below # "lora_te_text_model_encoder_layers_0_self_attn_k_proj.lora_down.weight" # as we have set the alpha beforehand, so just skip if ".alpha" in key or key in visited: continue if "text" in key: SCREAMING_SNAKE_CASE_ = key.split("." )[0].split(LORA_PREFIX_TEXT_ENCODER + "_" )[-1].split("_" ) SCREAMING_SNAKE_CASE_ = pipeline.text_encoder else: SCREAMING_SNAKE_CASE_ = key.split("." )[0].split(LORA_PREFIX_UNET + "_" )[-1].split("_" ) SCREAMING_SNAKE_CASE_ = pipeline.unet # find the target layer SCREAMING_SNAKE_CASE_ = layer_infos.pop(0 ) while len(a_ ) > -1: try: SCREAMING_SNAKE_CASE_ = curr_layer.__getattr__(a_ ) if len(a_ ) > 0: SCREAMING_SNAKE_CASE_ = layer_infos.pop(0 ) elif len(a_ ) == 0: break except Exception: if len(a_ ) > 0: temp_name += "_" + layer_infos.pop(0 ) else: SCREAMING_SNAKE_CASE_ = layer_infos.pop(0 ) SCREAMING_SNAKE_CASE_ = [] if "lora_down" in key: pair_keys.append(key.replace("lora_down" , "lora_up" ) ) pair_keys.append(a_ ) else: pair_keys.append(a_ ) pair_keys.append(key.replace("lora_up" , "lora_down" ) ) # update weight if len(state_dict[pair_keys[0]].shape ) == 4: SCREAMING_SNAKE_CASE_ = state_dict[pair_keys[0]].squeeze(3 ).squeeze(2 ).to(torch.floataa ) SCREAMING_SNAKE_CASE_ = state_dict[pair_keys[1]].squeeze(3 ).squeeze(2 ).to(torch.floataa ) curr_layer.weight.data += alpha * torch.mm(a_ , a_ ).unsqueeze(2 ).unsqueeze(3 ) else: SCREAMING_SNAKE_CASE_ = state_dict[pair_keys[0]].to(torch.floataa ) SCREAMING_SNAKE_CASE_ = state_dict[pair_keys[1]].to(torch.floataa ) curr_layer.weight.data += alpha * torch.mm(a_ , a_ ) # update visited list for item in pair_keys: visited.append(a_ ) return pipeline if __name__ == "__main__": A : Optional[Any] = argparse.ArgumentParser() parser.add_argument( "--base_model_path", default=None, type=str, required=True, help="Path to the base model in diffusers format." ) parser.add_argument( "--checkpoint_path", default=None, type=str, required=True, help="Path to the checkpoint to convert." ) parser.add_argument("--dump_path", default=None, type=str, required=True, help="Path to the output model.") parser.add_argument( "--lora_prefix_unet", default="lora_unet", type=str, help="The prefix of UNet weight in safetensors" ) parser.add_argument( "--lora_prefix_text_encoder", default="lora_te", type=str, help="The prefix of text encoder weight in safetensors", ) parser.add_argument("--alpha", default=0.75, type=float, help="The merging ratio in W = W0 + alpha * deltaW") parser.add_argument( "--to_safetensors", action="store_true", help="Whether to store pipeline in safetensors format or not." ) parser.add_argument("--device", type=str, help="Device to use (e.g. cpu, cuda:0, cuda:1, etc.)") A : int = parser.parse_args() A : Optional[Any] = args.base_model_path A : Optional[int] = args.checkpoint_path A : Optional[int] = args.dump_path A : Tuple = args.lora_prefix_unet A : List[str] = args.lora_prefix_text_encoder A : int = args.alpha A : str = convert(base_model_path, checkpoint_path, lora_prefix_unet, lora_prefix_text_encoder, alpha) A : Tuple = pipe.to(args.device) pipe.save_pretrained(args.dump_path, safe_serialization=args.to_safetensors)
364
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tf_available, is_torch_available A : List[str] = {"configuration_swin": ["SWIN_PRETRAINED_CONFIG_ARCHIVE_MAP", "SwinConfig", "SwinOnnxConfig"]} try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: A : Any = [ "SWIN_PRETRAINED_MODEL_ARCHIVE_LIST", "SwinForImageClassification", "SwinForMaskedImageModeling", "SwinModel", "SwinPreTrainedModel", "SwinBackbone", ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: A : str = [ "TF_SWIN_PRETRAINED_MODEL_ARCHIVE_LIST", "TFSwinForImageClassification", "TFSwinForMaskedImageModeling", "TFSwinModel", "TFSwinPreTrainedModel", ] if TYPE_CHECKING: from .configuration_swin import SWIN_PRETRAINED_CONFIG_ARCHIVE_MAP, SwinConfig, SwinOnnxConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_swin import ( SWIN_PRETRAINED_MODEL_ARCHIVE_LIST, SwinBackbone, SwinForImageClassification, SwinForMaskedImageModeling, SwinModel, SwinPreTrainedModel, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_swin import ( TF_SWIN_PRETRAINED_MODEL_ARCHIVE_LIST, TFSwinForImageClassification, TFSwinForMaskedImageModeling, TFSwinModel, TFSwinPreTrainedModel, ) else: import sys A : Tuple = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
305
0
"""simple docstring""" import warnings from ...utils import logging from .image_processing_deformable_detr import DeformableDetrImageProcessor A : Tuple = logging.get_logger(__name__) class lowerCamelCase (__lowercase ): """simple docstring""" def __init__( self : Dict , *__magic_name__ : str , **__magic_name__ : List[str] ) -> None: warnings.warn( "The class DeformableDetrFeatureExtractor is deprecated and will be removed in version 5 of Transformers." " Please use DeformableDetrImageProcessor instead." , _a , ) super().__init__(*_a , **_a )
365
import tempfile import unittest import numpy as np from huggingface_hub import HfFolder, delete_repo from requests.exceptions import HTTPError from transformers import BertConfig, is_flax_available from transformers.testing_utils import TOKEN, USER, is_staging_test, require_flax if is_flax_available(): import os from flax.core.frozen_dict import unfreeze from flax.traverse_util import flatten_dict from transformers import FlaxBertModel A : Union[str, Any] = "0.12" # assumed parallelism: 8 @require_flax @is_staging_test class lowerCamelCase (unittest.TestCase ): """simple docstring""" @classmethod def __A ( cls : Any ) -> Dict: SCREAMING_SNAKE_CASE_ = TOKEN HfFolder.save_token(__magic_name__ ) @classmethod def __A ( cls : Optional[int] ) -> Tuple: try: delete_repo(token=cls._token , repo_id="test-model-flax" ) except HTTPError: pass try: delete_repo(token=cls._token , repo_id="valid_org/test-model-flax-org" ) except HTTPError: pass def __A ( self : str ) -> str: SCREAMING_SNAKE_CASE_ = BertConfig( vocab_size=99 , hidden_size=32 , num_hidden_layers=5 , num_attention_heads=4 , intermediate_size=37 ) SCREAMING_SNAKE_CASE_ = FlaxBertModel(__magic_name__ ) model.push_to_hub("test-model-flax" , use_auth_token=self._token ) SCREAMING_SNAKE_CASE_ = FlaxBertModel.from_pretrained(F'''{USER}/test-model-flax''' ) SCREAMING_SNAKE_CASE_ = flatten_dict(unfreeze(model.params ) ) SCREAMING_SNAKE_CASE_ = flatten_dict(unfreeze(new_model.params ) ) for key in base_params.keys(): SCREAMING_SNAKE_CASE_ = (base_params[key] - new_params[key]).sum().item() self.assertLessEqual(__magic_name__ , 1e-3 , msg=F'''{key} not identical''' ) # Reset repo delete_repo(token=self._token , repo_id="test-model-flax" ) # Push to hub via save_pretrained with tempfile.TemporaryDirectory() as tmp_dir: model.save_pretrained(__magic_name__ , repo_id="test-model-flax" , push_to_hub=__magic_name__ , use_auth_token=self._token ) SCREAMING_SNAKE_CASE_ = FlaxBertModel.from_pretrained(F'''{USER}/test-model-flax''' ) SCREAMING_SNAKE_CASE_ = flatten_dict(unfreeze(model.params ) ) SCREAMING_SNAKE_CASE_ = flatten_dict(unfreeze(new_model.params ) ) for key in base_params.keys(): SCREAMING_SNAKE_CASE_ = (base_params[key] - new_params[key]).sum().item() self.assertLessEqual(__magic_name__ , 1e-3 , msg=F'''{key} not identical''' ) def __A ( self : int ) -> Tuple: SCREAMING_SNAKE_CASE_ = BertConfig( vocab_size=99 , hidden_size=32 , num_hidden_layers=5 , num_attention_heads=4 , intermediate_size=37 ) SCREAMING_SNAKE_CASE_ = FlaxBertModel(__magic_name__ ) model.push_to_hub("valid_org/test-model-flax-org" , use_auth_token=self._token ) SCREAMING_SNAKE_CASE_ = FlaxBertModel.from_pretrained("valid_org/test-model-flax-org" ) SCREAMING_SNAKE_CASE_ = flatten_dict(unfreeze(model.params ) ) SCREAMING_SNAKE_CASE_ = flatten_dict(unfreeze(new_model.params ) ) for key in base_params.keys(): SCREAMING_SNAKE_CASE_ = (base_params[key] - new_params[key]).sum().item() self.assertLessEqual(__magic_name__ , 1e-3 , msg=F'''{key} not identical''' ) # Reset repo delete_repo(token=self._token , repo_id="valid_org/test-model-flax-org" ) # Push to hub via save_pretrained with tempfile.TemporaryDirectory() as tmp_dir: model.save_pretrained( __magic_name__ , repo_id="valid_org/test-model-flax-org" , push_to_hub=__magic_name__ , use_auth_token=self._token ) SCREAMING_SNAKE_CASE_ = FlaxBertModel.from_pretrained("valid_org/test-model-flax-org" ) SCREAMING_SNAKE_CASE_ = flatten_dict(unfreeze(model.params ) ) SCREAMING_SNAKE_CASE_ = flatten_dict(unfreeze(new_model.params ) ) for key in base_params.keys(): SCREAMING_SNAKE_CASE_ = (base_params[key] - new_params[key]).sum().item() self.assertLessEqual(__magic_name__ , 1e-3 , msg=F'''{key} not identical''' ) def a__ ( __UpperCamelCase , __UpperCamelCase ): SCREAMING_SNAKE_CASE_ = True SCREAMING_SNAKE_CASE_ = flatten_dict(modela.params ) SCREAMING_SNAKE_CASE_ = flatten_dict(modela.params ) for key in flat_params_a.keys(): if np.sum(np.abs(flat_params_a[key] - flat_params_a[key] ) ) > 1E-4: SCREAMING_SNAKE_CASE_ = False return models_are_equal @require_flax class lowerCamelCase (unittest.TestCase ): """simple docstring""" def __A ( self : str ) -> Dict: SCREAMING_SNAKE_CASE_ = BertConfig.from_pretrained("hf-internal-testing/tiny-bert-flax-only" ) SCREAMING_SNAKE_CASE_ = FlaxBertModel(__magic_name__ ) SCREAMING_SNAKE_CASE_ = "bert" with tempfile.TemporaryDirectory() as tmp_dir: model.save_pretrained(os.path.join(__magic_name__ , __magic_name__ ) ) with self.assertRaises(__magic_name__ ): SCREAMING_SNAKE_CASE_ = FlaxBertModel.from_pretrained(__magic_name__ ) SCREAMING_SNAKE_CASE_ = FlaxBertModel.from_pretrained(__magic_name__ , subfolder=__magic_name__ ) self.assertTrue(check_models_equal(__magic_name__ , __magic_name__ ) ) def __A ( self : Optional[Any] ) -> Tuple: SCREAMING_SNAKE_CASE_ = BertConfig.from_pretrained("hf-internal-testing/tiny-bert-flax-only" ) SCREAMING_SNAKE_CASE_ = FlaxBertModel(__magic_name__ ) SCREAMING_SNAKE_CASE_ = "bert" with tempfile.TemporaryDirectory() as tmp_dir: model.save_pretrained(os.path.join(__magic_name__ , __magic_name__ ) , max_shard_size="10KB" ) with self.assertRaises(__magic_name__ ): SCREAMING_SNAKE_CASE_ = FlaxBertModel.from_pretrained(__magic_name__ ) SCREAMING_SNAKE_CASE_ = FlaxBertModel.from_pretrained(__magic_name__ , subfolder=__magic_name__ ) self.assertTrue(check_models_equal(__magic_name__ , __magic_name__ ) ) def __A ( self : Optional[int] ) -> Dict: SCREAMING_SNAKE_CASE_ = "bert" SCREAMING_SNAKE_CASE_ = "hf-internal-testing/tiny-random-bert-subfolder" with self.assertRaises(__magic_name__ ): SCREAMING_SNAKE_CASE_ = FlaxBertModel.from_pretrained(__magic_name__ ) SCREAMING_SNAKE_CASE_ = FlaxBertModel.from_pretrained(__magic_name__ , subfolder=__magic_name__ ) self.assertIsNotNone(__magic_name__ ) def __A ( self : List[str] ) -> Dict: SCREAMING_SNAKE_CASE_ = "bert" SCREAMING_SNAKE_CASE_ = "hf-internal-testing/tiny-random-bert-sharded-subfolder" with self.assertRaises(__magic_name__ ): SCREAMING_SNAKE_CASE_ = FlaxBertModel.from_pretrained(__magic_name__ ) SCREAMING_SNAKE_CASE_ = FlaxBertModel.from_pretrained(__magic_name__ , subfolder=__magic_name__ ) self.assertIsNotNone(__magic_name__ )
305
0
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available A : Optional[int] = {"configuration_sew": ["SEW_PRETRAINED_CONFIG_ARCHIVE_MAP", "SEWConfig"]} try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: A : List[str] = [ "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 A : List[Any] = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
366
# Copyright 2021 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. import argparse import os from accelerate.utils import ComputeEnvironment from .cluster import get_cluster_input from .config_args import cache_dir, default_config_file, default_yaml_config_file, load_config_from_file # noqa: F401 from .config_utils import _ask_field, _ask_options, _convert_compute_environment # noqa: F401 from .sagemaker import get_sagemaker_input A : Union[str, Any] = "Launches a series of prompts to create and save a `default_config.yaml` configuration file for your training system. Should always be ran first on your machine" def a__ ( ): SCREAMING_SNAKE_CASE_ = _ask_options( "In which compute environment are you running?" , ["This machine", "AWS (Amazon SageMaker)"] , _convert_compute_environment , ) if compute_environment == ComputeEnvironment.AMAZON_SAGEMAKER: SCREAMING_SNAKE_CASE_ = get_sagemaker_input() else: SCREAMING_SNAKE_CASE_ = get_cluster_input() return config def a__ ( __UpperCamelCase=None ): if subparsers is not None: SCREAMING_SNAKE_CASE_ = subparsers.add_parser("config" , description=__UpperCamelCase ) else: SCREAMING_SNAKE_CASE_ = argparse.ArgumentParser("Accelerate config command" , description=__UpperCamelCase ) parser.add_argument( "--config_file" , default=__UpperCamelCase , help=( "The path to use to store the config file. Will default to a file named default_config.yaml in the cache " "location, which is the content of the environment `HF_HOME` suffixed with 'accelerate', or if you don't have " "such an environment variable, your cache directory ('~/.cache' or the content of `XDG_CACHE_HOME`) suffixed " "with 'huggingface'." ) , ) if subparsers is not None: parser.set_defaults(func=__UpperCamelCase ) return parser def a__ ( __UpperCamelCase ): SCREAMING_SNAKE_CASE_ = get_user_input() if args.config_file is not None: SCREAMING_SNAKE_CASE_ = args.config_file else: if not os.path.isdir(__UpperCamelCase ): os.makedirs(__UpperCamelCase ) SCREAMING_SNAKE_CASE_ = default_yaml_config_file if config_file.endswith(".json" ): config.to_json_file(__UpperCamelCase ) else: config.to_yaml_file(__UpperCamelCase ) print(F'''accelerate configuration saved at {config_file}''' ) def a__ ( ): SCREAMING_SNAKE_CASE_ = config_command_parser() SCREAMING_SNAKE_CASE_ = parser.parse_args() config_command(__UpperCamelCase ) if __name__ == "__main__": main()
305
0
from __future__ import annotations from math import pow, sqrt def a__ ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase ): if (resistance, reactance, impedance).count(0 ) != 1: raise ValueError("One and only one argument must be 0" ) if resistance == 0: return {"resistance": sqrt(pow(a_ , 2 ) - pow(a_ , 2 ) )} elif reactance == 0: return {"reactance": sqrt(pow(a_ , 2 ) - pow(a_ , 2 ) )} elif impedance == 0: return {"impedance": sqrt(pow(a_ , 2 ) + pow(a_ , 2 ) )} else: raise ValueError("Exactly one argument must be 0" ) if __name__ == "__main__": import doctest doctest.testmod()
367
from dataclasses import dataclass, field from typing import ClassVar, Dict from ..features import Features, Value from .base import TaskTemplate @dataclass(frozen=SCREAMING_SNAKE_CASE__ ) class lowerCamelCase (SCREAMING_SNAKE_CASE__ ): """simple docstring""" lowerCamelCase__ = field(default='''summarization''' , metadata={'''include_in_asdict_even_if_is_default''': True} ) lowerCamelCase__ = Features({'''text''': Value('''string''' )} ) lowerCamelCase__ = Features({'''summary''': Value('''string''' )} ) lowerCamelCase__ = "text" lowerCamelCase__ = "summary" @property def __A ( self : Dict ) -> Dict[str, str]: return {self.text_column: "text", self.summary_column: "summary"}
305
0
import gc import random import unittest import numpy as np import torch from transformers import XLMRobertaTokenizer from diffusers import ( AltDiffusionImgaImgPipeline, AutoencoderKL, PNDMScheduler, UNetaDConditionModel, ) from diffusers.image_processor import VaeImageProcessor from diffusers.pipelines.alt_diffusion.modeling_roberta_series import ( RobertaSeriesConfig, RobertaSeriesModelWithTransformation, ) from diffusers.utils import floats_tensor, load_image, load_numpy, slow, torch_device from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu enable_full_determinism() class lowerCamelCase (unittest.TestCase ): """simple docstring""" def __A ( self : Optional[Any] ) -> Optional[int]: # clean up the VRAM after each test super().tearDown() gc.collect() torch.cuda.empty_cache() @property def __A ( self : Optional[int] ) -> Dict: SCREAMING_SNAKE_CASE_ = 1 SCREAMING_SNAKE_CASE_ = 3 SCREAMING_SNAKE_CASE_ = (32, 32) SCREAMING_SNAKE_CASE_ = floats_tensor((batch_size, num_channels) + sizes , rng=random.Random(0 ) ).to(_SCREAMING_SNAKE_CASE ) return image @property def __A ( self : Optional[int] ) -> Tuple: 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 , ) return model @property def __A ( self : Optional[int] ) -> Union[str, Any]: 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 , ) return model @property def __A ( self : Dict ) -> str: 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 , pad_token_id=1 , vocab_size=5_006 , ) return RobertaSeriesModelWithTransformation(_SCREAMING_SNAKE_CASE ) @property def __A ( self : str ) -> str: def extract(*__magic_name__ : List[Any] , **__magic_name__ : Dict ): class lowerCamelCase : """simple docstring""" def __init__( self : Optional[int] ) -> str: SCREAMING_SNAKE_CASE_ = torch.ones([0] ) def __A ( self : Optional[Any] , __magic_name__ : str ) -> Tuple: self.pixel_values.to(_SCREAMING_SNAKE_CASE ) return self return Out() return extract def __A ( self : Any ) -> Tuple: SCREAMING_SNAKE_CASE_ = "cpu" # ensure determinism for the device-dependent torch.Generator SCREAMING_SNAKE_CASE_ = self.dummy_cond_unet SCREAMING_SNAKE_CASE_ = PNDMScheduler(skip_prk_steps=_SCREAMING_SNAKE_CASE ) SCREAMING_SNAKE_CASE_ = self.dummy_vae SCREAMING_SNAKE_CASE_ = self.dummy_text_encoder SCREAMING_SNAKE_CASE_ = XLMRobertaTokenizer.from_pretrained("hf-internal-testing/tiny-xlm-roberta" ) SCREAMING_SNAKE_CASE_ = 77 SCREAMING_SNAKE_CASE_ = self.dummy_image.to(_SCREAMING_SNAKE_CASE ) SCREAMING_SNAKE_CASE_ = init_image / 2 + 0.5 # make sure here that pndm scheduler skips prk SCREAMING_SNAKE_CASE_ = AltDiffusionImgaImgPipeline( unet=_SCREAMING_SNAKE_CASE , scheduler=_SCREAMING_SNAKE_CASE , vae=_SCREAMING_SNAKE_CASE , text_encoder=_SCREAMING_SNAKE_CASE , tokenizer=_SCREAMING_SNAKE_CASE , safety_checker=_SCREAMING_SNAKE_CASE , feature_extractor=self.dummy_extractor , ) SCREAMING_SNAKE_CASE_ = VaeImageProcessor(vae_scale_factor=alt_pipe.vae_scale_factor , do_normalize=_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.Generator(device=_SCREAMING_SNAKE_CASE ).manual_seed(0 ) SCREAMING_SNAKE_CASE_ = alt_pipe( [prompt] , generator=_SCREAMING_SNAKE_CASE , guidance_scale=6.0 , num_inference_steps=2 , output_type="np" , image=_SCREAMING_SNAKE_CASE , ) SCREAMING_SNAKE_CASE_ = output.images SCREAMING_SNAKE_CASE_ = torch.Generator(device=_SCREAMING_SNAKE_CASE ).manual_seed(0 ) SCREAMING_SNAKE_CASE_ = alt_pipe( [prompt] , generator=_SCREAMING_SNAKE_CASE , guidance_scale=6.0 , num_inference_steps=2 , output_type="np" , image=_SCREAMING_SNAKE_CASE , return_dict=_SCREAMING_SNAKE_CASE , )[0] SCREAMING_SNAKE_CASE_ = image[0, -3:, -3:, -1] SCREAMING_SNAKE_CASE_ = image_from_tuple[0, -3:, -3:, -1] assert image.shape == (1, 32, 32, 3) SCREAMING_SNAKE_CASE_ = np.array([0.4427, 0.3731, 0.4249, 0.4941, 0.4546, 0.4148, 0.4193, 0.4666, 0.4499] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 5e-3 assert np.abs(image_from_tuple_slice.flatten() - expected_slice ).max() < 5e-3 @unittest.skipIf(torch_device != "cuda" , "This test requires a GPU" ) def __A ( self : Optional[Any] ) -> Union[str, Any]: SCREAMING_SNAKE_CASE_ = self.dummy_cond_unet SCREAMING_SNAKE_CASE_ = PNDMScheduler(skip_prk_steps=_SCREAMING_SNAKE_CASE ) SCREAMING_SNAKE_CASE_ = self.dummy_vae SCREAMING_SNAKE_CASE_ = self.dummy_text_encoder SCREAMING_SNAKE_CASE_ = XLMRobertaTokenizer.from_pretrained("hf-internal-testing/tiny-xlm-roberta" ) SCREAMING_SNAKE_CASE_ = 77 SCREAMING_SNAKE_CASE_ = self.dummy_image.to(_SCREAMING_SNAKE_CASE ) # put models in fp16 SCREAMING_SNAKE_CASE_ = unet.half() SCREAMING_SNAKE_CASE_ = vae.half() SCREAMING_SNAKE_CASE_ = bert.half() # make sure here that pndm scheduler skips prk SCREAMING_SNAKE_CASE_ = AltDiffusionImgaImgPipeline( unet=_SCREAMING_SNAKE_CASE , scheduler=_SCREAMING_SNAKE_CASE , vae=_SCREAMING_SNAKE_CASE , text_encoder=_SCREAMING_SNAKE_CASE , tokenizer=_SCREAMING_SNAKE_CASE , safety_checker=_SCREAMING_SNAKE_CASE , feature_extractor=self.dummy_extractor , ) SCREAMING_SNAKE_CASE_ = VaeImageProcessor(vae_scale_factor=alt_pipe.vae_scale_factor , do_normalize=_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="np" , image=_SCREAMING_SNAKE_CASE , ).images assert image.shape == (1, 32, 32, 3) @unittest.skipIf(torch_device != "cuda" , "This test requires a GPU" ) def __A ( self : List[str] ) -> str: SCREAMING_SNAKE_CASE_ = load_image( "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main" "/img2img/sketch-mountains-input.jpg" ) # resize to resolution that is divisible by 8 but not 16 or 32 SCREAMING_SNAKE_CASE_ = init_image.resize((760, 504) ) SCREAMING_SNAKE_CASE_ = "BAAI/AltDiffusion" SCREAMING_SNAKE_CASE_ = AltDiffusionImgaImgPipeline.from_pretrained( _SCREAMING_SNAKE_CASE , safety_checker=_SCREAMING_SNAKE_CASE , ) pipe.to(_SCREAMING_SNAKE_CASE ) pipe.set_progress_bar_config(disable=_SCREAMING_SNAKE_CASE ) pipe.enable_attention_slicing() SCREAMING_SNAKE_CASE_ = "A fantasy landscape, trending on artstation" SCREAMING_SNAKE_CASE_ = torch.manual_seed(0 ) SCREAMING_SNAKE_CASE_ = pipe( prompt=_SCREAMING_SNAKE_CASE , image=_SCREAMING_SNAKE_CASE , strength=0.75 , guidance_scale=7.5 , generator=_SCREAMING_SNAKE_CASE , output_type="np" , ) SCREAMING_SNAKE_CASE_ = output.images[0] SCREAMING_SNAKE_CASE_ = image[255:258, 383:386, -1] assert image.shape == (504, 760, 3) SCREAMING_SNAKE_CASE_ = np.array([0.9358, 0.9397, 0.9599, 0.9901, 1.0000, 1.0000, 0.9882, 1.0000, 1.0000] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-2 @slow @require_torch_gpu class lowerCamelCase (unittest.TestCase ): """simple docstring""" def __A ( self : int ) -> Tuple: # clean up the VRAM after each test super().tearDown() gc.collect() torch.cuda.empty_cache() def __A ( self : Union[str, Any] ) -> str: SCREAMING_SNAKE_CASE_ = load_image( "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main" "/img2img/sketch-mountains-input.jpg" ) SCREAMING_SNAKE_CASE_ = init_image.resize((768, 512) ) SCREAMING_SNAKE_CASE_ = load_numpy( "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/img2img/fantasy_landscape_alt.npy" ) SCREAMING_SNAKE_CASE_ = "BAAI/AltDiffusion" SCREAMING_SNAKE_CASE_ = AltDiffusionImgaImgPipeline.from_pretrained( _SCREAMING_SNAKE_CASE , safety_checker=_SCREAMING_SNAKE_CASE , ) pipe.to(_SCREAMING_SNAKE_CASE ) pipe.set_progress_bar_config(disable=_SCREAMING_SNAKE_CASE ) pipe.enable_attention_slicing() SCREAMING_SNAKE_CASE_ = "A fantasy landscape, trending on artstation" SCREAMING_SNAKE_CASE_ = torch.manual_seed(0 ) SCREAMING_SNAKE_CASE_ = pipe( prompt=_SCREAMING_SNAKE_CASE , image=_SCREAMING_SNAKE_CASE , strength=0.75 , guidance_scale=7.5 , generator=_SCREAMING_SNAKE_CASE , output_type="np" , ) SCREAMING_SNAKE_CASE_ = output.images[0] assert image.shape == (512, 768, 3) # img2img is flaky across GPUs even in fp32, so using MAE here assert np.abs(expected_image - image ).max() < 1e-2
368
from ....utils import logging A : List[str] = logging.get_logger(__name__) class lowerCamelCase (SCREAMING_SNAKE_CASE__ ): """simple docstring""" def __init__( self : List[str] , __magic_name__ : Optional[Any] , __magic_name__ : Any=None , __magic_name__ : List[str]=2_048 ) -> List[Any]: SCREAMING_SNAKE_CASE_ = config.__dict__ SCREAMING_SNAKE_CASE_ = modal_hidden_size if num_labels: SCREAMING_SNAKE_CASE_ = num_labels
305
0
import os import pytest import yaml from datasets.features.features import Features, Value from datasets.info import DatasetInfo, DatasetInfosDict @pytest.mark.parametrize( "files" , [ ["full:README.md", "dataset_infos.json"], ["empty:README.md", "dataset_infos.json"], ["dataset_infos.json"], ["full:README.md"], ] , ) def a__ ( __UpperCamelCase , __UpperCamelCase ): SCREAMING_SNAKE_CASE_ = tmp_path_factory.mktemp("dset_infos_dir" ) if "full:README.md" in files: with open(dataset_infos_dir / "README.md" , "w" ) as f: f.write("---\ndataset_info:\n dataset_size: 42\n---" ) if "empty:README.md" in files: with open(dataset_infos_dir / "README.md" , "w" ) as f: f.write("" ) # we want to support dataset_infos.json for backward compatibility if "dataset_infos.json" in files: with open(dataset_infos_dir / "dataset_infos.json" , "w" ) as f: f.write("{\"default\": {\"dataset_size\": 42}}" ) SCREAMING_SNAKE_CASE_ = DatasetInfosDict.from_directory(a__ ) assert dataset_infos assert dataset_infos["default"].dataset_size == 4_2 @pytest.mark.parametrize( "dataset_info" , [ DatasetInfo(), DatasetInfo( description="foo" , features=Features({"a": Value("int32" )} ) , builder_name="builder" , config_name="config" , version="1.0.0" , splits=[{"name": "train"}] , download_size=4_2 , ), ] , ) def a__ ( __UpperCamelCase , __UpperCamelCase ): SCREAMING_SNAKE_CASE_ = str(a__ ) dataset_info.write_to_directory(a__ ) SCREAMING_SNAKE_CASE_ = DatasetInfo.from_directory(a__ ) assert dataset_info == reloaded assert os.path.exists(os.path.join(a__ , "dataset_info.json" ) ) def a__ ( ): SCREAMING_SNAKE_CASE_ = DatasetInfo( description="foo" , citation="bar" , homepage="https://foo.bar" , license="CC0" , features=Features({"a": Value("int32" )} ) , post_processed={} , supervised_keys=() , task_templates=[] , builder_name="builder" , config_name="config" , version="1.0.0" , splits=[{"name": "train", "num_examples": 4_2}] , download_checksums={} , download_size=1_3_3_7 , post_processing_size=4_4_2 , dataset_size=1_2_3_4 , size_in_bytes=1_3_3_7 + 4_4_2 + 1_2_3_4 , ) SCREAMING_SNAKE_CASE_ = dataset_info._to_yaml_dict() assert sorted(a__ ) == sorted(DatasetInfo._INCLUDED_INFO_IN_YAML ) for key in DatasetInfo._INCLUDED_INFO_IN_YAML: assert key in dataset_info_yaml_dict assert isinstance(dataset_info_yaml_dict[key] , (list, dict, int, str) ) SCREAMING_SNAKE_CASE_ = yaml.safe_dump(a__ ) SCREAMING_SNAKE_CASE_ = yaml.safe_load(a__ ) assert dataset_info_yaml_dict == reloaded def a__ ( ): SCREAMING_SNAKE_CASE_ = DatasetInfo() SCREAMING_SNAKE_CASE_ = dataset_info._to_yaml_dict() assert dataset_info_yaml_dict == {} @pytest.mark.parametrize( "dataset_infos_dict" , [ DatasetInfosDict(), DatasetInfosDict({"default": DatasetInfo()} ), DatasetInfosDict({"my_config_name": DatasetInfo()} ), DatasetInfosDict( { "default": DatasetInfo( description="foo" , features=Features({"a": Value("int32" )} ) , builder_name="builder" , config_name="config" , version="1.0.0" , splits=[{"name": "train"}] , download_size=4_2 , ) } ), DatasetInfosDict( { "v1": DatasetInfo(dataset_size=4_2 ), "v2": DatasetInfo(dataset_size=1_3_3_7 ), } ), ] , ) def a__ ( __UpperCamelCase , __UpperCamelCase ): SCREAMING_SNAKE_CASE_ = str(a__ ) dataset_infos_dict.write_to_directory(a__ ) SCREAMING_SNAKE_CASE_ = DatasetInfosDict.from_directory(a__ ) # the config_name of the dataset_infos_dict take over the attribute for config_name, dataset_info in dataset_infos_dict.items(): SCREAMING_SNAKE_CASE_ = config_name # the yaml representation doesn't include fields like description or citation # so we just test that we can recover what we can from the yaml SCREAMING_SNAKE_CASE_ = DatasetInfo._from_yaml_dict(dataset_info._to_yaml_dict() ) assert dataset_infos_dict == reloaded if dataset_infos_dict: assert os.path.exists(os.path.join(a__ , "README.md" ) )
369
import warnings from typing import List, Optional, Union from ...processing_utils import ProcessorMixin from ...tokenization_utils_base import BatchEncoding, PaddingStrategy, PreTokenizedInput, TextInput, TruncationStrategy from ...utils import TensorType class lowerCamelCase (SCREAMING_SNAKE_CASE__ ): """simple docstring""" lowerCamelCase__ = ['''image_processor''', '''tokenizer'''] lowerCamelCase__ = '''ViltImageProcessor''' lowerCamelCase__ = ('''BertTokenizer''', '''BertTokenizerFast''') def __init__( self : Optional[int] , __magic_name__ : str=None , __magic_name__ : List[str]=None , **__magic_name__ : Any ) -> str: SCREAMING_SNAKE_CASE_ = None if "feature_extractor" in kwargs: warnings.warn( "The `feature_extractor` argument is deprecated and will be removed in v5, use `image_processor`" " instead." , __magic_name__ , ) SCREAMING_SNAKE_CASE_ = kwargs.pop("feature_extractor" ) SCREAMING_SNAKE_CASE_ = image_processor if image_processor is not None else feature_extractor if image_processor is None: raise ValueError("You need to specify an `image_processor`." ) if tokenizer is None: raise ValueError("You need to specify a `tokenizer`." ) super().__init__(__magic_name__ , __magic_name__ ) SCREAMING_SNAKE_CASE_ = self.image_processor def __call__( self : List[str] , __magic_name__ : List[str] , __magic_name__ : Union[TextInput, PreTokenizedInput, List[TextInput], List[PreTokenizedInput]] = None , __magic_name__ : bool = True , __magic_name__ : Union[bool, str, PaddingStrategy] = False , __magic_name__ : Union[bool, str, TruncationStrategy] = None , __magic_name__ : Optional[int] = None , __magic_name__ : int = 0 , __magic_name__ : Optional[int] = None , __magic_name__ : Optional[bool] = None , __magic_name__ : Optional[bool] = None , __magic_name__ : bool = False , __magic_name__ : bool = False , __magic_name__ : bool = False , __magic_name__ : bool = False , __magic_name__ : bool = True , __magic_name__ : Optional[Union[str, TensorType]] = None , **__magic_name__ : str , ) -> BatchEncoding: SCREAMING_SNAKE_CASE_ = self.tokenizer( text=__magic_name__ , add_special_tokens=__magic_name__ , padding=__magic_name__ , truncation=__magic_name__ , max_length=__magic_name__ , stride=__magic_name__ , pad_to_multiple_of=__magic_name__ , return_token_type_ids=__magic_name__ , return_attention_mask=__magic_name__ , return_overflowing_tokens=__magic_name__ , return_special_tokens_mask=__magic_name__ , return_offsets_mapping=__magic_name__ , return_length=__magic_name__ , verbose=__magic_name__ , return_tensors=__magic_name__ , **__magic_name__ , ) # add pixel_values + pixel_mask SCREAMING_SNAKE_CASE_ = self.image_processor(__magic_name__ , return_tensors=__magic_name__ ) encoding.update(__magic_name__ ) return encoding def __A ( self : Optional[int] , *__magic_name__ : List[Any] , **__magic_name__ : Optional[Any] ) -> Any: return self.tokenizer.batch_decode(*__magic_name__ , **__magic_name__ ) def __A ( self : Dict , *__magic_name__ : List[Any] , **__magic_name__ : Union[str, Any] ) -> str: return self.tokenizer.decode(*__magic_name__ , **__magic_name__ ) @property def __A ( self : Optional[int] ) -> Union[str, Any]: SCREAMING_SNAKE_CASE_ = self.tokenizer.model_input_names SCREAMING_SNAKE_CASE_ = self.image_processor.model_input_names return list(dict.fromkeys(tokenizer_input_names + image_processor_input_names ) ) @property def __A ( self : Dict ) -> List[Any]: warnings.warn( "`feature_extractor_class` is deprecated and will be removed in v5. Use `image_processor_class` instead." , __magic_name__ , ) return self.image_processor_class @property def __A ( self : int ) -> List[Any]: warnings.warn( "`feature_extractor` is deprecated and will be removed in v5. Use `image_processor` instead." , __magic_name__ , ) return self.image_processor
305
0
import math from collections import defaultdict from typing import List, Optional, Tuple, Union import numpy as np import torch from ..configuration_utils import ConfigMixin, register_to_config from .scheduling_utils import KarrasDiffusionSchedulers, SchedulerMixin, SchedulerOutput def a__ ( __UpperCamelCase , __UpperCamelCase=0.999 , __UpperCamelCase="cosine" , ): if alpha_transform_type == "cosine": def alpha_bar_fn(__UpperCamelCase ): return math.cos((t + 0.008) / 1.008 * math.pi / 2 ) ** 2 elif alpha_transform_type == "exp": def alpha_bar_fn(__UpperCamelCase ): return math.exp(t * -12.0 ) else: raise ValueError(F'''Unsupported alpha_tranform_type: {alpha_transform_type}''' ) SCREAMING_SNAKE_CASE_ = [] for i in range(__SCREAMING_SNAKE_CASE ): SCREAMING_SNAKE_CASE_ = i / num_diffusion_timesteps SCREAMING_SNAKE_CASE_ = (i + 1) / num_diffusion_timesteps betas.append(min(1 - alpha_bar_fn(__SCREAMING_SNAKE_CASE ) / alpha_bar_fn(__SCREAMING_SNAKE_CASE ) , __SCREAMING_SNAKE_CASE ) ) return torch.tensor(__SCREAMING_SNAKE_CASE , dtype=torch.floataa ) class lowerCamelCase (__UpperCamelCase , __UpperCamelCase ): """simple docstring""" lowerCamelCase__ = [e.name for e in KarrasDiffusionSchedulers] lowerCamelCase__ = 2 @register_to_config def __init__( self : Optional[int] , __magic_name__ : int = 1_000 , __magic_name__ : float = 0.0_0085 , __magic_name__ : float = 0.012 , __magic_name__ : str = "linear" , __magic_name__ : Optional[Union[np.ndarray, List[float]]] = None , __magic_name__ : str = "epsilon" , __magic_name__ : Optional[bool] = False , __magic_name__ : Optional[bool] = False , __magic_name__ : float = 1.0 , __magic_name__ : str = "linspace" , __magic_name__ : int = 0 , ) -> Union[str, Any]: if trained_betas is not None: SCREAMING_SNAKE_CASE_ = torch.tensor(_lowerCAmelCase , dtype=torch.floataa ) elif beta_schedule == "linear": SCREAMING_SNAKE_CASE_ = torch.linspace(_lowerCAmelCase , _lowerCAmelCase , _lowerCAmelCase , dtype=torch.floataa ) elif beta_schedule == "scaled_linear": # this schedule is very specific to the latent diffusion model. SCREAMING_SNAKE_CASE_ = ( torch.linspace(beta_start**0.5 , beta_end**0.5 , _lowerCAmelCase , dtype=torch.floataa ) ** 2 ) elif beta_schedule == "squaredcos_cap_v2": # Glide cosine schedule SCREAMING_SNAKE_CASE_ = betas_for_alpha_bar(_lowerCAmelCase , alpha_transform_type="cosine" ) elif beta_schedule == "exp": SCREAMING_SNAKE_CASE_ = betas_for_alpha_bar(_lowerCAmelCase , alpha_transform_type="exp" ) else: raise NotImplementedError(F'''{beta_schedule} does is not implemented for {self.__class__}''' ) SCREAMING_SNAKE_CASE_ = 1.0 - self.betas SCREAMING_SNAKE_CASE_ = torch.cumprod(self.alphas , dim=0 ) # set all values self.set_timesteps(_lowerCAmelCase , _lowerCAmelCase , _lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = use_karras_sigmas def __A ( self : Tuple , __magic_name__ : int , __magic_name__ : int=None ) -> List[str]: if schedule_timesteps is None: SCREAMING_SNAKE_CASE_ = self.timesteps SCREAMING_SNAKE_CASE_ = (schedule_timesteps == timestep).nonzero() # The sigma index that is taken for the **very** first `step` # is always the second index (or the last index if there is only 1) # This way we can ensure we don't accidentally skip a sigma in # case we start in the middle of the denoising schedule (e.g. for image-to-image) if len(self._index_counter ) == 0: SCREAMING_SNAKE_CASE_ = 1 if len(_lowerCAmelCase ) > 1 else 0 else: SCREAMING_SNAKE_CASE_ = timestep.cpu().item() if torch.is_tensor(_lowerCAmelCase ) else timestep SCREAMING_SNAKE_CASE_ = self._index_counter[timestep_int] return indices[pos].item() @property def __A ( self : Optional[Any] ) -> List[Any]: # standard deviation of the initial noise distribution if self.config.timestep_spacing in ["linspace", "trailing"]: return self.sigmas.max() return (self.sigmas.max() ** 2 + 1) ** 0.5 def __A ( self : Optional[Any] , __magic_name__ : torch.FloatTensor , __magic_name__ : Union[float, torch.FloatTensor] , ) -> Dict: SCREAMING_SNAKE_CASE_ = self.index_for_timestep(_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = self.sigmas[step_index] SCREAMING_SNAKE_CASE_ = sample / ((sigma**2 + 1) ** 0.5) return sample def __A ( self : List[Any] , __magic_name__ : int , __magic_name__ : Union[str, torch.device] = None , __magic_name__ : Optional[int] = None , ) -> Optional[int]: SCREAMING_SNAKE_CASE_ = num_inference_steps SCREAMING_SNAKE_CASE_ = num_train_timesteps or self.config.num_train_timesteps # "linspace", "leading", "trailing" corresponds to annotation of Table 2. of https://arxiv.org/abs/2305.08891 if self.config.timestep_spacing == "linspace": SCREAMING_SNAKE_CASE_ = np.linspace(0 , num_train_timesteps - 1 , _lowerCAmelCase , dtype=_lowerCAmelCase )[::-1].copy() elif self.config.timestep_spacing == "leading": SCREAMING_SNAKE_CASE_ = num_train_timesteps // self.num_inference_steps # creates integer timesteps by multiplying by ratio # casting to int to avoid issues when num_inference_step is power of 3 SCREAMING_SNAKE_CASE_ = (np.arange(0 , _lowerCAmelCase ) * step_ratio).round()[::-1].copy().astype(_lowerCAmelCase ) timesteps += self.config.steps_offset elif self.config.timestep_spacing == "trailing": SCREAMING_SNAKE_CASE_ = num_train_timesteps / self.num_inference_steps # creates integer timesteps by multiplying by ratio # casting to int to avoid issues when num_inference_step is power of 3 SCREAMING_SNAKE_CASE_ = (np.arange(_lowerCAmelCase , 0 , -step_ratio )).round().copy().astype(_lowerCAmelCase ) timesteps -= 1 else: raise ValueError( F'''{self.config.timestep_spacing} is not supported. Please make sure to choose one of \'linspace\', \'leading\' or \'trailing\'.''' ) SCREAMING_SNAKE_CASE_ = np.array(((1 - self.alphas_cumprod) / self.alphas_cumprod) ** 0.5 ) SCREAMING_SNAKE_CASE_ = np.log(_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = np.interp(_lowerCAmelCase , np.arange(0 , len(_lowerCAmelCase ) ) , _lowerCAmelCase ) if self.config.use_karras_sigmas: SCREAMING_SNAKE_CASE_ = self._convert_to_karras(in_sigmas=_lowerCAmelCase , num_inference_steps=self.num_inference_steps ) SCREAMING_SNAKE_CASE_ = np.array([self._sigma_to_t(_lowerCAmelCase , _lowerCAmelCase ) for sigma in sigmas] ) SCREAMING_SNAKE_CASE_ = np.concatenate([sigmas, [0.0]] ).astype(np.floataa ) SCREAMING_SNAKE_CASE_ = torch.from_numpy(_lowerCAmelCase ).to(device=_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = torch.cat([sigmas[:1], sigmas[1:-1].repeat_interleave(2 ), sigmas[-1:]] ) SCREAMING_SNAKE_CASE_ = torch.from_numpy(_lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = torch.cat([timesteps[:1], timesteps[1:].repeat_interleave(2 )] ) if str(_lowerCAmelCase ).startswith("mps" ): # mps does not support float64 SCREAMING_SNAKE_CASE_ = timesteps.to(_lowerCAmelCase , dtype=torch.floataa ) else: SCREAMING_SNAKE_CASE_ = timesteps.to(device=_lowerCAmelCase ) # empty dt and derivative SCREAMING_SNAKE_CASE_ = None SCREAMING_SNAKE_CASE_ = None # for exp beta schedules, such as the one for `pipeline_shap_e.py` # we need an index counter SCREAMING_SNAKE_CASE_ = defaultdict(_lowerCAmelCase ) def __A ( self : List[Any] , __magic_name__ : Optional[Any] , __magic_name__ : Optional[Any] ) -> str: # get log sigma SCREAMING_SNAKE_CASE_ = np.log(_lowerCAmelCase ) # get distribution SCREAMING_SNAKE_CASE_ = log_sigma - log_sigmas[:, np.newaxis] # get sigmas range SCREAMING_SNAKE_CASE_ = np.cumsum((dists >= 0) , axis=0 ).argmax(axis=0 ).clip(max=log_sigmas.shape[0] - 2 ) SCREAMING_SNAKE_CASE_ = low_idx + 1 SCREAMING_SNAKE_CASE_ = log_sigmas[low_idx] SCREAMING_SNAKE_CASE_ = log_sigmas[high_idx] # interpolate sigmas SCREAMING_SNAKE_CASE_ = (low - log_sigma) / (low - high) SCREAMING_SNAKE_CASE_ = np.clip(_lowerCAmelCase , 0 , 1 ) # transform interpolation to time range SCREAMING_SNAKE_CASE_ = (1 - w) * low_idx + w * high_idx SCREAMING_SNAKE_CASE_ = t.reshape(sigma.shape ) return t def __A ( self : Union[str, Any] , __magic_name__ : torch.FloatTensor , __magic_name__ : List[Any] ) -> Dict: SCREAMING_SNAKE_CASE_ = in_sigmas[-1].item() SCREAMING_SNAKE_CASE_ = in_sigmas[0].item() SCREAMING_SNAKE_CASE_ = 7.0 # 7.0 is the value used in the paper SCREAMING_SNAKE_CASE_ = np.linspace(0 , 1 , _lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = sigma_min ** (1 / rho) SCREAMING_SNAKE_CASE_ = sigma_max ** (1 / rho) SCREAMING_SNAKE_CASE_ = (max_inv_rho + ramp * (min_inv_rho - max_inv_rho)) ** rho return sigmas @property def __A ( self : Optional[int] ) -> Any: return self.dt is None def __A ( self : Any , __magic_name__ : Union[torch.FloatTensor, np.ndarray] , __magic_name__ : Union[float, torch.FloatTensor] , __magic_name__ : Union[torch.FloatTensor, np.ndarray] , __magic_name__ : bool = True , ) -> str: SCREAMING_SNAKE_CASE_ = self.index_for_timestep(_lowerCAmelCase ) # advance index counter by 1 SCREAMING_SNAKE_CASE_ = timestep.cpu().item() if torch.is_tensor(_lowerCAmelCase ) else timestep self._index_counter[timestep_int] += 1 if self.state_in_first_order: SCREAMING_SNAKE_CASE_ = self.sigmas[step_index] SCREAMING_SNAKE_CASE_ = self.sigmas[step_index + 1] else: # 2nd order / Heun's method SCREAMING_SNAKE_CASE_ = self.sigmas[step_index - 1] SCREAMING_SNAKE_CASE_ = self.sigmas[step_index] # currently only gamma=0 is supported. This usually works best anyways. # We can support gamma in the future but then need to scale the timestep before # passing it to the model which requires a change in API SCREAMING_SNAKE_CASE_ = 0 SCREAMING_SNAKE_CASE_ = sigma * (gamma + 1) # Note: sigma_hat == sigma for now # 1. compute predicted original sample (x_0) from sigma-scaled predicted noise if self.config.prediction_type == "epsilon": SCREAMING_SNAKE_CASE_ = sigma_hat if self.state_in_first_order else sigma_next SCREAMING_SNAKE_CASE_ = sample - sigma_input * model_output elif self.config.prediction_type == "v_prediction": SCREAMING_SNAKE_CASE_ = sigma_hat if self.state_in_first_order else sigma_next SCREAMING_SNAKE_CASE_ = model_output * (-sigma_input / (sigma_input**2 + 1) ** 0.5) + ( sample / (sigma_input**2 + 1) ) elif self.config.prediction_type == "sample": SCREAMING_SNAKE_CASE_ = model_output else: raise ValueError( F'''prediction_type given as {self.config.prediction_type} must be one of `epsilon`, or `v_prediction`''' ) if self.config.clip_sample: SCREAMING_SNAKE_CASE_ = pred_original_sample.clamp( -self.config.clip_sample_range , self.config.clip_sample_range ) if self.state_in_first_order: # 2. Convert to an ODE derivative for 1st order SCREAMING_SNAKE_CASE_ = (sample - pred_original_sample) / sigma_hat # 3. delta timestep SCREAMING_SNAKE_CASE_ = sigma_next - sigma_hat # store for 2nd order step SCREAMING_SNAKE_CASE_ = derivative SCREAMING_SNAKE_CASE_ = dt SCREAMING_SNAKE_CASE_ = sample else: # 2. 2nd order / Heun's method SCREAMING_SNAKE_CASE_ = (sample - pred_original_sample) / sigma_next SCREAMING_SNAKE_CASE_ = (self.prev_derivative + derivative) / 2 # 3. take prev timestep & sample SCREAMING_SNAKE_CASE_ = self.dt SCREAMING_SNAKE_CASE_ = self.sample # free dt and derivative # Note, this puts the scheduler in "first order mode" SCREAMING_SNAKE_CASE_ = None SCREAMING_SNAKE_CASE_ = None SCREAMING_SNAKE_CASE_ = None SCREAMING_SNAKE_CASE_ = sample + derivative * dt if not return_dict: return (prev_sample,) return SchedulerOutput(prev_sample=_lowerCAmelCase ) def __A ( self : Any , __magic_name__ : torch.FloatTensor , __magic_name__ : torch.FloatTensor , __magic_name__ : torch.FloatTensor , ) -> Optional[int]: # Make sure sigmas and timesteps have the same device and dtype as original_samples SCREAMING_SNAKE_CASE_ = self.sigmas.to(device=original_samples.device , dtype=original_samples.dtype ) if original_samples.device.type == "mps" and torch.is_floating_point(_lowerCAmelCase ): # mps does not support float64 SCREAMING_SNAKE_CASE_ = self.timesteps.to(original_samples.device , dtype=torch.floataa ) SCREAMING_SNAKE_CASE_ = timesteps.to(original_samples.device , dtype=torch.floataa ) else: SCREAMING_SNAKE_CASE_ = self.timesteps.to(original_samples.device ) SCREAMING_SNAKE_CASE_ = timesteps.to(original_samples.device ) SCREAMING_SNAKE_CASE_ = [self.index_for_timestep(_lowerCAmelCase , _lowerCAmelCase ) for t in timesteps] SCREAMING_SNAKE_CASE_ = sigmas[step_indices].flatten() while len(sigma.shape ) < len(original_samples.shape ): SCREAMING_SNAKE_CASE_ = sigma.unsqueeze(-1 ) SCREAMING_SNAKE_CASE_ = original_samples + noise * sigma return noisy_samples def __len__( self : Tuple ) -> Dict: return self.config.num_train_timesteps
370
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 ..auto import CONFIG_MAPPING A : str = logging.get_logger(__name__) A : Optional[int] = { "microsoft/table-transformer-detection": ( "https://huggingface.co/microsoft/table-transformer-detection/resolve/main/config.json" ), } class lowerCamelCase (SCREAMING_SNAKE_CASE__ ): """simple docstring""" lowerCamelCase__ = '''table-transformer''' lowerCamelCase__ = ['''past_key_values'''] lowerCamelCase__ = { '''hidden_size''': '''d_model''', '''num_attention_heads''': '''encoder_attention_heads''', } def __init__( self : List[Any] , __magic_name__ : Optional[Any]=True , __magic_name__ : Dict=None , __magic_name__ : Any=3 , __magic_name__ : List[str]=100 , __magic_name__ : Union[str, Any]=6 , __magic_name__ : Dict=2_048 , __magic_name__ : str=8 , __magic_name__ : int=6 , __magic_name__ : List[Any]=2_048 , __magic_name__ : Optional[int]=8 , __magic_name__ : Optional[int]=0.0 , __magic_name__ : List[Any]=0.0 , __magic_name__ : Optional[Any]=True , __magic_name__ : List[Any]="relu" , __magic_name__ : List[str]=256 , __magic_name__ : List[str]=0.1 , __magic_name__ : int=0.0 , __magic_name__ : Optional[Any]=0.0 , __magic_name__ : Tuple=0.02 , __magic_name__ : str=1.0 , __magic_name__ : int=False , __magic_name__ : Dict="sine" , __magic_name__ : Union[str, Any]="resnet50" , __magic_name__ : Optional[Any]=True , __magic_name__ : str=False , __magic_name__ : List[str]=1 , __magic_name__ : int=5 , __magic_name__ : Union[str, Any]=2 , __magic_name__ : Tuple=1 , __magic_name__ : Optional[int]=1 , __magic_name__ : Optional[Any]=5 , __magic_name__ : Optional[int]=2 , __magic_name__ : Union[str, Any]=0.1 , **__magic_name__ : Tuple , ) -> str: 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." ) SCREAMING_SNAKE_CASE_ = CONFIG_MAPPING["resnet"](out_features=["stage4"] ) elif isinstance(__magic_name__ , __magic_name__ ): SCREAMING_SNAKE_CASE_ = backbone_config.get("model_type" ) SCREAMING_SNAKE_CASE_ = CONFIG_MAPPING[backbone_model_type] SCREAMING_SNAKE_CASE_ = config_class.from_dict(__magic_name__ ) # set timm attributes to None SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = None, None, None SCREAMING_SNAKE_CASE_ = use_timm_backbone SCREAMING_SNAKE_CASE_ = backbone_config SCREAMING_SNAKE_CASE_ = num_channels SCREAMING_SNAKE_CASE_ = num_queries SCREAMING_SNAKE_CASE_ = d_model SCREAMING_SNAKE_CASE_ = encoder_ffn_dim SCREAMING_SNAKE_CASE_ = encoder_layers SCREAMING_SNAKE_CASE_ = encoder_attention_heads SCREAMING_SNAKE_CASE_ = decoder_ffn_dim SCREAMING_SNAKE_CASE_ = decoder_layers SCREAMING_SNAKE_CASE_ = decoder_attention_heads SCREAMING_SNAKE_CASE_ = dropout SCREAMING_SNAKE_CASE_ = attention_dropout SCREAMING_SNAKE_CASE_ = activation_dropout SCREAMING_SNAKE_CASE_ = activation_function SCREAMING_SNAKE_CASE_ = init_std SCREAMING_SNAKE_CASE_ = init_xavier_std SCREAMING_SNAKE_CASE_ = encoder_layerdrop SCREAMING_SNAKE_CASE_ = decoder_layerdrop SCREAMING_SNAKE_CASE_ = encoder_layers SCREAMING_SNAKE_CASE_ = auxiliary_loss SCREAMING_SNAKE_CASE_ = position_embedding_type SCREAMING_SNAKE_CASE_ = backbone SCREAMING_SNAKE_CASE_ = use_pretrained_backbone SCREAMING_SNAKE_CASE_ = dilation # Hungarian matcher SCREAMING_SNAKE_CASE_ = class_cost SCREAMING_SNAKE_CASE_ = bbox_cost SCREAMING_SNAKE_CASE_ = giou_cost # Loss coefficients SCREAMING_SNAKE_CASE_ = mask_loss_coefficient SCREAMING_SNAKE_CASE_ = dice_loss_coefficient SCREAMING_SNAKE_CASE_ = bbox_loss_coefficient SCREAMING_SNAKE_CASE_ = giou_loss_coefficient SCREAMING_SNAKE_CASE_ = eos_coefficient super().__init__(is_encoder_decoder=__magic_name__ , **__magic_name__ ) @property def __A ( self : Union[str, Any] ) -> int: return self.encoder_attention_heads @property def __A ( self : Any ) -> int: return self.d_model class lowerCamelCase (SCREAMING_SNAKE_CASE__ ): """simple docstring""" lowerCamelCase__ = version.parse('''1.11''' ) @property def __A ( self : Tuple ) -> Mapping[str, Mapping[int, str]]: return OrderedDict( [ ("pixel_values", {0: "batch", 1: "num_channels", 2: "height", 3: "width"}), ("pixel_mask", {0: "batch"}), ] ) @property def __A ( self : Any ) -> float: return 1e-5 @property def __A ( self : int ) -> int: return 12
305
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() A : Optional[int] = logging.get_logger(__name__) def a__ ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase ): SCREAMING_SNAKE_CASE_ = os.path.abspath(UpperCAmelCase__ ) logger.info(F'''Converting TensorFlow checkpoint from {tf_path}''' ) # Load weights from TF model SCREAMING_SNAKE_CASE_ = tf.train.list_variables(UpperCAmelCase__ ) SCREAMING_SNAKE_CASE_ = [] SCREAMING_SNAKE_CASE_ = [] SCREAMING_SNAKE_CASE_ = [] for full_name, shape in init_vars: # logger.info(f"Loading TF weight {name} with shape {shape}") SCREAMING_SNAKE_CASE_ = 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' SCREAMING_SNAKE_CASE_ = name[1:] # figure out how many levels deep the name is SCREAMING_SNAKE_CASE_ = 0 for _name in name: if _name.startswith("layer_with_weights" ): depth += 1 else: break layer_depth.append(UpperCAmelCase__ ) # read data SCREAMING_SNAKE_CASE_ = tf.train.load_variable(UpperCAmelCase__ , UpperCAmelCase__ ) names.append("/".join(UpperCAmelCase__ ) ) arrays.append(UpperCAmelCase__ ) logger.info(F'''Read a total of {len(UpperCAmelCase__ ):,} layers''' ) # Sanity check if len(set(UpperCAmelCase__ ) ) != 1: raise ValueError(F'''Found layer names with different depths (layer depth {list(set(UpperCAmelCase__ ) )})''' ) SCREAMING_SNAKE_CASE_ = list(set(UpperCAmelCase__ ) )[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(UpperCAmelCase__ , UpperCAmelCase__ ): SCREAMING_SNAKE_CASE_ = full_name.split("/" ) SCREAMING_SNAKE_CASE_ = model SCREAMING_SNAKE_CASE_ = [] for i, m_name in enumerate(UpperCAmelCase__ ): if m_name == ".ATTRIBUTES": # variable names end with .ATTRIBUTES/VARIABLE_VALUE break if m_name.startswith("layer_with_weights" ): SCREAMING_SNAKE_CASE_ = 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"] ) SCREAMING_SNAKE_CASE_ = getattr(UpperCAmelCase__ , "embeddings" ) SCREAMING_SNAKE_CASE_ = getattr(UpperCAmelCase__ , "LayerNorm" ) elif layer_num > 3 and layer_num < config.num_hidden_layers + 4: # encoder layers trace.extend(["encoder", "layer", str(layer_num - 4 )] ) SCREAMING_SNAKE_CASE_ = getattr(UpperCAmelCase__ , "encoder" ) SCREAMING_SNAKE_CASE_ = getattr(UpperCAmelCase__ , "layer" ) SCREAMING_SNAKE_CASE_ = pointer[layer_num - 4] elif layer_num == config.num_hidden_layers + 4: # pooler layer trace.extend(["pooler", "dense"] ) SCREAMING_SNAKE_CASE_ = getattr(UpperCAmelCase__ , "pooler" ) SCREAMING_SNAKE_CASE_ = getattr(UpperCAmelCase__ , "dense" ) elif m_name == "embeddings": trace.append("embeddings" ) SCREAMING_SNAKE_CASE_ = getattr(UpperCAmelCase__ , "embeddings" ) if layer_num == 0: trace.append("word_embeddings" ) SCREAMING_SNAKE_CASE_ = getattr(UpperCAmelCase__ , "word_embeddings" ) elif layer_num == 1: trace.append("position_embeddings" ) SCREAMING_SNAKE_CASE_ = getattr(UpperCAmelCase__ , "position_embeddings" ) elif layer_num == 2: trace.append("token_type_embeddings" ) SCREAMING_SNAKE_CASE_ = getattr(UpperCAmelCase__ , "token_type_embeddings" ) else: raise ValueError(F'''Unknown embedding layer with name {full_name}''' ) trace.append("weight" ) SCREAMING_SNAKE_CASE_ = getattr(UpperCAmelCase__ , "weight" ) elif m_name == "_attention_layer": # self-attention layer trace.extend(["attention", "self"] ) SCREAMING_SNAKE_CASE_ = getattr(UpperCAmelCase__ , "attention" ) SCREAMING_SNAKE_CASE_ = getattr(UpperCAmelCase__ , "self" ) elif m_name == "_attention_layer_norm": # output attention norm trace.extend(["attention", "output", "LayerNorm"] ) SCREAMING_SNAKE_CASE_ = getattr(UpperCAmelCase__ , "attention" ) SCREAMING_SNAKE_CASE_ = getattr(UpperCAmelCase__ , "output" ) SCREAMING_SNAKE_CASE_ = getattr(UpperCAmelCase__ , "LayerNorm" ) elif m_name == "_attention_output_dense": # output attention dense trace.extend(["attention", "output", "dense"] ) SCREAMING_SNAKE_CASE_ = getattr(UpperCAmelCase__ , "attention" ) SCREAMING_SNAKE_CASE_ = getattr(UpperCAmelCase__ , "output" ) SCREAMING_SNAKE_CASE_ = getattr(UpperCAmelCase__ , "dense" ) elif m_name == "_output_dense": # output dense trace.extend(["output", "dense"] ) SCREAMING_SNAKE_CASE_ = getattr(UpperCAmelCase__ , "output" ) SCREAMING_SNAKE_CASE_ = getattr(UpperCAmelCase__ , "dense" ) elif m_name == "_output_layer_norm": # output dense trace.extend(["output", "LayerNorm"] ) SCREAMING_SNAKE_CASE_ = getattr(UpperCAmelCase__ , "output" ) SCREAMING_SNAKE_CASE_ = getattr(UpperCAmelCase__ , "LayerNorm" ) elif m_name == "_key_dense": # attention key trace.append("key" ) SCREAMING_SNAKE_CASE_ = getattr(UpperCAmelCase__ , "key" ) elif m_name == "_query_dense": # attention query trace.append("query" ) SCREAMING_SNAKE_CASE_ = getattr(UpperCAmelCase__ , "query" ) elif m_name == "_value_dense": # attention value trace.append("value" ) SCREAMING_SNAKE_CASE_ = getattr(UpperCAmelCase__ , "value" ) elif m_name == "_intermediate_dense": # attention intermediate dense trace.extend(["intermediate", "dense"] ) SCREAMING_SNAKE_CASE_ = getattr(UpperCAmelCase__ , "intermediate" ) SCREAMING_SNAKE_CASE_ = getattr(UpperCAmelCase__ , "dense" ) elif m_name == "_output_layer_norm": # output layer norm trace.append("output" ) SCREAMING_SNAKE_CASE_ = getattr(UpperCAmelCase__ , "output" ) # weights & biases elif m_name in ["bias", "beta"]: trace.append("bias" ) SCREAMING_SNAKE_CASE_ = getattr(UpperCAmelCase__ , "bias" ) elif m_name in ["kernel", "gamma"]: trace.append("weight" ) SCREAMING_SNAKE_CASE_ = getattr(UpperCAmelCase__ , "weight" ) else: logger.warning(F'''Ignored {m_name}''' ) # for certain layers reshape is necessary SCREAMING_SNAKE_CASE_ = """.""".join(UpperCAmelCase__ ) if re.match(r"(\S+)\.attention\.self\.(key|value|query)\.(bias|weight)" , UpperCAmelCase__ ) or re.match( r"(\S+)\.attention\.output\.dense\.weight" , UpperCAmelCase__ ): SCREAMING_SNAKE_CASE_ = array.reshape(pointer.data.shape ) if "kernel" in full_name: SCREAMING_SNAKE_CASE_ = array.transpose() if pointer.shape == array.shape: SCREAMING_SNAKE_CASE_ = torch.from_numpy(UpperCAmelCase__ ) 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__ ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase ): # Instantiate model logger.info(F'''Loading model based on config from {config_path}...''' ) SCREAMING_SNAKE_CASE_ = BertConfig.from_json_file(UpperCAmelCase__ ) SCREAMING_SNAKE_CASE_ = BertModel(UpperCAmelCase__ ) # Load weights from checkpoint logger.info(F'''Loading weights from checkpoint {tf_checkpoint_path}...''' ) load_tfa_weights_in_bert(UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__ ) # Save pytorch-model logger.info(F'''Saving PyTorch model to {pytorch_dump_path}...''' ) torch.save(model.state_dict() , UpperCAmelCase__ ) if __name__ == "__main__": A : Optional[Any] = 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).", ) A : List[str] = parser.parse_args() convert_tfa_checkpoint_to_pytorch(args.tf_checkpoint_path, args.bert_config_file, args.pytorch_dump_path)
371
import warnings from diffusers import StableDiffusionImgaImgPipeline # noqa F401 warnings.warn( "The `image_to_image.py` script is outdated. Please use directly `from diffusers import" " StableDiffusionImg2ImgPipeline` instead." )
305
0
"""simple docstring""" import inspect import unittest import numpy as np from tests.test_modeling_common import floats_tensor from transformers import DetrConfig, MaskFormerConfig, SwinConfig, is_torch_available, is_vision_available from transformers.testing_utils import require_torch, require_torch_multi_gpu, require_vision, slow, torch_device from transformers.utils import cached_property from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from transformers import MaskFormerForInstanceSegmentation, MaskFormerModel if is_vision_available(): from transformers import MaskFormerImageProcessor if is_vision_available(): from PIL import Image class lowerCamelCase : """simple docstring""" def __init__( self : Optional[int] , __magic_name__ : Any , __magic_name__ : Optional[Any]=2 , __magic_name__ : Dict=True , __magic_name__ : int=False , __magic_name__ : int=10 , __magic_name__ : int=3 , __magic_name__ : int=32 * 4 , __magic_name__ : Optional[int]=32 * 6 , __magic_name__ : Tuple=4 , __magic_name__ : str=32 , ) -> List[str]: SCREAMING_SNAKE_CASE_ = parent SCREAMING_SNAKE_CASE_ = batch_size SCREAMING_SNAKE_CASE_ = is_training SCREAMING_SNAKE_CASE_ = use_auxiliary_loss SCREAMING_SNAKE_CASE_ = num_queries SCREAMING_SNAKE_CASE_ = num_channels SCREAMING_SNAKE_CASE_ = min_size SCREAMING_SNAKE_CASE_ = max_size SCREAMING_SNAKE_CASE_ = num_labels SCREAMING_SNAKE_CASE_ = mask_feature_size def __A ( self : Optional[Any] ) -> List[Any]: SCREAMING_SNAKE_CASE_ = floats_tensor([self.batch_size, self.num_channels, self.min_size, self.max_size] ).to( _a ) SCREAMING_SNAKE_CASE_ = torch.ones([self.batch_size, self.min_size, self.max_size] , device=_a ) SCREAMING_SNAKE_CASE_ = ( torch.rand([self.batch_size, self.num_labels, self.min_size, self.max_size] , device=_a ) > 0.5 ).float() SCREAMING_SNAKE_CASE_ = (torch.rand((self.batch_size, self.num_labels) , device=_a ) > 0.5).long() SCREAMING_SNAKE_CASE_ = self.get_config() return config, pixel_values, pixel_mask, mask_labels, class_labels def __A ( self : Any ) -> int: return MaskFormerConfig.from_backbone_and_decoder_configs( backbone_config=SwinConfig( depths=[1, 1, 1, 1] , ) , decoder_config=DetrConfig( decoder_ffn_dim=128 , num_queries=self.num_queries , decoder_attention_heads=2 , d_model=self.mask_feature_size , ) , mask_feature_size=self.mask_feature_size , fpn_feature_size=self.mask_feature_size , num_channels=self.num_channels , num_labels=self.num_labels , ) def __A ( self : List[Any] ) -> Union[str, Any]: SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = self.prepare_config_and_inputs() SCREAMING_SNAKE_CASE_ = {"pixel_values": pixel_values, "pixel_mask": pixel_mask} return config, inputs_dict def __A ( self : Dict , __magic_name__ : Optional[int] , __magic_name__ : int ) -> Optional[int]: SCREAMING_SNAKE_CASE_ = output.encoder_hidden_states SCREAMING_SNAKE_CASE_ = output.pixel_decoder_hidden_states SCREAMING_SNAKE_CASE_ = output.transformer_decoder_hidden_states self.parent.assertTrue(len(_a ) , len(config.backbone_config.depths ) ) self.parent.assertTrue(len(_a ) , len(config.backbone_config.depths ) ) self.parent.assertTrue(len(_a ) , config.decoder_config.decoder_layers ) def __A ( self : Dict , __magic_name__ : int , __magic_name__ : Tuple , __magic_name__ : Any , __magic_name__ : Tuple=False ) -> Optional[int]: with torch.no_grad(): SCREAMING_SNAKE_CASE_ = MaskFormerModel(config=_a ) model.to(_a ) model.eval() SCREAMING_SNAKE_CASE_ = model(pixel_values=_a , pixel_mask=_a ) SCREAMING_SNAKE_CASE_ = model(_a , output_hidden_states=_a ) # the correct shape of output.transformer_decoder_hidden_states ensure the correcteness of the # encoder and pixel decoder self.parent.assertEqual( output.transformer_decoder_last_hidden_state.shape , (self.batch_size, self.num_queries, self.mask_feature_size) , ) # let's ensure the other two hidden state exists self.parent.assertTrue(output.pixel_decoder_last_hidden_state is not None ) self.parent.assertTrue(output.encoder_last_hidden_state is not None ) if output_hidden_states: self.check_output_hidden_state(_a , _a ) def __A ( self : Union[str, Any] , __magic_name__ : List[Any] , __magic_name__ : Optional[int] , __magic_name__ : Dict , __magic_name__ : Optional[int] , __magic_name__ : int ) -> Tuple: SCREAMING_SNAKE_CASE_ = MaskFormerForInstanceSegmentation(config=_a ) model.to(_a ) model.eval() def comm_check_on_output(__magic_name__ : Any ): # let's still check that all the required stuff is there self.parent.assertTrue(result.transformer_decoder_last_hidden_state is not None ) self.parent.assertTrue(result.pixel_decoder_last_hidden_state is not None ) self.parent.assertTrue(result.encoder_last_hidden_state is not None ) # okay, now we need to check the logits shape # due to the encoder compression, masks have a //4 spatial size self.parent.assertEqual( result.masks_queries_logits.shape , (self.batch_size, self.num_queries, self.min_size // 4, self.max_size // 4) , ) # + 1 for null class self.parent.assertEqual( result.class_queries_logits.shape , (self.batch_size, self.num_queries, self.num_labels + 1) ) with torch.no_grad(): SCREAMING_SNAKE_CASE_ = model(pixel_values=_a , pixel_mask=_a ) SCREAMING_SNAKE_CASE_ = model(_a ) comm_check_on_output(_a ) SCREAMING_SNAKE_CASE_ = model( pixel_values=_a , pixel_mask=_a , mask_labels=_a , class_labels=_a ) comm_check_on_output(_a ) self.parent.assertTrue(result.loss is not None ) self.parent.assertEqual(result.loss.shape , torch.Size([1] ) ) @require_torch class lowerCamelCase (__lowercase , __lowercase , unittest.TestCase ): """simple docstring""" lowerCamelCase__ = (MaskFormerModel, MaskFormerForInstanceSegmentation) if is_torch_available() else () lowerCamelCase__ = ( {'''feature-extraction''': MaskFormerModel, '''image-segmentation''': MaskFormerForInstanceSegmentation} if is_torch_available() else {} ) lowerCamelCase__ = False lowerCamelCase__ = False lowerCamelCase__ = False lowerCamelCase__ = False def __A ( self : List[Any] ) -> Tuple: SCREAMING_SNAKE_CASE_ = MaskFormerModelTester(self ) SCREAMING_SNAKE_CASE_ = ConfigTester(self , config_class=_a , has_text_modality=_a ) def __A ( self : List[Any] ) -> str: self.config_tester.run_common_tests() def __A ( self : List[str] ) -> Any: SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = self.model_tester.prepare_config_and_inputs_for_common() self.model_tester.create_and_check_maskformer_model(_a , **_a , output_hidden_states=_a ) def __A ( self : int ) -> Dict: SCREAMING_SNAKE_CASE_ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_maskformer_instance_segmentation_head_model(*_a ) @unittest.skip(reason="MaskFormer does not use inputs_embeds" ) def __A ( self : Any ) -> Dict: pass @unittest.skip(reason="MaskFormer does not have a get_input_embeddings method" ) def __A ( self : Union[str, Any] ) -> str: pass @unittest.skip(reason="MaskFormer is not a generative model" ) def __A ( self : Tuple ) -> Dict: pass @unittest.skip(reason="MaskFormer does not use token embeddings" ) def __A ( self : List[Any] ) -> Tuple: pass @require_torch_multi_gpu @unittest.skip( reason="MaskFormer has some layers using `add_module` which doesn't work well with `nn.DataParallel`" ) def __A ( self : List[str] ) -> List[str]: pass @unittest.skip("Will be fixed soon by reducing the size of the model used for common tests." ) def __A ( self : Dict ) -> Dict: pass def __A ( self : Union[str, Any] ) -> int: SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: SCREAMING_SNAKE_CASE_ = model_class(_a ) SCREAMING_SNAKE_CASE_ = inspect.signature(model.forward ) # signature.parameters is an OrderedDict => so arg_names order is deterministic SCREAMING_SNAKE_CASE_ = [*signature.parameters.keys()] SCREAMING_SNAKE_CASE_ = ["pixel_values"] self.assertListEqual(arg_names[:1] , _a ) @slow def __A ( self : str ) -> List[str]: for model_name in ["facebook/maskformer-swin-small-coco"]: SCREAMING_SNAKE_CASE_ = MaskFormerModel.from_pretrained(_a ) self.assertIsNotNone(_a ) def __A ( self : str ) -> List[str]: SCREAMING_SNAKE_CASE_ = (self.model_tester.min_size,) * 2 SCREAMING_SNAKE_CASE_ = { "pixel_values": torch.randn((2, 3, *size) , device=_a ), "mask_labels": torch.randn((2, 10, *size) , device=_a ), "class_labels": torch.zeros(2 , 10 , device=_a ).long(), } SCREAMING_SNAKE_CASE_ = MaskFormerForInstanceSegmentation(MaskFormerConfig() ).to(_a ) SCREAMING_SNAKE_CASE_ = model(**_a ) self.assertTrue(outputs.loss is not None ) def __A ( self : Tuple ) -> Dict: SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = self.model_tester.prepare_config_and_inputs_for_common() self.model_tester.create_and_check_maskformer_model(_a , **_a , output_hidden_states=_a ) def __A ( self : Dict ) -> Any: SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: SCREAMING_SNAKE_CASE_ = model_class(_a ).to(_a ) SCREAMING_SNAKE_CASE_ = model(**_a , output_attentions=_a ) self.assertTrue(outputs.attentions is not None ) def __A ( self : Any ) -> Any: if not self.model_tester.is_training: return # only MaskFormerForInstanceSegmentation has the loss SCREAMING_SNAKE_CASE_ = self.all_model_classes[1] SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = self.model_tester.prepare_config_and_inputs() SCREAMING_SNAKE_CASE_ = model_class(_a ) model.to(_a ) model.train() SCREAMING_SNAKE_CASE_ = model(_a , mask_labels=_a , class_labels=_a ).loss loss.backward() def __A ( self : int ) -> Tuple: # only MaskFormerForInstanceSegmentation has the loss SCREAMING_SNAKE_CASE_ = self.all_model_classes[1] SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = self.model_tester.prepare_config_and_inputs() SCREAMING_SNAKE_CASE_ = True SCREAMING_SNAKE_CASE_ = True SCREAMING_SNAKE_CASE_ = model_class(_a ) model.to(_a ) model.train() SCREAMING_SNAKE_CASE_ = model(_a , mask_labels=_a , class_labels=_a ) SCREAMING_SNAKE_CASE_ = outputs.encoder_hidden_states[0] encoder_hidden_states.retain_grad() SCREAMING_SNAKE_CASE_ = outputs.pixel_decoder_hidden_states[0] pixel_decoder_hidden_states.retain_grad() # we requires_grad=True in inputs_embeds (line 2152), the original implementation don't SCREAMING_SNAKE_CASE_ = outputs.transformer_decoder_hidden_states[0] transformer_decoder_hidden_states.retain_grad() SCREAMING_SNAKE_CASE_ = outputs.attentions[0] attentions.retain_grad() outputs.loss.backward(retain_graph=_a ) self.assertIsNotNone(encoder_hidden_states.grad ) self.assertIsNotNone(pixel_decoder_hidden_states.grad ) self.assertIsNotNone(transformer_decoder_hidden_states.grad ) self.assertIsNotNone(attentions.grad ) A : int = 1E-4 def a__ ( ): SCREAMING_SNAKE_CASE_ = Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png" ) return image @require_vision @slow class lowerCamelCase (unittest.TestCase ): """simple docstring""" @cached_property def __A ( self : List[Any] ) -> Tuple: return ( MaskFormerImageProcessor.from_pretrained("facebook/maskformer-swin-small-coco" ) if is_vision_available() else None ) def __A ( self : Dict ) -> int: SCREAMING_SNAKE_CASE_ = MaskFormerModel.from_pretrained("facebook/maskformer-swin-small-coco" ).to(_a ) SCREAMING_SNAKE_CASE_ = self.default_image_processor SCREAMING_SNAKE_CASE_ = prepare_img() SCREAMING_SNAKE_CASE_ = image_processor(_a , return_tensors="pt" ).to(_a ) SCREAMING_SNAKE_CASE_ = inputs["pixel_values"].shape # check size is divisible by 32 self.assertTrue((inputs_shape[-1] % 32) == 0 and (inputs_shape[-2] % 32) == 0 ) # check size self.assertEqual(_a , (1, 3, 800, 1_088) ) with torch.no_grad(): SCREAMING_SNAKE_CASE_ = model(**_a ) SCREAMING_SNAKE_CASE_ = torch.tensor( [[-0.0482, 0.9228, 0.4951], [-0.2547, 0.8017, 0.8527], [-0.0069, 0.3385, -0.0089]] ).to(_a ) self.assertTrue( torch.allclose( outputs.encoder_last_hidden_state[0, 0, :3, :3] , _a , atol=_a ) ) SCREAMING_SNAKE_CASE_ = torch.tensor( [[-0.8422, -0.8434, -0.9718], [-1.0144, -0.5565, -0.4195], [-1.0038, -0.4484, -0.1961]] ).to(_a ) self.assertTrue( torch.allclose( outputs.pixel_decoder_last_hidden_state[0, 0, :3, :3] , _a , atol=_a ) ) SCREAMING_SNAKE_CASE_ = torch.tensor( [[0.2852, -0.0159, 0.9735], [0.6254, 0.1858, 0.8529], [-0.0680, -0.4116, 1.8413]] ).to(_a ) self.assertTrue( torch.allclose( outputs.transformer_decoder_last_hidden_state[0, :3, :3] , _a , atol=_a ) ) def __A ( self : Tuple ) -> List[str]: SCREAMING_SNAKE_CASE_ = ( MaskFormerForInstanceSegmentation.from_pretrained("facebook/maskformer-swin-small-coco" ) .to(_a ) .eval() ) SCREAMING_SNAKE_CASE_ = self.default_image_processor SCREAMING_SNAKE_CASE_ = prepare_img() SCREAMING_SNAKE_CASE_ = image_processor(_a , return_tensors="pt" ).to(_a ) SCREAMING_SNAKE_CASE_ = inputs["pixel_values"].shape # check size is divisible by 32 self.assertTrue((inputs_shape[-1] % 32) == 0 and (inputs_shape[-2] % 32) == 0 ) # check size self.assertEqual(_a , (1, 3, 800, 1_088) ) with torch.no_grad(): SCREAMING_SNAKE_CASE_ = model(**_a ) # masks_queries_logits SCREAMING_SNAKE_CASE_ = outputs.masks_queries_logits self.assertEqual( masks_queries_logits.shape , (1, model.config.decoder_config.num_queries, inputs_shape[-2] // 4, inputs_shape[-1] // 4) , ) SCREAMING_SNAKE_CASE_ = [ [-1.373_7124, -1.772_4937, -1.936_4233], [-1.597_7281, -1.986_7939, -2.152_3695], [-1.579_5398, -1.926_9832, -2.09_3942], ] SCREAMING_SNAKE_CASE_ = torch.tensor(_a ).to(_a ) self.assertTrue(torch.allclose(masks_queries_logits[0, 0, :3, :3] , _a , atol=_a ) ) # class_queries_logits SCREAMING_SNAKE_CASE_ = outputs.class_queries_logits self.assertEqual( class_queries_logits.shape , (1, model.config.decoder_config.num_queries, model.config.num_labels + 1) ) SCREAMING_SNAKE_CASE_ = torch.tensor( [ [1.6512e00, -5.2572e00, -3.3519e00], [3.6169e-02, -5.9025e00, -2.9313e00], [1.0766e-04, -7.7630e00, -5.1263e00], ] ).to(_a ) self.assertTrue(torch.allclose(outputs.class_queries_logits[0, :3, :3] , _a , atol=_a ) ) def __A ( self : str ) -> str: SCREAMING_SNAKE_CASE_ = ( MaskFormerForInstanceSegmentation.from_pretrained("facebook/maskformer-resnet101-coco-stuff" ) .to(_a ) .eval() ) SCREAMING_SNAKE_CASE_ = self.default_image_processor SCREAMING_SNAKE_CASE_ = prepare_img() SCREAMING_SNAKE_CASE_ = image_processor(_a , return_tensors="pt" ).to(_a ) SCREAMING_SNAKE_CASE_ = inputs["pixel_values"].shape # check size is divisible by 32 self.assertTrue((inputs_shape[-1] % 32) == 0 and (inputs_shape[-2] % 32) == 0 ) # check size self.assertEqual(_a , (1, 3, 800, 1_088) ) with torch.no_grad(): SCREAMING_SNAKE_CASE_ = model(**_a ) # masks_queries_logits SCREAMING_SNAKE_CASE_ = outputs.masks_queries_logits self.assertEqual( masks_queries_logits.shape , (1, model.config.decoder_config.num_queries, inputs_shape[-2] // 4, inputs_shape[-1] // 4) , ) SCREAMING_SNAKE_CASE_ = [[-0.9046, -2.6366, -4.6062], [-3.4179, -5.7890, -8.8057], [-4.9179, -7.6560, -10.7_711]] SCREAMING_SNAKE_CASE_ = torch.tensor(_a ).to(_a ) self.assertTrue(torch.allclose(masks_queries_logits[0, 0, :3, :3] , _a , atol=_a ) ) # class_queries_logits SCREAMING_SNAKE_CASE_ = outputs.class_queries_logits self.assertEqual( class_queries_logits.shape , (1, model.config.decoder_config.num_queries, model.config.num_labels + 1) ) SCREAMING_SNAKE_CASE_ = torch.tensor( [[4.7188, -3.2585, -2.8857], [6.6871, -2.9181, -1.2487], [7.2449, -2.2764, -2.1874]] ).to(_a ) self.assertTrue(torch.allclose(outputs.class_queries_logits[0, :3, :3] , _a , atol=_a ) ) def __A ( self : Optional[Any] ) -> int: SCREAMING_SNAKE_CASE_ = ( MaskFormerForInstanceSegmentation.from_pretrained("facebook/maskformer-swin-small-coco" ) .to(_a ) .eval() ) SCREAMING_SNAKE_CASE_ = self.default_image_processor SCREAMING_SNAKE_CASE_ = image_processor( [np.zeros((3, 800, 1_333) ), np.zeros((3, 800, 1_333) )] , segmentation_maps=[np.zeros((384, 384) ).astype(np.floataa ), np.zeros((384, 384) ).astype(np.floataa )] , return_tensors="pt" , ) SCREAMING_SNAKE_CASE_ = inputs["pixel_values"].to(_a ) SCREAMING_SNAKE_CASE_ = [el.to(_a ) for el in inputs["mask_labels"]] SCREAMING_SNAKE_CASE_ = [el.to(_a ) for el in inputs["class_labels"]] with torch.no_grad(): SCREAMING_SNAKE_CASE_ = model(**_a ) self.assertTrue(outputs.loss is not None )
350
from __future__ import annotations import numpy as np def a__ ( __UpperCamelCase ): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = np.shape(__UpperCamelCase ) if rows != columns: SCREAMING_SNAKE_CASE_ = ( "'table' has to be of square shaped array but got a " F'''{rows}x{columns} array:\n{table}''' ) raise ValueError(__UpperCamelCase ) SCREAMING_SNAKE_CASE_ = np.zeros((rows, columns) ) SCREAMING_SNAKE_CASE_ = np.zeros((rows, columns) ) for i in range(__UpperCamelCase ): for j in range(__UpperCamelCase ): SCREAMING_SNAKE_CASE_ = sum(lower[i][k] * upper[k][j] for k in range(__UpperCamelCase ) ) if upper[j][j] == 0: raise ArithmeticError("No LU decomposition exists" ) SCREAMING_SNAKE_CASE_ = (table[i][j] - total) / upper[j][j] SCREAMING_SNAKE_CASE_ = 1 for j in range(__UpperCamelCase , __UpperCamelCase ): SCREAMING_SNAKE_CASE_ = sum(lower[i][k] * upper[k][j] for k in range(__UpperCamelCase ) ) SCREAMING_SNAKE_CASE_ = table[i][j] - total return lower, upper if __name__ == "__main__": import doctest doctest.testmod()
305
0
"""simple docstring""" from __future__ import annotations def a__ ( __UpperCamelCase , __UpperCamelCase ): SCREAMING_SNAKE_CASE_ = get_failure_array(__lowerCamelCase ) # 2) Step through text searching for pattern SCREAMING_SNAKE_CASE_ = 0, 0 # index into text, pattern while i < len(__lowerCamelCase ): if pattern[j] == text[i]: if j == (len(__lowerCamelCase ) - 1): return True j += 1 # if this is a prefix in our pattern # just go back far enough to continue elif j > 0: SCREAMING_SNAKE_CASE_ = failure[j - 1] continue i += 1 return False def a__ ( __UpperCamelCase ): SCREAMING_SNAKE_CASE_ = [0] SCREAMING_SNAKE_CASE_ = 0 SCREAMING_SNAKE_CASE_ = 1 while j < len(__lowerCamelCase ): if pattern[i] == pattern[j]: i += 1 elif i > 0: SCREAMING_SNAKE_CASE_ = failure[i - 1] continue j += 1 failure.append(__lowerCamelCase ) return failure if __name__ == "__main__": # Test 1) A : List[str] = "abc1abc12" A : str = "alskfjaldsabc1abc1abc12k23adsfabcabc" A : int = "alskfjaldsk23adsfabcabc" assert kmp(pattern, texta) and not kmp(pattern, texta) # Test 2) A : Tuple = "ABABX" A : Dict = "ABABZABABYABABX" assert kmp(pattern, text) # Test 3) A : int = "AAAB" A : Dict = "ABAAAAAB" assert kmp(pattern, text) # Test 4) A : Any = "abcdabcy" A : Tuple = "abcxabcdabxabcdabcdabcy" assert kmp(pattern, text) # Test 5) A : Union[str, Any] = "aabaabaaa" assert get_failure_array(pattern) == [0, 1, 0, 1, 2, 3, 4, 5, 2]
351
from math import pi, sqrt, tan def a__ ( __UpperCamelCase ): if side_length < 0: raise ValueError("surface_area_cube() only accepts non-negative values" ) return 6 * side_length**2 def a__ ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase ): if length < 0 or breadth < 0 or height < 0: raise ValueError("surface_area_cuboid() only accepts non-negative values" ) return 2 * ((length * breadth) + (breadth * height) + (length * height)) def a__ ( __UpperCamelCase ): if radius < 0: raise ValueError("surface_area_sphere() only accepts non-negative values" ) return 4 * pi * radius**2 def a__ ( __UpperCamelCase ): if radius < 0: raise ValueError("surface_area_hemisphere() only accepts non-negative values" ) return 3 * pi * radius**2 def a__ ( __UpperCamelCase , __UpperCamelCase ): if radius < 0 or height < 0: raise ValueError("surface_area_cone() only accepts non-negative values" ) return pi * radius * (radius + (height**2 + radius**2) ** 0.5) def a__ ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase ): if radius_a < 0 or radius_a < 0 or height < 0: raise ValueError( "surface_area_conical_frustum() only accepts non-negative values" ) SCREAMING_SNAKE_CASE_ = (height**2 + (radius_a - radius_a) ** 2) ** 0.5 return pi * ((slant_height * (radius_a + radius_a)) + radius_a**2 + radius_a**2) def a__ ( __UpperCamelCase , __UpperCamelCase ): if radius < 0 or height < 0: raise ValueError("surface_area_cylinder() only accepts non-negative values" ) return 2 * pi * radius * (height + radius) def a__ ( __UpperCamelCase , __UpperCamelCase ): if torus_radius < 0 or tube_radius < 0: raise ValueError("surface_area_torus() only accepts non-negative values" ) if torus_radius < tube_radius: raise ValueError( "surface_area_torus() does not support spindle or self intersecting tori" ) return 4 * pow(__UpperCamelCase , 2 ) * torus_radius * tube_radius def a__ ( __UpperCamelCase , __UpperCamelCase ): if length < 0 or width < 0: raise ValueError("area_rectangle() only accepts non-negative values" ) return length * width def a__ ( __UpperCamelCase ): if side_length < 0: raise ValueError("area_square() only accepts non-negative values" ) return side_length**2 def a__ ( __UpperCamelCase , __UpperCamelCase ): if base < 0 or height < 0: raise ValueError("area_triangle() only accepts non-negative values" ) return (base * height) / 2 def a__ ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase ): if sidea < 0 or sidea < 0 or sidea < 0: raise ValueError("area_triangle_three_sides() only accepts non-negative values" ) elif sidea + sidea < sidea or sidea + sidea < sidea or sidea + sidea < sidea: raise ValueError("Given three sides do not form a triangle" ) SCREAMING_SNAKE_CASE_ = (sidea + sidea + sidea) / 2 SCREAMING_SNAKE_CASE_ = sqrt( semi_perimeter * (semi_perimeter - sidea) * (semi_perimeter - sidea) * (semi_perimeter - sidea) ) return area def a__ ( __UpperCamelCase , __UpperCamelCase ): if base < 0 or height < 0: raise ValueError("area_parallelogram() only accepts non-negative values" ) return base * height def a__ ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase ): if basea < 0 or basea < 0 or height < 0: raise ValueError("area_trapezium() only accepts non-negative values" ) return 1 / 2 * (basea + basea) * height def a__ ( __UpperCamelCase ): if radius < 0: raise ValueError("area_circle() only accepts non-negative values" ) return pi * radius**2 def a__ ( __UpperCamelCase , __UpperCamelCase ): if radius_x < 0 or radius_y < 0: raise ValueError("area_ellipse() only accepts non-negative values" ) return pi * radius_x * radius_y def a__ ( __UpperCamelCase , __UpperCamelCase ): if diagonal_a < 0 or diagonal_a < 0: raise ValueError("area_rhombus() only accepts non-negative values" ) return 1 / 2 * diagonal_a * diagonal_a def a__ ( __UpperCamelCase , __UpperCamelCase ): if not isinstance(__UpperCamelCase , __UpperCamelCase ) or sides < 3: raise ValueError( "area_reg_polygon() only accepts integers greater than or \ equal to three as number of sides" ) elif length < 0: raise ValueError( "area_reg_polygon() only accepts non-negative values as \ length of a side" ) return (sides * length**2) / (4 * tan(pi / sides )) return (sides * length**2) / (4 * tan(pi / sides )) if __name__ == "__main__": import doctest doctest.testmod(verbose=True) # verbose so we can see methods missing tests print("[DEMO] Areas of various geometric shapes: \n") print(f"Rectangle: {area_rectangle(10, 20) = }") print(f"Square: {area_square(10) = }") print(f"Triangle: {area_triangle(10, 10) = }") print(f"Triangle: {area_triangle_three_sides(5, 12, 13) = }") print(f"Parallelogram: {area_parallelogram(10, 20) = }") print(f"Rhombus: {area_rhombus(10, 20) = }") print(f"Trapezium: {area_trapezium(10, 20, 30) = }") print(f"Circle: {area_circle(20) = }") print(f"Ellipse: {area_ellipse(10, 20) = }") print("\nSurface Areas of various geometric shapes: \n") print(f"Cube: {surface_area_cube(20) = }") print(f"Cuboid: {surface_area_cuboid(10, 20, 30) = }") print(f"Sphere: {surface_area_sphere(20) = }") print(f"Hemisphere: {surface_area_hemisphere(20) = }") print(f"Cone: {surface_area_cone(10, 20) = }") print(f"Conical Frustum: {surface_area_conical_frustum(10, 20, 30) = }") print(f"Cylinder: {surface_area_cylinder(10, 20) = }") print(f"Torus: {surface_area_torus(20, 10) = }") print(f"Equilateral Triangle: {area_reg_polygon(3, 10) = }") print(f"Square: {area_reg_polygon(4, 10) = }") print(f"Reqular Pentagon: {area_reg_polygon(5, 10) = }")
305
0
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available A : Dict = {"""configuration_vit_msn""": ["""VIT_MSN_PRETRAINED_CONFIG_ARCHIVE_MAP""", """ViTMSNConfig"""]} try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: A : List[str] = [ """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 A : Union[str, Any] = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
352
from collections import OrderedDict from typing import Any, Mapping, Optional from ... import PreTrainedTokenizer from ...configuration_utils import PretrainedConfig from ...file_utils import TensorType, is_torch_available from ...onnx import OnnxConfig, OnnxConfigWithPast, OnnxSeqaSeqConfigWithPast from ...onnx.utils import compute_effective_axis_dimension from ...utils import logging A : List[str] = logging.get_logger(__name__) A : int = { "facebook/blenderbot_small-90M": "https://huggingface.co/facebook/blenderbot_small-90M/resolve/main/config.json", # See all BlenderbotSmall models at https://huggingface.co/models?filter=blenderbot_small } class lowerCamelCase (SCREAMING_SNAKE_CASE__ ): """simple docstring""" lowerCamelCase__ = '''blenderbot-small''' lowerCamelCase__ = ['''past_key_values'''] lowerCamelCase__ = {'''num_attention_heads''': '''encoder_attention_heads''', '''hidden_size''': '''d_model'''} def __init__( self : Dict , __magic_name__ : Dict=50_265 , __magic_name__ : str=512 , __magic_name__ : List[Any]=8 , __magic_name__ : Any=2_048 , __magic_name__ : Dict=16 , __magic_name__ : Any=8 , __magic_name__ : Optional[int]=2_048 , __magic_name__ : Dict=16 , __magic_name__ : Tuple=0.0 , __magic_name__ : Dict=0.0 , __magic_name__ : Optional[int]=True , __magic_name__ : Any=True , __magic_name__ : Dict="gelu" , __magic_name__ : Tuple=512 , __magic_name__ : List[str]=0.1 , __magic_name__ : List[Any]=0.0 , __magic_name__ : List[Any]=0.0 , __magic_name__ : Tuple=0.02 , __magic_name__ : Union[str, Any]=1 , __magic_name__ : List[Any]=False , __magic_name__ : str=0 , __magic_name__ : Dict=1 , __magic_name__ : str=2 , __magic_name__ : Union[str, Any]=2 , **__magic_name__ : Optional[Any] , ) -> Optional[Any]: SCREAMING_SNAKE_CASE_ = vocab_size SCREAMING_SNAKE_CASE_ = max_position_embeddings SCREAMING_SNAKE_CASE_ = d_model SCREAMING_SNAKE_CASE_ = encoder_ffn_dim SCREAMING_SNAKE_CASE_ = encoder_layers SCREAMING_SNAKE_CASE_ = encoder_attention_heads SCREAMING_SNAKE_CASE_ = decoder_ffn_dim SCREAMING_SNAKE_CASE_ = decoder_layers SCREAMING_SNAKE_CASE_ = decoder_attention_heads SCREAMING_SNAKE_CASE_ = dropout SCREAMING_SNAKE_CASE_ = attention_dropout SCREAMING_SNAKE_CASE_ = activation_dropout SCREAMING_SNAKE_CASE_ = activation_function SCREAMING_SNAKE_CASE_ = init_std SCREAMING_SNAKE_CASE_ = encoder_layerdrop SCREAMING_SNAKE_CASE_ = decoder_layerdrop SCREAMING_SNAKE_CASE_ = use_cache SCREAMING_SNAKE_CASE_ = encoder_layers SCREAMING_SNAKE_CASE_ = scale_embedding # scale factor will be sqrt(d_model) if True super().__init__( pad_token_id=__magic_name__ , bos_token_id=__magic_name__ , eos_token_id=__magic_name__ , is_encoder_decoder=__magic_name__ , decoder_start_token_id=__magic_name__ , forced_eos_token_id=__magic_name__ , **__magic_name__ , ) class lowerCamelCase (SCREAMING_SNAKE_CASE__ ): """simple docstring""" @property def __A ( self : str ) -> Mapping[str, Mapping[int, str]]: if self.task in ["default", "seq2seq-lm"]: SCREAMING_SNAKE_CASE_ = OrderedDict( [ ("input_ids", {0: "batch", 1: "encoder_sequence"}), ("attention_mask", {0: "batch", 1: "encoder_sequence"}), ] ) if self.use_past: SCREAMING_SNAKE_CASE_ = {0: "batch"} SCREAMING_SNAKE_CASE_ = {0: "batch", 1: "past_decoder_sequence + sequence"} else: SCREAMING_SNAKE_CASE_ = {0: "batch", 1: "decoder_sequence"} SCREAMING_SNAKE_CASE_ = {0: "batch", 1: "decoder_sequence"} if self.use_past: self.fill_with_past_key_values_(__magic_name__ , direction="inputs" ) elif self.task == "causal-lm": # TODO: figure this case out. SCREAMING_SNAKE_CASE_ = OrderedDict( [ ("input_ids", {0: "batch", 1: "encoder_sequence"}), ("attention_mask", {0: "batch", 1: "encoder_sequence"}), ] ) if self.use_past: SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = self.num_layers for i in range(__magic_name__ ): SCREAMING_SNAKE_CASE_ = {0: "batch", 2: "past_sequence + sequence"} SCREAMING_SNAKE_CASE_ = {0: "batch", 2: "past_sequence + sequence"} else: SCREAMING_SNAKE_CASE_ = OrderedDict( [ ("input_ids", {0: "batch", 1: "encoder_sequence"}), ("attention_mask", {0: "batch", 1: "encoder_sequence"}), ("decoder_input_ids", {0: "batch", 1: "decoder_sequence"}), ("decoder_attention_mask", {0: "batch", 1: "decoder_sequence"}), ] ) return common_inputs @property def __A ( self : Tuple ) -> Mapping[str, Mapping[int, str]]: if self.task in ["default", "seq2seq-lm"]: SCREAMING_SNAKE_CASE_ = super().outputs else: SCREAMING_SNAKE_CASE_ = super(__magic_name__ , self ).outputs if self.use_past: SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = self.num_layers for i in range(__magic_name__ ): SCREAMING_SNAKE_CASE_ = {0: "batch", 2: "past_sequence + sequence"} SCREAMING_SNAKE_CASE_ = {0: "batch", 2: "past_sequence + sequence"} return common_outputs def __A ( self : int , __magic_name__ : PreTrainedTokenizer , __magic_name__ : int = -1 , __magic_name__ : int = -1 , __magic_name__ : bool = False , __magic_name__ : Optional[TensorType] = None , ) -> Mapping[str, Any]: SCREAMING_SNAKE_CASE_ = self._generate_dummy_inputs_for_sequence_classification_and_question_answering( __magic_name__ , __magic_name__ , __magic_name__ , __magic_name__ , __magic_name__ ) # Generate decoder inputs SCREAMING_SNAKE_CASE_ = seq_length if not self.use_past else 1 SCREAMING_SNAKE_CASE_ = self._generate_dummy_inputs_for_sequence_classification_and_question_answering( __magic_name__ , __magic_name__ , __magic_name__ , __magic_name__ , __magic_name__ ) SCREAMING_SNAKE_CASE_ = {F'''decoder_{name}''': tensor for name, tensor in decoder_inputs.items()} SCREAMING_SNAKE_CASE_ = dict(**__magic_name__ , **__magic_name__ ) if self.use_past: if not is_torch_available(): raise ValueError("Cannot generate dummy past_keys inputs without PyTorch installed." ) else: import torch SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = common_inputs["input_ids"].shape SCREAMING_SNAKE_CASE_ = common_inputs["decoder_input_ids"].shape[1] SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = self.num_attention_heads SCREAMING_SNAKE_CASE_ = ( batch, num_encoder_attention_heads, encoder_seq_length, self._config.hidden_size // num_encoder_attention_heads, ) SCREAMING_SNAKE_CASE_ = decoder_seq_length + 3 SCREAMING_SNAKE_CASE_ = ( batch, num_decoder_attention_heads, decoder_past_length, self._config.hidden_size // num_decoder_attention_heads, ) SCREAMING_SNAKE_CASE_ = torch.cat( [common_inputs["decoder_attention_mask"], torch.ones(__magic_name__ , __magic_name__ )] , dim=1 ) SCREAMING_SNAKE_CASE_ = [] # If the number of encoder and decoder layers are present in the model configuration, both are considered SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = self.num_layers SCREAMING_SNAKE_CASE_ = min(__magic_name__ , __magic_name__ ) SCREAMING_SNAKE_CASE_ = max(__magic_name__ , __magic_name__ ) - min_num_layers SCREAMING_SNAKE_CASE_ = "encoder" if num_encoder_layers > num_decoder_layers else "decoder" for _ in range(__magic_name__ ): common_inputs["past_key_values"].append( ( torch.zeros(__magic_name__ ), torch.zeros(__magic_name__ ), torch.zeros(__magic_name__ ), torch.zeros(__magic_name__ ), ) ) # TODO: test this. SCREAMING_SNAKE_CASE_ = encoder_shape if remaining_side_name == "encoder" else decoder_shape for _ in range(__magic_name__ , __magic_name__ ): common_inputs["past_key_values"].append((torch.zeros(__magic_name__ ), torch.zeros(__magic_name__ )) ) return common_inputs def __A ( self : Union[str, Any] , __magic_name__ : PreTrainedTokenizer , __magic_name__ : int = -1 , __magic_name__ : int = -1 , __magic_name__ : bool = False , __magic_name__ : Optional[TensorType] = None , ) -> Mapping[str, Any]: SCREAMING_SNAKE_CASE_ = self._generate_dummy_inputs_for_sequence_classification_and_question_answering( __magic_name__ , __magic_name__ , __magic_name__ , __magic_name__ , __magic_name__ ) if self.use_past: if not is_torch_available(): raise ValueError("Cannot generate dummy past_keys inputs without PyTorch installed." ) else: import torch SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = common_inputs["input_ids"].shape # Not using the same length for past_key_values SCREAMING_SNAKE_CASE_ = seqlen + 2 SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = self.num_layers SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = self.num_attention_heads SCREAMING_SNAKE_CASE_ = ( batch, num_encoder_attention_heads, past_key_values_length, self._config.hidden_size // num_encoder_attention_heads, ) SCREAMING_SNAKE_CASE_ = common_inputs["attention_mask"].dtype SCREAMING_SNAKE_CASE_ = torch.cat( [common_inputs["attention_mask"], torch.ones(__magic_name__ , __magic_name__ , dtype=__magic_name__ )] , dim=1 ) SCREAMING_SNAKE_CASE_ = [ (torch.zeros(__magic_name__ ), torch.zeros(__magic_name__ )) for _ in range(__magic_name__ ) ] return common_inputs def __A ( self : Dict , __magic_name__ : PreTrainedTokenizer , __magic_name__ : int = -1 , __magic_name__ : int = -1 , __magic_name__ : bool = False , __magic_name__ : Optional[TensorType] = None , ) -> Mapping[str, Any]: # Copied from OnnxConfig.generate_dummy_inputs # Did not use super(OnnxConfigWithPast, self).generate_dummy_inputs for code clarity. # If dynamic axis (-1) we forward with a fixed dimension of 2 samples to avoid optimizations made by ONNX SCREAMING_SNAKE_CASE_ = compute_effective_axis_dimension( __magic_name__ , fixed_dimension=OnnxConfig.default_fixed_batch , num_token_to_add=0 ) # If dynamic axis (-1) we forward with a fixed dimension of 8 tokens to avoid optimizations made by ONNX SCREAMING_SNAKE_CASE_ = tokenizer.num_special_tokens_to_add(__magic_name__ ) SCREAMING_SNAKE_CASE_ = compute_effective_axis_dimension( __magic_name__ , fixed_dimension=OnnxConfig.default_fixed_sequence , num_token_to_add=__magic_name__ ) # Generate dummy inputs according to compute batch and sequence SCREAMING_SNAKE_CASE_ = [" ".join([tokenizer.unk_token] ) * seq_length] * batch_size SCREAMING_SNAKE_CASE_ = dict(tokenizer(__magic_name__ , return_tensors=__magic_name__ ) ) return common_inputs def __A ( self : Optional[Any] , __magic_name__ : PreTrainedTokenizer , __magic_name__ : int = -1 , __magic_name__ : int = -1 , __magic_name__ : bool = False , __magic_name__ : Optional[TensorType] = None , ) -> Mapping[str, Any]: if self.task in ["default", "seq2seq-lm"]: SCREAMING_SNAKE_CASE_ = self._generate_dummy_inputs_for_default_and_seqaseq_lm( __magic_name__ , batch_size=__magic_name__ , seq_length=__magic_name__ , is_pair=__magic_name__ , framework=__magic_name__ ) elif self.task == "causal-lm": SCREAMING_SNAKE_CASE_ = self._generate_dummy_inputs_for_causal_lm( __magic_name__ , batch_size=__magic_name__ , seq_length=__magic_name__ , is_pair=__magic_name__ , framework=__magic_name__ ) else: SCREAMING_SNAKE_CASE_ = self._generate_dummy_inputs_for_sequence_classification_and_question_answering( __magic_name__ , batch_size=__magic_name__ , seq_length=__magic_name__ , is_pair=__magic_name__ , framework=__magic_name__ ) return common_inputs def __A ( self : Optional[Any] , __magic_name__ : str , __magic_name__ : List[Any] , __magic_name__ : str , __magic_name__ : List[str] ) -> List[str]: if self.task in ["default", "seq2seq-lm"]: SCREAMING_SNAKE_CASE_ = super()._flatten_past_key_values_(__magic_name__ , __magic_name__ , __magic_name__ , __magic_name__ ) else: SCREAMING_SNAKE_CASE_ = super(__magic_name__ , self )._flatten_past_key_values_( __magic_name__ , __magic_name__ , __magic_name__ , __magic_name__ )
305
0
def a__ ( __UpperCamelCase ): if not isinstance(_A , _A ): raise TypeError("Input value must be an \'int\' type" ) SCREAMING_SNAKE_CASE_ = 0 while number: position += 1 number >>= 1 return position if __name__ == "__main__": import doctest doctest.testmod()
353
import inspect import unittest from datasets import load_dataset from packaging import version from transformers import BeitConfig from transformers.models.auto import get_values from transformers.testing_utils import require_torch, require_torch_multi_gpu, require_vision, slow, torch_device from transformers.utils import cached_property, is_torch_available, is_vision_available from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, _config_zero_init, floats_tensor, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from torch import nn from transformers import ( MODEL_MAPPING, BeitForImageClassification, BeitForMaskedImageModeling, BeitForSemanticSegmentation, BeitModel, ) from transformers.models.beit.modeling_beit import BEIT_PRETRAINED_MODEL_ARCHIVE_LIST if is_vision_available(): import PIL from PIL import Image from transformers import BeitImageProcessor class lowerCamelCase : """simple docstring""" def __init__( self : List[Any] , __magic_name__ : List[str] , __magic_name__ : int=100 , __magic_name__ : Optional[Any]=13 , __magic_name__ : Dict=30 , __magic_name__ : Tuple=2 , __magic_name__ : str=3 , __magic_name__ : str=True , __magic_name__ : Optional[int]=True , __magic_name__ : Union[str, Any]=32 , __magic_name__ : Optional[int]=4 , __magic_name__ : Dict=4 , __magic_name__ : Tuple=37 , __magic_name__ : Any="gelu" , __magic_name__ : int=0.1 , __magic_name__ : List[str]=0.1 , __magic_name__ : Optional[int]=10 , __magic_name__ : Tuple=0.02 , __magic_name__ : Optional[int]=3 , __magic_name__ : List[str]=None , __magic_name__ : Tuple=[0, 1, 2, 3] , ) -> List[str]: SCREAMING_SNAKE_CASE_ = parent SCREAMING_SNAKE_CASE_ = 100 SCREAMING_SNAKE_CASE_ = batch_size SCREAMING_SNAKE_CASE_ = image_size SCREAMING_SNAKE_CASE_ = patch_size SCREAMING_SNAKE_CASE_ = num_channels SCREAMING_SNAKE_CASE_ = is_training SCREAMING_SNAKE_CASE_ = use_labels 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_ = type_sequence_label_size SCREAMING_SNAKE_CASE_ = initializer_range SCREAMING_SNAKE_CASE_ = scope SCREAMING_SNAKE_CASE_ = out_indices SCREAMING_SNAKE_CASE_ = num_labels # in BeiT, the seq length equals the number of patches + 1 (we add 1 for the [CLS] token) SCREAMING_SNAKE_CASE_ = (image_size // patch_size) ** 2 SCREAMING_SNAKE_CASE_ = num_patches + 1 def __A ( self : Any ) -> int: SCREAMING_SNAKE_CASE_ = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_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.image_size, self.image_size] , self.num_labels ) SCREAMING_SNAKE_CASE_ = self.get_config() return config, pixel_values, labels, pixel_labels def __A ( self : Dict ) -> Optional[int]: return BeitConfig( vocab_size=self.vocab_size , image_size=self.image_size , patch_size=self.patch_size , num_channels=self.num_channels , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , is_decoder=__magic_name__ , initializer_range=self.initializer_range , out_indices=self.out_indices , ) def __A ( self : Optional[int] , __magic_name__ : List[str] , __magic_name__ : Optional[int] , __magic_name__ : Tuple , __magic_name__ : Tuple ) -> Optional[int]: SCREAMING_SNAKE_CASE_ = BeitModel(config=__magic_name__ ) model.to(__magic_name__ ) model.eval() SCREAMING_SNAKE_CASE_ = model(__magic_name__ ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def __A ( self : Union[str, Any] , __magic_name__ : List[str] , __magic_name__ : int , __magic_name__ : int , __magic_name__ : str ) -> int: SCREAMING_SNAKE_CASE_ = BeitForMaskedImageModeling(config=__magic_name__ ) model.to(__magic_name__ ) model.eval() SCREAMING_SNAKE_CASE_ = model(__magic_name__ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length - 1, self.vocab_size) ) def __A ( self : Dict , __magic_name__ : List[Any] , __magic_name__ : int , __magic_name__ : Tuple , __magic_name__ : Optional[Any] ) -> int: SCREAMING_SNAKE_CASE_ = self.type_sequence_label_size SCREAMING_SNAKE_CASE_ = BeitForImageClassification(__magic_name__ ) model.to(__magic_name__ ) model.eval() SCREAMING_SNAKE_CASE_ = model(__magic_name__ , labels=__magic_name__ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.type_sequence_label_size) ) # test greyscale images SCREAMING_SNAKE_CASE_ = 1 SCREAMING_SNAKE_CASE_ = BeitForImageClassification(__magic_name__ ) model.to(__magic_name__ ) model.eval() SCREAMING_SNAKE_CASE_ = floats_tensor([self.batch_size, 1, self.image_size, self.image_size] ) SCREAMING_SNAKE_CASE_ = model(__magic_name__ , labels=__magic_name__ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.type_sequence_label_size) ) def __A ( self : Tuple , __magic_name__ : Any , __magic_name__ : Optional[Any] , __magic_name__ : Tuple , __magic_name__ : int ) -> int: SCREAMING_SNAKE_CASE_ = self.num_labels SCREAMING_SNAKE_CASE_ = BeitForSemanticSegmentation(__magic_name__ ) model.to(__magic_name__ ) model.eval() SCREAMING_SNAKE_CASE_ = model(__magic_name__ ) self.parent.assertEqual( result.logits.shape , (self.batch_size, self.num_labels, self.image_size * 2, self.image_size * 2) ) SCREAMING_SNAKE_CASE_ = model(__magic_name__ , labels=__magic_name__ ) self.parent.assertEqual( result.logits.shape , (self.batch_size, self.num_labels, self.image_size * 2, self.image_size * 2) ) def __A ( self : str ) -> List[Any]: SCREAMING_SNAKE_CASE_ = self.prepare_config_and_inputs() SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = config_and_inputs SCREAMING_SNAKE_CASE_ = {"pixel_values": pixel_values} return config, inputs_dict @require_torch class lowerCamelCase (SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , unittest.TestCase ): """simple docstring""" lowerCamelCase__ = ( (BeitModel, BeitForImageClassification, BeitForMaskedImageModeling, BeitForSemanticSegmentation) if is_torch_available() else () ) lowerCamelCase__ = ( { '''feature-extraction''': BeitModel, '''image-classification''': BeitForImageClassification, '''image-segmentation''': BeitForSemanticSegmentation, } if is_torch_available() else {} ) lowerCamelCase__ = False lowerCamelCase__ = False lowerCamelCase__ = False def __A ( self : Tuple ) -> Any: SCREAMING_SNAKE_CASE_ = BeitModelTester(self ) SCREAMING_SNAKE_CASE_ = ConfigTester(self , config_class=__magic_name__ , has_text_modality=__magic_name__ , hidden_size=37 ) def __A ( self : Dict ) -> List[Any]: self.config_tester.run_common_tests() @unittest.skip(reason="BEiT does not use inputs_embeds" ) def __A ( self : List[str] ) -> Optional[Any]: pass @require_torch_multi_gpu @unittest.skip(reason="BEiT has some layers using `add_module` which doesn't work well with `nn.DataParallel`" ) def __A ( self : str ) -> List[str]: pass def __A ( self : List[Any] ) -> 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: SCREAMING_SNAKE_CASE_ = model_class(__magic_name__ ) self.assertIsInstance(model.get_input_embeddings() , (nn.Module) ) SCREAMING_SNAKE_CASE_ = model.get_output_embeddings() self.assertTrue(x is None or isinstance(__magic_name__ , nn.Linear ) ) def __A ( self : Union[str, Any] ) -> int: 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(__magic_name__ ) SCREAMING_SNAKE_CASE_ = inspect.signature(model.forward ) # signature.parameters is an OrderedDict => so arg_names order is deterministic SCREAMING_SNAKE_CASE_ = [*signature.parameters.keys()] SCREAMING_SNAKE_CASE_ = ["pixel_values"] self.assertListEqual(arg_names[:1] , __magic_name__ ) def __A ( self : Tuple ) -> Union[str, Any]: SCREAMING_SNAKE_CASE_ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*__magic_name__ ) def __A ( self : Dict ) -> Union[str, Any]: SCREAMING_SNAKE_CASE_ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_masked_lm(*__magic_name__ ) def __A ( self : Optional[int] ) -> List[Any]: SCREAMING_SNAKE_CASE_ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_image_classification(*__magic_name__ ) def __A ( self : Optional[Any] ) -> List[str]: SCREAMING_SNAKE_CASE_ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_semantic_segmentation(*__magic_name__ ) def __A ( self : int ) -> Optional[int]: if not self.model_tester.is_training: return SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = self.model_tester.prepare_config_and_inputs_for_common() SCREAMING_SNAKE_CASE_ = True for model_class in self.all_model_classes: # we don't test BeitForMaskedImageModeling if model_class in [*get_values(__magic_name__ ), BeitForMaskedImageModeling]: continue SCREAMING_SNAKE_CASE_ = model_class(__magic_name__ ) model.to(__magic_name__ ) model.train() SCREAMING_SNAKE_CASE_ = self._prepare_for_class(__magic_name__ , __magic_name__ , return_labels=__magic_name__ ) SCREAMING_SNAKE_CASE_ = model(**__magic_name__ ).loss loss.backward() def __A ( self : Any ) -> Tuple: SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = self.model_tester.prepare_config_and_inputs_for_common() if not self.model_tester.is_training: return SCREAMING_SNAKE_CASE_ = False SCREAMING_SNAKE_CASE_ = True for model_class in self.all_model_classes: # we don't test BeitForMaskedImageModeling if ( model_class in [*get_values(__magic_name__ ), BeitForMaskedImageModeling] or not model_class.supports_gradient_checkpointing ): continue SCREAMING_SNAKE_CASE_ = model_class(__magic_name__ ) model.gradient_checkpointing_enable() model.to(__magic_name__ ) model.train() SCREAMING_SNAKE_CASE_ = self._prepare_for_class(__magic_name__ , __magic_name__ , return_labels=__magic_name__ ) SCREAMING_SNAKE_CASE_ = model(**__magic_name__ ).loss loss.backward() def __A ( self : List[str] ) -> str: SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = self.model_tester.prepare_config_and_inputs_for_common() SCREAMING_SNAKE_CASE_ = _config_zero_init(__magic_name__ ) for model_class in self.all_model_classes: SCREAMING_SNAKE_CASE_ = model_class(config=__magic_name__ ) for name, param in model.named_parameters(): # we skip lambda parameters as these require special initial values # determined by config.layer_scale_init_value if "lambda" in name: continue if param.requires_grad: self.assertIn( ((param.data.mean() * 1e9).round() / 1e9).item() , [0.0, 1.0] , msg=F'''Parameter {name} of model {model_class} seems not properly initialized''' , ) @slow def __A ( self : int ) -> Optional[int]: for model_name in BEIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: SCREAMING_SNAKE_CASE_ = BeitModel.from_pretrained(__magic_name__ ) self.assertIsNotNone(__magic_name__ ) def a__ ( ): SCREAMING_SNAKE_CASE_ = Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png" ) return image @require_torch @require_vision class lowerCamelCase (unittest.TestCase ): """simple docstring""" @cached_property def __A ( self : List[Any] ) -> str: return BeitImageProcessor.from_pretrained("microsoft/beit-base-patch16-224" ) if is_vision_available() else None @slow def __A ( self : List[str] ) -> Union[str, Any]: SCREAMING_SNAKE_CASE_ = BeitForMaskedImageModeling.from_pretrained("microsoft/beit-base-patch16-224-pt22k" ).to(__magic_name__ ) SCREAMING_SNAKE_CASE_ = self.default_image_processor SCREAMING_SNAKE_CASE_ = prepare_img() SCREAMING_SNAKE_CASE_ = image_processor(images=__magic_name__ , return_tensors="pt" ).pixel_values.to(__magic_name__ ) # prepare bool_masked_pos SCREAMING_SNAKE_CASE_ = torch.ones((1, 196) , dtype=torch.bool ).to(__magic_name__ ) # forward pass with torch.no_grad(): SCREAMING_SNAKE_CASE_ = model(pixel_values=__magic_name__ , bool_masked_pos=__magic_name__ ) SCREAMING_SNAKE_CASE_ = outputs.logits # verify the logits SCREAMING_SNAKE_CASE_ = torch.Size((1, 196, 8_192) ) self.assertEqual(logits.shape , __magic_name__ ) SCREAMING_SNAKE_CASE_ = torch.tensor( [[-3.2437, 0.5072, -13.9174], [-3.2456, 0.4948, -13.9401], [-3.2033, 0.5121, -13.8550]] ).to(__magic_name__ ) self.assertTrue(torch.allclose(logits[bool_masked_pos][:3, :3] , __magic_name__ , atol=1e-2 ) ) @slow def __A ( self : Tuple ) -> int: SCREAMING_SNAKE_CASE_ = BeitForImageClassification.from_pretrained("microsoft/beit-base-patch16-224" ).to(__magic_name__ ) SCREAMING_SNAKE_CASE_ = self.default_image_processor SCREAMING_SNAKE_CASE_ = prepare_img() SCREAMING_SNAKE_CASE_ = image_processor(images=__magic_name__ , return_tensors="pt" ).to(__magic_name__ ) # forward pass with torch.no_grad(): SCREAMING_SNAKE_CASE_ = model(**__magic_name__ ) SCREAMING_SNAKE_CASE_ = outputs.logits # verify the logits SCREAMING_SNAKE_CASE_ = torch.Size((1, 1_000) ) self.assertEqual(logits.shape , __magic_name__ ) SCREAMING_SNAKE_CASE_ = torch.tensor([-1.2385, -1.0987, -1.0108] ).to(__magic_name__ ) self.assertTrue(torch.allclose(logits[0, :3] , __magic_name__ , atol=1e-4 ) ) SCREAMING_SNAKE_CASE_ = 281 self.assertEqual(logits.argmax(-1 ).item() , __magic_name__ ) @slow def __A ( self : Optional[Any] ) -> int: SCREAMING_SNAKE_CASE_ = BeitForImageClassification.from_pretrained("microsoft/beit-large-patch16-224-pt22k-ft22k" ).to( __magic_name__ ) SCREAMING_SNAKE_CASE_ = self.default_image_processor SCREAMING_SNAKE_CASE_ = prepare_img() SCREAMING_SNAKE_CASE_ = image_processor(images=__magic_name__ , return_tensors="pt" ).to(__magic_name__ ) # forward pass with torch.no_grad(): SCREAMING_SNAKE_CASE_ = model(**__magic_name__ ) SCREAMING_SNAKE_CASE_ = outputs.logits # verify the logits SCREAMING_SNAKE_CASE_ = torch.Size((1, 21_841) ) self.assertEqual(logits.shape , __magic_name__ ) SCREAMING_SNAKE_CASE_ = torch.tensor([1.6881, -0.2787, 0.5901] ).to(__magic_name__ ) self.assertTrue(torch.allclose(logits[0, :3] , __magic_name__ , atol=1e-4 ) ) SCREAMING_SNAKE_CASE_ = 2_396 self.assertEqual(logits.argmax(-1 ).item() , __magic_name__ ) @slow def __A ( self : Tuple ) -> Tuple: SCREAMING_SNAKE_CASE_ = BeitForSemanticSegmentation.from_pretrained("microsoft/beit-base-finetuned-ade-640-640" ) SCREAMING_SNAKE_CASE_ = model.to(__magic_name__ ) SCREAMING_SNAKE_CASE_ = BeitImageProcessor(do_resize=__magic_name__ , size=640 , do_center_crop=__magic_name__ ) SCREAMING_SNAKE_CASE_ = load_dataset("hf-internal-testing/fixtures_ade20k" , split="test" ) SCREAMING_SNAKE_CASE_ = Image.open(ds[0]["file"] ) SCREAMING_SNAKE_CASE_ = image_processor(images=__magic_name__ , return_tensors="pt" ).to(__magic_name__ ) # forward pass with torch.no_grad(): SCREAMING_SNAKE_CASE_ = model(**__magic_name__ ) SCREAMING_SNAKE_CASE_ = outputs.logits # verify the logits SCREAMING_SNAKE_CASE_ = torch.Size((1, 150, 160, 160) ) self.assertEqual(logits.shape , __magic_name__ ) SCREAMING_SNAKE_CASE_ = version.parse(PIL.__version__ ) < version.parse("9.0.0" ) if is_pillow_less_than_a: SCREAMING_SNAKE_CASE_ = torch.tensor( [ [[-4.9225, -2.3954, -3.0522], [-2.8822, -1.0046, -1.7561], [-2.9549, -1.3228, -2.1347]], [[-5.8168, -3.4129, -4.0778], [-3.8651, -2.2214, -3.0277], [-3.8356, -2.4643, -3.3535]], [[-0.0078, 3.9952, 4.0754], [2.9856, 4.6944, 5.0035], [3.2413, 4.7813, 4.9969]], ] , device=__magic_name__ , ) else: SCREAMING_SNAKE_CASE_ = torch.tensor( [ [[-4.8960, -2.3688, -3.0355], [-2.8478, -0.9836, -1.7418], [-2.9449, -1.3332, -2.1456]], [[-5.8081, -3.4124, -4.1006], [-3.8561, -2.2081, -3.0323], [-3.8365, -2.4601, -3.3669]], [[-0.0309, 3.9868, 4.0540], [2.9640, 4.6877, 4.9976], [3.2081, 4.7690, 4.9942]], ] , device=__magic_name__ , ) self.assertTrue(torch.allclose(logits[0, :3, :3, :3] , __magic_name__ , atol=1e-4 ) ) @slow def __A ( self : List[str] ) -> Tuple: SCREAMING_SNAKE_CASE_ = BeitForSemanticSegmentation.from_pretrained("microsoft/beit-base-finetuned-ade-640-640" ) SCREAMING_SNAKE_CASE_ = model.to(__magic_name__ ) SCREAMING_SNAKE_CASE_ = BeitImageProcessor(do_resize=__magic_name__ , size=640 , do_center_crop=__magic_name__ ) SCREAMING_SNAKE_CASE_ = load_dataset("hf-internal-testing/fixtures_ade20k" , split="test" ) SCREAMING_SNAKE_CASE_ = Image.open(ds[0]["file"] ) SCREAMING_SNAKE_CASE_ = image_processor(images=__magic_name__ , return_tensors="pt" ).to(__magic_name__ ) # forward pass with torch.no_grad(): SCREAMING_SNAKE_CASE_ = model(**__magic_name__ ) SCREAMING_SNAKE_CASE_ = outputs.logits.detach().cpu() SCREAMING_SNAKE_CASE_ = image_processor.post_process_semantic_segmentation(outputs=__magic_name__ , target_sizes=[(500, 300)] ) SCREAMING_SNAKE_CASE_ = torch.Size((500, 300) ) self.assertEqual(segmentation[0].shape , __magic_name__ ) SCREAMING_SNAKE_CASE_ = image_processor.post_process_semantic_segmentation(outputs=__magic_name__ ) SCREAMING_SNAKE_CASE_ = torch.Size((160, 160) ) self.assertEqual(segmentation[0].shape , __magic_name__ )
305
0
"""simple docstring""" import gc import unittest import numpy as np import torch from torch.backends.cuda import sdp_kernel from diffusers import ( CMStochasticIterativeScheduler, ConsistencyModelPipeline, UNetaDModel, ) from diffusers.utils import randn_tensor, slow, torch_device from diffusers.utils.testing_utils import enable_full_determinism, require_torch_a, require_torch_gpu from ..pipeline_params import UNCONDITIONAL_IMAGE_GENERATION_BATCH_PARAMS, UNCONDITIONAL_IMAGE_GENERATION_PARAMS from ..test_pipelines_common import PipelineTesterMixin enable_full_determinism() class lowerCamelCase (SCREAMING_SNAKE_CASE__ , unittest.TestCase ): """simple docstring""" lowerCamelCase__ = ConsistencyModelPipeline lowerCamelCase__ = UNCONDITIONAL_IMAGE_GENERATION_PARAMS lowerCamelCase__ = UNCONDITIONAL_IMAGE_GENERATION_BATCH_PARAMS # Override required_optional_params to remove num_images_per_prompt lowerCamelCase__ = frozenset( [ '''num_inference_steps''', '''generator''', '''latents''', '''output_type''', '''return_dict''', '''callback''', '''callback_steps''', ] ) @property def __A ( self : Dict ) -> Union[str, Any]: SCREAMING_SNAKE_CASE_ = UNetaDModel.from_pretrained( "diffusers/consistency-models-test" , subfolder="test_unet" , ) return unet @property def __A ( self : List[Any] ) -> str: SCREAMING_SNAKE_CASE_ = UNetaDModel.from_pretrained( "diffusers/consistency-models-test" , subfolder="test_unet_class_cond" , ) return unet def __A ( self : Dict , __magic_name__ : Optional[Any]=False ) -> List[Any]: if class_cond: SCREAMING_SNAKE_CASE_ = self.dummy_cond_unet else: SCREAMING_SNAKE_CASE_ = self.dummy_uncond_unet # Default to CM multistep sampler SCREAMING_SNAKE_CASE_ = CMStochasticIterativeScheduler( num_train_timesteps=40 , sigma_min=0.002 , sigma_max=80.0 , ) SCREAMING_SNAKE_CASE_ = { "unet": unet, "scheduler": scheduler, } return components def __A ( self : str , __magic_name__ : Optional[int] , __magic_name__ : Optional[Any]=0 ) -> Union[str, Any]: if str(__UpperCAmelCase ).startswith("mps" ): SCREAMING_SNAKE_CASE_ = torch.manual_seed(__UpperCAmelCase ) else: SCREAMING_SNAKE_CASE_ = torch.Generator(device=__UpperCAmelCase ).manual_seed(__UpperCAmelCase ) SCREAMING_SNAKE_CASE_ = { "batch_size": 1, "num_inference_steps": None, "timesteps": [22, 0], "generator": generator, "output_type": "np", } return inputs def __A ( self : Tuple ) -> Any: SCREAMING_SNAKE_CASE_ = "cpu" # ensure determinism for the device-dependent torch.Generator SCREAMING_SNAKE_CASE_ = self.get_dummy_components() SCREAMING_SNAKE_CASE_ = ConsistencyModelPipeline(**__UpperCAmelCase ) SCREAMING_SNAKE_CASE_ = pipe.to(__UpperCAmelCase ) pipe.set_progress_bar_config(disable=__UpperCAmelCase ) SCREAMING_SNAKE_CASE_ = self.get_dummy_inputs(__UpperCAmelCase ) SCREAMING_SNAKE_CASE_ = pipe(**__UpperCAmelCase ).images assert image.shape == (1, 32, 32, 3) SCREAMING_SNAKE_CASE_ = image[0, -3:, -3:, -1] SCREAMING_SNAKE_CASE_ = np.array([0.3572, 0.6273, 0.4031, 0.3961, 0.4321, 0.5730, 0.5266, 0.4780, 0.5004] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-3 def __A ( self : Optional[int] ) -> List[str]: SCREAMING_SNAKE_CASE_ = "cpu" # ensure determinism for the device-dependent torch.Generator SCREAMING_SNAKE_CASE_ = self.get_dummy_components(class_cond=__UpperCAmelCase ) SCREAMING_SNAKE_CASE_ = ConsistencyModelPipeline(**__UpperCAmelCase ) SCREAMING_SNAKE_CASE_ = pipe.to(__UpperCAmelCase ) pipe.set_progress_bar_config(disable=__UpperCAmelCase ) SCREAMING_SNAKE_CASE_ = self.get_dummy_inputs(__UpperCAmelCase ) SCREAMING_SNAKE_CASE_ = 0 SCREAMING_SNAKE_CASE_ = pipe(**__UpperCAmelCase ).images assert image.shape == (1, 32, 32, 3) SCREAMING_SNAKE_CASE_ = image[0, -3:, -3:, -1] SCREAMING_SNAKE_CASE_ = np.array([0.3572, 0.6273, 0.4031, 0.3961, 0.4321, 0.5730, 0.5266, 0.4780, 0.5004] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-3 def __A ( self : List[Any] ) -> str: SCREAMING_SNAKE_CASE_ = "cpu" # ensure determinism for the device-dependent torch.Generator SCREAMING_SNAKE_CASE_ = self.get_dummy_components() SCREAMING_SNAKE_CASE_ = ConsistencyModelPipeline(**__UpperCAmelCase ) SCREAMING_SNAKE_CASE_ = pipe.to(__UpperCAmelCase ) pipe.set_progress_bar_config(disable=__UpperCAmelCase ) SCREAMING_SNAKE_CASE_ = self.get_dummy_inputs(__UpperCAmelCase ) SCREAMING_SNAKE_CASE_ = 1 SCREAMING_SNAKE_CASE_ = None SCREAMING_SNAKE_CASE_ = pipe(**__UpperCAmelCase ).images assert image.shape == (1, 32, 32, 3) SCREAMING_SNAKE_CASE_ = image[0, -3:, -3:, -1] SCREAMING_SNAKE_CASE_ = np.array([0.5004, 0.5004, 0.4994, 0.5008, 0.4976, 0.5018, 0.4990, 0.4982, 0.4987] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-3 def __A ( self : str ) -> int: SCREAMING_SNAKE_CASE_ = "cpu" # ensure determinism for the device-dependent torch.Generator SCREAMING_SNAKE_CASE_ = self.get_dummy_components(class_cond=__UpperCAmelCase ) SCREAMING_SNAKE_CASE_ = ConsistencyModelPipeline(**__UpperCAmelCase ) SCREAMING_SNAKE_CASE_ = pipe.to(__UpperCAmelCase ) pipe.set_progress_bar_config(disable=__UpperCAmelCase ) SCREAMING_SNAKE_CASE_ = self.get_dummy_inputs(__UpperCAmelCase ) SCREAMING_SNAKE_CASE_ = 1 SCREAMING_SNAKE_CASE_ = None SCREAMING_SNAKE_CASE_ = 0 SCREAMING_SNAKE_CASE_ = pipe(**__UpperCAmelCase ).images assert image.shape == (1, 32, 32, 3) SCREAMING_SNAKE_CASE_ = image[0, -3:, -3:, -1] SCREAMING_SNAKE_CASE_ = np.array([0.5004, 0.5004, 0.4994, 0.5008, 0.4976, 0.5018, 0.4990, 0.4982, 0.4987] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-3 @slow @require_torch_gpu class lowerCamelCase (unittest.TestCase ): """simple docstring""" def __A ( self : Union[str, Any] ) -> Any: super().tearDown() gc.collect() torch.cuda.empty_cache() def __A ( self : Tuple , __magic_name__ : Dict=0 , __magic_name__ : Union[str, Any]=False , __magic_name__ : Dict="cpu" , __magic_name__ : str=torch.floataa , __magic_name__ : List[Any]=(1, 3, 64, 64) ) -> List[str]: SCREAMING_SNAKE_CASE_ = torch.manual_seed(__UpperCAmelCase ) SCREAMING_SNAKE_CASE_ = { "num_inference_steps": None, "timesteps": [22, 0], "class_labels": 0, "generator": generator, "output_type": "np", } if get_fixed_latents: SCREAMING_SNAKE_CASE_ = self.get_fixed_latents(seed=__UpperCAmelCase , device=__UpperCAmelCase , dtype=__UpperCAmelCase , shape=__UpperCAmelCase ) SCREAMING_SNAKE_CASE_ = latents return inputs def __A ( self : Union[str, Any] , __magic_name__ : List[str]=0 , __magic_name__ : List[str]="cpu" , __magic_name__ : Optional[int]=torch.floataa , __magic_name__ : Tuple=(1, 3, 64, 64) ) -> List[Any]: if type(__UpperCAmelCase ) == str: SCREAMING_SNAKE_CASE_ = torch.device(__UpperCAmelCase ) SCREAMING_SNAKE_CASE_ = torch.Generator(device=__UpperCAmelCase ).manual_seed(__UpperCAmelCase ) SCREAMING_SNAKE_CASE_ = randn_tensor(__UpperCAmelCase , generator=__UpperCAmelCase , device=__UpperCAmelCase , dtype=__UpperCAmelCase ) return latents def __A ( self : Dict ) -> Any: SCREAMING_SNAKE_CASE_ = UNetaDModel.from_pretrained("diffusers/consistency_models" , subfolder="diffusers_cd_imagenet64_l2" ) SCREAMING_SNAKE_CASE_ = CMStochasticIterativeScheduler( num_train_timesteps=40 , sigma_min=0.002 , sigma_max=80.0 , ) SCREAMING_SNAKE_CASE_ = ConsistencyModelPipeline(unet=__UpperCAmelCase , scheduler=__UpperCAmelCase ) pipe.to(torch_device=__UpperCAmelCase ) pipe.set_progress_bar_config(disable=__UpperCAmelCase ) SCREAMING_SNAKE_CASE_ = self.get_inputs() SCREAMING_SNAKE_CASE_ = pipe(**__UpperCAmelCase ).images assert image.shape == (1, 64, 64, 3) SCREAMING_SNAKE_CASE_ = image[0, -3:, -3:, -1] SCREAMING_SNAKE_CASE_ = np.array([0.0888, 0.0881, 0.0666, 0.0479, 0.0292, 0.0195, 0.0201, 0.0163, 0.0254] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 2e-2 def __A ( self : Dict ) -> List[Any]: SCREAMING_SNAKE_CASE_ = UNetaDModel.from_pretrained("diffusers/consistency_models" , subfolder="diffusers_cd_imagenet64_l2" ) SCREAMING_SNAKE_CASE_ = CMStochasticIterativeScheduler( num_train_timesteps=40 , sigma_min=0.002 , sigma_max=80.0 , ) SCREAMING_SNAKE_CASE_ = ConsistencyModelPipeline(unet=__UpperCAmelCase , scheduler=__UpperCAmelCase ) pipe.to(torch_device=__UpperCAmelCase ) pipe.set_progress_bar_config(disable=__UpperCAmelCase ) SCREAMING_SNAKE_CASE_ = self.get_inputs() SCREAMING_SNAKE_CASE_ = 1 SCREAMING_SNAKE_CASE_ = None SCREAMING_SNAKE_CASE_ = pipe(**__UpperCAmelCase ).images assert image.shape == (1, 64, 64, 3) SCREAMING_SNAKE_CASE_ = image[0, -3:, -3:, -1] SCREAMING_SNAKE_CASE_ = np.array([0.0340, 0.0152, 0.0063, 0.0267, 0.0221, 0.0107, 0.0416, 0.0186, 0.0217] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 2e-2 @require_torch_a def __A ( self : Dict ) -> Dict: SCREAMING_SNAKE_CASE_ = UNetaDModel.from_pretrained("diffusers/consistency_models" , subfolder="diffusers_cd_imagenet64_l2" ) SCREAMING_SNAKE_CASE_ = CMStochasticIterativeScheduler( num_train_timesteps=40 , sigma_min=0.002 , sigma_max=80.0 , ) SCREAMING_SNAKE_CASE_ = ConsistencyModelPipeline(unet=__UpperCAmelCase , scheduler=__UpperCAmelCase ) pipe.to(torch_device=__UpperCAmelCase , torch_dtype=torch.floataa ) pipe.set_progress_bar_config(disable=__UpperCAmelCase ) SCREAMING_SNAKE_CASE_ = self.get_inputs(get_fixed_latents=__UpperCAmelCase , device=__UpperCAmelCase ) # Ensure usage of flash attention in torch 2.0 with sdp_kernel(enable_flash=__UpperCAmelCase , enable_math=__UpperCAmelCase , enable_mem_efficient=__UpperCAmelCase ): SCREAMING_SNAKE_CASE_ = pipe(**__UpperCAmelCase ).images assert image.shape == (1, 64, 64, 3) SCREAMING_SNAKE_CASE_ = image[0, -3:, -3:, -1] SCREAMING_SNAKE_CASE_ = np.array([0.1875, 0.1428, 0.1289, 0.2151, 0.2092, 0.1477, 0.1877, 0.1641, 0.1353] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-3 @require_torch_a def __A ( self : Optional[Any] ) -> int: SCREAMING_SNAKE_CASE_ = UNetaDModel.from_pretrained("diffusers/consistency_models" , subfolder="diffusers_cd_imagenet64_l2" ) SCREAMING_SNAKE_CASE_ = CMStochasticIterativeScheduler( num_train_timesteps=40 , sigma_min=0.002 , sigma_max=80.0 , ) SCREAMING_SNAKE_CASE_ = ConsistencyModelPipeline(unet=__UpperCAmelCase , scheduler=__UpperCAmelCase ) pipe.to(torch_device=__UpperCAmelCase , torch_dtype=torch.floataa ) pipe.set_progress_bar_config(disable=__UpperCAmelCase ) SCREAMING_SNAKE_CASE_ = self.get_inputs(get_fixed_latents=__UpperCAmelCase , device=__UpperCAmelCase ) SCREAMING_SNAKE_CASE_ = 1 SCREAMING_SNAKE_CASE_ = None # Ensure usage of flash attention in torch 2.0 with sdp_kernel(enable_flash=__UpperCAmelCase , enable_math=__UpperCAmelCase , enable_mem_efficient=__UpperCAmelCase ): SCREAMING_SNAKE_CASE_ = pipe(**__UpperCAmelCase ).images assert image.shape == (1, 64, 64, 3) SCREAMING_SNAKE_CASE_ = image[0, -3:, -3:, -1] SCREAMING_SNAKE_CASE_ = np.array([0.1663, 0.1948, 0.2275, 0.1680, 0.1204, 0.1245, 0.1858, 0.1338, 0.2095] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-3
354
from __future__ import annotations A : Dict = "#" class lowerCamelCase : """simple docstring""" def __init__( self : Dict ) -> None: SCREAMING_SNAKE_CASE_ = {} def __A ( self : List[Any] , __magic_name__ : str ) -> None: SCREAMING_SNAKE_CASE_ = self._trie for char in text: if char not in trie: SCREAMING_SNAKE_CASE_ = {} SCREAMING_SNAKE_CASE_ = trie[char] SCREAMING_SNAKE_CASE_ = True def __A ( self : Union[str, Any] , __magic_name__ : str ) -> tuple | list: SCREAMING_SNAKE_CASE_ = self._trie for char in prefix: if char in trie: SCREAMING_SNAKE_CASE_ = trie[char] else: return [] return self._elements(__magic_name__ ) def __A ( self : int , __magic_name__ : dict ) -> tuple: SCREAMING_SNAKE_CASE_ = [] for c, v in d.items(): SCREAMING_SNAKE_CASE_ = [" "] if c == END else [(c + s) for s in self._elements(__magic_name__ )] result.extend(__magic_name__ ) return tuple(__magic_name__ ) A : Union[str, Any] = Trie() A : Optional[int] = ("depart", "detergent", "daring", "dog", "deer", "deal") for word in words: trie.insert_word(word) def a__ ( __UpperCamelCase ): SCREAMING_SNAKE_CASE_ = trie.find_word(__UpperCamelCase ) return tuple(string + word for word in suffixes ) def a__ ( ): print(autocomplete_using_trie("de" ) ) if __name__ == "__main__": import doctest doctest.testmod() main()
305
0
def a__ ( __UpperCamelCase ): if number < 0: raise ValueError("number must not be negative" ) return number & (number - 1) == 0 if __name__ == "__main__": import doctest doctest.testmod()
355
from collections import deque class lowerCamelCase : """simple docstring""" def __init__( self : str , __magic_name__ : str , __magic_name__ : int , __magic_name__ : int ) -> None: SCREAMING_SNAKE_CASE_ = process_name # process name SCREAMING_SNAKE_CASE_ = arrival_time # arrival time of the process # completion time of finished process or last interrupted time SCREAMING_SNAKE_CASE_ = arrival_time SCREAMING_SNAKE_CASE_ = burst_time # remaining burst time SCREAMING_SNAKE_CASE_ = 0 # total time of the process wait in ready queue SCREAMING_SNAKE_CASE_ = 0 # time from arrival time to completion time class lowerCamelCase : """simple docstring""" def __init__( self : Tuple , __magic_name__ : int , __magic_name__ : list[int] , __magic_name__ : deque[Process] , __magic_name__ : int , ) -> None: # total number of mlfq's queues SCREAMING_SNAKE_CASE_ = number_of_queues # time slice of queues that round robin algorithm applied SCREAMING_SNAKE_CASE_ = time_slices # unfinished process is in this ready_queue SCREAMING_SNAKE_CASE_ = queue # current time SCREAMING_SNAKE_CASE_ = current_time # finished process is in this sequence queue SCREAMING_SNAKE_CASE_ = deque() def __A ( self : Dict ) -> list[str]: SCREAMING_SNAKE_CASE_ = [] for i in range(len(self.finish_queue ) ): sequence.append(self.finish_queue[i].process_name ) return sequence def __A ( self : List[str] , __magic_name__ : list[Process] ) -> list[int]: SCREAMING_SNAKE_CASE_ = [] for i in range(len(__magic_name__ ) ): waiting_times.append(queue[i].waiting_time ) return waiting_times def __A ( self : List[str] , __magic_name__ : list[Process] ) -> list[int]: SCREAMING_SNAKE_CASE_ = [] for i in range(len(__magic_name__ ) ): turnaround_times.append(queue[i].turnaround_time ) return turnaround_times def __A ( self : Tuple , __magic_name__ : list[Process] ) -> list[int]: SCREAMING_SNAKE_CASE_ = [] for i in range(len(__magic_name__ ) ): completion_times.append(queue[i].stop_time ) return completion_times def __A ( self : str , __magic_name__ : deque[Process] ) -> list[int]: return [q.burst_time for q in queue] def __A ( self : Optional[Any] , __magic_name__ : Process ) -> int: process.waiting_time += self.current_time - process.stop_time return process.waiting_time def __A ( self : Optional[Any] , __magic_name__ : deque[Process] ) -> deque[Process]: SCREAMING_SNAKE_CASE_ = deque() # sequence deque of finished process while len(__magic_name__ ) != 0: SCREAMING_SNAKE_CASE_ = ready_queue.popleft() # current process # if process's arrival time is later than current time, update current time if self.current_time < cp.arrival_time: self.current_time += cp.arrival_time # update waiting time of current process self.update_waiting_time(__magic_name__ ) # update current time self.current_time += cp.burst_time # finish the process and set the process's burst-time 0 SCREAMING_SNAKE_CASE_ = 0 # set the process's turnaround time because it is finished SCREAMING_SNAKE_CASE_ = self.current_time - cp.arrival_time # set the completion time SCREAMING_SNAKE_CASE_ = self.current_time # add the process to queue that has finished queue finished.append(__magic_name__ ) self.finish_queue.extend(__magic_name__ ) # add finished process to finish queue # FCFS will finish all remaining processes return finished def __A ( self : Any , __magic_name__ : deque[Process] , __magic_name__ : int ) -> tuple[deque[Process], deque[Process]]: SCREAMING_SNAKE_CASE_ = deque() # sequence deque of terminated process # just for 1 cycle and unfinished processes will go back to queue for _ in range(len(__magic_name__ ) ): SCREAMING_SNAKE_CASE_ = ready_queue.popleft() # current process # if process's arrival time is later than current time, update current time if self.current_time < cp.arrival_time: self.current_time += cp.arrival_time # update waiting time of unfinished processes self.update_waiting_time(__magic_name__ ) # if the burst time of process is bigger than time-slice if cp.burst_time > time_slice: # use CPU for only time-slice self.current_time += time_slice # update remaining burst time cp.burst_time -= time_slice # update end point time SCREAMING_SNAKE_CASE_ = self.current_time # locate the process behind the queue because it is not finished ready_queue.append(__magic_name__ ) else: # use CPU for remaining burst time self.current_time += cp.burst_time # set burst time 0 because the process is finished SCREAMING_SNAKE_CASE_ = 0 # set the finish time SCREAMING_SNAKE_CASE_ = self.current_time # update the process' turnaround time because it is finished SCREAMING_SNAKE_CASE_ = self.current_time - cp.arrival_time # add the process to queue that has finished queue finished.append(__magic_name__ ) self.finish_queue.extend(__magic_name__ ) # add finished process to finish queue # return finished processes queue and remaining processes queue return finished, ready_queue def __A ( self : Any ) -> deque[Process]: # all queues except last one have round_robin algorithm for i in range(self.number_of_queues - 1 ): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = self.round_robin( self.ready_queue , self.time_slices[i] ) # the last queue has first_come_first_served algorithm self.first_come_first_served(self.ready_queue ) return self.finish_queue if __name__ == "__main__": import doctest A : Dict = Process("P1", 0, 53) A : str = Process("P2", 0, 17) A : List[Any] = Process("P3", 0, 68) A : List[str] = Process("P4", 0, 24) A : Dict = 3 A : Any = [17, 25] A : Dict = deque([Pa, Pa, Pa, Pa]) if len(time_slices) != number_of_queues - 1: raise SystemExit(0) doctest.testmod(extraglobs={"queue": deque([Pa, Pa, Pa, Pa])}) A : Union[str, Any] = Process("P1", 0, 53) A : Any = Process("P2", 0, 17) A : Dict = Process("P3", 0, 68) A : List[str] = Process("P4", 0, 24) A : Optional[int] = 3 A : int = [17, 25] A : Union[str, Any] = deque([Pa, Pa, Pa, Pa]) A : Tuple = MLFQ(number_of_queues, time_slices, queue, 0) A : Tuple = mlfq.multi_level_feedback_queue() # print total waiting times of processes(P1, P2, P3, P4) print( f"waiting time:\ \t\t\t{MLFQ.calculate_waiting_time(mlfq, [Pa, Pa, Pa, Pa])}" ) # print completion times of processes(P1, P2, P3, P4) print( f"completion time:\ \t\t{MLFQ.calculate_completion_time(mlfq, [Pa, Pa, Pa, Pa])}" ) # print total turnaround times of processes(P1, P2, P3, P4) print( f"turnaround time:\ \t\t{MLFQ.calculate_turnaround_time(mlfq, [Pa, Pa, Pa, Pa])}" ) # print sequence of finished processes print( f"sequence of finished processes:\ {mlfq.calculate_sequence_of_finish_queue()}" )
305
0
"""simple docstring""" 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_tf_available(): import tensorflow as tf from ..models.auto.modeling_tf_auto import TF_MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING from ..tf_utils import stable_softmax if is_torch_available(): from ..models.auto.modeling_auto import MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING A : Tuple = logging.get_logger(__name__) @add_end_docstrings(__UpperCamelCase ) class lowerCamelCase (__UpperCamelCase ): """simple docstring""" def __init__( self : List[str] , *__magic_name__ : int , **__magic_name__ : str ) -> Union[str, Any]: super().__init__(*__magic_name__ , **__magic_name__ ) requires_backends(self , "vision" ) self.check_model_type( TF_MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING if self.framework == "tf" else MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING ) def __A ( self : Tuple , __magic_name__ : int=None ) -> str: SCREAMING_SNAKE_CASE_ = {} if top_k is not None: SCREAMING_SNAKE_CASE_ = top_k return {}, {}, postprocess_params def __call__( self : Union[str, Any] , __magic_name__ : Union[str, List[str], "Image.Image", List["Image.Image"]] , **__magic_name__ : Any ) -> int: return super().__call__(__magic_name__ , **__magic_name__ ) def __A ( self : Dict , __magic_name__ : Optional[int] ) -> Optional[Any]: SCREAMING_SNAKE_CASE_ = load_image(__magic_name__ ) SCREAMING_SNAKE_CASE_ = self.image_processor(images=__magic_name__ , return_tensors=self.framework ) return model_inputs def __A ( self : Dict , __magic_name__ : Tuple ) -> List[str]: SCREAMING_SNAKE_CASE_ = self.model(**__magic_name__ ) return model_outputs def __A ( self : Tuple , __magic_name__ : int , __magic_name__ : List[Any]=5 ) -> int: if top_k > self.model.config.num_labels: SCREAMING_SNAKE_CASE_ = self.model.config.num_labels if self.framework == "pt": SCREAMING_SNAKE_CASE_ = model_outputs.logits.softmax(-1 )[0] SCREAMING_SNAKE_CASE_ = probs.topk(__magic_name__ ) elif self.framework == "tf": SCREAMING_SNAKE_CASE_ = stable_softmax(model_outputs.logits , axis=-1 )[0] SCREAMING_SNAKE_CASE_ = tf.math.top_k(__magic_name__ , k=__magic_name__ ) SCREAMING_SNAKE_CASE_ = topk.values.numpy(), topk.indices.numpy() else: raise ValueError(F'''Unsupported framework: {self.framework}''' ) SCREAMING_SNAKE_CASE_ = scores.tolist() SCREAMING_SNAKE_CASE_ = ids.tolist() return [{"score": score, "label": self.model.config.idalabel[_id]} for score, _id in zip(__magic_name__ , __magic_name__ )]
356
import torch def a__ ( ): if torch.cuda.is_available(): SCREAMING_SNAKE_CASE_ = torch.cuda.device_count() else: SCREAMING_SNAKE_CASE_ = 0 print(F'''Successfully ran on {num_gpus} GPUs''' ) if __name__ == "__main__": main()
305
0
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_tf_available, is_tokenizers_available, is_torch_available, ) A : List[str] = { "configuration_deberta": ["DEBERTA_PRETRAINED_CONFIG_ARCHIVE_MAP", "DebertaConfig", "DebertaOnnxConfig"], "tokenization_deberta": ["DebertaTokenizer"], } try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: A : Any = ["DebertaTokenizerFast"] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: A : List[Any] = [ "DEBERTA_PRETRAINED_MODEL_ARCHIVE_LIST", "DebertaForMaskedLM", "DebertaForQuestionAnswering", "DebertaForSequenceClassification", "DebertaForTokenClassification", "DebertaModel", "DebertaPreTrainedModel", ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: A : int = [ "TF_DEBERTA_PRETRAINED_MODEL_ARCHIVE_LIST", "TFDebertaForMaskedLM", "TFDebertaForQuestionAnswering", "TFDebertaForSequenceClassification", "TFDebertaForTokenClassification", "TFDebertaModel", "TFDebertaPreTrainedModel", ] if TYPE_CHECKING: from .configuration_deberta import DEBERTA_PRETRAINED_CONFIG_ARCHIVE_MAP, DebertaConfig, DebertaOnnxConfig from .tokenization_deberta import DebertaTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_deberta_fast import DebertaTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_deberta import ( DEBERTA_PRETRAINED_MODEL_ARCHIVE_LIST, DebertaForMaskedLM, DebertaForQuestionAnswering, DebertaForSequenceClassification, DebertaForTokenClassification, DebertaModel, DebertaPreTrainedModel, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_deberta import ( TF_DEBERTA_PRETRAINED_MODEL_ARCHIVE_LIST, TFDebertaForMaskedLM, TFDebertaForQuestionAnswering, TFDebertaForSequenceClassification, TFDebertaForTokenClassification, TFDebertaModel, TFDebertaPreTrainedModel, ) else: import sys A : Tuple = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
357
from collections.abc import Generator from math import sin def a__ ( __UpperCamelCase ): if len(__UpperCamelCase ) != 3_2: raise ValueError("Input must be of length 32" ) SCREAMING_SNAKE_CASE_ = b"" for i in [3, 2, 1, 0]: little_endian += string_aa[8 * i : 8 * i + 8] return little_endian def a__ ( __UpperCamelCase ): if i < 0: raise ValueError("Input must be non-negative" ) SCREAMING_SNAKE_CASE_ = format(__UpperCamelCase , "08x" )[-8:] SCREAMING_SNAKE_CASE_ = b"" for i in [3, 2, 1, 0]: little_endian_hex += hex_rep[2 * i : 2 * i + 2].encode("utf-8" ) return little_endian_hex def a__ ( __UpperCamelCase ): SCREAMING_SNAKE_CASE_ = b"" for char in message: bit_string += format(__UpperCamelCase , "08b" ).encode("utf-8" ) SCREAMING_SNAKE_CASE_ = format(len(__UpperCamelCase ) , "064b" ).encode("utf-8" ) # Pad bit_string to a multiple of 512 chars bit_string += b"1" while len(__UpperCamelCase ) % 5_1_2 != 4_4_8: bit_string += b"0" bit_string += to_little_endian(start_len[3_2:] ) + to_little_endian(start_len[:3_2] ) return bit_string def a__ ( __UpperCamelCase ): if len(__UpperCamelCase ) % 5_1_2 != 0: raise ValueError("Input must have length that's a multiple of 512" ) for pos in range(0 , len(__UpperCamelCase ) , 5_1_2 ): SCREAMING_SNAKE_CASE_ = bit_string[pos : pos + 5_1_2] SCREAMING_SNAKE_CASE_ = [] for i in range(0 , 5_1_2 , 3_2 ): block_words.append(int(to_little_endian(block[i : i + 3_2] ) , 2 ) ) yield block_words def a__ ( __UpperCamelCase ): if i < 0: raise ValueError("Input must be non-negative" ) SCREAMING_SNAKE_CASE_ = format(__UpperCamelCase , "032b" ) SCREAMING_SNAKE_CASE_ = "" for c in i_str: new_str += "1" if c == "0" else "0" return int(__UpperCamelCase , 2 ) def a__ ( __UpperCamelCase , __UpperCamelCase ): return (a + b) % 2**3_2 def a__ ( __UpperCamelCase , __UpperCamelCase ): if i < 0: raise ValueError("Input must be non-negative" ) if shift < 0: raise ValueError("Shift must be non-negative" ) return ((i << shift) ^ (i >> (3_2 - shift))) % 2**3_2 def a__ ( __UpperCamelCase ): SCREAMING_SNAKE_CASE_ = preprocess(__UpperCamelCase ) SCREAMING_SNAKE_CASE_ = [int(2**3_2 * abs(sin(i + 1 ) ) ) for i in range(6_4 )] # Starting states SCREAMING_SNAKE_CASE_ = 0X67452301 SCREAMING_SNAKE_CASE_ = 0Xefcdab89 SCREAMING_SNAKE_CASE_ = 0X98badcfe SCREAMING_SNAKE_CASE_ = 0X10325476 SCREAMING_SNAKE_CASE_ = [ 7, 1_2, 1_7, 2_2, 7, 1_2, 1_7, 2_2, 7, 1_2, 1_7, 2_2, 7, 1_2, 1_7, 2_2, 5, 9, 1_4, 2_0, 5, 9, 1_4, 2_0, 5, 9, 1_4, 2_0, 5, 9, 1_4, 2_0, 4, 1_1, 1_6, 2_3, 4, 1_1, 1_6, 2_3, 4, 1_1, 1_6, 2_3, 4, 1_1, 1_6, 2_3, 6, 1_0, 1_5, 2_1, 6, 1_0, 1_5, 2_1, 6, 1_0, 1_5, 2_1, 6, 1_0, 1_5, 2_1, ] # Process bit string in chunks, each with 16 32-char words for block_words in get_block_words(__UpperCamelCase ): SCREAMING_SNAKE_CASE_ = aa SCREAMING_SNAKE_CASE_ = ba SCREAMING_SNAKE_CASE_ = ca SCREAMING_SNAKE_CASE_ = da # Hash current chunk for i in range(6_4 ): if i <= 1_5: # f = (b & c) | (not_32(b) & d) # Alternate definition for f SCREAMING_SNAKE_CASE_ = d ^ (b & (c ^ d)) SCREAMING_SNAKE_CASE_ = i elif i <= 3_1: # f = (d & b) | (not_32(d) & c) # Alternate definition for f SCREAMING_SNAKE_CASE_ = c ^ (d & (b ^ c)) SCREAMING_SNAKE_CASE_ = (5 * i + 1) % 1_6 elif i <= 4_7: SCREAMING_SNAKE_CASE_ = b ^ c ^ d SCREAMING_SNAKE_CASE_ = (3 * i + 5) % 1_6 else: SCREAMING_SNAKE_CASE_ = c ^ (b | not_aa(__UpperCamelCase )) SCREAMING_SNAKE_CASE_ = (7 * i) % 1_6 SCREAMING_SNAKE_CASE_ = (f + a + added_consts[i] + block_words[g]) % 2**3_2 SCREAMING_SNAKE_CASE_ = d SCREAMING_SNAKE_CASE_ = c SCREAMING_SNAKE_CASE_ = b SCREAMING_SNAKE_CASE_ = sum_aa(__UpperCamelCase , left_rotate_aa(__UpperCamelCase , shift_amounts[i] ) ) # Add hashed chunk to running total SCREAMING_SNAKE_CASE_ = sum_aa(__UpperCamelCase , __UpperCamelCase ) SCREAMING_SNAKE_CASE_ = sum_aa(__UpperCamelCase , __UpperCamelCase ) SCREAMING_SNAKE_CASE_ = sum_aa(__UpperCamelCase , __UpperCamelCase ) SCREAMING_SNAKE_CASE_ = sum_aa(__UpperCamelCase , __UpperCamelCase ) SCREAMING_SNAKE_CASE_ = reformat_hex(__UpperCamelCase ) + reformat_hex(__UpperCamelCase ) + reformat_hex(__UpperCamelCase ) + reformat_hex(__UpperCamelCase ) return digest if __name__ == "__main__": import doctest doctest.testmod()
305
0
import argparse import os import evaluate import torch from datasets import load_dataset from torch.optim import AdamW from torch.utils.data import DataLoader from transformers import AutoModelForSequenceClassification, AutoTokenizer, get_linear_schedule_with_warmup, set_seed from accelerate import Accelerator, DistributedType from accelerate.local_sgd import LocalSGD ######################################################################## # This is a fully working simple example to use Accelerate # with LocalSGD, which is a method to synchronize model # parameters every K batches. It is different, but complementary # to gradient accumulation. # # This example trains a Bert base model on GLUE MRPC # in any of the following settings (with the same script): # - single CPU or single GPU # - multi GPUS (using PyTorch distributed mode) # - (multi) TPUs # - fp16 (mixed-precision) or fp32 (normal precision) # # To run it in each of these various modes, follow the instructions # in the readme for examples: # https://github.com/huggingface/accelerate/tree/main/examples # ######################################################################## A : List[Any] = 16 A : str = 32 def a__ ( __UpperCamelCase , __UpperCamelCase = 1_6 ): SCREAMING_SNAKE_CASE_ = AutoTokenizer.from_pretrained("bert-base-cased" ) SCREAMING_SNAKE_CASE_ = load_dataset("glue" , "mrpc" ) def tokenize_function(__UpperCamelCase ): # max_length=None => use the model max length (it's actually the default) SCREAMING_SNAKE_CASE_ = tokenizer(examples["sentence1"] , examples["sentence2"] , truncation=__UpperCamelCase , max_length=__UpperCamelCase ) 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( __UpperCamelCase , batched=__UpperCamelCase , 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(__UpperCamelCase ): # On TPU it's best to pad everything to the same length or training will be very slow. SCREAMING_SNAKE_CASE_ = 1_2_8 if accelerator.distributed_type == DistributedType.TPU else None # When using mixed precision we want round multiples of 8/16 if accelerator.mixed_precision == "fp8": SCREAMING_SNAKE_CASE_ = 1_6 elif accelerator.mixed_precision != "no": SCREAMING_SNAKE_CASE_ = 8 else: SCREAMING_SNAKE_CASE_ = None return tokenizer.pad( __UpperCamelCase , padding="longest" , max_length=__UpperCamelCase , pad_to_multiple_of=__UpperCamelCase , return_tensors="pt" , ) # Instantiate dataloaders. SCREAMING_SNAKE_CASE_ = DataLoader( tokenized_datasets["train"] , shuffle=__UpperCamelCase , collate_fn=__UpperCamelCase , batch_size=__UpperCamelCase ) SCREAMING_SNAKE_CASE_ = DataLoader( tokenized_datasets["validation"] , shuffle=__UpperCamelCase , collate_fn=__UpperCamelCase , batch_size=__UpperCamelCase ) 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 A : Tuple = mocked_dataloaders # noqa: F811 def a__ ( __UpperCamelCase , __UpperCamelCase ): # For testing only if os.environ.get("TESTING_MOCKED_DATALOADERS" , __UpperCamelCase ) == "1": SCREAMING_SNAKE_CASE_ = 2 # New Code # SCREAMING_SNAKE_CASE_ = int(args.gradient_accumulation_steps ) SCREAMING_SNAKE_CASE_ = int(args.local_sgd_steps ) # Initialize accelerator SCREAMING_SNAKE_CASE_ = Accelerator( cpu=args.cpu , mixed_precision=args.mixed_precision , gradient_accumulation_steps=__UpperCamelCase ) if accelerator.distributed_type not in [DistributedType.NO, DistributedType.MULTI_CPU, DistributedType.MULTI_GPU]: raise NotImplementedError("LocalSGD is supported only for CPUs and GPUs (no DeepSpeed or MegatronLM)" ) # Sample hyper-parameters for learning rate, batch size, seed and a few other HPs 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" ) set_seed(__UpperCamelCase ) SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = get_dataloaders(__UpperCamelCase , __UpperCamelCase ) # 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=__UpperCamelCase ) # 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=__UpperCamelCase ) # Instantiate scheduler SCREAMING_SNAKE_CASE_ = get_linear_schedule_with_warmup( optimizer=__UpperCamelCase , num_warmup_steps=1_0_0 , num_training_steps=(len(__UpperCamelCase ) * 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( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) # Now we train the model for epoch in range(__UpperCamelCase ): model.train() with LocalSGD( accelerator=__UpperCamelCase , model=__UpperCamelCase , local_sgd_steps=__UpperCamelCase , enabled=local_sgd_steps is not None ) as local_sgd: for step, batch in enumerate(__UpperCamelCase ): # We could avoid this line since we set the accelerator with `device_placement=True`. batch.to(accelerator.device ) # New code # # We use the new `accumulate` context manager to perform gradient accumulation # We also currently do not support TPUs nor advise it as bugs were found on the XLA side when running our tests. with accelerator.accumulate(__UpperCamelCase ): SCREAMING_SNAKE_CASE_ = model(**__UpperCamelCase ) SCREAMING_SNAKE_CASE_ = output.loss accelerator.backward(__UpperCamelCase ) optimizer.step() lr_scheduler.step() optimizer.zero_grad() # LocalSGD-specific line local_sgd.step() model.eval() for step, batch in enumerate(__UpperCamelCase ): # 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(**__UpperCamelCase ) 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=__UpperCamelCase , references=__UpperCamelCase , ) SCREAMING_SNAKE_CASE_ = metric.compute() # Use accelerator.print to print only on the main process. accelerator.print(F'''epoch {epoch}:''' , __UpperCamelCase ) def a__ ( ): SCREAMING_SNAKE_CASE_ = argparse.ArgumentParser(description="Simple example of training script." ) parser.add_argument( "--mixed_precision" , type=__UpperCamelCase , default=__UpperCamelCase , choices=["no", "fp16", "bf16", "fp8"] , help="Whether to use mixed precision. Choose" "between fp16 and bf16 (bfloat16). Bf16 requires PyTorch >= 1.10." "and an Nvidia Ampere GPU." , ) # New Code # parser.add_argument( "--gradient_accumulation_steps" , type=__UpperCamelCase , default=1 , help="The number of minibatches to be ran before gradients are accumulated." , ) parser.add_argument( "--local_sgd_steps" , type=__UpperCamelCase , default=8 , help="Number of local SGD steps or None to disable local SGD" ) parser.add_argument("--cpu" , action="store_true" , help="If passed, will train on the CPU." ) SCREAMING_SNAKE_CASE_ = parser.parse_args() SCREAMING_SNAKE_CASE_ = {"lr": 2E-5, "num_epochs": 3, "seed": 4_2, "batch_size": 1_6} training_function(__UpperCamelCase , __UpperCamelCase ) if __name__ == "__main__": main()
358
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, BlipaProcessor, BlipImageProcessor, GPTaTokenizer, PreTrainedTokenizerFast @require_vision class lowerCamelCase (unittest.TestCase ): """simple docstring""" def __A ( self : int ) -> Any: SCREAMING_SNAKE_CASE_ = tempfile.mkdtemp() SCREAMING_SNAKE_CASE_ = BlipImageProcessor() SCREAMING_SNAKE_CASE_ = GPTaTokenizer.from_pretrained("hf-internal-testing/tiny-random-GPT2Model" ) SCREAMING_SNAKE_CASE_ = BlipaProcessor(__magic_name__ , __magic_name__ ) processor.save_pretrained(self.tmpdirname ) def __A ( self : str , **__magic_name__ : int ) -> Union[str, Any]: return AutoProcessor.from_pretrained(self.tmpdirname , **__magic_name__ ).tokenizer def __A ( self : Dict , **__magic_name__ : List[Any] ) -> int: return AutoProcessor.from_pretrained(self.tmpdirname , **__magic_name__ ).image_processor def __A ( self : int ) -> Any: shutil.rmtree(self.tmpdirname ) def __A ( self : Dict ) -> Dict: SCREAMING_SNAKE_CASE_ = [np.random.randint(255 , size=(3, 30, 400) , dtype=np.uinta )] SCREAMING_SNAKE_CASE_ = [Image.fromarray(np.moveaxis(__magic_name__ , 0 , -1 ) ) for x in image_inputs] return image_inputs def __A ( self : List[Any] ) -> Union[str, Any]: SCREAMING_SNAKE_CASE_ = BlipaProcessor(tokenizer=self.get_tokenizer() , image_processor=self.get_image_processor() ) processor.save_pretrained(self.tmpdirname ) SCREAMING_SNAKE_CASE_ = self.get_tokenizer(bos_token="(BOS)" , eos_token="(EOS)" ) SCREAMING_SNAKE_CASE_ = self.get_image_processor(do_normalize=__magic_name__ , padding_value=1.0 ) SCREAMING_SNAKE_CASE_ = BlipaProcessor.from_pretrained( self.tmpdirname , bos_token="(BOS)" , eos_token="(EOS)" , do_normalize=__magic_name__ , padding_value=1.0 ) self.assertEqual(processor.tokenizer.get_vocab() , tokenizer_add_kwargs.get_vocab() ) self.assertIsInstance(processor.tokenizer , __magic_name__ ) self.assertEqual(processor.image_processor.to_json_string() , image_processor_add_kwargs.to_json_string() ) self.assertIsInstance(processor.image_processor , __magic_name__ ) def __A ( self : Tuple ) -> int: SCREAMING_SNAKE_CASE_ = self.get_image_processor() SCREAMING_SNAKE_CASE_ = self.get_tokenizer() SCREAMING_SNAKE_CASE_ = BlipaProcessor(tokenizer=__magic_name__ , image_processor=__magic_name__ ) SCREAMING_SNAKE_CASE_ = self.prepare_image_inputs() SCREAMING_SNAKE_CASE_ = image_processor(__magic_name__ , return_tensors="np" ) SCREAMING_SNAKE_CASE_ = processor(images=__magic_name__ , 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 __A ( self : str ) -> Tuple: SCREAMING_SNAKE_CASE_ = self.get_image_processor() SCREAMING_SNAKE_CASE_ = self.get_tokenizer() SCREAMING_SNAKE_CASE_ = BlipaProcessor(tokenizer=__magic_name__ , image_processor=__magic_name__ ) SCREAMING_SNAKE_CASE_ = "lower newer" SCREAMING_SNAKE_CASE_ = processor(text=__magic_name__ ) SCREAMING_SNAKE_CASE_ = tokenizer(__magic_name__ , return_token_type_ids=__magic_name__ ) for key in encoded_tok.keys(): self.assertListEqual(encoded_tok[key] , encoded_processor[key] ) def __A ( self : Dict ) -> Union[str, Any]: SCREAMING_SNAKE_CASE_ = self.get_image_processor() SCREAMING_SNAKE_CASE_ = self.get_tokenizer() SCREAMING_SNAKE_CASE_ = BlipaProcessor(tokenizer=__magic_name__ , image_processor=__magic_name__ ) SCREAMING_SNAKE_CASE_ = "lower newer" SCREAMING_SNAKE_CASE_ = self.prepare_image_inputs() SCREAMING_SNAKE_CASE_ = processor(text=__magic_name__ , images=__magic_name__ ) self.assertListEqual(list(inputs.keys() ) , ["pixel_values", "input_ids", "attention_mask"] ) # test if it raises when no input is passed with pytest.raises(__magic_name__ ): processor() def __A ( self : Dict ) -> Tuple: SCREAMING_SNAKE_CASE_ = self.get_image_processor() SCREAMING_SNAKE_CASE_ = self.get_tokenizer() SCREAMING_SNAKE_CASE_ = BlipaProcessor(tokenizer=__magic_name__ , image_processor=__magic_name__ ) SCREAMING_SNAKE_CASE_ = [[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]] SCREAMING_SNAKE_CASE_ = processor.batch_decode(__magic_name__ ) SCREAMING_SNAKE_CASE_ = tokenizer.batch_decode(__magic_name__ ) self.assertListEqual(__magic_name__ , __magic_name__ ) def __A ( self : List[str] ) -> int: SCREAMING_SNAKE_CASE_ = self.get_image_processor() SCREAMING_SNAKE_CASE_ = self.get_tokenizer() SCREAMING_SNAKE_CASE_ = BlipaProcessor(tokenizer=__magic_name__ , image_processor=__magic_name__ ) SCREAMING_SNAKE_CASE_ = "lower newer" SCREAMING_SNAKE_CASE_ = self.prepare_image_inputs() SCREAMING_SNAKE_CASE_ = processor(text=__magic_name__ , images=__magic_name__ ) # For now the processor supports only ['pixel_values', 'input_ids', 'attention_mask'] self.assertListEqual(list(inputs.keys() ) , ["pixel_values", "input_ids", "attention_mask"] )
305
0
import math import sys def a__ ( __UpperCamelCase ): SCREAMING_SNAKE_CASE_ = "" try: with open(_UpperCamelCase , "rb" ) as binary_file: SCREAMING_SNAKE_CASE_ = binary_file.read() for dat in data: SCREAMING_SNAKE_CASE_ = F'''{dat:08b}''' result += curr_byte return result except OSError: print("File not accessible" ) sys.exit() def a__ ( __UpperCamelCase ): SCREAMING_SNAKE_CASE_ = {"0": "0", "1": "1"} SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = "", "" SCREAMING_SNAKE_CASE_ = len(_UpperCamelCase ) for i in range(len(_UpperCamelCase ) ): curr_string += data_bits[i] if curr_string not in lexicon: continue SCREAMING_SNAKE_CASE_ = lexicon[curr_string] result += last_match_id SCREAMING_SNAKE_CASE_ = last_match_id + "0" if math.loga(_UpperCamelCase ).is_integer(): SCREAMING_SNAKE_CASE_ = {} for curr_key in list(_UpperCamelCase ): SCREAMING_SNAKE_CASE_ = lexicon.pop(_UpperCamelCase ) SCREAMING_SNAKE_CASE_ = new_lex SCREAMING_SNAKE_CASE_ = last_match_id + "1" index += 1 SCREAMING_SNAKE_CASE_ = "" return result def a__ ( __UpperCamelCase , __UpperCamelCase ): SCREAMING_SNAKE_CASE_ = 8 try: with open(_UpperCamelCase , "wb" ) as opened_file: SCREAMING_SNAKE_CASE_ = [ 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[:-1]: opened_file.write(int(_UpperCamelCase , 2 ).to_bytes(1 , byteorder="big" ) ) except OSError: print("File not accessible" ) sys.exit() def a__ ( __UpperCamelCase ): SCREAMING_SNAKE_CASE_ = 0 for letter in data_bits: if letter == "1": break counter += 1 SCREAMING_SNAKE_CASE_ = data_bits[counter:] SCREAMING_SNAKE_CASE_ = data_bits[counter + 1 :] return data_bits def a__ ( __UpperCamelCase , __UpperCamelCase ): SCREAMING_SNAKE_CASE_ = read_file_binary(_UpperCamelCase ) SCREAMING_SNAKE_CASE_ = remove_prefix(_UpperCamelCase ) SCREAMING_SNAKE_CASE_ = decompress_data(_UpperCamelCase ) write_file_binary(_UpperCamelCase , _UpperCamelCase ) if __name__ == "__main__": compress(sys.argv[1], sys.argv[2])
359
from typing import TYPE_CHECKING from ...file_utils import _LazyModule, is_tokenizers_available, is_torch_available, is_vision_available from ...utils import OptionalDependencyNotAvailable A : List[Any] = {"configuration_dpt": ["DPT_PRETRAINED_CONFIG_ARCHIVE_MAP", "DPTConfig"]} try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: A : Optional[Any] = ["DPTFeatureExtractor"] A : str = ["DPTImageProcessor"] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: A : Optional[Any] = [ "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 A : Optional[int] = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
305
0
from collections import OrderedDict from typing import Mapping from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging A : str = logging.get_logger(__name__) A : List[Any] = { """YituTech/conv-bert-base""": """https://huggingface.co/YituTech/conv-bert-base/resolve/main/config.json""", """YituTech/conv-bert-medium-small""": ( """https://huggingface.co/YituTech/conv-bert-medium-small/resolve/main/config.json""" ), """YituTech/conv-bert-small""": """https://huggingface.co/YituTech/conv-bert-small/resolve/main/config.json""", # See all ConvBERT models at https://huggingface.co/models?filter=convbert } class lowerCamelCase (SCREAMING_SNAKE_CASE__ ): """simple docstring""" lowerCamelCase__ = 'convbert' def __init__( self : Optional[int] , __magic_name__ : Union[str, Any]=30_522 , __magic_name__ : List[Any]=768 , __magic_name__ : List[str]=12 , __magic_name__ : Dict=12 , __magic_name__ : Dict=3_072 , __magic_name__ : int="gelu" , __magic_name__ : Optional[int]=0.1 , __magic_name__ : Optional[int]=0.1 , __magic_name__ : Dict=512 , __magic_name__ : List[Any]=2 , __magic_name__ : int=0.02 , __magic_name__ : Tuple=1e-12 , __magic_name__ : str=1 , __magic_name__ : Union[str, Any]=0 , __magic_name__ : List[Any]=2 , __magic_name__ : List[str]=768 , __magic_name__ : Any=2 , __magic_name__ : Union[str, Any]=9 , __magic_name__ : List[str]=1 , __magic_name__ : str=None , **__magic_name__ : List[str] , ) -> Optional[int]: super().__init__( pad_token_id=__lowerCAmelCase , bos_token_id=__lowerCAmelCase , eos_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_ = 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_ = initializer_range SCREAMING_SNAKE_CASE_ = layer_norm_eps SCREAMING_SNAKE_CASE_ = embedding_size SCREAMING_SNAKE_CASE_ = head_ratio SCREAMING_SNAKE_CASE_ = conv_kernel_size SCREAMING_SNAKE_CASE_ = num_groups SCREAMING_SNAKE_CASE_ = classifier_dropout class lowerCamelCase (SCREAMING_SNAKE_CASE__ ): """simple docstring""" @property def __A ( self : Any ) -> Tuple: if self.task == "multiple-choice": SCREAMING_SNAKE_CASE_ = {0: "batch", 1: "choice", 2: "sequence"} else: SCREAMING_SNAKE_CASE_ = {0: "batch", 1: "sequence"} return OrderedDict( [ ("input_ids", dynamic_axis), ("attention_mask", dynamic_axis), ("token_type_ids", dynamic_axis), ] )
360
from __future__ import annotations import collections import pprint from pathlib import Path def a__ ( __UpperCamelCase ): return "".join(sorted(__UpperCamelCase ) ) def a__ ( __UpperCamelCase ): return word_by_signature[signature(__UpperCamelCase )] A : str = Path(__file__).parent.joinpath("words.txt").read_text(encoding="utf-8") A : int = sorted({word.strip().lower() for word in data.splitlines()}) A : Tuple = collections.defaultdict(list) for word in word_list: word_by_signature[signature(word)].append(word) if __name__ == "__main__": A : Union[str, Any] = {word: anagram(word) for word in word_list if len(anagram(word)) > 1} with open("anagrams.txt", "w") as file: file.write("all_anagrams = \n ") file.write(pprint.pformat(all_anagrams))
305
0
import pytest from datasets import inspect_metric, list_metrics, load_metric @pytest.fixture def a__ ( __UpperCamelCase ): monkeypatch.setattr("datasets.utils.deprecation_utils._emitted_deprecation_warnings" , set() ) @pytest.fixture def a__ ( __UpperCamelCase ): class lowerCamelCase : """simple docstring""" def __init__( self : List[Any] , __magic_name__ : int ) -> int: """simple docstring""" SCREAMING_SNAKE_CASE_ = metric_id class lowerCamelCase : """simple docstring""" lowerCamelCase__ = [MetricMock(SCREAMING_SNAKE_CASE__ ) for metric_id in ['''accuracy''', '''mse''', '''precision''', '''codeparrot/apps_metric''']] def __A ( self : int ) -> List[Any]: """simple docstring""" return self._metrics monkeypatch.setattr("datasets.inspect.huggingface_hub" , HfhMock() ) @pytest.mark.parametrize( "func, args" , [(load_metric, ("metrics/mse",)), (list_metrics, ()), (inspect_metric, ("metrics/mse", "tmp_path"))] ) def a__ ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase ): if "tmp_path" in args: SCREAMING_SNAKE_CASE_ = tuple(arg if arg != "tmp_path" else tmp_path for arg in args ) with pytest.warns(a__ , match="https://huggingface.co/docs/evaluate" ): func(*a__ )
361
import copy import os from typing import TYPE_CHECKING, List, Union if TYPE_CHECKING: pass from ...configuration_utils import PretrainedConfig from ...utils import logging A : int = logging.get_logger(__name__) A : str = { "kakaobrain/align-base": "https://huggingface.co/kakaobrain/align-base/resolve/main/config.json", } class lowerCamelCase (SCREAMING_SNAKE_CASE__ ): """simple docstring""" lowerCamelCase__ = '''align_text_model''' def __init__( self : Optional[Any] , __magic_name__ : Union[str, Any]=30_522 , __magic_name__ : Tuple=768 , __magic_name__ : List[str]=12 , __magic_name__ : Optional[Any]=12 , __magic_name__ : str=3_072 , __magic_name__ : Dict="gelu" , __magic_name__ : Any=0.1 , __magic_name__ : Optional[int]=0.1 , __magic_name__ : List[str]=512 , __magic_name__ : Any=2 , __magic_name__ : Optional[Any]=0.02 , __magic_name__ : int=1e-12 , __magic_name__ : str=0 , __magic_name__ : Optional[Any]="absolute" , __magic_name__ : Optional[Any]=True , **__magic_name__ : Tuple , ) -> Union[str, Any]: super().__init__(**__magic_name__ ) 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_ = position_embedding_type SCREAMING_SNAKE_CASE_ = use_cache SCREAMING_SNAKE_CASE_ = pad_token_id @classmethod def __A ( cls : Any , __magic_name__ : Union[str, os.PathLike] , **__magic_name__ : Optional[Any] ) -> "PretrainedConfig": cls._set_token_in_kwargs(__magic_name__ ) SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = cls.get_config_dict(__magic_name__ , **__magic_name__ ) # get the text config dict if we are loading from AlignConfig if config_dict.get("model_type" ) == "align": SCREAMING_SNAKE_CASE_ = config_dict["text_config"] if "model_type" in config_dict and hasattr(cls , "model_type" ) and config_dict["model_type"] != cls.model_type: logger.warning( F'''You are using a model of type {config_dict["model_type"]} to instantiate a model of type ''' F'''{cls.model_type}. This is not supported for all configurations of models and can yield errors.''' ) return cls.from_dict(__magic_name__ , **__magic_name__ ) class lowerCamelCase (SCREAMING_SNAKE_CASE__ ): """simple docstring""" lowerCamelCase__ = '''align_vision_model''' def __init__( self : List[str] , __magic_name__ : int = 3 , __magic_name__ : int = 600 , __magic_name__ : float = 2.0 , __magic_name__ : float = 3.1 , __magic_name__ : int = 8 , __magic_name__ : List[int] = [3, 3, 5, 3, 5, 5, 3] , __magic_name__ : List[int] = [32, 16, 24, 40, 80, 112, 192] , __magic_name__ : List[int] = [16, 24, 40, 80, 112, 192, 320] , __magic_name__ : List[int] = [] , __magic_name__ : List[int] = [1, 2, 2, 2, 1, 2, 1] , __magic_name__ : List[int] = [1, 2, 2, 3, 3, 4, 1] , __magic_name__ : List[int] = [1, 6, 6, 6, 6, 6, 6] , __magic_name__ : float = 0.25 , __magic_name__ : str = "swish" , __magic_name__ : int = 2_560 , __magic_name__ : str = "mean" , __magic_name__ : float = 0.02 , __magic_name__ : float = 0.001 , __magic_name__ : float = 0.99 , __magic_name__ : float = 0.2 , **__magic_name__ : List[Any] , ) -> Tuple: super().__init__(**__magic_name__ ) SCREAMING_SNAKE_CASE_ = num_channels SCREAMING_SNAKE_CASE_ = image_size SCREAMING_SNAKE_CASE_ = width_coefficient SCREAMING_SNAKE_CASE_ = depth_coefficient SCREAMING_SNAKE_CASE_ = depth_divisor SCREAMING_SNAKE_CASE_ = kernel_sizes SCREAMING_SNAKE_CASE_ = in_channels SCREAMING_SNAKE_CASE_ = out_channels SCREAMING_SNAKE_CASE_ = depthwise_padding SCREAMING_SNAKE_CASE_ = strides SCREAMING_SNAKE_CASE_ = num_block_repeats SCREAMING_SNAKE_CASE_ = expand_ratios SCREAMING_SNAKE_CASE_ = squeeze_expansion_ratio SCREAMING_SNAKE_CASE_ = hidden_act SCREAMING_SNAKE_CASE_ = hidden_dim SCREAMING_SNAKE_CASE_ = pooling_type SCREAMING_SNAKE_CASE_ = initializer_range SCREAMING_SNAKE_CASE_ = batch_norm_eps SCREAMING_SNAKE_CASE_ = batch_norm_momentum SCREAMING_SNAKE_CASE_ = drop_connect_rate SCREAMING_SNAKE_CASE_ = sum(__magic_name__ ) * 4 @classmethod def __A ( cls : List[str] , __magic_name__ : Union[str, os.PathLike] , **__magic_name__ : Dict ) -> "PretrainedConfig": cls._set_token_in_kwargs(__magic_name__ ) SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = cls.get_config_dict(__magic_name__ , **__magic_name__ ) # get the vision config dict if we are loading from AlignConfig if config_dict.get("model_type" ) == "align": SCREAMING_SNAKE_CASE_ = 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(__magic_name__ , **__magic_name__ ) class lowerCamelCase (SCREAMING_SNAKE_CASE__ ): """simple docstring""" lowerCamelCase__ = '''align''' lowerCamelCase__ = True def __init__( self : Optional[Any] , __magic_name__ : Dict=None , __magic_name__ : List[Any]=None , __magic_name__ : str=640 , __magic_name__ : Any=1.0 , __magic_name__ : Dict=0.02 , **__magic_name__ : Union[str, Any] , ) -> int: super().__init__(**__magic_name__ ) if text_config is None: SCREAMING_SNAKE_CASE_ = {} logger.info("text_config is None. Initializing the AlignTextConfig with default values." ) if vision_config is None: SCREAMING_SNAKE_CASE_ = {} logger.info("vision_config is None. Initializing the AlignVisionConfig with default values." ) SCREAMING_SNAKE_CASE_ = AlignTextConfig(**__magic_name__ ) SCREAMING_SNAKE_CASE_ = AlignVisionConfig(**__magic_name__ ) SCREAMING_SNAKE_CASE_ = projection_dim SCREAMING_SNAKE_CASE_ = temperature_init_value SCREAMING_SNAKE_CASE_ = initializer_range @classmethod def __A ( cls : List[str] , __magic_name__ : AlignTextConfig , __magic_name__ : AlignVisionConfig , **__magic_name__ : Tuple ) -> Any: return cls(text_config=text_config.to_dict() , vision_config=vision_config.to_dict() , **__magic_name__ ) def __A ( self : int ) -> Union[str, Any]: SCREAMING_SNAKE_CASE_ = copy.deepcopy(self.__dict__ ) SCREAMING_SNAKE_CASE_ = self.text_config.to_dict() SCREAMING_SNAKE_CASE_ = self.vision_config.to_dict() SCREAMING_SNAKE_CASE_ = self.__class__.model_type return output
305
0
"""simple docstring""" import os # Precomputes a list of the 100 first triangular numbers A : Optional[Any] = [int(0.5 * n * (n + 1)) for n in range(1, 1_01)] def a__ ( ): SCREAMING_SNAKE_CASE_ = os.path.dirname(os.path.realpath(lowercase_ ) ) SCREAMING_SNAKE_CASE_ = os.path.join(lowercase_ , "words.txt" ) SCREAMING_SNAKE_CASE_ = "" with open(lowercase_ ) as f: SCREAMING_SNAKE_CASE_ = f.readline() SCREAMING_SNAKE_CASE_ = [word.strip("\"" ) for word in words.strip("\r\n" ).split("," )] SCREAMING_SNAKE_CASE_ = [ word for word in [sum(ord(lowercase_ ) - 6_4 for x in word ) for word in words] if word in TRIANGULAR_NUMBERS ] return len(lowercase_ ) if __name__ == "__main__": print(solution())
362
import unittest from transformers.testing_utils import CaptureStdout from transformers.tools.python_interpreter import evaluate def a__ ( __UpperCamelCase ): return x + 2 class lowerCamelCase (unittest.TestCase ): """simple docstring""" def __A ( self : List[Any] ) -> int: SCREAMING_SNAKE_CASE_ = "x = 3" SCREAMING_SNAKE_CASE_ = {} SCREAMING_SNAKE_CASE_ = evaluate(__magic_name__ , {} , state=__magic_name__ ) assert result == 3 self.assertDictEqual(__magic_name__ , {"x": 3} ) SCREAMING_SNAKE_CASE_ = "x = y" SCREAMING_SNAKE_CASE_ = {"y": 5} SCREAMING_SNAKE_CASE_ = evaluate(__magic_name__ , {} , state=__magic_name__ ) # evaluate returns the value of the last assignment. assert result == 5 self.assertDictEqual(__magic_name__ , {"x": 5, "y": 5} ) def __A ( self : Union[str, Any] ) -> str: SCREAMING_SNAKE_CASE_ = "y = add_two(x)" SCREAMING_SNAKE_CASE_ = {"x": 3} SCREAMING_SNAKE_CASE_ = evaluate(__magic_name__ , {"add_two": add_two} , state=__magic_name__ ) assert result == 5 self.assertDictEqual(__magic_name__ , {"x": 3, "y": 5} ) # Won't work without the tool with CaptureStdout() as out: SCREAMING_SNAKE_CASE_ = evaluate(__magic_name__ , {} , state=__magic_name__ ) assert result is None assert "tried to execute add_two" in out.out def __A ( self : List[str] ) -> int: SCREAMING_SNAKE_CASE_ = "x = 3" SCREAMING_SNAKE_CASE_ = {} SCREAMING_SNAKE_CASE_ = evaluate(__magic_name__ , {} , state=__magic_name__ ) assert result == 3 self.assertDictEqual(__magic_name__ , {"x": 3} ) def __A ( self : Optional[Any] ) -> str: SCREAMING_SNAKE_CASE_ = "test_dict = {'x': x, 'y': add_two(x)}" SCREAMING_SNAKE_CASE_ = {"x": 3} SCREAMING_SNAKE_CASE_ = evaluate(__magic_name__ , {"add_two": add_two} , state=__magic_name__ ) self.assertDictEqual(__magic_name__ , {"x": 3, "y": 5} ) self.assertDictEqual(__magic_name__ , {"x": 3, "test_dict": {"x": 3, "y": 5}} ) def __A ( self : Optional[int] ) -> List[str]: SCREAMING_SNAKE_CASE_ = "x = 3\ny = 5" SCREAMING_SNAKE_CASE_ = {} SCREAMING_SNAKE_CASE_ = evaluate(__magic_name__ , {} , state=__magic_name__ ) # evaluate returns the value of the last assignment. assert result == 5 self.assertDictEqual(__magic_name__ , {"x": 3, "y": 5} ) def __A ( self : Any ) -> List[str]: SCREAMING_SNAKE_CASE_ = "text = f'This is x: {x}.'" SCREAMING_SNAKE_CASE_ = {"x": 3} SCREAMING_SNAKE_CASE_ = evaluate(__magic_name__ , {} , state=__magic_name__ ) # evaluate returns the value of the last assignment. assert result == "This is x: 3." self.assertDictEqual(__magic_name__ , {"x": 3, "text": "This is x: 3."} ) def __A ( self : int ) -> Tuple: SCREAMING_SNAKE_CASE_ = "if x <= 3:\n y = 2\nelse:\n y = 5" SCREAMING_SNAKE_CASE_ = {"x": 3} SCREAMING_SNAKE_CASE_ = evaluate(__magic_name__ , {} , state=__magic_name__ ) # evaluate returns the value of the last assignment. assert result == 2 self.assertDictEqual(__magic_name__ , {"x": 3, "y": 2} ) SCREAMING_SNAKE_CASE_ = {"x": 8} SCREAMING_SNAKE_CASE_ = evaluate(__magic_name__ , {} , state=__magic_name__ ) # evaluate returns the value of the last assignment. assert result == 5 self.assertDictEqual(__magic_name__ , {"x": 8, "y": 5} ) def __A ( self : str ) -> str: SCREAMING_SNAKE_CASE_ = "test_list = [x, add_two(x)]" SCREAMING_SNAKE_CASE_ = {"x": 3} SCREAMING_SNAKE_CASE_ = evaluate(__magic_name__ , {"add_two": add_two} , state=__magic_name__ ) self.assertListEqual(__magic_name__ , [3, 5] ) self.assertDictEqual(__magic_name__ , {"x": 3, "test_list": [3, 5]} ) def __A ( self : Union[str, Any] ) -> List[Any]: SCREAMING_SNAKE_CASE_ = "y = x" SCREAMING_SNAKE_CASE_ = {"x": 3} SCREAMING_SNAKE_CASE_ = evaluate(__magic_name__ , {} , state=__magic_name__ ) assert result == 3 self.assertDictEqual(__magic_name__ , {"x": 3, "y": 3} ) def __A ( self : Tuple ) -> List[Any]: SCREAMING_SNAKE_CASE_ = "test_list = [x, add_two(x)]\ntest_list[1]" SCREAMING_SNAKE_CASE_ = {"x": 3} SCREAMING_SNAKE_CASE_ = evaluate(__magic_name__ , {"add_two": add_two} , state=__magic_name__ ) assert result == 5 self.assertDictEqual(__magic_name__ , {"x": 3, "test_list": [3, 5]} ) SCREAMING_SNAKE_CASE_ = "test_dict = {'x': x, 'y': add_two(x)}\ntest_dict['y']" SCREAMING_SNAKE_CASE_ = {"x": 3} SCREAMING_SNAKE_CASE_ = evaluate(__magic_name__ , {"add_two": add_two} , state=__magic_name__ ) assert result == 5 self.assertDictEqual(__magic_name__ , {"x": 3, "test_dict": {"x": 3, "y": 5}} ) def __A ( self : Tuple ) -> Any: SCREAMING_SNAKE_CASE_ = "x = 0\nfor i in range(3):\n x = i" SCREAMING_SNAKE_CASE_ = {} SCREAMING_SNAKE_CASE_ = evaluate(__magic_name__ , {"range": range} , state=__magic_name__ ) assert result == 2 self.assertDictEqual(__magic_name__ , {"x": 2, "i": 2} )
305
0
import unittest import numpy as np from transformers import RoFormerConfig, is_flax_available from transformers.testing_utils import require_flax, slow from ...test_modeling_flax_common import FlaxModelTesterMixin, ids_tensor, random_attention_mask if is_flax_available(): import jax.numpy as jnp from transformers.models.roformer.modeling_flax_roformer import ( FlaxRoFormerForMaskedLM, FlaxRoFormerForMultipleChoice, FlaxRoFormerForQuestionAnswering, FlaxRoFormerForSequenceClassification, FlaxRoFormerForTokenClassification, FlaxRoFormerModel, ) class lowerCamelCase (unittest.TestCase ): """simple docstring""" def __init__( self : List[str] , __magic_name__ : List[str] , __magic_name__ : Tuple=13 , __magic_name__ : List[Any]=7 , __magic_name__ : Optional[Any]=True , __magic_name__ : Dict=True , __magic_name__ : str=True , __magic_name__ : Tuple=True , __magic_name__ : List[Any]=99 , __magic_name__ : Union[str, Any]=32 , __magic_name__ : Optional[Any]=5 , __magic_name__ : str=4 , __magic_name__ : Optional[Any]=37 , __magic_name__ : Any="gelu" , __magic_name__ : Tuple=0.1 , __magic_name__ : List[Any]=0.1 , __magic_name__ : int=512 , __magic_name__ : Union[str, Any]=16 , __magic_name__ : str=2 , __magic_name__ : Any=0.02 , __magic_name__ : Any=4 , ) -> int: SCREAMING_SNAKE_CASE_ = parent SCREAMING_SNAKE_CASE_ = batch_size SCREAMING_SNAKE_CASE_ = seq_length SCREAMING_SNAKE_CASE_ = is_training SCREAMING_SNAKE_CASE_ = use_attention_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_ = num_choices def __A ( self : List[Any] ) -> Union[str, Any]: SCREAMING_SNAKE_CASE_ = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) SCREAMING_SNAKE_CASE_ = None if self.use_attention_mask: SCREAMING_SNAKE_CASE_ = random_attention_mask([self.batch_size, self.seq_length] ) SCREAMING_SNAKE_CASE_ = None if self.use_token_type_ids: SCREAMING_SNAKE_CASE_ = ids_tensor([self.batch_size, self.seq_length] , self.type_vocab_size ) SCREAMING_SNAKE_CASE_ = RoFormerConfig( vocab_size=self.vocab_size , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , max_position_embeddings=self.max_position_embeddings , type_vocab_size=self.type_vocab_size , is_decoder=_a , initializer_range=self.initializer_range , ) return config, input_ids, token_type_ids, attention_mask def __A ( self : List[str] ) -> Any: SCREAMING_SNAKE_CASE_ = self.prepare_config_and_inputs() SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = config_and_inputs SCREAMING_SNAKE_CASE_ = {"input_ids": input_ids, "token_type_ids": token_type_ids, "attention_mask": attention_mask} return config, inputs_dict @require_flax class lowerCamelCase (SCREAMING_SNAKE_CASE__ , unittest.TestCase ): """simple docstring""" lowerCamelCase__ = True lowerCamelCase__ = ( ( FlaxRoFormerModel, FlaxRoFormerForMaskedLM, FlaxRoFormerForSequenceClassification, FlaxRoFormerForTokenClassification, FlaxRoFormerForMultipleChoice, FlaxRoFormerForQuestionAnswering, ) if is_flax_available() else () ) def __A ( self : Optional[Any] ) -> str: SCREAMING_SNAKE_CASE_ = FlaxRoFormerModelTester(self ) @slow def __A ( self : Optional[Any] ) -> Any: for model_class_name in self.all_model_classes: SCREAMING_SNAKE_CASE_ = model_class_name.from_pretrained("junnyu/roformer_chinese_small" , from_pt=_a ) SCREAMING_SNAKE_CASE_ = model(np.ones((1, 1) ) ) self.assertIsNotNone(_a ) @require_flax class lowerCamelCase (unittest.TestCase ): """simple docstring""" @slow def __A ( self : Optional[int] ) -> Dict: SCREAMING_SNAKE_CASE_ = FlaxRoFormerForMaskedLM.from_pretrained("junnyu/roformer_chinese_base" ) SCREAMING_SNAKE_CASE_ = jnp.array([[0, 1, 2, 3, 4, 5]] ) SCREAMING_SNAKE_CASE_ = model(_a )[0] SCREAMING_SNAKE_CASE_ = 50_000 SCREAMING_SNAKE_CASE_ = (1, 6, vocab_size) self.assertEqual(output.shape , _a ) SCREAMING_SNAKE_CASE_ = jnp.array( [[[-0.1205, -1.0265, 0.2922], [-1.5134, 0.1974, 0.1519], [-5.0135, -3.9003, -0.8404]]] ) self.assertTrue(jnp.allclose(output[:, :3, :3] , _a , atol=1e-4 ) )
363
import numpy as np import pandas as pd from sklearn.preprocessing import Normalizer from sklearn.svm import SVR from statsmodels.tsa.statespace.sarimax import SARIMAX def a__ ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase ): SCREAMING_SNAKE_CASE_ = np.array([[1, item, train_mtch[i]] for i, item in enumerate(__UpperCamelCase )] ) SCREAMING_SNAKE_CASE_ = np.array(__UpperCamelCase ) SCREAMING_SNAKE_CASE_ = np.dot(np.dot(np.linalg.inv(np.dot(x.transpose() , __UpperCamelCase ) ) , x.transpose() ) , __UpperCamelCase ) return abs(beta[0] + test_dt[0] * beta[1] + test_mtch[0] + beta[2] ) def a__ ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase ): SCREAMING_SNAKE_CASE_ = (1, 2, 1) SCREAMING_SNAKE_CASE_ = (1, 1, 0, 7) SCREAMING_SNAKE_CASE_ = SARIMAX( __UpperCamelCase , exog=__UpperCamelCase , order=__UpperCamelCase , seasonal_order=__UpperCamelCase ) SCREAMING_SNAKE_CASE_ = model.fit(disp=__UpperCamelCase , maxiter=6_0_0 , method="nm" ) SCREAMING_SNAKE_CASE_ = model_fit.predict(1 , len(__UpperCamelCase ) , exog=[test_match] ) return result[0] def a__ ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase ): SCREAMING_SNAKE_CASE_ = SVR(kernel="rbf" , C=1 , gamma=0.1 , epsilon=0.1 ) regressor.fit(__UpperCamelCase , __UpperCamelCase ) SCREAMING_SNAKE_CASE_ = regressor.predict(__UpperCamelCase ) return y_pred[0] def a__ ( __UpperCamelCase ): train_user.sort() SCREAMING_SNAKE_CASE_ = np.percentile(__UpperCamelCase , 2_5 ) SCREAMING_SNAKE_CASE_ = np.percentile(__UpperCamelCase , 7_5 ) SCREAMING_SNAKE_CASE_ = qa - qa SCREAMING_SNAKE_CASE_ = qa - (iqr * 0.1) return low_lim def a__ ( __UpperCamelCase , __UpperCamelCase ): SCREAMING_SNAKE_CASE_ = 0 SCREAMING_SNAKE_CASE_ = 0 for i in list_vote: if i > actual_result: SCREAMING_SNAKE_CASE_ = not_safe + 1 else: if abs(abs(__UpperCamelCase ) - abs(__UpperCamelCase ) ) <= 0.1: safe += 1 else: not_safe += 1 return safe > not_safe if __name__ == "__main__": # data_input_df = pd.read_csv("ex_data.csv", header=None) A : Dict = [[1_82_31, 0.0, 1], [2_26_21, 1.0, 2], [1_56_75, 0.0, 3], [2_35_83, 1.0, 4]] A : Optional[Any] = pd.DataFrame( data_input, columns=["total_user", "total_even", "days"] ) A : Union[str, Any] = Normalizer().fit_transform(data_input_df.values) # split data A : Optional[int] = normalize_df[:, 2].tolist() A : List[str] = normalize_df[:, 0].tolist() A : int = normalize_df[:, 1].tolist() # for svr (input variable = total date and total match) A : int = normalize_df[:, [1, 2]].tolist() A : Tuple = x[: len(x) - 1] A : str = x[len(x) - 1 :] # for linear regression & sarimax A : Tuple = total_date[: len(total_date) - 1] A : Optional[int] = total_user[: len(total_user) - 1] A : str = total_match[: len(total_match) - 1] A : List[Any] = total_date[len(total_date) - 1 :] A : List[Any] = total_user[len(total_user) - 1 :] A : Optional[Any] = total_match[len(total_match) - 1 :] # voting system with forecasting A : Optional[int] = [ linear_regression_prediction( trn_date, trn_user, trn_match, tst_date, tst_match ), sarimax_predictor(trn_user, trn_match, tst_match), support_vector_regressor(x_train, x_test, trn_user), ] # check the safety of today's data A : str = "" if data_safety_checker(res_vote, tst_user) else "not " print("Today's data is {not_str}safe.")
305
0
from ...utils import ( OptionalDependencyNotAvailable, is_torch_available, is_transformers_available, is_transformers_version, ) try: if not (is_transformers_available() and is_torch_available() and is_transformers_version(">=", "4.25.0")): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from ...utils.dummy_torch_and_transformers_objects import ( VersatileDiffusionDualGuidedPipeline, VersatileDiffusionImageVariationPipeline, VersatileDiffusionPipeline, VersatileDiffusionTextToImagePipeline, ) else: from .modeling_text_unet import UNetFlatConditionModel from .pipeline_versatile_diffusion import VersatileDiffusionPipeline from .pipeline_versatile_diffusion_dual_guided import VersatileDiffusionDualGuidedPipeline from .pipeline_versatile_diffusion_image_variation import VersatileDiffusionImageVariationPipeline from .pipeline_versatile_diffusion_text_to_image import VersatileDiffusionTextToImagePipeline
364
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tf_available, is_torch_available A : List[str] = {"configuration_swin": ["SWIN_PRETRAINED_CONFIG_ARCHIVE_MAP", "SwinConfig", "SwinOnnxConfig"]} try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: A : Any = [ "SWIN_PRETRAINED_MODEL_ARCHIVE_LIST", "SwinForImageClassification", "SwinForMaskedImageModeling", "SwinModel", "SwinPreTrainedModel", "SwinBackbone", ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: A : str = [ "TF_SWIN_PRETRAINED_MODEL_ARCHIVE_LIST", "TFSwinForImageClassification", "TFSwinForMaskedImageModeling", "TFSwinModel", "TFSwinPreTrainedModel", ] if TYPE_CHECKING: from .configuration_swin import SWIN_PRETRAINED_CONFIG_ARCHIVE_MAP, SwinConfig, SwinOnnxConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_swin import ( SWIN_PRETRAINED_MODEL_ARCHIVE_LIST, SwinBackbone, SwinForImageClassification, SwinForMaskedImageModeling, SwinModel, SwinPreTrainedModel, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_swin import ( TF_SWIN_PRETRAINED_MODEL_ARCHIVE_LIST, TFSwinForImageClassification, TFSwinForMaskedImageModeling, TFSwinModel, TFSwinPreTrainedModel, ) else: import sys A : Tuple = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
305
0
"""simple docstring""" import json import os from typing import Optional, Tuple import regex as re from ...tokenization_utils import PreTrainedTokenizer from ...utils import logging A : Optional[Any] = logging.get_logger(__name__) A : Dict = { "vocab_file": "vocab.json", "merges_file": "merges.txt", } A : List[Any] = { "vocab_file": {"ctrl": "https://raw.githubusercontent.com/salesforce/ctrl/master/ctrl-vocab.json"}, "merges_file": {"ctrl": "https://raw.githubusercontent.com/salesforce/ctrl/master/ctrl-merges.txt"}, } A : List[Any] = { "ctrl": 2_56, } A : str = { "Pregnancy": 16_86_29, "Christianity": 76_75, "Explain": 10_64_23, "Fitness": 6_34_40, "Saving": 6_31_63, "Ask": 2_71_71, "Ass": 9_59_85, "Joke": 16_35_09, "Questions": 4_56_22, "Thoughts": 4_96_05, "Retail": 5_23_42, "Feminism": 16_43_38, "Writing": 1_19_92, "Atheism": 19_22_63, "Netflix": 4_86_16, "Computing": 3_96_39, "Opinion": 4_32_13, "Alone": 4_49_67, "Funny": 5_89_17, "Gaming": 4_03_58, "Human": 40_88, "India": 13_31, "Joker": 7_71_38, "Diet": 3_62_06, "Legal": 1_18_59, "Norman": 49_39, "Tip": 7_26_89, "Weight": 5_23_43, "Movies": 4_62_73, "Running": 2_34_25, "Science": 20_90, "Horror": 3_77_93, "Confession": 6_05_72, "Finance": 1_22_50, "Politics": 1_63_60, "Scary": 19_19_85, "Support": 1_26_54, "Technologies": 3_25_16, "Teenage": 6_61_60, "Event": 3_27_69, "Learned": 6_74_60, "Notion": 18_27_70, "Wikipedia": 3_75_83, "Books": 66_65, "Extract": 7_60_50, "Confessions": 10_27_01, "Conspiracy": 7_59_32, "Links": 6_36_74, "Narcissus": 15_04_25, "Relationship": 5_47_66, "Relationships": 13_47_96, "Reviews": 4_16_71, "News": 42_56, "Translation": 2_68_20, "multilingual": 12_84_06, } def a__ ( __UpperCamelCase ): SCREAMING_SNAKE_CASE_ = set() SCREAMING_SNAKE_CASE_ = word[0] for char in word[1:]: pairs.add((prev_char, char) ) SCREAMING_SNAKE_CASE_ = char SCREAMING_SNAKE_CASE_ = set(_UpperCAmelCase ) return pairs class lowerCamelCase (snake_case__ ): """simple docstring""" lowerCamelCase__ = VOCAB_FILES_NAMES lowerCamelCase__ = PRETRAINED_VOCAB_FILES_MAP lowerCamelCase__ = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES lowerCamelCase__ = CONTROL_CODES def __init__( self : Any , __magic_name__ : Tuple , __magic_name__ : Optional[int] , __magic_name__ : str="<unk>" , **__magic_name__ : Optional[int] ) -> int: super().__init__(unk_token=UpperCAmelCase_ , **UpperCAmelCase_ ) with open(UpperCAmelCase_ , encoding="utf-8" ) as vocab_handle: SCREAMING_SNAKE_CASE_ = json.load(UpperCAmelCase_ ) SCREAMING_SNAKE_CASE_ = {v: k for k, v in self.encoder.items()} with open(UpperCAmelCase_ , encoding="utf-8" ) as merges_handle: SCREAMING_SNAKE_CASE_ = merges_handle.read().split("\n" )[1:-1] SCREAMING_SNAKE_CASE_ = [tuple(merge.split() ) for merge in merges] SCREAMING_SNAKE_CASE_ = dict(zip(UpperCAmelCase_ , range(len(UpperCAmelCase_ ) ) ) ) SCREAMING_SNAKE_CASE_ = {} @property def __A ( self : Tuple ) -> str: return len(self.encoder ) def __A ( self : Union[str, Any] ) -> Tuple: return dict(self.encoder , **self.added_tokens_encoder ) def __A ( self : List[Any] , __magic_name__ : int ) -> int: if token in self.cache: return self.cache[token] SCREAMING_SNAKE_CASE_ = tuple(UpperCAmelCase_ ) SCREAMING_SNAKE_CASE_ = tuple(list(word[:-1] ) + [word[-1] + "</w>"] ) SCREAMING_SNAKE_CASE_ = get_pairs(UpperCAmelCase_ ) if not pairs: return token while True: SCREAMING_SNAKE_CASE_ = min(UpperCAmelCase_ , key=lambda __magic_name__ : self.bpe_ranks.get(UpperCAmelCase_ , float("inf" ) ) ) if bigram not in self.bpe_ranks: break SCREAMING_SNAKE_CASE_ = bigram SCREAMING_SNAKE_CASE_ = [] SCREAMING_SNAKE_CASE_ = 0 while i < len(UpperCAmelCase_ ): try: SCREAMING_SNAKE_CASE_ = word.index(UpperCAmelCase_ , UpperCAmelCase_ ) except ValueError: new_word.extend(word[i:] ) break else: new_word.extend(word[i:j] ) SCREAMING_SNAKE_CASE_ = j if word[i] == first and i < len(UpperCAmelCase_ ) - 1 and word[i + 1] == second: new_word.append(first + second ) i += 2 else: new_word.append(word[i] ) i += 1 SCREAMING_SNAKE_CASE_ = tuple(UpperCAmelCase_ ) SCREAMING_SNAKE_CASE_ = new_word if len(UpperCAmelCase_ ) == 1: break else: SCREAMING_SNAKE_CASE_ = get_pairs(UpperCAmelCase_ ) SCREAMING_SNAKE_CASE_ = "@@ ".join(UpperCAmelCase_ ) SCREAMING_SNAKE_CASE_ = word[:-4] SCREAMING_SNAKE_CASE_ = word return word def __A ( self : int , __magic_name__ : int ) -> Optional[Any]: SCREAMING_SNAKE_CASE_ = [] SCREAMING_SNAKE_CASE_ = re.findall(r"\S+\n?" , UpperCAmelCase_ ) for token in words: split_tokens.extend(list(self.bpe(UpperCAmelCase_ ).split(" " ) ) ) return split_tokens def __A ( self : str , __magic_name__ : Optional[int] ) -> Union[str, Any]: return self.encoder.get(UpperCAmelCase_ , self.encoder.get(self.unk_token ) ) def __A ( self : Tuple , __magic_name__ : str ) -> Optional[int]: return self.decoder.get(UpperCAmelCase_ , self.unk_token ) def __A ( self : int , __magic_name__ : int ) -> List[str]: SCREAMING_SNAKE_CASE_ = " ".join(UpperCAmelCase_ ).replace("@@ " , "" ).strip() return out_string def __A ( self : Union[str, Any] , __magic_name__ : str , __magic_name__ : Optional[str] = None ) -> Dict: if not os.path.isdir(UpperCAmelCase_ ): logger.error(F'''Vocabulary path ({save_directory}) should be a directory''' ) return SCREAMING_SNAKE_CASE_ = os.path.join( UpperCAmelCase_ , (filename_prefix + "-" if filename_prefix else "") + VOCAB_FILES_NAMES["vocab_file"] ) SCREAMING_SNAKE_CASE_ = os.path.join( UpperCAmelCase_ , (filename_prefix + "-" if filename_prefix else "") + VOCAB_FILES_NAMES["merges_file"] ) with open(UpperCAmelCase_ , "w" , encoding="utf-8" ) as f: f.write(json.dumps(self.encoder , indent=2 , sort_keys=UpperCAmelCase_ , ensure_ascii=UpperCAmelCase_ ) + "\n" ) SCREAMING_SNAKE_CASE_ = 0 with open(UpperCAmelCase_ , "w" , encoding="utf-8" ) as writer: writer.write("#version: 0.2\n" ) for bpe_tokens, token_index in sorted(self.bpe_ranks.items() , key=lambda __magic_name__ : kv[1] ): if index != token_index: logger.warning( F'''Saving vocabulary to {merge_file}: BPE merge indices are not consecutive.''' " Please check that the tokenizer is not corrupted!" ) SCREAMING_SNAKE_CASE_ = token_index writer.write(" ".join(UpperCAmelCase_ ) + "\n" ) index += 1 return vocab_file, merge_file # def decode(self, token_ids, skip_special_tokens=False, clean_up_tokenization_spaces=True): # filtered_tokens = ' '.join(self.convert_ids_to_tokens(token_ids, skip_special_tokens=skip_special_tokens)) # tokens_generated_so_far = re.sub('(@@ )', '', string=filtered_tokens) # tokens_generated_so_far = re.sub('(@@ ?$)', '', string=tokens_generated_so_far) # return ''.join(tokens_generated_so_far)
365
import tempfile import unittest import numpy as np from huggingface_hub import HfFolder, delete_repo from requests.exceptions import HTTPError from transformers import BertConfig, is_flax_available from transformers.testing_utils import TOKEN, USER, is_staging_test, require_flax if is_flax_available(): import os from flax.core.frozen_dict import unfreeze from flax.traverse_util import flatten_dict from transformers import FlaxBertModel A : Union[str, Any] = "0.12" # assumed parallelism: 8 @require_flax @is_staging_test class lowerCamelCase (unittest.TestCase ): """simple docstring""" @classmethod def __A ( cls : Any ) -> Dict: SCREAMING_SNAKE_CASE_ = TOKEN HfFolder.save_token(__magic_name__ ) @classmethod def __A ( cls : Optional[int] ) -> Tuple: try: delete_repo(token=cls._token , repo_id="test-model-flax" ) except HTTPError: pass try: delete_repo(token=cls._token , repo_id="valid_org/test-model-flax-org" ) except HTTPError: pass def __A ( self : str ) -> str: SCREAMING_SNAKE_CASE_ = BertConfig( vocab_size=99 , hidden_size=32 , num_hidden_layers=5 , num_attention_heads=4 , intermediate_size=37 ) SCREAMING_SNAKE_CASE_ = FlaxBertModel(__magic_name__ ) model.push_to_hub("test-model-flax" , use_auth_token=self._token ) SCREAMING_SNAKE_CASE_ = FlaxBertModel.from_pretrained(F'''{USER}/test-model-flax''' ) SCREAMING_SNAKE_CASE_ = flatten_dict(unfreeze(model.params ) ) SCREAMING_SNAKE_CASE_ = flatten_dict(unfreeze(new_model.params ) ) for key in base_params.keys(): SCREAMING_SNAKE_CASE_ = (base_params[key] - new_params[key]).sum().item() self.assertLessEqual(__magic_name__ , 1e-3 , msg=F'''{key} not identical''' ) # Reset repo delete_repo(token=self._token , repo_id="test-model-flax" ) # Push to hub via save_pretrained with tempfile.TemporaryDirectory() as tmp_dir: model.save_pretrained(__magic_name__ , repo_id="test-model-flax" , push_to_hub=__magic_name__ , use_auth_token=self._token ) SCREAMING_SNAKE_CASE_ = FlaxBertModel.from_pretrained(F'''{USER}/test-model-flax''' ) SCREAMING_SNAKE_CASE_ = flatten_dict(unfreeze(model.params ) ) SCREAMING_SNAKE_CASE_ = flatten_dict(unfreeze(new_model.params ) ) for key in base_params.keys(): SCREAMING_SNAKE_CASE_ = (base_params[key] - new_params[key]).sum().item() self.assertLessEqual(__magic_name__ , 1e-3 , msg=F'''{key} not identical''' ) def __A ( self : int ) -> Tuple: SCREAMING_SNAKE_CASE_ = BertConfig( vocab_size=99 , hidden_size=32 , num_hidden_layers=5 , num_attention_heads=4 , intermediate_size=37 ) SCREAMING_SNAKE_CASE_ = FlaxBertModel(__magic_name__ ) model.push_to_hub("valid_org/test-model-flax-org" , use_auth_token=self._token ) SCREAMING_SNAKE_CASE_ = FlaxBertModel.from_pretrained("valid_org/test-model-flax-org" ) SCREAMING_SNAKE_CASE_ = flatten_dict(unfreeze(model.params ) ) SCREAMING_SNAKE_CASE_ = flatten_dict(unfreeze(new_model.params ) ) for key in base_params.keys(): SCREAMING_SNAKE_CASE_ = (base_params[key] - new_params[key]).sum().item() self.assertLessEqual(__magic_name__ , 1e-3 , msg=F'''{key} not identical''' ) # Reset repo delete_repo(token=self._token , repo_id="valid_org/test-model-flax-org" ) # Push to hub via save_pretrained with tempfile.TemporaryDirectory() as tmp_dir: model.save_pretrained( __magic_name__ , repo_id="valid_org/test-model-flax-org" , push_to_hub=__magic_name__ , use_auth_token=self._token ) SCREAMING_SNAKE_CASE_ = FlaxBertModel.from_pretrained("valid_org/test-model-flax-org" ) SCREAMING_SNAKE_CASE_ = flatten_dict(unfreeze(model.params ) ) SCREAMING_SNAKE_CASE_ = flatten_dict(unfreeze(new_model.params ) ) for key in base_params.keys(): SCREAMING_SNAKE_CASE_ = (base_params[key] - new_params[key]).sum().item() self.assertLessEqual(__magic_name__ , 1e-3 , msg=F'''{key} not identical''' ) def a__ ( __UpperCamelCase , __UpperCamelCase ): SCREAMING_SNAKE_CASE_ = True SCREAMING_SNAKE_CASE_ = flatten_dict(modela.params ) SCREAMING_SNAKE_CASE_ = flatten_dict(modela.params ) for key in flat_params_a.keys(): if np.sum(np.abs(flat_params_a[key] - flat_params_a[key] ) ) > 1E-4: SCREAMING_SNAKE_CASE_ = False return models_are_equal @require_flax class lowerCamelCase (unittest.TestCase ): """simple docstring""" def __A ( self : str ) -> Dict: SCREAMING_SNAKE_CASE_ = BertConfig.from_pretrained("hf-internal-testing/tiny-bert-flax-only" ) SCREAMING_SNAKE_CASE_ = FlaxBertModel(__magic_name__ ) SCREAMING_SNAKE_CASE_ = "bert" with tempfile.TemporaryDirectory() as tmp_dir: model.save_pretrained(os.path.join(__magic_name__ , __magic_name__ ) ) with self.assertRaises(__magic_name__ ): SCREAMING_SNAKE_CASE_ = FlaxBertModel.from_pretrained(__magic_name__ ) SCREAMING_SNAKE_CASE_ = FlaxBertModel.from_pretrained(__magic_name__ , subfolder=__magic_name__ ) self.assertTrue(check_models_equal(__magic_name__ , __magic_name__ ) ) def __A ( self : Optional[Any] ) -> Tuple: SCREAMING_SNAKE_CASE_ = BertConfig.from_pretrained("hf-internal-testing/tiny-bert-flax-only" ) SCREAMING_SNAKE_CASE_ = FlaxBertModel(__magic_name__ ) SCREAMING_SNAKE_CASE_ = "bert" with tempfile.TemporaryDirectory() as tmp_dir: model.save_pretrained(os.path.join(__magic_name__ , __magic_name__ ) , max_shard_size="10KB" ) with self.assertRaises(__magic_name__ ): SCREAMING_SNAKE_CASE_ = FlaxBertModel.from_pretrained(__magic_name__ ) SCREAMING_SNAKE_CASE_ = FlaxBertModel.from_pretrained(__magic_name__ , subfolder=__magic_name__ ) self.assertTrue(check_models_equal(__magic_name__ , __magic_name__ ) ) def __A ( self : Optional[int] ) -> Dict: SCREAMING_SNAKE_CASE_ = "bert" SCREAMING_SNAKE_CASE_ = "hf-internal-testing/tiny-random-bert-subfolder" with self.assertRaises(__magic_name__ ): SCREAMING_SNAKE_CASE_ = FlaxBertModel.from_pretrained(__magic_name__ ) SCREAMING_SNAKE_CASE_ = FlaxBertModel.from_pretrained(__magic_name__ , subfolder=__magic_name__ ) self.assertIsNotNone(__magic_name__ ) def __A ( self : List[str] ) -> Dict: SCREAMING_SNAKE_CASE_ = "bert" SCREAMING_SNAKE_CASE_ = "hf-internal-testing/tiny-random-bert-sharded-subfolder" with self.assertRaises(__magic_name__ ): SCREAMING_SNAKE_CASE_ = FlaxBertModel.from_pretrained(__magic_name__ ) SCREAMING_SNAKE_CASE_ = FlaxBertModel.from_pretrained(__magic_name__ , subfolder=__magic_name__ ) self.assertIsNotNone(__magic_name__ )
305
0
import json import os import tempfile from transformers.testing_utils import check_json_file_has_correct_format class lowerCamelCase : """simple docstring""" lowerCamelCase__ = None def __A ( self : Optional[Any] ) -> int: SCREAMING_SNAKE_CASE_ = self.feature_extraction_class(**self.feat_extract_dict ) SCREAMING_SNAKE_CASE_ = json.loads(feat_extract.to_json_string() ) for key, value in self.feat_extract_dict.items(): self.assertEqual(obj[key] , lowerCAmelCase__ ) def __A ( self : Any ) -> str: SCREAMING_SNAKE_CASE_ = self.feature_extraction_class(**self.feat_extract_dict ) with tempfile.TemporaryDirectory() as tmpdirname: SCREAMING_SNAKE_CASE_ = os.path.join(lowerCAmelCase__ , "feat_extract.json" ) feat_extract_first.to_json_file(lowerCAmelCase__ ) SCREAMING_SNAKE_CASE_ = self.feature_extraction_class.from_json_file(lowerCAmelCase__ ) self.assertEqual(feat_extract_second.to_dict() , feat_extract_first.to_dict() ) def __A ( self : List[str] ) -> Tuple: SCREAMING_SNAKE_CASE_ = self.feature_extraction_class(**self.feat_extract_dict ) with tempfile.TemporaryDirectory() as tmpdirname: SCREAMING_SNAKE_CASE_ = feat_extract_first.save_pretrained(lowerCAmelCase__ )[0] check_json_file_has_correct_format(lowerCAmelCase__ ) SCREAMING_SNAKE_CASE_ = self.feature_extraction_class.from_pretrained(lowerCAmelCase__ ) self.assertEqual(feat_extract_second.to_dict() , feat_extract_first.to_dict() ) def __A ( self : Optional[int] ) -> List[Any]: SCREAMING_SNAKE_CASE_ = self.feature_extraction_class() self.assertIsNotNone(lowerCAmelCase__ )
366
# Copyright 2021 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. import argparse import os from accelerate.utils import ComputeEnvironment from .cluster import get_cluster_input from .config_args import cache_dir, default_config_file, default_yaml_config_file, load_config_from_file # noqa: F401 from .config_utils import _ask_field, _ask_options, _convert_compute_environment # noqa: F401 from .sagemaker import get_sagemaker_input A : Union[str, Any] = "Launches a series of prompts to create and save a `default_config.yaml` configuration file for your training system. Should always be ran first on your machine" def a__ ( ): SCREAMING_SNAKE_CASE_ = _ask_options( "In which compute environment are you running?" , ["This machine", "AWS (Amazon SageMaker)"] , _convert_compute_environment , ) if compute_environment == ComputeEnvironment.AMAZON_SAGEMAKER: SCREAMING_SNAKE_CASE_ = get_sagemaker_input() else: SCREAMING_SNAKE_CASE_ = get_cluster_input() return config def a__ ( __UpperCamelCase=None ): if subparsers is not None: SCREAMING_SNAKE_CASE_ = subparsers.add_parser("config" , description=__UpperCamelCase ) else: SCREAMING_SNAKE_CASE_ = argparse.ArgumentParser("Accelerate config command" , description=__UpperCamelCase ) parser.add_argument( "--config_file" , default=__UpperCamelCase , help=( "The path to use to store the config file. Will default to a file named default_config.yaml in the cache " "location, which is the content of the environment `HF_HOME` suffixed with 'accelerate', or if you don't have " "such an environment variable, your cache directory ('~/.cache' or the content of `XDG_CACHE_HOME`) suffixed " "with 'huggingface'." ) , ) if subparsers is not None: parser.set_defaults(func=__UpperCamelCase ) return parser def a__ ( __UpperCamelCase ): SCREAMING_SNAKE_CASE_ = get_user_input() if args.config_file is not None: SCREAMING_SNAKE_CASE_ = args.config_file else: if not os.path.isdir(__UpperCamelCase ): os.makedirs(__UpperCamelCase ) SCREAMING_SNAKE_CASE_ = default_yaml_config_file if config_file.endswith(".json" ): config.to_json_file(__UpperCamelCase ) else: config.to_yaml_file(__UpperCamelCase ) print(F'''accelerate configuration saved at {config_file}''' ) def a__ ( ): SCREAMING_SNAKE_CASE_ = config_command_parser() SCREAMING_SNAKE_CASE_ = parser.parse_args() config_command(__UpperCamelCase ) if __name__ == "__main__": main()
305
0
from typing import Optional, Union import numpy as np from ...image_processing_utils import BaseImageProcessor, BatchFeature from ...image_transforms import get_image_size, pad, rescale, to_channel_dimension_format from ...image_utils import ChannelDimension, ImageInput, make_list_of_images, to_numpy_array, valid_images from ...utils import TensorType, logging A : Optional[int] = logging.get_logger(__name__) class lowerCamelCase (UpperCamelCase__ ): """simple docstring""" lowerCamelCase__ = ['''pixel_values'''] def __init__( self : int , __magic_name__ : Any = True , __magic_name__ : Dict = 1 / 255 , __magic_name__ : Tuple = True , __magic_name__ : Optional[Any] = 8 , **__magic_name__ : Optional[int] , ) -> List[str]: super().__init__(**__lowerCamelCase ) SCREAMING_SNAKE_CASE_ = do_rescale SCREAMING_SNAKE_CASE_ = rescale_factor SCREAMING_SNAKE_CASE_ = do_pad SCREAMING_SNAKE_CASE_ = pad_size def __A ( self : str , __magic_name__ : Any , __magic_name__ : int , __magic_name__ : int = None , **__magic_name__ : List[str] ) -> List[Any]: return rescale(__lowerCamelCase , scale=__lowerCamelCase , data_format=__lowerCamelCase , **__lowerCamelCase ) def __A ( self : Optional[int] , __magic_name__ : Tuple , __magic_name__ : Any , __magic_name__ : Optional[Any] = None ) -> int: SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = get_image_size(__lowerCamelCase ) SCREAMING_SNAKE_CASE_ = (old_height // size + 1) * size - old_height SCREAMING_SNAKE_CASE_ = (old_width // size + 1) * size - old_width return pad(__lowerCamelCase , ((0, pad_height), (0, pad_width)) , mode="symmetric" , data_format=__lowerCamelCase ) def __A ( self : List[Any] , __magic_name__ : Dict , __magic_name__ : Tuple = None , __magic_name__ : Dict = None , __magic_name__ : Optional[Any] = None , __magic_name__ : Tuple = None , __magic_name__ : Optional[int] = None , __magic_name__ : Tuple = ChannelDimension.FIRST , **__magic_name__ : str , ) -> List[Any]: 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_pad if do_pad is not None else self.do_pad SCREAMING_SNAKE_CASE_ = pad_size if pad_size is not None else self.pad_size SCREAMING_SNAKE_CASE_ = make_list_of_images(__lowerCamelCase ) if not valid_images(__lowerCamelCase ): raise ValueError( "Invalid image type. Must be of type PIL.Image.Image, numpy.ndarray, " "torch.Tensor, tf.Tensor or jax.ndarray." ) if do_rescale and rescale_factor is None: raise ValueError("Rescale factor must be specified if do_rescale is True." ) # All transformations expect numpy arrays. SCREAMING_SNAKE_CASE_ = [to_numpy_array(__lowerCamelCase ) for image in images] if do_rescale: SCREAMING_SNAKE_CASE_ = [self.rescale(image=__lowerCamelCase , scale=__lowerCamelCase ) for image in images] if do_pad: SCREAMING_SNAKE_CASE_ = [self.pad(__lowerCamelCase , size=__lowerCamelCase ) for image in images] SCREAMING_SNAKE_CASE_ = [to_channel_dimension_format(__lowerCamelCase , __lowerCamelCase ) for image in images] SCREAMING_SNAKE_CASE_ = {"pixel_values": images} return BatchFeature(data=__lowerCamelCase , tensor_type=__lowerCamelCase )
367
from dataclasses import dataclass, field from typing import ClassVar, Dict from ..features import Features, Value from .base import TaskTemplate @dataclass(frozen=SCREAMING_SNAKE_CASE__ ) class lowerCamelCase (SCREAMING_SNAKE_CASE__ ): """simple docstring""" lowerCamelCase__ = field(default='''summarization''' , metadata={'''include_in_asdict_even_if_is_default''': True} ) lowerCamelCase__ = Features({'''text''': Value('''string''' )} ) lowerCamelCase__ = Features({'''summary''': Value('''string''' )} ) lowerCamelCase__ = "text" lowerCamelCase__ = "summary" @property def __A ( self : Dict ) -> Dict[str, str]: return {self.text_column: "text", self.summary_column: "summary"}
305
0
def a__ ( __UpperCamelCase , __UpperCamelCase ): if mass < 0: raise ValueError("The mass of a body cannot be negative" ) return 0.5 * mass * abs(__lowerCamelCase ) * abs(__lowerCamelCase ) if __name__ == "__main__": import doctest doctest.testmod(verbose=True)
368
from ....utils import logging A : List[str] = logging.get_logger(__name__) class lowerCamelCase (SCREAMING_SNAKE_CASE__ ): """simple docstring""" def __init__( self : List[str] , __magic_name__ : Optional[Any] , __magic_name__ : Any=None , __magic_name__ : List[str]=2_048 ) -> List[Any]: SCREAMING_SNAKE_CASE_ = config.__dict__ SCREAMING_SNAKE_CASE_ = modal_hidden_size if num_labels: SCREAMING_SNAKE_CASE_ = num_labels
305
0
A : Optional[int] = {str(digit): digit**5 for digit in range(10)} def a__ ( __UpperCamelCase ): return sum(DIGITS_FIFTH_POWER[digit] for digit in str(_snake_case ) ) def a__ ( ): return sum( number for number in range(1_0_0_0 , 1_0_0_0_0_0_0 ) if number == digits_fifth_powers_sum(_snake_case ) ) if __name__ == "__main__": print(solution())
369
import warnings from typing import List, Optional, Union from ...processing_utils import ProcessorMixin from ...tokenization_utils_base import BatchEncoding, PaddingStrategy, PreTokenizedInput, TextInput, TruncationStrategy from ...utils import TensorType class lowerCamelCase (SCREAMING_SNAKE_CASE__ ): """simple docstring""" lowerCamelCase__ = ['''image_processor''', '''tokenizer'''] lowerCamelCase__ = '''ViltImageProcessor''' lowerCamelCase__ = ('''BertTokenizer''', '''BertTokenizerFast''') def __init__( self : Optional[int] , __magic_name__ : str=None , __magic_name__ : List[str]=None , **__magic_name__ : Any ) -> str: SCREAMING_SNAKE_CASE_ = None if "feature_extractor" in kwargs: warnings.warn( "The `feature_extractor` argument is deprecated and will be removed in v5, use `image_processor`" " instead." , __magic_name__ , ) SCREAMING_SNAKE_CASE_ = kwargs.pop("feature_extractor" ) SCREAMING_SNAKE_CASE_ = image_processor if image_processor is not None else feature_extractor if image_processor is None: raise ValueError("You need to specify an `image_processor`." ) if tokenizer is None: raise ValueError("You need to specify a `tokenizer`." ) super().__init__(__magic_name__ , __magic_name__ ) SCREAMING_SNAKE_CASE_ = self.image_processor def __call__( self : List[str] , __magic_name__ : List[str] , __magic_name__ : Union[TextInput, PreTokenizedInput, List[TextInput], List[PreTokenizedInput]] = None , __magic_name__ : bool = True , __magic_name__ : Union[bool, str, PaddingStrategy] = False , __magic_name__ : Union[bool, str, TruncationStrategy] = None , __magic_name__ : Optional[int] = None , __magic_name__ : int = 0 , __magic_name__ : Optional[int] = None , __magic_name__ : Optional[bool] = None , __magic_name__ : Optional[bool] = None , __magic_name__ : bool = False , __magic_name__ : bool = False , __magic_name__ : bool = False , __magic_name__ : bool = False , __magic_name__ : bool = True , __magic_name__ : Optional[Union[str, TensorType]] = None , **__magic_name__ : str , ) -> BatchEncoding: SCREAMING_SNAKE_CASE_ = self.tokenizer( text=__magic_name__ , add_special_tokens=__magic_name__ , padding=__magic_name__ , truncation=__magic_name__ , max_length=__magic_name__ , stride=__magic_name__ , pad_to_multiple_of=__magic_name__ , return_token_type_ids=__magic_name__ , return_attention_mask=__magic_name__ , return_overflowing_tokens=__magic_name__ , return_special_tokens_mask=__magic_name__ , return_offsets_mapping=__magic_name__ , return_length=__magic_name__ , verbose=__magic_name__ , return_tensors=__magic_name__ , **__magic_name__ , ) # add pixel_values + pixel_mask SCREAMING_SNAKE_CASE_ = self.image_processor(__magic_name__ , return_tensors=__magic_name__ ) encoding.update(__magic_name__ ) return encoding def __A ( self : Optional[int] , *__magic_name__ : List[Any] , **__magic_name__ : Optional[Any] ) -> Any: return self.tokenizer.batch_decode(*__magic_name__ , **__magic_name__ ) def __A ( self : Dict , *__magic_name__ : List[Any] , **__magic_name__ : Union[str, Any] ) -> str: return self.tokenizer.decode(*__magic_name__ , **__magic_name__ ) @property def __A ( self : Optional[int] ) -> Union[str, Any]: SCREAMING_SNAKE_CASE_ = self.tokenizer.model_input_names SCREAMING_SNAKE_CASE_ = self.image_processor.model_input_names return list(dict.fromkeys(tokenizer_input_names + image_processor_input_names ) ) @property def __A ( self : Dict ) -> List[Any]: warnings.warn( "`feature_extractor_class` is deprecated and will be removed in v5. Use `image_processor_class` instead." , __magic_name__ , ) return self.image_processor_class @property def __A ( self : int ) -> List[Any]: warnings.warn( "`feature_extractor` is deprecated and will be removed in v5. Use `image_processor` instead." , __magic_name__ , ) return self.image_processor
305
0
import logging import random import ray from transformers import RagConfig, RagRetriever, RagTokenizer from transformers.models.rag.retrieval_rag import CustomHFIndex A : Optional[Any] = logging.getLogger(__name__) class lowerCamelCase : """simple docstring""" def __init__( self : Union[str, Any] ) -> Dict: SCREAMING_SNAKE_CASE_ = False def __A ( self : List[Any] , __magic_name__ : Optional[Any] , __magic_name__ : Optional[Any] , __magic_name__ : Union[str, Any] , __magic_name__ : int ) -> List[Any]: if not self.initialized: SCREAMING_SNAKE_CASE_ = RagRetriever( _a , question_encoder_tokenizer=_a , generator_tokenizer=_a , index=_a , init_retrieval=_a , ) SCREAMING_SNAKE_CASE_ = True def __A ( self : int ) -> Optional[Any]: self.retriever.index.init_index() def __A ( self : List[Any] , __magic_name__ : List[str] , __magic_name__ : Any ) -> Optional[int]: SCREAMING_SNAKE_CASE_ = self.retriever._main_retrieve(_a , _a ) return doc_ids, retrieved_doc_embeds class lowerCamelCase (_a ): """simple docstring""" def __init__( self : str , __magic_name__ : Tuple , __magic_name__ : int , __magic_name__ : Optional[int] , __magic_name__ : Optional[int] , __magic_name__ : int=None ) -> List[Any]: if index is not None and index.is_initialized() and len(_a ) > 0: raise ValueError( "When using Ray for distributed fine-tuning, " "you'll need to provide the paths instead, " "as the dataset and the index are loaded " "separately. More info in examples/rag/use_own_knowledge_dataset.py " ) super().__init__( _a , question_encoder_tokenizer=_a , generator_tokenizer=_a , index=_a , init_retrieval=_a , ) SCREAMING_SNAKE_CASE_ = retrieval_workers if len(self.retrieval_workers ) > 0: ray.get( [ worker.create_rag_retriever.remote(_a , _a , _a , _a ) for worker in self.retrieval_workers ] ) def __A ( self : str ) -> str: logger.info("initializing retrieval" ) if len(self.retrieval_workers ) > 0: ray.get([worker.init_retrieval.remote() for worker in self.retrieval_workers] ) else: # Non-distributed training. Load index into this same process. self.index.init_index() def __A ( self : List[Any] , __magic_name__ : Union[str, Any] , __magic_name__ : int ) -> int: if len(self.retrieval_workers ) > 0: # Select a random retrieval actor. SCREAMING_SNAKE_CASE_ = self.retrieval_workers[random.randint(0 , len(self.retrieval_workers ) - 1 )] SCREAMING_SNAKE_CASE_ = ray.get(random_worker.retrieve.remote(_a , _a ) ) else: SCREAMING_SNAKE_CASE_ = self._main_retrieve(_a , _a ) return retrieved_doc_embeds, doc_ids, self.index.get_doc_dicts(_a ) @classmethod def __A ( cls : List[Any] , __magic_name__ : Any , __magic_name__ : Optional[Any]=None , **__magic_name__ : Union[str, Any] ) -> Union[str, Any]: return super(_a , cls ).get_tokenizers(_a , _a , **_a ) @classmethod def __A ( cls : Optional[Any] , __magic_name__ : List[str] , __magic_name__ : Dict , __magic_name__ : Dict=None , **__magic_name__ : Optional[Any] ) -> int: SCREAMING_SNAKE_CASE_ = kwargs.pop("config" , _a ) or RagConfig.from_pretrained(_a , **_a ) SCREAMING_SNAKE_CASE_ = RagTokenizer.from_pretrained(_a , config=_a ) SCREAMING_SNAKE_CASE_ = rag_tokenizer.question_encoder SCREAMING_SNAKE_CASE_ = rag_tokenizer.generator if indexed_dataset is not None: SCREAMING_SNAKE_CASE_ = "custom" SCREAMING_SNAKE_CASE_ = CustomHFIndex(config.retrieval_vector_size , _a ) else: SCREAMING_SNAKE_CASE_ = cls._build_index(_a ) return cls( _a , question_encoder_tokenizer=_a , generator_tokenizer=_a , retrieval_workers=_a , index=_a , )
370
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 ..auto import CONFIG_MAPPING A : str = logging.get_logger(__name__) A : Optional[int] = { "microsoft/table-transformer-detection": ( "https://huggingface.co/microsoft/table-transformer-detection/resolve/main/config.json" ), } class lowerCamelCase (SCREAMING_SNAKE_CASE__ ): """simple docstring""" lowerCamelCase__ = '''table-transformer''' lowerCamelCase__ = ['''past_key_values'''] lowerCamelCase__ = { '''hidden_size''': '''d_model''', '''num_attention_heads''': '''encoder_attention_heads''', } def __init__( self : List[Any] , __magic_name__ : Optional[Any]=True , __magic_name__ : Dict=None , __magic_name__ : Any=3 , __magic_name__ : List[str]=100 , __magic_name__ : Union[str, Any]=6 , __magic_name__ : Dict=2_048 , __magic_name__ : str=8 , __magic_name__ : int=6 , __magic_name__ : List[Any]=2_048 , __magic_name__ : Optional[int]=8 , __magic_name__ : Optional[int]=0.0 , __magic_name__ : List[Any]=0.0 , __magic_name__ : Optional[Any]=True , __magic_name__ : List[Any]="relu" , __magic_name__ : List[str]=256 , __magic_name__ : List[str]=0.1 , __magic_name__ : int=0.0 , __magic_name__ : Optional[Any]=0.0 , __magic_name__ : Tuple=0.02 , __magic_name__ : str=1.0 , __magic_name__ : int=False , __magic_name__ : Dict="sine" , __magic_name__ : Union[str, Any]="resnet50" , __magic_name__ : Optional[Any]=True , __magic_name__ : str=False , __magic_name__ : List[str]=1 , __magic_name__ : int=5 , __magic_name__ : Union[str, Any]=2 , __magic_name__ : Tuple=1 , __magic_name__ : Optional[int]=1 , __magic_name__ : Optional[Any]=5 , __magic_name__ : Optional[int]=2 , __magic_name__ : Union[str, Any]=0.1 , **__magic_name__ : Tuple , ) -> str: 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." ) SCREAMING_SNAKE_CASE_ = CONFIG_MAPPING["resnet"](out_features=["stage4"] ) elif isinstance(__magic_name__ , __magic_name__ ): SCREAMING_SNAKE_CASE_ = backbone_config.get("model_type" ) SCREAMING_SNAKE_CASE_ = CONFIG_MAPPING[backbone_model_type] SCREAMING_SNAKE_CASE_ = config_class.from_dict(__magic_name__ ) # set timm attributes to None SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = None, None, None SCREAMING_SNAKE_CASE_ = use_timm_backbone SCREAMING_SNAKE_CASE_ = backbone_config SCREAMING_SNAKE_CASE_ = num_channels SCREAMING_SNAKE_CASE_ = num_queries SCREAMING_SNAKE_CASE_ = d_model SCREAMING_SNAKE_CASE_ = encoder_ffn_dim SCREAMING_SNAKE_CASE_ = encoder_layers SCREAMING_SNAKE_CASE_ = encoder_attention_heads SCREAMING_SNAKE_CASE_ = decoder_ffn_dim SCREAMING_SNAKE_CASE_ = decoder_layers SCREAMING_SNAKE_CASE_ = decoder_attention_heads SCREAMING_SNAKE_CASE_ = dropout SCREAMING_SNAKE_CASE_ = attention_dropout SCREAMING_SNAKE_CASE_ = activation_dropout SCREAMING_SNAKE_CASE_ = activation_function SCREAMING_SNAKE_CASE_ = init_std SCREAMING_SNAKE_CASE_ = init_xavier_std SCREAMING_SNAKE_CASE_ = encoder_layerdrop SCREAMING_SNAKE_CASE_ = decoder_layerdrop SCREAMING_SNAKE_CASE_ = encoder_layers SCREAMING_SNAKE_CASE_ = auxiliary_loss SCREAMING_SNAKE_CASE_ = position_embedding_type SCREAMING_SNAKE_CASE_ = backbone SCREAMING_SNAKE_CASE_ = use_pretrained_backbone SCREAMING_SNAKE_CASE_ = dilation # Hungarian matcher SCREAMING_SNAKE_CASE_ = class_cost SCREAMING_SNAKE_CASE_ = bbox_cost SCREAMING_SNAKE_CASE_ = giou_cost # Loss coefficients SCREAMING_SNAKE_CASE_ = mask_loss_coefficient SCREAMING_SNAKE_CASE_ = dice_loss_coefficient SCREAMING_SNAKE_CASE_ = bbox_loss_coefficient SCREAMING_SNAKE_CASE_ = giou_loss_coefficient SCREAMING_SNAKE_CASE_ = eos_coefficient super().__init__(is_encoder_decoder=__magic_name__ , **__magic_name__ ) @property def __A ( self : Union[str, Any] ) -> int: return self.encoder_attention_heads @property def __A ( self : Any ) -> int: return self.d_model class lowerCamelCase (SCREAMING_SNAKE_CASE__ ): """simple docstring""" lowerCamelCase__ = version.parse('''1.11''' ) @property def __A ( self : Tuple ) -> Mapping[str, Mapping[int, str]]: return OrderedDict( [ ("pixel_values", {0: "batch", 1: "num_channels", 2: "height", 3: "width"}), ("pixel_mask", {0: "batch"}), ] ) @property def __A ( self : Any ) -> float: return 1e-5 @property def __A ( self : int ) -> int: return 12
305
0
from typing import TYPE_CHECKING from ...file_utils import _LazyModule, is_torch_available from ...utils import OptionalDependencyNotAvailable A : List[str] = { "configuration_gpt_neox_japanese": ["GPT_NEOX_JAPANESE_PRETRAINED_CONFIG_ARCHIVE_MAP", "GPTNeoXJapaneseConfig"], "tokenization_gpt_neox_japanese": ["GPTNeoXJapaneseTokenizer"], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: A : Tuple = [ "GPT_NEOX_JAPANESE_PRETRAINED_MODEL_ARCHIVE_LIST", "GPTNeoXJapaneseForCausalLM", "GPTNeoXJapaneseLayer", "GPTNeoXJapaneseModel", "GPTNeoXJapanesePreTrainedModel", ] if TYPE_CHECKING: from .configuration_gpt_neox_japanese import GPT_NEOX_JAPANESE_PRETRAINED_CONFIG_ARCHIVE_MAP, GPTNeoXJapaneseConfig from .tokenization_gpt_neox_japanese import GPTNeoXJapaneseTokenizer try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_gpt_neox_japanese import ( GPT_NEOX_JAPANESE_PRETRAINED_MODEL_ARCHIVE_LIST, GPTNeoXJapaneseForCausalLM, GPTNeoXJapaneseLayer, GPTNeoXJapaneseModel, GPTNeoXJapanesePreTrainedModel, ) else: import sys A : Any = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
371
import warnings from diffusers import StableDiffusionImgaImgPipeline # noqa F401 warnings.warn( "The `image_to_image.py` script is outdated. Please use directly `from diffusers import" " StableDiffusionImg2ImgPipeline` instead." )
305
0
"""simple docstring""" import copy import tempfile import unittest from transformers import MaMaaaConfig, is_torch_available from transformers.testing_utils import require_sentencepiece, require_tokenizers, require_torch, slow, torch_device from transformers.utils import cached_property from ...generation.test_utils import GenerationTesterMixin from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from transformers import MaMaaaForConditionalGeneration, MaMaaaModel, MaMaaaTokenizer from transformers.models.mam_aaa.modeling_mam_aaa import MaMaaaDecoder, MaMaaaEncoder def a__ ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase=None , __UpperCamelCase=None , __UpperCamelCase=None , __UpperCamelCase=None , __UpperCamelCase=None , ): if attention_mask is None: SCREAMING_SNAKE_CASE_ = input_ids.ne(config.pad_token_id ) if decoder_attention_mask is None: SCREAMING_SNAKE_CASE_ = decoder_input_ids.ne(config.pad_token_id ) if head_mask is None: SCREAMING_SNAKE_CASE_ = torch.ones(config.encoder_layers , config.encoder_attention_heads , device=__lowerCAmelCase ) if decoder_head_mask is None: SCREAMING_SNAKE_CASE_ = torch.ones(config.decoder_layers , config.decoder_attention_heads , device=__lowerCAmelCase ) if cross_attn_head_mask is None: SCREAMING_SNAKE_CASE_ = torch.ones(config.decoder_layers , config.decoder_attention_heads , device=__lowerCAmelCase ) return { "input_ids": input_ids, "decoder_input_ids": decoder_input_ids, "attention_mask": attention_mask, "decoder_attention_mask": attention_mask, "head_mask": head_mask, "decoder_head_mask": decoder_head_mask, "cross_attn_head_mask": cross_attn_head_mask, } class lowerCamelCase : """simple docstring""" def __init__( self : str , __magic_name__ : Any , __magic_name__ : Optional[int]=13 , __magic_name__ : Any=7 , __magic_name__ : List[Any]=True , __magic_name__ : int=False , __magic_name__ : Optional[int]=99 , __magic_name__ : Optional[int]=16 , __magic_name__ : Optional[Any]=2 , __magic_name__ : List[str]=4 , __magic_name__ : Any=4 , __magic_name__ : List[str]="relu" , __magic_name__ : List[Any]=0.1 , __magic_name__ : Dict=0.1 , __magic_name__ : Optional[int]=0.0 , __magic_name__ : Tuple=0.0 , __magic_name__ : Union[str, Any]=20 , __magic_name__ : Optional[Any]=2 , __magic_name__ : Dict=1 , __magic_name__ : Dict=0 , ) -> Tuple: 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_act SCREAMING_SNAKE_CASE_ = hidden_dropout_prob SCREAMING_SNAKE_CASE_ = attention_probs_dropout_prob SCREAMING_SNAKE_CASE_ = encoder_layerdrop SCREAMING_SNAKE_CASE_ = decoder_layerdrop 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 __A ( self : Tuple ) -> Tuple: SCREAMING_SNAKE_CASE_ = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) SCREAMING_SNAKE_CASE_ = self.eos_token_id # Eos Token SCREAMING_SNAKE_CASE_ = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) # we need to clamp the input ids here to avoid having pad token in between # this is because for M2M100 the position_ids are prepared such that # all pad tokens have pos id = 2 and rest are between 2..seq_length # and the seq_length here is seq_length - num_pad_tokens # but when using past, there is no way of knowing if the past input ids had # pad tokens in them, which results in incorrect seq_lenth and which in turn results in # position_ids being off by num_pad_tokens in past input SCREAMING_SNAKE_CASE_ = input_ids.clamp(self.pad_token_id + 1 ) SCREAMING_SNAKE_CASE_ = decoder_input_ids.clamp(self.pad_token_id + 1 ) SCREAMING_SNAKE_CASE_ = self.get_config() SCREAMING_SNAKE_CASE_ = prepare_mam_aaa_inputs_dict(__lowercase , __lowercase , __lowercase ) return config, inputs_dict def __A ( self : Any ) -> List[str]: return MaMaaaConfig( 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 , encoder_layerdrop=self.encoder_layerdrop , decoder_layerdrop=self.decoder_layerdrop , max_position_embeddings=self.max_position_embeddings , eos_token_id=self.eos_token_id , bos_token_id=self.bos_token_id , pad_token_id=self.pad_token_id , ) def __A ( self : str ) -> int: SCREAMING_SNAKE_CASE_ = self.prepare_config_and_inputs() return config, inputs_dict def __A ( self : int , __magic_name__ : List[Any] , __magic_name__ : Tuple ) -> Optional[int]: SCREAMING_SNAKE_CASE_ = MaMaaaModel(config=__lowercase ).get_decoder().to(__lowercase ).eval() SCREAMING_SNAKE_CASE_ = inputs_dict['''input_ids'''] SCREAMING_SNAKE_CASE_ = inputs_dict['''attention_mask'''] SCREAMING_SNAKE_CASE_ = inputs_dict['''head_mask'''] # first forward pass SCREAMING_SNAKE_CASE_ = model(__lowercase , attention_mask=__lowercase , head_mask=__lowercase , use_cache=__lowercase ) SCREAMING_SNAKE_CASE_ = outputs.to_tuple() # create hypothetical multiple next token and extent to next_input_ids SCREAMING_SNAKE_CASE_ = ids_tensor((self.batch_size, 3) , config.vocab_size ) SCREAMING_SNAKE_CASE_ = ids_tensor((self.batch_size, 3) , 2 ) # append to next input_ids and SCREAMING_SNAKE_CASE_ = torch.cat([input_ids, next_tokens] , dim=-1 ) SCREAMING_SNAKE_CASE_ = torch.cat([attention_mask, next_attn_mask] , dim=-1 ) SCREAMING_SNAKE_CASE_ = model(__lowercase , attention_mask=__lowercase )['''last_hidden_state'''] SCREAMING_SNAKE_CASE_ = model(__lowercase , attention_mask=__lowercase , past_key_values=__lowercase )[ '''last_hidden_state''' ] # select random slice SCREAMING_SNAKE_CASE_ = ids_tensor((1,) , output_from_past.shape[-1] ).item() SCREAMING_SNAKE_CASE_ = output_from_no_past[:, -3:, random_slice_idx].detach() SCREAMING_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(__lowercase , __lowercase , atol=1e-2 ) ) def __A ( self : Dict , __magic_name__ : List[Any] , __magic_name__ : Dict ) -> Optional[Any]: SCREAMING_SNAKE_CASE_ = MaMaaaModel(config=__lowercase ).to(__lowercase ).eval() SCREAMING_SNAKE_CASE_ = model(**__lowercase ) SCREAMING_SNAKE_CASE_ = outputs.encoder_last_hidden_state SCREAMING_SNAKE_CASE_ = outputs.last_hidden_state with tempfile.TemporaryDirectory() as tmpdirname: SCREAMING_SNAKE_CASE_ = model.get_encoder() encoder.save_pretrained(__lowercase ) SCREAMING_SNAKE_CASE_ = MaMaaaEncoder.from_pretrained(__lowercase ).to(__lowercase ) SCREAMING_SNAKE_CASE_ = encoder(inputs_dict["input_ids"] , attention_mask=inputs_dict["attention_mask"] )[ 0 ] self.parent.assertTrue((encoder_last_hidden_state_a - encoder_last_hidden_state).abs().max().item() < 1e-3 ) with tempfile.TemporaryDirectory() as tmpdirname: SCREAMING_SNAKE_CASE_ = model.get_decoder() decoder.save_pretrained(__lowercase ) SCREAMING_SNAKE_CASE_ = MaMaaaDecoder.from_pretrained(__lowercase ).to(__lowercase ) SCREAMING_SNAKE_CASE_ = decoder( input_ids=inputs_dict["decoder_input_ids"] , attention_mask=inputs_dict["decoder_attention_mask"] , encoder_hidden_states=__lowercase , encoder_attention_mask=inputs_dict["attention_mask"] , )[0] self.parent.assertTrue((last_hidden_state_a - last_hidden_state).abs().max().item() < 1e-3 ) @require_torch class lowerCamelCase (__lowerCamelCase , __lowerCamelCase , __lowerCamelCase , unittest.TestCase ): """simple docstring""" lowerCamelCase__ = ( ( MaMaaaModel, MaMaaaForConditionalGeneration, ) if is_torch_available() else () ) lowerCamelCase__ = (MaMaaaForConditionalGeneration,) if is_torch_available() else () lowerCamelCase__ = ( { """conversational""": MaMaaaForConditionalGeneration, """feature-extraction""": MaMaaaModel, """summarization""": MaMaaaForConditionalGeneration, """text2text-generation""": MaMaaaForConditionalGeneration, """translation""": MaMaaaForConditionalGeneration, } if is_torch_available() else {} ) lowerCamelCase__ = True lowerCamelCase__ = True lowerCamelCase__ = False lowerCamelCase__ = False def __A ( self : List[str] , __magic_name__ : Tuple , __magic_name__ : Optional[Any] , __magic_name__ : List[str] , __magic_name__ : Any , __magic_name__ : Optional[Any] ) -> int: if pipeline_test_casse_name == "TranslationPipelineTests": # Get `ValueError: Translation requires a `src_lang` and a `tgt_lang` for this model`. # `M2M100Config` was never used in pipeline tests: cannot create a simple tokenizer. return True return False def __A ( self : List[Any] ) -> Tuple: SCREAMING_SNAKE_CASE_ = MaMaaaModelTester(self ) SCREAMING_SNAKE_CASE_ = ConfigTester(self , config_class=__lowercase ) def __A ( self : Any ) -> Any: self.config_tester.run_common_tests() def __A ( self : Optional[int] ) -> Tuple: SCREAMING_SNAKE_CASE_ = self.model_tester.prepare_config_and_inputs() for model_class in self.all_model_classes: SCREAMING_SNAKE_CASE_ = model_class(__lowercase ) with tempfile.TemporaryDirectory() as tmpdirname: model.save_pretrained(__lowercase ) SCREAMING_SNAKE_CASE_ = model_class.from_pretrained(__lowercase , output_loading_info=__lowercase ) self.assertEqual(info["missing_keys"] , [] ) def __A ( self : str ) -> Any: SCREAMING_SNAKE_CASE_ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_decoder_model_past_large_inputs(*__lowercase ) def __A ( self : List[str] ) -> Tuple: SCREAMING_SNAKE_CASE_ = self.model_tester.prepare_config_and_inputs_for_common() self.model_tester.check_encoder_decoder_model_standalone(*__lowercase ) def __A ( self : List[Any] ) -> int: SCREAMING_SNAKE_CASE_ = self.model_tester.prepare_config_and_inputs_for_common() for model_class in (MaMaaaModel, MaMaaaForConditionalGeneration): SCREAMING_SNAKE_CASE_ = model_class(__lowercase ) model.to(__lowercase ) model.eval() SCREAMING_SNAKE_CASE_ = copy.deepcopy(self._prepare_for_class(__lowercase , __lowercase ) ) if not self.is_encoder_decoder: SCREAMING_SNAKE_CASE_ = inputs['''input_ids'''] del inputs["input_ids"] else: SCREAMING_SNAKE_CASE_ = inputs['''input_ids'''] SCREAMING_SNAKE_CASE_ = inputs.get("decoder_input_ids" , __lowercase ) del inputs["input_ids"] inputs.pop("decoder_input_ids" , __lowercase ) SCREAMING_SNAKE_CASE_ = model.get_input_embeddings() if not self.is_encoder_decoder: SCREAMING_SNAKE_CASE_ = wte(__lowercase ) else: SCREAMING_SNAKE_CASE_ = wte(__lowercase ) SCREAMING_SNAKE_CASE_ = wte(__lowercase ) with torch.no_grad(): model(**__lowercase )[0] def __A ( self : Dict ) -> Optional[int]: SCREAMING_SNAKE_CASE_ = self.model_tester.prepare_config_and_inputs() SCREAMING_SNAKE_CASE_ = input_dict['''input_ids'''] SCREAMING_SNAKE_CASE_ = input_ids.ne(1 ).to(__lowercase ) SCREAMING_SNAKE_CASE_ = MaMaaaForConditionalGeneration(__lowercase ).eval().to(__lowercase ) if torch_device == "cuda": model.half() model.generate(__lowercase , attention_mask=__lowercase ) model.generate(num_beams=4 , do_sample=__lowercase , early_stopping=__lowercase , num_return_sequences=3 ) def a__ ( __UpperCamelCase ): return torch.tensor(__lowerCAmelCase , dtype=torch.long , device=__lowerCAmelCase ) A : List[str] = 1E-4 @require_torch @require_sentencepiece @require_tokenizers @slow class lowerCamelCase (unittest.TestCase ): """simple docstring""" @cached_property def __A ( self : Any ) -> Any: return MaMaaaTokenizer.from_pretrained("facebook/m2m100_418M" ) def __A ( self : Dict ) -> Optional[int]: SCREAMING_SNAKE_CASE_ = MaMaaaModel.from_pretrained("facebook/m2m100_418M" ).to(__lowercase ) SCREAMING_SNAKE_CASE_ = _long_tensor([[128_028, 98, 12, 30_527, 2_732, 159, 7_755, 61_904, 39_144, 38, 2]] ) SCREAMING_SNAKE_CASE_ = _long_tensor([[2, 128_028, 98, 12, 30_527, 2_732, 159, 7_755, 61_904, 39_144, 38]] ) SCREAMING_SNAKE_CASE_ = prepare_mam_aaa_inputs_dict(model.config , __lowercase , __lowercase ) with torch.no_grad(): SCREAMING_SNAKE_CASE_ = model(**__lowercase )[0] SCREAMING_SNAKE_CASE_ = torch.Size((1, 11, 1_024) ) self.assertEqual(output.shape , __lowercase ) # change to expected output here SCREAMING_SNAKE_CASE_ = torch.tensor( [[-0.7780, -0.1676, 0.1038], [-6.7556, -1.3992, 0.0567], [-7.5383, -0.5920, -0.2779]] , device=__lowercase ) self.assertTrue(torch.allclose(output[:, :3, :3] , __lowercase , atol=__lowercase ) ) def __A ( self : List[Any] ) -> int: SCREAMING_SNAKE_CASE_ = MaMaaaForConditionalGeneration.from_pretrained("facebook/m2m100_418M" ).to(__lowercase ) # change to intended input SCREAMING_SNAKE_CASE_ = _long_tensor([[128_028, 98, 12, 30_527, 2_732, 159, 7_755, 61_904, 39_144, 38, 2]] ) SCREAMING_SNAKE_CASE_ = _long_tensor([[2, 128_028, 98, 12, 30_527, 2_732, 159, 7_755, 61_904, 39_144, 38]] ) SCREAMING_SNAKE_CASE_ = prepare_mam_aaa_inputs_dict(model.config , __lowercase , __lowercase ) with torch.no_grad(): SCREAMING_SNAKE_CASE_ = model(**__lowercase )[0] SCREAMING_SNAKE_CASE_ = torch.Size((1, 11, model.config.vocab_size) ) self.assertEqual(output.shape , __lowercase ) # change to expected output here SCREAMING_SNAKE_CASE_ = torch.tensor( [[-1.0448, -1.0411, 3.7992], [-3.2191, -3.2386, -1.3451], [-3.6210, -3.5993, 0.4925]] , device=__lowercase ) self.assertTrue(torch.allclose(output[:, :3, :3] , __lowercase , atol=__lowercase ) ) def __A ( self : List[Any] ) -> Any: SCREAMING_SNAKE_CASE_ = MaMaaaForConditionalGeneration.from_pretrained("facebook/m2m100_418M" ).to(__lowercase ) SCREAMING_SNAKE_CASE_ = MaMaaaTokenizer.from_pretrained("facebook/m2m100_418M" , src_lang="fr" , tgt_lang="en" ) SCREAMING_SNAKE_CASE_ = [ '''L\'affaire NSA souligne l\'absence totale de débat sur le renseignement''', '''Selon moi, il y a deux niveaux de réponse de la part du gouvernement français.''', '''Lorsque François Hollande téléphone à Barack Obama ou quand le ministre des affaires étrangères Laurent''' ''' Fabius convoque l\'ambassadeur des Etats-Unis, ils réagissent à une vraie découverte, qui est celle de''' ''' l\'ampleur de la surveillance américaine sur l\'ensemble des communications en France.''', ] # The below article tests that we don't add any hypotheses outside of the top n_beams SCREAMING_SNAKE_CASE_ = tokenizer(__lowercase , padding=__lowercase , return_tensors="pt" ) SCREAMING_SNAKE_CASE_ = model.generate( input_ids=dct["input_ids"].to(__lowercase ) , attention_mask=dct["attention_mask"].to(__lowercase ) , num_beams=5 , forced_bos_token_id=tokenizer.get_lang_id("en" ) , ) SCREAMING_SNAKE_CASE_ = [ '''The NSA case highlights the total absence of intelligence debate''', '''I think there are two levels of response from the French government.''', '''When François Hollande calls Barack Obama or when Foreign Minister Laurent Fabius calls the U.S.''' ''' Ambassador, they respond to a real discovery, which is that of the scale of U.S. surveillance on all''' ''' communications in France.''', ] SCREAMING_SNAKE_CASE_ = tokenizer.batch_decode( hypotheses_batch.tolist() , clean_up_tokenization_spaces=__lowercase , skip_special_tokens=__lowercase ) assert generated == expected_en
350
from __future__ import annotations import numpy as np def a__ ( __UpperCamelCase ): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = np.shape(__UpperCamelCase ) if rows != columns: SCREAMING_SNAKE_CASE_ = ( "'table' has to be of square shaped array but got a " F'''{rows}x{columns} array:\n{table}''' ) raise ValueError(__UpperCamelCase ) SCREAMING_SNAKE_CASE_ = np.zeros((rows, columns) ) SCREAMING_SNAKE_CASE_ = np.zeros((rows, columns) ) for i in range(__UpperCamelCase ): for j in range(__UpperCamelCase ): SCREAMING_SNAKE_CASE_ = sum(lower[i][k] * upper[k][j] for k in range(__UpperCamelCase ) ) if upper[j][j] == 0: raise ArithmeticError("No LU decomposition exists" ) SCREAMING_SNAKE_CASE_ = (table[i][j] - total) / upper[j][j] SCREAMING_SNAKE_CASE_ = 1 for j in range(__UpperCamelCase , __UpperCamelCase ): SCREAMING_SNAKE_CASE_ = sum(lower[i][k] * upper[k][j] for k in range(__UpperCamelCase ) ) SCREAMING_SNAKE_CASE_ = table[i][j] - total return lower, upper if __name__ == "__main__": import doctest doctest.testmod()
305
0
"""simple docstring""" import unittest from transformers import is_torch_available from transformers.testing_utils import require_torch if is_torch_available(): import torch from transformers.activations import gelu_new, gelu_python, get_activation @require_torch class lowerCamelCase (unittest.TestCase ): """simple docstring""" def __A ( self : Optional[Any] ) -> List[str]: SCREAMING_SNAKE_CASE_ = torch.tensor([-100, -1, -0.1, 0, 0.1, 1.0, 100] ) SCREAMING_SNAKE_CASE_ = get_activation("gelu" ) self.assertTrue(torch.allclose(gelu_python(__magic_name__ ) , torch_builtin(__magic_name__ ) ) ) self.assertFalse(torch.allclose(gelu_python(__magic_name__ ) , gelu_new(__magic_name__ ) ) ) def __A ( self : Optional[Any] ) -> Dict: SCREAMING_SNAKE_CASE_ = torch.tensor([-100, -1, -0.1, 0, 0.1, 1.0, 100] ) SCREAMING_SNAKE_CASE_ = get_activation("gelu" ) SCREAMING_SNAKE_CASE_ = get_activation("gelu_10" ) SCREAMING_SNAKE_CASE_ = torch_builtin(__magic_name__ ) SCREAMING_SNAKE_CASE_ = geluaa(__magic_name__ ) SCREAMING_SNAKE_CASE_ = torch.where(y_gelu_aa < 10.0 , 1 , 0 ) self.assertTrue(torch.max(__magic_name__ ).item() == 10.0 ) self.assertTrue(torch.allclose(y_gelu * clipped_mask , y_gelu_aa * clipped_mask ) ) def __A ( self : Tuple ) -> Union[str, Any]: get_activation("gelu" ) get_activation("gelu_10" ) get_activation("gelu_fast" ) get_activation("gelu_new" ) get_activation("gelu_python" ) get_activation("gelu_pytorch_tanh" ) get_activation("linear" ) get_activation("mish" ) get_activation("quick_gelu" ) get_activation("relu" ) get_activation("sigmoid" ) get_activation("silu" ) get_activation("swish" ) get_activation("tanh" ) with self.assertRaises(__magic_name__ ): get_activation("bogus" ) with self.assertRaises(__magic_name__ ): get_activation(__magic_name__ ) def __A ( self : Any ) -> str: SCREAMING_SNAKE_CASE_ = get_activation("gelu" ) SCREAMING_SNAKE_CASE_ = 1 SCREAMING_SNAKE_CASE_ = get_activation("gelu" ) self.assertEqual(acta.a , 1 ) with self.assertRaises(__magic_name__ ): SCREAMING_SNAKE_CASE_ = acta.a
351
from math import pi, sqrt, tan def a__ ( __UpperCamelCase ): if side_length < 0: raise ValueError("surface_area_cube() only accepts non-negative values" ) return 6 * side_length**2 def a__ ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase ): if length < 0 or breadth < 0 or height < 0: raise ValueError("surface_area_cuboid() only accepts non-negative values" ) return 2 * ((length * breadth) + (breadth * height) + (length * height)) def a__ ( __UpperCamelCase ): if radius < 0: raise ValueError("surface_area_sphere() only accepts non-negative values" ) return 4 * pi * radius**2 def a__ ( __UpperCamelCase ): if radius < 0: raise ValueError("surface_area_hemisphere() only accepts non-negative values" ) return 3 * pi * radius**2 def a__ ( __UpperCamelCase , __UpperCamelCase ): if radius < 0 or height < 0: raise ValueError("surface_area_cone() only accepts non-negative values" ) return pi * radius * (radius + (height**2 + radius**2) ** 0.5) def a__ ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase ): if radius_a < 0 or radius_a < 0 or height < 0: raise ValueError( "surface_area_conical_frustum() only accepts non-negative values" ) SCREAMING_SNAKE_CASE_ = (height**2 + (radius_a - radius_a) ** 2) ** 0.5 return pi * ((slant_height * (radius_a + radius_a)) + radius_a**2 + radius_a**2) def a__ ( __UpperCamelCase , __UpperCamelCase ): if radius < 0 or height < 0: raise ValueError("surface_area_cylinder() only accepts non-negative values" ) return 2 * pi * radius * (height + radius) def a__ ( __UpperCamelCase , __UpperCamelCase ): if torus_radius < 0 or tube_radius < 0: raise ValueError("surface_area_torus() only accepts non-negative values" ) if torus_radius < tube_radius: raise ValueError( "surface_area_torus() does not support spindle or self intersecting tori" ) return 4 * pow(__UpperCamelCase , 2 ) * torus_radius * tube_radius def a__ ( __UpperCamelCase , __UpperCamelCase ): if length < 0 or width < 0: raise ValueError("area_rectangle() only accepts non-negative values" ) return length * width def a__ ( __UpperCamelCase ): if side_length < 0: raise ValueError("area_square() only accepts non-negative values" ) return side_length**2 def a__ ( __UpperCamelCase , __UpperCamelCase ): if base < 0 or height < 0: raise ValueError("area_triangle() only accepts non-negative values" ) return (base * height) / 2 def a__ ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase ): if sidea < 0 or sidea < 0 or sidea < 0: raise ValueError("area_triangle_three_sides() only accepts non-negative values" ) elif sidea + sidea < sidea or sidea + sidea < sidea or sidea + sidea < sidea: raise ValueError("Given three sides do not form a triangle" ) SCREAMING_SNAKE_CASE_ = (sidea + sidea + sidea) / 2 SCREAMING_SNAKE_CASE_ = sqrt( semi_perimeter * (semi_perimeter - sidea) * (semi_perimeter - sidea) * (semi_perimeter - sidea) ) return area def a__ ( __UpperCamelCase , __UpperCamelCase ): if base < 0 or height < 0: raise ValueError("area_parallelogram() only accepts non-negative values" ) return base * height def a__ ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase ): if basea < 0 or basea < 0 or height < 0: raise ValueError("area_trapezium() only accepts non-negative values" ) return 1 / 2 * (basea + basea) * height def a__ ( __UpperCamelCase ): if radius < 0: raise ValueError("area_circle() only accepts non-negative values" ) return pi * radius**2 def a__ ( __UpperCamelCase , __UpperCamelCase ): if radius_x < 0 or radius_y < 0: raise ValueError("area_ellipse() only accepts non-negative values" ) return pi * radius_x * radius_y def a__ ( __UpperCamelCase , __UpperCamelCase ): if diagonal_a < 0 or diagonal_a < 0: raise ValueError("area_rhombus() only accepts non-negative values" ) return 1 / 2 * diagonal_a * diagonal_a def a__ ( __UpperCamelCase , __UpperCamelCase ): if not isinstance(__UpperCamelCase , __UpperCamelCase ) or sides < 3: raise ValueError( "area_reg_polygon() only accepts integers greater than or \ equal to three as number of sides" ) elif length < 0: raise ValueError( "area_reg_polygon() only accepts non-negative values as \ length of a side" ) return (sides * length**2) / (4 * tan(pi / sides )) return (sides * length**2) / (4 * tan(pi / sides )) if __name__ == "__main__": import doctest doctest.testmod(verbose=True) # verbose so we can see methods missing tests print("[DEMO] Areas of various geometric shapes: \n") print(f"Rectangle: {area_rectangle(10, 20) = }") print(f"Square: {area_square(10) = }") print(f"Triangle: {area_triangle(10, 10) = }") print(f"Triangle: {area_triangle_three_sides(5, 12, 13) = }") print(f"Parallelogram: {area_parallelogram(10, 20) = }") print(f"Rhombus: {area_rhombus(10, 20) = }") print(f"Trapezium: {area_trapezium(10, 20, 30) = }") print(f"Circle: {area_circle(20) = }") print(f"Ellipse: {area_ellipse(10, 20) = }") print("\nSurface Areas of various geometric shapes: \n") print(f"Cube: {surface_area_cube(20) = }") print(f"Cuboid: {surface_area_cuboid(10, 20, 30) = }") print(f"Sphere: {surface_area_sphere(20) = }") print(f"Hemisphere: {surface_area_hemisphere(20) = }") print(f"Cone: {surface_area_cone(10, 20) = }") print(f"Conical Frustum: {surface_area_conical_frustum(10, 20, 30) = }") print(f"Cylinder: {surface_area_cylinder(10, 20) = }") print(f"Torus: {surface_area_torus(20, 10) = }") print(f"Equilateral Triangle: {area_reg_polygon(3, 10) = }") print(f"Square: {area_reg_polygon(4, 10) = }") print(f"Reqular Pentagon: {area_reg_polygon(5, 10) = }")
305
0
from collections import namedtuple import requests from lxml import html # type: ignore A : int = namedtuple("covid_data", "cases deaths recovered") def a__ ( __UpperCamelCase = "https://www.worldometers.info/coronavirus/" ): SCREAMING_SNAKE_CASE_ = "//div[@class = \"maincounter-number\"]/span/text()" return covid_data(*html.fromstring(requests.get(__UpperCamelCase ).content ).xpath(__UpperCamelCase ) ) A : Optional[Any] = """Total COVID-19 cases in the world: {} Total deaths due to COVID-19 in the world: {} Total COVID-19 patients recovered in the world: {}""" print(fmt.format(*covid_stats()))
352
from collections import OrderedDict from typing import Any, Mapping, Optional from ... import PreTrainedTokenizer from ...configuration_utils import PretrainedConfig from ...file_utils import TensorType, is_torch_available from ...onnx import OnnxConfig, OnnxConfigWithPast, OnnxSeqaSeqConfigWithPast from ...onnx.utils import compute_effective_axis_dimension from ...utils import logging A : List[str] = logging.get_logger(__name__) A : int = { "facebook/blenderbot_small-90M": "https://huggingface.co/facebook/blenderbot_small-90M/resolve/main/config.json", # See all BlenderbotSmall models at https://huggingface.co/models?filter=blenderbot_small } class lowerCamelCase (SCREAMING_SNAKE_CASE__ ): """simple docstring""" lowerCamelCase__ = '''blenderbot-small''' lowerCamelCase__ = ['''past_key_values'''] lowerCamelCase__ = {'''num_attention_heads''': '''encoder_attention_heads''', '''hidden_size''': '''d_model'''} def __init__( self : Dict , __magic_name__ : Dict=50_265 , __magic_name__ : str=512 , __magic_name__ : List[Any]=8 , __magic_name__ : Any=2_048 , __magic_name__ : Dict=16 , __magic_name__ : Any=8 , __magic_name__ : Optional[int]=2_048 , __magic_name__ : Dict=16 , __magic_name__ : Tuple=0.0 , __magic_name__ : Dict=0.0 , __magic_name__ : Optional[int]=True , __magic_name__ : Any=True , __magic_name__ : Dict="gelu" , __magic_name__ : Tuple=512 , __magic_name__ : List[str]=0.1 , __magic_name__ : List[Any]=0.0 , __magic_name__ : List[Any]=0.0 , __magic_name__ : Tuple=0.02 , __magic_name__ : Union[str, Any]=1 , __magic_name__ : List[Any]=False , __magic_name__ : str=0 , __magic_name__ : Dict=1 , __magic_name__ : str=2 , __magic_name__ : Union[str, Any]=2 , **__magic_name__ : Optional[Any] , ) -> Optional[Any]: SCREAMING_SNAKE_CASE_ = vocab_size SCREAMING_SNAKE_CASE_ = max_position_embeddings SCREAMING_SNAKE_CASE_ = d_model SCREAMING_SNAKE_CASE_ = encoder_ffn_dim SCREAMING_SNAKE_CASE_ = encoder_layers SCREAMING_SNAKE_CASE_ = encoder_attention_heads SCREAMING_SNAKE_CASE_ = decoder_ffn_dim SCREAMING_SNAKE_CASE_ = decoder_layers SCREAMING_SNAKE_CASE_ = decoder_attention_heads SCREAMING_SNAKE_CASE_ = dropout SCREAMING_SNAKE_CASE_ = attention_dropout SCREAMING_SNAKE_CASE_ = activation_dropout SCREAMING_SNAKE_CASE_ = activation_function SCREAMING_SNAKE_CASE_ = init_std SCREAMING_SNAKE_CASE_ = encoder_layerdrop SCREAMING_SNAKE_CASE_ = decoder_layerdrop SCREAMING_SNAKE_CASE_ = use_cache SCREAMING_SNAKE_CASE_ = encoder_layers SCREAMING_SNAKE_CASE_ = scale_embedding # scale factor will be sqrt(d_model) if True super().__init__( pad_token_id=__magic_name__ , bos_token_id=__magic_name__ , eos_token_id=__magic_name__ , is_encoder_decoder=__magic_name__ , decoder_start_token_id=__magic_name__ , forced_eos_token_id=__magic_name__ , **__magic_name__ , ) class lowerCamelCase (SCREAMING_SNAKE_CASE__ ): """simple docstring""" @property def __A ( self : str ) -> Mapping[str, Mapping[int, str]]: if self.task in ["default", "seq2seq-lm"]: SCREAMING_SNAKE_CASE_ = OrderedDict( [ ("input_ids", {0: "batch", 1: "encoder_sequence"}), ("attention_mask", {0: "batch", 1: "encoder_sequence"}), ] ) if self.use_past: SCREAMING_SNAKE_CASE_ = {0: "batch"} SCREAMING_SNAKE_CASE_ = {0: "batch", 1: "past_decoder_sequence + sequence"} else: SCREAMING_SNAKE_CASE_ = {0: "batch", 1: "decoder_sequence"} SCREAMING_SNAKE_CASE_ = {0: "batch", 1: "decoder_sequence"} if self.use_past: self.fill_with_past_key_values_(__magic_name__ , direction="inputs" ) elif self.task == "causal-lm": # TODO: figure this case out. SCREAMING_SNAKE_CASE_ = OrderedDict( [ ("input_ids", {0: "batch", 1: "encoder_sequence"}), ("attention_mask", {0: "batch", 1: "encoder_sequence"}), ] ) if self.use_past: SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = self.num_layers for i in range(__magic_name__ ): SCREAMING_SNAKE_CASE_ = {0: "batch", 2: "past_sequence + sequence"} SCREAMING_SNAKE_CASE_ = {0: "batch", 2: "past_sequence + sequence"} else: SCREAMING_SNAKE_CASE_ = OrderedDict( [ ("input_ids", {0: "batch", 1: "encoder_sequence"}), ("attention_mask", {0: "batch", 1: "encoder_sequence"}), ("decoder_input_ids", {0: "batch", 1: "decoder_sequence"}), ("decoder_attention_mask", {0: "batch", 1: "decoder_sequence"}), ] ) return common_inputs @property def __A ( self : Tuple ) -> Mapping[str, Mapping[int, str]]: if self.task in ["default", "seq2seq-lm"]: SCREAMING_SNAKE_CASE_ = super().outputs else: SCREAMING_SNAKE_CASE_ = super(__magic_name__ , self ).outputs if self.use_past: SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = self.num_layers for i in range(__magic_name__ ): SCREAMING_SNAKE_CASE_ = {0: "batch", 2: "past_sequence + sequence"} SCREAMING_SNAKE_CASE_ = {0: "batch", 2: "past_sequence + sequence"} return common_outputs def __A ( self : int , __magic_name__ : PreTrainedTokenizer , __magic_name__ : int = -1 , __magic_name__ : int = -1 , __magic_name__ : bool = False , __magic_name__ : Optional[TensorType] = None , ) -> Mapping[str, Any]: SCREAMING_SNAKE_CASE_ = self._generate_dummy_inputs_for_sequence_classification_and_question_answering( __magic_name__ , __magic_name__ , __magic_name__ , __magic_name__ , __magic_name__ ) # Generate decoder inputs SCREAMING_SNAKE_CASE_ = seq_length if not self.use_past else 1 SCREAMING_SNAKE_CASE_ = self._generate_dummy_inputs_for_sequence_classification_and_question_answering( __magic_name__ , __magic_name__ , __magic_name__ , __magic_name__ , __magic_name__ ) SCREAMING_SNAKE_CASE_ = {F'''decoder_{name}''': tensor for name, tensor in decoder_inputs.items()} SCREAMING_SNAKE_CASE_ = dict(**__magic_name__ , **__magic_name__ ) if self.use_past: if not is_torch_available(): raise ValueError("Cannot generate dummy past_keys inputs without PyTorch installed." ) else: import torch SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = common_inputs["input_ids"].shape SCREAMING_SNAKE_CASE_ = common_inputs["decoder_input_ids"].shape[1] SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = self.num_attention_heads SCREAMING_SNAKE_CASE_ = ( batch, num_encoder_attention_heads, encoder_seq_length, self._config.hidden_size // num_encoder_attention_heads, ) SCREAMING_SNAKE_CASE_ = decoder_seq_length + 3 SCREAMING_SNAKE_CASE_ = ( batch, num_decoder_attention_heads, decoder_past_length, self._config.hidden_size // num_decoder_attention_heads, ) SCREAMING_SNAKE_CASE_ = torch.cat( [common_inputs["decoder_attention_mask"], torch.ones(__magic_name__ , __magic_name__ )] , dim=1 ) SCREAMING_SNAKE_CASE_ = [] # If the number of encoder and decoder layers are present in the model configuration, both are considered SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = self.num_layers SCREAMING_SNAKE_CASE_ = min(__magic_name__ , __magic_name__ ) SCREAMING_SNAKE_CASE_ = max(__magic_name__ , __magic_name__ ) - min_num_layers SCREAMING_SNAKE_CASE_ = "encoder" if num_encoder_layers > num_decoder_layers else "decoder" for _ in range(__magic_name__ ): common_inputs["past_key_values"].append( ( torch.zeros(__magic_name__ ), torch.zeros(__magic_name__ ), torch.zeros(__magic_name__ ), torch.zeros(__magic_name__ ), ) ) # TODO: test this. SCREAMING_SNAKE_CASE_ = encoder_shape if remaining_side_name == "encoder" else decoder_shape for _ in range(__magic_name__ , __magic_name__ ): common_inputs["past_key_values"].append((torch.zeros(__magic_name__ ), torch.zeros(__magic_name__ )) ) return common_inputs def __A ( self : Union[str, Any] , __magic_name__ : PreTrainedTokenizer , __magic_name__ : int = -1 , __magic_name__ : int = -1 , __magic_name__ : bool = False , __magic_name__ : Optional[TensorType] = None , ) -> Mapping[str, Any]: SCREAMING_SNAKE_CASE_ = self._generate_dummy_inputs_for_sequence_classification_and_question_answering( __magic_name__ , __magic_name__ , __magic_name__ , __magic_name__ , __magic_name__ ) if self.use_past: if not is_torch_available(): raise ValueError("Cannot generate dummy past_keys inputs without PyTorch installed." ) else: import torch SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = common_inputs["input_ids"].shape # Not using the same length for past_key_values SCREAMING_SNAKE_CASE_ = seqlen + 2 SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = self.num_layers SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = self.num_attention_heads SCREAMING_SNAKE_CASE_ = ( batch, num_encoder_attention_heads, past_key_values_length, self._config.hidden_size // num_encoder_attention_heads, ) SCREAMING_SNAKE_CASE_ = common_inputs["attention_mask"].dtype SCREAMING_SNAKE_CASE_ = torch.cat( [common_inputs["attention_mask"], torch.ones(__magic_name__ , __magic_name__ , dtype=__magic_name__ )] , dim=1 ) SCREAMING_SNAKE_CASE_ = [ (torch.zeros(__magic_name__ ), torch.zeros(__magic_name__ )) for _ in range(__magic_name__ ) ] return common_inputs def __A ( self : Dict , __magic_name__ : PreTrainedTokenizer , __magic_name__ : int = -1 , __magic_name__ : int = -1 , __magic_name__ : bool = False , __magic_name__ : Optional[TensorType] = None , ) -> Mapping[str, Any]: # Copied from OnnxConfig.generate_dummy_inputs # Did not use super(OnnxConfigWithPast, self).generate_dummy_inputs for code clarity. # If dynamic axis (-1) we forward with a fixed dimension of 2 samples to avoid optimizations made by ONNX SCREAMING_SNAKE_CASE_ = compute_effective_axis_dimension( __magic_name__ , fixed_dimension=OnnxConfig.default_fixed_batch , num_token_to_add=0 ) # If dynamic axis (-1) we forward with a fixed dimension of 8 tokens to avoid optimizations made by ONNX SCREAMING_SNAKE_CASE_ = tokenizer.num_special_tokens_to_add(__magic_name__ ) SCREAMING_SNAKE_CASE_ = compute_effective_axis_dimension( __magic_name__ , fixed_dimension=OnnxConfig.default_fixed_sequence , num_token_to_add=__magic_name__ ) # Generate dummy inputs according to compute batch and sequence SCREAMING_SNAKE_CASE_ = [" ".join([tokenizer.unk_token] ) * seq_length] * batch_size SCREAMING_SNAKE_CASE_ = dict(tokenizer(__magic_name__ , return_tensors=__magic_name__ ) ) return common_inputs def __A ( self : Optional[Any] , __magic_name__ : PreTrainedTokenizer , __magic_name__ : int = -1 , __magic_name__ : int = -1 , __magic_name__ : bool = False , __magic_name__ : Optional[TensorType] = None , ) -> Mapping[str, Any]: if self.task in ["default", "seq2seq-lm"]: SCREAMING_SNAKE_CASE_ = self._generate_dummy_inputs_for_default_and_seqaseq_lm( __magic_name__ , batch_size=__magic_name__ , seq_length=__magic_name__ , is_pair=__magic_name__ , framework=__magic_name__ ) elif self.task == "causal-lm": SCREAMING_SNAKE_CASE_ = self._generate_dummy_inputs_for_causal_lm( __magic_name__ , batch_size=__magic_name__ , seq_length=__magic_name__ , is_pair=__magic_name__ , framework=__magic_name__ ) else: SCREAMING_SNAKE_CASE_ = self._generate_dummy_inputs_for_sequence_classification_and_question_answering( __magic_name__ , batch_size=__magic_name__ , seq_length=__magic_name__ , is_pair=__magic_name__ , framework=__magic_name__ ) return common_inputs def __A ( self : Optional[Any] , __magic_name__ : str , __magic_name__ : List[Any] , __magic_name__ : str , __magic_name__ : List[str] ) -> List[str]: if self.task in ["default", "seq2seq-lm"]: SCREAMING_SNAKE_CASE_ = super()._flatten_past_key_values_(__magic_name__ , __magic_name__ , __magic_name__ , __magic_name__ ) else: SCREAMING_SNAKE_CASE_ = super(__magic_name__ , self )._flatten_past_key_values_( __magic_name__ , __magic_name__ , __magic_name__ , __magic_name__ )
305
0
from ...configuration_utils import PretrainedConfig from ...utils import logging A : str = logging.get_logger(__name__) A : Union[str, Any] = { "MIT/ast-finetuned-audioset-10-10-0.4593": ( "https://huggingface.co/MIT/ast-finetuned-audioset-10-10-0.4593/resolve/main/config.json" ), } class lowerCamelCase (snake_case_ ): """simple docstring""" lowerCamelCase__ = '''audio-spectrogram-transformer''' def __init__( self : Any , __magic_name__ : List[Any]=768 , __magic_name__ : Optional[Any]=12 , __magic_name__ : Tuple=12 , __magic_name__ : Union[str, Any]=3_072 , __magic_name__ : Dict="gelu" , __magic_name__ : Dict=0.0 , __magic_name__ : Optional[Any]=0.0 , __magic_name__ : Optional[Any]=0.02 , __magic_name__ : str=1e-12 , __magic_name__ : int=16 , __magic_name__ : List[Any]=True , __magic_name__ : str=10 , __magic_name__ : str=10 , __magic_name__ : List[str]=1_024 , __magic_name__ : Tuple=128 , **__magic_name__ : List[str] , ) -> List[Any]: super().__init__(**__magic_name__ ) 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_ = initializer_range SCREAMING_SNAKE_CASE_ = layer_norm_eps SCREAMING_SNAKE_CASE_ = patch_size SCREAMING_SNAKE_CASE_ = qkv_bias SCREAMING_SNAKE_CASE_ = frequency_stride SCREAMING_SNAKE_CASE_ = time_stride SCREAMING_SNAKE_CASE_ = max_length SCREAMING_SNAKE_CASE_ = num_mel_bins
353
import inspect import unittest from datasets import load_dataset from packaging import version from transformers import BeitConfig from transformers.models.auto import get_values from transformers.testing_utils import require_torch, require_torch_multi_gpu, require_vision, slow, torch_device from transformers.utils import cached_property, is_torch_available, is_vision_available from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, _config_zero_init, floats_tensor, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from torch import nn from transformers import ( MODEL_MAPPING, BeitForImageClassification, BeitForMaskedImageModeling, BeitForSemanticSegmentation, BeitModel, ) from transformers.models.beit.modeling_beit import BEIT_PRETRAINED_MODEL_ARCHIVE_LIST if is_vision_available(): import PIL from PIL import Image from transformers import BeitImageProcessor class lowerCamelCase : """simple docstring""" def __init__( self : List[Any] , __magic_name__ : List[str] , __magic_name__ : int=100 , __magic_name__ : Optional[Any]=13 , __magic_name__ : Dict=30 , __magic_name__ : Tuple=2 , __magic_name__ : str=3 , __magic_name__ : str=True , __magic_name__ : Optional[int]=True , __magic_name__ : Union[str, Any]=32 , __magic_name__ : Optional[int]=4 , __magic_name__ : Dict=4 , __magic_name__ : Tuple=37 , __magic_name__ : Any="gelu" , __magic_name__ : int=0.1 , __magic_name__ : List[str]=0.1 , __magic_name__ : Optional[int]=10 , __magic_name__ : Tuple=0.02 , __magic_name__ : Optional[int]=3 , __magic_name__ : List[str]=None , __magic_name__ : Tuple=[0, 1, 2, 3] , ) -> List[str]: SCREAMING_SNAKE_CASE_ = parent SCREAMING_SNAKE_CASE_ = 100 SCREAMING_SNAKE_CASE_ = batch_size SCREAMING_SNAKE_CASE_ = image_size SCREAMING_SNAKE_CASE_ = patch_size SCREAMING_SNAKE_CASE_ = num_channels SCREAMING_SNAKE_CASE_ = is_training SCREAMING_SNAKE_CASE_ = use_labels 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_ = type_sequence_label_size SCREAMING_SNAKE_CASE_ = initializer_range SCREAMING_SNAKE_CASE_ = scope SCREAMING_SNAKE_CASE_ = out_indices SCREAMING_SNAKE_CASE_ = num_labels # in BeiT, the seq length equals the number of patches + 1 (we add 1 for the [CLS] token) SCREAMING_SNAKE_CASE_ = (image_size // patch_size) ** 2 SCREAMING_SNAKE_CASE_ = num_patches + 1 def __A ( self : Any ) -> int: SCREAMING_SNAKE_CASE_ = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_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.image_size, self.image_size] , self.num_labels ) SCREAMING_SNAKE_CASE_ = self.get_config() return config, pixel_values, labels, pixel_labels def __A ( self : Dict ) -> Optional[int]: return BeitConfig( vocab_size=self.vocab_size , image_size=self.image_size , patch_size=self.patch_size , num_channels=self.num_channels , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , is_decoder=__magic_name__ , initializer_range=self.initializer_range , out_indices=self.out_indices , ) def __A ( self : Optional[int] , __magic_name__ : List[str] , __magic_name__ : Optional[int] , __magic_name__ : Tuple , __magic_name__ : Tuple ) -> Optional[int]: SCREAMING_SNAKE_CASE_ = BeitModel(config=__magic_name__ ) model.to(__magic_name__ ) model.eval() SCREAMING_SNAKE_CASE_ = model(__magic_name__ ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def __A ( self : Union[str, Any] , __magic_name__ : List[str] , __magic_name__ : int , __magic_name__ : int , __magic_name__ : str ) -> int: SCREAMING_SNAKE_CASE_ = BeitForMaskedImageModeling(config=__magic_name__ ) model.to(__magic_name__ ) model.eval() SCREAMING_SNAKE_CASE_ = model(__magic_name__ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length - 1, self.vocab_size) ) def __A ( self : Dict , __magic_name__ : List[Any] , __magic_name__ : int , __magic_name__ : Tuple , __magic_name__ : Optional[Any] ) -> int: SCREAMING_SNAKE_CASE_ = self.type_sequence_label_size SCREAMING_SNAKE_CASE_ = BeitForImageClassification(__magic_name__ ) model.to(__magic_name__ ) model.eval() SCREAMING_SNAKE_CASE_ = model(__magic_name__ , labels=__magic_name__ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.type_sequence_label_size) ) # test greyscale images SCREAMING_SNAKE_CASE_ = 1 SCREAMING_SNAKE_CASE_ = BeitForImageClassification(__magic_name__ ) model.to(__magic_name__ ) model.eval() SCREAMING_SNAKE_CASE_ = floats_tensor([self.batch_size, 1, self.image_size, self.image_size] ) SCREAMING_SNAKE_CASE_ = model(__magic_name__ , labels=__magic_name__ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.type_sequence_label_size) ) def __A ( self : Tuple , __magic_name__ : Any , __magic_name__ : Optional[Any] , __magic_name__ : Tuple , __magic_name__ : int ) -> int: SCREAMING_SNAKE_CASE_ = self.num_labels SCREAMING_SNAKE_CASE_ = BeitForSemanticSegmentation(__magic_name__ ) model.to(__magic_name__ ) model.eval() SCREAMING_SNAKE_CASE_ = model(__magic_name__ ) self.parent.assertEqual( result.logits.shape , (self.batch_size, self.num_labels, self.image_size * 2, self.image_size * 2) ) SCREAMING_SNAKE_CASE_ = model(__magic_name__ , labels=__magic_name__ ) self.parent.assertEqual( result.logits.shape , (self.batch_size, self.num_labels, self.image_size * 2, self.image_size * 2) ) def __A ( self : str ) -> List[Any]: SCREAMING_SNAKE_CASE_ = self.prepare_config_and_inputs() SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = config_and_inputs SCREAMING_SNAKE_CASE_ = {"pixel_values": pixel_values} return config, inputs_dict @require_torch class lowerCamelCase (SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , unittest.TestCase ): """simple docstring""" lowerCamelCase__ = ( (BeitModel, BeitForImageClassification, BeitForMaskedImageModeling, BeitForSemanticSegmentation) if is_torch_available() else () ) lowerCamelCase__ = ( { '''feature-extraction''': BeitModel, '''image-classification''': BeitForImageClassification, '''image-segmentation''': BeitForSemanticSegmentation, } if is_torch_available() else {} ) lowerCamelCase__ = False lowerCamelCase__ = False lowerCamelCase__ = False def __A ( self : Tuple ) -> Any: SCREAMING_SNAKE_CASE_ = BeitModelTester(self ) SCREAMING_SNAKE_CASE_ = ConfigTester(self , config_class=__magic_name__ , has_text_modality=__magic_name__ , hidden_size=37 ) def __A ( self : Dict ) -> List[Any]: self.config_tester.run_common_tests() @unittest.skip(reason="BEiT does not use inputs_embeds" ) def __A ( self : List[str] ) -> Optional[Any]: pass @require_torch_multi_gpu @unittest.skip(reason="BEiT has some layers using `add_module` which doesn't work well with `nn.DataParallel`" ) def __A ( self : str ) -> List[str]: pass def __A ( self : List[Any] ) -> 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: SCREAMING_SNAKE_CASE_ = model_class(__magic_name__ ) self.assertIsInstance(model.get_input_embeddings() , (nn.Module) ) SCREAMING_SNAKE_CASE_ = model.get_output_embeddings() self.assertTrue(x is None or isinstance(__magic_name__ , nn.Linear ) ) def __A ( self : Union[str, Any] ) -> int: 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(__magic_name__ ) SCREAMING_SNAKE_CASE_ = inspect.signature(model.forward ) # signature.parameters is an OrderedDict => so arg_names order is deterministic SCREAMING_SNAKE_CASE_ = [*signature.parameters.keys()] SCREAMING_SNAKE_CASE_ = ["pixel_values"] self.assertListEqual(arg_names[:1] , __magic_name__ ) def __A ( self : Tuple ) -> Union[str, Any]: SCREAMING_SNAKE_CASE_ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*__magic_name__ ) def __A ( self : Dict ) -> Union[str, Any]: SCREAMING_SNAKE_CASE_ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_masked_lm(*__magic_name__ ) def __A ( self : Optional[int] ) -> List[Any]: SCREAMING_SNAKE_CASE_ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_image_classification(*__magic_name__ ) def __A ( self : Optional[Any] ) -> List[str]: SCREAMING_SNAKE_CASE_ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_semantic_segmentation(*__magic_name__ ) def __A ( self : int ) -> Optional[int]: if not self.model_tester.is_training: return SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = self.model_tester.prepare_config_and_inputs_for_common() SCREAMING_SNAKE_CASE_ = True for model_class in self.all_model_classes: # we don't test BeitForMaskedImageModeling if model_class in [*get_values(__magic_name__ ), BeitForMaskedImageModeling]: continue SCREAMING_SNAKE_CASE_ = model_class(__magic_name__ ) model.to(__magic_name__ ) model.train() SCREAMING_SNAKE_CASE_ = self._prepare_for_class(__magic_name__ , __magic_name__ , return_labels=__magic_name__ ) SCREAMING_SNAKE_CASE_ = model(**__magic_name__ ).loss loss.backward() def __A ( self : Any ) -> Tuple: SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = self.model_tester.prepare_config_and_inputs_for_common() if not self.model_tester.is_training: return SCREAMING_SNAKE_CASE_ = False SCREAMING_SNAKE_CASE_ = True for model_class in self.all_model_classes: # we don't test BeitForMaskedImageModeling if ( model_class in [*get_values(__magic_name__ ), BeitForMaskedImageModeling] or not model_class.supports_gradient_checkpointing ): continue SCREAMING_SNAKE_CASE_ = model_class(__magic_name__ ) model.gradient_checkpointing_enable() model.to(__magic_name__ ) model.train() SCREAMING_SNAKE_CASE_ = self._prepare_for_class(__magic_name__ , __magic_name__ , return_labels=__magic_name__ ) SCREAMING_SNAKE_CASE_ = model(**__magic_name__ ).loss loss.backward() def __A ( self : List[str] ) -> str: SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = self.model_tester.prepare_config_and_inputs_for_common() SCREAMING_SNAKE_CASE_ = _config_zero_init(__magic_name__ ) for model_class in self.all_model_classes: SCREAMING_SNAKE_CASE_ = model_class(config=__magic_name__ ) for name, param in model.named_parameters(): # we skip lambda parameters as these require special initial values # determined by config.layer_scale_init_value if "lambda" in name: continue if param.requires_grad: self.assertIn( ((param.data.mean() * 1e9).round() / 1e9).item() , [0.0, 1.0] , msg=F'''Parameter {name} of model {model_class} seems not properly initialized''' , ) @slow def __A ( self : int ) -> Optional[int]: for model_name in BEIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: SCREAMING_SNAKE_CASE_ = BeitModel.from_pretrained(__magic_name__ ) self.assertIsNotNone(__magic_name__ ) def a__ ( ): SCREAMING_SNAKE_CASE_ = Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png" ) return image @require_torch @require_vision class lowerCamelCase (unittest.TestCase ): """simple docstring""" @cached_property def __A ( self : List[Any] ) -> str: return BeitImageProcessor.from_pretrained("microsoft/beit-base-patch16-224" ) if is_vision_available() else None @slow def __A ( self : List[str] ) -> Union[str, Any]: SCREAMING_SNAKE_CASE_ = BeitForMaskedImageModeling.from_pretrained("microsoft/beit-base-patch16-224-pt22k" ).to(__magic_name__ ) SCREAMING_SNAKE_CASE_ = self.default_image_processor SCREAMING_SNAKE_CASE_ = prepare_img() SCREAMING_SNAKE_CASE_ = image_processor(images=__magic_name__ , return_tensors="pt" ).pixel_values.to(__magic_name__ ) # prepare bool_masked_pos SCREAMING_SNAKE_CASE_ = torch.ones((1, 196) , dtype=torch.bool ).to(__magic_name__ ) # forward pass with torch.no_grad(): SCREAMING_SNAKE_CASE_ = model(pixel_values=__magic_name__ , bool_masked_pos=__magic_name__ ) SCREAMING_SNAKE_CASE_ = outputs.logits # verify the logits SCREAMING_SNAKE_CASE_ = torch.Size((1, 196, 8_192) ) self.assertEqual(logits.shape , __magic_name__ ) SCREAMING_SNAKE_CASE_ = torch.tensor( [[-3.2437, 0.5072, -13.9174], [-3.2456, 0.4948, -13.9401], [-3.2033, 0.5121, -13.8550]] ).to(__magic_name__ ) self.assertTrue(torch.allclose(logits[bool_masked_pos][:3, :3] , __magic_name__ , atol=1e-2 ) ) @slow def __A ( self : Tuple ) -> int: SCREAMING_SNAKE_CASE_ = BeitForImageClassification.from_pretrained("microsoft/beit-base-patch16-224" ).to(__magic_name__ ) SCREAMING_SNAKE_CASE_ = self.default_image_processor SCREAMING_SNAKE_CASE_ = prepare_img() SCREAMING_SNAKE_CASE_ = image_processor(images=__magic_name__ , return_tensors="pt" ).to(__magic_name__ ) # forward pass with torch.no_grad(): SCREAMING_SNAKE_CASE_ = model(**__magic_name__ ) SCREAMING_SNAKE_CASE_ = outputs.logits # verify the logits SCREAMING_SNAKE_CASE_ = torch.Size((1, 1_000) ) self.assertEqual(logits.shape , __magic_name__ ) SCREAMING_SNAKE_CASE_ = torch.tensor([-1.2385, -1.0987, -1.0108] ).to(__magic_name__ ) self.assertTrue(torch.allclose(logits[0, :3] , __magic_name__ , atol=1e-4 ) ) SCREAMING_SNAKE_CASE_ = 281 self.assertEqual(logits.argmax(-1 ).item() , __magic_name__ ) @slow def __A ( self : Optional[Any] ) -> int: SCREAMING_SNAKE_CASE_ = BeitForImageClassification.from_pretrained("microsoft/beit-large-patch16-224-pt22k-ft22k" ).to( __magic_name__ ) SCREAMING_SNAKE_CASE_ = self.default_image_processor SCREAMING_SNAKE_CASE_ = prepare_img() SCREAMING_SNAKE_CASE_ = image_processor(images=__magic_name__ , return_tensors="pt" ).to(__magic_name__ ) # forward pass with torch.no_grad(): SCREAMING_SNAKE_CASE_ = model(**__magic_name__ ) SCREAMING_SNAKE_CASE_ = outputs.logits # verify the logits SCREAMING_SNAKE_CASE_ = torch.Size((1, 21_841) ) self.assertEqual(logits.shape , __magic_name__ ) SCREAMING_SNAKE_CASE_ = torch.tensor([1.6881, -0.2787, 0.5901] ).to(__magic_name__ ) self.assertTrue(torch.allclose(logits[0, :3] , __magic_name__ , atol=1e-4 ) ) SCREAMING_SNAKE_CASE_ = 2_396 self.assertEqual(logits.argmax(-1 ).item() , __magic_name__ ) @slow def __A ( self : Tuple ) -> Tuple: SCREAMING_SNAKE_CASE_ = BeitForSemanticSegmentation.from_pretrained("microsoft/beit-base-finetuned-ade-640-640" ) SCREAMING_SNAKE_CASE_ = model.to(__magic_name__ ) SCREAMING_SNAKE_CASE_ = BeitImageProcessor(do_resize=__magic_name__ , size=640 , do_center_crop=__magic_name__ ) SCREAMING_SNAKE_CASE_ = load_dataset("hf-internal-testing/fixtures_ade20k" , split="test" ) SCREAMING_SNAKE_CASE_ = Image.open(ds[0]["file"] ) SCREAMING_SNAKE_CASE_ = image_processor(images=__magic_name__ , return_tensors="pt" ).to(__magic_name__ ) # forward pass with torch.no_grad(): SCREAMING_SNAKE_CASE_ = model(**__magic_name__ ) SCREAMING_SNAKE_CASE_ = outputs.logits # verify the logits SCREAMING_SNAKE_CASE_ = torch.Size((1, 150, 160, 160) ) self.assertEqual(logits.shape , __magic_name__ ) SCREAMING_SNAKE_CASE_ = version.parse(PIL.__version__ ) < version.parse("9.0.0" ) if is_pillow_less_than_a: SCREAMING_SNAKE_CASE_ = torch.tensor( [ [[-4.9225, -2.3954, -3.0522], [-2.8822, -1.0046, -1.7561], [-2.9549, -1.3228, -2.1347]], [[-5.8168, -3.4129, -4.0778], [-3.8651, -2.2214, -3.0277], [-3.8356, -2.4643, -3.3535]], [[-0.0078, 3.9952, 4.0754], [2.9856, 4.6944, 5.0035], [3.2413, 4.7813, 4.9969]], ] , device=__magic_name__ , ) else: SCREAMING_SNAKE_CASE_ = torch.tensor( [ [[-4.8960, -2.3688, -3.0355], [-2.8478, -0.9836, -1.7418], [-2.9449, -1.3332, -2.1456]], [[-5.8081, -3.4124, -4.1006], [-3.8561, -2.2081, -3.0323], [-3.8365, -2.4601, -3.3669]], [[-0.0309, 3.9868, 4.0540], [2.9640, 4.6877, 4.9976], [3.2081, 4.7690, 4.9942]], ] , device=__magic_name__ , ) self.assertTrue(torch.allclose(logits[0, :3, :3, :3] , __magic_name__ , atol=1e-4 ) ) @slow def __A ( self : List[str] ) -> Tuple: SCREAMING_SNAKE_CASE_ = BeitForSemanticSegmentation.from_pretrained("microsoft/beit-base-finetuned-ade-640-640" ) SCREAMING_SNAKE_CASE_ = model.to(__magic_name__ ) SCREAMING_SNAKE_CASE_ = BeitImageProcessor(do_resize=__magic_name__ , size=640 , do_center_crop=__magic_name__ ) SCREAMING_SNAKE_CASE_ = load_dataset("hf-internal-testing/fixtures_ade20k" , split="test" ) SCREAMING_SNAKE_CASE_ = Image.open(ds[0]["file"] ) SCREAMING_SNAKE_CASE_ = image_processor(images=__magic_name__ , return_tensors="pt" ).to(__magic_name__ ) # forward pass with torch.no_grad(): SCREAMING_SNAKE_CASE_ = model(**__magic_name__ ) SCREAMING_SNAKE_CASE_ = outputs.logits.detach().cpu() SCREAMING_SNAKE_CASE_ = image_processor.post_process_semantic_segmentation(outputs=__magic_name__ , target_sizes=[(500, 300)] ) SCREAMING_SNAKE_CASE_ = torch.Size((500, 300) ) self.assertEqual(segmentation[0].shape , __magic_name__ ) SCREAMING_SNAKE_CASE_ = image_processor.post_process_semantic_segmentation(outputs=__magic_name__ ) SCREAMING_SNAKE_CASE_ = torch.Size((160, 160) ) self.assertEqual(segmentation[0].shape , __magic_name__ )
305
0
"""simple docstring""" from typing import TYPE_CHECKING # rely on isort to merge the imports from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tokenizers_available, is_torch_available A : Any = { "configuration_cpmant": ["CPMANT_PRETRAINED_CONFIG_ARCHIVE_MAP", "CpmAntConfig"], "tokenization_cpmant": ["CpmAntTokenizer"], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: A : List[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 A : Union[str, Any] = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
354
from __future__ import annotations A : Dict = "#" class lowerCamelCase : """simple docstring""" def __init__( self : Dict ) -> None: SCREAMING_SNAKE_CASE_ = {} def __A ( self : List[Any] , __magic_name__ : str ) -> None: SCREAMING_SNAKE_CASE_ = self._trie for char in text: if char not in trie: SCREAMING_SNAKE_CASE_ = {} SCREAMING_SNAKE_CASE_ = trie[char] SCREAMING_SNAKE_CASE_ = True def __A ( self : Union[str, Any] , __magic_name__ : str ) -> tuple | list: SCREAMING_SNAKE_CASE_ = self._trie for char in prefix: if char in trie: SCREAMING_SNAKE_CASE_ = trie[char] else: return [] return self._elements(__magic_name__ ) def __A ( self : int , __magic_name__ : dict ) -> tuple: SCREAMING_SNAKE_CASE_ = [] for c, v in d.items(): SCREAMING_SNAKE_CASE_ = [" "] if c == END else [(c + s) for s in self._elements(__magic_name__ )] result.extend(__magic_name__ ) return tuple(__magic_name__ ) A : Union[str, Any] = Trie() A : Optional[int] = ("depart", "detergent", "daring", "dog", "deer", "deal") for word in words: trie.insert_word(word) def a__ ( __UpperCamelCase ): SCREAMING_SNAKE_CASE_ = trie.find_word(__UpperCamelCase ) return tuple(string + word for word in suffixes ) def a__ ( ): print(autocomplete_using_trie("de" ) ) if __name__ == "__main__": import doctest doctest.testmod() main()
305
0
import collections import inspect import unittest from transformers import SwinvaConfig from transformers.testing_utils import require_torch, require_vision, slow, torch_device from transformers.utils import cached_property, is_torch_available, is_vision_available from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, _config_zero_init, floats_tensor, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from torch import nn from transformers import SwinvaForImageClassification, SwinvaForMaskedImageModeling, SwinvaModel from transformers.models.swinva.modeling_swinva import SWINV2_PRETRAINED_MODEL_ARCHIVE_LIST if is_vision_available(): from PIL import Image from transformers import AutoImageProcessor class lowerCamelCase : """simple docstring""" def __init__( self : Optional[int] , __magic_name__ : int , __magic_name__ : int=13 , __magic_name__ : List[Any]=32 , __magic_name__ : int=2 , __magic_name__ : Any=3 , __magic_name__ : Optional[int]=16 , __magic_name__ : Any=[1, 2, 1] , __magic_name__ : Tuple=[2, 2, 4] , __magic_name__ : str=2 , __magic_name__ : List[Any]=2.0 , __magic_name__ : Any=True , __magic_name__ : List[Any]=0.0 , __magic_name__ : List[str]=0.0 , __magic_name__ : Optional[int]=0.1 , __magic_name__ : Optional[int]="gelu" , __magic_name__ : int=False , __magic_name__ : List[Any]=True , __magic_name__ : Optional[Any]=0.02 , __magic_name__ : str=1e-5 , __magic_name__ : List[str]=True , __magic_name__ : Optional[int]=None , __magic_name__ : Union[str, Any]=True , __magic_name__ : List[str]=10 , __magic_name__ : Tuple=8 , ) -> Optional[Any]: SCREAMING_SNAKE_CASE_ = parent SCREAMING_SNAKE_CASE_ = batch_size SCREAMING_SNAKE_CASE_ = image_size SCREAMING_SNAKE_CASE_ = patch_size SCREAMING_SNAKE_CASE_ = num_channels SCREAMING_SNAKE_CASE_ = embed_dim SCREAMING_SNAKE_CASE_ = depths SCREAMING_SNAKE_CASE_ = num_heads SCREAMING_SNAKE_CASE_ = window_size SCREAMING_SNAKE_CASE_ = mlp_ratio SCREAMING_SNAKE_CASE_ = qkv_bias SCREAMING_SNAKE_CASE_ = hidden_dropout_prob SCREAMING_SNAKE_CASE_ = attention_probs_dropout_prob SCREAMING_SNAKE_CASE_ = drop_path_rate SCREAMING_SNAKE_CASE_ = hidden_act SCREAMING_SNAKE_CASE_ = use_absolute_embeddings SCREAMING_SNAKE_CASE_ = patch_norm SCREAMING_SNAKE_CASE_ = layer_norm_eps SCREAMING_SNAKE_CASE_ = initializer_range SCREAMING_SNAKE_CASE_ = is_training SCREAMING_SNAKE_CASE_ = scope SCREAMING_SNAKE_CASE_ = use_labels SCREAMING_SNAKE_CASE_ = type_sequence_label_size SCREAMING_SNAKE_CASE_ = encoder_stride def __A ( self : int ) -> List[Any]: SCREAMING_SNAKE_CASE_ = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) SCREAMING_SNAKE_CASE_ = None if self.use_labels: SCREAMING_SNAKE_CASE_ = ids_tensor([self.batch_size] , self.type_sequence_label_size ) SCREAMING_SNAKE_CASE_ = self.get_config() return config, pixel_values, labels def __A ( self : str ) -> Optional[int]: return SwinvaConfig( image_size=self.image_size , patch_size=self.patch_size , num_channels=self.num_channels , embed_dim=self.embed_dim , depths=self.depths , num_heads=self.num_heads , window_size=self.window_size , mlp_ratio=self.mlp_ratio , qkv_bias=self.qkv_bias , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , drop_path_rate=self.drop_path_rate , hidden_act=self.hidden_act , use_absolute_embeddings=self.use_absolute_embeddings , path_norm=self.patch_norm , layer_norm_eps=self.layer_norm_eps , initializer_range=self.initializer_range , encoder_stride=self.encoder_stride , ) def __A ( self : Any , __magic_name__ : Union[str, Any] , __magic_name__ : Tuple , __magic_name__ : Any ) -> Union[str, Any]: SCREAMING_SNAKE_CASE_ = SwinvaModel(config=_SCREAMING_SNAKE_CASE ) model.to(_SCREAMING_SNAKE_CASE ) model.eval() SCREAMING_SNAKE_CASE_ = model(_SCREAMING_SNAKE_CASE ) SCREAMING_SNAKE_CASE_ = ((config.image_size // config.patch_size) ** 2) // (4 ** (len(config.depths ) - 1)) SCREAMING_SNAKE_CASE_ = int(config.embed_dim * 2 ** (len(config.depths ) - 1) ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, expected_seq_len, expected_dim) ) def __A ( self : Optional[int] , __magic_name__ : Optional[int] , __magic_name__ : Optional[int] , __magic_name__ : int ) -> List[str]: SCREAMING_SNAKE_CASE_ = SwinvaForMaskedImageModeling(config=_SCREAMING_SNAKE_CASE ) model.to(_SCREAMING_SNAKE_CASE ) model.eval() SCREAMING_SNAKE_CASE_ = model(_SCREAMING_SNAKE_CASE ) self.parent.assertEqual( result.logits.shape , (self.batch_size, self.num_channels, self.image_size, self.image_size) ) # test greyscale images SCREAMING_SNAKE_CASE_ = 1 SCREAMING_SNAKE_CASE_ = SwinvaForMaskedImageModeling(_SCREAMING_SNAKE_CASE ) model.to(_SCREAMING_SNAKE_CASE ) model.eval() SCREAMING_SNAKE_CASE_ = floats_tensor([self.batch_size, 1, self.image_size, self.image_size] ) SCREAMING_SNAKE_CASE_ = model(_SCREAMING_SNAKE_CASE ) self.parent.assertEqual(result.logits.shape , (self.batch_size, 1, self.image_size, self.image_size) ) def __A ( self : List[str] , __magic_name__ : Optional[Any] , __magic_name__ : Tuple , __magic_name__ : str ) -> Optional[int]: SCREAMING_SNAKE_CASE_ = self.type_sequence_label_size SCREAMING_SNAKE_CASE_ = SwinvaForImageClassification(_SCREAMING_SNAKE_CASE ) model.to(_SCREAMING_SNAKE_CASE ) model.eval() SCREAMING_SNAKE_CASE_ = model(_SCREAMING_SNAKE_CASE , labels=_SCREAMING_SNAKE_CASE ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.type_sequence_label_size) ) def __A ( self : List[str] ) -> int: SCREAMING_SNAKE_CASE_ = self.prepare_config_and_inputs() SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = config_and_inputs SCREAMING_SNAKE_CASE_ = {"pixel_values": pixel_values} return config, inputs_dict @require_torch class lowerCamelCase (lowerCAmelCase__ , lowerCAmelCase__ , unittest.TestCase ): """simple docstring""" lowerCamelCase__ = ( (SwinvaModel, SwinvaForImageClassification, SwinvaForMaskedImageModeling) if is_torch_available() else () ) lowerCamelCase__ = ( {"feature-extraction": SwinvaModel, "image-classification": SwinvaForImageClassification} if is_torch_available() else {} ) lowerCamelCase__ = False lowerCamelCase__ = False lowerCamelCase__ = False lowerCamelCase__ = False def __A ( self : str ) -> int: SCREAMING_SNAKE_CASE_ = SwinvaModelTester(self ) SCREAMING_SNAKE_CASE_ = ConfigTester(self , config_class=_SCREAMING_SNAKE_CASE , embed_dim=37 ) def __A ( self : Union[str, Any] ) -> Tuple: self.config_tester.create_and_test_config_to_json_string() self.config_tester.create_and_test_config_to_json_file() self.config_tester.create_and_test_config_from_and_save_pretrained() self.config_tester.create_and_test_config_with_num_labels() self.config_tester.check_config_can_be_init_without_params() self.config_tester.check_config_arguments_init() def __A ( self : int ) -> List[str]: SCREAMING_SNAKE_CASE_ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*_SCREAMING_SNAKE_CASE ) @unittest.skip(reason="Got `CUDA error: misaligned address` with PyTorch 2.0.0." ) def __A ( self : List[Any] ) -> int: pass @unittest.skip(reason="Swinv2 does not use inputs_embeds" ) def __A ( self : Any ) -> List[Any]: pass def __A ( self : Any ) -> Any: 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(_SCREAMING_SNAKE_CASE ) self.assertIsInstance(model.get_input_embeddings() , (nn.Module) ) SCREAMING_SNAKE_CASE_ = model.get_output_embeddings() self.assertTrue(x is None or isinstance(_SCREAMING_SNAKE_CASE , nn.Linear ) ) def __A ( self : int ) -> Dict: 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(_SCREAMING_SNAKE_CASE ) SCREAMING_SNAKE_CASE_ = inspect.signature(model.forward ) # signature.parameters is an OrderedDict => so arg_names order is deterministic SCREAMING_SNAKE_CASE_ = [*signature.parameters.keys()] SCREAMING_SNAKE_CASE_ = ["pixel_values"] self.assertListEqual(arg_names[:1] , _SCREAMING_SNAKE_CASE ) def __A ( self : str ) -> Dict: SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = self.model_tester.prepare_config_and_inputs_for_common() SCREAMING_SNAKE_CASE_ = True for model_class in self.all_model_classes: SCREAMING_SNAKE_CASE_ = True SCREAMING_SNAKE_CASE_ = False SCREAMING_SNAKE_CASE_ = True SCREAMING_SNAKE_CASE_ = model_class(_SCREAMING_SNAKE_CASE ) model.to(_SCREAMING_SNAKE_CASE ) model.eval() with torch.no_grad(): SCREAMING_SNAKE_CASE_ = model(**self._prepare_for_class(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) ) SCREAMING_SNAKE_CASE_ = outputs.attentions SCREAMING_SNAKE_CASE_ = len(self.model_tester.depths ) self.assertEqual(len(_SCREAMING_SNAKE_CASE ) , _SCREAMING_SNAKE_CASE ) # check that output_attentions also work using config del inputs_dict["output_attentions"] SCREAMING_SNAKE_CASE_ = True SCREAMING_SNAKE_CASE_ = config.window_size**2 SCREAMING_SNAKE_CASE_ = model_class(_SCREAMING_SNAKE_CASE ) model.to(_SCREAMING_SNAKE_CASE ) model.eval() with torch.no_grad(): SCREAMING_SNAKE_CASE_ = model(**self._prepare_for_class(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) ) SCREAMING_SNAKE_CASE_ = outputs.attentions self.assertEqual(len(_SCREAMING_SNAKE_CASE ) , _SCREAMING_SNAKE_CASE ) self.assertListEqual( list(attentions[0].shape[-3:] ) , [self.model_tester.num_heads[0], window_size_squared, window_size_squared] , ) SCREAMING_SNAKE_CASE_ = len(_SCREAMING_SNAKE_CASE ) # Check attention is always last and order is fine SCREAMING_SNAKE_CASE_ = True SCREAMING_SNAKE_CASE_ = True SCREAMING_SNAKE_CASE_ = model_class(_SCREAMING_SNAKE_CASE ) model.to(_SCREAMING_SNAKE_CASE ) model.eval() with torch.no_grad(): SCREAMING_SNAKE_CASE_ = model(**self._prepare_for_class(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) ) if hasattr(self.model_tester , "num_hidden_states_types" ): SCREAMING_SNAKE_CASE_ = self.model_tester.num_hidden_states_types else: # also another +1 for reshaped_hidden_states SCREAMING_SNAKE_CASE_ = 2 self.assertEqual(out_len + added_hidden_states , len(_SCREAMING_SNAKE_CASE ) ) SCREAMING_SNAKE_CASE_ = outputs.attentions self.assertEqual(len(_SCREAMING_SNAKE_CASE ) , _SCREAMING_SNAKE_CASE ) self.assertListEqual( list(self_attentions[0].shape[-3:] ) , [self.model_tester.num_heads[0], window_size_squared, window_size_squared] , ) def __A ( self : List[str] , __magic_name__ : int , __magic_name__ : int , __magic_name__ : Any , __magic_name__ : List[Any] ) -> Tuple: SCREAMING_SNAKE_CASE_ = model_class(_SCREAMING_SNAKE_CASE ) model.to(_SCREAMING_SNAKE_CASE ) model.eval() with torch.no_grad(): SCREAMING_SNAKE_CASE_ = model(**self._prepare_for_class(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) ) SCREAMING_SNAKE_CASE_ = outputs.hidden_states SCREAMING_SNAKE_CASE_ = getattr( self.model_tester , "expected_num_hidden_layers" , len(self.model_tester.depths ) + 1 ) self.assertEqual(len(_SCREAMING_SNAKE_CASE ) , _SCREAMING_SNAKE_CASE ) # Swinv2 has a different seq_length SCREAMING_SNAKE_CASE_ = ( config.patch_size if isinstance(config.patch_size , collections.abc.Iterable ) else (config.patch_size, config.patch_size) ) SCREAMING_SNAKE_CASE_ = (image_size[1] // patch_size[1]) * (image_size[0] // patch_size[0]) self.assertListEqual( list(hidden_states[0].shape[-2:] ) , [num_patches, self.model_tester.embed_dim] , ) SCREAMING_SNAKE_CASE_ = outputs.reshaped_hidden_states self.assertEqual(len(_SCREAMING_SNAKE_CASE ) , _SCREAMING_SNAKE_CASE ) SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = reshaped_hidden_states[0].shape SCREAMING_SNAKE_CASE_ = ( reshaped_hidden_states[0].view(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , height * width ).permute(0 , 2 , 1 ) ) self.assertListEqual( list(reshaped_hidden_states.shape[-2:] ) , [num_patches, self.model_tester.embed_dim] , ) def __A ( self : Tuple ) -> Any: SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = self.model_tester.prepare_config_and_inputs_for_common() SCREAMING_SNAKE_CASE_ = ( self.model_tester.image_size if isinstance(self.model_tester.image_size , collections.abc.Iterable ) else (self.model_tester.image_size, self.model_tester.image_size) ) for model_class in self.all_model_classes: SCREAMING_SNAKE_CASE_ = True self.check_hidden_states_output(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) # check that output_hidden_states also work using config del inputs_dict["output_hidden_states"] SCREAMING_SNAKE_CASE_ = True self.check_hidden_states_output(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) def __A ( self : List[str] ) -> str: SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = self.model_tester.prepare_config_and_inputs_for_common() SCREAMING_SNAKE_CASE_ = 3 SCREAMING_SNAKE_CASE_ = ( self.model_tester.image_size if isinstance(self.model_tester.image_size , collections.abc.Iterable ) else (self.model_tester.image_size, self.model_tester.image_size) ) SCREAMING_SNAKE_CASE_ = ( config.patch_size if isinstance(config.patch_size , collections.abc.Iterable ) else (config.patch_size, config.patch_size) ) SCREAMING_SNAKE_CASE_ = image_size[0] + patch_size[0] - (image_size[0] % patch_size[0]) SCREAMING_SNAKE_CASE_ = image_size[1] + patch_size[1] - (image_size[1] % patch_size[1]) for model_class in self.all_model_classes: SCREAMING_SNAKE_CASE_ = True self.check_hidden_states_output(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , (padded_height, padded_width) ) # check that output_hidden_states also work using config del inputs_dict["output_hidden_states"] SCREAMING_SNAKE_CASE_ = True self.check_hidden_states_output(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , (padded_height, padded_width) ) def __A ( self : List[str] ) -> List[str]: SCREAMING_SNAKE_CASE_ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_masked_image_modeling(*_SCREAMING_SNAKE_CASE ) def __A ( self : Tuple ) -> Optional[int]: SCREAMING_SNAKE_CASE_ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_image_classification(*_SCREAMING_SNAKE_CASE ) @slow def __A ( self : Optional[int] ) -> Tuple: for model_name in SWINV2_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: SCREAMING_SNAKE_CASE_ = SwinvaModel.from_pretrained(_SCREAMING_SNAKE_CASE ) self.assertIsNotNone(_SCREAMING_SNAKE_CASE ) def __A ( self : Optional[int] ) -> Optional[int]: SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = self.model_tester.prepare_config_and_inputs_for_common() SCREAMING_SNAKE_CASE_ = _config_zero_init(_SCREAMING_SNAKE_CASE ) for model_class in self.all_model_classes: SCREAMING_SNAKE_CASE_ = model_class(config=_SCREAMING_SNAKE_CASE ) for name, param in model.named_parameters(): if "embeddings" not in name and "logit_scale" not in name and param.requires_grad: self.assertIn( ((param.data.mean() * 1e9).round() / 1e9).item() , [0.0, 1.0] , msg=F'''Parameter {name} of model {model_class} seems not properly initialized''' , ) @require_vision @require_torch class lowerCamelCase (unittest.TestCase ): """simple docstring""" @cached_property def __A ( self : List[Any] ) -> Dict: return ( AutoImageProcessor.from_pretrained("microsoft/swinv2-tiny-patch4-window8-256" ) if is_vision_available() else None ) @slow def __A ( self : List[str] ) -> List[str]: SCREAMING_SNAKE_CASE_ = SwinvaForImageClassification.from_pretrained("microsoft/swinv2-tiny-patch4-window8-256" ).to( _SCREAMING_SNAKE_CASE ) SCREAMING_SNAKE_CASE_ = self.default_image_processor SCREAMING_SNAKE_CASE_ = Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png" ) SCREAMING_SNAKE_CASE_ = image_processor(images=_SCREAMING_SNAKE_CASE , return_tensors="pt" ).to(_SCREAMING_SNAKE_CASE ) # forward pass with torch.no_grad(): SCREAMING_SNAKE_CASE_ = model(**_SCREAMING_SNAKE_CASE ) # verify the logits SCREAMING_SNAKE_CASE_ = torch.Size((1, 1_000) ) self.assertEqual(outputs.logits.shape , _SCREAMING_SNAKE_CASE ) SCREAMING_SNAKE_CASE_ = torch.tensor([-0.3947, -0.4306, 0.0026] ).to(_SCREAMING_SNAKE_CASE ) self.assertTrue(torch.allclose(outputs.logits[0, :3] , _SCREAMING_SNAKE_CASE , atol=1e-4 ) )
355
from collections import deque class lowerCamelCase : """simple docstring""" def __init__( self : str , __magic_name__ : str , __magic_name__ : int , __magic_name__ : int ) -> None: SCREAMING_SNAKE_CASE_ = process_name # process name SCREAMING_SNAKE_CASE_ = arrival_time # arrival time of the process # completion time of finished process or last interrupted time SCREAMING_SNAKE_CASE_ = arrival_time SCREAMING_SNAKE_CASE_ = burst_time # remaining burst time SCREAMING_SNAKE_CASE_ = 0 # total time of the process wait in ready queue SCREAMING_SNAKE_CASE_ = 0 # time from arrival time to completion time class lowerCamelCase : """simple docstring""" def __init__( self : Tuple , __magic_name__ : int , __magic_name__ : list[int] , __magic_name__ : deque[Process] , __magic_name__ : int , ) -> None: # total number of mlfq's queues SCREAMING_SNAKE_CASE_ = number_of_queues # time slice of queues that round robin algorithm applied SCREAMING_SNAKE_CASE_ = time_slices # unfinished process is in this ready_queue SCREAMING_SNAKE_CASE_ = queue # current time SCREAMING_SNAKE_CASE_ = current_time # finished process is in this sequence queue SCREAMING_SNAKE_CASE_ = deque() def __A ( self : Dict ) -> list[str]: SCREAMING_SNAKE_CASE_ = [] for i in range(len(self.finish_queue ) ): sequence.append(self.finish_queue[i].process_name ) return sequence def __A ( self : List[str] , __magic_name__ : list[Process] ) -> list[int]: SCREAMING_SNAKE_CASE_ = [] for i in range(len(__magic_name__ ) ): waiting_times.append(queue[i].waiting_time ) return waiting_times def __A ( self : List[str] , __magic_name__ : list[Process] ) -> list[int]: SCREAMING_SNAKE_CASE_ = [] for i in range(len(__magic_name__ ) ): turnaround_times.append(queue[i].turnaround_time ) return turnaround_times def __A ( self : Tuple , __magic_name__ : list[Process] ) -> list[int]: SCREAMING_SNAKE_CASE_ = [] for i in range(len(__magic_name__ ) ): completion_times.append(queue[i].stop_time ) return completion_times def __A ( self : str , __magic_name__ : deque[Process] ) -> list[int]: return [q.burst_time for q in queue] def __A ( self : Optional[Any] , __magic_name__ : Process ) -> int: process.waiting_time += self.current_time - process.stop_time return process.waiting_time def __A ( self : Optional[Any] , __magic_name__ : deque[Process] ) -> deque[Process]: SCREAMING_SNAKE_CASE_ = deque() # sequence deque of finished process while len(__magic_name__ ) != 0: SCREAMING_SNAKE_CASE_ = ready_queue.popleft() # current process # if process's arrival time is later than current time, update current time if self.current_time < cp.arrival_time: self.current_time += cp.arrival_time # update waiting time of current process self.update_waiting_time(__magic_name__ ) # update current time self.current_time += cp.burst_time # finish the process and set the process's burst-time 0 SCREAMING_SNAKE_CASE_ = 0 # set the process's turnaround time because it is finished SCREAMING_SNAKE_CASE_ = self.current_time - cp.arrival_time # set the completion time SCREAMING_SNAKE_CASE_ = self.current_time # add the process to queue that has finished queue finished.append(__magic_name__ ) self.finish_queue.extend(__magic_name__ ) # add finished process to finish queue # FCFS will finish all remaining processes return finished def __A ( self : Any , __magic_name__ : deque[Process] , __magic_name__ : int ) -> tuple[deque[Process], deque[Process]]: SCREAMING_SNAKE_CASE_ = deque() # sequence deque of terminated process # just for 1 cycle and unfinished processes will go back to queue for _ in range(len(__magic_name__ ) ): SCREAMING_SNAKE_CASE_ = ready_queue.popleft() # current process # if process's arrival time is later than current time, update current time if self.current_time < cp.arrival_time: self.current_time += cp.arrival_time # update waiting time of unfinished processes self.update_waiting_time(__magic_name__ ) # if the burst time of process is bigger than time-slice if cp.burst_time > time_slice: # use CPU for only time-slice self.current_time += time_slice # update remaining burst time cp.burst_time -= time_slice # update end point time SCREAMING_SNAKE_CASE_ = self.current_time # locate the process behind the queue because it is not finished ready_queue.append(__magic_name__ ) else: # use CPU for remaining burst time self.current_time += cp.burst_time # set burst time 0 because the process is finished SCREAMING_SNAKE_CASE_ = 0 # set the finish time SCREAMING_SNAKE_CASE_ = self.current_time # update the process' turnaround time because it is finished SCREAMING_SNAKE_CASE_ = self.current_time - cp.arrival_time # add the process to queue that has finished queue finished.append(__magic_name__ ) self.finish_queue.extend(__magic_name__ ) # add finished process to finish queue # return finished processes queue and remaining processes queue return finished, ready_queue def __A ( self : Any ) -> deque[Process]: # all queues except last one have round_robin algorithm for i in range(self.number_of_queues - 1 ): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = self.round_robin( self.ready_queue , self.time_slices[i] ) # the last queue has first_come_first_served algorithm self.first_come_first_served(self.ready_queue ) return self.finish_queue if __name__ == "__main__": import doctest A : Dict = Process("P1", 0, 53) A : str = Process("P2", 0, 17) A : List[Any] = Process("P3", 0, 68) A : List[str] = Process("P4", 0, 24) A : Dict = 3 A : Any = [17, 25] A : Dict = deque([Pa, Pa, Pa, Pa]) if len(time_slices) != number_of_queues - 1: raise SystemExit(0) doctest.testmod(extraglobs={"queue": deque([Pa, Pa, Pa, Pa])}) A : Union[str, Any] = Process("P1", 0, 53) A : Any = Process("P2", 0, 17) A : Dict = Process("P3", 0, 68) A : List[str] = Process("P4", 0, 24) A : Optional[int] = 3 A : int = [17, 25] A : Union[str, Any] = deque([Pa, Pa, Pa, Pa]) A : Tuple = MLFQ(number_of_queues, time_slices, queue, 0) A : Tuple = mlfq.multi_level_feedback_queue() # print total waiting times of processes(P1, P2, P3, P4) print( f"waiting time:\ \t\t\t{MLFQ.calculate_waiting_time(mlfq, [Pa, Pa, Pa, Pa])}" ) # print completion times of processes(P1, P2, P3, P4) print( f"completion time:\ \t\t{MLFQ.calculate_completion_time(mlfq, [Pa, Pa, Pa, Pa])}" ) # print total turnaround times of processes(P1, P2, P3, P4) print( f"turnaround time:\ \t\t{MLFQ.calculate_turnaround_time(mlfq, [Pa, Pa, Pa, Pa])}" ) # print sequence of finished processes print( f"sequence of finished processes:\ {mlfq.calculate_sequence_of_finish_queue()}" )
305
0
"""simple docstring""" from ..utils import DummyObject, requires_backends class lowerCamelCase (metaclass=snake_case_ ): """simple docstring""" lowerCamelCase__ = ['''torch'''] def __init__( self : Dict , *__magic_name__ : Union[str, Any] , **__magic_name__ : Optional[Any] ) -> Optional[int]: requires_backends(self , ["torch"] ) @classmethod def __A ( cls : Any , *__magic_name__ : Optional[Any] , **__magic_name__ : Optional[int] ) -> Dict: requires_backends(cls , ["torch"] ) @classmethod def __A ( cls : Union[str, Any] , *__magic_name__ : Optional[int] , **__magic_name__ : List[str] ) -> Optional[int]: requires_backends(cls , ["torch"] ) class lowerCamelCase (metaclass=snake_case_ ): """simple docstring""" lowerCamelCase__ = ['''torch'''] def __init__( self : List[str] , *__magic_name__ : Dict , **__magic_name__ : int ) -> Tuple: requires_backends(self , ["torch"] ) @classmethod def __A ( cls : Tuple , *__magic_name__ : Optional[Any] , **__magic_name__ : int ) -> str: requires_backends(cls , ["torch"] ) @classmethod def __A ( cls : List[str] , *__magic_name__ : int , **__magic_name__ : Any ) -> List[str]: requires_backends(cls , ["torch"] ) class lowerCamelCase (metaclass=snake_case_ ): """simple docstring""" lowerCamelCase__ = ['''torch'''] def __init__( self : Optional[int] , *__magic_name__ : Optional[Any] , **__magic_name__ : Union[str, Any] ) -> Union[str, Any]: requires_backends(self , ["torch"] ) @classmethod def __A ( cls : Tuple , *__magic_name__ : Tuple , **__magic_name__ : List[str] ) -> Tuple: requires_backends(cls , ["torch"] ) @classmethod def __A ( cls : List[Any] , *__magic_name__ : Dict , **__magic_name__ : List[str] ) -> List[str]: requires_backends(cls , ["torch"] ) class lowerCamelCase (metaclass=snake_case_ ): """simple docstring""" lowerCamelCase__ = ['''torch'''] def __init__( self : str , *__magic_name__ : Dict , **__magic_name__ : Optional[Any] ) -> Optional[int]: requires_backends(self , ["torch"] ) @classmethod def __A ( cls : Tuple , *__magic_name__ : Union[str, Any] , **__magic_name__ : List[str] ) -> List[Any]: requires_backends(cls , ["torch"] ) @classmethod def __A ( cls : str , *__magic_name__ : Optional[Any] , **__magic_name__ : str ) -> int: requires_backends(cls , ["torch"] ) class lowerCamelCase (metaclass=snake_case_ ): """simple docstring""" lowerCamelCase__ = ['''torch'''] def __init__( self : List[str] , *__magic_name__ : Optional[Any] , **__magic_name__ : List[Any] ) -> Union[str, Any]: requires_backends(self , ["torch"] ) @classmethod def __A ( cls : Dict , *__magic_name__ : Tuple , **__magic_name__ : Optional[Any] ) -> Tuple: requires_backends(cls , ["torch"] ) @classmethod def __A ( cls : Tuple , *__magic_name__ : Optional[int] , **__magic_name__ : Dict ) -> Tuple: requires_backends(cls , ["torch"] ) class lowerCamelCase (metaclass=snake_case_ ): """simple docstring""" lowerCamelCase__ = ['''torch'''] def __init__( self : str , *__magic_name__ : Union[str, Any] , **__magic_name__ : Tuple ) -> Optional[int]: requires_backends(self , ["torch"] ) @classmethod def __A ( cls : List[str] , *__magic_name__ : Optional[int] , **__magic_name__ : Optional[Any] ) -> List[str]: requires_backends(cls , ["torch"] ) @classmethod def __A ( cls : Union[str, Any] , *__magic_name__ : int , **__magic_name__ : Optional[Any] ) -> int: requires_backends(cls , ["torch"] ) class lowerCamelCase (metaclass=snake_case_ ): """simple docstring""" lowerCamelCase__ = ['''torch'''] def __init__( self : int , *__magic_name__ : List[str] , **__magic_name__ : List[str] ) -> Dict: requires_backends(self , ["torch"] ) @classmethod def __A ( cls : List[str] , *__magic_name__ : List[Any] , **__magic_name__ : str ) -> Any: requires_backends(cls , ["torch"] ) @classmethod def __A ( cls : List[Any] , *__magic_name__ : List[Any] , **__magic_name__ : Any ) -> List[Any]: requires_backends(cls , ["torch"] ) class lowerCamelCase (metaclass=snake_case_ ): """simple docstring""" lowerCamelCase__ = ['''torch'''] def __init__( self : str , *__magic_name__ : List[Any] , **__magic_name__ : Union[str, Any] ) -> Tuple: requires_backends(self , ["torch"] ) @classmethod def __A ( cls : str , *__magic_name__ : Optional[int] , **__magic_name__ : Optional[Any] ) -> str: requires_backends(cls , ["torch"] ) @classmethod def __A ( cls : Union[str, Any] , *__magic_name__ : Dict , **__magic_name__ : Any ) -> List[str]: requires_backends(cls , ["torch"] ) class lowerCamelCase (metaclass=snake_case_ ): """simple docstring""" lowerCamelCase__ = ['''torch'''] def __init__( self : Optional[Any] , *__magic_name__ : List[Any] , **__magic_name__ : List[Any] ) -> str: requires_backends(self , ["torch"] ) @classmethod def __A ( cls : Optional[Any] , *__magic_name__ : List[Any] , **__magic_name__ : int ) -> Tuple: requires_backends(cls , ["torch"] ) @classmethod def __A ( cls : Optional[int] , *__magic_name__ : int , **__magic_name__ : Dict ) -> Tuple: requires_backends(cls , ["torch"] ) class lowerCamelCase (metaclass=snake_case_ ): """simple docstring""" lowerCamelCase__ = ['''torch'''] def __init__( self : Optional[int] , *__magic_name__ : List[Any] , **__magic_name__ : Tuple ) -> Optional[Any]: requires_backends(self , ["torch"] ) @classmethod def __A ( cls : Tuple , *__magic_name__ : int , **__magic_name__ : Optional[int] ) -> Union[str, Any]: requires_backends(cls , ["torch"] ) @classmethod def __A ( cls : List[Any] , *__magic_name__ : List[Any] , **__magic_name__ : Optional[int] ) -> Optional[Any]: requires_backends(cls , ["torch"] ) class lowerCamelCase (metaclass=snake_case_ ): """simple docstring""" lowerCamelCase__ = ['''torch'''] def __init__( self : List[Any] , *__magic_name__ : Optional[Any] , **__magic_name__ : Optional[Any] ) -> str: requires_backends(self , ["torch"] ) @classmethod def __A ( cls : Union[str, Any] , *__magic_name__ : Union[str, Any] , **__magic_name__ : Tuple ) -> str: requires_backends(cls , ["torch"] ) @classmethod def __A ( cls : str , *__magic_name__ : List[str] , **__magic_name__ : Union[str, Any] ) -> List[str]: requires_backends(cls , ["torch"] ) def a__ ( *__UpperCamelCase , **__UpperCamelCase ): requires_backends(__UpperCamelCase , ["torch"] ) def a__ ( *__UpperCamelCase , **__UpperCamelCase ): requires_backends(__UpperCamelCase , ["torch"] ) def a__ ( *__UpperCamelCase , **__UpperCamelCase ): requires_backends(__UpperCamelCase , ["torch"] ) def a__ ( *__UpperCamelCase , **__UpperCamelCase ): requires_backends(__UpperCamelCase , ["torch"] ) def a__ ( *__UpperCamelCase , **__UpperCamelCase ): requires_backends(__UpperCamelCase , ["torch"] ) def a__ ( *__UpperCamelCase , **__UpperCamelCase ): requires_backends(__UpperCamelCase , ["torch"] ) def a__ ( *__UpperCamelCase , **__UpperCamelCase ): requires_backends(__UpperCamelCase , ["torch"] ) class lowerCamelCase (metaclass=snake_case_ ): """simple docstring""" lowerCamelCase__ = ['''torch'''] def __init__( self : List[str] , *__magic_name__ : Union[str, Any] , **__magic_name__ : str ) -> List[Any]: requires_backends(self , ["torch"] ) @classmethod def __A ( cls : List[Any] , *__magic_name__ : List[str] , **__magic_name__ : Tuple ) -> Union[str, Any]: requires_backends(cls , ["torch"] ) @classmethod def __A ( cls : Union[str, Any] , *__magic_name__ : List[str] , **__magic_name__ : int ) -> Optional[Any]: requires_backends(cls , ["torch"] ) class lowerCamelCase (metaclass=snake_case_ ): """simple docstring""" lowerCamelCase__ = ['''torch'''] def __init__( self : Dict , *__magic_name__ : Dict , **__magic_name__ : str ) -> Optional[Any]: requires_backends(self , ["torch"] ) @classmethod def __A ( cls : List[str] , *__magic_name__ : str , **__magic_name__ : int ) -> Union[str, Any]: requires_backends(cls , ["torch"] ) @classmethod def __A ( cls : List[Any] , *__magic_name__ : Tuple , **__magic_name__ : str ) -> Optional[Any]: requires_backends(cls , ["torch"] ) class lowerCamelCase (metaclass=snake_case_ ): """simple docstring""" lowerCamelCase__ = ['''torch'''] def __init__( self : Optional[int] , *__magic_name__ : Union[str, Any] , **__magic_name__ : Optional[Any] ) -> int: requires_backends(self , ["torch"] ) @classmethod def __A ( cls : Optional[int] , *__magic_name__ : str , **__magic_name__ : int ) -> Tuple: requires_backends(cls , ["torch"] ) @classmethod def __A ( cls : List[str] , *__magic_name__ : int , **__magic_name__ : Union[str, Any] ) -> Union[str, Any]: requires_backends(cls , ["torch"] ) class lowerCamelCase (metaclass=snake_case_ ): """simple docstring""" lowerCamelCase__ = ['''torch'''] def __init__( self : Dict , *__magic_name__ : Optional[Any] , **__magic_name__ : int ) -> Optional[Any]: requires_backends(self , ["torch"] ) @classmethod def __A ( cls : Tuple , *__magic_name__ : Optional[int] , **__magic_name__ : Union[str, Any] ) -> List[Any]: requires_backends(cls , ["torch"] ) @classmethod def __A ( cls : Optional[Any] , *__magic_name__ : Union[str, Any] , **__magic_name__ : Optional[Any] ) -> Tuple: requires_backends(cls , ["torch"] ) class lowerCamelCase (metaclass=snake_case_ ): """simple docstring""" lowerCamelCase__ = ['''torch'''] def __init__( self : Union[str, Any] , *__magic_name__ : Tuple , **__magic_name__ : str ) -> str: requires_backends(self , ["torch"] ) @classmethod def __A ( cls : Optional[int] , *__magic_name__ : List[str] , **__magic_name__ : List[Any] ) -> str: requires_backends(cls , ["torch"] ) @classmethod def __A ( cls : Tuple , *__magic_name__ : Tuple , **__magic_name__ : Dict ) -> List[Any]: requires_backends(cls , ["torch"] ) class lowerCamelCase (metaclass=snake_case_ ): """simple docstring""" lowerCamelCase__ = ['''torch'''] def __init__( self : List[Any] , *__magic_name__ : str , **__magic_name__ : Union[str, Any] ) -> List[str]: requires_backends(self , ["torch"] ) @classmethod def __A ( cls : Union[str, Any] , *__magic_name__ : List[Any] , **__magic_name__ : Optional[int] ) -> Any: requires_backends(cls , ["torch"] ) @classmethod def __A ( cls : Union[str, Any] , *__magic_name__ : List[Any] , **__magic_name__ : Any ) -> Tuple: requires_backends(cls , ["torch"] ) class lowerCamelCase (metaclass=snake_case_ ): """simple docstring""" lowerCamelCase__ = ['''torch'''] def __init__( self : Any , *__magic_name__ : str , **__magic_name__ : str ) -> List[Any]: requires_backends(self , ["torch"] ) @classmethod def __A ( cls : str , *__magic_name__ : List[str] , **__magic_name__ : Any ) -> Any: requires_backends(cls , ["torch"] ) @classmethod def __A ( cls : List[str] , *__magic_name__ : Optional[Any] , **__magic_name__ : Union[str, Any] ) -> List[str]: requires_backends(cls , ["torch"] ) class lowerCamelCase (metaclass=snake_case_ ): """simple docstring""" lowerCamelCase__ = ['''torch'''] def __init__( self : Optional[Any] , *__magic_name__ : str , **__magic_name__ : Optional[int] ) -> List[Any]: requires_backends(self , ["torch"] ) @classmethod def __A ( cls : int , *__magic_name__ : Dict , **__magic_name__ : Any ) -> Any: requires_backends(cls , ["torch"] ) @classmethod def __A ( cls : Union[str, Any] , *__magic_name__ : Optional[Any] , **__magic_name__ : str ) -> Dict: requires_backends(cls , ["torch"] ) class lowerCamelCase (metaclass=snake_case_ ): """simple docstring""" lowerCamelCase__ = ['''torch'''] def __init__( self : str , *__magic_name__ : Dict , **__magic_name__ : Optional[int] ) -> Dict: requires_backends(self , ["torch"] ) @classmethod def __A ( cls : Dict , *__magic_name__ : Tuple , **__magic_name__ : Union[str, Any] ) -> Union[str, Any]: requires_backends(cls , ["torch"] ) @classmethod def __A ( cls : Any , *__magic_name__ : List[str] , **__magic_name__ : List[Any] ) -> Optional[int]: requires_backends(cls , ["torch"] ) class lowerCamelCase (metaclass=snake_case_ ): """simple docstring""" lowerCamelCase__ = ['''torch'''] def __init__( self : Dict , *__magic_name__ : str , **__magic_name__ : Any ) -> Any: requires_backends(self , ["torch"] ) @classmethod def __A ( cls : Tuple , *__magic_name__ : Any , **__magic_name__ : Tuple ) -> Tuple: requires_backends(cls , ["torch"] ) @classmethod def __A ( cls : List[str] , *__magic_name__ : int , **__magic_name__ : str ) -> Optional[Any]: requires_backends(cls , ["torch"] ) class lowerCamelCase (metaclass=snake_case_ ): """simple docstring""" lowerCamelCase__ = ['''torch'''] def __init__( self : List[Any] , *__magic_name__ : List[str] , **__magic_name__ : List[Any] ) -> int: requires_backends(self , ["torch"] ) @classmethod def __A ( cls : Optional[Any] , *__magic_name__ : List[Any] , **__magic_name__ : Dict ) -> Any: requires_backends(cls , ["torch"] ) @classmethod def __A ( cls : int , *__magic_name__ : Union[str, Any] , **__magic_name__ : Union[str, Any] ) -> Tuple: requires_backends(cls , ["torch"] ) class lowerCamelCase (metaclass=snake_case_ ): """simple docstring""" lowerCamelCase__ = ['''torch'''] def __init__( self : Tuple , *__magic_name__ : List[Any] , **__magic_name__ : Tuple ) -> Tuple: requires_backends(self , ["torch"] ) @classmethod def __A ( cls : Optional[Any] , *__magic_name__ : Any , **__magic_name__ : List[Any] ) -> List[str]: requires_backends(cls , ["torch"] ) @classmethod def __A ( cls : List[Any] , *__magic_name__ : Union[str, Any] , **__magic_name__ : List[Any] ) -> int: requires_backends(cls , ["torch"] ) class lowerCamelCase (metaclass=snake_case_ ): """simple docstring""" lowerCamelCase__ = ['''torch'''] def __init__( self : Union[str, Any] , *__magic_name__ : List[str] , **__magic_name__ : int ) -> int: requires_backends(self , ["torch"] ) @classmethod def __A ( cls : List[Any] , *__magic_name__ : int , **__magic_name__ : List[Any] ) -> Union[str, Any]: requires_backends(cls , ["torch"] ) @classmethod def __A ( cls : Tuple , *__magic_name__ : Any , **__magic_name__ : Any ) -> int: requires_backends(cls , ["torch"] ) class lowerCamelCase (metaclass=snake_case_ ): """simple docstring""" lowerCamelCase__ = ['''torch'''] def __init__( self : str , *__magic_name__ : Optional[Any] , **__magic_name__ : Optional[int] ) -> Any: requires_backends(self , ["torch"] ) @classmethod def __A ( cls : Tuple , *__magic_name__ : List[str] , **__magic_name__ : Optional[int] ) -> Tuple: requires_backends(cls , ["torch"] ) @classmethod def __A ( cls : int , *__magic_name__ : Any , **__magic_name__ : Optional[int] ) -> Dict: requires_backends(cls , ["torch"] ) class lowerCamelCase (metaclass=snake_case_ ): """simple docstring""" lowerCamelCase__ = ['''torch'''] def __init__( self : int , *__magic_name__ : List[Any] , **__magic_name__ : List[Any] ) -> Dict: requires_backends(self , ["torch"] ) @classmethod def __A ( cls : List[Any] , *__magic_name__ : List[Any] , **__magic_name__ : int ) -> Tuple: requires_backends(cls , ["torch"] ) @classmethod def __A ( cls : Optional[int] , *__magic_name__ : Tuple , **__magic_name__ : int ) -> List[str]: requires_backends(cls , ["torch"] ) class lowerCamelCase (metaclass=snake_case_ ): """simple docstring""" lowerCamelCase__ = ['''torch'''] def __init__( self : Tuple , *__magic_name__ : Optional[Any] , **__magic_name__ : str ) -> List[Any]: requires_backends(self , ["torch"] ) @classmethod def __A ( cls : int , *__magic_name__ : List[str] , **__magic_name__ : Any ) -> List[str]: requires_backends(cls , ["torch"] ) @classmethod def __A ( cls : Tuple , *__magic_name__ : Union[str, Any] , **__magic_name__ : int ) -> int: requires_backends(cls , ["torch"] ) class lowerCamelCase (metaclass=snake_case_ ): """simple docstring""" lowerCamelCase__ = ['''torch'''] def __init__( self : str , *__magic_name__ : Tuple , **__magic_name__ : Dict ) -> str: requires_backends(self , ["torch"] ) @classmethod def __A ( cls : List[Any] , *__magic_name__ : Optional[int] , **__magic_name__ : List[Any] ) -> List[str]: requires_backends(cls , ["torch"] ) @classmethod def __A ( cls : Optional[int] , *__magic_name__ : List[str] , **__magic_name__ : Any ) -> Union[str, Any]: requires_backends(cls , ["torch"] ) class lowerCamelCase (metaclass=snake_case_ ): """simple docstring""" lowerCamelCase__ = ['''torch'''] def __init__( self : Optional[int] , *__magic_name__ : Optional[int] , **__magic_name__ : Optional[int] ) -> List[str]: requires_backends(self , ["torch"] ) @classmethod def __A ( cls : str , *__magic_name__ : Union[str, Any] , **__magic_name__ : List[Any] ) -> Tuple: requires_backends(cls , ["torch"] ) @classmethod def __A ( cls : Dict , *__magic_name__ : List[Any] , **__magic_name__ : List[Any] ) -> str: requires_backends(cls , ["torch"] ) class lowerCamelCase (metaclass=snake_case_ ): """simple docstring""" lowerCamelCase__ = ['''torch'''] def __init__( self : List[str] , *__magic_name__ : Any , **__magic_name__ : int ) -> Optional[Any]: requires_backends(self , ["torch"] ) @classmethod def __A ( cls : Optional[int] , *__magic_name__ : int , **__magic_name__ : str ) -> int: requires_backends(cls , ["torch"] ) @classmethod def __A ( cls : Dict , *__magic_name__ : int , **__magic_name__ : List[str] ) -> Union[str, Any]: requires_backends(cls , ["torch"] ) class lowerCamelCase (metaclass=snake_case_ ): """simple docstring""" lowerCamelCase__ = ['''torch'''] def __init__( self : Tuple , *__magic_name__ : Union[str, Any] , **__magic_name__ : str ) -> Dict: requires_backends(self , ["torch"] ) @classmethod def __A ( cls : str , *__magic_name__ : List[str] , **__magic_name__ : Tuple ) -> int: requires_backends(cls , ["torch"] ) @classmethod def __A ( cls : Dict , *__magic_name__ : Optional[int] , **__magic_name__ : Tuple ) -> Dict: requires_backends(cls , ["torch"] ) class lowerCamelCase (metaclass=snake_case_ ): """simple docstring""" lowerCamelCase__ = ['''torch'''] def __init__( self : Optional[int] , *__magic_name__ : Optional[Any] , **__magic_name__ : List[Any] ) -> Union[str, Any]: requires_backends(self , ["torch"] ) @classmethod def __A ( cls : Tuple , *__magic_name__ : str , **__magic_name__ : str ) -> Dict: requires_backends(cls , ["torch"] ) @classmethod def __A ( cls : List[str] , *__magic_name__ : Any , **__magic_name__ : Optional[int] ) -> List[str]: requires_backends(cls , ["torch"] ) class lowerCamelCase (metaclass=snake_case_ ): """simple docstring""" lowerCamelCase__ = ['''torch'''] def __init__( self : Tuple , *__magic_name__ : Optional[Any] , **__magic_name__ : Optional[Any] ) -> int: requires_backends(self , ["torch"] ) @classmethod def __A ( cls : Any , *__magic_name__ : int , **__magic_name__ : Any ) -> Dict: requires_backends(cls , ["torch"] ) @classmethod def __A ( cls : Any , *__magic_name__ : Optional[int] , **__magic_name__ : Union[str, Any] ) -> Optional[Any]: requires_backends(cls , ["torch"] ) class lowerCamelCase (metaclass=snake_case_ ): """simple docstring""" lowerCamelCase__ = ['''torch'''] def __init__( self : Union[str, Any] , *__magic_name__ : Any , **__magic_name__ : str ) -> List[Any]: requires_backends(self , ["torch"] ) @classmethod def __A ( cls : int , *__magic_name__ : List[Any] , **__magic_name__ : int ) -> Tuple: requires_backends(cls , ["torch"] ) @classmethod def __A ( cls : Union[str, Any] , *__magic_name__ : Optional[Any] , **__magic_name__ : List[str] ) -> Dict: requires_backends(cls , ["torch"] ) class lowerCamelCase (metaclass=snake_case_ ): """simple docstring""" lowerCamelCase__ = ['''torch'''] def __init__( self : int , *__magic_name__ : str , **__magic_name__ : Any ) -> Optional[int]: requires_backends(self , ["torch"] ) @classmethod def __A ( cls : Tuple , *__magic_name__ : List[str] , **__magic_name__ : Optional[Any] ) -> Tuple: requires_backends(cls , ["torch"] ) @classmethod def __A ( cls : Tuple , *__magic_name__ : List[Any] , **__magic_name__ : Union[str, Any] ) -> Any: requires_backends(cls , ["torch"] ) class lowerCamelCase (metaclass=snake_case_ ): """simple docstring""" lowerCamelCase__ = ['''torch'''] def __init__( self : List[Any] , *__magic_name__ : Union[str, Any] , **__magic_name__ : int ) -> Union[str, Any]: requires_backends(self , ["torch"] ) @classmethod def __A ( cls : Optional[Any] , *__magic_name__ : str , **__magic_name__ : Union[str, Any] ) -> int: requires_backends(cls , ["torch"] ) @classmethod def __A ( cls : str , *__magic_name__ : Tuple , **__magic_name__ : Tuple ) -> str: requires_backends(cls , ["torch"] ) class lowerCamelCase (metaclass=snake_case_ ): """simple docstring""" lowerCamelCase__ = ['''torch'''] def __init__( self : Union[str, Any] , *__magic_name__ : List[Any] , **__magic_name__ : Union[str, Any] ) -> Union[str, Any]: requires_backends(self , ["torch"] ) @classmethod def __A ( cls : Optional[int] , *__magic_name__ : int , **__magic_name__ : List[str] ) -> Tuple: requires_backends(cls , ["torch"] ) @classmethod def __A ( cls : str , *__magic_name__ : Dict , **__magic_name__ : List[str] ) -> Tuple: requires_backends(cls , ["torch"] ) class lowerCamelCase (metaclass=snake_case_ ): """simple docstring""" lowerCamelCase__ = ['''torch'''] def __init__( self : List[str] , *__magic_name__ : Any , **__magic_name__ : Optional[Any] ) -> Optional[Any]: requires_backends(self , ["torch"] ) @classmethod def __A ( cls : Optional[Any] , *__magic_name__ : List[str] , **__magic_name__ : Optional[Any] ) -> List[Any]: requires_backends(cls , ["torch"] ) @classmethod def __A ( cls : int , *__magic_name__ : Union[str, Any] , **__magic_name__ : int ) -> List[Any]: requires_backends(cls , ["torch"] ) class lowerCamelCase (metaclass=snake_case_ ): """simple docstring""" lowerCamelCase__ = ['''torch'''] def __init__( self : Tuple , *__magic_name__ : Optional[Any] , **__magic_name__ : int ) -> Any: requires_backends(self , ["torch"] ) @classmethod def __A ( cls : Optional[Any] , *__magic_name__ : int , **__magic_name__ : Union[str, Any] ) -> Dict: requires_backends(cls , ["torch"] ) @classmethod def __A ( cls : List[Any] , *__magic_name__ : str , **__magic_name__ : List[Any] ) -> Union[str, Any]: requires_backends(cls , ["torch"] ) class lowerCamelCase (metaclass=snake_case_ ): """simple docstring""" lowerCamelCase__ = ['''torch'''] def __init__( self : Tuple , *__magic_name__ : Dict , **__magic_name__ : Any ) -> List[Any]: requires_backends(self , ["torch"] ) @classmethod def __A ( cls : List[str] , *__magic_name__ : Optional[int] , **__magic_name__ : str ) -> Dict: requires_backends(cls , ["torch"] ) @classmethod def __A ( cls : int , *__magic_name__ : Optional[int] , **__magic_name__ : Any ) -> Dict: requires_backends(cls , ["torch"] ) class lowerCamelCase (metaclass=snake_case_ ): """simple docstring""" lowerCamelCase__ = ['''torch'''] def __init__( self : Union[str, Any] , *__magic_name__ : Any , **__magic_name__ : Optional[Any] ) -> List[Any]: requires_backends(self , ["torch"] ) @classmethod def __A ( cls : Dict , *__magic_name__ : Optional[int] , **__magic_name__ : List[str] ) -> List[Any]: requires_backends(cls , ["torch"] ) @classmethod def __A ( cls : int , *__magic_name__ : Any , **__magic_name__ : Any ) -> Any: requires_backends(cls , ["torch"] ) class lowerCamelCase (metaclass=snake_case_ ): """simple docstring""" lowerCamelCase__ = ['''torch'''] def __init__( self : Any , *__magic_name__ : Dict , **__magic_name__ : Optional[int] ) -> Union[str, Any]: requires_backends(self , ["torch"] ) @classmethod def __A ( cls : List[Any] , *__magic_name__ : List[str] , **__magic_name__ : int ) -> Optional[int]: requires_backends(cls , ["torch"] ) @classmethod def __A ( cls : str , *__magic_name__ : Dict , **__magic_name__ : Union[str, Any] ) -> Optional[int]: requires_backends(cls , ["torch"] ) class lowerCamelCase (metaclass=snake_case_ ): """simple docstring""" lowerCamelCase__ = ['''torch'''] def __init__( self : str , *__magic_name__ : Optional[Any] , **__magic_name__ : Optional[int] ) -> Optional[int]: requires_backends(self , ["torch"] ) @classmethod def __A ( cls : str , *__magic_name__ : Optional[int] , **__magic_name__ : Optional[Any] ) -> Optional[int]: requires_backends(cls , ["torch"] ) @classmethod def __A ( cls : Tuple , *__magic_name__ : List[Any] , **__magic_name__ : Optional[int] ) -> Dict: requires_backends(cls , ["torch"] ) class lowerCamelCase (metaclass=snake_case_ ): """simple docstring""" lowerCamelCase__ = ['''torch'''] def __init__( self : Optional[Any] , *__magic_name__ : List[str] , **__magic_name__ : Dict ) -> Tuple: requires_backends(self , ["torch"] ) @classmethod def __A ( cls : Any , *__magic_name__ : Any , **__magic_name__ : List[Any] ) -> Tuple: requires_backends(cls , ["torch"] ) @classmethod def __A ( cls : str , *__magic_name__ : Optional[Any] , **__magic_name__ : Union[str, Any] ) -> Optional[int]: requires_backends(cls , ["torch"] ) class lowerCamelCase (metaclass=snake_case_ ): """simple docstring""" lowerCamelCase__ = ['''torch'''] def __init__( self : str , *__magic_name__ : int , **__magic_name__ : Optional[int] ) -> Optional[Any]: requires_backends(self , ["torch"] ) @classmethod def __A ( cls : List[str] , *__magic_name__ : List[Any] , **__magic_name__ : Any ) -> Optional[int]: requires_backends(cls , ["torch"] ) @classmethod def __A ( cls : str , *__magic_name__ : str , **__magic_name__ : Optional[int] ) -> Tuple: requires_backends(cls , ["torch"] ) class lowerCamelCase (metaclass=snake_case_ ): """simple docstring""" lowerCamelCase__ = ['''torch'''] def __init__( self : Dict , *__magic_name__ : List[Any] , **__magic_name__ : int ) -> str: requires_backends(self , ["torch"] ) @classmethod def __A ( cls : int , *__magic_name__ : Optional[int] , **__magic_name__ : Any ) -> Union[str, Any]: requires_backends(cls , ["torch"] ) @classmethod def __A ( cls : Any , *__magic_name__ : Optional[int] , **__magic_name__ : Tuple ) -> List[str]: requires_backends(cls , ["torch"] ) class lowerCamelCase (metaclass=snake_case_ ): """simple docstring""" lowerCamelCase__ = ['''torch'''] def __init__( self : Any , *__magic_name__ : List[str] , **__magic_name__ : List[str] ) -> Tuple: requires_backends(self , ["torch"] ) @classmethod def __A ( cls : Dict , *__magic_name__ : Any , **__magic_name__ : Optional[Any] ) -> Tuple: requires_backends(cls , ["torch"] ) @classmethod def __A ( cls : Optional[int] , *__magic_name__ : Optional[Any] , **__magic_name__ : int ) -> int: requires_backends(cls , ["torch"] ) class lowerCamelCase (metaclass=snake_case_ ): """simple docstring""" lowerCamelCase__ = ['''torch'''] def __init__( self : Optional[Any] , *__magic_name__ : int , **__magic_name__ : str ) -> List[Any]: requires_backends(self , ["torch"] ) @classmethod def __A ( cls : List[str] , *__magic_name__ : Optional[Any] , **__magic_name__ : Optional[Any] ) -> int: requires_backends(cls , ["torch"] ) @classmethod def __A ( cls : List[Any] , *__magic_name__ : Dict , **__magic_name__ : Optional[Any] ) -> List[Any]: requires_backends(cls , ["torch"] ) class lowerCamelCase (metaclass=snake_case_ ): """simple docstring""" lowerCamelCase__ = ['''torch'''] def __init__( self : Optional[int] , *__magic_name__ : Tuple , **__magic_name__ : Dict ) -> Optional[Any]: requires_backends(self , ["torch"] ) @classmethod def __A ( cls : List[str] , *__magic_name__ : int , **__magic_name__ : Union[str, Any] ) -> int: requires_backends(cls , ["torch"] ) @classmethod def __A ( cls : Any , *__magic_name__ : List[str] , **__magic_name__ : Any ) -> str: requires_backends(cls , ["torch"] ) class lowerCamelCase (metaclass=snake_case_ ): """simple docstring""" lowerCamelCase__ = ['''torch'''] def __init__( self : Tuple , *__magic_name__ : Union[str, Any] , **__magic_name__ : Dict ) -> int: requires_backends(self , ["torch"] ) @classmethod def __A ( cls : Any , *__magic_name__ : List[Any] , **__magic_name__ : int ) -> Any: requires_backends(cls , ["torch"] ) @classmethod def __A ( cls : Union[str, Any] , *__magic_name__ : Optional[Any] , **__magic_name__ : Any ) -> List[Any]: requires_backends(cls , ["torch"] )
356
import torch def a__ ( ): if torch.cuda.is_available(): SCREAMING_SNAKE_CASE_ = torch.cuda.device_count() else: SCREAMING_SNAKE_CASE_ = 0 print(F'''Successfully ran on {num_gpus} GPUs''' ) if __name__ == "__main__": main()
305
0
def a__ ( __UpperCamelCase , __UpperCamelCase ): """simple docstring""" _validate_point(__UpperCamelCase ) _validate_point(__UpperCamelCase ) if len(__UpperCamelCase ) != len(__UpperCamelCase ): raise ValueError("Both points must be in the same n-dimensional space" ) return float(sum(abs(a - b ) for a, b in zip(__UpperCamelCase , __UpperCamelCase ) ) ) def a__ ( __UpperCamelCase ): """simple docstring""" if point: if isinstance(__UpperCamelCase , __UpperCamelCase ): for item in point: if not isinstance(__UpperCamelCase , (int, float) ): SCREAMING_SNAKE_CASE_ = ( '''Expected a list of numbers as input, found ''' F'''{type(__UpperCamelCase ).__name__}''' ) raise TypeError(__UpperCamelCase ) else: SCREAMING_SNAKE_CASE_ = F'''Expected a list of numbers as input, found {type(__UpperCamelCase ).__name__}''' raise TypeError(__UpperCamelCase ) else: raise ValueError("Missing an input" ) def a__ ( __UpperCamelCase , __UpperCamelCase ): """simple docstring""" _validate_point(__UpperCamelCase ) _validate_point(__UpperCamelCase ) if len(__UpperCamelCase ) != len(__UpperCamelCase ): raise ValueError("Both points must be in the same n-dimensional space" ) return float(sum(abs(x - y ) for x, y in zip(__UpperCamelCase , __UpperCamelCase ) ) ) if __name__ == "__main__": import doctest doctest.testmod()
357
from collections.abc import Generator from math import sin def a__ ( __UpperCamelCase ): if len(__UpperCamelCase ) != 3_2: raise ValueError("Input must be of length 32" ) SCREAMING_SNAKE_CASE_ = b"" for i in [3, 2, 1, 0]: little_endian += string_aa[8 * i : 8 * i + 8] return little_endian def a__ ( __UpperCamelCase ): if i < 0: raise ValueError("Input must be non-negative" ) SCREAMING_SNAKE_CASE_ = format(__UpperCamelCase , "08x" )[-8:] SCREAMING_SNAKE_CASE_ = b"" for i in [3, 2, 1, 0]: little_endian_hex += hex_rep[2 * i : 2 * i + 2].encode("utf-8" ) return little_endian_hex def a__ ( __UpperCamelCase ): SCREAMING_SNAKE_CASE_ = b"" for char in message: bit_string += format(__UpperCamelCase , "08b" ).encode("utf-8" ) SCREAMING_SNAKE_CASE_ = format(len(__UpperCamelCase ) , "064b" ).encode("utf-8" ) # Pad bit_string to a multiple of 512 chars bit_string += b"1" while len(__UpperCamelCase ) % 5_1_2 != 4_4_8: bit_string += b"0" bit_string += to_little_endian(start_len[3_2:] ) + to_little_endian(start_len[:3_2] ) return bit_string def a__ ( __UpperCamelCase ): if len(__UpperCamelCase ) % 5_1_2 != 0: raise ValueError("Input must have length that's a multiple of 512" ) for pos in range(0 , len(__UpperCamelCase ) , 5_1_2 ): SCREAMING_SNAKE_CASE_ = bit_string[pos : pos + 5_1_2] SCREAMING_SNAKE_CASE_ = [] for i in range(0 , 5_1_2 , 3_2 ): block_words.append(int(to_little_endian(block[i : i + 3_2] ) , 2 ) ) yield block_words def a__ ( __UpperCamelCase ): if i < 0: raise ValueError("Input must be non-negative" ) SCREAMING_SNAKE_CASE_ = format(__UpperCamelCase , "032b" ) SCREAMING_SNAKE_CASE_ = "" for c in i_str: new_str += "1" if c == "0" else "0" return int(__UpperCamelCase , 2 ) def a__ ( __UpperCamelCase , __UpperCamelCase ): return (a + b) % 2**3_2 def a__ ( __UpperCamelCase , __UpperCamelCase ): if i < 0: raise ValueError("Input must be non-negative" ) if shift < 0: raise ValueError("Shift must be non-negative" ) return ((i << shift) ^ (i >> (3_2 - shift))) % 2**3_2 def a__ ( __UpperCamelCase ): SCREAMING_SNAKE_CASE_ = preprocess(__UpperCamelCase ) SCREAMING_SNAKE_CASE_ = [int(2**3_2 * abs(sin(i + 1 ) ) ) for i in range(6_4 )] # Starting states SCREAMING_SNAKE_CASE_ = 0X67452301 SCREAMING_SNAKE_CASE_ = 0Xefcdab89 SCREAMING_SNAKE_CASE_ = 0X98badcfe SCREAMING_SNAKE_CASE_ = 0X10325476 SCREAMING_SNAKE_CASE_ = [ 7, 1_2, 1_7, 2_2, 7, 1_2, 1_7, 2_2, 7, 1_2, 1_7, 2_2, 7, 1_2, 1_7, 2_2, 5, 9, 1_4, 2_0, 5, 9, 1_4, 2_0, 5, 9, 1_4, 2_0, 5, 9, 1_4, 2_0, 4, 1_1, 1_6, 2_3, 4, 1_1, 1_6, 2_3, 4, 1_1, 1_6, 2_3, 4, 1_1, 1_6, 2_3, 6, 1_0, 1_5, 2_1, 6, 1_0, 1_5, 2_1, 6, 1_0, 1_5, 2_1, 6, 1_0, 1_5, 2_1, ] # Process bit string in chunks, each with 16 32-char words for block_words in get_block_words(__UpperCamelCase ): SCREAMING_SNAKE_CASE_ = aa SCREAMING_SNAKE_CASE_ = ba SCREAMING_SNAKE_CASE_ = ca SCREAMING_SNAKE_CASE_ = da # Hash current chunk for i in range(6_4 ): if i <= 1_5: # f = (b & c) | (not_32(b) & d) # Alternate definition for f SCREAMING_SNAKE_CASE_ = d ^ (b & (c ^ d)) SCREAMING_SNAKE_CASE_ = i elif i <= 3_1: # f = (d & b) | (not_32(d) & c) # Alternate definition for f SCREAMING_SNAKE_CASE_ = c ^ (d & (b ^ c)) SCREAMING_SNAKE_CASE_ = (5 * i + 1) % 1_6 elif i <= 4_7: SCREAMING_SNAKE_CASE_ = b ^ c ^ d SCREAMING_SNAKE_CASE_ = (3 * i + 5) % 1_6 else: SCREAMING_SNAKE_CASE_ = c ^ (b | not_aa(__UpperCamelCase )) SCREAMING_SNAKE_CASE_ = (7 * i) % 1_6 SCREAMING_SNAKE_CASE_ = (f + a + added_consts[i] + block_words[g]) % 2**3_2 SCREAMING_SNAKE_CASE_ = d SCREAMING_SNAKE_CASE_ = c SCREAMING_SNAKE_CASE_ = b SCREAMING_SNAKE_CASE_ = sum_aa(__UpperCamelCase , left_rotate_aa(__UpperCamelCase , shift_amounts[i] ) ) # Add hashed chunk to running total SCREAMING_SNAKE_CASE_ = sum_aa(__UpperCamelCase , __UpperCamelCase ) SCREAMING_SNAKE_CASE_ = sum_aa(__UpperCamelCase , __UpperCamelCase ) SCREAMING_SNAKE_CASE_ = sum_aa(__UpperCamelCase , __UpperCamelCase ) SCREAMING_SNAKE_CASE_ = sum_aa(__UpperCamelCase , __UpperCamelCase ) SCREAMING_SNAKE_CASE_ = reformat_hex(__UpperCamelCase ) + reformat_hex(__UpperCamelCase ) + reformat_hex(__UpperCamelCase ) + reformat_hex(__UpperCamelCase ) return digest if __name__ == "__main__": import doctest doctest.testmod()
305
0
def a__ ( __UpperCamelCase ): SCREAMING_SNAKE_CASE_ = int(A__ ) if decimal in (0, 1): # Exit cases for the recursion return str(A__ ) SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = divmod(A__ , 2 ) return binary_recursive(A__ ) + str(A__ ) def a__ ( __UpperCamelCase ): SCREAMING_SNAKE_CASE_ = str(A__ ).strip() if not number: raise ValueError("No input value was provided" ) SCREAMING_SNAKE_CASE_ = "-" if number.startswith("-" ) else "" SCREAMING_SNAKE_CASE_ = number.lstrip("-" ) if not number.isnumeric(): raise ValueError("Input value is not an integer" ) return F'''{negative}0b{binary_recursive(int(A__ ) )}''' if __name__ == "__main__": from doctest import testmod testmod()
358
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, BlipaProcessor, BlipImageProcessor, GPTaTokenizer, PreTrainedTokenizerFast @require_vision class lowerCamelCase (unittest.TestCase ): """simple docstring""" def __A ( self : int ) -> Any: SCREAMING_SNAKE_CASE_ = tempfile.mkdtemp() SCREAMING_SNAKE_CASE_ = BlipImageProcessor() SCREAMING_SNAKE_CASE_ = GPTaTokenizer.from_pretrained("hf-internal-testing/tiny-random-GPT2Model" ) SCREAMING_SNAKE_CASE_ = BlipaProcessor(__magic_name__ , __magic_name__ ) processor.save_pretrained(self.tmpdirname ) def __A ( self : str , **__magic_name__ : int ) -> Union[str, Any]: return AutoProcessor.from_pretrained(self.tmpdirname , **__magic_name__ ).tokenizer def __A ( self : Dict , **__magic_name__ : List[Any] ) -> int: return AutoProcessor.from_pretrained(self.tmpdirname , **__magic_name__ ).image_processor def __A ( self : int ) -> Any: shutil.rmtree(self.tmpdirname ) def __A ( self : Dict ) -> Dict: SCREAMING_SNAKE_CASE_ = [np.random.randint(255 , size=(3, 30, 400) , dtype=np.uinta )] SCREAMING_SNAKE_CASE_ = [Image.fromarray(np.moveaxis(__magic_name__ , 0 , -1 ) ) for x in image_inputs] return image_inputs def __A ( self : List[Any] ) -> Union[str, Any]: SCREAMING_SNAKE_CASE_ = BlipaProcessor(tokenizer=self.get_tokenizer() , image_processor=self.get_image_processor() ) processor.save_pretrained(self.tmpdirname ) SCREAMING_SNAKE_CASE_ = self.get_tokenizer(bos_token="(BOS)" , eos_token="(EOS)" ) SCREAMING_SNAKE_CASE_ = self.get_image_processor(do_normalize=__magic_name__ , padding_value=1.0 ) SCREAMING_SNAKE_CASE_ = BlipaProcessor.from_pretrained( self.tmpdirname , bos_token="(BOS)" , eos_token="(EOS)" , do_normalize=__magic_name__ , padding_value=1.0 ) self.assertEqual(processor.tokenizer.get_vocab() , tokenizer_add_kwargs.get_vocab() ) self.assertIsInstance(processor.tokenizer , __magic_name__ ) self.assertEqual(processor.image_processor.to_json_string() , image_processor_add_kwargs.to_json_string() ) self.assertIsInstance(processor.image_processor , __magic_name__ ) def __A ( self : Tuple ) -> int: SCREAMING_SNAKE_CASE_ = self.get_image_processor() SCREAMING_SNAKE_CASE_ = self.get_tokenizer() SCREAMING_SNAKE_CASE_ = BlipaProcessor(tokenizer=__magic_name__ , image_processor=__magic_name__ ) SCREAMING_SNAKE_CASE_ = self.prepare_image_inputs() SCREAMING_SNAKE_CASE_ = image_processor(__magic_name__ , return_tensors="np" ) SCREAMING_SNAKE_CASE_ = processor(images=__magic_name__ , 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 __A ( self : str ) -> Tuple: SCREAMING_SNAKE_CASE_ = self.get_image_processor() SCREAMING_SNAKE_CASE_ = self.get_tokenizer() SCREAMING_SNAKE_CASE_ = BlipaProcessor(tokenizer=__magic_name__ , image_processor=__magic_name__ ) SCREAMING_SNAKE_CASE_ = "lower newer" SCREAMING_SNAKE_CASE_ = processor(text=__magic_name__ ) SCREAMING_SNAKE_CASE_ = tokenizer(__magic_name__ , return_token_type_ids=__magic_name__ ) for key in encoded_tok.keys(): self.assertListEqual(encoded_tok[key] , encoded_processor[key] ) def __A ( self : Dict ) -> Union[str, Any]: SCREAMING_SNAKE_CASE_ = self.get_image_processor() SCREAMING_SNAKE_CASE_ = self.get_tokenizer() SCREAMING_SNAKE_CASE_ = BlipaProcessor(tokenizer=__magic_name__ , image_processor=__magic_name__ ) SCREAMING_SNAKE_CASE_ = "lower newer" SCREAMING_SNAKE_CASE_ = self.prepare_image_inputs() SCREAMING_SNAKE_CASE_ = processor(text=__magic_name__ , images=__magic_name__ ) self.assertListEqual(list(inputs.keys() ) , ["pixel_values", "input_ids", "attention_mask"] ) # test if it raises when no input is passed with pytest.raises(__magic_name__ ): processor() def __A ( self : Dict ) -> Tuple: SCREAMING_SNAKE_CASE_ = self.get_image_processor() SCREAMING_SNAKE_CASE_ = self.get_tokenizer() SCREAMING_SNAKE_CASE_ = BlipaProcessor(tokenizer=__magic_name__ , image_processor=__magic_name__ ) SCREAMING_SNAKE_CASE_ = [[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]] SCREAMING_SNAKE_CASE_ = processor.batch_decode(__magic_name__ ) SCREAMING_SNAKE_CASE_ = tokenizer.batch_decode(__magic_name__ ) self.assertListEqual(__magic_name__ , __magic_name__ ) def __A ( self : List[str] ) -> int: SCREAMING_SNAKE_CASE_ = self.get_image_processor() SCREAMING_SNAKE_CASE_ = self.get_tokenizer() SCREAMING_SNAKE_CASE_ = BlipaProcessor(tokenizer=__magic_name__ , image_processor=__magic_name__ ) SCREAMING_SNAKE_CASE_ = "lower newer" SCREAMING_SNAKE_CASE_ = self.prepare_image_inputs() SCREAMING_SNAKE_CASE_ = processor(text=__magic_name__ , images=__magic_name__ ) # For now the processor supports only ['pixel_values', 'input_ids', 'attention_mask'] self.assertListEqual(list(inputs.keys() ) , ["pixel_values", "input_ids", "attention_mask"] )
305
0
from typing import List, Optional import numpy as np from ...processing_utils import ProcessorMixin from ...utils import to_numpy class lowerCamelCase (SCREAMING_SNAKE_CASE__ ): """simple docstring""" lowerCamelCase__ = '''EncodecFeatureExtractor''' lowerCamelCase__ = ('''T5Tokenizer''', '''T5TokenizerFast''') def __init__( self : Union[str, Any] , __magic_name__ : List[str] , __magic_name__ : str ) -> List[str]: super().__init__(__magic_name__ , __magic_name__ ) SCREAMING_SNAKE_CASE_ = self.feature_extractor SCREAMING_SNAKE_CASE_ = False def __A ( self : Optional[Any] , __magic_name__ : int=None , __magic_name__ : List[str]=None , __magic_name__ : Any=True ) -> Dict: return self.tokenizer.get_decoder_prompt_ids(task=__magic_name__ , language=__magic_name__ , no_timestamps=__magic_name__ ) def __call__( self : Dict , *__magic_name__ : Optional[int] , **__magic_name__ : Dict ) -> Optional[int]: # For backward compatibility if self._in_target_context_manager: return self.current_processor(*__magic_name__ , **__magic_name__ ) SCREAMING_SNAKE_CASE_ = kwargs.pop("audio" , __magic_name__ ) SCREAMING_SNAKE_CASE_ = kwargs.pop("sampling_rate" , __magic_name__ ) SCREAMING_SNAKE_CASE_ = kwargs.pop("text" , __magic_name__ ) if len(__magic_name__ ) > 0: SCREAMING_SNAKE_CASE_ = args[0] SCREAMING_SNAKE_CASE_ = 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 text is not None: SCREAMING_SNAKE_CASE_ = self.tokenizer(__magic_name__ , **__magic_name__ ) if audio is not None: SCREAMING_SNAKE_CASE_ = self.feature_extractor(__magic_name__ , *__magic_name__ , sampling_rate=__magic_name__ , **__magic_name__ ) if audio is None: return inputs elif text is None: return audio_inputs else: SCREAMING_SNAKE_CASE_ = audio_inputs['input_values'] if "padding_mask" in audio_inputs: SCREAMING_SNAKE_CASE_ = audio_inputs['padding_mask'] return inputs def __A ( self : Tuple , *__magic_name__ : Optional[Any] , **__magic_name__ : Optional[int] ) -> Tuple: SCREAMING_SNAKE_CASE_ = kwargs.pop("audio" , __magic_name__ ) SCREAMING_SNAKE_CASE_ = kwargs.pop("padding_mask" , __magic_name__ ) if len(__magic_name__ ) > 0: SCREAMING_SNAKE_CASE_ = args[0] SCREAMING_SNAKE_CASE_ = args[1:] if audio_values is not None: return self._decode_audio(__magic_name__ , padding_mask=__magic_name__ ) else: return self.tokenizer.batch_decode(*__magic_name__ , **__magic_name__ ) def __A ( self : Tuple , *__magic_name__ : str , **__magic_name__ : Any ) -> List[Any]: return self.tokenizer.decode(*__magic_name__ , **__magic_name__ ) def __A ( self : Union[str, Any] , __magic_name__ : int , __magic_name__ : Optional = None ) -> List[np.ndarray]: SCREAMING_SNAKE_CASE_ = to_numpy(__magic_name__ ) SCREAMING_SNAKE_CASE_ = audio_values.shape if padding_mask is None: return list(__magic_name__ ) SCREAMING_SNAKE_CASE_ = to_numpy(__magic_name__ ) # match the sequence length of the padding mask to the generated audio arrays by padding with the **non-padding** # token (so that the generated audio values are **not** treated as padded tokens) SCREAMING_SNAKE_CASE_ = seq_len - padding_mask.shape[-1] SCREAMING_SNAKE_CASE_ = 1 - self.feature_extractor.padding_value SCREAMING_SNAKE_CASE_ = np.pad(__magic_name__ , ((0, 0), (0, difference)) , "constant" , constant_values=__magic_name__ ) SCREAMING_SNAKE_CASE_ = audio_values.tolist() for i in range(__magic_name__ ): SCREAMING_SNAKE_CASE_ = np.asarray(audio_values[i] )[ padding_mask[i][None, :] != self.feature_extractor.padding_value ] SCREAMING_SNAKE_CASE_ = sliced_audio.reshape(__magic_name__ , -1 ) return audio_values
359
from typing import TYPE_CHECKING from ...file_utils import _LazyModule, is_tokenizers_available, is_torch_available, is_vision_available from ...utils import OptionalDependencyNotAvailable A : List[Any] = {"configuration_dpt": ["DPT_PRETRAINED_CONFIG_ARCHIVE_MAP", "DPTConfig"]} try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: A : Optional[Any] = ["DPTFeatureExtractor"] A : str = ["DPTImageProcessor"] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: A : Optional[Any] = [ "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 A : Optional[int] = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
305
0
def a__ ( __UpperCamelCase , __UpperCamelCase ): # "extended trapezoidal rule" # int(f) = dx/2 * (f1 + 2f2 + ... + fn) SCREAMING_SNAKE_CASE_ = (boundary[1] - boundary[0]) / steps SCREAMING_SNAKE_CASE_ = boundary[0] SCREAMING_SNAKE_CASE_ = boundary[1] SCREAMING_SNAKE_CASE_ = make_points(a__ , a__ , a__ ) SCREAMING_SNAKE_CASE_ = 0.0 y += (h / 2.0) * f(a__ ) for i in x_i: # print(i) y += h * f(a__ ) y += (h / 2.0) * f(a__ ) return y def a__ ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase ): SCREAMING_SNAKE_CASE_ = a + h while x < (b - h): yield x SCREAMING_SNAKE_CASE_ = x + h def a__ ( __UpperCamelCase ): # enter your function here SCREAMING_SNAKE_CASE_ = (x - 0) * (x - 0) return y def a__ ( ): SCREAMING_SNAKE_CASE_ = 0.0 # Lower bound of integration SCREAMING_SNAKE_CASE_ = 1.0 # Upper bound of integration SCREAMING_SNAKE_CASE_ = 10.0 # define number of steps or resolution SCREAMING_SNAKE_CASE_ = [a, b] # define boundary of integration SCREAMING_SNAKE_CASE_ = method_a(a__ , a__ ) print(F'''y = {y}''' ) if __name__ == "__main__": main()
360
from __future__ import annotations import collections import pprint from pathlib import Path def a__ ( __UpperCamelCase ): return "".join(sorted(__UpperCamelCase ) ) def a__ ( __UpperCamelCase ): return word_by_signature[signature(__UpperCamelCase )] A : str = Path(__file__).parent.joinpath("words.txt").read_text(encoding="utf-8") A : int = sorted({word.strip().lower() for word in data.splitlines()}) A : Tuple = collections.defaultdict(list) for word in word_list: word_by_signature[signature(word)].append(word) if __name__ == "__main__": A : Union[str, Any] = {word: anagram(word) for word in word_list if len(anagram(word)) > 1} with open("anagrams.txt", "w") as file: file.write("all_anagrams = \n ") file.write(pprint.pformat(all_anagrams))
305
0
import copy from ...configuration_utils import PretrainedConfig from ...utils import logging A : Optional[Any] = logging.get_logger(__name__) class lowerCamelCase (__lowercase ): """simple docstring""" lowerCamelCase__ = '''encoder-decoder''' lowerCamelCase__ = True def __init__( self : Any , **__magic_name__ : List[Any] ) -> str: """simple docstring""" super().__init__(**UpperCAmelCase__ ) assert ( "encoder" in kwargs and "decoder" in kwargs ), "Config has to be initialized with encoder and decoder config" SCREAMING_SNAKE_CASE_ = kwargs.pop("encoder" ) SCREAMING_SNAKE_CASE_ = encoder_config.pop("model_type" ) SCREAMING_SNAKE_CASE_ = kwargs.pop("decoder" ) SCREAMING_SNAKE_CASE_ = decoder_config.pop("model_type" ) from ..auto.configuration_auto import AutoConfig SCREAMING_SNAKE_CASE_ = AutoConfig.for_model(UpperCAmelCase__ , **UpperCAmelCase__ ) SCREAMING_SNAKE_CASE_ = AutoConfig.for_model(UpperCAmelCase__ , **UpperCAmelCase__ ) SCREAMING_SNAKE_CASE_ = True @classmethod def __A ( cls : Union[str, Any] , __magic_name__ : PretrainedConfig , __magic_name__ : PretrainedConfig , **__magic_name__ : Any ) -> PretrainedConfig: """simple docstring""" logger.info("Set `config.is_decoder=True` and `config.add_cross_attention=True` for decoder_config" ) SCREAMING_SNAKE_CASE_ = True SCREAMING_SNAKE_CASE_ = True return cls(encoder=encoder_config.to_dict() , decoder=decoder_config.to_dict() , **UpperCAmelCase__ ) def __A ( self : Tuple ) -> Dict: """simple docstring""" SCREAMING_SNAKE_CASE_ = copy.deepcopy(self.__dict__ ) SCREAMING_SNAKE_CASE_ = self.encoder.to_dict() SCREAMING_SNAKE_CASE_ = self.decoder.to_dict() SCREAMING_SNAKE_CASE_ = self.__class__.model_type return output
361
import copy import os from typing import TYPE_CHECKING, List, Union if TYPE_CHECKING: pass from ...configuration_utils import PretrainedConfig from ...utils import logging A : int = logging.get_logger(__name__) A : str = { "kakaobrain/align-base": "https://huggingface.co/kakaobrain/align-base/resolve/main/config.json", } class lowerCamelCase (SCREAMING_SNAKE_CASE__ ): """simple docstring""" lowerCamelCase__ = '''align_text_model''' def __init__( self : Optional[Any] , __magic_name__ : Union[str, Any]=30_522 , __magic_name__ : Tuple=768 , __magic_name__ : List[str]=12 , __magic_name__ : Optional[Any]=12 , __magic_name__ : str=3_072 , __magic_name__ : Dict="gelu" , __magic_name__ : Any=0.1 , __magic_name__ : Optional[int]=0.1 , __magic_name__ : List[str]=512 , __magic_name__ : Any=2 , __magic_name__ : Optional[Any]=0.02 , __magic_name__ : int=1e-12 , __magic_name__ : str=0 , __magic_name__ : Optional[Any]="absolute" , __magic_name__ : Optional[Any]=True , **__magic_name__ : Tuple , ) -> Union[str, Any]: super().__init__(**__magic_name__ ) 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_ = position_embedding_type SCREAMING_SNAKE_CASE_ = use_cache SCREAMING_SNAKE_CASE_ = pad_token_id @classmethod def __A ( cls : Any , __magic_name__ : Union[str, os.PathLike] , **__magic_name__ : Optional[Any] ) -> "PretrainedConfig": cls._set_token_in_kwargs(__magic_name__ ) SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = cls.get_config_dict(__magic_name__ , **__magic_name__ ) # get the text config dict if we are loading from AlignConfig if config_dict.get("model_type" ) == "align": SCREAMING_SNAKE_CASE_ = config_dict["text_config"] if "model_type" in config_dict and hasattr(cls , "model_type" ) and config_dict["model_type"] != cls.model_type: logger.warning( F'''You are using a model of type {config_dict["model_type"]} to instantiate a model of type ''' F'''{cls.model_type}. This is not supported for all configurations of models and can yield errors.''' ) return cls.from_dict(__magic_name__ , **__magic_name__ ) class lowerCamelCase (SCREAMING_SNAKE_CASE__ ): """simple docstring""" lowerCamelCase__ = '''align_vision_model''' def __init__( self : List[str] , __magic_name__ : int = 3 , __magic_name__ : int = 600 , __magic_name__ : float = 2.0 , __magic_name__ : float = 3.1 , __magic_name__ : int = 8 , __magic_name__ : List[int] = [3, 3, 5, 3, 5, 5, 3] , __magic_name__ : List[int] = [32, 16, 24, 40, 80, 112, 192] , __magic_name__ : List[int] = [16, 24, 40, 80, 112, 192, 320] , __magic_name__ : List[int] = [] , __magic_name__ : List[int] = [1, 2, 2, 2, 1, 2, 1] , __magic_name__ : List[int] = [1, 2, 2, 3, 3, 4, 1] , __magic_name__ : List[int] = [1, 6, 6, 6, 6, 6, 6] , __magic_name__ : float = 0.25 , __magic_name__ : str = "swish" , __magic_name__ : int = 2_560 , __magic_name__ : str = "mean" , __magic_name__ : float = 0.02 , __magic_name__ : float = 0.001 , __magic_name__ : float = 0.99 , __magic_name__ : float = 0.2 , **__magic_name__ : List[Any] , ) -> Tuple: super().__init__(**__magic_name__ ) SCREAMING_SNAKE_CASE_ = num_channels SCREAMING_SNAKE_CASE_ = image_size SCREAMING_SNAKE_CASE_ = width_coefficient SCREAMING_SNAKE_CASE_ = depth_coefficient SCREAMING_SNAKE_CASE_ = depth_divisor SCREAMING_SNAKE_CASE_ = kernel_sizes SCREAMING_SNAKE_CASE_ = in_channels SCREAMING_SNAKE_CASE_ = out_channels SCREAMING_SNAKE_CASE_ = depthwise_padding SCREAMING_SNAKE_CASE_ = strides SCREAMING_SNAKE_CASE_ = num_block_repeats SCREAMING_SNAKE_CASE_ = expand_ratios SCREAMING_SNAKE_CASE_ = squeeze_expansion_ratio SCREAMING_SNAKE_CASE_ = hidden_act SCREAMING_SNAKE_CASE_ = hidden_dim SCREAMING_SNAKE_CASE_ = pooling_type SCREAMING_SNAKE_CASE_ = initializer_range SCREAMING_SNAKE_CASE_ = batch_norm_eps SCREAMING_SNAKE_CASE_ = batch_norm_momentum SCREAMING_SNAKE_CASE_ = drop_connect_rate SCREAMING_SNAKE_CASE_ = sum(__magic_name__ ) * 4 @classmethod def __A ( cls : List[str] , __magic_name__ : Union[str, os.PathLike] , **__magic_name__ : Dict ) -> "PretrainedConfig": cls._set_token_in_kwargs(__magic_name__ ) SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = cls.get_config_dict(__magic_name__ , **__magic_name__ ) # get the vision config dict if we are loading from AlignConfig if config_dict.get("model_type" ) == "align": SCREAMING_SNAKE_CASE_ = 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(__magic_name__ , **__magic_name__ ) class lowerCamelCase (SCREAMING_SNAKE_CASE__ ): """simple docstring""" lowerCamelCase__ = '''align''' lowerCamelCase__ = True def __init__( self : Optional[Any] , __magic_name__ : Dict=None , __magic_name__ : List[Any]=None , __magic_name__ : str=640 , __magic_name__ : Any=1.0 , __magic_name__ : Dict=0.02 , **__magic_name__ : Union[str, Any] , ) -> int: super().__init__(**__magic_name__ ) if text_config is None: SCREAMING_SNAKE_CASE_ = {} logger.info("text_config is None. Initializing the AlignTextConfig with default values." ) if vision_config is None: SCREAMING_SNAKE_CASE_ = {} logger.info("vision_config is None. Initializing the AlignVisionConfig with default values." ) SCREAMING_SNAKE_CASE_ = AlignTextConfig(**__magic_name__ ) SCREAMING_SNAKE_CASE_ = AlignVisionConfig(**__magic_name__ ) SCREAMING_SNAKE_CASE_ = projection_dim SCREAMING_SNAKE_CASE_ = temperature_init_value SCREAMING_SNAKE_CASE_ = initializer_range @classmethod def __A ( cls : List[str] , __magic_name__ : AlignTextConfig , __magic_name__ : AlignVisionConfig , **__magic_name__ : Tuple ) -> Any: return cls(text_config=text_config.to_dict() , vision_config=vision_config.to_dict() , **__magic_name__ ) def __A ( self : int ) -> Union[str, Any]: SCREAMING_SNAKE_CASE_ = copy.deepcopy(self.__dict__ ) SCREAMING_SNAKE_CASE_ = self.text_config.to_dict() SCREAMING_SNAKE_CASE_ = self.vision_config.to_dict() SCREAMING_SNAKE_CASE_ = self.__class__.model_type return output
305
0
"""simple docstring""" from collections import OrderedDict from typing import Any, Mapping, Optional, Union from ...configuration_utils import PretrainedConfig from ...feature_extraction_utils import FeatureExtractionMixin from ...onnx import OnnxConfig from ...onnx.utils import compute_effective_axis_dimension from ...tokenization_utils_base import PreTrainedTokenizerBase from ...utils import TensorType, logging A : Dict = logging.get_logger(__name__) A : Optional[int] = { "deepmind/language-perceiver": "https://huggingface.co/deepmind/language-perceiver/resolve/main/config.json", # See all Perceiver models at https://huggingface.co/models?filter=perceiver } class lowerCamelCase (UpperCamelCase_ ): """simple docstring""" lowerCamelCase__ = """perceiver""" def __init__( self : str , __magic_name__ : Optional[Any]=256 , __magic_name__ : Optional[int]=1_280 , __magic_name__ : List[str]=768 , __magic_name__ : Tuple=1 , __magic_name__ : Union[str, Any]=26 , __magic_name__ : Optional[Any]=8 , __magic_name__ : Tuple=8 , __magic_name__ : List[str]=None , __magic_name__ : int=None , __magic_name__ : List[Any]="kv" , __magic_name__ : Optional[Any]=1 , __magic_name__ : List[Any]=1 , __magic_name__ : List[Any]="gelu" , __magic_name__ : str=0.1 , __magic_name__ : str=0.02 , __magic_name__ : Dict=1e-12 , __magic_name__ : List[str]=True , __magic_name__ : Optional[int]=262 , __magic_name__ : Any=2_048 , __magic_name__ : Optional[int]=56 , __magic_name__ : List[Any]=[368, 496] , __magic_name__ : Tuple=16 , __magic_name__ : List[Any]=1_920 , __magic_name__ : List[Any]=16 , __magic_name__ : Tuple=[1, 16, 224, 224] , **__magic_name__ : Optional[int] , ) -> Union[str, Any]: super().__init__(**_a ) SCREAMING_SNAKE_CASE_ = num_latents SCREAMING_SNAKE_CASE_ = d_latents SCREAMING_SNAKE_CASE_ = d_model SCREAMING_SNAKE_CASE_ = num_blocks SCREAMING_SNAKE_CASE_ = num_self_attends_per_block SCREAMING_SNAKE_CASE_ = num_self_attention_heads SCREAMING_SNAKE_CASE_ = num_cross_attention_heads SCREAMING_SNAKE_CASE_ = qk_channels SCREAMING_SNAKE_CASE_ = v_channels SCREAMING_SNAKE_CASE_ = cross_attention_shape_for_attention SCREAMING_SNAKE_CASE_ = self_attention_widening_factor SCREAMING_SNAKE_CASE_ = cross_attention_widening_factor SCREAMING_SNAKE_CASE_ = hidden_act SCREAMING_SNAKE_CASE_ = attention_probs_dropout_prob SCREAMING_SNAKE_CASE_ = initializer_range SCREAMING_SNAKE_CASE_ = layer_norm_eps SCREAMING_SNAKE_CASE_ = use_query_residual # masked language modeling attributes SCREAMING_SNAKE_CASE_ = vocab_size SCREAMING_SNAKE_CASE_ = max_position_embeddings # image classification attributes SCREAMING_SNAKE_CASE_ = image_size # flow attributes SCREAMING_SNAKE_CASE_ = train_size # multimodal autoencoding attributes SCREAMING_SNAKE_CASE_ = num_frames SCREAMING_SNAKE_CASE_ = audio_samples_per_frame SCREAMING_SNAKE_CASE_ = samples_per_patch SCREAMING_SNAKE_CASE_ = output_shape class lowerCamelCase (UpperCamelCase_ ): """simple docstring""" @property def __A ( self : Any ) -> Mapping[str, Mapping[int, str]]: if self.task == "multiple-choice": SCREAMING_SNAKE_CASE_ = {0: """batch""", 1: """choice""", 2: """sequence"""} else: SCREAMING_SNAKE_CASE_ = {0: """batch""", 1: """sequence"""} return OrderedDict( [ ("inputs", dynamic_axis), ("attention_mask", dynamic_axis), ] ) @property def __A ( self : int ) -> float: return 1e-4 def __A ( self : Union[str, Any] , __magic_name__ : Tuple , __magic_name__ : Any = -1 , __magic_name__ : Dict = -1 , __magic_name__ : Dict = -1 , __magic_name__ : List[str] = False , __magic_name__ : Union[str, Any] = None , __magic_name__ : int = 3 , __magic_name__ : Dict = 40 , __magic_name__ : str = 40 , ) -> Mapping[str, Any]: if isinstance(_a , _a ): # If dynamic axis (-1) we forward with a fixed dimension of 2 samples to avoid optimizations made by ONNX SCREAMING_SNAKE_CASE_ = compute_effective_axis_dimension( _a , fixed_dimension=OnnxConfig.default_fixed_batch , num_token_to_add=0 ) # If dynamic axis (-1) we forward with a fixed dimension of 8 tokens to avoid optimizations made by ONNX SCREAMING_SNAKE_CASE_ = preprocessor.num_special_tokens_to_add(_a ) SCREAMING_SNAKE_CASE_ = compute_effective_axis_dimension( _a , fixed_dimension=OnnxConfig.default_fixed_sequence , num_token_to_add=_a ) # Generate dummy inputs according to compute batch and sequence SCREAMING_SNAKE_CASE_ = [""" """.join(["a"] ) * seq_length] * batch_size SCREAMING_SNAKE_CASE_ = dict(preprocessor(_a , return_tensors=_a ) ) SCREAMING_SNAKE_CASE_ = inputs.pop("input_ids" ) return inputs elif isinstance(_a , _a ) and preprocessor.model_input_names[0] == "pixel_values": # If dynamic axis (-1) we forward with a fixed dimension of 2 samples to avoid optimizations made by ONNX SCREAMING_SNAKE_CASE_ = compute_effective_axis_dimension(_a , fixed_dimension=OnnxConfig.default_fixed_batch ) SCREAMING_SNAKE_CASE_ = self._generate_dummy_images(_a , _a , _a , _a ) SCREAMING_SNAKE_CASE_ = dict(preprocessor(images=_a , return_tensors=_a ) ) SCREAMING_SNAKE_CASE_ = inputs.pop("pixel_values" ) return inputs else: raise ValueError( "Unable to generate dummy inputs for the model. Please provide a tokenizer or a preprocessor." )
362
import unittest from transformers.testing_utils import CaptureStdout from transformers.tools.python_interpreter import evaluate def a__ ( __UpperCamelCase ): return x + 2 class lowerCamelCase (unittest.TestCase ): """simple docstring""" def __A ( self : List[Any] ) -> int: SCREAMING_SNAKE_CASE_ = "x = 3" SCREAMING_SNAKE_CASE_ = {} SCREAMING_SNAKE_CASE_ = evaluate(__magic_name__ , {} , state=__magic_name__ ) assert result == 3 self.assertDictEqual(__magic_name__ , {"x": 3} ) SCREAMING_SNAKE_CASE_ = "x = y" SCREAMING_SNAKE_CASE_ = {"y": 5} SCREAMING_SNAKE_CASE_ = evaluate(__magic_name__ , {} , state=__magic_name__ ) # evaluate returns the value of the last assignment. assert result == 5 self.assertDictEqual(__magic_name__ , {"x": 5, "y": 5} ) def __A ( self : Union[str, Any] ) -> str: SCREAMING_SNAKE_CASE_ = "y = add_two(x)" SCREAMING_SNAKE_CASE_ = {"x": 3} SCREAMING_SNAKE_CASE_ = evaluate(__magic_name__ , {"add_two": add_two} , state=__magic_name__ ) assert result == 5 self.assertDictEqual(__magic_name__ , {"x": 3, "y": 5} ) # Won't work without the tool with CaptureStdout() as out: SCREAMING_SNAKE_CASE_ = evaluate(__magic_name__ , {} , state=__magic_name__ ) assert result is None assert "tried to execute add_two" in out.out def __A ( self : List[str] ) -> int: SCREAMING_SNAKE_CASE_ = "x = 3" SCREAMING_SNAKE_CASE_ = {} SCREAMING_SNAKE_CASE_ = evaluate(__magic_name__ , {} , state=__magic_name__ ) assert result == 3 self.assertDictEqual(__magic_name__ , {"x": 3} ) def __A ( self : Optional[Any] ) -> str: SCREAMING_SNAKE_CASE_ = "test_dict = {'x': x, 'y': add_two(x)}" SCREAMING_SNAKE_CASE_ = {"x": 3} SCREAMING_SNAKE_CASE_ = evaluate(__magic_name__ , {"add_two": add_two} , state=__magic_name__ ) self.assertDictEqual(__magic_name__ , {"x": 3, "y": 5} ) self.assertDictEqual(__magic_name__ , {"x": 3, "test_dict": {"x": 3, "y": 5}} ) def __A ( self : Optional[int] ) -> List[str]: SCREAMING_SNAKE_CASE_ = "x = 3\ny = 5" SCREAMING_SNAKE_CASE_ = {} SCREAMING_SNAKE_CASE_ = evaluate(__magic_name__ , {} , state=__magic_name__ ) # evaluate returns the value of the last assignment. assert result == 5 self.assertDictEqual(__magic_name__ , {"x": 3, "y": 5} ) def __A ( self : Any ) -> List[str]: SCREAMING_SNAKE_CASE_ = "text = f'This is x: {x}.'" SCREAMING_SNAKE_CASE_ = {"x": 3} SCREAMING_SNAKE_CASE_ = evaluate(__magic_name__ , {} , state=__magic_name__ ) # evaluate returns the value of the last assignment. assert result == "This is x: 3." self.assertDictEqual(__magic_name__ , {"x": 3, "text": "This is x: 3."} ) def __A ( self : int ) -> Tuple: SCREAMING_SNAKE_CASE_ = "if x <= 3:\n y = 2\nelse:\n y = 5" SCREAMING_SNAKE_CASE_ = {"x": 3} SCREAMING_SNAKE_CASE_ = evaluate(__magic_name__ , {} , state=__magic_name__ ) # evaluate returns the value of the last assignment. assert result == 2 self.assertDictEqual(__magic_name__ , {"x": 3, "y": 2} ) SCREAMING_SNAKE_CASE_ = {"x": 8} SCREAMING_SNAKE_CASE_ = evaluate(__magic_name__ , {} , state=__magic_name__ ) # evaluate returns the value of the last assignment. assert result == 5 self.assertDictEqual(__magic_name__ , {"x": 8, "y": 5} ) def __A ( self : str ) -> str: SCREAMING_SNAKE_CASE_ = "test_list = [x, add_two(x)]" SCREAMING_SNAKE_CASE_ = {"x": 3} SCREAMING_SNAKE_CASE_ = evaluate(__magic_name__ , {"add_two": add_two} , state=__magic_name__ ) self.assertListEqual(__magic_name__ , [3, 5] ) self.assertDictEqual(__magic_name__ , {"x": 3, "test_list": [3, 5]} ) def __A ( self : Union[str, Any] ) -> List[Any]: SCREAMING_SNAKE_CASE_ = "y = x" SCREAMING_SNAKE_CASE_ = {"x": 3} SCREAMING_SNAKE_CASE_ = evaluate(__magic_name__ , {} , state=__magic_name__ ) assert result == 3 self.assertDictEqual(__magic_name__ , {"x": 3, "y": 3} ) def __A ( self : Tuple ) -> List[Any]: SCREAMING_SNAKE_CASE_ = "test_list = [x, add_two(x)]\ntest_list[1]" SCREAMING_SNAKE_CASE_ = {"x": 3} SCREAMING_SNAKE_CASE_ = evaluate(__magic_name__ , {"add_two": add_two} , state=__magic_name__ ) assert result == 5 self.assertDictEqual(__magic_name__ , {"x": 3, "test_list": [3, 5]} ) SCREAMING_SNAKE_CASE_ = "test_dict = {'x': x, 'y': add_two(x)}\ntest_dict['y']" SCREAMING_SNAKE_CASE_ = {"x": 3} SCREAMING_SNAKE_CASE_ = evaluate(__magic_name__ , {"add_two": add_two} , state=__magic_name__ ) assert result == 5 self.assertDictEqual(__magic_name__ , {"x": 3, "test_dict": {"x": 3, "y": 5}} ) def __A ( self : Tuple ) -> Any: SCREAMING_SNAKE_CASE_ = "x = 0\nfor i in range(3):\n x = i" SCREAMING_SNAKE_CASE_ = {} SCREAMING_SNAKE_CASE_ = evaluate(__magic_name__ , {"range": range} , state=__magic_name__ ) assert result == 2 self.assertDictEqual(__magic_name__ , {"x": 2, "i": 2} )
305
0
import operator as op def a__ ( __UpperCamelCase ): SCREAMING_SNAKE_CASE_ = [] SCREAMING_SNAKE_CASE_ = lambda __UpperCamelCase , __UpperCamelCase : int(x / y ) # noqa: E731 integer division operation SCREAMING_SNAKE_CASE_ = { "^": op.pow, "*": op.mul, "/": div, "+": op.add, "-": op.sub, } # operators & their respective operation # print table header print("Symbol".center(8 ) , "Action".center(1_2 ) , "Stack" , sep=" | " ) print("-" * (3_0 + len(lowercase__ )) ) for x in post_fix: if x.isdigit(): # if x in digit stack.append(lowercase__ ) # append x to stack # output in tabular format print(x.rjust(8 ) , ("push(" + x + ")").ljust(1_2 ) , ",".join(lowercase__ ) , sep=" | " ) else: SCREAMING_SNAKE_CASE_ = stack.pop() # pop stack # output in tabular format print("".rjust(8 ) , ("pop(" + b + ")").ljust(1_2 ) , ",".join(lowercase__ ) , sep=" | " ) SCREAMING_SNAKE_CASE_ = stack.pop() # pop stack # output in tabular format print("".rjust(8 ) , ("pop(" + a + ")").ljust(1_2 ) , ",".join(lowercase__ ) , sep=" | " ) stack.append( str(opr[x](int(lowercase__ ) , int(lowercase__ ) ) ) ) # evaluate the 2 values popped from stack & push result to stack # output in tabular format print( x.rjust(8 ) , ("push(" + a + x + b + ")").ljust(1_2 ) , ",".join(lowercase__ ) , sep=" | " , ) return int(stack[0] ) if __name__ == "__main__": A : int = input("\n\nEnter a Postfix Equation (space separated) = ").split(" ") print("\n\tResult = ", solve(Postfix))
363
import numpy as np import pandas as pd from sklearn.preprocessing import Normalizer from sklearn.svm import SVR from statsmodels.tsa.statespace.sarimax import SARIMAX def a__ ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase ): SCREAMING_SNAKE_CASE_ = np.array([[1, item, train_mtch[i]] for i, item in enumerate(__UpperCamelCase )] ) SCREAMING_SNAKE_CASE_ = np.array(__UpperCamelCase ) SCREAMING_SNAKE_CASE_ = np.dot(np.dot(np.linalg.inv(np.dot(x.transpose() , __UpperCamelCase ) ) , x.transpose() ) , __UpperCamelCase ) return abs(beta[0] + test_dt[0] * beta[1] + test_mtch[0] + beta[2] ) def a__ ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase ): SCREAMING_SNAKE_CASE_ = (1, 2, 1) SCREAMING_SNAKE_CASE_ = (1, 1, 0, 7) SCREAMING_SNAKE_CASE_ = SARIMAX( __UpperCamelCase , exog=__UpperCamelCase , order=__UpperCamelCase , seasonal_order=__UpperCamelCase ) SCREAMING_SNAKE_CASE_ = model.fit(disp=__UpperCamelCase , maxiter=6_0_0 , method="nm" ) SCREAMING_SNAKE_CASE_ = model_fit.predict(1 , len(__UpperCamelCase ) , exog=[test_match] ) return result[0] def a__ ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase ): SCREAMING_SNAKE_CASE_ = SVR(kernel="rbf" , C=1 , gamma=0.1 , epsilon=0.1 ) regressor.fit(__UpperCamelCase , __UpperCamelCase ) SCREAMING_SNAKE_CASE_ = regressor.predict(__UpperCamelCase ) return y_pred[0] def a__ ( __UpperCamelCase ): train_user.sort() SCREAMING_SNAKE_CASE_ = np.percentile(__UpperCamelCase , 2_5 ) SCREAMING_SNAKE_CASE_ = np.percentile(__UpperCamelCase , 7_5 ) SCREAMING_SNAKE_CASE_ = qa - qa SCREAMING_SNAKE_CASE_ = qa - (iqr * 0.1) return low_lim def a__ ( __UpperCamelCase , __UpperCamelCase ): SCREAMING_SNAKE_CASE_ = 0 SCREAMING_SNAKE_CASE_ = 0 for i in list_vote: if i > actual_result: SCREAMING_SNAKE_CASE_ = not_safe + 1 else: if abs(abs(__UpperCamelCase ) - abs(__UpperCamelCase ) ) <= 0.1: safe += 1 else: not_safe += 1 return safe > not_safe if __name__ == "__main__": # data_input_df = pd.read_csv("ex_data.csv", header=None) A : Dict = [[1_82_31, 0.0, 1], [2_26_21, 1.0, 2], [1_56_75, 0.0, 3], [2_35_83, 1.0, 4]] A : Optional[Any] = pd.DataFrame( data_input, columns=["total_user", "total_even", "days"] ) A : Union[str, Any] = Normalizer().fit_transform(data_input_df.values) # split data A : Optional[int] = normalize_df[:, 2].tolist() A : List[str] = normalize_df[:, 0].tolist() A : int = normalize_df[:, 1].tolist() # for svr (input variable = total date and total match) A : int = normalize_df[:, [1, 2]].tolist() A : Tuple = x[: len(x) - 1] A : str = x[len(x) - 1 :] # for linear regression & sarimax A : Tuple = total_date[: len(total_date) - 1] A : Optional[int] = total_user[: len(total_user) - 1] A : str = total_match[: len(total_match) - 1] A : List[Any] = total_date[len(total_date) - 1 :] A : List[Any] = total_user[len(total_user) - 1 :] A : Optional[Any] = total_match[len(total_match) - 1 :] # voting system with forecasting A : Optional[int] = [ linear_regression_prediction( trn_date, trn_user, trn_match, tst_date, tst_match ), sarimax_predictor(trn_user, trn_match, tst_match), support_vector_regressor(x_train, x_test, trn_user), ] # check the safety of today's data A : str = "" if data_safety_checker(res_vote, tst_user) else "not " print("Today's data is {not_str}safe.")
305
0
def a__ ( __UpperCamelCase = 1_0_0_0 ): SCREAMING_SNAKE_CASE_ = 1, 1 SCREAMING_SNAKE_CASE_ = [] for i in range(1 , n + 1 ): SCREAMING_SNAKE_CASE_ = prev_numerator + 2 * prev_denominator SCREAMING_SNAKE_CASE_ = prev_numerator + prev_denominator if len(str(__snake_case ) ) > len(str(__snake_case ) ): result.append(__snake_case ) SCREAMING_SNAKE_CASE_ = numerator SCREAMING_SNAKE_CASE_ = denominator return len(__snake_case ) if __name__ == "__main__": print(f"{solution() = }")
364
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tf_available, is_torch_available A : List[str] = {"configuration_swin": ["SWIN_PRETRAINED_CONFIG_ARCHIVE_MAP", "SwinConfig", "SwinOnnxConfig"]} try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: A : Any = [ "SWIN_PRETRAINED_MODEL_ARCHIVE_LIST", "SwinForImageClassification", "SwinForMaskedImageModeling", "SwinModel", "SwinPreTrainedModel", "SwinBackbone", ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: A : str = [ "TF_SWIN_PRETRAINED_MODEL_ARCHIVE_LIST", "TFSwinForImageClassification", "TFSwinForMaskedImageModeling", "TFSwinModel", "TFSwinPreTrainedModel", ] if TYPE_CHECKING: from .configuration_swin import SWIN_PRETRAINED_CONFIG_ARCHIVE_MAP, SwinConfig, SwinOnnxConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_swin import ( SWIN_PRETRAINED_MODEL_ARCHIVE_LIST, SwinBackbone, SwinForImageClassification, SwinForMaskedImageModeling, SwinModel, SwinPreTrainedModel, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_swin import ( TF_SWIN_PRETRAINED_MODEL_ARCHIVE_LIST, TFSwinForImageClassification, TFSwinForMaskedImageModeling, TFSwinModel, TFSwinPreTrainedModel, ) else: import sys A : Tuple = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
305
0
"""simple docstring""" import os import unittest from transformers.models.phobert.tokenization_phobert import VOCAB_FILES_NAMES, PhobertTokenizer from ...test_tokenization_common import TokenizerTesterMixin class lowerCamelCase (__snake_case , unittest.TestCase ): """simple docstring""" lowerCamelCase__ = PhobertTokenizer lowerCamelCase__ = False def __A ( self : Tuple ) -> Any: super().setUp() # Adapted from Sennrich et al. 2015 and https://github.com/rsennrich/subword-nmt SCREAMING_SNAKE_CASE_ = ["""T@@""", """i""", """I""", """R@@""", """r""", """e@@"""] SCREAMING_SNAKE_CASE_ = dict(zip(lowerCamelCase_ , range(len(lowerCamelCase_ ) ) ) ) SCREAMING_SNAKE_CASE_ = ["""#version: 0.2""", """l à</w>"""] SCREAMING_SNAKE_CASE_ = {"""unk_token""": """<unk>"""} SCREAMING_SNAKE_CASE_ = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES["vocab_file"] ) SCREAMING_SNAKE_CASE_ = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES["merges_file"] ) with open(self.vocab_file , "w" , encoding="utf-8" ) as fp: for token in vocab_tokens: fp.write(F'''{token} {vocab_tokens[token]}\n''' ) with open(self.merges_file , "w" , encoding="utf-8" ) as fp: fp.write("\n".join(lowerCamelCase_ ) ) def __A ( self : Optional[Any] , **__magic_name__ : Optional[Any] ) -> List[Any]: kwargs.update(self.special_tokens_map ) return PhobertTokenizer.from_pretrained(self.tmpdirname , **lowerCamelCase_ ) def __A ( self : str , __magic_name__ : Union[str, Any] ) -> Dict: SCREAMING_SNAKE_CASE_ = """Tôi là VinAI Research""" SCREAMING_SNAKE_CASE_ = """T<unk> i <unk> <unk> <unk> <unk> <unk> <unk> I Re<unk> e<unk> <unk> <unk> <unk>""" return input_text, output_text def __A ( self : Union[str, Any] ) -> int: SCREAMING_SNAKE_CASE_ = PhobertTokenizer(self.vocab_file , self.merges_file , **self.special_tokens_map ) SCREAMING_SNAKE_CASE_ = """Tôi là VinAI Research""" SCREAMING_SNAKE_CASE_ = """T@@ ô@@ i l@@ à V@@ i@@ n@@ A@@ I R@@ e@@ s@@ e@@ a@@ r@@ c@@ h""".split() SCREAMING_SNAKE_CASE_ = tokenizer.tokenize(lowerCamelCase_ ) print(lowerCamelCase_ ) self.assertListEqual(lowerCamelCase_ , lowerCamelCase_ ) SCREAMING_SNAKE_CASE_ = tokens + [tokenizer.unk_token] SCREAMING_SNAKE_CASE_ = [4, 3, 5, 3, 3, 3, 3, 3, 3, 6, 7, 9, 3, 9, 3, 3, 3, 3, 3] self.assertListEqual(tokenizer.convert_tokens_to_ids(lowerCamelCase_ ) , lowerCamelCase_ )
365
import tempfile import unittest import numpy as np from huggingface_hub import HfFolder, delete_repo from requests.exceptions import HTTPError from transformers import BertConfig, is_flax_available from transformers.testing_utils import TOKEN, USER, is_staging_test, require_flax if is_flax_available(): import os from flax.core.frozen_dict import unfreeze from flax.traverse_util import flatten_dict from transformers import FlaxBertModel A : Union[str, Any] = "0.12" # assumed parallelism: 8 @require_flax @is_staging_test class lowerCamelCase (unittest.TestCase ): """simple docstring""" @classmethod def __A ( cls : Any ) -> Dict: SCREAMING_SNAKE_CASE_ = TOKEN HfFolder.save_token(__magic_name__ ) @classmethod def __A ( cls : Optional[int] ) -> Tuple: try: delete_repo(token=cls._token , repo_id="test-model-flax" ) except HTTPError: pass try: delete_repo(token=cls._token , repo_id="valid_org/test-model-flax-org" ) except HTTPError: pass def __A ( self : str ) -> str: SCREAMING_SNAKE_CASE_ = BertConfig( vocab_size=99 , hidden_size=32 , num_hidden_layers=5 , num_attention_heads=4 , intermediate_size=37 ) SCREAMING_SNAKE_CASE_ = FlaxBertModel(__magic_name__ ) model.push_to_hub("test-model-flax" , use_auth_token=self._token ) SCREAMING_SNAKE_CASE_ = FlaxBertModel.from_pretrained(F'''{USER}/test-model-flax''' ) SCREAMING_SNAKE_CASE_ = flatten_dict(unfreeze(model.params ) ) SCREAMING_SNAKE_CASE_ = flatten_dict(unfreeze(new_model.params ) ) for key in base_params.keys(): SCREAMING_SNAKE_CASE_ = (base_params[key] - new_params[key]).sum().item() self.assertLessEqual(__magic_name__ , 1e-3 , msg=F'''{key} not identical''' ) # Reset repo delete_repo(token=self._token , repo_id="test-model-flax" ) # Push to hub via save_pretrained with tempfile.TemporaryDirectory() as tmp_dir: model.save_pretrained(__magic_name__ , repo_id="test-model-flax" , push_to_hub=__magic_name__ , use_auth_token=self._token ) SCREAMING_SNAKE_CASE_ = FlaxBertModel.from_pretrained(F'''{USER}/test-model-flax''' ) SCREAMING_SNAKE_CASE_ = flatten_dict(unfreeze(model.params ) ) SCREAMING_SNAKE_CASE_ = flatten_dict(unfreeze(new_model.params ) ) for key in base_params.keys(): SCREAMING_SNAKE_CASE_ = (base_params[key] - new_params[key]).sum().item() self.assertLessEqual(__magic_name__ , 1e-3 , msg=F'''{key} not identical''' ) def __A ( self : int ) -> Tuple: SCREAMING_SNAKE_CASE_ = BertConfig( vocab_size=99 , hidden_size=32 , num_hidden_layers=5 , num_attention_heads=4 , intermediate_size=37 ) SCREAMING_SNAKE_CASE_ = FlaxBertModel(__magic_name__ ) model.push_to_hub("valid_org/test-model-flax-org" , use_auth_token=self._token ) SCREAMING_SNAKE_CASE_ = FlaxBertModel.from_pretrained("valid_org/test-model-flax-org" ) SCREAMING_SNAKE_CASE_ = flatten_dict(unfreeze(model.params ) ) SCREAMING_SNAKE_CASE_ = flatten_dict(unfreeze(new_model.params ) ) for key in base_params.keys(): SCREAMING_SNAKE_CASE_ = (base_params[key] - new_params[key]).sum().item() self.assertLessEqual(__magic_name__ , 1e-3 , msg=F'''{key} not identical''' ) # Reset repo delete_repo(token=self._token , repo_id="valid_org/test-model-flax-org" ) # Push to hub via save_pretrained with tempfile.TemporaryDirectory() as tmp_dir: model.save_pretrained( __magic_name__ , repo_id="valid_org/test-model-flax-org" , push_to_hub=__magic_name__ , use_auth_token=self._token ) SCREAMING_SNAKE_CASE_ = FlaxBertModel.from_pretrained("valid_org/test-model-flax-org" ) SCREAMING_SNAKE_CASE_ = flatten_dict(unfreeze(model.params ) ) SCREAMING_SNAKE_CASE_ = flatten_dict(unfreeze(new_model.params ) ) for key in base_params.keys(): SCREAMING_SNAKE_CASE_ = (base_params[key] - new_params[key]).sum().item() self.assertLessEqual(__magic_name__ , 1e-3 , msg=F'''{key} not identical''' ) def a__ ( __UpperCamelCase , __UpperCamelCase ): SCREAMING_SNAKE_CASE_ = True SCREAMING_SNAKE_CASE_ = flatten_dict(modela.params ) SCREAMING_SNAKE_CASE_ = flatten_dict(modela.params ) for key in flat_params_a.keys(): if np.sum(np.abs(flat_params_a[key] - flat_params_a[key] ) ) > 1E-4: SCREAMING_SNAKE_CASE_ = False return models_are_equal @require_flax class lowerCamelCase (unittest.TestCase ): """simple docstring""" def __A ( self : str ) -> Dict: SCREAMING_SNAKE_CASE_ = BertConfig.from_pretrained("hf-internal-testing/tiny-bert-flax-only" ) SCREAMING_SNAKE_CASE_ = FlaxBertModel(__magic_name__ ) SCREAMING_SNAKE_CASE_ = "bert" with tempfile.TemporaryDirectory() as tmp_dir: model.save_pretrained(os.path.join(__magic_name__ , __magic_name__ ) ) with self.assertRaises(__magic_name__ ): SCREAMING_SNAKE_CASE_ = FlaxBertModel.from_pretrained(__magic_name__ ) SCREAMING_SNAKE_CASE_ = FlaxBertModel.from_pretrained(__magic_name__ , subfolder=__magic_name__ ) self.assertTrue(check_models_equal(__magic_name__ , __magic_name__ ) ) def __A ( self : Optional[Any] ) -> Tuple: SCREAMING_SNAKE_CASE_ = BertConfig.from_pretrained("hf-internal-testing/tiny-bert-flax-only" ) SCREAMING_SNAKE_CASE_ = FlaxBertModel(__magic_name__ ) SCREAMING_SNAKE_CASE_ = "bert" with tempfile.TemporaryDirectory() as tmp_dir: model.save_pretrained(os.path.join(__magic_name__ , __magic_name__ ) , max_shard_size="10KB" ) with self.assertRaises(__magic_name__ ): SCREAMING_SNAKE_CASE_ = FlaxBertModel.from_pretrained(__magic_name__ ) SCREAMING_SNAKE_CASE_ = FlaxBertModel.from_pretrained(__magic_name__ , subfolder=__magic_name__ ) self.assertTrue(check_models_equal(__magic_name__ , __magic_name__ ) ) def __A ( self : Optional[int] ) -> Dict: SCREAMING_SNAKE_CASE_ = "bert" SCREAMING_SNAKE_CASE_ = "hf-internal-testing/tiny-random-bert-subfolder" with self.assertRaises(__magic_name__ ): SCREAMING_SNAKE_CASE_ = FlaxBertModel.from_pretrained(__magic_name__ ) SCREAMING_SNAKE_CASE_ = FlaxBertModel.from_pretrained(__magic_name__ , subfolder=__magic_name__ ) self.assertIsNotNone(__magic_name__ ) def __A ( self : List[str] ) -> Dict: SCREAMING_SNAKE_CASE_ = "bert" SCREAMING_SNAKE_CASE_ = "hf-internal-testing/tiny-random-bert-sharded-subfolder" with self.assertRaises(__magic_name__ ): SCREAMING_SNAKE_CASE_ = FlaxBertModel.from_pretrained(__magic_name__ ) SCREAMING_SNAKE_CASE_ = FlaxBertModel.from_pretrained(__magic_name__ , subfolder=__magic_name__ ) self.assertIsNotNone(__magic_name__ )
305
0
import unittest import numpy as np from transformers import is_flax_available from transformers.testing_utils import require_flax from ..test_modeling_flax_common import ids_tensor if is_flax_available(): import jax import jax.numpy as jnp from transformers.generation import ( FlaxForcedBOSTokenLogitsProcessor, FlaxForcedEOSTokenLogitsProcessor, FlaxLogitsProcessorList, FlaxMinLengthLogitsProcessor, FlaxTemperatureLogitsWarper, FlaxTopKLogitsWarper, FlaxTopPLogitsWarper, ) @require_flax class lowerCamelCase (unittest.TestCase ): """simple docstring""" def __A ( self : List[str] , __magic_name__ : Dict , __magic_name__ : List[str] ) -> Union[str, Any]: SCREAMING_SNAKE_CASE_ = jnp.ones((batch_size, length) ) / length return scores def __A ( self : Optional[Any] ) -> Union[str, Any]: SCREAMING_SNAKE_CASE_ = None SCREAMING_SNAKE_CASE_ = 20 SCREAMING_SNAKE_CASE_ = self._get_uniform_logits(batch_size=2 , length=_lowercase ) # tweak scores to not be uniform anymore SCREAMING_SNAKE_CASE_ = scores.at[1, 5].set((1 / length) + 0.1 ) # peak, 1st batch SCREAMING_SNAKE_CASE_ = scores.at[1, 10].set((1 / length) - 0.4 ) # valley, 1st batch # compute softmax SCREAMING_SNAKE_CASE_ = jax.nn.softmax(_lowercase , axis=-1 ) SCREAMING_SNAKE_CASE_ = FlaxTemperatureLogitsWarper(temperature=0.5 ) SCREAMING_SNAKE_CASE_ = FlaxTemperatureLogitsWarper(temperature=1.3 ) SCREAMING_SNAKE_CASE_ = jax.nn.softmax(temp_dist_warper_sharper(_lowercase , scores.copy() , cur_len=_lowercase ) , axis=-1 ) SCREAMING_SNAKE_CASE_ = jax.nn.softmax(temp_dist_warper_smoother(_lowercase , scores.copy() , cur_len=_lowercase ) , axis=-1 ) # uniform distribution stays uniform self.assertTrue(jnp.allclose(probs[0, :] , warped_prob_sharp[0, :] , atol=1e-3 ) ) self.assertTrue(jnp.allclose(probs[0, :] , warped_prob_smooth[0, :] , atol=1e-3 ) ) # sharp peaks get higher, valleys get lower self.assertLess(probs[1, :].max() , warped_prob_sharp[1, :].max() ) self.assertGreater(probs[1, :].min() , warped_prob_sharp[1, :].min() ) # smooth peaks get lower, valleys get higher self.assertGreater(probs[1, :].max() , warped_prob_smooth[1, :].max() ) self.assertLess(probs[1, :].min() , warped_prob_smooth[1, :].min() ) def __A ( self : str ) -> Tuple: SCREAMING_SNAKE_CASE_ = None SCREAMING_SNAKE_CASE_ = 10 SCREAMING_SNAKE_CASE_ = 2 # create ramp distribution SCREAMING_SNAKE_CASE_ = np.broadcast_to(np.arange(_lowercase )[None, :] , (batch_size, vocab_size) ).copy() SCREAMING_SNAKE_CASE_ = ramp_logits[1:, : vocab_size // 2] + vocab_size SCREAMING_SNAKE_CASE_ = FlaxTopKLogitsWarper(3 ) SCREAMING_SNAKE_CASE_ = top_k_warp(_lowercase , _lowercase , cur_len=_lowercase ) # check that correct tokens are filtered self.assertListEqual(jnp.isinf(scores[0] ).tolist() , 7 * [True] + 3 * [False] ) self.assertListEqual(jnp.isinf(scores[1] ).tolist() , 2 * [True] + 3 * [False] + 5 * [True] ) # check special case SCREAMING_SNAKE_CASE_ = 5 SCREAMING_SNAKE_CASE_ = FlaxTopKLogitsWarper(top_k=1 , filter_value=0.0 , min_tokens_to_keep=3 ) SCREAMING_SNAKE_CASE_ = np.broadcast_to(np.arange(_lowercase )[None, :] , (batch_size, length) ).copy() SCREAMING_SNAKE_CASE_ = top_k_warp_safety_check(_lowercase , _lowercase , cur_len=_lowercase ) # min_tokens overwrites k: 3 tokens are kept => 2 tokens are nullified self.assertListEqual((scores == 0.0).sum(axis=-1 ).tolist() , [2, 2] ) def __A ( self : Dict ) -> Optional[int]: SCREAMING_SNAKE_CASE_ = None SCREAMING_SNAKE_CASE_ = 10 SCREAMING_SNAKE_CASE_ = 2 # create distribution and take log (inverse to Softmax as taken in TopPLogitsWarper) SCREAMING_SNAKE_CASE_ = np.log(np.array([[0.3, 0.1, 0.1, 0.5], [0.15, 0.3, 0.3, 0.25]] ) ) SCREAMING_SNAKE_CASE_ = FlaxTopPLogitsWarper(0.8 ) SCREAMING_SNAKE_CASE_ = np.exp(top_p_warp(_lowercase , _lowercase , cur_len=_lowercase ) ) # dist should be filtered to keep min num values so that sum is >= top_p # exp (-inf) => 0 SCREAMING_SNAKE_CASE_ = np.array([[0.3, 0.0, 0.0, 0.5], [0.0, 0.3, 0.3, 0.25]] ) self.assertTrue(np.allclose(_lowercase , _lowercase , atol=1e-3 ) ) # check edge cases with negative and extreme logits SCREAMING_SNAKE_CASE_ = np.broadcast_to(np.arange(_lowercase )[None, :] , (batch_size, vocab_size) ).copy() - ( vocab_size // 2 ) # make ramp_logits more extreme SCREAMING_SNAKE_CASE_ = ramp_logits[1] * 100.0 # make sure at least 2 tokens are kept SCREAMING_SNAKE_CASE_ = FlaxTopPLogitsWarper(0.9 , min_tokens_to_keep=2 , filter_value=0.0 ) SCREAMING_SNAKE_CASE_ = top_p_warp(_lowercase , _lowercase , cur_len=_lowercase ) # first batch should keep three tokens, second batch would keep only 1, but due to `min_tokens_to_keep=2` keeps 2. self.assertListEqual((filtered_dist != 0.0).sum(axis=-1 ).tolist() , [3, 2] ) def __A ( self : Any ) -> Optional[Any]: SCREAMING_SNAKE_CASE_ = 20 SCREAMING_SNAKE_CASE_ = 4 SCREAMING_SNAKE_CASE_ = 0 SCREAMING_SNAKE_CASE_ = FlaxMinLengthLogitsProcessor(min_length=10 , eos_token_id=_lowercase ) # check that min length is applied at length 5 SCREAMING_SNAKE_CASE_ = ids_tensor((batch_size, 20) , vocab_size=20 ) SCREAMING_SNAKE_CASE_ = 5 SCREAMING_SNAKE_CASE_ = self._get_uniform_logits(_lowercase , _lowercase ) SCREAMING_SNAKE_CASE_ = min_dist_processor(_lowercase , _lowercase , cur_len=_lowercase ) self.assertListEqual(scores_before_min_length[:, eos_token_id].tolist() , 4 * [-float("inf" )] ) # check that min length is not applied anymore at length 15 SCREAMING_SNAKE_CASE_ = self._get_uniform_logits(_lowercase , _lowercase ) SCREAMING_SNAKE_CASE_ = 15 SCREAMING_SNAKE_CASE_ = min_dist_processor(_lowercase , _lowercase , cur_len=_lowercase ) self.assertFalse(jnp.isinf(_lowercase ).any() ) def __A ( self : Dict ) -> Optional[Any]: SCREAMING_SNAKE_CASE_ = 20 SCREAMING_SNAKE_CASE_ = 4 SCREAMING_SNAKE_CASE_ = 0 SCREAMING_SNAKE_CASE_ = FlaxForcedBOSTokenLogitsProcessor(bos_token_id=_lowercase ) # check that all scores are -inf except the bos_token_id score SCREAMING_SNAKE_CASE_ = ids_tensor((batch_size, 1) , vocab_size=20 ) SCREAMING_SNAKE_CASE_ = 1 SCREAMING_SNAKE_CASE_ = self._get_uniform_logits(_lowercase , _lowercase ) SCREAMING_SNAKE_CASE_ = logits_processor(_lowercase , _lowercase , cur_len=_lowercase ) self.assertTrue(jnp.isneginf(scores[:, bos_token_id + 1 :] ).all() ) self.assertListEqual(scores[:, bos_token_id].tolist() , 4 * [0] ) # score for bos_token_id shold be zero # check that bos_token_id is not forced if current length is greater than 1 SCREAMING_SNAKE_CASE_ = 3 SCREAMING_SNAKE_CASE_ = self._get_uniform_logits(_lowercase , _lowercase ) SCREAMING_SNAKE_CASE_ = logits_processor(_lowercase , _lowercase , cur_len=_lowercase ) self.assertFalse(jnp.isinf(_lowercase ).any() ) def __A ( self : List[str] ) -> List[str]: SCREAMING_SNAKE_CASE_ = 20 SCREAMING_SNAKE_CASE_ = 4 SCREAMING_SNAKE_CASE_ = 0 SCREAMING_SNAKE_CASE_ = 5 SCREAMING_SNAKE_CASE_ = FlaxForcedEOSTokenLogitsProcessor(max_length=_lowercase , eos_token_id=_lowercase ) # check that all scores are -inf except the eos_token_id when max_length is reached SCREAMING_SNAKE_CASE_ = ids_tensor((batch_size, 4) , vocab_size=20 ) SCREAMING_SNAKE_CASE_ = 4 SCREAMING_SNAKE_CASE_ = self._get_uniform_logits(_lowercase , _lowercase ) SCREAMING_SNAKE_CASE_ = logits_processor(_lowercase , _lowercase , cur_len=_lowercase ) self.assertTrue(jnp.isneginf(scores[:, eos_token_id + 1 :] ).all() ) self.assertListEqual(scores[:, eos_token_id].tolist() , 4 * [0] ) # score for eos_token_id should be zero # check that eos_token_id is not forced if max_length is not reached SCREAMING_SNAKE_CASE_ = 3 SCREAMING_SNAKE_CASE_ = self._get_uniform_logits(_lowercase , _lowercase ) SCREAMING_SNAKE_CASE_ = logits_processor(_lowercase , _lowercase , cur_len=_lowercase ) self.assertFalse(jnp.isinf(_lowercase ).any() ) def __A ( self : Optional[Any] ) -> Tuple: SCREAMING_SNAKE_CASE_ = 4 SCREAMING_SNAKE_CASE_ = 10 SCREAMING_SNAKE_CASE_ = 15 SCREAMING_SNAKE_CASE_ = 2 SCREAMING_SNAKE_CASE_ = 1 SCREAMING_SNAKE_CASE_ = 15 # dummy input_ids and scores SCREAMING_SNAKE_CASE_ = ids_tensor((batch_size, sequence_length) , _lowercase ) SCREAMING_SNAKE_CASE_ = input_ids.copy() SCREAMING_SNAKE_CASE_ = self._get_uniform_logits(_lowercase , _lowercase ) SCREAMING_SNAKE_CASE_ = scores.copy() # instantiate all dist processors SCREAMING_SNAKE_CASE_ = FlaxTemperatureLogitsWarper(temperature=0.5 ) SCREAMING_SNAKE_CASE_ = FlaxTopKLogitsWarper(3 ) SCREAMING_SNAKE_CASE_ = FlaxTopPLogitsWarper(0.8 ) # instantiate all logits processors SCREAMING_SNAKE_CASE_ = FlaxMinLengthLogitsProcessor(min_length=10 , eos_token_id=_lowercase ) SCREAMING_SNAKE_CASE_ = FlaxForcedBOSTokenLogitsProcessor(bos_token_id=_lowercase ) SCREAMING_SNAKE_CASE_ = FlaxForcedEOSTokenLogitsProcessor(max_length=_lowercase , eos_token_id=_lowercase ) SCREAMING_SNAKE_CASE_ = 10 # no processor list SCREAMING_SNAKE_CASE_ = temp_dist_warp(_lowercase , _lowercase , cur_len=_lowercase ) SCREAMING_SNAKE_CASE_ = top_k_warp(_lowercase , _lowercase , cur_len=_lowercase ) SCREAMING_SNAKE_CASE_ = top_p_warp(_lowercase , _lowercase , cur_len=_lowercase ) SCREAMING_SNAKE_CASE_ = min_dist_proc(_lowercase , _lowercase , cur_len=_lowercase ) SCREAMING_SNAKE_CASE_ = bos_dist_proc(_lowercase , _lowercase , cur_len=_lowercase ) SCREAMING_SNAKE_CASE_ = eos_dist_proc(_lowercase , _lowercase , cur_len=_lowercase ) # with processor list SCREAMING_SNAKE_CASE_ = FlaxLogitsProcessorList( [temp_dist_warp, top_k_warp, top_p_warp, min_dist_proc, bos_dist_proc, eos_dist_proc] ) SCREAMING_SNAKE_CASE_ = processor(_lowercase , _lowercase , cur_len=_lowercase ) # scores should be equal self.assertTrue(jnp.allclose(_lowercase , _lowercase , atol=1e-3 ) ) # input_ids should never be changed self.assertListEqual(input_ids.tolist() , input_ids_comp.tolist() ) def __A ( self : Optional[Any] ) -> Dict: SCREAMING_SNAKE_CASE_ = 4 SCREAMING_SNAKE_CASE_ = 10 SCREAMING_SNAKE_CASE_ = 15 SCREAMING_SNAKE_CASE_ = 2 SCREAMING_SNAKE_CASE_ = 1 SCREAMING_SNAKE_CASE_ = 15 # dummy input_ids and scores SCREAMING_SNAKE_CASE_ = ids_tensor((batch_size, sequence_length) , _lowercase ) SCREAMING_SNAKE_CASE_ = input_ids.copy() SCREAMING_SNAKE_CASE_ = self._get_uniform_logits(_lowercase , _lowercase ) SCREAMING_SNAKE_CASE_ = scores.copy() # instantiate all dist processors SCREAMING_SNAKE_CASE_ = FlaxTemperatureLogitsWarper(temperature=0.5 ) SCREAMING_SNAKE_CASE_ = FlaxTopKLogitsWarper(3 ) SCREAMING_SNAKE_CASE_ = FlaxTopPLogitsWarper(0.8 ) # instantiate all logits processors SCREAMING_SNAKE_CASE_ = FlaxMinLengthLogitsProcessor(min_length=10 , eos_token_id=_lowercase ) SCREAMING_SNAKE_CASE_ = FlaxForcedBOSTokenLogitsProcessor(bos_token_id=_lowercase ) SCREAMING_SNAKE_CASE_ = FlaxForcedEOSTokenLogitsProcessor(max_length=_lowercase , eos_token_id=_lowercase ) SCREAMING_SNAKE_CASE_ = 10 # no processor list def run_no_processor_list(__magic_name__ : Tuple , __magic_name__ : Optional[Any] , __magic_name__ : Optional[int] ): SCREAMING_SNAKE_CASE_ = temp_dist_warp(_lowercase , _lowercase , cur_len=_lowercase ) SCREAMING_SNAKE_CASE_ = top_k_warp(_lowercase , _lowercase , cur_len=_lowercase ) SCREAMING_SNAKE_CASE_ = top_p_warp(_lowercase , _lowercase , cur_len=_lowercase ) SCREAMING_SNAKE_CASE_ = min_dist_proc(_lowercase , _lowercase , cur_len=_lowercase ) SCREAMING_SNAKE_CASE_ = bos_dist_proc(_lowercase , _lowercase , cur_len=_lowercase ) SCREAMING_SNAKE_CASE_ = eos_dist_proc(_lowercase , _lowercase , cur_len=_lowercase ) return scores # with processor list def run_processor_list(__magic_name__ : int , __magic_name__ : List[Any] , __magic_name__ : Dict ): SCREAMING_SNAKE_CASE_ = FlaxLogitsProcessorList( [temp_dist_warp, top_k_warp, top_p_warp, min_dist_proc, bos_dist_proc, eos_dist_proc] ) SCREAMING_SNAKE_CASE_ = processor(_lowercase , _lowercase , cur_len=_lowercase ) return scores SCREAMING_SNAKE_CASE_ = jax.jit(_lowercase ) SCREAMING_SNAKE_CASE_ = jax.jit(_lowercase ) SCREAMING_SNAKE_CASE_ = jitted_run_no_processor_list(_lowercase , _lowercase , _lowercase ) SCREAMING_SNAKE_CASE_ = jitted_run_processor_list(_lowercase , _lowercase , _lowercase ) # scores should be equal self.assertTrue(jnp.allclose(_lowercase , _lowercase , atol=1e-3 ) ) # input_ids should never be changed self.assertListEqual(input_ids.tolist() , input_ids_comp.tolist() )
366
# Copyright 2021 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. import argparse import os from accelerate.utils import ComputeEnvironment from .cluster import get_cluster_input from .config_args import cache_dir, default_config_file, default_yaml_config_file, load_config_from_file # noqa: F401 from .config_utils import _ask_field, _ask_options, _convert_compute_environment # noqa: F401 from .sagemaker import get_sagemaker_input A : Union[str, Any] = "Launches a series of prompts to create and save a `default_config.yaml` configuration file for your training system. Should always be ran first on your machine" def a__ ( ): SCREAMING_SNAKE_CASE_ = _ask_options( "In which compute environment are you running?" , ["This machine", "AWS (Amazon SageMaker)"] , _convert_compute_environment , ) if compute_environment == ComputeEnvironment.AMAZON_SAGEMAKER: SCREAMING_SNAKE_CASE_ = get_sagemaker_input() else: SCREAMING_SNAKE_CASE_ = get_cluster_input() return config def a__ ( __UpperCamelCase=None ): if subparsers is not None: SCREAMING_SNAKE_CASE_ = subparsers.add_parser("config" , description=__UpperCamelCase ) else: SCREAMING_SNAKE_CASE_ = argparse.ArgumentParser("Accelerate config command" , description=__UpperCamelCase ) parser.add_argument( "--config_file" , default=__UpperCamelCase , help=( "The path to use to store the config file. Will default to a file named default_config.yaml in the cache " "location, which is the content of the environment `HF_HOME` suffixed with 'accelerate', or if you don't have " "such an environment variable, your cache directory ('~/.cache' or the content of `XDG_CACHE_HOME`) suffixed " "with 'huggingface'." ) , ) if subparsers is not None: parser.set_defaults(func=__UpperCamelCase ) return parser def a__ ( __UpperCamelCase ): SCREAMING_SNAKE_CASE_ = get_user_input() if args.config_file is not None: SCREAMING_SNAKE_CASE_ = args.config_file else: if not os.path.isdir(__UpperCamelCase ): os.makedirs(__UpperCamelCase ) SCREAMING_SNAKE_CASE_ = default_yaml_config_file if config_file.endswith(".json" ): config.to_json_file(__UpperCamelCase ) else: config.to_yaml_file(__UpperCamelCase ) print(F'''accelerate configuration saved at {config_file}''' ) def a__ ( ): SCREAMING_SNAKE_CASE_ = config_command_parser() SCREAMING_SNAKE_CASE_ = parser.parse_args() config_command(__UpperCamelCase ) if __name__ == "__main__": main()
305
0
from __future__ import annotations class lowerCamelCase : """simple docstring""" def __init__( self : Dict , __magic_name__ : int = 0 ) -> str: SCREAMING_SNAKE_CASE_ = key def __A ( self : List[str] , __magic_name__ : str , __magic_name__ : int ) -> Any: assert isinstance(_UpperCAmelCase , _UpperCAmelCase ) and isinstance(_UpperCAmelCase , _UpperCAmelCase ) SCREAMING_SNAKE_CASE_ = key or self.__key or 1 # make sure key is an appropriate size key %= 255 return [chr(ord(_UpperCAmelCase ) ^ key ) for ch in content] def __A ( self : Union[str, Any] , __magic_name__ : str , __magic_name__ : int ) -> Any: assert isinstance(_UpperCAmelCase , _UpperCAmelCase ) and isinstance(_UpperCAmelCase , _UpperCAmelCase ) SCREAMING_SNAKE_CASE_ = key or self.__key or 1 # make sure key is an appropriate size key %= 255 return [chr(ord(_UpperCAmelCase ) ^ key ) for ch in content] def __A ( self : Any , __magic_name__ : str , __magic_name__ : int = 0 ) -> Optional[Any]: assert isinstance(_UpperCAmelCase , _UpperCAmelCase ) and isinstance(_UpperCAmelCase , _UpperCAmelCase ) SCREAMING_SNAKE_CASE_ = key or self.__key or 1 # make sure key can be any size while key > 255: key -= 255 # This will be returned SCREAMING_SNAKE_CASE_ = "" for ch in content: ans += chr(ord(_UpperCAmelCase ) ^ key ) return ans def __A ( self : Union[str, Any] , __magic_name__ : str , __magic_name__ : int = 0 ) -> str: assert isinstance(_UpperCAmelCase , _UpperCAmelCase ) and isinstance(_UpperCAmelCase , _UpperCAmelCase ) SCREAMING_SNAKE_CASE_ = key or self.__key or 1 # make sure key can be any size while key > 255: key -= 255 # This will be returned SCREAMING_SNAKE_CASE_ = "" for ch in content: ans += chr(ord(_UpperCAmelCase ) ^ key ) return ans def __A ( self : Any , __magic_name__ : str , __magic_name__ : int = 0 ) -> Any: assert isinstance(_UpperCAmelCase , _UpperCAmelCase ) and isinstance(_UpperCAmelCase , _UpperCAmelCase ) try: with open(_UpperCAmelCase ) as fin, open("encrypt.out" , "w+" ) as fout: # actual encrypt-process for line in fin: fout.write(self.encrypt_string(_UpperCAmelCase , _UpperCAmelCase ) ) except OSError: return False return True def __A ( self : Tuple , __magic_name__ : str , __magic_name__ : int ) -> Union[str, Any]: assert isinstance(_UpperCAmelCase , _UpperCAmelCase ) and isinstance(_UpperCAmelCase , _UpperCAmelCase ) try: with open(_UpperCAmelCase ) as fin, open("decrypt.out" , "w+" ) as fout: # actual encrypt-process for line in fin: fout.write(self.decrypt_string(_UpperCAmelCase , _UpperCAmelCase ) ) except OSError: return False return True # Tests # crypt = XORCipher() # key = 67 # # test encrypt # print(crypt.encrypt("hallo welt",key)) # # test decrypt # print(crypt.decrypt(crypt.encrypt("hallo welt",key), key)) # # test encrypt_string # print(crypt.encrypt_string("hallo welt",key)) # # test decrypt_string # print(crypt.decrypt_string(crypt.encrypt_string("hallo welt",key),key)) # if (crypt.encrypt_file("test.txt",key)): # print("encrypt successful") # else: # print("encrypt unsuccessful") # if (crypt.decrypt_file("encrypt.out",key)): # print("decrypt successful") # else: # print("decrypt unsuccessful")
367
from dataclasses import dataclass, field from typing import ClassVar, Dict from ..features import Features, Value from .base import TaskTemplate @dataclass(frozen=SCREAMING_SNAKE_CASE__ ) class lowerCamelCase (SCREAMING_SNAKE_CASE__ ): """simple docstring""" lowerCamelCase__ = field(default='''summarization''' , metadata={'''include_in_asdict_even_if_is_default''': True} ) lowerCamelCase__ = Features({'''text''': Value('''string''' )} ) lowerCamelCase__ = Features({'''summary''': Value('''string''' )} ) lowerCamelCase__ = "text" lowerCamelCase__ = "summary" @property def __A ( self : Dict ) -> Dict[str, str]: return {self.text_column: "text", self.summary_column: "summary"}
305
0
import inspect import unittest import warnings from transformers import DeiTConfig from transformers.models.auto import get_values from transformers.testing_utils import ( require_accelerate, require_torch, require_torch_gpu, require_vision, slow, torch_device, ) from transformers.utils import cached_property, is_torch_available, is_vision_available from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from torch import nn from transformers import ( MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING, MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING, MODEL_MAPPING, DeiTForImageClassification, DeiTForImageClassificationWithTeacher, DeiTForMaskedImageModeling, DeiTModel, ) from transformers.models.deit.modeling_deit import DEIT_PRETRAINED_MODEL_ARCHIVE_LIST if is_vision_available(): from PIL import Image from transformers import DeiTImageProcessor class lowerCamelCase : """simple docstring""" def __init__( self : List[str] , __magic_name__ : Optional[int] , __magic_name__ : int=13 , __magic_name__ : str=30 , __magic_name__ : Tuple=2 , __magic_name__ : Tuple=3 , __magic_name__ : Optional[Any]=True , __magic_name__ : List[str]=True , __magic_name__ : Dict=32 , __magic_name__ : List[Any]=5 , __magic_name__ : Optional[int]=4 , __magic_name__ : List[Any]=37 , __magic_name__ : Any="gelu" , __magic_name__ : List[str]=0.1 , __magic_name__ : Union[str, Any]=0.1 , __magic_name__ : Optional[Any]=10 , __magic_name__ : List[Any]=0.02 , __magic_name__ : Optional[int]=3 , __magic_name__ : List[str]=None , __magic_name__ : Any=2 , ) -> List[str]: SCREAMING_SNAKE_CASE_ = parent SCREAMING_SNAKE_CASE_ = batch_size SCREAMING_SNAKE_CASE_ = image_size SCREAMING_SNAKE_CASE_ = patch_size SCREAMING_SNAKE_CASE_ = num_channels SCREAMING_SNAKE_CASE_ = is_training SCREAMING_SNAKE_CASE_ = use_labels 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_ = type_sequence_label_size SCREAMING_SNAKE_CASE_ = initializer_range SCREAMING_SNAKE_CASE_ = scope SCREAMING_SNAKE_CASE_ = encoder_stride # in DeiT, the seq length equals the number of patches + 2 (we add 2 for the [CLS] and distilation tokens) SCREAMING_SNAKE_CASE_ = (image_size // patch_size) ** 2 SCREAMING_SNAKE_CASE_ = num_patches + 2 def __A ( self : Tuple ) -> Dict: SCREAMING_SNAKE_CASE_ = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) SCREAMING_SNAKE_CASE_ = None if self.use_labels: SCREAMING_SNAKE_CASE_ = ids_tensor([self.batch_size] , self.type_sequence_label_size ) SCREAMING_SNAKE_CASE_ = self.get_config() return config, pixel_values, labels def __A ( self : Dict ) -> Union[str, Any]: return DeiTConfig( image_size=self.image_size , patch_size=self.patch_size , num_channels=self.num_channels , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , is_decoder=__lowerCamelCase , initializer_range=self.initializer_range , encoder_stride=self.encoder_stride , ) def __A ( self : int , __magic_name__ : List[str] , __magic_name__ : Optional[int] , __magic_name__ : Union[str, Any] ) -> int: SCREAMING_SNAKE_CASE_ = DeiTModel(config=__lowerCamelCase ) model.to(__lowerCamelCase ) model.eval() SCREAMING_SNAKE_CASE_ = model(__lowerCamelCase ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def __A ( self : Tuple , __magic_name__ : Union[str, Any] , __magic_name__ : Optional[Any] , __magic_name__ : Dict ) -> Union[str, Any]: SCREAMING_SNAKE_CASE_ = DeiTForMaskedImageModeling(config=__lowerCamelCase ) model.to(__lowerCamelCase ) model.eval() SCREAMING_SNAKE_CASE_ = model(__lowerCamelCase ) self.parent.assertEqual( result.reconstruction.shape , (self.batch_size, self.num_channels, self.image_size, self.image_size) ) # test greyscale images SCREAMING_SNAKE_CASE_ = 1 SCREAMING_SNAKE_CASE_ = DeiTForMaskedImageModeling(__lowerCamelCase ) model.to(__lowerCamelCase ) model.eval() SCREAMING_SNAKE_CASE_ = floats_tensor([self.batch_size, 1, self.image_size, self.image_size] ) SCREAMING_SNAKE_CASE_ = model(__lowerCamelCase ) self.parent.assertEqual(result.reconstruction.shape , (self.batch_size, 1, self.image_size, self.image_size) ) def __A ( self : Optional[int] , __magic_name__ : Tuple , __magic_name__ : Union[str, Any] , __magic_name__ : List[str] ) -> int: SCREAMING_SNAKE_CASE_ = self.type_sequence_label_size SCREAMING_SNAKE_CASE_ = DeiTForImageClassification(__lowerCamelCase ) model.to(__lowerCamelCase ) model.eval() SCREAMING_SNAKE_CASE_ = model(__lowerCamelCase , labels=__lowerCamelCase ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.type_sequence_label_size) ) # test greyscale images SCREAMING_SNAKE_CASE_ = 1 SCREAMING_SNAKE_CASE_ = DeiTForImageClassification(__lowerCamelCase ) model.to(__lowerCamelCase ) model.eval() SCREAMING_SNAKE_CASE_ = floats_tensor([self.batch_size, 1, self.image_size, self.image_size] ) SCREAMING_SNAKE_CASE_ = model(__lowerCamelCase , labels=__lowerCamelCase ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.type_sequence_label_size) ) def __A ( self : List[str] ) -> Tuple: SCREAMING_SNAKE_CASE_ = self.prepare_config_and_inputs() ( SCREAMING_SNAKE_CASE_ ) = config_and_inputs SCREAMING_SNAKE_CASE_ = {"""pixel_values""": pixel_values} return config, inputs_dict @require_torch class lowerCamelCase (_a , _a , unittest.TestCase ): """simple docstring""" lowerCamelCase__ = ( ( DeiTModel, DeiTForImageClassification, DeiTForImageClassificationWithTeacher, DeiTForMaskedImageModeling, ) if is_torch_available() else () ) lowerCamelCase__ = ( { """feature-extraction""": DeiTModel, """image-classification""": (DeiTForImageClassification, DeiTForImageClassificationWithTeacher), } if is_torch_available() else {} ) lowerCamelCase__ = False lowerCamelCase__ = False lowerCamelCase__ = False def __A ( self : Any ) -> Union[str, Any]: SCREAMING_SNAKE_CASE_ = DeiTModelTester(self ) SCREAMING_SNAKE_CASE_ = ConfigTester(self , config_class=__lowerCamelCase , has_text_modality=__lowerCamelCase , hidden_size=37 ) def __A ( self : str ) -> Union[str, Any]: self.config_tester.run_common_tests() @unittest.skip(reason="DeiT does not use inputs_embeds" ) def __A ( self : str ) -> List[Any]: pass def __A ( self : Any ) -> Optional[Any]: 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(__lowerCamelCase ) self.assertIsInstance(model.get_input_embeddings() , (nn.Module) ) SCREAMING_SNAKE_CASE_ = model.get_output_embeddings() self.assertTrue(x is None or isinstance(__lowerCamelCase , nn.Linear ) ) def __A ( self : Dict ) -> Optional[int]: 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(__lowerCamelCase ) SCREAMING_SNAKE_CASE_ = inspect.signature(model.forward ) # signature.parameters is an OrderedDict => so arg_names order is deterministic SCREAMING_SNAKE_CASE_ = [*signature.parameters.keys()] SCREAMING_SNAKE_CASE_ = ["""pixel_values"""] self.assertListEqual(arg_names[:1] , __lowerCamelCase ) def __A ( self : List[str] ) -> Dict: SCREAMING_SNAKE_CASE_ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*__lowerCamelCase ) def __A ( self : str ) -> Tuple: SCREAMING_SNAKE_CASE_ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_masked_image_modeling(*__lowerCamelCase ) def __A ( self : List[str] ) -> List[Any]: SCREAMING_SNAKE_CASE_ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_image_classification(*__lowerCamelCase ) def __A ( self : Union[str, Any] , __magic_name__ : Optional[int] , __magic_name__ : Optional[int] , __magic_name__ : Union[str, Any]=False ) -> Dict: SCREAMING_SNAKE_CASE_ = super()._prepare_for_class(__lowerCamelCase , __lowerCamelCase , return_labels=__lowerCamelCase ) if return_labels: if model_class.__name__ == "DeiTForImageClassificationWithTeacher": del inputs_dict["labels"] return inputs_dict def __A ( self : List[Any] ) -> Optional[int]: if not self.model_tester.is_training: return SCREAMING_SNAKE_CASE_ = self.model_tester.prepare_config_and_inputs_for_common() SCREAMING_SNAKE_CASE_ = True for model_class in self.all_model_classes: # DeiTForImageClassificationWithTeacher supports inference-only if ( model_class in get_values(__lowerCamelCase ) or model_class.__name__ == "DeiTForImageClassificationWithTeacher" ): continue SCREAMING_SNAKE_CASE_ = model_class(__lowerCamelCase ) model.to(__lowerCamelCase ) model.train() SCREAMING_SNAKE_CASE_ = self._prepare_for_class(__lowerCamelCase , __lowerCamelCase , return_labels=__lowerCamelCase ) SCREAMING_SNAKE_CASE_ = model(**__lowerCamelCase ).loss loss.backward() def __A ( self : Any ) -> Tuple: SCREAMING_SNAKE_CASE_ = self.model_tester.prepare_config_and_inputs_for_common() if not self.model_tester.is_training: return SCREAMING_SNAKE_CASE_ = False SCREAMING_SNAKE_CASE_ = True for model_class in self.all_model_classes: if model_class in get_values(__lowerCamelCase ) or not model_class.supports_gradient_checkpointing: continue # DeiTForImageClassificationWithTeacher supports inference-only if model_class.__name__ == "DeiTForImageClassificationWithTeacher": continue SCREAMING_SNAKE_CASE_ = model_class(__lowerCamelCase ) model.gradient_checkpointing_enable() model.to(__lowerCamelCase ) model.train() SCREAMING_SNAKE_CASE_ = self._prepare_for_class(__lowerCamelCase , __lowerCamelCase , return_labels=__lowerCamelCase ) SCREAMING_SNAKE_CASE_ = model(**__lowerCamelCase ).loss loss.backward() def __A ( self : Any ) -> Any: SCREAMING_SNAKE_CASE_ = self.model_tester.prepare_config_and_inputs_for_common() SCREAMING_SNAKE_CASE_ = [ {"""title""": """multi_label_classification""", """num_labels""": 2, """dtype""": torch.float}, {"""title""": """single_label_classification""", """num_labels""": 1, """dtype""": torch.long}, {"""title""": """regression""", """num_labels""": 1, """dtype""": torch.float}, ] for model_class in self.all_model_classes: if ( model_class not in [ *get_values(__lowerCamelCase ), *get_values(__lowerCamelCase ), ] or model_class.__name__ == "DeiTForImageClassificationWithTeacher" ): continue for problem_type in problem_types: with self.subTest(msg=F'''Testing {model_class} with {problem_type["title"]}''' ): SCREAMING_SNAKE_CASE_ = problem_type["""title"""] SCREAMING_SNAKE_CASE_ = problem_type["""num_labels"""] SCREAMING_SNAKE_CASE_ = model_class(__lowerCamelCase ) model.to(__lowerCamelCase ) model.train() SCREAMING_SNAKE_CASE_ = self._prepare_for_class(__lowerCamelCase , __lowerCamelCase , return_labels=__lowerCamelCase ) if problem_type["num_labels"] > 1: SCREAMING_SNAKE_CASE_ = inputs["""labels"""].unsqueeze(1 ).repeat(1 , problem_type["num_labels"] ) SCREAMING_SNAKE_CASE_ = inputs["""labels"""].to(problem_type["dtype"] ) # This tests that we do not trigger the warning form PyTorch "Using a target size that is different # to the input size. This will likely lead to incorrect results due to broadcasting. Please ensure # they have the same size." which is a symptom something in wrong for the regression problem. # See https://github.com/huggingface/transformers/issues/11780 with warnings.catch_warnings(record=__lowerCamelCase ) as warning_list: SCREAMING_SNAKE_CASE_ = model(**__lowerCamelCase ).loss for w in warning_list: if "Using a target size that is different to the input size" in str(w.message ): raise ValueError( F'''Something is going wrong in the regression problem: intercepted {w.message}''' ) loss.backward() @slow def __A ( self : Dict ) -> Tuple: for model_name in DEIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: SCREAMING_SNAKE_CASE_ = DeiTModel.from_pretrained(__lowerCamelCase ) self.assertIsNotNone(__lowerCamelCase ) def a__ ( ): SCREAMING_SNAKE_CASE_ = Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png" ) return image @require_torch @require_vision class lowerCamelCase (unittest.TestCase ): """simple docstring""" @cached_property def __A ( self : int ) -> int: return ( DeiTImageProcessor.from_pretrained("facebook/deit-base-distilled-patch16-224" ) if is_vision_available() else None ) @slow def __A ( self : Optional[Any] ) -> Any: SCREAMING_SNAKE_CASE_ = DeiTForImageClassificationWithTeacher.from_pretrained("facebook/deit-base-distilled-patch16-224" ).to( __lowerCamelCase ) SCREAMING_SNAKE_CASE_ = self.default_image_processor SCREAMING_SNAKE_CASE_ = prepare_img() SCREAMING_SNAKE_CASE_ = image_processor(images=__lowerCamelCase , return_tensors="pt" ).to(__lowerCamelCase ) # forward pass with torch.no_grad(): SCREAMING_SNAKE_CASE_ = model(**__lowerCamelCase ) # verify the logits SCREAMING_SNAKE_CASE_ = torch.Size((1, 1_000) ) self.assertEqual(outputs.logits.shape , __lowerCamelCase ) SCREAMING_SNAKE_CASE_ = torch.tensor([-1.0266, 0.1912, -1.2861] ).to(__lowerCamelCase ) self.assertTrue(torch.allclose(outputs.logits[0, :3] , __lowerCamelCase , atol=1e-4 ) ) @slow @require_accelerate @require_torch_gpu def __A ( self : Tuple ) -> int: SCREAMING_SNAKE_CASE_ = DeiTModel.from_pretrained( "facebook/deit-base-distilled-patch16-224" , torch_dtype=torch.floataa , device_map="auto" ) SCREAMING_SNAKE_CASE_ = self.default_image_processor SCREAMING_SNAKE_CASE_ = prepare_img() SCREAMING_SNAKE_CASE_ = image_processor(images=__lowerCamelCase , return_tensors="pt" ) SCREAMING_SNAKE_CASE_ = inputs.pixel_values.to(__lowerCamelCase ) # forward pass to make sure inference works in fp16 with torch.no_grad(): SCREAMING_SNAKE_CASE_ = model(__lowerCamelCase )
368
from ....utils import logging A : List[str] = logging.get_logger(__name__) class lowerCamelCase (SCREAMING_SNAKE_CASE__ ): """simple docstring""" def __init__( self : List[str] , __magic_name__ : Optional[Any] , __magic_name__ : Any=None , __magic_name__ : List[str]=2_048 ) -> List[Any]: SCREAMING_SNAKE_CASE_ = config.__dict__ SCREAMING_SNAKE_CASE_ = modal_hidden_size if num_labels: SCREAMING_SNAKE_CASE_ = num_labels
305
0
def a__ ( __UpperCamelCase , __UpperCamelCase ): if digit_amount > 0: return round(number - int(a__ ) , a__ ) return number - int(a__ ) if __name__ == "__main__": print(decimal_isolate(1.53, 0)) print(decimal_isolate(35.3_45, 1)) print(decimal_isolate(35.3_45, 2)) print(decimal_isolate(35.3_45, 3)) print(decimal_isolate(-14.7_89, 3)) print(decimal_isolate(0, 2)) print(decimal_isolate(-14.1_23, 1)) print(decimal_isolate(-14.1_23, 2)) print(decimal_isolate(-14.1_23, 3))
369
import warnings from typing import List, Optional, Union from ...processing_utils import ProcessorMixin from ...tokenization_utils_base import BatchEncoding, PaddingStrategy, PreTokenizedInput, TextInput, TruncationStrategy from ...utils import TensorType class lowerCamelCase (SCREAMING_SNAKE_CASE__ ): """simple docstring""" lowerCamelCase__ = ['''image_processor''', '''tokenizer'''] lowerCamelCase__ = '''ViltImageProcessor''' lowerCamelCase__ = ('''BertTokenizer''', '''BertTokenizerFast''') def __init__( self : Optional[int] , __magic_name__ : str=None , __magic_name__ : List[str]=None , **__magic_name__ : Any ) -> str: SCREAMING_SNAKE_CASE_ = None if "feature_extractor" in kwargs: warnings.warn( "The `feature_extractor` argument is deprecated and will be removed in v5, use `image_processor`" " instead." , __magic_name__ , ) SCREAMING_SNAKE_CASE_ = kwargs.pop("feature_extractor" ) SCREAMING_SNAKE_CASE_ = image_processor if image_processor is not None else feature_extractor if image_processor is None: raise ValueError("You need to specify an `image_processor`." ) if tokenizer is None: raise ValueError("You need to specify a `tokenizer`." ) super().__init__(__magic_name__ , __magic_name__ ) SCREAMING_SNAKE_CASE_ = self.image_processor def __call__( self : List[str] , __magic_name__ : List[str] , __magic_name__ : Union[TextInput, PreTokenizedInput, List[TextInput], List[PreTokenizedInput]] = None , __magic_name__ : bool = True , __magic_name__ : Union[bool, str, PaddingStrategy] = False , __magic_name__ : Union[bool, str, TruncationStrategy] = None , __magic_name__ : Optional[int] = None , __magic_name__ : int = 0 , __magic_name__ : Optional[int] = None , __magic_name__ : Optional[bool] = None , __magic_name__ : Optional[bool] = None , __magic_name__ : bool = False , __magic_name__ : bool = False , __magic_name__ : bool = False , __magic_name__ : bool = False , __magic_name__ : bool = True , __magic_name__ : Optional[Union[str, TensorType]] = None , **__magic_name__ : str , ) -> BatchEncoding: SCREAMING_SNAKE_CASE_ = self.tokenizer( text=__magic_name__ , add_special_tokens=__magic_name__ , padding=__magic_name__ , truncation=__magic_name__ , max_length=__magic_name__ , stride=__magic_name__ , pad_to_multiple_of=__magic_name__ , return_token_type_ids=__magic_name__ , return_attention_mask=__magic_name__ , return_overflowing_tokens=__magic_name__ , return_special_tokens_mask=__magic_name__ , return_offsets_mapping=__magic_name__ , return_length=__magic_name__ , verbose=__magic_name__ , return_tensors=__magic_name__ , **__magic_name__ , ) # add pixel_values + pixel_mask SCREAMING_SNAKE_CASE_ = self.image_processor(__magic_name__ , return_tensors=__magic_name__ ) encoding.update(__magic_name__ ) return encoding def __A ( self : Optional[int] , *__magic_name__ : List[Any] , **__magic_name__ : Optional[Any] ) -> Any: return self.tokenizer.batch_decode(*__magic_name__ , **__magic_name__ ) def __A ( self : Dict , *__magic_name__ : List[Any] , **__magic_name__ : Union[str, Any] ) -> str: return self.tokenizer.decode(*__magic_name__ , **__magic_name__ ) @property def __A ( self : Optional[int] ) -> Union[str, Any]: SCREAMING_SNAKE_CASE_ = self.tokenizer.model_input_names SCREAMING_SNAKE_CASE_ = self.image_processor.model_input_names return list(dict.fromkeys(tokenizer_input_names + image_processor_input_names ) ) @property def __A ( self : Dict ) -> List[Any]: warnings.warn( "`feature_extractor_class` is deprecated and will be removed in v5. Use `image_processor_class` instead." , __magic_name__ , ) return self.image_processor_class @property def __A ( self : int ) -> List[Any]: warnings.warn( "`feature_extractor` is deprecated and will be removed in v5. Use `image_processor` instead." , __magic_name__ , ) return self.image_processor
305
0
import argparse import os import pickle import sys import torch from transformers import TransfoXLConfig, TransfoXLLMHeadModel, load_tf_weights_in_transfo_xl from transformers.models.transfo_xl import tokenization_transfo_xl as data_utils from transformers.models.transfo_xl.tokenization_transfo_xl import CORPUS_NAME, VOCAB_FILES_NAMES from transformers.utils import CONFIG_NAME, WEIGHTS_NAME, logging logging.set_verbosity_info() # We do this to be able to load python 2 datasets pickles # See e.g. https://stackoverflow.com/questions/2121874/python-pickling-after-changing-a-modules-directory/2121918#2121918 A : Union[str, Any] = data_utils.TransfoXLTokenizer A : List[str] = data_utils.TransfoXLCorpus A : Optional[int] = data_utils A : List[Any] = data_utils def a__ ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase ): if transfo_xl_dataset_file: # Convert a pre-processed corpus (see original TensorFlow repo) with open(_lowerCamelCase , "rb" ) as fp: SCREAMING_SNAKE_CASE_ = pickle.load(_lowerCamelCase , encoding="latin1" ) # Save vocabulary and dataset cache as Dictionaries (should be better than pickles for the long-term) SCREAMING_SNAKE_CASE_ = pytorch_dump_folder_path + "/" + VOCAB_FILES_NAMES["pretrained_vocab_file"] print(F'''Save vocabulary to {pytorch_vocab_dump_path}''' ) SCREAMING_SNAKE_CASE_ = corpus.vocab.__dict__ torch.save(_lowerCamelCase , _lowerCamelCase ) SCREAMING_SNAKE_CASE_ = corpus.__dict__ corpus_dict_no_vocab.pop("vocab" , _lowerCamelCase ) SCREAMING_SNAKE_CASE_ = pytorch_dump_folder_path + "/" + CORPUS_NAME print(F'''Save dataset to {pytorch_dataset_dump_path}''' ) torch.save(_lowerCamelCase , _lowerCamelCase ) if tf_checkpoint_path: # Convert a pre-trained TensorFlow model SCREAMING_SNAKE_CASE_ = os.path.abspath(_lowerCamelCase ) SCREAMING_SNAKE_CASE_ = os.path.abspath(_lowerCamelCase ) print(F'''Converting Transformer XL checkpoint from {tf_path} with config at {config_path}.''' ) # Initialise PyTorch model if transfo_xl_config_file == "": SCREAMING_SNAKE_CASE_ = TransfoXLConfig() else: SCREAMING_SNAKE_CASE_ = TransfoXLConfig.from_json_file(_lowerCamelCase ) print(F'''Building PyTorch model from configuration: {config}''' ) SCREAMING_SNAKE_CASE_ = TransfoXLLMHeadModel(_lowerCamelCase ) SCREAMING_SNAKE_CASE_ = load_tf_weights_in_transfo_xl(_lowerCamelCase , _lowerCamelCase , _lowerCamelCase ) # Save pytorch-model SCREAMING_SNAKE_CASE_ = os.path.join(_lowerCamelCase , _lowerCamelCase ) SCREAMING_SNAKE_CASE_ = os.path.join(_lowerCamelCase , _lowerCamelCase ) print(F'''Save PyTorch model to {os.path.abspath(_lowerCamelCase )}''' ) torch.save(model.state_dict() , _lowerCamelCase ) print(F'''Save configuration file to {os.path.abspath(_lowerCamelCase )}''' ) with open(_lowerCamelCase , "w" , encoding="utf-8" ) as f: f.write(config.to_json_string() ) if __name__ == "__main__": A : Any = argparse.ArgumentParser() parser.add_argument( "--pytorch_dump_folder_path", default=None, type=str, required=True, help="Path to the folder to store the PyTorch model or dataset/vocab.", ) parser.add_argument( "--tf_checkpoint_path", default="", type=str, help="An optional path to a TensorFlow checkpoint path to be converted.", ) parser.add_argument( "--transfo_xl_config_file", default="", type=str, help=( "An optional config json file corresponding to the pre-trained BERT model. \n" "This specifies the model architecture." ), ) parser.add_argument( "--transfo_xl_dataset_file", default="", type=str, help="An optional dataset file to be converted in a vocabulary.", ) A : int = parser.parse_args() convert_transfo_xl_checkpoint_to_pytorch( args.tf_checkpoint_path, args.transfo_xl_config_file, args.pytorch_dump_folder_path, args.transfo_xl_dataset_file, )
370
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 ..auto import CONFIG_MAPPING A : str = logging.get_logger(__name__) A : Optional[int] = { "microsoft/table-transformer-detection": ( "https://huggingface.co/microsoft/table-transformer-detection/resolve/main/config.json" ), } class lowerCamelCase (SCREAMING_SNAKE_CASE__ ): """simple docstring""" lowerCamelCase__ = '''table-transformer''' lowerCamelCase__ = ['''past_key_values'''] lowerCamelCase__ = { '''hidden_size''': '''d_model''', '''num_attention_heads''': '''encoder_attention_heads''', } def __init__( self : List[Any] , __magic_name__ : Optional[Any]=True , __magic_name__ : Dict=None , __magic_name__ : Any=3 , __magic_name__ : List[str]=100 , __magic_name__ : Union[str, Any]=6 , __magic_name__ : Dict=2_048 , __magic_name__ : str=8 , __magic_name__ : int=6 , __magic_name__ : List[Any]=2_048 , __magic_name__ : Optional[int]=8 , __magic_name__ : Optional[int]=0.0 , __magic_name__ : List[Any]=0.0 , __magic_name__ : Optional[Any]=True , __magic_name__ : List[Any]="relu" , __magic_name__ : List[str]=256 , __magic_name__ : List[str]=0.1 , __magic_name__ : int=0.0 , __magic_name__ : Optional[Any]=0.0 , __magic_name__ : Tuple=0.02 , __magic_name__ : str=1.0 , __magic_name__ : int=False , __magic_name__ : Dict="sine" , __magic_name__ : Union[str, Any]="resnet50" , __magic_name__ : Optional[Any]=True , __magic_name__ : str=False , __magic_name__ : List[str]=1 , __magic_name__ : int=5 , __magic_name__ : Union[str, Any]=2 , __magic_name__ : Tuple=1 , __magic_name__ : Optional[int]=1 , __magic_name__ : Optional[Any]=5 , __magic_name__ : Optional[int]=2 , __magic_name__ : Union[str, Any]=0.1 , **__magic_name__ : Tuple , ) -> str: 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." ) SCREAMING_SNAKE_CASE_ = CONFIG_MAPPING["resnet"](out_features=["stage4"] ) elif isinstance(__magic_name__ , __magic_name__ ): SCREAMING_SNAKE_CASE_ = backbone_config.get("model_type" ) SCREAMING_SNAKE_CASE_ = CONFIG_MAPPING[backbone_model_type] SCREAMING_SNAKE_CASE_ = config_class.from_dict(__magic_name__ ) # set timm attributes to None SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = None, None, None SCREAMING_SNAKE_CASE_ = use_timm_backbone SCREAMING_SNAKE_CASE_ = backbone_config SCREAMING_SNAKE_CASE_ = num_channels SCREAMING_SNAKE_CASE_ = num_queries SCREAMING_SNAKE_CASE_ = d_model SCREAMING_SNAKE_CASE_ = encoder_ffn_dim SCREAMING_SNAKE_CASE_ = encoder_layers SCREAMING_SNAKE_CASE_ = encoder_attention_heads SCREAMING_SNAKE_CASE_ = decoder_ffn_dim SCREAMING_SNAKE_CASE_ = decoder_layers SCREAMING_SNAKE_CASE_ = decoder_attention_heads SCREAMING_SNAKE_CASE_ = dropout SCREAMING_SNAKE_CASE_ = attention_dropout SCREAMING_SNAKE_CASE_ = activation_dropout SCREAMING_SNAKE_CASE_ = activation_function SCREAMING_SNAKE_CASE_ = init_std SCREAMING_SNAKE_CASE_ = init_xavier_std SCREAMING_SNAKE_CASE_ = encoder_layerdrop SCREAMING_SNAKE_CASE_ = decoder_layerdrop SCREAMING_SNAKE_CASE_ = encoder_layers SCREAMING_SNAKE_CASE_ = auxiliary_loss SCREAMING_SNAKE_CASE_ = position_embedding_type SCREAMING_SNAKE_CASE_ = backbone SCREAMING_SNAKE_CASE_ = use_pretrained_backbone SCREAMING_SNAKE_CASE_ = dilation # Hungarian matcher SCREAMING_SNAKE_CASE_ = class_cost SCREAMING_SNAKE_CASE_ = bbox_cost SCREAMING_SNAKE_CASE_ = giou_cost # Loss coefficients SCREAMING_SNAKE_CASE_ = mask_loss_coefficient SCREAMING_SNAKE_CASE_ = dice_loss_coefficient SCREAMING_SNAKE_CASE_ = bbox_loss_coefficient SCREAMING_SNAKE_CASE_ = giou_loss_coefficient SCREAMING_SNAKE_CASE_ = eos_coefficient super().__init__(is_encoder_decoder=__magic_name__ , **__magic_name__ ) @property def __A ( self : Union[str, Any] ) -> int: return self.encoder_attention_heads @property def __A ( self : Any ) -> int: return self.d_model class lowerCamelCase (SCREAMING_SNAKE_CASE__ ): """simple docstring""" lowerCamelCase__ = version.parse('''1.11''' ) @property def __A ( self : Tuple ) -> Mapping[str, Mapping[int, str]]: return OrderedDict( [ ("pixel_values", {0: "batch", 1: "num_channels", 2: "height", 3: "width"}), ("pixel_mask", {0: "batch"}), ] ) @property def __A ( self : Any ) -> float: return 1e-5 @property def __A ( self : int ) -> int: return 12
305
0
from collections import deque from .hash_table import HashTable class lowerCamelCase (SCREAMING_SNAKE_CASE__ ): """simple docstring""" def __init__( self : int , *__magic_name__ : Any , **__magic_name__ : Any ) -> str: super().__init__(*_SCREAMING_SNAKE_CASE , **_SCREAMING_SNAKE_CASE ) def __A ( self : Dict , __magic_name__ : Union[str, Any] , __magic_name__ : Optional[Any] ) -> List[Any]: SCREAMING_SNAKE_CASE_ = deque([] ) if self.values[key] is None else self.values[key] self.values[key].appendleft(_SCREAMING_SNAKE_CASE ) SCREAMING_SNAKE_CASE_ = self.values[key] def __A ( self : Dict ) -> List[Any]: return ( sum(self.charge_factor - len(_SCREAMING_SNAKE_CASE ) for slot in self.values ) / self.size_table * self.charge_factor ) def __A ( self : Union[str, Any] , __magic_name__ : Optional[int] , __magic_name__ : Union[str, Any]=None ) -> Any: if not ( len(self.values[key] ) == self.charge_factor and self.values.count(_SCREAMING_SNAKE_CASE ) == 0 ): return key return super()._collision_resolution(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE )
371
import warnings from diffusers import StableDiffusionImgaImgPipeline # noqa F401 warnings.warn( "The `image_to_image.py` script is outdated. Please use directly `from diffusers import" " StableDiffusionImg2ImgPipeline` instead." )
305
0
"""simple docstring""" from collections import defaultdict from graphs.minimum_spanning_tree_prims import prisms_algorithm as mst def a__ ( ): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = 9, 1_4 # noqa: F841 SCREAMING_SNAKE_CASE_ = [ [0, 1, 4], [0, 7, 8], [1, 2, 8], [7, 8, 7], [7, 6, 1], [2, 8, 2], [8, 6, 6], [2, 3, 7], [2, 5, 4], [6, 5, 2], [3, 5, 1_4], [3, 4, 9], [5, 4, 1_0], [1, 7, 1_1], ] SCREAMING_SNAKE_CASE_ = defaultdict(lowerCamelCase__ ) for nodea, nodea, cost in edges: adjancency[nodea].append([nodea, cost] ) adjancency[nodea].append([nodea, cost] ) SCREAMING_SNAKE_CASE_ = mst(lowerCamelCase__ ) SCREAMING_SNAKE_CASE_ = [ [7, 6, 1], [2, 8, 2], [6, 5, 2], [0, 1, 4], [2, 5, 4], [2, 3, 7], [0, 7, 8], [3, 4, 9], ] for answer in expected: SCREAMING_SNAKE_CASE_ = tuple(answer[:2] ) SCREAMING_SNAKE_CASE_ = tuple(edge[::-1] ) assert edge in result or reverse in result
350
from __future__ import annotations import numpy as np def a__ ( __UpperCamelCase ): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = np.shape(__UpperCamelCase ) if rows != columns: SCREAMING_SNAKE_CASE_ = ( "'table' has to be of square shaped array but got a " F'''{rows}x{columns} array:\n{table}''' ) raise ValueError(__UpperCamelCase ) SCREAMING_SNAKE_CASE_ = np.zeros((rows, columns) ) SCREAMING_SNAKE_CASE_ = np.zeros((rows, columns) ) for i in range(__UpperCamelCase ): for j in range(__UpperCamelCase ): SCREAMING_SNAKE_CASE_ = sum(lower[i][k] * upper[k][j] for k in range(__UpperCamelCase ) ) if upper[j][j] == 0: raise ArithmeticError("No LU decomposition exists" ) SCREAMING_SNAKE_CASE_ = (table[i][j] - total) / upper[j][j] SCREAMING_SNAKE_CASE_ = 1 for j in range(__UpperCamelCase , __UpperCamelCase ): SCREAMING_SNAKE_CASE_ = sum(lower[i][k] * upper[k][j] for k in range(__UpperCamelCase ) ) SCREAMING_SNAKE_CASE_ = table[i][j] - total return lower, upper if __name__ == "__main__": import doctest doctest.testmod()
305
0
"""simple docstring""" import os import unittest from transformers.models.phobert.tokenization_phobert import VOCAB_FILES_NAMES, PhobertTokenizer from ...test_tokenization_common import TokenizerTesterMixin class lowerCamelCase (__SCREAMING_SNAKE_CASE , unittest.TestCase ): """simple docstring""" lowerCamelCase__ = PhobertTokenizer lowerCamelCase__ = False def __A ( self : Optional[Any] ) -> str: super().setUp() # Adapted from Sennrich et al. 2015 and https://github.com/rsennrich/subword-nmt SCREAMING_SNAKE_CASE_ = ["T@@", "i", "I", "R@@", "r", "e@@"] SCREAMING_SNAKE_CASE_ = dict(zip(_snake_case , range(len(_snake_case ) ) ) ) SCREAMING_SNAKE_CASE_ = ["#version: 0.2", "l à</w>"] SCREAMING_SNAKE_CASE_ = {"unk_token": "<unk>"} SCREAMING_SNAKE_CASE_ = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES["vocab_file"] ) SCREAMING_SNAKE_CASE_ = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES["merges_file"] ) with open(self.vocab_file , "w" , encoding="utf-8" ) as fp: for token in vocab_tokens: fp.write(F'''{token} {vocab_tokens[token]}\n''' ) with open(self.merges_file , "w" , encoding="utf-8" ) as fp: fp.write("\n".join(_snake_case ) ) def __A ( self : List[Any] , **__magic_name__ : Tuple ) -> Any: kwargs.update(self.special_tokens_map ) return PhobertTokenizer.from_pretrained(self.tmpdirname , **_snake_case ) def __A ( self : Dict , __magic_name__ : Dict ) -> List[Any]: SCREAMING_SNAKE_CASE_ = "Tôi là VinAI Research" SCREAMING_SNAKE_CASE_ = "T<unk> i <unk> <unk> <unk> <unk> <unk> <unk> I Re<unk> e<unk> <unk> <unk> <unk>" return input_text, output_text def __A ( self : List[str] ) -> List[Any]: SCREAMING_SNAKE_CASE_ = PhobertTokenizer(self.vocab_file , self.merges_file , **self.special_tokens_map ) SCREAMING_SNAKE_CASE_ = "Tôi là VinAI Research" SCREAMING_SNAKE_CASE_ = "T@@ ô@@ i l@@ à V@@ i@@ n@@ A@@ I R@@ e@@ s@@ e@@ a@@ r@@ c@@ h".split() SCREAMING_SNAKE_CASE_ = tokenizer.tokenize(_snake_case ) print(_snake_case ) self.assertListEqual(_snake_case , _snake_case ) SCREAMING_SNAKE_CASE_ = tokens + [tokenizer.unk_token] SCREAMING_SNAKE_CASE_ = [4, 3, 5, 3, 3, 3, 3, 3, 3, 6, 7, 9, 3, 9, 3, 3, 3, 3, 3] self.assertListEqual(tokenizer.convert_tokens_to_ids(_snake_case ) , _snake_case )
351
from math import pi, sqrt, tan def a__ ( __UpperCamelCase ): if side_length < 0: raise ValueError("surface_area_cube() only accepts non-negative values" ) return 6 * side_length**2 def a__ ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase ): if length < 0 or breadth < 0 or height < 0: raise ValueError("surface_area_cuboid() only accepts non-negative values" ) return 2 * ((length * breadth) + (breadth * height) + (length * height)) def a__ ( __UpperCamelCase ): if radius < 0: raise ValueError("surface_area_sphere() only accepts non-negative values" ) return 4 * pi * radius**2 def a__ ( __UpperCamelCase ): if radius < 0: raise ValueError("surface_area_hemisphere() only accepts non-negative values" ) return 3 * pi * radius**2 def a__ ( __UpperCamelCase , __UpperCamelCase ): if radius < 0 or height < 0: raise ValueError("surface_area_cone() only accepts non-negative values" ) return pi * radius * (radius + (height**2 + radius**2) ** 0.5) def a__ ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase ): if radius_a < 0 or radius_a < 0 or height < 0: raise ValueError( "surface_area_conical_frustum() only accepts non-negative values" ) SCREAMING_SNAKE_CASE_ = (height**2 + (radius_a - radius_a) ** 2) ** 0.5 return pi * ((slant_height * (radius_a + radius_a)) + radius_a**2 + radius_a**2) def a__ ( __UpperCamelCase , __UpperCamelCase ): if radius < 0 or height < 0: raise ValueError("surface_area_cylinder() only accepts non-negative values" ) return 2 * pi * radius * (height + radius) def a__ ( __UpperCamelCase , __UpperCamelCase ): if torus_radius < 0 or tube_radius < 0: raise ValueError("surface_area_torus() only accepts non-negative values" ) if torus_radius < tube_radius: raise ValueError( "surface_area_torus() does not support spindle or self intersecting tori" ) return 4 * pow(__UpperCamelCase , 2 ) * torus_radius * tube_radius def a__ ( __UpperCamelCase , __UpperCamelCase ): if length < 0 or width < 0: raise ValueError("area_rectangle() only accepts non-negative values" ) return length * width def a__ ( __UpperCamelCase ): if side_length < 0: raise ValueError("area_square() only accepts non-negative values" ) return side_length**2 def a__ ( __UpperCamelCase , __UpperCamelCase ): if base < 0 or height < 0: raise ValueError("area_triangle() only accepts non-negative values" ) return (base * height) / 2 def a__ ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase ): if sidea < 0 or sidea < 0 or sidea < 0: raise ValueError("area_triangle_three_sides() only accepts non-negative values" ) elif sidea + sidea < sidea or sidea + sidea < sidea or sidea + sidea < sidea: raise ValueError("Given three sides do not form a triangle" ) SCREAMING_SNAKE_CASE_ = (sidea + sidea + sidea) / 2 SCREAMING_SNAKE_CASE_ = sqrt( semi_perimeter * (semi_perimeter - sidea) * (semi_perimeter - sidea) * (semi_perimeter - sidea) ) return area def a__ ( __UpperCamelCase , __UpperCamelCase ): if base < 0 or height < 0: raise ValueError("area_parallelogram() only accepts non-negative values" ) return base * height def a__ ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase ): if basea < 0 or basea < 0 or height < 0: raise ValueError("area_trapezium() only accepts non-negative values" ) return 1 / 2 * (basea + basea) * height def a__ ( __UpperCamelCase ): if radius < 0: raise ValueError("area_circle() only accepts non-negative values" ) return pi * radius**2 def a__ ( __UpperCamelCase , __UpperCamelCase ): if radius_x < 0 or radius_y < 0: raise ValueError("area_ellipse() only accepts non-negative values" ) return pi * radius_x * radius_y def a__ ( __UpperCamelCase , __UpperCamelCase ): if diagonal_a < 0 or diagonal_a < 0: raise ValueError("area_rhombus() only accepts non-negative values" ) return 1 / 2 * diagonal_a * diagonal_a def a__ ( __UpperCamelCase , __UpperCamelCase ): if not isinstance(__UpperCamelCase , __UpperCamelCase ) or sides < 3: raise ValueError( "area_reg_polygon() only accepts integers greater than or \ equal to three as number of sides" ) elif length < 0: raise ValueError( "area_reg_polygon() only accepts non-negative values as \ length of a side" ) return (sides * length**2) / (4 * tan(pi / sides )) return (sides * length**2) / (4 * tan(pi / sides )) if __name__ == "__main__": import doctest doctest.testmod(verbose=True) # verbose so we can see methods missing tests print("[DEMO] Areas of various geometric shapes: \n") print(f"Rectangle: {area_rectangle(10, 20) = }") print(f"Square: {area_square(10) = }") print(f"Triangle: {area_triangle(10, 10) = }") print(f"Triangle: {area_triangle_three_sides(5, 12, 13) = }") print(f"Parallelogram: {area_parallelogram(10, 20) = }") print(f"Rhombus: {area_rhombus(10, 20) = }") print(f"Trapezium: {area_trapezium(10, 20, 30) = }") print(f"Circle: {area_circle(20) = }") print(f"Ellipse: {area_ellipse(10, 20) = }") print("\nSurface Areas of various geometric shapes: \n") print(f"Cube: {surface_area_cube(20) = }") print(f"Cuboid: {surface_area_cuboid(10, 20, 30) = }") print(f"Sphere: {surface_area_sphere(20) = }") print(f"Hemisphere: {surface_area_hemisphere(20) = }") print(f"Cone: {surface_area_cone(10, 20) = }") print(f"Conical Frustum: {surface_area_conical_frustum(10, 20, 30) = }") print(f"Cylinder: {surface_area_cylinder(10, 20) = }") print(f"Torus: {surface_area_torus(20, 10) = }") print(f"Equilateral Triangle: {area_reg_polygon(3, 10) = }") print(f"Square: {area_reg_polygon(4, 10) = }") print(f"Reqular Pentagon: {area_reg_polygon(5, 10) = }")
305
0
from __future__ import annotations from itertools import permutations from random import randint from timeit import repeat def a__ ( ): SCREAMING_SNAKE_CASE_ = [randint(-1_0_0_0 , 1_0_0_0 ) for i in range(1_0 )] SCREAMING_SNAKE_CASE_ = randint(-5_0_0_0 , 5_0_0_0 ) return (arr, r) A : Optional[Any] = make_dataset() def a__ ( __UpperCamelCase , __UpperCamelCase ): for triplet in permutations(UpperCamelCase__ , 3 ): if sum(UpperCamelCase__ ) == target: return tuple(sorted(UpperCamelCase__ ) ) return (0, 0, 0) def a__ ( __UpperCamelCase , __UpperCamelCase ): arr.sort() SCREAMING_SNAKE_CASE_ = len(UpperCamelCase__ ) for i in range(n - 1 ): SCREAMING_SNAKE_CASE_ = 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__ ( ): SCREAMING_SNAKE_CASE_ = ''' from __main__ import dataset, triplet_sum1, triplet_sum2 ''' SCREAMING_SNAKE_CASE_ = ''' triplet_sum1(*dataset) ''' SCREAMING_SNAKE_CASE_ = ''' triplet_sum2(*dataset) ''' SCREAMING_SNAKE_CASE_ = repeat(setup=UpperCamelCase__ , stmt=UpperCamelCase__ , repeat=5 , number=1_0_0_0_0 ) SCREAMING_SNAKE_CASE_ = repeat(setup=UpperCamelCase__ , stmt=UpperCamelCase__ , repeat=5 , number=1_0_0_0_0 ) return (min(UpperCamelCase__ ), min(UpperCamelCase__ )) if __name__ == "__main__": from doctest import testmod testmod() A : List[str] = solution_times() print(f"The time for naive implementation is {times[0]}.") print(f"The time for optimized implementation is {times[1]}.")
352
from collections import OrderedDict from typing import Any, Mapping, Optional from ... import PreTrainedTokenizer from ...configuration_utils import PretrainedConfig from ...file_utils import TensorType, is_torch_available from ...onnx import OnnxConfig, OnnxConfigWithPast, OnnxSeqaSeqConfigWithPast from ...onnx.utils import compute_effective_axis_dimension from ...utils import logging A : List[str] = logging.get_logger(__name__) A : int = { "facebook/blenderbot_small-90M": "https://huggingface.co/facebook/blenderbot_small-90M/resolve/main/config.json", # See all BlenderbotSmall models at https://huggingface.co/models?filter=blenderbot_small } class lowerCamelCase (SCREAMING_SNAKE_CASE__ ): """simple docstring""" lowerCamelCase__ = '''blenderbot-small''' lowerCamelCase__ = ['''past_key_values'''] lowerCamelCase__ = {'''num_attention_heads''': '''encoder_attention_heads''', '''hidden_size''': '''d_model'''} def __init__( self : Dict , __magic_name__ : Dict=50_265 , __magic_name__ : str=512 , __magic_name__ : List[Any]=8 , __magic_name__ : Any=2_048 , __magic_name__ : Dict=16 , __magic_name__ : Any=8 , __magic_name__ : Optional[int]=2_048 , __magic_name__ : Dict=16 , __magic_name__ : Tuple=0.0 , __magic_name__ : Dict=0.0 , __magic_name__ : Optional[int]=True , __magic_name__ : Any=True , __magic_name__ : Dict="gelu" , __magic_name__ : Tuple=512 , __magic_name__ : List[str]=0.1 , __magic_name__ : List[Any]=0.0 , __magic_name__ : List[Any]=0.0 , __magic_name__ : Tuple=0.02 , __magic_name__ : Union[str, Any]=1 , __magic_name__ : List[Any]=False , __magic_name__ : str=0 , __magic_name__ : Dict=1 , __magic_name__ : str=2 , __magic_name__ : Union[str, Any]=2 , **__magic_name__ : Optional[Any] , ) -> Optional[Any]: SCREAMING_SNAKE_CASE_ = vocab_size SCREAMING_SNAKE_CASE_ = max_position_embeddings SCREAMING_SNAKE_CASE_ = d_model SCREAMING_SNAKE_CASE_ = encoder_ffn_dim SCREAMING_SNAKE_CASE_ = encoder_layers SCREAMING_SNAKE_CASE_ = encoder_attention_heads SCREAMING_SNAKE_CASE_ = decoder_ffn_dim SCREAMING_SNAKE_CASE_ = decoder_layers SCREAMING_SNAKE_CASE_ = decoder_attention_heads SCREAMING_SNAKE_CASE_ = dropout SCREAMING_SNAKE_CASE_ = attention_dropout SCREAMING_SNAKE_CASE_ = activation_dropout SCREAMING_SNAKE_CASE_ = activation_function SCREAMING_SNAKE_CASE_ = init_std SCREAMING_SNAKE_CASE_ = encoder_layerdrop SCREAMING_SNAKE_CASE_ = decoder_layerdrop SCREAMING_SNAKE_CASE_ = use_cache SCREAMING_SNAKE_CASE_ = encoder_layers SCREAMING_SNAKE_CASE_ = scale_embedding # scale factor will be sqrt(d_model) if True super().__init__( pad_token_id=__magic_name__ , bos_token_id=__magic_name__ , eos_token_id=__magic_name__ , is_encoder_decoder=__magic_name__ , decoder_start_token_id=__magic_name__ , forced_eos_token_id=__magic_name__ , **__magic_name__ , ) class lowerCamelCase (SCREAMING_SNAKE_CASE__ ): """simple docstring""" @property def __A ( self : str ) -> Mapping[str, Mapping[int, str]]: if self.task in ["default", "seq2seq-lm"]: SCREAMING_SNAKE_CASE_ = OrderedDict( [ ("input_ids", {0: "batch", 1: "encoder_sequence"}), ("attention_mask", {0: "batch", 1: "encoder_sequence"}), ] ) if self.use_past: SCREAMING_SNAKE_CASE_ = {0: "batch"} SCREAMING_SNAKE_CASE_ = {0: "batch", 1: "past_decoder_sequence + sequence"} else: SCREAMING_SNAKE_CASE_ = {0: "batch", 1: "decoder_sequence"} SCREAMING_SNAKE_CASE_ = {0: "batch", 1: "decoder_sequence"} if self.use_past: self.fill_with_past_key_values_(__magic_name__ , direction="inputs" ) elif self.task == "causal-lm": # TODO: figure this case out. SCREAMING_SNAKE_CASE_ = OrderedDict( [ ("input_ids", {0: "batch", 1: "encoder_sequence"}), ("attention_mask", {0: "batch", 1: "encoder_sequence"}), ] ) if self.use_past: SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = self.num_layers for i in range(__magic_name__ ): SCREAMING_SNAKE_CASE_ = {0: "batch", 2: "past_sequence + sequence"} SCREAMING_SNAKE_CASE_ = {0: "batch", 2: "past_sequence + sequence"} else: SCREAMING_SNAKE_CASE_ = OrderedDict( [ ("input_ids", {0: "batch", 1: "encoder_sequence"}), ("attention_mask", {0: "batch", 1: "encoder_sequence"}), ("decoder_input_ids", {0: "batch", 1: "decoder_sequence"}), ("decoder_attention_mask", {0: "batch", 1: "decoder_sequence"}), ] ) return common_inputs @property def __A ( self : Tuple ) -> Mapping[str, Mapping[int, str]]: if self.task in ["default", "seq2seq-lm"]: SCREAMING_SNAKE_CASE_ = super().outputs else: SCREAMING_SNAKE_CASE_ = super(__magic_name__ , self ).outputs if self.use_past: SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = self.num_layers for i in range(__magic_name__ ): SCREAMING_SNAKE_CASE_ = {0: "batch", 2: "past_sequence + sequence"} SCREAMING_SNAKE_CASE_ = {0: "batch", 2: "past_sequence + sequence"} return common_outputs def __A ( self : int , __magic_name__ : PreTrainedTokenizer , __magic_name__ : int = -1 , __magic_name__ : int = -1 , __magic_name__ : bool = False , __magic_name__ : Optional[TensorType] = None , ) -> Mapping[str, Any]: SCREAMING_SNAKE_CASE_ = self._generate_dummy_inputs_for_sequence_classification_and_question_answering( __magic_name__ , __magic_name__ , __magic_name__ , __magic_name__ , __magic_name__ ) # Generate decoder inputs SCREAMING_SNAKE_CASE_ = seq_length if not self.use_past else 1 SCREAMING_SNAKE_CASE_ = self._generate_dummy_inputs_for_sequence_classification_and_question_answering( __magic_name__ , __magic_name__ , __magic_name__ , __magic_name__ , __magic_name__ ) SCREAMING_SNAKE_CASE_ = {F'''decoder_{name}''': tensor for name, tensor in decoder_inputs.items()} SCREAMING_SNAKE_CASE_ = dict(**__magic_name__ , **__magic_name__ ) if self.use_past: if not is_torch_available(): raise ValueError("Cannot generate dummy past_keys inputs without PyTorch installed." ) else: import torch SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = common_inputs["input_ids"].shape SCREAMING_SNAKE_CASE_ = common_inputs["decoder_input_ids"].shape[1] SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = self.num_attention_heads SCREAMING_SNAKE_CASE_ = ( batch, num_encoder_attention_heads, encoder_seq_length, self._config.hidden_size // num_encoder_attention_heads, ) SCREAMING_SNAKE_CASE_ = decoder_seq_length + 3 SCREAMING_SNAKE_CASE_ = ( batch, num_decoder_attention_heads, decoder_past_length, self._config.hidden_size // num_decoder_attention_heads, ) SCREAMING_SNAKE_CASE_ = torch.cat( [common_inputs["decoder_attention_mask"], torch.ones(__magic_name__ , __magic_name__ )] , dim=1 ) SCREAMING_SNAKE_CASE_ = [] # If the number of encoder and decoder layers are present in the model configuration, both are considered SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = self.num_layers SCREAMING_SNAKE_CASE_ = min(__magic_name__ , __magic_name__ ) SCREAMING_SNAKE_CASE_ = max(__magic_name__ , __magic_name__ ) - min_num_layers SCREAMING_SNAKE_CASE_ = "encoder" if num_encoder_layers > num_decoder_layers else "decoder" for _ in range(__magic_name__ ): common_inputs["past_key_values"].append( ( torch.zeros(__magic_name__ ), torch.zeros(__magic_name__ ), torch.zeros(__magic_name__ ), torch.zeros(__magic_name__ ), ) ) # TODO: test this. SCREAMING_SNAKE_CASE_ = encoder_shape if remaining_side_name == "encoder" else decoder_shape for _ in range(__magic_name__ , __magic_name__ ): common_inputs["past_key_values"].append((torch.zeros(__magic_name__ ), torch.zeros(__magic_name__ )) ) return common_inputs def __A ( self : Union[str, Any] , __magic_name__ : PreTrainedTokenizer , __magic_name__ : int = -1 , __magic_name__ : int = -1 , __magic_name__ : bool = False , __magic_name__ : Optional[TensorType] = None , ) -> Mapping[str, Any]: SCREAMING_SNAKE_CASE_ = self._generate_dummy_inputs_for_sequence_classification_and_question_answering( __magic_name__ , __magic_name__ , __magic_name__ , __magic_name__ , __magic_name__ ) if self.use_past: if not is_torch_available(): raise ValueError("Cannot generate dummy past_keys inputs without PyTorch installed." ) else: import torch SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = common_inputs["input_ids"].shape # Not using the same length for past_key_values SCREAMING_SNAKE_CASE_ = seqlen + 2 SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = self.num_layers SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = self.num_attention_heads SCREAMING_SNAKE_CASE_ = ( batch, num_encoder_attention_heads, past_key_values_length, self._config.hidden_size // num_encoder_attention_heads, ) SCREAMING_SNAKE_CASE_ = common_inputs["attention_mask"].dtype SCREAMING_SNAKE_CASE_ = torch.cat( [common_inputs["attention_mask"], torch.ones(__magic_name__ , __magic_name__ , dtype=__magic_name__ )] , dim=1 ) SCREAMING_SNAKE_CASE_ = [ (torch.zeros(__magic_name__ ), torch.zeros(__magic_name__ )) for _ in range(__magic_name__ ) ] return common_inputs def __A ( self : Dict , __magic_name__ : PreTrainedTokenizer , __magic_name__ : int = -1 , __magic_name__ : int = -1 , __magic_name__ : bool = False , __magic_name__ : Optional[TensorType] = None , ) -> Mapping[str, Any]: # Copied from OnnxConfig.generate_dummy_inputs # Did not use super(OnnxConfigWithPast, self).generate_dummy_inputs for code clarity. # If dynamic axis (-1) we forward with a fixed dimension of 2 samples to avoid optimizations made by ONNX SCREAMING_SNAKE_CASE_ = compute_effective_axis_dimension( __magic_name__ , fixed_dimension=OnnxConfig.default_fixed_batch , num_token_to_add=0 ) # If dynamic axis (-1) we forward with a fixed dimension of 8 tokens to avoid optimizations made by ONNX SCREAMING_SNAKE_CASE_ = tokenizer.num_special_tokens_to_add(__magic_name__ ) SCREAMING_SNAKE_CASE_ = compute_effective_axis_dimension( __magic_name__ , fixed_dimension=OnnxConfig.default_fixed_sequence , num_token_to_add=__magic_name__ ) # Generate dummy inputs according to compute batch and sequence SCREAMING_SNAKE_CASE_ = [" ".join([tokenizer.unk_token] ) * seq_length] * batch_size SCREAMING_SNAKE_CASE_ = dict(tokenizer(__magic_name__ , return_tensors=__magic_name__ ) ) return common_inputs def __A ( self : Optional[Any] , __magic_name__ : PreTrainedTokenizer , __magic_name__ : int = -1 , __magic_name__ : int = -1 , __magic_name__ : bool = False , __magic_name__ : Optional[TensorType] = None , ) -> Mapping[str, Any]: if self.task in ["default", "seq2seq-lm"]: SCREAMING_SNAKE_CASE_ = self._generate_dummy_inputs_for_default_and_seqaseq_lm( __magic_name__ , batch_size=__magic_name__ , seq_length=__magic_name__ , is_pair=__magic_name__ , framework=__magic_name__ ) elif self.task == "causal-lm": SCREAMING_SNAKE_CASE_ = self._generate_dummy_inputs_for_causal_lm( __magic_name__ , batch_size=__magic_name__ , seq_length=__magic_name__ , is_pair=__magic_name__ , framework=__magic_name__ ) else: SCREAMING_SNAKE_CASE_ = self._generate_dummy_inputs_for_sequence_classification_and_question_answering( __magic_name__ , batch_size=__magic_name__ , seq_length=__magic_name__ , is_pair=__magic_name__ , framework=__magic_name__ ) return common_inputs def __A ( self : Optional[Any] , __magic_name__ : str , __magic_name__ : List[Any] , __magic_name__ : str , __magic_name__ : List[str] ) -> List[str]: if self.task in ["default", "seq2seq-lm"]: SCREAMING_SNAKE_CASE_ = super()._flatten_past_key_values_(__magic_name__ , __magic_name__ , __magic_name__ , __magic_name__ ) else: SCREAMING_SNAKE_CASE_ = super(__magic_name__ , self )._flatten_past_key_values_( __magic_name__ , __magic_name__ , __magic_name__ , __magic_name__ )
305
0
import argparse import os import torch from transformers.utils import WEIGHTS_NAME A : Tuple = ['small', 'medium', 'large'] A : Union[str, Any] = 'lm_head.decoder.weight' A : Union[str, Any] = 'lm_head.weight' def a__ ( __UpperCamelCase , __UpperCamelCase ): SCREAMING_SNAKE_CASE_ = torch.load(UpperCAmelCase_ ) SCREAMING_SNAKE_CASE_ = d.pop(UpperCAmelCase_ ) os.makedirs(UpperCAmelCase_ , exist_ok=UpperCAmelCase_ ) torch.save(UpperCAmelCase_ , os.path.join(UpperCAmelCase_ , UpperCAmelCase_ ) ) if __name__ == "__main__": A : str = argparse.ArgumentParser() parser.add_argument("--dialogpt_path", default=".", type=str) A : Tuple = parser.parse_args() for MODEL in DIALOGPT_MODELS: A : Dict = os.path.join(args.dialogpt_path, f"{MODEL}_ft.pkl") A : List[Any] = f"./DialoGPT-{MODEL}" convert_dialogpt_checkpoint( checkpoint_path, pytorch_dump_folder_path, )
353
import inspect import unittest from datasets import load_dataset from packaging import version from transformers import BeitConfig from transformers.models.auto import get_values from transformers.testing_utils import require_torch, require_torch_multi_gpu, require_vision, slow, torch_device from transformers.utils import cached_property, is_torch_available, is_vision_available from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, _config_zero_init, floats_tensor, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from torch import nn from transformers import ( MODEL_MAPPING, BeitForImageClassification, BeitForMaskedImageModeling, BeitForSemanticSegmentation, BeitModel, ) from transformers.models.beit.modeling_beit import BEIT_PRETRAINED_MODEL_ARCHIVE_LIST if is_vision_available(): import PIL from PIL import Image from transformers import BeitImageProcessor class lowerCamelCase : """simple docstring""" def __init__( self : List[Any] , __magic_name__ : List[str] , __magic_name__ : int=100 , __magic_name__ : Optional[Any]=13 , __magic_name__ : Dict=30 , __magic_name__ : Tuple=2 , __magic_name__ : str=3 , __magic_name__ : str=True , __magic_name__ : Optional[int]=True , __magic_name__ : Union[str, Any]=32 , __magic_name__ : Optional[int]=4 , __magic_name__ : Dict=4 , __magic_name__ : Tuple=37 , __magic_name__ : Any="gelu" , __magic_name__ : int=0.1 , __magic_name__ : List[str]=0.1 , __magic_name__ : Optional[int]=10 , __magic_name__ : Tuple=0.02 , __magic_name__ : Optional[int]=3 , __magic_name__ : List[str]=None , __magic_name__ : Tuple=[0, 1, 2, 3] , ) -> List[str]: SCREAMING_SNAKE_CASE_ = parent SCREAMING_SNAKE_CASE_ = 100 SCREAMING_SNAKE_CASE_ = batch_size SCREAMING_SNAKE_CASE_ = image_size SCREAMING_SNAKE_CASE_ = patch_size SCREAMING_SNAKE_CASE_ = num_channels SCREAMING_SNAKE_CASE_ = is_training SCREAMING_SNAKE_CASE_ = use_labels 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_ = type_sequence_label_size SCREAMING_SNAKE_CASE_ = initializer_range SCREAMING_SNAKE_CASE_ = scope SCREAMING_SNAKE_CASE_ = out_indices SCREAMING_SNAKE_CASE_ = num_labels # in BeiT, the seq length equals the number of patches + 1 (we add 1 for the [CLS] token) SCREAMING_SNAKE_CASE_ = (image_size // patch_size) ** 2 SCREAMING_SNAKE_CASE_ = num_patches + 1 def __A ( self : Any ) -> int: SCREAMING_SNAKE_CASE_ = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_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.image_size, self.image_size] , self.num_labels ) SCREAMING_SNAKE_CASE_ = self.get_config() return config, pixel_values, labels, pixel_labels def __A ( self : Dict ) -> Optional[int]: return BeitConfig( vocab_size=self.vocab_size , image_size=self.image_size , patch_size=self.patch_size , num_channels=self.num_channels , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , is_decoder=__magic_name__ , initializer_range=self.initializer_range , out_indices=self.out_indices , ) def __A ( self : Optional[int] , __magic_name__ : List[str] , __magic_name__ : Optional[int] , __magic_name__ : Tuple , __magic_name__ : Tuple ) -> Optional[int]: SCREAMING_SNAKE_CASE_ = BeitModel(config=__magic_name__ ) model.to(__magic_name__ ) model.eval() SCREAMING_SNAKE_CASE_ = model(__magic_name__ ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def __A ( self : Union[str, Any] , __magic_name__ : List[str] , __magic_name__ : int , __magic_name__ : int , __magic_name__ : str ) -> int: SCREAMING_SNAKE_CASE_ = BeitForMaskedImageModeling(config=__magic_name__ ) model.to(__magic_name__ ) model.eval() SCREAMING_SNAKE_CASE_ = model(__magic_name__ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length - 1, self.vocab_size) ) def __A ( self : Dict , __magic_name__ : List[Any] , __magic_name__ : int , __magic_name__ : Tuple , __magic_name__ : Optional[Any] ) -> int: SCREAMING_SNAKE_CASE_ = self.type_sequence_label_size SCREAMING_SNAKE_CASE_ = BeitForImageClassification(__magic_name__ ) model.to(__magic_name__ ) model.eval() SCREAMING_SNAKE_CASE_ = model(__magic_name__ , labels=__magic_name__ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.type_sequence_label_size) ) # test greyscale images SCREAMING_SNAKE_CASE_ = 1 SCREAMING_SNAKE_CASE_ = BeitForImageClassification(__magic_name__ ) model.to(__magic_name__ ) model.eval() SCREAMING_SNAKE_CASE_ = floats_tensor([self.batch_size, 1, self.image_size, self.image_size] ) SCREAMING_SNAKE_CASE_ = model(__magic_name__ , labels=__magic_name__ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.type_sequence_label_size) ) def __A ( self : Tuple , __magic_name__ : Any , __magic_name__ : Optional[Any] , __magic_name__ : Tuple , __magic_name__ : int ) -> int: SCREAMING_SNAKE_CASE_ = self.num_labels SCREAMING_SNAKE_CASE_ = BeitForSemanticSegmentation(__magic_name__ ) model.to(__magic_name__ ) model.eval() SCREAMING_SNAKE_CASE_ = model(__magic_name__ ) self.parent.assertEqual( result.logits.shape , (self.batch_size, self.num_labels, self.image_size * 2, self.image_size * 2) ) SCREAMING_SNAKE_CASE_ = model(__magic_name__ , labels=__magic_name__ ) self.parent.assertEqual( result.logits.shape , (self.batch_size, self.num_labels, self.image_size * 2, self.image_size * 2) ) def __A ( self : str ) -> List[Any]: SCREAMING_SNAKE_CASE_ = self.prepare_config_and_inputs() SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = config_and_inputs SCREAMING_SNAKE_CASE_ = {"pixel_values": pixel_values} return config, inputs_dict @require_torch class lowerCamelCase (SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , unittest.TestCase ): """simple docstring""" lowerCamelCase__ = ( (BeitModel, BeitForImageClassification, BeitForMaskedImageModeling, BeitForSemanticSegmentation) if is_torch_available() else () ) lowerCamelCase__ = ( { '''feature-extraction''': BeitModel, '''image-classification''': BeitForImageClassification, '''image-segmentation''': BeitForSemanticSegmentation, } if is_torch_available() else {} ) lowerCamelCase__ = False lowerCamelCase__ = False lowerCamelCase__ = False def __A ( self : Tuple ) -> Any: SCREAMING_SNAKE_CASE_ = BeitModelTester(self ) SCREAMING_SNAKE_CASE_ = ConfigTester(self , config_class=__magic_name__ , has_text_modality=__magic_name__ , hidden_size=37 ) def __A ( self : Dict ) -> List[Any]: self.config_tester.run_common_tests() @unittest.skip(reason="BEiT does not use inputs_embeds" ) def __A ( self : List[str] ) -> Optional[Any]: pass @require_torch_multi_gpu @unittest.skip(reason="BEiT has some layers using `add_module` which doesn't work well with `nn.DataParallel`" ) def __A ( self : str ) -> List[str]: pass def __A ( self : List[Any] ) -> 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: SCREAMING_SNAKE_CASE_ = model_class(__magic_name__ ) self.assertIsInstance(model.get_input_embeddings() , (nn.Module) ) SCREAMING_SNAKE_CASE_ = model.get_output_embeddings() self.assertTrue(x is None or isinstance(__magic_name__ , nn.Linear ) ) def __A ( self : Union[str, Any] ) -> int: 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(__magic_name__ ) SCREAMING_SNAKE_CASE_ = inspect.signature(model.forward ) # signature.parameters is an OrderedDict => so arg_names order is deterministic SCREAMING_SNAKE_CASE_ = [*signature.parameters.keys()] SCREAMING_SNAKE_CASE_ = ["pixel_values"] self.assertListEqual(arg_names[:1] , __magic_name__ ) def __A ( self : Tuple ) -> Union[str, Any]: SCREAMING_SNAKE_CASE_ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*__magic_name__ ) def __A ( self : Dict ) -> Union[str, Any]: SCREAMING_SNAKE_CASE_ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_masked_lm(*__magic_name__ ) def __A ( self : Optional[int] ) -> List[Any]: SCREAMING_SNAKE_CASE_ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_image_classification(*__magic_name__ ) def __A ( self : Optional[Any] ) -> List[str]: SCREAMING_SNAKE_CASE_ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_semantic_segmentation(*__magic_name__ ) def __A ( self : int ) -> Optional[int]: if not self.model_tester.is_training: return SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = self.model_tester.prepare_config_and_inputs_for_common() SCREAMING_SNAKE_CASE_ = True for model_class in self.all_model_classes: # we don't test BeitForMaskedImageModeling if model_class in [*get_values(__magic_name__ ), BeitForMaskedImageModeling]: continue SCREAMING_SNAKE_CASE_ = model_class(__magic_name__ ) model.to(__magic_name__ ) model.train() SCREAMING_SNAKE_CASE_ = self._prepare_for_class(__magic_name__ , __magic_name__ , return_labels=__magic_name__ ) SCREAMING_SNAKE_CASE_ = model(**__magic_name__ ).loss loss.backward() def __A ( self : Any ) -> Tuple: SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = self.model_tester.prepare_config_and_inputs_for_common() if not self.model_tester.is_training: return SCREAMING_SNAKE_CASE_ = False SCREAMING_SNAKE_CASE_ = True for model_class in self.all_model_classes: # we don't test BeitForMaskedImageModeling if ( model_class in [*get_values(__magic_name__ ), BeitForMaskedImageModeling] or not model_class.supports_gradient_checkpointing ): continue SCREAMING_SNAKE_CASE_ = model_class(__magic_name__ ) model.gradient_checkpointing_enable() model.to(__magic_name__ ) model.train() SCREAMING_SNAKE_CASE_ = self._prepare_for_class(__magic_name__ , __magic_name__ , return_labels=__magic_name__ ) SCREAMING_SNAKE_CASE_ = model(**__magic_name__ ).loss loss.backward() def __A ( self : List[str] ) -> str: SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = self.model_tester.prepare_config_and_inputs_for_common() SCREAMING_SNAKE_CASE_ = _config_zero_init(__magic_name__ ) for model_class in self.all_model_classes: SCREAMING_SNAKE_CASE_ = model_class(config=__magic_name__ ) for name, param in model.named_parameters(): # we skip lambda parameters as these require special initial values # determined by config.layer_scale_init_value if "lambda" in name: continue if param.requires_grad: self.assertIn( ((param.data.mean() * 1e9).round() / 1e9).item() , [0.0, 1.0] , msg=F'''Parameter {name} of model {model_class} seems not properly initialized''' , ) @slow def __A ( self : int ) -> Optional[int]: for model_name in BEIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: SCREAMING_SNAKE_CASE_ = BeitModel.from_pretrained(__magic_name__ ) self.assertIsNotNone(__magic_name__ ) def a__ ( ): SCREAMING_SNAKE_CASE_ = Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png" ) return image @require_torch @require_vision class lowerCamelCase (unittest.TestCase ): """simple docstring""" @cached_property def __A ( self : List[Any] ) -> str: return BeitImageProcessor.from_pretrained("microsoft/beit-base-patch16-224" ) if is_vision_available() else None @slow def __A ( self : List[str] ) -> Union[str, Any]: SCREAMING_SNAKE_CASE_ = BeitForMaskedImageModeling.from_pretrained("microsoft/beit-base-patch16-224-pt22k" ).to(__magic_name__ ) SCREAMING_SNAKE_CASE_ = self.default_image_processor SCREAMING_SNAKE_CASE_ = prepare_img() SCREAMING_SNAKE_CASE_ = image_processor(images=__magic_name__ , return_tensors="pt" ).pixel_values.to(__magic_name__ ) # prepare bool_masked_pos SCREAMING_SNAKE_CASE_ = torch.ones((1, 196) , dtype=torch.bool ).to(__magic_name__ ) # forward pass with torch.no_grad(): SCREAMING_SNAKE_CASE_ = model(pixel_values=__magic_name__ , bool_masked_pos=__magic_name__ ) SCREAMING_SNAKE_CASE_ = outputs.logits # verify the logits SCREAMING_SNAKE_CASE_ = torch.Size((1, 196, 8_192) ) self.assertEqual(logits.shape , __magic_name__ ) SCREAMING_SNAKE_CASE_ = torch.tensor( [[-3.2437, 0.5072, -13.9174], [-3.2456, 0.4948, -13.9401], [-3.2033, 0.5121, -13.8550]] ).to(__magic_name__ ) self.assertTrue(torch.allclose(logits[bool_masked_pos][:3, :3] , __magic_name__ , atol=1e-2 ) ) @slow def __A ( self : Tuple ) -> int: SCREAMING_SNAKE_CASE_ = BeitForImageClassification.from_pretrained("microsoft/beit-base-patch16-224" ).to(__magic_name__ ) SCREAMING_SNAKE_CASE_ = self.default_image_processor SCREAMING_SNAKE_CASE_ = prepare_img() SCREAMING_SNAKE_CASE_ = image_processor(images=__magic_name__ , return_tensors="pt" ).to(__magic_name__ ) # forward pass with torch.no_grad(): SCREAMING_SNAKE_CASE_ = model(**__magic_name__ ) SCREAMING_SNAKE_CASE_ = outputs.logits # verify the logits SCREAMING_SNAKE_CASE_ = torch.Size((1, 1_000) ) self.assertEqual(logits.shape , __magic_name__ ) SCREAMING_SNAKE_CASE_ = torch.tensor([-1.2385, -1.0987, -1.0108] ).to(__magic_name__ ) self.assertTrue(torch.allclose(logits[0, :3] , __magic_name__ , atol=1e-4 ) ) SCREAMING_SNAKE_CASE_ = 281 self.assertEqual(logits.argmax(-1 ).item() , __magic_name__ ) @slow def __A ( self : Optional[Any] ) -> int: SCREAMING_SNAKE_CASE_ = BeitForImageClassification.from_pretrained("microsoft/beit-large-patch16-224-pt22k-ft22k" ).to( __magic_name__ ) SCREAMING_SNAKE_CASE_ = self.default_image_processor SCREAMING_SNAKE_CASE_ = prepare_img() SCREAMING_SNAKE_CASE_ = image_processor(images=__magic_name__ , return_tensors="pt" ).to(__magic_name__ ) # forward pass with torch.no_grad(): SCREAMING_SNAKE_CASE_ = model(**__magic_name__ ) SCREAMING_SNAKE_CASE_ = outputs.logits # verify the logits SCREAMING_SNAKE_CASE_ = torch.Size((1, 21_841) ) self.assertEqual(logits.shape , __magic_name__ ) SCREAMING_SNAKE_CASE_ = torch.tensor([1.6881, -0.2787, 0.5901] ).to(__magic_name__ ) self.assertTrue(torch.allclose(logits[0, :3] , __magic_name__ , atol=1e-4 ) ) SCREAMING_SNAKE_CASE_ = 2_396 self.assertEqual(logits.argmax(-1 ).item() , __magic_name__ ) @slow def __A ( self : Tuple ) -> Tuple: SCREAMING_SNAKE_CASE_ = BeitForSemanticSegmentation.from_pretrained("microsoft/beit-base-finetuned-ade-640-640" ) SCREAMING_SNAKE_CASE_ = model.to(__magic_name__ ) SCREAMING_SNAKE_CASE_ = BeitImageProcessor(do_resize=__magic_name__ , size=640 , do_center_crop=__magic_name__ ) SCREAMING_SNAKE_CASE_ = load_dataset("hf-internal-testing/fixtures_ade20k" , split="test" ) SCREAMING_SNAKE_CASE_ = Image.open(ds[0]["file"] ) SCREAMING_SNAKE_CASE_ = image_processor(images=__magic_name__ , return_tensors="pt" ).to(__magic_name__ ) # forward pass with torch.no_grad(): SCREAMING_SNAKE_CASE_ = model(**__magic_name__ ) SCREAMING_SNAKE_CASE_ = outputs.logits # verify the logits SCREAMING_SNAKE_CASE_ = torch.Size((1, 150, 160, 160) ) self.assertEqual(logits.shape , __magic_name__ ) SCREAMING_SNAKE_CASE_ = version.parse(PIL.__version__ ) < version.parse("9.0.0" ) if is_pillow_less_than_a: SCREAMING_SNAKE_CASE_ = torch.tensor( [ [[-4.9225, -2.3954, -3.0522], [-2.8822, -1.0046, -1.7561], [-2.9549, -1.3228, -2.1347]], [[-5.8168, -3.4129, -4.0778], [-3.8651, -2.2214, -3.0277], [-3.8356, -2.4643, -3.3535]], [[-0.0078, 3.9952, 4.0754], [2.9856, 4.6944, 5.0035], [3.2413, 4.7813, 4.9969]], ] , device=__magic_name__ , ) else: SCREAMING_SNAKE_CASE_ = torch.tensor( [ [[-4.8960, -2.3688, -3.0355], [-2.8478, -0.9836, -1.7418], [-2.9449, -1.3332, -2.1456]], [[-5.8081, -3.4124, -4.1006], [-3.8561, -2.2081, -3.0323], [-3.8365, -2.4601, -3.3669]], [[-0.0309, 3.9868, 4.0540], [2.9640, 4.6877, 4.9976], [3.2081, 4.7690, 4.9942]], ] , device=__magic_name__ , ) self.assertTrue(torch.allclose(logits[0, :3, :3, :3] , __magic_name__ , atol=1e-4 ) ) @slow def __A ( self : List[str] ) -> Tuple: SCREAMING_SNAKE_CASE_ = BeitForSemanticSegmentation.from_pretrained("microsoft/beit-base-finetuned-ade-640-640" ) SCREAMING_SNAKE_CASE_ = model.to(__magic_name__ ) SCREAMING_SNAKE_CASE_ = BeitImageProcessor(do_resize=__magic_name__ , size=640 , do_center_crop=__magic_name__ ) SCREAMING_SNAKE_CASE_ = load_dataset("hf-internal-testing/fixtures_ade20k" , split="test" ) SCREAMING_SNAKE_CASE_ = Image.open(ds[0]["file"] ) SCREAMING_SNAKE_CASE_ = image_processor(images=__magic_name__ , return_tensors="pt" ).to(__magic_name__ ) # forward pass with torch.no_grad(): SCREAMING_SNAKE_CASE_ = model(**__magic_name__ ) SCREAMING_SNAKE_CASE_ = outputs.logits.detach().cpu() SCREAMING_SNAKE_CASE_ = image_processor.post_process_semantic_segmentation(outputs=__magic_name__ , target_sizes=[(500, 300)] ) SCREAMING_SNAKE_CASE_ = torch.Size((500, 300) ) self.assertEqual(segmentation[0].shape , __magic_name__ ) SCREAMING_SNAKE_CASE_ = image_processor.post_process_semantic_segmentation(outputs=__magic_name__ ) SCREAMING_SNAKE_CASE_ = torch.Size((160, 160) ) self.assertEqual(segmentation[0].shape , __magic_name__ )
305
0
"""simple docstring""" import itertools from dataclasses import dataclass from typing import List, Optional import pyarrow as pa import pyarrow.parquet as pq import datasets from datasets.table import table_cast A : Optional[int] = datasets.utils.logging.get_logger(__name__) @dataclass class lowerCamelCase (datasets.BuilderConfig ): """simple docstring""" lowerCamelCase__ = 1_0_0_0_0 lowerCamelCase__ = None lowerCamelCase__ = None class lowerCamelCase (datasets.ArrowBasedBuilder ): """simple docstring""" lowerCamelCase__ = ParquetConfig def __A ( self : List[Any] ) -> Tuple: return datasets.DatasetInfo(features=self.config.features ) def __A ( self : Optional[int] , __magic_name__ : Any ) -> List[str]: if not self.config.data_files: raise ValueError(F'''At least one data file must be specified, but got data_files={self.config.data_files}''' ) SCREAMING_SNAKE_CASE_ = dl_manager.download_and_extract(self.config.data_files ) if isinstance(__magic_name__ , (str, list, tuple) ): SCREAMING_SNAKE_CASE_ = data_files if isinstance(__magic_name__ , __magic_name__ ): SCREAMING_SNAKE_CASE_ = [files] # Use `dl_manager.iter_files` to skip hidden files in an extracted archive SCREAMING_SNAKE_CASE_ = [dl_manager.iter_files(__magic_name__ ) for file in files] return [datasets.SplitGenerator(name=datasets.Split.TRAIN , gen_kwargs={"files": files} )] SCREAMING_SNAKE_CASE_ = [] for split_name, files in data_files.items(): if isinstance(__magic_name__ , __magic_name__ ): SCREAMING_SNAKE_CASE_ = [files] # Use `dl_manager.iter_files` to skip hidden files in an extracted archive SCREAMING_SNAKE_CASE_ = [dl_manager.iter_files(__magic_name__ ) for file in files] # Infer features is they are stoed in the arrow schema if self.info.features is None: for file in itertools.chain.from_iterable(__magic_name__ ): with open(__magic_name__ , "rb" ) as f: SCREAMING_SNAKE_CASE_ = datasets.Features.from_arrow_schema(pq.read_schema(__magic_name__ ) ) break splits.append(datasets.SplitGenerator(name=__magic_name__ , gen_kwargs={"files": files} ) ) return splits def __A ( self : List[str] , __magic_name__ : pa.Table ) -> List[Any]: if self.info.features is not None: # more expensive cast to support nested features with keys in a different order # allows str <-> int/float or str to Audio for example SCREAMING_SNAKE_CASE_ = table_cast(__magic_name__ , self.info.features.arrow_schema ) return pa_table def __A ( self : List[Any] , __magic_name__ : Any ) -> Any: SCREAMING_SNAKE_CASE_ = self.info.features.arrow_schema if self.info.features is not None else None if self.info.features is not None and self.config.columns is not None: if sorted(field.name for field in schema ) != sorted(self.config.columns ): raise ValueError( F'''Tried to load parquet data with columns \'{self.config.columns}\' with mismatching features \'{self.info.features}\'''' ) for file_idx, file in enumerate(itertools.chain.from_iterable(__magic_name__ ) ): with open(__magic_name__ , "rb" ) as f: SCREAMING_SNAKE_CASE_ = pq.ParquetFile(__magic_name__ ) try: for batch_idx, record_batch in enumerate( parquet_file.iter_batches(batch_size=self.config.batch_size , columns=self.config.columns ) ): SCREAMING_SNAKE_CASE_ = pa.Table.from_batches([record_batch] ) # Uncomment for debugging (will print the Arrow table size and elements) # logger.warning(f"pa_table: {pa_table} num rows: {pa_table.num_rows}") # logger.warning('\n'.join(str(pa_table.slice(i, 1).to_pydict()) for i in range(pa_table.num_rows))) yield F'''{file_idx}_{batch_idx}''', self._cast_table(__magic_name__ ) except ValueError as e: logger.error(F'''Failed to read file \'{file}\' with error {type(__magic_name__ )}: {e}''' ) raise
354
from __future__ import annotations A : Dict = "#" class lowerCamelCase : """simple docstring""" def __init__( self : Dict ) -> None: SCREAMING_SNAKE_CASE_ = {} def __A ( self : List[Any] , __magic_name__ : str ) -> None: SCREAMING_SNAKE_CASE_ = self._trie for char in text: if char not in trie: SCREAMING_SNAKE_CASE_ = {} SCREAMING_SNAKE_CASE_ = trie[char] SCREAMING_SNAKE_CASE_ = True def __A ( self : Union[str, Any] , __magic_name__ : str ) -> tuple | list: SCREAMING_SNAKE_CASE_ = self._trie for char in prefix: if char in trie: SCREAMING_SNAKE_CASE_ = trie[char] else: return [] return self._elements(__magic_name__ ) def __A ( self : int , __magic_name__ : dict ) -> tuple: SCREAMING_SNAKE_CASE_ = [] for c, v in d.items(): SCREAMING_SNAKE_CASE_ = [" "] if c == END else [(c + s) for s in self._elements(__magic_name__ )] result.extend(__magic_name__ ) return tuple(__magic_name__ ) A : Union[str, Any] = Trie() A : Optional[int] = ("depart", "detergent", "daring", "dog", "deer", "deal") for word in words: trie.insert_word(word) def a__ ( __UpperCamelCase ): SCREAMING_SNAKE_CASE_ = trie.find_word(__UpperCamelCase ) return tuple(string + word for word in suffixes ) def a__ ( ): print(autocomplete_using_trie("de" ) ) if __name__ == "__main__": import doctest doctest.testmod() main()
305
0
import json import os import shutil import tempfile import unittest import numpy as np import pytest from transformers import MgpstrTokenizer from transformers.models.mgp_str.tokenization_mgp_str import VOCAB_FILES_NAMES from transformers.testing_utils import require_torch, require_vision from transformers.utils import IMAGE_PROCESSOR_NAME, is_torch_available, is_vision_available if is_torch_available(): import torch if is_vision_available(): from PIL import Image from transformers import MgpstrProcessor, ViTImageProcessor @require_torch @require_vision class lowerCamelCase (unittest.TestCase ): """simple docstring""" lowerCamelCase__ = ViTImageProcessor if is_vision_available() else None @property def __A ( self : str ) -> Optional[int]: return self.image_processor_tester.prepare_image_processor_dict() def __A ( self : Dict ) -> Union[str, Any]: SCREAMING_SNAKE_CASE_ = (3, 32, 128) SCREAMING_SNAKE_CASE_ = tempfile.mkdtemp() # fmt: off SCREAMING_SNAKE_CASE_ = ["[GO]", "[s]", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"] # fmt: on SCREAMING_SNAKE_CASE_ = dict(zip(lowerCamelCase_ , range(len(lowerCamelCase_ ) ) ) ) SCREAMING_SNAKE_CASE_ = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES["vocab_file"] ) with open(self.vocab_file , "w" , encoding="utf-8" ) as fp: fp.write(json.dumps(lowerCamelCase_ ) + "\n" ) SCREAMING_SNAKE_CASE_ = { "do_normalize": False, "do_resize": True, "image_processor_type": "ViTImageProcessor", "resample": 3, "size": {"height": 32, "width": 128}, } SCREAMING_SNAKE_CASE_ = os.path.join(self.tmpdirname , lowerCamelCase_ ) with open(self.image_processor_file , "w" , encoding="utf-8" ) as fp: json.dump(lowerCamelCase_ , lowerCamelCase_ ) def __A ( self : List[Any] , **__magic_name__ : int ) -> Union[str, Any]: return MgpstrTokenizer.from_pretrained(self.tmpdirname , **lowerCamelCase_ ) def __A ( self : Optional[Any] , **__magic_name__ : Optional[int] ) -> List[str]: return ViTImageProcessor.from_pretrained(self.tmpdirname , **lowerCamelCase_ ) def __A ( self : int ) -> Dict: shutil.rmtree(self.tmpdirname ) def __A ( self : Union[str, Any] ) -> List[Any]: SCREAMING_SNAKE_CASE_ = np.random.randint(255 , size=(3, 30, 400) , dtype=np.uinta ) SCREAMING_SNAKE_CASE_ = Image.fromarray(np.moveaxis(lowerCamelCase_ , 0 , -1 ) ) return image_input def __A ( self : List[Any] ) -> Optional[Any]: SCREAMING_SNAKE_CASE_ = self.get_tokenizer() SCREAMING_SNAKE_CASE_ = self.get_image_processor() SCREAMING_SNAKE_CASE_ = MgpstrProcessor(tokenizer=lowerCamelCase_ , image_processor=lowerCamelCase_ ) processor.save_pretrained(self.tmpdirname ) SCREAMING_SNAKE_CASE_ = MgpstrProcessor.from_pretrained(self.tmpdirname , use_fast=lowerCamelCase_ ) self.assertEqual(processor.char_tokenizer.get_vocab() , tokenizer.get_vocab() ) self.assertIsInstance(processor.char_tokenizer , lowerCamelCase_ ) self.assertEqual(processor.image_processor.to_json_string() , image_processor.to_json_string() ) self.assertIsInstance(processor.image_processor , lowerCamelCase_ ) def __A ( self : List[str] ) -> Optional[Any]: SCREAMING_SNAKE_CASE_ = self.get_tokenizer() SCREAMING_SNAKE_CASE_ = self.get_image_processor() SCREAMING_SNAKE_CASE_ = MgpstrProcessor(tokenizer=lowerCamelCase_ , image_processor=lowerCamelCase_ ) processor.save_pretrained(self.tmpdirname ) SCREAMING_SNAKE_CASE_ = self.get_tokenizer(bos_token="(BOS)" , eos_token="(EOS)" ) SCREAMING_SNAKE_CASE_ = self.get_image_processor(do_normalize=lowerCamelCase_ , padding_value=1.0 ) SCREAMING_SNAKE_CASE_ = MgpstrProcessor.from_pretrained( self.tmpdirname , bos_token="(BOS)" , eos_token="(EOS)" , do_normalize=lowerCamelCase_ , padding_value=1.0 ) self.assertEqual(processor.char_tokenizer.get_vocab() , tokenizer_add_kwargs.get_vocab() ) self.assertIsInstance(processor.char_tokenizer , lowerCamelCase_ ) self.assertEqual(processor.image_processor.to_json_string() , image_processor_add_kwargs.to_json_string() ) self.assertIsInstance(processor.image_processor , lowerCamelCase_ ) def __A ( self : List[Any] ) -> Any: SCREAMING_SNAKE_CASE_ = self.get_image_processor() SCREAMING_SNAKE_CASE_ = self.get_tokenizer() SCREAMING_SNAKE_CASE_ = MgpstrProcessor(tokenizer=lowerCamelCase_ , image_processor=lowerCamelCase_ ) SCREAMING_SNAKE_CASE_ = self.prepare_image_inputs() SCREAMING_SNAKE_CASE_ = image_processor(lowerCamelCase_ , return_tensors="np" ) SCREAMING_SNAKE_CASE_ = processor(images=lowerCamelCase_ , 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 : str ) -> Union[str, Any]: SCREAMING_SNAKE_CASE_ = self.get_image_processor() SCREAMING_SNAKE_CASE_ = self.get_tokenizer() SCREAMING_SNAKE_CASE_ = MgpstrProcessor(tokenizer=lowerCamelCase_ , image_processor=lowerCamelCase_ ) SCREAMING_SNAKE_CASE_ = "test" SCREAMING_SNAKE_CASE_ = processor(text=lowerCamelCase_ ) SCREAMING_SNAKE_CASE_ = tokenizer(lowerCamelCase_ ) for key in encoded_tok.keys(): self.assertListEqual(encoded_tok[key] , encoded_processor[key] ) def __A ( self : List[Any] ) -> Union[str, Any]: SCREAMING_SNAKE_CASE_ = self.get_image_processor() SCREAMING_SNAKE_CASE_ = self.get_tokenizer() SCREAMING_SNAKE_CASE_ = MgpstrProcessor(tokenizer=lowerCamelCase_ , image_processor=lowerCamelCase_ ) SCREAMING_SNAKE_CASE_ = "test" SCREAMING_SNAKE_CASE_ = self.prepare_image_inputs() SCREAMING_SNAKE_CASE_ = processor(text=lowerCamelCase_ , images=lowerCamelCase_ ) self.assertListEqual(list(inputs.keys() ) , ["pixel_values", "labels"] ) # test if it raises when no input is passed with pytest.raises(lowerCamelCase_ ): processor() def __A ( self : Optional[int] ) -> Dict: SCREAMING_SNAKE_CASE_ = self.get_image_processor() SCREAMING_SNAKE_CASE_ = self.get_tokenizer() SCREAMING_SNAKE_CASE_ = MgpstrProcessor(tokenizer=lowerCamelCase_ , image_processor=lowerCamelCase_ ) SCREAMING_SNAKE_CASE_ = [[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9], [3, 4, 3, 1, 1, 8, 9]] SCREAMING_SNAKE_CASE_ = processor.char_decode(lowerCamelCase_ ) SCREAMING_SNAKE_CASE_ = tokenizer.batch_decode(lowerCamelCase_ ) SCREAMING_SNAKE_CASE_ = [seq.replace(" " , "" ) for seq in decoded_tok] self.assertListEqual(lowerCamelCase_ , lowerCamelCase_ ) def __A ( self : Optional[int] ) -> Dict: SCREAMING_SNAKE_CASE_ = self.get_image_processor() SCREAMING_SNAKE_CASE_ = self.get_tokenizer() SCREAMING_SNAKE_CASE_ = MgpstrProcessor(tokenizer=lowerCamelCase_ , image_processor=lowerCamelCase_ ) SCREAMING_SNAKE_CASE_ = None SCREAMING_SNAKE_CASE_ = self.prepare_image_inputs() SCREAMING_SNAKE_CASE_ = processor(text=lowerCamelCase_ , images=lowerCamelCase_ ) self.assertListEqual(list(inputs.keys() ) , processor.model_input_names ) def __A ( self : str ) -> Tuple: SCREAMING_SNAKE_CASE_ = self.get_image_processor() SCREAMING_SNAKE_CASE_ = self.get_tokenizer() SCREAMING_SNAKE_CASE_ = MgpstrProcessor(tokenizer=lowerCamelCase_ , image_processor=lowerCamelCase_ ) SCREAMING_SNAKE_CASE_ = torch.randn(1 , 27 , 38 ) SCREAMING_SNAKE_CASE_ = torch.randn(1 , 27 , 50_257 ) SCREAMING_SNAKE_CASE_ = torch.randn(1 , 27 , 30_522 ) SCREAMING_SNAKE_CASE_ = processor.batch_decode([char_input, bpe_input, wp_input] ) self.assertListEqual(list(results.keys() ) , ["generated_text", "scores", "char_preds", "bpe_preds", "wp_preds"] )
355
from collections import deque class lowerCamelCase : """simple docstring""" def __init__( self : str , __magic_name__ : str , __magic_name__ : int , __magic_name__ : int ) -> None: SCREAMING_SNAKE_CASE_ = process_name # process name SCREAMING_SNAKE_CASE_ = arrival_time # arrival time of the process # completion time of finished process or last interrupted time SCREAMING_SNAKE_CASE_ = arrival_time SCREAMING_SNAKE_CASE_ = burst_time # remaining burst time SCREAMING_SNAKE_CASE_ = 0 # total time of the process wait in ready queue SCREAMING_SNAKE_CASE_ = 0 # time from arrival time to completion time class lowerCamelCase : """simple docstring""" def __init__( self : Tuple , __magic_name__ : int , __magic_name__ : list[int] , __magic_name__ : deque[Process] , __magic_name__ : int , ) -> None: # total number of mlfq's queues SCREAMING_SNAKE_CASE_ = number_of_queues # time slice of queues that round robin algorithm applied SCREAMING_SNAKE_CASE_ = time_slices # unfinished process is in this ready_queue SCREAMING_SNAKE_CASE_ = queue # current time SCREAMING_SNAKE_CASE_ = current_time # finished process is in this sequence queue SCREAMING_SNAKE_CASE_ = deque() def __A ( self : Dict ) -> list[str]: SCREAMING_SNAKE_CASE_ = [] for i in range(len(self.finish_queue ) ): sequence.append(self.finish_queue[i].process_name ) return sequence def __A ( self : List[str] , __magic_name__ : list[Process] ) -> list[int]: SCREAMING_SNAKE_CASE_ = [] for i in range(len(__magic_name__ ) ): waiting_times.append(queue[i].waiting_time ) return waiting_times def __A ( self : List[str] , __magic_name__ : list[Process] ) -> list[int]: SCREAMING_SNAKE_CASE_ = [] for i in range(len(__magic_name__ ) ): turnaround_times.append(queue[i].turnaround_time ) return turnaround_times def __A ( self : Tuple , __magic_name__ : list[Process] ) -> list[int]: SCREAMING_SNAKE_CASE_ = [] for i in range(len(__magic_name__ ) ): completion_times.append(queue[i].stop_time ) return completion_times def __A ( self : str , __magic_name__ : deque[Process] ) -> list[int]: return [q.burst_time for q in queue] def __A ( self : Optional[Any] , __magic_name__ : Process ) -> int: process.waiting_time += self.current_time - process.stop_time return process.waiting_time def __A ( self : Optional[Any] , __magic_name__ : deque[Process] ) -> deque[Process]: SCREAMING_SNAKE_CASE_ = deque() # sequence deque of finished process while len(__magic_name__ ) != 0: SCREAMING_SNAKE_CASE_ = ready_queue.popleft() # current process # if process's arrival time is later than current time, update current time if self.current_time < cp.arrival_time: self.current_time += cp.arrival_time # update waiting time of current process self.update_waiting_time(__magic_name__ ) # update current time self.current_time += cp.burst_time # finish the process and set the process's burst-time 0 SCREAMING_SNAKE_CASE_ = 0 # set the process's turnaround time because it is finished SCREAMING_SNAKE_CASE_ = self.current_time - cp.arrival_time # set the completion time SCREAMING_SNAKE_CASE_ = self.current_time # add the process to queue that has finished queue finished.append(__magic_name__ ) self.finish_queue.extend(__magic_name__ ) # add finished process to finish queue # FCFS will finish all remaining processes return finished def __A ( self : Any , __magic_name__ : deque[Process] , __magic_name__ : int ) -> tuple[deque[Process], deque[Process]]: SCREAMING_SNAKE_CASE_ = deque() # sequence deque of terminated process # just for 1 cycle and unfinished processes will go back to queue for _ in range(len(__magic_name__ ) ): SCREAMING_SNAKE_CASE_ = ready_queue.popleft() # current process # if process's arrival time is later than current time, update current time if self.current_time < cp.arrival_time: self.current_time += cp.arrival_time # update waiting time of unfinished processes self.update_waiting_time(__magic_name__ ) # if the burst time of process is bigger than time-slice if cp.burst_time > time_slice: # use CPU for only time-slice self.current_time += time_slice # update remaining burst time cp.burst_time -= time_slice # update end point time SCREAMING_SNAKE_CASE_ = self.current_time # locate the process behind the queue because it is not finished ready_queue.append(__magic_name__ ) else: # use CPU for remaining burst time self.current_time += cp.burst_time # set burst time 0 because the process is finished SCREAMING_SNAKE_CASE_ = 0 # set the finish time SCREAMING_SNAKE_CASE_ = self.current_time # update the process' turnaround time because it is finished SCREAMING_SNAKE_CASE_ = self.current_time - cp.arrival_time # add the process to queue that has finished queue finished.append(__magic_name__ ) self.finish_queue.extend(__magic_name__ ) # add finished process to finish queue # return finished processes queue and remaining processes queue return finished, ready_queue def __A ( self : Any ) -> deque[Process]: # all queues except last one have round_robin algorithm for i in range(self.number_of_queues - 1 ): SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = self.round_robin( self.ready_queue , self.time_slices[i] ) # the last queue has first_come_first_served algorithm self.first_come_first_served(self.ready_queue ) return self.finish_queue if __name__ == "__main__": import doctest A : Dict = Process("P1", 0, 53) A : str = Process("P2", 0, 17) A : List[Any] = Process("P3", 0, 68) A : List[str] = Process("P4", 0, 24) A : Dict = 3 A : Any = [17, 25] A : Dict = deque([Pa, Pa, Pa, Pa]) if len(time_slices) != number_of_queues - 1: raise SystemExit(0) doctest.testmod(extraglobs={"queue": deque([Pa, Pa, Pa, Pa])}) A : Union[str, Any] = Process("P1", 0, 53) A : Any = Process("P2", 0, 17) A : Dict = Process("P3", 0, 68) A : List[str] = Process("P4", 0, 24) A : Optional[int] = 3 A : int = [17, 25] A : Union[str, Any] = deque([Pa, Pa, Pa, Pa]) A : Tuple = MLFQ(number_of_queues, time_slices, queue, 0) A : Tuple = mlfq.multi_level_feedback_queue() # print total waiting times of processes(P1, P2, P3, P4) print( f"waiting time:\ \t\t\t{MLFQ.calculate_waiting_time(mlfq, [Pa, Pa, Pa, Pa])}" ) # print completion times of processes(P1, P2, P3, P4) print( f"completion time:\ \t\t{MLFQ.calculate_completion_time(mlfq, [Pa, Pa, Pa, Pa])}" ) # print total turnaround times of processes(P1, P2, P3, P4) print( f"turnaround time:\ \t\t{MLFQ.calculate_turnaround_time(mlfq, [Pa, Pa, Pa, Pa])}" ) # print sequence of finished processes print( f"sequence of finished processes:\ {mlfq.calculate_sequence_of_finish_queue()}" )
305
0
"""simple docstring""" import importlib import inspect import os import re # All paths are set with the intent you should run this script from the root of the repo with the command # python utils/check_config_docstrings.py A : Union[str, Any] = 'src/transformers' # This is to make sure the transformers module imported is the one in the repo. A : Any = importlib.util.spec_from_file_location( "transformers", os.path.join(PATH_TO_TRANSFORMERS, "__init__.py"), submodule_search_locations=[PATH_TO_TRANSFORMERS], ) A : int = spec.loader.load_module() A : Dict = transformers.models.auto.configuration_auto.CONFIG_MAPPING # Regex pattern used to find the checkpoint mentioned in the docstring of `config_class`. # For example, `[bert-base-uncased](https://huggingface.co/bert-base-uncased)` A : str = re.compile("\[(.+?)\]\((https://huggingface\.co/.+?)\)") A : str = { 'CLIPConfigMixin', 'DecisionTransformerConfigMixin', 'EncoderDecoderConfigMixin', 'RagConfigMixin', 'SpeechEncoderDecoderConfigMixin', 'VisionEncoderDecoderConfigMixin', 'VisionTextDualEncoderConfigMixin', } def a__ ( ): SCREAMING_SNAKE_CASE_ = [] for config_class in list(CONFIG_MAPPING.values() ): SCREAMING_SNAKE_CASE_ = False # source code of `config_class` SCREAMING_SNAKE_CASE_ = inspect.getsource(__UpperCAmelCase ) SCREAMING_SNAKE_CASE_ = _re_checkpoint.findall(__UpperCAmelCase ) for checkpoint in checkpoints: # Each `checkpoint` is a tuple of a checkpoint name and a checkpoint link. # For example, `('bert-base-uncased', 'https://huggingface.co/bert-base-uncased')` SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = checkpoint # verify the checkpoint name corresponds to the checkpoint link SCREAMING_SNAKE_CASE_ = F'''https://huggingface.co/{ckpt_name}''' if ckpt_link == ckpt_link_from_name: SCREAMING_SNAKE_CASE_ = True break SCREAMING_SNAKE_CASE_ = config_class.__name__ if not checkpoint_found and name not in CONFIG_CLASSES_TO_IGNORE_FOR_DOCSTRING_CHECKPOINT_CHECK: configs_without_checkpoint.append(__UpperCAmelCase ) if len(__UpperCAmelCase ) > 0: SCREAMING_SNAKE_CASE_ = "\n".join(sorted(__UpperCAmelCase ) ) raise ValueError(F'''The following configurations don\'t contain any valid checkpoint:\n{message}''' ) if __name__ == "__main__": check_config_docstrings_have_checkpoints()
356
import torch def a__ ( ): if torch.cuda.is_available(): SCREAMING_SNAKE_CASE_ = torch.cuda.device_count() else: SCREAMING_SNAKE_CASE_ = 0 print(F'''Successfully ran on {num_gpus} GPUs''' ) if __name__ == "__main__": main()
305
0
import os import unicodedata from shutil import copyfile from typing import Any, Dict, List, Optional, Tuple import sentencepiece as spm from ...tokenization_utils import AddedToken, PreTrainedTokenizer from ...utils import SPIECE_UNDERLINE, logging A : Optional[int] = logging.get_logger(__name__) A : Any = {'''vocab_file''': '''spiece.model'''} A : Tuple = { '''vocab_file''': { '''TsinghuaAI/CPM-Generate''': '''https://huggingface.co/TsinghuaAI/CPM-Generate/resolve/main/spiece.model''', } } class lowerCamelCase (A__ ): """simple docstring""" def __init__( self : int , __magic_name__ : List[Any] , __magic_name__ : List[str]=False , __magic_name__ : Optional[int]=True , __magic_name__ : Any=False , __magic_name__ : List[Any]="<s>" , __magic_name__ : str="</s>" , __magic_name__ : Dict="<unk>" , __magic_name__ : Any="<sep>" , __magic_name__ : Dict="<pad>" , __magic_name__ : Optional[Any]="<cls>" , __magic_name__ : Optional[int]="<mask>" , __magic_name__ : List[str]=["<eop>", "<eod>"] , __magic_name__ : str = None , **__magic_name__ : int , ) -> Union[str, Any]: SCREAMING_SNAKE_CASE_ = AddedToken(lowerCamelCase__ , lstrip=lowerCamelCase__ , rstrip=lowerCamelCase__ ) if isinstance(lowerCamelCase__ , lowerCamelCase__ ) else mask_token SCREAMING_SNAKE_CASE_ = {} if sp_model_kwargs is None else sp_model_kwargs super().__init__( do_lower_case=lowerCamelCase__ , remove_space=lowerCamelCase__ , keep_accents=lowerCamelCase__ , bos_token=lowerCamelCase__ , eos_token=lowerCamelCase__ , unk_token=lowerCamelCase__ , sep_token=lowerCamelCase__ , pad_token=lowerCamelCase__ , cls_token=lowerCamelCase__ , mask_token=lowerCamelCase__ , additional_special_tokens=lowerCamelCase__ , sp_model_kwargs=self.sp_model_kwargs , **lowerCamelCase__ , ) SCREAMING_SNAKE_CASE_ = 3 SCREAMING_SNAKE_CASE_ = do_lower_case SCREAMING_SNAKE_CASE_ = remove_space SCREAMING_SNAKE_CASE_ = keep_accents SCREAMING_SNAKE_CASE_ = vocab_file SCREAMING_SNAKE_CASE_ = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(lowerCamelCase__ ) try: import jieba except ModuleNotFoundError as error: raise error.__class__( "You need to install jieba to use CpmTokenizer or CpmTokenizerFast. " "See https://pypi.org/project/jieba/ for installation." ) SCREAMING_SNAKE_CASE_ = jieba SCREAMING_SNAKE_CASE_ = str.maketrans(" \n" , "\u2582\u2583" ) @property # Copied from transformers.models.xlnet.tokenization_xlnet.XLNetTokenizer.vocab_size def __A ( self : List[str] ) -> List[Any]: return len(self.sp_model ) def __A ( self : List[Any] ) -> Tuple: SCREAMING_SNAKE_CASE_ = {self.convert_ids_to_tokens(lowerCamelCase__ ): i for i in range(self.vocab_size )} vocab.update(self.added_tokens_encoder ) return vocab def __getstate__( self : Optional[int] ) -> List[str]: SCREAMING_SNAKE_CASE_ = self.__dict__.copy() SCREAMING_SNAKE_CASE_ = None return state def __setstate__( self : str , __magic_name__ : Union[str, Any] ) -> Any: SCREAMING_SNAKE_CASE_ = d # for backward compatibility if not hasattr(self , "sp_model_kwargs" ): SCREAMING_SNAKE_CASE_ = {} SCREAMING_SNAKE_CASE_ = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(self.vocab_file ) def __A ( self : int , __magic_name__ : Any ) -> List[Any]: if self.remove_space: SCREAMING_SNAKE_CASE_ = " ".join(inputs.strip().split() ) else: SCREAMING_SNAKE_CASE_ = inputs SCREAMING_SNAKE_CASE_ = outputs.replace("``" , "\"" ).replace("\'\'" , "\"" ) if not self.keep_accents: SCREAMING_SNAKE_CASE_ = unicodedata.normalize("NFKD" , lowerCamelCase__ ) SCREAMING_SNAKE_CASE_ = "".join([c for c in outputs if not unicodedata.combining(lowerCamelCase__ )] ) if self.do_lower_case: SCREAMING_SNAKE_CASE_ = outputs.lower() return outputs def __A ( self : int , __magic_name__ : Tuple ) -> List[str]: SCREAMING_SNAKE_CASE_ = self.preprocess_text(lowerCamelCase__ ) SCREAMING_SNAKE_CASE_ = self.sp_model.encode(lowerCamelCase__ , out_type=lowerCamelCase__ ) SCREAMING_SNAKE_CASE_ = [] for piece in pieces: if len(lowerCamelCase__ ) > 1 and piece[-1] == str("," ) and piece[-2].isdigit(): SCREAMING_SNAKE_CASE_ = self.sp_model.EncodeAsPieces(piece[:-1].replace(lowerCamelCase__ , "" ) ) if piece[0] != SPIECE_UNDERLINE and cur_pieces[0][0] == SPIECE_UNDERLINE: if len(cur_pieces[0] ) == 1: SCREAMING_SNAKE_CASE_ = cur_pieces[1:] else: SCREAMING_SNAKE_CASE_ = cur_pieces[0][1:] cur_pieces.append(piece[-1] ) new_pieces.extend(lowerCamelCase__ ) else: new_pieces.append(lowerCamelCase__ ) return new_pieces def __A ( self : Dict , __magic_name__ : int ) -> int: return self.sp_model.PieceToId(lowerCamelCase__ ) def __A ( self : Tuple , __magic_name__ : str ) -> Union[str, Any]: return self.sp_model.IdToPiece(lowerCamelCase__ ) def __A ( self : List[str] , __magic_name__ : int ) -> Union[str, Any]: SCREAMING_SNAKE_CASE_ = "".join(lowerCamelCase__ ).replace(lowerCamelCase__ , " " ).strip() return out_string def __A ( self : List[str] , __magic_name__ : Any , __magic_name__ : List[Any] = None ) -> List[Any]: SCREAMING_SNAKE_CASE_ = [self.sep_token_id] SCREAMING_SNAKE_CASE_ = [self.cls_token_id] if token_ids_a is None: return token_ids_a + sep + cls return token_ids_a + sep + token_ids_a + sep + cls def __A ( self : Any , __magic_name__ : Optional[int] , __magic_name__ : Any = None , __magic_name__ : List[Any] = False ) -> Union[str, Any]: if already_has_special_tokens: return super().get_special_tokens_mask( token_ids_a=lowerCamelCase__ , token_ids_a=lowerCamelCase__ , already_has_special_tokens=lowerCamelCase__ ) if token_ids_a is not None: return ([0] * len(lowerCamelCase__ )) + [1] + ([0] * len(lowerCamelCase__ )) + [1, 1] return ([0] * len(lowerCamelCase__ )) + [1, 1] def __A ( self : Optional[Any] , __magic_name__ : List[Any] , __magic_name__ : Dict = None ) -> Union[str, Any]: SCREAMING_SNAKE_CASE_ = [self.sep_token_id] SCREAMING_SNAKE_CASE_ = [2] if token_ids_a is None: return len(token_ids_a + sep ) * [0] + cls_segment_id return len(token_ids_a + sep ) * [0] + len(token_ids_a + sep ) * [1] + cls_segment_id def __A ( self : Optional[int] , __magic_name__ : Union[str, Any] , __magic_name__ : Optional[Any] = None ) -> str: if not os.path.isdir(lowerCamelCase__ ): logger.error(F'''Vocabulary path ({save_directory}) should be a directory''' ) return SCREAMING_SNAKE_CASE_ = os.path.join( lowerCamelCase__ , (filename_prefix + "-" if filename_prefix else "") + VOCAB_FILES_NAMES["vocab_file"] ) if os.path.abspath(self.vocab_file ) != os.path.abspath(lowerCamelCase__ ) and os.path.isfile(self.vocab_file ): copyfile(self.vocab_file , lowerCamelCase__ ) elif not os.path.isfile(self.vocab_file ): with open(lowerCamelCase__ , "wb" ) as fi: SCREAMING_SNAKE_CASE_ = self.sp_model.serialized_model_proto() fi.write(lowerCamelCase__ ) return (out_vocab_file,) def __A ( self : Any , *__magic_name__ : Optional[int] , **__magic_name__ : int ) -> Dict: SCREAMING_SNAKE_CASE_ = super()._decode(*lowerCamelCase__ , **lowerCamelCase__ ) SCREAMING_SNAKE_CASE_ = text.replace(" " , "" ).replace("\u2582" , " " ).replace("\u2583" , "\n" ) return text
357
from collections.abc import Generator from math import sin def a__ ( __UpperCamelCase ): if len(__UpperCamelCase ) != 3_2: raise ValueError("Input must be of length 32" ) SCREAMING_SNAKE_CASE_ = b"" for i in [3, 2, 1, 0]: little_endian += string_aa[8 * i : 8 * i + 8] return little_endian def a__ ( __UpperCamelCase ): if i < 0: raise ValueError("Input must be non-negative" ) SCREAMING_SNAKE_CASE_ = format(__UpperCamelCase , "08x" )[-8:] SCREAMING_SNAKE_CASE_ = b"" for i in [3, 2, 1, 0]: little_endian_hex += hex_rep[2 * i : 2 * i + 2].encode("utf-8" ) return little_endian_hex def a__ ( __UpperCamelCase ): SCREAMING_SNAKE_CASE_ = b"" for char in message: bit_string += format(__UpperCamelCase , "08b" ).encode("utf-8" ) SCREAMING_SNAKE_CASE_ = format(len(__UpperCamelCase ) , "064b" ).encode("utf-8" ) # Pad bit_string to a multiple of 512 chars bit_string += b"1" while len(__UpperCamelCase ) % 5_1_2 != 4_4_8: bit_string += b"0" bit_string += to_little_endian(start_len[3_2:] ) + to_little_endian(start_len[:3_2] ) return bit_string def a__ ( __UpperCamelCase ): if len(__UpperCamelCase ) % 5_1_2 != 0: raise ValueError("Input must have length that's a multiple of 512" ) for pos in range(0 , len(__UpperCamelCase ) , 5_1_2 ): SCREAMING_SNAKE_CASE_ = bit_string[pos : pos + 5_1_2] SCREAMING_SNAKE_CASE_ = [] for i in range(0 , 5_1_2 , 3_2 ): block_words.append(int(to_little_endian(block[i : i + 3_2] ) , 2 ) ) yield block_words def a__ ( __UpperCamelCase ): if i < 0: raise ValueError("Input must be non-negative" ) SCREAMING_SNAKE_CASE_ = format(__UpperCamelCase , "032b" ) SCREAMING_SNAKE_CASE_ = "" for c in i_str: new_str += "1" if c == "0" else "0" return int(__UpperCamelCase , 2 ) def a__ ( __UpperCamelCase , __UpperCamelCase ): return (a + b) % 2**3_2 def a__ ( __UpperCamelCase , __UpperCamelCase ): if i < 0: raise ValueError("Input must be non-negative" ) if shift < 0: raise ValueError("Shift must be non-negative" ) return ((i << shift) ^ (i >> (3_2 - shift))) % 2**3_2 def a__ ( __UpperCamelCase ): SCREAMING_SNAKE_CASE_ = preprocess(__UpperCamelCase ) SCREAMING_SNAKE_CASE_ = [int(2**3_2 * abs(sin(i + 1 ) ) ) for i in range(6_4 )] # Starting states SCREAMING_SNAKE_CASE_ = 0X67452301 SCREAMING_SNAKE_CASE_ = 0Xefcdab89 SCREAMING_SNAKE_CASE_ = 0X98badcfe SCREAMING_SNAKE_CASE_ = 0X10325476 SCREAMING_SNAKE_CASE_ = [ 7, 1_2, 1_7, 2_2, 7, 1_2, 1_7, 2_2, 7, 1_2, 1_7, 2_2, 7, 1_2, 1_7, 2_2, 5, 9, 1_4, 2_0, 5, 9, 1_4, 2_0, 5, 9, 1_4, 2_0, 5, 9, 1_4, 2_0, 4, 1_1, 1_6, 2_3, 4, 1_1, 1_6, 2_3, 4, 1_1, 1_6, 2_3, 4, 1_1, 1_6, 2_3, 6, 1_0, 1_5, 2_1, 6, 1_0, 1_5, 2_1, 6, 1_0, 1_5, 2_1, 6, 1_0, 1_5, 2_1, ] # Process bit string in chunks, each with 16 32-char words for block_words in get_block_words(__UpperCamelCase ): SCREAMING_SNAKE_CASE_ = aa SCREAMING_SNAKE_CASE_ = ba SCREAMING_SNAKE_CASE_ = ca SCREAMING_SNAKE_CASE_ = da # Hash current chunk for i in range(6_4 ): if i <= 1_5: # f = (b & c) | (not_32(b) & d) # Alternate definition for f SCREAMING_SNAKE_CASE_ = d ^ (b & (c ^ d)) SCREAMING_SNAKE_CASE_ = i elif i <= 3_1: # f = (d & b) | (not_32(d) & c) # Alternate definition for f SCREAMING_SNAKE_CASE_ = c ^ (d & (b ^ c)) SCREAMING_SNAKE_CASE_ = (5 * i + 1) % 1_6 elif i <= 4_7: SCREAMING_SNAKE_CASE_ = b ^ c ^ d SCREAMING_SNAKE_CASE_ = (3 * i + 5) % 1_6 else: SCREAMING_SNAKE_CASE_ = c ^ (b | not_aa(__UpperCamelCase )) SCREAMING_SNAKE_CASE_ = (7 * i) % 1_6 SCREAMING_SNAKE_CASE_ = (f + a + added_consts[i] + block_words[g]) % 2**3_2 SCREAMING_SNAKE_CASE_ = d SCREAMING_SNAKE_CASE_ = c SCREAMING_SNAKE_CASE_ = b SCREAMING_SNAKE_CASE_ = sum_aa(__UpperCamelCase , left_rotate_aa(__UpperCamelCase , shift_amounts[i] ) ) # Add hashed chunk to running total SCREAMING_SNAKE_CASE_ = sum_aa(__UpperCamelCase , __UpperCamelCase ) SCREAMING_SNAKE_CASE_ = sum_aa(__UpperCamelCase , __UpperCamelCase ) SCREAMING_SNAKE_CASE_ = sum_aa(__UpperCamelCase , __UpperCamelCase ) SCREAMING_SNAKE_CASE_ = sum_aa(__UpperCamelCase , __UpperCamelCase ) SCREAMING_SNAKE_CASE_ = reformat_hex(__UpperCamelCase ) + reformat_hex(__UpperCamelCase ) + reformat_hex(__UpperCamelCase ) + reformat_hex(__UpperCamelCase ) return digest if __name__ == "__main__": import doctest doctest.testmod()
305
0
import os import sys import unittest A : int = os.path.abspath(os.path.dirname(os.path.dirname(os.path.dirname(__file__)))) sys.path.append(os.path.join(git_repo_path, "utils")) import check_dummies # noqa: E402 from check_dummies import create_dummy_files, create_dummy_object, find_backend, read_init # noqa: E402 # Align TRANSFORMERS_PATH in check_dummies with the current path A : Tuple = os.path.join(git_repo_path, "src", "diffusers") class lowerCamelCase (unittest.TestCase ): """simple docstring""" def __A ( self : str ) -> Union[str, Any]: SCREAMING_SNAKE_CASE_ = find_backend(" if not is_torch_available():" ) self.assertEqual(lowercase_ , "torch" ) # backend_with_underscore = find_backend(" if not is_tensorflow_text_available():") # self.assertEqual(backend_with_underscore, "tensorflow_text") SCREAMING_SNAKE_CASE_ = find_backend(" if not (is_torch_available() and is_transformers_available()):" ) self.assertEqual(lowercase_ , "torch_and_transformers" ) # double_backend_with_underscore = find_backend( # " if not (is_sentencepiece_available() and is_tensorflow_text_available()):" # ) # self.assertEqual(double_backend_with_underscore, "sentencepiece_and_tensorflow_text") SCREAMING_SNAKE_CASE_ = find_backend( " if not (is_torch_available() and is_transformers_available() and is_onnx_available()):" ) self.assertEqual(lowercase_ , "torch_and_transformers_and_onnx" ) def __A ( self : str ) -> Dict: SCREAMING_SNAKE_CASE_ = read_init() # We don't assert on the exact list of keys to allow for smooth grow of backend-specific objects self.assertIn("torch" , lowercase_ ) self.assertIn("torch_and_transformers" , lowercase_ ) self.assertIn("flax_and_transformers" , lowercase_ ) self.assertIn("torch_and_transformers_and_onnx" , lowercase_ ) # Likewise, we can't assert on the exact content of a key self.assertIn("UNet2DModel" , objects["torch"] ) self.assertIn("FlaxUNet2DConditionModel" , objects["flax"] ) self.assertIn("StableDiffusionPipeline" , objects["torch_and_transformers"] ) self.assertIn("FlaxStableDiffusionPipeline" , objects["flax_and_transformers"] ) self.assertIn("LMSDiscreteScheduler" , objects["torch_and_scipy"] ) self.assertIn("OnnxStableDiffusionPipeline" , objects["torch_and_transformers_and_onnx"] ) def __A ( self : int ) -> int: SCREAMING_SNAKE_CASE_ = create_dummy_object("CONSTANT" , "\'torch\'" ) self.assertEqual(lowercase_ , "\nCONSTANT = None\n" ) SCREAMING_SNAKE_CASE_ = create_dummy_object("function" , "\'torch\'" ) self.assertEqual( lowercase_ , "\ndef function(*args, **kwargs):\n requires_backends(function, \'torch\')\n" ) SCREAMING_SNAKE_CASE_ = ''' class FakeClass(metaclass=DummyObject): _backends = \'torch\' def __init__(self, *args, **kwargs): requires_backends(self, \'torch\') @classmethod def from_config(cls, *args, **kwargs): requires_backends(cls, \'torch\') @classmethod def from_pretrained(cls, *args, **kwargs): requires_backends(cls, \'torch\') ''' SCREAMING_SNAKE_CASE_ = create_dummy_object("FakeClass" , "\'torch\'" ) self.assertEqual(lowercase_ , lowercase_ ) def __A ( self : Optional[Any] ) -> Dict: SCREAMING_SNAKE_CASE_ = '''# This file is autogenerated by the command `make fix-copies`, do not edit. from ..utils import DummyObject, requires_backends CONSTANT = None def function(*args, **kwargs): requires_backends(function, ["torch"]) class FakeClass(metaclass=DummyObject): _backends = ["torch"] def __init__(self, *args, **kwargs): requires_backends(self, ["torch"]) @classmethod def from_config(cls, *args, **kwargs): requires_backends(cls, ["torch"]) @classmethod def from_pretrained(cls, *args, **kwargs): requires_backends(cls, ["torch"]) ''' SCREAMING_SNAKE_CASE_ = create_dummy_files({"torch": ["CONSTANT", "function", "FakeClass"]} ) self.assertEqual(dummy_files["torch"] , lowercase_ )
358
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, BlipaProcessor, BlipImageProcessor, GPTaTokenizer, PreTrainedTokenizerFast @require_vision class lowerCamelCase (unittest.TestCase ): """simple docstring""" def __A ( self : int ) -> Any: SCREAMING_SNAKE_CASE_ = tempfile.mkdtemp() SCREAMING_SNAKE_CASE_ = BlipImageProcessor() SCREAMING_SNAKE_CASE_ = GPTaTokenizer.from_pretrained("hf-internal-testing/tiny-random-GPT2Model" ) SCREAMING_SNAKE_CASE_ = BlipaProcessor(__magic_name__ , __magic_name__ ) processor.save_pretrained(self.tmpdirname ) def __A ( self : str , **__magic_name__ : int ) -> Union[str, Any]: return AutoProcessor.from_pretrained(self.tmpdirname , **__magic_name__ ).tokenizer def __A ( self : Dict , **__magic_name__ : List[Any] ) -> int: return AutoProcessor.from_pretrained(self.tmpdirname , **__magic_name__ ).image_processor def __A ( self : int ) -> Any: shutil.rmtree(self.tmpdirname ) def __A ( self : Dict ) -> Dict: SCREAMING_SNAKE_CASE_ = [np.random.randint(255 , size=(3, 30, 400) , dtype=np.uinta )] SCREAMING_SNAKE_CASE_ = [Image.fromarray(np.moveaxis(__magic_name__ , 0 , -1 ) ) for x in image_inputs] return image_inputs def __A ( self : List[Any] ) -> Union[str, Any]: SCREAMING_SNAKE_CASE_ = BlipaProcessor(tokenizer=self.get_tokenizer() , image_processor=self.get_image_processor() ) processor.save_pretrained(self.tmpdirname ) SCREAMING_SNAKE_CASE_ = self.get_tokenizer(bos_token="(BOS)" , eos_token="(EOS)" ) SCREAMING_SNAKE_CASE_ = self.get_image_processor(do_normalize=__magic_name__ , padding_value=1.0 ) SCREAMING_SNAKE_CASE_ = BlipaProcessor.from_pretrained( self.tmpdirname , bos_token="(BOS)" , eos_token="(EOS)" , do_normalize=__magic_name__ , padding_value=1.0 ) self.assertEqual(processor.tokenizer.get_vocab() , tokenizer_add_kwargs.get_vocab() ) self.assertIsInstance(processor.tokenizer , __magic_name__ ) self.assertEqual(processor.image_processor.to_json_string() , image_processor_add_kwargs.to_json_string() ) self.assertIsInstance(processor.image_processor , __magic_name__ ) def __A ( self : Tuple ) -> int: SCREAMING_SNAKE_CASE_ = self.get_image_processor() SCREAMING_SNAKE_CASE_ = self.get_tokenizer() SCREAMING_SNAKE_CASE_ = BlipaProcessor(tokenizer=__magic_name__ , image_processor=__magic_name__ ) SCREAMING_SNAKE_CASE_ = self.prepare_image_inputs() SCREAMING_SNAKE_CASE_ = image_processor(__magic_name__ , return_tensors="np" ) SCREAMING_SNAKE_CASE_ = processor(images=__magic_name__ , 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 __A ( self : str ) -> Tuple: SCREAMING_SNAKE_CASE_ = self.get_image_processor() SCREAMING_SNAKE_CASE_ = self.get_tokenizer() SCREAMING_SNAKE_CASE_ = BlipaProcessor(tokenizer=__magic_name__ , image_processor=__magic_name__ ) SCREAMING_SNAKE_CASE_ = "lower newer" SCREAMING_SNAKE_CASE_ = processor(text=__magic_name__ ) SCREAMING_SNAKE_CASE_ = tokenizer(__magic_name__ , return_token_type_ids=__magic_name__ ) for key in encoded_tok.keys(): self.assertListEqual(encoded_tok[key] , encoded_processor[key] ) def __A ( self : Dict ) -> Union[str, Any]: SCREAMING_SNAKE_CASE_ = self.get_image_processor() SCREAMING_SNAKE_CASE_ = self.get_tokenizer() SCREAMING_SNAKE_CASE_ = BlipaProcessor(tokenizer=__magic_name__ , image_processor=__magic_name__ ) SCREAMING_SNAKE_CASE_ = "lower newer" SCREAMING_SNAKE_CASE_ = self.prepare_image_inputs() SCREAMING_SNAKE_CASE_ = processor(text=__magic_name__ , images=__magic_name__ ) self.assertListEqual(list(inputs.keys() ) , ["pixel_values", "input_ids", "attention_mask"] ) # test if it raises when no input is passed with pytest.raises(__magic_name__ ): processor() def __A ( self : Dict ) -> Tuple: SCREAMING_SNAKE_CASE_ = self.get_image_processor() SCREAMING_SNAKE_CASE_ = self.get_tokenizer() SCREAMING_SNAKE_CASE_ = BlipaProcessor(tokenizer=__magic_name__ , image_processor=__magic_name__ ) SCREAMING_SNAKE_CASE_ = [[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]] SCREAMING_SNAKE_CASE_ = processor.batch_decode(__magic_name__ ) SCREAMING_SNAKE_CASE_ = tokenizer.batch_decode(__magic_name__ ) self.assertListEqual(__magic_name__ , __magic_name__ ) def __A ( self : List[str] ) -> int: SCREAMING_SNAKE_CASE_ = self.get_image_processor() SCREAMING_SNAKE_CASE_ = self.get_tokenizer() SCREAMING_SNAKE_CASE_ = BlipaProcessor(tokenizer=__magic_name__ , image_processor=__magic_name__ ) SCREAMING_SNAKE_CASE_ = "lower newer" SCREAMING_SNAKE_CASE_ = self.prepare_image_inputs() SCREAMING_SNAKE_CASE_ = processor(text=__magic_name__ , images=__magic_name__ ) # For now the processor supports only ['pixel_values', 'input_ids', 'attention_mask'] self.assertListEqual(list(inputs.keys() ) , ["pixel_values", "input_ids", "attention_mask"] )
305
0
import warnings from ...utils import logging from .image_processing_glpn import GLPNImageProcessor A : Optional[Any] = logging.get_logger(__name__) class lowerCamelCase (SCREAMING_SNAKE_CASE__ ): """simple docstring""" def __init__( self : Any , *__magic_name__ : Union[str, Any] , **__magic_name__ : Dict ) -> None: warnings.warn( "The class GLPNFeatureExtractor is deprecated and will be removed in version 5 of Transformers. Please" " use GLPNImageProcessor instead." , __A , ) super().__init__(*__A , **__A )
359
from typing import TYPE_CHECKING from ...file_utils import _LazyModule, is_tokenizers_available, is_torch_available, is_vision_available from ...utils import OptionalDependencyNotAvailable A : List[Any] = {"configuration_dpt": ["DPT_PRETRAINED_CONFIG_ARCHIVE_MAP", "DPTConfig"]} try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: A : Optional[Any] = ["DPTFeatureExtractor"] A : str = ["DPTImageProcessor"] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: A : Optional[Any] = [ "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 A : Optional[int] = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
305
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 A : Dict = logging.get_logger(__name__) class lowerCamelCase (UpperCAmelCase__ ): """simple docstring""" lowerCamelCase__ = ['input_features'] def __init__( self : Any , __magic_name__ : List[str]=80 , __magic_name__ : Tuple=16_000 , __magic_name__ : Tuple=160 , __magic_name__ : int=30 , __magic_name__ : Tuple=400 , __magic_name__ : List[str]=0.0 , __magic_name__ : List[Any]=False , **__magic_name__ : Optional[Any] , ) -> Dict: super().__init__( feature_size=_SCREAMING_SNAKE_CASE , sampling_rate=_SCREAMING_SNAKE_CASE , padding_value=_SCREAMING_SNAKE_CASE , return_attention_mask=_SCREAMING_SNAKE_CASE , **_SCREAMING_SNAKE_CASE , ) 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=_SCREAMING_SNAKE_CASE , min_frequency=0.0 , max_frequency=8000.0 , sampling_rate=_SCREAMING_SNAKE_CASE , norm="slaney" , mel_scale="slaney" , ) def __A ( self : Tuple , __magic_name__ : Optional[int] ) -> np.ndarray: SCREAMING_SNAKE_CASE_ = spectrogram( _SCREAMING_SNAKE_CASE , 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(_SCREAMING_SNAKE_CASE , 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 __A ( __magic_name__ : str , __magic_name__ : int , __magic_name__ : Tuple = 0.0 ) -> List[np.ndarray]: if attention_mask is not None: SCREAMING_SNAKE_CASE_ = np.array(_SCREAMING_SNAKE_CASE , np.intaa ) SCREAMING_SNAKE_CASE_ = [] for vector, length in zip(_SCREAMING_SNAKE_CASE , 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(_SCREAMING_SNAKE_CASE ) 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 : Union[str, Any] , __magic_name__ : Tuple , __magic_name__ : Optional[int] = True , __magic_name__ : List[str] = None , __magic_name__ : Any = None , __magic_name__ : List[Any] = None , __magic_name__ : Optional[int] = "max_length" , __magic_name__ : Tuple = None , __magic_name__ : List[str] = None , __magic_name__ : Any = None , **__magic_name__ : int , ) -> 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(_SCREAMING_SNAKE_CASE , np.ndarray ) and len(raw_speech.shape ) > 1 if is_batched_numpy and len(raw_speech.shape ) > 2: raise ValueError(F'''Only mono-channel audio is supported for input to {self}''' ) SCREAMING_SNAKE_CASE_ = is_batched_numpy or ( isinstance(_SCREAMING_SNAKE_CASE , (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(_SCREAMING_SNAKE_CASE , np.ndarray ): SCREAMING_SNAKE_CASE_ = np.asarray(_SCREAMING_SNAKE_CASE , dtype=np.floataa ) elif isinstance(_SCREAMING_SNAKE_CASE , 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( _SCREAMING_SNAKE_CASE , padding=_SCREAMING_SNAKE_CASE , max_length=max_length if max_length else self.n_samples , truncation=_SCREAMING_SNAKE_CASE , pad_to_multiple_of=_SCREAMING_SNAKE_CASE , 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(_SCREAMING_SNAKE_CASE ) for waveform in input_features[0]] if isinstance(input_features[0] , _SCREAMING_SNAKE_CASE ): SCREAMING_SNAKE_CASE_ = [np.asarray(_SCREAMING_SNAKE_CASE , 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(_SCREAMING_SNAKE_CASE ) return padded_inputs def __A ( self : Dict ) -> 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
360
from __future__ import annotations import collections import pprint from pathlib import Path def a__ ( __UpperCamelCase ): return "".join(sorted(__UpperCamelCase ) ) def a__ ( __UpperCamelCase ): return word_by_signature[signature(__UpperCamelCase )] A : str = Path(__file__).parent.joinpath("words.txt").read_text(encoding="utf-8") A : int = sorted({word.strip().lower() for word in data.splitlines()}) A : Tuple = collections.defaultdict(list) for word in word_list: word_by_signature[signature(word)].append(word) if __name__ == "__main__": A : Union[str, Any] = {word: anagram(word) for word in word_list if len(anagram(word)) > 1} with open("anagrams.txt", "w") as file: file.write("all_anagrams = \n ") file.write(pprint.pformat(all_anagrams))
305
0
import pytest A : Dict = "__dummy_dataset1__" A : Union[str, Any] = "\nimport json\nimport os\n\nimport datasets\n\n\nREPO_URL = \"https://huggingface.co/datasets/albertvillanova/tests-raw-jsonl/resolve/main/\"\nURLS = {\"train\": REPO_URL + \"wikiann-bn-train.jsonl\", \"validation\": REPO_URL + \"wikiann-bn-validation.jsonl\"}\n\n\nclass __DummyDataset1__(datasets.GeneratorBasedBuilder):\n\n def _info(self):\n features = datasets.Features(\n {\n \"tokens\": datasets.Sequence(datasets.Value(\"string\")),\n \"ner_tags\": datasets.Sequence(\n datasets.features.ClassLabel(\n names=[\n \"O\",\n \"B-PER\",\n \"I-PER\",\n \"B-ORG\",\n \"I-ORG\",\n \"B-LOC\",\n \"I-LOC\",\n ]\n )\n ),\n \"langs\": datasets.Sequence(datasets.Value(\"string\")),\n \"spans\": datasets.Sequence(datasets.Value(\"string\")),\n }\n )\n return datasets.DatasetInfo(features=features)\n\n def _split_generators(self, dl_manager):\n dl_path = dl_manager.download(URLS)\n return [\n datasets.SplitGenerator(datasets.Split.TRAIN, gen_kwargs={\"filepath\": dl_path[\"train\"]}),\n datasets.SplitGenerator(datasets.Split.VALIDATION, gen_kwargs={\"filepath\": dl_path[\"validation\"]}),\n ]\n\n def _generate_examples(self, filepath):\n with open(filepath, \"r\", encoding=\"utf-8\") as f:\n for i, line in enumerate(f):\n yield i, json.loads(line)\n" @pytest.fixture def a__ ( ): return DATASET_LOADING_SCRIPT_NAME @pytest.fixture def a__ ( ): return DATASET_LOADING_SCRIPT_CODE @pytest.fixture def a__ ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase ): SCREAMING_SNAKE_CASE_ = dataset_loading_script_name SCREAMING_SNAKE_CASE_ = tmp_path / "datasets" / script_name script_dir.mkdir(parents=_a ) SCREAMING_SNAKE_CASE_ = script_dir / F'''{script_name}.py''' with open(_a , "w" ) as f: f.write(_a ) return str(_a )
361
import copy import os from typing import TYPE_CHECKING, List, Union if TYPE_CHECKING: pass from ...configuration_utils import PretrainedConfig from ...utils import logging A : int = logging.get_logger(__name__) A : str = { "kakaobrain/align-base": "https://huggingface.co/kakaobrain/align-base/resolve/main/config.json", } class lowerCamelCase (SCREAMING_SNAKE_CASE__ ): """simple docstring""" lowerCamelCase__ = '''align_text_model''' def __init__( self : Optional[Any] , __magic_name__ : Union[str, Any]=30_522 , __magic_name__ : Tuple=768 , __magic_name__ : List[str]=12 , __magic_name__ : Optional[Any]=12 , __magic_name__ : str=3_072 , __magic_name__ : Dict="gelu" , __magic_name__ : Any=0.1 , __magic_name__ : Optional[int]=0.1 , __magic_name__ : List[str]=512 , __magic_name__ : Any=2 , __magic_name__ : Optional[Any]=0.02 , __magic_name__ : int=1e-12 , __magic_name__ : str=0 , __magic_name__ : Optional[Any]="absolute" , __magic_name__ : Optional[Any]=True , **__magic_name__ : Tuple , ) -> Union[str, Any]: super().__init__(**__magic_name__ ) 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_ = position_embedding_type SCREAMING_SNAKE_CASE_ = use_cache SCREAMING_SNAKE_CASE_ = pad_token_id @classmethod def __A ( cls : Any , __magic_name__ : Union[str, os.PathLike] , **__magic_name__ : Optional[Any] ) -> "PretrainedConfig": cls._set_token_in_kwargs(__magic_name__ ) SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = cls.get_config_dict(__magic_name__ , **__magic_name__ ) # get the text config dict if we are loading from AlignConfig if config_dict.get("model_type" ) == "align": SCREAMING_SNAKE_CASE_ = config_dict["text_config"] if "model_type" in config_dict and hasattr(cls , "model_type" ) and config_dict["model_type"] != cls.model_type: logger.warning( F'''You are using a model of type {config_dict["model_type"]} to instantiate a model of type ''' F'''{cls.model_type}. This is not supported for all configurations of models and can yield errors.''' ) return cls.from_dict(__magic_name__ , **__magic_name__ ) class lowerCamelCase (SCREAMING_SNAKE_CASE__ ): """simple docstring""" lowerCamelCase__ = '''align_vision_model''' def __init__( self : List[str] , __magic_name__ : int = 3 , __magic_name__ : int = 600 , __magic_name__ : float = 2.0 , __magic_name__ : float = 3.1 , __magic_name__ : int = 8 , __magic_name__ : List[int] = [3, 3, 5, 3, 5, 5, 3] , __magic_name__ : List[int] = [32, 16, 24, 40, 80, 112, 192] , __magic_name__ : List[int] = [16, 24, 40, 80, 112, 192, 320] , __magic_name__ : List[int] = [] , __magic_name__ : List[int] = [1, 2, 2, 2, 1, 2, 1] , __magic_name__ : List[int] = [1, 2, 2, 3, 3, 4, 1] , __magic_name__ : List[int] = [1, 6, 6, 6, 6, 6, 6] , __magic_name__ : float = 0.25 , __magic_name__ : str = "swish" , __magic_name__ : int = 2_560 , __magic_name__ : str = "mean" , __magic_name__ : float = 0.02 , __magic_name__ : float = 0.001 , __magic_name__ : float = 0.99 , __magic_name__ : float = 0.2 , **__magic_name__ : List[Any] , ) -> Tuple: super().__init__(**__magic_name__ ) SCREAMING_SNAKE_CASE_ = num_channels SCREAMING_SNAKE_CASE_ = image_size SCREAMING_SNAKE_CASE_ = width_coefficient SCREAMING_SNAKE_CASE_ = depth_coefficient SCREAMING_SNAKE_CASE_ = depth_divisor SCREAMING_SNAKE_CASE_ = kernel_sizes SCREAMING_SNAKE_CASE_ = in_channels SCREAMING_SNAKE_CASE_ = out_channels SCREAMING_SNAKE_CASE_ = depthwise_padding SCREAMING_SNAKE_CASE_ = strides SCREAMING_SNAKE_CASE_ = num_block_repeats SCREAMING_SNAKE_CASE_ = expand_ratios SCREAMING_SNAKE_CASE_ = squeeze_expansion_ratio SCREAMING_SNAKE_CASE_ = hidden_act SCREAMING_SNAKE_CASE_ = hidden_dim SCREAMING_SNAKE_CASE_ = pooling_type SCREAMING_SNAKE_CASE_ = initializer_range SCREAMING_SNAKE_CASE_ = batch_norm_eps SCREAMING_SNAKE_CASE_ = batch_norm_momentum SCREAMING_SNAKE_CASE_ = drop_connect_rate SCREAMING_SNAKE_CASE_ = sum(__magic_name__ ) * 4 @classmethod def __A ( cls : List[str] , __magic_name__ : Union[str, os.PathLike] , **__magic_name__ : Dict ) -> "PretrainedConfig": cls._set_token_in_kwargs(__magic_name__ ) SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = cls.get_config_dict(__magic_name__ , **__magic_name__ ) # get the vision config dict if we are loading from AlignConfig if config_dict.get("model_type" ) == "align": SCREAMING_SNAKE_CASE_ = 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(__magic_name__ , **__magic_name__ ) class lowerCamelCase (SCREAMING_SNAKE_CASE__ ): """simple docstring""" lowerCamelCase__ = '''align''' lowerCamelCase__ = True def __init__( self : Optional[Any] , __magic_name__ : Dict=None , __magic_name__ : List[Any]=None , __magic_name__ : str=640 , __magic_name__ : Any=1.0 , __magic_name__ : Dict=0.02 , **__magic_name__ : Union[str, Any] , ) -> int: super().__init__(**__magic_name__ ) if text_config is None: SCREAMING_SNAKE_CASE_ = {} logger.info("text_config is None. Initializing the AlignTextConfig with default values." ) if vision_config is None: SCREAMING_SNAKE_CASE_ = {} logger.info("vision_config is None. Initializing the AlignVisionConfig with default values." ) SCREAMING_SNAKE_CASE_ = AlignTextConfig(**__magic_name__ ) SCREAMING_SNAKE_CASE_ = AlignVisionConfig(**__magic_name__ ) SCREAMING_SNAKE_CASE_ = projection_dim SCREAMING_SNAKE_CASE_ = temperature_init_value SCREAMING_SNAKE_CASE_ = initializer_range @classmethod def __A ( cls : List[str] , __magic_name__ : AlignTextConfig , __magic_name__ : AlignVisionConfig , **__magic_name__ : Tuple ) -> Any: return cls(text_config=text_config.to_dict() , vision_config=vision_config.to_dict() , **__magic_name__ ) def __A ( self : int ) -> Union[str, Any]: SCREAMING_SNAKE_CASE_ = copy.deepcopy(self.__dict__ ) SCREAMING_SNAKE_CASE_ = self.text_config.to_dict() SCREAMING_SNAKE_CASE_ = self.vision_config.to_dict() SCREAMING_SNAKE_CASE_ = self.__class__.model_type return output
305
0
"""simple docstring""" def a__ ( ): SCREAMING_SNAKE_CASE_ = [] SCREAMING_SNAKE_CASE_ = 1 while len(__lowerCAmelCase ) < 1E6: constant.append(str(__lowerCAmelCase ) ) i += 1 SCREAMING_SNAKE_CASE_ = ''''''.join(__lowerCAmelCase ) return ( int(constant[0] ) * int(constant[9] ) * int(constant[9_9] ) * int(constant[9_9_9] ) * int(constant[9_9_9_9] ) * int(constant[9_9_9_9_9] ) * int(constant[9_9_9_9_9_9] ) ) if __name__ == "__main__": print(solution())
362
import unittest from transformers.testing_utils import CaptureStdout from transformers.tools.python_interpreter import evaluate def a__ ( __UpperCamelCase ): return x + 2 class lowerCamelCase (unittest.TestCase ): """simple docstring""" def __A ( self : List[Any] ) -> int: SCREAMING_SNAKE_CASE_ = "x = 3" SCREAMING_SNAKE_CASE_ = {} SCREAMING_SNAKE_CASE_ = evaluate(__magic_name__ , {} , state=__magic_name__ ) assert result == 3 self.assertDictEqual(__magic_name__ , {"x": 3} ) SCREAMING_SNAKE_CASE_ = "x = y" SCREAMING_SNAKE_CASE_ = {"y": 5} SCREAMING_SNAKE_CASE_ = evaluate(__magic_name__ , {} , state=__magic_name__ ) # evaluate returns the value of the last assignment. assert result == 5 self.assertDictEqual(__magic_name__ , {"x": 5, "y": 5} ) def __A ( self : Union[str, Any] ) -> str: SCREAMING_SNAKE_CASE_ = "y = add_two(x)" SCREAMING_SNAKE_CASE_ = {"x": 3} SCREAMING_SNAKE_CASE_ = evaluate(__magic_name__ , {"add_two": add_two} , state=__magic_name__ ) assert result == 5 self.assertDictEqual(__magic_name__ , {"x": 3, "y": 5} ) # Won't work without the tool with CaptureStdout() as out: SCREAMING_SNAKE_CASE_ = evaluate(__magic_name__ , {} , state=__magic_name__ ) assert result is None assert "tried to execute add_two" in out.out def __A ( self : List[str] ) -> int: SCREAMING_SNAKE_CASE_ = "x = 3" SCREAMING_SNAKE_CASE_ = {} SCREAMING_SNAKE_CASE_ = evaluate(__magic_name__ , {} , state=__magic_name__ ) assert result == 3 self.assertDictEqual(__magic_name__ , {"x": 3} ) def __A ( self : Optional[Any] ) -> str: SCREAMING_SNAKE_CASE_ = "test_dict = {'x': x, 'y': add_two(x)}" SCREAMING_SNAKE_CASE_ = {"x": 3} SCREAMING_SNAKE_CASE_ = evaluate(__magic_name__ , {"add_two": add_two} , state=__magic_name__ ) self.assertDictEqual(__magic_name__ , {"x": 3, "y": 5} ) self.assertDictEqual(__magic_name__ , {"x": 3, "test_dict": {"x": 3, "y": 5}} ) def __A ( self : Optional[int] ) -> List[str]: SCREAMING_SNAKE_CASE_ = "x = 3\ny = 5" SCREAMING_SNAKE_CASE_ = {} SCREAMING_SNAKE_CASE_ = evaluate(__magic_name__ , {} , state=__magic_name__ ) # evaluate returns the value of the last assignment. assert result == 5 self.assertDictEqual(__magic_name__ , {"x": 3, "y": 5} ) def __A ( self : Any ) -> List[str]: SCREAMING_SNAKE_CASE_ = "text = f'This is x: {x}.'" SCREAMING_SNAKE_CASE_ = {"x": 3} SCREAMING_SNAKE_CASE_ = evaluate(__magic_name__ , {} , state=__magic_name__ ) # evaluate returns the value of the last assignment. assert result == "This is x: 3." self.assertDictEqual(__magic_name__ , {"x": 3, "text": "This is x: 3."} ) def __A ( self : int ) -> Tuple: SCREAMING_SNAKE_CASE_ = "if x <= 3:\n y = 2\nelse:\n y = 5" SCREAMING_SNAKE_CASE_ = {"x": 3} SCREAMING_SNAKE_CASE_ = evaluate(__magic_name__ , {} , state=__magic_name__ ) # evaluate returns the value of the last assignment. assert result == 2 self.assertDictEqual(__magic_name__ , {"x": 3, "y": 2} ) SCREAMING_SNAKE_CASE_ = {"x": 8} SCREAMING_SNAKE_CASE_ = evaluate(__magic_name__ , {} , state=__magic_name__ ) # evaluate returns the value of the last assignment. assert result == 5 self.assertDictEqual(__magic_name__ , {"x": 8, "y": 5} ) def __A ( self : str ) -> str: SCREAMING_SNAKE_CASE_ = "test_list = [x, add_two(x)]" SCREAMING_SNAKE_CASE_ = {"x": 3} SCREAMING_SNAKE_CASE_ = evaluate(__magic_name__ , {"add_two": add_two} , state=__magic_name__ ) self.assertListEqual(__magic_name__ , [3, 5] ) self.assertDictEqual(__magic_name__ , {"x": 3, "test_list": [3, 5]} ) def __A ( self : Union[str, Any] ) -> List[Any]: SCREAMING_SNAKE_CASE_ = "y = x" SCREAMING_SNAKE_CASE_ = {"x": 3} SCREAMING_SNAKE_CASE_ = evaluate(__magic_name__ , {} , state=__magic_name__ ) assert result == 3 self.assertDictEqual(__magic_name__ , {"x": 3, "y": 3} ) def __A ( self : Tuple ) -> List[Any]: SCREAMING_SNAKE_CASE_ = "test_list = [x, add_two(x)]\ntest_list[1]" SCREAMING_SNAKE_CASE_ = {"x": 3} SCREAMING_SNAKE_CASE_ = evaluate(__magic_name__ , {"add_two": add_two} , state=__magic_name__ ) assert result == 5 self.assertDictEqual(__magic_name__ , {"x": 3, "test_list": [3, 5]} ) SCREAMING_SNAKE_CASE_ = "test_dict = {'x': x, 'y': add_two(x)}\ntest_dict['y']" SCREAMING_SNAKE_CASE_ = {"x": 3} SCREAMING_SNAKE_CASE_ = evaluate(__magic_name__ , {"add_two": add_two} , state=__magic_name__ ) assert result == 5 self.assertDictEqual(__magic_name__ , {"x": 3, "test_dict": {"x": 3, "y": 5}} ) def __A ( self : Tuple ) -> Any: SCREAMING_SNAKE_CASE_ = "x = 0\nfor i in range(3):\n x = i" SCREAMING_SNAKE_CASE_ = {} SCREAMING_SNAKE_CASE_ = evaluate(__magic_name__ , {"range": range} , state=__magic_name__ ) assert result == 2 self.assertDictEqual(__magic_name__ , {"x": 2, "i": 2} )
305
0
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 lowerCamelCase (a__ ): """simple docstring""" lowerCamelCase__ = """char""" lowerCamelCase__ = """bpe""" lowerCamelCase__ = """wp""" A : int = (DecodeType.CHARACTER, DecodeType.BPE, DecodeType.WORDPIECE) class lowerCamelCase (a__ ): """simple docstring""" lowerCamelCase__ = ["""image_processor""", """char_tokenizer"""] lowerCamelCase__ = """ViTImageProcessor""" lowerCamelCase__ = """MgpstrTokenizer""" def __init__( self : Any , __magic_name__ : Optional[int]=None , __magic_name__ : Tuple=None , **__magic_name__ : Dict ) -> List[str]: SCREAMING_SNAKE_CASE_ = None if "feature_extractor" in kwargs: warnings.warn( "The `feature_extractor` argument is deprecated and will be removed in v5, use `image_processor`" " instead." , lowerCAmelCase__ , ) SCREAMING_SNAKE_CASE_ = kwargs.pop("feature_extractor" ) SCREAMING_SNAKE_CASE_ = image_processor if image_processor is not None else feature_extractor if image_processor is None: raise ValueError("You need to specify an `image_processor`." ) if tokenizer is None: raise ValueError("You need to specify a `tokenizer`." ) SCREAMING_SNAKE_CASE_ = tokenizer SCREAMING_SNAKE_CASE_ = AutoTokenizer.from_pretrained("gpt2" ) SCREAMING_SNAKE_CASE_ = AutoTokenizer.from_pretrained("bert-base-uncased" ) super().__init__(lowerCAmelCase__ , lowerCAmelCase__ ) def __call__( self : Any , __magic_name__ : int=None , __magic_name__ : Dict=None , __magic_name__ : int=None , **__magic_name__ : int ) -> Optional[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: SCREAMING_SNAKE_CASE_ = self.image_processor(lowerCAmelCase__ , return_tensors=lowerCAmelCase__ , **lowerCAmelCase__ ) if text is not None: SCREAMING_SNAKE_CASE_ = self.char_tokenizer(lowerCAmelCase__ , return_tensors=lowerCAmelCase__ , **lowerCAmelCase__ ) if text is None: return inputs elif images is None: return encodings else: SCREAMING_SNAKE_CASE_ = encodings["input_ids"] return inputs def __A ( self : Dict , __magic_name__ : Optional[Any] ) -> str: SCREAMING_SNAKE_CASE_ = sequences SCREAMING_SNAKE_CASE_ = char_preds.size(0 ) SCREAMING_SNAKE_CASE_ = self._decode_helper(lowerCAmelCase__ , "char" ) SCREAMING_SNAKE_CASE_ = self._decode_helper(lowerCAmelCase__ , "bpe" ) SCREAMING_SNAKE_CASE_ = self._decode_helper(lowerCAmelCase__ , "wp" ) SCREAMING_SNAKE_CASE_ = [] SCREAMING_SNAKE_CASE_ = [] for i in range(lowerCAmelCase__ ): SCREAMING_SNAKE_CASE_ = [char_scores[i], bpe_scores[i], wp_scores[i]] SCREAMING_SNAKE_CASE_ = [char_strs[i], bpe_strs[i], wp_strs[i]] SCREAMING_SNAKE_CASE_ = scores.index(max(lowerCAmelCase__ ) ) final_strs.append(strs[max_score_index] ) final_scores.append(scores[max_score_index] ) SCREAMING_SNAKE_CASE_ = {} SCREAMING_SNAKE_CASE_ = final_strs SCREAMING_SNAKE_CASE_ = final_scores SCREAMING_SNAKE_CASE_ = char_strs SCREAMING_SNAKE_CASE_ = bpe_strs SCREAMING_SNAKE_CASE_ = wp_strs return out def __A ( self : Any , __magic_name__ : Optional[int] , __magic_name__ : Optional[Any] ) -> Dict: if format == DecodeType.CHARACTER: SCREAMING_SNAKE_CASE_ = self.char_decode SCREAMING_SNAKE_CASE_ = 1 SCREAMING_SNAKE_CASE_ = "[s]" elif format == DecodeType.BPE: SCREAMING_SNAKE_CASE_ = self.bpe_decode SCREAMING_SNAKE_CASE_ = 2 SCREAMING_SNAKE_CASE_ = "#" elif format == DecodeType.WORDPIECE: SCREAMING_SNAKE_CASE_ = self.wp_decode SCREAMING_SNAKE_CASE_ = 102 SCREAMING_SNAKE_CASE_ = "[SEP]" else: raise ValueError(F'''Format {format} is not supported.''' ) SCREAMING_SNAKE_CASE_ = [], [] SCREAMING_SNAKE_CASE_ = pred_logits.size(0 ) SCREAMING_SNAKE_CASE_ = pred_logits.size(1 ) SCREAMING_SNAKE_CASE_ = pred_logits.topk(1 , dim=-1 , largest=lowerCAmelCase__ , sorted=lowerCAmelCase__ ) SCREAMING_SNAKE_CASE_ = preds_index.view(-1 , lowerCAmelCase__ )[:, 1:] SCREAMING_SNAKE_CASE_ = decoder(lowerCAmelCase__ ) SCREAMING_SNAKE_CASE_ = torch.nn.functional.softmax(lowerCAmelCase__ , dim=2 ).max(dim=2 ) SCREAMING_SNAKE_CASE_ = preds_max_prob[:, 1:] for index in range(lowerCAmelCase__ ): SCREAMING_SNAKE_CASE_ = preds_str[index].find(lowerCAmelCase__ ) SCREAMING_SNAKE_CASE_ = preds_str[index][:pred_eos] SCREAMING_SNAKE_CASE_ = preds_index[index].cpu().tolist() SCREAMING_SNAKE_CASE_ = pred_index.index(lowerCAmelCase__ ) if eos_token in pred_index else -1 SCREAMING_SNAKE_CASE_ = preds_max_prob[index][: pred_eos_index + 1] SCREAMING_SNAKE_CASE_ = pred_max_prob.cumprod(dim=0 )[-1] if pred_max_prob.nelement() != 0 else 0.0 dec_strs.append(lowerCAmelCase__ ) conf_scores.append(lowerCAmelCase__ ) return dec_strs, conf_scores def __A ( self : Union[str, Any] , __magic_name__ : Any ) -> Any: SCREAMING_SNAKE_CASE_ = [seq.replace(" " , "" ) for seq in self.char_tokenizer.batch_decode(lowerCAmelCase__ )] return decode_strs def __A ( self : Optional[int] , __magic_name__ : Tuple ) -> str: return self.bpe_tokenizer.batch_decode(lowerCAmelCase__ ) def __A ( self : Dict , __magic_name__ : Any ) -> str: SCREAMING_SNAKE_CASE_ = [seq.replace(" " , "" ) for seq in self.wp_tokenizer.batch_decode(lowerCAmelCase__ )] return decode_strs
363
import numpy as np import pandas as pd from sklearn.preprocessing import Normalizer from sklearn.svm import SVR from statsmodels.tsa.statespace.sarimax import SARIMAX def a__ ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase ): SCREAMING_SNAKE_CASE_ = np.array([[1, item, train_mtch[i]] for i, item in enumerate(__UpperCamelCase )] ) SCREAMING_SNAKE_CASE_ = np.array(__UpperCamelCase ) SCREAMING_SNAKE_CASE_ = np.dot(np.dot(np.linalg.inv(np.dot(x.transpose() , __UpperCamelCase ) ) , x.transpose() ) , __UpperCamelCase ) return abs(beta[0] + test_dt[0] * beta[1] + test_mtch[0] + beta[2] ) def a__ ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase ): SCREAMING_SNAKE_CASE_ = (1, 2, 1) SCREAMING_SNAKE_CASE_ = (1, 1, 0, 7) SCREAMING_SNAKE_CASE_ = SARIMAX( __UpperCamelCase , exog=__UpperCamelCase , order=__UpperCamelCase , seasonal_order=__UpperCamelCase ) SCREAMING_SNAKE_CASE_ = model.fit(disp=__UpperCamelCase , maxiter=6_0_0 , method="nm" ) SCREAMING_SNAKE_CASE_ = model_fit.predict(1 , len(__UpperCamelCase ) , exog=[test_match] ) return result[0] def a__ ( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase ): SCREAMING_SNAKE_CASE_ = SVR(kernel="rbf" , C=1 , gamma=0.1 , epsilon=0.1 ) regressor.fit(__UpperCamelCase , __UpperCamelCase ) SCREAMING_SNAKE_CASE_ = regressor.predict(__UpperCamelCase ) return y_pred[0] def a__ ( __UpperCamelCase ): train_user.sort() SCREAMING_SNAKE_CASE_ = np.percentile(__UpperCamelCase , 2_5 ) SCREAMING_SNAKE_CASE_ = np.percentile(__UpperCamelCase , 7_5 ) SCREAMING_SNAKE_CASE_ = qa - qa SCREAMING_SNAKE_CASE_ = qa - (iqr * 0.1) return low_lim def a__ ( __UpperCamelCase , __UpperCamelCase ): SCREAMING_SNAKE_CASE_ = 0 SCREAMING_SNAKE_CASE_ = 0 for i in list_vote: if i > actual_result: SCREAMING_SNAKE_CASE_ = not_safe + 1 else: if abs(abs(__UpperCamelCase ) - abs(__UpperCamelCase ) ) <= 0.1: safe += 1 else: not_safe += 1 return safe > not_safe if __name__ == "__main__": # data_input_df = pd.read_csv("ex_data.csv", header=None) A : Dict = [[1_82_31, 0.0, 1], [2_26_21, 1.0, 2], [1_56_75, 0.0, 3], [2_35_83, 1.0, 4]] A : Optional[Any] = pd.DataFrame( data_input, columns=["total_user", "total_even", "days"] ) A : Union[str, Any] = Normalizer().fit_transform(data_input_df.values) # split data A : Optional[int] = normalize_df[:, 2].tolist() A : List[str] = normalize_df[:, 0].tolist() A : int = normalize_df[:, 1].tolist() # for svr (input variable = total date and total match) A : int = normalize_df[:, [1, 2]].tolist() A : Tuple = x[: len(x) - 1] A : str = x[len(x) - 1 :] # for linear regression & sarimax A : Tuple = total_date[: len(total_date) - 1] A : Optional[int] = total_user[: len(total_user) - 1] A : str = total_match[: len(total_match) - 1] A : List[Any] = total_date[len(total_date) - 1 :] A : List[Any] = total_user[len(total_user) - 1 :] A : Optional[Any] = total_match[len(total_match) - 1 :] # voting system with forecasting A : Optional[int] = [ linear_regression_prediction( trn_date, trn_user, trn_match, tst_date, tst_match ), sarimax_predictor(trn_user, trn_match, tst_match), support_vector_regressor(x_train, x_test, trn_user), ] # check the safety of today's data A : str = "" if data_safety_checker(res_vote, tst_user) else "not " print("Today's data is {not_str}safe.")
305
0
from typing import Dict, List, Optional, Union import numpy as np from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size_dict from ...image_transforms import rescale, resize, to_channel_dimension_format from ...image_utils import ( 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 : Optional[int] = logging.get_logger(__name__) def a__ ( __UpperCamelCase , __UpperCamelCase ): SCREAMING_SNAKE_CASE_ = b.T SCREAMING_SNAKE_CASE_ = np.sum(np.square(__UpperCamelCase ) , axis=1 ) SCREAMING_SNAKE_CASE_ = np.sum(np.square(__UpperCamelCase ) , axis=0 ) SCREAMING_SNAKE_CASE_ = np.matmul(__UpperCamelCase , __UpperCamelCase ) SCREAMING_SNAKE_CASE_ = aa[:, None] - 2 * ab + ba[None, :] return d def a__ ( __UpperCamelCase , __UpperCamelCase ): SCREAMING_SNAKE_CASE_ = x.reshape(-1 , 3 ) SCREAMING_SNAKE_CASE_ = squared_euclidean_distance(__UpperCamelCase , __UpperCamelCase ) return np.argmin(__UpperCamelCase , axis=1 ) class lowerCamelCase (lowerCAmelCase__ ): """simple docstring""" lowerCamelCase__ = ["pixel_values"] def __init__( self : List[str] , __magic_name__ : List[str] = None , __magic_name__ : List[Any] = True , __magic_name__ : Dict = None , __magic_name__ : Union[str, Any] = PILImageResampling.BILINEAR , __magic_name__ : Union[str, Any] = True , __magic_name__ : Union[str, Any] = True , **__magic_name__ : List[Any] , ) -> Tuple: super().__init__(**a__ ) SCREAMING_SNAKE_CASE_ = size if size is not None else {"height": 256, "width": 256} SCREAMING_SNAKE_CASE_ = get_size_dict(a__ ) SCREAMING_SNAKE_CASE_ = np.array(a__ ) if clusters is not None else None SCREAMING_SNAKE_CASE_ = do_resize SCREAMING_SNAKE_CASE_ = size SCREAMING_SNAKE_CASE_ = resample SCREAMING_SNAKE_CASE_ = do_normalize SCREAMING_SNAKE_CASE_ = do_color_quantize def __A ( self : Any , __magic_name__ : Optional[Any] , __magic_name__ : Optional[int] , __magic_name__ : Optional[int] = PILImageResampling.BILINEAR , __magic_name__ : Optional[int] = None , **__magic_name__ : List[str] , ) -> Tuple: SCREAMING_SNAKE_CASE_ = get_size_dict(a__ ) if "height" not in size or "width" not in size: raise ValueError(F'''Size dictionary must contain both height and width keys. Got {size.keys()}''' ) return resize( a__ , size=(size["height"], size["width"]) , resample=a__ , data_format=a__ , **a__ ) def __A ( self : Any , __magic_name__ : List[Any] , __magic_name__ : Optional[int] = None , ) -> int: SCREAMING_SNAKE_CASE_ = rescale(image=a__ , scale=1 / 127.5 , data_format=a__ ) SCREAMING_SNAKE_CASE_ = image - 1 return image def __A ( self : List[str] , __magic_name__ : Optional[Any] , __magic_name__ : List[Any] = None , __magic_name__ : Union[str, Any] = None , __magic_name__ : List[Any] = None , __magic_name__ : Any = None , __magic_name__ : List[Any] = None , __magic_name__ : List[str] = None , __magic_name__ : Union[str, Any] = None , __magic_name__ : Dict = ChannelDimension.FIRST , **__magic_name__ : int , ) -> int: SCREAMING_SNAKE_CASE_ = do_resize if do_resize is not None else self.do_resize SCREAMING_SNAKE_CASE_ = size if size is not None else self.size SCREAMING_SNAKE_CASE_ = get_size_dict(a__ ) SCREAMING_SNAKE_CASE_ = resample if resample is not None else self.resample SCREAMING_SNAKE_CASE_ = do_normalize if do_normalize is not None else self.do_normalize SCREAMING_SNAKE_CASE_ = do_color_quantize if do_color_quantize is not None else self.do_color_quantize SCREAMING_SNAKE_CASE_ = clusters if clusters is not None else self.clusters SCREAMING_SNAKE_CASE_ = np.array(a__ ) 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_color_quantize and clusters is None: raise ValueError("Clusters must be specified if do_color_quantize 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__ , resample=a__ ) for image in images] if do_normalize: SCREAMING_SNAKE_CASE_ = [self.normalize(image=a__ ) for image in images] if do_color_quantize: SCREAMING_SNAKE_CASE_ = [to_channel_dimension_format(a__ , ChannelDimension.LAST ) for image in images] # color quantize from (batch_size, height, width, 3) to (batch_size, height, width) SCREAMING_SNAKE_CASE_ = np.array(a__ ) SCREAMING_SNAKE_CASE_ = color_quantize(a__ , a__ ).reshape(images.shape[:-1] ) # flatten to (batch_size, height*width) SCREAMING_SNAKE_CASE_ = images.shape[0] SCREAMING_SNAKE_CASE_ = images.reshape(a__ , -1 ) # We need to convert back to a list of images to keep consistent behaviour across processors. SCREAMING_SNAKE_CASE_ = list(a__ ) else: SCREAMING_SNAKE_CASE_ = [to_channel_dimension_format(a__ , a__ ) for image in images] SCREAMING_SNAKE_CASE_ = {"input_ids": images} return BatchFeature(data=a__ , tensor_type=a__ )
364
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tf_available, is_torch_available A : List[str] = {"configuration_swin": ["SWIN_PRETRAINED_CONFIG_ARCHIVE_MAP", "SwinConfig", "SwinOnnxConfig"]} try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: A : Any = [ "SWIN_PRETRAINED_MODEL_ARCHIVE_LIST", "SwinForImageClassification", "SwinForMaskedImageModeling", "SwinModel", "SwinPreTrainedModel", "SwinBackbone", ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: A : str = [ "TF_SWIN_PRETRAINED_MODEL_ARCHIVE_LIST", "TFSwinForImageClassification", "TFSwinForMaskedImageModeling", "TFSwinModel", "TFSwinPreTrainedModel", ] if TYPE_CHECKING: from .configuration_swin import SWIN_PRETRAINED_CONFIG_ARCHIVE_MAP, SwinConfig, SwinOnnxConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_swin import ( SWIN_PRETRAINED_MODEL_ARCHIVE_LIST, SwinBackbone, SwinForImageClassification, SwinForMaskedImageModeling, SwinModel, SwinPreTrainedModel, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_swin import ( TF_SWIN_PRETRAINED_MODEL_ARCHIVE_LIST, TFSwinForImageClassification, TFSwinForMaskedImageModeling, TFSwinModel, TFSwinPreTrainedModel, ) else: import sys A : Tuple = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
305
0
"""simple docstring""" from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_tf_available, is_torch_available, ) A : Union[str, Any] = {"configuration_encoder_decoder": ["EncoderDecoderConfig"]} try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: A : Dict = ["EncoderDecoderModel"] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: A : Tuple = ["TFEncoderDecoderModel"] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: A : List[str] = ["FlaxEncoderDecoderModel"] if TYPE_CHECKING: from .configuration_encoder_decoder import EncoderDecoderConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_encoder_decoder import EncoderDecoderModel try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_encoder_decoder import TFEncoderDecoderModel try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_flax_encoder_decoder import FlaxEncoderDecoderModel else: import sys A : str = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
365
import tempfile import unittest import numpy as np from huggingface_hub import HfFolder, delete_repo from requests.exceptions import HTTPError from transformers import BertConfig, is_flax_available from transformers.testing_utils import TOKEN, USER, is_staging_test, require_flax if is_flax_available(): import os from flax.core.frozen_dict import unfreeze from flax.traverse_util import flatten_dict from transformers import FlaxBertModel A : Union[str, Any] = "0.12" # assumed parallelism: 8 @require_flax @is_staging_test class lowerCamelCase (unittest.TestCase ): """simple docstring""" @classmethod def __A ( cls : Any ) -> Dict: SCREAMING_SNAKE_CASE_ = TOKEN HfFolder.save_token(__magic_name__ ) @classmethod def __A ( cls : Optional[int] ) -> Tuple: try: delete_repo(token=cls._token , repo_id="test-model-flax" ) except HTTPError: pass try: delete_repo(token=cls._token , repo_id="valid_org/test-model-flax-org" ) except HTTPError: pass def __A ( self : str ) -> str: SCREAMING_SNAKE_CASE_ = BertConfig( vocab_size=99 , hidden_size=32 , num_hidden_layers=5 , num_attention_heads=4 , intermediate_size=37 ) SCREAMING_SNAKE_CASE_ = FlaxBertModel(__magic_name__ ) model.push_to_hub("test-model-flax" , use_auth_token=self._token ) SCREAMING_SNAKE_CASE_ = FlaxBertModel.from_pretrained(F'''{USER}/test-model-flax''' ) SCREAMING_SNAKE_CASE_ = flatten_dict(unfreeze(model.params ) ) SCREAMING_SNAKE_CASE_ = flatten_dict(unfreeze(new_model.params ) ) for key in base_params.keys(): SCREAMING_SNAKE_CASE_ = (base_params[key] - new_params[key]).sum().item() self.assertLessEqual(__magic_name__ , 1e-3 , msg=F'''{key} not identical''' ) # Reset repo delete_repo(token=self._token , repo_id="test-model-flax" ) # Push to hub via save_pretrained with tempfile.TemporaryDirectory() as tmp_dir: model.save_pretrained(__magic_name__ , repo_id="test-model-flax" , push_to_hub=__magic_name__ , use_auth_token=self._token ) SCREAMING_SNAKE_CASE_ = FlaxBertModel.from_pretrained(F'''{USER}/test-model-flax''' ) SCREAMING_SNAKE_CASE_ = flatten_dict(unfreeze(model.params ) ) SCREAMING_SNAKE_CASE_ = flatten_dict(unfreeze(new_model.params ) ) for key in base_params.keys(): SCREAMING_SNAKE_CASE_ = (base_params[key] - new_params[key]).sum().item() self.assertLessEqual(__magic_name__ , 1e-3 , msg=F'''{key} not identical''' ) def __A ( self : int ) -> Tuple: SCREAMING_SNAKE_CASE_ = BertConfig( vocab_size=99 , hidden_size=32 , num_hidden_layers=5 , num_attention_heads=4 , intermediate_size=37 ) SCREAMING_SNAKE_CASE_ = FlaxBertModel(__magic_name__ ) model.push_to_hub("valid_org/test-model-flax-org" , use_auth_token=self._token ) SCREAMING_SNAKE_CASE_ = FlaxBertModel.from_pretrained("valid_org/test-model-flax-org" ) SCREAMING_SNAKE_CASE_ = flatten_dict(unfreeze(model.params ) ) SCREAMING_SNAKE_CASE_ = flatten_dict(unfreeze(new_model.params ) ) for key in base_params.keys(): SCREAMING_SNAKE_CASE_ = (base_params[key] - new_params[key]).sum().item() self.assertLessEqual(__magic_name__ , 1e-3 , msg=F'''{key} not identical''' ) # Reset repo delete_repo(token=self._token , repo_id="valid_org/test-model-flax-org" ) # Push to hub via save_pretrained with tempfile.TemporaryDirectory() as tmp_dir: model.save_pretrained( __magic_name__ , repo_id="valid_org/test-model-flax-org" , push_to_hub=__magic_name__ , use_auth_token=self._token ) SCREAMING_SNAKE_CASE_ = FlaxBertModel.from_pretrained("valid_org/test-model-flax-org" ) SCREAMING_SNAKE_CASE_ = flatten_dict(unfreeze(model.params ) ) SCREAMING_SNAKE_CASE_ = flatten_dict(unfreeze(new_model.params ) ) for key in base_params.keys(): SCREAMING_SNAKE_CASE_ = (base_params[key] - new_params[key]).sum().item() self.assertLessEqual(__magic_name__ , 1e-3 , msg=F'''{key} not identical''' ) def a__ ( __UpperCamelCase , __UpperCamelCase ): SCREAMING_SNAKE_CASE_ = True SCREAMING_SNAKE_CASE_ = flatten_dict(modela.params ) SCREAMING_SNAKE_CASE_ = flatten_dict(modela.params ) for key in flat_params_a.keys(): if np.sum(np.abs(flat_params_a[key] - flat_params_a[key] ) ) > 1E-4: SCREAMING_SNAKE_CASE_ = False return models_are_equal @require_flax class lowerCamelCase (unittest.TestCase ): """simple docstring""" def __A ( self : str ) -> Dict: SCREAMING_SNAKE_CASE_ = BertConfig.from_pretrained("hf-internal-testing/tiny-bert-flax-only" ) SCREAMING_SNAKE_CASE_ = FlaxBertModel(__magic_name__ ) SCREAMING_SNAKE_CASE_ = "bert" with tempfile.TemporaryDirectory() as tmp_dir: model.save_pretrained(os.path.join(__magic_name__ , __magic_name__ ) ) with self.assertRaises(__magic_name__ ): SCREAMING_SNAKE_CASE_ = FlaxBertModel.from_pretrained(__magic_name__ ) SCREAMING_SNAKE_CASE_ = FlaxBertModel.from_pretrained(__magic_name__ , subfolder=__magic_name__ ) self.assertTrue(check_models_equal(__magic_name__ , __magic_name__ ) ) def __A ( self : Optional[Any] ) -> Tuple: SCREAMING_SNAKE_CASE_ = BertConfig.from_pretrained("hf-internal-testing/tiny-bert-flax-only" ) SCREAMING_SNAKE_CASE_ = FlaxBertModel(__magic_name__ ) SCREAMING_SNAKE_CASE_ = "bert" with tempfile.TemporaryDirectory() as tmp_dir: model.save_pretrained(os.path.join(__magic_name__ , __magic_name__ ) , max_shard_size="10KB" ) with self.assertRaises(__magic_name__ ): SCREAMING_SNAKE_CASE_ = FlaxBertModel.from_pretrained(__magic_name__ ) SCREAMING_SNAKE_CASE_ = FlaxBertModel.from_pretrained(__magic_name__ , subfolder=__magic_name__ ) self.assertTrue(check_models_equal(__magic_name__ , __magic_name__ ) ) def __A ( self : Optional[int] ) -> Dict: SCREAMING_SNAKE_CASE_ = "bert" SCREAMING_SNAKE_CASE_ = "hf-internal-testing/tiny-random-bert-subfolder" with self.assertRaises(__magic_name__ ): SCREAMING_SNAKE_CASE_ = FlaxBertModel.from_pretrained(__magic_name__ ) SCREAMING_SNAKE_CASE_ = FlaxBertModel.from_pretrained(__magic_name__ , subfolder=__magic_name__ ) self.assertIsNotNone(__magic_name__ ) def __A ( self : List[str] ) -> Dict: SCREAMING_SNAKE_CASE_ = "bert" SCREAMING_SNAKE_CASE_ = "hf-internal-testing/tiny-random-bert-sharded-subfolder" with self.assertRaises(__magic_name__ ): SCREAMING_SNAKE_CASE_ = FlaxBertModel.from_pretrained(__magic_name__ ) SCREAMING_SNAKE_CASE_ = FlaxBertModel.from_pretrained(__magic_name__ , subfolder=__magic_name__ ) self.assertIsNotNone(__magic_name__ )
305
0
import json from typing import List, Optional, Tuple from tokenizers import normalizers from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import logging from .tokenization_squeezebert import SqueezeBertTokenizer A : Optional[Any] = logging.get_logger(__name__) A : Optional[Any] = {'vocab_file': 'vocab.txt', 'tokenizer_file': 'tokenizer.json'} A : Dict = { 'vocab_file': { 'squeezebert/squeezebert-uncased': ( 'https://huggingface.co/squeezebert/squeezebert-uncased/resolve/main/vocab.txt' ), 'squeezebert/squeezebert-mnli': 'https://huggingface.co/squeezebert/squeezebert-mnli/resolve/main/vocab.txt', 'squeezebert/squeezebert-mnli-headless': ( 'https://huggingface.co/squeezebert/squeezebert-mnli-headless/resolve/main/vocab.txt' ), }, 'tokenizer_file': { 'squeezebert/squeezebert-uncased': ( 'https://huggingface.co/squeezebert/squeezebert-uncased/resolve/main/tokenizer.json' ), 'squeezebert/squeezebert-mnli': ( 'https://huggingface.co/squeezebert/squeezebert-mnli/resolve/main/tokenizer.json' ), 'squeezebert/squeezebert-mnli-headless': ( 'https://huggingface.co/squeezebert/squeezebert-mnli-headless/resolve/main/tokenizer.json' ), }, } A : str = { 'squeezebert/squeezebert-uncased': 5_12, 'squeezebert/squeezebert-mnli': 5_12, 'squeezebert/squeezebert-mnli-headless': 5_12, } A : Optional[int] = { 'squeezebert/squeezebert-uncased': {'do_lower_case': True}, 'squeezebert/squeezebert-mnli': {'do_lower_case': True}, 'squeezebert/squeezebert-mnli-headless': {'do_lower_case': True}, } class lowerCamelCase (_lowerCAmelCase ): """simple docstring""" lowerCamelCase__ = VOCAB_FILES_NAMES lowerCamelCase__ = PRETRAINED_VOCAB_FILES_MAP lowerCamelCase__ = PRETRAINED_INIT_CONFIGURATION lowerCamelCase__ = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES lowerCamelCase__ = SqueezeBertTokenizer def __init__( self : Any , __magic_name__ : Union[str, Any]=None , __magic_name__ : Tuple=None , __magic_name__ : List[Any]=True , __magic_name__ : Optional[int]="[UNK]" , __magic_name__ : List[Any]="[SEP]" , __magic_name__ : List[str]="[PAD]" , __magic_name__ : Dict="[CLS]" , __magic_name__ : List[str]="[MASK]" , __magic_name__ : Dict=True , __magic_name__ : Union[str, Any]=None , **__magic_name__ : Tuple , ) -> int: super().__init__( SCREAMING_SNAKE_CASE_ , tokenizer_file=SCREAMING_SNAKE_CASE_ , do_lower_case=SCREAMING_SNAKE_CASE_ , unk_token=SCREAMING_SNAKE_CASE_ , sep_token=SCREAMING_SNAKE_CASE_ , pad_token=SCREAMING_SNAKE_CASE_ , cls_token=SCREAMING_SNAKE_CASE_ , mask_token=SCREAMING_SNAKE_CASE_ , tokenize_chinese_chars=SCREAMING_SNAKE_CASE_ , strip_accents=SCREAMING_SNAKE_CASE_ , **SCREAMING_SNAKE_CASE_ , ) SCREAMING_SNAKE_CASE_ = json.loads(self.backend_tokenizer.normalizer.__getstate__() ) if ( normalizer_state.get("lowercase" , SCREAMING_SNAKE_CASE_ ) != do_lower_case or normalizer_state.get("strip_accents" , SCREAMING_SNAKE_CASE_ ) != strip_accents or normalizer_state.get("handle_chinese_chars" , SCREAMING_SNAKE_CASE_ ) != tokenize_chinese_chars ): SCREAMING_SNAKE_CASE_ = getattr(SCREAMING_SNAKE_CASE_ , normalizer_state.pop("type" ) ) SCREAMING_SNAKE_CASE_ = do_lower_case SCREAMING_SNAKE_CASE_ = strip_accents SCREAMING_SNAKE_CASE_ = tokenize_chinese_chars SCREAMING_SNAKE_CASE_ = normalizer_class(**SCREAMING_SNAKE_CASE_ ) SCREAMING_SNAKE_CASE_ = do_lower_case def __A ( self : Dict , __magic_name__ : List[str] , __magic_name__ : Optional[int]=None ) -> Optional[Any]: SCREAMING_SNAKE_CASE_ = [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 : Tuple , __magic_name__ : Dict , __magic_name__ : int = None ) -> List[int]: SCREAMING_SNAKE_CASE_ = [self.sep_token_id] SCREAMING_SNAKE_CASE_ = [self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep ) * [0] return len(cls + token_ids_a + sep ) * [0] + len(token_ids_a + sep ) * [1] def __A ( self : Optional[int] , __magic_name__ : str , __magic_name__ : Tuple = None ) -> Tuple[str]: SCREAMING_SNAKE_CASE_ = self._tokenizer.model.save(SCREAMING_SNAKE_CASE_ , name=SCREAMING_SNAKE_CASE_ ) return tuple(SCREAMING_SNAKE_CASE_ )
366
# Copyright 2021 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. import argparse import os from accelerate.utils import ComputeEnvironment from .cluster import get_cluster_input from .config_args import cache_dir, default_config_file, default_yaml_config_file, load_config_from_file # noqa: F401 from .config_utils import _ask_field, _ask_options, _convert_compute_environment # noqa: F401 from .sagemaker import get_sagemaker_input A : Union[str, Any] = "Launches a series of prompts to create and save a `default_config.yaml` configuration file for your training system. Should always be ran first on your machine" def a__ ( ): SCREAMING_SNAKE_CASE_ = _ask_options( "In which compute environment are you running?" , ["This machine", "AWS (Amazon SageMaker)"] , _convert_compute_environment , ) if compute_environment == ComputeEnvironment.AMAZON_SAGEMAKER: SCREAMING_SNAKE_CASE_ = get_sagemaker_input() else: SCREAMING_SNAKE_CASE_ = get_cluster_input() return config def a__ ( __UpperCamelCase=None ): if subparsers is not None: SCREAMING_SNAKE_CASE_ = subparsers.add_parser("config" , description=__UpperCamelCase ) else: SCREAMING_SNAKE_CASE_ = argparse.ArgumentParser("Accelerate config command" , description=__UpperCamelCase ) parser.add_argument( "--config_file" , default=__UpperCamelCase , help=( "The path to use to store the config file. Will default to a file named default_config.yaml in the cache " "location, which is the content of the environment `HF_HOME` suffixed with 'accelerate', or if you don't have " "such an environment variable, your cache directory ('~/.cache' or the content of `XDG_CACHE_HOME`) suffixed " "with 'huggingface'." ) , ) if subparsers is not None: parser.set_defaults(func=__UpperCamelCase ) return parser def a__ ( __UpperCamelCase ): SCREAMING_SNAKE_CASE_ = get_user_input() if args.config_file is not None: SCREAMING_SNAKE_CASE_ = args.config_file else: if not os.path.isdir(__UpperCamelCase ): os.makedirs(__UpperCamelCase ) SCREAMING_SNAKE_CASE_ = default_yaml_config_file if config_file.endswith(".json" ): config.to_json_file(__UpperCamelCase ) else: config.to_yaml_file(__UpperCamelCase ) print(F'''accelerate configuration saved at {config_file}''' ) def a__ ( ): SCREAMING_SNAKE_CASE_ = config_command_parser() SCREAMING_SNAKE_CASE_ = parser.parse_args() config_command(__UpperCamelCase ) if __name__ == "__main__": main()
305
0
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 lowerCamelCase : """simple docstring""" def __init__( self : Optional[Any] , __magic_name__ : Optional[int] , __magic_name__ : Optional[Any]=13 , __magic_name__ : Any=7 , __magic_name__ : Optional[Any]=True , __magic_name__ : Dict=True , __magic_name__ : List[Any]=True , __magic_name__ : Any=True , __magic_name__ : int=99 , __magic_name__ : Union[str, Any]=32 , __magic_name__ : Union[str, Any]=5 , __magic_name__ : int=4 , __magic_name__ : str=4 , __magic_name__ : Any="gelu" , __magic_name__ : Union[str, Any]=0.0 , __magic_name__ : Tuple=0.1 , __magic_name__ : Optional[Any]=True , __magic_name__ : Union[str, Any]=512 , __magic_name__ : Tuple=16 , __magic_name__ : List[Any]=2 , __magic_name__ : List[Any]=0.02 , __magic_name__ : Union[str, Any]=3 , __magic_name__ : List[str]=4 , __magic_name__ : Union[str, Any]=None , ) -> Optional[int]: SCREAMING_SNAKE_CASE_ = parent SCREAMING_SNAKE_CASE_ = batch_size SCREAMING_SNAKE_CASE_ = seq_length 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_multiple_size SCREAMING_SNAKE_CASE_ = hidden_act SCREAMING_SNAKE_CASE_ = hidden_dropout SCREAMING_SNAKE_CASE_ = attention_dropout SCREAMING_SNAKE_CASE_ = weight_tying 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_ = num_labels SCREAMING_SNAKE_CASE_ = num_choices SCREAMING_SNAKE_CASE_ = scope def __A ( self : List[str] ) -> Tuple: SCREAMING_SNAKE_CASE_ = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) SCREAMING_SNAKE_CASE_ = None if self.use_input_mask: SCREAMING_SNAKE_CASE_ = random_attention_mask([self.batch_size, self.seq_length] ) SCREAMING_SNAKE_CASE_ = None if self.use_labels: SCREAMING_SNAKE_CASE_ = ids_tensor([self.batch_size, self.seq_length] , self.num_labels ) SCREAMING_SNAKE_CASE_ = self.get_config() return config, input_ids, input_mask, token_labels def __A ( self : Dict ) -> Any: 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=UpperCamelCase__ , initializer_range=self.initializer_range , ) def __A ( self : str ) -> Union[str, Any]: SCREAMING_SNAKE_CASE_ = self.prepare_config_and_inputs() SCREAMING_SNAKE_CASE_ = True return config, input_ids, input_mask, token_labels def __A ( self : List[Any] , __magic_name__ : Tuple , __magic_name__ : Union[str, Any] , __magic_name__ : List[str] ) -> Dict: SCREAMING_SNAKE_CASE_ = GPTNeoXJapaneseModel(config=UpperCamelCase__ ) model.to(UpperCamelCase__ ) model.eval() SCREAMING_SNAKE_CASE_ = model(UpperCamelCase__ , attention_mask=UpperCamelCase__ ) SCREAMING_SNAKE_CASE_ = model(UpperCamelCase__ ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def __A ( self : Tuple , __magic_name__ : Optional[Any] , __magic_name__ : Union[str, Any] , __magic_name__ : Optional[Any] ) -> Tuple: SCREAMING_SNAKE_CASE_ = True SCREAMING_SNAKE_CASE_ = GPTNeoXJapaneseModel(UpperCamelCase__ ) model.to(UpperCamelCase__ ) model.eval() SCREAMING_SNAKE_CASE_ = model(UpperCamelCase__ , attention_mask=UpperCamelCase__ ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def __A ( self : Dict , __magic_name__ : Union[str, Any] , __magic_name__ : Dict , __magic_name__ : Optional[Any] , __magic_name__ : List[str] ) -> Optional[int]: SCREAMING_SNAKE_CASE_ = GPTNeoXJapaneseForCausalLM(config=UpperCamelCase__ ) model.to(UpperCamelCase__ ) model.eval() SCREAMING_SNAKE_CASE_ = model(UpperCamelCase__ , attention_mask=UpperCamelCase__ , labels=UpperCamelCase__ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.vocab_size) ) def __A ( self : Union[str, Any] , __magic_name__ : Union[str, Any] , __magic_name__ : Union[str, Any] , __magic_name__ : Tuple ) -> List[Any]: SCREAMING_SNAKE_CASE_ = True SCREAMING_SNAKE_CASE_ = GPTNeoXJapaneseForCausalLM(config=UpperCamelCase__ ) model.to(UpperCamelCase__ ) model.eval() # first forward pass SCREAMING_SNAKE_CASE_ = model(UpperCamelCase__ , attention_mask=UpperCamelCase__ , use_cache=UpperCamelCase__ ) SCREAMING_SNAKE_CASE_ = outputs.past_key_values # create hypothetical multiple next token and extent to next_input_ids SCREAMING_SNAKE_CASE_ = ids_tensor((self.batch_size, 3) , config.vocab_size ) SCREAMING_SNAKE_CASE_ = ids_tensor((self.batch_size, 3) , vocab_size=2 ) # append to next input_ids and SCREAMING_SNAKE_CASE_ = torch.cat([input_ids, next_tokens] , dim=-1 ) SCREAMING_SNAKE_CASE_ = torch.cat([input_mask, next_mask] , dim=-1 ) SCREAMING_SNAKE_CASE_ = model(UpperCamelCase__ , attention_mask=UpperCamelCase__ , output_hidden_states=UpperCamelCase__ ) SCREAMING_SNAKE_CASE_ = output_from_no_past['''hidden_states'''][0] SCREAMING_SNAKE_CASE_ = model( UpperCamelCase__ , attention_mask=UpperCamelCase__ , past_key_values=UpperCamelCase__ , output_hidden_states=UpperCamelCase__ , )['''hidden_states'''][0] # select random slice SCREAMING_SNAKE_CASE_ = ids_tensor((1,) , output_from_past.shape[-1] ).item() SCREAMING_SNAKE_CASE_ = output_from_no_past[:, -3:, random_slice_idx].detach() SCREAMING_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(UpperCamelCase__ , UpperCamelCase__ , atol=1e-3 ) ) def __A ( self : Optional[Any] ) -> Any: SCREAMING_SNAKE_CASE_ = self.prepare_config_and_inputs() SCREAMING_SNAKE_CASE_ = config_and_inputs SCREAMING_SNAKE_CASE_ = {'''input_ids''': input_ids, '''attention_mask''': input_mask} return config, inputs_dict @require_torch class lowerCamelCase (__SCREAMING_SNAKE_CASE , __SCREAMING_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 __A ( self : List[Any] ) -> Dict: SCREAMING_SNAKE_CASE_ = GPTNeoXJapaneseModelTester(self ) SCREAMING_SNAKE_CASE_ = ConfigTester(self , config_class=UpperCamelCase__ , hidden_size=37 ) def __A ( self : Tuple ) -> Any: self.config_tester.run_common_tests() def __A ( self : List[Any] ) -> Dict: SCREAMING_SNAKE_CASE_ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ ) def __A ( self : List[Any] ) -> List[str]: SCREAMING_SNAKE_CASE_ = self.model_tester.prepare_config_and_inputs_for_decoder() self.model_tester.create_and_check_model_as_decoder(UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ ) def __A ( self : Optional[int] ) -> Optional[Any]: SCREAMING_SNAKE_CASE_ = self.model_tester.prepare_config_and_inputs_for_decoder() SCREAMING_SNAKE_CASE_ = None self.model_tester.create_and_check_model_as_decoder(UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ ) def __A ( self : Union[str, Any] ) -> List[Any]: SCREAMING_SNAKE_CASE_ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_decoder_model_past_large_inputs(UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ ) def __A ( self : Any ) -> Optional[Any]: SCREAMING_SNAKE_CASE_ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_causal_lm(*UpperCamelCase__ ) @slow def __A ( self : Optional[int] ) -> str: SCREAMING_SNAKE_CASE_ = '''abeja/gpt-neox-japanese-2.7b''' SCREAMING_SNAKE_CASE_ = ['''データサイエンティストとは、''', '''100年後に必要とされる会社は、''', '''フルリモートの環境で働くために必要なことは、''', '''国境の長いトンネルを抜けると''', '''美味しい日本食といえば、'''] SCREAMING_SNAKE_CASE_ = [ '''データサイエンティストとは、データを分析し、ビジネスに役立つ知見を導き出す専門家のことです。''', '''100年後に必要とされる会社は、「人」が中心の会社です。''', '''フルリモートの環境で働くために必要なことは、「自分の時間をコントロールする」ことです。''', '''国境の長いトンネルを抜けると、そこは雪国だった。''', '''美味しい日本食といえば、やっぱりお寿司ですよね。''', ] SCREAMING_SNAKE_CASE_ = GPTNeoXJapaneseTokenizer.from_pretrained(UpperCamelCase__ ) SCREAMING_SNAKE_CASE_ = GPTNeoXJapaneseForCausalLM.from_pretrained(UpperCamelCase__ ) SCREAMING_SNAKE_CASE_ = [] for prompt in prompts: SCREAMING_SNAKE_CASE_ = tokenizer(UpperCamelCase__ , return_tensors="pt" ).input_ids SCREAMING_SNAKE_CASE_ = model.generate(UpperCamelCase__ , max_length=50 ) SCREAMING_SNAKE_CASE_ = tokenizer.batch_decode(UpperCamelCase__ , skip_special_tokens=UpperCamelCase__ ) predicted_outputs += generated_string self.assertListEqual(UpperCamelCase__ , UpperCamelCase__ )
367
from dataclasses import dataclass, field from typing import ClassVar, Dict from ..features import Features, Value from .base import TaskTemplate @dataclass(frozen=SCREAMING_SNAKE_CASE__ ) class lowerCamelCase (SCREAMING_SNAKE_CASE__ ): """simple docstring""" lowerCamelCase__ = field(default='''summarization''' , metadata={'''include_in_asdict_even_if_is_default''': True} ) lowerCamelCase__ = Features({'''text''': Value('''string''' )} ) lowerCamelCase__ = Features({'''summary''': Value('''string''' )} ) lowerCamelCase__ = "text" lowerCamelCase__ = "summary" @property def __A ( self : Dict ) -> Dict[str, str]: return {self.text_column: "text", self.summary_column: "summary"}
305
0
def a__ ( __UpperCamelCase ): SCREAMING_SNAKE_CASE_ = set() # edges = list of graph's edges SCREAMING_SNAKE_CASE_ = 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: SCREAMING_SNAKE_CASE_ = 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 a__ ( __UpperCamelCase ): SCREAMING_SNAKE_CASE_ = 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)}")
368
from ....utils import logging A : List[str] = logging.get_logger(__name__) class lowerCamelCase (SCREAMING_SNAKE_CASE__ ): """simple docstring""" def __init__( self : List[str] , __magic_name__ : Optional[Any] , __magic_name__ : Any=None , __magic_name__ : List[str]=2_048 ) -> List[Any]: SCREAMING_SNAKE_CASE_ = config.__dict__ SCREAMING_SNAKE_CASE_ = modal_hidden_size if num_labels: SCREAMING_SNAKE_CASE_ = num_labels
305
0
class lowerCamelCase : """simple docstring""" def __init__( self : str ) -> List[str]: SCREAMING_SNAKE_CASE_ = "" SCREAMING_SNAKE_CASE_ = "" SCREAMING_SNAKE_CASE_ = [] def __A ( self : Tuple , __magic_name__ : int , __magic_name__ : int ) -> int: if m == -1: return n + 1 elif n == -1: return m + 1 elif self.dp[m][n] > -1: return self.dp[m][n] else: if self.worda[m] == self.worda[n]: SCREAMING_SNAKE_CASE_ = self.__min_dist_top_down_dp(m - 1 , n - 1 ) else: SCREAMING_SNAKE_CASE_ = self.__min_dist_top_down_dp(UpperCamelCase__ , n - 1 ) SCREAMING_SNAKE_CASE_ = self.__min_dist_top_down_dp(m - 1 , UpperCamelCase__ ) SCREAMING_SNAKE_CASE_ = self.__min_dist_top_down_dp(m - 1 , n - 1 ) SCREAMING_SNAKE_CASE_ = 1 + min(UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ ) return self.dp[m][n] def __A ( self : List[str] , __magic_name__ : str , __magic_name__ : str ) -> int: SCREAMING_SNAKE_CASE_ = worda SCREAMING_SNAKE_CASE_ = worda SCREAMING_SNAKE_CASE_ = [[-1 for _ in range(len(UpperCamelCase__ ) )] for _ in range(len(UpperCamelCase__ ) )] return self.__min_dist_top_down_dp(len(UpperCamelCase__ ) - 1 , len(UpperCamelCase__ ) - 1 ) def __A ( self : List[Any] , __magic_name__ : str , __magic_name__ : str ) -> int: SCREAMING_SNAKE_CASE_ = worda SCREAMING_SNAKE_CASE_ = worda SCREAMING_SNAKE_CASE_ = len(UpperCamelCase__ ) SCREAMING_SNAKE_CASE_ = len(UpperCamelCase__ ) SCREAMING_SNAKE_CASE_ = [[0 for _ in range(n + 1 )] for _ in range(m + 1 )] for i in range(m + 1 ): for j in range(n + 1 ): if i == 0: # first string is empty SCREAMING_SNAKE_CASE_ = j elif j == 0: # second string is empty SCREAMING_SNAKE_CASE_ = i elif worda[i - 1] == worda[j - 1]: # last characters are equal SCREAMING_SNAKE_CASE_ = self.dp[i - 1][j - 1] else: SCREAMING_SNAKE_CASE_ = self.dp[i][j - 1] SCREAMING_SNAKE_CASE_ = self.dp[i - 1][j] SCREAMING_SNAKE_CASE_ = self.dp[i - 1][j - 1] SCREAMING_SNAKE_CASE_ = 1 + min(UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ ) return self.dp[m][n] if __name__ == "__main__": A : Union[str, Any] = EditDistance() print("****************** Testing Edit Distance DP Algorithm ******************") print() A : List[Any] = input("Enter the first string: ").strip() A : int = input("Enter the second string: ").strip() print() print(f"The minimum edit distance is: {solver.min_dist_top_down(Sa, Sa)}") print(f"The minimum edit distance is: {solver.min_dist_bottom_up(Sa, Sa)}") print() print("*************** End of Testing Edit Distance DP Algorithm ***************")
369
import warnings from typing import List, Optional, Union from ...processing_utils import ProcessorMixin from ...tokenization_utils_base import BatchEncoding, PaddingStrategy, PreTokenizedInput, TextInput, TruncationStrategy from ...utils import TensorType class lowerCamelCase (SCREAMING_SNAKE_CASE__ ): """simple docstring""" lowerCamelCase__ = ['''image_processor''', '''tokenizer'''] lowerCamelCase__ = '''ViltImageProcessor''' lowerCamelCase__ = ('''BertTokenizer''', '''BertTokenizerFast''') def __init__( self : Optional[int] , __magic_name__ : str=None , __magic_name__ : List[str]=None , **__magic_name__ : Any ) -> str: SCREAMING_SNAKE_CASE_ = None if "feature_extractor" in kwargs: warnings.warn( "The `feature_extractor` argument is deprecated and will be removed in v5, use `image_processor`" " instead." , __magic_name__ , ) SCREAMING_SNAKE_CASE_ = kwargs.pop("feature_extractor" ) SCREAMING_SNAKE_CASE_ = image_processor if image_processor is not None else feature_extractor if image_processor is None: raise ValueError("You need to specify an `image_processor`." ) if tokenizer is None: raise ValueError("You need to specify a `tokenizer`." ) super().__init__(__magic_name__ , __magic_name__ ) SCREAMING_SNAKE_CASE_ = self.image_processor def __call__( self : List[str] , __magic_name__ : List[str] , __magic_name__ : Union[TextInput, PreTokenizedInput, List[TextInput], List[PreTokenizedInput]] = None , __magic_name__ : bool = True , __magic_name__ : Union[bool, str, PaddingStrategy] = False , __magic_name__ : Union[bool, str, TruncationStrategy] = None , __magic_name__ : Optional[int] = None , __magic_name__ : int = 0 , __magic_name__ : Optional[int] = None , __magic_name__ : Optional[bool] = None , __magic_name__ : Optional[bool] = None , __magic_name__ : bool = False , __magic_name__ : bool = False , __magic_name__ : bool = False , __magic_name__ : bool = False , __magic_name__ : bool = True , __magic_name__ : Optional[Union[str, TensorType]] = None , **__magic_name__ : str , ) -> BatchEncoding: SCREAMING_SNAKE_CASE_ = self.tokenizer( text=__magic_name__ , add_special_tokens=__magic_name__ , padding=__magic_name__ , truncation=__magic_name__ , max_length=__magic_name__ , stride=__magic_name__ , pad_to_multiple_of=__magic_name__ , return_token_type_ids=__magic_name__ , return_attention_mask=__magic_name__ , return_overflowing_tokens=__magic_name__ , return_special_tokens_mask=__magic_name__ , return_offsets_mapping=__magic_name__ , return_length=__magic_name__ , verbose=__magic_name__ , return_tensors=__magic_name__ , **__magic_name__ , ) # add pixel_values + pixel_mask SCREAMING_SNAKE_CASE_ = self.image_processor(__magic_name__ , return_tensors=__magic_name__ ) encoding.update(__magic_name__ ) return encoding def __A ( self : Optional[int] , *__magic_name__ : List[Any] , **__magic_name__ : Optional[Any] ) -> Any: return self.tokenizer.batch_decode(*__magic_name__ , **__magic_name__ ) def __A ( self : Dict , *__magic_name__ : List[Any] , **__magic_name__ : Union[str, Any] ) -> str: return self.tokenizer.decode(*__magic_name__ , **__magic_name__ ) @property def __A ( self : Optional[int] ) -> Union[str, Any]: SCREAMING_SNAKE_CASE_ = self.tokenizer.model_input_names SCREAMING_SNAKE_CASE_ = self.image_processor.model_input_names return list(dict.fromkeys(tokenizer_input_names + image_processor_input_names ) ) @property def __A ( self : Dict ) -> List[Any]: warnings.warn( "`feature_extractor_class` is deprecated and will be removed in v5. Use `image_processor_class` instead." , __magic_name__ , ) return self.image_processor_class @property def __A ( self : int ) -> List[Any]: warnings.warn( "`feature_extractor` is deprecated and will be removed in v5. Use `image_processor` instead." , __magic_name__ , ) return self.image_processor
305
0
import gc import unittest import numpy as np import torch from diffusers import AutoencoderKL, DDIMScheduler, DiTPipeline, DPMSolverMultistepScheduler, TransformeraDModel from diffusers.utils import is_xformers_available, load_numpy, slow, torch_device from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu from ..pipeline_params import ( CLASS_CONDITIONED_IMAGE_GENERATION_BATCH_PARAMS, CLASS_CONDITIONED_IMAGE_GENERATION_PARAMS, ) from ..test_pipelines_common import PipelineTesterMixin enable_full_determinism() class lowerCamelCase (_lowerCAmelCase , unittest.TestCase ): """simple docstring""" lowerCamelCase__ = DiTPipeline lowerCamelCase__ = CLASS_CONDITIONED_IMAGE_GENERATION_PARAMS lowerCamelCase__ = PipelineTesterMixin.required_optional_params - { "latents", "num_images_per_prompt", "callback", "callback_steps", } lowerCamelCase__ = CLASS_CONDITIONED_IMAGE_GENERATION_BATCH_PARAMS lowerCamelCase__ = False def __A ( self : str ) -> int: torch.manual_seed(0 ) SCREAMING_SNAKE_CASE_ = TransformeraDModel( sample_size=16 , num_layers=2 , patch_size=4 , attention_head_dim=8 , num_attention_heads=2 , in_channels=4 , out_channels=8 , attention_bias=_lowercase , activation_fn="gelu-approximate" , num_embeds_ada_norm=1_000 , norm_type="ada_norm_zero" , norm_elementwise_affine=_lowercase , ) SCREAMING_SNAKE_CASE_ = AutoencoderKL() SCREAMING_SNAKE_CASE_ = DDIMScheduler() SCREAMING_SNAKE_CASE_ = {"transformer": transformer.eval(), "vae": vae.eval(), "scheduler": scheduler} return components def __A ( self : Optional[int] , __magic_name__ : List[Any] , __magic_name__ : str=0 ) -> str: if str(_lowercase ).startswith("mps" ): SCREAMING_SNAKE_CASE_ = torch.manual_seed(_lowercase ) else: SCREAMING_SNAKE_CASE_ = torch.Generator(device=_lowercase ).manual_seed(_lowercase ) SCREAMING_SNAKE_CASE_ = { "class_labels": [1], "generator": generator, "num_inference_steps": 2, "output_type": "numpy", } return inputs def __A ( self : Any ) -> int: SCREAMING_SNAKE_CASE_ = "cpu" SCREAMING_SNAKE_CASE_ = self.get_dummy_components() SCREAMING_SNAKE_CASE_ = self.pipeline_class(**_lowercase ) pipe.to(_lowercase ) pipe.set_progress_bar_config(disable=_lowercase ) SCREAMING_SNAKE_CASE_ = self.get_dummy_inputs(_lowercase ) SCREAMING_SNAKE_CASE_ = pipe(**_lowercase ).images SCREAMING_SNAKE_CASE_ = image[0, -3:, -3:, -1] self.assertEqual(image.shape , (1, 16, 16, 3) ) SCREAMING_SNAKE_CASE_ = np.array([0.2946, 0.6601, 0.4329, 0.3296, 0.4144, 0.5319, 0.7273, 0.5013, 0.4457] ) SCREAMING_SNAKE_CASE_ = np.abs(image_slice.flatten() - expected_slice ).max() self.assertLessEqual(_lowercase , 1e-3 ) def __A ( self : Any ) -> int: self._test_inference_batch_single_identical(relax_max_difference=_lowercase , expected_max_diff=1e-3 ) @unittest.skipIf( torch_device != "cuda" or not is_xformers_available() , reason="XFormers attention is only available with CUDA and `xformers` installed" , ) def __A ( self : str ) -> List[str]: self._test_xformers_attention_forwardGenerator_pass(expected_max_diff=1e-3 ) @require_torch_gpu @slow class lowerCamelCase (unittest.TestCase ): """simple docstring""" def __A ( self : Any ) -> List[Any]: super().tearDown() gc.collect() torch.cuda.empty_cache() def __A ( self : Any ) -> str: SCREAMING_SNAKE_CASE_ = torch.manual_seed(0 ) SCREAMING_SNAKE_CASE_ = DiTPipeline.from_pretrained("facebook/DiT-XL-2-256" ) pipe.to("cuda" ) SCREAMING_SNAKE_CASE_ = ["vase", "umbrella", "white shark", "white wolf"] SCREAMING_SNAKE_CASE_ = pipe.get_label_ids(_lowercase ) SCREAMING_SNAKE_CASE_ = pipe(_lowercase , generator=_lowercase , num_inference_steps=40 , output_type="np" ).images for word, image in zip(_lowercase , _lowercase ): SCREAMING_SNAKE_CASE_ = load_numpy( F'''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/dit/{word}.npy''' ) assert np.abs((expected_image - image).max() ) < 1e-2 def __A ( self : Any ) -> List[Any]: SCREAMING_SNAKE_CASE_ = DiTPipeline.from_pretrained("facebook/DiT-XL-2-512" ) SCREAMING_SNAKE_CASE_ = DPMSolverMultistepScheduler.from_config(pipe.scheduler.config ) pipe.to("cuda" ) SCREAMING_SNAKE_CASE_ = ["vase", "umbrella"] SCREAMING_SNAKE_CASE_ = pipe.get_label_ids(_lowercase ) SCREAMING_SNAKE_CASE_ = torch.manual_seed(0 ) SCREAMING_SNAKE_CASE_ = pipe(_lowercase , generator=_lowercase , num_inference_steps=25 , output_type="np" ).images for word, image in zip(_lowercase , _lowercase ): SCREAMING_SNAKE_CASE_ = load_numpy( "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main" F'''/dit/{word}_512.npy''' ) assert np.abs((expected_image - image).max() ) < 1e-1
370
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 ..auto import CONFIG_MAPPING A : str = logging.get_logger(__name__) A : Optional[int] = { "microsoft/table-transformer-detection": ( "https://huggingface.co/microsoft/table-transformer-detection/resolve/main/config.json" ), } class lowerCamelCase (SCREAMING_SNAKE_CASE__ ): """simple docstring""" lowerCamelCase__ = '''table-transformer''' lowerCamelCase__ = ['''past_key_values'''] lowerCamelCase__ = { '''hidden_size''': '''d_model''', '''num_attention_heads''': '''encoder_attention_heads''', } def __init__( self : List[Any] , __magic_name__ : Optional[Any]=True , __magic_name__ : Dict=None , __magic_name__ : Any=3 , __magic_name__ : List[str]=100 , __magic_name__ : Union[str, Any]=6 , __magic_name__ : Dict=2_048 , __magic_name__ : str=8 , __magic_name__ : int=6 , __magic_name__ : List[Any]=2_048 , __magic_name__ : Optional[int]=8 , __magic_name__ : Optional[int]=0.0 , __magic_name__ : List[Any]=0.0 , __magic_name__ : Optional[Any]=True , __magic_name__ : List[Any]="relu" , __magic_name__ : List[str]=256 , __magic_name__ : List[str]=0.1 , __magic_name__ : int=0.0 , __magic_name__ : Optional[Any]=0.0 , __magic_name__ : Tuple=0.02 , __magic_name__ : str=1.0 , __magic_name__ : int=False , __magic_name__ : Dict="sine" , __magic_name__ : Union[str, Any]="resnet50" , __magic_name__ : Optional[Any]=True , __magic_name__ : str=False , __magic_name__ : List[str]=1 , __magic_name__ : int=5 , __magic_name__ : Union[str, Any]=2 , __magic_name__ : Tuple=1 , __magic_name__ : Optional[int]=1 , __magic_name__ : Optional[Any]=5 , __magic_name__ : Optional[int]=2 , __magic_name__ : Union[str, Any]=0.1 , **__magic_name__ : Tuple , ) -> str: 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." ) SCREAMING_SNAKE_CASE_ = CONFIG_MAPPING["resnet"](out_features=["stage4"] ) elif isinstance(__magic_name__ , __magic_name__ ): SCREAMING_SNAKE_CASE_ = backbone_config.get("model_type" ) SCREAMING_SNAKE_CASE_ = CONFIG_MAPPING[backbone_model_type] SCREAMING_SNAKE_CASE_ = config_class.from_dict(__magic_name__ ) # set timm attributes to None SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = None, None, None SCREAMING_SNAKE_CASE_ = use_timm_backbone SCREAMING_SNAKE_CASE_ = backbone_config SCREAMING_SNAKE_CASE_ = num_channels SCREAMING_SNAKE_CASE_ = num_queries SCREAMING_SNAKE_CASE_ = d_model SCREAMING_SNAKE_CASE_ = encoder_ffn_dim SCREAMING_SNAKE_CASE_ = encoder_layers SCREAMING_SNAKE_CASE_ = encoder_attention_heads SCREAMING_SNAKE_CASE_ = decoder_ffn_dim SCREAMING_SNAKE_CASE_ = decoder_layers SCREAMING_SNAKE_CASE_ = decoder_attention_heads SCREAMING_SNAKE_CASE_ = dropout SCREAMING_SNAKE_CASE_ = attention_dropout SCREAMING_SNAKE_CASE_ = activation_dropout SCREAMING_SNAKE_CASE_ = activation_function SCREAMING_SNAKE_CASE_ = init_std SCREAMING_SNAKE_CASE_ = init_xavier_std SCREAMING_SNAKE_CASE_ = encoder_layerdrop SCREAMING_SNAKE_CASE_ = decoder_layerdrop SCREAMING_SNAKE_CASE_ = encoder_layers SCREAMING_SNAKE_CASE_ = auxiliary_loss SCREAMING_SNAKE_CASE_ = position_embedding_type SCREAMING_SNAKE_CASE_ = backbone SCREAMING_SNAKE_CASE_ = use_pretrained_backbone SCREAMING_SNAKE_CASE_ = dilation # Hungarian matcher SCREAMING_SNAKE_CASE_ = class_cost SCREAMING_SNAKE_CASE_ = bbox_cost SCREAMING_SNAKE_CASE_ = giou_cost # Loss coefficients SCREAMING_SNAKE_CASE_ = mask_loss_coefficient SCREAMING_SNAKE_CASE_ = dice_loss_coefficient SCREAMING_SNAKE_CASE_ = bbox_loss_coefficient SCREAMING_SNAKE_CASE_ = giou_loss_coefficient SCREAMING_SNAKE_CASE_ = eos_coefficient super().__init__(is_encoder_decoder=__magic_name__ , **__magic_name__ ) @property def __A ( self : Union[str, Any] ) -> int: return self.encoder_attention_heads @property def __A ( self : Any ) -> int: return self.d_model class lowerCamelCase (SCREAMING_SNAKE_CASE__ ): """simple docstring""" lowerCamelCase__ = version.parse('''1.11''' ) @property def __A ( self : Tuple ) -> Mapping[str, Mapping[int, str]]: return OrderedDict( [ ("pixel_values", {0: "batch", 1: "num_channels", 2: "height", 3: "width"}), ("pixel_mask", {0: "batch"}), ] ) @property def __A ( self : Any ) -> float: return 1e-5 @property def __A ( self : int ) -> int: return 12
305
0
import json import os import unittest from transformers import BatchEncoding, LEDTokenizer, LEDTokenizerFast from transformers.models.led.tokenization_led import VOCAB_FILES_NAMES from transformers.testing_utils import require_tokenizers, require_torch from transformers.utils import cached_property from ...test_tokenization_common import TokenizerTesterMixin @require_tokenizers class lowerCamelCase (_a , unittest.TestCase ): """simple docstring""" lowerCamelCase__ = LEDTokenizer lowerCamelCase__ = LEDTokenizerFast lowerCamelCase__ = True def __A ( self : List[Any] ) -> Tuple: super().setUp() SCREAMING_SNAKE_CASE_ = [ "l", "o", "w", "e", "r", "s", "t", "i", "d", "n", "\u0120", "\u0120l", "\u0120n", "\u0120lo", "\u0120low", "er", "\u0120lowest", "\u0120newer", "\u0120wider", "<unk>", ] SCREAMING_SNAKE_CASE_ = dict(zip(__lowerCAmelCase , range(len(__lowerCAmelCase ) ) ) ) SCREAMING_SNAKE_CASE_ = ["#version: 0.2", "\u0120 l", "\u0120l o", "\u0120lo w", "e r", ""] SCREAMING_SNAKE_CASE_ = {"unk_token": "<unk>"} SCREAMING_SNAKE_CASE_ = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES["vocab_file"] ) SCREAMING_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(__lowerCAmelCase ) + "\n" ) with open(self.merges_file , "w" , encoding="utf-8" ) as fp: fp.write("\n".join(__lowerCAmelCase ) ) def __A ( self : Any , **__magic_name__ : Dict ) -> List[Any]: kwargs.update(self.special_tokens_map ) return self.tokenizer_class.from_pretrained(self.tmpdirname , **__lowerCAmelCase ) def __A ( self : Tuple , **__magic_name__ : str ) -> Optional[int]: kwargs.update(self.special_tokens_map ) return self.rust_tokenizer_class.from_pretrained(self.tmpdirname , **__lowerCAmelCase ) def __A ( self : str , __magic_name__ : Union[str, Any] ) -> List[Any]: return "lower newer", "lower newer" @cached_property def __A ( self : List[str] ) -> Union[str, Any]: return LEDTokenizer.from_pretrained("allenai/led-base-16384" ) @cached_property def __A ( self : Tuple ) -> List[str]: return LEDTokenizerFast.from_pretrained("allenai/led-base-16384" ) @require_torch def __A ( self : Dict ) -> List[str]: SCREAMING_SNAKE_CASE_ = ["A long paragraph for summarization.", "Another paragraph for summarization."] SCREAMING_SNAKE_CASE_ = [0, 250, 251, 17_818, 13, 39_186, 1_938, 4, 2] for tokenizer in [self.default_tokenizer, self.default_tokenizer_fast]: SCREAMING_SNAKE_CASE_ = tokenizer(__lowerCAmelCase , max_length=len(__lowerCAmelCase ) , padding=__lowerCAmelCase , return_tensors="pt" ) self.assertIsInstance(__lowerCAmelCase , __lowerCAmelCase ) self.assertEqual((2, 9) , batch.input_ids.shape ) self.assertEqual((2, 9) , batch.attention_mask.shape ) SCREAMING_SNAKE_CASE_ = batch.input_ids.tolist()[0] self.assertListEqual(__lowerCAmelCase , __lowerCAmelCase ) @require_torch def __A ( self : Optional[int] ) -> Union[str, Any]: SCREAMING_SNAKE_CASE_ = ["A long paragraph for summarization.", "Another paragraph for summarization."] for tokenizer in [self.default_tokenizer, self.default_tokenizer_fast]: SCREAMING_SNAKE_CASE_ = tokenizer(__lowerCAmelCase , padding=__lowerCAmelCase , return_tensors="pt" ) self.assertIn("input_ids" , __lowerCAmelCase ) self.assertIn("attention_mask" , __lowerCAmelCase ) self.assertNotIn("labels" , __lowerCAmelCase ) self.assertNotIn("decoder_attention_mask" , __lowerCAmelCase ) @require_torch def __A ( self : Optional[Any] ) -> int: SCREAMING_SNAKE_CASE_ = [ "Summary of the text.", "Another summary.", ] for tokenizer in [self.default_tokenizer, self.default_tokenizer_fast]: SCREAMING_SNAKE_CASE_ = tokenizer(text_target=__lowerCAmelCase , max_length=32 , padding="max_length" , return_tensors="pt" ) self.assertEqual(32 , targets["input_ids"].shape[1] ) @require_torch def __A ( self : Union[str, Any] ) -> str: for tokenizer in [self.default_tokenizer, self.default_tokenizer_fast]: SCREAMING_SNAKE_CASE_ = tokenizer( ["I am a small frog" * 1_024, "I am a small frog"] , padding=__lowerCAmelCase , truncation=__lowerCAmelCase , return_tensors="pt" ) self.assertIsInstance(__lowerCAmelCase , __lowerCAmelCase ) self.assertEqual(batch.input_ids.shape , (2, 5_122) ) @require_torch def __A ( self : Dict ) -> Optional[Any]: SCREAMING_SNAKE_CASE_ = ["A long paragraph for summarization."] SCREAMING_SNAKE_CASE_ = [ "Summary of the text.", ] for tokenizer in [self.default_tokenizer, self.default_tokenizer_fast]: SCREAMING_SNAKE_CASE_ = tokenizer(__lowerCAmelCase , return_tensors="pt" ) SCREAMING_SNAKE_CASE_ = tokenizer(text_target=__lowerCAmelCase , return_tensors="pt" ) SCREAMING_SNAKE_CASE_ = inputs["input_ids"] SCREAMING_SNAKE_CASE_ = targets["input_ids"] self.assertTrue((input_ids[:, 0] == tokenizer.bos_token_id).all().item() ) self.assertTrue((labels[:, 0] == tokenizer.bos_token_id).all().item() ) self.assertTrue((input_ids[:, -1] == tokenizer.eos_token_id).all().item() ) self.assertTrue((labels[:, -1] == tokenizer.eos_token_id).all().item() ) @require_torch def __A ( self : Union[str, Any] ) -> int: for tokenizer in [self.default_tokenizer, self.default_tokenizer_fast]: SCREAMING_SNAKE_CASE_ = ["Summary of the text.", "Another summary."] SCREAMING_SNAKE_CASE_ = [[0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, -1, -1]] SCREAMING_SNAKE_CASE_ = tokenizer(__lowerCAmelCase , padding=__lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = [[0] * len(__lowerCAmelCase ) for x in encoded_output["input_ids"]] SCREAMING_SNAKE_CASE_ = tokenizer.pad(__lowerCAmelCase ) self.assertSequenceEqual(outputs["global_attention_mask"] , __lowerCAmelCase ) def __A ( self : Union[str, Any] ) -> Optional[int]: pass def __A ( self : Tuple ) -> str: for tokenizer, pretrained_name, kwargs in self.tokenizers_list: with self.subTest(F'''{tokenizer.__class__.__name__} ({pretrained_name})''' ): SCREAMING_SNAKE_CASE_ = self.rust_tokenizer_class.from_pretrained(__lowerCAmelCase , **__lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = self.tokenizer_class.from_pretrained(__lowerCAmelCase , **__lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = "A, <mask> AllenNLP sentence." SCREAMING_SNAKE_CASE_ = tokenizer_r.encode_plus(__lowerCAmelCase , add_special_tokens=__lowerCAmelCase , return_token_type_ids=__lowerCAmelCase ) SCREAMING_SNAKE_CASE_ = tokenizer_p.encode_plus(__lowerCAmelCase , add_special_tokens=__lowerCAmelCase , return_token_type_ids=__lowerCAmelCase ) self.assertEqual(sum(tokens_r["token_type_ids"] ) , sum(tokens_p["token_type_ids"] ) ) self.assertEqual( sum(tokens_r["attention_mask"] ) / len(tokens_r["attention_mask"] ) , sum(tokens_p["attention_mask"] ) / len(tokens_p["attention_mask"] ) , ) SCREAMING_SNAKE_CASE_ = tokenizer_r.convert_ids_to_tokens(tokens_r["input_ids"] ) SCREAMING_SNAKE_CASE_ = tokenizer_p.convert_ids_to_tokens(tokens_p["input_ids"] ) self.assertSequenceEqual(tokens_p["input_ids"] , [0, 250, 6, 50_264, 3_823, 487, 21_992, 3_645, 4, 2] ) self.assertSequenceEqual(tokens_r["input_ids"] , [0, 250, 6, 50_264, 3_823, 487, 21_992, 3_645, 4, 2] ) self.assertSequenceEqual( __lowerCAmelCase , ["<s>", "A", ",", "<mask>", "ĠAllen", "N", "LP", "Ġsentence", ".", "</s>"] ) self.assertSequenceEqual( __lowerCAmelCase , ["<s>", "A", ",", "<mask>", "ĠAllen", "N", "LP", "Ġsentence", ".", "</s>"] )
371
import warnings from diffusers import StableDiffusionImgaImgPipeline # noqa F401 warnings.warn( "The `image_to_image.py` script is outdated. Please use directly `from diffusers import" " StableDiffusionImg2ImgPipeline` instead." )
305
0