code
stringlengths
86
54.5k
code_codestyle
int64
0
371
style_context
stringlengths
87
49.2k
style_context_codestyle
int64
0
349
label
int64
0
1
"""simple docstring""" from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_tf_available, is_torch_available, is_vision_available, ) lowerCAmelCase_ = { 'configuration_mobilevit': ['MOBILEVIT_PRETRAINED_CONFIG_ARCHIVE_MAP', 'MobileViTConfig', 'MobileViTOnnxConfig'], } try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase_ = ['MobileViTFeatureExtractor'] lowerCAmelCase_ = ['MobileViTImageProcessor'] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase_ = [ 'MOBILEVIT_PRETRAINED_MODEL_ARCHIVE_LIST', 'MobileViTForImageClassification', 'MobileViTForSemanticSegmentation', 'MobileViTModel', 'MobileViTPreTrainedModel', ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase_ = [ 'TF_MOBILEVIT_PRETRAINED_MODEL_ARCHIVE_LIST', 'TFMobileViTForImageClassification', 'TFMobileViTForSemanticSegmentation', 'TFMobileViTModel', 'TFMobileViTPreTrainedModel', ] if TYPE_CHECKING: from .configuration_mobilevit import MOBILEVIT_PRETRAINED_CONFIG_ARCHIVE_MAP, MobileViTConfig, MobileViTOnnxConfig try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .feature_extraction_mobilevit import MobileViTFeatureExtractor from .image_processing_mobilevit import MobileViTImageProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_mobilevit import ( MOBILEVIT_PRETRAINED_MODEL_ARCHIVE_LIST, MobileViTForImageClassification, MobileViTForSemanticSegmentation, MobileViTModel, MobileViTPreTrainedModel, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_mobilevit import ( TF_MOBILEVIT_PRETRAINED_MODEL_ARCHIVE_LIST, TFMobileViTForImageClassification, TFMobileViTForSemanticSegmentation, TFMobileViTModel, TFMobileViTPreTrainedModel, ) else: import sys lowerCAmelCase_ = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
16
import unittest from transformers import BigBirdConfig, 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 from transformers.models.big_bird.modeling_flax_big_bird import ( FlaxBigBirdForCausalLM, FlaxBigBirdForMaskedLM, FlaxBigBirdForMultipleChoice, FlaxBigBirdForPreTraining, FlaxBigBirdForQuestionAnswering, FlaxBigBirdForSequenceClassification, FlaxBigBirdForTokenClassification, FlaxBigBirdModel, ) class _lowerCamelCase ( unittest.TestCase ): """simple docstring""" def __init__( self , UpperCAmelCase , UpperCAmelCase=2 , UpperCAmelCase=56 , UpperCAmelCase=True , UpperCAmelCase=True , UpperCAmelCase=True , UpperCAmelCase=True , UpperCAmelCase=99 , UpperCAmelCase=32 , UpperCAmelCase=2 , UpperCAmelCase=2 , UpperCAmelCase=7 , UpperCAmelCase="gelu_new" , UpperCAmelCase=0.1 , UpperCAmelCase=0.1 , UpperCAmelCase=512 , UpperCAmelCase=16 , UpperCAmelCase=2 , UpperCAmelCase=0.02 , UpperCAmelCase=4 , UpperCAmelCase="block_sparse" , UpperCAmelCase=True , UpperCAmelCase=False , UpperCAmelCase=2 , UpperCAmelCase=3 , ) -> Tuple: '''simple docstring''' __snake_case : Optional[int] = parent __snake_case : Tuple = batch_size __snake_case : List[str] = seq_length __snake_case : Optional[int] = is_training __snake_case : int = use_attention_mask __snake_case : Union[str, Any] = use_token_type_ids __snake_case : Any = use_labels __snake_case : List[str] = vocab_size __snake_case : int = hidden_size __snake_case : List[str] = num_hidden_layers __snake_case : List[Any] = num_attention_heads __snake_case : Optional[int] = intermediate_size __snake_case : Union[str, Any] = hidden_act __snake_case : Optional[int] = hidden_dropout_prob __snake_case : Optional[Any] = attention_probs_dropout_prob __snake_case : str = max_position_embeddings __snake_case : List[Any] = type_vocab_size __snake_case : int = type_sequence_label_size __snake_case : Dict = initializer_range __snake_case : List[Any] = num_choices __snake_case : Union[str, Any] = rescale_embeddings __snake_case : List[Any] = attention_type __snake_case : str = use_bias __snake_case : Dict = block_size __snake_case : Optional[Any] = num_random_blocks def UpperCAmelCase ( self ) -> int: '''simple docstring''' __snake_case : Dict = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) __snake_case : Any = None if self.use_attention_mask: __snake_case : Optional[Any] = random_attention_mask([self.batch_size, self.seq_length] ) __snake_case : Union[str, Any] = None if self.use_token_type_ids: __snake_case : Dict = ids_tensor([self.batch_size, self.seq_length] , self.type_vocab_size ) __snake_case : Optional[int] = BigBirdConfig( 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=UpperCAmelCase , initializer_range=self.initializer_range , attention_type=self.attention_type , block_size=self.block_size , num_random_blocks=self.num_random_blocks , use_bias=self.use_bias , rescale_embeddings=self.rescale_embeddings , ) return config, input_ids, token_type_ids, attention_mask def UpperCAmelCase ( self ) -> Any: '''simple docstring''' __snake_case : Optional[int] = self.prepare_config_and_inputs() __snake_case , __snake_case , __snake_case , __snake_case : Dict = config_and_inputs __snake_case : int = { "input_ids": input_ids, "token_type_ids": token_type_ids, "attention_mask": attention_mask, } return config, inputs_dict @require_flax class _lowerCamelCase ( a , unittest.TestCase ): """simple docstring""" UpperCAmelCase_ : Union[str, Any] =( ( FlaxBigBirdForCausalLM, FlaxBigBirdModel, FlaxBigBirdForPreTraining, FlaxBigBirdForMaskedLM, FlaxBigBirdForMultipleChoice, FlaxBigBirdForQuestionAnswering, FlaxBigBirdForSequenceClassification, FlaxBigBirdForTokenClassification, ) if is_flax_available() else () ) UpperCAmelCase_ : Dict =False UpperCAmelCase_ : str =False def UpperCAmelCase ( self ) -> str: '''simple docstring''' __snake_case : Dict = FlaxBigBirdModelTester(self ) @slow # copied from `test_modeling_flax_common` because it takes much longer than other models def UpperCAmelCase ( self ) -> Any: '''simple docstring''' super().test_from_pretrained_save_pretrained() @slow # copied from `test_modeling_flax_common` because it takes much longer than other models def UpperCAmelCase ( self ) -> Optional[int]: '''simple docstring''' super().test_from_pretrained_with_no_automatic_init() @slow # copied from `test_modeling_flax_common` because it takes much longer than other models def UpperCAmelCase ( self ) -> Tuple: '''simple docstring''' super().test_no_automatic_init() @slow # copied from `test_modeling_flax_common` because it takes much longer than other models def UpperCAmelCase ( self ) -> Tuple: '''simple docstring''' super().test_hidden_states_output() @slow def UpperCAmelCase ( self ) -> Dict: '''simple docstring''' for model_class_name in self.all_model_classes: __snake_case : Any = model_class_name.from_pretrained("google/bigbird-roberta-base" ) self.assertIsNotNone(UpperCAmelCase ) def UpperCAmelCase ( self ) -> Optional[int]: '''simple docstring''' if self.test_attn_probs: super().test_attention_outputs() @slow # copied from `test_modeling_flax_common` because it takes much longer than other models def UpperCAmelCase ( self ) -> int: '''simple docstring''' __snake_case , __snake_case : Optional[int] = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: with self.subTest(model_class.__name__ ): __snake_case : Optional[Any] = self._prepare_for_class(UpperCAmelCase , UpperCAmelCase ) __snake_case : Tuple = model_class(UpperCAmelCase ) @jax.jit def model_jitted(UpperCAmelCase , UpperCAmelCase=None , **UpperCAmelCase ): return model(input_ids=UpperCAmelCase , attention_mask=UpperCAmelCase , **UpperCAmelCase ) with self.subTest("JIT Enabled" ): __snake_case : int = model_jitted(**UpperCAmelCase ).to_tuple() with self.subTest("JIT Disabled" ): with jax.disable_jit(): __snake_case : List[Any] = model_jitted(**UpperCAmelCase ).to_tuple() self.assertEqual(len(UpperCAmelCase ) , len(UpperCAmelCase ) ) for jitted_output, output in zip(UpperCAmelCase , UpperCAmelCase ): self.assertEqual(jitted_output.shape , output.shape ) def UpperCAmelCase ( self , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase=1E-5 , UpperCAmelCase="outputs" , UpperCAmelCase=None ) -> int: '''simple docstring''' if name.startswith("outputs.attentions" ): return else: super().check_pt_flax_outputs(UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase )
326
0
from __future__ import annotations def __lowerCamelCase (UpperCAmelCase__ : float , UpperCAmelCase__ : float , UpperCAmelCase__ : float ): if days_between_payments <= 0: raise ValueError("days_between_payments must be > 0" ) if daily_interest_rate < 0: raise ValueError("daily_interest_rate must be >= 0" ) if principal <= 0: raise ValueError("principal must be > 0" ) return principal * daily_interest_rate * days_between_payments def __lowerCamelCase (UpperCAmelCase__ : float , UpperCAmelCase__ : float , UpperCAmelCase__ : float , ): if number_of_compounding_periods <= 0: raise ValueError("number_of_compounding_periods must be > 0" ) if nominal_annual_interest_rate_percentage < 0: raise ValueError("nominal_annual_interest_rate_percentage must be >= 0" ) if principal <= 0: raise ValueError("principal must be > 0" ) return principal * ( (1 + nominal_annual_interest_rate_percentage) ** number_of_compounding_periods - 1 ) def __lowerCamelCase (UpperCAmelCase__ : float , UpperCAmelCase__ : float , UpperCAmelCase__ : float , ): if number_of_years <= 0: raise ValueError("number_of_years must be > 0" ) if nominal_annual_percentage_rate < 0: raise ValueError("nominal_annual_percentage_rate must be >= 0" ) if principal <= 0: raise ValueError("principal must be > 0" ) return compound_interest( UpperCAmelCase__ , nominal_annual_percentage_rate / 3_6_5 , number_of_years * 3_6_5 ) if __name__ == "__main__": import doctest doctest.testmod()
358
from pickle import UnpicklingError import jax import jax.numpy as jnp import numpy as np from flax.serialization import from_bytes from flax.traverse_util import flatten_dict from ..utils import logging _lowerCamelCase : Optional[Any] = logging.get_logger(__name__) def __lowerCamelCase (UpperCAmelCase__ : str , UpperCAmelCase__ : Dict ): try: with open(UpperCAmelCase__ , "rb" ) as flax_state_f: SCREAMING_SNAKE_CASE = from_bytes(UpperCAmelCase__ , flax_state_f.read() ) except UnpicklingError as e: try: with open(UpperCAmelCase__ ) as f: if f.read().startswith("version" ): raise OSError( "You seem to have cloned a repository without having git-lfs installed. Please" " install git-lfs and run `git lfs install` followed by `git lfs pull` in the" " folder you cloned." ) else: raise ValueError from e except (UnicodeDecodeError, ValueError): raise EnvironmentError(F"Unable to convert {model_file} to Flax deserializable object. " ) return load_flax_weights_in_pytorch_model(UpperCAmelCase__ , UpperCAmelCase__ ) def __lowerCamelCase (UpperCAmelCase__ : int , UpperCAmelCase__ : Optional[Any] ): try: import torch # noqa: F401 except ImportError: logger.error( "Loading Flax weights in PyTorch requires both PyTorch and Flax to be installed. Please see" " https://pytorch.org/ and https://flax.readthedocs.io/en/latest/installation.html for installation" " instructions." ) raise # check if we have bf16 weights SCREAMING_SNAKE_CASE = flatten_dict(jax.tree_util.tree_map(lambda UpperCAmelCase__ : x.dtype == jnp.bfloataa , UpperCAmelCase__ ) ).values() if any(UpperCAmelCase__ ): # convert all weights to fp32 if they are bf16 since torch.from_numpy can-not handle bf16 # and bf16 is not fully supported in PT yet. logger.warning( "Found ``bfloat16`` weights in Flax model. Casting all ``bfloat16`` weights to ``float32`` " "before loading those in PyTorch model." ) SCREAMING_SNAKE_CASE = jax.tree_util.tree_map( lambda UpperCAmelCase__ : params.astype(np.floataa ) if params.dtype == jnp.bfloataa else params , UpperCAmelCase__ ) SCREAMING_SNAKE_CASE = "" SCREAMING_SNAKE_CASE = flatten_dict(UpperCAmelCase__ , sep="." ) SCREAMING_SNAKE_CASE = pt_model.state_dict() # keep track of unexpected & missing keys SCREAMING_SNAKE_CASE = [] SCREAMING_SNAKE_CASE = set(pt_model_dict.keys() ) for flax_key_tuple, flax_tensor in flax_state_dict.items(): SCREAMING_SNAKE_CASE = flax_key_tuple.split("." ) if flax_key_tuple_array[-1] == "kernel" and flax_tensor.ndim == 4: SCREAMING_SNAKE_CASE = flax_key_tuple_array[:-1] + ["weight"] SCREAMING_SNAKE_CASE = jnp.transpose(UpperCAmelCase__ , (3, 2, 0, 1) ) elif flax_key_tuple_array[-1] == "kernel": SCREAMING_SNAKE_CASE = flax_key_tuple_array[:-1] + ["weight"] SCREAMING_SNAKE_CASE = flax_tensor.T elif flax_key_tuple_array[-1] == "scale": SCREAMING_SNAKE_CASE = flax_key_tuple_array[:-1] + ["weight"] if "time_embedding" not in flax_key_tuple_array: for i, flax_key_tuple_string in enumerate(UpperCAmelCase__ ): SCREAMING_SNAKE_CASE = ( flax_key_tuple_string.replace("_0" , ".0" ) .replace("_1" , ".1" ) .replace("_2" , ".2" ) .replace("_3" , ".3" ) .replace("_4" , ".4" ) .replace("_5" , ".5" ) .replace("_6" , ".6" ) .replace("_7" , ".7" ) .replace("_8" , ".8" ) .replace("_9" , ".9" ) ) SCREAMING_SNAKE_CASE = ".".join(UpperCAmelCase__ ) if flax_key in pt_model_dict: if flax_tensor.shape != pt_model_dict[flax_key].shape: raise ValueError( F"Flax checkpoint seems to be incorrect. Weight {flax_key_tuple} was expected " F"to be of shape {pt_model_dict[flax_key].shape}, but is {flax_tensor.shape}." ) else: # add weight to pytorch dict SCREAMING_SNAKE_CASE = np.asarray(UpperCAmelCase__ ) if not isinstance(UpperCAmelCase__ , np.ndarray ) else flax_tensor SCREAMING_SNAKE_CASE = torch.from_numpy(UpperCAmelCase__ ) # remove from missing keys missing_keys.remove(UpperCAmelCase__ ) else: # weight is not expected by PyTorch model unexpected_keys.append(UpperCAmelCase__ ) pt_model.load_state_dict(UpperCAmelCase__ ) # re-transform missing_keys to list SCREAMING_SNAKE_CASE = list(UpperCAmelCase__ ) if len(UpperCAmelCase__ ) > 0: logger.warning( "Some weights of the Flax model were not used when initializing the PyTorch model" F" {pt_model.__class__.__name__}: {unexpected_keys}\n- This IS expected if you are initializing" F" {pt_model.__class__.__name__} from a Flax model trained on another task or with another architecture" " (e.g. initializing a BertForSequenceClassification model from a FlaxBertForPreTraining model).\n- This" F" IS NOT expected if you are initializing {pt_model.__class__.__name__} from a Flax model that you expect" " to be exactly identical (e.g. initializing a BertForSequenceClassification model from a" " FlaxBertForSequenceClassification model)." ) if len(UpperCAmelCase__ ) > 0: logger.warning( F"Some weights of {pt_model.__class__.__name__} were not initialized from the Flax model and are newly" F" initialized: {missing_keys}\nYou should probably TRAIN this model on a down-stream task to be able to" " use it for predictions and inference." ) return pt_model
206
0
'''simple docstring''' import torch from diffusers import CMStochasticIterativeScheduler from .test_schedulers import SchedulerCommonTest class _UpperCamelCase ( lowerCamelCase__ ): '''simple docstring''' _A : List[Any] = (CMStochasticIterativeScheduler,) _A : Optional[int] = 10 def UpperCamelCase__ ( self : Any , **lowerCAmelCase__ : int ): """simple docstring""" __SCREAMING_SNAKE_CASE : Union[str, Any] = { """num_train_timesteps""": 2_0_1, """sigma_min""": 0.0_02, """sigma_max""": 80.0, } config.update(**lowerCAmelCase__ ) return config def UpperCamelCase__ ( self : Optional[Any] ): """simple docstring""" __SCREAMING_SNAKE_CASE : List[Any] = 1_0 __SCREAMING_SNAKE_CASE : str = self.get_scheduler_config() __SCREAMING_SNAKE_CASE : Any = self.scheduler_classes[0](**lowerCAmelCase__ ) scheduler.set_timesteps(lowerCAmelCase__ ) __SCREAMING_SNAKE_CASE : Optional[int] = scheduler.timesteps[0] __SCREAMING_SNAKE_CASE : List[str] = scheduler.timesteps[1] __SCREAMING_SNAKE_CASE : Optional[int] = self.dummy_sample __SCREAMING_SNAKE_CASE : List[Any] = 0.1 * sample __SCREAMING_SNAKE_CASE : Optional[Any] = scheduler.step(lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ ).prev_sample __SCREAMING_SNAKE_CASE : List[str] = scheduler.step(lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ ).prev_sample self.assertEqual(output_a.shape , sample.shape ) self.assertEqual(output_a.shape , output_a.shape ) def UpperCamelCase__ ( self : int ): """simple docstring""" for timesteps in [1_0, 5_0, 1_0_0, 1_0_0_0]: self.check_over_configs(num_train_timesteps=lowerCAmelCase__ ) def UpperCamelCase__ ( self : Optional[Any] ): """simple docstring""" for clip_denoised in [True, False]: self.check_over_configs(clip_denoised=lowerCAmelCase__ ) def UpperCamelCase__ ( self : Optional[Any] ): """simple docstring""" __SCREAMING_SNAKE_CASE : Tuple = self.scheduler_classes[0] __SCREAMING_SNAKE_CASE : List[str] = self.get_scheduler_config() __SCREAMING_SNAKE_CASE : str = scheduler_class(**lowerCAmelCase__ ) __SCREAMING_SNAKE_CASE : List[Any] = 1 scheduler.set_timesteps(lowerCAmelCase__ ) __SCREAMING_SNAKE_CASE : Union[str, Any] = scheduler.timesteps __SCREAMING_SNAKE_CASE : Tuple = torch.manual_seed(0 ) __SCREAMING_SNAKE_CASE : Dict = self.dummy_model() __SCREAMING_SNAKE_CASE : int = self.dummy_sample_deter * scheduler.init_noise_sigma for i, t in enumerate(lowerCAmelCase__ ): # 1. scale model input __SCREAMING_SNAKE_CASE : List[Any] = scheduler.scale_model_input(lowerCAmelCase__ , lowerCAmelCase__ ) # 2. predict noise residual __SCREAMING_SNAKE_CASE : List[str] = model(lowerCAmelCase__ , lowerCAmelCase__ ) # 3. predict previous sample x_t-1 __SCREAMING_SNAKE_CASE : Union[str, Any] = scheduler.step(lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , generator=lowerCAmelCase__ ).prev_sample __SCREAMING_SNAKE_CASE : Union[str, Any] = pred_prev_sample __SCREAMING_SNAKE_CASE : Dict = torch.sum(torch.abs(lowerCAmelCase__ ) ) __SCREAMING_SNAKE_CASE : str = torch.mean(torch.abs(lowerCAmelCase__ ) ) assert abs(result_sum.item() - 1_92.76_14 ) < 1E-2 assert abs(result_mean.item() - 0.25_10 ) < 1E-3 def UpperCamelCase__ ( self : List[Any] ): """simple docstring""" __SCREAMING_SNAKE_CASE : Optional[int] = self.scheduler_classes[0] __SCREAMING_SNAKE_CASE : Optional[int] = self.get_scheduler_config() __SCREAMING_SNAKE_CASE : List[str] = scheduler_class(**lowerCAmelCase__ ) __SCREAMING_SNAKE_CASE : Optional[int] = [1_0_6, 0] scheduler.set_timesteps(timesteps=lowerCAmelCase__ ) __SCREAMING_SNAKE_CASE : Optional[int] = scheduler.timesteps __SCREAMING_SNAKE_CASE : Optional[int] = torch.manual_seed(0 ) __SCREAMING_SNAKE_CASE : Optional[Any] = self.dummy_model() __SCREAMING_SNAKE_CASE : Dict = self.dummy_sample_deter * scheduler.init_noise_sigma for t in timesteps: # 1. scale model input __SCREAMING_SNAKE_CASE : Tuple = scheduler.scale_model_input(lowerCAmelCase__ , lowerCAmelCase__ ) # 2. predict noise residual __SCREAMING_SNAKE_CASE : str = model(lowerCAmelCase__ , lowerCAmelCase__ ) # 3. predict previous sample x_t-1 __SCREAMING_SNAKE_CASE : Optional[Any] = scheduler.step(lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , generator=lowerCAmelCase__ ).prev_sample __SCREAMING_SNAKE_CASE : int = pred_prev_sample __SCREAMING_SNAKE_CASE : List[Any] = torch.sum(torch.abs(lowerCAmelCase__ ) ) __SCREAMING_SNAKE_CASE : int = torch.mean(torch.abs(lowerCAmelCase__ ) ) assert abs(result_sum.item() - 3_47.63_57 ) < 1E-2 assert abs(result_mean.item() - 0.45_27 ) < 1E-3 def UpperCamelCase__ ( self : Tuple ): """simple docstring""" __SCREAMING_SNAKE_CASE : Dict = self.scheduler_classes[0] __SCREAMING_SNAKE_CASE : List[str] = self.get_scheduler_config() __SCREAMING_SNAKE_CASE : Tuple = scheduler_class(**lowerCAmelCase__ ) __SCREAMING_SNAKE_CASE : List[str] = [3_9, 3_0, 1_2, 1_5, 0] with self.assertRaises(lowerCAmelCase__ , msg="""`timesteps` must be in descending order.""" ): scheduler.set_timesteps(timesteps=lowerCAmelCase__ ) def UpperCamelCase__ ( self : Optional[Any] ): """simple docstring""" __SCREAMING_SNAKE_CASE : Dict = self.scheduler_classes[0] __SCREAMING_SNAKE_CASE : str = self.get_scheduler_config() __SCREAMING_SNAKE_CASE : List[Any] = scheduler_class(**lowerCAmelCase__ ) __SCREAMING_SNAKE_CASE : Tuple = [3_9, 3_0, 1_2, 1, 0] __SCREAMING_SNAKE_CASE : List[Any] = len(lowerCAmelCase__ ) with self.assertRaises(lowerCAmelCase__ , msg="""Can only pass one of `num_inference_steps` or `timesteps`.""" ): scheduler.set_timesteps(num_inference_steps=lowerCAmelCase__ , timesteps=lowerCAmelCase__ ) def UpperCamelCase__ ( self : Dict ): """simple docstring""" __SCREAMING_SNAKE_CASE : List[Any] = self.scheduler_classes[0] __SCREAMING_SNAKE_CASE : Dict = self.get_scheduler_config() __SCREAMING_SNAKE_CASE : int = scheduler_class(**lowerCAmelCase__ ) __SCREAMING_SNAKE_CASE : Any = [scheduler.config.num_train_timesteps] with self.assertRaises( lowerCAmelCase__ , msg="""`timesteps` must start before `self.config.train_timesteps`: {scheduler.config.num_train_timesteps}}""" , ): scheduler.set_timesteps(timesteps=lowerCAmelCase__ )
112
'''simple docstring''' from __future__ import annotations def lowerCAmelCase_ ( _lowerCamelCase: list[int] ): if not nums: return 0 __SCREAMING_SNAKE_CASE : Optional[int] = nums[0] __SCREAMING_SNAKE_CASE : Union[str, Any] = 0 for num in nums[1:]: __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE : Any = ( max_excluding + num, max(_lowerCamelCase , _lowerCamelCase ), ) return max(_lowerCamelCase , _lowerCamelCase ) if __name__ == "__main__": import doctest doctest.testmod()
112
1
'''simple docstring''' # flake8: noqa # Lint as: python3 a_ : Dict = [ "VerificationMode", "Version", "disable_progress_bar", "enable_progress_bar", "is_progress_bar_enabled", "experimental", ] from .info_utils import VerificationMode from .logging import disable_progress_bar, enable_progress_bar, is_progress_bar_enabled from .version import Version from .experimental import experimental
104
'''simple docstring''' from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available a_ : str = { "configuration_nllb_moe": [ "NLLB_MOE_PRETRAINED_CONFIG_ARCHIVE_MAP", "NllbMoeConfig", ] } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: a_ : Tuple = [ "NLLB_MOE_PRETRAINED_MODEL_ARCHIVE_LIST", "NllbMoeForConditionalGeneration", "NllbMoeModel", "NllbMoePreTrainedModel", "NllbMoeTop2Router", "NllbMoeSparseMLP", ] if TYPE_CHECKING: from .configuration_nllb_moe import ( NLLB_MOE_PRETRAINED_CONFIG_ARCHIVE_MAP, NllbMoeConfig, ) try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_nllb_moe import ( NLLB_MOE_PRETRAINED_MODEL_ARCHIVE_LIST, NllbMoeForConditionalGeneration, NllbMoeModel, NllbMoePreTrainedModel, NllbMoeSparseMLP, NllbMoeTopaRouter, ) else: import sys a_ : str = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
104
1
import os import time import warnings from dataclasses import dataclass, field from enum import Enum from typing import List, Optional, Union import torch from filelock import FileLock from torch.utils.data import Dataset from ...tokenization_utils_base import PreTrainedTokenizerBase from ...utils import logging from ..processors.glue import glue_convert_examples_to_features, glue_output_modes, glue_processors from ..processors.utils import InputFeatures __lowerCAmelCase : str = logging.get_logger(__name__) @dataclass class UpperCAmelCase_ : '''simple docstring''' a__ = field(metadata={"""help""": """The name of the task to train on: """ + """, """.join(glue_processors.keys() )} ) a__ = field( metadata={"""help""": """The input data dir. Should contain the .tsv files (or other data files) for the task."""} ) a__ = field( default=1_28 , metadata={ """help""": ( """The maximum total input sequence length after tokenization. Sequences longer """ """than this will be truncated, sequences shorter will be padded.""" ) } , ) a__ = field( default=_A , metadata={"""help""": """Overwrite the cached training and evaluation sets"""} ) def _lowercase ( self : Optional[Any] ) -> List[str]: """simple docstring""" __magic_name__ = self.task_name.lower() class UpperCAmelCase_ ( _A ): '''simple docstring''' a__ = """train""" a__ = """dev""" a__ = """test""" class UpperCAmelCase_ ( _A ): '''simple docstring''' a__ = 42 a__ = 42 a__ = 42 def __init__( self : List[Any] , UpperCamelCase__ : GlueDataTrainingArguments , UpperCamelCase__ : PreTrainedTokenizerBase , UpperCamelCase__ : Optional[int] = None , UpperCamelCase__ : Union[str, Split] = Split.train , UpperCamelCase__ : Optional[str] = None , ) -> List[Any]: """simple docstring""" warnings.warn( """This dataset will be removed from the library soon, preprocessing should be handled with the 🤗 Datasets """ """library. You can have a look at this example script for pointers: """ """https://github.com/huggingface/transformers/blob/main/examples/pytorch/text-classification/run_glue.py""" , UpperCamelCase__ , ) __magic_name__ = args __magic_name__ = glue_processors[args.task_name]() __magic_name__ = glue_output_modes[args.task_name] if isinstance(UpperCamelCase__ , UpperCamelCase__ ): try: __magic_name__ = Split[mode] except KeyError: raise KeyError("""mode is not a valid split name""" ) # Load data features from cache or dataset file __magic_name__ = os.path.join( cache_dir if cache_dir is not None else args.data_dir , F'''cached_{mode.value}_{tokenizer.__class__.__name__}_{args.max_seq_length}_{args.task_name}''' , ) __magic_name__ = self.processor.get_labels() if args.task_name in ["mnli", "mnli-mm"] and tokenizer.__class__.__name__ in ( "RobertaTokenizer", "RobertaTokenizerFast", "XLMRobertaTokenizer", "BartTokenizer", "BartTokenizerFast", ): # HACK(label indices are swapped in RoBERTa pretrained model) __magic_name__ , __magic_name__ = label_list[2], label_list[1] __magic_name__ = label_list # Make sure only the first process in distributed training processes the dataset, # and the others will use the cache. __magic_name__ = cached_features_file + """.lock""" with FileLock(UpperCamelCase__ ): if os.path.exists(UpperCamelCase__ ) and not args.overwrite_cache: __magic_name__ = time.time() __magic_name__ = torch.load(UpperCamelCase__ ) logger.info( F'''Loading features from cached file {cached_features_file} [took %.3f s]''' , time.time() - start ) else: logger.info(F'''Creating features from dataset file at {args.data_dir}''' ) if mode == Split.dev: __magic_name__ = self.processor.get_dev_examples(args.data_dir ) elif mode == Split.test: __magic_name__ = self.processor.get_test_examples(args.data_dir ) else: __magic_name__ = self.processor.get_train_examples(args.data_dir ) if limit_length is not None: __magic_name__ = examples[:limit_length] __magic_name__ = glue_convert_examples_to_features( UpperCamelCase__ , UpperCamelCase__ , max_length=args.max_seq_length , label_list=UpperCamelCase__ , output_mode=self.output_mode , ) __magic_name__ = time.time() torch.save(self.features , UpperCamelCase__ ) # ^ This seems to take a lot of time so I want to investigate why and how we can improve. logger.info( F'''Saving features into cached file {cached_features_file} [took {time.time() - start:.3f} s]''' ) def __len__( self : Optional[int] ) -> str: """simple docstring""" return len(self.features ) def __getitem__( self : Tuple , UpperCamelCase__ : Dict ) -> InputFeatures: """simple docstring""" return self.features[i] def _lowercase ( self : Optional[Any] ) -> int: """simple docstring""" return self.label_list
88
import re import string import numpy as np import datasets __lowerCAmelCase : Optional[int] = '\nReturns the rate at which the input predicted strings exactly match their references, ignoring any strings input as part of the regexes_to_ignore list.\n' __lowerCAmelCase : Optional[int] = '\nArgs:\n predictions: List of predicted texts.\n references: List of reference texts.\n regexes_to_ignore: List, defaults to None. Regex expressions of characters to\n ignore when calculating the exact matches. Note: these regexes are removed\n from the input data before the changes based on the options below (e.g. ignore_case,\n ignore_punctuation, ignore_numbers) are applied.\n ignore_case: Boolean, defaults to False. If true, turns everything\n to lowercase so that capitalization differences are ignored.\n ignore_punctuation: Boolean, defaults to False. If true, removes all punctuation before\n comparing predictions and references.\n ignore_numbers: Boolean, defaults to False. If true, removes all punctuation before\n comparing predictions and references.\nReturns:\n exact_match: Dictionary containing exact_match rate. Possible values are between 0.0 and 100.0, inclusive.\nExamples:\n >>> exact_match = datasets.load_metric("exact_match")\n >>> refs = ["the cat", "theater", "YELLING", "agent007"]\n >>> preds = ["cat?", "theater", "yelling", "agent"]\n >>> results = exact_match.compute(references=refs, predictions=preds)\n >>> print(round(results["exact_match"], 1))\n 25.0\n\n >>> exact_match = datasets.load_metric("exact_match")\n >>> refs = ["the cat", "theater", "YELLING", "agent007"]\n >>> preds = ["cat?", "theater", "yelling", "agent"]\n >>> results = exact_match.compute(references=refs, predictions=preds, regexes_to_ignore=["the ", "yell"], ignore_case=True, ignore_punctuation=True)\n >>> print(round(results["exact_match"], 1))\n 50.0\n\n\n >>> exact_match = datasets.load_metric("exact_match")\n >>> refs = ["the cat", "theater", "YELLING", "agent007"]\n >>> preds = ["cat?", "theater", "yelling", "agent"]\n >>> results = exact_match.compute(references=refs, predictions=preds, regexes_to_ignore=["the ", "yell", "YELL"], ignore_case=True, ignore_punctuation=True)\n >>> print(round(results["exact_match"], 1))\n 75.0\n\n >>> exact_match = datasets.load_metric("exact_match")\n >>> refs = ["the cat", "theater", "YELLING", "agent007"]\n >>> preds = ["cat?", "theater", "yelling", "agent"]\n >>> results = exact_match.compute(references=refs, predictions=preds, regexes_to_ignore=["the ", "yell", "YELL"], ignore_case=True, ignore_punctuation=True, ignore_numbers=True)\n >>> print(round(results["exact_match"], 1))\n 100.0\n\n >>> exact_match = datasets.load_metric("exact_match")\n >>> refs = ["The cat sat on the mat.", "Theaters are great.", "It\'s like comparing oranges and apples."]\n >>> preds = ["The cat sat on the mat?", "Theaters are great.", "It\'s like comparing apples and oranges."]\n >>> results = exact_match.compute(references=refs, predictions=preds)\n >>> print(round(results["exact_match"], 1))\n 33.3\n\n' __lowerCAmelCase : Optional[int] = '\n' @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class UpperCAmelCase_ ( datasets.Metric ): '''simple docstring''' def _lowercase ( self : str ) -> Optional[int]: """simple docstring""" return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { """predictions""": datasets.Value("""string""" , id="""sequence""" ), """references""": datasets.Value("""string""" , id="""sequence""" ), } ) , reference_urls=[] , ) def _lowercase ( self : Optional[int] , UpperCamelCase__ : Optional[Any] , UpperCamelCase__ : Tuple , UpperCamelCase__ : Optional[Any]=None , UpperCamelCase__ : Optional[Any]=False , UpperCamelCase__ : int=False , UpperCamelCase__ : Tuple=False , ) -> Dict: """simple docstring""" if regexes_to_ignore is not None: for s in regexes_to_ignore: __magic_name__ = np.array([re.sub(UpperCamelCase__ , """""" , UpperCamelCase__ ) for x in predictions] ) __magic_name__ = np.array([re.sub(UpperCamelCase__ , """""" , UpperCamelCase__ ) for x in references] ) else: __magic_name__ = np.asarray(UpperCamelCase__ ) __magic_name__ = np.asarray(UpperCamelCase__ ) if ignore_case: __magic_name__ = np.char.lower(UpperCamelCase__ ) __magic_name__ = np.char.lower(UpperCamelCase__ ) if ignore_punctuation: __magic_name__ = string.punctuation.maketrans("""""" , """""" , string.punctuation ) __magic_name__ = np.char.translate(UpperCamelCase__ , table=UpperCamelCase__ ) __magic_name__ = np.char.translate(UpperCamelCase__ , table=UpperCamelCase__ ) if ignore_numbers: __magic_name__ = string.digits.maketrans("""""" , """""" , string.digits ) __magic_name__ = np.char.translate(UpperCamelCase__ , table=UpperCamelCase__ ) __magic_name__ = np.char.translate(UpperCamelCase__ , table=UpperCamelCase__ ) __magic_name__ = predictions == references return {"exact_match": np.mean(UpperCamelCase__ ) * 100}
88
1
from math import factorial _UpperCAmelCase = {str(d): factorial(d) for d in range(10)} def UpperCamelCase ( snake_case__ : int ) -> int: return sum(DIGIT_FACTORIAL[d] for d in str(snake_case__ ) ) def UpperCamelCase ( ) -> int: UpperCamelCase : int = 7 * factorial(9 ) + 1 return sum(i for i in range(3 , snake_case__ ) if sum_of_digit_factorial(snake_case__ ) == i ) if __name__ == "__main__": print(F"""{solution() = }""")
353
from typing import Optional import pyspark from .. import Features, NamedSplit from ..download import DownloadMode from ..packaged_modules.spark.spark import Spark from .abc import AbstractDatasetReader class lowerCAmelCase_ ( a__ ): def __init__( self, SCREAMING_SNAKE_CASE_, SCREAMING_SNAKE_CASE_ = None, SCREAMING_SNAKE_CASE_ = None, SCREAMING_SNAKE_CASE_ = True, SCREAMING_SNAKE_CASE_ = None, SCREAMING_SNAKE_CASE_ = False, SCREAMING_SNAKE_CASE_ = None, SCREAMING_SNAKE_CASE_ = True, SCREAMING_SNAKE_CASE_ = "arrow", **SCREAMING_SNAKE_CASE_, ) -> int: super().__init__( split=SCREAMING_SNAKE_CASE_, features=SCREAMING_SNAKE_CASE_, cache_dir=SCREAMING_SNAKE_CASE_, keep_in_memory=SCREAMING_SNAKE_CASE_, streaming=SCREAMING_SNAKE_CASE_, **SCREAMING_SNAKE_CASE_, ) UpperCamelCase : Any = load_from_cache_file UpperCamelCase : Any = file_format UpperCamelCase : str = Spark( df=SCREAMING_SNAKE_CASE_, features=SCREAMING_SNAKE_CASE_, cache_dir=SCREAMING_SNAKE_CASE_, working_dir=SCREAMING_SNAKE_CASE_, **SCREAMING_SNAKE_CASE_, ) def snake_case_ ( self ) -> Tuple: if self.streaming: return self.builder.as_streaming_dataset(split=self.split ) UpperCamelCase : Dict = None if self._load_from_cache_file else DownloadMode.FORCE_REDOWNLOAD self.builder.download_and_prepare( download_mode=SCREAMING_SNAKE_CASE_, file_format=self._file_format, ) return self.builder.as_dataset(split=self.split )
103
0
'''simple docstring''' import json from typing import List, Optional, Tuple from tokenizers import normalizers from ...tokenization_utils_base import BatchEncoding from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import PaddingStrategy, logging from .tokenization_realm import RealmTokenizer UpperCamelCase_ = logging.get_logger(__name__) UpperCamelCase_ = {"vocab_file": "vocab.txt", "tokenizer_file": "tokenizer.json"} UpperCamelCase_ = { "vocab_file": { "google/realm-cc-news-pretrained-embedder": ( "https://huggingface.co/google/realm-cc-news-pretrained-embedder/resolve/main/vocab.txt" ), "google/realm-cc-news-pretrained-encoder": ( "https://huggingface.co/google/realm-cc-news-pretrained-encoder/resolve/main/vocab.txt" ), "google/realm-cc-news-pretrained-scorer": ( "https://huggingface.co/google/realm-cc-news-pretrained-scorer/resolve/main/vocab.txt" ), "google/realm-cc-news-pretrained-openqa": ( "https://huggingface.co/google/realm-cc-news-pretrained-openqa/aresolve/main/vocab.txt" ), "google/realm-orqa-nq-openqa": "https://huggingface.co/google/realm-orqa-nq-openqa/resolve/main/vocab.txt", "google/realm-orqa-nq-reader": "https://huggingface.co/google/realm-orqa-nq-reader/resolve/main/vocab.txt", "google/realm-orqa-wq-openqa": "https://huggingface.co/google/realm-orqa-wq-openqa/resolve/main/vocab.txt", "google/realm-orqa-wq-reader": "https://huggingface.co/google/realm-orqa-wq-reader/resolve/main/vocab.txt", }, "tokenizer_file": { "google/realm-cc-news-pretrained-embedder": ( "https://huggingface.co/google/realm-cc-news-pretrained-embedder/resolve/main/tokenizer.jsont" ), "google/realm-cc-news-pretrained-encoder": ( "https://huggingface.co/google/realm-cc-news-pretrained-encoder/resolve/main/tokenizer.json" ), "google/realm-cc-news-pretrained-scorer": ( "https://huggingface.co/google/realm-cc-news-pretrained-scorer/resolve/main/tokenizer.json" ), "google/realm-cc-news-pretrained-openqa": ( "https://huggingface.co/google/realm-cc-news-pretrained-openqa/aresolve/main/tokenizer.json" ), "google/realm-orqa-nq-openqa": ( "https://huggingface.co/google/realm-orqa-nq-openqa/resolve/main/tokenizer.json" ), "google/realm-orqa-nq-reader": ( "https://huggingface.co/google/realm-orqa-nq-reader/resolve/main/tokenizer.json" ), "google/realm-orqa-wq-openqa": ( "https://huggingface.co/google/realm-orqa-wq-openqa/resolve/main/tokenizer.json" ), "google/realm-orqa-wq-reader": ( "https://huggingface.co/google/realm-orqa-wq-reader/resolve/main/tokenizer.json" ), }, } UpperCamelCase_ = { "google/realm-cc-news-pretrained-embedder": 5_1_2, "google/realm-cc-news-pretrained-encoder": 5_1_2, "google/realm-cc-news-pretrained-scorer": 5_1_2, "google/realm-cc-news-pretrained-openqa": 5_1_2, "google/realm-orqa-nq-openqa": 5_1_2, "google/realm-orqa-nq-reader": 5_1_2, "google/realm-orqa-wq-openqa": 5_1_2, "google/realm-orqa-wq-reader": 5_1_2, } UpperCamelCase_ = { "google/realm-cc-news-pretrained-embedder": {"do_lower_case": True}, "google/realm-cc-news-pretrained-encoder": {"do_lower_case": True}, "google/realm-cc-news-pretrained-scorer": {"do_lower_case": True}, "google/realm-cc-news-pretrained-openqa": {"do_lower_case": True}, "google/realm-orqa-nq-openqa": {"do_lower_case": True}, "google/realm-orqa-nq-reader": {"do_lower_case": True}, "google/realm-orqa-wq-openqa": {"do_lower_case": True}, "google/realm-orqa-wq-reader": {"do_lower_case": True}, } class _a ( SCREAMING_SNAKE_CASE ): '''simple docstring''' A : List[str] = VOCAB_FILES_NAMES A : List[Any] = PRETRAINED_VOCAB_FILES_MAP A : str = PRETRAINED_INIT_CONFIGURATION A : Dict = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES A : Union[str, Any] = RealmTokenizer def __init__( self, A=None, A=None, A=True, A="[UNK]", A="[SEP]", A="[PAD]", A="[CLS]", A="[MASK]", A=True, A=None, **A, ): '''simple docstring''' super().__init__( A, tokenizer_file=A, do_lower_case=A, unk_token=A, sep_token=A, pad_token=A, cls_token=A, mask_token=A, tokenize_chinese_chars=A, strip_accents=A, **A, ) SCREAMING_SNAKE_CASE : List[str] = json.loads(self.backend_tokenizer.normalizer.__getstate__() ) if ( normalizer_state.get('lowercase', A ) != do_lower_case or normalizer_state.get('strip_accents', A ) != strip_accents or normalizer_state.get('handle_chinese_chars', A ) != tokenize_chinese_chars ): SCREAMING_SNAKE_CASE : Tuple = getattr(A, normalizer_state.pop('type' ) ) SCREAMING_SNAKE_CASE : str = do_lower_case SCREAMING_SNAKE_CASE : List[str] = strip_accents SCREAMING_SNAKE_CASE : Any = tokenize_chinese_chars SCREAMING_SNAKE_CASE : Optional[Any] = normalizer_class(**A ) SCREAMING_SNAKE_CASE : Union[str, Any] = do_lower_case def UpperCamelCase_ ( self, A, **A ): '''simple docstring''' SCREAMING_SNAKE_CASE : Dict = PaddingStrategy.MAX_LENGTH SCREAMING_SNAKE_CASE : Union[str, Any] = text SCREAMING_SNAKE_CASE : str = kwargs.pop('text_pair', A ) SCREAMING_SNAKE_CASE : Tuple = kwargs.pop('return_tensors', A ) SCREAMING_SNAKE_CASE : int = { 'input_ids': [], 'attention_mask': [], 'token_type_ids': [], } for idx, candidate_text in enumerate(A ): if batch_text_pair is not None: SCREAMING_SNAKE_CASE : Tuple = batch_text_pair[idx] else: SCREAMING_SNAKE_CASE : Any = None SCREAMING_SNAKE_CASE : Any = super().__call__(A, A, return_tensors=A, **A ) SCREAMING_SNAKE_CASE : Dict = encoded_candidates.get('input_ids' ) SCREAMING_SNAKE_CASE : str = encoded_candidates.get('attention_mask' ) SCREAMING_SNAKE_CASE : List[Any] = encoded_candidates.get('token_type_ids' ) if encoded_input_ids is not None: output_data["input_ids"].append(A ) if encoded_attention_mask is not None: output_data["attention_mask"].append(A ) if encoded_token_type_ids is not None: output_data["token_type_ids"].append(A ) SCREAMING_SNAKE_CASE : Dict = {key: item for key, item in output_data.items() if len(A ) != 0} return BatchEncoding(A, tensor_type=A ) def UpperCamelCase_ ( self, A, A=None ): '''simple docstring''' SCREAMING_SNAKE_CASE : Union[str, Any] = [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 UpperCamelCase_ ( self, A, A = None ): '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[int] = [self.sep_token_id] SCREAMING_SNAKE_CASE : str = [self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep ) * [0] return len(cls + token_ids_a + sep ) * [0] + len(token_ids_a + sep ) * [1] def UpperCamelCase_ ( self, A, A = None ): '''simple docstring''' SCREAMING_SNAKE_CASE : Any = self._tokenizer.model.save(A, name=A ) return tuple(A )
251
'''simple docstring''' import logging import os import sys from dataclasses import dataclass, field from typing import Optional import evaluate import numpy as np import torch from datasets import load_dataset from PIL import Image from torchvision.transforms import ( CenterCrop, Compose, Normalize, RandomHorizontalFlip, RandomResizedCrop, Resize, ToTensor, ) import transformers from transformers import ( MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING, AutoConfig, AutoImageProcessor, AutoModelForImageClassification, HfArgumentParser, Trainer, TrainingArguments, set_seed, ) from transformers.trainer_utils import get_last_checkpoint from transformers.utils import check_min_version, send_example_telemetry from transformers.utils.versions import require_version UpperCamelCase_ = logging.getLogger(__name__) # Will error if the minimal version of Transformers is not installed. Remove at your own risks. check_min_version("4.31.0") require_version("datasets>=1.8.0", "To fix: pip install -r examples/pytorch/image-classification/requirements.txt") UpperCamelCase_ = list(MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING.keys()) UpperCamelCase_ = tuple(conf.model_type for conf in MODEL_CONFIG_CLASSES) def lowercase__( __UpperCamelCase: str ): """simple docstring""" with open(__UpperCamelCase ,'rb' ) as f: SCREAMING_SNAKE_CASE : List[str] = Image.open(__UpperCamelCase ) return im.convert('RGB' ) @dataclass class _a : '''simple docstring''' A : Optional[str] = field( default=SCREAMING_SNAKE_CASE , metadata={ '''help''': '''Name of a dataset from the hub (could be your own, possibly private dataset hosted on the hub).''' } , ) A : Optional[str] = field( default=SCREAMING_SNAKE_CASE , metadata={'''help''': '''The configuration name of the dataset to use (via the datasets library).'''} ) A : Optional[str] = field(default=SCREAMING_SNAKE_CASE , metadata={'''help''': '''A folder containing the training data.'''} ) A : Optional[str] = field(default=SCREAMING_SNAKE_CASE , metadata={'''help''': '''A folder containing the validation data.'''} ) A : Optional[float] = field( default=0.15 , metadata={'''help''': '''Percent to split off of train for validation.'''} ) A : Optional[int] = field( default=SCREAMING_SNAKE_CASE , metadata={ '''help''': ( '''For debugging purposes or quicker training, truncate the number of training examples to this ''' '''value if set.''' ) } , ) A : Optional[int] = field( default=SCREAMING_SNAKE_CASE , metadata={ '''help''': ( '''For debugging purposes or quicker training, truncate the number of evaluation examples to this ''' '''value if set.''' ) } , ) def UpperCamelCase_ ( self ): '''simple docstring''' if self.dataset_name is None and (self.train_dir is None and self.validation_dir is None): raise ValueError( 'You must specify either a dataset name from the hub or a train and/or validation directory.' ) @dataclass class _a : '''simple docstring''' A : str = field( default='''google/vit-base-patch16-224-in21k''' , metadata={'''help''': '''Path to pretrained model or model identifier from huggingface.co/models'''} , ) A : Optional[str] = field( default=SCREAMING_SNAKE_CASE , metadata={'''help''': '''If training from scratch, pass a model type from the list: ''' + ''', '''.join(SCREAMING_SNAKE_CASE )} , ) A : Optional[str] = field( default=SCREAMING_SNAKE_CASE , metadata={'''help''': '''Pretrained config name or path if not the same as model_name'''} ) A : Optional[str] = field( default=SCREAMING_SNAKE_CASE , metadata={'''help''': '''Where do you want to store the pretrained models downloaded from s3'''} ) A : str = field( default='''main''' , metadata={'''help''': '''The specific model version to use (can be a branch name, tag name or commit id).'''} , ) A : str = field(default=SCREAMING_SNAKE_CASE , metadata={'''help''': '''Name or path of preprocessor config.'''} ) A : bool = field( default=SCREAMING_SNAKE_CASE , metadata={ '''help''': ( '''Will use the token generated when running `huggingface-cli login` (necessary to use this script ''' '''with private models).''' ) } , ) A : bool = field( default=SCREAMING_SNAKE_CASE , metadata={'''help''': '''Will enable to load a pretrained model whose head dimensions are different.'''} , ) def lowercase__( __UpperCamelCase: Tuple ): """simple docstring""" SCREAMING_SNAKE_CASE : Any = torch.stack([example['pixel_values'] for example in examples] ) SCREAMING_SNAKE_CASE : Optional[Any] = torch.tensor([example['labels'] for example in examples] ) return {"pixel_values": pixel_values, "labels": labels} def lowercase__( ): """simple docstring""" SCREAMING_SNAKE_CASE : Dict = HfArgumentParser((ModelArguments, DataTrainingArguments, TrainingArguments) ) if len(sys.argv ) == 2 and sys.argv[1].endswith('.json' ): # If we pass only one argument to the script and it's the path to a json file, # let's parse it to get our arguments. SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE : int = parser.parse_json_file(json_file=os.path.abspath(sys.argv[1] ) ) else: SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE : Optional[Any] = parser.parse_args_into_dataclasses() # Sending telemetry. Tracking the example usage helps us better allocate resources to maintain them. The # information sent is the one passed as arguments along with your Python/PyTorch versions. send_example_telemetry('run_image_classification' ,__UpperCamelCase ,__UpperCamelCase ) # Setup logging logging.basicConfig( format='%(asctime)s - %(levelname)s - %(name)s - %(message)s' ,datefmt='%m/%d/%Y %H:%M:%S' ,handlers=[logging.StreamHandler(sys.stdout )] ,) if training_args.should_log: # The default of training_args.log_level is passive, so we set log level at info here to have that default. transformers.utils.logging.set_verbosity_info() SCREAMING_SNAKE_CASE : str = training_args.get_process_log_level() logger.setLevel(__UpperCamelCase ) transformers.utils.logging.set_verbosity(__UpperCamelCase ) transformers.utils.logging.enable_default_handler() transformers.utils.logging.enable_explicit_format() # Log on each process the small summary: logger.warning( f"Process rank: {training_args.local_rank}, device: {training_args.device}, n_gpu: {training_args.n_gpu}" + f"distributed training: {bool(training_args.local_rank != -1 )}, 16-bits training: {training_args.fpaa}" ) logger.info(f"Training/evaluation parameters {training_args}" ) # Detecting last checkpoint. SCREAMING_SNAKE_CASE : Optional[int] = None if os.path.isdir(training_args.output_dir ) and training_args.do_train and not training_args.overwrite_output_dir: SCREAMING_SNAKE_CASE : Union[str, Any] = get_last_checkpoint(training_args.output_dir ) if last_checkpoint is None and len(os.listdir(training_args.output_dir ) ) > 0: raise ValueError( f"Output directory ({training_args.output_dir}) already exists and is not empty. " 'Use --overwrite_output_dir to overcome.' ) elif last_checkpoint is not None and training_args.resume_from_checkpoint is None: logger.info( f"Checkpoint detected, resuming training at {last_checkpoint}. To avoid this behavior, change " 'the `--output_dir` or add `--overwrite_output_dir` to train from scratch.' ) # Set seed before initializing model. set_seed(training_args.seed ) # Initialize our dataset and prepare it for the 'image-classification' task. if data_args.dataset_name is not None: SCREAMING_SNAKE_CASE : Any = load_dataset( data_args.dataset_name ,data_args.dataset_config_name ,cache_dir=model_args.cache_dir ,task='image-classification' ,use_auth_token=True if model_args.use_auth_token else None ,) else: SCREAMING_SNAKE_CASE : Union[str, Any] = {} if data_args.train_dir is not None: SCREAMING_SNAKE_CASE : Tuple = os.path.join(data_args.train_dir ,'**' ) if data_args.validation_dir is not None: SCREAMING_SNAKE_CASE : Optional[int] = os.path.join(data_args.validation_dir ,'**' ) SCREAMING_SNAKE_CASE : str = load_dataset( 'imagefolder' ,data_files=__UpperCamelCase ,cache_dir=model_args.cache_dir ,task='image-classification' ,) # If we don't have a validation split, split off a percentage of train as validation. SCREAMING_SNAKE_CASE : Tuple = None if 'validation' in dataset.keys() else data_args.train_val_split if isinstance(data_args.train_val_split ,__UpperCamelCase ) and data_args.train_val_split > 0.0: SCREAMING_SNAKE_CASE : int = dataset['train'].train_test_split(data_args.train_val_split ) SCREAMING_SNAKE_CASE : Optional[int] = split['train'] SCREAMING_SNAKE_CASE : int = split['test'] # Prepare label mappings. # We'll include these in the model's config to get human readable labels in the Inference API. SCREAMING_SNAKE_CASE : int = dataset['train'].features['labels'].names SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE : Tuple = {}, {} for i, label in enumerate(__UpperCamelCase ): SCREAMING_SNAKE_CASE : List[Any] = str(__UpperCamelCase ) SCREAMING_SNAKE_CASE : int = label # Load the accuracy metric from the datasets package SCREAMING_SNAKE_CASE : Any = evaluate.load('accuracy' ) # Define our compute_metrics function. It takes an `EvalPrediction` object (a namedtuple with a # predictions and label_ids field) and has to return a dictionary string to float. def compute_metrics(__UpperCamelCase: Dict ): return metric.compute(predictions=np.argmax(p.predictions ,axis=1 ) ,references=p.label_ids ) SCREAMING_SNAKE_CASE : Optional[int] = AutoConfig.from_pretrained( model_args.config_name or model_args.model_name_or_path ,num_labels=len(__UpperCamelCase ) ,labelaid=__UpperCamelCase ,idalabel=__UpperCamelCase ,finetuning_task='image-classification' ,cache_dir=model_args.cache_dir ,revision=model_args.model_revision ,use_auth_token=True if model_args.use_auth_token else None ,) SCREAMING_SNAKE_CASE : Union[str, Any] = AutoModelForImageClassification.from_pretrained( model_args.model_name_or_path ,from_tf=bool('.ckpt' in model_args.model_name_or_path ) ,config=__UpperCamelCase ,cache_dir=model_args.cache_dir ,revision=model_args.model_revision ,use_auth_token=True if model_args.use_auth_token else None ,ignore_mismatched_sizes=model_args.ignore_mismatched_sizes ,) SCREAMING_SNAKE_CASE : List[str] = AutoImageProcessor.from_pretrained( model_args.image_processor_name or model_args.model_name_or_path ,cache_dir=model_args.cache_dir ,revision=model_args.model_revision ,use_auth_token=True if model_args.use_auth_token else None ,) # Define torchvision transforms to be applied to each image. if "shortest_edge" in image_processor.size: SCREAMING_SNAKE_CASE : Optional[Any] = image_processor.size['shortest_edge'] else: SCREAMING_SNAKE_CASE : List[Any] = (image_processor.size['height'], image_processor.size['width']) SCREAMING_SNAKE_CASE : Dict = Normalize(mean=image_processor.image_mean ,std=image_processor.image_std ) SCREAMING_SNAKE_CASE : Dict = Compose( [ RandomResizedCrop(__UpperCamelCase ), RandomHorizontalFlip(), ToTensor(), normalize, ] ) SCREAMING_SNAKE_CASE : List[Any] = Compose( [ Resize(__UpperCamelCase ), CenterCrop(__UpperCamelCase ), ToTensor(), normalize, ] ) def train_transforms(__UpperCamelCase: List[Any] ): SCREAMING_SNAKE_CASE : Optional[int] = [ _train_transforms(pil_img.convert('RGB' ) ) for pil_img in example_batch['image'] ] return example_batch def val_transforms(__UpperCamelCase: Dict ): SCREAMING_SNAKE_CASE : List[str] = [_val_transforms(pil_img.convert('RGB' ) ) for pil_img in example_batch['image']] return example_batch if training_args.do_train: if "train" not in dataset: raise ValueError('--do_train requires a train dataset' ) if data_args.max_train_samples is not None: SCREAMING_SNAKE_CASE : Tuple = ( dataset['train'].shuffle(seed=training_args.seed ).select(range(data_args.max_train_samples ) ) ) # Set the training transforms dataset["train"].set_transform(__UpperCamelCase ) if training_args.do_eval: if "validation" not in dataset: raise ValueError('--do_eval requires a validation dataset' ) if data_args.max_eval_samples is not None: SCREAMING_SNAKE_CASE : Optional[int] = ( dataset['validation'].shuffle(seed=training_args.seed ).select(range(data_args.max_eval_samples ) ) ) # Set the validation transforms dataset["validation"].set_transform(__UpperCamelCase ) # Initalize our trainer SCREAMING_SNAKE_CASE : List[Any] = Trainer( model=__UpperCamelCase ,args=__UpperCamelCase ,train_dataset=dataset['train'] if training_args.do_train else None ,eval_dataset=dataset['validation'] if training_args.do_eval else None ,compute_metrics=__UpperCamelCase ,tokenizer=__UpperCamelCase ,data_collator=__UpperCamelCase ,) # Training if training_args.do_train: SCREAMING_SNAKE_CASE : Any = None if training_args.resume_from_checkpoint is not None: SCREAMING_SNAKE_CASE : Any = training_args.resume_from_checkpoint elif last_checkpoint is not None: SCREAMING_SNAKE_CASE : Optional[Any] = last_checkpoint SCREAMING_SNAKE_CASE : Union[str, Any] = trainer.train(resume_from_checkpoint=__UpperCamelCase ) trainer.save_model() trainer.log_metrics('train' ,train_result.metrics ) trainer.save_metrics('train' ,train_result.metrics ) trainer.save_state() # Evaluation if training_args.do_eval: SCREAMING_SNAKE_CASE : Union[str, Any] = trainer.evaluate() trainer.log_metrics('eval' ,__UpperCamelCase ) trainer.save_metrics('eval' ,__UpperCamelCase ) # Write model card and (optionally) push to hub SCREAMING_SNAKE_CASE : List[str] = { 'finetuned_from': model_args.model_name_or_path, 'tasks': 'image-classification', 'dataset': data_args.dataset_name, 'tags': ['image-classification', 'vision'], } if training_args.push_to_hub: trainer.push_to_hub(**__UpperCamelCase ) else: trainer.create_model_card(**__UpperCamelCase ) if __name__ == "__main__": main()
251
1
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 snake_case__ (unittest.TestCase ): """simple docstring""" def SCREAMING_SNAKE_CASE__( self ) -> Any: """simple docstring""" a__ : int = tempfile.mkdtemp() a__ : Optional[Any] = BlipImageProcessor() a__ : int = GPTaTokenizer.from_pretrained("""hf-internal-testing/tiny-random-GPT2Model""" ) a__ : Tuple = BlipaProcessor(__lowercase , __lowercase ) processor.save_pretrained(self.tmpdirname ) def SCREAMING_SNAKE_CASE__( self , **__lowercase ) -> List[str]: """simple docstring""" return AutoProcessor.from_pretrained(self.tmpdirname , **__lowercase ).tokenizer def SCREAMING_SNAKE_CASE__( self , **__lowercase ) -> int: """simple docstring""" return AutoProcessor.from_pretrained(self.tmpdirname , **__lowercase ).image_processor def SCREAMING_SNAKE_CASE__( self ) -> List[str]: """simple docstring""" shutil.rmtree(self.tmpdirname ) def SCREAMING_SNAKE_CASE__( self ) -> Dict: """simple docstring""" a__ : List[Any] = [np.random.randint(2_5_5 , size=(3, 3_0, 4_0_0) , dtype=np.uinta )] a__ : Optional[Any] = [Image.fromarray(np.moveaxis(__lowercase , 0 , -1 ) ) for x in image_inputs] return image_inputs def SCREAMING_SNAKE_CASE__( self ) -> Optional[int]: """simple docstring""" a__ : int = BlipaProcessor(tokenizer=self.get_tokenizer() , image_processor=self.get_image_processor() ) processor.save_pretrained(self.tmpdirname ) a__ : int = self.get_tokenizer(bos_token="""(BOS)""" , eos_token="""(EOS)""" ) a__ : int = self.get_image_processor(do_normalize=__lowercase , padding_value=1.0 ) a__ : Optional[int] = BlipaProcessor.from_pretrained( self.tmpdirname , bos_token="""(BOS)""" , eos_token="""(EOS)""" , do_normalize=__lowercase , padding_value=1.0 ) self.assertEqual(processor.tokenizer.get_vocab() , tokenizer_add_kwargs.get_vocab() ) self.assertIsInstance(processor.tokenizer , __lowercase ) self.assertEqual(processor.image_processor.to_json_string() , image_processor_add_kwargs.to_json_string() ) self.assertIsInstance(processor.image_processor , __lowercase ) def SCREAMING_SNAKE_CASE__( self ) -> int: """simple docstring""" a__ : Optional[int] = self.get_image_processor() a__ : str = self.get_tokenizer() a__ : Any = BlipaProcessor(tokenizer=__lowercase , image_processor=__lowercase ) a__ : Tuple = self.prepare_image_inputs() a__ : Union[str, Any] = image_processor(__lowercase , return_tensors="""np""" ) a__ : int = processor(images=__lowercase , 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 SCREAMING_SNAKE_CASE__( self ) -> List[str]: """simple docstring""" a__ : int = self.get_image_processor() a__ : Union[str, Any] = self.get_tokenizer() a__ : List[str] = BlipaProcessor(tokenizer=__lowercase , image_processor=__lowercase ) a__ : str = """lower newer""" a__ : Any = processor(text=__lowercase ) a__ : Any = tokenizer(__lowercase , return_token_type_ids=__lowercase ) for key in encoded_tok.keys(): self.assertListEqual(encoded_tok[key] , encoded_processor[key] ) def SCREAMING_SNAKE_CASE__( self ) -> List[Any]: """simple docstring""" a__ : str = self.get_image_processor() a__ : Any = self.get_tokenizer() a__ : List[str] = BlipaProcessor(tokenizer=__lowercase , image_processor=__lowercase ) a__ : Union[str, Any] = """lower newer""" a__ : Optional[int] = self.prepare_image_inputs() a__ : Optional[int] = processor(text=__lowercase , images=__lowercase ) self.assertListEqual(list(inputs.keys() ) , ["""pixel_values""", """input_ids""", """attention_mask"""] ) # test if it raises when no input is passed with pytest.raises(__lowercase ): processor() def SCREAMING_SNAKE_CASE__( self ) -> Any: """simple docstring""" a__ : Optional[int] = self.get_image_processor() a__ : str = self.get_tokenizer() a__ : Optional[int] = BlipaProcessor(tokenizer=__lowercase , image_processor=__lowercase ) a__ : Optional[Any] = [[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]] a__ : Any = processor.batch_decode(__lowercase ) a__ : Union[str, Any] = tokenizer.batch_decode(__lowercase ) self.assertListEqual(__lowercase , __lowercase ) def SCREAMING_SNAKE_CASE__( self ) -> Tuple: """simple docstring""" a__ : Any = self.get_image_processor() a__ : Dict = self.get_tokenizer() a__ : Optional[Any] = BlipaProcessor(tokenizer=__lowercase , image_processor=__lowercase ) a__ : Any = """lower newer""" a__ : Any = self.prepare_image_inputs() a__ : Any = processor(text=__lowercase , images=__lowercase ) # For now the processor supports only ['pixel_values', 'input_ids', 'attention_mask'] self.assertListEqual(list(inputs.keys() ) , ["""pixel_values""", """input_ids""", """attention_mask"""] )
266
import argparse import json import os import evaluate import torch from datasets import load_dataset from torch.optim import AdamW from torch.utils.data import DataLoader from transformers import AutoModelForSequenceClassification, AutoTokenizer, get_linear_schedule_with_warmup, set_seed from accelerate import Accelerator, DistributedType from accelerate.utils.deepspeed import DummyOptim, DummyScheduler _lowercase : Union[str, Any] =16 _lowercase : Dict =32 def lowerCAmelCase_ ( _lowercase : Accelerator , _lowercase : int = 16 , _lowercase : str = "bert-base-cased") -> Union[str, Any]: """simple docstring""" a__ : Union[str, Any] = AutoTokenizer.from_pretrained(_lowercase) a__ : Any = load_dataset("""glue""" , """mrpc""") def tokenize_function(_lowercase : str): # max_length=None => use the model max length (it's actually the default) a__ : List[str] = tokenizer(examples["""sentence1"""] , examples["""sentence2"""] , truncation=_lowercase , max_length=_lowercase) return outputs # Apply the method we just defined to all the examples in all the splits of the dataset a__ : Any = datasets.map( _lowercase , batched=_lowercase , remove_columns=["""idx""", """sentence1""", """sentence2"""] , load_from_cache_file=_lowercase) # We also rename the 'label' column to 'labels' which is the expected name for labels by the models of the # transformers library a__ : Tuple = tokenized_datasets.rename_column("""label""" , """labels""") def collate_fn(_lowercase : Optional[Any]): # On TPU it's best to pad everything to the same length or training will be very slow. if accelerator.distributed_type == DistributedType.TPU: return tokenizer.pad(_lowercase , padding="""max_length""" , max_length=128 , return_tensors="""pt""") return tokenizer.pad(_lowercase , padding="""longest""" , return_tensors="""pt""") # Instantiate dataloaders. a__ : int = DataLoader( tokenized_datasets["""train"""] , shuffle=_lowercase , collate_fn=_lowercase , batch_size=_lowercase) a__ : Union[str, Any] = DataLoader( tokenized_datasets["""validation"""] , shuffle=_lowercase , collate_fn=_lowercase , batch_size=_lowercase) return train_dataloader, eval_dataloader def lowerCAmelCase_ ( _lowercase : Any , _lowercase : Dict) -> int: """simple docstring""" # Initialize accelerator a__ : Optional[int] = Accelerator() # Sample hyper-parameters for learning rate, batch size, seed and a few other HPs a__ : Union[str, Any] = config["""lr"""] a__ : List[str] = int(config["""num_epochs"""]) a__ : List[str] = int(config["""seed"""]) a__ : Tuple = int(config["""batch_size"""]) a__ : int = args.model_name_or_path set_seed(_lowercase) a__ , a__ : int = get_dataloaders(_lowercase , _lowercase , _lowercase) # Instantiate the model (we build the model here so that the seed also control new weights initialization) a__ : Optional[Any] = AutoModelForSequenceClassification.from_pretrained(_lowercase , return_dict=_lowercase) # Instantiate optimizer a__ : int = ( AdamW if accelerator.state.deepspeed_plugin is None or """optimizer""" not in accelerator.state.deepspeed_plugin.deepspeed_config else DummyOptim ) a__ : Optional[Any] = optimizer_cls(params=model.parameters() , lr=_lowercase) if accelerator.state.deepspeed_plugin is not None: a__ : Dict = accelerator.state.deepspeed_plugin.deepspeed_config[ """gradient_accumulation_steps""" ] else: a__ : List[str] = 1 a__ : List[Any] = (len(_lowercase) * num_epochs) // gradient_accumulation_steps # Instantiate scheduler if ( accelerator.state.deepspeed_plugin is None or "scheduler" not in accelerator.state.deepspeed_plugin.deepspeed_config ): a__ : Dict = get_linear_schedule_with_warmup( optimizer=_lowercase , num_warmup_steps=0 , num_training_steps=_lowercase , ) else: a__ : Dict = DummyScheduler(_lowercase , total_num_steps=_lowercase , warmup_num_steps=0) # Prepare everything # There is no specific order to remember, we just need to unpack the objects in the same order we gave them to the # prepare method. a__ , a__ , a__ , a__ , a__ : List[Any] = accelerator.prepare( _lowercase , _lowercase , _lowercase , _lowercase , _lowercase) # We need to keep track of how many total steps we have iterated over a__ : Optional[int] = 0 # We also need to keep track of the stating epoch so files are named properly a__ : Optional[int] = 0 # Now we train the model a__ : Tuple = evaluate.load("""glue""" , """mrpc""") a__ : List[Any] = 0 a__ : Tuple = {} for epoch in range(_lowercase , _lowercase): model.train() for step, batch in enumerate(_lowercase): a__ : Union[str, Any] = model(**_lowercase) a__ : Tuple = outputs.loss a__ : Any = loss / gradient_accumulation_steps accelerator.backward(_lowercase) if step % gradient_accumulation_steps == 0: optimizer.step() lr_scheduler.step() optimizer.zero_grad() overall_step += 1 model.eval() a__ : int = 0 for step, batch in enumerate(_lowercase): # We could avoid this line since we set the accelerator with `device_placement=True`. batch.to(accelerator.device) with torch.no_grad(): a__ : str = model(**_lowercase) a__ : Union[str, Any] = outputs.logits.argmax(dim=-1) # It is slightly faster to call this once, than multiple times a__ , a__ : Optional[int] = accelerator.gather( (predictions, batch["""labels"""])) # If we are in a multiprocess environment, the last batch has duplicates if accelerator.use_distributed: if step == len(_lowercase) - 1: a__ : Union[str, Any] = predictions[: len(eval_dataloader.dataset) - samples_seen] a__ : Any = references[: len(eval_dataloader.dataset) - samples_seen] else: samples_seen += references.shape[0] metric.add_batch( predictions=_lowercase , references=_lowercase , ) a__ : Optional[Any] = metric.compute() # Use accelerator.print to print only on the main process. accelerator.print(F'''epoch {epoch}:''' , _lowercase) a__ : Any = eval_metric["""accuracy"""] if best_performance < eval_metric["accuracy"]: a__ : List[str] = eval_metric["""accuracy"""] if args.performance_lower_bound is not None: assert ( args.performance_lower_bound <= best_performance ), F'''Best performance metric {best_performance} is lower than the lower bound {args.performance_lower_bound}''' accelerator.wait_for_everyone() if accelerator.is_main_process: with open(os.path.join(args.output_dir , """all_results.json""") , """w""") as f: json.dump(_lowercase , _lowercase) def lowerCAmelCase_ ( ) -> Tuple: """simple docstring""" a__ : Optional[Any] = argparse.ArgumentParser(description="""Simple example of training script tracking peak GPU memory usage.""") parser.add_argument( """--model_name_or_path""" , type=_lowercase , default="""bert-base-cased""" , help="""Path to pretrained model or model identifier from huggingface.co/models.""" , required=_lowercase , ) parser.add_argument( """--output_dir""" , type=_lowercase , default=""".""" , help="""Optional save directory where all checkpoint folders will be stored. Default is the current working directory.""" , ) parser.add_argument( """--performance_lower_bound""" , type=_lowercase , default=_lowercase , help="""Optional lower bound for the performance metric. If set, the training will throw error when the performance metric drops below this value.""" , ) parser.add_argument( """--num_epochs""" , type=_lowercase , default=3 , help="""Number of train epochs.""" , ) a__ : Any = parser.parse_args() a__ : Dict = {"""lr""": 2e-5, """num_epochs""": args.num_epochs, """seed""": 42, """batch_size""": 16} training_function(_lowercase , _lowercase) if __name__ == "__main__": main()
266
1
"""simple docstring""" import copy from dataclasses import dataclass, field from typing import ClassVar, Dict from ..features import ClassLabel, Features, Value from .base import TaskTemplate @dataclass(frozen=A__ ) class _SCREAMING_SNAKE_CASE ( A__ ): # `task` is not a ClassVar since we want it to be part of the `asdict` output for JSON serialization UpperCAmelCase_ :str = field(default="text-classification" , metadata={"include_in_asdict_even_if_is_default": True} ) UpperCAmelCase_ :ClassVar[Features] = Features({"text": Value("string" )} ) UpperCAmelCase_ :ClassVar[Features] = Features({"labels": ClassLabel} ) UpperCAmelCase_ :str = "text" UpperCAmelCase_ :str = "labels" def __lowerCAmelCase ( self , __A ) -> List[Any]: if self.label_column not in features: raise ValueError(f"""Column {self.label_column} is not present in features.""" ) if not isinstance(features[self.label_column] , _snake_case ): raise ValueError(f"""Column {self.label_column} is not a ClassLabel.""" ) lowerCAmelCase_ :List[str] = copy.deepcopy(self ) lowerCAmelCase_ :Any = self.label_schema.copy() lowerCAmelCase_ :Any = features[self.label_column] lowerCAmelCase_ :Optional[Any] = label_schema return task_template @property def __lowerCAmelCase ( self ) -> Union[str, Any]: return { self.text_column: "text", self.label_column: "labels", }
84
import json import os import re import shutil import tempfile import unittest from typing import Tuple from transformers import AddedToken, BatchEncoding, ByTaTokenizer from transformers.utils import cached_property, is_tf_available, is_torch_available from ...test_tokenization_common import TokenizerTesterMixin if is_torch_available(): snake_case_ : Optional[Any] = "pt" elif is_tf_available(): snake_case_ : Union[str, Any] = "tf" else: snake_case_ : str = "jax" class __snake_case ( a , unittest.TestCase ): UpperCAmelCase__ : List[Any] = ByTaTokenizer UpperCAmelCase__ : int = False def lowerCamelCase ( self : Optional[int]): """simple docstring""" super().setUp() UpperCAmelCase_ = ByTaTokenizer() tokenizer.save_pretrained(self.tmpdirname) @cached_property def lowerCamelCase ( self : Tuple): """simple docstring""" return ByTaTokenizer.from_pretrained('''google/byt5-small''') def lowerCamelCase ( self : List[str] , **_snake_case : Union[str, Any]): """simple docstring""" return self.tokenizer_class.from_pretrained(self.tmpdirname , **_snake_case) def lowerCamelCase ( self : Dict , _snake_case : int , _snake_case : Tuple=False , _snake_case : Dict=20 , _snake_case : Optional[Any]=5): """simple docstring""" UpperCAmelCase_ = [] for i in range(len(_snake_case)): try: UpperCAmelCase_ = tokenizer.decode([i] , clean_up_tokenization_spaces=_snake_case) except UnicodeDecodeError: pass toks.append((i, tok)) UpperCAmelCase_ = list(filter(lambda _snake_case: re.match(r'''^[ a-zA-Z]+$''' , t[1]) , _snake_case)) UpperCAmelCase_ = list(filter(lambda _snake_case: [t[0]] == tokenizer.encode(t[1] , add_special_tokens=_snake_case) , _snake_case)) if max_length is not None and len(_snake_case) > max_length: UpperCAmelCase_ = toks[:max_length] if min_length is not None and len(_snake_case) < min_length and len(_snake_case) > 0: while len(_snake_case) < min_length: UpperCAmelCase_ = toks + toks # toks_str = [t[1] for t in toks] UpperCAmelCase_ = [t[0] for t in toks] # Ensure consistency UpperCAmelCase_ = tokenizer.decode(_snake_case , clean_up_tokenization_spaces=_snake_case) if " " not in output_txt and len(_snake_case) > 1: UpperCAmelCase_ = ( tokenizer.decode([toks_ids[0]] , clean_up_tokenization_spaces=_snake_case) + ''' ''' + tokenizer.decode(toks_ids[1:] , clean_up_tokenization_spaces=_snake_case) ) if with_prefix_space: UpperCAmelCase_ = ''' ''' + output_txt UpperCAmelCase_ = tokenizer.encode(_snake_case , add_special_tokens=_snake_case) return output_txt, output_ids def lowerCamelCase ( self : Union[str, Any]): """simple docstring""" UpperCAmelCase_ = self.ta_base_tokenizer UpperCAmelCase_ = tokenizer(['''hi</s>''', '''I went to the gym</s>''', '''</s>''']) UpperCAmelCase_ = tokenizer(['''hi''', '''I went to the gym''', '''''']) self.assertListEqual(batch_with_eos_added['''input_ids'''] , batch_without_eos_added['''input_ids''']) def lowerCamelCase ( self : str): """simple docstring""" UpperCAmelCase_ = self.ta_base_tokenizer UpperCAmelCase_ = '''Unicode €.''' UpperCAmelCase_ = tokenizer(_snake_case) UpperCAmelCase_ = [88, 113, 108, 102, 114, 103, 104, 35, 229, 133, 175, 49, 1] self.assertEqual(encoded['''input_ids'''] , _snake_case) # decoding UpperCAmelCase_ = tokenizer.decode(_snake_case) self.assertEqual(_snake_case , '''Unicode €.</s>''') UpperCAmelCase_ = tokenizer('''e è é ê ë''') UpperCAmelCase_ = [104, 35, 198, 171, 35, 198, 172, 35, 198, 173, 35, 198, 174, 1] self.assertEqual(encoded['''input_ids'''] , _snake_case) # decoding UpperCAmelCase_ = tokenizer.decode(_snake_case) self.assertEqual(_snake_case , '''e è é ê ë</s>''') # encode/decode, but with `encode` instead of `__call__` self.assertEqual(tokenizer.decode(tokenizer.encode('''e è é ê ë''')) , '''e è é ê ë</s>''') def lowerCamelCase ( self : Any): """simple docstring""" UpperCAmelCase_ = self.ta_base_tokenizer UpperCAmelCase_ = ['''A long paragraph for summarization.''', '''Another paragraph for summarization.'''] # fmt: off UpperCAmelCase_ = [68, 35, 111, 114, 113, 106, 35, 115, 100, 117, 100, 106, 117, 100, 115, 107, 35, 105, 114, 117, 35, 118, 120, 112, 112, 100, 117, 108, 125, 100, 119, 108, 114, 113, 49, 1, 0] # fmt: on UpperCAmelCase_ = tokenizer(_snake_case , padding=_snake_case , return_tensors=_snake_case) self.assertIsInstance(_snake_case , _snake_case) if FRAMEWORK != "jax": UpperCAmelCase_ = list(batch.input_ids.numpy()[0]) else: UpperCAmelCase_ = list(batch.input_ids.tolist()[0]) self.assertListEqual(_snake_case , _snake_case) self.assertEqual((2, 37) , batch.input_ids.shape) self.assertEqual((2, 37) , batch.attention_mask.shape) def lowerCamelCase ( self : Optional[Any]): """simple docstring""" UpperCAmelCase_ = self.ta_base_tokenizer UpperCAmelCase_ = ['''A long paragraph for summarization.''', '''Another paragraph for summarization.'''] UpperCAmelCase_ = tokenizer(_snake_case , padding=_snake_case , return_tensors=_snake_case) # check if input_ids are returned and no decoder_input_ids self.assertIn('''input_ids''' , _snake_case) self.assertIn('''attention_mask''' , _snake_case) self.assertNotIn('''decoder_input_ids''' , _snake_case) self.assertNotIn('''decoder_attention_mask''' , _snake_case) def lowerCamelCase ( self : Tuple): """simple docstring""" UpperCAmelCase_ = self.ta_base_tokenizer UpperCAmelCase_ = [ '''Summary of the text.''', '''Another summary.''', ] UpperCAmelCase_ = tokenizer( text_target=_snake_case , max_length=32 , padding='''max_length''' , truncation=_snake_case , return_tensors=_snake_case) self.assertEqual(32 , targets['''input_ids'''].shape[1]) def lowerCamelCase ( self : int): """simple docstring""" UpperCAmelCase_ = self.ta_base_tokenizer UpperCAmelCase_ = ['''A long paragraph for summarization. </s>'''] UpperCAmelCase_ = ['''Summary of the text. </s>'''] # fmt: off UpperCAmelCase_ = [68, 35, 111, 114, 113, 106, 35, 115, 100, 117, 100, 106, 117, 100, 115, 107, 35, 105, 114, 117, 35, 118, 120, 112, 112, 100, 117, 108, 125, 100, 119, 108, 114, 113, 49, 35, 1] UpperCAmelCase_ = [86, 120, 112, 112, 100, 117, 124, 35, 114, 105, 35, 119, 107, 104, 35, 119, 104, 123, 119, 49, 35, 1] # fmt: on UpperCAmelCase_ = tokenizer(_snake_case , text_target=_snake_case) self.assertEqual(_snake_case , batch['''input_ids'''][0]) self.assertEqual(_snake_case , batch['''labels'''][0]) def lowerCamelCase ( self : Tuple): """simple docstring""" UpperCAmelCase_ = self.get_tokenizers() for tokenizer in tokenizers: with self.subTest(F"""{tokenizer.__class__.__name__}"""): self.assertNotEqual(tokenizer.model_max_length , 42) # Now let's start the test UpperCAmelCase_ = self.get_tokenizers() for tokenizer in tokenizers: with self.subTest(F"""{tokenizer.__class__.__name__}"""): # Isolate this from the other tests because we save additional tokens/etc UpperCAmelCase_ = tempfile.mkdtemp() UpperCAmelCase_ = ''' He is very happy, UNwant\u00E9d,running''' UpperCAmelCase_ = tokenizer.encode(_snake_case , add_special_tokens=_snake_case) tokenizer.save_pretrained(_snake_case) UpperCAmelCase_ = tokenizer.__class__.from_pretrained(_snake_case) UpperCAmelCase_ = after_tokenizer.encode(_snake_case , add_special_tokens=_snake_case) self.assertListEqual(_snake_case , _snake_case) shutil.rmtree(_snake_case) UpperCAmelCase_ = self.get_tokenizers(model_max_length=42) for tokenizer in tokenizers: with self.subTest(F"""{tokenizer.__class__.__name__}"""): # Isolate this from the other tests because we save additional tokens/etc UpperCAmelCase_ = tempfile.mkdtemp() UpperCAmelCase_ = ''' He is very happy, UNwant\u00E9d,running''' tokenizer.add_tokens(['''bim''', '''bambam''']) UpperCAmelCase_ = tokenizer.additional_special_tokens additional_special_tokens.append('''new_additional_special_token''') tokenizer.add_special_tokens({'''additional_special_tokens''': additional_special_tokens}) UpperCAmelCase_ = tokenizer.encode(_snake_case , add_special_tokens=_snake_case) tokenizer.save_pretrained(_snake_case) UpperCAmelCase_ = tokenizer.__class__.from_pretrained(_snake_case) UpperCAmelCase_ = after_tokenizer.encode(_snake_case , add_special_tokens=_snake_case) self.assertListEqual(_snake_case , _snake_case) self.assertIn('''new_additional_special_token''' , after_tokenizer.additional_special_tokens) self.assertEqual(after_tokenizer.model_max_length , 42) UpperCAmelCase_ = tokenizer.__class__.from_pretrained(_snake_case , model_max_length=43) self.assertEqual(tokenizer.model_max_length , 43) shutil.rmtree(_snake_case) def lowerCamelCase ( self : List[Any]): """simple docstring""" UpperCAmelCase_ = [] if self.test_slow_tokenizer: tokenizer_list.append((self.tokenizer_class, self.get_tokenizer())) if self.test_rust_tokenizer: tokenizer_list.append((self.rust_tokenizer_class, self.get_rust_tokenizer())) for tokenizer_class, tokenizer_utils in tokenizer_list: with tempfile.TemporaryDirectory() as tmp_dir: tokenizer_utils.save_pretrained(_snake_case) with open(os.path.join(_snake_case , '''special_tokens_map.json''') , encoding='''utf-8''') as json_file: UpperCAmelCase_ = json.load(_snake_case) with open(os.path.join(_snake_case , '''tokenizer_config.json''') , encoding='''utf-8''') as json_file: UpperCAmelCase_ = json.load(_snake_case) UpperCAmelCase_ = [F"""<extra_id_{i}>""" for i in range(125)] UpperCAmelCase_ = added_tokens_extra_ids + [ '''an_additional_special_token''' ] UpperCAmelCase_ = added_tokens_extra_ids + [ '''an_additional_special_token''' ] with open(os.path.join(_snake_case , '''special_tokens_map.json''') , '''w''' , encoding='''utf-8''') as outfile: json.dump(_snake_case , _snake_case) with open(os.path.join(_snake_case , '''tokenizer_config.json''') , '''w''' , encoding='''utf-8''') as outfile: json.dump(_snake_case , _snake_case) # the following checks allow us to verify that our test works as expected, i.e. that the tokenizer takes # into account the new value of additional_special_tokens given in the "tokenizer_config.json" and # "special_tokens_map.json" files UpperCAmelCase_ = tokenizer_class.from_pretrained( _snake_case , ) self.assertIn( '''an_additional_special_token''' , tokenizer_without_change_in_init.additional_special_tokens) # self.assertIn("an_additional_special_token",tokenizer_without_change_in_init.get_vocab()) # ByT5Tokenization no vocab self.assertEqual( ['''an_additional_special_token'''] , tokenizer_without_change_in_init.convert_ids_to_tokens( tokenizer_without_change_in_init.convert_tokens_to_ids(['''an_additional_special_token'''])) , ) # Now we test that we can change the value of additional_special_tokens in the from_pretrained UpperCAmelCase_ = added_tokens_extra_ids + [AddedToken('''a_new_additional_special_token''' , lstrip=_snake_case)] UpperCAmelCase_ = tokenizer_class.from_pretrained( _snake_case , additional_special_tokens=_snake_case , ) self.assertIn('''a_new_additional_special_token''' , tokenizer.additional_special_tokens) self.assertEqual( ['''a_new_additional_special_token'''] , tokenizer.convert_ids_to_tokens( tokenizer.convert_tokens_to_ids(['''a_new_additional_special_token'''])) , ) def lowerCamelCase ( self : Any): """simple docstring""" UpperCAmelCase_ = [] if self.test_slow_tokenizer: tokenizer_list.append((self.tokenizer_class, self.get_tokenizer())) if self.test_rust_tokenizer: tokenizer_list.append((self.rust_tokenizer_class, self.get_rust_tokenizer())) for tokenizer_class, tokenizer_utils in tokenizer_list: with tempfile.TemporaryDirectory() as tmp_dir: tokenizer_utils.save_pretrained(_snake_case) UpperCAmelCase_ = tokenizer_class.from_pretrained(_snake_case) self.assertTrue(tokenizer.decode([255]) == '''''') def lowerCamelCase ( self : int): """simple docstring""" pass def lowerCamelCase ( self : Optional[int]): """simple docstring""" pass def lowerCamelCase ( self : Dict): """simple docstring""" pass def lowerCamelCase ( self : List[Any]): """simple docstring""" pass def lowerCamelCase ( self : Tuple): """simple docstring""" UpperCAmelCase_ = self.get_tokenizers(fast=_snake_case , do_lower_case=_snake_case) for tokenizer in tokenizers: with self.subTest(F"""{tokenizer.__class__.__name__}"""): UpperCAmelCase_ = ['''t''', '''h''', '''i''', '''s''', ''' ''', '''i''', '''s''', ''' ''', '''a''', ''' ''', '''t''', '''e''', '''x''', '''t''', '''</s>'''] UpperCAmelCase_ = tokenizer.convert_tokens_to_string(_snake_case) self.assertIsInstance(_snake_case , _snake_case) def lowerCamelCase ( self : Union[str, Any]): """simple docstring""" UpperCAmelCase_ = self.get_tokenizers() for tokenizer in tokenizers: with self.subTest(F"""{tokenizer.__class__.__name__}"""): UpperCAmelCase_ = [ '''bos_token''', '''eos_token''', '''unk_token''', '''sep_token''', '''pad_token''', '''cls_token''', '''mask_token''', ] UpperCAmelCase_ = 0 UpperCAmelCase_ = tokenizer.convert_ids_to_tokens( _snake_case , skip_special_tokens=_snake_case) for attr in attributes_list: setattr(_snake_case , attr + '''_id''' , _snake_case) self.assertEqual(getattr(_snake_case , _snake_case) , _snake_case) self.assertEqual(getattr(_snake_case , attr + '''_id''') , _snake_case) setattr(_snake_case , attr + '''_id''' , _snake_case) self.assertEqual(getattr(_snake_case , _snake_case) , _snake_case) self.assertEqual(getattr(_snake_case , attr + '''_id''') , _snake_case) setattr(_snake_case , '''additional_special_tokens_ids''' , []) self.assertListEqual(getattr(_snake_case , '''additional_special_tokens''') , []) self.assertListEqual(getattr(_snake_case , '''additional_special_tokens_ids''') , []) setattr(_snake_case , '''additional_special_tokens_ids''' , [token_id_to_test_setters]) self.assertListEqual(getattr(_snake_case , '''additional_special_tokens''') , [token_to_test_setters]) self.assertListEqual(getattr(_snake_case , '''additional_special_tokens_ids''') , [token_id_to_test_setters])
51
0
"""simple docstring""" import json import os import pickle import shutil import tempfile from unittest import TestCase from unittest.mock import patch import numpy as np from datasets import Dataset from transformers import is_faiss_available from transformers.models.bart.configuration_bart import BartConfig from transformers.models.bart.tokenization_bart import BartTokenizer from transformers.models.bert.tokenization_bert import VOCAB_FILES_NAMES as DPR_VOCAB_FILES_NAMES from transformers.models.dpr.configuration_dpr import DPRConfig from transformers.models.dpr.tokenization_dpr import DPRContextEncoderTokenizer, DPRQuestionEncoderTokenizer from transformers.models.rag.configuration_rag import RagConfig from transformers.models.rag.retrieval_rag import CustomHFIndex, RagRetriever from transformers.models.roberta.tokenization_roberta import VOCAB_FILES_NAMES as BART_VOCAB_FILES_NAMES from transformers.testing_utils import require_faiss, require_sentencepiece, require_tokenizers, require_torch if is_faiss_available(): import faiss @require_faiss class __SCREAMING_SNAKE_CASE ( UpperCamelCase ): def _UpperCamelCase ( self : Union[str, Any] ): '''simple docstring''' A__ : Tuple = tempfile.mkdtemp() A__ : List[Any] = 8 # DPR tok A__ : str = [ """[UNK]""", """[CLS]""", """[SEP]""", """[PAD]""", """[MASK]""", """want""", """##want""", """##ed""", """wa""", """un""", """runn""", """##ing""", """,""", """low""", """lowest""", ] A__ : Union[str, Any] = os.path.join(self.tmpdirname , """dpr_tokenizer""" ) os.makedirs(snake_case , exist_ok=snake_case ) A__ : str = os.path.join(snake_case , DPR_VOCAB_FILES_NAMES["""vocab_file"""] ) with open(self.vocab_file , """w""" , encoding="""utf-8""" ) as vocab_writer: vocab_writer.write("""""".join([x + """\n""" for x in vocab_tokens] ) ) # BART tok A__ : Optional[int] = [ """l""", """o""", """w""", """e""", """r""", """s""", """t""", """i""", """d""", """n""", """\u0120""", """\u0120l""", """\u0120n""", """\u0120lo""", """\u0120low""", """er""", """\u0120lowest""", """\u0120newer""", """\u0120wider""", """<unk>""", ] A__ : Tuple = dict(zip(snake_case , range(len(snake_case ) ) ) ) A__ : Optional[Any] = ["""#version: 0.2""", """\u0120 l""", """\u0120l o""", """\u0120lo w""", """e r""", """"""] A__ : Any = {"""unk_token""": """<unk>"""} A__ : List[Any] = os.path.join(self.tmpdirname , """bart_tokenizer""" ) os.makedirs(snake_case , exist_ok=snake_case ) A__ : List[Any] = os.path.join(snake_case , BART_VOCAB_FILES_NAMES["""vocab_file"""] ) A__ : List[Any] = os.path.join(snake_case , BART_VOCAB_FILES_NAMES["""merges_file"""] ) with open(self.vocab_file , """w""" , encoding="""utf-8""" ) as fp: fp.write(json.dumps(snake_case ) + """\n""" ) with open(self.merges_file , """w""" , encoding="""utf-8""" ) as fp: fp.write("""\n""".join(snake_case ) ) def _UpperCamelCase ( self : Dict ): '''simple docstring''' return DPRQuestionEncoderTokenizer.from_pretrained(os.path.join(self.tmpdirname , """dpr_tokenizer""" ) ) def _UpperCamelCase ( self : List[str] ): '''simple docstring''' return DPRContextEncoderTokenizer.from_pretrained(os.path.join(self.tmpdirname , """dpr_tokenizer""" ) ) def _UpperCamelCase ( self : int ): '''simple docstring''' return BartTokenizer.from_pretrained(os.path.join(self.tmpdirname , """bart_tokenizer""" ) ) def _UpperCamelCase ( self : int ): '''simple docstring''' shutil.rmtree(self.tmpdirname ) def _UpperCamelCase ( self : Dict ): '''simple docstring''' A__ : List[str] = Dataset.from_dict( { """id""": ["""0""", """1"""], """text""": ["""foo""", """bar"""], """title""": ["""Foo""", """Bar"""], """embeddings""": [np.ones(self.retrieval_vector_size ), 2 * np.ones(self.retrieval_vector_size )], } ) dataset.add_faiss_index("""embeddings""" , string_factory="""Flat""" , metric_type=faiss.METRIC_INNER_PRODUCT ) return dataset def _UpperCamelCase ( self : Dict ): '''simple docstring''' A__ : Any = self.get_dummy_dataset() A__ : int = RagConfig( retrieval_vector_size=self.retrieval_vector_size , question_encoder=DPRConfig().to_dict() , generator=BartConfig().to_dict() , ) with patch("""transformers.models.rag.retrieval_rag.load_dataset""" ) as mock_load_dataset: A__ : Any = dataset A__ : Any = RagRetriever( snake_case , question_encoder_tokenizer=self.get_dpr_tokenizer() , generator_tokenizer=self.get_bart_tokenizer() , ) return retriever def _UpperCamelCase ( self : List[str] , snake_case : bool ): '''simple docstring''' A__ : Dict = self.get_dummy_dataset() A__ : Union[str, Any] = RagConfig( retrieval_vector_size=self.retrieval_vector_size , question_encoder=DPRConfig().to_dict() , generator=BartConfig().to_dict() , index_name="""custom""" , ) if from_disk: A__ : Any = os.path.join(self.tmpdirname , """dataset""" ) A__ : Tuple = os.path.join(self.tmpdirname , """index.faiss""" ) dataset.get_index("""embeddings""" ).save(os.path.join(self.tmpdirname , """index.faiss""" ) ) dataset.drop_index("""embeddings""" ) dataset.save_to_disk(os.path.join(self.tmpdirname , """dataset""" ) ) del dataset A__ : Tuple = RagRetriever( snake_case , question_encoder_tokenizer=self.get_dpr_tokenizer() , generator_tokenizer=self.get_bart_tokenizer() , ) else: A__ : int = RagRetriever( snake_case , question_encoder_tokenizer=self.get_dpr_tokenizer() , generator_tokenizer=self.get_bart_tokenizer() , index=CustomHFIndex(config.retrieval_vector_size , snake_case ) , ) return retriever def _UpperCamelCase ( self : str ): '''simple docstring''' A__ : Optional[Any] = Dataset.from_dict( { """id""": ["""0""", """1"""], """text""": ["""foo""", """bar"""], """title""": ["""Foo""", """Bar"""], """embeddings""": [np.ones(self.retrieval_vector_size + 1 ), 2 * np.ones(self.retrieval_vector_size + 1 )], } ) dataset.add_faiss_index("""embeddings""" , string_factory="""Flat""" , metric_type=faiss.METRIC_INNER_PRODUCT ) A__ : List[str] = os.path.join(self.tmpdirname , """hf_bert_base.hnswSQ8_correct_phi_128.c_index""" ) dataset.save_faiss_index("""embeddings""" , index_file_name + """.index.dpr""" ) pickle.dump(dataset["""id"""] , open(index_file_name + """.index_meta.dpr""" , """wb""" ) ) A__ : int = os.path.join(self.tmpdirname , """psgs_w100.tsv.pkl""" ) A__ : Dict = {sample["""id"""]: [sample["""text"""], sample["""title"""]] for sample in dataset} pickle.dump(snake_case , open(snake_case , """wb""" ) ) A__ : Optional[int] = RagConfig( retrieval_vector_size=self.retrieval_vector_size , question_encoder=DPRConfig().to_dict() , generator=BartConfig().to_dict() , index_name="""legacy""" , index_path=self.tmpdirname , ) A__ : List[Any] = RagRetriever( snake_case , question_encoder_tokenizer=self.get_dpr_tokenizer() , generator_tokenizer=self.get_bart_tokenizer() ) return retriever def _UpperCamelCase ( self : Union[str, Any] ): '''simple docstring''' A__ : Dict = 1 A__ : str = self.get_dummy_canonical_hf_index_retriever() A__ : Tuple = np.array( [np.ones(self.retrieval_vector_size ), -np.ones(self.retrieval_vector_size )] , dtype=np.floataa ) A__ , A__ , A__ : Optional[Any] = retriever.retrieve(snake_case , n_docs=snake_case ) self.assertEqual(retrieved_doc_embeds.shape , (2, n_docs, self.retrieval_vector_size) ) self.assertEqual(len(snake_case ) , 2 ) self.assertEqual(sorted(doc_dicts[0] ) , ["""embeddings""", """id""", """text""", """title"""] ) self.assertEqual(len(doc_dicts[0]["""id"""] ) , snake_case ) self.assertEqual(doc_dicts[0]["""id"""][0] , """1""" ) # max inner product is reached with second doc self.assertEqual(doc_dicts[1]["""id"""][0] , """0""" ) # max inner product is reached with first doc self.assertListEqual(doc_ids.tolist() , [[1], [0]] ) def _UpperCamelCase ( self : Optional[Any] ): '''simple docstring''' A__ : List[str] = self.get_dummy_canonical_hf_index_retriever() with tempfile.TemporaryDirectory() as tmp_dirname: with patch("""transformers.models.rag.retrieval_rag.load_dataset""" ) as mock_load_dataset: A__ : Dict = self.get_dummy_dataset() retriever.save_pretrained(snake_case ) A__ : List[str] = RagRetriever.from_pretrained(snake_case ) self.assertIsInstance(snake_case , snake_case ) A__ : Optional[int] = np.array( [np.ones(self.retrieval_vector_size ), -np.ones(self.retrieval_vector_size )] , dtype=np.floataa ) A__ : Union[str, Any] = retriever.retrieve(snake_case , n_docs=1 ) self.assertTrue(out is not None ) def _UpperCamelCase ( self : Optional[int] ): '''simple docstring''' A__ : Union[str, Any] = 1 A__ : int = self.get_dummy_custom_hf_index_retriever(from_disk=snake_case ) A__ : List[Any] = np.array( [np.ones(self.retrieval_vector_size ), -np.ones(self.retrieval_vector_size )] , dtype=np.floataa ) A__ , A__ , A__ : Optional[int] = retriever.retrieve(snake_case , n_docs=snake_case ) self.assertEqual(retrieved_doc_embeds.shape , (2, n_docs, self.retrieval_vector_size) ) self.assertEqual(len(snake_case ) , 2 ) self.assertEqual(sorted(doc_dicts[0] ) , ["""embeddings""", """id""", """text""", """title"""] ) self.assertEqual(len(doc_dicts[0]["""id"""] ) , snake_case ) self.assertEqual(doc_dicts[0]["""id"""][0] , """1""" ) # max inner product is reached with second doc self.assertEqual(doc_dicts[1]["""id"""][0] , """0""" ) # max inner product is reached with first doc self.assertListEqual(doc_ids.tolist() , [[1], [0]] ) def _UpperCamelCase ( self : Dict ): '''simple docstring''' A__ : Union[str, Any] = self.get_dummy_custom_hf_index_retriever(from_disk=snake_case ) with tempfile.TemporaryDirectory() as tmp_dirname: retriever.save_pretrained(snake_case ) A__ : Dict = RagRetriever.from_pretrained(snake_case ) self.assertIsInstance(snake_case , snake_case ) A__ : List[Any] = np.array( [np.ones(self.retrieval_vector_size ), -np.ones(self.retrieval_vector_size )] , dtype=np.floataa ) A__ : List[Any] = retriever.retrieve(snake_case , n_docs=1 ) self.assertTrue(out is not None ) def _UpperCamelCase ( self : Any ): '''simple docstring''' A__ : Tuple = 1 A__ : Dict = self.get_dummy_custom_hf_index_retriever(from_disk=snake_case ) A__ : Any = np.array( [np.ones(self.retrieval_vector_size ), -np.ones(self.retrieval_vector_size )] , dtype=np.floataa ) A__ , A__ , A__ : List[Any] = retriever.retrieve(snake_case , n_docs=snake_case ) self.assertEqual(retrieved_doc_embeds.shape , (2, n_docs, self.retrieval_vector_size) ) self.assertEqual(len(snake_case ) , 2 ) self.assertEqual(sorted(doc_dicts[0] ) , ["""embeddings""", """id""", """text""", """title"""] ) self.assertEqual(len(doc_dicts[0]["""id"""] ) , snake_case ) self.assertEqual(doc_dicts[0]["""id"""][0] , """1""" ) # max inner product is reached with second doc self.assertEqual(doc_dicts[1]["""id"""][0] , """0""" ) # max inner product is reached with first doc self.assertListEqual(doc_ids.tolist() , [[1], [0]] ) def _UpperCamelCase ( self : Union[str, Any] ): '''simple docstring''' A__ : int = self.get_dummy_custom_hf_index_retriever(from_disk=snake_case ) with tempfile.TemporaryDirectory() as tmp_dirname: retriever.save_pretrained(snake_case ) A__ : Optional[int] = RagRetriever.from_pretrained(snake_case ) self.assertIsInstance(snake_case , snake_case ) A__ : Tuple = np.array( [np.ones(self.retrieval_vector_size ), -np.ones(self.retrieval_vector_size )] , dtype=np.floataa ) A__ : Any = retriever.retrieve(snake_case , n_docs=1 ) self.assertTrue(out is not None ) def _UpperCamelCase ( self : List[Any] ): '''simple docstring''' A__ : List[str] = 1 A__ : Tuple = self.get_dummy_legacy_index_retriever() A__ : Optional[int] = np.array( [np.ones(self.retrieval_vector_size ), -np.ones(self.retrieval_vector_size )] , dtype=np.floataa ) A__ , A__ , A__ : int = retriever.retrieve(snake_case , n_docs=snake_case ) self.assertEqual(retrieved_doc_embeds.shape , (2, n_docs, self.retrieval_vector_size) ) self.assertEqual(len(snake_case ) , 2 ) self.assertEqual(sorted(doc_dicts[0] ) , ["""text""", """title"""] ) self.assertEqual(len(doc_dicts[0]["""text"""] ) , snake_case ) self.assertEqual(doc_dicts[0]["""text"""][0] , """bar""" ) # max inner product is reached with second doc self.assertEqual(doc_dicts[1]["""text"""][0] , """foo""" ) # max inner product is reached with first doc self.assertListEqual(doc_ids.tolist() , [[1], [0]] ) def _UpperCamelCase ( self : int ): '''simple docstring''' A__ : List[str] = self.get_dummy_legacy_index_retriever() with tempfile.TemporaryDirectory() as tmp_dirname: retriever.save_pretrained(snake_case ) A__ : Union[str, Any] = RagRetriever.from_pretrained(snake_case ) self.assertIsInstance(snake_case , snake_case ) A__ : Dict = np.array( [np.ones(self.retrieval_vector_size ), -np.ones(self.retrieval_vector_size )] , dtype=np.floataa ) A__ : List[str] = retriever.retrieve(snake_case , n_docs=1 ) self.assertTrue(out is not None ) @require_torch @require_tokenizers @require_sentencepiece def _UpperCamelCase ( self : Union[str, Any] ): '''simple docstring''' import torch A__ : Tuple = 1 A__ : Union[str, Any] = self.get_dummy_canonical_hf_index_retriever() A__ : Union[str, Any] = [[5, 7], [10, 11]] A__ : Optional[Any] = np.array( [np.ones(self.retrieval_vector_size ), -np.ones(self.retrieval_vector_size )] , dtype=np.floataa ) A__ : Optional[Any] = retriever(snake_case , snake_case , prefix=retriever.config.generator.prefix , n_docs=snake_case ) A__ , A__ , A__ : List[str] = ( out["""context_input_ids"""], out["""context_attention_mask"""], out["""retrieved_doc_embeds"""], ) self.assertEqual(retrieved_doc_embeds.shape , (2, n_docs, self.retrieval_vector_size) ) self.assertIsInstance(snake_case , snake_case ) self.assertIsInstance(snake_case , snake_case ) self.assertIsInstance(snake_case , np.ndarray ) A__ : Union[str, Any] = retriever( snake_case , snake_case , prefix=retriever.config.generator.prefix , n_docs=snake_case , return_tensors="""pt""" , ) A__ , A__ , A__ , A__ : List[Any] = ( # noqa: F841 out["""context_input_ids"""], out["""context_attention_mask"""], out["""retrieved_doc_embeds"""], out["""doc_ids"""], ) self.assertEqual(retrieved_doc_embeds.shape , (2, n_docs, self.retrieval_vector_size) ) self.assertIsInstance(snake_case , torch.Tensor ) self.assertIsInstance(snake_case , torch.Tensor ) self.assertIsInstance(snake_case , torch.Tensor ) @require_torch @require_tokenizers @require_sentencepiece def _UpperCamelCase ( self : Dict ): '''simple docstring''' A__ : Any = self.get_dpr_ctx_encoder_tokenizer() A__ : str = 1 A__ : Union[str, Any] = self.get_dummy_custom_hf_index_retriever(from_disk=snake_case ) retriever.set_ctx_encoder_tokenizer(snake_case ) A__ : List[str] = [[5, 7], [10, 11]] A__ : Dict = np.array( [np.ones(self.retrieval_vector_size ), -np.ones(self.retrieval_vector_size )] , dtype=np.floataa ) A__ : Dict = retriever(snake_case , snake_case , prefix=retriever.config.generator.prefix , n_docs=snake_case ) self.assertEqual( len(snake_case ) , 6 ) # check whether the retriever output consist of 6 attributes including tokenized docs self.assertEqual( all(k in out for k in ("""tokenized_doc_ids""", """tokenized_doc_attention_mask""") ) , snake_case ) # check for doc token related keys in dictionary.
296
"""simple docstring""" 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_ = logging.get_logger(__name__) A_ = {'''vocab_file''': '''spiece.model'''} A_ = { '''vocab_file''': { '''xlnet-base-cased''': '''https://huggingface.co/xlnet-base-cased/resolve/main/spiece.model''', '''xlnet-large-cased''': '''https://huggingface.co/xlnet-large-cased/resolve/main/spiece.model''', } } A_ = { '''xlnet-base-cased''': None, '''xlnet-large-cased''': None, } # Segments (not really needed) A_ = 0 A_ = 1 A_ = 2 A_ = 3 A_ = 4 class __SCREAMING_SNAKE_CASE ( UpperCamelCase ): snake_case_ = VOCAB_FILES_NAMES snake_case_ = PRETRAINED_VOCAB_FILES_MAP snake_case_ = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES snake_case_ = 'left' def __init__( self : Dict , snake_case : int , snake_case : List[Any]=False , snake_case : List[str]=True , snake_case : Dict=False , snake_case : Optional[Any]="<s>" , snake_case : List[str]="</s>" , snake_case : Tuple="<unk>" , snake_case : Tuple="<sep>" , snake_case : Union[str, Any]="<pad>" , snake_case : Dict="<cls>" , snake_case : Optional[Any]="<mask>" , snake_case : Optional[int]=["<eop>", "<eod>"] , snake_case : Optional[Dict[str, Any]] = None , **snake_case : Dict , ): '''simple docstring''' A__ : Optional[int] = AddedToken(snake_case , lstrip=snake_case , rstrip=snake_case ) if isinstance(snake_case , snake_case ) else mask_token A__ : Union[str, Any] = {} if sp_model_kwargs is None else sp_model_kwargs super().__init__( do_lower_case=snake_case , remove_space=snake_case , keep_accents=snake_case , bos_token=snake_case , eos_token=snake_case , unk_token=snake_case , sep_token=snake_case , pad_token=snake_case , cls_token=snake_case , mask_token=snake_case , additional_special_tokens=snake_case , sp_model_kwargs=self.sp_model_kwargs , **snake_case , ) A__ : str = 3 A__ : str = do_lower_case A__ : Optional[Any] = remove_space A__ : List[Any] = keep_accents A__ : Union[str, Any] = vocab_file A__ : Dict = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(snake_case ) @property def _UpperCamelCase ( self : Optional[int] ): '''simple docstring''' return len(self.sp_model ) def _UpperCamelCase ( self : List[Any] ): '''simple docstring''' A__ : int = {self.convert_ids_to_tokens(snake_case ): i for i in range(self.vocab_size )} vocab.update(self.added_tokens_encoder ) return vocab def __getstate__( self : str ): '''simple docstring''' A__ : int = self.__dict__.copy() A__ : int = None return state def __setstate__( self : Tuple , snake_case : Union[str, Any] ): '''simple docstring''' A__ : int = d # for backward compatibility if not hasattr(self , """sp_model_kwargs""" ): A__ : Optional[int] = {} A__ : Optional[int] = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(self.vocab_file ) def _UpperCamelCase ( self : List[str] , snake_case : Optional[Any] ): '''simple docstring''' if self.remove_space: A__ : Optional[Any] = """ """.join(inputs.strip().split() ) else: A__ : Dict = inputs A__ : str = outputs.replace("""``""" , """\"""" ).replace("""''""" , """\"""" ) if not self.keep_accents: A__ : Any = unicodedata.normalize("""NFKD""" , snake_case ) A__ : Optional[int] = """""".join([c for c in outputs if not unicodedata.combining(snake_case )] ) if self.do_lower_case: A__ : Any = outputs.lower() return outputs def _UpperCamelCase ( self : Union[str, Any] , snake_case : str ): '''simple docstring''' A__ : Dict = self.preprocess_text(snake_case ) A__ : Dict = self.sp_model.encode(snake_case , out_type=snake_case ) A__ : Optional[int] = [] for piece in pieces: if len(snake_case ) > 1 and piece[-1] == str(""",""" ) and piece[-2].isdigit(): A__ : Optional[Any] = self.sp_model.EncodeAsPieces(piece[:-1].replace(snake_case , """""" ) ) if piece[0] != SPIECE_UNDERLINE and cur_pieces[0][0] == SPIECE_UNDERLINE: if len(cur_pieces[0] ) == 1: A__ : int = cur_pieces[1:] else: A__ : Any = cur_pieces[0][1:] cur_pieces.append(piece[-1] ) new_pieces.extend(snake_case ) else: new_pieces.append(snake_case ) return new_pieces def _UpperCamelCase ( self : List[str] , snake_case : Tuple ): '''simple docstring''' return self.sp_model.PieceToId(snake_case ) def _UpperCamelCase ( self : List[str] , snake_case : Any ): '''simple docstring''' return self.sp_model.IdToPiece(snake_case ) def _UpperCamelCase ( self : Optional[int] , snake_case : Any ): '''simple docstring''' A__ : Union[str, Any] = """""".join(snake_case ).replace(snake_case , """ """ ).strip() return out_string def _UpperCamelCase ( self : int , snake_case : List[int] , snake_case : bool = False , snake_case : bool = None , snake_case : bool = True , **snake_case : Union[str, Any] , ): '''simple docstring''' A__ : List[str] = kwargs.pop("""use_source_tokenizer""" , snake_case ) A__ : Any = self.convert_ids_to_tokens(snake_case , skip_special_tokens=snake_case ) # To avoid mixing byte-level and unicode for byte-level BPT # we need to build string separately for added tokens and byte-level tokens # cf. https://github.com/huggingface/transformers/issues/1133 A__ : Any = [] A__ : Any = [] for token in filtered_tokens: if skip_special_tokens and token in self.all_special_ids: continue if token in self.added_tokens_encoder: if current_sub_text: sub_texts.append(self.convert_tokens_to_string(snake_case ) ) A__ : str = [] sub_texts.append(snake_case ) else: current_sub_text.append(snake_case ) if current_sub_text: sub_texts.append(self.convert_tokens_to_string(snake_case ) ) # Mimic the behavior of the Rust tokenizer: # By default, there are no spaces between special tokens A__ : Dict = """""".join(snake_case ) A__ : int = ( clean_up_tokenization_spaces if clean_up_tokenization_spaces is not None else self.clean_up_tokenization_spaces ) if clean_up_tokenization_spaces: A__ : Tuple = self.clean_up_tokenization(snake_case ) return clean_text else: return text def _UpperCamelCase ( self : str , snake_case : List[int] , snake_case : Optional[List[int]] = None ): '''simple docstring''' A__ : Tuple = [self.sep_token_id] A__ : Dict = [self.cls_token_id] if token_ids_a is None: return token_ids_a + sep + cls return token_ids_a + sep + token_ids_a + sep + cls def _UpperCamelCase ( self : Dict , snake_case : List[int] , snake_case : Optional[List[int]] = None , snake_case : bool = False ): '''simple docstring''' if already_has_special_tokens: return super().get_special_tokens_mask( token_ids_a=snake_case , token_ids_a=snake_case , already_has_special_tokens=snake_case ) if token_ids_a is not None: return ([0] * len(snake_case )) + [1] + ([0] * len(snake_case )) + [1, 1] return ([0] * len(snake_case )) + [1, 1] def _UpperCamelCase ( self : str , snake_case : List[int] , snake_case : Optional[List[int]] = None ): '''simple docstring''' A__ : Any = [self.sep_token_id] A__ : int = [2] if token_ids_a is None: return len(token_ids_a + sep ) * [0] + cls_segment_id return len(token_ids_a + sep ) * [0] + len(token_ids_a + sep ) * [1] + cls_segment_id def _UpperCamelCase ( self : Optional[Any] , snake_case : str , snake_case : Optional[str] = None ): '''simple docstring''' if not os.path.isdir(snake_case ): logger.error(F'Vocabulary path ({save_directory}) should be a directory' ) return A__ : List[Any] = os.path.join( snake_case , (filename_prefix + """-""" if filename_prefix else """""") + VOCAB_FILES_NAMES["""vocab_file"""] ) if os.path.abspath(self.vocab_file ) != os.path.abspath(snake_case ) and os.path.isfile(self.vocab_file ): copyfile(self.vocab_file , snake_case ) elif not os.path.isfile(self.vocab_file ): with open(snake_case , """wb""" ) as fi: A__ : Optional[Any] = self.sp_model.serialized_model_proto() fi.write(snake_case ) return (out_vocab_file,)
296
1
from __future__ import annotations import inspect import unittest from math import floor import numpy as np from transformers import CvtConfig from transformers.testing_utils import require_tf, require_vision, slow from transformers.utils import cached_property, is_tf_available, is_vision_available from ...test_configuration_common import ConfigTester from ...test_modeling_tf_common import TFModelTesterMixin, floats_tensor, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_tf_available(): import tensorflow as tf from transformers import TFCvtForImageClassification, TFCvtModel from transformers.models.cvt.modeling_tf_cvt import TF_CVT_PRETRAINED_MODEL_ARCHIVE_LIST if is_vision_available(): from PIL import Image from transformers import AutoImageProcessor class __magic_name__ ( __lowerCAmelCase): def UpperCAmelCase__ ( self : Tuple ) -> Union[str, Any]: '''simple docstring''' UpperCamelCase__ : Dict = self.config_class(**self.inputs_dict ) self.parent.assertTrue(hasattr(lowerCamelCase__ , '''embed_dim''' ) ) self.parent.assertTrue(hasattr(lowerCamelCase__ , '''num_heads''' ) ) class __magic_name__ : def __init__( self : Dict , lowerCamelCase__ : List[str] , lowerCamelCase__ : str=13 , lowerCamelCase__ : Optional[Any]=64 , lowerCamelCase__ : Tuple=3 , lowerCamelCase__ : Dict=[16, 48, 96] , lowerCamelCase__ : Optional[Any]=[1, 3, 6] , lowerCamelCase__ : Optional[Any]=[1, 2, 10] , lowerCamelCase__ : Optional[Any]=[7, 3, 3] , lowerCamelCase__ : Any=[4, 2, 2] , lowerCamelCase__ : str=[2, 1, 1] , lowerCamelCase__ : Union[str, Any]=[2, 2, 2] , lowerCamelCase__ : Any=[False, False, True] , lowerCamelCase__ : Optional[Any]=[0.0, 0.0, 0.0] , lowerCamelCase__ : Optional[Any]=0.02 , lowerCamelCase__ : Optional[Any]=1E-1_2 , lowerCamelCase__ : List[Any]=True , lowerCamelCase__ : Optional[Any]=True , lowerCamelCase__ : Optional[Any]=2 , ) -> str: '''simple docstring''' UpperCamelCase__ : Optional[int] = parent UpperCamelCase__ : Optional[Any] = batch_size UpperCamelCase__ : Any = image_size UpperCamelCase__ : Any = patch_sizes UpperCamelCase__ : Optional[int] = patch_stride UpperCamelCase__ : List[Any] = patch_padding UpperCamelCase__ : Any = is_training UpperCamelCase__ : Optional[int] = use_labels UpperCamelCase__ : str = num_labels UpperCamelCase__ : Optional[int] = num_channels UpperCamelCase__ : Optional[int] = embed_dim UpperCamelCase__ : List[str] = num_heads UpperCamelCase__ : List[Any] = stride_kv UpperCamelCase__ : Union[str, Any] = depth UpperCamelCase__ : Dict = cls_token UpperCamelCase__ : Tuple = attention_drop_rate UpperCamelCase__ : List[Any] = initializer_range UpperCamelCase__ : Tuple = layer_norm_eps def UpperCAmelCase__ ( self : Any ) -> Optional[Any]: '''simple docstring''' UpperCamelCase__ : Optional[int] = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) UpperCamelCase__ : Tuple = None if self.use_labels: # create a random int32 tensor of given shape UpperCamelCase__ : Optional[int] = ids_tensor([self.batch_size] , self.num_labels ) UpperCamelCase__ : str = self.get_config() return config, pixel_values, labels def UpperCAmelCase__ ( self : Optional[Any] ) -> int: '''simple docstring''' return CvtConfig( image_size=self.image_size , num_labels=self.num_labels , num_channels=self.num_channels , embed_dim=self.embed_dim , num_heads=self.num_heads , patch_sizes=self.patch_sizes , patch_padding=self.patch_padding , patch_stride=self.patch_stride , stride_kv=self.stride_kv , depth=self.depth , cls_token=self.cls_token , attention_drop_rate=self.attention_drop_rate , initializer_range=self.initializer_range , ) def UpperCAmelCase__ ( self : Optional[Any] , lowerCamelCase__ : List[str] , lowerCamelCase__ : List[Any] , lowerCamelCase__ : Union[str, Any] ) -> Optional[int]: '''simple docstring''' UpperCamelCase__ : List[Any] = TFCvtModel(config=lowerCamelCase__ ) UpperCamelCase__ : int = model(lowerCamelCase__ , training=lowerCamelCase__ ) UpperCamelCase__ : List[Any] = (self.image_size, self.image_size) UpperCamelCase__ , UpperCamelCase__ : List[Any] = image_size[0], image_size[1] for i in range(len(self.depth ) ): UpperCamelCase__ : List[str] = floor(((height + 2 * self.patch_padding[i] - self.patch_sizes[i]) / self.patch_stride[i]) + 1 ) UpperCamelCase__ : Tuple = floor(((width + 2 * self.patch_padding[i] - self.patch_sizes[i]) / self.patch_stride[i]) + 1 ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.embed_dim[-1], height, width) ) def UpperCAmelCase__ ( self : Tuple , lowerCamelCase__ : int , lowerCamelCase__ : Any , lowerCamelCase__ : Optional[Any] ) -> Optional[int]: '''simple docstring''' UpperCamelCase__ : Any = self.num_labels UpperCamelCase__ : str = TFCvtForImageClassification(lowerCamelCase__ ) UpperCamelCase__ : List[str] = model(lowerCamelCase__ , labels=lowerCamelCase__ , training=lowerCamelCase__ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_labels) ) def UpperCAmelCase__ ( self : Tuple ) -> int: '''simple docstring''' UpperCamelCase__ : List[Any] = self.prepare_config_and_inputs() UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ : List[Any] = config_and_inputs UpperCamelCase__ : Tuple = {'''pixel_values''': pixel_values} return config, inputs_dict @require_tf class __magic_name__ ( __lowerCAmelCase , __lowerCAmelCase , unittest.TestCase): A: int = (TFCvtModel, TFCvtForImageClassification) if is_tf_available() else () A: Tuple = ( {"feature-extraction": TFCvtModel, "image-classification": TFCvtForImageClassification} if is_tf_available() else {} ) A: str = False A: str = False A: int = False A: Union[str, Any] = False A: int = False def UpperCAmelCase__ ( self : Optional[int] ) -> Dict: '''simple docstring''' UpperCamelCase__ : Optional[Any] = TFCvtModelTester(self ) UpperCamelCase__ : Optional[int] = TFCvtConfigTester(self , config_class=lowerCamelCase__ , has_text_modality=lowerCamelCase__ , hidden_size=37 ) def UpperCAmelCase__ ( self : Tuple ) -> List[str]: '''simple docstring''' self.config_tester.create_and_test_config_common_properties() self.config_tester.create_and_test_config_to_json_string() self.config_tester.create_and_test_config_to_json_file() self.config_tester.create_and_test_config_from_and_save_pretrained() self.config_tester.create_and_test_config_with_num_labels() self.config_tester.check_config_can_be_init_without_params() self.config_tester.check_config_arguments_init() @unittest.skip(reason='''Cvt does not output attentions''' ) def UpperCAmelCase__ ( self : Any ) -> Optional[int]: '''simple docstring''' pass @unittest.skip(reason='''Cvt does not use inputs_embeds''' ) def UpperCAmelCase__ ( self : Tuple ) -> str: '''simple docstring''' pass @unittest.skip(reason='''Cvt does not support input and output embeddings''' ) def UpperCAmelCase__ ( self : Tuple ) -> Optional[int]: '''simple docstring''' pass @unittest.skipIf( not is_tf_available() or len(tf.config.list_physical_devices('''GPU''' ) ) == 0 , reason='''TF does not support backprop for grouped convolutions on CPU.''' , ) def UpperCAmelCase__ ( self : Tuple ) -> Optional[int]: '''simple docstring''' super().test_dataset_conversion() @unittest.skipIf( not is_tf_available() or len(tf.config.list_physical_devices('''GPU''' ) ) == 0 , reason='''TF does not support backprop for grouped convolutions on CPU.''' , ) @slow def UpperCAmelCase__ ( self : Optional[int] ) -> Dict: '''simple docstring''' super().test_keras_fit() @unittest.skip(reason='''Get `Failed to determine best cudnn convolution algo.` error after using TF 2.12+cuda 11.8''' ) def UpperCAmelCase__ ( self : Any ) -> Union[str, Any]: '''simple docstring''' UpperCamelCase__ : Tuple = tf.keras.mixed_precision.Policy('''mixed_float16''' ) tf.keras.mixed_precision.set_global_policy(lowerCamelCase__ ) super().test_keras_fit() tf.keras.mixed_precision.set_global_policy('''float32''' ) def UpperCAmelCase__ ( self : Optional[Any] ) -> Tuple: '''simple docstring''' UpperCamelCase__ , UpperCamelCase__ : Tuple = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: UpperCamelCase__ : int = model_class(lowerCamelCase__ ) UpperCamelCase__ : Dict = inspect.signature(model.call ) # signature.parameters is an OrderedDict => so arg_names order is deterministic UpperCamelCase__ : Dict = [*signature.parameters.keys()] UpperCamelCase__ : Union[str, Any] = ['''pixel_values'''] self.assertListEqual(arg_names[:1] , lowerCamelCase__ ) def UpperCAmelCase__ ( self : Dict ) -> List[Any]: '''simple docstring''' def check_hidden_states_output(lowerCamelCase__ : str , lowerCamelCase__ : Union[str, Any] , lowerCamelCase__ : List[Any] ): UpperCamelCase__ : Dict = model_class(lowerCamelCase__ ) UpperCamelCase__ : Tuple = model(**self._prepare_for_class(lowerCamelCase__ , lowerCamelCase__ ) ) UpperCamelCase__ : Optional[Any] = outputs.hidden_states UpperCamelCase__ : Optional[Any] = len(self.model_tester.depth ) self.assertEqual(len(lowerCamelCase__ ) , lowerCamelCase__ ) # verify the first hidden states (first block) self.assertListEqual( list(hidden_states[0].shape[-3:] ) , [ self.model_tester.embed_dim[0], self.model_tester.image_size // 4, self.model_tester.image_size // 4, ] , ) UpperCamelCase__ , UpperCamelCase__ : str = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: UpperCamelCase__ : int = True check_hidden_states_output(lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ ) # check that output_hidden_states also work using config del inputs_dict["output_hidden_states"] UpperCamelCase__ : int = True check_hidden_states_output(lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ ) def UpperCAmelCase__ ( self : Tuple ) -> str: '''simple docstring''' UpperCamelCase__ : Dict = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*lowerCamelCase__ ) def UpperCAmelCase__ ( self : List[Any] ) -> Union[str, Any]: '''simple docstring''' UpperCamelCase__ : List[Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_image_classification(*lowerCamelCase__ ) @slow def UpperCAmelCase__ ( self : Optional[Any] ) -> Tuple: '''simple docstring''' for model_name in TF_CVT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: UpperCamelCase__ : List[Any] = TFCvtModel.from_pretrained(lowerCamelCase__ ) self.assertIsNotNone(lowerCamelCase__ ) def _a ( ): """simple docstring""" UpperCamelCase__ : Tuple = Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png''' ) return image @require_tf @require_vision class __magic_name__ ( unittest.TestCase): @cached_property def UpperCAmelCase__ ( self : Union[str, Any] ) -> Tuple: '''simple docstring''' return AutoImageProcessor.from_pretrained(TF_CVT_PRETRAINED_MODEL_ARCHIVE_LIST[0] ) @slow def UpperCAmelCase__ ( self : Any ) -> List[Any]: '''simple docstring''' UpperCamelCase__ : Union[str, Any] = TFCvtForImageClassification.from_pretrained(TF_CVT_PRETRAINED_MODEL_ARCHIVE_LIST[0] ) UpperCamelCase__ : Dict = self.default_image_processor UpperCamelCase__ : Optional[Any] = prepare_img() UpperCamelCase__ : Union[str, Any] = image_processor(images=lowerCamelCase__ , return_tensors='''tf''' ) # forward pass UpperCamelCase__ : Any = model(**lowerCamelCase__ ) # verify the logits UpperCamelCase__ : str = tf.TensorShape((1, 1000) ) self.assertEqual(outputs.logits.shape , lowerCamelCase__ ) UpperCamelCase__ : Union[str, Any] = tf.constant([0.9285, 0.9015, -0.3150] ) self.assertTrue(np.allclose(outputs.logits[0, :3].numpy() , lowerCamelCase__ , atol=1E-4 ) )
146
import cmath import math def _a ( SCREAMING_SNAKE_CASE : float , SCREAMING_SNAKE_CASE : float , SCREAMING_SNAKE_CASE : float , SCREAMING_SNAKE_CASE : float ): """simple docstring""" UpperCamelCase__ : Union[str, Any] = math.radians(SCREAMING_SNAKE_CASE ) UpperCamelCase__ : List[str] = math.radians(SCREAMING_SNAKE_CASE ) # Convert voltage and current to rectangular form UpperCamelCase__ : str = cmath.rect(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ) UpperCamelCase__ : Any = cmath.rect(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ) # Calculate apparent power return voltage_rect * current_rect if __name__ == "__main__": import doctest doctest.testmod()
146
1
'''simple docstring''' import os import re import unicodedata from shutil import copyfile from typing import TYPE_CHECKING, Any, Dict, List, Optional, Tuple, Union import sentencepiece as spm from ...tokenization_utils import PreTrainedTokenizer from ...utils import is_torch_available, logging if is_torch_available(): import torch if TYPE_CHECKING: from transformers.pipelines.conversational import Conversation _UpperCAmelCase : List[str] = logging.get_logger(__name__) _UpperCAmelCase : Tuple = {"""vocab_file""": """spiece.model"""} _UpperCAmelCase : Union[str, Any] = { """vocab_file""": { """AI-Sweden/gpt-sw3-126m""": """https://huggingface.co/AI-Sweden/gpt-sw3-126m/resolve/main/spiece.model""", """AI-Sweden/gpt-sw3-350m""": """https://huggingface.co/AI-Sweden/gpt-sw3-350m/resolve/main/spiece.model""", """AI-Sweden/gpt-sw3-1.6b""": """https://huggingface.co/AI-Sweden/gpt-sw3-1.6b/resolve/main/spiece.model""", """AI-Sweden/gpt-sw3-6.7b""": """https://huggingface.co/AI-Sweden/gpt-sw3-6.7b/resolve/main/spiece.model""", """AI-Sweden/gpt-sw3-20b""": """https://huggingface.co/AI-Sweden/gpt-sw3-20b/resolve/main/spiece.model""", } } _UpperCAmelCase : Any = { """AI-Sweden/gpt-sw3-126m""": 2_0_4_8, """AI-Sweden/gpt-sw3-350m""": 2_0_4_8, """AI-Sweden/gpt-sw3-1.6b""": 2_0_4_8, """AI-Sweden/gpt-sw3-6.7b""": 2_0_4_8, """AI-Sweden/gpt-sw3-20b""": 2_0_4_8, } class a__ ( __lowerCAmelCase ): """simple docstring""" __UpperCamelCase : str = VOCAB_FILES_NAMES __UpperCamelCase : List[Any] = PRETRAINED_VOCAB_FILES_MAP __UpperCamelCase : Optional[Any] = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES __UpperCamelCase : Tuple = ['''input_ids''', '''attention_mask'''] def __init__(self , __lowercase , __lowercase=False , __lowercase=False , __lowercase=False , __lowercase=None , __lowercase=None , __lowercase=None , __lowercase=None , __lowercase = None , **__lowercase , ): __lowerCAmelCase = {} if sp_model_kwargs is None else sp_model_kwargs __lowerCAmelCase = kwargs.get('''name_or_path''' ) if name_or_path is None: logger.warning( '''name_or_path not provided, will work for all GPTSw3 models except gpt-sw3-7b,''' ''' you are testing the model, this can safely be ignored''' ) __lowerCAmelCase = '''None''' # Default definitions for our 2 tokenizer versions, with None-checks to enable proper testing __lowerCAmelCase = '''<|endoftext|>''' if eos_token is None else eos_token __lowerCAmelCase = '''<unk>''' if unk_token is None else unk_token if "gpt-sw3-7b" in name_or_path: __lowerCAmelCase = unk_token if pad_token is None else pad_token __lowerCAmelCase = eos_token if bos_token is None else bos_token else: __lowerCAmelCase = '''<pad>''' if pad_token is None else pad_token __lowerCAmelCase = '''<s>''' if bos_token is None else bos_token super().__init__( do_lower_case=lowerCAmelCase_ , remove_space=lowerCAmelCase_ , keep_accents=lowerCAmelCase_ , bos_token=lowerCAmelCase_ , eos_token=lowerCAmelCase_ , unk_token=lowerCAmelCase_ , pad_token=lowerCAmelCase_ , sp_model_kwargs=self.sp_model_kwargs , **lowerCAmelCase_ , ) __lowerCAmelCase = do_lower_case __lowerCAmelCase = remove_space __lowerCAmelCase = keep_accents __lowerCAmelCase = vocab_file __lowerCAmelCase = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(lowerCAmelCase_ ) # Used for whitespace normalization in input texts # fmt : off __lowerCAmelCase = {''' ''', ''' ''', ''' ''', ''' ''', ''' ''', ''' ''', ''' ''', ''' ''', ''' ''', ''' ''', '''''', '''„'''} # fmt : on # Regular expression to remove non-printing characters (e.g. some unicode control chars) in preprocessing __lowerCAmelCase = re.compile( F"""[{"".join(map(lowerCAmelCase_ , list(range(0 , 9 ) ) + list(range(11 , 32 ) ) + list(range(1_27 , 1_60 ) ) + [1_60, 1_73, 82_03] ) )}]""" ) def __getstate__(self ): __lowerCAmelCase = self.__dict__.copy() __lowerCAmelCase = None return state def __setstate__(self , __lowercase ): __lowerCAmelCase = d # for backward compatibility if not hasattr(self , '''sp_model_kwargs''' ): __lowerCAmelCase = {} __lowerCAmelCase = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(self.vocab_file ) @property # Copied from transformers.models.albert.tokenization_albert.AlbertTokenizer.vocab_size def _snake_case (self ): return len(self.sp_model ) def _snake_case (self , __lowercase ): __lowerCAmelCase = self.non_printing_characters_re.sub('''''' , lowerCAmelCase_ ) # Normalize whitespaces __lowerCAmelCase = ''''''.join([char if char not in self.whitespaces else ''' ''' for char in text] ) # NFC Unicode normalization __lowerCAmelCase = unicodedata.normalize('''NFC''' , lowerCAmelCase_ ) return text def _snake_case (self , __lowercase , **__lowercase ): __lowerCAmelCase = self.preprocess_text(lowerCAmelCase_ ) return self.sp_model.encode(lowerCAmelCase_ , out_type=lowerCAmelCase_ ) def _snake_case (self , __lowercase ): return self.sp_model.PieceToId(lowerCAmelCase_ ) def _snake_case (self , __lowercase ): return self.sp_model.IdToPiece(lowerCAmelCase_ ) @staticmethod def _snake_case (__lowercase ): return out_string def _snake_case (self , __lowercase ): __lowerCAmelCase = [] __lowerCAmelCase = '''''' __lowerCAmelCase = False for token in tokens: # make sure that special tokens are not decoded using sentencepiece model if token in self.all_special_tokens: # TODO: Check if this is needed, as it ensures that decode(encode(doc)) != doc by adding extra whitespace in the decoded document if not prev_is_special: out_string += " " out_string += self.sp_model.decode(lowerCAmelCase_ ) + token __lowerCAmelCase = True __lowerCAmelCase = [] else: current_sub_tokens.append(lowerCAmelCase_ ) __lowerCAmelCase = False out_string += self.sp_model.decode(lowerCAmelCase_ ) return out_string def _snake_case (self ): __lowerCAmelCase = {self.convert_ids_to_tokens(lowerCAmelCase_ ): i for i in range(self.vocab_size )} vocab.update(self.added_tokens_encoder ) return vocab def _snake_case (self , __lowercase , __lowercase = None ): if not os.path.isdir(lowerCAmelCase_ ): logger.error(F"""Vocabulary path ({save_directory}) should be a directory""" ) return __lowerCAmelCase = 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: __lowerCAmelCase = self.sp_model.serialized_model_proto() fi.write(lowerCAmelCase_ ) return (out_vocab_file,) def _snake_case (self , __lowercase , __lowercase = False ): if isinstance(lowerCAmelCase_ , lowerCAmelCase_ ): __lowerCAmelCase = self.preprocess_text(lowerCAmelCase_ ) __lowerCAmelCase = self.sp_model.encode(lowerCAmelCase_ ) else: __lowerCAmelCase = [self.preprocess_text(lowerCAmelCase_ ) for t in text] __lowerCAmelCase = self.sp_model.encode(lowerCAmelCase_ ) if return_tensors is True or return_tensors == "pt": __lowerCAmelCase = torch.tensor(lowerCAmelCase_ ) return token_ids def _snake_case (self , __lowercase ): return self.sp_model.decode(lowerCAmelCase_ ) def _snake_case (self , __lowercase ): __lowerCAmelCase = [F"""User: {text}""" if is_user else F"""Bot: {text}""" for is_user, text in conversation.iter_texts()] __lowerCAmelCase = ( F"""{self.eos_token}{self.bos_token}""" + F"""{self.bos_token}""".join(lowerCAmelCase_ ) + F"""{self.bos_token}Bot:""" ) return self.encode(text=lowerCAmelCase_ )
364
'''simple docstring''' import numpy as np def __magic_name__( lowerCamelCase, lowerCamelCase, lowerCamelCase = 1E-12, lowerCamelCase = 1_0_0, ): assert np.shape(lowerCamelCase)[0] == np.shape(lowerCamelCase)[1] # Ensure proper dimensionality. assert np.shape(lowerCamelCase)[0] == np.shape(lowerCamelCase)[0] # Ensure inputs are either both complex or both real assert np.iscomplexobj(lowerCamelCase) == np.iscomplexobj(lowerCamelCase) __lowerCAmelCase = np.iscomplexobj(lowerCamelCase) if is_complex: # Ensure complex input_matrix is Hermitian assert np.array_equal(lowerCamelCase, input_matrix.conj().T) # Set convergence to False. Will define convergence when we exceed max_iterations # or when we have small changes from one iteration to next. __lowerCAmelCase = False __lowerCAmelCase = 0 __lowerCAmelCase = 0 __lowerCAmelCase = 1E12 while not convergence: # Multiple matrix by the vector. __lowerCAmelCase = np.dot(lowerCamelCase, lowerCamelCase) # Normalize the resulting output vector. __lowerCAmelCase = w / np.linalg.norm(lowerCamelCase) # Find rayleigh quotient # (faster than usual b/c we know vector is normalized already) __lowerCAmelCase = vector.conj().T if is_complex else vector.T __lowerCAmelCase = np.dot(lowerCamelCase, np.dot(lowerCamelCase, lowerCamelCase)) # Check convergence. __lowerCAmelCase = np.abs(lambda_ - lambda_previous) / lambda_ iterations += 1 if error <= error_tol or iterations >= max_iterations: __lowerCAmelCase = True __lowerCAmelCase = lambda_ if is_complex: __lowerCAmelCase = np.real(lambda_) return lambda_, vector def __magic_name__( ): __lowerCAmelCase = np.array([[4_1, 4, 2_0], [4, 2_6, 3_0], [2_0, 3_0, 5_0]]) __lowerCAmelCase = np.array([4_1, 4, 2_0]) __lowerCAmelCase = real_input_matrix.astype(np.complexaaa) __lowerCAmelCase = np.triu(1J * complex_input_matrix, 1) complex_input_matrix += imag_matrix complex_input_matrix += -1 * imag_matrix.T __lowerCAmelCase = np.array([4_1, 4, 2_0]).astype(np.complexaaa) for problem_type in ["real", "complex"]: if problem_type == "real": __lowerCAmelCase = real_input_matrix __lowerCAmelCase = real_vector elif problem_type == "complex": __lowerCAmelCase = complex_input_matrix __lowerCAmelCase = complex_vector # Our implementation. __lowerCAmelCase , __lowerCAmelCase = power_iteration(lowerCamelCase, lowerCamelCase) # Numpy implementation. # Get eigenvalues and eigenvectors using built-in numpy # eigh (eigh used for symmetric or hermetian matrices). __lowerCAmelCase , __lowerCAmelCase = np.linalg.eigh(lowerCamelCase) # Last eigenvalue is the maximum one. __lowerCAmelCase = eigen_values[-1] # Last column in this matrix is eigenvector corresponding to largest eigenvalue. __lowerCAmelCase = eigen_vectors[:, -1] # Check our implementation and numpy gives close answers. assert np.abs(eigen_value - eigen_value_max) <= 1E-6 # Take absolute values element wise of each eigenvector. # as they are only unique to a minus sign. assert np.linalg.norm(np.abs(lowerCamelCase) - np.abs(lowerCamelCase)) <= 1E-6 if __name__ == "__main__": import doctest doctest.testmod() test_power_iteration()
9
0
"""simple docstring""" 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 logging __a = logging.get_logger(__name__) __a = {"vocab_file": "spiece.model"} __a = { "vocab_file": { "albert-base-v1": "https://huggingface.co/albert-base-v1/resolve/main/spiece.model", "albert-large-v1": "https://huggingface.co/albert-large-v1/resolve/main/spiece.model", "albert-xlarge-v1": "https://huggingface.co/albert-xlarge-v1/resolve/main/spiece.model", "albert-xxlarge-v1": "https://huggingface.co/albert-xxlarge-v1/resolve/main/spiece.model", "albert-base-v2": "https://huggingface.co/albert-base-v2/resolve/main/spiece.model", "albert-large-v2": "https://huggingface.co/albert-large-v2/resolve/main/spiece.model", "albert-xlarge-v2": "https://huggingface.co/albert-xlarge-v2/resolve/main/spiece.model", "albert-xxlarge-v2": "https://huggingface.co/albert-xxlarge-v2/resolve/main/spiece.model", } } __a = { "albert-base-v1": 5_12, "albert-large-v1": 5_12, "albert-xlarge-v1": 5_12, "albert-xxlarge-v1": 5_12, "albert-base-v2": 5_12, "albert-large-v2": 5_12, "albert-xlarge-v2": 5_12, "albert-xxlarge-v2": 5_12, } __a = "▁" class lowerCamelCase ( _lowerCAmelCase ): '''simple docstring''' _A : List[Any] = VOCAB_FILES_NAMES _A : Optional[Any] = PRETRAINED_VOCAB_FILES_MAP _A : List[Any] = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES def __init__( self: List[Any] , snake_case: Union[str, Any] , snake_case: Tuple=True , snake_case: Tuple=True , snake_case: List[str]=False , snake_case: str="[CLS]" , snake_case: Union[str, Any]="[SEP]" , snake_case: Union[str, Any]="<unk>" , snake_case: int="[SEP]" , snake_case: Optional[Any]="<pad>" , snake_case: Dict="[CLS]" , snake_case: Any="[MASK]" , snake_case: Optional[Dict[str, Any]] = None , **snake_case: int , ) -> None: # Mask token behave like a normal word, i.e. include the space before it and # is included in the raw text, there should be a match in a non-normalized sentence. snake_case_ :int = ( AddedToken(snake_case , lstrip=snake_case , rstrip=snake_case , normalized=snake_case ) if isinstance(snake_case , snake_case ) else mask_token ) snake_case_ :List[str] = {} if sp_model_kwargs is None else sp_model_kwargs super().__init__( do_lower_case=snake_case , remove_space=snake_case , keep_accents=snake_case , bos_token=snake_case , eos_token=snake_case , unk_token=snake_case , sep_token=snake_case , pad_token=snake_case , cls_token=snake_case , mask_token=snake_case , sp_model_kwargs=self.sp_model_kwargs , **snake_case , ) snake_case_ :Any = do_lower_case snake_case_ :Optional[int] = remove_space snake_case_ :Optional[Any] = keep_accents snake_case_ :List[str] = vocab_file snake_case_ :int = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(snake_case ) @property def lowerCAmelCase_ ( self: int ) -> str: return len(self.sp_model ) def lowerCAmelCase_ ( self: Tuple ) -> Optional[Any]: snake_case_ :Optional[int] = {self.convert_ids_to_tokens(snake_case ): i for i in range(self.vocab_size )} vocab.update(self.added_tokens_encoder ) return vocab def __getstate__( self: str ) -> List[str]: snake_case_ :List[str] = self.__dict__.copy() snake_case_ :Optional[Any] = None return state def __setstate__( self: Optional[int] , snake_case: Union[str, Any] ) -> Dict: snake_case_ :List[Any] = d # for backward compatibility if not hasattr(self , """sp_model_kwargs""" ): snake_case_ :str = {} snake_case_ :str = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(self.vocab_file ) def lowerCAmelCase_ ( self: Tuple , snake_case: List[str] ) -> List[str]: if self.remove_space: snake_case_ :str = """ """.join(inputs.strip().split() ) else: snake_case_ :List[str] = inputs snake_case_ :Union[str, Any] = outputs.replace("""``""" , """\"""" ).replace("""''""" , """\"""" ) if not self.keep_accents: snake_case_ :Optional[Any] = unicodedata.normalize("""NFKD""" , snake_case ) snake_case_ :Tuple = """""".join([c for c in outputs if not unicodedata.combining(snake_case )] ) if self.do_lower_case: snake_case_ :Optional[Any] = outputs.lower() return outputs def lowerCAmelCase_ ( self: List[str] , snake_case: str ) -> List[str]: snake_case_ :Tuple = self.preprocess_text(snake_case ) snake_case_ :str = self.sp_model.encode(snake_case , out_type=snake_case ) snake_case_ :List[str] = [] for piece in pieces: if len(snake_case ) > 1 and piece[-1] == str(""",""" ) and piece[-2].isdigit(): snake_case_ :Tuple = self.sp_model.EncodeAsPieces(piece[:-1].replace(snake_case , """""" ) ) if piece[0] != SPIECE_UNDERLINE and cur_pieces[0][0] == SPIECE_UNDERLINE: if len(cur_pieces[0] ) == 1: snake_case_ :List[Any] = cur_pieces[1:] else: snake_case_ :Any = cur_pieces[0][1:] cur_pieces.append(piece[-1] ) new_pieces.extend(snake_case ) else: new_pieces.append(snake_case ) return new_pieces def lowerCAmelCase_ ( self: Optional[int] , snake_case: Union[str, Any] ) -> Any: return self.sp_model.PieceToId(snake_case ) def lowerCAmelCase_ ( self: Tuple , snake_case: Dict ) -> str: return self.sp_model.IdToPiece(snake_case ) def lowerCAmelCase_ ( self: List[str] , snake_case: Dict ) -> Optional[int]: snake_case_ :str = [] snake_case_ :Tuple = """""" snake_case_ :Any = False for token in tokens: # make sure that special tokens are not decoded using sentencepiece model if token in self.all_special_tokens: if not prev_is_special: out_string += " " out_string += self.sp_model.decode(snake_case ) + token snake_case_ :Dict = True snake_case_ :Dict = [] else: current_sub_tokens.append(snake_case ) snake_case_ :Dict = False out_string += self.sp_model.decode(snake_case ) return out_string.strip() def lowerCAmelCase_ ( self: Optional[int] , snake_case: List[int] , snake_case: Optional[List[int]] = None ) -> List[int]: snake_case_ :int = [self.sep_token_id] snake_case_ :Union[str, Any] = [self.cls_token_id] if token_ids_a is None: return cls + token_ids_a + sep return cls + token_ids_a + sep + token_ids_a + sep def lowerCAmelCase_ ( self: Optional[int] , snake_case: List[int] , snake_case: Optional[List[int]] = None , snake_case: bool = False ) -> List[int]: if already_has_special_tokens: return super().get_special_tokens_mask( token_ids_a=snake_case , token_ids_a=snake_case , already_has_special_tokens=snake_case ) if token_ids_a is not None: return [1] + ([0] * len(snake_case )) + [1] + ([0] * len(snake_case )) + [1] return [1] + ([0] * len(snake_case )) + [1] def lowerCAmelCase_ ( self: Any , snake_case: List[int] , snake_case: Optional[List[int]] = None ) -> List[int]: snake_case_ :Tuple = [self.sep_token_id] snake_case_ :Tuple = [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 lowerCAmelCase_ ( self: Dict , snake_case: str , snake_case: Optional[str] = None ) -> Tuple[str]: if not os.path.isdir(snake_case ): logger.error(f"""Vocabulary path ({save_directory}) should be a directory""" ) return snake_case_ :Any = os.path.join( snake_case , (filename_prefix + """-""" if filename_prefix else """""") + VOCAB_FILES_NAMES["""vocab_file"""] ) if os.path.abspath(self.vocab_file ) != os.path.abspath(snake_case ) and os.path.isfile(self.vocab_file ): copyfile(self.vocab_file , snake_case ) elif not os.path.isfile(self.vocab_file ): with open(snake_case , """wb""" ) as fi: snake_case_ :Dict = self.sp_model.serialized_model_proto() fi.write(snake_case ) return (out_vocab_file,)
66
from collections import UserDict from typing import List, Union from ..utils import ( add_end_docstrings, is_tf_available, is_torch_available, is_vision_available, logging, requires_backends, ) from .base import PIPELINE_INIT_ARGS, Pipeline if is_vision_available(): from PIL import Image from ..image_utils import load_image if is_torch_available(): from ..models.auto.modeling_auto import MODEL_FOR_ZERO_SHOT_IMAGE_CLASSIFICATION_MAPPING if is_tf_available(): from ..models.auto.modeling_tf_auto import TF_MODEL_FOR_ZERO_SHOT_IMAGE_CLASSIFICATION_MAPPING from ..tf_utils import stable_softmax a_ = logging.get_logger(__name__) @add_end_docstrings(_UpperCAmelCase ) class lowercase__ ( _UpperCAmelCase ): def __init__( self , **__UpperCAmelCase )-> List[str]: '''simple docstring''' super().__init__(**__UpperCAmelCase ) requires_backends(self , "vision" ) self.check_model_type( TF_MODEL_FOR_ZERO_SHOT_IMAGE_CLASSIFICATION_MAPPING if self.framework == "tf" else MODEL_FOR_ZERO_SHOT_IMAGE_CLASSIFICATION_MAPPING ) def __call__( self , __UpperCAmelCase , **__UpperCAmelCase )-> int: '''simple docstring''' return super().__call__(__UpperCAmelCase , **__UpperCAmelCase ) def UpperCAmelCase ( self , **__UpperCAmelCase )-> List[str]: '''simple docstring''' lowerCAmelCase__ = {} if "candidate_labels" in kwargs: lowerCAmelCase__ = kwargs["candidate_labels"] if "hypothesis_template" in kwargs: lowerCAmelCase__ = kwargs["hypothesis_template"] return preprocess_params, {}, {} def UpperCAmelCase ( self , __UpperCAmelCase , __UpperCAmelCase=None , __UpperCAmelCase="This is a photo of {}." )-> Optional[int]: '''simple docstring''' lowerCAmelCase__ = load_image(__UpperCAmelCase ) lowerCAmelCase__ = self.image_processor(images=[image] , return_tensors=self.framework ) lowerCAmelCase__ = candidate_labels lowerCAmelCase__ = [hypothesis_template.format(__UpperCAmelCase ) for x in candidate_labels] lowerCAmelCase__ = self.tokenizer(__UpperCAmelCase , return_tensors=self.framework , padding=__UpperCAmelCase ) lowerCAmelCase__ = [text_inputs] return inputs def UpperCAmelCase ( self , __UpperCAmelCase )-> int: '''simple docstring''' lowerCAmelCase__ = model_inputs.pop("candidate_labels" ) lowerCAmelCase__ = model_inputs.pop("text_inputs" ) if isinstance(text_inputs[0] , __UpperCAmelCase ): lowerCAmelCase__ = text_inputs[0] else: # Batching case. lowerCAmelCase__ = text_inputs[0][0] lowerCAmelCase__ = self.model(**__UpperCAmelCase , **__UpperCAmelCase ) lowerCAmelCase__ = { "candidate_labels": candidate_labels, "logits": outputs.logits_per_image, } return model_outputs def UpperCAmelCase ( self , __UpperCAmelCase )-> Tuple: '''simple docstring''' lowerCAmelCase__ = model_outputs.pop("candidate_labels" ) lowerCAmelCase__ = model_outputs["logits"][0] if self.framework == "pt": lowerCAmelCase__ = logits.softmax(dim=-1 ).squeeze(-1 ) lowerCAmelCase__ = probs.tolist() if not isinstance(__UpperCAmelCase , __UpperCAmelCase ): lowerCAmelCase__ = [scores] elif self.framework == "tf": lowerCAmelCase__ = stable_softmax(__UpperCAmelCase , axis=-1 ) lowerCAmelCase__ = probs.numpy().tolist() else: raise ValueError(F"Unsupported framework: {self.framework}" ) lowerCAmelCase__ = [ {"score": score, "label": candidate_label} for score, candidate_label in sorted(zip(__UpperCAmelCase , __UpperCAmelCase ) , key=lambda __UpperCAmelCase : -x[0] ) ] return result
340
0
'''simple docstring''' def __UpperCamelCase ( _UpperCAmelCase, _UpperCAmelCase ): __UpperCAmelCase : Optional[int] = len(_UpperCAmelCase ) print("The following activities are selected:" ) # The first activity is always selected __UpperCAmelCase : List[Any] = 0 print(_UpperCAmelCase, end="," ) # Consider rest of the activities for j in range(_UpperCAmelCase ): # If this activity has start time greater than # or equal to the finish time of previously # selected activity, then select it if start[j] >= finish[i]: print(_UpperCAmelCase, end="," ) __UpperCAmelCase : Optional[int] = j if __name__ == "__main__": import doctest doctest.testmod() lowerCAmelCase__ : Optional[Any] = [1, 3, 0, 5, 8, 5] lowerCAmelCase__ : int = [2, 4, 6, 7, 9, 9] print_max_activities(start, finish)
366
'''simple docstring''' import warnings from ...utils import logging from .image_processing_imagegpt import ImageGPTImageProcessor lowerCAmelCase__ : Optional[Any] = logging.get_logger(__name__) class SCREAMING_SNAKE_CASE__ ( snake_case__ ): """simple docstring""" def __init__( self : Any , *UpperCAmelCase_ : List[Any] , **UpperCAmelCase_ : Union[str, Any] ): """simple docstring""" warnings.warn( "The class ImageGPTFeatureExtractor is deprecated and will be removed in version 5 of Transformers." " Please use ImageGPTImageProcessor instead." , UpperCAmelCase_ , ) super().__init__(*UpperCAmelCase_ , **UpperCAmelCase_ )
37
0
import unittest from transformers import CamembertTokenizer, CamembertTokenizerFast from transformers.testing_utils import get_tests_dir, require_sentencepiece, require_tokenizers, slow from transformers.utils import is_torch_available from ...test_tokenization_common import TokenizerTesterMixin SCREAMING_SNAKE_CASE_ = get_tests_dir("""fixtures/test_sentencepiece.model""") SCREAMING_SNAKE_CASE_ = get_tests_dir("""fixtures/test_sentencepiece_bpe.model""") SCREAMING_SNAKE_CASE_ = """pt""" if is_torch_available() else """tf""" @require_sentencepiece @require_tokenizers class UpperCamelCase__ ( lowerCAmelCase_ , unittest.TestCase ): '''simple docstring''' __snake_case : Tuple = CamembertTokenizer __snake_case : Tuple = CamembertTokenizerFast __snake_case : int = True __snake_case : Union[str, Any] = True def SCREAMING_SNAKE_CASE__ ( self : Dict ) -> Optional[Any]: '''simple docstring''' super().setUp() # We have a SentencePiece fixture for testing SCREAMING_SNAKE_CASE = CamembertTokenizer(lowerCamelCase__ ) tokenizer.save_pretrained(self.tmpdirname ) def SCREAMING_SNAKE_CASE__ ( self : List[str] ) -> str: '''simple docstring''' SCREAMING_SNAKE_CASE = """<pad>""" SCREAMING_SNAKE_CASE = 1 self.assertEqual(self.get_tokenizer()._convert_token_to_id(lowerCamelCase__ ) ,lowerCamelCase__ ) self.assertEqual(self.get_tokenizer()._convert_id_to_token(lowerCamelCase__ ) ,lowerCamelCase__ ) def SCREAMING_SNAKE_CASE__ ( self : List[str] ) -> Dict: '''simple docstring''' SCREAMING_SNAKE_CASE = list(self.get_tokenizer().get_vocab().keys() ) self.assertEqual(vocab_keys[0] ,"""<s>NOTUSED""" ) self.assertEqual(vocab_keys[1] ,"""<pad>""" ) self.assertEqual(vocab_keys[-1] ,"""<mask>""" ) self.assertEqual(len(lowerCamelCase__ ) ,1004 ) def SCREAMING_SNAKE_CASE__ ( self : Optional[Any] ) -> List[Any]: '''simple docstring''' self.assertEqual(self.get_tokenizer().vocab_size ,1005 ) def SCREAMING_SNAKE_CASE__ ( self : Dict ) -> Dict: '''simple docstring''' SCREAMING_SNAKE_CASE = CamembertTokenizer(lowerCamelCase__ ) tokenizer.save_pretrained(self.tmpdirname ) SCREAMING_SNAKE_CASE = CamembertTokenizerFast.from_pretrained(self.tmpdirname ) SCREAMING_SNAKE_CASE = """I was born in 92000, and this is falsé.""" SCREAMING_SNAKE_CASE = tokenizer.encode(lowerCamelCase__ ) SCREAMING_SNAKE_CASE = rust_tokenizer.encode(lowerCamelCase__ ) self.assertListEqual(lowerCamelCase__ ,lowerCamelCase__ ) SCREAMING_SNAKE_CASE = tokenizer.encode(lowerCamelCase__ ,add_special_tokens=lowerCamelCase__ ) SCREAMING_SNAKE_CASE = rust_tokenizer.encode(lowerCamelCase__ ,add_special_tokens=lowerCamelCase__ ) self.assertListEqual(lowerCamelCase__ ,lowerCamelCase__ ) # <unk> tokens are not the same for `rust` than for `slow`. # Because spm gives back raw token instead of `unk` in EncodeAsPieces # tokens = tokenizer.tokenize(sequence) SCREAMING_SNAKE_CASE = tokenizer.convert_ids_to_tokens(lowerCamelCase__ ) SCREAMING_SNAKE_CASE = rust_tokenizer.tokenize(lowerCamelCase__ ) self.assertListEqual(lowerCamelCase__ ,lowerCamelCase__ ) def SCREAMING_SNAKE_CASE__ ( self : Optional[Any] ) -> Optional[int]: '''simple docstring''' 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(lowerCamelCase__ ) SCREAMING_SNAKE_CASE = rust_tokenizer.tokenize(lowerCamelCase__ ) self.assertListEqual(lowerCamelCase__ ,lowerCamelCase__ ) SCREAMING_SNAKE_CASE = tokenizer.encode(lowerCamelCase__ ,add_special_tokens=lowerCamelCase__ ) SCREAMING_SNAKE_CASE = rust_tokenizer.encode(lowerCamelCase__ ,add_special_tokens=lowerCamelCase__ ) self.assertListEqual(lowerCamelCase__ ,lowerCamelCase__ ) SCREAMING_SNAKE_CASE = self.get_rust_tokenizer() SCREAMING_SNAKE_CASE = tokenizer.encode(lowerCamelCase__ ) SCREAMING_SNAKE_CASE = rust_tokenizer.encode(lowerCamelCase__ ) self.assertListEqual(lowerCamelCase__ ,lowerCamelCase__ ) @slow def SCREAMING_SNAKE_CASE__ ( self : Union[str, Any] ) -> Union[str, Any]: '''simple docstring''' SCREAMING_SNAKE_CASE = {"""input_ids""": [[5, 54, 7196, 297, 30, 23, 776, 18, 11, 3215, 3705, 8252, 22, 3164, 1181, 2116, 29, 16, 813, 25, 791, 3314, 20, 3446, 38, 27575, 120, 6, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [5, 468, 17, 11, 9088, 20, 1517, 8, 22804, 18818, 10, 38, 629, 607, 607, 142, 19, 7196, 867, 56, 10326, 24, 2267, 20, 416, 5072, 15612, 233, 734, 7, 2399, 27, 16, 3015, 1649, 7, 24, 20, 4338, 2399, 27, 13, 3400, 14, 13, 6189, 8, 930, 9, 6]], """attention_mask""": [[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]} # noqa: E501 # fmt: on # camembert is a french model. So we also use french texts. SCREAMING_SNAKE_CASE = [ """Le transformeur est un modèle d'apprentissage profond introduit en 2017, """ """utilisé principalement dans le domaine du traitement automatique des langues (TAL).""", """À l'instar des réseaux de neurones récurrents (RNN), les transformeurs sont conçus """ """pour gérer des données séquentielles, telles que le langage naturel, pour des tâches """ """telles que la traduction et la synthèse de texte.""", ] self.tokenizer_integration_test_util( expected_encoding=lowerCamelCase__ ,model_name="""camembert-base""" ,revision="""3a0641d9a1aeb7e848a74299e7e4c4bca216b4cf""" ,sequences=lowerCamelCase__ ,)
296
import os from distutils.util import strtobool def __lowercase ( _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> Tuple: '''simple docstring''' for e in env_keys: SCREAMING_SNAKE_CASE = int(os.environ.get(_SCREAMING_SNAKE_CASE , -1 ) ) if val >= 0: return val return default def __lowercase ( _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE=False ) -> Optional[int]: '''simple docstring''' SCREAMING_SNAKE_CASE = os.environ.get(_SCREAMING_SNAKE_CASE , str(_SCREAMING_SNAKE_CASE ) ) return strtobool(_SCREAMING_SNAKE_CASE ) == 1 # As its name indicates `strtobool` actually returns an int... def __lowercase ( _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE="no" ) -> Any: '''simple docstring''' SCREAMING_SNAKE_CASE = os.environ.get(_SCREAMING_SNAKE_CASE , str(_SCREAMING_SNAKE_CASE ) ) return value
296
1
'''simple docstring''' import argparse import json from dataclasses import dataclass, field from functools import partial from pathlib import Path from typing import Callable, Dict, List, Tuple import timm import torch import torch.nn as nn from classy_vision.models.regnet import RegNet, RegNetParams, RegNetYaagf, RegNetYaagf, RegNetYaaagf from huggingface_hub import cached_download, hf_hub_url from torch import Tensor from vissl.models.model_helpers import get_trunk_forward_outputs from transformers import AutoImageProcessor, RegNetConfig, RegNetForImageClassification, RegNetModel from transformers.utils import logging logging.set_verbosity_info() __a = logging.get_logger() @dataclass class A__ : """simple docstring""" UpperCamelCase_ : nn.Module UpperCamelCase_ : List[nn.Module] = field(default_factory=UpperCamelCase ) UpperCamelCase_ : list = field(default_factory=UpperCamelCase ) def _lowerCAmelCase ( self : Any , lowerCAmelCase__ : List[Any] , lowerCAmelCase__ : Tensor , lowerCAmelCase__ : Tensor ) -> List[Any]: """simple docstring""" _UpperCAmelCase : List[Any] = len(list(m.modules() ) ) == 1 or isinstance(lowerCAmelCase__ , nn.Convad ) or isinstance(lowerCAmelCase__ , nn.BatchNormad ) if has_not_submodules: self.traced.append(lowerCAmelCase__ ) def __call__( self : Optional[int] , lowerCAmelCase__ : Tensor ) -> List[Any]: """simple docstring""" for m in self.module.modules(): self.handles.append(m.register_forward_hook(self._forward_hook ) ) self.module(lowerCAmelCase__ ) [x.remove() for x in self.handles] return self @property def _lowerCAmelCase ( self : Optional[Any] ) -> Optional[int]: """simple docstring""" return list(filter(lambda lowerCAmelCase__ : len(list(x.state_dict().keys() ) ) > 0 , self.traced ) ) @dataclass class A__ : """simple docstring""" UpperCamelCase_ : nn.Module UpperCamelCase_ : nn.Module UpperCamelCase_ : int = 1 UpperCamelCase_ : List = field(default_factory=UpperCamelCase ) UpperCamelCase_ : List = field(default_factory=UpperCamelCase ) UpperCamelCase_ : bool = True def __call__( self : List[str] , lowerCAmelCase__ : Tensor ) -> str: """simple docstring""" _UpperCAmelCase : Optional[Any] = Tracker(self.dest )(lowerCAmelCase__ ).parametrized _UpperCAmelCase : str = Tracker(self.src )(lowerCAmelCase__ ).parametrized _UpperCAmelCase : Optional[int] = list(filter(lambda lowerCAmelCase__ : type(lowerCAmelCase__ ) not in self.src_skip , lowerCAmelCase__ ) ) _UpperCAmelCase : Optional[Any] = list(filter(lambda lowerCAmelCase__ : type(lowerCAmelCase__ ) not in self.dest_skip , lowerCAmelCase__ ) ) if len(lowerCAmelCase__ ) != len(lowerCAmelCase__ ) and self.raise_if_mismatch: raise Exception( F"""Numbers of operations are different. Source module has {len(lowerCAmelCase__ )} operations while""" F""" destination module has {len(lowerCAmelCase__ )}.""" ) for dest_m, src_m in zip(lowerCAmelCase__ , lowerCAmelCase__ ): dest_m.load_state_dict(src_m.state_dict() ) if self.verbose == 1: print(F"""Transfered from={src_m} to={dest_m}""" ) class A__ ( nn.Module ): """simple docstring""" def __init__( self : List[Any] , lowerCAmelCase__ : nn.Module ) -> Dict: """simple docstring""" super().__init__() _UpperCAmelCase : List[Tuple[str, nn.Module]] = [] # - get the stem feature_blocks.append(("conv1", model.stem) ) # - get all the feature blocks for k, v in model.trunk_output.named_children(): assert k.startswith("block" ), F"""Unexpected layer name {k}""" _UpperCAmelCase : Tuple = len(lowerCAmelCase__ ) + 1 feature_blocks.append((F"""res{block_index}""", v) ) _UpperCAmelCase : Optional[Any] = nn.ModuleDict(lowerCAmelCase__ ) def _lowerCAmelCase ( self : List[str] , lowerCAmelCase__ : Tensor ) -> List[str]: """simple docstring""" return get_trunk_forward_outputs( lowerCAmelCase__ , out_feat_keys=lowerCAmelCase__ , feature_blocks=self._feature_blocks , ) class A__ ( UpperCamelCase ): """simple docstring""" def _lowerCAmelCase ( self : str , lowerCAmelCase__ : str ) -> str: """simple docstring""" _UpperCAmelCase : int = x.split("-" ) return x_split[0] + x_split[1] + "_" + "".join(x_split[2:] ) def __getitem__( self : Dict , lowerCAmelCase__ : str ) -> Callable[[], Tuple[nn.Module, Dict]]: """simple docstring""" if x not in self: _UpperCAmelCase : Tuple = self.convert_name_to_timm(lowerCAmelCase__ ) _UpperCAmelCase : List[Any] = partial(lambda: (timm.create_model(lowerCAmelCase__ , pretrained=lowerCAmelCase__ ).eval(), None) ) else: _UpperCAmelCase : List[Any] = super().__getitem__(lowerCAmelCase__ ) return val class A__ ( UpperCamelCase ): """simple docstring""" def __getitem__( self : Optional[int] , lowerCAmelCase__ : str ) -> Callable[[], nn.Module]: """simple docstring""" if "seer" in x and "in1k" not in x: _UpperCAmelCase : Optional[Any] = RegNetModel else: _UpperCAmelCase : Dict = RegNetForImageClassification return val def __UpperCAmelCase ( a_: Tuple, a_: Dict, a_: List[Tuple[str, str]] ): for from_key, to_key in keys: _UpperCAmelCase : Union[str, Any] = from_state_dict[from_key].clone() print(f"""Copied key={from_key} to={to_key}""" ) return to_state_dict def __UpperCAmelCase ( a_: str, a_: Callable[[], nn.Module], a_: Callable[[], nn.Module], a_: RegNetConfig, a_: Path, a_: bool = True, ): print(f"""Converting {name}...""" ) with torch.no_grad(): _UpperCAmelCase : Any = from_model_func() _UpperCAmelCase : int = our_model_func(a_ ).eval() _UpperCAmelCase : List[Any] = ModuleTransfer(src=a_, dest=a_, raise_if_mismatch=a_ ) _UpperCAmelCase : Any = torch.randn((1, 3, 224, 224) ) module_transfer(a_ ) if from_state_dict is not None: _UpperCAmelCase : Optional[Any] = [] # for seer - in1k finetuned we have to manually copy the head if "seer" in name and "in1k" in name: _UpperCAmelCase : Optional[int] = [("0.clf.0.weight", "classifier.1.weight"), ("0.clf.0.bias", "classifier.1.bias")] _UpperCAmelCase : List[Any] = manually_copy_vissl_head(a_, our_model.state_dict(), a_ ) our_model.load_state_dict(a_ ) _UpperCAmelCase : Dict = our_model(a_, output_hidden_states=a_ ) _UpperCAmelCase : Optional[int] = ( our_outputs.logits if isinstance(a_, a_ ) else our_outputs.last_hidden_state ) _UpperCAmelCase : str = from_model(a_ ) _UpperCAmelCase : List[Any] = from_output[-1] if type(a_ ) is list else from_output # now since I don't want to use any config files, vissl seer model doesn't actually have an head, so let's just check the last hidden state if "seer" in name and "in1k" in name: _UpperCAmelCase : List[str] = our_outputs.hidden_states[-1] assert torch.allclose(a_, a_ ), "The model logits don't match the original one." if push_to_hub: our_model.push_to_hub( repo_path_or_name=save_directory / name, commit_message="Add model", use_temp_dir=a_, ) _UpperCAmelCase : Optional[Any] = 224 if "seer" not in name else 384 # we can use the convnext one _UpperCAmelCase : Optional[int] = AutoImageProcessor.from_pretrained("facebook/convnext-base-224-22k-1k", size=a_ ) image_processor.push_to_hub( repo_path_or_name=save_directory / name, commit_message="Add image processor", use_temp_dir=a_, ) print(f"""Pushed {name}""" ) def __UpperCAmelCase ( a_: Path, a_: str = None, a_: bool = True ): _UpperCAmelCase : Union[str, Any] = "imagenet-1k-id2label.json" _UpperCAmelCase : Any = 1_000 _UpperCAmelCase : int = (1, num_labels) _UpperCAmelCase : Any = "huggingface/label-files" _UpperCAmelCase : str = num_labels _UpperCAmelCase : List[Any] = json.load(open(cached_download(hf_hub_url(a_, a_, repo_type="dataset" ) ), "r" ) ) _UpperCAmelCase : Union[str, Any] = {int(a_ ): v for k, v in idalabel.items()} _UpperCAmelCase : Tuple = idalabel _UpperCAmelCase : List[Any] = {v: k for k, v in idalabel.items()} _UpperCAmelCase : str = partial(a_, num_labels=a_, idalabel=a_, labelaid=a_ ) _UpperCAmelCase : Union[str, Any] = { "regnet-x-002": ImageNetPreTrainedConfig( depths=[1, 1, 4, 7], hidden_sizes=[24, 56, 152, 368], groups_width=8, layer_type="x" ), "regnet-x-004": ImageNetPreTrainedConfig( depths=[1, 2, 7, 12], hidden_sizes=[32, 64, 160, 384], groups_width=16, layer_type="x" ), "regnet-x-006": ImageNetPreTrainedConfig( depths=[1, 3, 5, 7], hidden_sizes=[48, 96, 240, 528], groups_width=24, layer_type="x" ), "regnet-x-008": ImageNetPreTrainedConfig( depths=[1, 3, 7, 5], hidden_sizes=[64, 128, 288, 672], groups_width=16, layer_type="x" ), "regnet-x-016": ImageNetPreTrainedConfig( depths=[2, 4, 10, 2], hidden_sizes=[72, 168, 408, 912], groups_width=24, layer_type="x" ), "regnet-x-032": ImageNetPreTrainedConfig( depths=[2, 6, 15, 2], hidden_sizes=[96, 192, 432, 1_008], groups_width=48, layer_type="x" ), "regnet-x-040": ImageNetPreTrainedConfig( depths=[2, 5, 14, 2], hidden_sizes=[80, 240, 560, 1_360], groups_width=40, layer_type="x" ), "regnet-x-064": ImageNetPreTrainedConfig( depths=[2, 4, 10, 1], hidden_sizes=[168, 392, 784, 1_624], groups_width=56, layer_type="x" ), "regnet-x-080": ImageNetPreTrainedConfig( depths=[2, 5, 15, 1], hidden_sizes=[80, 240, 720, 1_920], groups_width=120, layer_type="x" ), "regnet-x-120": ImageNetPreTrainedConfig( depths=[2, 5, 11, 1], hidden_sizes=[224, 448, 896, 2_240], groups_width=112, layer_type="x" ), "regnet-x-160": ImageNetPreTrainedConfig( depths=[2, 6, 13, 1], hidden_sizes=[256, 512, 896, 2_048], groups_width=128, layer_type="x" ), "regnet-x-320": ImageNetPreTrainedConfig( depths=[2, 7, 13, 1], hidden_sizes=[336, 672, 1_344, 2_520], groups_width=168, layer_type="x" ), # y variant "regnet-y-002": ImageNetPreTrainedConfig(depths=[1, 1, 4, 7], hidden_sizes=[24, 56, 152, 368], groups_width=8 ), "regnet-y-004": ImageNetPreTrainedConfig( depths=[1, 3, 6, 6], hidden_sizes=[48, 104, 208, 440], groups_width=8 ), "regnet-y-006": ImageNetPreTrainedConfig( depths=[1, 3, 7, 4], hidden_sizes=[48, 112, 256, 608], groups_width=16 ), "regnet-y-008": ImageNetPreTrainedConfig( depths=[1, 3, 8, 2], hidden_sizes=[64, 128, 320, 768], groups_width=16 ), "regnet-y-016": ImageNetPreTrainedConfig( depths=[2, 6, 17, 2], hidden_sizes=[48, 120, 336, 888], groups_width=24 ), "regnet-y-032": ImageNetPreTrainedConfig( depths=[2, 5, 13, 1], hidden_sizes=[72, 216, 576, 1_512], groups_width=24 ), "regnet-y-040": ImageNetPreTrainedConfig( depths=[2, 6, 12, 2], hidden_sizes=[128, 192, 512, 1_088], groups_width=64 ), "regnet-y-064": ImageNetPreTrainedConfig( depths=[2, 7, 14, 2], hidden_sizes=[144, 288, 576, 1_296], groups_width=72 ), "regnet-y-080": ImageNetPreTrainedConfig( depths=[2, 4, 10, 1], hidden_sizes=[168, 448, 896, 2_016], groups_width=56 ), "regnet-y-120": ImageNetPreTrainedConfig( depths=[2, 5, 11, 1], hidden_sizes=[224, 448, 896, 2_240], groups_width=112 ), "regnet-y-160": ImageNetPreTrainedConfig( depths=[2, 4, 11, 1], hidden_sizes=[224, 448, 1_232, 3_024], groups_width=112 ), "regnet-y-320": ImageNetPreTrainedConfig( depths=[2, 5, 12, 1], hidden_sizes=[232, 696, 1_392, 3_712], groups_width=232 ), # models created by SEER -> https://arxiv.org/abs/2202.08360 "regnet-y-320-seer": RegNetConfig(depths=[2, 5, 12, 1], hidden_sizes=[232, 696, 1_392, 3_712], groups_width=232 ), "regnet-y-640-seer": RegNetConfig(depths=[2, 5, 12, 1], hidden_sizes=[328, 984, 1_968, 4_920], groups_width=328 ), "regnet-y-1280-seer": RegNetConfig( depths=[2, 7, 17, 1], hidden_sizes=[528, 1_056, 2_904, 7_392], groups_width=264 ), "regnet-y-2560-seer": RegNetConfig( depths=[3, 7, 16, 1], hidden_sizes=[640, 1_696, 2_544, 5_088], groups_width=640 ), "regnet-y-10b-seer": ImageNetPreTrainedConfig( depths=[2, 7, 17, 1], hidden_sizes=[2_020, 4_040, 11_110, 28_280], groups_width=1_010 ), # finetuned on imagenet "regnet-y-320-seer-in1k": ImageNetPreTrainedConfig( depths=[2, 5, 12, 1], hidden_sizes=[232, 696, 1_392, 3_712], groups_width=232 ), "regnet-y-640-seer-in1k": ImageNetPreTrainedConfig( depths=[2, 5, 12, 1], hidden_sizes=[328, 984, 1_968, 4_920], groups_width=328 ), "regnet-y-1280-seer-in1k": ImageNetPreTrainedConfig( depths=[2, 7, 17, 1], hidden_sizes=[528, 1_056, 2_904, 7_392], groups_width=264 ), "regnet-y-2560-seer-in1k": ImageNetPreTrainedConfig( depths=[3, 7, 16, 1], hidden_sizes=[640, 1_696, 2_544, 5_088], groups_width=640 ), "regnet-y-10b-seer-in1k": ImageNetPreTrainedConfig( depths=[2, 7, 17, 1], hidden_sizes=[2_020, 4_040, 11_110, 28_280], groups_width=1_010 ), } _UpperCAmelCase : Optional[int] = NameToOurModelFuncMap() _UpperCAmelCase : List[Any] = NameToFromModelFuncMap() # add seer weights logic def load_using_classy_vision(a_: str, a_: Callable[[], nn.Module] ) -> Tuple[nn.Module, Dict]: _UpperCAmelCase : Dict = torch.hub.load_state_dict_from_url(a_, model_dir=str(a_ ), map_location="cpu" ) _UpperCAmelCase : str = model_func() # check if we have a head, if yes add it _UpperCAmelCase : Tuple = files["classy_state_dict"]["base_model"]["model"] _UpperCAmelCase : List[Any] = model_state_dict["trunk"] model.load_state_dict(a_ ) return model.eval(), model_state_dict["heads"] # pretrained _UpperCAmelCase : Optional[Any] = partial( a_, "https://dl.fbaipublicfiles.com/vissl/model_zoo/seer_regnet32d/seer_regnet32gf_model_iteration244000.torch", lambda: FakeRegNetVisslWrapper(RegNetYaagf() ), ) _UpperCAmelCase : str = partial( a_, "https://dl.fbaipublicfiles.com/vissl/model_zoo/seer_regnet64/seer_regnet64gf_model_final_checkpoint_phase0.torch", lambda: FakeRegNetVisslWrapper(RegNetYaagf() ), ) _UpperCAmelCase : int = partial( a_, "https://dl.fbaipublicfiles.com/vissl/model_zoo/swav_ig1b_regnet128Gf_cnstant_bs32_node16_sinkhorn10_proto16k_syncBN64_warmup8k/model_final_checkpoint_phase0.torch", lambda: FakeRegNetVisslWrapper(RegNetYaaagf() ), ) _UpperCAmelCase : Optional[Any] = partial( a_, "https://dl.fbaipublicfiles.com/vissl/model_zoo/seer_regnet10B/model_iteration124500_conso.torch", lambda: FakeRegNetVisslWrapper( RegNet(RegNetParams(depth=27, group_width=1_010, w_a=1_744, w_a=620.83, w_m=2.52 ) ) ), ) # IN1K finetuned _UpperCAmelCase : Optional[Any] = partial( a_, "https://dl.fbaipublicfiles.com/vissl/model_zoo/seer_finetuned/seer_regnet32_finetuned_in1k_model_final_checkpoint_phase78.torch", lambda: FakeRegNetVisslWrapper(RegNetYaagf() ), ) _UpperCAmelCase : Tuple = partial( a_, "https://dl.fbaipublicfiles.com/vissl/model_zoo/seer_finetuned/seer_regnet64_finetuned_in1k_model_final_checkpoint_phase78.torch", lambda: FakeRegNetVisslWrapper(RegNetYaagf() ), ) _UpperCAmelCase : Dict = partial( a_, "https://dl.fbaipublicfiles.com/vissl/model_zoo/seer_finetuned/seer_regnet128_finetuned_in1k_model_final_checkpoint_phase78.torch", lambda: FakeRegNetVisslWrapper(RegNetYaaagf() ), ) _UpperCAmelCase : Dict = partial( a_, "https://dl.fbaipublicfiles.com/vissl/model_zoo/seer_finetuned/seer_10b_finetuned_in1k_model_phase28_conso.torch", lambda: FakeRegNetVisslWrapper( RegNet(RegNetParams(depth=27, group_width=1_010, w_a=1_744, w_a=620.83, w_m=2.52 ) ) ), ) if model_name: convert_weight_and_push( a_, names_to_from_model_map[model_name], names_to_ours_model_map[model_name], names_to_config[model_name], a_, a_, ) else: for model_name, config in names_to_config.items(): convert_weight_and_push( a_, names_to_from_model_map[model_name], names_to_ours_model_map[model_name], a_, a_, a_, ) return config, expected_shape if __name__ == "__main__": __a = argparse.ArgumentParser() # Required parameters parser.add_argument( '--model_name', default=None, type=str, help=( 'The name of the model you wish to convert, it must be one of the supported regnet* architecture,' ' currently: regnetx-*, regnety-*. If `None`, all of them will the converted.' ), ) parser.add_argument( '--pytorch_dump_folder_path', default=None, type=Path, required=True, help='Path to the output PyTorch model directory.', ) parser.add_argument( '--push_to_hub', default=True, type=bool, required=False, help='If True, push model and image processor to the hub.', ) __a = parser.parse_args() __a = args.pytorch_dump_folder_path pytorch_dump_folder_path.mkdir(exist_ok=True, parents=True) convert_weights_and_push(pytorch_dump_folder_path, args.model_name, args.push_to_hub)
361
'''simple docstring''' import itertools from dataclasses import dataclass from typing import Any, Callable, Dict, List, Optional, Union import pandas as pd import pyarrow as pa import datasets import datasets.config from datasets.features.features import require_storage_cast from datasets.table import table_cast from datasets.utils.py_utils import Literal __a = datasets.utils.logging.get_logger(__name__) __a = ['names', 'prefix'] __a = ['warn_bad_lines', 'error_bad_lines', 'mangle_dupe_cols'] __a = ['encoding_errors', 'on_bad_lines'] __a = ['date_format'] @dataclass class A__ ( datasets.BuilderConfig ): """simple docstring""" UpperCamelCase_ : str = "," UpperCamelCase_ : Optional[str] = None UpperCamelCase_ : Optional[Union[int, List[int], str]] = "infer" UpperCamelCase_ : Optional[List[str]] = None UpperCamelCase_ : Optional[List[str]] = None UpperCamelCase_ : Optional[Union[int, str, List[int], List[str]]] = None UpperCamelCase_ : Optional[Union[List[int], List[str]]] = None UpperCamelCase_ : Optional[str] = None UpperCamelCase_ : bool = True UpperCamelCase_ : Optional[Literal["c", "python", "pyarrow"]] = None UpperCamelCase_ : Dict[Union[int, str], Callable[[Any], Any]] = None UpperCamelCase_ : Optional[list] = None UpperCamelCase_ : Optional[list] = None UpperCamelCase_ : bool = False UpperCamelCase_ : Optional[Union[int, List[int]]] = None UpperCamelCase_ : Optional[int] = None UpperCamelCase_ : Optional[Union[str, List[str]]] = None UpperCamelCase_ : bool = True UpperCamelCase_ : bool = True UpperCamelCase_ : bool = False UpperCamelCase_ : bool = True UpperCamelCase_ : Optional[str] = None UpperCamelCase_ : str = "." UpperCamelCase_ : Optional[str] = None UpperCamelCase_ : str = '"' UpperCamelCase_ : int = 0 UpperCamelCase_ : Optional[str] = None UpperCamelCase_ : Optional[str] = None UpperCamelCase_ : Optional[str] = None UpperCamelCase_ : Optional[str] = None UpperCamelCase_ : bool = True UpperCamelCase_ : bool = True UpperCamelCase_ : int = 0 UpperCamelCase_ : bool = True UpperCamelCase_ : bool = False UpperCamelCase_ : Optional[str] = None UpperCamelCase_ : int = 1_00_00 UpperCamelCase_ : Optional[datasets.Features] = None UpperCamelCase_ : Optional[str] = "strict" UpperCamelCase_ : Literal["error", "warn", "skip"] = "error" UpperCamelCase_ : Optional[str] = None def _lowerCAmelCase ( self : str ) -> Tuple: """simple docstring""" if self.delimiter is not None: _UpperCAmelCase : Any = self.delimiter if self.column_names is not None: _UpperCAmelCase : List[Any] = self.column_names @property def _lowerCAmelCase ( self : Optional[int] ) -> Optional[int]: """simple docstring""" _UpperCAmelCase : Dict = { "sep": self.sep, "header": self.header, "names": self.names, "index_col": self.index_col, "usecols": self.usecols, "prefix": self.prefix, "mangle_dupe_cols": self.mangle_dupe_cols, "engine": self.engine, "converters": self.converters, "true_values": self.true_values, "false_values": self.false_values, "skipinitialspace": self.skipinitialspace, "skiprows": self.skiprows, "nrows": self.nrows, "na_values": self.na_values, "keep_default_na": self.keep_default_na, "na_filter": self.na_filter, "verbose": self.verbose, "skip_blank_lines": self.skip_blank_lines, "thousands": self.thousands, "decimal": self.decimal, "lineterminator": self.lineterminator, "quotechar": self.quotechar, "quoting": self.quoting, "escapechar": self.escapechar, "comment": self.comment, "encoding": self.encoding, "dialect": self.dialect, "error_bad_lines": self.error_bad_lines, "warn_bad_lines": self.warn_bad_lines, "skipfooter": self.skipfooter, "doublequote": self.doublequote, "memory_map": self.memory_map, "float_precision": self.float_precision, "chunksize": self.chunksize, "encoding_errors": self.encoding_errors, "on_bad_lines": self.on_bad_lines, "date_format": self.date_format, } # some kwargs must not be passed if they don't have a default value # some others are deprecated and we can also not pass them if they are the default value for pd_read_csv_parameter in _PANDAS_READ_CSV_NO_DEFAULT_PARAMETERS + _PANDAS_READ_CSV_DEPRECATED_PARAMETERS: if pd_read_csv_kwargs[pd_read_csv_parameter] == getattr(CsvConfig() , lowerCAmelCase__ ): del pd_read_csv_kwargs[pd_read_csv_parameter] # Remove 2.0 new arguments if not (datasets.config.PANDAS_VERSION.major >= 2): for pd_read_csv_parameter in _PANDAS_READ_CSV_NEW_2_0_0_PARAMETERS: del pd_read_csv_kwargs[pd_read_csv_parameter] # Remove 1.3 new arguments if not (datasets.config.PANDAS_VERSION.major >= 1 and datasets.config.PANDAS_VERSION.minor >= 3): for pd_read_csv_parameter in _PANDAS_READ_CSV_NEW_1_3_0_PARAMETERS: del pd_read_csv_kwargs[pd_read_csv_parameter] return pd_read_csv_kwargs class A__ ( datasets.ArrowBasedBuilder ): """simple docstring""" UpperCamelCase_ : int = CsvConfig def _lowerCAmelCase ( self : Optional[Any] ) -> Optional[int]: """simple docstring""" return datasets.DatasetInfo(features=self.config.features ) def _lowerCAmelCase ( self : Tuple , lowerCAmelCase__ : str ) -> List[str]: """simple docstring""" 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}""" ) _UpperCAmelCase : List[str] = dl_manager.download_and_extract(self.config.data_files ) if isinstance(lowerCAmelCase__ , (str, list, tuple) ): _UpperCAmelCase : int = data_files if isinstance(lowerCAmelCase__ , lowerCAmelCase__ ): _UpperCAmelCase : Any = [files] _UpperCAmelCase : List[Any] = [dl_manager.iter_files(lowerCAmelCase__ ) for file in files] return [datasets.SplitGenerator(name=datasets.Split.TRAIN , gen_kwargs={"files": files} )] _UpperCAmelCase : Optional[Any] = [] for split_name, files in data_files.items(): if isinstance(lowerCAmelCase__ , lowerCAmelCase__ ): _UpperCAmelCase : str = [files] _UpperCAmelCase : Any = [dl_manager.iter_files(lowerCAmelCase__ ) for file in files] splits.append(datasets.SplitGenerator(name=lowerCAmelCase__ , gen_kwargs={"files": files} ) ) return splits def _lowerCAmelCase ( self : List[Any] , lowerCAmelCase__ : pa.Table ) -> pa.Table: """simple docstring""" if self.config.features is not None: _UpperCAmelCase : Tuple = self.config.features.arrow_schema if all(not require_storage_cast(lowerCAmelCase__ ) for feature in self.config.features.values() ): # cheaper cast _UpperCAmelCase : Any = pa.Table.from_arrays([pa_table[field.name] for field in schema] , schema=lowerCAmelCase__ ) else: # more expensive cast; allows str <-> int/float or str to Audio for example _UpperCAmelCase : int = table_cast(lowerCAmelCase__ , lowerCAmelCase__ ) return pa_table def _lowerCAmelCase ( self : Dict , lowerCAmelCase__ : Dict ) -> Dict: """simple docstring""" _UpperCAmelCase : int = self.config.features.arrow_schema if self.config.features else None # dtype allows reading an int column as str _UpperCAmelCase : Optional[Any] = ( { name: dtype.to_pandas_dtype() if not require_storage_cast(lowerCAmelCase__ ) else object for name, dtype, feature in zip(schema.names , schema.types , self.config.features.values() ) } if schema is not None else None ) for file_idx, file in enumerate(itertools.chain.from_iterable(lowerCAmelCase__ ) ): _UpperCAmelCase : Optional[Any] = pd.read_csv(lowerCAmelCase__ , iterator=lowerCAmelCase__ , dtype=lowerCAmelCase__ , **self.config.pd_read_csv_kwargs ) try: for batch_idx, df in enumerate(lowerCAmelCase__ ): _UpperCAmelCase : Optional[int] = pa.Table.from_pandas(lowerCAmelCase__ ) # 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 (file_idx, batch_idx), self._cast_table(lowerCAmelCase__ ) except ValueError as e: logger.error(F"""Failed to read file '{file}' with error {type(lowerCAmelCase__ )}: {e}""" ) raise
17
0
def _a ( SCREAMING_SNAKE_CASE ): """simple docstring""" lowercase__ = abs(SCREAMING_SNAKE_CASE ) lowercase__ = 0 while n > 0: res += n % 10 n //= 10 return res def _a ( SCREAMING_SNAKE_CASE ): """simple docstring""" lowercase__ = abs(SCREAMING_SNAKE_CASE ) return n if n < 10 else n % 10 + sum_of_digits(n // 10 ) def _a ( SCREAMING_SNAKE_CASE ): """simple docstring""" return sum(int(SCREAMING_SNAKE_CASE ) for c in str(abs(SCREAMING_SNAKE_CASE ) ) ) def _a ( ): """simple docstring""" from collections.abc import Callable from timeit import timeit def benchmark_a_function(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ) -> None: lowercase__ = f'{func.__name__}({value})' lowercase__ = timeit(f'__main__.{call}' , setup='''import __main__''' ) print(f'{call:56} = {func(SCREAMING_SNAKE_CASE )} -- {timing:.4f} seconds' ) for value in (26_21_44, 11_25_89_99_06_84_26_24, 1_26_76_50_60_02_28_22_94_01_49_67_03_20_53_76): for func in (sum_of_digits, sum_of_digits_recursion, sum_of_digits_compact): benchmark_a_function(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ) print() if __name__ == "__main__": import doctest doctest.testmod() benchmark()
110
import argparse from collections import defaultdict import yaml _snake_case = "docs/source/en/_toctree.yml" def A ( _lowerCamelCase ): '''simple docstring''' _lowerCAmelCase : Dict = defaultdict(_lowerCamelCase ) _lowerCAmelCase : Any = [] _lowerCAmelCase : List[str] = [] for doc in doc_list: if "local" in doc: counts[doc["local"]] += 1 if doc["title"].lower() == "overview": overview_doc.append({"local": doc["local"], "title": doc["title"]} ) else: new_doc_list.append(_lowerCamelCase ) _lowerCAmelCase : Optional[Any] = new_doc_list _lowerCAmelCase : List[Any] = [key for key, value in counts.items() if value > 1] _lowerCAmelCase : str = [] for duplicate_key in duplicates: _lowerCAmelCase : List[str] = list({doc["title"] for doc in doc_list if doc["local"] == duplicate_key} ) if len(_lowerCamelCase ) > 1: raise ValueError( F"{duplicate_key} is present several times in the documentation table of content at " "`docs/source/en/_toctree.yml` with different *Title* values. Choose one of those and remove the " "others." ) # Only add this once new_doc.append({"local": duplicate_key, "title": titles[0]} ) # Add none duplicate-keys new_doc.extend([doc for doc in doc_list if "local" not in counts or counts[doc["local"]] == 1] ) _lowerCAmelCase : Optional[Any] = sorted(_lowerCamelCase , key=lambda _lowerCamelCase : s["title"].lower() ) # "overview" gets special treatment and is always first if len(_lowerCamelCase ) > 1: raise ValueError("{doc_list} has two 'overview' docs which is not allowed." ) overview_doc.extend(_lowerCamelCase ) # Sort return overview_doc def A ( _lowerCamelCase=False ): '''simple docstring''' with open(_lowerCamelCase , encoding="utf-8" ) as f: _lowerCAmelCase : int = yaml.safe_load(f.read() ) # Get to the API doc _lowerCAmelCase : Optional[Any] = 0 while content[api_idx]["title"] != "API": api_idx += 1 _lowerCAmelCase : List[str] = content[api_idx]["sections"] # Then to the model doc _lowerCAmelCase : Union[str, Any] = 0 while api_doc[scheduler_idx]["title"] != "Schedulers": scheduler_idx += 1 _lowerCAmelCase : Optional[Any] = api_doc[scheduler_idx]["sections"] _lowerCAmelCase : Optional[Any] = clean_doc_toc(_lowerCamelCase ) _lowerCAmelCase : int = False if new_scheduler_doc != scheduler_doc: _lowerCAmelCase : List[Any] = True if overwrite: _lowerCAmelCase : Dict = new_scheduler_doc if diff: if overwrite: _lowerCAmelCase : Tuple = api_doc with open(_lowerCamelCase , "w" , encoding="utf-8" ) as f: f.write(yaml.dump(_lowerCamelCase , allow_unicode=_lowerCamelCase ) ) else: raise ValueError( "The model doc part of the table of content is not properly sorted, run `make style` to fix this." ) def A ( _lowerCamelCase=False ): '''simple docstring''' with open(_lowerCamelCase , encoding="utf-8" ) as f: _lowerCAmelCase : Tuple = yaml.safe_load(f.read() ) # Get to the API doc _lowerCAmelCase : Optional[int] = 0 while content[api_idx]["title"] != "API": api_idx += 1 _lowerCAmelCase : int = content[api_idx]["sections"] # Then to the model doc _lowerCAmelCase : List[str] = 0 while api_doc[pipeline_idx]["title"] != "Pipelines": pipeline_idx += 1 _lowerCAmelCase : Dict = False _lowerCAmelCase : Optional[int] = api_doc[pipeline_idx]["sections"] _lowerCAmelCase : Tuple = [] # sort sub pipeline docs for pipeline_doc in pipeline_docs: if "section" in pipeline_doc: _lowerCAmelCase : List[Any] = pipeline_doc["section"] _lowerCAmelCase : Union[str, Any] = clean_doc_toc(_lowerCamelCase ) if overwrite: _lowerCAmelCase : Optional[Any] = new_sub_pipeline_doc new_pipeline_docs.append(_lowerCamelCase ) # sort overall pipeline doc _lowerCAmelCase : Union[str, Any] = clean_doc_toc(_lowerCamelCase ) if new_pipeline_docs != pipeline_docs: _lowerCAmelCase : Dict = True if overwrite: _lowerCAmelCase : Optional[int] = new_pipeline_docs if diff: if overwrite: _lowerCAmelCase : Optional[int] = api_doc with open(_lowerCamelCase , "w" , encoding="utf-8" ) as f: f.write(yaml.dump(_lowerCamelCase , allow_unicode=_lowerCamelCase ) ) else: raise ValueError( "The model doc part of the table of content is not properly sorted, run `make style` to fix this." ) if __name__ == "__main__": _snake_case = argparse.ArgumentParser() parser.add_argument("--fix_and_overwrite", action="store_true", help="Whether to fix inconsistencies.") _snake_case = parser.parse_args() check_scheduler_doc(args.fix_and_overwrite) check_pipeline_doc(args.fix_and_overwrite)
36
0
"""simple docstring""" from __future__ import annotations import copy import inspect import unittest import numpy as np from transformers import is_tf_available, is_vision_available from transformers.models.auto import get_values from transformers.testing_utils import require_tf, slow from transformers.utils import cached_property from ...test_configuration_common import ConfigTester from ...test_modeling_tf_common import TFModelTesterMixin, floats_tensor, ids_tensor, random_attention_mask from ...test_pipeline_mixin import PipelineTesterMixin if is_tf_available(): import tensorflow as tf from transformers import ( TF_LAYOUTLMV3_PRETRAINED_MODEL_ARCHIVE_LIST, TF_MODEL_FOR_MULTIPLE_CHOICE_MAPPING, TF_MODEL_FOR_QUESTION_ANSWERING_MAPPING, TF_MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING, TF_MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING, LayoutLMvaConfig, TFLayoutLMvaForQuestionAnswering, TFLayoutLMvaForSequenceClassification, TFLayoutLMvaForTokenClassification, TFLayoutLMvaModel, ) if is_vision_available(): from PIL import Image from transformers import LayoutLMvaImageProcessor class __lowerCamelCase : '''simple docstring''' def __init__( self , __UpperCAmelCase , __UpperCAmelCase=2 , __UpperCAmelCase=3 , __UpperCAmelCase=4 , __UpperCAmelCase=2 , __UpperCAmelCase=7 , __UpperCAmelCase=True , __UpperCAmelCase=True , __UpperCAmelCase=True , __UpperCAmelCase=True , __UpperCAmelCase=99 , __UpperCAmelCase=36 , __UpperCAmelCase=2 , __UpperCAmelCase=4 , __UpperCAmelCase=37 , __UpperCAmelCase="gelu" , __UpperCAmelCase=0.1 , __UpperCAmelCase=0.1 , __UpperCAmelCase=512 , __UpperCAmelCase=16 , __UpperCAmelCase=2 , __UpperCAmelCase=0.02 , __UpperCAmelCase=6 , __UpperCAmelCase=6 , __UpperCAmelCase=3 , __UpperCAmelCase=4 , __UpperCAmelCase=None , __UpperCAmelCase=1000 , ) -> int: _a = parent _a = batch_size _a = num_channels _a = image_size _a = patch_size _a = is_training _a = use_input_mask _a = use_token_type_ids _a = use_labels _a = vocab_size _a = hidden_size _a = num_hidden_layers _a = num_attention_heads _a = intermediate_size _a = hidden_act _a = hidden_dropout_prob _a = attention_probs_dropout_prob _a = max_position_embeddings _a = type_vocab_size _a = type_sequence_label_size _a = initializer_range _a = coordinate_size _a = shape_size _a = num_labels _a = num_choices _a = scope _a = range_bbox # LayoutLMv3's sequence length equals the number of text tokens + number of patches + 1 (we add 1 for the CLS token) _a = text_seq_length _a = (image_size // patch_size) ** 2 + 1 _a = self.text_seq_length + self.image_seq_length def _UpperCAmelCase ( self ) -> Tuple: _a = ids_tensor([self.batch_size, self.text_seq_length] , self.vocab_size ) _a = ids_tensor([self.batch_size, self.text_seq_length, 4] , self.range_bbox ) _a = bbox.numpy() # Ensure that bbox is legal for i in range(bbox.shape[0] ): for j in range(bbox.shape[1] ): if bbox[i, j, 3] < bbox[i, j, 1]: _a = bbox[i, j, 3] _a = bbox[i, j, 1] _a = tmp_coordinate if bbox[i, j, 2] < bbox[i, j, 0]: _a = bbox[i, j, 2] _a = bbox[i, j, 0] _a = tmp_coordinate _a = tf.constant(_snake_case ) _a = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) _a = None if self.use_input_mask: _a = random_attention_mask([self.batch_size, self.text_seq_length] ) _a = None if self.use_token_type_ids: _a = ids_tensor([self.batch_size, self.text_seq_length] , self.type_vocab_size ) _a = None _a = None if self.use_labels: _a = ids_tensor([self.batch_size] , self.type_sequence_label_size ) _a = ids_tensor([self.batch_size, self.text_seq_length] , self.num_labels ) _a = LayoutLMvaConfig( vocab_size=self.vocab_size , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , max_position_embeddings=self.max_position_embeddings , type_vocab_size=self.type_vocab_size , initializer_range=self.initializer_range , coordinate_size=self.coordinate_size , shape_size=self.shape_size , input_size=self.image_size , patch_size=self.patch_size , ) return config, input_ids, bbox, pixel_values, token_type_ids, input_mask, sequence_labels, token_labels def _UpperCAmelCase ( self , __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase ) -> List[Any]: _a = TFLayoutLMvaModel(config=_snake_case ) # text + image _a = model(_snake_case , pixel_values=_snake_case , training=_snake_case ) _a = model( _snake_case , bbox=_snake_case , pixel_values=_snake_case , attention_mask=_snake_case , token_type_ids=_snake_case , training=_snake_case , ) _a = model(_snake_case , bbox=_snake_case , pixel_values=_snake_case , training=_snake_case ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) # text only _a = model(_snake_case , training=_snake_case ) self.parent.assertEqual( result.last_hidden_state.shape , (self.batch_size, self.text_seq_length, self.hidden_size) ) # image only _a = model({'''pixel_values''': pixel_values} , training=_snake_case ) self.parent.assertEqual( result.last_hidden_state.shape , (self.batch_size, self.image_seq_length, self.hidden_size) ) def _UpperCAmelCase ( self , __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase ) -> Optional[Any]: _a = self.num_labels _a = TFLayoutLMvaForSequenceClassification(config=_snake_case ) _a = model( _snake_case , bbox=_snake_case , pixel_values=_snake_case , attention_mask=_snake_case , token_type_ids=_snake_case , labels=_snake_case , training=_snake_case , ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_labels) ) def _UpperCAmelCase ( self , __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase ) -> Tuple: _a = self.num_labels _a = TFLayoutLMvaForTokenClassification(config=_snake_case ) _a = model( _snake_case , bbox=_snake_case , pixel_values=_snake_case , attention_mask=_snake_case , token_type_ids=_snake_case , labels=_snake_case , training=_snake_case , ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.text_seq_length, self.num_labels) ) def _UpperCAmelCase ( self , __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase ) -> Any: _a = 2 _a = TFLayoutLMvaForQuestionAnswering(config=_snake_case ) _a = model( _snake_case , bbox=_snake_case , pixel_values=_snake_case , attention_mask=_snake_case , token_type_ids=_snake_case , start_positions=_snake_case , end_positions=_snake_case , training=_snake_case , ) self.parent.assertEqual(result.start_logits.shape , (self.batch_size, self.seq_length) ) self.parent.assertEqual(result.end_logits.shape , (self.batch_size, self.seq_length) ) def _UpperCAmelCase ( self ) -> Optional[int]: _a = self.prepare_config_and_inputs() ((_a) , (_a) , (_a) , (_a) , (_a) , (_a) , (_a) , (_a)) = config_and_inputs _a = { '''input_ids''': input_ids, '''bbox''': bbox, '''pixel_values''': pixel_values, '''token_type_ids''': token_type_ids, '''attention_mask''': input_mask, } return config, inputs_dict @require_tf class __lowerCamelCase ( a__ , a__ , unittest.TestCase ): '''simple docstring''' A_ : List[Any] = ( ( TFLayoutLMvaModel, TFLayoutLMvaForQuestionAnswering, TFLayoutLMvaForSequenceClassification, TFLayoutLMvaForTokenClassification, ) if is_tf_available() else () ) A_ : Optional[int] = ( {'document-question-answering': TFLayoutLMvaForQuestionAnswering, 'feature-extraction': TFLayoutLMvaModel} if is_tf_available() else {} ) A_ : Union[str, Any] = False A_ : Tuple = False A_ : List[Any] = False def _UpperCAmelCase ( self , __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase ) -> int: return True def _UpperCAmelCase ( self , __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase=False ) -> dict: _a = copy.deepcopy(_snake_case ) if model_class in get_values(_snake_case ): _a = { k: tf.tile(tf.expand_dims(_snake_case , 1 ) , (1, self.model_tester.num_choices) + (1,) * (v.ndim - 1) ) if isinstance(_snake_case , tf.Tensor ) and v.ndim > 0 else v for k, v in inputs_dict.items() } if return_labels: if model_class in get_values(_snake_case ): _a = tf.ones(self.model_tester.batch_size , dtype=tf.intaa ) elif model_class in get_values(_snake_case ): _a = tf.zeros(self.model_tester.batch_size , dtype=tf.intaa ) _a = tf.zeros(self.model_tester.batch_size , dtype=tf.intaa ) elif model_class in get_values(_snake_case ): _a = tf.zeros(self.model_tester.batch_size , dtype=tf.intaa ) elif model_class in get_values(_snake_case ): _a = tf.zeros( (self.model_tester.batch_size, self.model_tester.text_seq_length) , dtype=tf.intaa ) return inputs_dict def _UpperCAmelCase ( self ) -> Dict: _a = TFLayoutLMvaModelTester(self ) _a = ConfigTester(self , config_class=_snake_case , hidden_size=37 ) def _UpperCAmelCase ( self ) -> int: self.config_tester.run_common_tests() def _UpperCAmelCase ( self ) -> Any: _a , _a = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: _a = model_class(_snake_case ) if getattr(_snake_case , '''hf_compute_loss''' , _snake_case ): # The number of elements in the loss should be the same as the number of elements in the label _a = self._prepare_for_class(inputs_dict.copy() , _snake_case , return_labels=_snake_case ) _a = prepared_for_class[ sorted(prepared_for_class.keys() - inputs_dict.keys() , reverse=_snake_case )[0] ] _a = added_label.shape.as_list()[:1] # Test that model correctly compute the loss with kwargs _a = self._prepare_for_class(inputs_dict.copy() , _snake_case , return_labels=_snake_case ) _a = prepared_for_class.pop('''input_ids''' ) _a = model(_snake_case , **_snake_case )[0] self.assertTrue(loss.shape.as_list() == expected_loss_size or loss.shape.as_list() == [1] ) # Test that model correctly compute the loss when we mask some positions _a = self._prepare_for_class(inputs_dict.copy() , _snake_case , return_labels=_snake_case ) _a = prepared_for_class.pop('''input_ids''' ) if "labels" in prepared_for_class: _a = prepared_for_class['''labels'''].numpy() if len(labels.shape ) > 1 and labels.shape[1] != 1: _a = -100 _a = tf.convert_to_tensor(_snake_case ) _a = model(_snake_case , **_snake_case )[0] self.assertTrue(loss.shape.as_list() == expected_loss_size or loss.shape.as_list() == [1] ) self.assertTrue(not np.any(np.isnan(loss.numpy() ) ) ) # Test that model correctly compute the loss with a dict _a = self._prepare_for_class(inputs_dict.copy() , _snake_case , return_labels=_snake_case ) _a = model(_snake_case )[0] self.assertTrue(loss.shape.as_list() == expected_loss_size or loss.shape.as_list() == [1] ) # Test that model correctly compute the loss with a tuple _a = self._prepare_for_class(inputs_dict.copy() , _snake_case , return_labels=_snake_case ) # Get keys that were added with the _prepare_for_class function _a = prepared_for_class.keys() - inputs_dict.keys() _a = inspect.signature(model.call ).parameters _a = list(signature.keys() ) # Create a dictionary holding the location of the tensors in the tuple _a = {0: '''input_ids'''} for label_key in label_keys: _a = signature_names.index(_snake_case ) _a = label_key _a = sorted(tuple_index_mapping.items() ) # Initialize a list with their default values, update the values and convert to a tuple _a = [] for name in signature_names: if name != "kwargs": list_input.append(signature[name].default ) for index, value in sorted_tuple_index_mapping: _a = prepared_for_class[value] _a = tuple(_snake_case ) # Send to model _a = model(tuple_input[:-1] )[0] self.assertTrue(loss.shape.as_list() == expected_loss_size or loss.shape.as_list() == [1] ) def _UpperCAmelCase ( self ) -> Tuple: ( ( _a ) , ( _a ) , ( _a ) , ( _a ) , ( _a ) , ( _a ) , ( _a ) , ( _a ) , ) = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(_snake_case , _snake_case , _snake_case , _snake_case , _snake_case , _snake_case ) def _UpperCAmelCase ( self ) -> int: ( ( _a ) , ( _a ) , ( _a ) , ( _a ) , ( _a ) , ( _a ) , ( _a ) , ( _a ) , ) = self.model_tester.prepare_config_and_inputs() for type in ["absolute", "relative_key", "relative_key_query"]: _a = type self.model_tester.create_and_check_model(_snake_case , _snake_case , _snake_case , _snake_case , _snake_case , _snake_case ) def _UpperCAmelCase ( self ) -> str: ( ( _a ) , ( _a ) , ( _a ) , ( _a ) , ( _a ) , ( _a ) , ( _a ) , ( _a ) , ) = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_sequence_classification( _snake_case , _snake_case , _snake_case , _snake_case , _snake_case , _snake_case , _snake_case ) def _UpperCAmelCase ( self ) -> Union[str, Any]: ( ( _a ) , ( _a ) , ( _a ) , ( _a ) , ( _a ) , ( _a ) , ( _a ) , ( _a ) , ) = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_token_classification( _snake_case , _snake_case , _snake_case , _snake_case , _snake_case , _snake_case , _snake_case ) def _UpperCAmelCase ( self ) -> Any: ( ( _a ) , ( _a ) , ( _a ) , ( _a ) , ( _a ) , ( _a ) , ( _a ) , ( _a ) , ) = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_question_answering( _snake_case , _snake_case , _snake_case , _snake_case , _snake_case , _snake_case , _snake_case ) @slow def _UpperCAmelCase ( self ) -> Optional[int]: for model_name in TF_LAYOUTLMV3_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: _a = TFLayoutLMvaModel.from_pretrained(_snake_case ) self.assertIsNotNone(_snake_case ) def A_ ( ): """simple docstring""" _a = Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png''' ) return image @require_tf class __lowerCamelCase ( unittest.TestCase ): '''simple docstring''' @cached_property def _UpperCAmelCase ( self ) -> List[Any]: return LayoutLMvaImageProcessor(apply_ocr=_snake_case ) if is_vision_available() else None @slow def _UpperCAmelCase ( self ) -> Union[str, Any]: _a = TFLayoutLMvaModel.from_pretrained('''microsoft/layoutlmv3-base''' ) _a = self.default_image_processor _a = prepare_img() _a = image_processor(images=_snake_case , return_tensors='''tf''' ).pixel_values _a = tf.constant([[1, 2]] ) _a = tf.expand_dims(tf.constant([[1, 2, 3, 4], [5, 6, 7, 8]] ) , axis=0 ) # forward pass _a = model(input_ids=_snake_case , bbox=_snake_case , pixel_values=_snake_case , training=_snake_case ) # verify the logits _a = (1, 199, 768) self.assertEqual(outputs.last_hidden_state.shape , _snake_case ) _a = tf.constant( [[-0.0529, 0.3618, 0.1632], [-0.1587, -0.1667, -0.0400], [-0.1557, -0.1671, -0.0505]] ) self.assertTrue(np.allclose(outputs.last_hidden_state[0, :3, :3] , _snake_case , atol=1e-4 ) )
361
"""simple docstring""" # 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 from ...utils.dataclasses import ( ComputeEnvironment, DistributedType, DynamoBackend, PrecisionType, SageMakerDistributedType, ) from ..menu import BulletMenu __snake_case = [ '''EAGER''', '''AOT_EAGER''', '''INDUCTOR''', '''NVFUSER''', '''AOT_NVFUSER''', '''AOT_CUDAGRAPHS''', '''OFI''', '''FX2TRT''', '''ONNXRT''', '''IPEX''', ] def A_ ( _lowerCAmelCase : List[Any], _lowerCAmelCase : List[str]=None, _lowerCAmelCase : Optional[Any]=None, _lowerCAmelCase : Union[str, Any]=None ): """simple docstring""" _a = True while ask_again: _a = input(_lowerCAmelCase ) try: if default is not None and len(_lowerCAmelCase ) == 0: return default return convert_value(_lowerCAmelCase ) if convert_value is not None else result except Exception: if error_message is not None: print(_lowerCAmelCase ) def A_ ( _lowerCAmelCase : List[str], _lowerCAmelCase : Optional[Any]=[], _lowerCAmelCase : Tuple=None, _lowerCAmelCase : Dict=0 ): """simple docstring""" _a = BulletMenu(_lowerCAmelCase, _lowerCAmelCase ) _a = menu.run(default_choice=_lowerCAmelCase ) return convert_value(_lowerCAmelCase ) if convert_value is not None else result def A_ ( _lowerCAmelCase : List[str] ): """simple docstring""" _a = int(_lowerCAmelCase ) return ComputeEnvironment(['''LOCAL_MACHINE''', '''AMAZON_SAGEMAKER'''][value] ) def A_ ( _lowerCAmelCase : List[Any] ): """simple docstring""" _a = int(_lowerCAmelCase ) return DistributedType(['''NO''', '''MULTI_CPU''', '''MULTI_XPU''', '''MULTI_GPU''', '''MULTI_NPU''', '''TPU'''][value] ) def A_ ( _lowerCAmelCase : Optional[Any] ): """simple docstring""" _a = int(_lowerCAmelCase ) return DynamoBackend(DYNAMO_BACKENDS[value] ).value def A_ ( _lowerCAmelCase : Dict ): """simple docstring""" _a = int(_lowerCAmelCase ) return PrecisionType(['''no''', '''fp16''', '''bf16''', '''fp8'''][value] ) def A_ ( _lowerCAmelCase : Optional[int] ): """simple docstring""" _a = int(_lowerCAmelCase ) return SageMakerDistributedType(['''NO''', '''DATA_PARALLEL''', '''MODEL_PARALLEL'''][value] ) def A_ ( _lowerCAmelCase : Any ): """simple docstring""" return {"yes": True, "no": False}[value.lower()] class __lowerCamelCase ( argparse.RawDescriptionHelpFormatter ): '''simple docstring''' def _UpperCAmelCase ( self , __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase ) -> Union[str, Any]: _a = super()._format_usage(__UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase , __UpperCAmelCase ) _a = usage.replace('''<command> [<args>] ''' , '''''' ) return usage
153
0
'''simple docstring''' # Copyright 2023 The HuggingFace Inc. team. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. from typing import TYPE_CHECKING from ..models.auto import AutoModelForVisionaSeq from ..utils import requires_backends from .base import PipelineTool if TYPE_CHECKING: from PIL import Image class lowercase_ (lowerCamelCase__ ): """simple docstring""" SCREAMING_SNAKE_CASE : Any = 'Salesforce/blip-image-captioning-base' SCREAMING_SNAKE_CASE : Union[str, Any] = ( 'This is a tool that generates a description of an image. It takes an input named `image` which should be the ' 'image to caption, and returns a text that contains the description in English.' ) SCREAMING_SNAKE_CASE : str = 'image_captioner' SCREAMING_SNAKE_CASE : int = AutoModelForVisionaSeq SCREAMING_SNAKE_CASE : str = ['image'] SCREAMING_SNAKE_CASE : Optional[Any] = ['text'] def __init__( self : Union[str, Any] ,*lowercase__ : Dict ,**lowercase__ : Optional[Any] ): requires_backends(self ,['''vision'''] ) super().__init__(*lowercase__ ,**lowercase__ ) def SCREAMING_SNAKE_CASE ( self : Dict ,lowercase__ : "Image" ): return self.pre_processor(images=lowercase__ ,return_tensors='''pt''' ) def SCREAMING_SNAKE_CASE ( self : Tuple ,lowercase__ : Tuple ): return self.model.generate(**lowercase__ ) def SCREAMING_SNAKE_CASE ( self : Any ,lowercase__ : int ): return self.pre_processor.batch_decode(lowercase__ ,skip_special_tokens=lowercase__ )[0].strip()
104
'''simple docstring''' from __future__ import annotations from decimal import Decimal from numpy import array def _A ( A__ ): """simple docstring""" __lowercase = Decimal # Check if the provided matrix has 2 rows and 2 columns # since this implementation only works for 2x2 matrices if len(A__ ) == 2 and len(matrix[0] ) == 2 and len(matrix[1] ) == 2: # Calculate the determinant of the matrix __lowercase = float( d(matrix[0][0] ) * d(matrix[1][1] ) - d(matrix[1][0] ) * d(matrix[0][1] ) ) if determinant == 0: raise ValueError('''This matrix has no inverse.''' ) # Creates a copy of the matrix with swapped positions of the elements __lowercase = [[0.0, 0.0], [0.0, 0.0]] __lowercase , __lowercase = matrix[1][1], matrix[0][0] __lowercase , __lowercase = -matrix[1][0], -matrix[0][1] # Calculate the inverse of the matrix return [ [(float(d(A__ ) ) / determinant) or 0.0 for n in row] for row in swapped_matrix ] elif ( len(A__ ) == 3 and len(matrix[0] ) == 3 and len(matrix[1] ) == 3 and len(matrix[2] ) == 3 ): # Calculate the determinant of the matrix using Sarrus rule __lowercase = float( ( (d(matrix[0][0] ) * d(matrix[1][1] ) * d(matrix[2][2] )) + (d(matrix[0][1] ) * d(matrix[1][2] ) * d(matrix[2][0] )) + (d(matrix[0][2] ) * d(matrix[1][0] ) * d(matrix[2][1] )) ) - ( (d(matrix[0][2] ) * d(matrix[1][1] ) * d(matrix[2][0] )) + (d(matrix[0][1] ) * d(matrix[1][0] ) * d(matrix[2][2] )) + (d(matrix[0][0] ) * d(matrix[1][2] ) * d(matrix[2][1] )) ) ) if determinant == 0: raise ValueError('''This matrix has no inverse.''' ) # Creating cofactor matrix __lowercase = [ [d(0.0 ), d(0.0 ), d(0.0 )], [d(0.0 ), d(0.0 ), d(0.0 )], [d(0.0 ), d(0.0 ), d(0.0 )], ] __lowercase = (d(matrix[1][1] ) * d(matrix[2][2] )) - ( d(matrix[1][2] ) * d(matrix[2][1] ) ) __lowercase = -( (d(matrix[1][0] ) * d(matrix[2][2] )) - (d(matrix[1][2] ) * d(matrix[2][0] )) ) __lowercase = (d(matrix[1][0] ) * d(matrix[2][1] )) - ( d(matrix[1][1] ) * d(matrix[2][0] ) ) __lowercase = -( (d(matrix[0][1] ) * d(matrix[2][2] )) - (d(matrix[0][2] ) * d(matrix[2][1] )) ) __lowercase = (d(matrix[0][0] ) * d(matrix[2][2] )) - ( d(matrix[0][2] ) * d(matrix[2][0] ) ) __lowercase = -( (d(matrix[0][0] ) * d(matrix[2][1] )) - (d(matrix[0][1] ) * d(matrix[2][0] )) ) __lowercase = (d(matrix[0][1] ) * d(matrix[1][2] )) - ( d(matrix[0][2] ) * d(matrix[1][1] ) ) __lowercase = -( (d(matrix[0][0] ) * d(matrix[1][2] )) - (d(matrix[0][2] ) * d(matrix[1][0] )) ) __lowercase = (d(matrix[0][0] ) * d(matrix[1][1] )) - ( d(matrix[0][1] ) * d(matrix[1][0] ) ) # Transpose the cofactor matrix (Adjoint matrix) __lowercase = array(A__ ) for i in range(3 ): for j in range(3 ): __lowercase = cofactor_matrix[j][i] # Inverse of the matrix using the formula (1/determinant) * adjoint matrix __lowercase = array(A__ ) for i in range(3 ): for j in range(3 ): inverse_matrix[i][j] /= d(A__ ) # Calculate the inverse of the matrix return [[float(d(A__ ) ) or 0.0 for n in row] for row in inverse_matrix] raise ValueError('''Please provide a matrix of size 2x2 or 3x3.''' )
104
1
"""simple docstring""" from dataclasses import dataclass from typing import Tuple import numpy as np import torch @dataclass class UpperCamelCase : """simple docstring""" SCREAMING_SNAKE_CASE_ : torch.Tensor # [batch_size x 3] SCREAMING_SNAKE_CASE_ : torch.Tensor # [batch_size x 3] SCREAMING_SNAKE_CASE_ : torch.Tensor # [batch_size x 3] SCREAMING_SNAKE_CASE_ : torch.Tensor # [batch_size x 3] SCREAMING_SNAKE_CASE_ : int SCREAMING_SNAKE_CASE_ : int SCREAMING_SNAKE_CASE_ : float SCREAMING_SNAKE_CASE_ : float SCREAMING_SNAKE_CASE_ : Tuple[int] def lowerCamelCase__ ( self ): assert self.x.shape[0] == self.y.shape[0] == self.z.shape[0] == self.origin.shape[0] assert self.x.shape[1] == self.y.shape[1] == self.z.shape[1] == self.origin.shape[1] == 3 assert len(self.x.shape ) == len(self.y.shape ) == len(self.z.shape ) == len(self.origin.shape ) == 2 def lowerCamelCase__ ( self ): return torch.from_numpy(np.array([self.width, self.height] ,dtype=np.floataa ) ) def lowerCamelCase__ ( self ): return torch.from_numpy(np.array([self.x_fov, self.y_fov] ,dtype=np.floataa ) ) def lowerCamelCase__ ( self ): _lowercase : Optional[int] = torch.arange(self.height * self.width ) _lowercase : Union[str, Any] = torch.stack( [ pixel_indices % self.width, torch.div(UpperCAmelCase_ ,self.width ,rounding_mode="""trunc""" ), ] ,axis=1 ,) return coords @property def lowerCamelCase__ ( self ): _lowercase , *_lowercase : Dict = self.shape _lowercase : Any = int(np.prod(UpperCAmelCase_ ) ) _lowercase : List[Any] = self.get_image_coords() _lowercase : int = torch.broadcast_to(coords.unsqueeze(0 ) ,[batch_size * inner_batch_size, *coords.shape] ) _lowercase : Any = self.get_camera_rays(UpperCAmelCase_ ) _lowercase : str = rays.view(UpperCAmelCase_ ,inner_batch_size * self.height * self.width ,2 ,3 ) return rays def lowerCamelCase__ ( self ,UpperCAmelCase_ ): _lowercase , *_lowercase , _lowercase : Tuple = coords.shape assert n_coords == 2 assert batch_size == self.origin.shape[0] _lowercase : Any = coords.view(UpperCAmelCase_ ,-1 ,2 ) _lowercase : Any = self.resolution() _lowercase : int = self.fov() _lowercase : Optional[int] = (flat.float() / (res - 1)) * 2 - 1 _lowercase : List[Any] = fracs * torch.tan(fov / 2 ) _lowercase : Any = fracs.view(UpperCAmelCase_ ,-1 ,2 ) _lowercase : Optional[int] = ( self.z.view(UpperCAmelCase_ ,1 ,3 ) + self.x.view(UpperCAmelCase_ ,1 ,3 ) * fracs[:, :, :1] + self.y.view(UpperCAmelCase_ ,1 ,3 ) * fracs[:, :, 1:] ) _lowercase : List[str] = directions / directions.norm(dim=-1 ,keepdim=UpperCAmelCase_ ) _lowercase : List[str] = torch.stack( [ torch.broadcast_to(self.origin.view(UpperCAmelCase_ ,1 ,3 ) ,[batch_size, directions.shape[1], 3] ), directions, ] ,dim=2 ,) return rays.view(UpperCAmelCase_ ,*UpperCAmelCase_ ,2 ,3 ) def lowerCamelCase__ ( self ,UpperCAmelCase_ ,UpperCAmelCase_ ): assert width * self.height == height * self.width, "The aspect ratio should not change." return DifferentiableProjectiveCamera( origin=self.origin ,x=self.x ,y=self.y ,z=self.z ,width=UpperCAmelCase_ ,height=UpperCAmelCase_ ,x_fov=self.x_fov ,y_fov=self.y_fov ,) def __SCREAMING_SNAKE_CASE ( __UpperCAmelCase ): _lowercase : str = [] _lowercase : str = [] _lowercase : Union[str, Any] = [] _lowercase : Optional[int] = [] for theta in np.linspace(0 , 2 * np.pi , num=20 ): _lowercase : int = np.array([np.sin(lowerCamelCase__ ), np.cos(lowerCamelCase__ ), -0.5] ) z /= np.sqrt(np.sum(z**2 ) ) _lowercase : Any = -z * 4 _lowercase : Any = np.array([np.cos(lowerCamelCase__ ), -np.sin(lowerCamelCase__ ), 0.0] ) _lowercase : List[str] = np.cross(lowerCamelCase__ , lowerCamelCase__ ) origins.append(lowerCamelCase__ ) xs.append(lowerCamelCase__ ) ys.append(lowerCamelCase__ ) zs.append(lowerCamelCase__ ) return DifferentiableProjectiveCamera( origin=torch.from_numpy(np.stack(lowerCamelCase__ , axis=0 ) ).float() , x=torch.from_numpy(np.stack(lowerCamelCase__ , axis=0 ) ).float() , y=torch.from_numpy(np.stack(lowerCamelCase__ , axis=0 ) ).float() , z=torch.from_numpy(np.stack(lowerCamelCase__ , axis=0 ) ).float() , width=lowerCamelCase__ , height=lowerCamelCase__ , x_fov=0.7 , y_fov=0.7 , shape=(1, len(lowerCamelCase__ )) , )
350
"""simple docstring""" def __SCREAMING_SNAKE_CASE ( ): return [list(range(1000 - i , -1000 - i , -1 ) ) for i in range(1000 )] UpperCAmelCase: Any = generate_large_matrix() UpperCAmelCase: Dict = ( [[4, 3, 2, -1], [3, 2, 1, -1], [1, 1, -1, -2], [-1, -1, -2, -3]], [[3, 2], [1, 0]], [[7, 7, 6]], [[7, 7, 6], [-1, -2, -3]], grid, ) def __SCREAMING_SNAKE_CASE ( __UpperCAmelCase ): assert all(row == sorted(__UpperCAmelCase , reverse=__UpperCAmelCase ) for row in grid ) assert all(list(__UpperCAmelCase ) == sorted(__UpperCAmelCase , reverse=__UpperCAmelCase ) for col in zip(*__UpperCAmelCase ) ) def __SCREAMING_SNAKE_CASE ( __UpperCAmelCase ): _lowercase : Tuple = 0 _lowercase : List[Any] = len(__UpperCAmelCase ) - 1 # Edge cases such as no values or all numbers are negative. if not array or array[0] < 0: return 0 while right + 1 > left: _lowercase : Tuple = (left + right) // 2 _lowercase : List[Any] = array[mid] # Num must be negative and the index must be greater than or equal to 0. if num < 0 and array[mid - 1] >= 0: return mid if num >= 0: _lowercase : Dict = mid + 1 else: _lowercase : Dict = mid - 1 # No negative numbers so return the last index of the array + 1 which is the length. return len(__UpperCAmelCase ) def __SCREAMING_SNAKE_CASE ( __UpperCAmelCase ): _lowercase : Any = 0 _lowercase : Optional[int] = len(grid[0] ) for i in range(len(__UpperCAmelCase ) ): _lowercase : Union[str, Any] = find_negative_index(grid[i][:bound] ) total += bound return (len(__UpperCAmelCase ) * len(grid[0] )) - total def __SCREAMING_SNAKE_CASE ( __UpperCAmelCase ): return len([number for row in grid for number in row if number < 0] ) def __SCREAMING_SNAKE_CASE ( __UpperCAmelCase ): _lowercase : Tuple = 0 for row in grid: for i, number in enumerate(__UpperCAmelCase ): if number < 0: total += len(__UpperCAmelCase ) - i break return total def __SCREAMING_SNAKE_CASE ( ): from timeit import timeit print("""Running benchmarks""" ) _lowercase : Tuple = ( """from __main__ import count_negatives_binary_search, """ """count_negatives_brute_force, count_negatives_brute_force_with_break, grid""" ) for func in ( "count_negatives_binary_search", # took 0.7727 seconds "count_negatives_brute_force_with_break", # took 4.6505 seconds "count_negatives_brute_force", # took 12.8160 seconds ): _lowercase : Dict = timeit(F"""{func}(grid=grid)""" , setup=__UpperCAmelCase , number=500 ) print(F"""{func}() took {time:0.4f} seconds""" ) if __name__ == "__main__": import doctest doctest.testmod() benchmark()
336
0
import inspect import unittest from transformers import MobileNetVaConfig from transformers.testing_utils import require_torch, require_vision, slow, torch_device from transformers.utils import cached_property, is_torch_available, is_vision_available from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from transformers import MobileNetVaForImageClassification, MobileNetVaForSemanticSegmentation, MobileNetVaModel from transformers.models.mobilenet_va.modeling_mobilenet_va import MOBILENET_V2_PRETRAINED_MODEL_ARCHIVE_LIST if is_vision_available(): from PIL import Image from transformers import MobileNetVaImageProcessor class __snake_case ( UpperCamelCase_ ): def UpperCAmelCase__ ( self : List[Any]): lowerCAmelCase_ : str = self.config_class(**self.inputs_dict) self.parent.assertTrue(hasattr(A_ , '''tf_padding''')) self.parent.assertTrue(hasattr(A_ , '''depth_multiplier''')) class __snake_case : def __init__( self : List[str] , A_ : Any , A_ : Tuple=1_3 , A_ : Tuple=3 , A_ : Tuple=3_2 , A_ : List[str]=0.25 , A_ : Dict=8 , A_ : Optional[Any]=8 , A_ : int=6 , A_ : Tuple=3_2 , A_ : Union[str, Any]=True , A_ : Optional[int]=True , A_ : Optional[Any]=True , A_ : Tuple="relu6" , A_ : Union[str, Any]=1_2_8_0 , A_ : List[str]=0.1 , A_ : List[Any]=0.02 , A_ : Optional[int]=True , A_ : Union[str, Any]=True , A_ : List[Any]=1_0 , A_ : Tuple=None , ): lowerCAmelCase_ : List[str] = parent lowerCAmelCase_ : Dict = batch_size lowerCAmelCase_ : Optional[Any] = num_channels lowerCAmelCase_ : Optional[int] = image_size lowerCAmelCase_ : Union[str, Any] = depth_multiplier lowerCAmelCase_ : List[str] = depth_divisible_by lowerCAmelCase_ : List[Any] = min_depth lowerCAmelCase_ : str = expand_ratio lowerCAmelCase_ : str = tf_padding lowerCAmelCase_ : str = output_stride lowerCAmelCase_ : Optional[int] = first_layer_is_expansion lowerCAmelCase_ : Optional[Any] = finegrained_output lowerCAmelCase_ : Optional[int] = hidden_act lowerCAmelCase_ : List[str] = last_hidden_size if finegrained_output else int(last_hidden_size * depth_multiplier) lowerCAmelCase_ : Any = classifier_dropout_prob lowerCAmelCase_ : List[Any] = use_labels lowerCAmelCase_ : Dict = is_training lowerCAmelCase_ : List[str] = num_labels lowerCAmelCase_ : str = initializer_range lowerCAmelCase_ : str = scope def UpperCAmelCase__ ( self : List[str]): lowerCAmelCase_ : Optional[Any] = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]) lowerCAmelCase_ : Any = None lowerCAmelCase_ : List[Any] = None if self.use_labels: lowerCAmelCase_ : List[Any] = ids_tensor([self.batch_size] , self.num_labels) lowerCAmelCase_ : List[Any] = ids_tensor([self.batch_size, self.image_size, self.image_size] , self.num_labels) lowerCAmelCase_ : List[str] = self.get_config() return config, pixel_values, labels, pixel_labels def UpperCAmelCase__ ( self : Dict): return MobileNetVaConfig( num_channels=self.num_channels , image_size=self.image_size , depth_multiplier=self.depth_multiplier , depth_divisible_by=self.depth_divisible_by , min_depth=self.min_depth , expand_ratio=self.expand_ratio , output_stride=self.output_stride , first_layer_is_expansion=self.first_layer_is_expansion , finegrained_output=self.finegrained_output , hidden_act=self.hidden_act , tf_padding=self.tf_padding , classifier_dropout_prob=self.classifier_dropout_prob , initializer_range=self.initializer_range , ) def UpperCAmelCase__ ( self : Any , A_ : Any , A_ : List[Any] , A_ : List[str] , A_ : Tuple): lowerCAmelCase_ : int = MobileNetVaModel(config=A_) model.to(A_) model.eval() lowerCAmelCase_ : str = model(A_) self.parent.assertEqual( result.last_hidden_state.shape , ( self.batch_size, self.last_hidden_size, self.image_size // self.output_stride, self.image_size // self.output_stride, ) , ) self.parent.assertEqual( result.pooler_output.shape , (self.batch_size, self.last_hidden_size) , ) def UpperCAmelCase__ ( self : Optional[Any] , A_ : Tuple , A_ : List[Any] , A_ : Optional[int] , A_ : Optional[Any]): lowerCAmelCase_ : Any = self.num_labels lowerCAmelCase_ : List[str] = MobileNetVaForImageClassification(A_) model.to(A_) model.eval() lowerCAmelCase_ : Optional[int] = model(A_ , labels=A_) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_labels)) def UpperCAmelCase__ ( self : Optional[Any] , A_ : Union[str, Any] , A_ : int , A_ : Tuple , A_ : Optional[int]): lowerCAmelCase_ : Any = self.num_labels lowerCAmelCase_ : Optional[int] = MobileNetVaForSemanticSegmentation(A_) model.to(A_) model.eval() lowerCAmelCase_ : str = model(A_) self.parent.assertEqual( result.logits.shape , ( self.batch_size, self.num_labels, self.image_size // self.output_stride, self.image_size // self.output_stride, ) , ) lowerCAmelCase_ : Union[str, Any] = model(A_ , labels=A_) self.parent.assertEqual( result.logits.shape , ( self.batch_size, self.num_labels, self.image_size // self.output_stride, self.image_size // self.output_stride, ) , ) def UpperCAmelCase__ ( self : int): lowerCAmelCase_ : Tuple = self.prepare_config_and_inputs() lowerCAmelCase_ , lowerCAmelCase_ , lowerCAmelCase_ , lowerCAmelCase_ : Optional[Any] = config_and_inputs lowerCAmelCase_ : Dict = {'''pixel_values''': pixel_values} return config, inputs_dict @require_torch class __snake_case ( UpperCamelCase_ ,UpperCamelCase_ ,unittest.TestCase ): _a = ( (MobileNetVaModel, MobileNetVaForImageClassification, MobileNetVaForSemanticSegmentation) if is_torch_available() else () ) _a = ( { '''feature-extraction''': MobileNetVaModel, '''image-classification''': MobileNetVaForImageClassification, '''image-segmentation''': MobileNetVaForSemanticSegmentation, } if is_torch_available() else {} ) _a = False _a = False _a = False _a = False def UpperCAmelCase__ ( self : Optional[Any]): lowerCAmelCase_ : List[Any] = MobileNetVaModelTester(self) lowerCAmelCase_ : int = MobileNetVaConfigTester(self , config_class=A_ , has_text_modality=A_) def UpperCAmelCase__ ( self : Any): self.config_tester.run_common_tests() @unittest.skip(reason='''MobileNetV2 does not use inputs_embeds''') def UpperCAmelCase__ ( self : List[Any]): pass @unittest.skip(reason='''MobileNetV2 does not support input and output embeddings''') def UpperCAmelCase__ ( self : int): pass @unittest.skip(reason='''MobileNetV2 does not output attentions''') def UpperCAmelCase__ ( self : List[str]): pass def UpperCAmelCase__ ( self : int): lowerCAmelCase_ , lowerCAmelCase_ : str = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: lowerCAmelCase_ : Optional[Any] = model_class(A_) lowerCAmelCase_ : Optional[Any] = inspect.signature(model.forward) # signature.parameters is an OrderedDict => so arg_names order is deterministic lowerCAmelCase_ : Union[str, Any] = [*signature.parameters.keys()] lowerCAmelCase_ : str = ['''pixel_values'''] self.assertListEqual(arg_names[:1] , A_) def UpperCAmelCase__ ( self : int): lowerCAmelCase_ : Tuple = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*A_) def UpperCAmelCase__ ( self : int): def check_hidden_states_output(A_ : List[str] , A_ : int , A_ : Any): lowerCAmelCase_ : Tuple = model_class(A_) model.to(A_) model.eval() with torch.no_grad(): lowerCAmelCase_ : Any = model(**self._prepare_for_class(A_ , A_)) lowerCAmelCase_ : str = outputs.hidden_states lowerCAmelCase_ : List[Any] = 1_6 self.assertEqual(len(A_) , A_) lowerCAmelCase_ , lowerCAmelCase_ : Optional[Any] = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: lowerCAmelCase_ : Optional[int] = True check_hidden_states_output(A_ , A_ , A_) # check that output_hidden_states also work using config del inputs_dict["output_hidden_states"] lowerCAmelCase_ : int = True check_hidden_states_output(A_ , A_ , A_) def UpperCAmelCase__ ( self : str): lowerCAmelCase_ : Optional[Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_image_classification(*A_) def UpperCAmelCase__ ( self : Union[str, Any]): lowerCAmelCase_ : Union[str, Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_semantic_segmentation(*A_) @slow def UpperCAmelCase__ ( self : List[str]): for model_name in MOBILENET_V2_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: lowerCAmelCase_ : List[Any] = MobileNetVaModel.from_pretrained(A_) self.assertIsNotNone(A_) def UpperCamelCase( ): lowerCAmelCase_ : List[str] = Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png''' ) return image @require_torch @require_vision class __snake_case ( unittest.TestCase ): @cached_property def UpperCAmelCase__ ( self : Union[str, Any]): return ( MobileNetVaImageProcessor.from_pretrained('''google/mobilenet_v2_1.0_224''') if is_vision_available() else None ) @slow def UpperCAmelCase__ ( self : Dict): lowerCAmelCase_ : List[str] = MobileNetVaForImageClassification.from_pretrained('''google/mobilenet_v2_1.0_224''').to(A_) lowerCAmelCase_ : List[str] = self.default_image_processor lowerCAmelCase_ : List[str] = prepare_img() lowerCAmelCase_ : List[str] = image_processor(images=A_ , return_tensors='''pt''').to(A_) # forward pass with torch.no_grad(): lowerCAmelCase_ : int = model(**A_) # verify the logits lowerCAmelCase_ : Tuple = torch.Size((1, 1_0_0_1)) self.assertEqual(outputs.logits.shape , A_) lowerCAmelCase_ : str = torch.tensor([0.2445, -1.1993, 0.1905]).to(A_) self.assertTrue(torch.allclose(outputs.logits[0, :3] , A_ , atol=1e-4)) @slow def UpperCAmelCase__ ( self : Union[str, Any]): lowerCAmelCase_ : str = MobileNetVaForSemanticSegmentation.from_pretrained('''google/deeplabv3_mobilenet_v2_1.0_513''') lowerCAmelCase_ : int = model.to(A_) lowerCAmelCase_ : Optional[Any] = MobileNetVaImageProcessor.from_pretrained('''google/deeplabv3_mobilenet_v2_1.0_513''') lowerCAmelCase_ : int = prepare_img() lowerCAmelCase_ : Tuple = image_processor(images=A_ , return_tensors='''pt''').to(A_) # forward pass with torch.no_grad(): lowerCAmelCase_ : str = model(**A_) lowerCAmelCase_ : Optional[int] = outputs.logits # verify the logits lowerCAmelCase_ : Dict = torch.Size((1, 2_1, 6_5, 6_5)) self.assertEqual(logits.shape , A_) lowerCAmelCase_ : Optional[Any] = torch.tensor( [ [[17.5790, 17.7581, 18.3355], [18.3257, 18.4230, 18.8973], [18.6169, 18.8650, 19.2187]], [[-2.1595, -2.0977, -2.3741], [-2.4226, -2.3028, -2.6835], [-2.7819, -2.5991, -2.7706]], [[4.2058, 4.8317, 4.7638], [4.4136, 5.0361, 4.9383], [4.5028, 4.9644, 4.8734]], ] , device=A_ , ) self.assertTrue(torch.allclose(logits[0, :3, :3, :3] , A_ , atol=1e-4))
103
'''simple docstring''' 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 , _lowerCamelCase , _lowerCamelCase=13 , _lowerCamelCase=30 , _lowerCamelCase=2 , _lowerCamelCase=3 , _lowerCamelCase=True , _lowerCamelCase=True , _lowerCamelCase=32 , _lowerCamelCase=5 , _lowerCamelCase=4 , _lowerCamelCase=37 , _lowerCamelCase="gelu" , _lowerCamelCase=0.1 , _lowerCamelCase=0.1 , _lowerCamelCase=10 , _lowerCamelCase=0.02 , _lowerCamelCase=3 , _lowerCamelCase=None , _lowerCamelCase=2 , ) -> str: A_ : Optional[int] = parent A_ : Dict = batch_size A_ : List[Any] = image_size A_ : Optional[int] = patch_size A_ : List[str] = num_channels A_ : List[Any] = is_training A_ : Union[str, Any] = use_labels A_ : Union[str, Any] = hidden_size A_ : str = num_hidden_layers A_ : List[str] = num_attention_heads A_ : Union[str, Any] = intermediate_size A_ : Any = hidden_act A_ : Optional[Any] = hidden_dropout_prob A_ : List[Any] = attention_probs_dropout_prob A_ : Dict = type_sequence_label_size A_ : Optional[int] = initializer_range A_ : str = scope A_ : Optional[Any] = encoder_stride # in DeiT, the seq length equals the number of patches + 2 (we add 2 for the [CLS] and distilation tokens) A_ : Tuple = (image_size // patch_size) ** 2 A_ : Union[str, Any] = num_patches + 2 def UpperCAmelCase_ ( self ) -> Union[str, Any]: A_ : Optional[int] = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) A_ : Dict = None if self.use_labels: A_ : Any = ids_tensor([self.batch_size] , self.type_sequence_label_size ) A_ : Optional[Any] = self.get_config() return config, pixel_values, labels def UpperCAmelCase_ ( self ) -> int: 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 UpperCAmelCase_ ( self , _lowerCamelCase , _lowerCamelCase , _lowerCamelCase ) -> int: A_ : List[str] = DeiTModel(config=_lowerCamelCase ) model.to(_lowerCamelCase ) model.eval() A_ : Dict = model(_lowerCamelCase ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def UpperCAmelCase_ ( self , _lowerCamelCase , _lowerCamelCase , _lowerCamelCase ) -> int: A_ : int = DeiTForMaskedImageModeling(config=_lowerCamelCase ) model.to(_lowerCamelCase ) model.eval() A_ : int = model(_lowerCamelCase ) self.parent.assertEqual( result.reconstruction.shape , (self.batch_size, self.num_channels, self.image_size, self.image_size) ) # test greyscale images A_ : Dict = 1 A_ : Optional[int] = DeiTForMaskedImageModeling(_lowerCamelCase ) model.to(_lowerCamelCase ) model.eval() A_ : Optional[int] = floats_tensor([self.batch_size, 1, self.image_size, self.image_size] ) A_ : int = model(_lowerCamelCase ) self.parent.assertEqual(result.reconstruction.shape , (self.batch_size, 1, self.image_size, self.image_size) ) def UpperCAmelCase_ ( self , _lowerCamelCase , _lowerCamelCase , _lowerCamelCase ) -> Union[str, Any]: A_ : Tuple = self.type_sequence_label_size A_ : Tuple = DeiTForImageClassification(_lowerCamelCase ) model.to(_lowerCamelCase ) model.eval() A_ : int = model(_lowerCamelCase , labels=_lowerCamelCase ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.type_sequence_label_size) ) # test greyscale images A_ : Dict = 1 A_ : Any = DeiTForImageClassification(_lowerCamelCase ) model.to(_lowerCamelCase ) model.eval() A_ : str = floats_tensor([self.batch_size, 1, self.image_size, self.image_size] ) A_ : List[str] = model(_lowerCamelCase , labels=_lowerCamelCase ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.type_sequence_label_size) ) def UpperCAmelCase_ ( self ) -> List[str]: A_ : List[Any] = self.prepare_config_and_inputs() ( ( A_ ) , ( A_ ) , ( A_ ) , ) : Union[str, Any] = config_and_inputs A_ : Tuple = {"""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 UpperCAmelCase_ ( self ) -> Union[str, Any]: A_ : int = DeiTModelTester(self ) A_ : str = ConfigTester(self , config_class=_lowerCamelCase , has_text_modality=_lowerCamelCase , hidden_size=37 ) def UpperCAmelCase_ ( self ) -> List[str]: self.config_tester.run_common_tests() @unittest.skip(reason="""DeiT does not use inputs_embeds""" ) def UpperCAmelCase_ ( self ) -> Optional[int]: pass def UpperCAmelCase_ ( self ) -> Union[str, Any]: A_ , A_ : List[Any] = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: A_ : List[Any] = model_class(_lowerCamelCase ) self.assertIsInstance(model.get_input_embeddings() , (nn.Module) ) A_ : Union[str, Any] = model.get_output_embeddings() self.assertTrue(x is None or isinstance(_lowerCamelCase , nn.Linear ) ) def UpperCAmelCase_ ( self ) -> Optional[Any]: A_ , A_ : Optional[Any] = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: A_ : List[str] = model_class(_lowerCamelCase ) A_ : str = inspect.signature(model.forward ) # signature.parameters is an OrderedDict => so arg_names order is deterministic A_ : Union[str, Any] = [*signature.parameters.keys()] A_ : List[str] = ["""pixel_values"""] self.assertListEqual(arg_names[:1] , _lowerCamelCase ) def UpperCAmelCase_ ( self ) -> List[str]: A_ : Tuple = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*_lowerCamelCase ) def UpperCAmelCase_ ( self ) -> Union[str, Any]: A_ : Optional[Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_masked_image_modeling(*_lowerCamelCase ) def UpperCAmelCase_ ( self ) -> Optional[Any]: A_ : Any = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_image_classification(*_lowerCamelCase ) def UpperCAmelCase_ ( self , _lowerCamelCase , _lowerCamelCase , _lowerCamelCase=False ) -> Union[str, Any]: A_ : int = 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 UpperCAmelCase_ ( self ) -> Optional[Any]: if not self.model_tester.is_training: return A_ , A_ : Optional[Any] = self.model_tester.prepare_config_and_inputs_for_common() A_ : Optional[Any] = 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 A_ : List[str] = model_class(_lowerCamelCase ) model.to(_lowerCamelCase ) model.train() A_ : List[str] = self._prepare_for_class(_lowerCamelCase , _lowerCamelCase , return_labels=_lowerCamelCase ) A_ : List[str] = model(**_lowerCamelCase ).loss loss.backward() def UpperCAmelCase_ ( self ) -> int: A_ , A_ : Dict = self.model_tester.prepare_config_and_inputs_for_common() if not self.model_tester.is_training: return A_ : Any = False A_ : Union[str, Any] = 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 A_ : List[Any] = model_class(_lowerCamelCase ) model.gradient_checkpointing_enable() model.to(_lowerCamelCase ) model.train() A_ : str = self._prepare_for_class(_lowerCamelCase , _lowerCamelCase , return_labels=_lowerCamelCase ) A_ : Union[str, Any] = model(**_lowerCamelCase ).loss loss.backward() def UpperCAmelCase_ ( self ) -> Tuple: A_ , A_ : Optional[Any] = self.model_tester.prepare_config_and_inputs_for_common() A_ : Optional[Any] = [ {"""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']}" ): A_ : Dict = problem_type["""title"""] A_ : List[Any] = problem_type["""num_labels"""] A_ : List[str] = model_class(_lowerCamelCase ) model.to(_lowerCamelCase ) model.train() A_ : List[Any] = self._prepare_for_class(_lowerCamelCase , _lowerCamelCase , return_labels=_lowerCamelCase ) if problem_type["num_labels"] > 1: A_ : Tuple = inputs["""labels"""].unsqueeze(1 ).repeat(1 , problem_type["""num_labels"""] ) A_ : Union[str, Any] = 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: A_ : List[str] = 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 UpperCAmelCase_ ( self ) -> Tuple: for model_name in DEIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: A_ : int = DeiTModel.from_pretrained(_lowerCamelCase ) self.assertIsNotNone(_lowerCamelCase ) def UpperCAmelCase ( ) -> Tuple: """simple docstring""" A_ : Optional[Any] = Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png""" ) return image @require_torch @require_vision class _lowerCAmelCase ( unittest.TestCase ): """simple docstring""" @cached_property def UpperCAmelCase_ ( self ) -> Optional[Any]: return ( DeiTImageProcessor.from_pretrained("""facebook/deit-base-distilled-patch16-224""" ) if is_vision_available() else None ) @slow def UpperCAmelCase_ ( self ) -> Tuple: A_ : Any = DeiTForImageClassificationWithTeacher.from_pretrained("""facebook/deit-base-distilled-patch16-224""" ).to( _lowerCamelCase ) A_ : Optional[int] = self.default_image_processor A_ : str = prepare_img() A_ : Any = image_processor(images=_lowerCamelCase , return_tensors="""pt""" ).to(_lowerCamelCase ) # forward pass with torch.no_grad(): A_ : Any = model(**_lowerCamelCase ) # verify the logits A_ : Tuple = torch.Size((1, 1000) ) self.assertEqual(outputs.logits.shape , _lowerCamelCase ) A_ : List[Any] = 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 UpperCAmelCase_ ( self ) -> Tuple: A_ : Optional[Any] = DeiTModel.from_pretrained( """facebook/deit-base-distilled-patch16-224""" , torch_dtype=torch.floataa , device_map="""auto""" ) A_ : int = self.default_image_processor A_ : List[str] = prepare_img() A_ : List[Any] = image_processor(images=_lowerCamelCase , return_tensors="""pt""" ) A_ : Union[str, Any] = inputs.pixel_values.to(_lowerCamelCase ) # forward pass to make sure inference works in fp16 with torch.no_grad(): A_ : List[Any] = model(_lowerCamelCase )
344
0
"""simple docstring""" UpperCAmelCase =range(2, 20 + 1) UpperCAmelCase =[10**k for k in range(ks[-1] + 1)] UpperCAmelCase ={} def _A ( _a : int , _a : Tuple , _a : Tuple , _a : Optional[int] ): """simple docstring""" A = sum(a_i[j] for j in range(_a , len(_a ) ) ) A = sum(a_i[j] * base[j] for j in range(min(len(_a ) , _a ) ) ) A , A = 0, 0 A = n - i A = memo.get(_a ) if sub_memo is not None: A = sub_memo.get(_a ) if jumps is not None and len(_a ) > 0: # find and make the largest jump without going over A = -1 for _k in range(len(_a ) - 1 , -1 , -1 ): if jumps[_k][2] <= k and jumps[_k][1] <= max_dn: A = _k break if max_jump >= 0: A , A , A = jumps[max_jump] # since the difference between jumps is cached, add c A = diff + c for j in range(min(_a , len(_a ) ) ): A , A = divmod(_a , 1_0 ) if new_c > 0: add(_a , _a , _a ) else: A = [] else: A = {c: []} A = sub_memo if dn >= max_dn or c + diff >= base[k]: return diff, dn if k > ks[0]: while True: # keep doing smaller jumps A , A = next_term(_a , k - 1 , i + dn , _a ) diff += _diff dn += terms_jumped if dn >= max_dn or c + diff >= base[k]: break else: # would be too small a jump, just compute sequential terms instead A , A = compute(_a , _a , i + dn , _a ) diff += _diff dn += terms_jumped A = sub_memo[c] # keep jumps sorted by # of terms skipped A = 0 while j < len(_a ): if jumps[j][1] > dn: break j += 1 # cache the jump for this value digitsum(b) and c sub_memo[c].insert(_a , (diff, dn, k) ) return (diff, dn) def _A ( _a : int , _a : Dict , _a : str , _a : Tuple ): """simple docstring""" if i >= n: return 0, i if k > len(_a ): a_i.extend([0 for _ in range(k - len(_a ) )] ) # note: a_i -> b * 10^k + c # ds_b -> digitsum(b) # ds_c -> digitsum(c) A = i A , A , A = 0, 0, 0 for j in range(len(_a ) ): if j >= k: ds_b += a_i[j] else: ds_c += a_i[j] while i < n: i += 1 A = ds_c + ds_b diff += addend A = 0 for j in range(_a ): A = a_i[j] + addend A , A = divmod(_a , 1_0 ) ds_c += a_i[j] if addend > 0: break if addend > 0: add(_a , _a , _a ) return diff, i - start_i def _A ( _a : Union[str, Any] , _a : Optional[int] , _a : Any ): """simple docstring""" for j in range(_a , len(_a ) ): A = digits[j] + addend if s >= 1_0: A , A = divmod(_a , 1_0 ) A = addend // 1_0 + quotient else: A = s A = addend // 1_0 if addend == 0: break while addend > 0: A , A = divmod(_a , 1_0 ) digits.append(_a ) def _A ( _a : int = 1_0**1_5 ): """simple docstring""" A = [1] A = 1 A = 0 while True: A , A = next_term(_a , 2_0 , i + dn , _a ) dn += terms_jumped if dn == n - i: break A = 0 for j in range(len(_a ) ): a_n += digits[j] * 1_0**j return a_n if __name__ == "__main__": print(f"""{solution() = }""")
77
"""simple docstring""" def _A ( _a : str , _a : str ): """simple docstring""" A = len(_a ) + 1 A = len(_a ) + 1 # dp is a 2d matrix where dp[i][j] denotes whether prefix string of # length i of input_string matches with prefix string of length j of # given pattern. # "dp" stands for dynamic programming. A = [[0 for i in range(_a )] for j in range(_a )] # since string of zero length match pattern of zero length A = 1 # since pattern of zero length will never match with string of non-zero length for i in range(1 , _a ): A = 0 # since string of zero length will match with pattern where there # is at least one * alternatively for j in range(1 , _a ): A = dp[0][j - 2] if pattern[j - 1] == """*""" else 0 # now using bottom-up approach to find for all remaining lengths for i in range(1 , _a ): for j in range(1 , _a ): if input_string[i - 1] == pattern[j - 1] or pattern[j - 1] == ".": A = dp[i - 1][j - 1] elif pattern[j - 1] == "*": if dp[i][j - 2] == 1: A = 1 elif pattern[j - 2] in (input_string[i - 1], "."): A = dp[i - 1][j] else: A = 0 else: A = 0 return bool(dp[-1][-1] ) if __name__ == "__main__": import doctest doctest.testmod() # inputing the strings # input_string = input("input a string :") # pattern = input("input a pattern :") UpperCAmelCase ="aab" UpperCAmelCase ="c*a*b" # using function to check whether given string matches the given pattern if match_pattern(input_string, pattern): print(f"""{input_string} matches the given pattern {pattern}""") else: print(f"""{input_string} does not match with the given pattern {pattern}""")
77
1
"""simple docstring""" import unittest from transformers import AutoConfig, AutoTokenizer, BertConfig, TensorType, is_flax_available from transformers.testing_utils import DUMMY_UNKNOWN_IDENTIFIER, require_flax, slow if is_flax_available(): import jax from transformers.models.auto.modeling_flax_auto import FlaxAutoModel from transformers.models.bert.modeling_flax_bert import FlaxBertModel from transformers.models.roberta.modeling_flax_roberta import FlaxRobertaModel @require_flax class __A ( unittest.TestCase ): """simple docstring""" @slow def SCREAMING_SNAKE_CASE ( self ) -> List[str]: for model_name in ["bert-base-cased", "bert-large-uncased"]: with self.subTest(__A ): a =AutoConfig.from_pretrained(__A ) self.assertIsNotNone(__A ) self.assertIsInstance(__A , __A ) a =FlaxAutoModel.from_pretrained(__A ) self.assertIsNotNone(__A ) self.assertIsInstance(__A , __A ) @slow def SCREAMING_SNAKE_CASE ( self ) -> int: for model_name in ["roberta-base", "roberta-large"]: with self.subTest(__A ): a =AutoConfig.from_pretrained(__A ) self.assertIsNotNone(__A ) self.assertIsInstance(__A , __A ) a =FlaxAutoModel.from_pretrained(__A ) self.assertIsNotNone(__A ) self.assertIsInstance(__A , __A ) @slow def SCREAMING_SNAKE_CASE ( self ) -> Any: for model_name in ["bert-base-cased", "bert-large-uncased"]: a =AutoTokenizer.from_pretrained(__A ) a =FlaxBertModel.from_pretrained(__A ) a =tokenizer('''Do you support jax jitted function?''' , return_tensors=TensorType.JAX ) @jax.jit def eval(**__A ): return model(**__A ) eval(**__A ).block_until_ready() @slow def SCREAMING_SNAKE_CASE ( self ) -> Optional[Any]: for model_name in ["roberta-base", "roberta-large"]: a =AutoTokenizer.from_pretrained(__A ) a =FlaxRobertaModel.from_pretrained(__A ) a =tokenizer('''Do you support jax jitted function?''' , return_tensors=TensorType.JAX ) @jax.jit def eval(**__A ): return model(**__A ) eval(**__A ).block_until_ready() def SCREAMING_SNAKE_CASE ( self ) -> List[Any]: with self.assertRaisesRegex( __A , '''bert-base is not a local folder and is not a valid model identifier''' ): a =FlaxAutoModel.from_pretrained('''bert-base''' ) def SCREAMING_SNAKE_CASE ( self ) -> Any: with self.assertRaisesRegex( __A , r'''aaaaaa is not a valid git identifier \(branch name, tag name or commit id\)''' ): a =FlaxAutoModel.from_pretrained(__A , revision='''aaaaaa''' ) def SCREAMING_SNAKE_CASE ( self ) -> Optional[int]: with self.assertRaisesRegex( __A , '''hf-internal-testing/config-no-model does not appear to have a file named flax_model.msgpack''' , ): a =FlaxAutoModel.from_pretrained('''hf-internal-testing/config-no-model''' ) def SCREAMING_SNAKE_CASE ( self ) -> Any: with self.assertRaisesRegex(__A , '''Use `from_pt=True` to load this model''' ): a =FlaxAutoModel.from_pretrained('''hf-internal-testing/tiny-bert-pt-only''' )
81
"""simple docstring""" import warnings from ...utils import logging from .image_processing_clip import CLIPImageProcessor lowerCamelCase_ : str = logging.get_logger(__name__) class __A ( _SCREAMING_SNAKE_CASE ): """simple docstring""" def __init__( self , *__A , **__A ) -> None: warnings.warn( '''The class CLIPFeatureExtractor is deprecated and will be removed in version 5 of Transformers. Please''' ''' use CLIPImageProcessor instead.''' , __A , ) super().__init__(*__A , **__A )
81
1
'''simple docstring''' import warnings from typing import Dict import numpy as np from ..utils import ExplicitEnum, add_end_docstrings, is_tf_available, is_torch_available from .base import PIPELINE_INIT_ARGS, GenericTensor, Pipeline if is_tf_available(): from ..models.auto.modeling_tf_auto import TF_MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING if is_torch_available(): from ..models.auto.modeling_auto import MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING def lowercase__( __UpperCamelCase: Tuple ): """simple docstring""" return 1.0 / (1.0 + np.exp(-_outputs )) def lowercase__( __UpperCamelCase: Optional[int] ): """simple docstring""" SCREAMING_SNAKE_CASE : str = np.max(_outputs ,axis=-1 ,keepdims=__UpperCamelCase ) SCREAMING_SNAKE_CASE : Optional[Any] = np.exp(_outputs - maxes ) return shifted_exp / shifted_exp.sum(axis=-1 ,keepdims=__UpperCamelCase ) class _a ( SCREAMING_SNAKE_CASE ): '''simple docstring''' A : Optional[int] = '''sigmoid''' A : Dict = '''softmax''' A : str = '''none''' @add_end_docstrings( SCREAMING_SNAKE_CASE , R''' return_all_scores (`bool`, *optional*, defaults to `False`): Whether to return all prediction scores or just the one of the predicted class. function_to_apply (`str`, *optional*, defaults to `"default"`): The function to apply to the model outputs in order to retrieve the scores. Accepts four different values: - `"default"`: if the model has a single label, will apply the sigmoid function on the output. If the model has several labels, will apply the softmax function on the output. - `"sigmoid"`: Applies the sigmoid function on the output. - `"softmax"`: Applies the softmax function on the output. - `"none"`: Does not apply any function on the output. ''' , ) class _a ( SCREAMING_SNAKE_CASE ): '''simple docstring''' A : Tuple = False A : str = ClassificationFunction.NONE def __init__( self, **A ): '''simple docstring''' super().__init__(**A ) self.check_model_type( TF_MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING if self.framework == 'tf' else MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING ) def UpperCamelCase_ ( self, A=None, A=None, A="", **A ): '''simple docstring''' SCREAMING_SNAKE_CASE : List[str] = tokenizer_kwargs SCREAMING_SNAKE_CASE : List[str] = {} if hasattr(self.model.config, 'return_all_scores' ) and return_all_scores is None: SCREAMING_SNAKE_CASE : str = self.model.config.return_all_scores if isinstance(A, A ) or top_k is None: SCREAMING_SNAKE_CASE : Optional[Any] = top_k SCREAMING_SNAKE_CASE : List[str] = False elif return_all_scores is not None: warnings.warn( '`return_all_scores` is now deprecated, if want a similar functionality use `top_k=None` instead of' ' `return_all_scores=True` or `top_k=1` instead of `return_all_scores=False`.', A, ) if return_all_scores: SCREAMING_SNAKE_CASE : Any = None else: SCREAMING_SNAKE_CASE : int = 1 if isinstance(A, A ): SCREAMING_SNAKE_CASE : Dict = ClassificationFunction[function_to_apply.upper()] if function_to_apply is not None: SCREAMING_SNAKE_CASE : Any = function_to_apply return preprocess_params, {}, postprocess_params def __call__( self, *A, **A ): '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[Any] = super().__call__(*A, **A ) # TODO try and retrieve it in a nicer way from _sanitize_parameters. SCREAMING_SNAKE_CASE : Any = 'top_k' not in kwargs if isinstance(args[0], A ) and _legacy: # This pipeline is odd, and return a list when single item is run return [result] else: return result def UpperCamelCase_ ( self, A, **A ): '''simple docstring''' SCREAMING_SNAKE_CASE : List[str] = self.framework if isinstance(A, A ): return self.tokenizer(**A, return_tensors=A, **A ) elif isinstance(A, A ) and len(A ) == 1 and isinstance(inputs[0], A ) and len(inputs[0] ) == 2: # It used to be valid to use a list of list of list for text pairs, keeping this path for BC return self.tokenizer( text=inputs[0][0], text_pair=inputs[0][1], return_tensors=A, **A ) elif isinstance(A, A ): # This is likely an invalid usage of the pipeline attempting to pass text pairs. raise ValueError( 'The pipeline received invalid inputs, if you are trying to send text pairs, you can try to send a' ' dictionary `{"text": "My text", "text_pair": "My pair"}` in order to send a text pair.' ) return self.tokenizer(A, return_tensors=A, **A ) def UpperCamelCase_ ( self, A ): '''simple docstring''' return self.model(**A ) def UpperCamelCase_ ( self, A, A=None, A=1, A=True ): '''simple docstring''' if function_to_apply is None: if self.model.config.problem_type == "multi_label_classification" or self.model.config.num_labels == 1: SCREAMING_SNAKE_CASE : int = ClassificationFunction.SIGMOID elif self.model.config.problem_type == "single_label_classification" or self.model.config.num_labels > 1: SCREAMING_SNAKE_CASE : Any = ClassificationFunction.SOFTMAX elif hasattr(self.model.config, 'function_to_apply' ) and function_to_apply is None: SCREAMING_SNAKE_CASE : Union[str, Any] = self.model.config.function_to_apply else: SCREAMING_SNAKE_CASE : Any = ClassificationFunction.NONE SCREAMING_SNAKE_CASE : List[Any] = model_outputs['logits'][0] SCREAMING_SNAKE_CASE : Tuple = outputs.numpy() if function_to_apply == ClassificationFunction.SIGMOID: SCREAMING_SNAKE_CASE : List[str] = sigmoid(A ) elif function_to_apply == ClassificationFunction.SOFTMAX: SCREAMING_SNAKE_CASE : Optional[int] = softmax(A ) elif function_to_apply == ClassificationFunction.NONE: SCREAMING_SNAKE_CASE : int = outputs else: raise ValueError(F"Unrecognized `function_to_apply` argument: {function_to_apply}" ) if top_k == 1 and _legacy: return {"label": self.model.config.idalabel[scores.argmax().item()], "score": scores.max().item()} SCREAMING_SNAKE_CASE : str = [ {'label': self.model.config.idalabel[i], 'score': score.item()} for i, score in enumerate(A ) ] if not _legacy: dict_scores.sort(key=lambda A : x["score"], reverse=A ) if top_k is not None: SCREAMING_SNAKE_CASE : Optional[Any] = dict_scores[:top_k] return dict_scores
356
'''simple docstring''' import absl # noqa: F401 # Here to have a nice missing dependency error message early on import nltk # noqa: F401 # Here to have a nice missing dependency error message early on import numpy # noqa: F401 # Here to have a nice missing dependency error message early on import six # noqa: F401 # Here to have a nice missing dependency error message early on from rouge_score import rouge_scorer, scoring import datasets UpperCamelCase_ = "\\n@inproceedings{lin-2004-rouge,\n title = \"{ROUGE}: A Package for Automatic Evaluation of Summaries\",\n author = \"Lin, Chin-Yew\",\n booktitle = \"Text Summarization Branches Out\",\n month = jul,\n year = \"2004\",\n address = \"Barcelona, Spain\",\n publisher = \"Association for Computational Linguistics\",\n url = \"https://www.aclweb.org/anthology/W04-1013\",\n pages = \"74--81\",\n}\n" UpperCamelCase_ = "\\nROUGE, or Recall-Oriented Understudy for Gisting Evaluation, is a set of metrics and a software package used for\nevaluating automatic summarization and machine translation software in natural language processing.\nThe metrics compare an automatically produced summary or translation against a reference or a set of references (human-produced) summary or translation.\n\nNote that ROUGE is case insensitive, meaning that upper case letters are treated the same way as lower case letters.\n\nThis metrics is a wrapper around Google Research reimplementation of ROUGE:\nhttps://github.com/google-research/google-research/tree/master/rouge\n" UpperCamelCase_ = "\nCalculates average rouge scores for a list of hypotheses and references\nArgs:\n predictions: list of predictions to score. Each prediction\n should be a string with tokens separated by spaces.\n references: list of reference for each prediction. Each\n reference should be a string with tokens separated by spaces.\n rouge_types: A list of rouge types to calculate.\n Valid names:\n `\"rouge{n}\"` (e.g. `\"rouge1\"`, `\"rouge2\"`) where: {n} is the n-gram based scoring,\n `\"rougeL\"`: Longest common subsequence based scoring.\n `\"rougeLSum\"`: rougeLsum splits text using `\"\n\"`.\n See details in https://github.com/huggingface/datasets/issues/617\n use_stemmer: Bool indicating whether Porter stemmer should be used to strip word suffixes.\n use_aggregator: Return aggregates if this is set to True\nReturns:\n rouge1: rouge_1 (precision, recall, f1),\n rouge2: rouge_2 (precision, recall, f1),\n rougeL: rouge_l (precision, recall, f1),\n rougeLsum: rouge_lsum (precision, recall, f1)\nExamples:\n\n >>> rouge = datasets.load_metric('rouge')\n >>> predictions = [\"hello there\", \"general kenobi\"]\n >>> references = [\"hello there\", \"general kenobi\"]\n >>> results = rouge.compute(predictions=predictions, references=references)\n >>> print(list(results.keys()))\n ['rouge1', 'rouge2', 'rougeL', 'rougeLsum']\n >>> print(results[\"rouge1\"])\n AggregateScore(low=Score(precision=1.0, recall=1.0, fmeasure=1.0), mid=Score(precision=1.0, recall=1.0, fmeasure=1.0), high=Score(precision=1.0, recall=1.0, fmeasure=1.0))\n >>> print(results[\"rouge1\"].mid.fmeasure)\n 1.0\n" @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class _a ( datasets.Metric ): '''simple docstring''' def UpperCamelCase_ ( self ): '''simple docstring''' return datasets.MetricInfo( description=_DESCRIPTION, citation=_CITATION, inputs_description=_KWARGS_DESCRIPTION, features=datasets.Features( { 'predictions': datasets.Value('string', id='sequence' ), 'references': datasets.Value('string', id='sequence' ), } ), codebase_urls=['https://github.com/google-research/google-research/tree/master/rouge'], reference_urls=[ 'https://en.wikipedia.org/wiki/ROUGE_(metric)', 'https://github.com/google-research/google-research/tree/master/rouge', ], ) def UpperCamelCase_ ( self, A, A, A=None, A=True, A=False ): '''simple docstring''' if rouge_types is None: SCREAMING_SNAKE_CASE : List[Any] = ['rouge1', 'rouge2', 'rougeL', 'rougeLsum'] SCREAMING_SNAKE_CASE : int = rouge_scorer.RougeScorer(rouge_types=A, use_stemmer=A ) if use_aggregator: SCREAMING_SNAKE_CASE : Tuple = scoring.BootstrapAggregator() else: SCREAMING_SNAKE_CASE : Union[str, Any] = [] for ref, pred in zip(A, A ): SCREAMING_SNAKE_CASE : Tuple = scorer.score(A, A ) if use_aggregator: aggregator.add_scores(A ) else: scores.append(A ) if use_aggregator: SCREAMING_SNAKE_CASE : Union[str, Any] = aggregator.aggregate() else: SCREAMING_SNAKE_CASE : int = {} for key in scores[0]: SCREAMING_SNAKE_CASE : List[str] = [score[key] for score in scores] return result
246
0
'''simple docstring''' import math_equivalence # From: git+https://github.com/hendrycks/math.git import datasets SCREAMING_SNAKE_CASE_: Optional[Any] ='\\n@article{hendrycksmath2021,\n title={Measuring Mathematical Problem Solving With the MATH Dataset},\n author={Dan Hendrycks\n and Collin Burns\n and Saurav Kadavath\n and Akul Arora\n and Steven Basart\n and Eric Tang\n and Dawn Song\n and Jacob Steinhardt},\n journal={arXiv preprint arXiv:2103.03874},\n year={2021}\n}\n' SCREAMING_SNAKE_CASE_: Union[str, Any] ='\\nThis metric is used to assess performance on the Mathematics Aptitude Test of Heuristics (MATH) dataset.\nIt first canonicalizes the inputs (e.g., converting "1/2" to "\\frac{1}{2}") and then computes accuracy.\n' SCREAMING_SNAKE_CASE_: List[Any] =r'\nCalculates accuracy after canonicalizing inputs.\n\nArgs:\n predictions: list of predictions to score. Each prediction\n is a string that contains natural language and LaTex.\n references: list of reference for each prediction. Each\n reference is a string that contains natural language\n and LaTex.\nReturns:\n accuracy: accuracy after canonicalizing inputs\n (e.g., converting "1/2" to "\\frac{1}{2}")\n\nExamples:\n >>> metric = datasets.load_metric("competition_math")\n >>> results = metric.compute(references=["\\frac{1}{2}"], predictions=["1/2"])\n >>> print(results)\n {\'accuracy\': 1.0}\n' @datasets.utils.file_utils.add_end_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class __A ( datasets.Metric ): def _lowercase (self : Optional[Any] ): return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { "predictions": datasets.Value("string" ), "references": datasets.Value("string" ), } ) , homepage="https://github.com/hendrycks/math" , codebase_urls=["https://github.com/hendrycks/math"] , ) def _lowercase (self : Tuple , __a : Optional[int] , __a : List[Any] ): UpperCAmelCase_ = 0.0 for i, j in zip(__a , __a ): n_correct += 1.0 if math_equivalence.is_equiv(__a , __a ) else 0.0 UpperCAmelCase_ = n_correct / len(__a ) return { "accuracy": accuracy, }
1
'''simple docstring''' from typing import Optional import torch import torch.utils.checkpoint from torch import Tensor, nn from torch.nn import BCEWithLogitsLoss, CrossEntropyLoss, MSELoss from ...activations import ACTaFN from ...file_utils import add_code_sample_docstrings, add_start_docstrings, add_start_docstrings_to_model_forward from ...modeling_outputs import ( BaseModelOutputWithNoAttention, BaseModelOutputWithPoolingAndNoAttention, ImageClassifierOutputWithNoAttention, ) from ...modeling_utils import PreTrainedModel from ...utils import logging from .configuration_regnet import RegNetConfig a__ : Tuple = logging.get_logger(__name__) # General docstring a__ : List[Any] = "RegNetConfig" # Base docstring a__ : Dict = "facebook/regnet-y-040" a__ : Optional[int] = [1, 1_0_8_8, 7, 7] # Image classification docstring a__ : Union[str, Any] = "facebook/regnet-y-040" a__ : Union[str, Any] = "tabby, tabby cat" a__ : int = [ "facebook/regnet-y-040", # See all regnet models at https://huggingface.co/models?filter=regnet ] class UpperCamelCase__ ( nn.Module): def __init__( self :Union[str, Any] , _A :int , _A :int , _A :int = 3 , _A :int = 1 , _A :int = 1 , _A :Optional[str] = "relu" , ) -> int: '''simple docstring''' super().__init__() __A = nn.Convad( _A , _A , kernel_size=_A , stride=_A , padding=kernel_size // 2 , groups=_A , bias=_A , ) __A = nn.BatchNormad(_A ) __A = ACTaFN[activation] if activation is not None else nn.Identity() def lowercase_ ( self :Tuple , _A :Union[str, Any] ) -> int: '''simple docstring''' __A = self.convolution(_A ) __A = self.normalization(_A ) __A = self.activation(_A ) return hidden_state class UpperCamelCase__ ( nn.Module): def __init__( self :Optional[int] , _A :RegNetConfig ) -> List[str]: '''simple docstring''' super().__init__() __A = RegNetConvLayer( config.num_channels , config.embedding_size , kernel_size=3 , stride=2 , activation=config.hidden_act ) __A = config.num_channels def lowercase_ ( self :Any , _A :Optional[int] ) -> Optional[int]: '''simple docstring''' __A = pixel_values.shape[1] if num_channels != self.num_channels: raise ValueError( 'Make sure that the channel dimension of the pixel values match with the one set in the configuration.' ) __A = self.embedder(_A ) return hidden_state class UpperCamelCase__ ( nn.Module): def __init__( self :Optional[int] , _A :int , _A :int , _A :int = 2 ) -> Any: '''simple docstring''' super().__init__() __A = nn.Convad(_A , _A , kernel_size=1 , stride=_A , bias=_A ) __A = nn.BatchNormad(_A ) def lowercase_ ( self :Optional[int] , _A :Tensor ) -> Tensor: '''simple docstring''' __A = self.convolution(_A ) __A = self.normalization(_A ) return hidden_state class UpperCamelCase__ ( nn.Module): def __init__( self :Optional[Any] , _A :int , _A :int ) -> List[str]: '''simple docstring''' super().__init__() __A = nn.AdaptiveAvgPoolad((1, 1) ) __A = nn.Sequential( nn.Convad(_A , _A , kernel_size=1 ) , nn.ReLU() , nn.Convad(_A , _A , kernel_size=1 ) , nn.Sigmoid() , ) def lowercase_ ( self :Any , _A :str ) -> int: '''simple docstring''' __A = self.pooler(_A ) __A = self.attention(_A ) __A = hidden_state * attention return hidden_state class UpperCamelCase__ ( nn.Module): def __init__( self :int , _A :RegNetConfig , _A :int , _A :int , _A :int = 1 ) -> List[Any]: '''simple docstring''' super().__init__() __A = in_channels != out_channels or stride != 1 __A = max(1 , out_channels // config.groups_width ) __A = ( RegNetShortCut(_A , _A , stride=_A ) if should_apply_shortcut else nn.Identity() ) __A = nn.Sequential( RegNetConvLayer(_A , _A , kernel_size=1 , activation=config.hidden_act ) , RegNetConvLayer(_A , _A , stride=_A , groups=_A , activation=config.hidden_act ) , RegNetConvLayer(_A , _A , kernel_size=1 , activation=_A ) , ) __A = ACTaFN[config.hidden_act] def lowercase_ ( self :Optional[Any] , _A :int ) -> int: '''simple docstring''' __A = hidden_state __A = self.layer(_A ) __A = self.shortcut(_A ) hidden_state += residual __A = self.activation(_A ) return hidden_state class UpperCamelCase__ ( nn.Module): def __init__( self :Optional[int] , _A :RegNetConfig , _A :int , _A :int , _A :int = 1 ) -> Any: '''simple docstring''' super().__init__() __A = in_channels != out_channels or stride != 1 __A = max(1 , out_channels // config.groups_width ) __A = ( RegNetShortCut(_A , _A , stride=_A ) if should_apply_shortcut else nn.Identity() ) __A = nn.Sequential( RegNetConvLayer(_A , _A , kernel_size=1 , activation=config.hidden_act ) , RegNetConvLayer(_A , _A , stride=_A , groups=_A , activation=config.hidden_act ) , RegNetSELayer(_A , reduced_channels=int(round(in_channels / 4 ) ) ) , RegNetConvLayer(_A , _A , kernel_size=1 , activation=_A ) , ) __A = ACTaFN[config.hidden_act] def lowercase_ ( self :int , _A :int ) -> int: '''simple docstring''' __A = hidden_state __A = self.layer(_A ) __A = self.shortcut(_A ) hidden_state += residual __A = self.activation(_A ) return hidden_state class UpperCamelCase__ ( nn.Module): def __init__( self :Tuple , _A :RegNetConfig , _A :int , _A :int , _A :int = 2 , _A :int = 2 , ) -> Any: '''simple docstring''' super().__init__() __A = RegNetXLayer if config.layer_type == 'x' else RegNetYLayer __A = nn.Sequential( # downsampling is done in the first layer with stride of 2 layer( _A , _A , _A , stride=_A , ) , *[layer(_A , _A , _A ) for _ in range(depth - 1 )] , ) def lowercase_ ( self :List[str] , _A :Optional[int] ) -> Tuple: '''simple docstring''' __A = self.layers(_A ) return hidden_state class UpperCamelCase__ ( nn.Module): def __init__( self :Union[str, Any] , _A :RegNetConfig ) -> List[str]: '''simple docstring''' super().__init__() __A = nn.ModuleList([] ) # based on `downsample_in_first_stage`, the first layer of the first stage may or may not downsample the input self.stages.append( RegNetStage( _A , config.embedding_size , config.hidden_sizes[0] , stride=2 if config.downsample_in_first_stage else 1 , depth=config.depths[0] , ) ) __A = zip(config.hidden_sizes , config.hidden_sizes[1:] ) for (in_channels, out_channels), depth in zip(_A , config.depths[1:] ): self.stages.append(RegNetStage(_A , _A , _A , depth=_A ) ) def lowercase_ ( self :str , _A :Tensor , _A :bool = False , _A :bool = True ) -> BaseModelOutputWithNoAttention: '''simple docstring''' __A = () if output_hidden_states else None for stage_module in self.stages: if output_hidden_states: __A = hidden_states + (hidden_state,) __A = stage_module(_A ) if output_hidden_states: __A = hidden_states + (hidden_state,) if not return_dict: return tuple(v for v in [hidden_state, hidden_states] if v is not None ) return BaseModelOutputWithNoAttention(last_hidden_state=_A , hidden_states=_A ) class UpperCamelCase__ ( SCREAMING_SNAKE_CASE): UpperCAmelCase__ : int = RegNetConfig UpperCAmelCase__ : Dict = 'regnet' UpperCAmelCase__ : int = 'pixel_values' UpperCAmelCase__ : Optional[int] = True def lowercase_ ( self :str , _A :Optional[int] ) -> Tuple: '''simple docstring''' if isinstance(_A , nn.Convad ): nn.init.kaiming_normal_(module.weight , mode='fan_out' , nonlinearity='relu' ) elif isinstance(_A , (nn.BatchNormad, nn.GroupNorm) ): nn.init.constant_(module.weight , 1 ) nn.init.constant_(module.bias , 0 ) def lowercase_ ( self :int , _A :str , _A :Dict=False ) -> Dict: '''simple docstring''' if isinstance(_A , _A ): __A = value a__ : Optional[int] = R"\n This model is a PyTorch [torch.nn.Module](https://pytorch.org/docs/stable/nn.html#torch.nn.Module) subclass. Use it\n as a regular PyTorch Module and refer to the PyTorch documentation for all matter related to general usage and\n behavior.\n\n Parameters:\n config ([`RegNetConfig`]): Model configuration class with all the parameters of the model.\n Initializing with a config file does not load the weights associated with the model, only the\n configuration. Check out the [`~PreTrainedModel.from_pretrained`] method to load the model weights.\n" a__ : int = R"\n Args:\n pixel_values (`torch.FloatTensor` of shape `(batch_size, num_channels, height, width)`):\n Pixel values. Pixel values can be obtained using [`AutoImageProcessor`]. See\n [`ConvNextImageProcessor.__call__`] for details.\n\n output_hidden_states (`bool`, *optional*):\n Whether or not to return the hidden states of all layers. See `hidden_states` under returned tensors for\n more detail.\n return_dict (`bool`, *optional*):\n Whether or not to return a [`~file_utils.ModelOutput`] instead of a plain tuple.\n" @add_start_docstrings( 'The bare RegNet model outputting raw features without any specific head on top.' , SCREAMING_SNAKE_CASE , ) # Copied from transformers.models.resnet.modeling_resnet.ResNetModel with RESNET->REGNET,ResNet->RegNet class UpperCamelCase__ ( SCREAMING_SNAKE_CASE): def __init__( self :List[str] , _A :List[Any] ) -> List[str]: '''simple docstring''' super().__init__(_A ) __A = config __A = RegNetEmbeddings(_A ) __A = RegNetEncoder(_A ) __A = nn.AdaptiveAvgPoolad((1, 1) ) # Initialize weights and apply final processing self.post_init() @add_start_docstrings_to_model_forward(_A ) @add_code_sample_docstrings( checkpoint=_CHECKPOINT_FOR_DOC , output_type=_A , config_class=_CONFIG_FOR_DOC , modality='vision' , expected_output=_EXPECTED_OUTPUT_SHAPE , ) def lowercase_ ( self :List[Any] , _A :Tensor , _A :Optional[bool] = None , _A :Optional[bool] = None ) -> BaseModelOutputWithPoolingAndNoAttention: '''simple docstring''' __A = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states ) __A = return_dict if return_dict is not None else self.config.use_return_dict __A = self.embedder(_A ) __A = self.encoder( _A , output_hidden_states=_A , return_dict=_A ) __A = encoder_outputs[0] __A = self.pooler(_A ) if not return_dict: return (last_hidden_state, pooled_output) + encoder_outputs[1:] return BaseModelOutputWithPoolingAndNoAttention( last_hidden_state=_A , pooler_output=_A , hidden_states=encoder_outputs.hidden_states , ) @add_start_docstrings( '\n RegNet Model with an image classification head on top (a linear layer on top of the pooled features), e.g. for\n ImageNet.\n ' , SCREAMING_SNAKE_CASE , ) # Copied from transformers.models.resnet.modeling_resnet.ResNetForImageClassification with RESNET->REGNET,ResNet->RegNet,resnet->regnet class UpperCamelCase__ ( SCREAMING_SNAKE_CASE): def __init__( self :Optional[int] , _A :Optional[Any] ) -> Optional[Any]: '''simple docstring''' super().__init__(_A ) __A = config.num_labels __A = RegNetModel(_A ) # classification head __A = nn.Sequential( nn.Flatten() , nn.Linear(config.hidden_sizes[-1] , config.num_labels ) if config.num_labels > 0 else nn.Identity() , ) # initialize weights and apply final processing self.post_init() @add_start_docstrings_to_model_forward(_A ) @add_code_sample_docstrings( checkpoint=_IMAGE_CLASS_CHECKPOINT , output_type=_A , config_class=_CONFIG_FOR_DOC , expected_output=_IMAGE_CLASS_EXPECTED_OUTPUT , ) def lowercase_ ( self :Optional[int] , _A :Optional[torch.FloatTensor] = None , _A :Optional[torch.LongTensor] = None , _A :Optional[bool] = None , _A :Optional[bool] = None , ) -> ImageClassifierOutputWithNoAttention: '''simple docstring''' __A = return_dict if return_dict is not None else self.config.use_return_dict __A = self.regnet(_A , output_hidden_states=_A , return_dict=_A ) __A = outputs.pooler_output if return_dict else outputs[1] __A = self.classifier(_A ) __A = None if labels is not None: if self.config.problem_type is None: if self.num_labels == 1: __A = 'regression' elif self.num_labels > 1 and (labels.dtype == torch.long or labels.dtype == torch.int): __A = 'single_label_classification' else: __A = 'multi_label_classification' if self.config.problem_type == "regression": __A = MSELoss() if self.num_labels == 1: __A = loss_fct(logits.squeeze() , labels.squeeze() ) else: __A = loss_fct(_A , _A ) elif self.config.problem_type == "single_label_classification": __A = CrossEntropyLoss() __A = loss_fct(logits.view(-1 , self.num_labels ) , labels.view(-1 ) ) elif self.config.problem_type == "multi_label_classification": __A = BCEWithLogitsLoss() __A = loss_fct(_A , _A ) if not return_dict: __A = (logits,) + outputs[2:] return (loss,) + output if loss is not None else output return ImageClassifierOutputWithNoAttention(loss=_A , logits=_A , hidden_states=outputs.hidden_states )
161
0
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available, is_vision_available _lowerCAmelCase = {"configuration_yolos": ["YOLOS_PRETRAINED_CONFIG_ARCHIVE_MAP", "YolosConfig", "YolosOnnxConfig"]} try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _lowerCAmelCase = ["YolosFeatureExtractor"] _lowerCAmelCase = ["YolosImageProcessor"] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _lowerCAmelCase = [ "YOLOS_PRETRAINED_MODEL_ARCHIVE_LIST", "YolosForObjectDetection", "YolosModel", "YolosPreTrainedModel", ] if TYPE_CHECKING: from .configuration_yolos import YOLOS_PRETRAINED_CONFIG_ARCHIVE_MAP, YolosConfig, YolosOnnxConfig try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .feature_extraction_yolos import YolosFeatureExtractor from .image_processing_yolos import YolosImageProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_yolos import ( YOLOS_PRETRAINED_MODEL_ARCHIVE_LIST, YolosForObjectDetection, YolosModel, YolosPreTrainedModel, ) else: import sys _lowerCAmelCase = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
367
import functools def A ( __UpperCAmelCase , __UpperCAmelCase ) -> int: '''simple docstring''' UpperCAmelCase_ = len(__UpperCAmelCase ) UpperCAmelCase_ = len(__UpperCAmelCase ) @functools.cache def min_distance(__UpperCAmelCase , __UpperCAmelCase ) -> int: # if first word index is overflow - delete all from the second word if indexa >= len_worda: return len_worda - indexa # if second word index is overflow - delete all from the first word if indexa >= len_worda: return len_worda - indexa UpperCAmelCase_ = int(worda[indexa] != worda[indexa] ) # current letters not identical return min( 1 + min_distance(indexa + 1 , __UpperCAmelCase ) , 1 + min_distance(__UpperCAmelCase , indexa + 1 ) , diff + min_distance(indexa + 1 , indexa + 1 ) , ) return min_distance(0 , 0 ) if __name__ == "__main__": import doctest doctest.testmod()
344
0
"""simple docstring""" import gc import random import tempfile import unittest import numpy as np import torch from PIL import Image from transformers import CLIPTextConfig, CLIPTextModel, CLIPTokenizer from diffusers import ( AutoencoderKL, DDIMInverseScheduler, DDIMScheduler, DPMSolverMultistepInverseScheduler, DPMSolverMultistepScheduler, StableDiffusionDiffEditPipeline, UNetaDConditionModel, ) from diffusers.utils import load_image, slow from diffusers.utils.testing_utils import enable_full_determinism, floats_tensor, require_torch_gpu, torch_device from ..pipeline_params import TEXT_GUIDED_IMAGE_INPAINTING_BATCH_PARAMS, TEXT_GUIDED_IMAGE_INPAINTING_PARAMS from ..test_pipelines_common import PipelineLatentTesterMixin, PipelineTesterMixin enable_full_determinism() class __A ( _SCREAMING_SNAKE_CASE, _SCREAMING_SNAKE_CASE, unittest.TestCase ): """simple docstring""" __lowerCAmelCase = StableDiffusionDiffEditPipeline __lowerCAmelCase = TEXT_GUIDED_IMAGE_INPAINTING_PARAMS - {"height", "width", "image"} | {"image_latents"} __lowerCAmelCase = TEXT_GUIDED_IMAGE_INPAINTING_BATCH_PARAMS - {"image"} | {"image_latents"} __lowerCAmelCase = frozenset( [] ) # TO-DO: update image_params once pipeline is refactored with VaeImageProcessor.preprocess __lowerCAmelCase = frozenset([] ) def SCREAMING_SNAKE_CASE ( self ) -> Dict: torch.manual_seed(0 ) a =UNetaDConditionModel( block_out_channels=(32, 64) , layers_per_block=2 , sample_size=32 , in_channels=4 , out_channels=4 , down_block_types=('''DownBlock2D''', '''CrossAttnDownBlock2D''') , up_block_types=('''CrossAttnUpBlock2D''', '''UpBlock2D''') , cross_attention_dim=32 , attention_head_dim=(2, 4) , use_linear_projection=__A , ) a =DDIMScheduler( beta_start=0.00_085 , beta_end=0.012 , beta_schedule='''scaled_linear''' , clip_sample=__A , set_alpha_to_one=__A , ) a =DDIMInverseScheduler( beta_start=0.00_085 , beta_end=0.012 , beta_schedule='''scaled_linear''' , clip_sample=__A , set_alpha_to_zero=__A , ) torch.manual_seed(0 ) a =AutoencoderKL( block_out_channels=[32, 64] , in_channels=3 , out_channels=3 , down_block_types=['''DownEncoderBlock2D''', '''DownEncoderBlock2D'''] , up_block_types=['''UpDecoderBlock2D''', '''UpDecoderBlock2D'''] , latent_channels=4 , sample_size=128 , ) torch.manual_seed(0 ) a =CLIPTextConfig( bos_token_id=0 , eos_token_id=2 , hidden_size=32 , intermediate_size=37 , layer_norm_eps=1E-0_5 , num_attention_heads=4 , num_hidden_layers=5 , pad_token_id=1 , vocab_size=1000 , hidden_act='''gelu''' , projection_dim=512 , ) a =CLIPTextModel(__A ) a =CLIPTokenizer.from_pretrained('''hf-internal-testing/tiny-random-clip''' ) a ={ '''unet''': unet, '''scheduler''': scheduler, '''inverse_scheduler''': inverse_scheduler, '''vae''': vae, '''text_encoder''': text_encoder, '''tokenizer''': tokenizer, '''safety_checker''': None, '''feature_extractor''': None, } return components def SCREAMING_SNAKE_CASE ( self , __A , __A=0 ) -> str: a =floats_tensor((1, 16, 16) , rng=random.Random(__A ) ).to(__A ) a =floats_tensor((1, 2, 4, 16, 16) , rng=random.Random(__A ) ).to(__A ) if str(__A ).startswith('''mps''' ): a =torch.manual_seed(__A ) else: a =torch.Generator(device=__A ).manual_seed(__A ) a ={ '''prompt''': '''a dog and a newt''', '''mask_image''': mask, '''image_latents''': latents, '''generator''': generator, '''num_inference_steps''': 2, '''inpaint_strength''': 1.0, '''guidance_scale''': 6.0, '''output_type''': '''numpy''', } return inputs def SCREAMING_SNAKE_CASE ( self , __A , __A=0 ) -> Optional[Any]: a =floats_tensor((1, 3, 32, 32) , rng=random.Random(__A ) ).to(__A ) a =image.cpu().permute(0 , 2 , 3 , 1 )[0] a =Image.fromarray(np.uinta(__A ) ).convert('''RGB''' ) if str(__A ).startswith('''mps''' ): a =torch.manual_seed(__A ) else: a =torch.Generator(device=__A ).manual_seed(__A ) a ={ '''image''': image, '''source_prompt''': '''a cat and a frog''', '''target_prompt''': '''a dog and a newt''', '''generator''': generator, '''num_inference_steps''': 2, '''num_maps_per_mask''': 2, '''mask_encode_strength''': 1.0, '''guidance_scale''': 6.0, '''output_type''': '''numpy''', } return inputs def SCREAMING_SNAKE_CASE ( self , __A , __A=0 ) -> str: a =floats_tensor((1, 3, 32, 32) , rng=random.Random(__A ) ).to(__A ) a =image.cpu().permute(0 , 2 , 3 , 1 )[0] a =Image.fromarray(np.uinta(__A ) ).convert('''RGB''' ) if str(__A ).startswith('''mps''' ): a =torch.manual_seed(__A ) else: a =torch.Generator(device=__A ).manual_seed(__A ) a ={ '''image''': image, '''prompt''': '''a cat and a frog''', '''generator''': generator, '''num_inference_steps''': 2, '''inpaint_strength''': 1.0, '''guidance_scale''': 6.0, '''decode_latents''': True, '''output_type''': '''numpy''', } return inputs def SCREAMING_SNAKE_CASE ( self ) -> List[str]: if not hasattr(self.pipeline_class , '''_optional_components''' ): return a =self.get_dummy_components() a =self.pipeline_class(**__A ) pipe.to(__A ) pipe.set_progress_bar_config(disable=__A ) # set all optional components to None and update pipeline config accordingly for optional_component in pipe._optional_components: setattr(__A , __A , __A ) pipe.register_modules(**{optional_component: None for optional_component in pipe._optional_components} ) a =self.get_dummy_inputs(__A ) a =pipe(**__A )[0] with tempfile.TemporaryDirectory() as tmpdir: pipe.save_pretrained(__A ) a =self.pipeline_class.from_pretrained(__A ) pipe_loaded.to(__A ) pipe_loaded.set_progress_bar_config(disable=__A ) for optional_component in pipe._optional_components: self.assertTrue( getattr(__A , __A ) is None , f'''`{optional_component}` did not stay set to None after loading.''' , ) a =self.get_dummy_inputs(__A ) a =pipe_loaded(**__A )[0] a =np.abs(output - output_loaded ).max() self.assertLess(__A , 1E-4 ) def SCREAMING_SNAKE_CASE ( self ) -> List[str]: a ='''cpu''' a =self.get_dummy_components() a =self.pipeline_class(**__A ) pipe.to(__A ) pipe.set_progress_bar_config(disable=__A ) a =self.get_dummy_mask_inputs(__A ) a =pipe.generate_mask(**__A ) a =mask[0, -3:, -3:] self.assertEqual(mask.shape , (1, 16, 16) ) a =np.array([0] * 9 ) a =np.abs(mask_slice.flatten() - expected_slice ).max() self.assertLessEqual(__A , 1E-3 ) self.assertEqual(mask[0, -3, -4] , 0 ) def SCREAMING_SNAKE_CASE ( self ) -> Any: a ='''cpu''' a =self.get_dummy_components() a =self.pipeline_class(**__A ) pipe.to(__A ) pipe.set_progress_bar_config(disable=__A ) a =self.get_dummy_inversion_inputs(__A ) a =pipe.invert(**__A ).images a =image[0, -1, -3:, -3:] self.assertEqual(image.shape , (2, 32, 32, 3) ) a =np.array( [0.5_150, 0.5_134, 0.5_043, 0.5_376, 0.4_694, 0.51_050, 0.5_015, 0.4_407, 0.4_799] , ) a =np.abs(image_slice.flatten() - expected_slice ).max() self.assertLessEqual(__A , 1E-3 ) def SCREAMING_SNAKE_CASE ( self ) -> Dict: super().test_inference_batch_single_identical(expected_max_diff=5E-3 ) def SCREAMING_SNAKE_CASE ( self ) -> List[str]: a ='''cpu''' a =self.get_dummy_components() a ={'''beta_start''': 0.00_085, '''beta_end''': 0.012, '''beta_schedule''': '''scaled_linear'''} a =DPMSolverMultistepScheduler(**__A ) a =DPMSolverMultistepInverseScheduler(**__A ) a =self.pipeline_class(**__A ) pipe.to(__A ) pipe.set_progress_bar_config(disable=__A ) a =self.get_dummy_inversion_inputs(__A ) a =pipe.invert(**__A ).images a =image[0, -1, -3:, -3:] self.assertEqual(image.shape , (2, 32, 32, 3) ) a =np.array( [0.5_150, 0.5_134, 0.5_043, 0.5_376, 0.4_694, 0.51_050, 0.5_015, 0.4_407, 0.4_799] , ) a =np.abs(image_slice.flatten() - expected_slice ).max() self.assertLessEqual(__A , 1E-3 ) @require_torch_gpu @slow class __A ( unittest.TestCase ): """simple docstring""" def SCREAMING_SNAKE_CASE ( self ) -> List[str]: super().tearDown() gc.collect() torch.cuda.empty_cache() @classmethod def SCREAMING_SNAKE_CASE ( cls ) -> List[Any]: a =load_image( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/diffedit/fruit.png''' ) a =raw_image.convert('''RGB''' ).resize((768, 768) ) a =raw_image def SCREAMING_SNAKE_CASE ( self ) -> Optional[Any]: a =torch.manual_seed(0 ) a =StableDiffusionDiffEditPipeline.from_pretrained( '''stabilityai/stable-diffusion-2-1''' , safety_checker=__A , torch_dtype=torch.floataa ) a =DDIMScheduler.from_config(pipe.scheduler.config ) a =DDIMInverseScheduler.from_config(pipe.scheduler.config ) pipe.enable_model_cpu_offload() pipe.set_progress_bar_config(disable=__A ) a ='''a bowl of fruit''' a ='''a bowl of pears''' a =pipe.generate_mask( image=self.raw_image , source_prompt=__A , target_prompt=__A , generator=__A , ) a =pipe.invert( prompt=__A , image=self.raw_image , inpaint_strength=0.7 , generator=__A ).latents a =pipe( prompt=__A , mask_image=__A , image_latents=__A , generator=__A , negative_prompt=__A , inpaint_strength=0.7 , output_type='''numpy''' , ).images[0] a =( np.array( load_image( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main''' '''/diffedit/pears.png''' ).resize((768, 768) ) ) / 255 ) assert np.abs((expected_image - image).max() ) < 5E-1 def SCREAMING_SNAKE_CASE ( self ) -> List[Any]: a =torch.manual_seed(0 ) a =StableDiffusionDiffEditPipeline.from_pretrained( '''stabilityai/stable-diffusion-2-1''' , safety_checker=__A , torch_dtype=torch.floataa ) a =DPMSolverMultistepScheduler.from_config(pipe.scheduler.config ) a =DPMSolverMultistepInverseScheduler.from_config(pipe.scheduler.config ) pipe.enable_model_cpu_offload() pipe.set_progress_bar_config(disable=__A ) a ='''a bowl of fruit''' a ='''a bowl of pears''' a =pipe.generate_mask( image=self.raw_image , source_prompt=__A , target_prompt=__A , generator=__A , ) a =pipe.invert( prompt=__A , image=self.raw_image , inpaint_strength=0.7 , generator=__A , num_inference_steps=25 , ).latents a =pipe( prompt=__A , mask_image=__A , image_latents=__A , generator=__A , negative_prompt=__A , inpaint_strength=0.7 , num_inference_steps=25 , output_type='''numpy''' , ).images[0] a =( np.array( load_image( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main''' '''/diffedit/pears.png''' ).resize((768, 768) ) ) / 255 ) assert np.abs((expected_image - image).max() ) < 5E-1
81
import inspect import warnings from typing import Any, Dict, Optional, Union from packaging import version def _UpperCamelCase ( *lowercase__ , lowercase__ = None , lowercase__=True , lowercase__=2 ): from .. import __version__ __SCREAMING_SNAKE_CASE : Optional[Any] = take_from __SCREAMING_SNAKE_CASE : List[str] = () if not isinstance(args[0] , lowercase__ ): __SCREAMING_SNAKE_CASE : List[Any] = (args,) for attribute, version_name, message in args: if version.parse(version.parse(lowercase__ ).base_version ) >= version.parse(lowercase__ ): raise ValueError( F'''The deprecation tuple {(attribute, version_name, message)} should be removed since diffusers\'''' F''' version {__version__} is >= {version_name}''' ) __SCREAMING_SNAKE_CASE : Optional[Any] = None if isinstance(lowercase__ , lowercase__ ) and attribute in deprecated_kwargs: values += (deprecated_kwargs.pop(lowercase__ ),) __SCREAMING_SNAKE_CASE : List[Any] = F'''The `{attribute}` argument is deprecated and will be removed in version {version_name}.''' elif hasattr(lowercase__ , lowercase__ ): values += (getattr(lowercase__ , lowercase__ ),) __SCREAMING_SNAKE_CASE : List[str] = F'''The `{attribute}` attribute is deprecated and will be removed in version {version_name}.''' elif deprecated_kwargs is None: __SCREAMING_SNAKE_CASE : str = F'''`{attribute}` is deprecated and will be removed in version {version_name}.''' if warning is not None: __SCREAMING_SNAKE_CASE : Any = warning + ''' ''' if standard_warn else '''''' warnings.warn(warning + message , lowercase__ , stacklevel=lowercase__ ) if isinstance(lowercase__ , lowercase__ ) and len(lowercase__ ) > 0: __SCREAMING_SNAKE_CASE : Union[str, Any] = inspect.getouterframes(inspect.currentframe() )[1] __SCREAMING_SNAKE_CASE : Dict = call_frame.filename __SCREAMING_SNAKE_CASE : Optional[Any] = call_frame.lineno __SCREAMING_SNAKE_CASE : int = call_frame.function __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE : int = next(iter(deprecated_kwargs.items() ) ) raise TypeError(F'''{function} in {filename} line {line_number-1} got an unexpected keyword argument `{key}`''' ) if len(lowercase__ ) == 0: return elif len(lowercase__ ) == 1: return values[0] return values
9
0
from typing import TYPE_CHECKING from ....utils import _LazyModule _SCREAMING_SNAKE_CASE = {"""tokenization_tapex""": ["""TapexTokenizer"""]} if TYPE_CHECKING: from .tokenization_tapex import TapexTokenizer else: import sys _SCREAMING_SNAKE_CASE = _LazyModule(__name__, globals()["""__file__"""], _import_structure)
165
import argparse import requests import torch from PIL import Image from torchvision.transforms import Compose, Normalize, Resize, ToTensor from transformers import SwinaSRConfig, SwinaSRForImageSuperResolution, SwinaSRImageProcessor def lowercase( UpperCamelCase_ ) -> Optional[int]: '''simple docstring''' UpperCamelCase = SwinaSRConfig() if "Swin2SR_ClassicalSR_X4_64" in checkpoint_url: UpperCamelCase = 4 elif "Swin2SR_CompressedSR_X4_48" in checkpoint_url: UpperCamelCase = 4 UpperCamelCase = 48 UpperCamelCase = """pixelshuffle_aux""" elif "Swin2SR_Lightweight_X2_64" in checkpoint_url: UpperCamelCase = [6, 6, 6, 6] UpperCamelCase = 60 UpperCamelCase = [6, 6, 6, 6] UpperCamelCase = """pixelshuffledirect""" elif "Swin2SR_RealworldSR_X4_64_BSRGAN_PSNR" in checkpoint_url: UpperCamelCase = 4 UpperCamelCase = """nearest+conv""" elif "Swin2SR_Jpeg_dynamic" in checkpoint_url: UpperCamelCase = 1 UpperCamelCase = 1 UpperCamelCase = 126 UpperCamelCase = 7 UpperCamelCase = 2_5_5.0 UpperCamelCase = """""" return config def lowercase( UpperCamelCase_ , UpperCamelCase_ ) -> Dict: '''simple docstring''' if "patch_embed.proj" in name and "layers" not in name: UpperCamelCase = name.replace("""patch_embed.proj""" , """embeddings.patch_embeddings.projection""" ) if "patch_embed.norm" in name: UpperCamelCase = name.replace("""patch_embed.norm""" , """embeddings.patch_embeddings.layernorm""" ) if "layers" in name: UpperCamelCase = name.replace("""layers""" , """encoder.stages""" ) if "residual_group.blocks" in name: UpperCamelCase = name.replace("""residual_group.blocks""" , """layers""" ) if "attn.proj" in name: UpperCamelCase = name.replace("""attn.proj""" , """attention.output.dense""" ) if "attn" in name: UpperCamelCase = name.replace("""attn""" , """attention.self""" ) if "norm1" in name: UpperCamelCase = name.replace("""norm1""" , """layernorm_before""" ) if "norm2" in name: UpperCamelCase = name.replace("""norm2""" , """layernorm_after""" ) if "mlp.fc1" in name: UpperCamelCase = name.replace("""mlp.fc1""" , """intermediate.dense""" ) if "mlp.fc2" in name: UpperCamelCase = name.replace("""mlp.fc2""" , """output.dense""" ) if "q_bias" in name: UpperCamelCase = name.replace("""q_bias""" , """query.bias""" ) if "k_bias" in name: UpperCamelCase = name.replace("""k_bias""" , """key.bias""" ) if "v_bias" in name: UpperCamelCase = name.replace("""v_bias""" , """value.bias""" ) if "cpb_mlp" in name: UpperCamelCase = name.replace("""cpb_mlp""" , """continuous_position_bias_mlp""" ) if "patch_embed.proj" in name: UpperCamelCase = name.replace("""patch_embed.proj""" , """patch_embed.projection""" ) if name == "norm.weight": UpperCamelCase = """layernorm.weight""" if name == "norm.bias": UpperCamelCase = """layernorm.bias""" if "conv_first" in name: UpperCamelCase = name.replace("""conv_first""" , """first_convolution""" ) if ( "upsample" in name or "conv_before_upsample" in name or "conv_bicubic" in name or "conv_up" in name or "conv_hr" in name or "conv_last" in name or "aux" in name ): # heads if "conv_last" in name: UpperCamelCase = name.replace("""conv_last""" , """final_convolution""" ) if config.upsampler in ["pixelshuffle", "pixelshuffle_aux", "nearest+conv"]: if "conv_before_upsample.0" in name: UpperCamelCase = name.replace("""conv_before_upsample.0""" , """conv_before_upsample""" ) if "upsample.0" in name: UpperCamelCase = name.replace("""upsample.0""" , """upsample.convolution_0""" ) if "upsample.2" in name: UpperCamelCase = name.replace("""upsample.2""" , """upsample.convolution_1""" ) UpperCamelCase = """upsample.""" + name elif config.upsampler == "pixelshuffledirect": UpperCamelCase = name.replace("""upsample.0.weight""" , """upsample.conv.weight""" ) UpperCamelCase = name.replace("""upsample.0.bias""" , """upsample.conv.bias""" ) else: pass else: UpperCamelCase = """swin2sr.""" + name return name def lowercase( UpperCamelCase_ , UpperCamelCase_ ) -> Union[str, Any]: '''simple docstring''' for key in orig_state_dict.copy().keys(): UpperCamelCase = orig_state_dict.pop(UpperCamelCase_ ) if "qkv" in key: UpperCamelCase = key.split(""".""" ) UpperCamelCase = int(key_split[1] ) UpperCamelCase = int(key_split[4] ) UpperCamelCase = config.embed_dim if "weight" in key: UpperCamelCase = val[:dim, :] UpperCamelCase = val[dim : dim * 2, :] UpperCamelCase = val[-dim:, :] else: UpperCamelCase = val[:dim] UpperCamelCase = val[dim : dim * 2] UpperCamelCase = val[-dim:] pass else: UpperCamelCase = val return orig_state_dict def lowercase( UpperCamelCase_ , UpperCamelCase_ , UpperCamelCase_ ) -> Optional[Any]: '''simple docstring''' UpperCamelCase = get_config(UpperCamelCase_ ) UpperCamelCase = SwinaSRForImageSuperResolution(UpperCamelCase_ ) model.eval() UpperCamelCase = torch.hub.load_state_dict_from_url(UpperCamelCase_ , map_location="""cpu""" ) UpperCamelCase = convert_state_dict(UpperCamelCase_ , UpperCamelCase_ ) UpperCamelCase , UpperCamelCase = model.load_state_dict(UpperCamelCase_ , strict=UpperCamelCase_ ) if len(UpperCamelCase_ ) > 0: raise ValueError("""Missing keys when converting: {}""".format(UpperCamelCase_ ) ) for key in unexpected_keys: if not ("relative_position_index" in key or "relative_coords_table" in key or "self_mask" in key): raise ValueError(f"""Unexpected key {key} in state_dict""" ) # verify values UpperCamelCase = """https://github.com/mv-lab/swin2sr/blob/main/testsets/real-inputs/shanghai.jpg?raw=true""" UpperCamelCase = Image.open(requests.get(UpperCamelCase_ , stream=UpperCamelCase_ ).raw ).convert("""RGB""" ) UpperCamelCase = SwinaSRImageProcessor() # pixel_values = processor(image, return_tensors="pt").pixel_values UpperCamelCase = 126 if """Jpeg""" in checkpoint_url else 256 UpperCamelCase = Compose( [ Resize((image_size, image_size) ), ToTensor(), Normalize(mean=[0.4_8_5, 0.4_5_6, 0.4_0_6] , std=[0.2_2_9, 0.2_2_4, 0.2_2_5] ), ] ) UpperCamelCase = transforms(UpperCamelCase_ ).unsqueeze(0 ) if config.num_channels == 1: UpperCamelCase = pixel_values[:, 0, :, :].unsqueeze(1 ) UpperCamelCase = model(UpperCamelCase_ ) # assert values if "Swin2SR_ClassicalSR_X2_64" in checkpoint_url: UpperCamelCase = torch.Size([1, 3, 512, 512] ) UpperCamelCase = torch.tensor( [[-0.7_0_8_7, -0.7_1_3_8, -0.6_7_2_1], [-0.8_3_4_0, -0.8_0_9_5, -0.7_2_9_8], [-0.9_1_4_9, -0.8_4_1_4, -0.7_9_4_0]] ) elif "Swin2SR_ClassicalSR_X4_64" in checkpoint_url: UpperCamelCase = torch.Size([1, 3, 1024, 1024] ) UpperCamelCase = torch.tensor( [[-0.7_7_7_5, -0.8_1_0_5, -0.8_9_3_3], [-0.7_7_6_4, -0.8_3_5_6, -0.9_2_2_5], [-0.7_9_7_6, -0.8_6_8_6, -0.9_5_7_9]] ) elif "Swin2SR_CompressedSR_X4_48" in checkpoint_url: # TODO values didn't match exactly here UpperCamelCase = torch.Size([1, 3, 1024, 1024] ) UpperCamelCase = torch.tensor( [[-0.8_0_3_5, -0.7_5_0_4, -0.7_4_9_1], [-0.8_5_3_8, -0.8_1_2_4, -0.7_7_8_2], [-0.8_8_0_4, -0.8_6_5_1, -0.8_4_9_3]] ) elif "Swin2SR_Lightweight_X2_64" in checkpoint_url: UpperCamelCase = torch.Size([1, 3, 512, 512] ) UpperCamelCase = torch.tensor( [[-0.7_6_6_9, -0.8_6_6_2, -0.8_7_6_7], [-0.8_8_1_0, -0.9_9_6_2, -0.9_8_2_0], [-0.9_3_4_0, -1.0_3_2_2, -1.1_1_4_9]] ) elif "Swin2SR_RealworldSR_X4_64_BSRGAN_PSNR" in checkpoint_url: UpperCamelCase = torch.Size([1, 3, 1024, 1024] ) UpperCamelCase = torch.tensor( [[-0.5_2_3_8, -0.5_5_5_7, -0.6_3_2_1], [-0.6_0_1_6, -0.5_9_0_3, -0.6_3_9_1], [-0.6_2_4_4, -0.6_3_3_4, -0.6_8_8_9]] ) assert ( outputs.reconstruction.shape == expected_shape ), f"""Shape of reconstruction should be {expected_shape}, but is {outputs.reconstruction.shape}""" assert torch.allclose(outputs.reconstruction[0, 0, :3, :3] , UpperCamelCase_ , atol=1E-3 ) print("""Looks ok!""" ) UpperCamelCase = { """https://github.com/mv-lab/swin2sr/releases/download/v0.0.1/Swin2SR_ClassicalSR_X2_64.pth""": ( """swin2SR-classical-sr-x2-64""" ), """https://github.com/mv-lab/swin2sr/releases/download/v0.0.1/Swin2SR_ClassicalSR_X4_64.pth""": ( """swin2SR-classical-sr-x4-64""" ), """https://github.com/mv-lab/swin2sr/releases/download/v0.0.1/Swin2SR_CompressedSR_X4_48.pth""": ( """swin2SR-compressed-sr-x4-48""" ), """https://github.com/mv-lab/swin2sr/releases/download/v0.0.1/Swin2SR_Lightweight_X2_64.pth""": ( """swin2SR-lightweight-x2-64""" ), """https://github.com/mv-lab/swin2sr/releases/download/v0.0.1/Swin2SR_RealworldSR_X4_64_BSRGAN_PSNR.pth""": ( """swin2SR-realworld-sr-x4-64-bsrgan-psnr""" ), } UpperCamelCase = url_to_name[checkpoint_url] if pytorch_dump_folder_path is not None: print(f"""Saving model {model_name} to {pytorch_dump_folder_path}""" ) model.save_pretrained(UpperCamelCase_ ) print(f"""Saving image processor to {pytorch_dump_folder_path}""" ) processor.save_pretrained(UpperCamelCase_ ) if push_to_hub: model.push_to_hub(f"""caidas/{model_name}""" ) processor.push_to_hub(f"""caidas/{model_name}""" ) if __name__ == "__main__": _SCREAMING_SNAKE_CASE = argparse.ArgumentParser() # Required parameters parser.add_argument( """--checkpoint_url""", default="""https://github.com/mv-lab/swin2sr/releases/download/v0.0.1/Swin2SR_ClassicalSR_X2_64.pth""", type=str, help="""URL of the original Swin2SR checkpoint you'd like to convert.""", ) parser.add_argument( """--pytorch_dump_folder_path""", default=None, type=str, help="""Path to the output PyTorch model directory.""" ) parser.add_argument("""--push_to_hub""", action="""store_true""", help="""Whether to push the converted model to the hub.""") _SCREAMING_SNAKE_CASE = parser.parse_args() convert_swinasr_checkpoint(args.checkpoint_url, args.pytorch_dump_folder_path, args.push_to_hub)
165
1
'''simple docstring''' import inspect import unittest from transformers import MobileNetVaConfig from transformers.testing_utils import require_torch, require_vision, slow, torch_device from transformers.utils import cached_property, is_torch_available, is_vision_available from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from transformers import MobileNetVaForImageClassification, MobileNetVaForSemanticSegmentation, MobileNetVaModel from transformers.models.mobilenet_va.modeling_mobilenet_va import MOBILENET_V2_PRETRAINED_MODEL_ARCHIVE_LIST if is_vision_available(): from PIL import Image from transformers import MobileNetVaImageProcessor class a_ (_a ): def __UpperCamelCase ( self ): _lowerCAmelCase : Tuple = self.config_class(**self.inputs_dict ) self.parent.assertTrue(hasattr(snake_case_ , """tf_padding""" ) ) self.parent.assertTrue(hasattr(snake_case_ , """depth_multiplier""" ) ) class a_ : def __init__( self , snake_case_ , snake_case_=1_3 , snake_case_=3 , snake_case_=3_2 , snake_case_=0.25 , snake_case_=8 , snake_case_=8 , snake_case_=6 , snake_case_=3_2 , snake_case_=True , snake_case_=True , snake_case_=True , snake_case_="relu6" , snake_case_=1_2_8_0 , snake_case_=0.1 , snake_case_=0.02 , snake_case_=True , snake_case_=True , snake_case_=1_0 , snake_case_=None , ): _lowerCAmelCase : Tuple = parent _lowerCAmelCase : Optional[Any] = batch_size _lowerCAmelCase : Dict = num_channels _lowerCAmelCase : Optional[int] = image_size _lowerCAmelCase : str = depth_multiplier _lowerCAmelCase : Optional[int] = depth_divisible_by _lowerCAmelCase : Optional[int] = min_depth _lowerCAmelCase : Optional[int] = expand_ratio _lowerCAmelCase : str = tf_padding _lowerCAmelCase : List[Any] = output_stride _lowerCAmelCase : Tuple = first_layer_is_expansion _lowerCAmelCase : Optional[int] = finegrained_output _lowerCAmelCase : int = hidden_act _lowerCAmelCase : Optional[Any] = last_hidden_size if finegrained_output else int(last_hidden_size * depth_multiplier ) _lowerCAmelCase : Dict = classifier_dropout_prob _lowerCAmelCase : List[Any] = use_labels _lowerCAmelCase : Optional[int] = is_training _lowerCAmelCase : Union[str, Any] = num_labels _lowerCAmelCase : List[str] = initializer_range _lowerCAmelCase : Union[str, Any] = scope def __UpperCamelCase ( self ): _lowerCAmelCase : Union[str, Any] = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) _lowerCAmelCase : Optional[int] = None _lowerCAmelCase : Dict = None if self.use_labels: _lowerCAmelCase : Any = ids_tensor([self.batch_size] , self.num_labels ) _lowerCAmelCase : Optional[int] = ids_tensor([self.batch_size, self.image_size, self.image_size] , self.num_labels ) _lowerCAmelCase : Optional[int] = self.get_config() return config, pixel_values, labels, pixel_labels def __UpperCamelCase ( self ): return MobileNetVaConfig( num_channels=self.num_channels , image_size=self.image_size , depth_multiplier=self.depth_multiplier , depth_divisible_by=self.depth_divisible_by , min_depth=self.min_depth , expand_ratio=self.expand_ratio , output_stride=self.output_stride , first_layer_is_expansion=self.first_layer_is_expansion , finegrained_output=self.finegrained_output , hidden_act=self.hidden_act , tf_padding=self.tf_padding , classifier_dropout_prob=self.classifier_dropout_prob , initializer_range=self.initializer_range , ) def __UpperCamelCase ( self , snake_case_ , snake_case_ , snake_case_ , snake_case_ ): _lowerCAmelCase : List[Any] = MobileNetVaModel(config=snake_case_ ) model.to(snake_case_ ) model.eval() _lowerCAmelCase : List[str] = model(snake_case_ ) self.parent.assertEqual( result.last_hidden_state.shape , ( self.batch_size, self.last_hidden_size, self.image_size // self.output_stride, self.image_size // self.output_stride, ) , ) self.parent.assertEqual( result.pooler_output.shape , (self.batch_size, self.last_hidden_size) , ) def __UpperCamelCase ( self , snake_case_ , snake_case_ , snake_case_ , snake_case_ ): _lowerCAmelCase : Union[str, Any] = self.num_labels _lowerCAmelCase : Union[str, Any] = MobileNetVaForImageClassification(snake_case_ ) model.to(snake_case_ ) model.eval() _lowerCAmelCase : str = model(snake_case_ , labels=snake_case_ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_labels) ) def __UpperCamelCase ( self , snake_case_ , snake_case_ , snake_case_ , snake_case_ ): _lowerCAmelCase : List[Any] = self.num_labels _lowerCAmelCase : Optional[int] = MobileNetVaForSemanticSegmentation(snake_case_ ) model.to(snake_case_ ) model.eval() _lowerCAmelCase : Optional[int] = model(snake_case_ ) self.parent.assertEqual( result.logits.shape , ( self.batch_size, self.num_labels, self.image_size // self.output_stride, self.image_size // self.output_stride, ) , ) _lowerCAmelCase : int = model(snake_case_ , labels=snake_case_ ) self.parent.assertEqual( result.logits.shape , ( self.batch_size, self.num_labels, self.image_size // self.output_stride, self.image_size // self.output_stride, ) , ) def __UpperCamelCase ( self ): _lowerCAmelCase : str = self.prepare_config_and_inputs() _lowerCAmelCase , _lowerCAmelCase , _lowerCAmelCase , _lowerCAmelCase : Optional[int] = config_and_inputs _lowerCAmelCase : Optional[Any] = {"""pixel_values""": pixel_values} return config, inputs_dict @require_torch class a_ (_a , _a , unittest.TestCase ): __lowerCAmelCase : Dict = ( (MobileNetVaModel, MobileNetVaForImageClassification, MobileNetVaForSemanticSegmentation) if is_torch_available() else () ) __lowerCAmelCase : int = ( { """feature-extraction""": MobileNetVaModel, """image-classification""": MobileNetVaForImageClassification, """image-segmentation""": MobileNetVaForSemanticSegmentation, } if is_torch_available() else {} ) __lowerCAmelCase : str = False __lowerCAmelCase : List[str] = False __lowerCAmelCase : Any = False __lowerCAmelCase : Dict = False def __UpperCamelCase ( self ): _lowerCAmelCase : Optional[Any] = MobileNetVaModelTester(self ) _lowerCAmelCase : List[str] = MobileNetVaConfigTester(self , config_class=snake_case_ , has_text_modality=snake_case_ ) def __UpperCamelCase ( self ): self.config_tester.run_common_tests() @unittest.skip(reason="""MobileNetV2 does not use inputs_embeds""" ) def __UpperCamelCase ( self ): pass @unittest.skip(reason="""MobileNetV2 does not support input and output embeddings""" ) def __UpperCamelCase ( self ): pass @unittest.skip(reason="""MobileNetV2 does not output attentions""" ) def __UpperCamelCase ( self ): pass def __UpperCamelCase ( self ): _lowerCAmelCase , _lowerCAmelCase : Union[str, Any] = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: _lowerCAmelCase : Optional[Any] = model_class(snake_case_ ) _lowerCAmelCase : Dict = inspect.signature(model.forward ) # signature.parameters is an OrderedDict => so arg_names order is deterministic _lowerCAmelCase : Tuple = [*signature.parameters.keys()] _lowerCAmelCase : List[Any] = ["""pixel_values"""] self.assertListEqual(arg_names[:1] , snake_case_ ) def __UpperCamelCase ( self ): _lowerCAmelCase : Dict = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*snake_case_ ) def __UpperCamelCase ( self ): def check_hidden_states_output(snake_case_ , snake_case_ , snake_case_ ): _lowerCAmelCase : str = model_class(snake_case_ ) model.to(snake_case_ ) model.eval() with torch.no_grad(): _lowerCAmelCase : str = model(**self._prepare_for_class(snake_case_ , snake_case_ ) ) _lowerCAmelCase : int = outputs.hidden_states _lowerCAmelCase : Any = 1_6 self.assertEqual(len(snake_case_ ) , snake_case_ ) _lowerCAmelCase , _lowerCAmelCase : Dict = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: _lowerCAmelCase : Tuple = True check_hidden_states_output(snake_case_ , snake_case_ , snake_case_ ) # check that output_hidden_states also work using config del inputs_dict["output_hidden_states"] _lowerCAmelCase : List[Any] = True check_hidden_states_output(snake_case_ , snake_case_ , snake_case_ ) def __UpperCamelCase ( self ): _lowerCAmelCase : Optional[Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_image_classification(*snake_case_ ) def __UpperCamelCase ( self ): _lowerCAmelCase : Optional[int] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_semantic_segmentation(*snake_case_ ) @slow def __UpperCamelCase ( self ): for model_name in MOBILENET_V2_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: _lowerCAmelCase : Any = MobileNetVaModel.from_pretrained(snake_case_ ) self.assertIsNotNone(snake_case_ ) def _UpperCAmelCase ( ) -> List[str]: _lowerCAmelCase : Optional[int] = Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png""" ) return image @require_torch @require_vision class a_ (unittest.TestCase ): @cached_property def __UpperCamelCase ( self ): return ( MobileNetVaImageProcessor.from_pretrained("""google/mobilenet_v2_1.0_224""" ) if is_vision_available() else None ) @slow def __UpperCamelCase ( self ): _lowerCAmelCase : Optional[Any] = MobileNetVaForImageClassification.from_pretrained("""google/mobilenet_v2_1.0_224""" ).to(snake_case_ ) _lowerCAmelCase : Union[str, Any] = self.default_image_processor _lowerCAmelCase : Union[str, Any] = prepare_img() _lowerCAmelCase : Tuple = image_processor(images=snake_case_ , return_tensors="""pt""" ).to(snake_case_ ) # forward pass with torch.no_grad(): _lowerCAmelCase : str = model(**snake_case_ ) # verify the logits _lowerCAmelCase : Any = torch.Size((1, 1_0_0_1) ) self.assertEqual(outputs.logits.shape , snake_case_ ) _lowerCAmelCase : int = torch.tensor([0.2445, -1.1993, 0.1905] ).to(snake_case_ ) self.assertTrue(torch.allclose(outputs.logits[0, :3] , snake_case_ , atol=1E-4 ) ) @slow def __UpperCamelCase ( self ): _lowerCAmelCase : str = MobileNetVaForSemanticSegmentation.from_pretrained("""google/deeplabv3_mobilenet_v2_1.0_513""" ) _lowerCAmelCase : Dict = model.to(snake_case_ ) _lowerCAmelCase : Optional[Any] = MobileNetVaImageProcessor.from_pretrained("""google/deeplabv3_mobilenet_v2_1.0_513""" ) _lowerCAmelCase : Union[str, Any] = prepare_img() _lowerCAmelCase : Dict = image_processor(images=snake_case_ , return_tensors="""pt""" ).to(snake_case_ ) # forward pass with torch.no_grad(): _lowerCAmelCase : int = model(**snake_case_ ) _lowerCAmelCase : List[Any] = outputs.logits # verify the logits _lowerCAmelCase : Optional[int] = torch.Size((1, 2_1, 6_5, 6_5) ) self.assertEqual(logits.shape , snake_case_ ) _lowerCAmelCase : str = torch.tensor( [ [[17.5790, 17.7581, 18.3355], [18.3257, 18.4230, 18.8973], [18.6169, 18.8650, 19.2187]], [[-2.1595, -2.0977, -2.3741], [-2.4226, -2.3028, -2.6835], [-2.7819, -2.5991, -2.7706]], [[4.2058, 4.8317, 4.7638], [4.4136, 5.0361, 4.9383], [4.5028, 4.9644, 4.8734]], ] , device=snake_case_ , ) self.assertTrue(torch.allclose(logits[0, :3, :3, :3] , snake_case_ , atol=1E-4 ) )
309
'''simple docstring''' import argparse import requests import torch # pip3 install salesforce-lavis # I'm actually installing a slightly modified version: pip3 install git+https://github.com/nielsrogge/LAVIS.git@fix_lavis from lavis.models import load_model_and_preprocess from PIL import Image from transformers import ( AutoTokenizer, BlipaConfig, BlipaForConditionalGeneration, BlipaProcessor, BlipaVisionConfig, BlipImageProcessor, OPTConfig, TaConfig, ) from transformers.utils.constants import OPENAI_CLIP_MEAN, OPENAI_CLIP_STD def _UpperCAmelCase ( ) -> Tuple: _lowerCAmelCase : List[Any] = """https://storage.googleapis.com/sfr-vision-language-research/LAVIS/assets/merlion.png""" _lowerCAmelCase : int = Image.open(requests.get(_lowerCamelCase , stream=_lowerCamelCase ).raw ).convert("""RGB""" ) return image def _UpperCAmelCase ( _lowerCamelCase : Any ) -> Dict: _lowerCAmelCase : str = [] # fmt: off # vision encoder rename_keys.append(("""visual_encoder.cls_token""", """vision_model.embeddings.class_embedding""") ) rename_keys.append(("""visual_encoder.pos_embed""", """vision_model.embeddings.position_embedding""") ) rename_keys.append(("""visual_encoder.patch_embed.proj.weight""", """vision_model.embeddings.patch_embedding.weight""") ) rename_keys.append(("""visual_encoder.patch_embed.proj.bias""", """vision_model.embeddings.patch_embedding.bias""") ) rename_keys.append(("""ln_vision.weight""", """vision_model.post_layernorm.weight""") ) rename_keys.append(("""ln_vision.bias""", """vision_model.post_layernorm.bias""") ) for i in range(config.vision_config.num_hidden_layers ): rename_keys.append((f'visual_encoder.blocks.{i}.norm1.weight', f'vision_model.encoder.layers.{i}.layer_norm1.weight') ) rename_keys.append((f'visual_encoder.blocks.{i}.norm1.bias', f'vision_model.encoder.layers.{i}.layer_norm1.bias') ) rename_keys.append((f'visual_encoder.blocks.{i}.norm2.weight', f'vision_model.encoder.layers.{i}.layer_norm2.weight') ) rename_keys.append((f'visual_encoder.blocks.{i}.norm2.bias', f'vision_model.encoder.layers.{i}.layer_norm2.bias') ) rename_keys.append((f'visual_encoder.blocks.{i}.attn.qkv.weight', f'vision_model.encoder.layers.{i}.self_attn.qkv.weight') ) rename_keys.append((f'visual_encoder.blocks.{i}.attn.proj.weight', f'vision_model.encoder.layers.{i}.self_attn.projection.weight',) ) rename_keys.append((f'visual_encoder.blocks.{i}.attn.proj.bias', f'vision_model.encoder.layers.{i}.self_attn.projection.bias') ) rename_keys.append((f'visual_encoder.blocks.{i}.mlp.fc1.weight', f'vision_model.encoder.layers.{i}.mlp.fc1.weight') ) rename_keys.append((f'visual_encoder.blocks.{i}.mlp.fc1.bias', f'vision_model.encoder.layers.{i}.mlp.fc1.bias') ) rename_keys.append((f'visual_encoder.blocks.{i}.mlp.fc2.weight', f'vision_model.encoder.layers.{i}.mlp.fc2.weight') ) rename_keys.append((f'visual_encoder.blocks.{i}.mlp.fc2.bias', f'vision_model.encoder.layers.{i}.mlp.fc2.bias') ) # QFormer rename_keys.append(("""Qformer.bert.embeddings.LayerNorm.weight""", """qformer.layernorm.weight""") ) rename_keys.append(("""Qformer.bert.embeddings.LayerNorm.bias""", """qformer.layernorm.bias""") ) # fmt: on return rename_keys def _UpperCAmelCase ( _lowerCamelCase : List[Any] , _lowerCamelCase : Optional[int] , _lowerCamelCase : List[Any] ) -> Optional[Any]: _lowerCAmelCase : str = dct.pop(_lowerCamelCase ) _lowerCAmelCase : str = val def _UpperCAmelCase ( _lowerCamelCase : Union[str, Any] , _lowerCamelCase : Tuple ) -> Tuple: for i in range(config.vision_config.num_hidden_layers ): # read in original q and v biases _lowerCAmelCase : Tuple = state_dict.pop(f'visual_encoder.blocks.{i}.attn.q_bias' ) _lowerCAmelCase : Optional[Any] = state_dict.pop(f'visual_encoder.blocks.{i}.attn.v_bias' ) # next, set bias in the state dict _lowerCAmelCase : int = torch.cat((q_bias, torch.zeros_like(_lowerCamelCase , requires_grad=_lowerCamelCase ), v_bias) ) _lowerCAmelCase : str = qkv_bias def _UpperCAmelCase ( _lowerCamelCase : Optional[Any] , _lowerCamelCase : Union[str, Any] ) -> List[Any]: _lowerCAmelCase : str = 3_64 if """coco""" in model_name else 2_24 _lowerCAmelCase : str = BlipaVisionConfig(image_size=_lowerCamelCase ).to_dict() # make sure the models have proper bos_token_id and eos_token_id set (important for generation) # seems like flan-T5 models don't have bos_token_id properly set? if "opt-2.7b" in model_name: _lowerCAmelCase : int = OPTConfig.from_pretrained("""facebook/opt-2.7b""" , eos_token_id=_lowerCamelCase ).to_dict() elif "opt-6.7b" in model_name: _lowerCAmelCase : Union[str, Any] = OPTConfig.from_pretrained("""facebook/opt-6.7b""" , eos_token_id=_lowerCamelCase ).to_dict() elif "t5-xl" in model_name: _lowerCAmelCase : Optional[int] = TaConfig.from_pretrained("""google/flan-t5-xl""" , dense_act_fn="""gelu""" , bos_token_id=1 ).to_dict() elif "t5-xxl" in model_name: _lowerCAmelCase : str = TaConfig.from_pretrained("""google/flan-t5-xxl""" , dense_act_fn="""gelu""" , bos_token_id=1 ).to_dict() _lowerCAmelCase : Dict = BlipaConfig(vision_config=_lowerCamelCase , text_config=_lowerCamelCase ) return config, image_size @torch.no_grad() def _UpperCAmelCase ( _lowerCamelCase : Tuple , _lowerCamelCase : List[Any]=None , _lowerCamelCase : int=False ) -> List[str]: _lowerCAmelCase : int = ( AutoTokenizer.from_pretrained("""facebook/opt-2.7b""" ) if """opt""" in model_name else AutoTokenizer.from_pretrained("""google/flan-t5-xl""" ) ) _lowerCAmelCase : List[Any] = tokenizer("""\n""" , add_special_tokens=_lowerCamelCase ).input_ids[0] _lowerCAmelCase , _lowerCAmelCase : List[str] = get_blipa_config(_lowerCamelCase , eos_token_id=_lowerCamelCase ) _lowerCAmelCase : Optional[int] = BlipaForConditionalGeneration(_lowerCamelCase ).eval() _lowerCAmelCase : Union[str, Any] = { """blip2-opt-2.7b""": ("""blip2_opt""", """pretrain_opt2.7b"""), """blip2-opt-6.7b""": ("""blip2_opt""", """pretrain_opt6.7b"""), """blip2-opt-2.7b-coco""": ("""blip2_opt""", """caption_coco_opt2.7b"""), """blip2-opt-6.7b-coco""": ("""blip2_opt""", """caption_coco_opt6.7b"""), """blip2-flan-t5-xl""": ("""blip2_t5""", """pretrain_flant5xl"""), """blip2-flan-t5-xl-coco""": ("""blip2_t5""", """caption_coco_flant5xl"""), """blip2-flan-t5-xxl""": ("""blip2_t5""", """pretrain_flant5xxl"""), } _lowerCAmelCase , _lowerCAmelCase : List[str] = model_name_to_original[model_name] # load original model print("""Loading original model...""" ) _lowerCAmelCase : Dict = """cuda""" if torch.cuda.is_available() else """cpu""" _lowerCAmelCase , _lowerCAmelCase , _lowerCAmelCase : Optional[int] = load_model_and_preprocess( name=_lowerCamelCase , model_type=_lowerCamelCase , is_eval=_lowerCamelCase , device=_lowerCamelCase ) original_model.eval() print("""Done!""" ) # update state dict keys _lowerCAmelCase : List[Any] = original_model.state_dict() _lowerCAmelCase : Optional[int] = create_rename_keys(_lowerCamelCase ) for src, dest in rename_keys: rename_key(_lowerCamelCase , _lowerCamelCase , _lowerCamelCase ) # some keys can be renamed efficiently for key, val in state_dict.copy().items(): _lowerCAmelCase : Tuple = state_dict.pop(_lowerCamelCase ) if key.startswith("""Qformer.bert""" ): _lowerCAmelCase : List[Any] = key.replace("""Qformer.bert""" , """qformer""" ) if "attention.self" in key: _lowerCAmelCase : Optional[int] = key.replace("""self""" , """attention""" ) if "opt_proj" in key: _lowerCAmelCase : Dict = key.replace("""opt_proj""" , """language_projection""" ) if "t5_proj" in key: _lowerCAmelCase : Tuple = key.replace("""t5_proj""" , """language_projection""" ) if key.startswith("""opt""" ): _lowerCAmelCase : List[Any] = key.replace("""opt""" , """language""" ) if key.startswith("""t5""" ): _lowerCAmelCase : int = key.replace("""t5""" , """language""" ) _lowerCAmelCase : Tuple = val # read in qv biases read_in_q_v_bias(_lowerCamelCase , _lowerCamelCase ) _lowerCAmelCase , _lowerCAmelCase : Optional[int] = hf_model.load_state_dict(_lowerCamelCase , strict=_lowerCamelCase ) assert len(_lowerCamelCase ) == 0 assert unexpected_keys == ["qformer.embeddings.position_ids"] _lowerCAmelCase : Union[str, Any] = load_demo_image() _lowerCAmelCase : Optional[int] = vis_processors["""eval"""](_lowerCamelCase ).unsqueeze(0 ).to(_lowerCamelCase ) _lowerCAmelCase : List[str] = tokenizer(["""\n"""] , return_tensors="""pt""" ).input_ids.to(_lowerCamelCase ) # create processor _lowerCAmelCase : Optional[int] = BlipImageProcessor( size={"""height""": image_size, """width""": image_size} , image_mean=_lowerCamelCase , image_std=_lowerCamelCase ) _lowerCAmelCase : Tuple = BlipaProcessor(image_processor=_lowerCamelCase , tokenizer=_lowerCamelCase ) _lowerCAmelCase : Any = processor(images=_lowerCamelCase , return_tensors="""pt""" ).pixel_values.to(_lowerCamelCase ) # make sure processor creates exact same pixel values assert torch.allclose(_lowerCamelCase , _lowerCamelCase ) original_model.to(_lowerCamelCase ) hf_model.to(_lowerCamelCase ) with torch.no_grad(): if "opt" in model_name: _lowerCAmelCase : Optional[Any] = original_model({"""image""": original_pixel_values, """text_input""": [""""""]} ).logits _lowerCAmelCase : Optional[Any] = hf_model(_lowerCamelCase , _lowerCamelCase ).logits else: _lowerCAmelCase : List[Any] = original_model( {"""image""": original_pixel_values, """text_input""": ["""\n"""], """text_output""": ["""\n"""]} ).logits _lowerCAmelCase : Tuple = input_ids.masked_fill(input_ids == tokenizer.pad_token_id , -1_00 ) _lowerCAmelCase : Dict = hf_model(_lowerCamelCase , _lowerCamelCase , labels=_lowerCamelCase ).logits assert original_logits.shape == logits.shape print("""First values of original logits:""" , original_logits[0, :3, :3] ) print("""First values of HF logits:""" , logits[0, :3, :3] ) # assert values if model_name == "blip2-flan-t5-xl": _lowerCAmelCase : Any = torch.tensor( [[-41.5850, -4.4440, -8.9922], [-47.4322, -5.9143, -1.7340]] , device=_lowerCamelCase ) assert torch.allclose(logits[0, :3, :3] , _lowerCamelCase , atol=1e-4 ) elif model_name == "blip2-flan-t5-xl-coco": _lowerCAmelCase : List[Any] = torch.tensor( [[-57.0109, -9.8967, -12.6280], [-68.6578, -12.7191, -10.5065]] , device=_lowerCamelCase ) else: # cast to same type _lowerCAmelCase : Union[str, Any] = logits.dtype assert torch.allclose(original_logits.to(_lowerCamelCase ) , _lowerCamelCase , atol=1e-2 ) print("""Looks ok!""" ) print("""Generating a caption...""" ) _lowerCAmelCase : Optional[int] = """""" _lowerCAmelCase : Union[str, Any] = tokenizer(_lowerCamelCase , return_tensors="""pt""" ).input_ids.to(_lowerCamelCase ) _lowerCAmelCase : List[Any] = original_model.generate({"""image""": original_pixel_values} ) _lowerCAmelCase : Dict = hf_model.generate( _lowerCamelCase , _lowerCamelCase , do_sample=_lowerCamelCase , num_beams=5 , max_length=30 , min_length=1 , top_p=0.9 , repetition_penalty=1.0 , length_penalty=1.0 , temperature=1 , ) print("""Original generation:""" , _lowerCamelCase ) _lowerCAmelCase : int = input_ids.shape[1] _lowerCAmelCase : str = processor.batch_decode(outputs[:, prompt_length:] , skip_special_tokens=_lowerCamelCase ) _lowerCAmelCase : List[str] = [text.strip() for text in output_text] print("""HF generation:""" , _lowerCamelCase ) if pytorch_dump_folder_path is not None: processor.save_pretrained(_lowerCamelCase ) hf_model.save_pretrained(_lowerCamelCase ) if push_to_hub: processor.push_to_hub(f'nielsr/{model_name}' ) hf_model.push_to_hub(f'nielsr/{model_name}' ) if __name__ == "__main__": UpperCamelCase_ = argparse.ArgumentParser() UpperCamelCase_ = [ """blip2-opt-2.7b""", """blip2-opt-6.7b""", """blip2-opt-2.7b-coco""", """blip2-opt-6.7b-coco""", """blip2-flan-t5-xl""", """blip2-flan-t5-xl-coco""", """blip2-flan-t5-xxl""", ] parser.add_argument( """--model_name""", default="""blip2-opt-2.7b""", choices=choices, type=str, help="""Path to hf config.json of model to convert""", ) parser.add_argument("""--pytorch_dump_folder_path""", default=None, type=str, help="""Path to the output PyTorch model.""") parser.add_argument( """--push_to_hub""", action="""store_true""", help="""Whether to push the model and processor to the hub after converting""", ) UpperCamelCase_ = parser.parse_args() convert_blipa_checkpoint(args.model_name, args.pytorch_dump_folder_path, args.push_to_hub)
309
1
import gc import random import unittest import numpy as np import torch from transformers import CLIPTextConfig, CLIPTextModel, CLIPTokenizer import diffusers from diffusers import ( AutoencoderKL, EulerDiscreteScheduler, StableDiffusionLatentUpscalePipeline, StableDiffusionPipeline, UNetaDConditionModel, ) from diffusers.schedulers import KarrasDiffusionSchedulers from diffusers.utils import floats_tensor, load_image, load_numpy, slow, torch_device from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu from ..pipeline_params import TEXT_GUIDED_IMAGE_VARIATION_BATCH_PARAMS, TEXT_GUIDED_IMAGE_VARIATION_PARAMS from ..test_pipelines_common import PipelineKarrasSchedulerTesterMixin, PipelineLatentTesterMixin, PipelineTesterMixin enable_full_determinism() def A_ ( _lowerCAmelCase ) -> Dict: UpperCamelCase : Any = [tensor.shape for tensor in tensor_list] return all(shape == shapes[0] for shape in shapes[1:] ) class A__ ( __snake_case , __snake_case , __snake_case , unittest.TestCase ): _UpperCAmelCase :List[str] = StableDiffusionLatentUpscalePipeline _UpperCAmelCase :Union[str, Any] = TEXT_GUIDED_IMAGE_VARIATION_PARAMS - { 'height', 'width', 'cross_attention_kwargs', 'negative_prompt_embeds', 'prompt_embeds', } _UpperCAmelCase :Optional[int] = PipelineTesterMixin.required_optional_params - {'num_images_per_prompt'} _UpperCAmelCase :List[Any] = TEXT_GUIDED_IMAGE_VARIATION_BATCH_PARAMS _UpperCAmelCase :Union[str, Any] = frozenset( [] ) # TO-DO: update image_params once pipeline is refactored with VaeImageProcessor.preprocess _UpperCAmelCase :int = frozenset([] ) _UpperCAmelCase :Any = True @property def __UpperCamelCase( self ): '''simple docstring''' UpperCamelCase : List[Any] = 1 UpperCamelCase : Dict = 4 UpperCamelCase : Union[str, Any] = (16, 16) UpperCamelCase : Union[str, Any] = floats_tensor((batch_size, num_channels) + sizes , rng=random.Random(0 ) ).to(A_ ) return image def __UpperCamelCase( self ): '''simple docstring''' torch.manual_seed(0 ) UpperCamelCase : List[str] = UNetaDConditionModel( act_fn="gelu" , attention_head_dim=8 , norm_num_groups=A_ , block_out_channels=[32, 32, 64, 64] , time_cond_proj_dim=160 , conv_in_kernel=1 , conv_out_kernel=1 , cross_attention_dim=32 , down_block_types=( "KDownBlock2D", "KCrossAttnDownBlock2D", "KCrossAttnDownBlock2D", "KCrossAttnDownBlock2D", ) , in_channels=8 , mid_block_type=A_ , only_cross_attention=A_ , out_channels=5 , resnet_time_scale_shift="scale_shift" , time_embedding_type="fourier" , timestep_post_act="gelu" , up_block_types=("KCrossAttnUpBlock2D", "KCrossAttnUpBlock2D", "KCrossAttnUpBlock2D", "KUpBlock2D") , ) UpperCamelCase : Tuple = AutoencoderKL( block_out_channels=[32, 32, 64, 64] , in_channels=3 , out_channels=3 , down_block_types=[ "DownEncoderBlock2D", "DownEncoderBlock2D", "DownEncoderBlock2D", "DownEncoderBlock2D", ] , up_block_types=["UpDecoderBlock2D", "UpDecoderBlock2D", "UpDecoderBlock2D", "UpDecoderBlock2D"] , latent_channels=4 , ) UpperCamelCase : List[Any] = EulerDiscreteScheduler(prediction_type="sample" ) UpperCamelCase : Dict = CLIPTextConfig( bos_token_id=0 , eos_token_id=2 , hidden_size=32 , intermediate_size=37 , layer_norm_eps=1e-05 , num_attention_heads=4 , num_hidden_layers=5 , pad_token_id=1 , vocab_size=1000 , hidden_act="quick_gelu" , projection_dim=512 , ) UpperCamelCase : Union[str, Any] = CLIPTextModel(A_ ) UpperCamelCase : List[str] = CLIPTokenizer.from_pretrained("hf-internal-testing/tiny-random-clip" ) UpperCamelCase : Dict = { "unet": model.eval(), "vae": vae.eval(), "scheduler": scheduler, "text_encoder": text_encoder, "tokenizer": tokenizer, } return components def __UpperCamelCase( self , A_ , A_=0 ): '''simple docstring''' if str(A_ ).startswith("mps" ): UpperCamelCase : Dict = torch.manual_seed(A_ ) else: UpperCamelCase : Optional[int] = torch.Generator(device=A_ ).manual_seed(A_ ) UpperCamelCase : Union[str, Any] = { "prompt": "A painting of a squirrel eating a burger", "image": self.dummy_image.cpu(), "generator": generator, "num_inference_steps": 2, "output_type": "numpy", } return inputs def __UpperCamelCase( self ): '''simple docstring''' UpperCamelCase : Tuple = "cpu" UpperCamelCase : str = self.get_dummy_components() UpperCamelCase : List[Any] = self.pipeline_class(**A_ ) pipe.to(A_ ) pipe.set_progress_bar_config(disable=A_ ) UpperCamelCase : str = self.get_dummy_inputs(A_ ) UpperCamelCase : Dict = pipe(**A_ ).images UpperCamelCase : List[str] = image[0, -3:, -3:, -1] self.assertEqual(image.shape , (1, 256, 256, 3) ) UpperCamelCase : Union[str, Any] = np.array( [0.47_22_24_12, 0.41_92_16_33, 0.44_71_74_34, 0.46_87_41_92, 0.42_58_82_58, 0.46_15_07_26, 0.4_67_75_34, 0.45_58_38_32, 0.48_57_90_55] ) UpperCamelCase : Tuple = np.abs(image_slice.flatten() - expected_slice ).max() self.assertLessEqual(A_ , 1e-3 ) def __UpperCamelCase( self ): '''simple docstring''' super().test_attention_slicing_forward_pass(expected_max_diff=7e-3 ) def __UpperCamelCase( self ): '''simple docstring''' super().test_cpu_offload_forward_pass(expected_max_diff=3e-3 ) def __UpperCamelCase( self ): '''simple docstring''' super().test_dict_tuple_outputs_equivalent(expected_max_difference=3e-3 ) def __UpperCamelCase( self ): '''simple docstring''' super().test_inference_batch_single_identical(expected_max_diff=7e-3 ) def __UpperCamelCase( self ): '''simple docstring''' super().test_pt_np_pil_outputs_equivalent(expected_max_diff=3e-3 ) def __UpperCamelCase( self ): '''simple docstring''' super().test_save_load_local(expected_max_difference=3e-3 ) def __UpperCamelCase( self ): '''simple docstring''' super().test_save_load_optional_components(expected_max_difference=3e-3 ) def __UpperCamelCase( self ): '''simple docstring''' UpperCamelCase : str = [ "DDIMScheduler", "DDPMScheduler", "PNDMScheduler", "HeunDiscreteScheduler", "EulerAncestralDiscreteScheduler", "KDPM2DiscreteScheduler", "KDPM2AncestralDiscreteScheduler", "DPMSolverSDEScheduler", ] UpperCamelCase : int = self.get_dummy_components() UpperCamelCase : Tuple = self.pipeline_class(**A_ ) # make sure that PNDM does not need warm-up pipe.scheduler.register_to_config(skip_prk_steps=A_ ) pipe.to(A_ ) pipe.set_progress_bar_config(disable=A_ ) UpperCamelCase : Dict = self.get_dummy_inputs(A_ ) UpperCamelCase : Optional[Any] = 2 UpperCamelCase : Tuple = [] for scheduler_enum in KarrasDiffusionSchedulers: if scheduler_enum.name in skip_schedulers: # no sigma schedulers are not supported # no schedulers continue UpperCamelCase : Optional[Any] = getattr(A_ , scheduler_enum.name ) UpperCamelCase : Dict = scheduler_cls.from_config(pipe.scheduler.config ) UpperCamelCase : int = pipe(**A_ )[0] outputs.append(A_ ) assert check_same_shape(A_ ) @require_torch_gpu @slow class A__ ( unittest.TestCase ): def __UpperCamelCase( self ): '''simple docstring''' super().tearDown() gc.collect() torch.cuda.empty_cache() def __UpperCamelCase( self ): '''simple docstring''' UpperCamelCase : int = torch.manual_seed(33 ) UpperCamelCase : Tuple = StableDiffusionPipeline.from_pretrained("CompVis/stable-diffusion-v1-4" , torch_dtype=torch.floataa ) pipe.to("cuda" ) UpperCamelCase : List[str] = StableDiffusionLatentUpscalePipeline.from_pretrained( "stabilityai/sd-x2-latent-upscaler" , torch_dtype=torch.floataa ) upscaler.to("cuda" ) UpperCamelCase : Tuple = "a photo of an astronaut high resolution, unreal engine, ultra realistic" UpperCamelCase : Tuple = pipe(A_ , generator=A_ , output_type="latent" ).images UpperCamelCase : Union[str, Any] = upscaler( prompt=A_ , image=A_ , num_inference_steps=20 , guidance_scale=0 , generator=A_ , output_type="np" , ).images[0] UpperCamelCase : Tuple = load_numpy( "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/latent-upscaler/astronaut_1024.npy" ) assert np.abs((expected_image - image).mean() ) < 5e-2 def __UpperCamelCase( self ): '''simple docstring''' UpperCamelCase : List[Any] = torch.manual_seed(33 ) UpperCamelCase : List[Any] = StableDiffusionLatentUpscalePipeline.from_pretrained( "stabilityai/sd-x2-latent-upscaler" , torch_dtype=torch.floataa ) upscaler.to("cuda" ) UpperCamelCase : Dict = "the temple of fire by Ross Tran and Gerardo Dottori, oil on canvas" UpperCamelCase : int = load_image( "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/latent-upscaler/fire_temple_512.png" ) UpperCamelCase : int = upscaler( prompt=A_ , image=A_ , num_inference_steps=20 , guidance_scale=0 , generator=A_ , output_type="np" , ).images[0] UpperCamelCase : str = load_numpy( "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/latent-upscaler/fire_temple_1024.npy" ) assert np.abs((expected_image - image).max() ) < 5e-2
140
import os def A_ ( ) -> int: UpperCamelCase : List[str] = os.path.dirname(os.path.realpath(_lowerCAmelCase ) ) UpperCamelCase : Any = os.path.join(_lowerCAmelCase , "triangle.txt" ) with open(_lowerCAmelCase ) as f: UpperCamelCase : Optional[Any] = f.readlines() UpperCamelCase : Tuple = [] for line in triangle: UpperCamelCase : List[str] = [] for number in line.strip().split(" " ): numbers_from_line.append(int(_lowerCAmelCase ) ) a.append(_lowerCAmelCase ) for i in range(1 , len(_lowerCAmelCase ) ): for j in range(len(a[i] ) ): UpperCamelCase : List[Any] = a[i - 1][j] if j != len(a[i - 1] ) else 0 UpperCamelCase : Dict = a[i - 1][j - 1] if j > 0 else 0 a[i][j] += max(_lowerCAmelCase , _lowerCAmelCase ) return max(a[-1] ) if __name__ == "__main__": print(solution())
140
1
import numpy as np # Importing the Keras libraries and packages import tensorflow as tf from tensorflow.keras import layers, models if __name__ == "__main__": # Initialising the CNN # (Sequential- Building the model layer by layer) _UpperCamelCase = models.Sequential() # Step 1 - Convolution # Here 64,64 is the length & breadth of dataset images and 3 is for the RGB channel # (3,3) is the kernel size (filter matrix) classifier.add( layers.ConvaD(32, (3, 3), input_shape=(64, 64, 3), activation='''relu''') ) # Step 2 - Pooling classifier.add(layers.MaxPoolingaD(pool_size=(2, 2))) # Adding a second convolutional layer classifier.add(layers.ConvaD(32, (3, 3), activation='''relu''')) classifier.add(layers.MaxPoolingaD(pool_size=(2, 2))) # Step 3 - Flattening classifier.add(layers.Flatten()) # Step 4 - Full connection classifier.add(layers.Dense(units=128, activation='''relu''')) classifier.add(layers.Dense(units=1, activation='''sigmoid''')) # Compiling the CNN classifier.compile( optimizer='''adam''', loss='''binary_crossentropy''', metrics=['''accuracy'''] ) # Part 2 - Fitting the CNN to the images # Load Trained model weights # from keras.models import load_model # regressor=load_model('cnn.h5') _UpperCamelCase = tf.keras.preprocessing.image.ImageDataGenerator( rescale=1.0 / 255, shear_range=0.2, zoom_range=0.2, horizontal_flip=True ) _UpperCamelCase = tf.keras.preprocessing.image.ImageDataGenerator(rescale=1.0 / 255) _UpperCamelCase = train_datagen.flow_from_directory( '''dataset/training_set''', target_size=(64, 64), batch_size=32, class_mode='''binary''' ) _UpperCamelCase = test_datagen.flow_from_directory( '''dataset/test_set''', target_size=(64, 64), batch_size=32, class_mode='''binary''' ) classifier.fit_generator( training_set, steps_per_epoch=5, epochs=30, validation_data=test_set ) classifier.save('''cnn.h5''') # Part 3 - Making new predictions _UpperCamelCase = tf.keras.preprocessing.image.load_img( '''dataset/single_prediction/image.png''', target_size=(64, 64) ) _UpperCamelCase = tf.keras.preprocessing.image.img_to_array(test_image) _UpperCamelCase = np.expand_dims(test_image, axis=0) _UpperCamelCase = classifier.predict(test_image) # training_set.class_indices if result[0][0] == 0: _UpperCamelCase = '''Normal''' if result[0][0] == 1: _UpperCamelCase = '''Abnormality detected'''
326
"""simple docstring""" import itertools from dataclasses import dataclass from typing import Any, Callable, Dict, List, Optional, Union import pandas as pd import pyarrow as pa import datasets import datasets.config from datasets.features.features import require_storage_cast from datasets.table import table_cast from datasets.utils.py_utils import Literal _a = datasets.utils.logging.get_logger(__name__) _a = ['names', 'prefix'] _a = ['warn_bad_lines', 'error_bad_lines', 'mangle_dupe_cols'] _a = ['encoding_errors', 'on_bad_lines'] _a = ['date_format'] @dataclass class _lowerCAmelCase ( datasets.BuilderConfig ): """simple docstring""" __UpperCAmelCase : str = "," __UpperCAmelCase : Optional[str] = None __UpperCAmelCase : Optional[Union[int, List[int], str]] = "infer" __UpperCAmelCase : Optional[List[str]] = None __UpperCAmelCase : Optional[List[str]] = None __UpperCAmelCase : Optional[Union[int, str, List[int], List[str]]] = None __UpperCAmelCase : Optional[Union[List[int], List[str]]] = None __UpperCAmelCase : Optional[str] = None __UpperCAmelCase : bool = True __UpperCAmelCase : Optional[Literal["c", "python", "pyarrow"]] = None __UpperCAmelCase : Dict[Union[int, str], Callable[[Any], Any]] = None __UpperCAmelCase : Optional[list] = None __UpperCAmelCase : Optional[list] = None __UpperCAmelCase : bool = False __UpperCAmelCase : Optional[Union[int, List[int]]] = None __UpperCAmelCase : Optional[int] = None __UpperCAmelCase : Optional[Union[str, List[str]]] = None __UpperCAmelCase : bool = True __UpperCAmelCase : bool = True __UpperCAmelCase : bool = False __UpperCAmelCase : bool = True __UpperCAmelCase : Optional[str] = None __UpperCAmelCase : str = "." __UpperCAmelCase : Optional[str] = None __UpperCAmelCase : str = '"' __UpperCAmelCase : int = 0 __UpperCAmelCase : Optional[str] = None __UpperCAmelCase : Optional[str] = None __UpperCAmelCase : Optional[str] = None __UpperCAmelCase : Optional[str] = None __UpperCAmelCase : bool = True __UpperCAmelCase : bool = True __UpperCAmelCase : int = 0 __UpperCAmelCase : bool = True __UpperCAmelCase : bool = False __UpperCAmelCase : Optional[str] = None __UpperCAmelCase : int = 1_0_0_0_0 __UpperCAmelCase : Optional[datasets.Features] = None __UpperCAmelCase : Optional[str] = "strict" __UpperCAmelCase : Literal["error", "warn", "skip"] = "error" __UpperCAmelCase : Optional[str] = None def _lowercase ( self : Tuple ): if self.delimiter is not None: __lowercase = self.delimiter if self.column_names is not None: __lowercase = self.column_names @property def _lowercase ( self : Union[str, Any] ): __lowercase = { "sep": self.sep, "header": self.header, "names": self.names, "index_col": self.index_col, "usecols": self.usecols, "prefix": self.prefix, "mangle_dupe_cols": self.mangle_dupe_cols, "engine": self.engine, "converters": self.converters, "true_values": self.true_values, "false_values": self.false_values, "skipinitialspace": self.skipinitialspace, "skiprows": self.skiprows, "nrows": self.nrows, "na_values": self.na_values, "keep_default_na": self.keep_default_na, "na_filter": self.na_filter, "verbose": self.verbose, "skip_blank_lines": self.skip_blank_lines, "thousands": self.thousands, "decimal": self.decimal, "lineterminator": self.lineterminator, "quotechar": self.quotechar, "quoting": self.quoting, "escapechar": self.escapechar, "comment": self.comment, "encoding": self.encoding, "dialect": self.dialect, "error_bad_lines": self.error_bad_lines, "warn_bad_lines": self.warn_bad_lines, "skipfooter": self.skipfooter, "doublequote": self.doublequote, "memory_map": self.memory_map, "float_precision": self.float_precision, "chunksize": self.chunksize, "encoding_errors": self.encoding_errors, "on_bad_lines": self.on_bad_lines, "date_format": self.date_format, } # some kwargs must not be passed if they don't have a default value # some others are deprecated and we can also not pass them if they are the default value for pd_read_csv_parameter in _PANDAS_READ_CSV_NO_DEFAULT_PARAMETERS + _PANDAS_READ_CSV_DEPRECATED_PARAMETERS: if pd_read_csv_kwargs[pd_read_csv_parameter] == getattr(CsvConfig(), UpperCAmelCase__ ): del pd_read_csv_kwargs[pd_read_csv_parameter] # Remove 2.0 new arguments if not (datasets.config.PANDAS_VERSION.major >= 2): for pd_read_csv_parameter in _PANDAS_READ_CSV_NEW_2_0_0_PARAMETERS: del pd_read_csv_kwargs[pd_read_csv_parameter] # Remove 1.3 new arguments if not (datasets.config.PANDAS_VERSION.major >= 1 and datasets.config.PANDAS_VERSION.minor >= 3): for pd_read_csv_parameter in _PANDAS_READ_CSV_NEW_1_3_0_PARAMETERS: del pd_read_csv_kwargs[pd_read_csv_parameter] return pd_read_csv_kwargs class _lowerCAmelCase ( datasets.ArrowBasedBuilder ): """simple docstring""" __UpperCAmelCase : Tuple = CsvConfig def _lowercase ( self : List[str] ): return datasets.DatasetInfo(features=self.config.features ) def _lowercase ( self : List[Any], UpperCAmelCase__ : Dict ): 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}""" ) __lowercase = dl_manager.download_and_extract(self.config.data_files ) if isinstance(UpperCAmelCase__, (str, list, tuple) ): __lowercase = data_files if isinstance(UpperCAmelCase__, UpperCAmelCase__ ): __lowercase = [files] __lowercase = [dl_manager.iter_files(UpperCAmelCase__ ) for file in files] return [datasets.SplitGenerator(name=datasets.Split.TRAIN, gen_kwargs={"files": files} )] __lowercase = [] for split_name, files in data_files.items(): if isinstance(UpperCAmelCase__, UpperCAmelCase__ ): __lowercase = [files] __lowercase = [dl_manager.iter_files(UpperCAmelCase__ ) for file in files] splits.append(datasets.SplitGenerator(name=UpperCAmelCase__, gen_kwargs={"files": files} ) ) return splits def _lowercase ( self : Dict, UpperCAmelCase__ : pa.Table ): if self.config.features is not None: __lowercase = self.config.features.arrow_schema if all(not require_storage_cast(UpperCAmelCase__ ) for feature in self.config.features.values() ): # cheaper cast __lowercase = pa.Table.from_arrays([pa_table[field.name] for field in schema], schema=UpperCAmelCase__ ) else: # more expensive cast; allows str <-> int/float or str to Audio for example __lowercase = table_cast(UpperCAmelCase__, UpperCAmelCase__ ) return pa_table def _lowercase ( self : Optional[Any], UpperCAmelCase__ : List[str] ): __lowercase = self.config.features.arrow_schema if self.config.features else None # dtype allows reading an int column as str __lowercase = ( { name: dtype.to_pandas_dtype() if not require_storage_cast(UpperCAmelCase__ ) else object for name, dtype, feature in zip(schema.names, schema.types, self.config.features.values() ) } if schema is not None else None ) for file_idx, file in enumerate(itertools.chain.from_iterable(UpperCAmelCase__ ) ): __lowercase = pd.read_csv(UpperCAmelCase__, iterator=UpperCAmelCase__, dtype=UpperCAmelCase__, **self.config.pd_read_csv_kwargs ) try: for batch_idx, df in enumerate(UpperCAmelCase__ ): __lowercase = pa.Table.from_pandas(UpperCAmelCase__ ) # 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 (file_idx, batch_idx), self._cast_table(UpperCAmelCase__ ) except ValueError as e: logger.error(F"""Failed to read file '{file}' with error {type(UpperCAmelCase__ )}: {e}""" ) raise
17
0
'''simple docstring''' def UpperCAmelCase ( lowerCamelCase_ :list ): '''simple docstring''' if len(lowerCamelCase_ ) <= 1: return lst snake_case_ : Union[str, Any] = 1 while i < len(lowerCamelCase_ ): if lst[i - 1] <= lst[i]: i += 1 else: snake_case_ : Union[str, Any] = lst[i], lst[i - 1] i -= 1 if i == 0: snake_case_ : int = 1 return lst if __name__ == "__main__": __A : Optional[int] = input('Enter numbers separated by a comma:\n').strip() __A : int = [int(item) for item in user_input.split(',')] print(gnome_sort(unsorted))
369
'''simple docstring''' import collections import os import re from pathlib import Path __A : Dict = 'src/transformers' # Matches is_xxx_available() __A : Dict = re.compile(r'is\_([a-z_]*)_available()') # Catches a one-line _import_struct = {xxx} __A : Any = re.compile(r'^_import_structure\s+=\s+\{([^\}]+)\}') # Catches a line with a key-values pattern: "bla": ["foo", "bar"] __A : Tuple = re.compile(r'\s+"\S*":\s+\[([^\]]*)\]') # Catches a line if not is_foo_available __A : Optional[Any] = re.compile(r'^\s*if\s+not\s+is\_[a-z_]*\_available\(\)') # Catches a line _import_struct["bla"].append("foo") __A : Optional[int] = re.compile(r'^\s*_import_structure\["\S*"\]\.append\("(\S*)"\)') # Catches a line _import_struct["bla"].extend(["foo", "bar"]) or _import_struct["bla"] = ["foo", "bar"] __A : List[Any] = re.compile(r'^\s*_import_structure\[\S*\](?:\.extend\(|\s*=\s+)\[([^\]]*)\]') # Catches a line with an object between quotes and a comma: "MyModel", __A : Union[str, Any] = re.compile(r'^\s+"([^"]+)",') # Catches a line with objects between brackets only: ["foo", "bar"], __A : int = re.compile(r'^\s+\[([^\]]+)\]') # Catches a line with from foo import bar, bla, boo __A : int = re.compile(r'\s+from\s+\S*\s+import\s+([^\(\s].*)\n') # Catches a line with try: __A : List[Any] = re.compile(r'^\s*try:') # Catches a line with else: __A : Any = re.compile(r'^\s*else:') def UpperCAmelCase ( lowerCamelCase_ :str ): '''simple docstring''' if _re_test_backend.search(lowerCamelCase_ ) is None: return None snake_case_ : Tuple = [b[0] for b in _re_backend.findall(lowerCamelCase_ )] backends.sort() return "_and_".join(lowerCamelCase_ ) def UpperCAmelCase ( lowerCamelCase_ :Optional[int] ): '''simple docstring''' with open(lowerCamelCase_ , """r""" , encoding="""utf-8""" , newline="""\n""" ) as f: snake_case_ : str = f.readlines() snake_case_ : List[Any] = 0 while line_index < len(lowerCamelCase_ ) and not lines[line_index].startswith("""_import_structure = {""" ): line_index += 1 # If this is a traditional init, just return. if line_index >= len(lowerCamelCase_ ): return None # First grab the objects without a specific backend in _import_structure snake_case_ : Union[str, Any] = [] while not lines[line_index].startswith("""if TYPE_CHECKING""" ) and find_backend(lines[line_index] ) is None: snake_case_ : str = lines[line_index] # If we have everything on a single line, let's deal with it. if _re_one_line_import_struct.search(lowerCamelCase_ ): snake_case_ : Optional[int] = _re_one_line_import_struct.search(lowerCamelCase_ ).groups()[0] snake_case_ : Union[str, Any] = re.findall(R"""\[([^\]]+)\]""" , lowerCamelCase_ ) for imp in imports: objects.extend([obj[1:-1] for obj in imp.split(""", """ )] ) line_index += 1 continue snake_case_ : Any = _re_import_struct_key_value.search(lowerCamelCase_ ) if single_line_import_search is not None: snake_case_ : Optional[int] = [obj[1:-1] for obj in single_line_import_search.groups()[0].split(""", """ ) if len(lowerCamelCase_ ) > 0] objects.extend(lowerCamelCase_ ) elif line.startswith(""" """ * 8 + """\"""" ): objects.append(line[9:-3] ) line_index += 1 snake_case_ : Union[str, Any] = {"""none""": objects} # Let's continue with backend-specific objects in _import_structure while not lines[line_index].startswith("""if TYPE_CHECKING""" ): # If the line is an if not is_backend_available, we grab all objects associated. snake_case_ : List[str] = find_backend(lines[line_index] ) # Check if the backend declaration is inside a try block: if _re_try.search(lines[line_index - 1] ) is None: snake_case_ : Tuple = None if backend is not None: line_index += 1 # Scroll until we hit the else block of try-except-else while _re_else.search(lines[line_index] ) is None: line_index += 1 line_index += 1 snake_case_ : Dict = [] # Until we unindent, add backend objects to the list while len(lines[line_index] ) <= 1 or lines[line_index].startswith(""" """ * 4 ): snake_case_ : List[Any] = lines[line_index] if _re_import_struct_add_one.search(lowerCamelCase_ ) is not None: objects.append(_re_import_struct_add_one.search(lowerCamelCase_ ).groups()[0] ) elif _re_import_struct_add_many.search(lowerCamelCase_ ) is not None: snake_case_ : Optional[int] = _re_import_struct_add_many.search(lowerCamelCase_ ).groups()[0].split(""", """ ) snake_case_ : List[str] = [obj[1:-1] for obj in imports if len(lowerCamelCase_ ) > 0] objects.extend(lowerCamelCase_ ) elif _re_between_brackets.search(lowerCamelCase_ ) is not None: snake_case_ : List[str] = _re_between_brackets.search(lowerCamelCase_ ).groups()[0].split(""", """ ) snake_case_ : Any = [obj[1:-1] for obj in imports if len(lowerCamelCase_ ) > 0] objects.extend(lowerCamelCase_ ) elif _re_quote_object.search(lowerCamelCase_ ) is not None: objects.append(_re_quote_object.search(lowerCamelCase_ ).groups()[0] ) elif line.startswith(""" """ * 8 + """\"""" ): objects.append(line[9:-3] ) elif line.startswith(""" """ * 12 + """\"""" ): objects.append(line[13:-3] ) line_index += 1 snake_case_ : int = objects else: line_index += 1 # At this stage we are in the TYPE_CHECKING part, first grab the objects without a specific backend snake_case_ : List[Any] = [] while ( line_index < len(lowerCamelCase_ ) and find_backend(lines[line_index] ) is None and not lines[line_index].startswith("""else""" ) ): snake_case_ : Union[str, Any] = lines[line_index] snake_case_ : Union[str, Any] = _re_import.search(lowerCamelCase_ ) if single_line_import_search is not None: objects.extend(single_line_import_search.groups()[0].split(""", """ ) ) elif line.startswith(""" """ * 8 ): objects.append(line[8:-2] ) line_index += 1 snake_case_ : Dict = {"""none""": objects} # Let's continue with backend-specific objects while line_index < len(lowerCamelCase_ ): # If the line is an if is_backend_available, we grab all objects associated. snake_case_ : Optional[Any] = find_backend(lines[line_index] ) # Check if the backend declaration is inside a try block: if _re_try.search(lines[line_index - 1] ) is None: snake_case_ : str = None if backend is not None: line_index += 1 # Scroll until we hit the else block of try-except-else while _re_else.search(lines[line_index] ) is None: line_index += 1 line_index += 1 snake_case_ : Any = [] # Until we unindent, add backend objects to the list while len(lines[line_index] ) <= 1 or lines[line_index].startswith(""" """ * 8 ): snake_case_ : Dict = lines[line_index] snake_case_ : Any = _re_import.search(lowerCamelCase_ ) if single_line_import_search is not None: objects.extend(single_line_import_search.groups()[0].split(""", """ ) ) elif line.startswith(""" """ * 12 ): objects.append(line[12:-2] ) line_index += 1 snake_case_ : int = objects else: line_index += 1 return import_dict_objects, type_hint_objects def UpperCAmelCase ( lowerCamelCase_ :int , lowerCamelCase_ :List[str] ): '''simple docstring''' def find_duplicates(lowerCamelCase_ :Union[str, Any] ): return [k for k, v in collections.Counter(lowerCamelCase_ ).items() if v > 1] if list(import_dict_objects.keys() ) != list(type_hint_objects.keys() ): return ["Both sides of the init do not have the same backends!"] snake_case_ : Optional[int] = [] for key in import_dict_objects.keys(): snake_case_ : int = find_duplicates(import_dict_objects[key] ) if duplicate_imports: errors.append(F'''Duplicate _import_structure definitions for: {duplicate_imports}''' ) snake_case_ : List[str] = find_duplicates(type_hint_objects[key] ) if duplicate_type_hints: errors.append(F'''Duplicate TYPE_CHECKING objects for: {duplicate_type_hints}''' ) if sorted(set(import_dict_objects[key] ) ) != sorted(set(type_hint_objects[key] ) ): snake_case_ : str = """base imports""" if key == """none""" else F'''{key} backend''' errors.append(F'''Differences for {name}:''' ) for a in type_hint_objects[key]: if a not in import_dict_objects[key]: errors.append(F''' {a} in TYPE_HINT but not in _import_structure.''' ) for a in import_dict_objects[key]: if a not in type_hint_objects[key]: errors.append(F''' {a} in _import_structure but not in TYPE_HINT.''' ) return errors def UpperCAmelCase ( ): '''simple docstring''' snake_case_ : Tuple = [] for root, _, files in os.walk(lowerCamelCase_ ): if "__init__.py" in files: snake_case_ : Any = os.path.join(lowerCamelCase_ , """__init__.py""" ) snake_case_ : Dict = parse_init(lowerCamelCase_ ) if objects is not None: snake_case_ : Any = analyze_results(*lowerCamelCase_ ) if len(lowerCamelCase_ ) > 0: snake_case_ : Tuple = F'''Problem in {fname}, both halves do not define the same objects.\n{errors[0]}''' failures.append("""\n""".join(lowerCamelCase_ ) ) if len(lowerCamelCase_ ) > 0: raise ValueError("""\n\n""".join(lowerCamelCase_ ) ) def UpperCAmelCase ( ): '''simple docstring''' snake_case_ : Union[str, Any] = [] for path, directories, files in os.walk(lowerCamelCase_ ): for folder in directories: # Ignore private modules if folder.startswith("""_""" ): directories.remove(lowerCamelCase_ ) continue # Ignore leftovers from branches (empty folders apart from pycache) if len(list((Path(lowerCamelCase_ ) / folder).glob("""*.py""" ) ) ) == 0: continue snake_case_ : Tuple = str((Path(lowerCamelCase_ ) / folder).relative_to(lowerCamelCase_ ) ) snake_case_ : List[str] = short_path.replace(os.path.sep , """.""" ) submodules.append(lowerCamelCase_ ) for fname in files: if fname == "__init__.py": continue snake_case_ : Dict = str((Path(lowerCamelCase_ ) / fname).relative_to(lowerCamelCase_ ) ) snake_case_ : List[str] = short_path.replace(""".py""" , """""" ).replace(os.path.sep , """.""" ) if len(submodule.split(""".""" ) ) == 1: submodules.append(lowerCamelCase_ ) return submodules __A : List[Any] = [ 'convert_pytorch_checkpoint_to_tf2', 'modeling_flax_pytorch_utils', 'models.esm.openfold_utils', ] def UpperCAmelCase ( ): '''simple docstring''' # This is to make sure the transformers module imported is the one in the repo. from transformers.utils import direct_transformers_import snake_case_ : Union[str, Any] = direct_transformers_import(lowerCamelCase_ ) snake_case_ : List[str] = set(transformers._import_structure.keys() ) # This contains all the base keys of the _import_structure object defined in the init, but if the user is missing # some optional dependencies, they may not have all of them. Thus we read the init to read all additions and # (potentiall re-) add them. with open(os.path.join(lowerCamelCase_ , """__init__.py""" ) , """r""" ) as f: snake_case_ : str = f.read() import_structure_keys.update(set(re.findall(R"""import_structure\[\"([^\"]*)\"\]""" , lowerCamelCase_ ) ) ) snake_case_ : Dict = [ module for module in get_transformers_submodules() if module not in IGNORE_SUBMODULES and module not in import_structure_keys ] if len(lowerCamelCase_ ) > 0: snake_case_ : str = """\n""".join(F'''- {module}''' for module in module_not_registered ) raise ValueError( """The following submodules are not properly registed in the main init of Transformers:\n""" F'''{list_of_modules}\n''' """Make sure they appear somewhere in the keys of `_import_structure` with an empty list as value.""" ) if __name__ == "__main__": check_all_inits() check_submodules()
8
0
from maths.prime_factors import prime_factors def __UpperCamelCase ( _lowerCAmelCase ) -> int: """simple docstring""" if not isinstance(_lowerCAmelCase , _lowerCAmelCase ): A : List[Any] = f'''Input value of [number={number}] must be an integer''' raise TypeError(_lowerCAmelCase ) if number < 1: raise ValueError("""Input must be a positive integer""" ) return -1 if len(prime_factors(_lowerCAmelCase ) ) % 2 else 1 if __name__ == "__main__": import doctest doctest.testmod()
116
from __future__ import annotations from math import ceil, floor, sqrt def __UpperCamelCase ( _lowerCAmelCase = 200_0000 ) -> int: """simple docstring""" A : list[int] = [0] A : int for idx in range(1 , ceil(sqrt(target * 2 ) * 1.1 ) ): triangle_numbers.append(triangle_numbers[-1] + idx ) # we want this to be as close as possible to target A : int = 0 # the area corresponding to the grid that gives the product closest to target A : int = 0 # an estimate of b, using the quadratic formula A : float # the largest integer less than b_estimate A : int # the largest integer less than b_estimate A : int # the triangle number corresponding to b_floor A : int # the triangle number corresponding to b_ceil A : int for idx_a, triangle_a in enumerate(triangle_numbers[1:] , 1 ): A : Union[str, Any] = (-1 + sqrt(1 + 8 * target / triangle_a )) / 2 A : List[Any] = floor(_lowerCAmelCase ) A : Tuple = ceil(_lowerCAmelCase ) A : int = triangle_numbers[b_floor] A : Dict = triangle_numbers[b_ceil] if abs(target - triangle_b_first_guess * triangle_a ) < abs( target - best_product ): A : Optional[int] = triangle_b_first_guess * triangle_a A : Optional[int] = idx_a * b_floor if abs(target - triangle_b_second_guess * triangle_a ) < abs( target - best_product ): A : Tuple = triangle_b_second_guess * triangle_a A : Tuple = idx_a * b_ceil return area if __name__ == "__main__": print(F"""{solution() = }""")
116
1
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_sentencepiece_available UpperCAmelCase : Tuple = {} try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: UpperCAmelCase : List[Any] = ["""BartphoTokenizer"""] if TYPE_CHECKING: try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_bartpho import BartphoTokenizer else: import sys UpperCAmelCase : int = _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)
369
import torch from torch import nn from torch.nn import CrossEntropyLoss, MSELoss from transformers.file_utils import add_start_docstrings, add_start_docstrings_to_model_forward from transformers.models.bert.modeling_bert import ( BERT_INPUTS_DOCSTRING, BERT_START_DOCSTRING, BertEmbeddings, BertLayer, BertPooler, BertPreTrainedModel, ) def __lowerCAmelCase (SCREAMING_SNAKE_CASE )-> List[Any]: """simple docstring""" snake_case_ = torch.exp(SCREAMING_SNAKE_CASE ) snake_case_ = torch.sum(SCREAMING_SNAKE_CASE , dim=1 ) # sum of exp(x_i) snake_case_ = torch.sum(x * exp_x , dim=1 ) # sum of x_i * exp(x_i) return torch.log(SCREAMING_SNAKE_CASE ) - B / A class lowerCAmelCase_ ( nn.Module ): '''simple docstring''' def __init__( self , _UpperCAmelCase ): super().__init__() snake_case_ = config.output_attentions snake_case_ = config.output_hidden_states snake_case_ = nn.ModuleList([BertLayer(_UpperCAmelCase ) for _ in range(config.num_hidden_layers )] ) snake_case_ = nn.ModuleList([BertHighway(_UpperCAmelCase ) for _ in range(config.num_hidden_layers )] ) snake_case_ = [-1 for _ in range(config.num_hidden_layers )] def UpperCamelCase__ ( self , _UpperCAmelCase ): if (type(_UpperCAmelCase ) is float) or (type(_UpperCAmelCase ) is int): for i in range(len(self.early_exit_entropy ) ): snake_case_ = x else: snake_case_ = x def UpperCamelCase__ ( self , _UpperCAmelCase ): snake_case_ = pooler.state_dict() for highway in self.highway: for name, param in highway.pooler.state_dict().items(): param.copy_(loaded_model[name] ) def UpperCamelCase__ ( self , _UpperCAmelCase , _UpperCAmelCase=None , _UpperCAmelCase=None , _UpperCAmelCase=None , _UpperCAmelCase=None , ): snake_case_ = () snake_case_ = () snake_case_ = () for i, layer_module in enumerate(self.layer ): if self.output_hidden_states: snake_case_ = all_hidden_states + (hidden_states,) snake_case_ = layer_module( _UpperCAmelCase , _UpperCAmelCase , head_mask[i] , _UpperCAmelCase , _UpperCAmelCase ) snake_case_ = layer_outputs[0] if self.output_attentions: snake_case_ = all_attentions + (layer_outputs[1],) snake_case_ = (hidden_states,) if self.output_hidden_states: snake_case_ = current_outputs + (all_hidden_states,) if self.output_attentions: snake_case_ = current_outputs + (all_attentions,) snake_case_ = self.highway[i](_UpperCAmelCase ) # logits, pooled_output if not self.training: snake_case_ = highway_exit[0] snake_case_ = entropy(_UpperCAmelCase ) snake_case_ = highway_exit + (highway_entropy,) # logits, hidden_states(?), entropy snake_case_ = all_highway_exits + (highway_exit,) if highway_entropy < self.early_exit_entropy[i]: snake_case_ = (highway_logits,) + current_outputs[1:] + (all_highway_exits,) raise HighwayException(_UpperCAmelCase , i + 1 ) else: snake_case_ = all_highway_exits + (highway_exit,) # Add last layer if self.output_hidden_states: snake_case_ = all_hidden_states + (hidden_states,) snake_case_ = (hidden_states,) if self.output_hidden_states: snake_case_ = outputs + (all_hidden_states,) if self.output_attentions: snake_case_ = outputs + (all_attentions,) snake_case_ = outputs + (all_highway_exits,) return outputs # last-layer hidden state, (all hidden states), (all attentions), all highway exits @add_start_docstrings( "The Bert Model transformer with early exiting (DeeBERT). " , lowerCamelCase__ , ) class lowerCAmelCase_ ( lowerCamelCase__ ): '''simple docstring''' def __init__( self , _UpperCAmelCase ): super().__init__(_UpperCAmelCase ) snake_case_ = config snake_case_ = BertEmbeddings(_UpperCAmelCase ) snake_case_ = DeeBertEncoder(_UpperCAmelCase ) snake_case_ = BertPooler(_UpperCAmelCase ) self.init_weights() def UpperCamelCase__ ( self ): self.encoder.init_highway_pooler(self.pooler ) def UpperCamelCase__ ( self ): return self.embeddings.word_embeddings def UpperCamelCase__ ( self , _UpperCAmelCase ): snake_case_ = value def UpperCamelCase__ ( self , _UpperCAmelCase ): for layer, heads in heads_to_prune.items(): self.encoder.layer[layer].attention.prune_heads(_UpperCAmelCase ) @add_start_docstrings_to_model_forward(_UpperCAmelCase ) def UpperCamelCase__ ( self , _UpperCAmelCase=None , _UpperCAmelCase=None , _UpperCAmelCase=None , _UpperCAmelCase=None , _UpperCAmelCase=None , _UpperCAmelCase=None , _UpperCAmelCase=None , _UpperCAmelCase=None , ): if input_ids is not None and inputs_embeds is not None: raise ValueError('''You cannot specify both input_ids and inputs_embeds at the same time''' ) elif input_ids is not None: snake_case_ = input_ids.size() elif inputs_embeds is not None: snake_case_ = inputs_embeds.size()[:-1] else: raise ValueError('''You have to specify either input_ids or inputs_embeds''' ) snake_case_ = input_ids.device if input_ids is not None else inputs_embeds.device if attention_mask is None: snake_case_ = torch.ones(_UpperCAmelCase , device=_UpperCAmelCase ) if encoder_attention_mask is None: snake_case_ = torch.ones(_UpperCAmelCase , device=_UpperCAmelCase ) if token_type_ids is None: snake_case_ = torch.zeros(_UpperCAmelCase , dtype=torch.long , device=_UpperCAmelCase ) # We can provide a self-attention mask of dimensions [batch_size, from_seq_length, to_seq_length] # ourselves in which case we just need to make it broadcastable to all heads. snake_case_ = self.get_extended_attention_mask(_UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase ) # If a 2D ou 3D attention mask is provided for the cross-attention # we need to make broadcastable to [batch_size, num_heads, seq_length, seq_length] if encoder_attention_mask.dim() == 3: snake_case_ = encoder_attention_mask[:, None, :, :] if encoder_attention_mask.dim() == 2: snake_case_ = encoder_attention_mask[:, None, None, :] snake_case_ = encoder_extended_attention_mask.to( dtype=next(self.parameters() ).dtype ) # fp16 compatibility snake_case_ = (1.0 - encoder_extended_attention_mask) * -10_000.0 # Prepare head mask if needed # 1.0 in head_mask indicate we keep the head # attention_probs has shape bsz x n_heads x N x N # input head_mask has shape [num_heads] or [num_hidden_layers x num_heads] # and head_mask is converted to shape [num_hidden_layers x batch x num_heads x seq_length x seq_length] snake_case_ = self.get_head_mask(_UpperCAmelCase , self.config.num_hidden_layers ) snake_case_ = self.embeddings( input_ids=_UpperCAmelCase , position_ids=_UpperCAmelCase , token_type_ids=_UpperCAmelCase , inputs_embeds=_UpperCAmelCase ) snake_case_ = self.encoder( _UpperCAmelCase , attention_mask=_UpperCAmelCase , head_mask=_UpperCAmelCase , encoder_hidden_states=_UpperCAmelCase , encoder_attention_mask=_UpperCAmelCase , ) snake_case_ = encoder_outputs[0] snake_case_ = self.pooler(_UpperCAmelCase ) snake_case_ = ( sequence_output, pooled_output, ) + encoder_outputs[ 1: ] # add hidden_states and attentions if they are here return outputs # sequence_output, pooled_output, (hidden_states), (attentions), highway exits class lowerCAmelCase_ ( lowerCamelCase__ ): '''simple docstring''' def __init__( self , _UpperCAmelCase , _UpperCAmelCase ): snake_case_ = message snake_case_ = exit_layer # start from 1! class lowerCAmelCase_ ( nn.Module ): '''simple docstring''' def __init__( self , _UpperCAmelCase ): super().__init__() snake_case_ = BertPooler(_UpperCAmelCase ) snake_case_ = nn.Dropout(config.hidden_dropout_prob ) snake_case_ = nn.Linear(config.hidden_size , config.num_labels ) def UpperCamelCase__ ( self , _UpperCAmelCase ): # Pooler snake_case_ = encoder_outputs[0] snake_case_ = self.pooler(_UpperCAmelCase ) # "return" pooler_output # BertModel snake_case_ = (pooler_input, pooler_output) + encoder_outputs[1:] # "return" bmodel_output # Dropout and classification snake_case_ = bmodel_output[1] snake_case_ = self.dropout(_UpperCAmelCase ) snake_case_ = self.classifier(_UpperCAmelCase ) return logits, pooled_output @add_start_docstrings( "Bert Model (with early exiting - DeeBERT) with a classifier on top,\n also takes care of multi-layer training. " , lowerCamelCase__ , ) class lowerCAmelCase_ ( lowerCamelCase__ ): '''simple docstring''' def __init__( self , _UpperCAmelCase ): super().__init__(_UpperCAmelCase ) snake_case_ = config.num_labels snake_case_ = config.num_hidden_layers snake_case_ = DeeBertModel(_UpperCAmelCase ) snake_case_ = nn.Dropout(config.hidden_dropout_prob ) snake_case_ = nn.Linear(config.hidden_size , self.config.num_labels ) self.init_weights() @add_start_docstrings_to_model_forward(_UpperCAmelCase ) def UpperCamelCase__ ( self , _UpperCAmelCase=None , _UpperCAmelCase=None , _UpperCAmelCase=None , _UpperCAmelCase=None , _UpperCAmelCase=None , _UpperCAmelCase=None , _UpperCAmelCase=None , _UpperCAmelCase=-1 , _UpperCAmelCase=False , ): snake_case_ = self.num_layers try: snake_case_ = self.bert( _UpperCAmelCase , attention_mask=_UpperCAmelCase , token_type_ids=_UpperCAmelCase , position_ids=_UpperCAmelCase , head_mask=_UpperCAmelCase , inputs_embeds=_UpperCAmelCase , ) # sequence_output, pooled_output, (hidden_states), (attentions), highway exits snake_case_ = outputs[1] snake_case_ = self.dropout(_UpperCAmelCase ) snake_case_ = self.classifier(_UpperCAmelCase ) snake_case_ = (logits,) + outputs[2:] # add hidden states and attention if they are here except HighwayException as e: snake_case_ = e.message snake_case_ = e.exit_layer snake_case_ = outputs[0] if not self.training: snake_case_ = entropy(_UpperCAmelCase ) snake_case_ = [] snake_case_ = [] if labels is not None: if self.num_labels == 1: # We are doing regression snake_case_ = MSELoss() snake_case_ = loss_fct(logits.view(-1 ) , labels.view(-1 ) ) else: snake_case_ = CrossEntropyLoss() snake_case_ = loss_fct(logits.view(-1 , self.num_labels ) , labels.view(-1 ) ) # work with highway exits snake_case_ = [] for highway_exit in outputs[-1]: snake_case_ = highway_exit[0] if not self.training: highway_logits_all.append(_UpperCAmelCase ) highway_entropy.append(highway_exit[2] ) if self.num_labels == 1: # We are doing regression snake_case_ = MSELoss() snake_case_ = loss_fct(highway_logits.view(-1 ) , labels.view(-1 ) ) else: snake_case_ = CrossEntropyLoss() snake_case_ = loss_fct(highway_logits.view(-1 , self.num_labels ) , labels.view(-1 ) ) highway_losses.append(_UpperCAmelCase ) if train_highway: snake_case_ = (sum(highway_losses[:-1] ),) + outputs # exclude the final highway, of course else: snake_case_ = (loss,) + outputs if not self.training: snake_case_ = outputs + ((original_entropy, highway_entropy), exit_layer) if output_layer >= 0: snake_case_ = ( (outputs[0],) + (highway_logits_all[output_layer],) + outputs[2:] ) # use the highway of the last layer return outputs # (loss), logits, (hidden_states), (attentions), (highway_exits)
267
0
import unittest from transformers import CamembertTokenizer, CamembertTokenizerFast from transformers.testing_utils import get_tests_dir, require_sentencepiece, require_tokenizers, slow from transformers.utils import is_torch_available from ...test_tokenization_common import TokenizerTesterMixin __snake_case : List[str] =get_tests_dir('fixtures/test_sentencepiece.model') __snake_case : List[str] =get_tests_dir('fixtures/test_sentencepiece_bpe.model') __snake_case : List[Any] ='pt' if is_torch_available() else 'tf' @require_sentencepiece @require_tokenizers class lowerCamelCase__ ( lowerCamelCase__ , unittest.TestCase): '''simple docstring''' snake_case_ =CamembertTokenizer snake_case_ =CamembertTokenizerFast snake_case_ =True snake_case_ =True def lowerCAmelCase__ (self ) -> Optional[int]: """simple docstring""" super().setUp() # We have a SentencePiece fixture for testing lowerCAmelCase__ : Tuple = CamembertTokenizer(__lowerCamelCase ) tokenizer.save_pretrained(self.tmpdirname ) def lowerCAmelCase__ (self ) -> Tuple: """simple docstring""" lowerCAmelCase__ : List[Any] = '''<pad>''' lowerCAmelCase__ : Union[str, Any] = 1 self.assertEqual(self.get_tokenizer()._convert_token_to_id(__lowerCamelCase ) ,__lowerCamelCase ) self.assertEqual(self.get_tokenizer()._convert_id_to_token(__lowerCamelCase ) ,__lowerCamelCase ) def lowerCAmelCase__ (self ) -> Optional[int]: """simple docstring""" lowerCAmelCase__ : List[str] = list(self.get_tokenizer().get_vocab().keys() ) self.assertEqual(vocab_keys[0] ,'''<s>NOTUSED''' ) self.assertEqual(vocab_keys[1] ,'''<pad>''' ) self.assertEqual(vocab_keys[-1] ,'''<mask>''' ) self.assertEqual(len(__lowerCamelCase ) ,10_04 ) def lowerCAmelCase__ (self ) -> Union[str, Any]: """simple docstring""" self.assertEqual(self.get_tokenizer().vocab_size ,10_05 ) def lowerCAmelCase__ (self ) -> int: """simple docstring""" lowerCAmelCase__ : Tuple = CamembertTokenizer(__lowerCamelCase ) tokenizer.save_pretrained(self.tmpdirname ) lowerCAmelCase__ : List[str] = CamembertTokenizerFast.from_pretrained(self.tmpdirname ) lowerCAmelCase__ : List[str] = '''I was born in 92000, and this is falsé.''' lowerCAmelCase__ : List[str] = tokenizer.encode(__lowerCamelCase ) lowerCAmelCase__ : str = rust_tokenizer.encode(__lowerCamelCase ) self.assertListEqual(__lowerCamelCase ,__lowerCamelCase ) lowerCAmelCase__ : str = tokenizer.encode(__lowerCamelCase ,add_special_tokens=__lowerCamelCase ) lowerCAmelCase__ : Any = rust_tokenizer.encode(__lowerCamelCase ,add_special_tokens=__lowerCamelCase ) self.assertListEqual(__lowerCamelCase ,__lowerCamelCase ) # <unk> tokens are not the same for `rust` than for `slow`. # Because spm gives back raw token instead of `unk` in EncodeAsPieces # tokens = tokenizer.tokenize(sequence) lowerCAmelCase__ : Optional[Any] = tokenizer.convert_ids_to_tokens(__lowerCamelCase ) lowerCAmelCase__ : List[str] = rust_tokenizer.tokenize(__lowerCamelCase ) self.assertListEqual(__lowerCamelCase ,__lowerCamelCase ) def lowerCAmelCase__ (self ) -> int: """simple docstring""" if not self.test_rust_tokenizer: return lowerCAmelCase__ : Optional[Any] = self.get_tokenizer() lowerCAmelCase__ : Union[str, Any] = self.get_rust_tokenizer() lowerCAmelCase__ : Union[str, Any] = '''I was born in 92000, and this is falsé.''' lowerCAmelCase__ : Optional[int] = tokenizer.tokenize(__lowerCamelCase ) lowerCAmelCase__ : List[str] = rust_tokenizer.tokenize(__lowerCamelCase ) self.assertListEqual(__lowerCamelCase ,__lowerCamelCase ) lowerCAmelCase__ : str = tokenizer.encode(__lowerCamelCase ,add_special_tokens=__lowerCamelCase ) lowerCAmelCase__ : Tuple = rust_tokenizer.encode(__lowerCamelCase ,add_special_tokens=__lowerCamelCase ) self.assertListEqual(__lowerCamelCase ,__lowerCamelCase ) lowerCAmelCase__ : Dict = self.get_rust_tokenizer() lowerCAmelCase__ : Any = tokenizer.encode(__lowerCamelCase ) lowerCAmelCase__ : Any = rust_tokenizer.encode(__lowerCamelCase ) self.assertListEqual(__lowerCamelCase ,__lowerCamelCase ) @slow def lowerCAmelCase__ (self ) -> Tuple: """simple docstring""" lowerCAmelCase__ : Any = {'''input_ids''': [[5, 54, 71_96, 2_97, 30, 23, 7_76, 18, 11, 32_15, 37_05, 82_52, 22, 31_64, 11_81, 21_16, 29, 16, 8_13, 25, 7_91, 33_14, 20, 34_46, 38, 2_75_75, 1_20, 6, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [5, 4_68, 17, 11, 90_88, 20, 15_17, 8, 2_28_04, 1_88_18, 10, 38, 6_29, 6_07, 6_07, 1_42, 19, 71_96, 8_67, 56, 1_03_26, 24, 22_67, 20, 4_16, 50_72, 1_56_12, 2_33, 7_34, 7, 23_99, 27, 16, 30_15, 16_49, 7, 24, 20, 43_38, 23_99, 27, 13, 34_00, 14, 13, 61_89, 8, 9_30, 9, 6]], '''attention_mask''': [[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]} # noqa: E501 # fmt: on # camembert is a french model. So we also use french texts. lowerCAmelCase__ : List[str] = [ '''Le transformeur est un modèle d\'apprentissage profond introduit en 2017, ''' '''utilisé principalement dans le domaine du traitement automatique des langues (TAL).''', '''À l\'instar des réseaux de neurones récurrents (RNN), les transformeurs sont conçus ''' '''pour gérer des données séquentielles, telles que le langage naturel, pour des tâches ''' '''telles que la traduction et la synthèse de texte.''', ] self.tokenizer_integration_test_util( expected_encoding=__lowerCamelCase ,model_name='''camembert-base''' ,revision='''3a0641d9a1aeb7e848a74299e7e4c4bca216b4cf''' ,sequences=__lowerCamelCase ,)
129
import pytest from datasets.parallel import ParallelBackendConfig, parallel_backend from datasets.utils.py_utils import map_nested from .utils import require_dill_gt_0_3_2, require_joblibspark, require_not_windows def lowerCAmelCase__ ( lowerCamelCase_ : Optional[Any]): # picklable for multiprocessing '''simple docstring''' return i + 1 @require_dill_gt_0_3_2 @require_joblibspark @require_not_windows def lowerCAmelCase__ ( ): '''simple docstring''' with parallel_backend('''spark'''): assert ParallelBackendConfig.backend_name == "spark" lowerCAmelCase__ : Any = [1, 2, 3] with pytest.raises(lowerCamelCase_): with parallel_backend('''unsupported backend'''): map_nested(lowerCamelCase_ ,lowerCamelCase_ ,num_proc=2) with pytest.raises(lowerCamelCase_): with parallel_backend('''unsupported backend'''): map_nested(lowerCamelCase_ ,lowerCamelCase_ ,num_proc=-1) @require_dill_gt_0_3_2 @require_joblibspark @require_not_windows @pytest.mark.parametrize('''num_proc''' ,[2, -1]) def lowerCAmelCase__ ( lowerCamelCase_ : List[Any]): '''simple docstring''' lowerCAmelCase__ : List[str] = [1, 2] lowerCAmelCase__ : Tuple = {'''a''': 1, '''b''': 2} lowerCAmelCase__ : Union[str, Any] = {'''a''': [1, 2], '''b''': [3, 4]} lowerCAmelCase__ : Any = {'''a''': {'''1''': 1}, '''b''': 2} lowerCAmelCase__ : Union[str, Any] = {'''a''': 1, '''b''': 2, '''c''': 3, '''d''': 4} lowerCAmelCase__ : int = [2, 3] lowerCAmelCase__ : List[str] = {'''a''': 2, '''b''': 3} lowerCAmelCase__ : str = {'''a''': [2, 3], '''b''': [4, 5]} lowerCAmelCase__ : List[str] = {'''a''': {'''1''': 2}, '''b''': 3} lowerCAmelCase__ : Optional[int] = {'''a''': 2, '''b''': 3, '''c''': 4, '''d''': 5} with parallel_backend('''spark'''): assert map_nested(lowerCamelCase_ ,lowerCamelCase_ ,num_proc=lowerCamelCase_) == expected_map_nested_sa assert map_nested(lowerCamelCase_ ,lowerCamelCase_ ,num_proc=lowerCamelCase_) == expected_map_nested_sa assert map_nested(lowerCamelCase_ ,lowerCamelCase_ ,num_proc=lowerCamelCase_) == expected_map_nested_sa assert map_nested(lowerCamelCase_ ,lowerCamelCase_ ,num_proc=lowerCamelCase_) == expected_map_nested_sa assert map_nested(lowerCamelCase_ ,lowerCamelCase_ ,num_proc=lowerCamelCase_) == expected_map_nested_sa
129
1
"""simple docstring""" import argparse import requests import torch # pip3 install salesforce-lavis # I'm actually installing a slightly modified version: pip3 install git+https://github.com/nielsrogge/LAVIS.git@fix_lavis from lavis.models import load_model_and_preprocess from PIL import Image from transformers import ( AutoTokenizer, BlipaConfig, BlipaForConditionalGeneration, BlipaProcessor, BlipaVisionConfig, BlipImageProcessor, OPTConfig, TaConfig, ) from transformers.utils.constants import OPENAI_CLIP_MEAN, OPENAI_CLIP_STD def __a ( ) ->Dict: a__: int = 'https://storage.googleapis.com/sfr-vision-language-research/LAVIS/assets/merlion.png' a__: Any = Image.open(requests.get(_SCREAMING_SNAKE_CASE , stream=_SCREAMING_SNAKE_CASE ).raw ).convert('RGB' ) return image def __a ( _SCREAMING_SNAKE_CASE ) ->List[str]: a__: List[Any] = [] # fmt: off # vision encoder rename_keys.append(('visual_encoder.cls_token', 'vision_model.embeddings.class_embedding') ) rename_keys.append(('visual_encoder.pos_embed', 'vision_model.embeddings.position_embedding') ) rename_keys.append(('visual_encoder.patch_embed.proj.weight', 'vision_model.embeddings.patch_embedding.weight') ) rename_keys.append(('visual_encoder.patch_embed.proj.bias', 'vision_model.embeddings.patch_embedding.bias') ) rename_keys.append(('ln_vision.weight', 'vision_model.post_layernorm.weight') ) rename_keys.append(('ln_vision.bias', 'vision_model.post_layernorm.bias') ) for i in range(config.vision_config.num_hidden_layers ): rename_keys.append((F'visual_encoder.blocks.{i}.norm1.weight', F'vision_model.encoder.layers.{i}.layer_norm1.weight') ) rename_keys.append((F'visual_encoder.blocks.{i}.norm1.bias', F'vision_model.encoder.layers.{i}.layer_norm1.bias') ) rename_keys.append((F'visual_encoder.blocks.{i}.norm2.weight', F'vision_model.encoder.layers.{i}.layer_norm2.weight') ) rename_keys.append((F'visual_encoder.blocks.{i}.norm2.bias', F'vision_model.encoder.layers.{i}.layer_norm2.bias') ) rename_keys.append((F'visual_encoder.blocks.{i}.attn.qkv.weight', F'vision_model.encoder.layers.{i}.self_attn.qkv.weight') ) rename_keys.append((F'visual_encoder.blocks.{i}.attn.proj.weight', F'vision_model.encoder.layers.{i}.self_attn.projection.weight',) ) rename_keys.append((F'visual_encoder.blocks.{i}.attn.proj.bias', F'vision_model.encoder.layers.{i}.self_attn.projection.bias') ) rename_keys.append((F'visual_encoder.blocks.{i}.mlp.fc1.weight', F'vision_model.encoder.layers.{i}.mlp.fc1.weight') ) rename_keys.append((F'visual_encoder.blocks.{i}.mlp.fc1.bias', F'vision_model.encoder.layers.{i}.mlp.fc1.bias') ) rename_keys.append((F'visual_encoder.blocks.{i}.mlp.fc2.weight', F'vision_model.encoder.layers.{i}.mlp.fc2.weight') ) rename_keys.append((F'visual_encoder.blocks.{i}.mlp.fc2.bias', F'vision_model.encoder.layers.{i}.mlp.fc2.bias') ) # QFormer rename_keys.append(('Qformer.bert.embeddings.LayerNorm.weight', 'qformer.layernorm.weight') ) rename_keys.append(('Qformer.bert.embeddings.LayerNorm.bias', 'qformer.layernorm.bias') ) # fmt: on return rename_keys def __a ( _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) ->List[str]: a__: int = dct.pop(_SCREAMING_SNAKE_CASE ) a__: Tuple = val def __a ( _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) ->Optional[Any]: for i in range(config.vision_config.num_hidden_layers ): # read in original q and v biases a__: List[str] = state_dict.pop(F'visual_encoder.blocks.{i}.attn.q_bias' ) a__: Union[str, Any] = state_dict.pop(F'visual_encoder.blocks.{i}.attn.v_bias' ) # next, set bias in the state dict a__: str = torch.cat((q_bias, torch.zeros_like(_SCREAMING_SNAKE_CASE , requires_grad=_SCREAMING_SNAKE_CASE ), v_bias) ) a__: Union[str, Any] = qkv_bias def __a ( _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) ->Any: a__: List[Any] = 364 if 'coco' in model_name else 224 a__: Union[str, Any] = BlipaVisionConfig(image_size=_SCREAMING_SNAKE_CASE ).to_dict() # make sure the models have proper bos_token_id and eos_token_id set (important for generation) # seems like flan-T5 models don't have bos_token_id properly set? if "opt-2.7b" in model_name: a__: List[Any] = OPTConfig.from_pretrained('facebook/opt-2.7b' , eos_token_id=_SCREAMING_SNAKE_CASE ).to_dict() elif "opt-6.7b" in model_name: a__: List[str] = OPTConfig.from_pretrained('facebook/opt-6.7b' , eos_token_id=_SCREAMING_SNAKE_CASE ).to_dict() elif "t5-xl" in model_name: a__: Dict = TaConfig.from_pretrained('google/flan-t5-xl' , dense_act_fn='gelu' , bos_token_id=1 ).to_dict() elif "t5-xxl" in model_name: a__: Tuple = TaConfig.from_pretrained('google/flan-t5-xxl' , dense_act_fn='gelu' , bos_token_id=1 ).to_dict() a__: Any = BlipaConfig(vision_config=_SCREAMING_SNAKE_CASE , text_config=_SCREAMING_SNAKE_CASE ) return config, image_size @torch.no_grad() def __a ( _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE=None , _SCREAMING_SNAKE_CASE=False ) ->Optional[int]: a__: Optional[Any] = ( AutoTokenizer.from_pretrained('facebook/opt-2.7b' ) if 'opt' in model_name else AutoTokenizer.from_pretrained('google/flan-t5-xl' ) ) a__: int = tokenizer('\n' , add_special_tokens=_SCREAMING_SNAKE_CASE ).input_ids[0] a__ , a__: List[Any] = get_blipa_config(_SCREAMING_SNAKE_CASE , eos_token_id=_SCREAMING_SNAKE_CASE ) a__: Dict = BlipaForConditionalGeneration(_SCREAMING_SNAKE_CASE ).eval() a__: List[Any] = { 'blip2-opt-2.7b': ('blip2_opt', 'pretrain_opt2.7b'), 'blip2-opt-6.7b': ('blip2_opt', 'pretrain_opt6.7b'), 'blip2-opt-2.7b-coco': ('blip2_opt', 'caption_coco_opt2.7b'), 'blip2-opt-6.7b-coco': ('blip2_opt', 'caption_coco_opt6.7b'), 'blip2-flan-t5-xl': ('blip2_t5', 'pretrain_flant5xl'), 'blip2-flan-t5-xl-coco': ('blip2_t5', 'caption_coco_flant5xl'), 'blip2-flan-t5-xxl': ('blip2_t5', 'pretrain_flant5xxl'), } a__ , a__: Dict = model_name_to_original[model_name] # load original model print('Loading original model...' ) a__: Tuple = 'cuda' if torch.cuda.is_available() else 'cpu' a__ , a__ , a__: Tuple = load_model_and_preprocess( name=_SCREAMING_SNAKE_CASE , model_type=_SCREAMING_SNAKE_CASE , is_eval=_SCREAMING_SNAKE_CASE , device=_SCREAMING_SNAKE_CASE ) original_model.eval() print('Done!' ) # update state dict keys a__: int = original_model.state_dict() a__: List[str] = create_rename_keys(_SCREAMING_SNAKE_CASE ) for src, dest in rename_keys: rename_key(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) # some keys can be renamed efficiently for key, val in state_dict.copy().items(): a__: Tuple = state_dict.pop(_SCREAMING_SNAKE_CASE ) if key.startswith('Qformer.bert' ): a__: List[Any] = key.replace('Qformer.bert' , 'qformer' ) if "attention.self" in key: a__: Tuple = key.replace('self' , 'attention' ) if "opt_proj" in key: a__: int = key.replace('opt_proj' , 'language_projection' ) if "t5_proj" in key: a__: List[str] = key.replace('t5_proj' , 'language_projection' ) if key.startswith('opt' ): a__: Union[str, Any] = key.replace('opt' , 'language' ) if key.startswith('t5' ): a__: List[Any] = key.replace('t5' , 'language' ) a__: str = val # read in qv biases read_in_q_v_bias(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) a__ , a__: Dict = hf_model.load_state_dict(_SCREAMING_SNAKE_CASE , strict=_SCREAMING_SNAKE_CASE ) assert len(_SCREAMING_SNAKE_CASE ) == 0 assert unexpected_keys == ["qformer.embeddings.position_ids"] a__: Optional[Any] = load_demo_image() a__: Tuple = vis_processors['eval'](_SCREAMING_SNAKE_CASE ).unsqueeze(0 ).to(_SCREAMING_SNAKE_CASE ) a__: Optional[int] = tokenizer(['\n'] , return_tensors='pt' ).input_ids.to(_SCREAMING_SNAKE_CASE ) # create processor a__: Optional[Any] = BlipImageProcessor( size={'height': image_size, 'width': image_size} , image_mean=_SCREAMING_SNAKE_CASE , image_std=_SCREAMING_SNAKE_CASE ) a__: int = BlipaProcessor(image_processor=_SCREAMING_SNAKE_CASE , tokenizer=_SCREAMING_SNAKE_CASE ) a__: Dict = processor(images=_SCREAMING_SNAKE_CASE , return_tensors='pt' ).pixel_values.to(_SCREAMING_SNAKE_CASE ) # make sure processor creates exact same pixel values assert torch.allclose(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) original_model.to(_SCREAMING_SNAKE_CASE ) hf_model.to(_SCREAMING_SNAKE_CASE ) with torch.no_grad(): if "opt" in model_name: a__: Dict = original_model({'image': original_pixel_values, 'text_input': ['']} ).logits a__: Union[str, Any] = hf_model(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ).logits else: a__: Any = original_model( {'image': original_pixel_values, 'text_input': ['\n'], 'text_output': ['\n']} ).logits a__: str = input_ids.masked_fill(input_ids == tokenizer.pad_token_id , -100 ) a__: Any = hf_model(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , labels=_SCREAMING_SNAKE_CASE ).logits assert original_logits.shape == logits.shape print('First values of original logits:' , original_logits[0, :3, :3] ) print('First values of HF logits:' , logits[0, :3, :3] ) # assert values if model_name == "blip2-flan-t5-xl": a__: int = torch.tensor( [[-41.5_850, -4.4_440, -8.9_922], [-47.4_322, -5.9_143, -1.7_340]] , device=_SCREAMING_SNAKE_CASE ) assert torch.allclose(logits[0, :3, :3] , _SCREAMING_SNAKE_CASE , atol=1e-4 ) elif model_name == "blip2-flan-t5-xl-coco": a__: List[str] = torch.tensor( [[-57.0_109, -9.8_967, -12.6_280], [-68.6_578, -12.7_191, -10.5_065]] , device=_SCREAMING_SNAKE_CASE ) else: # cast to same type a__: Optional[int] = logits.dtype assert torch.allclose(original_logits.to(_SCREAMING_SNAKE_CASE ) , _SCREAMING_SNAKE_CASE , atol=1e-2 ) print('Looks ok!' ) print('Generating a caption...' ) a__: List[Any] = '' a__: Any = tokenizer(_SCREAMING_SNAKE_CASE , return_tensors='pt' ).input_ids.to(_SCREAMING_SNAKE_CASE ) a__: Any = original_model.generate({'image': original_pixel_values} ) a__: str = hf_model.generate( _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , do_sample=_SCREAMING_SNAKE_CASE , num_beams=5 , max_length=30 , min_length=1 , top_p=0.9 , repetition_penalty=1.0 , length_penalty=1.0 , temperature=1 , ) print('Original generation:' , _SCREAMING_SNAKE_CASE ) a__: Union[str, Any] = input_ids.shape[1] a__: Tuple = processor.batch_decode(outputs[:, prompt_length:] , skip_special_tokens=_SCREAMING_SNAKE_CASE ) a__: Optional[Any] = [text.strip() for text in output_text] print('HF generation:' , _SCREAMING_SNAKE_CASE ) if pytorch_dump_folder_path is not None: processor.save_pretrained(_SCREAMING_SNAKE_CASE ) hf_model.save_pretrained(_SCREAMING_SNAKE_CASE ) if push_to_hub: processor.push_to_hub(F'nielsr/{model_name}' ) hf_model.push_to_hub(F'nielsr/{model_name}' ) if __name__ == "__main__": lowercase__ = argparse.ArgumentParser() lowercase__ = [ 'blip2-opt-2.7b', 'blip2-opt-6.7b', 'blip2-opt-2.7b-coco', 'blip2-opt-6.7b-coco', 'blip2-flan-t5-xl', 'blip2-flan-t5-xl-coco', 'blip2-flan-t5-xxl', ] parser.add_argument( '--model_name', default='blip2-opt-2.7b', choices=choices, type=str, help='Path to hf config.json of model to convert', ) parser.add_argument('--pytorch_dump_folder_path', default=None, type=str, help='Path to the output PyTorch model.') parser.add_argument( '--push_to_hub', action='store_true', help='Whether to push the model and processor to the hub after converting', ) lowercase__ = parser.parse_args() convert_blipa_checkpoint(args.model_name, args.pytorch_dump_folder_path, args.push_to_hub)
203
"""simple docstring""" def __a ( _SCREAMING_SNAKE_CASE = 1000000 ) ->int: a__: Any = limit + 1 a__: List[str] = [0] * limit for first_term in range(1 , _SCREAMING_SNAKE_CASE ): for n in range(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ): a__: Any = first_term + n / first_term if common_difference % 4: # d must be divisble by 4 continue else: common_difference /= 4 if ( first_term > common_difference and first_term < 4 * common_difference ): # since x,y,z are positive integers frequency[n] += 1 # so z>0 and a>d ,also 4d<a a__: Optional[int] = sum(1 for x in frequency[1:limit] if x == 10 ) return count if __name__ == "__main__": print(f"{solution() = }")
203
1
import copy import os from typing import Union from ...configuration_utils import PretrainedConfig from ...utils import logging _lowerCAmelCase : Optional[int] = logging.get_logger(__name__) _lowerCAmelCase : List[str] = { "microsoft/git-base": "https://huggingface.co/microsoft/git-base/resolve/main/config.json", } class __magic_name__ ( lowerCAmelCase_ ): SCREAMING_SNAKE_CASE = 'git_vision_model' def __init__( self , __snake_case=768 , __snake_case=3072 , __snake_case=12 , __snake_case=12 , __snake_case=3 , __snake_case=224 , __snake_case=16 , __snake_case="quick_gelu" , __snake_case=1e-5 , __snake_case=0.0 , __snake_case=0.02 , **__snake_case , ) -> int: '''simple docstring''' super().__init__(**__snake_case ) __a =hidden_size __a =intermediate_size __a =num_hidden_layers __a =num_attention_heads __a =num_channels __a =patch_size __a =image_size __a =initializer_range __a =attention_dropout __a =layer_norm_eps __a =hidden_act @classmethod def __magic_name__ ( cls , __snake_case , **__snake_case ) -> "PretrainedConfig": '''simple docstring''' cls._set_token_in_kwargs(__snake_case ) __a , __a =cls.get_config_dict(__snake_case , **__snake_case ) # get the vision config dict if we are loading from GITConfig if config_dict.get('model_type' ) == "git": __a =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(__snake_case , **__snake_case ) class __magic_name__ ( lowerCAmelCase_ ): SCREAMING_SNAKE_CASE = 'git' def __init__( self , __snake_case=None , __snake_case=3_0522 , __snake_case=768 , __snake_case=6 , __snake_case=12 , __snake_case=3072 , __snake_case="gelu" , __snake_case=0.1 , __snake_case=0.1 , __snake_case=1024 , __snake_case=0.02 , __snake_case=1e-12 , __snake_case=0 , __snake_case="absolute" , __snake_case=True , __snake_case=False , __snake_case=101 , __snake_case=102 , __snake_case=None , **__snake_case , ) -> Optional[int]: '''simple docstring''' super().__init__(bos_token_id=__snake_case , eos_token_id=__snake_case , pad_token_id=__snake_case , **__snake_case ) if vision_config is None: __a ={} logger.info('vision_config is None. initializing the GitVisionConfig with default values.' ) __a =GitVisionConfig(**__snake_case ) __a =vocab_size __a =hidden_size __a =num_hidden_layers __a =num_attention_heads __a =hidden_act __a =intermediate_size __a =hidden_dropout_prob __a =attention_probs_dropout_prob __a =max_position_embeddings __a =initializer_range __a =layer_norm_eps __a =position_embedding_type __a =use_cache __a =tie_word_embeddings __a =num_image_with_embedding __a =bos_token_id __a =eos_token_id def __magic_name__ ( self ) -> Optional[Any]: '''simple docstring''' __a =copy.deepcopy(self.__dict__ ) __a =self.vision_config.to_dict() __a =self.__class__.model_type return output
218
import copy import os from typing import Union from ...configuration_utils import PretrainedConfig from ...models.auto.modeling_auto import MODEL_FOR_CAUSAL_LM_MAPPING_NAMES from ...utils import logging from ..auto import CONFIG_MAPPING _lowerCAmelCase : List[str] = logging.get_logger(__name__) _lowerCAmelCase : Union[str, Any] = { "Salesforce/instruct-blip-flan-t5": "https://huggingface.co/Salesforce/instruct-blip-flan-t5/resolve/main/config.json", } class __magic_name__ ( lowerCAmelCase_ ): SCREAMING_SNAKE_CASE = 'instructblip_vision_model' def __init__( self , __snake_case=1408 , __snake_case=6144 , __snake_case=39 , __snake_case=16 , __snake_case=224 , __snake_case=14 , __snake_case="gelu" , __snake_case=1e-6 , __snake_case=0.0 , __snake_case=1e-10 , __snake_case=True , **__snake_case , ) -> str: '''simple docstring''' super().__init__(**__snake_case ) __a =hidden_size __a =intermediate_size __a =num_hidden_layers __a =num_attention_heads __a =patch_size __a =image_size __a =initializer_range __a =attention_dropout __a =layer_norm_eps __a =hidden_act __a =qkv_bias @classmethod def __magic_name__ ( cls , __snake_case , **__snake_case ) -> "PretrainedConfig": '''simple docstring''' cls._set_token_in_kwargs(__snake_case ) __a , __a =cls.get_config_dict(__snake_case , **__snake_case ) # get the vision config dict if we are loading from InstructBlipConfig if config_dict.get('model_type' ) == "instructblip": __a =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(__snake_case , **__snake_case ) class __magic_name__ ( lowerCAmelCase_ ): SCREAMING_SNAKE_CASE = 'instructblip_qformer' def __init__( self , __snake_case=3_0522 , __snake_case=768 , __snake_case=12 , __snake_case=12 , __snake_case=3072 , __snake_case="gelu" , __snake_case=0.1 , __snake_case=0.1 , __snake_case=512 , __snake_case=0.02 , __snake_case=1e-12 , __snake_case=0 , __snake_case="absolute" , __snake_case=2 , __snake_case=1408 , **__snake_case , ) -> List[str]: '''simple docstring''' super().__init__(pad_token_id=__snake_case , **__snake_case ) __a =vocab_size __a =hidden_size __a =num_hidden_layers __a =num_attention_heads __a =hidden_act __a =intermediate_size __a =hidden_dropout_prob __a =attention_probs_dropout_prob __a =max_position_embeddings __a =initializer_range __a =layer_norm_eps __a =position_embedding_type __a =cross_attention_frequency __a =encoder_hidden_size @classmethod def __magic_name__ ( cls , __snake_case , **__snake_case ) -> "PretrainedConfig": '''simple docstring''' cls._set_token_in_kwargs(__snake_case ) __a , __a =cls.get_config_dict(__snake_case , **__snake_case ) # get the qformer config dict if we are loading from InstructBlipConfig if config_dict.get('model_type' ) == "instructblip": __a =config_dict['qformer_config'] if "model_type" in config_dict and hasattr(cls , 'model_type' ) and config_dict["model_type"] != cls.model_type: logger.warning( f'You are using a model of type {config_dict["model_type"]} to instantiate a model of type ' f'{cls.model_type}. This is not supported for all configurations of models and can yield errors.' ) return cls.from_dict(__snake_case , **__snake_case ) class __magic_name__ ( lowerCAmelCase_ ): SCREAMING_SNAKE_CASE = 'instructblip' SCREAMING_SNAKE_CASE = True def __init__( self , __snake_case=None , __snake_case=None , __snake_case=None , __snake_case=32 , **__snake_case ) -> str: '''simple docstring''' super().__init__(**__snake_case ) if vision_config is None: __a ={} logger.info('vision_config is None. initializing the InstructBlipVisionConfig with default values.' ) if qformer_config is None: __a ={} logger.info('qformer_config is None. Initializing the InstructBlipQFormerConfig with default values.' ) if text_config is None: __a ={} logger.info('text_config is None. Initializing the text config with default values (`OPTConfig`).' ) __a =InstructBlipVisionConfig(**__snake_case ) __a =InstructBlipQFormerConfig(**__snake_case ) __a =text_config['model_type'] if 'model_type' in text_config else 'opt' __a =CONFIG_MAPPING[text_model_type](**__snake_case ) __a =self.text_config.tie_word_embeddings __a =self.text_config.is_encoder_decoder __a =num_query_tokens __a =self.vision_config.hidden_size __a =self.text_config.model_type in MODEL_FOR_CAUSAL_LM_MAPPING_NAMES __a =1.0 __a =0.02 @classmethod def __magic_name__ ( cls , __snake_case , __snake_case , __snake_case , **__snake_case , ) -> Optional[Any]: '''simple docstring''' return cls( vision_config=vision_config.to_dict() , qformer_config=qformer_config.to_dict() , text_config=text_config.to_dict() , **__snake_case , ) def __magic_name__ ( self ) -> List[Any]: '''simple docstring''' __a =copy.deepcopy(self.__dict__ ) __a =self.vision_config.to_dict() __a =self.qformer_config.to_dict() __a =self.text_config.to_dict() __a =self.__class__.model_type return output
218
1
"""simple docstring""" import argparse import torch from torch import nn from transformers import MBartConfig, MBartForConditionalGeneration def _SCREAMING_SNAKE_CASE ( lowercase_ ) -> int: A__ = [ "encoder.version", "decoder.version", "model.encoder.version", "model.decoder.version", "_float_tensor", "decoder.output_projection.weight", ] for k in ignore_keys: state_dict.pop(lowercase_ , lowercase_ ) def _SCREAMING_SNAKE_CASE ( lowercase_ ) -> Tuple: A__, A__ = emb.weight.shape A__ = nn.Linear(lowercase_ , lowercase_ , bias=lowercase_ ) A__ = emb.weight.data return lin_layer def _SCREAMING_SNAKE_CASE ( lowercase_ , lowercase_="facebook/mbart-large-en-ro" , lowercase_=False , lowercase_=False ) -> List[Any]: A__ = torch.load(lowercase_ , map_location="cpu" )["model"] remove_ignore_keys_(lowercase_ ) A__ = state_dict["encoder.embed_tokens.weight"].shape[0] A__ = MBartConfig.from_pretrained(lowercase_ , vocab_size=lowercase_ ) if mbart_aa and finetuned: A__ = "relu" A__ = state_dict["decoder.embed_tokens.weight"] A__ = MBartForConditionalGeneration(lowercase_ ) model.model.load_state_dict(lowercase_ ) if finetuned: A__ = make_linear_from_emb(model.model.shared ) return model if __name__ == "__main__": SCREAMING_SNAKE_CASE = argparse.ArgumentParser() # Required parameters parser.add_argument( "fairseq_path", type=str, help="bart.large, bart.large.cnn or a path to a model.pt on local filesystem." ) parser.add_argument("pytorch_dump_folder_path", default=None, type=str, help="Path to the output PyTorch model.") parser.add_argument( "--hf_config", default="facebook/mbart-large-cc25", type=str, help="Which huggingface architecture to use: mbart-large", ) parser.add_argument("--mbart_50", action="store_true", help="whether the model is mMART-50 checkpoint") parser.add_argument("--finetuned", action="store_true", help="whether the model is a fine-tuned checkpoint") SCREAMING_SNAKE_CASE = parser.parse_args() SCREAMING_SNAKE_CASE = convert_fairseq_mbart_checkpoint_from_disk( args.fairseq_path, hf_config_path=args.hf_config, finetuned=args.finetuned, mbart_aa=args.mbart_aa ) model.save_pretrained(args.pytorch_dump_folder_path)
354
"""simple docstring""" # Copyright 2022 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 import platform import numpy as np import psutil import torch from accelerate import __version__ as version from accelerate.commands.config import default_config_file, load_config_from_file from ..utils import is_npu_available, is_xpu_available def _SCREAMING_SNAKE_CASE ( lowercase_=None ) -> Any: if subparsers is not None: A__ = subparsers.add_parser("env" ) else: A__ = argparse.ArgumentParser("Accelerate env command" ) parser.add_argument( "--config_file" , default=lowercase_ , help="The config file to use for the default values in the launching script." ) if subparsers is not None: parser.set_defaults(func=lowercase_ ) return parser def _SCREAMING_SNAKE_CASE ( lowercase_ ) -> Dict: A__ = torch.__version__ A__ = torch.cuda.is_available() A__ = is_xpu_available() A__ = is_npu_available() A__ = "Not found" # Get the default from the config file. if args.config_file is not None or os.path.isfile(lowercase_ ): A__ = load_config_from_file(args.config_file ).to_dict() A__ = { "`Accelerate` version": version, "Platform": platform.platform(), "Python version": platform.python_version(), "Numpy version": np.__version__, "PyTorch version (GPU?)": f"""{pt_version} ({pt_cuda_available})""", "PyTorch XPU available": str(lowercase_ ), "PyTorch NPU available": str(lowercase_ ), "System RAM": f"""{psutil.virtual_memory().total / 10_24 ** 3:.2f} GB""", } if pt_cuda_available: A__ = torch.cuda.get_device_name() print("\nCopy-and-paste the text below in your GitHub issue\n" ) print("\n".join([f"""- {prop}: {val}""" for prop, val in info.items()] ) ) print("- `Accelerate` default config:" if args.config_file is None else "- `Accelerate` config passed:" ) A__ = ( "\n".join([f"""\t- {prop}: {val}""" for prop, val in accelerate_config.items()] ) if isinstance(lowercase_ , lowercase_ ) else f"""\t{accelerate_config}""" ) print(lowercase_ ) A__ = accelerate_config return info def _SCREAMING_SNAKE_CASE ( ) -> int: A__ = env_command_parser() A__ = parser.parse_args() env_command(lowercase_ ) return 0 if __name__ == "__main__": raise SystemExit(main())
230
0
from __future__ import annotations _snake_case : Tuple = 8.9_88E9 # units = N * m^s * C^-2 def lowerCAmelCase_ ( __lowerCamelCase , __lowerCamelCase , __lowerCamelCase , __lowerCamelCase ): __snake_case : List[str] = abs(chargea * chargea ) if (force, chargea, chargea, distance).count(0 ) != 1: raise ValueError("One and only one argument must be 0" ) if distance < 0: raise ValueError("Distance cannot be negative" ) if force == 0: __snake_case : List[Any] = COULOMBS_CONSTANT * charge_product / (distance**2) return {"force": force} elif chargea == 0: __snake_case : str = abs(__lowerCamelCase ) * (distance**2) / (COULOMBS_CONSTANT * chargea) return {"charge1": chargea} elif chargea == 0: __snake_case : List[Any] = abs(__lowerCamelCase ) * (distance**2) / (COULOMBS_CONSTANT * chargea) return {"charge2": chargea} elif distance == 0: __snake_case : List[Any] = (COULOMBS_CONSTANT * charge_product / abs(__lowerCamelCase )) ** 0.5 return {"distance": distance} raise ValueError("Exactly one argument must be 0" ) if __name__ == "__main__": import doctest doctest.testmod()
123
from __future__ import annotations import time from math import sqrt # 1 for manhattan, 0 for euclidean _snake_case : Union[str, Any] = 0 _snake_case : List[str] = [ [0, 0, 0, 0, 0, 0, 0], [0, 1, 0, 0, 0, 0, 0], # 0 are free path whereas 1's are obstacles [0, 0, 0, 0, 0, 0, 0], [0, 0, 1, 0, 0, 0, 0], [1, 0, 1, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 1, 0, 0], ] _snake_case : List[Any] = [[-1, 0], [0, -1], [1, 0], [0, 1]] # up, left, down, right _snake_case : int = tuple[int, int] class a : """simple docstring""" def __init__( self : int , lowerCamelCase : int , lowerCamelCase : int , lowerCamelCase : int , lowerCamelCase : int , lowerCamelCase : int , lowerCamelCase : Node | None , ) -> None: __snake_case : List[str] = pos_x __snake_case : List[str] = pos_y __snake_case : Dict = (pos_y, pos_x) __snake_case : List[Any] = goal_x __snake_case : Union[str, Any] = goal_y __snake_case : int = g_cost __snake_case : List[Any] = parent __snake_case : Optional[Any] = self.calculate_heuristic() __snake_case : Union[str, Any] = self.g_cost + self.h_cost def __snake_case ( self : Optional[int] ) -> float: __snake_case : Union[str, Any] = self.pos_x - self.goal_x __snake_case : Tuple = self.pos_y - self.goal_y if HEURISTIC == 1: return abs(lowerCamelCase ) + abs(lowerCamelCase ) else: return sqrt(dy**2 + dx**2 ) def __lt__( self : Optional[int] , lowerCamelCase : Node ) -> bool: return self.f_cost < other.f_cost class a : """simple docstring""" def __init__( self : List[Any] , lowerCamelCase : TPosition , lowerCamelCase : TPosition ) -> Optional[Any]: __snake_case : Any = Node(start[1] , start[0] , goal[1] , goal[0] , 0 , lowerCamelCase ) __snake_case : Tuple = Node(goal[1] , goal[0] , goal[1] , goal[0] , 99999 , lowerCamelCase ) __snake_case : str = [self.start] __snake_case : list[Node] = [] __snake_case : int = False def __snake_case ( self : Tuple ) -> list[TPosition]: while self.open_nodes: # Open Nodes are sorted using __lt__ self.open_nodes.sort() __snake_case : Dict = self.open_nodes.pop(0 ) if current_node.pos == self.target.pos: return self.retrace_path(lowerCamelCase ) self.closed_nodes.append(lowerCamelCase ) __snake_case : Tuple = self.get_successors(lowerCamelCase ) for child_node in successors: if child_node in self.closed_nodes: continue if child_node not in self.open_nodes: self.open_nodes.append(lowerCamelCase ) else: # retrieve the best current path __snake_case : Any = self.open_nodes.pop(self.open_nodes.index(lowerCamelCase ) ) if child_node.g_cost < better_node.g_cost: self.open_nodes.append(lowerCamelCase ) else: self.open_nodes.append(lowerCamelCase ) return [self.start.pos] def __snake_case ( self : Optional[Any] , lowerCamelCase : Node ) -> list[Node]: __snake_case : int = [] for action in delta: __snake_case : Tuple = parent.pos_x + action[1] __snake_case : Tuple = parent.pos_y + action[0] if not (0 <= pos_x <= len(grid[0] ) - 1 and 0 <= pos_y <= len(lowerCamelCase ) - 1): continue if grid[pos_y][pos_x] != 0: continue successors.append( Node( lowerCamelCase , lowerCamelCase , self.target.pos_y , self.target.pos_x , parent.g_cost + 1 , lowerCamelCase , ) ) return successors def __snake_case ( self : Optional[Any] , lowerCamelCase : Node | None ) -> list[TPosition]: __snake_case : List[Any] = node __snake_case : Optional[int] = [] while current_node is not None: path.append((current_node.pos_y, current_node.pos_x) ) __snake_case : Tuple = current_node.parent path.reverse() return path class a : """simple docstring""" def __init__( self : Optional[Any] , lowerCamelCase : TPosition , lowerCamelCase : TPosition ) -> None: __snake_case : str = AStar(lowerCamelCase , lowerCamelCase ) __snake_case : int = AStar(lowerCamelCase , lowerCamelCase ) __snake_case : int = False def __snake_case ( self : str ) -> list[TPosition]: while self.fwd_astar.open_nodes or self.bwd_astar.open_nodes: self.fwd_astar.open_nodes.sort() self.bwd_astar.open_nodes.sort() __snake_case : Optional[int] = self.fwd_astar.open_nodes.pop(0 ) __snake_case : str = self.bwd_astar.open_nodes.pop(0 ) if current_bwd_node.pos == current_fwd_node.pos: return self.retrace_bidirectional_path( lowerCamelCase , lowerCamelCase ) self.fwd_astar.closed_nodes.append(lowerCamelCase ) self.bwd_astar.closed_nodes.append(lowerCamelCase ) __snake_case : Optional[Any] = current_bwd_node __snake_case : Any = current_fwd_node __snake_case : int = { self.fwd_astar: self.fwd_astar.get_successors(lowerCamelCase ), self.bwd_astar: self.bwd_astar.get_successors(lowerCamelCase ), } for astar in [self.fwd_astar, self.bwd_astar]: for child_node in successors[astar]: if child_node in astar.closed_nodes: continue if child_node not in astar.open_nodes: astar.open_nodes.append(lowerCamelCase ) else: # retrieve the best current path __snake_case : Optional[int] = astar.open_nodes.pop( astar.open_nodes.index(lowerCamelCase ) ) if child_node.g_cost < better_node.g_cost: astar.open_nodes.append(lowerCamelCase ) else: astar.open_nodes.append(lowerCamelCase ) return [self.fwd_astar.start.pos] def __snake_case ( self : Any , lowerCamelCase : Node , lowerCamelCase : Node ) -> list[TPosition]: __snake_case : Optional[int] = self.fwd_astar.retrace_path(lowerCamelCase ) __snake_case : Optional[Any] = self.bwd_astar.retrace_path(lowerCamelCase ) bwd_path.pop() bwd_path.reverse() __snake_case : int = fwd_path + bwd_path return path if __name__ == "__main__": # all coordinates are given in format [y,x] _snake_case : Dict = (0, 0) _snake_case : Any = (len(grid) - 1, len(grid[0]) - 1) for elem in grid: print(elem) _snake_case : List[Any] = time.time() _snake_case : Dict = AStar(init, goal) _snake_case : Optional[int] = a_star.search() _snake_case : Optional[Any] = time.time() - start_time print(f'''AStar execution time = {end_time:f} seconds''') _snake_case : List[str] = time.time() _snake_case : Any = BidirectionalAStar(init, goal) _snake_case : List[str] = time.time() - bd_start_time print(f'''BidirectionalAStar execution time = {bd_end_time:f} seconds''')
123
1
import argparse import logging import os from datetime import datetime import numpy as np import torch from torch import nn from torch.utils.data import DataLoader, RandomSampler, TensorDataset from tqdm import tqdm from transformers import GPTaLMHeadModel __snake_case : List[Any] = logging.getLogger(__name__) def _lowercase ( __snake_case ,__snake_case ) -> Dict: # save results if os.path.exists(__snake_case ): if os.path.exists(os.path.join(__snake_case ,"config.json" ) ) and os.path.isfile( os.path.join(__snake_case ,"config.json" ) ): os.remove(os.path.join(__snake_case ,"config.json" ) ) if os.path.exists(os.path.join(__snake_case ,"pytorch_model.bin" ) ) and os.path.isfile( os.path.join(__snake_case ,"pytorch_model.bin" ) ): os.remove(os.path.join(__snake_case ,"pytorch_model.bin" ) ) else: os.makedirs(__snake_case ) model.save_pretrained(__snake_case ) def _lowercase ( __snake_case ,__snake_case=False ) -> Optional[Any]: __lowerCAmelCase : int = 2 if unlogit: __lowerCAmelCase : int = torch.pow(__snake_case ,__snake_case ) __lowerCAmelCase : List[Any] = p * torch.log(__snake_case ) __lowerCAmelCase : int = 0 return -plogp.sum(dim=-1 ) def _lowercase ( __snake_case ) -> List[Any]: logger.info("lv, h >\t" + "\t".join(F"""{x + 1}""" for x in range(len(__snake_case ) ) ) ) for row in range(len(__snake_case ) ): if tensor.dtype != torch.long: logger.info(F"""layer {row + 1}:\t""" + "\t".join(F"""{x:.5f}""" for x in tensor[row].cpu().data ) ) else: logger.info(F"""layer {row + 1}:\t""" + "\t".join(F"""{x:d}""" for x in tensor[row].cpu().data ) ) def _lowercase ( __snake_case ,__snake_case ,__snake_case ,__snake_case=True ,__snake_case=True ,__snake_case=None ,__snake_case=False ) -> int: __lowerCAmelCase : Tuple = model.config.num_hidden_layers, model.config.num_attention_heads __lowerCAmelCase : List[Any] = torch.zeros(__snake_case ,__snake_case ).to(args.device ) __lowerCAmelCase : List[Any] = torch.zeros(__snake_case ,__snake_case ).to(args.device ) if head_mask is None: __lowerCAmelCase : Dict = torch.ones(__snake_case ,__snake_case ).to(args.device ) head_mask.requires_grad_(requires_grad=__snake_case ) # If actually pruned attention multi-head, set head mask to None to avoid shape mismatch if actually_pruned: __lowerCAmelCase : List[str] = None __lowerCAmelCase : str = 0.0 __lowerCAmelCase : Tuple = 0.0 for step, inputs in enumerate(tqdm(__snake_case ,desc="Iteration" ,disable=args.local_rank not in [-1, 0] ) ): __lowerCAmelCase : List[Any] = tuple(t.to(args.device ) for t in inputs ) (__lowerCAmelCase ) : Union[str, Any] = inputs # Do a forward pass (not with torch.no_grad() since we need gradients for importance score - see below) __lowerCAmelCase : Any = model(__snake_case ,labels=__snake_case ,head_mask=__snake_case ) # (loss), lm_logits, presents, (all hidden_states), (attentions) __lowerCAmelCase : Tuple = ( outputs[0], outputs[1], outputs[-1], ) # Loss and logits are the first, attention the last loss.backward() # Backpropagate to populate the gradients in the head mask total_loss += loss.detach().cpu().numpy() if compute_entropy: for layer, attn in enumerate(__snake_case ): __lowerCAmelCase : Any = entropy(attn.detach() ,__snake_case ) attn_entropy[layer] += masked_entropy.sum(-1 ).sum(0 ).sum(0 ).detach() if compute_importance: head_importance += head_mask.grad.abs().detach() tot_tokens += torch.ones_like(__snake_case ).float().detach().sum().data # Normalize attn_entropy /= tot_tokens head_importance /= tot_tokens # Layerwise importance normalization if not args.dont_normalize_importance_by_layer: __lowerCAmelCase : int = 2 __lowerCAmelCase : Optional[Any] = torch.pow(torch.pow(__snake_case ,__snake_case ).sum(-1 ) ,1 / exponent ) head_importance /= norm_by_layer.unsqueeze(-1 ) + 1e-2_0 if not args.dont_normalize_global_importance: __lowerCAmelCase : Optional[int] = (head_importance - head_importance.min()) / (head_importance.max() - head_importance.min()) # Print matrices if compute_entropy: logger.info("Attention entropies" ) print_ad_tensor(__snake_case ) if compute_importance: logger.info("Head importance scores" ) print_ad_tensor(__snake_case ) logger.info("Head ranked by importance scores" ) __lowerCAmelCase : str = torch.zeros(head_importance.numel() ,dtype=torch.long ,device=args.device ) __lowerCAmelCase : Dict = torch.arange( head_importance.numel() ,device=args.device ) __lowerCAmelCase : Dict = head_ranks.view_as(__snake_case ) print_ad_tensor(__snake_case ) return attn_entropy, head_importance, total_loss def _lowercase ( __snake_case ,__snake_case ,__snake_case ) -> Tuple: __lowerCAmelCase : Any = compute_heads_importance(__snake_case ,__snake_case ,__snake_case ,compute_entropy=__snake_case ) __lowerCAmelCase : Any = 1 / loss # instead of downsteam score use the LM loss logger.info("Pruning: original score: %f, threshold: %f" ,__snake_case ,original_score * args.masking_threshold ) __lowerCAmelCase : List[Any] = torch.ones_like(__snake_case ) __lowerCAmelCase : Optional[int] = max(1 ,int(new_head_mask.numel() * args.masking_amount ) ) __lowerCAmelCase : str = original_score while current_score >= original_score * args.masking_threshold: __lowerCAmelCase : int = new_head_mask.clone().detach() # save current head mask # heads from least important to most - keep only not-masked heads __lowerCAmelCase : List[Any] = float("Inf" ) __lowerCAmelCase : Tuple = head_importance.view(-1 ).sort()[1] if len(__snake_case ) <= num_to_mask: print("BREAK BY num_to_mask" ) break # mask heads __lowerCAmelCase : str = current_heads_to_mask[:num_to_mask] logger.info("Heads to mask: %s" ,str(current_heads_to_mask.tolist() ) ) __lowerCAmelCase : Dict = new_head_mask.view(-1 ) __lowerCAmelCase : List[Any] = 0.0 __lowerCAmelCase : Optional[int] = new_head_mask.view_as(__snake_case ) __lowerCAmelCase : int = new_head_mask.clone().detach() print_ad_tensor(__snake_case ) # Compute metric and head importance again __lowerCAmelCase : List[str] = compute_heads_importance( __snake_case ,__snake_case ,__snake_case ,compute_entropy=__snake_case ,head_mask=__snake_case ) __lowerCAmelCase : List[str] = 1 / loss logger.info( "Masking: current score: %f, remaining heads %d (%.1f percents)" ,__snake_case ,new_head_mask.sum() ,new_head_mask.sum() / new_head_mask.numel() * 100 ,) logger.info("Final head mask" ) print_ad_tensor(__snake_case ) np.save(os.path.join(args.output_dir ,"head_mask.npy" ) ,head_mask.detach().cpu().numpy() ) return head_mask def _lowercase ( __snake_case ,__snake_case ,__snake_case ,__snake_case ) -> Optional[int]: __lowerCAmelCase : Optional[Any] = datetime.now() __lowerCAmelCase : List[str] = compute_heads_importance( __snake_case ,__snake_case ,__snake_case ,compute_entropy=__snake_case ,compute_importance=__snake_case ,head_mask=__snake_case ) __lowerCAmelCase : List[Any] = 1 / loss __lowerCAmelCase : List[str] = datetime.now() - before_time __lowerCAmelCase : Dict = sum(p.numel() for p in model.parameters() ) __lowerCAmelCase : Any = { layer: (1 - head_mask[layer].long()).nonzero().squeeze().tolist() for layer in range(len(__snake_case ) ) } for k, v in heads_to_prune.items(): if isinstance(__snake_case ,__snake_case ): __lowerCAmelCase : List[Any] = [ v, ] assert sum(len(__snake_case ) for h in heads_to_prune.values() ) == (1 - head_mask.long()).sum().item() model.prune_heads(__snake_case ) __lowerCAmelCase : Any = sum(p.numel() for p in model.parameters() ) __lowerCAmelCase : str = datetime.now() __lowerCAmelCase : List[Any] = compute_heads_importance( __snake_case ,__snake_case ,__snake_case ,compute_entropy=__snake_case ,compute_importance=__snake_case ,head_mask=__snake_case ,actually_pruned=__snake_case ,) __lowerCAmelCase : Tuple = 1 / loss __lowerCAmelCase : Dict = datetime.now() - before_time logger.info( "Pruning: original num of params: %.2e, after pruning %.2e (%.1f percents)" ,__snake_case ,__snake_case ,pruned_num_params / original_num_params * 100 ,) logger.info("Pruning: score with masking: %f score with pruning: %f" ,__snake_case ,__snake_case ) logger.info("Pruning: speed ratio (original timing / new timing): %f percents" ,original_time / new_time * 100 ) save_model(__snake_case ,args.output_dir ) def _lowercase ( ) -> Optional[Any]: __lowerCAmelCase : Any = argparse.ArgumentParser() # Required parameters parser.add_argument( "--data_dir" ,default=__snake_case ,type=__snake_case ,required=__snake_case ,help="The input data dir. Should contain the .tsv files (or other data files) for the task." ,) parser.add_argument( "--model_name_or_path" ,default=__snake_case ,type=__snake_case ,required=__snake_case ,help="Path to pretrained model or model identifier from huggingface.co/models" ,) parser.add_argument( "--output_dir" ,default=__snake_case ,type=__snake_case ,required=__snake_case ,help="The output directory where the model predictions and checkpoints will be written." ,) # Other parameters parser.add_argument( "--config_name" ,default="" ,type=__snake_case ,help="Pretrained config name or path if not the same as model_name_or_path" ,) parser.add_argument( "--tokenizer_name" ,default="" ,type=__snake_case ,help="Pretrained tokenizer name or path if not the same as model_name_or_path" ,) parser.add_argument( "--cache_dir" ,default=__snake_case ,type=__snake_case ,help="Where do you want to store the pre-trained models downloaded from s3" ,) parser.add_argument( "--data_subset" ,type=__snake_case ,default=-1 ,help="If > 0: limit the data to a subset of data_subset instances." ) parser.add_argument( "--overwrite_output_dir" ,action="store_true" ,help="Whether to overwrite data in output directory" ) parser.add_argument( "--overwrite_cache" ,action="store_true" ,help="Overwrite the cached training and evaluation sets" ) parser.add_argument( "--dont_normalize_importance_by_layer" ,action="store_true" ,help="Don't normalize importance score by layers" ) parser.add_argument( "--dont_normalize_global_importance" ,action="store_true" ,help="Don't normalize all importance scores between 0 and 1" ,) parser.add_argument( "--try_masking" ,action="store_true" ,help="Whether to try to mask head until a threshold of accuracy." ) parser.add_argument( "--masking_threshold" ,default=0.9 ,type=__snake_case ,help="masking threshold in term of metrics (stop masking when metric < threshold * original metric value)." ,) parser.add_argument( "--masking_amount" ,default=0.1 ,type=__snake_case ,help="Amount to heads to masking at each masking step." ) parser.add_argument("--metric_name" ,default="acc" ,type=__snake_case ,help="Metric to use for head masking." ) parser.add_argument( "--max_seq_length" ,default=128 ,type=__snake_case ,help=( "The maximum total input sequence length after WordPiece tokenization. \n" "Sequences longer than this will be truncated, sequences shorter padded." ) ,) parser.add_argument("--batch_size" ,default=1 ,type=__snake_case ,help="Batch size." ) parser.add_argument("--seed" ,type=__snake_case ,default=42 ) parser.add_argument("--local_rank" ,type=__snake_case ,default=-1 ,help="local_rank for distributed training on gpus" ) parser.add_argument("--no_cuda" ,action="store_true" ,help="Whether not to use CUDA when available" ) parser.add_argument("--server_ip" ,type=__snake_case ,default="" ,help="Can be used for distant debugging." ) parser.add_argument("--server_port" ,type=__snake_case ,default="" ,help="Can be used for distant debugging." ) __lowerCAmelCase : List[str] = parser.parse_args() if args.server_ip and args.server_port: # Distant debugging - see https://code.visualstudio.com/docs/python/debugging#_attach-to-a-local-script import ptvsd print("Waiting for debugger attach" ) ptvsd.enable_attach(address=(args.server_ip, args.server_port) ,redirect_output=__snake_case ) ptvsd.wait_for_attach() # Setup devices and distributed training if args.local_rank == -1 or args.no_cuda: __lowerCAmelCase : Dict = torch.device("cuda" if torch.cuda.is_available() and not args.no_cuda else "cpu" ) __lowerCAmelCase : List[str] = 0 if args.no_cuda else torch.cuda.device_count() else: torch.cuda.set_device(args.local_rank ) __lowerCAmelCase : List[Any] = torch.device("cuda" ,args.local_rank ) __lowerCAmelCase : Union[str, Any] = 1 torch.distributed.init_process_group(backend="nccl" ) # Initializes the distributed backend # Setup logging logging.basicConfig(level=logging.INFO if args.local_rank in [-1, 0] else logging.WARN ) logger.info("device: {} n_gpu: {}, distributed: {}".format(args.device ,args.n_gpu ,bool(args.local_rank != -1 ) ) ) __lowerCAmelCase : Dict = GPTaLMHeadModel.from_pretrained(args.model_name_or_path ) # Distributed and parallel training model.to(args.device ) if args.local_rank != -1: __lowerCAmelCase : str = nn.parallel.DistributedDataParallel( __snake_case ,device_ids=[args.local_rank] ,output_device=args.local_rank ,find_unused_parameters=__snake_case ) elif args.n_gpu > 1: __lowerCAmelCase : Tuple = nn.DataParallel(__snake_case ) # Print/save training arguments os.makedirs(args.output_dir ,exist_ok=__snake_case ) torch.save(__snake_case ,os.path.join(args.output_dir ,"run_args.bin" ) ) logger.info("Training/evaluation parameters %s" ,__snake_case ) # Prepare dataset __lowerCAmelCase : List[Any] = np.concatenate( [ np.loadtxt(args.data_dir ,dtype=np.intaa ), ] ) __lowerCAmelCase : Dict = (torch.from_numpy(__snake_case ),) __lowerCAmelCase : Optional[Any] = TensorDataset(*__snake_case ) __lowerCAmelCase : List[str] = RandomSampler(__snake_case ) __lowerCAmelCase : List[str] = DataLoader(__snake_case ,sampler=__snake_case ,batch_size=args.batch_size ) # Compute head entropy and importance score compute_heads_importance(__snake_case ,__snake_case ,__snake_case ) # Try head masking (set heads to zero until the score goes under a threshole) # and head pruning (remove masked heads and see the effect on the network) if args.try_masking and args.masking_threshold > 0.0 and args.masking_threshold < 1.0: __lowerCAmelCase : Tuple = mask_heads(__snake_case ,__snake_case ,__snake_case ) prune_heads(__snake_case ,__snake_case ,__snake_case ,__snake_case ) if __name__ == "__main__": main()
370
"""simple docstring""" import unittest import numpy as np from transformers.testing_utils import require_torch, require_vision from transformers.utils import is_torch_available, is_vision_available from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_video_inputs if is_torch_available(): import torch if is_vision_available(): from PIL import Image from transformers import VivitImageProcessor class A__ ( unittest.TestCase ): '''simple docstring''' def __init__( self: str , _SCREAMING_SNAKE_CASE: Any , _SCREAMING_SNAKE_CASE: List[Any]=7 , _SCREAMING_SNAKE_CASE: Optional[Any]=3 , _SCREAMING_SNAKE_CASE: int=10 , _SCREAMING_SNAKE_CASE: Tuple=18 , _SCREAMING_SNAKE_CASE: Union[str, Any]=30 , _SCREAMING_SNAKE_CASE: Any=400 , _SCREAMING_SNAKE_CASE: List[str]=True , _SCREAMING_SNAKE_CASE: Union[str, Any]=None , _SCREAMING_SNAKE_CASE: str=True , _SCREAMING_SNAKE_CASE: Union[str, Any]=[0.5, 0.5, 0.5] , _SCREAMING_SNAKE_CASE: Any=[0.5, 0.5, 0.5] , _SCREAMING_SNAKE_CASE: Dict=None , ) -> Union[str, Any]: """simple docstring""" __lowerCAmelCase : Optional[Any] = size if size is not None else {"shortest_edge": 18} __lowerCAmelCase : int = crop_size if crop_size is not None else {"height": 18, "width": 18} __lowerCAmelCase : Tuple = parent __lowerCAmelCase : List[Any] = batch_size __lowerCAmelCase : List[str] = num_channels __lowerCAmelCase : int = num_frames __lowerCAmelCase : Union[str, Any] = image_size __lowerCAmelCase : Tuple = min_resolution __lowerCAmelCase : Tuple = max_resolution __lowerCAmelCase : str = do_resize __lowerCAmelCase : Optional[int] = size __lowerCAmelCase : Optional[int] = do_normalize __lowerCAmelCase : Dict = image_mean __lowerCAmelCase : List[Any] = image_std __lowerCAmelCase : List[Any] = crop_size def _SCREAMING_SNAKE_CASE ( self: int) -> Union[str, Any]: """simple docstring""" return { "image_mean": self.image_mean, "image_std": self.image_std, "do_normalize": self.do_normalize, "do_resize": self.do_resize, "size": self.size, "crop_size": self.crop_size, } @require_torch @require_vision class A__ ( __SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' SCREAMING_SNAKE_CASE = VivitImageProcessor if is_vision_available() else None def _SCREAMING_SNAKE_CASE ( self: int) -> Tuple: """simple docstring""" __lowerCAmelCase : Optional[int] = VivitImageProcessingTester(self) @property def _SCREAMING_SNAKE_CASE ( self: int) -> Tuple: """simple docstring""" return self.image_processor_tester.prepare_image_processor_dict() def _SCREAMING_SNAKE_CASE ( self: List[Any]) -> Optional[int]: """simple docstring""" __lowerCAmelCase : int = self.image_processing_class(**self.image_processor_dict) self.assertTrue(hasattr(_SCREAMING_SNAKE_CASE , "image_mean")) self.assertTrue(hasattr(_SCREAMING_SNAKE_CASE , "image_std")) self.assertTrue(hasattr(_SCREAMING_SNAKE_CASE , "do_normalize")) self.assertTrue(hasattr(_SCREAMING_SNAKE_CASE , "do_resize")) self.assertTrue(hasattr(_SCREAMING_SNAKE_CASE , "do_center_crop")) self.assertTrue(hasattr(_SCREAMING_SNAKE_CASE , "size")) def _SCREAMING_SNAKE_CASE ( self: Any) -> Optional[Any]: """simple docstring""" __lowerCAmelCase : List[Any] = self.image_processing_class.from_dict(self.image_processor_dict) self.assertEqual(image_processor.size , {"shortest_edge": 18}) self.assertEqual(image_processor.crop_size , {"height": 18, "width": 18}) __lowerCAmelCase : Optional[int] = self.image_processing_class.from_dict(self.image_processor_dict , size=42 , crop_size=84) self.assertEqual(image_processor.size , {"shortest_edge": 42}) self.assertEqual(image_processor.crop_size , {"height": 84, "width": 84}) def _SCREAMING_SNAKE_CASE ( self: int) -> Union[str, Any]: """simple docstring""" __lowerCAmelCase : List[str] = self.image_processing_class(**self.image_processor_dict) # create random PIL videos __lowerCAmelCase : Dict = prepare_video_inputs(self.image_processor_tester , equal_resolution=_SCREAMING_SNAKE_CASE) for video in video_inputs: self.assertIsInstance(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE) self.assertIsInstance(video[0] , Image.Image) # Test not batched input __lowerCAmelCase : Any = image_processing(video_inputs[0] , return_tensors="pt").pixel_values self.assertEqual( encoded_videos.shape , ( 1, self.image_processor_tester.num_frames, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size["height"], self.image_processor_tester.crop_size["width"], ) , ) # Test batched __lowerCAmelCase : str = image_processing(_SCREAMING_SNAKE_CASE , return_tensors="pt").pixel_values self.assertEqual( encoded_videos.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_frames, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size["height"], self.image_processor_tester.crop_size["width"], ) , ) def _SCREAMING_SNAKE_CASE ( self: List[str]) -> int: """simple docstring""" __lowerCAmelCase : Optional[int] = self.image_processing_class(**self.image_processor_dict) # create random numpy tensors __lowerCAmelCase : Optional[int] = prepare_video_inputs(self.image_processor_tester , equal_resolution=_SCREAMING_SNAKE_CASE , numpify=_SCREAMING_SNAKE_CASE) for video in video_inputs: self.assertIsInstance(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE) self.assertIsInstance(video[0] , np.ndarray) # Test not batched input __lowerCAmelCase : Any = image_processing(video_inputs[0] , return_tensors="pt").pixel_values self.assertEqual( encoded_videos.shape , ( 1, self.image_processor_tester.num_frames, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size["height"], self.image_processor_tester.crop_size["width"], ) , ) # Test batched __lowerCAmelCase : List[str] = image_processing(_SCREAMING_SNAKE_CASE , return_tensors="pt").pixel_values self.assertEqual( encoded_videos.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_frames, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size["height"], self.image_processor_tester.crop_size["width"], ) , ) def _SCREAMING_SNAKE_CASE ( self: Dict) -> int: """simple docstring""" __lowerCAmelCase : str = self.image_processing_class(**self.image_processor_dict) # create random PyTorch tensors __lowerCAmelCase : Optional[int] = prepare_video_inputs(self.image_processor_tester , equal_resolution=_SCREAMING_SNAKE_CASE , torchify=_SCREAMING_SNAKE_CASE) for video in video_inputs: self.assertIsInstance(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE) self.assertIsInstance(video[0] , torch.Tensor) # Test not batched input __lowerCAmelCase : List[str] = image_processing(video_inputs[0] , return_tensors="pt").pixel_values self.assertEqual( encoded_videos.shape , ( 1, self.image_processor_tester.num_frames, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size["height"], self.image_processor_tester.crop_size["width"], ) , ) # Test batched __lowerCAmelCase : Any = image_processing(_SCREAMING_SNAKE_CASE , return_tensors="pt").pixel_values self.assertEqual( encoded_videos.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_frames, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size["height"], self.image_processor_tester.crop_size["width"], ) , )
58
0
from unittest import TestCase from datasets import Sequence, Value from datasets.arrow_dataset import Dataset class a__ ( A__ ): def __UpperCamelCase ( self : List[Any] ): """simple docstring""" return [ {"col_1": 3, "col_2": "a"}, {"col_1": 2, "col_2": "b"}, {"col_1": 1, "col_2": "c"}, {"col_1": 0, "col_2": "d"}, ] def __UpperCamelCase ( self : int ): """simple docstring""" SCREAMING_SNAKE_CASE_ : int = {"col_1": [3, 2, 1, 0], "col_2": ["a", "b", "c", "d"]} return Dataset.from_dict(_A ) def __UpperCamelCase ( self : List[str] ): """simple docstring""" SCREAMING_SNAKE_CASE_ : List[str] = self._create_example_records() SCREAMING_SNAKE_CASE_ : List[str] = Dataset.from_list(_A ) self.assertListEqual(dset.column_names,["col_1", "col_2"] ) for i, r in enumerate(_A ): self.assertDictEqual(_A,example_records[i] ) def __UpperCamelCase ( self : Dict ): """simple docstring""" SCREAMING_SNAKE_CASE_ : Union[str, Any] = self._create_example_records() SCREAMING_SNAKE_CASE_ : Any = Dataset.from_list(_A ) SCREAMING_SNAKE_CASE_ : Dict = Dataset.from_dict({k: [r[k] for r in example_records] for k in example_records[0]} ) self.assertEqual(dset.info,dset_from_dict.info ) def __UpperCamelCase ( self : Tuple ): # checks what happens with missing columns """simple docstring""" SCREAMING_SNAKE_CASE_ : Dict = [{"col_1": 1}, {"col_2": "x"}] SCREAMING_SNAKE_CASE_ : Tuple = Dataset.from_list(_A ) self.assertDictEqual(dset[0],{"col_1": 1} ) self.assertDictEqual(dset[1],{"col_1": None} ) # NB: first record is used for columns def __UpperCamelCase ( self : Any ): # checks if the type can be inferred from the second record """simple docstring""" SCREAMING_SNAKE_CASE_ : int = [{"col_1": []}, {"col_1": [1, 2]}] SCREAMING_SNAKE_CASE_ : Optional[Any] = Dataset.from_list(_A ) self.assertEqual(dset.info.features["col_1"],Sequence(Value("int64" ) ) ) def __UpperCamelCase ( self : List[Any] ): """simple docstring""" SCREAMING_SNAKE_CASE_ : Optional[int] = Dataset.from_list([] ) self.assertEqual(len(_A ),0 ) self.assertListEqual(dset.column_names,[] )
18
import os from glob import glob import imageio import torch import torchvision import wandb from img_processing import custom_to_pil, loop_post_process, preprocess, preprocess_vqgan from loaders import load_vqgan from PIL import Image from torch import nn from transformers import CLIPModel, CLIPTokenizerFast from utils import get_device, get_timestamp, show_pil class _a : def __init__(self, SCREAMING_SNAKE_CASE_ = "cpu", SCREAMING_SNAKE_CASE_ = "openai/clip-vit-large-patch14" ) -> None: UpperCAmelCase_: Optional[Any] = device UpperCAmelCase_: Optional[Any] = CLIPTokenizerFast.from_pretrained(SCREAMING_SNAKE_CASE_ ) UpperCAmelCase_: Tuple = [0.4_8_1_4_5_4_6_6, 0.4_5_7_8_2_7_5, 0.4_0_8_2_1_0_7_3] UpperCAmelCase_: Optional[Any] = [0.2_6_8_6_2_9_5_4, 0.2_6_1_3_0_2_5_8, 0.2_7_5_7_7_7_1_1] UpperCAmelCase_: Optional[Any] = torchvision.transforms.Normalize(self.image_mean, self.image_std ) UpperCAmelCase_: Tuple = torchvision.transforms.Resize(224 ) UpperCAmelCase_: Any = torchvision.transforms.CenterCrop(224 ) def __snake_case (self, SCREAMING_SNAKE_CASE_ ) -> Optional[Any]: UpperCAmelCase_: Dict = self.resize(SCREAMING_SNAKE_CASE_ ) UpperCAmelCase_: Optional[int] = self.center_crop(SCREAMING_SNAKE_CASE_ ) UpperCAmelCase_: Tuple = self.normalize(SCREAMING_SNAKE_CASE_ ) return images def __call__(self, SCREAMING_SNAKE_CASE_=None, SCREAMING_SNAKE_CASE_=None, **SCREAMING_SNAKE_CASE_ ) -> List[str]: UpperCAmelCase_: Dict = self.tokenizer(text=SCREAMING_SNAKE_CASE_, **SCREAMING_SNAKE_CASE_ ) UpperCAmelCase_: Tuple = self.preprocess_img(SCREAMING_SNAKE_CASE_ ) UpperCAmelCase_: int = {key: value.to(self.device ) for (key, value) in encoding.items()} return encoding class _a ( nn.Module ): def __init__(self, SCREAMING_SNAKE_CASE_=10, SCREAMING_SNAKE_CASE_=0.0_1, SCREAMING_SNAKE_CASE_=None, SCREAMING_SNAKE_CASE_=None, SCREAMING_SNAKE_CASE_=None, SCREAMING_SNAKE_CASE_=None, SCREAMING_SNAKE_CASE_=None, SCREAMING_SNAKE_CASE_=None, SCREAMING_SNAKE_CASE_=False, SCREAMING_SNAKE_CASE_=True, SCREAMING_SNAKE_CASE_="image", SCREAMING_SNAKE_CASE_=True, SCREAMING_SNAKE_CASE_=False, SCREAMING_SNAKE_CASE_=False, SCREAMING_SNAKE_CASE_=False, ) -> None: super().__init__() UpperCAmelCase_: List[Any] = None UpperCAmelCase_: List[str] = device if device else get_device() if vqgan: UpperCAmelCase_: int = vqgan else: UpperCAmelCase_: Optional[Any] = load_vqgan(self.device, conf_path=SCREAMING_SNAKE_CASE_, ckpt_path=SCREAMING_SNAKE_CASE_ ) self.vqgan.eval() if clip: UpperCAmelCase_: List[str] = clip else: UpperCAmelCase_: Any = CLIPModel.from_pretrained("""openai/clip-vit-base-patch32""" ) self.clip.to(self.device ) UpperCAmelCase_: Optional[int] = ProcessorGradientFlow(device=self.device ) UpperCAmelCase_: Optional[int] = iterations UpperCAmelCase_: List[Any] = lr UpperCAmelCase_: str = log UpperCAmelCase_: Tuple = make_grid UpperCAmelCase_: List[str] = return_val UpperCAmelCase_: Dict = quantize UpperCAmelCase_: int = self.vqgan.decoder.z_shape def __snake_case (self, SCREAMING_SNAKE_CASE_=None, SCREAMING_SNAKE_CASE_=None, SCREAMING_SNAKE_CASE_=5, SCREAMING_SNAKE_CASE_=True ) -> List[Any]: UpperCAmelCase_: Tuple = [] if output_path is None: UpperCAmelCase_: Optional[int] = """./animation.gif""" if input_path is None: UpperCAmelCase_: Tuple = self.save_path UpperCAmelCase_: List[Any] = sorted(glob(input_path + """/*""" ) ) if not len(SCREAMING_SNAKE_CASE_ ): raise ValueError( """No images found in save path, aborting (did you pass save_intermediate=True to the generate""" """ function?)""" ) if len(SCREAMING_SNAKE_CASE_ ) == 1: print("""Only one image found in save path, (did you pass save_intermediate=True to the generate function?)""" ) UpperCAmelCase_: Dict = total_duration / len(SCREAMING_SNAKE_CASE_ ) UpperCAmelCase_: str = [frame_duration] * len(SCREAMING_SNAKE_CASE_ ) if extend_frames: UpperCAmelCase_: List[str] = 1.5 UpperCAmelCase_: List[Any] = 3 for file_name in paths: if file_name.endswith(""".png""" ): images.append(imageio.imread(SCREAMING_SNAKE_CASE_ ) ) imageio.mimsave(SCREAMING_SNAKE_CASE_, SCREAMING_SNAKE_CASE_, duration=SCREAMING_SNAKE_CASE_ ) print(f'gif saved to {output_path}' ) def __snake_case (self, SCREAMING_SNAKE_CASE_=None, SCREAMING_SNAKE_CASE_=None ) -> Optional[int]: if not (path or img): raise ValueError("""Input either path or tensor""" ) if img is not None: raise NotImplementedError UpperCAmelCase_: List[Any] = preprocess(Image.open(SCREAMING_SNAKE_CASE_ ), target_image_size=256 ).to(self.device ) UpperCAmelCase_: Union[str, Any] = preprocess_vqgan(SCREAMING_SNAKE_CASE_ ) UpperCAmelCase_ , *UpperCAmelCase_: str = self.vqgan.encode(SCREAMING_SNAKE_CASE_ ) return z def __snake_case (self, SCREAMING_SNAKE_CASE_ ) -> Union[str, Any]: UpperCAmelCase_: List[Any] = self.latent.detach().requires_grad_() UpperCAmelCase_: Optional[int] = base_latent + transform_vector if self.quantize: UpperCAmelCase_ , *UpperCAmelCase_: Optional[Any] = self.vqgan.quantize(SCREAMING_SNAKE_CASE_ ) else: UpperCAmelCase_: Tuple = trans_latent return self.vqgan.decode(SCREAMING_SNAKE_CASE_ ) def __snake_case (self, SCREAMING_SNAKE_CASE_, SCREAMING_SNAKE_CASE_, SCREAMING_SNAKE_CASE_=None ) -> List[str]: UpperCAmelCase_: Any = self.clip_preprocessor(text=SCREAMING_SNAKE_CASE_, images=SCREAMING_SNAKE_CASE_, return_tensors="""pt""", padding=SCREAMING_SNAKE_CASE_ ) UpperCAmelCase_: str = self.clip(**SCREAMING_SNAKE_CASE_ ) UpperCAmelCase_: Dict = clip_outputs.logits_per_image if weights is not None: UpperCAmelCase_: Any = similarity_logits * weights return similarity_logits.sum() def __snake_case (self, SCREAMING_SNAKE_CASE_, SCREAMING_SNAKE_CASE_, SCREAMING_SNAKE_CASE_ ) -> Any: UpperCAmelCase_: Dict = self._get_clip_similarity(pos_prompts["""prompts"""], SCREAMING_SNAKE_CASE_, weights=(1 / pos_prompts["""weights"""]) ) if neg_prompts: UpperCAmelCase_: Tuple = self._get_clip_similarity(neg_prompts["""prompts"""], SCREAMING_SNAKE_CASE_, weights=neg_prompts["""weights"""] ) else: UpperCAmelCase_: Any = torch.tensor([1], device=self.device ) UpperCAmelCase_: List[str] = -torch.log(SCREAMING_SNAKE_CASE_ ) + torch.log(SCREAMING_SNAKE_CASE_ ) return loss def __snake_case (self, SCREAMING_SNAKE_CASE_, SCREAMING_SNAKE_CASE_, SCREAMING_SNAKE_CASE_ ) -> Optional[Any]: UpperCAmelCase_: Tuple = torch.randn_like(self.latent, requires_grad=SCREAMING_SNAKE_CASE_, device=self.device ) UpperCAmelCase_: str = torch.optim.Adam([vector], lr=self.lr ) for i in range(self.iterations ): optim.zero_grad() UpperCAmelCase_: Optional[int] = self._add_vector(SCREAMING_SNAKE_CASE_ ) UpperCAmelCase_: Any = loop_post_process(SCREAMING_SNAKE_CASE_ ) UpperCAmelCase_: int = self._get_CLIP_loss(SCREAMING_SNAKE_CASE_, SCREAMING_SNAKE_CASE_, SCREAMING_SNAKE_CASE_ ) print("""CLIP loss""", SCREAMING_SNAKE_CASE_ ) if self.log: wandb.log({"""CLIP Loss""": clip_loss} ) clip_loss.backward(retain_graph=SCREAMING_SNAKE_CASE_ ) optim.step() if self.return_val == "image": yield custom_to_pil(transformed_img[0] ) else: yield vector def __snake_case (self, SCREAMING_SNAKE_CASE_, SCREAMING_SNAKE_CASE_, SCREAMING_SNAKE_CASE_ ) -> Dict: wandb.init(reinit=SCREAMING_SNAKE_CASE_, project="""face-editor""" ) wandb.config.update({"""Positive Prompts""": positive_prompts} ) wandb.config.update({"""Negative Prompts""": negative_prompts} ) wandb.config.update({"""lr""": self.lr, """iterations""": self.iterations} ) if image_path: UpperCAmelCase_: str = Image.open(SCREAMING_SNAKE_CASE_ ) UpperCAmelCase_: Optional[Any] = image.resize((256, 256) ) wandb.log("""Original Image""", wandb.Image(SCREAMING_SNAKE_CASE_ ) ) def __snake_case (self, SCREAMING_SNAKE_CASE_ ) -> Optional[int]: if not prompts: return [] UpperCAmelCase_: Tuple = [] UpperCAmelCase_: str = [] if isinstance(SCREAMING_SNAKE_CASE_, SCREAMING_SNAKE_CASE_ ): UpperCAmelCase_: Optional[Any] = [prompt.strip() for prompt in prompts.split("""|""" )] for prompt in prompts: if isinstance(SCREAMING_SNAKE_CASE_, (tuple, list) ): UpperCAmelCase_: str = prompt[0] UpperCAmelCase_: List[str] = float(prompt[1] ) elif ":" in prompt: UpperCAmelCase_ , UpperCAmelCase_: int = prompt.split(""":""" ) UpperCAmelCase_: int = float(SCREAMING_SNAKE_CASE_ ) else: UpperCAmelCase_: str = prompt UpperCAmelCase_: Dict = 1.0 processed_prompts.append(SCREAMING_SNAKE_CASE_ ) weights.append(SCREAMING_SNAKE_CASE_ ) return { "prompts": processed_prompts, "weights": torch.tensor(SCREAMING_SNAKE_CASE_, device=self.device ), } def __snake_case (self, SCREAMING_SNAKE_CASE_, SCREAMING_SNAKE_CASE_=None, SCREAMING_SNAKE_CASE_=None, SCREAMING_SNAKE_CASE_=True, SCREAMING_SNAKE_CASE_=False, SCREAMING_SNAKE_CASE_=True, SCREAMING_SNAKE_CASE_=True, SCREAMING_SNAKE_CASE_=None, ) -> Optional[Any]: if image_path: UpperCAmelCase_: Optional[int] = self._get_latent(SCREAMING_SNAKE_CASE_ ) else: UpperCAmelCase_: str = torch.randn(self.latent_dim, device=self.device ) if self.log: self._init_logging(SCREAMING_SNAKE_CASE_, SCREAMING_SNAKE_CASE_, SCREAMING_SNAKE_CASE_ ) assert pos_prompts, "You must provide at least one positive prompt." UpperCAmelCase_: List[Any] = self.process_prompts(SCREAMING_SNAKE_CASE_ ) UpperCAmelCase_: Dict = self.process_prompts(SCREAMING_SNAKE_CASE_ ) if save_final and save_path is None: UpperCAmelCase_: Optional[int] = os.path.join("""./outputs/""", """_""".join(pos_prompts["""prompts"""] ) ) if not os.path.exists(SCREAMING_SNAKE_CASE_ ): os.makedirs(SCREAMING_SNAKE_CASE_ ) else: UpperCAmelCase_: List[str] = save_path + """_""" + get_timestamp() os.makedirs(SCREAMING_SNAKE_CASE_ ) UpperCAmelCase_: Optional[int] = save_path UpperCAmelCase_: Optional[Any] = self.vqgan.decode(self.latent )[0] if show_intermediate: print("""Original Image""" ) show_pil(custom_to_pil(SCREAMING_SNAKE_CASE_ ) ) UpperCAmelCase_: Tuple = loop_post_process(SCREAMING_SNAKE_CASE_ ) for iter, transformed_img in enumerate(self._optimize_CLIP(SCREAMING_SNAKE_CASE_, SCREAMING_SNAKE_CASE_, SCREAMING_SNAKE_CASE_ ) ): if show_intermediate: show_pil(SCREAMING_SNAKE_CASE_ ) if save_intermediate: transformed_img.save(os.path.join(self.save_path, f'iter_{iter:03d}.png' ) ) if self.log: wandb.log({"""Image""": wandb.Image(SCREAMING_SNAKE_CASE_ )} ) if show_final: show_pil(SCREAMING_SNAKE_CASE_ ) if save_final: transformed_img.save(os.path.join(self.save_path, f'iter_{iter:03d}_final.png' ) )
147
0
import json import os import tempfile import datasets from utils import generate_example_dataset, get_duration _A = 5_00_00 _A = 50_00 _A = os.path.split(__file__) _A = os.path.join(RESULTS_BASEPATH, "results", RESULTS_FILENAME.replace(".py", ".json")) @get_duration def lowercase_ ( A__ , A__ ) -> str: """simple docstring""" for i in range(lowerCAmelCase__ ): snake_case = dataset[i] @get_duration def lowercase_ ( A__ , A__ , A__ ) -> List[str]: """simple docstring""" for i in range(0 , len(lowerCAmelCase__ ) , lowerCAmelCase__ ): snake_case = dataset[i : i + batch_size] @get_duration def lowercase_ ( A__ , A__ , A__ ) -> Dict: """simple docstring""" with dataset.formatted_as(type=lowerCAmelCase__ ): for i in range(lowerCAmelCase__ ): snake_case = dataset[i] @get_duration def lowercase_ ( A__ , A__ , A__ , A__ ) -> int: """simple docstring""" with dataset.formatted_as(type=lowerCAmelCase__ ): for i in range(0 , lowerCAmelCase__ , lowerCAmelCase__ ): snake_case = dataset[i : i + batch_size] def lowercase_ ( ) -> Dict: """simple docstring""" snake_case = {"num examples": SPEED_TEST_N_EXAMPLES} snake_case = [ (read, {"length": SMALL_TEST}), (read, {"length": SPEED_TEST_N_EXAMPLES}), (read_batch, {"length": SPEED_TEST_N_EXAMPLES, "batch_size": 10}), (read_batch, {"length": SPEED_TEST_N_EXAMPLES, "batch_size": 100}), (read_batch, {"length": SPEED_TEST_N_EXAMPLES, "batch_size": 1000}), (read_formatted, {"type": "numpy", "length": SMALL_TEST}), (read_formatted, {"type": "pandas", "length": SMALL_TEST}), (read_formatted, {"type": "torch", "length": SMALL_TEST}), (read_formatted, {"type": "tensorflow", "length": SMALL_TEST}), (read_formatted_batch, {"type": "numpy", "length": SMALL_TEST, "batch_size": 10}), (read_formatted_batch, {"type": "numpy", "length": SMALL_TEST, "batch_size": 1000}), ] snake_case = [ (read, {"length": SMALL_TEST}), (read, {"length": SPEED_TEST_N_EXAMPLES}), (read_batch, {"length": SPEED_TEST_N_EXAMPLES, "batch_size": 10}), (read_batch, {"length": SPEED_TEST_N_EXAMPLES, "batch_size": 100}), (read_batch, {"length": SPEED_TEST_N_EXAMPLES, "batch_size": 1000}), (read_formatted, {"type": "numpy", "length": SMALL_TEST}), (read_formatted_batch, {"type": "numpy", "length": SMALL_TEST, "batch_size": 10}), (read_formatted_batch, {"type": "numpy", "length": SMALL_TEST, "batch_size": 1000}), ] with tempfile.TemporaryDirectory() as tmp_dir: print("generating dataset" ) snake_case = datasets.Features( {"list": datasets.Sequence(datasets.Value("float32" ) ), "numbers": datasets.Value("float32" )} ) snake_case = generate_example_dataset( os.path.join(lowerCAmelCase__ , "dataset.arrow" ) , lowerCAmelCase__ , num_examples=lowerCAmelCase__ , seq_shapes={"list": (100,)} , ) print("first set of iterations" ) for func, kwargs in functions: print(func.__name__ , str(lowerCAmelCase__ ) ) snake_case = func(lowerCAmelCase__ , **lowerCAmelCase__ ) print("shuffling dataset" ) snake_case = dataset.shuffle() print("Second set of iterations (after shuffling" ) for func, kwargs in functions_shuffled: print("shuffled " , func.__name__ , str(lowerCAmelCase__ ) ) snake_case = func( lowerCAmelCase__ , **lowerCAmelCase__ ) with open(lowerCAmelCase__ , "wb" ) as f: f.write(json.dumps(lowerCAmelCase__ ).encode("utf-8" ) ) if __name__ == "__main__": # useful to run the profiler benchmark_iterating()
360
def lowercase_ ( A__ = 1000 ) -> int: """simple docstring""" return sum(2 * a * ((a - 1) // 2) for a in range(3 , n + 1 ) ) if __name__ == "__main__": print(solution())
137
0
import numpy as np def lowerCamelCase__ ( A__ : np.ndarray ): '''simple docstring''' return 1 / (1 + np.exp(-vector )) def lowerCamelCase__ ( A__ : np.ndarray ): '''simple docstring''' return vector * sigmoid(A__ ) if __name__ == "__main__": import doctest doctest.testmod()
12
'''simple docstring''' import datasets UpperCAmelCase = '''\ @InProceedings{conneau2018xnli, author = "Conneau, Alexis and Rinott, Ruty and Lample, Guillaume and Williams, Adina and Bowman, Samuel R. and Schwenk, Holger and Stoyanov, Veselin", title = "XNLI: Evaluating Cross-lingual Sentence Representations", booktitle = "Proceedings of the 2018 Conference on Empirical Methods in Natural Language Processing", year = "2018", publisher = "Association for Computational Linguistics", location = "Brussels, Belgium", } ''' UpperCAmelCase = '''\ XNLI is a subset of a few thousand examples from MNLI which has been translated into a 14 different languages (some low-ish resource). As with MNLI, the goal is to predict textual entailment (does sentence A imply/contradict/neither sentence B) and is a classification task (given two sentences, predict one of three labels). ''' UpperCAmelCase = ''' Computes XNLI score which is just simple accuracy. Args: predictions: Predicted labels. references: Ground truth labels. Returns: \'accuracy\': accuracy Examples: >>> predictions = [0, 1] >>> references = [0, 1] >>> xnli_metric = datasets.load_metric("xnli") >>> results = xnli_metric.compute(predictions=predictions, references=references) >>> print(results) {\'accuracy\': 1.0} ''' def __UpperCamelCase ( lowercase__ : Optional[Any], lowercase__ : List[str] ): '''simple docstring''' return (preds == labels).mean() @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class lowerCAmelCase ( datasets.Metric ): def snake_case ( self : int ): """simple docstring""" return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { 'predictions': datasets.Value('int64' if self.config_name != 'sts-b' else 'float32' ), 'references': datasets.Value('int64' if self.config_name != 'sts-b' else 'float32' ), } ) , codebase_urls=[] , reference_urls=[] , format='numpy' , ) def snake_case ( self : List[str] , __lowercase : Dict , __lowercase : Optional[Any] ): """simple docstring""" return {"accuracy": simple_accuracy(__lowercase , __lowercase )}
141
0
import os from pathlib import Path def lowerCamelCase_ ( _a : int , _a : int , _a : List[str] ): UpperCAmelCase_ : Dict = { """en""": """Machine learning is great, isn\'t it?""", """ru""": """Машинное обучение - это здорово, не так ли?""", """de""": """Maschinelles Lernen ist großartig, oder?""", } # BLUE scores as follows: # "pair": [fairseq, transformers] UpperCAmelCase_ : Dict = { """ru-en""": ["""[41.3](http://matrix.statmt.org/matrix/output/1907?run_id=6937)""", """39.20"""], """en-ru""": ["""[36.4](http://matrix.statmt.org/matrix/output/1914?run_id=6724)""", """33.47"""], """en-de""": ["""[43.1](http://matrix.statmt.org/matrix/output/1909?run_id=6862)""", """42.83"""], """de-en""": ["""[42.3](http://matrix.statmt.org/matrix/output/1902?run_id=6750)""", """41.35"""], } UpperCAmelCase_ : Optional[Any] = F'''{src_lang}-{tgt_lang}''' UpperCAmelCase_ : Dict = F''' --- language: - {src_lang} - {tgt_lang} thumbnail: tags: - translation - wmt19 - facebook license: apache-2.0 datasets: - wmt19 metrics: - bleu --- # FSMT ## Model description This is a ported version of [fairseq wmt19 transformer](https://github.com/pytorch/fairseq/blob/master/examples/wmt19/README.md) for {src_lang}-{tgt_lang}. For more details, please see, [Facebook FAIR\'s WMT19 News Translation Task Submission](https://arxiv.org/abs/1907.06616). The abbreviation FSMT stands for FairSeqMachineTranslation All four models are available: * [wmt19-en-ru](https://huggingface.co/facebook/wmt19-en-ru) * [wmt19-ru-en](https://huggingface.co/facebook/wmt19-ru-en) * [wmt19-en-de](https://huggingface.co/facebook/wmt19-en-de) * [wmt19-de-en](https://huggingface.co/facebook/wmt19-de-en) ## Intended uses & limitations #### How to use ```python from transformers import FSMTForConditionalGeneration, FSMTTokenizer mname = \"facebook/wmt19-{src_lang}-{tgt_lang}\" tokenizer = FSMTTokenizer.from_pretrained(mname) model = FSMTForConditionalGeneration.from_pretrained(mname) input = \"{texts[src_lang]}\" input_ids = tokenizer.encode(input, return_tensors=\"pt\") outputs = model.generate(input_ids) decoded = tokenizer.decode(outputs[0], skip_special_tokens=True) print(decoded) # {texts[tgt_lang]} ``` #### Limitations and bias - The original (and this ported model) doesn\'t seem to handle well inputs with repeated sub-phrases, [content gets truncated](https://discuss.huggingface.co/t/issues-with-translating-inputs-containing-repeated-phrases/981) ## Training data Pretrained weights were left identical to the original model released by fairseq. For more details, please, see the [paper](https://arxiv.org/abs/1907.06616). ## Eval results pair | fairseq | transformers -------|---------|---------- {pair} | {scores[pair][0]} | {scores[pair][1]} The score is slightly below the score reported by `fairseq`, since `transformers`` currently doesn\'t support: - model ensemble, therefore the best performing checkpoint was ported (``model4.pt``). - re-ranking The score was calculated using this code: ```bash git clone https://github.com/huggingface/transformers cd transformers export PAIR={pair} export DATA_DIR=data/$PAIR export SAVE_DIR=data/$PAIR export BS=8 export NUM_BEAMS=15 mkdir -p $DATA_DIR sacrebleu -t wmt19 -l $PAIR --echo src > $DATA_DIR/val.source sacrebleu -t wmt19 -l $PAIR --echo ref > $DATA_DIR/val.target echo $PAIR PYTHONPATH=\"src:examples/seq2seq\" python examples/seq2seq/run_eval.py facebook/wmt19-$PAIR $DATA_DIR/val.source $SAVE_DIR/test_translations.txt --reference_path $DATA_DIR/val.target --score_path $SAVE_DIR/test_bleu.json --bs $BS --task translation --num_beams $NUM_BEAMS ``` note: fairseq reports using a beam of 50, so you should get a slightly higher score if re-run with `--num_beams 50`. ## Data Sources - [training, etc.](http://www.statmt.org/wmt19/) - [test set](http://matrix.statmt.org/test_sets/newstest2019.tgz?1556572561) ### BibTeX entry and citation info ```bibtex @inproceedings{{..., year={{2020}}, title={{Facebook FAIR\'s WMT19 News Translation Task Submission}}, author={{Ng, Nathan and Yee, Kyra and Baevski, Alexei and Ott, Myle and Auli, Michael and Edunov, Sergey}}, booktitle={{Proc. of WMT}}, }} ``` ## TODO - port model ensemble (fairseq uses 4 model checkpoints) ''' os.makedirs(lowercase_ , exist_ok=lowercase_ ) UpperCAmelCase_ : Optional[int] = os.path.join(lowercase_ , """README.md""" ) print(F'''Generating {path}''' ) with open(lowercase_ , """w""" , encoding="""utf-8""" ) as f: f.write(lowercase_ ) # make sure we are under the root of the project UpperCamelCase_ = Path(__file__).resolve().parent.parent.parent UpperCamelCase_ = repo_dir / '''model_cards''' for model_name in ["wmt19-ru-en", "wmt19-en-ru", "wmt19-en-de", "wmt19-de-en"]: UpperCamelCase_ = model_name.split('''-''') UpperCamelCase_ = model_cards_dir / '''facebook''' / model_name write_model_card(model_card_dir, src_lang=src_lang, tgt_lang=tgt_lang)
356
import random from typing import Any def lowerCamelCase_ ( _a : list ): '''simple docstring''' for _ in range(len(_a ) ): UpperCAmelCase_ : List[str] = random.randint(0 , len(_a ) - 1 ) UpperCAmelCase_ : Any = random.randint(0 , len(_a ) - 1 ) UpperCAmelCase_ , UpperCAmelCase_ : Dict = data[b], data[a] return data if __name__ == "__main__": UpperCamelCase_ = [0, 1, 2, 3, 4, 5, 6, 7] UpperCamelCase_ = ['''python''', '''says''', '''hello''', '''!'''] print('''Fisher-Yates Shuffle:''') print('''List''', integers, strings) print('''FY Shuffle''', fisher_yates_shuffle(integers), fisher_yates_shuffle(strings))
59
0
"""simple docstring""" import string # frequency taken from https://en.wikipedia.org/wiki/Letter_frequency _UpperCamelCase : List[Any] = { '''E''': 12.70, '''T''': 9.0_6, '''A''': 8.1_7, '''O''': 7.5_1, '''I''': 6.9_7, '''N''': 6.7_5, '''S''': 6.3_3, '''H''': 6.0_9, '''R''': 5.9_9, '''D''': 4.2_5, '''L''': 4.0_3, '''C''': 2.7_8, '''U''': 2.7_6, '''M''': 2.4_1, '''W''': 2.3_6, '''F''': 2.2_3, '''G''': 2.0_2, '''Y''': 1.9_7, '''P''': 1.9_3, '''B''': 1.2_9, '''V''': 0.9_8, '''K''': 0.7_7, '''J''': 0.1_5, '''X''': 0.1_5, '''Q''': 0.1_0, '''Z''': 0.0_7, } _UpperCamelCase : Optional[Any] = '''ETAOINSHRDLCUMWFGYPBVKJXQZ''' _UpperCamelCase : Dict = '''ABCDEFGHIJKLMNOPQRSTUVWXYZ''' def a_ ( _lowerCAmelCase : str ): '''simple docstring''' lowercase__ : int = {letter: 0 for letter in string.ascii_uppercase} for letter in message.upper(): if letter in LETTERS: letter_count[letter] += 1 return letter_count def a_ ( _lowerCAmelCase : tuple ): '''simple docstring''' return x[0] def a_ ( _lowerCAmelCase : str ): '''simple docstring''' lowercase__ : Dict = get_letter_count(_lowerCamelCase ) lowercase__ : dict[int, list[str]] = { freq: [] for letter, freq in letter_to_freq.items() } for letter in LETTERS: freq_to_letter[letter_to_freq[letter]].append(_lowerCamelCase ) lowercase__ : dict[int, str] = {} for freq in freq_to_letter: freq_to_letter[freq].sort(key=ETAOIN.find , reverse=_lowerCamelCase ) lowercase__ : Tuple = """""".join(freq_to_letter[freq] ) lowercase__ : str = list(freq_to_letter_str.items() ) freq_pairs.sort(key=_lowerCamelCase , reverse=_lowerCamelCase ) lowercase__ : list[str] = [freq_pair[1] for freq_pair in freq_pairs] return "".join(_lowerCamelCase ) def a_ ( _lowerCAmelCase : str ): '''simple docstring''' lowercase__ : List[Any] = get_frequency_order(_lowerCamelCase ) lowercase__ : Union[str, Any] = 0 for common_letter in ETAOIN[:6]: if common_letter in freq_order[:6]: match_score += 1 for uncommon_letter in ETAOIN[-6:]: if uncommon_letter in freq_order[-6:]: match_score += 1 return match_score if __name__ == "__main__": import doctest doctest.testmod()
77
'''simple docstring''' def lowerCAmelCase_ ( _lowerCamelCase: int ): __SCREAMING_SNAKE_CASE : str = int(_lowerCamelCase ) if n_element < 1: __SCREAMING_SNAKE_CASE : List[str] = ValueError("""a should be a positive number""" ) raise my_error __SCREAMING_SNAKE_CASE : List[Any] = [1] __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE : Optional[int] = (0, 0, 0) __SCREAMING_SNAKE_CASE : List[str] = 1 while index < n_element: while hamming_list[i] * 2 <= hamming_list[-1]: i += 1 while hamming_list[j] * 3 <= hamming_list[-1]: j += 1 while hamming_list[k] * 5 <= hamming_list[-1]: k += 1 hamming_list.append( min(hamming_list[i] * 2 , hamming_list[j] * 3 , hamming_list[k] * 5 ) ) index += 1 return hamming_list if __name__ == "__main__": UpperCamelCase__ : Optional[Any] = input('''Enter the last number (nth term) of the Hamming Number Series: ''') print('''Formula of Hamming Number Series => 2^i * 3^j * 5^k''') UpperCamelCase__ : List[str] = hamming(int(n)) print('''-----------------------------------------------------''') print(f"The list with nth numbers is: {hamming_numbers}") print('''-----------------------------------------------------''')
112
0
import gc import random import unittest import numpy as np import torch from PIL import Image from transformers import CLIPTextConfig, CLIPTextModel, CLIPTokenizer from diffusers import AutoencoderKL, PNDMScheduler, StableDiffusionInpaintPipeline, UNetaDConditionModel from diffusers.utils import floats_tensor, load_image, load_numpy, torch_device from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu, slow from ..pipeline_params import TEXT_GUIDED_IMAGE_INPAINTING_BATCH_PARAMS, TEXT_GUIDED_IMAGE_INPAINTING_PARAMS from ..test_pipelines_common import PipelineKarrasSchedulerTesterMixin, PipelineLatentTesterMixin, PipelineTesterMixin enable_full_determinism() class a ( _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , unittest.TestCase ): """simple docstring""" lowerCamelCase :int = StableDiffusionInpaintPipeline lowerCamelCase :Union[str, Any] = TEXT_GUIDED_IMAGE_INPAINTING_PARAMS lowerCamelCase :List[Any] = TEXT_GUIDED_IMAGE_INPAINTING_BATCH_PARAMS lowerCamelCase :int = frozenset( [] ) # TO-DO: update image_params once pipeline is refactored with VaeImageProcessor.preprocess lowerCamelCase :Tuple = frozenset([] ) def UpperCAmelCase ( self ) -> Dict: torch.manual_seed(0 ) _A = UNetaDConditionModel( block_out_channels=(32, 64) , layers_per_block=2 , sample_size=32 , in_channels=9 , out_channels=4 , down_block_types=("""DownBlock2D""", """CrossAttnDownBlock2D""") , up_block_types=("""CrossAttnUpBlock2D""", """UpBlock2D""") , cross_attention_dim=32 , attention_head_dim=(2, 4) , use_linear_projection=lowerCAmelCase_ , ) _A = PNDMScheduler(skip_prk_steps=lowerCAmelCase_ ) torch.manual_seed(0 ) _A = AutoencoderKL( block_out_channels=[32, 64] , in_channels=3 , out_channels=3 , down_block_types=["""DownEncoderBlock2D""", """DownEncoderBlock2D"""] , up_block_types=["""UpDecoderBlock2D""", """UpDecoderBlock2D"""] , latent_channels=4 , sample_size=1_28 , ) torch.manual_seed(0 ) _A = CLIPTextConfig( bos_token_id=0 , eos_token_id=2 , hidden_size=32 , intermediate_size=37 , layer_norm_eps=1E-05 , num_attention_heads=4 , num_hidden_layers=5 , pad_token_id=1 , vocab_size=10_00 , hidden_act="""gelu""" , projection_dim=5_12 , ) _A = CLIPTextModel(lowerCAmelCase_ ) _A = CLIPTokenizer.from_pretrained("""hf-internal-testing/tiny-random-clip""" ) _A = { """unet""": unet, """scheduler""": scheduler, """vae""": vae, """text_encoder""": text_encoder, """tokenizer""": tokenizer, """safety_checker""": None, """feature_extractor""": None, } return components def UpperCAmelCase ( self , lowerCAmelCase_ , lowerCAmelCase_=0 ) -> List[Any]: # TODO: use tensor inputs instead of PIL, this is here just to leave the old expected_slices untouched _A = floats_tensor((1, 3, 32, 32) , rng=random.Random(lowerCAmelCase_ ) ).to(lowerCAmelCase_ ) _A = image.cpu().permute(0 , 2 , 3 , 1 )[0] _A = Image.fromarray(np.uinta(lowerCAmelCase_ ) ).convert("""RGB""" ).resize((64, 64) ) _A = Image.fromarray(np.uinta(image + 4 ) ).convert("""RGB""" ).resize((64, 64) ) if str(lowerCAmelCase_ ).startswith("""mps""" ): _A = torch.manual_seed(lowerCAmelCase_ ) else: _A = torch.Generator(device=lowerCAmelCase_ ).manual_seed(lowerCAmelCase_ ) _A = { """prompt""": """A painting of a squirrel eating a burger""", """image""": init_image, """mask_image""": mask_image, """generator""": generator, """num_inference_steps""": 2, """guidance_scale""": 6.0, """output_type""": """numpy""", } return inputs def UpperCAmelCase ( self ) -> Optional[int]: _A = """cpu""" # ensure determinism for the device-dependent torch.Generator _A = self.get_dummy_components() _A = StableDiffusionInpaintPipeline(**lowerCAmelCase_ ) _A = sd_pipe.to(lowerCAmelCase_ ) sd_pipe.set_progress_bar_config(disable=lowerCAmelCase_ ) _A = self.get_dummy_inputs(lowerCAmelCase_ ) _A = sd_pipe(**lowerCAmelCase_ ).images _A = image[0, -3:, -3:, -1] assert image.shape == (1, 64, 64, 3) _A = np.array([0.4727, 0.5735, 0.3941, 0.5446, 0.5926, 0.4394, 0.5062, 0.4654, 0.4476] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-2 def UpperCAmelCase ( self ) -> Union[str, Any]: super().test_inference_batch_single_identical(expected_max_diff=3E-3 ) @slow @require_torch_gpu class a ( unittest.TestCase ): """simple docstring""" def UpperCAmelCase ( self ) -> Any: # clean up the VRAM after each test super().tearDown() gc.collect() torch.cuda.empty_cache() def UpperCAmelCase ( self ) -> Dict: _A = load_image( """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main""" """/sd2-inpaint/init_image.png""" ) _A = load_image( """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd2-inpaint/mask.png""" ) _A = load_numpy( """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd2-inpaint""" """/yellow_cat_sitting_on_a_park_bench.npy""" ) _A = """stabilityai/stable-diffusion-2-inpainting""" _A = StableDiffusionInpaintPipeline.from_pretrained(lowerCAmelCase_ , safety_checker=lowerCAmelCase_ ) pipe.to(lowerCAmelCase_ ) pipe.set_progress_bar_config(disable=lowerCAmelCase_ ) pipe.enable_attention_slicing() _A = """Face of a yellow cat, high resolution, sitting on a park bench""" _A = torch.manual_seed(0 ) _A = pipe( prompt=lowerCAmelCase_ , image=lowerCAmelCase_ , mask_image=lowerCAmelCase_ , generator=lowerCAmelCase_ , output_type="""np""" , ) _A = output.images[0] assert image.shape == (5_12, 5_12, 3) assert np.abs(expected_image - image ).max() < 9E-3 def UpperCAmelCase ( self ) -> Union[str, Any]: _A = load_image( """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main""" """/sd2-inpaint/init_image.png""" ) _A = load_image( """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd2-inpaint/mask.png""" ) _A = load_numpy( """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd2-inpaint""" """/yellow_cat_sitting_on_a_park_bench_fp16.npy""" ) _A = """stabilityai/stable-diffusion-2-inpainting""" _A = StableDiffusionInpaintPipeline.from_pretrained( lowerCAmelCase_ , torch_dtype=torch.floataa , safety_checker=lowerCAmelCase_ , ) pipe.to(lowerCAmelCase_ ) pipe.set_progress_bar_config(disable=lowerCAmelCase_ ) pipe.enable_attention_slicing() _A = """Face of a yellow cat, high resolution, sitting on a park bench""" _A = torch.manual_seed(0 ) _A = pipe( prompt=lowerCAmelCase_ , image=lowerCAmelCase_ , mask_image=lowerCAmelCase_ , generator=lowerCAmelCase_ , output_type="""np""" , ) _A = output.images[0] assert image.shape == (5_12, 5_12, 3) assert np.abs(expected_image - image ).max() < 5E-1 def UpperCAmelCase ( self ) -> str: torch.cuda.empty_cache() torch.cuda.reset_max_memory_allocated() torch.cuda.reset_peak_memory_stats() _A = load_image( """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main""" """/sd2-inpaint/init_image.png""" ) _A = load_image( """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd2-inpaint/mask.png""" ) _A = """stabilityai/stable-diffusion-2-inpainting""" _A = PNDMScheduler.from_pretrained(lowerCAmelCase_ , subfolder="""scheduler""" ) _A = StableDiffusionInpaintPipeline.from_pretrained( lowerCAmelCase_ , safety_checker=lowerCAmelCase_ , scheduler=lowerCAmelCase_ , torch_dtype=torch.floataa , ) pipe.to(lowerCAmelCase_ ) pipe.set_progress_bar_config(disable=lowerCAmelCase_ ) pipe.enable_attention_slicing(1 ) pipe.enable_sequential_cpu_offload() _A = """Face of a yellow cat, high resolution, sitting on a park bench""" _A = torch.manual_seed(0 ) _A = pipe( prompt=lowerCAmelCase_ , image=lowerCAmelCase_ , mask_image=lowerCAmelCase_ , generator=lowerCAmelCase_ , num_inference_steps=2 , output_type="""np""" , ) _A = torch.cuda.max_memory_allocated() # make sure that less than 2.65 GB is allocated assert mem_bytes < 2.65 * 10**9
365
import colorsys from PIL import Image # type: ignore def snake_case ( snake_case__ :float , snake_case__ :float , snake_case__ :int) -> float: _A = x _A = y for step in range(snake_case__): # noqa: B007 _A = a * a - b * b + x _A = 2 * a * b + y _A = a_new # divergence happens for all complex number with an absolute value # greater than 4 if a * a + b * b > 4: break return step / (max_step - 1) def snake_case ( snake_case__ :float) -> tuple: if distance == 1: return (0, 0, 0) else: return (255, 255, 255) def snake_case ( snake_case__ :float) -> tuple: if distance == 1: return (0, 0, 0) else: return tuple(round(i * 255) for i in colorsys.hsv_to_rgb(snake_case__ , 1 , 1)) def snake_case ( snake_case__ :int = 800 , snake_case__ :int = 600 , snake_case__ :float = -0.6 , snake_case__ :float = 0 , snake_case__ :float = 3.2 , snake_case__ :int = 50 , snake_case__ :bool = True , ) -> Image.Image: _A = Image.new("""RGB""" , (image_width, image_height)) _A = img.load() # loop through the image-coordinates for image_x in range(snake_case__): for image_y in range(snake_case__): # determine the figure-coordinates based on the image-coordinates _A = figure_width / image_width * image_height _A = figure_center_x + (image_x / image_width - 0.5) * figure_width _A = figure_center_y + (image_y / image_height - 0.5) * figure_height _A = get_distance(snake_case__ , snake_case__ , snake_case__) # color the corresponding pixel based on the selected coloring-function if use_distance_color_coding: _A = get_color_coded_rgb(snake_case__) else: _A = get_black_and_white_rgb(snake_case__) return img if __name__ == "__main__": import doctest doctest.testmod() # colored version, full figure _SCREAMING_SNAKE_CASE = get_image() # uncomment for colored version, different section, zoomed in # img = get_image(figure_center_x = -0.6, figure_center_y = -0.4, # figure_width = 0.8) # uncomment for black and white version, full figure # img = get_image(use_distance_color_coding = False) # uncomment to save the image # img.save("mandelbrot.png") img.show()
81
0
from ...configuration_utils import PretrainedConfig from ...utils import logging a =logging.get_logger(__name__) a ={ """google/fnet-base""": """https://huggingface.co/google/fnet-base/resolve/main/config.json""", """google/fnet-large""": """https://huggingface.co/google/fnet-large/resolve/main/config.json""" # See all FNet models at https://huggingface.co/models?filter=fnet } class A_ ( __A ): _UpperCAmelCase : Dict = "fnet" def __init__( self : List[Any] ,SCREAMING_SNAKE_CASE__ : Tuple=3_2_0_0_0 ,SCREAMING_SNAKE_CASE__ : Optional[Any]=7_6_8 ,SCREAMING_SNAKE_CASE__ : Union[str, Any]=1_2 ,SCREAMING_SNAKE_CASE__ : str=3_0_7_2 ,SCREAMING_SNAKE_CASE__ : Optional[int]="gelu_new" ,SCREAMING_SNAKE_CASE__ : Optional[int]=0.1 ,SCREAMING_SNAKE_CASE__ : Union[str, Any]=5_1_2 ,SCREAMING_SNAKE_CASE__ : Tuple=4 ,SCREAMING_SNAKE_CASE__ : Tuple=0.02 ,SCREAMING_SNAKE_CASE__ : Tuple=1E-12 ,SCREAMING_SNAKE_CASE__ : List[str]=False ,SCREAMING_SNAKE_CASE__ : Union[str, Any]=5_1_2 ,SCREAMING_SNAKE_CASE__ : Any=3 ,SCREAMING_SNAKE_CASE__ : str=1 ,SCREAMING_SNAKE_CASE__ : List[Any]=2 ,**SCREAMING_SNAKE_CASE__ : List[str] ,): super().__init__(pad_token_id=_UpperCamelCase ,bos_token_id=_UpperCamelCase ,eos_token_id=_UpperCamelCase ,**_UpperCamelCase) __lowerCamelCase : Union[str, Any] = vocab_size __lowerCamelCase : str = max_position_embeddings __lowerCamelCase : List[str] = hidden_size __lowerCamelCase : Optional[int] = num_hidden_layers __lowerCamelCase : Any = intermediate_size __lowerCamelCase : str = hidden_act __lowerCamelCase : str = hidden_dropout_prob __lowerCamelCase : str = initializer_range __lowerCamelCase : List[str] = type_vocab_size __lowerCamelCase : List[Any] = layer_norm_eps __lowerCamelCase : Tuple = use_tpu_fourier_optimizations __lowerCamelCase : Any = tpu_short_seq_length
73
# Copyright 2023 The HuggingFace Inc. team. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. from ..models.auto import AutoModelForSeqaSeqLM, AutoTokenizer from .base import PipelineTool class snake_case_ ( __A ): '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[Any] = "philschmid/bart-large-cnn-samsum" SCREAMING_SNAKE_CASE : Tuple = ( "This is a tool that summarizes an English text. It takes an input `text` containing the text to summarize, " "and returns a summary of the text." ) SCREAMING_SNAKE_CASE : str = "summarizer" SCREAMING_SNAKE_CASE : str = AutoTokenizer SCREAMING_SNAKE_CASE : str = AutoModelForSeqaSeqLM SCREAMING_SNAKE_CASE : Optional[int] = ["text"] SCREAMING_SNAKE_CASE : Optional[int] = ["text"] def snake_case__( self : str , _UpperCamelCase : int ) ->Optional[int]: return self.pre_processor(_UpperCamelCase , return_tensors='''pt''' , truncation=_UpperCamelCase ) def snake_case__( self : Tuple , _UpperCamelCase : Optional[int] ) ->Tuple: return self.model.generate(**_UpperCamelCase )[0] def snake_case__( self : Optional[Any] , _UpperCamelCase : Optional[int] ) ->Any: return self.pre_processor.decode(_UpperCamelCase , skip_special_tokens=_UpperCamelCase , clean_up_tokenization_spaces=_UpperCamelCase )
8
0
from ..models.whisper import WhisperForConditionalGeneration, WhisperProcessor from .base import PipelineTool class SCREAMING_SNAKE_CASE__ ( A_ ): __SCREAMING_SNAKE_CASE = '''openai/whisper-base''' __SCREAMING_SNAKE_CASE = ( '''This is a tool that transcribes an audio into text. It takes an input named `audio` and returns the ''' '''transcribed text.''' ) __SCREAMING_SNAKE_CASE = '''transcriber''' __SCREAMING_SNAKE_CASE = WhisperProcessor __SCREAMING_SNAKE_CASE = WhisperForConditionalGeneration __SCREAMING_SNAKE_CASE = ['''audio'''] __SCREAMING_SNAKE_CASE = ['''text'''] def UpperCamelCase ( self,__lowerCamelCase ): return self.pre_processor(_lowerCamelCase,return_tensors='''pt''' ).input_features def UpperCamelCase ( self,__lowerCamelCase ): return self.model.generate(inputs=_lowerCamelCase ) def UpperCamelCase ( self,__lowerCamelCase ): return self.pre_processor.batch_decode(_lowerCamelCase,skip_special_tokens=_lowerCamelCase )[0]
367
def UpperCamelCase__( UpperCamelCase__ : str )->str: A__ = '''''' for ch in key: if ch == " " or ch not in key_no_dups and ch.isalpha(): key_no_dups += ch return key_no_dups def UpperCamelCase__( UpperCamelCase__ : str )->dict[str, str]: A__ = [chr(i + 65 ) for i in range(26 )] # Remove duplicate characters from key A__ = remove_duplicates(key.upper() ) A__ = len(UpperCamelCase__ ) # First fill cipher with key characters A__ = {alphabet[i]: char for i, char in enumerate(UpperCamelCase__ )} # Then map remaining characters in alphabet to # the alphabet from the beginning for i in range(len(UpperCamelCase__ ) , 26 ): A__ = alphabet[i - offset] # Ensure we are not mapping letters to letters previously mapped while char in key: offset -= 1 A__ = alphabet[i - offset] A__ = char return cipher_alphabet def UpperCamelCase__( UpperCamelCase__ : str , UpperCamelCase__ : dict[str, str] )->str: return "".join(cipher_map.get(UpperCamelCase__ , UpperCamelCase__ ) for ch in message.upper() ) def UpperCamelCase__( UpperCamelCase__ : str , UpperCamelCase__ : dict[str, str] )->str: A__ = {v: k for k, v in cipher_map.items()} return "".join(rev_cipher_map.get(UpperCamelCase__ , UpperCamelCase__ ) for ch in message.upper() ) def UpperCamelCase__( )->None: A__ = input('''Enter message to encode or decode: ''' ).strip() A__ = input('''Enter keyword: ''' ).strip() A__ = input('''Encipher or decipher? E/D:''' ).strip()[0].lower() try: A__ = {'''e''': encipher, '''d''': decipher}[option] except KeyError: raise KeyError('''invalid input option''' ) A__ = create_cipher_map(UpperCamelCase__ ) print(func(UpperCamelCase__ , UpperCamelCase__ ) ) if __name__ == "__main__": import doctest doctest.testmod() main()
39
0
def __lowercase ( lowerCamelCase : float , lowerCamelCase : float ): return price * (1 + tax_rate) if __name__ == "__main__": print(F"""{price_plus_tax(100, 0.25) = }""") print(F"""{price_plus_tax(125.50, 0.05) = }""")
175
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_tf_available, is_torch_available, ) a_ = {'configuration_encoder_decoder': ['EncoderDecoderConfig']} try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: a_ = ['EncoderDecoderModel'] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: a_ = ['TFEncoderDecoderModel'] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: a_ = ['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_ = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
175
1
"""simple docstring""" from dataclasses import dataclass from typing import Tuple import numpy as np import torch @dataclass class UpperCamelCase_ : _A : torch.Tensor # [batch_size x 3] _A : torch.Tensor # [batch_size x 3] _A : torch.Tensor # [batch_size x 3] _A : torch.Tensor # [batch_size x 3] _A : int _A : int _A : float _A : float _A : Tuple[int] def UpperCamelCase_ ( self ) -> str: """simple docstring""" assert self.x.shape[0] == self.y.shape[0] == self.z.shape[0] == self.origin.shape[0] assert self.x.shape[1] == self.y.shape[1] == self.z.shape[1] == self.origin.shape[1] == 3 assert len(self.x.shape ) == len(self.y.shape ) == len(self.z.shape ) == len(self.origin.shape ) == 2 def UpperCamelCase_ ( self ) -> Optional[Any]: """simple docstring""" return torch.from_numpy(np.array([self.width, self.height] , dtype=np.floataa ) ) def UpperCamelCase_ ( self ) -> Tuple: """simple docstring""" return torch.from_numpy(np.array([self.x_fov, self.y_fov] , dtype=np.floataa ) ) def UpperCamelCase_ ( self ) -> Optional[Any]: """simple docstring""" UpperCAmelCase = torch.arange(self.height * self.width ) UpperCAmelCase = torch.stack( [ pixel_indices % self.width, torch.div(snake_case_ , self.width , rounding_mode="""trunc""" ), ] , axis=1 , ) return coords @property def UpperCamelCase_ ( self ) -> Optional[Any]: """simple docstring""" UpperCAmelCase = self.shape UpperCAmelCase = int(np.prod(snake_case_ ) ) UpperCAmelCase = self.get_image_coords() UpperCAmelCase = torch.broadcast_to(coords.unsqueeze(0 ) , [batch_size * inner_batch_size, *coords.shape] ) UpperCAmelCase = self.get_camera_rays(snake_case_ ) UpperCAmelCase = rays.view(snake_case_ , inner_batch_size * self.height * self.width , 2 , 3 ) return rays def UpperCamelCase_ ( self , snake_case__ ) -> Tuple: """simple docstring""" UpperCAmelCase = coords.shape assert n_coords == 2 assert batch_size == self.origin.shape[0] UpperCAmelCase = coords.view(snake_case_ , -1 , 2 ) UpperCAmelCase = self.resolution() UpperCAmelCase = self.fov() UpperCAmelCase = (flat.float() / (res - 1)) * 2 - 1 UpperCAmelCase = fracs * torch.tan(fov / 2 ) UpperCAmelCase = fracs.view(snake_case_ , -1 , 2 ) UpperCAmelCase = ( self.z.view(snake_case_ , 1 , 3 ) + self.x.view(snake_case_ , 1 , 3 ) * fracs[:, :, :1] + self.y.view(snake_case_ , 1 , 3 ) * fracs[:, :, 1:] ) UpperCAmelCase = directions / directions.norm(dim=-1 , keepdim=snake_case_ ) UpperCAmelCase = torch.stack( [ torch.broadcast_to(self.origin.view(snake_case_ , 1 , 3 ) , [batch_size, directions.shape[1], 3] ), directions, ] , dim=2 , ) return rays.view(snake_case_ , *snake_case_ , 2 , 3 ) def UpperCamelCase_ ( self , snake_case__ , snake_case__ ) -> Dict: """simple docstring""" assert width * self.height == height * self.width, "The aspect ratio should not change." return DifferentiableProjectiveCamera( origin=self.origin , x=self.x , y=self.y , z=self.z , width=snake_case_ , height=snake_case_ , x_fov=self.x_fov , y_fov=self.y_fov , ) def _lowerCAmelCase ( lowerCAmelCase ): '''simple docstring''' UpperCAmelCase = [] UpperCAmelCase = [] UpperCAmelCase = [] UpperCAmelCase = [] for theta in np.linspace(0 , 2 * np.pi , num=20 ): UpperCAmelCase = np.array([np.sin(lowerCAmelCase__ ), np.cos(lowerCAmelCase__ ), -0.5] ) z /= np.sqrt(np.sum(z**2 ) ) UpperCAmelCase = -z * 4 UpperCAmelCase = np.array([np.cos(lowerCAmelCase__ ), -np.sin(lowerCAmelCase__ ), 0.0] ) UpperCAmelCase = np.cross(lowerCAmelCase__ , lowerCAmelCase__ ) origins.append(lowerCAmelCase__ ) xs.append(lowerCAmelCase__ ) ys.append(lowerCAmelCase__ ) zs.append(lowerCAmelCase__ ) return DifferentiableProjectiveCamera( origin=torch.from_numpy(np.stack(lowerCAmelCase__ , axis=0 ) ).float() , x=torch.from_numpy(np.stack(lowerCAmelCase__ , axis=0 ) ).float() , y=torch.from_numpy(np.stack(lowerCAmelCase__ , axis=0 ) ).float() , z=torch.from_numpy(np.stack(lowerCAmelCase__ , axis=0 ) ).float() , width=lowerCAmelCase__ , height=lowerCAmelCase__ , x_fov=0.7 , y_fov=0.7 , shape=(1, len(lowerCAmelCase__ )) , )
352
"""simple docstring""" import gc import random import unittest import numpy as np import torch from PIL import Image from transformers import CLIPTextConfig, CLIPTextModel, CLIPTokenizer from diffusers import AutoencoderKL, DDIMScheduler, DDPMScheduler, StableDiffusionUpscalePipeline, UNetaDConditionModel from diffusers.utils import floats_tensor, load_image, load_numpy, slow, torch_device from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu enable_full_determinism() class UpperCamelCase_ ( unittest.TestCase ): def UpperCamelCase_ ( self ) -> Optional[int]: """simple docstring""" super().tearDown() gc.collect() torch.cuda.empty_cache() @property def UpperCamelCase_ ( self ) -> Any: """simple docstring""" UpperCAmelCase = 1 UpperCAmelCase = 3 UpperCAmelCase = (32, 32) UpperCAmelCase = floats_tensor((batch_size, num_channels) + sizes , rng=random.Random(0 ) ).to(snake_case__ ) return image @property def UpperCamelCase_ ( self ) -> List[Any]: """simple docstring""" torch.manual_seed(0 ) UpperCAmelCase = UNetaDConditionModel( block_out_channels=(32, 32, 64) , layers_per_block=2 , sample_size=32 , in_channels=7 , out_channels=4 , down_block_types=("""DownBlock2D""", """CrossAttnDownBlock2D""", """CrossAttnDownBlock2D""") , up_block_types=("""CrossAttnUpBlock2D""", """CrossAttnUpBlock2D""", """UpBlock2D""") , cross_attention_dim=32 , attention_head_dim=8 , use_linear_projection=snake_case__ , only_cross_attention=(True, True, False) , num_class_embeds=1_00 , ) return model @property def UpperCamelCase_ ( self ) -> int: """simple docstring""" torch.manual_seed(0 ) UpperCAmelCase = AutoencoderKL( block_out_channels=[32, 32, 64] , in_channels=3 , out_channels=3 , down_block_types=["""DownEncoderBlock2D""", """DownEncoderBlock2D""", """DownEncoderBlock2D"""] , up_block_types=["""UpDecoderBlock2D""", """UpDecoderBlock2D""", """UpDecoderBlock2D"""] , latent_channels=4 , ) return model @property def UpperCamelCase_ ( self ) -> Dict: """simple docstring""" torch.manual_seed(0 ) UpperCAmelCase = CLIPTextConfig( bos_token_id=0 , eos_token_id=2 , hidden_size=32 , intermediate_size=37 , layer_norm_eps=1e-05 , num_attention_heads=4 , num_hidden_layers=5 , pad_token_id=1 , vocab_size=10_00 , hidden_act="""gelu""" , projection_dim=5_12 , ) return CLIPTextModel(snake_case__ ) def UpperCamelCase_ ( self ) -> Any: """simple docstring""" UpperCAmelCase = """cpu""" # ensure determinism for the device-dependent torch.Generator UpperCAmelCase = self.dummy_cond_unet_upscale UpperCAmelCase = DDPMScheduler() UpperCAmelCase = DDIMScheduler(prediction_type="""v_prediction""" ) UpperCAmelCase = self.dummy_vae UpperCAmelCase = self.dummy_text_encoder UpperCAmelCase = CLIPTokenizer.from_pretrained("""hf-internal-testing/tiny-random-clip""" ) UpperCAmelCase = self.dummy_image.cpu().permute(0 , 2 , 3 , 1 )[0] UpperCAmelCase = Image.fromarray(np.uinta(snake_case__ ) ).convert("""RGB""" ).resize((64, 64) ) # make sure here that pndm scheduler skips prk UpperCAmelCase = StableDiffusionUpscalePipeline( unet=snake_case__ , low_res_scheduler=snake_case__ , scheduler=snake_case__ , vae=snake_case__ , text_encoder=snake_case__ , tokenizer=snake_case__ , max_noise_level=3_50 , ) UpperCAmelCase = sd_pipe.to(snake_case__ ) sd_pipe.set_progress_bar_config(disable=snake_case__ ) UpperCAmelCase = """A painting of a squirrel eating a burger""" UpperCAmelCase = torch.Generator(device=snake_case__ ).manual_seed(0 ) UpperCAmelCase = sd_pipe( [prompt] , image=snake_case__ , generator=snake_case__ , guidance_scale=6.0 , noise_level=20 , num_inference_steps=2 , output_type="""np""" , ) UpperCAmelCase = output.images UpperCAmelCase = torch.Generator(device=snake_case__ ).manual_seed(0 ) UpperCAmelCase = sd_pipe( [prompt] , image=snake_case__ , generator=snake_case__ , guidance_scale=6.0 , noise_level=20 , num_inference_steps=2 , output_type="""np""" , return_dict=snake_case__ , )[0] UpperCAmelCase = image[0, -3:, -3:, -1] UpperCAmelCase = image_from_tuple[0, -3:, -3:, -1] UpperCAmelCase = low_res_image.size[0] * 4 assert image.shape == (1, expected_height_width, expected_height_width, 3) UpperCAmelCase = np.array([0.3_113, 0.3_910, 0.4_272, 0.4_859, 0.5_061, 0.4_652, 0.5_362, 0.5_715, 0.5_661] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-2 assert np.abs(image_from_tuple_slice.flatten() - expected_slice ).max() < 1e-2 def UpperCamelCase_ ( self ) -> List[Any]: """simple docstring""" UpperCAmelCase = """cpu""" # ensure determinism for the device-dependent torch.Generator UpperCAmelCase = self.dummy_cond_unet_upscale UpperCAmelCase = DDPMScheduler() UpperCAmelCase = DDIMScheduler(prediction_type="""v_prediction""" ) UpperCAmelCase = self.dummy_vae UpperCAmelCase = self.dummy_text_encoder UpperCAmelCase = CLIPTokenizer.from_pretrained("""hf-internal-testing/tiny-random-clip""" ) UpperCAmelCase = self.dummy_image.cpu().permute(0 , 2 , 3 , 1 )[0] UpperCAmelCase = Image.fromarray(np.uinta(snake_case__ ) ).convert("""RGB""" ).resize((64, 64) ) # make sure here that pndm scheduler skips prk UpperCAmelCase = StableDiffusionUpscalePipeline( unet=snake_case__ , low_res_scheduler=snake_case__ , scheduler=snake_case__ , vae=snake_case__ , text_encoder=snake_case__ , tokenizer=snake_case__ , max_noise_level=3_50 , ) UpperCAmelCase = sd_pipe.to(snake_case__ ) sd_pipe.set_progress_bar_config(disable=snake_case__ ) UpperCAmelCase = """A painting of a squirrel eating a burger""" UpperCAmelCase = sd_pipe( 2 * [prompt] , image=2 * [low_res_image] , guidance_scale=6.0 , noise_level=20 , num_inference_steps=2 , output_type="""np""" , ) UpperCAmelCase = output.images assert image.shape[0] == 2 UpperCAmelCase = torch.Generator(device=snake_case__ ).manual_seed(0 ) UpperCAmelCase = sd_pipe( [prompt] , image=snake_case__ , generator=snake_case__ , num_images_per_prompt=2 , guidance_scale=6.0 , noise_level=20 , num_inference_steps=2 , output_type="""np""" , ) UpperCAmelCase = output.images assert image.shape[0] == 2 @unittest.skipIf(torch_device != """cuda""" , """This test requires a GPU""" ) def UpperCamelCase_ ( self ) -> Optional[Any]: """simple docstring""" UpperCAmelCase = self.dummy_cond_unet_upscale UpperCAmelCase = DDPMScheduler() UpperCAmelCase = DDIMScheduler(prediction_type="""v_prediction""" ) UpperCAmelCase = self.dummy_vae UpperCAmelCase = self.dummy_text_encoder UpperCAmelCase = CLIPTokenizer.from_pretrained("""hf-internal-testing/tiny-random-clip""" ) UpperCAmelCase = self.dummy_image.cpu().permute(0 , 2 , 3 , 1 )[0] UpperCAmelCase = Image.fromarray(np.uinta(snake_case__ ) ).convert("""RGB""" ).resize((64, 64) ) # put models in fp16, except vae as it overflows in fp16 UpperCAmelCase = unet.half() UpperCAmelCase = text_encoder.half() # make sure here that pndm scheduler skips prk UpperCAmelCase = StableDiffusionUpscalePipeline( unet=snake_case__ , low_res_scheduler=snake_case__ , scheduler=snake_case__ , vae=snake_case__ , text_encoder=snake_case__ , tokenizer=snake_case__ , max_noise_level=3_50 , ) UpperCAmelCase = sd_pipe.to(snake_case__ ) sd_pipe.set_progress_bar_config(disable=snake_case__ ) UpperCAmelCase = """A painting of a squirrel eating a burger""" UpperCAmelCase = torch.manual_seed(0 ) UpperCAmelCase = sd_pipe( [prompt] , image=snake_case__ , generator=snake_case__ , num_inference_steps=2 , output_type="""np""" , ).images UpperCAmelCase = low_res_image.size[0] * 4 assert image.shape == (1, expected_height_width, expected_height_width, 3) @slow @require_torch_gpu class UpperCamelCase_ ( unittest.TestCase ): def UpperCamelCase_ ( self ) -> Optional[Any]: """simple docstring""" super().tearDown() gc.collect() torch.cuda.empty_cache() def UpperCamelCase_ ( self ) -> Union[str, Any]: """simple docstring""" UpperCAmelCase = load_image( """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main""" """/sd2-upscale/low_res_cat.png""" ) UpperCAmelCase = load_numpy( """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd2-upscale""" """/upsampled_cat.npy""" ) UpperCAmelCase = """stabilityai/stable-diffusion-x4-upscaler""" UpperCAmelCase = StableDiffusionUpscalePipeline.from_pretrained(snake_case__ ) pipe.to(snake_case__ ) pipe.set_progress_bar_config(disable=snake_case__ ) pipe.enable_attention_slicing() UpperCAmelCase = """a cat sitting on a park bench""" UpperCAmelCase = torch.manual_seed(0 ) UpperCAmelCase = pipe( prompt=snake_case__ , image=snake_case__ , generator=snake_case__ , output_type="""np""" , ) UpperCAmelCase = output.images[0] assert image.shape == (5_12, 5_12, 3) assert np.abs(expected_image - image ).max() < 1e-3 def UpperCamelCase_ ( self ) -> Dict: """simple docstring""" UpperCAmelCase = load_image( """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main""" """/sd2-upscale/low_res_cat.png""" ) UpperCAmelCase = load_numpy( """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd2-upscale""" """/upsampled_cat_fp16.npy""" ) UpperCAmelCase = """stabilityai/stable-diffusion-x4-upscaler""" UpperCAmelCase = StableDiffusionUpscalePipeline.from_pretrained( snake_case__ , torch_dtype=torch.floataa , ) pipe.to(snake_case__ ) pipe.set_progress_bar_config(disable=snake_case__ ) pipe.enable_attention_slicing() UpperCAmelCase = """a cat sitting on a park bench""" UpperCAmelCase = torch.manual_seed(0 ) UpperCAmelCase = pipe( prompt=snake_case__ , image=snake_case__ , generator=snake_case__ , output_type="""np""" , ) UpperCAmelCase = output.images[0] assert image.shape == (5_12, 5_12, 3) assert np.abs(expected_image - image ).max() < 5e-1 def UpperCamelCase_ ( self ) -> List[Any]: """simple docstring""" torch.cuda.empty_cache() torch.cuda.reset_max_memory_allocated() torch.cuda.reset_peak_memory_stats() UpperCAmelCase = load_image( """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main""" """/sd2-upscale/low_res_cat.png""" ) UpperCAmelCase = """stabilityai/stable-diffusion-x4-upscaler""" UpperCAmelCase = StableDiffusionUpscalePipeline.from_pretrained( snake_case__ , torch_dtype=torch.floataa , ) pipe.to(snake_case__ ) pipe.set_progress_bar_config(disable=snake_case__ ) pipe.enable_attention_slicing(1 ) pipe.enable_sequential_cpu_offload() UpperCAmelCase = """a cat sitting on a park bench""" UpperCAmelCase = torch.manual_seed(0 ) UpperCAmelCase = pipe( prompt=snake_case__ , image=snake_case__ , generator=snake_case__ , num_inference_steps=5 , output_type="""np""" , ) UpperCAmelCase = torch.cuda.max_memory_allocated() # make sure that less than 2.9 GB is allocated assert mem_bytes < 2.9 * 10**9
248
0
# Lint as: python3 import os import re import urllib.parse from pathlib import Path from typing import Callable, List, Optional, Union from zipfile import ZipFile from ..utils.file_utils import cached_path, hf_github_url from ..utils.logging import get_logger from ..utils.version import Version a_ = get_logger(__name__) class _lowercase : lowercase = 'dummy_data' lowercase = 'datasets' lowercase = False def __init__( self : Tuple , snake_case : str , snake_case : str , snake_case : Union[Version, str] , snake_case : Optional[str] = None , snake_case : bool = False , snake_case : bool = True , snake_case : Optional[List[Callable]] = None , ) -> str: """simple docstring""" UpperCamelCase_ : Dict = 0 UpperCamelCase_ : int = dataset_name UpperCamelCase_ : Tuple = cache_dir UpperCamelCase_ : List[str] = use_local_dummy_data UpperCamelCase_ : Dict = config # download_callbacks take a single url as input UpperCamelCase_ : List[Callable] = download_callbacks or [] # if False, it doesn't load existing files and it returns the paths of the dummy files relative # to the dummy_data zip file root UpperCamelCase_ : Optional[int] = load_existing_dummy_data # TODO(PVP, QL) might need to make this more general UpperCamelCase_ : Optional[Any] = str(snake_case ) # to be downloaded UpperCamelCase_ : str = None UpperCamelCase_ : Optional[int] = None @property def SCREAMING_SNAKE_CASE__ ( self : str ) -> Optional[Any]: """simple docstring""" if self._dummy_file is None: UpperCamelCase_ : Tuple = self.download_dummy_data() return self._dummy_file @property def SCREAMING_SNAKE_CASE__ ( self : Tuple ) -> Union[str, Any]: """simple docstring""" if self.config is not None: # structure is dummy / config_name / version_name return os.path.join('dummy' , self.config.name , self.version_name ) # structure is dummy / version_name return os.path.join('dummy' , self.version_name ) @property def SCREAMING_SNAKE_CASE__ ( self : Dict ) -> Optional[Any]: """simple docstring""" return os.path.join(self.dummy_data_folder , 'dummy_data.zip' ) def SCREAMING_SNAKE_CASE__ ( self : Union[str, Any] ) -> int: """simple docstring""" UpperCamelCase_ : Optional[int] = ( self.local_path_to_dummy_data if self.use_local_dummy_data is True else self.github_path_to_dummy_data ) UpperCamelCase_ : Union[str, Any] = cached_path( snake_case , cache_dir=self.cache_dir , extract_compressed_file=snake_case , force_extract=snake_case ) return os.path.join(snake_case , self.dummy_file_name ) @property def SCREAMING_SNAKE_CASE__ ( self : Optional[Any] ) -> Optional[Any]: """simple docstring""" return os.path.join(self.datasets_scripts_dir , self.dataset_name , self.dummy_zip_file ) @property def SCREAMING_SNAKE_CASE__ ( self : List[Any] ) -> str: """simple docstring""" if self._bucket_url is None: UpperCamelCase_ : int = hf_github_url(self.dataset_name , self.dummy_zip_file.replace(os.sep , '/' ) ) return self._bucket_url @property def SCREAMING_SNAKE_CASE__ ( self : Union[str, Any] ) -> List[Any]: """simple docstring""" if os.path.isdir(self.dummy_file ): return self.dummy_file # else cut off path to file -> example `xsum`. return "/".join(self.dummy_file.replace(os.sep , '/' ).split('/' )[:-1] ) def SCREAMING_SNAKE_CASE__ ( self : Any , snake_case : Optional[Any] , *snake_case : Optional[Any] ) -> Optional[int]: """simple docstring""" if self.load_existing_dummy_data: # dummy data is downloaded and tested UpperCamelCase_ : Optional[Any] = self.dummy_file else: # dummy data cannot be downloaded and only the path to dummy file is returned UpperCamelCase_ : List[Any] = self.dummy_file_name # special case when data_url is a dict if isinstance(snake_case , snake_case ): return self.create_dummy_data_dict(snake_case , snake_case ) elif isinstance(snake_case , (list, tuple) ): return self.create_dummy_data_list(snake_case , snake_case ) else: return self.create_dummy_data_single(snake_case , snake_case ) def SCREAMING_SNAKE_CASE__ ( self : Tuple , snake_case : Optional[int] , *snake_case : Optional[Any] ) -> int: """simple docstring""" return self.download_and_extract(snake_case ) def SCREAMING_SNAKE_CASE__ ( self : str , snake_case : Optional[Any] , snake_case : str ) -> List[Any]: """simple docstring""" return self.download_and_extract(snake_case ) def SCREAMING_SNAKE_CASE__ ( self : int , snake_case : Any , *snake_case : Dict , **snake_case : Optional[int] ) -> List[Any]: """simple docstring""" return path def SCREAMING_SNAKE_CASE__ ( self : Optional[int] ) -> Union[str, Any]: """simple docstring""" return {} def SCREAMING_SNAKE_CASE__ ( self : str , snake_case : str , snake_case : List[str] ) -> List[Any]: """simple docstring""" UpperCamelCase_ : Dict = {} for key, single_urls in data_url.items(): for download_callback in self.download_callbacks: if isinstance(snake_case , snake_case ): for single_url in single_urls: download_callback(snake_case ) else: UpperCamelCase_ : str = single_urls download_callback(snake_case ) # we force the name of each key to be the last file / folder name of the url path # if the url has arguments, we need to encode them with urllib.parse.quote_plus if isinstance(snake_case , snake_case ): UpperCamelCase_ : Any = [os.path.join(snake_case , urllib.parse.quote_plus(Path(snake_case ).name ) ) for x in single_urls] else: UpperCamelCase_ : str = single_urls UpperCamelCase_ : List[str] = os.path.join(snake_case , urllib.parse.quote_plus(Path(snake_case ).name ) ) UpperCamelCase_ : Optional[int] = value # make sure that values are unique if all(isinstance(snake_case , snake_case ) for i in dummy_data_dict.values() ) and len(set(dummy_data_dict.values() ) ) < len( dummy_data_dict.values() ): # append key to value to make its name unique UpperCamelCase_ : Union[str, Any] = {key: value + key for key, value in dummy_data_dict.items()} return dummy_data_dict def SCREAMING_SNAKE_CASE__ ( self : str , snake_case : Union[str, Any] , snake_case : str ) -> Any: """simple docstring""" UpperCamelCase_ : Tuple = [] # trick: if there are many shards named like `data.txt-000001-of-00300`, only use the first one UpperCamelCase_ : Dict = all(bool(re.findall('[0-9]{3,}-of-[0-9]{3,}' , snake_case ) ) for url in data_url ) UpperCamelCase_ : List[Any] = all( url.startswith('https://ftp.ncbi.nlm.nih.gov/pubmed/baseline/pubmed' ) for url in data_url ) if data_url and (is_tf_records or is_pubmed_records): UpperCamelCase_ : Any = [data_url[0]] * len(snake_case ) for single_url in data_url: for download_callback in self.download_callbacks: download_callback(snake_case ) # we force the name of each key to be the last file / folder name of the url path # if the url has arguments, we need to encode them with urllib.parse.quote_plus UpperCamelCase_ : int = os.path.join(snake_case , urllib.parse.quote_plus(single_url.split('/' )[-1] ) ) dummy_data_list.append(snake_case ) return dummy_data_list def SCREAMING_SNAKE_CASE__ ( self : Optional[Any] , snake_case : List[Any] , snake_case : List[Any] ) -> Tuple: """simple docstring""" for download_callback in self.download_callbacks: download_callback(snake_case ) # we force the name of each key to be the last file / folder name of the url path # if the url has arguments, we need to encode them with urllib.parse.quote_plus UpperCamelCase_ : Tuple = os.path.join(snake_case , urllib.parse.quote_plus(data_url.split('/' )[-1] ) ) if os.path.exists(snake_case ) or not self.load_existing_dummy_data: return value else: # Backward compatibility, maybe deprecate at one point. # For many datasets with single url calls to dl_manager.download_and_extract, # the dummy_data.zip file is actually the zipped downloaded file # while now we expected the dummy_data.zip file to be a directory containing # the downloaded file. return path_to_dummy_data def SCREAMING_SNAKE_CASE__ ( self : Optional[Any] ) -> Optional[Any]: """simple docstring""" pass def SCREAMING_SNAKE_CASE__ ( self : Dict ) -> Any: """simple docstring""" pass def SCREAMING_SNAKE_CASE__ ( self : int , snake_case : Any ) -> Union[str, Any]: """simple docstring""" def _iter_archive_members(snake_case : Dict ): # this preserves the order of the members inside the ZIP archive UpperCamelCase_ : Optional[int] = Path(self.dummy_file ).parent UpperCamelCase_ : Optional[Any] = path.relative_to(snake_case ) with ZipFile(self.local_path_to_dummy_data ) as zip_file: UpperCamelCase_ : Tuple = zip_file.namelist() for member in members: if member.startswith(relative_path.as_posix() ): yield dummy_parent_path.joinpath(snake_case ) UpperCamelCase_ : List[str] = Path(snake_case ) UpperCamelCase_ : Dict = _iter_archive_members(snake_case ) if self.use_local_dummy_data else path.rglob('*' ) for file_path in file_paths: if file_path.is_file() and not file_path.name.startswith(('.', '__') ): yield file_path.relative_to(snake_case ).as_posix(), file_path.open('rb' ) def SCREAMING_SNAKE_CASE__ ( self : Optional[Any] , snake_case : Any ) -> Any: """simple docstring""" if not isinstance(snake_case , snake_case ): UpperCamelCase_ : Tuple = [paths] for path in paths: if os.path.isfile(snake_case ): if os.path.basename(snake_case ).startswith(('.', '__') ): return yield path else: for dirpath, dirnames, filenames in os.walk(snake_case ): if os.path.basename(snake_case ).startswith(('.', '__') ): continue dirnames.sort() for filename in sorted(snake_case ): if filename.startswith(('.', '__') ): continue yield os.path.join(snake_case , snake_case )
175
from transformers import HfArgumentParser, TensorFlowBenchmark, TensorFlowBenchmarkArguments def __lowercase ( ): UpperCamelCase_ : Optional[Any] = HfArgumentParser(lowerCamelCase ) UpperCamelCase_ : Tuple = parser.parse_args_into_dataclasses()[0] UpperCamelCase_ : Dict = TensorFlowBenchmark(args=lowerCamelCase ) try: UpperCamelCase_ : Any = parser.parse_args_into_dataclasses()[0] except ValueError as e: UpperCamelCase_ : Any = 'Arg --no_{0} is no longer used, please use --no-{0} instead.' UpperCamelCase_ : Optional[int] = ' '.join(str(lowerCamelCase ).split(' ' )[:-1] ) UpperCamelCase_ : Any = '' UpperCamelCase_ : Any = eval(str(lowerCamelCase ).split(' ' )[-1] ) UpperCamelCase_ : List[Any] = [] for arg in depreciated_args: # arg[2:] removes '--' if arg[2:] in TensorFlowBenchmark.deprecated_args: # arg[5:] removes '--no_' full_error_msg += arg_error_msg.format(arg[5:] ) else: wrong_args.append(lowerCamelCase ) if len(lowerCamelCase ) > 0: UpperCamelCase_ : List[str] = full_error_msg + begin_error_msg + str(lowerCamelCase ) raise ValueError(lowerCamelCase ) benchmark.run() if __name__ == "__main__": main()
175
1
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available lowercase_ = { '''configuration_clipseg''': [ '''CLIPSEG_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''CLIPSegConfig''', '''CLIPSegTextConfig''', '''CLIPSegVisionConfig''', ], '''processing_clipseg''': ['''CLIPSegProcessor'''], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowercase_ = [ '''CLIPSEG_PRETRAINED_MODEL_ARCHIVE_LIST''', '''CLIPSegModel''', '''CLIPSegPreTrainedModel''', '''CLIPSegTextModel''', '''CLIPSegVisionModel''', '''CLIPSegForImageSegmentation''', ] if TYPE_CHECKING: from .configuration_clipseg import ( CLIPSEG_PRETRAINED_CONFIG_ARCHIVE_MAP, CLIPSegConfig, CLIPSegTextConfig, CLIPSegVisionConfig, ) from .processing_clipseg import CLIPSegProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_clipseg import ( CLIPSEG_PRETRAINED_MODEL_ARCHIVE_LIST, CLIPSegForImageSegmentation, CLIPSegModel, CLIPSegPreTrainedModel, CLIPSegTextModel, CLIPSegVisionModel, ) else: import sys lowercase_ = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
370
import inspect import warnings from typing import Any, Dict, Optional, Union from packaging import version def __lowerCAmelCase ( *__SCREAMING_SNAKE_CASE : int , __SCREAMING_SNAKE_CASE : Optional[Union[Dict, Any]] = None , __SCREAMING_SNAKE_CASE : Any=True , __SCREAMING_SNAKE_CASE : int=2 ): '''simple docstring''' from .. import __version__ __snake_case : List[Any] = take_from __snake_case : List[Any] = () if not isinstance(args[0] , __SCREAMING_SNAKE_CASE ): __snake_case : str = (args,) for attribute, version_name, message in args: if version.parse(version.parse(__SCREAMING_SNAKE_CASE ).base_version ) >= version.parse(__SCREAMING_SNAKE_CASE ): raise ValueError( F'''The deprecation tuple {(attribute, version_name, message)} should be removed since diffusers\'''' F''' version {__version__} is >= {version_name}''' ) __snake_case : Optional[Any] = None if isinstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) and attribute in deprecated_kwargs: values += (deprecated_kwargs.pop(__SCREAMING_SNAKE_CASE ),) __snake_case : Optional[Any] = F'''The `{attribute}` argument is deprecated and will be removed in version {version_name}.''' elif hasattr(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ): values += (getattr(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ),) __snake_case : Any = F'''The `{attribute}` attribute is deprecated and will be removed in version {version_name}.''' elif deprecated_kwargs is None: __snake_case : Tuple = F'''`{attribute}` is deprecated and will be removed in version {version_name}.''' if warning is not None: __snake_case : Optional[Any] = warning + """ """ if standard_warn else """""" warnings.warn(warning + message , __SCREAMING_SNAKE_CASE , stacklevel=__SCREAMING_SNAKE_CASE ) if isinstance(__SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE ) and len(__SCREAMING_SNAKE_CASE ) > 0: __snake_case : Dict = inspect.getouterframes(inspect.currentframe() )[1] __snake_case : int = call_frame.filename __snake_case : int = call_frame.lineno __snake_case : List[str] = call_frame.function __snake_case , __snake_case : List[Any] = next(iter(deprecated_kwargs.items() ) ) raise TypeError(F'''{function} in {filename} line {line_number-1} got an unexpected keyword argument `{key}`''' ) if len(__SCREAMING_SNAKE_CASE ) == 0: return elif len(__SCREAMING_SNAKE_CASE ) == 1: return values[0] return values
20
0
'''simple docstring''' import os from shutil import copyfile from typing import Any, Dict, List, Optional, Tuple import sentencepiece as spm from ...tokenization_utils import PreTrainedTokenizer from ...utils import logging lowerCAmelCase__ = logging.get_logger(__name__) lowerCAmelCase__ = {'''vocab_file''': '''spiece.model'''} lowerCAmelCase__ = { '''vocab_file''': { '''bert_for_seq_generation''': ( '''https://huggingface.co/google/bert_for_seq_generation_L-24_bbc_encoder/resolve/main/spiece.model''' ), } } lowerCAmelCase__ = {'''bert_for_seq_generation''': 512} class lowercase_ (lowerCamelCase__ ): """simple docstring""" SCREAMING_SNAKE_CASE : List[Any] = VOCAB_FILES_NAMES SCREAMING_SNAKE_CASE : Dict = PRETRAINED_VOCAB_FILES_MAP SCREAMING_SNAKE_CASE : Optional[int] = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES SCREAMING_SNAKE_CASE : List[int] = [] SCREAMING_SNAKE_CASE : Any = ['input_ids', 'attention_mask'] def __init__( self : Optional[Any] ,lowercase__ : Tuple ,lowercase__ : Tuple="<s>" ,lowercase__ : Union[str, Any]="</s>" ,lowercase__ : str="<unk>" ,lowercase__ : Tuple="<pad>" ,lowercase__ : Union[str, Any]="<::::>" ,lowercase__ : Optional[Dict[str, Any]] = None ,**lowercase__ : Any ,): __lowercase = {} if sp_model_kwargs is None else sp_model_kwargs # Add extra_ids to the special token list super().__init__( bos_token=lowercase__ ,eos_token=lowercase__ ,unk_token=lowercase__ ,pad_token=lowercase__ ,sep_token=lowercase__ ,sp_model_kwargs=self.sp_model_kwargs ,**lowercase__ ,) __lowercase = vocab_file __lowercase = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(lowercase__ ) @property def SCREAMING_SNAKE_CASE ( self : Optional[int] ): return self.sp_model.get_piece_size() def SCREAMING_SNAKE_CASE ( self : Union[str, Any] ): __lowercase = {self.convert_ids_to_tokens(lowercase__ ): i for i in range(self.vocab_size )} vocab.update(self.added_tokens_encoder ) return vocab def __getstate__( self : str ): __lowercase = self.__dict__.copy() __lowercase = None return state def __setstate__( self : Optional[int] ,lowercase__ : Optional[Any] ): __lowercase = d # for backward compatibility if not hasattr(self ,'''sp_model_kwargs''' ): __lowercase = {} __lowercase = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(self.vocab_file ) def SCREAMING_SNAKE_CASE ( self : str ,lowercase__ : str ): return self.sp_model.encode(lowercase__ ,out_type=lowercase__ ) def SCREAMING_SNAKE_CASE ( self : Tuple ,lowercase__ : Union[str, Any] ): return self.sp_model.piece_to_id(lowercase__ ) def SCREAMING_SNAKE_CASE ( self : Any ,lowercase__ : Tuple ): __lowercase = self.sp_model.IdToPiece(lowercase__ ) return token def SCREAMING_SNAKE_CASE ( self : Optional[int] ,lowercase__ : int ): __lowercase = [] __lowercase = '''''' for token in tokens: # make sure that special tokens are not decoded using sentencepiece model if token in self.all_special_tokens: out_string += self.sp_model.decode(lowercase__ ) + token __lowercase = [] else: current_sub_tokens.append(lowercase__ ) out_string += self.sp_model.decode(lowercase__ ) return out_string.strip() def SCREAMING_SNAKE_CASE ( self : int ,lowercase__ : str ,lowercase__ : Optional[str] = None ): if not os.path.isdir(lowercase__ ): logger.error(F"Vocabulary path ({save_directory}) should be a directory" ) return __lowercase = os.path.join( lowercase__ ,(filename_prefix + '''-''' if filename_prefix else '''''') + VOCAB_FILES_NAMES['''vocab_file'''] ) if os.path.abspath(self.vocab_file ) != os.path.abspath(lowercase__ ) and os.path.isfile(self.vocab_file ): copyfile(self.vocab_file ,lowercase__ ) elif not os.path.isfile(self.vocab_file ): with open(lowercase__ ,'''wb''' ) as fi: __lowercase = self.sp_model.serialized_model_proto() fi.write(lowercase__ ) return (out_vocab_file,)
104
def __magic_name__ ( __a : str ): '''simple docstring''' return [ txt[:a] + txt[a].upper() + txt[a + 1 :] for a in range(len(__a ) ) if txt[a].isalpha() ] if __name__ == "__main__": __import__('''doctest''').testmod()
244
0
import argparse import torch from transformers import ( SpeechTaConfig, SpeechTaFeatureExtractor, SpeechTaForSpeechToSpeech, SpeechTaForSpeechToText, SpeechTaForTextToSpeech, SpeechTaProcessor, SpeechTaTokenizer, logging, ) from transformers.tokenization_utils import AddedToken logging.set_verbosity_info() lowercase_ = logging.get_logger('transformers.models.speecht5') lowercase_ = { 'speech_encoder_prenet.layer_norm': 'speecht5.encoder.prenet.feature_projection.layer_norm', 'speech_encoder_prenet.post_extract_proj': 'speecht5.encoder.prenet.feature_projection.projection', 'speech_encoder_prenet.pos_conv.0': 'speecht5.encoder.prenet.pos_conv_embed.conv', 'speech_encoder_prenet.mask_emb': 'speecht5.encoder.prenet.masked_spec_embed', } lowercase_ = { 'text_encoder_prenet.encoder_prenet.0': 'speecht5.encoder.prenet.embed_tokens', 'text_encoder_prenet.encoder_prenet.1.alpha': 'speecht5.encoder.prenet.encode_positions.alpha', } lowercase_ = { 'speech_decoder_prenet.decoder_prenet.0.0.prenet.0.0': 'speecht5.decoder.prenet.layers.0', 'speech_decoder_prenet.decoder_prenet.0.0.prenet.1.0': 'speecht5.decoder.prenet.layers.1', 'speech_decoder_prenet.decoder_prenet.0.1': 'speecht5.decoder.prenet.final_layer', 'speech_decoder_prenet.decoder_prenet.1.alpha': 'speecht5.decoder.prenet.encode_positions.alpha', 'speech_decoder_prenet.spkembs_layer.0': 'speecht5.decoder.prenet.speaker_embeds_layer', } lowercase_ = { 'speech_decoder_postnet.feat_out': 'speech_decoder_postnet.feat_out', 'speech_decoder_postnet.prob_out': 'speech_decoder_postnet.prob_out', 'speech_decoder_postnet.postnet.postnet.0.0': 'speech_decoder_postnet.layers.0.conv', 'speech_decoder_postnet.postnet.postnet.0.1': 'speech_decoder_postnet.layers.0.batch_norm', 'speech_decoder_postnet.postnet.postnet.1.0': 'speech_decoder_postnet.layers.1.conv', 'speech_decoder_postnet.postnet.postnet.1.1': 'speech_decoder_postnet.layers.1.batch_norm', 'speech_decoder_postnet.postnet.postnet.2.0': 'speech_decoder_postnet.layers.2.conv', 'speech_decoder_postnet.postnet.postnet.2.1': 'speech_decoder_postnet.layers.2.batch_norm', 'speech_decoder_postnet.postnet.postnet.3.0': 'speech_decoder_postnet.layers.3.conv', 'speech_decoder_postnet.postnet.postnet.3.1': 'speech_decoder_postnet.layers.3.batch_norm', 'speech_decoder_postnet.postnet.postnet.4.0': 'speech_decoder_postnet.layers.4.conv', 'speech_decoder_postnet.postnet.postnet.4.1': 'speech_decoder_postnet.layers.4.batch_norm', } lowercase_ = { 'text_decoder_prenet.embed_tokens': 'speecht5.decoder.prenet.embed_tokens', } lowercase_ = { 'text_decoder_postnet.output_projection': 'text_decoder_postnet.lm_head', } lowercase_ = { 'encoder.layers.*.self_attn.k_proj': 'speecht5.encoder.wrapped_encoder.layers.*.attention.k_proj', 'encoder.layers.*.self_attn.v_proj': 'speecht5.encoder.wrapped_encoder.layers.*.attention.v_proj', 'encoder.layers.*.self_attn.q_proj': 'speecht5.encoder.wrapped_encoder.layers.*.attention.q_proj', 'encoder.layers.*.self_attn.out_proj': 'speecht5.encoder.wrapped_encoder.layers.*.attention.out_proj', 'encoder.layers.*.self_attn_layer_norm': 'speecht5.encoder.wrapped_encoder.layers.*.layer_norm', 'encoder.layers.*.fc1': 'speecht5.encoder.wrapped_encoder.layers.*.feed_forward.intermediate_dense', 'encoder.layers.*.fc2': 'speecht5.encoder.wrapped_encoder.layers.*.feed_forward.output_dense', 'encoder.layers.*.final_layer_norm': 'speecht5.encoder.wrapped_encoder.layers.*.final_layer_norm', 'encoder.layer_norm': 'speecht5.encoder.wrapped_encoder.layer_norm', 'encoder.pos_emb.pe_k': 'speecht5.encoder.wrapped_encoder.embed_positions.pe_k', } lowercase_ = { 'decoder.layers.*.self_attn.k_proj': 'speecht5.decoder.wrapped_decoder.layers.*.self_attn.k_proj', 'decoder.layers.*.self_attn.v_proj': 'speecht5.decoder.wrapped_decoder.layers.*.self_attn.v_proj', 'decoder.layers.*.self_attn.q_proj': 'speecht5.decoder.wrapped_decoder.layers.*.self_attn.q_proj', 'decoder.layers.*.self_attn.out_proj': 'speecht5.decoder.wrapped_decoder.layers.*.self_attn.out_proj', 'decoder.layers.*.self_attn_layer_norm': 'speecht5.decoder.wrapped_decoder.layers.*.self_attn_layer_norm', 'decoder.layers.*.encoder_attn.k_proj': 'speecht5.decoder.wrapped_decoder.layers.*.encoder_attn.k_proj', 'decoder.layers.*.encoder_attn.v_proj': 'speecht5.decoder.wrapped_decoder.layers.*.encoder_attn.v_proj', 'decoder.layers.*.encoder_attn.q_proj': 'speecht5.decoder.wrapped_decoder.layers.*.encoder_attn.q_proj', 'decoder.layers.*.encoder_attn.out_proj': 'speecht5.decoder.wrapped_decoder.layers.*.encoder_attn.out_proj', 'decoder.layers.*.encoder_attn_layer_norm': 'speecht5.decoder.wrapped_decoder.layers.*.encoder_attn_layer_norm', 'decoder.layers.*.fc1': 'speecht5.decoder.wrapped_decoder.layers.*.feed_forward.intermediate_dense', 'decoder.layers.*.fc2': 'speecht5.decoder.wrapped_decoder.layers.*.feed_forward.output_dense', 'decoder.layers.*.final_layer_norm': 'speecht5.decoder.wrapped_decoder.layers.*.final_layer_norm', } lowercase_ = { **MAPPING_SPEECH_ENCODER_PRENET, **MAPPING_ENCODER, **MAPPING_DECODER, **MAPPING_TEXT_DECODER_PRENET, **MAPPING_TEXT_DECODER_POSTNET, } lowercase_ = { **MAPPING_TEXT_ENCODER_PRENET, **MAPPING_ENCODER, **MAPPING_DECODER, **MAPPING_SPEECH_DECODER_PRENET, **MAPPING_SPEECH_DECODER_POSTNET, } lowercase_ = { **MAPPING_SPEECH_ENCODER_PRENET, **MAPPING_ENCODER, **MAPPING_DECODER, **MAPPING_SPEECH_DECODER_PRENET, **MAPPING_SPEECH_DECODER_POSTNET, } lowercase_ = [] lowercase_ = [ 'encoder.version', 'encoder.layers.*.norm_k.weight', 'encoder.layers.*.norm_k.bias', 'decoder.version', 'decoder.layers.*.norm_k.weight', 'decoder.layers.*.norm_k.bias', 'decoder.pos_emb.pe_k', 'speech_encoder_prenet.embed_positions._float_tensor', 'text_decoder_prenet.embed_positions._float_tensor', ] lowercase_ = IGNORE_KEYS + [ 'encoder.proj', 'text_encoder_prenet.*', 'speech_decoder_prenet.*', 'speech_decoder_postnet.*', ] lowercase_ = IGNORE_KEYS + [ 'encoder.proj', 'speech_encoder_prenet.*', 'text_decoder_prenet.*', 'text_decoder_postnet.*', ] lowercase_ = IGNORE_KEYS + [ 'encoder.proj', 'text_encoder_prenet.*', 'text_decoder_prenet.*', 'text_decoder_postnet.*', ] def UpperCamelCase__ ( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ): for attribute in key.split('.' ): __lowerCamelCase : str = getattr(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) if weight_type is not None: __lowerCamelCase : str = getattr(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ).shape else: __lowerCamelCase : Any = hf_pointer.shape if hf_shape != value.shape: raise ValueError( f'Shape of hf {key + "." + weight_type if weight_type is not None else ""} is {hf_shape}, but should be' f' {value.shape} for {full_name}' ) if weight_type == "weight": __lowerCamelCase : str = value elif weight_type == "weight_g": __lowerCamelCase : Any = value elif weight_type == "weight_v": __lowerCamelCase : int = value elif weight_type == "bias": __lowerCamelCase : List[Any] = value elif weight_type == "running_mean": __lowerCamelCase : List[str] = value elif weight_type == "running_var": __lowerCamelCase : str = value elif weight_type == "num_batches_tracked": __lowerCamelCase : str = value else: __lowerCamelCase : Optional[Any] = value logger.info(f'{key + ("." + weight_type if weight_type is not None else "")} was initialized from {full_name}.' ) def UpperCamelCase__ ( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ): for key in ignore_keys: if key.endswith('.*' ): if name.startswith(key[:-1] ): return True elif ".*." in key: __lowerCamelCase , __lowerCamelCase : str = key.split('.*.' ) if prefix in name and suffix in name: return True elif key in name: return True return False def UpperCamelCase__ ( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ): __lowerCamelCase : Tuple = [] if task == "s2t": __lowerCamelCase : Optional[Any] = hf_model.speechta.encoder.prenet.feature_encoder __lowerCamelCase : Optional[Any] = MAPPING_S2T __lowerCamelCase : Optional[int] = IGNORE_KEYS_S2T elif task == "t2s": __lowerCamelCase : List[str] = None __lowerCamelCase : Optional[int] = MAPPING_T2S __lowerCamelCase : List[str] = IGNORE_KEYS_T2S elif task == "s2s": __lowerCamelCase : Any = hf_model.speechta.encoder.prenet.feature_encoder __lowerCamelCase : Optional[Any] = MAPPING_S2S __lowerCamelCase : Union[str, Any] = IGNORE_KEYS_S2S else: raise ValueError(f'Unsupported task: {task}' ) for name, value in fairseq_dict.items(): if should_ignore(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ): logger.info(f'{name} was ignored' ) continue __lowerCamelCase : Optional[int] = False if "conv_layers" in name: load_conv_layer( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , hf_model.config.feat_extract_norm == 'group' , ) __lowerCamelCase : Any = True else: for key, mapped_key in MAPPING.items(): # mapped_key = "speecht5." + mapped_key if mapped_key not in TOP_LEVEL_KEYS else mapped_key if "*" in key: __lowerCamelCase , __lowerCamelCase : List[str] = key.split('.*.' ) if prefix in name and suffix in name: __lowerCamelCase : Optional[Any] = suffix # if key in name or key.split("w2v_model.")[-1] == name.split(".")[0]: if key in name: __lowerCamelCase : Optional[int] = True if "*" in mapped_key: __lowerCamelCase : List[str] = name.split(SCREAMING_SNAKE_CASE__ )[0].split('.' )[-2] __lowerCamelCase : Any = mapped_key.replace('*' , SCREAMING_SNAKE_CASE__ ) if "weight_g" in name: __lowerCamelCase : List[Any] = 'weight_g' elif "weight_v" in name: __lowerCamelCase : Dict = 'weight_v' elif "bias" in name: __lowerCamelCase : Any = 'bias' elif "weight" in name: __lowerCamelCase : List[Any] = 'weight' elif "running_mean" in name: __lowerCamelCase : Union[str, Any] = 'running_mean' elif "running_var" in name: __lowerCamelCase : Optional[int] = 'running_var' elif "num_batches_tracked" in name: __lowerCamelCase : Optional[Any] = 'num_batches_tracked' else: __lowerCamelCase : Dict = None set_recursively(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) continue if not is_used: unused_weights.append(SCREAMING_SNAKE_CASE__ ) logger.warning(f'Unused weights: {unused_weights}' ) def UpperCamelCase__ ( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ): __lowerCamelCase : Dict = full_name.split('conv_layers.' )[-1] __lowerCamelCase : List[Any] = name.split('.' ) __lowerCamelCase : str = int(items[0] ) __lowerCamelCase : List[str] = int(items[1] ) if type_id == 0: if "bias" in name: if value.shape != feature_extractor.conv_layers[layer_id].conv.bias.data.shape: raise ValueError( f'{full_name} has size {value.shape}, but' f' {feature_extractor.conv_layers[layer_id].conv.bias.data.shape} was found.' ) __lowerCamelCase : Dict = value logger.info(f'Feat extract conv layer {layer_id} was initialized from {full_name}.' ) elif "weight" in name: if value.shape != feature_extractor.conv_layers[layer_id].conv.weight.data.shape: raise ValueError( f'{full_name} has size {value.shape}, but' f' {feature_extractor.conv_layers[layer_id].conv.weight.data.shape} was found.' ) __lowerCamelCase : Union[str, Any] = value logger.info(f'Feat extract conv layer {layer_id} was initialized from {full_name}.' ) elif (type_id == 2 and not use_group_norm) or (type_id == 2 and layer_id == 0 and use_group_norm): if "bias" in name: if value.shape != feature_extractor.conv_layers[layer_id].layer_norm.bias.data.shape: raise ValueError( f'{full_name} has size {value.shape}, but' f' {feature_extractor.conv_layers[layer_id].layer_norm.bias.data.shape} was found.' ) __lowerCamelCase : Union[str, Any] = value logger.info(f'Feat extract layer norm weight of layer {layer_id} was initialized from {full_name}.' ) elif "weight" in name: if value.shape != feature_extractor.conv_layers[layer_id].layer_norm.weight.data.shape: raise ValueError( f'{full_name} has size {value.shape}, but' f' {feature_extractor.conv_layers[layer_id].layer_norm.weight.data.shape} was found.' ) __lowerCamelCase : int = value logger.info(f'Feat extract layer norm weight of layer {layer_id} was initialized from {full_name}.' ) else: unused_weights.append(SCREAMING_SNAKE_CASE__ ) @torch.no_grad() def UpperCamelCase__ ( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__=None , SCREAMING_SNAKE_CASE__=None , SCREAMING_SNAKE_CASE__=None , ): if config_path is not None: __lowerCamelCase : Optional[int] = SpeechTaConfig.from_pretrained(SCREAMING_SNAKE_CASE__ ) else: __lowerCamelCase : Optional[int] = SpeechTaConfig() if task == "s2t": __lowerCamelCase : Union[str, Any] = config.max_text_positions __lowerCamelCase : List[Any] = SpeechTaForSpeechToText(SCREAMING_SNAKE_CASE__ ) elif task == "t2s": __lowerCamelCase : Tuple = 1_876 __lowerCamelCase : int = 600 __lowerCamelCase : Union[str, Any] = config.max_speech_positions __lowerCamelCase : Optional[Any] = SpeechTaForTextToSpeech(SCREAMING_SNAKE_CASE__ ) elif task == "s2s": __lowerCamelCase : Optional[int] = 1_876 __lowerCamelCase : Union[str, Any] = config.max_speech_positions __lowerCamelCase : str = SpeechTaForSpeechToSpeech(SCREAMING_SNAKE_CASE__ ) else: raise ValueError(f'Unknown task name: {task}' ) if vocab_path: __lowerCamelCase : List[str] = SpeechTaTokenizer(SCREAMING_SNAKE_CASE__ , model_max_length=config.max_text_positions ) # Mask token behaves like a normal word, i.e. include the space before it __lowerCamelCase : Optional[int] = AddedToken('<mask>' , lstrip=SCREAMING_SNAKE_CASE__ , rstrip=SCREAMING_SNAKE_CASE__ ) __lowerCamelCase : Any = mask_token tokenizer.add_special_tokens({'mask_token': mask_token} ) tokenizer.add_tokens(['<ctc_blank>'] ) __lowerCamelCase : int = SpeechTaFeatureExtractor() __lowerCamelCase : Dict = SpeechTaProcessor(tokenizer=SCREAMING_SNAKE_CASE__ , feature_extractor=SCREAMING_SNAKE_CASE__ ) processor.save_pretrained(SCREAMING_SNAKE_CASE__ ) __lowerCamelCase : Optional[int] = torch.load(SCREAMING_SNAKE_CASE__ ) recursively_load_weights(fairseq_checkpoint['model'] , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) model.save_pretrained(SCREAMING_SNAKE_CASE__ ) if repo_id: print('Pushing to the hub...' ) processor.push_to_hub(SCREAMING_SNAKE_CASE__ ) model.push_to_hub(SCREAMING_SNAKE_CASE__ ) if __name__ == "__main__": lowercase_ = argparse.ArgumentParser() parser.add_argument( '--task', default='s2t', type=str, help='Type of the SpeechT5 model you\'d like to convert. Should be one of \'s2t\', \'t2s\', \'s2s\'.', ) parser.add_argument('--checkpoint_path', required=True, default=None, type=str, help='Path to fairseq checkpoint') parser.add_argument('--vocab_path', default=None, type=str, help='Path to SentencePiece model') parser.add_argument('--config_path', default=None, type=str, help='Path to hf config.json of model to convert') parser.add_argument( '--pytorch_dump_folder_path', required=True, default=None, type=str, help='Path to the output PyTorch model.' ) parser.add_argument( '--push_to_hub', default=None, type=str, help='Where to upload the converted model on the 🤗 hub.' ) lowercase_ = parser.parse_args() convert_speechta_checkpoint( args.task, args.checkpoint_path, args.pytorch_dump_folder_path, args.config_path, args.vocab_path, args.push_to_hub, )
194
import inspect from typing import Optional, Union import numpy as np import PIL import torch from torch.nn import functional as F from torchvision import transforms from transformers import CLIPFeatureExtractor, CLIPModel, CLIPTextModel, CLIPTokenizer from diffusers import ( AutoencoderKL, DDIMScheduler, DiffusionPipeline, DPMSolverMultistepScheduler, LMSDiscreteScheduler, PNDMScheduler, UNetaDConditionModel, ) from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion import StableDiffusionPipelineOutput from diffusers.utils import ( PIL_INTERPOLATION, randn_tensor, ) def UpperCamelCase__ ( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ): if isinstance(SCREAMING_SNAKE_CASE__ , torch.Tensor ): return image elif isinstance(SCREAMING_SNAKE_CASE__ , PIL.Image.Image ): __lowerCamelCase : Union[str, Any] = [image] if isinstance(image[0] , PIL.Image.Image ): __lowerCamelCase : Any = [np.array(i.resize((w, h) , resample=PIL_INTERPOLATION['lanczos'] ) )[None, :] for i in image] __lowerCamelCase : Optional[int] = np.concatenate(SCREAMING_SNAKE_CASE__ , axis=0 ) __lowerCamelCase : str = np.array(SCREAMING_SNAKE_CASE__ ).astype(np.floataa ) / 255.0 __lowerCamelCase : List[str] = image.transpose(0 , 3 , 1 , 2 ) __lowerCamelCase : Union[str, Any] = 2.0 * image - 1.0 __lowerCamelCase : Tuple = torch.from_numpy(SCREAMING_SNAKE_CASE__ ) elif isinstance(image[0] , torch.Tensor ): __lowerCamelCase : str = torch.cat(SCREAMING_SNAKE_CASE__ , dim=0 ) return image def UpperCamelCase__ ( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__=0.9_995 ): if not isinstance(SCREAMING_SNAKE_CASE__ , np.ndarray ): __lowerCamelCase : List[str] = True __lowerCamelCase : str = va.device __lowerCamelCase : int = va.cpu().numpy() __lowerCamelCase : List[str] = va.cpu().numpy() __lowerCamelCase : str = np.sum(va * va / (np.linalg.norm(SCREAMING_SNAKE_CASE__ ) * np.linalg.norm(SCREAMING_SNAKE_CASE__ )) ) if np.abs(SCREAMING_SNAKE_CASE__ ) > DOT_THRESHOLD: __lowerCamelCase : Union[str, Any] = (1 - t) * va + t * va else: __lowerCamelCase : List[Any] = np.arccos(SCREAMING_SNAKE_CASE__ ) __lowerCamelCase : Dict = np.sin(SCREAMING_SNAKE_CASE__ ) __lowerCamelCase : str = theta_a * t __lowerCamelCase : List[Any] = np.sin(SCREAMING_SNAKE_CASE__ ) __lowerCamelCase : str = np.sin(theta_a - theta_t ) / sin_theta_a __lowerCamelCase : List[Any] = sin_theta_t / sin_theta_a __lowerCamelCase : Union[str, Any] = sa * va + sa * va if inputs_are_torch: __lowerCamelCase : str = torch.from_numpy(SCREAMING_SNAKE_CASE__ ).to(SCREAMING_SNAKE_CASE__ ) return va def UpperCamelCase__ ( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ): __lowerCamelCase : List[Any] = F.normalize(SCREAMING_SNAKE_CASE__ , dim=-1 ) __lowerCamelCase : Union[str, Any] = F.normalize(SCREAMING_SNAKE_CASE__ , dim=-1 ) return (x - y).norm(dim=-1 ).div(2 ).arcsin().pow(2 ).mul(2 ) def UpperCamelCase__ ( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ): for param in model.parameters(): __lowerCamelCase : Any = value class A_ ( __UpperCamelCase ): '''simple docstring''' def __init__( self: Any , a: AutoencoderKL , a: CLIPTextModel , a: CLIPModel , a: CLIPTokenizer , a: UNetaDConditionModel , a: Union[PNDMScheduler, LMSDiscreteScheduler, DDIMScheduler, DPMSolverMultistepScheduler] , a: CLIPFeatureExtractor , a: Union[str, Any]=None , a: Union[str, Any]=None , a: Union[str, Any]=None , ): super().__init__() self.register_modules( vae=a , text_encoder=a , clip_model=a , tokenizer=a , unet=a , scheduler=a , feature_extractor=a , coca_model=a , coca_tokenizer=a , coca_transform=a , ) __lowerCamelCase : Tuple = ( feature_extractor.size if isinstance(feature_extractor.size , a ) else feature_extractor.size['shortest_edge'] ) __lowerCamelCase : List[Any] = transforms.Normalize(mean=feature_extractor.image_mean , std=feature_extractor.image_std ) set_requires_grad(self.text_encoder , a ) set_requires_grad(self.clip_model , a ) def _snake_case ( self: Optional[Any] , a: Optional[Union[str, int]] = "auto" ): if slice_size == "auto": # half the attention head size is usually a good trade-off between # speed and memory __lowerCamelCase : Any = self.unet.config.attention_head_dim // 2 self.unet.set_attention_slice(a ) def _snake_case ( self: Dict ): self.enable_attention_slicing(a ) def _snake_case ( self: Optional[Any] ): set_requires_grad(self.vae , a ) def _snake_case ( self: List[Any] ): set_requires_grad(self.vae , a ) def _snake_case ( self: int ): set_requires_grad(self.unet , a ) def _snake_case ( self: int ): set_requires_grad(self.unet , a ) def _snake_case ( self: Optional[Any] , a: Union[str, Any] , a: List[str] , a: List[Any] ): # get the original timestep using init_timestep __lowerCamelCase : List[Any] = min(int(num_inference_steps * strength ) , a ) __lowerCamelCase : str = max(num_inference_steps - init_timestep , 0 ) __lowerCamelCase : List[Any] = self.scheduler.timesteps[t_start:] return timesteps, num_inference_steps - t_start def _snake_case ( self: Union[str, Any] , a: Optional[Any] , a: Any , a: Optional[int] , a: Optional[Any] , a: Union[str, Any] , a: List[str]=None ): if not isinstance(a , torch.Tensor ): raise ValueError(F'`image` has to be of type `torch.Tensor` but is {type(a )}' ) __lowerCamelCase : Union[str, Any] = image.to(device=a , dtype=a ) if isinstance(a , a ): __lowerCamelCase : str = [ self.vae.encode(image[i : i + 1] ).latent_dist.sample(generator[i] ) for i in range(a ) ] __lowerCamelCase : Tuple = torch.cat(a , dim=0 ) else: __lowerCamelCase : List[Any] = self.vae.encode(a ).latent_dist.sample(a ) # Hardcode 0.18215 because stable-diffusion-2-base has not self.vae.config.scaling_factor __lowerCamelCase : List[str] = 0.1_8_2_1_5 * init_latents __lowerCamelCase : Union[str, Any] = init_latents.repeat_interleave(a , dim=0 ) __lowerCamelCase : Optional[int] = randn_tensor(init_latents.shape , generator=a , device=a , dtype=a ) # get latents __lowerCamelCase : Union[str, Any] = self.scheduler.add_noise(a , a , a ) __lowerCamelCase : int = init_latents return latents def _snake_case ( self: Optional[int] , a: Any ): __lowerCamelCase : List[Any] = self.coca_transform(a ).unsqueeze(0 ) with torch.no_grad(), torch.cuda.amp.autocast(): __lowerCamelCase : Any = self.coca_model.generate(transformed_image.to(device=self.device , dtype=self.coca_model.dtype ) ) __lowerCamelCase : str = self.coca_tokenizer.decode(generated[0].cpu().numpy() ) return generated.split('<end_of_text>' )[0].replace('<start_of_text>' , '' ).rstrip(' .,' ) def _snake_case ( self: Any , a: Tuple , a: Tuple ): __lowerCamelCase : Dict = self.feature_extractor.preprocess(a ) __lowerCamelCase : Dict = torch.from_numpy(clip_image_input['pixel_values'][0] ).unsqueeze(0 ).to(self.device ).half() __lowerCamelCase : List[str] = self.clip_model.get_image_features(a ) __lowerCamelCase : Optional[Any] = image_embeddings_clip / image_embeddings_clip.norm(p=2 , dim=-1 , keepdim=a ) __lowerCamelCase : Tuple = image_embeddings_clip.repeat_interleave(a , dim=0 ) return image_embeddings_clip @torch.enable_grad() def _snake_case ( self: str , a: str , a: int , a: List[Any] , a: str , a: List[Any] , a: Dict , a: int , ): __lowerCamelCase : Optional[Any] = latents.detach().requires_grad_() __lowerCamelCase : str = self.scheduler.scale_model_input(a , a ) # predict the noise residual __lowerCamelCase : Optional[int] = self.unet(a , a , encoder_hidden_states=a ).sample if isinstance(self.scheduler , (PNDMScheduler, DDIMScheduler, DPMSolverMultistepScheduler) ): __lowerCamelCase : str = self.scheduler.alphas_cumprod[timestep] __lowerCamelCase : Dict = 1 - alpha_prod_t # compute predicted original sample from predicted noise also called # "predicted x_0" of formula (12) from https://arxiv.org/pdf/2010.02502.pdf __lowerCamelCase : Optional[int] = (latents - beta_prod_t ** 0.5 * noise_pred) / alpha_prod_t ** 0.5 __lowerCamelCase : Optional[int] = torch.sqrt(a ) __lowerCamelCase : int = pred_original_sample * (fac) + latents * (1 - fac) elif isinstance(self.scheduler , a ): __lowerCamelCase : str = self.scheduler.sigmas[index] __lowerCamelCase : List[Any] = latents - sigma * noise_pred else: raise ValueError(F'scheduler type {type(self.scheduler )} not supported' ) # Hardcode 0.18215 because stable-diffusion-2-base has not self.vae.config.scaling_factor __lowerCamelCase : Optional[int] = 1 / 0.1_8_2_1_5 * sample __lowerCamelCase : Optional[Any] = self.vae.decode(a ).sample __lowerCamelCase : Tuple = (image / 2 + 0.5).clamp(0 , 1 ) __lowerCamelCase : Any = transforms.Resize(self.feature_extractor_size )(a ) __lowerCamelCase : Union[str, Any] = self.normalize(a ).to(latents.dtype ) __lowerCamelCase : Tuple = self.clip_model.get_image_features(a ) __lowerCamelCase : List[str] = image_embeddings_clip / image_embeddings_clip.norm(p=2 , dim=-1 , keepdim=a ) __lowerCamelCase : List[str] = spherical_dist_loss(a , a ).mean() * clip_guidance_scale __lowerCamelCase : Tuple = -torch.autograd.grad(a , a )[0] if isinstance(self.scheduler , a ): __lowerCamelCase : Optional[int] = latents.detach() + grads * (sigma**2) __lowerCamelCase : List[Any] = noise_pred_original else: __lowerCamelCase : str = noise_pred_original - torch.sqrt(a ) * grads return noise_pred, latents @torch.no_grad() def __call__( self: Any , a: Union[torch.FloatTensor, PIL.Image.Image] , a: Union[torch.FloatTensor, PIL.Image.Image] , a: Optional[str] = None , a: Optional[str] = None , a: Optional[int] = 512 , a: Optional[int] = 512 , a: float = 0.6 , a: Optional[int] = 50 , a: Optional[float] = 7.5 , a: Optional[int] = 1 , a: float = 0.0 , a: Optional[float] = 100 , a: Optional[torch.Generator] = None , a: Optional[str] = "pil" , a: bool = True , a: float = 0.8 , a: float = 0.1 , a: float = 0.1 , ): if isinstance(a , a ) and len(a ) != batch_size: raise ValueError(F'You have passed {batch_size} batch_size, but only {len(a )} generators.' ) if height % 8 != 0 or width % 8 != 0: raise ValueError(F'`height` and `width` have to be divisible by 8 but are {height} and {width}.' ) if isinstance(a , torch.Generator ) and batch_size > 1: __lowerCamelCase : List[Any] = [generator] + [None] * (batch_size - 1) __lowerCamelCase : Dict = [ ('model', self.coca_model is None), ('tokenizer', self.coca_tokenizer is None), ('transform', self.coca_transform is None), ] __lowerCamelCase : Any = [x[0] for x in coca_is_none if x[1]] __lowerCamelCase : str = ', '.join(a ) # generate prompts with coca model if prompt is None if content_prompt is None: if len(a ): raise ValueError( F'Content prompt is None and CoCa [{coca_is_none_str}] is None.' F'Set prompt or pass Coca [{coca_is_none_str}] to DiffusionPipeline.' ) __lowerCamelCase : Any = self.get_image_description(a ) if style_prompt is None: if len(a ): raise ValueError( F'Style prompt is None and CoCa [{coca_is_none_str}] is None.' F' Set prompt or pass Coca [{coca_is_none_str}] to DiffusionPipeline.' ) __lowerCamelCase : Tuple = self.get_image_description(a ) # get prompt text embeddings for content and style __lowerCamelCase : int = self.tokenizer( a , padding='max_length' , max_length=self.tokenizer.model_max_length , truncation=a , return_tensors='pt' , ) __lowerCamelCase : Dict = self.text_encoder(content_text_input.input_ids.to(self.device ) )[0] __lowerCamelCase : Union[str, Any] = self.tokenizer( a , padding='max_length' , max_length=self.tokenizer.model_max_length , truncation=a , return_tensors='pt' , ) __lowerCamelCase : Any = self.text_encoder(style_text_input.input_ids.to(self.device ) )[0] __lowerCamelCase : List[Any] = slerp(a , a , a ) # duplicate text embeddings for each generation per prompt __lowerCamelCase : Any = text_embeddings.repeat_interleave(a , dim=0 ) # set timesteps __lowerCamelCase : List[Any] = 'offset' in set(inspect.signature(self.scheduler.set_timesteps ).parameters.keys() ) __lowerCamelCase : Union[str, Any] = {} if accepts_offset: __lowerCamelCase : Dict = 1 self.scheduler.set_timesteps(a , **a ) # Some schedulers like PNDM have timesteps as arrays # It's more optimized to move all timesteps to correct device beforehand self.scheduler.timesteps.to(self.device ) __lowerCamelCase , __lowerCamelCase : Dict = self.get_timesteps(a , a , self.device ) __lowerCamelCase : Tuple = timesteps[:1].repeat(a ) # Preprocess image __lowerCamelCase : Any = preprocess(a , a , a ) __lowerCamelCase : str = self.prepare_latents( a , a , a , text_embeddings.dtype , self.device , a ) __lowerCamelCase : Dict = preprocess(a , a , a ) __lowerCamelCase : Optional[int] = self.prepare_latents( a , a , a , text_embeddings.dtype , self.device , a ) __lowerCamelCase : int = slerp(a , a , a ) if clip_guidance_scale > 0: __lowerCamelCase : List[str] = self.get_clip_image_embeddings(a , a ) __lowerCamelCase : Union[str, Any] = self.get_clip_image_embeddings(a , a ) __lowerCamelCase : Union[str, Any] = slerp( a , a , a ) # here `guidance_scale` is defined analog to the guidance weight `w` of equation (2) # of the Imagen paper: https://arxiv.org/pdf/2205.11487.pdf . `guidance_scale = 1` # corresponds to doing no classifier free guidance. __lowerCamelCase : Tuple = guidance_scale > 1.0 # get unconditional embeddings for classifier free guidance if do_classifier_free_guidance: __lowerCamelCase : Optional[int] = content_text_input.input_ids.shape[-1] __lowerCamelCase : int = self.tokenizer([''] , padding='max_length' , max_length=a , return_tensors='pt' ) __lowerCamelCase : List[Any] = self.text_encoder(uncond_input.input_ids.to(self.device ) )[0] # duplicate unconditional embeddings for each generation per prompt __lowerCamelCase : List[Any] = uncond_embeddings.repeat_interleave(a , dim=0 ) # For classifier free guidance, we need to do two forward passes. # Here we concatenate the unconditional and text embeddings into a single batch # to avoid doing two forward passes __lowerCamelCase : int = torch.cat([uncond_embeddings, text_embeddings] ) # get the initial random noise unless the user supplied it # Unlike in other pipelines, latents need to be generated in the target device # for 1-to-1 results reproducibility with the CompVis implementation. # However this currently doesn't work in `mps`. __lowerCamelCase : str = (batch_size, self.unet.config.in_channels, height // 8, width // 8) __lowerCamelCase : List[str] = text_embeddings.dtype if latents is None: if self.device.type == "mps": # randn does not work reproducibly on mps __lowerCamelCase : Tuple = torch.randn(a , generator=a , device='cpu' , dtype=a ).to( self.device ) else: __lowerCamelCase : List[Any] = torch.randn(a , generator=a , device=self.device , dtype=a ) else: if latents.shape != latents_shape: raise ValueError(F'Unexpected latents shape, got {latents.shape}, expected {latents_shape}' ) __lowerCamelCase : List[str] = latents.to(self.device ) # scale the initial noise by the standard deviation required by the scheduler __lowerCamelCase : str = latents * self.scheduler.init_noise_sigma # prepare extra kwargs for the scheduler step, since not all schedulers have the same signature # eta (η) is only used with the DDIMScheduler, it will be ignored for other schedulers. # eta corresponds to η in DDIM paper: https://arxiv.org/abs/2010.02502 # and should be between [0, 1] __lowerCamelCase : int = 'eta' in set(inspect.signature(self.scheduler.step ).parameters.keys() ) __lowerCamelCase : Dict = {} if accepts_eta: __lowerCamelCase : List[str] = eta # check if the scheduler accepts generator __lowerCamelCase : Optional[int] = 'generator' in set(inspect.signature(self.scheduler.step ).parameters.keys() ) if accepts_generator: __lowerCamelCase : Optional[Any] = generator with self.progress_bar(total=a ): for i, t in enumerate(a ): # expand the latents if we are doing classifier free guidance __lowerCamelCase : Optional[int] = torch.cat([latents] * 2 ) if do_classifier_free_guidance else latents __lowerCamelCase : Union[str, Any] = self.scheduler.scale_model_input(a , a ) # predict the noise residual __lowerCamelCase : Tuple = self.unet(a , a , encoder_hidden_states=a ).sample # perform classifier free guidance if do_classifier_free_guidance: __lowerCamelCase , __lowerCamelCase : str = noise_pred.chunk(2 ) __lowerCamelCase : List[str] = noise_pred_uncond + guidance_scale * (noise_pred_text - noise_pred_uncond) # perform clip guidance if clip_guidance_scale > 0: __lowerCamelCase : str = ( text_embeddings.chunk(2 )[1] if do_classifier_free_guidance else text_embeddings ) __lowerCamelCase , __lowerCamelCase : int = self.cond_fn( a , a , a , a , a , a , a , ) # compute the previous noisy sample x_t -> x_t-1 __lowerCamelCase : Tuple = self.scheduler.step(a , a , a , **a ).prev_sample # Hardcode 0.18215 because stable-diffusion-2-base has not self.vae.config.scaling_factor __lowerCamelCase : List[Any] = 1 / 0.1_8_2_1_5 * latents __lowerCamelCase : Union[str, Any] = self.vae.decode(a ).sample __lowerCamelCase : Optional[int] = (image / 2 + 0.5).clamp(0 , 1 ) __lowerCamelCase : Any = image.cpu().permute(0 , 2 , 3 , 1 ).numpy() if output_type == "pil": __lowerCamelCase : Union[str, Any] = self.numpy_to_pil(a ) if not return_dict: return (image, None) return StableDiffusionPipelineOutput(images=a , nsfw_content_detected=a )
194
1
import math from ...configuration_utils import PretrainedConfig from ...utils import logging __UpperCAmelCase = logging.get_logger(__name__) __UpperCAmelCase = { 'facebook/data2vec-base-960h': 'https://huggingface.co/facebook/data2vec-audio-base-960h/resolve/main/config.json', # See all Data2VecAudio models at https://huggingface.co/models?filter=data2vec-audio } class lowerCamelCase (_snake_case ): '''simple docstring''' _snake_case : Optional[Any] = '''data2vec-audio''' def __init__( self , _UpperCamelCase=3_2 , _UpperCamelCase=7_6_8 , _UpperCamelCase=1_2 , _UpperCamelCase=1_2 , _UpperCamelCase=3_0_7_2 , _UpperCamelCase="gelu" , _UpperCamelCase=0.1 , _UpperCamelCase=0.1 , _UpperCamelCase=0.1 , _UpperCamelCase=0.0 , _UpperCamelCase=0.1 , _UpperCamelCase=0.1 , _UpperCamelCase=0.02 , _UpperCamelCase=1E-5 , _UpperCamelCase="gelu" , _UpperCamelCase=(5_1_2, 5_1_2, 5_1_2, 5_1_2, 5_1_2, 5_1_2, 5_1_2) , _UpperCamelCase=(5, 2, 2, 2, 2, 2, 2) , _UpperCamelCase=(1_0, 3, 3, 3, 3, 2, 2) , _UpperCamelCase=False , _UpperCamelCase=1_6 , _UpperCamelCase=1_9 , _UpperCamelCase=5 , _UpperCamelCase=0.05 , _UpperCamelCase=1_0 , _UpperCamelCase=2 , _UpperCamelCase=0.0 , _UpperCamelCase=1_0 , _UpperCamelCase=0 , _UpperCamelCase="sum" , _UpperCamelCase=False , _UpperCamelCase=False , _UpperCamelCase=2_5_6 , _UpperCamelCase=(5_1_2, 5_1_2, 5_1_2, 5_1_2, 1_5_0_0) , _UpperCamelCase=(5, 3, 3, 1, 1) , _UpperCamelCase=(1, 2, 3, 1, 1) , _UpperCamelCase=5_1_2 , _UpperCamelCase=0 , _UpperCamelCase=1 , _UpperCamelCase=2 , _UpperCamelCase=False , _UpperCamelCase=3 , _UpperCamelCase=2 , _UpperCamelCase=3 , _UpperCamelCase=None , **_UpperCamelCase , ) -> Any: super().__init__(**_UpperCamelCase , pad_token_id=_UpperCamelCase , bos_token_id=_UpperCamelCase , eos_token_id=_UpperCamelCase ) UpperCAmelCase_ : Optional[int] = hidden_size UpperCAmelCase_ : Optional[int] = feat_extract_activation UpperCAmelCase_ : Optional[Any] = list(_UpperCamelCase ) UpperCAmelCase_ : int = list(_UpperCamelCase ) UpperCAmelCase_ : Optional[int] = list(_UpperCamelCase ) UpperCAmelCase_ : List[Any] = conv_bias UpperCAmelCase_ : int = num_conv_pos_embeddings UpperCAmelCase_ : int = num_conv_pos_embedding_groups UpperCAmelCase_ : List[str] = conv_pos_kernel_size UpperCAmelCase_ : List[Any] = len(self.conv_dim ) UpperCAmelCase_ : Union[str, Any] = num_hidden_layers UpperCAmelCase_ : Any = intermediate_size UpperCAmelCase_ : List[Any] = hidden_act UpperCAmelCase_ : int = num_attention_heads UpperCAmelCase_ : Optional[Any] = hidden_dropout UpperCAmelCase_ : Any = attention_dropout UpperCAmelCase_ : Dict = activation_dropout UpperCAmelCase_ : Optional[Any] = feat_proj_dropout UpperCAmelCase_ : Any = final_dropout UpperCAmelCase_ : Optional[Any] = layerdrop UpperCAmelCase_ : Dict = layer_norm_eps UpperCAmelCase_ : List[str] = initializer_range UpperCAmelCase_ : Union[str, Any] = vocab_size UpperCAmelCase_ : List[Any] = use_weighted_layer_sum if ( (len(self.conv_stride ) != self.num_feat_extract_layers) or (len(self.conv_kernel ) != self.num_feat_extract_layers) or (len(self.conv_dim ) != self.num_feat_extract_layers) ): raise ValueError( 'Configuration for convolutional layers is incorrect. It is required that `len(config.conv_dim)` ==' ' `len(config.conv_stride)` == `len(config.conv_kernel)`, but is `len(config.conv_dim) =' f" {len(self.conv_dim )}`, `len(config.conv_stride) = {len(self.conv_stride )}`," f" `len(config.conv_kernel) = {len(self.conv_kernel )}`." ) # fine-tuning config parameters for SpecAugment: https://arxiv.org/abs/1904.08779 UpperCAmelCase_ : Tuple = mask_time_prob UpperCAmelCase_ : int = mask_time_length UpperCAmelCase_ : Tuple = mask_time_min_masks UpperCAmelCase_ : Dict = mask_feature_prob UpperCAmelCase_ : List[str] = mask_feature_length UpperCAmelCase_ : Optional[Any] = mask_feature_min_masks # ctc loss UpperCAmelCase_ : Dict = ctc_loss_reduction UpperCAmelCase_ : Any = ctc_zero_infinity # adapter UpperCAmelCase_ : List[Any] = add_adapter UpperCAmelCase_ : Tuple = adapter_kernel_size UpperCAmelCase_ : Tuple = adapter_stride UpperCAmelCase_ : Any = num_adapter_layers UpperCAmelCase_ : List[str] = output_hidden_size or hidden_size # SequenceClassification-specific parameter. Feel free to ignore for other classes. UpperCAmelCase_ : int = classifier_proj_size # XVector-specific parameters. Feel free to ignore for other classes. UpperCAmelCase_ : Dict = list(_UpperCamelCase ) UpperCAmelCase_ : Optional[Any] = list(_UpperCamelCase ) UpperCAmelCase_ : Union[str, Any] = list(_UpperCamelCase ) UpperCAmelCase_ : int = xvector_output_dim @property def __UpperCAmelCase ( self ) -> str: return math.prod(self.conv_stride )
29
from collections import OrderedDict from typing import Mapping from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging A : Union[str, Any] = logging.get_logger(__name__) A : int = { '''xlm-roberta-base''': '''https://huggingface.co/xlm-roberta-base/resolve/main/config.json''', '''xlm-roberta-large''': '''https://huggingface.co/xlm-roberta-large/resolve/main/config.json''', '''xlm-roberta-large-finetuned-conll02-dutch''': ( '''https://huggingface.co/xlm-roberta-large-finetuned-conll02-dutch/resolve/main/config.json''' ), '''xlm-roberta-large-finetuned-conll02-spanish''': ( '''https://huggingface.co/xlm-roberta-large-finetuned-conll02-spanish/resolve/main/config.json''' ), '''xlm-roberta-large-finetuned-conll03-english''': ( '''https://huggingface.co/xlm-roberta-large-finetuned-conll03-english/resolve/main/config.json''' ), '''xlm-roberta-large-finetuned-conll03-german''': ( '''https://huggingface.co/xlm-roberta-large-finetuned-conll03-german/resolve/main/config.json''' ), } class A (SCREAMING_SNAKE_CASE ): '''simple docstring''' __lowerCamelCase : Any = '''xlm-roberta''' def __init__( self : Optional[Any] , __lowerCAmelCase : List[Any]=3_05_22 , __lowerCAmelCase : int=7_68 , __lowerCAmelCase : Tuple=12 , __lowerCAmelCase : str=12 , __lowerCAmelCase : Union[str, Any]=30_72 , __lowerCAmelCase : Union[str, Any]="gelu" , __lowerCAmelCase : Optional[int]=0.1 , __lowerCAmelCase : Dict=0.1 , __lowerCAmelCase : List[str]=5_12 , __lowerCAmelCase : Tuple=2 , __lowerCAmelCase : Dict=0.0_2 , __lowerCAmelCase : List[str]=1e-12 , __lowerCAmelCase : Union[str, Any]=1 , __lowerCAmelCase : str=0 , __lowerCAmelCase : Optional[int]=2 , __lowerCAmelCase : Tuple="absolute" , __lowerCAmelCase : Any=True , __lowerCAmelCase : Any=None , **__lowerCAmelCase : str , ) -> Optional[Any]: """simple docstring""" super().__init__(pad_token_id=__lowerCAmelCase , bos_token_id=__lowerCAmelCase , eos_token_id=__lowerCAmelCase , **__lowerCAmelCase ) A__ = vocab_size A__ = hidden_size A__ = num_hidden_layers A__ = num_attention_heads A__ = hidden_act A__ = intermediate_size A__ = hidden_dropout_prob A__ = attention_probs_dropout_prob A__ = max_position_embeddings A__ = type_vocab_size A__ = initializer_range A__ = layer_norm_eps A__ = position_embedding_type A__ = use_cache A__ = classifier_dropout class A (SCREAMING_SNAKE_CASE ): '''simple docstring''' @property def a_ ( self : int ) -> Mapping[str, Mapping[int, str]]: """simple docstring""" if self.task == "multiple-choice": A__ = {0: """batch""", 1: """choice""", 2: """sequence"""} else: A__ = {0: """batch""", 1: """sequence"""} return OrderedDict( [ ("""input_ids""", dynamic_axis), ("""attention_mask""", dynamic_axis), ] )
274
0
from io import BytesIO from typing import List, Union import requests from ..utils import add_end_docstrings, is_decord_available, is_torch_available, logging, requires_backends from .base import PIPELINE_INIT_ARGS, Pipeline if is_decord_available(): import numpy as np from decord import VideoReader if is_torch_available(): from ..models.auto.modeling_auto import MODEL_FOR_VIDEO_CLASSIFICATION_MAPPING lowerCAmelCase_ = logging.get_logger(__name__) @add_end_docstrings(UpperCAmelCase_ ) class _lowerCAmelCase ( UpperCAmelCase_ ): '''simple docstring''' def __init__( self : Optional[int] , *UpperCamelCase : str , **UpperCamelCase : Tuple ): '''simple docstring''' super().__init__(*UpperCamelCase , **UpperCamelCase ) requires_backends(self , 'decord' ) self.check_model_type(UpperCamelCase ) def UpperCamelCase_ ( self : Any , UpperCamelCase : Any=None , UpperCamelCase : Union[str, Any]=None , UpperCamelCase : Optional[int]=None ): '''simple docstring''' _snake_case : Optional[Any] = {} if frame_sampling_rate is not None: _snake_case : List[str] = frame_sampling_rate if num_frames is not None: _snake_case : int = num_frames _snake_case : Any = {} if top_k is not None: _snake_case : Tuple = top_k return preprocess_params, {}, postprocess_params def __call__( self : Optional[Any] , UpperCamelCase : Union[str, List[str]] , **UpperCamelCase : int ): '''simple docstring''' return super().__call__(UpperCamelCase , **UpperCamelCase ) def UpperCamelCase_ ( self : Optional[Any] , UpperCamelCase : List[Any] , UpperCamelCase : Optional[int]=None , UpperCamelCase : Any=1 ): '''simple docstring''' if num_frames is None: _snake_case : Union[str, Any] = self.model.config.num_frames if video.startswith('http://' ) or video.startswith('https://' ): _snake_case : Union[str, Any] = BytesIO(requests.get(UpperCamelCase ).content ) _snake_case : List[str] = VideoReader(UpperCamelCase ) videoreader.seek(0 ) _snake_case : str = 0 _snake_case : Dict = num_frames * frame_sampling_rate - 1 _snake_case : Optional[int] = np.linspace(UpperCamelCase , UpperCamelCase , num=UpperCamelCase , dtype=np.intaa ) _snake_case : int = videoreader.get_batch(UpperCamelCase ).asnumpy() _snake_case : Union[str, Any] = list(UpperCamelCase ) _snake_case : List[Any] = self.image_processor(UpperCamelCase , return_tensors=self.framework ) return model_inputs def UpperCamelCase_ ( self : Union[str, Any] , UpperCamelCase : List[str] ): '''simple docstring''' _snake_case : Optional[int] = self.model(**UpperCamelCase ) return model_outputs def UpperCamelCase_ ( self : List[str] , UpperCamelCase : Dict , UpperCamelCase : int=5 ): '''simple docstring''' if top_k > self.model.config.num_labels: _snake_case : str = self.model.config.num_labels if self.framework == "pt": _snake_case : Optional[int] = model_outputs.logits.softmax(-1 )[0] _snake_case , _snake_case : Any = probs.topk(UpperCamelCase ) else: raise ValueError(f"""Unsupported framework: {self.framework}""" ) _snake_case : int = scores.tolist() _snake_case : Optional[Any] = ids.tolist() return [{"score": score, "label": self.model.config.idalabel[_id]} for score, _id in zip(UpperCamelCase , UpperCamelCase )]
260
from ...configuration_utils import PretrainedConfig from ...utils import logging lowerCAmelCase_ = logging.get_logger(__name__) lowerCAmelCase_ = { """google/fnet-base""": """https://huggingface.co/google/fnet-base/resolve/main/config.json""", """google/fnet-large""": """https://huggingface.co/google/fnet-large/resolve/main/config.json""" # See all FNet models at https://huggingface.co/models?filter=fnet } class _lowerCAmelCase ( UpperCAmelCase_ ): '''simple docstring''' a_ : List[str] ="""fnet""" def __init__( self : Optional[Any] , UpperCamelCase : Union[str, Any]=3_20_00 , UpperCamelCase : Any=7_68 , UpperCamelCase : Tuple=12 , UpperCamelCase : Union[str, Any]=30_72 , UpperCamelCase : int="gelu_new" , UpperCamelCase : str=0.1 , UpperCamelCase : Optional[Any]=5_12 , UpperCamelCase : List[str]=4 , UpperCamelCase : Union[str, Any]=0.02 , UpperCamelCase : Union[str, Any]=1e-1_2 , UpperCamelCase : List[str]=False , UpperCamelCase : Optional[int]=5_12 , UpperCamelCase : Any=3 , UpperCamelCase : str=1 , UpperCamelCase : Optional[Any]=2 , **UpperCamelCase : List[Any] , ): '''simple docstring''' super().__init__(pad_token_id=UpperCamelCase , bos_token_id=UpperCamelCase , eos_token_id=UpperCamelCase , **UpperCamelCase ) _snake_case : Optional[int] = vocab_size _snake_case : int = max_position_embeddings _snake_case : Dict = hidden_size _snake_case : str = num_hidden_layers _snake_case : List[str] = intermediate_size _snake_case : Union[str, Any] = hidden_act _snake_case : Optional[Any] = hidden_dropout_prob _snake_case : List[Any] = initializer_range _snake_case : int = type_vocab_size _snake_case : Union[str, Any] = layer_norm_eps _snake_case : str = use_tpu_fourier_optimizations _snake_case : Tuple = tpu_short_seq_length
260
1
import argparse import json import os from collections import OrderedDict import torch from transformers import LukeConfig, LukeForMaskedLM, MLukeTokenizer, XLMRobertaTokenizer from transformers.tokenization_utils_base import AddedToken @torch.no_grad() def lowerCAmelCase__(__snake_case ,__snake_case ,__snake_case ,__snake_case ,__snake_case ) -> Union[str, Any]: '''simple docstring''' with open(a_ ) as metadata_file: lowerCamelCase__ = json.load(a_ ) lowerCamelCase__ = LukeConfig(use_entity_aware_attention=a_ ,**metadata['''model_config'''] ) # Load in the weights from the checkpoint_path lowerCamelCase__ = torch.load(a_ ,map_location='''cpu''' )['''module'''] # Load the entity vocab file lowerCamelCase__ = load_original_entity_vocab(a_ ) # add an entry for [MASK2] lowerCamelCase__ = max(entity_vocab.values() ) + 1 config.entity_vocab_size += 1 lowerCamelCase__ = XLMRobertaTokenizer.from_pretrained(metadata['''model_config''']['''bert_model_name'''] ) # Add special tokens to the token vocabulary for downstream tasks lowerCamelCase__ = AddedToken('''<ent>''' ,lstrip=a_ ,rstrip=a_ ) lowerCamelCase__ = AddedToken('''<ent2>''' ,lstrip=a_ ,rstrip=a_ ) tokenizer.add_special_tokens({'''additional_special_tokens''': [entity_token_a, entity_token_a]} ) config.vocab_size += 2 print(F'Saving tokenizer to {pytorch_dump_folder_path}' ) tokenizer.save_pretrained(a_ ) with open(os.path.join(a_ ,'''tokenizer_config.json''' ) ,'''r''' ) as f: lowerCamelCase__ = json.load(a_ ) lowerCamelCase__ = '''MLukeTokenizer''' with open(os.path.join(a_ ,'''tokenizer_config.json''' ) ,'''w''' ) as f: json.dump(a_ ,a_ ) with open(os.path.join(a_ ,MLukeTokenizer.vocab_files_names['''entity_vocab_file'''] ) ,'''w''' ) as f: json.dump(a_ ,a_ ) lowerCamelCase__ = MLukeTokenizer.from_pretrained(a_ ) # Initialize the embeddings of the special tokens lowerCamelCase__ = tokenizer.convert_tokens_to_ids(['''@'''] )[0] lowerCamelCase__ = tokenizer.convert_tokens_to_ids(['''#'''] )[0] lowerCamelCase__ = state_dict['''embeddings.word_embeddings.weight'''] lowerCamelCase__ = word_emb[ent_init_index].unsqueeze(0 ) lowerCamelCase__ = word_emb[enta_init_index].unsqueeze(0 ) lowerCamelCase__ = torch.cat([word_emb, ent_emb, enta_emb] ) # add special tokens for 'entity_predictions.bias' for bias_name in ["lm_head.decoder.bias", "lm_head.bias"]: lowerCamelCase__ = state_dict[bias_name] lowerCamelCase__ = decoder_bias[ent_init_index].unsqueeze(0 ) lowerCamelCase__ = decoder_bias[enta_init_index].unsqueeze(0 ) lowerCamelCase__ = torch.cat([decoder_bias, ent_decoder_bias, enta_decoder_bias] ) # Initialize the query layers of the entity-aware self-attention mechanism for layer_index in range(config.num_hidden_layers ): for matrix_name in ["query.weight", "query.bias"]: lowerCamelCase__ = F'encoder.layer.{layer_index}.attention.self.' lowerCamelCase__ = state_dict[prefix + matrix_name] lowerCamelCase__ = state_dict[prefix + matrix_name] lowerCamelCase__ = state_dict[prefix + matrix_name] # Initialize the embedding of the [MASK2] entity using that of the [MASK] entity for downstream tasks lowerCamelCase__ = state_dict['''entity_embeddings.entity_embeddings.weight'''] lowerCamelCase__ = entity_emb[entity_vocab['''[MASK]''']].unsqueeze(0 ) lowerCamelCase__ = torch.cat([entity_emb, entity_mask_emb] ) # add [MASK2] for 'entity_predictions.bias' lowerCamelCase__ = state_dict['''entity_predictions.bias'''] lowerCamelCase__ = entity_prediction_bias[entity_vocab['''[MASK]''']].unsqueeze(0 ) lowerCamelCase__ = torch.cat([entity_prediction_bias, entity_mask_bias] ) lowerCamelCase__ = LukeForMaskedLM(config=a_ ).eval() state_dict.pop('''entity_predictions.decoder.weight''' ) state_dict.pop('''lm_head.decoder.weight''' ) state_dict.pop('''lm_head.decoder.bias''' ) lowerCamelCase__ = OrderedDict() for key, value in state_dict.items(): if not (key.startswith('''lm_head''' ) or key.startswith('''entity_predictions''' )): lowerCamelCase__ = state_dict[key] else: lowerCamelCase__ = state_dict[key] lowerCamelCase__ , lowerCamelCase__ = model.load_state_dict(a_ ,strict=a_ ) if set(a_ ) != {"luke.embeddings.position_ids"}: raise ValueError(F'Unexpected unexpected_keys: {unexpected_keys}' ) if set(a_ ) != { "lm_head.decoder.weight", "lm_head.decoder.bias", "entity_predictions.decoder.weight", }: raise ValueError(F'Unexpected missing_keys: {missing_keys}' ) model.tie_weights() assert (model.luke.embeddings.word_embeddings.weight == model.lm_head.decoder.weight).all() assert (model.luke.entity_embeddings.entity_embeddings.weight == model.entity_predictions.decoder.weight).all() # Check outputs lowerCamelCase__ = MLukeTokenizer.from_pretrained(a_ ,task='''entity_classification''' ) lowerCamelCase__ = '''ISO 639-3 uses the code fas for the dialects spoken across Iran and アフガニスタン (Afghanistan).''' lowerCamelCase__ = (0, 9) lowerCamelCase__ = tokenizer(a_ ,entity_spans=[span] ,return_tensors='''pt''' ) lowerCamelCase__ = model(**a_ ) # Verify word hidden states if model_size == "large": raise NotImplementedError else: # base lowerCamelCase__ = torch.Size((1, 33, 768) ) lowerCamelCase__ = torch.tensor([[0.0_8_9_2, 0.0_5_9_6, -0.2_8_1_9], [0.0_1_3_4, 0.1_1_9_9, 0.0_5_7_3], [-0.0_1_6_9, 0.0_9_2_7, 0.0_6_4_4]] ) if not (outputs.last_hidden_state.shape == expected_shape): raise ValueError( F'Outputs.last_hidden_state.shape is {outputs.last_hidden_state.shape}, Expected shape is {expected_shape}' ) if not torch.allclose(outputs.last_hidden_state[0, :3, :3] ,a_ ,atol=1E-4 ): raise ValueError # Verify entity hidden states if model_size == "large": raise NotImplementedError else: # base lowerCamelCase__ = torch.Size((1, 1, 768) ) lowerCamelCase__ = torch.tensor([[-0.1_4_8_2, 0.0_6_0_9, 0.0_3_2_2]] ) if not (outputs.entity_last_hidden_state.shape == expected_shape): raise ValueError( F'Outputs.entity_last_hidden_state.shape is {outputs.entity_last_hidden_state.shape}, Expected shape is' F' {expected_shape}' ) if not torch.allclose(outputs.entity_last_hidden_state[0, :3, :3] ,a_ ,atol=1E-4 ): raise ValueError # Verify masked word/entity prediction lowerCamelCase__ = MLukeTokenizer.from_pretrained(a_ ) lowerCamelCase__ = '''Tokyo is the capital of <mask>.''' lowerCamelCase__ = (24, 30) lowerCamelCase__ = tokenizer(a_ ,entity_spans=[span] ,return_tensors='''pt''' ) lowerCamelCase__ = model(**a_ ) lowerCamelCase__ = encoding['''input_ids'''][0].tolist() lowerCamelCase__ = input_ids.index(tokenizer.convert_tokens_to_ids('''<mask>''' ) ) lowerCamelCase__ = outputs.logits[0][mask_position_id].argmax(dim=-1 ) assert "Japan" == tokenizer.decode(a_ ) lowerCamelCase__ = outputs.entity_logits[0][0].argmax().item() lowerCamelCase__ = [ entity for entity, entity_id in tokenizer.entity_vocab.items() if entity_id == predicted_entity_id ] assert [e for e in multilingual_predicted_entities if e.startswith('''en:''' )][0] == "en:Japan" # Finally, save our PyTorch model and tokenizer print('''Saving PyTorch model to {}'''.format(a_ ) ) model.save_pretrained(a_ ) def lowerCAmelCase__(__snake_case ) -> Union[str, Any]: '''simple docstring''' lowerCamelCase__ = ['''[MASK]''', '''[PAD]''', '''[UNK]'''] lowerCamelCase__ = [json.loads(a_ ) for line in open(a_ )] lowerCamelCase__ = {} for entry in data: lowerCamelCase__ = entry['''id'''] for entity_name, language in entry["entities"]: if entity_name in SPECIAL_TOKENS: lowerCamelCase__ = entity_id break lowerCamelCase__ = F'{language}:{entity_name}' lowerCamelCase__ = entity_id return new_mapping if __name__ == "__main__": _a = argparse.ArgumentParser() # Required parameters parser.add_argument("--checkpoint_path", type=str, help="Path to a pytorch_model.bin file.") parser.add_argument( "--metadata_path", default=None, type=str, help="Path to a metadata.json file, defining the configuration." ) parser.add_argument( "--entity_vocab_path", default=None, type=str, help="Path to an entity_vocab.tsv file, containing the entity vocabulary.", ) parser.add_argument( "--pytorch_dump_folder_path", default=None, type=str, help="Path to where to dump the output PyTorch model." ) parser.add_argument( "--model_size", default="base", type=str, choices=["base", "large"], help="Size of the model to be converted." ) _a = parser.parse_args() convert_luke_checkpoint( args.checkpoint_path, args.metadata_path, args.entity_vocab_path, args.pytorch_dump_folder_path, args.model_size, )
209
from typing import List, Optional, Union import torch from ...models import UNetaDConditionModel, VQModel from ...pipelines import DiffusionPipeline from ...pipelines.pipeline_utils import ImagePipelineOutput from ...schedulers import DDPMScheduler from ...utils import ( is_accelerate_available, is_accelerate_version, logging, randn_tensor, replace_example_docstring, ) lowercase = logging.get_logger(__name__) # pylint: disable=invalid-name lowercase = "\n Examples:\n ```py\n >>> import torch\n >>> import numpy as np\n\n >>> from diffusers import KandinskyV22PriorPipeline, KandinskyV22ControlnetPipeline\n >>> from transformers import pipeline\n >>> from diffusers.utils import load_image\n\n\n >>> def make_hint(image, depth_estimator):\n ... image = depth_estimator(image)[\"depth\"]\n ... image = np.array(image)\n ... image = image[:, :, None]\n ... image = np.concatenate([image, image, image], axis=2)\n ... detected_map = torch.from_numpy(image).float() / 255.0\n ... hint = detected_map.permute(2, 0, 1)\n ... return hint\n\n\n >>> depth_estimator = pipeline(\"depth-estimation\")\n\n >>> pipe_prior = KandinskyV22PriorPipeline.from_pretrained(\n ... \"kandinsky-community/kandinsky-2-2-prior\", torch_dtype=torch.float16\n ... )\n >>> pipe_prior = pipe_prior.to(\"cuda\")\n\n >>> pipe = KandinskyV22ControlnetPipeline.from_pretrained(\n ... \"kandinsky-community/kandinsky-2-2-controlnet-depth\", torch_dtype=torch.float16\n ... )\n >>> pipe = pipe.to(\"cuda\")\n\n\n >>> img = load_image(\n ... \"https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main\"\n ... \"/kandinsky/cat.png\"\n ... ).resize((768, 768))\n\n >>> hint = make_hint(img, depth_estimator).unsqueeze(0).half().to(\"cuda\")\n\n >>> prompt = \"A robot, 4k photo\"\n >>> negative_prior_prompt = \"lowres, text, error, cropped, worst quality, low quality, jpeg artifacts, ugly, duplicate, morbid, mutilated, out of frame, extra fingers, mutated hands, poorly drawn hands, poorly drawn face, mutation, deformed, blurry, dehydrated, bad anatomy, bad proportions, extra limbs, cloned face, disfigured, gross proportions, malformed limbs, missing arms, missing legs, extra arms, extra legs, fused fingers, too many fingers, long neck, username, watermark, signature\"\n\n >>> generator = torch.Generator(device=\"cuda\").manual_seed(43)\n\n >>> image_emb, zero_image_emb = pipe_prior(\n ... prompt=prompt, negative_prompt=negative_prior_prompt, generator=generator\n ... ).to_tuple()\n\n >>> images = pipe(\n ... image_embeds=image_emb,\n ... negative_image_embeds=zero_image_emb,\n ... hint=hint,\n ... num_inference_steps=50,\n ... generator=generator,\n ... height=768,\n ... width=768,\n ... ).images\n\n >>> images[0].save(\"robot_cat.png\")\n ```\n" def __UpperCAmelCase ( a_ , a_ , a_=8): snake_case_ = height // scale_factor**2 if height % scale_factor**2 != 0: new_height += 1 snake_case_ = width // scale_factor**2 if width % scale_factor**2 != 0: new_width += 1 return new_height * scale_factor, new_width * scale_factor class UpperCamelCase_ ( snake_case_ ): '''simple docstring''' def __init__( self , a , a , a , ) -> Tuple: super().__init__() self.register_modules( unet=a , scheduler=a , movq=a , ) snake_case_ = 2 ** (len(self.movq.config.block_out_channels ) - 1) def _UpperCamelCase ( self , a , a , a , a , a , a ) -> Any: if latents is None: snake_case_ = randn_tensor(a , generator=a , device=a , dtype=a ) else: if latents.shape != shape: raise ValueError(F'''Unexpected latents shape, got {latents.shape}, expected {shape}''' ) snake_case_ = latents.to(a ) snake_case_ = latents * scheduler.init_noise_sigma return latents def _UpperCamelCase ( self , a=0 ) -> str: if is_accelerate_available(): from accelerate import cpu_offload else: raise ImportError('Please install accelerate via `pip install accelerate`' ) snake_case_ = torch.device(F'''cuda:{gpu_id}''' ) snake_case_ = [ self.unet, self.movq, ] for cpu_offloaded_model in models: if cpu_offloaded_model is not None: cpu_offload(a , a ) def _UpperCamelCase ( self , a=0 ) -> List[str]: if is_accelerate_available() and is_accelerate_version('>=' , '0.17.0.dev0' ): from accelerate import cpu_offload_with_hook else: raise ImportError('`enable_model_cpu_offload` requires `accelerate v0.17.0` or higher.' ) snake_case_ = torch.device(F'''cuda:{gpu_id}''' ) if self.device.type != "cpu": self.to('cpu' , silence_dtype_warnings=a ) torch.cuda.empty_cache() # otherwise we don't see the memory savings (but they probably exist) snake_case_ = None for cpu_offloaded_model in [self.unet, self.movq]: snake_case_ , snake_case_ = cpu_offload_with_hook(a , a , prev_module_hook=a ) # We'll offload the last model manually. snake_case_ = hook @property # Copied from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion.StableDiffusionPipeline._execution_device def _UpperCamelCase ( self ) -> Any: if not hasattr(self.unet , '_hf_hook' ): return self.device for module in self.unet.modules(): if ( hasattr(a , '_hf_hook' ) and hasattr(module._hf_hook , 'execution_device' ) and module._hf_hook.execution_device is not None ): return torch.device(module._hf_hook.execution_device ) return self.device @torch.no_grad() @replace_example_docstring(a ) def __call__( self , a , a , a , a = 5_12 , a = 5_12 , a = 1_00 , a = 4.0 , a = 1 , a = None , a = None , a = "pil" , a = True , ) -> List[str]: snake_case_ = self._execution_device snake_case_ = guidance_scale > 1.0 if isinstance(a , a ): snake_case_ = torch.cat(a , dim=0 ) if isinstance(a , a ): snake_case_ = torch.cat(a , dim=0 ) if isinstance(a , a ): snake_case_ = torch.cat(a , dim=0 ) snake_case_ = image_embeds.shape[0] * num_images_per_prompt if do_classifier_free_guidance: snake_case_ = image_embeds.repeat_interleave(a , dim=0 ) snake_case_ = negative_image_embeds.repeat_interleave(a , dim=0 ) snake_case_ = hint.repeat_interleave(a , dim=0 ) snake_case_ = torch.cat([negative_image_embeds, image_embeds] , dim=0 ).to(dtype=self.unet.dtype , device=a ) snake_case_ = torch.cat([hint, hint] , dim=0 ).to(dtype=self.unet.dtype , device=a ) self.scheduler.set_timesteps(a , device=a ) snake_case_ = self.scheduler.timesteps snake_case_ = self.movq.config.latent_channels snake_case_ , snake_case_ = downscale_height_and_width(a , a , self.movq_scale_factor ) # create initial latent snake_case_ = self.prepare_latents( (batch_size, num_channels_latents, height, width) , image_embeds.dtype , a , a , a , self.scheduler , ) for i, t in enumerate(self.progress_bar(a ) ): # expand the latents if we are doing classifier free guidance snake_case_ = torch.cat([latents] * 2 ) if do_classifier_free_guidance else latents snake_case_ = {'image_embeds': image_embeds, 'hint': hint} snake_case_ = self.unet( sample=a , timestep=a , encoder_hidden_states=a , added_cond_kwargs=a , return_dict=a , )[0] if do_classifier_free_guidance: snake_case_ , snake_case_ = noise_pred.split(latents.shape[1] , dim=1 ) snake_case_ , snake_case_ = noise_pred.chunk(2 ) snake_case_ , snake_case_ = variance_pred.chunk(2 ) snake_case_ = noise_pred_uncond + guidance_scale * (noise_pred_text - noise_pred_uncond) snake_case_ = torch.cat([noise_pred, variance_pred_text] , dim=1 ) if not ( hasattr(self.scheduler.config , 'variance_type' ) and self.scheduler.config.variance_type in ["learned", "learned_range"] ): snake_case_ , snake_case_ = noise_pred.split(latents.shape[1] , dim=1 ) # compute the previous noisy sample x_t -> x_t-1 snake_case_ = self.scheduler.step( a , a , a , generator=a , )[0] # post-processing snake_case_ = self.movq.decode(a , force_not_quantize=a )['sample'] if output_type not in ["pt", "np", "pil"]: raise ValueError(F'''Only the output types `pt`, `pil` and `np` are supported not output_type={output_type}''' ) if output_type in ["np", "pil"]: snake_case_ = image * 0.5 + 0.5 snake_case_ = image.clamp(0 , 1 ) snake_case_ = image.cpu().permute(0 , 2 , 3 , 1 ).float().numpy() if output_type == "pil": snake_case_ = self.numpy_to_pil(a ) if not return_dict: return (image,) return ImagePipelineOutput(images=a )
178
0
'''simple docstring''' from cva import destroyAllWindows, imread, imshow, waitKey def __UpperCamelCase ( _UpperCAmelCase ): # getting number of pixels in the image __UpperCAmelCase , __UpperCAmelCase : Dict = img.shape[0], img.shape[1] # converting each pixel's color to its negative for i in range(_UpperCAmelCase ): for j in range(_UpperCAmelCase ): __UpperCAmelCase : Optional[int] = [255, 255, 255] - img[i][j] return img if __name__ == "__main__": # read original image lowerCAmelCase__ : Optional[Any] = imread("image_data/lena.jpg", 1) # convert to its negative lowerCAmelCase__ : Optional[Any] = convert_to_negative(img) # show result image imshow("negative of original image", img) waitKey(0) destroyAllWindows()
37
'''simple docstring''' from collections import UserDict from typing import List, Union from ..utils import ( add_end_docstrings, is_tf_available, is_torch_available, is_vision_available, logging, requires_backends, ) from .base import PIPELINE_INIT_ARGS, Pipeline if is_vision_available(): from PIL import Image from ..image_utils import load_image if is_torch_available(): from ..models.auto.modeling_auto import MODEL_FOR_ZERO_SHOT_IMAGE_CLASSIFICATION_MAPPING if is_tf_available(): from ..models.auto.modeling_tf_auto import TF_MODEL_FOR_ZERO_SHOT_IMAGE_CLASSIFICATION_MAPPING from ..tf_utils import stable_softmax lowerCAmelCase__ : Optional[int] = logging.get_logger(__name__) @add_end_docstrings(snake_case__ ) class SCREAMING_SNAKE_CASE__ ( snake_case__ ): """simple docstring""" def __init__( self : List[Any] , **UpperCAmelCase_ : Dict ): """simple docstring""" super().__init__(**UpperCAmelCase_ ) requires_backends(self , "vision" ) self.check_model_type( TF_MODEL_FOR_ZERO_SHOT_IMAGE_CLASSIFICATION_MAPPING if self.framework == "tf" else MODEL_FOR_ZERO_SHOT_IMAGE_CLASSIFICATION_MAPPING ) def __call__( self : List[str] , UpperCAmelCase_ : Union[str, List[str], "Image", List["Image"]] , **UpperCAmelCase_ : Tuple ): """simple docstring""" return super().__call__(UpperCAmelCase_ , **UpperCAmelCase_ ) def lowerCamelCase_ ( self : Union[str, Any] , **UpperCAmelCase_ : Optional[Any] ): """simple docstring""" __UpperCAmelCase : List[Any] = {} if "candidate_labels" in kwargs: __UpperCAmelCase : Union[str, Any] = kwargs["candidate_labels"] if "hypothesis_template" in kwargs: __UpperCAmelCase : int = kwargs["hypothesis_template"] return preprocess_params, {}, {} def lowerCamelCase_ ( self : Optional[int] , UpperCAmelCase_ : int , UpperCAmelCase_ : List[str]=None , UpperCAmelCase_ : Optional[Any]="This is a photo of {}." ): """simple docstring""" __UpperCAmelCase : Tuple = load_image(UpperCAmelCase_ ) __UpperCAmelCase : List[Any] = self.image_processor(images=[image] , return_tensors=self.framework ) __UpperCAmelCase : Dict = candidate_labels __UpperCAmelCase : Any = [hypothesis_template.format(UpperCAmelCase_ ) for x in candidate_labels] __UpperCAmelCase : Optional[int] = self.tokenizer(UpperCAmelCase_ , return_tensors=self.framework , padding=UpperCAmelCase_ ) __UpperCAmelCase : List[Any] = [text_inputs] return inputs def lowerCamelCase_ ( self : Tuple , UpperCAmelCase_ : Tuple ): """simple docstring""" __UpperCAmelCase : Union[str, Any] = model_inputs.pop("candidate_labels" ) __UpperCAmelCase : str = model_inputs.pop("text_inputs" ) if isinstance(text_inputs[0] , UpperCAmelCase_ ): __UpperCAmelCase : Tuple = text_inputs[0] else: # Batching case. __UpperCAmelCase : Optional[int] = text_inputs[0][0] __UpperCAmelCase : Any = self.model(**UpperCAmelCase_ , **UpperCAmelCase_ ) __UpperCAmelCase : Dict = { "candidate_labels": candidate_labels, "logits": outputs.logits_per_image, } return model_outputs def lowerCamelCase_ ( self : Optional[int] , UpperCAmelCase_ : Dict ): """simple docstring""" __UpperCAmelCase : Any = model_outputs.pop("candidate_labels" ) __UpperCAmelCase : Tuple = model_outputs["logits"][0] if self.framework == "pt": __UpperCAmelCase : Union[str, Any] = logits.softmax(dim=-1 ).squeeze(-1 ) __UpperCAmelCase : Dict = probs.tolist() if not isinstance(UpperCAmelCase_ , UpperCAmelCase_ ): __UpperCAmelCase : List[Any] = [scores] elif self.framework == "tf": __UpperCAmelCase : Union[str, Any] = stable_softmax(UpperCAmelCase_ , axis=-1 ) __UpperCAmelCase : List[str] = probs.numpy().tolist() else: raise ValueError(f"Unsupported framework: {self.framework}" ) __UpperCAmelCase : Dict = [ {"score": score, "label": candidate_label} for score, candidate_label in sorted(zip(UpperCAmelCase_ , UpperCAmelCase_ ) , key=lambda UpperCAmelCase_ : -x[0] ) ] return result
37
1
from math import ceil def lowerCamelCase_ ( UpperCamelCase__ : int = 1001 ) -> int: """simple docstring""" __lowerCamelCase = 1 for i in range(1 , int(ceil(n / 2.0 ) ) ): __lowerCamelCase = 2 * i + 1 __lowerCamelCase = 2 * i __lowerCamelCase = total + 4 * odd**2 - 6 * even return total if __name__ == "__main__": import sys if len(sys.argv) == 1: print(solution()) else: try: __A = int(sys.argv[1]) print(solution(n)) except ValueError: print("Invalid entry - please enter a number")
90
import argparse import dataclasses import json import logging import os import shutil from typing import List, Optional import datasets from accelerate import Accelerator from datasets import load_dataset from finetuning import finetune from tqdm.auto import tqdm import transformers from transformers import AutoConfig, set_seed from transformers.trainer_utils import IntervalStrategy __lowerCamelCase = logging.getLogger(__name__) __lowerCamelCase = """pytorch_model.bin""" @dataclasses.dataclass class UpperCAmelCase : A__ : str = dataclasses.field( metadata={"help": "Path to pretrained model or model identifier from huggingface.co/models."} ) A__ : Optional[str] = dataclasses.field( default=A_ ,metadata={"help": "Where do you want to store the pretrained models downloaded from huggingface.co."} ,) @dataclasses.dataclass class UpperCAmelCase : A__ : str = dataclasses.field(metadata={"help": "A csv or a json file containing the training data."} ) A__ : str = dataclasses.field(metadata={"help": "A csv or a json file containing the data to predict on."} ) A__ : Optional[str] = dataclasses.field( default=A_ ,metadata={"help": "A csv or a json file containing the validation data."} ) A__ : Optional[str] = dataclasses.field( default=A_ ,metadata={"help": "The name of the task to train on."} ,) A__ : Optional[List[str]] = dataclasses.field( default=A_ ,metadata={"help": "The list of labels for the task."} ) @dataclasses.dataclass class UpperCAmelCase : A__ : str = dataclasses.field( metadata={"help": "The output directory where the model predictions and checkpoints will be written."} ) A__ : Optional[str] = dataclasses.field( default="accuracy" ,metadata={"help": "The evaluation metric used for the task."} ) A__ : Optional[str] = dataclasses.field( default="no" ,metadata={ "help": "The evaluation strategy to adopt during training. Possible values are: [\"no\", \"step\", \"epoch]" } ,) A__ : Optional[int] = dataclasses.field( default=10 ,metadata={"help": "Number of evaluation calls with no improvement after which training will be stopped."} ,) A__ : Optional[float] = dataclasses.field( default=0.0 ,metadata={ "help": "How much the specified evaluation metric must improve to satisfy early stopping conditions." } ,) A__ : Optional[bool] = dataclasses.field( default=A_ ,metadata={"help": "Whether to filter the pseudo-labeled data based on the confidence score."} ,) A__ : Optional[bool] = dataclasses.field( default=A_ ,metadata={"help": "Whether to filter the pseudo-labeled data based on the validation performance."} ,) A__ : Optional[bool] = dataclasses.field( default=A_ ,metadata={"help": "Whether to fine-tune on labeled data after pseudo training."} ,) A__ : Optional[float] = dataclasses.field( default=0.0 ,metadata={"help": "Confidence threshold for pseudo-labeled data filtering."} ,) A__ : Optional[int] = dataclasses.field( default=1_00 ,metadata={"help": "Number of evaluation calls with no improvement after which training will be stopped."} ,) A__ : Optional[int] = dataclasses.field( default=A_ ,metadata={"help": "Random seed for initialization."} ,) def UpperCamelCase ( __lowerCamelCase : int , __lowerCamelCase : Tuple , __lowerCamelCase : Union[str, Any] , __lowerCamelCase : Tuple , __lowerCamelCase : Dict , __lowerCamelCase : Optional[int] ): snake_case : Tuple = datasets.concatenate_datasets([infer_input, infer_output] , axis=1 ) if args.do_filter_by_confidence: snake_case : Optional[int] = dataset.filter(lambda __lowerCamelCase : example["probability"] > args.confidence_threshold ) if args.do_filter_by_val_performance: assert eval_result >= 0.0 and eval_result <= 1.0 snake_case : int = int(eval_result * len(__lowerCamelCase ) ) print(__lowerCamelCase ) snake_case : List[str] = dataset.sort("probability" , reverse=__lowerCamelCase ) snake_case : Tuple = dataset.select(range(__lowerCamelCase ) ) snake_case : List[Any] = dataset.remove_columns(["label", "probability"] ) snake_case : Any = dataset.rename_column("prediction" , "label" ) snake_case : str = dataset.map(lambda __lowerCamelCase : {"label": idalabel[example["label"]]} ) snake_case : List[str] = dataset.shuffle(seed=args.seed ) snake_case : int = os.path.join(__lowerCamelCase , f"""train_pseudo.{args.data_file_extension}""" ) if args.data_file_extension == "csv": dataset.to_csv(__lowerCamelCase , index=__lowerCamelCase ) else: dataset.to_json(__lowerCamelCase ) def UpperCamelCase ( __lowerCamelCase : Optional[int] , __lowerCamelCase : str , __lowerCamelCase : Optional[int] , __lowerCamelCase : Tuple , **__lowerCamelCase : List[Any] ): snake_case : int = Accelerator() # Make one log on every process with the configuration for debugging. logging.basicConfig( format="%(asctime)s - %(levelname)s - %(name)s - %(message)s" , datefmt="%m/%d/%Y %H:%M:%S" , level=logging.INFO , ) logger.info(accelerator.state ) # Setup logging, we only want one process per machine to log things on the # screen. accelerator.is_local_main_process is only True for one process per # machine. logger.setLevel(logging.INFO if accelerator.is_local_main_process else logging.ERROR ) if accelerator.is_local_main_process: datasets.utils.logging.set_verbosity_warning() transformers.utils.logging.set_verbosity_info() else: datasets.utils.logging.set_verbosity_error() transformers.utils.logging.set_verbosity_error() snake_case : Dict = STModelArguments(model_name_or_path=__lowerCamelCase ) snake_case : Tuple = STDataArguments(train_file=__lowerCamelCase , infer_file=__lowerCamelCase ) snake_case : str = STTrainingArguments(output_dir=__lowerCamelCase ) snake_case : int = argparse.Namespace() for arg_class in (model_args, data_args, training_args): for key, value in vars(__lowerCamelCase ).items(): setattr(__lowerCamelCase , __lowerCamelCase , __lowerCamelCase ) for key, value in kwargs.items(): if hasattr(__lowerCamelCase , __lowerCamelCase ): setattr(__lowerCamelCase , __lowerCamelCase , __lowerCamelCase ) # Sanity checks snake_case : List[str] = {} snake_case : Optional[int] = None # You need to provide the training data and the data to predict on assert args.train_file is not None assert args.infer_file is not None snake_case : str = args.train_file snake_case : Tuple = args.infer_file if args.evaluation_strategy != IntervalStrategy.NO.value: assert args.eval_file is not None snake_case : Tuple = args.eval_file for key in data_files: snake_case : List[Any] = data_files[key].split("." )[-1] assert extension in ["csv", "json"], f"""`{key}_file` should be a csv or a json file.""" if args.data_file_extension is None: snake_case : Union[str, Any] = extension else: assert extension == args.data_file_extension, f"""`{key}_file` should be a {args.data_file_extension} file`.""" assert ( args.eval_metric in datasets.list_metrics() ), f"""{args.eval_metric} not in the list of supported metrics {datasets.list_metrics()}.""" # If passed along, set the training seed now. if args.seed is not None: set_seed(args.seed ) logger.info("Creating the initial data directory for self-training..." ) snake_case : List[Any] = f"""{args.output_dir}/self-train_iter-{{}}""".format snake_case : Optional[int] = data_dir_format(0 ) if accelerator.is_main_process: if args.output_dir is not None: os.makedirs(args.output_dir , exist_ok=__lowerCamelCase ) os.makedirs(__lowerCamelCase , exist_ok=__lowerCamelCase ) accelerator.wait_for_everyone() snake_case : Dict = None snake_case : Union[str, Any] = None snake_case : Tuple = 0 snake_case : List[Any] = False # Show the progress bar snake_case : List[Any] = tqdm(range(args.max_selftrain_iterations ) , disable=not accelerator.is_local_main_process ) # Self-train for iteration in range(0 , int(args.max_selftrain_iterations ) ): snake_case : str = data_dir_format(__lowerCamelCase ) assert os.path.exists(__lowerCamelCase ) # Stage 1: initial fine-tuning for iteration = 0 or pseudo-training for # iteration > 0 snake_case : Dict = os.path.join(__lowerCamelCase , "stage-1" ) snake_case : Optional[Any] = { "accelerator": accelerator, "model_name_or_path": args.model_name_or_path, "cache_dir": args.cache_dir, "do_train": True, "train_file": data_files["train"] if iteration == 0 else data_files["train_pseudo"], "do_eval": True if args.eval_file is not None else False, "eval_file": data_files["eval"], "do_predict": True, "infer_file": data_files["infer"], "task_name": args.task_name, "label_list": args.label_list, "output_dir": current_output_dir, "eval_metric": args.eval_metric, "evaluation_strategy": args.evaluation_strategy, "early_stopping_patience": args.early_stopping_patience, "early_stopping_threshold": args.early_stopping_threshold, "seed": args.seed, } # Add additional training arguments for key, value in kwargs.items(): if key not in arguments_dict and not hasattr(__lowerCamelCase , __lowerCamelCase ): arguments_dict.update({key: value} ) snake_case : int = os.path.join(__lowerCamelCase , "best-checkpoint" , __lowerCamelCase ) if os.path.exists(__lowerCamelCase ): logger.info( "Found existing model checkpoint at %s. Skipping self-training: iteration: %d, stage: 1." , __lowerCamelCase , __lowerCamelCase , ) else: logger.info("***** Running self-training: iteration: %d, stage: 1 *****" , __lowerCamelCase ) finetune(**__lowerCamelCase ) accelerator.wait_for_everyone() assert os.path.exists(__lowerCamelCase ) logger.info("Self-training job completed: iteration: %d, stage: 1." , __lowerCamelCase ) if iteration > 0 and args.finetune_on_labeled_data: # Stage 2 (optional): fine-tuning on the original labeled data snake_case : str = os.path.join(__lowerCamelCase , "best-checkpoint" ) snake_case : Dict = os.path.join(__lowerCamelCase , "stage-2" ) # Update arguments_dict snake_case : List[str] = model_path snake_case : Optional[Any] = data_files["train"] snake_case : Optional[Any] = current_output_dir snake_case : Union[str, Any] = os.path.join(__lowerCamelCase , "best-checkpoint" , __lowerCamelCase ) if os.path.exists(__lowerCamelCase ): logger.info( "Found existing model checkpoint at %s. Skipping self-training: iteration: %d, stage: 2." , __lowerCamelCase , __lowerCamelCase , ) else: logger.info("***** Running self-training: iteration: %d, stage: 2 *****" , __lowerCamelCase ) finetune(**__lowerCamelCase ) accelerator.wait_for_everyone() assert os.path.exists(__lowerCamelCase ) logger.info("Self-training job completed: iteration: %d, stage: 2." , __lowerCamelCase ) snake_case : int = iteration snake_case : Tuple = data_dir_format(iteration + 1 ) snake_case : Tuple = AutoConfig.from_pretrained(os.path.join(__lowerCamelCase , "best-checkpoint" ) ) snake_case : Optional[int] = config.idalabel snake_case : List[Any] = os.path.join(__lowerCamelCase , "eval_results_best-checkpoint.json" ) snake_case : Union[str, Any] = os.path.join(__lowerCamelCase , "test_results_best-checkpoint.json" ) assert os.path.exists(__lowerCamelCase ) with open(__lowerCamelCase , "r" ) as f: snake_case : Dict = float(json.load(__lowerCamelCase )[args.eval_metric] ) snake_case : Optional[int] = os.path.join(__lowerCamelCase , "infer_output_best-checkpoint.csv" ) assert os.path.exists(__lowerCamelCase ) # Loading the dataset from local csv or json files. snake_case : Optional[Any] = load_dataset(args.data_file_extension , data_files={"data": data_files["infer"]} )["data"] snake_case : Dict = load_dataset("csv" , data_files={"data": infer_output_file} )["data"] if accelerator.is_main_process: os.makedirs(__lowerCamelCase , exist_ok=__lowerCamelCase ) shutil.copy(__lowerCamelCase , os.path.join(__lowerCamelCase , f"""eval_results_iter-{iteration}.json""" ) ) if os.path.exists(__lowerCamelCase ): shutil.copy(__lowerCamelCase , os.path.join(__lowerCamelCase , f"""test_results_iter-{iteration}.json""" ) ) create_pseudo_labeled_data(__lowerCamelCase , __lowerCamelCase , __lowerCamelCase , __lowerCamelCase , __lowerCamelCase , __lowerCamelCase ) accelerator.wait_for_everyone() snake_case : str = os.path.join(__lowerCamelCase , f"""train_pseudo.{args.data_file_extension}""" ) if args.evaluation_strategy != IntervalStrategy.NO.value: snake_case : List[Any] = eval_result if best_iteration is None: snake_case : List[Any] = new_iteration snake_case : int = new_eval_result else: if new_eval_result - best_eval_result > args.early_stopping_threshold: snake_case : int = new_iteration snake_case : Union[str, Any] = new_eval_result snake_case : str = 0 else: if new_eval_result == best_eval_result: snake_case : Any = new_iteration snake_case : Union[str, Any] = new_eval_result early_stopping_patience_counter += 1 if early_stopping_patience_counter >= args.early_stopping_patience: snake_case : Tuple = True progress_bar.update(1 ) if should_training_stop: break if best_iteration is not None: # Save the best iteration logger.info("Best iteration: %d" , __lowerCamelCase ) logger.info("Best evaluation result: %s = %f" , args.eval_metric , __lowerCamelCase ) accelerator.wait_for_everyone() if accelerator.is_main_process: shutil.copy( os.path.join(__lowerCamelCase , f"""eval_results_iter-{iteration}.json""" ) , os.path.join(__lowerCamelCase , "eval_results_best-iteration.json" ) , ) else: # Assume that the last iteration is the best logger.info("Best iteration: %d" , args.max_selftrain_iterations - 1 ) logger.info("Best evaluation result: %s = %f" , args.eval_metric , __lowerCamelCase ) accelerator.wait_for_everyone() if accelerator.is_main_process: shutil.copy( os.path.join(__lowerCamelCase , f"""eval_results_iter-{args.max_selftrain_iterations - 1}.json""" ) , os.path.join(__lowerCamelCase , "eval_results_best-iteration.json" ) , )
59
0
import argparse import logging from collections import namedtuple import torch from model_bertabs import BertAbsSummarizer from models.model_builder import AbsSummarizer # The authors' implementation from transformers import BertTokenizer logging.basicConfig(level=logging.INFO) UpperCAmelCase__ = logging.getLogger(__name__) UpperCAmelCase__ = "Hello world! cécé herlolip" UpperCAmelCase__ = namedtuple( "BertAbsConfig", [ "temp_dir", "large", "use_bert_emb", "finetune_bert", "encoder", "share_emb", "max_pos", "enc_layers", "enc_hidden_size", "enc_heads", "enc_ff_size", "enc_dropout", "dec_layers", "dec_hidden_size", "dec_heads", "dec_ff_size", "dec_dropout", ], ) def _a ( a :Any , a :List[str] ) -> int: a = BertAbsConfig( temp_dir='''.''' , finetune_bert=a , large=a , share_emb=a , use_bert_emb=a , encoder='''bert''' , max_pos=512 , enc_layers=6 , enc_hidden_size=512 , enc_heads=8 , enc_ff_size=512 , enc_dropout=0.2 , dec_layers=6 , dec_hidden_size=768 , dec_heads=8 , dec_ff_size=2_048 , dec_dropout=0.2 , ) a = torch.load(a , lambda a , a : storage ) a = AbsSummarizer(a , torch.device('''cpu''' ) , a ) original.eval() a = BertAbsSummarizer(a , torch.device('''cpu''' ) ) new_model.eval() # ------------------- # Convert the weights # ------------------- logging.info('''convert the model''' ) new_model.bert.load_state_dict(original.bert.state_dict() ) new_model.decoder.load_state_dict(original.decoder.state_dict() ) new_model.generator.load_state_dict(original.generator.state_dict() ) # ---------------------------------- # Make sure the outpus are identical # ---------------------------------- logging.info('''Make sure that the models\' outputs are identical''' ) a = BertTokenizer.from_pretrained('''bert-base-uncased''' ) # prepare the model inputs a = tokenizer.encode('''This is sample éàalj\'-.''' ) encoder_input_ids.extend([tokenizer.pad_token_id] * (512 - len(a )) ) a = torch.tensor(a ).unsqueeze(0 ) a = tokenizer.encode('''This is sample 3 éàalj\'-.''' ) decoder_input_ids.extend([tokenizer.pad_token_id] * (512 - len(a )) ) a = torch.tensor(a ).unsqueeze(0 ) # failsafe to make sure the weights reset does not affect the # loaded weights. assert torch.max(torch.abs(original.generator[0].weight - new_model.generator[0].weight ) ) == 0 # forward pass a = encoder_input_ids a = decoder_input_ids a = a = None a = None a = a = None a = a = None a = None # The original model does not apply the geneator layer immediatly but rather in # the beam search (where it combines softmax + linear layer). Since we already # apply the softmax in our generation process we only apply the linear layer here. # We make sure that the outputs of the full stack are identical a = original(a , a , a , a , a , a , a )[0] a = original.generator(a ) a = new_model( a , a , a , a , a )[0] a = new_model.generator(a ) a = torch.max(torch.abs(output_converted_model - output_original_model ) ).item() print('''Maximum absolute difference beween weights: {:.2f}'''.format(a ) ) a = torch.max(torch.abs(output_converted_generator - output_original_generator ) ).item() print('''Maximum absolute difference beween weights: {:.2f}'''.format(a ) ) a = torch.allclose(a , a , atol=1e-3 ) if are_identical: logging.info('''all weights are equal up to 1e-3''' ) else: raise ValueError('''the weights are different. The new model is likely different from the original one.''' ) # The model has been saved with torch.save(model) and this is bound to the exact # directory structure. We save the state_dict instead. logging.info('''saving the model\'s state dictionary''' ) torch.save( new_model.state_dict() , '''./bertabs-finetuned-cnndm-extractive-abstractive-summarization/pytorch_model.bin''' ) if __name__ == "__main__": UpperCAmelCase__ = argparse.ArgumentParser() parser.add_argument( "--bertabs_checkpoint_path", default=None, type=str, required=True, help="Path the official PyTorch dump.", ) parser.add_argument( "--pytorch_dump_folder_path", default=None, type=str, required=True, help="Path to the output PyTorch model.", ) UpperCAmelCase__ = parser.parse_args() convert_bertabs_checkpoints( args.bertabs_checkpoint_path, args.pytorch_dump_folder_path, )
26
import math def _a ( a :int = 100 ) -> int: a = sum(i * i for i in range(1 , n + 1 ) ) a = int(math.pow(sum(range(1 , n + 1 ) ) , 2 ) ) return square_of_sum - sum_of_squares if __name__ == "__main__": print(f"""{solution() = }""")
26
1
"""simple docstring""" from collections import defaultdict from pathlib import Path import pandas as pd from rouge_cli import calculate_rouge_path from utils import calculate_rouge SCREAMING_SNAKE_CASE = [ "Prosecutor: \"No videos were used in the crash investigation\" German papers say they saw a cell phone video of the" " final seconds on board Flight 9525. The Germanwings co-pilot says he had a \"previous episode of severe" " depression\" German airline confirms it knew of Andreas Lubitz's depression years before he took control.", "The Palestinian Authority officially becomes the 123rd member of the International Criminal Court. The formal" " accession was marked with a ceremony at The Hague, in the Netherlands. The Palestinians signed the ICC's" " founding Rome Statute in January. Israel and the United States opposed the Palestinians' efforts to join the" " body.", "Amnesty International releases its annual report on the death penalty. The report catalogs the use of" " state-sanctioned killing as a punitive measure across the globe. At least 607 people were executed around the" " world in 2014, compared to 778 in 2013. The U.S. remains one of the worst offenders for imposing capital" " punishment.", ] SCREAMING_SNAKE_CASE = [ "Marseille prosecutor says \"so far no videos were used in the crash investigation\" despite media reports ." " Journalists at Bild and Paris Match are \"very confident\" the video clip is real, an editor says . Andreas Lubitz" " had informed his Lufthansa training school of an episode of severe depression, airline says .", "Membership gives the ICC jurisdiction over alleged crimes committed in Palestinian territories since last June ." " Israel and the United States opposed the move, which could open the door to war crimes investigations against" " Israelis .", "Amnesty's annual death penalty report catalogs encouraging signs, but setbacks in numbers of those sentenced to" " death . Organization claims that governments around the world are using the threat of terrorism to advance" " executions . The number of executions worldwide has gone down by almost 22% compared with 2013, but death" " sentences up by 28% .", ] def _SCREAMING_SNAKE_CASE ( ) -> List[str]: A__ = calculate_rouge(lowercase_ , lowercase_ , bootstrap_aggregation=lowercase_ , rouge_keys=["rouge2", "rougeL"] ) assert isinstance(lowercase_ , lowercase_ ) A__ = calculate_rouge(lowercase_ , lowercase_ , bootstrap_aggregation=lowercase_ , rouge_keys=["rouge2"] ) assert ( pd.DataFrame(no_aggregation["rouge2"] ).fmeasure.mean() == pd.DataFrame(no_aggregation_just_ra["rouge2"] ).fmeasure.mean() ) def _SCREAMING_SNAKE_CASE ( ) -> int: A__ = "rougeLsum" A__ = calculate_rouge(lowercase_ , lowercase_ , newline_sep=lowercase_ , rouge_keys=[k] )[k] A__ = calculate_rouge(lowercase_ , lowercase_ , newline_sep=lowercase_ , rouge_keys=[k] )[k] assert score > score_no_sep def _SCREAMING_SNAKE_CASE ( ) -> Any: A__ = ["rouge1", "rouge2", "rougeL"] A__ = calculate_rouge(lowercase_ , lowercase_ , newline_sep=lowercase_ , rouge_keys=lowercase_ ) A__ = calculate_rouge(lowercase_ , lowercase_ , newline_sep=lowercase_ , rouge_keys=lowercase_ ) assert score_sep == score_no_sep def _SCREAMING_SNAKE_CASE ( ) -> Any: A__ = [ "Her older sister, Margot Frank, died in 1945, a month earlier than previously thought.", "Marseille prosecutor says \"so far no videos were used in the crash investigation\" despite media reports .", ] A__ = [ "Margot Frank, died in 1945, a month earlier than previously thought.", "Prosecutor: \"No videos were used in the crash investigation\" German papers say they saw a cell phone video of" " the final seconds on board Flight 9525.", ] assert calculate_rouge(lowercase_ , lowercase_ , newline_sep=lowercase_ ) == calculate_rouge(lowercase_ , lowercase_ , newline_sep=lowercase_ ) def _SCREAMING_SNAKE_CASE ( ) -> Optional[int]: A__ = [ "\" \"a person who has such a video needs to immediately give it to the investigators,\" prosecutor says .<n> \"it is a very disturbing scene,\" editor-in-chief of bild online tells \"erin burnett: outfront\" " ] A__ = [ " Marseille prosecutor says \"so far no videos were used in the crash investigation\" despite media reports . Journalists at Bild and Paris Match are \"very confident\" the video clip is real, an editor says . Andreas Lubitz had informed his Lufthansa training school of an episode of severe depression, airline says ." ] A__ = calculate_rouge(lowercase_ , lowercase_ , rouge_keys=["rougeLsum"] , newline_sep=lowercase_ )["rougeLsum"] A__ = calculate_rouge(lowercase_ , lowercase_ , rouge_keys=["rougeLsum"] )["rougeLsum"] assert new_score > prev_score def _SCREAMING_SNAKE_CASE ( ) -> Optional[Any]: A__ = Path("examples/seq2seq/test_data/wmt_en_ro" ) A__ = calculate_rouge_path(data_dir.joinpath("test.source" ) , data_dir.joinpath("test.target" ) ) assert isinstance(lowercase_ , lowercase_ ) A__ = calculate_rouge_path( data_dir.joinpath("test.source" ) , data_dir.joinpath("test.target" ) , bootstrap_aggregation=lowercase_ ) assert isinstance(lowercase_ , lowercase_ )
247
"""simple docstring""" import logging import os from typing import Dict, List, Optional, Union import torch import torch.nn as nn from accelerate.utils.imports import ( is_abit_bnb_available, is_abit_bnb_available, is_bnb_available, ) from ..big_modeling import dispatch_model, init_empty_weights from .dataclasses import BnbQuantizationConfig from .modeling import ( find_tied_parameters, get_balanced_memory, infer_auto_device_map, load_checkpoint_in_model, offload_weight, set_module_tensor_to_device, ) if is_bnb_available(): import bitsandbytes as bnb from copy import deepcopy SCREAMING_SNAKE_CASE = logging.getLogger(__name__) def _SCREAMING_SNAKE_CASE ( lowercase_ , lowercase_ , lowercase_ = None , lowercase_ = None , lowercase_ = None , lowercase_ = None , lowercase_ = None , lowercase_ = False , ) -> List[Any]: A__ = bnb_quantization_config.load_in_abit A__ = bnb_quantization_config.load_in_abit if load_in_abit and not is_abit_bnb_available(): raise ImportError( "You have a version of `bitsandbytes` that is not compatible with 8bit quantization," " make sure you have the latest version of `bitsandbytes` installed." ) if load_in_abit and not is_abit_bnb_available(): raise ValueError( "You have a version of `bitsandbytes` that is not compatible with 4bit quantization," "make sure you have the latest version of `bitsandbytes` installed." ) A__ = [] # custom device map if isinstance(lowercase_ , lowercase_ ) and len(device_map.keys() ) > 1: A__ = [key for key, value in device_map.items() if value in ["disk", "cpu"]] # We keep some modules such as the lm_head in their original dtype for numerical stability reasons if bnb_quantization_config.skip_modules is None: A__ = get_keys_to_not_convert(lowercase_ ) # add cpu modules to skip modules only for 4-bit modules if load_in_abit: bnb_quantization_config.skip_modules.extend(lowercase_ ) A__ = bnb_quantization_config.skip_modules # We add the modules we want to keep in full precision if bnb_quantization_config.keep_in_fpaa_modules is None: A__ = [] A__ = bnb_quantization_config.keep_in_fpaa_modules modules_to_not_convert.extend(lowercase_ ) # compatibility with peft A__ = load_in_abit A__ = load_in_abit A__ = get_parameter_device(lowercase_ ) if model_device.type != "meta": # quantization of an already loaded model logger.warning( "It is not recommended to quantize a loaded model. " "The model should be instantiated under the `init_empty_weights` context manager." ) A__ = replace_with_bnb_layers(lowercase_ , lowercase_ , modules_to_not_convert=lowercase_ ) # convert param to the right dtype A__ = bnb_quantization_config.torch_dtype for name, param in model.state_dict().items(): if any(module_to_keep_in_fpaa in name for module_to_keep_in_fpaa in keep_in_fpaa_modules ): param.to(torch.floataa ) if param.dtype != torch.floataa: A__ = name.replace(".weight" , "" ).replace(".bias" , "" ) A__ = getattr(lowercase_ , lowercase_ , lowercase_ ) if param is not None: param.to(torch.floataa ) elif torch.is_floating_point(lowercase_ ): param.to(lowercase_ ) if model_device.type == "cuda": # move everything to cpu in the first place because we can't do quantization if the weights are already on cuda model.cuda(torch.cuda.current_device() ) torch.cuda.empty_cache() elif torch.cuda.is_available(): model.to(torch.cuda.current_device() ) else: raise RuntimeError("No GPU found. A GPU is needed for quantization." ) logger.info( f"""The model device type is {model_device.type}. However, cuda is needed for quantization.""" "We move the model to cuda." ) return model elif weights_location is None: raise RuntimeError( f"""`weights_location` needs to be the folder path containing the weights of the model, but we found {weights_location} """ ) else: with init_empty_weights(): A__ = replace_with_bnb_layers( lowercase_ , lowercase_ , modules_to_not_convert=lowercase_ ) A__ = get_quantized_model_device_map( lowercase_ , lowercase_ , lowercase_ , max_memory=lowercase_ , no_split_module_classes=lowercase_ , ) if offload_state_dict is None and device_map is not None and "disk" in device_map.values(): A__ = True A__ = any(x in list(device_map.values() ) for x in ["cpu", "disk"] ) load_checkpoint_in_model( lowercase_ , lowercase_ , lowercase_ , dtype=bnb_quantization_config.torch_dtype , offload_folder=lowercase_ , offload_state_dict=lowercase_ , keep_in_fpaa_modules=bnb_quantization_config.keep_in_fpaa_modules , offload_abit_bnb=load_in_abit and offload , ) return dispatch_model(lowercase_ , device_map=lowercase_ , offload_dir=lowercase_ ) def _SCREAMING_SNAKE_CASE ( lowercase_ , lowercase_ , lowercase_=None , lowercase_=None , lowercase_=None ) -> List[str]: if device_map is None: if torch.cuda.is_available(): A__ = {"": torch.cuda.current_device()} else: raise RuntimeError("No GPU found. A GPU is needed for quantization." ) logger.info("The device_map was not initialized." "Setting device_map to `{'':torch.cuda.current_device()}`." ) if isinstance(lowercase_ , lowercase_ ): if device_map not in ["auto", "balanced", "balanced_low_0", "sequential"]: raise ValueError( "If passing a string for `device_map`, please choose 'auto', 'balanced', 'balanced_low_0' or " "'sequential'." ) A__ = {} special_dtypes.update( { name: bnb_quantization_config.torch_dtype for name, _ in model.named_parameters() if any(m in name for m in bnb_quantization_config.skip_modules ) } ) special_dtypes.update( { name: torch.floataa for name, _ in model.named_parameters() if any(m in name for m in bnb_quantization_config.keep_in_fpaa_modules ) } ) A__ = {} A__ = special_dtypes A__ = no_split_module_classes A__ = bnb_quantization_config.target_dtype # get max_memory for each device. if device_map != "sequential": A__ = get_balanced_memory( lowercase_ , low_zero=(device_map == "balanced_low_0") , max_memory=lowercase_ , **lowercase_ , ) A__ = max_memory A__ = infer_auto_device_map(lowercase_ , **lowercase_ ) if isinstance(lowercase_ , lowercase_ ): # check if don't have any quantized module on the cpu A__ = bnb_quantization_config.skip_modules + bnb_quantization_config.keep_in_fpaa_modules A__ = { key: device_map[key] for key in device_map.keys() if key not in modules_not_to_convert } for device in ["cpu", "disk"]: if device in device_map_without_some_modules.values(): if bnb_quantization_config.load_in_abit: raise ValueError( "\n Some modules are dispatched on the CPU or the disk. Make sure you have enough GPU RAM to fit\n the quantized model. If you want to dispatch the model on the CPU or the disk while keeping\n these modules in `torch_dtype`, you need to pass a custom `device_map` to\n `load_and_quantize_model`. Check\n https://huggingface.co/docs/accelerate/main/en/usage_guides/quantization#offload-modules-to-cpu-and-disk\n for more details.\n " ) else: logger.info( "Some modules are are offloaded to the CPU or the disk. Note that these modules will be converted to 8-bit" ) del device_map_without_some_modules return device_map def _SCREAMING_SNAKE_CASE ( lowercase_ , lowercase_ , lowercase_=None , lowercase_=None ) -> Optional[int]: if modules_to_not_convert is None: A__ = [] A__, A__ = _replace_with_bnb_layers( lowercase_ , lowercase_ , lowercase_ , lowercase_ ) if not has_been_replaced: logger.warning( "You are loading your model in 8bit or 4bit but no linear modules were found in your model." " this can happen for some architectures such as gpt2 that uses Conv1D instead of Linear layers." " Please double check your model architecture, or submit an issue on github if you think this is" " a bug." ) return model def _SCREAMING_SNAKE_CASE ( lowercase_ , lowercase_ , lowercase_=None , lowercase_=None , ) -> Optional[int]: A__ = False for name, module in model.named_children(): if current_key_name is None: A__ = [] current_key_name.append(lowercase_ ) if isinstance(lowercase_ , nn.Linear ) and name not in modules_to_not_convert: # Check if the current key is not in the `modules_to_not_convert` A__ = ".".join(lowercase_ ) A__ = True for key in modules_to_not_convert: if ( (key in current_key_name_str) and (key + "." in current_key_name_str) ) or key == current_key_name_str: A__ = False break if proceed: # Load bnb module with empty weight and replace ``nn.Linear` module if bnb_quantization_config.load_in_abit: A__ = bnb.nn.LinearabitLt( module.in_features , module.out_features , module.bias is not None , has_fpaa_weights=lowercase_ , threshold=bnb_quantization_config.llm_inta_threshold , ) elif bnb_quantization_config.load_in_abit: A__ = bnb.nn.Linearabit( module.in_features , module.out_features , module.bias is not None , bnb_quantization_config.bnb_abit_compute_dtype , compress_statistics=bnb_quantization_config.bnb_abit_use_double_quant , quant_type=bnb_quantization_config.bnb_abit_quant_type , ) else: raise ValueError("load_in_8bit and load_in_4bit can't be both False" ) A__ = module.weight.data if module.bias is not None: A__ = module.bias.data bnb_module.requires_grad_(lowercase_ ) setattr(lowercase_ , lowercase_ , lowercase_ ) A__ = True if len(list(module.children() ) ) > 0: A__, A__ = _replace_with_bnb_layers( lowercase_ , lowercase_ , lowercase_ , lowercase_ ) A__ = has_been_replaced | _has_been_replaced # Remove the last key for recursion current_key_name.pop(-1 ) return model, has_been_replaced def _SCREAMING_SNAKE_CASE ( lowercase_ ) -> str: # Create a copy of the model with init_empty_weights(): A__ = deepcopy(lowercase_ ) # this has 0 cost since it is done inside `init_empty_weights` context manager` A__ = find_tied_parameters(lowercase_ ) # For compatibility with Accelerate < 0.18 if isinstance(lowercase_ , lowercase_ ): A__ = sum(list(tied_params.values() ) , [] ) + list(tied_params.keys() ) else: A__ = sum(lowercase_ , [] ) A__ = len(lowercase_ ) > 0 # Check if it is a base model A__ = False if hasattr(lowercase_ , "base_model_prefix" ): A__ = not hasattr(lowercase_ , model.base_model_prefix ) # Ignore this for base models (BertModel, GPT2Model, etc.) if (not has_tied_params) and is_base_model: return [] # otherwise they have an attached head A__ = list(model.named_children() ) A__ = [list_modules[-1][0]] # add last module together with tied weights A__ = set(lowercase_ ) - set(lowercase_ ) A__ = list(set(lowercase_ ) ) + list(lowercase_ ) # remove ".weight" from the keys A__ = [".weight", ".bias"] A__ = [] for name in list_untouched: for name_to_remove in names_to_remove: if name_to_remove in name: A__ = name.replace(lowercase_ , "" ) filtered_module_names.append(lowercase_ ) return filtered_module_names def _SCREAMING_SNAKE_CASE ( lowercase_ ) -> Dict: for m in model.modules(): if isinstance(lowercase_ , bnb.nn.Linearabit ): return True return False def _SCREAMING_SNAKE_CASE ( lowercase_ ) -> List[Any]: return next(parameter.parameters() ).device def _SCREAMING_SNAKE_CASE ( lowercase_ , lowercase_ , lowercase_ , lowercase_ , lowercase_ , lowercase_ , lowercase_ ) -> Union[str, Any]: # if it is not quantized, we quantize and offload the quantized weights and the SCB stats if fpaa_statistics is None: set_module_tensor_to_device(lowercase_ , lowercase_ , 0 , dtype=lowercase_ , value=lowercase_ ) A__ = param_name A__ = model if "." in tensor_name: A__ = tensor_name.split("." ) for split in splits[:-1]: A__ = getattr(lowercase_ , lowercase_ ) if new_module is None: raise ValueError(f"""{module} has no attribute {split}.""" ) A__ = new_module A__ = splits[-1] # offload weights A__ = False offload_weight(module._parameters[tensor_name] , lowercase_ , lowercase_ , index=lowercase_ ) if hasattr(module._parameters[tensor_name] , "SCB" ): offload_weight( module._parameters[tensor_name].SCB , param_name.replace("weight" , "SCB" ) , lowercase_ , index=lowercase_ , ) else: offload_weight(lowercase_ , lowercase_ , lowercase_ , index=lowercase_ ) offload_weight(lowercase_ , param_name.replace("weight" , "SCB" ) , lowercase_ , index=lowercase_ ) set_module_tensor_to_device(lowercase_ , lowercase_ , "meta" , dtype=lowercase_ , value=torch.empty(*param.size() ) )
247
1
"""simple docstring""" from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available a__ : List[Any] = { '''configuration_clipseg''': [ '''CLIPSEG_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''CLIPSegConfig''', '''CLIPSegTextConfig''', '''CLIPSegVisionConfig''', ], '''processing_clipseg''': ['''CLIPSegProcessor'''], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: a__ : int = [ '''CLIPSEG_PRETRAINED_MODEL_ARCHIVE_LIST''', '''CLIPSegModel''', '''CLIPSegPreTrainedModel''', '''CLIPSegTextModel''', '''CLIPSegVisionModel''', '''CLIPSegForImageSegmentation''', ] if TYPE_CHECKING: from .configuration_clipseg import ( CLIPSEG_PRETRAINED_CONFIG_ARCHIVE_MAP, CLIPSegConfig, CLIPSegTextConfig, CLIPSegVisionConfig, ) from .processing_clipseg import CLIPSegProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_clipseg import ( CLIPSEG_PRETRAINED_MODEL_ARCHIVE_LIST, CLIPSegForImageSegmentation, CLIPSegModel, CLIPSegPreTrainedModel, CLIPSegTextModel, CLIPSegVisionModel, ) else: import sys a__ : Union[str, Any] = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
195
"""simple docstring""" from typing import TYPE_CHECKING from ...utils import _LazyModule a__ : Optional[int] = {'''tokenization_byt5''': ['''ByT5Tokenizer''']} if TYPE_CHECKING: from .tokenization_byta import ByTaTokenizer else: import sys a__ : Any = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
195
1
"""simple docstring""" class SCREAMING_SNAKE_CASE_ : """simple docstring""" def __init__( self , lowerCAmelCase__): # we need a list not a string, so do something to change the type __SCREAMING_SNAKE_CASE = arr.split(""",""") def snake_case_ ( self): __SCREAMING_SNAKE_CASE = [int(self.array[0])] * len(self.array) __SCREAMING_SNAKE_CASE = [int(self.array[0])] * len(self.array) for i in range(1 , len(self.array)): __SCREAMING_SNAKE_CASE = max( int(self.array[i]) + sum_value[i - 1] , int(self.array[i])) __SCREAMING_SNAKE_CASE = max(sum_value[i] , rear[i - 1]) return rear[len(self.array) - 1] if __name__ == "__main__": __magic_name__ = input("please input some numbers:") __magic_name__ = SubArray(whole_array) __magic_name__ = array.solve_sub_array() print(("the results is:", re))
100
"""simple docstring""" from __future__ import annotations __magic_name__ = [-10, -5, 0, 5, 5.1, 11, 13, 21, 3, 4, -21, -10, -5, -1, 0] __magic_name__ = [-5, 0, 5, 5.1, 11, 13, 21, -1, 4, -1, -10, -5, -1, 0, -1] def _lowerCAmelCase ( UpperCamelCase_ ): __SCREAMING_SNAKE_CASE = [] __SCREAMING_SNAKE_CASE = len(UpperCamelCase_ ) for i in range(UpperCamelCase_ ): __SCREAMING_SNAKE_CASE = -1 for j in range(i + 1 , UpperCamelCase_ ): if arr[i] < arr[j]: __SCREAMING_SNAKE_CASE = arr[j] break result.append(UpperCamelCase_ ) return result def _lowerCAmelCase ( UpperCamelCase_ ): __SCREAMING_SNAKE_CASE = [] for i, outer in enumerate(UpperCamelCase_ ): __SCREAMING_SNAKE_CASE = -1 for inner in arr[i + 1 :]: if outer < inner: __SCREAMING_SNAKE_CASE = inner break result.append(UpperCamelCase_ ) return result def _lowerCAmelCase ( UpperCamelCase_ ): __SCREAMING_SNAKE_CASE = len(UpperCamelCase_ ) __SCREAMING_SNAKE_CASE = [] __SCREAMING_SNAKE_CASE = [-1] * arr_size for index in reversed(range(UpperCamelCase_ ) ): if stack: while stack[-1] <= arr[index]: stack.pop() if not stack: break if stack: __SCREAMING_SNAKE_CASE = stack[-1] stack.append(arr[index] ) return result if __name__ == "__main__": from doctest import testmod from timeit import timeit testmod() print(next_greatest_element_slow(arr)) print(next_greatest_element_fast(arr)) print(next_greatest_element(arr)) __magic_name__ = ( "from __main__ import arr, next_greatest_element_slow, " "next_greatest_element_fast, next_greatest_element" ) print( "next_greatest_element_slow():", timeit("next_greatest_element_slow(arr)", setup=setup), ) print( "next_greatest_element_fast():", timeit("next_greatest_element_fast(arr)", setup=setup), ) print( " next_greatest_element():", timeit("next_greatest_element(arr)", setup=setup), )
100
1
"""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 __SCREAMING_SNAKE_CASE : Dict = {'''configuration_mra''': ['''MRA_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''MraConfig''']} try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __SCREAMING_SNAKE_CASE : Optional[int] = [ '''MRA_PRETRAINED_MODEL_ARCHIVE_LIST''', '''MraForMaskedLM''', '''MraForMultipleChoice''', '''MraForQuestionAnswering''', '''MraForSequenceClassification''', '''MraForTokenClassification''', '''MraLayer''', '''MraModel''', '''MraPreTrainedModel''', ] if TYPE_CHECKING: from .configuration_mra import MRA_PRETRAINED_CONFIG_ARCHIVE_MAP, MraConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_mra import ( MRA_PRETRAINED_MODEL_ARCHIVE_LIST, MraForMaskedLM, MraForMultipleChoice, MraForQuestionAnswering, MraForSequenceClassification, MraForTokenClassification, MraLayer, MraModel, MraPreTrainedModel, ) else: import sys __SCREAMING_SNAKE_CASE : List[str] = _LazyModule(__name__, globals()['__file__'], _import_structure)
357
import os import pytest from attr import dataclass __SCREAMING_SNAKE_CASE : int = 'us-east-1' # defaults region @dataclass class lowercase_ : _lowerCamelCase = 42 _lowerCamelCase = 'arn:aws:iam::558105141721:role/sagemaker_execution_role' _lowerCamelCase = { 'task_name': 'mnli', 'per_device_train_batch_size': 16, 'per_device_eval_batch_size': 16, 'do_train': True, 'do_eval': True, 'do_predict': True, 'output_dir': '/opt/ml/model', 'overwrite_output_dir': True, 'max_steps': 500, 'save_steps': 5_500, } _lowerCamelCase = {**hyperparameters, 'max_steps': 1_000} @property def UpperCamelCase ( self ): if self.framework == "pytorch": return [ {"Name": "train_runtime", "Regex": r"train_runtime.*=\D*(.*?)$"}, {"Name": "eval_accuracy", "Regex": r"eval_accuracy.*=\D*(.*?)$"}, {"Name": "eval_loss", "Regex": r"eval_loss.*=\D*(.*?)$"}, ] else: return [ {"Name": "train_runtime", "Regex": r"train_runtime.*=\D*(.*?)$"}, {"Name": "eval_accuracy", "Regex": r"loss.*=\D*(.*?)]?$"}, {"Name": "eval_loss", "Regex": r"sparse_categorical_accuracy.*=\D*(.*?)]?$"}, ] @property def UpperCamelCase ( self ): return f"""{self.framework}-transfromers-test""" @property def UpperCamelCase ( self ): return f"""./tests/sagemaker/scripts/{self.framework}""" @property def UpperCamelCase ( self ): if self.framework == "pytorch": return "763104351884.dkr.ecr.us-east-1.amazonaws.com/huggingface-pytorch-training:1.7.1-transformers4.6.1-gpu-py36-cu110-ubuntu18.04" else: return "763104351884.dkr.ecr.us-east-1.amazonaws.com/huggingface-tensorflow-training:2.4.1-transformers4.6.1-gpu-py37-cu110-ubuntu18.04" @pytest.fixture(scope="class" ) def snake_case (__lowercase ) -> str: '''simple docstring''' _snake_case : List[str] = SageMakerTestEnvironment(framework=request.cls.framework )
284
0
"""simple docstring""" import os import re import warnings from shutil import copyfile from typing import List, Optional, Tuple from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import is_sentencepiece_available, logging if is_sentencepiece_available(): from .tokenization_ta import TaTokenizer else: A_ = None A_ = logging.get_logger(__name__) A_ = {'''vocab_file''': '''spiece.model''', '''tokenizer_file''': '''tokenizer.json'''} A_ = { '''vocab_file''': { '''t5-small''': '''https://huggingface.co/t5-small/resolve/main/spiece.model''', '''t5-base''': '''https://huggingface.co/t5-base/resolve/main/spiece.model''', '''t5-large''': '''https://huggingface.co/t5-large/resolve/main/spiece.model''', '''t5-3b''': '''https://huggingface.co/t5-3b/resolve/main/spiece.model''', '''t5-11b''': '''https://huggingface.co/t5-11b/resolve/main/spiece.model''', }, '''tokenizer_file''': { '''t5-small''': '''https://huggingface.co/t5-small/resolve/main/tokenizer.json''', '''t5-base''': '''https://huggingface.co/t5-base/resolve/main/tokenizer.json''', '''t5-large''': '''https://huggingface.co/t5-large/resolve/main/tokenizer.json''', '''t5-3b''': '''https://huggingface.co/t5-3b/resolve/main/tokenizer.json''', '''t5-11b''': '''https://huggingface.co/t5-11b/resolve/main/tokenizer.json''', }, } # TODO(PVP) - this should be removed in Transformers v5 A_ = { '''t5-small''': 5_12, '''t5-base''': 5_12, '''t5-large''': 5_12, '''t5-3b''': 5_12, '''t5-11b''': 5_12, } class lowercase( __a ): '''simple docstring''' lowercase__ = VOCAB_FILES_NAMES lowercase__ = PRETRAINED_VOCAB_FILES_MAP lowercase__ = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES lowercase__ = ["input_ids", "attention_mask"] lowercase__ = TaTokenizer lowercase__ = [] def __init__( self: List[Any], a_: Dict=None, a_: str=None, a_: Optional[Any]="</s>", a_: Optional[Any]="<unk>", a_: Any="<pad>", a_: Optional[int]=100, a_: Optional[Any]=None, **a_: Dict, ): '''simple docstring''' if extra_ids > 0 and additional_special_tokens is None: _snake_case : int = [f"<extra_id_{i}>" for i in range(a_ )] elif extra_ids > 0 and additional_special_tokens is not None: # Check that we have the right number of extra special tokens _snake_case : Optional[Any] = len(set(filter(lambda a_ : bool("""extra_id_""" in str(a_ ) ), a_ ) ) ) if extra_tokens != extra_ids: raise ValueError( f"Both extra_ids ({extra_ids}) and additional_special_tokens ({additional_special_tokens}) are" """ provided to T5Tokenizer. In this case the additional_special_tokens must include the extra_ids""" """ tokens""" ) super().__init__( a_, tokenizer_file=a_, eos_token=a_, unk_token=a_, pad_token=a_, extra_ids=a_, additional_special_tokens=a_, **a_, ) _snake_case : str = vocab_file _snake_case : Dict = False if not self.vocab_file else True _snake_case : Dict = extra_ids @staticmethod def UpperCamelCase_ ( a_: Union[str, Any], a_: List[Any], a_: Optional[int] ): '''simple docstring''' if pretrained_model_name_or_path in TaTokenizerFast.max_model_input_sizes: _snake_case : Union[str, Any] = TaTokenizerFast.max_model_input_sizes[pretrained_model_name_or_path] if init_max_model_length is not None and init_max_model_length != max_model_length: return init_max_model_length elif init_max_model_length is None: warnings.warn( """This tokenizer was incorrectly instantiated with a model max length of""" f" {deprecated_max_model_length} which will be corrected in Transformers v5.\nFor now, this" """ behavior is kept to avoid breaking backwards compatibility when padding/encoding with""" """ `truncation is True`.\n- Be aware that you SHOULD NOT rely on""" f" {pretrained_model_name_or_path} automatically truncating your input to" f" {deprecated_max_model_length} when padding/encoding.\n- If you want to encode/pad to sequences" f" longer than {deprecated_max_model_length} you can either instantiate this tokenizer with" """ `model_max_length` or pass `max_length` when encoding/padding.\n- To avoid this warning, please""" """ instantiate this tokenizer with `model_max_length` set to your preferred value.""", a_, ) return max_model_length def UpperCamelCase_ ( self: Dict, a_: str, a_: Optional[str] = None ): '''simple docstring''' if not self.can_save_slow_tokenizer: raise ValueError( """Your fast tokenizer does not have the necessary information to save the vocabulary for a slow """ """tokenizer.""" ) if not os.path.isdir(a_ ): logger.error(f"Vocabulary path ({save_directory}) should be a directory" ) return _snake_case : Optional[Any] = os.path.join( a_, (filename_prefix + """-""" if filename_prefix else """""") + VOCAB_FILES_NAMES["""vocab_file"""] ) if os.path.abspath(self.vocab_file ) != os.path.abspath(a_ ): copyfile(self.vocab_file, a_ ) logger.info(f"Copy vocab file to {out_vocab_file}" ) return (out_vocab_file,) def UpperCamelCase_ ( self: int, a_: List[int], a_: Optional[List[int]] = None ): '''simple docstring''' _snake_case : str = token_ids_a + [self.eos_token_id] if token_ids_a is None: return self.prefix_tokens + token_ids_a else: _snake_case : str = token_ids_a + [self.eos_token_id] return self.prefix_tokens + token_ids_a + token_ids_a def UpperCamelCase_ ( self: Tuple, a_: List[int], a_: Optional[List[int]] = None ): '''simple docstring''' _snake_case : List[Any] = [self.eos_token_id] if token_ids_a is None: return len(token_ids_a + eos ) * [0] return len(token_ids_a + eos + token_ids_a + eos ) * [0] def UpperCamelCase_ ( self: int ): '''simple docstring''' return list( set(filter(lambda a_ : bool(re.search(r"""<extra_id_\d+>""", a_ ) ) is not None, self.additional_special_tokens ) ) ) def UpperCamelCase_ ( self: Union[str, Any] ): '''simple docstring''' return [self.convert_tokens_to_ids(a_ ) for token in self.get_sentinel_tokens()]
64
"""simple docstring""" import unittest import numpy as np from diffusers import OnnxStableDiffusionInpaintPipelineLegacy from diffusers.utils.testing_utils import ( is_onnx_available, load_image, load_numpy, nightly, require_onnxruntime, require_torch_gpu, ) if is_onnx_available(): import onnxruntime as ort @nightly @require_onnxruntime @require_torch_gpu class lowercase( unittest.TestCase ): '''simple docstring''' @property def UpperCamelCase_ ( self: Optional[Any] ): '''simple docstring''' return ( "CUDAExecutionProvider", { "gpu_mem_limit": "15000000000", # 15GB "arena_extend_strategy": "kSameAsRequested", }, ) @property def UpperCamelCase_ ( self: int ): '''simple docstring''' _snake_case : Any = ort.SessionOptions() _snake_case : Union[str, Any] = False return options def UpperCamelCase_ ( self: List[Any] ): '''simple docstring''' _snake_case : Any = load_image( """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main""" """/in_paint/overture-creations-5sI6fQgYIuo.png""" ) _snake_case : Union[str, Any] = load_image( """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main""" """/in_paint/overture-creations-5sI6fQgYIuo_mask.png""" ) _snake_case : Union[str, Any] = load_numpy( """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main""" """/in_paint/red_cat_sitting_on_a_park_bench_onnx.npy""" ) # using the PNDM scheduler by default _snake_case : Optional[Any] = OnnxStableDiffusionInpaintPipelineLegacy.from_pretrained( """CompVis/stable-diffusion-v1-4""", revision="""onnx""", safety_checker=a_, feature_extractor=a_, provider=self.gpu_provider, sess_options=self.gpu_options, ) pipe.set_progress_bar_config(disable=a_ ) _snake_case : Optional[Any] = """A red cat sitting on a park bench""" _snake_case : Optional[int] = np.random.RandomState(0 ) _snake_case : Any = pipe( prompt=a_, image=a_, mask_image=a_, strength=0.75, guidance_scale=7.5, num_inference_steps=15, generator=a_, output_type="""np""", ) _snake_case : Dict = output.images[0] assert image.shape == (512, 512, 3) assert np.abs(expected_image - image ).max() < 1E-2
64
1
'''simple docstring''' from dataclasses import dataclass from typing import Optional, Tuple, Union import torch import torch.nn as nn from ..configuration_utils import ConfigMixin, register_to_config from ..utils import BaseOutput, apply_forward_hook from .modeling_utils import ModelMixin from .vae import Decoder, DecoderOutput, Encoder, VectorQuantizer @dataclass class UpperCAmelCase ( SCREAMING_SNAKE_CASE__): _lowerCamelCase : torch.FloatTensor class UpperCAmelCase ( SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__): @register_to_config def __init__( self : List[Any], a_ : int = 3, a_ : int = 3, a_ : Tuple[str] = ("DownEncoderBlock2D",), a_ : Tuple[str] = ("UpDecoderBlock2D",), a_ : Tuple[int] = (64,), a_ : int = 1, a_ : str = "silu", a_ : int = 3, a_ : int = 32, a_ : int = 256, a_ : int = 32, a_ : Optional[int] = None, a_ : float = 0.18_215, a_ : str = "group", ): """simple docstring""" super().__init__() # pass init params to Encoder UpperCamelCase__ = Encoder( in_channels=a_, out_channels=a_, down_block_types=a_, block_out_channels=a_, layers_per_block=a_, act_fn=a_, norm_num_groups=a_, double_z=a_, ) UpperCamelCase__ = vq_embed_dim if vq_embed_dim is not None else latent_channels UpperCamelCase__ = nn.Convad(a_, a_, 1 ) UpperCamelCase__ = VectorQuantizer(a_, a_, beta=0.25, remap=a_, sane_index_shape=a_ ) UpperCamelCase__ = nn.Convad(a_, a_, 1 ) # pass init params to Decoder UpperCamelCase__ = Decoder( in_channels=a_, out_channels=a_, up_block_types=a_, block_out_channels=a_, layers_per_block=a_, act_fn=a_, norm_num_groups=a_, norm_type=a_, ) @apply_forward_hook def lowercase_ ( self : List[Any], a_ : torch.FloatTensor, a_ : bool = True ): """simple docstring""" UpperCamelCase__ = self.encoder(a_ ) UpperCamelCase__ = self.quant_conv(a_ ) if not return_dict: return (h,) return VQEncoderOutput(latents=a_ ) @apply_forward_hook def lowercase_ ( self : int, a_ : torch.FloatTensor, a_ : bool = False, a_ : bool = True ): """simple docstring""" if not force_not_quantize: UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ = self.quantize(a_ ) else: UpperCamelCase__ = h UpperCamelCase__ = self.post_quant_conv(a_ ) UpperCamelCase__ = self.decoder(a_, quant if self.config.norm_type == "spatial" else None ) if not return_dict: return (dec,) return DecoderOutput(sample=a_ ) def lowercase_ ( self : List[str], a_ : torch.FloatTensor, a_ : bool = True ): """simple docstring""" UpperCamelCase__ = sample UpperCamelCase__ = self.encode(a_ ).latents UpperCamelCase__ = self.decode(a_ ).sample if not return_dict: return (dec,) return DecoderOutput(sample=a_ )
31
'''simple docstring''' from __future__ import annotations def SCREAMING_SNAKE_CASE__( _UpperCamelCase : int | float | str , _UpperCamelCase : int | float | str ) -> list[str]: '''simple docstring''' if nth_term == "": return [""] UpperCamelCase__ = int(_UpperCamelCase ) UpperCamelCase__ = int(_UpperCamelCase ) UpperCamelCase__ = [] for temp in range(int(_UpperCamelCase ) ): series.append(F'1 / {pow(temp + 1 , int(_UpperCamelCase ) )}' if series else "1" ) return series if __name__ == "__main__": import doctest doctest.testmod() __lowercase: Dict = int(input("Enter the last number (nth term) of the P-Series")) __lowercase: Optional[int] = int(input("Enter the power for P-Series")) print("Formula of P-Series => 1+1/2^p+1/3^p ..... 1/n^p") print(p_series(nth_term, power))
31
1
"""simple docstring""" from math import sqrt def a__ ( SCREAMING_SNAKE_CASE : int ): '''simple docstring''' assert isinstance(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ) and ( number >= 0 ), "'number' must been an int and positive" lowerCAmelCase : Any = True # 0 and 1 are none primes. if number <= 1: lowerCAmelCase : Any = False for divisor in range(2 , int(round(sqrt(SCREAMING_SNAKE_CASE ) ) ) + 1 ): # if 'number' divisible by 'divisor' then sets 'status' # of false and break up the loop. if number % divisor == 0: lowerCAmelCase : Optional[Any] = False break # precondition assert isinstance(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ), "'status' must been from type bool" return status def a__ ( SCREAMING_SNAKE_CASE : str ): '''simple docstring''' assert isinstance(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ) and (n > 2), "'N' must been an int and > 2" # beginList: contains all natural numbers from 2 up to N lowerCAmelCase : Tuple = list(range(2 , n + 1 ) ) lowerCAmelCase : List[Any] = [] # this list will be returns. # actual sieve of erathostenes for i in range(len(SCREAMING_SNAKE_CASE ) ): for j in range(i + 1 , len(SCREAMING_SNAKE_CASE ) ): if (begin_list[i] != 0) and (begin_list[j] % begin_list[i] == 0): lowerCAmelCase : Tuple = 0 # filters actual prime numbers. lowerCAmelCase : str = [x for x in begin_list if x != 0] # precondition assert isinstance(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ), "'ans' must been from type list" return ans def a__ ( SCREAMING_SNAKE_CASE : Any ): '''simple docstring''' assert isinstance(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ) and (n > 2), "'N' must been an int and > 2" lowerCAmelCase : str = [] # iterates over all numbers between 2 up to N+1 # if a number is prime then appends to list 'ans' for number in range(2 , n + 1 ): if is_prime(SCREAMING_SNAKE_CASE ): ans.append(SCREAMING_SNAKE_CASE ) # precondition assert isinstance(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ), "'ans' must been from type list" return ans def a__ ( SCREAMING_SNAKE_CASE : Optional[Any] ): '''simple docstring''' assert isinstance(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ) and number >= 0, "'number' must been an int and >= 0" lowerCAmelCase : Optional[Any] = [] # this list will be returns of the function. # potential prime number factors. lowerCAmelCase : Optional[Any] = 2 lowerCAmelCase : Optional[Any] = number if number == 0 or number == 1: ans.append(SCREAMING_SNAKE_CASE ) # if 'number' not prime then builds the prime factorization of 'number' elif not is_prime(SCREAMING_SNAKE_CASE ): while quotient != 1: if is_prime(SCREAMING_SNAKE_CASE ) and (quotient % factor == 0): ans.append(SCREAMING_SNAKE_CASE ) quotient /= factor else: factor += 1 else: ans.append(SCREAMING_SNAKE_CASE ) # precondition assert isinstance(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ), "'ans' must been from type list" return ans def a__ ( SCREAMING_SNAKE_CASE : str ): '''simple docstring''' assert isinstance(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ) and ( number >= 0 ), "'number' bust been an int and >= 0" lowerCAmelCase : List[Any] = 0 # prime factorization of 'number' lowerCAmelCase : Union[str, Any] = prime_factorization(SCREAMING_SNAKE_CASE ) lowerCAmelCase : Any = max(SCREAMING_SNAKE_CASE ) # precondition assert isinstance(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ), "'ans' must been from type int" return ans def a__ ( SCREAMING_SNAKE_CASE : List[Any] ): '''simple docstring''' assert isinstance(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ) and ( number >= 0 ), "'number' bust been an int and >= 0" lowerCAmelCase : Any = 0 # prime factorization of 'number' lowerCAmelCase : str = prime_factorization(SCREAMING_SNAKE_CASE ) lowerCAmelCase : List[Any] = min(SCREAMING_SNAKE_CASE ) # precondition assert isinstance(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ), "'ans' must been from type int" return ans def a__ ( SCREAMING_SNAKE_CASE : Dict ): '''simple docstring''' assert isinstance(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ), "'number' must been an int" assert isinstance(number % 2 == 0 , SCREAMING_SNAKE_CASE ), "compare bust been from type bool" return number % 2 == 0 def a__ ( SCREAMING_SNAKE_CASE : int ): '''simple docstring''' assert isinstance(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ), "'number' must been an int" assert isinstance(number % 2 != 0 , SCREAMING_SNAKE_CASE ), "compare bust been from type bool" return number % 2 != 0 def a__ ( SCREAMING_SNAKE_CASE : Optional[int] ): '''simple docstring''' assert ( isinstance(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ) and (number > 2) and is_even(SCREAMING_SNAKE_CASE ) ), "'number' must been an int, even and > 2" lowerCAmelCase : str = [] # this list will returned # creates a list of prime numbers between 2 up to 'number' lowerCAmelCase : Any = get_prime_numbers(SCREAMING_SNAKE_CASE ) lowerCAmelCase : Optional[int] = len(SCREAMING_SNAKE_CASE ) # run variable for while-loops. lowerCAmelCase : Dict = 0 lowerCAmelCase : Optional[int] = None # exit variable. for break up the loops lowerCAmelCase : Optional[int] = True while i < len_pn and loop: lowerCAmelCase : int = i + 1 while j < len_pn and loop: if prime_numbers[i] + prime_numbers[j] == number: lowerCAmelCase : Dict = False ans.append(prime_numbers[i] ) ans.append(prime_numbers[j] ) j += 1 i += 1 # precondition assert ( isinstance(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ) and (len(SCREAMING_SNAKE_CASE ) == 2) and (ans[0] + ans[1] == number) and is_prime(ans[0] ) and is_prime(ans[1] ) ), "'ans' must contains two primes. And sum of elements must been eq 'number'" return ans def a__ ( SCREAMING_SNAKE_CASE : Tuple , SCREAMING_SNAKE_CASE : Any ): '''simple docstring''' assert ( isinstance(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ) and isinstance(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ) and (numbera >= 0) and (numbera >= 0) ), "'number1' and 'number2' must been positive integer." lowerCAmelCase : int = 0 while numbera != 0: lowerCAmelCase : Dict = numbera % numbera lowerCAmelCase : int = numbera lowerCAmelCase : Optional[Any] = rest # precondition assert isinstance(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ) and ( numbera >= 0 ), "'number' must been from type int and positive" return numbera def a__ ( SCREAMING_SNAKE_CASE : Optional[int] , SCREAMING_SNAKE_CASE : List[str] ): '''simple docstring''' assert ( isinstance(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ) and isinstance(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ) and (numbera >= 1) and (numbera >= 1) ), "'number1' and 'number2' must been positive integer." lowerCAmelCase : List[str] = 1 # actual answer that will be return. # for kgV (x,1) if numbera > 1 and numbera > 1: # builds the prime factorization of 'number1' and 'number2' lowerCAmelCase : Dict = prime_factorization(SCREAMING_SNAKE_CASE ) lowerCAmelCase : Optional[int] = prime_factorization(SCREAMING_SNAKE_CASE ) elif numbera == 1 or numbera == 1: lowerCAmelCase : Any = [] lowerCAmelCase : Tuple = [] lowerCAmelCase : List[str] = max(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ) lowerCAmelCase : Optional[int] = 0 lowerCAmelCase : Optional[Any] = 0 lowerCAmelCase : Union[str, Any] = [] # captured numbers int both 'primeFac1' and 'primeFac2' # iterates through primeFac1 for n in prime_fac_a: if n not in done: if n in prime_fac_a: lowerCAmelCase : Any = prime_fac_a.count(SCREAMING_SNAKE_CASE ) lowerCAmelCase : int = prime_fac_a.count(SCREAMING_SNAKE_CASE ) for _ in range(max(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ) ): ans *= n else: lowerCAmelCase : List[str] = prime_fac_a.count(SCREAMING_SNAKE_CASE ) for _ in range(SCREAMING_SNAKE_CASE ): ans *= n done.append(SCREAMING_SNAKE_CASE ) # iterates through primeFac2 for n in prime_fac_a: if n not in done: lowerCAmelCase : Any = prime_fac_a.count(SCREAMING_SNAKE_CASE ) for _ in range(SCREAMING_SNAKE_CASE ): ans *= n done.append(SCREAMING_SNAKE_CASE ) # precondition assert isinstance(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ) and ( ans >= 0 ), "'ans' must been from type int and positive" return ans def a__ ( SCREAMING_SNAKE_CASE : Any ): '''simple docstring''' assert isinstance(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ) and (n >= 0), "'number' must been a positive int" lowerCAmelCase : int = 0 lowerCAmelCase : Tuple = 2 # this variable holds the answer while index < n: index += 1 ans += 1 # counts to the next number # if ans not prime then # runs to the next prime number. while not is_prime(SCREAMING_SNAKE_CASE ): ans += 1 # precondition assert isinstance(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ) and is_prime( SCREAMING_SNAKE_CASE ), "'ans' must been a prime number and from type int" return ans def a__ ( SCREAMING_SNAKE_CASE : List[Any] , SCREAMING_SNAKE_CASE : Optional[Any] ): '''simple docstring''' assert ( is_prime(SCREAMING_SNAKE_CASE ) and is_prime(SCREAMING_SNAKE_CASE ) and (p_number_a < p_number_a) ), "The arguments must been prime numbers and 'pNumber1' < 'pNumber2'" lowerCAmelCase : List[Any] = p_number_a + 1 # jump to the next number lowerCAmelCase : Tuple = [] # this list will be returns. # if number is not prime then # fetch the next prime number. while not is_prime(SCREAMING_SNAKE_CASE ): number += 1 while number < p_number_a: ans.append(SCREAMING_SNAKE_CASE ) number += 1 # fetch the next prime number. while not is_prime(SCREAMING_SNAKE_CASE ): number += 1 # precondition assert ( isinstance(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ) and ans[0] != p_number_a and ans[len(SCREAMING_SNAKE_CASE ) - 1] != p_number_a ), "'ans' must been a list without the arguments" # 'ans' contains not 'pNumber1' and 'pNumber2' ! return ans def a__ ( SCREAMING_SNAKE_CASE : Optional[int] ): '''simple docstring''' assert isinstance(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ) and (n >= 1), "'n' must been int and >= 1" lowerCAmelCase : Any = [] # will be returned. for divisor in range(1 , n + 1 ): if n % divisor == 0: ans.append(SCREAMING_SNAKE_CASE ) # precondition assert ans[0] == 1 and ans[len(SCREAMING_SNAKE_CASE ) - 1] == n, "Error in function getDivisiors(...)" return ans def a__ ( SCREAMING_SNAKE_CASE : List[Any] ): '''simple docstring''' assert isinstance(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ) and ( number > 1 ), "'number' must been an int and >= 1" lowerCAmelCase : Any = get_divisors(SCREAMING_SNAKE_CASE ) # precondition assert ( isinstance(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ) and (divisors[0] == 1) and (divisors[len(SCREAMING_SNAKE_CASE ) - 1] == number) ), "Error in help-function getDivisiors(...)" # summed all divisors up to 'number' (exclusive), hence [:-1] return sum(divisors[:-1] ) == number def a__ ( SCREAMING_SNAKE_CASE : List[str] , SCREAMING_SNAKE_CASE : Tuple ): '''simple docstring''' assert ( isinstance(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ) and isinstance(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ) and (denominator != 0) ), "The arguments must been from type int and 'denominator' != 0" # build the greatest common divisor of numerator and denominator. lowerCAmelCase : Any = gcd(abs(SCREAMING_SNAKE_CASE ) , abs(SCREAMING_SNAKE_CASE ) ) # precondition assert ( isinstance(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ) and (numerator % gcd_of_fraction == 0) and (denominator % gcd_of_fraction == 0) ), "Error in function gcd(...,...)" return (numerator // gcd_of_fraction, denominator // gcd_of_fraction) def a__ ( SCREAMING_SNAKE_CASE : Any ): '''simple docstring''' assert isinstance(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ) and (n >= 0), "'n' must been a int and >= 0" lowerCAmelCase : Any = 1 # this will be return. for factor in range(1 , n + 1 ): ans *= factor return ans def a__ ( SCREAMING_SNAKE_CASE : str ): '''simple docstring''' assert isinstance(SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ) and (n >= 0), "'n' must been an int and >= 0" lowerCAmelCase : Union[str, Any] = 0 lowerCAmelCase : Tuple = 1 lowerCAmelCase : Any = 1 # this will be return for _ in range(n - 1 ): lowerCAmelCase : List[str] = ans ans += fiba lowerCAmelCase : Optional[Any] = tmp return ans
108
from typing import Dict import numpy as np from ..utils import add_end_docstrings, is_tf_available, is_torch_available, logging from .base import PIPELINE_INIT_ARGS, GenericTensor, Pipeline, PipelineException if is_tf_available(): import tensorflow as tf from ..tf_utils import stable_softmax if is_torch_available(): import torch lowercase : List[str] = logging.get_logger(__name__) @add_end_docstrings( lowerCAmelCase , R"\n top_k (`int`, defaults to 5):\n The number of predictions to return.\n targets (`str` or `List[str]`, *optional*):\n When passed, the model will limit the scores to the passed targets instead of looking up in the whole\n vocab. If the provided targets are not in the model vocab, they will be tokenized and the first resulting\n token will be used (with a warning, and that might be slower).\n\n " , ) class __snake_case ( lowerCAmelCase ): def _SCREAMING_SNAKE_CASE ( self ,snake_case ): '''simple docstring''' if self.framework == "tf": lowercase : str = tf.where(input_ids == self.tokenizer.mask_token_id ).numpy() elif self.framework == "pt": lowercase : Optional[int] = torch.nonzero(input_ids == self.tokenizer.mask_token_id ,as_tuple=snake_case ) else: raise ValueError("""Unsupported framework""" ) return masked_index def _SCREAMING_SNAKE_CASE ( self ,snake_case ): '''simple docstring''' lowercase : Tuple = self.get_masked_index(snake_case ) lowercase : Dict = np.prod(masked_index.shape ) if numel < 1: raise PipelineException( """fill-mask""" ,self.model.base_model_prefix ,f"No mask_token ({self.tokenizer.mask_token}) found on the input" ,) def _SCREAMING_SNAKE_CASE ( self ,snake_case ): '''simple docstring''' if isinstance(snake_case ,snake_case ): for model_input in model_inputs: self._ensure_exactly_one_mask_token(model_input["""input_ids"""][0] ) else: for input_ids in model_inputs["input_ids"]: self._ensure_exactly_one_mask_token(snake_case ) def _SCREAMING_SNAKE_CASE ( self ,snake_case ,snake_case=None ,**snake_case ): '''simple docstring''' if return_tensors is None: lowercase : int = self.framework lowercase : Optional[Any] = self.tokenizer(snake_case ,return_tensors=snake_case ) self.ensure_exactly_one_mask_token(snake_case ) return model_inputs def _SCREAMING_SNAKE_CASE ( self ,snake_case ): '''simple docstring''' lowercase : Optional[int] = self.model(**snake_case ) lowercase : Tuple = model_inputs["""input_ids"""] return model_outputs def _SCREAMING_SNAKE_CASE ( self ,snake_case ,snake_case=5 ,snake_case=None ): '''simple docstring''' if target_ids is not None and target_ids.shape[0] < top_k: lowercase : str = target_ids.shape[0] lowercase : Optional[Any] = model_outputs["""input_ids"""][0] lowercase : List[str] = model_outputs["""logits"""] if self.framework == "tf": lowercase : List[str] = tf.where(input_ids == self.tokenizer.mask_token_id ).numpy()[:, 0] lowercase : Tuple = outputs.numpy() lowercase : Tuple = outputs[0, masked_index, :] lowercase : Any = stable_softmax(snake_case ,axis=-1 ) if target_ids is not None: lowercase : Union[str, Any] = tf.gather_nd(tf.squeeze(snake_case ,0 ) ,target_ids.reshape(-1 ,1 ) ) lowercase : int = tf.expand_dims(snake_case ,0 ) lowercase : Tuple = tf.math.top_k(snake_case ,k=snake_case ) lowercase , lowercase : int = topk.values.numpy(), topk.indices.numpy() else: lowercase : Optional[Any] = torch.nonzero(input_ids == self.tokenizer.mask_token_id ,as_tuple=snake_case ).squeeze(-1 ) # Fill mask pipeline supports only one ${mask_token} per sample lowercase : Union[str, Any] = outputs[0, masked_index, :] lowercase : Tuple = logits.softmax(dim=-1 ) if target_ids is not None: lowercase : List[str] = probs[..., target_ids] lowercase , lowercase : Union[str, Any] = probs.topk(snake_case ) lowercase : Any = [] lowercase : List[Any] = values.shape[0] == 1 for i, (_values, _predictions) in enumerate(zip(values.tolist() ,predictions.tolist() ) ): lowercase : Dict = [] for v, p in zip(_values ,_predictions ): # Copy is important since we're going to modify this array in place lowercase : Dict = input_ids.numpy().copy() if target_ids is not None: lowercase : Union[str, Any] = target_ids[p].tolist() lowercase : Tuple = p # Filter padding out: lowercase : List[str] = tokens[np.where(tokens != self.tokenizer.pad_token_id )] # Originally we skip special tokens to give readable output. # For multi masks though, the other [MASK] would be removed otherwise # making the output look odd, so we add them back lowercase : Tuple = self.tokenizer.decode(snake_case ,skip_special_tokens=snake_case ) lowercase : Optional[Any] = {"""score""": v, """token""": p, """token_str""": self.tokenizer.decode([p] ), """sequence""": sequence} row.append(snake_case ) result.append(snake_case ) if single_mask: return result[0] return result def _SCREAMING_SNAKE_CASE ( self ,snake_case ,snake_case=None ): '''simple docstring''' if isinstance(snake_case ,snake_case ): lowercase : List[Any] = [targets] try: lowercase : List[str] = self.tokenizer.get_vocab() except Exception: lowercase : Any = {} lowercase : Dict = [] for target in targets: lowercase : Dict = vocab.get(snake_case ,snake_case ) if id_ is None: lowercase : Optional[int] = self.tokenizer( snake_case ,add_special_tokens=snake_case ,return_attention_mask=snake_case ,return_token_type_ids=snake_case ,max_length=1 ,truncation=snake_case ,)["""input_ids"""] if len(snake_case ) == 0: logger.warning( f"The specified target token `{target}` does not exist in the model vocabulary. " """We cannot replace it with anything meaningful, ignoring it""" ) continue lowercase : Union[str, Any] = input_ids[0] # XXX: If users encounter this pass # it becomes pretty slow, so let's make sure # The warning enables them to fix the input to # get faster performance. logger.warning( f"The specified target token `{target}` does not exist in the model vocabulary. " f"Replacing with `{self.tokenizer.convert_ids_to_tokens(id_ )}`." ) target_ids.append(id_ ) lowercase : Optional[Any] = list(set(snake_case ) ) if len(snake_case ) == 0: raise ValueError("""At least one target must be provided when passed.""" ) lowercase : Optional[Any] = np.array(snake_case ) return target_ids def _SCREAMING_SNAKE_CASE ( self ,snake_case=None ,snake_case=None ): '''simple docstring''' lowercase : Dict = {} if targets is not None: lowercase : str = self.get_target_ids(snake_case ,snake_case ) lowercase : List[Any] = target_ids if top_k is not None: lowercase : List[str] = top_k if self.tokenizer.mask_token_id is None: raise PipelineException( """fill-mask""" ,self.model.base_model_prefix ,"""The tokenizer does not define a `mask_token`.""" ) return {}, {}, postprocess_params def __call__( self ,snake_case ,*snake_case ,**snake_case ): '''simple docstring''' lowercase : Tuple = super().__call__(snake_case ,**snake_case ) if isinstance(snake_case ,snake_case ) and len(snake_case ) == 1: return outputs[0] return outputs
20
0
"""simple docstring""" import logging import os from typing import List, Tuple import numpy as np import psutil import torch import torch.distributed as dist from transformers import RagRetriever _snake_case = logging.getLogger(__name__) class lowerCAmelCase ( lowerCamelCase__ ): def __init__( self :Tuple , _lowercase :Optional[int] , _lowercase :List[Any] , _lowercase :Union[str, Any] , _lowercase :Union[str, Any]=None ): '''simple docstring''' super().__init__( _lowercase , question_encoder_tokenizer=_lowercase , generator_tokenizer=_lowercase , index=_lowercase , init_retrieval=_lowercase , ) lowercase__ = None def UpperCAmelCase ( self :int , _lowercase :int ): '''simple docstring''' logger.info("initializing retrieval" ) # initializing a separate process group for retrieval as the default # nccl backend doesn't support gather/scatter operations while gloo # is too slow to replace nccl for the core gpu communication if dist.is_initialized(): logger.info("dist initialized" ) # needs to be set manually lowercase__ = self._infer_socket_ifname() # avoid clash with the NCCL port lowercase__ = str(distributed_port + 1 ) lowercase__ = dist.new_group(ranks=_lowercase , backend="gloo" ) # initialize retriever only on the main worker if not dist.is_initialized() or self._is_main(): logger.info("dist not initialized / main" ) self.index.init_index() # all processes wait untill the retriever is initialized by the main process if dist.is_initialized(): torch.distributed.barrier(group=self.process_group ) def UpperCAmelCase ( self :Optional[Any] ): '''simple docstring''' return dist.get_rank(group=self.process_group ) == 0 def UpperCAmelCase ( self :List[str] , _lowercase :Optional[int] , _lowercase :Tuple , _lowercase :List[Any]=torch.floataa ): '''simple docstring''' lowercase__ = torch.empty(_lowercase , dtype=_lowercase ) dist.scatter(_lowercase , src=0 , scatter_list=_lowercase , group=self.process_group ) return target_tensor def UpperCAmelCase ( self :Any ): '''simple docstring''' lowercase__ = psutil.net_if_addrs() # a hacky way to deal with varying network interface names lowercase__ = next((addr for addr in addrs if addr.startswith("e" )) , _lowercase ) return ifname def UpperCAmelCase ( self :Tuple , _lowercase :np.ndarray , _lowercase :int ): '''simple docstring''' if not dist.is_initialized(): lowercase__ = self._main_retrieve(_lowercase , _lowercase ) return retrieved_doc_embeds, doc_ids, self.index.get_doc_dicts(_lowercase ) # distributed training lowercase__ = dist.get_world_size(group=self.process_group ) # gather logic lowercase__ = None if self._is_main(): lowercase__ = [torch.empty(question_hidden_states.shape , dtype=torch.floataa ) for _ in range(_lowercase )] dist.gather(torch.tensor(_lowercase ) , dst=0 , gather_list=_lowercase , group=self.process_group ) # scatter logic lowercase__ = question_hidden_states.shape[0] lowercase__ = [] lowercase__ = [] if self._is_main(): assert len(_lowercase ) == world_size lowercase__ = self._main_retrieve(torch.cat(_lowercase ).numpy() , _lowercase ) lowercase__ = torch.tensor(_lowercase ), torch.tensor(_lowercase ) lowercase__ = self._chunk_tensor(_lowercase , _lowercase ) lowercase__ = self._chunk_tensor(_lowercase , _lowercase ) lowercase__ = self._scattered(_lowercase , [n_queries, n_docs] , target_type=torch.intaa ) lowercase__ = self._scattered(_lowercase , [n_queries, n_docs, question_hidden_states.shape[1]] ) return retrieved_doc_embeds.numpy(), doc_ids.numpy(), self.index.get_doc_dicts(_lowercase )
354
import os import socket from contextlib import contextmanager import torch from ..commands.config.default import write_basic_config # noqa: F401 from ..state import PartialState from .dataclasses import DistributedType from .imports import is_deepspeed_available, is_tpu_available from .transformer_engine import convert_model from .versions import is_torch_version if is_deepspeed_available(): from deepspeed import DeepSpeedEngine if is_tpu_available(check_device=False): import torch_xla.core.xla_model as xm def _A ( __magic_name__ ): if is_torch_version("<" , "2.0.0" ) or not hasattr(__magic_name__ , "_dynamo" ): return False return isinstance(__magic_name__ , torch._dynamo.eval_frame.OptimizedModule ) def _A ( __magic_name__ , __magic_name__ = True ): lowercase__ = (torch.nn.parallel.DistributedDataParallel, torch.nn.DataParallel) lowercase__ = is_compiled_module(__magic_name__ ) if is_compiled: lowercase__ = model lowercase__ = model._orig_mod if is_deepspeed_available(): options += (DeepSpeedEngine,) while isinstance(__magic_name__ , __magic_name__ ): lowercase__ = model.module if not keep_fpaa_wrapper: lowercase__ = getattr(__magic_name__ , "forward" ) lowercase__ = model.__dict__.pop("_original_forward" , __magic_name__ ) if original_forward is not None: while hasattr(__magic_name__ , "__wrapped__" ): lowercase__ = forward.__wrapped__ if forward == original_forward: break lowercase__ = forward if getattr(__magic_name__ , "_converted_to_transformer_engine" , __magic_name__ ): convert_model(__magic_name__ , to_transformer_engine=__magic_name__ ) if is_compiled: lowercase__ = model lowercase__ = compiled_model return model def _A ( ): PartialState().wait_for_everyone() def _A ( __magic_name__ , __magic_name__ ): if PartialState().distributed_type == DistributedType.TPU: xm.save(__magic_name__ , __magic_name__ ) elif PartialState().local_process_index == 0: torch.save(__magic_name__ , __magic_name__ ) @contextmanager def _A ( **__magic_name__ ): for key, value in kwargs.items(): lowercase__ = str(__magic_name__ ) yield for key in kwargs: if key.upper() in os.environ: del os.environ[key.upper()] def _A ( __magic_name__ ): if not hasattr(__magic_name__ , "__qualname__" ) and not hasattr(__magic_name__ , "__name__" ): lowercase__ = getattr(__magic_name__ , "__class__" , __magic_name__ ) if hasattr(__magic_name__ , "__qualname__" ): return obj.__qualname__ if hasattr(__magic_name__ , "__name__" ): return obj.__name__ return str(__magic_name__ ) def _A ( __magic_name__ , __magic_name__ ): for key, value in source.items(): if isinstance(__magic_name__ , __magic_name__ ): lowercase__ = destination.setdefault(__magic_name__ , {} ) merge_dicts(__magic_name__ , __magic_name__ ) else: lowercase__ = value return destination def _A ( __magic_name__ = None ): if port is None: lowercase__ = 2_9500 with socket.socket(socket.AF_INET , socket.SOCK_STREAM ) as s: return s.connect_ex(("localhost", port) ) == 0
201
0
"""simple docstring""" from __future__ import annotations _a = list[list[int]] # assigning initial values to the grid _a = [ [3, 0, 6, 5, 0, 8, 4, 0, 0], [5, 2, 0, 0, 0, 0, 0, 0, 0], [0, 8, 7, 0, 0, 0, 0, 3, 1], [0, 0, 3, 0, 1, 0, 0, 8, 0], [9, 0, 0, 8, 6, 3, 0, 0, 5], [0, 5, 0, 0, 9, 0, 6, 0, 0], [1, 3, 0, 0, 0, 0, 2, 5, 0], [0, 0, 0, 0, 0, 0, 0, 7, 4], [0, 0, 5, 2, 0, 6, 3, 0, 0], ] # a grid with no solution _a = [ [5, 0, 6, 5, 0, 8, 4, 0, 3], [5, 2, 0, 0, 0, 0, 0, 0, 2], [1, 8, 7, 0, 0, 0, 0, 3, 1], [0, 0, 3, 0, 1, 0, 0, 8, 0], [9, 0, 0, 8, 6, 3, 0, 0, 5], [0, 5, 0, 0, 9, 0, 6, 0, 0], [1, 3, 0, 0, 0, 0, 2, 5, 0], [0, 0, 0, 0, 0, 0, 0, 7, 4], [0, 0, 5, 2, 0, 6, 3, 0, 0], ] def lowerCamelCase__ ( __snake_case, __snake_case, __snake_case, __snake_case ) -> bool: """simple docstring""" for i in range(9 ): if grid[row][i] == n or grid[i][column] == n: return False for i in range(3 ): for j in range(3 ): if grid[(row - row % 3) + i][(column - column % 3) + j] == n: return False return True def lowerCamelCase__ ( __snake_case ) -> tuple[int, int] | None: """simple docstring""" for i in range(9 ): for j in range(9 ): if grid[i][j] == 0: return i, j return None def lowerCamelCase__ ( __snake_case ) -> Matrix | None: """simple docstring""" if location := find_empty_location(__snake_case ): _UpperCamelCase , _UpperCamelCase = location else: # If the location is ``None``, then the grid is solved. return grid for digit in range(1, 10 ): if is_safe(__snake_case, __snake_case, __snake_case, __snake_case ): _UpperCamelCase = digit if sudoku(__snake_case ) is not None: return grid _UpperCamelCase = 0 return None def lowerCamelCase__ ( __snake_case ) -> None: """simple docstring""" for row in grid: for cell in row: print(__snake_case, end=''' ''' ) print() if __name__ == "__main__": # make a copy of grid so that you can compare with the unmodified grid for example_grid in (initial_grid, no_solution): print("""\nExample grid:\n""" + """=""" * 20) print_solution(example_grid) print("""\nExample grid solution:""") _a = sudoku(example_grid) if solution is not None: print_solution(solution) else: print("""Cannot find a solution.""")
194
"""simple docstring""" from typing import TYPE_CHECKING # rely on isort to merge the imports from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available _a = { """configuration_autoformer""": [ """AUTOFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP""", """AutoformerConfig""", ], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _a = [ """AUTOFORMER_PRETRAINED_MODEL_ARCHIVE_LIST""", """AutoformerForPrediction""", """AutoformerModel""", """AutoformerPreTrainedModel""", ] if TYPE_CHECKING: from .configuration_autoformer import ( AUTOFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP, AutoformerConfig, ) try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_autoformer import ( AUTOFORMER_PRETRAINED_MODEL_ARCHIVE_LIST, AutoformerForPrediction, AutoformerModel, AutoformerPreTrainedModel, ) else: import sys _a = _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)
194
1
def UpperCamelCase_( lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ ) -> Any: # Return True if there is node that has not iterated. _lowercase : Optional[int] = [False] * len(lowerCamelCase_ ) _lowercase : Union[str, Any] = [] queue.append(lowerCamelCase_ ) _lowercase : Any = True while queue: _lowercase : str = queue.pop(0 ) for ind in range(len(graph[u] ) ): if visited[ind] is False and graph[u][ind] > 0: queue.append(lowerCamelCase_ ) _lowercase : Optional[int] = True _lowercase : int = u return visited[t] def UpperCamelCase_( lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ ) -> List[Any]: # This array is filled by BFS and to store path _lowercase : Optional[Any] = [-1] * (len(lowerCamelCase_ )) _lowercase : Optional[Any] = 0 while bfs(lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ ): _lowercase : Optional[int] = float('Inf' ) _lowercase : List[Any] = sink while s != source: # Find the minimum value in select path _lowercase : Any = min(lowerCamelCase_ , graph[parent[s]][s] ) _lowercase : List[str] = parent[s] max_flow += path_flow _lowercase : Optional[Any] = sink while v != source: _lowercase : Optional[int] = parent[v] graph[u][v] -= path_flow graph[v][u] += path_flow _lowercase : str = parent[v] return max_flow SCREAMING_SNAKE_CASE : Dict = [ [0, 16, 13, 0, 0, 0], [0, 0, 10, 12, 0, 0], [0, 4, 0, 0, 14, 0], [0, 0, 9, 0, 0, 20], [0, 0, 0, 7, 0, 4], [0, 0, 0, 0, 0, 0], ] SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE : int = 0, 5 print(ford_fulkerson(graph, source, sink))
84
import tempfile import numpy as np import torch from transformers import AutoTokenizer, TaEncoderModel from diffusers import DDPMScheduler, UNetaDConditionModel from diffusers.models.attention_processor import AttnAddedKVProcessor from diffusers.pipelines.deepfloyd_if import IFWatermarker from diffusers.utils.testing_utils import torch_device from ..test_pipelines_common import to_np class _lowerCamelCase: def UpperCamelCase ( self) -> Union[str, Any]: """simple docstring""" torch.manual_seed(0) _lowercase : Dict = TaEncoderModel.from_pretrained('hf-internal-testing/tiny-random-t5') torch.manual_seed(0) _lowercase : List[str] = AutoTokenizer.from_pretrained('hf-internal-testing/tiny-random-t5') torch.manual_seed(0) _lowercase : Optional[int] = UNetaDConditionModel( sample_size=32, layers_per_block=1, block_out_channels=[32, 64], down_block_types=[ 'ResnetDownsampleBlock2D', 'SimpleCrossAttnDownBlock2D', ], mid_block_type='UNetMidBlock2DSimpleCrossAttn', up_block_types=['SimpleCrossAttnUpBlock2D', 'ResnetUpsampleBlock2D'], in_channels=3, out_channels=6, cross_attention_dim=32, encoder_hid_dim=32, attention_head_dim=8, addition_embed_type='text', addition_embed_type_num_heads=2, cross_attention_norm='group_norm', resnet_time_scale_shift='scale_shift', act_fn='gelu', ) unet.set_attn_processor(AttnAddedKVProcessor()) # For reproducibility tests torch.manual_seed(0) _lowercase : Dict = DDPMScheduler( num_train_timesteps=10_00, beta_schedule='squaredcos_cap_v2', beta_start=0.0_0_0_1, beta_end=0.0_2, thresholding=lowerCamelCase, dynamic_thresholding_ratio=0.9_5, sample_max_value=1.0, prediction_type='epsilon', variance_type='learned_range', ) torch.manual_seed(0) _lowercase : List[Any] = IFWatermarker() return { "text_encoder": text_encoder, "tokenizer": tokenizer, "unet": unet, "scheduler": scheduler, "watermarker": watermarker, "safety_checker": None, "feature_extractor": None, } def UpperCamelCase ( self) -> List[str]: """simple docstring""" torch.manual_seed(0) _lowercase : List[str] = TaEncoderModel.from_pretrained('hf-internal-testing/tiny-random-t5') torch.manual_seed(0) _lowercase : int = AutoTokenizer.from_pretrained('hf-internal-testing/tiny-random-t5') torch.manual_seed(0) _lowercase : List[str] = UNetaDConditionModel( sample_size=32, layers_per_block=[1, 2], block_out_channels=[32, 64], down_block_types=[ 'ResnetDownsampleBlock2D', 'SimpleCrossAttnDownBlock2D', ], mid_block_type='UNetMidBlock2DSimpleCrossAttn', up_block_types=['SimpleCrossAttnUpBlock2D', 'ResnetUpsampleBlock2D'], in_channels=6, out_channels=6, cross_attention_dim=32, encoder_hid_dim=32, attention_head_dim=8, addition_embed_type='text', addition_embed_type_num_heads=2, cross_attention_norm='group_norm', resnet_time_scale_shift='scale_shift', act_fn='gelu', class_embed_type='timestep', mid_block_scale_factor=1.4_1_4, time_embedding_act_fn='gelu', time_embedding_dim=32, ) unet.set_attn_processor(AttnAddedKVProcessor()) # For reproducibility tests torch.manual_seed(0) _lowercase : Optional[int] = DDPMScheduler( num_train_timesteps=10_00, beta_schedule='squaredcos_cap_v2', beta_start=0.0_0_0_1, beta_end=0.0_2, thresholding=lowerCamelCase, dynamic_thresholding_ratio=0.9_5, sample_max_value=1.0, prediction_type='epsilon', variance_type='learned_range', ) torch.manual_seed(0) _lowercase : str = DDPMScheduler( num_train_timesteps=10_00, beta_schedule='squaredcos_cap_v2', beta_start=0.0_0_0_1, beta_end=0.0_2, ) torch.manual_seed(0) _lowercase : Union[str, Any] = IFWatermarker() return { "text_encoder": text_encoder, "tokenizer": tokenizer, "unet": unet, "scheduler": scheduler, "image_noising_scheduler": image_noising_scheduler, "watermarker": watermarker, "safety_checker": None, "feature_extractor": None, } def UpperCamelCase ( self) -> List[Any]: """simple docstring""" _lowercase : List[Any] = self.get_dummy_components() _lowercase : List[str] = self.pipeline_class(**lowerCamelCase) pipe.to(lowerCamelCase) pipe.set_progress_bar_config(disable=lowerCamelCase) _lowercase : int = self.get_dummy_inputs(lowerCamelCase) _lowercase : int = inputs['prompt'] _lowercase : Dict = inputs['generator'] _lowercase : Optional[int] = inputs['num_inference_steps'] _lowercase : str = inputs['output_type'] if "image" in inputs: _lowercase : List[Any] = inputs['image'] else: _lowercase : List[Any] = None if "mask_image" in inputs: _lowercase : Union[str, Any] = inputs['mask_image'] else: _lowercase : Dict = None if "original_image" in inputs: _lowercase : Any = inputs['original_image'] else: _lowercase : Tuple = None _lowercase , _lowercase : str = pipe.encode_prompt(lowerCamelCase) # inputs with prompt converted to embeddings _lowercase : Any = { 'prompt_embeds': prompt_embeds, 'negative_prompt_embeds': negative_prompt_embeds, 'generator': generator, 'num_inference_steps': num_inference_steps, 'output_type': output_type, } if image is not None: _lowercase : int = image if mask_image is not None: _lowercase : str = mask_image if original_image is not None: _lowercase : Optional[Any] = original_image # set all optional components to None for optional_component in pipe._optional_components: setattr(lowerCamelCase, lowerCamelCase, lowerCamelCase) _lowercase : Dict = pipe(**lowerCamelCase)[0] with tempfile.TemporaryDirectory() as tmpdir: pipe.save_pretrained(lowerCamelCase) _lowercase : Any = self.pipeline_class.from_pretrained(lowerCamelCase) pipe_loaded.to(lowerCamelCase) pipe_loaded.set_progress_bar_config(disable=lowerCamelCase) pipe_loaded.unet.set_attn_processor(AttnAddedKVProcessor()) # For reproducibility tests for optional_component in pipe._optional_components: self.assertTrue( getattr(lowerCamelCase, lowerCamelCase) is None, F'''`{optional_component}` did not stay set to None after loading.''', ) _lowercase : Dict = self.get_dummy_inputs(lowerCamelCase) _lowercase : Optional[Any] = inputs['generator'] _lowercase : Any = inputs['num_inference_steps'] _lowercase : List[Any] = inputs['output_type'] # inputs with prompt converted to embeddings _lowercase : Optional[int] = { 'prompt_embeds': prompt_embeds, 'negative_prompt_embeds': negative_prompt_embeds, 'generator': generator, 'num_inference_steps': num_inference_steps, 'output_type': output_type, } if image is not None: _lowercase : str = image if mask_image is not None: _lowercase : Optional[int] = mask_image if original_image is not None: _lowercase : int = original_image _lowercase : str = pipe_loaded(**lowerCamelCase)[0] _lowercase : List[Any] = np.abs(to_np(lowerCamelCase) - to_np(lowerCamelCase)).max() self.assertLess(lowerCamelCase, 1E-4) def UpperCamelCase ( self) -> Union[str, Any]: """simple docstring""" _lowercase : Optional[Any] = self.get_dummy_components() _lowercase : Any = self.pipeline_class(**lowerCamelCase) pipe.to(lowerCamelCase) pipe.set_progress_bar_config(disable=lowerCamelCase) _lowercase : List[str] = self.get_dummy_inputs(lowerCamelCase) _lowercase : Tuple = pipe(**lowerCamelCase)[0] with tempfile.TemporaryDirectory() as tmpdir: pipe.save_pretrained(lowerCamelCase) _lowercase : List[str] = self.pipeline_class.from_pretrained(lowerCamelCase) pipe_loaded.to(lowerCamelCase) pipe_loaded.set_progress_bar_config(disable=lowerCamelCase) pipe_loaded.unet.set_attn_processor(AttnAddedKVProcessor()) # For reproducibility tests _lowercase : int = self.get_dummy_inputs(lowerCamelCase) _lowercase : Tuple = pipe_loaded(**lowerCamelCase)[0] _lowercase : str = np.abs(to_np(lowerCamelCase) - to_np(lowerCamelCase)).max() self.assertLess(lowerCamelCase, 1E-4)
84
1
import warnings from ...utils import logging from .image_processing_flava import FlavaImageProcessor __snake_case = logging.get_logger(__name__) class lowercase__ ( _UpperCAmelCase ): def __init__( self : Any , *UpperCAmelCase_ : Dict , **UpperCAmelCase_ : List[Any] ): warnings.warn( 'The class FlavaFeatureExtractor is deprecated and will be removed in version 5 of Transformers. Please' ' use FlavaImageProcessor instead.' , UpperCAmelCase_ , ) super().__init__(*UpperCAmelCase_ , **UpperCAmelCase_ )
176
import json import os import unittest from transformers.models.gptsan_japanese.tokenization_gptsan_japanese import ( VOCAB_FILES_NAMES, GPTSanJapaneseTokenizer, ) from transformers.testing_utils import require_tokenizers, slow from ...test_tokenization_common import TokenizerTesterMixin @require_tokenizers class lowercase__ ( _UpperCAmelCase , unittest.TestCase ): A__ : Any =GPTSanJapaneseTokenizer A__ : str =False A__ : int ={"""do_clean_text""": False, """add_prefix_space""": False} def A_ ( self : Any ): super().setUp() # fmt: off SCREAMING_SNAKE_CASE__ = ['こん', 'こんに', 'にちは', 'ばんは', '世界,㔺界', '、', '。', '<BR>', '<SP>', '<TAB>', '<URL>', '<EMAIL>', '<TEL>', '<DATE>', '<PRICE>', '<BLOCK>', '<KIGOU>', '<U2000U2BFF>', '<|emoji1|>', '<unk>', '<|bagoftoken|>', '<|endoftext|>'] # fmt: on SCREAMING_SNAKE_CASE__ = {'emoji': {'\ud83d\ude00': '<|emoji1|>'}, 'emoji_inv': {'<|emoji1|>': '\ud83d\ude00'}} # 😀 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['emoji_file'] ) with open(self.vocab_file , 'w' , encoding='utf-8' ) as vocab_writer: vocab_writer.write(''.join([x + '\n' for x in vocab_tokens] ) ) with open(self.emoji_file , 'w' ) as emoji_writer: emoji_writer.write(json.dumps(UpperCAmelCase_ ) ) def A_ ( self : str , **UpperCAmelCase_ : Optional[Any] ): kwargs.update(self.special_tokens_map ) return GPTSanJapaneseTokenizer.from_pretrained(self.tmpdirname , **UpperCAmelCase_ ) def A_ ( self : int , UpperCAmelCase_ : List[str] ): SCREAMING_SNAKE_CASE__ = 'こんにちは、世界。 \nこんばんは、㔺界。😀' SCREAMING_SNAKE_CASE__ = 'こんにちは、世界。 \nこんばんは、世界。😀' return input_text, output_text def A_ ( self : Any , UpperCAmelCase_ : str ): SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ = self.get_input_output_texts(UpperCAmelCase_ ) SCREAMING_SNAKE_CASE__ = tokenizer.encode(UpperCAmelCase_ , add_special_tokens=UpperCAmelCase_ ) SCREAMING_SNAKE_CASE__ = tokenizer.decode(UpperCAmelCase_ , clean_up_tokenization_spaces=UpperCAmelCase_ ) return text, ids def A_ ( self : str ): pass # TODO add if relevant def A_ ( self : Tuple ): pass # TODO add if relevant def A_ ( self : int ): pass # TODO add if relevant def A_ ( self : Optional[int] ): SCREAMING_SNAKE_CASE__ = self.get_tokenizer() # Testing tokenization SCREAMING_SNAKE_CASE__ = 'こんにちは、世界。 こんばんは、㔺界。' SCREAMING_SNAKE_CASE__ = ['こん', 'にちは', '、', '世界', '。', '<SP>', 'こん', 'ばんは', '、', '㔺界', '。'] SCREAMING_SNAKE_CASE__ = tokenizer.tokenize(UpperCAmelCase_ ) self.assertListEqual(UpperCAmelCase_ , UpperCAmelCase_ ) # Testing conversion to ids without special tokens SCREAMING_SNAKE_CASE__ = [0, 2, 5, 4, 6, 8, 0, 3, 5, 4, 6] SCREAMING_SNAKE_CASE__ = tokenizer.convert_tokens_to_ids(UpperCAmelCase_ ) self.assertListEqual(UpperCAmelCase_ , UpperCAmelCase_ ) # Testing conversion to ids with special tokens SCREAMING_SNAKE_CASE__ = tokens + [tokenizer.unk_token] SCREAMING_SNAKE_CASE__ = [0, 2, 5, 4, 6, 8, 0, 3, 5, 4, 6, 19] SCREAMING_SNAKE_CASE__ = tokenizer.convert_tokens_to_ids(UpperCAmelCase_ ) self.assertListEqual(UpperCAmelCase_ , UpperCAmelCase_ ) def A_ ( self : Union[str, Any] ): SCREAMING_SNAKE_CASE__ = self.get_tokenizer() # Testing tokenization SCREAMING_SNAKE_CASE__ = 'こんにちは、<|bagoftoken|>世界。こんばんは、<|bagoftoken|>㔺界。' SCREAMING_SNAKE_CASE__ = 'こんにちは、、、、世界。こんばんは、、、、世界。' SCREAMING_SNAKE_CASE__ = tokenizer.encode(UpperCAmelCase_ ) SCREAMING_SNAKE_CASE__ = tokenizer.decode(UpperCAmelCase_ ) self.assertEqual(UpperCAmelCase_ , UpperCAmelCase_ ) @slow def A_ ( self : str ): SCREAMING_SNAKE_CASE__ = self.tokenizer_class.from_pretrained('Tanrei/GPTSAN-japanese' ) # Testing tokenization SCREAMING_SNAKE_CASE__ = 'こんにちは、世界。' SCREAMING_SNAKE_CASE__ = 'こんばんは、㔺界。😀' SCREAMING_SNAKE_CASE__ = 'こんにちは、世界。こんばんは、世界。😀' SCREAMING_SNAKE_CASE__ = tokenizer.encode(prefix_text + input_text ) SCREAMING_SNAKE_CASE__ = tokenizer.encode('' , prefix_text=prefix_text + input_text ) SCREAMING_SNAKE_CASE__ = tokenizer.encode(UpperCAmelCase_ , prefix_text=UpperCAmelCase_ ) SCREAMING_SNAKE_CASE__ = tokenizer.decode(UpperCAmelCase_ ) SCREAMING_SNAKE_CASE__ = tokenizer.decode(UpperCAmelCase_ ) SCREAMING_SNAKE_CASE__ = tokenizer.decode(UpperCAmelCase_ ) self.assertEqual(UpperCAmelCase_ , UpperCAmelCase_ ) self.assertEqual(UpperCAmelCase_ , UpperCAmelCase_ ) self.assertEqual(UpperCAmelCase_ , UpperCAmelCase_ ) @slow def A_ ( self : Union[str, Any] ): SCREAMING_SNAKE_CASE__ = self.tokenizer_class.from_pretrained('Tanrei/GPTSAN-japanese' ) # Testing tokenization SCREAMING_SNAKE_CASE__ = 'こんにちは、世界。' SCREAMING_SNAKE_CASE__ = 'こんばんは、㔺界。😀' SCREAMING_SNAKE_CASE__ = len(tokenizer.encode(UpperCAmelCase_ ) ) - 2 SCREAMING_SNAKE_CASE__ = len(tokenizer.encode(UpperCAmelCase_ ) ) - 2 SCREAMING_SNAKE_CASE__ = [1] + [0] * (len_prefix + len_text + 1) SCREAMING_SNAKE_CASE__ = [1] * (len_prefix + len_text + 1) + [0] SCREAMING_SNAKE_CASE__ = [1] + [1] * (len_prefix) + [0] * (len_text + 1) SCREAMING_SNAKE_CASE__ = tokenizer(prefix_text + input_text ).token_type_ids SCREAMING_SNAKE_CASE__ = tokenizer('' , prefix_text=prefix_text + input_text ).token_type_ids SCREAMING_SNAKE_CASE__ = tokenizer(UpperCAmelCase_ , prefix_text=UpperCAmelCase_ ).token_type_ids self.assertListEqual(UpperCAmelCase_ , UpperCAmelCase_ ) self.assertListEqual(UpperCAmelCase_ , UpperCAmelCase_ ) self.assertListEqual(UpperCAmelCase_ , UpperCAmelCase_ ) @slow def A_ ( self : Tuple ): SCREAMING_SNAKE_CASE__ = self.tokenizer_class.from_pretrained('Tanrei/GPTSAN-japanese' ) SCREAMING_SNAKE_CASE__ = tokenizer.encode('あンいワ' ) SCREAMING_SNAKE_CASE__ = tokenizer.encode('' , prefix_text='あンいワ' ) SCREAMING_SNAKE_CASE__ = tokenizer.encode('いワ' , prefix_text='あン' ) self.assertEqual(tokenizer.decode(UpperCAmelCase_ ) , tokenizer.decode(UpperCAmelCase_ ) ) self.assertEqual(tokenizer.decode(UpperCAmelCase_ ) , tokenizer.decode(UpperCAmelCase_ ) ) self.assertNotEqual(UpperCAmelCase_ , UpperCAmelCase_ ) self.assertNotEqual(UpperCAmelCase_ , UpperCAmelCase_ ) self.assertEqual(x_token_a[1] , x_token_a[-1] ) # SEG token self.assertEqual(x_token_a[1] , x_token_a[3] ) # SEG token @slow def A_ ( self : Union[str, Any] ): SCREAMING_SNAKE_CASE__ = self.tokenizer_class.from_pretrained('Tanrei/GPTSAN-japanese' ) SCREAMING_SNAKE_CASE__ = [['武田信玄', 'は、'], ['織田信長', 'の配下の、']] SCREAMING_SNAKE_CASE__ = tokenizer(UpperCAmelCase_ , padding=UpperCAmelCase_ ) SCREAMING_SNAKE_CASE__ = tokenizer.batch_encode_plus(UpperCAmelCase_ , padding=UpperCAmelCase_ ) # fmt: off SCREAMING_SNAKE_CASE__ = [[35993, 8640, 25948, 35998, 30647, 35675, 35999, 35999], [35993, 10382, 9868, 35998, 30646, 9459, 30646, 35675]] SCREAMING_SNAKE_CASE__ = [[1, 1, 1, 0, 0, 0, 0, 0], [1, 1, 1, 0, 0, 0, 0, 0]] SCREAMING_SNAKE_CASE__ = [[1, 1, 1, 1, 1, 1, 0, 0], [1, 1, 1, 1, 1, 1, 1, 1]] # fmt: on self.assertListEqual(x_token.input_ids , UpperCAmelCase_ ) self.assertListEqual(x_token.token_type_ids , UpperCAmelCase_ ) self.assertListEqual(x_token.attention_mask , UpperCAmelCase_ ) self.assertListEqual(x_token_a.input_ids , UpperCAmelCase_ ) self.assertListEqual(x_token_a.token_type_ids , UpperCAmelCase_ ) self.assertListEqual(x_token_a.attention_mask , UpperCAmelCase_ ) def A_ ( self : Tuple ): # Intentionally convert some words to accommodate character fluctuations unique to Japanese pass def A_ ( self : List[str] ): # tokenizer has no padding token pass
176
1
import unittest from transformers.models.xlm_prophetnet.tokenization_xlm_prophetnet import SPIECE_UNDERLINE, XLMProphetNetTokenizer from transformers.testing_utils import get_tests_dir, require_sentencepiece, slow from transformers.utils import cached_property from ...test_tokenization_common import TokenizerTesterMixin __A = get_tests_dir("fixtures/test_sentencepiece.model") @require_sentencepiece class __lowerCAmelCase ( __magic_name__ , unittest.TestCase ): """simple docstring""" snake_case_ = XLMProphetNetTokenizer snake_case_ = False snake_case_ = True def lowercase_ ( self ) -> int: '''simple docstring''' super().setUp() # We have a SentencePiece fixture for testing __lowerCamelCase = XLMProphetNetTokenizer(lowerCamelCase__ , keep_accents=lowerCamelCase__ ) tokenizer.save_pretrained(self.tmpdirname ) def lowercase_ ( self ) -> int: '''simple docstring''' __lowerCamelCase = '[PAD]' __lowerCamelCase = 0 self.assertEqual(self.get_tokenizer()._convert_token_to_id(lowerCamelCase__ ) , lowerCamelCase__ ) self.assertEqual(self.get_tokenizer()._convert_id_to_token(lowerCamelCase__ ) , lowerCamelCase__ ) def lowercase_ ( self ) -> Tuple: '''simple docstring''' __lowerCamelCase = list(self.get_tokenizer().get_vocab().keys() ) self.assertEqual(vocab_keys[0] , '[PAD]' ) self.assertEqual(vocab_keys[1] , '[CLS]' ) self.assertEqual(vocab_keys[-1] , 'j' ) self.assertEqual(len(lowerCamelCase__ ) , 1_012 ) def lowercase_ ( self ) -> str: '''simple docstring''' self.assertEqual(self.get_tokenizer().vocab_size , 1_012 ) def lowercase_ ( self ) -> Dict: '''simple docstring''' __lowerCamelCase = XLMProphetNetTokenizer(lowerCamelCase__ , keep_accents=lowerCamelCase__ ) __lowerCamelCase = tokenizer.tokenize('This is a test' ) self.assertListEqual(lowerCamelCase__ , ['▁This', '▁is', '▁a', '▁t', 'est'] ) self.assertListEqual( tokenizer.convert_tokens_to_ids(lowerCamelCase__ ) , [value + tokenizer.fairseq_offset for value in [285, 46, 10, 170, 382]] , ) __lowerCamelCase = tokenizer.tokenize('I was born in 92000, and this is falsé.' ) self.assertListEqual( lowerCamelCase__ , [ SPIECE_UNDERLINE + 'I', SPIECE_UNDERLINE + 'was', SPIECE_UNDERLINE + 'b', 'or', 'n', SPIECE_UNDERLINE + 'in', SPIECE_UNDERLINE + '', '9', '2', '0', '0', '0', ',', SPIECE_UNDERLINE + 'and', SPIECE_UNDERLINE + 'this', SPIECE_UNDERLINE + 'is', SPIECE_UNDERLINE + 'f', 'al', 's', 'é', '.', ] , ) __lowerCamelCase = tokenizer.convert_tokens_to_ids(lowerCamelCase__ ) self.assertListEqual( lowerCamelCase__ , [ value + tokenizer.fairseq_offset for value in [8, 21, 84, 55, 24, 19, 7, -9, 602, 347, 347, 347, 3, 12, 66, 46, 72, 80, 6, -9, 4] ] , ) __lowerCamelCase = tokenizer.convert_ids_to_tokens(lowerCamelCase__ ) self.assertListEqual( lowerCamelCase__ , [ 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 lowercase_ ( self ) -> str: '''simple docstring''' return XLMProphetNetTokenizer.from_pretrained('microsoft/xprophetnet-large-wiki100-cased' ) @slow def lowercase_ ( self ) -> Any: '''simple docstring''' __lowerCamelCase = 'Hello World!' __lowerCamelCase = [35_389, 6_672, 49, 2] self.assertListEqual(lowerCamelCase__ , self.big_tokenizer.encode(lowerCamelCase__ ) ) @slow def lowercase_ ( self ) -> int: '''simple docstring''' # fmt: off __lowerCamelCase = {'input_ids': [[11_073, 82_783, 18, 26, 82_783, 549, 51_540, 248, 17_209, 1_301, 217, 20, 215_186, 1_325, 147, 17_209, 1_301, 217, 20, 56_370, 53, 122_020, 20, 16_477, 27, 87_355, 4_548, 20, 4_728, 78_392, 17, 159_969, 18, 26, 24_491, 629, 15, 538, 22_704, 5_439, 15, 2_788, 24_491, 9_885, 15, 43_534, 605, 15, 814, 18_403, 33_200, 29, 15, 43_534, 24_458, 12_410, 111, 24_966, 83_669, 9_637, 144_068, 26, 850, 22_346, 27, 147, 24_966, 83_669, 83_490, 26, 39_113, 735, 27, 689, 656, 2_800, 1_339, 4_600, 53, 122_020, 115_785, 34, 816, 1_339, 46_887, 18, 147, 53_905, 1_951, 42_238, 41_170, 17_732, 834, 436, 15, 27_523, 98_733, 217, 147, 5_542, 4_981, 930, 17_347, 16, 2], [20_091, 629, 94, 82_786, 58, 490, 20, 1_528, 84, 53_905, 344, 80_592, 110_128, 18_822, 5_267, 1_306, 62, 152_537, 308, 7_997, 401, 124_427, 549, 35_442, 225, 109, 15_055, 25_748, 147, 7_119, 43_712, 34, 767, 135_366, 18, 16, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [592, 63_784, 119_466, 17, 147_808, 88_214, 18, 656, 81, 32, 3_296, 10_280, 16, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]], '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, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]} # noqa: E501 # fmt: on self.tokenizer_integration_test_util( expected_encoding=lowerCamelCase__ , model_name='microsoft/xprophetnet-large-wiki100-cased' , revision='1acad1643ddd54a44df6a1b797ada8373685d90e' , )
348
import unittest from transformers import is_torch_available from transformers.testing_utils import require_sentencepiece, require_tokenizers, require_torch, slow, torch_device if is_torch_available(): from transformers import AutoModelForSeqaSeqLM, AutoTokenizer @require_torch @require_sentencepiece @require_tokenizers class __lowerCAmelCase ( unittest.TestCase ): """simple docstring""" @slow def lowercase_ ( self ) -> List[str]: '''simple docstring''' __lowerCamelCase = AutoModelForSeqaSeqLM.from_pretrained('google/mt5-small' , return_dict=lowerCamelCase__ ).to(lowerCamelCase__ ) __lowerCamelCase = AutoTokenizer.from_pretrained('google/mt5-small' ) __lowerCamelCase = tokenizer('Hello there' , return_tensors='pt' ).input_ids __lowerCamelCase = tokenizer('Hi I am' , return_tensors='pt' ).input_ids __lowerCamelCase = model(input_ids.to(lowerCamelCase__ ) , labels=labels.to(lowerCamelCase__ ) ).loss __lowerCamelCase = -(labels.shape[-1] * loss.item()) __lowerCamelCase = -84.91_27 self.assertTrue(abs(mtf_score - EXPECTED_SCORE ) < 1e-4 )
348
1
import requests from bsa import BeautifulSoup def A ( _SCREAMING_SNAKE_CASE = "AAPL" ) -> int: lowerCamelCase : List[str] = f'''https://in.finance.yahoo.com/quote/{symbol}?s={symbol}''' lowerCamelCase : int = BeautifulSoup(requests.get(lowerCAmelCase_ ).text ,"html.parser" ) lowerCamelCase : Union[str, Any] = "My(6px) Pos(r) smartphone_Mt(6px)" return soup.find("div" ,class_=class_ ).find("span" ).text if __name__ == "__main__": for symbol in "AAPL AMZN IBM GOOG MSFT ORCL".split(): print(f'''Current {symbol:<4} stock price is {stock_price(symbol):>8}''')
48
"""simple docstring""" from __future__ import annotations def UpperCAmelCase__ (lowerCAmelCase_ , lowerCAmelCase_ = None , lowerCAmelCase_ = None ): '''simple docstring''' if start is None: __SCREAMING_SNAKE_CASE = 0 if end is None: __SCREAMING_SNAKE_CASE = len(lowerCAmelCase_ ) - 1 if start >= end: return __SCREAMING_SNAKE_CASE = (start + end) // 2 slowsort(lowerCAmelCase_ , lowerCAmelCase_ , lowerCAmelCase_ ) slowsort(lowerCAmelCase_ , mid + 1 , lowerCAmelCase_ ) if sequence[end] < sequence[mid]: __SCREAMING_SNAKE_CASE , __SCREAMING_SNAKE_CASE = sequence[mid], sequence[end] slowsort(lowerCAmelCase_ , lowerCAmelCase_ , end - 1 ) if __name__ == "__main__": from doctest import testmod testmod()
54
0
"""simple docstring""" from ..models.auto import AutoModelForSeqaSeqLM, AutoTokenizer from .base import PipelineTool __A : str = { '''Acehnese Arabic''': '''ace_Arab''', '''Acehnese Latin''': '''ace_Latn''', '''Mesopotamian Arabic''': '''acm_Arab''', '''Ta\'izzi-Adeni Arabic''': '''acq_Arab''', '''Tunisian Arabic''': '''aeb_Arab''', '''Afrikaans''': '''afr_Latn''', '''South Levantine Arabic''': '''ajp_Arab''', '''Akan''': '''aka_Latn''', '''Amharic''': '''amh_Ethi''', '''North Levantine Arabic''': '''apc_Arab''', '''Modern Standard Arabic''': '''arb_Arab''', '''Modern Standard Arabic Romanized''': '''arb_Latn''', '''Najdi Arabic''': '''ars_Arab''', '''Moroccan Arabic''': '''ary_Arab''', '''Egyptian Arabic''': '''arz_Arab''', '''Assamese''': '''asm_Beng''', '''Asturian''': '''ast_Latn''', '''Awadhi''': '''awa_Deva''', '''Central Aymara''': '''ayr_Latn''', '''South Azerbaijani''': '''azb_Arab''', '''North Azerbaijani''': '''azj_Latn''', '''Bashkir''': '''bak_Cyrl''', '''Bambara''': '''bam_Latn''', '''Balinese''': '''ban_Latn''', '''Belarusian''': '''bel_Cyrl''', '''Bemba''': '''bem_Latn''', '''Bengali''': '''ben_Beng''', '''Bhojpuri''': '''bho_Deva''', '''Banjar Arabic''': '''bjn_Arab''', '''Banjar Latin''': '''bjn_Latn''', '''Standard Tibetan''': '''bod_Tibt''', '''Bosnian''': '''bos_Latn''', '''Buginese''': '''bug_Latn''', '''Bulgarian''': '''bul_Cyrl''', '''Catalan''': '''cat_Latn''', '''Cebuano''': '''ceb_Latn''', '''Czech''': '''ces_Latn''', '''Chokwe''': '''cjk_Latn''', '''Central Kurdish''': '''ckb_Arab''', '''Crimean Tatar''': '''crh_Latn''', '''Welsh''': '''cym_Latn''', '''Danish''': '''dan_Latn''', '''German''': '''deu_Latn''', '''Southwestern Dinka''': '''dik_Latn''', '''Dyula''': '''dyu_Latn''', '''Dzongkha''': '''dzo_Tibt''', '''Greek''': '''ell_Grek''', '''English''': '''eng_Latn''', '''Esperanto''': '''epo_Latn''', '''Estonian''': '''est_Latn''', '''Basque''': '''eus_Latn''', '''Ewe''': '''ewe_Latn''', '''Faroese''': '''fao_Latn''', '''Fijian''': '''fij_Latn''', '''Finnish''': '''fin_Latn''', '''Fon''': '''fon_Latn''', '''French''': '''fra_Latn''', '''Friulian''': '''fur_Latn''', '''Nigerian Fulfulde''': '''fuv_Latn''', '''Scottish Gaelic''': '''gla_Latn''', '''Irish''': '''gle_Latn''', '''Galician''': '''glg_Latn''', '''Guarani''': '''grn_Latn''', '''Gujarati''': '''guj_Gujr''', '''Haitian Creole''': '''hat_Latn''', '''Hausa''': '''hau_Latn''', '''Hebrew''': '''heb_Hebr''', '''Hindi''': '''hin_Deva''', '''Chhattisgarhi''': '''hne_Deva''', '''Croatian''': '''hrv_Latn''', '''Hungarian''': '''hun_Latn''', '''Armenian''': '''hye_Armn''', '''Igbo''': '''ibo_Latn''', '''Ilocano''': '''ilo_Latn''', '''Indonesian''': '''ind_Latn''', '''Icelandic''': '''isl_Latn''', '''Italian''': '''ita_Latn''', '''Javanese''': '''jav_Latn''', '''Japanese''': '''jpn_Jpan''', '''Kabyle''': '''kab_Latn''', '''Jingpho''': '''kac_Latn''', '''Kamba''': '''kam_Latn''', '''Kannada''': '''kan_Knda''', '''Kashmiri Arabic''': '''kas_Arab''', '''Kashmiri Devanagari''': '''kas_Deva''', '''Georgian''': '''kat_Geor''', '''Central Kanuri Arabic''': '''knc_Arab''', '''Central Kanuri Latin''': '''knc_Latn''', '''Kazakh''': '''kaz_Cyrl''', '''Kabiyè''': '''kbp_Latn''', '''Kabuverdianu''': '''kea_Latn''', '''Khmer''': '''khm_Khmr''', '''Kikuyu''': '''kik_Latn''', '''Kinyarwanda''': '''kin_Latn''', '''Kyrgyz''': '''kir_Cyrl''', '''Kimbundu''': '''kmb_Latn''', '''Northern Kurdish''': '''kmr_Latn''', '''Kikongo''': '''kon_Latn''', '''Korean''': '''kor_Hang''', '''Lao''': '''lao_Laoo''', '''Ligurian''': '''lij_Latn''', '''Limburgish''': '''lim_Latn''', '''Lingala''': '''lin_Latn''', '''Lithuanian''': '''lit_Latn''', '''Lombard''': '''lmo_Latn''', '''Latgalian''': '''ltg_Latn''', '''Luxembourgish''': '''ltz_Latn''', '''Luba-Kasai''': '''lua_Latn''', '''Ganda''': '''lug_Latn''', '''Luo''': '''luo_Latn''', '''Mizo''': '''lus_Latn''', '''Standard Latvian''': '''lvs_Latn''', '''Magahi''': '''mag_Deva''', '''Maithili''': '''mai_Deva''', '''Malayalam''': '''mal_Mlym''', '''Marathi''': '''mar_Deva''', '''Minangkabau Arabic ''': '''min_Arab''', '''Minangkabau Latin''': '''min_Latn''', '''Macedonian''': '''mkd_Cyrl''', '''Plateau Malagasy''': '''plt_Latn''', '''Maltese''': '''mlt_Latn''', '''Meitei Bengali''': '''mni_Beng''', '''Halh Mongolian''': '''khk_Cyrl''', '''Mossi''': '''mos_Latn''', '''Maori''': '''mri_Latn''', '''Burmese''': '''mya_Mymr''', '''Dutch''': '''nld_Latn''', '''Norwegian Nynorsk''': '''nno_Latn''', '''Norwegian Bokmål''': '''nob_Latn''', '''Nepali''': '''npi_Deva''', '''Northern Sotho''': '''nso_Latn''', '''Nuer''': '''nus_Latn''', '''Nyanja''': '''nya_Latn''', '''Occitan''': '''oci_Latn''', '''West Central Oromo''': '''gaz_Latn''', '''Odia''': '''ory_Orya''', '''Pangasinan''': '''pag_Latn''', '''Eastern Panjabi''': '''pan_Guru''', '''Papiamento''': '''pap_Latn''', '''Western Persian''': '''pes_Arab''', '''Polish''': '''pol_Latn''', '''Portuguese''': '''por_Latn''', '''Dari''': '''prs_Arab''', '''Southern Pashto''': '''pbt_Arab''', '''Ayacucho Quechua''': '''quy_Latn''', '''Romanian''': '''ron_Latn''', '''Rundi''': '''run_Latn''', '''Russian''': '''rus_Cyrl''', '''Sango''': '''sag_Latn''', '''Sanskrit''': '''san_Deva''', '''Santali''': '''sat_Olck''', '''Sicilian''': '''scn_Latn''', '''Shan''': '''shn_Mymr''', '''Sinhala''': '''sin_Sinh''', '''Slovak''': '''slk_Latn''', '''Slovenian''': '''slv_Latn''', '''Samoan''': '''smo_Latn''', '''Shona''': '''sna_Latn''', '''Sindhi''': '''snd_Arab''', '''Somali''': '''som_Latn''', '''Southern Sotho''': '''sot_Latn''', '''Spanish''': '''spa_Latn''', '''Tosk Albanian''': '''als_Latn''', '''Sardinian''': '''srd_Latn''', '''Serbian''': '''srp_Cyrl''', '''Swati''': '''ssw_Latn''', '''Sundanese''': '''sun_Latn''', '''Swedish''': '''swe_Latn''', '''Swahili''': '''swh_Latn''', '''Silesian''': '''szl_Latn''', '''Tamil''': '''tam_Taml''', '''Tatar''': '''tat_Cyrl''', '''Telugu''': '''tel_Telu''', '''Tajik''': '''tgk_Cyrl''', '''Tagalog''': '''tgl_Latn''', '''Thai''': '''tha_Thai''', '''Tigrinya''': '''tir_Ethi''', '''Tamasheq Latin''': '''taq_Latn''', '''Tamasheq Tifinagh''': '''taq_Tfng''', '''Tok Pisin''': '''tpi_Latn''', '''Tswana''': '''tsn_Latn''', '''Tsonga''': '''tso_Latn''', '''Turkmen''': '''tuk_Latn''', '''Tumbuka''': '''tum_Latn''', '''Turkish''': '''tur_Latn''', '''Twi''': '''twi_Latn''', '''Central Atlas Tamazight''': '''tzm_Tfng''', '''Uyghur''': '''uig_Arab''', '''Ukrainian''': '''ukr_Cyrl''', '''Umbundu''': '''umb_Latn''', '''Urdu''': '''urd_Arab''', '''Northern Uzbek''': '''uzn_Latn''', '''Venetian''': '''vec_Latn''', '''Vietnamese''': '''vie_Latn''', '''Waray''': '''war_Latn''', '''Wolof''': '''wol_Latn''', '''Xhosa''': '''xho_Latn''', '''Eastern Yiddish''': '''ydd_Hebr''', '''Yoruba''': '''yor_Latn''', '''Yue Chinese''': '''yue_Hant''', '''Chinese Simplified''': '''zho_Hans''', '''Chinese Traditional''': '''zho_Hant''', '''Standard Malay''': '''zsm_Latn''', '''Zulu''': '''zul_Latn''', } class lowerCamelCase ( _UpperCAmelCase ): lowercase : List[str] = 'facebook/nllb-200-distilled-600M' lowercase : Union[str, Any] = ( 'This is a tool that translates text from a language to another. It takes three inputs: `text`, which should ' 'be the text to translate, `src_lang`, which should be the language of the text to translate and `tgt_lang`, ' 'which should be the language for the desired ouput language. Both `src_lang` and `tgt_lang` are written in ' 'plain English, such as \'Romanian\', or \'Albanian\'. It returns the text translated in `tgt_lang`.' ) lowercase : List[str] = 'translator' lowercase : Union[str, Any] = AutoTokenizer lowercase : List[str] = AutoModelForSeqaSeqLM lowercase : List[str] = LANGUAGE_CODES lowercase : int = ['text', 'text', 'text'] lowercase : List[str] = ['text'] def a_ ( self , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ): if src_lang not in self.lang_to_code: raise ValueError(f'{src_lang} is not a supported language.' ) if tgt_lang not in self.lang_to_code: raise ValueError(f'{tgt_lang} is not a supported language.' ) UpperCamelCase : str = self.lang_to_code[src_lang] UpperCamelCase : Optional[int] = self.lang_to_code[tgt_lang] return self.pre_processor._build_translation_inputs( SCREAMING_SNAKE_CASE_ , return_tensors="""pt""" , src_lang=SCREAMING_SNAKE_CASE_ , tgt_lang=SCREAMING_SNAKE_CASE_ ) def a_ ( self , SCREAMING_SNAKE_CASE_ ): return self.model.generate(**SCREAMING_SNAKE_CASE_ ) def a_ ( self , SCREAMING_SNAKE_CASE_ ): return self.post_processor.decode(outputs[0].tolist() , skip_special_tokens=SCREAMING_SNAKE_CASE_ )
353
"""simple docstring""" import argparse import os import re __A : Dict = '''src/diffusers''' # Pattern that looks at the indentation in a line. __A : Union[str, Any] = re.compile(R'''^(\s*)\S''') # Pattern that matches `"key":" and puts `key` in group 0. __A : Dict = re.compile(R'''^\s*"([^"]+)":''') # Pattern that matches `_import_structure["key"]` and puts `key` in group 0. __A : List[str] = re.compile(R'''^\s*_import_structure\["([^"]+)"\]''') # Pattern that matches `"key",` and puts `key` in group 0. __A : Tuple = re.compile(R'''^\s*"([^"]+)",\s*$''') # Pattern that matches any `[stuff]` and puts `stuff` in group 0. __A : Tuple = re.compile(R'''\[([^\]]+)\]''') def A_ ( snake_case_ : Dict ): '''simple docstring''' UpperCamelCase : Union[str, Any] = _re_indent.search(snake_case_ ) return "" if search is None else search.groups()[0] def A_ ( snake_case_ : Union[str, Any] ,snake_case_ : Dict="" ,snake_case_ : Dict=None ,snake_case_ : Any=None ): '''simple docstring''' UpperCamelCase : Optional[int] = 0 UpperCamelCase : List[Any] = code.split("""\n""" ) if start_prompt is not None: while not lines[index].startswith(snake_case_ ): index += 1 UpperCamelCase : Optional[Any] = ["""\n""".join(lines[:index] )] else: UpperCamelCase : int = [] # We split into blocks until we get to the `end_prompt` (or the end of the block). UpperCamelCase : Any = [lines[index]] index += 1 while index < len(snake_case_ ) and (end_prompt is None or not lines[index].startswith(snake_case_ )): if len(lines[index] ) > 0 and get_indent(lines[index] ) == indent_level: if len(snake_case_ ) > 0 and get_indent(current_block[-1] ).startswith(indent_level + """ """ ): current_block.append(lines[index] ) blocks.append("""\n""".join(snake_case_ ) ) if index < len(snake_case_ ) - 1: UpperCamelCase : Any = [lines[index + 1]] index += 1 else: UpperCamelCase : List[str] = [] else: blocks.append("""\n""".join(snake_case_ ) ) UpperCamelCase : int = [lines[index]] else: current_block.append(lines[index] ) index += 1 # Adds current block if it's nonempty. if len(snake_case_ ) > 0: blocks.append("""\n""".join(snake_case_ ) ) # Add final block after end_prompt if provided. if end_prompt is not None and index < len(snake_case_ ): blocks.append("""\n""".join(lines[index:] ) ) return blocks def A_ ( snake_case_ : Optional[Any] ): '''simple docstring''' def _inner(snake_case_ : Tuple ): return key(snake_case_ ).lower().replace("""_""" ,"""""" ) return _inner def A_ ( snake_case_ : List[Any] ,snake_case_ : Optional[int]=None ): '''simple docstring''' # If no key is provided, we use a noop. def noop(snake_case_ : Dict ): return x if key is None: UpperCamelCase : int = noop # Constants are all uppercase, they go first. UpperCamelCase : List[Any] = [obj for obj in objects if key(snake_case_ ).isupper()] # Classes are not all uppercase but start with a capital, they go second. UpperCamelCase : str = [obj for obj in objects if key(snake_case_ )[0].isupper() and not key(snake_case_ ).isupper()] # Functions begin with a lowercase, they go last. UpperCamelCase : List[str] = [obj for obj in objects if not key(snake_case_ )[0].isupper()] UpperCamelCase : Tuple = ignore_underscore(snake_case_ ) return sorted(snake_case_ ,key=snake_case_ ) + sorted(snake_case_ ,key=snake_case_ ) + sorted(snake_case_ ,key=snake_case_ ) def A_ ( snake_case_ : int ): '''simple docstring''' # This inner function sort imports between [ ]. def _replace(snake_case_ : List[Any] ): UpperCamelCase : Any = match.groups()[0] if "," not in imports: return f'[{imports}]' UpperCamelCase : Union[str, Any] = [part.strip().replace("""\"""" ,"""""" ) for part in imports.split(""",""" )] # We will have a final empty element if the line finished with a comma. if len(keys[-1] ) == 0: UpperCamelCase : List[str] = keys[:-1] return "[" + ", ".join([f'"{k}"' for k in sort_objects(snake_case_ )] ) + "]" UpperCamelCase : str = import_statement.split("""\n""" ) if len(snake_case_ ) > 3: # Here we have to sort internal imports that are on several lines (one per name): # key: [ # "object1", # "object2", # ... # ] # We may have to ignore one or two lines on each side. UpperCamelCase : str = 2 if lines[1].strip() == """[""" else 1 UpperCamelCase : Dict = [(i, _re_strip_line.search(snake_case_ ).groups()[0]) for i, line in enumerate(lines[idx:-idx] )] UpperCamelCase : int = sort_objects(snake_case_ ,key=lambda snake_case_ : x[1] ) UpperCamelCase : Any = [lines[x[0] + idx] for x in sorted_indices] return "\n".join(lines[:idx] + sorted_lines + lines[-idx:] ) elif len(snake_case_ ) == 3: # Here we have to sort internal imports that are on one separate line: # key: [ # "object1", "object2", ... # ] if _re_bracket_content.search(lines[1] ) is not None: UpperCamelCase : List[Any] = _re_bracket_content.sub(_replace ,lines[1] ) else: UpperCamelCase : Optional[Any] = [part.strip().replace("""\"""" ,"""""" ) for part in lines[1].split(""",""" )] # We will have a final empty element if the line finished with a comma. if len(keys[-1] ) == 0: UpperCamelCase : List[Any] = keys[:-1] UpperCamelCase : int = get_indent(lines[1] ) + """, """.join([f'"{k}"' for k in sort_objects(snake_case_ )] ) return "\n".join(snake_case_ ) else: # Finally we have to deal with imports fitting on one line UpperCamelCase : List[str] = _re_bracket_content.sub(_replace ,snake_case_ ) return import_statement def A_ ( snake_case_ : Tuple ,snake_case_ : str=True ): '''simple docstring''' with open(snake_case_ ,"""r""" ) as f: UpperCamelCase : int = f.read() if "_import_structure" not in code: return # Blocks of indent level 0 UpperCamelCase : Dict = split_code_in_indented_blocks( snake_case_ ,start_prompt="""_import_structure = {""" ,end_prompt="""if TYPE_CHECKING:""" ) # We ignore block 0 (everything until start_prompt) and the last block (everything after end_prompt). for block_idx in range(1 ,len(snake_case_ ) - 1 ): # Check if the block contains some `_import_structure`s thingy to sort. UpperCamelCase : Optional[Any] = main_blocks[block_idx] UpperCamelCase : Optional[int] = block.split("""\n""" ) # Get to the start of the imports. UpperCamelCase : Union[str, Any] = 0 while line_idx < len(snake_case_ ) and "_import_structure" not in block_lines[line_idx]: # Skip dummy import blocks if "import dummy" in block_lines[line_idx]: UpperCamelCase : List[str] = len(snake_case_ ) else: line_idx += 1 if line_idx >= len(snake_case_ ): continue # Ignore beginning and last line: they don't contain anything. UpperCamelCase : Dict = """\n""".join(block_lines[line_idx:-1] ) UpperCamelCase : Union[str, Any] = get_indent(block_lines[1] ) # Slit the internal block into blocks of indent level 1. UpperCamelCase : Optional[int] = split_code_in_indented_blocks(snake_case_ ,indent_level=snake_case_ ) # We have two categories of import key: list or _import_structure[key].append/extend UpperCamelCase : Union[str, Any] = _re_direct_key if """_import_structure""" in block_lines[0] else _re_indirect_key # Grab the keys, but there is a trap: some lines are empty or just comments. UpperCamelCase : Union[str, Any] = [(pattern.search(snake_case_ ).groups()[0] if pattern.search(snake_case_ ) is not None else None) for b in internal_blocks] # We only sort the lines with a key. UpperCamelCase : Optional[Any] = [(i, key) for i, key in enumerate(snake_case_ ) if key is not None] UpperCamelCase : List[Any] = [x[0] for x in sorted(snake_case_ ,key=lambda snake_case_ : x[1] )] # We reorder the blocks by leaving empty lines/comments as they were and reorder the rest. UpperCamelCase : str = 0 UpperCamelCase : List[Any] = [] for i in range(len(snake_case_ ) ): if keys[i] is None: reordered_blocks.append(internal_blocks[i] ) else: UpperCamelCase : str = sort_objects_in_import(internal_blocks[sorted_indices[count]] ) reordered_blocks.append(snake_case_ ) count += 1 # And we put our main block back together with its first and last line. UpperCamelCase : Tuple = """\n""".join(block_lines[:line_idx] + reordered_blocks + [block_lines[-1]] ) if code != "\n".join(snake_case_ ): if check_only: return True else: print(f'Overwriting {file}.' ) with open(snake_case_ ,"""w""" ) as f: f.write("""\n""".join(snake_case_ ) ) def A_ ( snake_case_ : int=True ): '''simple docstring''' UpperCamelCase : Any = [] for root, _, files in os.walk(snake_case_ ): if "__init__.py" in files: UpperCamelCase : Union[str, Any] = sort_imports(os.path.join(snake_case_ ,"""__init__.py""" ) ,check_only=snake_case_ ) if result: UpperCamelCase : Any = [os.path.join(snake_case_ ,"""__init__.py""" )] if len(snake_case_ ) > 0: raise ValueError(f'Would overwrite {len(snake_case_ )} files, run `make style`.' ) if __name__ == "__main__": __A : Any = argparse.ArgumentParser() parser.add_argument('''--check_only''', action='''store_true''', help='''Whether to only check or fix style.''') __A : str = parser.parse_args() sort_imports_in_all_inits(check_only=args.check_only)
27
0
import random import unittest import numpy as np import torch from transformers import CLIPTextConfig, CLIPTextModel, CLIPTokenizer from diffusers import ( AutoencoderKL, DDIMScheduler, UNetaDConditionModel, VideoToVideoSDPipeline, ) from diffusers.utils import floats_tensor, is_xformers_available, skip_mps from diffusers.utils.testing_utils import enable_full_determinism, slow, torch_device from ..pipeline_params import ( TEXT_GUIDED_IMAGE_VARIATION_BATCH_PARAMS, TEXT_GUIDED_IMAGE_VARIATION_PARAMS, ) from ..test_pipelines_common import PipelineTesterMixin enable_full_determinism() @skip_mps class lowercase ( UpperCamelCase__,unittest.TestCase ): _a = VideoToVideoSDPipeline _a = TEXT_GUIDED_IMAGE_VARIATION_PARAMS.union({"video"} ) - {"image", "width", "height"} _a = TEXT_GUIDED_IMAGE_VARIATION_BATCH_PARAMS.union({"video"} ) - {"image"} _a = PipelineTesterMixin.required_optional_params - {"latents"} _a = False # No `output_type`. _a = frozenset( [ "num_inference_steps", "generator", "latents", "return_dict", "callback", "callback_steps", ] ) def a__ ( self ) -> Optional[Any]: torch.manual_seed(0 ) _A : Tuple = UNetaDConditionModel( block_out_channels=(32, 64, 64, 64) , layers_per_block=2 , sample_size=32 , in_channels=4 , out_channels=4 , down_block_types=("""CrossAttnDownBlock3D""", """CrossAttnDownBlock3D""", """CrossAttnDownBlock3D""", """DownBlock3D""") , up_block_types=("""UpBlock3D""", """CrossAttnUpBlock3D""", """CrossAttnUpBlock3D""", """CrossAttnUpBlock3D""") , cross_attention_dim=32 , attention_head_dim=4 , ) _A : int = DDIMScheduler( beta_start=0.00085 , beta_end=0.012 , beta_schedule="""scaled_linear""" , clip_sample=_a , set_alpha_to_one=_a , ) torch.manual_seed(0 ) _A : str = AutoencoderKL( block_out_channels=[32, 64] , in_channels=3 , out_channels=3 , down_block_types=["""DownEncoderBlock2D""", """DownEncoderBlock2D"""] , up_block_types=["""UpDecoderBlock2D""", """UpDecoderBlock2D"""] , latent_channels=4 , sample_size=128 , ) torch.manual_seed(0 ) _A : Tuple = CLIPTextConfig( bos_token_id=0 , eos_token_id=2 , hidden_size=32 , intermediate_size=37 , layer_norm_eps=1e-05 , num_attention_heads=4 , num_hidden_layers=5 , pad_token_id=1 , vocab_size=1000 , hidden_act="""gelu""" , projection_dim=512 , ) _A : Optional[Any] = CLIPTextModel(_a ) _A : Optional[int] = CLIPTokenizer.from_pretrained("""hf-internal-testing/tiny-random-clip""" ) _A : List[Any] = { """unet""": unet, """scheduler""": scheduler, """vae""": vae, """text_encoder""": text_encoder, """tokenizer""": tokenizer, } return components def a__ ( self , _a , _a=0 ) -> List[Any]: # 3 frames _A : List[str] = floats_tensor((1, 3, 3, 32, 32) , rng=random.Random(_a ) ).to(_a ) if str(_a ).startswith("""mps""" ): _A : Optional[int] = torch.manual_seed(_a ) else: _A : Any = torch.Generator(device=_a ).manual_seed(_a ) _A : Optional[int] = { """prompt""": """A painting of a squirrel eating a burger""", """video""": video, """generator""": generator, """num_inference_steps""": 2, """guidance_scale""": 6.0, """output_type""": """pt""", } return inputs def a__ ( self ) -> int: _A : Any = """cpu""" # ensure determinism for the device-dependent torch.Generator _A : str = self.get_dummy_components() _A : Optional[int] = VideoToVideoSDPipeline(**_a ) _A : List[Any] = sd_pipe.to(_a ) sd_pipe.set_progress_bar_config(disable=_a ) _A : int = self.get_dummy_inputs(_a ) _A : Optional[int] = """np""" _A : int = sd_pipe(**_a ).frames _A : Union[str, Any] = frames[0][-3:, -3:, -1] assert frames[0].shape == (32, 32, 3) _A : Dict = np.array([106, 117, 113, 174, 137, 112, 148, 151, 131] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-2 @unittest.skipIf( torch_device != """cuda""" or not is_xformers_available() , reason="""XFormers attention is only available with CUDA and `xformers` installed""" , ) def a__ ( self ) -> Optional[int]: self._test_xformers_attention_forwardGenerator_pass(test_mean_pixel_difference=_a , expected_max_diff=5e-3 ) @unittest.skip(reason="""Batching needs to be properly figured out first for this pipeline.""" ) def a__ ( self ) -> Tuple: pass @unittest.skip(reason="""Batching needs to be properly figured out first for this pipeline.""" ) def a__ ( self ) -> Dict: pass @unittest.skip(reason="""`num_images_per_prompt` argument is not supported for this pipeline.""" ) def a__ ( self ) -> List[Any]: pass def a__ ( self ) -> List[str]: return super().test_progress_bar() @slow @skip_mps class lowercase ( unittest.TestCase ): def a__ ( self ) -> Any: _A : Union[str, Any] = VideoToVideoSDPipeline.from_pretrained("""cerspense/zeroscope_v2_XL""" , torch_dtype=torch.floataa ) pipe.enable_model_cpu_offload() # 10 frames _A : List[Any] = torch.Generator(device="""cpu""" ).manual_seed(0 ) _A : int = torch.randn((1, 10, 3, 1024, 576) , generator=_a ) _A : int = video.to("""cuda""" ) _A : Optional[Any] = """Spiderman is surfing""" _A : List[Any] = pipe(_a , video=_a , generator=_a , num_inference_steps=3 , output_type="""pt""" ).frames _A : Dict = np.array([-1.0458984, -1.1279297, -0.9663086, -0.91503906, -0.75097656] ) assert np.abs(video_frames.cpu().numpy()[0, 0, 0, 0, -5:] - expected_array ).sum() < 1e-2
26
import torch from diffusers import DPMSolverSDEScheduler from diffusers.utils import torch_device from diffusers.utils.testing_utils import require_torchsde from .test_schedulers import SchedulerCommonTest @require_torchsde class lowercase ( UpperCamelCase__ ): _a = (DPMSolverSDEScheduler,) _a = 1_0 def a__ ( self , **_a ) -> Optional[Any]: _A : str = { """num_train_timesteps""": 1100, """beta_start""": 0.0001, """beta_end""": 0.02, """beta_schedule""": """linear""", """noise_sampler_seed""": 0, } config.update(**_a ) return config def a__ ( self ) -> Tuple: for timesteps in [10, 50, 100, 1000]: self.check_over_configs(num_train_timesteps=_a ) def a__ ( self ) -> Optional[int]: for beta_start, beta_end in zip([0.00001, 0.0001, 0.001] , [0.0002, 0.002, 0.02] ): self.check_over_configs(beta_start=_a , beta_end=_a ) def a__ ( self ) -> Any: for schedule in ["linear", "scaled_linear"]: self.check_over_configs(beta_schedule=_a ) def a__ ( self ) -> Optional[int]: for prediction_type in ["epsilon", "v_prediction"]: self.check_over_configs(prediction_type=_a ) def a__ ( self ) -> Optional[int]: _A : Any = self.scheduler_classes[0] _A : List[str] = self.get_scheduler_config() _A : Optional[Any] = scheduler_class(**_a ) scheduler.set_timesteps(self.num_inference_steps ) _A : Dict = self.dummy_model() _A : Any = self.dummy_sample_deter * scheduler.init_noise_sigma _A : Dict = sample.to(_a ) for i, t in enumerate(scheduler.timesteps ): _A : Optional[int] = scheduler.scale_model_input(_a , _a ) _A : str = model(_a , _a ) _A : List[Any] = scheduler.step(_a , _a , _a ) _A : Optional[int] = output.prev_sample _A : Dict = torch.sum(torch.abs(_a ) ) _A : Dict = torch.mean(torch.abs(_a ) ) if torch_device in ["mps"]: assert abs(result_sum.item() - 167.47821044921875 ) < 1e-2 assert abs(result_mean.item() - 0.2178705964565277 ) < 1e-3 elif torch_device in ["cuda"]: assert abs(result_sum.item() - 171.59352111816406 ) < 1e-2 assert abs(result_mean.item() - 0.22342906892299652 ) < 1e-3 else: assert abs(result_sum.item() - 162.52383422851562 ) < 1e-2 assert abs(result_mean.item() - 0.211619570851326 ) < 1e-3 def a__ ( self ) -> Optional[Any]: _A : Dict = self.scheduler_classes[0] _A : Optional[int] = self.get_scheduler_config(prediction_type="""v_prediction""" ) _A : Optional[Any] = scheduler_class(**_a ) scheduler.set_timesteps(self.num_inference_steps ) _A : Tuple = self.dummy_model() _A : int = self.dummy_sample_deter * scheduler.init_noise_sigma _A : Tuple = sample.to(_a ) for i, t in enumerate(scheduler.timesteps ): _A : int = scheduler.scale_model_input(_a , _a ) _A : Tuple = model(_a , _a ) _A : Dict = scheduler.step(_a , _a , _a ) _A : Optional[int] = output.prev_sample _A : Optional[Any] = torch.sum(torch.abs(_a ) ) _A : List[Any] = torch.mean(torch.abs(_a ) ) if torch_device in ["mps"]: assert abs(result_sum.item() - 124.77149200439453 ) < 1e-2 assert abs(result_mean.item() - 0.16226289014816284 ) < 1e-3 elif torch_device in ["cuda"]: assert abs(result_sum.item() - 128.1663360595703 ) < 1e-2 assert abs(result_mean.item() - 0.16688326001167297 ) < 1e-3 else: assert abs(result_sum.item() - 119.8487548828125 ) < 1e-2 assert abs(result_mean.item() - 0.1560530662536621 ) < 1e-3 def a__ ( self ) -> List[str]: _A : Union[str, Any] = self.scheduler_classes[0] _A : List[Any] = self.get_scheduler_config() _A : List[str] = scheduler_class(**_a ) scheduler.set_timesteps(self.num_inference_steps , device=_a ) _A : Union[str, Any] = self.dummy_model() _A : Optional[Any] = self.dummy_sample_deter.to(_a ) * scheduler.init_noise_sigma for t in scheduler.timesteps: _A : int = scheduler.scale_model_input(_a , _a ) _A : List[Any] = model(_a , _a ) _A : Dict = scheduler.step(_a , _a , _a ) _A : Dict = output.prev_sample _A : str = torch.sum(torch.abs(_a ) ) _A : str = torch.mean(torch.abs(_a ) ) if torch_device in ["mps"]: assert abs(result_sum.item() - 167.46957397460938 ) < 1e-2 assert abs(result_mean.item() - 0.21805934607982635 ) < 1e-3 elif torch_device in ["cuda"]: assert abs(result_sum.item() - 171.59353637695312 ) < 1e-2 assert abs(result_mean.item() - 0.22342908382415771 ) < 1e-3 else: assert abs(result_sum.item() - 162.52383422851562 ) < 1e-2 assert abs(result_mean.item() - 0.211619570851326 ) < 1e-3 def a__ ( self ) -> Union[str, Any]: _A : List[Any] = self.scheduler_classes[0] _A : Optional[Any] = self.get_scheduler_config() _A : int = scheduler_class(**_a , use_karras_sigmas=_a ) scheduler.set_timesteps(self.num_inference_steps , device=_a ) _A : Optional[Any] = self.dummy_model() _A : Dict = self.dummy_sample_deter.to(_a ) * scheduler.init_noise_sigma _A : str = sample.to(_a ) for t in scheduler.timesteps: _A : Optional[int] = scheduler.scale_model_input(_a , _a ) _A : List[Any] = model(_a , _a ) _A : Dict = scheduler.step(_a , _a , _a ) _A : List[str] = output.prev_sample _A : str = torch.sum(torch.abs(_a ) ) _A : List[str] = torch.mean(torch.abs(_a ) ) if torch_device in ["mps"]: assert abs(result_sum.item() - 176.66974135742188 ) < 1e-2 assert abs(result_mean.item() - 0.23003872730981811 ) < 1e-2 elif torch_device in ["cuda"]: assert abs(result_sum.item() - 177.63653564453125 ) < 1e-2 assert abs(result_mean.item() - 0.23003872730981811 ) < 1e-2 else: assert abs(result_sum.item() - 170.3135223388672 ) < 1e-2 assert abs(result_mean.item() - 0.23003872730981811 ) < 1e-2
26
1
from typing import Optional, Tuple, Union import flax import flax.linen as nn import jax import jax.numpy as jnp from flax.core.frozen_dict import FrozenDict from ..configuration_utils import ConfigMixin, flax_register_to_config from ..utils import BaseOutput from .embeddings_flax import FlaxTimestepEmbedding, FlaxTimesteps from .modeling_flax_utils import FlaxModelMixin from .unet_ad_blocks_flax import ( FlaxCrossAttnDownBlockaD, FlaxDownBlockaD, FlaxUNetMidBlockaDCrossAttn, ) @flax.struct.dataclass class a__ ( UpperCamelCase__ ): a : jnp.ndarray a : jnp.ndarray class a__ ( nn.Module ): a : int a : Tuple[int] = (16, 32, 96, 256) a : jnp.dtype = jnp.floataa def lowerCAmelCase_ ( self ) -> List[Any]: '''simple docstring''' a = nn.Conv( self.block_out_channels[0] , kernel_size=(3, 3) , padding=((1, 1), (1, 1)) , dtype=self.dtype , ) a = [] for i in range(len(self.block_out_channels ) - 1 ): a = self.block_out_channels[i] a = self.block_out_channels[i + 1] a = nn.Conv( A , kernel_size=(3, 3) , padding=((1, 1), (1, 1)) , dtype=self.dtype , ) blocks.append(A ) a = nn.Conv( A , kernel_size=(3, 3) , strides=(2, 2) , padding=((1, 1), (1, 1)) , dtype=self.dtype , ) blocks.append(A ) a = blocks a = nn.Conv( self.conditioning_embedding_channels , kernel_size=(3, 3) , padding=((1, 1), (1, 1)) , kernel_init=nn.initializers.zeros_init() , bias_init=nn.initializers.zeros_init() , dtype=self.dtype , ) def __call__( self , A ) -> Optional[Any]: '''simple docstring''' a = self.conv_in(A ) a = nn.silu(A ) for block in self.blocks: a = block(A ) a = nn.silu(A ) a = self.conv_out(A ) return embedding @flax_register_to_config class a__ ( nn.Module , UpperCamelCase__ , UpperCamelCase__ ): a : int = 32 a : int = 4 a : Tuple[str] = ( "CrossAttnDownBlock2D", "CrossAttnDownBlock2D", "CrossAttnDownBlock2D", "DownBlock2D", ) a : Union[bool, Tuple[bool]] = False a : Tuple[int] = (320, 640, 1280, 1280) a : int = 2 a : Union[int, Tuple[int]] = 8 a : Optional[Union[int, Tuple[int]]] = None a : int = 1280 a : float = 0.0 a : bool = False a : jnp.dtype = jnp.floataa a : bool = True a : int = 0 a : str = "rgb" a : Tuple[int] = (16, 32, 96, 256) def lowerCAmelCase_ ( self , A ) -> FrozenDict: '''simple docstring''' a = (1, self.in_channels, self.sample_size, self.sample_size) a = jnp.zeros(A , dtype=jnp.floataa ) a = jnp.ones((1,) , dtype=jnp.intaa ) a = jnp.zeros((1, 1, self.cross_attention_dim) , dtype=jnp.floataa ) a = (1, 3, self.sample_size * 8, self.sample_size * 8) a = jnp.zeros(A , dtype=jnp.floataa ) a , a = jax.random.split(A ) a = {"params": params_rng, "dropout": dropout_rng} return self.init(A , A , A , A , A )["params"] def lowerCAmelCase_ ( self ) -> int: '''simple docstring''' a = self.block_out_channels a = block_out_channels[0] * 4 # If `num_attention_heads` is not defined (which is the case for most models) # it will default to `attention_head_dim`. This looks weird upon first reading it and it is. # The reason for this behavior is to correct for incorrectly named variables that were introduced # when this library was created. The incorrect naming was only discovered much later in https://github.com/huggingface/diffusers/issues/2011#issuecomment-1547958131 # Changing `attention_head_dim` to `num_attention_heads` for 40,000+ configurations is too backwards breaking # which is why we correct for the naming here. a = self.num_attention_heads or self.attention_head_dim # input a = nn.Conv( block_out_channels[0] , kernel_size=(3, 3) , strides=(1, 1) , padding=((1, 1), (1, 1)) , dtype=self.dtype , ) # time a = FlaxTimesteps( block_out_channels[0] , flip_sin_to_cos=self.flip_sin_to_cos , freq_shift=self.config.freq_shift ) a = FlaxTimestepEmbedding(A , dtype=self.dtype ) a = FlaxControlNetConditioningEmbedding( conditioning_embedding_channels=block_out_channels[0] , block_out_channels=self.conditioning_embedding_out_channels , ) a = self.only_cross_attention if isinstance(A , A ): a = (only_cross_attention,) * len(self.down_block_types ) if isinstance(A , A ): a = (num_attention_heads,) * len(self.down_block_types ) # down a = [] a = [] a = block_out_channels[0] a = nn.Conv( A , kernel_size=(1, 1) , padding="VALID" , kernel_init=nn.initializers.zeros_init() , bias_init=nn.initializers.zeros_init() , dtype=self.dtype , ) controlnet_down_blocks.append(A ) for i, down_block_type in enumerate(self.down_block_types ): a = output_channel a = block_out_channels[i] a = i == len(A ) - 1 if down_block_type == "CrossAttnDownBlock2D": a = FlaxCrossAttnDownBlockaD( in_channels=A , out_channels=A , dropout=self.dropout , num_layers=self.layers_per_block , num_attention_heads=num_attention_heads[i] , add_downsample=not is_final_block , use_linear_projection=self.use_linear_projection , only_cross_attention=only_cross_attention[i] , dtype=self.dtype , ) else: a = FlaxDownBlockaD( in_channels=A , out_channels=A , dropout=self.dropout , num_layers=self.layers_per_block , add_downsample=not is_final_block , dtype=self.dtype , ) down_blocks.append(A ) for _ in range(self.layers_per_block ): a = nn.Conv( A , kernel_size=(1, 1) , padding="VALID" , kernel_init=nn.initializers.zeros_init() , bias_init=nn.initializers.zeros_init() , dtype=self.dtype , ) controlnet_down_blocks.append(A ) if not is_final_block: a = nn.Conv( A , kernel_size=(1, 1) , padding="VALID" , kernel_init=nn.initializers.zeros_init() , bias_init=nn.initializers.zeros_init() , dtype=self.dtype , ) controlnet_down_blocks.append(A ) a = down_blocks a = controlnet_down_blocks # mid a = block_out_channels[-1] a = FlaxUNetMidBlockaDCrossAttn( in_channels=A , dropout=self.dropout , num_attention_heads=num_attention_heads[-1] , use_linear_projection=self.use_linear_projection , dtype=self.dtype , ) a = nn.Conv( A , kernel_size=(1, 1) , padding="VALID" , kernel_init=nn.initializers.zeros_init() , bias_init=nn.initializers.zeros_init() , dtype=self.dtype , ) def __call__( self , A , A , A , A , A = 1.0 , A = True , A = False , ) -> Union[FlaxControlNetOutput, Tuple]: '''simple docstring''' a = self.controlnet_conditioning_channel_order if channel_order == "bgr": a = jnp.flip(A , axis=1 ) # 1. time if not isinstance(A , jnp.ndarray ): a = jnp.array([timesteps] , dtype=jnp.intaa ) elif isinstance(A , jnp.ndarray ) and len(timesteps.shape ) == 0: a = timesteps.astype(dtype=jnp.floataa ) a = jnp.expand_dims(A , 0 ) a = self.time_proj(A ) a = self.time_embedding(A ) # 2. pre-process a = jnp.transpose(A , (0, 2, 3, 1) ) a = self.conv_in(A ) a = jnp.transpose(A , (0, 2, 3, 1) ) a = self.controlnet_cond_embedding(A ) sample += controlnet_cond # 3. down a = (sample,) for down_block in self.down_blocks: if isinstance(A , A ): a , a = down_block(A , A , A , deterministic=not train ) else: a , a = down_block(A , A , deterministic=not train ) down_block_res_samples += res_samples # 4. mid a = self.mid_block(A , A , A , deterministic=not train ) # 5. contronet blocks a = () for down_block_res_sample, controlnet_block in zip(A , self.controlnet_down_blocks ): a = controlnet_block(A ) controlnet_down_block_res_samples += (down_block_res_sample,) a = controlnet_down_block_res_samples a = self.controlnet_mid_block(A ) # 6. scaling a = [sample * conditioning_scale for sample in down_block_res_samples] mid_block_res_sample *= conditioning_scale if not return_dict: return (down_block_res_samples, mid_block_res_sample) return FlaxControlNetOutput( down_block_res_samples=A , mid_block_res_sample=A )
180
from collections import OrderedDict from typing import Mapping from packaging import version from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging from ...utils.backbone_utils import BackboneConfigMixin, get_aligned_output_features_output_indices lowercase__ : Tuple = logging.get_logger(__name__) lowercase__ : int = { "microsoft/resnet-50": "https://huggingface.co/microsoft/resnet-50/blob/main/config.json", } class a__ ( UpperCamelCase__ , UpperCamelCase__ ): a : Any = """resnet""" a : Tuple = ["""basic""", """bottleneck"""] def __init__( self , A=3 , A=64 , A=[256, 512, 1024, 2048] , A=[3, 4, 6, 3] , A="bottleneck" , A="relu" , A=False , A=None , A=None , **A , ) -> Any: '''simple docstring''' super().__init__(**A ) if layer_type not in self.layer_types: raise ValueError(F'''layer_type={layer_type} is not one of {",".join(self.layer_types )}''' ) a = num_channels a = embedding_size a = hidden_sizes a = depths a = layer_type a = hidden_act a = downsample_in_first_stage a = ["stem"] + [F'''stage{idx}''' for idx in range(1 , len(A ) + 1 )] a , a = get_aligned_output_features_output_indices( out_features=A , out_indices=A , stage_names=self.stage_names ) class a__ ( UpperCamelCase__ ): a : Optional[int] = version.parse("""1.11""" ) @property def lowerCAmelCase_ ( self ) -> Mapping[str, Mapping[int, str]]: '''simple docstring''' return OrderedDict( [ ("pixel_values", {0: "batch", 1: "num_channels", 2: "height", 3: "width"}), ] ) @property def lowerCAmelCase_ ( self ) -> float: '''simple docstring''' return 1e-3
180
1
"""simple docstring""" from __future__ import annotations from collections.abc import Sequence from typing import Literal def _snake_case ( lowercase__ , lowercase__ ): _lowerCamelCase : Optional[int] = list(lowerCamelCase_ ) _lowerCamelCase : Tuple = list(lowerCamelCase_ ) _lowerCamelCase : Optional[Any] = 0 for i in range(len(lowerCamelCase_ ) ): if lista[i] != lista[i]: count += 1 _lowerCamelCase : int = '_' if count > 1: return False else: return "".join(lowerCamelCase_ ) def _snake_case ( lowercase__ ): _lowerCamelCase : str = [] while True: _lowerCamelCase : Tuple = ['$'] * len(lowerCamelCase_ ) _lowerCamelCase : Optional[int] = [] for i in range(len(lowerCamelCase_ ) ): for j in range(i + 1 , len(lowerCamelCase_ ) ): _lowerCamelCase : Optional[int] = compare_string(binary[i] , binary[j] ) if k is False: _lowerCamelCase : Optional[Any] = '*' _lowerCamelCase : List[Any] = '*' temp.append('X' ) for i in range(len(lowerCamelCase_ ) ): if checka[i] == "$": pi.append(binary[i] ) if len(lowerCamelCase_ ) == 0: return pi _lowerCamelCase : Tuple = list(set(lowerCamelCase_ ) ) def _snake_case ( lowercase__ , lowercase__ ): _lowerCamelCase : int = [] for minterm in minterms: _lowerCamelCase : Union[str, Any] = '' for _ in range(lowerCamelCase_ ): _lowerCamelCase : Optional[int] = str(minterm % 2 ) + string minterm //= 2 temp.append(lowerCamelCase_ ) return temp def _snake_case ( lowercase__ , lowercase__ , lowercase__ ): _lowerCamelCase : List[Any] = list(lowerCamelCase_ ) _lowerCamelCase : Any = list(lowerCamelCase_ ) _lowerCamelCase : List[str] = 0 for i in range(len(lowerCamelCase_ ) ): if lista[i] != lista[i]: count_n += 1 return count_n == count def _snake_case ( lowercase__ , lowercase__ ): _lowerCamelCase : Dict = [] _lowerCamelCase : Optional[int] = [0] * len(lowerCamelCase_ ) for i in range(len(chart[0] ) ): _lowerCamelCase : int = 0 _lowerCamelCase : Union[str, Any] = -1 for j in range(len(lowerCamelCase_ ) ): if chart[j][i] == 1: count += 1 _lowerCamelCase : Tuple = j if count == 1: _lowerCamelCase : Dict = 1 for i in range(len(lowerCamelCase_ ) ): if select[i] == 1: for j in range(len(chart[0] ) ): if chart[i][j] == 1: for k in range(len(lowerCamelCase_ ) ): _lowerCamelCase : List[Any] = 0 temp.append(prime_implicants[i] ) while True: _lowerCamelCase : str = 0 _lowerCamelCase : int = -1 _lowerCamelCase : Union[str, Any] = 0 for i in range(len(lowerCamelCase_ ) ): _lowerCamelCase : Any = chart[i].count(1 ) if count_n > max_n: _lowerCamelCase : Union[str, Any] = count_n _lowerCamelCase : int = i if max_n == 0: return temp temp.append(prime_implicants[rem] ) for i in range(len(chart[0] ) ): if chart[rem][i] == 1: for j in range(len(lowerCamelCase_ ) ): _lowerCamelCase : Any = 0 def _snake_case ( lowercase__ , lowercase__ ): _lowerCamelCase : Any = [[0 for x in range(len(lowerCamelCase_ ) )] for x in range(len(lowerCamelCase_ ) )] for i in range(len(lowerCamelCase_ ) ): _lowerCamelCase : Optional[Any] = prime_implicants[i].count('_' ) for j in range(len(lowerCamelCase_ ) ): if is_for_table(prime_implicants[i] , binary[j] , lowerCamelCase_ ): _lowerCamelCase : Tuple = 1 return chart def _snake_case ( ): _lowerCamelCase : Optional[Any] = int(input('Enter the no. of variables\n' ) ) _lowerCamelCase : Tuple = [ float(lowerCamelCase_ ) for x in input( 'Enter the decimal representation of Minterms \'Spaces Separated\'\n' ).split() ] _lowerCamelCase : Dict = decimal_to_binary(lowerCamelCase_ , lowerCamelCase_ ) _lowerCamelCase : Optional[Any] = check(lowerCamelCase_ ) print('Prime Implicants are:' ) print(lowerCamelCase_ ) _lowerCamelCase : Any = prime_implicant_chart(lowerCamelCase_ , lowerCamelCase_ ) _lowerCamelCase : List[str] = selection(lowerCamelCase_ , lowerCamelCase_ ) print('Essential Prime Implicants are:' ) print(lowerCamelCase_ ) if __name__ == "__main__": import doctest doctest.testmod() main()
96
def a ( lowerCamelCase_ ): '''simple docstring''' lowercase__ = [1] lowercase__ , lowercase__ , lowercase__ = 0, 0, 0 lowercase__ = ugly_nums[ia] * 2 lowercase__ = ugly_nums[ia] * 3 lowercase__ = ugly_nums[ia] * 5 for _ in range(1 , lowerCamelCase_ ): lowercase__ = min(lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ ) ugly_nums.append(lowerCamelCase_ ) if next_num == next_a: ia += 1 lowercase__ = ugly_nums[ia] * 2 if next_num == next_a: ia += 1 lowercase__ = ugly_nums[ia] * 3 if next_num == next_a: ia += 1 lowercase__ = ugly_nums[ia] * 5 return ugly_nums[-1] if __name__ == "__main__": from doctest import testmod testmod(verbose=True) print(F"{ugly_numbers(2_00) = }")
207
0
import sys from typing import Tuple import numpy as np import torch from PIL import Image from torch import nn from transformers.image_utils import PILImageResampling from utils import img_tensorize class _a : def __init__(self, SCREAMING_SNAKE_CASE_, SCREAMING_SNAKE_CASE_=sys.maxsize ) -> Optional[Any]: UpperCAmelCase_: Optional[int] = """bilinear""" UpperCAmelCase_: Any = max_size UpperCAmelCase_: Union[str, Any] = short_edge_length def __call__(self, SCREAMING_SNAKE_CASE_ ) -> Optional[Any]: UpperCAmelCase_: List[Any] = [] for img in imgs: UpperCAmelCase_: List[Any] = img.shape[:2] # later: provide list and randomly choose index for resize UpperCAmelCase_: Tuple = np.random.randint(self.short_edge_length[0], self.short_edge_length[1] + 1 ) if size == 0: return img UpperCAmelCase_: Optional[int] = size * 1.0 / min(SCREAMING_SNAKE_CASE_, SCREAMING_SNAKE_CASE_ ) if h < w: UpperCAmelCase_: Optional[int] = size, scale * w else: UpperCAmelCase_: List[Any] = scale * h, size if max(SCREAMING_SNAKE_CASE_, SCREAMING_SNAKE_CASE_ ) > self.max_size: UpperCAmelCase_: List[Any] = self.max_size * 1.0 / max(SCREAMING_SNAKE_CASE_, SCREAMING_SNAKE_CASE_ ) UpperCAmelCase_: int = newh * scale UpperCAmelCase_: List[Any] = neww * scale UpperCAmelCase_: List[str] = int(neww + 0.5 ) UpperCAmelCase_: Tuple = int(newh + 0.5 ) if img.dtype == np.uinta: UpperCAmelCase_: str = Image.fromarray(SCREAMING_SNAKE_CASE_ ) UpperCAmelCase_: Dict = pil_image.resize((neww, newh), PILImageResampling.BILINEAR ) UpperCAmelCase_: Union[str, Any] = np.asarray(SCREAMING_SNAKE_CASE_ ) else: UpperCAmelCase_: List[Any] = img.permute(2, 0, 1 ).unsqueeze(0 ) # 3, 0, 1) # hw(c) -> nchw UpperCAmelCase_: Dict = nn.functional.interpolate( SCREAMING_SNAKE_CASE_, (newh, neww), mode=self.interp_method, align_corners=SCREAMING_SNAKE_CASE_ ).squeeze(0 ) img_augs.append(SCREAMING_SNAKE_CASE_ ) return img_augs class _a : def __init__(self, SCREAMING_SNAKE_CASE_ ) -> Optional[Any]: UpperCAmelCase_: Optional[int] = ResizeShortestEdge([cfg.INPUT.MIN_SIZE_TEST, cfg.INPUT.MIN_SIZE_TEST], cfg.INPUT.MAX_SIZE_TEST ) UpperCAmelCase_: Union[str, Any] = cfg.INPUT.FORMAT UpperCAmelCase_: List[Any] = cfg.SIZE_DIVISIBILITY UpperCAmelCase_: str = cfg.PAD_VALUE UpperCAmelCase_: Optional[Any] = cfg.INPUT.MAX_SIZE_TEST UpperCAmelCase_: Optional[Any] = cfg.MODEL.DEVICE UpperCAmelCase_: List[str] = torch.tensor(cfg.MODEL.PIXEL_STD ).to(self.device ).view(len(cfg.MODEL.PIXEL_STD ), 1, 1 ) UpperCAmelCase_: Union[str, Any] = torch.tensor(cfg.MODEL.PIXEL_MEAN ).to(self.device ).view(len(cfg.MODEL.PIXEL_STD ), 1, 1 ) UpperCAmelCase_: Tuple = lambda SCREAMING_SNAKE_CASE_ : (x - self.pixel_mean) / self.pixel_std def __snake_case (self, SCREAMING_SNAKE_CASE_ ) -> List[str]: UpperCAmelCase_: Optional[Any] = tuple(max(SCREAMING_SNAKE_CASE_ ) for s in zip(*[img.shape for img in images] ) ) UpperCAmelCase_: List[Any] = [im.shape[-2:] for im in images] UpperCAmelCase_: Optional[int] = [ nn.functional.pad( SCREAMING_SNAKE_CASE_, [0, max_size[-1] - size[1], 0, max_size[-2] - size[0]], value=self.pad_value, ) for size, im in zip(SCREAMING_SNAKE_CASE_, SCREAMING_SNAKE_CASE_ ) ] return torch.stack(SCREAMING_SNAKE_CASE_ ), torch.tensor(SCREAMING_SNAKE_CASE_ ) def __call__(self, SCREAMING_SNAKE_CASE_, SCREAMING_SNAKE_CASE_=False ) -> Tuple: with torch.no_grad(): if not isinstance(SCREAMING_SNAKE_CASE_, SCREAMING_SNAKE_CASE_ ): UpperCAmelCase_: str = [images] if single_image: assert len(SCREAMING_SNAKE_CASE_ ) == 1 for i in range(len(SCREAMING_SNAKE_CASE_ ) ): if isinstance(images[i], torch.Tensor ): images.insert(SCREAMING_SNAKE_CASE_, images.pop(SCREAMING_SNAKE_CASE_ ).to(self.device ).float() ) elif not isinstance(images[i], torch.Tensor ): images.insert( SCREAMING_SNAKE_CASE_, torch.as_tensor(img_tensorize(images.pop(SCREAMING_SNAKE_CASE_ ), input_format=self.input_format ) ) .to(self.device ) .float(), ) # resize smallest edge UpperCAmelCase_: List[Any] = torch.tensor([im.shape[:2] for im in images] ) UpperCAmelCase_: Any = self.aug(SCREAMING_SNAKE_CASE_ ) # transpose images and convert to torch tensors # images = [torch.as_tensor(i.astype("float32")).permute(2, 0, 1).to(self.device) for i in images] # now normalize before pad to avoid useless arithmetic UpperCAmelCase_: List[Any] = [self.normalizer(SCREAMING_SNAKE_CASE_ ) for x in images] # now pad them to do the following operations UpperCAmelCase_: Union[str, Any] = self.pad(SCREAMING_SNAKE_CASE_ ) # Normalize if self.size_divisibility > 0: raise NotImplementedError() # pad UpperCAmelCase_: Optional[int] = torch.true_divide(SCREAMING_SNAKE_CASE_, SCREAMING_SNAKE_CASE_ ) if single_image: return images[0], sizes[0], scales_yx[0] else: return images, sizes, scales_yx def lowerCAmelCase_ (lowerCAmelCase__: List[str] , lowerCAmelCase__: Any ): """simple docstring""" boxes[:, 0::2] *= scale_yx[:, 1] boxes[:, 1::2] *= scale_yx[:, 0] return boxes def lowerCAmelCase_ (lowerCAmelCase__: Optional[Any] , lowerCAmelCase__: Tuple[int, int] ): """simple docstring""" assert torch.isfinite(lowerCAmelCase__ ).all(), "Box tensor contains infinite or NaN!" UpperCAmelCase_: Dict = box_size tensor[:, 0].clamp_(min=0 , max=lowerCAmelCase__ ) tensor[:, 1].clamp_(min=0 , max=lowerCAmelCase__ ) tensor[:, 2].clamp_(min=0 , max=lowerCAmelCase__ ) tensor[:, 3].clamp_(min=0 , max=lowerCAmelCase__ )
362
from __future__ import annotations def lowerCAmelCase_ (lowerCAmelCase__: list[float] ): """simple docstring""" UpperCAmelCase_: Union[str, Any] = 0.00 UpperCAmelCase_: List[str] = 0 for resistor in resistors: if resistor <= 0: UpperCAmelCase_: Dict = F'Resistor at index {index} has a negative or zero value!' raise ValueError(lowerCAmelCase__ ) first_sum += 1 / float(lowerCAmelCase__ ) index += 1 return 1 / first_sum def lowerCAmelCase_ (lowerCAmelCase__: list[float] ): """simple docstring""" UpperCAmelCase_: Any = 0.00 UpperCAmelCase_: int = 0 for resistor in resistors: sum_r += resistor if resistor < 0: UpperCAmelCase_: int = F'Resistor at index {index} has a negative value!' raise ValueError(lowerCAmelCase__ ) index += 1 return sum_r if __name__ == "__main__": import doctest doctest.testmod()
82
0
"""simple docstring""" import unittest import numpy as np import torch from diffusers import DDIMPipeline, DDIMScheduler, UNetaDModel from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu, slow, torch_device from ..pipeline_params import UNCONDITIONAL_IMAGE_GENERATION_BATCH_PARAMS, UNCONDITIONAL_IMAGE_GENERATION_PARAMS from ..test_pipelines_common import PipelineTesterMixin enable_full_determinism() class _lowerCamelCase ( _lowercase , unittest.TestCase ): UpperCAmelCase_ = DDIMPipeline UpperCAmelCase_ = UNCONDITIONAL_IMAGE_GENERATION_PARAMS UpperCAmelCase_ = PipelineTesterMixin.required_optional_params - { "num_images_per_prompt", "latents", "callback", "callback_steps", } UpperCAmelCase_ = UNCONDITIONAL_IMAGE_GENERATION_BATCH_PARAMS UpperCAmelCase_ = False def snake_case_ (self ) -> Tuple: torch.manual_seed(0 ) UpperCamelCase = UNetaDModel( block_out_channels=(32, 64) , layers_per_block=2 , sample_size=32 , in_channels=3 , out_channels=3 , down_block_types=("DownBlock2D", "AttnDownBlock2D") , up_block_types=("AttnUpBlock2D", "UpBlock2D") , ) UpperCamelCase = DDIMScheduler() UpperCamelCase = {"unet": unet, "scheduler": scheduler} return components def snake_case_ (self , __a , __a=0 ) -> List[str]: if str(__a ).startswith("mps" ): UpperCamelCase = torch.manual_seed(__a ) else: UpperCamelCase = torch.Generator(device=__a ).manual_seed(__a ) UpperCamelCase = { "batch_size": 1, "generator": generator, "num_inference_steps": 2, "output_type": "numpy", } return inputs def snake_case_ (self ) -> Union[str, Any]: UpperCamelCase = "cpu" UpperCamelCase = self.get_dummy_components() UpperCamelCase = self.pipeline_class(**__a ) pipe.to(__a ) pipe.set_progress_bar_config(disable=__a ) UpperCamelCase = self.get_dummy_inputs(__a ) UpperCamelCase = pipe(**__a ).images UpperCamelCase = image[0, -3:, -3:, -1] self.assertEqual(image.shape , (1, 32, 32, 3) ) UpperCamelCase = np.array( [1.0_0_0e0_0, 5.7_1_7e-0_1, 4.7_1_7e-0_1, 1.0_0_0e0_0, 0.0_0_0e0_0, 1.0_0_0e0_0, 3.0_0_0e-0_4, 0.0_0_0e0_0, 9.0_0_0e-0_4] ) UpperCamelCase = np.abs(image_slice.flatten() - expected_slice ).max() self.assertLessEqual(__a , 1e-3 ) def snake_case_ (self ) -> Union[str, Any]: super().test_dict_tuple_outputs_equivalent(expected_max_difference=3e-3 ) def snake_case_ (self ) -> Dict: super().test_save_load_local(expected_max_difference=3e-3 ) def snake_case_ (self ) -> Optional[int]: super().test_save_load_optional_components(expected_max_difference=3e-3 ) def snake_case_ (self ) -> Union[str, Any]: super().test_inference_batch_single_identical(expected_max_diff=3e-3 ) @slow @require_torch_gpu class _lowerCamelCase ( unittest.TestCase ): def snake_case_ (self ) -> List[str]: UpperCamelCase = "google/ddpm-cifar10-32" UpperCamelCase = UNetaDModel.from_pretrained(__a ) UpperCamelCase = DDIMScheduler() UpperCamelCase = DDIMPipeline(unet=__a , scheduler=__a ) ddim.to(__a ) ddim.set_progress_bar_config(disable=__a ) UpperCamelCase = torch.manual_seed(0 ) UpperCamelCase = ddim(generator=__a , eta=0.0 , output_type="numpy" ).images UpperCamelCase = image[0, -3:, -3:, -1] assert image.shape == (1, 32, 32, 3) UpperCamelCase = np.array([0.1723, 0.1617, 0.1600, 0.1626, 0.1497, 0.1513, 0.1505, 0.1442, 0.1453] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-2 def snake_case_ (self ) -> Dict: UpperCamelCase = "google/ddpm-ema-bedroom-256" UpperCamelCase = UNetaDModel.from_pretrained(__a ) UpperCamelCase = DDIMScheduler.from_pretrained(__a ) UpperCamelCase = DDIMPipeline(unet=__a , scheduler=__a ) ddpm.to(__a ) ddpm.set_progress_bar_config(disable=__a ) UpperCamelCase = torch.manual_seed(0 ) UpperCamelCase = ddpm(generator=__a , output_type="numpy" ).images UpperCamelCase = image[0, -3:, -3:, -1] assert image.shape == (1, 2_56, 2_56, 3) UpperCamelCase = np.array([0.0060, 0.0201, 0.0344, 0.0024, 0.0018, 0.0002, 0.0022, 0.0000, 0.0069] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1e-2
153
"""simple docstring""" import gc import random import unittest import numpy as np import torch from diffusers import ( DDIMScheduler, KandinskyVaaControlnetPipeline, KandinskyVaaPriorPipeline, UNetaDConditionModel, VQModel, ) from diffusers.utils import floats_tensor, load_image, load_numpy, slow, torch_device from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu from ..test_pipelines_common import PipelineTesterMixin, assert_mean_pixel_difference enable_full_determinism() class _lowerCamelCase ( _lowercase , unittest.TestCase ): UpperCAmelCase_ = KandinskyVaaControlnetPipeline UpperCAmelCase_ = ["image_embeds", "negative_image_embeds", "hint"] UpperCAmelCase_ = ["image_embeds", "negative_image_embeds", "hint"] UpperCAmelCase_ = [ "generator", "height", "width", "latents", "guidance_scale", "num_inference_steps", "return_dict", "guidance_scale", "num_images_per_prompt", "output_type", "return_dict", ] UpperCAmelCase_ = False @property def snake_case_ (self ) -> Tuple: return 32 @property def snake_case_ (self ) -> Optional[int]: return 32 @property def snake_case_ (self ) -> int: return self.time_input_dim @property def snake_case_ (self ) -> Dict: return self.time_input_dim * 4 @property def snake_case_ (self ) -> List[str]: return 1_00 @property def snake_case_ (self ) -> Union[str, Any]: torch.manual_seed(0 ) UpperCamelCase = { "in_channels": 8, # Out channels is double in channels because predicts mean and variance "out_channels": 8, "addition_embed_type": "image_hint", "down_block_types": ("ResnetDownsampleBlock2D", "SimpleCrossAttnDownBlock2D"), "up_block_types": ("SimpleCrossAttnUpBlock2D", "ResnetUpsampleBlock2D"), "mid_block_type": "UNetMidBlock2DSimpleCrossAttn", "block_out_channels": (self.block_out_channels_a, self.block_out_channels_a * 2), "layers_per_block": 1, "encoder_hid_dim": self.text_embedder_hidden_size, "encoder_hid_dim_type": "image_proj", "cross_attention_dim": self.cross_attention_dim, "attention_head_dim": 4, "resnet_time_scale_shift": "scale_shift", "class_embed_type": None, } UpperCamelCase = UNetaDConditionModel(**__a ) return model @property def snake_case_ (self ) -> Dict: return { "block_out_channels": [32, 32, 64, 64], "down_block_types": [ "DownEncoderBlock2D", "DownEncoderBlock2D", "DownEncoderBlock2D", "AttnDownEncoderBlock2D", ], "in_channels": 3, "latent_channels": 4, "layers_per_block": 1, "norm_num_groups": 8, "norm_type": "spatial", "num_vq_embeddings": 12, "out_channels": 3, "up_block_types": ["AttnUpDecoderBlock2D", "UpDecoderBlock2D", "UpDecoderBlock2D", "UpDecoderBlock2D"], "vq_embed_dim": 4, } @property def snake_case_ (self ) -> Optional[Any]: torch.manual_seed(0 ) UpperCamelCase = VQModel(**self.dummy_movq_kwargs ) return model def snake_case_ (self ) -> Optional[Any]: UpperCamelCase = self.dummy_unet UpperCamelCase = self.dummy_movq UpperCamelCase = DDIMScheduler( num_train_timesteps=10_00 , beta_schedule="linear" , beta_start=0.00085 , beta_end=0.012 , clip_sample=__a , set_alpha_to_one=__a , steps_offset=1 , prediction_type="epsilon" , thresholding=__a , ) UpperCamelCase = { "unet": unet, "scheduler": scheduler, "movq": movq, } return components def snake_case_ (self , __a , __a=0 ) -> Any: UpperCamelCase = floats_tensor((1, self.text_embedder_hidden_size) , rng=random.Random(__a ) ).to(__a ) UpperCamelCase = floats_tensor((1, self.text_embedder_hidden_size) , rng=random.Random(seed + 1 ) ).to( __a ) # create hint UpperCamelCase = floats_tensor((1, 3, 64, 64) , rng=random.Random(__a ) ).to(__a ) if str(__a ).startswith("mps" ): UpperCamelCase = torch.manual_seed(__a ) else: UpperCamelCase = torch.Generator(device=__a ).manual_seed(__a ) UpperCamelCase = { "image_embeds": image_embeds, "negative_image_embeds": negative_image_embeds, "hint": hint, "generator": generator, "height": 64, "width": 64, "guidance_scale": 4.0, "num_inference_steps": 2, "output_type": "np", } return inputs def snake_case_ (self ) -> int: UpperCamelCase = "cpu" UpperCamelCase = self.get_dummy_components() UpperCamelCase = self.pipeline_class(**__a ) UpperCamelCase = pipe.to(__a ) pipe.set_progress_bar_config(disable=__a ) UpperCamelCase = pipe(**self.get_dummy_inputs(__a ) ) UpperCamelCase = output.images UpperCamelCase = pipe( **self.get_dummy_inputs(__a ) , return_dict=__a , )[0] UpperCamelCase = image[0, -3:, -3:, -1] UpperCamelCase = image_from_tuple[0, -3:, -3:, -1] assert image.shape == (1, 64, 64, 3) UpperCamelCase = np.array( [0.6959826, 0.868279, 0.7558092, 0.68769467, 0.85805804, 0.65977496, 0.44885302, 0.5959111, 0.4251595] ) assert ( np.abs(image_slice.flatten() - expected_slice ).max() < 1e-2 ), F" expected_slice {expected_slice}, but got {image_slice.flatten()}" assert ( np.abs(image_from_tuple_slice.flatten() - expected_slice ).max() < 1e-2 ), F" expected_slice {expected_slice}, but got {image_from_tuple_slice.flatten()}" @slow @require_torch_gpu class _lowerCamelCase ( unittest.TestCase ): def snake_case_ (self ) -> Optional[Any]: # clean up the VRAM after each test super().tearDown() gc.collect() torch.cuda.empty_cache() def snake_case_ (self ) -> Dict: UpperCamelCase = load_numpy( "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main" "/kandinskyv22/kandinskyv22_controlnet_robotcat_fp16.npy" ) UpperCamelCase = load_image( "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main" "/kandinskyv22/hint_image_cat.png" ) UpperCamelCase = torch.from_numpy(np.array(__a ) ).float() / 255.0 UpperCamelCase = hint.permute(2 , 0 , 1 ).unsqueeze(0 ) UpperCamelCase = KandinskyVaaPriorPipeline.from_pretrained( "kandinsky-community/kandinsky-2-2-prior" , torch_dtype=torch.floataa ) pipe_prior.to(__a ) UpperCamelCase = KandinskyVaaControlnetPipeline.from_pretrained( "kandinsky-community/kandinsky-2-2-controlnet-depth" , torch_dtype=torch.floataa ) UpperCamelCase = pipeline.to(__a ) pipeline.set_progress_bar_config(disable=__a ) UpperCamelCase = "A robot, 4k photo" UpperCamelCase = torch.Generator(device="cuda" ).manual_seed(0 ) UpperCamelCase , UpperCamelCase = pipe_prior( __a , generator=__a , num_inference_steps=5 , negative_prompt="" , ).to_tuple() UpperCamelCase = torch.Generator(device="cuda" ).manual_seed(0 ) UpperCamelCase = pipeline( image_embeds=__a , negative_image_embeds=__a , hint=__a , generator=__a , num_inference_steps=1_00 , output_type="np" , ) UpperCamelCase = output.images[0] assert image.shape == (5_12, 5_12, 3) assert_mean_pixel_difference(__a , __a )
153
1
"""simple docstring""" import warnings from typing import Dict, List, Optional, Tuple from ...tokenization_utils import AddedToken, PreTrainedTokenizer from ...utils import logging __lowercase = logging.get_logger(__name__) class _A ( _a ): """simple docstring""" UpperCAmelCase : int = ["""input_ids""", """attention_mask"""] def __init__( self : Optional[int] , __UpperCAmelCase : Optional[int]="</s>" , __UpperCAmelCase : Union[str, Any]="<unk>" , __UpperCAmelCase : Tuple="<pad>" , __UpperCAmelCase : Dict=125 , __UpperCAmelCase : List[Any]=None , **__UpperCAmelCase : Optional[int] , ): # Add extra_ids to the special token list if extra_ids > 0 and additional_special_tokens is None: a : List[Any] = [f'''<extra_id_{i}>''' for i in range(__UpperCAmelCase)] elif extra_ids > 0 and additional_special_tokens is not None: # Check that we have the right number of extra_id special tokens a : Optional[Any] = len(set(filter(lambda __UpperCAmelCase: bool("extra_id" in str(__UpperCAmelCase)) , __UpperCAmelCase))) if extra_tokens != extra_ids: raise ValueError( f'''Both extra_ids ({extra_ids}) and additional_special_tokens ({additional_special_tokens}) are''' " provided to ByT5Tokenizer. In this case the additional_special_tokens must include the" " extra_ids tokens") a : List[str] = AddedToken(__UpperCAmelCase , lstrip=__UpperCAmelCase , rstrip=__UpperCAmelCase) if isinstance(__UpperCAmelCase , __UpperCAmelCase) else pad_token a : Tuple = AddedToken(__UpperCAmelCase , lstrip=__UpperCAmelCase , rstrip=__UpperCAmelCase) if isinstance(__UpperCAmelCase , __UpperCAmelCase) else eos_token a : List[str] = AddedToken(__UpperCAmelCase , lstrip=__UpperCAmelCase , rstrip=__UpperCAmelCase) if isinstance(__UpperCAmelCase , __UpperCAmelCase) else unk_token super().__init__( eos_token=__UpperCAmelCase , unk_token=__UpperCAmelCase , pad_token=__UpperCAmelCase , extra_ids=__UpperCAmelCase , additional_special_tokens=__UpperCAmelCase , **__UpperCAmelCase , ) a : Union[str, Any] = extra_ids a : Dict = 2**8 # utf is 8 bits # define special tokens dict a : Dict[int, str] = { self.pad_token: 0, self.eos_token: 1, self.unk_token: 2, } a : Union[str, Any] = len(self.special_tokens_encoder) a : int = len(__UpperCAmelCase) for i, token in enumerate(__UpperCAmelCase): a : str = self.vocab_size + i - n a : Dict[str, int] = {v: k for k, v in self.special_tokens_encoder.items()} @property def __snake_case ( self : str): return self._utf_vocab_size + self._num_special_tokens + self._extra_ids def __snake_case ( self : Optional[Any] , __UpperCAmelCase : List[int] , __UpperCAmelCase : Optional[List[int]] = None , __UpperCAmelCase : bool = False): if already_has_special_tokens: return super().get_special_tokens_mask( token_ids_a=__UpperCAmelCase , token_ids_a=__UpperCAmelCase , already_has_special_tokens=__UpperCAmelCase) # normal case: some special tokens if token_ids_a is None: return ([0] * len(__UpperCAmelCase)) + [1] return ([0] * len(__UpperCAmelCase)) + [1] + ([0] * len(__UpperCAmelCase)) + [1] def __snake_case ( self : int , __UpperCAmelCase : List[int]): if len(__UpperCAmelCase) > 0 and token_ids[-1] == self.eos_token_id: warnings.warn( f'''This sequence already has {self.eos_token}. In future versions this behavior may lead to duplicated''' " eos tokens being added.") return token_ids else: return token_ids + [self.eos_token_id] def __snake_case ( self : List[Any] , __UpperCAmelCase : List[int] , __UpperCAmelCase : Optional[List[int]] = None): a : Union[str, Any] = [self.eos_token_id] if token_ids_a is None: return len(token_ids_a + eos) * [0] return len(token_ids_a + eos + token_ids_a + eos) * [0] def __snake_case ( self : Union[str, Any] , __UpperCAmelCase : List[int] , __UpperCAmelCase : Optional[List[int]] = None): a : Optional[int] = self._add_eos_if_not_present(__UpperCAmelCase) if token_ids_a is None: return token_ids_a else: a : str = self._add_eos_if_not_present(__UpperCAmelCase) return token_ids_a + token_ids_a def __snake_case ( self : Dict , __UpperCAmelCase : str): a : List[str] = [chr(__UpperCAmelCase) for i in text.encode("utf-8")] return tokens def __snake_case ( self : Union[str, Any] , __UpperCAmelCase : int): if token in self.special_tokens_encoder: a : List[str] = self.special_tokens_encoder[token] elif token in self.added_tokens_encoder: a : List[Any] = self.added_tokens_encoder[token] elif len(__UpperCAmelCase) != 1: a : Dict = self.unk_token_id else: a : Any = ord(__UpperCAmelCase) + self._num_special_tokens return token_id def __snake_case ( self : Dict , __UpperCAmelCase : Union[str, Any]): if index in self.special_tokens_decoder: a : Union[str, Any] = self.special_tokens_decoder[index] else: a : int = chr(index - self._num_special_tokens) return token def __snake_case ( self : List[Any] , __UpperCAmelCase : Any): a : Optional[Any] = B"" for token in tokens: if token in self.special_tokens_decoder: a : List[Any] = self.special_tokens_decoder[token].encode("utf-8") elif token in self.added_tokens_decoder: a : Union[str, Any] = self.special_tokens_decoder[token].encode("utf-8") elif token in self.special_tokens_encoder: a : Optional[Any] = token.encode("utf-8") elif token in self.added_tokens_encoder: a : Optional[int] = token.encode("utf-8") else: a : Optional[Any] = bytes([ord(__UpperCAmelCase)]) bstring += tok_string a : Tuple = bstring.decode("utf-8" , errors="ignore") return string def __snake_case ( self : Dict , __UpperCAmelCase : str , __UpperCAmelCase : Optional[str] = None): return ()
226
"""simple docstring""" import warnings from transformers import AutoTokenizer from transformers.utils import is_torch_available from transformers.utils.generic import ExplicitEnum from ...processing_utils import ProcessorMixin if is_torch_available(): import torch class _A ( _a ): """simple docstring""" UpperCAmelCase : str = """char""" UpperCAmelCase : Optional[Any] = """bpe""" UpperCAmelCase : Optional[Any] = """wp""" __lowercase = (DecodeType.CHARACTER, DecodeType.BPE, DecodeType.WORDPIECE) class _A ( _a ): """simple docstring""" UpperCAmelCase : Optional[Any] = ["""image_processor""", """char_tokenizer"""] UpperCAmelCase : Optional[Any] = """ViTImageProcessor""" UpperCAmelCase : List[Any] = """MgpstrTokenizer""" def __init__( self : List[Any] , __UpperCAmelCase : Union[str, Any]=None , __UpperCAmelCase : Dict=None , **__UpperCAmelCase : str): a : Tuple = None if "feature_extractor" in kwargs: warnings.warn( "The `feature_extractor` argument is deprecated and will be removed in v5, use `image_processor`" " instead." , __UpperCAmelCase , ) a : List[str] = kwargs.pop("feature_extractor") a : Dict = image_processor if image_processor is not None else feature_extractor if image_processor is None: raise ValueError("You need to specify an `image_processor`.") if tokenizer is None: raise ValueError("You need to specify a `tokenizer`.") a : Union[str, Any] = tokenizer a : int = AutoTokenizer.from_pretrained("gpt2") a : str = AutoTokenizer.from_pretrained("bert-base-uncased") super().__init__(__UpperCAmelCase , __UpperCAmelCase) def __call__( self : str , __UpperCAmelCase : Dict=None , __UpperCAmelCase : Tuple=None , __UpperCAmelCase : Union[str, Any]=None , **__UpperCAmelCase : 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: a : List[str] = self.image_processor(__UpperCAmelCase , return_tensors=__UpperCAmelCase , **__UpperCAmelCase) if text is not None: a : Optional[Any] = self.char_tokenizer(__UpperCAmelCase , return_tensors=__UpperCAmelCase , **__UpperCAmelCase) if text is None: return inputs elif images is None: return encodings else: a : Any = encodings["input_ids"] return inputs def __snake_case ( self : List[Any] , __UpperCAmelCase : List[str]): a , a , a : Tuple = sequences a : Optional[int] = char_preds.size(0) a , a : Dict = self._decode_helper(__UpperCAmelCase , "char") a , a : Dict = self._decode_helper(__UpperCAmelCase , "bpe") a , a : Union[str, Any] = self._decode_helper(__UpperCAmelCase , "wp") a : Any = [] a : Union[str, Any] = [] for i in range(__UpperCAmelCase): a : Any = [char_scores[i], bpe_scores[i], wp_scores[i]] a : Optional[Any] = [char_strs[i], bpe_strs[i], wp_strs[i]] a : List[str] = scores.index(max(__UpperCAmelCase)) final_strs.append(strs[max_score_index]) final_scores.append(scores[max_score_index]) a : Dict = {} a : List[str] = final_strs a : str = final_scores a : int = char_strs a : int = bpe_strs a : Tuple = wp_strs return out def __snake_case ( self : Any , __UpperCAmelCase : List[str] , __UpperCAmelCase : List[str]): if format == DecodeType.CHARACTER: a : int = self.char_decode a : int = 1 a : Dict = "[s]" elif format == DecodeType.BPE: a : List[str] = self.bpe_decode a : List[str] = 2 a : int = "#" elif format == DecodeType.WORDPIECE: a : Union[str, Any] = self.wp_decode a : List[str] = 102 a : int = "[SEP]" else: raise ValueError(f'''Format {format} is not supported.''') a , a : str = [], [] a : Optional[int] = pred_logits.size(0) a : List[str] = pred_logits.size(1) a , a : Tuple = pred_logits.topk(1 , dim=-1 , largest=__UpperCAmelCase , sorted=__UpperCAmelCase) a : List[str] = preds_index.view(-1 , __UpperCAmelCase)[:, 1:] a : Any = decoder(__UpperCAmelCase) a , a : Union[str, Any] = torch.nn.functional.softmax(__UpperCAmelCase , dim=2).max(dim=2) a : Union[str, Any] = preds_max_prob[:, 1:] for index in range(__UpperCAmelCase): a : str = preds_str[index].find(__UpperCAmelCase) a : Optional[Any] = preds_str[index][:pred_eos] a : Optional[int] = preds_index[index].cpu().tolist() a : Optional[int] = pred_index.index(__UpperCAmelCase) if eos_token in pred_index else -1 a : List[str] = preds_max_prob[index][: pred_eos_index + 1] a : int = pred_max_prob.cumprod(dim=0)[-1] if pred_max_prob.nelement() != 0 else 0.0 dec_strs.append(__UpperCAmelCase) conf_scores.append(__UpperCAmelCase) return dec_strs, conf_scores def __snake_case ( self : Optional[int] , __UpperCAmelCase : Any): a : Dict = [seq.replace(" " , "") for seq in self.char_tokenizer.batch_decode(__UpperCAmelCase)] return decode_strs def __snake_case ( self : Optional[int] , __UpperCAmelCase : List[str]): return self.bpe_tokenizer.batch_decode(__UpperCAmelCase) def __snake_case ( self : Union[str, Any] , __UpperCAmelCase : int): a : Any = [seq.replace(" " , "") for seq in self.wp_tokenizer.batch_decode(__UpperCAmelCase)] return decode_strs
226
1
"""simple docstring""" from __future__ import annotations import os import tempfile import unittest from transformers import ConvBertConfig, is_tf_available from transformers.testing_utils import require_tf, slow from ...test_configuration_common import ConfigTester from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor, random_attention_mask from ...test_pipeline_mixin import PipelineTesterMixin if is_tf_available(): import tensorflow as tf from transformers import ( TFConvBertForMaskedLM, TFConvBertForMultipleChoice, TFConvBertForQuestionAnswering, TFConvBertForSequenceClassification, TFConvBertForTokenClassification, TFConvBertModel, ) class lowerCAmelCase_ : '''simple docstring''' def __init__( self , snake_case_ , snake_case_=13 , snake_case_=7 , snake_case_=True , snake_case_=True , snake_case_=True , snake_case_=True , snake_case_=99 , snake_case_=32 , snake_case_=2 , snake_case_=4 , snake_case_=37 , snake_case_="gelu" , snake_case_=0.1 , snake_case_=0.1 , snake_case_=512 , snake_case_=16 , snake_case_=2 , snake_case_=0.02 , snake_case_=3 , snake_case_=4 , snake_case_=None , ) -> Dict: __lowerCAmelCase = parent __lowerCAmelCase = 13 __lowerCAmelCase = 7 __lowerCAmelCase = True __lowerCAmelCase = True __lowerCAmelCase = True __lowerCAmelCase = True __lowerCAmelCase = 99 __lowerCAmelCase = 384 __lowerCAmelCase = 2 __lowerCAmelCase = 4 __lowerCAmelCase = 37 __lowerCAmelCase = """gelu""" __lowerCAmelCase = 0.1 __lowerCAmelCase = 0.1 __lowerCAmelCase = 512 __lowerCAmelCase = 16 __lowerCAmelCase = 2 __lowerCAmelCase = 0.02 __lowerCAmelCase = 3 __lowerCAmelCase = 4 __lowerCAmelCase = 128 __lowerCAmelCase = 2 __lowerCAmelCase = 9 __lowerCAmelCase = 1 __lowerCAmelCase = None def A__ ( self ) -> str: __lowerCAmelCase = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) __lowerCAmelCase = None if self.use_input_mask: __lowerCAmelCase = random_attention_mask([self.batch_size, self.seq_length] ) __lowerCAmelCase = None if self.use_token_type_ids: __lowerCAmelCase = ids_tensor([self.batch_size, self.seq_length] , self.type_vocab_size ) __lowerCAmelCase = None __lowerCAmelCase = None __lowerCAmelCase = None if self.use_labels: __lowerCAmelCase = ids_tensor([self.batch_size] , self.type_sequence_label_size ) __lowerCAmelCase = ids_tensor([self.batch_size, self.seq_length] , self.num_labels ) __lowerCAmelCase = ids_tensor([self.batch_size] , self.num_choices ) __lowerCAmelCase = ConvBertConfig( vocab_size=self.vocab_size , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , max_position_embeddings=self.max_position_embeddings , type_vocab_size=self.type_vocab_size , initializer_range=self.initializer_range , return_dict=snake_case_ , ) return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels def A__ ( self , snake_case_ , snake_case_ , snake_case_ , snake_case_ , snake_case_ , snake_case_ , snake_case_ ) -> str: __lowerCAmelCase = TFConvBertModel(config=snake_case_ ) __lowerCAmelCase = {"""input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids} __lowerCAmelCase = [input_ids, input_mask] __lowerCAmelCase = model(snake_case_ ) __lowerCAmelCase = model(snake_case_ ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def A__ ( self , snake_case_ , snake_case_ , snake_case_ , snake_case_ , snake_case_ , snake_case_ , snake_case_ ) -> Union[str, Any]: __lowerCAmelCase = TFConvBertForMaskedLM(config=snake_case_ ) __lowerCAmelCase = { """input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids, } __lowerCAmelCase = model(snake_case_ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.vocab_size) ) def A__ ( self , snake_case_ , snake_case_ , snake_case_ , snake_case_ , snake_case_ , snake_case_ , snake_case_ ) -> int: __lowerCAmelCase = self.num_labels __lowerCAmelCase = TFConvBertForSequenceClassification(config=snake_case_ ) __lowerCAmelCase = { """input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids, } __lowerCAmelCase = model(snake_case_ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_labels) ) def A__ ( self , snake_case_ , snake_case_ , snake_case_ , snake_case_ , snake_case_ , snake_case_ , snake_case_ ) -> Any: __lowerCAmelCase = self.num_choices __lowerCAmelCase = TFConvBertForMultipleChoice(config=snake_case_ ) __lowerCAmelCase = tf.tile(tf.expand_dims(snake_case_ , 1 ) , (1, self.num_choices, 1) ) __lowerCAmelCase = tf.tile(tf.expand_dims(snake_case_ , 1 ) , (1, self.num_choices, 1) ) __lowerCAmelCase = tf.tile(tf.expand_dims(snake_case_ , 1 ) , (1, self.num_choices, 1) ) __lowerCAmelCase = { """input_ids""": multiple_choice_inputs_ids, """attention_mask""": multiple_choice_input_mask, """token_type_ids""": multiple_choice_token_type_ids, } __lowerCAmelCase = model(snake_case_ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_choices) ) def A__ ( self , snake_case_ , snake_case_ , snake_case_ , snake_case_ , snake_case_ , snake_case_ , snake_case_ ) -> int: __lowerCAmelCase = self.num_labels __lowerCAmelCase = TFConvBertForTokenClassification(config=snake_case_ ) __lowerCAmelCase = { """input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids, } __lowerCAmelCase = model(snake_case_ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.num_labels) ) def A__ ( self , snake_case_ , snake_case_ , snake_case_ , snake_case_ , snake_case_ , snake_case_ , snake_case_ ) -> Optional[Any]: __lowerCAmelCase = TFConvBertForQuestionAnswering(config=snake_case_ ) __lowerCAmelCase = { """input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids, } __lowerCAmelCase = model(snake_case_ ) self.parent.assertEqual(result.start_logits.shape , (self.batch_size, self.seq_length) ) self.parent.assertEqual(result.end_logits.shape , (self.batch_size, self.seq_length) ) def A__ ( self ) -> List[str]: __lowerCAmelCase = self.prepare_config_and_inputs() ( ( __lowerCAmelCase ) , ( __lowerCAmelCase ) , ( __lowerCAmelCase ) , ( __lowerCAmelCase ) , ( __lowerCAmelCase ) , ( __lowerCAmelCase ) , ( __lowerCAmelCase ) , ) = config_and_inputs __lowerCAmelCase = {"""input_ids""": input_ids, """token_type_ids""": token_type_ids, """attention_mask""": input_mask} return config, inputs_dict @require_tf class lowerCAmelCase_ ( A__ , A__ , unittest.TestCase ): '''simple docstring''' _snake_case = ( ( TFConvBertModel, TFConvBertForMaskedLM, TFConvBertForQuestionAnswering, TFConvBertForSequenceClassification, TFConvBertForTokenClassification, TFConvBertForMultipleChoice, ) if is_tf_available() else () ) _snake_case = ( { '''feature-extraction''': TFConvBertModel, '''fill-mask''': TFConvBertForMaskedLM, '''question-answering''': TFConvBertForQuestionAnswering, '''text-classification''': TFConvBertForSequenceClassification, '''token-classification''': TFConvBertForTokenClassification, '''zero-shot''': TFConvBertForSequenceClassification, } if is_tf_available() else {} ) _snake_case = False _snake_case = False _snake_case = False def A__ ( self ) -> int: __lowerCAmelCase = TFConvBertModelTester(self ) __lowerCAmelCase = ConfigTester(self , config_class=snake_case_ , hidden_size=37 ) def A__ ( self ) -> List[str]: self.config_tester.run_common_tests() def A__ ( self ) -> Optional[int]: __lowerCAmelCase = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*snake_case_ ) def A__ ( self ) -> Optional[int]: __lowerCAmelCase = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_masked_lm(*snake_case_ ) def A__ ( self ) -> Optional[Any]: __lowerCAmelCase = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_multiple_choice(*snake_case_ ) def A__ ( self ) -> List[Any]: __lowerCAmelCase = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_question_answering(*snake_case_ ) def A__ ( self ) -> Optional[Any]: __lowerCAmelCase = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_sequence_classification(*snake_case_ ) def A__ ( self ) -> Optional[Any]: __lowerCAmelCase = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_token_classification(*snake_case_ ) @slow def A__ ( self ) -> Union[str, Any]: __lowerCAmelCase , __lowerCAmelCase = self.model_tester.prepare_config_and_inputs_for_common() __lowerCAmelCase = True __lowerCAmelCase = True if hasattr(snake_case_ , """use_cache""" ): __lowerCAmelCase = True __lowerCAmelCase = getattr(self.model_tester , """encoder_seq_length""" , self.model_tester.seq_length ) __lowerCAmelCase = getattr(self.model_tester , """key_length""" , snake_case_ ) for model_class in self.all_model_classes: __lowerCAmelCase = self._prepare_for_class(snake_case_ , snake_case_ ) __lowerCAmelCase = model_class(snake_case_ ) __lowerCAmelCase = len(model(snake_case_ ) ) with tempfile.TemporaryDirectory() as tmpdirname: model.save_pretrained(snake_case_ , saved_model=snake_case_ ) __lowerCAmelCase = os.path.join(snake_case_ , """saved_model""" , """1""" ) __lowerCAmelCase = tf.keras.models.load_model(snake_case_ ) __lowerCAmelCase = model(snake_case_ ) if self.is_encoder_decoder: __lowerCAmelCase = outputs["""encoder_hidden_states"""] __lowerCAmelCase = outputs["""encoder_attentions"""] else: __lowerCAmelCase = outputs["""hidden_states"""] __lowerCAmelCase = outputs["""attentions"""] self.assertEqual(len(snake_case_ ) , snake_case_ ) __lowerCAmelCase = getattr( self.model_tester , """expected_num_hidden_layers""" , self.model_tester.num_hidden_layers + 1 ) self.assertEqual(len(snake_case_ ) , snake_case_ ) self.assertListEqual( list(output_hidden_states[0].shape[-2:] ) , [self.model_tester.seq_length, self.model_tester.hidden_size] , ) self.assertEqual(len(snake_case_ ) , self.model_tester.num_hidden_layers ) self.assertListEqual( list(output_attentions[0].shape[-3:] ) , [self.model_tester.num_attention_heads / 2, encoder_seq_length, encoder_key_length] , ) @slow def A__ ( self ) -> Tuple: __lowerCAmelCase = TFConvBertModel.from_pretrained("""YituTech/conv-bert-base""" ) self.assertIsNotNone(snake_case_ ) def A__ ( self ) -> str: __lowerCAmelCase , __lowerCAmelCase = self.model_tester.prepare_config_and_inputs_for_common() __lowerCAmelCase = True __lowerCAmelCase = getattr(self.model_tester , """decoder_seq_length""" , self.model_tester.seq_length ) __lowerCAmelCase = getattr(self.model_tester , """encoder_seq_length""" , self.model_tester.seq_length ) __lowerCAmelCase = getattr(self.model_tester , """key_length""" , snake_case_ ) __lowerCAmelCase = getattr(self.model_tester , """key_length""" , snake_case_ ) def check_decoder_attentions_output(snake_case_ ): __lowerCAmelCase = len(snake_case_ ) self.assertEqual(out_len % 2 , 0 ) __lowerCAmelCase = outputs.decoder_attentions self.assertEqual(len(snake_case_ ) , self.model_tester.num_hidden_layers ) self.assertListEqual( list(decoder_attentions[0].shape[-3:] ) , [self.model_tester.num_attention_heads / 2, decoder_seq_length, decoder_key_length] , ) def check_encoder_attentions_output(snake_case_ ): __lowerCAmelCase = [ t.numpy() for t in (outputs.encoder_attentions if config.is_encoder_decoder else outputs.attentions) ] self.assertEqual(len(snake_case_ ) , self.model_tester.num_hidden_layers ) self.assertListEqual( list(attentions[0].shape[-3:] ) , [self.model_tester.num_attention_heads / 2, encoder_seq_length, encoder_key_length] , ) for model_class in self.all_model_classes: __lowerCAmelCase = True __lowerCAmelCase = False __lowerCAmelCase = model_class(snake_case_ ) __lowerCAmelCase = model(self._prepare_for_class(snake_case_ , snake_case_ ) ) __lowerCAmelCase = len(snake_case_ ) self.assertEqual(config.output_hidden_states , snake_case_ ) check_encoder_attentions_output(snake_case_ ) if self.is_encoder_decoder: __lowerCAmelCase = model_class(snake_case_ ) __lowerCAmelCase = model(self._prepare_for_class(snake_case_ , snake_case_ ) ) self.assertEqual(config.output_hidden_states , snake_case_ ) check_decoder_attentions_output(snake_case_ ) # Check that output attentions can also be changed via the config del inputs_dict["output_attentions"] __lowerCAmelCase = True __lowerCAmelCase = model_class(snake_case_ ) __lowerCAmelCase = model(self._prepare_for_class(snake_case_ , snake_case_ ) ) self.assertEqual(config.output_hidden_states , snake_case_ ) check_encoder_attentions_output(snake_case_ ) # Check attention is always last and order is fine __lowerCAmelCase = True __lowerCAmelCase = True __lowerCAmelCase = model_class(snake_case_ ) __lowerCAmelCase = model(self._prepare_for_class(snake_case_ , snake_case_ ) ) self.assertEqual(out_len + (2 if self.is_encoder_decoder else 1) , len(snake_case_ ) ) self.assertEqual(model.config.output_hidden_states , snake_case_ ) check_encoder_attentions_output(snake_case_ ) @require_tf class lowerCAmelCase_ ( unittest.TestCase ): '''simple docstring''' @slow def A__ ( self ) -> Any: __lowerCAmelCase = TFConvBertModel.from_pretrained("""YituTech/conv-bert-base""" ) __lowerCAmelCase = tf.constant([[0, 1, 2, 3, 4, 5]] ) __lowerCAmelCase = model(snake_case_ )[0] __lowerCAmelCase = [1, 6, 768] self.assertEqual(output.shape , snake_case_ ) __lowerCAmelCase = tf.constant( [ [ [-0.03_475_493, -0.4_686_034, -0.30_638_832], [0.22_637_248, -0.26_988_646, -0.7_423_424], [0.10_324_868, -0.45_013_508, -0.58_280_784], ] ] ) tf.debugging.assert_near(output[:, :3, :3] , snake_case_ , atol=1e-4 )
301
"""simple docstring""" import os from distutils.util import strtobool def lowercase (_lowerCAmelCase , _lowerCAmelCase ): for e in env_keys: __lowerCAmelCase = int(os.environ.get(_lowerCAmelCase , -1 ) ) if val >= 0: return val return default def lowercase (_lowerCAmelCase , _lowerCAmelCase=False ): __lowerCAmelCase = os.environ.get(_lowerCAmelCase , str(_lowerCAmelCase ) ) return strtobool(_lowerCAmelCase ) == 1 # As its name indicates `strtobool` actually returns an int... def lowercase (_lowerCAmelCase , _lowerCAmelCase="no" ): __lowerCAmelCase = os.environ.get(_lowerCAmelCase , str(_lowerCAmelCase ) ) return value
301
1
"""simple docstring""" SCREAMING_SNAKE_CASE : dict[str, float] = { "km/h": 1.0, "m/s": 3.6, "mph": 1.6_0_9_3_4_4, "knot": 1.8_5_2, } SCREAMING_SNAKE_CASE : dict[str, float] = { "km/h": 1.0, "m/s": 0.2_7_7_7_7_7_7_7_8, "mph": 0.6_2_1_3_7_1_1_9_2, "knot": 0.5_3_9_9_5_6_8_0_3, } def __UpperCAmelCase ( snake_case_ : float , snake_case_ : str , snake_case_ : str ) -> float: """simple docstring""" if unit_to not in speed_chart or unit_from not in speed_chart_inverse: _lowerCAmelCase = ( F"""Incorrect 'from_type' or 'to_type' value: {unit_from!r}, {unit_to!r}\n""" F"""Valid values are: {", ".join(snake_case_ )}""" ) raise ValueError(snake_case_ ) return round(speed * speed_chart[unit_from] * speed_chart_inverse[unit_to] , 3 ) if __name__ == "__main__": import doctest doctest.testmod()
317
"""simple docstring""" from math import isqrt def __UpperCAmelCase ( snake_case_ : int ) -> list[int]: """simple docstring""" _lowerCAmelCase = [True] * max_number for i in range(2 , isqrt(max_number - 1 ) + 1 ): if is_prime[i]: for j in range(i**2 , snake_case_ , snake_case_ ): _lowerCAmelCase = False return [i for i in range(2 , snake_case_ ) if is_prime[i]] def __UpperCAmelCase ( snake_case_ : int = 10**8 ) -> int: """simple docstring""" _lowerCAmelCase = calculate_prime_numbers(max_number // 2 ) _lowerCAmelCase = 0 _lowerCAmelCase = 0 _lowerCAmelCase = len(snake_case_ ) - 1 while left <= right: while prime_numbers[left] * prime_numbers[right] >= max_number: right -= 1 semiprimes_count += right - left + 1 left += 1 return semiprimes_count if __name__ == "__main__": print(F'{solution() = }')
317
1
from __future__ import annotations from collections.abc import Iterator class lowercase : def __init__( self ,A__): lowercase = value lowercase = None lowercase = None class lowercase : def __init__( self ,A__): lowercase = tree def A__ ( self ,A__): if node is None: return 0 return node.value + ( self.depth_first_search(node.left) + self.depth_first_search(node.right) ) def __iter__( self): yield self.depth_first_search(self.tree) if __name__ == "__main__": import doctest doctest.testmod()
101
"""simple docstring""" from collections import OrderedDict from typing import Any, Mapping, Optional from ... import PreTrainedTokenizer from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig, OnnxConfigWithPast, OnnxSeqaSeqConfigWithPast from ...onnx.utils import compute_effective_axis_dimension from ...utils import TensorType, is_torch_available, logging _snake_case : List[Any] = logging.get_logger(__name__) _snake_case : int = { 'Helsinki-NLP/opus-mt-en-de': 'https://huggingface.co/Helsinki-NLP/opus-mt-en-de/resolve/main/config.json', # See all Marian models at https://huggingface.co/models?filter=marian } class _UpperCAmelCase ( lowercase_ ): UpperCamelCase = '''marian''' UpperCamelCase = ['''past_key_values'''] UpperCamelCase = {'''num_attention_heads''': '''encoder_attention_heads''', '''hidden_size''': '''d_model'''} def __init__( self :int , __UpperCamelCase :Any=5_81_01 , __UpperCamelCase :int=None , __UpperCamelCase :Union[str, Any]=10_24 , __UpperCamelCase :Union[str, Any]=12 , __UpperCamelCase :str=40_96 , __UpperCamelCase :int=16 , __UpperCamelCase :int=12 , __UpperCamelCase :Optional[Any]=40_96 , __UpperCamelCase :Optional[Any]=16 , __UpperCamelCase :Dict=0.0 , __UpperCamelCase :Dict=0.0 , __UpperCamelCase :str=True , __UpperCamelCase :Optional[int]=True , __UpperCamelCase :Any="gelu" , __UpperCamelCase :Any=10_24 , __UpperCamelCase :List[Any]=0.1 , __UpperCamelCase :Optional[Any]=0.0 , __UpperCamelCase :Union[str, Any]=0.0 , __UpperCamelCase :Tuple=0.02 , __UpperCamelCase :List[str]=5_81_00 , __UpperCamelCase :str=False , __UpperCamelCase :Optional[int]=5_81_00 , __UpperCamelCase :List[Any]=0 , __UpperCamelCase :List[str]=0 , __UpperCamelCase :Dict=True , **__UpperCamelCase :Tuple , ): A = vocab_size A = decoder_vocab_size or vocab_size A = max_position_embeddings A = d_model A = encoder_ffn_dim A = encoder_layers A = encoder_attention_heads A = decoder_ffn_dim A = decoder_layers A = decoder_attention_heads A = dropout A = attention_dropout A = activation_dropout A = activation_function A = init_std A = encoder_layerdrop A = decoder_layerdrop A = use_cache A = encoder_layers A = scale_embedding # scale factor will be sqrt(d_model) if True A = share_encoder_decoder_embeddings super().__init__( pad_token_id=__UpperCamelCase , eos_token_id=__UpperCamelCase , is_encoder_decoder=__UpperCamelCase , decoder_start_token_id=__UpperCamelCase , forced_eos_token_id=__UpperCamelCase , **__UpperCamelCase , ) class _UpperCAmelCase ( lowercase_ ): @property # Copied from transformers.models.bart.configuration_bart.BartOnnxConfig.inputs def lowerCamelCase ( self :List[str] ): if self.task in ["default", "seq2seq-lm"]: A = OrderedDict( [ ("input_ids", {0: "batch", 1: "encoder_sequence"}), ("attention_mask", {0: "batch", 1: "encoder_sequence"}), ] ) if self.use_past: A = {0: "batch"} A = {0: "batch", 1: "past_decoder_sequence + sequence"} else: A = {0: "batch", 1: "decoder_sequence"} A = {0: "batch", 1: "decoder_sequence"} if self.use_past: self.fill_with_past_key_values_(__UpperCamelCase , direction="inputs" ) elif self.task == "causal-lm": # TODO: figure this case out. A = OrderedDict( [ ("input_ids", {0: "batch", 1: "encoder_sequence"}), ("attention_mask", {0: "batch", 1: "encoder_sequence"}), ] ) if self.use_past: A, A = self.num_layers for i in range(__UpperCamelCase ): A = {0: "batch", 2: "past_sequence + sequence"} A = {0: "batch", 2: "past_sequence + sequence"} else: A = 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 # Copied from transformers.models.bart.configuration_bart.BartOnnxConfig.outputs def lowerCamelCase ( self :List[str] ): if self.task in ["default", "seq2seq-lm"]: A = super().outputs else: A = super(__UpperCamelCase , self ).outputs if self.use_past: A, A = self.num_layers for i in range(__UpperCamelCase ): A = {0: "batch", 2: "past_sequence + sequence"} A = {0: "batch", 2: "past_sequence + sequence"} return common_outputs def lowerCamelCase ( self :Optional[int] , __UpperCamelCase :PreTrainedTokenizer , __UpperCamelCase :int = -1 , __UpperCamelCase :int = -1 , __UpperCamelCase :bool = False , __UpperCamelCase :Optional[TensorType] = None , ): A = self._generate_dummy_inputs_for_encoder_and_decoder( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) # Generate decoder inputs A = seq_length if not self.use_past else 1 A = self._generate_dummy_inputs_for_encoder_and_decoder( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) A = {f"decoder_{name}": tensor for name, tensor in decoder_inputs.items()} A = dict(**__UpperCamelCase , **__UpperCamelCase ) if self.use_past: if not is_torch_available(): raise ValueError("Cannot generate dummy past_keys inputs without PyTorch installed." ) else: import torch A, A = common_inputs["input_ids"].shape A = common_inputs["decoder_input_ids"].shape[1] A, A = self.num_attention_heads A = ( batch, num_encoder_attention_heads, encoder_seq_length, self._config.hidden_size // num_encoder_attention_heads, ) A = decoder_seq_length + 3 A = ( batch, num_decoder_attention_heads, decoder_past_length, self._config.hidden_size // num_decoder_attention_heads, ) A = torch.cat( [common_inputs["decoder_attention_mask"], torch.ones(__UpperCamelCase , __UpperCamelCase )] , dim=1 ) A = [] # If the number of encoder and decoder layers are present in the model configuration, both are considered A, A = self.num_layers A = min(__UpperCamelCase , __UpperCamelCase ) A = max(__UpperCamelCase , __UpperCamelCase ) - min_num_layers A = "encoder" if num_encoder_layers > num_decoder_layers else "decoder" for _ in range(__UpperCamelCase ): common_inputs["past_key_values"].append( ( torch.zeros(__UpperCamelCase ), torch.zeros(__UpperCamelCase ), torch.zeros(__UpperCamelCase ), torch.zeros(__UpperCamelCase ), ) ) # TODO: test this. A = encoder_shape if remaining_side_name == "encoder" else decoder_shape for _ in range(__UpperCamelCase , __UpperCamelCase ): common_inputs["past_key_values"].append((torch.zeros(__UpperCamelCase ), torch.zeros(__UpperCamelCase )) ) return common_inputs def lowerCamelCase ( self :Optional[int] , __UpperCamelCase :PreTrainedTokenizer , __UpperCamelCase :int = -1 , __UpperCamelCase :int = -1 , __UpperCamelCase :bool = False , __UpperCamelCase :Optional[TensorType] = None , ): A = self._generate_dummy_inputs_for_encoder_and_decoder( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) if self.use_past: if not is_torch_available(): raise ValueError("Cannot generate dummy past_keys inputs without PyTorch installed." ) else: import torch A, A = common_inputs["input_ids"].shape # Not using the same length for past_key_values A = seqlen + 2 A, A = self.num_layers A, A = self.num_attention_heads A = ( batch, num_encoder_attention_heads, past_key_values_length, self._config.hidden_size // num_encoder_attention_heads, ) A = common_inputs["attention_mask"].dtype A = torch.cat( [common_inputs["attention_mask"], torch.ones(__UpperCamelCase , __UpperCamelCase , dtype=__UpperCamelCase )] , dim=1 ) A = [ (torch.zeros(__UpperCamelCase ), torch.zeros(__UpperCamelCase )) for _ in range(__UpperCamelCase ) ] return common_inputs def lowerCamelCase ( self :Tuple , __UpperCamelCase :PreTrainedTokenizer , __UpperCamelCase :int = -1 , __UpperCamelCase :int = -1 , __UpperCamelCase :bool = False , __UpperCamelCase :Optional[TensorType] = None , ): # 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 A = compute_effective_axis_dimension( __UpperCamelCase , 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 A = tokenizer.num_special_tokens_to_add(__UpperCamelCase ) A = compute_effective_axis_dimension( __UpperCamelCase , fixed_dimension=OnnxConfig.default_fixed_sequence , num_token_to_add=__UpperCamelCase ) # Generate dummy inputs according to compute batch and sequence A = [" ".join([tokenizer.unk_token] ) * seq_length] * batch_size A = dict(tokenizer(__UpperCamelCase , return_tensors=__UpperCamelCase ) ) return common_inputs def lowerCamelCase ( self :List[Any] , __UpperCamelCase :PreTrainedTokenizer , __UpperCamelCase :int = -1 , __UpperCamelCase :int = -1 , __UpperCamelCase :bool = False , __UpperCamelCase :Optional[TensorType] = None , ): if self.task in ["default", "seq2seq-lm"]: A = self._generate_dummy_inputs_for_default_and_seqaseq_lm( __UpperCamelCase , batch_size=__UpperCamelCase , seq_length=__UpperCamelCase , is_pair=__UpperCamelCase , framework=__UpperCamelCase ) else: A = self._generate_dummy_inputs_for_causal_lm( __UpperCamelCase , batch_size=__UpperCamelCase , seq_length=__UpperCamelCase , is_pair=__UpperCamelCase , framework=__UpperCamelCase ) return common_inputs def lowerCamelCase ( self :List[Any] , __UpperCamelCase :Tuple , __UpperCamelCase :List[str] , __UpperCamelCase :str , __UpperCamelCase :str ): if self.task in ["default", "seq2seq-lm"]: A = super()._flatten_past_key_values_(__UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) else: A = super(__UpperCamelCase , self )._flatten_past_key_values_( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) @property def lowerCamelCase ( self :List[str] ): return 1e-4
292
0
from __future__ import annotations # This is the precision for this function which can be altered. # It is recommended for users to keep this number greater than or equal to 10. _lowerCamelCase = 10 def SCREAMING_SNAKE_CASE ( __UpperCamelCase : int , __UpperCamelCase : int , __UpperCamelCase : list[int] , __UpperCamelCase : int ) -> int: for i in range(lowerCAmelCase__ , lowerCAmelCase__ ): if array[i] == target: return i return -1 def SCREAMING_SNAKE_CASE ( __UpperCamelCase : list[int] , __UpperCamelCase : int ) -> int: UpperCAmelCase_ = 0 UpperCAmelCase_ = len(lowerCAmelCase__ ) while left <= right: if right - left < precision: return lin_search(lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ ) UpperCAmelCase_ = (left + right) // 3 + 1 UpperCAmelCase_ = 2 * (left + right) // 3 + 1 if array[one_third] == target: return one_third elif array[two_third] == target: return two_third elif target < array[one_third]: UpperCAmelCase_ = one_third - 1 elif array[two_third] < target: UpperCAmelCase_ = two_third + 1 else: UpperCAmelCase_ = one_third + 1 UpperCAmelCase_ = two_third - 1 else: return -1 def SCREAMING_SNAKE_CASE ( __UpperCamelCase : int , __UpperCamelCase : int , __UpperCamelCase : list[int] , __UpperCamelCase : int ) -> int: if left < right: if right - left < precision: return lin_search(lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ ) UpperCAmelCase_ = (left + right) // 3 + 1 UpperCAmelCase_ = 2 * (left + right) // 3 + 1 if array[one_third] == target: return one_third elif array[two_third] == target: return two_third elif target < array[one_third]: return rec_ternary_search(lowerCAmelCase__ , one_third - 1 , lowerCAmelCase__ , lowerCAmelCase__ ) elif array[two_third] < target: return rec_ternary_search(two_third + 1 , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ ) else: return rec_ternary_search(one_third + 1 , two_third - 1 , lowerCAmelCase__ , lowerCAmelCase__ ) else: return -1 if __name__ == "__main__": import doctest doctest.testmod() _lowerCamelCase = input('Enter numbers separated by comma:\n').strip() _lowerCamelCase = [int(item.strip()) for item in user_input.split(',')] assert collection == sorted(collection), F"List must be ordered.\n{collection}." _lowerCamelCase = int(input('Enter the number to be found in the list:\n').strip()) _lowerCamelCase = ite_ternary_search(collection, target) _lowerCamelCase = rec_ternary_search(0, len(collection) - 1, collection, target) if resulta != -1: print(F"Iterative search: {target} found at positions: {resulta}") print(F"Recursive search: {target} found at positions: {resulta}") else: print('Not found')
361
def SCREAMING_SNAKE_CASE ( __UpperCamelCase : Optional[Any] ) -> Union[str, Any]: UpperCAmelCase_ = len(__UpperCamelCase ) while cur > 1: # Find the maximum number in arr UpperCAmelCase_ = arr.index(max(arr[0:cur] ) ) # Reverse from 0 to mi UpperCAmelCase_ = arr[mi::-1] + arr[mi + 1 : len(__UpperCamelCase )] # Reverse whole list UpperCAmelCase_ = arr[cur - 1 :: -1] + arr[cur : len(__UpperCamelCase )] cur -= 1 return arr if __name__ == "__main__": _lowerCamelCase = input('Enter numbers separated by a comma:\n').strip() _lowerCamelCase = [int(item) for item in user_input.split(',')] print(pancake_sort(unsorted))
177
0
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available _SCREAMING_SNAKE_CASE = { """configuration_git""": ["""GIT_PRETRAINED_CONFIG_ARCHIVE_MAP""", """GitConfig""", """GitVisionConfig"""], """processing_git""": ["""GitProcessor"""], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _SCREAMING_SNAKE_CASE = [ """GIT_PRETRAINED_MODEL_ARCHIVE_LIST""", """GitForCausalLM""", """GitModel""", """GitPreTrainedModel""", """GitVisionModel""", ] if TYPE_CHECKING: from .configuration_git import GIT_PRETRAINED_CONFIG_ARCHIVE_MAP, GitConfig, GitVisionConfig from .processing_git import GitProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_git import ( GIT_PRETRAINED_MODEL_ARCHIVE_LIST, GitForCausalLM, GitModel, GitPreTrainedModel, GitVisionModel, ) else: import sys _SCREAMING_SNAKE_CASE = _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)
343
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_tf_available, is_torch_available, is_vision_available, ) _SCREAMING_SNAKE_CASE = { """configuration_convnext""": ["""CONVNEXT_PRETRAINED_CONFIG_ARCHIVE_MAP""", """ConvNextConfig""", """ConvNextOnnxConfig"""] } try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _SCREAMING_SNAKE_CASE = ["""ConvNextFeatureExtractor"""] _SCREAMING_SNAKE_CASE = ["""ConvNextImageProcessor"""] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _SCREAMING_SNAKE_CASE = [ """CONVNEXT_PRETRAINED_MODEL_ARCHIVE_LIST""", """ConvNextForImageClassification""", """ConvNextModel""", """ConvNextPreTrainedModel""", """ConvNextBackbone""", ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: _SCREAMING_SNAKE_CASE = [ """TFConvNextForImageClassification""", """TFConvNextModel""", """TFConvNextPreTrainedModel""", ] if TYPE_CHECKING: from .configuration_convnext import CONVNEXT_PRETRAINED_CONFIG_ARCHIVE_MAP, ConvNextConfig, ConvNextOnnxConfig try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .feature_extraction_convnext import ConvNextFeatureExtractor from .image_processing_convnext import ConvNextImageProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_convnext import ( CONVNEXT_PRETRAINED_MODEL_ARCHIVE_LIST, ConvNextBackbone, ConvNextForImageClassification, ConvNextModel, ConvNextPreTrainedModel, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_convnext import TFConvNextForImageClassification, TFConvNextModel, TFConvNextPreTrainedModel else: import sys _SCREAMING_SNAKE_CASE = _LazyModule(__name__, globals()["""__file__"""], _import_structure)
343
1
from sklearn.metrics import fa_score, matthews_corrcoef import datasets from .record_evaluation import evaluate as evaluate_record _A = '\\n@article{wang2019superglue,\n title={SuperGLUE: A Stickier Benchmark for General-Purpose Language Understanding Systems},\n author={Wang, Alex and Pruksachatkun, Yada and Nangia, Nikita and Singh, Amanpreet and Michael, Julian and Hill, Felix and Levy, Omer and Bowman, Samuel R},\n journal={arXiv preprint arXiv:1905.00537},\n year={2019}\n}\n' _A = '\\nSuperGLUE (https://super.gluebenchmark.com/) is a new benchmark styled after\nGLUE with a new set of more difficult language understanding tasks, improved\nresources, and a new public leaderboard.\n' _A = '\nCompute SuperGLUE evaluation metric associated to each SuperGLUE dataset.\nArgs:\n predictions: list of predictions to score. Depending on the SuperGlUE subset:\n - for \'record\': list of question-answer dictionaries with the following keys:\n - \'idx\': index of the question as specified by the dataset\n - \'prediction_text\': the predicted answer text\n - for \'multirc\': list of question-answer dictionaries with the following keys:\n - \'idx\': index of the question-answer pair as specified by the dataset\n - \'prediction\': the predicted answer label\n - otherwise: list of predicted labels\n references: list of reference labels. Depending on the SuperGLUE subset:\n - for \'record\': list of question-answers dictionaries with the following keys:\n - \'idx\': index of the question as specified by the dataset\n - \'answers\': list of possible answers\n - otherwise: list of reference labels\nReturns: depending on the SuperGLUE subset:\n - for \'record\':\n - \'exact_match\': Exact match between answer and gold answer\n - \'f1\': F1 score\n - for \'multirc\':\n - \'exact_match\': Exact match between answer and gold answer\n - \'f1_m\': Per-question macro-F1 score\n - \'f1_a\': Average F1 score over all answers\n - for \'axb\':\n \'matthews_correlation\': Matthew Correlation\n - for \'cb\':\n - \'accuracy\': Accuracy\n - \'f1\': F1 score\n - for all others:\n - \'accuracy\': Accuracy\nExamples:\n\n >>> super_glue_metric = datasets.load_metric(\'super_glue\', \'copa\') # any of [\"copa\", \"rte\", \"wic\", \"wsc\", \"wsc.fixed\", \"boolq\", \"axg\"]\n >>> predictions = [0, 1]\n >>> references = [0, 1]\n >>> results = super_glue_metric.compute(predictions=predictions, references=references)\n >>> print(results)\n {\'accuracy\': 1.0}\n\n >>> super_glue_metric = datasets.load_metric(\'super_glue\', \'cb\')\n >>> predictions = [0, 1]\n >>> references = [0, 1]\n >>> results = super_glue_metric.compute(predictions=predictions, references=references)\n >>> print(results)\n {\'accuracy\': 1.0, \'f1\': 1.0}\n\n >>> super_glue_metric = datasets.load_metric(\'super_glue\', \'record\')\n >>> predictions = [{\'idx\': {\'passage\': 0, \'query\': 0}, \'prediction_text\': \'answer\'}]\n >>> references = [{\'idx\': {\'passage\': 0, \'query\': 0}, \'answers\': [\'answer\', \'another_answer\']}]\n >>> results = super_glue_metric.compute(predictions=predictions, references=references)\n >>> print(results)\n {\'exact_match\': 1.0, \'f1\': 1.0}\n\n >>> super_glue_metric = datasets.load_metric(\'super_glue\', \'multirc\')\n >>> predictions = [{\'idx\': {\'answer\': 0, \'paragraph\': 0, \'question\': 0}, \'prediction\': 0}, {\'idx\': {\'answer\': 1, \'paragraph\': 2, \'question\': 3}, \'prediction\': 1}]\n >>> references = [0, 1]\n >>> results = super_glue_metric.compute(predictions=predictions, references=references)\n >>> print(results)\n {\'exact_match\': 1.0, \'f1_m\': 1.0, \'f1_a\': 1.0}\n\n >>> super_glue_metric = datasets.load_metric(\'super_glue\', \'axb\')\n >>> references = [0, 1]\n >>> predictions = [0, 1]\n >>> results = super_glue_metric.compute(predictions=predictions, references=references)\n >>> print(results)\n {\'matthews_correlation\': 1.0}\n' def _UpperCAmelCase ( SCREAMING_SNAKE_CASE__ : List[str] , SCREAMING_SNAKE_CASE__ : Any ): return float((preds == labels).mean() ) def _UpperCAmelCase ( SCREAMING_SNAKE_CASE__ : Optional[int] , SCREAMING_SNAKE_CASE__ : Dict , SCREAMING_SNAKE_CASE__ : Union[str, Any]="binary" ): __UpperCamelCase =simple_accuracy(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) __UpperCamelCase =float(fa_score(y_true=SCREAMING_SNAKE_CASE__ , y_pred=SCREAMING_SNAKE_CASE__ , average=SCREAMING_SNAKE_CASE__ ) ) return { "accuracy": acc, "f1": fa, } def _UpperCAmelCase ( SCREAMING_SNAKE_CASE__ : Dict , SCREAMING_SNAKE_CASE__ : str ): __UpperCamelCase ={} for id_pred, label in zip(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ): __UpperCamelCase =F'{id_pred["idx"]["paragraph"]}-{id_pred["idx"]["question"]}' __UpperCamelCase =id_pred['prediction'] if question_id in question_map: question_map[question_id].append((pred, label) ) else: __UpperCamelCase =[(pred, label)] __UpperCamelCase , __UpperCamelCase =[], [] for question, preds_labels in question_map.items(): __UpperCamelCase , __UpperCamelCase =zip(*SCREAMING_SNAKE_CASE__ ) __UpperCamelCase =fa_score(y_true=SCREAMING_SNAKE_CASE__ , y_pred=SCREAMING_SNAKE_CASE__ , average='macro' ) fas.append(SCREAMING_SNAKE_CASE__ ) __UpperCamelCase =int(sum(pred == label for pred, label in preds_labels ) == len(SCREAMING_SNAKE_CASE__ ) ) ems.append(SCREAMING_SNAKE_CASE__ ) __UpperCamelCase =float(sum(SCREAMING_SNAKE_CASE__ ) / len(SCREAMING_SNAKE_CASE__ ) ) __UpperCamelCase =sum(SCREAMING_SNAKE_CASE__ ) / len(SCREAMING_SNAKE_CASE__ ) __UpperCamelCase =float(fa_score(y_true=SCREAMING_SNAKE_CASE__ , y_pred=[id_pred['prediction'] for id_pred in ids_preds] ) ) return {"exact_match": em, "f1_m": fa_m, "f1_a": fa_a} @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class UpperCAmelCase__ ( datasets.Metric ): """simple docstring""" def _a ( self ) -> int: if self.config_name not in [ "boolq", "cb", "copa", "multirc", "record", "rte", "wic", "wsc", "wsc.fixed", "axb", "axg", ]: raise KeyError( 'You should supply a configuration name selected in ' '["boolq", "cb", "copa", "multirc", "record", "rte", "wic", "wsc", "wsc.fixed", "axb", "axg",]' ) return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features(self._get_feature_types() ) , codebase_urls=[] , reference_urls=[] , format='numpy' if not self.config_name == 'record' and not self.config_name == 'multirc' else None , ) def _a ( self ) -> List[str]: if self.config_name == "record": return { "predictions": { "idx": { "passage": datasets.Value('int64' ), "query": datasets.Value('int64' ), }, "prediction_text": datasets.Value('string' ), }, "references": { "idx": { "passage": datasets.Value('int64' ), "query": datasets.Value('int64' ), }, "answers": datasets.Sequence(datasets.Value('string' ) ), }, } elif self.config_name == "multirc": return { "predictions": { "idx": { "answer": datasets.Value('int64' ), "paragraph": datasets.Value('int64' ), "question": datasets.Value('int64' ), }, "prediction": datasets.Value('int64' ), }, "references": datasets.Value('int64' ), } else: return { "predictions": datasets.Value('int64' ), "references": datasets.Value('int64' ), } def _a ( self , A_ , A_ ) -> Union[str, Any]: if self.config_name == "axb": return {"matthews_correlation": matthews_corrcoef(UpperCAmelCase__ , UpperCAmelCase__ )} elif self.config_name == "cb": return acc_and_fa(UpperCAmelCase__ , UpperCAmelCase__ , fa_avg='macro' ) elif self.config_name == "record": __UpperCamelCase =[ { 'qas': [ {'id': ref['idx']['query'], 'answers': [{'text': ans} for ans in ref['answers']]} for ref in references ] } ] __UpperCamelCase ={pred['idx']['query']: pred['prediction_text'] for pred in predictions} return evaluate_record(UpperCAmelCase__ , UpperCAmelCase__ )[0] elif self.config_name == "multirc": return evaluate_multirc(UpperCAmelCase__ , UpperCAmelCase__ ) elif self.config_name in ["copa", "rte", "wic", "wsc", "wsc.fixed", "boolq", "axg"]: return {"accuracy": simple_accuracy(UpperCAmelCase__ , UpperCAmelCase__ )} else: raise KeyError( 'You should supply a configuration name selected in ' '["boolq", "cb", "copa", "multirc", "record", "rte", "wic", "wsc", "wsc.fixed", "axb", "axg",]' )
361
# 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 from ...utils.dataclasses import ( ComputeEnvironment, DistributedType, DynamoBackend, PrecisionType, SageMakerDistributedType, ) from ..menu import BulletMenu _A = [ 'EAGER', 'AOT_EAGER', 'INDUCTOR', 'NVFUSER', 'AOT_NVFUSER', 'AOT_CUDAGRAPHS', 'OFI', 'FX2TRT', 'ONNXRT', 'IPEX', ] def _UpperCAmelCase ( SCREAMING_SNAKE_CASE__ : Optional[int] , SCREAMING_SNAKE_CASE__ : Optional[Any]=None , SCREAMING_SNAKE_CASE__ : Optional[Any]=None , SCREAMING_SNAKE_CASE__ : str=None ): __UpperCamelCase =True while ask_again: __UpperCamelCase =input(SCREAMING_SNAKE_CASE__ ) try: if default is not None and len(SCREAMING_SNAKE_CASE__ ) == 0: return default return convert_value(SCREAMING_SNAKE_CASE__ ) if convert_value is not None else result except Exception: if error_message is not None: print(SCREAMING_SNAKE_CASE__ ) def _UpperCAmelCase ( SCREAMING_SNAKE_CASE__ : List[Any] , SCREAMING_SNAKE_CASE__ : List[str]=[] , SCREAMING_SNAKE_CASE__ : Union[str, Any]=None , SCREAMING_SNAKE_CASE__ : str=0 ): __UpperCamelCase =BulletMenu(SCREAMING_SNAKE_CASE__ , SCREAMING_SNAKE_CASE__ ) __UpperCamelCase =menu.run(default_choice=SCREAMING_SNAKE_CASE__ ) return convert_value(SCREAMING_SNAKE_CASE__ ) if convert_value is not None else result def _UpperCAmelCase ( SCREAMING_SNAKE_CASE__ : Union[str, Any] ): __UpperCamelCase =int(SCREAMING_SNAKE_CASE__ ) return ComputeEnvironment(['LOCAL_MACHINE', 'AMAZON_SAGEMAKER'][value] ) def _UpperCAmelCase ( SCREAMING_SNAKE_CASE__ : List[str] ): __UpperCamelCase =int(SCREAMING_SNAKE_CASE__ ) return DistributedType(['NO', 'MULTI_CPU', 'MULTI_XPU', 'MULTI_GPU', 'MULTI_NPU', 'TPU'][value] ) def _UpperCAmelCase ( SCREAMING_SNAKE_CASE__ : Optional[int] ): __UpperCamelCase =int(SCREAMING_SNAKE_CASE__ ) return DynamoBackend(DYNAMO_BACKENDS[value] ).value def _UpperCAmelCase ( SCREAMING_SNAKE_CASE__ : Dict ): __UpperCamelCase =int(SCREAMING_SNAKE_CASE__ ) return PrecisionType(['no', 'fp16', 'bf16', 'fp8'][value] ) def _UpperCAmelCase ( SCREAMING_SNAKE_CASE__ : Optional[Any] ): __UpperCamelCase =int(SCREAMING_SNAKE_CASE__ ) return SageMakerDistributedType(['NO', 'DATA_PARALLEL', 'MODEL_PARALLEL'][value] ) def _UpperCAmelCase ( SCREAMING_SNAKE_CASE__ : Any ): return {"yes": True, "no": False}[value.lower()] class UpperCAmelCase__ ( argparse.RawDescriptionHelpFormatter ): """simple docstring""" def _a ( self , A_ , A_ , A_ , A_ ) -> Optional[int]: __UpperCamelCase =super()._format_usage(A_ , A_ , A_ , A_ ) __UpperCamelCase =usage.replace('<command> [<args>] ' , '' ) return usage
117
0
from ...configuration_utils import PretrainedConfig from ...utils import logging lowerCAmelCase = logging.get_logger(__name__) lowerCAmelCase = { 'caidas/swin2sr-classicalsr-x2-64': ( 'https://huggingface.co/caidas/swin2sr-classicalsr-x2-64/resolve/main/config.json' ), } class _a ( UpperCamelCase__ ): _lowercase : List[str] = '''swin2sr''' _lowercase : Dict = { '''hidden_size''': '''embed_dim''', '''num_attention_heads''': '''num_heads''', '''num_hidden_layers''': '''num_layers''', } def __init__( self: List[str] , UpperCamelCase_: Optional[Any]=64 , UpperCamelCase_: str=1 , UpperCamelCase_: Union[str, Any]=3 , UpperCamelCase_: Optional[Any]=180 , UpperCamelCase_: Union[str, Any]=[6, 6, 6, 6, 6, 6] , UpperCamelCase_: Union[str, Any]=[6, 6, 6, 6, 6, 6] , UpperCamelCase_: Union[str, Any]=8 , UpperCamelCase_: List[str]=2.0 , UpperCamelCase_: List[str]=True , UpperCamelCase_: int=0.0 , UpperCamelCase_: Union[str, Any]=0.0 , UpperCamelCase_: List[str]=0.1 , UpperCamelCase_: List[Any]="gelu" , UpperCamelCase_: Any=False , UpperCamelCase_: List[str]=0.02 , UpperCamelCase_: str=1E-5 , UpperCamelCase_: str=2 , UpperCamelCase_: Optional[int]=1.0 , UpperCamelCase_: Any="1conv" , UpperCamelCase_: Any="pixelshuffle" , **UpperCamelCase_: Union[str, Any] , ) -> str: """simple docstring""" super().__init__(**UpperCamelCase_ ) lowercase__ = image_size lowercase__ = patch_size lowercase__ = num_channels lowercase__ = embed_dim lowercase__ = depths lowercase__ = len(UpperCamelCase_ ) lowercase__ = num_heads lowercase__ = window_size lowercase__ = mlp_ratio lowercase__ = qkv_bias lowercase__ = hidden_dropout_prob lowercase__ = attention_probs_dropout_prob lowercase__ = drop_path_rate lowercase__ = hidden_act lowercase__ = use_absolute_embeddings lowercase__ = layer_norm_eps lowercase__ = initializer_range lowercase__ = upscale lowercase__ = img_range lowercase__ = resi_connection lowercase__ = upsampler
110
import unittest import numpy as np from transformers.testing_utils import require_torch, require_vision from transformers.utils import is_torch_available, is_vision_available from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs if is_torch_available(): import torch if is_vision_available(): from PIL import Image from transformers import LevitImageProcessor class _a ( unittest.TestCase ): def __init__( self: Dict , UpperCamelCase_: Any , UpperCamelCase_: Dict=7 , UpperCamelCase_: Optional[int]=3 , UpperCamelCase_: List[Any]=18 , UpperCamelCase_: Dict=30 , UpperCamelCase_: Optional[int]=400 , UpperCamelCase_: Optional[int]=True , UpperCamelCase_: Union[str, Any]=None , UpperCamelCase_: List[str]=True , UpperCamelCase_: Dict=None , UpperCamelCase_: Optional[int]=True , UpperCamelCase_: Tuple=[0.5, 0.5, 0.5] , UpperCamelCase_: List[str]=[0.5, 0.5, 0.5] , ) -> int: """simple docstring""" lowercase__ = size if size is not None else {'''shortest_edge''': 18} lowercase__ = crop_size if crop_size is not None else {'''height''': 18, '''width''': 18} lowercase__ = parent lowercase__ = batch_size lowercase__ = num_channels lowercase__ = image_size lowercase__ = min_resolution lowercase__ = max_resolution lowercase__ = do_resize lowercase__ = size lowercase__ = do_center_crop lowercase__ = crop_size lowercase__ = do_normalize lowercase__ = image_mean lowercase__ = image_std def lowerCamelCase_ ( self: List[str] ) -> Optional[Any]: """simple docstring""" return { "image_mean": self.image_mean, "image_std": self.image_std, "do_normalize": self.do_normalize, "do_resize": self.do_resize, "do_center_crop": self.do_center_crop, "size": self.size, "crop_size": self.crop_size, } @require_torch @require_vision class _a ( UpperCamelCase__ , unittest.TestCase ): _lowercase : List[str] = LevitImageProcessor if is_vision_available() else None def lowerCamelCase_ ( self: Optional[Any] ) -> List[str]: """simple docstring""" lowercase__ = LevitImageProcessingTester(self ) @property def lowerCamelCase_ ( self: List[Any] ) -> Any: """simple docstring""" return self.image_processor_tester.prepare_image_processor_dict() def lowerCamelCase_ ( self: Union[str, Any] ) -> str: """simple docstring""" lowercase__ = self.image_processing_class(**self.image_processor_dict ) self.assertTrue(hasattr(UpperCamelCase_ , '''image_mean''' ) ) self.assertTrue(hasattr(UpperCamelCase_ , '''image_std''' ) ) self.assertTrue(hasattr(UpperCamelCase_ , '''do_normalize''' ) ) self.assertTrue(hasattr(UpperCamelCase_ , '''do_resize''' ) ) self.assertTrue(hasattr(UpperCamelCase_ , '''do_center_crop''' ) ) self.assertTrue(hasattr(UpperCamelCase_ , '''size''' ) ) def lowerCamelCase_ ( self: str ) -> Tuple: """simple docstring""" lowercase__ = self.image_processing_class.from_dict(self.image_processor_dict ) self.assertEqual(image_processor.size , {'''shortest_edge''': 18} ) self.assertEqual(image_processor.crop_size , {'''height''': 18, '''width''': 18} ) lowercase__ = self.image_processing_class.from_dict(self.image_processor_dict , size=42 , crop_size=84 ) self.assertEqual(image_processor.size , {'''shortest_edge''': 42} ) self.assertEqual(image_processor.crop_size , {'''height''': 84, '''width''': 84} ) def lowerCamelCase_ ( self: List[str] ) -> Dict: """simple docstring""" pass def lowerCamelCase_ ( self: List[str] ) -> int: """simple docstring""" lowercase__ = self.image_processing_class(**self.image_processor_dict ) # create random PIL images lowercase__ = prepare_image_inputs(self.image_processor_tester , equal_resolution=UpperCamelCase_ ) for image in image_inputs: self.assertIsInstance(UpperCamelCase_ , Image.Image ) # Test not batched input lowercase__ = image_processing(image_inputs[0] , return_tensors='''pt''' ).pixel_values self.assertEqual( encoded_images.shape , ( 1, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size['''height'''], self.image_processor_tester.crop_size['''width'''], ) , ) # Test batched lowercase__ = image_processing(UpperCamelCase_ , return_tensors='''pt''' ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size['''height'''], self.image_processor_tester.crop_size['''width'''], ) , ) def lowerCamelCase_ ( self: Union[str, Any] ) -> Optional[int]: """simple docstring""" lowercase__ = self.image_processing_class(**self.image_processor_dict ) # create random numpy tensors lowercase__ = prepare_image_inputs(self.image_processor_tester , equal_resolution=UpperCamelCase_ , numpify=UpperCamelCase_ ) for image in image_inputs: self.assertIsInstance(UpperCamelCase_ , np.ndarray ) # Test not batched input lowercase__ = image_processing(image_inputs[0] , return_tensors='''pt''' ).pixel_values self.assertEqual( encoded_images.shape , ( 1, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size['''height'''], self.image_processor_tester.crop_size['''width'''], ) , ) # Test batched lowercase__ = image_processing(UpperCamelCase_ , return_tensors='''pt''' ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size['''height'''], self.image_processor_tester.crop_size['''width'''], ) , ) def lowerCamelCase_ ( self: Any ) -> Optional[Any]: """simple docstring""" lowercase__ = self.image_processing_class(**self.image_processor_dict ) # create random PyTorch tensors lowercase__ = prepare_image_inputs(self.image_processor_tester , equal_resolution=UpperCamelCase_ , torchify=UpperCamelCase_ ) for image in image_inputs: self.assertIsInstance(UpperCamelCase_ , torch.Tensor ) # Test not batched input lowercase__ = image_processing(image_inputs[0] , return_tensors='''pt''' ).pixel_values self.assertEqual( encoded_images.shape , ( 1, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size['''height'''], self.image_processor_tester.crop_size['''width'''], ) , ) # Test batched lowercase__ = image_processing(UpperCamelCase_ , return_tensors='''pt''' ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size['''height'''], self.image_processor_tester.crop_size['''width'''], ) , )
110
1
"""simple docstring""" # Logistic Regression from scratch # In[62]: # In[63]: # importing all the required libraries import numpy as np from matplotlib import pyplot as plt from sklearn import datasets def lowerCamelCase_ ( _lowerCamelCase ): return 1 / (1 + np.exp(-z )) def lowerCamelCase_ ( _lowerCamelCase , _lowerCamelCase ): return (-y * np.log(_lowerCamelCase ) - (1 - y) * np.log(1 - h )).mean() def lowerCamelCase_ ( _lowerCamelCase , _lowerCamelCase , _lowerCamelCase ): lowerCamelCase__ : int = np.dot(_lowerCamelCase , _lowerCamelCase ) return np.sum(y * scores - np.log(1 + np.exp(_lowerCamelCase ) ) ) def lowerCamelCase_ ( _lowerCamelCase , _lowerCamelCase , _lowerCamelCase , _lowerCamelCase=7_0000 ): lowerCamelCase__ : Dict = np.zeros(x.shape[1] ) for iterations in range(_lowerCamelCase ): lowerCamelCase__ : Union[str, Any] = np.dot(_lowerCamelCase , _lowerCamelCase ) lowerCamelCase__ : List[Any] = sigmoid_function(_lowerCamelCase ) lowerCamelCase__ : Union[str, Any] = np.dot(x.T , h - y ) / y.size lowerCamelCase__ : Tuple = theta - alpha * gradient # updating the weights lowerCamelCase__ : Tuple = np.dot(_lowerCamelCase , _lowerCamelCase ) lowerCamelCase__ : Dict = sigmoid_function(_lowerCamelCase ) lowerCamelCase__ : Tuple = cost_function(_lowerCamelCase , _lowerCamelCase ) if iterations % 100 == 0: print(f'''loss: {j} \t''' ) # printing the loss after every 100 iterations return theta # In[68]: if __name__ == "__main__": A_ : str = datasets.load_iris() A_ : str = iris.data[:, :2] A_ : Optional[Any] = (iris.target != 0) * 1 A_ : List[Any] = 0.1 A_ : Union[str, Any] = logistic_reg(alpha, x, y, max_iterations=7_00_00) print("theta: ", theta) # printing the theta i.e our weights vector def lowerCamelCase_ ( _lowerCamelCase ): return sigmoid_function( np.dot(_lowerCamelCase , _lowerCamelCase ) ) # predicting the value of probability from the logistic regression algorithm plt.figure(figsize=(10, 6)) plt.scatter(x[y == 0][:, 0], x[y == 0][:, 1], color="b", label="0") plt.scatter(x[y == 1][:, 0], x[y == 1][:, 1], color="r", label="1") ((A_), (A_)) : List[str] = (x[:, 0].min(), x[:, 0].max()) ((A_), (A_)) : List[Any] = (x[:, 1].min(), x[:, 1].max()) ((A_), (A_)) : Tuple = np.meshgrid(np.linspace(xa_min, xa_max), np.linspace(xa_min, xa_max)) A_ : Optional[int] = np.c_[xxa.ravel(), xxa.ravel()] A_ : List[str] = predict_prob(grid).reshape(xxa.shape) plt.contour(xxa, xxa, probs, [0.5], linewidths=1, colors="black") plt.legend() plt.show()
316
"""simple docstring""" from __future__ import annotations import queue class a_ : '''simple docstring''' def __init__(self, lowerCamelCase_ ): '''simple docstring''' lowerCamelCase__ : Union[str, Any] = data lowerCamelCase__ : Optional[int] = None lowerCamelCase__ : List[Any] = None def lowerCamelCase_ ( ): print('\n********Press N to stop entering at any point of time********\n' ) lowerCamelCase__ : str = input('Enter the value of the root node: ' ).strip().lower() lowerCamelCase__ : queue.Queue = queue.Queue() lowerCamelCase__ : Optional[Any] = TreeNode(int(_lowerCamelCase ) ) q.put(_lowerCamelCase ) while not q.empty(): lowerCamelCase__ : List[Any] = q.get() lowerCamelCase__ : str = f'''Enter the left node of {node_found.data}: ''' lowerCamelCase__ : Dict = input(_lowerCamelCase ).strip().lower() or 'n' if check == "n": return tree_node lowerCamelCase__ : str = TreeNode(int(_lowerCamelCase ) ) lowerCamelCase__ : Dict = left_node q.put(_lowerCamelCase ) lowerCamelCase__ : List[str] = f'''Enter the right node of {node_found.data}: ''' lowerCamelCase__ : List[str] = input(_lowerCamelCase ).strip().lower() or 'n' if check == "n": return tree_node lowerCamelCase__ : Optional[int] = TreeNode(int(_lowerCamelCase ) ) lowerCamelCase__ : Any = right_node q.put(_lowerCamelCase ) raise def lowerCamelCase_ ( _lowerCamelCase ): if not isinstance(_lowerCamelCase , _lowerCamelCase ) or not node: return print(node.data , end=',' ) pre_order(node.left ) pre_order(node.right ) def lowerCamelCase_ ( _lowerCamelCase ): if not isinstance(_lowerCamelCase , _lowerCamelCase ) or not node: return in_order(node.left ) print(node.data , end=',' ) in_order(node.right ) def lowerCamelCase_ ( _lowerCamelCase ): if not isinstance(_lowerCamelCase , _lowerCamelCase ) or not node: return post_order(node.left ) post_order(node.right ) print(node.data , end=',' ) def lowerCamelCase_ ( _lowerCamelCase ): if not isinstance(_lowerCamelCase , _lowerCamelCase ) or not node: return lowerCamelCase__ : queue.Queue = queue.Queue() q.put(_lowerCamelCase ) while not q.empty(): lowerCamelCase__ : Any = q.get() print(node_dequeued.data , end=',' ) if node_dequeued.left: q.put(node_dequeued.left ) if node_dequeued.right: q.put(node_dequeued.right ) def lowerCamelCase_ ( _lowerCamelCase ): if not isinstance(_lowerCamelCase , _lowerCamelCase ) or not node: return lowerCamelCase__ : queue.Queue = queue.Queue() q.put(_lowerCamelCase ) while not q.empty(): lowerCamelCase__ : List[Any] = [] while not q.empty(): lowerCamelCase__ : str = q.get() print(node_dequeued.data , end=',' ) if node_dequeued.left: list_.append(node_dequeued.left ) if node_dequeued.right: list_.append(node_dequeued.right ) print() for node in list_: q.put(_lowerCamelCase ) def lowerCamelCase_ ( _lowerCamelCase ): if not isinstance(_lowerCamelCase , _lowerCamelCase ) or not node: return lowerCamelCase__ : list[TreeNode] = [] lowerCamelCase__ : int = node while n or stack: while n: # start from root node, find its left child print(n.data , end=',' ) stack.append(_lowerCamelCase ) lowerCamelCase__ : Union[str, Any] = n.left # end of while means current node doesn't have left child lowerCamelCase__ : List[Any] = stack.pop() # start to traverse its right child lowerCamelCase__ : Optional[Any] = n.right def lowerCamelCase_ ( _lowerCamelCase ): if not isinstance(_lowerCamelCase , _lowerCamelCase ) or not node: return lowerCamelCase__ : list[TreeNode] = [] lowerCamelCase__ : int = node while n or stack: while n: stack.append(_lowerCamelCase ) lowerCamelCase__ : List[str] = n.left lowerCamelCase__ : Tuple = stack.pop() print(n.data , end=',' ) lowerCamelCase__ : Union[str, Any] = n.right def lowerCamelCase_ ( _lowerCamelCase ): if not isinstance(_lowerCamelCase , _lowerCamelCase ) or not node: return lowerCamelCase__ , lowerCamelCase__ : Any = [], [] lowerCamelCase__ : int = node stacka.append(_lowerCamelCase ) while stacka: # to find the reversed order of post order, store it in stack2 lowerCamelCase__ : List[str] = stacka.pop() if n.left: stacka.append(n.left ) if n.right: stacka.append(n.right ) stacka.append(_lowerCamelCase ) while stacka: # pop up from stack2 will be the post order print(stacka.pop().data , end=',' ) def lowerCamelCase_ ( _lowerCamelCase = "" , _lowerCamelCase=50 , _lowerCamelCase="*" ): if not s: return "\n" + width * char lowerCamelCase__ , lowerCamelCase__ : Dict = divmod(width - len(_lowerCamelCase ) - 2 , 2 ) return f'''{left * char} {s} {(left + extra) * char}''' if __name__ == "__main__": import doctest doctest.testmod() print(prompt("Binary Tree Traversals")) A_ : TreeNode = build_tree() print(prompt("Pre Order Traversal")) pre_order(node) print(prompt() + "\n") print(prompt("In Order Traversal")) in_order(node) print(prompt() + "\n") print(prompt("Post Order Traversal")) post_order(node) print(prompt() + "\n") print(prompt("Level Order Traversal")) level_order(node) print(prompt() + "\n") print(prompt("Actual Level Order Traversal")) level_order_actual(node) print("*" * 50 + "\n") print(prompt("Pre Order Traversal - Iteration Version")) pre_order_iter(node) print(prompt() + "\n") print(prompt("In Order Traversal - Iteration Version")) in_order_iter(node) print(prompt() + "\n") print(prompt("Post Order Traversal - Iteration Version")) post_order_iter(node) print(prompt())
316
1
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_sentencepiece_available, is_tf_available, is_tokenizers_available, is_torch_available, ) if is_sentencepiece_available(): from ..ta.tokenization_ta import TaTokenizer else: from ...utils.dummy_sentencepiece_objects import TaTokenizer a_ = TaTokenizer if is_tokenizers_available(): from ..ta.tokenization_ta_fast import TaTokenizerFast else: from ...utils.dummy_tokenizers_objects import TaTokenizerFast a_ = TaTokenizerFast a_ = {'configuration_mt5': ['MT5Config', 'MT5OnnxConfig']} try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: a_ = [ 'MT5EncoderModel', 'MT5ForConditionalGeneration', 'MT5ForQuestionAnswering', 'MT5Model', 'MT5PreTrainedModel', 'MT5Stack', ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: a_ = ['TFMT5EncoderModel', 'TFMT5ForConditionalGeneration', 'TFMT5Model'] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: a_ = ['FlaxMT5EncoderModel', 'FlaxMT5ForConditionalGeneration', 'FlaxMT5Model'] if TYPE_CHECKING: from .configuration_mta import MTaConfig, MTaOnnxConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_mta import ( MTaEncoderModel, MTaForConditionalGeneration, MTaForQuestionAnswering, MTaModel, MTaPreTrainedModel, MTaStack, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_mta import TFMTaEncoderModel, TFMTaForConditionalGeneration, TFMTaModel try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_flax_mta import FlaxMTaEncoderModel, FlaxMTaForConditionalGeneration, FlaxMTaModel else: import sys a_ = _LazyModule( __name__, globals()['__file__'], _import_structure, extra_objects={'MT5Tokenizer': MTaTokenizer, 'MT5TokenizerFast': MTaTokenizerFast}, module_spec=__spec__, )
175
'''simple docstring''' import os import textwrap import pyarrow as pa import pytest from datasets import ClassLabel, Features, Image from datasets.packaged_modules.csv.csv import Csv from ..utils import require_pil @pytest.fixture def lowerCamelCase (_SCREAMING_SNAKE_CASE : Dict ): __a : Optional[Any] = tmp_path / 'file.csv' __a : Union[str, Any] = textwrap.dedent( '\\n header1,header2\n 1,2\n 10,20\n ' ) with open(_SCREAMING_SNAKE_CASE , 'w' ) as f: f.write(_SCREAMING_SNAKE_CASE ) return str(_SCREAMING_SNAKE_CASE ) @pytest.fixture def lowerCamelCase (_SCREAMING_SNAKE_CASE : Union[str, Any] ): __a : str = tmp_path / 'malformed_file.csv' __a : int = textwrap.dedent( '\\n header1,header2\n 1,2\n 10,20,\n ' ) with open(_SCREAMING_SNAKE_CASE , 'w' ) as f: f.write(_SCREAMING_SNAKE_CASE ) return str(_SCREAMING_SNAKE_CASE ) @pytest.fixture def lowerCamelCase (_SCREAMING_SNAKE_CASE : Optional[Any] , _SCREAMING_SNAKE_CASE : List[str] ): __a : Optional[Any] = tmp_path / 'csv_with_image.csv' __a : Dict = textwrap.dedent( F"""\ image {image_file} """ ) with open(_SCREAMING_SNAKE_CASE , 'w' ) as f: f.write(_SCREAMING_SNAKE_CASE ) return str(_SCREAMING_SNAKE_CASE ) @pytest.fixture def lowerCamelCase (_SCREAMING_SNAKE_CASE : Optional[Any] ): __a : Union[str, Any] = tmp_path / 'csv_with_label.csv' __a : Any = textwrap.dedent( '\\n label\n good\n bad\n good\n ' ) with open(_SCREAMING_SNAKE_CASE , 'w' ) as f: f.write(_SCREAMING_SNAKE_CASE ) return str(_SCREAMING_SNAKE_CASE ) @pytest.fixture def lowerCamelCase (_SCREAMING_SNAKE_CASE : List[Any] ): __a : Dict = tmp_path / 'csv_with_int_list.csv' __a : Tuple = textwrap.dedent( '\\n int_list\n 1 2 3\n 4 5 6\n 7 8 9\n ' ) with open(_SCREAMING_SNAKE_CASE , 'w' ) as f: f.write(_SCREAMING_SNAKE_CASE ) return str(_SCREAMING_SNAKE_CASE ) def lowerCamelCase (_SCREAMING_SNAKE_CASE : List[str] , _SCREAMING_SNAKE_CASE : int , _SCREAMING_SNAKE_CASE : List[str] ): __a : int = Csv() __a : str = csv._generate_tables([[csv_file, malformed_csv_file]] ) with pytest.raises(_SCREAMING_SNAKE_CASE , match='Error tokenizing data' ): for _ in generator: pass assert any( record.levelname == 'ERROR' and 'Failed to read file' in record.message and os.path.basename(_SCREAMING_SNAKE_CASE ) in record.message for record in caplog.records ) @require_pil def lowerCamelCase (_SCREAMING_SNAKE_CASE : List[str] ): with open(_SCREAMING_SNAKE_CASE , encoding='utf-8' ) as f: __a : Tuple = f.read().splitlines()[1] __a : Tuple = Csv(encoding='utf-8' , features=Features({'image': Image()} ) ) __a : Any = csv._generate_tables([[csv_file_with_image]] ) __a : int = pa.concat_tables([table for _, table in generator] ) assert pa_table.schema.field('image' ).type == Image()() __a : Any = pa_table.to_pydict()['image'] assert generated_content == [{"path": image_file, "bytes": None}] def lowerCamelCase (_SCREAMING_SNAKE_CASE : Union[str, Any] ): with open(_SCREAMING_SNAKE_CASE , encoding='utf-8' ) as f: __a : Tuple = f.read().splitlines()[1:] __a : Optional[int] = Csv(encoding='utf-8' , features=Features({'label': ClassLabel(names=['good', 'bad'] )} ) ) __a : List[str] = csv._generate_tables([[csv_file_with_label]] ) __a : Dict = pa.concat_tables([table for _, table in generator] ) assert pa_table.schema.field('label' ).type == ClassLabel(names=['good', 'bad'] )() __a : int = pa_table.to_pydict()['label'] assert generated_content == [ClassLabel(names=['good', 'bad'] ).straint(_SCREAMING_SNAKE_CASE ) for label in labels] def lowerCamelCase (_SCREAMING_SNAKE_CASE : Optional[int] ): __a : str = Csv(encoding='utf-8' , sep=',' , converters={'int_list': lambda _SCREAMING_SNAKE_CASE : [int(_SCREAMING_SNAKE_CASE ) for i in x.split()]} ) __a : Any = csv._generate_tables([[csv_file_with_int_list]] ) __a : Any = pa.concat_tables([table for _, table in generator] ) assert pa.types.is_list(pa_table.schema.field('int_list' ).type ) __a : Tuple = pa_table.to_pydict()['int_list'] assert generated_content == [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
27
0
__SCREAMING_SNAKE_CASE = [0, 2, 4, 6, 8] __SCREAMING_SNAKE_CASE = [1, 3, 5, 7, 9] def UpperCAmelCase ( _lowerCamelCase , _lowerCamelCase , _lowerCamelCase , _lowerCamelCase ): if remaining_length == 0: if digits[0] == 0 or digits[-1] == 0: return 0 for i in range(length // 2 - 1 , -1 , -1 ): remainder += digits[i] + digits[length - i - 1] if remainder % 2 == 0: return 0 remainder //= 10 return 1 if remaining_length == 1: if remainder % 2 == 0: return 0 A : Union[str, Any] = 0 for digit in range(10 ): A : List[Any] = digit result += reversible_numbers( 0 , (remainder + 2 * digit) // 10 , _lowerCamelCase , _lowerCamelCase ) return result A : Optional[Any] = 0 for digita in range(10 ): A : Dict = digita if (remainder + digita) % 2 == 0: A : str = ODD_DIGITS else: A : str = EVEN_DIGITS for digita in other_parity_digits: A : Tuple = digita result += reversible_numbers( remaining_length - 2 , (remainder + digita + digita) // 10 , _lowerCamelCase , _lowerCamelCase , ) return result def UpperCAmelCase ( _lowerCamelCase = 9 ): A : List[str] = 0 for length in range(1 , max_power + 1 ): result += reversible_numbers(_lowerCamelCase , 0 , [0] * length , _lowerCamelCase ) return result if __name__ == "__main__": print(F"""{solution() = }""")
256
def UpperCAmelCase ( _lowerCamelCase , _lowerCamelCase , _lowerCamelCase , _lowerCamelCase ): A , A : Optional[Any] = len(_lowerCamelCase ), len(grid[0] ) if ( min(_lowerCamelCase , _lowerCamelCase ) < 0 or row == row_length or col == col_length or (row, col) in visit or grid[row][col] == 1 ): return 0 if row == row_length - 1 and col == col_length - 1: return 1 visit.add((row, col) ) A : Tuple = 0 count += depth_first_search(_lowerCamelCase , row + 1 , _lowerCamelCase , _lowerCamelCase ) count += depth_first_search(_lowerCamelCase , row - 1 , _lowerCamelCase , _lowerCamelCase ) count += depth_first_search(_lowerCamelCase , _lowerCamelCase , col + 1 , _lowerCamelCase ) count += depth_first_search(_lowerCamelCase , _lowerCamelCase , col - 1 , _lowerCamelCase ) visit.remove((row, col) ) return count if __name__ == "__main__": import doctest doctest.testmod()
256
1
import cva import numpy as np class a : """simple docstring""" def __init__( self , lowerCAmelCase_ , lowerCAmelCase_ ) -> List[str]: if k in (0.04, 0.06): _A = k _A = window_size else: raise ValueError("""invalid k value""" ) def __str__( self ) -> str: return str(self.k ) def UpperCAmelCase ( self , lowerCAmelCase_ ) -> tuple[cva.Mat, list[list[int]]]: _A = cva.imread(lowerCAmelCase_ , 0 ) _A , _A = img.shape _A = [] _A = img.copy() _A = cva.cvtColor(lowerCAmelCase_ , cva.COLOR_GRAY2RGB ) _A , _A = np.gradient(lowerCAmelCase_ ) _A = dx**2 _A = dy**2 _A = dx * dy _A = 0.04 _A = self.window_size // 2 for y in range(lowerCAmelCase_ , h - offset ): for x in range(lowerCAmelCase_ , w - offset ): _A = ixx[ y - offset : y + offset + 1, x - offset : x + offset + 1 ].sum() _A = iyy[ y - offset : y + offset + 1, x - offset : x + offset + 1 ].sum() _A = ixy[ y - offset : y + offset + 1, x - offset : x + offset + 1 ].sum() _A = (wxx * wyy) - (wxy**2) _A = wxx + wyy _A = det - k * (trace**2) # Can change the value if r > 0.5: corner_list.append([x, y, r] ) color_img.itemset((y, x, 0) , 0 ) color_img.itemset((y, x, 1) , 0 ) color_img.itemset((y, x, 2) , 2_55 ) return color_img, corner_list if __name__ == "__main__": _SCREAMING_SNAKE_CASE = HarrisCorner(0.04, 3) _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE = edge_detect.detect('path_to_image') cva.imwrite('detect.png', color_img)
180
import math import os import re import sys import unittest from pathlib import Path from typing import Tuple from unittest.mock import patch from parameterized import parameterized from transformers.testing_utils import ( CaptureStderr, ExtendSysPath, TestCasePlus, execute_subprocess_async, get_gpu_count, get_torch_dist_unique_port, require_apex, require_bitsandbytes, require_fairscale, require_torch, require_torch_gpu, require_torch_multi_gpu, require_torch_non_multi_gpu, slow, ) from transformers.trainer_callback import TrainerState from transformers.trainer_utils import set_seed _SCREAMING_SNAKE_CASE = os.path.abspath(os.path.dirname(__file__)) with ExtendSysPath(F'''{bindir}/../../examples/pytorch/translation'''): from run_translation import main # noqa set_seed(42) _SCREAMING_SNAKE_CASE = 'sshleifer/student_marian_en_ro_6_1' _SCREAMING_SNAKE_CASE = 'sshleifer/tiny-mbart' @require_torch class a ( __lowerCAmelCase ): """simple docstring""" def UpperCAmelCase ( self , lowerCAmelCase_=False , lowerCAmelCase_=None , lowerCAmelCase_=True , lowerCAmelCase_=True , lowerCAmelCase_=True , lowerCAmelCase_=True , ) -> int: _A = self.run_trainer( eval_steps=1 , max_len=12 , model_name=lowerCAmelCase_ , num_train_epochs=1 , distributed=lowerCAmelCase_ , extra_args_str=lowerCAmelCase_ , predict_with_generate=lowerCAmelCase_ , do_train=lowerCAmelCase_ , do_eval=lowerCAmelCase_ , do_predict=lowerCAmelCase_ , ) _A = TrainerState.load_from_json(os.path.join(lowerCAmelCase_ , """trainer_state.json""" ) ).log_history if not do_eval: return _A = [log for log in logs if """eval_loss""" in log.keys()] _A = eval_metrics[0] if predict_with_generate: assert "eval_bleu" in first_step_stats _A = eval_metrics[-1] assert isinstance(last_step_stats["""eval_bleu"""] , lowerCAmelCase_ ) assert not math.isnan(float(last_step_stats["""eval_loss"""] ) ), "eval_loss must not be `nan`" @require_torch_non_multi_gpu def UpperCAmelCase ( self ) -> Optional[int]: self.run_seqaseq_quick() @require_torch_multi_gpu def UpperCAmelCase ( self ) -> Dict: self.run_seqaseq_quick(distributed=lowerCAmelCase_ ) @require_torch_multi_gpu def UpperCAmelCase ( self ) -> Dict: self.run_seqaseq_quick(distributed=lowerCAmelCase_ ) @unittest.skip("""Requires an update of the env running those tests""" ) @require_torch_multi_gpu @require_fairscale def UpperCAmelCase ( self ) -> str: self.run_seqaseq_quick(distributed=lowerCAmelCase_ , extra_args_str="""--sharded_ddp simple""" ) @unittest.skip("""Requires an update of the env running those tests""" ) @require_torch_multi_gpu @require_fairscale def UpperCAmelCase ( self ) -> Dict: self.run_seqaseq_quick(distributed=lowerCAmelCase_ , extra_args_str="""--sharded_ddp simple --fp16""" ) @unittest.skip("""Requires an update of the env running those tests""" ) @require_torch_multi_gpu @require_fairscale def UpperCAmelCase ( self ) -> Optional[Any]: self.run_seqaseq_quick(distributed=lowerCAmelCase_ , extra_args_str="""--sharded_ddp zero_dp_2""" , predict_with_generate=lowerCAmelCase_ ) @unittest.skip("""Requires an update of the env running those tests""" ) @require_torch_multi_gpu @require_fairscale def UpperCAmelCase ( self ) -> Tuple: self.run_seqaseq_quick( distributed=lowerCAmelCase_ , extra_args_str="""--sharded_ddp zero_dp_2 --fp16""" , predict_with_generate=lowerCAmelCase_ ) @require_apex @require_torch_gpu def UpperCAmelCase ( self ) -> int: # XXX: apex breaks the trainer if it's run twice e.g. run_seq2seq.main() from the same # program and it breaks other tests that run from the same pytest worker, therefore until this is # sorted out it must be run only in an external program, that is distributed=True in this # test and only under one or more gpus - if we want cpu will need to make a special test # # specifically to the problem traced it to self.optimizer.step() - if it's run 2nd time via # 2nd main() call it botches the future eval. # self.run_seqaseq_quick(distributed=lowerCAmelCase_ , extra_args_str="""--fp16 --fp16_backend=apex""" ) # test 2nd time - was getting eval_loss': nan' # to reproduce the problem set distributed=False self.run_seqaseq_quick(distributed=lowerCAmelCase_ , extra_args_str="""--fp16 --fp16_backend=apex""" ) @parameterized.expand(["""base""", """low""", """high""", """mixed"""] ) @require_torch_multi_gpu def UpperCAmelCase ( self , lowerCAmelCase_ ) -> int: # as each sub-test is slow-ish split into multiple sub-tests to avoid CI timeout _A = { # test with the default log_level - should be info and thus log info once """base""": {"""extra_args_str""": """""", """n_matches""": 1}, # test with low log_level and log_level_replica - should be noisy on all processes # now the info string should appear twice on 2 processes """low""": {"""extra_args_str""": """--log_level debug --log_level_replica debug""", """n_matches""": 2}, # test with high log_level and low log_level_replica # now the info string should appear once only on the replica """high""": {"""extra_args_str""": """--log_level error --log_level_replica debug""", """n_matches""": 1}, # test with high log_level and log_level_replica - should be quiet on all processes """mixed""": {"""extra_args_str""": """--log_level error --log_level_replica error""", """n_matches""": 0}, } _A = experiments[experiment_id] _A = {"""distributed""": True, """predict_with_generate""": False, """do_eval""": False, """do_predict""": False} _A = """Running training""" with CaptureStderr() as cl: self.run_seqaseq_quick(**lowerCAmelCase_ , extra_args_str=data["""extra_args_str"""] ) _A = len(re.findall(lowerCAmelCase_ , cl.err ) ) self.assertEqual(lowerCAmelCase_ , data["""n_matches"""] ) @slow def UpperCAmelCase ( self ) -> Dict: _A = self.run_trainer( eval_steps=2 , max_len=1_28 , model_name=lowerCAmelCase_ , learning_rate=3E-4 , num_train_epochs=10 , distributed=lowerCAmelCase_ , ) # Check metrics _A = TrainerState.load_from_json(os.path.join(lowerCAmelCase_ , """trainer_state.json""" ) ).log_history _A = [log for log in logs if """eval_loss""" in log.keys()] _A = eval_metrics[0] _A = eval_metrics[-1] assert first_step_stats["eval_loss"] > last_step_stats["eval_loss"], "model learned nothing" assert isinstance(last_step_stats["""eval_bleu"""] , lowerCAmelCase_ ) # test if do_predict saves generations and metrics _A = os.listdir(lowerCAmelCase_ ) _A = {os.path.basename(lowerCAmelCase_ ) for p in contents} assert "generated_predictions.txt" in contents assert "predict_results.json" in contents @slow @require_bitsandbytes def UpperCAmelCase ( self ) -> Optional[Any]: from transformers.training_args import OptimizerNames def train_and_return_metrics(lowerCAmelCase_ ) -> Tuple[int, float]: _A = """--skip_memory_metrics 0""" _A = self.run_trainer( max_len=1_28 , model_name=lowerCAmelCase_ , learning_rate=3E-4 , num_train_epochs=1 , optim=lowerCAmelCase_ , distributed=lowerCAmelCase_ , extra_args_str=lowerCAmelCase_ , do_eval=lowerCAmelCase_ , do_predict=lowerCAmelCase_ , n_gpus_to_use=1 , ) # Check metrics _A = TrainerState.load_from_json(Path(lowerCAmelCase_ , """trainer_state.json""" ) ).log_history _A = int(logs[0]["""train_mem_gpu_peaked_delta"""] / 2**20 ) _A = int(logs[0]["""train_mem_gpu_alloc_delta"""] / 2**20 ) _A = logs[0]["""train_loss"""] return gpu_peak_mem_mb, gpu_alloc_mem_mb, loss _A , _A , _A = train_and_return_metrics(OptimizerNames.ADAMW_TORCH.value ) _A , _A , _A = train_and_return_metrics(OptimizerNames.ADAMW_BNB.value ) _A = gpu_alloc_mem_orig - gpu_alloc_mem_bnb _A = gpu_peak_mem_orig + gpu_alloc_mem_orig _A = gpu_peak_mem_bnb + gpu_alloc_mem_bnb _A = gpu_total_mem_orig - gpu_total_mem_bnb # sshleifer/student_marian_en_ro_6_1 has 54M parameter, 29M of which is `nn.Embedding` which # doesn't get quantized and remains in fp32. Therefore we only have 25M parameters quantized # in 2 bytes and the diff in optim memory usage is derived as so: # # - normal 25*8=~200MB (8 bytes per param) # - bnb 25*2= ~50MB (2 bytes per param) # # Thus we should expect ~150MB total memory saved. # # Peak memory should be the same - the total should be different by about that same margin # # After leaving a small margin to accommodate for differences between gpus let's check # that we have at least 120MB in savings _A = 1_20 # uncomment the following if this test starts failing - requires py38 for a new print feature # gpu_peak_mem_diff = gpu_peak_mem_orig - gpu_peak_mem_bnb # print(f"{gpu_alloc_mem_orig=}MB {gpu_peak_mem_orig=}MB {gpu_alloc_mem_orig+gpu_peak_mem_orig=}MB") # print(f" {gpu_alloc_mem_bnb=}MB {gpu_peak_mem_bnb=}MB {gpu_alloc_mem_bnb+gpu_peak_mem_bnb=}MB") # print(f"{gpu_alloc_mem_diff=}MB") # print(f"{gpu_peak_mem_diff=}MB") # print(f"{gpu_total_mem_orig=}MB, {gpu_total_mem_bnb=}MB") # print(f"{gpu_total_mem_diff=}MB, {gpu_total_mem_diff=}MB") self.assertGreater( lowerCAmelCase_ , lowerCAmelCase_ , """should use ~150MB less alloc gpu memory with BNB, compared to without it for this model but got""" F''' a difference of {gpu_alloc_mem_diff}MB, with gpu_alloc_mem_orig={gpu_alloc_mem_orig}MB and''' F''' gpu_alloc_mem_bnb={gpu_alloc_mem_bnb}MB''' , ) self.assertGreater( lowerCAmelCase_ , lowerCAmelCase_ , """should use ~150MB less total gpu memory with BNB, compared to without it for this model but got""" F''' a difference of {gpu_total_mem_diff}MB, with gpu_total_mem_orig={gpu_total_mem_orig}MB and''' F''' gpu_total_mem_bnb={gpu_total_mem_bnb}MB''' , ) self.assertEqual( lowerCAmelCase_ , lowerCAmelCase_ , F'''loss should be the same, but got loss_orig={loss_orig}, loss_bnb={loss_bnb}''' ) def UpperCAmelCase ( self , lowerCAmelCase_ , lowerCAmelCase_ , lowerCAmelCase_ , lowerCAmelCase_ = 3E-3 , lowerCAmelCase_ = "adafactor" , lowerCAmelCase_ = False , lowerCAmelCase_ = None , lowerCAmelCase_ = 0 , lowerCAmelCase_ = True , lowerCAmelCase_ = True , lowerCAmelCase_ = True , lowerCAmelCase_ = True , lowerCAmelCase_ = None , ) -> str: _A = self.test_file_dir / """../fixtures/tests_samples/wmt_en_ro""" _A = self.get_auto_remove_tmp_dir() _A = F''' --model_name_or_path {model_name} --train_file {data_dir}/train.json --validation_file {data_dir}/val.json --test_file {data_dir}/test.json --output_dir {output_dir} --overwrite_output_dir --max_train_samples 8 --max_source_length {max_len} --max_target_length {max_len} --do_train --num_train_epochs {str(lowerCAmelCase_ )} --per_device_train_batch_size 4 --learning_rate {learning_rate} --warmup_steps 8 --logging_steps 0 --logging_strategy no --save_steps {str(lowerCAmelCase_ )} --group_by_length --label_smoothing_factor 0.1 --target_lang ro_RO --source_lang en_XX '''.split() _A = F''' --do_eval --per_device_eval_batch_size 4 --max_eval_samples 8 --val_max_target_length {max_len} --evaluation_strategy steps --eval_steps {str(lowerCAmelCase_ )} '''.split() _A = """ --do_predict """.split() _A = [] if do_train: args += args_train if do_eval: args += args_eval if do_predict: args += args_predict if predict_with_generate: args += "--predict_with_generate".split() if do_train: if optim == "adafactor": args += "--adafactor".split() else: args += F'''--optim {optim}'''.split() if extra_args_str is not None: args += extra_args_str.split() if distributed: if n_gpus_to_use is None: _A = get_gpu_count() _A = get_torch_dist_unique_port() _A = F''' -m torch.distributed.run --nproc_per_node={n_gpus_to_use} --master_port={master_port} {self.examples_dir_str}/pytorch/translation/run_translation.py '''.split() _A = [sys.executable] + distributed_args + args # keep for quick debug # print(" ".join([f"\nPYTHONPATH={self.src_dir_str}"] +cmd)); die execute_subprocess_async(lowerCAmelCase_ , env=self.get_env() ) else: _A = ["""run_translation.py"""] + args with patch.object(lowerCAmelCase_ , """argv""" , lowerCAmelCase_ ): main() return output_dir
180
1
import unittest import numpy as np from transformers.testing_utils import require_torch, require_vision from transformers.utils import is_torch_available, is_vision_available from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs if is_torch_available(): import torch if is_vision_available(): from PIL import Image from transformers import ViTImageProcessor class _a ( unittest.TestCase ): def __init__( self: Optional[int] , UpperCamelCase_: List[Any] , UpperCamelCase_: List[Any]=13 , UpperCamelCase_: str=3 , UpperCamelCase_: int=224 , UpperCamelCase_: Dict=30 , UpperCamelCase_: int=400 , UpperCamelCase_: str=True , UpperCamelCase_: List[str]=None , UpperCamelCase_: Any=True , UpperCamelCase_: str=[0.5, 0.5, 0.5] , UpperCamelCase_: int=[0.5, 0.5, 0.5] , ) -> str: """simple docstring""" lowercase__ = size if size is not None else {'''height''': 18, '''width''': 18} lowercase__ = parent lowercase__ = batch_size lowercase__ = num_channels lowercase__ = image_size lowercase__ = min_resolution lowercase__ = max_resolution lowercase__ = do_resize lowercase__ = size lowercase__ = do_normalize lowercase__ = image_mean lowercase__ = image_std def lowerCamelCase_ ( self: List[Any] ) -> Optional[Any]: """simple docstring""" return { "image_mean": self.image_mean, "image_std": self.image_std, "do_normalize": self.do_normalize, "do_resize": self.do_resize, "size": self.size, } @require_torch @require_vision class _a ( UpperCamelCase__ , unittest.TestCase ): _lowercase : int = ViTImageProcessor if is_vision_available() else None def lowerCamelCase_ ( self: List[str] ) -> List[Any]: """simple docstring""" lowercase__ = EfficientFormerImageProcessorTester(self ) @property def lowerCamelCase_ ( self: Any ) -> Tuple: """simple docstring""" return self.image_proc_tester.prepare_image_processor_dict() def lowerCamelCase_ ( self: Any ) -> Union[str, Any]: """simple docstring""" lowercase__ = self.image_processing_class(**self.image_processor_dict ) self.assertTrue(hasattr(UpperCamelCase_ , '''image_mean''' ) ) self.assertTrue(hasattr(UpperCamelCase_ , '''image_std''' ) ) self.assertTrue(hasattr(UpperCamelCase_ , '''do_normalize''' ) ) self.assertTrue(hasattr(UpperCamelCase_ , '''do_resize''' ) ) self.assertTrue(hasattr(UpperCamelCase_ , '''size''' ) ) def lowerCamelCase_ ( self: int ) -> int: """simple docstring""" pass def lowerCamelCase_ ( self: Union[str, Any] ) -> int: """simple docstring""" lowercase__ = self.image_processing_class(**self.image_processor_dict ) # create random PIL images lowercase__ = prepare_image_inputs(self.image_proc_tester , equal_resolution=UpperCamelCase_ ) for image in image_inputs: self.assertIsInstance(UpperCamelCase_ , Image.Image ) # Test not batched input lowercase__ = image_processor(image_inputs[0] , return_tensors='''pt''' ).pixel_values self.assertEqual( encoded_images.shape , ( 1, self.image_proc_tester.num_channels, self.image_proc_tester.size['''height'''], self.image_proc_tester.size['''width'''], ) , ) # Test batched lowercase__ = image_processor(UpperCamelCase_ , return_tensors='''pt''' ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_proc_tester.batch_size, self.image_proc_tester.num_channels, self.image_proc_tester.size['''height'''], self.image_proc_tester.size['''width'''], ) , ) def lowerCamelCase_ ( self: Union[str, Any] ) -> int: """simple docstring""" lowercase__ = self.image_processing_class(**self.image_processor_dict ) # create random numpy tensors lowercase__ = prepare_image_inputs(self.image_proc_tester , equal_resolution=UpperCamelCase_ , numpify=UpperCamelCase_ ) for image in image_inputs: self.assertIsInstance(UpperCamelCase_ , np.ndarray ) # Test not batched input lowercase__ = image_processor(image_inputs[0] , return_tensors='''pt''' ).pixel_values self.assertEqual( encoded_images.shape , ( 1, self.image_proc_tester.num_channels, self.image_proc_tester.size['''height'''], self.image_proc_tester.size['''width'''], ) , ) # Test batched lowercase__ = image_processor(UpperCamelCase_ , return_tensors='''pt''' ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_proc_tester.batch_size, self.image_proc_tester.num_channels, self.image_proc_tester.size['''height'''], self.image_proc_tester.size['''width'''], ) , ) def lowerCamelCase_ ( self: Any ) -> Optional[int]: """simple docstring""" lowercase__ = self.image_processing_class(**self.image_processor_dict ) # create random PyTorch tensors lowercase__ = prepare_image_inputs(self.image_proc_tester , equal_resolution=UpperCamelCase_ , torchify=UpperCamelCase_ ) for image in image_inputs: self.assertIsInstance(UpperCamelCase_ , torch.Tensor ) # Test not batched input lowercase__ = image_processor(image_inputs[0] , return_tensors='''pt''' ).pixel_values self.assertEqual( encoded_images.shape , ( 1, self.image_proc_tester.num_channels, self.image_proc_tester.size['''height'''], self.image_proc_tester.size['''width'''], ) , ) # Test batched lowercase__ = image_processor(UpperCamelCase_ , return_tensors='''pt''' ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_proc_tester.batch_size, self.image_proc_tester.num_channels, self.image_proc_tester.size['''height'''], self.image_proc_tester.size['''width'''], ) , )
93
lowerCAmelCase = [sum(int(c, 10) ** 2 for c in i.__str__()) for i in range(10_0000)] def _a ( SCREAMING_SNAKE_CASE ): """simple docstring""" lowercase__ = 0 while number: # Increased Speed Slightly by checking every 5 digits together. sum_of_digits_squared += DIGITS_SQUARED[number % 10_00_00] number //= 10_00_00 return sum_of_digits_squared # There are 2 Chains made, # One ends with 89 with the chain member 58 being the one which when declared first, # there will be the least number of iterations for all the members to be checked. # The other one ends with 1 and has only one element 1. # So 58 and 1 are chosen to be declared at the starting. # Changed dictionary to an array to quicken the solution lowerCAmelCase = [None] * 1000_0000 lowerCAmelCase = True lowerCAmelCase = False def _a ( SCREAMING_SNAKE_CASE ): """simple docstring""" if CHAINS[number - 1] is not None: return CHAINS[number - 1] # type: ignore lowercase__ = chain(next_number(SCREAMING_SNAKE_CASE ) ) lowercase__ = number_chain while number < 10_00_00_00: lowercase__ = number_chain number *= 10 return number_chain def _a ( SCREAMING_SNAKE_CASE = 10_00_00_00 ): """simple docstring""" for i in range(1 , SCREAMING_SNAKE_CASE ): if CHAINS[i] is None: chain(i + 1 ) return CHAINS[:number].count(SCREAMING_SNAKE_CASE ) if __name__ == "__main__": import doctest doctest.testmod() print(f"""{solution() = }""")
93
1
"""simple docstring""" from __future__ import annotations class _UpperCAmelCase : '''simple docstring''' def __init__( self , snake_case_ = 0 ): """simple docstring""" A_ : List[str] = key def lowerCamelCase_ ( self , snake_case_ , snake_case_ ): """simple docstring""" assert isinstance(snake_case_ , snake_case_ ) and isinstance(snake_case_ , snake_case_ ) A_ : List[Any] = key or self.__key or 1 # make sure key is an appropriate size key %= 2_5_5 return [chr(ord(snake_case_ ) ^ key ) for ch in content] def lowerCamelCase_ ( self , snake_case_ , snake_case_ ): """simple docstring""" assert isinstance(snake_case_ , snake_case_ ) and isinstance(snake_case_ , snake_case_ ) A_ : Dict = key or self.__key or 1 # make sure key is an appropriate size key %= 2_5_5 return [chr(ord(snake_case_ ) ^ key ) for ch in content] def lowerCamelCase_ ( self , snake_case_ , snake_case_ = 0 ): """simple docstring""" assert isinstance(snake_case_ , snake_case_ ) and isinstance(snake_case_ , snake_case_ ) A_ : str = key or self.__key or 1 # make sure key can be any size while key > 2_5_5: key -= 2_5_5 # This will be returned A_ : Union[str, Any] = '' for ch in content: ans += chr(ord(snake_case_ ) ^ key ) return ans def lowerCamelCase_ ( self , snake_case_ , snake_case_ = 0 ): """simple docstring""" assert isinstance(snake_case_ , snake_case_ ) and isinstance(snake_case_ , snake_case_ ) A_ : Any = key or self.__key or 1 # make sure key can be any size while key > 2_5_5: key -= 2_5_5 # This will be returned A_ : Optional[Any] = '' for ch in content: ans += chr(ord(snake_case_ ) ^ key ) return ans def lowerCamelCase_ ( self , snake_case_ , snake_case_ = 0 ): """simple docstring""" assert isinstance(snake_case_ , snake_case_ ) and isinstance(snake_case_ , snake_case_ ) try: with open(snake_case_ ) as fin, open('encrypt.out' , 'w+' ) as fout: # actual encrypt-process for line in fin: fout.write(self.encrypt_string(snake_case_ , snake_case_ ) ) except OSError: return False return True def lowerCamelCase_ ( self , snake_case_ , snake_case_ ): """simple docstring""" assert isinstance(snake_case_ , snake_case_ ) and isinstance(snake_case_ , snake_case_ ) try: with open(snake_case_ ) as fin, open('decrypt.out' , 'w+' ) as fout: # actual encrypt-process for line in fin: fout.write(self.decrypt_string(snake_case_ , snake_case_ ) ) 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")
286
"""simple docstring""" from typing import Optional, Union import torch from torch import nn from ...configuration_utils import ConfigMixin, register_to_config from ...models.modeling_utils import ModelMixin class _UpperCAmelCase ( UpperCAmelCase__ , UpperCAmelCase__ ): '''simple docstring''' @register_to_config def __init__( self , snake_case_ = 7_6_8 , ): """simple docstring""" super().__init__() A_ : Optional[int] = nn.Parameter(torch.zeros(1 , snake_case_ ) ) A_ : Optional[int] = nn.Parameter(torch.ones(1 , snake_case_ ) ) def lowerCamelCase_ ( self , snake_case_ = None , snake_case_ = None , ): """simple docstring""" A_ : str = nn.Parameter(self.mean.to(snake_case_ ).to(snake_case_ ) ) A_ : Optional[int] = nn.Parameter(self.std.to(snake_case_ ).to(snake_case_ ) ) return self def lowerCamelCase_ ( self , snake_case_ ): """simple docstring""" A_ : Tuple = (embeds - self.mean) * 1.0 / self.std return embeds def lowerCamelCase_ ( self , snake_case_ ): """simple docstring""" A_ : List[str] = (embeds * self.std) + self.mean return embeds
286
1
from math import ceil, sqrt def lowerCamelCase__ (_UpperCAmelCase = 100_0000): SCREAMING_SNAKE_CASE = 0 for outer_width in range(3 , (limit // 4) + 2): if outer_width**2 > limit: SCREAMING_SNAKE_CASE = max(ceil(sqrt(outer_width**2 - limit)) , 1) else: SCREAMING_SNAKE_CASE = 1 if (outer_width - hole_width_lower_bound) % 2: hole_width_lower_bound += 1 answer += (outer_width - hole_width_lower_bound - 2) // 2 + 1 return answer if __name__ == "__main__": print(f"""{solution() = }""")
354
class _snake_case : def __init__( self , a) -> Optional[Any]: SCREAMING_SNAKE_CASE = val SCREAMING_SNAKE_CASE = None SCREAMING_SNAKE_CASE = None def SCREAMING_SNAKE_CASE__ ( self , a) -> str: if self.val: if val < self.val: if self.left is None: SCREAMING_SNAKE_CASE = Node(a) else: self.left.insert(a) elif val > self.val: if self.right is None: SCREAMING_SNAKE_CASE = Node(a) else: self.right.insert(a) else: SCREAMING_SNAKE_CASE = val def lowerCamelCase__ (_UpperCAmelCase , _UpperCAmelCase): # Recursive traversal if root: inorder(root.left , _UpperCAmelCase) res.append(root.val) inorder(root.right , _UpperCAmelCase) def lowerCamelCase__ (_UpperCAmelCase): # Build BST if len(_UpperCAmelCase) == 0: return arr SCREAMING_SNAKE_CASE = Node(arr[0]) for i in range(1 , len(_UpperCAmelCase)): root.insert(arr[i]) # Traverse BST in order. SCREAMING_SNAKE_CASE = [] inorder(_UpperCAmelCase , _UpperCAmelCase) return res if __name__ == "__main__": print(tree_sort([10, 1, 3, 2, 9, 14, 13]))
327
0
import argparse import os from io import BytesIO from pathlib import Path import requests from clip_retrieval.clip_client import ClipClient from PIL import Image from tqdm import tqdm def a__ ( A_, A_, A_ ): '''simple docstring''' __magic_name__ = 1.5 __magic_name__ = int(factor * num_class_images ) __magic_name__ = ClipClient( url="""https://knn.laion.ai/knn-service""", indice_name="""laion_400m""", num_images=A_, aesthetic_weight=0.1 ) os.makedirs(f'''{class_data_dir}/images''', exist_ok=A_ ) if len(list(Path(f'''{class_data_dir}/images''' ).iterdir() ) ) >= num_class_images: return while True: __magic_name__ = client.query(text=A_ ) if len(A_ ) >= factor * num_class_images or num_images > 1e4: break else: __magic_name__ = int(factor * num_images ) __magic_name__ = ClipClient( url="""https://knn.laion.ai/knn-service""", indice_name="""laion_400m""", num_images=A_, aesthetic_weight=0.1, ) __magic_name__ = 0 __magic_name__ = 0 __magic_name__ = tqdm(desc="""downloading real regularization images""", total=A_ ) with open(f'''{class_data_dir}/caption.txt''', """w""" ) as fa, open(f'''{class_data_dir}/urls.txt''', """w""" ) as fa, open( f'''{class_data_dir}/images.txt''', """w""" ) as fa: while total < num_class_images: __magic_name__ = class_images[count] count += 1 try: __magic_name__ = requests.get(images["""url"""] ) if img.status_code == 200: __magic_name__ = Image.open(BytesIO(img.content ) ) with open(f'''{class_data_dir}/images/{total}.jpg''', """wb""" ) as f: f.write(img.content ) fa.write(images["""caption"""] + """\n""" ) fa.write(images["""url"""] + """\n""" ) fa.write(f'''{class_data_dir}/images/{total}.jpg''' + """\n""" ) total += 1 pbar.update(1 ) else: continue except Exception: continue return def a__ ( ): '''simple docstring''' __magic_name__ = argparse.ArgumentParser("""""", add_help=A_ ) parser.add_argument("""--class_prompt""", help="""text prompt to retrieve images""", required=A_, type=A_ ) parser.add_argument("""--class_data_dir""", help="""path to save images""", required=A_, type=A_ ) parser.add_argument("""--num_class_images""", help="""number of images to download""", default=200, type=A_ ) return parser.parse_args() if __name__ == "__main__": __lowerCAmelCase : Dict = parse_args() retrieve(args.class_prompt, args.class_data_dir, args.num_class_images)
88
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available __UpperCamelCase : Dict = { """configuration_jukebox""": [ """JUKEBOX_PRETRAINED_CONFIG_ARCHIVE_MAP""", """JukeboxConfig""", """JukeboxPriorConfig""", """JukeboxVQVAEConfig""", ], """tokenization_jukebox""": ["""JukeboxTokenizer"""], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __UpperCamelCase : Tuple = [ """JUKEBOX_PRETRAINED_MODEL_ARCHIVE_LIST""", """JukeboxModel""", """JukeboxPreTrainedModel""", """JukeboxVQVAE""", """JukeboxPrior""", ] if TYPE_CHECKING: from .configuration_jukebox import ( JUKEBOX_PRETRAINED_CONFIG_ARCHIVE_MAP, JukeboxConfig, JukeboxPriorConfig, JukeboxVQVAEConfig, ) from .tokenization_jukebox import JukeboxTokenizer try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_jukebox import ( JUKEBOX_PRETRAINED_MODEL_ARCHIVE_LIST, JukeboxModel, JukeboxPreTrainedModel, JukeboxPrior, JukeboxVQVAE, ) else: import sys __UpperCamelCase : str = _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)
307
0
from typing import Dict import numpy as np from ..utils import add_end_docstrings, is_tf_available, is_torch_available, logging from .base import PIPELINE_INIT_ARGS, GenericTensor, Pipeline, PipelineException if is_tf_available(): import tensorflow as tf from ..tf_utils import stable_softmax if is_torch_available(): import torch __lowerCamelCase : Optional[Any] = logging.get_logger(__name__) @add_end_docstrings( __snake_case , r'\n top_k (`int`, defaults to 5):\n The number of predictions to return.\n targets (`str` or `List[str]`, *optional*):\n When passed, the model will limit the scores to the passed targets instead of looking up in the whole\n vocab. If the provided targets are not in the model vocab, they will be tokenized and the first resulting\n token will be used (with a warning, and that might be slower).\n\n ' , ) class A__ ( __snake_case ): def __UpperCamelCase( self , A_ ): '''simple docstring''' if self.framework == "tf": UpperCamelCase : Tuple = tf.where(input_ids == self.tokenizer.mask_token_id ).numpy() elif self.framework == "pt": UpperCamelCase : List[Any] = torch.nonzero(input_ids == self.tokenizer.mask_token_id , as_tuple=A_ ) else: raise ValueError("Unsupported framework" ) return masked_index def __UpperCamelCase( self , A_ ): '''simple docstring''' UpperCamelCase : Dict = self.get_masked_index(A_ ) UpperCamelCase : Optional[Any] = np.prod(masked_index.shape ) if numel < 1: raise PipelineException( "fill-mask" , self.model.base_model_prefix , F"""No mask_token ({self.tokenizer.mask_token}) found on the input""" , ) def __UpperCamelCase( self , A_ ): '''simple docstring''' if isinstance(A_ , A_ ): for model_input in model_inputs: self._ensure_exactly_one_mask_token(model_input["input_ids"][0] ) else: for input_ids in model_inputs["input_ids"]: self._ensure_exactly_one_mask_token(A_ ) def __UpperCamelCase( self , A_ , A_=None , **A_ ): '''simple docstring''' if return_tensors is None: UpperCamelCase : List[str] = self.framework UpperCamelCase : int = self.tokenizer(A_ , return_tensors=A_ ) self.ensure_exactly_one_mask_token(A_ ) return model_inputs def __UpperCamelCase( self , A_ ): '''simple docstring''' UpperCamelCase : str = self.model(**A_ ) UpperCamelCase : List[Any] = model_inputs["input_ids"] return model_outputs def __UpperCamelCase( self , A_ , A_=5 , A_=None ): '''simple docstring''' if target_ids is not None and target_ids.shape[0] < top_k: UpperCamelCase : str = target_ids.shape[0] UpperCamelCase : List[str] = model_outputs["input_ids"][0] UpperCamelCase : Union[str, Any] = model_outputs["logits"] if self.framework == "tf": UpperCamelCase : str = tf.where(input_ids == self.tokenizer.mask_token_id ).numpy()[:, 0] UpperCamelCase : Dict = outputs.numpy() UpperCamelCase : Any = outputs[0, masked_index, :] UpperCamelCase : int = stable_softmax(A_ , axis=-1 ) if target_ids is not None: UpperCamelCase : Union[str, Any] = tf.gather_nd(tf.squeeze(A_ , 0 ) , target_ids.reshape(-1 , 1 ) ) UpperCamelCase : Union[str, Any] = tf.expand_dims(A_ , 0 ) UpperCamelCase : Optional[int] = tf.math.top_k(A_ , k=A_ ) UpperCamelCase , UpperCamelCase : str = topk.values.numpy(), topk.indices.numpy() else: UpperCamelCase : Tuple = torch.nonzero(input_ids == self.tokenizer.mask_token_id , as_tuple=A_ ).squeeze(-1 ) # Fill mask pipeline supports only one ${mask_token} per sample UpperCamelCase : Tuple = outputs[0, masked_index, :] UpperCamelCase : Union[str, Any] = logits.softmax(dim=-1 ) if target_ids is not None: UpperCamelCase : Any = probs[..., target_ids] UpperCamelCase , UpperCamelCase : List[str] = probs.topk(A_ ) UpperCamelCase : List[Any] = [] UpperCamelCase : Optional[Any] = values.shape[0] == 1 for i, (_values, _predictions) in enumerate(zip(values.tolist() , predictions.tolist() ) ): UpperCamelCase : Dict = [] for v, p in zip(_values , _predictions ): # Copy is important since we're going to modify this array in place UpperCamelCase : Tuple = input_ids.numpy().copy() if target_ids is not None: UpperCamelCase : str = target_ids[p].tolist() UpperCamelCase : List[str] = p # Filter padding out: UpperCamelCase : Union[str, Any] = tokens[np.where(tokens != self.tokenizer.pad_token_id )] # Originally we skip special tokens to give readable output. # For multi masks though, the other [MASK] would be removed otherwise # making the output look odd, so we add them back UpperCamelCase : int = self.tokenizer.decode(A_ , skip_special_tokens=A_ ) UpperCamelCase : int = {"score": v, "token": p, "token_str": self.tokenizer.decode([p] ), "sequence": sequence} row.append(A_ ) result.append(A_ ) if single_mask: return result[0] return result def __UpperCamelCase( self , A_ , A_=None ): '''simple docstring''' if isinstance(A_ , A_ ): UpperCamelCase : Dict = [targets] try: UpperCamelCase : int = self.tokenizer.get_vocab() except Exception: UpperCamelCase : Tuple = {} UpperCamelCase : Union[str, Any] = [] for target in targets: UpperCamelCase : Optional[int] = vocab.get(A_ , A_ ) if id_ is None: UpperCamelCase : Any = self.tokenizer( A_ , add_special_tokens=A_ , return_attention_mask=A_ , return_token_type_ids=A_ , max_length=1 , truncation=A_ , )["input_ids"] if len(A_ ) == 0: logger.warning( F"""The specified target token `{target}` does not exist in the model vocabulary. """ "We cannot replace it with anything meaningful, ignoring it" ) continue UpperCamelCase : str = input_ids[0] # XXX: If users encounter this pass # it becomes pretty slow, so let's make sure # The warning enables them to fix the input to # get faster performance. logger.warning( F"""The specified target token `{target}` does not exist in the model vocabulary. """ F"""Replacing with `{self.tokenizer.convert_ids_to_tokens(id_ )}`.""" ) target_ids.append(id_ ) UpperCamelCase : Dict = list(set(A_ ) ) if len(A_ ) == 0: raise ValueError("At least one target must be provided when passed." ) UpperCamelCase : Optional[int] = np.array(A_ ) return target_ids def __UpperCamelCase( self , A_=None , A_=None ): '''simple docstring''' UpperCamelCase : Optional[int] = {} if targets is not None: UpperCamelCase : Optional[Any] = self.get_target_ids(A_ , A_ ) UpperCamelCase : int = target_ids if top_k is not None: UpperCamelCase : int = top_k if self.tokenizer.mask_token_id is None: raise PipelineException( "fill-mask" , self.model.base_model_prefix , "The tokenizer does not define a `mask_token`." ) return {}, {}, postprocess_params def __call__( self , A_ , *A_ , **A_ ): '''simple docstring''' UpperCamelCase : Tuple = super().__call__(A_ , **A_ ) if isinstance(A_ , A_ ) and len(A_ ) == 1: return outputs[0] return outputs
140
import inspect from typing import Callable, List, Optional, Union import torch from transformers import CLIPImageProcessor, CLIPTextModel, CLIPTokenizer from diffusers import DiffusionPipeline from diffusers.models import AutoencoderKL, UNetaDConditionModel from diffusers.pipelines.stable_diffusion import StableDiffusionPipelineOutput from diffusers.pipelines.stable_diffusion.safety_checker import StableDiffusionSafetyChecker from diffusers.schedulers import DDIMScheduler, LMSDiscreteScheduler, PNDMScheduler from diffusers.utils import logging __lowerCamelCase : List[str] = logging.get_logger(__name__) # pylint: disable=invalid-name class A__ ( __snake_case ): def __init__( self , A_ , A_ , A_ , A_ , A_ , A_ , A_ , ): '''simple docstring''' super().__init__() self.register_modules( vae=A_ , text_encoder=A_ , tokenizer=A_ , unet=A_ , scheduler=A_ , safety_checker=A_ , feature_extractor=A_ , ) def __UpperCamelCase( self , A_ = "auto" ): '''simple docstring''' if slice_size == "auto": # half the attention head size is usually a good trade-off between # speed and memory UpperCamelCase : Union[str, Any] = self.unet.config.attention_head_dim // 2 self.unet.set_attention_slice(A_ ) def __UpperCamelCase( self ): '''simple docstring''' self.enable_attention_slicing(A_ ) @torch.no_grad() def __call__( self , A_ , A_ = 512 , A_ = 512 , A_ = 50 , A_ = 7.5 , A_ = None , A_ = 1 , A_ = 0.0 , A_ = None , A_ = None , A_ = "pil" , A_ = True , A_ = None , A_ = 1 , A_ = None , **A_ , ): '''simple docstring''' if isinstance(A_ , A_ ): UpperCamelCase : Any = 1 elif isinstance(A_ , A_ ): UpperCamelCase : Optional[Any] = len(A_ ) else: raise ValueError(F"""`prompt` has to be of type `str` or `list` but is {type(A_ )}""" ) if height % 8 != 0 or width % 8 != 0: raise ValueError(F"""`height` and `width` have to be divisible by 8 but are {height} and {width}.""" ) if (callback_steps is None) or ( callback_steps is not None and (not isinstance(A_ , A_ ) or callback_steps <= 0) ): raise ValueError( F"""`callback_steps` has to be a positive integer but is {callback_steps} of type""" F""" {type(A_ )}.""" ) # get prompt text embeddings UpperCamelCase : int = self.tokenizer( A_ , padding="max_length" , max_length=self.tokenizer.model_max_length , return_tensors="pt" , ) UpperCamelCase : Optional[Any] = text_inputs.input_ids if text_input_ids.shape[-1] > self.tokenizer.model_max_length: UpperCamelCase : List[str] = 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}""" ) UpperCamelCase : Any = text_input_ids[:, : self.tokenizer.model_max_length] if text_embeddings is None: UpperCamelCase : str = self.text_encoder(text_input_ids.to(self.device ) )[0] # duplicate text embeddings for each generation per prompt, using mps friendly method UpperCamelCase , UpperCamelCase , UpperCamelCase : str = text_embeddings.shape UpperCamelCase : int = text_embeddings.repeat(1 , A_ , 1 ) UpperCamelCase : str = text_embeddings.view(bs_embed * num_images_per_prompt , A_ , -1 ) # here `guidance_scale` is defined analog to the guidance weight `w` of equation (2) # of the Imagen paper: https://arxiv.org/pdf/2205.11487.pdf . `guidance_scale = 1` # corresponds to doing no classifier free guidance. UpperCamelCase : Optional[int] = guidance_scale > 1.0 # get unconditional embeddings for classifier free guidance if do_classifier_free_guidance: UpperCamelCase : List[str] if negative_prompt is None: UpperCamelCase : Tuple = [""] elif type(A_ ) is not type(A_ ): raise TypeError( F"""`negative_prompt` should be the same type to `prompt`, but got {type(A_ )} !=""" F""" {type(A_ )}.""" ) elif isinstance(A_ , A_ ): UpperCamelCase : Optional[int] = [negative_prompt] elif batch_size != len(A_ ): raise ValueError( F"""`negative_prompt`: {negative_prompt} has batch size {len(A_ )}, but `prompt`:""" F""" {prompt} has batch size {batch_size}. Please make sure that passed `negative_prompt` matches""" " the batch size of `prompt`." ) else: UpperCamelCase : Union[str, Any] = negative_prompt UpperCamelCase : Tuple = text_input_ids.shape[-1] UpperCamelCase : str = self.tokenizer( A_ , padding="max_length" , max_length=A_ , truncation=A_ , return_tensors="pt" , ) UpperCamelCase : str = self.text_encoder(uncond_input.input_ids.to(self.device ) )[0] # duplicate unconditional embeddings for each generation per prompt, using mps friendly method UpperCamelCase : Optional[int] = uncond_embeddings.shape[1] UpperCamelCase : Optional[Any] = uncond_embeddings.repeat(A_ , A_ , 1 ) UpperCamelCase : Dict = uncond_embeddings.view(batch_size * num_images_per_prompt , A_ , -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 UpperCamelCase : Optional[int] = torch.cat([uncond_embeddings, text_embeddings] ) # get the initial random noise unless the user supplied it # Unlike in other pipelines, latents need to be generated in the target device # for 1-to-1 results reproducibility with the CompVis implementation. # However this currently doesn't work in `mps`. UpperCamelCase : Union[str, Any] = (batch_size * num_images_per_prompt, self.unet.config.in_channels, height // 8, width // 8) UpperCamelCase : Tuple = (batch_size * num_images_per_prompt, self.unet.config.in_channels, 64, 64) UpperCamelCase : Any = text_embeddings.dtype if latents is None: if self.device.type == "mps": # randn does not exist on mps UpperCamelCase : Optional[Any] = torch.randn( A_ , generator=A_ , device="cpu" , dtype=A_ ).to(self.device ) UpperCamelCase : Dict = torch.randn(A_ , generator=A_ , device="cpu" , dtype=A_ ).to( self.device ) else: UpperCamelCase : Tuple = torch.randn( A_ , generator=A_ , device=self.device , dtype=A_ ) UpperCamelCase : str = torch.randn(A_ , generator=A_ , device=self.device , dtype=A_ ) else: if latents_reference.shape != latents_shape: raise ValueError(F"""Unexpected latents shape, got {latents.shape}, expected {latents_shape}""" ) UpperCamelCase : List[Any] = latents_reference.to(self.device ) UpperCamelCase : Optional[Any] = latents.to(self.device ) # This is the key part of the pipeline where we # try to ensure that the generated images w/ the same seed # but different sizes actually result in similar images UpperCamelCase : List[str] = (latents_shape[3] - latents_shape_reference[3]) // 2 UpperCamelCase : str = (latents_shape[2] - latents_shape_reference[2]) // 2 UpperCamelCase : List[Any] = latents_shape_reference[3] if dx >= 0 else latents_shape_reference[3] + 2 * dx UpperCamelCase : Optional[Any] = latents_shape_reference[2] if dy >= 0 else latents_shape_reference[2] + 2 * dy UpperCamelCase : str = 0 if dx < 0 else dx UpperCamelCase : Union[str, Any] = 0 if dy < 0 else dy UpperCamelCase : Union[str, Any] = max(-dx , 0 ) UpperCamelCase : Tuple = max(-dy , 0 ) # import pdb # pdb.set_trace() UpperCamelCase : Optional[Any] = latents_reference[:, :, dy : dy + h, dx : dx + w] # set timesteps self.scheduler.set_timesteps(A_ ) # Some schedulers like PNDM have timesteps as arrays # It's more optimized to move all timesteps to correct device beforehand UpperCamelCase : Union[str, Any] = self.scheduler.timesteps.to(self.device ) # scale the initial noise by the standard deviation required by the scheduler UpperCamelCase : str = latents * self.scheduler.init_noise_sigma # prepare extra kwargs for the scheduler step, since not all schedulers have the same signature # eta (η) is only used with the DDIMScheduler, it will be ignored for other schedulers. # eta corresponds to η in DDIM paper: https://arxiv.org/abs/2010.02502 # and should be between [0, 1] UpperCamelCase : List[str] = "eta" in set(inspect.signature(self.scheduler.step ).parameters.keys() ) UpperCamelCase : str = {} if accepts_eta: UpperCamelCase : int = eta for i, t in enumerate(self.progress_bar(A_ ) ): # expand the latents if we are doing classifier free guidance UpperCamelCase : Any = torch.cat([latents] * 2 ) if do_classifier_free_guidance else latents UpperCamelCase : Tuple = self.scheduler.scale_model_input(A_ , A_ ) # predict the noise residual UpperCamelCase : List[str] = self.unet(A_ , A_ , encoder_hidden_states=A_ ).sample # perform guidance if do_classifier_free_guidance: UpperCamelCase , UpperCamelCase : Any = noise_pred.chunk(2 ) UpperCamelCase : List[str] = noise_pred_uncond + guidance_scale * (noise_pred_text - noise_pred_uncond) # compute the previous noisy sample x_t -> x_t-1 UpperCamelCase : Optional[Any] = self.scheduler.step(A_ , A_ , A_ , **A_ ).prev_sample # call the callback, if provided if callback is not None and i % callback_steps == 0: callback(A_ , A_ , A_ ) UpperCamelCase : int = 1 / 0.1_82_15 * latents UpperCamelCase : Tuple = self.vae.decode(A_ ).sample UpperCamelCase : Any = (image / 2 + 0.5).clamp(0 , 1 ) # we always cast to float32 as this does not cause significant overhead and is compatible with bfloat16 UpperCamelCase : Tuple = image.cpu().permute(0 , 2 , 3 , 1 ).float().numpy() if self.safety_checker is not None: UpperCamelCase : int = self.feature_extractor(self.numpy_to_pil(A_ ) , return_tensors="pt" ).to( self.device ) UpperCamelCase , UpperCamelCase : int = self.safety_checker( images=A_ , clip_input=safety_checker_input.pixel_values.to(text_embeddings.dtype ) ) else: UpperCamelCase : int = None if output_type == "pil": UpperCamelCase : Tuple = self.numpy_to_pil(A_ ) if not return_dict: return (image, has_nsfw_concept) return StableDiffusionPipelineOutput(images=A_ , nsfw_content_detected=A_ )
140
1
"""simple docstring""" import shutil import tempfile import unittest from unittest.mock import patch from transformers import ( DefaultFlowCallback, IntervalStrategy, PrinterCallback, ProgressCallback, Trainer, TrainerCallback, TrainingArguments, is_torch_available, ) from transformers.testing_utils import require_torch if is_torch_available(): from transformers.trainer import DEFAULT_CALLBACKS from .test_trainer import RegressionDataset, RegressionModelConfig, RegressionPreTrainedModel class __A ( __UpperCAmelCase ): def __init__( self ): _lowerCAmelCase : Optional[int] = [] def __A ( self , a__ , a__ , a__ , **a__ ): self.events.append("""on_init_end""" ) def __A ( self , a__ , a__ , a__ , **a__ ): self.events.append("""on_train_begin""" ) def __A ( self , a__ , a__ , a__ , **a__ ): self.events.append("""on_train_end""" ) def __A ( self , a__ , a__ , a__ , **a__ ): self.events.append("""on_epoch_begin""" ) def __A ( self , a__ , a__ , a__ , **a__ ): self.events.append("""on_epoch_end""" ) def __A ( self , a__ , a__ , a__ , **a__ ): self.events.append("""on_step_begin""" ) def __A ( self , a__ , a__ , a__ , **a__ ): self.events.append("""on_step_end""" ) def __A ( self , a__ , a__ , a__ , **a__ ): self.events.append("""on_evaluate""" ) def __A ( self , a__ , a__ , a__ , **a__ ): self.events.append("""on_predict""" ) def __A ( self , a__ , a__ , a__ , **a__ ): self.events.append("""on_save""" ) def __A ( self , a__ , a__ , a__ , **a__ ): self.events.append("""on_log""" ) def __A ( self , a__ , a__ , a__ , **a__ ): self.events.append("""on_prediction_step""" ) @require_torch class __A ( unittest.TestCase ): def __A ( self ): _lowerCAmelCase : Any = tempfile.mkdtemp() def __A ( self ): shutil.rmtree(self.output_dir ) def __A ( self , a__=0 , a__=0 , a__=64 , a__=64 , a__=None , a__=False , **a__ ): # disable_tqdm in TrainingArguments has a flaky default since it depends on the level of logging. We make sure # its set to False since the tests later on depend on its value. _lowerCAmelCase : Tuple = RegressionDataset(length=lowerCamelCase__ ) _lowerCAmelCase : Tuple = RegressionDataset(length=lowerCamelCase__ ) _lowerCAmelCase : str = RegressionModelConfig(a=lowerCamelCase__ , b=lowerCamelCase__ ) _lowerCAmelCase : Tuple = RegressionPreTrainedModel(lowerCamelCase__ ) _lowerCAmelCase : str = TrainingArguments(self.output_dir , disable_tqdm=lowerCamelCase__ , report_to=[] , **lowerCamelCase__ ) return Trainer( lowerCamelCase__ , lowerCamelCase__ , train_dataset=lowerCamelCase__ , eval_dataset=lowerCamelCase__ , callbacks=lowerCamelCase__ , ) def __A ( self , a__ , a__ ): self.assertEqual(len(lowerCamelCase__ ) , len(lowerCamelCase__ ) ) # Order doesn't matter _lowerCAmelCase : Optional[int] = sorted(lowerCamelCase__ , key=lambda a__ : cb.__name__ if isinstance(lowerCamelCase__ , lowerCamelCase__ ) else cb.__class__.__name__ ) _lowerCAmelCase : Optional[int] = sorted(lowerCamelCase__ , key=lambda a__ : cb.__name__ if isinstance(lowerCamelCase__ , lowerCamelCase__ ) else cb.__class__.__name__ ) for cba, cba in zip(lowerCamelCase__ , lowerCamelCase__ ): if isinstance(lowerCamelCase__ , lowerCamelCase__ ) and isinstance(lowerCamelCase__ , lowerCamelCase__ ): self.assertEqual(lowerCamelCase__ , lowerCamelCase__ ) elif isinstance(lowerCamelCase__ , lowerCamelCase__ ) and not isinstance(lowerCamelCase__ , lowerCamelCase__ ): self.assertEqual(lowerCamelCase__ , cba.__class__ ) elif not isinstance(lowerCamelCase__ , lowerCamelCase__ ) and isinstance(lowerCamelCase__ , lowerCamelCase__ ): self.assertEqual(cba.__class__ , lowerCamelCase__ ) else: self.assertEqual(lowerCamelCase__ , lowerCamelCase__ ) def __A ( self , a__ ): _lowerCAmelCase : List[Any] = ["""on_init_end""", """on_train_begin"""] _lowerCAmelCase : Union[str, Any] = 0 _lowerCAmelCase : Optional[int] = len(trainer.get_eval_dataloader() ) _lowerCAmelCase : int = ["""on_prediction_step"""] * len(trainer.get_eval_dataloader() ) + ["""on_log""", """on_evaluate"""] for _ in range(trainer.state.num_train_epochs ): expected_events.append("""on_epoch_begin""" ) for _ in range(lowerCamelCase__ ): step += 1 expected_events += ["on_step_begin", "on_step_end"] if step % trainer.args.logging_steps == 0: expected_events.append("""on_log""" ) if trainer.args.evaluation_strategy == IntervalStrategy.STEPS and step % trainer.args.eval_steps == 0: expected_events += evaluation_events.copy() if step % trainer.args.save_steps == 0: expected_events.append("""on_save""" ) expected_events.append("""on_epoch_end""" ) if trainer.args.evaluation_strategy == IntervalStrategy.EPOCH: expected_events += evaluation_events.copy() expected_events += ["on_log", "on_train_end"] return expected_events def __A ( self ): _lowerCAmelCase : Optional[Any] = self.get_trainer() _lowerCAmelCase : Union[str, Any] = DEFAULT_CALLBACKS.copy() + [ProgressCallback] self.check_callbacks_equality(trainer.callback_handler.callbacks , lowerCamelCase__ ) # Callbacks passed at init are added to the default callbacks _lowerCAmelCase : Dict = self.get_trainer(callbacks=[MyTestTrainerCallback] ) expected_callbacks.append(lowerCamelCase__ ) self.check_callbacks_equality(trainer.callback_handler.callbacks , lowerCamelCase__ ) # TrainingArguments.disable_tqdm controls if use ProgressCallback or PrinterCallback _lowerCAmelCase : str = self.get_trainer(disable_tqdm=lowerCamelCase__ ) _lowerCAmelCase : Optional[Any] = DEFAULT_CALLBACKS.copy() + [PrinterCallback] self.check_callbacks_equality(trainer.callback_handler.callbacks , lowerCamelCase__ ) def __A ( self ): _lowerCAmelCase : int = DEFAULT_CALLBACKS.copy() + [ProgressCallback] _lowerCAmelCase : str = self.get_trainer() # We can add, pop, or remove by class name trainer.remove_callback(lowerCamelCase__ ) expected_callbacks.remove(lowerCamelCase__ ) self.check_callbacks_equality(trainer.callback_handler.callbacks , lowerCamelCase__ ) _lowerCAmelCase : Tuple = self.get_trainer() _lowerCAmelCase : Tuple = trainer.pop_callback(lowerCamelCase__ ) self.assertEqual(cb.__class__ , lowerCamelCase__ ) self.check_callbacks_equality(trainer.callback_handler.callbacks , lowerCamelCase__ ) trainer.add_callback(lowerCamelCase__ ) expected_callbacks.insert(0 , lowerCamelCase__ ) self.check_callbacks_equality(trainer.callback_handler.callbacks , lowerCamelCase__ ) # We can also add, pop, or remove by instance _lowerCAmelCase : Tuple = self.get_trainer() _lowerCAmelCase : Optional[int] = trainer.callback_handler.callbacks[0] trainer.remove_callback(lowerCamelCase__ ) expected_callbacks.remove(lowerCamelCase__ ) self.check_callbacks_equality(trainer.callback_handler.callbacks , lowerCamelCase__ ) _lowerCAmelCase : Optional[Any] = self.get_trainer() _lowerCAmelCase : Any = trainer.callback_handler.callbacks[0] _lowerCAmelCase : Union[str, Any] = trainer.pop_callback(lowerCamelCase__ ) self.assertEqual(lowerCamelCase__ , lowerCamelCase__ ) self.check_callbacks_equality(trainer.callback_handler.callbacks , lowerCamelCase__ ) trainer.add_callback(lowerCamelCase__ ) expected_callbacks.insert(0 , lowerCamelCase__ ) self.check_callbacks_equality(trainer.callback_handler.callbacks , lowerCamelCase__ ) def __A ( self ): import warnings # XXX: for now ignore scatter_gather warnings in this test since it's not relevant to what's being tested warnings.simplefilter(action="""ignore""" , category=lowerCamelCase__ ) _lowerCAmelCase : Optional[int] = self.get_trainer(callbacks=[MyTestTrainerCallback] ) trainer.train() _lowerCAmelCase : List[str] = trainer.callback_handler.callbacks[-2].events self.assertEqual(lowerCamelCase__ , self.get_expected_events(lowerCamelCase__ ) ) # Independent log/save/eval _lowerCAmelCase : Dict = self.get_trainer(callbacks=[MyTestTrainerCallback] , logging_steps=5 ) trainer.train() _lowerCAmelCase : Dict = trainer.callback_handler.callbacks[-2].events self.assertEqual(lowerCamelCase__ , self.get_expected_events(lowerCamelCase__ ) ) _lowerCAmelCase : Union[str, Any] = self.get_trainer(callbacks=[MyTestTrainerCallback] , save_steps=5 ) trainer.train() _lowerCAmelCase : str = trainer.callback_handler.callbacks[-2].events self.assertEqual(lowerCamelCase__ , self.get_expected_events(lowerCamelCase__ ) ) _lowerCAmelCase : Tuple = self.get_trainer(callbacks=[MyTestTrainerCallback] , eval_steps=5 , evaluation_strategy="""steps""" ) trainer.train() _lowerCAmelCase : List[Any] = trainer.callback_handler.callbacks[-2].events self.assertEqual(lowerCamelCase__ , self.get_expected_events(lowerCamelCase__ ) ) _lowerCAmelCase : Optional[Any] = self.get_trainer(callbacks=[MyTestTrainerCallback] , evaluation_strategy="""epoch""" ) trainer.train() _lowerCAmelCase : Any = trainer.callback_handler.callbacks[-2].events self.assertEqual(lowerCamelCase__ , self.get_expected_events(lowerCamelCase__ ) ) # A bit of everything _lowerCAmelCase : Union[str, Any] = self.get_trainer( callbacks=[MyTestTrainerCallback] , logging_steps=3 , save_steps=10 , eval_steps=5 , evaluation_strategy="""steps""" , ) trainer.train() _lowerCAmelCase : Optional[int] = trainer.callback_handler.callbacks[-2].events self.assertEqual(lowerCamelCase__ , self.get_expected_events(lowerCamelCase__ ) ) # warning should be emitted for duplicated callbacks with patch("""transformers.trainer_callback.logger.warning""" ) as warn_mock: _lowerCAmelCase : int = self.get_trainer( callbacks=[MyTestTrainerCallback, MyTestTrainerCallback] , ) assert str(lowerCamelCase__ ) in warn_mock.call_args[0][0]
44
"""simple docstring""" import warnings from typing import Dict, List, Optional, Tuple from ...tokenization_utils import AddedToken, PreTrainedTokenizer from ...utils import logging lowerCAmelCase__ : Optional[int] = logging.get_logger(__name__) class snake_case ( __UpperCAmelCase ): """simple docstring""" snake_case__ = ["input_ids", "attention_mask"] def __init__( self : Optional[int] ,lowerCamelCase__ : int="</s>" ,lowerCamelCase__ : str="<unk>" ,lowerCamelCase__ : Union[str, Any]="<pad>" ,lowerCamelCase__ : int=125 ,lowerCamelCase__ : str=None ,**lowerCamelCase__ : Union[str, Any] ,): # Add extra_ids to the special token list if extra_ids > 0 and additional_special_tokens is None: UpperCAmelCase__ = [f'''<extra_id_{i}>''' for i in range(lowerCamelCase__ )] elif extra_ids > 0 and additional_special_tokens is not None: # Check that we have the right number of extra_id special tokens UpperCAmelCase__ = len(set(filter(lambda lowerCamelCase__ : bool('extra_id' in str(lowerCamelCase__ ) ) ,lowerCamelCase__ ) ) ) if extra_tokens != extra_ids: raise ValueError( f'''Both extra_ids ({extra_ids}) and additional_special_tokens ({additional_special_tokens}) are''' ' provided to ByT5Tokenizer. In this case the additional_special_tokens must include the' ' extra_ids tokens' ) UpperCAmelCase__ = AddedToken(lowerCamelCase__ ,lstrip=lowerCamelCase__ ,rstrip=lowerCamelCase__ ) if isinstance(lowerCamelCase__ ,lowerCamelCase__ ) else pad_token UpperCAmelCase__ = AddedToken(lowerCamelCase__ ,lstrip=lowerCamelCase__ ,rstrip=lowerCamelCase__ ) if isinstance(lowerCamelCase__ ,lowerCamelCase__ ) else eos_token UpperCAmelCase__ = AddedToken(lowerCamelCase__ ,lstrip=lowerCamelCase__ ,rstrip=lowerCamelCase__ ) if isinstance(lowerCamelCase__ ,lowerCamelCase__ ) else unk_token super().__init__( eos_token=lowerCamelCase__ ,unk_token=lowerCamelCase__ ,pad_token=lowerCamelCase__ ,extra_ids=lowerCamelCase__ ,additional_special_tokens=lowerCamelCase__ ,**lowerCamelCase__ ,) UpperCAmelCase__ = extra_ids UpperCAmelCase__ = 2**8 # utf is 8 bits # define special tokens dict UpperCAmelCase__ = { self.pad_token: 0, self.eos_token: 1, self.unk_token: 2, } UpperCAmelCase__ = len(self.special_tokens_encoder ) UpperCAmelCase__ = len(lowerCamelCase__ ) for i, token in enumerate(lowerCamelCase__ ): UpperCAmelCase__ = self.vocab_size + i - n UpperCAmelCase__ = {v: k for k, v in self.special_tokens_encoder.items()} @property def __lowerCAmelCase ( self : Union[str, Any] ): return self._utf_vocab_size + self._num_special_tokens + self._extra_ids def __lowerCAmelCase ( self : Optional[Any] ,lowerCamelCase__ : List[int] ,lowerCamelCase__ : Optional[List[int]] = None ,lowerCamelCase__ : bool = False ): if already_has_special_tokens: return super().get_special_tokens_mask( token_ids_a=lowerCamelCase__ ,token_ids_a=lowerCamelCase__ ,already_has_special_tokens=lowerCamelCase__ ) # normal case: some special tokens if token_ids_a is None: return ([0] * len(lowerCamelCase__ )) + [1] return ([0] * len(lowerCamelCase__ )) + [1] + ([0] * len(lowerCamelCase__ )) + [1] def __lowerCAmelCase ( self : str ,lowerCamelCase__ : List[int] ): if len(lowerCamelCase__ ) > 0 and token_ids[-1] == self.eos_token_id: warnings.warn( f'''This sequence already has {self.eos_token}. In future versions this behavior may lead to duplicated''' ' eos tokens being added.' ) return token_ids else: return token_ids + [self.eos_token_id] def __lowerCAmelCase ( self : str ,lowerCamelCase__ : List[int] ,lowerCamelCase__ : Optional[List[int]] = None ): UpperCAmelCase__ = [self.eos_token_id] if token_ids_a is None: return len(token_ids_a + eos ) * [0] return len(token_ids_a + eos + token_ids_a + eos ) * [0] def __lowerCAmelCase ( self : Dict ,lowerCamelCase__ : List[int] ,lowerCamelCase__ : Optional[List[int]] = None ): UpperCAmelCase__ = self._add_eos_if_not_present(lowerCamelCase__ ) if token_ids_a is None: return token_ids_a else: UpperCAmelCase__ = self._add_eos_if_not_present(lowerCamelCase__ ) return token_ids_a + token_ids_a def __lowerCAmelCase ( self : Optional[Any] ,lowerCamelCase__ : str ): UpperCAmelCase__ = [chr(lowerCamelCase__ ) for i in text.encode('utf-8' )] return tokens def __lowerCAmelCase ( self : List[Any] ,lowerCamelCase__ : str ): if token in self.special_tokens_encoder: UpperCAmelCase__ = self.special_tokens_encoder[token] elif token in self.added_tokens_encoder: UpperCAmelCase__ = self.added_tokens_encoder[token] elif len(lowerCamelCase__ ) != 1: UpperCAmelCase__ = self.unk_token_id else: UpperCAmelCase__ = ord(lowerCamelCase__ ) + self._num_special_tokens return token_id def __lowerCAmelCase ( self : Union[str, Any] ,lowerCamelCase__ : Any ): if index in self.special_tokens_decoder: UpperCAmelCase__ = self.special_tokens_decoder[index] else: UpperCAmelCase__ = chr(index - self._num_special_tokens ) return token def __lowerCAmelCase ( self : Optional[Any] ,lowerCamelCase__ : Union[str, Any] ): UpperCAmelCase__ = b'' for token in tokens: if token in self.special_tokens_decoder: UpperCAmelCase__ = self.special_tokens_decoder[token].encode('utf-8' ) elif token in self.added_tokens_decoder: UpperCAmelCase__ = self.special_tokens_decoder[token].encode('utf-8' ) elif token in self.special_tokens_encoder: UpperCAmelCase__ = token.encode('utf-8' ) elif token in self.added_tokens_encoder: UpperCAmelCase__ = token.encode('utf-8' ) else: UpperCAmelCase__ = bytes([ord(lowerCamelCase__ )] ) bstring += tok_string UpperCAmelCase__ = bstring.decode('utf-8' ,errors='ignore' ) return string def __lowerCAmelCase ( self : str ,lowerCamelCase__ : str ,lowerCamelCase__ : Optional[str] = None ): return ()
98
0
'''simple docstring''' from ..models.whisper import WhisperForConditionalGeneration, WhisperProcessor from .base import PipelineTool class A__ ( lowerCamelCase_ ): """simple docstring""" UpperCamelCase_ : List[Any] = """openai/whisper-base""" UpperCamelCase_ : int = ( """This is a tool that transcribes an audio into text. It takes an input named `audio` and returns the """ """transcribed text.""" ) UpperCamelCase_ : List[Any] = """transcriber""" UpperCamelCase_ : Optional[int] = WhisperProcessor UpperCamelCase_ : Optional[int] = WhisperForConditionalGeneration UpperCamelCase_ : Any = ["""audio"""] UpperCamelCase_ : Any = ["""text"""] def _lowerCAmelCase ( self : Optional[Any] , lowerCAmelCase__ : Dict ) -> List[Any]: """simple docstring""" return self.pre_processor(lowerCAmelCase__ , return_tensors="pt" ).input_features def _lowerCAmelCase ( self : Tuple , lowerCAmelCase__ : Any ) -> Tuple: """simple docstring""" return self.model.generate(inputs=lowerCAmelCase__ ) def _lowerCAmelCase ( self : Tuple , lowerCAmelCase__ : str ) -> List[str]: """simple docstring""" return self.pre_processor.batch_decode(lowerCAmelCase__ , skip_special_tokens=lowerCAmelCase__ )[0]
365
'''simple docstring''' import unittest from transformers import BarthezTokenizer, BarthezTokenizerFast, BatchEncoding from transformers.testing_utils import require_sentencepiece, require_tokenizers, require_torch, slow from ...test_tokenization_common import TokenizerTesterMixin @require_tokenizers @require_sentencepiece @slow # see https://github.com/huggingface/transformers/issues/11457 class A__ ( UpperCamelCase , unittest.TestCase ): """simple docstring""" UpperCamelCase_ : str = BarthezTokenizer UpperCamelCase_ : List[Any] = BarthezTokenizerFast UpperCamelCase_ : Optional[int] = True UpperCamelCase_ : Optional[int] = True def _lowerCAmelCase ( self : Optional[int] ) -> Dict: """simple docstring""" super().setUp() _UpperCAmelCase : Tuple = BarthezTokenizerFast.from_pretrained("moussaKam/mbarthez" ) tokenizer.save_pretrained(self.tmpdirname ) tokenizer.save_pretrained(self.tmpdirname , legacy_format=lowerCAmelCase__ ) _UpperCAmelCase : List[str] = tokenizer def _lowerCAmelCase ( self : List[str] ) -> Optional[int]: """simple docstring""" _UpperCAmelCase : Tuple = "<pad>" _UpperCAmelCase : Dict = 1 self.assertEqual(self.get_tokenizer()._convert_token_to_id(lowerCAmelCase__ ) , lowerCAmelCase__ ) self.assertEqual(self.get_tokenizer()._convert_id_to_token(lowerCAmelCase__ ) , lowerCAmelCase__ ) def _lowerCAmelCase ( self : Tuple ) -> Optional[int]: """simple docstring""" _UpperCAmelCase : List[str] = list(self.get_tokenizer().get_vocab().keys() ) self.assertEqual(vocab_keys[0] , "<s>" ) self.assertEqual(vocab_keys[1] , "<pad>" ) self.assertEqual(vocab_keys[-1] , "<mask>" ) self.assertEqual(len(lowerCAmelCase__ ) , 1_0_1_1_2_2 ) def _lowerCAmelCase ( self : Union[str, Any] ) -> Dict: """simple docstring""" self.assertEqual(self.get_tokenizer().vocab_size , 1_0_1_1_2_2 ) @require_torch def _lowerCAmelCase ( self : Any ) -> int: """simple docstring""" _UpperCAmelCase : int = ["A long paragraph for summarization.", "Another paragraph for summarization."] _UpperCAmelCase : Optional[int] = [0, 5_7, 3_0_1_8, 7_0_3_0_7, 9_1, 2] _UpperCAmelCase : int = self.tokenizer( lowerCAmelCase__ , max_length=len(lowerCAmelCase__ ) , padding=lowerCAmelCase__ , truncation=lowerCAmelCase__ , return_tensors="pt" ) self.assertIsInstance(lowerCAmelCase__ , lowerCAmelCase__ ) self.assertEqual((2, 6) , batch.input_ids.shape ) self.assertEqual((2, 6) , batch.attention_mask.shape ) _UpperCAmelCase : str = batch.input_ids.tolist()[0] self.assertListEqual(lowerCAmelCase__ , lowerCAmelCase__ ) def _lowerCAmelCase ( self : str ) -> Optional[Any]: """simple docstring""" if not self.test_rust_tokenizer: return _UpperCAmelCase : Optional[int] = self.get_tokenizer() _UpperCAmelCase : Optional[int] = self.get_rust_tokenizer() _UpperCAmelCase : Tuple = "I was born in 92000, and this is falsé." _UpperCAmelCase : Dict = tokenizer.tokenize(lowerCAmelCase__ ) _UpperCAmelCase : Union[str, Any] = rust_tokenizer.tokenize(lowerCAmelCase__ ) self.assertListEqual(lowerCAmelCase__ , lowerCAmelCase__ ) _UpperCAmelCase : Dict = tokenizer.encode(lowerCAmelCase__ , add_special_tokens=lowerCAmelCase__ ) _UpperCAmelCase : Union[str, Any] = rust_tokenizer.encode(lowerCAmelCase__ , add_special_tokens=lowerCAmelCase__ ) self.assertListEqual(lowerCAmelCase__ , lowerCAmelCase__ ) _UpperCAmelCase : Optional[Any] = self.get_rust_tokenizer() _UpperCAmelCase : Optional[Any] = tokenizer.encode(lowerCAmelCase__ ) _UpperCAmelCase : Optional[int] = rust_tokenizer.encode(lowerCAmelCase__ ) self.assertListEqual(lowerCAmelCase__ , lowerCAmelCase__ ) @slow def _lowerCAmelCase ( self : int ) -> int: """simple docstring""" _UpperCAmelCase : Optional[Any] = {"input_ids": [[0, 4_9_0, 1_4_3_2_8, 4_5_0_7, 3_5_4, 4_7, 4_3_6_6_9, 9_5, 2_5, 7_8_1_1_7, 2_0_2_1_5, 1_9_7_7_9, 1_9_0, 2_2, 4_0_0, 4, 3_5_3_4_3, 8_0_3_1_0, 6_0_3, 8_6, 2_4_9_3_7, 1_0_5, 3_3_4_3_8, 9_4_7_6_2, 1_9_6, 3_9_6_4_2, 7, 1_5, 1_5_9_3_3, 1_7_3, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [0, 1_0_5_3_4, 8_7, 2_5, 6_6, 3_3_5_8, 1_9_6, 5_5_2_8_9, 8, 8_2_9_6_1, 8_1, 2_2_0_4, 7_5_2_0_3, 7, 1_5, 7_6_3, 1_2_9_5_6, 2_1_6, 1_7_8, 1_4_3_2_8, 9_5_9_5, 1_3_7_7, 6_9_6_9_3, 7, 4_4_8, 7_1_0_2_1, 1_9_6, 1_8_1_0_6, 1_4_3_7, 1_3_9_7_4, 1_0_8, 9_0_8_3, 4, 4_9_3_1_5, 7, 3_9, 8_6, 1_3_2_6, 2_7_9_3, 4_6_3_3_3, 4, 4_4_8, 1_9_6, 7_4_5_8_8, 7, 4_9_3_1_5, 7, 3_9, 2_1, 8_2_2, 3_8_4_7_0, 7_4, 2_1, 6_6_7_2_3, 6_2_4_8_0, 8, 2_2_0_5_0, 5, 2]], "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, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 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 # moussaKam/mbarthez is a french model. So we also use french texts. _UpperCAmelCase : Tuple = [ "Le transformeur est un modèle d'apprentissage profond introduit en 2017, " "utilisé principalement dans le domaine du traitement automatique des langues (TAL).", "À l'instar des réseaux de neurones récurrents (RNN), les transformeurs sont conçus " "pour gérer des données séquentielles, telles que le langage naturel, pour des tâches " "telles que la traduction et la synthèse de texte.", ] self.tokenizer_integration_test_util( expected_encoding=lowerCAmelCase__ , model_name="moussaKam/mbarthez" , revision="c2e4ecbca5e3cd2c37fe1ac285ca4fbdf1366fb6" , sequences=lowerCAmelCase__ , )
17
0
'''simple docstring''' import argparse import os import gluonnlp as nlp import mxnet as mx import numpy as np import torch from gluonnlp.base import get_home_dir from gluonnlp.model.bert import BERTEncoder from gluonnlp.model.utils import _load_vocab from gluonnlp.vocab import Vocab from packaging import version from torch import nn from transformers import BertConfig, BertForMaskedLM, BertModel, RobertaTokenizer from transformers.models.bert.modeling_bert import ( BertIntermediate, BertLayer, BertOutput, BertSelfAttention, BertSelfOutput, ) from transformers.utils import logging if version.parse(nlp.__version__) != version.parse("0.8.3"): raise Exception("requires gluonnlp == 0.8.3") if version.parse(mx.__version__) != version.parse("1.5.0"): raise Exception("requires mxnet == 1.5.0") logging.set_verbosity_info() __UpperCAmelCase =logging.get_logger(__name__) __UpperCAmelCase ="The Nymphenburg Palace is a beautiful palace in Munich!" def __lowerCAmelCase ( UpperCamelCase__ , UpperCamelCase__ ) -> Optional[Any]: __lowerCamelCase = { '''attention_cell''': '''multi_head''', '''num_layers''': 4, '''units''': 10_24, '''hidden_size''': 7_68, '''max_length''': 5_12, '''num_heads''': 8, '''scaled''': True, '''dropout''': 0.1, '''use_residual''': True, '''embed_size''': 10_24, '''embed_dropout''': 0.1, '''word_embed''': None, '''layer_norm_eps''': 1E-5, '''token_type_vocab_size''': 2, } __lowerCamelCase = bort_4_8_768_1024_hparams # Let's construct the original Bort model here # Taken from official BERT implementation, see: # https://github.com/alexa/bort/blob/master/bort/bort.py __lowerCamelCase = BERTEncoder( attention_cell=predefined_args['''attention_cell'''] , num_layers=predefined_args['''num_layers'''] , units=predefined_args['''units'''] , hidden_size=predefined_args['''hidden_size'''] , max_length=predefined_args['''max_length'''] , num_heads=predefined_args['''num_heads'''] , scaled=predefined_args['''scaled'''] , dropout=predefined_args['''dropout'''] , output_attention=UpperCamelCase__ , output_all_encodings=UpperCamelCase__ , use_residual=predefined_args['''use_residual'''] , activation=predefined_args.get('''activation''' , '''gelu''' ) , layer_norm_eps=predefined_args.get('''layer_norm_eps''' , UpperCamelCase__ ) , ) # Vocab information needs to be fetched first # It's the same as RoBERTa, so RobertaTokenizer can be used later __lowerCamelCase = '''openwebtext_ccnews_stories_books_cased''' # Specify download folder to Gluonnlp's vocab __lowerCamelCase = os.path.join(get_home_dir() , '''models''' ) __lowerCamelCase = _load_vocab(UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , cls=UpperCamelCase__ ) __lowerCamelCase = nlp.model.BERTModel( UpperCamelCase__ , len(UpperCamelCase__ ) , units=predefined_args['''units'''] , embed_size=predefined_args['''embed_size'''] , embed_dropout=predefined_args['''embed_dropout'''] , word_embed=predefined_args['''word_embed'''] , use_pooler=UpperCamelCase__ , use_token_type_embed=UpperCamelCase__ , token_type_vocab_size=predefined_args['''token_type_vocab_size'''] , use_classifier=UpperCamelCase__ , use_decoder=UpperCamelCase__ , ) original_bort.load_parameters(UpperCamelCase__ , cast_dtype=UpperCamelCase__ , ignore_extra=UpperCamelCase__ ) __lowerCamelCase = original_bort._collect_params_with_prefix() # Build our config 🤗 __lowerCamelCase = { '''architectures''': ['''BertForMaskedLM'''], '''attention_probs_dropout_prob''': predefined_args['''dropout'''], '''hidden_act''': '''gelu''', '''hidden_dropout_prob''': predefined_args['''dropout'''], '''hidden_size''': predefined_args['''embed_size'''], '''initializer_range''': 0.0_2, '''intermediate_size''': predefined_args['''hidden_size'''], '''layer_norm_eps''': predefined_args['''layer_norm_eps'''], '''max_position_embeddings''': predefined_args['''max_length'''], '''model_type''': '''bort''', '''num_attention_heads''': predefined_args['''num_heads'''], '''num_hidden_layers''': predefined_args['''num_layers'''], '''pad_token_id''': 1, # 2 = BERT, 1 = RoBERTa '''type_vocab_size''': 1, # 2 = BERT, 1 = RoBERTa '''vocab_size''': len(UpperCamelCase__ ), } __lowerCamelCase = BertConfig.from_dict(UpperCamelCase__ ) __lowerCamelCase = BertForMaskedLM(UpperCamelCase__ ) hf_bort_model.eval() # Parameter mapping table (Gluonnlp to Transformers) # * denotes layer index # # | Gluon Parameter | Transformers Parameter # | -------------------------------------------------------------- | ---------------------- # | `encoder.layer_norm.beta` | `bert.embeddings.LayerNorm.bias` # | `encoder.layer_norm.gamma` | `bert.embeddings.LayerNorm.weight` # | `encoder.position_weight` | `bert.embeddings.position_embeddings.weight` # | `word_embed.0.weight` | `bert.embeddings.word_embeddings.weight` # | `encoder.transformer_cells.*.attention_cell.proj_key.bias` | `bert.encoder.layer.*.attention.self.key.bias` # | `encoder.transformer_cells.*.attention_cell.proj_key.weight` | `bert.encoder.layer.*.attention.self.key.weight` # | `encoder.transformer_cells.*.attention_cell.proj_query.bias` | `bert.encoder.layer.*.attention.self.query.bias` # | `encoder.transformer_cells.*.attention_cell.proj_query.weight` | `bert.encoder.layer.*.attention.self.query.weight` # | `encoder.transformer_cells.*.attention_cell.proj_value.bias` | `bert.encoder.layer.*.attention.self.value.bias` # | `encoder.transformer_cells.*.attention_cell.proj_value.weight` | `bert.encoder.layer.*.attention.self.value.weight` # | `encoder.transformer_cells.*.ffn.ffn_2.bias` | `bert.encoder.layer.*.attention.output.dense.bias` # | `encoder.transformer_cells.*.ffn.ffn_2.weight` | `bert.encoder.layer.*.attention.output.dense.weight` # | `encoder.transformer_cells.*.layer_norm.beta` | `bert.encoder.layer.*.attention.output.LayerNorm.bias` # | `encoder.transformer_cells.*.layer_norm.gamma` | `bert.encoder.layer.*.attention.output.LayerNorm.weight` # | `encoder.transformer_cells.*.ffn.ffn_1.bias` | `bert.encoder.layer.*.intermediate.dense.bias` # | `encoder.transformer_cells.*.ffn.ffn_1.weight` | `bert.encoder.layer.*.intermediate.dense.weight` # | `encoder.transformer_cells.*.ffn.layer_norm.beta` | `bert.encoder.layer.*.output.LayerNorm.bias` # | `encoder.transformer_cells.*.ffn.layer_norm.gamma` | `bert.encoder.layer.*.output.LayerNorm.weight` # | `encoder.transformer_cells.*.proj.bias` | `bert.encoder.layer.*.output.dense.bias` # | `encoder.transformer_cells.*.proj.weight` | `bert.encoder.layer.*.output.dense.weight` # Helper function to convert MXNET Arrays to PyTorch def to_torch(UpperCamelCase__ ) -> nn.Parameter: return nn.Parameter(torch.FloatTensor(mx_array.data().asnumpy() ) ) # Check param shapes and map new HF param back def check_and_map_params(UpperCamelCase__ , UpperCamelCase__ ): __lowerCamelCase = hf_param.shape __lowerCamelCase = to_torch(params[gluon_param] ) __lowerCamelCase = gluon_param.shape assert ( shape_hf == shape_gluon ), f"""The gluon parameter {gluon_param} has shape {shape_gluon}, but expects shape {shape_hf} for Transformers""" return gluon_param __lowerCamelCase = check_and_map_params( hf_bort_model.bert.embeddings.word_embeddings.weight , '''word_embed.0.weight''' ) __lowerCamelCase = check_and_map_params( hf_bort_model.bert.embeddings.position_embeddings.weight , '''encoder.position_weight''' ) __lowerCamelCase = check_and_map_params( hf_bort_model.bert.embeddings.LayerNorm.bias , '''encoder.layer_norm.beta''' ) __lowerCamelCase = check_and_map_params( hf_bort_model.bert.embeddings.LayerNorm.weight , '''encoder.layer_norm.gamma''' ) # Inspired by RoBERTa conversion script, we just zero them out (Bort does not use them) __lowerCamelCase = torch.zeros_like( hf_bort_model.bert.embeddings.token_type_embeddings.weight.data ) for i in range(hf_bort_config.num_hidden_layers ): __lowerCamelCase = hf_bort_model.bert.encoder.layer[i] # self attention __lowerCamelCase = layer.attention.self __lowerCamelCase = check_and_map_params( self_attn.key.bias.data , f"""encoder.transformer_cells.{i}.attention_cell.proj_key.bias""" ) __lowerCamelCase = check_and_map_params( self_attn.key.weight.data , f"""encoder.transformer_cells.{i}.attention_cell.proj_key.weight""" ) __lowerCamelCase = check_and_map_params( self_attn.query.bias.data , f"""encoder.transformer_cells.{i}.attention_cell.proj_query.bias""" ) __lowerCamelCase = check_and_map_params( self_attn.query.weight.data , f"""encoder.transformer_cells.{i}.attention_cell.proj_query.weight""" ) __lowerCamelCase = check_and_map_params( self_attn.value.bias.data , f"""encoder.transformer_cells.{i}.attention_cell.proj_value.bias""" ) __lowerCamelCase = check_and_map_params( self_attn.value.weight.data , f"""encoder.transformer_cells.{i}.attention_cell.proj_value.weight""" ) # self attention output __lowerCamelCase = layer.attention.output __lowerCamelCase = check_and_map_params( self_output.dense.bias , f"""encoder.transformer_cells.{i}.proj.bias""" ) __lowerCamelCase = check_and_map_params( self_output.dense.weight , f"""encoder.transformer_cells.{i}.proj.weight""" ) __lowerCamelCase = check_and_map_params( self_output.LayerNorm.bias , f"""encoder.transformer_cells.{i}.layer_norm.beta""" ) __lowerCamelCase = check_and_map_params( self_output.LayerNorm.weight , f"""encoder.transformer_cells.{i}.layer_norm.gamma""" ) # intermediate __lowerCamelCase = layer.intermediate __lowerCamelCase = check_and_map_params( intermediate.dense.bias , f"""encoder.transformer_cells.{i}.ffn.ffn_1.bias""" ) __lowerCamelCase = check_and_map_params( intermediate.dense.weight , f"""encoder.transformer_cells.{i}.ffn.ffn_1.weight""" ) # output __lowerCamelCase = layer.output __lowerCamelCase = check_and_map_params( bert_output.dense.bias , f"""encoder.transformer_cells.{i}.ffn.ffn_2.bias""" ) __lowerCamelCase = check_and_map_params( bert_output.dense.weight , f"""encoder.transformer_cells.{i}.ffn.ffn_2.weight""" ) __lowerCamelCase = check_and_map_params( bert_output.LayerNorm.bias , f"""encoder.transformer_cells.{i}.ffn.layer_norm.beta""" ) __lowerCamelCase = check_and_map_params( bert_output.LayerNorm.weight , f"""encoder.transformer_cells.{i}.ffn.layer_norm.gamma""" ) # Save space and energy 🎄 hf_bort_model.half() # Compare output of both models __lowerCamelCase = RobertaTokenizer.from_pretrained('''roberta-base''' ) __lowerCamelCase = tokenizer.encode_plus(UpperCamelCase__ )['''input_ids'''] # Get gluon output __lowerCamelCase = mx.nd.array([input_ids] ) __lowerCamelCase = original_bort(inputs=UpperCamelCase__ , token_types=[] ) # Get Transformer output (save and reload model again) hf_bort_model.save_pretrained(UpperCamelCase__ ) __lowerCamelCase = BertModel.from_pretrained(UpperCamelCase__ ) hf_bort_model.eval() __lowerCamelCase = tokenizer.encode_plus(UpperCamelCase__ , return_tensors='''pt''' ) __lowerCamelCase = hf_bort_model(**UpperCamelCase__ )[0] __lowerCamelCase = output_gluon[0].asnumpy() __lowerCamelCase = output_hf[0].detach().numpy() __lowerCamelCase = np.max(np.abs(hf_layer - gluon_layer ) ).item() __lowerCamelCase = np.allclose(UpperCamelCase__ , UpperCamelCase__ , atol=1E-3 ) if success: print('''✔️ Both model do output the same tensors''' ) else: print('''❌ Both model do **NOT** output the same tensors''' ) print('''Absolute difference is:''' , UpperCamelCase__ ) if __name__ == "__main__": __UpperCAmelCase =argparse.ArgumentParser() # Required parameters parser.add_argument( "--bort_checkpoint_path", default=None, type=str, required=True, help="Path the official Bort params file." ) parser.add_argument( "--pytorch_dump_folder_path", default=None, type=str, required=True, help="Path to the output PyTorch model." ) __UpperCAmelCase =parser.parse_args() convert_bort_checkpoint_to_pytorch(args.bort_checkpoint_path, args.pytorch_dump_folder_path)
67
'''simple docstring''' import doctest import logging import os import unittest from pathlib import Path from typing import List, Union import transformers from transformers.testing_utils import require_tf, require_torch, slow __lowerCAmelCase = logging.getLogger() @unittest.skip('''Temporarily disable the doc tests.''' ) @require_torch @require_tf @slow class UpperCAmelCase__ ( unittest.TestCase ): """simple docstring""" def __lowercase ( self : str ,_a : Path ,_a : Union[str, None] = None ,_a : Union[List[str], None] = None ,_a : Union[str, List[str], None] = None ,_a : bool = True ,): '''simple docstring''' _a : Optional[int] = [file for file in os.listdir(_a ) if os.path.isfile(os.path.join(_a ,_a ) )] if identifier is not None: _a : List[str] = [file for file in files if identifier in file] if n_identifier is not None: if isinstance(_a ,_a ): for n_ in n_identifier: _a : Tuple = [file for file in files if n_ not in file] else: _a : Optional[Any] = [file for file in files if n_identifier not in file] _a : List[str] = ignore_files or [] ignore_files.append('__init__.py' ) _a : Tuple = [file for file in files if file not in ignore_files] for file in files: # Open all files print('Testing' ,_a ) if only_modules: _a : Any = file.split('.' )[0] try: _a : List[str] = getattr(_a ,_a ) _a : int = doctest.DocTestSuite(_a ) _a : Any = unittest.TextTestRunner().run(_a ) self.assertIs(len(result.failures ) ,0 ) except AttributeError: logger.info(F"""{module_identifier} is not a module.""" ) else: _a : Union[str, Any] = doctest.testfile(str('..' / directory / file ) ,optionflags=doctest.ELLIPSIS ) self.assertIs(result.failed ,0 ) def __lowercase ( self : Any ): '''simple docstring''' _a : int = Path('src/transformers' ) _a : List[Any] = 'modeling' _a : Optional[Any] = [ 'modeling_ctrl.py', 'modeling_tf_ctrl.py', ] self.analyze_directory(_a ,identifier=_a ,ignore_files=_a ) def __lowercase ( self : Optional[int] ): '''simple docstring''' _a : Optional[Any] = Path('src/transformers' ) _a : Optional[Any] = 'tokenization' self.analyze_directory(_a ,identifier=_a ) def __lowercase ( self : List[str] ): '''simple docstring''' _a : Dict = Path('src/transformers' ) _a : str = 'configuration' self.analyze_directory(_a ,identifier=_a ) def __lowercase ( self : Any ): '''simple docstring''' _a : Tuple = Path('src/transformers' ) _a : List[Any] = ['configuration', 'modeling', 'tokenization'] self.analyze_directory(_a ,n_identifier=_a ) def __lowercase ( self : Tuple ): '''simple docstring''' _a : List[Any] = Path('docs/source' ) _a : List[str] = ['favicon.ico'] self.analyze_directory(_a ,ignore_files=_a ,only_modules=_a )
271
0
'''simple docstring''' import json import os import tempfile import transformers import datasets from utils import generate_example_dataset, get_duration UpperCamelCase__ : Optional[Any] = 50_0000 UpperCamelCase__ ,UpperCamelCase__ : List[Any] = os.path.split(__file__) UpperCamelCase__ : List[str] = os.path.join(RESULTS_BASEPATH, 'results', RESULTS_FILENAME.replace('.py', '.json')) @get_duration def __magic_name__ ( __UpperCAmelCase, **__UpperCAmelCase ) -> int: '''simple docstring''' snake_case_ = dataset.map(**_lowerCamelCase ) @get_duration def __magic_name__ ( __UpperCAmelCase, **__UpperCAmelCase ) -> Tuple: '''simple docstring''' snake_case_ = dataset.filter(**_lowerCamelCase ) def __magic_name__ ( ) -> Optional[Any]: '''simple docstring''' snake_case_ = {"num examples": SPEED_TEST_N_EXAMPLES} with tempfile.TemporaryDirectory() as tmp_dir: snake_case_ = datasets.Features({'''text''': datasets.Value('''string''' ), '''numbers''': datasets.Value('''float32''' )} ) snake_case_ = generate_example_dataset( os.path.join(_lowerCamelCase, '''dataset.arrow''' ), _lowerCamelCase, num_examples=_lowerCamelCase ) snake_case_ = transformers.AutoTokenizer.from_pretrained('''bert-base-cased''', use_fast=_lowerCamelCase ) def tokenize(__UpperCAmelCase ): return tokenizer(examples['''text'''] ) snake_case_ = map(_lowerCamelCase ) snake_case_ = map(_lowerCamelCase, batched=_lowerCamelCase ) snake_case_ = map(_lowerCamelCase, function=lambda __UpperCAmelCase : None, batched=_lowerCamelCase ) with dataset.formatted_as(type='''numpy''' ): snake_case_ = map(_lowerCamelCase, function=lambda __UpperCAmelCase : None, batched=_lowerCamelCase ) with dataset.formatted_as(type='''pandas''' ): snake_case_ = map(_lowerCamelCase, function=lambda __UpperCAmelCase : None, batched=_lowerCamelCase ) with dataset.formatted_as(type='''torch''', columns='''numbers''' ): snake_case_ = map(_lowerCamelCase, function=lambda __UpperCAmelCase : None, batched=_lowerCamelCase ) with dataset.formatted_as(type='''tensorflow''', columns='''numbers''' ): snake_case_ = map(_lowerCamelCase, function=lambda __UpperCAmelCase : None, batched=_lowerCamelCase ) snake_case_ = map(_lowerCamelCase, function=_lowerCamelCase, batched=_lowerCamelCase ) snake_case_ = filter(_lowerCamelCase ) # Activate later when tokenizer support batched inputs # with dataset.formatted_as(type='numpy'): # times[func.__name__ + " fast-tokenizer batched numpy"] = func(dataset, function=tokenize, batched=True) with open(_lowerCamelCase, '''wb''' ) as f: f.write(json.dumps(_lowerCamelCase ).encode('''utf-8''' ) ) if __name__ == "__main__": # useful to run the profiler benchmark_map_filter()
370
'''simple docstring''' import os from shutil import copyfile from typing import Any, Dict, List, Optional, Tuple import sentencepiece as spm from ...tokenization_utils import PreTrainedTokenizer from ...utils import logging a : Optional[int] = logging.get_logger(__name__) a : Optional[Any] = {'vocab_file': 'spiece.model'} a : Tuple = { 'vocab_file': { 'bert_for_seq_generation': ( 'https://huggingface.co/google/bert_for_seq_generation_L-24_bbc_encoder/resolve/main/spiece.model' ), } } a : Dict = {'bert_for_seq_generation': 512} class a ( _lowerCamelCase ): snake_case_ = VOCAB_FILES_NAMES snake_case_ = PRETRAINED_VOCAB_FILES_MAP snake_case_ = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES snake_case_ = [] snake_case_ = ["input_ids", "attention_mask"] def __init__( self : Any , lowercase_ : str , lowercase_ : Optional[Any]="<s>" , lowercase_ : Any="</s>" , lowercase_ : Optional[Any]="<unk>" , lowercase_ : List[Any]="<pad>" , lowercase_ : List[str]="<::::>" , lowercase_ : Optional[Dict[str, Any]] = None , **lowercase_ : Optional[int] , ): snake_case_ = {} if sp_model_kwargs is None else sp_model_kwargs # Add extra_ids to the special token list super().__init__( bos_token=lowercase_ , eos_token=lowercase_ , unk_token=lowercase_ , pad_token=lowercase_ , sep_token=lowercase_ , sp_model_kwargs=self.sp_model_kwargs , **lowercase_ , ) snake_case_ = vocab_file snake_case_ = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(lowercase_ ) @property def A_ ( self : int ): return self.sp_model.get_piece_size() def A_ ( self : Union[str, Any] ): snake_case_ = {self.convert_ids_to_tokens(lowercase_ ): i for i in range(self.vocab_size )} vocab.update(self.added_tokens_encoder ) return vocab def __getstate__( self : Optional[int] ): snake_case_ = self.__dict__.copy() snake_case_ = None return state def __setstate__( self : Any , lowercase_ : Optional[int] ): snake_case_ = d # for backward compatibility if not hasattr(self , '''sp_model_kwargs''' ): snake_case_ = {} snake_case_ = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(self.vocab_file ) def A_ ( self : Any , lowercase_ : str ): return self.sp_model.encode(lowercase_ , out_type=lowercase_ ) def A_ ( self : Optional[int] , lowercase_ : Union[str, Any] ): return self.sp_model.piece_to_id(lowercase_ ) def A_ ( self : Dict , lowercase_ : str ): snake_case_ = self.sp_model.IdToPiece(lowercase_ ) return token def A_ ( self : Optional[int] , lowercase_ : List[Any] ): snake_case_ = [] snake_case_ = '''''' for token in tokens: # make sure that special tokens are not decoded using sentencepiece model if token in self.all_special_tokens: out_string += self.sp_model.decode(lowercase_ ) + token snake_case_ = [] else: current_sub_tokens.append(lowercase_ ) out_string += self.sp_model.decode(lowercase_ ) return out_string.strip() def A_ ( self : List[str] , lowercase_ : str , lowercase_ : Optional[str] = None ): if not os.path.isdir(lowercase_ ): logger.error(F"Vocabulary path ({save_directory}) should be a directory" ) return snake_case_ = os.path.join( lowercase_ , (filename_prefix + '''-''' if filename_prefix else '''''') + VOCAB_FILES_NAMES['''vocab_file'''] ) if os.path.abspath(self.vocab_file ) != os.path.abspath(lowercase_ ) and os.path.isfile(self.vocab_file ): copyfile(self.vocab_file , lowercase_ ) elif not os.path.isfile(self.vocab_file ): with open(lowercase_ , '''wb''' ) as fi: snake_case_ = self.sp_model.serialized_model_proto() fi.write(lowercase_ ) return (out_vocab_file,)
72
0