code
stringlengths
81
54k
code_codestyle
int64
0
721
style_context
stringlengths
91
41.9k
style_context_codestyle
int64
0
699
label
int64
0
1
"""simple docstring""" import numpy as np import torch from torch.nn import CrossEntropyLoss from transformers import AutoModelForCausalLM, AutoTokenizer import datasets from datasets import logging lowerCamelCase__ : Tuple = "\\n\n" lowerCamelCase__ : Optional[Any] = "\nPerplexity (PPL) is one of the most common metrics for evaluating language models.\nIt is defined as the exponentiated average negative log-likelihood of a sequence.\n\nFor more information, see https://huggingface.co/docs/transformers/perplexity\n" lowerCamelCase__ : Dict = "\nArgs:\n model_id (str): model used for calculating Perplexity\n NOTE: Perplexity can only be calculated for causal language models.\n This includes models such as gpt2, causal variations of bert,\n causal versions of t5, and more (the full list can be found\n in the AutoModelForCausalLM documentation here:\n https://huggingface.co/docs/transformers/master/en/model_doc/auto#transformers.AutoModelForCausalLM )\n\n input_texts (list of str): input text, each separate text snippet\n is one list entry.\n batch_size (int): the batch size to run texts through the model. Defaults to 16.\n add_start_token (bool): whether to add the start token to the texts,\n so the perplexity can include the probability of the first word. Defaults to True.\n device (str): device to run on, defaults to 'cuda' when available\nReturns:\n perplexity: dictionary containing the perplexity scores for the texts\n in the input list, as well as the mean perplexity. If one of the input texts is\n longer than the max input length of the model, then it is truncated to the\n max length for the perplexity computation.\nExamples:\n Example 1:\n >>> perplexity = datasets.load_metric(\"perplexity\")\n >>> input_texts = [\"lorem ipsum\", \"Happy Birthday!\", \"Bienvenue\"]\n >>> results = perplexity.compute(model_id='gpt2',\n ... add_start_token=False,\n ... input_texts=input_texts) # doctest:+ELLIPSIS\n >>> print(list(results.keys()))\n ['perplexities', 'mean_perplexity']\n >>> print(round(results[\"mean_perplexity\"], 2))\n 78.22\n >>> print(round(results[\"perplexities\"][0], 2))\n 11.11\n\n Example 2:\n >>> perplexity = datasets.load_metric(\"perplexity\")\n >>> input_texts = datasets.load_dataset(\"wikitext\",\n ... \"wikitext-2-raw-v1\",\n ... split=\"test\")[\"text\"][:50] # doctest:+ELLIPSIS\n [...]\n >>> input_texts = [s for s in input_texts if s!='']\n >>> results = perplexity.compute(model_id='gpt2',\n ... input_texts=input_texts) # doctest:+ELLIPSIS\n >>> print(list(results.keys()))\n ['perplexities', 'mean_perplexity']\n >>> print(round(results[\"mean_perplexity\"], 2))\n 60.35\n >>> print(round(results[\"perplexities\"][0], 2))\n 81.12\n" @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class lowercase__( datasets.Metric ): '''simple docstring''' def __lowerCAmelCase ( self :int ) -> List[Any]: '''simple docstring''' return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { '''input_texts''': datasets.Value('''string''' ), } ) , reference_urls=['''https://huggingface.co/docs/transformers/perplexity'''] , ) def __lowerCAmelCase ( self :str , lowerCamelCase_ :Union[str, Any] , lowerCamelCase_ :List[Any] , lowerCamelCase_ :int = 16 , lowerCamelCase_ :bool = True , lowerCamelCase_ :Dict=None ) -> Tuple: '''simple docstring''' if device is not None: assert device in ["gpu", "cpu", "cuda"], "device should be either gpu or cpu." if device == "gpu": SCREAMING_SNAKE_CASE : int = '''cuda''' else: SCREAMING_SNAKE_CASE : List[Any] = '''cuda''' if torch.cuda.is_available() else '''cpu''' SCREAMING_SNAKE_CASE : List[Any] = AutoModelForCausalLM.from_pretrained(lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Union[str, Any] = model.to(lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Union[str, Any] = AutoTokenizer.from_pretrained(lowerCamelCase_ ) # if batch_size > 1 (which generally leads to padding being required), and # if there is not an already assigned pad_token, assign an existing # special token to also be the padding token if tokenizer.pad_token is None and batch_size > 1: SCREAMING_SNAKE_CASE : List[Any] = list(tokenizer.special_tokens_map_extended.values() ) # check that the model already has at least one special token defined assert ( len(lowerCamelCase_ ) > 0 ), "If batch_size > 1, model must have at least one special token to use for padding. Please use a different model or set batch_size=1." # assign one of the special tokens to also be the pad token tokenizer.add_special_tokens({'''pad_token''': existing_special_tokens[0]} ) if add_start_token: # leave room for <BOS> token to be added: assert ( tokenizer.bos_token is not None ), "Input model must already have a BOS token if using add_start_token=True. Please use a different model, or set add_start_token=False" SCREAMING_SNAKE_CASE : Optional[Any] = model.config.max_length - 1 else: SCREAMING_SNAKE_CASE : Any = model.config.max_length SCREAMING_SNAKE_CASE : Dict = tokenizer( lowerCamelCase_ , add_special_tokens=lowerCamelCase_ , padding=lowerCamelCase_ , truncation=lowerCamelCase_ , max_length=lowerCamelCase_ , return_tensors='''pt''' , return_attention_mask=lowerCamelCase_ , ).to(lowerCamelCase_ ) SCREAMING_SNAKE_CASE : str = encodings['''input_ids'''] SCREAMING_SNAKE_CASE : List[str] = encodings['''attention_mask'''] # check that each input is long enough: if add_start_token: assert torch.all(torch.ge(attn_masks.sum(1 ) , 1 ) ), "Each input text must be at least one token long." else: assert torch.all( torch.ge(attn_masks.sum(1 ) , 2 ) ), "When add_start_token=False, each input text must be at least two tokens long. Run with add_start_token=True if inputting strings of only one token, and remove all empty input strings." SCREAMING_SNAKE_CASE : List[Any] = [] SCREAMING_SNAKE_CASE : Union[str, Any] = CrossEntropyLoss(reduction='''none''' ) for start_index in logging.tqdm(range(0 , len(lowerCamelCase_ ) , lowerCamelCase_ ) ): SCREAMING_SNAKE_CASE : Union[str, Any] = min(start_index + batch_size , len(lowerCamelCase_ ) ) SCREAMING_SNAKE_CASE : Optional[Any] = encoded_texts[start_index:end_index] SCREAMING_SNAKE_CASE : str = attn_masks[start_index:end_index] if add_start_token: SCREAMING_SNAKE_CASE : Optional[int] = torch.tensor([[tokenizer.bos_token_id]] * encoded_batch.size(dim=0 ) ).to(lowerCamelCase_ ) SCREAMING_SNAKE_CASE : List[Any] = torch.cat([bos_tokens_tensor, encoded_batch] , dim=1 ) SCREAMING_SNAKE_CASE : Dict = torch.cat( [torch.ones(bos_tokens_tensor.size() , dtype=torch.intaa ).to(lowerCamelCase_ ), attn_mask] , dim=1 ) SCREAMING_SNAKE_CASE : str = encoded_batch with torch.no_grad(): SCREAMING_SNAKE_CASE : Tuple = model(lowerCamelCase_ , attention_mask=lowerCamelCase_ ).logits SCREAMING_SNAKE_CASE : int = out_logits[..., :-1, :].contiguous() SCREAMING_SNAKE_CASE : str = labels[..., 1:].contiguous() SCREAMING_SNAKE_CASE : Optional[int] = attn_mask[..., 1:].contiguous() SCREAMING_SNAKE_CASE : Any = torch.expa( (loss_fct(shift_logits.transpose(1 , 2 ) , lowerCamelCase_ ) * shift_attention_mask_batch).sum(1 ) / shift_attention_mask_batch.sum(1 ) ) ppls += perplexity_batch.tolist() return {"perplexities": ppls, "mean_perplexity": np.mean(lowerCamelCase_ )}
18
"""simple docstring""" def __A ( a_ : int )-> list[int]: '''simple docstring''' if num <= 0: raise ValueError('''Input must be a positive integer''' ) SCREAMING_SNAKE_CASE : Optional[int] = [True] * (num + 1) SCREAMING_SNAKE_CASE : Optional[Any] = 2 while p * p <= num: if primes[p]: for i in range(p * p , num + 1 , a_ ): SCREAMING_SNAKE_CASE : Any = False p += 1 return [prime for prime in range(2 , num + 1 ) if primes[prime]] if __name__ == "__main__": import doctest doctest.testmod() lowerCamelCase__ : str = int(input("Enter a positive integer: ").strip()) print(prime_sieve_eratosthenes(user_num))
18
1
"""simple docstring""" import multiprocessing import time from arguments import PretokenizationArguments from datasets import load_dataset from transformers import AutoTokenizer, HfArgumentParser def __A ( a_ : Any )-> str: '''simple docstring''' SCREAMING_SNAKE_CASE : Any = {} SCREAMING_SNAKE_CASE : Optional[Any] = tokenizer(example['''content'''] , truncation=a_ )['''input_ids'''] SCREAMING_SNAKE_CASE : Union[str, Any] = len(example['''content'''] ) / len(output['''input_ids'''] ) return output lowerCamelCase__ : Any = HfArgumentParser(PretokenizationArguments) lowerCamelCase__ : int = parser.parse_args() if args.num_workers is None: lowerCamelCase__ : List[str] = multiprocessing.cpu_count() lowerCamelCase__ : Optional[int] = AutoTokenizer.from_pretrained(args.tokenizer_dir) lowerCamelCase__ : int = time.time() lowerCamelCase__ : Union[str, Any] = load_dataset(args.dataset_name, split="train") print(f'''Dataset loaded in {time.time()-t_start:.2f}s''') lowerCamelCase__ : Optional[Any] = time.time() lowerCamelCase__ : List[Any] = ds.map( tokenize, num_proc=args.num_workers, remove_columns=[ "repo_name", "path", "copies", "size", "content", "license", "hash", "line_mean", "line_max", "alpha_frac", "autogenerated", ], ) print(f'''Dataset tokenized in {time.time()-t_start:.2f}s''') lowerCamelCase__ : Optional[Any] = time.time() ds.push_to_hub(args.tokenized_data_repo) print(f'''Data pushed to the hub in {time.time()-t_start:.2f}s''')
18
"""simple docstring""" from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_tf_available, is_tokenizers_available, is_torch_available, ) lowerCamelCase__ : Optional[Any] = { "configuration_funnel": ["FUNNEL_PRETRAINED_CONFIG_ARCHIVE_MAP", "FunnelConfig"], "convert_funnel_original_tf_checkpoint_to_pytorch": [], "tokenization_funnel": ["FunnelTokenizer"], } try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCamelCase__ : Optional[Any] = ["FunnelTokenizerFast"] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCamelCase__ : Dict = [ "FUNNEL_PRETRAINED_MODEL_ARCHIVE_LIST", "FunnelBaseModel", "FunnelForMaskedLM", "FunnelForMultipleChoice", "FunnelForPreTraining", "FunnelForQuestionAnswering", "FunnelForSequenceClassification", "FunnelForTokenClassification", "FunnelModel", "FunnelPreTrainedModel", "load_tf_weights_in_funnel", ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCamelCase__ : Tuple = [ "TF_FUNNEL_PRETRAINED_MODEL_ARCHIVE_LIST", "TFFunnelBaseModel", "TFFunnelForMaskedLM", "TFFunnelForMultipleChoice", "TFFunnelForPreTraining", "TFFunnelForQuestionAnswering", "TFFunnelForSequenceClassification", "TFFunnelForTokenClassification", "TFFunnelModel", "TFFunnelPreTrainedModel", ] if TYPE_CHECKING: from .configuration_funnel import FUNNEL_PRETRAINED_CONFIG_ARCHIVE_MAP, FunnelConfig from .tokenization_funnel import FunnelTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_funnel_fast import FunnelTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_funnel import ( FUNNEL_PRETRAINED_MODEL_ARCHIVE_LIST, FunnelBaseModel, FunnelForMaskedLM, FunnelForMultipleChoice, FunnelForPreTraining, FunnelForQuestionAnswering, FunnelForSequenceClassification, FunnelForTokenClassification, FunnelModel, FunnelPreTrainedModel, load_tf_weights_in_funnel, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_funnel import ( TF_FUNNEL_PRETRAINED_MODEL_ARCHIVE_LIST, TFFunnelBaseModel, TFFunnelForMaskedLM, TFFunnelForMultipleChoice, TFFunnelForPreTraining, TFFunnelForQuestionAnswering, TFFunnelForSequenceClassification, TFFunnelForTokenClassification, TFFunnelModel, TFFunnelPreTrainedModel, ) else: import sys lowerCamelCase__ : Optional[int] = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
18
1
"""simple docstring""" import math import flax.linen as nn import jax.numpy as jnp def __A ( a_ : jnp.ndarray , a_ : int , a_ : float = 1 , a_ : float = 1 , a_ : float = 1.0E4 , a_ : bool = False , a_ : float = 1.0 , )-> jnp.ndarray: '''simple docstring''' assert timesteps.ndim == 1, "Timesteps should be a 1d-array" assert embedding_dim % 2 == 0, F"Embedding dimension {embedding_dim} should be even" SCREAMING_SNAKE_CASE : List[Any] = float(embedding_dim // 2 ) SCREAMING_SNAKE_CASE : List[Any] = math.log(max_timescale / min_timescale ) / (num_timescales - freq_shift) SCREAMING_SNAKE_CASE : Union[str, Any] = min_timescale * jnp.exp(jnp.arange(a_ , dtype=jnp.floataa ) * -log_timescale_increment ) SCREAMING_SNAKE_CASE : Tuple = jnp.expand_dims(a_ , 1 ) * jnp.expand_dims(a_ , 0 ) # scale embeddings SCREAMING_SNAKE_CASE : int = scale * emb if flip_sin_to_cos: SCREAMING_SNAKE_CASE : Optional[int] = jnp.concatenate([jnp.cos(a_ ), jnp.sin(a_ )] , axis=1 ) else: SCREAMING_SNAKE_CASE : Union[str, Any] = jnp.concatenate([jnp.sin(a_ ), jnp.cos(a_ )] , axis=1 ) SCREAMING_SNAKE_CASE : List[str] = jnp.reshape(a_ , [jnp.shape(a_ )[0], embedding_dim] ) return signal class lowercase__( nn.Module ): '''simple docstring''' UpperCamelCase = 32 UpperCamelCase = jnp.floataa @nn.compact def __call__( self :Optional[int] , lowerCamelCase_ :List[Any] ) -> Optional[int]: '''simple docstring''' SCREAMING_SNAKE_CASE : Tuple = nn.Dense(self.time_embed_dim , dtype=self.dtype , name='''linear_1''' )(lowerCamelCase_ ) SCREAMING_SNAKE_CASE : str = nn.silu(lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Union[str, Any] = nn.Dense(self.time_embed_dim , dtype=self.dtype , name='''linear_2''' )(lowerCamelCase_ ) return temb class lowercase__( nn.Module ): '''simple docstring''' UpperCamelCase = 32 UpperCamelCase = False UpperCamelCase = 1 @nn.compact def __call__( self :List[str] , lowerCamelCase_ :Any ) -> List[str]: '''simple docstring''' return get_sinusoidal_embeddings( lowerCamelCase_ , embedding_dim=self.dim , flip_sin_to_cos=self.flip_sin_to_cos , freq_shift=self.freq_shift )
18
"""simple docstring""" import os import sys lowerCamelCase__ : List[Any] = os.path.join(os.path.dirname(__file__), "src") sys.path.append(SRC_DIR) from transformers import ( AutoConfig, AutoModel, AutoModelForCausalLM, AutoModelForMaskedLM, AutoModelForQuestionAnswering, AutoModelForSequenceClassification, AutoTokenizer, add_start_docstrings, ) lowerCamelCase__ : str = [ "torch", "numpy", "tokenizers", "filelock", "requests", "tqdm", "regex", "sentencepiece", "sacremoses", "importlib_metadata", "huggingface_hub", ] @add_start_docstrings(AutoConfig.__doc__ ) def __A ( *a_ : Any , **a_ : Union[str, Any] )-> Dict: '''simple docstring''' return AutoConfig.from_pretrained(*a_ , **a_ ) @add_start_docstrings(AutoTokenizer.__doc__ ) def __A ( *a_ : str , **a_ : Union[str, Any] )-> Union[str, Any]: '''simple docstring''' return AutoTokenizer.from_pretrained(*a_ , **a_ ) @add_start_docstrings(AutoModel.__doc__ ) def __A ( *a_ : List[str] , **a_ : int )-> Dict: '''simple docstring''' return AutoModel.from_pretrained(*a_ , **a_ ) @add_start_docstrings(AutoModelForCausalLM.__doc__ ) def __A ( *a_ : Any , **a_ : Tuple )-> Dict: '''simple docstring''' return AutoModelForCausalLM.from_pretrained(*a_ , **a_ ) @add_start_docstrings(AutoModelForMaskedLM.__doc__ ) def __A ( *a_ : Dict , **a_ : Optional[Any] )-> Optional[int]: '''simple docstring''' return AutoModelForMaskedLM.from_pretrained(*a_ , **a_ ) @add_start_docstrings(AutoModelForSequenceClassification.__doc__ ) def __A ( *a_ : Optional[int] , **a_ : str )-> Optional[int]: '''simple docstring''' return AutoModelForSequenceClassification.from_pretrained(*a_ , **a_ ) @add_start_docstrings(AutoModelForQuestionAnswering.__doc__ ) def __A ( *a_ : List[str] , **a_ : int )-> List[Any]: '''simple docstring''' return AutoModelForQuestionAnswering.from_pretrained(*a_ , **a_ )
18
1
"""simple docstring""" from typing import TYPE_CHECKING # rely on isort to merge the imports from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available lowerCamelCase__ : Optional[Any] = { "configuration_autoformer": [ "AUTOFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP", "AutoformerConfig", ], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCamelCase__ : int = [ "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 lowerCamelCase__ : List[Any] = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
18
"""simple docstring""" import math from typing import Optional import numpy as np from ...configuration_utils import PretrainedConfig from ...utils import logging lowerCamelCase__ : Tuple = logging.get_logger(__name__) lowerCamelCase__ : Any = { "facebook/encodec_24khz": "https://huggingface.co/facebook/encodec_24khz/resolve/main/config.json", "facebook/encodec_48khz": "https://huggingface.co/facebook/encodec_48khz/resolve/main/config.json", } class lowercase__( _UpperCAmelCase ): '''simple docstring''' UpperCamelCase = """encodec""" def __init__( self :List[str] , lowerCamelCase_ :Tuple=[1.5, 3.0, 6.0, 1_2.0, 2_4.0] , lowerCamelCase_ :str=2_40_00 , lowerCamelCase_ :Any=1 , lowerCamelCase_ :List[Any]=False , lowerCamelCase_ :Optional[int]=None , lowerCamelCase_ :Optional[Any]=None , lowerCamelCase_ :str=1_28 , lowerCamelCase_ :Any=32 , lowerCamelCase_ :int=1 , lowerCamelCase_ :Dict=[8, 5, 4, 2] , lowerCamelCase_ :List[Any]="weight_norm" , lowerCamelCase_ :Optional[int]=7 , lowerCamelCase_ :Tuple=7 , lowerCamelCase_ :Optional[Any]=3 , lowerCamelCase_ :int=2 , lowerCamelCase_ :Dict=True , lowerCamelCase_ :Optional[int]="reflect" , lowerCamelCase_ :Optional[int]=2 , lowerCamelCase_ :Union[str, Any]=2 , lowerCamelCase_ :Dict=1.0 , lowerCamelCase_ :Any=10_24 , lowerCamelCase_ :str=None , lowerCamelCase_ :Union[str, Any]=True , **lowerCamelCase_ :Optional[int] , ) -> Dict: '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[Any] = target_bandwidths SCREAMING_SNAKE_CASE : List[str] = sampling_rate SCREAMING_SNAKE_CASE : Tuple = audio_channels SCREAMING_SNAKE_CASE : Tuple = normalize SCREAMING_SNAKE_CASE : str = chunk_length_s SCREAMING_SNAKE_CASE : List[str] = overlap SCREAMING_SNAKE_CASE : int = hidden_size SCREAMING_SNAKE_CASE : Optional[int] = num_filters SCREAMING_SNAKE_CASE : Tuple = num_residual_layers SCREAMING_SNAKE_CASE : List[Any] = upsampling_ratios SCREAMING_SNAKE_CASE : Optional[int] = norm_type SCREAMING_SNAKE_CASE : Any = kernel_size SCREAMING_SNAKE_CASE : Union[str, Any] = last_kernel_size SCREAMING_SNAKE_CASE : Tuple = residual_kernel_size SCREAMING_SNAKE_CASE : Any = dilation_growth_rate SCREAMING_SNAKE_CASE : Optional[int] = use_causal_conv SCREAMING_SNAKE_CASE : str = pad_mode SCREAMING_SNAKE_CASE : List[Any] = compress SCREAMING_SNAKE_CASE : Optional[Any] = num_lstm_layers SCREAMING_SNAKE_CASE : Dict = trim_right_ratio SCREAMING_SNAKE_CASE : List[Any] = codebook_size SCREAMING_SNAKE_CASE : Union[str, Any] = codebook_dim if codebook_dim is not None else hidden_size SCREAMING_SNAKE_CASE : Union[str, Any] = use_conv_shortcut if self.norm_type not in ["weight_norm", "time_group_norm"]: raise ValueError( f"self.norm_type must be one of `\"weight_norm\"`, `\"time_group_norm\"`), got {self.norm_type}" ) super().__init__(**lowerCamelCase_ ) @property def __lowerCAmelCase ( self :Optional[Any] ) -> Optional[int]: '''simple docstring''' if self.chunk_length_s is None: return None else: return int(self.chunk_length_s * self.sampling_rate ) @property def __lowerCAmelCase ( self :List[str] ) -> Optional[int]: '''simple docstring''' if self.chunk_length_s is None or self.overlap is None: return None else: return max(1 , int((1.0 - self.overlap) * self.chunk_length ) ) @property def __lowerCAmelCase ( self :Dict ) -> int: '''simple docstring''' SCREAMING_SNAKE_CASE : Tuple = np.prod(self.upsampling_ratios ) return math.ceil(self.sampling_rate / hop_length ) @property def __lowerCAmelCase ( self :Dict ) -> int: '''simple docstring''' return int(10_00 * self.target_bandwidths[-1] // (self.frame_rate * 10) )
18
1
"""simple docstring""" import gc import random import tempfile import unittest import numpy as np import torch from transformers import CLIPTextConfig, CLIPTextModel, CLIPTokenizer from diffusers import AutoencoderKL, DDIMScheduler, LMSDiscreteScheduler, PNDMScheduler, UNetaDConditionModel from diffusers.pipelines.stable_diffusion_safe import StableDiffusionPipelineSafe as StableDiffusionPipeline from diffusers.utils import floats_tensor, nightly, torch_device from diffusers.utils.testing_utils import require_torch_gpu class lowercase__( unittest.TestCase ): '''simple docstring''' def __lowerCAmelCase ( self :str ) -> List[str]: '''simple docstring''' super().tearDown() gc.collect() torch.cuda.empty_cache() @property def __lowerCAmelCase ( self :Optional[Any] ) -> str: '''simple docstring''' SCREAMING_SNAKE_CASE : List[Any] = 1 SCREAMING_SNAKE_CASE : Any = 3 SCREAMING_SNAKE_CASE : Optional[int] = (32, 32) SCREAMING_SNAKE_CASE : Optional[int] = floats_tensor((batch_size, num_channels) + sizes , rng=random.Random(0 ) ).to(lowerCamelCase_ ) return image @property def __lowerCAmelCase ( self :Union[str, Any] ) -> Dict: '''simple docstring''' torch.manual_seed(0 ) SCREAMING_SNAKE_CASE : str = UNetaDConditionModel( block_out_channels=(32, 64) , layers_per_block=2 , sample_size=32 , in_channels=4 , out_channels=4 , down_block_types=('''DownBlock2D''', '''CrossAttnDownBlock2D''') , up_block_types=('''CrossAttnUpBlock2D''', '''UpBlock2D''') , cross_attention_dim=32 , ) return model @property def __lowerCAmelCase ( self :Tuple ) -> Union[str, Any]: '''simple docstring''' torch.manual_seed(0 ) SCREAMING_SNAKE_CASE : Any = AutoencoderKL( block_out_channels=[32, 64] , in_channels=3 , out_channels=3 , down_block_types=['''DownEncoderBlock2D''', '''DownEncoderBlock2D'''] , up_block_types=['''UpDecoderBlock2D''', '''UpDecoderBlock2D'''] , latent_channels=4 , ) return model @property def __lowerCAmelCase ( self :Any ) -> Union[str, Any]: '''simple docstring''' torch.manual_seed(0 ) SCREAMING_SNAKE_CASE : Any = CLIPTextConfig( bos_token_id=0 , eos_token_id=2 , hidden_size=32 , intermediate_size=37 , layer_norm_eps=1E-05 , num_attention_heads=4 , num_hidden_layers=5 , pad_token_id=1 , vocab_size=10_00 , ) return CLIPTextModel(lowerCamelCase_ ) @property def __lowerCAmelCase ( self :Optional[Any] ) -> str: '''simple docstring''' def extract(*lowerCamelCase_ :Union[str, Any] , **lowerCamelCase_ :List[str] ): class lowercase__: '''simple docstring''' def __init__( self :Any ) -> Union[str, Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : Any = torch.ones([0] ) def __lowerCAmelCase ( self :Any , lowerCamelCase_ :Tuple ) -> int: '''simple docstring''' self.pixel_values.to(lowerCamelCase_ ) return self return Out() return extract def __lowerCAmelCase ( self :Union[str, Any] ) -> int: '''simple docstring''' SCREAMING_SNAKE_CASE : Any = '''cpu''' # ensure determinism for the device-dependent torch.Generator SCREAMING_SNAKE_CASE : Any = self.dummy_cond_unet SCREAMING_SNAKE_CASE : int = DDIMScheduler( beta_start=0.0_0_0_8_5 , beta_end=0.0_1_2 , beta_schedule='''scaled_linear''' , clip_sample=lowerCamelCase_ , set_alpha_to_one=lowerCamelCase_ , ) SCREAMING_SNAKE_CASE : Optional[int] = self.dummy_vae SCREAMING_SNAKE_CASE : Optional[int] = self.dummy_text_encoder SCREAMING_SNAKE_CASE : str = CLIPTokenizer.from_pretrained('''hf-internal-testing/tiny-random-clip''' ) # make sure here that pndm scheduler skips prk SCREAMING_SNAKE_CASE : List[str] = StableDiffusionPipeline( unet=lowerCamelCase_ , scheduler=lowerCamelCase_ , vae=lowerCamelCase_ , text_encoder=lowerCamelCase_ , tokenizer=lowerCamelCase_ , safety_checker=lowerCamelCase_ , feature_extractor=self.dummy_extractor , ) SCREAMING_SNAKE_CASE : str = sd_pipe.to(lowerCamelCase_ ) sd_pipe.set_progress_bar_config(disable=lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Optional[int] = '''A painting of a squirrel eating a burger''' SCREAMING_SNAKE_CASE : Tuple = torch.Generator(device=lowerCamelCase_ ).manual_seed(0 ) SCREAMING_SNAKE_CASE : List[Any] = sd_pipe([prompt] , generator=lowerCamelCase_ , guidance_scale=6.0 , num_inference_steps=2 , output_type='''np''' ) SCREAMING_SNAKE_CASE : Tuple = output.images SCREAMING_SNAKE_CASE : Tuple = torch.Generator(device=lowerCamelCase_ ).manual_seed(0 ) SCREAMING_SNAKE_CASE : Union[str, Any] = sd_pipe( [prompt] , generator=lowerCamelCase_ , guidance_scale=6.0 , num_inference_steps=2 , output_type='''np''' , return_dict=lowerCamelCase_ , )[0] SCREAMING_SNAKE_CASE : Any = image[0, -3:, -3:, -1] SCREAMING_SNAKE_CASE : List[Any] = image_from_tuple[0, -3:, -3:, -1] assert image.shape == (1, 64, 64, 3) SCREAMING_SNAKE_CASE : Optional[int] = np.array([0.5_7_5_6, 0.6_1_1_8, 0.5_0_0_5, 0.5_0_4_1, 0.5_4_7_1, 0.4_7_2_6, 0.4_9_7_6, 0.4_8_6_5, 0.4_8_6_4] ) 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 __lowerCAmelCase ( self :int ) -> Any: '''simple docstring''' SCREAMING_SNAKE_CASE : int = '''cpu''' # ensure determinism for the device-dependent torch.Generator SCREAMING_SNAKE_CASE : int = self.dummy_cond_unet SCREAMING_SNAKE_CASE : Optional[Any] = PNDMScheduler(skip_prk_steps=lowerCamelCase_ ) SCREAMING_SNAKE_CASE : List[str] = self.dummy_vae SCREAMING_SNAKE_CASE : Optional[int] = self.dummy_text_encoder SCREAMING_SNAKE_CASE : int = CLIPTokenizer.from_pretrained('''hf-internal-testing/tiny-random-clip''' ) # make sure here that pndm scheduler skips prk SCREAMING_SNAKE_CASE : Optional[int] = StableDiffusionPipeline( unet=lowerCamelCase_ , scheduler=lowerCamelCase_ , vae=lowerCamelCase_ , text_encoder=lowerCamelCase_ , tokenizer=lowerCamelCase_ , safety_checker=lowerCamelCase_ , feature_extractor=self.dummy_extractor , ) SCREAMING_SNAKE_CASE : List[Any] = sd_pipe.to(lowerCamelCase_ ) sd_pipe.set_progress_bar_config(disable=lowerCamelCase_ ) SCREAMING_SNAKE_CASE : str = '''A painting of a squirrel eating a burger''' SCREAMING_SNAKE_CASE : Dict = torch.Generator(device=lowerCamelCase_ ).manual_seed(0 ) SCREAMING_SNAKE_CASE : Optional[int] = sd_pipe([prompt] , generator=lowerCamelCase_ , guidance_scale=6.0 , num_inference_steps=2 , output_type='''np''' ) SCREAMING_SNAKE_CASE : List[Any] = output.images SCREAMING_SNAKE_CASE : Any = torch.Generator(device=lowerCamelCase_ ).manual_seed(0 ) SCREAMING_SNAKE_CASE : str = sd_pipe( [prompt] , generator=lowerCamelCase_ , guidance_scale=6.0 , num_inference_steps=2 , output_type='''np''' , return_dict=lowerCamelCase_ , )[0] SCREAMING_SNAKE_CASE : Any = image[0, -3:, -3:, -1] SCREAMING_SNAKE_CASE : Optional[int] = image_from_tuple[0, -3:, -3:, -1] assert image.shape == (1, 64, 64, 3) SCREAMING_SNAKE_CASE : Optional[int] = np.array([0.5_1_2_5, 0.5_7_1_6, 0.4_8_2_8, 0.5_0_6_0, 0.5_6_5_0, 0.4_7_6_8, 0.5_1_8_5, 0.4_8_9_5, 0.4_9_9_3] ) 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 __lowerCAmelCase ( self :str ) -> List[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : str = StableDiffusionPipeline.from_pretrained( '''hf-internal-testing/tiny-stable-diffusion-lms-pipe''' , safety_checker=lowerCamelCase_ ) assert isinstance(lowerCamelCase_ , lowerCamelCase_ ) assert isinstance(pipe.scheduler , lowerCamelCase_ ) assert pipe.safety_checker is None SCREAMING_SNAKE_CASE : List[str] = pipe('''example prompt''' , num_inference_steps=2 ).images[0] assert image is not None # check that there's no error when saving a pipeline with one of the models being None with tempfile.TemporaryDirectory() as tmpdirname: pipe.save_pretrained(lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Dict = StableDiffusionPipeline.from_pretrained(lowerCamelCase_ ) # sanity check that the pipeline still works assert pipe.safety_checker is None SCREAMING_SNAKE_CASE : List[str] = pipe('''example prompt''' , num_inference_steps=2 ).images[0] assert image is not None @unittest.skipIf(torch_device != '''cuda''' , '''This test requires a GPU''' ) def __lowerCAmelCase ( self :Tuple ) -> int: '''simple docstring''' SCREAMING_SNAKE_CASE : Dict = self.dummy_cond_unet SCREAMING_SNAKE_CASE : Tuple = PNDMScheduler(skip_prk_steps=lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Dict = self.dummy_vae SCREAMING_SNAKE_CASE : List[Any] = self.dummy_text_encoder SCREAMING_SNAKE_CASE : str = CLIPTokenizer.from_pretrained('''hf-internal-testing/tiny-random-clip''' ) # put models in fp16 SCREAMING_SNAKE_CASE : Dict = unet.half() SCREAMING_SNAKE_CASE : Optional[int] = vae.half() SCREAMING_SNAKE_CASE : Optional[Any] = bert.half() # make sure here that pndm scheduler skips prk SCREAMING_SNAKE_CASE : List[str] = StableDiffusionPipeline( unet=lowerCamelCase_ , scheduler=lowerCamelCase_ , vae=lowerCamelCase_ , text_encoder=lowerCamelCase_ , tokenizer=lowerCamelCase_ , safety_checker=lowerCamelCase_ , feature_extractor=self.dummy_extractor , ) SCREAMING_SNAKE_CASE : str = sd_pipe.to(lowerCamelCase_ ) sd_pipe.set_progress_bar_config(disable=lowerCamelCase_ ) SCREAMING_SNAKE_CASE : str = '''A painting of a squirrel eating a burger''' SCREAMING_SNAKE_CASE : Dict = sd_pipe([prompt] , num_inference_steps=2 , output_type='''np''' ).images assert image.shape == (1, 64, 64, 3) @nightly @require_torch_gpu class lowercase__( unittest.TestCase ): '''simple docstring''' def __lowerCAmelCase ( self :int ) -> List[str]: '''simple docstring''' super().tearDown() gc.collect() torch.cuda.empty_cache() def __lowerCAmelCase ( self :List[str] ) -> str: '''simple docstring''' SCREAMING_SNAKE_CASE : Tuple = StableDiffusionPipeline.from_pretrained('''runwayml/stable-diffusion-v1-5''' , safety_checker=lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Optional[Any] = LMSDiscreteScheduler.from_config(sd_pipe.scheduler.config ) SCREAMING_SNAKE_CASE : Any = sd_pipe.to(lowerCamelCase_ ) sd_pipe.set_progress_bar_config(disable=lowerCamelCase_ ) SCREAMING_SNAKE_CASE : int = ( '''portrait of girl with smokey eyes makeup in abandoned hotel, grange clothes, redshift, wide high angle''' ''' coloured polaroid photograph with flash, kodak film, hyper real, stunning moody cinematography, with''' ''' anamorphic lenses, by maripol, fallen angels by wong kar - wai, style of suspiria and neon demon and''' ''' children from bahnhof zoo, detailed ''' ) SCREAMING_SNAKE_CASE : Union[str, Any] = 40_03_66_03_46 SCREAMING_SNAKE_CASE : Optional[int] = 7 # without safety guidance (sld_guidance_scale = 0) SCREAMING_SNAKE_CASE : Any = torch.manual_seed(lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Dict = sd_pipe( [prompt] , generator=lowerCamelCase_ , guidance_scale=lowerCamelCase_ , num_inference_steps=50 , output_type='''np''' , width=5_12 , height=5_12 , sld_guidance_scale=0 , ) SCREAMING_SNAKE_CASE : Union[str, Any] = output.images SCREAMING_SNAKE_CASE : List[str] = image[0, -3:, -3:, -1] SCREAMING_SNAKE_CASE : Any = [0.2_2_7_8, 0.2_2_3_1, 0.2_2_4_9, 0.2_3_3_3, 0.2_3_0_3, 0.1_8_8_5, 0.2_2_7_3, 0.2_1_4_4, 0.2_1_7_6] assert image.shape == (1, 5_12, 5_12, 3) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-2 # without safety guidance (strong configuration) SCREAMING_SNAKE_CASE : str = torch.manual_seed(lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Union[str, Any] = sd_pipe( [prompt] , generator=lowerCamelCase_ , guidance_scale=lowerCamelCase_ , num_inference_steps=50 , output_type='''np''' , width=5_12 , height=5_12 , sld_guidance_scale=20_00 , sld_warmup_steps=7 , sld_threshold=0.0_2_5 , sld_momentum_scale=0.5 , sld_mom_beta=0.7 , ) SCREAMING_SNAKE_CASE : str = output.images SCREAMING_SNAKE_CASE : List[str] = image[0, -3:, -3:, -1] SCREAMING_SNAKE_CASE : Any = [0.2_3_8_3, 0.2_2_7_6, 0.2_3_6, 0.2_1_9_2, 0.2_1_8_6, 0.2_0_5_3, 0.1_9_7_1, 0.1_9_0_1, 0.1_7_1_9] assert image.shape == (1, 5_12, 5_12, 3) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-2 def __lowerCAmelCase ( self :List[Any] ) -> Tuple: '''simple docstring''' SCREAMING_SNAKE_CASE : Tuple = StableDiffusionPipeline.from_pretrained('''runwayml/stable-diffusion-v1-5''' , safety_checker=lowerCamelCase_ ) SCREAMING_SNAKE_CASE : str = LMSDiscreteScheduler.from_config(sd_pipe.scheduler.config ) SCREAMING_SNAKE_CASE : Dict = sd_pipe.to(lowerCamelCase_ ) sd_pipe.set_progress_bar_config(disable=lowerCamelCase_ ) SCREAMING_SNAKE_CASE : int = '''padme amidala taking a bath artwork, safe for work, no nudity''' SCREAMING_SNAKE_CASE : List[Any] = 27_34_97_17_55 SCREAMING_SNAKE_CASE : str = 7 SCREAMING_SNAKE_CASE : List[str] = torch.manual_seed(lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Optional[int] = sd_pipe( [prompt] , generator=lowerCamelCase_ , guidance_scale=lowerCamelCase_ , num_inference_steps=50 , output_type='''np''' , width=5_12 , height=5_12 , sld_guidance_scale=0 , ) SCREAMING_SNAKE_CASE : Optional[int] = output.images SCREAMING_SNAKE_CASE : Dict = image[0, -3:, -3:, -1] SCREAMING_SNAKE_CASE : List[str] = [0.3_5_0_2, 0.3_6_2_2, 0.3_3_9_6, 0.3_6_4_2, 0.3_4_7_8, 0.3_3_1_8, 0.3_5, 0.3_3_4_8, 0.3_2_9_7] assert image.shape == (1, 5_12, 5_12, 3) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-2 SCREAMING_SNAKE_CASE : Dict = torch.manual_seed(lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Union[str, Any] = sd_pipe( [prompt] , generator=lowerCamelCase_ , guidance_scale=lowerCamelCase_ , num_inference_steps=50 , output_type='''np''' , width=5_12 , height=5_12 , sld_guidance_scale=20_00 , sld_warmup_steps=7 , sld_threshold=0.0_2_5 , sld_momentum_scale=0.5 , sld_mom_beta=0.7 , ) SCREAMING_SNAKE_CASE : List[Any] = output.images SCREAMING_SNAKE_CASE : List[str] = image[0, -3:, -3:, -1] SCREAMING_SNAKE_CASE : int = [0.5_5_3_1, 0.5_2_0_6, 0.4_8_9_5, 0.5_1_5_6, 0.5_1_8_2, 0.4_7_5_1, 0.4_8_0_2, 0.4_8_0_3, 0.4_4_4_3] assert image.shape == (1, 5_12, 5_12, 3) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-2 def __lowerCAmelCase ( self :Any ) -> Any: '''simple docstring''' SCREAMING_SNAKE_CASE : Union[str, Any] = StableDiffusionPipeline.from_pretrained('''runwayml/stable-diffusion-v1-5''' ) SCREAMING_SNAKE_CASE : Optional[int] = sd_pipe.to(lowerCamelCase_ ) sd_pipe.set_progress_bar_config(disable=lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Tuple = ( '''the four horsewomen of the apocalypse, painting by tom of finland, gaston bussiere, craig mullins, j. c.''' ''' leyendecker''' ) SCREAMING_SNAKE_CASE : List[str] = 10_44_35_52_34 SCREAMING_SNAKE_CASE : Any = 12 SCREAMING_SNAKE_CASE : int = torch.manual_seed(lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Any = sd_pipe( [prompt] , generator=lowerCamelCase_ , guidance_scale=lowerCamelCase_ , num_inference_steps=50 , output_type='''np''' , width=5_12 , height=5_12 , sld_guidance_scale=0 , ) SCREAMING_SNAKE_CASE : Any = output.images SCREAMING_SNAKE_CASE : List[str] = image[0, -3:, -3:, -1] SCREAMING_SNAKE_CASE : Any = np.array([0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0] ) assert image.shape == (1, 5_12, 5_12, 3) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-7 SCREAMING_SNAKE_CASE : int = torch.manual_seed(lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Union[str, Any] = sd_pipe( [prompt] , generator=lowerCamelCase_ , guidance_scale=lowerCamelCase_ , num_inference_steps=50 , output_type='''np''' , width=5_12 , height=5_12 , sld_guidance_scale=20_00 , sld_warmup_steps=7 , sld_threshold=0.0_2_5 , sld_momentum_scale=0.5 , sld_mom_beta=0.7 , ) SCREAMING_SNAKE_CASE : Union[str, Any] = output.images SCREAMING_SNAKE_CASE : Dict = image[0, -3:, -3:, -1] SCREAMING_SNAKE_CASE : str = np.array([0.5_8_1_8, 0.6_2_8_5, 0.6_8_3_5, 0.6_0_1_9, 0.6_2_5, 0.6_7_5_4, 0.6_0_9_6, 0.6_3_3_4, 0.6_5_6_1] ) assert image.shape == (1, 5_12, 5_12, 3) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-2
18
"""simple docstring""" import copy import unittest from transformers.models.auto import get_values from transformers.testing_utils import require_torch, 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, random_attention_mask from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from transformers import ( MODEL_FOR_MULTIPLE_CHOICE_MAPPING, MODEL_FOR_QUESTION_ANSWERING_MAPPING, MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING, MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING, LayoutLMvaConfig, LayoutLMvaForQuestionAnswering, LayoutLMvaForSequenceClassification, LayoutLMvaForTokenClassification, LayoutLMvaModel, ) from transformers.models.layoutlmva.modeling_layoutlmva import LAYOUTLMV3_PRETRAINED_MODEL_ARCHIVE_LIST if is_vision_available(): from PIL import Image from transformers import LayoutLMvaImageProcessor class lowercase__: '''simple docstring''' def __init__( self :Tuple , lowerCamelCase_ :Tuple , lowerCamelCase_ :Union[str, Any]=2 , lowerCamelCase_ :Any=3 , lowerCamelCase_ :Union[str, Any]=4 , lowerCamelCase_ :List[str]=2 , lowerCamelCase_ :str=7 , lowerCamelCase_ :Tuple=True , lowerCamelCase_ :Dict=True , lowerCamelCase_ :int=True , lowerCamelCase_ :List[Any]=True , lowerCamelCase_ :Optional[Any]=99 , lowerCamelCase_ :Any=36 , lowerCamelCase_ :Any=3 , lowerCamelCase_ :str=4 , lowerCamelCase_ :Tuple=37 , lowerCamelCase_ :Optional[int]="gelu" , lowerCamelCase_ :str=0.1 , lowerCamelCase_ :Optional[int]=0.1 , lowerCamelCase_ :Tuple=5_12 , lowerCamelCase_ :Optional[Any]=16 , lowerCamelCase_ :List[str]=2 , lowerCamelCase_ :Optional[int]=0.0_2 , lowerCamelCase_ :int=6 , lowerCamelCase_ :str=6 , lowerCamelCase_ :Optional[Any]=3 , lowerCamelCase_ :Union[str, Any]=4 , lowerCamelCase_ :List[Any]=None , lowerCamelCase_ :Tuple=10_00 , ) -> Union[str, Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : Dict = parent SCREAMING_SNAKE_CASE : Optional[Any] = batch_size SCREAMING_SNAKE_CASE : List[str] = num_channels SCREAMING_SNAKE_CASE : str = image_size SCREAMING_SNAKE_CASE : Optional[int] = patch_size SCREAMING_SNAKE_CASE : Tuple = text_seq_length SCREAMING_SNAKE_CASE : Optional[int] = is_training SCREAMING_SNAKE_CASE : Dict = use_input_mask SCREAMING_SNAKE_CASE : Any = use_token_type_ids SCREAMING_SNAKE_CASE : List[Any] = use_labels SCREAMING_SNAKE_CASE : List[Any] = vocab_size SCREAMING_SNAKE_CASE : Optional[Any] = hidden_size SCREAMING_SNAKE_CASE : Tuple = num_hidden_layers SCREAMING_SNAKE_CASE : Optional[int] = num_attention_heads SCREAMING_SNAKE_CASE : Optional[int] = intermediate_size SCREAMING_SNAKE_CASE : List[str] = hidden_act SCREAMING_SNAKE_CASE : Any = hidden_dropout_prob SCREAMING_SNAKE_CASE : Optional[Any] = attention_probs_dropout_prob SCREAMING_SNAKE_CASE : Union[str, Any] = max_position_embeddings SCREAMING_SNAKE_CASE : int = type_vocab_size SCREAMING_SNAKE_CASE : Union[str, Any] = type_sequence_label_size SCREAMING_SNAKE_CASE : Optional[Any] = initializer_range SCREAMING_SNAKE_CASE : Dict = coordinate_size SCREAMING_SNAKE_CASE : List[Any] = shape_size SCREAMING_SNAKE_CASE : Dict = num_labels SCREAMING_SNAKE_CASE : Union[str, Any] = num_choices SCREAMING_SNAKE_CASE : List[str] = scope SCREAMING_SNAKE_CASE : Optional[int] = range_bbox # LayoutLMv3's sequence length equals the number of text tokens + number of patches + 1 (we add 1 for the CLS token) SCREAMING_SNAKE_CASE : str = text_seq_length SCREAMING_SNAKE_CASE : int = (image_size // patch_size) ** 2 + 1 SCREAMING_SNAKE_CASE : Optional[Any] = self.text_seq_length + self.image_seq_length def __lowerCAmelCase ( self :List[Any] ) -> Optional[int]: '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[int] = ids_tensor([self.batch_size, self.text_seq_length] , self.vocab_size ) SCREAMING_SNAKE_CASE : List[Any] = ids_tensor([self.batch_size, self.text_seq_length, 4] , self.range_bbox ) # Ensure that bbox is legal for i in range(bbox.shape[0] ): for j in range(bbox.shape[1] ): if bbox[i, j, 3] < bbox[i, j, 1]: SCREAMING_SNAKE_CASE : str = bbox[i, j, 3] SCREAMING_SNAKE_CASE : List[str] = bbox[i, j, 1] SCREAMING_SNAKE_CASE : Any = t if bbox[i, j, 2] < bbox[i, j, 0]: SCREAMING_SNAKE_CASE : Any = bbox[i, j, 2] SCREAMING_SNAKE_CASE : Any = bbox[i, j, 0] SCREAMING_SNAKE_CASE : Optional[Any] = t SCREAMING_SNAKE_CASE : Dict = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) SCREAMING_SNAKE_CASE : Optional[Any] = None if self.use_input_mask: SCREAMING_SNAKE_CASE : Dict = random_attention_mask([self.batch_size, self.text_seq_length] ) SCREAMING_SNAKE_CASE : Any = None if self.use_token_type_ids: SCREAMING_SNAKE_CASE : Tuple = ids_tensor([self.batch_size, self.text_seq_length] , self.type_vocab_size ) SCREAMING_SNAKE_CASE : Tuple = None SCREAMING_SNAKE_CASE : Optional[int] = None if self.use_labels: SCREAMING_SNAKE_CASE : List[str] = ids_tensor([self.batch_size] , self.type_sequence_label_size ) SCREAMING_SNAKE_CASE : Dict = ids_tensor([self.batch_size, self.text_seq_length] , self.num_labels ) SCREAMING_SNAKE_CASE : List[str] = 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 __lowerCAmelCase ( self :int , lowerCamelCase_ :Union[str, Any] , lowerCamelCase_ :int , lowerCamelCase_ :str , lowerCamelCase_ :int , lowerCamelCase_ :Union[str, Any] , lowerCamelCase_ :Any , lowerCamelCase_ :Dict , lowerCamelCase_ :Union[str, Any] ) -> Any: '''simple docstring''' SCREAMING_SNAKE_CASE : Dict = LayoutLMvaModel(config=lowerCamelCase_ ) model.to(lowerCamelCase_ ) model.eval() # text + image SCREAMING_SNAKE_CASE : Optional[Any] = model(lowerCamelCase_ , pixel_values=lowerCamelCase_ ) SCREAMING_SNAKE_CASE : List[str] = model( lowerCamelCase_ , bbox=lowerCamelCase_ , pixel_values=lowerCamelCase_ , attention_mask=lowerCamelCase_ , token_type_ids=lowerCamelCase_ ) SCREAMING_SNAKE_CASE : List[Any] = model(lowerCamelCase_ , bbox=lowerCamelCase_ , pixel_values=lowerCamelCase_ , token_type_ids=lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Optional[Any] = model(lowerCamelCase_ , bbox=lowerCamelCase_ , pixel_values=lowerCamelCase_ ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) # text only SCREAMING_SNAKE_CASE : List[str] = model(lowerCamelCase_ ) self.parent.assertEqual( result.last_hidden_state.shape , (self.batch_size, self.text_seq_length, self.hidden_size) ) # image only SCREAMING_SNAKE_CASE : List[str] = model(pixel_values=lowerCamelCase_ ) self.parent.assertEqual( result.last_hidden_state.shape , (self.batch_size, self.image_seq_length, self.hidden_size) ) def __lowerCAmelCase ( self :str , lowerCamelCase_ :Union[str, Any] , lowerCamelCase_ :Union[str, Any] , lowerCamelCase_ :Tuple , lowerCamelCase_ :List[str] , lowerCamelCase_ :Any , lowerCamelCase_ :int , lowerCamelCase_ :List[Any] , lowerCamelCase_ :List[str] ) -> Tuple: '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[Any] = self.num_labels SCREAMING_SNAKE_CASE : Dict = LayoutLMvaForSequenceClassification(lowerCamelCase_ ) model.to(lowerCamelCase_ ) model.eval() SCREAMING_SNAKE_CASE : int = model( lowerCamelCase_ , bbox=lowerCamelCase_ , pixel_values=lowerCamelCase_ , attention_mask=lowerCamelCase_ , token_type_ids=lowerCamelCase_ , labels=lowerCamelCase_ , ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_labels) ) def __lowerCAmelCase ( self :Union[str, Any] , lowerCamelCase_ :Tuple , lowerCamelCase_ :Any , lowerCamelCase_ :Optional[int] , lowerCamelCase_ :Any , lowerCamelCase_ :Optional[int] , lowerCamelCase_ :int , lowerCamelCase_ :str , lowerCamelCase_ :Any ) -> Union[str, Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : int = self.num_labels SCREAMING_SNAKE_CASE : int = LayoutLMvaForTokenClassification(config=lowerCamelCase_ ) model.to(lowerCamelCase_ ) model.eval() SCREAMING_SNAKE_CASE : Optional[Any] = model( lowerCamelCase_ , bbox=lowerCamelCase_ , pixel_values=lowerCamelCase_ , attention_mask=lowerCamelCase_ , token_type_ids=lowerCamelCase_ , labels=lowerCamelCase_ , ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.text_seq_length, self.num_labels) ) def __lowerCAmelCase ( self :Optional[Any] , lowerCamelCase_ :Dict , lowerCamelCase_ :Dict , lowerCamelCase_ :Dict , lowerCamelCase_ :int , lowerCamelCase_ :List[str] , lowerCamelCase_ :int , lowerCamelCase_ :str , lowerCamelCase_ :Optional[int] ) -> Union[str, Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[int] = LayoutLMvaForQuestionAnswering(config=lowerCamelCase_ ) model.to(lowerCamelCase_ ) model.eval() SCREAMING_SNAKE_CASE : List[Any] = model( lowerCamelCase_ , bbox=lowerCamelCase_ , pixel_values=lowerCamelCase_ , attention_mask=lowerCamelCase_ , token_type_ids=lowerCamelCase_ , start_positions=lowerCamelCase_ , end_positions=lowerCamelCase_ , ) self.parent.assertEqual(result.start_logits.shape , (self.batch_size, self.seq_length) ) self.parent.assertEqual(result.end_logits.shape , (self.batch_size, self.seq_length) ) def __lowerCAmelCase ( self :Optional[int] ) -> Optional[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[int] = self.prepare_config_and_inputs() ( ( SCREAMING_SNAKE_CASE ), ( SCREAMING_SNAKE_CASE ), ( SCREAMING_SNAKE_CASE ), ( SCREAMING_SNAKE_CASE ), ( SCREAMING_SNAKE_CASE ), ( SCREAMING_SNAKE_CASE ), ( SCREAMING_SNAKE_CASE ), ( SCREAMING_SNAKE_CASE ), ) : Tuple = config_and_inputs SCREAMING_SNAKE_CASE : Dict = { '''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_torch class lowercase__( _UpperCAmelCase , _UpperCAmelCase , unittest.TestCase ): '''simple docstring''' UpperCamelCase = False UpperCamelCase = False UpperCamelCase = False UpperCamelCase = ( ( LayoutLMvaModel, LayoutLMvaForSequenceClassification, LayoutLMvaForTokenClassification, LayoutLMvaForQuestionAnswering, ) if is_torch_available() else () ) UpperCamelCase = ( {"""document-question-answering""": LayoutLMvaForQuestionAnswering, """feature-extraction""": LayoutLMvaModel} if is_torch_available() else {} ) def __lowerCAmelCase ( self :int , lowerCamelCase_ :Union[str, Any] , lowerCamelCase_ :str , lowerCamelCase_ :str , lowerCamelCase_ :Optional[Any] , lowerCamelCase_ :List[str] ) -> Union[str, Any]: '''simple docstring''' return True def __lowerCAmelCase ( self :List[str] ) -> Union[str, Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : str = LayoutLMvaModelTester(self ) SCREAMING_SNAKE_CASE : List[Any] = ConfigTester(self , config_class=lowerCamelCase_ , hidden_size=37 ) def __lowerCAmelCase ( self :Any , lowerCamelCase_ :Dict , lowerCamelCase_ :List[str] , lowerCamelCase_ :str=False ) -> Tuple: '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[Any] = copy.deepcopy(lowerCamelCase_ ) if model_class in get_values(lowerCamelCase_ ): SCREAMING_SNAKE_CASE : Tuple = { k: v.unsqueeze(1 ).expand(-1 , self.model_tester.num_choices , -1 ).contiguous() if isinstance(lowerCamelCase_ , torch.Tensor ) and v.ndim > 1 else v for k, v in inputs_dict.items() } if return_labels: if model_class in get_values(lowerCamelCase_ ): SCREAMING_SNAKE_CASE : Union[str, Any] = torch.ones(self.model_tester.batch_size , dtype=torch.long , device=lowerCamelCase_ ) elif model_class in get_values(lowerCamelCase_ ): SCREAMING_SNAKE_CASE : List[Any] = torch.zeros( self.model_tester.batch_size , dtype=torch.long , device=lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Any = torch.zeros( self.model_tester.batch_size , dtype=torch.long , device=lowerCamelCase_ ) elif model_class in [ *get_values(lowerCamelCase_ ), ]: SCREAMING_SNAKE_CASE : str = torch.zeros( self.model_tester.batch_size , dtype=torch.long , device=lowerCamelCase_ ) elif model_class in [ *get_values(lowerCamelCase_ ), ]: SCREAMING_SNAKE_CASE : Dict = torch.zeros( (self.model_tester.batch_size, self.model_tester.text_seq_length) , dtype=torch.long , device=lowerCamelCase_ , ) return inputs_dict def __lowerCAmelCase ( self :Optional[Any] ) -> Optional[int]: '''simple docstring''' self.config_tester.run_common_tests() def __lowerCAmelCase ( self :str ) -> List[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : int = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*lowerCamelCase_ ) def __lowerCAmelCase ( self :Any ) -> List[str]: '''simple docstring''' SCREAMING_SNAKE_CASE : int = self.model_tester.prepare_config_and_inputs() for type in ["absolute", "relative_key", "relative_key_query"]: SCREAMING_SNAKE_CASE : str = type self.model_tester.create_and_check_model(*lowerCamelCase_ ) def __lowerCAmelCase ( self :Union[str, Any] ) -> str: '''simple docstring''' SCREAMING_SNAKE_CASE : List[Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_sequence_classification(*lowerCamelCase_ ) def __lowerCAmelCase ( self :str ) -> Any: '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_token_classification(*lowerCamelCase_ ) def __lowerCAmelCase ( self :List[str] ) -> Union[str, Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : int = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_question_answering(*lowerCamelCase_ ) @slow def __lowerCAmelCase ( self :int ) -> Union[str, Any]: '''simple docstring''' for model_name in LAYOUTLMV3_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: SCREAMING_SNAKE_CASE : Optional[int] = LayoutLMvaModel.from_pretrained(lowerCamelCase_ ) self.assertIsNotNone(lowerCamelCase_ ) def __A ( )-> Optional[int]: '''simple docstring''' SCREAMING_SNAKE_CASE : Tuple = Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png''' ) return image @require_torch class lowercase__( unittest.TestCase ): '''simple docstring''' @cached_property def __lowerCAmelCase ( self :str ) -> int: '''simple docstring''' return LayoutLMvaImageProcessor(apply_ocr=lowerCamelCase_ ) if is_vision_available() else None @slow def __lowerCAmelCase ( self :Optional[Any] ) -> Dict: '''simple docstring''' SCREAMING_SNAKE_CASE : Union[str, Any] = LayoutLMvaModel.from_pretrained('''microsoft/layoutlmv3-base''' ).to(lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Any = self.default_image_processor SCREAMING_SNAKE_CASE : List[Any] = prepare_img() SCREAMING_SNAKE_CASE : Tuple = image_processor(images=lowerCamelCase_ , return_tensors='''pt''' ).pixel_values.to(lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Optional[Any] = torch.tensor([[1, 2]] ) SCREAMING_SNAKE_CASE : Dict = torch.tensor([[1, 2, 3, 4], [5, 6, 7, 8]] ).unsqueeze(0 ) # forward pass SCREAMING_SNAKE_CASE : Tuple = model( input_ids=input_ids.to(lowerCamelCase_ ) , bbox=bbox.to(lowerCamelCase_ ) , pixel_values=pixel_values.to(lowerCamelCase_ ) , ) # verify the logits SCREAMING_SNAKE_CASE : Union[str, Any] = torch.Size((1, 1_99, 7_68) ) self.assertEqual(outputs.last_hidden_state.shape , lowerCamelCase_ ) SCREAMING_SNAKE_CASE : List[Any] = torch.tensor( [[-0.0_5_2_9, 0.3_6_1_8, 0.1_6_3_2], [-0.1_5_8_7, -0.1_6_6_7, -0.0_4_0_0], [-0.1_5_5_7, -0.1_6_7_1, -0.0_5_0_5]] ).to(lowerCamelCase_ ) self.assertTrue(torch.allclose(outputs.last_hidden_state[0, :3, :3] , lowerCamelCase_ , atol=1E-4 ) )
18
1
"""simple docstring""" import logging import os import quant_trainer import torch from torch.utils.data import DataLoader from transformers import Trainer, is_torch_tpu_available from transformers.trainer_utils import PredictionOutput lowerCamelCase__ : Dict = logging.getLogger(__name__) if is_torch_tpu_available(check_device=False): import torch_xla.core.xla_model as xm import torch_xla.debug.metrics as met class lowercase__( _UpperCAmelCase ): '''simple docstring''' def __init__( self :Any , *lowerCamelCase_ :Any , lowerCamelCase_ :str=None , lowerCamelCase_ :Union[str, Any]=None , lowerCamelCase_ :Optional[int]=None , **lowerCamelCase_ :Dict ) -> str: '''simple docstring''' super().__init__(*lowerCamelCase_ , **lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Union[str, Any] = eval_examples SCREAMING_SNAKE_CASE : Any = post_process_function SCREAMING_SNAKE_CASE : Dict = quant_trainer_args SCREAMING_SNAKE_CASE : List[Any] = 1_28 # default number of calibration samples def __lowerCAmelCase ( self :str , lowerCamelCase_ :Dict=None ) -> Optional[Any]: '''simple docstring''' if calib_dataset is None and self.calib_dataset is None: raise ValueError('''Trainer: calibration requires an calib_dataset.''' ) SCREAMING_SNAKE_CASE : Union[str, Any] = calib_dataset if calib_dataset is not None else self.calib_dataset SCREAMING_SNAKE_CASE : Tuple = self._remove_unused_columns(lowerCamelCase_ , description='''Calibration''' ) return DataLoader( lowerCamelCase_ , batch_size=self.args.eval_batch_size , collate_fn=self.data_collator , drop_last=self.args.dataloader_drop_last , num_workers=self.args.dataloader_num_workers , pin_memory=self.args.dataloader_pin_memory , shuffle=lowerCamelCase_ , ) def __lowerCAmelCase ( self :Optional[int] , lowerCamelCase_ :int=None ) -> Tuple: '''simple docstring''' SCREAMING_SNAKE_CASE : str = self.train_dataset if calib_dataset is None else calib_dataset SCREAMING_SNAKE_CASE : List[Any] = self.get_calib_dataloader(lowerCamelCase_ ) SCREAMING_SNAKE_CASE : List[str] = self.model quant_trainer.configure_model(lowerCamelCase_ , self.quant_trainer_args , calib=lowerCamelCase_ ) model.eval() quant_trainer.enable_calibration(lowerCamelCase_ ) logger.info('''***** Running calibration *****''' ) logger.info(f" Num examples = {self.calib_num}" ) logger.info(f" Batch size = {calib_dataloader.batch_size}" ) for step, inputs in enumerate(lowerCamelCase_ ): # Prediction step SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : Dict = self.prediction_step(lowerCamelCase_ , lowerCamelCase_ , prediction_loss_only=lowerCamelCase_ ) if (step + 1) * calib_dataloader.batch_size >= self.calib_num: break quant_trainer.finish_calibration(lowerCamelCase_ , self.quant_trainer_args ) SCREAMING_SNAKE_CASE : Union[str, Any] = model def __lowerCAmelCase ( self :int , lowerCamelCase_ :Tuple=None , lowerCamelCase_ :str=None , lowerCamelCase_ :Any=None , lowerCamelCase_ :str = "eval" ) -> Optional[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : Any = self.eval_dataset if eval_dataset is None else eval_dataset SCREAMING_SNAKE_CASE : Tuple = self.get_eval_dataloader(lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Any = self.eval_examples if eval_examples is None else eval_examples # Temporarily disable metric computation, we will do it in the loop here. SCREAMING_SNAKE_CASE : Union[str, Any] = self.compute_metrics SCREAMING_SNAKE_CASE : List[str] = None SCREAMING_SNAKE_CASE : Any = self.prediction_loop if self.args.use_legacy_prediction_loop else self.evaluation_loop try: SCREAMING_SNAKE_CASE : Dict = eval_loop( lowerCamelCase_ , description='''Evaluation''' , prediction_loss_only=True if compute_metrics is None else None , ignore_keys=lowerCamelCase_ , ) finally: SCREAMING_SNAKE_CASE : Optional[int] = compute_metrics if self.post_process_function is not None and self.compute_metrics is not None: SCREAMING_SNAKE_CASE : Tuple = self.post_process_function(lowerCamelCase_ , lowerCamelCase_ , output.predictions ) SCREAMING_SNAKE_CASE : List[str] = self.compute_metrics(lowerCamelCase_ ) # Prefix all keys with metric_key_prefix + '_' for key in list(metrics.keys() ): if not key.startswith(f"{metric_key_prefix}_" ): SCREAMING_SNAKE_CASE : List[Any] = metrics.pop(lowerCamelCase_ ) self.log(lowerCamelCase_ ) else: SCREAMING_SNAKE_CASE : Optional[Any] = {} if self.args.tpu_metrics_debug or self.args.debug: # tpu-comment: Logging debug metrics for PyTorch/XLA (compile, execute times, ops, etc.) xm.master_print(met.metrics_report() ) SCREAMING_SNAKE_CASE : List[Any] = self.callback_handler.on_evaluate(self.args , self.state , self.control , lowerCamelCase_ ) return metrics def __lowerCAmelCase ( self :List[Any] , lowerCamelCase_ :Any , lowerCamelCase_ :Optional[Any] , lowerCamelCase_ :Any=None , lowerCamelCase_ :str = "test" ) -> List[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : List[Any] = self.get_test_dataloader(lowerCamelCase_ ) # Temporarily disable metric computation, we will do it in the loop here. SCREAMING_SNAKE_CASE : List[Any] = self.compute_metrics SCREAMING_SNAKE_CASE : int = None SCREAMING_SNAKE_CASE : str = self.prediction_loop if self.args.use_legacy_prediction_loop else self.evaluation_loop try: SCREAMING_SNAKE_CASE : int = eval_loop( lowerCamelCase_ , description='''Prediction''' , prediction_loss_only=True if compute_metrics is None else None , ignore_keys=lowerCamelCase_ , ) finally: SCREAMING_SNAKE_CASE : int = compute_metrics if self.post_process_function is None or self.compute_metrics is None: return output SCREAMING_SNAKE_CASE : int = self.post_process_function(lowerCamelCase_ , lowerCamelCase_ , output.predictions , '''predict''' ) SCREAMING_SNAKE_CASE : int = self.compute_metrics(lowerCamelCase_ ) # Prefix all keys with metric_key_prefix + '_' for key in list(metrics.keys() ): if not key.startswith(f"{metric_key_prefix}_" ): SCREAMING_SNAKE_CASE : Optional[Any] = metrics.pop(lowerCamelCase_ ) return PredictionOutput(predictions=predictions.predictions , label_ids=predictions.label_ids , metrics=lowerCamelCase_ ) def __lowerCAmelCase ( self :Optional[int] , lowerCamelCase_ :Any="./" ) -> Any: '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[int] = self.eval_dataset SCREAMING_SNAKE_CASE : List[Any] = self.get_eval_dataloader(lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Dict = next(iter(lowerCamelCase_ ) ) # saving device - to make it consistent SCREAMING_SNAKE_CASE : str = torch.device('''cuda''' if torch.cuda.is_available() else '''cpu''' ) # convert to tuple SCREAMING_SNAKE_CASE : Union[str, Any] = tuple(v.to(lowerCamelCase_ ) for k, v in batch.items() ) logger.info('''Converting model to be onnx compatible''' ) from pytorch_quantization.nn import TensorQuantizer SCREAMING_SNAKE_CASE : Dict = True SCREAMING_SNAKE_CASE : Any = self.model.to(lowerCamelCase_ ) model.eval() model.float() SCREAMING_SNAKE_CASE : Tuple = model.module if hasattr(lowerCamelCase_ , '''module''' ) else model quant_trainer.configure_model(lowerCamelCase_ , self.quant_trainer_args ) SCREAMING_SNAKE_CASE : Optional[int] = os.path.join(lowerCamelCase_ , '''model.onnx''' ) logger.info(f"exporting model to {output_model_file}" ) SCREAMING_SNAKE_CASE : Optional[int] = {0: '''batch_size''', 1: '''seq_len'''} torch.onnx.export( lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , export_params=lowerCamelCase_ , opset_version=13 , do_constant_folding=lowerCamelCase_ , input_names=['''input_ids''', '''attention_mask''', '''token_type_ids'''] , output_names=['''output_start_logits''', '''output_end_logits'''] , dynamic_axes={ '''input_ids''': axes, '''attention_mask''': axes, '''token_type_ids''': axes, '''output_start_logits''': axes, '''output_end_logits''': axes, } , verbose=lowerCamelCase_ , ) logger.info('''onnx export finished''' )
18
"""simple docstring""" import logging import os import random import sys from dataclasses import dataclass, field from typing import Optional import datasets import numpy as np import pandas as pd from datasets import load_dataset import transformers from transformers import ( AutoConfig, BartForSequenceClassification, DataCollatorWithPadding, EvalPrediction, HfArgumentParser, TapexTokenizer, Trainer, TrainingArguments, default_data_collator, set_seed, ) from transformers.trainer_utils import get_last_checkpoint from transformers.utils import check_min_version from transformers.utils.versions import require_version # Will error if the minimal version of Transformers is not installed. Remove at your own risks. check_min_version("4.17.0.dev0") require_version("datasets>=1.8.0", "To fix: pip install -r examples/pytorch/text-classification/requirements.txt") lowerCamelCase__ : Any = logging.getLogger(__name__) @dataclass class lowercase__: '''simple docstring''' UpperCamelCase = field( default="""tab_fact""" , metadata={"""help""": """The name of the dataset to use (via the datasets library)."""} ) UpperCamelCase = field( default="""tab_fact""" , metadata={"""help""": """The configuration name of the dataset to use (via the datasets library)."""} , ) UpperCamelCase = field( default=10_24 , metadata={ """help""": ( """The maximum total input sequence length after tokenization. Sequences longer """ """than this will be truncated, sequences shorter will be padded.""" ) } , ) UpperCamelCase = field( default=_UpperCAmelCase , metadata={"""help""": """Overwrite the cached preprocessed datasets or not."""} ) UpperCamelCase = field( default=_UpperCAmelCase , metadata={ """help""": ( """Whether to pad all samples to `max_seq_length`. """ """If False, will pad the samples dynamically when batching to the maximum length in the batch.""" ) } , ) UpperCamelCase = field( default=_UpperCAmelCase , metadata={ """help""": ( """For debugging purposes or quicker training, truncate the number of training examples to this """ """value if set.""" ) } , ) UpperCamelCase = field( default=_UpperCAmelCase , metadata={ """help""": ( """For debugging purposes or quicker training, truncate the number of evaluation examples to this """ """value if set.""" ) } , ) UpperCamelCase = field( default=_UpperCAmelCase , metadata={ """help""": ( """For debugging purposes or quicker training, truncate the number of prediction examples to this """ """value if set.""" ) } , ) UpperCamelCase = field( default=_UpperCAmelCase , metadata={"""help""": """A csv or a json file containing the training data."""} ) UpperCamelCase = field( default=_UpperCAmelCase , metadata={"""help""": """A csv or a json file containing the validation data."""} ) UpperCamelCase = field(default=_UpperCAmelCase , metadata={"""help""": """A csv or a json file containing the test data."""} ) def __lowerCAmelCase ( self :Union[str, Any] ) -> Dict: '''simple docstring''' if self.dataset_name is not None: pass elif self.train_file is None or self.validation_file is None: raise ValueError('''Need either a GLUE task, a training/validation file or a dataset name.''' ) else: SCREAMING_SNAKE_CASE : Union[str, Any] = self.train_file.split('''.''' )[-1] assert train_extension in ["csv", "json"], "`train_file` should be a csv or a json file." SCREAMING_SNAKE_CASE : Optional[int] = self.validation_file.split('''.''' )[-1] assert ( validation_extension == train_extension ), "`validation_file` should have the same extension (csv or json) as `train_file`." @dataclass class lowercase__: '''simple docstring''' UpperCamelCase = field( default=_UpperCAmelCase , metadata={"""help""": """Path to pretrained model or model identifier from huggingface.co/models"""} ) UpperCamelCase = field( default=_UpperCAmelCase , metadata={"""help""": """Pretrained config name or path if not the same as model_name"""} ) UpperCamelCase = field( default=_UpperCAmelCase , metadata={"""help""": """Pretrained tokenizer name or path if not the same as model_name"""} ) UpperCamelCase = field( default=_UpperCAmelCase , metadata={"""help""": """Where do you want to store the pretrained models downloaded from huggingface.co"""} , ) UpperCamelCase = field( default=_UpperCAmelCase , metadata={"""help""": """Whether to use one of the fast tokenizer (backed by the tokenizers library) or not."""} , ) UpperCamelCase = field( default="""main""" , metadata={"""help""": """The specific model version to use (can be a branch name, tag name or commit id)."""} , ) UpperCamelCase = field( default=_UpperCAmelCase , metadata={ """help""": ( """Will use the token generated when running `huggingface-cli login` (necessary to use this script """ """with private models).""" ) } , ) def __A ( )-> List[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : Union[str, Any] = 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 : Dict = parser.parse_json_file(json_file=os.path.abspath(sys.argv[1] ) ) else: SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : Dict = parser.parse_args_into_dataclasses() # 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 )] , ) SCREAMING_SNAKE_CASE : Union[str, Any] = training_args.get_process_log_level() logger.setLevel(a_ ) datasets.utils.logging.set_verbosity(a_ ) transformers.utils.logging.set_verbosity(a_ ) 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 : 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 ) # Get the datasets: you can either provide your own CSV/JSON training and evaluation files (see below) # or specify a GLUE benchmark task (the dataset will be downloaded automatically from the datasets Hub). # # For JSON files, this script will use the `question` column for the input question and `table` column for the corresponding table. # # If the CSVs/JSONs contain only one non-label column, the script does single sentence classification on this # single column. You can easily tweak this behavior (see below) # # In distributed training, the load_dataset function guarantee that only one local process can concurrently # download the dataset. if data_args.dataset_name is not None: # Downloading and loading a dataset from the hub. SCREAMING_SNAKE_CASE : List[str] = load_dataset( data_args.dataset_name , data_args.dataset_config_name , cache_dir=model_args.cache_dir ) else: # Loading a dataset from your local files. # CSV/JSON training and evaluation files are needed. SCREAMING_SNAKE_CASE : Any = {'''train''': data_args.train_file, '''validation''': data_args.validation_file} # Get the test dataset: you can provide your own CSV/JSON test file (see below) # when you use `do_predict` without specifying a GLUE benchmark task. if training_args.do_predict: if data_args.test_file is not None: SCREAMING_SNAKE_CASE : List[Any] = data_args.train_file.split('''.''' )[-1] SCREAMING_SNAKE_CASE : Optional[int] = data_args.test_file.split('''.''' )[-1] assert ( test_extension == train_extension ), "`test_file` should have the same extension (csv or json) as `train_file`." SCREAMING_SNAKE_CASE : str = data_args.test_file else: raise ValueError('''Need either a GLUE task or a test file for `do_predict`.''' ) for key in data_files.keys(): logger.info(F"load a local file for {key}: {data_files[key]}" ) if data_args.train_file.endswith('''.csv''' ): # Loading a dataset from local csv files SCREAMING_SNAKE_CASE : int = load_dataset('''csv''' , data_files=a_ , cache_dir=model_args.cache_dir ) else: # Loading a dataset from local json files SCREAMING_SNAKE_CASE : Tuple = load_dataset('''json''' , data_files=a_ , cache_dir=model_args.cache_dir ) # See more about loading any type of standard or custom dataset at # https://huggingface.co/docs/datasets/loading_datasets.html. # Labels SCREAMING_SNAKE_CASE : str = raw_datasets['''train'''].features['''label'''].names SCREAMING_SNAKE_CASE : Union[str, Any] = len(a_ ) # Load pretrained model and tokenizer # # In distributed training, the .from_pretrained methods guarantee that only one local process can concurrently # download model & vocab. SCREAMING_SNAKE_CASE : Optional[Any] = AutoConfig.from_pretrained( model_args.config_name if model_args.config_name else model_args.model_name_or_path , num_labels=a_ , cache_dir=model_args.cache_dir , revision=model_args.model_revision , use_auth_token=True if model_args.use_auth_token else None , ) # load tapex tokenizer SCREAMING_SNAKE_CASE : Dict = TapexTokenizer.from_pretrained( model_args.tokenizer_name if model_args.tokenizer_name else model_args.model_name_or_path , cache_dir=model_args.cache_dir , use_fast=model_args.use_fast_tokenizer , revision=model_args.model_revision , use_auth_token=True if model_args.use_auth_token else None , add_prefix_space=a_ , ) SCREAMING_SNAKE_CASE : List[Any] = BartForSequenceClassification.from_pretrained( model_args.model_name_or_path , from_tf=bool('''.ckpt''' in model_args.model_name_or_path ) , config=a_ , cache_dir=model_args.cache_dir , revision=model_args.model_revision , use_auth_token=True if model_args.use_auth_token else None , ) # Padding strategy if data_args.pad_to_max_length: SCREAMING_SNAKE_CASE : Tuple = '''max_length''' else: # We will pad later, dynamically at batch creation, to the max sequence length in each batch SCREAMING_SNAKE_CASE : Optional[Any] = False # Some models have set the order of the labels to use, so let's make sure we do use it. SCREAMING_SNAKE_CASE : Tuple = {'''Refused''': 0, '''Entailed''': 1} SCREAMING_SNAKE_CASE : List[Any] = {0: '''Refused''', 1: '''Entailed'''} if data_args.max_seq_length > tokenizer.model_max_length: logger.warning( F"The max_seq_length passed ({data_args.max_seq_length}) is larger than the maximum length for the" F"model ({tokenizer.model_max_length}). Using max_seq_length={tokenizer.model_max_length}." ) SCREAMING_SNAKE_CASE : Optional[int] = min(data_args.max_seq_length , tokenizer.model_max_length ) def preprocess_tabfact_function(a_ : str ): # Tokenize the texts def _convert_table_text_to_pandas(a_ : List[Any] ): SCREAMING_SNAKE_CASE : List[Any] = [_table_row.split('''#''' ) for _table_row in _table_text.strip('''\n''' ).split('''\n''' )] SCREAMING_SNAKE_CASE : Dict = pd.DataFrame.from_records(_table_content[1:] , columns=_table_content[0] ) return _table_pd SCREAMING_SNAKE_CASE : List[Any] = examples['''statement'''] SCREAMING_SNAKE_CASE : Optional[int] = list(map(_convert_table_text_to_pandas , examples['''table_text'''] ) ) SCREAMING_SNAKE_CASE : Any = tokenizer(a_ , a_ , padding=a_ , max_length=a_ , truncation=a_ ) SCREAMING_SNAKE_CASE : List[Any] = examples['''label'''] return result with training_args.main_process_first(desc='''dataset map pre-processing''' ): SCREAMING_SNAKE_CASE : Optional[Any] = raw_datasets.map( a_ , batched=a_ , load_from_cache_file=not data_args.overwrite_cache , desc='''Running tokenizer on dataset''' , ) if training_args.do_train: if "train" not in raw_datasets: raise ValueError('''--do_train requires a train dataset''' ) SCREAMING_SNAKE_CASE : List[str] = raw_datasets['''train'''] if data_args.max_train_samples is not None: SCREAMING_SNAKE_CASE : Any = train_dataset.select(range(data_args.max_train_samples ) ) if training_args.do_eval: if "validation" not in raw_datasets and "validation_matched" not in raw_datasets: raise ValueError('''--do_eval requires a validation dataset''' ) SCREAMING_SNAKE_CASE : List[str] = raw_datasets['''validation'''] if data_args.max_eval_samples is not None: SCREAMING_SNAKE_CASE : Union[str, Any] = eval_dataset.select(range(data_args.max_eval_samples ) ) if training_args.do_predict or data_args.test_file is not None: if "test" not in raw_datasets and "test_matched" not in raw_datasets: raise ValueError('''--do_predict requires a test dataset''' ) SCREAMING_SNAKE_CASE : Tuple = raw_datasets['''test'''] if data_args.max_predict_samples is not None: SCREAMING_SNAKE_CASE : Optional[int] = predict_dataset.select(range(data_args.max_predict_samples ) ) # Log a few random samples from the training set: if training_args.do_train: for index in random.sample(range(len(a_ ) ) , 3 ): logger.info(F"Sample {index} of the training set: {train_dataset[index]}." ) # You can define your custom 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(a_ : EvalPrediction ): SCREAMING_SNAKE_CASE : str = p.predictions[0] if isinstance(p.predictions , a_ ) else p.predictions SCREAMING_SNAKE_CASE : Tuple = np.argmax(a_ , axis=1 ) return {"accuracy": (preds == p.label_ids).astype(np.floataa ).mean().item()} # Data collator will default to DataCollatorWithPadding, so we change it if we already did the padding. if data_args.pad_to_max_length: SCREAMING_SNAKE_CASE : Tuple = default_data_collator elif training_args.fpaa: SCREAMING_SNAKE_CASE : Union[str, Any] = DataCollatorWithPadding(a_ , pad_to_multiple_of=8 ) else: SCREAMING_SNAKE_CASE : List[Any] = None # Initialize our Trainer SCREAMING_SNAKE_CASE : Optional[Any] = Trainer( model=a_ , args=a_ , train_dataset=train_dataset if training_args.do_train else None , eval_dataset=eval_dataset if training_args.do_eval else None , compute_metrics=a_ , tokenizer=a_ , data_collator=a_ , ) # Training if training_args.do_train: SCREAMING_SNAKE_CASE : List[str] = None if training_args.resume_from_checkpoint is not None: SCREAMING_SNAKE_CASE : Dict = training_args.resume_from_checkpoint elif last_checkpoint is not None: SCREAMING_SNAKE_CASE : str = last_checkpoint SCREAMING_SNAKE_CASE : str = trainer.train(resume_from_checkpoint=a_ ) SCREAMING_SNAKE_CASE : Optional[int] = train_result.metrics SCREAMING_SNAKE_CASE : int = ( data_args.max_train_samples if data_args.max_train_samples is not None else len(a_ ) ) SCREAMING_SNAKE_CASE : Optional[int] = min(a_ , len(a_ ) ) trainer.save_model() # Saves the tokenizer too for easy upload trainer.log_metrics('''train''' , a_ ) trainer.save_metrics('''train''' , a_ ) trainer.save_state() # Evaluation if training_args.do_eval: logger.info('''*** Evaluate ***''' ) SCREAMING_SNAKE_CASE : Tuple = trainer.evaluate(eval_dataset=a_ ) SCREAMING_SNAKE_CASE : str = data_args.max_eval_samples if data_args.max_eval_samples is not None else len(a_ ) SCREAMING_SNAKE_CASE : Union[str, Any] = min(a_ , len(a_ ) ) trainer.log_metrics('''eval''' , a_ ) trainer.save_metrics('''eval''' , a_ ) if training_args.do_predict: logger.info('''*** Predict ***''' ) # Removing the `label` columns because it contains -1 and Trainer won't like that. SCREAMING_SNAKE_CASE : Optional[Any] = predict_dataset.remove_columns('''label''' ) SCREAMING_SNAKE_CASE : Optional[Any] = trainer.predict(a_ , metric_key_prefix='''predict''' ).predictions SCREAMING_SNAKE_CASE : Union[str, Any] = np.argmax(a_ , axis=1 ) SCREAMING_SNAKE_CASE : Optional[int] = os.path.join(training_args.output_dir , '''predict_results_tabfact.txt''' ) if trainer.is_world_process_zero(): with open(a_ , '''w''' ) as writer: logger.info('''***** Predict Results *****''' ) writer.write('''index\tprediction\n''' ) for index, item in enumerate(a_ ): SCREAMING_SNAKE_CASE : Union[str, Any] = label_list[item] writer.write(F"{index}\t{item}\n" ) SCREAMING_SNAKE_CASE : Optional[int] = {'''finetuned_from''': model_args.model_name_or_path, '''tasks''': '''text-classification'''} if training_args.push_to_hub: trainer.push_to_hub(**a_ ) else: trainer.create_model_card(**a_ ) def __A ( a_ : List[str] )-> int: '''simple docstring''' main() if __name__ == "__main__": main()
18
1
"""simple docstring""" from __future__ import annotations class lowercase__: '''simple docstring''' def __init__( self :Dict , lowerCamelCase_ :int ) -> None: '''simple docstring''' SCREAMING_SNAKE_CASE : int = data SCREAMING_SNAKE_CASE : Node | None = None SCREAMING_SNAKE_CASE : Node | None = None def __A ( a_ : Node | None )-> None: # In Order traversal of the tree '''simple docstring''' if tree: display(tree.left ) print(tree.data ) display(tree.right ) def __A ( a_ : Node | None )-> int: '''simple docstring''' return 1 + max(depth_of_tree(tree.left ) , depth_of_tree(tree.right ) ) if tree else 0 def __A ( a_ : Node )-> bool: '''simple docstring''' if not tree: return True if tree.left and tree.right: return is_full_binary_tree(tree.left ) and is_full_binary_tree(tree.right ) else: return not tree.left and not tree.right def __A ( )-> None: # Main function for testing. '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[int] = Node(1 ) SCREAMING_SNAKE_CASE : Optional[Any] = Node(2 ) SCREAMING_SNAKE_CASE : List[Any] = Node(3 ) SCREAMING_SNAKE_CASE : int = Node(4 ) SCREAMING_SNAKE_CASE : str = Node(5 ) SCREAMING_SNAKE_CASE : List[Any] = Node(6 ) SCREAMING_SNAKE_CASE : Any = Node(7 ) SCREAMING_SNAKE_CASE : List[str] = Node(8 ) SCREAMING_SNAKE_CASE : Dict = Node(9 ) print(is_full_binary_tree(a_ ) ) print(depth_of_tree(a_ ) ) print('''Tree is: ''' ) display(a_ ) if __name__ == "__main__": main()
18
"""simple docstring""" import collections import inspect import unittest from transformers import SwinvaConfig from transformers.testing_utils import require_torch, require_vision, slow, torch_device from transformers.utils import cached_property, is_torch_available, is_vision_available from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, _config_zero_init, floats_tensor, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from torch import nn from transformers import SwinvaForImageClassification, SwinvaForMaskedImageModeling, SwinvaModel from transformers.models.swinva.modeling_swinva import SWINV2_PRETRAINED_MODEL_ARCHIVE_LIST if is_vision_available(): from PIL import Image from transformers import AutoImageProcessor class lowercase__: '''simple docstring''' def __init__( self :str , lowerCamelCase_ :List[Any] , lowerCamelCase_ :Any=13 , lowerCamelCase_ :Any=32 , lowerCamelCase_ :Union[str, Any]=2 , lowerCamelCase_ :Any=3 , lowerCamelCase_ :Union[str, Any]=16 , lowerCamelCase_ :int=[1, 2, 1] , lowerCamelCase_ :str=[2, 2, 4] , lowerCamelCase_ :str=2 , lowerCamelCase_ :Tuple=2.0 , lowerCamelCase_ :Union[str, Any]=True , lowerCamelCase_ :str=0.0 , lowerCamelCase_ :Optional[int]=0.0 , lowerCamelCase_ :Dict=0.1 , lowerCamelCase_ :Union[str, Any]="gelu" , lowerCamelCase_ :str=False , lowerCamelCase_ :List[Any]=True , lowerCamelCase_ :int=0.0_2 , lowerCamelCase_ :List[Any]=1E-5 , lowerCamelCase_ :int=True , lowerCamelCase_ :str=None , lowerCamelCase_ :List[Any]=True , lowerCamelCase_ :Union[str, Any]=10 , lowerCamelCase_ :List[Any]=8 , ) -> int: '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[Any] = parent SCREAMING_SNAKE_CASE : int = batch_size SCREAMING_SNAKE_CASE : int = image_size SCREAMING_SNAKE_CASE : Union[str, Any] = patch_size SCREAMING_SNAKE_CASE : Tuple = num_channels SCREAMING_SNAKE_CASE : Any = embed_dim SCREAMING_SNAKE_CASE : int = depths SCREAMING_SNAKE_CASE : List[str] = num_heads SCREAMING_SNAKE_CASE : Union[str, Any] = window_size SCREAMING_SNAKE_CASE : Optional[Any] = mlp_ratio SCREAMING_SNAKE_CASE : List[Any] = qkv_bias SCREAMING_SNAKE_CASE : Union[str, Any] = hidden_dropout_prob SCREAMING_SNAKE_CASE : Dict = attention_probs_dropout_prob SCREAMING_SNAKE_CASE : List[str] = drop_path_rate SCREAMING_SNAKE_CASE : List[Any] = hidden_act SCREAMING_SNAKE_CASE : Optional[Any] = use_absolute_embeddings SCREAMING_SNAKE_CASE : Any = patch_norm SCREAMING_SNAKE_CASE : Union[str, Any] = layer_norm_eps SCREAMING_SNAKE_CASE : List[Any] = initializer_range SCREAMING_SNAKE_CASE : Any = is_training SCREAMING_SNAKE_CASE : List[Any] = scope SCREAMING_SNAKE_CASE : Optional[Any] = use_labels SCREAMING_SNAKE_CASE : Optional[Any] = type_sequence_label_size SCREAMING_SNAKE_CASE : Optional[Any] = encoder_stride def __lowerCAmelCase ( self :Optional[int] ) -> List[str]: '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[int] = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) SCREAMING_SNAKE_CASE : int = None if self.use_labels: SCREAMING_SNAKE_CASE : Dict = ids_tensor([self.batch_size] , self.type_sequence_label_size ) SCREAMING_SNAKE_CASE : Tuple = self.get_config() return config, pixel_values, labels def __lowerCAmelCase ( self :int ) -> int: '''simple docstring''' return SwinvaConfig( image_size=self.image_size , patch_size=self.patch_size , num_channels=self.num_channels , embed_dim=self.embed_dim , depths=self.depths , num_heads=self.num_heads , window_size=self.window_size , mlp_ratio=self.mlp_ratio , qkv_bias=self.qkv_bias , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , drop_path_rate=self.drop_path_rate , hidden_act=self.hidden_act , use_absolute_embeddings=self.use_absolute_embeddings , path_norm=self.patch_norm , layer_norm_eps=self.layer_norm_eps , initializer_range=self.initializer_range , encoder_stride=self.encoder_stride , ) def __lowerCAmelCase ( self :List[Any] , lowerCamelCase_ :int , lowerCamelCase_ :Optional[int] , lowerCamelCase_ :Optional[Any] ) -> Union[str, Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : str = SwinvaModel(config=lowerCamelCase_ ) model.to(lowerCamelCase_ ) model.eval() SCREAMING_SNAKE_CASE : int = model(lowerCamelCase_ ) SCREAMING_SNAKE_CASE : List[Any] = ((config.image_size // config.patch_size) ** 2) // (4 ** (len(config.depths ) - 1)) SCREAMING_SNAKE_CASE : Dict = int(config.embed_dim * 2 ** (len(config.depths ) - 1) ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, expected_seq_len, expected_dim) ) def __lowerCAmelCase ( self :str , lowerCamelCase_ :str , lowerCamelCase_ :Tuple , lowerCamelCase_ :Any ) -> Union[str, Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : List[str] = SwinvaForMaskedImageModeling(config=lowerCamelCase_ ) model.to(lowerCamelCase_ ) model.eval() SCREAMING_SNAKE_CASE : Dict = model(lowerCamelCase_ ) self.parent.assertEqual( result.logits.shape , (self.batch_size, self.num_channels, self.image_size, self.image_size) ) # test greyscale images SCREAMING_SNAKE_CASE : Tuple = 1 SCREAMING_SNAKE_CASE : List[Any] = SwinvaForMaskedImageModeling(lowerCamelCase_ ) model.to(lowerCamelCase_ ) model.eval() SCREAMING_SNAKE_CASE : List[str] = floats_tensor([self.batch_size, 1, self.image_size, self.image_size] ) SCREAMING_SNAKE_CASE : Tuple = model(lowerCamelCase_ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, 1, self.image_size, self.image_size) ) def __lowerCAmelCase ( self :Tuple , lowerCamelCase_ :Union[str, Any] , lowerCamelCase_ :Any , lowerCamelCase_ :int ) -> Tuple: '''simple docstring''' SCREAMING_SNAKE_CASE : Tuple = self.type_sequence_label_size SCREAMING_SNAKE_CASE : Optional[Any] = SwinvaForImageClassification(lowerCamelCase_ ) model.to(lowerCamelCase_ ) model.eval() SCREAMING_SNAKE_CASE : Optional[int] = model(lowerCamelCase_ , labels=lowerCamelCase_ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.type_sequence_label_size) ) def __lowerCAmelCase ( self :List[Any] ) -> List[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : List[str] = self.prepare_config_and_inputs() SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : Optional[int] = config_and_inputs SCREAMING_SNAKE_CASE : int = {'''pixel_values''': pixel_values} return config, inputs_dict @require_torch class lowercase__( _UpperCAmelCase , _UpperCAmelCase , unittest.TestCase ): '''simple docstring''' UpperCamelCase = ( (SwinvaModel, SwinvaForImageClassification, SwinvaForMaskedImageModeling) if is_torch_available() else () ) UpperCamelCase = ( {"""feature-extraction""": SwinvaModel, """image-classification""": SwinvaForImageClassification} if is_torch_available() else {} ) UpperCamelCase = False UpperCamelCase = False UpperCamelCase = False UpperCamelCase = False def __lowerCAmelCase ( self :Union[str, Any] ) -> Union[str, Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : int = SwinvaModelTester(self ) SCREAMING_SNAKE_CASE : Dict = ConfigTester(self , config_class=lowerCamelCase_ , embed_dim=37 ) def __lowerCAmelCase ( self :Dict ) -> List[str]: '''simple docstring''' self.config_tester.create_and_test_config_to_json_string() self.config_tester.create_and_test_config_to_json_file() self.config_tester.create_and_test_config_from_and_save_pretrained() self.config_tester.create_and_test_config_with_num_labels() self.config_tester.check_config_can_be_init_without_params() self.config_tester.check_config_arguments_init() def __lowerCAmelCase ( self :List[Any] ) -> List[str]: '''simple docstring''' SCREAMING_SNAKE_CASE : Union[str, Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*lowerCamelCase_ ) @unittest.skip(reason='''Got `CUDA error: misaligned address` with PyTorch 2.0.0.''' ) def __lowerCAmelCase ( self :str ) -> List[Any]: '''simple docstring''' pass @unittest.skip(reason='''Swinv2 does not use inputs_embeds''' ) def __lowerCAmelCase ( self :List[Any] ) -> Any: '''simple docstring''' pass def __lowerCAmelCase ( self :int ) -> Union[str, Any]: '''simple docstring''' SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : Optional[int] = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: SCREAMING_SNAKE_CASE : List[Any] = model_class(lowerCamelCase_ ) self.assertIsInstance(model.get_input_embeddings() , (nn.Module) ) SCREAMING_SNAKE_CASE : str = model.get_output_embeddings() self.assertTrue(x is None or isinstance(lowerCamelCase_ , nn.Linear ) ) def __lowerCAmelCase ( self :int ) -> List[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : Tuple = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: SCREAMING_SNAKE_CASE : Optional[Any] = model_class(lowerCamelCase_ ) SCREAMING_SNAKE_CASE : List[str] = inspect.signature(model.forward ) # signature.parameters is an OrderedDict => so arg_names order is deterministic SCREAMING_SNAKE_CASE : Optional[int] = [*signature.parameters.keys()] SCREAMING_SNAKE_CASE : Union[str, Any] = ['''pixel_values'''] self.assertListEqual(arg_names[:1] , lowerCamelCase_ ) def __lowerCAmelCase ( self :int ) -> Optional[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : str = self.model_tester.prepare_config_and_inputs_for_common() SCREAMING_SNAKE_CASE : Tuple = True for model_class in self.all_model_classes: SCREAMING_SNAKE_CASE : List[str] = True SCREAMING_SNAKE_CASE : List[Any] = False SCREAMING_SNAKE_CASE : Any = True SCREAMING_SNAKE_CASE : Any = model_class(lowerCamelCase_ ) model.to(lowerCamelCase_ ) model.eval() with torch.no_grad(): SCREAMING_SNAKE_CASE : str = model(**self._prepare_for_class(lowerCamelCase_ , lowerCamelCase_ ) ) SCREAMING_SNAKE_CASE : Tuple = outputs.attentions SCREAMING_SNAKE_CASE : Tuple = len(self.model_tester.depths ) self.assertEqual(len(lowerCamelCase_ ) , lowerCamelCase_ ) # check that output_attentions also work using config del inputs_dict["output_attentions"] SCREAMING_SNAKE_CASE : Union[str, Any] = True SCREAMING_SNAKE_CASE : Optional[int] = config.window_size**2 SCREAMING_SNAKE_CASE : str = model_class(lowerCamelCase_ ) model.to(lowerCamelCase_ ) model.eval() with torch.no_grad(): SCREAMING_SNAKE_CASE : Union[str, Any] = model(**self._prepare_for_class(lowerCamelCase_ , lowerCamelCase_ ) ) SCREAMING_SNAKE_CASE : Dict = outputs.attentions self.assertEqual(len(lowerCamelCase_ ) , lowerCamelCase_ ) self.assertListEqual( list(attentions[0].shape[-3:] ) , [self.model_tester.num_heads[0], window_size_squared, window_size_squared] , ) SCREAMING_SNAKE_CASE : Dict = len(lowerCamelCase_ ) # Check attention is always last and order is fine SCREAMING_SNAKE_CASE : int = True SCREAMING_SNAKE_CASE : Dict = True SCREAMING_SNAKE_CASE : int = model_class(lowerCamelCase_ ) model.to(lowerCamelCase_ ) model.eval() with torch.no_grad(): SCREAMING_SNAKE_CASE : List[Any] = model(**self._prepare_for_class(lowerCamelCase_ , lowerCamelCase_ ) ) if hasattr(self.model_tester , '''num_hidden_states_types''' ): SCREAMING_SNAKE_CASE : Any = self.model_tester.num_hidden_states_types else: # also another +1 for reshaped_hidden_states SCREAMING_SNAKE_CASE : Optional[Any] = 2 self.assertEqual(out_len + added_hidden_states , len(lowerCamelCase_ ) ) SCREAMING_SNAKE_CASE : Tuple = outputs.attentions self.assertEqual(len(lowerCamelCase_ ) , lowerCamelCase_ ) self.assertListEqual( list(self_attentions[0].shape[-3:] ) , [self.model_tester.num_heads[0], window_size_squared, window_size_squared] , ) def __lowerCAmelCase ( self :Dict , lowerCamelCase_ :int , lowerCamelCase_ :Any , lowerCamelCase_ :Tuple , lowerCamelCase_ :Any ) -> Optional[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : Union[str, Any] = model_class(lowerCamelCase_ ) model.to(lowerCamelCase_ ) model.eval() with torch.no_grad(): SCREAMING_SNAKE_CASE : Optional[int] = model(**self._prepare_for_class(lowerCamelCase_ , lowerCamelCase_ ) ) SCREAMING_SNAKE_CASE : Dict = outputs.hidden_states SCREAMING_SNAKE_CASE : str = getattr( self.model_tester , '''expected_num_hidden_layers''' , len(self.model_tester.depths ) + 1 ) self.assertEqual(len(lowerCamelCase_ ) , lowerCamelCase_ ) # Swinv2 has a different seq_length SCREAMING_SNAKE_CASE : List[str] = ( config.patch_size if isinstance(config.patch_size , collections.abc.Iterable ) else (config.patch_size, config.patch_size) ) SCREAMING_SNAKE_CASE : Dict = (image_size[1] // patch_size[1]) * (image_size[0] // patch_size[0]) self.assertListEqual( list(hidden_states[0].shape[-2:] ) , [num_patches, self.model_tester.embed_dim] , ) SCREAMING_SNAKE_CASE : Any = outputs.reshaped_hidden_states self.assertEqual(len(lowerCamelCase_ ) , lowerCamelCase_ ) SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : Any = reshaped_hidden_states[0].shape SCREAMING_SNAKE_CASE : Optional[int] = ( reshaped_hidden_states[0].view(lowerCamelCase_ , lowerCamelCase_ , height * width ).permute(0 , 2 , 1 ) ) self.assertListEqual( list(reshaped_hidden_states.shape[-2:] ) , [num_patches, self.model_tester.embed_dim] , ) def __lowerCAmelCase ( self :Optional[Any] ) -> Optional[int]: '''simple docstring''' SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : Union[str, Any] = self.model_tester.prepare_config_and_inputs_for_common() SCREAMING_SNAKE_CASE : List[str] = ( self.model_tester.image_size if isinstance(self.model_tester.image_size , collections.abc.Iterable ) else (self.model_tester.image_size, self.model_tester.image_size) ) for model_class in self.all_model_classes: SCREAMING_SNAKE_CASE : Tuple = True self.check_hidden_states_output(lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ ) # check that output_hidden_states also work using config del inputs_dict["output_hidden_states"] SCREAMING_SNAKE_CASE : List[str] = True self.check_hidden_states_output(lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ ) def __lowerCAmelCase ( self :str ) -> Optional[int]: '''simple docstring''' SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : str = self.model_tester.prepare_config_and_inputs_for_common() SCREAMING_SNAKE_CASE : List[Any] = 3 SCREAMING_SNAKE_CASE : int = ( self.model_tester.image_size if isinstance(self.model_tester.image_size , collections.abc.Iterable ) else (self.model_tester.image_size, self.model_tester.image_size) ) SCREAMING_SNAKE_CASE : Optional[Any] = ( config.patch_size if isinstance(config.patch_size , collections.abc.Iterable ) else (config.patch_size, config.patch_size) ) SCREAMING_SNAKE_CASE : Optional[int] = image_size[0] + patch_size[0] - (image_size[0] % patch_size[0]) SCREAMING_SNAKE_CASE : Optional[int] = image_size[1] + patch_size[1] - (image_size[1] % patch_size[1]) for model_class in self.all_model_classes: SCREAMING_SNAKE_CASE : Optional[Any] = True self.check_hidden_states_output(lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , (padded_height, padded_width) ) # check that output_hidden_states also work using config del inputs_dict["output_hidden_states"] SCREAMING_SNAKE_CASE : Optional[Any] = True self.check_hidden_states_output(lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , (padded_height, padded_width) ) def __lowerCAmelCase ( self :str ) -> List[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : Tuple = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_masked_image_modeling(*lowerCamelCase_ ) def __lowerCAmelCase ( self :Tuple ) -> Optional[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : Tuple = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_image_classification(*lowerCamelCase_ ) @slow def __lowerCAmelCase ( self :Tuple ) -> List[str]: '''simple docstring''' for model_name in SWINV2_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: SCREAMING_SNAKE_CASE : Tuple = SwinvaModel.from_pretrained(lowerCamelCase_ ) self.assertIsNotNone(lowerCamelCase_ ) def __lowerCAmelCase ( self :int ) -> Any: '''simple docstring''' SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : List[str] = self.model_tester.prepare_config_and_inputs_for_common() SCREAMING_SNAKE_CASE : Tuple = _config_zero_init(lowerCamelCase_ ) for model_class in self.all_model_classes: SCREAMING_SNAKE_CASE : Dict = model_class(config=lowerCamelCase_ ) for name, param in model.named_parameters(): if "embeddings" not in name and "logit_scale" not in name and param.requires_grad: self.assertIn( ((param.data.mean() * 1E9).round() / 1E9).item() , [0.0, 1.0] , msg=f"Parameter {name} of model {model_class} seems not properly initialized" , ) @require_vision @require_torch class lowercase__( unittest.TestCase ): '''simple docstring''' @cached_property def __lowerCAmelCase ( self :Dict ) -> List[Any]: '''simple docstring''' return ( AutoImageProcessor.from_pretrained('''microsoft/swinv2-tiny-patch4-window8-256''' ) if is_vision_available() else None ) @slow def __lowerCAmelCase ( self :Dict ) -> Tuple: '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[int] = SwinvaForImageClassification.from_pretrained('''microsoft/swinv2-tiny-patch4-window8-256''' ).to( lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Dict = self.default_image_processor SCREAMING_SNAKE_CASE : Optional[Any] = Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png''' ) SCREAMING_SNAKE_CASE : List[str] = image_processor(images=lowerCamelCase_ , return_tensors='''pt''' ).to(lowerCamelCase_ ) # forward pass with torch.no_grad(): SCREAMING_SNAKE_CASE : Tuple = model(**lowerCamelCase_ ) # verify the logits SCREAMING_SNAKE_CASE : List[str] = torch.Size((1, 10_00) ) self.assertEqual(outputs.logits.shape , lowerCamelCase_ ) SCREAMING_SNAKE_CASE : int = torch.tensor([-0.3_9_4_7, -0.4_3_0_6, 0.0_0_2_6] ).to(lowerCamelCase_ ) self.assertTrue(torch.allclose(outputs.logits[0, :3] , lowerCamelCase_ , atol=1E-4 ) )
18
1
"""simple docstring""" import warnings from ...utils import logging from .image_processing_glpn import GLPNImageProcessor lowerCamelCase__ : Optional[Any] = logging.get_logger(__name__) class lowercase__( _UpperCAmelCase ): '''simple docstring''' def __init__( self :List[str] , *lowerCamelCase_ :Any , **lowerCamelCase_ :Union[str, Any] ) -> None: '''simple docstring''' warnings.warn( '''The class GLPNFeatureExtractor is deprecated and will be removed in version 5 of Transformers. Please''' ''' use GLPNImageProcessor instead.''' , lowerCamelCase_ , ) super().__init__(*lowerCamelCase_ , **lowerCamelCase_ )
18
"""simple docstring""" from collections import OrderedDict from typing import Any, Mapping, Optional from ... import PreTrainedTokenizer from ...configuration_utils import PretrainedConfig from ...file_utils import TensorType, is_torch_available from ...onnx import OnnxConfig, OnnxConfigWithPast, OnnxSeqaSeqConfigWithPast from ...onnx.utils import compute_effective_axis_dimension from ...utils import logging lowerCamelCase__ : Tuple = logging.get_logger(__name__) lowerCamelCase__ : str = { "facebook/blenderbot_small-90M": "https://huggingface.co/facebook/blenderbot_small-90M/resolve/main/config.json", # See all BlenderbotSmall models at https://huggingface.co/models?filter=blenderbot_small } class lowercase__( _UpperCAmelCase ): '''simple docstring''' UpperCamelCase = """blenderbot-small""" UpperCamelCase = ["""past_key_values"""] UpperCamelCase = {"""num_attention_heads""": """encoder_attention_heads""", """hidden_size""": """d_model"""} def __init__( self :Any , lowerCamelCase_ :Dict=5_02_65 , lowerCamelCase_ :str=5_12 , lowerCamelCase_ :Tuple=8 , lowerCamelCase_ :int=20_48 , lowerCamelCase_ :str=16 , lowerCamelCase_ :Optional[int]=8 , lowerCamelCase_ :str=20_48 , lowerCamelCase_ :Optional[Any]=16 , lowerCamelCase_ :Union[str, Any]=0.0 , lowerCamelCase_ :List[str]=0.0 , lowerCamelCase_ :Tuple=True , lowerCamelCase_ :Tuple=True , lowerCamelCase_ :int="gelu" , lowerCamelCase_ :Tuple=5_12 , lowerCamelCase_ :Optional[int]=0.1 , lowerCamelCase_ :int=0.0 , lowerCamelCase_ :Tuple=0.0 , lowerCamelCase_ :Optional[int]=0.0_2 , lowerCamelCase_ :Union[str, Any]=1 , lowerCamelCase_ :Dict=False , lowerCamelCase_ :Optional[int]=0 , lowerCamelCase_ :List[Any]=1 , lowerCamelCase_ :Any=2 , lowerCamelCase_ :Optional[Any]=2 , **lowerCamelCase_ :Dict , ) -> Any: '''simple docstring''' SCREAMING_SNAKE_CASE : int = vocab_size SCREAMING_SNAKE_CASE : List[str] = max_position_embeddings SCREAMING_SNAKE_CASE : Optional[Any] = d_model SCREAMING_SNAKE_CASE : Dict = encoder_ffn_dim SCREAMING_SNAKE_CASE : Tuple = encoder_layers SCREAMING_SNAKE_CASE : Dict = encoder_attention_heads SCREAMING_SNAKE_CASE : Any = decoder_ffn_dim SCREAMING_SNAKE_CASE : str = decoder_layers SCREAMING_SNAKE_CASE : str = decoder_attention_heads SCREAMING_SNAKE_CASE : List[Any] = dropout SCREAMING_SNAKE_CASE : Optional[Any] = attention_dropout SCREAMING_SNAKE_CASE : Any = activation_dropout SCREAMING_SNAKE_CASE : List[str] = activation_function SCREAMING_SNAKE_CASE : Optional[int] = init_std SCREAMING_SNAKE_CASE : List[Any] = encoder_layerdrop SCREAMING_SNAKE_CASE : Union[str, Any] = decoder_layerdrop SCREAMING_SNAKE_CASE : List[Any] = use_cache SCREAMING_SNAKE_CASE : Union[str, Any] = encoder_layers SCREAMING_SNAKE_CASE : List[Any] = scale_embedding # scale factor will be sqrt(d_model) if True super().__init__( pad_token_id=lowerCamelCase_ , bos_token_id=lowerCamelCase_ , eos_token_id=lowerCamelCase_ , is_encoder_decoder=lowerCamelCase_ , decoder_start_token_id=lowerCamelCase_ , forced_eos_token_id=lowerCamelCase_ , **lowerCamelCase_ , ) class lowercase__( _UpperCAmelCase ): '''simple docstring''' @property def __lowerCAmelCase ( self :Any ) -> Mapping[str, Mapping[int, str]]: '''simple docstring''' if self.task in ["default", "seq2seq-lm"]: SCREAMING_SNAKE_CASE : Tuple = OrderedDict( [ ('''input_ids''', {0: '''batch''', 1: '''encoder_sequence'''}), ('''attention_mask''', {0: '''batch''', 1: '''encoder_sequence'''}), ] ) if self.use_past: SCREAMING_SNAKE_CASE : Union[str, Any] = {0: '''batch'''} SCREAMING_SNAKE_CASE : List[Any] = {0: '''batch''', 1: '''past_decoder_sequence + sequence'''} else: SCREAMING_SNAKE_CASE : Optional[Any] = {0: '''batch''', 1: '''decoder_sequence'''} SCREAMING_SNAKE_CASE : Union[str, Any] = {0: '''batch''', 1: '''decoder_sequence'''} if self.use_past: self.fill_with_past_key_values_(lowerCamelCase_ , direction='''inputs''' ) elif self.task == "causal-lm": # TODO: figure this case out. SCREAMING_SNAKE_CASE : Tuple = OrderedDict( [ ('''input_ids''', {0: '''batch''', 1: '''encoder_sequence'''}), ('''attention_mask''', {0: '''batch''', 1: '''encoder_sequence'''}), ] ) if self.use_past: SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : List[Any] = self.num_layers for i in range(lowerCamelCase_ ): SCREAMING_SNAKE_CASE : int = {0: '''batch''', 2: '''past_sequence + sequence'''} SCREAMING_SNAKE_CASE : List[str] = {0: '''batch''', 2: '''past_sequence + sequence'''} else: SCREAMING_SNAKE_CASE : Any = OrderedDict( [ ('''input_ids''', {0: '''batch''', 1: '''encoder_sequence'''}), ('''attention_mask''', {0: '''batch''', 1: '''encoder_sequence'''}), ('''decoder_input_ids''', {0: '''batch''', 1: '''decoder_sequence'''}), ('''decoder_attention_mask''', {0: '''batch''', 1: '''decoder_sequence'''}), ] ) return common_inputs @property def __lowerCAmelCase ( self :List[str] ) -> Mapping[str, Mapping[int, str]]: '''simple docstring''' if self.task in ["default", "seq2seq-lm"]: SCREAMING_SNAKE_CASE : Any = super().outputs else: SCREAMING_SNAKE_CASE : Tuple = super(lowerCamelCase_ , self ).outputs if self.use_past: SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : Union[str, Any] = self.num_layers for i in range(lowerCamelCase_ ): SCREAMING_SNAKE_CASE : Union[str, Any] = {0: '''batch''', 2: '''past_sequence + sequence'''} SCREAMING_SNAKE_CASE : str = {0: '''batch''', 2: '''past_sequence + sequence'''} return common_outputs def __lowerCAmelCase ( self :int , lowerCamelCase_ :PreTrainedTokenizer , lowerCamelCase_ :int = -1 , lowerCamelCase_ :int = -1 , lowerCamelCase_ :bool = False , lowerCamelCase_ :Optional[TensorType] = None , ) -> Mapping[str, Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : Dict = self._generate_dummy_inputs_for_sequence_classification_and_question_answering( lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ ) # Generate decoder inputs SCREAMING_SNAKE_CASE : Optional[int] = seq_length if not self.use_past else 1 SCREAMING_SNAKE_CASE : Optional[int] = self._generate_dummy_inputs_for_sequence_classification_and_question_answering( lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ ) SCREAMING_SNAKE_CASE : List[Any] = {f"decoder_{name}": tensor for name, tensor in decoder_inputs.items()} SCREAMING_SNAKE_CASE : str = dict(**lowerCamelCase_ , **lowerCamelCase_ ) if self.use_past: if not is_torch_available(): raise ValueError('''Cannot generate dummy past_keys inputs without PyTorch installed.''' ) else: import torch SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : Any = common_inputs['''input_ids'''].shape SCREAMING_SNAKE_CASE : str = common_inputs['''decoder_input_ids'''].shape[1] SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : Any = self.num_attention_heads SCREAMING_SNAKE_CASE : str = ( batch, num_encoder_attention_heads, encoder_seq_length, self._config.hidden_size // num_encoder_attention_heads, ) SCREAMING_SNAKE_CASE : Optional[Any] = decoder_seq_length + 3 SCREAMING_SNAKE_CASE : int = ( batch, num_decoder_attention_heads, decoder_past_length, self._config.hidden_size // num_decoder_attention_heads, ) SCREAMING_SNAKE_CASE : List[Any] = torch.cat( [common_inputs['''decoder_attention_mask'''], torch.ones(lowerCamelCase_ , lowerCamelCase_ )] , dim=1 ) SCREAMING_SNAKE_CASE : Optional[int] = [] # If the number of encoder and decoder layers are present in the model configuration, both are considered SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : Optional[int] = self.num_layers SCREAMING_SNAKE_CASE : int = min(lowerCamelCase_ , lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Optional[int] = max(lowerCamelCase_ , lowerCamelCase_ ) - min_num_layers SCREAMING_SNAKE_CASE : Tuple = '''encoder''' if num_encoder_layers > num_decoder_layers else '''decoder''' for _ in range(lowerCamelCase_ ): common_inputs["past_key_values"].append( ( torch.zeros(lowerCamelCase_ ), torch.zeros(lowerCamelCase_ ), torch.zeros(lowerCamelCase_ ), torch.zeros(lowerCamelCase_ ), ) ) # TODO: test this. SCREAMING_SNAKE_CASE : int = encoder_shape if remaining_side_name == '''encoder''' else decoder_shape for _ in range(lowerCamelCase_ , lowerCamelCase_ ): common_inputs["past_key_values"].append((torch.zeros(lowerCamelCase_ ), torch.zeros(lowerCamelCase_ )) ) return common_inputs def __lowerCAmelCase ( self :Any , lowerCamelCase_ :PreTrainedTokenizer , lowerCamelCase_ :int = -1 , lowerCamelCase_ :int = -1 , lowerCamelCase_ :bool = False , lowerCamelCase_ :Optional[TensorType] = None , ) -> Mapping[str, Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : Any = self._generate_dummy_inputs_for_sequence_classification_and_question_answering( lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ ) if self.use_past: if not is_torch_available(): raise ValueError('''Cannot generate dummy past_keys inputs without PyTorch installed.''' ) else: import torch SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : Tuple = common_inputs['''input_ids'''].shape # Not using the same length for past_key_values SCREAMING_SNAKE_CASE : List[str] = seqlen + 2 SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : Dict = self.num_layers SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : List[Any] = self.num_attention_heads SCREAMING_SNAKE_CASE : Union[str, Any] = ( batch, num_encoder_attention_heads, past_key_values_length, self._config.hidden_size // num_encoder_attention_heads, ) SCREAMING_SNAKE_CASE : Tuple = common_inputs['''attention_mask'''].dtype SCREAMING_SNAKE_CASE : Any = torch.cat( [common_inputs['''attention_mask'''], torch.ones(lowerCamelCase_ , lowerCamelCase_ , dtype=lowerCamelCase_ )] , dim=1 ) SCREAMING_SNAKE_CASE : Optional[int] = [ (torch.zeros(lowerCamelCase_ ), torch.zeros(lowerCamelCase_ )) for _ in range(lowerCamelCase_ ) ] return common_inputs def __lowerCAmelCase ( self :Union[str, Any] , lowerCamelCase_ :PreTrainedTokenizer , lowerCamelCase_ :int = -1 , lowerCamelCase_ :int = -1 , lowerCamelCase_ :bool = False , lowerCamelCase_ :Optional[TensorType] = None , ) -> Mapping[str, Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : List[str] = compute_effective_axis_dimension( lowerCamelCase_ , fixed_dimension=OnnxConfig.default_fixed_batch , num_token_to_add=0 ) # If dynamic axis (-1) we forward with a fixed dimension of 8 tokens to avoid optimizations made by ONNX SCREAMING_SNAKE_CASE : int = tokenizer.num_special_tokens_to_add(lowerCamelCase_ ) SCREAMING_SNAKE_CASE : int = compute_effective_axis_dimension( lowerCamelCase_ , fixed_dimension=OnnxConfig.default_fixed_sequence , num_token_to_add=lowerCamelCase_ ) # Generate dummy inputs according to compute batch and sequence SCREAMING_SNAKE_CASE : Tuple = [''' '''.join([tokenizer.unk_token] ) * seq_length] * batch_size SCREAMING_SNAKE_CASE : Any = dict(tokenizer(lowerCamelCase_ , return_tensors=lowerCamelCase_ ) ) return common_inputs def __lowerCAmelCase ( self :List[Any] , lowerCamelCase_ :PreTrainedTokenizer , lowerCamelCase_ :int = -1 , lowerCamelCase_ :int = -1 , lowerCamelCase_ :bool = False , lowerCamelCase_ :Optional[TensorType] = None , ) -> Mapping[str, Any]: '''simple docstring''' if self.task in ["default", "seq2seq-lm"]: SCREAMING_SNAKE_CASE : Dict = self._generate_dummy_inputs_for_default_and_seqaseq_lm( lowerCamelCase_ , batch_size=lowerCamelCase_ , seq_length=lowerCamelCase_ , is_pair=lowerCamelCase_ , framework=lowerCamelCase_ ) elif self.task == "causal-lm": SCREAMING_SNAKE_CASE : Union[str, Any] = self._generate_dummy_inputs_for_causal_lm( lowerCamelCase_ , batch_size=lowerCamelCase_ , seq_length=lowerCamelCase_ , is_pair=lowerCamelCase_ , framework=lowerCamelCase_ ) else: SCREAMING_SNAKE_CASE : List[Any] = self._generate_dummy_inputs_for_sequence_classification_and_question_answering( lowerCamelCase_ , batch_size=lowerCamelCase_ , seq_length=lowerCamelCase_ , is_pair=lowerCamelCase_ , framework=lowerCamelCase_ ) return common_inputs def __lowerCAmelCase ( self :Dict , lowerCamelCase_ :int , lowerCamelCase_ :List[str] , lowerCamelCase_ :List[str] , lowerCamelCase_ :Dict ) -> List[Any]: '''simple docstring''' if self.task in ["default", "seq2seq-lm"]: SCREAMING_SNAKE_CASE : Optional[Any] = super()._flatten_past_key_values_(lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ ) else: SCREAMING_SNAKE_CASE : Tuple = super(lowerCamelCase_ , self )._flatten_past_key_values_( lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ )
18
1
"""simple docstring""" import re import string import numpy as np import datasets lowerCamelCase__ : List[Any] = "\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__ : 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__ : List[str] = "\n" @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class lowercase__( datasets.Metric ): '''simple docstring''' def __lowerCAmelCase ( self :str ) -> Any: '''simple docstring''' return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { '''predictions''': datasets.Value('''string''' , id='''sequence''' ), '''references''': datasets.Value('''string''' , id='''sequence''' ), } ) , reference_urls=[] , ) def __lowerCAmelCase ( self :str , lowerCamelCase_ :Union[str, Any] , lowerCamelCase_ :Optional[int] , lowerCamelCase_ :int=None , lowerCamelCase_ :List[Any]=False , lowerCamelCase_ :Union[str, Any]=False , lowerCamelCase_ :int=False , ) -> Any: '''simple docstring''' if regexes_to_ignore is not None: for s in regexes_to_ignore: SCREAMING_SNAKE_CASE : List[Any] = np.array([re.sub(lowerCamelCase_ , '''''' , lowerCamelCase_ ) for x in predictions] ) SCREAMING_SNAKE_CASE : Any = np.array([re.sub(lowerCamelCase_ , '''''' , lowerCamelCase_ ) for x in references] ) else: SCREAMING_SNAKE_CASE : Dict = np.asarray(lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Optional[Any] = np.asarray(lowerCamelCase_ ) if ignore_case: SCREAMING_SNAKE_CASE : List[Any] = np.char.lower(lowerCamelCase_ ) SCREAMING_SNAKE_CASE : List[str] = np.char.lower(lowerCamelCase_ ) if ignore_punctuation: SCREAMING_SNAKE_CASE : Optional[Any] = string.punctuation.maketrans('''''' , '''''' , string.punctuation ) SCREAMING_SNAKE_CASE : Union[str, Any] = np.char.translate(lowerCamelCase_ , table=lowerCamelCase_ ) SCREAMING_SNAKE_CASE : str = np.char.translate(lowerCamelCase_ , table=lowerCamelCase_ ) if ignore_numbers: SCREAMING_SNAKE_CASE : str = string.digits.maketrans('''''' , '''''' , string.digits ) SCREAMING_SNAKE_CASE : Optional[Any] = np.char.translate(lowerCamelCase_ , table=lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Optional[int] = np.char.translate(lowerCamelCase_ , table=lowerCamelCase_ ) SCREAMING_SNAKE_CASE : int = predictions == references return {"exact_match": np.mean(lowerCamelCase_ ) * 1_00}
18
"""simple docstring""" from collections import OrderedDict from typing import TYPE_CHECKING, Any, Mapping, Optional from packaging import version from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...onnx.utils import compute_effective_axis_dimension from ...utils import logging if TYPE_CHECKING: from ...processing_utils import ProcessorMixin from ...utils import TensorType lowerCamelCase__ : Dict = logging.get_logger(__name__) lowerCamelCase__ : Dict = { "microsoft/layoutlmv3-base": "https://huggingface.co/microsoft/layoutlmv3-base/resolve/main/config.json", } class lowercase__( _UpperCAmelCase ): '''simple docstring''' UpperCamelCase = """layoutlmv3""" def __init__( self :str , lowerCamelCase_ :Optional[Any]=5_02_65 , lowerCamelCase_ :Dict=7_68 , lowerCamelCase_ :Union[str, Any]=12 , lowerCamelCase_ :Optional[Any]=12 , lowerCamelCase_ :Union[str, Any]=30_72 , lowerCamelCase_ :Any="gelu" , lowerCamelCase_ :Union[str, Any]=0.1 , lowerCamelCase_ :str=0.1 , lowerCamelCase_ :Any=5_12 , lowerCamelCase_ :int=2 , lowerCamelCase_ :Optional[Any]=0.0_2 , lowerCamelCase_ :Optional[int]=1E-5 , lowerCamelCase_ :Dict=1 , lowerCamelCase_ :int=0 , lowerCamelCase_ :Tuple=2 , lowerCamelCase_ :List[str]=10_24 , lowerCamelCase_ :Tuple=1_28 , lowerCamelCase_ :Any=1_28 , lowerCamelCase_ :Optional[Any]=True , lowerCamelCase_ :str=32 , lowerCamelCase_ :int=1_28 , lowerCamelCase_ :int=64 , lowerCamelCase_ :List[Any]=2_56 , lowerCamelCase_ :Any=True , lowerCamelCase_ :str=True , lowerCamelCase_ :Union[str, Any]=True , lowerCamelCase_ :List[str]=2_24 , lowerCamelCase_ :Dict=3 , lowerCamelCase_ :Union[str, Any]=16 , lowerCamelCase_ :Any=None , **lowerCamelCase_ :Optional[Any] , ) -> int: '''simple docstring''' super().__init__( vocab_size=lowerCamelCase_ , hidden_size=lowerCamelCase_ , num_hidden_layers=lowerCamelCase_ , num_attention_heads=lowerCamelCase_ , intermediate_size=lowerCamelCase_ , hidden_act=lowerCamelCase_ , hidden_dropout_prob=lowerCamelCase_ , attention_probs_dropout_prob=lowerCamelCase_ , max_position_embeddings=lowerCamelCase_ , type_vocab_size=lowerCamelCase_ , initializer_range=lowerCamelCase_ , layer_norm_eps=lowerCamelCase_ , pad_token_id=lowerCamelCase_ , bos_token_id=lowerCamelCase_ , eos_token_id=lowerCamelCase_ , **lowerCamelCase_ , ) SCREAMING_SNAKE_CASE : Optional[Any] = max_ad_position_embeddings SCREAMING_SNAKE_CASE : List[Any] = coordinate_size SCREAMING_SNAKE_CASE : Tuple = shape_size SCREAMING_SNAKE_CASE : Optional[int] = has_relative_attention_bias SCREAMING_SNAKE_CASE : List[Any] = rel_pos_bins SCREAMING_SNAKE_CASE : int = max_rel_pos SCREAMING_SNAKE_CASE : Any = has_spatial_attention_bias SCREAMING_SNAKE_CASE : List[Any] = rel_ad_pos_bins SCREAMING_SNAKE_CASE : Dict = max_rel_ad_pos SCREAMING_SNAKE_CASE : Optional[int] = text_embed SCREAMING_SNAKE_CASE : Any = visual_embed SCREAMING_SNAKE_CASE : Any = input_size SCREAMING_SNAKE_CASE : Tuple = num_channels SCREAMING_SNAKE_CASE : List[str] = patch_size SCREAMING_SNAKE_CASE : str = classifier_dropout class lowercase__( _UpperCAmelCase ): '''simple docstring''' UpperCamelCase = version.parse("""1.12""" ) @property def __lowerCAmelCase ( self :List[Any] ) -> Mapping[str, Mapping[int, str]]: '''simple docstring''' if self.task in ["question-answering", "sequence-classification"]: return OrderedDict( [ ('''input_ids''', {0: '''batch''', 1: '''sequence'''}), ('''attention_mask''', {0: '''batch''', 1: '''sequence'''}), ('''bbox''', {0: '''batch''', 1: '''sequence'''}), ('''pixel_values''', {0: '''batch''', 1: '''num_channels''', 2: '''height''', 3: '''width'''}), ] ) else: return OrderedDict( [ ('''input_ids''', {0: '''batch''', 1: '''sequence'''}), ('''bbox''', {0: '''batch''', 1: '''sequence'''}), ('''attention_mask''', {0: '''batch''', 1: '''sequence'''}), ('''pixel_values''', {0: '''batch''', 1: '''num_channels'''}), ] ) @property def __lowerCAmelCase ( self :Optional[int] ) -> float: '''simple docstring''' return 1E-5 @property def __lowerCAmelCase ( self :Tuple ) -> int: '''simple docstring''' return 12 def __lowerCAmelCase ( self :List[Any] , lowerCamelCase_ :"ProcessorMixin" , lowerCamelCase_ :int = -1 , lowerCamelCase_ :int = -1 , lowerCamelCase_ :bool = False , lowerCamelCase_ :Optional["TensorType"] = None , lowerCamelCase_ :int = 3 , lowerCamelCase_ :int = 40 , lowerCamelCase_ :int = 40 , ) -> Mapping[str, Any]: '''simple docstring''' setattr(processor.image_processor , '''apply_ocr''' , lowerCamelCase_ ) # If dynamic axis (-1) we forward with a fixed dimension of 2 samples to avoid optimizations made by ONNX SCREAMING_SNAKE_CASE : Dict = compute_effective_axis_dimension( lowerCamelCase_ , fixed_dimension=OnnxConfig.default_fixed_batch , num_token_to_add=0 ) # If dynamic axis (-1) we forward with a fixed dimension of 8 tokens to avoid optimizations made by ONNX SCREAMING_SNAKE_CASE : Union[str, Any] = processor.tokenizer.num_special_tokens_to_add(lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Any = compute_effective_axis_dimension( lowerCamelCase_ , fixed_dimension=OnnxConfig.default_fixed_sequence , num_token_to_add=lowerCamelCase_ ) # Generate dummy inputs according to compute batch and sequence SCREAMING_SNAKE_CASE : Union[str, Any] = [[''' '''.join([processor.tokenizer.unk_token] ) * seq_length]] * batch_size # Generate dummy bounding boxes SCREAMING_SNAKE_CASE : int = [[[48, 84, 73, 1_28]]] * batch_size # If dynamic axis (-1) we forward with a fixed dimension of 2 samples to avoid optimizations made by ONNX # batch_size = compute_effective_axis_dimension(batch_size, fixed_dimension=OnnxConfig.default_fixed_batch) SCREAMING_SNAKE_CASE : List[Any] = self._generate_dummy_images(lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ ) SCREAMING_SNAKE_CASE : int = dict( processor( lowerCamelCase_ , text=lowerCamelCase_ , boxes=lowerCamelCase_ , return_tensors=lowerCamelCase_ , ) ) return inputs
18
1
"""simple docstring""" import warnings from typing import List, Optional, Tuple, Union import numpy as np import PIL import torch from ...models import UNetaDModel from ...schedulers import RePaintScheduler from ...utils import PIL_INTERPOLATION, logging, randn_tensor from ..pipeline_utils import DiffusionPipeline, ImagePipelineOutput lowerCamelCase__ : Union[str, Any] = logging.get_logger(__name__) # pylint: disable=invalid-name def __A ( a_ : Union[List, PIL.Image.Image, torch.Tensor] )-> List[Any]: '''simple docstring''' warnings.warn( '''The preprocess method is deprecated and will be removed in a future version. Please''' ''' use VaeImageProcessor.preprocess instead''' , a_ , ) if isinstance(a_ , torch.Tensor ): return image elif isinstance(a_ , PIL.Image.Image ): SCREAMING_SNAKE_CASE : Union[str, Any] = [image] if isinstance(image[0] , PIL.Image.Image ): SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : List[str] = image[0].size SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : Union[str, Any] = (x - x % 8 for x in (w, h)) # resize to integer multiple of 8 SCREAMING_SNAKE_CASE : str = [np.array(i.resize((w, h) , resample=PIL_INTERPOLATION['''lanczos'''] ) )[None, :] for i in image] SCREAMING_SNAKE_CASE : str = np.concatenate(a_ , axis=0 ) SCREAMING_SNAKE_CASE : List[str] = np.array(a_ ).astype(np.floataa ) / 255.0 SCREAMING_SNAKE_CASE : List[str] = image.transpose(0 , 3 , 1 , 2 ) SCREAMING_SNAKE_CASE : Optional[Any] = 2.0 * image - 1.0 SCREAMING_SNAKE_CASE : List[Any] = torch.from_numpy(a_ ) elif isinstance(image[0] , torch.Tensor ): SCREAMING_SNAKE_CASE : Tuple = torch.cat(a_ , dim=0 ) return image def __A ( a_ : Union[List, PIL.Image.Image, torch.Tensor] )-> Any: '''simple docstring''' if isinstance(a_ , torch.Tensor ): return mask elif isinstance(a_ , PIL.Image.Image ): SCREAMING_SNAKE_CASE : Optional[int] = [mask] if isinstance(mask[0] , PIL.Image.Image ): SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : Any = mask[0].size SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : List[str] = (x - x % 32 for x in (w, h)) # resize to integer multiple of 32 SCREAMING_SNAKE_CASE : List[str] = [np.array(m.convert('''L''' ).resize((w, h) , resample=PIL_INTERPOLATION['''nearest'''] ) )[None, :] for m in mask] SCREAMING_SNAKE_CASE : List[str] = np.concatenate(a_ , axis=0 ) SCREAMING_SNAKE_CASE : Any = mask.astype(np.floataa ) / 255.0 SCREAMING_SNAKE_CASE : str = 0 SCREAMING_SNAKE_CASE : str = 1 SCREAMING_SNAKE_CASE : Union[str, Any] = torch.from_numpy(a_ ) elif isinstance(mask[0] , torch.Tensor ): SCREAMING_SNAKE_CASE : Tuple = torch.cat(a_ , dim=0 ) return mask class lowercase__( _UpperCAmelCase ): '''simple docstring''' UpperCamelCase = 42 UpperCamelCase = 42 def __init__( self :Union[str, Any] , lowerCamelCase_ :int , lowerCamelCase_ :List[Any] ) -> Optional[int]: '''simple docstring''' super().__init__() self.register_modules(unet=lowerCamelCase_ , scheduler=lowerCamelCase_ ) @torch.no_grad() def __call__( self :Tuple , lowerCamelCase_ :Union[torch.Tensor, PIL.Image.Image] , lowerCamelCase_ :Union[torch.Tensor, PIL.Image.Image] , lowerCamelCase_ :int = 2_50 , lowerCamelCase_ :float = 0.0 , lowerCamelCase_ :int = 10 , lowerCamelCase_ :int = 10 , lowerCamelCase_ :Optional[Union[torch.Generator, List[torch.Generator]]] = None , lowerCamelCase_ :Optional[str] = "pil" , lowerCamelCase_ :bool = True , ) -> Union[ImagePipelineOutput, Tuple]: '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[int] = image SCREAMING_SNAKE_CASE : Any = _preprocess_image(lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Dict = original_image.to(device=self.device , dtype=self.unet.dtype ) SCREAMING_SNAKE_CASE : Optional[Any] = _preprocess_mask(lowerCamelCase_ ) SCREAMING_SNAKE_CASE : int = mask_image.to(device=self.device , dtype=self.unet.dtype ) SCREAMING_SNAKE_CASE : Optional[Any] = original_image.shape[0] # sample gaussian noise to begin the loop if isinstance(lowerCamelCase_ , lowerCamelCase_ ) and len(lowerCamelCase_ ) != batch_size: raise ValueError( f"You have passed a list of generators of length {len(lowerCamelCase_ )}, but requested an effective batch" f" size of {batch_size}. Make sure the batch size matches the length of the generators." ) SCREAMING_SNAKE_CASE : Tuple = original_image.shape SCREAMING_SNAKE_CASE : List[Any] = randn_tensor(lowerCamelCase_ , generator=lowerCamelCase_ , device=self.device , dtype=self.unet.dtype ) # set step values self.scheduler.set_timesteps(lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , self.device ) SCREAMING_SNAKE_CASE : Optional[Any] = eta SCREAMING_SNAKE_CASE : Optional[Any] = self.scheduler.timesteps[0] + 1 SCREAMING_SNAKE_CASE : Dict = generator[0] if isinstance(lowerCamelCase_ , lowerCamelCase_ ) else generator for i, t in enumerate(self.progress_bar(self.scheduler.timesteps ) ): if t < t_last: # predict the noise residual SCREAMING_SNAKE_CASE : str = self.unet(lowerCamelCase_ , lowerCamelCase_ ).sample # compute previous image: x_t -> x_t-1 SCREAMING_SNAKE_CASE : int = self.scheduler.step(lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ ).prev_sample else: # compute the reverse: x_t-1 -> x_t SCREAMING_SNAKE_CASE : Union[str, Any] = self.scheduler.undo_step(lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Optional[int] = t SCREAMING_SNAKE_CASE : Any = (image / 2 + 0.5).clamp(0 , 1 ) SCREAMING_SNAKE_CASE : Optional[int] = image.cpu().permute(0 , 2 , 3 , 1 ).numpy() if output_type == "pil": SCREAMING_SNAKE_CASE : List[str] = self.numpy_to_pil(lowerCamelCase_ ) if not return_dict: return (image,) return ImagePipelineOutput(images=lowerCamelCase_ )
18
"""simple docstring""" import math def __A ( a_ : list , a_ : int )-> int: '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[int] = len(a_ ) SCREAMING_SNAKE_CASE : Optional[Any] = int(math.floor(math.sqrt(a_ ) ) ) SCREAMING_SNAKE_CASE : List[str] = 0 while arr[min(a_ , a_ ) - 1] < x: SCREAMING_SNAKE_CASE : Optional[Any] = step step += int(math.floor(math.sqrt(a_ ) ) ) if prev >= n: return -1 while arr[prev] < x: SCREAMING_SNAKE_CASE : Any = prev + 1 if prev == min(a_ , a_ ): return -1 if arr[prev] == x: return prev return -1 if __name__ == "__main__": lowerCamelCase__ : Union[str, Any] = input("Enter numbers separated by a comma:\n").strip() lowerCamelCase__ : List[str] = [int(item) for item in user_input.split(",")] lowerCamelCase__ : Dict = int(input("Enter the number to be searched:\n")) lowerCamelCase__ : Tuple = jump_search(arr, x) if res == -1: print("Number not found!") else: print(f'''Number {x} is at index {res}''')
18
1
"""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 import os from accelerate.test_utils import execute_subprocess_async def __A ( a_ : Union[str, Any]=None )-> Any: '''simple docstring''' if subparsers is not None: SCREAMING_SNAKE_CASE : Optional[Any] = subparsers.add_parser('''test''' ) else: SCREAMING_SNAKE_CASE : List[Any] = argparse.ArgumentParser('''Accelerate test command''' ) parser.add_argument( '''--config_file''' , default=a_ , help=( '''The path to use to store the config file. Will default to a file named default_config.yaml in the cache ''' '''location, which is the content of the environment `HF_HOME` suffixed with \'accelerate\', or if you don\'t have ''' '''such an environment variable, your cache directory (\'~/.cache\' or the content of `XDG_CACHE_HOME`) suffixed ''' '''with \'huggingface\'.''' ) , ) if subparsers is not None: parser.set_defaults(func=a_ ) return parser def __A ( a_ : Optional[int] )-> Optional[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : List[str] = os.path.sep.join(__file__.split(os.path.sep )[:-2] + ['''test_utils''', '''scripts''', '''test_script.py'''] ) if args.config_file is None: SCREAMING_SNAKE_CASE : List[Any] = script_name else: SCREAMING_SNAKE_CASE : Dict = F"--config_file={args.config_file} {script_name}" SCREAMING_SNAKE_CASE : Optional[Any] = ['''accelerate-launch'''] + test_args.split() SCREAMING_SNAKE_CASE : List[Any] = execute_subprocess_async(a_ , env=os.environ.copy() ) if result.returncode == 0: print('''Test is a success! You are ready for your distributed training!''' ) def __A ( )-> Dict: '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[Any] = test_command_parser() SCREAMING_SNAKE_CASE : Dict = parser.parse_args() test_command(a_ ) if __name__ == "__main__": main()
18
"""simple docstring""" from sklearn.metrics import fa_score import datasets lowerCamelCase__ : List[Any] = "\nThe F1 score is the harmonic mean of the precision and recall. It can be computed with the equation:\nF1 = 2 * (precision * recall) / (precision + recall)\n" lowerCamelCase__ : str = "\nArgs:\n predictions (`list` of `int`): Predicted labels.\n references (`list` of `int`): Ground truth labels.\n labels (`list` of `int`): The set of labels to include when `average` is not set to `'binary'`, and the order of the labels if `average` is `None`. Labels present in the data can be excluded, for example to calculate a multiclass average ignoring a majority negative class. Labels not present in the data will result in 0 components in a macro average. For multilabel targets, labels are column indices. By default, all labels in `predictions` and `references` are used in sorted order. Defaults to None.\n pos_label (`int`): The class to be considered the positive class, in the case where `average` is set to `binary`. Defaults to 1.\n average (`string`): This parameter is required for multiclass/multilabel targets. If set to `None`, the scores for each class are returned. Otherwise, this determines the type of averaging performed on the data. Defaults to `'binary'`.\n\n - 'binary': Only report results for the class specified by `pos_label`. This is applicable only if the classes found in `predictions` and `references` are binary.\n - 'micro': Calculate metrics globally by counting the total true positives, false negatives and false positives.\n - 'macro': Calculate metrics for each label, and find their unweighted mean. This does not take label imbalance into account.\n - 'weighted': Calculate metrics for each label, and find their average weighted by support (the number of true instances for each label). This alters `'macro'` to account for label imbalance. This option can result in an F-score that is not between precision and recall.\n - 'samples': Calculate metrics for each instance, and find their average (only meaningful for multilabel classification).\n sample_weight (`list` of `float`): Sample weights Defaults to None.\n\nReturns:\n f1 (`float` or `array` of `float`): F1 score or list of f1 scores, depending on the value passed to `average`. Minimum possible value is 0. Maximum possible value is 1. Higher f1 scores are better.\n\nExamples:\n\n Example 1-A simple binary example\n >>> f1_metric = datasets.load_metric(\"f1\")\n >>> results = f1_metric.compute(references=[0, 1, 0, 1, 0], predictions=[0, 0, 1, 1, 0])\n >>> print(results)\n {'f1': 0.5}\n\n Example 2-The same simple binary example as in Example 1, but with `pos_label` set to `0`.\n >>> f1_metric = datasets.load_metric(\"f1\")\n >>> results = f1_metric.compute(references=[0, 1, 0, 1, 0], predictions=[0, 0, 1, 1, 0], pos_label=0)\n >>> print(round(results['f1'], 2))\n 0.67\n\n Example 3-The same simple binary example as in Example 1, but with `sample_weight` included.\n >>> f1_metric = datasets.load_metric(\"f1\")\n >>> results = f1_metric.compute(references=[0, 1, 0, 1, 0], predictions=[0, 0, 1, 1, 0], sample_weight=[0.9, 0.5, 3.9, 1.2, 0.3])\n >>> print(round(results['f1'], 2))\n 0.35\n\n Example 4-A multiclass example, with different values for the `average` input.\n >>> predictions = [0, 2, 1, 0, 0, 1]\n >>> references = [0, 1, 2, 0, 1, 2]\n >>> results = f1_metric.compute(predictions=predictions, references=references, average=\"macro\")\n >>> print(round(results['f1'], 2))\n 0.27\n >>> results = f1_metric.compute(predictions=predictions, references=references, average=\"micro\")\n >>> print(round(results['f1'], 2))\n 0.33\n >>> results = f1_metric.compute(predictions=predictions, references=references, average=\"weighted\")\n >>> print(round(results['f1'], 2))\n 0.27\n >>> results = f1_metric.compute(predictions=predictions, references=references, average=None)\n >>> print(results)\n {'f1': array([0.8, 0. , 0. ])}\n" lowerCamelCase__ : int = "\n@article{scikit-learn,\n title={Scikit-learn: Machine Learning in {P}ython},\n author={Pedregosa, F. and Varoquaux, G. and Gramfort, A. and Michel, V.\n and Thirion, B. and Grisel, O. and Blondel, M. and Prettenhofer, P.\n and Weiss, R. and Dubourg, V. and Vanderplas, J. and Passos, A. and\n Cournapeau, D. and Brucher, M. and Perrot, M. and Duchesnay, E.},\n journal={Journal of Machine Learning Research},\n volume={12},\n pages={2825--2830},\n year={2011}\n}\n" @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class lowercase__( datasets.Metric ): '''simple docstring''' def __lowerCAmelCase ( self :str ) -> Any: '''simple docstring''' return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { '''predictions''': datasets.Sequence(datasets.Value('''int32''' ) ), '''references''': datasets.Sequence(datasets.Value('''int32''' ) ), } if self.config_name == '''multilabel''' else { '''predictions''': datasets.Value('''int32''' ), '''references''': datasets.Value('''int32''' ), } ) , reference_urls=['''https://scikit-learn.org/stable/modules/generated/sklearn.metrics.f1_score.html'''] , ) def __lowerCAmelCase ( self :Any , lowerCamelCase_ :Dict , lowerCamelCase_ :Optional[Any] , lowerCamelCase_ :int=None , lowerCamelCase_ :str=1 , lowerCamelCase_ :Union[str, Any]="binary" , lowerCamelCase_ :Dict=None ) -> int: '''simple docstring''' SCREAMING_SNAKE_CASE : int = fa_score( lowerCamelCase_ , lowerCamelCase_ , labels=lowerCamelCase_ , pos_label=lowerCamelCase_ , average=lowerCamelCase_ , sample_weight=lowerCamelCase_ ) return {"f1": float(lowerCamelCase_ ) if score.size == 1 else score}
18
1
"""simple docstring""" import argparse import io import requests import torch from omegaconf import OmegaConf from diffusers import AutoencoderKL from diffusers.pipelines.stable_diffusion.convert_from_ckpt import ( assign_to_checkpoint, conv_attn_to_linear, create_vae_diffusers_config, renew_vae_attention_paths, renew_vae_resnet_paths, ) def __A ( a_ : str , a_ : List[Any] )-> Optional[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : Tuple = checkpoint SCREAMING_SNAKE_CASE : int = {} SCREAMING_SNAKE_CASE : int = vae_state_dict['''encoder.conv_in.weight'''] SCREAMING_SNAKE_CASE : List[Any] = vae_state_dict['''encoder.conv_in.bias'''] SCREAMING_SNAKE_CASE : Optional[int] = vae_state_dict['''encoder.conv_out.weight'''] SCREAMING_SNAKE_CASE : Tuple = vae_state_dict['''encoder.conv_out.bias'''] SCREAMING_SNAKE_CASE : int = vae_state_dict['''encoder.norm_out.weight'''] SCREAMING_SNAKE_CASE : Union[str, Any] = vae_state_dict['''encoder.norm_out.bias'''] SCREAMING_SNAKE_CASE : List[str] = vae_state_dict['''decoder.conv_in.weight'''] SCREAMING_SNAKE_CASE : Any = vae_state_dict['''decoder.conv_in.bias'''] SCREAMING_SNAKE_CASE : List[Any] = vae_state_dict['''decoder.conv_out.weight'''] SCREAMING_SNAKE_CASE : Dict = vae_state_dict['''decoder.conv_out.bias'''] SCREAMING_SNAKE_CASE : Union[str, Any] = vae_state_dict['''decoder.norm_out.weight'''] SCREAMING_SNAKE_CASE : Dict = vae_state_dict['''decoder.norm_out.bias'''] SCREAMING_SNAKE_CASE : Optional[Any] = vae_state_dict['''quant_conv.weight'''] SCREAMING_SNAKE_CASE : List[str] = vae_state_dict['''quant_conv.bias'''] SCREAMING_SNAKE_CASE : Optional[int] = vae_state_dict['''post_quant_conv.weight'''] SCREAMING_SNAKE_CASE : str = vae_state_dict['''post_quant_conv.bias'''] # Retrieves the keys for the encoder down blocks only SCREAMING_SNAKE_CASE : Any = len({'''.'''.join(layer.split('''.''' )[:3] ) for layer in vae_state_dict if '''encoder.down''' in layer} ) SCREAMING_SNAKE_CASE : List[Any] = { layer_id: [key for key in vae_state_dict if F"down.{layer_id}" in key] for layer_id in range(a_ ) } # Retrieves the keys for the decoder up blocks only SCREAMING_SNAKE_CASE : Any = len({'''.'''.join(layer.split('''.''' )[:3] ) for layer in vae_state_dict if '''decoder.up''' in layer} ) SCREAMING_SNAKE_CASE : Union[str, Any] = { layer_id: [key for key in vae_state_dict if F"up.{layer_id}" in key] for layer_id in range(a_ ) } for i in range(a_ ): SCREAMING_SNAKE_CASE : Any = [key for key in down_blocks[i] if F"down.{i}" in key and F"down.{i}.downsample" not in key] if F"encoder.down.{i}.downsample.conv.weight" in vae_state_dict: SCREAMING_SNAKE_CASE : List[str] = vae_state_dict.pop( F"encoder.down.{i}.downsample.conv.weight" ) SCREAMING_SNAKE_CASE : Optional[int] = vae_state_dict.pop( F"encoder.down.{i}.downsample.conv.bias" ) SCREAMING_SNAKE_CASE : str = renew_vae_resnet_paths(a_ ) SCREAMING_SNAKE_CASE : Optional[Any] = {'''old''': F"down.{i}.block", '''new''': F"down_blocks.{i}.resnets"} assign_to_checkpoint(a_ , a_ , a_ , additional_replacements=[meta_path] , config=a_ ) SCREAMING_SNAKE_CASE : List[str] = [key for key in vae_state_dict if '''encoder.mid.block''' in key] SCREAMING_SNAKE_CASE : Any = 2 for i in range(1 , num_mid_res_blocks + 1 ): SCREAMING_SNAKE_CASE : Optional[Any] = [key for key in mid_resnets if F"encoder.mid.block_{i}" in key] SCREAMING_SNAKE_CASE : int = renew_vae_resnet_paths(a_ ) SCREAMING_SNAKE_CASE : str = {'''old''': F"mid.block_{i}", '''new''': F"mid_block.resnets.{i - 1}"} assign_to_checkpoint(a_ , a_ , a_ , additional_replacements=[meta_path] , config=a_ ) SCREAMING_SNAKE_CASE : Dict = [key for key in vae_state_dict if '''encoder.mid.attn''' in key] SCREAMING_SNAKE_CASE : List[str] = renew_vae_attention_paths(a_ ) SCREAMING_SNAKE_CASE : Optional[Any] = {'''old''': '''mid.attn_1''', '''new''': '''mid_block.attentions.0'''} assign_to_checkpoint(a_ , a_ , a_ , additional_replacements=[meta_path] , config=a_ ) conv_attn_to_linear(a_ ) for i in range(a_ ): SCREAMING_SNAKE_CASE : Optional[Any] = num_up_blocks - 1 - i SCREAMING_SNAKE_CASE : Union[str, Any] = [ key for key in up_blocks[block_id] if F"up.{block_id}" in key and F"up.{block_id}.upsample" not in key ] if F"decoder.up.{block_id}.upsample.conv.weight" in vae_state_dict: SCREAMING_SNAKE_CASE : Tuple = vae_state_dict[ F"decoder.up.{block_id}.upsample.conv.weight" ] SCREAMING_SNAKE_CASE : List[str] = vae_state_dict[ F"decoder.up.{block_id}.upsample.conv.bias" ] SCREAMING_SNAKE_CASE : List[str] = renew_vae_resnet_paths(a_ ) SCREAMING_SNAKE_CASE : str = {'''old''': F"up.{block_id}.block", '''new''': F"up_blocks.{i}.resnets"} assign_to_checkpoint(a_ , a_ , a_ , additional_replacements=[meta_path] , config=a_ ) SCREAMING_SNAKE_CASE : Dict = [key for key in vae_state_dict if '''decoder.mid.block''' in key] SCREAMING_SNAKE_CASE : Any = 2 for i in range(1 , num_mid_res_blocks + 1 ): SCREAMING_SNAKE_CASE : Optional[Any] = [key for key in mid_resnets if F"decoder.mid.block_{i}" in key] SCREAMING_SNAKE_CASE : Union[str, Any] = renew_vae_resnet_paths(a_ ) SCREAMING_SNAKE_CASE : List[str] = {'''old''': F"mid.block_{i}", '''new''': F"mid_block.resnets.{i - 1}"} assign_to_checkpoint(a_ , a_ , a_ , additional_replacements=[meta_path] , config=a_ ) SCREAMING_SNAKE_CASE : Tuple = [key for key in vae_state_dict if '''decoder.mid.attn''' in key] SCREAMING_SNAKE_CASE : Dict = renew_vae_attention_paths(a_ ) SCREAMING_SNAKE_CASE : Tuple = {'''old''': '''mid.attn_1''', '''new''': '''mid_block.attentions.0'''} assign_to_checkpoint(a_ , a_ , a_ , additional_replacements=[meta_path] , config=a_ ) conv_attn_to_linear(a_ ) return new_checkpoint def __A ( a_ : str , a_ : str , )-> Union[str, Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[int] = requests.get( ''' https://raw.githubusercontent.com/CompVis/stable-diffusion/main/configs/stable-diffusion/v1-inference.yaml''' ) SCREAMING_SNAKE_CASE : Union[str, Any] = io.BytesIO(r.content ) SCREAMING_SNAKE_CASE : Union[str, Any] = OmegaConf.load(a_ ) SCREAMING_SNAKE_CASE : List[Any] = 5_12 SCREAMING_SNAKE_CASE : int = '''cuda''' if torch.cuda.is_available() else '''cpu''' if checkpoint_path.endswith('''safetensors''' ): from safetensors import safe_open SCREAMING_SNAKE_CASE : Dict = {} with safe_open(a_ , framework='''pt''' , device='''cpu''' ) as f: for key in f.keys(): SCREAMING_SNAKE_CASE : Any = f.get_tensor(a_ ) else: SCREAMING_SNAKE_CASE : Any = torch.load(a_ , map_location=a_ )['''state_dict'''] # Convert the VAE model. SCREAMING_SNAKE_CASE : List[Any] = create_vae_diffusers_config(a_ , image_size=a_ ) SCREAMING_SNAKE_CASE : Any = custom_convert_ldm_vae_checkpoint(a_ , a_ ) SCREAMING_SNAKE_CASE : Dict = AutoencoderKL(**a_ ) vae.load_state_dict(a_ ) vae.save_pretrained(a_ ) if __name__ == "__main__": lowerCamelCase__ : Any = argparse.ArgumentParser() parser.add_argument("--vae_pt_path", default=None, type=str, required=True, help="Path to the VAE.pt to convert.") parser.add_argument("--dump_path", default=None, type=str, required=True, help="Path to the VAE.pt to convert.") lowerCamelCase__ : Any = parser.parse_args() vae_pt_to_vae_diffuser(args.vae_pt_path, args.dump_path)
18
"""simple docstring""" from __future__ import annotations from fractions import Fraction def __A ( a_ : int , a_ : int )-> bool: '''simple docstring''' return ( num != den and num % 10 == den // 10 and (num // 10) / (den % 10) == num / den ) def __A ( a_ : int )-> list[str]: '''simple docstring''' SCREAMING_SNAKE_CASE : Union[str, Any] = [] SCREAMING_SNAKE_CASE : List[str] = 11 SCREAMING_SNAKE_CASE : Union[str, Any] = int('''1''' + '''0''' * digit_len ) for num in range(a_ , a_ ): while den <= 99: if (num != den) and (num % 10 == den // 10) and (den % 10 != 0): if is_digit_cancelling(a_ , a_ ): solutions.append(F"{num}/{den}" ) den += 1 num += 1 SCREAMING_SNAKE_CASE : Optional[Any] = 10 return solutions def __A ( a_ : int = 2 )-> int: '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[Any] = 1.0 for fraction in fraction_list(a_ ): SCREAMING_SNAKE_CASE : List[str] = Fraction(a_ ) result *= frac.denominator / frac.numerator return int(a_ ) if __name__ == "__main__": print(solution())
18
1
"""simple docstring""" from argparse import ArgumentParser, Namespace from ..utils import logging from . import BaseTransformersCLICommand def __A ( a_ : Namespace )-> Tuple: '''simple docstring''' return ConvertCommand( args.model_type , args.tf_checkpoint , args.pytorch_dump_output , args.config , args.finetuning_task_name ) lowerCamelCase__ : int = "\ntransformers can only be used from the commandline to convert TensorFlow models in PyTorch, In that case, it requires\nTensorFlow to be installed. Please see https://www.tensorflow.org/install/ for installation instructions.\n" class lowercase__( _UpperCAmelCase ): '''simple docstring''' @staticmethod def __lowerCAmelCase ( lowerCamelCase_ :ArgumentParser ) -> Any: '''simple docstring''' SCREAMING_SNAKE_CASE : str = parser.add_parser( '''convert''' , help='''CLI tool to run convert model from original author checkpoints to Transformers PyTorch checkpoints.''' , ) train_parser.add_argument('''--model_type''' , type=lowerCamelCase_ , required=lowerCamelCase_ , help='''Model\'s type.''' ) train_parser.add_argument( '''--tf_checkpoint''' , type=lowerCamelCase_ , required=lowerCamelCase_ , help='''TensorFlow checkpoint path or folder.''' ) train_parser.add_argument( '''--pytorch_dump_output''' , type=lowerCamelCase_ , required=lowerCamelCase_ , help='''Path to the PyTorch saved model output.''' ) train_parser.add_argument('''--config''' , type=lowerCamelCase_ , default='''''' , help='''Configuration file path or folder.''' ) train_parser.add_argument( '''--finetuning_task_name''' , type=lowerCamelCase_ , default=lowerCamelCase_ , help='''Optional fine-tuning task name if the TF model was a finetuned model.''' , ) train_parser.set_defaults(func=lowerCamelCase_ ) def __init__( self :List[Any] , lowerCamelCase_ :str , lowerCamelCase_ :str , lowerCamelCase_ :str , lowerCamelCase_ :str , lowerCamelCase_ :str , *lowerCamelCase_ :Dict , ) -> Union[str, Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[Any] = logging.get_logger('''transformers-cli/converting''' ) self._logger.info(f"Loading model {model_type}" ) SCREAMING_SNAKE_CASE : Optional[Any] = model_type SCREAMING_SNAKE_CASE : List[Any] = tf_checkpoint SCREAMING_SNAKE_CASE : List[str] = pytorch_dump_output SCREAMING_SNAKE_CASE : Optional[int] = config SCREAMING_SNAKE_CASE : str = finetuning_task_name def __lowerCAmelCase ( self :List[str] ) -> List[Any]: '''simple docstring''' if self._model_type == "albert": try: from ..models.albert.convert_albert_original_tf_checkpoint_to_pytorch import ( convert_tf_checkpoint_to_pytorch, ) except ImportError: raise ImportError(lowerCamelCase_ ) convert_tf_checkpoint_to_pytorch(self._tf_checkpoint , self._config , self._pytorch_dump_output ) elif self._model_type == "bert": try: from ..models.bert.convert_bert_original_tf_checkpoint_to_pytorch import ( convert_tf_checkpoint_to_pytorch, ) except ImportError: raise ImportError(lowerCamelCase_ ) convert_tf_checkpoint_to_pytorch(self._tf_checkpoint , self._config , self._pytorch_dump_output ) elif self._model_type == "funnel": try: from ..models.funnel.convert_funnel_original_tf_checkpoint_to_pytorch import ( convert_tf_checkpoint_to_pytorch, ) except ImportError: raise ImportError(lowerCamelCase_ ) convert_tf_checkpoint_to_pytorch(self._tf_checkpoint , self._config , self._pytorch_dump_output ) elif self._model_type == "t5": try: from ..models.ta.convert_ta_original_tf_checkpoint_to_pytorch import convert_tf_checkpoint_to_pytorch except ImportError: raise ImportError(lowerCamelCase_ ) convert_tf_checkpoint_to_pytorch(self._tf_checkpoint , self._config , self._pytorch_dump_output ) elif self._model_type == "gpt": from ..models.openai.convert_openai_original_tf_checkpoint_to_pytorch import ( convert_openai_checkpoint_to_pytorch, ) convert_openai_checkpoint_to_pytorch(self._tf_checkpoint , self._config , self._pytorch_dump_output ) elif self._model_type == "transfo_xl": try: from ..models.transfo_xl.convert_transfo_xl_original_tf_checkpoint_to_pytorch import ( convert_transfo_xl_checkpoint_to_pytorch, ) except ImportError: raise ImportError(lowerCamelCase_ ) if "ckpt" in self._tf_checkpoint.lower(): SCREAMING_SNAKE_CASE : Dict = self._tf_checkpoint SCREAMING_SNAKE_CASE : Tuple = '''''' else: SCREAMING_SNAKE_CASE : List[Any] = self._tf_checkpoint SCREAMING_SNAKE_CASE : Optional[int] = '''''' convert_transfo_xl_checkpoint_to_pytorch( lowerCamelCase_ , self._config , self._pytorch_dump_output , lowerCamelCase_ ) elif self._model_type == "gpt2": try: from ..models.gpta.convert_gpta_original_tf_checkpoint_to_pytorch import ( convert_gpta_checkpoint_to_pytorch, ) except ImportError: raise ImportError(lowerCamelCase_ ) convert_gpta_checkpoint_to_pytorch(self._tf_checkpoint , self._config , self._pytorch_dump_output ) elif self._model_type == "xlnet": try: from ..models.xlnet.convert_xlnet_original_tf_checkpoint_to_pytorch import ( convert_xlnet_checkpoint_to_pytorch, ) except ImportError: raise ImportError(lowerCamelCase_ ) convert_xlnet_checkpoint_to_pytorch( self._tf_checkpoint , self._config , self._pytorch_dump_output , self._finetuning_task_name ) elif self._model_type == "xlm": from ..models.xlm.convert_xlm_original_pytorch_checkpoint_to_pytorch import ( convert_xlm_checkpoint_to_pytorch, ) convert_xlm_checkpoint_to_pytorch(self._tf_checkpoint , self._pytorch_dump_output ) elif self._model_type == "lxmert": from ..models.lxmert.convert_lxmert_original_tf_checkpoint_to_pytorch import ( convert_lxmert_checkpoint_to_pytorch, ) convert_lxmert_checkpoint_to_pytorch(self._tf_checkpoint , self._pytorch_dump_output ) elif self._model_type == "rembert": from ..models.rembert.convert_rembert_tf_checkpoint_to_pytorch import ( convert_rembert_tf_checkpoint_to_pytorch, ) convert_rembert_tf_checkpoint_to_pytorch(self._tf_checkpoint , self._config , self._pytorch_dump_output ) else: raise ValueError( '''--model_type should be selected in the list [bert, gpt, gpt2, t5, transfo_xl, xlnet, xlm, lxmert]''' )
18
"""simple docstring""" from ...configuration_utils import PretrainedConfig from ...utils import logging from ...utils.backbone_utils import BackboneConfigMixin, get_aligned_output_features_output_indices lowerCamelCase__ : int = logging.get_logger(__name__) class lowercase__( _UpperCAmelCase , _UpperCAmelCase ): '''simple docstring''' UpperCamelCase = """maskformer-swin""" UpperCamelCase = { """num_attention_heads""": """num_heads""", """num_hidden_layers""": """num_layers""", } def __init__( self :Optional[int] , lowerCamelCase_ :List[Any]=2_24 , lowerCamelCase_ :Tuple=4 , lowerCamelCase_ :Optional[Any]=3 , lowerCamelCase_ :List[str]=96 , lowerCamelCase_ :int=[2, 2, 6, 2] , lowerCamelCase_ :Union[str, Any]=[3, 6, 12, 24] , lowerCamelCase_ :Optional[int]=7 , lowerCamelCase_ :Tuple=4.0 , lowerCamelCase_ :Tuple=True , lowerCamelCase_ :Dict=0.0 , lowerCamelCase_ :Any=0.0 , lowerCamelCase_ :List[Any]=0.1 , lowerCamelCase_ :Dict="gelu" , lowerCamelCase_ :Optional[int]=False , lowerCamelCase_ :List[str]=0.0_2 , lowerCamelCase_ :Any=1E-5 , lowerCamelCase_ :Union[str, Any]=None , lowerCamelCase_ :List[str]=None , **lowerCamelCase_ :Union[str, Any] , ) -> Dict: '''simple docstring''' super().__init__(**lowerCamelCase_ ) SCREAMING_SNAKE_CASE : int = image_size SCREAMING_SNAKE_CASE : Optional[Any] = patch_size SCREAMING_SNAKE_CASE : str = num_channels SCREAMING_SNAKE_CASE : Union[str, Any] = embed_dim SCREAMING_SNAKE_CASE : List[Any] = depths SCREAMING_SNAKE_CASE : List[str] = len(lowerCamelCase_ ) SCREAMING_SNAKE_CASE : str = num_heads SCREAMING_SNAKE_CASE : Any = window_size SCREAMING_SNAKE_CASE : List[str] = mlp_ratio SCREAMING_SNAKE_CASE : str = qkv_bias SCREAMING_SNAKE_CASE : Tuple = hidden_dropout_prob SCREAMING_SNAKE_CASE : int = attention_probs_dropout_prob SCREAMING_SNAKE_CASE : int = drop_path_rate SCREAMING_SNAKE_CASE : Tuple = hidden_act SCREAMING_SNAKE_CASE : Any = use_absolute_embeddings SCREAMING_SNAKE_CASE : List[str] = layer_norm_eps SCREAMING_SNAKE_CASE : List[str] = initializer_range # we set the hidden_size attribute in order to make Swin work with VisionEncoderDecoderModel # this indicates the channel dimension after the last stage of the model SCREAMING_SNAKE_CASE : int = int(embed_dim * 2 ** (len(lowerCamelCase_ ) - 1) ) SCREAMING_SNAKE_CASE : Dict = ['''stem'''] + [f"stage{idx}" for idx in range(1 , len(lowerCamelCase_ ) + 1 )] SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : int = get_aligned_output_features_output_indices( out_features=lowerCamelCase_ , out_indices=lowerCamelCase_ , stage_names=self.stage_names )
18
1
"""simple docstring""" from ...configuration_utils import PretrainedConfig from ...utils import logging lowerCamelCase__ : int = logging.get_logger(__name__) lowerCamelCase__ : List[Any] = { "google/canine-s": "https://huggingface.co/google/canine-s/resolve/main/config.json", # See all CANINE models at https://huggingface.co/models?filter=canine } class lowercase__( _UpperCAmelCase ): '''simple docstring''' UpperCamelCase = """canine""" def __init__( self :List[str] , lowerCamelCase_ :Dict=7_68 , lowerCamelCase_ :Tuple=12 , lowerCamelCase_ :List[Any]=12 , lowerCamelCase_ :Optional[int]=30_72 , lowerCamelCase_ :Tuple="gelu" , lowerCamelCase_ :List[Any]=0.1 , lowerCamelCase_ :Optional[Any]=0.1 , lowerCamelCase_ :List[Any]=1_63_84 , lowerCamelCase_ :List[Any]=16 , lowerCamelCase_ :Any=0.0_2 , lowerCamelCase_ :List[str]=1E-12 , lowerCamelCase_ :int=0 , lowerCamelCase_ :Optional[int]=0xE000 , lowerCamelCase_ :List[Any]=0xE001 , lowerCamelCase_ :Union[str, Any]=4 , lowerCamelCase_ :str=4 , lowerCamelCase_ :str=8 , lowerCamelCase_ :Tuple=1_63_84 , lowerCamelCase_ :Dict=1_28 , **lowerCamelCase_ :Optional[int] , ) -> int: '''simple docstring''' super().__init__(pad_token_id=lowerCamelCase_ , bos_token_id=lowerCamelCase_ , eos_token_id=lowerCamelCase_ , **lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Union[str, Any] = max_position_embeddings SCREAMING_SNAKE_CASE : List[Any] = hidden_size SCREAMING_SNAKE_CASE : str = num_hidden_layers SCREAMING_SNAKE_CASE : List[Any] = num_attention_heads SCREAMING_SNAKE_CASE : Optional[Any] = intermediate_size SCREAMING_SNAKE_CASE : Any = hidden_act SCREAMING_SNAKE_CASE : Union[str, Any] = hidden_dropout_prob SCREAMING_SNAKE_CASE : int = attention_probs_dropout_prob SCREAMING_SNAKE_CASE : List[Any] = initializer_range SCREAMING_SNAKE_CASE : Dict = type_vocab_size SCREAMING_SNAKE_CASE : Optional[Any] = layer_norm_eps # Character config: SCREAMING_SNAKE_CASE : Any = downsampling_rate SCREAMING_SNAKE_CASE : List[Any] = upsampling_kernel_size SCREAMING_SNAKE_CASE : List[Any] = num_hash_functions SCREAMING_SNAKE_CASE : Dict = num_hash_buckets SCREAMING_SNAKE_CASE : str = local_transformer_stride
18
"""simple docstring""" import math class lowercase__: '''simple docstring''' def __init__( self :Union[str, Any] , lowerCamelCase_ :List[str]=0 ) -> List[Any]: # a graph with Node 0,1,...,N-1 '''simple docstring''' SCREAMING_SNAKE_CASE : Tuple = n SCREAMING_SNAKE_CASE : List[Any] = [ [math.inf for j in range(0 , lowerCamelCase_ )] for i in range(0 , lowerCamelCase_ ) ] # adjacency matrix for weight SCREAMING_SNAKE_CASE : Any = [ [math.inf for j in range(0 , lowerCamelCase_ )] for i in range(0 , lowerCamelCase_ ) ] # dp[i][j] stores minimum distance from i to j def __lowerCAmelCase ( self :Dict , lowerCamelCase_ :Optional[int] , lowerCamelCase_ :Union[str, Any] , lowerCamelCase_ :Union[str, Any] ) -> Tuple: '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[Any] = w def __lowerCAmelCase ( self :str ) -> Union[str, Any]: '''simple docstring''' for k in range(0 , self.n ): for i in range(0 , self.n ): for j in range(0 , self.n ): SCREAMING_SNAKE_CASE : List[str] = min(self.dp[i][j] , self.dp[i][k] + self.dp[k][j] ) def __lowerCAmelCase ( self :int , lowerCamelCase_ :Union[str, Any] , lowerCamelCase_ :Optional[int] ) -> Optional[Any]: '''simple docstring''' return self.dp[u][v] if __name__ == "__main__": lowerCamelCase__ : Dict = Graph(5) graph.add_edge(0, 2, 9) graph.add_edge(0, 4, 10) graph.add_edge(1, 3, 5) graph.add_edge(2, 3, 7) graph.add_edge(3, 0, 10) graph.add_edge(3, 1, 2) graph.add_edge(3, 2, 1) graph.add_edge(3, 4, 6) graph.add_edge(4, 1, 3) graph.add_edge(4, 2, 4) graph.add_edge(4, 3, 9) graph.floyd_warshall() graph.show_min(1, 4) graph.show_min(0, 3)
18
1
"""simple docstring""" from ..utils import DummyObject, requires_backends class lowercase__( metaclass=_UpperCAmelCase ): '''simple docstring''' UpperCamelCase = ["""flax"""] def __init__( self :Any , *lowerCamelCase_ :int , **lowerCamelCase_ :List[Any] ) -> Optional[int]: '''simple docstring''' requires_backends(self , ['''flax'''] ) @classmethod def __lowerCAmelCase ( cls :Optional[int] , *lowerCamelCase_ :List[Any] , **lowerCamelCase_ :str ) -> Any: '''simple docstring''' requires_backends(cls , ['''flax'''] ) @classmethod def __lowerCAmelCase ( cls :int , *lowerCamelCase_ :Optional[Any] , **lowerCamelCase_ :str ) -> List[str]: '''simple docstring''' requires_backends(cls , ['''flax'''] ) class lowercase__( metaclass=_UpperCAmelCase ): '''simple docstring''' UpperCamelCase = ["""flax"""] def __init__( self :Dict , *lowerCamelCase_ :str , **lowerCamelCase_ :Tuple ) -> Any: '''simple docstring''' requires_backends(self , ['''flax'''] ) @classmethod def __lowerCAmelCase ( cls :Any , *lowerCamelCase_ :str , **lowerCamelCase_ :Union[str, Any] ) -> Optional[int]: '''simple docstring''' requires_backends(cls , ['''flax'''] ) @classmethod def __lowerCAmelCase ( cls :Union[str, Any] , *lowerCamelCase_ :Tuple , **lowerCamelCase_ :List[Any] ) -> Optional[int]: '''simple docstring''' requires_backends(cls , ['''flax'''] ) class lowercase__( metaclass=_UpperCAmelCase ): '''simple docstring''' UpperCamelCase = ["""flax"""] def __init__( self :int , *lowerCamelCase_ :Dict , **lowerCamelCase_ :Optional[Any] ) -> int: '''simple docstring''' requires_backends(self , ['''flax'''] ) @classmethod def __lowerCAmelCase ( cls :Optional[int] , *lowerCamelCase_ :Optional[int] , **lowerCamelCase_ :Any ) -> int: '''simple docstring''' requires_backends(cls , ['''flax'''] ) @classmethod def __lowerCAmelCase ( cls :Dict , *lowerCamelCase_ :Optional[int] , **lowerCamelCase_ :Union[str, Any] ) -> int: '''simple docstring''' requires_backends(cls , ['''flax'''] ) class lowercase__( metaclass=_UpperCAmelCase ): '''simple docstring''' UpperCamelCase = ["""flax"""] def __init__( self :List[str] , *lowerCamelCase_ :Dict , **lowerCamelCase_ :Tuple ) -> List[str]: '''simple docstring''' requires_backends(self , ['''flax'''] ) @classmethod def __lowerCAmelCase ( cls :int , *lowerCamelCase_ :Optional[int] , **lowerCamelCase_ :Union[str, Any] ) -> Dict: '''simple docstring''' requires_backends(cls , ['''flax'''] ) @classmethod def __lowerCAmelCase ( cls :Union[str, Any] , *lowerCamelCase_ :Tuple , **lowerCamelCase_ :Optional[Any] ) -> List[Any]: '''simple docstring''' requires_backends(cls , ['''flax'''] ) class lowercase__( metaclass=_UpperCAmelCase ): '''simple docstring''' UpperCamelCase = ["""flax"""] def __init__( self :Optional[Any] , *lowerCamelCase_ :int , **lowerCamelCase_ :List[Any] ) -> Optional[int]: '''simple docstring''' requires_backends(self , ['''flax'''] ) @classmethod def __lowerCAmelCase ( cls :Dict , *lowerCamelCase_ :List[str] , **lowerCamelCase_ :Union[str, Any] ) -> int: '''simple docstring''' requires_backends(cls , ['''flax'''] ) @classmethod def __lowerCAmelCase ( cls :Any , *lowerCamelCase_ :Union[str, Any] , **lowerCamelCase_ :Dict ) -> Optional[Any]: '''simple docstring''' requires_backends(cls , ['''flax'''] ) class lowercase__( metaclass=_UpperCAmelCase ): '''simple docstring''' UpperCamelCase = ["""flax"""] def __init__( self :Tuple , *lowerCamelCase_ :Optional[Any] , **lowerCamelCase_ :List[Any] ) -> int: '''simple docstring''' requires_backends(self , ['''flax'''] ) @classmethod def __lowerCAmelCase ( cls :str , *lowerCamelCase_ :List[Any] , **lowerCamelCase_ :Dict ) -> int: '''simple docstring''' requires_backends(cls , ['''flax'''] ) @classmethod def __lowerCAmelCase ( cls :Optional[int] , *lowerCamelCase_ :Union[str, Any] , **lowerCamelCase_ :Tuple ) -> Optional[Any]: '''simple docstring''' requires_backends(cls , ['''flax'''] ) class lowercase__( metaclass=_UpperCAmelCase ): '''simple docstring''' UpperCamelCase = ["""flax"""] def __init__( self :List[Any] , *lowerCamelCase_ :Optional[Any] , **lowerCamelCase_ :str ) -> int: '''simple docstring''' requires_backends(self , ['''flax'''] ) @classmethod def __lowerCAmelCase ( cls :str , *lowerCamelCase_ :List[Any] , **lowerCamelCase_ :Union[str, Any] ) -> Any: '''simple docstring''' requires_backends(cls , ['''flax'''] ) @classmethod def __lowerCAmelCase ( cls :Optional[int] , *lowerCamelCase_ :Optional[Any] , **lowerCamelCase_ :Any ) -> Union[str, Any]: '''simple docstring''' requires_backends(cls , ['''flax'''] ) class lowercase__( metaclass=_UpperCAmelCase ): '''simple docstring''' UpperCamelCase = ["""flax"""] def __init__( self :Union[str, Any] , *lowerCamelCase_ :List[str] , **lowerCamelCase_ :Any ) -> Dict: '''simple docstring''' requires_backends(self , ['''flax'''] ) @classmethod def __lowerCAmelCase ( cls :Dict , *lowerCamelCase_ :Tuple , **lowerCamelCase_ :Tuple ) -> List[str]: '''simple docstring''' requires_backends(cls , ['''flax'''] ) @classmethod def __lowerCAmelCase ( cls :Any , *lowerCamelCase_ :Optional[Any] , **lowerCamelCase_ :Optional[Any] ) -> Any: '''simple docstring''' requires_backends(cls , ['''flax'''] ) class lowercase__( metaclass=_UpperCAmelCase ): '''simple docstring''' UpperCamelCase = ["""flax"""] def __init__( self :List[str] , *lowerCamelCase_ :Optional[int] , **lowerCamelCase_ :Tuple ) -> Any: '''simple docstring''' requires_backends(self , ['''flax'''] ) @classmethod def __lowerCAmelCase ( cls :Union[str, Any] , *lowerCamelCase_ :Optional[int] , **lowerCamelCase_ :Optional[Any] ) -> Union[str, Any]: '''simple docstring''' requires_backends(cls , ['''flax'''] ) @classmethod def __lowerCAmelCase ( cls :Optional[Any] , *lowerCamelCase_ :List[str] , **lowerCamelCase_ :Tuple ) -> List[Any]: '''simple docstring''' requires_backends(cls , ['''flax'''] ) class lowercase__( metaclass=_UpperCAmelCase ): '''simple docstring''' UpperCamelCase = ["""flax"""] def __init__( self :Dict , *lowerCamelCase_ :Dict , **lowerCamelCase_ :int ) -> str: '''simple docstring''' requires_backends(self , ['''flax'''] ) @classmethod def __lowerCAmelCase ( cls :Any , *lowerCamelCase_ :Optional[int] , **lowerCamelCase_ :Union[str, Any] ) -> str: '''simple docstring''' requires_backends(cls , ['''flax'''] ) @classmethod def __lowerCAmelCase ( cls :Optional[int] , *lowerCamelCase_ :List[Any] , **lowerCamelCase_ :Any ) -> Union[str, Any]: '''simple docstring''' requires_backends(cls , ['''flax'''] ) class lowercase__( metaclass=_UpperCAmelCase ): '''simple docstring''' UpperCamelCase = ["""flax"""] def __init__( self :Optional[Any] , *lowerCamelCase_ :Dict , **lowerCamelCase_ :List[Any] ) -> List[Any]: '''simple docstring''' requires_backends(self , ['''flax'''] ) @classmethod def __lowerCAmelCase ( cls :List[str] , *lowerCamelCase_ :Union[str, Any] , **lowerCamelCase_ :Any ) -> int: '''simple docstring''' requires_backends(cls , ['''flax'''] ) @classmethod def __lowerCAmelCase ( cls :List[Any] , *lowerCamelCase_ :str , **lowerCamelCase_ :Any ) -> List[Any]: '''simple docstring''' requires_backends(cls , ['''flax'''] ) class lowercase__( metaclass=_UpperCAmelCase ): '''simple docstring''' UpperCamelCase = ["""flax"""] def __init__( self :Optional[int] , *lowerCamelCase_ :Union[str, Any] , **lowerCamelCase_ :Optional[Any] ) -> str: '''simple docstring''' requires_backends(self , ['''flax'''] ) @classmethod def __lowerCAmelCase ( cls :Any , *lowerCamelCase_ :Optional[Any] , **lowerCamelCase_ :Optional[int] ) -> List[Any]: '''simple docstring''' requires_backends(cls , ['''flax'''] ) @classmethod def __lowerCAmelCase ( cls :Any , *lowerCamelCase_ :List[str] , **lowerCamelCase_ :Tuple ) -> Dict: '''simple docstring''' requires_backends(cls , ['''flax'''] ) class lowercase__( metaclass=_UpperCAmelCase ): '''simple docstring''' UpperCamelCase = ["""flax"""] def __init__( self :Tuple , *lowerCamelCase_ :str , **lowerCamelCase_ :Any ) -> Dict: '''simple docstring''' requires_backends(self , ['''flax'''] ) @classmethod def __lowerCAmelCase ( cls :List[Any] , *lowerCamelCase_ :int , **lowerCamelCase_ :List[str] ) -> Any: '''simple docstring''' requires_backends(cls , ['''flax'''] ) @classmethod def __lowerCAmelCase ( cls :List[str] , *lowerCamelCase_ :Dict , **lowerCamelCase_ :Tuple ) -> Union[str, Any]: '''simple docstring''' requires_backends(cls , ['''flax'''] )
18
"""simple docstring""" from typing import TYPE_CHECKING from ....utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available lowerCamelCase__ : Tuple = { "configuration_mctct": ["MCTCT_PRETRAINED_CONFIG_ARCHIVE_MAP", "MCTCTConfig"], "feature_extraction_mctct": ["MCTCTFeatureExtractor"], "processing_mctct": ["MCTCTProcessor"], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCamelCase__ : Union[str, Any] = [ "MCTCT_PRETRAINED_MODEL_ARCHIVE_LIST", "MCTCTForCTC", "MCTCTModel", "MCTCTPreTrainedModel", ] if TYPE_CHECKING: from .configuration_mctct import MCTCT_PRETRAINED_CONFIG_ARCHIVE_MAP, MCTCTConfig from .feature_extraction_mctct import MCTCTFeatureExtractor from .processing_mctct import MCTCTProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_mctct import MCTCT_PRETRAINED_MODEL_ARCHIVE_LIST, MCTCTForCTC, MCTCTModel, MCTCTPreTrainedModel else: import sys lowerCamelCase__ : Tuple = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
18
1
"""simple docstring""" import warnings from ...utils import logging from .image_processing_layoutlmva import LayoutLMvaImageProcessor lowerCamelCase__ : Tuple = logging.get_logger(__name__) class lowercase__( _UpperCAmelCase ): '''simple docstring''' def __init__( self :List[Any] , *lowerCamelCase_ :Tuple , **lowerCamelCase_ :List[str] ) -> None: '''simple docstring''' warnings.warn( '''The class LayoutLMv2FeatureExtractor is deprecated and will be removed in version 5 of Transformers.''' ''' Please use LayoutLMv2ImageProcessor instead.''' , lowerCamelCase_ , ) super().__init__(*lowerCamelCase_ , **lowerCamelCase_ )
18
"""simple docstring""" import os from typing import Any, Callable, Dict, List, Optional, Tuple, Union import torch from torch import nn from ...models.controlnet import ControlNetModel, ControlNetOutput from ...models.modeling_utils import ModelMixin from ...utils import logging lowerCamelCase__ : List[Any] = logging.get_logger(__name__) class lowercase__( _UpperCAmelCase ): '''simple docstring''' def __init__( self :List[str] , lowerCamelCase_ :Union[List[ControlNetModel], Tuple[ControlNetModel]] ) -> Union[str, Any]: '''simple docstring''' super().__init__() SCREAMING_SNAKE_CASE : Optional[Any] = nn.ModuleList(lowerCamelCase_ ) def __lowerCAmelCase ( self :Any , lowerCamelCase_ :torch.FloatTensor , lowerCamelCase_ :Union[torch.Tensor, float, int] , lowerCamelCase_ :torch.Tensor , lowerCamelCase_ :List[torch.tensor] , lowerCamelCase_ :List[float] , lowerCamelCase_ :Optional[torch.Tensor] = None , lowerCamelCase_ :Optional[torch.Tensor] = None , lowerCamelCase_ :Optional[torch.Tensor] = None , lowerCamelCase_ :Optional[Dict[str, Any]] = None , lowerCamelCase_ :bool = False , lowerCamelCase_ :bool = True , ) -> Union[ControlNetOutput, Tuple]: '''simple docstring''' for i, (image, scale, controlnet) in enumerate(zip(lowerCamelCase_ , lowerCamelCase_ , self.nets ) ): SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : List[Any] = controlnet( lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , ) # merge samples if i == 0: SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : int = down_samples, mid_sample else: SCREAMING_SNAKE_CASE : Optional[int] = [ samples_prev + samples_curr for samples_prev, samples_curr in zip(lowerCamelCase_ , lowerCamelCase_ ) ] mid_block_res_sample += mid_sample return down_block_res_samples, mid_block_res_sample def __lowerCAmelCase ( self :Any , lowerCamelCase_ :Union[str, os.PathLike] , lowerCamelCase_ :bool = True , lowerCamelCase_ :Callable = None , lowerCamelCase_ :bool = False , lowerCamelCase_ :Optional[str] = None , ) -> Optional[int]: '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[Any] = 0 SCREAMING_SNAKE_CASE : Any = save_directory for controlnet in self.nets: controlnet.save_pretrained( lowerCamelCase_ , is_main_process=lowerCamelCase_ , save_function=lowerCamelCase_ , safe_serialization=lowerCamelCase_ , variant=lowerCamelCase_ , ) idx += 1 SCREAMING_SNAKE_CASE : Union[str, Any] = model_path_to_save + f"_{idx}" @classmethod def __lowerCAmelCase ( cls :Dict , lowerCamelCase_ :Optional[Union[str, os.PathLike]] , **lowerCamelCase_ :Tuple ) -> List[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : str = 0 SCREAMING_SNAKE_CASE : Optional[int] = [] # load controlnet and append to list until no controlnet directory exists anymore # first controlnet has to be saved under `./mydirectory/controlnet` to be compliant with `DiffusionPipeline.from_prertained` # second, third, ... controlnets have to be saved under `./mydirectory/controlnet_1`, `./mydirectory/controlnet_2`, ... SCREAMING_SNAKE_CASE : Dict = pretrained_model_path while os.path.isdir(lowerCamelCase_ ): SCREAMING_SNAKE_CASE : Tuple = ControlNetModel.from_pretrained(lowerCamelCase_ , **lowerCamelCase_ ) controlnets.append(lowerCamelCase_ ) idx += 1 SCREAMING_SNAKE_CASE : Union[str, Any] = pretrained_model_path + f"_{idx}" logger.info(f"{len(lowerCamelCase_ )} controlnets loaded from {pretrained_model_path}." ) if len(lowerCamelCase_ ) == 0: raise ValueError( f"No ControlNets found under {os.path.dirname(lowerCamelCase_ )}. Expected at least {pretrained_model_path + '_0'}." ) return cls(lowerCamelCase_ )
18
1
"""simple docstring""" from ...configuration_utils import PretrainedConfig from ...utils import logging lowerCamelCase__ : List[Any] = logging.get_logger(__name__) lowerCamelCase__ : Optional[int] = { "microsoft/swinv2-tiny-patch4-window8-256": ( "https://huggingface.co/microsoft/swinv2-tiny-patch4-window8-256/resolve/main/config.json" ), } class lowercase__( _UpperCAmelCase ): '''simple docstring''' UpperCamelCase = """swinv2""" UpperCamelCase = { """num_attention_heads""": """num_heads""", """num_hidden_layers""": """num_layers""", } def __init__( self :Any , lowerCamelCase_ :str=2_24 , lowerCamelCase_ :List[str]=4 , lowerCamelCase_ :Optional[int]=3 , lowerCamelCase_ :List[str]=96 , lowerCamelCase_ :Optional[int]=[2, 2, 6, 2] , lowerCamelCase_ :Any=[3, 6, 12, 24] , lowerCamelCase_ :List[str]=7 , lowerCamelCase_ :List[str]=4.0 , lowerCamelCase_ :str=True , lowerCamelCase_ :Any=0.0 , lowerCamelCase_ :List[Any]=0.0 , lowerCamelCase_ :Tuple=0.1 , lowerCamelCase_ :Dict="gelu" , lowerCamelCase_ :str=False , lowerCamelCase_ :Tuple=0.0_2 , lowerCamelCase_ :List[str]=1E-5 , lowerCamelCase_ :Dict=32 , **lowerCamelCase_ :Optional[Any] , ) -> Union[str, Any]: '''simple docstring''' super().__init__(**lowerCamelCase_ ) SCREAMING_SNAKE_CASE : int = image_size SCREAMING_SNAKE_CASE : Union[str, Any] = patch_size SCREAMING_SNAKE_CASE : int = num_channels SCREAMING_SNAKE_CASE : int = embed_dim SCREAMING_SNAKE_CASE : int = depths SCREAMING_SNAKE_CASE : List[Any] = len(lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Any = num_heads SCREAMING_SNAKE_CASE : Dict = window_size SCREAMING_SNAKE_CASE : int = mlp_ratio SCREAMING_SNAKE_CASE : Dict = qkv_bias SCREAMING_SNAKE_CASE : Dict = hidden_dropout_prob SCREAMING_SNAKE_CASE : List[Any] = attention_probs_dropout_prob SCREAMING_SNAKE_CASE : List[str] = drop_path_rate SCREAMING_SNAKE_CASE : Tuple = hidden_act SCREAMING_SNAKE_CASE : Union[str, Any] = use_absolute_embeddings SCREAMING_SNAKE_CASE : int = layer_norm_eps SCREAMING_SNAKE_CASE : List[str] = initializer_range SCREAMING_SNAKE_CASE : Tuple = encoder_stride # we set the hidden_size attribute in order to make Swinv2 work with VisionEncoderDecoderModel # this indicates the channel dimension after the last stage of the model SCREAMING_SNAKE_CASE : Union[str, Any] = int(embed_dim * 2 ** (len(lowerCamelCase_ ) - 1) ) SCREAMING_SNAKE_CASE : str = (0, 0, 0, 0)
18
"""simple docstring""" from __future__ import annotations from numpy import array, cos, cross, floataa, radians, sin from numpy.typing import NDArray def __A ( a_ : float , a_ : float , a_ : bool = False )-> list[float]: '''simple docstring''' if radian_mode: return [magnitude * cos(a_ ), magnitude * sin(a_ )] return [magnitude * cos(radians(a_ ) ), magnitude * sin(radians(a_ ) )] def __A ( a_ : NDArray[floataa] , a_ : NDArray[floataa] , a_ : float = 10**-1 )-> bool: '''simple docstring''' SCREAMING_SNAKE_CASE : NDArray[floataa] = cross(a_ , a_ ) SCREAMING_SNAKE_CASE : float = sum(a_ ) return abs(a_ ) < eps if __name__ == "__main__": # Test to check if it works lowerCamelCase__ : Optional[Any] = array( [ polar_force(7_1_8.4, 180 - 30), polar_force(8_7_9.5_4, 45), polar_force(100, -90), ] ) lowerCamelCase__ : NDArray[floataa] = array([[0, 0], [0, 0], [0, 0]]) assert in_static_equilibrium(forces, location) # Problem 1 in image_data/2D_problems.jpg lowerCamelCase__ : Union[str, Any] = array( [ polar_force(30 * 9.8_1, 15), polar_force(215, 180 - 45), polar_force(264, 90 - 30), ] ) lowerCamelCase__ : Any = array([[0, 0], [0, 0], [0, 0]]) assert in_static_equilibrium(forces, location) # Problem in image_data/2D_problems_1.jpg lowerCamelCase__ : Union[str, Any] = array([[0, -2000], [0, -1200], [0, 15600], [0, -12400]]) lowerCamelCase__ : Optional[int] = array([[0, 0], [6, 0], [10, 0], [12, 0]]) assert in_static_equilibrium(forces, location) import doctest doctest.testmod()
18
1
"""simple docstring""" from typing import Mapping from ...configuration_utils import PretrainedConfig from ...onnx import OnnxSeqaSeqConfigWithPast from ...utils import logging lowerCamelCase__ : Dict = logging.get_logger(__name__) lowerCamelCase__ : Any = { "t5-small": "https://huggingface.co/t5-small/resolve/main/config.json", "t5-base": "https://huggingface.co/t5-base/resolve/main/config.json", "t5-large": "https://huggingface.co/t5-large/resolve/main/config.json", "t5-3b": "https://huggingface.co/t5-3b/resolve/main/config.json", "t5-11b": "https://huggingface.co/t5-11b/resolve/main/config.json", } class lowercase__( _UpperCAmelCase ): '''simple docstring''' UpperCamelCase = """t5""" UpperCamelCase = ["""past_key_values"""] UpperCamelCase = {"""hidden_size""": """d_model""", """num_attention_heads""": """num_heads""", """num_hidden_layers""": """num_layers"""} def __init__( self :Optional[Any] , lowerCamelCase_ :Dict=3_21_28 , lowerCamelCase_ :Optional[Any]=5_12 , lowerCamelCase_ :str=64 , lowerCamelCase_ :int=20_48 , lowerCamelCase_ :Tuple=6 , lowerCamelCase_ :int=None , lowerCamelCase_ :int=8 , lowerCamelCase_ :Optional[Any]=32 , lowerCamelCase_ :List[Any]=1_28 , lowerCamelCase_ :Dict=0.1 , lowerCamelCase_ :Tuple=1E-6 , lowerCamelCase_ :Dict=1.0 , lowerCamelCase_ :Tuple="relu" , lowerCamelCase_ :int=True , lowerCamelCase_ :List[Any]=True , lowerCamelCase_ :Optional[Any]=0 , lowerCamelCase_ :Any=1 , **lowerCamelCase_ :Tuple , ) -> Optional[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : Dict = vocab_size SCREAMING_SNAKE_CASE : Dict = d_model SCREAMING_SNAKE_CASE : List[str] = d_kv SCREAMING_SNAKE_CASE : Tuple = d_ff SCREAMING_SNAKE_CASE : str = num_layers SCREAMING_SNAKE_CASE : List[Any] = ( num_decoder_layers if num_decoder_layers is not None else self.num_layers ) # default = symmetry SCREAMING_SNAKE_CASE : Union[str, Any] = num_heads SCREAMING_SNAKE_CASE : Any = relative_attention_num_buckets SCREAMING_SNAKE_CASE : Optional[Any] = relative_attention_max_distance SCREAMING_SNAKE_CASE : str = dropout_rate SCREAMING_SNAKE_CASE : Optional[Any] = layer_norm_epsilon SCREAMING_SNAKE_CASE : Tuple = initializer_factor SCREAMING_SNAKE_CASE : Optional[Any] = feed_forward_proj SCREAMING_SNAKE_CASE : str = use_cache SCREAMING_SNAKE_CASE : Any = self.feed_forward_proj.split('''-''' ) SCREAMING_SNAKE_CASE : List[str] = act_info[-1] SCREAMING_SNAKE_CASE : Optional[Any] = act_info[0] == '''gated''' if len(lowerCamelCase_ ) > 1 and act_info[0] != "gated" or len(lowerCamelCase_ ) > 2: raise ValueError( f"`feed_forward_proj`: {feed_forward_proj} is not a valid activation function of the dense layer." '''Please make sure `feed_forward_proj` is of the format `gated-{ACT_FN}` or `{ACT_FN}`, e.g. ''' '''\'gated-gelu\' or \'relu\'''' ) # for backwards compatibility if feed_forward_proj == "gated-gelu": SCREAMING_SNAKE_CASE : Dict = '''gelu_new''' super().__init__( pad_token_id=lowerCamelCase_ , eos_token_id=lowerCamelCase_ , is_encoder_decoder=lowerCamelCase_ , **lowerCamelCase_ , ) class lowercase__( _UpperCAmelCase ): '''simple docstring''' @property def __lowerCAmelCase ( self :List[Any] ) -> Mapping[str, Mapping[int, str]]: '''simple docstring''' SCREAMING_SNAKE_CASE : Tuple = { '''input_ids''': {0: '''batch''', 1: '''encoder_sequence'''}, '''attention_mask''': {0: '''batch''', 1: '''encoder_sequence'''}, } if self.use_past: SCREAMING_SNAKE_CASE : List[Any] = '''past_encoder_sequence + sequence''' SCREAMING_SNAKE_CASE : List[Any] = {0: '''batch'''} SCREAMING_SNAKE_CASE : Dict = {0: '''batch''', 1: '''past_decoder_sequence + sequence'''} else: SCREAMING_SNAKE_CASE : Optional[int] = {0: '''batch''', 1: '''decoder_sequence'''} SCREAMING_SNAKE_CASE : Union[str, Any] = {0: '''batch''', 1: '''decoder_sequence'''} if self.use_past: self.fill_with_past_key_values_(lowerCamelCase_ , direction='''inputs''' ) return common_inputs @property def __lowerCAmelCase ( self :Optional[int] ) -> int: '''simple docstring''' return 13
18
"""simple docstring""" from __future__ import annotations import random # Maximum size of the population. Bigger could be faster but is more memory expensive. lowerCamelCase__ : Optional[Any] = 200 # Number of elements selected in every generation of evolution. The selection takes # place from best to worst of that generation and must be smaller than N_POPULATION. lowerCamelCase__ : Optional[int] = 50 # Probability that an element of a generation can mutate, changing one of its genes. # This will guarantee that all genes will be used during evolution. lowerCamelCase__ : Optional[Any] = 0.4 # Just a seed to improve randomness required by the algorithm. random.seed(random.randint(0, 1000)) def __A ( a_ : str , a_ : str )-> tuple[str, float]: '''simple docstring''' SCREAMING_SNAKE_CASE : Tuple = len([g for position, g in enumerate(a_ ) if g == main_target[position]] ) return (item, float(a_ )) def __A ( a_ : str , a_ : str )-> tuple[str, str]: '''simple docstring''' SCREAMING_SNAKE_CASE : Tuple = random.randint(0 , len(a_ ) - 1 ) SCREAMING_SNAKE_CASE : str = parent_a[:random_slice] + parent_a[random_slice:] SCREAMING_SNAKE_CASE : Dict = parent_a[:random_slice] + parent_a[random_slice:] return (child_a, child_a) def __A ( a_ : str , a_ : list[str] )-> str: '''simple docstring''' SCREAMING_SNAKE_CASE : Any = list(a_ ) if random.uniform(0 , 1 ) < MUTATION_PROBABILITY: SCREAMING_SNAKE_CASE : Any = random.choice(a_ ) return "".join(a_ ) def __A ( a_ : tuple[str, float] , a_ : list[tuple[str, float]] , a_ : list[str] , )-> list[str]: '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[int] = [] # Generate more children proportionally to the fitness score. SCREAMING_SNAKE_CASE : List[str] = int(parent_a[1] * 1_00 ) + 1 SCREAMING_SNAKE_CASE : Optional[Any] = 10 if child_n >= 10 else child_n for _ in range(a_ ): SCREAMING_SNAKE_CASE : List[str] = population_score[random.randint(0 , a_ )][0] SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : Tuple = crossover(parent_a[0] , a_ ) # Append new string to the population list. pop.append(mutate(a_ , a_ ) ) pop.append(mutate(a_ , a_ ) ) return pop def __A ( a_ : str , a_ : list[str] , a_ : bool = True )-> tuple[int, int, str]: '''simple docstring''' if N_POPULATION < N_SELECTED: SCREAMING_SNAKE_CASE : List[Any] = F"{N_POPULATION} must be bigger than {N_SELECTED}" raise ValueError(a_ ) # Verify that the target contains no genes besides the ones inside genes variable. SCREAMING_SNAKE_CASE : List[str] = sorted({c for c in target if c not in genes} ) if not_in_genes_list: SCREAMING_SNAKE_CASE : str = F"{not_in_genes_list} is not in genes list, evolution cannot converge" raise ValueError(a_ ) # Generate random starting population. SCREAMING_SNAKE_CASE : Tuple = [] for _ in range(a_ ): population.append(''''''.join([random.choice(a_ ) for i in range(len(a_ ) )] ) ) # Just some logs to know what the algorithms is doing. SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : List[str] = 0, 0 # This loop will end when we find a perfect match for our target. while True: generation += 1 total_population += len(a_ ) # Random population created. Now it's time to evaluate. # Adding a bit of concurrency can make everything faster, # # import concurrent.futures # population_score: list[tuple[str, float]] = [] # with concurrent.futures.ThreadPoolExecutor( # max_workers=NUM_WORKERS) as executor: # futures = {executor.submit(evaluate, item) for item in population} # concurrent.futures.wait(futures) # population_score = [item.result() for item in futures] # # but with a simple algorithm like this, it will probably be slower. # We just need to call evaluate for every item inside the population. SCREAMING_SNAKE_CASE : int = [evaluate(a_ , a_ ) for item in population] # Check if there is a matching evolution. SCREAMING_SNAKE_CASE : List[Any] = sorted(a_ , key=lambda a_ : x[1] , reverse=a_ ) if population_score[0][0] == target: return (generation, total_population, population_score[0][0]) # Print the best result every 10 generation. # Just to know that the algorithm is working. if debug and generation % 10 == 0: print( F"\nGeneration: {generation}" F"\nTotal Population:{total_population}" F"\nBest score: {population_score[0][1]}" F"\nBest string: {population_score[0][0]}" ) # Flush the old population, keeping some of the best evolutions. # Keeping this avoid regression of evolution. SCREAMING_SNAKE_CASE : Optional[Any] = population[: int(N_POPULATION / 3 )] population.clear() population.extend(a_ ) # Normalize population score to be between 0 and 1. SCREAMING_SNAKE_CASE : Optional[int] = [ (item, score / len(a_ )) for item, score in population_score ] # This is selection for i in range(a_ ): population.extend(select(population_score[int(a_ )] , a_ , a_ ) ) # Check if the population has already reached the maximum value and if so, # break the cycle. If this check is disabled, the algorithm will take # forever to compute large strings, but will also calculate small strings in # a far fewer generations. if len(a_ ) > N_POPULATION: break if __name__ == "__main__": lowerCamelCase__ : Dict = ( "This is a genetic algorithm to evaluate, combine, evolve, and mutate a string!" ) lowerCamelCase__ : int = list( " ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklm" "nopqrstuvwxyz.,;!?+-*#@^'èéòà€ù=)(&%$£/\\" ) lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ : Dict = basic(target_str, genes_list) print( f'''\nGeneration: {generation}\nTotal Population: {population}\nTarget: {target}''' )
18
1
"""simple docstring""" def __A ( a_ : int , a_ : int )-> Tuple: '''simple docstring''' if b == 0: return 1 if (b % 2) == 0: return actual_power(a_ , int(b / 2 ) ) * actual_power(a_ , int(b / 2 ) ) else: return a * actual_power(a_ , int(b / 2 ) ) * actual_power(a_ , int(b / 2 ) ) def __A ( a_ : int , a_ : int )-> float: '''simple docstring''' if b < 0: return 1 / actual_power(a_ , a_ ) return actual_power(a_ , a_ ) if __name__ == "__main__": print(power(-2, -3))
18
"""simple docstring""" import argparse import ast import logging import os import sys import pandas as pd import torch from tqdm import tqdm from transformers import BartForConditionalGeneration, RagRetriever, RagSequenceForGeneration, RagTokenForGeneration from transformers import logging as transformers_logging sys.path.append(os.path.join(os.getcwd())) # noqa: E402 # isort:skip from utils_rag import exact_match_score, fa_score # noqa: E402 # isort:skip lowerCamelCase__ : Optional[Any] = logging.getLogger(__name__) logging.basicConfig(level=logging.INFO) transformers_logging.set_verbosity_info() def __A ( a_ : Optional[int] )-> Dict: '''simple docstring''' if "token" in model_name_or_path: return "rag_token" if "sequence" in model_name_or_path: return "rag_sequence" if "bart" in model_name_or_path: return "bart" return None def __A ( a_ : List[Any] , a_ : Optional[int] , a_ : Optional[int] )-> Dict: '''simple docstring''' return max(metric_fn(a_ , a_ ) for gt in ground_truths ) def __A ( a_ : List[Any] , a_ : Union[str, Any] , a_ : str )-> str: '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[int] = [line.strip() for line in open(a_ , '''r''' ).readlines()] SCREAMING_SNAKE_CASE : Optional[Any] = [] if args.gold_data_mode == "qa": SCREAMING_SNAKE_CASE : List[Any] = pd.read_csv(a_ , sep='''\t''' , header=a_ ) for answer_list in data[1]: SCREAMING_SNAKE_CASE : str = ast.literal_eval(a_ ) answers.append(a_ ) else: SCREAMING_SNAKE_CASE : Any = [line.strip() for line in open(a_ , '''r''' ).readlines()] SCREAMING_SNAKE_CASE : Dict = [[reference] for reference in references] SCREAMING_SNAKE_CASE : Dict = 0 for prediction, ground_truths in zip(a_ , a_ ): total += 1 em += metric_max_over_ground_truths(a_ , a_ , a_ ) fa += metric_max_over_ground_truths(a_ , a_ , a_ ) SCREAMING_SNAKE_CASE : Any = 100.0 * em / total SCREAMING_SNAKE_CASE : Optional[int] = 100.0 * fa / total logger.info(F"F1: {fa:.2f}" ) logger.info(F"EM: {em:.2f}" ) def __A ( a_ : Any , a_ : Any , a_ : List[Any] )-> Tuple: '''simple docstring''' SCREAMING_SNAKE_CASE : str = args.k SCREAMING_SNAKE_CASE : Tuple = [line.strip() for line in open(a_ , '''r''' ).readlines()] SCREAMING_SNAKE_CASE : Union[str, Any] = [line.strip() for line in open(a_ , '''r''' ).readlines()] SCREAMING_SNAKE_CASE : Dict = 0 for hypo, reference in zip(a_ , a_ ): SCREAMING_SNAKE_CASE : Optional[int] = set(hypo.split('''\t''' )[:k] ) SCREAMING_SNAKE_CASE : List[str] = set(reference.split('''\t''' ) ) total += 1 em += len(hypo_provenance & ref_provenance ) / k SCREAMING_SNAKE_CASE : Dict = 100.0 * em / total logger.info(F"Precision@{k}: {em: .2f}" ) def __A ( a_ : Any , a_ : List[str] , a_ : str )-> int: '''simple docstring''' def strip_title(a_ : Optional[Any] ): if title.startswith('''"''' ): SCREAMING_SNAKE_CASE : Tuple = title[1:] if title.endswith('''"''' ): SCREAMING_SNAKE_CASE : Any = title[:-1] return title SCREAMING_SNAKE_CASE : Tuple = rag_model.retriever.question_encoder_tokenizer.batch_encode_plus( a_ , return_tensors='''pt''' , padding=a_ , truncation=a_ , )['''input_ids'''].to(args.device ) SCREAMING_SNAKE_CASE : Any = rag_model.rag.question_encoder(a_ ) SCREAMING_SNAKE_CASE : Union[str, Any] = question_enc_outputs[0] SCREAMING_SNAKE_CASE : Dict = rag_model.retriever( a_ , question_enc_pool_output.cpu().detach().to(torch.floataa ).numpy() , prefix=rag_model.rag.generator.config.prefix , n_docs=rag_model.config.n_docs , return_tensors='''pt''' , ) SCREAMING_SNAKE_CASE : Any = rag_model.retriever.index.get_doc_dicts(result.doc_ids ) SCREAMING_SNAKE_CASE : Dict = [] for docs in all_docs: SCREAMING_SNAKE_CASE : List[Any] = [strip_title(a_ ) for title in docs['''title''']] provenance_strings.append('''\t'''.join(a_ ) ) return provenance_strings def __A ( a_ : List[Any] , a_ : int , a_ : str )-> Tuple: '''simple docstring''' with torch.no_grad(): SCREAMING_SNAKE_CASE : Optional[int] = rag_model.retriever.question_encoder_tokenizer.batch_encode_plus( a_ , return_tensors='''pt''' , padding=a_ , truncation=a_ ) SCREAMING_SNAKE_CASE : Dict = inputs_dict.input_ids.to(args.device ) SCREAMING_SNAKE_CASE : Any = inputs_dict.attention_mask.to(args.device ) SCREAMING_SNAKE_CASE : Tuple = rag_model.generate( # rag_model overwrites generate a_ , attention_mask=a_ , num_beams=args.num_beams , min_length=args.min_length , max_length=args.max_length , early_stopping=a_ , num_return_sequences=1 , bad_words_ids=[[0, 0]] , ) SCREAMING_SNAKE_CASE : Dict = rag_model.retriever.generator_tokenizer.batch_decode(a_ , skip_special_tokens=a_ ) if args.print_predictions: for q, a in zip(a_ , a_ ): logger.info('''Q: {} - A: {}'''.format(a_ , a_ ) ) return answers def __A ( )-> int: '''simple docstring''' SCREAMING_SNAKE_CASE : int = argparse.ArgumentParser() parser.add_argument( '''--model_type''' , choices=['''rag_sequence''', '''rag_token''', '''bart'''] , type=a_ , help=( '''RAG model type: rag_sequence, rag_token or bart, if none specified, the type is inferred from the''' ''' model_name_or_path''' ) , ) parser.add_argument( '''--index_name''' , default=a_ , choices=['''exact''', '''compressed''', '''legacy'''] , type=a_ , help='''RAG model retriever type''' , ) parser.add_argument( '''--index_path''' , default=a_ , type=a_ , help='''Path to the retrieval index''' , ) parser.add_argument('''--n_docs''' , default=5 , type=a_ , help='''Number of retrieved docs''' ) parser.add_argument( '''--model_name_or_path''' , default=a_ , type=a_ , required=a_ , help='''Path to pretrained checkpoints or model identifier from huggingface.co/models''' , ) parser.add_argument( '''--eval_mode''' , choices=['''e2e''', '''retrieval'''] , default='''e2e''' , type=a_ , help=( '''Evaluation mode, e2e calculates exact match and F1 of the downstream task, retrieval calculates''' ''' precision@k.''' ) , ) parser.add_argument('''--k''' , default=1 , type=a_ , help='''k for the precision@k calculation''' ) parser.add_argument( '''--evaluation_set''' , default=a_ , type=a_ , required=a_ , help='''Path to a file containing evaluation samples''' , ) parser.add_argument( '''--gold_data_path''' , default=a_ , type=a_ , required=a_ , help='''Path to a tab-separated file with gold samples''' , ) parser.add_argument( '''--gold_data_mode''' , default='''qa''' , type=a_ , choices=['''qa''', '''ans'''] , help=( '''Format of the gold data file''' '''qa - a single line in the following format: question [tab] answer_list''' '''ans - a single line of the gold file contains the expected answer string''' ) , ) parser.add_argument( '''--predictions_path''' , type=a_ , default='''predictions.txt''' , help='''Name of the predictions file, to be stored in the checkpoints directory''' , ) parser.add_argument( '''--eval_all_checkpoints''' , action='''store_true''' , help='''Evaluate all checkpoints starting with the same prefix as model_name ending and ending with step number''' , ) parser.add_argument( '''--eval_batch_size''' , default=8 , type=a_ , help='''Batch size per GPU/CPU for evaluation.''' , ) parser.add_argument( '''--recalculate''' , help='''Recalculate predictions even if the prediction file exists''' , action='''store_true''' , ) parser.add_argument( '''--num_beams''' , default=4 , type=a_ , help='''Number of beams to be used when generating answers''' , ) parser.add_argument('''--min_length''' , default=1 , type=a_ , help='''Min length of the generated answers''' ) parser.add_argument('''--max_length''' , default=50 , type=a_ , help='''Max length of the generated answers''' ) parser.add_argument( '''--print_predictions''' , action='''store_true''' , help='''If True, prints predictions while evaluating.''' , ) parser.add_argument( '''--print_docs''' , action='''store_true''' , help='''If True, prints docs retried while generating.''' , ) SCREAMING_SNAKE_CASE : List[str] = parser.parse_args() SCREAMING_SNAKE_CASE : Dict = torch.device('''cuda''' if torch.cuda.is_available() else '''cpu''' ) return args def __A ( a_ : Optional[Any] )-> int: '''simple docstring''' SCREAMING_SNAKE_CASE : Any = {} if args.model_type is None: SCREAMING_SNAKE_CASE : List[str] = infer_model_type(args.model_name_or_path ) assert args.model_type is not None if args.model_type.startswith('''rag''' ): SCREAMING_SNAKE_CASE : List[str] = RagTokenForGeneration if args.model_type == '''rag_token''' else RagSequenceForGeneration SCREAMING_SNAKE_CASE : Optional[Any] = args.n_docs if args.index_name is not None: SCREAMING_SNAKE_CASE : Tuple = args.index_name if args.index_path is not None: SCREAMING_SNAKE_CASE : List[Any] = args.index_path else: SCREAMING_SNAKE_CASE : str = BartForConditionalGeneration SCREAMING_SNAKE_CASE : Optional[int] = ( [f.path for f in os.scandir(args.model_name_or_path ) if f.is_dir()] if args.eval_all_checkpoints else [args.model_name_or_path] ) logger.info('''Evaluate the following checkpoints: %s''' , a_ ) SCREAMING_SNAKE_CASE : int = get_scores if args.eval_mode == '''e2e''' else get_precision_at_k SCREAMING_SNAKE_CASE : str = evaluate_batch_eae if args.eval_mode == '''e2e''' else evaluate_batch_retrieval for checkpoint in checkpoints: if os.path.exists(args.predictions_path ) and (not args.recalculate): logger.info('''Calculating metrics based on an existing predictions file: {}'''.format(args.predictions_path ) ) score_fn(a_ , args.predictions_path , args.gold_data_path ) continue logger.info('''***** Running evaluation for {} *****'''.format(a_ ) ) logger.info(''' Batch size = %d''' , args.eval_batch_size ) logger.info(''' Predictions will be stored under {}'''.format(args.predictions_path ) ) if args.model_type.startswith('''rag''' ): SCREAMING_SNAKE_CASE : Dict = RagRetriever.from_pretrained(a_ , **a_ ) SCREAMING_SNAKE_CASE : Union[str, Any] = model_class.from_pretrained(a_ , retriever=a_ , **a_ ) model.retriever.init_retrieval() else: SCREAMING_SNAKE_CASE : str = model_class.from_pretrained(a_ , **a_ ) model.to(args.device ) with open(args.evaluation_set , '''r''' ) as eval_file, open(args.predictions_path , '''w''' ) as preds_file: SCREAMING_SNAKE_CASE : Dict = [] for line in tqdm(a_ ): questions.append(line.strip() ) if len(a_ ) == args.eval_batch_size: SCREAMING_SNAKE_CASE : str = evaluate_batch_fn(a_ , a_ , a_ ) preds_file.write('''\n'''.join(a_ ) + '''\n''' ) preds_file.flush() SCREAMING_SNAKE_CASE : Union[str, Any] = [] if len(a_ ) > 0: SCREAMING_SNAKE_CASE : str = evaluate_batch_fn(a_ , a_ , a_ ) preds_file.write('''\n'''.join(a_ ) ) preds_file.flush() score_fn(a_ , args.predictions_path , args.gold_data_path ) if __name__ == "__main__": lowerCamelCase__ : List[str] = get_args() main(args)
18
1
"""simple docstring""" from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_torch_available lowerCamelCase__ : Dict = { "configuration_gpt_neo": ["GPT_NEO_PRETRAINED_CONFIG_ARCHIVE_MAP", "GPTNeoConfig", "GPTNeoOnnxConfig"], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCamelCase__ : Dict = [ "GPT_NEO_PRETRAINED_MODEL_ARCHIVE_LIST", "GPTNeoForCausalLM", "GPTNeoForQuestionAnswering", "GPTNeoForSequenceClassification", "GPTNeoForTokenClassification", "GPTNeoModel", "GPTNeoPreTrainedModel", "load_tf_weights_in_gpt_neo", ] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCamelCase__ : Dict = [ "FlaxGPTNeoForCausalLM", "FlaxGPTNeoModel", "FlaxGPTNeoPreTrainedModel", ] if TYPE_CHECKING: from .configuration_gpt_neo import GPT_NEO_PRETRAINED_CONFIG_ARCHIVE_MAP, GPTNeoConfig, GPTNeoOnnxConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_gpt_neo import ( GPT_NEO_PRETRAINED_MODEL_ARCHIVE_LIST, GPTNeoForCausalLM, GPTNeoForQuestionAnswering, GPTNeoForSequenceClassification, GPTNeoForTokenClassification, GPTNeoModel, GPTNeoPreTrainedModel, load_tf_weights_in_gpt_neo, ) try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_flax_gpt_neo import FlaxGPTNeoForCausalLM, FlaxGPTNeoModel, FlaxGPTNeoPreTrainedModel else: import sys lowerCamelCase__ : Dict = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
18
"""simple docstring""" import json import os from typing import Optional, Tuple from ...tokenization_utils import PreTrainedTokenizer from ...utils import logging lowerCamelCase__ : List[str] = logging.get_logger(__name__) lowerCamelCase__ : Optional[int] = {"vocab_file": "vocab.json"} lowerCamelCase__ : Dict = { "vocab_file": { "mgp-str": "https://huggingface.co/alibaba-damo/mgp-str-base/blob/main/vocab.json", } } lowerCamelCase__ : Optional[Any] = {"mgp-str": 27} class lowercase__( _UpperCAmelCase ): '''simple docstring''' UpperCamelCase = VOCAB_FILES_NAMES UpperCamelCase = PRETRAINED_VOCAB_FILES_MAP UpperCamelCase = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES def __init__( self :int , lowerCamelCase_ :Optional[int] , lowerCamelCase_ :Optional[int]="[GO]" , lowerCamelCase_ :int="[GO]" , lowerCamelCase_ :str="[s]" , lowerCamelCase_ :Dict="[GO]" , **lowerCamelCase_ :List[str] ) -> Tuple: '''simple docstring''' super().__init__( unk_token=lowerCamelCase_ , bos_token=lowerCamelCase_ , eos_token=lowerCamelCase_ , pad_token=lowerCamelCase_ , **lowerCamelCase_ , ) with open(lowerCamelCase_ , encoding='''utf-8''' ) as vocab_handle: SCREAMING_SNAKE_CASE : int = json.load(lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Union[str, Any] = {v: k for k, v in self.vocab.items()} @property def __lowerCAmelCase ( self :int ) -> Dict: '''simple docstring''' return len(self.vocab ) def __lowerCAmelCase ( self :List[str] ) -> Dict: '''simple docstring''' return dict(self.vocab , **self.added_tokens_encoder ) def __lowerCAmelCase ( self :List[str] , lowerCamelCase_ :Optional[Any] ) -> List[str]: '''simple docstring''' SCREAMING_SNAKE_CASE : Union[str, Any] = [] for s in text: char_tokens.extend(lowerCamelCase_ ) return char_tokens def __lowerCAmelCase ( self :Dict , lowerCamelCase_ :Tuple ) -> Optional[int]: '''simple docstring''' return self.vocab.get(lowerCamelCase_ , self.vocab.get(self.unk_token ) ) def __lowerCAmelCase ( self :List[str] , lowerCamelCase_ :Dict ) -> Optional[int]: '''simple docstring''' return self.decoder.get(lowerCamelCase_ ) def __lowerCAmelCase ( self :Optional[int] , lowerCamelCase_ :str , lowerCamelCase_ :Optional[str] = None ) -> Tuple[str]: '''simple docstring''' if not os.path.isdir(lowerCamelCase_ ): logger.error('''Vocabulary path ({}) should be a directory'''.format(lowerCamelCase_ ) ) return SCREAMING_SNAKE_CASE : List[Any] = os.path.join( lowerCamelCase_ , (filename_prefix + '''-''' if filename_prefix else '''''') + VOCAB_FILES_NAMES['''vocab_file'''] ) with open(lowerCamelCase_ , '''w''' , encoding='''utf-8''' ) as f: f.write(json.dumps(self.vocab , indent=2 , sort_keys=lowerCamelCase_ , ensure_ascii=lowerCamelCase_ ) + '''\n''' ) return (vocab_file,)
18
1
"""simple docstring""" import unittest from queue import Empty from threading import Thread from transformers import AutoTokenizer, TextIteratorStreamer, TextStreamer, is_torch_available from transformers.testing_utils import CaptureStdout, require_torch, torch_device from ..test_modeling_common import ids_tensor if is_torch_available(): import torch from transformers import AutoModelForCausalLM @require_torch class lowercase__( unittest.TestCase ): '''simple docstring''' def __lowerCAmelCase ( self :Optional[Any] ) -> Any: '''simple docstring''' SCREAMING_SNAKE_CASE : Any = AutoTokenizer.from_pretrained('''hf-internal-testing/tiny-random-gpt2''' ) SCREAMING_SNAKE_CASE : Optional[Any] = AutoModelForCausalLM.from_pretrained('''hf-internal-testing/tiny-random-gpt2''' ).to(lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Optional[Any] = -1 SCREAMING_SNAKE_CASE : Any = ids_tensor((1, 5) , vocab_size=model.config.vocab_size ).to(lowerCamelCase_ ) SCREAMING_SNAKE_CASE : List[Any] = model.generate(lowerCamelCase_ , max_new_tokens=10 , do_sample=lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Optional[int] = tokenizer.decode(greedy_ids[0] ) with CaptureStdout() as cs: SCREAMING_SNAKE_CASE : Tuple = TextStreamer(lowerCamelCase_ ) model.generate(lowerCamelCase_ , max_new_tokens=10 , do_sample=lowerCamelCase_ , streamer=lowerCamelCase_ ) # The greedy text should be printed to stdout, except for the final "\n" in the streamer SCREAMING_SNAKE_CASE : Tuple = cs.out[:-1] self.assertEqual(lowerCamelCase_ , lowerCamelCase_ ) def __lowerCAmelCase ( self :Dict ) -> Tuple: '''simple docstring''' SCREAMING_SNAKE_CASE : Dict = AutoTokenizer.from_pretrained('''hf-internal-testing/tiny-random-gpt2''' ) SCREAMING_SNAKE_CASE : Union[str, Any] = AutoModelForCausalLM.from_pretrained('''hf-internal-testing/tiny-random-gpt2''' ).to(lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Tuple = -1 SCREAMING_SNAKE_CASE : Dict = ids_tensor((1, 5) , vocab_size=model.config.vocab_size ).to(lowerCamelCase_ ) SCREAMING_SNAKE_CASE : List[Any] = model.generate(lowerCamelCase_ , max_new_tokens=10 , do_sample=lowerCamelCase_ ) SCREAMING_SNAKE_CASE : List[Any] = tokenizer.decode(greedy_ids[0] ) SCREAMING_SNAKE_CASE : Union[str, Any] = TextIteratorStreamer(lowerCamelCase_ ) SCREAMING_SNAKE_CASE : List[Any] = {'''input_ids''': input_ids, '''max_new_tokens''': 10, '''do_sample''': False, '''streamer''': streamer} SCREAMING_SNAKE_CASE : Optional[int] = Thread(target=model.generate , kwargs=lowerCamelCase_ ) thread.start() SCREAMING_SNAKE_CASE : Union[str, Any] = '''''' for new_text in streamer: streamer_text += new_text self.assertEqual(lowerCamelCase_ , lowerCamelCase_ ) def __lowerCAmelCase ( self :List[str] ) -> str: '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[Any] = AutoTokenizer.from_pretrained('''hf-internal-testing/tiny-random-gpt2''' ) SCREAMING_SNAKE_CASE : Tuple = AutoModelForCausalLM.from_pretrained('''hf-internal-testing/tiny-random-gpt2''' ).to(lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Dict = -1 SCREAMING_SNAKE_CASE : List[str] = ids_tensor((1, 5) , vocab_size=model.config.vocab_size ).to(lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Optional[Any] = model.generate(lowerCamelCase_ , max_new_tokens=10 , do_sample=lowerCamelCase_ ) SCREAMING_SNAKE_CASE : str = greedy_ids[:, input_ids.shape[1] :] SCREAMING_SNAKE_CASE : List[str] = tokenizer.decode(new_greedy_ids[0] ) with CaptureStdout() as cs: SCREAMING_SNAKE_CASE : int = TextStreamer(lowerCamelCase_ , skip_prompt=lowerCamelCase_ ) model.generate(lowerCamelCase_ , max_new_tokens=10 , do_sample=lowerCamelCase_ , streamer=lowerCamelCase_ ) # The greedy text should be printed to stdout, except for the final "\n" in the streamer SCREAMING_SNAKE_CASE : List[str] = cs.out[:-1] self.assertEqual(lowerCamelCase_ , lowerCamelCase_ ) def __lowerCAmelCase ( self :Union[str, Any] ) -> Union[str, Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : Any = AutoTokenizer.from_pretrained('''distilgpt2''' ) SCREAMING_SNAKE_CASE : Tuple = AutoModelForCausalLM.from_pretrained('''distilgpt2''' ).to(lowerCamelCase_ ) SCREAMING_SNAKE_CASE : List[str] = -1 SCREAMING_SNAKE_CASE : str = torch.ones((1, 5) , device=lowerCamelCase_ ).long() * model.config.bos_token_id with CaptureStdout() as cs: SCREAMING_SNAKE_CASE : Tuple = TextStreamer(lowerCamelCase_ , skip_special_tokens=lowerCamelCase_ ) model.generate(lowerCamelCase_ , max_new_tokens=1 , do_sample=lowerCamelCase_ , streamer=lowerCamelCase_ ) # The prompt contains a special token, so the streamer should not print it. As such, the output text, when # re-tokenized, must only contain one token SCREAMING_SNAKE_CASE : str = cs.out[:-1] # Remove the final "\n" SCREAMING_SNAKE_CASE : int = tokenizer(lowerCamelCase_ , return_tensors='''pt''' ) self.assertEqual(streamer_text_tokenized.input_ids.shape , (1, 1) ) def __lowerCAmelCase ( self :Any ) -> List[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : List[Any] = AutoTokenizer.from_pretrained('''hf-internal-testing/tiny-random-gpt2''' ) SCREAMING_SNAKE_CASE : Dict = AutoModelForCausalLM.from_pretrained('''hf-internal-testing/tiny-random-gpt2''' ).to(lowerCamelCase_ ) SCREAMING_SNAKE_CASE : List[str] = -1 SCREAMING_SNAKE_CASE : int = ids_tensor((1, 5) , vocab_size=model.config.vocab_size ).to(lowerCamelCase_ ) SCREAMING_SNAKE_CASE : List[Any] = TextIteratorStreamer(lowerCamelCase_ , timeout=0.0_0_1 ) SCREAMING_SNAKE_CASE : Tuple = {'''input_ids''': input_ids, '''max_new_tokens''': 10, '''do_sample''': False, '''streamer''': streamer} SCREAMING_SNAKE_CASE : Optional[Any] = Thread(target=model.generate , kwargs=lowerCamelCase_ ) thread.start() # The streamer will timeout after 0.001 seconds, so an exception will be raised with self.assertRaises(lowerCamelCase_ ): SCREAMING_SNAKE_CASE : List[Any] = '''''' for new_text in streamer: streamer_text += new_text
18
"""simple docstring""" from ...configuration_utils import PretrainedConfig from ...utils import logging lowerCamelCase__ : Tuple = logging.get_logger(__name__) lowerCamelCase__ : str = { "studio-ousia/luke-base": "https://huggingface.co/studio-ousia/luke-base/resolve/main/config.json", "studio-ousia/luke-large": "https://huggingface.co/studio-ousia/luke-large/resolve/main/config.json", } class lowercase__( _UpperCAmelCase ): '''simple docstring''' UpperCamelCase = """luke""" def __init__( self :Optional[int] , lowerCamelCase_ :Union[str, Any]=5_02_67 , lowerCamelCase_ :int=50_00_00 , lowerCamelCase_ :Tuple=7_68 , lowerCamelCase_ :List[str]=2_56 , lowerCamelCase_ :Dict=12 , lowerCamelCase_ :Optional[int]=12 , lowerCamelCase_ :Optional[Any]=30_72 , lowerCamelCase_ :List[Any]="gelu" , lowerCamelCase_ :Any=0.1 , lowerCamelCase_ :Union[str, Any]=0.1 , lowerCamelCase_ :List[str]=5_12 , lowerCamelCase_ :Optional[int]=2 , lowerCamelCase_ :Tuple=0.0_2 , lowerCamelCase_ :Optional[int]=1E-12 , lowerCamelCase_ :List[Any]=True , lowerCamelCase_ :List[str]=None , lowerCamelCase_ :str=1 , lowerCamelCase_ :Any=0 , lowerCamelCase_ :str=2 , **lowerCamelCase_ :List[Any] , ) -> Optional[int]: '''simple docstring''' super().__init__(pad_token_id=lowerCamelCase_ , bos_token_id=lowerCamelCase_ , eos_token_id=lowerCamelCase_ , **lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Dict = vocab_size SCREAMING_SNAKE_CASE : List[str] = entity_vocab_size SCREAMING_SNAKE_CASE : str = hidden_size SCREAMING_SNAKE_CASE : Union[str, Any] = entity_emb_size SCREAMING_SNAKE_CASE : List[Any] = num_hidden_layers SCREAMING_SNAKE_CASE : Optional[int] = num_attention_heads SCREAMING_SNAKE_CASE : Optional[Any] = hidden_act SCREAMING_SNAKE_CASE : Optional[Any] = intermediate_size SCREAMING_SNAKE_CASE : Union[str, Any] = hidden_dropout_prob SCREAMING_SNAKE_CASE : Dict = attention_probs_dropout_prob SCREAMING_SNAKE_CASE : Dict = max_position_embeddings SCREAMING_SNAKE_CASE : Tuple = type_vocab_size SCREAMING_SNAKE_CASE : str = initializer_range SCREAMING_SNAKE_CASE : Dict = layer_norm_eps SCREAMING_SNAKE_CASE : Optional[int] = use_entity_aware_attention SCREAMING_SNAKE_CASE : List[str] = classifier_dropout
18
1
"""simple docstring""" 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, ) lowerCamelCase__ : Union[str, Any] = logging.get_logger(__name__) # pylint: disable=invalid-name lowerCamelCase__ : Dict = "\n Examples:\n ```py\n >>> from diffusers import KandinskyV22Pipeline, KandinskyV22PriorPipeline\n >>> import torch\n\n >>> pipe_prior = KandinskyV22PriorPipeline.from_pretrained(\"kandinsky-community/kandinsky-2-2-prior\")\n >>> pipe_prior.to(\"cuda\")\n >>> prompt = \"red cat, 4k photo\"\n >>> out = pipe_prior(prompt)\n >>> image_emb = out.image_embeds\n >>> zero_image_emb = out.negative_image_embeds\n >>> pipe = KandinskyV22Pipeline.from_pretrained(\"kandinsky-community/kandinsky-2-2-decoder\")\n >>> pipe.to(\"cuda\")\n >>> image = pipe(\n ... image_embeds=image_emb,\n ... negative_image_embeds=zero_image_emb,\n ... height=768,\n ... width=768,\n ... num_inference_steps=50,\n ... ).images\n >>> image[0].save(\"cat.png\")\n ```\n" def __A ( a_ : Any , a_ : Tuple , a_ : Optional[int]=8 )-> List[str]: '''simple docstring''' SCREAMING_SNAKE_CASE : Union[str, Any] = height // scale_factor**2 if height % scale_factor**2 != 0: new_height += 1 SCREAMING_SNAKE_CASE : str = width // scale_factor**2 if width % scale_factor**2 != 0: new_width += 1 return new_height * scale_factor, new_width * scale_factor class lowercase__( _UpperCAmelCase ): '''simple docstring''' def __init__( self :Optional[int] , lowerCamelCase_ :UNetaDConditionModel , lowerCamelCase_ :DDPMScheduler , lowerCamelCase_ :VQModel , ) -> Tuple: '''simple docstring''' super().__init__() self.register_modules( unet=lowerCamelCase_ , scheduler=lowerCamelCase_ , movq=lowerCamelCase_ , ) SCREAMING_SNAKE_CASE : str = 2 ** (len(self.movq.config.block_out_channels ) - 1) def __lowerCAmelCase ( self :Optional[Any] , lowerCamelCase_ :Union[str, Any] , lowerCamelCase_ :Optional[int] , lowerCamelCase_ :int , lowerCamelCase_ :Union[str, Any] , lowerCamelCase_ :str , lowerCamelCase_ :int ) -> Optional[int]: '''simple docstring''' if latents is None: SCREAMING_SNAKE_CASE : Union[str, Any] = randn_tensor(lowerCamelCase_ , generator=lowerCamelCase_ , device=lowerCamelCase_ , dtype=lowerCamelCase_ ) else: if latents.shape != shape: raise ValueError(f"Unexpected latents shape, got {latents.shape}, expected {shape}" ) SCREAMING_SNAKE_CASE : List[str] = latents.to(lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Union[str, Any] = latents * scheduler.init_noise_sigma return latents def __lowerCAmelCase ( self :Optional[int] , lowerCamelCase_ :Optional[Any]=0 ) -> List[Any]: '''simple docstring''' if is_accelerate_available(): from accelerate import cpu_offload else: raise ImportError('''Please install accelerate via `pip install accelerate`''' ) SCREAMING_SNAKE_CASE : Union[str, Any] = torch.device(f"cuda:{gpu_id}" ) SCREAMING_SNAKE_CASE : str = [ self.unet, self.movq, ] for cpu_offloaded_model in models: if cpu_offloaded_model is not None: cpu_offload(lowerCamelCase_ , lowerCamelCase_ ) def __lowerCAmelCase ( self :Tuple , lowerCamelCase_ :Union[str, Any]=0 ) -> Union[str, Any]: '''simple docstring''' 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.''' ) SCREAMING_SNAKE_CASE : Any = torch.device(f"cuda:{gpu_id}" ) if self.device.type != "cpu": self.to('''cpu''' , silence_dtype_warnings=lowerCamelCase_ ) torch.cuda.empty_cache() # otherwise we don't see the memory savings (but they probably exist) SCREAMING_SNAKE_CASE : str = None for cpu_offloaded_model in [self.unet, self.movq]: SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : Dict = cpu_offload_with_hook(lowerCamelCase_ , lowerCamelCase_ , prev_module_hook=lowerCamelCase_ ) # We'll offload the last model manually. SCREAMING_SNAKE_CASE : str = hook @property # Copied from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion.StableDiffusionPipeline._execution_device def __lowerCAmelCase ( self :Optional[Any] ) -> Dict: '''simple docstring''' if not hasattr(self.unet , '''_hf_hook''' ): return self.device for module in self.unet.modules(): if ( hasattr(lowerCamelCase_ , '''_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(lowerCamelCase_ ) def __call__( self :Union[str, Any] , lowerCamelCase_ :Union[torch.FloatTensor, List[torch.FloatTensor]] , lowerCamelCase_ :Union[torch.FloatTensor, List[torch.FloatTensor]] , lowerCamelCase_ :int = 5_12 , lowerCamelCase_ :int = 5_12 , lowerCamelCase_ :int = 1_00 , lowerCamelCase_ :float = 4.0 , lowerCamelCase_ :int = 1 , lowerCamelCase_ :Optional[Union[torch.Generator, List[torch.Generator]]] = None , lowerCamelCase_ :Optional[torch.FloatTensor] = None , lowerCamelCase_ :Optional[str] = "pil" , lowerCamelCase_ :bool = True , ) -> str: '''simple docstring''' SCREAMING_SNAKE_CASE : Any = self._execution_device SCREAMING_SNAKE_CASE : Optional[int] = guidance_scale > 1.0 if isinstance(lowerCamelCase_ , lowerCamelCase_ ): SCREAMING_SNAKE_CASE : Optional[Any] = torch.cat(lowerCamelCase_ , dim=0 ) SCREAMING_SNAKE_CASE : Optional[int] = image_embeds.shape[0] * num_images_per_prompt if isinstance(lowerCamelCase_ , lowerCamelCase_ ): SCREAMING_SNAKE_CASE : List[str] = torch.cat(lowerCamelCase_ , dim=0 ) if do_classifier_free_guidance: SCREAMING_SNAKE_CASE : Optional[int] = image_embeds.repeat_interleave(lowerCamelCase_ , dim=0 ) SCREAMING_SNAKE_CASE : str = negative_image_embeds.repeat_interleave(lowerCamelCase_ , dim=0 ) SCREAMING_SNAKE_CASE : Any = torch.cat([negative_image_embeds, image_embeds] , dim=0 ).to(dtype=self.unet.dtype , device=lowerCamelCase_ ) self.scheduler.set_timesteps(lowerCamelCase_ , device=lowerCamelCase_ ) SCREAMING_SNAKE_CASE : str = self.scheduler.timesteps SCREAMING_SNAKE_CASE : Optional[Any] = self.unet.config.in_channels SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : List[str] = downscale_height_and_width(lowerCamelCase_ , lowerCamelCase_ , self.movq_scale_factor ) # create initial latent SCREAMING_SNAKE_CASE : Any = self.prepare_latents( (batch_size, num_channels_latents, height, width) , image_embeds.dtype , lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , self.scheduler , ) for i, t in enumerate(self.progress_bar(lowerCamelCase_ ) ): # expand the latents if we are doing classifier free guidance SCREAMING_SNAKE_CASE : int = torch.cat([latents] * 2 ) if do_classifier_free_guidance else latents SCREAMING_SNAKE_CASE : Union[str, Any] = {'''image_embeds''': image_embeds} SCREAMING_SNAKE_CASE : Optional[int] = self.unet( sample=lowerCamelCase_ , timestep=lowerCamelCase_ , encoder_hidden_states=lowerCamelCase_ , added_cond_kwargs=lowerCamelCase_ , return_dict=lowerCamelCase_ , )[0] if do_classifier_free_guidance: SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : List[Any] = noise_pred.split(latents.shape[1] , dim=1 ) SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : List[str] = noise_pred.chunk(2 ) SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : Any = variance_pred.chunk(2 ) SCREAMING_SNAKE_CASE : Tuple = noise_pred_uncond + guidance_scale * (noise_pred_text - noise_pred_uncond) SCREAMING_SNAKE_CASE : Union[str, Any] = 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"] ): SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : Optional[Any] = noise_pred.split(latents.shape[1] , dim=1 ) # compute the previous noisy sample x_t -> x_t-1 SCREAMING_SNAKE_CASE : int = self.scheduler.step( lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , generator=lowerCamelCase_ , )[0] # post-processing SCREAMING_SNAKE_CASE : int = self.movq.decode(lowerCamelCase_ , force_not_quantize=lowerCamelCase_ )['''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"]: SCREAMING_SNAKE_CASE : Union[str, Any] = image * 0.5 + 0.5 SCREAMING_SNAKE_CASE : Dict = image.clamp(0 , 1 ) SCREAMING_SNAKE_CASE : List[Any] = image.cpu().permute(0 , 2 , 3 , 1 ).float().numpy() if output_type == "pil": SCREAMING_SNAKE_CASE : str = self.numpy_to_pil(lowerCamelCase_ ) if not return_dict: return (image,) return ImagePipelineOutput(images=lowerCamelCase_ )
18
"""simple docstring""" def __A ( a_ : list , a_ : int , a_ : int = 0 , a_ : int = 0 )-> int: '''simple docstring''' SCREAMING_SNAKE_CASE : str = right or len(a_ ) - 1 if left > right: return -1 elif list_data[left] == key: return left elif list_data[right] == key: return right else: return search(a_ , a_ , left + 1 , right - 1 ) if __name__ == "__main__": import doctest doctest.testmod()
18
1
"""simple docstring""" import argparse from argparse import Namespace import torch from torch import nn from transformers import XGLMConfig, XGLMForCausalLM def __A ( a_ : List[Any] )-> Any: '''simple docstring''' SCREAMING_SNAKE_CASE : Dict = [ '''decoder.version''', '''decoder.output_projection.weight''', '''_float_tensor''', '''decoder.embed_positions._float_tensor''', ] for k in ignore_keys: state_dict.pop(a_ , a_ ) def __A ( a_ : Optional[Any] )-> str: '''simple docstring''' SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : Union[str, Any] = emb.weight.shape SCREAMING_SNAKE_CASE : int = nn.Linear(a_ , a_ , bias=a_ ) SCREAMING_SNAKE_CASE : Any = emb.weight.data return lin_layer def __A ( a_ : List[Any] )-> int: '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[int] = torch.load(a_ , map_location='''cpu''' ) SCREAMING_SNAKE_CASE : Any = Namespace(**checkpoint['''cfg''']['''model'''] ) SCREAMING_SNAKE_CASE : List[Any] = checkpoint['''model'''] remove_ignore_keys_(a_ ) SCREAMING_SNAKE_CASE : Tuple = state_dict['''decoder.embed_tokens.weight'''].shape[0] SCREAMING_SNAKE_CASE : Union[str, Any] = {key.replace('''decoder''' , '''model''' ): val for key, val in state_dict.items()} SCREAMING_SNAKE_CASE : Tuple = XGLMConfig( vocab_size=a_ , max_position_embeddings=args.max_target_positions , num_layers=args.decoder_layers , attention_heads=args.decoder_attention_heads , ffn_dim=args.decoder_ffn_embed_dim , d_model=args.decoder_embed_dim , layerdrop=args.decoder_layerdrop , dropout=args.dropout , attention_dropout=args.attention_dropout , activation_dropout=args.activation_dropout , activation_function='''gelu''' , scale_embedding=not args.no_scale_embedding , tie_word_embeddings=args.share_decoder_input_output_embed , ) SCREAMING_SNAKE_CASE : Optional[int] = XGLMForCausalLM(a_ ) SCREAMING_SNAKE_CASE : str = model.load_state_dict(a_ , strict=a_ ) print(a_ ) SCREAMING_SNAKE_CASE : Union[str, Any] = make_linear_from_emb(model.model.embed_tokens ) return model if __name__ == "__main__": lowerCamelCase__ : str = argparse.ArgumentParser() # Required parameters parser.add_argument("fairseq_path", type=str, help="path to a model.pt on local filesystem.") parser.add_argument("pytorch_dump_folder_path", default=None, type=str, help="Path to the output PyTorch model.") lowerCamelCase__ : Optional[int] = parser.parse_args() lowerCamelCase__ : int = convert_fairseq_xglm_checkpoint_from_disk(args.fairseq_path) model.save_pretrained(args.pytorch_dump_folder_path)
18
"""simple docstring""" def __A ( a_ : int )-> list[int]: '''simple docstring''' if num <= 0: raise ValueError('''Input must be a positive integer''' ) SCREAMING_SNAKE_CASE : Optional[int] = [True] * (num + 1) SCREAMING_SNAKE_CASE : Optional[Any] = 2 while p * p <= num: if primes[p]: for i in range(p * p , num + 1 , a_ ): SCREAMING_SNAKE_CASE : Any = False p += 1 return [prime for prime in range(2 , num + 1 ) if primes[prime]] if __name__ == "__main__": import doctest doctest.testmod() lowerCamelCase__ : str = int(input("Enter a positive integer: ").strip()) print(prime_sieve_eratosthenes(user_num))
18
1
"""simple docstring""" import argparse import json import requests import timm import torch from huggingface_hub import hf_hub_download from PIL import Image from transformers import AutoImageProcessor, SwinConfig, SwinForImageClassification def __A ( a_ : str )-> Any: '''simple docstring''' SCREAMING_SNAKE_CASE : str = SwinConfig() SCREAMING_SNAKE_CASE : Optional[Any] = swin_name.split('''_''' ) SCREAMING_SNAKE_CASE : Optional[Any] = name_split[1] SCREAMING_SNAKE_CASE : str = int(name_split[4] ) SCREAMING_SNAKE_CASE : Optional[int] = int(name_split[3][-1] ) if model_size == "tiny": SCREAMING_SNAKE_CASE : List[Any] = 96 SCREAMING_SNAKE_CASE : Dict = (2, 2, 6, 2) SCREAMING_SNAKE_CASE : List[Any] = (3, 6, 12, 24) elif model_size == "small": SCREAMING_SNAKE_CASE : Tuple = 96 SCREAMING_SNAKE_CASE : Union[str, Any] = (2, 2, 18, 2) SCREAMING_SNAKE_CASE : Union[str, Any] = (3, 6, 12, 24) elif model_size == "base": SCREAMING_SNAKE_CASE : List[Any] = 1_28 SCREAMING_SNAKE_CASE : Optional[int] = (2, 2, 18, 2) SCREAMING_SNAKE_CASE : Any = (4, 8, 16, 32) else: SCREAMING_SNAKE_CASE : List[str] = 1_92 SCREAMING_SNAKE_CASE : Dict = (2, 2, 18, 2) SCREAMING_SNAKE_CASE : int = (6, 12, 24, 48) if "in22k" in swin_name: SCREAMING_SNAKE_CASE : Optional[int] = 2_18_41 else: SCREAMING_SNAKE_CASE : Optional[int] = 10_00 SCREAMING_SNAKE_CASE : List[str] = '''huggingface/label-files''' SCREAMING_SNAKE_CASE : Union[str, Any] = '''imagenet-1k-id2label.json''' SCREAMING_SNAKE_CASE : Any = json.load(open(hf_hub_download(a_ , a_ , repo_type='''dataset''' ) , '''r''' ) ) SCREAMING_SNAKE_CASE : Union[str, Any] = {int(a_ ): v for k, v in idalabel.items()} SCREAMING_SNAKE_CASE : Union[str, Any] = idalabel SCREAMING_SNAKE_CASE : Optional[Any] = {v: k for k, v in idalabel.items()} SCREAMING_SNAKE_CASE : Optional[Any] = img_size SCREAMING_SNAKE_CASE : Union[str, Any] = num_classes SCREAMING_SNAKE_CASE : Tuple = embed_dim SCREAMING_SNAKE_CASE : Optional[Any] = depths SCREAMING_SNAKE_CASE : int = num_heads SCREAMING_SNAKE_CASE : Optional[int] = window_size return config def __A ( a_ : str )-> Optional[Any]: '''simple docstring''' if "patch_embed.proj" in name: SCREAMING_SNAKE_CASE : int = name.replace('''patch_embed.proj''' , '''embeddings.patch_embeddings.projection''' ) if "patch_embed.norm" in name: SCREAMING_SNAKE_CASE : Optional[Any] = name.replace('''patch_embed.norm''' , '''embeddings.norm''' ) if "layers" in name: SCREAMING_SNAKE_CASE : str = '''encoder.''' + name if "attn.proj" in name: SCREAMING_SNAKE_CASE : Tuple = name.replace('''attn.proj''' , '''attention.output.dense''' ) if "attn" in name: SCREAMING_SNAKE_CASE : str = name.replace('''attn''' , '''attention.self''' ) if "norm1" in name: SCREAMING_SNAKE_CASE : int = name.replace('''norm1''' , '''layernorm_before''' ) if "norm2" in name: SCREAMING_SNAKE_CASE : Optional[int] = name.replace('''norm2''' , '''layernorm_after''' ) if "mlp.fc1" in name: SCREAMING_SNAKE_CASE : Union[str, Any] = name.replace('''mlp.fc1''' , '''intermediate.dense''' ) if "mlp.fc2" in name: SCREAMING_SNAKE_CASE : List[str] = name.replace('''mlp.fc2''' , '''output.dense''' ) if name == "norm.weight": SCREAMING_SNAKE_CASE : Optional[Any] = '''layernorm.weight''' if name == "norm.bias": SCREAMING_SNAKE_CASE : Optional[int] = '''layernorm.bias''' if "head" in name: SCREAMING_SNAKE_CASE : Tuple = name.replace('''head''' , '''classifier''' ) else: SCREAMING_SNAKE_CASE : Tuple = '''swin.''' + name return name def __A ( a_ : Any , a_ : Any )-> str: '''simple docstring''' for key in orig_state_dict.copy().keys(): SCREAMING_SNAKE_CASE : List[Any] = orig_state_dict.pop(a_ ) if "mask" in key: continue elif "qkv" in key: SCREAMING_SNAKE_CASE : List[str] = key.split('''.''' ) SCREAMING_SNAKE_CASE : Optional[Any] = int(key_split[1] ) SCREAMING_SNAKE_CASE : Any = int(key_split[3] ) SCREAMING_SNAKE_CASE : int = model.swin.encoder.layers[layer_num].blocks[block_num].attention.self.all_head_size if "weight" in key: SCREAMING_SNAKE_CASE : Any = val[:dim, :] SCREAMING_SNAKE_CASE : Optional[int] = val[ dim : dim * 2, : ] SCREAMING_SNAKE_CASE : Any = val[-dim:, :] else: SCREAMING_SNAKE_CASE : List[Any] = val[ :dim ] SCREAMING_SNAKE_CASE : List[Any] = val[ dim : dim * 2 ] SCREAMING_SNAKE_CASE : str = val[ -dim: ] else: SCREAMING_SNAKE_CASE : List[Any] = val return orig_state_dict def __A ( a_ : Optional[int] , a_ : str )-> Optional[int]: '''simple docstring''' SCREAMING_SNAKE_CASE : Union[str, Any] = timm.create_model(a_ , pretrained=a_ ) timm_model.eval() SCREAMING_SNAKE_CASE : Any = get_swin_config(a_ ) SCREAMING_SNAKE_CASE : Any = SwinForImageClassification(a_ ) model.eval() SCREAMING_SNAKE_CASE : Optional[int] = convert_state_dict(timm_model.state_dict() , a_ ) model.load_state_dict(a_ ) SCREAMING_SNAKE_CASE : List[str] = '''http://images.cocodataset.org/val2017/000000039769.jpg''' SCREAMING_SNAKE_CASE : Tuple = AutoImageProcessor.from_pretrained('''microsoft/{}'''.format(swin_name.replace('''_''' , '''-''' ) ) ) SCREAMING_SNAKE_CASE : Optional[Any] = Image.open(requests.get(a_ , stream=a_ ).raw ) SCREAMING_SNAKE_CASE : int = image_processor(images=a_ , return_tensors='''pt''' ) SCREAMING_SNAKE_CASE : str = timm_model(inputs['''pixel_values'''] ) SCREAMING_SNAKE_CASE : List[str] = model(**a_ ).logits assert torch.allclose(a_ , a_ , atol=1E-3 ) print(F"Saving model {swin_name} to {pytorch_dump_folder_path}" ) model.save_pretrained(a_ ) print(F"Saving image processor to {pytorch_dump_folder_path}" ) image_processor.save_pretrained(a_ ) if __name__ == "__main__": lowerCamelCase__ : Optional[Any] = argparse.ArgumentParser() # Required parameters parser.add_argument( "--swin_name", default="swin_tiny_patch4_window7_224", type=str, help="Name of the Swin timm model you'd like to convert.", ) parser.add_argument( "--pytorch_dump_folder_path", default=None, type=str, help="Path to the output PyTorch model directory." ) lowerCamelCase__ : List[Any] = parser.parse_args() convert_swin_checkpoint(args.swin_name, args.pytorch_dump_folder_path)
18
"""simple docstring""" from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_tf_available, is_tokenizers_available, is_torch_available, ) lowerCamelCase__ : Optional[Any] = { "configuration_funnel": ["FUNNEL_PRETRAINED_CONFIG_ARCHIVE_MAP", "FunnelConfig"], "convert_funnel_original_tf_checkpoint_to_pytorch": [], "tokenization_funnel": ["FunnelTokenizer"], } try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCamelCase__ : Optional[Any] = ["FunnelTokenizerFast"] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCamelCase__ : Dict = [ "FUNNEL_PRETRAINED_MODEL_ARCHIVE_LIST", "FunnelBaseModel", "FunnelForMaskedLM", "FunnelForMultipleChoice", "FunnelForPreTraining", "FunnelForQuestionAnswering", "FunnelForSequenceClassification", "FunnelForTokenClassification", "FunnelModel", "FunnelPreTrainedModel", "load_tf_weights_in_funnel", ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCamelCase__ : Tuple = [ "TF_FUNNEL_PRETRAINED_MODEL_ARCHIVE_LIST", "TFFunnelBaseModel", "TFFunnelForMaskedLM", "TFFunnelForMultipleChoice", "TFFunnelForPreTraining", "TFFunnelForQuestionAnswering", "TFFunnelForSequenceClassification", "TFFunnelForTokenClassification", "TFFunnelModel", "TFFunnelPreTrainedModel", ] if TYPE_CHECKING: from .configuration_funnel import FUNNEL_PRETRAINED_CONFIG_ARCHIVE_MAP, FunnelConfig from .tokenization_funnel import FunnelTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_funnel_fast import FunnelTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_funnel import ( FUNNEL_PRETRAINED_MODEL_ARCHIVE_LIST, FunnelBaseModel, FunnelForMaskedLM, FunnelForMultipleChoice, FunnelForPreTraining, FunnelForQuestionAnswering, FunnelForSequenceClassification, FunnelForTokenClassification, FunnelModel, FunnelPreTrainedModel, load_tf_weights_in_funnel, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_funnel import ( TF_FUNNEL_PRETRAINED_MODEL_ARCHIVE_LIST, TFFunnelBaseModel, TFFunnelForMaskedLM, TFFunnelForMultipleChoice, TFFunnelForPreTraining, TFFunnelForQuestionAnswering, TFFunnelForSequenceClassification, TFFunnelForTokenClassification, TFFunnelModel, TFFunnelPreTrainedModel, ) else: import sys lowerCamelCase__ : Optional[int] = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
18
1
"""simple docstring""" lowerCamelCase__ : List[Any] = [ "Audio", "Array2D", "Array3D", "Array4D", "Array5D", "ClassLabel", "Features", "Sequence", "Value", "Image", "Translation", "TranslationVariableLanguages", ] from .audio import Audio from .features import ArrayaD, ArrayaD, ArrayaD, ArrayaD, ClassLabel, Features, Sequence, Value from .image import Image from .translation import Translation, TranslationVariableLanguages
18
"""simple docstring""" import os import sys lowerCamelCase__ : List[Any] = os.path.join(os.path.dirname(__file__), "src") sys.path.append(SRC_DIR) from transformers import ( AutoConfig, AutoModel, AutoModelForCausalLM, AutoModelForMaskedLM, AutoModelForQuestionAnswering, AutoModelForSequenceClassification, AutoTokenizer, add_start_docstrings, ) lowerCamelCase__ : str = [ "torch", "numpy", "tokenizers", "filelock", "requests", "tqdm", "regex", "sentencepiece", "sacremoses", "importlib_metadata", "huggingface_hub", ] @add_start_docstrings(AutoConfig.__doc__ ) def __A ( *a_ : Any , **a_ : Union[str, Any] )-> Dict: '''simple docstring''' return AutoConfig.from_pretrained(*a_ , **a_ ) @add_start_docstrings(AutoTokenizer.__doc__ ) def __A ( *a_ : str , **a_ : Union[str, Any] )-> Union[str, Any]: '''simple docstring''' return AutoTokenizer.from_pretrained(*a_ , **a_ ) @add_start_docstrings(AutoModel.__doc__ ) def __A ( *a_ : List[str] , **a_ : int )-> Dict: '''simple docstring''' return AutoModel.from_pretrained(*a_ , **a_ ) @add_start_docstrings(AutoModelForCausalLM.__doc__ ) def __A ( *a_ : Any , **a_ : Tuple )-> Dict: '''simple docstring''' return AutoModelForCausalLM.from_pretrained(*a_ , **a_ ) @add_start_docstrings(AutoModelForMaskedLM.__doc__ ) def __A ( *a_ : Dict , **a_ : Optional[Any] )-> Optional[int]: '''simple docstring''' return AutoModelForMaskedLM.from_pretrained(*a_ , **a_ ) @add_start_docstrings(AutoModelForSequenceClassification.__doc__ ) def __A ( *a_ : Optional[int] , **a_ : str )-> Optional[int]: '''simple docstring''' return AutoModelForSequenceClassification.from_pretrained(*a_ , **a_ ) @add_start_docstrings(AutoModelForQuestionAnswering.__doc__ ) def __A ( *a_ : List[str] , **a_ : int )-> List[Any]: '''simple docstring''' return AutoModelForQuestionAnswering.from_pretrained(*a_ , **a_ )
18
1
"""simple docstring""" def __A ( a_ : str )-> str: '''simple docstring''' SCREAMING_SNAKE_CASE : int = 0 # if input_string is "aba" than new_input_string become "a|b|a" SCREAMING_SNAKE_CASE : Any = '''''' SCREAMING_SNAKE_CASE : str = '''''' # append each character + "|" in new_string for range(0, length-1) for i in input_string[: len(a_ ) - 1]: new_input_string += i + "|" # append last character new_input_string += input_string[-1] # we will store the starting and ending of previous furthest ending palindromic # substring SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : int = 0, 0 # length[i] shows the length of palindromic substring with center i SCREAMING_SNAKE_CASE : str = [1 for i in range(len(a_ ) )] # for each character in new_string find corresponding palindromic string SCREAMING_SNAKE_CASE : Optional[Any] = 0 for j in range(len(a_ ) ): SCREAMING_SNAKE_CASE : str = 1 if j > r else min(length[l + r - j] // 2 , r - j + 1 ) while ( j - k >= 0 and j + k < len(a_ ) and new_input_string[k + j] == new_input_string[j - k] ): k += 1 SCREAMING_SNAKE_CASE : Dict = 2 * k - 1 # does this string is ending after the previously explored end (that is r) ? # if yes the update the new r to the last index of this if j + k - 1 > r: SCREAMING_SNAKE_CASE : Tuple = j - k + 1 # noqa: E741 SCREAMING_SNAKE_CASE : List[str] = j + k - 1 # update max_length and start position if max_length < length[j]: SCREAMING_SNAKE_CASE : Optional[int] = length[j] SCREAMING_SNAKE_CASE : List[str] = j # create that string SCREAMING_SNAKE_CASE : Optional[Any] = new_input_string[start - max_length // 2 : start + max_length // 2 + 1] for i in s: if i != "|": output_string += i return output_string if __name__ == "__main__": import doctest doctest.testmod()
18
"""simple docstring""" import math from typing import Optional import numpy as np from ...configuration_utils import PretrainedConfig from ...utils import logging lowerCamelCase__ : Tuple = logging.get_logger(__name__) lowerCamelCase__ : Any = { "facebook/encodec_24khz": "https://huggingface.co/facebook/encodec_24khz/resolve/main/config.json", "facebook/encodec_48khz": "https://huggingface.co/facebook/encodec_48khz/resolve/main/config.json", } class lowercase__( _UpperCAmelCase ): '''simple docstring''' UpperCamelCase = """encodec""" def __init__( self :List[str] , lowerCamelCase_ :Tuple=[1.5, 3.0, 6.0, 1_2.0, 2_4.0] , lowerCamelCase_ :str=2_40_00 , lowerCamelCase_ :Any=1 , lowerCamelCase_ :List[Any]=False , lowerCamelCase_ :Optional[int]=None , lowerCamelCase_ :Optional[Any]=None , lowerCamelCase_ :str=1_28 , lowerCamelCase_ :Any=32 , lowerCamelCase_ :int=1 , lowerCamelCase_ :Dict=[8, 5, 4, 2] , lowerCamelCase_ :List[Any]="weight_norm" , lowerCamelCase_ :Optional[int]=7 , lowerCamelCase_ :Tuple=7 , lowerCamelCase_ :Optional[Any]=3 , lowerCamelCase_ :int=2 , lowerCamelCase_ :Dict=True , lowerCamelCase_ :Optional[int]="reflect" , lowerCamelCase_ :Optional[int]=2 , lowerCamelCase_ :Union[str, Any]=2 , lowerCamelCase_ :Dict=1.0 , lowerCamelCase_ :Any=10_24 , lowerCamelCase_ :str=None , lowerCamelCase_ :Union[str, Any]=True , **lowerCamelCase_ :Optional[int] , ) -> Dict: '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[Any] = target_bandwidths SCREAMING_SNAKE_CASE : List[str] = sampling_rate SCREAMING_SNAKE_CASE : Tuple = audio_channels SCREAMING_SNAKE_CASE : Tuple = normalize SCREAMING_SNAKE_CASE : str = chunk_length_s SCREAMING_SNAKE_CASE : List[str] = overlap SCREAMING_SNAKE_CASE : int = hidden_size SCREAMING_SNAKE_CASE : Optional[int] = num_filters SCREAMING_SNAKE_CASE : Tuple = num_residual_layers SCREAMING_SNAKE_CASE : List[Any] = upsampling_ratios SCREAMING_SNAKE_CASE : Optional[int] = norm_type SCREAMING_SNAKE_CASE : Any = kernel_size SCREAMING_SNAKE_CASE : Union[str, Any] = last_kernel_size SCREAMING_SNAKE_CASE : Tuple = residual_kernel_size SCREAMING_SNAKE_CASE : Any = dilation_growth_rate SCREAMING_SNAKE_CASE : Optional[int] = use_causal_conv SCREAMING_SNAKE_CASE : str = pad_mode SCREAMING_SNAKE_CASE : List[Any] = compress SCREAMING_SNAKE_CASE : Optional[Any] = num_lstm_layers SCREAMING_SNAKE_CASE : Dict = trim_right_ratio SCREAMING_SNAKE_CASE : List[Any] = codebook_size SCREAMING_SNAKE_CASE : Union[str, Any] = codebook_dim if codebook_dim is not None else hidden_size SCREAMING_SNAKE_CASE : Union[str, Any] = use_conv_shortcut if self.norm_type not in ["weight_norm", "time_group_norm"]: raise ValueError( f"self.norm_type must be one of `\"weight_norm\"`, `\"time_group_norm\"`), got {self.norm_type}" ) super().__init__(**lowerCamelCase_ ) @property def __lowerCAmelCase ( self :Optional[Any] ) -> Optional[int]: '''simple docstring''' if self.chunk_length_s is None: return None else: return int(self.chunk_length_s * self.sampling_rate ) @property def __lowerCAmelCase ( self :List[str] ) -> Optional[int]: '''simple docstring''' if self.chunk_length_s is None or self.overlap is None: return None else: return max(1 , int((1.0 - self.overlap) * self.chunk_length ) ) @property def __lowerCAmelCase ( self :Dict ) -> int: '''simple docstring''' SCREAMING_SNAKE_CASE : Tuple = np.prod(self.upsampling_ratios ) return math.ceil(self.sampling_rate / hop_length ) @property def __lowerCAmelCase ( self :Dict ) -> int: '''simple docstring''' return int(10_00 * self.target_bandwidths[-1] // (self.frame_rate * 10) )
18
1
"""simple docstring""" from __future__ import annotations from fractions import Fraction def __A ( a_ : int , a_ : int )-> bool: '''simple docstring''' return ( num != den and num % 10 == den // 10 and (num // 10) / (den % 10) == num / den ) def __A ( a_ : int )-> list[str]: '''simple docstring''' SCREAMING_SNAKE_CASE : Union[str, Any] = [] SCREAMING_SNAKE_CASE : List[str] = 11 SCREAMING_SNAKE_CASE : Union[str, Any] = int('''1''' + '''0''' * digit_len ) for num in range(a_ , a_ ): while den <= 99: if (num != den) and (num % 10 == den // 10) and (den % 10 != 0): if is_digit_cancelling(a_ , a_ ): solutions.append(F"{num}/{den}" ) den += 1 num += 1 SCREAMING_SNAKE_CASE : Optional[Any] = 10 return solutions def __A ( a_ : int = 2 )-> int: '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[Any] = 1.0 for fraction in fraction_list(a_ ): SCREAMING_SNAKE_CASE : List[str] = Fraction(a_ ) result *= frac.denominator / frac.numerator return int(a_ ) if __name__ == "__main__": print(solution())
18
"""simple docstring""" import copy import unittest from transformers.models.auto import get_values from transformers.testing_utils import require_torch, 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, random_attention_mask from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from transformers import ( MODEL_FOR_MULTIPLE_CHOICE_MAPPING, MODEL_FOR_QUESTION_ANSWERING_MAPPING, MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING, MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING, LayoutLMvaConfig, LayoutLMvaForQuestionAnswering, LayoutLMvaForSequenceClassification, LayoutLMvaForTokenClassification, LayoutLMvaModel, ) from transformers.models.layoutlmva.modeling_layoutlmva import LAYOUTLMV3_PRETRAINED_MODEL_ARCHIVE_LIST if is_vision_available(): from PIL import Image from transformers import LayoutLMvaImageProcessor class lowercase__: '''simple docstring''' def __init__( self :Tuple , lowerCamelCase_ :Tuple , lowerCamelCase_ :Union[str, Any]=2 , lowerCamelCase_ :Any=3 , lowerCamelCase_ :Union[str, Any]=4 , lowerCamelCase_ :List[str]=2 , lowerCamelCase_ :str=7 , lowerCamelCase_ :Tuple=True , lowerCamelCase_ :Dict=True , lowerCamelCase_ :int=True , lowerCamelCase_ :List[Any]=True , lowerCamelCase_ :Optional[Any]=99 , lowerCamelCase_ :Any=36 , lowerCamelCase_ :Any=3 , lowerCamelCase_ :str=4 , lowerCamelCase_ :Tuple=37 , lowerCamelCase_ :Optional[int]="gelu" , lowerCamelCase_ :str=0.1 , lowerCamelCase_ :Optional[int]=0.1 , lowerCamelCase_ :Tuple=5_12 , lowerCamelCase_ :Optional[Any]=16 , lowerCamelCase_ :List[str]=2 , lowerCamelCase_ :Optional[int]=0.0_2 , lowerCamelCase_ :int=6 , lowerCamelCase_ :str=6 , lowerCamelCase_ :Optional[Any]=3 , lowerCamelCase_ :Union[str, Any]=4 , lowerCamelCase_ :List[Any]=None , lowerCamelCase_ :Tuple=10_00 , ) -> Union[str, Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : Dict = parent SCREAMING_SNAKE_CASE : Optional[Any] = batch_size SCREAMING_SNAKE_CASE : List[str] = num_channels SCREAMING_SNAKE_CASE : str = image_size SCREAMING_SNAKE_CASE : Optional[int] = patch_size SCREAMING_SNAKE_CASE : Tuple = text_seq_length SCREAMING_SNAKE_CASE : Optional[int] = is_training SCREAMING_SNAKE_CASE : Dict = use_input_mask SCREAMING_SNAKE_CASE : Any = use_token_type_ids SCREAMING_SNAKE_CASE : List[Any] = use_labels SCREAMING_SNAKE_CASE : List[Any] = vocab_size SCREAMING_SNAKE_CASE : Optional[Any] = hidden_size SCREAMING_SNAKE_CASE : Tuple = num_hidden_layers SCREAMING_SNAKE_CASE : Optional[int] = num_attention_heads SCREAMING_SNAKE_CASE : Optional[int] = intermediate_size SCREAMING_SNAKE_CASE : List[str] = hidden_act SCREAMING_SNAKE_CASE : Any = hidden_dropout_prob SCREAMING_SNAKE_CASE : Optional[Any] = attention_probs_dropout_prob SCREAMING_SNAKE_CASE : Union[str, Any] = max_position_embeddings SCREAMING_SNAKE_CASE : int = type_vocab_size SCREAMING_SNAKE_CASE : Union[str, Any] = type_sequence_label_size SCREAMING_SNAKE_CASE : Optional[Any] = initializer_range SCREAMING_SNAKE_CASE : Dict = coordinate_size SCREAMING_SNAKE_CASE : List[Any] = shape_size SCREAMING_SNAKE_CASE : Dict = num_labels SCREAMING_SNAKE_CASE : Union[str, Any] = num_choices SCREAMING_SNAKE_CASE : List[str] = scope SCREAMING_SNAKE_CASE : Optional[int] = range_bbox # LayoutLMv3's sequence length equals the number of text tokens + number of patches + 1 (we add 1 for the CLS token) SCREAMING_SNAKE_CASE : str = text_seq_length SCREAMING_SNAKE_CASE : int = (image_size // patch_size) ** 2 + 1 SCREAMING_SNAKE_CASE : Optional[Any] = self.text_seq_length + self.image_seq_length def __lowerCAmelCase ( self :List[Any] ) -> Optional[int]: '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[int] = ids_tensor([self.batch_size, self.text_seq_length] , self.vocab_size ) SCREAMING_SNAKE_CASE : List[Any] = ids_tensor([self.batch_size, self.text_seq_length, 4] , self.range_bbox ) # Ensure that bbox is legal for i in range(bbox.shape[0] ): for j in range(bbox.shape[1] ): if bbox[i, j, 3] < bbox[i, j, 1]: SCREAMING_SNAKE_CASE : str = bbox[i, j, 3] SCREAMING_SNAKE_CASE : List[str] = bbox[i, j, 1] SCREAMING_SNAKE_CASE : Any = t if bbox[i, j, 2] < bbox[i, j, 0]: SCREAMING_SNAKE_CASE : Any = bbox[i, j, 2] SCREAMING_SNAKE_CASE : Any = bbox[i, j, 0] SCREAMING_SNAKE_CASE : Optional[Any] = t SCREAMING_SNAKE_CASE : Dict = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) SCREAMING_SNAKE_CASE : Optional[Any] = None if self.use_input_mask: SCREAMING_SNAKE_CASE : Dict = random_attention_mask([self.batch_size, self.text_seq_length] ) SCREAMING_SNAKE_CASE : Any = None if self.use_token_type_ids: SCREAMING_SNAKE_CASE : Tuple = ids_tensor([self.batch_size, self.text_seq_length] , self.type_vocab_size ) SCREAMING_SNAKE_CASE : Tuple = None SCREAMING_SNAKE_CASE : Optional[int] = None if self.use_labels: SCREAMING_SNAKE_CASE : List[str] = ids_tensor([self.batch_size] , self.type_sequence_label_size ) SCREAMING_SNAKE_CASE : Dict = ids_tensor([self.batch_size, self.text_seq_length] , self.num_labels ) SCREAMING_SNAKE_CASE : List[str] = 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 __lowerCAmelCase ( self :int , lowerCamelCase_ :Union[str, Any] , lowerCamelCase_ :int , lowerCamelCase_ :str , lowerCamelCase_ :int , lowerCamelCase_ :Union[str, Any] , lowerCamelCase_ :Any , lowerCamelCase_ :Dict , lowerCamelCase_ :Union[str, Any] ) -> Any: '''simple docstring''' SCREAMING_SNAKE_CASE : Dict = LayoutLMvaModel(config=lowerCamelCase_ ) model.to(lowerCamelCase_ ) model.eval() # text + image SCREAMING_SNAKE_CASE : Optional[Any] = model(lowerCamelCase_ , pixel_values=lowerCamelCase_ ) SCREAMING_SNAKE_CASE : List[str] = model( lowerCamelCase_ , bbox=lowerCamelCase_ , pixel_values=lowerCamelCase_ , attention_mask=lowerCamelCase_ , token_type_ids=lowerCamelCase_ ) SCREAMING_SNAKE_CASE : List[Any] = model(lowerCamelCase_ , bbox=lowerCamelCase_ , pixel_values=lowerCamelCase_ , token_type_ids=lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Optional[Any] = model(lowerCamelCase_ , bbox=lowerCamelCase_ , pixel_values=lowerCamelCase_ ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) # text only SCREAMING_SNAKE_CASE : List[str] = model(lowerCamelCase_ ) self.parent.assertEqual( result.last_hidden_state.shape , (self.batch_size, self.text_seq_length, self.hidden_size) ) # image only SCREAMING_SNAKE_CASE : List[str] = model(pixel_values=lowerCamelCase_ ) self.parent.assertEqual( result.last_hidden_state.shape , (self.batch_size, self.image_seq_length, self.hidden_size) ) def __lowerCAmelCase ( self :str , lowerCamelCase_ :Union[str, Any] , lowerCamelCase_ :Union[str, Any] , lowerCamelCase_ :Tuple , lowerCamelCase_ :List[str] , lowerCamelCase_ :Any , lowerCamelCase_ :int , lowerCamelCase_ :List[Any] , lowerCamelCase_ :List[str] ) -> Tuple: '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[Any] = self.num_labels SCREAMING_SNAKE_CASE : Dict = LayoutLMvaForSequenceClassification(lowerCamelCase_ ) model.to(lowerCamelCase_ ) model.eval() SCREAMING_SNAKE_CASE : int = model( lowerCamelCase_ , bbox=lowerCamelCase_ , pixel_values=lowerCamelCase_ , attention_mask=lowerCamelCase_ , token_type_ids=lowerCamelCase_ , labels=lowerCamelCase_ , ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_labels) ) def __lowerCAmelCase ( self :Union[str, Any] , lowerCamelCase_ :Tuple , lowerCamelCase_ :Any , lowerCamelCase_ :Optional[int] , lowerCamelCase_ :Any , lowerCamelCase_ :Optional[int] , lowerCamelCase_ :int , lowerCamelCase_ :str , lowerCamelCase_ :Any ) -> Union[str, Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : int = self.num_labels SCREAMING_SNAKE_CASE : int = LayoutLMvaForTokenClassification(config=lowerCamelCase_ ) model.to(lowerCamelCase_ ) model.eval() SCREAMING_SNAKE_CASE : Optional[Any] = model( lowerCamelCase_ , bbox=lowerCamelCase_ , pixel_values=lowerCamelCase_ , attention_mask=lowerCamelCase_ , token_type_ids=lowerCamelCase_ , labels=lowerCamelCase_ , ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.text_seq_length, self.num_labels) ) def __lowerCAmelCase ( self :Optional[Any] , lowerCamelCase_ :Dict , lowerCamelCase_ :Dict , lowerCamelCase_ :Dict , lowerCamelCase_ :int , lowerCamelCase_ :List[str] , lowerCamelCase_ :int , lowerCamelCase_ :str , lowerCamelCase_ :Optional[int] ) -> Union[str, Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[int] = LayoutLMvaForQuestionAnswering(config=lowerCamelCase_ ) model.to(lowerCamelCase_ ) model.eval() SCREAMING_SNAKE_CASE : List[Any] = model( lowerCamelCase_ , bbox=lowerCamelCase_ , pixel_values=lowerCamelCase_ , attention_mask=lowerCamelCase_ , token_type_ids=lowerCamelCase_ , start_positions=lowerCamelCase_ , end_positions=lowerCamelCase_ , ) self.parent.assertEqual(result.start_logits.shape , (self.batch_size, self.seq_length) ) self.parent.assertEqual(result.end_logits.shape , (self.batch_size, self.seq_length) ) def __lowerCAmelCase ( self :Optional[int] ) -> Optional[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[int] = self.prepare_config_and_inputs() ( ( SCREAMING_SNAKE_CASE ), ( SCREAMING_SNAKE_CASE ), ( SCREAMING_SNAKE_CASE ), ( SCREAMING_SNAKE_CASE ), ( SCREAMING_SNAKE_CASE ), ( SCREAMING_SNAKE_CASE ), ( SCREAMING_SNAKE_CASE ), ( SCREAMING_SNAKE_CASE ), ) : Tuple = config_and_inputs SCREAMING_SNAKE_CASE : Dict = { '''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_torch class lowercase__( _UpperCAmelCase , _UpperCAmelCase , unittest.TestCase ): '''simple docstring''' UpperCamelCase = False UpperCamelCase = False UpperCamelCase = False UpperCamelCase = ( ( LayoutLMvaModel, LayoutLMvaForSequenceClassification, LayoutLMvaForTokenClassification, LayoutLMvaForQuestionAnswering, ) if is_torch_available() else () ) UpperCamelCase = ( {"""document-question-answering""": LayoutLMvaForQuestionAnswering, """feature-extraction""": LayoutLMvaModel} if is_torch_available() else {} ) def __lowerCAmelCase ( self :int , lowerCamelCase_ :Union[str, Any] , lowerCamelCase_ :str , lowerCamelCase_ :str , lowerCamelCase_ :Optional[Any] , lowerCamelCase_ :List[str] ) -> Union[str, Any]: '''simple docstring''' return True def __lowerCAmelCase ( self :List[str] ) -> Union[str, Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : str = LayoutLMvaModelTester(self ) SCREAMING_SNAKE_CASE : List[Any] = ConfigTester(self , config_class=lowerCamelCase_ , hidden_size=37 ) def __lowerCAmelCase ( self :Any , lowerCamelCase_ :Dict , lowerCamelCase_ :List[str] , lowerCamelCase_ :str=False ) -> Tuple: '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[Any] = copy.deepcopy(lowerCamelCase_ ) if model_class in get_values(lowerCamelCase_ ): SCREAMING_SNAKE_CASE : Tuple = { k: v.unsqueeze(1 ).expand(-1 , self.model_tester.num_choices , -1 ).contiguous() if isinstance(lowerCamelCase_ , torch.Tensor ) and v.ndim > 1 else v for k, v in inputs_dict.items() } if return_labels: if model_class in get_values(lowerCamelCase_ ): SCREAMING_SNAKE_CASE : Union[str, Any] = torch.ones(self.model_tester.batch_size , dtype=torch.long , device=lowerCamelCase_ ) elif model_class in get_values(lowerCamelCase_ ): SCREAMING_SNAKE_CASE : List[Any] = torch.zeros( self.model_tester.batch_size , dtype=torch.long , device=lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Any = torch.zeros( self.model_tester.batch_size , dtype=torch.long , device=lowerCamelCase_ ) elif model_class in [ *get_values(lowerCamelCase_ ), ]: SCREAMING_SNAKE_CASE : str = torch.zeros( self.model_tester.batch_size , dtype=torch.long , device=lowerCamelCase_ ) elif model_class in [ *get_values(lowerCamelCase_ ), ]: SCREAMING_SNAKE_CASE : Dict = torch.zeros( (self.model_tester.batch_size, self.model_tester.text_seq_length) , dtype=torch.long , device=lowerCamelCase_ , ) return inputs_dict def __lowerCAmelCase ( self :Optional[Any] ) -> Optional[int]: '''simple docstring''' self.config_tester.run_common_tests() def __lowerCAmelCase ( self :str ) -> List[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : int = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*lowerCamelCase_ ) def __lowerCAmelCase ( self :Any ) -> List[str]: '''simple docstring''' SCREAMING_SNAKE_CASE : int = self.model_tester.prepare_config_and_inputs() for type in ["absolute", "relative_key", "relative_key_query"]: SCREAMING_SNAKE_CASE : str = type self.model_tester.create_and_check_model(*lowerCamelCase_ ) def __lowerCAmelCase ( self :Union[str, Any] ) -> str: '''simple docstring''' SCREAMING_SNAKE_CASE : List[Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_sequence_classification(*lowerCamelCase_ ) def __lowerCAmelCase ( self :str ) -> Any: '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_token_classification(*lowerCamelCase_ ) def __lowerCAmelCase ( self :List[str] ) -> Union[str, Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : int = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_question_answering(*lowerCamelCase_ ) @slow def __lowerCAmelCase ( self :int ) -> Union[str, Any]: '''simple docstring''' for model_name in LAYOUTLMV3_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: SCREAMING_SNAKE_CASE : Optional[int] = LayoutLMvaModel.from_pretrained(lowerCamelCase_ ) self.assertIsNotNone(lowerCamelCase_ ) def __A ( )-> Optional[int]: '''simple docstring''' SCREAMING_SNAKE_CASE : Tuple = Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png''' ) return image @require_torch class lowercase__( unittest.TestCase ): '''simple docstring''' @cached_property def __lowerCAmelCase ( self :str ) -> int: '''simple docstring''' return LayoutLMvaImageProcessor(apply_ocr=lowerCamelCase_ ) if is_vision_available() else None @slow def __lowerCAmelCase ( self :Optional[Any] ) -> Dict: '''simple docstring''' SCREAMING_SNAKE_CASE : Union[str, Any] = LayoutLMvaModel.from_pretrained('''microsoft/layoutlmv3-base''' ).to(lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Any = self.default_image_processor SCREAMING_SNAKE_CASE : List[Any] = prepare_img() SCREAMING_SNAKE_CASE : Tuple = image_processor(images=lowerCamelCase_ , return_tensors='''pt''' ).pixel_values.to(lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Optional[Any] = torch.tensor([[1, 2]] ) SCREAMING_SNAKE_CASE : Dict = torch.tensor([[1, 2, 3, 4], [5, 6, 7, 8]] ).unsqueeze(0 ) # forward pass SCREAMING_SNAKE_CASE : Tuple = model( input_ids=input_ids.to(lowerCamelCase_ ) , bbox=bbox.to(lowerCamelCase_ ) , pixel_values=pixel_values.to(lowerCamelCase_ ) , ) # verify the logits SCREAMING_SNAKE_CASE : Union[str, Any] = torch.Size((1, 1_99, 7_68) ) self.assertEqual(outputs.last_hidden_state.shape , lowerCamelCase_ ) SCREAMING_SNAKE_CASE : List[Any] = torch.tensor( [[-0.0_5_2_9, 0.3_6_1_8, 0.1_6_3_2], [-0.1_5_8_7, -0.1_6_6_7, -0.0_4_0_0], [-0.1_5_5_7, -0.1_6_7_1, -0.0_5_0_5]] ).to(lowerCamelCase_ ) self.assertTrue(torch.allclose(outputs.last_hidden_state[0, :3, :3] , lowerCamelCase_ , atol=1E-4 ) )
18
1
"""simple docstring""" import collections import os from shutil import copyfile from typing import Any, Dict, List, Optional, Tuple from ...tokenization_utils import PreTrainedTokenizer from ...utils import logging lowerCamelCase__ : Any = logging.get_logger(__name__) lowerCamelCase__ : int = "▁" lowerCamelCase__ : str = {"vocab_file": "prophetnet.tokenizer"} lowerCamelCase__ : Optional[int] = { "vocab_file": { "microsoft/xprophetnet-large-wiki100-cased": ( "https://huggingface.co/microsoft/xprophetnet-large-wiki100-cased/resolve/main/prophetnet.tokenizer" ), } } lowerCamelCase__ : List[Any] = { "microsoft/xprophetnet-large-wiki100-cased": {"do_lower_case": False}, } lowerCamelCase__ : Tuple = { "microsoft/xprophetnet-large-wiki100-cased": 512, } def __A ( a_ : Optional[int] )-> int: '''simple docstring''' SCREAMING_SNAKE_CASE : Dict = collections.OrderedDict() with open(a_ , '''r''' , encoding='''utf-8''' ) as reader: SCREAMING_SNAKE_CASE : int = reader.readlines() for index, token in enumerate(a_ ): SCREAMING_SNAKE_CASE : List[str] = token.rstrip('''\n''' ) SCREAMING_SNAKE_CASE : int = index return vocab class lowercase__( _UpperCAmelCase ): '''simple docstring''' UpperCamelCase = VOCAB_FILES_NAMES UpperCamelCase = PRETRAINED_VOCAB_FILES_MAP UpperCamelCase = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES UpperCamelCase = ["""input_ids""", """attention_mask"""] def __init__( self :str , lowerCamelCase_ :int , lowerCamelCase_ :Tuple="[SEP]" , lowerCamelCase_ :Optional[int]="[SEP]" , lowerCamelCase_ :str="[SEP]" , lowerCamelCase_ :Union[str, Any]="[UNK]" , lowerCamelCase_ :Tuple="[PAD]" , lowerCamelCase_ :List[Any]="[CLS]" , lowerCamelCase_ :int="[MASK]" , lowerCamelCase_ :Optional[Dict[str, Any]] = None , **lowerCamelCase_ :Dict , ) -> None: '''simple docstring''' SCREAMING_SNAKE_CASE : Tuple = {} if sp_model_kwargs is None else sp_model_kwargs super().__init__( bos_token=lowerCamelCase_ , eos_token=lowerCamelCase_ , sep_token=lowerCamelCase_ , unk_token=lowerCamelCase_ , pad_token=lowerCamelCase_ , cls_token=lowerCamelCase_ , mask_token=lowerCamelCase_ , sp_model_kwargs=self.sp_model_kwargs , **lowerCamelCase_ , ) try: import sentencepiece as spm except ImportError: logger.warning( '''You need to install SentencePiece to use XLMRobertaTokenizer: https://github.com/google/sentencepiece''' ''' pip install sentencepiece''' ) raise SCREAMING_SNAKE_CASE : Optional[Any] = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(str(lowerCamelCase_ ) ) SCREAMING_SNAKE_CASE : int = vocab_file # Original fairseq vocab and spm vocab must be "aligned": # Vocab | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 # -------- | ------- | ------- | ------ | ------- | --- | --- | --- | ----- | ----- | ---- # fairseq | '<s>' | '<pad>' | '</s>' | '<unk>' | ',' | '.' | '▁' | 's' | '▁de' | '-' # spm | '<unk>' | '<s>' | '</s>' | ',' | '.' | '▁' | 's' | '▁de' | '-' | '▁a' # put special tokens and [unused] tokens into the vocab SCREAMING_SNAKE_CASE : str = {'''[PAD]''': 0, '''[CLS]''': 1, '''[SEP]''': 2, '''[UNK]''': 3, '''[MASK]''': 4} for i in range(10 ): SCREAMING_SNAKE_CASE : Union[str, Any] = f"[unused{i}]" SCREAMING_SNAKE_CASE : str = 5 + i # The first "real" token "," has position 15 in the embedding vocab and position 3 in the spm vocab SCREAMING_SNAKE_CASE : str = 12 SCREAMING_SNAKE_CASE : Optional[int] = {v: k for k, v in self.fairseq_tokens_to_ids.items()} for k in self.fairseq_tokens_to_ids.keys(): self.unique_no_split_tokens.append(lowerCamelCase_ ) def __getstate__( self :Optional[Any] ) -> str: '''simple docstring''' SCREAMING_SNAKE_CASE : Tuple = self.__dict__.copy() SCREAMING_SNAKE_CASE : List[str] = None return state def __setstate__( self :Optional[int] , lowerCamelCase_ :List[str] ) -> int: '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[Any] = d try: import sentencepiece as spm except ImportError: logger.warning( '''You need to install SentencePiece to use XLMRobertaTokenizer: https://github.com/google/sentencepiece''' ''' pip install sentencepiece''' ) raise # for backward compatibility if not hasattr(self , '''sp_model_kwargs''' ): SCREAMING_SNAKE_CASE : Dict = {} SCREAMING_SNAKE_CASE : Dict = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(self.vocab_file ) def __lowerCAmelCase ( self :Optional[Any] , lowerCamelCase_ :List[int] , lowerCamelCase_ :Optional[List[int]] = None , lowerCamelCase_ :bool = False ) -> List[int]: '''simple docstring''' if already_has_special_tokens: return super().get_special_tokens_mask( token_ids_a=lowerCamelCase_ , token_ids_a=lowerCamelCase_ , already_has_special_tokens=lowerCamelCase_ ) if token_ids_a is None: return ([0] * len(lowerCamelCase_ )) + [1] return ([0] * len(lowerCamelCase_ )) + [1] + ([0] * len(lowerCamelCase_ )) + [1] def __lowerCAmelCase ( self :Optional[int] , lowerCamelCase_ :List[int] , lowerCamelCase_ :Optional[List[int]] = None ) -> List[int]: '''simple docstring''' SCREAMING_SNAKE_CASE : str = [self.sep_token_id] if token_ids_a is None: return len(token_ids_a + sep ) * [0] return len(token_ids_a + sep + sep + token_ids_a + sep ) * [0] @property def __lowerCAmelCase ( self :Union[str, Any] ) -> Optional[int]: '''simple docstring''' return len(self.sp_model ) + self.fairseq_offset def __lowerCAmelCase ( self :Tuple ) -> Union[str, Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : Tuple = {self.convert_ids_to_tokens(lowerCamelCase_ ): i for i in range(self.vocab_size )} vocab.update(self.added_tokens_encoder ) return vocab def __lowerCAmelCase ( self :Dict , lowerCamelCase_ :str ) -> str: '''simple docstring''' return self.sp_model.encode(lowerCamelCase_ , out_type=lowerCamelCase_ ) def __lowerCAmelCase ( self :Optional[int] , lowerCamelCase_ :Any ) -> Optional[int]: '''simple docstring''' if token in self.fairseq_tokens_to_ids: return self.fairseq_tokens_to_ids[token] SCREAMING_SNAKE_CASE : List[Any] = self.sp_model.PieceToId(lowerCamelCase_ ) # Need to return unknown token if the SP model returned 0 return spm_id + self.fairseq_offset if spm_id else self.unk_token_id def __lowerCAmelCase ( self :Dict , lowerCamelCase_ :Optional[Any] ) -> Union[str, Any]: '''simple docstring''' if index in self.fairseq_ids_to_tokens: return self.fairseq_ids_to_tokens[index] return self.sp_model.IdToPiece(index - self.fairseq_offset ) def __lowerCAmelCase ( self :Optional[int] , lowerCamelCase_ :Dict ) -> List[str]: '''simple docstring''' SCREAMING_SNAKE_CASE : int = ''''''.join(lowerCamelCase_ ).replace(lowerCamelCase_ , ''' ''' ).strip() return out_string def __lowerCAmelCase ( self :Any , lowerCamelCase_ :str , lowerCamelCase_ :Optional[str] = None ) -> Tuple[str]: '''simple docstring''' if not os.path.isdir(lowerCamelCase_ ): logger.error(f"Vocabulary path ({save_directory}) should be a directory" ) return SCREAMING_SNAKE_CASE : str = os.path.join( lowerCamelCase_ , (filename_prefix + '''-''' if filename_prefix else '''''') + VOCAB_FILES_NAMES['''vocab_file'''] ) if os.path.abspath(self.vocab_file ) != os.path.abspath(lowerCamelCase_ ) and os.path.isfile(self.vocab_file ): copyfile(self.vocab_file , lowerCamelCase_ ) elif not os.path.isfile(self.vocab_file ): with open(lowerCamelCase_ , '''wb''' ) as fi: SCREAMING_SNAKE_CASE : Any = self.sp_model.serialized_model_proto() fi.write(lowerCamelCase_ ) return (out_vocab_file,) def __lowerCAmelCase ( self :Optional[Any] , lowerCamelCase_ :List[int] , lowerCamelCase_ :Optional[List[int]] = None ) -> List[int]: '''simple docstring''' if token_ids_a is None: return token_ids_a + [self.sep_token_id] SCREAMING_SNAKE_CASE : Tuple = [self.sep_token_id] return token_ids_a + sep + token_ids_a + sep
18
"""simple docstring""" import logging import os import random import sys from dataclasses import dataclass, field from typing import Optional import datasets import numpy as np import pandas as pd from datasets import load_dataset import transformers from transformers import ( AutoConfig, BartForSequenceClassification, DataCollatorWithPadding, EvalPrediction, HfArgumentParser, TapexTokenizer, Trainer, TrainingArguments, default_data_collator, set_seed, ) from transformers.trainer_utils import get_last_checkpoint from transformers.utils import check_min_version from transformers.utils.versions import require_version # Will error if the minimal version of Transformers is not installed. Remove at your own risks. check_min_version("4.17.0.dev0") require_version("datasets>=1.8.0", "To fix: pip install -r examples/pytorch/text-classification/requirements.txt") lowerCamelCase__ : Any = logging.getLogger(__name__) @dataclass class lowercase__: '''simple docstring''' UpperCamelCase = field( default="""tab_fact""" , metadata={"""help""": """The name of the dataset to use (via the datasets library)."""} ) UpperCamelCase = field( default="""tab_fact""" , metadata={"""help""": """The configuration name of the dataset to use (via the datasets library)."""} , ) UpperCamelCase = field( default=10_24 , metadata={ """help""": ( """The maximum total input sequence length after tokenization. Sequences longer """ """than this will be truncated, sequences shorter will be padded.""" ) } , ) UpperCamelCase = field( default=_UpperCAmelCase , metadata={"""help""": """Overwrite the cached preprocessed datasets or not."""} ) UpperCamelCase = field( default=_UpperCAmelCase , metadata={ """help""": ( """Whether to pad all samples to `max_seq_length`. """ """If False, will pad the samples dynamically when batching to the maximum length in the batch.""" ) } , ) UpperCamelCase = field( default=_UpperCAmelCase , metadata={ """help""": ( """For debugging purposes or quicker training, truncate the number of training examples to this """ """value if set.""" ) } , ) UpperCamelCase = field( default=_UpperCAmelCase , metadata={ """help""": ( """For debugging purposes or quicker training, truncate the number of evaluation examples to this """ """value if set.""" ) } , ) UpperCamelCase = field( default=_UpperCAmelCase , metadata={ """help""": ( """For debugging purposes or quicker training, truncate the number of prediction examples to this """ """value if set.""" ) } , ) UpperCamelCase = field( default=_UpperCAmelCase , metadata={"""help""": """A csv or a json file containing the training data."""} ) UpperCamelCase = field( default=_UpperCAmelCase , metadata={"""help""": """A csv or a json file containing the validation data."""} ) UpperCamelCase = field(default=_UpperCAmelCase , metadata={"""help""": """A csv or a json file containing the test data."""} ) def __lowerCAmelCase ( self :Union[str, Any] ) -> Dict: '''simple docstring''' if self.dataset_name is not None: pass elif self.train_file is None or self.validation_file is None: raise ValueError('''Need either a GLUE task, a training/validation file or a dataset name.''' ) else: SCREAMING_SNAKE_CASE : Union[str, Any] = self.train_file.split('''.''' )[-1] assert train_extension in ["csv", "json"], "`train_file` should be a csv or a json file." SCREAMING_SNAKE_CASE : Optional[int] = self.validation_file.split('''.''' )[-1] assert ( validation_extension == train_extension ), "`validation_file` should have the same extension (csv or json) as `train_file`." @dataclass class lowercase__: '''simple docstring''' UpperCamelCase = field( default=_UpperCAmelCase , metadata={"""help""": """Path to pretrained model or model identifier from huggingface.co/models"""} ) UpperCamelCase = field( default=_UpperCAmelCase , metadata={"""help""": """Pretrained config name or path if not the same as model_name"""} ) UpperCamelCase = field( default=_UpperCAmelCase , metadata={"""help""": """Pretrained tokenizer name or path if not the same as model_name"""} ) UpperCamelCase = field( default=_UpperCAmelCase , metadata={"""help""": """Where do you want to store the pretrained models downloaded from huggingface.co"""} , ) UpperCamelCase = field( default=_UpperCAmelCase , metadata={"""help""": """Whether to use one of the fast tokenizer (backed by the tokenizers library) or not."""} , ) UpperCamelCase = field( default="""main""" , metadata={"""help""": """The specific model version to use (can be a branch name, tag name or commit id)."""} , ) UpperCamelCase = field( default=_UpperCAmelCase , metadata={ """help""": ( """Will use the token generated when running `huggingface-cli login` (necessary to use this script """ """with private models).""" ) } , ) def __A ( )-> List[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : Union[str, Any] = 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 : Dict = parser.parse_json_file(json_file=os.path.abspath(sys.argv[1] ) ) else: SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : Dict = parser.parse_args_into_dataclasses() # 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 )] , ) SCREAMING_SNAKE_CASE : Union[str, Any] = training_args.get_process_log_level() logger.setLevel(a_ ) datasets.utils.logging.set_verbosity(a_ ) transformers.utils.logging.set_verbosity(a_ ) 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 : 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 ) # Get the datasets: you can either provide your own CSV/JSON training and evaluation files (see below) # or specify a GLUE benchmark task (the dataset will be downloaded automatically from the datasets Hub). # # For JSON files, this script will use the `question` column for the input question and `table` column for the corresponding table. # # If the CSVs/JSONs contain only one non-label column, the script does single sentence classification on this # single column. You can easily tweak this behavior (see below) # # In distributed training, the load_dataset function guarantee that only one local process can concurrently # download the dataset. if data_args.dataset_name is not None: # Downloading and loading a dataset from the hub. SCREAMING_SNAKE_CASE : List[str] = load_dataset( data_args.dataset_name , data_args.dataset_config_name , cache_dir=model_args.cache_dir ) else: # Loading a dataset from your local files. # CSV/JSON training and evaluation files are needed. SCREAMING_SNAKE_CASE : Any = {'''train''': data_args.train_file, '''validation''': data_args.validation_file} # Get the test dataset: you can provide your own CSV/JSON test file (see below) # when you use `do_predict` without specifying a GLUE benchmark task. if training_args.do_predict: if data_args.test_file is not None: SCREAMING_SNAKE_CASE : List[Any] = data_args.train_file.split('''.''' )[-1] SCREAMING_SNAKE_CASE : Optional[int] = data_args.test_file.split('''.''' )[-1] assert ( test_extension == train_extension ), "`test_file` should have the same extension (csv or json) as `train_file`." SCREAMING_SNAKE_CASE : str = data_args.test_file else: raise ValueError('''Need either a GLUE task or a test file for `do_predict`.''' ) for key in data_files.keys(): logger.info(F"load a local file for {key}: {data_files[key]}" ) if data_args.train_file.endswith('''.csv''' ): # Loading a dataset from local csv files SCREAMING_SNAKE_CASE : int = load_dataset('''csv''' , data_files=a_ , cache_dir=model_args.cache_dir ) else: # Loading a dataset from local json files SCREAMING_SNAKE_CASE : Tuple = load_dataset('''json''' , data_files=a_ , cache_dir=model_args.cache_dir ) # See more about loading any type of standard or custom dataset at # https://huggingface.co/docs/datasets/loading_datasets.html. # Labels SCREAMING_SNAKE_CASE : str = raw_datasets['''train'''].features['''label'''].names SCREAMING_SNAKE_CASE : Union[str, Any] = len(a_ ) # Load pretrained model and tokenizer # # In distributed training, the .from_pretrained methods guarantee that only one local process can concurrently # download model & vocab. SCREAMING_SNAKE_CASE : Optional[Any] = AutoConfig.from_pretrained( model_args.config_name if model_args.config_name else model_args.model_name_or_path , num_labels=a_ , cache_dir=model_args.cache_dir , revision=model_args.model_revision , use_auth_token=True if model_args.use_auth_token else None , ) # load tapex tokenizer SCREAMING_SNAKE_CASE : Dict = TapexTokenizer.from_pretrained( model_args.tokenizer_name if model_args.tokenizer_name else model_args.model_name_or_path , cache_dir=model_args.cache_dir , use_fast=model_args.use_fast_tokenizer , revision=model_args.model_revision , use_auth_token=True if model_args.use_auth_token else None , add_prefix_space=a_ , ) SCREAMING_SNAKE_CASE : List[Any] = BartForSequenceClassification.from_pretrained( model_args.model_name_or_path , from_tf=bool('''.ckpt''' in model_args.model_name_or_path ) , config=a_ , cache_dir=model_args.cache_dir , revision=model_args.model_revision , use_auth_token=True if model_args.use_auth_token else None , ) # Padding strategy if data_args.pad_to_max_length: SCREAMING_SNAKE_CASE : Tuple = '''max_length''' else: # We will pad later, dynamically at batch creation, to the max sequence length in each batch SCREAMING_SNAKE_CASE : Optional[Any] = False # Some models have set the order of the labels to use, so let's make sure we do use it. SCREAMING_SNAKE_CASE : Tuple = {'''Refused''': 0, '''Entailed''': 1} SCREAMING_SNAKE_CASE : List[Any] = {0: '''Refused''', 1: '''Entailed'''} if data_args.max_seq_length > tokenizer.model_max_length: logger.warning( F"The max_seq_length passed ({data_args.max_seq_length}) is larger than the maximum length for the" F"model ({tokenizer.model_max_length}). Using max_seq_length={tokenizer.model_max_length}." ) SCREAMING_SNAKE_CASE : Optional[int] = min(data_args.max_seq_length , tokenizer.model_max_length ) def preprocess_tabfact_function(a_ : str ): # Tokenize the texts def _convert_table_text_to_pandas(a_ : List[Any] ): SCREAMING_SNAKE_CASE : List[Any] = [_table_row.split('''#''' ) for _table_row in _table_text.strip('''\n''' ).split('''\n''' )] SCREAMING_SNAKE_CASE : Dict = pd.DataFrame.from_records(_table_content[1:] , columns=_table_content[0] ) return _table_pd SCREAMING_SNAKE_CASE : List[Any] = examples['''statement'''] SCREAMING_SNAKE_CASE : Optional[int] = list(map(_convert_table_text_to_pandas , examples['''table_text'''] ) ) SCREAMING_SNAKE_CASE : Any = tokenizer(a_ , a_ , padding=a_ , max_length=a_ , truncation=a_ ) SCREAMING_SNAKE_CASE : List[Any] = examples['''label'''] return result with training_args.main_process_first(desc='''dataset map pre-processing''' ): SCREAMING_SNAKE_CASE : Optional[Any] = raw_datasets.map( a_ , batched=a_ , load_from_cache_file=not data_args.overwrite_cache , desc='''Running tokenizer on dataset''' , ) if training_args.do_train: if "train" not in raw_datasets: raise ValueError('''--do_train requires a train dataset''' ) SCREAMING_SNAKE_CASE : List[str] = raw_datasets['''train'''] if data_args.max_train_samples is not None: SCREAMING_SNAKE_CASE : Any = train_dataset.select(range(data_args.max_train_samples ) ) if training_args.do_eval: if "validation" not in raw_datasets and "validation_matched" not in raw_datasets: raise ValueError('''--do_eval requires a validation dataset''' ) SCREAMING_SNAKE_CASE : List[str] = raw_datasets['''validation'''] if data_args.max_eval_samples is not None: SCREAMING_SNAKE_CASE : Union[str, Any] = eval_dataset.select(range(data_args.max_eval_samples ) ) if training_args.do_predict or data_args.test_file is not None: if "test" not in raw_datasets and "test_matched" not in raw_datasets: raise ValueError('''--do_predict requires a test dataset''' ) SCREAMING_SNAKE_CASE : Tuple = raw_datasets['''test'''] if data_args.max_predict_samples is not None: SCREAMING_SNAKE_CASE : Optional[int] = predict_dataset.select(range(data_args.max_predict_samples ) ) # Log a few random samples from the training set: if training_args.do_train: for index in random.sample(range(len(a_ ) ) , 3 ): logger.info(F"Sample {index} of the training set: {train_dataset[index]}." ) # You can define your custom 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(a_ : EvalPrediction ): SCREAMING_SNAKE_CASE : str = p.predictions[0] if isinstance(p.predictions , a_ ) else p.predictions SCREAMING_SNAKE_CASE : Tuple = np.argmax(a_ , axis=1 ) return {"accuracy": (preds == p.label_ids).astype(np.floataa ).mean().item()} # Data collator will default to DataCollatorWithPadding, so we change it if we already did the padding. if data_args.pad_to_max_length: SCREAMING_SNAKE_CASE : Tuple = default_data_collator elif training_args.fpaa: SCREAMING_SNAKE_CASE : Union[str, Any] = DataCollatorWithPadding(a_ , pad_to_multiple_of=8 ) else: SCREAMING_SNAKE_CASE : List[Any] = None # Initialize our Trainer SCREAMING_SNAKE_CASE : Optional[Any] = Trainer( model=a_ , args=a_ , train_dataset=train_dataset if training_args.do_train else None , eval_dataset=eval_dataset if training_args.do_eval else None , compute_metrics=a_ , tokenizer=a_ , data_collator=a_ , ) # Training if training_args.do_train: SCREAMING_SNAKE_CASE : List[str] = None if training_args.resume_from_checkpoint is not None: SCREAMING_SNAKE_CASE : Dict = training_args.resume_from_checkpoint elif last_checkpoint is not None: SCREAMING_SNAKE_CASE : str = last_checkpoint SCREAMING_SNAKE_CASE : str = trainer.train(resume_from_checkpoint=a_ ) SCREAMING_SNAKE_CASE : Optional[int] = train_result.metrics SCREAMING_SNAKE_CASE : int = ( data_args.max_train_samples if data_args.max_train_samples is not None else len(a_ ) ) SCREAMING_SNAKE_CASE : Optional[int] = min(a_ , len(a_ ) ) trainer.save_model() # Saves the tokenizer too for easy upload trainer.log_metrics('''train''' , a_ ) trainer.save_metrics('''train''' , a_ ) trainer.save_state() # Evaluation if training_args.do_eval: logger.info('''*** Evaluate ***''' ) SCREAMING_SNAKE_CASE : Tuple = trainer.evaluate(eval_dataset=a_ ) SCREAMING_SNAKE_CASE : str = data_args.max_eval_samples if data_args.max_eval_samples is not None else len(a_ ) SCREAMING_SNAKE_CASE : Union[str, Any] = min(a_ , len(a_ ) ) trainer.log_metrics('''eval''' , a_ ) trainer.save_metrics('''eval''' , a_ ) if training_args.do_predict: logger.info('''*** Predict ***''' ) # Removing the `label` columns because it contains -1 and Trainer won't like that. SCREAMING_SNAKE_CASE : Optional[Any] = predict_dataset.remove_columns('''label''' ) SCREAMING_SNAKE_CASE : Optional[Any] = trainer.predict(a_ , metric_key_prefix='''predict''' ).predictions SCREAMING_SNAKE_CASE : Union[str, Any] = np.argmax(a_ , axis=1 ) SCREAMING_SNAKE_CASE : Optional[int] = os.path.join(training_args.output_dir , '''predict_results_tabfact.txt''' ) if trainer.is_world_process_zero(): with open(a_ , '''w''' ) as writer: logger.info('''***** Predict Results *****''' ) writer.write('''index\tprediction\n''' ) for index, item in enumerate(a_ ): SCREAMING_SNAKE_CASE : Union[str, Any] = label_list[item] writer.write(F"{index}\t{item}\n" ) SCREAMING_SNAKE_CASE : Optional[int] = {'''finetuned_from''': model_args.model_name_or_path, '''tasks''': '''text-classification'''} if training_args.push_to_hub: trainer.push_to_hub(**a_ ) else: trainer.create_model_card(**a_ ) def __A ( a_ : List[str] )-> int: '''simple docstring''' main() if __name__ == "__main__": main()
18
1
"""simple docstring""" from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_tf_available, is_tokenizers_available, is_torch_available, ) lowerCamelCase__ : List[str] = { "configuration_deberta": ["DEBERTA_PRETRAINED_CONFIG_ARCHIVE_MAP", "DebertaConfig", "DebertaOnnxConfig"], "tokenization_deberta": ["DebertaTokenizer"], } try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCamelCase__ : Optional[Any] = ["DebertaTokenizerFast"] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCamelCase__ : Union[str, Any] = [ "DEBERTA_PRETRAINED_MODEL_ARCHIVE_LIST", "DebertaForMaskedLM", "DebertaForQuestionAnswering", "DebertaForSequenceClassification", "DebertaForTokenClassification", "DebertaModel", "DebertaPreTrainedModel", ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCamelCase__ : Optional[int] = [ "TF_DEBERTA_PRETRAINED_MODEL_ARCHIVE_LIST", "TFDebertaForMaskedLM", "TFDebertaForQuestionAnswering", "TFDebertaForSequenceClassification", "TFDebertaForTokenClassification", "TFDebertaModel", "TFDebertaPreTrainedModel", ] if TYPE_CHECKING: from .configuration_deberta import DEBERTA_PRETRAINED_CONFIG_ARCHIVE_MAP, DebertaConfig, DebertaOnnxConfig from .tokenization_deberta import DebertaTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_deberta_fast import DebertaTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_deberta import ( DEBERTA_PRETRAINED_MODEL_ARCHIVE_LIST, DebertaForMaskedLM, DebertaForQuestionAnswering, DebertaForSequenceClassification, DebertaForTokenClassification, DebertaModel, DebertaPreTrainedModel, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_deberta import ( TF_DEBERTA_PRETRAINED_MODEL_ARCHIVE_LIST, TFDebertaForMaskedLM, TFDebertaForQuestionAnswering, TFDebertaForSequenceClassification, TFDebertaForTokenClassification, TFDebertaModel, TFDebertaPreTrainedModel, ) else: import sys lowerCamelCase__ : str = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
18
"""simple docstring""" import collections import inspect import unittest from transformers import SwinvaConfig from transformers.testing_utils import require_torch, require_vision, slow, torch_device from transformers.utils import cached_property, is_torch_available, is_vision_available from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, _config_zero_init, floats_tensor, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from torch import nn from transformers import SwinvaForImageClassification, SwinvaForMaskedImageModeling, SwinvaModel from transformers.models.swinva.modeling_swinva import SWINV2_PRETRAINED_MODEL_ARCHIVE_LIST if is_vision_available(): from PIL import Image from transformers import AutoImageProcessor class lowercase__: '''simple docstring''' def __init__( self :str , lowerCamelCase_ :List[Any] , lowerCamelCase_ :Any=13 , lowerCamelCase_ :Any=32 , lowerCamelCase_ :Union[str, Any]=2 , lowerCamelCase_ :Any=3 , lowerCamelCase_ :Union[str, Any]=16 , lowerCamelCase_ :int=[1, 2, 1] , lowerCamelCase_ :str=[2, 2, 4] , lowerCamelCase_ :str=2 , lowerCamelCase_ :Tuple=2.0 , lowerCamelCase_ :Union[str, Any]=True , lowerCamelCase_ :str=0.0 , lowerCamelCase_ :Optional[int]=0.0 , lowerCamelCase_ :Dict=0.1 , lowerCamelCase_ :Union[str, Any]="gelu" , lowerCamelCase_ :str=False , lowerCamelCase_ :List[Any]=True , lowerCamelCase_ :int=0.0_2 , lowerCamelCase_ :List[Any]=1E-5 , lowerCamelCase_ :int=True , lowerCamelCase_ :str=None , lowerCamelCase_ :List[Any]=True , lowerCamelCase_ :Union[str, Any]=10 , lowerCamelCase_ :List[Any]=8 , ) -> int: '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[Any] = parent SCREAMING_SNAKE_CASE : int = batch_size SCREAMING_SNAKE_CASE : int = image_size SCREAMING_SNAKE_CASE : Union[str, Any] = patch_size SCREAMING_SNAKE_CASE : Tuple = num_channels SCREAMING_SNAKE_CASE : Any = embed_dim SCREAMING_SNAKE_CASE : int = depths SCREAMING_SNAKE_CASE : List[str] = num_heads SCREAMING_SNAKE_CASE : Union[str, Any] = window_size SCREAMING_SNAKE_CASE : Optional[Any] = mlp_ratio SCREAMING_SNAKE_CASE : List[Any] = qkv_bias SCREAMING_SNAKE_CASE : Union[str, Any] = hidden_dropout_prob SCREAMING_SNAKE_CASE : Dict = attention_probs_dropout_prob SCREAMING_SNAKE_CASE : List[str] = drop_path_rate SCREAMING_SNAKE_CASE : List[Any] = hidden_act SCREAMING_SNAKE_CASE : Optional[Any] = use_absolute_embeddings SCREAMING_SNAKE_CASE : Any = patch_norm SCREAMING_SNAKE_CASE : Union[str, Any] = layer_norm_eps SCREAMING_SNAKE_CASE : List[Any] = initializer_range SCREAMING_SNAKE_CASE : Any = is_training SCREAMING_SNAKE_CASE : List[Any] = scope SCREAMING_SNAKE_CASE : Optional[Any] = use_labels SCREAMING_SNAKE_CASE : Optional[Any] = type_sequence_label_size SCREAMING_SNAKE_CASE : Optional[Any] = encoder_stride def __lowerCAmelCase ( self :Optional[int] ) -> List[str]: '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[int] = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) SCREAMING_SNAKE_CASE : int = None if self.use_labels: SCREAMING_SNAKE_CASE : Dict = ids_tensor([self.batch_size] , self.type_sequence_label_size ) SCREAMING_SNAKE_CASE : Tuple = self.get_config() return config, pixel_values, labels def __lowerCAmelCase ( self :int ) -> int: '''simple docstring''' return SwinvaConfig( image_size=self.image_size , patch_size=self.patch_size , num_channels=self.num_channels , embed_dim=self.embed_dim , depths=self.depths , num_heads=self.num_heads , window_size=self.window_size , mlp_ratio=self.mlp_ratio , qkv_bias=self.qkv_bias , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , drop_path_rate=self.drop_path_rate , hidden_act=self.hidden_act , use_absolute_embeddings=self.use_absolute_embeddings , path_norm=self.patch_norm , layer_norm_eps=self.layer_norm_eps , initializer_range=self.initializer_range , encoder_stride=self.encoder_stride , ) def __lowerCAmelCase ( self :List[Any] , lowerCamelCase_ :int , lowerCamelCase_ :Optional[int] , lowerCamelCase_ :Optional[Any] ) -> Union[str, Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : str = SwinvaModel(config=lowerCamelCase_ ) model.to(lowerCamelCase_ ) model.eval() SCREAMING_SNAKE_CASE : int = model(lowerCamelCase_ ) SCREAMING_SNAKE_CASE : List[Any] = ((config.image_size // config.patch_size) ** 2) // (4 ** (len(config.depths ) - 1)) SCREAMING_SNAKE_CASE : Dict = int(config.embed_dim * 2 ** (len(config.depths ) - 1) ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, expected_seq_len, expected_dim) ) def __lowerCAmelCase ( self :str , lowerCamelCase_ :str , lowerCamelCase_ :Tuple , lowerCamelCase_ :Any ) -> Union[str, Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : List[str] = SwinvaForMaskedImageModeling(config=lowerCamelCase_ ) model.to(lowerCamelCase_ ) model.eval() SCREAMING_SNAKE_CASE : Dict = model(lowerCamelCase_ ) self.parent.assertEqual( result.logits.shape , (self.batch_size, self.num_channels, self.image_size, self.image_size) ) # test greyscale images SCREAMING_SNAKE_CASE : Tuple = 1 SCREAMING_SNAKE_CASE : List[Any] = SwinvaForMaskedImageModeling(lowerCamelCase_ ) model.to(lowerCamelCase_ ) model.eval() SCREAMING_SNAKE_CASE : List[str] = floats_tensor([self.batch_size, 1, self.image_size, self.image_size] ) SCREAMING_SNAKE_CASE : Tuple = model(lowerCamelCase_ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, 1, self.image_size, self.image_size) ) def __lowerCAmelCase ( self :Tuple , lowerCamelCase_ :Union[str, Any] , lowerCamelCase_ :Any , lowerCamelCase_ :int ) -> Tuple: '''simple docstring''' SCREAMING_SNAKE_CASE : Tuple = self.type_sequence_label_size SCREAMING_SNAKE_CASE : Optional[Any] = SwinvaForImageClassification(lowerCamelCase_ ) model.to(lowerCamelCase_ ) model.eval() SCREAMING_SNAKE_CASE : Optional[int] = model(lowerCamelCase_ , labels=lowerCamelCase_ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.type_sequence_label_size) ) def __lowerCAmelCase ( self :List[Any] ) -> List[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : List[str] = self.prepare_config_and_inputs() SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : Optional[int] = config_and_inputs SCREAMING_SNAKE_CASE : int = {'''pixel_values''': pixel_values} return config, inputs_dict @require_torch class lowercase__( _UpperCAmelCase , _UpperCAmelCase , unittest.TestCase ): '''simple docstring''' UpperCamelCase = ( (SwinvaModel, SwinvaForImageClassification, SwinvaForMaskedImageModeling) if is_torch_available() else () ) UpperCamelCase = ( {"""feature-extraction""": SwinvaModel, """image-classification""": SwinvaForImageClassification} if is_torch_available() else {} ) UpperCamelCase = False UpperCamelCase = False UpperCamelCase = False UpperCamelCase = False def __lowerCAmelCase ( self :Union[str, Any] ) -> Union[str, Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : int = SwinvaModelTester(self ) SCREAMING_SNAKE_CASE : Dict = ConfigTester(self , config_class=lowerCamelCase_ , embed_dim=37 ) def __lowerCAmelCase ( self :Dict ) -> List[str]: '''simple docstring''' self.config_tester.create_and_test_config_to_json_string() self.config_tester.create_and_test_config_to_json_file() self.config_tester.create_and_test_config_from_and_save_pretrained() self.config_tester.create_and_test_config_with_num_labels() self.config_tester.check_config_can_be_init_without_params() self.config_tester.check_config_arguments_init() def __lowerCAmelCase ( self :List[Any] ) -> List[str]: '''simple docstring''' SCREAMING_SNAKE_CASE : Union[str, Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*lowerCamelCase_ ) @unittest.skip(reason='''Got `CUDA error: misaligned address` with PyTorch 2.0.0.''' ) def __lowerCAmelCase ( self :str ) -> List[Any]: '''simple docstring''' pass @unittest.skip(reason='''Swinv2 does not use inputs_embeds''' ) def __lowerCAmelCase ( self :List[Any] ) -> Any: '''simple docstring''' pass def __lowerCAmelCase ( self :int ) -> Union[str, Any]: '''simple docstring''' SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : Optional[int] = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: SCREAMING_SNAKE_CASE : List[Any] = model_class(lowerCamelCase_ ) self.assertIsInstance(model.get_input_embeddings() , (nn.Module) ) SCREAMING_SNAKE_CASE : str = model.get_output_embeddings() self.assertTrue(x is None or isinstance(lowerCamelCase_ , nn.Linear ) ) def __lowerCAmelCase ( self :int ) -> List[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : Tuple = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: SCREAMING_SNAKE_CASE : Optional[Any] = model_class(lowerCamelCase_ ) SCREAMING_SNAKE_CASE : List[str] = inspect.signature(model.forward ) # signature.parameters is an OrderedDict => so arg_names order is deterministic SCREAMING_SNAKE_CASE : Optional[int] = [*signature.parameters.keys()] SCREAMING_SNAKE_CASE : Union[str, Any] = ['''pixel_values'''] self.assertListEqual(arg_names[:1] , lowerCamelCase_ ) def __lowerCAmelCase ( self :int ) -> Optional[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : str = self.model_tester.prepare_config_and_inputs_for_common() SCREAMING_SNAKE_CASE : Tuple = True for model_class in self.all_model_classes: SCREAMING_SNAKE_CASE : List[str] = True SCREAMING_SNAKE_CASE : List[Any] = False SCREAMING_SNAKE_CASE : Any = True SCREAMING_SNAKE_CASE : Any = model_class(lowerCamelCase_ ) model.to(lowerCamelCase_ ) model.eval() with torch.no_grad(): SCREAMING_SNAKE_CASE : str = model(**self._prepare_for_class(lowerCamelCase_ , lowerCamelCase_ ) ) SCREAMING_SNAKE_CASE : Tuple = outputs.attentions SCREAMING_SNAKE_CASE : Tuple = len(self.model_tester.depths ) self.assertEqual(len(lowerCamelCase_ ) , lowerCamelCase_ ) # check that output_attentions also work using config del inputs_dict["output_attentions"] SCREAMING_SNAKE_CASE : Union[str, Any] = True SCREAMING_SNAKE_CASE : Optional[int] = config.window_size**2 SCREAMING_SNAKE_CASE : str = model_class(lowerCamelCase_ ) model.to(lowerCamelCase_ ) model.eval() with torch.no_grad(): SCREAMING_SNAKE_CASE : Union[str, Any] = model(**self._prepare_for_class(lowerCamelCase_ , lowerCamelCase_ ) ) SCREAMING_SNAKE_CASE : Dict = outputs.attentions self.assertEqual(len(lowerCamelCase_ ) , lowerCamelCase_ ) self.assertListEqual( list(attentions[0].shape[-3:] ) , [self.model_tester.num_heads[0], window_size_squared, window_size_squared] , ) SCREAMING_SNAKE_CASE : Dict = len(lowerCamelCase_ ) # Check attention is always last and order is fine SCREAMING_SNAKE_CASE : int = True SCREAMING_SNAKE_CASE : Dict = True SCREAMING_SNAKE_CASE : int = model_class(lowerCamelCase_ ) model.to(lowerCamelCase_ ) model.eval() with torch.no_grad(): SCREAMING_SNAKE_CASE : List[Any] = model(**self._prepare_for_class(lowerCamelCase_ , lowerCamelCase_ ) ) if hasattr(self.model_tester , '''num_hidden_states_types''' ): SCREAMING_SNAKE_CASE : Any = self.model_tester.num_hidden_states_types else: # also another +1 for reshaped_hidden_states SCREAMING_SNAKE_CASE : Optional[Any] = 2 self.assertEqual(out_len + added_hidden_states , len(lowerCamelCase_ ) ) SCREAMING_SNAKE_CASE : Tuple = outputs.attentions self.assertEqual(len(lowerCamelCase_ ) , lowerCamelCase_ ) self.assertListEqual( list(self_attentions[0].shape[-3:] ) , [self.model_tester.num_heads[0], window_size_squared, window_size_squared] , ) def __lowerCAmelCase ( self :Dict , lowerCamelCase_ :int , lowerCamelCase_ :Any , lowerCamelCase_ :Tuple , lowerCamelCase_ :Any ) -> Optional[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : Union[str, Any] = model_class(lowerCamelCase_ ) model.to(lowerCamelCase_ ) model.eval() with torch.no_grad(): SCREAMING_SNAKE_CASE : Optional[int] = model(**self._prepare_for_class(lowerCamelCase_ , lowerCamelCase_ ) ) SCREAMING_SNAKE_CASE : Dict = outputs.hidden_states SCREAMING_SNAKE_CASE : str = getattr( self.model_tester , '''expected_num_hidden_layers''' , len(self.model_tester.depths ) + 1 ) self.assertEqual(len(lowerCamelCase_ ) , lowerCamelCase_ ) # Swinv2 has a different seq_length SCREAMING_SNAKE_CASE : List[str] = ( config.patch_size if isinstance(config.patch_size , collections.abc.Iterable ) else (config.patch_size, config.patch_size) ) SCREAMING_SNAKE_CASE : Dict = (image_size[1] // patch_size[1]) * (image_size[0] // patch_size[0]) self.assertListEqual( list(hidden_states[0].shape[-2:] ) , [num_patches, self.model_tester.embed_dim] , ) SCREAMING_SNAKE_CASE : Any = outputs.reshaped_hidden_states self.assertEqual(len(lowerCamelCase_ ) , lowerCamelCase_ ) SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : Any = reshaped_hidden_states[0].shape SCREAMING_SNAKE_CASE : Optional[int] = ( reshaped_hidden_states[0].view(lowerCamelCase_ , lowerCamelCase_ , height * width ).permute(0 , 2 , 1 ) ) self.assertListEqual( list(reshaped_hidden_states.shape[-2:] ) , [num_patches, self.model_tester.embed_dim] , ) def __lowerCAmelCase ( self :Optional[Any] ) -> Optional[int]: '''simple docstring''' SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : Union[str, Any] = self.model_tester.prepare_config_and_inputs_for_common() SCREAMING_SNAKE_CASE : List[str] = ( self.model_tester.image_size if isinstance(self.model_tester.image_size , collections.abc.Iterable ) else (self.model_tester.image_size, self.model_tester.image_size) ) for model_class in self.all_model_classes: SCREAMING_SNAKE_CASE : Tuple = True self.check_hidden_states_output(lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ ) # check that output_hidden_states also work using config del inputs_dict["output_hidden_states"] SCREAMING_SNAKE_CASE : List[str] = True self.check_hidden_states_output(lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ ) def __lowerCAmelCase ( self :str ) -> Optional[int]: '''simple docstring''' SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : str = self.model_tester.prepare_config_and_inputs_for_common() SCREAMING_SNAKE_CASE : List[Any] = 3 SCREAMING_SNAKE_CASE : int = ( self.model_tester.image_size if isinstance(self.model_tester.image_size , collections.abc.Iterable ) else (self.model_tester.image_size, self.model_tester.image_size) ) SCREAMING_SNAKE_CASE : Optional[Any] = ( config.patch_size if isinstance(config.patch_size , collections.abc.Iterable ) else (config.patch_size, config.patch_size) ) SCREAMING_SNAKE_CASE : Optional[int] = image_size[0] + patch_size[0] - (image_size[0] % patch_size[0]) SCREAMING_SNAKE_CASE : Optional[int] = image_size[1] + patch_size[1] - (image_size[1] % patch_size[1]) for model_class in self.all_model_classes: SCREAMING_SNAKE_CASE : Optional[Any] = True self.check_hidden_states_output(lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , (padded_height, padded_width) ) # check that output_hidden_states also work using config del inputs_dict["output_hidden_states"] SCREAMING_SNAKE_CASE : Optional[Any] = True self.check_hidden_states_output(lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , (padded_height, padded_width) ) def __lowerCAmelCase ( self :str ) -> List[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : Tuple = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_masked_image_modeling(*lowerCamelCase_ ) def __lowerCAmelCase ( self :Tuple ) -> Optional[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : Tuple = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_image_classification(*lowerCamelCase_ ) @slow def __lowerCAmelCase ( self :Tuple ) -> List[str]: '''simple docstring''' for model_name in SWINV2_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: SCREAMING_SNAKE_CASE : Tuple = SwinvaModel.from_pretrained(lowerCamelCase_ ) self.assertIsNotNone(lowerCamelCase_ ) def __lowerCAmelCase ( self :int ) -> Any: '''simple docstring''' SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : List[str] = self.model_tester.prepare_config_and_inputs_for_common() SCREAMING_SNAKE_CASE : Tuple = _config_zero_init(lowerCamelCase_ ) for model_class in self.all_model_classes: SCREAMING_SNAKE_CASE : Dict = model_class(config=lowerCamelCase_ ) for name, param in model.named_parameters(): if "embeddings" not in name and "logit_scale" not in name and param.requires_grad: self.assertIn( ((param.data.mean() * 1E9).round() / 1E9).item() , [0.0, 1.0] , msg=f"Parameter {name} of model {model_class} seems not properly initialized" , ) @require_vision @require_torch class lowercase__( unittest.TestCase ): '''simple docstring''' @cached_property def __lowerCAmelCase ( self :Dict ) -> List[Any]: '''simple docstring''' return ( AutoImageProcessor.from_pretrained('''microsoft/swinv2-tiny-patch4-window8-256''' ) if is_vision_available() else None ) @slow def __lowerCAmelCase ( self :Dict ) -> Tuple: '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[int] = SwinvaForImageClassification.from_pretrained('''microsoft/swinv2-tiny-patch4-window8-256''' ).to( lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Dict = self.default_image_processor SCREAMING_SNAKE_CASE : Optional[Any] = Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png''' ) SCREAMING_SNAKE_CASE : List[str] = image_processor(images=lowerCamelCase_ , return_tensors='''pt''' ).to(lowerCamelCase_ ) # forward pass with torch.no_grad(): SCREAMING_SNAKE_CASE : Tuple = model(**lowerCamelCase_ ) # verify the logits SCREAMING_SNAKE_CASE : List[str] = torch.Size((1, 10_00) ) self.assertEqual(outputs.logits.shape , lowerCamelCase_ ) SCREAMING_SNAKE_CASE : int = torch.tensor([-0.3_9_4_7, -0.4_3_0_6, 0.0_0_2_6] ).to(lowerCamelCase_ ) self.assertTrue(torch.allclose(outputs.logits[0, :3] , lowerCamelCase_ , atol=1E-4 ) )
18
1
"""simple docstring""" import argparse import requests import torch from PIL import Image from transformers import CLIPProcessor, GroupViTConfig, GroupViTModel def __A ( a_ : List[str] )-> Tuple: '''simple docstring''' if "img_encoder.pos_embed" in name: SCREAMING_SNAKE_CASE : str = name.replace('''img_encoder.pos_embed''' , '''vision_model.embeddings.position_embeddings''' ) if "img_encoder.patch_embed.proj" in name: SCREAMING_SNAKE_CASE : Tuple = name.replace('''img_encoder.patch_embed.proj''' , '''vision_model.embeddings.patch_embeddings.projection''' ) if "img_encoder.patch_embed.norm" in name: SCREAMING_SNAKE_CASE : Tuple = name.replace('''img_encoder.patch_embed.norm''' , '''vision_model.embeddings.layernorm''' ) if "img_encoder.layers" in name: SCREAMING_SNAKE_CASE : Tuple = name.replace('''img_encoder.layers''' , '''vision_model.encoder.stages''' ) if "blocks" in name and "res" not in name: SCREAMING_SNAKE_CASE : Dict = name.replace('''blocks''' , '''layers''' ) if "attn" in name and "pre_assign" not in name: SCREAMING_SNAKE_CASE : Any = name.replace('''attn''' , '''self_attn''' ) if "proj" in name and "self_attn" in name and "text" not in name: SCREAMING_SNAKE_CASE : Any = name.replace('''proj''' , '''out_proj''' ) if "pre_assign_attn.attn.proj" in name: SCREAMING_SNAKE_CASE : int = name.replace('''pre_assign_attn.attn.proj''' , '''pre_assign_attn.attn.out_proj''' ) if "norm1" in name: SCREAMING_SNAKE_CASE : Union[str, Any] = name.replace('''norm1''' , '''layer_norm1''' ) if "norm2" in name and "pre_assign" not in name: SCREAMING_SNAKE_CASE : Dict = name.replace('''norm2''' , '''layer_norm2''' ) if "img_encoder.norm" in name: SCREAMING_SNAKE_CASE : Optional[int] = name.replace('''img_encoder.norm''' , '''vision_model.layernorm''' ) # text encoder if "text_encoder.token_embedding" in name: SCREAMING_SNAKE_CASE : Optional[int] = name.replace('''text_encoder.token_embedding''' , '''text_model.embeddings.token_embedding''' ) if "text_encoder.positional_embedding" in name: SCREAMING_SNAKE_CASE : Optional[int] = name.replace('''text_encoder.positional_embedding''' , '''text_model.embeddings.position_embedding.weight''' ) if "text_encoder.transformer.resblocks." in name: SCREAMING_SNAKE_CASE : Optional[Any] = name.replace('''text_encoder.transformer.resblocks.''' , '''text_model.encoder.layers.''' ) if "ln_1" in name: SCREAMING_SNAKE_CASE : List[Any] = name.replace('''ln_1''' , '''layer_norm1''' ) if "ln_2" in name: SCREAMING_SNAKE_CASE : str = name.replace('''ln_2''' , '''layer_norm2''' ) if "c_fc" in name: SCREAMING_SNAKE_CASE : Optional[int] = name.replace('''c_fc''' , '''fc1''' ) if "c_proj" in name: SCREAMING_SNAKE_CASE : Tuple = name.replace('''c_proj''' , '''fc2''' ) if "text_encoder" in name: SCREAMING_SNAKE_CASE : int = name.replace('''text_encoder''' , '''text_model''' ) if "ln_final" in name: SCREAMING_SNAKE_CASE : List[str] = name.replace('''ln_final''' , '''final_layer_norm''' ) # projection layers if "img_projector.linear_hidden." in name: SCREAMING_SNAKE_CASE : List[str] = name.replace('''img_projector.linear_hidden.''' , '''visual_projection.''' ) if "img_projector.linear_out." in name: SCREAMING_SNAKE_CASE : List[Any] = name.replace('''img_projector.linear_out.''' , '''visual_projection.3.''' ) if "text_projector.linear_hidden" in name: SCREAMING_SNAKE_CASE : Any = name.replace('''text_projector.linear_hidden''' , '''text_projection''' ) if "text_projector.linear_out" in name: SCREAMING_SNAKE_CASE : Dict = name.replace('''text_projector.linear_out''' , '''text_projection.3''' ) return name def __A ( a_ : Any , a_ : List[str] )-> Any: '''simple docstring''' for key in orig_state_dict.copy().keys(): SCREAMING_SNAKE_CASE : Union[str, Any] = orig_state_dict.pop(a_ ) if "qkv" in key: # weights and biases of the key, value and query projections of vision encoder's attention layers require special treatment: # we need to split them up into separate matrices/vectors SCREAMING_SNAKE_CASE : List[str] = key.split('''.''' ) SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : Dict = int(key_split[2] ), int(key_split[4] ) SCREAMING_SNAKE_CASE : Any = config.vision_config.hidden_size if "weight" in key: SCREAMING_SNAKE_CASE : str = val[:dim, :] SCREAMING_SNAKE_CASE : List[str] = val[dim : dim * 2, :] SCREAMING_SNAKE_CASE : Union[str, Any] = val[-dim:, :] else: SCREAMING_SNAKE_CASE : str = val[:dim] SCREAMING_SNAKE_CASE : Dict = val[dim : dim * 2] SCREAMING_SNAKE_CASE : Union[str, Any] = val[-dim:] elif "in_proj" in key: # weights and biases of the key, value and query projections of text encoder's attention layers require special treatment: # we need to split them up into separate matrices/vectors SCREAMING_SNAKE_CASE : Any = key.split('''.''' ) SCREAMING_SNAKE_CASE : Any = int(key_split[3] ) SCREAMING_SNAKE_CASE : List[str] = config.text_config.hidden_size if "weight" in key: SCREAMING_SNAKE_CASE : Tuple = val[:dim, :] SCREAMING_SNAKE_CASE : str = val[ dim : dim * 2, : ] SCREAMING_SNAKE_CASE : List[str] = val[-dim:, :] else: SCREAMING_SNAKE_CASE : Tuple = val[:dim] SCREAMING_SNAKE_CASE : Any = val[dim : dim * 2] SCREAMING_SNAKE_CASE : List[Any] = val[-dim:] else: SCREAMING_SNAKE_CASE : List[Any] = rename_key(a_ ) # squeeze if necessary if ( "text_projection.0" in new_name or "text_projection.3" in new_name or "visual_projection.0" in new_name or "visual_projection.3" in new_name ): SCREAMING_SNAKE_CASE : Union[str, Any] = val.squeeze_() else: SCREAMING_SNAKE_CASE : Dict = val return orig_state_dict def __A ( )-> List[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : Dict = '''http://images.cocodataset.org/val2017/000000039769.jpg''' SCREAMING_SNAKE_CASE : Any = Image.open(requests.get(a_ , stream=a_ ).raw ) return im @torch.no_grad() def __A ( a_ : List[Any] , a_ : Any , a_ : Any="groupvit-gcc-yfcc" , a_ : Optional[int]=False )-> Any: '''simple docstring''' SCREAMING_SNAKE_CASE : Tuple = GroupViTConfig() SCREAMING_SNAKE_CASE : Optional[int] = GroupViTModel(a_ ).eval() SCREAMING_SNAKE_CASE : str = torch.load(a_ , map_location='''cpu''' )['''model'''] SCREAMING_SNAKE_CASE : str = convert_state_dict(a_ , a_ ) SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : List[Any] = model.load_state_dict(a_ , strict=a_ ) assert missing_keys == ["text_model.embeddings.position_ids"] assert (unexpected_keys == ["multi_label_logit_scale"]) or (len(a_ ) == 0) # verify result SCREAMING_SNAKE_CASE : Tuple = CLIPProcessor.from_pretrained('''openai/clip-vit-base-patch32''' ) SCREAMING_SNAKE_CASE : List[str] = prepare_img() SCREAMING_SNAKE_CASE : List[str] = processor(text=['''a photo of a cat''', '''a photo of a dog'''] , images=a_ , padding=a_ , return_tensors='''pt''' ) with torch.no_grad(): SCREAMING_SNAKE_CASE : str = model(**a_ ) if model_name == "groupvit-gcc-yfcc": SCREAMING_SNAKE_CASE : Union[str, Any] = torch.tensor([[13.3523, 6.3629]] ) elif model_name == "groupvit-gcc-redcaps": SCREAMING_SNAKE_CASE : List[Any] = torch.tensor([[16.1873, 8.6230]] ) else: raise ValueError(F"Model name {model_name} not supported." ) assert torch.allclose(outputs.logits_per_image , a_ , atol=1E-3 ) processor.save_pretrained(a_ ) model.save_pretrained(a_ ) print('''Successfully saved processor and model to''' , a_ ) if push_to_hub: print('''Pushing to the hub...''' ) processor.push_to_hub(a_ , organization='''nielsr''' ) model.push_to_hub(a_ , organization='''nielsr''' ) if __name__ == "__main__": lowerCamelCase__ : str = argparse.ArgumentParser() parser.add_argument( "--pytorch_dump_folder_path", default=None, type=str, help="Path to dump the processor and PyTorch model." ) parser.add_argument("--checkpoint_path", default=None, type=str, help="Path to GroupViT checkpoint") parser.add_argument( "--model_name", default="groupvit-gccy-fcc", type=str, help="Name of the model. Expecting either 'groupvit-gcc-yfcc' or 'groupvit-gcc-redcaps'", ) parser.add_argument( "--push_to_hub", action="store_true", help="Whether or not to push the converted model and processor to the 🤗 hub using the provided `model_name`.", ) lowerCamelCase__ : Union[str, Any] = parser.parse_args() convert_groupvit_checkpoint(args.checkpoint_path, args.pytorch_dump_folder_path, args.model_name, args.push_to_hub)
18
"""simple docstring""" from collections import OrderedDict from typing import Any, Mapping, Optional from ... import PreTrainedTokenizer from ...configuration_utils import PretrainedConfig from ...file_utils import TensorType, is_torch_available from ...onnx import OnnxConfig, OnnxConfigWithPast, OnnxSeqaSeqConfigWithPast from ...onnx.utils import compute_effective_axis_dimension from ...utils import logging lowerCamelCase__ : Tuple = logging.get_logger(__name__) lowerCamelCase__ : str = { "facebook/blenderbot_small-90M": "https://huggingface.co/facebook/blenderbot_small-90M/resolve/main/config.json", # See all BlenderbotSmall models at https://huggingface.co/models?filter=blenderbot_small } class lowercase__( _UpperCAmelCase ): '''simple docstring''' UpperCamelCase = """blenderbot-small""" UpperCamelCase = ["""past_key_values"""] UpperCamelCase = {"""num_attention_heads""": """encoder_attention_heads""", """hidden_size""": """d_model"""} def __init__( self :Any , lowerCamelCase_ :Dict=5_02_65 , lowerCamelCase_ :str=5_12 , lowerCamelCase_ :Tuple=8 , lowerCamelCase_ :int=20_48 , lowerCamelCase_ :str=16 , lowerCamelCase_ :Optional[int]=8 , lowerCamelCase_ :str=20_48 , lowerCamelCase_ :Optional[Any]=16 , lowerCamelCase_ :Union[str, Any]=0.0 , lowerCamelCase_ :List[str]=0.0 , lowerCamelCase_ :Tuple=True , lowerCamelCase_ :Tuple=True , lowerCamelCase_ :int="gelu" , lowerCamelCase_ :Tuple=5_12 , lowerCamelCase_ :Optional[int]=0.1 , lowerCamelCase_ :int=0.0 , lowerCamelCase_ :Tuple=0.0 , lowerCamelCase_ :Optional[int]=0.0_2 , lowerCamelCase_ :Union[str, Any]=1 , lowerCamelCase_ :Dict=False , lowerCamelCase_ :Optional[int]=0 , lowerCamelCase_ :List[Any]=1 , lowerCamelCase_ :Any=2 , lowerCamelCase_ :Optional[Any]=2 , **lowerCamelCase_ :Dict , ) -> Any: '''simple docstring''' SCREAMING_SNAKE_CASE : int = vocab_size SCREAMING_SNAKE_CASE : List[str] = max_position_embeddings SCREAMING_SNAKE_CASE : Optional[Any] = d_model SCREAMING_SNAKE_CASE : Dict = encoder_ffn_dim SCREAMING_SNAKE_CASE : Tuple = encoder_layers SCREAMING_SNAKE_CASE : Dict = encoder_attention_heads SCREAMING_SNAKE_CASE : Any = decoder_ffn_dim SCREAMING_SNAKE_CASE : str = decoder_layers SCREAMING_SNAKE_CASE : str = decoder_attention_heads SCREAMING_SNAKE_CASE : List[Any] = dropout SCREAMING_SNAKE_CASE : Optional[Any] = attention_dropout SCREAMING_SNAKE_CASE : Any = activation_dropout SCREAMING_SNAKE_CASE : List[str] = activation_function SCREAMING_SNAKE_CASE : Optional[int] = init_std SCREAMING_SNAKE_CASE : List[Any] = encoder_layerdrop SCREAMING_SNAKE_CASE : Union[str, Any] = decoder_layerdrop SCREAMING_SNAKE_CASE : List[Any] = use_cache SCREAMING_SNAKE_CASE : Union[str, Any] = encoder_layers SCREAMING_SNAKE_CASE : List[Any] = scale_embedding # scale factor will be sqrt(d_model) if True super().__init__( pad_token_id=lowerCamelCase_ , bos_token_id=lowerCamelCase_ , eos_token_id=lowerCamelCase_ , is_encoder_decoder=lowerCamelCase_ , decoder_start_token_id=lowerCamelCase_ , forced_eos_token_id=lowerCamelCase_ , **lowerCamelCase_ , ) class lowercase__( _UpperCAmelCase ): '''simple docstring''' @property def __lowerCAmelCase ( self :Any ) -> Mapping[str, Mapping[int, str]]: '''simple docstring''' if self.task in ["default", "seq2seq-lm"]: SCREAMING_SNAKE_CASE : Tuple = OrderedDict( [ ('''input_ids''', {0: '''batch''', 1: '''encoder_sequence'''}), ('''attention_mask''', {0: '''batch''', 1: '''encoder_sequence'''}), ] ) if self.use_past: SCREAMING_SNAKE_CASE : Union[str, Any] = {0: '''batch'''} SCREAMING_SNAKE_CASE : List[Any] = {0: '''batch''', 1: '''past_decoder_sequence + sequence'''} else: SCREAMING_SNAKE_CASE : Optional[Any] = {0: '''batch''', 1: '''decoder_sequence'''} SCREAMING_SNAKE_CASE : Union[str, Any] = {0: '''batch''', 1: '''decoder_sequence'''} if self.use_past: self.fill_with_past_key_values_(lowerCamelCase_ , direction='''inputs''' ) elif self.task == "causal-lm": # TODO: figure this case out. SCREAMING_SNAKE_CASE : Tuple = OrderedDict( [ ('''input_ids''', {0: '''batch''', 1: '''encoder_sequence'''}), ('''attention_mask''', {0: '''batch''', 1: '''encoder_sequence'''}), ] ) if self.use_past: SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : List[Any] = self.num_layers for i in range(lowerCamelCase_ ): SCREAMING_SNAKE_CASE : int = {0: '''batch''', 2: '''past_sequence + sequence'''} SCREAMING_SNAKE_CASE : List[str] = {0: '''batch''', 2: '''past_sequence + sequence'''} else: SCREAMING_SNAKE_CASE : Any = OrderedDict( [ ('''input_ids''', {0: '''batch''', 1: '''encoder_sequence'''}), ('''attention_mask''', {0: '''batch''', 1: '''encoder_sequence'''}), ('''decoder_input_ids''', {0: '''batch''', 1: '''decoder_sequence'''}), ('''decoder_attention_mask''', {0: '''batch''', 1: '''decoder_sequence'''}), ] ) return common_inputs @property def __lowerCAmelCase ( self :List[str] ) -> Mapping[str, Mapping[int, str]]: '''simple docstring''' if self.task in ["default", "seq2seq-lm"]: SCREAMING_SNAKE_CASE : Any = super().outputs else: SCREAMING_SNAKE_CASE : Tuple = super(lowerCamelCase_ , self ).outputs if self.use_past: SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : Union[str, Any] = self.num_layers for i in range(lowerCamelCase_ ): SCREAMING_SNAKE_CASE : Union[str, Any] = {0: '''batch''', 2: '''past_sequence + sequence'''} SCREAMING_SNAKE_CASE : str = {0: '''batch''', 2: '''past_sequence + sequence'''} return common_outputs def __lowerCAmelCase ( self :int , lowerCamelCase_ :PreTrainedTokenizer , lowerCamelCase_ :int = -1 , lowerCamelCase_ :int = -1 , lowerCamelCase_ :bool = False , lowerCamelCase_ :Optional[TensorType] = None , ) -> Mapping[str, Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : Dict = self._generate_dummy_inputs_for_sequence_classification_and_question_answering( lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ ) # Generate decoder inputs SCREAMING_SNAKE_CASE : Optional[int] = seq_length if not self.use_past else 1 SCREAMING_SNAKE_CASE : Optional[int] = self._generate_dummy_inputs_for_sequence_classification_and_question_answering( lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ ) SCREAMING_SNAKE_CASE : List[Any] = {f"decoder_{name}": tensor for name, tensor in decoder_inputs.items()} SCREAMING_SNAKE_CASE : str = dict(**lowerCamelCase_ , **lowerCamelCase_ ) if self.use_past: if not is_torch_available(): raise ValueError('''Cannot generate dummy past_keys inputs without PyTorch installed.''' ) else: import torch SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : Any = common_inputs['''input_ids'''].shape SCREAMING_SNAKE_CASE : str = common_inputs['''decoder_input_ids'''].shape[1] SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : Any = self.num_attention_heads SCREAMING_SNAKE_CASE : str = ( batch, num_encoder_attention_heads, encoder_seq_length, self._config.hidden_size // num_encoder_attention_heads, ) SCREAMING_SNAKE_CASE : Optional[Any] = decoder_seq_length + 3 SCREAMING_SNAKE_CASE : int = ( batch, num_decoder_attention_heads, decoder_past_length, self._config.hidden_size // num_decoder_attention_heads, ) SCREAMING_SNAKE_CASE : List[Any] = torch.cat( [common_inputs['''decoder_attention_mask'''], torch.ones(lowerCamelCase_ , lowerCamelCase_ )] , dim=1 ) SCREAMING_SNAKE_CASE : Optional[int] = [] # If the number of encoder and decoder layers are present in the model configuration, both are considered SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : Optional[int] = self.num_layers SCREAMING_SNAKE_CASE : int = min(lowerCamelCase_ , lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Optional[int] = max(lowerCamelCase_ , lowerCamelCase_ ) - min_num_layers SCREAMING_SNAKE_CASE : Tuple = '''encoder''' if num_encoder_layers > num_decoder_layers else '''decoder''' for _ in range(lowerCamelCase_ ): common_inputs["past_key_values"].append( ( torch.zeros(lowerCamelCase_ ), torch.zeros(lowerCamelCase_ ), torch.zeros(lowerCamelCase_ ), torch.zeros(lowerCamelCase_ ), ) ) # TODO: test this. SCREAMING_SNAKE_CASE : int = encoder_shape if remaining_side_name == '''encoder''' else decoder_shape for _ in range(lowerCamelCase_ , lowerCamelCase_ ): common_inputs["past_key_values"].append((torch.zeros(lowerCamelCase_ ), torch.zeros(lowerCamelCase_ )) ) return common_inputs def __lowerCAmelCase ( self :Any , lowerCamelCase_ :PreTrainedTokenizer , lowerCamelCase_ :int = -1 , lowerCamelCase_ :int = -1 , lowerCamelCase_ :bool = False , lowerCamelCase_ :Optional[TensorType] = None , ) -> Mapping[str, Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : Any = self._generate_dummy_inputs_for_sequence_classification_and_question_answering( lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ ) if self.use_past: if not is_torch_available(): raise ValueError('''Cannot generate dummy past_keys inputs without PyTorch installed.''' ) else: import torch SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : Tuple = common_inputs['''input_ids'''].shape # Not using the same length for past_key_values SCREAMING_SNAKE_CASE : List[str] = seqlen + 2 SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : Dict = self.num_layers SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : List[Any] = self.num_attention_heads SCREAMING_SNAKE_CASE : Union[str, Any] = ( batch, num_encoder_attention_heads, past_key_values_length, self._config.hidden_size // num_encoder_attention_heads, ) SCREAMING_SNAKE_CASE : Tuple = common_inputs['''attention_mask'''].dtype SCREAMING_SNAKE_CASE : Any = torch.cat( [common_inputs['''attention_mask'''], torch.ones(lowerCamelCase_ , lowerCamelCase_ , dtype=lowerCamelCase_ )] , dim=1 ) SCREAMING_SNAKE_CASE : Optional[int] = [ (torch.zeros(lowerCamelCase_ ), torch.zeros(lowerCamelCase_ )) for _ in range(lowerCamelCase_ ) ] return common_inputs def __lowerCAmelCase ( self :Union[str, Any] , lowerCamelCase_ :PreTrainedTokenizer , lowerCamelCase_ :int = -1 , lowerCamelCase_ :int = -1 , lowerCamelCase_ :bool = False , lowerCamelCase_ :Optional[TensorType] = None , ) -> Mapping[str, Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : List[str] = compute_effective_axis_dimension( lowerCamelCase_ , fixed_dimension=OnnxConfig.default_fixed_batch , num_token_to_add=0 ) # If dynamic axis (-1) we forward with a fixed dimension of 8 tokens to avoid optimizations made by ONNX SCREAMING_SNAKE_CASE : int = tokenizer.num_special_tokens_to_add(lowerCamelCase_ ) SCREAMING_SNAKE_CASE : int = compute_effective_axis_dimension( lowerCamelCase_ , fixed_dimension=OnnxConfig.default_fixed_sequence , num_token_to_add=lowerCamelCase_ ) # Generate dummy inputs according to compute batch and sequence SCREAMING_SNAKE_CASE : Tuple = [''' '''.join([tokenizer.unk_token] ) * seq_length] * batch_size SCREAMING_SNAKE_CASE : Any = dict(tokenizer(lowerCamelCase_ , return_tensors=lowerCamelCase_ ) ) return common_inputs def __lowerCAmelCase ( self :List[Any] , lowerCamelCase_ :PreTrainedTokenizer , lowerCamelCase_ :int = -1 , lowerCamelCase_ :int = -1 , lowerCamelCase_ :bool = False , lowerCamelCase_ :Optional[TensorType] = None , ) -> Mapping[str, Any]: '''simple docstring''' if self.task in ["default", "seq2seq-lm"]: SCREAMING_SNAKE_CASE : Dict = self._generate_dummy_inputs_for_default_and_seqaseq_lm( lowerCamelCase_ , batch_size=lowerCamelCase_ , seq_length=lowerCamelCase_ , is_pair=lowerCamelCase_ , framework=lowerCamelCase_ ) elif self.task == "causal-lm": SCREAMING_SNAKE_CASE : Union[str, Any] = self._generate_dummy_inputs_for_causal_lm( lowerCamelCase_ , batch_size=lowerCamelCase_ , seq_length=lowerCamelCase_ , is_pair=lowerCamelCase_ , framework=lowerCamelCase_ ) else: SCREAMING_SNAKE_CASE : List[Any] = self._generate_dummy_inputs_for_sequence_classification_and_question_answering( lowerCamelCase_ , batch_size=lowerCamelCase_ , seq_length=lowerCamelCase_ , is_pair=lowerCamelCase_ , framework=lowerCamelCase_ ) return common_inputs def __lowerCAmelCase ( self :Dict , lowerCamelCase_ :int , lowerCamelCase_ :List[str] , lowerCamelCase_ :List[str] , lowerCamelCase_ :Dict ) -> List[Any]: '''simple docstring''' if self.task in ["default", "seq2seq-lm"]: SCREAMING_SNAKE_CASE : Optional[Any] = super()._flatten_past_key_values_(lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ ) else: SCREAMING_SNAKE_CASE : Tuple = super(lowerCamelCase_ , self )._flatten_past_key_values_( lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ )
18
1
"""simple docstring""" import logging import os import random import sys from dataclasses import dataclass, field from typing import Optional import datasets import numpy as np import pandas as pd from datasets import load_dataset import transformers from transformers import ( AutoConfig, BartForSequenceClassification, DataCollatorWithPadding, EvalPrediction, HfArgumentParser, TapexTokenizer, Trainer, TrainingArguments, default_data_collator, set_seed, ) from transformers.trainer_utils import get_last_checkpoint from transformers.utils import check_min_version from transformers.utils.versions import require_version # Will error if the minimal version of Transformers is not installed. Remove at your own risks. check_min_version("4.17.0.dev0") require_version("datasets>=1.8.0", "To fix: pip install -r examples/pytorch/text-classification/requirements.txt") lowerCamelCase__ : Any = logging.getLogger(__name__) @dataclass class lowercase__: '''simple docstring''' UpperCamelCase = field( default="""tab_fact""" , metadata={"""help""": """The name of the dataset to use (via the datasets library)."""} ) UpperCamelCase = field( default="""tab_fact""" , metadata={"""help""": """The configuration name of the dataset to use (via the datasets library)."""} , ) UpperCamelCase = field( default=10_24 , metadata={ """help""": ( """The maximum total input sequence length after tokenization. Sequences longer """ """than this will be truncated, sequences shorter will be padded.""" ) } , ) UpperCamelCase = field( default=_UpperCAmelCase , metadata={"""help""": """Overwrite the cached preprocessed datasets or not."""} ) UpperCamelCase = field( default=_UpperCAmelCase , metadata={ """help""": ( """Whether to pad all samples to `max_seq_length`. """ """If False, will pad the samples dynamically when batching to the maximum length in the batch.""" ) } , ) UpperCamelCase = field( default=_UpperCAmelCase , metadata={ """help""": ( """For debugging purposes or quicker training, truncate the number of training examples to this """ """value if set.""" ) } , ) UpperCamelCase = field( default=_UpperCAmelCase , metadata={ """help""": ( """For debugging purposes or quicker training, truncate the number of evaluation examples to this """ """value if set.""" ) } , ) UpperCamelCase = field( default=_UpperCAmelCase , metadata={ """help""": ( """For debugging purposes or quicker training, truncate the number of prediction examples to this """ """value if set.""" ) } , ) UpperCamelCase = field( default=_UpperCAmelCase , metadata={"""help""": """A csv or a json file containing the training data."""} ) UpperCamelCase = field( default=_UpperCAmelCase , metadata={"""help""": """A csv or a json file containing the validation data."""} ) UpperCamelCase = field(default=_UpperCAmelCase , metadata={"""help""": """A csv or a json file containing the test data."""} ) def __lowerCAmelCase ( self :Union[str, Any] ) -> Dict: '''simple docstring''' if self.dataset_name is not None: pass elif self.train_file is None or self.validation_file is None: raise ValueError('''Need either a GLUE task, a training/validation file or a dataset name.''' ) else: SCREAMING_SNAKE_CASE : Union[str, Any] = self.train_file.split('''.''' )[-1] assert train_extension in ["csv", "json"], "`train_file` should be a csv or a json file." SCREAMING_SNAKE_CASE : Optional[int] = self.validation_file.split('''.''' )[-1] assert ( validation_extension == train_extension ), "`validation_file` should have the same extension (csv or json) as `train_file`." @dataclass class lowercase__: '''simple docstring''' UpperCamelCase = field( default=_UpperCAmelCase , metadata={"""help""": """Path to pretrained model or model identifier from huggingface.co/models"""} ) UpperCamelCase = field( default=_UpperCAmelCase , metadata={"""help""": """Pretrained config name or path if not the same as model_name"""} ) UpperCamelCase = field( default=_UpperCAmelCase , metadata={"""help""": """Pretrained tokenizer name or path if not the same as model_name"""} ) UpperCamelCase = field( default=_UpperCAmelCase , metadata={"""help""": """Where do you want to store the pretrained models downloaded from huggingface.co"""} , ) UpperCamelCase = field( default=_UpperCAmelCase , metadata={"""help""": """Whether to use one of the fast tokenizer (backed by the tokenizers library) or not."""} , ) UpperCamelCase = field( default="""main""" , metadata={"""help""": """The specific model version to use (can be a branch name, tag name or commit id)."""} , ) UpperCamelCase = field( default=_UpperCAmelCase , metadata={ """help""": ( """Will use the token generated when running `huggingface-cli login` (necessary to use this script """ """with private models).""" ) } , ) def __A ( )-> List[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : Union[str, Any] = 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 : Dict = parser.parse_json_file(json_file=os.path.abspath(sys.argv[1] ) ) else: SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : Dict = parser.parse_args_into_dataclasses() # 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 )] , ) SCREAMING_SNAKE_CASE : Union[str, Any] = training_args.get_process_log_level() logger.setLevel(a_ ) datasets.utils.logging.set_verbosity(a_ ) transformers.utils.logging.set_verbosity(a_ ) 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 : 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 ) # Get the datasets: you can either provide your own CSV/JSON training and evaluation files (see below) # or specify a GLUE benchmark task (the dataset will be downloaded automatically from the datasets Hub). # # For JSON files, this script will use the `question` column for the input question and `table` column for the corresponding table. # # If the CSVs/JSONs contain only one non-label column, the script does single sentence classification on this # single column. You can easily tweak this behavior (see below) # # In distributed training, the load_dataset function guarantee that only one local process can concurrently # download the dataset. if data_args.dataset_name is not None: # Downloading and loading a dataset from the hub. SCREAMING_SNAKE_CASE : List[str] = load_dataset( data_args.dataset_name , data_args.dataset_config_name , cache_dir=model_args.cache_dir ) else: # Loading a dataset from your local files. # CSV/JSON training and evaluation files are needed. SCREAMING_SNAKE_CASE : Any = {'''train''': data_args.train_file, '''validation''': data_args.validation_file} # Get the test dataset: you can provide your own CSV/JSON test file (see below) # when you use `do_predict` without specifying a GLUE benchmark task. if training_args.do_predict: if data_args.test_file is not None: SCREAMING_SNAKE_CASE : List[Any] = data_args.train_file.split('''.''' )[-1] SCREAMING_SNAKE_CASE : Optional[int] = data_args.test_file.split('''.''' )[-1] assert ( test_extension == train_extension ), "`test_file` should have the same extension (csv or json) as `train_file`." SCREAMING_SNAKE_CASE : str = data_args.test_file else: raise ValueError('''Need either a GLUE task or a test file for `do_predict`.''' ) for key in data_files.keys(): logger.info(F"load a local file for {key}: {data_files[key]}" ) if data_args.train_file.endswith('''.csv''' ): # Loading a dataset from local csv files SCREAMING_SNAKE_CASE : int = load_dataset('''csv''' , data_files=a_ , cache_dir=model_args.cache_dir ) else: # Loading a dataset from local json files SCREAMING_SNAKE_CASE : Tuple = load_dataset('''json''' , data_files=a_ , cache_dir=model_args.cache_dir ) # See more about loading any type of standard or custom dataset at # https://huggingface.co/docs/datasets/loading_datasets.html. # Labels SCREAMING_SNAKE_CASE : str = raw_datasets['''train'''].features['''label'''].names SCREAMING_SNAKE_CASE : Union[str, Any] = len(a_ ) # Load pretrained model and tokenizer # # In distributed training, the .from_pretrained methods guarantee that only one local process can concurrently # download model & vocab. SCREAMING_SNAKE_CASE : Optional[Any] = AutoConfig.from_pretrained( model_args.config_name if model_args.config_name else model_args.model_name_or_path , num_labels=a_ , cache_dir=model_args.cache_dir , revision=model_args.model_revision , use_auth_token=True if model_args.use_auth_token else None , ) # load tapex tokenizer SCREAMING_SNAKE_CASE : Dict = TapexTokenizer.from_pretrained( model_args.tokenizer_name if model_args.tokenizer_name else model_args.model_name_or_path , cache_dir=model_args.cache_dir , use_fast=model_args.use_fast_tokenizer , revision=model_args.model_revision , use_auth_token=True if model_args.use_auth_token else None , add_prefix_space=a_ , ) SCREAMING_SNAKE_CASE : List[Any] = BartForSequenceClassification.from_pretrained( model_args.model_name_or_path , from_tf=bool('''.ckpt''' in model_args.model_name_or_path ) , config=a_ , cache_dir=model_args.cache_dir , revision=model_args.model_revision , use_auth_token=True if model_args.use_auth_token else None , ) # Padding strategy if data_args.pad_to_max_length: SCREAMING_SNAKE_CASE : Tuple = '''max_length''' else: # We will pad later, dynamically at batch creation, to the max sequence length in each batch SCREAMING_SNAKE_CASE : Optional[Any] = False # Some models have set the order of the labels to use, so let's make sure we do use it. SCREAMING_SNAKE_CASE : Tuple = {'''Refused''': 0, '''Entailed''': 1} SCREAMING_SNAKE_CASE : List[Any] = {0: '''Refused''', 1: '''Entailed'''} if data_args.max_seq_length > tokenizer.model_max_length: logger.warning( F"The max_seq_length passed ({data_args.max_seq_length}) is larger than the maximum length for the" F"model ({tokenizer.model_max_length}). Using max_seq_length={tokenizer.model_max_length}." ) SCREAMING_SNAKE_CASE : Optional[int] = min(data_args.max_seq_length , tokenizer.model_max_length ) def preprocess_tabfact_function(a_ : str ): # Tokenize the texts def _convert_table_text_to_pandas(a_ : List[Any] ): SCREAMING_SNAKE_CASE : List[Any] = [_table_row.split('''#''' ) for _table_row in _table_text.strip('''\n''' ).split('''\n''' )] SCREAMING_SNAKE_CASE : Dict = pd.DataFrame.from_records(_table_content[1:] , columns=_table_content[0] ) return _table_pd SCREAMING_SNAKE_CASE : List[Any] = examples['''statement'''] SCREAMING_SNAKE_CASE : Optional[int] = list(map(_convert_table_text_to_pandas , examples['''table_text'''] ) ) SCREAMING_SNAKE_CASE : Any = tokenizer(a_ , a_ , padding=a_ , max_length=a_ , truncation=a_ ) SCREAMING_SNAKE_CASE : List[Any] = examples['''label'''] return result with training_args.main_process_first(desc='''dataset map pre-processing''' ): SCREAMING_SNAKE_CASE : Optional[Any] = raw_datasets.map( a_ , batched=a_ , load_from_cache_file=not data_args.overwrite_cache , desc='''Running tokenizer on dataset''' , ) if training_args.do_train: if "train" not in raw_datasets: raise ValueError('''--do_train requires a train dataset''' ) SCREAMING_SNAKE_CASE : List[str] = raw_datasets['''train'''] if data_args.max_train_samples is not None: SCREAMING_SNAKE_CASE : Any = train_dataset.select(range(data_args.max_train_samples ) ) if training_args.do_eval: if "validation" not in raw_datasets and "validation_matched" not in raw_datasets: raise ValueError('''--do_eval requires a validation dataset''' ) SCREAMING_SNAKE_CASE : List[str] = raw_datasets['''validation'''] if data_args.max_eval_samples is not None: SCREAMING_SNAKE_CASE : Union[str, Any] = eval_dataset.select(range(data_args.max_eval_samples ) ) if training_args.do_predict or data_args.test_file is not None: if "test" not in raw_datasets and "test_matched" not in raw_datasets: raise ValueError('''--do_predict requires a test dataset''' ) SCREAMING_SNAKE_CASE : Tuple = raw_datasets['''test'''] if data_args.max_predict_samples is not None: SCREAMING_SNAKE_CASE : Optional[int] = predict_dataset.select(range(data_args.max_predict_samples ) ) # Log a few random samples from the training set: if training_args.do_train: for index in random.sample(range(len(a_ ) ) , 3 ): logger.info(F"Sample {index} of the training set: {train_dataset[index]}." ) # You can define your custom 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(a_ : EvalPrediction ): SCREAMING_SNAKE_CASE : str = p.predictions[0] if isinstance(p.predictions , a_ ) else p.predictions SCREAMING_SNAKE_CASE : Tuple = np.argmax(a_ , axis=1 ) return {"accuracy": (preds == p.label_ids).astype(np.floataa ).mean().item()} # Data collator will default to DataCollatorWithPadding, so we change it if we already did the padding. if data_args.pad_to_max_length: SCREAMING_SNAKE_CASE : Tuple = default_data_collator elif training_args.fpaa: SCREAMING_SNAKE_CASE : Union[str, Any] = DataCollatorWithPadding(a_ , pad_to_multiple_of=8 ) else: SCREAMING_SNAKE_CASE : List[Any] = None # Initialize our Trainer SCREAMING_SNAKE_CASE : Optional[Any] = Trainer( model=a_ , args=a_ , train_dataset=train_dataset if training_args.do_train else None , eval_dataset=eval_dataset if training_args.do_eval else None , compute_metrics=a_ , tokenizer=a_ , data_collator=a_ , ) # Training if training_args.do_train: SCREAMING_SNAKE_CASE : List[str] = None if training_args.resume_from_checkpoint is not None: SCREAMING_SNAKE_CASE : Dict = training_args.resume_from_checkpoint elif last_checkpoint is not None: SCREAMING_SNAKE_CASE : str = last_checkpoint SCREAMING_SNAKE_CASE : str = trainer.train(resume_from_checkpoint=a_ ) SCREAMING_SNAKE_CASE : Optional[int] = train_result.metrics SCREAMING_SNAKE_CASE : int = ( data_args.max_train_samples if data_args.max_train_samples is not None else len(a_ ) ) SCREAMING_SNAKE_CASE : Optional[int] = min(a_ , len(a_ ) ) trainer.save_model() # Saves the tokenizer too for easy upload trainer.log_metrics('''train''' , a_ ) trainer.save_metrics('''train''' , a_ ) trainer.save_state() # Evaluation if training_args.do_eval: logger.info('''*** Evaluate ***''' ) SCREAMING_SNAKE_CASE : Tuple = trainer.evaluate(eval_dataset=a_ ) SCREAMING_SNAKE_CASE : str = data_args.max_eval_samples if data_args.max_eval_samples is not None else len(a_ ) SCREAMING_SNAKE_CASE : Union[str, Any] = min(a_ , len(a_ ) ) trainer.log_metrics('''eval''' , a_ ) trainer.save_metrics('''eval''' , a_ ) if training_args.do_predict: logger.info('''*** Predict ***''' ) # Removing the `label` columns because it contains -1 and Trainer won't like that. SCREAMING_SNAKE_CASE : Optional[Any] = predict_dataset.remove_columns('''label''' ) SCREAMING_SNAKE_CASE : Optional[Any] = trainer.predict(a_ , metric_key_prefix='''predict''' ).predictions SCREAMING_SNAKE_CASE : Union[str, Any] = np.argmax(a_ , axis=1 ) SCREAMING_SNAKE_CASE : Optional[int] = os.path.join(training_args.output_dir , '''predict_results_tabfact.txt''' ) if trainer.is_world_process_zero(): with open(a_ , '''w''' ) as writer: logger.info('''***** Predict Results *****''' ) writer.write('''index\tprediction\n''' ) for index, item in enumerate(a_ ): SCREAMING_SNAKE_CASE : Union[str, Any] = label_list[item] writer.write(F"{index}\t{item}\n" ) SCREAMING_SNAKE_CASE : Optional[int] = {'''finetuned_from''': model_args.model_name_or_path, '''tasks''': '''text-classification'''} if training_args.push_to_hub: trainer.push_to_hub(**a_ ) else: trainer.create_model_card(**a_ ) def __A ( a_ : List[str] )-> int: '''simple docstring''' main() if __name__ == "__main__": main()
18
"""simple docstring""" from collections import OrderedDict from typing import TYPE_CHECKING, Any, Mapping, Optional from packaging import version from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...onnx.utils import compute_effective_axis_dimension from ...utils import logging if TYPE_CHECKING: from ...processing_utils import ProcessorMixin from ...utils import TensorType lowerCamelCase__ : Dict = logging.get_logger(__name__) lowerCamelCase__ : Dict = { "microsoft/layoutlmv3-base": "https://huggingface.co/microsoft/layoutlmv3-base/resolve/main/config.json", } class lowercase__( _UpperCAmelCase ): '''simple docstring''' UpperCamelCase = """layoutlmv3""" def __init__( self :str , lowerCamelCase_ :Optional[Any]=5_02_65 , lowerCamelCase_ :Dict=7_68 , lowerCamelCase_ :Union[str, Any]=12 , lowerCamelCase_ :Optional[Any]=12 , lowerCamelCase_ :Union[str, Any]=30_72 , lowerCamelCase_ :Any="gelu" , lowerCamelCase_ :Union[str, Any]=0.1 , lowerCamelCase_ :str=0.1 , lowerCamelCase_ :Any=5_12 , lowerCamelCase_ :int=2 , lowerCamelCase_ :Optional[Any]=0.0_2 , lowerCamelCase_ :Optional[int]=1E-5 , lowerCamelCase_ :Dict=1 , lowerCamelCase_ :int=0 , lowerCamelCase_ :Tuple=2 , lowerCamelCase_ :List[str]=10_24 , lowerCamelCase_ :Tuple=1_28 , lowerCamelCase_ :Any=1_28 , lowerCamelCase_ :Optional[Any]=True , lowerCamelCase_ :str=32 , lowerCamelCase_ :int=1_28 , lowerCamelCase_ :int=64 , lowerCamelCase_ :List[Any]=2_56 , lowerCamelCase_ :Any=True , lowerCamelCase_ :str=True , lowerCamelCase_ :Union[str, Any]=True , lowerCamelCase_ :List[str]=2_24 , lowerCamelCase_ :Dict=3 , lowerCamelCase_ :Union[str, Any]=16 , lowerCamelCase_ :Any=None , **lowerCamelCase_ :Optional[Any] , ) -> int: '''simple docstring''' super().__init__( vocab_size=lowerCamelCase_ , hidden_size=lowerCamelCase_ , num_hidden_layers=lowerCamelCase_ , num_attention_heads=lowerCamelCase_ , intermediate_size=lowerCamelCase_ , hidden_act=lowerCamelCase_ , hidden_dropout_prob=lowerCamelCase_ , attention_probs_dropout_prob=lowerCamelCase_ , max_position_embeddings=lowerCamelCase_ , type_vocab_size=lowerCamelCase_ , initializer_range=lowerCamelCase_ , layer_norm_eps=lowerCamelCase_ , pad_token_id=lowerCamelCase_ , bos_token_id=lowerCamelCase_ , eos_token_id=lowerCamelCase_ , **lowerCamelCase_ , ) SCREAMING_SNAKE_CASE : Optional[Any] = max_ad_position_embeddings SCREAMING_SNAKE_CASE : List[Any] = coordinate_size SCREAMING_SNAKE_CASE : Tuple = shape_size SCREAMING_SNAKE_CASE : Optional[int] = has_relative_attention_bias SCREAMING_SNAKE_CASE : List[Any] = rel_pos_bins SCREAMING_SNAKE_CASE : int = max_rel_pos SCREAMING_SNAKE_CASE : Any = has_spatial_attention_bias SCREAMING_SNAKE_CASE : List[Any] = rel_ad_pos_bins SCREAMING_SNAKE_CASE : Dict = max_rel_ad_pos SCREAMING_SNAKE_CASE : Optional[int] = text_embed SCREAMING_SNAKE_CASE : Any = visual_embed SCREAMING_SNAKE_CASE : Any = input_size SCREAMING_SNAKE_CASE : Tuple = num_channels SCREAMING_SNAKE_CASE : List[str] = patch_size SCREAMING_SNAKE_CASE : str = classifier_dropout class lowercase__( _UpperCAmelCase ): '''simple docstring''' UpperCamelCase = version.parse("""1.12""" ) @property def __lowerCAmelCase ( self :List[Any] ) -> Mapping[str, Mapping[int, str]]: '''simple docstring''' if self.task in ["question-answering", "sequence-classification"]: return OrderedDict( [ ('''input_ids''', {0: '''batch''', 1: '''sequence'''}), ('''attention_mask''', {0: '''batch''', 1: '''sequence'''}), ('''bbox''', {0: '''batch''', 1: '''sequence'''}), ('''pixel_values''', {0: '''batch''', 1: '''num_channels''', 2: '''height''', 3: '''width'''}), ] ) else: return OrderedDict( [ ('''input_ids''', {0: '''batch''', 1: '''sequence'''}), ('''bbox''', {0: '''batch''', 1: '''sequence'''}), ('''attention_mask''', {0: '''batch''', 1: '''sequence'''}), ('''pixel_values''', {0: '''batch''', 1: '''num_channels'''}), ] ) @property def __lowerCAmelCase ( self :Optional[int] ) -> float: '''simple docstring''' return 1E-5 @property def __lowerCAmelCase ( self :Tuple ) -> int: '''simple docstring''' return 12 def __lowerCAmelCase ( self :List[Any] , lowerCamelCase_ :"ProcessorMixin" , lowerCamelCase_ :int = -1 , lowerCamelCase_ :int = -1 , lowerCamelCase_ :bool = False , lowerCamelCase_ :Optional["TensorType"] = None , lowerCamelCase_ :int = 3 , lowerCamelCase_ :int = 40 , lowerCamelCase_ :int = 40 , ) -> Mapping[str, Any]: '''simple docstring''' setattr(processor.image_processor , '''apply_ocr''' , lowerCamelCase_ ) # If dynamic axis (-1) we forward with a fixed dimension of 2 samples to avoid optimizations made by ONNX SCREAMING_SNAKE_CASE : Dict = compute_effective_axis_dimension( lowerCamelCase_ , fixed_dimension=OnnxConfig.default_fixed_batch , num_token_to_add=0 ) # If dynamic axis (-1) we forward with a fixed dimension of 8 tokens to avoid optimizations made by ONNX SCREAMING_SNAKE_CASE : Union[str, Any] = processor.tokenizer.num_special_tokens_to_add(lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Any = compute_effective_axis_dimension( lowerCamelCase_ , fixed_dimension=OnnxConfig.default_fixed_sequence , num_token_to_add=lowerCamelCase_ ) # Generate dummy inputs according to compute batch and sequence SCREAMING_SNAKE_CASE : Union[str, Any] = [[''' '''.join([processor.tokenizer.unk_token] ) * seq_length]] * batch_size # Generate dummy bounding boxes SCREAMING_SNAKE_CASE : int = [[[48, 84, 73, 1_28]]] * batch_size # If dynamic axis (-1) we forward with a fixed dimension of 2 samples to avoid optimizations made by ONNX # batch_size = compute_effective_axis_dimension(batch_size, fixed_dimension=OnnxConfig.default_fixed_batch) SCREAMING_SNAKE_CASE : List[Any] = self._generate_dummy_images(lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ ) SCREAMING_SNAKE_CASE : int = dict( processor( lowerCamelCase_ , text=lowerCamelCase_ , boxes=lowerCamelCase_ , return_tensors=lowerCamelCase_ , ) ) return inputs
18
1
"""simple docstring""" from ...configuration_utils import PretrainedConfig from ...utils import logging lowerCamelCase__ : List[Any] = logging.get_logger(__name__) lowerCamelCase__ : Tuple = { "facebook/dpr-ctx_encoder-single-nq-base": ( "https://huggingface.co/facebook/dpr-ctx_encoder-single-nq-base/resolve/main/config.json" ), "facebook/dpr-question_encoder-single-nq-base": ( "https://huggingface.co/facebook/dpr-question_encoder-single-nq-base/resolve/main/config.json" ), "facebook/dpr-reader-single-nq-base": ( "https://huggingface.co/facebook/dpr-reader-single-nq-base/resolve/main/config.json" ), "facebook/dpr-ctx_encoder-multiset-base": ( "https://huggingface.co/facebook/dpr-ctx_encoder-multiset-base/resolve/main/config.json" ), "facebook/dpr-question_encoder-multiset-base": ( "https://huggingface.co/facebook/dpr-question_encoder-multiset-base/resolve/main/config.json" ), "facebook/dpr-reader-multiset-base": ( "https://huggingface.co/facebook/dpr-reader-multiset-base/resolve/main/config.json" ), } class lowercase__( _UpperCAmelCase ): '''simple docstring''' UpperCamelCase = """dpr""" def __init__( self :Optional[int] , lowerCamelCase_ :Dict=3_05_22 , lowerCamelCase_ :Any=7_68 , lowerCamelCase_ :List[Any]=12 , lowerCamelCase_ :str=12 , lowerCamelCase_ :Union[str, Any]=30_72 , lowerCamelCase_ :List[Any]="gelu" , lowerCamelCase_ :str=0.1 , lowerCamelCase_ :Any=0.1 , lowerCamelCase_ :List[str]=5_12 , lowerCamelCase_ :Optional[Any]=2 , lowerCamelCase_ :str=0.0_2 , lowerCamelCase_ :Optional[Any]=1E-12 , lowerCamelCase_ :List[str]=0 , lowerCamelCase_ :Any="absolute" , lowerCamelCase_ :int = 0 , **lowerCamelCase_ :Tuple , ) -> Optional[Any]: '''simple docstring''' super().__init__(pad_token_id=lowerCamelCase_ , **lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Union[str, Any] = vocab_size SCREAMING_SNAKE_CASE : Any = hidden_size SCREAMING_SNAKE_CASE : int = num_hidden_layers SCREAMING_SNAKE_CASE : str = num_attention_heads SCREAMING_SNAKE_CASE : Optional[Any] = hidden_act SCREAMING_SNAKE_CASE : Dict = intermediate_size SCREAMING_SNAKE_CASE : int = hidden_dropout_prob SCREAMING_SNAKE_CASE : Optional[int] = attention_probs_dropout_prob SCREAMING_SNAKE_CASE : List[str] = max_position_embeddings SCREAMING_SNAKE_CASE : Optional[int] = type_vocab_size SCREAMING_SNAKE_CASE : Any = initializer_range SCREAMING_SNAKE_CASE : List[str] = layer_norm_eps SCREAMING_SNAKE_CASE : List[Any] = projection_dim SCREAMING_SNAKE_CASE : List[Any] = position_embedding_type
18
"""simple docstring""" import math def __A ( a_ : list , a_ : int )-> int: '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[int] = len(a_ ) SCREAMING_SNAKE_CASE : Optional[Any] = int(math.floor(math.sqrt(a_ ) ) ) SCREAMING_SNAKE_CASE : List[str] = 0 while arr[min(a_ , a_ ) - 1] < x: SCREAMING_SNAKE_CASE : Optional[Any] = step step += int(math.floor(math.sqrt(a_ ) ) ) if prev >= n: return -1 while arr[prev] < x: SCREAMING_SNAKE_CASE : Any = prev + 1 if prev == min(a_ , a_ ): return -1 if arr[prev] == x: return prev return -1 if __name__ == "__main__": lowerCamelCase__ : Union[str, Any] = input("Enter numbers separated by a comma:\n").strip() lowerCamelCase__ : List[str] = [int(item) for item in user_input.split(",")] lowerCamelCase__ : Dict = int(input("Enter the number to be searched:\n")) lowerCamelCase__ : Tuple = jump_search(arr, x) if res == -1: print("Number not found!") else: print(f'''Number {x} is at index {res}''')
18
1
"""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 lowercase__( unittest.TestCase ): '''simple docstring''' def __lowerCAmelCase ( self :int ) -> List[Any]: '''simple docstring''' super().tearDown() gc.collect() torch.cuda.empty_cache() @property def __lowerCAmelCase ( self :Any ) -> Tuple: '''simple docstring''' SCREAMING_SNAKE_CASE : Any = 1 SCREAMING_SNAKE_CASE : Any = 3 SCREAMING_SNAKE_CASE : str = (32, 32) SCREAMING_SNAKE_CASE : List[Any] = floats_tensor((batch_size, num_channels) + sizes , rng=random.Random(0 ) ).to(lowerCamelCase_ ) return image @property def __lowerCAmelCase ( self :List[str] ) -> Tuple: '''simple docstring''' torch.manual_seed(0 ) SCREAMING_SNAKE_CASE : Any = 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=lowerCamelCase_ , only_cross_attention=(True, True, False) , num_class_embeds=1_00 , ) return model @property def __lowerCAmelCase ( self :List[str] ) -> str: '''simple docstring''' torch.manual_seed(0 ) SCREAMING_SNAKE_CASE : Dict = 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 __lowerCAmelCase ( self :Tuple ) -> Optional[Any]: '''simple docstring''' torch.manual_seed(0 ) SCREAMING_SNAKE_CASE : Optional[Any] = CLIPTextConfig( bos_token_id=0 , eos_token_id=2 , hidden_size=32 , intermediate_size=37 , layer_norm_eps=1E-05 , num_attention_heads=4 , num_hidden_layers=5 , pad_token_id=1 , vocab_size=10_00 , hidden_act='''gelu''' , projection_dim=5_12 , ) return CLIPTextModel(lowerCamelCase_ ) def __lowerCAmelCase ( self :Tuple ) -> Optional[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : int = '''cpu''' # ensure determinism for the device-dependent torch.Generator SCREAMING_SNAKE_CASE : Tuple = self.dummy_cond_unet_upscale SCREAMING_SNAKE_CASE : int = DDPMScheduler() SCREAMING_SNAKE_CASE : Optional[int] = DDIMScheduler(prediction_type='''v_prediction''' ) SCREAMING_SNAKE_CASE : List[Any] = self.dummy_vae SCREAMING_SNAKE_CASE : Tuple = self.dummy_text_encoder SCREAMING_SNAKE_CASE : List[Any] = CLIPTokenizer.from_pretrained('''hf-internal-testing/tiny-random-clip''' ) SCREAMING_SNAKE_CASE : List[Any] = self.dummy_image.cpu().permute(0 , 2 , 3 , 1 )[0] SCREAMING_SNAKE_CASE : Union[str, Any] = Image.fromarray(np.uinta(lowerCamelCase_ ) ).convert('''RGB''' ).resize((64, 64) ) # make sure here that pndm scheduler skips prk SCREAMING_SNAKE_CASE : int = StableDiffusionUpscalePipeline( unet=lowerCamelCase_ , low_res_scheduler=lowerCamelCase_ , scheduler=lowerCamelCase_ , vae=lowerCamelCase_ , text_encoder=lowerCamelCase_ , tokenizer=lowerCamelCase_ , max_noise_level=3_50 , ) SCREAMING_SNAKE_CASE : str = sd_pipe.to(lowerCamelCase_ ) sd_pipe.set_progress_bar_config(disable=lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Union[str, Any] = '''A painting of a squirrel eating a burger''' SCREAMING_SNAKE_CASE : Union[str, Any] = torch.Generator(device=lowerCamelCase_ ).manual_seed(0 ) SCREAMING_SNAKE_CASE : List[Any] = sd_pipe( [prompt] , image=lowerCamelCase_ , generator=lowerCamelCase_ , guidance_scale=6.0 , noise_level=20 , num_inference_steps=2 , output_type='''np''' , ) SCREAMING_SNAKE_CASE : Union[str, Any] = output.images SCREAMING_SNAKE_CASE : List[Any] = torch.Generator(device=lowerCamelCase_ ).manual_seed(0 ) SCREAMING_SNAKE_CASE : Tuple = sd_pipe( [prompt] , image=lowerCamelCase_ , generator=lowerCamelCase_ , guidance_scale=6.0 , noise_level=20 , num_inference_steps=2 , output_type='''np''' , return_dict=lowerCamelCase_ , )[0] SCREAMING_SNAKE_CASE : str = image[0, -3:, -3:, -1] SCREAMING_SNAKE_CASE : Optional[Any] = image_from_tuple[0, -3:, -3:, -1] SCREAMING_SNAKE_CASE : List[str] = low_res_image.size[0] * 4 assert image.shape == (1, expected_height_width, expected_height_width, 3) SCREAMING_SNAKE_CASE : List[Any] = np.array([0.3_1_1_3, 0.3_9_1_0, 0.4_2_7_2, 0.4_8_5_9, 0.5_0_6_1, 0.4_6_5_2, 0.5_3_6_2, 0.5_7_1_5, 0.5_6_6_1] ) 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 __lowerCAmelCase ( self :List[str] ) -> Tuple: '''simple docstring''' SCREAMING_SNAKE_CASE : Dict = '''cpu''' # ensure determinism for the device-dependent torch.Generator SCREAMING_SNAKE_CASE : Union[str, Any] = self.dummy_cond_unet_upscale SCREAMING_SNAKE_CASE : Any = DDPMScheduler() SCREAMING_SNAKE_CASE : Optional[int] = DDIMScheduler(prediction_type='''v_prediction''' ) SCREAMING_SNAKE_CASE : List[str] = self.dummy_vae SCREAMING_SNAKE_CASE : int = self.dummy_text_encoder SCREAMING_SNAKE_CASE : Optional[Any] = CLIPTokenizer.from_pretrained('''hf-internal-testing/tiny-random-clip''' ) SCREAMING_SNAKE_CASE : Optional[Any] = self.dummy_image.cpu().permute(0 , 2 , 3 , 1 )[0] SCREAMING_SNAKE_CASE : List[Any] = Image.fromarray(np.uinta(lowerCamelCase_ ) ).convert('''RGB''' ).resize((64, 64) ) # make sure here that pndm scheduler skips prk SCREAMING_SNAKE_CASE : int = StableDiffusionUpscalePipeline( unet=lowerCamelCase_ , low_res_scheduler=lowerCamelCase_ , scheduler=lowerCamelCase_ , vae=lowerCamelCase_ , text_encoder=lowerCamelCase_ , tokenizer=lowerCamelCase_ , max_noise_level=3_50 , ) SCREAMING_SNAKE_CASE : Any = sd_pipe.to(lowerCamelCase_ ) sd_pipe.set_progress_bar_config(disable=lowerCamelCase_ ) SCREAMING_SNAKE_CASE : int = '''A painting of a squirrel eating a burger''' SCREAMING_SNAKE_CASE : List[str] = sd_pipe( 2 * [prompt] , image=2 * [low_res_image] , guidance_scale=6.0 , noise_level=20 , num_inference_steps=2 , output_type='''np''' , ) SCREAMING_SNAKE_CASE : Union[str, Any] = output.images assert image.shape[0] == 2 SCREAMING_SNAKE_CASE : Optional[Any] = torch.Generator(device=lowerCamelCase_ ).manual_seed(0 ) SCREAMING_SNAKE_CASE : Optional[Any] = sd_pipe( [prompt] , image=lowerCamelCase_ , generator=lowerCamelCase_ , num_images_per_prompt=2 , guidance_scale=6.0 , noise_level=20 , num_inference_steps=2 , output_type='''np''' , ) SCREAMING_SNAKE_CASE : Optional[Any] = output.images assert image.shape[0] == 2 @unittest.skipIf(torch_device != '''cuda''' , '''This test requires a GPU''' ) def __lowerCAmelCase ( self :Union[str, Any] ) -> Optional[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : List[str] = self.dummy_cond_unet_upscale SCREAMING_SNAKE_CASE : Dict = DDPMScheduler() SCREAMING_SNAKE_CASE : str = DDIMScheduler(prediction_type='''v_prediction''' ) SCREAMING_SNAKE_CASE : int = self.dummy_vae SCREAMING_SNAKE_CASE : Optional[Any] = self.dummy_text_encoder SCREAMING_SNAKE_CASE : Any = CLIPTokenizer.from_pretrained('''hf-internal-testing/tiny-random-clip''' ) SCREAMING_SNAKE_CASE : str = self.dummy_image.cpu().permute(0 , 2 , 3 , 1 )[0] SCREAMING_SNAKE_CASE : Union[str, Any] = Image.fromarray(np.uinta(lowerCamelCase_ ) ).convert('''RGB''' ).resize((64, 64) ) # put models in fp16, except vae as it overflows in fp16 SCREAMING_SNAKE_CASE : List[str] = unet.half() SCREAMING_SNAKE_CASE : List[Any] = text_encoder.half() # make sure here that pndm scheduler skips prk SCREAMING_SNAKE_CASE : int = StableDiffusionUpscalePipeline( unet=lowerCamelCase_ , low_res_scheduler=lowerCamelCase_ , scheduler=lowerCamelCase_ , vae=lowerCamelCase_ , text_encoder=lowerCamelCase_ , tokenizer=lowerCamelCase_ , max_noise_level=3_50 , ) SCREAMING_SNAKE_CASE : Any = sd_pipe.to(lowerCamelCase_ ) sd_pipe.set_progress_bar_config(disable=lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Any = '''A painting of a squirrel eating a burger''' SCREAMING_SNAKE_CASE : Any = torch.manual_seed(0 ) SCREAMING_SNAKE_CASE : Dict = sd_pipe( [prompt] , image=lowerCamelCase_ , generator=lowerCamelCase_ , num_inference_steps=2 , output_type='''np''' , ).images SCREAMING_SNAKE_CASE : Any = low_res_image.size[0] * 4 assert image.shape == (1, expected_height_width, expected_height_width, 3) @slow @require_torch_gpu class lowercase__( unittest.TestCase ): '''simple docstring''' def __lowerCAmelCase ( self :Optional[int] ) -> List[Any]: '''simple docstring''' super().tearDown() gc.collect() torch.cuda.empty_cache() def __lowerCAmelCase ( self :List[Any] ) -> List[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : List[Any] = load_image( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main''' '''/sd2-upscale/low_res_cat.png''' ) SCREAMING_SNAKE_CASE : Union[str, Any] = load_numpy( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd2-upscale''' '''/upsampled_cat.npy''' ) SCREAMING_SNAKE_CASE : Dict = '''stabilityai/stable-diffusion-x4-upscaler''' SCREAMING_SNAKE_CASE : Tuple = StableDiffusionUpscalePipeline.from_pretrained(lowerCamelCase_ ) pipe.to(lowerCamelCase_ ) pipe.set_progress_bar_config(disable=lowerCamelCase_ ) pipe.enable_attention_slicing() SCREAMING_SNAKE_CASE : Dict = '''a cat sitting on a park bench''' SCREAMING_SNAKE_CASE : str = torch.manual_seed(0 ) SCREAMING_SNAKE_CASE : Any = pipe( prompt=lowerCamelCase_ , image=lowerCamelCase_ , generator=lowerCamelCase_ , output_type='''np''' , ) SCREAMING_SNAKE_CASE : Dict = output.images[0] assert image.shape == (5_12, 5_12, 3) assert np.abs(expected_image - image ).max() < 1E-3 def __lowerCAmelCase ( self :Any ) -> int: '''simple docstring''' SCREAMING_SNAKE_CASE : Dict = load_image( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main''' '''/sd2-upscale/low_res_cat.png''' ) SCREAMING_SNAKE_CASE : int = load_numpy( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd2-upscale''' '''/upsampled_cat_fp16.npy''' ) SCREAMING_SNAKE_CASE : List[Any] = '''stabilityai/stable-diffusion-x4-upscaler''' SCREAMING_SNAKE_CASE : Any = StableDiffusionUpscalePipeline.from_pretrained( lowerCamelCase_ , torch_dtype=torch.floataa , ) pipe.to(lowerCamelCase_ ) pipe.set_progress_bar_config(disable=lowerCamelCase_ ) pipe.enable_attention_slicing() SCREAMING_SNAKE_CASE : Tuple = '''a cat sitting on a park bench''' SCREAMING_SNAKE_CASE : Optional[Any] = torch.manual_seed(0 ) SCREAMING_SNAKE_CASE : Union[str, Any] = pipe( prompt=lowerCamelCase_ , image=lowerCamelCase_ , generator=lowerCamelCase_ , output_type='''np''' , ) SCREAMING_SNAKE_CASE : Tuple = output.images[0] assert image.shape == (5_12, 5_12, 3) assert np.abs(expected_image - image ).max() < 5E-1 def __lowerCAmelCase ( self :Tuple ) -> Dict: '''simple docstring''' torch.cuda.empty_cache() torch.cuda.reset_max_memory_allocated() torch.cuda.reset_peak_memory_stats() SCREAMING_SNAKE_CASE : Dict = load_image( '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main''' '''/sd2-upscale/low_res_cat.png''' ) SCREAMING_SNAKE_CASE : List[str] = '''stabilityai/stable-diffusion-x4-upscaler''' SCREAMING_SNAKE_CASE : List[Any] = StableDiffusionUpscalePipeline.from_pretrained( 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() SCREAMING_SNAKE_CASE : Optional[int] = '''a cat sitting on a park bench''' SCREAMING_SNAKE_CASE : str = torch.manual_seed(0 ) SCREAMING_SNAKE_CASE : Optional[Any] = pipe( prompt=lowerCamelCase_ , image=lowerCamelCase_ , generator=lowerCamelCase_ , num_inference_steps=5 , output_type='''np''' , ) SCREAMING_SNAKE_CASE : Dict = torch.cuda.max_memory_allocated() # make sure that less than 2.9 GB is allocated assert mem_bytes < 2.9 * 10**9
18
"""simple docstring""" from sklearn.metrics import fa_score import datasets lowerCamelCase__ : List[Any] = "\nThe F1 score is the harmonic mean of the precision and recall. It can be computed with the equation:\nF1 = 2 * (precision * recall) / (precision + recall)\n" lowerCamelCase__ : str = "\nArgs:\n predictions (`list` of `int`): Predicted labels.\n references (`list` of `int`): Ground truth labels.\n labels (`list` of `int`): The set of labels to include when `average` is not set to `'binary'`, and the order of the labels if `average` is `None`. Labels present in the data can be excluded, for example to calculate a multiclass average ignoring a majority negative class. Labels not present in the data will result in 0 components in a macro average. For multilabel targets, labels are column indices. By default, all labels in `predictions` and `references` are used in sorted order. Defaults to None.\n pos_label (`int`): The class to be considered the positive class, in the case where `average` is set to `binary`. Defaults to 1.\n average (`string`): This parameter is required for multiclass/multilabel targets. If set to `None`, the scores for each class are returned. Otherwise, this determines the type of averaging performed on the data. Defaults to `'binary'`.\n\n - 'binary': Only report results for the class specified by `pos_label`. This is applicable only if the classes found in `predictions` and `references` are binary.\n - 'micro': Calculate metrics globally by counting the total true positives, false negatives and false positives.\n - 'macro': Calculate metrics for each label, and find their unweighted mean. This does not take label imbalance into account.\n - 'weighted': Calculate metrics for each label, and find their average weighted by support (the number of true instances for each label). This alters `'macro'` to account for label imbalance. This option can result in an F-score that is not between precision and recall.\n - 'samples': Calculate metrics for each instance, and find their average (only meaningful for multilabel classification).\n sample_weight (`list` of `float`): Sample weights Defaults to None.\n\nReturns:\n f1 (`float` or `array` of `float`): F1 score or list of f1 scores, depending on the value passed to `average`. Minimum possible value is 0. Maximum possible value is 1. Higher f1 scores are better.\n\nExamples:\n\n Example 1-A simple binary example\n >>> f1_metric = datasets.load_metric(\"f1\")\n >>> results = f1_metric.compute(references=[0, 1, 0, 1, 0], predictions=[0, 0, 1, 1, 0])\n >>> print(results)\n {'f1': 0.5}\n\n Example 2-The same simple binary example as in Example 1, but with `pos_label` set to `0`.\n >>> f1_metric = datasets.load_metric(\"f1\")\n >>> results = f1_metric.compute(references=[0, 1, 0, 1, 0], predictions=[0, 0, 1, 1, 0], pos_label=0)\n >>> print(round(results['f1'], 2))\n 0.67\n\n Example 3-The same simple binary example as in Example 1, but with `sample_weight` included.\n >>> f1_metric = datasets.load_metric(\"f1\")\n >>> results = f1_metric.compute(references=[0, 1, 0, 1, 0], predictions=[0, 0, 1, 1, 0], sample_weight=[0.9, 0.5, 3.9, 1.2, 0.3])\n >>> print(round(results['f1'], 2))\n 0.35\n\n Example 4-A multiclass example, with different values for the `average` input.\n >>> predictions = [0, 2, 1, 0, 0, 1]\n >>> references = [0, 1, 2, 0, 1, 2]\n >>> results = f1_metric.compute(predictions=predictions, references=references, average=\"macro\")\n >>> print(round(results['f1'], 2))\n 0.27\n >>> results = f1_metric.compute(predictions=predictions, references=references, average=\"micro\")\n >>> print(round(results['f1'], 2))\n 0.33\n >>> results = f1_metric.compute(predictions=predictions, references=references, average=\"weighted\")\n >>> print(round(results['f1'], 2))\n 0.27\n >>> results = f1_metric.compute(predictions=predictions, references=references, average=None)\n >>> print(results)\n {'f1': array([0.8, 0. , 0. ])}\n" lowerCamelCase__ : int = "\n@article{scikit-learn,\n title={Scikit-learn: Machine Learning in {P}ython},\n author={Pedregosa, F. and Varoquaux, G. and Gramfort, A. and Michel, V.\n and Thirion, B. and Grisel, O. and Blondel, M. and Prettenhofer, P.\n and Weiss, R. and Dubourg, V. and Vanderplas, J. and Passos, A. and\n Cournapeau, D. and Brucher, M. and Perrot, M. and Duchesnay, E.},\n journal={Journal of Machine Learning Research},\n volume={12},\n pages={2825--2830},\n year={2011}\n}\n" @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class lowercase__( datasets.Metric ): '''simple docstring''' def __lowerCAmelCase ( self :str ) -> Any: '''simple docstring''' return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { '''predictions''': datasets.Sequence(datasets.Value('''int32''' ) ), '''references''': datasets.Sequence(datasets.Value('''int32''' ) ), } if self.config_name == '''multilabel''' else { '''predictions''': datasets.Value('''int32''' ), '''references''': datasets.Value('''int32''' ), } ) , reference_urls=['''https://scikit-learn.org/stable/modules/generated/sklearn.metrics.f1_score.html'''] , ) def __lowerCAmelCase ( self :Any , lowerCamelCase_ :Dict , lowerCamelCase_ :Optional[Any] , lowerCamelCase_ :int=None , lowerCamelCase_ :str=1 , lowerCamelCase_ :Union[str, Any]="binary" , lowerCamelCase_ :Dict=None ) -> int: '''simple docstring''' SCREAMING_SNAKE_CASE : int = fa_score( lowerCamelCase_ , lowerCamelCase_ , labels=lowerCamelCase_ , pos_label=lowerCamelCase_ , average=lowerCamelCase_ , sample_weight=lowerCamelCase_ ) return {"f1": float(lowerCamelCase_ ) if score.size == 1 else score}
18
1
"""simple docstring""" import pandas as pd from matplotlib import pyplot as plt from sklearn.linear_model import LinearRegression # Splitting the dataset into the Training set and Test set from sklearn.model_selection import train_test_split # Fitting Polynomial Regression to the dataset from sklearn.preprocessing import PolynomialFeatures # Importing the dataset lowerCamelCase__ : List[str] = pd.read_csv( "https://s3.us-west-2.amazonaws.com/public.gamelab.fun/dataset/" "position_salaries.csv" ) lowerCamelCase__ : Any = dataset.iloc[:, 1:2].values lowerCamelCase__ : Union[str, Any] = dataset.iloc[:, 2].values lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ : int = train_test_split(X, y, test_size=0.2, random_state=0) lowerCamelCase__ : Union[str, Any] = PolynomialFeatures(degree=4) lowerCamelCase__ : List[str] = poly_reg.fit_transform(X) lowerCamelCase__ : Tuple = LinearRegression() pol_reg.fit(X_poly, y) def __A ( )-> List[Any]: '''simple docstring''' plt.scatter(a_ , a_ , color='''red''' ) plt.plot(a_ , pol_reg.predict(poly_reg.fit_transform(a_ ) ) , color='''blue''' ) plt.title('''Truth or Bluff (Linear Regression)''' ) plt.xlabel('''Position level''' ) plt.ylabel('''Salary''' ) plt.show() if __name__ == "__main__": viz_polymonial() # Predicting a new result with Polymonial Regression pol_reg.predict(poly_reg.fit_transform([[5.5]])) # output should be 132148.43750003
18
"""simple docstring""" from __future__ import annotations from fractions import Fraction def __A ( a_ : int , a_ : int )-> bool: '''simple docstring''' return ( num != den and num % 10 == den // 10 and (num // 10) / (den % 10) == num / den ) def __A ( a_ : int )-> list[str]: '''simple docstring''' SCREAMING_SNAKE_CASE : Union[str, Any] = [] SCREAMING_SNAKE_CASE : List[str] = 11 SCREAMING_SNAKE_CASE : Union[str, Any] = int('''1''' + '''0''' * digit_len ) for num in range(a_ , a_ ): while den <= 99: if (num != den) and (num % 10 == den // 10) and (den % 10 != 0): if is_digit_cancelling(a_ , a_ ): solutions.append(F"{num}/{den}" ) den += 1 num += 1 SCREAMING_SNAKE_CASE : Optional[Any] = 10 return solutions def __A ( a_ : int = 2 )-> int: '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[Any] = 1.0 for fraction in fraction_list(a_ ): SCREAMING_SNAKE_CASE : List[str] = Fraction(a_ ) result *= frac.denominator / frac.numerator return int(a_ ) if __name__ == "__main__": print(solution())
18
1
"""simple docstring""" import json import os import unittest from transformers.models.ctrl.tokenization_ctrl import VOCAB_FILES_NAMES, CTRLTokenizer from ...test_tokenization_common import TokenizerTesterMixin class lowercase__( _UpperCAmelCase , unittest.TestCase ): '''simple docstring''' UpperCamelCase = CTRLTokenizer UpperCamelCase = False UpperCamelCase = False def __lowerCAmelCase ( self :Optional[Any] ) -> str: '''simple docstring''' super().setUp() # Adapted from Sennrich et al. 2015 and https://github.com/rsennrich/subword-nmt SCREAMING_SNAKE_CASE : Tuple = ['''adapt''', '''re@@''', '''a@@''', '''apt''', '''c@@''', '''t''', '''<unk>'''] SCREAMING_SNAKE_CASE : Optional[Any] = dict(zip(lowerCamelCase_ , range(len(lowerCamelCase_ ) ) ) ) SCREAMING_SNAKE_CASE : List[str] = ['''#version: 0.2''', '''a p''', '''ap t</w>''', '''r e''', '''a d''', '''ad apt</w>''', ''''''] SCREAMING_SNAKE_CASE : Tuple = {'''unk_token''': '''<unk>'''} SCREAMING_SNAKE_CASE : Dict = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES['''vocab_file'''] ) SCREAMING_SNAKE_CASE : List[str] = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES['''merges_file'''] ) with open(self.vocab_file , '''w''' , encoding='''utf-8''' ) as fp: fp.write(json.dumps(lowerCamelCase_ ) + '''\n''' ) with open(self.merges_file , '''w''' , encoding='''utf-8''' ) as fp: fp.write('''\n'''.join(lowerCamelCase_ ) ) def __lowerCAmelCase ( self :Optional[int] , **lowerCamelCase_ :Any ) -> int: '''simple docstring''' kwargs.update(self.special_tokens_map ) return CTRLTokenizer.from_pretrained(self.tmpdirname , **lowerCamelCase_ ) def __lowerCAmelCase ( self :List[str] , lowerCamelCase_ :Optional[Any] ) -> Optional[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : Tuple = '''adapt react readapt apt''' SCREAMING_SNAKE_CASE : Any = '''adapt react readapt apt''' return input_text, output_text def __lowerCAmelCase ( self :List[Any] ) -> Optional[int]: '''simple docstring''' SCREAMING_SNAKE_CASE : List[str] = CTRLTokenizer(self.vocab_file , self.merges_file , **self.special_tokens_map ) SCREAMING_SNAKE_CASE : int = '''adapt react readapt apt''' SCREAMING_SNAKE_CASE : Any = '''adapt re@@ a@@ c@@ t re@@ adapt apt'''.split() SCREAMING_SNAKE_CASE : List[str] = tokenizer.tokenize(lowerCamelCase_ ) self.assertListEqual(lowerCamelCase_ , lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Dict = tokens + [tokenizer.unk_token] SCREAMING_SNAKE_CASE : Dict = [0, 1, 2, 4, 5, 1, 0, 3, 6] self.assertListEqual(tokenizer.convert_tokens_to_ids(lowerCamelCase_ ) , lowerCamelCase_ )
18
"""simple docstring""" from ...configuration_utils import PretrainedConfig from ...utils import logging from ...utils.backbone_utils import BackboneConfigMixin, get_aligned_output_features_output_indices lowerCamelCase__ : int = logging.get_logger(__name__) class lowercase__( _UpperCAmelCase , _UpperCAmelCase ): '''simple docstring''' UpperCamelCase = """maskformer-swin""" UpperCamelCase = { """num_attention_heads""": """num_heads""", """num_hidden_layers""": """num_layers""", } def __init__( self :Optional[int] , lowerCamelCase_ :List[Any]=2_24 , lowerCamelCase_ :Tuple=4 , lowerCamelCase_ :Optional[Any]=3 , lowerCamelCase_ :List[str]=96 , lowerCamelCase_ :int=[2, 2, 6, 2] , lowerCamelCase_ :Union[str, Any]=[3, 6, 12, 24] , lowerCamelCase_ :Optional[int]=7 , lowerCamelCase_ :Tuple=4.0 , lowerCamelCase_ :Tuple=True , lowerCamelCase_ :Dict=0.0 , lowerCamelCase_ :Any=0.0 , lowerCamelCase_ :List[Any]=0.1 , lowerCamelCase_ :Dict="gelu" , lowerCamelCase_ :Optional[int]=False , lowerCamelCase_ :List[str]=0.0_2 , lowerCamelCase_ :Any=1E-5 , lowerCamelCase_ :Union[str, Any]=None , lowerCamelCase_ :List[str]=None , **lowerCamelCase_ :Union[str, Any] , ) -> Dict: '''simple docstring''' super().__init__(**lowerCamelCase_ ) SCREAMING_SNAKE_CASE : int = image_size SCREAMING_SNAKE_CASE : Optional[Any] = patch_size SCREAMING_SNAKE_CASE : str = num_channels SCREAMING_SNAKE_CASE : Union[str, Any] = embed_dim SCREAMING_SNAKE_CASE : List[Any] = depths SCREAMING_SNAKE_CASE : List[str] = len(lowerCamelCase_ ) SCREAMING_SNAKE_CASE : str = num_heads SCREAMING_SNAKE_CASE : Any = window_size SCREAMING_SNAKE_CASE : List[str] = mlp_ratio SCREAMING_SNAKE_CASE : str = qkv_bias SCREAMING_SNAKE_CASE : Tuple = hidden_dropout_prob SCREAMING_SNAKE_CASE : int = attention_probs_dropout_prob SCREAMING_SNAKE_CASE : int = drop_path_rate SCREAMING_SNAKE_CASE : Tuple = hidden_act SCREAMING_SNAKE_CASE : Any = use_absolute_embeddings SCREAMING_SNAKE_CASE : List[str] = layer_norm_eps SCREAMING_SNAKE_CASE : List[str] = initializer_range # we set the hidden_size attribute in order to make Swin work with VisionEncoderDecoderModel # this indicates the channel dimension after the last stage of the model SCREAMING_SNAKE_CASE : int = int(embed_dim * 2 ** (len(lowerCamelCase_ ) - 1) ) SCREAMING_SNAKE_CASE : Dict = ['''stem'''] + [f"stage{idx}" for idx in range(1 , len(lowerCamelCase_ ) + 1 )] SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : int = get_aligned_output_features_output_indices( out_features=lowerCamelCase_ , out_indices=lowerCamelCase_ , stage_names=self.stage_names )
18
1
"""simple docstring""" from collections.abc import Generator from math import sin def __A ( a_ : bytes )-> bytes: '''simple docstring''' if len(a_ ) != 32: raise ValueError('''Input must be of length 32''' ) SCREAMING_SNAKE_CASE : str = B'''''' for i in [3, 2, 1, 0]: little_endian += string_aa[8 * i : 8 * i + 8] return little_endian def __A ( a_ : int )-> bytes: '''simple docstring''' if i < 0: raise ValueError('''Input must be non-negative''' ) SCREAMING_SNAKE_CASE : Tuple = format(a_ , '''08x''' )[-8:] SCREAMING_SNAKE_CASE : int = B'''''' for i in [3, 2, 1, 0]: little_endian_hex += hex_rep[2 * i : 2 * i + 2].encode('''utf-8''' ) return little_endian_hex def __A ( a_ : bytes )-> bytes: '''simple docstring''' SCREAMING_SNAKE_CASE : Dict = B'''''' for char in message: bit_string += format(a_ , '''08b''' ).encode('''utf-8''' ) SCREAMING_SNAKE_CASE : int = format(len(a_ ) , '''064b''' ).encode('''utf-8''' ) # Pad bit_string to a multiple of 512 chars bit_string += b"1" while len(a_ ) % 5_12 != 4_48: bit_string += b"0" bit_string += to_little_endian(start_len[32:] ) + to_little_endian(start_len[:32] ) return bit_string def __A ( a_ : bytes )-> Generator[list[int], None, None]: '''simple docstring''' if len(a_ ) % 5_12 != 0: raise ValueError('''Input must have length that\'s a multiple of 512''' ) for pos in range(0 , len(a_ ) , 5_12 ): SCREAMING_SNAKE_CASE : List[str] = bit_string[pos : pos + 5_12] SCREAMING_SNAKE_CASE : Optional[int] = [] for i in range(0 , 5_12 , 32 ): block_words.append(int(to_little_endian(block[i : i + 32] ) , 2 ) ) yield block_words def __A ( a_ : int )-> int: '''simple docstring''' if i < 0: raise ValueError('''Input must be non-negative''' ) SCREAMING_SNAKE_CASE : Optional[int] = format(a_ , '''032b''' ) SCREAMING_SNAKE_CASE : Optional[int] = '''''' for c in i_str: new_str += "1" if c == "0" else "0" return int(a_ , 2 ) def __A ( a_ : int , a_ : int )-> int: '''simple docstring''' return (a + b) % 2**32 def __A ( a_ : int , a_ : int )-> int: '''simple docstring''' if i < 0: raise ValueError('''Input must be non-negative''' ) if shift < 0: raise ValueError('''Shift must be non-negative''' ) return ((i << shift) ^ (i >> (32 - shift))) % 2**32 def __A ( a_ : bytes )-> bytes: '''simple docstring''' SCREAMING_SNAKE_CASE : Dict = preprocess(a_ ) SCREAMING_SNAKE_CASE : List[Any] = [int(2**32 * abs(sin(i + 1 ) ) ) for i in range(64 )] # Starting states SCREAMING_SNAKE_CASE : Dict = 0x67_45_23_01 SCREAMING_SNAKE_CASE : Tuple = 0xEF_CD_AB_89 SCREAMING_SNAKE_CASE : str = 0x98_BA_DC_FE SCREAMING_SNAKE_CASE : List[Any] = 0x10_32_54_76 SCREAMING_SNAKE_CASE : Any = [ 7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22, 5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20, 4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23, 6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21, ] # Process bit string in chunks, each with 16 32-char words for block_words in get_block_words(a_ ): SCREAMING_SNAKE_CASE : Dict = aa SCREAMING_SNAKE_CASE : Optional[Any] = ba SCREAMING_SNAKE_CASE : Tuple = ca SCREAMING_SNAKE_CASE : Any = da # Hash current chunk for i in range(64 ): if i <= 15: # f = (b & c) | (not_32(b) & d) # Alternate definition for f SCREAMING_SNAKE_CASE : str = d ^ (b & (c ^ d)) SCREAMING_SNAKE_CASE : Optional[Any] = i elif i <= 31: # f = (d & b) | (not_32(d) & c) # Alternate definition for f SCREAMING_SNAKE_CASE : List[Any] = c ^ (d & (b ^ c)) SCREAMING_SNAKE_CASE : Optional[int] = (5 * i + 1) % 16 elif i <= 47: SCREAMING_SNAKE_CASE : List[Any] = b ^ c ^ d SCREAMING_SNAKE_CASE : Union[str, Any] = (3 * i + 5) % 16 else: SCREAMING_SNAKE_CASE : Union[str, Any] = c ^ (b | not_aa(a_ )) SCREAMING_SNAKE_CASE : Optional[Any] = (7 * i) % 16 SCREAMING_SNAKE_CASE : Dict = (f + a + added_consts[i] + block_words[g]) % 2**32 SCREAMING_SNAKE_CASE : int = d SCREAMING_SNAKE_CASE : Union[str, Any] = c SCREAMING_SNAKE_CASE : Any = b SCREAMING_SNAKE_CASE : List[Any] = sum_aa(a_ , left_rotate_aa(a_ , shift_amounts[i] ) ) # Add hashed chunk to running total SCREAMING_SNAKE_CASE : str = sum_aa(a_ , a_ ) SCREAMING_SNAKE_CASE : str = sum_aa(a_ , a_ ) SCREAMING_SNAKE_CASE : Dict = sum_aa(a_ , a_ ) SCREAMING_SNAKE_CASE : Dict = sum_aa(a_ , a_ ) SCREAMING_SNAKE_CASE : int = reformat_hex(a_ ) + reformat_hex(a_ ) + reformat_hex(a_ ) + reformat_hex(a_ ) return digest if __name__ == "__main__": import doctest doctest.testmod()
18
"""simple docstring""" import math class lowercase__: '''simple docstring''' def __init__( self :Union[str, Any] , lowerCamelCase_ :List[str]=0 ) -> List[Any]: # a graph with Node 0,1,...,N-1 '''simple docstring''' SCREAMING_SNAKE_CASE : Tuple = n SCREAMING_SNAKE_CASE : List[Any] = [ [math.inf for j in range(0 , lowerCamelCase_ )] for i in range(0 , lowerCamelCase_ ) ] # adjacency matrix for weight SCREAMING_SNAKE_CASE : Any = [ [math.inf for j in range(0 , lowerCamelCase_ )] for i in range(0 , lowerCamelCase_ ) ] # dp[i][j] stores minimum distance from i to j def __lowerCAmelCase ( self :Dict , lowerCamelCase_ :Optional[int] , lowerCamelCase_ :Union[str, Any] , lowerCamelCase_ :Union[str, Any] ) -> Tuple: '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[Any] = w def __lowerCAmelCase ( self :str ) -> Union[str, Any]: '''simple docstring''' for k in range(0 , self.n ): for i in range(0 , self.n ): for j in range(0 , self.n ): SCREAMING_SNAKE_CASE : List[str] = min(self.dp[i][j] , self.dp[i][k] + self.dp[k][j] ) def __lowerCAmelCase ( self :int , lowerCamelCase_ :Union[str, Any] , lowerCamelCase_ :Optional[int] ) -> Optional[Any]: '''simple docstring''' return self.dp[u][v] if __name__ == "__main__": lowerCamelCase__ : Dict = Graph(5) graph.add_edge(0, 2, 9) graph.add_edge(0, 4, 10) graph.add_edge(1, 3, 5) graph.add_edge(2, 3, 7) graph.add_edge(3, 0, 10) graph.add_edge(3, 1, 2) graph.add_edge(3, 2, 1) graph.add_edge(3, 4, 6) graph.add_edge(4, 1, 3) graph.add_edge(4, 2, 4) graph.add_edge(4, 3, 9) graph.floyd_warshall() graph.show_min(1, 4) graph.show_min(0, 3)
18
1
"""simple docstring""" import math import os import unittest from transformers import MegatronBertConfig, is_torch_available from transformers.models.auto import get_values from transformers.testing_utils import require_sentencepiece, require_tokenizers, require_torch, slow, torch_device from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, ids_tensor, random_attention_mask from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from transformers import ( MODEL_FOR_PRETRAINING_MAPPING, MegatronBertForCausalLM, MegatronBertForMaskedLM, MegatronBertForMultipleChoice, MegatronBertForNextSentencePrediction, MegatronBertForPreTraining, MegatronBertForQuestionAnswering, MegatronBertForSequenceClassification, MegatronBertForTokenClassification, MegatronBertModel, ) class lowercase__: '''simple docstring''' def __init__( self :Dict , lowerCamelCase_ :Optional[int] , lowerCamelCase_ :List[Any]=13 , lowerCamelCase_ :int=7 , lowerCamelCase_ :Tuple=True , lowerCamelCase_ :Union[str, Any]=True , lowerCamelCase_ :int=True , lowerCamelCase_ :Union[str, Any]=True , lowerCamelCase_ :str=99 , lowerCamelCase_ :Optional[Any]=64 , lowerCamelCase_ :Optional[Any]=32 , lowerCamelCase_ :Union[str, Any]=5 , lowerCamelCase_ :Optional[int]=4 , lowerCamelCase_ :List[Any]=37 , lowerCamelCase_ :str="gelu" , lowerCamelCase_ :Any=0.1 , lowerCamelCase_ :Dict=0.1 , lowerCamelCase_ :Tuple=5_12 , lowerCamelCase_ :Optional[Any]=16 , lowerCamelCase_ :Optional[Any]=2 , lowerCamelCase_ :str=0.0_2 , lowerCamelCase_ :List[str]=3 , lowerCamelCase_ :Optional[Any]=4 , lowerCamelCase_ :Tuple=None , ) -> Optional[int]: '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[int] = parent SCREAMING_SNAKE_CASE : int = batch_size SCREAMING_SNAKE_CASE : Any = seq_length SCREAMING_SNAKE_CASE : str = is_training SCREAMING_SNAKE_CASE : Any = use_input_mask SCREAMING_SNAKE_CASE : List[Any] = use_token_type_ids SCREAMING_SNAKE_CASE : int = use_labels SCREAMING_SNAKE_CASE : Optional[int] = vocab_size SCREAMING_SNAKE_CASE : List[Any] = hidden_size SCREAMING_SNAKE_CASE : List[str] = embedding_size SCREAMING_SNAKE_CASE : List[str] = num_hidden_layers SCREAMING_SNAKE_CASE : str = num_attention_heads SCREAMING_SNAKE_CASE : Any = intermediate_size SCREAMING_SNAKE_CASE : List[str] = hidden_act SCREAMING_SNAKE_CASE : Any = hidden_dropout_prob SCREAMING_SNAKE_CASE : Any = attention_probs_dropout_prob SCREAMING_SNAKE_CASE : Union[str, Any] = max_position_embeddings SCREAMING_SNAKE_CASE : Dict = type_vocab_size SCREAMING_SNAKE_CASE : List[Any] = type_sequence_label_size SCREAMING_SNAKE_CASE : Dict = initializer_range SCREAMING_SNAKE_CASE : Union[str, Any] = num_labels SCREAMING_SNAKE_CASE : Dict = num_choices SCREAMING_SNAKE_CASE : Optional[Any] = scope def __lowerCAmelCase ( self :Tuple ) -> List[str]: '''simple docstring''' SCREAMING_SNAKE_CASE : Tuple = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) SCREAMING_SNAKE_CASE : int = None if self.use_input_mask: SCREAMING_SNAKE_CASE : Any = random_attention_mask([self.batch_size, self.seq_length] ) SCREAMING_SNAKE_CASE : int = None if self.use_token_type_ids: SCREAMING_SNAKE_CASE : Optional[int] = ids_tensor([self.batch_size, self.seq_length] , self.type_vocab_size ) SCREAMING_SNAKE_CASE : Tuple = None SCREAMING_SNAKE_CASE : Tuple = None SCREAMING_SNAKE_CASE : Optional[int] = None if self.use_labels: SCREAMING_SNAKE_CASE : List[str] = ids_tensor([self.batch_size] , self.type_sequence_label_size ) SCREAMING_SNAKE_CASE : Dict = ids_tensor([self.batch_size, self.seq_length] , self.num_labels ) SCREAMING_SNAKE_CASE : Tuple = ids_tensor([self.batch_size] , self.num_choices ) SCREAMING_SNAKE_CASE : List[Any] = self.get_config() return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels def __lowerCAmelCase ( self :str ) -> Union[str, Any]: '''simple docstring''' return MegatronBertConfig( 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 , embedding_size=self.embedding_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=lowerCamelCase_ , initializer_range=self.initializer_range , ) def __lowerCAmelCase ( self :Tuple , lowerCamelCase_ :str , lowerCamelCase_ :int , lowerCamelCase_ :Any , lowerCamelCase_ :Any , lowerCamelCase_ :Any , lowerCamelCase_ :Tuple , lowerCamelCase_ :int ) -> Tuple: '''simple docstring''' SCREAMING_SNAKE_CASE : List[Any] = MegatronBertModel(config=lowerCamelCase_ ) model.to(lowerCamelCase_ ) model.eval() SCREAMING_SNAKE_CASE : str = model(lowerCamelCase_ , attention_mask=lowerCamelCase_ , token_type_ids=lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Optional[int] = model(lowerCamelCase_ , token_type_ids=lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Any = model(lowerCamelCase_ ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) self.parent.assertEqual(result.pooler_output.shape , (self.batch_size, self.hidden_size) ) def __lowerCAmelCase ( self :str , lowerCamelCase_ :str , lowerCamelCase_ :Optional[Any] , lowerCamelCase_ :List[str] , lowerCamelCase_ :Dict , lowerCamelCase_ :List[Any] , lowerCamelCase_ :int , lowerCamelCase_ :Union[str, Any] ) -> List[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : int = MegatronBertForMaskedLM(config=lowerCamelCase_ ) model.to(lowerCamelCase_ ) model.eval() SCREAMING_SNAKE_CASE : List[Any] = model(lowerCamelCase_ , attention_mask=lowerCamelCase_ , token_type_ids=lowerCamelCase_ , labels=lowerCamelCase_ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.vocab_size) ) def __lowerCAmelCase ( self :Optional[int] , lowerCamelCase_ :str , lowerCamelCase_ :Dict , lowerCamelCase_ :str , lowerCamelCase_ :str , lowerCamelCase_ :Optional[int] , lowerCamelCase_ :Any , lowerCamelCase_ :Any ) -> Any: '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[Any] = MegatronBertForCausalLM(config=lowerCamelCase_ ) model.to(lowerCamelCase_ ) model.eval() SCREAMING_SNAKE_CASE : List[Any] = model(lowerCamelCase_ , attention_mask=lowerCamelCase_ , token_type_ids=lowerCamelCase_ , labels=lowerCamelCase_ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.vocab_size) ) def __lowerCAmelCase ( self :List[str] , lowerCamelCase_ :Any , lowerCamelCase_ :Optional[int] , lowerCamelCase_ :List[str] , lowerCamelCase_ :Tuple , lowerCamelCase_ :int , lowerCamelCase_ :Union[str, Any] , lowerCamelCase_ :List[str] ) -> List[str]: '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[int] = MegatronBertForNextSentencePrediction(config=lowerCamelCase_ ) model.to(lowerCamelCase_ ) model.eval() SCREAMING_SNAKE_CASE : Dict = model( lowerCamelCase_ , attention_mask=lowerCamelCase_ , token_type_ids=lowerCamelCase_ , labels=lowerCamelCase_ , ) self.parent.assertEqual(result.logits.shape , (self.batch_size, 2) ) def __lowerCAmelCase ( self :Union[str, Any] , lowerCamelCase_ :Union[str, Any] , lowerCamelCase_ :Union[str, Any] , lowerCamelCase_ :Optional[Any] , lowerCamelCase_ :Optional[int] , lowerCamelCase_ :Tuple , lowerCamelCase_ :str , lowerCamelCase_ :List[Any] ) -> Optional[int]: '''simple docstring''' SCREAMING_SNAKE_CASE : Dict = MegatronBertForPreTraining(config=lowerCamelCase_ ) model.to(lowerCamelCase_ ) model.eval() SCREAMING_SNAKE_CASE : Union[str, Any] = model( lowerCamelCase_ , attention_mask=lowerCamelCase_ , token_type_ids=lowerCamelCase_ , labels=lowerCamelCase_ , next_sentence_label=lowerCamelCase_ , ) self.parent.assertEqual(result.prediction_logits.shape , (self.batch_size, self.seq_length, self.vocab_size) ) self.parent.assertEqual(result.seq_relationship_logits.shape , (self.batch_size, 2) ) def __lowerCAmelCase ( self :Union[str, Any] , lowerCamelCase_ :str , lowerCamelCase_ :List[str] , lowerCamelCase_ :Optional[int] , lowerCamelCase_ :List[Any] , lowerCamelCase_ :Optional[int] , lowerCamelCase_ :List[str] , lowerCamelCase_ :str ) -> List[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : Tuple = MegatronBertForQuestionAnswering(config=lowerCamelCase_ ) model.to(lowerCamelCase_ ) model.eval() SCREAMING_SNAKE_CASE : int = model( lowerCamelCase_ , attention_mask=lowerCamelCase_ , token_type_ids=lowerCamelCase_ , start_positions=lowerCamelCase_ , end_positions=lowerCamelCase_ , ) self.parent.assertEqual(result.start_logits.shape , (self.batch_size, self.seq_length) ) self.parent.assertEqual(result.end_logits.shape , (self.batch_size, self.seq_length) ) def __lowerCAmelCase ( self :List[Any] , lowerCamelCase_ :List[str] , lowerCamelCase_ :Dict , lowerCamelCase_ :int , lowerCamelCase_ :int , lowerCamelCase_ :str , lowerCamelCase_ :List[Any] , lowerCamelCase_ :Dict ) -> Optional[int]: '''simple docstring''' SCREAMING_SNAKE_CASE : Any = self.num_labels SCREAMING_SNAKE_CASE : Any = MegatronBertForSequenceClassification(lowerCamelCase_ ) model.to(lowerCamelCase_ ) model.eval() SCREAMING_SNAKE_CASE : int = model(lowerCamelCase_ , attention_mask=lowerCamelCase_ , token_type_ids=lowerCamelCase_ , labels=lowerCamelCase_ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_labels) ) def __lowerCAmelCase ( self :int , lowerCamelCase_ :Union[str, Any] , lowerCamelCase_ :Optional[int] , lowerCamelCase_ :Optional[int] , lowerCamelCase_ :Dict , lowerCamelCase_ :Optional[int] , lowerCamelCase_ :Optional[int] , lowerCamelCase_ :Union[str, Any] ) -> List[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : str = self.num_labels SCREAMING_SNAKE_CASE : Tuple = MegatronBertForTokenClassification(config=lowerCamelCase_ ) model.to(lowerCamelCase_ ) model.eval() SCREAMING_SNAKE_CASE : Dict = model(lowerCamelCase_ , attention_mask=lowerCamelCase_ , token_type_ids=lowerCamelCase_ , labels=lowerCamelCase_ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.num_labels) ) def __lowerCAmelCase ( self :Any , lowerCamelCase_ :List[Any] , lowerCamelCase_ :Optional[Any] , lowerCamelCase_ :List[Any] , lowerCamelCase_ :List[str] , lowerCamelCase_ :Tuple , lowerCamelCase_ :Dict , lowerCamelCase_ :Optional[int] ) -> List[str]: '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[int] = self.num_choices SCREAMING_SNAKE_CASE : str = MegatronBertForMultipleChoice(config=lowerCamelCase_ ) model.to(lowerCamelCase_ ) model.eval() SCREAMING_SNAKE_CASE : Union[str, Any] = input_ids.unsqueeze(1 ).expand(-1 , self.num_choices , -1 ).contiguous() SCREAMING_SNAKE_CASE : List[Any] = token_type_ids.unsqueeze(1 ).expand(-1 , self.num_choices , -1 ).contiguous() SCREAMING_SNAKE_CASE : List[Any] = input_mask.unsqueeze(1 ).expand(-1 , self.num_choices , -1 ).contiguous() SCREAMING_SNAKE_CASE : List[str] = model( lowerCamelCase_ , attention_mask=lowerCamelCase_ , token_type_ids=lowerCamelCase_ , labels=lowerCamelCase_ , ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_choices) ) def __lowerCAmelCase ( self :str ) -> Any: '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[int] = self.prepare_config_and_inputs() ( ( SCREAMING_SNAKE_CASE ), ( SCREAMING_SNAKE_CASE ), ( SCREAMING_SNAKE_CASE ), ( SCREAMING_SNAKE_CASE ), ( SCREAMING_SNAKE_CASE ), ( SCREAMING_SNAKE_CASE ), ( SCREAMING_SNAKE_CASE ), ) : Optional[int] = config_and_inputs SCREAMING_SNAKE_CASE : List[Any] = {'''input_ids''': input_ids, '''token_type_ids''': token_type_ids, '''attention_mask''': input_mask} return config, inputs_dict @require_torch class lowercase__( _UpperCAmelCase , _UpperCAmelCase , unittest.TestCase ): '''simple docstring''' UpperCamelCase = ( ( MegatronBertModel, MegatronBertForMaskedLM, MegatronBertForCausalLM, MegatronBertForMultipleChoice, MegatronBertForNextSentencePrediction, MegatronBertForPreTraining, MegatronBertForQuestionAnswering, MegatronBertForSequenceClassification, MegatronBertForTokenClassification, ) if is_torch_available() else () ) UpperCamelCase = ( { """feature-extraction""": MegatronBertModel, """fill-mask""": MegatronBertForMaskedLM, """question-answering""": MegatronBertForQuestionAnswering, """text-classification""": MegatronBertForSequenceClassification, """text-generation""": MegatronBertForCausalLM, """token-classification""": MegatronBertForTokenClassification, """zero-shot""": MegatronBertForSequenceClassification, } if is_torch_available() else {} ) UpperCamelCase = True # test_resize_embeddings = False UpperCamelCase = False def __lowerCAmelCase ( self :Any , lowerCamelCase_ :List[Any] , lowerCamelCase_ :Tuple , lowerCamelCase_ :Optional[int]=False ) -> Optional[int]: '''simple docstring''' SCREAMING_SNAKE_CASE : Tuple = super()._prepare_for_class(lowerCamelCase_ , lowerCamelCase_ , return_labels=lowerCamelCase_ ) if return_labels: if model_class in get_values(lowerCamelCase_ ): SCREAMING_SNAKE_CASE : str = torch.zeros( (self.model_tester.batch_size, self.model_tester.seq_length) , dtype=torch.long , device=lowerCamelCase_ ) SCREAMING_SNAKE_CASE : int = torch.zeros( self.model_tester.batch_size , dtype=torch.long , device=lowerCamelCase_ ) return inputs_dict def __lowerCAmelCase ( self :Dict ) -> List[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[Any] = MegatronBertModelTester(self ) SCREAMING_SNAKE_CASE : Tuple = ConfigTester(self , config_class=lowerCamelCase_ , hidden_size=37 ) def __lowerCAmelCase ( self :Tuple ) -> Any: '''simple docstring''' self.config_tester.run_common_tests() def __lowerCAmelCase ( self :Any ) -> Any: '''simple docstring''' SCREAMING_SNAKE_CASE : Tuple = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_megatron_bert_model(*lowerCamelCase_ ) def __lowerCAmelCase ( self :Optional[int] ) -> str: '''simple docstring''' SCREAMING_SNAKE_CASE : List[Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_megatron_bert_for_masked_lm(*lowerCamelCase_ ) def __lowerCAmelCase ( self :Tuple ) -> List[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : List[Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_megatron_bert_for_multiple_choice(*lowerCamelCase_ ) def __lowerCAmelCase ( self :List[Any] ) -> Dict: '''simple docstring''' SCREAMING_SNAKE_CASE : int = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_megatron_bert_for_next_sequence_prediction(*lowerCamelCase_ ) def __lowerCAmelCase ( self :Optional[Any] ) -> Optional[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : int = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_megatron_bert_for_pretraining(*lowerCamelCase_ ) def __lowerCAmelCase ( self :Union[str, Any] ) -> Dict: '''simple docstring''' SCREAMING_SNAKE_CASE : Any = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_megatron_bert_for_question_answering(*lowerCamelCase_ ) def __lowerCAmelCase ( self :Optional[int] ) -> Any: '''simple docstring''' SCREAMING_SNAKE_CASE : Any = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_megatron_bert_for_sequence_classification(*lowerCamelCase_ ) def __lowerCAmelCase ( self :Tuple ) -> Optional[int]: '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[int] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_megatron_bert_for_token_classification(*lowerCamelCase_ ) def __A ( a_ : int )-> Dict: '''simple docstring''' return torch.tensor( a_ , dtype=torch.long , device=a_ , ) lowerCamelCase__ : str = 1e-4 @require_torch @require_sentencepiece @require_tokenizers class lowercase__( unittest.TestCase ): '''simple docstring''' @slow @unittest.skip('''Model is not available.''' ) def __lowerCAmelCase ( self :Any ) -> Optional[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : Tuple = '''nvidia/megatron-bert-uncased-345m''' if "MYDIR" in os.environ: SCREAMING_SNAKE_CASE : Tuple = os.path.join(os.environ['''MYDIR'''] , lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Dict = MegatronBertModel.from_pretrained(lowerCamelCase_ ) model.to(lowerCamelCase_ ) model.half() SCREAMING_SNAKE_CASE : List[str] = _long_tensor([[1_01, 71_10, 10_05, 10_56, 20_23, 1_13_33, 1_74_13, 10_29, 1_02]] ) with torch.no_grad(): SCREAMING_SNAKE_CASE : List[str] = model(lowerCamelCase_ )[0] SCREAMING_SNAKE_CASE : Any = torch.Size((1, 9, 10_24) ) self.assertEqual(output.shape , lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Union[str, Any] = [-0.6_0_4_0, -0.2_5_1_7, -0.1_0_2_5, 0.3_4_2_0, -0.6_7_5_8, -0.0_0_1_7, -0.1_0_8_9, -0.1_9_9_0, 0.5_7_2_8] for ii in range(3 ): for jj in range(3 ): SCREAMING_SNAKE_CASE : Optional[Any] = output[0, ii, jj] SCREAMING_SNAKE_CASE : Optional[Any] = expected[3 * ii + jj] SCREAMING_SNAKE_CASE : int = '''ii={} jj={} a={} b={}'''.format(lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ ) self.assertTrue(math.isclose(lowerCamelCase_ , lowerCamelCase_ , rel_tol=lowerCamelCase_ , abs_tol=lowerCamelCase_ ) , msg=lowerCamelCase_ )
18
"""simple docstring""" from typing import TYPE_CHECKING from ....utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available lowerCamelCase__ : Tuple = { "configuration_mctct": ["MCTCT_PRETRAINED_CONFIG_ARCHIVE_MAP", "MCTCTConfig"], "feature_extraction_mctct": ["MCTCTFeatureExtractor"], "processing_mctct": ["MCTCTProcessor"], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCamelCase__ : Union[str, Any] = [ "MCTCT_PRETRAINED_MODEL_ARCHIVE_LIST", "MCTCTForCTC", "MCTCTModel", "MCTCTPreTrainedModel", ] if TYPE_CHECKING: from .configuration_mctct import MCTCT_PRETRAINED_CONFIG_ARCHIVE_MAP, MCTCTConfig from .feature_extraction_mctct import MCTCTFeatureExtractor from .processing_mctct import MCTCTProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_mctct import MCTCT_PRETRAINED_MODEL_ARCHIVE_LIST, MCTCTForCTC, MCTCTModel, MCTCTPreTrainedModel else: import sys lowerCamelCase__ : Tuple = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
18
1
"""simple docstring""" import json import os import subprocess import unittest from ast import literal_eval import pytest from parameterized import parameterized_class from . import is_sagemaker_available if is_sagemaker_available(): from sagemaker import Session, TrainingJobAnalytics from sagemaker.huggingface import HuggingFace @pytest.mark.skipif( literal_eval(os.getenv("""TEST_SAGEMAKER""" , """False""" ) ) is not True , reason="""Skipping test because should only be run when releasing minor transformers version""" , ) @pytest.mark.usefixtures("""sm_env""" ) @parameterized_class( [ { """framework""": """pytorch""", """script""": """run_glue.py""", """model_name_or_path""": """distilbert-base-cased""", """instance_type""": """ml.g4dn.xlarge""", """results""": {"""train_runtime""": 6_50, """eval_accuracy""": 0.6, """eval_loss""": 0.9}, }, { """framework""": """tensorflow""", """script""": """run_tf.py""", """model_name_or_path""": """distilbert-base-cased""", """instance_type""": """ml.g4dn.xlarge""", """results""": {"""train_runtime""": 6_00, """eval_accuracy""": 0.3, """eval_loss""": 0.9}, }, ] ) class lowercase__( unittest.TestCase ): '''simple docstring''' def __lowerCAmelCase ( self :List[str] ) -> int: '''simple docstring''' if self.framework == "pytorch": subprocess.run( f"cp ./examples/pytorch/text-classification/run_glue.py {self.env.test_path}/run_glue.py".split() , encoding='''utf-8''' , check=lowerCamelCase_ , ) assert hasattr(self , '''env''' ) def __lowerCAmelCase ( self :Optional[Any] , lowerCamelCase_ :Any=1 ) -> Optional[int]: '''simple docstring''' return HuggingFace( entry_point=self.script , source_dir=self.env.test_path , role=self.env.role , image_uri=self.env.image_uri , base_job_name=f"{self.env.base_job_name}-single" , instance_count=lowerCamelCase_ , instance_type=self.instance_type , debugger_hook_config=lowerCamelCase_ , hyperparameters={**self.env.hyperparameters, '''model_name_or_path''': self.model_name_or_path} , metric_definitions=self.env.metric_definitions , py_version='''py36''' , ) def __lowerCAmelCase ( self :str , lowerCamelCase_ :int ) -> Optional[Any]: '''simple docstring''' TrainingJobAnalytics(lowerCamelCase_ ).export_csv(f"{self.env.test_path}/{job_name}_metrics.csv" ) def __lowerCAmelCase ( self :str ) -> Dict: '''simple docstring''' SCREAMING_SNAKE_CASE : Tuple = self.create_estimator() # run training estimator.fit() # result dataframe SCREAMING_SNAKE_CASE : Optional[int] = TrainingJobAnalytics(estimator.latest_training_job.name ).dataframe() # extract kpis SCREAMING_SNAKE_CASE : int = list(result_metrics_df[result_metrics_df.metric_name == '''eval_accuracy''']['''value'''] ) SCREAMING_SNAKE_CASE : Dict = list(result_metrics_df[result_metrics_df.metric_name == '''eval_loss''']['''value'''] ) # get train time from SageMaker job, this includes starting, preprocessing, stopping SCREAMING_SNAKE_CASE : Any = ( Session().describe_training_job(estimator.latest_training_job.name ).get('''TrainingTimeInSeconds''' , 99_99_99 ) ) # assert kpis assert train_runtime <= self.results["train_runtime"] assert all(t >= self.results['''eval_accuracy'''] for t in eval_accuracy ) assert all(t <= self.results['''eval_loss'''] for t in eval_loss ) # dump tests result into json file to share in PR with open(f"{estimator.latest_training_job.name}.json" , '''w''' ) as outfile: json.dump({'''train_time''': train_runtime, '''eval_accuracy''': eval_accuracy, '''eval_loss''': eval_loss} , lowerCamelCase_ )
18
"""simple docstring""" import os from typing import Any, Callable, Dict, List, Optional, Tuple, Union import torch from torch import nn from ...models.controlnet import ControlNetModel, ControlNetOutput from ...models.modeling_utils import ModelMixin from ...utils import logging lowerCamelCase__ : List[Any] = logging.get_logger(__name__) class lowercase__( _UpperCAmelCase ): '''simple docstring''' def __init__( self :List[str] , lowerCamelCase_ :Union[List[ControlNetModel], Tuple[ControlNetModel]] ) -> Union[str, Any]: '''simple docstring''' super().__init__() SCREAMING_SNAKE_CASE : Optional[Any] = nn.ModuleList(lowerCamelCase_ ) def __lowerCAmelCase ( self :Any , lowerCamelCase_ :torch.FloatTensor , lowerCamelCase_ :Union[torch.Tensor, float, int] , lowerCamelCase_ :torch.Tensor , lowerCamelCase_ :List[torch.tensor] , lowerCamelCase_ :List[float] , lowerCamelCase_ :Optional[torch.Tensor] = None , lowerCamelCase_ :Optional[torch.Tensor] = None , lowerCamelCase_ :Optional[torch.Tensor] = None , lowerCamelCase_ :Optional[Dict[str, Any]] = None , lowerCamelCase_ :bool = False , lowerCamelCase_ :bool = True , ) -> Union[ControlNetOutput, Tuple]: '''simple docstring''' for i, (image, scale, controlnet) in enumerate(zip(lowerCamelCase_ , lowerCamelCase_ , self.nets ) ): SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : List[Any] = controlnet( lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , ) # merge samples if i == 0: SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : int = down_samples, mid_sample else: SCREAMING_SNAKE_CASE : Optional[int] = [ samples_prev + samples_curr for samples_prev, samples_curr in zip(lowerCamelCase_ , lowerCamelCase_ ) ] mid_block_res_sample += mid_sample return down_block_res_samples, mid_block_res_sample def __lowerCAmelCase ( self :Any , lowerCamelCase_ :Union[str, os.PathLike] , lowerCamelCase_ :bool = True , lowerCamelCase_ :Callable = None , lowerCamelCase_ :bool = False , lowerCamelCase_ :Optional[str] = None , ) -> Optional[int]: '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[Any] = 0 SCREAMING_SNAKE_CASE : Any = save_directory for controlnet in self.nets: controlnet.save_pretrained( lowerCamelCase_ , is_main_process=lowerCamelCase_ , save_function=lowerCamelCase_ , safe_serialization=lowerCamelCase_ , variant=lowerCamelCase_ , ) idx += 1 SCREAMING_SNAKE_CASE : Union[str, Any] = model_path_to_save + f"_{idx}" @classmethod def __lowerCAmelCase ( cls :Dict , lowerCamelCase_ :Optional[Union[str, os.PathLike]] , **lowerCamelCase_ :Tuple ) -> List[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : str = 0 SCREAMING_SNAKE_CASE : Optional[int] = [] # load controlnet and append to list until no controlnet directory exists anymore # first controlnet has to be saved under `./mydirectory/controlnet` to be compliant with `DiffusionPipeline.from_prertained` # second, third, ... controlnets have to be saved under `./mydirectory/controlnet_1`, `./mydirectory/controlnet_2`, ... SCREAMING_SNAKE_CASE : Dict = pretrained_model_path while os.path.isdir(lowerCamelCase_ ): SCREAMING_SNAKE_CASE : Tuple = ControlNetModel.from_pretrained(lowerCamelCase_ , **lowerCamelCase_ ) controlnets.append(lowerCamelCase_ ) idx += 1 SCREAMING_SNAKE_CASE : Union[str, Any] = pretrained_model_path + f"_{idx}" logger.info(f"{len(lowerCamelCase_ )} controlnets loaded from {pretrained_model_path}." ) if len(lowerCamelCase_ ) == 0: raise ValueError( f"No ControlNets found under {os.path.dirname(lowerCamelCase_ )}. Expected at least {pretrained_model_path + '_0'}." ) return cls(lowerCamelCase_ )
18
1
"""simple docstring""" from __future__ import annotations from math import gcd def __A ( a_ : int , a_ : int = 2 , a_ : int = 1 , a_ : int = 3 , )-> int | None: '''simple docstring''' if num < 2: raise ValueError('''The input value cannot be less than 2''' ) # Because of the relationship between ``f(f(x))`` and ``f(x)``, this # algorithm struggles to find factors that are divisible by two. # As a workaround, we specifically check for two and even inputs. # See: https://math.stackexchange.com/a/2856214/165820 if num > 2 and num % 2 == 0: return 2 # Pollard's Rho algorithm requires a function that returns pseudorandom # values between 0 <= X < ``num``. It doesn't need to be random in the # sense that the output value is cryptographically secure or difficult # to calculate, it only needs to be random in the sense that all output # values should be equally likely to appear. # For this reason, Pollard suggested using ``f(x) = (x**2 - 1) % num`` # However, the success of Pollard's algorithm isn't guaranteed and is # determined in part by the initial seed and the chosen random function. # To make retries easier, we will instead use ``f(x) = (x**2 + C) % num`` # where ``C`` is a value that we can modify between each attempt. def rand_fn(a_ : int , a_ : int , a_ : int ) -> int: return (pow(a_ , 2 ) + step) % modulus for _ in range(a_ ): # These track the position within the cycle detection logic. SCREAMING_SNAKE_CASE : str = seed SCREAMING_SNAKE_CASE : List[Any] = seed while True: # At each iteration, the tortoise moves one step and the hare moves two. SCREAMING_SNAKE_CASE : Any = rand_fn(a_ , a_ , a_ ) SCREAMING_SNAKE_CASE : Dict = rand_fn(a_ , a_ , a_ ) SCREAMING_SNAKE_CASE : Optional[int] = rand_fn(a_ , a_ , a_ ) # At some point both the tortoise and the hare will enter a cycle whose # length ``p`` is a divisor of ``num``. Once in that cycle, at some point # the tortoise and hare will end up on the same value modulo ``p``. # We can detect when this happens because the position difference between # the tortoise and the hare will share a common divisor with ``num``. SCREAMING_SNAKE_CASE : str = gcd(hare - tortoise , a_ ) if divisor == 1: # No common divisor yet, just keep searching. continue else: # We found a common divisor! if divisor == num: # Unfortunately, the divisor is ``num`` itself and is useless. break else: # The divisor is a nontrivial factor of ``num``! return divisor # If we made it here, then this attempt failed. # We need to pick a new starting seed for the tortoise and hare # in addition to a new step value for the random function. # To keep this example implementation deterministic, the # new values will be generated based on currently available # values instead of using something like ``random.randint``. # We can use the hare's position as the new seed. # This is actually what Richard Brent's the "optimized" variant does. SCREAMING_SNAKE_CASE : List[Any] = hare # The new step value for the random function can just be incremented. # At first the results will be similar to what the old function would # have produced, but the value will quickly diverge after a bit. step += 1 # We haven't found a divisor within the requested number of attempts. # We were unlucky or ``num`` itself is actually prime. return None if __name__ == "__main__": import argparse lowerCamelCase__ : int = argparse.ArgumentParser() parser.add_argument( "num", type=int, help="The value to find a divisor of", ) parser.add_argument( "--attempts", type=int, default=3, help="The number of attempts before giving up", ) lowerCamelCase__ : Any = parser.parse_args() lowerCamelCase__ : str = pollard_rho(args.num, attempts=args.attempts) if divisor is None: print(f'''{args.num} is probably prime''') else: lowerCamelCase__ : Any = args.num // divisor print(f'''{args.num} = {divisor} * {quotient}''')
18
"""simple docstring""" from __future__ import annotations from numpy import array, cos, cross, floataa, radians, sin from numpy.typing import NDArray def __A ( a_ : float , a_ : float , a_ : bool = False )-> list[float]: '''simple docstring''' if radian_mode: return [magnitude * cos(a_ ), magnitude * sin(a_ )] return [magnitude * cos(radians(a_ ) ), magnitude * sin(radians(a_ ) )] def __A ( a_ : NDArray[floataa] , a_ : NDArray[floataa] , a_ : float = 10**-1 )-> bool: '''simple docstring''' SCREAMING_SNAKE_CASE : NDArray[floataa] = cross(a_ , a_ ) SCREAMING_SNAKE_CASE : float = sum(a_ ) return abs(a_ ) < eps if __name__ == "__main__": # Test to check if it works lowerCamelCase__ : Optional[Any] = array( [ polar_force(7_1_8.4, 180 - 30), polar_force(8_7_9.5_4, 45), polar_force(100, -90), ] ) lowerCamelCase__ : NDArray[floataa] = array([[0, 0], [0, 0], [0, 0]]) assert in_static_equilibrium(forces, location) # Problem 1 in image_data/2D_problems.jpg lowerCamelCase__ : Union[str, Any] = array( [ polar_force(30 * 9.8_1, 15), polar_force(215, 180 - 45), polar_force(264, 90 - 30), ] ) lowerCamelCase__ : Any = array([[0, 0], [0, 0], [0, 0]]) assert in_static_equilibrium(forces, location) # Problem in image_data/2D_problems_1.jpg lowerCamelCase__ : Union[str, Any] = array([[0, -2000], [0, -1200], [0, 15600], [0, -12400]]) lowerCamelCase__ : Optional[int] = array([[0, 0], [6, 0], [10, 0], [12, 0]]) assert in_static_equilibrium(forces, location) import doctest doctest.testmod()
18
1
"""simple docstring""" import json import pathlib import unittest import numpy as np from transformers.testing_utils import require_torch, require_vision, slow 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 DetaImageProcessor class lowercase__( unittest.TestCase ): def __init__( self :Any , lowerCamelCase_ :List[Any] , lowerCamelCase_ :List[Any]=7 , lowerCamelCase_ :List[Any]=3 , lowerCamelCase_ :Dict=30 , lowerCamelCase_ :Union[str, Any]=4_00 , lowerCamelCase_ :List[Any]=True , lowerCamelCase_ :Dict=None , lowerCamelCase_ :Dict=True , lowerCamelCase_ :Any=[0.5, 0.5, 0.5] , lowerCamelCase_ :Union[str, Any]=[0.5, 0.5, 0.5] , lowerCamelCase_ :Optional[Any]=True , lowerCamelCase_ :Dict=1 / 2_55 , lowerCamelCase_ :str=True , ) -> Optional[int]: '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[Any] = size if size is not None else {'''shortest_edge''': 18, '''longest_edge''': 13_33} SCREAMING_SNAKE_CASE : Optional[int] = parent SCREAMING_SNAKE_CASE : Any = batch_size SCREAMING_SNAKE_CASE : List[Any] = num_channels SCREAMING_SNAKE_CASE : Tuple = min_resolution SCREAMING_SNAKE_CASE : Tuple = max_resolution SCREAMING_SNAKE_CASE : Tuple = do_resize SCREAMING_SNAKE_CASE : Optional[Any] = size SCREAMING_SNAKE_CASE : List[str] = do_normalize SCREAMING_SNAKE_CASE : List[str] = image_mean SCREAMING_SNAKE_CASE : Any = image_std SCREAMING_SNAKE_CASE : List[Any] = do_rescale SCREAMING_SNAKE_CASE : Optional[Any] = rescale_factor SCREAMING_SNAKE_CASE : int = do_pad def __lowerCAmelCase ( self :str ) -> List[str]: '''simple docstring''' return { "do_resize": self.do_resize, "size": self.size, "do_normalize": self.do_normalize, "image_mean": self.image_mean, "image_std": self.image_std, "do_rescale": self.do_rescale, "rescale_factor": self.rescale_factor, "do_pad": self.do_pad, } def __lowerCAmelCase ( self :Dict , lowerCamelCase_ :Union[str, Any] , lowerCamelCase_ :Tuple=False ) -> List[str]: '''simple docstring''' if not batched: SCREAMING_SNAKE_CASE : str = image_inputs[0] if isinstance(A__ , Image.Image ): SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : Dict = image.size else: SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : Any = image.shape[1], image.shape[2] if w < h: SCREAMING_SNAKE_CASE : Optional[Any] = int(self.size['''shortest_edge'''] * h / w ) SCREAMING_SNAKE_CASE : str = self.size['''shortest_edge'''] elif w > h: SCREAMING_SNAKE_CASE : Optional[Any] = self.size['''shortest_edge'''] SCREAMING_SNAKE_CASE : Optional[Any] = int(self.size['''shortest_edge'''] * w / h ) else: SCREAMING_SNAKE_CASE : Optional[Any] = self.size['''shortest_edge'''] SCREAMING_SNAKE_CASE : List[str] = self.size['''shortest_edge'''] else: SCREAMING_SNAKE_CASE : str = [] for image in image_inputs: SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : Any = self.get_expected_values([image] ) expected_values.append((expected_height, expected_width) ) SCREAMING_SNAKE_CASE : List[str] = max(A__ , key=lambda lowerCamelCase_ : item[0] )[0] SCREAMING_SNAKE_CASE : Any = max(A__ , key=lambda lowerCamelCase_ : item[1] )[1] return expected_height, expected_width @require_torch @require_vision class lowercase__( _lowerCamelCase , unittest.TestCase ): UpperCamelCase = DetaImageProcessor if is_vision_available() else None def __lowerCAmelCase ( self :Any ) -> Optional[int]: '''simple docstring''' SCREAMING_SNAKE_CASE : Tuple = DetaImageProcessingTester(self ) @property def __lowerCAmelCase ( self :Optional[int] ) -> int: '''simple docstring''' return self.image_processor_tester.prepare_image_processor_dict() def __lowerCAmelCase ( self :Any ) -> Any: '''simple docstring''' SCREAMING_SNAKE_CASE : Dict = self.image_processing_class(**self.image_processor_dict ) self.assertTrue(hasattr(A__ , '''image_mean''' ) ) self.assertTrue(hasattr(A__ , '''image_std''' ) ) self.assertTrue(hasattr(A__ , '''do_normalize''' ) ) self.assertTrue(hasattr(A__ , '''do_resize''' ) ) self.assertTrue(hasattr(A__ , '''do_rescale''' ) ) self.assertTrue(hasattr(A__ , '''do_pad''' ) ) self.assertTrue(hasattr(A__ , '''size''' ) ) def __lowerCAmelCase ( self :Optional[int] ) -> Dict: '''simple docstring''' SCREAMING_SNAKE_CASE : Tuple = self.image_processing_class.from_dict(self.image_processor_dict ) self.assertEqual(image_processor.size , {'''shortest_edge''': 18, '''longest_edge''': 13_33} ) self.assertEqual(image_processor.do_pad , A__ ) def __lowerCAmelCase ( self :Dict ) -> List[str]: '''simple docstring''' pass def __lowerCAmelCase ( self :List[str] ) -> List[str]: '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[Any] = self.image_processing_class(**self.image_processor_dict ) # create random PIL images SCREAMING_SNAKE_CASE : Dict = prepare_image_inputs(self.image_processor_tester , equal_resolution=A__ ) for image in image_inputs: self.assertIsInstance(A__ , Image.Image ) # Test not batched input SCREAMING_SNAKE_CASE : Tuple = image_processing(image_inputs[0] , return_tensors='''pt''' ).pixel_values SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : List[str] = self.image_processor_tester.get_expected_values(A__ ) self.assertEqual( encoded_images.shape , (1, self.image_processor_tester.num_channels, expected_height, expected_width) , ) # Test batched SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : str = self.image_processor_tester.get_expected_values(A__ , batched=A__ ) SCREAMING_SNAKE_CASE : List[str] = image_processing(A__ , return_tensors='''pt''' ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, expected_height, expected_width, ) , ) def __lowerCAmelCase ( self :List[str] ) -> Optional[int]: '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[int] = self.image_processing_class(**self.image_processor_dict ) # create random numpy tensors SCREAMING_SNAKE_CASE : Tuple = prepare_image_inputs(self.image_processor_tester , equal_resolution=A__ , numpify=A__ ) for image in image_inputs: self.assertIsInstance(A__ , np.ndarray ) # Test not batched input SCREAMING_SNAKE_CASE : str = image_processing(image_inputs[0] , return_tensors='''pt''' ).pixel_values SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : List[Any] = self.image_processor_tester.get_expected_values(A__ ) self.assertEqual( encoded_images.shape , (1, self.image_processor_tester.num_channels, expected_height, expected_width) , ) # Test batched SCREAMING_SNAKE_CASE : Tuple = image_processing(A__ , return_tensors='''pt''' ).pixel_values SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : int = self.image_processor_tester.get_expected_values(A__ , batched=A__ ) self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, expected_height, expected_width, ) , ) def __lowerCAmelCase ( self :Optional[int] ) -> List[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : Union[str, Any] = self.image_processing_class(**self.image_processor_dict ) # create random PyTorch tensors SCREAMING_SNAKE_CASE : List[Any] = prepare_image_inputs(self.image_processor_tester , equal_resolution=A__ , torchify=A__ ) for image in image_inputs: self.assertIsInstance(A__ , torch.Tensor ) # Test not batched input SCREAMING_SNAKE_CASE : str = image_processing(image_inputs[0] , return_tensors='''pt''' ).pixel_values SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : Tuple = self.image_processor_tester.get_expected_values(A__ ) self.assertEqual( encoded_images.shape , (1, self.image_processor_tester.num_channels, expected_height, expected_width) , ) # Test batched SCREAMING_SNAKE_CASE : Optional[Any] = image_processing(A__ , return_tensors='''pt''' ).pixel_values SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : str = self.image_processor_tester.get_expected_values(A__ , batched=A__ ) self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, expected_height, expected_width, ) , ) @slow def __lowerCAmelCase ( self :Any ) -> Tuple: '''simple docstring''' SCREAMING_SNAKE_CASE : int = Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png''' ) with open('''./tests/fixtures/tests_samples/COCO/coco_annotations.txt''' , '''r''' ) as f: SCREAMING_SNAKE_CASE : List[Any] = json.loads(f.read() ) SCREAMING_SNAKE_CASE : Tuple = {'''image_id''': 3_97_69, '''annotations''': target} # encode them SCREAMING_SNAKE_CASE : Dict = DetaImageProcessor() SCREAMING_SNAKE_CASE : Any = image_processing(images=A__ , annotations=A__ , return_tensors='''pt''' ) # verify pixel values SCREAMING_SNAKE_CASE : Optional[Any] = torch.Size([1, 3, 8_00, 10_66] ) self.assertEqual(encoding['''pixel_values'''].shape , A__ ) SCREAMING_SNAKE_CASE : List[str] = torch.tensor([0.2_7_9_6, 0.3_1_3_8, 0.3_4_8_1] ) self.assertTrue(torch.allclose(encoding['''pixel_values'''][0, 0, 0, :3] , A__ , atol=1E-4 ) ) # verify area SCREAMING_SNAKE_CASE : str = torch.tensor([5_8_8_7.9_6_0_0, 1_12_50.20_61, 48_93_53.84_38, 83_71_22.75_00, 14_79_67.51_56, 16_57_32.34_38] ) self.assertTrue(torch.allclose(encoding['''labels'''][0]['''area'''] , A__ ) ) # verify boxes SCREAMING_SNAKE_CASE : Tuple = torch.Size([6, 4] ) self.assertEqual(encoding['''labels'''][0]['''boxes'''].shape , A__ ) SCREAMING_SNAKE_CASE : Union[str, Any] = torch.tensor([0.5_5_0_3, 0.2_7_6_5, 0.0_6_0_4, 0.2_2_1_5] ) self.assertTrue(torch.allclose(encoding['''labels'''][0]['''boxes'''][0] , A__ , atol=1E-3 ) ) # verify image_id SCREAMING_SNAKE_CASE : List[str] = torch.tensor([3_97_69] ) self.assertTrue(torch.allclose(encoding['''labels'''][0]['''image_id'''] , A__ ) ) # verify is_crowd SCREAMING_SNAKE_CASE : Union[str, Any] = torch.tensor([0, 0, 0, 0, 0, 0] ) self.assertTrue(torch.allclose(encoding['''labels'''][0]['''iscrowd'''] , A__ ) ) # verify class_labels SCREAMING_SNAKE_CASE : int = torch.tensor([75, 75, 63, 65, 17, 17] ) self.assertTrue(torch.allclose(encoding['''labels'''][0]['''class_labels'''] , A__ ) ) # verify orig_size SCREAMING_SNAKE_CASE : Tuple = torch.tensor([4_80, 6_40] ) self.assertTrue(torch.allclose(encoding['''labels'''][0]['''orig_size'''] , A__ ) ) # verify size SCREAMING_SNAKE_CASE : int = torch.tensor([8_00, 10_66] ) self.assertTrue(torch.allclose(encoding['''labels'''][0]['''size'''] , A__ ) ) @slow def __lowerCAmelCase ( self :List[str] ) -> List[str]: '''simple docstring''' SCREAMING_SNAKE_CASE : Dict = Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png''' ) with open('''./tests/fixtures/tests_samples/COCO/coco_panoptic_annotations.txt''' , '''r''' ) as f: SCREAMING_SNAKE_CASE : List[str] = json.loads(f.read() ) SCREAMING_SNAKE_CASE : Dict = {'''file_name''': '''000000039769.png''', '''image_id''': 3_97_69, '''segments_info''': target} SCREAMING_SNAKE_CASE : Optional[int] = pathlib.Path('''./tests/fixtures/tests_samples/COCO/coco_panoptic''' ) # encode them SCREAMING_SNAKE_CASE : Union[str, Any] = DetaImageProcessor(format='''coco_panoptic''' ) SCREAMING_SNAKE_CASE : Optional[int] = image_processing(images=A__ , annotations=A__ , masks_path=A__ , return_tensors='''pt''' ) # verify pixel values SCREAMING_SNAKE_CASE : str = torch.Size([1, 3, 8_00, 10_66] ) self.assertEqual(encoding['''pixel_values'''].shape , A__ ) SCREAMING_SNAKE_CASE : List[Any] = torch.tensor([0.2_7_9_6, 0.3_1_3_8, 0.3_4_8_1] ) self.assertTrue(torch.allclose(encoding['''pixel_values'''][0, 0, 0, :3] , A__ , atol=1E-4 ) ) # verify area SCREAMING_SNAKE_CASE : Tuple = torch.tensor([14_79_79.68_75, 16_55_27.04_69, 48_46_38.59_38, 1_12_92.93_75, 5_8_7_9.6_5_6_2, 7_6_3_4.1_1_4_7] ) self.assertTrue(torch.allclose(encoding['''labels'''][0]['''area'''] , A__ ) ) # verify boxes SCREAMING_SNAKE_CASE : int = torch.Size([6, 4] ) self.assertEqual(encoding['''labels'''][0]['''boxes'''].shape , A__ ) SCREAMING_SNAKE_CASE : Union[str, Any] = torch.tensor([0.2_6_2_5, 0.5_4_3_7, 0.4_6_8_8, 0.8_6_2_5] ) self.assertTrue(torch.allclose(encoding['''labels'''][0]['''boxes'''][0] , A__ , atol=1E-3 ) ) # verify image_id SCREAMING_SNAKE_CASE : Optional[Any] = torch.tensor([3_97_69] ) self.assertTrue(torch.allclose(encoding['''labels'''][0]['''image_id'''] , A__ ) ) # verify is_crowd SCREAMING_SNAKE_CASE : List[Any] = torch.tensor([0, 0, 0, 0, 0, 0] ) self.assertTrue(torch.allclose(encoding['''labels'''][0]['''iscrowd'''] , A__ ) ) # verify class_labels SCREAMING_SNAKE_CASE : Union[str, Any] = torch.tensor([17, 17, 63, 75, 75, 93] ) self.assertTrue(torch.allclose(encoding['''labels'''][0]['''class_labels'''] , A__ ) ) # verify masks SCREAMING_SNAKE_CASE : List[str] = 82_28_73 self.assertEqual(encoding['''labels'''][0]['''masks'''].sum().item() , A__ ) # verify orig_size SCREAMING_SNAKE_CASE : List[str] = torch.tensor([4_80, 6_40] ) self.assertTrue(torch.allclose(encoding['''labels'''][0]['''orig_size'''] , A__ ) ) # verify size SCREAMING_SNAKE_CASE : Tuple = torch.tensor([8_00, 10_66] ) self.assertTrue(torch.allclose(encoding['''labels'''][0]['''size'''] , A__ ) )
700
"""simple docstring""" from __future__ import annotations import random # Maximum size of the population. Bigger could be faster but is more memory expensive. lowerCamelCase__ : Optional[Any] = 200 # Number of elements selected in every generation of evolution. The selection takes # place from best to worst of that generation and must be smaller than N_POPULATION. lowerCamelCase__ : Optional[int] = 50 # Probability that an element of a generation can mutate, changing one of its genes. # This will guarantee that all genes will be used during evolution. lowerCamelCase__ : Optional[Any] = 0.4 # Just a seed to improve randomness required by the algorithm. random.seed(random.randint(0, 1000)) def __A ( a_ : str , a_ : str )-> tuple[str, float]: '''simple docstring''' SCREAMING_SNAKE_CASE : Tuple = len([g for position, g in enumerate(a_ ) if g == main_target[position]] ) return (item, float(a_ )) def __A ( a_ : str , a_ : str )-> tuple[str, str]: '''simple docstring''' SCREAMING_SNAKE_CASE : Tuple = random.randint(0 , len(a_ ) - 1 ) SCREAMING_SNAKE_CASE : str = parent_a[:random_slice] + parent_a[random_slice:] SCREAMING_SNAKE_CASE : Dict = parent_a[:random_slice] + parent_a[random_slice:] return (child_a, child_a) def __A ( a_ : str , a_ : list[str] )-> str: '''simple docstring''' SCREAMING_SNAKE_CASE : Any = list(a_ ) if random.uniform(0 , 1 ) < MUTATION_PROBABILITY: SCREAMING_SNAKE_CASE : Any = random.choice(a_ ) return "".join(a_ ) def __A ( a_ : tuple[str, float] , a_ : list[tuple[str, float]] , a_ : list[str] , )-> list[str]: '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[int] = [] # Generate more children proportionally to the fitness score. SCREAMING_SNAKE_CASE : List[str] = int(parent_a[1] * 1_00 ) + 1 SCREAMING_SNAKE_CASE : Optional[Any] = 10 if child_n >= 10 else child_n for _ in range(a_ ): SCREAMING_SNAKE_CASE : List[str] = population_score[random.randint(0 , a_ )][0] SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : Tuple = crossover(parent_a[0] , a_ ) # Append new string to the population list. pop.append(mutate(a_ , a_ ) ) pop.append(mutate(a_ , a_ ) ) return pop def __A ( a_ : str , a_ : list[str] , a_ : bool = True )-> tuple[int, int, str]: '''simple docstring''' if N_POPULATION < N_SELECTED: SCREAMING_SNAKE_CASE : List[Any] = F"{N_POPULATION} must be bigger than {N_SELECTED}" raise ValueError(a_ ) # Verify that the target contains no genes besides the ones inside genes variable. SCREAMING_SNAKE_CASE : List[str] = sorted({c for c in target if c not in genes} ) if not_in_genes_list: SCREAMING_SNAKE_CASE : str = F"{not_in_genes_list} is not in genes list, evolution cannot converge" raise ValueError(a_ ) # Generate random starting population. SCREAMING_SNAKE_CASE : Tuple = [] for _ in range(a_ ): population.append(''''''.join([random.choice(a_ ) for i in range(len(a_ ) )] ) ) # Just some logs to know what the algorithms is doing. SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : List[str] = 0, 0 # This loop will end when we find a perfect match for our target. while True: generation += 1 total_population += len(a_ ) # Random population created. Now it's time to evaluate. # Adding a bit of concurrency can make everything faster, # # import concurrent.futures # population_score: list[tuple[str, float]] = [] # with concurrent.futures.ThreadPoolExecutor( # max_workers=NUM_WORKERS) as executor: # futures = {executor.submit(evaluate, item) for item in population} # concurrent.futures.wait(futures) # population_score = [item.result() for item in futures] # # but with a simple algorithm like this, it will probably be slower. # We just need to call evaluate for every item inside the population. SCREAMING_SNAKE_CASE : int = [evaluate(a_ , a_ ) for item in population] # Check if there is a matching evolution. SCREAMING_SNAKE_CASE : List[Any] = sorted(a_ , key=lambda a_ : x[1] , reverse=a_ ) if population_score[0][0] == target: return (generation, total_population, population_score[0][0]) # Print the best result every 10 generation. # Just to know that the algorithm is working. if debug and generation % 10 == 0: print( F"\nGeneration: {generation}" F"\nTotal Population:{total_population}" F"\nBest score: {population_score[0][1]}" F"\nBest string: {population_score[0][0]}" ) # Flush the old population, keeping some of the best evolutions. # Keeping this avoid regression of evolution. SCREAMING_SNAKE_CASE : Optional[Any] = population[: int(N_POPULATION / 3 )] population.clear() population.extend(a_ ) # Normalize population score to be between 0 and 1. SCREAMING_SNAKE_CASE : Optional[int] = [ (item, score / len(a_ )) for item, score in population_score ] # This is selection for i in range(a_ ): population.extend(select(population_score[int(a_ )] , a_ , a_ ) ) # Check if the population has already reached the maximum value and if so, # break the cycle. If this check is disabled, the algorithm will take # forever to compute large strings, but will also calculate small strings in # a far fewer generations. if len(a_ ) > N_POPULATION: break if __name__ == "__main__": lowerCamelCase__ : Dict = ( "This is a genetic algorithm to evaluate, combine, evolve, and mutate a string!" ) lowerCamelCase__ : int = list( " ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklm" "nopqrstuvwxyz.,;!?+-*#@^'èéòà€ù=)(&%$£/\\" ) lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ : Dict = basic(target_str, genes_list) print( f'''\nGeneration: {generation}\nTotal Population: {population}\nTarget: {target}''' )
18
0
"""simple docstring""" import unittest from transformers import AutoTokenizer, FalconConfig, is_torch_available from transformers.testing_utils import require_torch, slow, torch_device from ...generation.test_utils import GenerationTesterMixin from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, ids_tensor, random_attention_mask from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from transformers import ( FalconForCausalLM, FalconForQuestionAnswering, FalconForSequenceClassification, FalconForTokenClassification, FalconModel, ) class lowercase__: '''simple docstring''' def __init__( self :Optional[Any] , lowerCamelCase_ :Tuple , lowerCamelCase_ :Dict=3 , lowerCamelCase_ :str=7 , lowerCamelCase_ :Dict=True , lowerCamelCase_ :Tuple=True , lowerCamelCase_ :int=False , lowerCamelCase_ :Optional[Any]=True , lowerCamelCase_ :List[Any]=99 , lowerCamelCase_ :Union[str, Any]=32 , lowerCamelCase_ :Optional[Any]=5 , lowerCamelCase_ :Any=4 , lowerCamelCase_ :Tuple=37 , lowerCamelCase_ :Optional[int]="gelu" , lowerCamelCase_ :int=0.1 , lowerCamelCase_ :Tuple=0.1 , lowerCamelCase_ :List[str]=5_12 , lowerCamelCase_ :Any=16 , lowerCamelCase_ :Any=2 , lowerCamelCase_ :int=0.0_2 , lowerCamelCase_ :Dict=3 , lowerCamelCase_ :List[Any]=4 , lowerCamelCase_ :Optional[int]=None , ) -> Dict: '''simple docstring''' SCREAMING_SNAKE_CASE : List[str] = parent SCREAMING_SNAKE_CASE : Tuple = batch_size SCREAMING_SNAKE_CASE : Tuple = seq_length SCREAMING_SNAKE_CASE : Any = is_training SCREAMING_SNAKE_CASE : Optional[int] = use_input_mask SCREAMING_SNAKE_CASE : str = use_token_type_ids SCREAMING_SNAKE_CASE : Dict = use_labels SCREAMING_SNAKE_CASE : List[str] = vocab_size SCREAMING_SNAKE_CASE : Optional[Any] = hidden_size SCREAMING_SNAKE_CASE : str = num_hidden_layers SCREAMING_SNAKE_CASE : Optional[Any] = num_attention_heads SCREAMING_SNAKE_CASE : Union[str, Any] = intermediate_size SCREAMING_SNAKE_CASE : List[str] = hidden_act SCREAMING_SNAKE_CASE : List[Any] = hidden_dropout_prob SCREAMING_SNAKE_CASE : Dict = attention_probs_dropout_prob SCREAMING_SNAKE_CASE : int = max_position_embeddings SCREAMING_SNAKE_CASE : Optional[Any] = type_vocab_size SCREAMING_SNAKE_CASE : Any = type_sequence_label_size SCREAMING_SNAKE_CASE : Optional[Any] = initializer_range SCREAMING_SNAKE_CASE : Tuple = num_labels SCREAMING_SNAKE_CASE : Optional[int] = num_choices SCREAMING_SNAKE_CASE : List[Any] = scope def __lowerCAmelCase ( self :int ) -> Tuple: '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[Any] = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) SCREAMING_SNAKE_CASE : List[str] = None if self.use_input_mask: SCREAMING_SNAKE_CASE : Tuple = random_attention_mask([self.batch_size, self.seq_length] ) SCREAMING_SNAKE_CASE : Tuple = None SCREAMING_SNAKE_CASE : List[str] = None SCREAMING_SNAKE_CASE : List[str] = None SCREAMING_SNAKE_CASE : Union[str, Any] = None if self.use_labels: SCREAMING_SNAKE_CASE : Optional[Any] = ids_tensor([self.batch_size] , self.type_sequence_label_size ) SCREAMING_SNAKE_CASE : Any = ids_tensor([self.batch_size, self.seq_length] , self.num_labels ) SCREAMING_SNAKE_CASE : str = ids_tensor([self.batch_size] , self.num_choices ) SCREAMING_SNAKE_CASE : Any = self.get_config() return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels def __lowerCAmelCase ( self :List[Any] ) -> Tuple: '''simple docstring''' return FalconConfig( 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=lowerCAmelCase__ , initializer_range=self.initializer_range , pad_token_id=1 , new_decoder_architecture=lowerCAmelCase__ , ) def __lowerCAmelCase ( self :Dict , lowerCamelCase_ :Optional[int] , lowerCamelCase_ :List[Any] , lowerCamelCase_ :Optional[int] , lowerCamelCase_ :Tuple , lowerCamelCase_ :Union[str, Any] , lowerCamelCase_ :str , lowerCamelCase_ :Union[str, Any] ) -> Dict: '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[Any] = FalconModel(config=lowerCAmelCase__ ) model.to(lowerCAmelCase__ ) model.eval() SCREAMING_SNAKE_CASE : List[str] = model(lowerCAmelCase__ , attention_mask=lowerCAmelCase__ ) SCREAMING_SNAKE_CASE : Dict = model(lowerCAmelCase__ ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def __lowerCAmelCase ( self :Union[str, Any] , lowerCamelCase_ :Tuple , lowerCamelCase_ :Tuple , lowerCamelCase_ :Union[str, Any] , lowerCamelCase_ :Optional[int] , lowerCamelCase_ :Union[str, Any] , lowerCamelCase_ :str , lowerCamelCase_ :Tuple , lowerCamelCase_ :int , lowerCamelCase_ :Union[str, Any] , ) -> Tuple: '''simple docstring''' SCREAMING_SNAKE_CASE : List[Any] = True SCREAMING_SNAKE_CASE : List[str] = FalconModel(lowerCAmelCase__ ) model.to(lowerCAmelCase__ ) model.eval() SCREAMING_SNAKE_CASE : List[Any] = model( lowerCAmelCase__ , attention_mask=lowerCAmelCase__ , encoder_hidden_states=lowerCAmelCase__ , encoder_attention_mask=lowerCAmelCase__ , ) SCREAMING_SNAKE_CASE : str = model( lowerCAmelCase__ , attention_mask=lowerCAmelCase__ , encoder_hidden_states=lowerCAmelCase__ , ) SCREAMING_SNAKE_CASE : Tuple = model(lowerCAmelCase__ , attention_mask=lowerCAmelCase__ ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def __lowerCAmelCase ( self :Optional[Any] , lowerCamelCase_ :List[str] , lowerCamelCase_ :List[Any] , lowerCamelCase_ :int , lowerCamelCase_ :List[Any] , lowerCamelCase_ :int , lowerCamelCase_ :List[str] , lowerCamelCase_ :int , lowerCamelCase_ :Any , lowerCamelCase_ :Dict , ) -> str: '''simple docstring''' SCREAMING_SNAKE_CASE : str = FalconForCausalLM(config=lowerCAmelCase__ ) model.to(lowerCAmelCase__ ) model.eval() SCREAMING_SNAKE_CASE : int = model(lowerCAmelCase__ , attention_mask=lowerCAmelCase__ , labels=lowerCAmelCase__ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.vocab_size) ) def __lowerCAmelCase ( self :Union[str, Any] , lowerCamelCase_ :Optional[int] , lowerCamelCase_ :Dict , lowerCamelCase_ :int , lowerCamelCase_ :Optional[int] , lowerCamelCase_ :Tuple , lowerCamelCase_ :Optional[Any] , lowerCamelCase_ :List[Any] , lowerCamelCase_ :str , lowerCamelCase_ :Dict , ) -> Optional[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : Any = True SCREAMING_SNAKE_CASE : List[str] = True SCREAMING_SNAKE_CASE : int = FalconForCausalLM(config=lowerCAmelCase__ ) model.to(lowerCAmelCase__ ) model.eval() # first forward pass SCREAMING_SNAKE_CASE : List[Any] = model( lowerCAmelCase__ , attention_mask=lowerCAmelCase__ , encoder_hidden_states=lowerCAmelCase__ , encoder_attention_mask=lowerCAmelCase__ , use_cache=lowerCAmelCase__ , ) SCREAMING_SNAKE_CASE : Dict = outputs.past_key_values # create hypothetical multiple next token and extent to next_input_ids SCREAMING_SNAKE_CASE : Optional[Any] = ids_tensor((self.batch_size, 3) , config.vocab_size ) SCREAMING_SNAKE_CASE : str = ids_tensor((self.batch_size, 3) , vocab_size=2 ) # append to next input_ids and SCREAMING_SNAKE_CASE : List[Any] = torch.cat([input_ids, next_tokens] , dim=-1 ) SCREAMING_SNAKE_CASE : str = torch.cat([input_mask, next_mask] , dim=-1 ) SCREAMING_SNAKE_CASE : Optional[int] = model( lowerCAmelCase__ , attention_mask=lowerCAmelCase__ , encoder_hidden_states=lowerCAmelCase__ , encoder_attention_mask=lowerCAmelCase__ , output_hidden_states=lowerCAmelCase__ , )['''hidden_states'''][0] SCREAMING_SNAKE_CASE : Any = model( lowerCAmelCase__ , attention_mask=lowerCAmelCase__ , encoder_hidden_states=lowerCAmelCase__ , encoder_attention_mask=lowerCAmelCase__ , past_key_values=lowerCAmelCase__ , output_hidden_states=lowerCAmelCase__ , )['''hidden_states'''][0] # select random slice SCREAMING_SNAKE_CASE : Tuple = ids_tensor((1,) , output_from_past.shape[-1] ).item() SCREAMING_SNAKE_CASE : Union[str, Any] = output_from_no_past[:, -3:, random_slice_idx].detach() SCREAMING_SNAKE_CASE : Dict = output_from_past[:, :, random_slice_idx].detach() self.parent.assertTrue(output_from_past_slice.shape[1] == next_tokens.shape[1] ) # test that outputs are equal for slice self.parent.assertTrue(torch.allclose(lowerCAmelCase__ , lowerCAmelCase__ , atol=1E-3 ) ) def __lowerCAmelCase ( self :List[str] ) -> str: '''simple docstring''' SCREAMING_SNAKE_CASE : int = self.prepare_config_and_inputs() ( ( SCREAMING_SNAKE_CASE ), ( SCREAMING_SNAKE_CASE ), ( SCREAMING_SNAKE_CASE ), ( SCREAMING_SNAKE_CASE ), ( SCREAMING_SNAKE_CASE ), ( SCREAMING_SNAKE_CASE ), ( SCREAMING_SNAKE_CASE ), ) : List[str] = config_and_inputs SCREAMING_SNAKE_CASE : Union[str, Any] = {'''input_ids''': input_ids, '''attention_mask''': input_mask} return config, inputs_dict @require_torch class lowercase__( _snake_case , _snake_case , _snake_case , unittest.TestCase ): '''simple docstring''' UpperCamelCase = ( ( FalconModel, FalconForCausalLM, FalconForSequenceClassification, FalconForTokenClassification, FalconForQuestionAnswering, ) if is_torch_available() else () ) UpperCamelCase = (FalconForCausalLM,) if is_torch_available() else () UpperCamelCase = ( { """feature-extraction""": FalconModel, """text-classification""": FalconForSequenceClassification, """text-generation""": FalconForCausalLM, """question-answering""": FalconForQuestionAnswering, """token-classification""": FalconForTokenClassification, """zero-shot""": FalconForSequenceClassification, } if is_torch_available() else {} ) UpperCamelCase = False UpperCamelCase = False def __lowerCAmelCase ( self :Any ) -> Dict: '''simple docstring''' SCREAMING_SNAKE_CASE : List[str] = FalconModelTester(self ) SCREAMING_SNAKE_CASE : List[Any] = ConfigTester(self , config_class=lowerCAmelCase__ , hidden_size=37 ) def __lowerCAmelCase ( self :List[str] ) -> Any: '''simple docstring''' self.config_tester.run_common_tests() def __lowerCAmelCase ( self :List[str] ) -> int: '''simple docstring''' SCREAMING_SNAKE_CASE : Any = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*lowerCAmelCase__ ) def __lowerCAmelCase ( self :Any ) -> Union[str, Any]: '''simple docstring''' SCREAMING_SNAKE_CASE, *SCREAMING_SNAKE_CASE : int = self.model_tester.prepare_config_and_inputs() for alibi in [True, False]: SCREAMING_SNAKE_CASE : List[str] = alibi self.model_tester.create_and_check_model(lowerCAmelCase__ , *lowerCAmelCase__ ) def __lowerCAmelCase ( self :Dict ) -> Dict: '''simple docstring''' SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : Tuple = self.model_tester.prepare_config_and_inputs_for_common() SCREAMING_SNAKE_CASE : List[str] = 3 SCREAMING_SNAKE_CASE : Union[str, Any] = input_dict['''input_ids'''] SCREAMING_SNAKE_CASE : Tuple = input_ids.ne(1 ).to(lowerCAmelCase__ ) SCREAMING_SNAKE_CASE : int = ids_tensor([self.model_tester.batch_size] , self.model_tester.type_sequence_label_size ) SCREAMING_SNAKE_CASE : List[Any] = FalconForSequenceClassification(lowerCAmelCase__ ) model.to(lowerCAmelCase__ ) model.eval() SCREAMING_SNAKE_CASE : Tuple = model(lowerCAmelCase__ , attention_mask=lowerCAmelCase__ , labels=lowerCAmelCase__ ) self.assertEqual(result.logits.shape , (self.model_tester.batch_size, self.model_tester.num_labels) ) def __lowerCAmelCase ( self :Optional[Any] ) -> Any: '''simple docstring''' SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : List[Any] = self.model_tester.prepare_config_and_inputs_for_common() SCREAMING_SNAKE_CASE : str = 3 SCREAMING_SNAKE_CASE : Optional[int] = '''single_label_classification''' SCREAMING_SNAKE_CASE : int = input_dict['''input_ids'''] SCREAMING_SNAKE_CASE : List[str] = input_ids.ne(1 ).to(lowerCAmelCase__ ) SCREAMING_SNAKE_CASE : Dict = ids_tensor([self.model_tester.batch_size] , self.model_tester.type_sequence_label_size ) SCREAMING_SNAKE_CASE : Optional[int] = FalconForSequenceClassification(lowerCAmelCase__ ) model.to(lowerCAmelCase__ ) model.eval() SCREAMING_SNAKE_CASE : int = model(lowerCAmelCase__ , attention_mask=lowerCAmelCase__ , labels=lowerCAmelCase__ ) self.assertEqual(result.logits.shape , (self.model_tester.batch_size, self.model_tester.num_labels) ) def __lowerCAmelCase ( self :List[str] ) -> Any: '''simple docstring''' SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : Optional[int] = self.model_tester.prepare_config_and_inputs_for_common() SCREAMING_SNAKE_CASE : List[Any] = input_dict['''input_ids'''] SCREAMING_SNAKE_CASE : int = FalconForCausalLM(lowerCAmelCase__ ) model.to(lowerCAmelCase__ ) model.eval() SCREAMING_SNAKE_CASE : Tuple = model(lowerCAmelCase__ , use_cache=lowerCAmelCase__ ) SCREAMING_SNAKE_CASE : Tuple = input_ids.shape[0] SCREAMING_SNAKE_CASE : Any = model._convert_to_rw_cache(result.past_key_values ) SCREAMING_SNAKE_CASE : int = model._convert_cache_to_standard_format(lowerCAmelCase__ , lowerCAmelCase__ ) for layer in range(len(lowerCAmelCase__ ) ): for tensor_idx in range(2 ): self.assertTrue(rw_cache[layer][tensor_idx].ndim == 3 ) self.assertTrue(result.past_key_values[layer][tensor_idx].ndim == 4 ) self.assertTrue( torch.all(result.past_key_values[layer][tensor_idx] == standard_cache[layer][tensor_idx] ) ) def __lowerCAmelCase ( self :List[str] ) -> Dict: '''simple docstring''' SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : Dict = self.model_tester.prepare_config_and_inputs_for_common() SCREAMING_SNAKE_CASE : List[str] = 3 SCREAMING_SNAKE_CASE : List[str] = '''multi_label_classification''' SCREAMING_SNAKE_CASE : Any = input_dict['''input_ids'''] SCREAMING_SNAKE_CASE : List[str] = input_ids.ne(1 ).to(lowerCAmelCase__ ) SCREAMING_SNAKE_CASE : Any = ids_tensor( [self.model_tester.batch_size, config.num_labels] , self.model_tester.type_sequence_label_size ).to(torch.float ) SCREAMING_SNAKE_CASE : List[str] = FalconForSequenceClassification(lowerCAmelCase__ ) model.to(lowerCAmelCase__ ) model.eval() SCREAMING_SNAKE_CASE : Tuple = model(lowerCAmelCase__ , attention_mask=lowerCAmelCase__ , labels=lowerCAmelCase__ ) self.assertEqual(result.logits.shape , (self.model_tester.batch_size, self.model_tester.num_labels) ) def __lowerCAmelCase ( self :Union[str, Any] ) -> Tuple: '''simple docstring''' for model_class in self.all_generative_model_classes: SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : Dict = self.model_tester.prepare_config_and_inputs_for_common() # If it doesn't support cache, pass the test if not hasattr(lowerCAmelCase__ , '''use_cache''' ): return SCREAMING_SNAKE_CASE : Dict = model_class(lowerCAmelCase__ ).to(lowerCAmelCase__ ) if "use_cache" not in inputs: SCREAMING_SNAKE_CASE : int = True SCREAMING_SNAKE_CASE : str = model(**lowerCAmelCase__ ) # If "past_key_values" is not returned, pass the test (e.g. RWKV uses a different cache name and format) if "past_key_values" not in outputs: return SCREAMING_SNAKE_CASE : List[str] = ( getattr(lowerCAmelCase__ , '''decoder_layers''' , lowerCAmelCase__ ) or getattr(lowerCAmelCase__ , '''num_decoder_layers''' , lowerCAmelCase__ ) or config.num_hidden_layers ) SCREAMING_SNAKE_CASE : str = getattr(lowerCAmelCase__ , '''num_kv_heads''' , config.num_attention_heads ) SCREAMING_SNAKE_CASE : Tuple = getattr(lowerCAmelCase__ , '''d_model''' , config.hidden_size ) SCREAMING_SNAKE_CASE : Optional[Any] = embed_dim // num_attention_heads SCREAMING_SNAKE_CASE : Union[str, Any] = outputs['''past_key_values'''] self.assertEqual(len(lowerCAmelCase__ ) , lowerCAmelCase__ ) SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : Optional[Any] = inputs['''input_ids'''].shape for i in range(lowerCAmelCase__ ): if config.new_decoder_architecture: SCREAMING_SNAKE_CASE : Tuple = config.num_attention_heads elif config.multi_query: SCREAMING_SNAKE_CASE : List[str] = 1 self.assertEqual(len(past_kv[0] ) , 2 ) # K V for the decoder = 2 self.assertEqual( past_kv[i][0].shape , (batch_size, num_attention_heads, seq_length, per_head_embed_dim) ) self.assertEqual( past_kv[i][1].shape , (batch_size, num_attention_heads, seq_length, per_head_embed_dim) ) @require_torch class lowercase__( unittest.TestCase ): '''simple docstring''' @slow def __lowerCAmelCase ( self :Optional[Any] ) -> Optional[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : int = AutoTokenizer.from_pretrained('''Rocketknight1/falcon-rw-1b''' ) SCREAMING_SNAKE_CASE : Union[str, Any] = FalconForCausalLM.from_pretrained('''Rocketknight1/falcon-rw-1b''' ) model.eval() model.to(lowerCAmelCase__ ) SCREAMING_SNAKE_CASE : Dict = tokenizer('''My favorite food is''' , return_tensors='''pt''' ).to(lowerCAmelCase__ ) SCREAMING_SNAKE_CASE : List[Any] = ( '''My favorite food is pizza. I love it so much that I have a pizza party every year for my birthday.''' ) SCREAMING_SNAKE_CASE : List[Any] = model.generate(**lowerCAmelCase__ , do_sample=lowerCAmelCase__ , max_new_tokens=19 ) SCREAMING_SNAKE_CASE : Dict = tokenizer.batch_decode(lowerCAmelCase__ )[0] self.assertEqual(lowerCAmelCase__ , lowerCAmelCase__ ) @slow def __lowerCAmelCase ( self :Optional[int] ) -> Optional[int]: '''simple docstring''' for repo in ["Rocketknight1/tiny-random-falcon-7b", "Rocketknight1/tiny-random-falcon-40b"]: SCREAMING_SNAKE_CASE : str = AutoTokenizer.from_pretrained(lowerCAmelCase__ ) SCREAMING_SNAKE_CASE : Optional[int] = FalconForCausalLM.from_pretrained(lowerCAmelCase__ ) model.eval() model.to(lowerCAmelCase__ ) SCREAMING_SNAKE_CASE : List[Any] = tokenizer('''My favorite food is''' , return_tensors='''pt''' ).to(lowerCAmelCase__ ) # We just test that these run without errors - the models are randomly initialized # and so the actual text outputs will be garbage model.generate(**lowerCAmelCase__ , do_sample=lowerCAmelCase__ , max_new_tokens=4 ) model.generate(**lowerCAmelCase__ , do_sample=lowerCAmelCase__ , max_new_tokens=4 ) model.generate(**lowerCAmelCase__ , num_beams=2 , max_new_tokens=4 ) @slow def __lowerCAmelCase ( self :List[str] ) -> Tuple: '''simple docstring''' with torch.no_grad(): for repo in [ "Rocketknight1/falcon-rw-1b", "Rocketknight1/tiny-random-falcon-7b", "Rocketknight1/tiny-random-falcon-40b", ]: SCREAMING_SNAKE_CASE : Dict = AutoTokenizer.from_pretrained(lowerCAmelCase__ ) SCREAMING_SNAKE_CASE : Dict = FalconForCausalLM.from_pretrained(lowerCAmelCase__ ) model.eval() model.to(device=lowerCAmelCase__ ) SCREAMING_SNAKE_CASE : Dict = tokenizer('''My favorite food is''' , return_tensors='''pt''' ).to(lowerCAmelCase__ ) # Test results are the same with and without cache SCREAMING_SNAKE_CASE : Any = model.generate(**lowerCAmelCase__ , do_sample=lowerCAmelCase__ , max_new_tokens=20 , use_cache=lowerCAmelCase__ ) SCREAMING_SNAKE_CASE : str = model.generate(**lowerCAmelCase__ , do_sample=lowerCAmelCase__ , max_new_tokens=20 , use_cache=lowerCAmelCase__ ) self.assertTrue((outputs_cache - outputs_no_cache).sum().item() == 0 )
701
"""simple docstring""" import argparse import ast import logging import os import sys import pandas as pd import torch from tqdm import tqdm from transformers import BartForConditionalGeneration, RagRetriever, RagSequenceForGeneration, RagTokenForGeneration from transformers import logging as transformers_logging sys.path.append(os.path.join(os.getcwd())) # noqa: E402 # isort:skip from utils_rag import exact_match_score, fa_score # noqa: E402 # isort:skip lowerCamelCase__ : Optional[Any] = logging.getLogger(__name__) logging.basicConfig(level=logging.INFO) transformers_logging.set_verbosity_info() def __A ( a_ : Optional[int] )-> Dict: '''simple docstring''' if "token" in model_name_or_path: return "rag_token" if "sequence" in model_name_or_path: return "rag_sequence" if "bart" in model_name_or_path: return "bart" return None def __A ( a_ : List[Any] , a_ : Optional[int] , a_ : Optional[int] )-> Dict: '''simple docstring''' return max(metric_fn(a_ , a_ ) for gt in ground_truths ) def __A ( a_ : List[Any] , a_ : Union[str, Any] , a_ : str )-> str: '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[int] = [line.strip() for line in open(a_ , '''r''' ).readlines()] SCREAMING_SNAKE_CASE : Optional[Any] = [] if args.gold_data_mode == "qa": SCREAMING_SNAKE_CASE : List[Any] = pd.read_csv(a_ , sep='''\t''' , header=a_ ) for answer_list in data[1]: SCREAMING_SNAKE_CASE : str = ast.literal_eval(a_ ) answers.append(a_ ) else: SCREAMING_SNAKE_CASE : Any = [line.strip() for line in open(a_ , '''r''' ).readlines()] SCREAMING_SNAKE_CASE : Dict = [[reference] for reference in references] SCREAMING_SNAKE_CASE : Dict = 0 for prediction, ground_truths in zip(a_ , a_ ): total += 1 em += metric_max_over_ground_truths(a_ , a_ , a_ ) fa += metric_max_over_ground_truths(a_ , a_ , a_ ) SCREAMING_SNAKE_CASE : Any = 100.0 * em / total SCREAMING_SNAKE_CASE : Optional[int] = 100.0 * fa / total logger.info(F"F1: {fa:.2f}" ) logger.info(F"EM: {em:.2f}" ) def __A ( a_ : Any , a_ : Any , a_ : List[Any] )-> Tuple: '''simple docstring''' SCREAMING_SNAKE_CASE : str = args.k SCREAMING_SNAKE_CASE : Tuple = [line.strip() for line in open(a_ , '''r''' ).readlines()] SCREAMING_SNAKE_CASE : Union[str, Any] = [line.strip() for line in open(a_ , '''r''' ).readlines()] SCREAMING_SNAKE_CASE : Dict = 0 for hypo, reference in zip(a_ , a_ ): SCREAMING_SNAKE_CASE : Optional[int] = set(hypo.split('''\t''' )[:k] ) SCREAMING_SNAKE_CASE : List[str] = set(reference.split('''\t''' ) ) total += 1 em += len(hypo_provenance & ref_provenance ) / k SCREAMING_SNAKE_CASE : Dict = 100.0 * em / total logger.info(F"Precision@{k}: {em: .2f}" ) def __A ( a_ : Any , a_ : List[str] , a_ : str )-> int: '''simple docstring''' def strip_title(a_ : Optional[Any] ): if title.startswith('''"''' ): SCREAMING_SNAKE_CASE : Tuple = title[1:] if title.endswith('''"''' ): SCREAMING_SNAKE_CASE : Any = title[:-1] return title SCREAMING_SNAKE_CASE : Tuple = rag_model.retriever.question_encoder_tokenizer.batch_encode_plus( a_ , return_tensors='''pt''' , padding=a_ , truncation=a_ , )['''input_ids'''].to(args.device ) SCREAMING_SNAKE_CASE : Any = rag_model.rag.question_encoder(a_ ) SCREAMING_SNAKE_CASE : Union[str, Any] = question_enc_outputs[0] SCREAMING_SNAKE_CASE : Dict = rag_model.retriever( a_ , question_enc_pool_output.cpu().detach().to(torch.floataa ).numpy() , prefix=rag_model.rag.generator.config.prefix , n_docs=rag_model.config.n_docs , return_tensors='''pt''' , ) SCREAMING_SNAKE_CASE : Any = rag_model.retriever.index.get_doc_dicts(result.doc_ids ) SCREAMING_SNAKE_CASE : Dict = [] for docs in all_docs: SCREAMING_SNAKE_CASE : List[Any] = [strip_title(a_ ) for title in docs['''title''']] provenance_strings.append('''\t'''.join(a_ ) ) return provenance_strings def __A ( a_ : List[Any] , a_ : int , a_ : str )-> Tuple: '''simple docstring''' with torch.no_grad(): SCREAMING_SNAKE_CASE : Optional[int] = rag_model.retriever.question_encoder_tokenizer.batch_encode_plus( a_ , return_tensors='''pt''' , padding=a_ , truncation=a_ ) SCREAMING_SNAKE_CASE : Dict = inputs_dict.input_ids.to(args.device ) SCREAMING_SNAKE_CASE : Any = inputs_dict.attention_mask.to(args.device ) SCREAMING_SNAKE_CASE : Tuple = rag_model.generate( # rag_model overwrites generate a_ , attention_mask=a_ , num_beams=args.num_beams , min_length=args.min_length , max_length=args.max_length , early_stopping=a_ , num_return_sequences=1 , bad_words_ids=[[0, 0]] , ) SCREAMING_SNAKE_CASE : Dict = rag_model.retriever.generator_tokenizer.batch_decode(a_ , skip_special_tokens=a_ ) if args.print_predictions: for q, a in zip(a_ , a_ ): logger.info('''Q: {} - A: {}'''.format(a_ , a_ ) ) return answers def __A ( )-> int: '''simple docstring''' SCREAMING_SNAKE_CASE : int = argparse.ArgumentParser() parser.add_argument( '''--model_type''' , choices=['''rag_sequence''', '''rag_token''', '''bart'''] , type=a_ , help=( '''RAG model type: rag_sequence, rag_token or bart, if none specified, the type is inferred from the''' ''' model_name_or_path''' ) , ) parser.add_argument( '''--index_name''' , default=a_ , choices=['''exact''', '''compressed''', '''legacy'''] , type=a_ , help='''RAG model retriever type''' , ) parser.add_argument( '''--index_path''' , default=a_ , type=a_ , help='''Path to the retrieval index''' , ) parser.add_argument('''--n_docs''' , default=5 , type=a_ , help='''Number of retrieved docs''' ) parser.add_argument( '''--model_name_or_path''' , default=a_ , type=a_ , required=a_ , help='''Path to pretrained checkpoints or model identifier from huggingface.co/models''' , ) parser.add_argument( '''--eval_mode''' , choices=['''e2e''', '''retrieval'''] , default='''e2e''' , type=a_ , help=( '''Evaluation mode, e2e calculates exact match and F1 of the downstream task, retrieval calculates''' ''' precision@k.''' ) , ) parser.add_argument('''--k''' , default=1 , type=a_ , help='''k for the precision@k calculation''' ) parser.add_argument( '''--evaluation_set''' , default=a_ , type=a_ , required=a_ , help='''Path to a file containing evaluation samples''' , ) parser.add_argument( '''--gold_data_path''' , default=a_ , type=a_ , required=a_ , help='''Path to a tab-separated file with gold samples''' , ) parser.add_argument( '''--gold_data_mode''' , default='''qa''' , type=a_ , choices=['''qa''', '''ans'''] , help=( '''Format of the gold data file''' '''qa - a single line in the following format: question [tab] answer_list''' '''ans - a single line of the gold file contains the expected answer string''' ) , ) parser.add_argument( '''--predictions_path''' , type=a_ , default='''predictions.txt''' , help='''Name of the predictions file, to be stored in the checkpoints directory''' , ) parser.add_argument( '''--eval_all_checkpoints''' , action='''store_true''' , help='''Evaluate all checkpoints starting with the same prefix as model_name ending and ending with step number''' , ) parser.add_argument( '''--eval_batch_size''' , default=8 , type=a_ , help='''Batch size per GPU/CPU for evaluation.''' , ) parser.add_argument( '''--recalculate''' , help='''Recalculate predictions even if the prediction file exists''' , action='''store_true''' , ) parser.add_argument( '''--num_beams''' , default=4 , type=a_ , help='''Number of beams to be used when generating answers''' , ) parser.add_argument('''--min_length''' , default=1 , type=a_ , help='''Min length of the generated answers''' ) parser.add_argument('''--max_length''' , default=50 , type=a_ , help='''Max length of the generated answers''' ) parser.add_argument( '''--print_predictions''' , action='''store_true''' , help='''If True, prints predictions while evaluating.''' , ) parser.add_argument( '''--print_docs''' , action='''store_true''' , help='''If True, prints docs retried while generating.''' , ) SCREAMING_SNAKE_CASE : List[str] = parser.parse_args() SCREAMING_SNAKE_CASE : Dict = torch.device('''cuda''' if torch.cuda.is_available() else '''cpu''' ) return args def __A ( a_ : Optional[Any] )-> int: '''simple docstring''' SCREAMING_SNAKE_CASE : Any = {} if args.model_type is None: SCREAMING_SNAKE_CASE : List[str] = infer_model_type(args.model_name_or_path ) assert args.model_type is not None if args.model_type.startswith('''rag''' ): SCREAMING_SNAKE_CASE : List[str] = RagTokenForGeneration if args.model_type == '''rag_token''' else RagSequenceForGeneration SCREAMING_SNAKE_CASE : Optional[Any] = args.n_docs if args.index_name is not None: SCREAMING_SNAKE_CASE : Tuple = args.index_name if args.index_path is not None: SCREAMING_SNAKE_CASE : List[Any] = args.index_path else: SCREAMING_SNAKE_CASE : str = BartForConditionalGeneration SCREAMING_SNAKE_CASE : Optional[int] = ( [f.path for f in os.scandir(args.model_name_or_path ) if f.is_dir()] if args.eval_all_checkpoints else [args.model_name_or_path] ) logger.info('''Evaluate the following checkpoints: %s''' , a_ ) SCREAMING_SNAKE_CASE : int = get_scores if args.eval_mode == '''e2e''' else get_precision_at_k SCREAMING_SNAKE_CASE : str = evaluate_batch_eae if args.eval_mode == '''e2e''' else evaluate_batch_retrieval for checkpoint in checkpoints: if os.path.exists(args.predictions_path ) and (not args.recalculate): logger.info('''Calculating metrics based on an existing predictions file: {}'''.format(args.predictions_path ) ) score_fn(a_ , args.predictions_path , args.gold_data_path ) continue logger.info('''***** Running evaluation for {} *****'''.format(a_ ) ) logger.info(''' Batch size = %d''' , args.eval_batch_size ) logger.info(''' Predictions will be stored under {}'''.format(args.predictions_path ) ) if args.model_type.startswith('''rag''' ): SCREAMING_SNAKE_CASE : Dict = RagRetriever.from_pretrained(a_ , **a_ ) SCREAMING_SNAKE_CASE : Union[str, Any] = model_class.from_pretrained(a_ , retriever=a_ , **a_ ) model.retriever.init_retrieval() else: SCREAMING_SNAKE_CASE : str = model_class.from_pretrained(a_ , **a_ ) model.to(args.device ) with open(args.evaluation_set , '''r''' ) as eval_file, open(args.predictions_path , '''w''' ) as preds_file: SCREAMING_SNAKE_CASE : Dict = [] for line in tqdm(a_ ): questions.append(line.strip() ) if len(a_ ) == args.eval_batch_size: SCREAMING_SNAKE_CASE : str = evaluate_batch_fn(a_ , a_ , a_ ) preds_file.write('''\n'''.join(a_ ) + '''\n''' ) preds_file.flush() SCREAMING_SNAKE_CASE : Union[str, Any] = [] if len(a_ ) > 0: SCREAMING_SNAKE_CASE : str = evaluate_batch_fn(a_ , a_ , a_ ) preds_file.write('''\n'''.join(a_ ) ) preds_file.flush() score_fn(a_ , args.predictions_path , args.gold_data_path ) if __name__ == "__main__": lowerCamelCase__ : List[str] = get_args() main(args)
18
0
"""simple docstring""" def __A ( a_ : int = 60_08_51_47_51_43 )-> int: '''simple docstring''' try: SCREAMING_SNAKE_CASE : Tuple = int(_SCREAMING_SNAKE_CASE ) except (TypeError, ValueError): raise TypeError('''Parameter n must be int or castable to int.''' ) if n <= 0: raise ValueError('''Parameter n must be greater than or equal to one.''' ) SCREAMING_SNAKE_CASE : Any = 1 SCREAMING_SNAKE_CASE : List[str] = 2 while i * i <= n: while n % i == 0: SCREAMING_SNAKE_CASE : Optional[Any] = i n //= i i += 1 if n > 1: SCREAMING_SNAKE_CASE : int = n return int(_SCREAMING_SNAKE_CASE ) if __name__ == "__main__": print(f'''{solution() = }''')
702
"""simple docstring""" import json import os from typing import Optional, Tuple from ...tokenization_utils import PreTrainedTokenizer from ...utils import logging lowerCamelCase__ : List[str] = logging.get_logger(__name__) lowerCamelCase__ : Optional[int] = {"vocab_file": "vocab.json"} lowerCamelCase__ : Dict = { "vocab_file": { "mgp-str": "https://huggingface.co/alibaba-damo/mgp-str-base/blob/main/vocab.json", } } lowerCamelCase__ : Optional[Any] = {"mgp-str": 27} class lowercase__( _UpperCAmelCase ): '''simple docstring''' UpperCamelCase = VOCAB_FILES_NAMES UpperCamelCase = PRETRAINED_VOCAB_FILES_MAP UpperCamelCase = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES def __init__( self :int , lowerCamelCase_ :Optional[int] , lowerCamelCase_ :Optional[int]="[GO]" , lowerCamelCase_ :int="[GO]" , lowerCamelCase_ :str="[s]" , lowerCamelCase_ :Dict="[GO]" , **lowerCamelCase_ :List[str] ) -> Tuple: '''simple docstring''' super().__init__( unk_token=lowerCamelCase_ , bos_token=lowerCamelCase_ , eos_token=lowerCamelCase_ , pad_token=lowerCamelCase_ , **lowerCamelCase_ , ) with open(lowerCamelCase_ , encoding='''utf-8''' ) as vocab_handle: SCREAMING_SNAKE_CASE : int = json.load(lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Union[str, Any] = {v: k for k, v in self.vocab.items()} @property def __lowerCAmelCase ( self :int ) -> Dict: '''simple docstring''' return len(self.vocab ) def __lowerCAmelCase ( self :List[str] ) -> Dict: '''simple docstring''' return dict(self.vocab , **self.added_tokens_encoder ) def __lowerCAmelCase ( self :List[str] , lowerCamelCase_ :Optional[Any] ) -> List[str]: '''simple docstring''' SCREAMING_SNAKE_CASE : Union[str, Any] = [] for s in text: char_tokens.extend(lowerCamelCase_ ) return char_tokens def __lowerCAmelCase ( self :Dict , lowerCamelCase_ :Tuple ) -> Optional[int]: '''simple docstring''' return self.vocab.get(lowerCamelCase_ , self.vocab.get(self.unk_token ) ) def __lowerCAmelCase ( self :List[str] , lowerCamelCase_ :Dict ) -> Optional[int]: '''simple docstring''' return self.decoder.get(lowerCamelCase_ ) def __lowerCAmelCase ( self :Optional[int] , lowerCamelCase_ :str , lowerCamelCase_ :Optional[str] = None ) -> Tuple[str]: '''simple docstring''' if not os.path.isdir(lowerCamelCase_ ): logger.error('''Vocabulary path ({}) should be a directory'''.format(lowerCamelCase_ ) ) return SCREAMING_SNAKE_CASE : List[Any] = os.path.join( lowerCamelCase_ , (filename_prefix + '''-''' if filename_prefix else '''''') + VOCAB_FILES_NAMES['''vocab_file'''] ) with open(lowerCamelCase_ , '''w''' , encoding='''utf-8''' ) as f: f.write(json.dumps(self.vocab , indent=2 , sort_keys=lowerCamelCase_ , ensure_ascii=lowerCamelCase_ ) + '''\n''' ) return (vocab_file,)
18
0
from typing import List import numpy as np def __A ( a_ : Tuple )-> int: '''simple docstring''' SCREAMING_SNAKE_CASE : Tuple = {key: len(a_ ) for key, value in gen_kwargs.items() if isinstance(a_ , a_ )} if len(set(lists_lengths.values() ) ) > 1: raise RuntimeError( ( '''Sharding is ambiguous for this dataset: ''' + '''we found several data sources lists of different lengths, and we don\'t know over which list we should parallelize:\n''' + '''\n'''.join(F"\t- key {key} has length {length}" for key, length in lists_lengths.items() ) + '''\nTo fix this, check the \'gen_kwargs\' and make sure to use lists only for data sources, ''' + '''and use tuples otherwise. In the end there should only be one single list, or several lists with the same length.''' ) ) SCREAMING_SNAKE_CASE : List[str] = max(lists_lengths.values() , default=0 ) return max(1 , a_ ) def __A ( a_ : Tuple , a_ : Any )-> List[range]: '''simple docstring''' SCREAMING_SNAKE_CASE : Dict = [] for group_idx in range(a_ ): SCREAMING_SNAKE_CASE : List[str] = num_shards // max_num_jobs + (group_idx < (num_shards % max_num_jobs)) if num_shards_to_add == 0: break SCREAMING_SNAKE_CASE : Optional[Any] = shards_indices_per_group[-1].stop if shards_indices_per_group else 0 SCREAMING_SNAKE_CASE : Dict = range(a_ , start + num_shards_to_add ) shards_indices_per_group.append(a_ ) return shards_indices_per_group def __A ( a_ : int , a_ : Any )-> List[dict]: '''simple docstring''' SCREAMING_SNAKE_CASE : Any = _number_of_shards_in_gen_kwargs(a_ ) if num_shards == 1: return [dict(a_ )] else: SCREAMING_SNAKE_CASE : List[str] = _distribute_shards(num_shards=a_ , max_num_jobs=a_ ) return [ { key: [value[shard_idx] for shard_idx in shard_indices_per_group[group_idx]] if isinstance(a_ , a_ ) else value for key, value in gen_kwargs.items() } for group_idx in range(len(a_ ) ) ] def __A ( a_ : Dict )-> dict: '''simple docstring''' return { key: [value for gen_kwargs in gen_kwargs_list for value in gen_kwargs[key]] if isinstance(gen_kwargs_list[0][key] , a_ ) else gen_kwargs_list[0][key] for key in gen_kwargs_list[0] } def __A ( a_ : Optional[Any] , a_ : Optional[Any] )-> dict: '''simple docstring''' SCREAMING_SNAKE_CASE : str = {len(a_ ) for value in gen_kwargs.values() if isinstance(a_ , a_ )} SCREAMING_SNAKE_CASE : Optional[int] = {} for size in list_sizes: SCREAMING_SNAKE_CASE : Tuple = list(range(a_ ) ) rng.shuffle(indices_per_size[size] ) # Now let's copy the gen_kwargs and shuffle the lists based on their sizes SCREAMING_SNAKE_CASE : str = dict(a_ ) for key, value in shuffled_kwargs.items(): if isinstance(a_ , a_ ): SCREAMING_SNAKE_CASE : List[Any] = [value[i] for i in indices_per_size[len(a_ )]] return shuffled_kwargs
703
"""simple docstring""" from ...configuration_utils import PretrainedConfig from ...utils import logging lowerCamelCase__ : Tuple = logging.get_logger(__name__) lowerCamelCase__ : str = { "studio-ousia/luke-base": "https://huggingface.co/studio-ousia/luke-base/resolve/main/config.json", "studio-ousia/luke-large": "https://huggingface.co/studio-ousia/luke-large/resolve/main/config.json", } class lowercase__( _UpperCAmelCase ): '''simple docstring''' UpperCamelCase = """luke""" def __init__( self :Optional[int] , lowerCamelCase_ :Union[str, Any]=5_02_67 , lowerCamelCase_ :int=50_00_00 , lowerCamelCase_ :Tuple=7_68 , lowerCamelCase_ :List[str]=2_56 , lowerCamelCase_ :Dict=12 , lowerCamelCase_ :Optional[int]=12 , lowerCamelCase_ :Optional[Any]=30_72 , lowerCamelCase_ :List[Any]="gelu" , lowerCamelCase_ :Any=0.1 , lowerCamelCase_ :Union[str, Any]=0.1 , lowerCamelCase_ :List[str]=5_12 , lowerCamelCase_ :Optional[int]=2 , lowerCamelCase_ :Tuple=0.0_2 , lowerCamelCase_ :Optional[int]=1E-12 , lowerCamelCase_ :List[Any]=True , lowerCamelCase_ :List[str]=None , lowerCamelCase_ :str=1 , lowerCamelCase_ :Any=0 , lowerCamelCase_ :str=2 , **lowerCamelCase_ :List[Any] , ) -> Optional[int]: '''simple docstring''' super().__init__(pad_token_id=lowerCamelCase_ , bos_token_id=lowerCamelCase_ , eos_token_id=lowerCamelCase_ , **lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Dict = vocab_size SCREAMING_SNAKE_CASE : List[str] = entity_vocab_size SCREAMING_SNAKE_CASE : str = hidden_size SCREAMING_SNAKE_CASE : Union[str, Any] = entity_emb_size SCREAMING_SNAKE_CASE : List[Any] = num_hidden_layers SCREAMING_SNAKE_CASE : Optional[int] = num_attention_heads SCREAMING_SNAKE_CASE : Optional[Any] = hidden_act SCREAMING_SNAKE_CASE : Optional[Any] = intermediate_size SCREAMING_SNAKE_CASE : Union[str, Any] = hidden_dropout_prob SCREAMING_SNAKE_CASE : Dict = attention_probs_dropout_prob SCREAMING_SNAKE_CASE : Dict = max_position_embeddings SCREAMING_SNAKE_CASE : Tuple = type_vocab_size SCREAMING_SNAKE_CASE : str = initializer_range SCREAMING_SNAKE_CASE : Dict = layer_norm_eps SCREAMING_SNAKE_CASE : Optional[int] = use_entity_aware_attention SCREAMING_SNAKE_CASE : List[str] = classifier_dropout
18
0
"""simple docstring""" import argparse import json import os import time import zipfile from get_ci_error_statistics import download_artifact, get_artifacts_links from transformers import logging lowerCamelCase__ : Any = logging.get_logger(__name__) def __A ( a_ : str , a_ : Optional[Any] )-> Union[str, Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : str = set() SCREAMING_SNAKE_CASE : Optional[Any] = [] def parse_line(a_ : int ): for line in fp: if isinstance(_lowercase , _lowercase ): SCREAMING_SNAKE_CASE : List[str] = line.decode('''UTF-8''' ) if "warnings summary (final)" in line: continue # This means we are outside the body of a warning elif not line.startswith(''' ''' ): # process a single warning and move it to `selected_warnings`. if len(_lowercase ) > 0: SCREAMING_SNAKE_CASE : List[str] = '\n'.join(_lowercase ) # Only keep the warnings specified in `targets` if any(F": {x}: " in warning for x in targets ): selected_warnings.add(_lowercase ) buffer.clear() continue else: SCREAMING_SNAKE_CASE : Any = line.strip() buffer.append(_lowercase ) if from_gh: for filename in os.listdir(_lowercase ): SCREAMING_SNAKE_CASE : Dict = os.path.join(_lowercase , _lowercase ) if not os.path.isdir(_lowercase ): # read the file if filename != "warnings.txt": continue with open(_lowercase ) as fp: parse_line(_lowercase ) else: try: with zipfile.ZipFile(_lowercase ) as z: for filename in z.namelist(): if not os.path.isdir(_lowercase ): # read the file if filename != "warnings.txt": continue with z.open(_lowercase ) as fp: parse_line(_lowercase ) except Exception: logger.warning( F"{artifact_path} is either an invalid zip file or something else wrong. This file is skipped." ) return selected_warnings def __A ( a_ : Optional[int] , a_ : Union[str, Any] )-> Optional[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : Tuple = set() SCREAMING_SNAKE_CASE : Any = [os.path.join(_lowercase , _lowercase ) for p in os.listdir(_lowercase ) if (p.endswith('''.zip''' ) or from_gh)] for p in paths: selected_warnings.update(extract_warnings_from_single_artifact(_lowercase , _lowercase ) ) return selected_warnings if __name__ == "__main__": def __A ( a_ : Dict )-> Optional[Any]: '''simple docstring''' return values.split(''',''' ) lowerCamelCase__ : str = argparse.ArgumentParser() # Required parameters parser.add_argument("--workflow_run_id", type=str, required=True, help="A GitHub Actions workflow run id.") parser.add_argument( "--output_dir", type=str, required=True, help="Where to store the downloaded artifacts and other result files.", ) parser.add_argument("--token", default=None, type=str, help="A token that has actions:read permission.") # optional parameters parser.add_argument( "--targets", default="DeprecationWarning,UserWarning,FutureWarning", type=list_str, help="Comma-separated list of target warning(s) which we want to extract.", ) parser.add_argument( "--from_gh", action="store_true", help="If running from a GitHub action workflow and collecting warnings from its artifacts.", ) lowerCamelCase__ : Any = parser.parse_args() lowerCamelCase__ : Tuple = args.from_gh if from_gh: # The artifacts have to be downloaded using `actions/download-artifact@v3` pass else: os.makedirs(args.output_dir, exist_ok=True) # get download links lowerCamelCase__ : Dict = get_artifacts_links(args.workflow_run_id, token=args.token) with open(os.path.join(args.output_dir, "artifacts.json"), "w", encoding="UTF-8") as fp: json.dump(artifacts, fp, ensure_ascii=False, indent=4) # download artifacts for idx, (name, url) in enumerate(artifacts.items()): print(name) print(url) print("=" * 80) download_artifact(name, url, args.output_dir, args.token) # Be gentle to GitHub time.sleep(1) # extract warnings from artifacts lowerCamelCase__ : Union[str, Any] = extract_warnings(args.output_dir, args.targets) lowerCamelCase__ : str = sorted(selected_warnings) with open(os.path.join(args.output_dir, "selected_warnings.json"), "w", encoding="UTF-8") as fp: json.dump(selected_warnings, fp, ensure_ascii=False, indent=4)
704
"""simple docstring""" def __A ( a_ : list , a_ : int , a_ : int = 0 , a_ : int = 0 )-> int: '''simple docstring''' SCREAMING_SNAKE_CASE : str = right or len(a_ ) - 1 if left > right: return -1 elif list_data[left] == key: return left elif list_data[right] == key: return right else: return search(a_ , a_ , left + 1 , right - 1 ) if __name__ == "__main__": import doctest doctest.testmod()
18
0
"""simple docstring""" import gc import unittest from parameterized import parameterized from diffusers import FlaxUNetaDConditionModel from diffusers.utils import is_flax_available from diffusers.utils.testing_utils import load_hf_numpy, require_flax, slow if is_flax_available(): import jax import jax.numpy as jnp @slow @require_flax class lowercase__( unittest.TestCase ): '''simple docstring''' def __lowerCAmelCase ( self :List[str] , lowerCamelCase_ :Union[str, Any] , lowerCamelCase_ :Optional[Any] ) -> str: '''simple docstring''' return f"gaussian_noise_s={seed}_shape={'_'.join([str(__lowerCamelCase ) for s in shape] )}.npy" def __lowerCAmelCase ( self :Tuple ) -> str: '''simple docstring''' super().tearDown() gc.collect() def __lowerCAmelCase ( self :List[str] , lowerCamelCase_ :List[Any]=0 , lowerCamelCase_ :List[Any]=(4, 4, 64, 64) , lowerCamelCase_ :Dict=False ) -> List[str]: '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[int] = jnp.bfloataa if fpaa else jnp.floataa SCREAMING_SNAKE_CASE : Any = jnp.array(load_hf_numpy(self.get_file_format(__lowerCamelCase , __lowerCamelCase ) ) , dtype=__lowerCamelCase ) return image def __lowerCAmelCase ( self :str , lowerCamelCase_ :Dict=False , lowerCamelCase_ :Optional[Any]="CompVis/stable-diffusion-v1-4" ) -> Union[str, Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[int] = jnp.bfloataa if fpaa else jnp.floataa SCREAMING_SNAKE_CASE : Optional[int] = "bf16" if fpaa else None SCREAMING_SNAKE_CASE : Optional[int] = FlaxUNetaDConditionModel.from_pretrained( __lowerCamelCase , subfolder='''unet''' , dtype=__lowerCamelCase , revision=__lowerCamelCase ) return model, params def __lowerCAmelCase ( self :Tuple , lowerCamelCase_ :Tuple=0 , lowerCamelCase_ :List[Any]=(4, 77, 7_68) , lowerCamelCase_ :Dict=False ) -> Union[str, Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : str = jnp.bfloataa if fpaa else jnp.floataa SCREAMING_SNAKE_CASE : Optional[Any] = jnp.array(load_hf_numpy(self.get_file_format(__lowerCamelCase , __lowerCamelCase ) ) , dtype=__lowerCamelCase ) return hidden_states @parameterized.expand( [ # fmt: off [83, 4, [-0.2_3_2_3, -0.1_3_0_4, 0.0_8_1_3, -0.3_0_9_3, -0.0_9_1_9, -0.1_5_7_1, -0.1_1_2_5, -0.5_8_0_6]], [17, 0.5_5, [-0.0_8_3_1, -0.2_4_4_3, 0.0_9_0_1, -0.0_9_1_9, 0.3_3_9_6, 0.0_1_0_3, -0.3_7_4_3, 0.0_7_0_1]], [8, 0.8_9, [-0.4_8_6_3, 0.0_8_5_9, 0.0_8_7_5, -0.1_6_5_8, 0.9_1_9_9, -0.0_1_1_4, 0.4_8_3_9, 0.4_6_3_9]], [3, 10_00, [-0.5_6_4_9, 0.2_4_0_2, -0.5_5_1_8, 0.1_2_4_8, 1.1_3_2_8, -0.2_4_4_3, -0.0_3_2_5, -1.0_0_7_8]], # fmt: on ] ) def __lowerCAmelCase ( self :List[str] , lowerCamelCase_ :Optional[Any] , lowerCamelCase_ :List[str] , lowerCamelCase_ :Optional[Any] ) -> Union[str, Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : Tuple = self.get_unet_model(model_id='''CompVis/stable-diffusion-v1-4''' , fpaa=__lowerCamelCase ) SCREAMING_SNAKE_CASE : int = self.get_latents(__lowerCamelCase , fpaa=__lowerCamelCase ) SCREAMING_SNAKE_CASE : int = self.get_encoder_hidden_states(__lowerCamelCase , fpaa=__lowerCamelCase ) SCREAMING_SNAKE_CASE : int = model.apply( {'''params''': params} , __lowerCamelCase , jnp.array(__lowerCamelCase , dtype=jnp.intaa ) , encoder_hidden_states=__lowerCamelCase , ).sample assert sample.shape == latents.shape SCREAMING_SNAKE_CASE : Optional[int] = jnp.asarray(jax.device_get((sample[-1, -2:, -2:, :2].flatten()) ) , dtype=jnp.floataa ) SCREAMING_SNAKE_CASE : List[Any] = jnp.array(__lowerCamelCase , dtype=jnp.floataa ) # Found torch (float16) and flax (bfloat16) outputs to be within this tolerance, in the same hardware assert jnp.allclose(__lowerCamelCase , __lowerCamelCase , atol=1E-2 ) @parameterized.expand( [ # fmt: off [83, 4, [0.1_5_1_4, 0.0_8_0_7, 0.1_6_2_4, 0.1_0_1_6, -0.1_8_9_6, 0.0_2_6_3, 0.0_6_7_7, 0.2_3_1_0]], [17, 0.5_5, [0.1_1_6_4, -0.0_2_1_6, 0.0_1_7_0, 0.1_5_8_9, -0.3_1_2_0, 0.1_0_0_5, -0.0_5_8_1, -0.1_4_5_8]], [8, 0.8_9, [-0.1_7_5_8, -0.0_1_6_9, 0.1_0_0_4, -0.1_4_1_1, 0.1_3_1_2, 0.1_1_0_3, -0.1_9_9_6, 0.2_1_3_9]], [3, 10_00, [0.1_2_1_4, 0.0_3_5_2, -0.0_7_3_1, -0.1_5_6_2, -0.0_9_9_4, -0.0_9_0_6, -0.2_3_4_0, -0.0_5_3_9]], # fmt: on ] ) def __lowerCAmelCase ( self :Optional[int] , lowerCamelCase_ :int , lowerCamelCase_ :Dict , lowerCamelCase_ :Optional[Any] ) -> List[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : str = self.get_unet_model(model_id='''stabilityai/stable-diffusion-2''' , fpaa=__lowerCamelCase ) SCREAMING_SNAKE_CASE : Dict = self.get_latents(__lowerCamelCase , shape=(4, 4, 96, 96) , fpaa=__lowerCamelCase ) SCREAMING_SNAKE_CASE : str = self.get_encoder_hidden_states(__lowerCamelCase , shape=(4, 77, 10_24) , fpaa=__lowerCamelCase ) SCREAMING_SNAKE_CASE : Union[str, Any] = model.apply( {'''params''': params} , __lowerCamelCase , jnp.array(__lowerCamelCase , dtype=jnp.intaa ) , encoder_hidden_states=__lowerCamelCase , ).sample assert sample.shape == latents.shape SCREAMING_SNAKE_CASE : str = jnp.asarray(jax.device_get((sample[-1, -2:, -2:, :2].flatten()) ) , dtype=jnp.floataa ) SCREAMING_SNAKE_CASE : Optional[Any] = jnp.array(__lowerCamelCase , dtype=jnp.floataa ) # Found torch (float16) and flax (bfloat16) outputs to be within this tolerance, on the same hardware assert jnp.allclose(__lowerCamelCase , __lowerCamelCase , atol=1E-2 )
705
"""simple docstring""" def __A ( a_ : int )-> list[int]: '''simple docstring''' if num <= 0: raise ValueError('''Input must be a positive integer''' ) SCREAMING_SNAKE_CASE : Optional[int] = [True] * (num + 1) SCREAMING_SNAKE_CASE : Optional[Any] = 2 while p * p <= num: if primes[p]: for i in range(p * p , num + 1 , a_ ): SCREAMING_SNAKE_CASE : Any = False p += 1 return [prime for prime in range(2 , num + 1 ) if primes[prime]] if __name__ == "__main__": import doctest doctest.testmod() lowerCamelCase__ : str = int(input("Enter a positive integer: ").strip()) print(prime_sieve_eratosthenes(user_num))
18
0
"""simple docstring""" import math def __A ( a_ : Union[str, Any] , a_ : int )-> int: '''simple docstring''' SCREAMING_SNAKE_CASE : List[str] = len(lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Optional[int] = int(math.floor(math.sqrt(lowerCamelCase_ ) ) ) SCREAMING_SNAKE_CASE : List[str] = 0 while arr[min(lowerCamelCase_ , lowerCamelCase_ ) - 1] < x: SCREAMING_SNAKE_CASE : Tuple = step step += int(math.floor(math.sqrt(lowerCamelCase_ ) ) ) if prev >= n: return -1 while arr[prev] < x: SCREAMING_SNAKE_CASE : int = prev + 1 if prev == min(lowerCamelCase_ , lowerCamelCase_ ): return -1 if arr[prev] == x: return prev return -1 if __name__ == "__main__": lowerCamelCase__ : str = input("Enter numbers separated by a comma:\n").strip() lowerCamelCase__ : Union[str, Any] = [int(item) for item in user_input.split(",")] lowerCamelCase__ : str = int(input("Enter the number to be searched:\n")) lowerCamelCase__ : Union[str, Any] = jump_search(arr, x) if res == -1: print("Number not found!") else: print(f'''Number {x} is at index {res}''')
706
"""simple docstring""" from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_tf_available, is_tokenizers_available, is_torch_available, ) lowerCamelCase__ : Optional[Any] = { "configuration_funnel": ["FUNNEL_PRETRAINED_CONFIG_ARCHIVE_MAP", "FunnelConfig"], "convert_funnel_original_tf_checkpoint_to_pytorch": [], "tokenization_funnel": ["FunnelTokenizer"], } try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCamelCase__ : Optional[Any] = ["FunnelTokenizerFast"] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCamelCase__ : Dict = [ "FUNNEL_PRETRAINED_MODEL_ARCHIVE_LIST", "FunnelBaseModel", "FunnelForMaskedLM", "FunnelForMultipleChoice", "FunnelForPreTraining", "FunnelForQuestionAnswering", "FunnelForSequenceClassification", "FunnelForTokenClassification", "FunnelModel", "FunnelPreTrainedModel", "load_tf_weights_in_funnel", ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCamelCase__ : Tuple = [ "TF_FUNNEL_PRETRAINED_MODEL_ARCHIVE_LIST", "TFFunnelBaseModel", "TFFunnelForMaskedLM", "TFFunnelForMultipleChoice", "TFFunnelForPreTraining", "TFFunnelForQuestionAnswering", "TFFunnelForSequenceClassification", "TFFunnelForTokenClassification", "TFFunnelModel", "TFFunnelPreTrainedModel", ] if TYPE_CHECKING: from .configuration_funnel import FUNNEL_PRETRAINED_CONFIG_ARCHIVE_MAP, FunnelConfig from .tokenization_funnel import FunnelTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_funnel_fast import FunnelTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_funnel import ( FUNNEL_PRETRAINED_MODEL_ARCHIVE_LIST, FunnelBaseModel, FunnelForMaskedLM, FunnelForMultipleChoice, FunnelForPreTraining, FunnelForQuestionAnswering, FunnelForSequenceClassification, FunnelForTokenClassification, FunnelModel, FunnelPreTrainedModel, load_tf_weights_in_funnel, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_funnel import ( TF_FUNNEL_PRETRAINED_MODEL_ARCHIVE_LIST, TFFunnelBaseModel, TFFunnelForMaskedLM, TFFunnelForMultipleChoice, TFFunnelForPreTraining, TFFunnelForQuestionAnswering, TFFunnelForSequenceClassification, TFFunnelForTokenClassification, TFFunnelModel, TFFunnelPreTrainedModel, ) else: import sys lowerCamelCase__ : Optional[int] = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
18
0
"""simple docstring""" import itertools import string from collections.abc import Generator, Iterable def __A ( a_ : Iterable[str] , a_ : int )-> Generator[tuple[str, ...], None, None]: '''simple docstring''' SCREAMING_SNAKE_CASE : Union[str, Any] = iter(a_ ) while True: SCREAMING_SNAKE_CASE : Optional[int] = tuple(itertools.islice(a_ , a_ ) ) if not chunk: return yield chunk def __A ( a_ : str )-> str: '''simple docstring''' SCREAMING_SNAKE_CASE : int = ''.join([c.upper() for c in dirty if c in string.ascii_letters] ) SCREAMING_SNAKE_CASE : int = '' if len(a_ ) < 2: return dirty for i in range(len(a_ ) - 1 ): clean += dirty[i] if dirty[i] == dirty[i + 1]: clean += "X" clean += dirty[-1] if len(a_ ) & 1: clean += "X" return clean def __A ( a_ : str )-> list[str]: '''simple docstring''' SCREAMING_SNAKE_CASE : Tuple = 'ABCDEFGHIKLMNOPQRSTUVWXYZ' # we're using a list instead of a '2d' array because it makes the math # for setting up the table and doing the actual encoding/decoding simpler SCREAMING_SNAKE_CASE : Any = [] # copy key chars into the table if they are in `alphabet` ignoring duplicates for char in key.upper(): if char not in table and char in alphabet: table.append(a_ ) # fill the rest of the table in with the remaining alphabet chars for char in alphabet: if char not in table: table.append(a_ ) return table def __A ( a_ : str , a_ : str )-> str: '''simple docstring''' SCREAMING_SNAKE_CASE : List[str] = generate_table(a_ ) SCREAMING_SNAKE_CASE : Optional[Any] = prepare_input(a_ ) SCREAMING_SNAKE_CASE : Any = '' # https://en.wikipedia.org/wiki/Playfair_cipher#Description for chara, chara in chunker(a_ , 2 ): SCREAMING_SNAKE_CASE : Tuple = divmod(table.index(a_ ) , 5 ) SCREAMING_SNAKE_CASE : List[Any] = divmod(table.index(a_ ) , 5 ) if rowa == rowa: ciphertext += table[rowa * 5 + (cola + 1) % 5] ciphertext += table[rowa * 5 + (cola + 1) % 5] elif cola == cola: ciphertext += table[((rowa + 1) % 5) * 5 + cola] ciphertext += table[((rowa + 1) % 5) * 5 + cola] else: # rectangle ciphertext += table[rowa * 5 + cola] ciphertext += table[rowa * 5 + cola] return ciphertext def __A ( a_ : str , a_ : str )-> str: '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[int] = generate_table(a_ ) SCREAMING_SNAKE_CASE : int = '' # https://en.wikipedia.org/wiki/Playfair_cipher#Description for chara, chara in chunker(a_ , 2 ): SCREAMING_SNAKE_CASE : Union[str, Any] = divmod(table.index(a_ ) , 5 ) SCREAMING_SNAKE_CASE : str = divmod(table.index(a_ ) , 5 ) if rowa == rowa: plaintext += table[rowa * 5 + (cola - 1) % 5] plaintext += table[rowa * 5 + (cola - 1) % 5] elif cola == cola: plaintext += table[((rowa - 1) % 5) * 5 + cola] plaintext += table[((rowa - 1) % 5) * 5 + cola] else: # rectangle plaintext += table[rowa * 5 + cola] plaintext += table[rowa * 5 + cola] return plaintext
707
"""simple docstring""" import os import sys lowerCamelCase__ : List[Any] = os.path.join(os.path.dirname(__file__), "src") sys.path.append(SRC_DIR) from transformers import ( AutoConfig, AutoModel, AutoModelForCausalLM, AutoModelForMaskedLM, AutoModelForQuestionAnswering, AutoModelForSequenceClassification, AutoTokenizer, add_start_docstrings, ) lowerCamelCase__ : str = [ "torch", "numpy", "tokenizers", "filelock", "requests", "tqdm", "regex", "sentencepiece", "sacremoses", "importlib_metadata", "huggingface_hub", ] @add_start_docstrings(AutoConfig.__doc__ ) def __A ( *a_ : Any , **a_ : Union[str, Any] )-> Dict: '''simple docstring''' return AutoConfig.from_pretrained(*a_ , **a_ ) @add_start_docstrings(AutoTokenizer.__doc__ ) def __A ( *a_ : str , **a_ : Union[str, Any] )-> Union[str, Any]: '''simple docstring''' return AutoTokenizer.from_pretrained(*a_ , **a_ ) @add_start_docstrings(AutoModel.__doc__ ) def __A ( *a_ : List[str] , **a_ : int )-> Dict: '''simple docstring''' return AutoModel.from_pretrained(*a_ , **a_ ) @add_start_docstrings(AutoModelForCausalLM.__doc__ ) def __A ( *a_ : Any , **a_ : Tuple )-> Dict: '''simple docstring''' return AutoModelForCausalLM.from_pretrained(*a_ , **a_ ) @add_start_docstrings(AutoModelForMaskedLM.__doc__ ) def __A ( *a_ : Dict , **a_ : Optional[Any] )-> Optional[int]: '''simple docstring''' return AutoModelForMaskedLM.from_pretrained(*a_ , **a_ ) @add_start_docstrings(AutoModelForSequenceClassification.__doc__ ) def __A ( *a_ : Optional[int] , **a_ : str )-> Optional[int]: '''simple docstring''' return AutoModelForSequenceClassification.from_pretrained(*a_ , **a_ ) @add_start_docstrings(AutoModelForQuestionAnswering.__doc__ ) def __A ( *a_ : List[str] , **a_ : int )-> List[Any]: '''simple docstring''' return AutoModelForQuestionAnswering.from_pretrained(*a_ , **a_ )
18
0
import unittest import torch from diffusers import DDIMScheduler, DDPMScheduler, UNetaDModel from diffusers.training_utils import set_seed from diffusers.utils.testing_utils import slow lowerCamelCase__ : Any = False class lowercase__( unittest.TestCase ): '''simple docstring''' def __lowerCAmelCase ( self :str , lowerCamelCase_ :Optional[Any]=32 ) -> List[str]: '''simple docstring''' set_seed(0 ) SCREAMING_SNAKE_CASE : str = UNetaDModel(sample_size=lowerCamelCase_ , in_channels=3 , out_channels=3 ) SCREAMING_SNAKE_CASE : int = torch.optim.SGD(model.parameters() , lr=0.0_0_0_1 ) return model, optimizer @slow def __lowerCAmelCase ( self :int ) -> Any: '''simple docstring''' SCREAMING_SNAKE_CASE : int = '''cpu''' # ensure full determinism without setting the CUBLAS_WORKSPACE_CONFIG env variable SCREAMING_SNAKE_CASE : Tuple = DDPMScheduler( num_train_timesteps=10_00 , beta_start=0.0_0_0_1 , beta_end=0.0_2 , beta_schedule='''linear''' , clip_sample=lowerCamelCase_ , ) SCREAMING_SNAKE_CASE : Union[str, Any] = DDIMScheduler( num_train_timesteps=10_00 , beta_start=0.0_0_0_1 , beta_end=0.0_2 , beta_schedule='''linear''' , clip_sample=lowerCamelCase_ , ) assert ddpm_scheduler.config.num_train_timesteps == ddim_scheduler.config.num_train_timesteps # shared batches for DDPM and DDIM set_seed(0 ) SCREAMING_SNAKE_CASE : Any = [torch.randn((4, 3, 32, 32) ).clip(-1 , 1 ).to(lowerCamelCase_ ) for _ in range(4 )] SCREAMING_SNAKE_CASE : Tuple = [torch.randn((4, 3, 32, 32) ).to(lowerCamelCase_ ) for _ in range(4 )] SCREAMING_SNAKE_CASE : str = [torch.randint(0 , 10_00 , (4,) ).long().to(lowerCamelCase_ ) for _ in range(4 )] # train with a DDPM scheduler SCREAMING_SNAKE_CASE : List[str] = self.get_model_optimizer(resolution=32 ) model.train().to(lowerCamelCase_ ) for i in range(4 ): optimizer.zero_grad() SCREAMING_SNAKE_CASE : Any = ddpm_scheduler.add_noise(clean_images[i] , noise[i] , timesteps[i] ) SCREAMING_SNAKE_CASE : List[Any] = model(lowerCamelCase_ , timesteps[i] ).sample SCREAMING_SNAKE_CASE : Any = torch.nn.functional.mse_loss(lowerCamelCase_ , noise[i] ) loss.backward() optimizer.step() del model, optimizer # recreate the model and optimizer, and retry with DDIM SCREAMING_SNAKE_CASE : Union[str, Any] = self.get_model_optimizer(resolution=32 ) model.train().to(lowerCamelCase_ ) for i in range(4 ): optimizer.zero_grad() SCREAMING_SNAKE_CASE : Dict = ddim_scheduler.add_noise(clean_images[i] , noise[i] , timesteps[i] ) SCREAMING_SNAKE_CASE : Any = model(lowerCamelCase_ , timesteps[i] ).sample SCREAMING_SNAKE_CASE : Optional[Any] = torch.nn.functional.mse_loss(lowerCamelCase_ , noise[i] ) loss.backward() optimizer.step() del model, optimizer self.assertTrue(torch.allclose(lowerCamelCase_ , lowerCamelCase_ , atol=1E-5 ) ) self.assertTrue(torch.allclose(lowerCamelCase_ , lowerCamelCase_ , atol=1E-5 ) )
708
"""simple docstring""" import math from typing import Optional import numpy as np from ...configuration_utils import PretrainedConfig from ...utils import logging lowerCamelCase__ : Tuple = logging.get_logger(__name__) lowerCamelCase__ : Any = { "facebook/encodec_24khz": "https://huggingface.co/facebook/encodec_24khz/resolve/main/config.json", "facebook/encodec_48khz": "https://huggingface.co/facebook/encodec_48khz/resolve/main/config.json", } class lowercase__( _UpperCAmelCase ): '''simple docstring''' UpperCamelCase = """encodec""" def __init__( self :List[str] , lowerCamelCase_ :Tuple=[1.5, 3.0, 6.0, 1_2.0, 2_4.0] , lowerCamelCase_ :str=2_40_00 , lowerCamelCase_ :Any=1 , lowerCamelCase_ :List[Any]=False , lowerCamelCase_ :Optional[int]=None , lowerCamelCase_ :Optional[Any]=None , lowerCamelCase_ :str=1_28 , lowerCamelCase_ :Any=32 , lowerCamelCase_ :int=1 , lowerCamelCase_ :Dict=[8, 5, 4, 2] , lowerCamelCase_ :List[Any]="weight_norm" , lowerCamelCase_ :Optional[int]=7 , lowerCamelCase_ :Tuple=7 , lowerCamelCase_ :Optional[Any]=3 , lowerCamelCase_ :int=2 , lowerCamelCase_ :Dict=True , lowerCamelCase_ :Optional[int]="reflect" , lowerCamelCase_ :Optional[int]=2 , lowerCamelCase_ :Union[str, Any]=2 , lowerCamelCase_ :Dict=1.0 , lowerCamelCase_ :Any=10_24 , lowerCamelCase_ :str=None , lowerCamelCase_ :Union[str, Any]=True , **lowerCamelCase_ :Optional[int] , ) -> Dict: '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[Any] = target_bandwidths SCREAMING_SNAKE_CASE : List[str] = sampling_rate SCREAMING_SNAKE_CASE : Tuple = audio_channels SCREAMING_SNAKE_CASE : Tuple = normalize SCREAMING_SNAKE_CASE : str = chunk_length_s SCREAMING_SNAKE_CASE : List[str] = overlap SCREAMING_SNAKE_CASE : int = hidden_size SCREAMING_SNAKE_CASE : Optional[int] = num_filters SCREAMING_SNAKE_CASE : Tuple = num_residual_layers SCREAMING_SNAKE_CASE : List[Any] = upsampling_ratios SCREAMING_SNAKE_CASE : Optional[int] = norm_type SCREAMING_SNAKE_CASE : Any = kernel_size SCREAMING_SNAKE_CASE : Union[str, Any] = last_kernel_size SCREAMING_SNAKE_CASE : Tuple = residual_kernel_size SCREAMING_SNAKE_CASE : Any = dilation_growth_rate SCREAMING_SNAKE_CASE : Optional[int] = use_causal_conv SCREAMING_SNAKE_CASE : str = pad_mode SCREAMING_SNAKE_CASE : List[Any] = compress SCREAMING_SNAKE_CASE : Optional[Any] = num_lstm_layers SCREAMING_SNAKE_CASE : Dict = trim_right_ratio SCREAMING_SNAKE_CASE : List[Any] = codebook_size SCREAMING_SNAKE_CASE : Union[str, Any] = codebook_dim if codebook_dim is not None else hidden_size SCREAMING_SNAKE_CASE : Union[str, Any] = use_conv_shortcut if self.norm_type not in ["weight_norm", "time_group_norm"]: raise ValueError( f"self.norm_type must be one of `\"weight_norm\"`, `\"time_group_norm\"`), got {self.norm_type}" ) super().__init__(**lowerCamelCase_ ) @property def __lowerCAmelCase ( self :Optional[Any] ) -> Optional[int]: '''simple docstring''' if self.chunk_length_s is None: return None else: return int(self.chunk_length_s * self.sampling_rate ) @property def __lowerCAmelCase ( self :List[str] ) -> Optional[int]: '''simple docstring''' if self.chunk_length_s is None or self.overlap is None: return None else: return max(1 , int((1.0 - self.overlap) * self.chunk_length ) ) @property def __lowerCAmelCase ( self :Dict ) -> int: '''simple docstring''' SCREAMING_SNAKE_CASE : Tuple = np.prod(self.upsampling_ratios ) return math.ceil(self.sampling_rate / hop_length ) @property def __lowerCAmelCase ( self :Dict ) -> int: '''simple docstring''' return int(10_00 * self.target_bandwidths[-1] // (self.frame_rate * 10) )
18
0
"""simple docstring""" import numpy # List of input, output pairs lowerCamelCase__ : List[Any] = ( ((5, 2, 3), 15), ((6, 5, 9), 25), ((11, 12, 13), 41), ((1, 1, 1), 8), ((11, 12, 13), 41), ) lowerCamelCase__ : Optional[int] = (((515, 22, 13), 555), ((61, 35, 49), 150)) lowerCamelCase__ : int = [2, 4, 1, 5] lowerCamelCase__ : int = len(train_data) lowerCamelCase__ : Dict = 0.0_0_9 def __A ( a_ : int , a_ : str="train" )-> Optional[int]: '''simple docstring''' return calculate_hypothesis_value(__lowerCAmelCase , __lowerCAmelCase ) - output( __lowerCAmelCase , __lowerCAmelCase ) def __A ( a_ : Any )-> Union[str, Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : Dict = 0 for i in range(len(__lowerCAmelCase ) - 1 ): hyp_val += data_input_tuple[i] * parameter_vector[i + 1] hyp_val += parameter_vector[0] return hyp_val def __A ( a_ : Optional[Any] , a_ : Tuple )-> Optional[int]: '''simple docstring''' if data_set == "train": return train_data[example_no][1] elif data_set == "test": return test_data[example_no][1] return None def __A ( a_ : int , a_ : Dict )-> Optional[int]: '''simple docstring''' if data_set == "train": return _hypothesis_value(train_data[example_no][0] ) elif data_set == "test": return _hypothesis_value(test_data[example_no][0] ) return None def __A ( a_ : List[Any] , a_ : Optional[Any]=m )-> Union[str, Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : str = 0 for i in range(__lowerCAmelCase ): if index == -1: summation_value += _error(__lowerCAmelCase ) else: summation_value += _error(__lowerCAmelCase ) * train_data[i][0][index] return summation_value def __A ( a_ : List[Any] )-> Union[str, Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : Union[str, Any] = summation_of_cost_derivative(__lowerCAmelCase , __lowerCAmelCase ) / m return cost_derivative_value def __A ( )-> Optional[int]: '''simple docstring''' global parameter_vector # Tune these values to set a tolerance value for predicted output SCREAMING_SNAKE_CASE : Optional[Any] = 0.00_0002 SCREAMING_SNAKE_CASE : Any = 0 SCREAMING_SNAKE_CASE : List[str] = 0 while True: j += 1 SCREAMING_SNAKE_CASE : Tuple = [0, 0, 0, 0] for i in range(0 , len(__lowerCAmelCase ) ): SCREAMING_SNAKE_CASE : str = get_cost_derivative(i - 1 ) SCREAMING_SNAKE_CASE : Optional[Any] = ( parameter_vector[i] - LEARNING_RATE * cost_derivative ) if numpy.allclose( __lowerCAmelCase , __lowerCAmelCase , atol=__lowerCAmelCase , rtol=__lowerCAmelCase , ): break SCREAMING_SNAKE_CASE : Tuple = temp_parameter_vector print(('''Number of iterations:''', j) ) def __A ( )-> Tuple: '''simple docstring''' for i in range(len(__lowerCAmelCase ) ): print(('''Actual output value:''', output(__lowerCAmelCase , '''test''' )) ) print(('''Hypothesis output:''', calculate_hypothesis_value(__lowerCAmelCase , '''test''' )) ) if __name__ == "__main__": run_gradient_descent() print("\nTesting gradient descent for a linear hypothesis function.\n") test_gradient_descent()
709
"""simple docstring""" import copy import unittest from transformers.models.auto import get_values from transformers.testing_utils import require_torch, 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, random_attention_mask from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from transformers import ( MODEL_FOR_MULTIPLE_CHOICE_MAPPING, MODEL_FOR_QUESTION_ANSWERING_MAPPING, MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING, MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING, LayoutLMvaConfig, LayoutLMvaForQuestionAnswering, LayoutLMvaForSequenceClassification, LayoutLMvaForTokenClassification, LayoutLMvaModel, ) from transformers.models.layoutlmva.modeling_layoutlmva import LAYOUTLMV3_PRETRAINED_MODEL_ARCHIVE_LIST if is_vision_available(): from PIL import Image from transformers import LayoutLMvaImageProcessor class lowercase__: '''simple docstring''' def __init__( self :Tuple , lowerCamelCase_ :Tuple , lowerCamelCase_ :Union[str, Any]=2 , lowerCamelCase_ :Any=3 , lowerCamelCase_ :Union[str, Any]=4 , lowerCamelCase_ :List[str]=2 , lowerCamelCase_ :str=7 , lowerCamelCase_ :Tuple=True , lowerCamelCase_ :Dict=True , lowerCamelCase_ :int=True , lowerCamelCase_ :List[Any]=True , lowerCamelCase_ :Optional[Any]=99 , lowerCamelCase_ :Any=36 , lowerCamelCase_ :Any=3 , lowerCamelCase_ :str=4 , lowerCamelCase_ :Tuple=37 , lowerCamelCase_ :Optional[int]="gelu" , lowerCamelCase_ :str=0.1 , lowerCamelCase_ :Optional[int]=0.1 , lowerCamelCase_ :Tuple=5_12 , lowerCamelCase_ :Optional[Any]=16 , lowerCamelCase_ :List[str]=2 , lowerCamelCase_ :Optional[int]=0.0_2 , lowerCamelCase_ :int=6 , lowerCamelCase_ :str=6 , lowerCamelCase_ :Optional[Any]=3 , lowerCamelCase_ :Union[str, Any]=4 , lowerCamelCase_ :List[Any]=None , lowerCamelCase_ :Tuple=10_00 , ) -> Union[str, Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : Dict = parent SCREAMING_SNAKE_CASE : Optional[Any] = batch_size SCREAMING_SNAKE_CASE : List[str] = num_channels SCREAMING_SNAKE_CASE : str = image_size SCREAMING_SNAKE_CASE : Optional[int] = patch_size SCREAMING_SNAKE_CASE : Tuple = text_seq_length SCREAMING_SNAKE_CASE : Optional[int] = is_training SCREAMING_SNAKE_CASE : Dict = use_input_mask SCREAMING_SNAKE_CASE : Any = use_token_type_ids SCREAMING_SNAKE_CASE : List[Any] = use_labels SCREAMING_SNAKE_CASE : List[Any] = vocab_size SCREAMING_SNAKE_CASE : Optional[Any] = hidden_size SCREAMING_SNAKE_CASE : Tuple = num_hidden_layers SCREAMING_SNAKE_CASE : Optional[int] = num_attention_heads SCREAMING_SNAKE_CASE : Optional[int] = intermediate_size SCREAMING_SNAKE_CASE : List[str] = hidden_act SCREAMING_SNAKE_CASE : Any = hidden_dropout_prob SCREAMING_SNAKE_CASE : Optional[Any] = attention_probs_dropout_prob SCREAMING_SNAKE_CASE : Union[str, Any] = max_position_embeddings SCREAMING_SNAKE_CASE : int = type_vocab_size SCREAMING_SNAKE_CASE : Union[str, Any] = type_sequence_label_size SCREAMING_SNAKE_CASE : Optional[Any] = initializer_range SCREAMING_SNAKE_CASE : Dict = coordinate_size SCREAMING_SNAKE_CASE : List[Any] = shape_size SCREAMING_SNAKE_CASE : Dict = num_labels SCREAMING_SNAKE_CASE : Union[str, Any] = num_choices SCREAMING_SNAKE_CASE : List[str] = scope SCREAMING_SNAKE_CASE : Optional[int] = range_bbox # LayoutLMv3's sequence length equals the number of text tokens + number of patches + 1 (we add 1 for the CLS token) SCREAMING_SNAKE_CASE : str = text_seq_length SCREAMING_SNAKE_CASE : int = (image_size // patch_size) ** 2 + 1 SCREAMING_SNAKE_CASE : Optional[Any] = self.text_seq_length + self.image_seq_length def __lowerCAmelCase ( self :List[Any] ) -> Optional[int]: '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[int] = ids_tensor([self.batch_size, self.text_seq_length] , self.vocab_size ) SCREAMING_SNAKE_CASE : List[Any] = ids_tensor([self.batch_size, self.text_seq_length, 4] , self.range_bbox ) # Ensure that bbox is legal for i in range(bbox.shape[0] ): for j in range(bbox.shape[1] ): if bbox[i, j, 3] < bbox[i, j, 1]: SCREAMING_SNAKE_CASE : str = bbox[i, j, 3] SCREAMING_SNAKE_CASE : List[str] = bbox[i, j, 1] SCREAMING_SNAKE_CASE : Any = t if bbox[i, j, 2] < bbox[i, j, 0]: SCREAMING_SNAKE_CASE : Any = bbox[i, j, 2] SCREAMING_SNAKE_CASE : Any = bbox[i, j, 0] SCREAMING_SNAKE_CASE : Optional[Any] = t SCREAMING_SNAKE_CASE : Dict = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) SCREAMING_SNAKE_CASE : Optional[Any] = None if self.use_input_mask: SCREAMING_SNAKE_CASE : Dict = random_attention_mask([self.batch_size, self.text_seq_length] ) SCREAMING_SNAKE_CASE : Any = None if self.use_token_type_ids: SCREAMING_SNAKE_CASE : Tuple = ids_tensor([self.batch_size, self.text_seq_length] , self.type_vocab_size ) SCREAMING_SNAKE_CASE : Tuple = None SCREAMING_SNAKE_CASE : Optional[int] = None if self.use_labels: SCREAMING_SNAKE_CASE : List[str] = ids_tensor([self.batch_size] , self.type_sequence_label_size ) SCREAMING_SNAKE_CASE : Dict = ids_tensor([self.batch_size, self.text_seq_length] , self.num_labels ) SCREAMING_SNAKE_CASE : List[str] = 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 __lowerCAmelCase ( self :int , lowerCamelCase_ :Union[str, Any] , lowerCamelCase_ :int , lowerCamelCase_ :str , lowerCamelCase_ :int , lowerCamelCase_ :Union[str, Any] , lowerCamelCase_ :Any , lowerCamelCase_ :Dict , lowerCamelCase_ :Union[str, Any] ) -> Any: '''simple docstring''' SCREAMING_SNAKE_CASE : Dict = LayoutLMvaModel(config=lowerCamelCase_ ) model.to(lowerCamelCase_ ) model.eval() # text + image SCREAMING_SNAKE_CASE : Optional[Any] = model(lowerCamelCase_ , pixel_values=lowerCamelCase_ ) SCREAMING_SNAKE_CASE : List[str] = model( lowerCamelCase_ , bbox=lowerCamelCase_ , pixel_values=lowerCamelCase_ , attention_mask=lowerCamelCase_ , token_type_ids=lowerCamelCase_ ) SCREAMING_SNAKE_CASE : List[Any] = model(lowerCamelCase_ , bbox=lowerCamelCase_ , pixel_values=lowerCamelCase_ , token_type_ids=lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Optional[Any] = model(lowerCamelCase_ , bbox=lowerCamelCase_ , pixel_values=lowerCamelCase_ ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) # text only SCREAMING_SNAKE_CASE : List[str] = model(lowerCamelCase_ ) self.parent.assertEqual( result.last_hidden_state.shape , (self.batch_size, self.text_seq_length, self.hidden_size) ) # image only SCREAMING_SNAKE_CASE : List[str] = model(pixel_values=lowerCamelCase_ ) self.parent.assertEqual( result.last_hidden_state.shape , (self.batch_size, self.image_seq_length, self.hidden_size) ) def __lowerCAmelCase ( self :str , lowerCamelCase_ :Union[str, Any] , lowerCamelCase_ :Union[str, Any] , lowerCamelCase_ :Tuple , lowerCamelCase_ :List[str] , lowerCamelCase_ :Any , lowerCamelCase_ :int , lowerCamelCase_ :List[Any] , lowerCamelCase_ :List[str] ) -> Tuple: '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[Any] = self.num_labels SCREAMING_SNAKE_CASE : Dict = LayoutLMvaForSequenceClassification(lowerCamelCase_ ) model.to(lowerCamelCase_ ) model.eval() SCREAMING_SNAKE_CASE : int = model( lowerCamelCase_ , bbox=lowerCamelCase_ , pixel_values=lowerCamelCase_ , attention_mask=lowerCamelCase_ , token_type_ids=lowerCamelCase_ , labels=lowerCamelCase_ , ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_labels) ) def __lowerCAmelCase ( self :Union[str, Any] , lowerCamelCase_ :Tuple , lowerCamelCase_ :Any , lowerCamelCase_ :Optional[int] , lowerCamelCase_ :Any , lowerCamelCase_ :Optional[int] , lowerCamelCase_ :int , lowerCamelCase_ :str , lowerCamelCase_ :Any ) -> Union[str, Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : int = self.num_labels SCREAMING_SNAKE_CASE : int = LayoutLMvaForTokenClassification(config=lowerCamelCase_ ) model.to(lowerCamelCase_ ) model.eval() SCREAMING_SNAKE_CASE : Optional[Any] = model( lowerCamelCase_ , bbox=lowerCamelCase_ , pixel_values=lowerCamelCase_ , attention_mask=lowerCamelCase_ , token_type_ids=lowerCamelCase_ , labels=lowerCamelCase_ , ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.text_seq_length, self.num_labels) ) def __lowerCAmelCase ( self :Optional[Any] , lowerCamelCase_ :Dict , lowerCamelCase_ :Dict , lowerCamelCase_ :Dict , lowerCamelCase_ :int , lowerCamelCase_ :List[str] , lowerCamelCase_ :int , lowerCamelCase_ :str , lowerCamelCase_ :Optional[int] ) -> Union[str, Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[int] = LayoutLMvaForQuestionAnswering(config=lowerCamelCase_ ) model.to(lowerCamelCase_ ) model.eval() SCREAMING_SNAKE_CASE : List[Any] = model( lowerCamelCase_ , bbox=lowerCamelCase_ , pixel_values=lowerCamelCase_ , attention_mask=lowerCamelCase_ , token_type_ids=lowerCamelCase_ , start_positions=lowerCamelCase_ , end_positions=lowerCamelCase_ , ) self.parent.assertEqual(result.start_logits.shape , (self.batch_size, self.seq_length) ) self.parent.assertEqual(result.end_logits.shape , (self.batch_size, self.seq_length) ) def __lowerCAmelCase ( self :Optional[int] ) -> Optional[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[int] = self.prepare_config_and_inputs() ( ( SCREAMING_SNAKE_CASE ), ( SCREAMING_SNAKE_CASE ), ( SCREAMING_SNAKE_CASE ), ( SCREAMING_SNAKE_CASE ), ( SCREAMING_SNAKE_CASE ), ( SCREAMING_SNAKE_CASE ), ( SCREAMING_SNAKE_CASE ), ( SCREAMING_SNAKE_CASE ), ) : Tuple = config_and_inputs SCREAMING_SNAKE_CASE : Dict = { '''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_torch class lowercase__( _UpperCAmelCase , _UpperCAmelCase , unittest.TestCase ): '''simple docstring''' UpperCamelCase = False UpperCamelCase = False UpperCamelCase = False UpperCamelCase = ( ( LayoutLMvaModel, LayoutLMvaForSequenceClassification, LayoutLMvaForTokenClassification, LayoutLMvaForQuestionAnswering, ) if is_torch_available() else () ) UpperCamelCase = ( {"""document-question-answering""": LayoutLMvaForQuestionAnswering, """feature-extraction""": LayoutLMvaModel} if is_torch_available() else {} ) def __lowerCAmelCase ( self :int , lowerCamelCase_ :Union[str, Any] , lowerCamelCase_ :str , lowerCamelCase_ :str , lowerCamelCase_ :Optional[Any] , lowerCamelCase_ :List[str] ) -> Union[str, Any]: '''simple docstring''' return True def __lowerCAmelCase ( self :List[str] ) -> Union[str, Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : str = LayoutLMvaModelTester(self ) SCREAMING_SNAKE_CASE : List[Any] = ConfigTester(self , config_class=lowerCamelCase_ , hidden_size=37 ) def __lowerCAmelCase ( self :Any , lowerCamelCase_ :Dict , lowerCamelCase_ :List[str] , lowerCamelCase_ :str=False ) -> Tuple: '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[Any] = copy.deepcopy(lowerCamelCase_ ) if model_class in get_values(lowerCamelCase_ ): SCREAMING_SNAKE_CASE : Tuple = { k: v.unsqueeze(1 ).expand(-1 , self.model_tester.num_choices , -1 ).contiguous() if isinstance(lowerCamelCase_ , torch.Tensor ) and v.ndim > 1 else v for k, v in inputs_dict.items() } if return_labels: if model_class in get_values(lowerCamelCase_ ): SCREAMING_SNAKE_CASE : Union[str, Any] = torch.ones(self.model_tester.batch_size , dtype=torch.long , device=lowerCamelCase_ ) elif model_class in get_values(lowerCamelCase_ ): SCREAMING_SNAKE_CASE : List[Any] = torch.zeros( self.model_tester.batch_size , dtype=torch.long , device=lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Any = torch.zeros( self.model_tester.batch_size , dtype=torch.long , device=lowerCamelCase_ ) elif model_class in [ *get_values(lowerCamelCase_ ), ]: SCREAMING_SNAKE_CASE : str = torch.zeros( self.model_tester.batch_size , dtype=torch.long , device=lowerCamelCase_ ) elif model_class in [ *get_values(lowerCamelCase_ ), ]: SCREAMING_SNAKE_CASE : Dict = torch.zeros( (self.model_tester.batch_size, self.model_tester.text_seq_length) , dtype=torch.long , device=lowerCamelCase_ , ) return inputs_dict def __lowerCAmelCase ( self :Optional[Any] ) -> Optional[int]: '''simple docstring''' self.config_tester.run_common_tests() def __lowerCAmelCase ( self :str ) -> List[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : int = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*lowerCamelCase_ ) def __lowerCAmelCase ( self :Any ) -> List[str]: '''simple docstring''' SCREAMING_SNAKE_CASE : int = self.model_tester.prepare_config_and_inputs() for type in ["absolute", "relative_key", "relative_key_query"]: SCREAMING_SNAKE_CASE : str = type self.model_tester.create_and_check_model(*lowerCamelCase_ ) def __lowerCAmelCase ( self :Union[str, Any] ) -> str: '''simple docstring''' SCREAMING_SNAKE_CASE : List[Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_sequence_classification(*lowerCamelCase_ ) def __lowerCAmelCase ( self :str ) -> Any: '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_token_classification(*lowerCamelCase_ ) def __lowerCAmelCase ( self :List[str] ) -> Union[str, Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : int = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_question_answering(*lowerCamelCase_ ) @slow def __lowerCAmelCase ( self :int ) -> Union[str, Any]: '''simple docstring''' for model_name in LAYOUTLMV3_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: SCREAMING_SNAKE_CASE : Optional[int] = LayoutLMvaModel.from_pretrained(lowerCamelCase_ ) self.assertIsNotNone(lowerCamelCase_ ) def __A ( )-> Optional[int]: '''simple docstring''' SCREAMING_SNAKE_CASE : Tuple = Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png''' ) return image @require_torch class lowercase__( unittest.TestCase ): '''simple docstring''' @cached_property def __lowerCAmelCase ( self :str ) -> int: '''simple docstring''' return LayoutLMvaImageProcessor(apply_ocr=lowerCamelCase_ ) if is_vision_available() else None @slow def __lowerCAmelCase ( self :Optional[Any] ) -> Dict: '''simple docstring''' SCREAMING_SNAKE_CASE : Union[str, Any] = LayoutLMvaModel.from_pretrained('''microsoft/layoutlmv3-base''' ).to(lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Any = self.default_image_processor SCREAMING_SNAKE_CASE : List[Any] = prepare_img() SCREAMING_SNAKE_CASE : Tuple = image_processor(images=lowerCamelCase_ , return_tensors='''pt''' ).pixel_values.to(lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Optional[Any] = torch.tensor([[1, 2]] ) SCREAMING_SNAKE_CASE : Dict = torch.tensor([[1, 2, 3, 4], [5, 6, 7, 8]] ).unsqueeze(0 ) # forward pass SCREAMING_SNAKE_CASE : Tuple = model( input_ids=input_ids.to(lowerCamelCase_ ) , bbox=bbox.to(lowerCamelCase_ ) , pixel_values=pixel_values.to(lowerCamelCase_ ) , ) # verify the logits SCREAMING_SNAKE_CASE : Union[str, Any] = torch.Size((1, 1_99, 7_68) ) self.assertEqual(outputs.last_hidden_state.shape , lowerCamelCase_ ) SCREAMING_SNAKE_CASE : List[Any] = torch.tensor( [[-0.0_5_2_9, 0.3_6_1_8, 0.1_6_3_2], [-0.1_5_8_7, -0.1_6_6_7, -0.0_4_0_0], [-0.1_5_5_7, -0.1_6_7_1, -0.0_5_0_5]] ).to(lowerCamelCase_ ) self.assertTrue(torch.allclose(outputs.last_hidden_state[0, :3, :3] , lowerCamelCase_ , atol=1E-4 ) )
18
0
"""simple docstring""" def __A ( a_ : Any = 10**12 )-> int: '''simple docstring''' SCREAMING_SNAKE_CASE : Any = 1 SCREAMING_SNAKE_CASE : int = 0 SCREAMING_SNAKE_CASE : str = 1 SCREAMING_SNAKE_CASE : Union[str, Any] = 1 while numerator <= 2 * min_total - 1: prev_numerator += 2 * numerator numerator += 2 * prev_numerator prev_denominator += 2 * denominator denominator += 2 * prev_denominator return (denominator + 1) // 2 if __name__ == "__main__": print(f'''{solution() = }''')
710
"""simple docstring""" import logging import os import random import sys from dataclasses import dataclass, field from typing import Optional import datasets import numpy as np import pandas as pd from datasets import load_dataset import transformers from transformers import ( AutoConfig, BartForSequenceClassification, DataCollatorWithPadding, EvalPrediction, HfArgumentParser, TapexTokenizer, Trainer, TrainingArguments, default_data_collator, set_seed, ) from transformers.trainer_utils import get_last_checkpoint from transformers.utils import check_min_version from transformers.utils.versions import require_version # Will error if the minimal version of Transformers is not installed. Remove at your own risks. check_min_version("4.17.0.dev0") require_version("datasets>=1.8.0", "To fix: pip install -r examples/pytorch/text-classification/requirements.txt") lowerCamelCase__ : Any = logging.getLogger(__name__) @dataclass class lowercase__: '''simple docstring''' UpperCamelCase = field( default="""tab_fact""" , metadata={"""help""": """The name of the dataset to use (via the datasets library)."""} ) UpperCamelCase = field( default="""tab_fact""" , metadata={"""help""": """The configuration name of the dataset to use (via the datasets library)."""} , ) UpperCamelCase = field( default=10_24 , metadata={ """help""": ( """The maximum total input sequence length after tokenization. Sequences longer """ """than this will be truncated, sequences shorter will be padded.""" ) } , ) UpperCamelCase = field( default=_UpperCAmelCase , metadata={"""help""": """Overwrite the cached preprocessed datasets or not."""} ) UpperCamelCase = field( default=_UpperCAmelCase , metadata={ """help""": ( """Whether to pad all samples to `max_seq_length`. """ """If False, will pad the samples dynamically when batching to the maximum length in the batch.""" ) } , ) UpperCamelCase = field( default=_UpperCAmelCase , metadata={ """help""": ( """For debugging purposes or quicker training, truncate the number of training examples to this """ """value if set.""" ) } , ) UpperCamelCase = field( default=_UpperCAmelCase , metadata={ """help""": ( """For debugging purposes or quicker training, truncate the number of evaluation examples to this """ """value if set.""" ) } , ) UpperCamelCase = field( default=_UpperCAmelCase , metadata={ """help""": ( """For debugging purposes or quicker training, truncate the number of prediction examples to this """ """value if set.""" ) } , ) UpperCamelCase = field( default=_UpperCAmelCase , metadata={"""help""": """A csv or a json file containing the training data."""} ) UpperCamelCase = field( default=_UpperCAmelCase , metadata={"""help""": """A csv or a json file containing the validation data."""} ) UpperCamelCase = field(default=_UpperCAmelCase , metadata={"""help""": """A csv or a json file containing the test data."""} ) def __lowerCAmelCase ( self :Union[str, Any] ) -> Dict: '''simple docstring''' if self.dataset_name is not None: pass elif self.train_file is None or self.validation_file is None: raise ValueError('''Need either a GLUE task, a training/validation file or a dataset name.''' ) else: SCREAMING_SNAKE_CASE : Union[str, Any] = self.train_file.split('''.''' )[-1] assert train_extension in ["csv", "json"], "`train_file` should be a csv or a json file." SCREAMING_SNAKE_CASE : Optional[int] = self.validation_file.split('''.''' )[-1] assert ( validation_extension == train_extension ), "`validation_file` should have the same extension (csv or json) as `train_file`." @dataclass class lowercase__: '''simple docstring''' UpperCamelCase = field( default=_UpperCAmelCase , metadata={"""help""": """Path to pretrained model or model identifier from huggingface.co/models"""} ) UpperCamelCase = field( default=_UpperCAmelCase , metadata={"""help""": """Pretrained config name or path if not the same as model_name"""} ) UpperCamelCase = field( default=_UpperCAmelCase , metadata={"""help""": """Pretrained tokenizer name or path if not the same as model_name"""} ) UpperCamelCase = field( default=_UpperCAmelCase , metadata={"""help""": """Where do you want to store the pretrained models downloaded from huggingface.co"""} , ) UpperCamelCase = field( default=_UpperCAmelCase , metadata={"""help""": """Whether to use one of the fast tokenizer (backed by the tokenizers library) or not."""} , ) UpperCamelCase = field( default="""main""" , metadata={"""help""": """The specific model version to use (can be a branch name, tag name or commit id)."""} , ) UpperCamelCase = field( default=_UpperCAmelCase , metadata={ """help""": ( """Will use the token generated when running `huggingface-cli login` (necessary to use this script """ """with private models).""" ) } , ) def __A ( )-> List[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : Union[str, Any] = 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 : Dict = parser.parse_json_file(json_file=os.path.abspath(sys.argv[1] ) ) else: SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : Dict = parser.parse_args_into_dataclasses() # 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 )] , ) SCREAMING_SNAKE_CASE : Union[str, Any] = training_args.get_process_log_level() logger.setLevel(a_ ) datasets.utils.logging.set_verbosity(a_ ) transformers.utils.logging.set_verbosity(a_ ) 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 : 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 ) # Get the datasets: you can either provide your own CSV/JSON training and evaluation files (see below) # or specify a GLUE benchmark task (the dataset will be downloaded automatically from the datasets Hub). # # For JSON files, this script will use the `question` column for the input question and `table` column for the corresponding table. # # If the CSVs/JSONs contain only one non-label column, the script does single sentence classification on this # single column. You can easily tweak this behavior (see below) # # In distributed training, the load_dataset function guarantee that only one local process can concurrently # download the dataset. if data_args.dataset_name is not None: # Downloading and loading a dataset from the hub. SCREAMING_SNAKE_CASE : List[str] = load_dataset( data_args.dataset_name , data_args.dataset_config_name , cache_dir=model_args.cache_dir ) else: # Loading a dataset from your local files. # CSV/JSON training and evaluation files are needed. SCREAMING_SNAKE_CASE : Any = {'''train''': data_args.train_file, '''validation''': data_args.validation_file} # Get the test dataset: you can provide your own CSV/JSON test file (see below) # when you use `do_predict` without specifying a GLUE benchmark task. if training_args.do_predict: if data_args.test_file is not None: SCREAMING_SNAKE_CASE : List[Any] = data_args.train_file.split('''.''' )[-1] SCREAMING_SNAKE_CASE : Optional[int] = data_args.test_file.split('''.''' )[-1] assert ( test_extension == train_extension ), "`test_file` should have the same extension (csv or json) as `train_file`." SCREAMING_SNAKE_CASE : str = data_args.test_file else: raise ValueError('''Need either a GLUE task or a test file for `do_predict`.''' ) for key in data_files.keys(): logger.info(F"load a local file for {key}: {data_files[key]}" ) if data_args.train_file.endswith('''.csv''' ): # Loading a dataset from local csv files SCREAMING_SNAKE_CASE : int = load_dataset('''csv''' , data_files=a_ , cache_dir=model_args.cache_dir ) else: # Loading a dataset from local json files SCREAMING_SNAKE_CASE : Tuple = load_dataset('''json''' , data_files=a_ , cache_dir=model_args.cache_dir ) # See more about loading any type of standard or custom dataset at # https://huggingface.co/docs/datasets/loading_datasets.html. # Labels SCREAMING_SNAKE_CASE : str = raw_datasets['''train'''].features['''label'''].names SCREAMING_SNAKE_CASE : Union[str, Any] = len(a_ ) # Load pretrained model and tokenizer # # In distributed training, the .from_pretrained methods guarantee that only one local process can concurrently # download model & vocab. SCREAMING_SNAKE_CASE : Optional[Any] = AutoConfig.from_pretrained( model_args.config_name if model_args.config_name else model_args.model_name_or_path , num_labels=a_ , cache_dir=model_args.cache_dir , revision=model_args.model_revision , use_auth_token=True if model_args.use_auth_token else None , ) # load tapex tokenizer SCREAMING_SNAKE_CASE : Dict = TapexTokenizer.from_pretrained( model_args.tokenizer_name if model_args.tokenizer_name else model_args.model_name_or_path , cache_dir=model_args.cache_dir , use_fast=model_args.use_fast_tokenizer , revision=model_args.model_revision , use_auth_token=True if model_args.use_auth_token else None , add_prefix_space=a_ , ) SCREAMING_SNAKE_CASE : List[Any] = BartForSequenceClassification.from_pretrained( model_args.model_name_or_path , from_tf=bool('''.ckpt''' in model_args.model_name_or_path ) , config=a_ , cache_dir=model_args.cache_dir , revision=model_args.model_revision , use_auth_token=True if model_args.use_auth_token else None , ) # Padding strategy if data_args.pad_to_max_length: SCREAMING_SNAKE_CASE : Tuple = '''max_length''' else: # We will pad later, dynamically at batch creation, to the max sequence length in each batch SCREAMING_SNAKE_CASE : Optional[Any] = False # Some models have set the order of the labels to use, so let's make sure we do use it. SCREAMING_SNAKE_CASE : Tuple = {'''Refused''': 0, '''Entailed''': 1} SCREAMING_SNAKE_CASE : List[Any] = {0: '''Refused''', 1: '''Entailed'''} if data_args.max_seq_length > tokenizer.model_max_length: logger.warning( F"The max_seq_length passed ({data_args.max_seq_length}) is larger than the maximum length for the" F"model ({tokenizer.model_max_length}). Using max_seq_length={tokenizer.model_max_length}." ) SCREAMING_SNAKE_CASE : Optional[int] = min(data_args.max_seq_length , tokenizer.model_max_length ) def preprocess_tabfact_function(a_ : str ): # Tokenize the texts def _convert_table_text_to_pandas(a_ : List[Any] ): SCREAMING_SNAKE_CASE : List[Any] = [_table_row.split('''#''' ) for _table_row in _table_text.strip('''\n''' ).split('''\n''' )] SCREAMING_SNAKE_CASE : Dict = pd.DataFrame.from_records(_table_content[1:] , columns=_table_content[0] ) return _table_pd SCREAMING_SNAKE_CASE : List[Any] = examples['''statement'''] SCREAMING_SNAKE_CASE : Optional[int] = list(map(_convert_table_text_to_pandas , examples['''table_text'''] ) ) SCREAMING_SNAKE_CASE : Any = tokenizer(a_ , a_ , padding=a_ , max_length=a_ , truncation=a_ ) SCREAMING_SNAKE_CASE : List[Any] = examples['''label'''] return result with training_args.main_process_first(desc='''dataset map pre-processing''' ): SCREAMING_SNAKE_CASE : Optional[Any] = raw_datasets.map( a_ , batched=a_ , load_from_cache_file=not data_args.overwrite_cache , desc='''Running tokenizer on dataset''' , ) if training_args.do_train: if "train" not in raw_datasets: raise ValueError('''--do_train requires a train dataset''' ) SCREAMING_SNAKE_CASE : List[str] = raw_datasets['''train'''] if data_args.max_train_samples is not None: SCREAMING_SNAKE_CASE : Any = train_dataset.select(range(data_args.max_train_samples ) ) if training_args.do_eval: if "validation" not in raw_datasets and "validation_matched" not in raw_datasets: raise ValueError('''--do_eval requires a validation dataset''' ) SCREAMING_SNAKE_CASE : List[str] = raw_datasets['''validation'''] if data_args.max_eval_samples is not None: SCREAMING_SNAKE_CASE : Union[str, Any] = eval_dataset.select(range(data_args.max_eval_samples ) ) if training_args.do_predict or data_args.test_file is not None: if "test" not in raw_datasets and "test_matched" not in raw_datasets: raise ValueError('''--do_predict requires a test dataset''' ) SCREAMING_SNAKE_CASE : Tuple = raw_datasets['''test'''] if data_args.max_predict_samples is not None: SCREAMING_SNAKE_CASE : Optional[int] = predict_dataset.select(range(data_args.max_predict_samples ) ) # Log a few random samples from the training set: if training_args.do_train: for index in random.sample(range(len(a_ ) ) , 3 ): logger.info(F"Sample {index} of the training set: {train_dataset[index]}." ) # You can define your custom 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(a_ : EvalPrediction ): SCREAMING_SNAKE_CASE : str = p.predictions[0] if isinstance(p.predictions , a_ ) else p.predictions SCREAMING_SNAKE_CASE : Tuple = np.argmax(a_ , axis=1 ) return {"accuracy": (preds == p.label_ids).astype(np.floataa ).mean().item()} # Data collator will default to DataCollatorWithPadding, so we change it if we already did the padding. if data_args.pad_to_max_length: SCREAMING_SNAKE_CASE : Tuple = default_data_collator elif training_args.fpaa: SCREAMING_SNAKE_CASE : Union[str, Any] = DataCollatorWithPadding(a_ , pad_to_multiple_of=8 ) else: SCREAMING_SNAKE_CASE : List[Any] = None # Initialize our Trainer SCREAMING_SNAKE_CASE : Optional[Any] = Trainer( model=a_ , args=a_ , train_dataset=train_dataset if training_args.do_train else None , eval_dataset=eval_dataset if training_args.do_eval else None , compute_metrics=a_ , tokenizer=a_ , data_collator=a_ , ) # Training if training_args.do_train: SCREAMING_SNAKE_CASE : List[str] = None if training_args.resume_from_checkpoint is not None: SCREAMING_SNAKE_CASE : Dict = training_args.resume_from_checkpoint elif last_checkpoint is not None: SCREAMING_SNAKE_CASE : str = last_checkpoint SCREAMING_SNAKE_CASE : str = trainer.train(resume_from_checkpoint=a_ ) SCREAMING_SNAKE_CASE : Optional[int] = train_result.metrics SCREAMING_SNAKE_CASE : int = ( data_args.max_train_samples if data_args.max_train_samples is not None else len(a_ ) ) SCREAMING_SNAKE_CASE : Optional[int] = min(a_ , len(a_ ) ) trainer.save_model() # Saves the tokenizer too for easy upload trainer.log_metrics('''train''' , a_ ) trainer.save_metrics('''train''' , a_ ) trainer.save_state() # Evaluation if training_args.do_eval: logger.info('''*** Evaluate ***''' ) SCREAMING_SNAKE_CASE : Tuple = trainer.evaluate(eval_dataset=a_ ) SCREAMING_SNAKE_CASE : str = data_args.max_eval_samples if data_args.max_eval_samples is not None else len(a_ ) SCREAMING_SNAKE_CASE : Union[str, Any] = min(a_ , len(a_ ) ) trainer.log_metrics('''eval''' , a_ ) trainer.save_metrics('''eval''' , a_ ) if training_args.do_predict: logger.info('''*** Predict ***''' ) # Removing the `label` columns because it contains -1 and Trainer won't like that. SCREAMING_SNAKE_CASE : Optional[Any] = predict_dataset.remove_columns('''label''' ) SCREAMING_SNAKE_CASE : Optional[Any] = trainer.predict(a_ , metric_key_prefix='''predict''' ).predictions SCREAMING_SNAKE_CASE : Union[str, Any] = np.argmax(a_ , axis=1 ) SCREAMING_SNAKE_CASE : Optional[int] = os.path.join(training_args.output_dir , '''predict_results_tabfact.txt''' ) if trainer.is_world_process_zero(): with open(a_ , '''w''' ) as writer: logger.info('''***** Predict Results *****''' ) writer.write('''index\tprediction\n''' ) for index, item in enumerate(a_ ): SCREAMING_SNAKE_CASE : Union[str, Any] = label_list[item] writer.write(F"{index}\t{item}\n" ) SCREAMING_SNAKE_CASE : Optional[int] = {'''finetuned_from''': model_args.model_name_or_path, '''tasks''': '''text-classification'''} if training_args.push_to_hub: trainer.push_to_hub(**a_ ) else: trainer.create_model_card(**a_ ) def __A ( a_ : List[str] )-> int: '''simple docstring''' main() if __name__ == "__main__": main()
18
0
"""simple docstring""" def __A ( a_ : float )-> Any: '''simple docstring''' return 10 - x * x def __A ( a_ : float , a_ : float )-> List[str]: '''simple docstring''' if equation(_lowerCAmelCase ) * equation(_lowerCAmelCase ) >= 0: raise ValueError('''Wrong space!''' ) SCREAMING_SNAKE_CASE : List[str] = a while (b - a) >= 0.01: # Find middle point SCREAMING_SNAKE_CASE : Union[str, Any] = (a + b) / 2 # Check if middle point is root if equation(_lowerCAmelCase ) == 0.0: break # Decide the side to repeat the steps if equation(_lowerCAmelCase ) * equation(_lowerCAmelCase ) < 0: SCREAMING_SNAKE_CASE : Union[str, Any] = c else: SCREAMING_SNAKE_CASE : Any = c return c if __name__ == "__main__": import doctest doctest.testmod() print(bisection(-2, 5)) print(bisection(0, 6))
711
"""simple docstring""" import collections import inspect import unittest from transformers import SwinvaConfig from transformers.testing_utils import require_torch, require_vision, slow, torch_device from transformers.utils import cached_property, is_torch_available, is_vision_available from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, _config_zero_init, floats_tensor, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from torch import nn from transformers import SwinvaForImageClassification, SwinvaForMaskedImageModeling, SwinvaModel from transformers.models.swinva.modeling_swinva import SWINV2_PRETRAINED_MODEL_ARCHIVE_LIST if is_vision_available(): from PIL import Image from transformers import AutoImageProcessor class lowercase__: '''simple docstring''' def __init__( self :str , lowerCamelCase_ :List[Any] , lowerCamelCase_ :Any=13 , lowerCamelCase_ :Any=32 , lowerCamelCase_ :Union[str, Any]=2 , lowerCamelCase_ :Any=3 , lowerCamelCase_ :Union[str, Any]=16 , lowerCamelCase_ :int=[1, 2, 1] , lowerCamelCase_ :str=[2, 2, 4] , lowerCamelCase_ :str=2 , lowerCamelCase_ :Tuple=2.0 , lowerCamelCase_ :Union[str, Any]=True , lowerCamelCase_ :str=0.0 , lowerCamelCase_ :Optional[int]=0.0 , lowerCamelCase_ :Dict=0.1 , lowerCamelCase_ :Union[str, Any]="gelu" , lowerCamelCase_ :str=False , lowerCamelCase_ :List[Any]=True , lowerCamelCase_ :int=0.0_2 , lowerCamelCase_ :List[Any]=1E-5 , lowerCamelCase_ :int=True , lowerCamelCase_ :str=None , lowerCamelCase_ :List[Any]=True , lowerCamelCase_ :Union[str, Any]=10 , lowerCamelCase_ :List[Any]=8 , ) -> int: '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[Any] = parent SCREAMING_SNAKE_CASE : int = batch_size SCREAMING_SNAKE_CASE : int = image_size SCREAMING_SNAKE_CASE : Union[str, Any] = patch_size SCREAMING_SNAKE_CASE : Tuple = num_channels SCREAMING_SNAKE_CASE : Any = embed_dim SCREAMING_SNAKE_CASE : int = depths SCREAMING_SNAKE_CASE : List[str] = num_heads SCREAMING_SNAKE_CASE : Union[str, Any] = window_size SCREAMING_SNAKE_CASE : Optional[Any] = mlp_ratio SCREAMING_SNAKE_CASE : List[Any] = qkv_bias SCREAMING_SNAKE_CASE : Union[str, Any] = hidden_dropout_prob SCREAMING_SNAKE_CASE : Dict = attention_probs_dropout_prob SCREAMING_SNAKE_CASE : List[str] = drop_path_rate SCREAMING_SNAKE_CASE : List[Any] = hidden_act SCREAMING_SNAKE_CASE : Optional[Any] = use_absolute_embeddings SCREAMING_SNAKE_CASE : Any = patch_norm SCREAMING_SNAKE_CASE : Union[str, Any] = layer_norm_eps SCREAMING_SNAKE_CASE : List[Any] = initializer_range SCREAMING_SNAKE_CASE : Any = is_training SCREAMING_SNAKE_CASE : List[Any] = scope SCREAMING_SNAKE_CASE : Optional[Any] = use_labels SCREAMING_SNAKE_CASE : Optional[Any] = type_sequence_label_size SCREAMING_SNAKE_CASE : Optional[Any] = encoder_stride def __lowerCAmelCase ( self :Optional[int] ) -> List[str]: '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[int] = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) SCREAMING_SNAKE_CASE : int = None if self.use_labels: SCREAMING_SNAKE_CASE : Dict = ids_tensor([self.batch_size] , self.type_sequence_label_size ) SCREAMING_SNAKE_CASE : Tuple = self.get_config() return config, pixel_values, labels def __lowerCAmelCase ( self :int ) -> int: '''simple docstring''' return SwinvaConfig( image_size=self.image_size , patch_size=self.patch_size , num_channels=self.num_channels , embed_dim=self.embed_dim , depths=self.depths , num_heads=self.num_heads , window_size=self.window_size , mlp_ratio=self.mlp_ratio , qkv_bias=self.qkv_bias , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , drop_path_rate=self.drop_path_rate , hidden_act=self.hidden_act , use_absolute_embeddings=self.use_absolute_embeddings , path_norm=self.patch_norm , layer_norm_eps=self.layer_norm_eps , initializer_range=self.initializer_range , encoder_stride=self.encoder_stride , ) def __lowerCAmelCase ( self :List[Any] , lowerCamelCase_ :int , lowerCamelCase_ :Optional[int] , lowerCamelCase_ :Optional[Any] ) -> Union[str, Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : str = SwinvaModel(config=lowerCamelCase_ ) model.to(lowerCamelCase_ ) model.eval() SCREAMING_SNAKE_CASE : int = model(lowerCamelCase_ ) SCREAMING_SNAKE_CASE : List[Any] = ((config.image_size // config.patch_size) ** 2) // (4 ** (len(config.depths ) - 1)) SCREAMING_SNAKE_CASE : Dict = int(config.embed_dim * 2 ** (len(config.depths ) - 1) ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, expected_seq_len, expected_dim) ) def __lowerCAmelCase ( self :str , lowerCamelCase_ :str , lowerCamelCase_ :Tuple , lowerCamelCase_ :Any ) -> Union[str, Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : List[str] = SwinvaForMaskedImageModeling(config=lowerCamelCase_ ) model.to(lowerCamelCase_ ) model.eval() SCREAMING_SNAKE_CASE : Dict = model(lowerCamelCase_ ) self.parent.assertEqual( result.logits.shape , (self.batch_size, self.num_channels, self.image_size, self.image_size) ) # test greyscale images SCREAMING_SNAKE_CASE : Tuple = 1 SCREAMING_SNAKE_CASE : List[Any] = SwinvaForMaskedImageModeling(lowerCamelCase_ ) model.to(lowerCamelCase_ ) model.eval() SCREAMING_SNAKE_CASE : List[str] = floats_tensor([self.batch_size, 1, self.image_size, self.image_size] ) SCREAMING_SNAKE_CASE : Tuple = model(lowerCamelCase_ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, 1, self.image_size, self.image_size) ) def __lowerCAmelCase ( self :Tuple , lowerCamelCase_ :Union[str, Any] , lowerCamelCase_ :Any , lowerCamelCase_ :int ) -> Tuple: '''simple docstring''' SCREAMING_SNAKE_CASE : Tuple = self.type_sequence_label_size SCREAMING_SNAKE_CASE : Optional[Any] = SwinvaForImageClassification(lowerCamelCase_ ) model.to(lowerCamelCase_ ) model.eval() SCREAMING_SNAKE_CASE : Optional[int] = model(lowerCamelCase_ , labels=lowerCamelCase_ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.type_sequence_label_size) ) def __lowerCAmelCase ( self :List[Any] ) -> List[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : List[str] = self.prepare_config_and_inputs() SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : Optional[int] = config_and_inputs SCREAMING_SNAKE_CASE : int = {'''pixel_values''': pixel_values} return config, inputs_dict @require_torch class lowercase__( _UpperCAmelCase , _UpperCAmelCase , unittest.TestCase ): '''simple docstring''' UpperCamelCase = ( (SwinvaModel, SwinvaForImageClassification, SwinvaForMaskedImageModeling) if is_torch_available() else () ) UpperCamelCase = ( {"""feature-extraction""": SwinvaModel, """image-classification""": SwinvaForImageClassification} if is_torch_available() else {} ) UpperCamelCase = False UpperCamelCase = False UpperCamelCase = False UpperCamelCase = False def __lowerCAmelCase ( self :Union[str, Any] ) -> Union[str, Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : int = SwinvaModelTester(self ) SCREAMING_SNAKE_CASE : Dict = ConfigTester(self , config_class=lowerCamelCase_ , embed_dim=37 ) def __lowerCAmelCase ( self :Dict ) -> List[str]: '''simple docstring''' self.config_tester.create_and_test_config_to_json_string() self.config_tester.create_and_test_config_to_json_file() self.config_tester.create_and_test_config_from_and_save_pretrained() self.config_tester.create_and_test_config_with_num_labels() self.config_tester.check_config_can_be_init_without_params() self.config_tester.check_config_arguments_init() def __lowerCAmelCase ( self :List[Any] ) -> List[str]: '''simple docstring''' SCREAMING_SNAKE_CASE : Union[str, Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*lowerCamelCase_ ) @unittest.skip(reason='''Got `CUDA error: misaligned address` with PyTorch 2.0.0.''' ) def __lowerCAmelCase ( self :str ) -> List[Any]: '''simple docstring''' pass @unittest.skip(reason='''Swinv2 does not use inputs_embeds''' ) def __lowerCAmelCase ( self :List[Any] ) -> Any: '''simple docstring''' pass def __lowerCAmelCase ( self :int ) -> Union[str, Any]: '''simple docstring''' SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : Optional[int] = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: SCREAMING_SNAKE_CASE : List[Any] = model_class(lowerCamelCase_ ) self.assertIsInstance(model.get_input_embeddings() , (nn.Module) ) SCREAMING_SNAKE_CASE : str = model.get_output_embeddings() self.assertTrue(x is None or isinstance(lowerCamelCase_ , nn.Linear ) ) def __lowerCAmelCase ( self :int ) -> List[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : Tuple = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: SCREAMING_SNAKE_CASE : Optional[Any] = model_class(lowerCamelCase_ ) SCREAMING_SNAKE_CASE : List[str] = inspect.signature(model.forward ) # signature.parameters is an OrderedDict => so arg_names order is deterministic SCREAMING_SNAKE_CASE : Optional[int] = [*signature.parameters.keys()] SCREAMING_SNAKE_CASE : Union[str, Any] = ['''pixel_values'''] self.assertListEqual(arg_names[:1] , lowerCamelCase_ ) def __lowerCAmelCase ( self :int ) -> Optional[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : str = self.model_tester.prepare_config_and_inputs_for_common() SCREAMING_SNAKE_CASE : Tuple = True for model_class in self.all_model_classes: SCREAMING_SNAKE_CASE : List[str] = True SCREAMING_SNAKE_CASE : List[Any] = False SCREAMING_SNAKE_CASE : Any = True SCREAMING_SNAKE_CASE : Any = model_class(lowerCamelCase_ ) model.to(lowerCamelCase_ ) model.eval() with torch.no_grad(): SCREAMING_SNAKE_CASE : str = model(**self._prepare_for_class(lowerCamelCase_ , lowerCamelCase_ ) ) SCREAMING_SNAKE_CASE : Tuple = outputs.attentions SCREAMING_SNAKE_CASE : Tuple = len(self.model_tester.depths ) self.assertEqual(len(lowerCamelCase_ ) , lowerCamelCase_ ) # check that output_attentions also work using config del inputs_dict["output_attentions"] SCREAMING_SNAKE_CASE : Union[str, Any] = True SCREAMING_SNAKE_CASE : Optional[int] = config.window_size**2 SCREAMING_SNAKE_CASE : str = model_class(lowerCamelCase_ ) model.to(lowerCamelCase_ ) model.eval() with torch.no_grad(): SCREAMING_SNAKE_CASE : Union[str, Any] = model(**self._prepare_for_class(lowerCamelCase_ , lowerCamelCase_ ) ) SCREAMING_SNAKE_CASE : Dict = outputs.attentions self.assertEqual(len(lowerCamelCase_ ) , lowerCamelCase_ ) self.assertListEqual( list(attentions[0].shape[-3:] ) , [self.model_tester.num_heads[0], window_size_squared, window_size_squared] , ) SCREAMING_SNAKE_CASE : Dict = len(lowerCamelCase_ ) # Check attention is always last and order is fine SCREAMING_SNAKE_CASE : int = True SCREAMING_SNAKE_CASE : Dict = True SCREAMING_SNAKE_CASE : int = model_class(lowerCamelCase_ ) model.to(lowerCamelCase_ ) model.eval() with torch.no_grad(): SCREAMING_SNAKE_CASE : List[Any] = model(**self._prepare_for_class(lowerCamelCase_ , lowerCamelCase_ ) ) if hasattr(self.model_tester , '''num_hidden_states_types''' ): SCREAMING_SNAKE_CASE : Any = self.model_tester.num_hidden_states_types else: # also another +1 for reshaped_hidden_states SCREAMING_SNAKE_CASE : Optional[Any] = 2 self.assertEqual(out_len + added_hidden_states , len(lowerCamelCase_ ) ) SCREAMING_SNAKE_CASE : Tuple = outputs.attentions self.assertEqual(len(lowerCamelCase_ ) , lowerCamelCase_ ) self.assertListEqual( list(self_attentions[0].shape[-3:] ) , [self.model_tester.num_heads[0], window_size_squared, window_size_squared] , ) def __lowerCAmelCase ( self :Dict , lowerCamelCase_ :int , lowerCamelCase_ :Any , lowerCamelCase_ :Tuple , lowerCamelCase_ :Any ) -> Optional[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : Union[str, Any] = model_class(lowerCamelCase_ ) model.to(lowerCamelCase_ ) model.eval() with torch.no_grad(): SCREAMING_SNAKE_CASE : Optional[int] = model(**self._prepare_for_class(lowerCamelCase_ , lowerCamelCase_ ) ) SCREAMING_SNAKE_CASE : Dict = outputs.hidden_states SCREAMING_SNAKE_CASE : str = getattr( self.model_tester , '''expected_num_hidden_layers''' , len(self.model_tester.depths ) + 1 ) self.assertEqual(len(lowerCamelCase_ ) , lowerCamelCase_ ) # Swinv2 has a different seq_length SCREAMING_SNAKE_CASE : List[str] = ( config.patch_size if isinstance(config.patch_size , collections.abc.Iterable ) else (config.patch_size, config.patch_size) ) SCREAMING_SNAKE_CASE : Dict = (image_size[1] // patch_size[1]) * (image_size[0] // patch_size[0]) self.assertListEqual( list(hidden_states[0].shape[-2:] ) , [num_patches, self.model_tester.embed_dim] , ) SCREAMING_SNAKE_CASE : Any = outputs.reshaped_hidden_states self.assertEqual(len(lowerCamelCase_ ) , lowerCamelCase_ ) SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : Any = reshaped_hidden_states[0].shape SCREAMING_SNAKE_CASE : Optional[int] = ( reshaped_hidden_states[0].view(lowerCamelCase_ , lowerCamelCase_ , height * width ).permute(0 , 2 , 1 ) ) self.assertListEqual( list(reshaped_hidden_states.shape[-2:] ) , [num_patches, self.model_tester.embed_dim] , ) def __lowerCAmelCase ( self :Optional[Any] ) -> Optional[int]: '''simple docstring''' SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : Union[str, Any] = self.model_tester.prepare_config_and_inputs_for_common() SCREAMING_SNAKE_CASE : List[str] = ( self.model_tester.image_size if isinstance(self.model_tester.image_size , collections.abc.Iterable ) else (self.model_tester.image_size, self.model_tester.image_size) ) for model_class in self.all_model_classes: SCREAMING_SNAKE_CASE : Tuple = True self.check_hidden_states_output(lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ ) # check that output_hidden_states also work using config del inputs_dict["output_hidden_states"] SCREAMING_SNAKE_CASE : List[str] = True self.check_hidden_states_output(lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ ) def __lowerCAmelCase ( self :str ) -> Optional[int]: '''simple docstring''' SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : str = self.model_tester.prepare_config_and_inputs_for_common() SCREAMING_SNAKE_CASE : List[Any] = 3 SCREAMING_SNAKE_CASE : int = ( self.model_tester.image_size if isinstance(self.model_tester.image_size , collections.abc.Iterable ) else (self.model_tester.image_size, self.model_tester.image_size) ) SCREAMING_SNAKE_CASE : Optional[Any] = ( config.patch_size if isinstance(config.patch_size , collections.abc.Iterable ) else (config.patch_size, config.patch_size) ) SCREAMING_SNAKE_CASE : Optional[int] = image_size[0] + patch_size[0] - (image_size[0] % patch_size[0]) SCREAMING_SNAKE_CASE : Optional[int] = image_size[1] + patch_size[1] - (image_size[1] % patch_size[1]) for model_class in self.all_model_classes: SCREAMING_SNAKE_CASE : Optional[Any] = True self.check_hidden_states_output(lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , (padded_height, padded_width) ) # check that output_hidden_states also work using config del inputs_dict["output_hidden_states"] SCREAMING_SNAKE_CASE : Optional[Any] = True self.check_hidden_states_output(lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , (padded_height, padded_width) ) def __lowerCAmelCase ( self :str ) -> List[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : Tuple = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_masked_image_modeling(*lowerCamelCase_ ) def __lowerCAmelCase ( self :Tuple ) -> Optional[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : Tuple = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_image_classification(*lowerCamelCase_ ) @slow def __lowerCAmelCase ( self :Tuple ) -> List[str]: '''simple docstring''' for model_name in SWINV2_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: SCREAMING_SNAKE_CASE : Tuple = SwinvaModel.from_pretrained(lowerCamelCase_ ) self.assertIsNotNone(lowerCamelCase_ ) def __lowerCAmelCase ( self :int ) -> Any: '''simple docstring''' SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : List[str] = self.model_tester.prepare_config_and_inputs_for_common() SCREAMING_SNAKE_CASE : Tuple = _config_zero_init(lowerCamelCase_ ) for model_class in self.all_model_classes: SCREAMING_SNAKE_CASE : Dict = model_class(config=lowerCamelCase_ ) for name, param in model.named_parameters(): if "embeddings" not in name and "logit_scale" not in name and param.requires_grad: self.assertIn( ((param.data.mean() * 1E9).round() / 1E9).item() , [0.0, 1.0] , msg=f"Parameter {name} of model {model_class} seems not properly initialized" , ) @require_vision @require_torch class lowercase__( unittest.TestCase ): '''simple docstring''' @cached_property def __lowerCAmelCase ( self :Dict ) -> List[Any]: '''simple docstring''' return ( AutoImageProcessor.from_pretrained('''microsoft/swinv2-tiny-patch4-window8-256''' ) if is_vision_available() else None ) @slow def __lowerCAmelCase ( self :Dict ) -> Tuple: '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[int] = SwinvaForImageClassification.from_pretrained('''microsoft/swinv2-tiny-patch4-window8-256''' ).to( lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Dict = self.default_image_processor SCREAMING_SNAKE_CASE : Optional[Any] = Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png''' ) SCREAMING_SNAKE_CASE : List[str] = image_processor(images=lowerCamelCase_ , return_tensors='''pt''' ).to(lowerCamelCase_ ) # forward pass with torch.no_grad(): SCREAMING_SNAKE_CASE : Tuple = model(**lowerCamelCase_ ) # verify the logits SCREAMING_SNAKE_CASE : List[str] = torch.Size((1, 10_00) ) self.assertEqual(outputs.logits.shape , lowerCamelCase_ ) SCREAMING_SNAKE_CASE : int = torch.tensor([-0.3_9_4_7, -0.4_3_0_6, 0.0_0_2_6] ).to(lowerCamelCase_ ) self.assertTrue(torch.allclose(outputs.logits[0, :3] , lowerCamelCase_ , atol=1E-4 ) )
18
0
"""simple docstring""" from math import factorial lowerCamelCase__ : dict[str, int] = {str(digit): factorial(digit) for digit in range(10)} def __A ( a_ : List[Any] )-> int: '''simple docstring''' if not isinstance(__lowerCAmelCase , __lowerCAmelCase ): raise TypeError('''Parameter number must be int''' ) if number < 0: raise ValueError('''Parameter number must be greater than or equal to 0''' ) # Converts number in string to iterate on its digits and adds its factorial. return sum(DIGIT_FACTORIAL[digit] for digit in str(__lowerCAmelCase ) ) def __A ( a_ : Tuple = 60 , a_ : List[str] = 1_00_00_00 )-> int: '''simple docstring''' if not isinstance(__lowerCAmelCase , __lowerCAmelCase ) or not isinstance(__lowerCAmelCase , __lowerCAmelCase ): raise TypeError('''Parameters chain_length and number_limit must be int''' ) if chain_length <= 0 or number_limit <= 0: raise ValueError( '''Parameters chain_length and number_limit must be greater than 0''' ) # the counter for the chains with the exact desired length SCREAMING_SNAKE_CASE : str = 0 # the cached sizes of the previous chains SCREAMING_SNAKE_CASE : dict[int, int] = {} for start_chain_element in range(1 , __lowerCAmelCase ): # The temporary set will contain the elements of the chain SCREAMING_SNAKE_CASE : Optional[Any] = set() SCREAMING_SNAKE_CASE : List[str] = 0 # Stop computing the chain when you find a cached size, a repeating item or the # length is greater then the desired one. SCREAMING_SNAKE_CASE : str = start_chain_element while ( chain_element not in chain_sets_lengths and chain_element not in chain_set and chain_set_length <= chain_length ): chain_set.add(__lowerCAmelCase ) chain_set_length += 1 SCREAMING_SNAKE_CASE : Union[str, Any] = digit_factorial_sum(__lowerCAmelCase ) if chain_element in chain_sets_lengths: chain_set_length += chain_sets_lengths[chain_element] SCREAMING_SNAKE_CASE : str = chain_set_length # If chain contains the exact amount of elements increase the counter if chain_set_length == chain_length: chains_counter += 1 return chains_counter if __name__ == "__main__": import doctest doctest.testmod() print(f'''{solution()}''')
712
"""simple docstring""" from collections import OrderedDict from typing import Any, Mapping, Optional from ... import PreTrainedTokenizer from ...configuration_utils import PretrainedConfig from ...file_utils import TensorType, is_torch_available from ...onnx import OnnxConfig, OnnxConfigWithPast, OnnxSeqaSeqConfigWithPast from ...onnx.utils import compute_effective_axis_dimension from ...utils import logging lowerCamelCase__ : Tuple = logging.get_logger(__name__) lowerCamelCase__ : str = { "facebook/blenderbot_small-90M": "https://huggingface.co/facebook/blenderbot_small-90M/resolve/main/config.json", # See all BlenderbotSmall models at https://huggingface.co/models?filter=blenderbot_small } class lowercase__( _UpperCAmelCase ): '''simple docstring''' UpperCamelCase = """blenderbot-small""" UpperCamelCase = ["""past_key_values"""] UpperCamelCase = {"""num_attention_heads""": """encoder_attention_heads""", """hidden_size""": """d_model"""} def __init__( self :Any , lowerCamelCase_ :Dict=5_02_65 , lowerCamelCase_ :str=5_12 , lowerCamelCase_ :Tuple=8 , lowerCamelCase_ :int=20_48 , lowerCamelCase_ :str=16 , lowerCamelCase_ :Optional[int]=8 , lowerCamelCase_ :str=20_48 , lowerCamelCase_ :Optional[Any]=16 , lowerCamelCase_ :Union[str, Any]=0.0 , lowerCamelCase_ :List[str]=0.0 , lowerCamelCase_ :Tuple=True , lowerCamelCase_ :Tuple=True , lowerCamelCase_ :int="gelu" , lowerCamelCase_ :Tuple=5_12 , lowerCamelCase_ :Optional[int]=0.1 , lowerCamelCase_ :int=0.0 , lowerCamelCase_ :Tuple=0.0 , lowerCamelCase_ :Optional[int]=0.0_2 , lowerCamelCase_ :Union[str, Any]=1 , lowerCamelCase_ :Dict=False , lowerCamelCase_ :Optional[int]=0 , lowerCamelCase_ :List[Any]=1 , lowerCamelCase_ :Any=2 , lowerCamelCase_ :Optional[Any]=2 , **lowerCamelCase_ :Dict , ) -> Any: '''simple docstring''' SCREAMING_SNAKE_CASE : int = vocab_size SCREAMING_SNAKE_CASE : List[str] = max_position_embeddings SCREAMING_SNAKE_CASE : Optional[Any] = d_model SCREAMING_SNAKE_CASE : Dict = encoder_ffn_dim SCREAMING_SNAKE_CASE : Tuple = encoder_layers SCREAMING_SNAKE_CASE : Dict = encoder_attention_heads SCREAMING_SNAKE_CASE : Any = decoder_ffn_dim SCREAMING_SNAKE_CASE : str = decoder_layers SCREAMING_SNAKE_CASE : str = decoder_attention_heads SCREAMING_SNAKE_CASE : List[Any] = dropout SCREAMING_SNAKE_CASE : Optional[Any] = attention_dropout SCREAMING_SNAKE_CASE : Any = activation_dropout SCREAMING_SNAKE_CASE : List[str] = activation_function SCREAMING_SNAKE_CASE : Optional[int] = init_std SCREAMING_SNAKE_CASE : List[Any] = encoder_layerdrop SCREAMING_SNAKE_CASE : Union[str, Any] = decoder_layerdrop SCREAMING_SNAKE_CASE : List[Any] = use_cache SCREAMING_SNAKE_CASE : Union[str, Any] = encoder_layers SCREAMING_SNAKE_CASE : List[Any] = scale_embedding # scale factor will be sqrt(d_model) if True super().__init__( pad_token_id=lowerCamelCase_ , bos_token_id=lowerCamelCase_ , eos_token_id=lowerCamelCase_ , is_encoder_decoder=lowerCamelCase_ , decoder_start_token_id=lowerCamelCase_ , forced_eos_token_id=lowerCamelCase_ , **lowerCamelCase_ , ) class lowercase__( _UpperCAmelCase ): '''simple docstring''' @property def __lowerCAmelCase ( self :Any ) -> Mapping[str, Mapping[int, str]]: '''simple docstring''' if self.task in ["default", "seq2seq-lm"]: SCREAMING_SNAKE_CASE : Tuple = OrderedDict( [ ('''input_ids''', {0: '''batch''', 1: '''encoder_sequence'''}), ('''attention_mask''', {0: '''batch''', 1: '''encoder_sequence'''}), ] ) if self.use_past: SCREAMING_SNAKE_CASE : Union[str, Any] = {0: '''batch'''} SCREAMING_SNAKE_CASE : List[Any] = {0: '''batch''', 1: '''past_decoder_sequence + sequence'''} else: SCREAMING_SNAKE_CASE : Optional[Any] = {0: '''batch''', 1: '''decoder_sequence'''} SCREAMING_SNAKE_CASE : Union[str, Any] = {0: '''batch''', 1: '''decoder_sequence'''} if self.use_past: self.fill_with_past_key_values_(lowerCamelCase_ , direction='''inputs''' ) elif self.task == "causal-lm": # TODO: figure this case out. SCREAMING_SNAKE_CASE : Tuple = OrderedDict( [ ('''input_ids''', {0: '''batch''', 1: '''encoder_sequence'''}), ('''attention_mask''', {0: '''batch''', 1: '''encoder_sequence'''}), ] ) if self.use_past: SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : List[Any] = self.num_layers for i in range(lowerCamelCase_ ): SCREAMING_SNAKE_CASE : int = {0: '''batch''', 2: '''past_sequence + sequence'''} SCREAMING_SNAKE_CASE : List[str] = {0: '''batch''', 2: '''past_sequence + sequence'''} else: SCREAMING_SNAKE_CASE : Any = OrderedDict( [ ('''input_ids''', {0: '''batch''', 1: '''encoder_sequence'''}), ('''attention_mask''', {0: '''batch''', 1: '''encoder_sequence'''}), ('''decoder_input_ids''', {0: '''batch''', 1: '''decoder_sequence'''}), ('''decoder_attention_mask''', {0: '''batch''', 1: '''decoder_sequence'''}), ] ) return common_inputs @property def __lowerCAmelCase ( self :List[str] ) -> Mapping[str, Mapping[int, str]]: '''simple docstring''' if self.task in ["default", "seq2seq-lm"]: SCREAMING_SNAKE_CASE : Any = super().outputs else: SCREAMING_SNAKE_CASE : Tuple = super(lowerCamelCase_ , self ).outputs if self.use_past: SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : Union[str, Any] = self.num_layers for i in range(lowerCamelCase_ ): SCREAMING_SNAKE_CASE : Union[str, Any] = {0: '''batch''', 2: '''past_sequence + sequence'''} SCREAMING_SNAKE_CASE : str = {0: '''batch''', 2: '''past_sequence + sequence'''} return common_outputs def __lowerCAmelCase ( self :int , lowerCamelCase_ :PreTrainedTokenizer , lowerCamelCase_ :int = -1 , lowerCamelCase_ :int = -1 , lowerCamelCase_ :bool = False , lowerCamelCase_ :Optional[TensorType] = None , ) -> Mapping[str, Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : Dict = self._generate_dummy_inputs_for_sequence_classification_and_question_answering( lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ ) # Generate decoder inputs SCREAMING_SNAKE_CASE : Optional[int] = seq_length if not self.use_past else 1 SCREAMING_SNAKE_CASE : Optional[int] = self._generate_dummy_inputs_for_sequence_classification_and_question_answering( lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ ) SCREAMING_SNAKE_CASE : List[Any] = {f"decoder_{name}": tensor for name, tensor in decoder_inputs.items()} SCREAMING_SNAKE_CASE : str = dict(**lowerCamelCase_ , **lowerCamelCase_ ) if self.use_past: if not is_torch_available(): raise ValueError('''Cannot generate dummy past_keys inputs without PyTorch installed.''' ) else: import torch SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : Any = common_inputs['''input_ids'''].shape SCREAMING_SNAKE_CASE : str = common_inputs['''decoder_input_ids'''].shape[1] SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : Any = self.num_attention_heads SCREAMING_SNAKE_CASE : str = ( batch, num_encoder_attention_heads, encoder_seq_length, self._config.hidden_size // num_encoder_attention_heads, ) SCREAMING_SNAKE_CASE : Optional[Any] = decoder_seq_length + 3 SCREAMING_SNAKE_CASE : int = ( batch, num_decoder_attention_heads, decoder_past_length, self._config.hidden_size // num_decoder_attention_heads, ) SCREAMING_SNAKE_CASE : List[Any] = torch.cat( [common_inputs['''decoder_attention_mask'''], torch.ones(lowerCamelCase_ , lowerCamelCase_ )] , dim=1 ) SCREAMING_SNAKE_CASE : Optional[int] = [] # If the number of encoder and decoder layers are present in the model configuration, both are considered SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : Optional[int] = self.num_layers SCREAMING_SNAKE_CASE : int = min(lowerCamelCase_ , lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Optional[int] = max(lowerCamelCase_ , lowerCamelCase_ ) - min_num_layers SCREAMING_SNAKE_CASE : Tuple = '''encoder''' if num_encoder_layers > num_decoder_layers else '''decoder''' for _ in range(lowerCamelCase_ ): common_inputs["past_key_values"].append( ( torch.zeros(lowerCamelCase_ ), torch.zeros(lowerCamelCase_ ), torch.zeros(lowerCamelCase_ ), torch.zeros(lowerCamelCase_ ), ) ) # TODO: test this. SCREAMING_SNAKE_CASE : int = encoder_shape if remaining_side_name == '''encoder''' else decoder_shape for _ in range(lowerCamelCase_ , lowerCamelCase_ ): common_inputs["past_key_values"].append((torch.zeros(lowerCamelCase_ ), torch.zeros(lowerCamelCase_ )) ) return common_inputs def __lowerCAmelCase ( self :Any , lowerCamelCase_ :PreTrainedTokenizer , lowerCamelCase_ :int = -1 , lowerCamelCase_ :int = -1 , lowerCamelCase_ :bool = False , lowerCamelCase_ :Optional[TensorType] = None , ) -> Mapping[str, Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : Any = self._generate_dummy_inputs_for_sequence_classification_and_question_answering( lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ ) if self.use_past: if not is_torch_available(): raise ValueError('''Cannot generate dummy past_keys inputs without PyTorch installed.''' ) else: import torch SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : Tuple = common_inputs['''input_ids'''].shape # Not using the same length for past_key_values SCREAMING_SNAKE_CASE : List[str] = seqlen + 2 SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : Dict = self.num_layers SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : List[Any] = self.num_attention_heads SCREAMING_SNAKE_CASE : Union[str, Any] = ( batch, num_encoder_attention_heads, past_key_values_length, self._config.hidden_size // num_encoder_attention_heads, ) SCREAMING_SNAKE_CASE : Tuple = common_inputs['''attention_mask'''].dtype SCREAMING_SNAKE_CASE : Any = torch.cat( [common_inputs['''attention_mask'''], torch.ones(lowerCamelCase_ , lowerCamelCase_ , dtype=lowerCamelCase_ )] , dim=1 ) SCREAMING_SNAKE_CASE : Optional[int] = [ (torch.zeros(lowerCamelCase_ ), torch.zeros(lowerCamelCase_ )) for _ in range(lowerCamelCase_ ) ] return common_inputs def __lowerCAmelCase ( self :Union[str, Any] , lowerCamelCase_ :PreTrainedTokenizer , lowerCamelCase_ :int = -1 , lowerCamelCase_ :int = -1 , lowerCamelCase_ :bool = False , lowerCamelCase_ :Optional[TensorType] = None , ) -> Mapping[str, Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : List[str] = compute_effective_axis_dimension( lowerCamelCase_ , fixed_dimension=OnnxConfig.default_fixed_batch , num_token_to_add=0 ) # If dynamic axis (-1) we forward with a fixed dimension of 8 tokens to avoid optimizations made by ONNX SCREAMING_SNAKE_CASE : int = tokenizer.num_special_tokens_to_add(lowerCamelCase_ ) SCREAMING_SNAKE_CASE : int = compute_effective_axis_dimension( lowerCamelCase_ , fixed_dimension=OnnxConfig.default_fixed_sequence , num_token_to_add=lowerCamelCase_ ) # Generate dummy inputs according to compute batch and sequence SCREAMING_SNAKE_CASE : Tuple = [''' '''.join([tokenizer.unk_token] ) * seq_length] * batch_size SCREAMING_SNAKE_CASE : Any = dict(tokenizer(lowerCamelCase_ , return_tensors=lowerCamelCase_ ) ) return common_inputs def __lowerCAmelCase ( self :List[Any] , lowerCamelCase_ :PreTrainedTokenizer , lowerCamelCase_ :int = -1 , lowerCamelCase_ :int = -1 , lowerCamelCase_ :bool = False , lowerCamelCase_ :Optional[TensorType] = None , ) -> Mapping[str, Any]: '''simple docstring''' if self.task in ["default", "seq2seq-lm"]: SCREAMING_SNAKE_CASE : Dict = self._generate_dummy_inputs_for_default_and_seqaseq_lm( lowerCamelCase_ , batch_size=lowerCamelCase_ , seq_length=lowerCamelCase_ , is_pair=lowerCamelCase_ , framework=lowerCamelCase_ ) elif self.task == "causal-lm": SCREAMING_SNAKE_CASE : Union[str, Any] = self._generate_dummy_inputs_for_causal_lm( lowerCamelCase_ , batch_size=lowerCamelCase_ , seq_length=lowerCamelCase_ , is_pair=lowerCamelCase_ , framework=lowerCamelCase_ ) else: SCREAMING_SNAKE_CASE : List[Any] = self._generate_dummy_inputs_for_sequence_classification_and_question_answering( lowerCamelCase_ , batch_size=lowerCamelCase_ , seq_length=lowerCamelCase_ , is_pair=lowerCamelCase_ , framework=lowerCamelCase_ ) return common_inputs def __lowerCAmelCase ( self :Dict , lowerCamelCase_ :int , lowerCamelCase_ :List[str] , lowerCamelCase_ :List[str] , lowerCamelCase_ :Dict ) -> List[Any]: '''simple docstring''' if self.task in ["default", "seq2seq-lm"]: SCREAMING_SNAKE_CASE : Optional[Any] = super()._flatten_past_key_values_(lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ ) else: SCREAMING_SNAKE_CASE : Tuple = super(lowerCamelCase_ , self )._flatten_past_key_values_( lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ )
18
0
"""simple docstring""" import numpy as np import datasets lowerCamelCase__ : Optional[int] = "\nCompute the Mahalanobis Distance\n\nMahalonobis distance is the distance between a point and a distribution.\nAnd not between two distinct points. It is effectively a multivariate equivalent of the Euclidean distance.\nIt was introduced by Prof. P. C. Mahalanobis in 1936\nand has been used in various statistical applications ever since\n[source: https://www.machinelearningplus.com/statistics/mahalanobis-distance/]\n" lowerCamelCase__ : List[Any] = "\\n@article{de2000mahalanobis,\n title={The mahalanobis distance},\n author={De Maesschalck, Roy and Jouan-Rimbaud, Delphine and Massart, D{\'e}sir{\'e} L},\n journal={Chemometrics and intelligent laboratory systems},\n volume={50},\n number={1},\n pages={1--18},\n year={2000},\n publisher={Elsevier}\n}\n" lowerCamelCase__ : Optional[int] = "\nArgs:\n X: List of datapoints to be compared with the `reference_distribution`.\n reference_distribution: List of datapoints from the reference distribution we want to compare to.\nReturns:\n mahalanobis: The Mahalonobis distance for each datapoint in `X`.\nExamples:\n\n >>> mahalanobis_metric = datasets.load_metric(\"mahalanobis\")\n >>> results = mahalanobis_metric.compute(reference_distribution=[[0, 1], [1, 0]], X=[[0, 1]])\n >>> print(results)\n {'mahalanobis': array([0.5])}\n" @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class lowercase__( datasets.Metric ): '''simple docstring''' def __lowerCAmelCase ( self :Union[str, Any] ) -> int: '''simple docstring''' return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { '''X''': datasets.Sequence(datasets.Value('''float''' , id='''sequence''' ) , id='''X''' ), } ) , ) def __lowerCAmelCase ( self :Optional[int] , lowerCamelCase_ :str , lowerCamelCase_ :List[Any] ) -> List[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : List[str] = np.array(lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Tuple = np.array(lowerCamelCase_ ) # Assert that arrays are 2D if len(X.shape ) != 2: raise ValueError('''Expected `X` to be a 2D vector''' ) if len(reference_distribution.shape ) != 2: raise ValueError('''Expected `reference_distribution` to be a 2D vector''' ) if reference_distribution.shape[0] < 2: raise ValueError( '''Expected `reference_distribution` to be a 2D vector with more than one element in the first dimension''' ) # Get mahalanobis distance for each prediction SCREAMING_SNAKE_CASE : Dict = X - np.mean(lowerCamelCase_ ) SCREAMING_SNAKE_CASE : List[Any] = np.cov(reference_distribution.T ) try: SCREAMING_SNAKE_CASE : Union[str, Any] = np.linalg.inv(lowerCamelCase_ ) except np.linalg.LinAlgError: SCREAMING_SNAKE_CASE : Dict = np.linalg.pinv(lowerCamelCase_ ) SCREAMING_SNAKE_CASE : List[str] = np.dot(lowerCamelCase_ , lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Dict = np.dot(lowerCamelCase_ , X_minus_mu.T ).diagonal() return {"mahalanobis": mahal_dist}
713
"""simple docstring""" from collections import OrderedDict from typing import TYPE_CHECKING, Any, Mapping, Optional from packaging import version from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...onnx.utils import compute_effective_axis_dimension from ...utils import logging if TYPE_CHECKING: from ...processing_utils import ProcessorMixin from ...utils import TensorType lowerCamelCase__ : Dict = logging.get_logger(__name__) lowerCamelCase__ : Dict = { "microsoft/layoutlmv3-base": "https://huggingface.co/microsoft/layoutlmv3-base/resolve/main/config.json", } class lowercase__( _UpperCAmelCase ): '''simple docstring''' UpperCamelCase = """layoutlmv3""" def __init__( self :str , lowerCamelCase_ :Optional[Any]=5_02_65 , lowerCamelCase_ :Dict=7_68 , lowerCamelCase_ :Union[str, Any]=12 , lowerCamelCase_ :Optional[Any]=12 , lowerCamelCase_ :Union[str, Any]=30_72 , lowerCamelCase_ :Any="gelu" , lowerCamelCase_ :Union[str, Any]=0.1 , lowerCamelCase_ :str=0.1 , lowerCamelCase_ :Any=5_12 , lowerCamelCase_ :int=2 , lowerCamelCase_ :Optional[Any]=0.0_2 , lowerCamelCase_ :Optional[int]=1E-5 , lowerCamelCase_ :Dict=1 , lowerCamelCase_ :int=0 , lowerCamelCase_ :Tuple=2 , lowerCamelCase_ :List[str]=10_24 , lowerCamelCase_ :Tuple=1_28 , lowerCamelCase_ :Any=1_28 , lowerCamelCase_ :Optional[Any]=True , lowerCamelCase_ :str=32 , lowerCamelCase_ :int=1_28 , lowerCamelCase_ :int=64 , lowerCamelCase_ :List[Any]=2_56 , lowerCamelCase_ :Any=True , lowerCamelCase_ :str=True , lowerCamelCase_ :Union[str, Any]=True , lowerCamelCase_ :List[str]=2_24 , lowerCamelCase_ :Dict=3 , lowerCamelCase_ :Union[str, Any]=16 , lowerCamelCase_ :Any=None , **lowerCamelCase_ :Optional[Any] , ) -> int: '''simple docstring''' super().__init__( vocab_size=lowerCamelCase_ , hidden_size=lowerCamelCase_ , num_hidden_layers=lowerCamelCase_ , num_attention_heads=lowerCamelCase_ , intermediate_size=lowerCamelCase_ , hidden_act=lowerCamelCase_ , hidden_dropout_prob=lowerCamelCase_ , attention_probs_dropout_prob=lowerCamelCase_ , max_position_embeddings=lowerCamelCase_ , type_vocab_size=lowerCamelCase_ , initializer_range=lowerCamelCase_ , layer_norm_eps=lowerCamelCase_ , pad_token_id=lowerCamelCase_ , bos_token_id=lowerCamelCase_ , eos_token_id=lowerCamelCase_ , **lowerCamelCase_ , ) SCREAMING_SNAKE_CASE : Optional[Any] = max_ad_position_embeddings SCREAMING_SNAKE_CASE : List[Any] = coordinate_size SCREAMING_SNAKE_CASE : Tuple = shape_size SCREAMING_SNAKE_CASE : Optional[int] = has_relative_attention_bias SCREAMING_SNAKE_CASE : List[Any] = rel_pos_bins SCREAMING_SNAKE_CASE : int = max_rel_pos SCREAMING_SNAKE_CASE : Any = has_spatial_attention_bias SCREAMING_SNAKE_CASE : List[Any] = rel_ad_pos_bins SCREAMING_SNAKE_CASE : Dict = max_rel_ad_pos SCREAMING_SNAKE_CASE : Optional[int] = text_embed SCREAMING_SNAKE_CASE : Any = visual_embed SCREAMING_SNAKE_CASE : Any = input_size SCREAMING_SNAKE_CASE : Tuple = num_channels SCREAMING_SNAKE_CASE : List[str] = patch_size SCREAMING_SNAKE_CASE : str = classifier_dropout class lowercase__( _UpperCAmelCase ): '''simple docstring''' UpperCamelCase = version.parse("""1.12""" ) @property def __lowerCAmelCase ( self :List[Any] ) -> Mapping[str, Mapping[int, str]]: '''simple docstring''' if self.task in ["question-answering", "sequence-classification"]: return OrderedDict( [ ('''input_ids''', {0: '''batch''', 1: '''sequence'''}), ('''attention_mask''', {0: '''batch''', 1: '''sequence'''}), ('''bbox''', {0: '''batch''', 1: '''sequence'''}), ('''pixel_values''', {0: '''batch''', 1: '''num_channels''', 2: '''height''', 3: '''width'''}), ] ) else: return OrderedDict( [ ('''input_ids''', {0: '''batch''', 1: '''sequence'''}), ('''bbox''', {0: '''batch''', 1: '''sequence'''}), ('''attention_mask''', {0: '''batch''', 1: '''sequence'''}), ('''pixel_values''', {0: '''batch''', 1: '''num_channels'''}), ] ) @property def __lowerCAmelCase ( self :Optional[int] ) -> float: '''simple docstring''' return 1E-5 @property def __lowerCAmelCase ( self :Tuple ) -> int: '''simple docstring''' return 12 def __lowerCAmelCase ( self :List[Any] , lowerCamelCase_ :"ProcessorMixin" , lowerCamelCase_ :int = -1 , lowerCamelCase_ :int = -1 , lowerCamelCase_ :bool = False , lowerCamelCase_ :Optional["TensorType"] = None , lowerCamelCase_ :int = 3 , lowerCamelCase_ :int = 40 , lowerCamelCase_ :int = 40 , ) -> Mapping[str, Any]: '''simple docstring''' setattr(processor.image_processor , '''apply_ocr''' , lowerCamelCase_ ) # If dynamic axis (-1) we forward with a fixed dimension of 2 samples to avoid optimizations made by ONNX SCREAMING_SNAKE_CASE : Dict = compute_effective_axis_dimension( lowerCamelCase_ , fixed_dimension=OnnxConfig.default_fixed_batch , num_token_to_add=0 ) # If dynamic axis (-1) we forward with a fixed dimension of 8 tokens to avoid optimizations made by ONNX SCREAMING_SNAKE_CASE : Union[str, Any] = processor.tokenizer.num_special_tokens_to_add(lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Any = compute_effective_axis_dimension( lowerCamelCase_ , fixed_dimension=OnnxConfig.default_fixed_sequence , num_token_to_add=lowerCamelCase_ ) # Generate dummy inputs according to compute batch and sequence SCREAMING_SNAKE_CASE : Union[str, Any] = [[''' '''.join([processor.tokenizer.unk_token] ) * seq_length]] * batch_size # Generate dummy bounding boxes SCREAMING_SNAKE_CASE : int = [[[48, 84, 73, 1_28]]] * batch_size # If dynamic axis (-1) we forward with a fixed dimension of 2 samples to avoid optimizations made by ONNX # batch_size = compute_effective_axis_dimension(batch_size, fixed_dimension=OnnxConfig.default_fixed_batch) SCREAMING_SNAKE_CASE : List[Any] = self._generate_dummy_images(lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ ) SCREAMING_SNAKE_CASE : int = dict( processor( lowerCamelCase_ , text=lowerCamelCase_ , boxes=lowerCamelCase_ , return_tensors=lowerCamelCase_ , ) ) return inputs
18
0
"""simple docstring""" from __future__ import annotations def __A ( a_ : Union[str, Any] , a_ : Optional[int] )-> Union[str, Any]: '''simple docstring''' if len(lowerCAmelCase__ ) < k or k < 0: raise ValueError('''Invalid Input''' ) SCREAMING_SNAKE_CASE : List[str] = sum(array[:k] ) for i in range(len(lowerCAmelCase__ ) - k ): SCREAMING_SNAKE_CASE : List[str] = current_sum - array[i] + array[i + k] SCREAMING_SNAKE_CASE : Tuple = max(lowerCAmelCase__ , lowerCAmelCase__ ) return max_sum if __name__ == "__main__": from doctest import testmod from random import randint testmod() lowerCamelCase__ : Any = [randint(-1000, 1000) for i in range(100)] lowerCamelCase__ : str = randint(0, 110) print(f'''The maximum sum of {k} consecutive elements is {max_sum_in_array(array,k)}''')
714
"""simple docstring""" import math def __A ( a_ : list , a_ : int )-> int: '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[int] = len(a_ ) SCREAMING_SNAKE_CASE : Optional[Any] = int(math.floor(math.sqrt(a_ ) ) ) SCREAMING_SNAKE_CASE : List[str] = 0 while arr[min(a_ , a_ ) - 1] < x: SCREAMING_SNAKE_CASE : Optional[Any] = step step += int(math.floor(math.sqrt(a_ ) ) ) if prev >= n: return -1 while arr[prev] < x: SCREAMING_SNAKE_CASE : Any = prev + 1 if prev == min(a_ , a_ ): return -1 if arr[prev] == x: return prev return -1 if __name__ == "__main__": lowerCamelCase__ : Union[str, Any] = input("Enter numbers separated by a comma:\n").strip() lowerCamelCase__ : List[str] = [int(item) for item in user_input.split(",")] lowerCamelCase__ : Dict = int(input("Enter the number to be searched:\n")) lowerCamelCase__ : Tuple = jump_search(arr, x) if res == -1: print("Number not found!") else: print(f'''Number {x} is at index {res}''')
18
0
"""simple docstring""" import copy from collections import OrderedDict from typing import Dict, Mapping from packaging import version from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging from ..auto import CONFIG_MAPPING lowerCamelCase__ : Tuple = logging.get_logger(__name__) lowerCamelCase__ : Dict = { "facebook/detr-resnet-50": "https://huggingface.co/facebook/detr-resnet-50/resolve/main/config.json", # See all DETR models at https://huggingface.co/models?filter=detr } class lowercase__( __lowerCAmelCase ): '''simple docstring''' UpperCamelCase = """detr""" UpperCamelCase = ["""past_key_values"""] UpperCamelCase = { """hidden_size""": """d_model""", """num_attention_heads""": """encoder_attention_heads""", } def __init__( self :Any , lowerCamelCase_ :Dict=True , lowerCamelCase_ :List[Any]=None , lowerCamelCase_ :Any=3 , lowerCamelCase_ :Optional[Any]=1_00 , lowerCamelCase_ :int=6 , lowerCamelCase_ :Optional[Any]=20_48 , lowerCamelCase_ :Union[str, Any]=8 , lowerCamelCase_ :str=6 , lowerCamelCase_ :List[str]=20_48 , lowerCamelCase_ :Optional[int]=8 , lowerCamelCase_ :str=0.0 , lowerCamelCase_ :List[Any]=0.0 , lowerCamelCase_ :Any=True , lowerCamelCase_ :Optional[Any]="relu" , lowerCamelCase_ :Tuple=2_56 , lowerCamelCase_ :Any=0.1 , lowerCamelCase_ :Optional[Any]=0.0 , lowerCamelCase_ :Dict=0.0 , lowerCamelCase_ :Dict=0.0_2 , lowerCamelCase_ :Dict=1.0 , lowerCamelCase_ :Optional[int]=False , lowerCamelCase_ :Dict="sine" , lowerCamelCase_ :Dict="resnet50" , lowerCamelCase_ :Optional[int]=True , lowerCamelCase_ :Tuple=False , lowerCamelCase_ :str=1 , lowerCamelCase_ :int=5 , lowerCamelCase_ :Optional[int]=2 , lowerCamelCase_ :List[Any]=1 , lowerCamelCase_ :Union[str, Any]=1 , lowerCamelCase_ :str=5 , lowerCamelCase_ :List[Any]=2 , lowerCamelCase_ :List[str]=0.1 , **lowerCamelCase_ :Any , ) -> str: '''simple docstring''' if backbone_config is not None and use_timm_backbone: raise ValueError('''You can\'t specify both `backbone_config` and `use_timm_backbone`.''' ) if not use_timm_backbone: if backbone_config is None: logger.info('''`backbone_config` is `None`. Initializing the config with the default `ResNet` backbone.''' ) SCREAMING_SNAKE_CASE : Optional[Any] = CONFIG_MAPPING["""resnet"""](out_features=['''stage4'''] ) elif isinstance(_UpperCamelCase , _UpperCamelCase ): SCREAMING_SNAKE_CASE : Dict = backbone_config.get('''model_type''' ) SCREAMING_SNAKE_CASE : List[str] = CONFIG_MAPPING[backbone_model_type] SCREAMING_SNAKE_CASE : Tuple = config_class.from_dict(_UpperCamelCase ) # set timm attributes to None SCREAMING_SNAKE_CASE : Union[str, Any] = None, None, None SCREAMING_SNAKE_CASE : Optional[int] = use_timm_backbone SCREAMING_SNAKE_CASE : Union[str, Any] = backbone_config SCREAMING_SNAKE_CASE : Dict = num_channels SCREAMING_SNAKE_CASE : Optional[int] = num_queries SCREAMING_SNAKE_CASE : Tuple = d_model SCREAMING_SNAKE_CASE : Union[str, Any] = encoder_ffn_dim SCREAMING_SNAKE_CASE : Optional[Any] = encoder_layers SCREAMING_SNAKE_CASE : str = encoder_attention_heads SCREAMING_SNAKE_CASE : List[Any] = decoder_ffn_dim SCREAMING_SNAKE_CASE : List[str] = decoder_layers SCREAMING_SNAKE_CASE : Tuple = decoder_attention_heads SCREAMING_SNAKE_CASE : List[str] = dropout SCREAMING_SNAKE_CASE : Any = attention_dropout SCREAMING_SNAKE_CASE : List[str] = activation_dropout SCREAMING_SNAKE_CASE : Optional[int] = activation_function SCREAMING_SNAKE_CASE : List[Any] = init_std SCREAMING_SNAKE_CASE : Optional[Any] = init_xavier_std SCREAMING_SNAKE_CASE : Optional[Any] = encoder_layerdrop SCREAMING_SNAKE_CASE : Optional[int] = decoder_layerdrop SCREAMING_SNAKE_CASE : Union[str, Any] = encoder_layers SCREAMING_SNAKE_CASE : List[Any] = auxiliary_loss SCREAMING_SNAKE_CASE : int = position_embedding_type SCREAMING_SNAKE_CASE : int = backbone SCREAMING_SNAKE_CASE : Optional[int] = use_pretrained_backbone SCREAMING_SNAKE_CASE : List[str] = dilation # Hungarian matcher SCREAMING_SNAKE_CASE : int = class_cost SCREAMING_SNAKE_CASE : Optional[Any] = bbox_cost SCREAMING_SNAKE_CASE : int = giou_cost # Loss coefficients SCREAMING_SNAKE_CASE : str = mask_loss_coefficient SCREAMING_SNAKE_CASE : List[str] = dice_loss_coefficient SCREAMING_SNAKE_CASE : Dict = bbox_loss_coefficient SCREAMING_SNAKE_CASE : Optional[int] = giou_loss_coefficient SCREAMING_SNAKE_CASE : int = eos_coefficient super().__init__(is_encoder_decoder=_UpperCamelCase , **_UpperCamelCase ) @property def __lowerCAmelCase ( self :Union[str, Any] ) -> int: '''simple docstring''' return self.encoder_attention_heads @property def __lowerCAmelCase ( self :Dict ) -> int: '''simple docstring''' return self.d_model @classmethod def __lowerCAmelCase ( cls :List[str] , lowerCamelCase_ :PretrainedConfig , **lowerCamelCase_ :List[str] ) -> List[Any]: '''simple docstring''' return cls(backbone_config=_UpperCamelCase , **_UpperCamelCase ) def __lowerCAmelCase ( self :List[Any] ) -> Dict[str, any]: '''simple docstring''' SCREAMING_SNAKE_CASE : Tuple = copy.deepcopy(self.__dict__ ) if output["backbone_config"] is not None: SCREAMING_SNAKE_CASE : List[str] = self.backbone_config.to_dict() SCREAMING_SNAKE_CASE : List[Any] = self.__class__.model_type return output class lowercase__( __lowerCAmelCase ): '''simple docstring''' UpperCamelCase = version.parse("""1.11""" ) @property def __lowerCAmelCase ( self :Tuple ) -> Mapping[str, Mapping[int, str]]: '''simple docstring''' return OrderedDict( [ ('''pixel_values''', {0: '''batch''', 1: '''num_channels''', 2: '''height''', 3: '''width'''}), ('''pixel_mask''', {0: '''batch'''}), ] ) @property def __lowerCAmelCase ( self :int ) -> float: '''simple docstring''' return 1E-5 @property def __lowerCAmelCase ( self :List[str] ) -> int: '''simple docstring''' return 12
715
"""simple docstring""" from sklearn.metrics import fa_score import datasets lowerCamelCase__ : List[Any] = "\nThe F1 score is the harmonic mean of the precision and recall. It can be computed with the equation:\nF1 = 2 * (precision * recall) / (precision + recall)\n" lowerCamelCase__ : str = "\nArgs:\n predictions (`list` of `int`): Predicted labels.\n references (`list` of `int`): Ground truth labels.\n labels (`list` of `int`): The set of labels to include when `average` is not set to `'binary'`, and the order of the labels if `average` is `None`. Labels present in the data can be excluded, for example to calculate a multiclass average ignoring a majority negative class. Labels not present in the data will result in 0 components in a macro average. For multilabel targets, labels are column indices. By default, all labels in `predictions` and `references` are used in sorted order. Defaults to None.\n pos_label (`int`): The class to be considered the positive class, in the case where `average` is set to `binary`. Defaults to 1.\n average (`string`): This parameter is required for multiclass/multilabel targets. If set to `None`, the scores for each class are returned. Otherwise, this determines the type of averaging performed on the data. Defaults to `'binary'`.\n\n - 'binary': Only report results for the class specified by `pos_label`. This is applicable only if the classes found in `predictions` and `references` are binary.\n - 'micro': Calculate metrics globally by counting the total true positives, false negatives and false positives.\n - 'macro': Calculate metrics for each label, and find their unweighted mean. This does not take label imbalance into account.\n - 'weighted': Calculate metrics for each label, and find their average weighted by support (the number of true instances for each label). This alters `'macro'` to account for label imbalance. This option can result in an F-score that is not between precision and recall.\n - 'samples': Calculate metrics for each instance, and find their average (only meaningful for multilabel classification).\n sample_weight (`list` of `float`): Sample weights Defaults to None.\n\nReturns:\n f1 (`float` or `array` of `float`): F1 score or list of f1 scores, depending on the value passed to `average`. Minimum possible value is 0. Maximum possible value is 1. Higher f1 scores are better.\n\nExamples:\n\n Example 1-A simple binary example\n >>> f1_metric = datasets.load_metric(\"f1\")\n >>> results = f1_metric.compute(references=[0, 1, 0, 1, 0], predictions=[0, 0, 1, 1, 0])\n >>> print(results)\n {'f1': 0.5}\n\n Example 2-The same simple binary example as in Example 1, but with `pos_label` set to `0`.\n >>> f1_metric = datasets.load_metric(\"f1\")\n >>> results = f1_metric.compute(references=[0, 1, 0, 1, 0], predictions=[0, 0, 1, 1, 0], pos_label=0)\n >>> print(round(results['f1'], 2))\n 0.67\n\n Example 3-The same simple binary example as in Example 1, but with `sample_weight` included.\n >>> f1_metric = datasets.load_metric(\"f1\")\n >>> results = f1_metric.compute(references=[0, 1, 0, 1, 0], predictions=[0, 0, 1, 1, 0], sample_weight=[0.9, 0.5, 3.9, 1.2, 0.3])\n >>> print(round(results['f1'], 2))\n 0.35\n\n Example 4-A multiclass example, with different values for the `average` input.\n >>> predictions = [0, 2, 1, 0, 0, 1]\n >>> references = [0, 1, 2, 0, 1, 2]\n >>> results = f1_metric.compute(predictions=predictions, references=references, average=\"macro\")\n >>> print(round(results['f1'], 2))\n 0.27\n >>> results = f1_metric.compute(predictions=predictions, references=references, average=\"micro\")\n >>> print(round(results['f1'], 2))\n 0.33\n >>> results = f1_metric.compute(predictions=predictions, references=references, average=\"weighted\")\n >>> print(round(results['f1'], 2))\n 0.27\n >>> results = f1_metric.compute(predictions=predictions, references=references, average=None)\n >>> print(results)\n {'f1': array([0.8, 0. , 0. ])}\n" lowerCamelCase__ : int = "\n@article{scikit-learn,\n title={Scikit-learn: Machine Learning in {P}ython},\n author={Pedregosa, F. and Varoquaux, G. and Gramfort, A. and Michel, V.\n and Thirion, B. and Grisel, O. and Blondel, M. and Prettenhofer, P.\n and Weiss, R. and Dubourg, V. and Vanderplas, J. and Passos, A. and\n Cournapeau, D. and Brucher, M. and Perrot, M. and Duchesnay, E.},\n journal={Journal of Machine Learning Research},\n volume={12},\n pages={2825--2830},\n year={2011}\n}\n" @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class lowercase__( datasets.Metric ): '''simple docstring''' def __lowerCAmelCase ( self :str ) -> Any: '''simple docstring''' return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { '''predictions''': datasets.Sequence(datasets.Value('''int32''' ) ), '''references''': datasets.Sequence(datasets.Value('''int32''' ) ), } if self.config_name == '''multilabel''' else { '''predictions''': datasets.Value('''int32''' ), '''references''': datasets.Value('''int32''' ), } ) , reference_urls=['''https://scikit-learn.org/stable/modules/generated/sklearn.metrics.f1_score.html'''] , ) def __lowerCAmelCase ( self :Any , lowerCamelCase_ :Dict , lowerCamelCase_ :Optional[Any] , lowerCamelCase_ :int=None , lowerCamelCase_ :str=1 , lowerCamelCase_ :Union[str, Any]="binary" , lowerCamelCase_ :Dict=None ) -> int: '''simple docstring''' SCREAMING_SNAKE_CASE : int = fa_score( lowerCamelCase_ , lowerCamelCase_ , labels=lowerCamelCase_ , pos_label=lowerCamelCase_ , average=lowerCamelCase_ , sample_weight=lowerCamelCase_ ) return {"f1": float(lowerCamelCase_ ) if score.size == 1 else score}
18
0
"""simple docstring""" import logging from pathlib import Path import numpy as np import pytorch_lightning as pl import torch from pytorch_lightning.callbacks import EarlyStopping, ModelCheckpoint from pytorch_lightning.utilities import rank_zero_only from utils_rag import save_json def __A ( a_ : Optional[Any] )-> Dict: '''simple docstring''' SCREAMING_SNAKE_CASE : int = filter(lambda a_ : p.requires_grad , model.parameters() ) SCREAMING_SNAKE_CASE : Any = sum([np.prod(p.size() ) for p in model_parameters] ) return params lowerCamelCase__ : Dict = logging.getLogger(__name__) def __A ( a_ : Optional[Any] , a_ : Optional[int] )-> str: '''simple docstring''' if metric == "rouge2": SCREAMING_SNAKE_CASE : Dict = '{val_avg_rouge2:.4f}-{step_count}' elif metric == "bleu": SCREAMING_SNAKE_CASE : int = '{val_avg_bleu:.4f}-{step_count}' elif metric == "em": SCREAMING_SNAKE_CASE : List[str] = '{val_avg_em:.4f}-{step_count}' else: raise NotImplementedError( F"seq2seq callbacks only support rouge2 and bleu, got {metric}, You can make your own by adding to this" ''' function.''' ) SCREAMING_SNAKE_CASE : Union[str, Any] = ModelCheckpoint( dirpath=_lowerCamelCase , filename=_lowerCamelCase , monitor=F"val_{metric}" , mode='''max''' , save_top_k=3 , every_n_epochs=1 , ) return checkpoint_callback def __A ( a_ : str , a_ : List[Any] )-> Any: '''simple docstring''' return EarlyStopping( monitor=F"val_{metric}" , mode='''min''' if '''loss''' in metric else '''max''' , patience=_lowerCamelCase , verbose=_lowerCamelCase , ) class lowercase__( pl.Callback ): '''simple docstring''' def __lowerCAmelCase ( self :Tuple , lowerCamelCase_ :List[str] , lowerCamelCase_ :Union[str, Any] ) -> Tuple: '''simple docstring''' SCREAMING_SNAKE_CASE : str = {f"lr_group_{i}": param['lr'] for i, param in enumerate(pl_module.trainer.optimizers[0].param_groups )} pl_module.logger.log_metrics(_A ) @rank_zero_only def __lowerCAmelCase ( self :str , lowerCamelCase_ :Dict , lowerCamelCase_ :str , lowerCamelCase_ :Union[str, Any] , lowerCamelCase_ :Tuple=True ) -> Union[str, Any]: '''simple docstring''' logger.info(f"***** {type_path} results at step {trainer.global_step:05d} *****" ) SCREAMING_SNAKE_CASE : Union[str, Any] = trainer.callback_metrics trainer.logger.log_metrics({k: v for k, v in metrics.items() if k not in ['''log''', '''progress_bar''', '''preds''']} ) # Log results SCREAMING_SNAKE_CASE : List[str] = Path(pl_module.hparams.output_dir ) if type_path == "test": SCREAMING_SNAKE_CASE : str = od / 'test_results.txt' SCREAMING_SNAKE_CASE : Dict = od / 'test_generations.txt' else: # this never gets hit. I prefer not to save intermediate generations, and results are in metrics.json # If people want this it will be easy enough to add back. SCREAMING_SNAKE_CASE : Union[str, Any] = od / f"{type_path}_results/{trainer.global_step:05d}.txt" SCREAMING_SNAKE_CASE : Dict = od / f"{type_path}_generations/{trainer.global_step:05d}.txt" results_file.parent.mkdir(exist_ok=_A ) generations_file.parent.mkdir(exist_ok=_A ) with open(_A , '''a+''' ) as writer: for key in sorted(_A ): if key in ["log", "progress_bar", "preds"]: continue SCREAMING_SNAKE_CASE : Union[str, Any] = metrics[key] if isinstance(_A , torch.Tensor ): SCREAMING_SNAKE_CASE : int = val.item() SCREAMING_SNAKE_CASE : Optional[Any] = f"{key}: {val:.6f}\n" writer.write(_A ) if not save_generations: return if "preds" in metrics: SCREAMING_SNAKE_CASE : Optional[Any] = '\n'.join(metrics['''preds'''] ) generations_file.open('''w+''' ).write(_A ) @rank_zero_only def __lowerCAmelCase ( self :Optional[int] , lowerCamelCase_ :Optional[int] , lowerCamelCase_ :Union[str, Any] ) -> Optional[int]: '''simple docstring''' try: SCREAMING_SNAKE_CASE : Any = pl_module.model.model.num_parameters() except AttributeError: SCREAMING_SNAKE_CASE : Any = pl_module.model.num_parameters() SCREAMING_SNAKE_CASE : Optional[int] = count_trainable_parameters(_A ) # mp stands for million parameters trainer.logger.log_metrics({'''n_params''': npars, '''mp''': npars / 1E6, '''grad_mp''': n_trainable_pars / 1E6} ) @rank_zero_only def __lowerCAmelCase ( self :str , lowerCamelCase_ :Union[str, Any] , lowerCamelCase_ :Optional[int] ) -> Tuple: '''simple docstring''' save_json(pl_module.metrics , pl_module.metrics_save_path ) return self._write_logs(_A , _A , '''test''' ) @rank_zero_only def __lowerCAmelCase ( self :Any , lowerCamelCase_ :Dict , lowerCamelCase_ :Optional[int] ) -> Union[str, Any]: '''simple docstring''' save_json(pl_module.metrics , pl_module.metrics_save_path ) # Uncommenting this will save val generations # return self._write_logs(trainer, pl_module, "valid")
716
"""simple docstring""" from __future__ import annotations from fractions import Fraction def __A ( a_ : int , a_ : int )-> bool: '''simple docstring''' return ( num != den and num % 10 == den // 10 and (num // 10) / (den % 10) == num / den ) def __A ( a_ : int )-> list[str]: '''simple docstring''' SCREAMING_SNAKE_CASE : Union[str, Any] = [] SCREAMING_SNAKE_CASE : List[str] = 11 SCREAMING_SNAKE_CASE : Union[str, Any] = int('''1''' + '''0''' * digit_len ) for num in range(a_ , a_ ): while den <= 99: if (num != den) and (num % 10 == den // 10) and (den % 10 != 0): if is_digit_cancelling(a_ , a_ ): solutions.append(F"{num}/{den}" ) den += 1 num += 1 SCREAMING_SNAKE_CASE : Optional[Any] = 10 return solutions def __A ( a_ : int = 2 )-> int: '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[Any] = 1.0 for fraction in fraction_list(a_ ): SCREAMING_SNAKE_CASE : List[str] = Fraction(a_ ) result *= frac.denominator / frac.numerator return int(a_ ) if __name__ == "__main__": print(solution())
18
0
"""simple docstring""" def __A ( a_ : Tuple = 1_00_00_00 )-> Union[str, Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : str = [i - 1 for i in range(limit + 1 )] for i in range(2 , limit + 1 ): if phi[i] == i - 1: for j in range(2 * i , limit + 1 , _lowerCamelCase ): phi[j] -= phi[j] // i return sum(phi[2 : limit + 1] ) if __name__ == "__main__": print(solution())
717
"""simple docstring""" from ...configuration_utils import PretrainedConfig from ...utils import logging from ...utils.backbone_utils import BackboneConfigMixin, get_aligned_output_features_output_indices lowerCamelCase__ : int = logging.get_logger(__name__) class lowercase__( _UpperCAmelCase , _UpperCAmelCase ): '''simple docstring''' UpperCamelCase = """maskformer-swin""" UpperCamelCase = { """num_attention_heads""": """num_heads""", """num_hidden_layers""": """num_layers""", } def __init__( self :Optional[int] , lowerCamelCase_ :List[Any]=2_24 , lowerCamelCase_ :Tuple=4 , lowerCamelCase_ :Optional[Any]=3 , lowerCamelCase_ :List[str]=96 , lowerCamelCase_ :int=[2, 2, 6, 2] , lowerCamelCase_ :Union[str, Any]=[3, 6, 12, 24] , lowerCamelCase_ :Optional[int]=7 , lowerCamelCase_ :Tuple=4.0 , lowerCamelCase_ :Tuple=True , lowerCamelCase_ :Dict=0.0 , lowerCamelCase_ :Any=0.0 , lowerCamelCase_ :List[Any]=0.1 , lowerCamelCase_ :Dict="gelu" , lowerCamelCase_ :Optional[int]=False , lowerCamelCase_ :List[str]=0.0_2 , lowerCamelCase_ :Any=1E-5 , lowerCamelCase_ :Union[str, Any]=None , lowerCamelCase_ :List[str]=None , **lowerCamelCase_ :Union[str, Any] , ) -> Dict: '''simple docstring''' super().__init__(**lowerCamelCase_ ) SCREAMING_SNAKE_CASE : int = image_size SCREAMING_SNAKE_CASE : Optional[Any] = patch_size SCREAMING_SNAKE_CASE : str = num_channels SCREAMING_SNAKE_CASE : Union[str, Any] = embed_dim SCREAMING_SNAKE_CASE : List[Any] = depths SCREAMING_SNAKE_CASE : List[str] = len(lowerCamelCase_ ) SCREAMING_SNAKE_CASE : str = num_heads SCREAMING_SNAKE_CASE : Any = window_size SCREAMING_SNAKE_CASE : List[str] = mlp_ratio SCREAMING_SNAKE_CASE : str = qkv_bias SCREAMING_SNAKE_CASE : Tuple = hidden_dropout_prob SCREAMING_SNAKE_CASE : int = attention_probs_dropout_prob SCREAMING_SNAKE_CASE : int = drop_path_rate SCREAMING_SNAKE_CASE : Tuple = hidden_act SCREAMING_SNAKE_CASE : Any = use_absolute_embeddings SCREAMING_SNAKE_CASE : List[str] = layer_norm_eps SCREAMING_SNAKE_CASE : List[str] = initializer_range # we set the hidden_size attribute in order to make Swin work with VisionEncoderDecoderModel # this indicates the channel dimension after the last stage of the model SCREAMING_SNAKE_CASE : int = int(embed_dim * 2 ** (len(lowerCamelCase_ ) - 1) ) SCREAMING_SNAKE_CASE : Dict = ['''stem'''] + [f"stage{idx}" for idx in range(1 , len(lowerCamelCase_ ) + 1 )] SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : int = get_aligned_output_features_output_indices( out_features=lowerCamelCase_ , out_indices=lowerCamelCase_ , stage_names=self.stage_names )
18
0
"""simple docstring""" import datasets import faiss import numpy as np import streamlit as st import torch from elasticsearch import Elasticsearch from elia_utils import ( embed_questions_for_retrieval, make_qa_sas_model, qa_sas_generate, query_es_index, query_qa_dense_index, ) import transformers from transformers import AutoModel, AutoModelForSeqaSeqLM, AutoTokenizer lowerCamelCase__ : int = '''bart''' lowerCamelCase__ : str = True @st.cache(allow_output_mutation=_A ) def __A ( )-> str: '''simple docstring''' if LOAD_DENSE_INDEX: SCREAMING_SNAKE_CASE : Any = AutoTokenizer.from_pretrained('''yjernite/retribert-base-uncased''' ) SCREAMING_SNAKE_CASE : int = AutoModel.from_pretrained('''yjernite/retribert-base-uncased''' ).to('''cuda:0''' ) SCREAMING_SNAKE_CASE : int = qar_model.eval() else: SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : List[Any] = (None, None) if MODEL_TYPE == "bart": SCREAMING_SNAKE_CASE : Any = AutoTokenizer.from_pretrained('''yjernite/bart_eli5''' ) SCREAMING_SNAKE_CASE : List[str] = AutoModelForSeqaSeqLM.from_pretrained('''yjernite/bart_eli5''' ).to('''cuda:0''' ) SCREAMING_SNAKE_CASE : Union[str, Any] = torch.load('''seq2seq_models/eli5_bart_model_blm_2.pth''' ) sas_model.load_state_dict(save_dict['''model'''] ) SCREAMING_SNAKE_CASE : Tuple = sas_model.eval() else: SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : Dict = make_qa_sas_model( model_name='''t5-small''' , from_file='''seq2seq_models/eli5_t5_model_1024_4.pth''' , device='''cuda:0''' ) return (qar_tokenizer, qar_model, sas_tokenizer, sas_model) @st.cache(allow_output_mutation=_A ) def __A ( )-> Dict: '''simple docstring''' if LOAD_DENSE_INDEX: SCREAMING_SNAKE_CASE : Tuple = faiss.StandardGpuResources() SCREAMING_SNAKE_CASE : List[str] = datasets.load_dataset(path='''wiki_snippets''' , name='''wiki40b_en_100_0''' )['''train'''] SCREAMING_SNAKE_CASE : Tuple = np.memmap( '''wiki40b_passages_reps_32_l-8_h-768_b-512-512.dat''' , dtype='''float32''' , mode='''r''' , shape=(wikiaab_passages.num_rows, 1_28) , ) SCREAMING_SNAKE_CASE : List[str] = faiss.IndexFlatIP(1_28 ) SCREAMING_SNAKE_CASE : Tuple = faiss.index_cpu_to_gpu(_A , 1 , _A ) wikiaab_gpu_index_flat.add(_A ) # TODO fix for larger GPU else: SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : List[str] = (None, None) SCREAMING_SNAKE_CASE : List[str] = Elasticsearch([{'''host''': '''localhost''', '''port''': '''9200'''}] ) return (wikiaab_passages, wikiaab_gpu_index_flat, es_client) @st.cache(allow_output_mutation=_A ) def __A ( )-> List[str]: '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[Any] = datasets.load_dataset('''eli5''' , name='''LFQA_reddit''' ) SCREAMING_SNAKE_CASE : Optional[Any] = elia['''train_eli5'''] SCREAMING_SNAKE_CASE : Optional[int] = np.memmap( '''eli5_questions_reps.dat''' , dtype='''float32''' , mode='''r''' , shape=(elia_train.num_rows, 1_28) ) SCREAMING_SNAKE_CASE : int = faiss.IndexFlatIP(1_28 ) eli5_train_q_index.add(_A ) return (elia_train, eli5_train_q_index) lowerCamelCase__ : str = load_indexes() lowerCamelCase__ : Union[str, Any] = load_models() lowerCamelCase__ : Tuple = load_train_data() def __A ( a_ : Tuple , a_ : Tuple=10 )-> List[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : List[str] = embed_questions_for_retrieval([question] , _A , _A ) SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : Optional[int] = eli5_train_q_index.search(_A , _A ) SCREAMING_SNAKE_CASE : Union[str, Any] = [elia_train[int(_A )] for i in I[0]] return nn_examples def __A ( a_ : Optional[Any] , a_ : List[Any]="wiki40b" , a_ : str="dense" , a_ : Any=10 )-> List[Any]: '''simple docstring''' if source == "none": SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : Tuple = (''' <P> '''.join(['''''' for _ in range(11 )] ).strip(), []) else: if method == "dense": SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : Tuple = query_qa_dense_index( _A , _A , _A , _A , _A , _A ) else: SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : int = query_es_index( _A , _A , index_name='''english_wiki40b_snippets_100w''' , n_results=_A , ) SCREAMING_SNAKE_CASE : Dict = [ (res['''article_title'''], res['''section_title'''].strip(), res['''score'''], res['''passage_text''']) for res in hit_lst ] SCREAMING_SNAKE_CASE : Dict = '''question: {} context: {}'''.format(_A , _A ) return question_doc, support_list @st.cache( hash_funcs={ torch.Tensor: (lambda a_ : None), transformers.models.bart.tokenization_bart.BartTokenizer: (lambda a_ : None), } ) def __A ( a_ : List[Any] , a_ : List[Any] , a_ : List[Any] , a_ : Optional[int]=64 , a_ : Dict=2_56 , a_ : Optional[Any]=False , a_ : int=2 , a_ : Optional[int]=0.95 , a_ : List[str]=0.8 )-> Any: '''simple docstring''' with torch.no_grad(): SCREAMING_SNAKE_CASE : Dict = qa_sas_generate( _A , _A , _A , num_answers=1 , num_beams=_A , min_len=_A , max_len=_A , do_sample=_A , temp=_A , top_p=_A , top_k=_A , max_input_length=10_24 , device='''cuda:0''' , )[0] return (answer, support_list) st.title("Long Form Question Answering with ELI5") # Start sidebar lowerCamelCase__ : Optional[Any] = '''<img src=\'https://huggingface.co/front/assets/huggingface_logo.svg\'>''' lowerCamelCase__ : Optional[int] = ''' <html> <head> <style> .img-container { padding-left: 90px; padding-right: 90px; padding-top: 50px; padding-bottom: 50px; background-color: #f0f3f9; } </style> </head> <body> <span class="img-container"> <!-- Inline parent element --> %s </span> </body> </html> ''' % ( header_html, ) st.sidebar.markdown( header_full, unsafe_allow_html=True, ) # Long Form QA with ELI5 and Wikipedia lowerCamelCase__ : Any = ''' This demo presents a model trained to [provide long-form answers to open-domain questions](https://yjernite.github.io/lfqa.html). First, a document retriever fetches a set of relevant Wikipedia passages given the question from the [Wiki40b](https://research.google/pubs/pub49029/) dataset, a pre-processed fixed snapshot of Wikipedia. ''' st.sidebar.markdown(description, unsafe_allow_html=True) lowerCamelCase__ : Dict = [ '''Answer the question''', '''View the retrieved document only''', '''View the most similar ELI5 question and answer''', '''Show me everything, please!''', ] lowerCamelCase__ : Optional[Any] = st.sidebar.checkbox("Demo options") if demo_options: lowerCamelCase__ : List[str] = st.sidebar.selectbox( "", action_list, index=3, ) lowerCamelCase__ : Any = action_list.index(action_st) lowerCamelCase__ : List[Any] = st.sidebar.selectbox( "", ["Show full text of passages", "Show passage section titles"], index=0, ) lowerCamelCase__ : Optional[Any] = show_type == '''Show full text of passages''' else: lowerCamelCase__ : Optional[int] = 3 lowerCamelCase__ : Optional[Any] = True lowerCamelCase__ : Optional[Any] = st.sidebar.checkbox("Retrieval options") if retrieval_options: lowerCamelCase__ : Union[str, Any] = ''' ### Information retriever options The **sparse** retriever uses ElasticSearch, while the **dense** retriever uses max-inner-product search between a question and passage embedding trained using the [ELI5](https://arxiv.org/abs/1907.09190) questions-answer pairs. The answer is then generated by sequence to sequence model which takes the question and retrieved document as input. ''' st.sidebar.markdown(retriever_info) lowerCamelCase__ : Dict = st.sidebar.selectbox("Which Wikipedia format should the model use?", ["wiki40b", "none"]) lowerCamelCase__ : List[Any] = st.sidebar.selectbox("Which Wikipedia indexer should the model use?", ["dense", "sparse", "mixed"]) else: lowerCamelCase__ : int = '''wiki40b''' lowerCamelCase__ : Dict = '''dense''' lowerCamelCase__ : Any = '''beam''' lowerCamelCase__ : str = 2 lowerCamelCase__ : Tuple = 64 lowerCamelCase__ : Union[str, Any] = 256 lowerCamelCase__ : Optional[int] = None lowerCamelCase__ : Tuple = None lowerCamelCase__ : str = st.sidebar.checkbox("Generation options") if generate_options: lowerCamelCase__ : str = ''' ### Answer generation options The sequence-to-sequence model was initialized with [BART](https://huggingface.co/facebook/bart-large) weights and fine-tuned on the ELI5 QA pairs and retrieved documents. You can use the model for greedy decoding with **beam** search, or **sample** from the decoder\'s output probabilities. ''' st.sidebar.markdown(generate_info) lowerCamelCase__ : Dict = st.sidebar.selectbox("Would you like to use beam search or sample an answer?", ["beam", "sampled"]) lowerCamelCase__ : List[Any] = st.sidebar.slider( "Minimum generation length", min_value=8, max_value=256, value=64, step=8, format=None, key=None ) lowerCamelCase__ : Optional[int] = st.sidebar.slider( "Maximum generation length", min_value=64, max_value=512, value=256, step=16, format=None, key=None ) if sampled == "beam": lowerCamelCase__ : List[Any] = st.sidebar.slider("Beam size", min_value=1, max_value=8, value=2, step=None, format=None, key=None) else: lowerCamelCase__ : int = st.sidebar.slider( "Nucleus sampling p", min_value=0.1, max_value=1.0, value=0.9_5, step=0.0_1, format=None, key=None ) lowerCamelCase__ : Optional[Any] = st.sidebar.slider( "Temperature", min_value=0.1, max_value=1.0, value=0.7, step=0.0_1, format=None, key=None ) lowerCamelCase__ : Optional[Any] = None # start main text lowerCamelCase__ : Union[str, Any] = [ '''<MY QUESTION>''', '''How do people make chocolate?''', '''Why do we get a fever when we are sick?''', '''How can different animals perceive different colors?''', '''What is natural language processing?''', '''What\'s the best way to treat a sunburn?''', '''What exactly are vitamins ?''', '''How does nuclear energy provide electricity?''', '''What\'s the difference between viruses and bacteria?''', '''Why are flutes classified as woodwinds when most of them are made out of metal ?''', '''Why do people like drinking coffee even though it tastes so bad?''', '''What happens when wine ages? How does it make the wine taste better?''', '''If an animal is an herbivore, where does it get the protein that it needs to survive if it only eats grass?''', '''How can we set a date to the beginning or end of an artistic period? Doesn\'t the change happen gradually?''', '''How does New Zealand have so many large bird predators?''', ] lowerCamelCase__ : Dict = st.selectbox( "What would you like to ask? ---- select <MY QUESTION> to enter a new query", questions_list, index=1, ) if question_s == "<MY QUESTION>": lowerCamelCase__ : Union[str, Any] = st.text_input("Enter your question here:", "") else: lowerCamelCase__ : Tuple = question_s if st.button("Show me!"): if action in [0, 1, 3]: if index_type == "mixed": lowerCamelCase__ : List[Any] = make_support(question, source=wiki_source, method="dense", n_results=10) lowerCamelCase__ : Any = make_support(question, source=wiki_source, method="sparse", n_results=10) lowerCamelCase__ : Any = [] for res_d, res_s in zip(support_list_dense, support_list_sparse): if tuple(res_d) not in support_list: support_list += [tuple(res_d)] if tuple(res_s) not in support_list: support_list += [tuple(res_s)] lowerCamelCase__ : List[Any] = support_list[:10] lowerCamelCase__ : List[Any] = '''<P> ''' + ''' <P> '''.join([res[-1] for res in support_list]) else: lowerCamelCase__ : Any = make_support(question, source=wiki_source, method=index_type, n_results=10) if action in [0, 3]: lowerCamelCase__ : Any = answer_question( question_doc, sas_model, sas_tokenizer, min_len=min_len, max_len=int(max_len), sampling=(sampled == "sampled"), n_beams=n_beams, top_p=top_p, temp=temp, ) st.markdown("### The model generated answer is:") st.write(answer) if action in [0, 1, 3] and wiki_source != "none": st.markdown("--- \n ### The model is drawing information from the following Wikipedia passages:") for i, res in enumerate(support_list): lowerCamelCase__ : str = '''https://en.wikipedia.org/wiki/{}'''.format(res[0].replace(" ", "_")) lowerCamelCase__ : Tuple = res[1].strip() if sec_titles == "": lowerCamelCase__ : List[Any] = '''[{}]({})'''.format(res[0], wiki_url) else: lowerCamelCase__ : Union[str, Any] = sec_titles.split(" & ") lowerCamelCase__ : Optional[Any] = ''' & '''.join( ["[{}]({}#{})".format(sec.strip(), wiki_url, sec.strip().replace(" ", "_")) for sec in sec_list] ) st.markdown( "{0:02d} - **Article**: {1:<18} <br> _Section_: {2}".format(i + 1, res[0], sections), unsafe_allow_html=True, ) if show_passages: st.write( "> <span style=\"font-family:arial; font-size:10pt;\">" + res[-1] + "</span>", unsafe_allow_html=True ) if action in [2, 3]: lowerCamelCase__ : int = find_nearest_training(question) lowerCamelCase__ : int = nn_train_list[0] st.markdown( "--- \n ### The most similar question in the ELI5 training set was: \n\n {}".format(train_exple["title"]) ) lowerCamelCase__ : Union[str, Any] = [ '''{}. {}'''.format(i + 1, " \n".join([line.strip() for line in ans.split("\n") if line.strip() != ""])) for i, (ans, sc) in enumerate(zip(train_exple["answers"]["text"], train_exple["answers"]["score"])) if i == 0 or sc > 2 ] st.markdown("##### Its answers were: \n\n {}".format("\n".join(answers_st))) lowerCamelCase__ : Any = ''' --- **Disclaimer** *The intent of this app is to provide some (hopefully entertaining) insights into the behavior of a current LFQA system. Evaluating biases of such a model and ensuring factual generations are still very much open research problems. Therefore, until some significant progress is achieved, we caution against using the generated answers for practical purposes.* ''' st.sidebar.markdown(disclaimer, unsafe_allow_html=True)
718
"""simple docstring""" import math class lowercase__: '''simple docstring''' def __init__( self :Union[str, Any] , lowerCamelCase_ :List[str]=0 ) -> List[Any]: # a graph with Node 0,1,...,N-1 '''simple docstring''' SCREAMING_SNAKE_CASE : Tuple = n SCREAMING_SNAKE_CASE : List[Any] = [ [math.inf for j in range(0 , lowerCamelCase_ )] for i in range(0 , lowerCamelCase_ ) ] # adjacency matrix for weight SCREAMING_SNAKE_CASE : Any = [ [math.inf for j in range(0 , lowerCamelCase_ )] for i in range(0 , lowerCamelCase_ ) ] # dp[i][j] stores minimum distance from i to j def __lowerCAmelCase ( self :Dict , lowerCamelCase_ :Optional[int] , lowerCamelCase_ :Union[str, Any] , lowerCamelCase_ :Union[str, Any] ) -> Tuple: '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[Any] = w def __lowerCAmelCase ( self :str ) -> Union[str, Any]: '''simple docstring''' for k in range(0 , self.n ): for i in range(0 , self.n ): for j in range(0 , self.n ): SCREAMING_SNAKE_CASE : List[str] = min(self.dp[i][j] , self.dp[i][k] + self.dp[k][j] ) def __lowerCAmelCase ( self :int , lowerCamelCase_ :Union[str, Any] , lowerCamelCase_ :Optional[int] ) -> Optional[Any]: '''simple docstring''' return self.dp[u][v] if __name__ == "__main__": lowerCamelCase__ : Dict = Graph(5) graph.add_edge(0, 2, 9) graph.add_edge(0, 4, 10) graph.add_edge(1, 3, 5) graph.add_edge(2, 3, 7) graph.add_edge(3, 0, 10) graph.add_edge(3, 1, 2) graph.add_edge(3, 2, 1) graph.add_edge(3, 4, 6) graph.add_edge(4, 1, 3) graph.add_edge(4, 2, 4) graph.add_edge(4, 3, 9) graph.floyd_warshall() graph.show_min(1, 4) graph.show_min(0, 3)
18
0
"""simple docstring""" from typing import Mapping from ...configuration_utils import PretrainedConfig from ...onnx import OnnxSeqaSeqConfigWithPast from ...utils import logging lowerCamelCase__ : List[str] = logging.get_logger(__name__) lowerCamelCase__ : List[str] = { """t5-small""": """https://huggingface.co/t5-small/resolve/main/config.json""", """t5-base""": """https://huggingface.co/t5-base/resolve/main/config.json""", """t5-large""": """https://huggingface.co/t5-large/resolve/main/config.json""", """t5-3b""": """https://huggingface.co/t5-3b/resolve/main/config.json""", """t5-11b""": """https://huggingface.co/t5-11b/resolve/main/config.json""", } class lowercase__( _a ): '''simple docstring''' UpperCamelCase = """t5""" UpperCamelCase = ["""past_key_values"""] UpperCamelCase = {"""hidden_size""": """d_model""", """num_attention_heads""": """num_heads""", """num_hidden_layers""": """num_layers"""} def __init__( self :Tuple , lowerCamelCase_ :Tuple=3_21_28 , lowerCamelCase_ :Union[str, Any]=5_12 , lowerCamelCase_ :Optional[int]=64 , lowerCamelCase_ :Union[str, Any]=20_48 , lowerCamelCase_ :Any=6 , lowerCamelCase_ :str=None , lowerCamelCase_ :Dict=8 , lowerCamelCase_ :Optional[Any]=32 , lowerCamelCase_ :List[str]=1_28 , lowerCamelCase_ :Tuple=0.1 , lowerCamelCase_ :List[str]=1E-6 , lowerCamelCase_ :Any=1.0 , lowerCamelCase_ :Tuple="relu" , lowerCamelCase_ :List[Any]=True , lowerCamelCase_ :List[Any]=True , lowerCamelCase_ :Dict=0 , lowerCamelCase_ :Tuple=1 , **lowerCamelCase_ :Any , ) -> List[str]: '''simple docstring''' SCREAMING_SNAKE_CASE : Any = vocab_size SCREAMING_SNAKE_CASE : Dict = d_model SCREAMING_SNAKE_CASE : Any = d_kv SCREAMING_SNAKE_CASE : List[str] = d_ff SCREAMING_SNAKE_CASE : Tuple = num_layers SCREAMING_SNAKE_CASE : str = ( num_decoder_layers if num_decoder_layers is not None else self.num_layers ) # default = symmetry SCREAMING_SNAKE_CASE : Optional[Any] = num_heads SCREAMING_SNAKE_CASE : int = relative_attention_num_buckets SCREAMING_SNAKE_CASE : List[str] = relative_attention_max_distance SCREAMING_SNAKE_CASE : Dict = dropout_rate SCREAMING_SNAKE_CASE : List[str] = layer_norm_epsilon SCREAMING_SNAKE_CASE : Optional[int] = initializer_factor SCREAMING_SNAKE_CASE : Union[str, Any] = feed_forward_proj SCREAMING_SNAKE_CASE : Optional[Any] = use_cache SCREAMING_SNAKE_CASE : int = self.feed_forward_proj.split('''-''' ) SCREAMING_SNAKE_CASE : Union[str, Any] = act_info[-1] SCREAMING_SNAKE_CASE : Tuple = act_info[0] == '''gated''' if len(snake_case_ ) > 1 and act_info[0] != "gated" or len(snake_case_ ) > 2: raise ValueError( f"`feed_forward_proj`: {feed_forward_proj} is not a valid activation function of the dense layer." '''Please make sure `feed_forward_proj` is of the format `gated-{ACT_FN}` or `{ACT_FN}`, e.g. ''' '''\'gated-gelu\' or \'relu\'''' ) # for backwards compatibility if feed_forward_proj == "gated-gelu": SCREAMING_SNAKE_CASE : Union[str, Any] = '''gelu_new''' super().__init__( pad_token_id=snake_case_ , eos_token_id=snake_case_ , is_encoder_decoder=snake_case_ , **snake_case_ , ) class lowercase__( _a ): '''simple docstring''' @property def __lowerCAmelCase ( self :Tuple ) -> Mapping[str, Mapping[int, str]]: '''simple docstring''' SCREAMING_SNAKE_CASE : Union[str, Any] = { '''input_ids''': {0: '''batch''', 1: '''encoder_sequence'''}, '''attention_mask''': {0: '''batch''', 1: '''encoder_sequence'''}, } if self.use_past: SCREAMING_SNAKE_CASE : Dict = '''past_encoder_sequence + sequence''' SCREAMING_SNAKE_CASE : List[str] = {0: '''batch'''} SCREAMING_SNAKE_CASE : Optional[int] = {0: '''batch''', 1: '''past_decoder_sequence + sequence'''} else: SCREAMING_SNAKE_CASE : int = {0: '''batch''', 1: '''decoder_sequence'''} SCREAMING_SNAKE_CASE : List[Any] = {0: '''batch''', 1: '''decoder_sequence'''} if self.use_past: self.fill_with_past_key_values_(snake_case_ , direction='''inputs''' ) return common_inputs @property def __lowerCAmelCase ( self :Any ) -> int: '''simple docstring''' return 13
719
"""simple docstring""" from typing import TYPE_CHECKING from ....utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available lowerCamelCase__ : Tuple = { "configuration_mctct": ["MCTCT_PRETRAINED_CONFIG_ARCHIVE_MAP", "MCTCTConfig"], "feature_extraction_mctct": ["MCTCTFeatureExtractor"], "processing_mctct": ["MCTCTProcessor"], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCamelCase__ : Union[str, Any] = [ "MCTCT_PRETRAINED_MODEL_ARCHIVE_LIST", "MCTCTForCTC", "MCTCTModel", "MCTCTPreTrainedModel", ] if TYPE_CHECKING: from .configuration_mctct import MCTCT_PRETRAINED_CONFIG_ARCHIVE_MAP, MCTCTConfig from .feature_extraction_mctct import MCTCTFeatureExtractor from .processing_mctct import MCTCTProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_mctct import MCTCT_PRETRAINED_MODEL_ARCHIVE_LIST, MCTCTForCTC, MCTCTModel, MCTCTPreTrainedModel else: import sys lowerCamelCase__ : Tuple = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
18
0
"""simple docstring""" from typing import Any def __A ( a_ : list , a_ : list , a_ : dict , a_ : dict , a_ : dict , )-> Dict: '''simple docstring''' _validation( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , ) # Creates data structures and fill initial step SCREAMING_SNAKE_CASE : dict = {} SCREAMING_SNAKE_CASE : dict = {} for state in states_space: SCREAMING_SNAKE_CASE : List[str] = observations_space[0] SCREAMING_SNAKE_CASE : Optional[int] = ( initial_probabilities[state] * emission_probabilities[state][observation] ) SCREAMING_SNAKE_CASE : List[str] = None # Fills the data structure with the probabilities of # different transitions and pointers to previous states for o in range(1 , len(__UpperCamelCase ) ): SCREAMING_SNAKE_CASE : List[Any] = observations_space[o] SCREAMING_SNAKE_CASE : int = observations_space[o - 1] for state in states_space: # Calculates the argmax for probability function SCREAMING_SNAKE_CASE : int = """""" SCREAMING_SNAKE_CASE : List[str] = -1 for k_state in states_space: SCREAMING_SNAKE_CASE : List[str] = ( probabilities[(k_state, prior_observation)] * transition_probabilities[k_state][state] * emission_probabilities[state][observation] ) if probability > max_probability: SCREAMING_SNAKE_CASE : List[str] = probability SCREAMING_SNAKE_CASE : List[Any] = k_state # Update probabilities and pointers dicts SCREAMING_SNAKE_CASE : Union[str, Any] = ( probabilities[(arg_max, prior_observation)] * transition_probabilities[arg_max][state] * emission_probabilities[state][observation] ) SCREAMING_SNAKE_CASE : str = arg_max # The final observation SCREAMING_SNAKE_CASE : List[str] = observations_space[len(__UpperCamelCase ) - 1] # argmax for given final observation SCREAMING_SNAKE_CASE : int = """""" SCREAMING_SNAKE_CASE : Optional[int] = -1 for k_state in states_space: SCREAMING_SNAKE_CASE : List[str] = probabilities[(k_state, final_observation)] if probability > max_probability: SCREAMING_SNAKE_CASE : Any = probability SCREAMING_SNAKE_CASE : Tuple = k_state SCREAMING_SNAKE_CASE : Union[str, Any] = arg_max # Process pointers backwards SCREAMING_SNAKE_CASE : Tuple = last_state SCREAMING_SNAKE_CASE : Optional[Any] = [] for o in range(len(__UpperCamelCase ) - 1 , -1 , -1 ): result.append(__UpperCamelCase ) SCREAMING_SNAKE_CASE : Dict = pointers[previous, observations_space[o]] result.reverse() return result def __A ( a_ : Any , a_ : Any , a_ : Any , a_ : Any , a_ : Any , )-> Any: '''simple docstring''' _validate_not_empty( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , ) _validate_lists(__UpperCamelCase , __UpperCamelCase ) _validate_dicts( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) def __A ( a_ : Any , a_ : Any , a_ : Any , a_ : Any , a_ : Any , )-> Any: '''simple docstring''' if not all( [ observations_space, states_space, initial_probabilities, transition_probabilities, emission_probabilities, ] ): raise ValueError('''There\'s an empty parameter''' ) def __A ( a_ : Any , a_ : Any )-> Optional[int]: '''simple docstring''' _validate_list(__UpperCamelCase , '''observations_space''' ) _validate_list(__UpperCamelCase , '''states_space''' ) def __A ( a_ : Any , a_ : str )-> Union[str, Any]: '''simple docstring''' if not isinstance(_object , __UpperCamelCase ): SCREAMING_SNAKE_CASE : Any = F"{var_name} must be a list" raise ValueError(__UpperCamelCase ) else: for x in _object: if not isinstance(__UpperCamelCase , __UpperCamelCase ): SCREAMING_SNAKE_CASE : str = F"{var_name} must be a list of strings" raise ValueError(__UpperCamelCase ) def __A ( a_ : Any , a_ : Any , a_ : Any , )-> Optional[Any]: '''simple docstring''' _validate_dict(__UpperCamelCase , '''initial_probabilities''' , __UpperCamelCase ) _validate_nested_dict(__UpperCamelCase , '''transition_probabilities''' ) _validate_nested_dict(__UpperCamelCase , '''emission_probabilities''' ) def __A ( a_ : Any , a_ : str )-> Union[str, Any]: '''simple docstring''' _validate_dict(_object , __UpperCamelCase , __UpperCamelCase ) for x in _object.values(): _validate_dict(__UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) def __A ( a_ : Any , a_ : str , a_ : type , a_ : bool = False )-> Optional[int]: '''simple docstring''' if not isinstance(_object , __UpperCamelCase ): SCREAMING_SNAKE_CASE : str = F"{var_name} must be a dict" raise ValueError(__UpperCamelCase ) if not all(isinstance(__UpperCamelCase , __UpperCamelCase ) for x in _object ): SCREAMING_SNAKE_CASE : Any = F"{var_name} all keys must be strings" raise ValueError(__UpperCamelCase ) if not all(isinstance(__UpperCamelCase , __UpperCamelCase ) for x in _object.values() ): SCREAMING_SNAKE_CASE : int = """nested dictionary """ if nested else """""" SCREAMING_SNAKE_CASE : str = F"{var_name} {nested_text}all values must be {value_type.__name__}" raise ValueError(__UpperCamelCase ) if __name__ == "__main__": from doctest import testmod testmod()
720
"""simple docstring""" import os from typing import Any, Callable, Dict, List, Optional, Tuple, Union import torch from torch import nn from ...models.controlnet import ControlNetModel, ControlNetOutput from ...models.modeling_utils import ModelMixin from ...utils import logging lowerCamelCase__ : List[Any] = logging.get_logger(__name__) class lowercase__( _UpperCAmelCase ): '''simple docstring''' def __init__( self :List[str] , lowerCamelCase_ :Union[List[ControlNetModel], Tuple[ControlNetModel]] ) -> Union[str, Any]: '''simple docstring''' super().__init__() SCREAMING_SNAKE_CASE : Optional[Any] = nn.ModuleList(lowerCamelCase_ ) def __lowerCAmelCase ( self :Any , lowerCamelCase_ :torch.FloatTensor , lowerCamelCase_ :Union[torch.Tensor, float, int] , lowerCamelCase_ :torch.Tensor , lowerCamelCase_ :List[torch.tensor] , lowerCamelCase_ :List[float] , lowerCamelCase_ :Optional[torch.Tensor] = None , lowerCamelCase_ :Optional[torch.Tensor] = None , lowerCamelCase_ :Optional[torch.Tensor] = None , lowerCamelCase_ :Optional[Dict[str, Any]] = None , lowerCamelCase_ :bool = False , lowerCamelCase_ :bool = True , ) -> Union[ControlNetOutput, Tuple]: '''simple docstring''' for i, (image, scale, controlnet) in enumerate(zip(lowerCamelCase_ , lowerCamelCase_ , self.nets ) ): SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : List[Any] = controlnet( lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , ) # merge samples if i == 0: SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : int = down_samples, mid_sample else: SCREAMING_SNAKE_CASE : Optional[int] = [ samples_prev + samples_curr for samples_prev, samples_curr in zip(lowerCamelCase_ , lowerCamelCase_ ) ] mid_block_res_sample += mid_sample return down_block_res_samples, mid_block_res_sample def __lowerCAmelCase ( self :Any , lowerCamelCase_ :Union[str, os.PathLike] , lowerCamelCase_ :bool = True , lowerCamelCase_ :Callable = None , lowerCamelCase_ :bool = False , lowerCamelCase_ :Optional[str] = None , ) -> Optional[int]: '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[Any] = 0 SCREAMING_SNAKE_CASE : Any = save_directory for controlnet in self.nets: controlnet.save_pretrained( lowerCamelCase_ , is_main_process=lowerCamelCase_ , save_function=lowerCamelCase_ , safe_serialization=lowerCamelCase_ , variant=lowerCamelCase_ , ) idx += 1 SCREAMING_SNAKE_CASE : Union[str, Any] = model_path_to_save + f"_{idx}" @classmethod def __lowerCAmelCase ( cls :Dict , lowerCamelCase_ :Optional[Union[str, os.PathLike]] , **lowerCamelCase_ :Tuple ) -> List[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : str = 0 SCREAMING_SNAKE_CASE : Optional[int] = [] # load controlnet and append to list until no controlnet directory exists anymore # first controlnet has to be saved under `./mydirectory/controlnet` to be compliant with `DiffusionPipeline.from_prertained` # second, third, ... controlnets have to be saved under `./mydirectory/controlnet_1`, `./mydirectory/controlnet_2`, ... SCREAMING_SNAKE_CASE : Dict = pretrained_model_path while os.path.isdir(lowerCamelCase_ ): SCREAMING_SNAKE_CASE : Tuple = ControlNetModel.from_pretrained(lowerCamelCase_ , **lowerCamelCase_ ) controlnets.append(lowerCamelCase_ ) idx += 1 SCREAMING_SNAKE_CASE : Union[str, Any] = pretrained_model_path + f"_{idx}" logger.info(f"{len(lowerCamelCase_ )} controlnets loaded from {pretrained_model_path}." ) if len(lowerCamelCase_ ) == 0: raise ValueError( f"No ControlNets found under {os.path.dirname(lowerCamelCase_ )}. Expected at least {pretrained_model_path + '_0'}." ) return cls(lowerCamelCase_ )
18
0
"""simple docstring""" def __A ( a_ : list )-> List[Any]: '''simple docstring''' if len(snake_case__ ) <= 1: return lst SCREAMING_SNAKE_CASE : Union[str, Any] = 1 while i < len(snake_case__ ): if lst[i - 1] <= lst[i]: i += 1 else: SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : int = lst[i], lst[i - 1] i -= 1 if i == 0: SCREAMING_SNAKE_CASE : int = 1 return lst if __name__ == "__main__": lowerCamelCase__ : int = input("Enter numbers separated by a comma:\n").strip() lowerCamelCase__ : List[Any] = [int(item) for item in user_input.split(",")] print(gnome_sort(unsorted))
721
"""simple docstring""" from __future__ import annotations from numpy import array, cos, cross, floataa, radians, sin from numpy.typing import NDArray def __A ( a_ : float , a_ : float , a_ : bool = False )-> list[float]: '''simple docstring''' if radian_mode: return [magnitude * cos(a_ ), magnitude * sin(a_ )] return [magnitude * cos(radians(a_ ) ), magnitude * sin(radians(a_ ) )] def __A ( a_ : NDArray[floataa] , a_ : NDArray[floataa] , a_ : float = 10**-1 )-> bool: '''simple docstring''' SCREAMING_SNAKE_CASE : NDArray[floataa] = cross(a_ , a_ ) SCREAMING_SNAKE_CASE : float = sum(a_ ) return abs(a_ ) < eps if __name__ == "__main__": # Test to check if it works lowerCamelCase__ : Optional[Any] = array( [ polar_force(7_1_8.4, 180 - 30), polar_force(8_7_9.5_4, 45), polar_force(100, -90), ] ) lowerCamelCase__ : NDArray[floataa] = array([[0, 0], [0, 0], [0, 0]]) assert in_static_equilibrium(forces, location) # Problem 1 in image_data/2D_problems.jpg lowerCamelCase__ : Union[str, Any] = array( [ polar_force(30 * 9.8_1, 15), polar_force(215, 180 - 45), polar_force(264, 90 - 30), ] ) lowerCamelCase__ : Any = array([[0, 0], [0, 0], [0, 0]]) assert in_static_equilibrium(forces, location) # Problem in image_data/2D_problems_1.jpg lowerCamelCase__ : Union[str, Any] = array([[0, -2000], [0, -1200], [0, 15600], [0, -12400]]) lowerCamelCase__ : Optional[int] = array([[0, 0], [6, 0], [10, 0], [12, 0]]) assert in_static_equilibrium(forces, location) import doctest doctest.testmod()
18
0
"""simple docstring""" import gc import unittest from diffusers import FlaxDPMSolverMultistepScheduler, FlaxStableDiffusionPipeline from diffusers.utils import is_flax_available, slow from diffusers.utils.testing_utils import require_flax if is_flax_available(): import jax import jax.numpy as jnp from flax.jax_utils import replicate from flax.training.common_utils import shard @slow @require_flax class lowercase__( unittest.TestCase ): def __lowerCAmelCase ( self :str ) -> int: '''simple docstring''' super().tearDown() gc.collect() def __lowerCAmelCase ( self :str ) -> Tuple: '''simple docstring''' SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : List[Any] = FlaxStableDiffusionPipeline.from_pretrained( '''stabilityai/stable-diffusion-2''' , revision='''bf16''' , dtype=jnp.bfloataa , ) SCREAMING_SNAKE_CASE : Dict = '''A painting of a squirrel eating a burger''' SCREAMING_SNAKE_CASE : List[Any] = jax.device_count() SCREAMING_SNAKE_CASE : Dict = num_samples * [prompt] SCREAMING_SNAKE_CASE : Any = sd_pipe.prepare_inputs(lowerCamelCase_ ) SCREAMING_SNAKE_CASE : List[str] = replicate(lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Dict = shard(lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Dict = jax.random.PRNGKey(0 ) SCREAMING_SNAKE_CASE : Tuple = jax.random.split(lowerCamelCase_ , jax.device_count() ) SCREAMING_SNAKE_CASE : Union[str, Any] = sd_pipe(lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , num_inference_steps=25 , jit=lowerCamelCase_ )[0] assert images.shape == (jax.device_count(), 1, 7_68, 7_68, 3) SCREAMING_SNAKE_CASE : Optional[int] = images.reshape((images.shape[0] * images.shape[1],) + images.shape[-3:] ) SCREAMING_SNAKE_CASE : Any = images[0, 2_53:2_56, 2_53:2_56, -1] SCREAMING_SNAKE_CASE : Optional[int] = jnp.asarray(jax.device_get(image_slice.flatten() ) ) SCREAMING_SNAKE_CASE : Dict = jnp.array([0.4_2_3_8, 0.4_4_1_4, 0.4_3_9_5, 0.4_4_5_3, 0.4_6_2_9, 0.4_5_9_0, 0.4_5_3_1, 0.4_5_5_0_8, 0.4_5_1_2] ) print(f"output_slice: {output_slice}" ) assert jnp.abs(output_slice - expected_slice ).max() < 1E-2 def __lowerCAmelCase ( self :Tuple ) -> Any: '''simple docstring''' SCREAMING_SNAKE_CASE : List[str] = '''stabilityai/stable-diffusion-2''' SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : int = FlaxDPMSolverMultistepScheduler.from_pretrained(lowerCamelCase_ , subfolder='''scheduler''' ) SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : List[Any] = FlaxStableDiffusionPipeline.from_pretrained( lowerCamelCase_ , scheduler=lowerCamelCase_ , revision='''bf16''' , dtype=jnp.bfloataa , ) SCREAMING_SNAKE_CASE : Tuple = scheduler_params SCREAMING_SNAKE_CASE : Optional[int] = '''A painting of a squirrel eating a burger''' SCREAMING_SNAKE_CASE : Union[str, Any] = jax.device_count() SCREAMING_SNAKE_CASE : Tuple = num_samples * [prompt] SCREAMING_SNAKE_CASE : List[Any] = sd_pipe.prepare_inputs(lowerCamelCase_ ) SCREAMING_SNAKE_CASE : List[Any] = replicate(lowerCamelCase_ ) SCREAMING_SNAKE_CASE : int = shard(lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Dict = jax.random.PRNGKey(0 ) SCREAMING_SNAKE_CASE : Union[str, Any] = jax.random.split(lowerCamelCase_ , jax.device_count() ) SCREAMING_SNAKE_CASE : List[str] = sd_pipe(lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , num_inference_steps=25 , jit=lowerCamelCase_ )[0] assert images.shape == (jax.device_count(), 1, 7_68, 7_68, 3) SCREAMING_SNAKE_CASE : Optional[int] = images.reshape((images.shape[0] * images.shape[1],) + images.shape[-3:] ) SCREAMING_SNAKE_CASE : Optional[int] = images[0, 2_53:2_56, 2_53:2_56, -1] SCREAMING_SNAKE_CASE : str = jnp.asarray(jax.device_get(image_slice.flatten() ) ) SCREAMING_SNAKE_CASE : Union[str, Any] = jnp.array([0.4_3_3_6, 0.4_2_9_6_9, 0.4_4_5_3, 0.4_1_9_9, 0.4_2_9_7, 0.4_5_3_1, 0.4_4_3_4, 0.4_4_3_4, 0.4_2_9_7] ) print(f"output_slice: {output_slice}" ) assert jnp.abs(output_slice - expected_slice ).max() < 1E-2
700
"""simple docstring""" from __future__ import annotations import random # Maximum size of the population. Bigger could be faster but is more memory expensive. lowerCamelCase__ : Optional[Any] = 200 # Number of elements selected in every generation of evolution. The selection takes # place from best to worst of that generation and must be smaller than N_POPULATION. lowerCamelCase__ : Optional[int] = 50 # Probability that an element of a generation can mutate, changing one of its genes. # This will guarantee that all genes will be used during evolution. lowerCamelCase__ : Optional[Any] = 0.4 # Just a seed to improve randomness required by the algorithm. random.seed(random.randint(0, 1000)) def __A ( a_ : str , a_ : str )-> tuple[str, float]: '''simple docstring''' SCREAMING_SNAKE_CASE : Tuple = len([g for position, g in enumerate(a_ ) if g == main_target[position]] ) return (item, float(a_ )) def __A ( a_ : str , a_ : str )-> tuple[str, str]: '''simple docstring''' SCREAMING_SNAKE_CASE : Tuple = random.randint(0 , len(a_ ) - 1 ) SCREAMING_SNAKE_CASE : str = parent_a[:random_slice] + parent_a[random_slice:] SCREAMING_SNAKE_CASE : Dict = parent_a[:random_slice] + parent_a[random_slice:] return (child_a, child_a) def __A ( a_ : str , a_ : list[str] )-> str: '''simple docstring''' SCREAMING_SNAKE_CASE : Any = list(a_ ) if random.uniform(0 , 1 ) < MUTATION_PROBABILITY: SCREAMING_SNAKE_CASE : Any = random.choice(a_ ) return "".join(a_ ) def __A ( a_ : tuple[str, float] , a_ : list[tuple[str, float]] , a_ : list[str] , )-> list[str]: '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[int] = [] # Generate more children proportionally to the fitness score. SCREAMING_SNAKE_CASE : List[str] = int(parent_a[1] * 1_00 ) + 1 SCREAMING_SNAKE_CASE : Optional[Any] = 10 if child_n >= 10 else child_n for _ in range(a_ ): SCREAMING_SNAKE_CASE : List[str] = population_score[random.randint(0 , a_ )][0] SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : Tuple = crossover(parent_a[0] , a_ ) # Append new string to the population list. pop.append(mutate(a_ , a_ ) ) pop.append(mutate(a_ , a_ ) ) return pop def __A ( a_ : str , a_ : list[str] , a_ : bool = True )-> tuple[int, int, str]: '''simple docstring''' if N_POPULATION < N_SELECTED: SCREAMING_SNAKE_CASE : List[Any] = F"{N_POPULATION} must be bigger than {N_SELECTED}" raise ValueError(a_ ) # Verify that the target contains no genes besides the ones inside genes variable. SCREAMING_SNAKE_CASE : List[str] = sorted({c for c in target if c not in genes} ) if not_in_genes_list: SCREAMING_SNAKE_CASE : str = F"{not_in_genes_list} is not in genes list, evolution cannot converge" raise ValueError(a_ ) # Generate random starting population. SCREAMING_SNAKE_CASE : Tuple = [] for _ in range(a_ ): population.append(''''''.join([random.choice(a_ ) for i in range(len(a_ ) )] ) ) # Just some logs to know what the algorithms is doing. SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : List[str] = 0, 0 # This loop will end when we find a perfect match for our target. while True: generation += 1 total_population += len(a_ ) # Random population created. Now it's time to evaluate. # Adding a bit of concurrency can make everything faster, # # import concurrent.futures # population_score: list[tuple[str, float]] = [] # with concurrent.futures.ThreadPoolExecutor( # max_workers=NUM_WORKERS) as executor: # futures = {executor.submit(evaluate, item) for item in population} # concurrent.futures.wait(futures) # population_score = [item.result() for item in futures] # # but with a simple algorithm like this, it will probably be slower. # We just need to call evaluate for every item inside the population. SCREAMING_SNAKE_CASE : int = [evaluate(a_ , a_ ) for item in population] # Check if there is a matching evolution. SCREAMING_SNAKE_CASE : List[Any] = sorted(a_ , key=lambda a_ : x[1] , reverse=a_ ) if population_score[0][0] == target: return (generation, total_population, population_score[0][0]) # Print the best result every 10 generation. # Just to know that the algorithm is working. if debug and generation % 10 == 0: print( F"\nGeneration: {generation}" F"\nTotal Population:{total_population}" F"\nBest score: {population_score[0][1]}" F"\nBest string: {population_score[0][0]}" ) # Flush the old population, keeping some of the best evolutions. # Keeping this avoid regression of evolution. SCREAMING_SNAKE_CASE : Optional[Any] = population[: int(N_POPULATION / 3 )] population.clear() population.extend(a_ ) # Normalize population score to be between 0 and 1. SCREAMING_SNAKE_CASE : Optional[int] = [ (item, score / len(a_ )) for item, score in population_score ] # This is selection for i in range(a_ ): population.extend(select(population_score[int(a_ )] , a_ , a_ ) ) # Check if the population has already reached the maximum value and if so, # break the cycle. If this check is disabled, the algorithm will take # forever to compute large strings, but will also calculate small strings in # a far fewer generations. if len(a_ ) > N_POPULATION: break if __name__ == "__main__": lowerCamelCase__ : Dict = ( "This is a genetic algorithm to evaluate, combine, evolve, and mutate a string!" ) lowerCamelCase__ : int = list( " ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklm" "nopqrstuvwxyz.,;!?+-*#@^'èéòà€ù=)(&%$£/\\" ) lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ : Dict = basic(target_str, genes_list) print( f'''\nGeneration: {generation}\nTotal Population: {population}\nTarget: {target}''' )
18
0
"""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 lowerCamelCase__ : List[Any] = logging.get_logger(__name__) lowerCamelCase__ : 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 lowercase__( _UpperCAmelCase ): '''simple docstring''' UpperCamelCase = """marian""" UpperCamelCase = ["""past_key_values"""] UpperCamelCase = {"""num_attention_heads""": """encoder_attention_heads""", """hidden_size""": """d_model"""} def __init__( self :Dict , lowerCamelCase_ :Optional[int]=5_81_01 , lowerCamelCase_ :List[str]=None , lowerCamelCase_ :Optional[Any]=10_24 , lowerCamelCase_ :Optional[Any]=12 , lowerCamelCase_ :int=40_96 , lowerCamelCase_ :Dict=16 , lowerCamelCase_ :Optional[Any]=12 , lowerCamelCase_ :str=40_96 , lowerCamelCase_ :str=16 , lowerCamelCase_ :List[Any]=0.0 , lowerCamelCase_ :int=0.0 , lowerCamelCase_ :Union[str, Any]=True , lowerCamelCase_ :Any=True , lowerCamelCase_ :Union[str, Any]="gelu" , lowerCamelCase_ :Union[str, Any]=10_24 , lowerCamelCase_ :Optional[int]=0.1 , lowerCamelCase_ :Optional[Any]=0.0 , lowerCamelCase_ :Any=0.0 , lowerCamelCase_ :Any=0.0_2 , lowerCamelCase_ :List[str]=5_81_00 , lowerCamelCase_ :Union[str, Any]=False , lowerCamelCase_ :int=5_81_00 , lowerCamelCase_ :Any=0 , lowerCamelCase_ :List[str]=0 , lowerCamelCase_ :Tuple=True , **lowerCamelCase_ :Dict , ) -> Dict: '''simple docstring''' SCREAMING_SNAKE_CASE : Union[str, Any] = vocab_size SCREAMING_SNAKE_CASE : Dict = decoder_vocab_size or vocab_size SCREAMING_SNAKE_CASE : Any = max_position_embeddings SCREAMING_SNAKE_CASE : Tuple = d_model SCREAMING_SNAKE_CASE : Union[str, Any] = encoder_ffn_dim SCREAMING_SNAKE_CASE : List[Any] = encoder_layers SCREAMING_SNAKE_CASE : Dict = encoder_attention_heads SCREAMING_SNAKE_CASE : int = decoder_ffn_dim SCREAMING_SNAKE_CASE : List[Any] = decoder_layers SCREAMING_SNAKE_CASE : Optional[Any] = decoder_attention_heads SCREAMING_SNAKE_CASE : Dict = dropout SCREAMING_SNAKE_CASE : str = attention_dropout SCREAMING_SNAKE_CASE : Any = activation_dropout SCREAMING_SNAKE_CASE : int = activation_function SCREAMING_SNAKE_CASE : Any = init_std SCREAMING_SNAKE_CASE : Optional[Any] = encoder_layerdrop SCREAMING_SNAKE_CASE : Optional[int] = decoder_layerdrop SCREAMING_SNAKE_CASE : List[Any] = use_cache SCREAMING_SNAKE_CASE : List[Any] = encoder_layers SCREAMING_SNAKE_CASE : int = scale_embedding # scale factor will be sqrt(d_model) if True SCREAMING_SNAKE_CASE : List[str] = share_encoder_decoder_embeddings super().__init__( pad_token_id=lowerCamelCase_ , eos_token_id=lowerCamelCase_ , is_encoder_decoder=lowerCamelCase_ , decoder_start_token_id=lowerCamelCase_ , forced_eos_token_id=lowerCamelCase_ , **lowerCamelCase_ , ) class lowercase__( _UpperCAmelCase ): '''simple docstring''' @property # Copied from transformers.models.bart.configuration_bart.BartOnnxConfig.inputs def __lowerCAmelCase ( self :Optional[Any] ) -> Mapping[str, Mapping[int, str]]: '''simple docstring''' if self.task in ["default", "seq2seq-lm"]: SCREAMING_SNAKE_CASE : List[str] = OrderedDict( [ ('''input_ids''', {0: '''batch''', 1: '''encoder_sequence'''}), ('''attention_mask''', {0: '''batch''', 1: '''encoder_sequence'''}), ] ) if self.use_past: SCREAMING_SNAKE_CASE : List[str] = {0: '''batch'''} SCREAMING_SNAKE_CASE : Any = {0: '''batch''', 1: '''past_decoder_sequence + sequence'''} else: SCREAMING_SNAKE_CASE : List[str] = {0: '''batch''', 1: '''decoder_sequence'''} SCREAMING_SNAKE_CASE : Optional[int] = {0: '''batch''', 1: '''decoder_sequence'''} if self.use_past: self.fill_with_past_key_values_(lowerCamelCase_ , direction='''inputs''' ) elif self.task == "causal-lm": # TODO: figure this case out. SCREAMING_SNAKE_CASE : Optional[Any] = OrderedDict( [ ('''input_ids''', {0: '''batch''', 1: '''encoder_sequence'''}), ('''attention_mask''', {0: '''batch''', 1: '''encoder_sequence'''}), ] ) if self.use_past: SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : Optional[int] = self.num_layers for i in range(lowerCamelCase_ ): SCREAMING_SNAKE_CASE : Dict = {0: '''batch''', 2: '''past_sequence + sequence'''} SCREAMING_SNAKE_CASE : List[Any] = {0: '''batch''', 2: '''past_sequence + sequence'''} else: SCREAMING_SNAKE_CASE : int = 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[Any] ) -> Mapping[str, Mapping[int, str]]: '''simple docstring''' if self.task in ["default", "seq2seq-lm"]: SCREAMING_SNAKE_CASE : Union[str, Any] = super().outputs else: SCREAMING_SNAKE_CASE : Dict = super(lowerCamelCase_ , self ).outputs if self.use_past: SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : str = self.num_layers for i in range(lowerCamelCase_ ): SCREAMING_SNAKE_CASE : List[Any] = {0: '''batch''', 2: '''past_sequence + sequence'''} SCREAMING_SNAKE_CASE : Optional[Any] = {0: '''batch''', 2: '''past_sequence + sequence'''} return common_outputs def __lowerCAmelCase ( self :Dict , lowerCamelCase_ :Any , lowerCamelCase_ :int = -1 , lowerCamelCase_ :List[Any] = -1 , lowerCamelCase_ :Union[str, Any] = False , lowerCamelCase_ :Any = None , ) -> Mapping[str, Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : List[Any] = self._generate_dummy_inputs_for_encoder_and_decoder( lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ ) # Generate decoder inputs SCREAMING_SNAKE_CASE : int = seq_length if not self.use_past else 1 SCREAMING_SNAKE_CASE : Optional[int] = self._generate_dummy_inputs_for_encoder_and_decoder( lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ ) SCREAMING_SNAKE_CASE : List[str] = {f"decoder_{name}": tensor for name, tensor in decoder_inputs.items()} SCREAMING_SNAKE_CASE : Optional[Any] = dict(**lowerCamelCase_ , **lowerCamelCase_ ) if self.use_past: if not is_torch_available(): raise ValueError('''Cannot generate dummy past_keys inputs without PyTorch installed.''' ) else: import torch SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : int = common_inputs['''input_ids'''].shape SCREAMING_SNAKE_CASE : Optional[Any] = common_inputs['''decoder_input_ids'''].shape[1] SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : List[Any] = self.num_attention_heads SCREAMING_SNAKE_CASE : int = ( batch, num_encoder_attention_heads, encoder_seq_length, self._config.hidden_size // num_encoder_attention_heads, ) SCREAMING_SNAKE_CASE : Optional[Any] = decoder_seq_length + 3 SCREAMING_SNAKE_CASE : Tuple = ( batch, num_decoder_attention_heads, decoder_past_length, self._config.hidden_size // num_decoder_attention_heads, ) SCREAMING_SNAKE_CASE : List[Any] = torch.cat( [common_inputs['''decoder_attention_mask'''], torch.ones(lowerCamelCase_ , lowerCamelCase_ )] , dim=1 ) SCREAMING_SNAKE_CASE : Optional[Any] = [] # If the number of encoder and decoder layers are present in the model configuration, both are considered SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : Optional[Any] = self.num_layers SCREAMING_SNAKE_CASE : Optional[int] = min(lowerCamelCase_ , lowerCamelCase_ ) SCREAMING_SNAKE_CASE : int = max(lowerCamelCase_ , lowerCamelCase_ ) - min_num_layers SCREAMING_SNAKE_CASE : List[str] = '''encoder''' if num_encoder_layers > num_decoder_layers else '''decoder''' for _ in range(lowerCamelCase_ ): common_inputs["past_key_values"].append( ( torch.zeros(lowerCamelCase_ ), torch.zeros(lowerCamelCase_ ), torch.zeros(lowerCamelCase_ ), torch.zeros(lowerCamelCase_ ), ) ) # TODO: test this. SCREAMING_SNAKE_CASE : Tuple = encoder_shape if remaining_side_name == '''encoder''' else decoder_shape for _ in range(lowerCamelCase_ , lowerCamelCase_ ): common_inputs["past_key_values"].append((torch.zeros(lowerCamelCase_ ), torch.zeros(lowerCamelCase_ )) ) return common_inputs def __lowerCAmelCase ( self :Union[str, Any] , lowerCamelCase_ :Any , lowerCamelCase_ :Union[str, Any] = -1 , lowerCamelCase_ :Union[str, Any] = -1 , lowerCamelCase_ :List[str] = False , lowerCamelCase_ :Any = None , ) -> Mapping[str, Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[Any] = self._generate_dummy_inputs_for_encoder_and_decoder( lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ ) if self.use_past: if not is_torch_available(): raise ValueError('''Cannot generate dummy past_keys inputs without PyTorch installed.''' ) else: import torch SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : Dict = common_inputs['''input_ids'''].shape # Not using the same length for past_key_values SCREAMING_SNAKE_CASE : Dict = seqlen + 2 SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : str = self.num_layers SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : Optional[int] = self.num_attention_heads SCREAMING_SNAKE_CASE : Optional[Any] = ( batch, num_encoder_attention_heads, past_key_values_length, self._config.hidden_size // num_encoder_attention_heads, ) SCREAMING_SNAKE_CASE : List[Any] = common_inputs['''attention_mask'''].dtype SCREAMING_SNAKE_CASE : Any = torch.cat( [common_inputs['''attention_mask'''], torch.ones(lowerCamelCase_ , lowerCamelCase_ , dtype=lowerCamelCase_ )] , dim=1 ) SCREAMING_SNAKE_CASE : Any = [ (torch.zeros(lowerCamelCase_ ), torch.zeros(lowerCamelCase_ )) for _ in range(lowerCamelCase_ ) ] return common_inputs def __lowerCAmelCase ( self :Any , lowerCamelCase_ :Tuple , lowerCamelCase_ :Dict = -1 , lowerCamelCase_ :Optional[int] = -1 , lowerCamelCase_ :Union[str, Any] = False , lowerCamelCase_ :str = None , ) -> Mapping[str, Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : Dict = compute_effective_axis_dimension( lowerCamelCase_ , fixed_dimension=OnnxConfig.default_fixed_batch , num_token_to_add=0 ) # If dynamic axis (-1) we forward with a fixed dimension of 8 tokens to avoid optimizations made by ONNX SCREAMING_SNAKE_CASE : Union[str, Any] = tokenizer.num_special_tokens_to_add(lowerCamelCase_ ) SCREAMING_SNAKE_CASE : str = compute_effective_axis_dimension( lowerCamelCase_ , fixed_dimension=OnnxConfig.default_fixed_sequence , num_token_to_add=lowerCamelCase_ ) # Generate dummy inputs according to compute batch and sequence SCREAMING_SNAKE_CASE : Optional[Any] = [''' '''.join([tokenizer.unk_token] ) * seq_length] * batch_size SCREAMING_SNAKE_CASE : Any = dict(tokenizer(lowerCamelCase_ , return_tensors=lowerCamelCase_ ) ) return common_inputs def __lowerCAmelCase ( self :List[Any] , lowerCamelCase_ :Optional[int] , lowerCamelCase_ :Optional[Any] = -1 , lowerCamelCase_ :str = -1 , lowerCamelCase_ :List[str] = False , lowerCamelCase_ :Any = None , ) -> Mapping[str, Any]: '''simple docstring''' if self.task in ["default", "seq2seq-lm"]: SCREAMING_SNAKE_CASE : Optional[Any] = self._generate_dummy_inputs_for_default_and_seqaseq_lm( lowerCamelCase_ , batch_size=lowerCamelCase_ , seq_length=lowerCamelCase_ , is_pair=lowerCamelCase_ , framework=lowerCamelCase_ ) else: SCREAMING_SNAKE_CASE : str = self._generate_dummy_inputs_for_causal_lm( lowerCamelCase_ , batch_size=lowerCamelCase_ , seq_length=lowerCamelCase_ , is_pair=lowerCamelCase_ , framework=lowerCamelCase_ ) return common_inputs def __lowerCAmelCase ( self :Optional[Any] , lowerCamelCase_ :Optional[Any] , lowerCamelCase_ :int , lowerCamelCase_ :int , lowerCamelCase_ :Tuple ) -> str: '''simple docstring''' if self.task in ["default", "seq2seq-lm"]: SCREAMING_SNAKE_CASE : Optional[Any] = super()._flatten_past_key_values_(lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ ) else: SCREAMING_SNAKE_CASE : Tuple = super(lowerCamelCase_ , self )._flatten_past_key_values_( lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ ) @property def __lowerCAmelCase ( self :Optional[Any] ) -> float: '''simple docstring''' return 1E-4
701
"""simple docstring""" import argparse import ast import logging import os import sys import pandas as pd import torch from tqdm import tqdm from transformers import BartForConditionalGeneration, RagRetriever, RagSequenceForGeneration, RagTokenForGeneration from transformers import logging as transformers_logging sys.path.append(os.path.join(os.getcwd())) # noqa: E402 # isort:skip from utils_rag import exact_match_score, fa_score # noqa: E402 # isort:skip lowerCamelCase__ : Optional[Any] = logging.getLogger(__name__) logging.basicConfig(level=logging.INFO) transformers_logging.set_verbosity_info() def __A ( a_ : Optional[int] )-> Dict: '''simple docstring''' if "token" in model_name_or_path: return "rag_token" if "sequence" in model_name_or_path: return "rag_sequence" if "bart" in model_name_or_path: return "bart" return None def __A ( a_ : List[Any] , a_ : Optional[int] , a_ : Optional[int] )-> Dict: '''simple docstring''' return max(metric_fn(a_ , a_ ) for gt in ground_truths ) def __A ( a_ : List[Any] , a_ : Union[str, Any] , a_ : str )-> str: '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[int] = [line.strip() for line in open(a_ , '''r''' ).readlines()] SCREAMING_SNAKE_CASE : Optional[Any] = [] if args.gold_data_mode == "qa": SCREAMING_SNAKE_CASE : List[Any] = pd.read_csv(a_ , sep='''\t''' , header=a_ ) for answer_list in data[1]: SCREAMING_SNAKE_CASE : str = ast.literal_eval(a_ ) answers.append(a_ ) else: SCREAMING_SNAKE_CASE : Any = [line.strip() for line in open(a_ , '''r''' ).readlines()] SCREAMING_SNAKE_CASE : Dict = [[reference] for reference in references] SCREAMING_SNAKE_CASE : Dict = 0 for prediction, ground_truths in zip(a_ , a_ ): total += 1 em += metric_max_over_ground_truths(a_ , a_ , a_ ) fa += metric_max_over_ground_truths(a_ , a_ , a_ ) SCREAMING_SNAKE_CASE : Any = 100.0 * em / total SCREAMING_SNAKE_CASE : Optional[int] = 100.0 * fa / total logger.info(F"F1: {fa:.2f}" ) logger.info(F"EM: {em:.2f}" ) def __A ( a_ : Any , a_ : Any , a_ : List[Any] )-> Tuple: '''simple docstring''' SCREAMING_SNAKE_CASE : str = args.k SCREAMING_SNAKE_CASE : Tuple = [line.strip() for line in open(a_ , '''r''' ).readlines()] SCREAMING_SNAKE_CASE : Union[str, Any] = [line.strip() for line in open(a_ , '''r''' ).readlines()] SCREAMING_SNAKE_CASE : Dict = 0 for hypo, reference in zip(a_ , a_ ): SCREAMING_SNAKE_CASE : Optional[int] = set(hypo.split('''\t''' )[:k] ) SCREAMING_SNAKE_CASE : List[str] = set(reference.split('''\t''' ) ) total += 1 em += len(hypo_provenance & ref_provenance ) / k SCREAMING_SNAKE_CASE : Dict = 100.0 * em / total logger.info(F"Precision@{k}: {em: .2f}" ) def __A ( a_ : Any , a_ : List[str] , a_ : str )-> int: '''simple docstring''' def strip_title(a_ : Optional[Any] ): if title.startswith('''"''' ): SCREAMING_SNAKE_CASE : Tuple = title[1:] if title.endswith('''"''' ): SCREAMING_SNAKE_CASE : Any = title[:-1] return title SCREAMING_SNAKE_CASE : Tuple = rag_model.retriever.question_encoder_tokenizer.batch_encode_plus( a_ , return_tensors='''pt''' , padding=a_ , truncation=a_ , )['''input_ids'''].to(args.device ) SCREAMING_SNAKE_CASE : Any = rag_model.rag.question_encoder(a_ ) SCREAMING_SNAKE_CASE : Union[str, Any] = question_enc_outputs[0] SCREAMING_SNAKE_CASE : Dict = rag_model.retriever( a_ , question_enc_pool_output.cpu().detach().to(torch.floataa ).numpy() , prefix=rag_model.rag.generator.config.prefix , n_docs=rag_model.config.n_docs , return_tensors='''pt''' , ) SCREAMING_SNAKE_CASE : Any = rag_model.retriever.index.get_doc_dicts(result.doc_ids ) SCREAMING_SNAKE_CASE : Dict = [] for docs in all_docs: SCREAMING_SNAKE_CASE : List[Any] = [strip_title(a_ ) for title in docs['''title''']] provenance_strings.append('''\t'''.join(a_ ) ) return provenance_strings def __A ( a_ : List[Any] , a_ : int , a_ : str )-> Tuple: '''simple docstring''' with torch.no_grad(): SCREAMING_SNAKE_CASE : Optional[int] = rag_model.retriever.question_encoder_tokenizer.batch_encode_plus( a_ , return_tensors='''pt''' , padding=a_ , truncation=a_ ) SCREAMING_SNAKE_CASE : Dict = inputs_dict.input_ids.to(args.device ) SCREAMING_SNAKE_CASE : Any = inputs_dict.attention_mask.to(args.device ) SCREAMING_SNAKE_CASE : Tuple = rag_model.generate( # rag_model overwrites generate a_ , attention_mask=a_ , num_beams=args.num_beams , min_length=args.min_length , max_length=args.max_length , early_stopping=a_ , num_return_sequences=1 , bad_words_ids=[[0, 0]] , ) SCREAMING_SNAKE_CASE : Dict = rag_model.retriever.generator_tokenizer.batch_decode(a_ , skip_special_tokens=a_ ) if args.print_predictions: for q, a in zip(a_ , a_ ): logger.info('''Q: {} - A: {}'''.format(a_ , a_ ) ) return answers def __A ( )-> int: '''simple docstring''' SCREAMING_SNAKE_CASE : int = argparse.ArgumentParser() parser.add_argument( '''--model_type''' , choices=['''rag_sequence''', '''rag_token''', '''bart'''] , type=a_ , help=( '''RAG model type: rag_sequence, rag_token or bart, if none specified, the type is inferred from the''' ''' model_name_or_path''' ) , ) parser.add_argument( '''--index_name''' , default=a_ , choices=['''exact''', '''compressed''', '''legacy'''] , type=a_ , help='''RAG model retriever type''' , ) parser.add_argument( '''--index_path''' , default=a_ , type=a_ , help='''Path to the retrieval index''' , ) parser.add_argument('''--n_docs''' , default=5 , type=a_ , help='''Number of retrieved docs''' ) parser.add_argument( '''--model_name_or_path''' , default=a_ , type=a_ , required=a_ , help='''Path to pretrained checkpoints or model identifier from huggingface.co/models''' , ) parser.add_argument( '''--eval_mode''' , choices=['''e2e''', '''retrieval'''] , default='''e2e''' , type=a_ , help=( '''Evaluation mode, e2e calculates exact match and F1 of the downstream task, retrieval calculates''' ''' precision@k.''' ) , ) parser.add_argument('''--k''' , default=1 , type=a_ , help='''k for the precision@k calculation''' ) parser.add_argument( '''--evaluation_set''' , default=a_ , type=a_ , required=a_ , help='''Path to a file containing evaluation samples''' , ) parser.add_argument( '''--gold_data_path''' , default=a_ , type=a_ , required=a_ , help='''Path to a tab-separated file with gold samples''' , ) parser.add_argument( '''--gold_data_mode''' , default='''qa''' , type=a_ , choices=['''qa''', '''ans'''] , help=( '''Format of the gold data file''' '''qa - a single line in the following format: question [tab] answer_list''' '''ans - a single line of the gold file contains the expected answer string''' ) , ) parser.add_argument( '''--predictions_path''' , type=a_ , default='''predictions.txt''' , help='''Name of the predictions file, to be stored in the checkpoints directory''' , ) parser.add_argument( '''--eval_all_checkpoints''' , action='''store_true''' , help='''Evaluate all checkpoints starting with the same prefix as model_name ending and ending with step number''' , ) parser.add_argument( '''--eval_batch_size''' , default=8 , type=a_ , help='''Batch size per GPU/CPU for evaluation.''' , ) parser.add_argument( '''--recalculate''' , help='''Recalculate predictions even if the prediction file exists''' , action='''store_true''' , ) parser.add_argument( '''--num_beams''' , default=4 , type=a_ , help='''Number of beams to be used when generating answers''' , ) parser.add_argument('''--min_length''' , default=1 , type=a_ , help='''Min length of the generated answers''' ) parser.add_argument('''--max_length''' , default=50 , type=a_ , help='''Max length of the generated answers''' ) parser.add_argument( '''--print_predictions''' , action='''store_true''' , help='''If True, prints predictions while evaluating.''' , ) parser.add_argument( '''--print_docs''' , action='''store_true''' , help='''If True, prints docs retried while generating.''' , ) SCREAMING_SNAKE_CASE : List[str] = parser.parse_args() SCREAMING_SNAKE_CASE : Dict = torch.device('''cuda''' if torch.cuda.is_available() else '''cpu''' ) return args def __A ( a_ : Optional[Any] )-> int: '''simple docstring''' SCREAMING_SNAKE_CASE : Any = {} if args.model_type is None: SCREAMING_SNAKE_CASE : List[str] = infer_model_type(args.model_name_or_path ) assert args.model_type is not None if args.model_type.startswith('''rag''' ): SCREAMING_SNAKE_CASE : List[str] = RagTokenForGeneration if args.model_type == '''rag_token''' else RagSequenceForGeneration SCREAMING_SNAKE_CASE : Optional[Any] = args.n_docs if args.index_name is not None: SCREAMING_SNAKE_CASE : Tuple = args.index_name if args.index_path is not None: SCREAMING_SNAKE_CASE : List[Any] = args.index_path else: SCREAMING_SNAKE_CASE : str = BartForConditionalGeneration SCREAMING_SNAKE_CASE : Optional[int] = ( [f.path for f in os.scandir(args.model_name_or_path ) if f.is_dir()] if args.eval_all_checkpoints else [args.model_name_or_path] ) logger.info('''Evaluate the following checkpoints: %s''' , a_ ) SCREAMING_SNAKE_CASE : int = get_scores if args.eval_mode == '''e2e''' else get_precision_at_k SCREAMING_SNAKE_CASE : str = evaluate_batch_eae if args.eval_mode == '''e2e''' else evaluate_batch_retrieval for checkpoint in checkpoints: if os.path.exists(args.predictions_path ) and (not args.recalculate): logger.info('''Calculating metrics based on an existing predictions file: {}'''.format(args.predictions_path ) ) score_fn(a_ , args.predictions_path , args.gold_data_path ) continue logger.info('''***** Running evaluation for {} *****'''.format(a_ ) ) logger.info(''' Batch size = %d''' , args.eval_batch_size ) logger.info(''' Predictions will be stored under {}'''.format(args.predictions_path ) ) if args.model_type.startswith('''rag''' ): SCREAMING_SNAKE_CASE : Dict = RagRetriever.from_pretrained(a_ , **a_ ) SCREAMING_SNAKE_CASE : Union[str, Any] = model_class.from_pretrained(a_ , retriever=a_ , **a_ ) model.retriever.init_retrieval() else: SCREAMING_SNAKE_CASE : str = model_class.from_pretrained(a_ , **a_ ) model.to(args.device ) with open(args.evaluation_set , '''r''' ) as eval_file, open(args.predictions_path , '''w''' ) as preds_file: SCREAMING_SNAKE_CASE : Dict = [] for line in tqdm(a_ ): questions.append(line.strip() ) if len(a_ ) == args.eval_batch_size: SCREAMING_SNAKE_CASE : str = evaluate_batch_fn(a_ , a_ , a_ ) preds_file.write('''\n'''.join(a_ ) + '''\n''' ) preds_file.flush() SCREAMING_SNAKE_CASE : Union[str, Any] = [] if len(a_ ) > 0: SCREAMING_SNAKE_CASE : str = evaluate_batch_fn(a_ , a_ , a_ ) preds_file.write('''\n'''.join(a_ ) ) preds_file.flush() score_fn(a_ , args.predictions_path , args.gold_data_path ) if __name__ == "__main__": lowerCamelCase__ : List[str] = get_args() main(args)
18
0
"""simple docstring""" import warnings from pathlib import Path from typing import List, Tuple, Union import fire from torch import nn from transformers import AutoModelForSeqaSeqLM, AutoTokenizer, PreTrainedModel from transformers.utils import logging lowerCamelCase__ : List[str] = logging.get_logger(__name__) def __A ( a_ : nn.ModuleList , a_ : nn.ModuleList , a_ : List[int] )-> int: '''simple docstring''' SCREAMING_SNAKE_CASE : str = nn.ModuleList([src_layers[i] for i in layers_to_copy] ) assert len(snake_case_ ) == len(snake_case_ ), F"{len(snake_case_ )} != {len(snake_case_ )}" dest_layers.load_state_dict(layers_to_copy.state_dict() ) lowerCamelCase__ : Dict = { # maps num layers in teacher -> num_layers in student -> which teacher layers to copy. # 12: bart, 16: pegasus, 6: marian/Helsinki-NLP 12: { 1: [0], # This says that if the teacher has 12 layers and the student has 1, copy layer 0 of the teacher 2: [0, 6], 3: [0, 6, 11], 4: [0, 4, 8, 11], 6: [0, 2, 4, 7, 9, 11], 9: [0, 1, 2, 4, 5, 7, 9, 10, 11], 12: list(range(12)), }, 16: { # maps num layers in student -> which teacher layers to copy 1: [0], 2: [0, 15], 3: [0, 8, 15], 4: [0, 5, 10, 15], 6: [0, 3, 6, 9, 12, 15], 8: [0, 2, 4, 6, 8, 10, 12, 15], 9: [0, 1, 3, 5, 7, 9, 11, 13, 15], 12: [0, 1, 2, 3, 4, 5, 6, 7, 9, 11, 13, 15], 16: list(range(16)), }, 6: {1: [0], 2: [0, 5], 3: [0, 2, 5], 4: [0, 1, 3, 5], 6: list(range(6))}, } lowerCamelCase__ : Dict = { # maps num layers in student -> which teacher layers to copy. 6: {1: [5], 2: [3, 5], 3: [1, 4, 5], 4: [1, 2, 4, 5]}, 12: {1: [11], 2: [5, 11], 3: [3, 7, 11], 6: [1, 3, 5, 8, 10, 11]}, 16: {1: [15], 4: [4, 9, 12, 15], 8: [1, 3, 5, 7, 9, 11, 13, 15]}, } def __A ( a_ : Any , a_ : Optional[Any] )-> Optional[int]: '''simple docstring''' try: SCREAMING_SNAKE_CASE : Optional[Any] = LAYERS_TO_COPY[n_teacher][n_student] return val except KeyError: if n_student != n_teacher: warnings.warn( F"no hardcoded layers to copy for teacher {n_teacher} -> student {n_student}, defaulting to first" F" {n_student}" ) return list(range(snake_case_ ) ) def __A ( a_ : List[str] , a_ : str )-> int: '''simple docstring''' if n_student > n_teacher: raise ValueError(F"Cannot perform intermediate supervision for student {n_student} > teacher {n_teacher}" ) elif n_teacher == n_student: return list(range(snake_case_ ) ) elif n_student == 1: return [n_teacher - 1] else: return LAYERS_TO_SUPERVISE[n_teacher][n_student] def __A ( a_ : Union[str, PreTrainedModel] , a_ : Union[str, Path] = "student" , a_ : Union[int, None] = None , a_ : Union[int, None] = None , a_ : Optional[Any]=False , a_ : Union[str, Any]=None , a_ : Optional[Any]=None , **a_ : Tuple , )-> Any: '''simple docstring''' SCREAMING_SNAKE_CASE : str = "encoder_layers and decoder_layers cannot be both None-- you would just have an identical teacher." assert (e is not None) or (d is not None), _msg if isinstance(snake_case_ , snake_case_ ): AutoTokenizer.from_pretrained(snake_case_ ).save_pretrained(snake_case_ ) # purely for convenience SCREAMING_SNAKE_CASE : str = AutoModelForSeqaSeqLM.from_pretrained(snake_case_ ).eval() else: assert isinstance(snake_case_ , snake_case_ ), F"teacher must be a model or string got type {type(snake_case_ )}" SCREAMING_SNAKE_CASE : Dict = teacher.config.to_diff_dict() try: SCREAMING_SNAKE_CASE : Union[str, Any] = teacher.config.encoder_layers, teacher.config.decoder_layers if e is None: SCREAMING_SNAKE_CASE : str = teacher_e if d is None: SCREAMING_SNAKE_CASE : Optional[Any] = teacher_d init_kwargs.update({'''encoder_layers''': e, '''decoder_layers''': d} ) except AttributeError: # T5 if hasattr(teacher.config , '''num_encoder_layers''' ): SCREAMING_SNAKE_CASE : Tuple = teacher.config.num_encoder_layers, teacher.config.num_decoder_layers else: SCREAMING_SNAKE_CASE : List[Any] = teacher.config.num_layers, teacher.config.num_decoder_layers if e is None: SCREAMING_SNAKE_CASE : Any = teacher_e if d is None: SCREAMING_SNAKE_CASE : str = teacher_d if hasattr(teacher.config , '''num_encoder_layers''' ): init_kwargs.update({'''num_encoder_layers''': e, '''num_decoder_layers''': d} ) else: init_kwargs.update({'''num_layers''': e, '''num_decoder_layers''': d} ) # Kwargs to instantiate student: teacher kwargs with updated layer numbers + **extra_config_kwargs init_kwargs.update(snake_case_ ) # Copy weights SCREAMING_SNAKE_CASE : Any = teacher.config_class(**snake_case_ ) SCREAMING_SNAKE_CASE : Optional[int] = AutoModelForSeqaSeqLM.from_config(snake_case_ ) # Start by copying the full teacher state dict this will copy the first N teacher layers to the student. SCREAMING_SNAKE_CASE : Tuple = student.load_state_dict(teacher.state_dict() , strict=snake_case_ ) assert info.missing_keys == [], info.missing_keys # every student key should have a teacher keys. if copy_first_teacher_layers: # Our copying is done. We just log and save SCREAMING_SNAKE_CASE : List[Any] = list(range(snake_case_ ) ), list(range(snake_case_ ) ) logger.info( F"Copied encoder layers {e_layers_to_copy} and decoder layers {d_layers_to_copy}. Saving them to" F" {save_path}" ) student.save_pretrained(snake_case_ ) return student, e_layers_to_copy, d_layers_to_copy # Decide which layers of the teacher to copy. Not exactly alternating -- we try to keep first and last layer. if e_layers_to_copy is None: SCREAMING_SNAKE_CASE : List[int] = pick_layers_to_copy(snake_case_ , snake_case_ ) if d_layers_to_copy is None: SCREAMING_SNAKE_CASE : List[int] = pick_layers_to_copy(snake_case_ , snake_case_ ) try: if hasattr( snake_case_ , '''prophetnet''' ): # For ProphetNet, student.model.encoder.layers is called student.prophetnet.encoder.layers copy_layers(teacher.prophetnet.encoder.layers , student.prophetnet.encoder.layers , snake_case_ ) copy_layers(teacher.prophetnet.decoder.layers , student.prophetnet.decoder.layers , snake_case_ ) else: copy_layers(teacher.model.encoder.layers , student.model.encoder.layers , snake_case_ ) copy_layers(teacher.model.decoder.layers , student.model.decoder.layers , snake_case_ ) except AttributeError: # For t5, student.model.encoder.layers is called student.encoder.block copy_layers(teacher.encoder.block , student.encoder.block , snake_case_ ) copy_layers(teacher.decoder.block , student.decoder.block , snake_case_ ) logger.info( F"Copied encoder layers {e_layers_to_copy} and decoder layers {d_layers_to_copy}. Saving them to {save_path}" ) SCREAMING_SNAKE_CASE : str = { "teacher_type": teacher.config.model_type, "copied_encoder_layers": e_layers_to_copy, "copied_decoder_layers": d_layers_to_copy, } student.save_pretrained(snake_case_ ) # Save information about copying for easier reproducibility return student, e_layers_to_copy, d_layers_to_copy if __name__ == "__main__": fire.Fire(create_student_by_copying_alternating_layers)
702
"""simple docstring""" import json import os from typing import Optional, Tuple from ...tokenization_utils import PreTrainedTokenizer from ...utils import logging lowerCamelCase__ : List[str] = logging.get_logger(__name__) lowerCamelCase__ : Optional[int] = {"vocab_file": "vocab.json"} lowerCamelCase__ : Dict = { "vocab_file": { "mgp-str": "https://huggingface.co/alibaba-damo/mgp-str-base/blob/main/vocab.json", } } lowerCamelCase__ : Optional[Any] = {"mgp-str": 27} class lowercase__( _UpperCAmelCase ): '''simple docstring''' UpperCamelCase = VOCAB_FILES_NAMES UpperCamelCase = PRETRAINED_VOCAB_FILES_MAP UpperCamelCase = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES def __init__( self :int , lowerCamelCase_ :Optional[int] , lowerCamelCase_ :Optional[int]="[GO]" , lowerCamelCase_ :int="[GO]" , lowerCamelCase_ :str="[s]" , lowerCamelCase_ :Dict="[GO]" , **lowerCamelCase_ :List[str] ) -> Tuple: '''simple docstring''' super().__init__( unk_token=lowerCamelCase_ , bos_token=lowerCamelCase_ , eos_token=lowerCamelCase_ , pad_token=lowerCamelCase_ , **lowerCamelCase_ , ) with open(lowerCamelCase_ , encoding='''utf-8''' ) as vocab_handle: SCREAMING_SNAKE_CASE : int = json.load(lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Union[str, Any] = {v: k for k, v in self.vocab.items()} @property def __lowerCAmelCase ( self :int ) -> Dict: '''simple docstring''' return len(self.vocab ) def __lowerCAmelCase ( self :List[str] ) -> Dict: '''simple docstring''' return dict(self.vocab , **self.added_tokens_encoder ) def __lowerCAmelCase ( self :List[str] , lowerCamelCase_ :Optional[Any] ) -> List[str]: '''simple docstring''' SCREAMING_SNAKE_CASE : Union[str, Any] = [] for s in text: char_tokens.extend(lowerCamelCase_ ) return char_tokens def __lowerCAmelCase ( self :Dict , lowerCamelCase_ :Tuple ) -> Optional[int]: '''simple docstring''' return self.vocab.get(lowerCamelCase_ , self.vocab.get(self.unk_token ) ) def __lowerCAmelCase ( self :List[str] , lowerCamelCase_ :Dict ) -> Optional[int]: '''simple docstring''' return self.decoder.get(lowerCamelCase_ ) def __lowerCAmelCase ( self :Optional[int] , lowerCamelCase_ :str , lowerCamelCase_ :Optional[str] = None ) -> Tuple[str]: '''simple docstring''' if not os.path.isdir(lowerCamelCase_ ): logger.error('''Vocabulary path ({}) should be a directory'''.format(lowerCamelCase_ ) ) return SCREAMING_SNAKE_CASE : List[Any] = os.path.join( lowerCamelCase_ , (filename_prefix + '''-''' if filename_prefix else '''''') + VOCAB_FILES_NAMES['''vocab_file'''] ) with open(lowerCamelCase_ , '''w''' , encoding='''utf-8''' ) as f: f.write(json.dumps(self.vocab , indent=2 , sort_keys=lowerCamelCase_ , ensure_ascii=lowerCamelCase_ ) + '''\n''' ) return (vocab_file,)
18
0
from typing import TYPE_CHECKING from ..utils import _LazyModule lowerCamelCase__ : Optional[int] = { "config": [ "EXTERNAL_DATA_FORMAT_SIZE_LIMIT", "OnnxConfig", "OnnxConfigWithPast", "OnnxSeq2SeqConfigWithPast", "PatchingSpec", ], "convert": ["export", "validate_model_outputs"], "features": ["FeaturesManager"], "utils": ["ParameterFormat", "compute_serialized_parameters_size"], } if TYPE_CHECKING: from .config import ( EXTERNAL_DATA_FORMAT_SIZE_LIMIT, OnnxConfig, OnnxConfigWithPast, OnnxSeqaSeqConfigWithPast, PatchingSpec, ) from .convert import export, validate_model_outputs from .features import FeaturesManager from .utils import ParameterFormat, compute_serialized_parameters_size else: import sys lowerCamelCase__ : int = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
703
"""simple docstring""" from ...configuration_utils import PretrainedConfig from ...utils import logging lowerCamelCase__ : Tuple = logging.get_logger(__name__) lowerCamelCase__ : str = { "studio-ousia/luke-base": "https://huggingface.co/studio-ousia/luke-base/resolve/main/config.json", "studio-ousia/luke-large": "https://huggingface.co/studio-ousia/luke-large/resolve/main/config.json", } class lowercase__( _UpperCAmelCase ): '''simple docstring''' UpperCamelCase = """luke""" def __init__( self :Optional[int] , lowerCamelCase_ :Union[str, Any]=5_02_67 , lowerCamelCase_ :int=50_00_00 , lowerCamelCase_ :Tuple=7_68 , lowerCamelCase_ :List[str]=2_56 , lowerCamelCase_ :Dict=12 , lowerCamelCase_ :Optional[int]=12 , lowerCamelCase_ :Optional[Any]=30_72 , lowerCamelCase_ :List[Any]="gelu" , lowerCamelCase_ :Any=0.1 , lowerCamelCase_ :Union[str, Any]=0.1 , lowerCamelCase_ :List[str]=5_12 , lowerCamelCase_ :Optional[int]=2 , lowerCamelCase_ :Tuple=0.0_2 , lowerCamelCase_ :Optional[int]=1E-12 , lowerCamelCase_ :List[Any]=True , lowerCamelCase_ :List[str]=None , lowerCamelCase_ :str=1 , lowerCamelCase_ :Any=0 , lowerCamelCase_ :str=2 , **lowerCamelCase_ :List[Any] , ) -> Optional[int]: '''simple docstring''' super().__init__(pad_token_id=lowerCamelCase_ , bos_token_id=lowerCamelCase_ , eos_token_id=lowerCamelCase_ , **lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Dict = vocab_size SCREAMING_SNAKE_CASE : List[str] = entity_vocab_size SCREAMING_SNAKE_CASE : str = hidden_size SCREAMING_SNAKE_CASE : Union[str, Any] = entity_emb_size SCREAMING_SNAKE_CASE : List[Any] = num_hidden_layers SCREAMING_SNAKE_CASE : Optional[int] = num_attention_heads SCREAMING_SNAKE_CASE : Optional[Any] = hidden_act SCREAMING_SNAKE_CASE : Optional[Any] = intermediate_size SCREAMING_SNAKE_CASE : Union[str, Any] = hidden_dropout_prob SCREAMING_SNAKE_CASE : Dict = attention_probs_dropout_prob SCREAMING_SNAKE_CASE : Dict = max_position_embeddings SCREAMING_SNAKE_CASE : Tuple = type_vocab_size SCREAMING_SNAKE_CASE : str = initializer_range SCREAMING_SNAKE_CASE : Dict = layer_norm_eps SCREAMING_SNAKE_CASE : Optional[int] = use_entity_aware_attention SCREAMING_SNAKE_CASE : List[str] = classifier_dropout
18
0
"""simple docstring""" def __A ( a_ : List[str] , a_ : Optional[Any] )-> Union[str, Any]: '''simple docstring''' print('''\nThe shortest path matrix using Floyd Warshall algorithm\n''' ) for i in range(a_ ): for j in range(a_ ): if dist[i][j] != float('''inf''' ): print(int(dist[i][j] ) , end='''\t''' ) else: print('''INF''' , end='''\t''' ) print() def __A ( a_ : Optional[int] , a_ : Union[str, Any] )-> Dict: '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[int] = [[float('''inf''' ) for _ in range(a_ )] for _ in range(a_ )] for i in range(a_ ): for j in range(a_ ): SCREAMING_SNAKE_CASE : List[str] = graph[i][j] # check vertex k against all other vertices (i, j) for k in range(a_ ): # looping through rows of graph array for i in range(a_ ): # looping through columns of graph array for j in range(a_ ): if ( dist[i][k] != float('''inf''' ) and dist[k][j] != float('''inf''' ) and dist[i][k] + dist[k][j] < dist[i][j] ): SCREAMING_SNAKE_CASE : Tuple = dist[i][k] + dist[k][j] _print_dist(a_ , a_ ) return dist, v if __name__ == "__main__": lowerCamelCase__ : Dict = int(input("Enter number of vertices: ")) lowerCamelCase__ : Tuple = int(input("Enter number of edges: ")) lowerCamelCase__ : str = [[float("inf") for i in range(v)] for j in range(v)] for i in range(v): lowerCamelCase__ : str = 0.0 # src and dst are indices that must be within the array size graph[e][v] # failure to follow this will result in an error for i in range(e): print("\nEdge ", i + 1) lowerCamelCase__ : List[Any] = int(input("Enter source:")) lowerCamelCase__ : List[str] = int(input("Enter destination:")) lowerCamelCase__ : Optional[int] = float(input("Enter weight:")) lowerCamelCase__ : Tuple = weight floyd_warshall(graph, v) # Example Input # Enter number of vertices: 3 # Enter number of edges: 2 # # generated graph from vertex and edge inputs # [[inf, inf, inf], [inf, inf, inf], [inf, inf, inf]] # [[0.0, inf, inf], [inf, 0.0, inf], [inf, inf, 0.0]] # specify source, destination and weight for edge #1 # Edge 1 # Enter source:1 # Enter destination:2 # Enter weight:2 # specify source, destination and weight for edge #2 # Edge 2 # Enter source:2 # Enter destination:1 # Enter weight:1 # # Expected Output from the vertice, edge and src, dst, weight inputs!! # 0 INF INF # INF 0 2 # INF 1 0
704
"""simple docstring""" def __A ( a_ : list , a_ : int , a_ : int = 0 , a_ : int = 0 )-> int: '''simple docstring''' SCREAMING_SNAKE_CASE : str = right or len(a_ ) - 1 if left > right: return -1 elif list_data[left] == key: return left elif list_data[right] == key: return right else: return search(a_ , a_ , left + 1 , right - 1 ) if __name__ == "__main__": import doctest doctest.testmod()
18
0
"""simple docstring""" from __future__ import annotations def __A ( a_ : dict , a_ : str )-> Any: '''simple docstring''' SCREAMING_SNAKE_CASE : List[str] = set(snake_case__ ), [start] while stack: SCREAMING_SNAKE_CASE : Dict = stack.pop() explored.add(snake_case__ ) # Differences from BFS: # 1) pop last element instead of first one # 2) add adjacent elements to stack without exploring them for adj in reversed(graph[v] ): if adj not in explored: stack.append(snake_case__ ) return explored lowerCamelCase__ : List[Any] = { "A": ["B", "C", "D"], "B": ["A", "D", "E"], "C": ["A", "F"], "D": ["B", "D"], "E": ["B", "F"], "F": ["C", "E", "G"], "G": ["F"], } if __name__ == "__main__": import doctest doctest.testmod() print(depth_first_search(G, "A"))
705
"""simple docstring""" def __A ( a_ : int )-> list[int]: '''simple docstring''' if num <= 0: raise ValueError('''Input must be a positive integer''' ) SCREAMING_SNAKE_CASE : Optional[int] = [True] * (num + 1) SCREAMING_SNAKE_CASE : Optional[Any] = 2 while p * p <= num: if primes[p]: for i in range(p * p , num + 1 , a_ ): SCREAMING_SNAKE_CASE : Any = False p += 1 return [prime for prime in range(2 , num + 1 ) if primes[prime]] if __name__ == "__main__": import doctest doctest.testmod() lowerCamelCase__ : str = int(input("Enter a positive integer: ").strip()) print(prime_sieve_eratosthenes(user_num))
18
0
"""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 lowerCamelCase__ : Tuple = "\\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" lowerCamelCase__ : Optional[Any] = "\\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" lowerCamelCase__ : Optional[int] = "\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 lowercase__( datasets.Metric ): '''simple docstring''' def __lowerCAmelCase ( self :int ) -> Dict: '''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 __lowerCAmelCase ( self :Union[str, Any] , lowerCamelCase_ :Union[str, Any] , lowerCamelCase_ :Optional[int] , lowerCamelCase_ :Dict=None , lowerCamelCase_ :Union[str, Any]=True , lowerCamelCase_ :Tuple=False ) -> Dict: '''simple docstring''' if rouge_types is None: SCREAMING_SNAKE_CASE : Optional[Any] = ['''rouge1''', '''rouge2''', '''rougeL''', '''rougeLsum'''] SCREAMING_SNAKE_CASE : int = rouge_scorer.RougeScorer(rouge_types=__lowerCAmelCase , use_stemmer=__lowerCAmelCase ) if use_aggregator: SCREAMING_SNAKE_CASE : Any = scoring.BootstrapAggregator() else: SCREAMING_SNAKE_CASE : Optional[int] = [] for ref, pred in zip(__lowerCAmelCase , __lowerCAmelCase ): SCREAMING_SNAKE_CASE : Tuple = scorer.score(__lowerCAmelCase , __lowerCAmelCase ) if use_aggregator: aggregator.add_scores(__lowerCAmelCase ) else: scores.append(__lowerCAmelCase ) if use_aggregator: SCREAMING_SNAKE_CASE : str = 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
706
"""simple docstring""" from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_tf_available, is_tokenizers_available, is_torch_available, ) lowerCamelCase__ : Optional[Any] = { "configuration_funnel": ["FUNNEL_PRETRAINED_CONFIG_ARCHIVE_MAP", "FunnelConfig"], "convert_funnel_original_tf_checkpoint_to_pytorch": [], "tokenization_funnel": ["FunnelTokenizer"], } try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCamelCase__ : Optional[Any] = ["FunnelTokenizerFast"] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCamelCase__ : Dict = [ "FUNNEL_PRETRAINED_MODEL_ARCHIVE_LIST", "FunnelBaseModel", "FunnelForMaskedLM", "FunnelForMultipleChoice", "FunnelForPreTraining", "FunnelForQuestionAnswering", "FunnelForSequenceClassification", "FunnelForTokenClassification", "FunnelModel", "FunnelPreTrainedModel", "load_tf_weights_in_funnel", ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCamelCase__ : Tuple = [ "TF_FUNNEL_PRETRAINED_MODEL_ARCHIVE_LIST", "TFFunnelBaseModel", "TFFunnelForMaskedLM", "TFFunnelForMultipleChoice", "TFFunnelForPreTraining", "TFFunnelForQuestionAnswering", "TFFunnelForSequenceClassification", "TFFunnelForTokenClassification", "TFFunnelModel", "TFFunnelPreTrainedModel", ] if TYPE_CHECKING: from .configuration_funnel import FUNNEL_PRETRAINED_CONFIG_ARCHIVE_MAP, FunnelConfig from .tokenization_funnel import FunnelTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_funnel_fast import FunnelTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_funnel import ( FUNNEL_PRETRAINED_MODEL_ARCHIVE_LIST, FunnelBaseModel, FunnelForMaskedLM, FunnelForMultipleChoice, FunnelForPreTraining, FunnelForQuestionAnswering, FunnelForSequenceClassification, FunnelForTokenClassification, FunnelModel, FunnelPreTrainedModel, load_tf_weights_in_funnel, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_funnel import ( TF_FUNNEL_PRETRAINED_MODEL_ARCHIVE_LIST, TFFunnelBaseModel, TFFunnelForMaskedLM, TFFunnelForMultipleChoice, TFFunnelForPreTraining, TFFunnelForQuestionAnswering, TFFunnelForSequenceClassification, TFFunnelForTokenClassification, TFFunnelModel, TFFunnelPreTrainedModel, ) else: import sys lowerCamelCase__ : Optional[int] = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
18
0
"""simple docstring""" import itertools import json import linecache import os import pickle import re import socket import string from collections import Counter from logging import getLogger from pathlib import Path from typing import Callable, Dict, Iterable, List import git import torch from torch.utils.data import Dataset from transformers import BartTokenizer, RagTokenizer, TaTokenizer def __A ( a_ : str , a_ : List[str] , a_ : Dict , a_ : Union[str, Any] , a_ : int=True , a_ : Optional[Any]="pt" )-> str: '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[Any] = {"""add_prefix_space""": True} if isinstance(UpperCAmelCase__ , UpperCAmelCase__ ) and not line.startswith(''' ''' ) else {} SCREAMING_SNAKE_CASE : List[str] = padding_side return tokenizer( [line] , max_length=UpperCAmelCase__ , padding='''max_length''' if pad_to_max_length else None , truncation=UpperCAmelCase__ , return_tensors=UpperCAmelCase__ , add_special_tokens=UpperCAmelCase__ , **UpperCAmelCase__ , ) def __A ( a_ : Tuple , a_ : List[Any] , a_ : Optional[int]=None , )-> List[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : Union[str, Any] = input_ids.ne(UpperCAmelCase__ ).any(dim=0 ) if attention_mask is None: return input_ids[:, keep_column_mask] else: return (input_ids[:, keep_column_mask], attention_mask[:, keep_column_mask]) class lowercase__( _UpperCAmelCase ): '''simple docstring''' def __init__( self :List[str] , lowerCamelCase_ :List[str] , lowerCamelCase_ :List[str] , lowerCamelCase_ :Dict , lowerCamelCase_ :Optional[int] , lowerCamelCase_ :Any="train" , lowerCamelCase_ :List[Any]=None , lowerCamelCase_ :Optional[int]=None , lowerCamelCase_ :Optional[int]=None , lowerCamelCase_ :Optional[Any]="" , ) -> Union[str, Any]: '''simple docstring''' super().__init__() SCREAMING_SNAKE_CASE : Union[str, Any] = Path(lowercase_ ).joinpath(type_path + '''.source''' ) SCREAMING_SNAKE_CASE : Union[str, Any] = Path(lowercase_ ).joinpath(type_path + '''.target''' ) SCREAMING_SNAKE_CASE : Any = self.get_char_lens(self.src_file ) SCREAMING_SNAKE_CASE : Tuple = max_source_length SCREAMING_SNAKE_CASE : str = max_target_length assert min(self.src_lens ) > 0, f"found empty line in {self.src_file}" SCREAMING_SNAKE_CASE : Optional[Any] = tokenizer SCREAMING_SNAKE_CASE : str = prefix if n_obs is not None: SCREAMING_SNAKE_CASE : Any = self.src_lens[:n_obs] SCREAMING_SNAKE_CASE : Tuple = src_lang SCREAMING_SNAKE_CASE : int = tgt_lang def __len__( self :Optional[Any] ) -> Any: '''simple docstring''' return len(self.src_lens ) def __getitem__( self :Optional[int] , lowerCamelCase_ :List[str] ) -> Optional[int]: '''simple docstring''' SCREAMING_SNAKE_CASE : List[str] = index + 1 # linecache starts at 1 SCREAMING_SNAKE_CASE : Union[str, Any] = self.prefix + linecache.getline(str(self.src_file ) , lowercase_ ).rstrip('''\n''' ) SCREAMING_SNAKE_CASE : List[Any] = linecache.getline(str(self.tgt_file ) , lowercase_ ).rstrip('''\n''' ) assert source_line, f"empty source line for index {index}" assert tgt_line, f"empty tgt line for index {index}" # Need to add eos token manually for T5 if isinstance(self.tokenizer , lowercase_ ): source_line += self.tokenizer.eos_token tgt_line += self.tokenizer.eos_token # Pad source and target to the right SCREAMING_SNAKE_CASE : Optional[Any] = ( self.tokenizer.question_encoder if isinstance(self.tokenizer , lowercase_ ) else self.tokenizer ) SCREAMING_SNAKE_CASE : Tuple = self.tokenizer.generator if isinstance(self.tokenizer , lowercase_ ) else self.tokenizer SCREAMING_SNAKE_CASE : str = encode_line(lowercase_ , lowercase_ , self.max_source_length , '''right''' ) SCREAMING_SNAKE_CASE : Optional[int] = encode_line(lowercase_ , lowercase_ , self.max_target_length , '''right''' ) SCREAMING_SNAKE_CASE : Any = source_inputs["""input_ids"""].squeeze() SCREAMING_SNAKE_CASE : Dict = target_inputs["""input_ids"""].squeeze() SCREAMING_SNAKE_CASE : int = source_inputs["""attention_mask"""].squeeze() return { "input_ids": source_ids, "attention_mask": src_mask, "decoder_input_ids": target_ids, } @staticmethod def __lowerCAmelCase ( lowerCamelCase_ :int ) -> Any: '''simple docstring''' return [len(lowercase_ ) for x in Path(lowercase_ ).open().readlines()] def __lowerCAmelCase ( self :List[str] , lowerCamelCase_ :List[str] ) -> List[str]: '''simple docstring''' SCREAMING_SNAKE_CASE : Any = torch.stack([x['''input_ids'''] for x in batch] ) SCREAMING_SNAKE_CASE : Optional[Any] = torch.stack([x['''attention_mask'''] for x in batch] ) SCREAMING_SNAKE_CASE : Any = torch.stack([x['''decoder_input_ids'''] for x in batch] ) SCREAMING_SNAKE_CASE : int = ( self.tokenizer.generator.pad_token_id if isinstance(self.tokenizer , lowercase_ ) else self.tokenizer.pad_token_id ) SCREAMING_SNAKE_CASE : Union[str, Any] = ( self.tokenizer.question_encoder.pad_token_id if isinstance(self.tokenizer , lowercase_ ) else self.tokenizer.pad_token_id ) SCREAMING_SNAKE_CASE : Tuple = trim_batch(lowercase_ , lowercase_ ) SCREAMING_SNAKE_CASE : List[str] = trim_batch(lowercase_ , lowercase_ , attention_mask=lowercase_ ) SCREAMING_SNAKE_CASE : str = { """input_ids""": source_ids, """attention_mask""": source_mask, """decoder_input_ids""": y, } return batch lowerCamelCase__ : Union[str, Any] = getLogger(__name__) def __A ( a_ : List[List] )-> Optional[Any]: '''simple docstring''' return list(itertools.chain.from_iterable(UpperCAmelCase__ ) ) def __A ( a_ : str )-> None: '''simple docstring''' SCREAMING_SNAKE_CASE : Dict = get_git_info() save_json(UpperCAmelCase__ , os.path.join(UpperCAmelCase__ , '''git_log.json''' ) ) def __A ( a_ : Optional[Any] , a_ : List[Any] , a_ : int=4 , **a_ : List[Any] )-> int: '''simple docstring''' with open(UpperCAmelCase__ , '''w''' ) as f: json.dump(UpperCAmelCase__ , UpperCAmelCase__ , indent=UpperCAmelCase__ , **UpperCAmelCase__ ) def __A ( a_ : str )-> Union[str, Any]: '''simple docstring''' with open(UpperCAmelCase__ ) as f: return json.load(UpperCAmelCase__ ) def __A ( )-> Optional[int]: '''simple docstring''' SCREAMING_SNAKE_CASE : Union[str, Any] = git.Repo(search_parent_directories=UpperCAmelCase__ ) SCREAMING_SNAKE_CASE : str = { """repo_id""": str(UpperCAmelCase__ ), """repo_sha""": str(repo.head.object.hexsha ), """repo_branch""": str(repo.active_branch ), """hostname""": str(socket.gethostname() ), } return repo_infos def __A ( a_ : Callable , a_ : Iterable )-> List: '''simple docstring''' return list(map(UpperCAmelCase__ , UpperCAmelCase__ ) ) def __A ( a_ : List[str] , a_ : Optional[Any] )-> Any: '''simple docstring''' with open(UpperCAmelCase__ , '''wb''' ) as f: return pickle.dump(UpperCAmelCase__ , UpperCAmelCase__ ) def __A ( a_ : List[str] )-> Dict: '''simple docstring''' def remove_articles(a_ : List[str] ): return re.sub(r'''\b(a|an|the)\b''' , ''' ''' , UpperCAmelCase__ ) def white_space_fix(a_ : str ): return " ".join(text.split() ) def remove_punc(a_ : Tuple ): SCREAMING_SNAKE_CASE : List[Any] = set(string.punctuation ) return "".join(ch for ch in text if ch not in exclude ) def lower(a_ : Optional[Any] ): return text.lower() return white_space_fix(remove_articles(remove_punc(lower(UpperCAmelCase__ ) ) ) ) def __A ( a_ : List[str] , a_ : Optional[Any] )-> Union[str, Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[int] = normalize_answer(UpperCAmelCase__ ).split() SCREAMING_SNAKE_CASE : Union[str, Any] = normalize_answer(UpperCAmelCase__ ).split() SCREAMING_SNAKE_CASE : Optional[Any] = Counter(UpperCAmelCase__ ) & Counter(UpperCAmelCase__ ) SCREAMING_SNAKE_CASE : List[str] = sum(common.values() ) if num_same == 0: return 0 SCREAMING_SNAKE_CASE : Dict = 1.0 * num_same / len(UpperCAmelCase__ ) SCREAMING_SNAKE_CASE : str = 1.0 * num_same / len(UpperCAmelCase__ ) SCREAMING_SNAKE_CASE : Any = (2 * precision * recall) / (precision + recall) return fa def __A ( a_ : str , a_ : Any )-> Tuple: '''simple docstring''' return normalize_answer(UpperCAmelCase__ ) == normalize_answer(UpperCAmelCase__ ) def __A ( a_ : List[str] , a_ : List[str] )-> Dict: '''simple docstring''' assert len(UpperCAmelCase__ ) == len(UpperCAmelCase__ ) SCREAMING_SNAKE_CASE : str = 0 for hypo, pred in zip(UpperCAmelCase__ , UpperCAmelCase__ ): em += exact_match_score(UpperCAmelCase__ , UpperCAmelCase__ ) if len(UpperCAmelCase__ ) > 0: em /= len(UpperCAmelCase__ ) return {"em": em} def __A ( a_ : Any )-> Any: '''simple docstring''' return model_prefix.startswith('''rag''' ) def __A ( a_ : Union[str, Any] , a_ : str , a_ : List[str] )-> Optional[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : List[str] = {p: p for p in extra_params} # T5 models don't have `dropout` param, they have `dropout_rate` instead SCREAMING_SNAKE_CASE : List[Any] = """dropout_rate""" for p in extra_params: if getattr(UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__ ): if not hasattr(UpperCAmelCase__ , UpperCAmelCase__ ) and not hasattr(UpperCAmelCase__ , equivalent_param[p] ): logger.info('''config doesn\'t have a `{}` attribute'''.format(UpperCAmelCase__ ) ) delattr(UpperCAmelCase__ , UpperCAmelCase__ ) continue SCREAMING_SNAKE_CASE : Dict = p if hasattr(UpperCAmelCase__ , UpperCAmelCase__ ) else equivalent_param[p] setattr(UpperCAmelCase__ , UpperCAmelCase__ , getattr(UpperCAmelCase__ , UpperCAmelCase__ ) ) delattr(UpperCAmelCase__ , UpperCAmelCase__ ) return hparams, config
707
"""simple docstring""" import os import sys lowerCamelCase__ : List[Any] = os.path.join(os.path.dirname(__file__), "src") sys.path.append(SRC_DIR) from transformers import ( AutoConfig, AutoModel, AutoModelForCausalLM, AutoModelForMaskedLM, AutoModelForQuestionAnswering, AutoModelForSequenceClassification, AutoTokenizer, add_start_docstrings, ) lowerCamelCase__ : str = [ "torch", "numpy", "tokenizers", "filelock", "requests", "tqdm", "regex", "sentencepiece", "sacremoses", "importlib_metadata", "huggingface_hub", ] @add_start_docstrings(AutoConfig.__doc__ ) def __A ( *a_ : Any , **a_ : Union[str, Any] )-> Dict: '''simple docstring''' return AutoConfig.from_pretrained(*a_ , **a_ ) @add_start_docstrings(AutoTokenizer.__doc__ ) def __A ( *a_ : str , **a_ : Union[str, Any] )-> Union[str, Any]: '''simple docstring''' return AutoTokenizer.from_pretrained(*a_ , **a_ ) @add_start_docstrings(AutoModel.__doc__ ) def __A ( *a_ : List[str] , **a_ : int )-> Dict: '''simple docstring''' return AutoModel.from_pretrained(*a_ , **a_ ) @add_start_docstrings(AutoModelForCausalLM.__doc__ ) def __A ( *a_ : Any , **a_ : Tuple )-> Dict: '''simple docstring''' return AutoModelForCausalLM.from_pretrained(*a_ , **a_ ) @add_start_docstrings(AutoModelForMaskedLM.__doc__ ) def __A ( *a_ : Dict , **a_ : Optional[Any] )-> Optional[int]: '''simple docstring''' return AutoModelForMaskedLM.from_pretrained(*a_ , **a_ ) @add_start_docstrings(AutoModelForSequenceClassification.__doc__ ) def __A ( *a_ : Optional[int] , **a_ : str )-> Optional[int]: '''simple docstring''' return AutoModelForSequenceClassification.from_pretrained(*a_ , **a_ ) @add_start_docstrings(AutoModelForQuestionAnswering.__doc__ ) def __A ( *a_ : List[str] , **a_ : int )-> List[Any]: '''simple docstring''' return AutoModelForQuestionAnswering.from_pretrained(*a_ , **a_ )
18
0
from collections import OrderedDict from typing import Any, List, Mapping, Optional from ... import PreTrainedTokenizer, TensorType, is_torch_available from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfigWithPast, PatchingSpec from ...utils import logging lowerCamelCase__ : Union[str, Any] = logging.get_logger(__name__) lowerCamelCase__ : Tuple = { '''EleutherAI/gpt-j-6B''': '''https://huggingface.co/EleutherAI/gpt-j-6B/resolve/main/config.json''', # See all GPT-J models at https://huggingface.co/models?filter=gpt_j } class lowercase__( a__ ): '''simple docstring''' UpperCamelCase = """gptj""" UpperCamelCase = { """max_position_embeddings""": """n_positions""", """hidden_size""": """n_embd""", """num_attention_heads""": """n_head""", """num_hidden_layers""": """n_layer""", } def __init__( self :Tuple , lowerCamelCase_ :int=5_04_00 , lowerCamelCase_ :Any=20_48 , lowerCamelCase_ :int=40_96 , lowerCamelCase_ :str=28 , lowerCamelCase_ :str=16 , lowerCamelCase_ :Dict=64 , lowerCamelCase_ :Tuple=None , lowerCamelCase_ :Optional[Any]="gelu_new" , lowerCamelCase_ :Dict=0.0 , lowerCamelCase_ :List[Any]=0.0 , lowerCamelCase_ :str=0.0 , lowerCamelCase_ :Any=1E-5 , lowerCamelCase_ :Dict=0.0_2 , lowerCamelCase_ :List[Any]=True , lowerCamelCase_ :Optional[Any]=5_02_56 , lowerCamelCase_ :Optional[int]=5_02_56 , lowerCamelCase_ :str=False , **lowerCamelCase_ :List[Any] , ) -> Dict: '''simple docstring''' SCREAMING_SNAKE_CASE : int = vocab_size SCREAMING_SNAKE_CASE : Tuple = n_positions SCREAMING_SNAKE_CASE : List[Any] = n_embd SCREAMING_SNAKE_CASE : int = n_layer SCREAMING_SNAKE_CASE : Union[str, Any] = n_head SCREAMING_SNAKE_CASE : Optional[int] = n_inner SCREAMING_SNAKE_CASE : Any = rotary_dim SCREAMING_SNAKE_CASE : Any = activation_function SCREAMING_SNAKE_CASE : Optional[int] = resid_pdrop SCREAMING_SNAKE_CASE : Optional[int] = embd_pdrop SCREAMING_SNAKE_CASE : int = attn_pdrop SCREAMING_SNAKE_CASE : Optional[Any] = layer_norm_epsilon SCREAMING_SNAKE_CASE : Optional[int] = initializer_range SCREAMING_SNAKE_CASE : Union[str, Any] = use_cache SCREAMING_SNAKE_CASE : Union[str, Any] = bos_token_id SCREAMING_SNAKE_CASE : Dict = eos_token_id super().__init__( bos_token_id=lowerCAmelCase__ , eos_token_id=lowerCAmelCase__ , tie_word_embeddings=lowerCAmelCase__ , **lowerCAmelCase__ ) class lowercase__( a__ ): '''simple docstring''' def __init__( self :str , lowerCamelCase_ :Optional[Any] , lowerCamelCase_ :Union[str, Any] = "default" , lowerCamelCase_ :List[Any] = None , lowerCamelCase_ :Optional[int] = False , ) -> Dict: '''simple docstring''' super().__init__(lowerCAmelCase__ , task=lowerCAmelCase__ , patching_specs=lowerCAmelCase__ , use_past=lowerCAmelCase__ ) if not getattr(self._config , '''pad_token_id''' , lowerCAmelCase__ ): # TODO: how to do that better? SCREAMING_SNAKE_CASE : Optional[Any] = 0 @property def __lowerCAmelCase ( self :Tuple ) -> Mapping[str, Mapping[int, str]]: '''simple docstring''' SCREAMING_SNAKE_CASE : Union[str, Any] = OrderedDict({'''input_ids''': {0: '''batch''', 1: '''sequence'''}} ) if self.use_past: self.fill_with_past_key_values_(lowerCAmelCase__ , direction='''inputs''' ) SCREAMING_SNAKE_CASE : Tuple = {0: "batch", 1: "past_sequence + sequence"} else: SCREAMING_SNAKE_CASE : List[Any] = {0: "batch", 1: "sequence"} return common_inputs @property def __lowerCAmelCase ( self :Tuple ) -> int: '''simple docstring''' return self._config.n_layer @property def __lowerCAmelCase ( self :Optional[int] ) -> int: '''simple docstring''' return self._config.n_head def __lowerCAmelCase ( self :List[Any] , lowerCamelCase_ :int , lowerCamelCase_ :Union[str, Any] = -1 , lowerCamelCase_ :Union[str, Any] = -1 , lowerCamelCase_ :Any = False , lowerCamelCase_ :Union[str, Any] = None , ) -> Mapping[str, Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : List[Any] = super(lowerCAmelCase__ , self ).generate_dummy_inputs( lowerCAmelCase__ , batch_size=lowerCAmelCase__ , seq_length=lowerCAmelCase__ , is_pair=lowerCAmelCase__ , framework=lowerCAmelCase__ ) # We need to order the input in the way they appears in the forward() SCREAMING_SNAKE_CASE : Dict = OrderedDict({'''input_ids''': common_inputs['''input_ids''']} ) # Need to add the past_keys if self.use_past: if not is_torch_available(): raise ValueError('''Cannot generate dummy past_keys inputs without PyTorch installed.''' ) else: import torch SCREAMING_SNAKE_CASE : Optional[int] = common_inputs["input_ids"].shape # Not using the same length for past_key_values SCREAMING_SNAKE_CASE : Optional[int] = seqlen + 2 SCREAMING_SNAKE_CASE : Optional[int] = ( batch, self.num_attention_heads, past_key_values_length, self._config.hidden_size // self.num_attention_heads, ) SCREAMING_SNAKE_CASE : List[Any] = [ (torch.zeros(lowerCAmelCase__ ), torch.zeros(lowerCAmelCase__ )) for _ in range(self.num_layers ) ] SCREAMING_SNAKE_CASE : List[str] = common_inputs["attention_mask"] if self.use_past: SCREAMING_SNAKE_CASE : List[str] = ordered_inputs["attention_mask"].dtype SCREAMING_SNAKE_CASE : Union[str, Any] = torch.cat( [ordered_inputs['''attention_mask'''], torch.ones(lowerCAmelCase__ , lowerCAmelCase__ , dtype=lowerCAmelCase__ )] , dim=1 ) return ordered_inputs @property def __lowerCAmelCase ( self :Any ) -> int: '''simple docstring''' return 13
708
"""simple docstring""" import math from typing import Optional import numpy as np from ...configuration_utils import PretrainedConfig from ...utils import logging lowerCamelCase__ : Tuple = logging.get_logger(__name__) lowerCamelCase__ : Any = { "facebook/encodec_24khz": "https://huggingface.co/facebook/encodec_24khz/resolve/main/config.json", "facebook/encodec_48khz": "https://huggingface.co/facebook/encodec_48khz/resolve/main/config.json", } class lowercase__( _UpperCAmelCase ): '''simple docstring''' UpperCamelCase = """encodec""" def __init__( self :List[str] , lowerCamelCase_ :Tuple=[1.5, 3.0, 6.0, 1_2.0, 2_4.0] , lowerCamelCase_ :str=2_40_00 , lowerCamelCase_ :Any=1 , lowerCamelCase_ :List[Any]=False , lowerCamelCase_ :Optional[int]=None , lowerCamelCase_ :Optional[Any]=None , lowerCamelCase_ :str=1_28 , lowerCamelCase_ :Any=32 , lowerCamelCase_ :int=1 , lowerCamelCase_ :Dict=[8, 5, 4, 2] , lowerCamelCase_ :List[Any]="weight_norm" , lowerCamelCase_ :Optional[int]=7 , lowerCamelCase_ :Tuple=7 , lowerCamelCase_ :Optional[Any]=3 , lowerCamelCase_ :int=2 , lowerCamelCase_ :Dict=True , lowerCamelCase_ :Optional[int]="reflect" , lowerCamelCase_ :Optional[int]=2 , lowerCamelCase_ :Union[str, Any]=2 , lowerCamelCase_ :Dict=1.0 , lowerCamelCase_ :Any=10_24 , lowerCamelCase_ :str=None , lowerCamelCase_ :Union[str, Any]=True , **lowerCamelCase_ :Optional[int] , ) -> Dict: '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[Any] = target_bandwidths SCREAMING_SNAKE_CASE : List[str] = sampling_rate SCREAMING_SNAKE_CASE : Tuple = audio_channels SCREAMING_SNAKE_CASE : Tuple = normalize SCREAMING_SNAKE_CASE : str = chunk_length_s SCREAMING_SNAKE_CASE : List[str] = overlap SCREAMING_SNAKE_CASE : int = hidden_size SCREAMING_SNAKE_CASE : Optional[int] = num_filters SCREAMING_SNAKE_CASE : Tuple = num_residual_layers SCREAMING_SNAKE_CASE : List[Any] = upsampling_ratios SCREAMING_SNAKE_CASE : Optional[int] = norm_type SCREAMING_SNAKE_CASE : Any = kernel_size SCREAMING_SNAKE_CASE : Union[str, Any] = last_kernel_size SCREAMING_SNAKE_CASE : Tuple = residual_kernel_size SCREAMING_SNAKE_CASE : Any = dilation_growth_rate SCREAMING_SNAKE_CASE : Optional[int] = use_causal_conv SCREAMING_SNAKE_CASE : str = pad_mode SCREAMING_SNAKE_CASE : List[Any] = compress SCREAMING_SNAKE_CASE : Optional[Any] = num_lstm_layers SCREAMING_SNAKE_CASE : Dict = trim_right_ratio SCREAMING_SNAKE_CASE : List[Any] = codebook_size SCREAMING_SNAKE_CASE : Union[str, Any] = codebook_dim if codebook_dim is not None else hidden_size SCREAMING_SNAKE_CASE : Union[str, Any] = use_conv_shortcut if self.norm_type not in ["weight_norm", "time_group_norm"]: raise ValueError( f"self.norm_type must be one of `\"weight_norm\"`, `\"time_group_norm\"`), got {self.norm_type}" ) super().__init__(**lowerCamelCase_ ) @property def __lowerCAmelCase ( self :Optional[Any] ) -> Optional[int]: '''simple docstring''' if self.chunk_length_s is None: return None else: return int(self.chunk_length_s * self.sampling_rate ) @property def __lowerCAmelCase ( self :List[str] ) -> Optional[int]: '''simple docstring''' if self.chunk_length_s is None or self.overlap is None: return None else: return max(1 , int((1.0 - self.overlap) * self.chunk_length ) ) @property def __lowerCAmelCase ( self :Dict ) -> int: '''simple docstring''' SCREAMING_SNAKE_CASE : Tuple = np.prod(self.upsampling_ratios ) return math.ceil(self.sampling_rate / hop_length ) @property def __lowerCAmelCase ( self :Dict ) -> int: '''simple docstring''' return int(10_00 * self.target_bandwidths[-1] // (self.frame_rate * 10) )
18
0
"""simple docstring""" from collections.abc import Iterable from typing import Any class lowercase__: '''simple docstring''' def __init__( self :Optional[int] , lowerCamelCase_ :int | None = None ) -> Optional[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[int] = value SCREAMING_SNAKE_CASE : List[Any] = None # Added in order to delete a node easier SCREAMING_SNAKE_CASE : Tuple = None SCREAMING_SNAKE_CASE : Union[str, Any] = None def __repr__( self :str ) -> str: '''simple docstring''' from pprint import pformat if self.left is None and self.right is None: return str(self.value ) return pformat({f"{self.value}": (self.left, self.right)} , indent=1 ) class lowercase__: '''simple docstring''' def __init__( self :int , lowerCamelCase_ :Node | None = None ) -> str: '''simple docstring''' SCREAMING_SNAKE_CASE : str = root def __str__( self :str ) -> str: '''simple docstring''' return str(self.root ) def __lowerCAmelCase ( self :Any , lowerCamelCase_ :Node , lowerCamelCase_ :Node | None ) -> None: '''simple docstring''' if new_children is not None: # reset its kids SCREAMING_SNAKE_CASE : Any = node.parent if node.parent is not None: # reset its parent if self.is_right(__lowerCAmelCase ): # If it is the right children SCREAMING_SNAKE_CASE : Any = new_children else: SCREAMING_SNAKE_CASE : Tuple = new_children else: SCREAMING_SNAKE_CASE : List[Any] = new_children def __lowerCAmelCase ( self :Tuple , lowerCamelCase_ :Node ) -> bool: '''simple docstring''' if node.parent and node.parent.right: return node == node.parent.right return False def __lowerCAmelCase ( self :str ) -> bool: '''simple docstring''' return self.root is None def __lowerCAmelCase ( self :int , lowerCamelCase_ :List[Any] ) -> None: '''simple docstring''' SCREAMING_SNAKE_CASE : List[Any] = Node(__lowerCAmelCase ) # create a new Node if self.empty(): # if Tree is empty SCREAMING_SNAKE_CASE : Dict = new_node # set its root else: # Tree is not empty SCREAMING_SNAKE_CASE : Tuple = self.root # from root if parent_node is None: return while True: # While we don't get to a leaf if value < parent_node.value: # We go left if parent_node.left is None: SCREAMING_SNAKE_CASE : Dict = new_node # We insert the new node in a leaf break else: SCREAMING_SNAKE_CASE : Dict = parent_node.left else: if parent_node.right is None: SCREAMING_SNAKE_CASE : Optional[int] = new_node break else: SCREAMING_SNAKE_CASE : List[str] = parent_node.right SCREAMING_SNAKE_CASE : Optional[int] = parent_node def __lowerCAmelCase ( self :Any , *lowerCamelCase_ :Any ) -> None: '''simple docstring''' for value in values: self.__insert(__lowerCAmelCase ) def __lowerCAmelCase ( self :Tuple , lowerCamelCase_ :List[str] ) -> Node | None: '''simple docstring''' if self.empty(): raise IndexError('''Warning: Tree is empty! please use another.''' ) else: SCREAMING_SNAKE_CASE : Optional[Any] = self.root # use lazy evaluation here to avoid NoneType Attribute error while node is not None and node.value is not value: SCREAMING_SNAKE_CASE : Optional[Any] = node.left if value < node.value else node.right return node def __lowerCAmelCase ( self :Dict , lowerCamelCase_ :Node | None = None ) -> Node | None: '''simple docstring''' if node is None: if self.root is None: return None SCREAMING_SNAKE_CASE : str = self.root if not self.empty(): while node.right is not None: SCREAMING_SNAKE_CASE : Optional[int] = node.right return node def __lowerCAmelCase ( self :Tuple , lowerCamelCase_ :Node | None = None ) -> Node | None: '''simple docstring''' if node is None: SCREAMING_SNAKE_CASE : str = self.root if self.root is None: return None if not self.empty(): SCREAMING_SNAKE_CASE : Dict = self.root while node.left is not None: SCREAMING_SNAKE_CASE : Optional[int] = node.left return node def __lowerCAmelCase ( self :Any , lowerCamelCase_ :int ) -> None: '''simple docstring''' SCREAMING_SNAKE_CASE : List[str] = self.search(__lowerCAmelCase ) # Look for the node with that label if node is not None: if node.left is None and node.right is None: # If it has no children self.__reassign_nodes(__lowerCAmelCase , __lowerCAmelCase ) elif node.left is None: # Has only right children self.__reassign_nodes(__lowerCAmelCase , node.right ) elif node.right is None: # Has only left children self.__reassign_nodes(__lowerCAmelCase , node.left ) else: SCREAMING_SNAKE_CASE : Optional[int] = self.get_max( node.left ) # Gets the max value of the left branch self.remove(tmp_node.value ) # type: ignore SCREAMING_SNAKE_CASE : Any = ( tmp_node.value # type: ignore ) # Assigns the value to the node to delete and keep tree structure def __lowerCAmelCase ( self :Any , lowerCamelCase_ :Node | None ) -> Iterable: '''simple docstring''' if node is not None: yield node # Preorder Traversal yield from self.preorder_traverse(node.left ) yield from self.preorder_traverse(node.right ) def __lowerCAmelCase ( self :Dict , lowerCamelCase_ :Tuple=None ) -> Any: '''simple docstring''' if traversal_function is None: return self.preorder_traverse(self.root ) else: return traversal_function(self.root ) def __lowerCAmelCase ( self :str , lowerCamelCase_ :list , lowerCamelCase_ :Node | None ) -> None: '''simple docstring''' if node: self.inorder(__lowerCAmelCase , node.left ) arr.append(node.value ) self.inorder(__lowerCAmelCase , node.right ) def __lowerCAmelCase ( self :List[Any] , lowerCamelCase_ :int , lowerCamelCase_ :Node ) -> int: '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[Any] = [] self.inorder(__lowerCAmelCase , __lowerCAmelCase ) # append all values to list using inorder traversal return arr[k - 1] def __A ( a_ : Node | None )-> Optional[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : List[str] = [] if curr_node is not None: SCREAMING_SNAKE_CASE : str = postorder(curr_node.left ) + postorder(curr_node.right ) + [curr_node] return node_list def __A ( )-> Dict: '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[Any] = (8, 3, 6, 1, 10, 14, 13, 4, 7) SCREAMING_SNAKE_CASE : Tuple = BinarySearchTree() for i in testlist: t.insert(UpperCAmelCase__ ) # Prints all the elements of the list in order traversal print(UpperCAmelCase__ ) if t.search(6 ) is not None: print('''The value 6 exists''' ) else: print('''The value 6 doesn\'t exist''' ) if t.search(-1 ) is not None: print('''The value -1 exists''' ) else: print('''The value -1 doesn\'t exist''' ) if not t.empty(): print('''Max Value: ''' , t.get_max().value ) # type: ignore print('''Min Value: ''' , t.get_min().value ) # type: ignore for i in testlist: t.remove(UpperCAmelCase__ ) print(UpperCAmelCase__ ) if __name__ == "__main__": import doctest doctest.testmod(verbose=True)
709
"""simple docstring""" import copy import unittest from transformers.models.auto import get_values from transformers.testing_utils import require_torch, 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, random_attention_mask from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from transformers import ( MODEL_FOR_MULTIPLE_CHOICE_MAPPING, MODEL_FOR_QUESTION_ANSWERING_MAPPING, MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING, MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING, LayoutLMvaConfig, LayoutLMvaForQuestionAnswering, LayoutLMvaForSequenceClassification, LayoutLMvaForTokenClassification, LayoutLMvaModel, ) from transformers.models.layoutlmva.modeling_layoutlmva import LAYOUTLMV3_PRETRAINED_MODEL_ARCHIVE_LIST if is_vision_available(): from PIL import Image from transformers import LayoutLMvaImageProcessor class lowercase__: '''simple docstring''' def __init__( self :Tuple , lowerCamelCase_ :Tuple , lowerCamelCase_ :Union[str, Any]=2 , lowerCamelCase_ :Any=3 , lowerCamelCase_ :Union[str, Any]=4 , lowerCamelCase_ :List[str]=2 , lowerCamelCase_ :str=7 , lowerCamelCase_ :Tuple=True , lowerCamelCase_ :Dict=True , lowerCamelCase_ :int=True , lowerCamelCase_ :List[Any]=True , lowerCamelCase_ :Optional[Any]=99 , lowerCamelCase_ :Any=36 , lowerCamelCase_ :Any=3 , lowerCamelCase_ :str=4 , lowerCamelCase_ :Tuple=37 , lowerCamelCase_ :Optional[int]="gelu" , lowerCamelCase_ :str=0.1 , lowerCamelCase_ :Optional[int]=0.1 , lowerCamelCase_ :Tuple=5_12 , lowerCamelCase_ :Optional[Any]=16 , lowerCamelCase_ :List[str]=2 , lowerCamelCase_ :Optional[int]=0.0_2 , lowerCamelCase_ :int=6 , lowerCamelCase_ :str=6 , lowerCamelCase_ :Optional[Any]=3 , lowerCamelCase_ :Union[str, Any]=4 , lowerCamelCase_ :List[Any]=None , lowerCamelCase_ :Tuple=10_00 , ) -> Union[str, Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : Dict = parent SCREAMING_SNAKE_CASE : Optional[Any] = batch_size SCREAMING_SNAKE_CASE : List[str] = num_channels SCREAMING_SNAKE_CASE : str = image_size SCREAMING_SNAKE_CASE : Optional[int] = patch_size SCREAMING_SNAKE_CASE : Tuple = text_seq_length SCREAMING_SNAKE_CASE : Optional[int] = is_training SCREAMING_SNAKE_CASE : Dict = use_input_mask SCREAMING_SNAKE_CASE : Any = use_token_type_ids SCREAMING_SNAKE_CASE : List[Any] = use_labels SCREAMING_SNAKE_CASE : List[Any] = vocab_size SCREAMING_SNAKE_CASE : Optional[Any] = hidden_size SCREAMING_SNAKE_CASE : Tuple = num_hidden_layers SCREAMING_SNAKE_CASE : Optional[int] = num_attention_heads SCREAMING_SNAKE_CASE : Optional[int] = intermediate_size SCREAMING_SNAKE_CASE : List[str] = hidden_act SCREAMING_SNAKE_CASE : Any = hidden_dropout_prob SCREAMING_SNAKE_CASE : Optional[Any] = attention_probs_dropout_prob SCREAMING_SNAKE_CASE : Union[str, Any] = max_position_embeddings SCREAMING_SNAKE_CASE : int = type_vocab_size SCREAMING_SNAKE_CASE : Union[str, Any] = type_sequence_label_size SCREAMING_SNAKE_CASE : Optional[Any] = initializer_range SCREAMING_SNAKE_CASE : Dict = coordinate_size SCREAMING_SNAKE_CASE : List[Any] = shape_size SCREAMING_SNAKE_CASE : Dict = num_labels SCREAMING_SNAKE_CASE : Union[str, Any] = num_choices SCREAMING_SNAKE_CASE : List[str] = scope SCREAMING_SNAKE_CASE : Optional[int] = range_bbox # LayoutLMv3's sequence length equals the number of text tokens + number of patches + 1 (we add 1 for the CLS token) SCREAMING_SNAKE_CASE : str = text_seq_length SCREAMING_SNAKE_CASE : int = (image_size // patch_size) ** 2 + 1 SCREAMING_SNAKE_CASE : Optional[Any] = self.text_seq_length + self.image_seq_length def __lowerCAmelCase ( self :List[Any] ) -> Optional[int]: '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[int] = ids_tensor([self.batch_size, self.text_seq_length] , self.vocab_size ) SCREAMING_SNAKE_CASE : List[Any] = ids_tensor([self.batch_size, self.text_seq_length, 4] , self.range_bbox ) # Ensure that bbox is legal for i in range(bbox.shape[0] ): for j in range(bbox.shape[1] ): if bbox[i, j, 3] < bbox[i, j, 1]: SCREAMING_SNAKE_CASE : str = bbox[i, j, 3] SCREAMING_SNAKE_CASE : List[str] = bbox[i, j, 1] SCREAMING_SNAKE_CASE : Any = t if bbox[i, j, 2] < bbox[i, j, 0]: SCREAMING_SNAKE_CASE : Any = bbox[i, j, 2] SCREAMING_SNAKE_CASE : Any = bbox[i, j, 0] SCREAMING_SNAKE_CASE : Optional[Any] = t SCREAMING_SNAKE_CASE : Dict = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) SCREAMING_SNAKE_CASE : Optional[Any] = None if self.use_input_mask: SCREAMING_SNAKE_CASE : Dict = random_attention_mask([self.batch_size, self.text_seq_length] ) SCREAMING_SNAKE_CASE : Any = None if self.use_token_type_ids: SCREAMING_SNAKE_CASE : Tuple = ids_tensor([self.batch_size, self.text_seq_length] , self.type_vocab_size ) SCREAMING_SNAKE_CASE : Tuple = None SCREAMING_SNAKE_CASE : Optional[int] = None if self.use_labels: SCREAMING_SNAKE_CASE : List[str] = ids_tensor([self.batch_size] , self.type_sequence_label_size ) SCREAMING_SNAKE_CASE : Dict = ids_tensor([self.batch_size, self.text_seq_length] , self.num_labels ) SCREAMING_SNAKE_CASE : List[str] = 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 __lowerCAmelCase ( self :int , lowerCamelCase_ :Union[str, Any] , lowerCamelCase_ :int , lowerCamelCase_ :str , lowerCamelCase_ :int , lowerCamelCase_ :Union[str, Any] , lowerCamelCase_ :Any , lowerCamelCase_ :Dict , lowerCamelCase_ :Union[str, Any] ) -> Any: '''simple docstring''' SCREAMING_SNAKE_CASE : Dict = LayoutLMvaModel(config=lowerCamelCase_ ) model.to(lowerCamelCase_ ) model.eval() # text + image SCREAMING_SNAKE_CASE : Optional[Any] = model(lowerCamelCase_ , pixel_values=lowerCamelCase_ ) SCREAMING_SNAKE_CASE : List[str] = model( lowerCamelCase_ , bbox=lowerCamelCase_ , pixel_values=lowerCamelCase_ , attention_mask=lowerCamelCase_ , token_type_ids=lowerCamelCase_ ) SCREAMING_SNAKE_CASE : List[Any] = model(lowerCamelCase_ , bbox=lowerCamelCase_ , pixel_values=lowerCamelCase_ , token_type_ids=lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Optional[Any] = model(lowerCamelCase_ , bbox=lowerCamelCase_ , pixel_values=lowerCamelCase_ ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) # text only SCREAMING_SNAKE_CASE : List[str] = model(lowerCamelCase_ ) self.parent.assertEqual( result.last_hidden_state.shape , (self.batch_size, self.text_seq_length, self.hidden_size) ) # image only SCREAMING_SNAKE_CASE : List[str] = model(pixel_values=lowerCamelCase_ ) self.parent.assertEqual( result.last_hidden_state.shape , (self.batch_size, self.image_seq_length, self.hidden_size) ) def __lowerCAmelCase ( self :str , lowerCamelCase_ :Union[str, Any] , lowerCamelCase_ :Union[str, Any] , lowerCamelCase_ :Tuple , lowerCamelCase_ :List[str] , lowerCamelCase_ :Any , lowerCamelCase_ :int , lowerCamelCase_ :List[Any] , lowerCamelCase_ :List[str] ) -> Tuple: '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[Any] = self.num_labels SCREAMING_SNAKE_CASE : Dict = LayoutLMvaForSequenceClassification(lowerCamelCase_ ) model.to(lowerCamelCase_ ) model.eval() SCREAMING_SNAKE_CASE : int = model( lowerCamelCase_ , bbox=lowerCamelCase_ , pixel_values=lowerCamelCase_ , attention_mask=lowerCamelCase_ , token_type_ids=lowerCamelCase_ , labels=lowerCamelCase_ , ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_labels) ) def __lowerCAmelCase ( self :Union[str, Any] , lowerCamelCase_ :Tuple , lowerCamelCase_ :Any , lowerCamelCase_ :Optional[int] , lowerCamelCase_ :Any , lowerCamelCase_ :Optional[int] , lowerCamelCase_ :int , lowerCamelCase_ :str , lowerCamelCase_ :Any ) -> Union[str, Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : int = self.num_labels SCREAMING_SNAKE_CASE : int = LayoutLMvaForTokenClassification(config=lowerCamelCase_ ) model.to(lowerCamelCase_ ) model.eval() SCREAMING_SNAKE_CASE : Optional[Any] = model( lowerCamelCase_ , bbox=lowerCamelCase_ , pixel_values=lowerCamelCase_ , attention_mask=lowerCamelCase_ , token_type_ids=lowerCamelCase_ , labels=lowerCamelCase_ , ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.text_seq_length, self.num_labels) ) def __lowerCAmelCase ( self :Optional[Any] , lowerCamelCase_ :Dict , lowerCamelCase_ :Dict , lowerCamelCase_ :Dict , lowerCamelCase_ :int , lowerCamelCase_ :List[str] , lowerCamelCase_ :int , lowerCamelCase_ :str , lowerCamelCase_ :Optional[int] ) -> Union[str, Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[int] = LayoutLMvaForQuestionAnswering(config=lowerCamelCase_ ) model.to(lowerCamelCase_ ) model.eval() SCREAMING_SNAKE_CASE : List[Any] = model( lowerCamelCase_ , bbox=lowerCamelCase_ , pixel_values=lowerCamelCase_ , attention_mask=lowerCamelCase_ , token_type_ids=lowerCamelCase_ , start_positions=lowerCamelCase_ , end_positions=lowerCamelCase_ , ) self.parent.assertEqual(result.start_logits.shape , (self.batch_size, self.seq_length) ) self.parent.assertEqual(result.end_logits.shape , (self.batch_size, self.seq_length) ) def __lowerCAmelCase ( self :Optional[int] ) -> Optional[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[int] = self.prepare_config_and_inputs() ( ( SCREAMING_SNAKE_CASE ), ( SCREAMING_SNAKE_CASE ), ( SCREAMING_SNAKE_CASE ), ( SCREAMING_SNAKE_CASE ), ( SCREAMING_SNAKE_CASE ), ( SCREAMING_SNAKE_CASE ), ( SCREAMING_SNAKE_CASE ), ( SCREAMING_SNAKE_CASE ), ) : Tuple = config_and_inputs SCREAMING_SNAKE_CASE : Dict = { '''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_torch class lowercase__( _UpperCAmelCase , _UpperCAmelCase , unittest.TestCase ): '''simple docstring''' UpperCamelCase = False UpperCamelCase = False UpperCamelCase = False UpperCamelCase = ( ( LayoutLMvaModel, LayoutLMvaForSequenceClassification, LayoutLMvaForTokenClassification, LayoutLMvaForQuestionAnswering, ) if is_torch_available() else () ) UpperCamelCase = ( {"""document-question-answering""": LayoutLMvaForQuestionAnswering, """feature-extraction""": LayoutLMvaModel} if is_torch_available() else {} ) def __lowerCAmelCase ( self :int , lowerCamelCase_ :Union[str, Any] , lowerCamelCase_ :str , lowerCamelCase_ :str , lowerCamelCase_ :Optional[Any] , lowerCamelCase_ :List[str] ) -> Union[str, Any]: '''simple docstring''' return True def __lowerCAmelCase ( self :List[str] ) -> Union[str, Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : str = LayoutLMvaModelTester(self ) SCREAMING_SNAKE_CASE : List[Any] = ConfigTester(self , config_class=lowerCamelCase_ , hidden_size=37 ) def __lowerCAmelCase ( self :Any , lowerCamelCase_ :Dict , lowerCamelCase_ :List[str] , lowerCamelCase_ :str=False ) -> Tuple: '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[Any] = copy.deepcopy(lowerCamelCase_ ) if model_class in get_values(lowerCamelCase_ ): SCREAMING_SNAKE_CASE : Tuple = { k: v.unsqueeze(1 ).expand(-1 , self.model_tester.num_choices , -1 ).contiguous() if isinstance(lowerCamelCase_ , torch.Tensor ) and v.ndim > 1 else v for k, v in inputs_dict.items() } if return_labels: if model_class in get_values(lowerCamelCase_ ): SCREAMING_SNAKE_CASE : Union[str, Any] = torch.ones(self.model_tester.batch_size , dtype=torch.long , device=lowerCamelCase_ ) elif model_class in get_values(lowerCamelCase_ ): SCREAMING_SNAKE_CASE : List[Any] = torch.zeros( self.model_tester.batch_size , dtype=torch.long , device=lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Any = torch.zeros( self.model_tester.batch_size , dtype=torch.long , device=lowerCamelCase_ ) elif model_class in [ *get_values(lowerCamelCase_ ), ]: SCREAMING_SNAKE_CASE : str = torch.zeros( self.model_tester.batch_size , dtype=torch.long , device=lowerCamelCase_ ) elif model_class in [ *get_values(lowerCamelCase_ ), ]: SCREAMING_SNAKE_CASE : Dict = torch.zeros( (self.model_tester.batch_size, self.model_tester.text_seq_length) , dtype=torch.long , device=lowerCamelCase_ , ) return inputs_dict def __lowerCAmelCase ( self :Optional[Any] ) -> Optional[int]: '''simple docstring''' self.config_tester.run_common_tests() def __lowerCAmelCase ( self :str ) -> List[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : int = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*lowerCamelCase_ ) def __lowerCAmelCase ( self :Any ) -> List[str]: '''simple docstring''' SCREAMING_SNAKE_CASE : int = self.model_tester.prepare_config_and_inputs() for type in ["absolute", "relative_key", "relative_key_query"]: SCREAMING_SNAKE_CASE : str = type self.model_tester.create_and_check_model(*lowerCamelCase_ ) def __lowerCAmelCase ( self :Union[str, Any] ) -> str: '''simple docstring''' SCREAMING_SNAKE_CASE : List[Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_sequence_classification(*lowerCamelCase_ ) def __lowerCAmelCase ( self :str ) -> Any: '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_token_classification(*lowerCamelCase_ ) def __lowerCAmelCase ( self :List[str] ) -> Union[str, Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : int = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_question_answering(*lowerCamelCase_ ) @slow def __lowerCAmelCase ( self :int ) -> Union[str, Any]: '''simple docstring''' for model_name in LAYOUTLMV3_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: SCREAMING_SNAKE_CASE : Optional[int] = LayoutLMvaModel.from_pretrained(lowerCamelCase_ ) self.assertIsNotNone(lowerCamelCase_ ) def __A ( )-> Optional[int]: '''simple docstring''' SCREAMING_SNAKE_CASE : Tuple = Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png''' ) return image @require_torch class lowercase__( unittest.TestCase ): '''simple docstring''' @cached_property def __lowerCAmelCase ( self :str ) -> int: '''simple docstring''' return LayoutLMvaImageProcessor(apply_ocr=lowerCamelCase_ ) if is_vision_available() else None @slow def __lowerCAmelCase ( self :Optional[Any] ) -> Dict: '''simple docstring''' SCREAMING_SNAKE_CASE : Union[str, Any] = LayoutLMvaModel.from_pretrained('''microsoft/layoutlmv3-base''' ).to(lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Any = self.default_image_processor SCREAMING_SNAKE_CASE : List[Any] = prepare_img() SCREAMING_SNAKE_CASE : Tuple = image_processor(images=lowerCamelCase_ , return_tensors='''pt''' ).pixel_values.to(lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Optional[Any] = torch.tensor([[1, 2]] ) SCREAMING_SNAKE_CASE : Dict = torch.tensor([[1, 2, 3, 4], [5, 6, 7, 8]] ).unsqueeze(0 ) # forward pass SCREAMING_SNAKE_CASE : Tuple = model( input_ids=input_ids.to(lowerCamelCase_ ) , bbox=bbox.to(lowerCamelCase_ ) , pixel_values=pixel_values.to(lowerCamelCase_ ) , ) # verify the logits SCREAMING_SNAKE_CASE : Union[str, Any] = torch.Size((1, 1_99, 7_68) ) self.assertEqual(outputs.last_hidden_state.shape , lowerCamelCase_ ) SCREAMING_SNAKE_CASE : List[Any] = torch.tensor( [[-0.0_5_2_9, 0.3_6_1_8, 0.1_6_3_2], [-0.1_5_8_7, -0.1_6_6_7, -0.0_4_0_0], [-0.1_5_5_7, -0.1_6_7_1, -0.0_5_0_5]] ).to(lowerCamelCase_ ) self.assertTrue(torch.allclose(outputs.last_hidden_state[0, :3, :3] , lowerCamelCase_ , atol=1E-4 ) )
18
0
"""simple docstring""" import argparse import os import re lowerCamelCase__ : Tuple = "src/transformers/models/auto" # re pattern that matches mapping introductions: # SUPER_MODEL_MAPPING_NAMES = OrderedDict or SUPER_MODEL_MAPPING = OrderedDict lowerCamelCase__ : List[str] = re.compile(r"[A-Z_]+_MAPPING(\s+|_[A-Z_]+\s+)=\s+OrderedDict") # re pattern that matches identifiers in mappings lowerCamelCase__ : Union[str, Any] = re.compile(r"\s*\(\s*\"(\S[^\"]+)\"") def __A ( a_ : List[Any] , a_ : int = False )-> Any: '''simple docstring''' with open(_snake_case , '''r''' , encoding='''utf-8''' ) as f: SCREAMING_SNAKE_CASE : Dict = f.read() SCREAMING_SNAKE_CASE : Dict = content.split('''\n''' ) SCREAMING_SNAKE_CASE : List[Any] = [] SCREAMING_SNAKE_CASE : Union[str, Any] = 0 while line_idx < len(_snake_case ): if _re_intro_mapping.search(lines[line_idx] ) is not None: SCREAMING_SNAKE_CASE : List[str] = len(re.search(r'''^(\s*)\S''' , lines[line_idx] ).groups()[0] ) + 8 # Start of a new mapping! while not lines[line_idx].startswith(''' ''' * indent + '''(''' ): new_lines.append(lines[line_idx] ) line_idx += 1 SCREAMING_SNAKE_CASE : str = [] while lines[line_idx].strip() != "]": # Blocks either fit in one line or not if lines[line_idx].strip() == "(": SCREAMING_SNAKE_CASE : Optional[Any] = line_idx while not lines[line_idx].startswith(''' ''' * indent + ''')''' ): line_idx += 1 blocks.append('''\n'''.join(lines[start_idx : line_idx + 1] ) ) else: blocks.append(lines[line_idx] ) line_idx += 1 # Sort blocks by their identifiers SCREAMING_SNAKE_CASE : str = sorted(_snake_case , key=lambda a_ : _re_identifier.search(_snake_case ).groups()[0] ) new_lines += blocks else: new_lines.append(lines[line_idx] ) line_idx += 1 if overwrite: with open(_snake_case , '''w''' , encoding='''utf-8''' ) as f: f.write('''\n'''.join(_snake_case ) ) elif "\n".join(_snake_case ) != content: return True def __A ( a_ : Optional[Any] = False )-> Optional[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : int = [os.path.join(_snake_case , _snake_case ) for f in os.listdir(_snake_case ) if f.endswith('''.py''' )] SCREAMING_SNAKE_CASE : Tuple = [sort_auto_mapping(_snake_case , overwrite=_snake_case ) for fname in fnames] if not overwrite and any(_snake_case ): SCREAMING_SNAKE_CASE : int = [f for f, d in zip(_snake_case , _snake_case ) if d] raise ValueError( F"The following files have auto mappings that need sorting: {', '.join(_snake_case )}. Run `make style` to fix" ''' this.''' ) if __name__ == "__main__": lowerCamelCase__ : int = argparse.ArgumentParser() parser.add_argument("--check_only", action="store_true", help="Whether to only check or fix style.") lowerCamelCase__ : List[str] = parser.parse_args() sort_all_auto_mappings(not args.check_only)
710
"""simple docstring""" import logging import os import random import sys from dataclasses import dataclass, field from typing import Optional import datasets import numpy as np import pandas as pd from datasets import load_dataset import transformers from transformers import ( AutoConfig, BartForSequenceClassification, DataCollatorWithPadding, EvalPrediction, HfArgumentParser, TapexTokenizer, Trainer, TrainingArguments, default_data_collator, set_seed, ) from transformers.trainer_utils import get_last_checkpoint from transformers.utils import check_min_version from transformers.utils.versions import require_version # Will error if the minimal version of Transformers is not installed. Remove at your own risks. check_min_version("4.17.0.dev0") require_version("datasets>=1.8.0", "To fix: pip install -r examples/pytorch/text-classification/requirements.txt") lowerCamelCase__ : Any = logging.getLogger(__name__) @dataclass class lowercase__: '''simple docstring''' UpperCamelCase = field( default="""tab_fact""" , metadata={"""help""": """The name of the dataset to use (via the datasets library)."""} ) UpperCamelCase = field( default="""tab_fact""" , metadata={"""help""": """The configuration name of the dataset to use (via the datasets library)."""} , ) UpperCamelCase = field( default=10_24 , metadata={ """help""": ( """The maximum total input sequence length after tokenization. Sequences longer """ """than this will be truncated, sequences shorter will be padded.""" ) } , ) UpperCamelCase = field( default=_UpperCAmelCase , metadata={"""help""": """Overwrite the cached preprocessed datasets or not."""} ) UpperCamelCase = field( default=_UpperCAmelCase , metadata={ """help""": ( """Whether to pad all samples to `max_seq_length`. """ """If False, will pad the samples dynamically when batching to the maximum length in the batch.""" ) } , ) UpperCamelCase = field( default=_UpperCAmelCase , metadata={ """help""": ( """For debugging purposes or quicker training, truncate the number of training examples to this """ """value if set.""" ) } , ) UpperCamelCase = field( default=_UpperCAmelCase , metadata={ """help""": ( """For debugging purposes or quicker training, truncate the number of evaluation examples to this """ """value if set.""" ) } , ) UpperCamelCase = field( default=_UpperCAmelCase , metadata={ """help""": ( """For debugging purposes or quicker training, truncate the number of prediction examples to this """ """value if set.""" ) } , ) UpperCamelCase = field( default=_UpperCAmelCase , metadata={"""help""": """A csv or a json file containing the training data."""} ) UpperCamelCase = field( default=_UpperCAmelCase , metadata={"""help""": """A csv or a json file containing the validation data."""} ) UpperCamelCase = field(default=_UpperCAmelCase , metadata={"""help""": """A csv or a json file containing the test data."""} ) def __lowerCAmelCase ( self :Union[str, Any] ) -> Dict: '''simple docstring''' if self.dataset_name is not None: pass elif self.train_file is None or self.validation_file is None: raise ValueError('''Need either a GLUE task, a training/validation file or a dataset name.''' ) else: SCREAMING_SNAKE_CASE : Union[str, Any] = self.train_file.split('''.''' )[-1] assert train_extension in ["csv", "json"], "`train_file` should be a csv or a json file." SCREAMING_SNAKE_CASE : Optional[int] = self.validation_file.split('''.''' )[-1] assert ( validation_extension == train_extension ), "`validation_file` should have the same extension (csv or json) as `train_file`." @dataclass class lowercase__: '''simple docstring''' UpperCamelCase = field( default=_UpperCAmelCase , metadata={"""help""": """Path to pretrained model or model identifier from huggingface.co/models"""} ) UpperCamelCase = field( default=_UpperCAmelCase , metadata={"""help""": """Pretrained config name or path if not the same as model_name"""} ) UpperCamelCase = field( default=_UpperCAmelCase , metadata={"""help""": """Pretrained tokenizer name or path if not the same as model_name"""} ) UpperCamelCase = field( default=_UpperCAmelCase , metadata={"""help""": """Where do you want to store the pretrained models downloaded from huggingface.co"""} , ) UpperCamelCase = field( default=_UpperCAmelCase , metadata={"""help""": """Whether to use one of the fast tokenizer (backed by the tokenizers library) or not."""} , ) UpperCamelCase = field( default="""main""" , metadata={"""help""": """The specific model version to use (can be a branch name, tag name or commit id)."""} , ) UpperCamelCase = field( default=_UpperCAmelCase , metadata={ """help""": ( """Will use the token generated when running `huggingface-cli login` (necessary to use this script """ """with private models).""" ) } , ) def __A ( )-> List[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : Union[str, Any] = 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 : Dict = parser.parse_json_file(json_file=os.path.abspath(sys.argv[1] ) ) else: SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : Dict = parser.parse_args_into_dataclasses() # 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 )] , ) SCREAMING_SNAKE_CASE : Union[str, Any] = training_args.get_process_log_level() logger.setLevel(a_ ) datasets.utils.logging.set_verbosity(a_ ) transformers.utils.logging.set_verbosity(a_ ) 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 : 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 ) # Get the datasets: you can either provide your own CSV/JSON training and evaluation files (see below) # or specify a GLUE benchmark task (the dataset will be downloaded automatically from the datasets Hub). # # For JSON files, this script will use the `question` column for the input question and `table` column for the corresponding table. # # If the CSVs/JSONs contain only one non-label column, the script does single sentence classification on this # single column. You can easily tweak this behavior (see below) # # In distributed training, the load_dataset function guarantee that only one local process can concurrently # download the dataset. if data_args.dataset_name is not None: # Downloading and loading a dataset from the hub. SCREAMING_SNAKE_CASE : List[str] = load_dataset( data_args.dataset_name , data_args.dataset_config_name , cache_dir=model_args.cache_dir ) else: # Loading a dataset from your local files. # CSV/JSON training and evaluation files are needed. SCREAMING_SNAKE_CASE : Any = {'''train''': data_args.train_file, '''validation''': data_args.validation_file} # Get the test dataset: you can provide your own CSV/JSON test file (see below) # when you use `do_predict` without specifying a GLUE benchmark task. if training_args.do_predict: if data_args.test_file is not None: SCREAMING_SNAKE_CASE : List[Any] = data_args.train_file.split('''.''' )[-1] SCREAMING_SNAKE_CASE : Optional[int] = data_args.test_file.split('''.''' )[-1] assert ( test_extension == train_extension ), "`test_file` should have the same extension (csv or json) as `train_file`." SCREAMING_SNAKE_CASE : str = data_args.test_file else: raise ValueError('''Need either a GLUE task or a test file for `do_predict`.''' ) for key in data_files.keys(): logger.info(F"load a local file for {key}: {data_files[key]}" ) if data_args.train_file.endswith('''.csv''' ): # Loading a dataset from local csv files SCREAMING_SNAKE_CASE : int = load_dataset('''csv''' , data_files=a_ , cache_dir=model_args.cache_dir ) else: # Loading a dataset from local json files SCREAMING_SNAKE_CASE : Tuple = load_dataset('''json''' , data_files=a_ , cache_dir=model_args.cache_dir ) # See more about loading any type of standard or custom dataset at # https://huggingface.co/docs/datasets/loading_datasets.html. # Labels SCREAMING_SNAKE_CASE : str = raw_datasets['''train'''].features['''label'''].names SCREAMING_SNAKE_CASE : Union[str, Any] = len(a_ ) # Load pretrained model and tokenizer # # In distributed training, the .from_pretrained methods guarantee that only one local process can concurrently # download model & vocab. SCREAMING_SNAKE_CASE : Optional[Any] = AutoConfig.from_pretrained( model_args.config_name if model_args.config_name else model_args.model_name_or_path , num_labels=a_ , cache_dir=model_args.cache_dir , revision=model_args.model_revision , use_auth_token=True if model_args.use_auth_token else None , ) # load tapex tokenizer SCREAMING_SNAKE_CASE : Dict = TapexTokenizer.from_pretrained( model_args.tokenizer_name if model_args.tokenizer_name else model_args.model_name_or_path , cache_dir=model_args.cache_dir , use_fast=model_args.use_fast_tokenizer , revision=model_args.model_revision , use_auth_token=True if model_args.use_auth_token else None , add_prefix_space=a_ , ) SCREAMING_SNAKE_CASE : List[Any] = BartForSequenceClassification.from_pretrained( model_args.model_name_or_path , from_tf=bool('''.ckpt''' in model_args.model_name_or_path ) , config=a_ , cache_dir=model_args.cache_dir , revision=model_args.model_revision , use_auth_token=True if model_args.use_auth_token else None , ) # Padding strategy if data_args.pad_to_max_length: SCREAMING_SNAKE_CASE : Tuple = '''max_length''' else: # We will pad later, dynamically at batch creation, to the max sequence length in each batch SCREAMING_SNAKE_CASE : Optional[Any] = False # Some models have set the order of the labels to use, so let's make sure we do use it. SCREAMING_SNAKE_CASE : Tuple = {'''Refused''': 0, '''Entailed''': 1} SCREAMING_SNAKE_CASE : List[Any] = {0: '''Refused''', 1: '''Entailed'''} if data_args.max_seq_length > tokenizer.model_max_length: logger.warning( F"The max_seq_length passed ({data_args.max_seq_length}) is larger than the maximum length for the" F"model ({tokenizer.model_max_length}). Using max_seq_length={tokenizer.model_max_length}." ) SCREAMING_SNAKE_CASE : Optional[int] = min(data_args.max_seq_length , tokenizer.model_max_length ) def preprocess_tabfact_function(a_ : str ): # Tokenize the texts def _convert_table_text_to_pandas(a_ : List[Any] ): SCREAMING_SNAKE_CASE : List[Any] = [_table_row.split('''#''' ) for _table_row in _table_text.strip('''\n''' ).split('''\n''' )] SCREAMING_SNAKE_CASE : Dict = pd.DataFrame.from_records(_table_content[1:] , columns=_table_content[0] ) return _table_pd SCREAMING_SNAKE_CASE : List[Any] = examples['''statement'''] SCREAMING_SNAKE_CASE : Optional[int] = list(map(_convert_table_text_to_pandas , examples['''table_text'''] ) ) SCREAMING_SNAKE_CASE : Any = tokenizer(a_ , a_ , padding=a_ , max_length=a_ , truncation=a_ ) SCREAMING_SNAKE_CASE : List[Any] = examples['''label'''] return result with training_args.main_process_first(desc='''dataset map pre-processing''' ): SCREAMING_SNAKE_CASE : Optional[Any] = raw_datasets.map( a_ , batched=a_ , load_from_cache_file=not data_args.overwrite_cache , desc='''Running tokenizer on dataset''' , ) if training_args.do_train: if "train" not in raw_datasets: raise ValueError('''--do_train requires a train dataset''' ) SCREAMING_SNAKE_CASE : List[str] = raw_datasets['''train'''] if data_args.max_train_samples is not None: SCREAMING_SNAKE_CASE : Any = train_dataset.select(range(data_args.max_train_samples ) ) if training_args.do_eval: if "validation" not in raw_datasets and "validation_matched" not in raw_datasets: raise ValueError('''--do_eval requires a validation dataset''' ) SCREAMING_SNAKE_CASE : List[str] = raw_datasets['''validation'''] if data_args.max_eval_samples is not None: SCREAMING_SNAKE_CASE : Union[str, Any] = eval_dataset.select(range(data_args.max_eval_samples ) ) if training_args.do_predict or data_args.test_file is not None: if "test" not in raw_datasets and "test_matched" not in raw_datasets: raise ValueError('''--do_predict requires a test dataset''' ) SCREAMING_SNAKE_CASE : Tuple = raw_datasets['''test'''] if data_args.max_predict_samples is not None: SCREAMING_SNAKE_CASE : Optional[int] = predict_dataset.select(range(data_args.max_predict_samples ) ) # Log a few random samples from the training set: if training_args.do_train: for index in random.sample(range(len(a_ ) ) , 3 ): logger.info(F"Sample {index} of the training set: {train_dataset[index]}." ) # You can define your custom 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(a_ : EvalPrediction ): SCREAMING_SNAKE_CASE : str = p.predictions[0] if isinstance(p.predictions , a_ ) else p.predictions SCREAMING_SNAKE_CASE : Tuple = np.argmax(a_ , axis=1 ) return {"accuracy": (preds == p.label_ids).astype(np.floataa ).mean().item()} # Data collator will default to DataCollatorWithPadding, so we change it if we already did the padding. if data_args.pad_to_max_length: SCREAMING_SNAKE_CASE : Tuple = default_data_collator elif training_args.fpaa: SCREAMING_SNAKE_CASE : Union[str, Any] = DataCollatorWithPadding(a_ , pad_to_multiple_of=8 ) else: SCREAMING_SNAKE_CASE : List[Any] = None # Initialize our Trainer SCREAMING_SNAKE_CASE : Optional[Any] = Trainer( model=a_ , args=a_ , train_dataset=train_dataset if training_args.do_train else None , eval_dataset=eval_dataset if training_args.do_eval else None , compute_metrics=a_ , tokenizer=a_ , data_collator=a_ , ) # Training if training_args.do_train: SCREAMING_SNAKE_CASE : List[str] = None if training_args.resume_from_checkpoint is not None: SCREAMING_SNAKE_CASE : Dict = training_args.resume_from_checkpoint elif last_checkpoint is not None: SCREAMING_SNAKE_CASE : str = last_checkpoint SCREAMING_SNAKE_CASE : str = trainer.train(resume_from_checkpoint=a_ ) SCREAMING_SNAKE_CASE : Optional[int] = train_result.metrics SCREAMING_SNAKE_CASE : int = ( data_args.max_train_samples if data_args.max_train_samples is not None else len(a_ ) ) SCREAMING_SNAKE_CASE : Optional[int] = min(a_ , len(a_ ) ) trainer.save_model() # Saves the tokenizer too for easy upload trainer.log_metrics('''train''' , a_ ) trainer.save_metrics('''train''' , a_ ) trainer.save_state() # Evaluation if training_args.do_eval: logger.info('''*** Evaluate ***''' ) SCREAMING_SNAKE_CASE : Tuple = trainer.evaluate(eval_dataset=a_ ) SCREAMING_SNAKE_CASE : str = data_args.max_eval_samples if data_args.max_eval_samples is not None else len(a_ ) SCREAMING_SNAKE_CASE : Union[str, Any] = min(a_ , len(a_ ) ) trainer.log_metrics('''eval''' , a_ ) trainer.save_metrics('''eval''' , a_ ) if training_args.do_predict: logger.info('''*** Predict ***''' ) # Removing the `label` columns because it contains -1 and Trainer won't like that. SCREAMING_SNAKE_CASE : Optional[Any] = predict_dataset.remove_columns('''label''' ) SCREAMING_SNAKE_CASE : Optional[Any] = trainer.predict(a_ , metric_key_prefix='''predict''' ).predictions SCREAMING_SNAKE_CASE : Union[str, Any] = np.argmax(a_ , axis=1 ) SCREAMING_SNAKE_CASE : Optional[int] = os.path.join(training_args.output_dir , '''predict_results_tabfact.txt''' ) if trainer.is_world_process_zero(): with open(a_ , '''w''' ) as writer: logger.info('''***** Predict Results *****''' ) writer.write('''index\tprediction\n''' ) for index, item in enumerate(a_ ): SCREAMING_SNAKE_CASE : Union[str, Any] = label_list[item] writer.write(F"{index}\t{item}\n" ) SCREAMING_SNAKE_CASE : Optional[int] = {'''finetuned_from''': model_args.model_name_or_path, '''tasks''': '''text-classification'''} if training_args.push_to_hub: trainer.push_to_hub(**a_ ) else: trainer.create_model_card(**a_ ) def __A ( a_ : List[str] )-> int: '''simple docstring''' main() if __name__ == "__main__": main()
18
0
"""simple docstring""" from unittest.mock import patch import pyspark from datasets.packaged_modules.spark.spark import ( Spark, SparkExamplesIterable, _generate_iterable_examples, ) from ..utils import ( require_dill_gt_0_3_2, require_not_windows, ) def __A ( a_ : List[Any] , a_ : str )-> Optional[int]: '''simple docstring''' SCREAMING_SNAKE_CASE : List[Any] = [] for part_id in partition_order: SCREAMING_SNAKE_CASE : Optional[Any] = df.where(F"SPARK_PARTITION_ID() = {part_id}" ).collect() for row_idx, row in enumerate(_lowercase ): expected_row_ids_and_row_dicts.append((F"{part_id}_{row_idx}", row.asDict()) ) return expected_row_ids_and_row_dicts @require_not_windows @require_dill_gt_0_3_2 def __A ( )-> List[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : int = pyspark.sql.SparkSession.builder.master('''local[*]''' ).appName('''pyspark''' ).getOrCreate() SCREAMING_SNAKE_CASE : Tuple = spark.range(1_00 ).repartition(1 ) SCREAMING_SNAKE_CASE : Optional[int] = Spark(_lowercase ) # The id ints will be converted to Pyarrow int64s, so each row will be 8 bytes. Setting a max_shard_size of 16 means # that each partition can hold 2 rows. spark_builder._repartition_df_if_needed(max_shard_size=16 ) # Given that the dataframe has 100 rows and each partition has 2 rows, we expect 50 partitions. assert spark_builder.df.rdd.getNumPartitions() == 50 @require_not_windows @require_dill_gt_0_3_2 def __A ( )-> Optional[int]: '''simple docstring''' SCREAMING_SNAKE_CASE : Dict = pyspark.sql.SparkSession.builder.master('''local[*]''' ).appName('''pyspark''' ).getOrCreate() SCREAMING_SNAKE_CASE : List[Any] = spark.range(10 ).repartition(2 ) SCREAMING_SNAKE_CASE : Optional[int] = [1, 0] SCREAMING_SNAKE_CASE : int = _generate_iterable_examples(_lowercase , _lowercase ) # Reverse the partitions. SCREAMING_SNAKE_CASE : str = _get_expected_row_ids_and_row_dicts_for_partition_order(_lowercase , _lowercase ) for i, (row_id, row_dict) in enumerate(generate_fn() ): SCREAMING_SNAKE_CASE : Dict = expected_row_ids_and_row_dicts[i] assert row_id == expected_row_id assert row_dict == expected_row_dict @require_not_windows @require_dill_gt_0_3_2 def __A ( )-> Dict: '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[Any] = pyspark.sql.SparkSession.builder.master('''local[*]''' ).appName('''pyspark''' ).getOrCreate() SCREAMING_SNAKE_CASE : Optional[int] = spark.range(10 ).repartition(1 ) SCREAMING_SNAKE_CASE : List[Any] = SparkExamplesIterable(_lowercase ) assert it.n_shards == 1 for i, (row_id, row_dict) in enumerate(_lowercase ): assert row_id == F"0_{i}" assert row_dict == {"id": i} @require_not_windows @require_dill_gt_0_3_2 def __A ( )-> int: '''simple docstring''' SCREAMING_SNAKE_CASE : Tuple = pyspark.sql.SparkSession.builder.master('''local[*]''' ).appName('''pyspark''' ).getOrCreate() SCREAMING_SNAKE_CASE : Dict = spark.range(30 ).repartition(3 ) # Mock the generator so that shuffle reverses the partition indices. with patch('''numpy.random.Generator''' ) as generator_mock: SCREAMING_SNAKE_CASE : Tuple = lambda a_ : x.reverse() SCREAMING_SNAKE_CASE : Tuple = _get_expected_row_ids_and_row_dicts_for_partition_order(_lowercase , [2, 1, 0] ) SCREAMING_SNAKE_CASE : str = SparkExamplesIterable(_lowercase ).shuffle_data_sources(_lowercase ) assert shuffled_it.n_shards == 3 for i, (row_id, row_dict) in enumerate(_lowercase ): SCREAMING_SNAKE_CASE : List[Any] = expected_row_ids_and_row_dicts[i] assert row_id == expected_row_id assert row_dict == expected_row_dict @require_not_windows @require_dill_gt_0_3_2 def __A ( )-> List[str]: '''simple docstring''' SCREAMING_SNAKE_CASE : Union[str, Any] = pyspark.sql.SparkSession.builder.master('''local[*]''' ).appName('''pyspark''' ).getOrCreate() SCREAMING_SNAKE_CASE : Union[str, Any] = spark.range(20 ).repartition(4 ) # Partitions 0 and 2 SCREAMING_SNAKE_CASE : int = SparkExamplesIterable(_lowercase ).shard_data_sources(worker_id=0 , num_workers=2 ) assert shard_it_a.n_shards == 2 SCREAMING_SNAKE_CASE : str = _get_expected_row_ids_and_row_dicts_for_partition_order(_lowercase , [0, 2] ) for i, (row_id, row_dict) in enumerate(_lowercase ): SCREAMING_SNAKE_CASE : str = expected_row_ids_and_row_dicts_a[i] assert row_id == expected_row_id assert row_dict == expected_row_dict # Partitions 1 and 3 SCREAMING_SNAKE_CASE : Any = SparkExamplesIterable(_lowercase ).shard_data_sources(worker_id=1 , num_workers=2 ) assert shard_it_a.n_shards == 2 SCREAMING_SNAKE_CASE : Dict = _get_expected_row_ids_and_row_dicts_for_partition_order(_lowercase , [1, 3] ) for i, (row_id, row_dict) in enumerate(_lowercase ): SCREAMING_SNAKE_CASE : Any = expected_row_ids_and_row_dicts_a[i] assert row_id == expected_row_id assert row_dict == expected_row_dict @require_not_windows @require_dill_gt_0_3_2 def __A ( )-> List[str]: '''simple docstring''' SCREAMING_SNAKE_CASE : Union[str, Any] = pyspark.sql.SparkSession.builder.master('''local[*]''' ).appName('''pyspark''' ).getOrCreate() SCREAMING_SNAKE_CASE : List[Any] = spark.range(1_00 ).repartition(1 ) SCREAMING_SNAKE_CASE : Dict = Spark(_lowercase ) # Choose a small max_shard_size for maximum partitioning. spark_builder._repartition_df_if_needed(max_shard_size=1 ) # The new number of partitions should not be greater than the number of rows. assert spark_builder.df.rdd.getNumPartitions() == 1_00
711
"""simple docstring""" import collections import inspect import unittest from transformers import SwinvaConfig from transformers.testing_utils import require_torch, require_vision, slow, torch_device from transformers.utils import cached_property, is_torch_available, is_vision_available from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, _config_zero_init, floats_tensor, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from torch import nn from transformers import SwinvaForImageClassification, SwinvaForMaskedImageModeling, SwinvaModel from transformers.models.swinva.modeling_swinva import SWINV2_PRETRAINED_MODEL_ARCHIVE_LIST if is_vision_available(): from PIL import Image from transformers import AutoImageProcessor class lowercase__: '''simple docstring''' def __init__( self :str , lowerCamelCase_ :List[Any] , lowerCamelCase_ :Any=13 , lowerCamelCase_ :Any=32 , lowerCamelCase_ :Union[str, Any]=2 , lowerCamelCase_ :Any=3 , lowerCamelCase_ :Union[str, Any]=16 , lowerCamelCase_ :int=[1, 2, 1] , lowerCamelCase_ :str=[2, 2, 4] , lowerCamelCase_ :str=2 , lowerCamelCase_ :Tuple=2.0 , lowerCamelCase_ :Union[str, Any]=True , lowerCamelCase_ :str=0.0 , lowerCamelCase_ :Optional[int]=0.0 , lowerCamelCase_ :Dict=0.1 , lowerCamelCase_ :Union[str, Any]="gelu" , lowerCamelCase_ :str=False , lowerCamelCase_ :List[Any]=True , lowerCamelCase_ :int=0.0_2 , lowerCamelCase_ :List[Any]=1E-5 , lowerCamelCase_ :int=True , lowerCamelCase_ :str=None , lowerCamelCase_ :List[Any]=True , lowerCamelCase_ :Union[str, Any]=10 , lowerCamelCase_ :List[Any]=8 , ) -> int: '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[Any] = parent SCREAMING_SNAKE_CASE : int = batch_size SCREAMING_SNAKE_CASE : int = image_size SCREAMING_SNAKE_CASE : Union[str, Any] = patch_size SCREAMING_SNAKE_CASE : Tuple = num_channels SCREAMING_SNAKE_CASE : Any = embed_dim SCREAMING_SNAKE_CASE : int = depths SCREAMING_SNAKE_CASE : List[str] = num_heads SCREAMING_SNAKE_CASE : Union[str, Any] = window_size SCREAMING_SNAKE_CASE : Optional[Any] = mlp_ratio SCREAMING_SNAKE_CASE : List[Any] = qkv_bias SCREAMING_SNAKE_CASE : Union[str, Any] = hidden_dropout_prob SCREAMING_SNAKE_CASE : Dict = attention_probs_dropout_prob SCREAMING_SNAKE_CASE : List[str] = drop_path_rate SCREAMING_SNAKE_CASE : List[Any] = hidden_act SCREAMING_SNAKE_CASE : Optional[Any] = use_absolute_embeddings SCREAMING_SNAKE_CASE : Any = patch_norm SCREAMING_SNAKE_CASE : Union[str, Any] = layer_norm_eps SCREAMING_SNAKE_CASE : List[Any] = initializer_range SCREAMING_SNAKE_CASE : Any = is_training SCREAMING_SNAKE_CASE : List[Any] = scope SCREAMING_SNAKE_CASE : Optional[Any] = use_labels SCREAMING_SNAKE_CASE : Optional[Any] = type_sequence_label_size SCREAMING_SNAKE_CASE : Optional[Any] = encoder_stride def __lowerCAmelCase ( self :Optional[int] ) -> List[str]: '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[int] = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) SCREAMING_SNAKE_CASE : int = None if self.use_labels: SCREAMING_SNAKE_CASE : Dict = ids_tensor([self.batch_size] , self.type_sequence_label_size ) SCREAMING_SNAKE_CASE : Tuple = self.get_config() return config, pixel_values, labels def __lowerCAmelCase ( self :int ) -> int: '''simple docstring''' return SwinvaConfig( image_size=self.image_size , patch_size=self.patch_size , num_channels=self.num_channels , embed_dim=self.embed_dim , depths=self.depths , num_heads=self.num_heads , window_size=self.window_size , mlp_ratio=self.mlp_ratio , qkv_bias=self.qkv_bias , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , drop_path_rate=self.drop_path_rate , hidden_act=self.hidden_act , use_absolute_embeddings=self.use_absolute_embeddings , path_norm=self.patch_norm , layer_norm_eps=self.layer_norm_eps , initializer_range=self.initializer_range , encoder_stride=self.encoder_stride , ) def __lowerCAmelCase ( self :List[Any] , lowerCamelCase_ :int , lowerCamelCase_ :Optional[int] , lowerCamelCase_ :Optional[Any] ) -> Union[str, Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : str = SwinvaModel(config=lowerCamelCase_ ) model.to(lowerCamelCase_ ) model.eval() SCREAMING_SNAKE_CASE : int = model(lowerCamelCase_ ) SCREAMING_SNAKE_CASE : List[Any] = ((config.image_size // config.patch_size) ** 2) // (4 ** (len(config.depths ) - 1)) SCREAMING_SNAKE_CASE : Dict = int(config.embed_dim * 2 ** (len(config.depths ) - 1) ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, expected_seq_len, expected_dim) ) def __lowerCAmelCase ( self :str , lowerCamelCase_ :str , lowerCamelCase_ :Tuple , lowerCamelCase_ :Any ) -> Union[str, Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : List[str] = SwinvaForMaskedImageModeling(config=lowerCamelCase_ ) model.to(lowerCamelCase_ ) model.eval() SCREAMING_SNAKE_CASE : Dict = model(lowerCamelCase_ ) self.parent.assertEqual( result.logits.shape , (self.batch_size, self.num_channels, self.image_size, self.image_size) ) # test greyscale images SCREAMING_SNAKE_CASE : Tuple = 1 SCREAMING_SNAKE_CASE : List[Any] = SwinvaForMaskedImageModeling(lowerCamelCase_ ) model.to(lowerCamelCase_ ) model.eval() SCREAMING_SNAKE_CASE : List[str] = floats_tensor([self.batch_size, 1, self.image_size, self.image_size] ) SCREAMING_SNAKE_CASE : Tuple = model(lowerCamelCase_ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, 1, self.image_size, self.image_size) ) def __lowerCAmelCase ( self :Tuple , lowerCamelCase_ :Union[str, Any] , lowerCamelCase_ :Any , lowerCamelCase_ :int ) -> Tuple: '''simple docstring''' SCREAMING_SNAKE_CASE : Tuple = self.type_sequence_label_size SCREAMING_SNAKE_CASE : Optional[Any] = SwinvaForImageClassification(lowerCamelCase_ ) model.to(lowerCamelCase_ ) model.eval() SCREAMING_SNAKE_CASE : Optional[int] = model(lowerCamelCase_ , labels=lowerCamelCase_ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.type_sequence_label_size) ) def __lowerCAmelCase ( self :List[Any] ) -> List[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : List[str] = self.prepare_config_and_inputs() SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : Optional[int] = config_and_inputs SCREAMING_SNAKE_CASE : int = {'''pixel_values''': pixel_values} return config, inputs_dict @require_torch class lowercase__( _UpperCAmelCase , _UpperCAmelCase , unittest.TestCase ): '''simple docstring''' UpperCamelCase = ( (SwinvaModel, SwinvaForImageClassification, SwinvaForMaskedImageModeling) if is_torch_available() else () ) UpperCamelCase = ( {"""feature-extraction""": SwinvaModel, """image-classification""": SwinvaForImageClassification} if is_torch_available() else {} ) UpperCamelCase = False UpperCamelCase = False UpperCamelCase = False UpperCamelCase = False def __lowerCAmelCase ( self :Union[str, Any] ) -> Union[str, Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : int = SwinvaModelTester(self ) SCREAMING_SNAKE_CASE : Dict = ConfigTester(self , config_class=lowerCamelCase_ , embed_dim=37 ) def __lowerCAmelCase ( self :Dict ) -> List[str]: '''simple docstring''' self.config_tester.create_and_test_config_to_json_string() self.config_tester.create_and_test_config_to_json_file() self.config_tester.create_and_test_config_from_and_save_pretrained() self.config_tester.create_and_test_config_with_num_labels() self.config_tester.check_config_can_be_init_without_params() self.config_tester.check_config_arguments_init() def __lowerCAmelCase ( self :List[Any] ) -> List[str]: '''simple docstring''' SCREAMING_SNAKE_CASE : Union[str, Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*lowerCamelCase_ ) @unittest.skip(reason='''Got `CUDA error: misaligned address` with PyTorch 2.0.0.''' ) def __lowerCAmelCase ( self :str ) -> List[Any]: '''simple docstring''' pass @unittest.skip(reason='''Swinv2 does not use inputs_embeds''' ) def __lowerCAmelCase ( self :List[Any] ) -> Any: '''simple docstring''' pass def __lowerCAmelCase ( self :int ) -> Union[str, Any]: '''simple docstring''' SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : Optional[int] = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: SCREAMING_SNAKE_CASE : List[Any] = model_class(lowerCamelCase_ ) self.assertIsInstance(model.get_input_embeddings() , (nn.Module) ) SCREAMING_SNAKE_CASE : str = model.get_output_embeddings() self.assertTrue(x is None or isinstance(lowerCamelCase_ , nn.Linear ) ) def __lowerCAmelCase ( self :int ) -> List[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : Tuple = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: SCREAMING_SNAKE_CASE : Optional[Any] = model_class(lowerCamelCase_ ) SCREAMING_SNAKE_CASE : List[str] = inspect.signature(model.forward ) # signature.parameters is an OrderedDict => so arg_names order is deterministic SCREAMING_SNAKE_CASE : Optional[int] = [*signature.parameters.keys()] SCREAMING_SNAKE_CASE : Union[str, Any] = ['''pixel_values'''] self.assertListEqual(arg_names[:1] , lowerCamelCase_ ) def __lowerCAmelCase ( self :int ) -> Optional[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : str = self.model_tester.prepare_config_and_inputs_for_common() SCREAMING_SNAKE_CASE : Tuple = True for model_class in self.all_model_classes: SCREAMING_SNAKE_CASE : List[str] = True SCREAMING_SNAKE_CASE : List[Any] = False SCREAMING_SNAKE_CASE : Any = True SCREAMING_SNAKE_CASE : Any = model_class(lowerCamelCase_ ) model.to(lowerCamelCase_ ) model.eval() with torch.no_grad(): SCREAMING_SNAKE_CASE : str = model(**self._prepare_for_class(lowerCamelCase_ , lowerCamelCase_ ) ) SCREAMING_SNAKE_CASE : Tuple = outputs.attentions SCREAMING_SNAKE_CASE : Tuple = len(self.model_tester.depths ) self.assertEqual(len(lowerCamelCase_ ) , lowerCamelCase_ ) # check that output_attentions also work using config del inputs_dict["output_attentions"] SCREAMING_SNAKE_CASE : Union[str, Any] = True SCREAMING_SNAKE_CASE : Optional[int] = config.window_size**2 SCREAMING_SNAKE_CASE : str = model_class(lowerCamelCase_ ) model.to(lowerCamelCase_ ) model.eval() with torch.no_grad(): SCREAMING_SNAKE_CASE : Union[str, Any] = model(**self._prepare_for_class(lowerCamelCase_ , lowerCamelCase_ ) ) SCREAMING_SNAKE_CASE : Dict = outputs.attentions self.assertEqual(len(lowerCamelCase_ ) , lowerCamelCase_ ) self.assertListEqual( list(attentions[0].shape[-3:] ) , [self.model_tester.num_heads[0], window_size_squared, window_size_squared] , ) SCREAMING_SNAKE_CASE : Dict = len(lowerCamelCase_ ) # Check attention is always last and order is fine SCREAMING_SNAKE_CASE : int = True SCREAMING_SNAKE_CASE : Dict = True SCREAMING_SNAKE_CASE : int = model_class(lowerCamelCase_ ) model.to(lowerCamelCase_ ) model.eval() with torch.no_grad(): SCREAMING_SNAKE_CASE : List[Any] = model(**self._prepare_for_class(lowerCamelCase_ , lowerCamelCase_ ) ) if hasattr(self.model_tester , '''num_hidden_states_types''' ): SCREAMING_SNAKE_CASE : Any = self.model_tester.num_hidden_states_types else: # also another +1 for reshaped_hidden_states SCREAMING_SNAKE_CASE : Optional[Any] = 2 self.assertEqual(out_len + added_hidden_states , len(lowerCamelCase_ ) ) SCREAMING_SNAKE_CASE : Tuple = outputs.attentions self.assertEqual(len(lowerCamelCase_ ) , lowerCamelCase_ ) self.assertListEqual( list(self_attentions[0].shape[-3:] ) , [self.model_tester.num_heads[0], window_size_squared, window_size_squared] , ) def __lowerCAmelCase ( self :Dict , lowerCamelCase_ :int , lowerCamelCase_ :Any , lowerCamelCase_ :Tuple , lowerCamelCase_ :Any ) -> Optional[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : Union[str, Any] = model_class(lowerCamelCase_ ) model.to(lowerCamelCase_ ) model.eval() with torch.no_grad(): SCREAMING_SNAKE_CASE : Optional[int] = model(**self._prepare_for_class(lowerCamelCase_ , lowerCamelCase_ ) ) SCREAMING_SNAKE_CASE : Dict = outputs.hidden_states SCREAMING_SNAKE_CASE : str = getattr( self.model_tester , '''expected_num_hidden_layers''' , len(self.model_tester.depths ) + 1 ) self.assertEqual(len(lowerCamelCase_ ) , lowerCamelCase_ ) # Swinv2 has a different seq_length SCREAMING_SNAKE_CASE : List[str] = ( config.patch_size if isinstance(config.patch_size , collections.abc.Iterable ) else (config.patch_size, config.patch_size) ) SCREAMING_SNAKE_CASE : Dict = (image_size[1] // patch_size[1]) * (image_size[0] // patch_size[0]) self.assertListEqual( list(hidden_states[0].shape[-2:] ) , [num_patches, self.model_tester.embed_dim] , ) SCREAMING_SNAKE_CASE : Any = outputs.reshaped_hidden_states self.assertEqual(len(lowerCamelCase_ ) , lowerCamelCase_ ) SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : Any = reshaped_hidden_states[0].shape SCREAMING_SNAKE_CASE : Optional[int] = ( reshaped_hidden_states[0].view(lowerCamelCase_ , lowerCamelCase_ , height * width ).permute(0 , 2 , 1 ) ) self.assertListEqual( list(reshaped_hidden_states.shape[-2:] ) , [num_patches, self.model_tester.embed_dim] , ) def __lowerCAmelCase ( self :Optional[Any] ) -> Optional[int]: '''simple docstring''' SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : Union[str, Any] = self.model_tester.prepare_config_and_inputs_for_common() SCREAMING_SNAKE_CASE : List[str] = ( self.model_tester.image_size if isinstance(self.model_tester.image_size , collections.abc.Iterable ) else (self.model_tester.image_size, self.model_tester.image_size) ) for model_class in self.all_model_classes: SCREAMING_SNAKE_CASE : Tuple = True self.check_hidden_states_output(lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ ) # check that output_hidden_states also work using config del inputs_dict["output_hidden_states"] SCREAMING_SNAKE_CASE : List[str] = True self.check_hidden_states_output(lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ ) def __lowerCAmelCase ( self :str ) -> Optional[int]: '''simple docstring''' SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : str = self.model_tester.prepare_config_and_inputs_for_common() SCREAMING_SNAKE_CASE : List[Any] = 3 SCREAMING_SNAKE_CASE : int = ( self.model_tester.image_size if isinstance(self.model_tester.image_size , collections.abc.Iterable ) else (self.model_tester.image_size, self.model_tester.image_size) ) SCREAMING_SNAKE_CASE : Optional[Any] = ( config.patch_size if isinstance(config.patch_size , collections.abc.Iterable ) else (config.patch_size, config.patch_size) ) SCREAMING_SNAKE_CASE : Optional[int] = image_size[0] + patch_size[0] - (image_size[0] % patch_size[0]) SCREAMING_SNAKE_CASE : Optional[int] = image_size[1] + patch_size[1] - (image_size[1] % patch_size[1]) for model_class in self.all_model_classes: SCREAMING_SNAKE_CASE : Optional[Any] = True self.check_hidden_states_output(lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , (padded_height, padded_width) ) # check that output_hidden_states also work using config del inputs_dict["output_hidden_states"] SCREAMING_SNAKE_CASE : Optional[Any] = True self.check_hidden_states_output(lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , (padded_height, padded_width) ) def __lowerCAmelCase ( self :str ) -> List[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : Tuple = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_masked_image_modeling(*lowerCamelCase_ ) def __lowerCAmelCase ( self :Tuple ) -> Optional[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : Tuple = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_image_classification(*lowerCamelCase_ ) @slow def __lowerCAmelCase ( self :Tuple ) -> List[str]: '''simple docstring''' for model_name in SWINV2_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: SCREAMING_SNAKE_CASE : Tuple = SwinvaModel.from_pretrained(lowerCamelCase_ ) self.assertIsNotNone(lowerCamelCase_ ) def __lowerCAmelCase ( self :int ) -> Any: '''simple docstring''' SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : List[str] = self.model_tester.prepare_config_and_inputs_for_common() SCREAMING_SNAKE_CASE : Tuple = _config_zero_init(lowerCamelCase_ ) for model_class in self.all_model_classes: SCREAMING_SNAKE_CASE : Dict = model_class(config=lowerCamelCase_ ) for name, param in model.named_parameters(): if "embeddings" not in name and "logit_scale" not in name and param.requires_grad: self.assertIn( ((param.data.mean() * 1E9).round() / 1E9).item() , [0.0, 1.0] , msg=f"Parameter {name} of model {model_class} seems not properly initialized" , ) @require_vision @require_torch class lowercase__( unittest.TestCase ): '''simple docstring''' @cached_property def __lowerCAmelCase ( self :Dict ) -> List[Any]: '''simple docstring''' return ( AutoImageProcessor.from_pretrained('''microsoft/swinv2-tiny-patch4-window8-256''' ) if is_vision_available() else None ) @slow def __lowerCAmelCase ( self :Dict ) -> Tuple: '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[int] = SwinvaForImageClassification.from_pretrained('''microsoft/swinv2-tiny-patch4-window8-256''' ).to( lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Dict = self.default_image_processor SCREAMING_SNAKE_CASE : Optional[Any] = Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png''' ) SCREAMING_SNAKE_CASE : List[str] = image_processor(images=lowerCamelCase_ , return_tensors='''pt''' ).to(lowerCamelCase_ ) # forward pass with torch.no_grad(): SCREAMING_SNAKE_CASE : Tuple = model(**lowerCamelCase_ ) # verify the logits SCREAMING_SNAKE_CASE : List[str] = torch.Size((1, 10_00) ) self.assertEqual(outputs.logits.shape , lowerCamelCase_ ) SCREAMING_SNAKE_CASE : int = torch.tensor([-0.3_9_4_7, -0.4_3_0_6, 0.0_0_2_6] ).to(lowerCamelCase_ ) self.assertTrue(torch.allclose(outputs.logits[0, :3] , lowerCamelCase_ , atol=1E-4 ) )
18
0
"""simple docstring""" import argparse import os import evaluate import torch from datasets import load_dataset from torch.optim import AdamW from torch.utils.data import DataLoader from transformers import AutoModelForSequenceClassification, AutoTokenizer, get_linear_schedule_with_warmup, set_seed from accelerate import Accelerator, DistributedType from accelerate.local_sgd import LocalSGD ######################################################################## # This is a fully working simple example to use Accelerate # with LocalSGD, which is a method to synchronize model # parameters every K batches. It is different, but complementary # to gradient accumulation. # # This example trains a Bert base model on GLUE MRPC # in any of the following settings (with the same script): # - single CPU or single GPU # - multi GPUS (using PyTorch distributed mode) # - (multi) TPUs # - fp16 (mixed-precision) or fp32 (normal precision) # # To run it in each of these various modes, follow the instructions # in the readme for examples: # https://github.com/huggingface/accelerate/tree/main/examples # ######################################################################## lowerCamelCase__ : List[str] = 16 lowerCamelCase__ : Union[str, Any] = 32 def __A ( a_ : Any , a_ : str = 16 )-> Dict: '''simple docstring''' SCREAMING_SNAKE_CASE : List[str] = AutoTokenizer.from_pretrained('''bert-base-cased''' ) SCREAMING_SNAKE_CASE : Tuple = load_dataset('''glue''' , '''mrpc''' ) def tokenize_function(a_ : str ): # max_length=None => use the model max length (it's actually the default) SCREAMING_SNAKE_CASE : Tuple = tokenizer(examples['''sentence1'''] , examples['''sentence2'''] , truncation=__UpperCamelCase , max_length=__UpperCamelCase ) return outputs # Apply the method we just defined to all the examples in all the splits of the dataset # starting with the main process first: with accelerator.main_process_first(): SCREAMING_SNAKE_CASE : str = datasets.map( __UpperCamelCase , batched=__UpperCamelCase , remove_columns=['''idx''', '''sentence1''', '''sentence2'''] , ) # We also rename the 'label' column to 'labels' which is the expected name for labels by the models of the # transformers library SCREAMING_SNAKE_CASE : Dict = tokenized_datasets.rename_column('''label''' , '''labels''' ) def collate_fn(a_ : List[str] ): # On TPU it's best to pad everything to the same length or training will be very slow. SCREAMING_SNAKE_CASE : List[Any] = 1_28 if accelerator.distributed_type == DistributedType.TPU else None # When using mixed precision we want round multiples of 8/16 if accelerator.mixed_precision == "fp8": SCREAMING_SNAKE_CASE : str = 16 elif accelerator.mixed_precision != "no": SCREAMING_SNAKE_CASE : Any = 8 else: SCREAMING_SNAKE_CASE : int = None return tokenizer.pad( __UpperCamelCase , padding='''longest''' , max_length=__UpperCamelCase , pad_to_multiple_of=__UpperCamelCase , return_tensors='''pt''' , ) # Instantiate dataloaders. SCREAMING_SNAKE_CASE : Optional[Any] = DataLoader( tokenized_datasets['''train'''] , shuffle=__UpperCamelCase , collate_fn=__UpperCamelCase , batch_size=__UpperCamelCase ) SCREAMING_SNAKE_CASE : Optional[Any] = DataLoader( tokenized_datasets['''validation'''] , shuffle=__UpperCamelCase , collate_fn=__UpperCamelCase , batch_size=__UpperCamelCase ) return train_dataloader, eval_dataloader # For testing only if os.environ.get("TESTING_MOCKED_DATALOADERS", None) == "1": from accelerate.test_utils.training import mocked_dataloaders lowerCamelCase__ : int = mocked_dataloaders # noqa: F811 def __A ( a_ : Tuple , a_ : Tuple )-> List[Any]: '''simple docstring''' if os.environ.get('''TESTING_MOCKED_DATALOADERS''' , __UpperCamelCase ) == "1": SCREAMING_SNAKE_CASE : str = 2 # New Code # SCREAMING_SNAKE_CASE : Union[str, Any] = int(args.gradient_accumulation_steps ) SCREAMING_SNAKE_CASE : int = int(args.local_sgd_steps ) # Initialize accelerator SCREAMING_SNAKE_CASE : str = Accelerator( cpu=args.cpu , mixed_precision=args.mixed_precision , gradient_accumulation_steps=__UpperCamelCase ) if accelerator.distributed_type not in [DistributedType.NO, DistributedType.MULTI_CPU, DistributedType.MULTI_GPU]: raise NotImplementedError('''LocalSGD is supported only for CPUs and GPUs (no DeepSpeed or MegatronLM)''' ) # Sample hyper-parameters for learning rate, batch size, seed and a few other HPs SCREAMING_SNAKE_CASE : Any = config['''lr'''] SCREAMING_SNAKE_CASE : Optional[int] = int(config['''num_epochs'''] ) SCREAMING_SNAKE_CASE : Tuple = int(config['''seed'''] ) SCREAMING_SNAKE_CASE : Any = int(config['''batch_size'''] ) SCREAMING_SNAKE_CASE : Optional[Any] = evaluate.load('''glue''' , '''mrpc''' ) set_seed(__UpperCamelCase ) SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : Dict = get_dataloaders(__UpperCamelCase , __UpperCamelCase ) # Instantiate the model (we build the model here so that the seed also control new weights initialization) SCREAMING_SNAKE_CASE : Optional[Any] = AutoModelForSequenceClassification.from_pretrained('''bert-base-cased''' , return_dict=__UpperCamelCase ) # We could avoid this line since the accelerator is set with `device_placement=True` (default value). # Note that if you are placing tensors on devices manually, this line absolutely needs to be before the optimizer # creation otherwise training will not work on TPU (`accelerate` will kindly throw an error to make us aware of that). SCREAMING_SNAKE_CASE : str = model.to(accelerator.device ) # Instantiate optimizer SCREAMING_SNAKE_CASE : Dict = AdamW(params=model.parameters() , lr=__UpperCamelCase ) # Instantiate scheduler SCREAMING_SNAKE_CASE : List[Any] = get_linear_schedule_with_warmup( optimizer=__UpperCamelCase , num_warmup_steps=1_00 , num_training_steps=(len(__UpperCamelCase ) * num_epochs) , ) # Prepare everything # There is no specific order to remember, we just need to unpack the objects in the same order we gave them to the # prepare method. SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : Union[str, Any] = accelerator.prepare( __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase , __UpperCamelCase ) # Now we train the model for epoch in range(__UpperCamelCase ): model.train() with LocalSGD( accelerator=__UpperCamelCase , model=__UpperCamelCase , local_sgd_steps=__UpperCamelCase , enabled=local_sgd_steps is not None ) as local_sgd: for step, batch in enumerate(__UpperCamelCase ): # We could avoid this line since we set the accelerator with `device_placement=True`. batch.to(accelerator.device ) # New code # # We use the new `accumulate` context manager to perform gradient accumulation # We also currently do not support TPUs nor advise it as bugs were found on the XLA side when running our tests. with accelerator.accumulate(__UpperCamelCase ): SCREAMING_SNAKE_CASE : Union[str, Any] = model(**__UpperCamelCase ) SCREAMING_SNAKE_CASE : Any = output.loss accelerator.backward(__UpperCamelCase ) optimizer.step() lr_scheduler.step() optimizer.zero_grad() # LocalSGD-specific line local_sgd.step() model.eval() for step, batch in enumerate(__UpperCamelCase ): # We could avoid this line since we set the accelerator with `device_placement=True`. batch.to(accelerator.device ) with torch.no_grad(): SCREAMING_SNAKE_CASE : str = model(**__UpperCamelCase ) SCREAMING_SNAKE_CASE : str = outputs.logits.argmax(dim=-1 ) SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : List[str] = accelerator.gather_for_metrics((predictions, batch['''labels''']) ) metric.add_batch( predictions=__UpperCamelCase , references=__UpperCamelCase , ) SCREAMING_SNAKE_CASE : List[Any] = metric.compute() # Use accelerator.print to print only on the main process. accelerator.print(F"epoch {epoch}:" , __UpperCamelCase ) def __A ( )-> List[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[int] = argparse.ArgumentParser(description='''Simple example of training script.''' ) parser.add_argument( '''--mixed_precision''' , type=__UpperCamelCase , default=__UpperCamelCase , choices=['''no''', '''fp16''', '''bf16''', '''fp8'''] , help='''Whether to use mixed precision. Choose''' '''between fp16 and bf16 (bfloat16). Bf16 requires PyTorch >= 1.10.''' '''and an Nvidia Ampere GPU.''' , ) # New Code # parser.add_argument( '''--gradient_accumulation_steps''' , type=__UpperCamelCase , default=1 , help='''The number of minibatches to be ran before gradients are accumulated.''' , ) parser.add_argument( '''--local_sgd_steps''' , type=__UpperCamelCase , default=8 , help='''Number of local SGD steps or None to disable local SGD''' ) parser.add_argument('''--cpu''' , action='''store_true''' , help='''If passed, will train on the CPU.''' ) SCREAMING_SNAKE_CASE : Dict = parser.parse_args() SCREAMING_SNAKE_CASE : List[str] = {'''lr''': 2E-5, '''num_epochs''': 3, '''seed''': 42, '''batch_size''': 16} training_function(__UpperCamelCase , __UpperCamelCase ) if __name__ == "__main__": main()
712
"""simple docstring""" from collections import OrderedDict from typing import Any, Mapping, Optional from ... import PreTrainedTokenizer from ...configuration_utils import PretrainedConfig from ...file_utils import TensorType, is_torch_available from ...onnx import OnnxConfig, OnnxConfigWithPast, OnnxSeqaSeqConfigWithPast from ...onnx.utils import compute_effective_axis_dimension from ...utils import logging lowerCamelCase__ : Tuple = logging.get_logger(__name__) lowerCamelCase__ : str = { "facebook/blenderbot_small-90M": "https://huggingface.co/facebook/blenderbot_small-90M/resolve/main/config.json", # See all BlenderbotSmall models at https://huggingface.co/models?filter=blenderbot_small } class lowercase__( _UpperCAmelCase ): '''simple docstring''' UpperCamelCase = """blenderbot-small""" UpperCamelCase = ["""past_key_values"""] UpperCamelCase = {"""num_attention_heads""": """encoder_attention_heads""", """hidden_size""": """d_model"""} def __init__( self :Any , lowerCamelCase_ :Dict=5_02_65 , lowerCamelCase_ :str=5_12 , lowerCamelCase_ :Tuple=8 , lowerCamelCase_ :int=20_48 , lowerCamelCase_ :str=16 , lowerCamelCase_ :Optional[int]=8 , lowerCamelCase_ :str=20_48 , lowerCamelCase_ :Optional[Any]=16 , lowerCamelCase_ :Union[str, Any]=0.0 , lowerCamelCase_ :List[str]=0.0 , lowerCamelCase_ :Tuple=True , lowerCamelCase_ :Tuple=True , lowerCamelCase_ :int="gelu" , lowerCamelCase_ :Tuple=5_12 , lowerCamelCase_ :Optional[int]=0.1 , lowerCamelCase_ :int=0.0 , lowerCamelCase_ :Tuple=0.0 , lowerCamelCase_ :Optional[int]=0.0_2 , lowerCamelCase_ :Union[str, Any]=1 , lowerCamelCase_ :Dict=False , lowerCamelCase_ :Optional[int]=0 , lowerCamelCase_ :List[Any]=1 , lowerCamelCase_ :Any=2 , lowerCamelCase_ :Optional[Any]=2 , **lowerCamelCase_ :Dict , ) -> Any: '''simple docstring''' SCREAMING_SNAKE_CASE : int = vocab_size SCREAMING_SNAKE_CASE : List[str] = max_position_embeddings SCREAMING_SNAKE_CASE : Optional[Any] = d_model SCREAMING_SNAKE_CASE : Dict = encoder_ffn_dim SCREAMING_SNAKE_CASE : Tuple = encoder_layers SCREAMING_SNAKE_CASE : Dict = encoder_attention_heads SCREAMING_SNAKE_CASE : Any = decoder_ffn_dim SCREAMING_SNAKE_CASE : str = decoder_layers SCREAMING_SNAKE_CASE : str = decoder_attention_heads SCREAMING_SNAKE_CASE : List[Any] = dropout SCREAMING_SNAKE_CASE : Optional[Any] = attention_dropout SCREAMING_SNAKE_CASE : Any = activation_dropout SCREAMING_SNAKE_CASE : List[str] = activation_function SCREAMING_SNAKE_CASE : Optional[int] = init_std SCREAMING_SNAKE_CASE : List[Any] = encoder_layerdrop SCREAMING_SNAKE_CASE : Union[str, Any] = decoder_layerdrop SCREAMING_SNAKE_CASE : List[Any] = use_cache SCREAMING_SNAKE_CASE : Union[str, Any] = encoder_layers SCREAMING_SNAKE_CASE : List[Any] = scale_embedding # scale factor will be sqrt(d_model) if True super().__init__( pad_token_id=lowerCamelCase_ , bos_token_id=lowerCamelCase_ , eos_token_id=lowerCamelCase_ , is_encoder_decoder=lowerCamelCase_ , decoder_start_token_id=lowerCamelCase_ , forced_eos_token_id=lowerCamelCase_ , **lowerCamelCase_ , ) class lowercase__( _UpperCAmelCase ): '''simple docstring''' @property def __lowerCAmelCase ( self :Any ) -> Mapping[str, Mapping[int, str]]: '''simple docstring''' if self.task in ["default", "seq2seq-lm"]: SCREAMING_SNAKE_CASE : Tuple = OrderedDict( [ ('''input_ids''', {0: '''batch''', 1: '''encoder_sequence'''}), ('''attention_mask''', {0: '''batch''', 1: '''encoder_sequence'''}), ] ) if self.use_past: SCREAMING_SNAKE_CASE : Union[str, Any] = {0: '''batch'''} SCREAMING_SNAKE_CASE : List[Any] = {0: '''batch''', 1: '''past_decoder_sequence + sequence'''} else: SCREAMING_SNAKE_CASE : Optional[Any] = {0: '''batch''', 1: '''decoder_sequence'''} SCREAMING_SNAKE_CASE : Union[str, Any] = {0: '''batch''', 1: '''decoder_sequence'''} if self.use_past: self.fill_with_past_key_values_(lowerCamelCase_ , direction='''inputs''' ) elif self.task == "causal-lm": # TODO: figure this case out. SCREAMING_SNAKE_CASE : Tuple = OrderedDict( [ ('''input_ids''', {0: '''batch''', 1: '''encoder_sequence'''}), ('''attention_mask''', {0: '''batch''', 1: '''encoder_sequence'''}), ] ) if self.use_past: SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : List[Any] = self.num_layers for i in range(lowerCamelCase_ ): SCREAMING_SNAKE_CASE : int = {0: '''batch''', 2: '''past_sequence + sequence'''} SCREAMING_SNAKE_CASE : List[str] = {0: '''batch''', 2: '''past_sequence + sequence'''} else: SCREAMING_SNAKE_CASE : Any = OrderedDict( [ ('''input_ids''', {0: '''batch''', 1: '''encoder_sequence'''}), ('''attention_mask''', {0: '''batch''', 1: '''encoder_sequence'''}), ('''decoder_input_ids''', {0: '''batch''', 1: '''decoder_sequence'''}), ('''decoder_attention_mask''', {0: '''batch''', 1: '''decoder_sequence'''}), ] ) return common_inputs @property def __lowerCAmelCase ( self :List[str] ) -> Mapping[str, Mapping[int, str]]: '''simple docstring''' if self.task in ["default", "seq2seq-lm"]: SCREAMING_SNAKE_CASE : Any = super().outputs else: SCREAMING_SNAKE_CASE : Tuple = super(lowerCamelCase_ , self ).outputs if self.use_past: SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : Union[str, Any] = self.num_layers for i in range(lowerCamelCase_ ): SCREAMING_SNAKE_CASE : Union[str, Any] = {0: '''batch''', 2: '''past_sequence + sequence'''} SCREAMING_SNAKE_CASE : str = {0: '''batch''', 2: '''past_sequence + sequence'''} return common_outputs def __lowerCAmelCase ( self :int , lowerCamelCase_ :PreTrainedTokenizer , lowerCamelCase_ :int = -1 , lowerCamelCase_ :int = -1 , lowerCamelCase_ :bool = False , lowerCamelCase_ :Optional[TensorType] = None , ) -> Mapping[str, Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : Dict = self._generate_dummy_inputs_for_sequence_classification_and_question_answering( lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ ) # Generate decoder inputs SCREAMING_SNAKE_CASE : Optional[int] = seq_length if not self.use_past else 1 SCREAMING_SNAKE_CASE : Optional[int] = self._generate_dummy_inputs_for_sequence_classification_and_question_answering( lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ ) SCREAMING_SNAKE_CASE : List[Any] = {f"decoder_{name}": tensor for name, tensor in decoder_inputs.items()} SCREAMING_SNAKE_CASE : str = dict(**lowerCamelCase_ , **lowerCamelCase_ ) if self.use_past: if not is_torch_available(): raise ValueError('''Cannot generate dummy past_keys inputs without PyTorch installed.''' ) else: import torch SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : Any = common_inputs['''input_ids'''].shape SCREAMING_SNAKE_CASE : str = common_inputs['''decoder_input_ids'''].shape[1] SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : Any = self.num_attention_heads SCREAMING_SNAKE_CASE : str = ( batch, num_encoder_attention_heads, encoder_seq_length, self._config.hidden_size // num_encoder_attention_heads, ) SCREAMING_SNAKE_CASE : Optional[Any] = decoder_seq_length + 3 SCREAMING_SNAKE_CASE : int = ( batch, num_decoder_attention_heads, decoder_past_length, self._config.hidden_size // num_decoder_attention_heads, ) SCREAMING_SNAKE_CASE : List[Any] = torch.cat( [common_inputs['''decoder_attention_mask'''], torch.ones(lowerCamelCase_ , lowerCamelCase_ )] , dim=1 ) SCREAMING_SNAKE_CASE : Optional[int] = [] # If the number of encoder and decoder layers are present in the model configuration, both are considered SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : Optional[int] = self.num_layers SCREAMING_SNAKE_CASE : int = min(lowerCamelCase_ , lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Optional[int] = max(lowerCamelCase_ , lowerCamelCase_ ) - min_num_layers SCREAMING_SNAKE_CASE : Tuple = '''encoder''' if num_encoder_layers > num_decoder_layers else '''decoder''' for _ in range(lowerCamelCase_ ): common_inputs["past_key_values"].append( ( torch.zeros(lowerCamelCase_ ), torch.zeros(lowerCamelCase_ ), torch.zeros(lowerCamelCase_ ), torch.zeros(lowerCamelCase_ ), ) ) # TODO: test this. SCREAMING_SNAKE_CASE : int = encoder_shape if remaining_side_name == '''encoder''' else decoder_shape for _ in range(lowerCamelCase_ , lowerCamelCase_ ): common_inputs["past_key_values"].append((torch.zeros(lowerCamelCase_ ), torch.zeros(lowerCamelCase_ )) ) return common_inputs def __lowerCAmelCase ( self :Any , lowerCamelCase_ :PreTrainedTokenizer , lowerCamelCase_ :int = -1 , lowerCamelCase_ :int = -1 , lowerCamelCase_ :bool = False , lowerCamelCase_ :Optional[TensorType] = None , ) -> Mapping[str, Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : Any = self._generate_dummy_inputs_for_sequence_classification_and_question_answering( lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ ) if self.use_past: if not is_torch_available(): raise ValueError('''Cannot generate dummy past_keys inputs without PyTorch installed.''' ) else: import torch SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : Tuple = common_inputs['''input_ids'''].shape # Not using the same length for past_key_values SCREAMING_SNAKE_CASE : List[str] = seqlen + 2 SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : Dict = self.num_layers SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : List[Any] = self.num_attention_heads SCREAMING_SNAKE_CASE : Union[str, Any] = ( batch, num_encoder_attention_heads, past_key_values_length, self._config.hidden_size // num_encoder_attention_heads, ) SCREAMING_SNAKE_CASE : Tuple = common_inputs['''attention_mask'''].dtype SCREAMING_SNAKE_CASE : Any = torch.cat( [common_inputs['''attention_mask'''], torch.ones(lowerCamelCase_ , lowerCamelCase_ , dtype=lowerCamelCase_ )] , dim=1 ) SCREAMING_SNAKE_CASE : Optional[int] = [ (torch.zeros(lowerCamelCase_ ), torch.zeros(lowerCamelCase_ )) for _ in range(lowerCamelCase_ ) ] return common_inputs def __lowerCAmelCase ( self :Union[str, Any] , lowerCamelCase_ :PreTrainedTokenizer , lowerCamelCase_ :int = -1 , lowerCamelCase_ :int = -1 , lowerCamelCase_ :bool = False , lowerCamelCase_ :Optional[TensorType] = None , ) -> Mapping[str, Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : List[str] = compute_effective_axis_dimension( lowerCamelCase_ , fixed_dimension=OnnxConfig.default_fixed_batch , num_token_to_add=0 ) # If dynamic axis (-1) we forward with a fixed dimension of 8 tokens to avoid optimizations made by ONNX SCREAMING_SNAKE_CASE : int = tokenizer.num_special_tokens_to_add(lowerCamelCase_ ) SCREAMING_SNAKE_CASE : int = compute_effective_axis_dimension( lowerCamelCase_ , fixed_dimension=OnnxConfig.default_fixed_sequence , num_token_to_add=lowerCamelCase_ ) # Generate dummy inputs according to compute batch and sequence SCREAMING_SNAKE_CASE : Tuple = [''' '''.join([tokenizer.unk_token] ) * seq_length] * batch_size SCREAMING_SNAKE_CASE : Any = dict(tokenizer(lowerCamelCase_ , return_tensors=lowerCamelCase_ ) ) return common_inputs def __lowerCAmelCase ( self :List[Any] , lowerCamelCase_ :PreTrainedTokenizer , lowerCamelCase_ :int = -1 , lowerCamelCase_ :int = -1 , lowerCamelCase_ :bool = False , lowerCamelCase_ :Optional[TensorType] = None , ) -> Mapping[str, Any]: '''simple docstring''' if self.task in ["default", "seq2seq-lm"]: SCREAMING_SNAKE_CASE : Dict = self._generate_dummy_inputs_for_default_and_seqaseq_lm( lowerCamelCase_ , batch_size=lowerCamelCase_ , seq_length=lowerCamelCase_ , is_pair=lowerCamelCase_ , framework=lowerCamelCase_ ) elif self.task == "causal-lm": SCREAMING_SNAKE_CASE : Union[str, Any] = self._generate_dummy_inputs_for_causal_lm( lowerCamelCase_ , batch_size=lowerCamelCase_ , seq_length=lowerCamelCase_ , is_pair=lowerCamelCase_ , framework=lowerCamelCase_ ) else: SCREAMING_SNAKE_CASE : List[Any] = self._generate_dummy_inputs_for_sequence_classification_and_question_answering( lowerCamelCase_ , batch_size=lowerCamelCase_ , seq_length=lowerCamelCase_ , is_pair=lowerCamelCase_ , framework=lowerCamelCase_ ) return common_inputs def __lowerCAmelCase ( self :Dict , lowerCamelCase_ :int , lowerCamelCase_ :List[str] , lowerCamelCase_ :List[str] , lowerCamelCase_ :Dict ) -> List[Any]: '''simple docstring''' if self.task in ["default", "seq2seq-lm"]: SCREAMING_SNAKE_CASE : Optional[Any] = super()._flatten_past_key_values_(lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ ) else: SCREAMING_SNAKE_CASE : Tuple = super(lowerCamelCase_ , self )._flatten_past_key_values_( lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ )
18
0
"""simple docstring""" import pytest import requests from datasets.utils.file_utils import http_head from .utils import OfflineSimulationMode, RequestWouldHangIndefinitelyError, offline @pytest.mark.integration def __A ( )-> Optional[int]: '''simple docstring''' with offline(OfflineSimulationMode.CONNECTION_TIMES_OUT ): with pytest.raises(a_ ): requests.request('''GET''' , '''https://huggingface.co''' ) with pytest.raises(requests.exceptions.ConnectTimeout ): requests.request('''GET''' , '''https://huggingface.co''' , timeout=1.0 ) @pytest.mark.integration def __A ( )-> Optional[Any]: '''simple docstring''' with offline(OfflineSimulationMode.CONNECTION_FAILS ): with pytest.raises(requests.exceptions.ConnectionError ): requests.request('''GET''' , '''https://huggingface.co''' ) def __A ( )-> Optional[int]: '''simple docstring''' with offline(OfflineSimulationMode.HF_DATASETS_OFFLINE_SET_TO_1 ): with pytest.raises(a_ ): http_head('''https://huggingface.co''' )
713
"""simple docstring""" from collections import OrderedDict from typing import TYPE_CHECKING, Any, Mapping, Optional from packaging import version from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...onnx.utils import compute_effective_axis_dimension from ...utils import logging if TYPE_CHECKING: from ...processing_utils import ProcessorMixin from ...utils import TensorType lowerCamelCase__ : Dict = logging.get_logger(__name__) lowerCamelCase__ : Dict = { "microsoft/layoutlmv3-base": "https://huggingface.co/microsoft/layoutlmv3-base/resolve/main/config.json", } class lowercase__( _UpperCAmelCase ): '''simple docstring''' UpperCamelCase = """layoutlmv3""" def __init__( self :str , lowerCamelCase_ :Optional[Any]=5_02_65 , lowerCamelCase_ :Dict=7_68 , lowerCamelCase_ :Union[str, Any]=12 , lowerCamelCase_ :Optional[Any]=12 , lowerCamelCase_ :Union[str, Any]=30_72 , lowerCamelCase_ :Any="gelu" , lowerCamelCase_ :Union[str, Any]=0.1 , lowerCamelCase_ :str=0.1 , lowerCamelCase_ :Any=5_12 , lowerCamelCase_ :int=2 , lowerCamelCase_ :Optional[Any]=0.0_2 , lowerCamelCase_ :Optional[int]=1E-5 , lowerCamelCase_ :Dict=1 , lowerCamelCase_ :int=0 , lowerCamelCase_ :Tuple=2 , lowerCamelCase_ :List[str]=10_24 , lowerCamelCase_ :Tuple=1_28 , lowerCamelCase_ :Any=1_28 , lowerCamelCase_ :Optional[Any]=True , lowerCamelCase_ :str=32 , lowerCamelCase_ :int=1_28 , lowerCamelCase_ :int=64 , lowerCamelCase_ :List[Any]=2_56 , lowerCamelCase_ :Any=True , lowerCamelCase_ :str=True , lowerCamelCase_ :Union[str, Any]=True , lowerCamelCase_ :List[str]=2_24 , lowerCamelCase_ :Dict=3 , lowerCamelCase_ :Union[str, Any]=16 , lowerCamelCase_ :Any=None , **lowerCamelCase_ :Optional[Any] , ) -> int: '''simple docstring''' super().__init__( vocab_size=lowerCamelCase_ , hidden_size=lowerCamelCase_ , num_hidden_layers=lowerCamelCase_ , num_attention_heads=lowerCamelCase_ , intermediate_size=lowerCamelCase_ , hidden_act=lowerCamelCase_ , hidden_dropout_prob=lowerCamelCase_ , attention_probs_dropout_prob=lowerCamelCase_ , max_position_embeddings=lowerCamelCase_ , type_vocab_size=lowerCamelCase_ , initializer_range=lowerCamelCase_ , layer_norm_eps=lowerCamelCase_ , pad_token_id=lowerCamelCase_ , bos_token_id=lowerCamelCase_ , eos_token_id=lowerCamelCase_ , **lowerCamelCase_ , ) SCREAMING_SNAKE_CASE : Optional[Any] = max_ad_position_embeddings SCREAMING_SNAKE_CASE : List[Any] = coordinate_size SCREAMING_SNAKE_CASE : Tuple = shape_size SCREAMING_SNAKE_CASE : Optional[int] = has_relative_attention_bias SCREAMING_SNAKE_CASE : List[Any] = rel_pos_bins SCREAMING_SNAKE_CASE : int = max_rel_pos SCREAMING_SNAKE_CASE : Any = has_spatial_attention_bias SCREAMING_SNAKE_CASE : List[Any] = rel_ad_pos_bins SCREAMING_SNAKE_CASE : Dict = max_rel_ad_pos SCREAMING_SNAKE_CASE : Optional[int] = text_embed SCREAMING_SNAKE_CASE : Any = visual_embed SCREAMING_SNAKE_CASE : Any = input_size SCREAMING_SNAKE_CASE : Tuple = num_channels SCREAMING_SNAKE_CASE : List[str] = patch_size SCREAMING_SNAKE_CASE : str = classifier_dropout class lowercase__( _UpperCAmelCase ): '''simple docstring''' UpperCamelCase = version.parse("""1.12""" ) @property def __lowerCAmelCase ( self :List[Any] ) -> Mapping[str, Mapping[int, str]]: '''simple docstring''' if self.task in ["question-answering", "sequence-classification"]: return OrderedDict( [ ('''input_ids''', {0: '''batch''', 1: '''sequence'''}), ('''attention_mask''', {0: '''batch''', 1: '''sequence'''}), ('''bbox''', {0: '''batch''', 1: '''sequence'''}), ('''pixel_values''', {0: '''batch''', 1: '''num_channels''', 2: '''height''', 3: '''width'''}), ] ) else: return OrderedDict( [ ('''input_ids''', {0: '''batch''', 1: '''sequence'''}), ('''bbox''', {0: '''batch''', 1: '''sequence'''}), ('''attention_mask''', {0: '''batch''', 1: '''sequence'''}), ('''pixel_values''', {0: '''batch''', 1: '''num_channels'''}), ] ) @property def __lowerCAmelCase ( self :Optional[int] ) -> float: '''simple docstring''' return 1E-5 @property def __lowerCAmelCase ( self :Tuple ) -> int: '''simple docstring''' return 12 def __lowerCAmelCase ( self :List[Any] , lowerCamelCase_ :"ProcessorMixin" , lowerCamelCase_ :int = -1 , lowerCamelCase_ :int = -1 , lowerCamelCase_ :bool = False , lowerCamelCase_ :Optional["TensorType"] = None , lowerCamelCase_ :int = 3 , lowerCamelCase_ :int = 40 , lowerCamelCase_ :int = 40 , ) -> Mapping[str, Any]: '''simple docstring''' setattr(processor.image_processor , '''apply_ocr''' , lowerCamelCase_ ) # If dynamic axis (-1) we forward with a fixed dimension of 2 samples to avoid optimizations made by ONNX SCREAMING_SNAKE_CASE : Dict = compute_effective_axis_dimension( lowerCamelCase_ , fixed_dimension=OnnxConfig.default_fixed_batch , num_token_to_add=0 ) # If dynamic axis (-1) we forward with a fixed dimension of 8 tokens to avoid optimizations made by ONNX SCREAMING_SNAKE_CASE : Union[str, Any] = processor.tokenizer.num_special_tokens_to_add(lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Any = compute_effective_axis_dimension( lowerCamelCase_ , fixed_dimension=OnnxConfig.default_fixed_sequence , num_token_to_add=lowerCamelCase_ ) # Generate dummy inputs according to compute batch and sequence SCREAMING_SNAKE_CASE : Union[str, Any] = [[''' '''.join([processor.tokenizer.unk_token] ) * seq_length]] * batch_size # Generate dummy bounding boxes SCREAMING_SNAKE_CASE : int = [[[48, 84, 73, 1_28]]] * batch_size # If dynamic axis (-1) we forward with a fixed dimension of 2 samples to avoid optimizations made by ONNX # batch_size = compute_effective_axis_dimension(batch_size, fixed_dimension=OnnxConfig.default_fixed_batch) SCREAMING_SNAKE_CASE : List[Any] = self._generate_dummy_images(lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ ) SCREAMING_SNAKE_CASE : int = dict( processor( lowerCamelCase_ , text=lowerCamelCase_ , boxes=lowerCamelCase_ , return_tensors=lowerCamelCase_ , ) ) return inputs
18
0
"""simple docstring""" import argparse import json from typing import List from ltp import LTP from transformers import BertTokenizer def __A ( a_ : List[str] )-> Tuple: '''simple docstring''' if ( (cp >= 0x4E_00 and cp <= 0x9F_FF) or (cp >= 0x34_00 and cp <= 0x4D_BF) # or (cp >= 0x2_00_00 and cp <= 0x2_A6_DF) # or (cp >= 0x2_A7_00 and cp <= 0x2_B7_3F) # or (cp >= 0x2_B7_40 and cp <= 0x2_B8_1F) # or (cp >= 0x2_B8_20 and cp <= 0x2_CE_AF) # or (cp >= 0xF9_00 and cp <= 0xFA_FF) or (cp >= 0x2_F8_00 and cp <= 0x2_FA_1F) # ): # return True return False def __A ( a_ : Optional[int] )-> List[str]: '''simple docstring''' for char in word: SCREAMING_SNAKE_CASE : Optional[Any] = ord(SCREAMING_SNAKE_CASE_ ) if not _is_chinese_char(SCREAMING_SNAKE_CASE_ ): return 0 return 1 def __A ( a_ : Tuple )-> Dict: '''simple docstring''' SCREAMING_SNAKE_CASE : Dict = set() for token in tokens: SCREAMING_SNAKE_CASE : Optional[Any] = len(SCREAMING_SNAKE_CASE_ ) > 1 and is_chinese(SCREAMING_SNAKE_CASE_ ) if chinese_word: word_set.add(SCREAMING_SNAKE_CASE_ ) SCREAMING_SNAKE_CASE : List[Any] = list(SCREAMING_SNAKE_CASE_ ) return word_list def __A ( a_ : Tuple , a_ : Union[str, Any] )-> List[str]: '''simple docstring''' if not chinese_word_set: return bert_tokens SCREAMING_SNAKE_CASE : Any = max([len(SCREAMING_SNAKE_CASE_ ) for w in chinese_word_set] ) SCREAMING_SNAKE_CASE : str = bert_tokens SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : Dict = 0, len(SCREAMING_SNAKE_CASE_ ) while start < end: SCREAMING_SNAKE_CASE : Optional[int] = True if is_chinese(bert_word[start] ): SCREAMING_SNAKE_CASE : str = min(end - start , SCREAMING_SNAKE_CASE_ ) for i in range(SCREAMING_SNAKE_CASE_ , 1 , -1 ): SCREAMING_SNAKE_CASE : Tuple = ''''''.join(bert_word[start : start + i] ) if whole_word in chinese_word_set: for j in range(start + 1 , start + i ): SCREAMING_SNAKE_CASE : Dict = '''##''' + bert_word[j] SCREAMING_SNAKE_CASE : Union[str, Any] = start + i SCREAMING_SNAKE_CASE : Dict = False break if single_word: start += 1 return bert_word def __A ( a_ : Optional[int] , a_ : Any , a_ : Union[str, Any] )-> List[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : Any = [] for i in range(0 , len(SCREAMING_SNAKE_CASE_ ) , 1_00 ): SCREAMING_SNAKE_CASE : List[str] = ltp_tokenizer.seg(lines[i : i + 1_00] )[0] SCREAMING_SNAKE_CASE : Optional[int] = [get_chinese_word(SCREAMING_SNAKE_CASE_ ) for r in res] ltp_res.extend(SCREAMING_SNAKE_CASE_ ) assert len(SCREAMING_SNAKE_CASE_ ) == len(SCREAMING_SNAKE_CASE_ ) SCREAMING_SNAKE_CASE : str = [] for i in range(0 , len(SCREAMING_SNAKE_CASE_ ) , 1_00 ): SCREAMING_SNAKE_CASE : Any = bert_tokenizer(lines[i : i + 1_00] , add_special_tokens=SCREAMING_SNAKE_CASE_ , truncation=SCREAMING_SNAKE_CASE_ , max_length=5_12 ) bert_res.extend(res['''input_ids'''] ) assert len(SCREAMING_SNAKE_CASE_ ) == len(SCREAMING_SNAKE_CASE_ ) SCREAMING_SNAKE_CASE : Union[str, Any] = [] for input_ids, chinese_word in zip(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ): SCREAMING_SNAKE_CASE : str = [] for id in input_ids: SCREAMING_SNAKE_CASE : str = bert_tokenizer._convert_id_to_token(SCREAMING_SNAKE_CASE_ ) input_tokens.append(SCREAMING_SNAKE_CASE_ ) SCREAMING_SNAKE_CASE : int = add_sub_symbol(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) SCREAMING_SNAKE_CASE : Optional[Any] = [] # We only save pos of chinese subwords start with ##, which mean is part of a whole word. for i, token in enumerate(SCREAMING_SNAKE_CASE_ ): if token[:2] == "##": SCREAMING_SNAKE_CASE : int = token[2:] # save chinese tokens' pos if len(SCREAMING_SNAKE_CASE_ ) == 1 and _is_chinese_char(ord(SCREAMING_SNAKE_CASE_ ) ): ref_id.append(SCREAMING_SNAKE_CASE_ ) ref_ids.append(SCREAMING_SNAKE_CASE_ ) assert len(SCREAMING_SNAKE_CASE_ ) == len(SCREAMING_SNAKE_CASE_ ) return ref_ids def __A ( a_ : List[Any] )-> Optional[Any]: '''simple docstring''' with open(args.file_name , '''r''' , encoding='''utf-8''' ) as f: SCREAMING_SNAKE_CASE : Dict = f.readlines() SCREAMING_SNAKE_CASE : str = [line.strip() for line in data if len(SCREAMING_SNAKE_CASE_ ) > 0 and not line.isspace()] # avoid delimiter like '\u2029' SCREAMING_SNAKE_CASE : Optional[int] = LTP(args.ltp ) # faster in GPU device SCREAMING_SNAKE_CASE : Dict = BertTokenizer.from_pretrained(args.bert ) SCREAMING_SNAKE_CASE : Union[str, Any] = prepare_ref(SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ) with open(args.save_path , '''w''' , encoding='''utf-8''' ) as f: SCREAMING_SNAKE_CASE : Dict = [json.dumps(SCREAMING_SNAKE_CASE_ ) + '''\n''' for ref in ref_ids] f.writelines(SCREAMING_SNAKE_CASE_ ) if __name__ == "__main__": lowerCamelCase__ : Dict = argparse.ArgumentParser(description="prepare_chinese_ref") parser.add_argument( "--file_name", type=str, default="./resources/chinese-demo.txt", help="file need process, same as training data in lm", ) parser.add_argument( "--ltp", type=str, default="./resources/ltp", help="resources for LTP tokenizer, usually a path" ) parser.add_argument("--bert", type=str, default="./resources/robert", help="resources for Bert tokenizer") parser.add_argument("--save_path", type=str, default="./resources/ref.txt", help="path to save res") lowerCamelCase__ : Optional[Any] = parser.parse_args() main(args)
714
"""simple docstring""" import math def __A ( a_ : list , a_ : int )-> int: '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[int] = len(a_ ) SCREAMING_SNAKE_CASE : Optional[Any] = int(math.floor(math.sqrt(a_ ) ) ) SCREAMING_SNAKE_CASE : List[str] = 0 while arr[min(a_ , a_ ) - 1] < x: SCREAMING_SNAKE_CASE : Optional[Any] = step step += int(math.floor(math.sqrt(a_ ) ) ) if prev >= n: return -1 while arr[prev] < x: SCREAMING_SNAKE_CASE : Any = prev + 1 if prev == min(a_ , a_ ): return -1 if arr[prev] == x: return prev return -1 if __name__ == "__main__": lowerCamelCase__ : Union[str, Any] = input("Enter numbers separated by a comma:\n").strip() lowerCamelCase__ : List[str] = [int(item) for item in user_input.split(",")] lowerCamelCase__ : Dict = int(input("Enter the number to be searched:\n")) lowerCamelCase__ : Tuple = jump_search(arr, x) if res == -1: print("Number not found!") else: print(f'''Number {x} is at index {res}''')
18
0
"""simple docstring""" def __A ( a_ : List[str] , a_ : Optional[int] )-> int: '''simple docstring''' return int(input_a == input_a == 0 ) def __A ( )-> None: '''simple docstring''' print('''Truth Table of NOR Gate:''' ) print('''| Input 1 | Input 2 | Output |''' ) print(F"| 0 | 0 | {nor_gate(0 , 0 )} |" ) print(F"| 0 | 1 | {nor_gate(0 , 1 )} |" ) print(F"| 1 | 0 | {nor_gate(1 , 0 )} |" ) print(F"| 1 | 1 | {nor_gate(1 , 1 )} |" ) if __name__ == "__main__": import doctest doctest.testmod() main()
715
"""simple docstring""" from sklearn.metrics import fa_score import datasets lowerCamelCase__ : List[Any] = "\nThe F1 score is the harmonic mean of the precision and recall. It can be computed with the equation:\nF1 = 2 * (precision * recall) / (precision + recall)\n" lowerCamelCase__ : str = "\nArgs:\n predictions (`list` of `int`): Predicted labels.\n references (`list` of `int`): Ground truth labels.\n labels (`list` of `int`): The set of labels to include when `average` is not set to `'binary'`, and the order of the labels if `average` is `None`. Labels present in the data can be excluded, for example to calculate a multiclass average ignoring a majority negative class. Labels not present in the data will result in 0 components in a macro average. For multilabel targets, labels are column indices. By default, all labels in `predictions` and `references` are used in sorted order. Defaults to None.\n pos_label (`int`): The class to be considered the positive class, in the case where `average` is set to `binary`. Defaults to 1.\n average (`string`): This parameter is required for multiclass/multilabel targets. If set to `None`, the scores for each class are returned. Otherwise, this determines the type of averaging performed on the data. Defaults to `'binary'`.\n\n - 'binary': Only report results for the class specified by `pos_label`. This is applicable only if the classes found in `predictions` and `references` are binary.\n - 'micro': Calculate metrics globally by counting the total true positives, false negatives and false positives.\n - 'macro': Calculate metrics for each label, and find their unweighted mean. This does not take label imbalance into account.\n - 'weighted': Calculate metrics for each label, and find their average weighted by support (the number of true instances for each label). This alters `'macro'` to account for label imbalance. This option can result in an F-score that is not between precision and recall.\n - 'samples': Calculate metrics for each instance, and find their average (only meaningful for multilabel classification).\n sample_weight (`list` of `float`): Sample weights Defaults to None.\n\nReturns:\n f1 (`float` or `array` of `float`): F1 score or list of f1 scores, depending on the value passed to `average`. Minimum possible value is 0. Maximum possible value is 1. Higher f1 scores are better.\n\nExamples:\n\n Example 1-A simple binary example\n >>> f1_metric = datasets.load_metric(\"f1\")\n >>> results = f1_metric.compute(references=[0, 1, 0, 1, 0], predictions=[0, 0, 1, 1, 0])\n >>> print(results)\n {'f1': 0.5}\n\n Example 2-The same simple binary example as in Example 1, but with `pos_label` set to `0`.\n >>> f1_metric = datasets.load_metric(\"f1\")\n >>> results = f1_metric.compute(references=[0, 1, 0, 1, 0], predictions=[0, 0, 1, 1, 0], pos_label=0)\n >>> print(round(results['f1'], 2))\n 0.67\n\n Example 3-The same simple binary example as in Example 1, but with `sample_weight` included.\n >>> f1_metric = datasets.load_metric(\"f1\")\n >>> results = f1_metric.compute(references=[0, 1, 0, 1, 0], predictions=[0, 0, 1, 1, 0], sample_weight=[0.9, 0.5, 3.9, 1.2, 0.3])\n >>> print(round(results['f1'], 2))\n 0.35\n\n Example 4-A multiclass example, with different values for the `average` input.\n >>> predictions = [0, 2, 1, 0, 0, 1]\n >>> references = [0, 1, 2, 0, 1, 2]\n >>> results = f1_metric.compute(predictions=predictions, references=references, average=\"macro\")\n >>> print(round(results['f1'], 2))\n 0.27\n >>> results = f1_metric.compute(predictions=predictions, references=references, average=\"micro\")\n >>> print(round(results['f1'], 2))\n 0.33\n >>> results = f1_metric.compute(predictions=predictions, references=references, average=\"weighted\")\n >>> print(round(results['f1'], 2))\n 0.27\n >>> results = f1_metric.compute(predictions=predictions, references=references, average=None)\n >>> print(results)\n {'f1': array([0.8, 0. , 0. ])}\n" lowerCamelCase__ : int = "\n@article{scikit-learn,\n title={Scikit-learn: Machine Learning in {P}ython},\n author={Pedregosa, F. and Varoquaux, G. and Gramfort, A. and Michel, V.\n and Thirion, B. and Grisel, O. and Blondel, M. and Prettenhofer, P.\n and Weiss, R. and Dubourg, V. and Vanderplas, J. and Passos, A. and\n Cournapeau, D. and Brucher, M. and Perrot, M. and Duchesnay, E.},\n journal={Journal of Machine Learning Research},\n volume={12},\n pages={2825--2830},\n year={2011}\n}\n" @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class lowercase__( datasets.Metric ): '''simple docstring''' def __lowerCAmelCase ( self :str ) -> Any: '''simple docstring''' return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { '''predictions''': datasets.Sequence(datasets.Value('''int32''' ) ), '''references''': datasets.Sequence(datasets.Value('''int32''' ) ), } if self.config_name == '''multilabel''' else { '''predictions''': datasets.Value('''int32''' ), '''references''': datasets.Value('''int32''' ), } ) , reference_urls=['''https://scikit-learn.org/stable/modules/generated/sklearn.metrics.f1_score.html'''] , ) def __lowerCAmelCase ( self :Any , lowerCamelCase_ :Dict , lowerCamelCase_ :Optional[Any] , lowerCamelCase_ :int=None , lowerCamelCase_ :str=1 , lowerCamelCase_ :Union[str, Any]="binary" , lowerCamelCase_ :Dict=None ) -> int: '''simple docstring''' SCREAMING_SNAKE_CASE : int = fa_score( lowerCamelCase_ , lowerCamelCase_ , labels=lowerCamelCase_ , pos_label=lowerCamelCase_ , average=lowerCamelCase_ , sample_weight=lowerCamelCase_ ) return {"f1": float(lowerCamelCase_ ) if score.size == 1 else score}
18
0
"""simple docstring""" import logging import os import sys from dataclasses import dataclass, field from itertools import chain from typing import Optional, Union import datasets import numpy as np import torch from datasets import load_dataset import transformers from transformers import ( AutoConfig, AutoModelForMultipleChoice, AutoTokenizer, HfArgumentParser, Trainer, TrainingArguments, default_data_collator, set_seed, ) from transformers.tokenization_utils_base import PreTrainedTokenizerBase from transformers.trainer_utils import get_last_checkpoint from transformers.utils import PaddingStrategy, check_min_version, send_example_telemetry # Will error if the minimal version of Transformers is not installed. Remove at your own risks. check_min_version("4.31.0") lowerCamelCase__ : Any = logging.getLogger(__name__) @dataclass class lowercase__: '''simple docstring''' UpperCamelCase = field( metadata={"""help""": """Path to pretrained model or model identifier from huggingface.co/models"""} ) UpperCamelCase = field( default=UpperCamelCase__ , metadata={"""help""": """Pretrained config name or path if not the same as model_name"""} ) UpperCamelCase = field( default=UpperCamelCase__ , metadata={"""help""": """Pretrained tokenizer name or path if not the same as model_name"""} ) UpperCamelCase = field( default=UpperCamelCase__ , metadata={"""help""": """Where do you want to store the pretrained models downloaded from huggingface.co"""} , ) UpperCamelCase = field( default=UpperCamelCase__ , metadata={"""help""": """Whether to use one of the fast tokenizer (backed by the tokenizers library) or not."""} , ) UpperCamelCase = field( default="""main""" , metadata={"""help""": """The specific model version to use (can be a branch name, tag name or commit id)."""} , ) UpperCamelCase = field( default=UpperCamelCase__ , metadata={ """help""": ( """Will use the token generated when running `huggingface-cli login` (necessary to use this script """ """with private models).""" ) } , ) @dataclass class lowercase__: '''simple docstring''' UpperCamelCase = field(default=UpperCamelCase__ , metadata={"""help""": """The input training data file (a text file)."""} ) UpperCamelCase = field( default=UpperCamelCase__ , metadata={"""help""": """An optional input evaluation data file to evaluate the perplexity on (a text file)."""} , ) UpperCamelCase = field( default=UpperCamelCase__ , metadata={"""help""": """Overwrite the cached training and evaluation sets"""} ) UpperCamelCase = field( default=UpperCamelCase__ , metadata={"""help""": """The number of processes to use for the preprocessing."""} , ) UpperCamelCase = field( default=UpperCamelCase__ , metadata={ """help""": ( """The maximum total input sequence length after tokenization. If passed, sequences longer """ """than this will be truncated, sequences shorter will be padded.""" ) } , ) UpperCamelCase = field( default=UpperCamelCase__ , metadata={ """help""": ( """Whether to pad all samples to the maximum sentence length. """ """If False, will pad the samples dynamically when batching to the maximum length in the batch. More """ """efficient on GPU but very bad for TPU.""" ) } , ) UpperCamelCase = field( default=UpperCamelCase__ , metadata={ """help""": ( """For debugging purposes or quicker training, truncate the number of training examples to this """ """value if set.""" ) } , ) UpperCamelCase = field( default=UpperCamelCase__ , metadata={ """help""": ( """For debugging purposes or quicker training, truncate the number of evaluation examples to this """ """value if set.""" ) } , ) def __lowerCAmelCase ( self :str ) -> Tuple: '''simple docstring''' if self.train_file is not None: SCREAMING_SNAKE_CASE : Dict = self.train_file.split('''.''' )[-1] assert extension in ["csv", "json"], "`train_file` should be a csv or a json file." if self.validation_file is not None: SCREAMING_SNAKE_CASE : Optional[Any] = self.validation_file.split('''.''' )[-1] assert extension in ["csv", "json"], "`validation_file` should be a csv or a json file." @dataclass class lowercase__: '''simple docstring''' UpperCamelCase = 42 UpperCamelCase = True UpperCamelCase = None UpperCamelCase = None def __call__( self :List[str] , lowerCamelCase_ :str ) -> Optional[int]: '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[Any] = '''label''' if '''label''' in features[0].keys() else '''labels''' SCREAMING_SNAKE_CASE : Dict = [feature.pop(lowerCamelCase_ ) for feature in features] SCREAMING_SNAKE_CASE : Optional[int] = len(lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Dict = len(features[0]['''input_ids'''] ) SCREAMING_SNAKE_CASE : str = [ [{k: v[i] for k, v in feature.items()} for i in range(lowerCamelCase_ )] for feature in features ] SCREAMING_SNAKE_CASE : List[str] = list(chain(*lowerCamelCase_ ) ) SCREAMING_SNAKE_CASE : str = self.tokenizer.pad( lowerCamelCase_ , padding=self.padding , max_length=self.max_length , pad_to_multiple_of=self.pad_to_multiple_of , return_tensors='''pt''' , ) # Un-flatten SCREAMING_SNAKE_CASE : str = {k: v.view(lowerCamelCase_ , lowerCamelCase_ , -1 ) for k, v in batch.items()} # Add back labels SCREAMING_SNAKE_CASE : str = torch.tensor(lowerCamelCase_ , dtype=torch.intaa ) return batch def __A ( )-> Optional[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[int] = 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 : List[str] = parser.parse_json_file(json_file=os.path.abspath(sys.argv[1] ) ) else: SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : Union[str, 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_swag''' , a_ , a_ ) # 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 : Union[str, Any] = training_args.get_process_log_level() logger.setLevel(a_ ) datasets.utils.logging.set_verbosity(a_ ) transformers.utils.logging.set_verbosity(a_ ) 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 : 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 : Optional[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 ) # Get the datasets: you can either provide your own CSV/JSON/TXT training and evaluation files (see below) # or just provide the name of one of the public datasets available on the hub at https://huggingface.co/datasets/ # (the dataset will be downloaded automatically from the datasets Hub). # For CSV/JSON files, this script will use the column called 'text' or the first column if no column called # 'text' is found. You can easily tweak this behavior (see below). # In distributed training, the load_dataset function guarantee that only one local process can concurrently # download the dataset. if data_args.train_file is not None or data_args.validation_file is not None: SCREAMING_SNAKE_CASE : Optional[Any] = {} if data_args.train_file is not None: SCREAMING_SNAKE_CASE : Tuple = data_args.train_file if data_args.validation_file is not None: SCREAMING_SNAKE_CASE : Optional[int] = data_args.validation_file SCREAMING_SNAKE_CASE : str = data_args.train_file.split('''.''' )[-1] SCREAMING_SNAKE_CASE : str = load_dataset( a_ , data_files=a_ , cache_dir=model_args.cache_dir , use_auth_token=True if model_args.use_auth_token else None , ) else: # Downloading and loading the swag dataset from the hub. SCREAMING_SNAKE_CASE : Any = load_dataset( '''swag''' , '''regular''' , cache_dir=model_args.cache_dir , use_auth_token=True if model_args.use_auth_token else None , ) # See more about loading any type of standard or custom dataset (from files, python dict, pandas DataFrame, etc) at # https://huggingface.co/docs/datasets/loading_datasets.html. # Load pretrained model and tokenizer # Distributed training: # The .from_pretrained methods guarantee that only one local process can concurrently # download model & vocab. SCREAMING_SNAKE_CASE : Union[str, Any] = AutoConfig.from_pretrained( model_args.config_name if model_args.config_name else 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 , ) SCREAMING_SNAKE_CASE : List[str] = AutoTokenizer.from_pretrained( model_args.tokenizer_name if model_args.tokenizer_name else model_args.model_name_or_path , cache_dir=model_args.cache_dir , use_fast=model_args.use_fast_tokenizer , revision=model_args.model_revision , use_auth_token=True if model_args.use_auth_token else None , ) SCREAMING_SNAKE_CASE : Optional[int] = AutoModelForMultipleChoice.from_pretrained( model_args.model_name_or_path , from_tf=bool('''.ckpt''' in model_args.model_name_or_path ) , config=a_ , cache_dir=model_args.cache_dir , revision=model_args.model_revision , use_auth_token=True if model_args.use_auth_token else None , ) # When using your own dataset or a different dataset from swag, you will probably need to change this. SCREAMING_SNAKE_CASE : Tuple = [F"ending{i}" for i in range(4 )] SCREAMING_SNAKE_CASE : List[Any] = '''sent1''' SCREAMING_SNAKE_CASE : Dict = '''sent2''' if data_args.max_seq_length is None: SCREAMING_SNAKE_CASE : Tuple = tokenizer.model_max_length if max_seq_length > 10_24: logger.warning( '''The chosen tokenizer supports a `model_max_length` that is longer than the default `block_size` value''' ''' of 1024. If you would like to use a longer `block_size` up to `tokenizer.model_max_length` you can''' ''' override this default with `--block_size xxx`.''' ) SCREAMING_SNAKE_CASE : Tuple = 10_24 else: if data_args.max_seq_length > tokenizer.model_max_length: logger.warning( F"The max_seq_length passed ({data_args.max_seq_length}) is larger than the maximum length for the" F"model ({tokenizer.model_max_length}). Using max_seq_length={tokenizer.model_max_length}." ) SCREAMING_SNAKE_CASE : Optional[Any] = min(data_args.max_seq_length , tokenizer.model_max_length ) # Preprocessing the datasets. def preprocess_function(a_ : Tuple ): SCREAMING_SNAKE_CASE : int = [[context] * 4 for context in examples[context_name]] SCREAMING_SNAKE_CASE : Optional[int] = examples[question_header_name] SCREAMING_SNAKE_CASE : Dict = [ [F"{header} {examples[end][i]}" for end in ending_names] for i, header in enumerate(a_ ) ] # Flatten out SCREAMING_SNAKE_CASE : str = list(chain(*a_ ) ) SCREAMING_SNAKE_CASE : List[Any] = list(chain(*a_ ) ) # Tokenize SCREAMING_SNAKE_CASE : str = tokenizer( a_ , a_ , truncation=a_ , max_length=a_ , padding='''max_length''' if data_args.pad_to_max_length else False , ) # Un-flatten return {k: [v[i : i + 4] for i in range(0 , len(a_ ) , 4 )] for k, v in tokenized_examples.items()} if training_args.do_train: if "train" not in raw_datasets: raise ValueError('''--do_train requires a train dataset''' ) SCREAMING_SNAKE_CASE : Tuple = raw_datasets['''train'''] if data_args.max_train_samples is not None: SCREAMING_SNAKE_CASE : Tuple = min(len(a_ ) , data_args.max_train_samples ) SCREAMING_SNAKE_CASE : List[str] = train_dataset.select(range(a_ ) ) with training_args.main_process_first(desc='''train dataset map pre-processing''' ): SCREAMING_SNAKE_CASE : Union[str, Any] = train_dataset.map( a_ , batched=a_ , num_proc=data_args.preprocessing_num_workers , load_from_cache_file=not data_args.overwrite_cache , ) if training_args.do_eval: if "validation" not in raw_datasets: raise ValueError('''--do_eval requires a validation dataset''' ) SCREAMING_SNAKE_CASE : Optional[Any] = raw_datasets['''validation'''] if data_args.max_eval_samples is not None: SCREAMING_SNAKE_CASE : Dict = min(len(a_ ) , data_args.max_eval_samples ) SCREAMING_SNAKE_CASE : int = eval_dataset.select(range(a_ ) ) with training_args.main_process_first(desc='''validation dataset map pre-processing''' ): SCREAMING_SNAKE_CASE : int = eval_dataset.map( a_ , batched=a_ , num_proc=data_args.preprocessing_num_workers , load_from_cache_file=not data_args.overwrite_cache , ) # Data collator SCREAMING_SNAKE_CASE : Tuple = ( default_data_collator if data_args.pad_to_max_length else DataCollatorForMultipleChoice(tokenizer=a_ , pad_to_multiple_of=8 if training_args.fpaa else None ) ) # Metric def compute_metrics(a_ : Dict ): SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : Optional[Any] = eval_predictions SCREAMING_SNAKE_CASE : List[Any] = np.argmax(a_ , axis=1 ) return {"accuracy": (preds == label_ids).astype(np.floataa ).mean().item()} # Initialize our Trainer SCREAMING_SNAKE_CASE : Union[str, Any] = Trainer( model=a_ , args=a_ , train_dataset=train_dataset if training_args.do_train else None , eval_dataset=eval_dataset if training_args.do_eval else None , tokenizer=a_ , data_collator=a_ , compute_metrics=a_ , ) # Training if training_args.do_train: SCREAMING_SNAKE_CASE : Optional[int] = None if training_args.resume_from_checkpoint is not None: SCREAMING_SNAKE_CASE : Optional[int] = training_args.resume_from_checkpoint elif last_checkpoint is not None: SCREAMING_SNAKE_CASE : Tuple = last_checkpoint SCREAMING_SNAKE_CASE : int = trainer.train(resume_from_checkpoint=a_ ) trainer.save_model() # Saves the tokenizer too for easy upload SCREAMING_SNAKE_CASE : Optional[Any] = train_result.metrics SCREAMING_SNAKE_CASE : List[str] = ( data_args.max_train_samples if data_args.max_train_samples is not None else len(a_ ) ) SCREAMING_SNAKE_CASE : Dict = min(a_ , len(a_ ) ) trainer.log_metrics('''train''' , a_ ) trainer.save_metrics('''train''' , a_ ) trainer.save_state() # Evaluation if training_args.do_eval: logger.info('''*** Evaluate ***''' ) SCREAMING_SNAKE_CASE : List[Any] = trainer.evaluate() SCREAMING_SNAKE_CASE : Union[str, Any] = data_args.max_eval_samples if data_args.max_eval_samples is not None else len(a_ ) SCREAMING_SNAKE_CASE : str = min(a_ , len(a_ ) ) trainer.log_metrics('''eval''' , a_ ) trainer.save_metrics('''eval''' , a_ ) SCREAMING_SNAKE_CASE : List[str] = { '''finetuned_from''': model_args.model_name_or_path, '''tasks''': '''multiple-choice''', '''dataset_tags''': '''swag''', '''dataset_args''': '''regular''', '''dataset''': '''SWAG''', '''language''': '''en''', } if training_args.push_to_hub: trainer.push_to_hub(**a_ ) else: trainer.create_model_card(**a_ ) def __A ( a_ : Tuple )-> Optional[int]: '''simple docstring''' main() if __name__ == "__main__": main()
716
"""simple docstring""" from __future__ import annotations from fractions import Fraction def __A ( a_ : int , a_ : int )-> bool: '''simple docstring''' return ( num != den and num % 10 == den // 10 and (num // 10) / (den % 10) == num / den ) def __A ( a_ : int )-> list[str]: '''simple docstring''' SCREAMING_SNAKE_CASE : Union[str, Any] = [] SCREAMING_SNAKE_CASE : List[str] = 11 SCREAMING_SNAKE_CASE : Union[str, Any] = int('''1''' + '''0''' * digit_len ) for num in range(a_ , a_ ): while den <= 99: if (num != den) and (num % 10 == den // 10) and (den % 10 != 0): if is_digit_cancelling(a_ , a_ ): solutions.append(F"{num}/{den}" ) den += 1 num += 1 SCREAMING_SNAKE_CASE : Optional[Any] = 10 return solutions def __A ( a_ : int = 2 )-> int: '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[Any] = 1.0 for fraction in fraction_list(a_ ): SCREAMING_SNAKE_CASE : List[str] = Fraction(a_ ) result *= frac.denominator / frac.numerator return int(a_ ) if __name__ == "__main__": print(solution())
18
0
"""simple docstring""" from typing import Callable, List, Optional, Tuple, Union import torch from transformers import CLIPTextModel, CLIPTokenizer from ...configuration_utils import ConfigMixin, register_to_config from ...models import ModelMixin, TransformeraDModel, VQModel from ...schedulers import VQDiffusionScheduler from ...utils import logging from ..pipeline_utils import DiffusionPipeline, ImagePipelineOutput lowerCamelCase__ : Dict = logging.get_logger(__name__) # pylint: disable=invalid-name class lowercase__( UpperCAmelCase__ , UpperCAmelCase__ ): '''simple docstring''' @register_to_config def __init__( self :str , lowerCamelCase_ :bool , lowerCamelCase_ :Optional[int] = None , lowerCamelCase_ :Optional[int] = None ) -> Optional[int]: '''simple docstring''' super().__init__() SCREAMING_SNAKE_CASE : Optional[int] = learnable if self.learnable: assert hidden_size is not None, "learnable=True requires `hidden_size` to be set" assert length is not None, "learnable=True requires `length` to be set" SCREAMING_SNAKE_CASE : str = torch.zeros(lowerCamelCase__ , lowerCamelCase__ ) else: SCREAMING_SNAKE_CASE : str = None SCREAMING_SNAKE_CASE : str = torch.nn.Parameter(lowerCamelCase__ ) class lowercase__( UpperCAmelCase__ ): '''simple docstring''' UpperCamelCase = 42 UpperCamelCase = 42 UpperCamelCase = 42 UpperCamelCase = 42 UpperCamelCase = 42 UpperCamelCase = 42 def __init__( self :int , lowerCamelCase_ :VQModel , lowerCamelCase_ :CLIPTextModel , lowerCamelCase_ :CLIPTokenizer , lowerCamelCase_ :TransformeraDModel , lowerCamelCase_ :VQDiffusionScheduler , lowerCamelCase_ :LearnedClassifierFreeSamplingEmbeddings , ) -> Dict: '''simple docstring''' super().__init__() self.register_modules( vqvae=lowerCamelCase__ , transformer=lowerCamelCase__ , text_encoder=lowerCamelCase__ , tokenizer=lowerCamelCase__ , scheduler=lowerCamelCase__ , learned_classifier_free_sampling_embeddings=lowerCamelCase__ , ) def __lowerCAmelCase ( self :Optional[Any] , lowerCamelCase_ :Union[str, Any] , lowerCamelCase_ :Optional[int] , lowerCamelCase_ :List[Any] ) -> int: '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[Any] = len(lowerCamelCase__ ) if isinstance(lowerCamelCase__ , lowerCamelCase__ ) else 1 # get prompt text embeddings SCREAMING_SNAKE_CASE : Union[str, Any] = self.tokenizer( lowerCamelCase__ , padding='''max_length''' , max_length=self.tokenizer.model_max_length , return_tensors='''pt''' , ) SCREAMING_SNAKE_CASE : Union[str, Any] = text_inputs.input_ids if text_input_ids.shape[-1] > self.tokenizer.model_max_length: SCREAMING_SNAKE_CASE : Tuple = self.tokenizer.batch_decode(text_input_ids[:, self.tokenizer.model_max_length :] ) logger.warning( '''The following part of your input was truncated because CLIP can only handle sequences up to''' f" {self.tokenizer.model_max_length} tokens: {removed_text}" ) SCREAMING_SNAKE_CASE : List[Any] = text_input_ids[:, : self.tokenizer.model_max_length] SCREAMING_SNAKE_CASE : int = self.text_encoder(text_input_ids.to(self.device ) )[0] # NOTE: This additional step of normalizing the text embeddings is from VQ-Diffusion. # While CLIP does normalize the pooled output of the text transformer when combining # the image and text embeddings, CLIP does not directly normalize the last hidden state. # # CLIP normalizing the pooled output. # https://github.com/huggingface/transformers/blob/d92e22d1f28324f513f3080e5c47c071a3916721/src/transformers/models/clip/modeling_clip.py#L1052-L1053 SCREAMING_SNAKE_CASE : Union[str, Any] = prompt_embeds / prompt_embeds.norm(dim=-1 , keepdim=lowerCamelCase__ ) # duplicate text embeddings for each generation per prompt SCREAMING_SNAKE_CASE : Tuple = prompt_embeds.repeat_interleave(lowerCamelCase__ , dim=0 ) if do_classifier_free_guidance: if self.learned_classifier_free_sampling_embeddings.learnable: SCREAMING_SNAKE_CASE : List[str] = self.learned_classifier_free_sampling_embeddings.embeddings SCREAMING_SNAKE_CASE : Union[str, Any] = negative_prompt_embeds.unsqueeze(0 ).repeat(lowerCamelCase__ , 1 , 1 ) else: SCREAMING_SNAKE_CASE : Optional[int] = [""] * batch_size SCREAMING_SNAKE_CASE : List[Any] = text_input_ids.shape[-1] SCREAMING_SNAKE_CASE : Dict = self.tokenizer( lowerCamelCase__ , padding='''max_length''' , max_length=lowerCamelCase__ , truncation=lowerCamelCase__ , return_tensors='''pt''' , ) SCREAMING_SNAKE_CASE : Optional[int] = self.text_encoder(uncond_input.input_ids.to(self.device ) )[0] # See comment for normalizing text embeddings SCREAMING_SNAKE_CASE : Any = negative_prompt_embeds / negative_prompt_embeds.norm(dim=-1 , keepdim=lowerCamelCase__ ) # duplicate unconditional embeddings for each generation per prompt, using mps friendly method SCREAMING_SNAKE_CASE : Optional[Any] = negative_prompt_embeds.shape[1] SCREAMING_SNAKE_CASE : str = negative_prompt_embeds.repeat(1 , lowerCamelCase__ , 1 ) SCREAMING_SNAKE_CASE : Any = negative_prompt_embeds.view(batch_size * num_images_per_prompt , lowerCamelCase__ , -1 ) # For classifier free guidance, we need to do two forward passes. # Here we concatenate the unconditional and text embeddings into a single batch # to avoid doing two forward passes SCREAMING_SNAKE_CASE : str = torch.cat([negative_prompt_embeds, prompt_embeds] ) return prompt_embeds @torch.no_grad() def __call__( self :Tuple , lowerCamelCase_ :Union[str, List[str]] , lowerCamelCase_ :int = 1_00 , lowerCamelCase_ :float = 5.0 , lowerCamelCase_ :float = 1.0 , lowerCamelCase_ :int = 1 , lowerCamelCase_ :Optional[Union[torch.Generator, List[torch.Generator]]] = None , lowerCamelCase_ :Optional[torch.FloatTensor] = None , lowerCamelCase_ :Optional[str] = "pil" , lowerCamelCase_ :bool = True , lowerCamelCase_ :Optional[Callable[[int, int, torch.FloatTensor], None]] = None , lowerCamelCase_ :int = 1 , ) -> Union[ImagePipelineOutput, Tuple]: '''simple docstring''' if isinstance(lowerCamelCase__ , lowerCamelCase__ ): SCREAMING_SNAKE_CASE : Tuple = 1 elif isinstance(lowerCamelCase__ , lowerCamelCase__ ): SCREAMING_SNAKE_CASE : Optional[Any] = len(lowerCamelCase__ ) else: raise ValueError(f"`prompt` has to be of type `str` or `list` but is {type(lowerCamelCase__ )}" ) SCREAMING_SNAKE_CASE : List[str] = batch_size * num_images_per_prompt SCREAMING_SNAKE_CASE : Optional[Any] = guidance_scale > 1.0 SCREAMING_SNAKE_CASE : Union[str, Any] = self._encode_prompt(lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ ) if (callback_steps is None) or ( callback_steps is not None and (not isinstance(lowerCamelCase__ , lowerCamelCase__ ) or callback_steps <= 0) ): raise ValueError( f"`callback_steps` has to be a positive integer but is {callback_steps} of type" f" {type(lowerCamelCase__ )}." ) # get the initial completely masked latents unless the user supplied it SCREAMING_SNAKE_CASE : int = (batch_size, self.transformer.num_latent_pixels) if latents is None: SCREAMING_SNAKE_CASE : Optional[int] = self.transformer.num_vector_embeds - 1 SCREAMING_SNAKE_CASE : Optional[int] = torch.full(lowerCamelCase__ , lowerCamelCase__ ).to(self.device ) else: if latents.shape != latents_shape: raise ValueError(f"Unexpected latents shape, got {latents.shape}, expected {latents_shape}" ) if (latents < 0).any() or (latents >= self.transformer.num_vector_embeds).any(): raise ValueError( '''Unexpected latents value(s). All latents be valid embedding indices i.e. in the range 0,''' f" {self.transformer.num_vector_embeds - 1} (inclusive)." ) SCREAMING_SNAKE_CASE : Optional[Any] = latents.to(self.device ) # set timesteps self.scheduler.set_timesteps(lowerCamelCase__ , device=self.device ) SCREAMING_SNAKE_CASE : str = self.scheduler.timesteps.to(self.device ) SCREAMING_SNAKE_CASE : Any = latents for i, t in enumerate(self.progress_bar(lowerCamelCase__ ) ): # expand the sample if we are doing classifier free guidance SCREAMING_SNAKE_CASE : Dict = torch.cat([sample] * 2 ) if do_classifier_free_guidance else sample # predict the un-noised image # model_output == `log_p_x_0` SCREAMING_SNAKE_CASE : Any = self.transformer(lowerCamelCase__ , encoder_hidden_states=lowerCamelCase__ , timestep=lowerCamelCase__ ).sample if do_classifier_free_guidance: SCREAMING_SNAKE_CASE : int = model_output.chunk(2 ) SCREAMING_SNAKE_CASE : List[Any] = model_output_uncond + guidance_scale * (model_output_text - model_output_uncond) model_output -= torch.logsumexp(lowerCamelCase__ , dim=1 , keepdim=lowerCamelCase__ ) SCREAMING_SNAKE_CASE : Dict = self.truncate(lowerCamelCase__ , lowerCamelCase__ ) # remove `log(0)`'s (`-inf`s) SCREAMING_SNAKE_CASE : Optional[int] = model_output.clamp(-70 ) # compute the previous noisy sample x_t -> x_t-1 SCREAMING_SNAKE_CASE : Any = self.scheduler.step(lowerCamelCase__ , timestep=lowerCamelCase__ , sample=lowerCamelCase__ , generator=lowerCamelCase__ ).prev_sample # call the callback, if provided if callback is not None and i % callback_steps == 0: callback(lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ ) SCREAMING_SNAKE_CASE : Any = self.vqvae.config.vq_embed_dim SCREAMING_SNAKE_CASE : Dict = (batch_size, self.transformer.height, self.transformer.width, embedding_channels) SCREAMING_SNAKE_CASE : Optional[int] = self.vqvae.quantize.get_codebook_entry(lowerCamelCase__ , shape=lowerCamelCase__ ) SCREAMING_SNAKE_CASE : int = self.vqvae.decode(lowerCamelCase__ , force_not_quantize=lowerCamelCase__ ).sample SCREAMING_SNAKE_CASE : str = (image / 2 + 0.5).clamp(0 , 1 ) SCREAMING_SNAKE_CASE : Optional[int] = image.cpu().permute(0 , 2 , 3 , 1 ).numpy() if output_type == "pil": SCREAMING_SNAKE_CASE : List[str] = self.numpy_to_pil(lowerCamelCase__ ) if not return_dict: return (image,) return ImagePipelineOutput(images=lowerCamelCase__ ) def __lowerCAmelCase ( self :Optional[Any] , lowerCamelCase_ :torch.FloatTensor , lowerCamelCase_ :float ) -> torch.FloatTensor: '''simple docstring''' SCREAMING_SNAKE_CASE : Tuple = torch.sort(lowerCamelCase__ , 1 , descending=lowerCamelCase__ ) SCREAMING_SNAKE_CASE : Any = torch.exp(lowerCamelCase__ ) SCREAMING_SNAKE_CASE : Union[str, Any] = sorted_p_x_0.cumsum(dim=1 ) < truncation_rate # Ensure that at least the largest probability is not zeroed out SCREAMING_SNAKE_CASE : List[str] = torch.full_like(keep_mask[:, 0:1, :] , lowerCamelCase__ ) SCREAMING_SNAKE_CASE : Optional[Any] = torch.cat((all_true, keep_mask) , dim=1 ) SCREAMING_SNAKE_CASE : str = keep_mask[:, :-1, :] SCREAMING_SNAKE_CASE : str = keep_mask.gather(1 , indices.argsort(1 ) ) SCREAMING_SNAKE_CASE : int = log_p_x_0.clone() SCREAMING_SNAKE_CASE : int = -torch.inf # -inf = log(0) return rv
717
"""simple docstring""" from ...configuration_utils import PretrainedConfig from ...utils import logging from ...utils.backbone_utils import BackboneConfigMixin, get_aligned_output_features_output_indices lowerCamelCase__ : int = logging.get_logger(__name__) class lowercase__( _UpperCAmelCase , _UpperCAmelCase ): '''simple docstring''' UpperCamelCase = """maskformer-swin""" UpperCamelCase = { """num_attention_heads""": """num_heads""", """num_hidden_layers""": """num_layers""", } def __init__( self :Optional[int] , lowerCamelCase_ :List[Any]=2_24 , lowerCamelCase_ :Tuple=4 , lowerCamelCase_ :Optional[Any]=3 , lowerCamelCase_ :List[str]=96 , lowerCamelCase_ :int=[2, 2, 6, 2] , lowerCamelCase_ :Union[str, Any]=[3, 6, 12, 24] , lowerCamelCase_ :Optional[int]=7 , lowerCamelCase_ :Tuple=4.0 , lowerCamelCase_ :Tuple=True , lowerCamelCase_ :Dict=0.0 , lowerCamelCase_ :Any=0.0 , lowerCamelCase_ :List[Any]=0.1 , lowerCamelCase_ :Dict="gelu" , lowerCamelCase_ :Optional[int]=False , lowerCamelCase_ :List[str]=0.0_2 , lowerCamelCase_ :Any=1E-5 , lowerCamelCase_ :Union[str, Any]=None , lowerCamelCase_ :List[str]=None , **lowerCamelCase_ :Union[str, Any] , ) -> Dict: '''simple docstring''' super().__init__(**lowerCamelCase_ ) SCREAMING_SNAKE_CASE : int = image_size SCREAMING_SNAKE_CASE : Optional[Any] = patch_size SCREAMING_SNAKE_CASE : str = num_channels SCREAMING_SNAKE_CASE : Union[str, Any] = embed_dim SCREAMING_SNAKE_CASE : List[Any] = depths SCREAMING_SNAKE_CASE : List[str] = len(lowerCamelCase_ ) SCREAMING_SNAKE_CASE : str = num_heads SCREAMING_SNAKE_CASE : Any = window_size SCREAMING_SNAKE_CASE : List[str] = mlp_ratio SCREAMING_SNAKE_CASE : str = qkv_bias SCREAMING_SNAKE_CASE : Tuple = hidden_dropout_prob SCREAMING_SNAKE_CASE : int = attention_probs_dropout_prob SCREAMING_SNAKE_CASE : int = drop_path_rate SCREAMING_SNAKE_CASE : Tuple = hidden_act SCREAMING_SNAKE_CASE : Any = use_absolute_embeddings SCREAMING_SNAKE_CASE : List[str] = layer_norm_eps SCREAMING_SNAKE_CASE : List[str] = initializer_range # we set the hidden_size attribute in order to make Swin work with VisionEncoderDecoderModel # this indicates the channel dimension after the last stage of the model SCREAMING_SNAKE_CASE : int = int(embed_dim * 2 ** (len(lowerCamelCase_ ) - 1) ) SCREAMING_SNAKE_CASE : Dict = ['''stem'''] + [f"stage{idx}" for idx in range(1 , len(lowerCamelCase_ ) + 1 )] SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : int = get_aligned_output_features_output_indices( out_features=lowerCamelCase_ , out_indices=lowerCamelCase_ , stage_names=self.stage_names )
18
0
"""simple docstring""" import os import time import pytest from datasets.utils.filelock import FileLock, Timeout def __A ( a_ : Dict )-> int: '''simple docstring''' SCREAMING_SNAKE_CASE : Tuple = FileLock(str(tmpdir / '''foo.lock''' ) ) SCREAMING_SNAKE_CASE : Tuple = FileLock(str(tmpdir / '''foo.lock''' ) ) SCREAMING_SNAKE_CASE : Dict = 0.01 with locka.acquire(): with pytest.raises(lowercase__ ): SCREAMING_SNAKE_CASE : Union[str, Any] = time.time() locka.acquire(lowercase__ ) assert time.time() - _start > timeout def __A ( a_ : Union[str, Any] )-> Any: '''simple docstring''' SCREAMING_SNAKE_CASE : Tuple = '''a''' * 10_00 + '''.lock''' SCREAMING_SNAKE_CASE : Optional[int] = FileLock(str(tmpdir / filename ) ) assert locka._lock_file.endswith('''.lock''' ) assert not locka._lock_file.endswith(lowercase__ ) assert len(os.path.basename(locka._lock_file ) ) <= 2_55 SCREAMING_SNAKE_CASE : Union[str, Any] = FileLock(tmpdir / filename ) with locka.acquire(): with pytest.raises(lowercase__ ): locka.acquire(0 )
718
"""simple docstring""" import math class lowercase__: '''simple docstring''' def __init__( self :Union[str, Any] , lowerCamelCase_ :List[str]=0 ) -> List[Any]: # a graph with Node 0,1,...,N-1 '''simple docstring''' SCREAMING_SNAKE_CASE : Tuple = n SCREAMING_SNAKE_CASE : List[Any] = [ [math.inf for j in range(0 , lowerCamelCase_ )] for i in range(0 , lowerCamelCase_ ) ] # adjacency matrix for weight SCREAMING_SNAKE_CASE : Any = [ [math.inf for j in range(0 , lowerCamelCase_ )] for i in range(0 , lowerCamelCase_ ) ] # dp[i][j] stores minimum distance from i to j def __lowerCAmelCase ( self :Dict , lowerCamelCase_ :Optional[int] , lowerCamelCase_ :Union[str, Any] , lowerCamelCase_ :Union[str, Any] ) -> Tuple: '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[Any] = w def __lowerCAmelCase ( self :str ) -> Union[str, Any]: '''simple docstring''' for k in range(0 , self.n ): for i in range(0 , self.n ): for j in range(0 , self.n ): SCREAMING_SNAKE_CASE : List[str] = min(self.dp[i][j] , self.dp[i][k] + self.dp[k][j] ) def __lowerCAmelCase ( self :int , lowerCamelCase_ :Union[str, Any] , lowerCamelCase_ :Optional[int] ) -> Optional[Any]: '''simple docstring''' return self.dp[u][v] if __name__ == "__main__": lowerCamelCase__ : Dict = Graph(5) graph.add_edge(0, 2, 9) graph.add_edge(0, 4, 10) graph.add_edge(1, 3, 5) graph.add_edge(2, 3, 7) graph.add_edge(3, 0, 10) graph.add_edge(3, 1, 2) graph.add_edge(3, 2, 1) graph.add_edge(3, 4, 6) graph.add_edge(4, 1, 3) graph.add_edge(4, 2, 4) graph.add_edge(4, 3, 9) graph.floyd_warshall() graph.show_min(1, 4) graph.show_min(0, 3)
18
0
"""simple docstring""" import numpy as np from cva import COLOR_BGR2GRAY, CV_8UC3, cvtColor, filteraD, imread, imshow, waitKey def __A ( a_ : int , a_ : int , a_ : int , a_ : int , a_ : int , a_ : int )-> np.ndarray: '''simple docstring''' if (ksize % 2) == 0: SCREAMING_SNAKE_CASE : Tuple = ksize + 1 SCREAMING_SNAKE_CASE : Optional[Any] = np.zeros((ksize, ksize) , dtype=np.floataa ) # each value for y in range(UpperCamelCase__ ): for x in range(UpperCamelCase__ ): # distance from center SCREAMING_SNAKE_CASE : int = x - ksize // 2 SCREAMING_SNAKE_CASE : Optional[Any] = y - ksize // 2 # degree to radiant SCREAMING_SNAKE_CASE : Dict = theta / 1_80 * np.pi SCREAMING_SNAKE_CASE : Dict = np.cos(_theta ) SCREAMING_SNAKE_CASE : str = np.sin(_theta ) # get kernel x SCREAMING_SNAKE_CASE : List[str] = cos_theta * px + sin_theta * py # get kernel y SCREAMING_SNAKE_CASE : Tuple = -sin_theta * px + cos_theta * py # fill kernel SCREAMING_SNAKE_CASE : int = np.exp( -(_x**2 + gamma**2 * _y**2) / (2 * sigma**2) ) * np.cos(2 * np.pi * _x / lambd + psi ) return gabor if __name__ == "__main__": import doctest doctest.testmod() # read original image lowerCamelCase__ : int = imread("../image_data/lena.jpg") # turn image in gray scale value lowerCamelCase__ : int = cvtColor(img, COLOR_BGR2GRAY) # Apply multiple Kernel to detect edges lowerCamelCase__ : str = np.zeros(gray.shape[:2]) for theta in [0, 30, 60, 90, 120, 150]: lowerCamelCase__ : int = gabor_filter_kernel(10, 8, theta, 10, 0, 0) out += filteraD(gray, CV_8UC3, kernel_aa) lowerCamelCase__ : int = out / out.max() * 255 lowerCamelCase__ : Tuple = out.astype(np.uinta) imshow("Original", gray) imshow("Gabor filter with 20x20 mask and 6 directions", out) waitKey(0)
719
"""simple docstring""" from typing import TYPE_CHECKING from ....utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available lowerCamelCase__ : Tuple = { "configuration_mctct": ["MCTCT_PRETRAINED_CONFIG_ARCHIVE_MAP", "MCTCTConfig"], "feature_extraction_mctct": ["MCTCTFeatureExtractor"], "processing_mctct": ["MCTCTProcessor"], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCamelCase__ : Union[str, Any] = [ "MCTCT_PRETRAINED_MODEL_ARCHIVE_LIST", "MCTCTForCTC", "MCTCTModel", "MCTCTPreTrainedModel", ] if TYPE_CHECKING: from .configuration_mctct import MCTCT_PRETRAINED_CONFIG_ARCHIVE_MAP, MCTCTConfig from .feature_extraction_mctct import MCTCTFeatureExtractor from .processing_mctct import MCTCTProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_mctct import MCTCT_PRETRAINED_MODEL_ARCHIVE_LIST, MCTCTForCTC, MCTCTModel, MCTCTPreTrainedModel else: import sys lowerCamelCase__ : Tuple = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
18
0
"""simple docstring""" from collections.abc import Sequence def __A ( a_ : Sequence[int] | None = None )-> Union[str, Any]: '''simple docstring''' if nums is None or not nums: raise ValueError('''Input sequence should not be empty''' ) SCREAMING_SNAKE_CASE : Any = nums[0] for i in range(1 , len(_UpperCamelCase ) ): SCREAMING_SNAKE_CASE : str = nums[i] SCREAMING_SNAKE_CASE : Optional[int] = max(_UpperCamelCase , ans + num , _UpperCamelCase ) return ans if __name__ == "__main__": import doctest doctest.testmod() # Try on a sample input from the user lowerCamelCase__ : str = int(input("Enter number of elements : ").strip()) lowerCamelCase__ : int = list(map(int, input("\nEnter the numbers : ").strip().split()))[:n] print(max_subsequence_sum(array))
720
"""simple docstring""" import os from typing import Any, Callable, Dict, List, Optional, Tuple, Union import torch from torch import nn from ...models.controlnet import ControlNetModel, ControlNetOutput from ...models.modeling_utils import ModelMixin from ...utils import logging lowerCamelCase__ : List[Any] = logging.get_logger(__name__) class lowercase__( _UpperCAmelCase ): '''simple docstring''' def __init__( self :List[str] , lowerCamelCase_ :Union[List[ControlNetModel], Tuple[ControlNetModel]] ) -> Union[str, Any]: '''simple docstring''' super().__init__() SCREAMING_SNAKE_CASE : Optional[Any] = nn.ModuleList(lowerCamelCase_ ) def __lowerCAmelCase ( self :Any , lowerCamelCase_ :torch.FloatTensor , lowerCamelCase_ :Union[torch.Tensor, float, int] , lowerCamelCase_ :torch.Tensor , lowerCamelCase_ :List[torch.tensor] , lowerCamelCase_ :List[float] , lowerCamelCase_ :Optional[torch.Tensor] = None , lowerCamelCase_ :Optional[torch.Tensor] = None , lowerCamelCase_ :Optional[torch.Tensor] = None , lowerCamelCase_ :Optional[Dict[str, Any]] = None , lowerCamelCase_ :bool = False , lowerCamelCase_ :bool = True , ) -> Union[ControlNetOutput, Tuple]: '''simple docstring''' for i, (image, scale, controlnet) in enumerate(zip(lowerCamelCase_ , lowerCamelCase_ , self.nets ) ): SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : List[Any] = controlnet( lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , ) # merge samples if i == 0: SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : int = down_samples, mid_sample else: SCREAMING_SNAKE_CASE : Optional[int] = [ samples_prev + samples_curr for samples_prev, samples_curr in zip(lowerCamelCase_ , lowerCamelCase_ ) ] mid_block_res_sample += mid_sample return down_block_res_samples, mid_block_res_sample def __lowerCAmelCase ( self :Any , lowerCamelCase_ :Union[str, os.PathLike] , lowerCamelCase_ :bool = True , lowerCamelCase_ :Callable = None , lowerCamelCase_ :bool = False , lowerCamelCase_ :Optional[str] = None , ) -> Optional[int]: '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[Any] = 0 SCREAMING_SNAKE_CASE : Any = save_directory for controlnet in self.nets: controlnet.save_pretrained( lowerCamelCase_ , is_main_process=lowerCamelCase_ , save_function=lowerCamelCase_ , safe_serialization=lowerCamelCase_ , variant=lowerCamelCase_ , ) idx += 1 SCREAMING_SNAKE_CASE : Union[str, Any] = model_path_to_save + f"_{idx}" @classmethod def __lowerCAmelCase ( cls :Dict , lowerCamelCase_ :Optional[Union[str, os.PathLike]] , **lowerCamelCase_ :Tuple ) -> List[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : str = 0 SCREAMING_SNAKE_CASE : Optional[int] = [] # load controlnet and append to list until no controlnet directory exists anymore # first controlnet has to be saved under `./mydirectory/controlnet` to be compliant with `DiffusionPipeline.from_prertained` # second, third, ... controlnets have to be saved under `./mydirectory/controlnet_1`, `./mydirectory/controlnet_2`, ... SCREAMING_SNAKE_CASE : Dict = pretrained_model_path while os.path.isdir(lowerCamelCase_ ): SCREAMING_SNAKE_CASE : Tuple = ControlNetModel.from_pretrained(lowerCamelCase_ , **lowerCamelCase_ ) controlnets.append(lowerCamelCase_ ) idx += 1 SCREAMING_SNAKE_CASE : Union[str, Any] = pretrained_model_path + f"_{idx}" logger.info(f"{len(lowerCamelCase_ )} controlnets loaded from {pretrained_model_path}." ) if len(lowerCamelCase_ ) == 0: raise ValueError( f"No ControlNets found under {os.path.dirname(lowerCamelCase_ )}. Expected at least {pretrained_model_path + '_0'}." ) return cls(lowerCamelCase_ )
18
0
"""simple docstring""" from collections import OrderedDict from typing import Mapping from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging lowerCamelCase__ : Dict = logging.get_logger(__name__) lowerCamelCase__ : Dict = { "facebook/xlm-roberta-xl": "https://huggingface.co/facebook/xlm-roberta-xl/resolve/main/config.json", "facebook/xlm-roberta-xxl": "https://huggingface.co/facebook/xlm-roberta-xxl/resolve/main/config.json", # See all XLM-RoBERTa-XL models at https://huggingface.co/models?filter=xlm-roberta-xl } class lowercase__( __a ): '''simple docstring''' UpperCamelCase = """xlm-roberta-xl""" def __init__( self :List[Any] , lowerCamelCase_ :Optional[Any]=25_08_80 , lowerCamelCase_ :List[Any]=25_60 , lowerCamelCase_ :List[str]=36 , lowerCamelCase_ :Optional[Any]=32 , lowerCamelCase_ :int=1_02_40 , lowerCamelCase_ :Union[str, Any]="gelu" , lowerCamelCase_ :Optional[int]=0.1 , lowerCamelCase_ :Optional[Any]=0.1 , lowerCamelCase_ :str=5_14 , lowerCamelCase_ :Dict=1 , lowerCamelCase_ :str=0.0_2 , lowerCamelCase_ :int=1E-05 , lowerCamelCase_ :Dict=1 , lowerCamelCase_ :Optional[Any]=0 , lowerCamelCase_ :Optional[Any]=2 , lowerCamelCase_ :int="absolute" , lowerCamelCase_ :Dict=True , lowerCamelCase_ :Optional[int]=None , **lowerCamelCase_ :Tuple , ) -> List[Any]: '''simple docstring''' super().__init__(pad_token_id=A__ , bos_token_id=A__ , eos_token_id=A__ , **A__ ) SCREAMING_SNAKE_CASE : List[Any] = vocab_size SCREAMING_SNAKE_CASE : Any = hidden_size SCREAMING_SNAKE_CASE : Optional[int] = num_hidden_layers SCREAMING_SNAKE_CASE : Optional[int] = num_attention_heads SCREAMING_SNAKE_CASE : Union[str, Any] = hidden_act SCREAMING_SNAKE_CASE : int = intermediate_size SCREAMING_SNAKE_CASE : List[str] = hidden_dropout_prob SCREAMING_SNAKE_CASE : Any = attention_probs_dropout_prob SCREAMING_SNAKE_CASE : int = max_position_embeddings SCREAMING_SNAKE_CASE : str = type_vocab_size SCREAMING_SNAKE_CASE : List[Any] = initializer_range SCREAMING_SNAKE_CASE : Any = layer_norm_eps SCREAMING_SNAKE_CASE : int = position_embedding_type SCREAMING_SNAKE_CASE : Dict = use_cache SCREAMING_SNAKE_CASE : Any = classifier_dropout class lowercase__( __a ): '''simple docstring''' @property def __lowerCAmelCase ( self :int ) -> Mapping[str, Mapping[int, str]]: '''simple docstring''' if self.task == "multiple-choice": SCREAMING_SNAKE_CASE : List[Any] = {0: '''batch''', 1: '''choice''', 2: '''sequence'''} else: SCREAMING_SNAKE_CASE : Any = {0: '''batch''', 1: '''sequence'''} return OrderedDict( [ ('''input_ids''', dynamic_axis), ('''attention_mask''', dynamic_axis), ] )
721
"""simple docstring""" from __future__ import annotations from numpy import array, cos, cross, floataa, radians, sin from numpy.typing import NDArray def __A ( a_ : float , a_ : float , a_ : bool = False )-> list[float]: '''simple docstring''' if radian_mode: return [magnitude * cos(a_ ), magnitude * sin(a_ )] return [magnitude * cos(radians(a_ ) ), magnitude * sin(radians(a_ ) )] def __A ( a_ : NDArray[floataa] , a_ : NDArray[floataa] , a_ : float = 10**-1 )-> bool: '''simple docstring''' SCREAMING_SNAKE_CASE : NDArray[floataa] = cross(a_ , a_ ) SCREAMING_SNAKE_CASE : float = sum(a_ ) return abs(a_ ) < eps if __name__ == "__main__": # Test to check if it works lowerCamelCase__ : Optional[Any] = array( [ polar_force(7_1_8.4, 180 - 30), polar_force(8_7_9.5_4, 45), polar_force(100, -90), ] ) lowerCamelCase__ : NDArray[floataa] = array([[0, 0], [0, 0], [0, 0]]) assert in_static_equilibrium(forces, location) # Problem 1 in image_data/2D_problems.jpg lowerCamelCase__ : Union[str, Any] = array( [ polar_force(30 * 9.8_1, 15), polar_force(215, 180 - 45), polar_force(264, 90 - 30), ] ) lowerCamelCase__ : Any = array([[0, 0], [0, 0], [0, 0]]) assert in_static_equilibrium(forces, location) # Problem in image_data/2D_problems_1.jpg lowerCamelCase__ : Union[str, Any] = array([[0, -2000], [0, -1200], [0, 15600], [0, -12400]]) lowerCamelCase__ : Optional[int] = array([[0, 0], [6, 0], [10, 0], [12, 0]]) assert in_static_equilibrium(forces, location) import doctest doctest.testmod()
18
0
"""simple docstring""" from sympy import diff, lambdify, symbols from sympy.functions import * # noqa: F403 def __A ( a_ : Union[str, Any] , a_ : Any , a_ : Optional[int] = "x" , a_ : List[Any] = 10**-10 , a_ : Optional[int] = 1 , )-> complex: '''simple docstring''' SCREAMING_SNAKE_CASE : int = symbols(_SCREAMING_SNAKE_CASE ) SCREAMING_SNAKE_CASE : List[str] = lambdify(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) SCREAMING_SNAKE_CASE : Tuple = lambdify(_SCREAMING_SNAKE_CASE , diff(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) ) SCREAMING_SNAKE_CASE : Tuple = starting_point while True: if diff_function(_SCREAMING_SNAKE_CASE ) != 0: SCREAMING_SNAKE_CASE : Optional[Any] = prev_guess - multiplicity * func(_SCREAMING_SNAKE_CASE ) / diff_function( _SCREAMING_SNAKE_CASE ) else: raise ZeroDivisionError('''Could not find root''' ) from None # Precision is checked by comparing the difference of consecutive guesses if abs(next_guess - prev_guess ) < precision: return next_guess SCREAMING_SNAKE_CASE : Optional[int] = next_guess # Let's Execute if __name__ == "__main__": # Find root of trigonometric function # Find value of pi print(f'''The root of sin(x) = 0 is {newton_raphson("sin(x)", 2)}''') # Find root of polynomial # Find fourth Root of 5 print(f'''The root of x**4 - 5 = 0 is {newton_raphson("x**4 -5", 0.4 +5J)}''') # Find value of e print( "The root of log(y) - 1 = 0 is ", f'''{newton_raphson("log(y) - 1", 2, variable="y")}''', ) # Exponential Roots print( "The root of exp(x) - 1 = 0 is", f'''{newton_raphson("exp(x) - 1", 10, precision=0.0_0_5)}''', ) # Find root of cos(x) print(f'''The root of cos(x) = 0 is {newton_raphson("cos(x)", 0)}''')
700
"""simple docstring""" from __future__ import annotations import random # Maximum size of the population. Bigger could be faster but is more memory expensive. lowerCamelCase__ : Optional[Any] = 200 # Number of elements selected in every generation of evolution. The selection takes # place from best to worst of that generation and must be smaller than N_POPULATION. lowerCamelCase__ : Optional[int] = 50 # Probability that an element of a generation can mutate, changing one of its genes. # This will guarantee that all genes will be used during evolution. lowerCamelCase__ : Optional[Any] = 0.4 # Just a seed to improve randomness required by the algorithm. random.seed(random.randint(0, 1000)) def __A ( a_ : str , a_ : str )-> tuple[str, float]: '''simple docstring''' SCREAMING_SNAKE_CASE : Tuple = len([g for position, g in enumerate(a_ ) if g == main_target[position]] ) return (item, float(a_ )) def __A ( a_ : str , a_ : str )-> tuple[str, str]: '''simple docstring''' SCREAMING_SNAKE_CASE : Tuple = random.randint(0 , len(a_ ) - 1 ) SCREAMING_SNAKE_CASE : str = parent_a[:random_slice] + parent_a[random_slice:] SCREAMING_SNAKE_CASE : Dict = parent_a[:random_slice] + parent_a[random_slice:] return (child_a, child_a) def __A ( a_ : str , a_ : list[str] )-> str: '''simple docstring''' SCREAMING_SNAKE_CASE : Any = list(a_ ) if random.uniform(0 , 1 ) < MUTATION_PROBABILITY: SCREAMING_SNAKE_CASE : Any = random.choice(a_ ) return "".join(a_ ) def __A ( a_ : tuple[str, float] , a_ : list[tuple[str, float]] , a_ : list[str] , )-> list[str]: '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[int] = [] # Generate more children proportionally to the fitness score. SCREAMING_SNAKE_CASE : List[str] = int(parent_a[1] * 1_00 ) + 1 SCREAMING_SNAKE_CASE : Optional[Any] = 10 if child_n >= 10 else child_n for _ in range(a_ ): SCREAMING_SNAKE_CASE : List[str] = population_score[random.randint(0 , a_ )][0] SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : Tuple = crossover(parent_a[0] , a_ ) # Append new string to the population list. pop.append(mutate(a_ , a_ ) ) pop.append(mutate(a_ , a_ ) ) return pop def __A ( a_ : str , a_ : list[str] , a_ : bool = True )-> tuple[int, int, str]: '''simple docstring''' if N_POPULATION < N_SELECTED: SCREAMING_SNAKE_CASE : List[Any] = F"{N_POPULATION} must be bigger than {N_SELECTED}" raise ValueError(a_ ) # Verify that the target contains no genes besides the ones inside genes variable. SCREAMING_SNAKE_CASE : List[str] = sorted({c for c in target if c not in genes} ) if not_in_genes_list: SCREAMING_SNAKE_CASE : str = F"{not_in_genes_list} is not in genes list, evolution cannot converge" raise ValueError(a_ ) # Generate random starting population. SCREAMING_SNAKE_CASE : Tuple = [] for _ in range(a_ ): population.append(''''''.join([random.choice(a_ ) for i in range(len(a_ ) )] ) ) # Just some logs to know what the algorithms is doing. SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : List[str] = 0, 0 # This loop will end when we find a perfect match for our target. while True: generation += 1 total_population += len(a_ ) # Random population created. Now it's time to evaluate. # Adding a bit of concurrency can make everything faster, # # import concurrent.futures # population_score: list[tuple[str, float]] = [] # with concurrent.futures.ThreadPoolExecutor( # max_workers=NUM_WORKERS) as executor: # futures = {executor.submit(evaluate, item) for item in population} # concurrent.futures.wait(futures) # population_score = [item.result() for item in futures] # # but with a simple algorithm like this, it will probably be slower. # We just need to call evaluate for every item inside the population. SCREAMING_SNAKE_CASE : int = [evaluate(a_ , a_ ) for item in population] # Check if there is a matching evolution. SCREAMING_SNAKE_CASE : List[Any] = sorted(a_ , key=lambda a_ : x[1] , reverse=a_ ) if population_score[0][0] == target: return (generation, total_population, population_score[0][0]) # Print the best result every 10 generation. # Just to know that the algorithm is working. if debug and generation % 10 == 0: print( F"\nGeneration: {generation}" F"\nTotal Population:{total_population}" F"\nBest score: {population_score[0][1]}" F"\nBest string: {population_score[0][0]}" ) # Flush the old population, keeping some of the best evolutions. # Keeping this avoid regression of evolution. SCREAMING_SNAKE_CASE : Optional[Any] = population[: int(N_POPULATION / 3 )] population.clear() population.extend(a_ ) # Normalize population score to be between 0 and 1. SCREAMING_SNAKE_CASE : Optional[int] = [ (item, score / len(a_ )) for item, score in population_score ] # This is selection for i in range(a_ ): population.extend(select(population_score[int(a_ )] , a_ , a_ ) ) # Check if the population has already reached the maximum value and if so, # break the cycle. If this check is disabled, the algorithm will take # forever to compute large strings, but will also calculate small strings in # a far fewer generations. if len(a_ ) > N_POPULATION: break if __name__ == "__main__": lowerCamelCase__ : Dict = ( "This is a genetic algorithm to evaluate, combine, evolve, and mutate a string!" ) lowerCamelCase__ : int = list( " ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklm" "nopqrstuvwxyz.,;!?+-*#@^'èéòà€ù=)(&%$£/\\" ) lowerCamelCase__ , lowerCamelCase__ , lowerCamelCase__ : Dict = basic(target_str, genes_list) print( f'''\nGeneration: {generation}\nTotal Population: {population}\nTarget: {target}''' )
18
0
"""simple docstring""" import os import tempfile import unittest from transformers.models.marian.convert_marian_tatoeba_to_pytorch import DEFAULT_REPO, TatoebaConverter from transformers.testing_utils import slow from transformers.utils import cached_property @unittest.skipUnless(os.path.exists(__snake_case ) , """Tatoeba directory does not exist.""" ) class lowercase__( unittest.TestCase ): '''simple docstring''' @cached_property def __lowerCAmelCase ( self :List[str] ) -> List[str]: '''simple docstring''' SCREAMING_SNAKE_CASE : List[Any] = tempfile.mkdtemp() return TatoebaConverter(save_dir=__UpperCamelCase ) @slow def __lowerCAmelCase ( self :List[str] ) -> List[Any]: '''simple docstring''' self.resolver.convert_models(['''heb-eng'''] ) @slow def __lowerCAmelCase ( self :int ) -> Union[str, Any]: '''simple docstring''' SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : Dict = self.resolver.write_model_card('''opus-mt-he-en''' , dry_run=__UpperCamelCase ) assert mmeta["long_pair"] == "heb-eng"
701
"""simple docstring""" import argparse import ast import logging import os import sys import pandas as pd import torch from tqdm import tqdm from transformers import BartForConditionalGeneration, RagRetriever, RagSequenceForGeneration, RagTokenForGeneration from transformers import logging as transformers_logging sys.path.append(os.path.join(os.getcwd())) # noqa: E402 # isort:skip from utils_rag import exact_match_score, fa_score # noqa: E402 # isort:skip lowerCamelCase__ : Optional[Any] = logging.getLogger(__name__) logging.basicConfig(level=logging.INFO) transformers_logging.set_verbosity_info() def __A ( a_ : Optional[int] )-> Dict: '''simple docstring''' if "token" in model_name_or_path: return "rag_token" if "sequence" in model_name_or_path: return "rag_sequence" if "bart" in model_name_or_path: return "bart" return None def __A ( a_ : List[Any] , a_ : Optional[int] , a_ : Optional[int] )-> Dict: '''simple docstring''' return max(metric_fn(a_ , a_ ) for gt in ground_truths ) def __A ( a_ : List[Any] , a_ : Union[str, Any] , a_ : str )-> str: '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[int] = [line.strip() for line in open(a_ , '''r''' ).readlines()] SCREAMING_SNAKE_CASE : Optional[Any] = [] if args.gold_data_mode == "qa": SCREAMING_SNAKE_CASE : List[Any] = pd.read_csv(a_ , sep='''\t''' , header=a_ ) for answer_list in data[1]: SCREAMING_SNAKE_CASE : str = ast.literal_eval(a_ ) answers.append(a_ ) else: SCREAMING_SNAKE_CASE : Any = [line.strip() for line in open(a_ , '''r''' ).readlines()] SCREAMING_SNAKE_CASE : Dict = [[reference] for reference in references] SCREAMING_SNAKE_CASE : Dict = 0 for prediction, ground_truths in zip(a_ , a_ ): total += 1 em += metric_max_over_ground_truths(a_ , a_ , a_ ) fa += metric_max_over_ground_truths(a_ , a_ , a_ ) SCREAMING_SNAKE_CASE : Any = 100.0 * em / total SCREAMING_SNAKE_CASE : Optional[int] = 100.0 * fa / total logger.info(F"F1: {fa:.2f}" ) logger.info(F"EM: {em:.2f}" ) def __A ( a_ : Any , a_ : Any , a_ : List[Any] )-> Tuple: '''simple docstring''' SCREAMING_SNAKE_CASE : str = args.k SCREAMING_SNAKE_CASE : Tuple = [line.strip() for line in open(a_ , '''r''' ).readlines()] SCREAMING_SNAKE_CASE : Union[str, Any] = [line.strip() for line in open(a_ , '''r''' ).readlines()] SCREAMING_SNAKE_CASE : Dict = 0 for hypo, reference in zip(a_ , a_ ): SCREAMING_SNAKE_CASE : Optional[int] = set(hypo.split('''\t''' )[:k] ) SCREAMING_SNAKE_CASE : List[str] = set(reference.split('''\t''' ) ) total += 1 em += len(hypo_provenance & ref_provenance ) / k SCREAMING_SNAKE_CASE : Dict = 100.0 * em / total logger.info(F"Precision@{k}: {em: .2f}" ) def __A ( a_ : Any , a_ : List[str] , a_ : str )-> int: '''simple docstring''' def strip_title(a_ : Optional[Any] ): if title.startswith('''"''' ): SCREAMING_SNAKE_CASE : Tuple = title[1:] if title.endswith('''"''' ): SCREAMING_SNAKE_CASE : Any = title[:-1] return title SCREAMING_SNAKE_CASE : Tuple = rag_model.retriever.question_encoder_tokenizer.batch_encode_plus( a_ , return_tensors='''pt''' , padding=a_ , truncation=a_ , )['''input_ids'''].to(args.device ) SCREAMING_SNAKE_CASE : Any = rag_model.rag.question_encoder(a_ ) SCREAMING_SNAKE_CASE : Union[str, Any] = question_enc_outputs[0] SCREAMING_SNAKE_CASE : Dict = rag_model.retriever( a_ , question_enc_pool_output.cpu().detach().to(torch.floataa ).numpy() , prefix=rag_model.rag.generator.config.prefix , n_docs=rag_model.config.n_docs , return_tensors='''pt''' , ) SCREAMING_SNAKE_CASE : Any = rag_model.retriever.index.get_doc_dicts(result.doc_ids ) SCREAMING_SNAKE_CASE : Dict = [] for docs in all_docs: SCREAMING_SNAKE_CASE : List[Any] = [strip_title(a_ ) for title in docs['''title''']] provenance_strings.append('''\t'''.join(a_ ) ) return provenance_strings def __A ( a_ : List[Any] , a_ : int , a_ : str )-> Tuple: '''simple docstring''' with torch.no_grad(): SCREAMING_SNAKE_CASE : Optional[int] = rag_model.retriever.question_encoder_tokenizer.batch_encode_plus( a_ , return_tensors='''pt''' , padding=a_ , truncation=a_ ) SCREAMING_SNAKE_CASE : Dict = inputs_dict.input_ids.to(args.device ) SCREAMING_SNAKE_CASE : Any = inputs_dict.attention_mask.to(args.device ) SCREAMING_SNAKE_CASE : Tuple = rag_model.generate( # rag_model overwrites generate a_ , attention_mask=a_ , num_beams=args.num_beams , min_length=args.min_length , max_length=args.max_length , early_stopping=a_ , num_return_sequences=1 , bad_words_ids=[[0, 0]] , ) SCREAMING_SNAKE_CASE : Dict = rag_model.retriever.generator_tokenizer.batch_decode(a_ , skip_special_tokens=a_ ) if args.print_predictions: for q, a in zip(a_ , a_ ): logger.info('''Q: {} - A: {}'''.format(a_ , a_ ) ) return answers def __A ( )-> int: '''simple docstring''' SCREAMING_SNAKE_CASE : int = argparse.ArgumentParser() parser.add_argument( '''--model_type''' , choices=['''rag_sequence''', '''rag_token''', '''bart'''] , type=a_ , help=( '''RAG model type: rag_sequence, rag_token or bart, if none specified, the type is inferred from the''' ''' model_name_or_path''' ) , ) parser.add_argument( '''--index_name''' , default=a_ , choices=['''exact''', '''compressed''', '''legacy'''] , type=a_ , help='''RAG model retriever type''' , ) parser.add_argument( '''--index_path''' , default=a_ , type=a_ , help='''Path to the retrieval index''' , ) parser.add_argument('''--n_docs''' , default=5 , type=a_ , help='''Number of retrieved docs''' ) parser.add_argument( '''--model_name_or_path''' , default=a_ , type=a_ , required=a_ , help='''Path to pretrained checkpoints or model identifier from huggingface.co/models''' , ) parser.add_argument( '''--eval_mode''' , choices=['''e2e''', '''retrieval'''] , default='''e2e''' , type=a_ , help=( '''Evaluation mode, e2e calculates exact match and F1 of the downstream task, retrieval calculates''' ''' precision@k.''' ) , ) parser.add_argument('''--k''' , default=1 , type=a_ , help='''k for the precision@k calculation''' ) parser.add_argument( '''--evaluation_set''' , default=a_ , type=a_ , required=a_ , help='''Path to a file containing evaluation samples''' , ) parser.add_argument( '''--gold_data_path''' , default=a_ , type=a_ , required=a_ , help='''Path to a tab-separated file with gold samples''' , ) parser.add_argument( '''--gold_data_mode''' , default='''qa''' , type=a_ , choices=['''qa''', '''ans'''] , help=( '''Format of the gold data file''' '''qa - a single line in the following format: question [tab] answer_list''' '''ans - a single line of the gold file contains the expected answer string''' ) , ) parser.add_argument( '''--predictions_path''' , type=a_ , default='''predictions.txt''' , help='''Name of the predictions file, to be stored in the checkpoints directory''' , ) parser.add_argument( '''--eval_all_checkpoints''' , action='''store_true''' , help='''Evaluate all checkpoints starting with the same prefix as model_name ending and ending with step number''' , ) parser.add_argument( '''--eval_batch_size''' , default=8 , type=a_ , help='''Batch size per GPU/CPU for evaluation.''' , ) parser.add_argument( '''--recalculate''' , help='''Recalculate predictions even if the prediction file exists''' , action='''store_true''' , ) parser.add_argument( '''--num_beams''' , default=4 , type=a_ , help='''Number of beams to be used when generating answers''' , ) parser.add_argument('''--min_length''' , default=1 , type=a_ , help='''Min length of the generated answers''' ) parser.add_argument('''--max_length''' , default=50 , type=a_ , help='''Max length of the generated answers''' ) parser.add_argument( '''--print_predictions''' , action='''store_true''' , help='''If True, prints predictions while evaluating.''' , ) parser.add_argument( '''--print_docs''' , action='''store_true''' , help='''If True, prints docs retried while generating.''' , ) SCREAMING_SNAKE_CASE : List[str] = parser.parse_args() SCREAMING_SNAKE_CASE : Dict = torch.device('''cuda''' if torch.cuda.is_available() else '''cpu''' ) return args def __A ( a_ : Optional[Any] )-> int: '''simple docstring''' SCREAMING_SNAKE_CASE : Any = {} if args.model_type is None: SCREAMING_SNAKE_CASE : List[str] = infer_model_type(args.model_name_or_path ) assert args.model_type is not None if args.model_type.startswith('''rag''' ): SCREAMING_SNAKE_CASE : List[str] = RagTokenForGeneration if args.model_type == '''rag_token''' else RagSequenceForGeneration SCREAMING_SNAKE_CASE : Optional[Any] = args.n_docs if args.index_name is not None: SCREAMING_SNAKE_CASE : Tuple = args.index_name if args.index_path is not None: SCREAMING_SNAKE_CASE : List[Any] = args.index_path else: SCREAMING_SNAKE_CASE : str = BartForConditionalGeneration SCREAMING_SNAKE_CASE : Optional[int] = ( [f.path for f in os.scandir(args.model_name_or_path ) if f.is_dir()] if args.eval_all_checkpoints else [args.model_name_or_path] ) logger.info('''Evaluate the following checkpoints: %s''' , a_ ) SCREAMING_SNAKE_CASE : int = get_scores if args.eval_mode == '''e2e''' else get_precision_at_k SCREAMING_SNAKE_CASE : str = evaluate_batch_eae if args.eval_mode == '''e2e''' else evaluate_batch_retrieval for checkpoint in checkpoints: if os.path.exists(args.predictions_path ) and (not args.recalculate): logger.info('''Calculating metrics based on an existing predictions file: {}'''.format(args.predictions_path ) ) score_fn(a_ , args.predictions_path , args.gold_data_path ) continue logger.info('''***** Running evaluation for {} *****'''.format(a_ ) ) logger.info(''' Batch size = %d''' , args.eval_batch_size ) logger.info(''' Predictions will be stored under {}'''.format(args.predictions_path ) ) if args.model_type.startswith('''rag''' ): SCREAMING_SNAKE_CASE : Dict = RagRetriever.from_pretrained(a_ , **a_ ) SCREAMING_SNAKE_CASE : Union[str, Any] = model_class.from_pretrained(a_ , retriever=a_ , **a_ ) model.retriever.init_retrieval() else: SCREAMING_SNAKE_CASE : str = model_class.from_pretrained(a_ , **a_ ) model.to(args.device ) with open(args.evaluation_set , '''r''' ) as eval_file, open(args.predictions_path , '''w''' ) as preds_file: SCREAMING_SNAKE_CASE : Dict = [] for line in tqdm(a_ ): questions.append(line.strip() ) if len(a_ ) == args.eval_batch_size: SCREAMING_SNAKE_CASE : str = evaluate_batch_fn(a_ , a_ , a_ ) preds_file.write('''\n'''.join(a_ ) + '''\n''' ) preds_file.flush() SCREAMING_SNAKE_CASE : Union[str, Any] = [] if len(a_ ) > 0: SCREAMING_SNAKE_CASE : str = evaluate_batch_fn(a_ , a_ , a_ ) preds_file.write('''\n'''.join(a_ ) ) preds_file.flush() score_fn(a_ , args.predictions_path , args.gold_data_path ) if __name__ == "__main__": lowerCamelCase__ : List[str] = get_args() main(args)
18
0
"""simple docstring""" import argparse import gc import json import os import shutil import warnings import torch from transformers import LlamaConfig, LlamaForCausalLM, LlamaTokenizer try: from transformers import LlamaTokenizerFast except ImportError as e: warnings.warn(e) warnings.warn( "The converted tokenizer will be the `slow` tokenizer. To use the fast, update your `tokenizers` library and re-run the tokenizer conversion" ) lowerCamelCase__ : Any = None lowerCamelCase__ : List[str] = { '7B': 11008, '13B': 13824, '30B': 17920, '65B': 22016, '70B': 28672, } lowerCamelCase__ : Tuple = { '7B': 1, '7Bf': 1, '13B': 2, '13Bf': 2, '30B': 4, '65B': 8, '70B': 8, '70Bf': 8, } def __A ( a_ : List[str] , a_ : Optional[Any]=1 , a_ : List[str]=2_56 )-> str: '''simple docstring''' return multiple_of * ((int(ffn_dim_multiplier * int(8 * n / 3 ) ) + multiple_of - 1) // multiple_of) def __A ( a_ : Optional[int] )-> Any: '''simple docstring''' with open(lowerCAmelCase_ , '''r''' ) as f: return json.load(lowerCAmelCase_ ) def __A ( a_ : Union[str, Any] , a_ : Union[str, Any] )-> List[Any]: '''simple docstring''' with open(lowerCAmelCase_ , '''w''' ) as f: json.dump(lowerCAmelCase_ , lowerCAmelCase_ ) def __A ( a_ : Tuple , a_ : int , a_ : Tuple , a_ : Optional[int]=True )-> List[str]: '''simple docstring''' os.makedirs(lowerCAmelCase_ , exist_ok=lowerCAmelCase_ ) SCREAMING_SNAKE_CASE : Tuple = os.path.join(lowerCAmelCase_ , '''tmp''' ) os.makedirs(lowerCAmelCase_ , exist_ok=lowerCAmelCase_ ) SCREAMING_SNAKE_CASE : Optional[Any] = read_json(os.path.join(lowerCAmelCase_ , '''params.json''' ) ) SCREAMING_SNAKE_CASE : Optional[int] = NUM_SHARDS[model_size] SCREAMING_SNAKE_CASE : Tuple = params['''n_layers'''] SCREAMING_SNAKE_CASE : str = params['''n_heads'''] SCREAMING_SNAKE_CASE : Optional[Any] = n_heads // num_shards SCREAMING_SNAKE_CASE : Any = params['''dim'''] SCREAMING_SNAKE_CASE : Any = dim // n_heads SCREAMING_SNAKE_CASE : Union[str, Any] = 1_00_00.0 SCREAMING_SNAKE_CASE : int = 1.0 / (base ** (torch.arange(0 , lowerCAmelCase_ , 2 ).float() / dims_per_head)) if "n_kv_heads" in params: SCREAMING_SNAKE_CASE : str = params['''n_kv_heads'''] # for GQA / MQA SCREAMING_SNAKE_CASE : Optional[int] = n_heads_per_shard // num_key_value_heads SCREAMING_SNAKE_CASE : Optional[Any] = dim // num_key_value_heads else: # compatibility with other checkpoints SCREAMING_SNAKE_CASE : Any = n_heads SCREAMING_SNAKE_CASE : List[Any] = n_heads_per_shard SCREAMING_SNAKE_CASE : List[Any] = dim # permute for sliced rotary def permute(a_ : Tuple , a_ : Optional[int]=n_heads , a_ : List[str]=dim , a_ : List[Any]=dim ): return w.view(lowerCAmelCase_ , dima // n_heads // 2 , 2 , lowerCAmelCase_ ).transpose(1 , 2 ).reshape(lowerCAmelCase_ , lowerCAmelCase_ ) print(F"Fetching all parameters from the checkpoint at {input_base_path}." ) # Load weights if model_size == "7B": # Not sharded # (The sharded implementation would also work, but this is simpler.) SCREAMING_SNAKE_CASE : List[str] = torch.load(os.path.join(lowerCAmelCase_ , '''consolidated.00.pth''' ) , map_location='''cpu''' ) else: # Sharded SCREAMING_SNAKE_CASE : Any = [ torch.load(os.path.join(lowerCAmelCase_ , F"consolidated.{i:02d}.pth" ) , map_location='''cpu''' ) for i in range(lowerCAmelCase_ ) ] SCREAMING_SNAKE_CASE : Dict = 0 SCREAMING_SNAKE_CASE : Optional[Any] = {'''weight_map''': {}} for layer_i in range(lowerCAmelCase_ ): SCREAMING_SNAKE_CASE : int = F"pytorch_model-{layer_i + 1}-of-{n_layers + 1}.bin" if model_size == "7B": # Unsharded SCREAMING_SNAKE_CASE : List[str] = { F"model.layers.{layer_i}.self_attn.q_proj.weight": permute( loaded[F"layers.{layer_i}.attention.wq.weight"] ), F"model.layers.{layer_i}.self_attn.k_proj.weight": permute( loaded[F"layers.{layer_i}.attention.wk.weight"] ), F"model.layers.{layer_i}.self_attn.v_proj.weight": loaded[F"layers.{layer_i}.attention.wv.weight"], F"model.layers.{layer_i}.self_attn.o_proj.weight": loaded[F"layers.{layer_i}.attention.wo.weight"], F"model.layers.{layer_i}.mlp.gate_proj.weight": loaded[F"layers.{layer_i}.feed_forward.w1.weight"], F"model.layers.{layer_i}.mlp.down_proj.weight": loaded[F"layers.{layer_i}.feed_forward.w2.weight"], F"model.layers.{layer_i}.mlp.up_proj.weight": loaded[F"layers.{layer_i}.feed_forward.w3.weight"], F"model.layers.{layer_i}.input_layernorm.weight": loaded[F"layers.{layer_i}.attention_norm.weight"], F"model.layers.{layer_i}.post_attention_layernorm.weight": loaded[F"layers.{layer_i}.ffn_norm.weight"], } else: # Sharded # Note that attention.w{q,k,v,o}, feed_fordward.w[1,2,3], attention_norm.weight and ffn_norm.weight share # the same storage object, saving attention_norm and ffn_norm will save other weights too, which is # redundant as other weights will be stitched from multiple shards. To avoid that, they are cloned. SCREAMING_SNAKE_CASE : str = { F"model.layers.{layer_i}.input_layernorm.weight": loaded[0][ F"layers.{layer_i}.attention_norm.weight" ].clone(), F"model.layers.{layer_i}.post_attention_layernorm.weight": loaded[0][ F"layers.{layer_i}.ffn_norm.weight" ].clone(), } SCREAMING_SNAKE_CASE : int = permute( torch.cat( [ loaded[i][F"layers.{layer_i}.attention.wq.weight"].view(lowerCAmelCase_ , lowerCAmelCase_ , lowerCAmelCase_ ) for i in range(lowerCAmelCase_ ) ] , dim=0 , ).reshape(lowerCAmelCase_ , lowerCAmelCase_ ) ) SCREAMING_SNAKE_CASE : Any = permute( torch.cat( [ loaded[i][F"layers.{layer_i}.attention.wk.weight"].view( lowerCAmelCase_ , lowerCAmelCase_ , lowerCAmelCase_ ) for i in range(lowerCAmelCase_ ) ] , dim=0 , ).reshape(lowerCAmelCase_ , lowerCAmelCase_ ) , lowerCAmelCase_ , lowerCAmelCase_ , lowerCAmelCase_ , ) SCREAMING_SNAKE_CASE : str = torch.cat( [ loaded[i][F"layers.{layer_i}.attention.wv.weight"].view( lowerCAmelCase_ , lowerCAmelCase_ , lowerCAmelCase_ ) for i in range(lowerCAmelCase_ ) ] , dim=0 , ).reshape(lowerCAmelCase_ , lowerCAmelCase_ ) SCREAMING_SNAKE_CASE : Union[str, Any] = torch.cat( [loaded[i][F"layers.{layer_i}.attention.wo.weight"] for i in range(lowerCAmelCase_ )] , dim=1 ) SCREAMING_SNAKE_CASE : Optional[Any] = torch.cat( [loaded[i][F"layers.{layer_i}.feed_forward.w1.weight"] for i in range(lowerCAmelCase_ )] , dim=0 ) SCREAMING_SNAKE_CASE : List[str] = torch.cat( [loaded[i][F"layers.{layer_i}.feed_forward.w2.weight"] for i in range(lowerCAmelCase_ )] , dim=1 ) SCREAMING_SNAKE_CASE : int = torch.cat( [loaded[i][F"layers.{layer_i}.feed_forward.w3.weight"] for i in range(lowerCAmelCase_ )] , dim=0 ) SCREAMING_SNAKE_CASE : Dict = inv_freq for k, v in state_dict.items(): SCREAMING_SNAKE_CASE : List[str] = filename param_count += v.numel() torch.save(lowerCAmelCase_ , os.path.join(lowerCAmelCase_ , lowerCAmelCase_ ) ) SCREAMING_SNAKE_CASE : Tuple = F"pytorch_model-{n_layers + 1}-of-{n_layers + 1}.bin" if model_size == "7B": # Unsharded SCREAMING_SNAKE_CASE : Optional[int] = { '''model.embed_tokens.weight''': loaded['''tok_embeddings.weight'''], '''model.norm.weight''': loaded['''norm.weight'''], '''lm_head.weight''': loaded['''output.weight'''], } else: SCREAMING_SNAKE_CASE : Any = { '''model.norm.weight''': loaded[0]['''norm.weight'''], '''model.embed_tokens.weight''': torch.cat( [loaded[i]['''tok_embeddings.weight'''] for i in range(lowerCAmelCase_ )] , dim=1 ), '''lm_head.weight''': torch.cat([loaded[i]['''output.weight'''] for i in range(lowerCAmelCase_ )] , dim=0 ), } for k, v in state_dict.items(): SCREAMING_SNAKE_CASE : List[Any] = filename param_count += v.numel() torch.save(lowerCAmelCase_ , os.path.join(lowerCAmelCase_ , lowerCAmelCase_ ) ) # Write configs SCREAMING_SNAKE_CASE : Optional[Any] = {'''total_size''': param_count * 2} write_json(lowerCAmelCase_ , os.path.join(lowerCAmelCase_ , '''pytorch_model.bin.index.json''' ) ) SCREAMING_SNAKE_CASE : Optional[int] = params['''ffn_dim_multiplier'''] if '''ffn_dim_multiplier''' in params else 1 SCREAMING_SNAKE_CASE : str = params['''multiple_of'''] if '''multiple_of''' in params else 2_56 SCREAMING_SNAKE_CASE : Optional[int] = LlamaConfig( hidden_size=lowerCAmelCase_ , intermediate_size=compute_intermediate_size(lowerCAmelCase_ , lowerCAmelCase_ , lowerCAmelCase_ ) , num_attention_heads=params['''n_heads'''] , num_hidden_layers=params['''n_layers'''] , rms_norm_eps=params['''norm_eps'''] , num_key_value_heads=lowerCAmelCase_ , ) config.save_pretrained(lowerCAmelCase_ ) # Make space so we can load the model properly now. del state_dict del loaded gc.collect() print('''Loading the checkpoint in a Llama model.''' ) SCREAMING_SNAKE_CASE : Optional[int] = LlamaForCausalLM.from_pretrained(lowerCAmelCase_ , torch_dtype=torch.floataa , low_cpu_mem_usage=lowerCAmelCase_ ) # Avoid saving this as part of the config. del model.config._name_or_path print('''Saving in the Transformers format.''' ) model.save_pretrained(lowerCAmelCase_ , safe_serialization=lowerCAmelCase_ ) shutil.rmtree(lowerCAmelCase_ ) def __A ( a_ : Union[str, Any] , a_ : Union[str, Any] )-> Tuple: '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[int] = LlamaTokenizer if LlamaTokenizerFast is None else LlamaTokenizerFast print(F"Saving a {tokenizer_class.__name__} to {tokenizer_path}." ) SCREAMING_SNAKE_CASE : Optional[int] = tokenizer_class(lowerCAmelCase_ ) tokenizer.save_pretrained(lowerCAmelCase_ ) def __A ( )-> Any: '''simple docstring''' SCREAMING_SNAKE_CASE : List[Any] = argparse.ArgumentParser() parser.add_argument( '''--input_dir''' , help='''Location of LLaMA weights, which contains tokenizer.model and model folders''' , ) parser.add_argument( '''--model_size''' , choices=['''7B''', '''7Bf''', '''13B''', '''13Bf''', '''30B''', '''65B''', '''70B''', '''70Bf''', '''tokenizer_only'''] , ) parser.add_argument( '''--output_dir''' , help='''Location to write HF model and tokenizer''' , ) parser.add_argument('''--safe_serialization''' , type=lowerCAmelCase_ , help='''Whether or not to save using `safetensors`.''' ) SCREAMING_SNAKE_CASE : int = parser.parse_args() if args.model_size != "tokenizer_only": write_model( model_path=args.output_dir , input_base_path=os.path.join(args.input_dir , args.model_size ) , model_size=args.model_size , safe_serialization=args.safe_serialization , ) SCREAMING_SNAKE_CASE : List[str] = os.path.join(args.input_dir , '''tokenizer.model''' ) write_tokenizer(args.output_dir , lowerCAmelCase_ ) if __name__ == "__main__": main()
702
"""simple docstring""" import json import os from typing import Optional, Tuple from ...tokenization_utils import PreTrainedTokenizer from ...utils import logging lowerCamelCase__ : List[str] = logging.get_logger(__name__) lowerCamelCase__ : Optional[int] = {"vocab_file": "vocab.json"} lowerCamelCase__ : Dict = { "vocab_file": { "mgp-str": "https://huggingface.co/alibaba-damo/mgp-str-base/blob/main/vocab.json", } } lowerCamelCase__ : Optional[Any] = {"mgp-str": 27} class lowercase__( _UpperCAmelCase ): '''simple docstring''' UpperCamelCase = VOCAB_FILES_NAMES UpperCamelCase = PRETRAINED_VOCAB_FILES_MAP UpperCamelCase = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES def __init__( self :int , lowerCamelCase_ :Optional[int] , lowerCamelCase_ :Optional[int]="[GO]" , lowerCamelCase_ :int="[GO]" , lowerCamelCase_ :str="[s]" , lowerCamelCase_ :Dict="[GO]" , **lowerCamelCase_ :List[str] ) -> Tuple: '''simple docstring''' super().__init__( unk_token=lowerCamelCase_ , bos_token=lowerCamelCase_ , eos_token=lowerCamelCase_ , pad_token=lowerCamelCase_ , **lowerCamelCase_ , ) with open(lowerCamelCase_ , encoding='''utf-8''' ) as vocab_handle: SCREAMING_SNAKE_CASE : int = json.load(lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Union[str, Any] = {v: k for k, v in self.vocab.items()} @property def __lowerCAmelCase ( self :int ) -> Dict: '''simple docstring''' return len(self.vocab ) def __lowerCAmelCase ( self :List[str] ) -> Dict: '''simple docstring''' return dict(self.vocab , **self.added_tokens_encoder ) def __lowerCAmelCase ( self :List[str] , lowerCamelCase_ :Optional[Any] ) -> List[str]: '''simple docstring''' SCREAMING_SNAKE_CASE : Union[str, Any] = [] for s in text: char_tokens.extend(lowerCamelCase_ ) return char_tokens def __lowerCAmelCase ( self :Dict , lowerCamelCase_ :Tuple ) -> Optional[int]: '''simple docstring''' return self.vocab.get(lowerCamelCase_ , self.vocab.get(self.unk_token ) ) def __lowerCAmelCase ( self :List[str] , lowerCamelCase_ :Dict ) -> Optional[int]: '''simple docstring''' return self.decoder.get(lowerCamelCase_ ) def __lowerCAmelCase ( self :Optional[int] , lowerCamelCase_ :str , lowerCamelCase_ :Optional[str] = None ) -> Tuple[str]: '''simple docstring''' if not os.path.isdir(lowerCamelCase_ ): logger.error('''Vocabulary path ({}) should be a directory'''.format(lowerCamelCase_ ) ) return SCREAMING_SNAKE_CASE : List[Any] = os.path.join( lowerCamelCase_ , (filename_prefix + '''-''' if filename_prefix else '''''') + VOCAB_FILES_NAMES['''vocab_file'''] ) with open(lowerCamelCase_ , '''w''' , encoding='''utf-8''' ) as f: f.write(json.dumps(self.vocab , indent=2 , sort_keys=lowerCamelCase_ , ensure_ascii=lowerCamelCase_ ) + '''\n''' ) return (vocab_file,)
18
0
import json from typing import List, Optional, Tuple from tokenizers import normalizers from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import logging from .tokenization_mobilebert import MobileBertTokenizer lowerCamelCase__ : Any = logging.get_logger(__name__) lowerCamelCase__ : List[str] = {"vocab_file": "vocab.txt", "tokenizer_file": "tokenizer.json"} lowerCamelCase__ : Tuple = { "vocab_file": {"mobilebert-uncased": "https://huggingface.co/google/mobilebert-uncased/resolve/main/vocab.txt"}, "tokenizer_file": { "mobilebert-uncased": "https://huggingface.co/google/mobilebert-uncased/resolve/main/tokenizer.json" }, } lowerCamelCase__ : Tuple = {"mobilebert-uncased": 512} lowerCamelCase__ : Union[str, Any] = {} class lowercase__( _UpperCAmelCase ): '''simple docstring''' UpperCamelCase = VOCAB_FILES_NAMES UpperCamelCase = PRETRAINED_VOCAB_FILES_MAP UpperCamelCase = PRETRAINED_INIT_CONFIGURATION UpperCamelCase = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES UpperCamelCase = MobileBertTokenizer def __init__( self :List[str] , lowerCamelCase_ :str=None , lowerCamelCase_ :int=None , lowerCamelCase_ :Tuple=True , lowerCamelCase_ :int="[UNK]" , lowerCamelCase_ :Optional[int]="[SEP]" , lowerCamelCase_ :int="[PAD]" , lowerCamelCase_ :Tuple="[CLS]" , lowerCamelCase_ :Dict="[MASK]" , lowerCamelCase_ :str=True , lowerCamelCase_ :Optional[Any]=None , **lowerCamelCase_ :List[str] , ) -> Tuple: '''simple docstring''' super().__init__( lowercase__ , tokenizer_file=lowercase__ , do_lower_case=lowercase__ , unk_token=lowercase__ , sep_token=lowercase__ , pad_token=lowercase__ , cls_token=lowercase__ , mask_token=lowercase__ , tokenize_chinese_chars=lowercase__ , strip_accents=lowercase__ , **lowercase__ , ) SCREAMING_SNAKE_CASE : Optional[Any] = json.loads(self.backend_tokenizer.normalizer.__getstate__() ) if ( normalizer_state.get('''lowercase''' , lowercase__ ) != do_lower_case or normalizer_state.get('''strip_accents''' , lowercase__ ) != strip_accents or normalizer_state.get('''handle_chinese_chars''' , lowercase__ ) != tokenize_chinese_chars ): SCREAMING_SNAKE_CASE : Optional[Any] = getattr(lowercase__ , normalizer_state.pop('''type''' ) ) SCREAMING_SNAKE_CASE : Any = do_lower_case SCREAMING_SNAKE_CASE : Any = strip_accents SCREAMING_SNAKE_CASE : Optional[int] = tokenize_chinese_chars SCREAMING_SNAKE_CASE : Optional[int] = normalizer_class(**lowercase__ ) SCREAMING_SNAKE_CASE : Optional[int] = do_lower_case def __lowerCAmelCase ( self :Any , lowerCamelCase_ :Any , lowerCamelCase_ :Union[str, Any]=None ) -> List[str]: '''simple docstring''' SCREAMING_SNAKE_CASE : List[str] = [self.cls_token_id] + token_ids_a + [self.sep_token_id] if token_ids_a: output += token_ids_a + [self.sep_token_id] return output def __lowerCAmelCase ( self :Optional[int] , lowerCamelCase_ :Union[str, Any] , lowerCamelCase_ :List[str] = None ) -> Optional[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : Dict = [self.sep_token_id] SCREAMING_SNAKE_CASE : Any = [self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep ) * [0] return len(cls + token_ids_a + sep ) * [0] + len(token_ids_a + sep ) * [1] def __lowerCAmelCase ( self :int , lowerCamelCase_ :Union[str, Any] , lowerCamelCase_ :Any = None ) -> str: '''simple docstring''' SCREAMING_SNAKE_CASE : Union[str, Any] = self._tokenizer.model.save(lowercase__ , name=lowercase__ ) return tuple(lowercase__ )
703
"""simple docstring""" from ...configuration_utils import PretrainedConfig from ...utils import logging lowerCamelCase__ : Tuple = logging.get_logger(__name__) lowerCamelCase__ : str = { "studio-ousia/luke-base": "https://huggingface.co/studio-ousia/luke-base/resolve/main/config.json", "studio-ousia/luke-large": "https://huggingface.co/studio-ousia/luke-large/resolve/main/config.json", } class lowercase__( _UpperCAmelCase ): '''simple docstring''' UpperCamelCase = """luke""" def __init__( self :Optional[int] , lowerCamelCase_ :Union[str, Any]=5_02_67 , lowerCamelCase_ :int=50_00_00 , lowerCamelCase_ :Tuple=7_68 , lowerCamelCase_ :List[str]=2_56 , lowerCamelCase_ :Dict=12 , lowerCamelCase_ :Optional[int]=12 , lowerCamelCase_ :Optional[Any]=30_72 , lowerCamelCase_ :List[Any]="gelu" , lowerCamelCase_ :Any=0.1 , lowerCamelCase_ :Union[str, Any]=0.1 , lowerCamelCase_ :List[str]=5_12 , lowerCamelCase_ :Optional[int]=2 , lowerCamelCase_ :Tuple=0.0_2 , lowerCamelCase_ :Optional[int]=1E-12 , lowerCamelCase_ :List[Any]=True , lowerCamelCase_ :List[str]=None , lowerCamelCase_ :str=1 , lowerCamelCase_ :Any=0 , lowerCamelCase_ :str=2 , **lowerCamelCase_ :List[Any] , ) -> Optional[int]: '''simple docstring''' super().__init__(pad_token_id=lowerCamelCase_ , bos_token_id=lowerCamelCase_ , eos_token_id=lowerCamelCase_ , **lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Dict = vocab_size SCREAMING_SNAKE_CASE : List[str] = entity_vocab_size SCREAMING_SNAKE_CASE : str = hidden_size SCREAMING_SNAKE_CASE : Union[str, Any] = entity_emb_size SCREAMING_SNAKE_CASE : List[Any] = num_hidden_layers SCREAMING_SNAKE_CASE : Optional[int] = num_attention_heads SCREAMING_SNAKE_CASE : Optional[Any] = hidden_act SCREAMING_SNAKE_CASE : Optional[Any] = intermediate_size SCREAMING_SNAKE_CASE : Union[str, Any] = hidden_dropout_prob SCREAMING_SNAKE_CASE : Dict = attention_probs_dropout_prob SCREAMING_SNAKE_CASE : Dict = max_position_embeddings SCREAMING_SNAKE_CASE : Tuple = type_vocab_size SCREAMING_SNAKE_CASE : str = initializer_range SCREAMING_SNAKE_CASE : Dict = layer_norm_eps SCREAMING_SNAKE_CASE : Optional[int] = use_entity_aware_attention SCREAMING_SNAKE_CASE : List[str] = classifier_dropout
18
0
"""simple docstring""" from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_sentencepiece_available lowerCamelCase__ : List[Any] = {} try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCamelCase__ : Tuple = ["GPTSw3Tokenizer"] if TYPE_CHECKING: try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_gpt_swa import GPTSwaTokenizer else: import sys lowerCamelCase__ : Union[str, Any] = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
704
"""simple docstring""" def __A ( a_ : list , a_ : int , a_ : int = 0 , a_ : int = 0 )-> int: '''simple docstring''' SCREAMING_SNAKE_CASE : str = right or len(a_ ) - 1 if left > right: return -1 elif list_data[left] == key: return left elif list_data[right] == key: return right else: return search(a_ , a_ , left + 1 , right - 1 ) if __name__ == "__main__": import doctest doctest.testmod()
18
0
"""simple docstring""" import os import unittest from transformers import BatchEncoding from transformers.models.bert.tokenization_bert import ( BasicTokenizer, WordpieceTokenizer, _is_control, _is_punctuation, _is_whitespace, ) from transformers.models.prophetnet.tokenization_prophetnet import VOCAB_FILES_NAMES, ProphetNetTokenizer from transformers.testing_utils import require_torch, slow from ...test_tokenization_common import TokenizerTesterMixin class lowercase__( _UpperCAmelCase , unittest.TestCase ): '''simple docstring''' UpperCamelCase = ProphetNetTokenizer UpperCamelCase = False def __lowerCAmelCase ( self :Dict ) -> Union[str, Any]: '''simple docstring''' super().setUp() SCREAMING_SNAKE_CASE : Optional[int] = [ '''[UNK]''', '''[CLS]''', '''[SEP]''', '''[PAD]''', '''[MASK]''', '''want''', '''##want''', '''##ed''', '''wa''', '''un''', '''runn''', '''##ing''', ''',''', '''low''', '''lowest''', ] SCREAMING_SNAKE_CASE : Any = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES['''vocab_file'''] ) with open(self.vocab_file , '''w''' , encoding='''utf-8''' ) as vocab_writer: vocab_writer.write(''''''.join([x + '''\n''' for x in vocab_tokens] ) ) def __lowerCAmelCase ( self :Tuple , lowerCamelCase_ :List[Any] ) -> Optional[int]: '''simple docstring''' SCREAMING_SNAKE_CASE : Union[str, Any] = '''UNwant\u00E9d,running''' SCREAMING_SNAKE_CASE : List[Any] = '''unwanted, running''' return input_text, output_text def __lowerCAmelCase ( self :int ) -> List[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : Dict = self.tokenizer_class(self.vocab_file ) SCREAMING_SNAKE_CASE : int = tokenizer.tokenize('''UNwant\u00E9d,running''' ) self.assertListEqual(lowerCamelCase_ , ['''un''', '''##want''', '''##ed''', ''',''', '''runn''', '''##ing'''] ) self.assertListEqual(tokenizer.convert_tokens_to_ids(lowerCamelCase_ ) , [9, 6, 7, 12, 10, 11] ) def __lowerCAmelCase ( self :int ) -> Dict: '''simple docstring''' SCREAMING_SNAKE_CASE : str = BasicTokenizer() self.assertListEqual(tokenizer.tokenize('''ah\u535A\u63A8zz''' ) , ['''ah''', '''\u535A''', '''\u63A8''', '''zz'''] ) def __lowerCAmelCase ( self :Tuple ) -> int: '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[int] = BasicTokenizer(do_lower_case=lowerCamelCase_ ) self.assertListEqual( tokenizer.tokenize(''' \tHeLLo!how \n Are yoU? ''' ) , ['''hello''', '''!''', '''how''', '''are''', '''you''', '''?'''] ) self.assertListEqual(tokenizer.tokenize('''H\u00E9llo''' ) , ['''hello'''] ) def __lowerCAmelCase ( self :Dict ) -> Union[str, Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : List[str] = BasicTokenizer(do_lower_case=lowerCamelCase_ , strip_accents=lowerCamelCase_ ) self.assertListEqual( tokenizer.tokenize(''' \tHäLLo!how \n Are yoU? ''' ) , ['''hällo''', '''!''', '''how''', '''are''', '''you''', '''?'''] ) self.assertListEqual(tokenizer.tokenize('''H\u00E9llo''' ) , ['''h\u00E9llo'''] ) def __lowerCAmelCase ( self :Any ) -> Any: '''simple docstring''' SCREAMING_SNAKE_CASE : List[Any] = BasicTokenizer(do_lower_case=lowerCamelCase_ , strip_accents=lowerCamelCase_ ) self.assertListEqual( tokenizer.tokenize(''' \tHäLLo!how \n Are yoU? ''' ) , ['''hallo''', '''!''', '''how''', '''are''', '''you''', '''?'''] ) self.assertListEqual(tokenizer.tokenize('''H\u00E9llo''' ) , ['''hello'''] ) def __lowerCAmelCase ( self :Any ) -> int: '''simple docstring''' SCREAMING_SNAKE_CASE : Tuple = BasicTokenizer(do_lower_case=lowerCamelCase_ ) self.assertListEqual( tokenizer.tokenize(''' \tHäLLo!how \n Are yoU? ''' ) , ['''hallo''', '''!''', '''how''', '''are''', '''you''', '''?'''] ) self.assertListEqual(tokenizer.tokenize('''H\u00E9llo''' ) , ['''hello'''] ) def __lowerCAmelCase ( self :Optional[int] ) -> Any: '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[Any] = BasicTokenizer(do_lower_case=lowerCamelCase_ ) self.assertListEqual( tokenizer.tokenize(''' \tHeLLo!how \n Are yoU? ''' ) , ['''HeLLo''', '''!''', '''how''', '''Are''', '''yoU''', '''?'''] ) def __lowerCAmelCase ( self :List[str] ) -> Dict: '''simple docstring''' SCREAMING_SNAKE_CASE : str = BasicTokenizer(do_lower_case=lowerCamelCase_ , strip_accents=lowerCamelCase_ ) self.assertListEqual( tokenizer.tokenize(''' \tHäLLo!how \n Are yoU? ''' ) , ['''HäLLo''', '''!''', '''how''', '''Are''', '''yoU''', '''?'''] ) def __lowerCAmelCase ( self :Optional[int] ) -> List[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : Tuple = BasicTokenizer(do_lower_case=lowerCamelCase_ , strip_accents=lowerCamelCase_ ) self.assertListEqual( tokenizer.tokenize(''' \tHäLLo!how \n Are yoU? ''' ) , ['''HaLLo''', '''!''', '''how''', '''Are''', '''yoU''', '''?'''] ) def __lowerCAmelCase ( self :Dict ) -> Optional[int]: '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[Any] = BasicTokenizer(do_lower_case=lowerCamelCase_ , never_split=['''[UNK]'''] ) self.assertListEqual( tokenizer.tokenize(''' \tHeLLo!how \n Are yoU? [UNK]''' ) , ['''HeLLo''', '''!''', '''how''', '''Are''', '''yoU''', '''?''', '''[UNK]'''] ) def __lowerCAmelCase ( self :Optional[Any] ) -> int: '''simple docstring''' SCREAMING_SNAKE_CASE : List[str] = ['''[UNK]''', '''[CLS]''', '''[SEP]''', '''want''', '''##want''', '''##ed''', '''wa''', '''un''', '''runn''', '''##ing'''] SCREAMING_SNAKE_CASE : int = {} for i, token in enumerate(lowerCamelCase_ ): SCREAMING_SNAKE_CASE : int = i SCREAMING_SNAKE_CASE : List[Any] = WordpieceTokenizer(vocab=lowerCamelCase_ , unk_token='''[UNK]''' ) self.assertListEqual(tokenizer.tokenize('''''' ) , [] ) self.assertListEqual(tokenizer.tokenize('''unwanted running''' ) , ['''un''', '''##want''', '''##ed''', '''runn''', '''##ing'''] ) self.assertListEqual(tokenizer.tokenize('''unwantedX running''' ) , ['''[UNK]''', '''runn''', '''##ing'''] ) @require_torch def __lowerCAmelCase ( self :Any ) -> Optional[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[Any] = self.tokenizer_class.from_pretrained('''microsoft/prophetnet-large-uncased''' ) SCREAMING_SNAKE_CASE : Optional[Any] = ['''A long paragraph for summarization.''', '''Another paragraph for summarization.'''] SCREAMING_SNAKE_CASE : str = [10_37, 21_46, 2_04_23, 20_05, 76_80, 78_49, 39_89, 10_12, 1_02] SCREAMING_SNAKE_CASE : Tuple = tokenizer(lowerCamelCase_ , padding=lowerCamelCase_ , return_tensors='''pt''' ) self.assertIsInstance(lowerCamelCase_ , lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Tuple = list(batch.input_ids.numpy()[0] ) self.assertListEqual(lowerCamelCase_ , lowerCamelCase_ ) self.assertEqual((2, 9) , batch.input_ids.shape ) self.assertEqual((2, 9) , batch.attention_mask.shape ) def __lowerCAmelCase ( self :int ) -> Tuple: '''simple docstring''' self.assertTrue(_is_whitespace(''' ''' ) ) self.assertTrue(_is_whitespace('''\t''' ) ) self.assertTrue(_is_whitespace('''\r''' ) ) self.assertTrue(_is_whitespace('''\n''' ) ) self.assertTrue(_is_whitespace('''\u00A0''' ) ) self.assertFalse(_is_whitespace('''A''' ) ) self.assertFalse(_is_whitespace('''-''' ) ) def __lowerCAmelCase ( self :str ) -> int: '''simple docstring''' self.assertTrue(_is_control('''\u0005''' ) ) self.assertFalse(_is_control('''A''' ) ) self.assertFalse(_is_control(''' ''' ) ) self.assertFalse(_is_control('''\t''' ) ) self.assertFalse(_is_control('''\r''' ) ) def __lowerCAmelCase ( self :Tuple ) -> Any: '''simple docstring''' self.assertTrue(_is_punctuation('''-''' ) ) self.assertTrue(_is_punctuation('''$''' ) ) self.assertTrue(_is_punctuation('''`''' ) ) self.assertTrue(_is_punctuation('''.''' ) ) self.assertFalse(_is_punctuation('''A''' ) ) self.assertFalse(_is_punctuation(''' ''' ) ) @slow def __lowerCAmelCase ( self :Tuple ) -> Dict: '''simple docstring''' SCREAMING_SNAKE_CASE : Tuple = self.tokenizer_class.from_pretrained('''microsoft/prophetnet-large-uncased''' ) SCREAMING_SNAKE_CASE : Optional[int] = tokenizer.encode('''sequence builders''' , add_special_tokens=lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Optional[int] = tokenizer.encode('''multi-sequence build''' , add_special_tokens=lowerCamelCase_ ) SCREAMING_SNAKE_CASE : str = tokenizer.build_inputs_with_special_tokens(lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Any = tokenizer.build_inputs_with_special_tokens(lowerCamelCase_ , lowerCamelCase_ ) assert encoded_sentence == text + [1_02] assert encoded_pair == text + [1_02] + text_a + [1_02]
705
"""simple docstring""" def __A ( a_ : int )-> list[int]: '''simple docstring''' if num <= 0: raise ValueError('''Input must be a positive integer''' ) SCREAMING_SNAKE_CASE : Optional[int] = [True] * (num + 1) SCREAMING_SNAKE_CASE : Optional[Any] = 2 while p * p <= num: if primes[p]: for i in range(p * p , num + 1 , a_ ): SCREAMING_SNAKE_CASE : Any = False p += 1 return [prime for prime in range(2 , num + 1 ) if primes[prime]] if __name__ == "__main__": import doctest doctest.testmod() lowerCamelCase__ : str = int(input("Enter a positive integer: ").strip()) print(prime_sieve_eratosthenes(user_num))
18
0
"""simple docstring""" import unittest from transformers import MraConfig, is_torch_available from transformers.testing_utils import require_torch, slow, torch_device from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor, random_attention_mask if is_torch_available(): import torch from transformers import ( MraForMaskedLM, MraForMultipleChoice, MraForQuestionAnswering, MraForSequenceClassification, MraForTokenClassification, MraModel, ) from transformers.models.mra.modeling_mra import MRA_PRETRAINED_MODEL_ARCHIVE_LIST class lowercase__: '''simple docstring''' def __init__( self :int , lowerCamelCase_ :str , lowerCamelCase_ :List[Any]=2 , lowerCamelCase_ :int=8 , lowerCamelCase_ :Dict=True , lowerCamelCase_ :Tuple=True , lowerCamelCase_ :List[str]=True , lowerCamelCase_ :Dict=True , lowerCamelCase_ :List[str]=99 , lowerCamelCase_ :Union[str, Any]=16 , lowerCamelCase_ :Any=5 , lowerCamelCase_ :Any=2 , lowerCamelCase_ :Any=36 , lowerCamelCase_ :Tuple="gelu" , lowerCamelCase_ :Dict=0.0 , lowerCamelCase_ :Optional[int]=0.0 , lowerCamelCase_ :str=5_12 , lowerCamelCase_ :Optional[int]=16 , lowerCamelCase_ :Dict=2 , lowerCamelCase_ :Dict=0.0_2 , lowerCamelCase_ :Any=3 , lowerCamelCase_ :List[str]=4 , lowerCamelCase_ :Optional[int]=None , ) -> Any: '''simple docstring''' SCREAMING_SNAKE_CASE : Any = parent SCREAMING_SNAKE_CASE : Union[str, Any] = batch_size SCREAMING_SNAKE_CASE : Optional[int] = seq_length SCREAMING_SNAKE_CASE : Union[str, Any] = is_training SCREAMING_SNAKE_CASE : Optional[int] = use_input_mask SCREAMING_SNAKE_CASE : Dict = use_token_type_ids SCREAMING_SNAKE_CASE : int = use_labels SCREAMING_SNAKE_CASE : Any = vocab_size SCREAMING_SNAKE_CASE : List[str] = hidden_size SCREAMING_SNAKE_CASE : Optional[int] = num_hidden_layers SCREAMING_SNAKE_CASE : Any = num_attention_heads SCREAMING_SNAKE_CASE : Union[str, Any] = intermediate_size SCREAMING_SNAKE_CASE : Optional[Any] = hidden_act SCREAMING_SNAKE_CASE : int = hidden_dropout_prob SCREAMING_SNAKE_CASE : Dict = attention_probs_dropout_prob SCREAMING_SNAKE_CASE : Union[str, Any] = max_position_embeddings SCREAMING_SNAKE_CASE : Tuple = type_vocab_size SCREAMING_SNAKE_CASE : Dict = type_sequence_label_size SCREAMING_SNAKE_CASE : List[str] = initializer_range SCREAMING_SNAKE_CASE : List[str] = num_labels SCREAMING_SNAKE_CASE : Dict = num_choices SCREAMING_SNAKE_CASE : Any = scope def __lowerCAmelCase ( self :List[Any] ) -> Dict: '''simple docstring''' SCREAMING_SNAKE_CASE : List[str] = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) SCREAMING_SNAKE_CASE : List[Any] = None if self.use_input_mask: SCREAMING_SNAKE_CASE : Optional[Any] = random_attention_mask([self.batch_size, self.seq_length] ) SCREAMING_SNAKE_CASE : List[str] = None if self.use_token_type_ids: SCREAMING_SNAKE_CASE : int = ids_tensor([self.batch_size, self.seq_length] , self.type_vocab_size ) SCREAMING_SNAKE_CASE : List[Any] = None SCREAMING_SNAKE_CASE : List[str] = None SCREAMING_SNAKE_CASE : List[str] = None if self.use_labels: SCREAMING_SNAKE_CASE : List[str] = ids_tensor([self.batch_size] , self.type_sequence_label_size ) SCREAMING_SNAKE_CASE : Any = ids_tensor([self.batch_size, self.seq_length] , self.num_labels ) SCREAMING_SNAKE_CASE : Optional[int] = ids_tensor([self.batch_size] , self.num_choices ) SCREAMING_SNAKE_CASE : List[str] = self.get_config() return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels def __lowerCAmelCase ( self :Any ) -> Optional[int]: '''simple docstring''' return MraConfig( vocab_size=self.vocab_size , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , max_position_embeddings=self.max_position_embeddings , type_vocab_size=self.type_vocab_size , is_decoder=__a , initializer_range=self.initializer_range , ) def __lowerCAmelCase ( self :str ) -> Tuple: '''simple docstring''' SCREAMING_SNAKE_CASE : List[Any] = self.get_config() SCREAMING_SNAKE_CASE : List[str] = 3_00 return config def __lowerCAmelCase ( self :List[str] ) -> Union[str, Any]: '''simple docstring''' ( SCREAMING_SNAKE_CASE ) : Union[str, Any] = self.prepare_config_and_inputs() SCREAMING_SNAKE_CASE : Optional[int] = True SCREAMING_SNAKE_CASE : Union[str, Any] = floats_tensor([self.batch_size, self.seq_length, self.hidden_size] ) SCREAMING_SNAKE_CASE : Optional[int] = ids_tensor([self.batch_size, self.seq_length] , vocab_size=2 ) return ( config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels, encoder_hidden_states, encoder_attention_mask, ) def __lowerCAmelCase ( self :Optional[Any] , lowerCamelCase_ :List[str] , lowerCamelCase_ :Tuple , lowerCamelCase_ :Tuple , lowerCamelCase_ :List[Any] , lowerCamelCase_ :Optional[int] , lowerCamelCase_ :List[str] , lowerCamelCase_ :Union[str, Any] ) -> Tuple: '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[Any] = MraModel(config=__a ) model.to(__a ) model.eval() SCREAMING_SNAKE_CASE : Tuple = model(__a , attention_mask=__a , token_type_ids=__a ) SCREAMING_SNAKE_CASE : str = model(__a , token_type_ids=__a ) SCREAMING_SNAKE_CASE : Tuple = model(__a ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def __lowerCAmelCase ( self :Dict , lowerCamelCase_ :Any , lowerCamelCase_ :Optional[Any] , lowerCamelCase_ :int , lowerCamelCase_ :List[str] , lowerCamelCase_ :str , lowerCamelCase_ :Tuple , lowerCamelCase_ :Optional[int] , lowerCamelCase_ :Optional[int] , lowerCamelCase_ :int , ) -> int: '''simple docstring''' SCREAMING_SNAKE_CASE : List[Any] = True SCREAMING_SNAKE_CASE : str = MraModel(__a ) model.to(__a ) model.eval() SCREAMING_SNAKE_CASE : str = model( __a , attention_mask=__a , token_type_ids=__a , encoder_hidden_states=__a , encoder_attention_mask=__a , ) SCREAMING_SNAKE_CASE : List[str] = model( __a , attention_mask=__a , token_type_ids=__a , encoder_hidden_states=__a , ) SCREAMING_SNAKE_CASE : Dict = model(__a , attention_mask=__a , token_type_ids=__a ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def __lowerCAmelCase ( self :Tuple , lowerCamelCase_ :Union[str, Any] , lowerCamelCase_ :str , lowerCamelCase_ :Dict , lowerCamelCase_ :int , lowerCamelCase_ :List[str] , lowerCamelCase_ :int , lowerCamelCase_ :int ) -> int: '''simple docstring''' SCREAMING_SNAKE_CASE : Tuple = MraForMaskedLM(config=__a ) model.to(__a ) model.eval() SCREAMING_SNAKE_CASE : Any = model(__a , attention_mask=__a , token_type_ids=__a , labels=__a ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.vocab_size) ) def __lowerCAmelCase ( self :Optional[Any] , lowerCamelCase_ :int , lowerCamelCase_ :int , lowerCamelCase_ :int , lowerCamelCase_ :Any , lowerCamelCase_ :List[str] , lowerCamelCase_ :List[str] , lowerCamelCase_ :Dict ) -> str: '''simple docstring''' SCREAMING_SNAKE_CASE : Union[str, Any] = MraForQuestionAnswering(config=__a ) model.to(__a ) model.eval() SCREAMING_SNAKE_CASE : List[Any] = model( __a , attention_mask=__a , token_type_ids=__a , start_positions=__a , end_positions=__a , ) self.parent.assertEqual(result.start_logits.shape , (self.batch_size, self.seq_length) ) self.parent.assertEqual(result.end_logits.shape , (self.batch_size, self.seq_length) ) def __lowerCAmelCase ( self :Union[str, Any] , lowerCamelCase_ :Dict , lowerCamelCase_ :Any , lowerCamelCase_ :Any , lowerCamelCase_ :Optional[Any] , lowerCamelCase_ :List[Any] , lowerCamelCase_ :Optional[int] , lowerCamelCase_ :Any ) -> Optional[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : List[Any] = self.num_labels SCREAMING_SNAKE_CASE : Optional[int] = MraForSequenceClassification(__a ) model.to(__a ) model.eval() SCREAMING_SNAKE_CASE : int = model(__a , attention_mask=__a , token_type_ids=__a , labels=__a ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_labels) ) def __lowerCAmelCase ( self :List[str] , lowerCamelCase_ :str , lowerCamelCase_ :Tuple , lowerCamelCase_ :int , lowerCamelCase_ :Any , lowerCamelCase_ :Optional[int] , lowerCamelCase_ :str , lowerCamelCase_ :str ) -> int: '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[int] = self.num_labels SCREAMING_SNAKE_CASE : Tuple = MraForTokenClassification(config=__a ) model.to(__a ) model.eval() SCREAMING_SNAKE_CASE : Any = model(__a , attention_mask=__a , token_type_ids=__a , labels=__a ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.num_labels) ) def __lowerCAmelCase ( self :Tuple , lowerCamelCase_ :Union[str, Any] , lowerCamelCase_ :Optional[Any] , lowerCamelCase_ :Any , lowerCamelCase_ :Optional[int] , lowerCamelCase_ :str , lowerCamelCase_ :Any , lowerCamelCase_ :Any ) -> str: '''simple docstring''' SCREAMING_SNAKE_CASE : Tuple = self.num_choices SCREAMING_SNAKE_CASE : Optional[int] = MraForMultipleChoice(config=__a ) model.to(__a ) model.eval() SCREAMING_SNAKE_CASE : str = input_ids.unsqueeze(1 ).expand(-1 , self.num_choices , -1 ).contiguous() SCREAMING_SNAKE_CASE : Optional[Any] = token_type_ids.unsqueeze(1 ).expand(-1 , self.num_choices , -1 ).contiguous() SCREAMING_SNAKE_CASE : Tuple = input_mask.unsqueeze(1 ).expand(-1 , self.num_choices , -1 ).contiguous() SCREAMING_SNAKE_CASE : Optional[int] = model( __a , attention_mask=__a , token_type_ids=__a , labels=__a , ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_choices) ) def __lowerCAmelCase ( self :Optional[Any] ) -> Optional[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[Any] = self.prepare_config_and_inputs() ( SCREAMING_SNAKE_CASE ) : int = config_and_inputs SCREAMING_SNAKE_CASE : str = {"input_ids": input_ids, "token_type_ids": token_type_ids, "attention_mask": input_mask} return config, inputs_dict @require_torch class lowercase__( _UpperCamelCase , unittest.TestCase ): '''simple docstring''' UpperCamelCase = ( ( MraModel, MraForMaskedLM, MraForMultipleChoice, MraForQuestionAnswering, MraForSequenceClassification, MraForTokenClassification, ) if is_torch_available() else () ) UpperCamelCase = False UpperCamelCase = False UpperCamelCase = False UpperCamelCase = False UpperCamelCase = () def __lowerCAmelCase ( self :int ) -> Any: '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[int] = MraModelTester(self ) SCREAMING_SNAKE_CASE : Any = ConfigTester(self , config_class=__a , hidden_size=37 ) def __lowerCAmelCase ( self :int ) -> Dict: '''simple docstring''' self.config_tester.run_common_tests() def __lowerCAmelCase ( self :Tuple ) -> List[str]: '''simple docstring''' SCREAMING_SNAKE_CASE : int = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*__a ) def __lowerCAmelCase ( self :List[Any] ) -> List[str]: '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[int] = self.model_tester.prepare_config_and_inputs() for type in ["absolute", "relative_key", "relative_key_query"]: SCREAMING_SNAKE_CASE : Tuple = type self.model_tester.create_and_check_model(*__a ) def __lowerCAmelCase ( self :Tuple ) -> List[str]: '''simple docstring''' SCREAMING_SNAKE_CASE : Union[str, Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_masked_lm(*__a ) def __lowerCAmelCase ( self :List[Any] ) -> str: '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_multiple_choice(*__a ) def __lowerCAmelCase ( self :Any ) -> Optional[int]: '''simple docstring''' SCREAMING_SNAKE_CASE : List[Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_question_answering(*__a ) def __lowerCAmelCase ( self :Union[str, Any] ) -> Any: '''simple docstring''' SCREAMING_SNAKE_CASE : Union[str, Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_sequence_classification(*__a ) def __lowerCAmelCase ( self :Tuple ) -> Optional[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : Tuple = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_token_classification(*__a ) @slow def __lowerCAmelCase ( self :Optional[int] ) -> Union[str, Any]: '''simple docstring''' for model_name in MRA_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: SCREAMING_SNAKE_CASE : Tuple = MraModel.from_pretrained(__a ) self.assertIsNotNone(__a ) @unittest.skip(reason='''MRA does not output attentions''' ) def __lowerCAmelCase ( self :List[str] ) -> Tuple: '''simple docstring''' return @require_torch class lowercase__( unittest.TestCase ): '''simple docstring''' @slow def __lowerCAmelCase ( self :Dict ) -> Any: '''simple docstring''' SCREAMING_SNAKE_CASE : Tuple = MraModel.from_pretrained('''uw-madison/mra-base-512-4''' ) SCREAMING_SNAKE_CASE : Optional[Any] = torch.arange(2_56 ).unsqueeze(0 ) with torch.no_grad(): SCREAMING_SNAKE_CASE : Dict = model(__a )[0] SCREAMING_SNAKE_CASE : Optional[Any] = torch.Size((1, 2_56, 7_68) ) self.assertEqual(output.shape , __a ) SCREAMING_SNAKE_CASE : Any = torch.tensor( [[[-0.0_1_4_0, 0.0_8_3_0, -0.0_3_8_1], [0.1_5_4_6, 0.1_4_0_2, 0.0_2_2_0], [0.1_1_6_2, 0.0_8_5_1, 0.0_1_6_5]]] ) self.assertTrue(torch.allclose(output[:, :3, :3] , __a , atol=1E-4 ) ) @slow def __lowerCAmelCase ( self :List[Any] ) -> List[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : List[Any] = MraForMaskedLM.from_pretrained('''uw-madison/mra-base-512-4''' ) SCREAMING_SNAKE_CASE : str = torch.arange(2_56 ).unsqueeze(0 ) with torch.no_grad(): SCREAMING_SNAKE_CASE : Optional[int] = model(__a )[0] SCREAMING_SNAKE_CASE : int = 5_02_65 SCREAMING_SNAKE_CASE : str = torch.Size((1, 2_56, vocab_size) ) self.assertEqual(output.shape , __a ) SCREAMING_SNAKE_CASE : List[str] = torch.tensor( [[[9.2_5_9_5, -3.6_0_3_8, 1_1.8_8_1_9], [9.3_8_6_9, -3.2_6_9_3, 1_1.0_9_5_6], [1_1.8_5_2_4, -3.4_9_3_8, 1_3.1_2_1_0]]] ) self.assertTrue(torch.allclose(output[:, :3, :3] , __a , atol=1E-4 ) ) @slow def __lowerCAmelCase ( self :List[Any] ) -> List[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : Dict = MraForMaskedLM.from_pretrained('''uw-madison/mra-base-4096-8-d3''' ) SCREAMING_SNAKE_CASE : Any = torch.arange(40_96 ).unsqueeze(0 ) with torch.no_grad(): SCREAMING_SNAKE_CASE : Any = model(__a )[0] SCREAMING_SNAKE_CASE : Any = 5_02_65 SCREAMING_SNAKE_CASE : str = torch.Size((1, 40_96, vocab_size) ) self.assertEqual(output.shape , __a ) SCREAMING_SNAKE_CASE : List[str] = torch.tensor( [[[5.4_7_8_9, -2.3_5_6_4, 7.5_0_6_4], [7.9_0_6_7, -1.3_3_6_9, 9.9_6_6_8], [9.0_7_1_2, -1.8_1_0_6, 7.0_3_8_0]]] ) self.assertTrue(torch.allclose(output[:, :3, :3] , __a , atol=1E-4 ) )
706
"""simple docstring""" from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_tf_available, is_tokenizers_available, is_torch_available, ) lowerCamelCase__ : Optional[Any] = { "configuration_funnel": ["FUNNEL_PRETRAINED_CONFIG_ARCHIVE_MAP", "FunnelConfig"], "convert_funnel_original_tf_checkpoint_to_pytorch": [], "tokenization_funnel": ["FunnelTokenizer"], } try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCamelCase__ : Optional[Any] = ["FunnelTokenizerFast"] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCamelCase__ : Dict = [ "FUNNEL_PRETRAINED_MODEL_ARCHIVE_LIST", "FunnelBaseModel", "FunnelForMaskedLM", "FunnelForMultipleChoice", "FunnelForPreTraining", "FunnelForQuestionAnswering", "FunnelForSequenceClassification", "FunnelForTokenClassification", "FunnelModel", "FunnelPreTrainedModel", "load_tf_weights_in_funnel", ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCamelCase__ : Tuple = [ "TF_FUNNEL_PRETRAINED_MODEL_ARCHIVE_LIST", "TFFunnelBaseModel", "TFFunnelForMaskedLM", "TFFunnelForMultipleChoice", "TFFunnelForPreTraining", "TFFunnelForQuestionAnswering", "TFFunnelForSequenceClassification", "TFFunnelForTokenClassification", "TFFunnelModel", "TFFunnelPreTrainedModel", ] if TYPE_CHECKING: from .configuration_funnel import FUNNEL_PRETRAINED_CONFIG_ARCHIVE_MAP, FunnelConfig from .tokenization_funnel import FunnelTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_funnel_fast import FunnelTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_funnel import ( FUNNEL_PRETRAINED_MODEL_ARCHIVE_LIST, FunnelBaseModel, FunnelForMaskedLM, FunnelForMultipleChoice, FunnelForPreTraining, FunnelForQuestionAnswering, FunnelForSequenceClassification, FunnelForTokenClassification, FunnelModel, FunnelPreTrainedModel, load_tf_weights_in_funnel, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_funnel import ( TF_FUNNEL_PRETRAINED_MODEL_ARCHIVE_LIST, TFFunnelBaseModel, TFFunnelForMaskedLM, TFFunnelForMultipleChoice, TFFunnelForPreTraining, TFFunnelForQuestionAnswering, TFFunnelForSequenceClassification, TFFunnelForTokenClassification, TFFunnelModel, TFFunnelPreTrainedModel, ) else: import sys lowerCamelCase__ : Optional[int] = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
18
0
"""simple docstring""" import unittest from transformers import TrOCRConfig from transformers.testing_utils import is_torch_available, require_torch, torch_device from ...generation.test_utils import GenerationTesterMixin from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from transformers.models.trocr.modeling_trocr import TrOCRDecoder, TrOCRForCausalLM @require_torch class lowercase__: '''simple docstring''' def __init__( self :Any , lowerCamelCase_ :List[Any] , lowerCamelCase_ :Dict=99 , lowerCamelCase_ :List[str]=13 , lowerCamelCase_ :List[Any]=16 , lowerCamelCase_ :List[Any]=7 , lowerCamelCase_ :Tuple=True , lowerCamelCase_ :Optional[Any]=True , lowerCamelCase_ :Dict=True , lowerCamelCase_ :Dict=False , lowerCamelCase_ :int=True , lowerCamelCase_ :Union[str, Any]=2 , lowerCamelCase_ :Optional[int]=32 , lowerCamelCase_ :Tuple=4 , lowerCamelCase_ :Tuple=4 , lowerCamelCase_ :Dict=30 , lowerCamelCase_ :Tuple=0 , lowerCamelCase_ :Dict=1 , lowerCamelCase_ :List[Any]=2 , lowerCamelCase_ :List[str]=None , ) -> List[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : str = parent SCREAMING_SNAKE_CASE : Optional[Any] = batch_size SCREAMING_SNAKE_CASE : List[str] = decoder_seq_length # For common tests SCREAMING_SNAKE_CASE : Optional[int] = self.decoder_seq_length SCREAMING_SNAKE_CASE : Union[str, Any] = is_training SCREAMING_SNAKE_CASE : List[Any] = use_attention_mask SCREAMING_SNAKE_CASE : Dict = use_labels SCREAMING_SNAKE_CASE : Union[str, Any] = vocab_size SCREAMING_SNAKE_CASE : Tuple = d_model SCREAMING_SNAKE_CASE : Tuple = d_model SCREAMING_SNAKE_CASE : str = decoder_layers SCREAMING_SNAKE_CASE : int = decoder_layers SCREAMING_SNAKE_CASE : int = decoder_ffn_dim SCREAMING_SNAKE_CASE : str = decoder_attention_heads SCREAMING_SNAKE_CASE : Optional[Any] = decoder_attention_heads SCREAMING_SNAKE_CASE : Optional[Any] = eos_token_id SCREAMING_SNAKE_CASE : List[str] = bos_token_id SCREAMING_SNAKE_CASE : List[Any] = pad_token_id SCREAMING_SNAKE_CASE : str = decoder_start_token_id SCREAMING_SNAKE_CASE : Tuple = use_cache SCREAMING_SNAKE_CASE : Tuple = max_position_embeddings SCREAMING_SNAKE_CASE : Dict = None SCREAMING_SNAKE_CASE : Optional[Any] = decoder_seq_length SCREAMING_SNAKE_CASE : Optional[int] = 2 SCREAMING_SNAKE_CASE : Tuple = 1 def __lowerCAmelCase ( self :List[Any] ) -> Dict: '''simple docstring''' SCREAMING_SNAKE_CASE : Union[str, Any] = ids_tensor([self.batch_size, self.decoder_seq_length] , self.vocab_size ) SCREAMING_SNAKE_CASE : List[str] = None if self.use_attention_mask: SCREAMING_SNAKE_CASE : int = ids_tensor([self.batch_size, self.decoder_seq_length] , vocab_size=2 ) SCREAMING_SNAKE_CASE : int = None if self.use_labels: SCREAMING_SNAKE_CASE : Tuple = ids_tensor([self.batch_size, self.decoder_seq_length] , self.vocab_size ) SCREAMING_SNAKE_CASE : Dict = TrOCRConfig( vocab_size=self.vocab_size , d_model=self.d_model , decoder_layers=self.decoder_layers , decoder_ffn_dim=self.decoder_ffn_dim , decoder_attention_heads=self.decoder_attention_heads , eos_token_id=self.eos_token_id , bos_token_id=self.bos_token_id , use_cache=self.use_cache , pad_token_id=self.pad_token_id , decoder_start_token_id=self.decoder_start_token_id , max_position_embeddings=self.max_position_embeddings , ) return (config, input_ids, attention_mask, lm_labels) def __lowerCAmelCase ( self :List[Any] , lowerCamelCase_ :Dict , lowerCamelCase_ :List[Any] , lowerCamelCase_ :List[str] , lowerCamelCase_ :List[str] , ) -> Any: '''simple docstring''' SCREAMING_SNAKE_CASE : Any = True SCREAMING_SNAKE_CASE : Dict = TrOCRDecoder(config=_snake_case ).to(_snake_case ).eval() SCREAMING_SNAKE_CASE : str = input_ids[:2] input_ids[input_ids == 0] += 1 # first forward pass SCREAMING_SNAKE_CASE : Any = model(_snake_case , use_cache=_snake_case ) SCREAMING_SNAKE_CASE : Dict = model(_snake_case ) SCREAMING_SNAKE_CASE : Tuple = model(_snake_case , use_cache=_snake_case ) self.parent.assertTrue(len(_snake_case ) == len(_snake_case ) ) self.parent.assertTrue(len(_snake_case ) == len(_snake_case ) + 1 ) SCREAMING_SNAKE_CASE : Union[str, Any] = outputs['''past_key_values'''] # create hypothetical next token and extent to next_input_ids SCREAMING_SNAKE_CASE : List[str] = ids_tensor((2, 1) , config.vocab_size - 1 ) + 1 # append to next input_ids and SCREAMING_SNAKE_CASE : Union[str, Any] = torch.cat([input_ids, next_tokens] , dim=-1 ) SCREAMING_SNAKE_CASE : Union[str, Any] = model(_snake_case )['''last_hidden_state'''] SCREAMING_SNAKE_CASE : Optional[int] = model(_snake_case , past_key_values=_snake_case )['''last_hidden_state'''] # select random slice SCREAMING_SNAKE_CASE : List[Any] = ids_tensor((1,) , output_from_past.shape[-1] ).item() SCREAMING_SNAKE_CASE : Union[str, Any] = output_from_no_past[:, next_input_ids.shape[-1] - 1, random_slice_idx].detach() SCREAMING_SNAKE_CASE : Union[str, Any] = output_from_past[:, 0, random_slice_idx].detach() # test that outputs are equal for slice assert torch.allclose(_snake_case , _snake_case , atol=1E-3 ) def __lowerCAmelCase ( self :Optional[int] ) -> Optional[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : int = self.prepare_config_and_inputs() SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : Tuple = config_and_inputs SCREAMING_SNAKE_CASE : Optional[Any] = {'''input_ids''': input_ids, '''attention_mask''': attention_mask} return config, inputs_dict @require_torch class lowercase__( UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ , unittest.TestCase ): '''simple docstring''' UpperCamelCase = (TrOCRDecoder, TrOCRForCausalLM) if is_torch_available() else () UpperCamelCase = (TrOCRForCausalLM,) if is_torch_available() else () UpperCamelCase = {"text-generation": TrOCRForCausalLM} if is_torch_available() else {} UpperCamelCase = True UpperCamelCase = False def __lowerCAmelCase ( self :Tuple ) -> Tuple: '''simple docstring''' SCREAMING_SNAKE_CASE : List[str] = TrOCRStandaloneDecoderModelTester(self , is_training=_snake_case ) SCREAMING_SNAKE_CASE : List[Any] = ConfigTester(self , config_class=_snake_case ) def __lowerCAmelCase ( self :Tuple ) -> List[Any]: '''simple docstring''' pass def __lowerCAmelCase ( self :Optional[Any] ) -> Dict: '''simple docstring''' pass def __lowerCAmelCase ( self :Optional[int] ) -> Dict: '''simple docstring''' pass def __lowerCAmelCase ( self :List[Any] ) -> Optional[Any]: '''simple docstring''' self.config_tester.run_common_tests() def __lowerCAmelCase ( self :Optional[Any] ) -> List[str]: '''simple docstring''' SCREAMING_SNAKE_CASE : str = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_decoder_model_past(*_snake_case ) def __lowerCAmelCase ( self :Any ) -> Union[str, Any]: '''simple docstring''' return @unittest.skip('''The model doesn\'t support left padding''' ) # and it's not used enough to be worth fixing :) def __lowerCAmelCase ( self :Union[str, Any] ) -> int: '''simple docstring''' pass
707
"""simple docstring""" import os import sys lowerCamelCase__ : List[Any] = os.path.join(os.path.dirname(__file__), "src") sys.path.append(SRC_DIR) from transformers import ( AutoConfig, AutoModel, AutoModelForCausalLM, AutoModelForMaskedLM, AutoModelForQuestionAnswering, AutoModelForSequenceClassification, AutoTokenizer, add_start_docstrings, ) lowerCamelCase__ : str = [ "torch", "numpy", "tokenizers", "filelock", "requests", "tqdm", "regex", "sentencepiece", "sacremoses", "importlib_metadata", "huggingface_hub", ] @add_start_docstrings(AutoConfig.__doc__ ) def __A ( *a_ : Any , **a_ : Union[str, Any] )-> Dict: '''simple docstring''' return AutoConfig.from_pretrained(*a_ , **a_ ) @add_start_docstrings(AutoTokenizer.__doc__ ) def __A ( *a_ : str , **a_ : Union[str, Any] )-> Union[str, Any]: '''simple docstring''' return AutoTokenizer.from_pretrained(*a_ , **a_ ) @add_start_docstrings(AutoModel.__doc__ ) def __A ( *a_ : List[str] , **a_ : int )-> Dict: '''simple docstring''' return AutoModel.from_pretrained(*a_ , **a_ ) @add_start_docstrings(AutoModelForCausalLM.__doc__ ) def __A ( *a_ : Any , **a_ : Tuple )-> Dict: '''simple docstring''' return AutoModelForCausalLM.from_pretrained(*a_ , **a_ ) @add_start_docstrings(AutoModelForMaskedLM.__doc__ ) def __A ( *a_ : Dict , **a_ : Optional[Any] )-> Optional[int]: '''simple docstring''' return AutoModelForMaskedLM.from_pretrained(*a_ , **a_ ) @add_start_docstrings(AutoModelForSequenceClassification.__doc__ ) def __A ( *a_ : Optional[int] , **a_ : str )-> Optional[int]: '''simple docstring''' return AutoModelForSequenceClassification.from_pretrained(*a_ , **a_ ) @add_start_docstrings(AutoModelForQuestionAnswering.__doc__ ) def __A ( *a_ : List[str] , **a_ : int )-> List[Any]: '''simple docstring''' return AutoModelForQuestionAnswering.from_pretrained(*a_ , **a_ )
18
0
import argparse import json import os import time import zipfile from get_ci_error_statistics import download_artifact, get_artifacts_links from transformers import logging lowerCamelCase__ : Optional[Any] = logging.get_logger(__name__) def __A ( a_ : Optional[Any] , a_ : Any )-> Tuple: '''simple docstring''' SCREAMING_SNAKE_CASE : List[str] = set() SCREAMING_SNAKE_CASE : Union[str, Any] = [] def parse_line(a_ : List[Any] ): for line in fp: if isinstance(a_ , a_ ): SCREAMING_SNAKE_CASE : List[str] = line.decode('''UTF-8''' ) if "warnings summary (final)" in line: continue # This means we are outside the body of a warning elif not line.startswith(''' ''' ): # process a single warning and move it to `selected_warnings`. if len(a_ ) > 0: SCREAMING_SNAKE_CASE : Optional[Any] = '''\n'''.join(a_ ) # Only keep the warnings specified in `targets` if any(F": {x}: " in warning for x in targets ): selected_warnings.add(a_ ) buffer.clear() continue else: SCREAMING_SNAKE_CASE : Dict = line.strip() buffer.append(a_ ) if from_gh: for filename in os.listdir(a_ ): SCREAMING_SNAKE_CASE : Any = os.path.join(a_ , a_ ) if not os.path.isdir(a_ ): # read the file if filename != "warnings.txt": continue with open(a_ ) as fp: parse_line(a_ ) else: try: with zipfile.ZipFile(a_ ) as z: for filename in z.namelist(): if not os.path.isdir(a_ ): # read the file if filename != "warnings.txt": continue with z.open(a_ ) as fp: parse_line(a_ ) except Exception: logger.warning( F"{artifact_path} is either an invalid zip file or something else wrong. This file is skipped." ) return selected_warnings def __A ( a_ : Optional[Any] , a_ : Any )-> Any: '''simple docstring''' SCREAMING_SNAKE_CASE : Union[str, Any] = set() SCREAMING_SNAKE_CASE : Optional[Any] = [os.path.join(a_ , a_ ) for p in os.listdir(a_ ) if (p.endswith('''.zip''' ) or from_gh)] for p in paths: selected_warnings.update(extract_warnings_from_single_artifact(a_ , a_ ) ) return selected_warnings if __name__ == "__main__": def __A ( a_ : Union[str, Any] )-> int: '''simple docstring''' return values.split(''',''' ) lowerCamelCase__ : Union[str, Any] = argparse.ArgumentParser() # Required parameters parser.add_argument("--workflow_run_id", type=str, required=True, help="A GitHub Actions workflow run id.") parser.add_argument( "--output_dir", type=str, required=True, help="Where to store the downloaded artifacts and other result files.", ) parser.add_argument("--token", default=None, type=str, help="A token that has actions:read permission.") # optional parameters parser.add_argument( "--targets", default="DeprecationWarning,UserWarning,FutureWarning", type=list_str, help="Comma-separated list of target warning(s) which we want to extract.", ) parser.add_argument( "--from_gh", action="store_true", help="If running from a GitHub action workflow and collecting warnings from its artifacts.", ) lowerCamelCase__ : List[str] = parser.parse_args() lowerCamelCase__ : Union[str, Any] = args.from_gh if from_gh: # The artifacts have to be downloaded using `actions/download-artifact@v3` pass else: os.makedirs(args.output_dir, exist_ok=True) # get download links lowerCamelCase__ : Any = get_artifacts_links(args.workflow_run_id, token=args.token) with open(os.path.join(args.output_dir, "artifacts.json"), "w", encoding="UTF-8") as fp: json.dump(artifacts, fp, ensure_ascii=False, indent=4) # download artifacts for idx, (name, url) in enumerate(artifacts.items()): print(name) print(url) print("=" * 80) download_artifact(name, url, args.output_dir, args.token) # Be gentle to GitHub time.sleep(1) # extract warnings from artifacts lowerCamelCase__ : Union[str, Any] = extract_warnings(args.output_dir, args.targets) lowerCamelCase__ : Any = sorted(selected_warnings) with open(os.path.join(args.output_dir, "selected_warnings.json"), "w", encoding="UTF-8") as fp: json.dump(selected_warnings, fp, ensure_ascii=False, indent=4)
708
"""simple docstring""" import math from typing import Optional import numpy as np from ...configuration_utils import PretrainedConfig from ...utils import logging lowerCamelCase__ : Tuple = logging.get_logger(__name__) lowerCamelCase__ : Any = { "facebook/encodec_24khz": "https://huggingface.co/facebook/encodec_24khz/resolve/main/config.json", "facebook/encodec_48khz": "https://huggingface.co/facebook/encodec_48khz/resolve/main/config.json", } class lowercase__( _UpperCAmelCase ): '''simple docstring''' UpperCamelCase = """encodec""" def __init__( self :List[str] , lowerCamelCase_ :Tuple=[1.5, 3.0, 6.0, 1_2.0, 2_4.0] , lowerCamelCase_ :str=2_40_00 , lowerCamelCase_ :Any=1 , lowerCamelCase_ :List[Any]=False , lowerCamelCase_ :Optional[int]=None , lowerCamelCase_ :Optional[Any]=None , lowerCamelCase_ :str=1_28 , lowerCamelCase_ :Any=32 , lowerCamelCase_ :int=1 , lowerCamelCase_ :Dict=[8, 5, 4, 2] , lowerCamelCase_ :List[Any]="weight_norm" , lowerCamelCase_ :Optional[int]=7 , lowerCamelCase_ :Tuple=7 , lowerCamelCase_ :Optional[Any]=3 , lowerCamelCase_ :int=2 , lowerCamelCase_ :Dict=True , lowerCamelCase_ :Optional[int]="reflect" , lowerCamelCase_ :Optional[int]=2 , lowerCamelCase_ :Union[str, Any]=2 , lowerCamelCase_ :Dict=1.0 , lowerCamelCase_ :Any=10_24 , lowerCamelCase_ :str=None , lowerCamelCase_ :Union[str, Any]=True , **lowerCamelCase_ :Optional[int] , ) -> Dict: '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[Any] = target_bandwidths SCREAMING_SNAKE_CASE : List[str] = sampling_rate SCREAMING_SNAKE_CASE : Tuple = audio_channels SCREAMING_SNAKE_CASE : Tuple = normalize SCREAMING_SNAKE_CASE : str = chunk_length_s SCREAMING_SNAKE_CASE : List[str] = overlap SCREAMING_SNAKE_CASE : int = hidden_size SCREAMING_SNAKE_CASE : Optional[int] = num_filters SCREAMING_SNAKE_CASE : Tuple = num_residual_layers SCREAMING_SNAKE_CASE : List[Any] = upsampling_ratios SCREAMING_SNAKE_CASE : Optional[int] = norm_type SCREAMING_SNAKE_CASE : Any = kernel_size SCREAMING_SNAKE_CASE : Union[str, Any] = last_kernel_size SCREAMING_SNAKE_CASE : Tuple = residual_kernel_size SCREAMING_SNAKE_CASE : Any = dilation_growth_rate SCREAMING_SNAKE_CASE : Optional[int] = use_causal_conv SCREAMING_SNAKE_CASE : str = pad_mode SCREAMING_SNAKE_CASE : List[Any] = compress SCREAMING_SNAKE_CASE : Optional[Any] = num_lstm_layers SCREAMING_SNAKE_CASE : Dict = trim_right_ratio SCREAMING_SNAKE_CASE : List[Any] = codebook_size SCREAMING_SNAKE_CASE : Union[str, Any] = codebook_dim if codebook_dim is not None else hidden_size SCREAMING_SNAKE_CASE : Union[str, Any] = use_conv_shortcut if self.norm_type not in ["weight_norm", "time_group_norm"]: raise ValueError( f"self.norm_type must be one of `\"weight_norm\"`, `\"time_group_norm\"`), got {self.norm_type}" ) super().__init__(**lowerCamelCase_ ) @property def __lowerCAmelCase ( self :Optional[Any] ) -> Optional[int]: '''simple docstring''' if self.chunk_length_s is None: return None else: return int(self.chunk_length_s * self.sampling_rate ) @property def __lowerCAmelCase ( self :List[str] ) -> Optional[int]: '''simple docstring''' if self.chunk_length_s is None or self.overlap is None: return None else: return max(1 , int((1.0 - self.overlap) * self.chunk_length ) ) @property def __lowerCAmelCase ( self :Dict ) -> int: '''simple docstring''' SCREAMING_SNAKE_CASE : Tuple = np.prod(self.upsampling_ratios ) return math.ceil(self.sampling_rate / hop_length ) @property def __lowerCAmelCase ( self :Dict ) -> int: '''simple docstring''' return int(10_00 * self.target_bandwidths[-1] // (self.frame_rate * 10) )
18
0
"""simple docstring""" # # This a `torch.distributed` diagnostics script that checks that all GPUs in the cluster (one or # many nodes) can talk to each other via nccl and allocate gpu memory. # # To run first adjust the number of processes and nodes: # # python -m torch.distributed.run --nproc_per_node 2 --nnodes 1 torch-distributed-gpu-test.py # # You may need to add --master_addr $MASTER_ADDR --master_port $MASTER_PORT if using a custom addr:port # # You can also use the rdzv API: --rdzv_endpoint $MASTER_ADDR:$MASTER_PORT --rdzv_backend c10d # # use torch.distributed.launch instead of torch.distributed.run for torch < 1.9 # # If you get a hanging in `barrier` calls you have some network issues, you may try to debug this with: # # NCCL_DEBUG=INFO python -m torch.distributed.run --nproc_per_node 2 --nnodes 1 torch-distributed-gpu-test.py # # which should tell you what's going on behind the scenes. # # # This script can be run via `srun` in the SLURM environment as well. Here is a SLURM script that # runs on 2 nodes of 4 gpus per node: # # #SBATCH --job-name=test-nodes # name # #SBATCH --nodes=2 # nodes # #SBATCH --ntasks-per-node=1 # crucial - only 1 task per dist per node! # #SBATCH --cpus-per-task=10 # number of cores per tasks # #SBATCH --gres=gpu:4 # number of gpus # #SBATCH --time 0:05:00 # maximum execution time (HH:MM:SS) # #SBATCH --output=%x-%j.out # output file name # # GPUS_PER_NODE=4 # MASTER_ADDR=$(scontrol show hostnames $SLURM_JOB_NODELIST | head -n 1) # MASTER_PORT=6000 # # srun --jobid $SLURM_JOBID bash -c 'python -m torch.distributed.run \ # --nproc_per_node $GPUS_PER_NODE --nnodes $SLURM_NNODES --node_rank $SLURM_PROCID \ # --master_addr $MASTER_ADDR --master_port $MASTER_PORT \ # torch-distributed-gpu-test.py' # import fcntl import os import socket import torch import torch.distributed as dist def __A ( *a_ : Any )-> Optional[int]: '''simple docstring''' with open(snake_case_ , '''r''' ) as fh: fcntl.flock(snake_case_ , fcntl.LOCK_EX ) try: print(*snake_case_ ) finally: fcntl.flock(snake_case_ , fcntl.LOCK_UN ) lowerCamelCase__ : Union[str, Any] = int(os.environ["LOCAL_RANK"]) torch.cuda.set_device(local_rank) lowerCamelCase__ : Dict = torch.device("cuda", local_rank) lowerCamelCase__ : Dict = socket.gethostname() lowerCamelCase__ : Optional[Any] = f'''[{hostname}-{local_rank}]''' try: # test distributed dist.init_process_group("nccl") dist.all_reduce(torch.ones(1).to(device), op=dist.ReduceOp.SUM) dist.barrier() # test cuda is available and can allocate memory torch.cuda.is_available() torch.ones(1).cuda(local_rank) # global rank lowerCamelCase__ : List[str] = dist.get_rank() lowerCamelCase__ : Optional[int] = dist.get_world_size() printflock(f'''{gpu} is OK (global rank: {rank}/{world_size})''') dist.barrier() if rank == 0: printflock(f'''pt={torch.__version__}, cuda={torch.version.cuda}, nccl={torch.cuda.nccl.version()}''') except Exception: printflock(f'''{gpu} is broken''') raise
709
"""simple docstring""" import copy import unittest from transformers.models.auto import get_values from transformers.testing_utils import require_torch, 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, random_attention_mask from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from transformers import ( MODEL_FOR_MULTIPLE_CHOICE_MAPPING, MODEL_FOR_QUESTION_ANSWERING_MAPPING, MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING, MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING, LayoutLMvaConfig, LayoutLMvaForQuestionAnswering, LayoutLMvaForSequenceClassification, LayoutLMvaForTokenClassification, LayoutLMvaModel, ) from transformers.models.layoutlmva.modeling_layoutlmva import LAYOUTLMV3_PRETRAINED_MODEL_ARCHIVE_LIST if is_vision_available(): from PIL import Image from transformers import LayoutLMvaImageProcessor class lowercase__: '''simple docstring''' def __init__( self :Tuple , lowerCamelCase_ :Tuple , lowerCamelCase_ :Union[str, Any]=2 , lowerCamelCase_ :Any=3 , lowerCamelCase_ :Union[str, Any]=4 , lowerCamelCase_ :List[str]=2 , lowerCamelCase_ :str=7 , lowerCamelCase_ :Tuple=True , lowerCamelCase_ :Dict=True , lowerCamelCase_ :int=True , lowerCamelCase_ :List[Any]=True , lowerCamelCase_ :Optional[Any]=99 , lowerCamelCase_ :Any=36 , lowerCamelCase_ :Any=3 , lowerCamelCase_ :str=4 , lowerCamelCase_ :Tuple=37 , lowerCamelCase_ :Optional[int]="gelu" , lowerCamelCase_ :str=0.1 , lowerCamelCase_ :Optional[int]=0.1 , lowerCamelCase_ :Tuple=5_12 , lowerCamelCase_ :Optional[Any]=16 , lowerCamelCase_ :List[str]=2 , lowerCamelCase_ :Optional[int]=0.0_2 , lowerCamelCase_ :int=6 , lowerCamelCase_ :str=6 , lowerCamelCase_ :Optional[Any]=3 , lowerCamelCase_ :Union[str, Any]=4 , lowerCamelCase_ :List[Any]=None , lowerCamelCase_ :Tuple=10_00 , ) -> Union[str, Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : Dict = parent SCREAMING_SNAKE_CASE : Optional[Any] = batch_size SCREAMING_SNAKE_CASE : List[str] = num_channels SCREAMING_SNAKE_CASE : str = image_size SCREAMING_SNAKE_CASE : Optional[int] = patch_size SCREAMING_SNAKE_CASE : Tuple = text_seq_length SCREAMING_SNAKE_CASE : Optional[int] = is_training SCREAMING_SNAKE_CASE : Dict = use_input_mask SCREAMING_SNAKE_CASE : Any = use_token_type_ids SCREAMING_SNAKE_CASE : List[Any] = use_labels SCREAMING_SNAKE_CASE : List[Any] = vocab_size SCREAMING_SNAKE_CASE : Optional[Any] = hidden_size SCREAMING_SNAKE_CASE : Tuple = num_hidden_layers SCREAMING_SNAKE_CASE : Optional[int] = num_attention_heads SCREAMING_SNAKE_CASE : Optional[int] = intermediate_size SCREAMING_SNAKE_CASE : List[str] = hidden_act SCREAMING_SNAKE_CASE : Any = hidden_dropout_prob SCREAMING_SNAKE_CASE : Optional[Any] = attention_probs_dropout_prob SCREAMING_SNAKE_CASE : Union[str, Any] = max_position_embeddings SCREAMING_SNAKE_CASE : int = type_vocab_size SCREAMING_SNAKE_CASE : Union[str, Any] = type_sequence_label_size SCREAMING_SNAKE_CASE : Optional[Any] = initializer_range SCREAMING_SNAKE_CASE : Dict = coordinate_size SCREAMING_SNAKE_CASE : List[Any] = shape_size SCREAMING_SNAKE_CASE : Dict = num_labels SCREAMING_SNAKE_CASE : Union[str, Any] = num_choices SCREAMING_SNAKE_CASE : List[str] = scope SCREAMING_SNAKE_CASE : Optional[int] = range_bbox # LayoutLMv3's sequence length equals the number of text tokens + number of patches + 1 (we add 1 for the CLS token) SCREAMING_SNAKE_CASE : str = text_seq_length SCREAMING_SNAKE_CASE : int = (image_size // patch_size) ** 2 + 1 SCREAMING_SNAKE_CASE : Optional[Any] = self.text_seq_length + self.image_seq_length def __lowerCAmelCase ( self :List[Any] ) -> Optional[int]: '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[int] = ids_tensor([self.batch_size, self.text_seq_length] , self.vocab_size ) SCREAMING_SNAKE_CASE : List[Any] = ids_tensor([self.batch_size, self.text_seq_length, 4] , self.range_bbox ) # Ensure that bbox is legal for i in range(bbox.shape[0] ): for j in range(bbox.shape[1] ): if bbox[i, j, 3] < bbox[i, j, 1]: SCREAMING_SNAKE_CASE : str = bbox[i, j, 3] SCREAMING_SNAKE_CASE : List[str] = bbox[i, j, 1] SCREAMING_SNAKE_CASE : Any = t if bbox[i, j, 2] < bbox[i, j, 0]: SCREAMING_SNAKE_CASE : Any = bbox[i, j, 2] SCREAMING_SNAKE_CASE : Any = bbox[i, j, 0] SCREAMING_SNAKE_CASE : Optional[Any] = t SCREAMING_SNAKE_CASE : Dict = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) SCREAMING_SNAKE_CASE : Optional[Any] = None if self.use_input_mask: SCREAMING_SNAKE_CASE : Dict = random_attention_mask([self.batch_size, self.text_seq_length] ) SCREAMING_SNAKE_CASE : Any = None if self.use_token_type_ids: SCREAMING_SNAKE_CASE : Tuple = ids_tensor([self.batch_size, self.text_seq_length] , self.type_vocab_size ) SCREAMING_SNAKE_CASE : Tuple = None SCREAMING_SNAKE_CASE : Optional[int] = None if self.use_labels: SCREAMING_SNAKE_CASE : List[str] = ids_tensor([self.batch_size] , self.type_sequence_label_size ) SCREAMING_SNAKE_CASE : Dict = ids_tensor([self.batch_size, self.text_seq_length] , self.num_labels ) SCREAMING_SNAKE_CASE : List[str] = 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 __lowerCAmelCase ( self :int , lowerCamelCase_ :Union[str, Any] , lowerCamelCase_ :int , lowerCamelCase_ :str , lowerCamelCase_ :int , lowerCamelCase_ :Union[str, Any] , lowerCamelCase_ :Any , lowerCamelCase_ :Dict , lowerCamelCase_ :Union[str, Any] ) -> Any: '''simple docstring''' SCREAMING_SNAKE_CASE : Dict = LayoutLMvaModel(config=lowerCamelCase_ ) model.to(lowerCamelCase_ ) model.eval() # text + image SCREAMING_SNAKE_CASE : Optional[Any] = model(lowerCamelCase_ , pixel_values=lowerCamelCase_ ) SCREAMING_SNAKE_CASE : List[str] = model( lowerCamelCase_ , bbox=lowerCamelCase_ , pixel_values=lowerCamelCase_ , attention_mask=lowerCamelCase_ , token_type_ids=lowerCamelCase_ ) SCREAMING_SNAKE_CASE : List[Any] = model(lowerCamelCase_ , bbox=lowerCamelCase_ , pixel_values=lowerCamelCase_ , token_type_ids=lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Optional[Any] = model(lowerCamelCase_ , bbox=lowerCamelCase_ , pixel_values=lowerCamelCase_ ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) # text only SCREAMING_SNAKE_CASE : List[str] = model(lowerCamelCase_ ) self.parent.assertEqual( result.last_hidden_state.shape , (self.batch_size, self.text_seq_length, self.hidden_size) ) # image only SCREAMING_SNAKE_CASE : List[str] = model(pixel_values=lowerCamelCase_ ) self.parent.assertEqual( result.last_hidden_state.shape , (self.batch_size, self.image_seq_length, self.hidden_size) ) def __lowerCAmelCase ( self :str , lowerCamelCase_ :Union[str, Any] , lowerCamelCase_ :Union[str, Any] , lowerCamelCase_ :Tuple , lowerCamelCase_ :List[str] , lowerCamelCase_ :Any , lowerCamelCase_ :int , lowerCamelCase_ :List[Any] , lowerCamelCase_ :List[str] ) -> Tuple: '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[Any] = self.num_labels SCREAMING_SNAKE_CASE : Dict = LayoutLMvaForSequenceClassification(lowerCamelCase_ ) model.to(lowerCamelCase_ ) model.eval() SCREAMING_SNAKE_CASE : int = model( lowerCamelCase_ , bbox=lowerCamelCase_ , pixel_values=lowerCamelCase_ , attention_mask=lowerCamelCase_ , token_type_ids=lowerCamelCase_ , labels=lowerCamelCase_ , ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_labels) ) def __lowerCAmelCase ( self :Union[str, Any] , lowerCamelCase_ :Tuple , lowerCamelCase_ :Any , lowerCamelCase_ :Optional[int] , lowerCamelCase_ :Any , lowerCamelCase_ :Optional[int] , lowerCamelCase_ :int , lowerCamelCase_ :str , lowerCamelCase_ :Any ) -> Union[str, Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : int = self.num_labels SCREAMING_SNAKE_CASE : int = LayoutLMvaForTokenClassification(config=lowerCamelCase_ ) model.to(lowerCamelCase_ ) model.eval() SCREAMING_SNAKE_CASE : Optional[Any] = model( lowerCamelCase_ , bbox=lowerCamelCase_ , pixel_values=lowerCamelCase_ , attention_mask=lowerCamelCase_ , token_type_ids=lowerCamelCase_ , labels=lowerCamelCase_ , ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.text_seq_length, self.num_labels) ) def __lowerCAmelCase ( self :Optional[Any] , lowerCamelCase_ :Dict , lowerCamelCase_ :Dict , lowerCamelCase_ :Dict , lowerCamelCase_ :int , lowerCamelCase_ :List[str] , lowerCamelCase_ :int , lowerCamelCase_ :str , lowerCamelCase_ :Optional[int] ) -> Union[str, Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[int] = LayoutLMvaForQuestionAnswering(config=lowerCamelCase_ ) model.to(lowerCamelCase_ ) model.eval() SCREAMING_SNAKE_CASE : List[Any] = model( lowerCamelCase_ , bbox=lowerCamelCase_ , pixel_values=lowerCamelCase_ , attention_mask=lowerCamelCase_ , token_type_ids=lowerCamelCase_ , start_positions=lowerCamelCase_ , end_positions=lowerCamelCase_ , ) self.parent.assertEqual(result.start_logits.shape , (self.batch_size, self.seq_length) ) self.parent.assertEqual(result.end_logits.shape , (self.batch_size, self.seq_length) ) def __lowerCAmelCase ( self :Optional[int] ) -> Optional[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[int] = self.prepare_config_and_inputs() ( ( SCREAMING_SNAKE_CASE ), ( SCREAMING_SNAKE_CASE ), ( SCREAMING_SNAKE_CASE ), ( SCREAMING_SNAKE_CASE ), ( SCREAMING_SNAKE_CASE ), ( SCREAMING_SNAKE_CASE ), ( SCREAMING_SNAKE_CASE ), ( SCREAMING_SNAKE_CASE ), ) : Tuple = config_and_inputs SCREAMING_SNAKE_CASE : Dict = { '''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_torch class lowercase__( _UpperCAmelCase , _UpperCAmelCase , unittest.TestCase ): '''simple docstring''' UpperCamelCase = False UpperCamelCase = False UpperCamelCase = False UpperCamelCase = ( ( LayoutLMvaModel, LayoutLMvaForSequenceClassification, LayoutLMvaForTokenClassification, LayoutLMvaForQuestionAnswering, ) if is_torch_available() else () ) UpperCamelCase = ( {"""document-question-answering""": LayoutLMvaForQuestionAnswering, """feature-extraction""": LayoutLMvaModel} if is_torch_available() else {} ) def __lowerCAmelCase ( self :int , lowerCamelCase_ :Union[str, Any] , lowerCamelCase_ :str , lowerCamelCase_ :str , lowerCamelCase_ :Optional[Any] , lowerCamelCase_ :List[str] ) -> Union[str, Any]: '''simple docstring''' return True def __lowerCAmelCase ( self :List[str] ) -> Union[str, Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : str = LayoutLMvaModelTester(self ) SCREAMING_SNAKE_CASE : List[Any] = ConfigTester(self , config_class=lowerCamelCase_ , hidden_size=37 ) def __lowerCAmelCase ( self :Any , lowerCamelCase_ :Dict , lowerCamelCase_ :List[str] , lowerCamelCase_ :str=False ) -> Tuple: '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[Any] = copy.deepcopy(lowerCamelCase_ ) if model_class in get_values(lowerCamelCase_ ): SCREAMING_SNAKE_CASE : Tuple = { k: v.unsqueeze(1 ).expand(-1 , self.model_tester.num_choices , -1 ).contiguous() if isinstance(lowerCamelCase_ , torch.Tensor ) and v.ndim > 1 else v for k, v in inputs_dict.items() } if return_labels: if model_class in get_values(lowerCamelCase_ ): SCREAMING_SNAKE_CASE : Union[str, Any] = torch.ones(self.model_tester.batch_size , dtype=torch.long , device=lowerCamelCase_ ) elif model_class in get_values(lowerCamelCase_ ): SCREAMING_SNAKE_CASE : List[Any] = torch.zeros( self.model_tester.batch_size , dtype=torch.long , device=lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Any = torch.zeros( self.model_tester.batch_size , dtype=torch.long , device=lowerCamelCase_ ) elif model_class in [ *get_values(lowerCamelCase_ ), ]: SCREAMING_SNAKE_CASE : str = torch.zeros( self.model_tester.batch_size , dtype=torch.long , device=lowerCamelCase_ ) elif model_class in [ *get_values(lowerCamelCase_ ), ]: SCREAMING_SNAKE_CASE : Dict = torch.zeros( (self.model_tester.batch_size, self.model_tester.text_seq_length) , dtype=torch.long , device=lowerCamelCase_ , ) return inputs_dict def __lowerCAmelCase ( self :Optional[Any] ) -> Optional[int]: '''simple docstring''' self.config_tester.run_common_tests() def __lowerCAmelCase ( self :str ) -> List[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : int = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*lowerCamelCase_ ) def __lowerCAmelCase ( self :Any ) -> List[str]: '''simple docstring''' SCREAMING_SNAKE_CASE : int = self.model_tester.prepare_config_and_inputs() for type in ["absolute", "relative_key", "relative_key_query"]: SCREAMING_SNAKE_CASE : str = type self.model_tester.create_and_check_model(*lowerCamelCase_ ) def __lowerCAmelCase ( self :Union[str, Any] ) -> str: '''simple docstring''' SCREAMING_SNAKE_CASE : List[Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_sequence_classification(*lowerCamelCase_ ) def __lowerCAmelCase ( self :str ) -> Any: '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_token_classification(*lowerCamelCase_ ) def __lowerCAmelCase ( self :List[str] ) -> Union[str, Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : int = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_question_answering(*lowerCamelCase_ ) @slow def __lowerCAmelCase ( self :int ) -> Union[str, Any]: '''simple docstring''' for model_name in LAYOUTLMV3_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: SCREAMING_SNAKE_CASE : Optional[int] = LayoutLMvaModel.from_pretrained(lowerCamelCase_ ) self.assertIsNotNone(lowerCamelCase_ ) def __A ( )-> Optional[int]: '''simple docstring''' SCREAMING_SNAKE_CASE : Tuple = Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png''' ) return image @require_torch class lowercase__( unittest.TestCase ): '''simple docstring''' @cached_property def __lowerCAmelCase ( self :str ) -> int: '''simple docstring''' return LayoutLMvaImageProcessor(apply_ocr=lowerCamelCase_ ) if is_vision_available() else None @slow def __lowerCAmelCase ( self :Optional[Any] ) -> Dict: '''simple docstring''' SCREAMING_SNAKE_CASE : Union[str, Any] = LayoutLMvaModel.from_pretrained('''microsoft/layoutlmv3-base''' ).to(lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Any = self.default_image_processor SCREAMING_SNAKE_CASE : List[Any] = prepare_img() SCREAMING_SNAKE_CASE : Tuple = image_processor(images=lowerCamelCase_ , return_tensors='''pt''' ).pixel_values.to(lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Optional[Any] = torch.tensor([[1, 2]] ) SCREAMING_SNAKE_CASE : Dict = torch.tensor([[1, 2, 3, 4], [5, 6, 7, 8]] ).unsqueeze(0 ) # forward pass SCREAMING_SNAKE_CASE : Tuple = model( input_ids=input_ids.to(lowerCamelCase_ ) , bbox=bbox.to(lowerCamelCase_ ) , pixel_values=pixel_values.to(lowerCamelCase_ ) , ) # verify the logits SCREAMING_SNAKE_CASE : Union[str, Any] = torch.Size((1, 1_99, 7_68) ) self.assertEqual(outputs.last_hidden_state.shape , lowerCamelCase_ ) SCREAMING_SNAKE_CASE : List[Any] = torch.tensor( [[-0.0_5_2_9, 0.3_6_1_8, 0.1_6_3_2], [-0.1_5_8_7, -0.1_6_6_7, -0.0_4_0_0], [-0.1_5_5_7, -0.1_6_7_1, -0.0_5_0_5]] ).to(lowerCamelCase_ ) self.assertTrue(torch.allclose(outputs.last_hidden_state[0, :3, :3] , lowerCamelCase_ , atol=1E-4 ) )
18
0
"""simple docstring""" import argparse import json import logging import os import shutil import sys import tempfile import unittest from unittest import mock import torch from accelerate.utils import write_basic_config from transformers.testing_utils import TestCasePlus, get_gpu_count, run_command, slow, torch_device from transformers.utils import is_apex_available logging.basicConfig(level=logging.DEBUG) lowerCamelCase__ : str = logging.getLogger() def __A ( )-> List[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[Any] = argparse.ArgumentParser() parser.add_argument('''-f''' ) SCREAMING_SNAKE_CASE : Dict = parser.parse_args() return args.f def __A ( a_ : Any )-> Optional[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : Tuple = {} SCREAMING_SNAKE_CASE : Optional[int] = os.path.join(__UpperCamelCase , '''all_results.json''' ) if os.path.exists(__UpperCamelCase ): with open(__UpperCamelCase , '''r''' ) as f: SCREAMING_SNAKE_CASE : Optional[int] = json.load(__UpperCamelCase ) else: raise ValueError(F"can't find {path}" ) return results def __A ( )-> Dict: '''simple docstring''' SCREAMING_SNAKE_CASE : str = torch.cuda.is_available() and torch_device == '''cuda''' return is_using_cuda and is_apex_available() lowerCamelCase__ : Tuple = logging.StreamHandler(sys.stdout) logger.addHandler(stream_handler) class lowercase__( SCREAMING_SNAKE_CASE_ ): '''simple docstring''' @classmethod def __lowerCAmelCase ( cls :Any ) -> int: '''simple docstring''' SCREAMING_SNAKE_CASE : Any = tempfile.mkdtemp() SCREAMING_SNAKE_CASE : List[Any] = os.path.join(cls.tmpdir , '''default_config.yml''' ) write_basic_config(save_location=cls.configPath ) SCREAMING_SNAKE_CASE : int = ['''accelerate''', '''launch''', '''--config_file''', cls.configPath] @classmethod def __lowerCAmelCase ( cls :Dict ) -> Optional[int]: '''simple docstring''' shutil.rmtree(cls.tmpdir ) @mock.patch.dict(os.environ , {'''WANDB_MODE''': '''offline'''} ) def __lowerCAmelCase ( self :Any ) -> Tuple: '''simple docstring''' SCREAMING_SNAKE_CASE : Dict = self.get_auto_remove_tmp_dir() SCREAMING_SNAKE_CASE : Optional[int] = f"\n {self.examples_dir}/pytorch/text-classification/run_glue_no_trainer.py\n --model_name_or_path distilbert-base-uncased\n --output_dir {tmp_dir}\n --train_file ./tests/fixtures/tests_samples/MRPC/train.csv\n --validation_file ./tests/fixtures/tests_samples/MRPC/dev.csv\n --per_device_train_batch_size=2\n --per_device_eval_batch_size=1\n --learning_rate=1e-4\n --seed=42\n --checkpointing_steps epoch\n --with_tracking\n ".split() if is_cuda_and_apex_available(): testargs.append('''--fp16''' ) run_command(self._launch_args + testargs ) SCREAMING_SNAKE_CASE : Tuple = get_results(UpperCamelCase__ ) self.assertGreaterEqual(result['''eval_accuracy'''] , 0.7_5 ) self.assertTrue(os.path.exists(os.path.join(UpperCamelCase__ , '''epoch_0''' ) ) ) self.assertTrue(os.path.exists(os.path.join(UpperCamelCase__ , '''glue_no_trainer''' ) ) ) @mock.patch.dict(os.environ , {'''WANDB_MODE''': '''offline'''} ) def __lowerCAmelCase ( self :List[Any] ) -> int: '''simple docstring''' SCREAMING_SNAKE_CASE : List[Any] = self.get_auto_remove_tmp_dir() SCREAMING_SNAKE_CASE : int = f"\n {self.examples_dir}/pytorch/language-modeling/run_clm_no_trainer.py\n --model_name_or_path distilgpt2\n --train_file ./tests/fixtures/sample_text.txt\n --validation_file ./tests/fixtures/sample_text.txt\n --block_size 128\n --per_device_train_batch_size 5\n --per_device_eval_batch_size 5\n --num_train_epochs 2\n --output_dir {tmp_dir}\n --checkpointing_steps epoch\n --with_tracking\n ".split() if torch.cuda.device_count() > 1: # Skipping because there are not enough batches to train the model + would need a drop_last to work. return run_command(self._launch_args + testargs ) SCREAMING_SNAKE_CASE : Optional[Any] = get_results(UpperCamelCase__ ) self.assertLess(result['''perplexity'''] , 1_00 ) self.assertTrue(os.path.exists(os.path.join(UpperCamelCase__ , '''epoch_0''' ) ) ) self.assertTrue(os.path.exists(os.path.join(UpperCamelCase__ , '''clm_no_trainer''' ) ) ) @mock.patch.dict(os.environ , {'''WANDB_MODE''': '''offline'''} ) def __lowerCAmelCase ( self :Any ) -> Tuple: '''simple docstring''' SCREAMING_SNAKE_CASE : Tuple = self.get_auto_remove_tmp_dir() SCREAMING_SNAKE_CASE : str = f"\n {self.examples_dir}/pytorch/language-modeling/run_mlm_no_trainer.py\n --model_name_or_path distilroberta-base\n --train_file ./tests/fixtures/sample_text.txt\n --validation_file ./tests/fixtures/sample_text.txt\n --output_dir {tmp_dir}\n --num_train_epochs=1\n --checkpointing_steps epoch\n --with_tracking\n ".split() run_command(self._launch_args + testargs ) SCREAMING_SNAKE_CASE : Optional[int] = get_results(UpperCamelCase__ ) self.assertLess(result['''perplexity'''] , 42 ) self.assertTrue(os.path.exists(os.path.join(UpperCamelCase__ , '''epoch_0''' ) ) ) self.assertTrue(os.path.exists(os.path.join(UpperCamelCase__ , '''mlm_no_trainer''' ) ) ) @mock.patch.dict(os.environ , {'''WANDB_MODE''': '''offline'''} ) def __lowerCAmelCase ( self :Any ) -> Optional[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[int] = 7 if get_gpu_count() > 1 else 2 SCREAMING_SNAKE_CASE : Dict = self.get_auto_remove_tmp_dir() SCREAMING_SNAKE_CASE : Dict = f"\n {self.examples_dir}/pytorch/token-classification/run_ner_no_trainer.py\n --model_name_or_path bert-base-uncased\n --train_file tests/fixtures/tests_samples/conll/sample.json\n --validation_file tests/fixtures/tests_samples/conll/sample.json\n --output_dir {tmp_dir}\n --learning_rate=2e-4\n --per_device_train_batch_size=2\n --per_device_eval_batch_size=2\n --num_train_epochs={epochs}\n --seed 7\n --checkpointing_steps epoch\n --with_tracking\n ".split() run_command(self._launch_args + testargs ) SCREAMING_SNAKE_CASE : str = get_results(UpperCamelCase__ ) self.assertGreaterEqual(result['''eval_accuracy'''] , 0.7_5 ) self.assertLess(result['''train_loss'''] , 0.5 ) self.assertTrue(os.path.exists(os.path.join(UpperCamelCase__ , '''epoch_0''' ) ) ) self.assertTrue(os.path.exists(os.path.join(UpperCamelCase__ , '''ner_no_trainer''' ) ) ) @unittest.skip(reason='''Fix me @muellerzr''' ) @mock.patch.dict(os.environ , {'''WANDB_MODE''': '''offline'''} ) def __lowerCAmelCase ( self :Any ) -> Union[str, Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : List[Any] = self.get_auto_remove_tmp_dir() SCREAMING_SNAKE_CASE : Any = f"\n {self.examples_dir}/pytorch/question-answering/run_qa_no_trainer.py\n --model_name_or_path bert-base-uncased\n --version_2_with_negative\n --train_file tests/fixtures/tests_samples/SQUAD/sample.json\n --validation_file tests/fixtures/tests_samples/SQUAD/sample.json\n --output_dir {tmp_dir}\n --seed=42\n --max_train_steps=10\n --num_warmup_steps=2\n --learning_rate=2e-4\n --per_device_train_batch_size=2\n --per_device_eval_batch_size=1\n --checkpointing_steps epoch\n --with_tracking\n ".split() run_command(self._launch_args + testargs ) SCREAMING_SNAKE_CASE : Tuple = get_results(UpperCamelCase__ ) # Because we use --version_2_with_negative the testing script uses SQuAD v2 metrics. self.assertGreaterEqual(result['''eval_f1'''] , 28 ) self.assertGreaterEqual(result['''eval_exact'''] , 28 ) self.assertTrue(os.path.exists(os.path.join(UpperCamelCase__ , '''epoch_0''' ) ) ) self.assertTrue(os.path.exists(os.path.join(UpperCamelCase__ , '''qa_no_trainer''' ) ) ) @mock.patch.dict(os.environ , {'''WANDB_MODE''': '''offline'''} ) def __lowerCAmelCase ( self :Any ) -> Union[str, Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[int] = self.get_auto_remove_tmp_dir() SCREAMING_SNAKE_CASE : Dict = f"\n {self.examples_dir}/pytorch/multiple-choice/run_swag_no_trainer.py\n --model_name_or_path bert-base-uncased\n --train_file tests/fixtures/tests_samples/swag/sample.json\n --validation_file tests/fixtures/tests_samples/swag/sample.json\n --output_dir {tmp_dir}\n --max_train_steps=20\n --num_warmup_steps=2\n --learning_rate=2e-4\n --per_device_train_batch_size=2\n --per_device_eval_batch_size=1\n --with_tracking\n ".split() run_command(self._launch_args + testargs ) SCREAMING_SNAKE_CASE : List[Any] = get_results(UpperCamelCase__ ) self.assertGreaterEqual(result['''eval_accuracy'''] , 0.8 ) self.assertTrue(os.path.exists(os.path.join(UpperCamelCase__ , '''swag_no_trainer''' ) ) ) @slow @mock.patch.dict(os.environ , {'''WANDB_MODE''': '''offline'''} ) def __lowerCAmelCase ( self :Optional[Any] ) -> str: '''simple docstring''' SCREAMING_SNAKE_CASE : List[Any] = self.get_auto_remove_tmp_dir() SCREAMING_SNAKE_CASE : Tuple = f"\n {self.examples_dir}/pytorch/summarization/run_summarization_no_trainer.py\n --model_name_or_path t5-small\n --train_file tests/fixtures/tests_samples/xsum/sample.json\n --validation_file tests/fixtures/tests_samples/xsum/sample.json\n --output_dir {tmp_dir}\n --max_train_steps=50\n --num_warmup_steps=8\n --learning_rate=2e-4\n --per_device_train_batch_size=2\n --per_device_eval_batch_size=1\n --checkpointing_steps epoch\n --with_tracking\n ".split() run_command(self._launch_args + testargs ) SCREAMING_SNAKE_CASE : Tuple = get_results(UpperCamelCase__ ) self.assertGreaterEqual(result['''eval_rouge1'''] , 10 ) self.assertGreaterEqual(result['''eval_rouge2'''] , 2 ) self.assertGreaterEqual(result['''eval_rougeL'''] , 7 ) self.assertGreaterEqual(result['''eval_rougeLsum'''] , 7 ) self.assertTrue(os.path.exists(os.path.join(UpperCamelCase__ , '''epoch_0''' ) ) ) self.assertTrue(os.path.exists(os.path.join(UpperCamelCase__ , '''summarization_no_trainer''' ) ) ) @slow @mock.patch.dict(os.environ , {'''WANDB_MODE''': '''offline'''} ) def __lowerCAmelCase ( self :Optional[int] ) -> Any: '''simple docstring''' SCREAMING_SNAKE_CASE : Any = self.get_auto_remove_tmp_dir() SCREAMING_SNAKE_CASE : Optional[int] = f"\n {self.examples_dir}/pytorch/translation/run_translation_no_trainer.py\n --model_name_or_path sshleifer/student_marian_en_ro_6_1\n --source_lang en\n --target_lang ro\n --train_file tests/fixtures/tests_samples/wmt16/sample.json\n --validation_file tests/fixtures/tests_samples/wmt16/sample.json\n --output_dir {tmp_dir}\n --max_train_steps=50\n --num_warmup_steps=8\n --num_beams=6\n --learning_rate=3e-3\n --per_device_train_batch_size=2\n --per_device_eval_batch_size=1\n --source_lang en_XX\n --target_lang ro_RO\n --checkpointing_steps epoch\n --with_tracking\n ".split() run_command(self._launch_args + testargs ) SCREAMING_SNAKE_CASE : Optional[int] = get_results(UpperCamelCase__ ) self.assertGreaterEqual(result['''eval_bleu'''] , 30 ) self.assertTrue(os.path.exists(os.path.join(UpperCamelCase__ , '''epoch_0''' ) ) ) self.assertTrue(os.path.exists(os.path.join(UpperCamelCase__ , '''translation_no_trainer''' ) ) ) @slow def __lowerCAmelCase ( self :List[Any] ) -> Optional[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : List[Any] = logging.StreamHandler(sys.stdout ) logger.addHandler(UpperCamelCase__ ) SCREAMING_SNAKE_CASE : Optional[int] = self.get_auto_remove_tmp_dir() SCREAMING_SNAKE_CASE : List[Any] = f"\n {self.examples_dir}/pytorch/semantic-segmentation/run_semantic_segmentation_no_trainer.py\n --dataset_name huggingface/semantic-segmentation-test-sample\n --output_dir {tmp_dir}\n --max_train_steps=10\n --num_warmup_steps=2\n --learning_rate=2e-4\n --per_device_train_batch_size=2\n --per_device_eval_batch_size=1\n --checkpointing_steps epoch\n ".split() run_command(self._launch_args + testargs ) SCREAMING_SNAKE_CASE : Dict = get_results(UpperCamelCase__ ) self.assertGreaterEqual(result['''eval_overall_accuracy'''] , 0.1_0 ) @mock.patch.dict(os.environ , {'''WANDB_MODE''': '''offline'''} ) def __lowerCAmelCase ( self :int ) -> int: '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[Any] = self.get_auto_remove_tmp_dir() SCREAMING_SNAKE_CASE : List[Any] = f"\n {self.examples_dir}/pytorch/image-classification/run_image_classification_no_trainer.py\n --model_name_or_path google/vit-base-patch16-224-in21k\n --dataset_name hf-internal-testing/cats_vs_dogs_sample\n --learning_rate 1e-4\n --per_device_train_batch_size 2\n --per_device_eval_batch_size 1\n --max_train_steps 2\n --train_val_split 0.1\n --seed 42\n --output_dir {tmp_dir}\n --with_tracking\n --checkpointing_steps 1\n ".split() if is_cuda_and_apex_available(): testargs.append('''--fp16''' ) run_command(self._launch_args + testargs ) SCREAMING_SNAKE_CASE : Dict = get_results(UpperCamelCase__ ) # The base model scores a 25% self.assertGreaterEqual(result['''eval_accuracy'''] , 0.6 ) self.assertTrue(os.path.exists(os.path.join(UpperCamelCase__ , '''step_1''' ) ) ) self.assertTrue(os.path.exists(os.path.join(UpperCamelCase__ , '''image_classification_no_trainer''' ) ) )
710
"""simple docstring""" import logging import os import random import sys from dataclasses import dataclass, field from typing import Optional import datasets import numpy as np import pandas as pd from datasets import load_dataset import transformers from transformers import ( AutoConfig, BartForSequenceClassification, DataCollatorWithPadding, EvalPrediction, HfArgumentParser, TapexTokenizer, Trainer, TrainingArguments, default_data_collator, set_seed, ) from transformers.trainer_utils import get_last_checkpoint from transformers.utils import check_min_version from transformers.utils.versions import require_version # Will error if the minimal version of Transformers is not installed. Remove at your own risks. check_min_version("4.17.0.dev0") require_version("datasets>=1.8.0", "To fix: pip install -r examples/pytorch/text-classification/requirements.txt") lowerCamelCase__ : Any = logging.getLogger(__name__) @dataclass class lowercase__: '''simple docstring''' UpperCamelCase = field( default="""tab_fact""" , metadata={"""help""": """The name of the dataset to use (via the datasets library)."""} ) UpperCamelCase = field( default="""tab_fact""" , metadata={"""help""": """The configuration name of the dataset to use (via the datasets library)."""} , ) UpperCamelCase = field( default=10_24 , metadata={ """help""": ( """The maximum total input sequence length after tokenization. Sequences longer """ """than this will be truncated, sequences shorter will be padded.""" ) } , ) UpperCamelCase = field( default=_UpperCAmelCase , metadata={"""help""": """Overwrite the cached preprocessed datasets or not."""} ) UpperCamelCase = field( default=_UpperCAmelCase , metadata={ """help""": ( """Whether to pad all samples to `max_seq_length`. """ """If False, will pad the samples dynamically when batching to the maximum length in the batch.""" ) } , ) UpperCamelCase = field( default=_UpperCAmelCase , metadata={ """help""": ( """For debugging purposes or quicker training, truncate the number of training examples to this """ """value if set.""" ) } , ) UpperCamelCase = field( default=_UpperCAmelCase , metadata={ """help""": ( """For debugging purposes or quicker training, truncate the number of evaluation examples to this """ """value if set.""" ) } , ) UpperCamelCase = field( default=_UpperCAmelCase , metadata={ """help""": ( """For debugging purposes or quicker training, truncate the number of prediction examples to this """ """value if set.""" ) } , ) UpperCamelCase = field( default=_UpperCAmelCase , metadata={"""help""": """A csv or a json file containing the training data."""} ) UpperCamelCase = field( default=_UpperCAmelCase , metadata={"""help""": """A csv or a json file containing the validation data."""} ) UpperCamelCase = field(default=_UpperCAmelCase , metadata={"""help""": """A csv or a json file containing the test data."""} ) def __lowerCAmelCase ( self :Union[str, Any] ) -> Dict: '''simple docstring''' if self.dataset_name is not None: pass elif self.train_file is None or self.validation_file is None: raise ValueError('''Need either a GLUE task, a training/validation file or a dataset name.''' ) else: SCREAMING_SNAKE_CASE : Union[str, Any] = self.train_file.split('''.''' )[-1] assert train_extension in ["csv", "json"], "`train_file` should be a csv or a json file." SCREAMING_SNAKE_CASE : Optional[int] = self.validation_file.split('''.''' )[-1] assert ( validation_extension == train_extension ), "`validation_file` should have the same extension (csv or json) as `train_file`." @dataclass class lowercase__: '''simple docstring''' UpperCamelCase = field( default=_UpperCAmelCase , metadata={"""help""": """Path to pretrained model or model identifier from huggingface.co/models"""} ) UpperCamelCase = field( default=_UpperCAmelCase , metadata={"""help""": """Pretrained config name or path if not the same as model_name"""} ) UpperCamelCase = field( default=_UpperCAmelCase , metadata={"""help""": """Pretrained tokenizer name or path if not the same as model_name"""} ) UpperCamelCase = field( default=_UpperCAmelCase , metadata={"""help""": """Where do you want to store the pretrained models downloaded from huggingface.co"""} , ) UpperCamelCase = field( default=_UpperCAmelCase , metadata={"""help""": """Whether to use one of the fast tokenizer (backed by the tokenizers library) or not."""} , ) UpperCamelCase = field( default="""main""" , metadata={"""help""": """The specific model version to use (can be a branch name, tag name or commit id)."""} , ) UpperCamelCase = field( default=_UpperCAmelCase , metadata={ """help""": ( """Will use the token generated when running `huggingface-cli login` (necessary to use this script """ """with private models).""" ) } , ) def __A ( )-> List[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : Union[str, Any] = 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 : Dict = parser.parse_json_file(json_file=os.path.abspath(sys.argv[1] ) ) else: SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : Dict = parser.parse_args_into_dataclasses() # 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 )] , ) SCREAMING_SNAKE_CASE : Union[str, Any] = training_args.get_process_log_level() logger.setLevel(a_ ) datasets.utils.logging.set_verbosity(a_ ) transformers.utils.logging.set_verbosity(a_ ) 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 : 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 ) # Get the datasets: you can either provide your own CSV/JSON training and evaluation files (see below) # or specify a GLUE benchmark task (the dataset will be downloaded automatically from the datasets Hub). # # For JSON files, this script will use the `question` column for the input question and `table` column for the corresponding table. # # If the CSVs/JSONs contain only one non-label column, the script does single sentence classification on this # single column. You can easily tweak this behavior (see below) # # In distributed training, the load_dataset function guarantee that only one local process can concurrently # download the dataset. if data_args.dataset_name is not None: # Downloading and loading a dataset from the hub. SCREAMING_SNAKE_CASE : List[str] = load_dataset( data_args.dataset_name , data_args.dataset_config_name , cache_dir=model_args.cache_dir ) else: # Loading a dataset from your local files. # CSV/JSON training and evaluation files are needed. SCREAMING_SNAKE_CASE : Any = {'''train''': data_args.train_file, '''validation''': data_args.validation_file} # Get the test dataset: you can provide your own CSV/JSON test file (see below) # when you use `do_predict` without specifying a GLUE benchmark task. if training_args.do_predict: if data_args.test_file is not None: SCREAMING_SNAKE_CASE : List[Any] = data_args.train_file.split('''.''' )[-1] SCREAMING_SNAKE_CASE : Optional[int] = data_args.test_file.split('''.''' )[-1] assert ( test_extension == train_extension ), "`test_file` should have the same extension (csv or json) as `train_file`." SCREAMING_SNAKE_CASE : str = data_args.test_file else: raise ValueError('''Need either a GLUE task or a test file for `do_predict`.''' ) for key in data_files.keys(): logger.info(F"load a local file for {key}: {data_files[key]}" ) if data_args.train_file.endswith('''.csv''' ): # Loading a dataset from local csv files SCREAMING_SNAKE_CASE : int = load_dataset('''csv''' , data_files=a_ , cache_dir=model_args.cache_dir ) else: # Loading a dataset from local json files SCREAMING_SNAKE_CASE : Tuple = load_dataset('''json''' , data_files=a_ , cache_dir=model_args.cache_dir ) # See more about loading any type of standard or custom dataset at # https://huggingface.co/docs/datasets/loading_datasets.html. # Labels SCREAMING_SNAKE_CASE : str = raw_datasets['''train'''].features['''label'''].names SCREAMING_SNAKE_CASE : Union[str, Any] = len(a_ ) # Load pretrained model and tokenizer # # In distributed training, the .from_pretrained methods guarantee that only one local process can concurrently # download model & vocab. SCREAMING_SNAKE_CASE : Optional[Any] = AutoConfig.from_pretrained( model_args.config_name if model_args.config_name else model_args.model_name_or_path , num_labels=a_ , cache_dir=model_args.cache_dir , revision=model_args.model_revision , use_auth_token=True if model_args.use_auth_token else None , ) # load tapex tokenizer SCREAMING_SNAKE_CASE : Dict = TapexTokenizer.from_pretrained( model_args.tokenizer_name if model_args.tokenizer_name else model_args.model_name_or_path , cache_dir=model_args.cache_dir , use_fast=model_args.use_fast_tokenizer , revision=model_args.model_revision , use_auth_token=True if model_args.use_auth_token else None , add_prefix_space=a_ , ) SCREAMING_SNAKE_CASE : List[Any] = BartForSequenceClassification.from_pretrained( model_args.model_name_or_path , from_tf=bool('''.ckpt''' in model_args.model_name_or_path ) , config=a_ , cache_dir=model_args.cache_dir , revision=model_args.model_revision , use_auth_token=True if model_args.use_auth_token else None , ) # Padding strategy if data_args.pad_to_max_length: SCREAMING_SNAKE_CASE : Tuple = '''max_length''' else: # We will pad later, dynamically at batch creation, to the max sequence length in each batch SCREAMING_SNAKE_CASE : Optional[Any] = False # Some models have set the order of the labels to use, so let's make sure we do use it. SCREAMING_SNAKE_CASE : Tuple = {'''Refused''': 0, '''Entailed''': 1} SCREAMING_SNAKE_CASE : List[Any] = {0: '''Refused''', 1: '''Entailed'''} if data_args.max_seq_length > tokenizer.model_max_length: logger.warning( F"The max_seq_length passed ({data_args.max_seq_length}) is larger than the maximum length for the" F"model ({tokenizer.model_max_length}). Using max_seq_length={tokenizer.model_max_length}." ) SCREAMING_SNAKE_CASE : Optional[int] = min(data_args.max_seq_length , tokenizer.model_max_length ) def preprocess_tabfact_function(a_ : str ): # Tokenize the texts def _convert_table_text_to_pandas(a_ : List[Any] ): SCREAMING_SNAKE_CASE : List[Any] = [_table_row.split('''#''' ) for _table_row in _table_text.strip('''\n''' ).split('''\n''' )] SCREAMING_SNAKE_CASE : Dict = pd.DataFrame.from_records(_table_content[1:] , columns=_table_content[0] ) return _table_pd SCREAMING_SNAKE_CASE : List[Any] = examples['''statement'''] SCREAMING_SNAKE_CASE : Optional[int] = list(map(_convert_table_text_to_pandas , examples['''table_text'''] ) ) SCREAMING_SNAKE_CASE : Any = tokenizer(a_ , a_ , padding=a_ , max_length=a_ , truncation=a_ ) SCREAMING_SNAKE_CASE : List[Any] = examples['''label'''] return result with training_args.main_process_first(desc='''dataset map pre-processing''' ): SCREAMING_SNAKE_CASE : Optional[Any] = raw_datasets.map( a_ , batched=a_ , load_from_cache_file=not data_args.overwrite_cache , desc='''Running tokenizer on dataset''' , ) if training_args.do_train: if "train" not in raw_datasets: raise ValueError('''--do_train requires a train dataset''' ) SCREAMING_SNAKE_CASE : List[str] = raw_datasets['''train'''] if data_args.max_train_samples is not None: SCREAMING_SNAKE_CASE : Any = train_dataset.select(range(data_args.max_train_samples ) ) if training_args.do_eval: if "validation" not in raw_datasets and "validation_matched" not in raw_datasets: raise ValueError('''--do_eval requires a validation dataset''' ) SCREAMING_SNAKE_CASE : List[str] = raw_datasets['''validation'''] if data_args.max_eval_samples is not None: SCREAMING_SNAKE_CASE : Union[str, Any] = eval_dataset.select(range(data_args.max_eval_samples ) ) if training_args.do_predict or data_args.test_file is not None: if "test" not in raw_datasets and "test_matched" not in raw_datasets: raise ValueError('''--do_predict requires a test dataset''' ) SCREAMING_SNAKE_CASE : Tuple = raw_datasets['''test'''] if data_args.max_predict_samples is not None: SCREAMING_SNAKE_CASE : Optional[int] = predict_dataset.select(range(data_args.max_predict_samples ) ) # Log a few random samples from the training set: if training_args.do_train: for index in random.sample(range(len(a_ ) ) , 3 ): logger.info(F"Sample {index} of the training set: {train_dataset[index]}." ) # You can define your custom 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(a_ : EvalPrediction ): SCREAMING_SNAKE_CASE : str = p.predictions[0] if isinstance(p.predictions , a_ ) else p.predictions SCREAMING_SNAKE_CASE : Tuple = np.argmax(a_ , axis=1 ) return {"accuracy": (preds == p.label_ids).astype(np.floataa ).mean().item()} # Data collator will default to DataCollatorWithPadding, so we change it if we already did the padding. if data_args.pad_to_max_length: SCREAMING_SNAKE_CASE : Tuple = default_data_collator elif training_args.fpaa: SCREAMING_SNAKE_CASE : Union[str, Any] = DataCollatorWithPadding(a_ , pad_to_multiple_of=8 ) else: SCREAMING_SNAKE_CASE : List[Any] = None # Initialize our Trainer SCREAMING_SNAKE_CASE : Optional[Any] = Trainer( model=a_ , args=a_ , train_dataset=train_dataset if training_args.do_train else None , eval_dataset=eval_dataset if training_args.do_eval else None , compute_metrics=a_ , tokenizer=a_ , data_collator=a_ , ) # Training if training_args.do_train: SCREAMING_SNAKE_CASE : List[str] = None if training_args.resume_from_checkpoint is not None: SCREAMING_SNAKE_CASE : Dict = training_args.resume_from_checkpoint elif last_checkpoint is not None: SCREAMING_SNAKE_CASE : str = last_checkpoint SCREAMING_SNAKE_CASE : str = trainer.train(resume_from_checkpoint=a_ ) SCREAMING_SNAKE_CASE : Optional[int] = train_result.metrics SCREAMING_SNAKE_CASE : int = ( data_args.max_train_samples if data_args.max_train_samples is not None else len(a_ ) ) SCREAMING_SNAKE_CASE : Optional[int] = min(a_ , len(a_ ) ) trainer.save_model() # Saves the tokenizer too for easy upload trainer.log_metrics('''train''' , a_ ) trainer.save_metrics('''train''' , a_ ) trainer.save_state() # Evaluation if training_args.do_eval: logger.info('''*** Evaluate ***''' ) SCREAMING_SNAKE_CASE : Tuple = trainer.evaluate(eval_dataset=a_ ) SCREAMING_SNAKE_CASE : str = data_args.max_eval_samples if data_args.max_eval_samples is not None else len(a_ ) SCREAMING_SNAKE_CASE : Union[str, Any] = min(a_ , len(a_ ) ) trainer.log_metrics('''eval''' , a_ ) trainer.save_metrics('''eval''' , a_ ) if training_args.do_predict: logger.info('''*** Predict ***''' ) # Removing the `label` columns because it contains -1 and Trainer won't like that. SCREAMING_SNAKE_CASE : Optional[Any] = predict_dataset.remove_columns('''label''' ) SCREAMING_SNAKE_CASE : Optional[Any] = trainer.predict(a_ , metric_key_prefix='''predict''' ).predictions SCREAMING_SNAKE_CASE : Union[str, Any] = np.argmax(a_ , axis=1 ) SCREAMING_SNAKE_CASE : Optional[int] = os.path.join(training_args.output_dir , '''predict_results_tabfact.txt''' ) if trainer.is_world_process_zero(): with open(a_ , '''w''' ) as writer: logger.info('''***** Predict Results *****''' ) writer.write('''index\tprediction\n''' ) for index, item in enumerate(a_ ): SCREAMING_SNAKE_CASE : Union[str, Any] = label_list[item] writer.write(F"{index}\t{item}\n" ) SCREAMING_SNAKE_CASE : Optional[int] = {'''finetuned_from''': model_args.model_name_or_path, '''tasks''': '''text-classification'''} if training_args.push_to_hub: trainer.push_to_hub(**a_ ) else: trainer.create_model_card(**a_ ) def __A ( a_ : List[str] )-> int: '''simple docstring''' main() if __name__ == "__main__": main()
18
0
"""simple docstring""" from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_torch_available, is_vision_available, ) lowerCamelCase__ : Any = {"configuration_beit": ["BEIT_PRETRAINED_CONFIG_ARCHIVE_MAP", "BeitConfig", "BeitOnnxConfig"]} try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCamelCase__ : Any = ["BeitFeatureExtractor"] lowerCamelCase__ : List[Any] = ["BeitImageProcessor"] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCamelCase__ : Union[str, Any] = [ "BEIT_PRETRAINED_MODEL_ARCHIVE_LIST", "BeitForImageClassification", "BeitForMaskedImageModeling", "BeitForSemanticSegmentation", "BeitModel", "BeitPreTrainedModel", ] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCamelCase__ : Dict = [ "FlaxBeitForImageClassification", "FlaxBeitForMaskedImageModeling", "FlaxBeitModel", "FlaxBeitPreTrainedModel", ] if TYPE_CHECKING: from .configuration_beit import BEIT_PRETRAINED_CONFIG_ARCHIVE_MAP, BeitConfig, BeitOnnxConfig try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .feature_extraction_beit import BeitFeatureExtractor from .image_processing_beit import BeitImageProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_beit import ( BEIT_PRETRAINED_MODEL_ARCHIVE_LIST, BeitForImageClassification, BeitForMaskedImageModeling, BeitForSemanticSegmentation, BeitModel, BeitPreTrainedModel, ) try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_flax_beit import ( FlaxBeitForImageClassification, FlaxBeitForMaskedImageModeling, FlaxBeitModel, FlaxBeitPreTrainedModel, ) else: import sys lowerCamelCase__ : Union[str, Any] = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
711
"""simple docstring""" import collections import inspect import unittest from transformers import SwinvaConfig from transformers.testing_utils import require_torch, require_vision, slow, torch_device from transformers.utils import cached_property, is_torch_available, is_vision_available from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, _config_zero_init, floats_tensor, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from torch import nn from transformers import SwinvaForImageClassification, SwinvaForMaskedImageModeling, SwinvaModel from transformers.models.swinva.modeling_swinva import SWINV2_PRETRAINED_MODEL_ARCHIVE_LIST if is_vision_available(): from PIL import Image from transformers import AutoImageProcessor class lowercase__: '''simple docstring''' def __init__( self :str , lowerCamelCase_ :List[Any] , lowerCamelCase_ :Any=13 , lowerCamelCase_ :Any=32 , lowerCamelCase_ :Union[str, Any]=2 , lowerCamelCase_ :Any=3 , lowerCamelCase_ :Union[str, Any]=16 , lowerCamelCase_ :int=[1, 2, 1] , lowerCamelCase_ :str=[2, 2, 4] , lowerCamelCase_ :str=2 , lowerCamelCase_ :Tuple=2.0 , lowerCamelCase_ :Union[str, Any]=True , lowerCamelCase_ :str=0.0 , lowerCamelCase_ :Optional[int]=0.0 , lowerCamelCase_ :Dict=0.1 , lowerCamelCase_ :Union[str, Any]="gelu" , lowerCamelCase_ :str=False , lowerCamelCase_ :List[Any]=True , lowerCamelCase_ :int=0.0_2 , lowerCamelCase_ :List[Any]=1E-5 , lowerCamelCase_ :int=True , lowerCamelCase_ :str=None , lowerCamelCase_ :List[Any]=True , lowerCamelCase_ :Union[str, Any]=10 , lowerCamelCase_ :List[Any]=8 , ) -> int: '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[Any] = parent SCREAMING_SNAKE_CASE : int = batch_size SCREAMING_SNAKE_CASE : int = image_size SCREAMING_SNAKE_CASE : Union[str, Any] = patch_size SCREAMING_SNAKE_CASE : Tuple = num_channels SCREAMING_SNAKE_CASE : Any = embed_dim SCREAMING_SNAKE_CASE : int = depths SCREAMING_SNAKE_CASE : List[str] = num_heads SCREAMING_SNAKE_CASE : Union[str, Any] = window_size SCREAMING_SNAKE_CASE : Optional[Any] = mlp_ratio SCREAMING_SNAKE_CASE : List[Any] = qkv_bias SCREAMING_SNAKE_CASE : Union[str, Any] = hidden_dropout_prob SCREAMING_SNAKE_CASE : Dict = attention_probs_dropout_prob SCREAMING_SNAKE_CASE : List[str] = drop_path_rate SCREAMING_SNAKE_CASE : List[Any] = hidden_act SCREAMING_SNAKE_CASE : Optional[Any] = use_absolute_embeddings SCREAMING_SNAKE_CASE : Any = patch_norm SCREAMING_SNAKE_CASE : Union[str, Any] = layer_norm_eps SCREAMING_SNAKE_CASE : List[Any] = initializer_range SCREAMING_SNAKE_CASE : Any = is_training SCREAMING_SNAKE_CASE : List[Any] = scope SCREAMING_SNAKE_CASE : Optional[Any] = use_labels SCREAMING_SNAKE_CASE : Optional[Any] = type_sequence_label_size SCREAMING_SNAKE_CASE : Optional[Any] = encoder_stride def __lowerCAmelCase ( self :Optional[int] ) -> List[str]: '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[int] = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) SCREAMING_SNAKE_CASE : int = None if self.use_labels: SCREAMING_SNAKE_CASE : Dict = ids_tensor([self.batch_size] , self.type_sequence_label_size ) SCREAMING_SNAKE_CASE : Tuple = self.get_config() return config, pixel_values, labels def __lowerCAmelCase ( self :int ) -> int: '''simple docstring''' return SwinvaConfig( image_size=self.image_size , patch_size=self.patch_size , num_channels=self.num_channels , embed_dim=self.embed_dim , depths=self.depths , num_heads=self.num_heads , window_size=self.window_size , mlp_ratio=self.mlp_ratio , qkv_bias=self.qkv_bias , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , drop_path_rate=self.drop_path_rate , hidden_act=self.hidden_act , use_absolute_embeddings=self.use_absolute_embeddings , path_norm=self.patch_norm , layer_norm_eps=self.layer_norm_eps , initializer_range=self.initializer_range , encoder_stride=self.encoder_stride , ) def __lowerCAmelCase ( self :List[Any] , lowerCamelCase_ :int , lowerCamelCase_ :Optional[int] , lowerCamelCase_ :Optional[Any] ) -> Union[str, Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : str = SwinvaModel(config=lowerCamelCase_ ) model.to(lowerCamelCase_ ) model.eval() SCREAMING_SNAKE_CASE : int = model(lowerCamelCase_ ) SCREAMING_SNAKE_CASE : List[Any] = ((config.image_size // config.patch_size) ** 2) // (4 ** (len(config.depths ) - 1)) SCREAMING_SNAKE_CASE : Dict = int(config.embed_dim * 2 ** (len(config.depths ) - 1) ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, expected_seq_len, expected_dim) ) def __lowerCAmelCase ( self :str , lowerCamelCase_ :str , lowerCamelCase_ :Tuple , lowerCamelCase_ :Any ) -> Union[str, Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : List[str] = SwinvaForMaskedImageModeling(config=lowerCamelCase_ ) model.to(lowerCamelCase_ ) model.eval() SCREAMING_SNAKE_CASE : Dict = model(lowerCamelCase_ ) self.parent.assertEqual( result.logits.shape , (self.batch_size, self.num_channels, self.image_size, self.image_size) ) # test greyscale images SCREAMING_SNAKE_CASE : Tuple = 1 SCREAMING_SNAKE_CASE : List[Any] = SwinvaForMaskedImageModeling(lowerCamelCase_ ) model.to(lowerCamelCase_ ) model.eval() SCREAMING_SNAKE_CASE : List[str] = floats_tensor([self.batch_size, 1, self.image_size, self.image_size] ) SCREAMING_SNAKE_CASE : Tuple = model(lowerCamelCase_ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, 1, self.image_size, self.image_size) ) def __lowerCAmelCase ( self :Tuple , lowerCamelCase_ :Union[str, Any] , lowerCamelCase_ :Any , lowerCamelCase_ :int ) -> Tuple: '''simple docstring''' SCREAMING_SNAKE_CASE : Tuple = self.type_sequence_label_size SCREAMING_SNAKE_CASE : Optional[Any] = SwinvaForImageClassification(lowerCamelCase_ ) model.to(lowerCamelCase_ ) model.eval() SCREAMING_SNAKE_CASE : Optional[int] = model(lowerCamelCase_ , labels=lowerCamelCase_ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.type_sequence_label_size) ) def __lowerCAmelCase ( self :List[Any] ) -> List[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : List[str] = self.prepare_config_and_inputs() SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : Optional[int] = config_and_inputs SCREAMING_SNAKE_CASE : int = {'''pixel_values''': pixel_values} return config, inputs_dict @require_torch class lowercase__( _UpperCAmelCase , _UpperCAmelCase , unittest.TestCase ): '''simple docstring''' UpperCamelCase = ( (SwinvaModel, SwinvaForImageClassification, SwinvaForMaskedImageModeling) if is_torch_available() else () ) UpperCamelCase = ( {"""feature-extraction""": SwinvaModel, """image-classification""": SwinvaForImageClassification} if is_torch_available() else {} ) UpperCamelCase = False UpperCamelCase = False UpperCamelCase = False UpperCamelCase = False def __lowerCAmelCase ( self :Union[str, Any] ) -> Union[str, Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : int = SwinvaModelTester(self ) SCREAMING_SNAKE_CASE : Dict = ConfigTester(self , config_class=lowerCamelCase_ , embed_dim=37 ) def __lowerCAmelCase ( self :Dict ) -> List[str]: '''simple docstring''' self.config_tester.create_and_test_config_to_json_string() self.config_tester.create_and_test_config_to_json_file() self.config_tester.create_and_test_config_from_and_save_pretrained() self.config_tester.create_and_test_config_with_num_labels() self.config_tester.check_config_can_be_init_without_params() self.config_tester.check_config_arguments_init() def __lowerCAmelCase ( self :List[Any] ) -> List[str]: '''simple docstring''' SCREAMING_SNAKE_CASE : Union[str, Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*lowerCamelCase_ ) @unittest.skip(reason='''Got `CUDA error: misaligned address` with PyTorch 2.0.0.''' ) def __lowerCAmelCase ( self :str ) -> List[Any]: '''simple docstring''' pass @unittest.skip(reason='''Swinv2 does not use inputs_embeds''' ) def __lowerCAmelCase ( self :List[Any] ) -> Any: '''simple docstring''' pass def __lowerCAmelCase ( self :int ) -> Union[str, Any]: '''simple docstring''' SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : Optional[int] = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: SCREAMING_SNAKE_CASE : List[Any] = model_class(lowerCamelCase_ ) self.assertIsInstance(model.get_input_embeddings() , (nn.Module) ) SCREAMING_SNAKE_CASE : str = model.get_output_embeddings() self.assertTrue(x is None or isinstance(lowerCamelCase_ , nn.Linear ) ) def __lowerCAmelCase ( self :int ) -> List[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : Tuple = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: SCREAMING_SNAKE_CASE : Optional[Any] = model_class(lowerCamelCase_ ) SCREAMING_SNAKE_CASE : List[str] = inspect.signature(model.forward ) # signature.parameters is an OrderedDict => so arg_names order is deterministic SCREAMING_SNAKE_CASE : Optional[int] = [*signature.parameters.keys()] SCREAMING_SNAKE_CASE : Union[str, Any] = ['''pixel_values'''] self.assertListEqual(arg_names[:1] , lowerCamelCase_ ) def __lowerCAmelCase ( self :int ) -> Optional[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : str = self.model_tester.prepare_config_and_inputs_for_common() SCREAMING_SNAKE_CASE : Tuple = True for model_class in self.all_model_classes: SCREAMING_SNAKE_CASE : List[str] = True SCREAMING_SNAKE_CASE : List[Any] = False SCREAMING_SNAKE_CASE : Any = True SCREAMING_SNAKE_CASE : Any = model_class(lowerCamelCase_ ) model.to(lowerCamelCase_ ) model.eval() with torch.no_grad(): SCREAMING_SNAKE_CASE : str = model(**self._prepare_for_class(lowerCamelCase_ , lowerCamelCase_ ) ) SCREAMING_SNAKE_CASE : Tuple = outputs.attentions SCREAMING_SNAKE_CASE : Tuple = len(self.model_tester.depths ) self.assertEqual(len(lowerCamelCase_ ) , lowerCamelCase_ ) # check that output_attentions also work using config del inputs_dict["output_attentions"] SCREAMING_SNAKE_CASE : Union[str, Any] = True SCREAMING_SNAKE_CASE : Optional[int] = config.window_size**2 SCREAMING_SNAKE_CASE : str = model_class(lowerCamelCase_ ) model.to(lowerCamelCase_ ) model.eval() with torch.no_grad(): SCREAMING_SNAKE_CASE : Union[str, Any] = model(**self._prepare_for_class(lowerCamelCase_ , lowerCamelCase_ ) ) SCREAMING_SNAKE_CASE : Dict = outputs.attentions self.assertEqual(len(lowerCamelCase_ ) , lowerCamelCase_ ) self.assertListEqual( list(attentions[0].shape[-3:] ) , [self.model_tester.num_heads[0], window_size_squared, window_size_squared] , ) SCREAMING_SNAKE_CASE : Dict = len(lowerCamelCase_ ) # Check attention is always last and order is fine SCREAMING_SNAKE_CASE : int = True SCREAMING_SNAKE_CASE : Dict = True SCREAMING_SNAKE_CASE : int = model_class(lowerCamelCase_ ) model.to(lowerCamelCase_ ) model.eval() with torch.no_grad(): SCREAMING_SNAKE_CASE : List[Any] = model(**self._prepare_for_class(lowerCamelCase_ , lowerCamelCase_ ) ) if hasattr(self.model_tester , '''num_hidden_states_types''' ): SCREAMING_SNAKE_CASE : Any = self.model_tester.num_hidden_states_types else: # also another +1 for reshaped_hidden_states SCREAMING_SNAKE_CASE : Optional[Any] = 2 self.assertEqual(out_len + added_hidden_states , len(lowerCamelCase_ ) ) SCREAMING_SNAKE_CASE : Tuple = outputs.attentions self.assertEqual(len(lowerCamelCase_ ) , lowerCamelCase_ ) self.assertListEqual( list(self_attentions[0].shape[-3:] ) , [self.model_tester.num_heads[0], window_size_squared, window_size_squared] , ) def __lowerCAmelCase ( self :Dict , lowerCamelCase_ :int , lowerCamelCase_ :Any , lowerCamelCase_ :Tuple , lowerCamelCase_ :Any ) -> Optional[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : Union[str, Any] = model_class(lowerCamelCase_ ) model.to(lowerCamelCase_ ) model.eval() with torch.no_grad(): SCREAMING_SNAKE_CASE : Optional[int] = model(**self._prepare_for_class(lowerCamelCase_ , lowerCamelCase_ ) ) SCREAMING_SNAKE_CASE : Dict = outputs.hidden_states SCREAMING_SNAKE_CASE : str = getattr( self.model_tester , '''expected_num_hidden_layers''' , len(self.model_tester.depths ) + 1 ) self.assertEqual(len(lowerCamelCase_ ) , lowerCamelCase_ ) # Swinv2 has a different seq_length SCREAMING_SNAKE_CASE : List[str] = ( config.patch_size if isinstance(config.patch_size , collections.abc.Iterable ) else (config.patch_size, config.patch_size) ) SCREAMING_SNAKE_CASE : Dict = (image_size[1] // patch_size[1]) * (image_size[0] // patch_size[0]) self.assertListEqual( list(hidden_states[0].shape[-2:] ) , [num_patches, self.model_tester.embed_dim] , ) SCREAMING_SNAKE_CASE : Any = outputs.reshaped_hidden_states self.assertEqual(len(lowerCamelCase_ ) , lowerCamelCase_ ) SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : Any = reshaped_hidden_states[0].shape SCREAMING_SNAKE_CASE : Optional[int] = ( reshaped_hidden_states[0].view(lowerCamelCase_ , lowerCamelCase_ , height * width ).permute(0 , 2 , 1 ) ) self.assertListEqual( list(reshaped_hidden_states.shape[-2:] ) , [num_patches, self.model_tester.embed_dim] , ) def __lowerCAmelCase ( self :Optional[Any] ) -> Optional[int]: '''simple docstring''' SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : Union[str, Any] = self.model_tester.prepare_config_and_inputs_for_common() SCREAMING_SNAKE_CASE : List[str] = ( self.model_tester.image_size if isinstance(self.model_tester.image_size , collections.abc.Iterable ) else (self.model_tester.image_size, self.model_tester.image_size) ) for model_class in self.all_model_classes: SCREAMING_SNAKE_CASE : Tuple = True self.check_hidden_states_output(lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ ) # check that output_hidden_states also work using config del inputs_dict["output_hidden_states"] SCREAMING_SNAKE_CASE : List[str] = True self.check_hidden_states_output(lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ ) def __lowerCAmelCase ( self :str ) -> Optional[int]: '''simple docstring''' SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : str = self.model_tester.prepare_config_and_inputs_for_common() SCREAMING_SNAKE_CASE : List[Any] = 3 SCREAMING_SNAKE_CASE : int = ( self.model_tester.image_size if isinstance(self.model_tester.image_size , collections.abc.Iterable ) else (self.model_tester.image_size, self.model_tester.image_size) ) SCREAMING_SNAKE_CASE : Optional[Any] = ( config.patch_size if isinstance(config.patch_size , collections.abc.Iterable ) else (config.patch_size, config.patch_size) ) SCREAMING_SNAKE_CASE : Optional[int] = image_size[0] + patch_size[0] - (image_size[0] % patch_size[0]) SCREAMING_SNAKE_CASE : Optional[int] = image_size[1] + patch_size[1] - (image_size[1] % patch_size[1]) for model_class in self.all_model_classes: SCREAMING_SNAKE_CASE : Optional[Any] = True self.check_hidden_states_output(lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , (padded_height, padded_width) ) # check that output_hidden_states also work using config del inputs_dict["output_hidden_states"] SCREAMING_SNAKE_CASE : Optional[Any] = True self.check_hidden_states_output(lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , (padded_height, padded_width) ) def __lowerCAmelCase ( self :str ) -> List[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : Tuple = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_masked_image_modeling(*lowerCamelCase_ ) def __lowerCAmelCase ( self :Tuple ) -> Optional[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : Tuple = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_image_classification(*lowerCamelCase_ ) @slow def __lowerCAmelCase ( self :Tuple ) -> List[str]: '''simple docstring''' for model_name in SWINV2_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: SCREAMING_SNAKE_CASE : Tuple = SwinvaModel.from_pretrained(lowerCamelCase_ ) self.assertIsNotNone(lowerCamelCase_ ) def __lowerCAmelCase ( self :int ) -> Any: '''simple docstring''' SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : List[str] = self.model_tester.prepare_config_and_inputs_for_common() SCREAMING_SNAKE_CASE : Tuple = _config_zero_init(lowerCamelCase_ ) for model_class in self.all_model_classes: SCREAMING_SNAKE_CASE : Dict = model_class(config=lowerCamelCase_ ) for name, param in model.named_parameters(): if "embeddings" not in name and "logit_scale" not in name and param.requires_grad: self.assertIn( ((param.data.mean() * 1E9).round() / 1E9).item() , [0.0, 1.0] , msg=f"Parameter {name} of model {model_class} seems not properly initialized" , ) @require_vision @require_torch class lowercase__( unittest.TestCase ): '''simple docstring''' @cached_property def __lowerCAmelCase ( self :Dict ) -> List[Any]: '''simple docstring''' return ( AutoImageProcessor.from_pretrained('''microsoft/swinv2-tiny-patch4-window8-256''' ) if is_vision_available() else None ) @slow def __lowerCAmelCase ( self :Dict ) -> Tuple: '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[int] = SwinvaForImageClassification.from_pretrained('''microsoft/swinv2-tiny-patch4-window8-256''' ).to( lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Dict = self.default_image_processor SCREAMING_SNAKE_CASE : Optional[Any] = Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png''' ) SCREAMING_SNAKE_CASE : List[str] = image_processor(images=lowerCamelCase_ , return_tensors='''pt''' ).to(lowerCamelCase_ ) # forward pass with torch.no_grad(): SCREAMING_SNAKE_CASE : Tuple = model(**lowerCamelCase_ ) # verify the logits SCREAMING_SNAKE_CASE : List[str] = torch.Size((1, 10_00) ) self.assertEqual(outputs.logits.shape , lowerCamelCase_ ) SCREAMING_SNAKE_CASE : int = torch.tensor([-0.3_9_4_7, -0.4_3_0_6, 0.0_0_2_6] ).to(lowerCamelCase_ ) self.assertTrue(torch.allclose(outputs.logits[0, :3] , lowerCamelCase_ , atol=1E-4 ) )
18
0
"""simple docstring""" from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available lowerCamelCase__ : List[str] = { "configuration_xlm_roberta_xl": [ "XLM_ROBERTA_XL_PRETRAINED_CONFIG_ARCHIVE_MAP", "XLMRobertaXLConfig", "XLMRobertaXLOnnxConfig", ], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCamelCase__ : int = [ "XLM_ROBERTA_XL_PRETRAINED_MODEL_ARCHIVE_LIST", "XLMRobertaXLForCausalLM", "XLMRobertaXLForMaskedLM", "XLMRobertaXLForMultipleChoice", "XLMRobertaXLForQuestionAnswering", "XLMRobertaXLForSequenceClassification", "XLMRobertaXLForTokenClassification", "XLMRobertaXLModel", "XLMRobertaXLPreTrainedModel", ] if TYPE_CHECKING: from .configuration_xlm_roberta_xl import ( XLM_ROBERTA_XL_PRETRAINED_CONFIG_ARCHIVE_MAP, XLMRobertaXLConfig, XLMRobertaXLOnnxConfig, ) try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_xlm_roberta_xl import ( XLM_ROBERTA_XL_PRETRAINED_MODEL_ARCHIVE_LIST, XLMRobertaXLForCausalLM, XLMRobertaXLForMaskedLM, XLMRobertaXLForMultipleChoice, XLMRobertaXLForQuestionAnswering, XLMRobertaXLForSequenceClassification, XLMRobertaXLForTokenClassification, XLMRobertaXLModel, XLMRobertaXLPreTrainedModel, ) else: import sys lowerCamelCase__ : List[Any] = _LazyModule(__name__, globals()["__file__"], _import_structure)
712
"""simple docstring""" from collections import OrderedDict from typing import Any, Mapping, Optional from ... import PreTrainedTokenizer from ...configuration_utils import PretrainedConfig from ...file_utils import TensorType, is_torch_available from ...onnx import OnnxConfig, OnnxConfigWithPast, OnnxSeqaSeqConfigWithPast from ...onnx.utils import compute_effective_axis_dimension from ...utils import logging lowerCamelCase__ : Tuple = logging.get_logger(__name__) lowerCamelCase__ : str = { "facebook/blenderbot_small-90M": "https://huggingface.co/facebook/blenderbot_small-90M/resolve/main/config.json", # See all BlenderbotSmall models at https://huggingface.co/models?filter=blenderbot_small } class lowercase__( _UpperCAmelCase ): '''simple docstring''' UpperCamelCase = """blenderbot-small""" UpperCamelCase = ["""past_key_values"""] UpperCamelCase = {"""num_attention_heads""": """encoder_attention_heads""", """hidden_size""": """d_model"""} def __init__( self :Any , lowerCamelCase_ :Dict=5_02_65 , lowerCamelCase_ :str=5_12 , lowerCamelCase_ :Tuple=8 , lowerCamelCase_ :int=20_48 , lowerCamelCase_ :str=16 , lowerCamelCase_ :Optional[int]=8 , lowerCamelCase_ :str=20_48 , lowerCamelCase_ :Optional[Any]=16 , lowerCamelCase_ :Union[str, Any]=0.0 , lowerCamelCase_ :List[str]=0.0 , lowerCamelCase_ :Tuple=True , lowerCamelCase_ :Tuple=True , lowerCamelCase_ :int="gelu" , lowerCamelCase_ :Tuple=5_12 , lowerCamelCase_ :Optional[int]=0.1 , lowerCamelCase_ :int=0.0 , lowerCamelCase_ :Tuple=0.0 , lowerCamelCase_ :Optional[int]=0.0_2 , lowerCamelCase_ :Union[str, Any]=1 , lowerCamelCase_ :Dict=False , lowerCamelCase_ :Optional[int]=0 , lowerCamelCase_ :List[Any]=1 , lowerCamelCase_ :Any=2 , lowerCamelCase_ :Optional[Any]=2 , **lowerCamelCase_ :Dict , ) -> Any: '''simple docstring''' SCREAMING_SNAKE_CASE : int = vocab_size SCREAMING_SNAKE_CASE : List[str] = max_position_embeddings SCREAMING_SNAKE_CASE : Optional[Any] = d_model SCREAMING_SNAKE_CASE : Dict = encoder_ffn_dim SCREAMING_SNAKE_CASE : Tuple = encoder_layers SCREAMING_SNAKE_CASE : Dict = encoder_attention_heads SCREAMING_SNAKE_CASE : Any = decoder_ffn_dim SCREAMING_SNAKE_CASE : str = decoder_layers SCREAMING_SNAKE_CASE : str = decoder_attention_heads SCREAMING_SNAKE_CASE : List[Any] = dropout SCREAMING_SNAKE_CASE : Optional[Any] = attention_dropout SCREAMING_SNAKE_CASE : Any = activation_dropout SCREAMING_SNAKE_CASE : List[str] = activation_function SCREAMING_SNAKE_CASE : Optional[int] = init_std SCREAMING_SNAKE_CASE : List[Any] = encoder_layerdrop SCREAMING_SNAKE_CASE : Union[str, Any] = decoder_layerdrop SCREAMING_SNAKE_CASE : List[Any] = use_cache SCREAMING_SNAKE_CASE : Union[str, Any] = encoder_layers SCREAMING_SNAKE_CASE : List[Any] = scale_embedding # scale factor will be sqrt(d_model) if True super().__init__( pad_token_id=lowerCamelCase_ , bos_token_id=lowerCamelCase_ , eos_token_id=lowerCamelCase_ , is_encoder_decoder=lowerCamelCase_ , decoder_start_token_id=lowerCamelCase_ , forced_eos_token_id=lowerCamelCase_ , **lowerCamelCase_ , ) class lowercase__( _UpperCAmelCase ): '''simple docstring''' @property def __lowerCAmelCase ( self :Any ) -> Mapping[str, Mapping[int, str]]: '''simple docstring''' if self.task in ["default", "seq2seq-lm"]: SCREAMING_SNAKE_CASE : Tuple = OrderedDict( [ ('''input_ids''', {0: '''batch''', 1: '''encoder_sequence'''}), ('''attention_mask''', {0: '''batch''', 1: '''encoder_sequence'''}), ] ) if self.use_past: SCREAMING_SNAKE_CASE : Union[str, Any] = {0: '''batch'''} SCREAMING_SNAKE_CASE : List[Any] = {0: '''batch''', 1: '''past_decoder_sequence + sequence'''} else: SCREAMING_SNAKE_CASE : Optional[Any] = {0: '''batch''', 1: '''decoder_sequence'''} SCREAMING_SNAKE_CASE : Union[str, Any] = {0: '''batch''', 1: '''decoder_sequence'''} if self.use_past: self.fill_with_past_key_values_(lowerCamelCase_ , direction='''inputs''' ) elif self.task == "causal-lm": # TODO: figure this case out. SCREAMING_SNAKE_CASE : Tuple = OrderedDict( [ ('''input_ids''', {0: '''batch''', 1: '''encoder_sequence'''}), ('''attention_mask''', {0: '''batch''', 1: '''encoder_sequence'''}), ] ) if self.use_past: SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : List[Any] = self.num_layers for i in range(lowerCamelCase_ ): SCREAMING_SNAKE_CASE : int = {0: '''batch''', 2: '''past_sequence + sequence'''} SCREAMING_SNAKE_CASE : List[str] = {0: '''batch''', 2: '''past_sequence + sequence'''} else: SCREAMING_SNAKE_CASE : Any = OrderedDict( [ ('''input_ids''', {0: '''batch''', 1: '''encoder_sequence'''}), ('''attention_mask''', {0: '''batch''', 1: '''encoder_sequence'''}), ('''decoder_input_ids''', {0: '''batch''', 1: '''decoder_sequence'''}), ('''decoder_attention_mask''', {0: '''batch''', 1: '''decoder_sequence'''}), ] ) return common_inputs @property def __lowerCAmelCase ( self :List[str] ) -> Mapping[str, Mapping[int, str]]: '''simple docstring''' if self.task in ["default", "seq2seq-lm"]: SCREAMING_SNAKE_CASE : Any = super().outputs else: SCREAMING_SNAKE_CASE : Tuple = super(lowerCamelCase_ , self ).outputs if self.use_past: SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : Union[str, Any] = self.num_layers for i in range(lowerCamelCase_ ): SCREAMING_SNAKE_CASE : Union[str, Any] = {0: '''batch''', 2: '''past_sequence + sequence'''} SCREAMING_SNAKE_CASE : str = {0: '''batch''', 2: '''past_sequence + sequence'''} return common_outputs def __lowerCAmelCase ( self :int , lowerCamelCase_ :PreTrainedTokenizer , lowerCamelCase_ :int = -1 , lowerCamelCase_ :int = -1 , lowerCamelCase_ :bool = False , lowerCamelCase_ :Optional[TensorType] = None , ) -> Mapping[str, Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : Dict = self._generate_dummy_inputs_for_sequence_classification_and_question_answering( lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ ) # Generate decoder inputs SCREAMING_SNAKE_CASE : Optional[int] = seq_length if not self.use_past else 1 SCREAMING_SNAKE_CASE : Optional[int] = self._generate_dummy_inputs_for_sequence_classification_and_question_answering( lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ ) SCREAMING_SNAKE_CASE : List[Any] = {f"decoder_{name}": tensor for name, tensor in decoder_inputs.items()} SCREAMING_SNAKE_CASE : str = dict(**lowerCamelCase_ , **lowerCamelCase_ ) if self.use_past: if not is_torch_available(): raise ValueError('''Cannot generate dummy past_keys inputs without PyTorch installed.''' ) else: import torch SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : Any = common_inputs['''input_ids'''].shape SCREAMING_SNAKE_CASE : str = common_inputs['''decoder_input_ids'''].shape[1] SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : Any = self.num_attention_heads SCREAMING_SNAKE_CASE : str = ( batch, num_encoder_attention_heads, encoder_seq_length, self._config.hidden_size // num_encoder_attention_heads, ) SCREAMING_SNAKE_CASE : Optional[Any] = decoder_seq_length + 3 SCREAMING_SNAKE_CASE : int = ( batch, num_decoder_attention_heads, decoder_past_length, self._config.hidden_size // num_decoder_attention_heads, ) SCREAMING_SNAKE_CASE : List[Any] = torch.cat( [common_inputs['''decoder_attention_mask'''], torch.ones(lowerCamelCase_ , lowerCamelCase_ )] , dim=1 ) SCREAMING_SNAKE_CASE : Optional[int] = [] # If the number of encoder and decoder layers are present in the model configuration, both are considered SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : Optional[int] = self.num_layers SCREAMING_SNAKE_CASE : int = min(lowerCamelCase_ , lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Optional[int] = max(lowerCamelCase_ , lowerCamelCase_ ) - min_num_layers SCREAMING_SNAKE_CASE : Tuple = '''encoder''' if num_encoder_layers > num_decoder_layers else '''decoder''' for _ in range(lowerCamelCase_ ): common_inputs["past_key_values"].append( ( torch.zeros(lowerCamelCase_ ), torch.zeros(lowerCamelCase_ ), torch.zeros(lowerCamelCase_ ), torch.zeros(lowerCamelCase_ ), ) ) # TODO: test this. SCREAMING_SNAKE_CASE : int = encoder_shape if remaining_side_name == '''encoder''' else decoder_shape for _ in range(lowerCamelCase_ , lowerCamelCase_ ): common_inputs["past_key_values"].append((torch.zeros(lowerCamelCase_ ), torch.zeros(lowerCamelCase_ )) ) return common_inputs def __lowerCAmelCase ( self :Any , lowerCamelCase_ :PreTrainedTokenizer , lowerCamelCase_ :int = -1 , lowerCamelCase_ :int = -1 , lowerCamelCase_ :bool = False , lowerCamelCase_ :Optional[TensorType] = None , ) -> Mapping[str, Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : Any = self._generate_dummy_inputs_for_sequence_classification_and_question_answering( lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ ) if self.use_past: if not is_torch_available(): raise ValueError('''Cannot generate dummy past_keys inputs without PyTorch installed.''' ) else: import torch SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : Tuple = common_inputs['''input_ids'''].shape # Not using the same length for past_key_values SCREAMING_SNAKE_CASE : List[str] = seqlen + 2 SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : Dict = self.num_layers SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : List[Any] = self.num_attention_heads SCREAMING_SNAKE_CASE : Union[str, Any] = ( batch, num_encoder_attention_heads, past_key_values_length, self._config.hidden_size // num_encoder_attention_heads, ) SCREAMING_SNAKE_CASE : Tuple = common_inputs['''attention_mask'''].dtype SCREAMING_SNAKE_CASE : Any = torch.cat( [common_inputs['''attention_mask'''], torch.ones(lowerCamelCase_ , lowerCamelCase_ , dtype=lowerCamelCase_ )] , dim=1 ) SCREAMING_SNAKE_CASE : Optional[int] = [ (torch.zeros(lowerCamelCase_ ), torch.zeros(lowerCamelCase_ )) for _ in range(lowerCamelCase_ ) ] return common_inputs def __lowerCAmelCase ( self :Union[str, Any] , lowerCamelCase_ :PreTrainedTokenizer , lowerCamelCase_ :int = -1 , lowerCamelCase_ :int = -1 , lowerCamelCase_ :bool = False , lowerCamelCase_ :Optional[TensorType] = None , ) -> Mapping[str, Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : List[str] = compute_effective_axis_dimension( lowerCamelCase_ , fixed_dimension=OnnxConfig.default_fixed_batch , num_token_to_add=0 ) # If dynamic axis (-1) we forward with a fixed dimension of 8 tokens to avoid optimizations made by ONNX SCREAMING_SNAKE_CASE : int = tokenizer.num_special_tokens_to_add(lowerCamelCase_ ) SCREAMING_SNAKE_CASE : int = compute_effective_axis_dimension( lowerCamelCase_ , fixed_dimension=OnnxConfig.default_fixed_sequence , num_token_to_add=lowerCamelCase_ ) # Generate dummy inputs according to compute batch and sequence SCREAMING_SNAKE_CASE : Tuple = [''' '''.join([tokenizer.unk_token] ) * seq_length] * batch_size SCREAMING_SNAKE_CASE : Any = dict(tokenizer(lowerCamelCase_ , return_tensors=lowerCamelCase_ ) ) return common_inputs def __lowerCAmelCase ( self :List[Any] , lowerCamelCase_ :PreTrainedTokenizer , lowerCamelCase_ :int = -1 , lowerCamelCase_ :int = -1 , lowerCamelCase_ :bool = False , lowerCamelCase_ :Optional[TensorType] = None , ) -> Mapping[str, Any]: '''simple docstring''' if self.task in ["default", "seq2seq-lm"]: SCREAMING_SNAKE_CASE : Dict = self._generate_dummy_inputs_for_default_and_seqaseq_lm( lowerCamelCase_ , batch_size=lowerCamelCase_ , seq_length=lowerCamelCase_ , is_pair=lowerCamelCase_ , framework=lowerCamelCase_ ) elif self.task == "causal-lm": SCREAMING_SNAKE_CASE : Union[str, Any] = self._generate_dummy_inputs_for_causal_lm( lowerCamelCase_ , batch_size=lowerCamelCase_ , seq_length=lowerCamelCase_ , is_pair=lowerCamelCase_ , framework=lowerCamelCase_ ) else: SCREAMING_SNAKE_CASE : List[Any] = self._generate_dummy_inputs_for_sequence_classification_and_question_answering( lowerCamelCase_ , batch_size=lowerCamelCase_ , seq_length=lowerCamelCase_ , is_pair=lowerCamelCase_ , framework=lowerCamelCase_ ) return common_inputs def __lowerCAmelCase ( self :Dict , lowerCamelCase_ :int , lowerCamelCase_ :List[str] , lowerCamelCase_ :List[str] , lowerCamelCase_ :Dict ) -> List[Any]: '''simple docstring''' if self.task in ["default", "seq2seq-lm"]: SCREAMING_SNAKE_CASE : Optional[Any] = super()._flatten_past_key_values_(lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ ) else: SCREAMING_SNAKE_CASE : Tuple = super(lowerCamelCase_ , self )._flatten_past_key_values_( lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ )
18
0
"""simple docstring""" def __A ( a_ : int )-> Union[str, Any]: '''simple docstring''' assert isinstance(a__ , a__ ), F"The input value of [n={number}] is not an integer" if number == 1: return 2 elif number < 1: SCREAMING_SNAKE_CASE : Optional[int] = F"The input value of [n={number}] has to be > 0" raise ValueError(a__ ) else: SCREAMING_SNAKE_CASE : List[str] = sylvester(number - 1 ) SCREAMING_SNAKE_CASE : Tuple = num - 1 SCREAMING_SNAKE_CASE : Any = num return lower * upper + 1 if __name__ == "__main__": print(f'''The 8th number in Sylvester\'s sequence: {sylvester(8)}''')
713
"""simple docstring""" from collections import OrderedDict from typing import TYPE_CHECKING, Any, Mapping, Optional from packaging import version from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...onnx.utils import compute_effective_axis_dimension from ...utils import logging if TYPE_CHECKING: from ...processing_utils import ProcessorMixin from ...utils import TensorType lowerCamelCase__ : Dict = logging.get_logger(__name__) lowerCamelCase__ : Dict = { "microsoft/layoutlmv3-base": "https://huggingface.co/microsoft/layoutlmv3-base/resolve/main/config.json", } class lowercase__( _UpperCAmelCase ): '''simple docstring''' UpperCamelCase = """layoutlmv3""" def __init__( self :str , lowerCamelCase_ :Optional[Any]=5_02_65 , lowerCamelCase_ :Dict=7_68 , lowerCamelCase_ :Union[str, Any]=12 , lowerCamelCase_ :Optional[Any]=12 , lowerCamelCase_ :Union[str, Any]=30_72 , lowerCamelCase_ :Any="gelu" , lowerCamelCase_ :Union[str, Any]=0.1 , lowerCamelCase_ :str=0.1 , lowerCamelCase_ :Any=5_12 , lowerCamelCase_ :int=2 , lowerCamelCase_ :Optional[Any]=0.0_2 , lowerCamelCase_ :Optional[int]=1E-5 , lowerCamelCase_ :Dict=1 , lowerCamelCase_ :int=0 , lowerCamelCase_ :Tuple=2 , lowerCamelCase_ :List[str]=10_24 , lowerCamelCase_ :Tuple=1_28 , lowerCamelCase_ :Any=1_28 , lowerCamelCase_ :Optional[Any]=True , lowerCamelCase_ :str=32 , lowerCamelCase_ :int=1_28 , lowerCamelCase_ :int=64 , lowerCamelCase_ :List[Any]=2_56 , lowerCamelCase_ :Any=True , lowerCamelCase_ :str=True , lowerCamelCase_ :Union[str, Any]=True , lowerCamelCase_ :List[str]=2_24 , lowerCamelCase_ :Dict=3 , lowerCamelCase_ :Union[str, Any]=16 , lowerCamelCase_ :Any=None , **lowerCamelCase_ :Optional[Any] , ) -> int: '''simple docstring''' super().__init__( vocab_size=lowerCamelCase_ , hidden_size=lowerCamelCase_ , num_hidden_layers=lowerCamelCase_ , num_attention_heads=lowerCamelCase_ , intermediate_size=lowerCamelCase_ , hidden_act=lowerCamelCase_ , hidden_dropout_prob=lowerCamelCase_ , attention_probs_dropout_prob=lowerCamelCase_ , max_position_embeddings=lowerCamelCase_ , type_vocab_size=lowerCamelCase_ , initializer_range=lowerCamelCase_ , layer_norm_eps=lowerCamelCase_ , pad_token_id=lowerCamelCase_ , bos_token_id=lowerCamelCase_ , eos_token_id=lowerCamelCase_ , **lowerCamelCase_ , ) SCREAMING_SNAKE_CASE : Optional[Any] = max_ad_position_embeddings SCREAMING_SNAKE_CASE : List[Any] = coordinate_size SCREAMING_SNAKE_CASE : Tuple = shape_size SCREAMING_SNAKE_CASE : Optional[int] = has_relative_attention_bias SCREAMING_SNAKE_CASE : List[Any] = rel_pos_bins SCREAMING_SNAKE_CASE : int = max_rel_pos SCREAMING_SNAKE_CASE : Any = has_spatial_attention_bias SCREAMING_SNAKE_CASE : List[Any] = rel_ad_pos_bins SCREAMING_SNAKE_CASE : Dict = max_rel_ad_pos SCREAMING_SNAKE_CASE : Optional[int] = text_embed SCREAMING_SNAKE_CASE : Any = visual_embed SCREAMING_SNAKE_CASE : Any = input_size SCREAMING_SNAKE_CASE : Tuple = num_channels SCREAMING_SNAKE_CASE : List[str] = patch_size SCREAMING_SNAKE_CASE : str = classifier_dropout class lowercase__( _UpperCAmelCase ): '''simple docstring''' UpperCamelCase = version.parse("""1.12""" ) @property def __lowerCAmelCase ( self :List[Any] ) -> Mapping[str, Mapping[int, str]]: '''simple docstring''' if self.task in ["question-answering", "sequence-classification"]: return OrderedDict( [ ('''input_ids''', {0: '''batch''', 1: '''sequence'''}), ('''attention_mask''', {0: '''batch''', 1: '''sequence'''}), ('''bbox''', {0: '''batch''', 1: '''sequence'''}), ('''pixel_values''', {0: '''batch''', 1: '''num_channels''', 2: '''height''', 3: '''width'''}), ] ) else: return OrderedDict( [ ('''input_ids''', {0: '''batch''', 1: '''sequence'''}), ('''bbox''', {0: '''batch''', 1: '''sequence'''}), ('''attention_mask''', {0: '''batch''', 1: '''sequence'''}), ('''pixel_values''', {0: '''batch''', 1: '''num_channels'''}), ] ) @property def __lowerCAmelCase ( self :Optional[int] ) -> float: '''simple docstring''' return 1E-5 @property def __lowerCAmelCase ( self :Tuple ) -> int: '''simple docstring''' return 12 def __lowerCAmelCase ( self :List[Any] , lowerCamelCase_ :"ProcessorMixin" , lowerCamelCase_ :int = -1 , lowerCamelCase_ :int = -1 , lowerCamelCase_ :bool = False , lowerCamelCase_ :Optional["TensorType"] = None , lowerCamelCase_ :int = 3 , lowerCamelCase_ :int = 40 , lowerCamelCase_ :int = 40 , ) -> Mapping[str, Any]: '''simple docstring''' setattr(processor.image_processor , '''apply_ocr''' , lowerCamelCase_ ) # If dynamic axis (-1) we forward with a fixed dimension of 2 samples to avoid optimizations made by ONNX SCREAMING_SNAKE_CASE : Dict = compute_effective_axis_dimension( lowerCamelCase_ , fixed_dimension=OnnxConfig.default_fixed_batch , num_token_to_add=0 ) # If dynamic axis (-1) we forward with a fixed dimension of 8 tokens to avoid optimizations made by ONNX SCREAMING_SNAKE_CASE : Union[str, Any] = processor.tokenizer.num_special_tokens_to_add(lowerCamelCase_ ) SCREAMING_SNAKE_CASE : Any = compute_effective_axis_dimension( lowerCamelCase_ , fixed_dimension=OnnxConfig.default_fixed_sequence , num_token_to_add=lowerCamelCase_ ) # Generate dummy inputs according to compute batch and sequence SCREAMING_SNAKE_CASE : Union[str, Any] = [[''' '''.join([processor.tokenizer.unk_token] ) * seq_length]] * batch_size # Generate dummy bounding boxes SCREAMING_SNAKE_CASE : int = [[[48, 84, 73, 1_28]]] * batch_size # If dynamic axis (-1) we forward with a fixed dimension of 2 samples to avoid optimizations made by ONNX # batch_size = compute_effective_axis_dimension(batch_size, fixed_dimension=OnnxConfig.default_fixed_batch) SCREAMING_SNAKE_CASE : List[Any] = self._generate_dummy_images(lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ , lowerCamelCase_ ) SCREAMING_SNAKE_CASE : int = dict( processor( lowerCamelCase_ , text=lowerCamelCase_ , boxes=lowerCamelCase_ , return_tensors=lowerCamelCase_ , ) ) return inputs
18
0
"""simple docstring""" import json import os from dataclasses import dataclass from functools import partial from typing import Callable import flax.linen as nn import jax import jax.numpy as jnp import joblib import optax import wandb from flax import jax_utils, struct, traverse_util from flax.serialization import from_bytes, to_bytes from flax.training import train_state from flax.training.common_utils import shard from tqdm.auto import tqdm from transformers import BigBirdConfig, FlaxBigBirdForQuestionAnswering from transformers.models.big_bird.modeling_flax_big_bird import FlaxBigBirdForQuestionAnsweringModule class lowercase__( UpperCamelCase__ ): '''simple docstring''' UpperCamelCase = 42 UpperCamelCase = jnp.floataa UpperCamelCase = True def __lowerCAmelCase ( self :Optional[int] ) -> List[Any]: '''simple docstring''' super().setup() SCREAMING_SNAKE_CASE : int = nn.Dense(5 , dtype=self.dtype ) def __call__( self :Union[str, Any] , *lowerCamelCase_ :Optional[Any] , **lowerCamelCase_ :Optional[Any] ) -> Any: '''simple docstring''' SCREAMING_SNAKE_CASE : Tuple = super().__call__(*__A , **__A ) SCREAMING_SNAKE_CASE : Tuple = self.cls(outputs[2] ) return outputs[:2] + (cls_out,) class lowercase__( UpperCamelCase__ ): '''simple docstring''' UpperCamelCase = FlaxBigBirdForNaturalQuestionsModule def __A ( a_ : List[str] , a_ : List[str] , a_ : Tuple , a_ : List[str] , a_ : Optional[int] , a_ : Optional[Any] )-> Tuple: '''simple docstring''' def cross_entropy(a_ : List[Any] , a_ : Tuple , a_ : Union[str, Any]=None ): SCREAMING_SNAKE_CASE : str = logits.shape[-1] SCREAMING_SNAKE_CASE : Dict = (labels[..., None] == jnp.arange(UpperCamelCase__ )[None]).astype('''f4''' ) SCREAMING_SNAKE_CASE : Any = jax.nn.log_softmax(UpperCamelCase__ , axis=-1 ) SCREAMING_SNAKE_CASE : Optional[int] = -jnp.sum(labels * logits , axis=-1 ) if reduction is not None: SCREAMING_SNAKE_CASE : List[str] = reduction(UpperCamelCase__ ) return loss SCREAMING_SNAKE_CASE : List[Any] = partial(UpperCamelCase__ , reduction=jnp.mean ) SCREAMING_SNAKE_CASE : int = cross_entropy(UpperCamelCase__ , UpperCamelCase__ ) SCREAMING_SNAKE_CASE : List[Any] = cross_entropy(UpperCamelCase__ , UpperCamelCase__ ) SCREAMING_SNAKE_CASE : Optional[int] = cross_entropy(UpperCamelCase__ , UpperCamelCase__ ) return (start_loss + end_loss + pooled_loss) / 3 @dataclass class lowercase__: '''simple docstring''' UpperCamelCase = """google/bigbird-roberta-base""" UpperCamelCase = 30_00 UpperCamelCase = 1_05_00 UpperCamelCase = 1_28 UpperCamelCase = 3 UpperCamelCase = 1 UpperCamelCase = 5 # tx_args UpperCamelCase = 3E-5 UpperCamelCase = 0.0 UpperCamelCase = 2_00_00 UpperCamelCase = 0.0095 UpperCamelCase = """bigbird-roberta-natural-questions""" UpperCamelCase = """training-expt""" UpperCamelCase = """data/nq-training.jsonl""" UpperCamelCase = """data/nq-validation.jsonl""" def __lowerCAmelCase ( self :int ) -> List[str]: '''simple docstring''' os.makedirs(self.base_dir , exist_ok=__A ) SCREAMING_SNAKE_CASE : str = os.path.join(self.base_dir , self.save_dir ) SCREAMING_SNAKE_CASE : List[Any] = self.batch_size_per_device * jax.device_count() @dataclass class lowercase__: '''simple docstring''' UpperCamelCase = 42 UpperCamelCase = 40_96 # no dynamic padding on TPUs def __call__( self :str , lowerCamelCase_ :Union[str, Any] ) -> Union[str, Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : Union[str, Any] = self.collate_fn(__A ) SCREAMING_SNAKE_CASE : Union[str, Any] = jax.tree_util.tree_map(__A , __A ) return batch def __lowerCAmelCase ( self :List[str] , lowerCamelCase_ :Union[str, Any] ) -> List[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : Optional[Any] = self.fetch_inputs(features['''input_ids'''] ) SCREAMING_SNAKE_CASE : List[str] = { '''input_ids''': jnp.array(__A , dtype=jnp.intaa ), '''attention_mask''': jnp.array(__A , dtype=jnp.intaa ), '''start_labels''': jnp.array(features['''start_token'''] , dtype=jnp.intaa ), '''end_labels''': jnp.array(features['''end_token'''] , dtype=jnp.intaa ), '''pooled_labels''': jnp.array(features['''category'''] , dtype=jnp.intaa ), } return batch def __lowerCAmelCase ( self :List[Any] , lowerCamelCase_ :list ) -> Tuple: '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[Any] = [self._fetch_inputs(__A ) for ids in input_ids] return zip(*__A ) def __lowerCAmelCase ( self :Union[str, Any] , lowerCamelCase_ :list ) -> str: '''simple docstring''' SCREAMING_SNAKE_CASE : Dict = [1 for _ in range(len(__A ) )] while len(__A ) < self.max_length: input_ids.append(self.pad_id ) attention_mask.append(0 ) return input_ids, attention_mask def __A ( a_ : List[Any] , a_ : Any , a_ : int=None )-> Dict: '''simple docstring''' if seed is not None: SCREAMING_SNAKE_CASE : List[str] = dataset.shuffle(seed=UpperCamelCase__ ) for i in range(len(UpperCamelCase__ ) // batch_size ): SCREAMING_SNAKE_CASE : int = dataset[i * batch_size : (i + 1) * batch_size] yield dict(UpperCamelCase__ ) @partial(jax.pmap , axis_name='''batch''' ) def __A ( a_ : Optional[Any] , a_ : int , **a_ : Dict )-> int: '''simple docstring''' def loss_fn(a_ : Any ): SCREAMING_SNAKE_CASE : Optional[Any] = model_inputs.pop('''start_labels''' ) SCREAMING_SNAKE_CASE : int = model_inputs.pop('''end_labels''' ) SCREAMING_SNAKE_CASE : str = model_inputs.pop('''pooled_labels''' ) SCREAMING_SNAKE_CASE : int = state.apply_fn(**UpperCamelCase__ , params=UpperCamelCase__ , dropout_rng=UpperCamelCase__ , train=UpperCamelCase__ ) SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : int = outputs return state.loss_fn( UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , ) SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : Optional[Any] = jax.random.split(UpperCamelCase__ ) SCREAMING_SNAKE_CASE : List[Any] = jax.value_and_grad(UpperCamelCase__ ) SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : Union[str, Any] = grad_fn(state.params ) SCREAMING_SNAKE_CASE : Union[str, Any] = jax.lax.pmean({'''loss''': loss} , axis_name='''batch''' ) SCREAMING_SNAKE_CASE : Optional[int] = jax.lax.pmean(UpperCamelCase__ , '''batch''' ) SCREAMING_SNAKE_CASE : int = state.apply_gradients(grads=UpperCamelCase__ ) return state, metrics, new_drp_rng @partial(jax.pmap , axis_name='''batch''' ) def __A ( a_ : Optional[Any] , **a_ : Optional[Any] )-> Optional[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : Tuple = model_inputs.pop('''start_labels''' ) SCREAMING_SNAKE_CASE : Tuple = model_inputs.pop('''end_labels''' ) SCREAMING_SNAKE_CASE : Union[str, Any] = model_inputs.pop('''pooled_labels''' ) SCREAMING_SNAKE_CASE : Dict = state.apply_fn(**UpperCamelCase__ , params=state.params , train=UpperCamelCase__ ) SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : int = outputs SCREAMING_SNAKE_CASE : int = state.loss_fn(UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ ) SCREAMING_SNAKE_CASE : List[str] = jax.lax.pmean({'''loss''': loss} , axis_name='''batch''' ) return metrics class lowercase__( train_state.TrainState ): '''simple docstring''' UpperCamelCase = struct.field(pytree_node=UpperCamelCase__ ) @dataclass class lowercase__: '''simple docstring''' UpperCamelCase = 42 UpperCamelCase = 42 UpperCamelCase = 42 UpperCamelCase = 42 UpperCamelCase = 42 UpperCamelCase = 42 UpperCamelCase = None def __lowerCAmelCase ( self :Dict , lowerCamelCase_ :int , lowerCamelCase_ :str , lowerCamelCase_ :Optional[Any] , lowerCamelCase_ :Union[str, Any]=None ) -> str: '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[Any] = model.params SCREAMING_SNAKE_CASE : List[Any] = TrainState.create( apply_fn=model.__call__ , params=__A , tx=__A , loss_fn=__A , ) if ckpt_dir is not None: SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : Optional[Any] = restore_checkpoint(__A , __A ) SCREAMING_SNAKE_CASE : Optional[int] = { '''lr''': args.lr, '''init_lr''': args.init_lr, '''warmup_steps''': args.warmup_steps, '''num_train_steps''': num_train_steps, '''weight_decay''': args.weight_decay, } SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : List[Any] = build_tx(**__A ) SCREAMING_SNAKE_CASE : List[Any] = train_state.TrainState( step=__A , apply_fn=model.__call__ , params=__A , tx=__A , opt_state=__A , ) SCREAMING_SNAKE_CASE : str = args SCREAMING_SNAKE_CASE : Tuple = data_collator SCREAMING_SNAKE_CASE : Dict = lr SCREAMING_SNAKE_CASE : Dict = params SCREAMING_SNAKE_CASE : str = jax_utils.replicate(__A ) return state def __lowerCAmelCase ( self :Union[str, Any] , lowerCamelCase_ :Any , lowerCamelCase_ :Optional[Any] , lowerCamelCase_ :Optional[Any] ) -> List[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : str = self.args SCREAMING_SNAKE_CASE : Tuple = len(__A ) // args.batch_size SCREAMING_SNAKE_CASE : Optional[int] = jax.random.PRNGKey(0 ) SCREAMING_SNAKE_CASE : Dict = jax.random.split(__A , jax.device_count() ) for epoch in range(args.max_epochs ): SCREAMING_SNAKE_CASE : List[Any] = jnp.array(0 , dtype=jnp.floataa ) SCREAMING_SNAKE_CASE : str = get_batched_dataset(__A , args.batch_size , seed=__A ) SCREAMING_SNAKE_CASE : Union[str, Any] = 0 for batch in tqdm(__A , total=__A , desc=f"Running EPOCH-{epoch}" ): SCREAMING_SNAKE_CASE : List[Any] = self.data_collator(__A ) SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE : List[str] = self.train_step_fn(__A , __A , **__A ) running_loss += jax_utils.unreplicate(metrics['''loss'''] ) i += 1 if i % args.logging_steps == 0: SCREAMING_SNAKE_CASE : Dict = jax_utils.unreplicate(state.step ) SCREAMING_SNAKE_CASE : List[str] = running_loss.item() / i SCREAMING_SNAKE_CASE : str = self.scheduler_fn(state_step - 1 ) SCREAMING_SNAKE_CASE : List[Any] = self.evaluate(__A , __A ) SCREAMING_SNAKE_CASE : Dict = { '''step''': state_step.item(), '''eval_loss''': eval_loss.item(), '''tr_loss''': tr_loss, '''lr''': lr.item(), } tqdm.write(str(__A ) ) self.logger.log(__A , commit=__A ) if i % args.save_steps == 0: self.save_checkpoint(args.save_dir + f"-e{epoch}-s{i}" , state=__A ) def __lowerCAmelCase ( self :str , lowerCamelCase_ :Union[str, Any] , lowerCamelCase_ :Optional[int] ) -> Optional[Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : Any = get_batched_dataset(__A , self.args.batch_size ) SCREAMING_SNAKE_CASE : List[str] = len(__A ) // self.args.batch_size SCREAMING_SNAKE_CASE : Dict = jnp.array(0 , dtype=jnp.floataa ) SCREAMING_SNAKE_CASE : int = 0 for batch in tqdm(__A , total=__A , desc='''Evaluating ... ''' ): SCREAMING_SNAKE_CASE : Tuple = self.data_collator(__A ) SCREAMING_SNAKE_CASE : Optional[Any] = self.val_step_fn(__A , **__A ) running_loss += jax_utils.unreplicate(metrics['''loss'''] ) i += 1 return running_loss / i def __lowerCAmelCase ( self :int , lowerCamelCase_ :int , lowerCamelCase_ :Tuple ) -> Optional[int]: '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[Any] = jax_utils.unreplicate(__A ) print(f"SAVING CHECKPOINT IN {save_dir}" , end=''' ... ''' ) self.model_save_fn(__A , params=state.params ) with open(os.path.join(__A , '''opt_state.msgpack''' ) , '''wb''' ) as f: f.write(to_bytes(state.opt_state ) ) joblib.dump(self.args , os.path.join(__A , '''args.joblib''' ) ) joblib.dump(self.data_collator , os.path.join(__A , '''data_collator.joblib''' ) ) with open(os.path.join(__A , '''training_state.json''' ) , '''w''' ) as f: json.dump({'''step''': state.step.item()} , __A ) print('''DONE''' ) def __A ( a_ : List[str] , a_ : str )-> Optional[Any]: '''simple docstring''' print(F"RESTORING CHECKPOINT FROM {save_dir}" , end=''' ... ''' ) with open(os.path.join(UpperCamelCase__ , '''flax_model.msgpack''' ) , '''rb''' ) as f: SCREAMING_SNAKE_CASE : Dict = from_bytes(state.params , f.read() ) with open(os.path.join(UpperCamelCase__ , '''opt_state.msgpack''' ) , '''rb''' ) as f: SCREAMING_SNAKE_CASE : Tuple = from_bytes(state.opt_state , f.read() ) SCREAMING_SNAKE_CASE : str = joblib.load(os.path.join(UpperCamelCase__ , '''args.joblib''' ) ) SCREAMING_SNAKE_CASE : Optional[Any] = joblib.load(os.path.join(UpperCamelCase__ , '''data_collator.joblib''' ) ) with open(os.path.join(UpperCamelCase__ , '''training_state.json''' ) , '''r''' ) as f: SCREAMING_SNAKE_CASE : Tuple = json.load(UpperCamelCase__ ) SCREAMING_SNAKE_CASE : Union[str, Any] = training_state['''step'''] print('''DONE''' ) return params, opt_state, step, args, data_collator def __A ( a_ : int , a_ : Optional[Any] , a_ : Optional[int] , a_ : Optional[int] )-> List[str]: '''simple docstring''' SCREAMING_SNAKE_CASE : Any = num_train_steps - warmup_steps SCREAMING_SNAKE_CASE : Dict = optax.linear_schedule(init_value=UpperCamelCase__ , end_value=UpperCamelCase__ , transition_steps=UpperCamelCase__ ) SCREAMING_SNAKE_CASE : Union[str, Any] = optax.linear_schedule(init_value=UpperCamelCase__ , end_value=1E-7 , transition_steps=UpperCamelCase__ ) SCREAMING_SNAKE_CASE : str = optax.join_schedules(schedules=[warmup_fn, decay_fn] , boundaries=[warmup_steps] ) return lr def __A ( a_ : Optional[Any] , a_ : Optional[int] , a_ : List[str] , a_ : Optional[int] , a_ : Dict )-> List[Any]: '''simple docstring''' def weight_decay_mask(a_ : Optional[Any] ): SCREAMING_SNAKE_CASE : Any = traverse_util.flatten_dict(UpperCamelCase__ ) SCREAMING_SNAKE_CASE : str = {k: (v[-1] != '''bias''' and v[-2:] != ('''LayerNorm''', '''scale''')) for k, v in params.items()} return traverse_util.unflatten_dict(UpperCamelCase__ ) SCREAMING_SNAKE_CASE : List[Any] = scheduler_fn(UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ ) SCREAMING_SNAKE_CASE : str = optax.adamw(learning_rate=UpperCamelCase__ , weight_decay=UpperCamelCase__ , mask=UpperCamelCase__ ) return tx, lr
714
"""simple docstring""" import math def __A ( a_ : list , a_ : int )-> int: '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[int] = len(a_ ) SCREAMING_SNAKE_CASE : Optional[Any] = int(math.floor(math.sqrt(a_ ) ) ) SCREAMING_SNAKE_CASE : List[str] = 0 while arr[min(a_ , a_ ) - 1] < x: SCREAMING_SNAKE_CASE : Optional[Any] = step step += int(math.floor(math.sqrt(a_ ) ) ) if prev >= n: return -1 while arr[prev] < x: SCREAMING_SNAKE_CASE : Any = prev + 1 if prev == min(a_ , a_ ): return -1 if arr[prev] == x: return prev return -1 if __name__ == "__main__": lowerCamelCase__ : Union[str, Any] = input("Enter numbers separated by a comma:\n").strip() lowerCamelCase__ : List[str] = [int(item) for item in user_input.split(",")] lowerCamelCase__ : Dict = int(input("Enter the number to be searched:\n")) lowerCamelCase__ : Tuple = jump_search(arr, x) if res == -1: print("Number not found!") else: print(f'''Number {x} is at index {res}''')
18
0
"""simple docstring""" def __A ( a_ : Optional[int] )-> Union[str, Any]: '''simple docstring''' SCREAMING_SNAKE_CASE : List[Any] = [] SCREAMING_SNAKE_CASE : Optional[int] = set({'''(''', '''[''', '''{'''} ) SCREAMING_SNAKE_CASE : int = set({''')''', ''']''', '''}'''} ) SCREAMING_SNAKE_CASE : Optional[Any] = {'''{''': '''}''', '''[''': ''']''', '''(''': ''')'''} for i in range(len(_UpperCAmelCase ) ): if s[i] in open_brackets: stack.append(s[i] ) elif s[i] in closed_brackets and ( len(_UpperCAmelCase ) == 0 or (len(_UpperCAmelCase ) > 0 and open_to_closed[stack.pop()] != s[i]) ): return False return len(_UpperCAmelCase ) == 0 def __A ( )-> Any: '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[Any] = input('''Enter sequence of brackets: ''' ) if is_balanced(_UpperCAmelCase ): print(_UpperCAmelCase , '''is balanced''' ) else: print(_UpperCAmelCase , '''is not balanced''' ) if __name__ == "__main__": main()
715
"""simple docstring""" from sklearn.metrics import fa_score import datasets lowerCamelCase__ : List[Any] = "\nThe F1 score is the harmonic mean of the precision and recall. It can be computed with the equation:\nF1 = 2 * (precision * recall) / (precision + recall)\n" lowerCamelCase__ : str = "\nArgs:\n predictions (`list` of `int`): Predicted labels.\n references (`list` of `int`): Ground truth labels.\n labels (`list` of `int`): The set of labels to include when `average` is not set to `'binary'`, and the order of the labels if `average` is `None`. Labels present in the data can be excluded, for example to calculate a multiclass average ignoring a majority negative class. Labels not present in the data will result in 0 components in a macro average. For multilabel targets, labels are column indices. By default, all labels in `predictions` and `references` are used in sorted order. Defaults to None.\n pos_label (`int`): The class to be considered the positive class, in the case where `average` is set to `binary`. Defaults to 1.\n average (`string`): This parameter is required for multiclass/multilabel targets. If set to `None`, the scores for each class are returned. Otherwise, this determines the type of averaging performed on the data. Defaults to `'binary'`.\n\n - 'binary': Only report results for the class specified by `pos_label`. This is applicable only if the classes found in `predictions` and `references` are binary.\n - 'micro': Calculate metrics globally by counting the total true positives, false negatives and false positives.\n - 'macro': Calculate metrics for each label, and find their unweighted mean. This does not take label imbalance into account.\n - 'weighted': Calculate metrics for each label, and find their average weighted by support (the number of true instances for each label). This alters `'macro'` to account for label imbalance. This option can result in an F-score that is not between precision and recall.\n - 'samples': Calculate metrics for each instance, and find their average (only meaningful for multilabel classification).\n sample_weight (`list` of `float`): Sample weights Defaults to None.\n\nReturns:\n f1 (`float` or `array` of `float`): F1 score or list of f1 scores, depending on the value passed to `average`. Minimum possible value is 0. Maximum possible value is 1. Higher f1 scores are better.\n\nExamples:\n\n Example 1-A simple binary example\n >>> f1_metric = datasets.load_metric(\"f1\")\n >>> results = f1_metric.compute(references=[0, 1, 0, 1, 0], predictions=[0, 0, 1, 1, 0])\n >>> print(results)\n {'f1': 0.5}\n\n Example 2-The same simple binary example as in Example 1, but with `pos_label` set to `0`.\n >>> f1_metric = datasets.load_metric(\"f1\")\n >>> results = f1_metric.compute(references=[0, 1, 0, 1, 0], predictions=[0, 0, 1, 1, 0], pos_label=0)\n >>> print(round(results['f1'], 2))\n 0.67\n\n Example 3-The same simple binary example as in Example 1, but with `sample_weight` included.\n >>> f1_metric = datasets.load_metric(\"f1\")\n >>> results = f1_metric.compute(references=[0, 1, 0, 1, 0], predictions=[0, 0, 1, 1, 0], sample_weight=[0.9, 0.5, 3.9, 1.2, 0.3])\n >>> print(round(results['f1'], 2))\n 0.35\n\n Example 4-A multiclass example, with different values for the `average` input.\n >>> predictions = [0, 2, 1, 0, 0, 1]\n >>> references = [0, 1, 2, 0, 1, 2]\n >>> results = f1_metric.compute(predictions=predictions, references=references, average=\"macro\")\n >>> print(round(results['f1'], 2))\n 0.27\n >>> results = f1_metric.compute(predictions=predictions, references=references, average=\"micro\")\n >>> print(round(results['f1'], 2))\n 0.33\n >>> results = f1_metric.compute(predictions=predictions, references=references, average=\"weighted\")\n >>> print(round(results['f1'], 2))\n 0.27\n >>> results = f1_metric.compute(predictions=predictions, references=references, average=None)\n >>> print(results)\n {'f1': array([0.8, 0. , 0. ])}\n" lowerCamelCase__ : int = "\n@article{scikit-learn,\n title={Scikit-learn: Machine Learning in {P}ython},\n author={Pedregosa, F. and Varoquaux, G. and Gramfort, A. and Michel, V.\n and Thirion, B. and Grisel, O. and Blondel, M. and Prettenhofer, P.\n and Weiss, R. and Dubourg, V. and Vanderplas, J. and Passos, A. and\n Cournapeau, D. and Brucher, M. and Perrot, M. and Duchesnay, E.},\n journal={Journal of Machine Learning Research},\n volume={12},\n pages={2825--2830},\n year={2011}\n}\n" @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class lowercase__( datasets.Metric ): '''simple docstring''' def __lowerCAmelCase ( self :str ) -> Any: '''simple docstring''' return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { '''predictions''': datasets.Sequence(datasets.Value('''int32''' ) ), '''references''': datasets.Sequence(datasets.Value('''int32''' ) ), } if self.config_name == '''multilabel''' else { '''predictions''': datasets.Value('''int32''' ), '''references''': datasets.Value('''int32''' ), } ) , reference_urls=['''https://scikit-learn.org/stable/modules/generated/sklearn.metrics.f1_score.html'''] , ) def __lowerCAmelCase ( self :Any , lowerCamelCase_ :Dict , lowerCamelCase_ :Optional[Any] , lowerCamelCase_ :int=None , lowerCamelCase_ :str=1 , lowerCamelCase_ :Union[str, Any]="binary" , lowerCamelCase_ :Dict=None ) -> int: '''simple docstring''' SCREAMING_SNAKE_CASE : int = fa_score( lowerCamelCase_ , lowerCamelCase_ , labels=lowerCamelCase_ , pos_label=lowerCamelCase_ , average=lowerCamelCase_ , sample_weight=lowerCamelCase_ ) return {"f1": float(lowerCamelCase_ ) if score.size == 1 else score}
18
0
"""simple docstring""" import argparse import os from pathlib import Path import torch from bark.generation import _load_model as _bark_load_model from huggingface_hub import hf_hub_download from transformers import EncodecConfig, EncodecModel, set_seed from transformers.models.bark.configuration_bark import ( BarkCoarseConfig, BarkConfig, BarkFineConfig, BarkSemanticConfig, ) from transformers.models.bark.generation_configuration_bark import ( BarkCoarseGenerationConfig, BarkFineGenerationConfig, BarkGenerationConfig, BarkSemanticGenerationConfig, ) from transformers.models.bark.modeling_bark import BarkCoarseModel, BarkFineModel, BarkModel, BarkSemanticModel from transformers.utils import logging logging.set_verbosity_info() lowerCamelCase__ : Optional[int] = logging.get_logger(__name__) set_seed(770) lowerCamelCase__ : Union[str, Any] = { "c_attn": "att_proj", "c_proj": "out_proj", "c_fc": "in_proj", "transformer.": "", "h.": "layers.", "ln_1": "layernorm_1", "ln_2": "layernorm_2", "ln_f": "layernorm_final", "wpe": "position_embeds_layer", "wte": "input_embeds_layer", } lowerCamelCase__ : List[str] = { "text_small": { "repo_id": "suno/bark", "file_name": "text.pt", }, "coarse_small": { "repo_id": "suno/bark", "file_name": "coarse.pt", }, "fine_small": { "repo_id": "suno/bark", "file_name": "fine.pt", }, "text": { "repo_id": "suno/bark", "file_name": "text_2.pt", }, "coarse": { "repo_id": "suno/bark", "file_name": "coarse_2.pt", }, "fine": { "repo_id": "suno/bark", "file_name": "fine_2.pt", }, } lowerCamelCase__ : Optional[Any] = os.path.dirname(os.path.abspath(__file__)) lowerCamelCase__ : int = os.path.join(os.path.expanduser("~"), ".cache") lowerCamelCase__ : Dict = os.path.join(os.getenv("XDG_CACHE_HOME", default_cache_dir), "suno", "bark_v0") def __A ( a_ : List[Any] , a_ : Dict=False )-> Any: '''simple docstring''' SCREAMING_SNAKE_CASE : Any = model_type if use_small: key += "_small" return os.path.join(a_ , REMOTE_MODEL_PATHS[key]['''file_name'''] ) def __A ( a_ : List[Any] , a_ : Any )-> List[Any]: '''simple docstring''' os.makedirs(a_ , exist_ok=a_ ) hf_hub_download(repo_id=a_ , filename=a_ , local_dir=a_ ) def __A ( a_ : Dict , a_ : int , a_ : Dict=False , a_ : Dict="text" )-> str: '''simple docstring''' if model_type == "text": SCREAMING_SNAKE_CASE : List[Any] = BarkSemanticModel SCREAMING_SNAKE_CASE : int = BarkSemanticConfig SCREAMING_SNAKE_CASE : Any = BarkSemanticGenerationConfig elif model_type == "coarse": SCREAMING_SNAKE_CASE : Union[str, Any] = BarkCoarseModel SCREAMING_SNAKE_CASE : Optional[int] = BarkCoarseConfig SCREAMING_SNAKE_CASE : str = BarkCoarseGenerationConfig elif model_type == "fine": SCREAMING_SNAKE_CASE : Dict = BarkFineModel SCREAMING_SNAKE_CASE : Optional[Any] = BarkFineConfig SCREAMING_SNAKE_CASE : str = BarkFineGenerationConfig else: raise NotImplementedError() SCREAMING_SNAKE_CASE : Any = F"{model_type}_small" if use_small else model_type SCREAMING_SNAKE_CASE : List[Any] = REMOTE_MODEL_PATHS[model_key] if not os.path.exists(a_ ): logger.info(F"{model_type} model not found, downloading into `{CACHE_DIR}`." ) _download(model_info['''repo_id'''] , model_info['''file_name'''] ) SCREAMING_SNAKE_CASE : List[Any] = torch.load(a_ , map_location=a_ ) # this is a hack SCREAMING_SNAKE_CASE : Optional[Any] = checkpoint['model_args'] if "input_vocab_size" not in model_args: SCREAMING_SNAKE_CASE : Optional[Any] = model_args['vocab_size'] SCREAMING_SNAKE_CASE : Optional[Any] = model_args['vocab_size'] del model_args["vocab_size"] # convert Bark model arguments to HF Bark model arguments SCREAMING_SNAKE_CASE : Any = model_args.pop('''n_head''' ) SCREAMING_SNAKE_CASE : Tuple = model_args.pop('''n_embd''' ) SCREAMING_SNAKE_CASE : int = model_args.pop('''n_layer''' ) SCREAMING_SNAKE_CASE : str = ConfigClass(**checkpoint['''model_args'''] ) SCREAMING_SNAKE_CASE : List[str] = ModelClass(config=a_ ) SCREAMING_SNAKE_CASE : List[Any] = GenerationConfigClass() SCREAMING_SNAKE_CASE : Optional[Any] = model_generation_config SCREAMING_SNAKE_CASE : int = checkpoint['model'] # fixup checkpoint SCREAMING_SNAKE_CASE : Optional[Any] = '_orig_mod.' for k, v in list(state_dict.items() ): if k.startswith(a_ ): # replace part of the key with corresponding layer name in HF implementation SCREAMING_SNAKE_CASE : Union[str, Any] = k[len(a_ ) :] for old_layer_name in new_layer_name_dict: SCREAMING_SNAKE_CASE : List[Any] = new_k.replace(a_ , new_layer_name_dict[old_layer_name] ) SCREAMING_SNAKE_CASE : Optional[int] = state_dict.pop(a_ ) SCREAMING_SNAKE_CASE : Union[str, Any] = set(state_dict.keys() ) - set(model.state_dict().keys() ) SCREAMING_SNAKE_CASE : List[str] = {k for k in extra_keys if not k.endswith('''.attn.bias''' )} SCREAMING_SNAKE_CASE : Union[str, Any] = set(model.state_dict().keys() ) - set(state_dict.keys() ) SCREAMING_SNAKE_CASE : Optional[Any] = {k for k in missing_keys if not k.endswith('''.attn.bias''' )} if len(a_ ) != 0: raise ValueError(F"extra keys found: {extra_keys}" ) if len(a_ ) != 0: raise ValueError(F"missing keys: {missing_keys}" ) model.load_state_dict(a_ , strict=a_ ) SCREAMING_SNAKE_CASE : int = model.num_parameters(exclude_embeddings=a_ ) SCREAMING_SNAKE_CASE : List[Any] = checkpoint['best_val_loss'].item() logger.info(F"model loaded: {round(n_params/1E6 , 1 )}M params, {round(a_ , 3 )} loss" ) model.eval() model.to(a_ ) del checkpoint, state_dict return model def __A ( a_ : Optional[int] , a_ : Optional[int]=False , a_ : Union[str, Any]="text" )-> Dict: '''simple docstring''' if model_type not in ("text", "coarse", "fine"): raise NotImplementedError() SCREAMING_SNAKE_CASE : List[str] = 'cpu' # do conversion on cpu SCREAMING_SNAKE_CASE : str = _get_ckpt_path(a_ , use_small=a_ ) SCREAMING_SNAKE_CASE : List[Any] = _load_model(a_ , a_ , model_type=a_ , use_small=a_ ) # load bark initial model SCREAMING_SNAKE_CASE : Optional[int] = _bark_load_model(a_ , '''cpu''' , model_type=a_ , use_small=a_ ) if model_type == "text": SCREAMING_SNAKE_CASE : str = bark_model['model'] if model.num_parameters(exclude_embeddings=a_ ) != bark_model.get_num_params(): raise ValueError('''initial and new models don\'t have the same number of parameters''' ) # check if same output as the bark model SCREAMING_SNAKE_CASE : Optional[Any] = 5 SCREAMING_SNAKE_CASE : Optional[Any] = 10 if model_type in ["text", "coarse"]: SCREAMING_SNAKE_CASE : Any = torch.randint(2_56 , (batch_size, sequence_length) , dtype=torch.int ) SCREAMING_SNAKE_CASE : Union[str, Any] = bark_model(a_ )[0] SCREAMING_SNAKE_CASE : Optional[int] = model(a_ ) # take last logits SCREAMING_SNAKE_CASE : List[Any] = output_new_model_total.logits[:, [-1], :] else: SCREAMING_SNAKE_CASE : Tuple = 3 SCREAMING_SNAKE_CASE : List[str] = 8 SCREAMING_SNAKE_CASE : str = torch.randint(2_56 , (batch_size, sequence_length, n_codes_total) , dtype=torch.int ) SCREAMING_SNAKE_CASE : Optional[Any] = model(a_ , a_ ) SCREAMING_SNAKE_CASE : Optional[Any] = bark_model(a_ , a_ ) SCREAMING_SNAKE_CASE : List[str] = output_new_model_total.logits # output difference should come from the difference of self-attention implementation design if output_new_model.shape != output_old_model.shape: raise ValueError('''initial and new outputs don\'t have the same shape''' ) if (output_new_model - output_old_model).abs().max().item() > 1E-3: raise ValueError('''initial and new outputs are not equal''' ) Path(a_ ).mkdir(exist_ok=a_ ) model.save_pretrained(a_ ) def __A ( a_ : List[str] , a_ : Dict , a_ : Optional[int] , a_ : int , a_ : List[str] , a_ : str , )-> int: '''simple docstring''' SCREAMING_SNAKE_CASE : Tuple = os.path.join(a_ , a_ ) SCREAMING_SNAKE_CASE : List[Any] = BarkSemanticConfig.from_pretrained(os.path.join(a_ , '''config.json''' ) ) SCREAMING_SNAKE_CASE : List[Any] = BarkCoarseConfig.from_pretrained(os.path.join(a_ , '''config.json''' ) ) SCREAMING_SNAKE_CASE : Any = BarkFineConfig.from_pretrained(os.path.join(a_ , '''config.json''' ) ) SCREAMING_SNAKE_CASE : Union[str, Any] = EncodecConfig.from_pretrained('''facebook/encodec_24khz''' ) SCREAMING_SNAKE_CASE : List[str] = BarkSemanticModel.from_pretrained(a_ ) SCREAMING_SNAKE_CASE : Tuple = BarkCoarseModel.from_pretrained(a_ ) SCREAMING_SNAKE_CASE : Optional[Any] = BarkFineModel.from_pretrained(a_ ) SCREAMING_SNAKE_CASE : Optional[int] = EncodecModel.from_pretrained('''facebook/encodec_24khz''' ) SCREAMING_SNAKE_CASE : List[Any] = BarkConfig.from_sub_model_configs( a_ , a_ , a_ , a_ ) SCREAMING_SNAKE_CASE : Optional[int] = BarkGenerationConfig.from_sub_model_configs( semantic.generation_config , coarseAcoustic.generation_config , fineAcoustic.generation_config ) SCREAMING_SNAKE_CASE : Union[str, Any] = BarkModel(a_ ) SCREAMING_SNAKE_CASE : List[Any] = semantic SCREAMING_SNAKE_CASE : int = coarseAcoustic SCREAMING_SNAKE_CASE : Union[str, Any] = fineAcoustic SCREAMING_SNAKE_CASE : Any = codec SCREAMING_SNAKE_CASE : Dict = bark_generation_config Path(a_ ).mkdir(exist_ok=a_ ) bark.save_pretrained(a_ , repo_id=a_ , push_to_hub=a_ ) if __name__ == "__main__": lowerCamelCase__ : int = argparse.ArgumentParser() # Required parameters parser.add_argument("model_type", type=str, help="text, coarse or fine.") parser.add_argument("pytorch_dump_folder_path", default=None, type=str, help="Path to the output PyTorch model.") parser.add_argument("--is_small", action="store_true", help="convert the small version instead of the large.") lowerCamelCase__ : Optional[int] = parser.parse_args() load_model(args.pytorch_dump_folder_path, model_type=args.model_type, use_small=args.is_small)
716
"""simple docstring""" from __future__ import annotations from fractions import Fraction def __A ( a_ : int , a_ : int )-> bool: '''simple docstring''' return ( num != den and num % 10 == den // 10 and (num // 10) / (den % 10) == num / den ) def __A ( a_ : int )-> list[str]: '''simple docstring''' SCREAMING_SNAKE_CASE : Union[str, Any] = [] SCREAMING_SNAKE_CASE : List[str] = 11 SCREAMING_SNAKE_CASE : Union[str, Any] = int('''1''' + '''0''' * digit_len ) for num in range(a_ , a_ ): while den <= 99: if (num != den) and (num % 10 == den // 10) and (den % 10 != 0): if is_digit_cancelling(a_ , a_ ): solutions.append(F"{num}/{den}" ) den += 1 num += 1 SCREAMING_SNAKE_CASE : Optional[Any] = 10 return solutions def __A ( a_ : int = 2 )-> int: '''simple docstring''' SCREAMING_SNAKE_CASE : Optional[Any] = 1.0 for fraction in fraction_list(a_ ): SCREAMING_SNAKE_CASE : List[str] = Fraction(a_ ) result *= frac.denominator / frac.numerator return int(a_ ) if __name__ == "__main__": print(solution())
18
0