code
stringlengths
87
55.2k
code_codestyle
int64
0
349
style_context
stringlengths
135
49.1k
style_context_codestyle
int64
0
349
label
int64
0
1
def _A ( _lowercase ) -> bool: """simple docstring""" return str(UpperCamelCase__ ) == str(UpperCamelCase__ )[::-1] def _A ( _lowercase ) -> int: """simple docstring""" return int(UpperCamelCase__ ) + int(str(UpperCamelCase__ )[::-1] ) def _A ( _lowercase = 1_00_00 ) -> int: """simple docstring""" __UpperCamelCase = [] for num in range(1 , UpperCamelCase__ ): __UpperCamelCase = 0 __UpperCamelCase = num while iterations < 50: __UpperCamelCase = sum_reverse(UpperCamelCase__ ) iterations += 1 if is_palindrome(UpperCamelCase__ ): break else: lychrel_nums.append(UpperCamelCase__ ) return len(UpperCamelCase__ ) if __name__ == "__main__": print(f"""{solution() = }""")
310
from __future__ import annotations from collections.abc import Callable def __SCREAMING_SNAKE_CASE ( UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ = 100 , ) -> float: '''simple docstring''' UpperCAmelCase = x_start UpperCAmelCase = fnc(UpperCamelCase__ ) UpperCAmelCase = 0.0 for _ in range(UpperCamelCase__ ): # Approximates small segments of curve as linear and solve # for trapezoidal area UpperCAmelCase = (x_end - x_start) / steps + xa UpperCAmelCase = fnc(UpperCamelCase__ ) area += abs(fxa + fxa ) * (xa - xa) / 2 # Increment step UpperCAmelCase = xa UpperCAmelCase = fxa return area if __name__ == "__main__": def __SCREAMING_SNAKE_CASE ( UpperCamelCase__ ) -> str: '''simple docstring''' return x**3 + x**2 print("f(x) = x^3 + x^2") print("The area between the curve, x = -5, x = 5 and the x axis is:") __A : List[Any] = 10 while i <= 100_000: print(F'with {i} steps: {trapezoidal_area(f, -5, 5, i)}') i *= 10
273
0
from __future__ import annotations import pandas as pd def lowerCamelCase_ ( _a , _a , _a ): """simple docstring""" lowerCAmelCase__ : List[str] = [0] * no_of_processes lowerCAmelCase__ : Optional[int] = [0] * no_of_processes # Copy the burst time into remaining_time[] for i in range(UpperCamelCase__ ): lowerCAmelCase__ : Tuple = burst_time[i] lowerCAmelCase__ : Optional[Any] = 0 lowerCAmelCase__ : Optional[Any] = 0 lowerCAmelCase__ : Tuple = 999_999_999 lowerCAmelCase__ : Optional[Any] = 0 lowerCAmelCase__ : Optional[Any] = False # Process until all processes are completed while complete != no_of_processes: for j in range(UpperCamelCase__ ): if arrival_time[j] <= increment_time and remaining_time[j] > 0: if remaining_time[j] < minm: lowerCAmelCase__ : List[Any] = remaining_time[j] lowerCAmelCase__ : List[str] = j lowerCAmelCase__ : List[str] = True if not check: increment_time += 1 continue remaining_time[short] -= 1 lowerCAmelCase__ : Optional[Any] = remaining_time[short] if minm == 0: lowerCAmelCase__ : Optional[int] = 999_999_999 if remaining_time[short] == 0: complete += 1 lowerCAmelCase__ : Union[str, Any] = False # Find finish time of current process lowerCAmelCase__ : Optional[int] = increment_time + 1 # Calculate waiting time lowerCAmelCase__ : Union[str, Any] = finish_time - arrival_time[short] lowerCAmelCase__ : Optional[int] = finar - burst_time[short] if waiting_time[short] < 0: lowerCAmelCase__ : Union[str, Any] = 0 # Increment time increment_time += 1 return waiting_time def lowerCamelCase_ ( _a , _a , _a ): """simple docstring""" lowerCAmelCase__ : Optional[Any] = [0] * no_of_processes for i in range(UpperCamelCase__ ): lowerCAmelCase__ : Optional[int] = burst_time[i] + waiting_time[i] return turn_around_time def lowerCamelCase_ ( _a , _a , _a ): """simple docstring""" lowerCAmelCase__ : Tuple = 0 lowerCAmelCase__ : List[Any] = 0 for i in range(UpperCamelCase__ ): lowerCAmelCase__ : Optional[Any] = total_waiting_time + waiting_time[i] lowerCAmelCase__ : Optional[int] = total_turn_around_time + turn_around_time[i] print(f'Average waiting time = {total_waiting_time / no_of_processes:.5f}' ) print('''Average turn around time =''' , total_turn_around_time / no_of_processes ) if __name__ == "__main__": print('''Enter how many process you want to analyze''') lowerCamelCase = int(input()) lowerCamelCase = [0] * no_of_processes lowerCamelCase = [0] * no_of_processes lowerCamelCase = list(range(1, no_of_processes + 1)) for i in range(no_of_processes): print('''Enter the arrival time and burst time for process:--''' + str(i + 1)) lowerCamelCase = map(int, input().split()) lowerCamelCase = calculate_waitingtime(arrival_time, burst_time, no_of_processes) lowerCamelCase = burst_time lowerCamelCase = no_of_processes lowerCamelCase = waiting_time lowerCamelCase = calculate_turnaroundtime(bt, n, wt) calculate_average_times(waiting_time, turn_around_time, no_of_processes) lowerCamelCase = pd.DataFrame( list(zip(processes, burst_time, arrival_time, waiting_time, turn_around_time)), columns=[ '''Process''', '''BurstTime''', '''ArrivalTime''', '''WaitingTime''', '''TurnAroundTime''', ], ) # Printing the dataFrame pd.set_option('''display.max_rows''', fcfs.shape[0] + 1) print(fcfs)
131
import json from typing import List, Optional, Tuple from tokenizers import normalizers from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import logging from .tokenization_squeezebert import SqueezeBertTokenizer __A : Dict = logging.get_logger(__name__) __A : Any = {"vocab_file": "vocab.txt", "tokenizer_file": "tokenizer.json"} __A : Tuple = { "vocab_file": { "squeezebert/squeezebert-uncased": ( "https://huggingface.co/squeezebert/squeezebert-uncased/resolve/main/vocab.txt" ), "squeezebert/squeezebert-mnli": "https://huggingface.co/squeezebert/squeezebert-mnli/resolve/main/vocab.txt", "squeezebert/squeezebert-mnli-headless": ( "https://huggingface.co/squeezebert/squeezebert-mnli-headless/resolve/main/vocab.txt" ), }, "tokenizer_file": { "squeezebert/squeezebert-uncased": ( "https://huggingface.co/squeezebert/squeezebert-uncased/resolve/main/tokenizer.json" ), "squeezebert/squeezebert-mnli": ( "https://huggingface.co/squeezebert/squeezebert-mnli/resolve/main/tokenizer.json" ), "squeezebert/squeezebert-mnli-headless": ( "https://huggingface.co/squeezebert/squeezebert-mnli-headless/resolve/main/tokenizer.json" ), }, } __A : List[Any] = { "squeezebert/squeezebert-uncased": 512, "squeezebert/squeezebert-mnli": 512, "squeezebert/squeezebert-mnli-headless": 512, } __A : List[Any] = { "squeezebert/squeezebert-uncased": {"do_lower_case": True}, "squeezebert/squeezebert-mnli": {"do_lower_case": True}, "squeezebert/squeezebert-mnli-headless": {"do_lower_case": True}, } class A_ (a_ ): UpperCAmelCase__ = VOCAB_FILES_NAMES UpperCAmelCase__ = PRETRAINED_VOCAB_FILES_MAP UpperCAmelCase__ = PRETRAINED_INIT_CONFIGURATION UpperCAmelCase__ = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES UpperCAmelCase__ = SqueezeBertTokenizer def __init__( self , _A=None , _A=None , _A=True , _A="[UNK]" , _A="[SEP]" , _A="[PAD]" , _A="[CLS]" , _A="[MASK]" , _A=True , _A=None , **_A , ): '''simple docstring''' super().__init__( _A , tokenizer_file=_A , do_lower_case=_A , unk_token=_A , sep_token=_A , pad_token=_A , cls_token=_A , mask_token=_A , tokenize_chinese_chars=_A , strip_accents=_A , **_A , ) UpperCAmelCase = json.loads(self.backend_tokenizer.normalizer.__getstate__() ) if ( normalizer_state.get('''lowercase''' , _A ) != do_lower_case or normalizer_state.get('''strip_accents''' , _A ) != strip_accents or normalizer_state.get('''handle_chinese_chars''' , _A ) != tokenize_chinese_chars ): UpperCAmelCase = getattr(_A , normalizer_state.pop('''type''' ) ) UpperCAmelCase = do_lower_case UpperCAmelCase = strip_accents UpperCAmelCase = tokenize_chinese_chars UpperCAmelCase = normalizer_class(**_A ) UpperCAmelCase = do_lower_case def _lowercase ( self , _A , _A=None ): '''simple docstring''' UpperCAmelCase = [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 _lowercase ( self , _A , _A = None ): '''simple docstring''' UpperCAmelCase = [self.sep_token_id] UpperCAmelCase = [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 _lowercase ( self , _A , _A = None ): '''simple docstring''' UpperCAmelCase = self._tokenizer.model.save(_A , name=_A ) return tuple(_A )
273
0
import shutil import tempfile import unittest import numpy as np import pytest from transformers.testing_utils import require_vision from transformers.utils import is_vision_available if is_vision_available(): from PIL import Image from transformers import AutoProcessor, BertTokenizer, BlipImageProcessor, BlipProcessor, PreTrainedTokenizerFast @require_vision class _lowerCamelCase ( unittest.TestCase ): """simple docstring""" def UpperCAmelCase ( self ) -> int: '''simple docstring''' __snake_case : int = tempfile.mkdtemp() __snake_case : Dict = BlipImageProcessor() __snake_case : Optional[Any] = BertTokenizer.from_pretrained("hf-internal-testing/tiny-random-BertModel" ) __snake_case : Union[str, Any] = BlipProcessor(_A , _A ) processor.save_pretrained(self.tmpdirname ) def UpperCAmelCase ( self , **UpperCAmelCase ) -> Optional[Any]: '''simple docstring''' return AutoProcessor.from_pretrained(self.tmpdirname , **_A ).tokenizer def UpperCAmelCase ( self , **UpperCAmelCase ) -> Optional[Any]: '''simple docstring''' return AutoProcessor.from_pretrained(self.tmpdirname , **_A ).image_processor def UpperCAmelCase ( self ) -> int: '''simple docstring''' shutil.rmtree(self.tmpdirname ) def UpperCAmelCase ( self ) -> int: '''simple docstring''' __snake_case : int = [np.random.randint(255 , size=(3, 30, 400) , dtype=np.uinta )] __snake_case : int = [Image.fromarray(np.moveaxis(_A , 0 , -1 ) ) for x in image_inputs] return image_inputs def UpperCAmelCase ( self ) -> List[str]: '''simple docstring''' __snake_case : str = BlipProcessor(tokenizer=self.get_tokenizer() , image_processor=self.get_image_processor() ) processor.save_pretrained(self.tmpdirname ) __snake_case : Tuple = self.get_tokenizer(bos_token="(BOS)" , eos_token="(EOS)" ) __snake_case : Optional[int] = self.get_image_processor(do_normalize=_A , padding_value=1.0 ) __snake_case : Union[str, Any] = BlipProcessor.from_pretrained( self.tmpdirname , bos_token="(BOS)" , eos_token="(EOS)" , do_normalize=_A , padding_value=1.0 ) self.assertEqual(processor.tokenizer.get_vocab() , tokenizer_add_kwargs.get_vocab() ) self.assertIsInstance(processor.tokenizer , _A ) self.assertEqual(processor.image_processor.to_json_string() , image_processor_add_kwargs.to_json_string() ) self.assertIsInstance(processor.image_processor , _A ) def UpperCAmelCase ( self ) -> Dict: '''simple docstring''' __snake_case : List[str] = self.get_image_processor() __snake_case : int = self.get_tokenizer() __snake_case : Tuple = BlipProcessor(tokenizer=_A , image_processor=_A ) __snake_case : Optional[Any] = self.prepare_image_inputs() __snake_case : int = image_processor(_A , return_tensors="np" ) __snake_case : List[Any] = processor(images=_A , return_tensors="np" ) for key in input_feat_extract.keys(): self.assertAlmostEqual(input_feat_extract[key].sum() , input_processor[key].sum() , delta=1E-2 ) def UpperCAmelCase ( self ) -> Any: '''simple docstring''' __snake_case : Optional[int] = self.get_image_processor() __snake_case : Optional[Any] = self.get_tokenizer() __snake_case : Any = BlipProcessor(tokenizer=_A , image_processor=_A ) __snake_case : Optional[int] = "lower newer" __snake_case : Tuple = processor(text=_A ) __snake_case : Optional[Any] = tokenizer(_A , return_token_type_ids=_A ) for key in encoded_tok.keys(): self.assertListEqual(encoded_tok[key] , encoded_processor[key] ) def UpperCAmelCase ( self ) -> List[str]: '''simple docstring''' __snake_case : Tuple = self.get_image_processor() __snake_case : str = self.get_tokenizer() __snake_case : Any = BlipProcessor(tokenizer=_A , image_processor=_A ) __snake_case : Tuple = "lower newer" __snake_case : Union[str, Any] = self.prepare_image_inputs() __snake_case : Union[str, Any] = processor(text=_A , images=_A ) self.assertListEqual(list(inputs.keys() ) , ["pixel_values", "input_ids", "attention_mask"] ) # test if it raises when no input is passed with pytest.raises(_A ): processor() def UpperCAmelCase ( self ) -> List[Any]: '''simple docstring''' __snake_case : Tuple = self.get_image_processor() __snake_case : Tuple = self.get_tokenizer() __snake_case : str = BlipProcessor(tokenizer=_A , image_processor=_A ) __snake_case : str = [[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]] __snake_case : Union[str, Any] = processor.batch_decode(_A ) __snake_case : Any = tokenizer.batch_decode(_A ) self.assertListEqual(_A , _A ) def UpperCAmelCase ( self ) -> Any: '''simple docstring''' __snake_case : Union[str, Any] = self.get_image_processor() __snake_case : Optional[int] = self.get_tokenizer() __snake_case : Tuple = BlipProcessor(tokenizer=_A , image_processor=_A ) __snake_case : str = "lower newer" __snake_case : Any = self.prepare_image_inputs() __snake_case : int = processor(text=_A , images=_A ) # For now the processor supports only ['pixel_values', 'input_ids', 'attention_mask'] self.assertListEqual(list(inputs.keys() ) , ["pixel_values", "input_ids", "attention_mask"] )
326
import argparse import re from flax.traverse_util import flatten_dict, unflatten_dict from tax import checkpoints from transformers import SwitchTransformersConfig, SwitchTransformersForConditionalGeneration from transformers.modeling_flax_pytorch_utils import load_flax_weights_in_pytorch_model from transformers.utils import logging logging.set_verbosity_info() # should not include what is already done by the `from_pt` argument __A : int = { "/attention/": "/0/SelfAttention/", "/self_attention/": "/0/SelfAttention/", "/encoder_decoder_attention/": "/1/EncDecAttention/", "value": "v", "query": "q", "key": "k", "out": "o", "pre_self_attention_layer_norm": "0/layer_norm", "pre_cross_attention_layer_norm": "1/layer_norm", "pre_attention_layer_norm": "0/layer_norm", # previously 1, but seems wrong "token_embedder": "shared", "encoder_norm": "final_layer_norm", "decoder_norm": "final_layer_norm", "relpos_bias/rel_embedding": "block/0/layer/0/SelfAttention/relative_attention_bias/weight", "router/router_weights/w/": "router/classifier/", "roer/roer_weights/w/": "router/classifier/", "logits_dense": "lm_head", } def __SCREAMING_SNAKE_CASE ( UpperCamelCase__ ) -> List[str]: '''simple docstring''' UpperCAmelCase = list(s_dict.keys() ) for key in keys: UpperCAmelCase = R'''.*/layers_(\d+)''' UpperCAmelCase = key if re.match(UpperCamelCase__ , UpperCamelCase__ ): UpperCAmelCase = re.sub(R'''layers_(\d+)''' , R'''block/\1/layer''' , UpperCamelCase__ ) UpperCAmelCase = R'''(encoder|decoder)\/''' if re.match(UpperCamelCase__ , UpperCamelCase__ ): UpperCAmelCase = re.match(UpperCamelCase__ , UpperCamelCase__ ).groups() if groups[0] == "encoder": UpperCAmelCase = re.sub(R'''/mlp/''' , R'''/1/mlp/''' , UpperCamelCase__ ) UpperCAmelCase = re.sub(R'''/pre_mlp_layer_norm/''' , R'''/1/layer_norm/''' , UpperCamelCase__ ) elif groups[0] == "decoder": UpperCAmelCase = re.sub(R'''/mlp/''' , R'''/2/mlp/''' , UpperCamelCase__ ) UpperCAmelCase = re.sub(R'''/pre_mlp_layer_norm/''' , R'''/2/layer_norm/''' , UpperCamelCase__ ) # 2. Convert other classic mappings for old_key, temp_key in MOE_LAYER_NAME_MAPPING.items(): if old_key in new_key: UpperCAmelCase = new_key.replace(UpperCamelCase__ , UpperCamelCase__ ) print(F"""{key} -> {new_key}""" ) UpperCAmelCase = s_dict.pop(UpperCamelCase__ ) if "encoder/block/0/layer/0/SelfAttention/relative_attention_bias/weight" in s_dict: UpperCAmelCase = s_dict[ '''encoder/block/0/layer/0/SelfAttention/relative_attention_bias/weight''' ].T if "decoder/block/0/layer/0/SelfAttention/relative_attention_bias/weight" in s_dict: UpperCAmelCase = s_dict[ '''decoder/block/0/layer/0/SelfAttention/relative_attention_bias/weight''' ].T # 3. Take extra care of the EXPERTS layer for key in list(s_dict.keys() ): if "expert" in key: UpperCAmelCase = s_dict[key].shape[0] UpperCAmelCase = s_dict[key] for idx in range(UpperCamelCase__ ): UpperCAmelCase = expert_weihts[idx] print(F"""{key} -> {key.replace("expert/" , "nested fstring" )}""" ) s_dict.pop(UpperCamelCase__ ) return s_dict __A : Optional[int] = { "NUM_ENCODER_LAYERS": "num_layers", "NUM_DECODER_LAYERS": "num_decoder_layers", "NUM_HEADS": "num_heads", "HEAD_DIM": "d_kv", "EMBED_DIM": "d_model", "MLP_DIM": "d_ff", "NUM_SELECTED_EXPERTS": "num_selected_experts", "NUM_ENCODER_SPARSE_LAYERS": "num_sparse_encoder_layers", "NUM_DECODER_SPARSE_LAYERS": "num_sparse_decoder_layers", "dense.MlpBlock.activations": "feed_forward_proj", } def __SCREAMING_SNAKE_CASE ( UpperCamelCase__ , UpperCamelCase__ ) -> List[str]: '''simple docstring''' import regex as re with open(UpperCamelCase__ , '''r''' ) as f: UpperCAmelCase = f.read() UpperCAmelCase = re.findall(R'''(.*) = ([0-9.]*)''' , UpperCamelCase__ ) UpperCAmelCase = {} for param, value in regex_match: if param in GIN_TO_CONFIG_MAPPING and value != "": UpperCAmelCase = float(UpperCamelCase__ ) if '''.''' in value else int(UpperCamelCase__ ) UpperCAmelCase = re.findall(R'''(.*activations) = \(\'(.*)\',\)''' , UpperCamelCase__ )[0] UpperCAmelCase = str(activation[1] ) UpperCAmelCase = num_experts UpperCAmelCase = SwitchTransformersConfig(**UpperCamelCase__ ) return config def __SCREAMING_SNAKE_CASE ( UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__=None , UpperCamelCase__="./" , UpperCamelCase__=8 ) -> List[Any]: '''simple docstring''' print(F"""Loading flax weights from : {flax_checkpoint_path}""" ) UpperCAmelCase = checkpoints.load_tax_checkpoint(UpperCamelCase__ ) if gin_file is not None: UpperCAmelCase = convert_gin_to_config(UpperCamelCase__ , UpperCamelCase__ ) else: UpperCAmelCase = SwitchTransformersConfig.from_pretrained(UpperCamelCase__ ) UpperCAmelCase = SwitchTransformersForConditionalGeneration(UpperCamelCase__ ) UpperCAmelCase = flax_params['''target'''] UpperCAmelCase = flatten_dict(UpperCamelCase__ , sep='''/''' ) UpperCAmelCase = rename_keys(UpperCamelCase__ ) UpperCAmelCase = unflatten_dict(UpperCamelCase__ , sep='''/''' ) # Load the flax params in the PT model load_flax_weights_in_pytorch_model(UpperCamelCase__ , UpperCamelCase__ ) print(F"""Save PyTorch model to {pytorch_dump_path}""" ) pt_model.save_pretrained(UpperCamelCase__ ) if __name__ == "__main__": __A : Tuple = argparse.ArgumentParser() # Required parameters parser.add_argument( "--switch_t5x_checkpoint_path", default=None, type=str, required=True, help=( "The config json file corresponding to the pre-trained SwitchTransformers model. \nThis specifies the" " model architecture. If not provided, a `gin_file` has to be provided." ), ) parser.add_argument( "--gin_file", default=None, type=str, required=False, help="Path to the gin config file. If not provided, a `config_file` has to be passed ", ) parser.add_argument( "--config_name", default=None, type=str, required=False, help="Config name of SwitchTransformers model." ) parser.add_argument( "--pytorch_dump_folder_path", default=None, type=str, required=True, help="Path to the output pytorch model." ) parser.add_argument("--num_experts", default=8, type=int, required=False, help="Number of experts") __A : Tuple = parser.parse_args() convert_flax_checkpoint_to_pytorch( args.switch_tax_checkpoint_path, args.config_name, args.gin_file, args.pytorch_dump_folder_path, args.num_experts, )
273
0
"""simple docstring""" import argparse import os import shutil import torch from emmental.modules import MagnitudeBinarizer, ThresholdBinarizer, TopKBinarizer def a__ ( __SCREAMING_SNAKE_CASE ) -> Dict: __lowerCAmelCase: int = args.pruning_method __lowerCAmelCase: Union[str, Any] = args.threshold __lowerCAmelCase: str = args.model_name_or_path.rstrip("/" ) __lowerCAmelCase: Optional[int] = args.target_model_path print(F"Load fine-pruned model from {model_name_or_path}" ) __lowerCAmelCase: Dict = torch.load(os.path.join(UpperCamelCase__ , "pytorch_model.bin" ) ) __lowerCAmelCase: List[str] = {} for name, tensor in model.items(): if "embeddings" in name or "LayerNorm" in name or "pooler" in name: __lowerCAmelCase: Optional[int] = tensor print(F"Copied layer {name}" ) elif "classifier" in name or "qa_output" in name: __lowerCAmelCase: Dict = tensor print(F"Copied layer {name}" ) elif "bias" in name: __lowerCAmelCase: Dict = tensor print(F"Copied layer {name}" ) else: if pruning_method == "magnitude": __lowerCAmelCase: List[Any] = MagnitudeBinarizer.apply(inputs=UpperCamelCase__ , threshold=UpperCamelCase__ ) __lowerCAmelCase: List[Any] = tensor * mask print(F"Pruned layer {name}" ) elif pruning_method == "topK": if "mask_scores" in name: continue __lowerCAmelCase: int = name[:-6] __lowerCAmelCase: int = model[F"{prefix_}mask_scores"] __lowerCAmelCase: List[str] = TopKBinarizer.apply(UpperCamelCase__ , UpperCamelCase__ ) __lowerCAmelCase: Dict = tensor * mask print(F"Pruned layer {name}" ) elif pruning_method == "sigmoied_threshold": if "mask_scores" in name: continue __lowerCAmelCase: Any = name[:-6] __lowerCAmelCase: Optional[int] = model[F"{prefix_}mask_scores"] __lowerCAmelCase: Optional[Any] = ThresholdBinarizer.apply(UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ ) __lowerCAmelCase: Any = tensor * mask print(F"Pruned layer {name}" ) elif pruning_method == "l0": if "mask_scores" in name: continue __lowerCAmelCase: List[str] = name[:-6] __lowerCAmelCase: Any = model[F"{prefix_}mask_scores"] __lowerCAmelCase , __lowerCAmelCase: Dict = -0.1, 1.1 __lowerCAmelCase: Optional[Any] = torch.sigmoid(UpperCamelCase__ ) __lowerCAmelCase: Union[str, Any] = s * (r - l) + l __lowerCAmelCase: Optional[int] = s_bar.clamp(min=0.0 , max=1.0 ) __lowerCAmelCase: int = tensor * mask print(F"Pruned layer {name}" ) else: raise ValueError("Unknown pruning method" ) if target_model_path is None: __lowerCAmelCase: List[str] = os.path.join( os.path.dirname(UpperCamelCase__ ) , F"bertarized_{os.path.basename(UpperCamelCase__ )}" ) if not os.path.isdir(UpperCamelCase__ ): shutil.copytree(UpperCamelCase__ , UpperCamelCase__ ) print(F"\nCreated folder {target_model_path}" ) torch.save(UpperCamelCase__ , os.path.join(UpperCamelCase__ , "pytorch_model.bin" ) ) print("\nPruned model saved! See you later!" ) if __name__ == "__main__": __A = argparse.ArgumentParser() parser.add_argument( "--pruning_method", choices=["l0", "magnitude", "topK", "sigmoied_threshold"], type=str, required=True, help=( "Pruning Method (l0 = L0 regularization, magnitude = Magnitude pruning, topK = Movement pruning," " sigmoied_threshold = Soft movement pruning)" ), ) parser.add_argument( "--threshold", type=float, required=False, help=( "For `magnitude` and `topK`, it is the level of remaining weights (in %) in the fine-pruned model." "For `sigmoied_threshold`, it is the threshold \tau against which the (sigmoied) scores are compared." "Not needed for `l0`" ), ) parser.add_argument( "--model_name_or_path", type=str, required=True, help="Folder containing the model that was previously fine-pruned", ) parser.add_argument( "--target_model_path", default=None, type=str, required=False, help="Folder containing the model that was previously fine-pruned", ) __A = parser.parse_args() main(args)
217
import tempfile import numpy as np import torch from transformers import AutoTokenizer, TaEncoderModel from diffusers import DDPMScheduler, UNetaDConditionModel from diffusers.models.attention_processor import AttnAddedKVProcessor from diffusers.pipelines.deepfloyd_if import IFWatermarker from diffusers.utils.testing_utils import torch_device from ..test_pipelines_common import to_np class A_ : def _lowercase ( self ): '''simple docstring''' torch.manual_seed(0 ) UpperCAmelCase = TaEncoderModel.from_pretrained('''hf-internal-testing/tiny-random-t5''' ) torch.manual_seed(0 ) UpperCAmelCase = AutoTokenizer.from_pretrained('''hf-internal-testing/tiny-random-t5''' ) torch.manual_seed(0 ) UpperCAmelCase = UNetaDConditionModel( sample_size=3_2 , layers_per_block=1 , block_out_channels=[3_2, 6_4] , down_block_types=[ '''ResnetDownsampleBlock2D''', '''SimpleCrossAttnDownBlock2D''', ] , mid_block_type='''UNetMidBlock2DSimpleCrossAttn''' , up_block_types=['''SimpleCrossAttnUpBlock2D''', '''ResnetUpsampleBlock2D'''] , in_channels=3 , out_channels=6 , cross_attention_dim=3_2 , encoder_hid_dim=3_2 , attention_head_dim=8 , addition_embed_type='''text''' , addition_embed_type_num_heads=2 , cross_attention_norm='''group_norm''' , resnet_time_scale_shift='''scale_shift''' , act_fn='''gelu''' , ) unet.set_attn_processor(AttnAddedKVProcessor() ) # For reproducibility tests torch.manual_seed(0 ) UpperCAmelCase = DDPMScheduler( num_train_timesteps=1_0_0_0 , beta_schedule='''squaredcos_cap_v2''' , beta_start=0.00_01 , beta_end=0.02 , thresholding=_A , dynamic_thresholding_ratio=0.95 , sample_max_value=1.0 , prediction_type='''epsilon''' , variance_type='''learned_range''' , ) torch.manual_seed(0 ) UpperCAmelCase = IFWatermarker() return { "text_encoder": text_encoder, "tokenizer": tokenizer, "unet": unet, "scheduler": scheduler, "watermarker": watermarker, "safety_checker": None, "feature_extractor": None, } def _lowercase ( self ): '''simple docstring''' torch.manual_seed(0 ) UpperCAmelCase = TaEncoderModel.from_pretrained('''hf-internal-testing/tiny-random-t5''' ) torch.manual_seed(0 ) UpperCAmelCase = AutoTokenizer.from_pretrained('''hf-internal-testing/tiny-random-t5''' ) torch.manual_seed(0 ) UpperCAmelCase = UNetaDConditionModel( sample_size=3_2 , layers_per_block=[1, 2] , block_out_channels=[3_2, 6_4] , down_block_types=[ '''ResnetDownsampleBlock2D''', '''SimpleCrossAttnDownBlock2D''', ] , mid_block_type='''UNetMidBlock2DSimpleCrossAttn''' , up_block_types=['''SimpleCrossAttnUpBlock2D''', '''ResnetUpsampleBlock2D'''] , in_channels=6 , out_channels=6 , cross_attention_dim=3_2 , encoder_hid_dim=3_2 , attention_head_dim=8 , addition_embed_type='''text''' , addition_embed_type_num_heads=2 , cross_attention_norm='''group_norm''' , resnet_time_scale_shift='''scale_shift''' , act_fn='''gelu''' , class_embed_type='''timestep''' , mid_block_scale_factor=1.4_14 , time_embedding_act_fn='''gelu''' , time_embedding_dim=3_2 , ) unet.set_attn_processor(AttnAddedKVProcessor() ) # For reproducibility tests torch.manual_seed(0 ) UpperCAmelCase = DDPMScheduler( num_train_timesteps=1_0_0_0 , beta_schedule='''squaredcos_cap_v2''' , beta_start=0.00_01 , beta_end=0.02 , thresholding=_A , dynamic_thresholding_ratio=0.95 , sample_max_value=1.0 , prediction_type='''epsilon''' , variance_type='''learned_range''' , ) torch.manual_seed(0 ) UpperCAmelCase = DDPMScheduler( num_train_timesteps=1_0_0_0 , beta_schedule='''squaredcos_cap_v2''' , beta_start=0.00_01 , beta_end=0.02 , ) torch.manual_seed(0 ) UpperCAmelCase = IFWatermarker() return { "text_encoder": text_encoder, "tokenizer": tokenizer, "unet": unet, "scheduler": scheduler, "image_noising_scheduler": image_noising_scheduler, "watermarker": watermarker, "safety_checker": None, "feature_extractor": None, } def _lowercase ( self ): '''simple docstring''' UpperCAmelCase = self.get_dummy_components() UpperCAmelCase = self.pipeline_class(**_A ) pipe.to(_A ) pipe.set_progress_bar_config(disable=_A ) UpperCAmelCase = self.get_dummy_inputs(_A ) UpperCAmelCase = inputs['''prompt'''] UpperCAmelCase = inputs['''generator'''] UpperCAmelCase = inputs['''num_inference_steps'''] UpperCAmelCase = inputs['''output_type'''] if "image" in inputs: UpperCAmelCase = inputs['''image'''] else: UpperCAmelCase = None if "mask_image" in inputs: UpperCAmelCase = inputs['''mask_image'''] else: UpperCAmelCase = None if "original_image" in inputs: UpperCAmelCase = inputs['''original_image'''] else: UpperCAmelCase = None UpperCAmelCase , UpperCAmelCase = pipe.encode_prompt(_A ) # inputs with prompt converted to embeddings UpperCAmelCase = { '''prompt_embeds''': prompt_embeds, '''negative_prompt_embeds''': negative_prompt_embeds, '''generator''': generator, '''num_inference_steps''': num_inference_steps, '''output_type''': output_type, } if image is not None: UpperCAmelCase = image if mask_image is not None: UpperCAmelCase = mask_image if original_image is not None: UpperCAmelCase = original_image # set all optional components to None for optional_component in pipe._optional_components: setattr(_A , _A , _A ) UpperCAmelCase = pipe(**_A )[0] with tempfile.TemporaryDirectory() as tmpdir: pipe.save_pretrained(_A ) UpperCAmelCase = self.pipeline_class.from_pretrained(_A ) pipe_loaded.to(_A ) pipe_loaded.set_progress_bar_config(disable=_A ) pipe_loaded.unet.set_attn_processor(AttnAddedKVProcessor() ) # For reproducibility tests for optional_component in pipe._optional_components: self.assertTrue( getattr(_A , _A ) is None , F"""`{optional_component}` did not stay set to None after loading.""" , ) UpperCAmelCase = self.get_dummy_inputs(_A ) UpperCAmelCase = inputs['''generator'''] UpperCAmelCase = inputs['''num_inference_steps'''] UpperCAmelCase = inputs['''output_type'''] # inputs with prompt converted to embeddings UpperCAmelCase = { '''prompt_embeds''': prompt_embeds, '''negative_prompt_embeds''': negative_prompt_embeds, '''generator''': generator, '''num_inference_steps''': num_inference_steps, '''output_type''': output_type, } if image is not None: UpperCAmelCase = image if mask_image is not None: UpperCAmelCase = mask_image if original_image is not None: UpperCAmelCase = original_image UpperCAmelCase = pipe_loaded(**_A )[0] UpperCAmelCase = np.abs(to_np(_A ) - to_np(_A ) ).max() self.assertLess(_A , 1E-4 ) def _lowercase ( self ): '''simple docstring''' UpperCAmelCase = self.get_dummy_components() UpperCAmelCase = self.pipeline_class(**_A ) pipe.to(_A ) pipe.set_progress_bar_config(disable=_A ) UpperCAmelCase = self.get_dummy_inputs(_A ) UpperCAmelCase = pipe(**_A )[0] with tempfile.TemporaryDirectory() as tmpdir: pipe.save_pretrained(_A ) UpperCAmelCase = self.pipeline_class.from_pretrained(_A ) pipe_loaded.to(_A ) pipe_loaded.set_progress_bar_config(disable=_A ) pipe_loaded.unet.set_attn_processor(AttnAddedKVProcessor() ) # For reproducibility tests UpperCAmelCase = self.get_dummy_inputs(_A ) UpperCAmelCase = pipe_loaded(**_A )[0] UpperCAmelCase = np.abs(to_np(_A ) - to_np(_A ) ).max() self.assertLess(_A , 1E-4 )
273
0
import darl # noqa import gym import tqdm from diffusers.experimental import ValueGuidedRLPipeline _lowerCAmelCase : Any = { "n_samples": 64, "horizon": 32, "num_inference_steps": 20, "n_guide_steps": 2, # can set to 0 for faster sampling, does not use value network "scale_grad_by_std": True, "scale": 0.1, "eta": 0.0, "t_grad_cutoff": 2, "device": "cpu", } if __name__ == "__main__": _lowerCAmelCase : Dict = "hopper-medium-v2" _lowerCAmelCase : int = gym.make(env_name) _lowerCAmelCase : Tuple = ValueGuidedRLPipeline.from_pretrained( '''bglick13/hopper-medium-v2-value-function-hor32''', env=env, ) env.seed(0) _lowerCAmelCase : List[str] = env.reset() _lowerCAmelCase : Dict = 0 _lowerCAmelCase : Optional[int] = 0 _lowerCAmelCase : Any = 1_000 _lowerCAmelCase : int = [obs.copy()] try: for t in tqdm.tqdm(range(T)): # call the policy _lowerCAmelCase : Dict = pipeline(obs, planning_horizon=32) # execute action in environment _lowerCAmelCase : Tuple = env.step(denorm_actions) _lowerCAmelCase : Optional[Any] = env.get_normalized_score(total_reward) # update return total_reward += reward total_score += score print( F'''Step: {t}, Reward: {reward}, Total Reward: {total_reward}, Score: {score}, Total Score:''' F''' {total_score}''' ) # save observations for rendering rollout.append(next_observation.copy()) _lowerCAmelCase : List[str] = next_observation except KeyboardInterrupt: pass print(F'''Total reward: {total_reward}''')
300
from __future__ import annotations from collections import namedtuple def __SCREAMING_SNAKE_CASE ( UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ ) -> tuple: '''simple docstring''' UpperCAmelCase = namedtuple('''result''' , '''name value''' ) if (voltage, current, power).count(0 ) != 1: raise ValueError('''Only one argument must be 0''' ) elif power < 0: raise ValueError( '''Power cannot be negative in any electrical/electronics system''' ) elif voltage == 0: return result('''voltage''' , power / current ) elif current == 0: return result('''current''' , power / voltage ) elif power == 0: return result('''power''' , float(round(abs(voltage * current ) , 2 ) ) ) else: raise ValueError('''Exactly one argument must be 0''' ) if __name__ == "__main__": import doctest doctest.testmod()
273
0
"""simple docstring""" from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_tf_available, is_tokenizers_available, is_torch_available, ) lowerCAmelCase_ = { "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_ = ["FunnelTokenizerFast"] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase_ = [ "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_ = [ "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_ = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
16
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_tf_available, is_tokenizers_available, is_torch_available, ) __A : Dict = { "configuration_roformer": ["ROFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP", "RoFormerConfig", "RoFormerOnnxConfig"], "tokenization_roformer": ["RoFormerTokenizer"], } try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __A : Union[str, Any] = ["RoFormerTokenizerFast"] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __A : str = [ "ROFORMER_PRETRAINED_MODEL_ARCHIVE_LIST", "RoFormerForCausalLM", "RoFormerForMaskedLM", "RoFormerForMultipleChoice", "RoFormerForQuestionAnswering", "RoFormerForSequenceClassification", "RoFormerForTokenClassification", "RoFormerLayer", "RoFormerModel", "RoFormerPreTrainedModel", "load_tf_weights_in_roformer", ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __A : int = [ "TF_ROFORMER_PRETRAINED_MODEL_ARCHIVE_LIST", "TFRoFormerForCausalLM", "TFRoFormerForMaskedLM", "TFRoFormerForMultipleChoice", "TFRoFormerForQuestionAnswering", "TFRoFormerForSequenceClassification", "TFRoFormerForTokenClassification", "TFRoFormerLayer", "TFRoFormerModel", "TFRoFormerPreTrainedModel", ] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __A : Any = [ "FLAX_ROFORMER_PRETRAINED_MODEL_ARCHIVE_LIST", "FlaxRoFormerForMaskedLM", "FlaxRoFormerForMultipleChoice", "FlaxRoFormerForQuestionAnswering", "FlaxRoFormerForSequenceClassification", "FlaxRoFormerForTokenClassification", "FlaxRoFormerModel", "FlaxRoFormerPreTrainedModel", ] if TYPE_CHECKING: from .configuration_roformer import ROFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP, RoFormerConfig, RoFormerOnnxConfig from .tokenization_roformer import RoFormerTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_roformer_fast import RoFormerTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_roformer import ( ROFORMER_PRETRAINED_MODEL_ARCHIVE_LIST, RoFormerForCausalLM, RoFormerForMaskedLM, RoFormerForMultipleChoice, RoFormerForQuestionAnswering, RoFormerForSequenceClassification, RoFormerForTokenClassification, RoFormerLayer, RoFormerModel, RoFormerPreTrainedModel, load_tf_weights_in_roformer, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_roformer import ( TF_ROFORMER_PRETRAINED_MODEL_ARCHIVE_LIST, TFRoFormerForCausalLM, TFRoFormerForMaskedLM, TFRoFormerForMultipleChoice, TFRoFormerForQuestionAnswering, TFRoFormerForSequenceClassification, TFRoFormerForTokenClassification, TFRoFormerLayer, TFRoFormerModel, TFRoFormerPreTrainedModel, ) try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_flax_roformer import ( FLAX_ROFORMER_PRETRAINED_MODEL_ARCHIVE_LIST, FlaxRoFormerForMaskedLM, FlaxRoFormerForMultipleChoice, FlaxRoFormerForQuestionAnswering, FlaxRoFormerForSequenceClassification, FlaxRoFormerForTokenClassification, FlaxRoFormerModel, FlaxRoFormerPreTrainedModel, ) else: import sys __A : int = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
273
0
from typing import Dict, List, Optional, Union import numpy as np from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size_dict from ...image_transforms import ( center_crop, get_resize_output_image_size, normalize, rescale, resize, to_channel_dimension_format, ) from ...image_utils import ( IMAGENET_DEFAULT_MEAN, IMAGENET_DEFAULT_STD, ChannelDimension, ImageInput, PILImageResampling, is_batched, to_numpy_array, valid_images, ) from ...utils import TensorType, logging UpperCAmelCase = logging.get_logger(__name__) class A_ ( a_ ): '''simple docstring''' _UpperCamelCase : Union[str, Any] = ["""pixel_values"""] def __init__( self , snake_case = True , snake_case = None , snake_case = PILImageResampling.BICUBIC , snake_case = True , snake_case = True , snake_case = 1 / 255 , snake_case = None , snake_case = True , snake_case = None , snake_case = None , **snake_case , ): super().__init__(**_A ) lowercase = size if size is not None else {'height': 224, 'width': 224} lowercase = get_size_dict(_A ) lowercase = crop_size if crop_size is not None else {'height': 224, 'width': 224} lowercase = get_size_dict(_A , default_to_square=_A , param_name='crop_size' ) lowercase = do_resize lowercase = do_rescale lowercase = do_normalize lowercase = do_center_crop lowercase = crop_size lowercase = size lowercase = resample lowercase = rescale_factor lowercase = image_mean if image_mean is not None else IMAGENET_DEFAULT_MEAN lowercase = image_std if image_std is not None else IMAGENET_DEFAULT_STD def SCREAMING_SNAKE_CASE__ ( self , snake_case , snake_case , snake_case = PILImageResampling.BILINEAR , snake_case = None , **snake_case , ): lowercase = get_size_dict(_A ) if "shortest_edge" in size: lowercase = get_resize_output_image_size(_A , size=size['shortest_edge'] , default_to_square=_A ) # size = get_resize_output_image_size(image, size["shortest_edge"], size["longest_edge"]) elif "height" in size and "width" in size: lowercase = (size['height'], size['width']) else: raise ValueError(F'''Size must contain \'height\' and \'width\' keys or \'shortest_edge\' key. Got {size.keys()}''' ) return resize(_A , size=_A , resample=_A , data_format=_A , **_A ) def SCREAMING_SNAKE_CASE__ ( self , snake_case , snake_case , snake_case = None , **snake_case , ): lowercase = get_size_dict(_A ) if "height" not in size or "width" not in size: raise ValueError(F'''The `size` parameter must contain the keys (height, width). Got {size.keys()}''' ) return center_crop(_A , size=(size['height'], size['width']) , data_format=_A , **_A ) def SCREAMING_SNAKE_CASE__ ( self , snake_case , snake_case , snake_case = None , **snake_case ): return rescale(_A , scale=_A , data_format=_A , **_A ) def SCREAMING_SNAKE_CASE__ ( self , snake_case , snake_case , snake_case , snake_case = None , **snake_case , ): return normalize(_A , mean=_A , std=_A , data_format=_A , **_A ) def SCREAMING_SNAKE_CASE__ ( self , snake_case , snake_case = None , snake_case = None , snake_case = None , snake_case = None , snake_case = None , snake_case = None , snake_case = None , snake_case = None , snake_case = None , snake_case = None , snake_case = None , snake_case = ChannelDimension.FIRST , **snake_case , ): lowercase = do_resize if do_resize is not None else self.do_resize lowercase = do_rescale if do_rescale is not None else self.do_rescale lowercase = do_normalize if do_normalize is not None else self.do_normalize lowercase = do_center_crop if do_center_crop is not None else self.do_center_crop lowercase = crop_size if crop_size is not None else self.crop_size lowercase = get_size_dict(_A , param_name='crop_size' , default_to_square=_A ) lowercase = resample if resample is not None else self.resample lowercase = rescale_factor if rescale_factor is not None else self.rescale_factor lowercase = image_mean if image_mean is not None else self.image_mean lowercase = image_std if image_std is not None else self.image_std lowercase = size if size is not None else self.size lowercase = get_size_dict(_A ) if not is_batched(_A ): lowercase = [images] if not valid_images(_A ): raise ValueError( 'Invalid image type. Must be of type PIL.Image.Image, numpy.ndarray, ' 'torch.Tensor, tf.Tensor or jax.ndarray.' ) if do_resize and size is None: raise ValueError('Size must be specified if do_resize is True.' ) if do_center_crop and crop_size is None: raise ValueError('Crop size must be specified if do_center_crop is True.' ) if do_rescale and rescale_factor is None: raise ValueError('Rescale factor must be specified if do_rescale is True.' ) # All transformations expect numpy arrays. lowercase = [to_numpy_array(_A ) for image in images] if do_resize: lowercase = [self.resize(image=_A , size=_A , resample=_A ) for image in images] if do_center_crop: lowercase = [self.center_crop(image=_A , size=_A ) for image in images] if do_rescale: lowercase = [self.rescale(image=_A , scale=_A ) for image in images] if do_normalize: lowercase = [self.normalize(image=_A , mean=_A , std=_A ) for image in images] lowercase = [to_channel_dimension_format(_A , _A ) for image in images] lowercase = {'pixel_values': images} return BatchFeature(data=_A , tensor_type=_A )
195
import argparse import torch from transformers import YosoConfig, YosoForMaskedLM def __SCREAMING_SNAKE_CASE ( UpperCamelCase__ ) -> Union[str, Any]: '''simple docstring''' if "model" in orig_key: UpperCAmelCase = orig_key.replace('''model.''' , '''''' ) if "norm1" in orig_key: UpperCAmelCase = orig_key.replace('''norm1''' , '''attention.output.LayerNorm''' ) if "norm2" in orig_key: UpperCAmelCase = orig_key.replace('''norm2''' , '''output.LayerNorm''' ) if "norm" in orig_key: UpperCAmelCase = orig_key.replace('''norm''' , '''LayerNorm''' ) if "transformer" in orig_key: UpperCAmelCase = orig_key.split('''.''' )[0].split('''_''' )[-1] UpperCAmelCase = orig_key.replace(F"""transformer_{layer_num}""" , F"""encoder.layer.{layer_num}""" ) if "mha.attn" in orig_key: UpperCAmelCase = orig_key.replace('''mha.attn''' , '''attention.self''' ) if "mha" in orig_key: UpperCAmelCase = orig_key.replace('''mha''' , '''attention''' ) if "W_q" in orig_key: UpperCAmelCase = orig_key.replace('''W_q''' , '''self.query''' ) if "W_k" in orig_key: UpperCAmelCase = orig_key.replace('''W_k''' , '''self.key''' ) if "W_v" in orig_key: UpperCAmelCase = orig_key.replace('''W_v''' , '''self.value''' ) if "ff1" in orig_key: UpperCAmelCase = orig_key.replace('''ff1''' , '''intermediate.dense''' ) if "ff2" in orig_key: UpperCAmelCase = orig_key.replace('''ff2''' , '''output.dense''' ) if "ff" in orig_key: UpperCAmelCase = orig_key.replace('''ff''' , '''output.dense''' ) if "mlm_class" in orig_key: UpperCAmelCase = orig_key.replace('''mlm.mlm_class''' , '''cls.predictions.decoder''' ) if "mlm" in orig_key: UpperCAmelCase = orig_key.replace('''mlm''' , '''cls.predictions.transform''' ) if "cls" not in orig_key: UpperCAmelCase = '''yoso.''' + orig_key return orig_key def __SCREAMING_SNAKE_CASE ( UpperCamelCase__ , UpperCamelCase__ ) -> Dict: '''simple docstring''' for key in orig_state_dict.copy().keys(): UpperCAmelCase = orig_state_dict.pop(UpperCamelCase__ ) if ("pooler" in key) or ("sen_class" in key): continue else: UpperCAmelCase = val UpperCAmelCase = orig_state_dict['''cls.predictions.decoder.bias'''] UpperCAmelCase = torch.arange(UpperCamelCase__ ).expand((1, -1) ) + 2 return orig_state_dict def __SCREAMING_SNAKE_CASE ( UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ ) -> int: '''simple docstring''' UpperCAmelCase = torch.load(UpperCamelCase__ , map_location='''cpu''' )['''model_state_dict'''] UpperCAmelCase = YosoConfig.from_json_file(UpperCamelCase__ ) UpperCAmelCase = YosoForMaskedLM(UpperCamelCase__ ) UpperCAmelCase = convert_checkpoint_helper(config.max_position_embeddings , UpperCamelCase__ ) print(model.load_state_dict(UpperCamelCase__ ) ) model.eval() model.save_pretrained(UpperCamelCase__ ) print(F"""Checkpoint successfuly converted. Model saved at {pytorch_dump_path}""" ) if __name__ == "__main__": __A : Any = argparse.ArgumentParser() # Required parameters parser.add_argument( "--pytorch_model_path", default=None, type=str, required=True, help="Path to YOSO pytorch checkpoint." ) parser.add_argument( "--config_file", default=None, type=str, required=True, help="The json file for YOSO model config.", ) parser.add_argument( "--pytorch_dump_path", default=None, type=str, required=True, help="Path to the output PyTorch model." ) __A : List[str] = parser.parse_args() convert_yoso_checkpoint(args.pytorch_model_path, args.config_file, args.pytorch_dump_path)
273
0
'''simple docstring''' import logging from transformers.configuration_utils import PretrainedConfig lowercase__ : List[str] = logging.getLogger(__name__) class __lowerCAmelCase ( a_ ): """simple docstring""" _snake_case : Optional[int] = 'masked_bert' def __init__( self : Optional[int] , lowerCAmelCase__ : Dict=30522 , lowerCAmelCase__ : str=768 , lowerCAmelCase__ : List[str]=12 , lowerCAmelCase__ : Dict=12 , lowerCAmelCase__ : Dict=3072 , lowerCAmelCase__ : str="gelu" , lowerCAmelCase__ : Optional[Any]=0.1 , lowerCAmelCase__ : List[Any]=0.1 , lowerCAmelCase__ : Dict=512 , lowerCAmelCase__ : Dict=2 , lowerCAmelCase__ : Union[str, Any]=0.02 , lowerCAmelCase__ : Tuple=1e-1_2 , lowerCAmelCase__ : Optional[int]=0 , lowerCAmelCase__ : Tuple="topK" , lowerCAmelCase__ : Optional[Any]="constant" , lowerCAmelCase__ : Optional[int]=0.0 , **lowerCAmelCase__ : Dict , ) -> int: '''simple docstring''' super().__init__(pad_token_id=_A , **_A ) _UpperCamelCase = vocab_size _UpperCamelCase = hidden_size _UpperCamelCase = num_hidden_layers _UpperCamelCase = num_attention_heads _UpperCamelCase = hidden_act _UpperCamelCase = intermediate_size _UpperCamelCase = hidden_dropout_prob _UpperCamelCase = attention_probs_dropout_prob _UpperCamelCase = max_position_embeddings _UpperCamelCase = type_vocab_size _UpperCamelCase = initializer_range _UpperCamelCase = layer_norm_eps _UpperCamelCase = pruning_method _UpperCamelCase = mask_init _UpperCamelCase = mask_scale
324
def __SCREAMING_SNAKE_CASE ( UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ ) -> int: '''simple docstring''' if exponent == 1: return base if exponent % 2 == 0: UpperCAmelCase = _modexpt(UpperCamelCase__ , exponent // 2 , UpperCamelCase__ ) % modulo_value return (x * x) % modulo_value else: return (base * _modexpt(UpperCamelCase__ , exponent - 1 , UpperCamelCase__ )) % modulo_value def __SCREAMING_SNAKE_CASE ( UpperCamelCase__ = 1777 , UpperCamelCase__ = 1855 , UpperCamelCase__ = 8 ) -> int: '''simple docstring''' UpperCAmelCase = base for _ in range(1 , UpperCamelCase__ ): UpperCAmelCase = _modexpt(UpperCamelCase__ , UpperCamelCase__ , 10**digits ) return result if __name__ == "__main__": print(F'{solution() = }')
273
0
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tensorflow_text_available, is_torch_available lowerCamelCase_ = { "configuration_ernie": ["ERNIE_PRETRAINED_CONFIG_ARCHIVE_MAP", "ErnieConfig", "ErnieOnnxConfig"], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCamelCase_ = [ "ERNIE_PRETRAINED_MODEL_ARCHIVE_LIST", "ErnieForCausalLM", "ErnieForMaskedLM", "ErnieForMultipleChoice", "ErnieForNextSentencePrediction", "ErnieForPreTraining", "ErnieForQuestionAnswering", "ErnieForSequenceClassification", "ErnieForTokenClassification", "ErnieModel", "ErniePreTrainedModel", ] if TYPE_CHECKING: from .configuration_ernie import ERNIE_PRETRAINED_CONFIG_ARCHIVE_MAP, ErnieConfig, ErnieOnnxConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_ernie import ( ERNIE_PRETRAINED_MODEL_ARCHIVE_LIST, ErnieForCausalLM, ErnieForMaskedLM, ErnieForMultipleChoice, ErnieForNextSentencePrediction, ErnieForPreTraining, ErnieForQuestionAnswering, ErnieForSequenceClassification, ErnieForTokenClassification, ErnieModel, ErniePreTrainedModel, ) else: import sys lowerCamelCase_ = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
244
from collections import OrderedDict from typing import TYPE_CHECKING, Any, List, Mapping, Optional, Union from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import TensorType, logging if TYPE_CHECKING: from ...onnx.config import PatchingSpec from ...tokenization_utils_base import PreTrainedTokenizerBase __A : Dict = logging.get_logger(__name__) __A : str = { "allenai/longformer-base-4096": "https://huggingface.co/allenai/longformer-base-4096/resolve/main/config.json", "allenai/longformer-large-4096": "https://huggingface.co/allenai/longformer-large-4096/resolve/main/config.json", "allenai/longformer-large-4096-finetuned-triviaqa": ( "https://huggingface.co/allenai/longformer-large-4096-finetuned-triviaqa/resolve/main/config.json" ), "allenai/longformer-base-4096-extra.pos.embd.only": ( "https://huggingface.co/allenai/longformer-base-4096-extra.pos.embd.only/resolve/main/config.json" ), "allenai/longformer-large-4096-extra.pos.embd.only": ( "https://huggingface.co/allenai/longformer-large-4096-extra.pos.embd.only/resolve/main/config.json" ), } class A_ (a_ ): UpperCAmelCase__ = '''longformer''' def __init__( self , _A = 5_1_2 , _A = 2 , _A = 1 , _A = 0 , _A = 2 , _A = 3_0_5_2_2 , _A = 7_6_8 , _A = 1_2 , _A = 1_2 , _A = 3_0_7_2 , _A = "gelu" , _A = 0.1 , _A = 0.1 , _A = 5_1_2 , _A = 2 , _A = 0.02 , _A = 1E-12 , _A = False , **_A , ): '''simple docstring''' super().__init__(pad_token_id=_A , **_A ) UpperCAmelCase = attention_window UpperCAmelCase = sep_token_id UpperCAmelCase = bos_token_id UpperCAmelCase = eos_token_id UpperCAmelCase = vocab_size UpperCAmelCase = hidden_size UpperCAmelCase = num_hidden_layers UpperCAmelCase = num_attention_heads UpperCAmelCase = hidden_act UpperCAmelCase = intermediate_size UpperCAmelCase = hidden_dropout_prob UpperCAmelCase = attention_probs_dropout_prob UpperCAmelCase = max_position_embeddings UpperCAmelCase = type_vocab_size UpperCAmelCase = initializer_range UpperCAmelCase = layer_norm_eps UpperCAmelCase = onnx_export class A_ (a_ ): def __init__( self , _A , _A = "default" , _A = None ): '''simple docstring''' super().__init__(_A , _A , _A ) UpperCAmelCase = True @property def _lowercase ( self ): '''simple docstring''' if self.task == "multiple-choice": UpperCAmelCase = {0: '''batch''', 1: '''choice''', 2: '''sequence'''} else: UpperCAmelCase = {0: '''batch''', 1: '''sequence'''} return OrderedDict( [ ('''input_ids''', dynamic_axis), ('''attention_mask''', dynamic_axis), ('''global_attention_mask''', dynamic_axis), ] ) @property def _lowercase ( self ): '''simple docstring''' UpperCAmelCase = super().outputs if self.task == "default": UpperCAmelCase = {0: '''batch'''} return outputs @property def _lowercase ( self ): '''simple docstring''' return 1E-4 @property def _lowercase ( self ): '''simple docstring''' return max(super().default_onnx_opset , 1_4 ) def _lowercase ( self , _A , _A = -1 , _A = -1 , _A = False , _A = None , ): '''simple docstring''' UpperCAmelCase = super().generate_dummy_inputs( preprocessor=_A , batch_size=_A , seq_length=_A , is_pair=_A , framework=_A ) import torch # for some reason, replacing this code by inputs["global_attention_mask"] = torch.randint(2, inputs["input_ids"].shape, dtype=torch.int64) # makes the export fail randomly UpperCAmelCase = torch.zeros_like(inputs['''input_ids'''] ) # make every second token global UpperCAmelCase = 1 return inputs
273
0
from argparse import ArgumentParser, Namespace from ..utils import logging from . import BaseTransformersCLICommand def __lowercase ( __lowerCAmelCase : Optional[Any] ): return ConvertCommand( args.model_type , args.tf_checkpoint , args.pytorch_dump_output , args.config , args.finetuning_task_name ) snake_case : List[Any] = "\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 snake_case_ (a_ ): @staticmethod def lowerCamelCase__( __snake_case :Union[str, Any] ) -> List[Any]: a__ = 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=_A ,required=_A ,help='Model\'s type.' ) train_parser.add_argument( '--tf_checkpoint' ,type=_A ,required=_A ,help='TensorFlow checkpoint path or folder.' ) train_parser.add_argument( '--pytorch_dump_output' ,type=_A ,required=_A ,help='Path to the PyTorch saved model output.' ) train_parser.add_argument('--config' ,type=_A ,default='' ,help='Configuration file path or folder.' ) train_parser.add_argument( '--finetuning_task_name' ,type=_A ,default=_A ,help='Optional fine-tuning task name if the TF model was a finetuned model.' ,) train_parser.set_defaults(func=_A ) def __init__( self :int ,__snake_case :List[Any] ,__snake_case :Tuple ,__snake_case :Any ,__snake_case :Optional[Any] ,__snake_case :List[Any] ,*__snake_case :Dict ,) -> Dict: a__ = logging.get_logger('transformers-cli/converting' ) self._logger.info(F'Loading model {model_type}' ) a__ = model_type a__ = tf_checkpoint a__ = pytorch_dump_output a__ = config a__ = finetuning_task_name def lowerCamelCase__( self :Dict ) -> Tuple: 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(_A ) 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(_A ) 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(_A ) 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(_A ) 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(_A ) if "ckpt" in self._tf_checkpoint.lower(): a__ = self._tf_checkpoint a__ = '' else: a__ = self._tf_checkpoint a__ = '' convert_transfo_xl_checkpoint_to_pytorch( _A ,self._config ,self._pytorch_dump_output ,_A ) 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(_A ) 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(_A ) 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]' )
240
from typing import List, Optional, Tuple, Union import torch from ...models import UNetaDModel from ...schedulers import KarrasVeScheduler from ...utils import randn_tensor from ..pipeline_utils import DiffusionPipeline, ImagePipelineOutput class A_ (a_ ): UpperCAmelCase__ = 42 UpperCAmelCase__ = 42 def __init__( self , _A , _A ): '''simple docstring''' super().__init__() self.register_modules(unet=_A , scheduler=_A ) @torch.no_grad() def __call__( self , _A = 1 , _A = 5_0 , _A = None , _A = "pil" , _A = True , **_A , ): '''simple docstring''' UpperCAmelCase = self.unet.config.sample_size UpperCAmelCase = (batch_size, 3, img_size, img_size) UpperCAmelCase = self.unet # sample x_0 ~ N(0, sigma_0^2 * I) UpperCAmelCase = randn_tensor(_A , generator=_A , device=self.device ) * self.scheduler.init_noise_sigma self.scheduler.set_timesteps(_A ) for t in self.progress_bar(self.scheduler.timesteps ): # here sigma_t == t_i from the paper UpperCAmelCase = self.scheduler.schedule[t] UpperCAmelCase = self.scheduler.schedule[t - 1] if t > 0 else 0 # 1. Select temporarily increased noise level sigma_hat # 2. Add new noise to move from sample_i to sample_hat UpperCAmelCase , UpperCAmelCase = self.scheduler.add_noise_to_input(_A , _A , generator=_A ) # 3. Predict the noise residual given the noise magnitude `sigma_hat` # The model inputs and output are adjusted by following eq. (213) in [1]. UpperCAmelCase = (sigma_hat / 2) * model((sample_hat + 1) / 2 , sigma_hat / 2 ).sample # 4. Evaluate dx/dt at sigma_hat # 5. Take Euler step from sigma to sigma_prev UpperCAmelCase = self.scheduler.step(_A , _A , _A , _A ) if sigma_prev != 0: # 6. Apply 2nd order correction # The model inputs and output are adjusted by following eq. (213) in [1]. UpperCAmelCase = (sigma_prev / 2) * model((step_output.prev_sample + 1) / 2 , sigma_prev / 2 ).sample UpperCAmelCase = self.scheduler.step_correct( _A , _A , _A , _A , step_output.prev_sample , step_output['''derivative'''] , ) UpperCAmelCase = step_output.prev_sample UpperCAmelCase = (sample / 2 + 0.5).clamp(0 , 1 ) UpperCAmelCase = sample.cpu().permute(0 , 2 , 3 , 1 ).numpy() if output_type == "pil": UpperCAmelCase = self.numpy_to_pil(_A ) if not return_dict: return (image,) return ImagePipelineOutput(images=_A )
273
0
'''simple docstring''' import json import sys import tempfile import unittest from pathlib import Path import transformers from transformers import ( CONFIG_MAPPING, IMAGE_PROCESSOR_MAPPING, AutoConfig, AutoImageProcessor, CLIPConfig, CLIPImageProcessor, ) from transformers.testing_utils import DUMMY_UNKNOWN_IDENTIFIER sys.path.append(str(Path(__file__).parent.parent.parent.parent / '''utils''')) from test_module.custom_configuration import CustomConfig # noqa E402 from test_module.custom_image_processing import CustomImageProcessor # noqa E402 class A ( unittest.TestCase ): '''simple docstring''' def a_ (self ) -> Any: __UpperCamelCase : str = 0 def a_ (self ) -> Tuple: __UpperCamelCase : str = AutoImageProcessor.from_pretrained("openai/clip-vit-base-patch32" ) self.assertIsInstance(_A , _A ) def a_ (self ) -> Any: with tempfile.TemporaryDirectory() as tmpdirname: __UpperCamelCase : List[str] = Path(_A ) / "preprocessor_config.json" __UpperCamelCase : Tuple = Path(_A ) / "config.json" json.dump( {"image_processor_type": "CLIPImageProcessor", "processor_class": "CLIPProcessor"} , open(_A , "w" ) , ) json.dump({"model_type": "clip"} , open(_A , "w" ) ) __UpperCamelCase : Optional[int] = AutoImageProcessor.from_pretrained(_A ) self.assertIsInstance(_A , _A ) def a_ (self ) -> str: with tempfile.TemporaryDirectory() as tmpdirname: __UpperCamelCase : Optional[Any] = Path(_A ) / "preprocessor_config.json" __UpperCamelCase : List[Any] = Path(_A ) / "config.json" json.dump( {"feature_extractor_type": "CLIPFeatureExtractor", "processor_class": "CLIPProcessor"} , open(_A , "w" ) , ) json.dump({"model_type": "clip"} , open(_A , "w" ) ) __UpperCamelCase : Union[str, Any] = AutoImageProcessor.from_pretrained(_A ) self.assertIsInstance(_A , _A ) def a_ (self ) -> Dict: with tempfile.TemporaryDirectory() as tmpdirname: __UpperCamelCase : Dict = CLIPConfig() # Create a dummy config file with image_proceesor_type __UpperCamelCase : int = Path(_A ) / "preprocessor_config.json" __UpperCamelCase : List[str] = Path(_A ) / "config.json" json.dump( {"image_processor_type": "CLIPImageProcessor", "processor_class": "CLIPProcessor"} , open(_A , "w" ) , ) json.dump({"model_type": "clip"} , open(_A , "w" ) ) # remove image_processor_type to make sure config.json alone is enough to load image processor locally __UpperCamelCase : List[Any] = AutoImageProcessor.from_pretrained(_A ).to_dict() config_dict.pop("image_processor_type" ) __UpperCamelCase : List[str] = CLIPImageProcessor(**_A ) # save in new folder model_config.save_pretrained(_A ) config.save_pretrained(_A ) __UpperCamelCase : str = AutoImageProcessor.from_pretrained(_A ) # make sure private variable is not incorrectly saved __UpperCamelCase : Optional[Any] = json.loads(config.to_json_string() ) self.assertTrue("_processor_class" not in dict_as_saved ) self.assertIsInstance(_A , _A ) def a_ (self ) -> Dict: with tempfile.TemporaryDirectory() as tmpdirname: __UpperCamelCase : List[Any] = Path(_A ) / "preprocessor_config.json" json.dump( {"image_processor_type": "CLIPImageProcessor", "processor_class": "CLIPProcessor"} , open(_A , "w" ) , ) __UpperCamelCase : int = AutoImageProcessor.from_pretrained(_A ) self.assertIsInstance(_A , _A ) def a_ (self ) -> List[Any]: with self.assertRaisesRegex( _A , "clip-base is not a local folder and is not a valid model identifier" ): __UpperCamelCase : Optional[int] = AutoImageProcessor.from_pretrained("clip-base" ) def a_ (self ) -> Union[str, Any]: with self.assertRaisesRegex( _A , R"aaaaaa is not a valid git identifier \(branch name, tag name or commit id\)" ): __UpperCamelCase : Tuple = AutoImageProcessor.from_pretrained(_A , revision="aaaaaa" ) def a_ (self ) -> Tuple: with self.assertRaisesRegex( _A , "hf-internal-testing/config-no-model does not appear to have a file named preprocessor_config.json." , ): __UpperCamelCase : List[Any] = AutoImageProcessor.from_pretrained("hf-internal-testing/config-no-model" ) def a_ (self ) -> Optional[int]: with self.assertRaises(_A ): __UpperCamelCase : Tuple = AutoImageProcessor.from_pretrained("hf-internal-testing/test_dynamic_image_processor" ) # If remote code is disabled, we can't load this config. with self.assertRaises(_A ): __UpperCamelCase : Tuple = AutoImageProcessor.from_pretrained( "hf-internal-testing/test_dynamic_image_processor" , trust_remote_code=_A ) __UpperCamelCase : Union[str, Any] = AutoImageProcessor.from_pretrained( "hf-internal-testing/test_dynamic_image_processor" , trust_remote_code=_A ) self.assertEqual(image_processor.__class__.__name__ , "NewImageProcessor" ) # Test image processor can be reloaded. with tempfile.TemporaryDirectory() as tmp_dir: image_processor.save_pretrained(_A ) __UpperCamelCase : List[str] = AutoImageProcessor.from_pretrained(_A , trust_remote_code=_A ) self.assertEqual(reloaded_image_processor.__class__.__name__ , "NewImageProcessor" ) def a_ (self ) -> Tuple: try: AutoConfig.register("custom" , _A ) AutoImageProcessor.register(_A , _A ) # Trying to register something existing in the Transformers library will raise an error with self.assertRaises(_A ): AutoImageProcessor.register(_A , _A ) with tempfile.TemporaryDirectory() as tmpdirname: __UpperCamelCase : Union[str, Any] = Path(_A ) / "preprocessor_config.json" __UpperCamelCase : Dict = Path(_A ) / "config.json" json.dump( {"feature_extractor_type": "CLIPFeatureExtractor", "processor_class": "CLIPProcessor"} , open(_A , "w" ) , ) json.dump({"model_type": "clip"} , open(_A , "w" ) ) __UpperCamelCase : int = CustomImageProcessor.from_pretrained(_A ) # Now that the config is registered, it can be used as any other config with the auto-API with tempfile.TemporaryDirectory() as tmp_dir: image_processor.save_pretrained(_A ) __UpperCamelCase : List[Any] = AutoImageProcessor.from_pretrained(_A ) self.assertIsInstance(_A , _A ) finally: if "custom" in CONFIG_MAPPING._extra_content: del CONFIG_MAPPING._extra_content["custom"] if CustomConfig in IMAGE_PROCESSOR_MAPPING._extra_content: del IMAGE_PROCESSOR_MAPPING._extra_content[CustomConfig] def a_ (self ) -> Optional[Any]: class A ( a_ ): '''simple docstring''' A = True try: AutoConfig.register("custom" , _A ) AutoImageProcessor.register(_A , _A ) # If remote code is not set, the default is to use local __UpperCamelCase : Tuple = AutoImageProcessor.from_pretrained("hf-internal-testing/test_dynamic_image_processor" ) self.assertEqual(image_processor.__class__.__name__ , "NewImageProcessor" ) self.assertTrue(image_processor.is_local ) # If remote code is disabled, we load the local one. __UpperCamelCase : Optional[Any] = AutoImageProcessor.from_pretrained( "hf-internal-testing/test_dynamic_image_processor" , trust_remote_code=_A ) self.assertEqual(image_processor.__class__.__name__ , "NewImageProcessor" ) self.assertTrue(image_processor.is_local ) # If remote is enabled, we load from the Hub __UpperCamelCase : Union[str, Any] = AutoImageProcessor.from_pretrained( "hf-internal-testing/test_dynamic_image_processor" , trust_remote_code=_A ) self.assertEqual(image_processor.__class__.__name__ , "NewImageProcessor" ) self.assertTrue(not hasattr(_A , "is_local" ) ) finally: if "custom" in CONFIG_MAPPING._extra_content: del CONFIG_MAPPING._extra_content["custom"] if CustomConfig in IMAGE_PROCESSOR_MAPPING._extra_content: del IMAGE_PROCESSOR_MAPPING._extra_content[CustomConfig]
298
import itertools import random import unittest import numpy as np from transformers import BatchFeature, SpeechTaFeatureExtractor from transformers.testing_utils import require_torch from transformers.utils.import_utils import is_torch_available from ...test_sequence_feature_extraction_common import SequenceFeatureExtractionTestMixin if is_torch_available(): import torch __A : str = random.Random() def __SCREAMING_SNAKE_CASE ( UpperCamelCase__ , UpperCamelCase__=1.0 , UpperCamelCase__=None , UpperCamelCase__=None ) -> Tuple: '''simple docstring''' if rng is None: UpperCAmelCase = global_rng UpperCAmelCase = [] for batch_idx in range(shape[0] ): values.append([] ) for _ in range(shape[1] ): values[-1].append(rng.random() * scale ) return values @require_torch class A_ (unittest.TestCase ): def __init__( self , _A , _A=7 , _A=4_0_0 , _A=2_0_0_0 , _A=1 , _A=0.0 , _A=1_6_0_0_0 , _A=True , _A=8_0 , _A=1_6 , _A=6_4 , _A="hann_window" , _A=8_0 , _A=7_6_0_0 , _A=1E-10 , _A=True , ): '''simple docstring''' UpperCAmelCase = parent UpperCAmelCase = batch_size UpperCAmelCase = min_seq_length UpperCAmelCase = max_seq_length UpperCAmelCase = (self.max_seq_length - self.min_seq_length) // (self.batch_size - 1) UpperCAmelCase = feature_size UpperCAmelCase = padding_value UpperCAmelCase = sampling_rate UpperCAmelCase = do_normalize UpperCAmelCase = num_mel_bins UpperCAmelCase = hop_length UpperCAmelCase = win_length UpperCAmelCase = win_function UpperCAmelCase = fmin UpperCAmelCase = fmax UpperCAmelCase = mel_floor UpperCAmelCase = return_attention_mask def _lowercase ( self ): '''simple docstring''' return { "feature_size": self.feature_size, "padding_value": self.padding_value, "sampling_rate": self.sampling_rate, "do_normalize": self.do_normalize, "num_mel_bins": self.num_mel_bins, "hop_length": self.hop_length, "win_length": self.win_length, "win_function": self.win_function, "fmin": self.fmin, "fmax": self.fmax, "mel_floor": self.mel_floor, "return_attention_mask": self.return_attention_mask, } def _lowercase ( self , _A=False , _A=False ): '''simple docstring''' def _flatten(_A ): return list(itertools.chain(*_A ) ) if equal_length: UpperCAmelCase = floats_list((self.batch_size, self.max_seq_length) ) else: # make sure that inputs increase in size UpperCAmelCase = [ _flatten(floats_list((x, self.feature_size) ) ) for x in range(self.min_seq_length , self.max_seq_length , self.seq_length_diff ) ] if numpify: UpperCAmelCase = [np.asarray(_A ) for x in speech_inputs] return speech_inputs def _lowercase ( self , _A=False , _A=False ): '''simple docstring''' if equal_length: UpperCAmelCase = [floats_list((self.max_seq_length, self.num_mel_bins) ) for _ in range(self.batch_size )] else: # make sure that inputs increase in size UpperCAmelCase = [ floats_list((x, self.num_mel_bins) ) for x in range(self.min_seq_length , self.max_seq_length , self.seq_length_diff ) ] if numpify: UpperCAmelCase = [np.asarray(_A ) for x in speech_inputs] return speech_inputs @require_torch class A_ (a_ , unittest.TestCase ): UpperCAmelCase__ = SpeechTaFeatureExtractor def _lowercase ( self ): '''simple docstring''' UpperCAmelCase = SpeechTaFeatureExtractionTester(self ) def _lowercase ( self , _A ): '''simple docstring''' self.assertTrue(np.all(np.mean(_A , axis=0 ) < 1E-3 ) ) self.assertTrue(np.all(np.abs(np.var(_A , axis=0 ) - 1 ) < 1E-3 ) ) def _lowercase ( self ): '''simple docstring''' UpperCAmelCase = self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() ) # create three inputs of length 800, 1000, and 1200 UpperCAmelCase = [floats_list((1, x) )[0] for x in range(8_0_0 , 1_4_0_0 , 2_0_0 )] UpperCAmelCase = [np.asarray(_A ) for speech_input in speech_inputs] # Test not batched input UpperCAmelCase = feat_extract(speech_inputs[0] , return_tensors='''np''' ).input_values UpperCAmelCase = feat_extract(np_speech_inputs[0] , return_tensors='''np''' ).input_values self.assertTrue(np.allclose(_A , _A , atol=1E-3 ) ) # Test batched UpperCAmelCase = feat_extract(_A , return_tensors='''np''' ).input_values UpperCAmelCase = feat_extract(_A , return_tensors='''np''' ).input_values for enc_seq_a, enc_seq_a in zip(_A , _A ): self.assertTrue(np.allclose(_A , _A , atol=1E-3 ) ) def _lowercase ( self ): '''simple docstring''' UpperCAmelCase = self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() ) UpperCAmelCase = [floats_list((1, x) )[0] for x in range(8_0_0 , 1_4_0_0 , 2_0_0 )] UpperCAmelCase = ['''longest''', '''max_length''', '''do_not_pad'''] UpperCAmelCase = [None, 1_6_0_0, None] for max_length, padding in zip(_A , _A ): UpperCAmelCase = feat_extract(_A , padding=_A , max_length=_A , return_tensors='''np''' ) UpperCAmelCase = processed.input_values self._check_zero_mean_unit_variance(input_values[0][:8_0_0] ) self.assertTrue(input_values[0][8_0_0:].sum() < 1E-6 ) self._check_zero_mean_unit_variance(input_values[1][:1_0_0_0] ) self.assertTrue(input_values[0][1_0_0_0:].sum() < 1E-6 ) self._check_zero_mean_unit_variance(input_values[2][:1_2_0_0] ) def _lowercase ( self ): '''simple docstring''' UpperCAmelCase = self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() ) UpperCAmelCase = range(8_0_0 , 1_4_0_0 , 2_0_0 ) UpperCAmelCase = [floats_list((1, x) )[0] for x in lengths] UpperCAmelCase = ['''longest''', '''max_length''', '''do_not_pad'''] UpperCAmelCase = [None, 1_6_0_0, None] for max_length, padding in zip(_A , _A ): UpperCAmelCase = feat_extract(_A , max_length=_A , padding=_A ) UpperCAmelCase = processed.input_values self._check_zero_mean_unit_variance(input_values[0][:8_0_0] ) self._check_zero_mean_unit_variance(input_values[1][:1_0_0_0] ) self._check_zero_mean_unit_variance(input_values[2][:1_2_0_0] ) def _lowercase ( self ): '''simple docstring''' UpperCAmelCase = self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() ) UpperCAmelCase = [floats_list((1, x) )[0] for x in range(8_0_0 , 1_4_0_0 , 2_0_0 )] UpperCAmelCase = feat_extract( _A , truncation=_A , max_length=1_0_0_0 , padding='''max_length''' , return_tensors='''np''' ) UpperCAmelCase = processed.input_values self._check_zero_mean_unit_variance(input_values[0, :8_0_0] ) self._check_zero_mean_unit_variance(input_values[1] ) self._check_zero_mean_unit_variance(input_values[2] ) def _lowercase ( self ): '''simple docstring''' UpperCAmelCase = self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() ) UpperCAmelCase = [floats_list((1, x) )[0] for x in range(8_0_0 , 1_4_0_0 , 2_0_0 )] UpperCAmelCase = feat_extract( _A , truncation=_A , max_length=1_0_0_0 , padding='''longest''' , return_tensors='''np''' ) UpperCAmelCase = processed.input_values self._check_zero_mean_unit_variance(input_values[0, :8_0_0] ) self._check_zero_mean_unit_variance(input_values[1, :1_0_0_0] ) self._check_zero_mean_unit_variance(input_values[2] ) # make sure that if max_length < longest -> then pad to max_length self.assertTrue(input_values.shape == (3, 1_0_0_0) ) UpperCAmelCase = [floats_list((1, x) )[0] for x in range(8_0_0 , 1_4_0_0 , 2_0_0 )] UpperCAmelCase = feat_extract( _A , truncation=_A , max_length=2_0_0_0 , padding='''longest''' , return_tensors='''np''' ) UpperCAmelCase = processed.input_values self._check_zero_mean_unit_variance(input_values[0, :8_0_0] ) self._check_zero_mean_unit_variance(input_values[1, :1_0_0_0] ) self._check_zero_mean_unit_variance(input_values[2] ) # make sure that if max_length > longest -> then pad to longest self.assertTrue(input_values.shape == (3, 1_2_0_0) ) def _lowercase ( self ): '''simple docstring''' UpperCAmelCase = self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() ) UpperCAmelCase = np.random.rand(1_0_0 ).astype(np.floataa ) UpperCAmelCase = np_speech_inputs.tolist() for inputs in [py_speech_inputs, np_speech_inputs]: UpperCAmelCase = feature_extractor.pad([{'''input_values''': inputs}] , return_tensors='''np''' ) self.assertTrue(np_processed.input_values.dtype == np.floataa ) UpperCAmelCase = feature_extractor.pad([{'''input_values''': inputs}] , return_tensors='''pt''' ) self.assertTrue(pt_processed.input_values.dtype == torch.floataa ) def _lowercase ( self ): '''simple docstring''' UpperCAmelCase = self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() ) # create three inputs of length 800, 1000, and 1200 UpperCAmelCase = [floats_list((1, x) )[0] for x in range(8_0_0 , 1_4_0_0 , 2_0_0 )] UpperCAmelCase = [np.asarray(_A ) for speech_input in speech_inputs] # Test feature size UpperCAmelCase = feature_extractor(audio_target=_A , padding=_A , return_tensors='''np''' ).input_values self.assertTrue(input_values.ndim == 3 ) self.assertTrue(input_values.shape[-1] == feature_extractor.num_mel_bins ) # Test not batched input UpperCAmelCase = feature_extractor(speech_inputs[0] , return_tensors='''np''' ).input_values UpperCAmelCase = feature_extractor(np_speech_inputs[0] , return_tensors='''np''' ).input_values self.assertTrue(np.allclose(_A , _A , atol=1E-3 ) ) # Test batched UpperCAmelCase = feature_extractor(_A , return_tensors='''np''' ).input_values UpperCAmelCase = feature_extractor(_A , return_tensors='''np''' ).input_values for enc_seq_a, enc_seq_a in zip(_A , _A ): self.assertTrue(np.allclose(_A , _A , atol=1E-3 ) ) # Test 2-D numpy arrays are batched. UpperCAmelCase = [floats_list((1, x) )[0] for x in (8_0_0, 8_0_0, 8_0_0)] UpperCAmelCase = np.asarray(_A ) UpperCAmelCase = feature_extractor(_A , return_tensors='''np''' ).input_values UpperCAmelCase = feature_extractor(_A , return_tensors='''np''' ).input_values for enc_seq_a, enc_seq_a in zip(_A , _A ): self.assertTrue(np.allclose(_A , _A , atol=1E-3 ) ) def _lowercase ( self ): '''simple docstring''' UpperCAmelCase = self.feat_extract_tester.prepare_inputs_for_target() UpperCAmelCase = self.feature_extraction_class(**self.feat_extract_dict ) UpperCAmelCase = feat_extract.model_input_names[0] UpperCAmelCase = BatchFeature({input_name: speech_inputs} ) self.assertTrue(all(len(_A ) == len(_A ) for x, y in zip(_A , processed_features[input_name] ) ) ) UpperCAmelCase = self.feat_extract_tester.prepare_inputs_for_target(equal_length=_A ) UpperCAmelCase = BatchFeature({input_name: speech_inputs} , tensor_type='''np''' ) UpperCAmelCase = processed_features[input_name] if len(batch_features_input.shape ) < 3: UpperCAmelCase = batch_features_input[:, :, None] self.assertTrue( batch_features_input.shape == (self.feat_extract_tester.batch_size, len(speech_inputs[0] ), self.feat_extract_tester.num_mel_bins) ) @require_torch def _lowercase ( self ): '''simple docstring''' UpperCAmelCase = self.feat_extract_tester.prepare_inputs_for_target(equal_length=_A ) UpperCAmelCase = self.feature_extraction_class(**self.feat_extract_dict ) UpperCAmelCase = feat_extract.model_input_names[0] UpperCAmelCase = BatchFeature({input_name: speech_inputs} , tensor_type='''pt''' ) UpperCAmelCase = processed_features[input_name] if len(batch_features_input.shape ) < 3: UpperCAmelCase = batch_features_input[:, :, None] self.assertTrue( batch_features_input.shape == (self.feat_extract_tester.batch_size, len(speech_inputs[0] ), self.feat_extract_tester.num_mel_bins) ) @require_torch def _lowercase ( self ): '''simple docstring''' UpperCAmelCase = self.feature_extraction_class(**self.feat_extract_dict ) UpperCAmelCase = self.feat_extract_tester.prepare_inputs_for_target() UpperCAmelCase = feat_extract.model_input_names[0] UpperCAmelCase = BatchFeature({input_name: speech_inputs} ) UpperCAmelCase = feat_extract.num_mel_bins # hack! UpperCAmelCase = feat_extract.pad(_A , padding='''longest''' , return_tensors='''np''' )[input_name] UpperCAmelCase = feat_extract.pad(_A , padding='''longest''' , return_tensors='''pt''' )[input_name] self.assertTrue(abs(input_np.astype(np.floataa ).sum() - input_pt.numpy().astype(np.floataa ).sum() ) < 1E-2 ) def _lowercase ( self ): '''simple docstring''' UpperCAmelCase = self.feat_extract_dict UpperCAmelCase = True UpperCAmelCase = self.feature_extraction_class(**_A ) UpperCAmelCase = self.feat_extract_tester.prepare_inputs_for_target() UpperCAmelCase = [len(_A ) for x in speech_inputs] UpperCAmelCase = feat_extract.model_input_names[0] UpperCAmelCase = BatchFeature({input_name: speech_inputs} ) UpperCAmelCase = feat_extract.num_mel_bins # hack! UpperCAmelCase = feat_extract.pad(_A , padding='''longest''' , return_tensors='''np''' ) self.assertIn('''attention_mask''' , _A ) self.assertListEqual(list(processed.attention_mask.shape ) , list(processed[input_name].shape[:2] ) ) self.assertListEqual(processed.attention_mask.sum(-1 ).tolist() , _A ) def _lowercase ( self ): '''simple docstring''' UpperCAmelCase = self.feat_extract_dict UpperCAmelCase = True UpperCAmelCase = self.feature_extraction_class(**_A ) UpperCAmelCase = self.feat_extract_tester.prepare_inputs_for_target() UpperCAmelCase = [len(_A ) for x in speech_inputs] UpperCAmelCase = feat_extract.model_input_names[0] UpperCAmelCase = BatchFeature({input_name: speech_inputs} ) UpperCAmelCase = min(_A ) UpperCAmelCase = feat_extract.num_mel_bins # hack! UpperCAmelCase = feat_extract.pad( _A , padding='''max_length''' , max_length=_A , truncation=_A , return_tensors='''np''' ) self.assertIn('''attention_mask''' , _A ) self.assertListEqual( list(processed_pad.attention_mask.shape ) , [processed_pad[input_name].shape[0], max_length] ) self.assertListEqual( processed_pad.attention_mask[:, :max_length].sum(-1 ).tolist() , [max_length for x in speech_inputs] ) def _lowercase ( self , _A ): '''simple docstring''' from datasets import load_dataset UpperCAmelCase = load_dataset('''hf-internal-testing/librispeech_asr_dummy''' , '''clean''' , split='''validation''' ) # automatic decoding with librispeech UpperCAmelCase = ds.sort('''id''' ).select(range(_A ) )[:num_samples]['''audio'''] return [x["array"] for x in speech_samples] def _lowercase ( self ): '''simple docstring''' UpperCAmelCase = torch.tensor( [2.3804E-03, 2.0752E-03, 1.9836E-03, 2.1057E-03, 1.6174E-03, 3.0518E-04, 9.1553E-05, 3.3569E-04, 9.7656E-04, 1.8311E-03, 2.0142E-03, 2.1057E-03, 1.7395E-03, 4.5776E-04, -3.9673E-04, 4.5776E-04, 1.0071E-03, 9.1553E-05, 4.8828E-04, 1.1597E-03, 7.3242E-04, 9.4604E-04, 1.8005E-03, 1.8311E-03, 8.8501E-04, 4.2725E-04, 4.8828E-04, 7.3242E-04, 1.0986E-03, 2.1057E-03] ) # fmt: on UpperCAmelCase = self._load_datasamples(1 ) UpperCAmelCase = SpeechTaFeatureExtractor() UpperCAmelCase = feature_extractor(_A , return_tensors='''pt''' ).input_values self.assertEquals(input_values.shape , (1, 9_3_6_8_0) ) self.assertTrue(torch.allclose(input_values[0, :3_0] , _A , atol=1E-6 ) ) def _lowercase ( self ): '''simple docstring''' UpperCAmelCase = torch.tensor( [-2.68_70, -3.01_04, -3.13_56, -3.53_52, -3.00_44, -3.03_53, -3.47_19, -3.67_77, -3.15_20, -2.94_35, -2.65_53, -2.87_95, -2.99_44, -2.59_21, -3.02_79, -3.03_86, -3.08_64, -3.12_91, -3.23_53, -2.74_44, -2.68_31, -2.72_87, -3.17_61, -3.15_71, -3.27_26, -3.05_82, -3.10_07, -3.45_33, -3.46_95, -3.09_98] ) # fmt: on UpperCAmelCase = self._load_datasamples(1 ) UpperCAmelCase = SpeechTaFeatureExtractor() UpperCAmelCase = feature_extractor(audio_target=_A , return_tensors='''pt''' ).input_values self.assertEquals(input_values.shape , (1, 3_6_6, 8_0) ) self.assertTrue(torch.allclose(input_values[0, 0, :3_0] , _A , atol=1E-4 ) )
273
0
import json import os import shutil import tempfile import unittest import numpy as np import pytest from transformers import MgpstrTokenizer from transformers.models.mgp_str.tokenization_mgp_str import VOCAB_FILES_NAMES from transformers.testing_utils import require_torch, require_vision from transformers.utils import IMAGE_PROCESSOR_NAME, is_torch_available, is_vision_available if is_torch_available(): import torch if is_vision_available(): from PIL import Image from transformers import MgpstrProcessor, ViTImageProcessor @require_torch @require_vision class __lowerCamelCase (unittest.TestCase ): _lowercase = ViTImageProcessor if is_vision_available() else None @property def snake_case_ ( self: str ): '''simple docstring''' return self.image_processor_tester.prepare_image_processor_dict() def snake_case_ ( self: Dict ): '''simple docstring''' __UpperCamelCase = (3, 32, 128) __UpperCamelCase = tempfile.mkdtemp() # fmt: off __UpperCamelCase = ['[GO]', '[s]', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'] # fmt: on __UpperCamelCase = dict(zip(_A,range(len(_A ) ) ) ) __UpperCamelCase = os.path.join(self.tmpdirname,VOCAB_FILES_NAMES['vocab_file'] ) with open(self.vocab_file,'w',encoding='utf-8' ) as fp: fp.write(json.dumps(_A ) + '\n' ) __UpperCamelCase = { 'do_normalize': False, 'do_resize': True, 'image_processor_type': 'ViTImageProcessor', 'resample': 3, 'size': {'height': 32, 'width': 128}, } __UpperCamelCase = os.path.join(self.tmpdirname,_A ) with open(self.image_processor_file,'w',encoding='utf-8' ) as fp: json.dump(_A,_A ) def snake_case_ ( self: str,**A_: Any ): '''simple docstring''' return MgpstrTokenizer.from_pretrained(self.tmpdirname,**_A ) def snake_case_ ( self: Any,**A_: Dict ): '''simple docstring''' return ViTImageProcessor.from_pretrained(self.tmpdirname,**_A ) def snake_case_ ( self: str ): '''simple docstring''' shutil.rmtree(self.tmpdirname ) def snake_case_ ( self: Optional[int] ): '''simple docstring''' __UpperCamelCase = np.random.randint(255,size=(3, 30, 400),dtype=np.uinta ) __UpperCamelCase = Image.fromarray(np.moveaxis(_A,0,-1 ) ) return image_input def snake_case_ ( self: List[str] ): '''simple docstring''' __UpperCamelCase = self.get_tokenizer() __UpperCamelCase = self.get_image_processor() __UpperCamelCase = MgpstrProcessor(tokenizer=_A,image_processor=_A ) processor.save_pretrained(self.tmpdirname ) __UpperCamelCase = MgpstrProcessor.from_pretrained(self.tmpdirname,use_fast=_A ) self.assertEqual(processor.char_tokenizer.get_vocab(),tokenizer.get_vocab() ) self.assertIsInstance(processor.char_tokenizer,_A ) self.assertEqual(processor.image_processor.to_json_string(),image_processor.to_json_string() ) self.assertIsInstance(processor.image_processor,_A ) def snake_case_ ( self: Optional[int] ): '''simple docstring''' __UpperCamelCase = self.get_tokenizer() __UpperCamelCase = self.get_image_processor() __UpperCamelCase = MgpstrProcessor(tokenizer=_A,image_processor=_A ) processor.save_pretrained(self.tmpdirname ) __UpperCamelCase = self.get_tokenizer(bos_token='(BOS)',eos_token='(EOS)' ) __UpperCamelCase = self.get_image_processor(do_normalize=_A,padding_value=1.0 ) __UpperCamelCase = MgpstrProcessor.from_pretrained( self.tmpdirname,bos_token='(BOS)',eos_token='(EOS)',do_normalize=_A,padding_value=1.0 ) self.assertEqual(processor.char_tokenizer.get_vocab(),tokenizer_add_kwargs.get_vocab() ) self.assertIsInstance(processor.char_tokenizer,_A ) self.assertEqual(processor.image_processor.to_json_string(),image_processor_add_kwargs.to_json_string() ) self.assertIsInstance(processor.image_processor,_A ) def snake_case_ ( self: Union[str, Any] ): '''simple docstring''' __UpperCamelCase = self.get_image_processor() __UpperCamelCase = self.get_tokenizer() __UpperCamelCase = MgpstrProcessor(tokenizer=_A,image_processor=_A ) __UpperCamelCase = self.prepare_image_inputs() __UpperCamelCase = image_processor(_A,return_tensors='np' ) __UpperCamelCase = processor(images=_A,return_tensors='np' ) for key in input_image_proc.keys(): self.assertAlmostEqual(input_image_proc[key].sum(),input_processor[key].sum(),delta=1E-2 ) def snake_case_ ( self: List[str] ): '''simple docstring''' __UpperCamelCase = self.get_image_processor() __UpperCamelCase = self.get_tokenizer() __UpperCamelCase = MgpstrProcessor(tokenizer=_A,image_processor=_A ) __UpperCamelCase = 'test' __UpperCamelCase = processor(text=_A ) __UpperCamelCase = tokenizer(_A ) for key in encoded_tok.keys(): self.assertListEqual(encoded_tok[key],encoded_processor[key] ) def snake_case_ ( self: str ): '''simple docstring''' __UpperCamelCase = self.get_image_processor() __UpperCamelCase = self.get_tokenizer() __UpperCamelCase = MgpstrProcessor(tokenizer=_A,image_processor=_A ) __UpperCamelCase = 'test' __UpperCamelCase = self.prepare_image_inputs() __UpperCamelCase = processor(text=_A,images=_A ) self.assertListEqual(list(inputs.keys() ),['pixel_values', 'labels'] ) # test if it raises when no input is passed with pytest.raises(_A ): processor() def snake_case_ ( self: str ): '''simple docstring''' __UpperCamelCase = self.get_image_processor() __UpperCamelCase = self.get_tokenizer() __UpperCamelCase = MgpstrProcessor(tokenizer=_A,image_processor=_A ) __UpperCamelCase = [[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9], [3, 4, 3, 1, 1, 8, 9]] __UpperCamelCase = processor.char_decode(_A ) __UpperCamelCase = tokenizer.batch_decode(_A ) __UpperCamelCase = [seq.replace(' ','' ) for seq in decoded_tok] self.assertListEqual(_A,_A ) def snake_case_ ( self: List[str] ): '''simple docstring''' __UpperCamelCase = self.get_image_processor() __UpperCamelCase = self.get_tokenizer() __UpperCamelCase = MgpstrProcessor(tokenizer=_A,image_processor=_A ) __UpperCamelCase = None __UpperCamelCase = self.prepare_image_inputs() __UpperCamelCase = processor(text=_A,images=_A ) self.assertListEqual(list(inputs.keys() ),processor.model_input_names ) def snake_case_ ( self: Dict ): '''simple docstring''' __UpperCamelCase = self.get_image_processor() __UpperCamelCase = self.get_tokenizer() __UpperCamelCase = MgpstrProcessor(tokenizer=_A,image_processor=_A ) __UpperCamelCase = torch.randn(1,27,38 ) __UpperCamelCase = torch.randn(1,27,5_0257 ) __UpperCamelCase = torch.randn(1,27,3_0522 ) __UpperCamelCase = processor.batch_decode([char_input, bpe_input, wp_input] ) self.assertListEqual(list(results.keys() ),['generated_text', 'scores', 'char_preds', 'bpe_preds', 'wp_preds'] )
310
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_sentencepiece_available, is_speech_available, is_tf_available, is_torch_available, ) __A : Union[str, Any] = { "configuration_speech_to_text": ["SPEECH_TO_TEXT_PRETRAINED_CONFIG_ARCHIVE_MAP", "Speech2TextConfig"], "processing_speech_to_text": ["Speech2TextProcessor"], } try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __A : List[Any] = ["Speech2TextTokenizer"] try: if not is_speech_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __A : List[Any] = ["Speech2TextFeatureExtractor"] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __A : int = [ "TF_SPEECH_TO_TEXT_PRETRAINED_MODEL_ARCHIVE_LIST", "TFSpeech2TextForConditionalGeneration", "TFSpeech2TextModel", "TFSpeech2TextPreTrainedModel", ] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __A : Tuple = [ "SPEECH_TO_TEXT_PRETRAINED_MODEL_ARCHIVE_LIST", "Speech2TextForConditionalGeneration", "Speech2TextModel", "Speech2TextPreTrainedModel", ] if TYPE_CHECKING: from .configuration_speech_to_text import SPEECH_TO_TEXT_PRETRAINED_CONFIG_ARCHIVE_MAP, SpeechaTextConfig from .processing_speech_to_text import SpeechaTextProcessor try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_speech_to_text import SpeechaTextTokenizer try: if not is_speech_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .feature_extraction_speech_to_text import SpeechaTextFeatureExtractor try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_speech_to_text import ( TF_SPEECH_TO_TEXT_PRETRAINED_MODEL_ARCHIVE_LIST, TFSpeechaTextForConditionalGeneration, TFSpeechaTextModel, TFSpeechaTextPreTrainedModel, ) try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_speech_to_text import ( SPEECH_TO_TEXT_PRETRAINED_MODEL_ARCHIVE_LIST, SpeechaTextForConditionalGeneration, SpeechaTextModel, SpeechaTextPreTrainedModel, ) else: import sys __A : Dict = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
273
0
import argparse import torch from transformers import YosoConfig, YosoForMaskedLM def lowerCamelCase_ ( _a ): """simple docstring""" if "model" in orig_key: lowerCAmelCase__ : Any = orig_key.replace('''model.''' , '''''' ) if "norm1" in orig_key: lowerCAmelCase__ : Optional[int] = orig_key.replace('''norm1''' , '''attention.output.LayerNorm''' ) if "norm2" in orig_key: lowerCAmelCase__ : Any = orig_key.replace('''norm2''' , '''output.LayerNorm''' ) if "norm" in orig_key: lowerCAmelCase__ : Optional[int] = orig_key.replace('''norm''' , '''LayerNorm''' ) if "transformer" in orig_key: lowerCAmelCase__ : Dict = orig_key.split('''.''' )[0].split('''_''' )[-1] lowerCAmelCase__ : Any = orig_key.replace(f'transformer_{layer_num}' , f'encoder.layer.{layer_num}' ) if "mha.attn" in orig_key: lowerCAmelCase__ : Tuple = orig_key.replace('''mha.attn''' , '''attention.self''' ) if "mha" in orig_key: lowerCAmelCase__ : List[str] = orig_key.replace('''mha''' , '''attention''' ) if "W_q" in orig_key: lowerCAmelCase__ : Optional[int] = orig_key.replace('''W_q''' , '''self.query''' ) if "W_k" in orig_key: lowerCAmelCase__ : Any = orig_key.replace('''W_k''' , '''self.key''' ) if "W_v" in orig_key: lowerCAmelCase__ : Optional[Any] = orig_key.replace('''W_v''' , '''self.value''' ) if "ff1" in orig_key: lowerCAmelCase__ : Dict = orig_key.replace('''ff1''' , '''intermediate.dense''' ) if "ff2" in orig_key: lowerCAmelCase__ : str = orig_key.replace('''ff2''' , '''output.dense''' ) if "ff" in orig_key: lowerCAmelCase__ : str = orig_key.replace('''ff''' , '''output.dense''' ) if "mlm_class" in orig_key: lowerCAmelCase__ : str = orig_key.replace('''mlm.mlm_class''' , '''cls.predictions.decoder''' ) if "mlm" in orig_key: lowerCAmelCase__ : Tuple = orig_key.replace('''mlm''' , '''cls.predictions.transform''' ) if "cls" not in orig_key: lowerCAmelCase__ : Tuple = '''yoso.''' + orig_key return orig_key def lowerCamelCase_ ( _a , _a ): """simple docstring""" for key in orig_state_dict.copy().keys(): lowerCAmelCase__ : List[str] = orig_state_dict.pop(UpperCamelCase__ ) if ("pooler" in key) or ("sen_class" in key): continue else: lowerCAmelCase__ : int = val lowerCAmelCase__ : Tuple = orig_state_dict['''cls.predictions.decoder.bias'''] lowerCAmelCase__ : Dict = torch.arange(UpperCamelCase__ ).expand((1, -1) ) + 2 return orig_state_dict def lowerCamelCase_ ( _a , _a , _a ): """simple docstring""" lowerCAmelCase__ : Dict = torch.load(UpperCamelCase__ , map_location='''cpu''' )['''model_state_dict'''] lowerCAmelCase__ : Tuple = YosoConfig.from_json_file(UpperCamelCase__ ) lowerCAmelCase__ : Any = YosoForMaskedLM(UpperCamelCase__ ) lowerCAmelCase__ : Union[str, Any] = convert_checkpoint_helper(config.max_position_embeddings , UpperCamelCase__ ) print(model.load_state_dict(UpperCamelCase__ ) ) model.eval() model.save_pretrained(UpperCamelCase__ ) print(f'Checkpoint successfuly converted. Model saved at {pytorch_dump_path}' ) if __name__ == "__main__": lowerCamelCase = argparse.ArgumentParser() # Required parameters parser.add_argument( '''--pytorch_model_path''', default=None, type=str, required=True, help='''Path to YOSO pytorch checkpoint.''' ) parser.add_argument( '''--config_file''', default=None, type=str, required=True, help='''The json file for YOSO model config.''', ) parser.add_argument( '''--pytorch_dump_path''', default=None, type=str, required=True, help='''Path to the output PyTorch model.''' ) lowerCamelCase = parser.parse_args() convert_yoso_checkpoint(args.pytorch_model_path, args.config_file, args.pytorch_dump_path)
131
def __SCREAMING_SNAKE_CASE ( UpperCamelCase__ ) -> list[int]: '''simple docstring''' if length <= 0 or not isinstance(UpperCamelCase__ , UpperCamelCase__ ): raise ValueError('''Length must be a positive integer.''' ) return [n * (2 * n - 1) for n in range(UpperCamelCase__ )] if __name__ == "__main__": print(hexagonal_numbers(length=5)) print(hexagonal_numbers(length=10))
273
0
from collections.abc import Generator from math import sin def lowerCAmelCase__( lowercase : str ) -> bytes: if len(UpperCamelCase__ ) != 32: raise ValueError("Input must be of length 32" ) __snake_case : Any = B"" for i in [3, 2, 1, 0]: little_endian += string_aa[8 * i : 8 * i + 8] return little_endian def lowerCAmelCase__( lowercase : str ) -> bytes: if i < 0: raise ValueError("Input must be non-negative" ) __snake_case : Any = format(UpperCamelCase__ , "08x" )[-8:] __snake_case : List[str] = 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 lowerCAmelCase__( lowercase : str ) -> bytes: __snake_case : List[Any] = B"" for char in message: bit_string += format(UpperCamelCase__ , "08b" ).encode("utf-8" ) __snake_case : List[str] = format(len(UpperCamelCase__ ) , "064b" ).encode("utf-8" ) # Pad bit_string to a multiple of 512 chars bit_string += b"1" while len(UpperCamelCase__ ) % 512 != 448: bit_string += b"0" bit_string += to_little_endian(start_len[32:] ) + to_little_endian(start_len[:32] ) return bit_string def lowerCAmelCase__( lowercase : Union[str, Any] ) -> Generator[list[int], None, None]: if len(UpperCamelCase__ ) % 512 != 0: raise ValueError("Input must have length that\'s a multiple of 512" ) for pos in range(0 , len(UpperCamelCase__ ) , 512 ): __snake_case : int = bit_string[pos : pos + 512] __snake_case : Tuple = [] for i in range(0 , 512 , 32 ): block_words.append(int(to_little_endian(block[i : i + 32] ) , 2 ) ) yield block_words def lowerCAmelCase__( lowercase : Optional[Any] ) -> int: if i < 0: raise ValueError("Input must be non-negative" ) __snake_case : Any = format(UpperCamelCase__ , "032b" ) __snake_case : List[str] = "" for c in i_str: new_str += "1" if c == "0" else "0" return int(UpperCamelCase__ , 2 ) def lowerCAmelCase__( lowercase : Any , lowercase : Dict ) -> int: return (a + b) % 2**32 def lowerCAmelCase__( lowercase : Union[str, Any] , lowercase : List[str] ) -> int: 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 lowerCAmelCase__( lowercase : Optional[int] ) -> bytes: __snake_case : Union[str, Any] = preprocess(UpperCamelCase__ ) __snake_case : Optional[int] = [int(2**32 * abs(sin(i + 1 ) ) ) for i in range(64 )] # Starting states __snake_case : Union[str, Any] = 0X67_452_301 __snake_case : List[Any] = 0Xef_cda_b89 __snake_case : Optional[int] = 0X98_bad_cfe __snake_case : List[Any] = 0X10_325_476 __snake_case : List[str] = [ 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(UpperCamelCase__ ): __snake_case : Tuple = aa __snake_case : str = ba __snake_case : Dict = ca __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 __snake_case : Optional[int] = d ^ (b & (c ^ d)) __snake_case : str = i elif i <= 31: # f = (d & b) | (not_32(d) & c) # Alternate definition for f __snake_case : Optional[int] = c ^ (d & (b ^ c)) __snake_case : Dict = (5 * i + 1) % 16 elif i <= 47: __snake_case : Tuple = b ^ c ^ d __snake_case : Optional[int] = (3 * i + 5) % 16 else: __snake_case : str = c ^ (b | not_aa(UpperCamelCase__ )) __snake_case : Union[str, Any] = (7 * i) % 16 __snake_case : Any = (f + a + added_consts[i] + block_words[g]) % 2**32 __snake_case : Any = d __snake_case : int = c __snake_case : int = b __snake_case : List[Any] = sum_aa(UpperCamelCase__ , left_rotate_aa(UpperCamelCase__ , shift_amounts[i] ) ) # Add hashed chunk to running total __snake_case : Dict = sum_aa(UpperCamelCase__ , UpperCamelCase__ ) __snake_case : Optional[int] = sum_aa(UpperCamelCase__ , UpperCamelCase__ ) __snake_case : Optional[int] = sum_aa(UpperCamelCase__ , UpperCamelCase__ ) __snake_case : Optional[Any] = sum_aa(UpperCamelCase__ , UpperCamelCase__ ) __snake_case : Optional[int] = reformat_hex(UpperCamelCase__ ) + reformat_hex(UpperCamelCase__ ) + reformat_hex(UpperCamelCase__ ) + reformat_hex(UpperCamelCase__ ) return digest if __name__ == "__main__": import doctest doctest.testmod()
326
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_5_0, '''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_0_0, '''eval_accuracy''': 0.3, '''eval_loss''': 0.9}, }, ] ) class A_ (unittest.TestCase ): def _lowercase ( self ): '''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=_A , ) assert hasattr(self , '''env''' ) def _lowercase ( self , _A=1 ): '''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=_A , instance_type=self.instance_type , debugger_hook_config=_A , hyperparameters={**self.env.hyperparameters, '''model_name_or_path''': self.model_name_or_path} , metric_definitions=self.env.metric_definitions , py_version='''py36''' , ) def _lowercase ( self , _A ): '''simple docstring''' TrainingJobAnalytics(_A ).export_csv(F"""{self.env.test_path}/{job_name}_metrics.csv""" ) def _lowercase ( self ): '''simple docstring''' UpperCAmelCase = self.create_estimator() # run training estimator.fit() # result dataframe UpperCAmelCase = TrainingJobAnalytics(estimator.latest_training_job.name ).dataframe() # extract kpis UpperCAmelCase = list(result_metrics_df[result_metrics_df.metric_name == '''eval_accuracy''']['''value'''] ) UpperCAmelCase = list(result_metrics_df[result_metrics_df.metric_name == '''eval_loss''']['''value'''] ) # get train time from SageMaker job, this includes starting, preprocessing, stopping UpperCAmelCase = ( Session().describe_training_job(estimator.latest_training_job.name ).get('''TrainingTimeInSeconds''' , 9_9_9_9_9_9 ) ) # 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} , _A )
273
0
"""simple docstring""" import warnings from ...utils import logging from .image_processing_beit import BeitImageProcessor __A = logging.get_logger(__name__) class snake_case ( a_ ): def __init__( self : str , *UpperCamelCase__ : Tuple , **UpperCamelCase__ : Any)-> List[str]: '''simple docstring''' warnings.warn( "The class BeitFeatureExtractor is deprecated and will be removed in version 5 of Transformers. Please" " use BeitImageProcessor instead." , _A , ) super().__init__(*_A , **_A)
217
from collections import OrderedDict from typing import Mapping from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging __A : int = logging.get_logger(__name__) __A : Tuple = { "google/bigbird-roberta-base": "https://huggingface.co/google/bigbird-roberta-base/resolve/main/config.json", "google/bigbird-roberta-large": "https://huggingface.co/google/bigbird-roberta-large/resolve/main/config.json", "google/bigbird-base-trivia-itc": "https://huggingface.co/google/bigbird-base-trivia-itc/resolve/main/config.json", # See all BigBird models at https://huggingface.co/models?filter=big_bird } class A_ (a_ ): UpperCAmelCase__ = '''big_bird''' def __init__( self , _A=5_0_3_5_8 , _A=7_6_8 , _A=1_2 , _A=1_2 , _A=3_0_7_2 , _A="gelu_new" , _A=0.1 , _A=0.1 , _A=4_0_9_6 , _A=2 , _A=0.02 , _A=1E-12 , _A=True , _A=0 , _A=1 , _A=2 , _A=6_6 , _A="block_sparse" , _A=True , _A=False , _A=6_4 , _A=3 , _A=None , **_A , ): '''simple docstring''' super().__init__( pad_token_id=_A , bos_token_id=_A , eos_token_id=_A , sep_token_id=_A , **_A , ) UpperCAmelCase = vocab_size UpperCAmelCase = max_position_embeddings UpperCAmelCase = hidden_size UpperCAmelCase = num_hidden_layers UpperCAmelCase = num_attention_heads UpperCAmelCase = intermediate_size UpperCAmelCase = hidden_act UpperCAmelCase = hidden_dropout_prob UpperCAmelCase = attention_probs_dropout_prob UpperCAmelCase = initializer_range UpperCAmelCase = type_vocab_size UpperCAmelCase = layer_norm_eps UpperCAmelCase = use_cache UpperCAmelCase = rescale_embeddings UpperCAmelCase = attention_type UpperCAmelCase = use_bias UpperCAmelCase = block_size UpperCAmelCase = num_random_blocks UpperCAmelCase = classifier_dropout class A_ (a_ ): @property def _lowercase ( self ): '''simple docstring''' if self.task == "multiple-choice": UpperCAmelCase = {0: '''batch''', 1: '''choice''', 2: '''sequence'''} else: UpperCAmelCase = {0: '''batch''', 1: '''sequence'''} return OrderedDict( [ ('''input_ids''', dynamic_axis), ('''attention_mask''', dynamic_axis), ] )
273
0
from __future__ import annotations import unittest from transformers import MobileBertConfig, is_tf_available from transformers.models.auto import get_values from transformers.testing_utils import require_tf, slow from ...test_configuration_common import ConfigTester from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor, random_attention_mask from ...test_pipeline_mixin import PipelineTesterMixin if is_tf_available(): import tensorflow as tf from transformers import ( TF_MODEL_FOR_PRETRAINING_MAPPING, TFMobileBertForMaskedLM, TFMobileBertForMultipleChoice, TFMobileBertForNextSentencePrediction, TFMobileBertForPreTraining, TFMobileBertForQuestionAnswering, TFMobileBertForSequenceClassification, TFMobileBertForTokenClassification, TFMobileBertModel, ) @require_tf class __magic_name__ ( a_ , a_ , unittest.TestCase ): """simple docstring""" __UpperCamelCase = ( ( TFMobileBertModel, TFMobileBertForMaskedLM, TFMobileBertForNextSentencePrediction, TFMobileBertForPreTraining, TFMobileBertForQuestionAnswering, TFMobileBertForSequenceClassification, TFMobileBertForTokenClassification, TFMobileBertForMultipleChoice, ) if is_tf_available() else () ) __UpperCamelCase = ( { '''feature-extraction''': TFMobileBertModel, '''fill-mask''': TFMobileBertForMaskedLM, '''question-answering''': TFMobileBertForQuestionAnswering, '''text-classification''': TFMobileBertForSequenceClassification, '''token-classification''': TFMobileBertForTokenClassification, '''zero-shot''': TFMobileBertForSequenceClassification, } if is_tf_available() else {} ) __UpperCamelCase = False __UpperCamelCase = False def SCREAMING_SNAKE_CASE ( self :Dict , snake_case :Any , snake_case :List[Any] , snake_case :int=False ): '''simple docstring''' A_ : Any = super()._prepare_for_class(_A , _A , return_labels=_A ) if return_labels: if model_class in get_values(_A ): A_ : Optional[Any] = tf.zeros(self.model_tester.batch_size , dtype=tf.intaa ) return inputs_dict class __magic_name__ ( a_ ): """simple docstring""" def __init__( self :Any , snake_case :Optional[Any] , snake_case :Union[str, Any]=13 , snake_case :Any=7 , snake_case :List[Any]=True , snake_case :str=True , snake_case :List[Any]=True , snake_case :Union[str, Any]=True , snake_case :Tuple=99 , snake_case :str=32 , snake_case :Optional[Any]=32 , snake_case :List[str]=2 , snake_case :Dict=4 , snake_case :Optional[Any]=37 , snake_case :int="gelu" , snake_case :List[str]=0.1 , snake_case :Dict=0.1 , snake_case :Tuple=512 , snake_case :List[Any]=16 , snake_case :List[str]=2 , snake_case :int=0.02 , snake_case :Dict=3 , snake_case :Optional[int]=4 , snake_case :List[Any]=None , ): '''simple docstring''' A_ : List[str] = parent A_ : List[Any] = batch_size A_ : List[Any] = seq_length A_ : Dict = is_training A_ : Optional[Any] = use_input_mask A_ : Optional[Any] = use_token_type_ids A_ : Any = use_labels A_ : Optional[int] = vocab_size A_ : Union[str, Any] = hidden_size A_ : Union[str, Any] = num_hidden_layers A_ : List[Any] = num_attention_heads A_ : str = intermediate_size A_ : Dict = hidden_act A_ : str = hidden_dropout_prob A_ : Union[str, Any] = attention_probs_dropout_prob A_ : Union[str, Any] = max_position_embeddings A_ : List[str] = type_vocab_size A_ : Union[str, Any] = type_sequence_label_size A_ : Optional[Any] = initializer_range A_ : int = num_labels A_ : str = num_choices A_ : Any = scope A_ : Dict = embedding_size def SCREAMING_SNAKE_CASE ( self :Any ): '''simple docstring''' A_ : Any = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) A_ : List[Any] = None if self.use_input_mask: A_ : Tuple = random_attention_mask([self.batch_size, self.seq_length] ) A_ : Tuple = None if self.use_token_type_ids: A_ : int = ids_tensor([self.batch_size, self.seq_length] , self.type_vocab_size ) A_ : Optional[int] = None A_ : Tuple = None A_ : Dict = None if self.use_labels: A_ : Any = ids_tensor([self.batch_size] , self.type_sequence_label_size ) A_ : Optional[Any] = ids_tensor([self.batch_size, self.seq_length] , self.num_labels ) A_ : List[Any] = ids_tensor([self.batch_size] , self.num_choices ) A_ : Any = MobileBertConfig( 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 , embedding_size=self.embedding_size , ) return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels def SCREAMING_SNAKE_CASE ( self :Optional[Any] , snake_case :Optional[Any] , snake_case :Optional[int] , snake_case :Optional[Any] , snake_case :Tuple , snake_case :List[Any] , snake_case :str , snake_case :str ): '''simple docstring''' A_ : Dict = TFMobileBertModel(config=_A ) A_ : List[str] = {"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids} A_ : List[Any] = model(_A ) A_ : List[Any] = [input_ids, input_mask] A_ : int = model(_A ) A_ : Tuple = model(_A ) 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 SCREAMING_SNAKE_CASE ( self :List[Any] , snake_case :int , snake_case :str , snake_case :str , snake_case :Any , snake_case :Any , snake_case :Union[str, Any] , snake_case :Optional[Any] ): '''simple docstring''' A_ : str = TFMobileBertForMaskedLM(config=_A ) A_ : int = {"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids} A_ : Optional[int] = model(_A ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.vocab_size) ) def SCREAMING_SNAKE_CASE ( self :Dict , snake_case :str , snake_case :Union[str, Any] , snake_case :Dict , snake_case :Dict , snake_case :Tuple , snake_case :str , snake_case :List[Any] ): '''simple docstring''' A_ : Any = TFMobileBertForNextSentencePrediction(config=_A ) A_ : Optional[Any] = {"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids} A_ : Optional[int] = model(_A ) self.parent.assertEqual(result.logits.shape , (self.batch_size, 2) ) def SCREAMING_SNAKE_CASE ( self :int , snake_case :List[Any] , snake_case :Any , snake_case :List[Any] , snake_case :Any , snake_case :List[Any] , snake_case :Any , snake_case :Union[str, Any] ): '''simple docstring''' A_ : str = TFMobileBertForPreTraining(config=_A ) A_ : Optional[int] = {"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids} A_ : str = model(_A ) 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 SCREAMING_SNAKE_CASE ( self :Any , snake_case :List[str] , snake_case :Tuple , snake_case :Optional[Any] , snake_case :Tuple , snake_case :Optional[int] , snake_case :Dict , snake_case :Union[str, Any] ): '''simple docstring''' A_ : Tuple = self.num_labels A_ : Optional[int] = TFMobileBertForSequenceClassification(config=_A ) A_ : str = {"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids} A_ : Optional[int] = model(_A ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_labels) ) def SCREAMING_SNAKE_CASE ( self :str , snake_case :Optional[int] , snake_case :Any , snake_case :str , snake_case :str , snake_case :Union[str, Any] , snake_case :Optional[Any] , snake_case :Optional[int] ): '''simple docstring''' A_ : Any = self.num_choices A_ : str = TFMobileBertForMultipleChoice(config=_A ) A_ : str = tf.tile(tf.expand_dims(_A , 1 ) , (1, self.num_choices, 1) ) A_ : Union[str, Any] = tf.tile(tf.expand_dims(_A , 1 ) , (1, self.num_choices, 1) ) A_ : int = tf.tile(tf.expand_dims(_A , 1 ) , (1, self.num_choices, 1) ) A_ : List[Any] = { "input_ids": multiple_choice_inputs_ids, "attention_mask": multiple_choice_input_mask, "token_type_ids": multiple_choice_token_type_ids, } A_ : Dict = model(_A ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_choices) ) def SCREAMING_SNAKE_CASE ( self :int , snake_case :Any , snake_case :Optional[int] , snake_case :str , snake_case :Optional[int] , snake_case :List[str] , snake_case :Dict , snake_case :Optional[Any] ): '''simple docstring''' A_ : Dict = self.num_labels A_ : Dict = TFMobileBertForTokenClassification(config=_A ) A_ : List[str] = {"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids} A_ : Optional[Any] = model(_A ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.num_labels) ) def SCREAMING_SNAKE_CASE ( self :List[str] , snake_case :Tuple , snake_case :Tuple , snake_case :Optional[int] , snake_case :Tuple , snake_case :str , snake_case :Tuple , snake_case :List[Any] ): '''simple docstring''' A_ : Union[str, Any] = TFMobileBertForQuestionAnswering(config=_A ) A_ : List[str] = {"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids} A_ : int = model(_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 SCREAMING_SNAKE_CASE ( self :List[Any] ): '''simple docstring''' A_ : Optional[Any] = self.prepare_config_and_inputs() ( ( A_ ) , ( A_ ) , ( A_ ) , ( A_ ) , ( A_ ) , ( A_ ) , ( A_ ) , ) : List[str] = config_and_inputs A_ : Tuple = {"input_ids": input_ids, "token_type_ids": token_type_ids, "attention_mask": input_mask} return config, inputs_dict def SCREAMING_SNAKE_CASE ( self :int ): '''simple docstring''' A_ : Tuple = TFMobileBertModelTest.TFMobileBertModelTester(self ) A_ : Optional[int] = ConfigTester(self , config_class=_A , hidden_size=37 ) def SCREAMING_SNAKE_CASE ( self :str ): '''simple docstring''' self.config_tester.run_common_tests() def SCREAMING_SNAKE_CASE ( self :List[str] ): '''simple docstring''' A_ : List[str] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_mobilebert_model(*_A ) def SCREAMING_SNAKE_CASE ( self :Dict ): '''simple docstring''' A_ : Dict = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_mobilebert_for_masked_lm(*_A ) def SCREAMING_SNAKE_CASE ( self :Tuple ): '''simple docstring''' A_ : Tuple = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_mobilebert_for_multiple_choice(*_A ) def SCREAMING_SNAKE_CASE ( self :Tuple ): '''simple docstring''' A_ : Union[str, Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_mobilebert_for_next_sequence_prediction(*_A ) def SCREAMING_SNAKE_CASE ( self :List[str] ): '''simple docstring''' A_ : int = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_mobilebert_for_pretraining(*_A ) def SCREAMING_SNAKE_CASE ( self :str ): '''simple docstring''' A_ : Tuple = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_mobilebert_for_question_answering(*_A ) def SCREAMING_SNAKE_CASE ( self :Dict ): '''simple docstring''' A_ : Optional[int] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_mobilebert_for_sequence_classification(*_A ) def SCREAMING_SNAKE_CASE ( self :List[Any] ): '''simple docstring''' A_ : Optional[Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_mobilebert_for_token_classification(*_A ) @slow def SCREAMING_SNAKE_CASE ( self :List[str] ): '''simple docstring''' for model_name in ["google/mobilebert-uncased"]: A_ : Union[str, Any] = TFMobileBertModel.from_pretrained(_A ) self.assertIsNotNone(_A ) @require_tf class __magic_name__ ( unittest.TestCase ): """simple docstring""" @slow def SCREAMING_SNAKE_CASE ( self :Tuple ): '''simple docstring''' A_ : List[Any] = TFMobileBertForPreTraining.from_pretrained("google/mobilebert-uncased" ) A_ : Optional[Any] = tf.constant([[0, 1, 2, 3, 4, 5]] ) A_ : Optional[Any] = model(_A )[0] A_ : Optional[int] = [1, 6, 30_522] self.assertEqual(output.shape , _A ) A_ : Optional[int] = tf.constant( [ [ [-4.5919547, -9.248295, -9.645256], [-6.7306175, -6.440284, -6.6052837], [-7.2743506, -6.7847915, -6.024673], ] ] ) tf.debugging.assert_near(output[:, :3, :3] , _A , atol=1e-4 )
300
from __future__ import annotations import inspect import unittest from transformers import ViTConfig from transformers.testing_utils import require_tf, require_vision, slow from transformers.utils import cached_property, is_tf_available, is_vision_available from ...test_configuration_common import ConfigTester from ...test_modeling_tf_common import TFModelTesterMixin, floats_tensor, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_tf_available(): import tensorflow as tf from transformers import TFViTForImageClassification, TFViTModel if is_vision_available(): from PIL import Image from transformers import ViTImageProcessor class A_ : def __init__( self , _A , _A=1_3 , _A=3_0 , _A=2 , _A=3 , _A=True , _A=True , _A=3_2 , _A=2 , _A=4 , _A=3_7 , _A="gelu" , _A=0.1 , _A=0.1 , _A=1_0 , _A=0.02 , _A=3 , _A=None , ): '''simple docstring''' UpperCAmelCase = parent UpperCAmelCase = batch_size UpperCAmelCase = image_size UpperCAmelCase = patch_size UpperCAmelCase = num_channels UpperCAmelCase = is_training UpperCAmelCase = use_labels UpperCAmelCase = hidden_size UpperCAmelCase = num_hidden_layers UpperCAmelCase = num_attention_heads UpperCAmelCase = intermediate_size UpperCAmelCase = hidden_act UpperCAmelCase = hidden_dropout_prob UpperCAmelCase = attention_probs_dropout_prob UpperCAmelCase = type_sequence_label_size UpperCAmelCase = initializer_range UpperCAmelCase = scope # in ViT, the seq length equals the number of patches + 1 (we add 1 for the [CLS] token) UpperCAmelCase = (image_size // patch_size) ** 2 UpperCAmelCase = num_patches + 1 def _lowercase ( self ): '''simple docstring''' UpperCAmelCase = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) UpperCAmelCase = None if self.use_labels: UpperCAmelCase = ids_tensor([self.batch_size] , self.type_sequence_label_size ) UpperCAmelCase = self.get_config() return config, pixel_values, labels def _lowercase ( self ): '''simple docstring''' return ViTConfig( image_size=self.image_size , patch_size=self.patch_size , num_channels=self.num_channels , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , is_decoder=_A , initializer_range=self.initializer_range , ) def _lowercase ( self , _A , _A , _A ): '''simple docstring''' UpperCAmelCase = TFViTModel(config=_A ) UpperCAmelCase = model(_A , training=_A ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) # Test with an image with different size than the one specified in config. UpperCAmelCase = self.image_size // 2 UpperCAmelCase = pixel_values[:, :, :image_size, :image_size] UpperCAmelCase = model(_A , interpolate_pos_encoding=_A , training=_A ) UpperCAmelCase = (image_size // self.patch_size) ** 2 + 1 self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, seq_length, self.hidden_size) ) def _lowercase ( self , _A , _A , _A ): '''simple docstring''' UpperCAmelCase = self.type_sequence_label_size UpperCAmelCase = TFViTForImageClassification(_A ) UpperCAmelCase = model(_A , labels=_A , training=_A ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.type_sequence_label_size) ) # Test with an image with different size than the one specified in config. UpperCAmelCase = self.image_size // 2 UpperCAmelCase = pixel_values[:, :, :image_size, :image_size] UpperCAmelCase = model(_A , interpolate_pos_encoding=_A , training=_A ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.type_sequence_label_size) ) # test greyscale images UpperCAmelCase = 1 UpperCAmelCase = TFViTForImageClassification(_A ) UpperCAmelCase = floats_tensor([self.batch_size, 1, self.image_size, self.image_size] ) UpperCAmelCase = model(_A ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.type_sequence_label_size) ) def _lowercase ( self ): '''simple docstring''' UpperCAmelCase = self.prepare_config_and_inputs() UpperCAmelCase , UpperCAmelCase , UpperCAmelCase = config_and_inputs UpperCAmelCase = {'''pixel_values''': pixel_values} return config, inputs_dict @require_tf class A_ (a_ , a_ , unittest.TestCase ): UpperCAmelCase__ = (TFViTModel, TFViTForImageClassification) if is_tf_available() else () UpperCAmelCase__ = ( {'''feature-extraction''': TFViTModel, '''image-classification''': TFViTForImageClassification} if is_tf_available() else {} ) UpperCAmelCase__ = False UpperCAmelCase__ = False UpperCAmelCase__ = False def _lowercase ( self ): '''simple docstring''' UpperCAmelCase = TFViTModelTester(self ) UpperCAmelCase = ConfigTester(self , config_class=_A , has_text_modality=_A , hidden_size=3_7 ) def _lowercase ( self ): '''simple docstring''' self.config_tester.run_common_tests() @unittest.skip(reason='''ViT does not use inputs_embeds''' ) def _lowercase ( self ): '''simple docstring''' pass @unittest.skip(reason='''ViT does not use inputs_embeds''' ) def _lowercase ( self ): '''simple docstring''' pass def _lowercase ( self ): '''simple docstring''' UpperCAmelCase , UpperCAmelCase = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: UpperCAmelCase = model_class(_A ) self.assertIsInstance(model.get_input_embeddings() , (tf.keras.layers.Layer) ) UpperCAmelCase = model.get_output_embeddings() self.assertTrue(x is None or isinstance(_A , tf.keras.layers.Layer ) ) def _lowercase ( self ): '''simple docstring''' UpperCAmelCase , UpperCAmelCase = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: UpperCAmelCase = model_class(_A ) UpperCAmelCase = inspect.signature(model.call ) # signature.parameters is an OrderedDict => so arg_names order is deterministic UpperCAmelCase = [*signature.parameters.keys()] UpperCAmelCase = ['''pixel_values'''] self.assertListEqual(arg_names[:1] , _A ) def _lowercase ( self ): '''simple docstring''' UpperCAmelCase = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*_A ) def _lowercase ( self ): '''simple docstring''' UpperCAmelCase = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_image_classification(*_A ) @slow def _lowercase ( self ): '''simple docstring''' UpperCAmelCase = TFViTModel.from_pretrained('''google/vit-base-patch16-224''' ) self.assertIsNotNone(_A ) def __SCREAMING_SNAKE_CASE ( ) -> Optional[Any]: '''simple docstring''' UpperCAmelCase = Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png''' ) return image @require_tf @require_vision class A_ (unittest.TestCase ): @cached_property def _lowercase ( self ): '''simple docstring''' return ViTImageProcessor.from_pretrained('''google/vit-base-patch16-224''' ) if is_vision_available() else None @slow def _lowercase ( self ): '''simple docstring''' UpperCAmelCase = TFViTForImageClassification.from_pretrained('''google/vit-base-patch16-224''' ) UpperCAmelCase = self.default_image_processor UpperCAmelCase = prepare_img() UpperCAmelCase = image_processor(images=_A , return_tensors='''tf''' ) # forward pass UpperCAmelCase = model(**_A ) # verify the logits UpperCAmelCase = tf.TensorShape((1, 1_0_0_0) ) self.assertEqual(outputs.logits.shape , _A ) UpperCAmelCase = tf.constant([-0.27_44, 0.82_15, -0.08_36] ) tf.debugging.assert_near(outputs.logits[0, :3] , _A , atol=1E-4 )
273
0
"""simple docstring""" def __UpperCAmelCase ( __lowerCamelCase ) -> set: lowercase__ : Optional[int] = set() # edges = list of graph's edges lowercase__ : Optional[Any] = get_edges(UpperCamelCase__ ) # While there are still elements in edges list, take an arbitrary edge # (from_node, to_node) and add his extremity to chosen_vertices and then # remove all arcs adjacent to the from_node and to_node while edges: lowercase__ , lowercase__ : Tuple = edges.pop() chosen_vertices.add(UpperCamelCase__ ) chosen_vertices.add(UpperCamelCase__ ) for edge in edges.copy(): if from_node in edge or to_node in edge: edges.discard(UpperCamelCase__ ) return chosen_vertices def __UpperCAmelCase ( __lowerCamelCase ) -> set: lowercase__ : int = set() for from_node, to_nodes in graph.items(): for to_node in to_nodes: edges.add((from_node, to_node) ) return edges if __name__ == "__main__": import doctest doctest.testmod() # graph = {0: [1, 3], 1: [0, 3], 2: [0, 3, 4], 3: [0, 1, 2], 4: [2, 3]} # print(f"Matching vertex cover:\n{matching_min_vertex_cover(graph)}")
16
import unittest from transformers import is_torch_available from transformers.testing_utils import require_sentencepiece, require_tokenizers, require_torch, slow if is_torch_available(): import torch from transformers import XLMRobertaModel @require_sentencepiece @require_tokenizers @require_torch class A_ (unittest.TestCase ): @slow def _lowercase ( self ): '''simple docstring''' UpperCAmelCase = XLMRobertaModel.from_pretrained('''xlm-roberta-base''' ) UpperCAmelCase = torch.tensor([[0, 5_8_1, 1_0_2_6_9, 8_3, 9_9_9_4_2, 1_3_6, 6_0_7_4_2, 2_3, 7_0, 8_0_5_8_3, 1_8_2_7_6, 2]] ) # The dog is cute and lives in the garden house UpperCAmelCase = torch.Size((1, 1_2, 7_6_8) ) # batch_size, sequence_length, embedding_vector_dim UpperCAmelCase = torch.tensor( [[-0.01_01, 0.12_18, -0.08_03, 0.08_01, 0.13_27, 0.07_76, -0.12_15, 0.23_83, 0.33_38, 0.31_06, 0.03_00, 0.02_52]] ) # xlmr = torch.hub.load('pytorch/fairseq', 'xlmr.base') # xlmr.eval() # expected_output_values_last_dim = xlmr.extract_features(input_ids[0])[:, :, -1] with torch.no_grad(): UpperCAmelCase = model(_A )['''last_hidden_state'''].detach() self.assertEqual(output.shape , _A ) # compare the actual values for a slice of last dim self.assertTrue(torch.allclose(output[:, :, -1] , _A , atol=1E-3 ) ) @slow def _lowercase ( self ): '''simple docstring''' UpperCAmelCase = XLMRobertaModel.from_pretrained('''xlm-roberta-large''' ) UpperCAmelCase = torch.tensor([[0, 5_8_1, 1_0_2_6_9, 8_3, 9_9_9_4_2, 1_3_6, 6_0_7_4_2, 2_3, 7_0, 8_0_5_8_3, 1_8_2_7_6, 2]] ) # The dog is cute and lives in the garden house UpperCAmelCase = torch.Size((1, 1_2, 1_0_2_4) ) # batch_size, sequence_length, embedding_vector_dim UpperCAmelCase = torch.tensor( [[-0.06_99, -0.03_18, 0.07_05, -0.12_41, 0.09_99, -0.05_20, 0.10_04, -0.18_38, -0.47_04, 0.14_37, 0.08_21, 0.01_26]] ) # xlmr = torch.hub.load('pytorch/fairseq', 'xlmr.large') # xlmr.eval() # expected_output_values_last_dim = xlmr.extract_features(input_ids[0])[:, :, -1] with torch.no_grad(): UpperCAmelCase = model(_A )['''last_hidden_state'''].detach() self.assertEqual(output.shape , _A ) # compare the actual values for a slice of last dim self.assertTrue(torch.allclose(output[:, :, -1] , _A , atol=1E-3 ) )
273
0
from __future__ import annotations import unittest from transformers import DebertaVaConfig, is_tf_available from transformers.testing_utils import require_tf, slow from ...test_configuration_common import ConfigTester from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor, random_attention_mask from ...test_pipeline_mixin import PipelineTesterMixin if is_tf_available(): import tensorflow as tf from transformers import ( TFDebertaVaForMaskedLM, TFDebertaVaForQuestionAnswering, TFDebertaVaForSequenceClassification, TFDebertaVaForTokenClassification, TFDebertaVaModel, ) class A_ : '''simple docstring''' def __init__( self , snake_case , snake_case=13 , snake_case=7 , snake_case=True , snake_case=True , snake_case=True , snake_case=True , snake_case=99 , snake_case=32 , snake_case=2 , snake_case=4 , snake_case=37 , snake_case="gelu" , snake_case=0.1 , snake_case=0.1 , snake_case=512 , snake_case=16 , snake_case=2 , snake_case=0.02 , snake_case=False , snake_case=True , snake_case="None" , snake_case=3 , snake_case=4 , snake_case=None , ): lowercase = parent lowercase = batch_size lowercase = seq_length lowercase = is_training lowercase = use_input_mask lowercase = use_token_type_ids lowercase = use_labels lowercase = vocab_size lowercase = hidden_size lowercase = num_hidden_layers lowercase = num_attention_heads lowercase = intermediate_size lowercase = hidden_act lowercase = hidden_dropout_prob lowercase = attention_probs_dropout_prob lowercase = max_position_embeddings lowercase = type_vocab_size lowercase = type_sequence_label_size lowercase = initializer_range lowercase = num_labels lowercase = num_choices lowercase = relative_attention lowercase = position_biased_input lowercase = pos_att_type lowercase = scope def SCREAMING_SNAKE_CASE__ ( self ): lowercase = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) lowercase = None if self.use_input_mask: lowercase = random_attention_mask([self.batch_size, self.seq_length] ) lowercase = None if self.use_token_type_ids: lowercase = ids_tensor([self.batch_size, self.seq_length] , self.type_vocab_size ) lowercase = None lowercase = None lowercase = None if self.use_labels: lowercase = ids_tensor([self.batch_size] , self.type_sequence_label_size ) lowercase = ids_tensor([self.batch_size, self.seq_length] , self.num_labels ) lowercase = DebertaVaConfig( 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 , relative_attention=self.relative_attention , position_biased_input=self.position_biased_input , initializer_range=self.initializer_range , return_dict=_A , ) return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels def SCREAMING_SNAKE_CASE__ ( self , snake_case , snake_case , snake_case , snake_case , snake_case , snake_case , snake_case ): lowercase = TFDebertaVaModel(config=_A ) lowercase = {'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids} lowercase = [input_ids, input_mask] lowercase = model(_A ) lowercase = model(_A ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def SCREAMING_SNAKE_CASE__ ( self , snake_case , snake_case , snake_case , snake_case , snake_case , snake_case , snake_case ): lowercase = TFDebertaVaForMaskedLM(config=_A ) lowercase = { 'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids, } lowercase = model(_A ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.vocab_size) ) def SCREAMING_SNAKE_CASE__ ( self , snake_case , snake_case , snake_case , snake_case , snake_case , snake_case , snake_case ): lowercase = self.num_labels lowercase = TFDebertaVaForSequenceClassification(config=_A ) lowercase = { 'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids, } lowercase = model(_A ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_labels) ) def SCREAMING_SNAKE_CASE__ ( self , snake_case , snake_case , snake_case , snake_case , snake_case , snake_case , snake_case ): lowercase = self.num_labels lowercase = TFDebertaVaForTokenClassification(config=_A ) lowercase = { 'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids, } lowercase = model(_A ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.num_labels) ) def SCREAMING_SNAKE_CASE__ ( self , snake_case , snake_case , snake_case , snake_case , snake_case , snake_case , snake_case ): lowercase = TFDebertaVaForQuestionAnswering(config=_A ) lowercase = { 'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids, } lowercase = model(_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 SCREAMING_SNAKE_CASE__ ( self ): lowercase = self.prepare_config_and_inputs() ( ( lowercase ) , ( lowercase ) , ( lowercase ) , ( lowercase ) , ( lowercase ) , ( lowercase ) , ( lowercase ) , ) = config_and_inputs lowercase = {'input_ids': input_ids, 'token_type_ids': token_type_ids, 'attention_mask': input_mask} return config, inputs_dict @require_tf class A_ ( a_ , a_ , unittest.TestCase ): '''simple docstring''' _UpperCamelCase : Any = ( ( TFDebertaVaModel, TFDebertaVaForMaskedLM, TFDebertaVaForQuestionAnswering, TFDebertaVaForSequenceClassification, TFDebertaVaForTokenClassification, ) if is_tf_available() else () ) _UpperCamelCase : Any = ( { """feature-extraction""": TFDebertaVaModel, """fill-mask""": TFDebertaVaForMaskedLM, """question-answering""": TFDebertaVaForQuestionAnswering, """text-classification""": TFDebertaVaForSequenceClassification, """token-classification""": TFDebertaVaForTokenClassification, """zero-shot""": TFDebertaVaForSequenceClassification, } if is_tf_available() else {} ) _UpperCamelCase : List[str] = False _UpperCamelCase : Tuple = False def SCREAMING_SNAKE_CASE__ ( self ): lowercase = TFDebertaVaModelTester(self ) lowercase = ConfigTester(self , config_class=_A , hidden_size=37 ) def SCREAMING_SNAKE_CASE__ ( self ): self.config_tester.run_common_tests() def SCREAMING_SNAKE_CASE__ ( self ): lowercase = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*_A ) def SCREAMING_SNAKE_CASE__ ( self ): lowercase = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_masked_lm(*_A ) def SCREAMING_SNAKE_CASE__ ( self ): lowercase = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_question_answering(*_A ) def SCREAMING_SNAKE_CASE__ ( self ): lowercase = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_sequence_classification(*_A ) def SCREAMING_SNAKE_CASE__ ( self ): lowercase = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_token_classification(*_A ) @slow def SCREAMING_SNAKE_CASE__ ( self ): lowercase = TFDebertaVaModel.from_pretrained('kamalkraj/deberta-v2-xlarge' ) self.assertIsNotNone(_A ) @require_tf class A_ ( unittest.TestCase ): '''simple docstring''' @unittest.skip(reason='Model not available yet' ) def SCREAMING_SNAKE_CASE__ ( self ): pass @slow def SCREAMING_SNAKE_CASE__ ( self ): lowercase = TFDebertaVaModel.from_pretrained('kamalkraj/deberta-v2-xlarge' ) lowercase = tf.constant([[0, 3_1414, 232, 328, 740, 1140, 1_2695, 69, 4_6078, 1588, 2]] ) lowercase = tf.constant([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]] ) lowercase = model(_A , attention_mask=_A )[0] lowercase = tf.constant( [[[0.2_356, 0.1_948, 0.0_369], [-0.1_063, 0.3_586, -0.5_152], [-0.6_399, -0.0_259, -0.2_525]]] ) tf.debugging.assert_near(output[:, 1:4, 1:4] , _A , atol=1E-4 )
195
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") __A : Optional[int] = logging.getLogger(__name__) @dataclass class A_ : UpperCAmelCase__ = field( metadata={'''help''': '''Path to pretrained model or model identifier from huggingface.co/models'''} ) UpperCAmelCase__ = field( default=a_ , metadata={'''help''': '''Pretrained config name or path if not the same as model_name'''} ) UpperCAmelCase__ = field( default=a_ , metadata={'''help''': '''Pretrained tokenizer name or path if not the same as model_name'''} ) UpperCAmelCase__ = field( default=a_ , metadata={'''help''': '''Where do you want to store the pretrained models downloaded from huggingface.co'''} , ) UpperCAmelCase__ = field( default=a_ , 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=a_ , metadata={ '''help''': ( '''Will use the token generated when running `huggingface-cli login` (necessary to use this script ''' '''with private models).''' ) } , ) @dataclass class A_ : UpperCAmelCase__ = field(default=a_ , metadata={'''help''': '''The input training data file (a text file).'''} ) UpperCAmelCase__ = field( default=a_ , metadata={'''help''': '''An optional input evaluation data file to evaluate the perplexity on (a text file).'''} , ) UpperCAmelCase__ = field( default=a_ , metadata={'''help''': '''Overwrite the cached training and evaluation sets'''} ) UpperCAmelCase__ = field( default=a_ , metadata={'''help''': '''The number of processes to use for the preprocessing.'''} , ) UpperCAmelCase__ = field( default=a_ , 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=a_ , 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=a_ , metadata={ '''help''': ( '''For debugging purposes or quicker training, truncate the number of training examples to this ''' '''value if set.''' ) } , ) UpperCAmelCase__ = field( default=a_ , metadata={ '''help''': ( '''For debugging purposes or quicker training, truncate the number of evaluation examples to this ''' '''value if set.''' ) } , ) def _lowercase ( self ): '''simple docstring''' if self.train_file is not None: UpperCAmelCase = 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: UpperCAmelCase = self.validation_file.split('''.''' )[-1] assert extension in ["csv", "json"], "`validation_file` should be a csv or a json file." @dataclass class A_ : UpperCAmelCase__ = 42 UpperCAmelCase__ = True UpperCAmelCase__ = None UpperCAmelCase__ = None def __call__( self , _A ): '''simple docstring''' UpperCAmelCase = '''label''' if '''label''' in features[0].keys() else '''labels''' UpperCAmelCase = [feature.pop(_A ) for feature in features] UpperCAmelCase = len(_A ) UpperCAmelCase = len(features[0]['''input_ids'''] ) UpperCAmelCase = [ [{k: v[i] for k, v in feature.items()} for i in range(_A )] for feature in features ] UpperCAmelCase = list(chain(*_A ) ) UpperCAmelCase = self.tokenizer.pad( _A , padding=self.padding , max_length=self.max_length , pad_to_multiple_of=self.pad_to_multiple_of , return_tensors='''pt''' , ) # Un-flatten UpperCAmelCase = {k: v.view(_A , _A , -1 ) for k, v in batch.items()} # Add back labels UpperCAmelCase = torch.tensor(_A , dtype=torch.intaa ) return batch def __SCREAMING_SNAKE_CASE ( ) -> Optional[int]: '''simple docstring''' UpperCAmelCase = 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. UpperCAmelCase , UpperCAmelCase , UpperCAmelCase = parser.parse_json_file(json_file=os.path.abspath(sys.argv[1] ) ) else: UpperCAmelCase , UpperCAmelCase , UpperCAmelCase = 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''' , UpperCamelCase__ , UpperCamelCase__ ) # Setup logging logging.basicConfig( format='''%(asctime)s - %(levelname)s - %(name)s - %(message)s''' , datefmt='''%m/%d/%Y %H:%M:%S''' , handlers=[logging.StreamHandler(sys.stdout )] , ) if training_args.should_log: # The default of training_args.log_level is passive, so we set log level at info here to have that default. transformers.utils.logging.set_verbosity_info() UpperCAmelCase = training_args.get_process_log_level() logger.setLevel(UpperCamelCase__ ) datasets.utils.logging.set_verbosity(UpperCamelCase__ ) transformers.utils.logging.set_verbosity(UpperCamelCase__ ) transformers.utils.logging.enable_default_handler() transformers.utils.logging.enable_explicit_format() # Log on each process the small summary: logger.warning( F"""Process rank: {training_args.local_rank}, device: {training_args.device}, n_gpu: {training_args.n_gpu}""" + F"""distributed training: {bool(training_args.local_rank != -1 )}, 16-bits training: {training_args.fpaa}""" ) logger.info(F"""Training/evaluation parameters {training_args}""" ) # Detecting last checkpoint. UpperCAmelCase = None if os.path.isdir(training_args.output_dir ) and training_args.do_train and not training_args.overwrite_output_dir: UpperCAmelCase = 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: UpperCAmelCase = {} if data_args.train_file is not None: UpperCAmelCase = data_args.train_file if data_args.validation_file is not None: UpperCAmelCase = data_args.validation_file UpperCAmelCase = data_args.train_file.split('''.''' )[-1] UpperCAmelCase = load_dataset( UpperCamelCase__ , data_files=UpperCamelCase__ , 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. UpperCAmelCase = 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. UpperCAmelCase = 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 , ) UpperCAmelCase = 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 , ) UpperCAmelCase = AutoModelForMultipleChoice.from_pretrained( model_args.model_name_or_path , from_tf=bool('''.ckpt''' in model_args.model_name_or_path ) , config=UpperCamelCase__ , cache_dir=model_args.cache_dir , revision=model_args.model_revision , use_auth_token=True if model_args.use_auth_token else None , ) # When using your own dataset or a different dataset from swag, you will probably need to change this. UpperCAmelCase = [F"""ending{i}""" for i in range(4 )] UpperCAmelCase = '''sent1''' UpperCAmelCase = '''sent2''' if data_args.max_seq_length is None: UpperCAmelCase = tokenizer.model_max_length if max_seq_length > 1024: 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`.''' ) UpperCAmelCase = 1024 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}.""" ) UpperCAmelCase = min(data_args.max_seq_length , tokenizer.model_max_length ) # Preprocessing the datasets. def preprocess_function(UpperCamelCase__ ): UpperCAmelCase = [[context] * 4 for context in examples[context_name]] UpperCAmelCase = examples[question_header_name] UpperCAmelCase = [ [F"""{header} {examples[end][i]}""" for end in ending_names] for i, header in enumerate(UpperCamelCase__ ) ] # Flatten out UpperCAmelCase = list(chain(*UpperCamelCase__ ) ) UpperCAmelCase = list(chain(*UpperCamelCase__ ) ) # Tokenize UpperCAmelCase = tokenizer( UpperCamelCase__ , UpperCamelCase__ , truncation=UpperCamelCase__ , max_length=UpperCamelCase__ , 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(UpperCamelCase__ ) , 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''' ) UpperCAmelCase = raw_datasets['''train'''] if data_args.max_train_samples is not None: UpperCAmelCase = min(len(UpperCamelCase__ ) , data_args.max_train_samples ) UpperCAmelCase = train_dataset.select(range(UpperCamelCase__ ) ) with training_args.main_process_first(desc='''train dataset map pre-processing''' ): UpperCAmelCase = train_dataset.map( UpperCamelCase__ , batched=UpperCamelCase__ , 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''' ) UpperCAmelCase = raw_datasets['''validation'''] if data_args.max_eval_samples is not None: UpperCAmelCase = min(len(UpperCamelCase__ ) , data_args.max_eval_samples ) UpperCAmelCase = eval_dataset.select(range(UpperCamelCase__ ) ) with training_args.main_process_first(desc='''validation dataset map pre-processing''' ): UpperCAmelCase = eval_dataset.map( UpperCamelCase__ , batched=UpperCamelCase__ , num_proc=data_args.preprocessing_num_workers , load_from_cache_file=not data_args.overwrite_cache , ) # Data collator UpperCAmelCase = ( default_data_collator if data_args.pad_to_max_length else DataCollatorForMultipleChoice(tokenizer=UpperCamelCase__ , pad_to_multiple_of=8 if training_args.fpaa else None ) ) # Metric def compute_metrics(UpperCamelCase__ ): UpperCAmelCase , UpperCAmelCase = eval_predictions UpperCAmelCase = np.argmax(UpperCamelCase__ , axis=1 ) return {"accuracy": (preds == label_ids).astype(np.floataa ).mean().item()} # Initialize our Trainer UpperCAmelCase = Trainer( model=UpperCamelCase__ , args=UpperCamelCase__ , train_dataset=train_dataset if training_args.do_train else None , eval_dataset=eval_dataset if training_args.do_eval else None , tokenizer=UpperCamelCase__ , data_collator=UpperCamelCase__ , compute_metrics=UpperCamelCase__ , ) # Training if training_args.do_train: UpperCAmelCase = None if training_args.resume_from_checkpoint is not None: UpperCAmelCase = training_args.resume_from_checkpoint elif last_checkpoint is not None: UpperCAmelCase = last_checkpoint UpperCAmelCase = trainer.train(resume_from_checkpoint=UpperCamelCase__ ) trainer.save_model() # Saves the tokenizer too for easy upload UpperCAmelCase = train_result.metrics UpperCAmelCase = ( data_args.max_train_samples if data_args.max_train_samples is not None else len(UpperCamelCase__ ) ) UpperCAmelCase = min(UpperCamelCase__ , len(UpperCamelCase__ ) ) trainer.log_metrics('''train''' , UpperCamelCase__ ) trainer.save_metrics('''train''' , UpperCamelCase__ ) trainer.save_state() # Evaluation if training_args.do_eval: logger.info('''*** Evaluate ***''' ) UpperCAmelCase = trainer.evaluate() UpperCAmelCase = data_args.max_eval_samples if data_args.max_eval_samples is not None else len(UpperCamelCase__ ) UpperCAmelCase = min(UpperCamelCase__ , len(UpperCamelCase__ ) ) trainer.log_metrics('''eval''' , UpperCamelCase__ ) trainer.save_metrics('''eval''' , UpperCamelCase__ ) UpperCAmelCase = { '''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(**UpperCamelCase__ ) else: trainer.create_model_card(**UpperCamelCase__ ) def __SCREAMING_SNAKE_CASE ( UpperCamelCase__ ) -> int: '''simple docstring''' main() if __name__ == "__main__": main()
273
0
'''simple docstring''' from __future__ import annotations from collections import namedtuple from dataclasses import dataclass @dataclass class __lowerCAmelCase : """simple docstring""" _snake_case : Any = 4_2 _snake_case : Optional[Any] = None _snake_case : Any = None lowercase__ : Union[str, Any] = namedtuple('CoinsDistribResult', 'moves excess') def a__ ( lowercase : str ) -> int: """simple docstring""" if root is None: return 0 # Validation def count_nodes(lowercase : Tuple ) -> int: if node is None: return 0 return count_nodes(node.left ) + count_nodes(node.right ) + 1 def count_coins(lowercase : Any ) -> int: if node is None: return 0 return count_coins(node.left ) + count_coins(node.right ) + node.data if count_nodes(UpperCamelCase__ ) != count_coins(UpperCamelCase__ ): raise ValueError('''The nodes number should be same as the number of coins''' ) # Main calculation def get_distrib(lowercase : str ) -> CoinsDistribResult: if node is None: return CoinsDistribResult(0, 1 ) _UpperCamelCase , _UpperCamelCase = get_distrib(node.left ) _UpperCamelCase , _UpperCamelCase = get_distrib(node.right ) _UpperCamelCase = 1 - left_distrib_excess _UpperCamelCase = 1 - right_distrib_excess _UpperCamelCase = ( left_distrib_moves + right_distrib_moves + abs(UpperCamelCase__ ) + abs(UpperCamelCase__ ) ) _UpperCamelCase = node.data - coins_to_left - coins_to_right return CoinsDistribResult(UpperCamelCase__, UpperCamelCase__ ) return get_distrib(UpperCamelCase__ )[0] if __name__ == "__main__": import doctest doctest.testmod()
324
from __future__ import annotations import unittest from transformers import AutoTokenizer, MBartConfig, is_tf_available from transformers.testing_utils import require_sentencepiece, require_tf, require_tokenizers, slow from transformers.utils import cached_property from ...test_configuration_common import ConfigTester from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_tf_available(): import tensorflow as tf from transformers import TFAutoModelForSeqaSeqLM, TFMBartForConditionalGeneration, TFMBartModel @require_tf class A_ : UpperCAmelCase__ = MBartConfig UpperCAmelCase__ = {} UpperCAmelCase__ = '''gelu''' def __init__( self , _A , _A=1_3 , _A=7 , _A=True , _A=False , _A=9_9 , _A=3_2 , _A=2 , _A=4 , _A=3_7 , _A=0.1 , _A=0.1 , _A=2_0 , _A=2 , _A=1 , _A=0 , ): '''simple docstring''' UpperCAmelCase = parent UpperCAmelCase = batch_size UpperCAmelCase = seq_length UpperCAmelCase = is_training UpperCAmelCase = use_labels UpperCAmelCase = vocab_size UpperCAmelCase = hidden_size UpperCAmelCase = num_hidden_layers UpperCAmelCase = num_attention_heads UpperCAmelCase = intermediate_size UpperCAmelCase = hidden_dropout_prob UpperCAmelCase = attention_probs_dropout_prob UpperCAmelCase = max_position_embeddings UpperCAmelCase = eos_token_id UpperCAmelCase = pad_token_id UpperCAmelCase = bos_token_id def _lowercase ( self ): '''simple docstring''' UpperCAmelCase = ids_tensor([self.batch_size, self.seq_length - 1] , self.vocab_size ) UpperCAmelCase = tf.expand_dims(tf.constant([self.eos_token_id] * self.batch_size ) , 1 ) UpperCAmelCase = tf.concat([input_ids, eos_tensor] , axis=1 ) UpperCAmelCase = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) UpperCAmelCase = self.config_cls( vocab_size=self.vocab_size , d_model=self.hidden_size , encoder_layers=self.num_hidden_layers , decoder_layers=self.num_hidden_layers , encoder_attention_heads=self.num_attention_heads , decoder_attention_heads=self.num_attention_heads , encoder_ffn_dim=self.intermediate_size , decoder_ffn_dim=self.intermediate_size , dropout=self.hidden_dropout_prob , attention_dropout=self.attention_probs_dropout_prob , max_position_embeddings=self.max_position_embeddings , eos_token_ids=[2] , bos_token_id=self.bos_token_id , pad_token_id=self.pad_token_id , decoder_start_token_id=self.pad_token_id , **self.config_updates , ) UpperCAmelCase = prepare_mbart_inputs_dict(_A , _A , _A ) return config, inputs_dict def _lowercase ( self , _A , _A ): '''simple docstring''' UpperCAmelCase = TFMBartModel(config=_A ).get_decoder() UpperCAmelCase = inputs_dict['''input_ids'''] UpperCAmelCase = input_ids[:1, :] UpperCAmelCase = inputs_dict['''attention_mask'''][:1, :] UpperCAmelCase = inputs_dict['''head_mask'''] UpperCAmelCase = 1 # first forward pass UpperCAmelCase = model(_A , attention_mask=_A , head_mask=_A , use_cache=_A ) UpperCAmelCase , UpperCAmelCase = outputs.to_tuple() UpperCAmelCase = past_key_values[1] def __SCREAMING_SNAKE_CASE ( UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__=None , UpperCamelCase__=None , UpperCamelCase__=None , UpperCamelCase__=None , UpperCamelCase__=None , ) -> List[str]: '''simple docstring''' if attention_mask is None: UpperCAmelCase = tf.cast(tf.math.not_equal(UpperCamelCase__ , config.pad_token_id ) , tf.inta ) if decoder_attention_mask is None: UpperCAmelCase = tf.concat( [ tf.ones(decoder_input_ids[:, :1].shape , dtype=tf.inta ), tf.cast(tf.math.not_equal(decoder_input_ids[:, 1:] , config.pad_token_id ) , tf.inta ), ] , axis=-1 , ) if head_mask is None: UpperCAmelCase = tf.ones((config.encoder_layers, config.encoder_attention_heads) ) if decoder_head_mask is None: UpperCAmelCase = tf.ones((config.decoder_layers, config.decoder_attention_heads) ) if cross_attn_head_mask is None: UpperCAmelCase = tf.ones((config.decoder_layers, config.decoder_attention_heads) ) return { "input_ids": input_ids, "decoder_input_ids": decoder_input_ids, "attention_mask": attention_mask, "decoder_attention_mask": decoder_attention_mask, "head_mask": head_mask, "decoder_head_mask": decoder_head_mask, "cross_attn_head_mask": cross_attn_head_mask, } @require_tf class A_ (a_ , a_ , unittest.TestCase ): UpperCAmelCase__ = (TFMBartForConditionalGeneration, TFMBartModel) if is_tf_available() else () UpperCAmelCase__ = (TFMBartForConditionalGeneration,) if is_tf_available() else () UpperCAmelCase__ = ( { '''conversational''': TFMBartForConditionalGeneration, '''feature-extraction''': TFMBartModel, '''summarization''': TFMBartForConditionalGeneration, '''text2text-generation''': TFMBartForConditionalGeneration, '''translation''': TFMBartForConditionalGeneration, } if is_tf_available() else {} ) UpperCAmelCase__ = True UpperCAmelCase__ = False UpperCAmelCase__ = False def _lowercase ( self , _A , _A , _A , _A , _A ): '''simple docstring''' if pipeline_test_casse_name != "FeatureExtractionPipelineTests": # Exception encountered when calling layer '...' return True return False def _lowercase ( self ): '''simple docstring''' UpperCAmelCase = TFMBartModelTester(self ) UpperCAmelCase = ConfigTester(self , config_class=_A ) def _lowercase ( self ): '''simple docstring''' self.config_tester.run_common_tests() def _lowercase ( self ): '''simple docstring''' UpperCAmelCase = self.model_tester.prepare_config_and_inputs_for_common() self.model_tester.check_decoder_model_past_large_inputs(*_A ) @require_sentencepiece @require_tokenizers @require_tf class A_ (unittest.TestCase ): UpperCAmelCase__ = [ ''' UN Chief Says There Is No Military Solution in Syria''', ] UpperCAmelCase__ = [ '''Şeful ONU declară că nu există o soluţie militară în Siria''', ] UpperCAmelCase__ = '''facebook/mbart-large-en-ro''' @cached_property def _lowercase ( self ): '''simple docstring''' return AutoTokenizer.from_pretrained(self.model_name ) @cached_property def _lowercase ( self ): '''simple docstring''' UpperCAmelCase = TFAutoModelForSeqaSeqLM.from_pretrained(self.model_name ) return model def _lowercase ( self , **_A ): '''simple docstring''' UpperCAmelCase = self.translate_src_text(**_A ) self.assertListEqual(self.expected_text , _A ) def _lowercase ( self , **_A ): '''simple docstring''' UpperCAmelCase = self.tokenizer(self.src_text , **_A , return_tensors='''tf''' ) UpperCAmelCase = self.model.generate( model_inputs.input_ids , attention_mask=model_inputs.attention_mask , num_beams=2 ) UpperCAmelCase = self.tokenizer.batch_decode(_A , skip_special_tokens=_A ) return generated_words @slow def _lowercase ( self ): '''simple docstring''' self._assert_generated_batch_equal_expected()
273
0
import argparse from copy import deepcopy import numpy as np from datasets import ClassLabel, DatasetDict, load_dataset from evaluate import load from transformers import ( AutoModelForSequenceClassification, AutoTokenizer, DataCollatorWithPadding, Trainer, TrainerCallback, TrainingArguments, set_seed, ) def __magic_name__ ( ): '''simple docstring''' UpperCamelCase__ = argparse.ArgumentParser() parser.add_argument("""--model_ckpt""" , type=UpperCamelCase__ , default="""microsoft/unixcoder-base-nine""" ) parser.add_argument("""--num_epochs""" , type=UpperCamelCase__ , default=5 ) parser.add_argument("""--batch_size""" , type=UpperCamelCase__ , default=6 ) parser.add_argument("""--gradient_accumulation_steps""" , type=UpperCamelCase__ , default=1 ) parser.add_argument("""--freeze""" , type=UpperCamelCase__ , default=UpperCamelCase__ ) parser.add_argument("""--learning_rate""" , type=UpperCamelCase__ , default=5E-4 ) parser.add_argument("""--seed""" , type=UpperCamelCase__ , default=0 ) parser.add_argument("""--lr_scheduler_type""" , type=UpperCamelCase__ , default="""cosine""" ) parser.add_argument("""--num_warmup_steps""" , type=UpperCamelCase__ , default=10 ) parser.add_argument("""--weight_decay""" , type=UpperCamelCase__ , default=0.01 ) parser.add_argument("""--output_dir""" , type=UpperCamelCase__ , default="""./results""" ) return parser.parse_args() lowerCamelCase_ = load('''accuracy''') def __magic_name__ ( __a : Dict ): '''simple docstring''' UpperCamelCase__ , UpperCamelCase__ = eval_pred UpperCamelCase__ = np.argmax(UpperCamelCase__ , axis=1 ) return metric.compute(predictions=UpperCamelCase__ , references=UpperCamelCase__ ) class __A( a_ ): """simple docstring""" def __init__(self , SCREAMING_SNAKE_CASE_ ): super().__init__() UpperCamelCase__ = trainer def UpperCAmelCase_ (self , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , **SCREAMING_SNAKE_CASE_ ): if control.should_evaluate: UpperCamelCase__ = deepcopy(_A ) self._trainer.evaluate(eval_dataset=self._trainer.train_dataset , metric_key_prefix="""train""" ) return control_copy def __magic_name__ ( ): '''simple docstring''' UpperCamelCase__ = get_args() set_seed(args.seed ) UpperCamelCase__ = load_dataset("""codeparrot/codecomplex""" , split="""train""" ) UpperCamelCase__ = dataset.train_test_split(test_size=0.2 ) UpperCamelCase__ = train_test["""test"""].train_test_split(test_size=0.5 ) UpperCamelCase__ = DatasetDict( { """train""": train_test["""train"""], """test""": test_validation["""train"""], """valid""": test_validation["""test"""], } ) print("""Loading tokenizer and model""" ) UpperCamelCase__ = AutoTokenizer.from_pretrained(args.model_ckpt ) UpperCamelCase__ = tokenizer.eos_token UpperCamelCase__ = AutoModelForSequenceClassification.from_pretrained(args.model_ckpt , num_labels=7 ) UpperCamelCase__ = model.config.eos_token_id if args.freeze: for param in model.roberta.parameters(): UpperCamelCase__ = False UpperCamelCase__ = ClassLabel(num_classes=7 , names=list(set(train_test_validation["""train"""]["""complexity"""] ) ) ) def tokenize(__a : Any ): UpperCamelCase__ = tokenizer(example["""src"""] , truncation=UpperCamelCase__ , max_length=1_024 ) UpperCamelCase__ = labels.straint(example["""complexity"""] ) return { "input_ids": inputs["input_ids"], "attention_mask": inputs["attention_mask"], "label": label, } UpperCamelCase__ = train_test_validation.map( UpperCamelCase__ , batched=UpperCamelCase__ , remove_columns=train_test_validation["""train"""].column_names , ) UpperCamelCase__ = DataCollatorWithPadding(tokenizer=UpperCamelCase__ ) UpperCamelCase__ = TrainingArguments( output_dir=args.output_dir , learning_rate=args.learning_rate , lr_scheduler_type=args.lr_scheduler_type , evaluation_strategy="""epoch""" , save_strategy="""epoch""" , logging_strategy="""epoch""" , per_device_train_batch_size=args.batch_size , per_device_eval_batch_size=args.batch_size , num_train_epochs=args.num_epochs , gradient_accumulation_steps=args.gradient_accumulation_steps , weight_decay=0.01 , metric_for_best_model="""accuracy""" , run_name="""complexity-java""" , report_to="""wandb""" , ) UpperCamelCase__ = Trainer( model=UpperCamelCase__ , args=UpperCamelCase__ , train_dataset=tokenized_datasets["""train"""] , eval_dataset=tokenized_datasets["""valid"""] , tokenizer=UpperCamelCase__ , data_collator=UpperCamelCase__ , compute_metrics=UpperCamelCase__ , ) print("""Training...""" ) trainer.add_callback(CustomCallback(UpperCamelCase__ ) ) trainer.train() if __name__ == "__main__": main()
244
import gc import unittest from transformers import CTRLConfig, 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 ( CTRL_PRETRAINED_MODEL_ARCHIVE_LIST, CTRLForSequenceClassification, CTRLLMHeadModel, CTRLModel, ) class A_ : def __init__( self , _A , _A=1_4 , _A=7 , _A=True , _A=True , _A=True , _A=True , _A=True , _A=9_9 , _A=3_2 , _A=5 , _A=4 , _A=3_7 , _A="gelu" , _A=0.1 , _A=0.1 , _A=5_1_2 , _A=1_6 , _A=2 , _A=0.02 , _A=3 , _A=4 , _A=None , ): '''simple docstring''' UpperCAmelCase = parent UpperCAmelCase = batch_size UpperCAmelCase = seq_length UpperCAmelCase = is_training UpperCAmelCase = use_token_type_ids UpperCAmelCase = use_input_mask UpperCAmelCase = use_labels UpperCAmelCase = use_mc_token_ids UpperCAmelCase = vocab_size UpperCAmelCase = hidden_size UpperCAmelCase = num_hidden_layers UpperCAmelCase = num_attention_heads UpperCAmelCase = intermediate_size UpperCAmelCase = hidden_act UpperCAmelCase = hidden_dropout_prob UpperCAmelCase = attention_probs_dropout_prob UpperCAmelCase = max_position_embeddings UpperCAmelCase = type_vocab_size UpperCAmelCase = type_sequence_label_size UpperCAmelCase = initializer_range UpperCAmelCase = num_labels UpperCAmelCase = num_choices UpperCAmelCase = scope UpperCAmelCase = self.vocab_size - 1 def _lowercase ( self ): '''simple docstring''' UpperCAmelCase = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) UpperCAmelCase = None if self.use_input_mask: UpperCAmelCase = random_attention_mask([self.batch_size, self.seq_length] ) UpperCAmelCase = None if self.use_token_type_ids: UpperCAmelCase = ids_tensor([self.batch_size, self.seq_length] , self.type_vocab_size ) UpperCAmelCase = None if self.use_mc_token_ids: UpperCAmelCase = ids_tensor([self.batch_size, self.num_choices] , self.seq_length ) UpperCAmelCase = None UpperCAmelCase = None UpperCAmelCase = None if self.use_labels: UpperCAmelCase = ids_tensor([self.batch_size] , self.type_sequence_label_size ) UpperCAmelCase = ids_tensor([self.batch_size, self.seq_length] , self.num_labels ) UpperCAmelCase = ids_tensor([self.batch_size] , self.num_choices ) UpperCAmelCase = self.get_config() UpperCAmelCase = ids_tensor([self.num_hidden_layers, self.num_attention_heads] , 2 ) return ( config, input_ids, input_mask, head_mask, token_type_ids, mc_token_ids, sequence_labels, token_labels, choice_labels, ) def _lowercase ( self ): '''simple docstring''' return CTRLConfig( vocab_size=self.vocab_size , n_embd=self.hidden_size , n_layer=self.num_hidden_layers , n_head=self.num_attention_heads , n_positions=self.max_position_embeddings , pad_token_id=self.pad_token_id , ) def _lowercase ( self , _A , _A , _A , _A , _A , *_A ): '''simple docstring''' UpperCAmelCase = CTRLModel(config=_A ) model.to(_A ) model.eval() model(_A , token_type_ids=_A , head_mask=_A ) model(_A , token_type_ids=_A ) UpperCAmelCase = model(_A ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) self.parent.assertEqual(len(result.past_key_values ) , config.n_layer ) def _lowercase ( self , _A , _A , _A , _A , _A , *_A ): '''simple docstring''' UpperCAmelCase = CTRLLMHeadModel(_A ) model.to(_A ) model.eval() UpperCAmelCase = model(_A , token_type_ids=_A , labels=_A ) self.parent.assertEqual(result.loss.shape , () ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.vocab_size) ) def _lowercase ( self ): '''simple docstring''' UpperCAmelCase = self.prepare_config_and_inputs() ( ( UpperCAmelCase ) , ( UpperCAmelCase ) , ( UpperCAmelCase ) , ( UpperCAmelCase ) , ( UpperCAmelCase ) , ( UpperCAmelCase ) , ( UpperCAmelCase ) , ( UpperCAmelCase ) , ( UpperCAmelCase ) , ) = config_and_inputs UpperCAmelCase = {'''input_ids''': input_ids, '''token_type_ids''': token_type_ids, '''head_mask''': head_mask} return config, inputs_dict def _lowercase ( self , _A , _A , _A , _A , *_A ): '''simple docstring''' UpperCAmelCase = self.num_labels UpperCAmelCase = CTRLForSequenceClassification(_A ) model.to(_A ) model.eval() UpperCAmelCase = ids_tensor([self.batch_size] , self.type_sequence_label_size ) UpperCAmelCase = model(_A , token_type_ids=_A , labels=_A ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_labels) ) @require_torch class A_ (a_ , a_ , a_ , unittest.TestCase ): UpperCAmelCase__ = (CTRLModel, CTRLLMHeadModel, CTRLForSequenceClassification) if is_torch_available() else () UpperCAmelCase__ = (CTRLLMHeadModel,) if is_torch_available() else () UpperCAmelCase__ = ( { '''feature-extraction''': CTRLModel, '''text-classification''': CTRLForSequenceClassification, '''text-generation''': CTRLLMHeadModel, '''zero-shot''': CTRLForSequenceClassification, } if is_torch_available() else {} ) UpperCAmelCase__ = True UpperCAmelCase__ = False UpperCAmelCase__ = False def _lowercase ( self , _A , _A , _A , _A , _A ): '''simple docstring''' if pipeline_test_casse_name == "ZeroShotClassificationPipelineTests": # Get `tokenizer does not have a padding token` error for both fast/slow tokenizers. # `CTRLConfig` was never used in pipeline tests, either because of a missing checkpoint or because a tiny # config could not be created. return True return False def _lowercase ( self ): '''simple docstring''' UpperCAmelCase = CTRLModelTester(self ) UpperCAmelCase = ConfigTester(self , config_class=_A , n_embd=3_7 ) def _lowercase ( self ): '''simple docstring''' super().tearDown() # clean-up as much as possible GPU memory occupied by PyTorch gc.collect() torch.cuda.empty_cache() def _lowercase ( self ): '''simple docstring''' self.config_tester.run_common_tests() def _lowercase ( self ): '''simple docstring''' UpperCAmelCase = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_ctrl_model(*_A ) def _lowercase ( self ): '''simple docstring''' UpperCAmelCase = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_lm_head_model(*_A ) @unittest.skip('''Will be fixed soon by reducing the size of the model used for common tests.''' ) def _lowercase ( self ): '''simple docstring''' pass @slow def _lowercase ( self ): '''simple docstring''' for model_name in CTRL_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: UpperCAmelCase = CTRLModel.from_pretrained(_A ) self.assertIsNotNone(_A ) @unittest.skip('''The model doesn\'t support left padding''' ) # and it's not used enough to be worth fixing :) def _lowercase ( self ): '''simple docstring''' pass @require_torch class A_ (unittest.TestCase ): def _lowercase ( self ): '''simple docstring''' super().tearDown() # clean-up as much as possible GPU memory occupied by PyTorch gc.collect() torch.cuda.empty_cache() @slow def _lowercase ( self ): '''simple docstring''' UpperCAmelCase = CTRLLMHeadModel.from_pretrained('''ctrl''' ) model.to(_A ) UpperCAmelCase = torch.tensor( [[1_1_8_5_9, 0, 1_6_1_1, 8]] , dtype=torch.long , device=_A ) # Legal the president is UpperCAmelCase = [ 1_1_8_5_9, 0, 1_6_1_1, 8, 5, 1_5_0, 2_6_4_4_9, 2, 1_9, 3_4_8, 4_6_9, 3, 2_5_9_5, 4_8, 2_0_7_4_0, 2_4_6_5_3_3, 2_4_6_5_3_3, 1_9, 3_0, 5, ] # Legal the president is a good guy and I don't want to lose my job. \n \n I have a UpperCAmelCase = model.generate(_A , do_sample=_A ) self.assertListEqual(output_ids[0].tolist() , _A )
273
0
from ... import PretrainedConfig snake_case : str = { "sijunhe/nezha-cn-base": "https://huggingface.co/sijunhe/nezha-cn-base/resolve/main/config.json", } class snake_case_ (a_ ): UpperCAmelCase__ : List[str] = NEZHA_PRETRAINED_CONFIG_ARCHIVE_MAP UpperCAmelCase__ : Dict = '''nezha''' def __init__( self :Tuple ,__snake_case :int=2_11_28 ,__snake_case :Union[str, Any]=7_68 ,__snake_case :Union[str, Any]=12 ,__snake_case :int=12 ,__snake_case :int=30_72 ,__snake_case :Dict="gelu" ,__snake_case :Union[str, Any]=0.1 ,__snake_case :Any=0.1 ,__snake_case :str=5_12 ,__snake_case :Any=64 ,__snake_case :Union[str, Any]=2 ,__snake_case :List[str]=0.02 ,__snake_case :str=1E-12 ,__snake_case :Dict=0.1 ,__snake_case :str=0 ,__snake_case :str=2 ,__snake_case :List[Any]=3 ,__snake_case :Any=True ,**__snake_case :Optional[int] ,) -> List[Any]: super().__init__(pad_token_id=_A ,bos_token_id=_A ,eos_token_id=_A ,**_A ) a__ = vocab_size a__ = hidden_size a__ = num_hidden_layers a__ = num_attention_heads a__ = hidden_act a__ = intermediate_size a__ = hidden_dropout_prob a__ = attention_probs_dropout_prob a__ = max_position_embeddings a__ = max_relative_position a__ = type_vocab_size a__ = initializer_range a__ = layer_norm_eps a__ = classifier_dropout a__ = use_cache
240
import cva import numpy as np class A_ : def __init__( self , _A , _A ): '''simple docstring''' if k in (0.04, 0.06): UpperCAmelCase = k UpperCAmelCase = window_size else: raise ValueError('''invalid k value''' ) def __str__( self ): '''simple docstring''' return str(self.k ) def _lowercase ( self , _A ): '''simple docstring''' UpperCAmelCase = cva.imread(_A , 0 ) UpperCAmelCase , UpperCAmelCase = img.shape UpperCAmelCase = [] UpperCAmelCase = img.copy() UpperCAmelCase = cva.cvtColor(_A , cva.COLOR_GRAY2RGB ) UpperCAmelCase , UpperCAmelCase = np.gradient(_A ) UpperCAmelCase = dx**2 UpperCAmelCase = dy**2 UpperCAmelCase = dx * dy UpperCAmelCase = 0.04 UpperCAmelCase = self.window_size // 2 for y in range(_A , h - offset ): for x in range(_A , w - offset ): UpperCAmelCase = ixx[ y - offset : y + offset + 1, x - offset : x + offset + 1 ].sum() UpperCAmelCase = iyy[ y - offset : y + offset + 1, x - offset : x + offset + 1 ].sum() UpperCAmelCase = ixy[ y - offset : y + offset + 1, x - offset : x + offset + 1 ].sum() UpperCAmelCase = (wxx * wyy) - (wxy**2) UpperCAmelCase = wxx + wyy UpperCAmelCase = det - k * (trace**2) # Can change the value if r > 0.5: corner_list.append([x, y, r] ) color_img.itemset((y, x, 0) , 0 ) color_img.itemset((y, x, 1) , 0 ) color_img.itemset((y, x, 2) , 2_5_5 ) return color_img, corner_list if __name__ == "__main__": __A : Tuple = HarrisCorner(0.04, 3) __A , __A : List[Any] = edge_detect.detect("path_to_image") cva.imwrite("detect.png", color_img)
273
0
'''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_5_0, "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_0_0, "eval_accuracy": 0.3, "eval_loss": 0.9}, }, ] ) class A ( unittest.TestCase ): '''simple docstring''' def a_ (self ) -> Optional[Any]: 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=_A , ) assert hasattr(self , "env" ) def a_ (self , _UpperCAmelCase=1 ) -> Dict: 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=_A , instance_type=self.instance_type , debugger_hook_config=_A , hyperparameters={**self.env.hyperparameters, "model_name_or_path": self.model_name_or_path} , metric_definitions=self.env.metric_definitions , py_version="py36" , ) def a_ (self , _UpperCAmelCase ) -> List[Any]: TrainingJobAnalytics(_A ).export_csv(f"{self.env.test_path}/{job_name}_metrics.csv" ) def a_ (self ) -> List[str]: __UpperCamelCase : Union[str, Any] = self.create_estimator() # run training estimator.fit() # result dataframe __UpperCamelCase : Optional[int] = TrainingJobAnalytics(estimator.latest_training_job.name ).dataframe() # extract kpis __UpperCamelCase : Union[str, Any] = list(result_metrics_df[result_metrics_df.metric_name == "eval_accuracy"]["value"] ) __UpperCamelCase : Optional[int] = list(result_metrics_df[result_metrics_df.metric_name == "eval_loss"]["value"] ) # get train time from SageMaker job, this includes starting, preprocessing, stopping __UpperCamelCase : Tuple = ( Session().describe_training_job(estimator.latest_training_job.name ).get("TrainingTimeInSeconds" , 9_9_9_9_9_9 ) ) # 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} , _A )
298
from datetime import datetime import requests def __SCREAMING_SNAKE_CASE ( UpperCamelCase__ ) -> bytes: '''simple docstring''' UpperCAmelCase = '''https://downloadgram.net/wp-json/wppress/video-downloader/video?url=''' UpperCAmelCase = requests.get(base_url + url ).json()[0]['''urls'''][0]['''src'''] return requests.get(UpperCamelCase__ ).content if __name__ == "__main__": __A : Union[str, Any] = input("Enter Video/IGTV url: ").strip() __A : Tuple = F'{datetime.now():%Y-%m-%d_%H:%M:%S}.mp4' with open(file_name, "wb") as fp: fp.write(download_video(url)) print(F'Done. Video saved to disk as {file_name}.')
273
0
def _A ( _lowercase ) -> str: """simple docstring""" __UpperCamelCase = 0 # if input_string is "aba" than new_input_string become "a|b|a" __UpperCamelCase = '' __UpperCamelCase = '' # append each character + "|" in new_string for range(0, length-1) for i in input_string[: len(UpperCamelCase__ ) - 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 __UpperCamelCase, __UpperCamelCase = 0, 0 # length[i] shows the length of palindromic substring with center i __UpperCamelCase = [1 for i in range(len(UpperCamelCase__ ) )] # for each character in new_string find corresponding palindromic string __UpperCamelCase = 0 for j in range(len(UpperCamelCase__ ) ): __UpperCamelCase = 1 if j > r else min(length[l + r - j] // 2 , r - j + 1 ) while ( j - k >= 0 and j + k < len(UpperCamelCase__ ) and new_input_string[k + j] == new_input_string[j - k] ): k += 1 __UpperCamelCase = 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: __UpperCamelCase = j - k + 1 # noqa: E741 __UpperCamelCase = j + k - 1 # update max_length and start position if max_length < length[j]: __UpperCamelCase = length[j] __UpperCamelCase = j # create that string __UpperCamelCase = 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()
310
from __future__ import annotations from collections.abc import Callable def __SCREAMING_SNAKE_CASE ( UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ = 100 , ) -> float: '''simple docstring''' UpperCAmelCase = x_start UpperCAmelCase = fnc(UpperCamelCase__ ) UpperCAmelCase = 0.0 for _ in range(UpperCamelCase__ ): # Approximates small segments of curve as linear and solve # for trapezoidal area UpperCAmelCase = (x_end - x_start) / steps + xa UpperCAmelCase = fnc(UpperCamelCase__ ) area += abs(fxa + fxa ) * (xa - xa) / 2 # Increment step UpperCAmelCase = xa UpperCAmelCase = fxa return area if __name__ == "__main__": def __SCREAMING_SNAKE_CASE ( UpperCamelCase__ ) -> str: '''simple docstring''' return x**3 + x**2 print("f(x) = x^3 + x^2") print("The area between the curve, x = -5, x = 5 and the x axis is:") __A : List[Any] = 10 while i <= 100_000: print(F'with {i} steps: {trapezoidal_area(f, -5, 5, i)}') i *= 10
273
0
import argparse import os import numpy as np import tensorflow as tf import torch from transformers import BertModel def lowerCamelCase_ ( _a , _a , _a ): """simple docstring""" lowerCAmelCase__ : Union[str, Any] = ('''dense.weight''', '''attention.self.query''', '''attention.self.key''', '''attention.self.value''') lowerCAmelCase__ : Union[str, Any] = ( ('''layer.''', '''layer_'''), ('''word_embeddings.weight''', '''word_embeddings'''), ('''position_embeddings.weight''', '''position_embeddings'''), ('''token_type_embeddings.weight''', '''token_type_embeddings'''), ('''.''', '''/'''), ('''LayerNorm/weight''', '''LayerNorm/gamma'''), ('''LayerNorm/bias''', '''LayerNorm/beta'''), ('''weight''', '''kernel'''), ) if not os.path.isdir(UpperCamelCase__ ): os.makedirs(UpperCamelCase__ ) lowerCAmelCase__ : int = model.state_dict() def to_tf_var_name(_a ): for patt, repl in iter(UpperCamelCase__ ): lowerCAmelCase__ : Optional[int] = name.replace(UpperCamelCase__ , UpperCamelCase__ ) return f'bert/{name}' def create_tf_var(_a , _a , _a ): lowerCAmelCase__ : str = tf.dtypes.as_dtype(tensor.dtype ) lowerCAmelCase__ : Optional[Any] = tf.get_variable(dtype=UpperCamelCase__ , shape=tensor.shape , name=UpperCamelCase__ , initializer=tf.zeros_initializer() ) session.run(tf.variables_initializer([tf_var] ) ) session.run(UpperCamelCase__ ) return tf_var tf.reset_default_graph() with tf.Session() as session: for var_name in state_dict: lowerCAmelCase__ : Optional[Any] = to_tf_var_name(UpperCamelCase__ ) lowerCAmelCase__ : List[str] = state_dict[var_name].numpy() if any(x in var_name for x in tensors_to_transpose ): lowerCAmelCase__ : Dict = torch_tensor.T lowerCAmelCase__ : int = create_tf_var(tensor=UpperCamelCase__ , name=UpperCamelCase__ , session=UpperCamelCase__ ) tf.keras.backend.set_value(UpperCamelCase__ , UpperCamelCase__ ) lowerCAmelCase__ : Any = session.run(UpperCamelCase__ ) print(f'Successfully created {tf_name}: {np.allclose(UpperCamelCase__ , UpperCamelCase__ )}' ) lowerCAmelCase__ : Optional[int] = tf.train.Saver(tf.trainable_variables() ) saver.save(UpperCamelCase__ , os.path.join(UpperCamelCase__ , model_name.replace('''-''' , '''_''' ) + '''.ckpt''' ) ) def lowerCamelCase_ ( _a=None ): """simple docstring""" lowerCAmelCase__ : int = argparse.ArgumentParser() parser.add_argument('''--model_name''' , type=UpperCamelCase__ , required=UpperCamelCase__ , help='''model name e.g. bert-base-uncased''' ) parser.add_argument( '''--cache_dir''' , type=UpperCamelCase__ , default=UpperCamelCase__ , required=UpperCamelCase__ , help='''Directory containing pytorch model''' ) parser.add_argument('''--pytorch_model_path''' , type=UpperCamelCase__ , required=UpperCamelCase__ , help='''/path/to/<pytorch-model-name>.bin''' ) parser.add_argument('''--tf_cache_dir''' , type=UpperCamelCase__ , required=UpperCamelCase__ , help='''Directory in which to save tensorflow model''' ) lowerCAmelCase__ : Optional[int] = parser.parse_args(UpperCamelCase__ ) lowerCAmelCase__ : str = BertModel.from_pretrained( pretrained_model_name_or_path=args.model_name , state_dict=torch.load(args.pytorch_model_path ) , cache_dir=args.cache_dir , ) convert_pytorch_checkpoint_to_tf(model=UpperCamelCase__ , ckpt_dir=args.tf_cache_dir , model_name=args.model_name ) if __name__ == "__main__": main()
131
import json from typing import List, Optional, Tuple from tokenizers import normalizers from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import logging from .tokenization_squeezebert import SqueezeBertTokenizer __A : Dict = logging.get_logger(__name__) __A : Any = {"vocab_file": "vocab.txt", "tokenizer_file": "tokenizer.json"} __A : Tuple = { "vocab_file": { "squeezebert/squeezebert-uncased": ( "https://huggingface.co/squeezebert/squeezebert-uncased/resolve/main/vocab.txt" ), "squeezebert/squeezebert-mnli": "https://huggingface.co/squeezebert/squeezebert-mnli/resolve/main/vocab.txt", "squeezebert/squeezebert-mnli-headless": ( "https://huggingface.co/squeezebert/squeezebert-mnli-headless/resolve/main/vocab.txt" ), }, "tokenizer_file": { "squeezebert/squeezebert-uncased": ( "https://huggingface.co/squeezebert/squeezebert-uncased/resolve/main/tokenizer.json" ), "squeezebert/squeezebert-mnli": ( "https://huggingface.co/squeezebert/squeezebert-mnli/resolve/main/tokenizer.json" ), "squeezebert/squeezebert-mnli-headless": ( "https://huggingface.co/squeezebert/squeezebert-mnli-headless/resolve/main/tokenizer.json" ), }, } __A : List[Any] = { "squeezebert/squeezebert-uncased": 512, "squeezebert/squeezebert-mnli": 512, "squeezebert/squeezebert-mnli-headless": 512, } __A : List[Any] = { "squeezebert/squeezebert-uncased": {"do_lower_case": True}, "squeezebert/squeezebert-mnli": {"do_lower_case": True}, "squeezebert/squeezebert-mnli-headless": {"do_lower_case": True}, } class A_ (a_ ): UpperCAmelCase__ = VOCAB_FILES_NAMES UpperCAmelCase__ = PRETRAINED_VOCAB_FILES_MAP UpperCAmelCase__ = PRETRAINED_INIT_CONFIGURATION UpperCAmelCase__ = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES UpperCAmelCase__ = SqueezeBertTokenizer def __init__( self , _A=None , _A=None , _A=True , _A="[UNK]" , _A="[SEP]" , _A="[PAD]" , _A="[CLS]" , _A="[MASK]" , _A=True , _A=None , **_A , ): '''simple docstring''' super().__init__( _A , tokenizer_file=_A , do_lower_case=_A , unk_token=_A , sep_token=_A , pad_token=_A , cls_token=_A , mask_token=_A , tokenize_chinese_chars=_A , strip_accents=_A , **_A , ) UpperCAmelCase = json.loads(self.backend_tokenizer.normalizer.__getstate__() ) if ( normalizer_state.get('''lowercase''' , _A ) != do_lower_case or normalizer_state.get('''strip_accents''' , _A ) != strip_accents or normalizer_state.get('''handle_chinese_chars''' , _A ) != tokenize_chinese_chars ): UpperCAmelCase = getattr(_A , normalizer_state.pop('''type''' ) ) UpperCAmelCase = do_lower_case UpperCAmelCase = strip_accents UpperCAmelCase = tokenize_chinese_chars UpperCAmelCase = normalizer_class(**_A ) UpperCAmelCase = do_lower_case def _lowercase ( self , _A , _A=None ): '''simple docstring''' UpperCAmelCase = [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 _lowercase ( self , _A , _A = None ): '''simple docstring''' UpperCAmelCase = [self.sep_token_id] UpperCAmelCase = [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 _lowercase ( self , _A , _A = None ): '''simple docstring''' UpperCAmelCase = self._tokenizer.model.save(_A , name=_A ) return tuple(_A )
273
0
import argparse from typing import List import evaluate import numpy as np import torch from datasets import DatasetDict, load_dataset # New Code # # We'll be using StratifiedKFold for this example from sklearn.model_selection import StratifiedKFold from torch.optim import AdamW from torch.utils.data import DataLoader from transformers import AutoModelForSequenceClassification, AutoTokenizer, get_linear_schedule_with_warmup, set_seed from accelerate import Accelerator, DistributedType ######################################################################## # This is a fully working simple example to use Accelerate, # specifically showcasing how to perform Cross Validation, # and builds off the `nlp_example.py` script. # # This example trains a Bert base model on GLUE MRPC # in any of the following settings (with the same script): # - single CPU or single GPU # - multi GPUS (using PyTorch distributed mode) # - (multi) TPUs # - fp16 (mixed-precision) or fp32 (normal precision) # # To help focus on the differences in the code, building `DataLoaders` # was refactored into its own function. # New additions from the base script can be found quickly by # looking for the # New Code # tags # # To run it in each of these various modes, follow the instructions # in the readme for examples: # https://github.com/huggingface/accelerate/tree/main/examples # ######################################################################## _UpperCamelCase = 16 _UpperCamelCase = 32 def lowerCAmelCase__( lowercase : Tuple , lowercase : int , lowercase : Optional[Any] , lowercase : Optional[int] , lowercase : Tuple = 16 ) -> Tuple: __snake_case : List[Any] = AutoTokenizer.from_pretrained("bert-base-cased" ) __snake_case : List[str] = DatasetDict( { "train": dataset["train"].select(UpperCamelCase__ ), "validation": dataset["train"].select(UpperCamelCase__ ), "test": dataset["validation"], } ) def tokenize_function(lowercase : List[str] ): # max_length=None => use the model max length (it's actually the default) __snake_case : Dict = 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(): __snake_case : List[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 __snake_case : List[Any] = tokenized_datasets.rename_column("label" , "labels" ) def collate_fn(lowercase : List[Any] ): # On TPU it's best to pad everything to the same length or training will be very slow. __snake_case : List[Any] = 128 if accelerator.distributed_type == DistributedType.TPU else None # When using mixed precision we want round multiples of 8/16 if accelerator.mixed_precision == "fp8": __snake_case : List[Any] = 16 elif accelerator.mixed_precision != "no": __snake_case : str = 8 else: __snake_case : Dict = None return tokenizer.pad( UpperCamelCase__ , padding="longest" , max_length=UpperCamelCase__ , pad_to_multiple_of=UpperCamelCase__ , return_tensors="pt" , ) # Instantiate dataloaders. __snake_case : List[Any] = DataLoader( tokenized_datasets["train"] , shuffle=UpperCamelCase__ , collate_fn=UpperCamelCase__ , batch_size=UpperCamelCase__ ) __snake_case : List[str] = DataLoader( tokenized_datasets["validation"] , shuffle=UpperCamelCase__ , collate_fn=UpperCamelCase__ , batch_size=UpperCamelCase__ ) __snake_case : Tuple = DataLoader( tokenized_datasets["test"] , shuffle=UpperCamelCase__ , collate_fn=UpperCamelCase__ , batch_size=UpperCamelCase__ ) return train_dataloader, eval_dataloader, test_dataloader def lowerCAmelCase__( lowercase : Optional[Any] , lowercase : Dict ) -> Optional[Any]: __snake_case : Dict = [] # Download the dataset __snake_case : Optional[int] = load_dataset("glue" , "mrpc" ) # Create our splits __snake_case : Union[str, Any] = StratifiedKFold(n_splits=int(args.num_folds ) ) # Initialize accelerator __snake_case : Dict = Accelerator(cpu=args.cpu , mixed_precision=args.mixed_precision ) # Sample hyper-parameters for learning rate, batch size, seed and a few other HPs __snake_case : Optional[int] = config["lr"] __snake_case : Union[str, Any] = int(config["num_epochs"] ) __snake_case : Union[str, Any] = int(config["seed"] ) __snake_case : List[str] = int(config["batch_size"] ) __snake_case : List[str] = evaluate.load("glue" , "mrpc" ) # If the batch size is too big we use gradient accumulation __snake_case : Union[str, Any] = 1 if batch_size > MAX_GPU_BATCH_SIZE and accelerator.distributed_type != DistributedType.TPU: __snake_case : Optional[int] = batch_size // MAX_GPU_BATCH_SIZE __snake_case : Tuple = MAX_GPU_BATCH_SIZE set_seed(UpperCamelCase__ ) # New Code # # Create our folds: __snake_case : List[str] = kfold.split(np.zeros(datasets["train"].num_rows ) , datasets["train"]["label"] ) __snake_case : Union[str, Any] = [] # Iterate over them for i, (train_idxs, valid_idxs) in enumerate(UpperCamelCase__ ): __snake_case , __snake_case , __snake_case : Dict = get_fold_dataloaders( UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , ) # Instantiate the model (we build the model here so that the seed also control new weights initialization) __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). __snake_case : Union[str, Any] = model.to(accelerator.device ) # Instantiate optimizer __snake_case : int = AdamW(params=model.parameters() , lr=UpperCamelCase__ ) # Instantiate scheduler __snake_case : Union[str, Any] = get_linear_schedule_with_warmup( optimizer=UpperCamelCase__ , num_warmup_steps=100 , num_training_steps=(len(UpperCamelCase__ ) * num_epochs) // gradient_accumulation_steps , ) # Prepare everything # There is no specific order to remember, we just need to unpack the objects in the same order we gave them to the # prepare method. __snake_case , __snake_case , __snake_case , __snake_case , __snake_case : Tuple = accelerator.prepare( UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ ) # Now we train the model for epoch in range(UpperCamelCase__ ): model.train() for step, batch in enumerate(UpperCamelCase__ ): # We could avoid this line since we set the accelerator with `device_placement=True`. batch.to(accelerator.device ) __snake_case : Optional[int] = model(**UpperCamelCase__ ) __snake_case : List[Any] = outputs.loss __snake_case : Tuple = loss / gradient_accumulation_steps accelerator.backward(UpperCamelCase__ ) if step % gradient_accumulation_steps == 0: optimizer.step() lr_scheduler.step() optimizer.zero_grad() 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(): __snake_case : List[str] = model(**UpperCamelCase__ ) __snake_case : Union[str, Any] = outputs.logits.argmax(dim=-1 ) __snake_case , __snake_case : List[str] = accelerator.gather_for_metrics((predictions, batch["labels"]) ) metric.add_batch( predictions=UpperCamelCase__ , references=UpperCamelCase__ , ) __snake_case : str = metric.compute() # Use accelerator.print to print only on the main process. accelerator.print(f"""epoch {epoch}:""" , UpperCamelCase__ ) # New Code # # We also run predictions on the test set at the very end __snake_case : Dict = [] 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(): __snake_case : Optional[int] = model(**UpperCamelCase__ ) __snake_case : Optional[Any] = outputs.logits __snake_case , __snake_case : Dict = accelerator.gather_for_metrics((predictions, batch["labels"]) ) fold_predictions.append(predictions.cpu() ) if i == 0: # We need all of the test predictions test_references.append(references.cpu() ) # Use accelerator.print to print only on the main process. test_predictions.append(torch.cat(UpperCamelCase__ , dim=0 ) ) # We now need to release all our memory and get rid of the current model, optimizer, etc accelerator.free_memory() # New Code # # Finally we check the accuracy of our folded results: __snake_case : List[Any] = torch.cat(UpperCamelCase__ , dim=0 ) __snake_case : Union[str, Any] = torch.stack(UpperCamelCase__ , dim=0 ).sum(dim=0 ).div(int(args.num_folds ) ).argmax(dim=-1 ) __snake_case : Union[str, Any] = metric.compute(predictions=UpperCamelCase__ , references=UpperCamelCase__ ) accelerator.print("Average test metrics from all folds:" , UpperCamelCase__ ) def lowerCAmelCase__( ) -> str: __snake_case : List[str] = 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." , ) parser.add_argument("--cpu" , action="store_true" , help="If passed, will train on the CPU." ) # New Code # parser.add_argument("--num_folds" , type=UpperCamelCase__ , default=3 , help="The number of splits to perform across the dataset" ) __snake_case : Dict = parser.parse_args() __snake_case : Optional[Any] = {"lr": 2E-5, "num_epochs": 3, "seed": 42, "batch_size": 16} training_function(UpperCamelCase__ , UpperCamelCase__ ) if __name__ == "__main__": main()
326
import argparse import re from flax.traverse_util import flatten_dict, unflatten_dict from tax import checkpoints from transformers import SwitchTransformersConfig, SwitchTransformersForConditionalGeneration from transformers.modeling_flax_pytorch_utils import load_flax_weights_in_pytorch_model from transformers.utils import logging logging.set_verbosity_info() # should not include what is already done by the `from_pt` argument __A : int = { "/attention/": "/0/SelfAttention/", "/self_attention/": "/0/SelfAttention/", "/encoder_decoder_attention/": "/1/EncDecAttention/", "value": "v", "query": "q", "key": "k", "out": "o", "pre_self_attention_layer_norm": "0/layer_norm", "pre_cross_attention_layer_norm": "1/layer_norm", "pre_attention_layer_norm": "0/layer_norm", # previously 1, but seems wrong "token_embedder": "shared", "encoder_norm": "final_layer_norm", "decoder_norm": "final_layer_norm", "relpos_bias/rel_embedding": "block/0/layer/0/SelfAttention/relative_attention_bias/weight", "router/router_weights/w/": "router/classifier/", "roer/roer_weights/w/": "router/classifier/", "logits_dense": "lm_head", } def __SCREAMING_SNAKE_CASE ( UpperCamelCase__ ) -> List[str]: '''simple docstring''' UpperCAmelCase = list(s_dict.keys() ) for key in keys: UpperCAmelCase = R'''.*/layers_(\d+)''' UpperCAmelCase = key if re.match(UpperCamelCase__ , UpperCamelCase__ ): UpperCAmelCase = re.sub(R'''layers_(\d+)''' , R'''block/\1/layer''' , UpperCamelCase__ ) UpperCAmelCase = R'''(encoder|decoder)\/''' if re.match(UpperCamelCase__ , UpperCamelCase__ ): UpperCAmelCase = re.match(UpperCamelCase__ , UpperCamelCase__ ).groups() if groups[0] == "encoder": UpperCAmelCase = re.sub(R'''/mlp/''' , R'''/1/mlp/''' , UpperCamelCase__ ) UpperCAmelCase = re.sub(R'''/pre_mlp_layer_norm/''' , R'''/1/layer_norm/''' , UpperCamelCase__ ) elif groups[0] == "decoder": UpperCAmelCase = re.sub(R'''/mlp/''' , R'''/2/mlp/''' , UpperCamelCase__ ) UpperCAmelCase = re.sub(R'''/pre_mlp_layer_norm/''' , R'''/2/layer_norm/''' , UpperCamelCase__ ) # 2. Convert other classic mappings for old_key, temp_key in MOE_LAYER_NAME_MAPPING.items(): if old_key in new_key: UpperCAmelCase = new_key.replace(UpperCamelCase__ , UpperCamelCase__ ) print(F"""{key} -> {new_key}""" ) UpperCAmelCase = s_dict.pop(UpperCamelCase__ ) if "encoder/block/0/layer/0/SelfAttention/relative_attention_bias/weight" in s_dict: UpperCAmelCase = s_dict[ '''encoder/block/0/layer/0/SelfAttention/relative_attention_bias/weight''' ].T if "decoder/block/0/layer/0/SelfAttention/relative_attention_bias/weight" in s_dict: UpperCAmelCase = s_dict[ '''decoder/block/0/layer/0/SelfAttention/relative_attention_bias/weight''' ].T # 3. Take extra care of the EXPERTS layer for key in list(s_dict.keys() ): if "expert" in key: UpperCAmelCase = s_dict[key].shape[0] UpperCAmelCase = s_dict[key] for idx in range(UpperCamelCase__ ): UpperCAmelCase = expert_weihts[idx] print(F"""{key} -> {key.replace("expert/" , "nested fstring" )}""" ) s_dict.pop(UpperCamelCase__ ) return s_dict __A : Optional[int] = { "NUM_ENCODER_LAYERS": "num_layers", "NUM_DECODER_LAYERS": "num_decoder_layers", "NUM_HEADS": "num_heads", "HEAD_DIM": "d_kv", "EMBED_DIM": "d_model", "MLP_DIM": "d_ff", "NUM_SELECTED_EXPERTS": "num_selected_experts", "NUM_ENCODER_SPARSE_LAYERS": "num_sparse_encoder_layers", "NUM_DECODER_SPARSE_LAYERS": "num_sparse_decoder_layers", "dense.MlpBlock.activations": "feed_forward_proj", } def __SCREAMING_SNAKE_CASE ( UpperCamelCase__ , UpperCamelCase__ ) -> List[str]: '''simple docstring''' import regex as re with open(UpperCamelCase__ , '''r''' ) as f: UpperCAmelCase = f.read() UpperCAmelCase = re.findall(R'''(.*) = ([0-9.]*)''' , UpperCamelCase__ ) UpperCAmelCase = {} for param, value in regex_match: if param in GIN_TO_CONFIG_MAPPING and value != "": UpperCAmelCase = float(UpperCamelCase__ ) if '''.''' in value else int(UpperCamelCase__ ) UpperCAmelCase = re.findall(R'''(.*activations) = \(\'(.*)\',\)''' , UpperCamelCase__ )[0] UpperCAmelCase = str(activation[1] ) UpperCAmelCase = num_experts UpperCAmelCase = SwitchTransformersConfig(**UpperCamelCase__ ) return config def __SCREAMING_SNAKE_CASE ( UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__=None , UpperCamelCase__="./" , UpperCamelCase__=8 ) -> List[Any]: '''simple docstring''' print(F"""Loading flax weights from : {flax_checkpoint_path}""" ) UpperCAmelCase = checkpoints.load_tax_checkpoint(UpperCamelCase__ ) if gin_file is not None: UpperCAmelCase = convert_gin_to_config(UpperCamelCase__ , UpperCamelCase__ ) else: UpperCAmelCase = SwitchTransformersConfig.from_pretrained(UpperCamelCase__ ) UpperCAmelCase = SwitchTransformersForConditionalGeneration(UpperCamelCase__ ) UpperCAmelCase = flax_params['''target'''] UpperCAmelCase = flatten_dict(UpperCamelCase__ , sep='''/''' ) UpperCAmelCase = rename_keys(UpperCamelCase__ ) UpperCAmelCase = unflatten_dict(UpperCamelCase__ , sep='''/''' ) # Load the flax params in the PT model load_flax_weights_in_pytorch_model(UpperCamelCase__ , UpperCamelCase__ ) print(F"""Save PyTorch model to {pytorch_dump_path}""" ) pt_model.save_pretrained(UpperCamelCase__ ) if __name__ == "__main__": __A : Tuple = argparse.ArgumentParser() # Required parameters parser.add_argument( "--switch_t5x_checkpoint_path", default=None, type=str, required=True, help=( "The config json file corresponding to the pre-trained SwitchTransformers model. \nThis specifies the" " model architecture. If not provided, a `gin_file` has to be provided." ), ) parser.add_argument( "--gin_file", default=None, type=str, required=False, help="Path to the gin config file. If not provided, a `config_file` has to be passed ", ) parser.add_argument( "--config_name", default=None, type=str, required=False, help="Config name of SwitchTransformers model." ) parser.add_argument( "--pytorch_dump_folder_path", default=None, type=str, required=True, help="Path to the output pytorch model." ) parser.add_argument("--num_experts", default=8, type=int, required=False, help="Number of experts") __A : Tuple = parser.parse_args() convert_flax_checkpoint_to_pytorch( args.switch_tax_checkpoint_path, args.config_name, args.gin_file, args.pytorch_dump_folder_path, args.num_experts, )
273
0
"""simple docstring""" from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_sentencepiece_available, is_speech_available, is_tf_available, is_torch_available, ) __A = { "configuration_speech_to_text": ["SPEECH_TO_TEXT_PRETRAINED_CONFIG_ARCHIVE_MAP", "Speech2TextConfig"], "processing_speech_to_text": ["Speech2TextProcessor"], } try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __A = ["Speech2TextTokenizer"] try: if not is_speech_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __A = ["Speech2TextFeatureExtractor"] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __A = [ "TF_SPEECH_TO_TEXT_PRETRAINED_MODEL_ARCHIVE_LIST", "TFSpeech2TextForConditionalGeneration", "TFSpeech2TextModel", "TFSpeech2TextPreTrainedModel", ] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __A = [ "SPEECH_TO_TEXT_PRETRAINED_MODEL_ARCHIVE_LIST", "Speech2TextForConditionalGeneration", "Speech2TextModel", "Speech2TextPreTrainedModel", ] if TYPE_CHECKING: from .configuration_speech_to_text import SPEECH_TO_TEXT_PRETRAINED_CONFIG_ARCHIVE_MAP, SpeechaTextConfig from .processing_speech_to_text import SpeechaTextProcessor try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_speech_to_text import SpeechaTextTokenizer try: if not is_speech_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .feature_extraction_speech_to_text import SpeechaTextFeatureExtractor try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_speech_to_text import ( TF_SPEECH_TO_TEXT_PRETRAINED_MODEL_ARCHIVE_LIST, TFSpeechaTextForConditionalGeneration, TFSpeechaTextModel, TFSpeechaTextPreTrainedModel, ) try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_speech_to_text import ( SPEECH_TO_TEXT_PRETRAINED_MODEL_ARCHIVE_LIST, SpeechaTextForConditionalGeneration, SpeechaTextModel, SpeechaTextPreTrainedModel, ) else: import sys __A = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
217
import tempfile import numpy as np import torch from transformers import AutoTokenizer, TaEncoderModel from diffusers import DDPMScheduler, UNetaDConditionModel from diffusers.models.attention_processor import AttnAddedKVProcessor from diffusers.pipelines.deepfloyd_if import IFWatermarker from diffusers.utils.testing_utils import torch_device from ..test_pipelines_common import to_np class A_ : def _lowercase ( self ): '''simple docstring''' torch.manual_seed(0 ) UpperCAmelCase = TaEncoderModel.from_pretrained('''hf-internal-testing/tiny-random-t5''' ) torch.manual_seed(0 ) UpperCAmelCase = AutoTokenizer.from_pretrained('''hf-internal-testing/tiny-random-t5''' ) torch.manual_seed(0 ) UpperCAmelCase = UNetaDConditionModel( sample_size=3_2 , layers_per_block=1 , block_out_channels=[3_2, 6_4] , down_block_types=[ '''ResnetDownsampleBlock2D''', '''SimpleCrossAttnDownBlock2D''', ] , mid_block_type='''UNetMidBlock2DSimpleCrossAttn''' , up_block_types=['''SimpleCrossAttnUpBlock2D''', '''ResnetUpsampleBlock2D'''] , in_channels=3 , out_channels=6 , cross_attention_dim=3_2 , encoder_hid_dim=3_2 , attention_head_dim=8 , addition_embed_type='''text''' , addition_embed_type_num_heads=2 , cross_attention_norm='''group_norm''' , resnet_time_scale_shift='''scale_shift''' , act_fn='''gelu''' , ) unet.set_attn_processor(AttnAddedKVProcessor() ) # For reproducibility tests torch.manual_seed(0 ) UpperCAmelCase = DDPMScheduler( num_train_timesteps=1_0_0_0 , beta_schedule='''squaredcos_cap_v2''' , beta_start=0.00_01 , beta_end=0.02 , thresholding=_A , dynamic_thresholding_ratio=0.95 , sample_max_value=1.0 , prediction_type='''epsilon''' , variance_type='''learned_range''' , ) torch.manual_seed(0 ) UpperCAmelCase = IFWatermarker() return { "text_encoder": text_encoder, "tokenizer": tokenizer, "unet": unet, "scheduler": scheduler, "watermarker": watermarker, "safety_checker": None, "feature_extractor": None, } def _lowercase ( self ): '''simple docstring''' torch.manual_seed(0 ) UpperCAmelCase = TaEncoderModel.from_pretrained('''hf-internal-testing/tiny-random-t5''' ) torch.manual_seed(0 ) UpperCAmelCase = AutoTokenizer.from_pretrained('''hf-internal-testing/tiny-random-t5''' ) torch.manual_seed(0 ) UpperCAmelCase = UNetaDConditionModel( sample_size=3_2 , layers_per_block=[1, 2] , block_out_channels=[3_2, 6_4] , down_block_types=[ '''ResnetDownsampleBlock2D''', '''SimpleCrossAttnDownBlock2D''', ] , mid_block_type='''UNetMidBlock2DSimpleCrossAttn''' , up_block_types=['''SimpleCrossAttnUpBlock2D''', '''ResnetUpsampleBlock2D'''] , in_channels=6 , out_channels=6 , cross_attention_dim=3_2 , encoder_hid_dim=3_2 , attention_head_dim=8 , addition_embed_type='''text''' , addition_embed_type_num_heads=2 , cross_attention_norm='''group_norm''' , resnet_time_scale_shift='''scale_shift''' , act_fn='''gelu''' , class_embed_type='''timestep''' , mid_block_scale_factor=1.4_14 , time_embedding_act_fn='''gelu''' , time_embedding_dim=3_2 , ) unet.set_attn_processor(AttnAddedKVProcessor() ) # For reproducibility tests torch.manual_seed(0 ) UpperCAmelCase = DDPMScheduler( num_train_timesteps=1_0_0_0 , beta_schedule='''squaredcos_cap_v2''' , beta_start=0.00_01 , beta_end=0.02 , thresholding=_A , dynamic_thresholding_ratio=0.95 , sample_max_value=1.0 , prediction_type='''epsilon''' , variance_type='''learned_range''' , ) torch.manual_seed(0 ) UpperCAmelCase = DDPMScheduler( num_train_timesteps=1_0_0_0 , beta_schedule='''squaredcos_cap_v2''' , beta_start=0.00_01 , beta_end=0.02 , ) torch.manual_seed(0 ) UpperCAmelCase = IFWatermarker() return { "text_encoder": text_encoder, "tokenizer": tokenizer, "unet": unet, "scheduler": scheduler, "image_noising_scheduler": image_noising_scheduler, "watermarker": watermarker, "safety_checker": None, "feature_extractor": None, } def _lowercase ( self ): '''simple docstring''' UpperCAmelCase = self.get_dummy_components() UpperCAmelCase = self.pipeline_class(**_A ) pipe.to(_A ) pipe.set_progress_bar_config(disable=_A ) UpperCAmelCase = self.get_dummy_inputs(_A ) UpperCAmelCase = inputs['''prompt'''] UpperCAmelCase = inputs['''generator'''] UpperCAmelCase = inputs['''num_inference_steps'''] UpperCAmelCase = inputs['''output_type'''] if "image" in inputs: UpperCAmelCase = inputs['''image'''] else: UpperCAmelCase = None if "mask_image" in inputs: UpperCAmelCase = inputs['''mask_image'''] else: UpperCAmelCase = None if "original_image" in inputs: UpperCAmelCase = inputs['''original_image'''] else: UpperCAmelCase = None UpperCAmelCase , UpperCAmelCase = pipe.encode_prompt(_A ) # inputs with prompt converted to embeddings UpperCAmelCase = { '''prompt_embeds''': prompt_embeds, '''negative_prompt_embeds''': negative_prompt_embeds, '''generator''': generator, '''num_inference_steps''': num_inference_steps, '''output_type''': output_type, } if image is not None: UpperCAmelCase = image if mask_image is not None: UpperCAmelCase = mask_image if original_image is not None: UpperCAmelCase = original_image # set all optional components to None for optional_component in pipe._optional_components: setattr(_A , _A , _A ) UpperCAmelCase = pipe(**_A )[0] with tempfile.TemporaryDirectory() as tmpdir: pipe.save_pretrained(_A ) UpperCAmelCase = self.pipeline_class.from_pretrained(_A ) pipe_loaded.to(_A ) pipe_loaded.set_progress_bar_config(disable=_A ) pipe_loaded.unet.set_attn_processor(AttnAddedKVProcessor() ) # For reproducibility tests for optional_component in pipe._optional_components: self.assertTrue( getattr(_A , _A ) is None , F"""`{optional_component}` did not stay set to None after loading.""" , ) UpperCAmelCase = self.get_dummy_inputs(_A ) UpperCAmelCase = inputs['''generator'''] UpperCAmelCase = inputs['''num_inference_steps'''] UpperCAmelCase = inputs['''output_type'''] # inputs with prompt converted to embeddings UpperCAmelCase = { '''prompt_embeds''': prompt_embeds, '''negative_prompt_embeds''': negative_prompt_embeds, '''generator''': generator, '''num_inference_steps''': num_inference_steps, '''output_type''': output_type, } if image is not None: UpperCAmelCase = image if mask_image is not None: UpperCAmelCase = mask_image if original_image is not None: UpperCAmelCase = original_image UpperCAmelCase = pipe_loaded(**_A )[0] UpperCAmelCase = np.abs(to_np(_A ) - to_np(_A ) ).max() self.assertLess(_A , 1E-4 ) def _lowercase ( self ): '''simple docstring''' UpperCAmelCase = self.get_dummy_components() UpperCAmelCase = self.pipeline_class(**_A ) pipe.to(_A ) pipe.set_progress_bar_config(disable=_A ) UpperCAmelCase = self.get_dummy_inputs(_A ) UpperCAmelCase = pipe(**_A )[0] with tempfile.TemporaryDirectory() as tmpdir: pipe.save_pretrained(_A ) UpperCAmelCase = self.pipeline_class.from_pretrained(_A ) pipe_loaded.to(_A ) pipe_loaded.set_progress_bar_config(disable=_A ) pipe_loaded.unet.set_attn_processor(AttnAddedKVProcessor() ) # For reproducibility tests UpperCAmelCase = self.get_dummy_inputs(_A ) UpperCAmelCase = pipe_loaded(**_A )[0] UpperCAmelCase = np.abs(to_np(_A ) - to_np(_A ) ).max() self.assertLess(_A , 1E-4 )
273
0
def __snake_case ( _lowerCAmelCase : Tuple ) -> bool: A_ : Dict = 0 for ch in input_str: A_ : str = ord(UpperCamelCase__ ) A_ : List[Any] = pow(2 , UpperCamelCase__ ) # If we already turned on bit for current character's unicode if bitmap >> ch_unicode & 1 == 1: return False bitmap |= ch_bit_index_on return True if __name__ == "__main__": import doctest doctest.testmod()
300
from __future__ import annotations from collections import namedtuple def __SCREAMING_SNAKE_CASE ( UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ ) -> tuple: '''simple docstring''' UpperCAmelCase = namedtuple('''result''' , '''name value''' ) if (voltage, current, power).count(0 ) != 1: raise ValueError('''Only one argument must be 0''' ) elif power < 0: raise ValueError( '''Power cannot be negative in any electrical/electronics system''' ) elif voltage == 0: return result('''voltage''' , power / current ) elif current == 0: return result('''current''' , power / voltage ) elif power == 0: return result('''power''' , float(round(abs(voltage * current ) , 2 ) ) ) else: raise ValueError('''Exactly one argument must be 0''' ) if __name__ == "__main__": import doctest doctest.testmod()
273
0
"""simple docstring""" def __UpperCAmelCase ( __lowerCamelCase , __lowerCamelCase ) -> str: lowercase__ : int = [0 for i in range(r + 1 )] # nc0 = 1 lowercase__ : Union[str, Any] = 1 for i in range(1 , n + 1 ): # to compute current row from previous row. lowercase__ : Union[str, Any] = min(UpperCamelCase__ , UpperCamelCase__ ) while j > 0: c[j] += c[j - 1] j -= 1 return c[r] print(binomial_coefficient(n=10, r=5))
16
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_tf_available, is_tokenizers_available, is_torch_available, ) __A : Dict = { "configuration_roformer": ["ROFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP", "RoFormerConfig", "RoFormerOnnxConfig"], "tokenization_roformer": ["RoFormerTokenizer"], } try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __A : Union[str, Any] = ["RoFormerTokenizerFast"] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __A : str = [ "ROFORMER_PRETRAINED_MODEL_ARCHIVE_LIST", "RoFormerForCausalLM", "RoFormerForMaskedLM", "RoFormerForMultipleChoice", "RoFormerForQuestionAnswering", "RoFormerForSequenceClassification", "RoFormerForTokenClassification", "RoFormerLayer", "RoFormerModel", "RoFormerPreTrainedModel", "load_tf_weights_in_roformer", ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __A : int = [ "TF_ROFORMER_PRETRAINED_MODEL_ARCHIVE_LIST", "TFRoFormerForCausalLM", "TFRoFormerForMaskedLM", "TFRoFormerForMultipleChoice", "TFRoFormerForQuestionAnswering", "TFRoFormerForSequenceClassification", "TFRoFormerForTokenClassification", "TFRoFormerLayer", "TFRoFormerModel", "TFRoFormerPreTrainedModel", ] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __A : Any = [ "FLAX_ROFORMER_PRETRAINED_MODEL_ARCHIVE_LIST", "FlaxRoFormerForMaskedLM", "FlaxRoFormerForMultipleChoice", "FlaxRoFormerForQuestionAnswering", "FlaxRoFormerForSequenceClassification", "FlaxRoFormerForTokenClassification", "FlaxRoFormerModel", "FlaxRoFormerPreTrainedModel", ] if TYPE_CHECKING: from .configuration_roformer import ROFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP, RoFormerConfig, RoFormerOnnxConfig from .tokenization_roformer import RoFormerTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_roformer_fast import RoFormerTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_roformer import ( ROFORMER_PRETRAINED_MODEL_ARCHIVE_LIST, RoFormerForCausalLM, RoFormerForMaskedLM, RoFormerForMultipleChoice, RoFormerForQuestionAnswering, RoFormerForSequenceClassification, RoFormerForTokenClassification, RoFormerLayer, RoFormerModel, RoFormerPreTrainedModel, load_tf_weights_in_roformer, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_roformer import ( TF_ROFORMER_PRETRAINED_MODEL_ARCHIVE_LIST, TFRoFormerForCausalLM, TFRoFormerForMaskedLM, TFRoFormerForMultipleChoice, TFRoFormerForQuestionAnswering, TFRoFormerForSequenceClassification, TFRoFormerForTokenClassification, TFRoFormerLayer, TFRoFormerModel, TFRoFormerPreTrainedModel, ) try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_flax_roformer import ( FLAX_ROFORMER_PRETRAINED_MODEL_ARCHIVE_LIST, FlaxRoFormerForMaskedLM, FlaxRoFormerForMultipleChoice, FlaxRoFormerForQuestionAnswering, FlaxRoFormerForSequenceClassification, FlaxRoFormerForTokenClassification, FlaxRoFormerModel, FlaxRoFormerPreTrainedModel, ) else: import sys __A : int = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
273
0
import shutil import tempfile import unittest import numpy as np import pytest from transformers.testing_utils import require_vision from transformers.utils import is_vision_available if is_vision_available(): from PIL import Image from transformers import AutoProcessor, BlipaProcessor, BlipImageProcessor, GPTaTokenizer, PreTrainedTokenizerFast @require_vision class A_ ( unittest.TestCase ): '''simple docstring''' def SCREAMING_SNAKE_CASE__ ( self ): lowercase = tempfile.mkdtemp() lowercase = BlipImageProcessor() lowercase = GPTaTokenizer.from_pretrained('hf-internal-testing/tiny-random-GPT2Model' ) lowercase = BlipaProcessor(_A , _A ) processor.save_pretrained(self.tmpdirname ) def SCREAMING_SNAKE_CASE__ ( self , **snake_case ): return AutoProcessor.from_pretrained(self.tmpdirname , **_A ).tokenizer def SCREAMING_SNAKE_CASE__ ( self , **snake_case ): return AutoProcessor.from_pretrained(self.tmpdirname , **_A ).image_processor def SCREAMING_SNAKE_CASE__ ( self ): shutil.rmtree(self.tmpdirname ) def SCREAMING_SNAKE_CASE__ ( self ): lowercase = [np.random.randint(255 , size=(3, 30, 400) , dtype=np.uinta )] lowercase = [Image.fromarray(np.moveaxis(_A , 0 , -1 ) ) for x in image_inputs] return image_inputs def SCREAMING_SNAKE_CASE__ ( self ): lowercase = BlipaProcessor(tokenizer=self.get_tokenizer() , image_processor=self.get_image_processor() ) processor.save_pretrained(self.tmpdirname ) lowercase = self.get_tokenizer(bos_token='(BOS)' , eos_token='(EOS)' ) lowercase = self.get_image_processor(do_normalize=_A , padding_value=1.0 ) lowercase = BlipaProcessor.from_pretrained( self.tmpdirname , bos_token='(BOS)' , eos_token='(EOS)' , do_normalize=_A , padding_value=1.0 ) self.assertEqual(processor.tokenizer.get_vocab() , tokenizer_add_kwargs.get_vocab() ) self.assertIsInstance(processor.tokenizer , _A ) self.assertEqual(processor.image_processor.to_json_string() , image_processor_add_kwargs.to_json_string() ) self.assertIsInstance(processor.image_processor , _A ) def SCREAMING_SNAKE_CASE__ ( self ): lowercase = self.get_image_processor() lowercase = self.get_tokenizer() lowercase = BlipaProcessor(tokenizer=_A , image_processor=_A ) lowercase = self.prepare_image_inputs() lowercase = image_processor(_A , return_tensors='np' ) lowercase = processor(images=_A , return_tensors='np' ) for key in input_feat_extract.keys(): self.assertAlmostEqual(input_feat_extract[key].sum() , input_processor[key].sum() , delta=1E-2 ) def SCREAMING_SNAKE_CASE__ ( self ): lowercase = self.get_image_processor() lowercase = self.get_tokenizer() lowercase = BlipaProcessor(tokenizer=_A , image_processor=_A ) lowercase = 'lower newer' lowercase = processor(text=_A ) lowercase = tokenizer(_A , return_token_type_ids=_A ) for key in encoded_tok.keys(): self.assertListEqual(encoded_tok[key] , encoded_processor[key] ) def SCREAMING_SNAKE_CASE__ ( self ): lowercase = self.get_image_processor() lowercase = self.get_tokenizer() lowercase = BlipaProcessor(tokenizer=_A , image_processor=_A ) lowercase = 'lower newer' lowercase = self.prepare_image_inputs() lowercase = processor(text=_A , images=_A ) self.assertListEqual(list(inputs.keys() ) , ['pixel_values', 'input_ids', 'attention_mask'] ) # test if it raises when no input is passed with pytest.raises(_A ): processor() def SCREAMING_SNAKE_CASE__ ( self ): lowercase = self.get_image_processor() lowercase = self.get_tokenizer() lowercase = BlipaProcessor(tokenizer=_A , image_processor=_A ) lowercase = [[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]] lowercase = processor.batch_decode(_A ) lowercase = tokenizer.batch_decode(_A ) self.assertListEqual(_A , _A ) def SCREAMING_SNAKE_CASE__ ( self ): lowercase = self.get_image_processor() lowercase = self.get_tokenizer() lowercase = BlipaProcessor(tokenizer=_A , image_processor=_A ) lowercase = 'lower newer' lowercase = self.prepare_image_inputs() lowercase = processor(text=_A , images=_A ) # For now the processor supports only ['pixel_values', 'input_ids', 'attention_mask'] self.assertListEqual(list(inputs.keys() ) , ['pixel_values', 'input_ids', 'attention_mask'] )
195
import argparse import torch from transformers import YosoConfig, YosoForMaskedLM def __SCREAMING_SNAKE_CASE ( UpperCamelCase__ ) -> Union[str, Any]: '''simple docstring''' if "model" in orig_key: UpperCAmelCase = orig_key.replace('''model.''' , '''''' ) if "norm1" in orig_key: UpperCAmelCase = orig_key.replace('''norm1''' , '''attention.output.LayerNorm''' ) if "norm2" in orig_key: UpperCAmelCase = orig_key.replace('''norm2''' , '''output.LayerNorm''' ) if "norm" in orig_key: UpperCAmelCase = orig_key.replace('''norm''' , '''LayerNorm''' ) if "transformer" in orig_key: UpperCAmelCase = orig_key.split('''.''' )[0].split('''_''' )[-1] UpperCAmelCase = orig_key.replace(F"""transformer_{layer_num}""" , F"""encoder.layer.{layer_num}""" ) if "mha.attn" in orig_key: UpperCAmelCase = orig_key.replace('''mha.attn''' , '''attention.self''' ) if "mha" in orig_key: UpperCAmelCase = orig_key.replace('''mha''' , '''attention''' ) if "W_q" in orig_key: UpperCAmelCase = orig_key.replace('''W_q''' , '''self.query''' ) if "W_k" in orig_key: UpperCAmelCase = orig_key.replace('''W_k''' , '''self.key''' ) if "W_v" in orig_key: UpperCAmelCase = orig_key.replace('''W_v''' , '''self.value''' ) if "ff1" in orig_key: UpperCAmelCase = orig_key.replace('''ff1''' , '''intermediate.dense''' ) if "ff2" in orig_key: UpperCAmelCase = orig_key.replace('''ff2''' , '''output.dense''' ) if "ff" in orig_key: UpperCAmelCase = orig_key.replace('''ff''' , '''output.dense''' ) if "mlm_class" in orig_key: UpperCAmelCase = orig_key.replace('''mlm.mlm_class''' , '''cls.predictions.decoder''' ) if "mlm" in orig_key: UpperCAmelCase = orig_key.replace('''mlm''' , '''cls.predictions.transform''' ) if "cls" not in orig_key: UpperCAmelCase = '''yoso.''' + orig_key return orig_key def __SCREAMING_SNAKE_CASE ( UpperCamelCase__ , UpperCamelCase__ ) -> Dict: '''simple docstring''' for key in orig_state_dict.copy().keys(): UpperCAmelCase = orig_state_dict.pop(UpperCamelCase__ ) if ("pooler" in key) or ("sen_class" in key): continue else: UpperCAmelCase = val UpperCAmelCase = orig_state_dict['''cls.predictions.decoder.bias'''] UpperCAmelCase = torch.arange(UpperCamelCase__ ).expand((1, -1) ) + 2 return orig_state_dict def __SCREAMING_SNAKE_CASE ( UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ ) -> int: '''simple docstring''' UpperCAmelCase = torch.load(UpperCamelCase__ , map_location='''cpu''' )['''model_state_dict'''] UpperCAmelCase = YosoConfig.from_json_file(UpperCamelCase__ ) UpperCAmelCase = YosoForMaskedLM(UpperCamelCase__ ) UpperCAmelCase = convert_checkpoint_helper(config.max_position_embeddings , UpperCamelCase__ ) print(model.load_state_dict(UpperCamelCase__ ) ) model.eval() model.save_pretrained(UpperCamelCase__ ) print(F"""Checkpoint successfuly converted. Model saved at {pytorch_dump_path}""" ) if __name__ == "__main__": __A : Any = argparse.ArgumentParser() # Required parameters parser.add_argument( "--pytorch_model_path", default=None, type=str, required=True, help="Path to YOSO pytorch checkpoint." ) parser.add_argument( "--config_file", default=None, type=str, required=True, help="The json file for YOSO model config.", ) parser.add_argument( "--pytorch_dump_path", default=None, type=str, required=True, help="Path to the output PyTorch model." ) __A : List[str] = parser.parse_args() convert_yoso_checkpoint(args.pytorch_model_path, args.config_file, args.pytorch_dump_path)
273
0
'''simple docstring''' import unittest from transformers import is_vision_available from transformers.pipelines import pipeline from transformers.testing_utils import ( is_pipeline_test, nested_simplify, require_tf, require_torch, require_vision, slow, ) from .test_pipelines_common import ANY if is_vision_available(): from PIL import Image else: class __lowerCAmelCase : """simple docstring""" @staticmethod def snake_case__ ( *lowerCAmelCase__ : Any , **lowerCAmelCase__ : Tuple ) -> Any: '''simple docstring''' pass @is_pipeline_test @require_vision class __lowerCAmelCase ( unittest.TestCase ): """simple docstring""" @require_torch def snake_case__ ( self : int ) -> Tuple: '''simple docstring''' _UpperCamelCase = pipeline( model='''hf-internal-testing/tiny-random-clip-zero-shot-image-classification''' , ) _UpperCamelCase = Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png''' ) _UpperCamelCase = image_classifier(_A , candidate_labels=['''a''', '''b''', '''c'''] ) # The floating scores are so close, we enter floating error approximation and the order is not guaranteed across # python and torch versions. self.assertIn( nested_simplify(_A ) , [ [{'''score''': 0.333, '''label''': '''a'''}, {'''score''': 0.333, '''label''': '''b'''}, {'''score''': 0.333, '''label''': '''c'''}], [{'''score''': 0.333, '''label''': '''a'''}, {'''score''': 0.333, '''label''': '''c'''}, {'''score''': 0.333, '''label''': '''b'''}], ] , ) _UpperCamelCase = image_classifier([image] * 5 , candidate_labels=['''A''', '''B''', '''C'''] , batch_size=2 ) self.assertEqual( nested_simplify(_A ) , [ [ {'''score''': 0.333, '''label''': ANY(_A )}, {'''score''': 0.333, '''label''': ANY(_A )}, {'''score''': 0.333, '''label''': ANY(_A )}, ], [ {'''score''': 0.333, '''label''': ANY(_A )}, {'''score''': 0.333, '''label''': ANY(_A )}, {'''score''': 0.333, '''label''': ANY(_A )}, ], [ {'''score''': 0.333, '''label''': ANY(_A )}, {'''score''': 0.333, '''label''': ANY(_A )}, {'''score''': 0.333, '''label''': ANY(_A )}, ], [ {'''score''': 0.333, '''label''': ANY(_A )}, {'''score''': 0.333, '''label''': ANY(_A )}, {'''score''': 0.333, '''label''': ANY(_A )}, ], [ {'''score''': 0.333, '''label''': ANY(_A )}, {'''score''': 0.333, '''label''': ANY(_A )}, {'''score''': 0.333, '''label''': ANY(_A )}, ], ] , ) @require_tf def snake_case__ ( self : Tuple ) -> Optional[int]: '''simple docstring''' _UpperCamelCase = pipeline( model='''hf-internal-testing/tiny-random-clip-zero-shot-image-classification''' , framework='''tf''' ) _UpperCamelCase = Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png''' ) _UpperCamelCase = image_classifier(_A , candidate_labels=['''a''', '''b''', '''c'''] ) self.assertEqual( nested_simplify(_A ) , [{'''score''': 0.333, '''label''': '''a'''}, {'''score''': 0.333, '''label''': '''b'''}, {'''score''': 0.333, '''label''': '''c'''}] , ) _UpperCamelCase = image_classifier([image] * 5 , candidate_labels=['''A''', '''B''', '''C'''] , batch_size=2 ) self.assertEqual( nested_simplify(_A ) , [ [ {'''score''': 0.333, '''label''': ANY(_A )}, {'''score''': 0.333, '''label''': ANY(_A )}, {'''score''': 0.333, '''label''': ANY(_A )}, ], [ {'''score''': 0.333, '''label''': ANY(_A )}, {'''score''': 0.333, '''label''': ANY(_A )}, {'''score''': 0.333, '''label''': ANY(_A )}, ], [ {'''score''': 0.333, '''label''': ANY(_A )}, {'''score''': 0.333, '''label''': ANY(_A )}, {'''score''': 0.333, '''label''': ANY(_A )}, ], [ {'''score''': 0.333, '''label''': ANY(_A )}, {'''score''': 0.333, '''label''': ANY(_A )}, {'''score''': 0.333, '''label''': ANY(_A )}, ], [ {'''score''': 0.333, '''label''': ANY(_A )}, {'''score''': 0.333, '''label''': ANY(_A )}, {'''score''': 0.333, '''label''': ANY(_A )}, ], ] , ) @slow @require_torch def snake_case__ ( self : Tuple ) -> Optional[int]: '''simple docstring''' _UpperCamelCase = pipeline( task='''zero-shot-image-classification''' , model='''openai/clip-vit-base-patch32''' , ) # This is an image of 2 cats with remotes and no planes _UpperCamelCase = Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png''' ) _UpperCamelCase = image_classifier(_A , candidate_labels=['''cat''', '''plane''', '''remote'''] ) self.assertEqual( nested_simplify(_A ) , [ {'''score''': 0.511, '''label''': '''remote'''}, {'''score''': 0.485, '''label''': '''cat'''}, {'''score''': 0.004, '''label''': '''plane'''}, ] , ) _UpperCamelCase = image_classifier([image] * 5 , candidate_labels=['''cat''', '''plane''', '''remote'''] , batch_size=2 ) self.assertEqual( nested_simplify(_A ) , [ [ {'''score''': 0.511, '''label''': '''remote'''}, {'''score''': 0.485, '''label''': '''cat'''}, {'''score''': 0.004, '''label''': '''plane'''}, ], ] * 5 , ) @slow @require_tf def snake_case__ ( self : List[Any] ) -> Any: '''simple docstring''' _UpperCamelCase = pipeline( task='''zero-shot-image-classification''' , model='''openai/clip-vit-base-patch32''' , framework='''tf''' ) # This is an image of 2 cats with remotes and no planes _UpperCamelCase = Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png''' ) _UpperCamelCase = image_classifier(_A , candidate_labels=['''cat''', '''plane''', '''remote'''] ) self.assertEqual( nested_simplify(_A ) , [ {'''score''': 0.511, '''label''': '''remote'''}, {'''score''': 0.485, '''label''': '''cat'''}, {'''score''': 0.004, '''label''': '''plane'''}, ] , ) _UpperCamelCase = image_classifier([image] * 5 , candidate_labels=['''cat''', '''plane''', '''remote'''] , batch_size=2 ) self.assertEqual( nested_simplify(_A ) , [ [ {'''score''': 0.511, '''label''': '''remote'''}, {'''score''': 0.485, '''label''': '''cat'''}, {'''score''': 0.004, '''label''': '''plane'''}, ], ] * 5 , )
324
def __SCREAMING_SNAKE_CASE ( UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ ) -> int: '''simple docstring''' if exponent == 1: return base if exponent % 2 == 0: UpperCAmelCase = _modexpt(UpperCamelCase__ , exponent // 2 , UpperCamelCase__ ) % modulo_value return (x * x) % modulo_value else: return (base * _modexpt(UpperCamelCase__ , exponent - 1 , UpperCamelCase__ )) % modulo_value def __SCREAMING_SNAKE_CASE ( UpperCamelCase__ = 1777 , UpperCamelCase__ = 1855 , UpperCamelCase__ = 8 ) -> int: '''simple docstring''' UpperCAmelCase = base for _ in range(1 , UpperCamelCase__ ): UpperCAmelCase = _modexpt(UpperCamelCase__ , UpperCamelCase__ , 10**digits ) return result if __name__ == "__main__": print(F'{solution() = }')
273
0
import unittest from transformers import MODEL_FOR_DOCUMENT_QUESTION_ANSWERING_MAPPING, AutoTokenizer, is_vision_available from transformers.pipelines import pipeline from transformers.pipelines.document_question_answering import apply_tesseract from transformers.testing_utils import ( is_pipeline_test, nested_simplify, require_detectrona, require_pytesseract, require_tf, require_torch, require_vision, slow, ) from .test_pipelines_common import ANY if is_vision_available(): from PIL import Image from transformers.image_utils import load_image else: class __A: """simple docstring""" @staticmethod def UpperCAmelCase_ (*SCREAMING_SNAKE_CASE_ , **SCREAMING_SNAKE_CASE_ ): pass def __magic_name__ ( __a : str ): '''simple docstring''' return None # This is a pinned image from a specific revision of a document question answering space, hosted by HuggingFace, # so we can expect it to be available. lowerCamelCase_ = ( "https://huggingface.co/spaces/impira/docquery/resolve/2f6c96314dc84dfda62d40de9da55f2f5165d403/invoice.png" ) @is_pipeline_test @require_torch @require_vision class __A( unittest.TestCase ): """simple docstring""" SCREAMING_SNAKE_CASE__ = MODEL_FOR_DOCUMENT_QUESTION_ANSWERING_MAPPING @require_pytesseract @require_vision def UpperCAmelCase_ (self , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ): UpperCamelCase__ = pipeline( """document-question-answering""" , model=_A , tokenizer=_A , image_processor=_A ) UpperCamelCase__ = INVOICE_URL UpperCamelCase__ = list(zip(*apply_tesseract(load_image(_A ) , _A , """""" ) ) ) UpperCamelCase__ = """What is the placebo?""" UpperCamelCase__ = [ { """image""": load_image(_A ), """question""": question, }, { """image""": image, """question""": question, }, { """image""": image, """question""": question, """word_boxes""": word_boxes, }, ] return dqa_pipeline, examples def UpperCAmelCase_ (self , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ ): UpperCamelCase__ = dqa_pipeline(_A , top_k=2 ) self.assertEqual( _A , [ [ {"""score""": ANY(_A ), """answer""": ANY(_A ), """start""": ANY(_A ), """end""": ANY(_A )}, {"""score""": ANY(_A ), """answer""": ANY(_A ), """start""": ANY(_A ), """end""": ANY(_A )}, ] ] * 3 , ) @require_torch @require_detectrona @require_pytesseract def UpperCAmelCase_ (self ): UpperCamelCase__ = pipeline("""document-question-answering""" , model="""hf-internal-testing/tiny-random-layoutlmv2""" ) UpperCamelCase__ = INVOICE_URL UpperCamelCase__ = """How many cats are there?""" UpperCamelCase__ = [ {"""score""": 0.0001, """answer""": """oy 2312/2019""", """start""": 38, """end""": 39}, {"""score""": 0.0001, """answer""": """oy 2312/2019 DUE""", """start""": 38, """end""": 40}, ] UpperCamelCase__ = dqa_pipeline(image=_A , question=_A , top_k=2 ) self.assertEqual(nested_simplify(_A , decimals=4 ) , _A ) UpperCamelCase__ = dqa_pipeline({"""image""": image, """question""": question} , top_k=2 ) self.assertEqual(nested_simplify(_A , decimals=4 ) , _A ) # This image does not detect ANY text in it, meaning layoutlmv2 should fail. # Empty answer probably UpperCamelCase__ = """./tests/fixtures/tests_samples/COCO/000000039769.png""" UpperCamelCase__ = dqa_pipeline(image=_A , question=_A , top_k=2 ) self.assertEqual(_A , [] ) # We can optionnally pass directly the words and bounding boxes UpperCamelCase__ = """./tests/fixtures/tests_samples/COCO/000000039769.png""" UpperCamelCase__ = [] UpperCamelCase__ = [] UpperCamelCase__ = dqa_pipeline(image=_A , question=_A , words=_A , boxes=_A , top_k=2 ) self.assertEqual(_A , [] ) @slow @require_torch @require_detectrona @require_pytesseract def UpperCAmelCase_ (self ): UpperCamelCase__ = pipeline( """document-question-answering""" , model="""tiennvcs/layoutlmv2-base-uncased-finetuned-docvqa""" , revision="""9977165""" , ) UpperCamelCase__ = INVOICE_URL UpperCamelCase__ = """What is the invoice number?""" UpperCamelCase__ = dqa_pipeline(image=_A , question=_A , top_k=2 ) self.assertEqual( nested_simplify(_A , decimals=4 ) , [ {"""score""": 0.9944, """answer""": """us-001""", """start""": 16, """end""": 16}, {"""score""": 0.0009, """answer""": """us-001""", """start""": 16, """end""": 16}, ] , ) UpperCamelCase__ = dqa_pipeline({"""image""": image, """question""": question} , top_k=2 ) self.assertEqual( nested_simplify(_A , decimals=4 ) , [ {"""score""": 0.9944, """answer""": """us-001""", """start""": 16, """end""": 16}, {"""score""": 0.0009, """answer""": """us-001""", """start""": 16, """end""": 16}, ] , ) UpperCamelCase__ = dqa_pipeline( [{"""image""": image, """question""": question}, {"""image""": image, """question""": question}] , top_k=2 ) self.assertEqual( nested_simplify(_A , decimals=4 ) , [ [ {"""score""": 0.9944, """answer""": """us-001""", """start""": 16, """end""": 16}, {"""score""": 0.0009, """answer""": """us-001""", """start""": 16, """end""": 16}, ], ] * 2 , ) @slow @require_torch @require_detectrona @require_pytesseract def UpperCAmelCase_ (self ): UpperCamelCase__ = pipeline( """document-question-answering""" , model="""tiennvcs/layoutlmv2-base-uncased-finetuned-docvqa""" , revision="""9977165""" , max_seq_len=50 , ) UpperCamelCase__ = INVOICE_URL UpperCamelCase__ = """What is the invoice number?""" UpperCamelCase__ = dqa_pipeline(image=_A , question=_A , top_k=2 ) self.assertEqual( nested_simplify(_A , decimals=4 ) , [ {"""score""": 0.9974, """answer""": """1110212019""", """start""": 23, """end""": 23}, {"""score""": 0.9948, """answer""": """us-001""", """start""": 16, """end""": 16}, ] , ) UpperCamelCase__ = dqa_pipeline({"""image""": image, """question""": question} , top_k=2 ) self.assertEqual( nested_simplify(_A , decimals=4 ) , [ {"""score""": 0.9974, """answer""": """1110212019""", """start""": 23, """end""": 23}, {"""score""": 0.9948, """answer""": """us-001""", """start""": 16, """end""": 16}, ] , ) UpperCamelCase__ = dqa_pipeline( [{"""image""": image, """question""": question}, {"""image""": image, """question""": question}] , top_k=2 ) self.assertEqual( nested_simplify(_A , decimals=4 ) , [ [ {"""score""": 0.9974, """answer""": """1110212019""", """start""": 23, """end""": 23}, {"""score""": 0.9948, """answer""": """us-001""", """start""": 16, """end""": 16}, ] ] * 2 , ) @slow @require_torch @require_pytesseract @require_vision def UpperCAmelCase_ (self ): UpperCamelCase__ = AutoTokenizer.from_pretrained( """impira/layoutlm-document-qa""" , revision="""3dc6de3""" , add_prefix_space=_A ) UpperCamelCase__ = pipeline( """document-question-answering""" , model="""impira/layoutlm-document-qa""" , tokenizer=_A , revision="""3dc6de3""" , ) UpperCamelCase__ = INVOICE_URL UpperCamelCase__ = """What is the invoice number?""" UpperCamelCase__ = dqa_pipeline(image=_A , question=_A , top_k=2 ) self.assertEqual( nested_simplify(_A , decimals=4 ) , [ {"""score""": 0.4251, """answer""": """us-001""", """start""": 16, """end""": 16}, {"""score""": 0.0819, """answer""": """1110212019""", """start""": 23, """end""": 23}, ] , ) UpperCamelCase__ = dqa_pipeline({"""image""": image, """question""": question} , top_k=2 ) self.assertEqual( nested_simplify(_A , decimals=4 ) , [ {"""score""": 0.4251, """answer""": """us-001""", """start""": 16, """end""": 16}, {"""score""": 0.0819, """answer""": """1110212019""", """start""": 23, """end""": 23}, ] , ) UpperCamelCase__ = dqa_pipeline( [{"""image""": image, """question""": question}, {"""image""": image, """question""": question}] , top_k=2 ) self.assertEqual( nested_simplify(_A , decimals=4 ) , [ [ {"""score""": 0.4251, """answer""": """us-001""", """start""": 16, """end""": 16}, {"""score""": 0.0819, """answer""": """1110212019""", """start""": 23, """end""": 23}, ] ] * 2 , ) UpperCamelCase__ = list(zip(*apply_tesseract(load_image(_A ) , _A , """""" ) ) ) # This model should also work if `image` is set to None UpperCamelCase__ = dqa_pipeline({"""image""": None, """word_boxes""": word_boxes, """question""": question} , top_k=2 ) self.assertEqual( nested_simplify(_A , decimals=4 ) , [ {"""score""": 0.4251, """answer""": """us-001""", """start""": 16, """end""": 16}, {"""score""": 0.0819, """answer""": """1110212019""", """start""": 23, """end""": 23}, ] , ) @slow @require_torch @require_pytesseract @require_vision def UpperCAmelCase_ (self ): UpperCamelCase__ = AutoTokenizer.from_pretrained( """impira/layoutlm-document-qa""" , revision="""3dc6de3""" , add_prefix_space=_A ) UpperCamelCase__ = pipeline( """document-question-answering""" , model="""impira/layoutlm-document-qa""" , tokenizer=_A , revision="""3dc6de3""" , max_seq_len=50 , ) UpperCamelCase__ = INVOICE_URL UpperCamelCase__ = """What is the invoice number?""" UpperCamelCase__ = dqa_pipeline(image=_A , question=_A , top_k=2 ) self.assertEqual( nested_simplify(_A , decimals=4 ) , [ {"""score""": 0.9999, """answer""": """us-001""", """start""": 16, """end""": 16}, {"""score""": 0.9998, """answer""": """us-001""", """start""": 16, """end""": 16}, ] , ) UpperCamelCase__ = dqa_pipeline( [{"""image""": image, """question""": question}, {"""image""": image, """question""": question}] , top_k=2 ) self.assertEqual( nested_simplify(_A , decimals=4 ) , [ [ {"""score""": 0.9999, """answer""": """us-001""", """start""": 16, """end""": 16}, {"""score""": 0.9998, """answer""": """us-001""", """start""": 16, """end""": 16}, ] ] * 2 , ) UpperCamelCase__ = list(zip(*apply_tesseract(load_image(_A ) , _A , """""" ) ) ) # This model should also work if `image` is set to None UpperCamelCase__ = dqa_pipeline({"""image""": None, """word_boxes""": word_boxes, """question""": question} , top_k=2 ) self.assertEqual( nested_simplify(_A , decimals=4 ) , [ {"""score""": 0.9999, """answer""": """us-001""", """start""": 16, """end""": 16}, {"""score""": 0.9998, """answer""": """us-001""", """start""": 16, """end""": 16}, ] , ) @slow @require_torch def UpperCAmelCase_ (self ): UpperCamelCase__ = pipeline( """document-question-answering""" , model="""naver-clova-ix/donut-base-finetuned-docvqa""" , tokenizer=AutoTokenizer.from_pretrained("""naver-clova-ix/donut-base-finetuned-docvqa""" ) , feature_extractor="""naver-clova-ix/donut-base-finetuned-docvqa""" , ) UpperCamelCase__ = INVOICE_URL UpperCamelCase__ = """What is the invoice number?""" UpperCamelCase__ = dqa_pipeline(image=_A , question=_A , top_k=2 ) self.assertEqual(nested_simplify(_A , decimals=4 ) , [{"""answer""": """us-001"""}] ) @require_tf @unittest.skip("""Document question answering not implemented in TF""" ) def UpperCAmelCase_ (self ): pass
244
from collections import OrderedDict from typing import TYPE_CHECKING, Any, List, Mapping, Optional, Union from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import TensorType, logging if TYPE_CHECKING: from ...onnx.config import PatchingSpec from ...tokenization_utils_base import PreTrainedTokenizerBase __A : Dict = logging.get_logger(__name__) __A : str = { "allenai/longformer-base-4096": "https://huggingface.co/allenai/longformer-base-4096/resolve/main/config.json", "allenai/longformer-large-4096": "https://huggingface.co/allenai/longformer-large-4096/resolve/main/config.json", "allenai/longformer-large-4096-finetuned-triviaqa": ( "https://huggingface.co/allenai/longformer-large-4096-finetuned-triviaqa/resolve/main/config.json" ), "allenai/longformer-base-4096-extra.pos.embd.only": ( "https://huggingface.co/allenai/longformer-base-4096-extra.pos.embd.only/resolve/main/config.json" ), "allenai/longformer-large-4096-extra.pos.embd.only": ( "https://huggingface.co/allenai/longformer-large-4096-extra.pos.embd.only/resolve/main/config.json" ), } class A_ (a_ ): UpperCAmelCase__ = '''longformer''' def __init__( self , _A = 5_1_2 , _A = 2 , _A = 1 , _A = 0 , _A = 2 , _A = 3_0_5_2_2 , _A = 7_6_8 , _A = 1_2 , _A = 1_2 , _A = 3_0_7_2 , _A = "gelu" , _A = 0.1 , _A = 0.1 , _A = 5_1_2 , _A = 2 , _A = 0.02 , _A = 1E-12 , _A = False , **_A , ): '''simple docstring''' super().__init__(pad_token_id=_A , **_A ) UpperCAmelCase = attention_window UpperCAmelCase = sep_token_id UpperCAmelCase = bos_token_id UpperCAmelCase = eos_token_id UpperCAmelCase = vocab_size UpperCAmelCase = hidden_size UpperCAmelCase = num_hidden_layers UpperCAmelCase = num_attention_heads UpperCAmelCase = hidden_act UpperCAmelCase = intermediate_size UpperCAmelCase = hidden_dropout_prob UpperCAmelCase = attention_probs_dropout_prob UpperCAmelCase = max_position_embeddings UpperCAmelCase = type_vocab_size UpperCAmelCase = initializer_range UpperCAmelCase = layer_norm_eps UpperCAmelCase = onnx_export class A_ (a_ ): def __init__( self , _A , _A = "default" , _A = None ): '''simple docstring''' super().__init__(_A , _A , _A ) UpperCAmelCase = True @property def _lowercase ( self ): '''simple docstring''' if self.task == "multiple-choice": UpperCAmelCase = {0: '''batch''', 1: '''choice''', 2: '''sequence'''} else: UpperCAmelCase = {0: '''batch''', 1: '''sequence'''} return OrderedDict( [ ('''input_ids''', dynamic_axis), ('''attention_mask''', dynamic_axis), ('''global_attention_mask''', dynamic_axis), ] ) @property def _lowercase ( self ): '''simple docstring''' UpperCAmelCase = super().outputs if self.task == "default": UpperCAmelCase = {0: '''batch'''} return outputs @property def _lowercase ( self ): '''simple docstring''' return 1E-4 @property def _lowercase ( self ): '''simple docstring''' return max(super().default_onnx_opset , 1_4 ) def _lowercase ( self , _A , _A = -1 , _A = -1 , _A = False , _A = None , ): '''simple docstring''' UpperCAmelCase = super().generate_dummy_inputs( preprocessor=_A , batch_size=_A , seq_length=_A , is_pair=_A , framework=_A ) import torch # for some reason, replacing this code by inputs["global_attention_mask"] = torch.randint(2, inputs["input_ids"].shape, dtype=torch.int64) # makes the export fail randomly UpperCAmelCase = torch.zeros_like(inputs['''input_ids'''] ) # make every second token global UpperCAmelCase = 1 return inputs
273
0
import gc import math import unittest import torch from diffusers import UNetaDModel from diffusers.utils import floats_tensor, logging, slow, torch_all_close, torch_device from diffusers.utils.testing_utils import enable_full_determinism from .test_modeling_common import ModelTesterMixin, UNetTesterMixin snake_case : Tuple = logging.get_logger(__name__) enable_full_determinism() class snake_case_ (a_ , a_ , unittest.TestCase ): UpperCAmelCase__ : str = UNetaDModel UpperCAmelCase__ : Union[str, Any] = '''sample''' @property def lowerCamelCase__( self :Dict ) -> int: a__ = 4 a__ = 3 a__ = (32, 32) a__ = floats_tensor((batch_size, num_channels) + sizes ).to(_A ) a__ = torch.tensor([10] ).to(_A ) return {"sample": noise, "timestep": time_step} @property def lowerCamelCase__( self :Optional[Any] ) -> Tuple: return (3, 32, 32) @property def lowerCamelCase__( self :List[str] ) -> List[str]: return (3, 32, 32) def lowerCamelCase__( self :Any ) -> List[Any]: a__ = { 'block_out_channels': (32, 64), 'down_block_types': ('DownBlock2D', 'AttnDownBlock2D'), 'up_block_types': ('AttnUpBlock2D', 'UpBlock2D'), 'attention_head_dim': 3, 'out_channels': 3, 'in_channels': 3, 'layers_per_block': 2, 'sample_size': 32, } a__ = self.dummy_input return init_dict, inputs_dict class snake_case_ (a_ , a_ , unittest.TestCase ): UpperCAmelCase__ : Optional[int] = UNetaDModel UpperCAmelCase__ : Dict = '''sample''' @property def lowerCamelCase__( self :Any ) -> Tuple: a__ = 4 a__ = 4 a__ = (32, 32) a__ = floats_tensor((batch_size, num_channels) + sizes ).to(_A ) a__ = torch.tensor([10] ).to(_A ) return {"sample": noise, "timestep": time_step} @property def lowerCamelCase__( self :Tuple ) -> Optional[int]: return (4, 32, 32) @property def lowerCamelCase__( self :List[str] ) -> List[Any]: return (4, 32, 32) def lowerCamelCase__( self :Any ) -> List[str]: a__ = { 'sample_size': 32, 'in_channels': 4, 'out_channels': 4, 'layers_per_block': 2, 'block_out_channels': (32, 64), 'attention_head_dim': 32, 'down_block_types': ('DownBlock2D', 'DownBlock2D'), 'up_block_types': ('UpBlock2D', 'UpBlock2D'), } a__ = self.dummy_input return init_dict, inputs_dict def lowerCamelCase__( self :Tuple ) -> Any: a__ , a__ = UNetaDModel.from_pretrained('fusing/unet-ldm-dummy-update' ,output_loading_info=_A ) self.assertIsNotNone(_A ) self.assertEqual(len(loading_info['missing_keys'] ) ,0 ) model.to(_A ) a__ = model(**self.dummy_input ).sample assert image is not None, "Make sure output is not None" @unittest.skipIf(torch_device != 'cuda' ,'This test is supposed to run on GPU' ) def lowerCamelCase__( self :Tuple ) -> List[str]: a__ , a__ = UNetaDModel.from_pretrained('fusing/unet-ldm-dummy-update' ,output_loading_info=_A ) model.to(_A ) a__ = model(**self.dummy_input ).sample assert image is not None, "Make sure output is not None" @unittest.skipIf(torch_device != 'cuda' ,'This test is supposed to run on GPU' ) def lowerCamelCase__( self :str ) -> Tuple: a__ , a__ = UNetaDModel.from_pretrained('fusing/unet-ldm-dummy-update' ,output_loading_info=_A ) model_accelerate.to(_A ) model_accelerate.eval() a__ = torch.randn( 1 ,model_accelerate.config.in_channels ,model_accelerate.config.sample_size ,model_accelerate.config.sample_size ,generator=torch.manual_seed(0 ) ,) a__ = noise.to(_A ) a__ = torch.tensor([10] * noise.shape[0] ).to(_A ) a__ = model_accelerate(_A ,_A )['sample'] # two models don't need to stay in the device at the same time del model_accelerate torch.cuda.empty_cache() gc.collect() a__ , a__ = UNetaDModel.from_pretrained( 'fusing/unet-ldm-dummy-update' ,output_loading_info=_A ,low_cpu_mem_usage=_A ) model_normal_load.to(_A ) model_normal_load.eval() a__ = model_normal_load(_A ,_A )['sample'] assert torch_all_close(_A ,_A ,rtol=1E-3 ) def lowerCamelCase__( self :List[Any] ) -> str: a__ = UNetaDModel.from_pretrained('fusing/unet-ldm-dummy-update' ) model.eval() model.to(_A ) a__ = torch.randn( 1 ,model.config.in_channels ,model.config.sample_size ,model.config.sample_size ,generator=torch.manual_seed(0 ) ,) a__ = noise.to(_A ) a__ = torch.tensor([10] * noise.shape[0] ).to(_A ) with torch.no_grad(): a__ = model(_A ,_A ).sample a__ = output[0, -1, -3:, -3:].flatten().cpu() # fmt: off a__ = torch.tensor([-13.32_58, -20.11_00, -15.98_73, -17.66_17, -23.05_96, -17.94_19, -13.36_75, -16.18_89, -12.38_00] ) # fmt: on self.assertTrue(torch_all_close(_A ,_A ,rtol=1E-3 ) ) class snake_case_ (a_ , a_ , unittest.TestCase ): UpperCAmelCase__ : Optional[Any] = UNetaDModel UpperCAmelCase__ : Dict = '''sample''' @property def lowerCamelCase__( self :str ,__snake_case :Dict=(32, 32) ) -> Dict: a__ = 4 a__ = 3 a__ = floats_tensor((batch_size, num_channels) + sizes ).to(_A ) a__ = torch.tensor(batch_size * [10] ).to(dtype=torch.intaa ,device=_A ) return {"sample": noise, "timestep": time_step} @property def lowerCamelCase__( self :Union[str, Any] ) -> Tuple: return (3, 32, 32) @property def lowerCamelCase__( self :str ) -> str: return (3, 32, 32) def lowerCamelCase__( self :List[Any] ) -> List[str]: a__ = { 'block_out_channels': [32, 64, 64, 64], 'in_channels': 3, 'layers_per_block': 1, 'out_channels': 3, 'time_embedding_type': 'fourier', 'norm_eps': 1E-6, 'mid_block_scale_factor': math.sqrt(2.0 ), 'norm_num_groups': None, 'down_block_types': [ 'SkipDownBlock2D', 'AttnSkipDownBlock2D', 'SkipDownBlock2D', 'SkipDownBlock2D', ], 'up_block_types': [ 'SkipUpBlock2D', 'SkipUpBlock2D', 'AttnSkipUpBlock2D', 'SkipUpBlock2D', ], } a__ = self.dummy_input return init_dict, inputs_dict @slow def lowerCamelCase__( self :List[Any] ) -> Optional[Any]: a__ , a__ = UNetaDModel.from_pretrained('google/ncsnpp-celebahq-256' ,output_loading_info=_A ) self.assertIsNotNone(_A ) self.assertEqual(len(loading_info['missing_keys'] ) ,0 ) model.to(_A ) a__ = self.dummy_input a__ = floats_tensor((4, 3) + (2_56, 2_56) ).to(_A ) a__ = noise a__ = model(**_A ) assert image is not None, "Make sure output is not None" @slow def lowerCamelCase__( self :Tuple ) -> List[Any]: a__ = UNetaDModel.from_pretrained('google/ncsnpp-celebahq-256' ) model.to(_A ) a__ = 4 a__ = 3 a__ = (2_56, 2_56) a__ = torch.ones((batch_size, num_channels) + sizes ).to(_A ) a__ = torch.tensor(batch_size * [1E-4] ).to(_A ) with torch.no_grad(): a__ = model(_A ,_A ).sample a__ = output[0, -3:, -3:, -1].flatten().cpu() # fmt: off a__ = torch.tensor([-48_42.86_91, -64_99.66_31, -38_00.19_53, -79_78.26_86, -1_09_80.71_29, -2_00_28.85_35, 81_48.28_22, 23_42.29_05, 5_67.76_08] ) # fmt: on self.assertTrue(torch_all_close(_A ,_A ,rtol=1E-2 ) ) def lowerCamelCase__( self :Any ) -> int: a__ = UNetaDModel.from_pretrained('fusing/ncsnpp-ffhq-ve-dummy-update' ) model.to(_A ) a__ = 4 a__ = 3 a__ = (32, 32) a__ = torch.ones((batch_size, num_channels) + sizes ).to(_A ) a__ = torch.tensor(batch_size * [1E-4] ).to(_A ) with torch.no_grad(): a__ = model(_A ,_A ).sample a__ = output[0, -3:, -3:, -1].flatten().cpu() # fmt: off a__ = torch.tensor([-0.03_25, -0.09_00, -0.08_69, -0.03_32, -0.07_25, -0.02_70, -0.01_01, 0.02_27, 0.02_56] ) # fmt: on self.assertTrue(torch_all_close(_A ,_A ,rtol=1E-2 ) ) def lowerCamelCase__( self :List[str] ) -> Optional[int]: pass
240
from typing import List, Optional, Tuple, Union import torch from ...models import UNetaDModel from ...schedulers import KarrasVeScheduler from ...utils import randn_tensor from ..pipeline_utils import DiffusionPipeline, ImagePipelineOutput class A_ (a_ ): UpperCAmelCase__ = 42 UpperCAmelCase__ = 42 def __init__( self , _A , _A ): '''simple docstring''' super().__init__() self.register_modules(unet=_A , scheduler=_A ) @torch.no_grad() def __call__( self , _A = 1 , _A = 5_0 , _A = None , _A = "pil" , _A = True , **_A , ): '''simple docstring''' UpperCAmelCase = self.unet.config.sample_size UpperCAmelCase = (batch_size, 3, img_size, img_size) UpperCAmelCase = self.unet # sample x_0 ~ N(0, sigma_0^2 * I) UpperCAmelCase = randn_tensor(_A , generator=_A , device=self.device ) * self.scheduler.init_noise_sigma self.scheduler.set_timesteps(_A ) for t in self.progress_bar(self.scheduler.timesteps ): # here sigma_t == t_i from the paper UpperCAmelCase = self.scheduler.schedule[t] UpperCAmelCase = self.scheduler.schedule[t - 1] if t > 0 else 0 # 1. Select temporarily increased noise level sigma_hat # 2. Add new noise to move from sample_i to sample_hat UpperCAmelCase , UpperCAmelCase = self.scheduler.add_noise_to_input(_A , _A , generator=_A ) # 3. Predict the noise residual given the noise magnitude `sigma_hat` # The model inputs and output are adjusted by following eq. (213) in [1]. UpperCAmelCase = (sigma_hat / 2) * model((sample_hat + 1) / 2 , sigma_hat / 2 ).sample # 4. Evaluate dx/dt at sigma_hat # 5. Take Euler step from sigma to sigma_prev UpperCAmelCase = self.scheduler.step(_A , _A , _A , _A ) if sigma_prev != 0: # 6. Apply 2nd order correction # The model inputs and output are adjusted by following eq. (213) in [1]. UpperCAmelCase = (sigma_prev / 2) * model((step_output.prev_sample + 1) / 2 , sigma_prev / 2 ).sample UpperCAmelCase = self.scheduler.step_correct( _A , _A , _A , _A , step_output.prev_sample , step_output['''derivative'''] , ) UpperCAmelCase = step_output.prev_sample UpperCAmelCase = (sample / 2 + 0.5).clamp(0 , 1 ) UpperCAmelCase = sample.cpu().permute(0 , 2 , 3 , 1 ).numpy() if output_type == "pil": UpperCAmelCase = self.numpy_to_pil(_A ) if not return_dict: return (image,) return ImagePipelineOutput(images=_A )
273
0
'''simple docstring''' import tempfile import torch from diffusers import ( DEISMultistepScheduler, DPMSolverMultistepScheduler, DPMSolverSinglestepScheduler, UniPCMultistepScheduler, ) from .test_schedulers import SchedulerCommonTest class A ( a_ ): '''simple docstring''' A = (DEISMultistepScheduler,) A = (("num_inference_steps", 2_5),) def a_ (self , **_UpperCAmelCase ) -> List[Any]: __UpperCamelCase : Dict = { "num_train_timesteps": 1_0_0_0, "beta_start": 0.0_001, "beta_end": 0.02, "beta_schedule": "linear", "solver_order": 2, } config.update(**_A ) return config def a_ (self , _UpperCAmelCase=0 , **_UpperCAmelCase ) -> Any: __UpperCamelCase : Any = dict(self.forward_default_kwargs ) __UpperCamelCase : Optional[int] = kwargs.pop("num_inference_steps" , _A ) __UpperCamelCase : Union[str, Any] = self.dummy_sample __UpperCamelCase : Optional[int] = 0.1 * sample __UpperCamelCase : Optional[Any] = [residual + 0.2, residual + 0.15, residual + 0.10] for scheduler_class in self.scheduler_classes: __UpperCamelCase : Dict = self.get_scheduler_config(**_A ) __UpperCamelCase : Union[str, Any] = scheduler_class(**_A ) scheduler.set_timesteps(_A ) # copy over dummy past residuals __UpperCamelCase : List[str] = dummy_past_residuals[: scheduler.config.solver_order] with tempfile.TemporaryDirectory() as tmpdirname: scheduler.save_config(_A ) __UpperCamelCase : Dict = scheduler_class.from_pretrained(_A ) new_scheduler.set_timesteps(_A ) # copy over dummy past residuals __UpperCamelCase : Optional[Any] = dummy_past_residuals[: new_scheduler.config.solver_order] __UpperCamelCase , __UpperCamelCase : Dict = sample, sample for t in range(_A , time_step + scheduler.config.solver_order + 1 ): __UpperCamelCase : Optional[int] = scheduler.step(_A , _A , _A , **_A ).prev_sample __UpperCamelCase : List[str] = new_scheduler.step(_A , _A , _A , **_A ).prev_sample assert torch.sum(torch.abs(output - new_output ) ) < 1E-5, "Scheduler outputs are not identical" def a_ (self ) -> Optional[int]: pass def a_ (self , _UpperCAmelCase=0 , **_UpperCAmelCase ) -> Tuple: __UpperCamelCase : Union[str, Any] = dict(self.forward_default_kwargs ) __UpperCamelCase : Dict = kwargs.pop("num_inference_steps" , _A ) __UpperCamelCase : Optional[Any] = self.dummy_sample __UpperCamelCase : Optional[Any] = 0.1 * sample __UpperCamelCase : List[str] = [residual + 0.2, residual + 0.15, residual + 0.10] for scheduler_class in self.scheduler_classes: __UpperCamelCase : Optional[int] = self.get_scheduler_config() __UpperCamelCase : Dict = scheduler_class(**_A ) scheduler.set_timesteps(_A ) # copy over dummy past residuals (must be after setting timesteps) __UpperCamelCase : Union[str, Any] = dummy_past_residuals[: scheduler.config.solver_order] with tempfile.TemporaryDirectory() as tmpdirname: scheduler.save_config(_A ) __UpperCamelCase : Tuple = scheduler_class.from_pretrained(_A ) # copy over dummy past residuals new_scheduler.set_timesteps(_A ) # copy over dummy past residual (must be after setting timesteps) __UpperCamelCase : Union[str, Any] = dummy_past_residuals[: new_scheduler.config.solver_order] __UpperCamelCase : Dict = scheduler.step(_A , _A , _A , **_A ).prev_sample __UpperCamelCase : Any = new_scheduler.step(_A , _A , _A , **_A ).prev_sample assert torch.sum(torch.abs(output - new_output ) ) < 1E-5, "Scheduler outputs are not identical" def a_ (self , _UpperCAmelCase=None , **_UpperCAmelCase ) -> str: if scheduler is None: __UpperCamelCase : Dict = self.scheduler_classes[0] __UpperCamelCase : List[str] = self.get_scheduler_config(**_A ) __UpperCamelCase : Dict = scheduler_class(**_A ) __UpperCamelCase : Any = self.scheduler_classes[0] __UpperCamelCase : int = self.get_scheduler_config(**_A ) __UpperCamelCase : Tuple = scheduler_class(**_A ) __UpperCamelCase : List[Any] = 1_0 __UpperCamelCase : str = self.dummy_model() __UpperCamelCase : Union[str, Any] = self.dummy_sample_deter scheduler.set_timesteps(_A ) for i, t in enumerate(scheduler.timesteps ): __UpperCamelCase : Tuple = model(_A , _A ) __UpperCamelCase : Dict = scheduler.step(_A , _A , _A ).prev_sample return sample def a_ (self ) -> Dict: __UpperCamelCase : Tuple = dict(self.forward_default_kwargs ) __UpperCamelCase : List[str] = kwargs.pop("num_inference_steps" , _A ) for scheduler_class in self.scheduler_classes: __UpperCamelCase : List[Any] = self.get_scheduler_config() __UpperCamelCase : List[Any] = scheduler_class(**_A ) __UpperCamelCase : int = self.dummy_sample __UpperCamelCase : Dict = 0.1 * sample if num_inference_steps is not None and hasattr(_A , "set_timesteps" ): scheduler.set_timesteps(_A ) elif num_inference_steps is not None and not hasattr(_A , "set_timesteps" ): __UpperCamelCase : List[Any] = num_inference_steps # copy over dummy past residuals (must be done after set_timesteps) __UpperCamelCase : List[Any] = [residual + 0.2, residual + 0.15, residual + 0.10] __UpperCamelCase : List[Any] = dummy_past_residuals[: scheduler.config.solver_order] __UpperCamelCase : Union[str, Any] = scheduler.timesteps[5] __UpperCamelCase : List[Any] = scheduler.timesteps[6] __UpperCamelCase : Tuple = scheduler.step(_A , _A , _A , **_A ).prev_sample __UpperCamelCase : Any = scheduler.step(_A , _A , _A , **_A ).prev_sample self.assertEqual(output_a.shape , sample.shape ) self.assertEqual(output_a.shape , output_a.shape ) def a_ (self ) -> Optional[int]: __UpperCamelCase : Dict = DEISMultistepScheduler(**self.get_scheduler_config() ) __UpperCamelCase : Tuple = self.full_loop(scheduler=_A ) __UpperCamelCase : Tuple = torch.mean(torch.abs(_A ) ) assert abs(result_mean.item() - 0.23_916 ) < 1E-3 __UpperCamelCase : int = DPMSolverSinglestepScheduler.from_config(scheduler.config ) __UpperCamelCase : Union[str, Any] = DPMSolverMultistepScheduler.from_config(scheduler.config ) __UpperCamelCase : Optional[Any] = UniPCMultistepScheduler.from_config(scheduler.config ) __UpperCamelCase : List[Any] = DEISMultistepScheduler.from_config(scheduler.config ) __UpperCamelCase : Any = self.full_loop(scheduler=_A ) __UpperCamelCase : str = torch.mean(torch.abs(_A ) ) assert abs(result_mean.item() - 0.23_916 ) < 1E-3 def a_ (self ) -> str: for timesteps in [2_5, 5_0, 1_0_0, 9_9_9, 1_0_0_0]: self.check_over_configs(num_train_timesteps=_A ) def a_ (self ) -> List[str]: self.check_over_configs(thresholding=_A ) for order in [1, 2, 3]: for solver_type in ["logrho"]: for threshold in [0.5, 1.0, 2.0]: for prediction_type in ["epsilon", "sample"]: self.check_over_configs( thresholding=_A , prediction_type=_A , sample_max_value=_A , algorithm_type="deis" , solver_order=_A , solver_type=_A , ) def a_ (self ) -> str: for prediction_type in ["epsilon", "v_prediction"]: self.check_over_configs(prediction_type=_A ) def a_ (self ) -> Dict: for algorithm_type in ["deis"]: for solver_type in ["logrho"]: for order in [1, 2, 3]: for prediction_type in ["epsilon", "sample"]: self.check_over_configs( solver_order=_A , solver_type=_A , prediction_type=_A , algorithm_type=_A , ) __UpperCamelCase : Optional[Any] = self.full_loop( solver_order=_A , solver_type=_A , prediction_type=_A , algorithm_type=_A , ) assert not torch.isnan(_A ).any(), "Samples have nan numbers" def a_ (self ) -> Tuple: self.check_over_configs(lower_order_final=_A ) self.check_over_configs(lower_order_final=_A ) def a_ (self ) -> List[str]: for num_inference_steps in [1, 2, 3, 5, 1_0, 5_0, 1_0_0, 9_9_9, 1_0_0_0]: self.check_over_forward(num_inference_steps=_A , time_step=0 ) def a_ (self ) -> Union[str, Any]: __UpperCamelCase : List[Any] = self.full_loop() __UpperCamelCase : List[Any] = torch.mean(torch.abs(_A ) ) assert abs(result_mean.item() - 0.23_916 ) < 1E-3 def a_ (self ) -> Any: __UpperCamelCase : Union[str, Any] = self.full_loop(prediction_type="v_prediction" ) __UpperCamelCase : Optional[Any] = torch.mean(torch.abs(_A ) ) assert abs(result_mean.item() - 0.091 ) < 1E-3 def a_ (self ) -> Dict: __UpperCamelCase : Optional[Any] = self.scheduler_classes[0] __UpperCamelCase : List[str] = self.get_scheduler_config(thresholding=_A , dynamic_thresholding_ratio=0 ) __UpperCamelCase : Optional[int] = scheduler_class(**_A ) __UpperCamelCase : Any = 1_0 __UpperCamelCase : Optional[Any] = self.dummy_model() __UpperCamelCase : Optional[int] = self.dummy_sample_deter.half() scheduler.set_timesteps(_A ) for i, t in enumerate(scheduler.timesteps ): __UpperCamelCase : Any = model(_A , _A ) __UpperCamelCase : Optional[int] = scheduler.step(_A , _A , _A ).prev_sample assert sample.dtype == torch.floataa
298
import itertools import random import unittest import numpy as np from transformers import BatchFeature, SpeechTaFeatureExtractor from transformers.testing_utils import require_torch from transformers.utils.import_utils import is_torch_available from ...test_sequence_feature_extraction_common import SequenceFeatureExtractionTestMixin if is_torch_available(): import torch __A : str = random.Random() def __SCREAMING_SNAKE_CASE ( UpperCamelCase__ , UpperCamelCase__=1.0 , UpperCamelCase__=None , UpperCamelCase__=None ) -> Tuple: '''simple docstring''' if rng is None: UpperCAmelCase = global_rng UpperCAmelCase = [] for batch_idx in range(shape[0] ): values.append([] ) for _ in range(shape[1] ): values[-1].append(rng.random() * scale ) return values @require_torch class A_ (unittest.TestCase ): def __init__( self , _A , _A=7 , _A=4_0_0 , _A=2_0_0_0 , _A=1 , _A=0.0 , _A=1_6_0_0_0 , _A=True , _A=8_0 , _A=1_6 , _A=6_4 , _A="hann_window" , _A=8_0 , _A=7_6_0_0 , _A=1E-10 , _A=True , ): '''simple docstring''' UpperCAmelCase = parent UpperCAmelCase = batch_size UpperCAmelCase = min_seq_length UpperCAmelCase = max_seq_length UpperCAmelCase = (self.max_seq_length - self.min_seq_length) // (self.batch_size - 1) UpperCAmelCase = feature_size UpperCAmelCase = padding_value UpperCAmelCase = sampling_rate UpperCAmelCase = do_normalize UpperCAmelCase = num_mel_bins UpperCAmelCase = hop_length UpperCAmelCase = win_length UpperCAmelCase = win_function UpperCAmelCase = fmin UpperCAmelCase = fmax UpperCAmelCase = mel_floor UpperCAmelCase = return_attention_mask def _lowercase ( self ): '''simple docstring''' return { "feature_size": self.feature_size, "padding_value": self.padding_value, "sampling_rate": self.sampling_rate, "do_normalize": self.do_normalize, "num_mel_bins": self.num_mel_bins, "hop_length": self.hop_length, "win_length": self.win_length, "win_function": self.win_function, "fmin": self.fmin, "fmax": self.fmax, "mel_floor": self.mel_floor, "return_attention_mask": self.return_attention_mask, } def _lowercase ( self , _A=False , _A=False ): '''simple docstring''' def _flatten(_A ): return list(itertools.chain(*_A ) ) if equal_length: UpperCAmelCase = floats_list((self.batch_size, self.max_seq_length) ) else: # make sure that inputs increase in size UpperCAmelCase = [ _flatten(floats_list((x, self.feature_size) ) ) for x in range(self.min_seq_length , self.max_seq_length , self.seq_length_diff ) ] if numpify: UpperCAmelCase = [np.asarray(_A ) for x in speech_inputs] return speech_inputs def _lowercase ( self , _A=False , _A=False ): '''simple docstring''' if equal_length: UpperCAmelCase = [floats_list((self.max_seq_length, self.num_mel_bins) ) for _ in range(self.batch_size )] else: # make sure that inputs increase in size UpperCAmelCase = [ floats_list((x, self.num_mel_bins) ) for x in range(self.min_seq_length , self.max_seq_length , self.seq_length_diff ) ] if numpify: UpperCAmelCase = [np.asarray(_A ) for x in speech_inputs] return speech_inputs @require_torch class A_ (a_ , unittest.TestCase ): UpperCAmelCase__ = SpeechTaFeatureExtractor def _lowercase ( self ): '''simple docstring''' UpperCAmelCase = SpeechTaFeatureExtractionTester(self ) def _lowercase ( self , _A ): '''simple docstring''' self.assertTrue(np.all(np.mean(_A , axis=0 ) < 1E-3 ) ) self.assertTrue(np.all(np.abs(np.var(_A , axis=0 ) - 1 ) < 1E-3 ) ) def _lowercase ( self ): '''simple docstring''' UpperCAmelCase = self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() ) # create three inputs of length 800, 1000, and 1200 UpperCAmelCase = [floats_list((1, x) )[0] for x in range(8_0_0 , 1_4_0_0 , 2_0_0 )] UpperCAmelCase = [np.asarray(_A ) for speech_input in speech_inputs] # Test not batched input UpperCAmelCase = feat_extract(speech_inputs[0] , return_tensors='''np''' ).input_values UpperCAmelCase = feat_extract(np_speech_inputs[0] , return_tensors='''np''' ).input_values self.assertTrue(np.allclose(_A , _A , atol=1E-3 ) ) # Test batched UpperCAmelCase = feat_extract(_A , return_tensors='''np''' ).input_values UpperCAmelCase = feat_extract(_A , return_tensors='''np''' ).input_values for enc_seq_a, enc_seq_a in zip(_A , _A ): self.assertTrue(np.allclose(_A , _A , atol=1E-3 ) ) def _lowercase ( self ): '''simple docstring''' UpperCAmelCase = self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() ) UpperCAmelCase = [floats_list((1, x) )[0] for x in range(8_0_0 , 1_4_0_0 , 2_0_0 )] UpperCAmelCase = ['''longest''', '''max_length''', '''do_not_pad'''] UpperCAmelCase = [None, 1_6_0_0, None] for max_length, padding in zip(_A , _A ): UpperCAmelCase = feat_extract(_A , padding=_A , max_length=_A , return_tensors='''np''' ) UpperCAmelCase = processed.input_values self._check_zero_mean_unit_variance(input_values[0][:8_0_0] ) self.assertTrue(input_values[0][8_0_0:].sum() < 1E-6 ) self._check_zero_mean_unit_variance(input_values[1][:1_0_0_0] ) self.assertTrue(input_values[0][1_0_0_0:].sum() < 1E-6 ) self._check_zero_mean_unit_variance(input_values[2][:1_2_0_0] ) def _lowercase ( self ): '''simple docstring''' UpperCAmelCase = self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() ) UpperCAmelCase = range(8_0_0 , 1_4_0_0 , 2_0_0 ) UpperCAmelCase = [floats_list((1, x) )[0] for x in lengths] UpperCAmelCase = ['''longest''', '''max_length''', '''do_not_pad'''] UpperCAmelCase = [None, 1_6_0_0, None] for max_length, padding in zip(_A , _A ): UpperCAmelCase = feat_extract(_A , max_length=_A , padding=_A ) UpperCAmelCase = processed.input_values self._check_zero_mean_unit_variance(input_values[0][:8_0_0] ) self._check_zero_mean_unit_variance(input_values[1][:1_0_0_0] ) self._check_zero_mean_unit_variance(input_values[2][:1_2_0_0] ) def _lowercase ( self ): '''simple docstring''' UpperCAmelCase = self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() ) UpperCAmelCase = [floats_list((1, x) )[0] for x in range(8_0_0 , 1_4_0_0 , 2_0_0 )] UpperCAmelCase = feat_extract( _A , truncation=_A , max_length=1_0_0_0 , padding='''max_length''' , return_tensors='''np''' ) UpperCAmelCase = processed.input_values self._check_zero_mean_unit_variance(input_values[0, :8_0_0] ) self._check_zero_mean_unit_variance(input_values[1] ) self._check_zero_mean_unit_variance(input_values[2] ) def _lowercase ( self ): '''simple docstring''' UpperCAmelCase = self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() ) UpperCAmelCase = [floats_list((1, x) )[0] for x in range(8_0_0 , 1_4_0_0 , 2_0_0 )] UpperCAmelCase = feat_extract( _A , truncation=_A , max_length=1_0_0_0 , padding='''longest''' , return_tensors='''np''' ) UpperCAmelCase = processed.input_values self._check_zero_mean_unit_variance(input_values[0, :8_0_0] ) self._check_zero_mean_unit_variance(input_values[1, :1_0_0_0] ) self._check_zero_mean_unit_variance(input_values[2] ) # make sure that if max_length < longest -> then pad to max_length self.assertTrue(input_values.shape == (3, 1_0_0_0) ) UpperCAmelCase = [floats_list((1, x) )[0] for x in range(8_0_0 , 1_4_0_0 , 2_0_0 )] UpperCAmelCase = feat_extract( _A , truncation=_A , max_length=2_0_0_0 , padding='''longest''' , return_tensors='''np''' ) UpperCAmelCase = processed.input_values self._check_zero_mean_unit_variance(input_values[0, :8_0_0] ) self._check_zero_mean_unit_variance(input_values[1, :1_0_0_0] ) self._check_zero_mean_unit_variance(input_values[2] ) # make sure that if max_length > longest -> then pad to longest self.assertTrue(input_values.shape == (3, 1_2_0_0) ) def _lowercase ( self ): '''simple docstring''' UpperCAmelCase = self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() ) UpperCAmelCase = np.random.rand(1_0_0 ).astype(np.floataa ) UpperCAmelCase = np_speech_inputs.tolist() for inputs in [py_speech_inputs, np_speech_inputs]: UpperCAmelCase = feature_extractor.pad([{'''input_values''': inputs}] , return_tensors='''np''' ) self.assertTrue(np_processed.input_values.dtype == np.floataa ) UpperCAmelCase = feature_extractor.pad([{'''input_values''': inputs}] , return_tensors='''pt''' ) self.assertTrue(pt_processed.input_values.dtype == torch.floataa ) def _lowercase ( self ): '''simple docstring''' UpperCAmelCase = self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() ) # create three inputs of length 800, 1000, and 1200 UpperCAmelCase = [floats_list((1, x) )[0] for x in range(8_0_0 , 1_4_0_0 , 2_0_0 )] UpperCAmelCase = [np.asarray(_A ) for speech_input in speech_inputs] # Test feature size UpperCAmelCase = feature_extractor(audio_target=_A , padding=_A , return_tensors='''np''' ).input_values self.assertTrue(input_values.ndim == 3 ) self.assertTrue(input_values.shape[-1] == feature_extractor.num_mel_bins ) # Test not batched input UpperCAmelCase = feature_extractor(speech_inputs[0] , return_tensors='''np''' ).input_values UpperCAmelCase = feature_extractor(np_speech_inputs[0] , return_tensors='''np''' ).input_values self.assertTrue(np.allclose(_A , _A , atol=1E-3 ) ) # Test batched UpperCAmelCase = feature_extractor(_A , return_tensors='''np''' ).input_values UpperCAmelCase = feature_extractor(_A , return_tensors='''np''' ).input_values for enc_seq_a, enc_seq_a in zip(_A , _A ): self.assertTrue(np.allclose(_A , _A , atol=1E-3 ) ) # Test 2-D numpy arrays are batched. UpperCAmelCase = [floats_list((1, x) )[0] for x in (8_0_0, 8_0_0, 8_0_0)] UpperCAmelCase = np.asarray(_A ) UpperCAmelCase = feature_extractor(_A , return_tensors='''np''' ).input_values UpperCAmelCase = feature_extractor(_A , return_tensors='''np''' ).input_values for enc_seq_a, enc_seq_a in zip(_A , _A ): self.assertTrue(np.allclose(_A , _A , atol=1E-3 ) ) def _lowercase ( self ): '''simple docstring''' UpperCAmelCase = self.feat_extract_tester.prepare_inputs_for_target() UpperCAmelCase = self.feature_extraction_class(**self.feat_extract_dict ) UpperCAmelCase = feat_extract.model_input_names[0] UpperCAmelCase = BatchFeature({input_name: speech_inputs} ) self.assertTrue(all(len(_A ) == len(_A ) for x, y in zip(_A , processed_features[input_name] ) ) ) UpperCAmelCase = self.feat_extract_tester.prepare_inputs_for_target(equal_length=_A ) UpperCAmelCase = BatchFeature({input_name: speech_inputs} , tensor_type='''np''' ) UpperCAmelCase = processed_features[input_name] if len(batch_features_input.shape ) < 3: UpperCAmelCase = batch_features_input[:, :, None] self.assertTrue( batch_features_input.shape == (self.feat_extract_tester.batch_size, len(speech_inputs[0] ), self.feat_extract_tester.num_mel_bins) ) @require_torch def _lowercase ( self ): '''simple docstring''' UpperCAmelCase = self.feat_extract_tester.prepare_inputs_for_target(equal_length=_A ) UpperCAmelCase = self.feature_extraction_class(**self.feat_extract_dict ) UpperCAmelCase = feat_extract.model_input_names[0] UpperCAmelCase = BatchFeature({input_name: speech_inputs} , tensor_type='''pt''' ) UpperCAmelCase = processed_features[input_name] if len(batch_features_input.shape ) < 3: UpperCAmelCase = batch_features_input[:, :, None] self.assertTrue( batch_features_input.shape == (self.feat_extract_tester.batch_size, len(speech_inputs[0] ), self.feat_extract_tester.num_mel_bins) ) @require_torch def _lowercase ( self ): '''simple docstring''' UpperCAmelCase = self.feature_extraction_class(**self.feat_extract_dict ) UpperCAmelCase = self.feat_extract_tester.prepare_inputs_for_target() UpperCAmelCase = feat_extract.model_input_names[0] UpperCAmelCase = BatchFeature({input_name: speech_inputs} ) UpperCAmelCase = feat_extract.num_mel_bins # hack! UpperCAmelCase = feat_extract.pad(_A , padding='''longest''' , return_tensors='''np''' )[input_name] UpperCAmelCase = feat_extract.pad(_A , padding='''longest''' , return_tensors='''pt''' )[input_name] self.assertTrue(abs(input_np.astype(np.floataa ).sum() - input_pt.numpy().astype(np.floataa ).sum() ) < 1E-2 ) def _lowercase ( self ): '''simple docstring''' UpperCAmelCase = self.feat_extract_dict UpperCAmelCase = True UpperCAmelCase = self.feature_extraction_class(**_A ) UpperCAmelCase = self.feat_extract_tester.prepare_inputs_for_target() UpperCAmelCase = [len(_A ) for x in speech_inputs] UpperCAmelCase = feat_extract.model_input_names[0] UpperCAmelCase = BatchFeature({input_name: speech_inputs} ) UpperCAmelCase = feat_extract.num_mel_bins # hack! UpperCAmelCase = feat_extract.pad(_A , padding='''longest''' , return_tensors='''np''' ) self.assertIn('''attention_mask''' , _A ) self.assertListEqual(list(processed.attention_mask.shape ) , list(processed[input_name].shape[:2] ) ) self.assertListEqual(processed.attention_mask.sum(-1 ).tolist() , _A ) def _lowercase ( self ): '''simple docstring''' UpperCAmelCase = self.feat_extract_dict UpperCAmelCase = True UpperCAmelCase = self.feature_extraction_class(**_A ) UpperCAmelCase = self.feat_extract_tester.prepare_inputs_for_target() UpperCAmelCase = [len(_A ) for x in speech_inputs] UpperCAmelCase = feat_extract.model_input_names[0] UpperCAmelCase = BatchFeature({input_name: speech_inputs} ) UpperCAmelCase = min(_A ) UpperCAmelCase = feat_extract.num_mel_bins # hack! UpperCAmelCase = feat_extract.pad( _A , padding='''max_length''' , max_length=_A , truncation=_A , return_tensors='''np''' ) self.assertIn('''attention_mask''' , _A ) self.assertListEqual( list(processed_pad.attention_mask.shape ) , [processed_pad[input_name].shape[0], max_length] ) self.assertListEqual( processed_pad.attention_mask[:, :max_length].sum(-1 ).tolist() , [max_length for x in speech_inputs] ) def _lowercase ( self , _A ): '''simple docstring''' from datasets import load_dataset UpperCAmelCase = load_dataset('''hf-internal-testing/librispeech_asr_dummy''' , '''clean''' , split='''validation''' ) # automatic decoding with librispeech UpperCAmelCase = ds.sort('''id''' ).select(range(_A ) )[:num_samples]['''audio'''] return [x["array"] for x in speech_samples] def _lowercase ( self ): '''simple docstring''' UpperCAmelCase = torch.tensor( [2.3804E-03, 2.0752E-03, 1.9836E-03, 2.1057E-03, 1.6174E-03, 3.0518E-04, 9.1553E-05, 3.3569E-04, 9.7656E-04, 1.8311E-03, 2.0142E-03, 2.1057E-03, 1.7395E-03, 4.5776E-04, -3.9673E-04, 4.5776E-04, 1.0071E-03, 9.1553E-05, 4.8828E-04, 1.1597E-03, 7.3242E-04, 9.4604E-04, 1.8005E-03, 1.8311E-03, 8.8501E-04, 4.2725E-04, 4.8828E-04, 7.3242E-04, 1.0986E-03, 2.1057E-03] ) # fmt: on UpperCAmelCase = self._load_datasamples(1 ) UpperCAmelCase = SpeechTaFeatureExtractor() UpperCAmelCase = feature_extractor(_A , return_tensors='''pt''' ).input_values self.assertEquals(input_values.shape , (1, 9_3_6_8_0) ) self.assertTrue(torch.allclose(input_values[0, :3_0] , _A , atol=1E-6 ) ) def _lowercase ( self ): '''simple docstring''' UpperCAmelCase = torch.tensor( [-2.68_70, -3.01_04, -3.13_56, -3.53_52, -3.00_44, -3.03_53, -3.47_19, -3.67_77, -3.15_20, -2.94_35, -2.65_53, -2.87_95, -2.99_44, -2.59_21, -3.02_79, -3.03_86, -3.08_64, -3.12_91, -3.23_53, -2.74_44, -2.68_31, -2.72_87, -3.17_61, -3.15_71, -3.27_26, -3.05_82, -3.10_07, -3.45_33, -3.46_95, -3.09_98] ) # fmt: on UpperCAmelCase = self._load_datasamples(1 ) UpperCAmelCase = SpeechTaFeatureExtractor() UpperCAmelCase = feature_extractor(audio_target=_A , return_tensors='''pt''' ).input_values self.assertEquals(input_values.shape , (1, 3_6_6, 8_0) ) self.assertTrue(torch.allclose(input_values[0, 0, :3_0] , _A , atol=1E-4 ) )
273
0
import itertools import random import unittest import numpy as np from transformers import BatchFeature, SpeechTaFeatureExtractor from transformers.testing_utils import require_torch from transformers.utils.import_utils import is_torch_available from ...test_sequence_feature_extraction_common import SequenceFeatureExtractionTestMixin if is_torch_available(): import torch __snake_case = random.Random() def _A ( _lowercase , _lowercase=1.0 , _lowercase=None , _lowercase=None ) -> Tuple: """simple docstring""" if rng is None: __UpperCamelCase = global_rng __UpperCamelCase = [] for batch_idx in range(shape[0] ): values.append([] ) for _ in range(shape[1] ): values[-1].append(rng.random() * scale ) return values @require_torch class __lowerCamelCase (unittest.TestCase ): def __init__( self: Optional[int],A_: Union[str, Any],A_: Optional[int]=7,A_: int=400,A_: Union[str, Any]=2000,A_: Union[str, Any]=1,A_: Tuple=0.0,A_: Any=1_6000,A_: List[Any]=True,A_: Any=80,A_: str=16,A_: List[str]=64,A_: Tuple="hann_window",A_: Optional[Any]=80,A_: Tuple=7600,A_: str=1E-10,A_: str=True,): '''simple docstring''' __UpperCamelCase = parent __UpperCamelCase = batch_size __UpperCamelCase = min_seq_length __UpperCamelCase = max_seq_length __UpperCamelCase = (self.max_seq_length - self.min_seq_length) // (self.batch_size - 1) __UpperCamelCase = feature_size __UpperCamelCase = padding_value __UpperCamelCase = sampling_rate __UpperCamelCase = do_normalize __UpperCamelCase = num_mel_bins __UpperCamelCase = hop_length __UpperCamelCase = win_length __UpperCamelCase = win_function __UpperCamelCase = fmin __UpperCamelCase = fmax __UpperCamelCase = mel_floor __UpperCamelCase = return_attention_mask def snake_case_ ( self: List[str] ): '''simple docstring''' return { "feature_size": self.feature_size, "padding_value": self.padding_value, "sampling_rate": self.sampling_rate, "do_normalize": self.do_normalize, "num_mel_bins": self.num_mel_bins, "hop_length": self.hop_length, "win_length": self.win_length, "win_function": self.win_function, "fmin": self.fmin, "fmax": self.fmax, "mel_floor": self.mel_floor, "return_attention_mask": self.return_attention_mask, } def snake_case_ ( self: Dict,A_: str=False,A_: Optional[Any]=False ): '''simple docstring''' def _flatten(A_: str ): return list(itertools.chain(*_A ) ) if equal_length: __UpperCamelCase = floats_list((self.batch_size, self.max_seq_length) ) else: # make sure that inputs increase in size __UpperCamelCase = [ _flatten(floats_list((x, self.feature_size) ) ) for x in range(self.min_seq_length,self.max_seq_length,self.seq_length_diff ) ] if numpify: __UpperCamelCase = [np.asarray(_A ) for x in speech_inputs] return speech_inputs def snake_case_ ( self: Optional[Any],A_: Optional[Any]=False,A_: Tuple=False ): '''simple docstring''' if equal_length: __UpperCamelCase = [floats_list((self.max_seq_length, self.num_mel_bins) ) for _ in range(self.batch_size )] else: # make sure that inputs increase in size __UpperCamelCase = [ floats_list((x, self.num_mel_bins) ) for x in range(self.min_seq_length,self.max_seq_length,self.seq_length_diff ) ] if numpify: __UpperCamelCase = [np.asarray(_A ) for x in speech_inputs] return speech_inputs @require_torch class __lowerCamelCase (a_ , unittest.TestCase ): _lowercase = SpeechTaFeatureExtractor def snake_case_ ( self: Optional[int] ): '''simple docstring''' __UpperCamelCase = SpeechTaFeatureExtractionTester(self ) def snake_case_ ( self: Optional[int],A_: Optional[int] ): '''simple docstring''' self.assertTrue(np.all(np.mean(_A,axis=0 ) < 1E-3 ) ) self.assertTrue(np.all(np.abs(np.var(_A,axis=0 ) - 1 ) < 1E-3 ) ) def snake_case_ ( self: List[str] ): '''simple docstring''' __UpperCamelCase = self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() ) # create three inputs of length 800, 1000, and 1200 __UpperCamelCase = [floats_list((1, x) )[0] for x in range(800,1400,200 )] __UpperCamelCase = [np.asarray(_A ) for speech_input in speech_inputs] # Test not batched input __UpperCamelCase = feat_extract(speech_inputs[0],return_tensors='np' ).input_values __UpperCamelCase = feat_extract(np_speech_inputs[0],return_tensors='np' ).input_values self.assertTrue(np.allclose(_A,_A,atol=1E-3 ) ) # Test batched __UpperCamelCase = feat_extract(_A,return_tensors='np' ).input_values __UpperCamelCase = feat_extract(_A,return_tensors='np' ).input_values for enc_seq_a, enc_seq_a in zip(_A,_A ): self.assertTrue(np.allclose(_A,_A,atol=1E-3 ) ) def snake_case_ ( self: Tuple ): '''simple docstring''' __UpperCamelCase = self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() ) __UpperCamelCase = [floats_list((1, x) )[0] for x in range(800,1400,200 )] __UpperCamelCase = ['longest', 'max_length', 'do_not_pad'] __UpperCamelCase = [None, 1600, None] for max_length, padding in zip(_A,_A ): __UpperCamelCase = feat_extract(_A,padding=_A,max_length=_A,return_tensors='np' ) __UpperCamelCase = processed.input_values self._check_zero_mean_unit_variance(input_values[0][:800] ) self.assertTrue(input_values[0][800:].sum() < 1E-6 ) self._check_zero_mean_unit_variance(input_values[1][:1000] ) self.assertTrue(input_values[0][1000:].sum() < 1E-6 ) self._check_zero_mean_unit_variance(input_values[2][:1200] ) def snake_case_ ( self: Optional[int] ): '''simple docstring''' __UpperCamelCase = self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() ) __UpperCamelCase = range(800,1400,200 ) __UpperCamelCase = [floats_list((1, x) )[0] for x in lengths] __UpperCamelCase = ['longest', 'max_length', 'do_not_pad'] __UpperCamelCase = [None, 1600, None] for max_length, padding in zip(_A,_A ): __UpperCamelCase = feat_extract(_A,max_length=_A,padding=_A ) __UpperCamelCase = processed.input_values self._check_zero_mean_unit_variance(input_values[0][:800] ) self._check_zero_mean_unit_variance(input_values[1][:1000] ) self._check_zero_mean_unit_variance(input_values[2][:1200] ) def snake_case_ ( self: str ): '''simple docstring''' __UpperCamelCase = self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() ) __UpperCamelCase = [floats_list((1, x) )[0] for x in range(800,1400,200 )] __UpperCamelCase = feat_extract( _A,truncation=_A,max_length=1000,padding='max_length',return_tensors='np' ) __UpperCamelCase = processed.input_values self._check_zero_mean_unit_variance(input_values[0, :800] ) self._check_zero_mean_unit_variance(input_values[1] ) self._check_zero_mean_unit_variance(input_values[2] ) def snake_case_ ( self: int ): '''simple docstring''' __UpperCamelCase = self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() ) __UpperCamelCase = [floats_list((1, x) )[0] for x in range(800,1400,200 )] __UpperCamelCase = feat_extract( _A,truncation=_A,max_length=1000,padding='longest',return_tensors='np' ) __UpperCamelCase = processed.input_values self._check_zero_mean_unit_variance(input_values[0, :800] ) self._check_zero_mean_unit_variance(input_values[1, :1000] ) self._check_zero_mean_unit_variance(input_values[2] ) # make sure that if max_length < longest -> then pad to max_length self.assertTrue(input_values.shape == (3, 1000) ) __UpperCamelCase = [floats_list((1, x) )[0] for x in range(800,1400,200 )] __UpperCamelCase = feat_extract( _A,truncation=_A,max_length=2000,padding='longest',return_tensors='np' ) __UpperCamelCase = processed.input_values self._check_zero_mean_unit_variance(input_values[0, :800] ) self._check_zero_mean_unit_variance(input_values[1, :1000] ) self._check_zero_mean_unit_variance(input_values[2] ) # make sure that if max_length > longest -> then pad to longest self.assertTrue(input_values.shape == (3, 1200) ) def snake_case_ ( self: Dict ): '''simple docstring''' __UpperCamelCase = self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() ) __UpperCamelCase = np.random.rand(100 ).astype(np.floataa ) __UpperCamelCase = np_speech_inputs.tolist() for inputs in [py_speech_inputs, np_speech_inputs]: __UpperCamelCase = feature_extractor.pad([{'input_values': inputs}],return_tensors='np' ) self.assertTrue(np_processed.input_values.dtype == np.floataa ) __UpperCamelCase = feature_extractor.pad([{'input_values': inputs}],return_tensors='pt' ) self.assertTrue(pt_processed.input_values.dtype == torch.floataa ) def snake_case_ ( self: Dict ): '''simple docstring''' __UpperCamelCase = self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() ) # create three inputs of length 800, 1000, and 1200 __UpperCamelCase = [floats_list((1, x) )[0] for x in range(800,1400,200 )] __UpperCamelCase = [np.asarray(_A ) for speech_input in speech_inputs] # Test feature size __UpperCamelCase = feature_extractor(audio_target=_A,padding=_A,return_tensors='np' ).input_values self.assertTrue(input_values.ndim == 3 ) self.assertTrue(input_values.shape[-1] == feature_extractor.num_mel_bins ) # Test not batched input __UpperCamelCase = feature_extractor(speech_inputs[0],return_tensors='np' ).input_values __UpperCamelCase = feature_extractor(np_speech_inputs[0],return_tensors='np' ).input_values self.assertTrue(np.allclose(_A,_A,atol=1E-3 ) ) # Test batched __UpperCamelCase = feature_extractor(_A,return_tensors='np' ).input_values __UpperCamelCase = feature_extractor(_A,return_tensors='np' ).input_values for enc_seq_a, enc_seq_a in zip(_A,_A ): self.assertTrue(np.allclose(_A,_A,atol=1E-3 ) ) # Test 2-D numpy arrays are batched. __UpperCamelCase = [floats_list((1, x) )[0] for x in (800, 800, 800)] __UpperCamelCase = np.asarray(_A ) __UpperCamelCase = feature_extractor(_A,return_tensors='np' ).input_values __UpperCamelCase = feature_extractor(_A,return_tensors='np' ).input_values for enc_seq_a, enc_seq_a in zip(_A,_A ): self.assertTrue(np.allclose(_A,_A,atol=1E-3 ) ) def snake_case_ ( self: Union[str, Any] ): '''simple docstring''' __UpperCamelCase = self.feat_extract_tester.prepare_inputs_for_target() __UpperCamelCase = self.feature_extraction_class(**self.feat_extract_dict ) __UpperCamelCase = feat_extract.model_input_names[0] __UpperCamelCase = BatchFeature({input_name: speech_inputs} ) self.assertTrue(all(len(_A ) == len(_A ) for x, y in zip(_A,processed_features[input_name] ) ) ) __UpperCamelCase = self.feat_extract_tester.prepare_inputs_for_target(equal_length=_A ) __UpperCamelCase = BatchFeature({input_name: speech_inputs},tensor_type='np' ) __UpperCamelCase = processed_features[input_name] if len(batch_features_input.shape ) < 3: __UpperCamelCase = batch_features_input[:, :, None] self.assertTrue( batch_features_input.shape == (self.feat_extract_tester.batch_size, len(speech_inputs[0] ), self.feat_extract_tester.num_mel_bins) ) @require_torch def snake_case_ ( self: Optional[Any] ): '''simple docstring''' __UpperCamelCase = self.feat_extract_tester.prepare_inputs_for_target(equal_length=_A ) __UpperCamelCase = self.feature_extraction_class(**self.feat_extract_dict ) __UpperCamelCase = feat_extract.model_input_names[0] __UpperCamelCase = BatchFeature({input_name: speech_inputs},tensor_type='pt' ) __UpperCamelCase = processed_features[input_name] if len(batch_features_input.shape ) < 3: __UpperCamelCase = batch_features_input[:, :, None] self.assertTrue( batch_features_input.shape == (self.feat_extract_tester.batch_size, len(speech_inputs[0] ), self.feat_extract_tester.num_mel_bins) ) @require_torch def snake_case_ ( self: Optional[Any] ): '''simple docstring''' __UpperCamelCase = self.feature_extraction_class(**self.feat_extract_dict ) __UpperCamelCase = self.feat_extract_tester.prepare_inputs_for_target() __UpperCamelCase = feat_extract.model_input_names[0] __UpperCamelCase = BatchFeature({input_name: speech_inputs} ) __UpperCamelCase = feat_extract.num_mel_bins # hack! __UpperCamelCase = feat_extract.pad(_A,padding='longest',return_tensors='np' )[input_name] __UpperCamelCase = feat_extract.pad(_A,padding='longest',return_tensors='pt' )[input_name] self.assertTrue(abs(input_np.astype(np.floataa ).sum() - input_pt.numpy().astype(np.floataa ).sum() ) < 1E-2 ) def snake_case_ ( self: List[Any] ): '''simple docstring''' __UpperCamelCase = self.feat_extract_dict __UpperCamelCase = True __UpperCamelCase = self.feature_extraction_class(**_A ) __UpperCamelCase = self.feat_extract_tester.prepare_inputs_for_target() __UpperCamelCase = [len(_A ) for x in speech_inputs] __UpperCamelCase = feat_extract.model_input_names[0] __UpperCamelCase = BatchFeature({input_name: speech_inputs} ) __UpperCamelCase = feat_extract.num_mel_bins # hack! __UpperCamelCase = feat_extract.pad(_A,padding='longest',return_tensors='np' ) self.assertIn('attention_mask',_A ) self.assertListEqual(list(processed.attention_mask.shape ),list(processed[input_name].shape[:2] ) ) self.assertListEqual(processed.attention_mask.sum(-1 ).tolist(),_A ) def snake_case_ ( self: Optional[Any] ): '''simple docstring''' __UpperCamelCase = self.feat_extract_dict __UpperCamelCase = True __UpperCamelCase = self.feature_extraction_class(**_A ) __UpperCamelCase = self.feat_extract_tester.prepare_inputs_for_target() __UpperCamelCase = [len(_A ) for x in speech_inputs] __UpperCamelCase = feat_extract.model_input_names[0] __UpperCamelCase = BatchFeature({input_name: speech_inputs} ) __UpperCamelCase = min(_A ) __UpperCamelCase = feat_extract.num_mel_bins # hack! __UpperCamelCase = feat_extract.pad( _A,padding='max_length',max_length=_A,truncation=_A,return_tensors='np' ) self.assertIn('attention_mask',_A ) self.assertListEqual( list(processed_pad.attention_mask.shape ),[processed_pad[input_name].shape[0], max_length] ) self.assertListEqual( processed_pad.attention_mask[:, :max_length].sum(-1 ).tolist(),[max_length for x in speech_inputs] ) def snake_case_ ( self: int,A_: List[str] ): '''simple docstring''' from datasets import load_dataset __UpperCamelCase = load_dataset('hf-internal-testing/librispeech_asr_dummy','clean',split='validation' ) # automatic decoding with librispeech __UpperCamelCase = ds.sort('id' ).select(range(_A ) )[:num_samples]['audio'] return [x["array"] for x in speech_samples] def snake_case_ ( self: Union[str, Any] ): '''simple docstring''' __UpperCamelCase = torch.tensor( [2.3_804E-03, 2.0_752E-03, 1.9_836E-03, 2.1_057E-03, 1.6_174E-03, 3.0_518E-04, 9.1_553E-05, 3.3_569E-04, 9.7_656E-04, 1.8_311E-03, 2.0_142E-03, 2.1_057E-03, 1.7_395E-03, 4.5_776E-04, -3.9_673E-04, 4.5_776E-04, 1.0_071E-03, 9.1_553E-05, 4.8_828E-04, 1.1_597E-03, 7.3_242E-04, 9.4_604E-04, 1.8_005E-03, 1.8_311E-03, 8.8_501E-04, 4.2_725E-04, 4.8_828E-04, 7.3_242E-04, 1.0_986E-03, 2.1_057E-03] ) # fmt: on __UpperCamelCase = self._load_datasamples(1 ) __UpperCamelCase = SpeechTaFeatureExtractor() __UpperCamelCase = feature_extractor(_A,return_tensors='pt' ).input_values self.assertEquals(input_values.shape,(1, 9_3680) ) self.assertTrue(torch.allclose(input_values[0, :30],_A,atol=1E-6 ) ) def snake_case_ ( self: str ): '''simple docstring''' __UpperCamelCase = torch.tensor( [-2.6_8_7_0, -3.0_1_0_4, -3.1_3_5_6, -3.5_3_5_2, -3.0_0_4_4, -3.0_3_5_3, -3.4_7_1_9, -3.6_7_7_7, -3.1_5_2_0, -2.9_4_3_5, -2.6_5_5_3, -2.8_7_9_5, -2.9_9_4_4, -2.5_9_2_1, -3.0_2_7_9, -3.0_3_8_6, -3.0_8_6_4, -3.1_2_9_1, -3.2_3_5_3, -2.7_4_4_4, -2.6_8_3_1, -2.7_2_8_7, -3.1_7_6_1, -3.1_5_7_1, -3.2_7_2_6, -3.0_5_8_2, -3.1_0_0_7, -3.4_5_3_3, -3.4_6_9_5, -3.0_9_9_8] ) # fmt: on __UpperCamelCase = self._load_datasamples(1 ) __UpperCamelCase = SpeechTaFeatureExtractor() __UpperCamelCase = feature_extractor(audio_target=_A,return_tensors='pt' ).input_values self.assertEquals(input_values.shape,(1, 366, 80) ) self.assertTrue(torch.allclose(input_values[0, 0, :30],_A,atol=1E-4 ) )
310
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_sentencepiece_available, is_speech_available, is_tf_available, is_torch_available, ) __A : Union[str, Any] = { "configuration_speech_to_text": ["SPEECH_TO_TEXT_PRETRAINED_CONFIG_ARCHIVE_MAP", "Speech2TextConfig"], "processing_speech_to_text": ["Speech2TextProcessor"], } try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __A : List[Any] = ["Speech2TextTokenizer"] try: if not is_speech_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __A : List[Any] = ["Speech2TextFeatureExtractor"] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __A : int = [ "TF_SPEECH_TO_TEXT_PRETRAINED_MODEL_ARCHIVE_LIST", "TFSpeech2TextForConditionalGeneration", "TFSpeech2TextModel", "TFSpeech2TextPreTrainedModel", ] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __A : Tuple = [ "SPEECH_TO_TEXT_PRETRAINED_MODEL_ARCHIVE_LIST", "Speech2TextForConditionalGeneration", "Speech2TextModel", "Speech2TextPreTrainedModel", ] if TYPE_CHECKING: from .configuration_speech_to_text import SPEECH_TO_TEXT_PRETRAINED_CONFIG_ARCHIVE_MAP, SpeechaTextConfig from .processing_speech_to_text import SpeechaTextProcessor try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_speech_to_text import SpeechaTextTokenizer try: if not is_speech_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .feature_extraction_speech_to_text import SpeechaTextFeatureExtractor try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_speech_to_text import ( TF_SPEECH_TO_TEXT_PRETRAINED_MODEL_ARCHIVE_LIST, TFSpeechaTextForConditionalGeneration, TFSpeechaTextModel, TFSpeechaTextPreTrainedModel, ) try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_speech_to_text import ( SPEECH_TO_TEXT_PRETRAINED_MODEL_ARCHIVE_LIST, SpeechaTextForConditionalGeneration, SpeechaTextModel, SpeechaTextPreTrainedModel, ) else: import sys __A : Dict = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
273
0
from collections import OrderedDict from typing import Mapping from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging lowerCamelCase = logging.get_logger(__name__) lowerCamelCase = { "distilbert-base-uncased": "https://huggingface.co/distilbert-base-uncased/resolve/main/config.json", "distilbert-base-uncased-distilled-squad": ( "https://huggingface.co/distilbert-base-uncased-distilled-squad/resolve/main/config.json" ), "distilbert-base-cased": "https://huggingface.co/distilbert-base-cased/resolve/main/config.json", "distilbert-base-cased-distilled-squad": ( "https://huggingface.co/distilbert-base-cased-distilled-squad/resolve/main/config.json" ), "distilbert-base-german-cased": "https://huggingface.co/distilbert-base-german-cased/resolve/main/config.json", "distilbert-base-multilingual-cased": ( "https://huggingface.co/distilbert-base-multilingual-cased/resolve/main/config.json" ), "distilbert-base-uncased-finetuned-sst-2-english": ( "https://huggingface.co/distilbert-base-uncased-finetuned-sst-2-english/resolve/main/config.json" ), } class _a ( a_): _a : Optional[int] = '''distilbert''' _a : Optional[int] = { '''hidden_size''': '''dim''', '''num_attention_heads''': '''n_heads''', '''num_hidden_layers''': '''n_layers''', } def __init__( self : Optional[Any] , _SCREAMING_SNAKE_CASE : str=3_0522 , _SCREAMING_SNAKE_CASE : str=512 , _SCREAMING_SNAKE_CASE : str=False , _SCREAMING_SNAKE_CASE : str=6 , _SCREAMING_SNAKE_CASE : int=12 , _SCREAMING_SNAKE_CASE : Optional[int]=768 , _SCREAMING_SNAKE_CASE : str=4 * 768 , _SCREAMING_SNAKE_CASE : Any=0.1 , _SCREAMING_SNAKE_CASE : Optional[int]=0.1 , _SCREAMING_SNAKE_CASE : Dict="gelu" , _SCREAMING_SNAKE_CASE : str=0.02 , _SCREAMING_SNAKE_CASE : Optional[Any]=0.1 , _SCREAMING_SNAKE_CASE : List[str]=0.2 , _SCREAMING_SNAKE_CASE : Optional[Any]=0 , **_SCREAMING_SNAKE_CASE : int , )-> int: lowerCAmelCase__ : str = vocab_size lowerCAmelCase__ : Optional[int] = max_position_embeddings lowerCAmelCase__ : int = sinusoidal_pos_embds lowerCAmelCase__ : List[Any] = n_layers lowerCAmelCase__ : str = n_heads lowerCAmelCase__ : List[Any] = dim lowerCAmelCase__ : int = hidden_dim lowerCAmelCase__ : Optional[int] = dropout lowerCAmelCase__ : Any = attention_dropout lowerCAmelCase__ : Tuple = activation lowerCAmelCase__ : Tuple = initializer_range lowerCAmelCase__ : List[str] = qa_dropout lowerCAmelCase__ : Optional[int] = seq_classif_dropout super().__init__(**_A , pad_token_id=_A ) class _a ( a_): @property def UpperCAmelCase__( self : Optional[Any] )-> Dict: if self.task == "multiple-choice": lowerCAmelCase__ : Any = {0: '''batch''', 1: '''choice''', 2: '''sequence'''} else: lowerCAmelCase__ : List[str] = {0: '''batch''', 1: '''sequence'''} return OrderedDict( [ ('''input_ids''', dynamic_axis), ('''attention_mask''', dynamic_axis), ] )
131
def __SCREAMING_SNAKE_CASE ( UpperCamelCase__ ) -> list[int]: '''simple docstring''' if length <= 0 or not isinstance(UpperCamelCase__ , UpperCamelCase__ ): raise ValueError('''Length must be a positive integer.''' ) return [n * (2 * n - 1) for n in range(UpperCamelCase__ )] if __name__ == "__main__": print(hexagonal_numbers(length=5)) print(hexagonal_numbers(length=10))
273
0
import io import json import unittest from parameterized import parameterized from transformers import FSMTForConditionalGeneration, FSMTTokenizer from transformers.testing_utils import get_tests_dir, require_torch, slow, torch_device from utils import calculate_bleu _UpperCamelCase = get_tests_dir() + "/test_data/fsmt/fsmt_val_data.json" with io.open(filename, '''r''', encoding='''utf-8''') as f: _UpperCamelCase = json.load(f) @require_torch class _lowerCamelCase ( unittest.TestCase ): """simple docstring""" def UpperCAmelCase ( self , UpperCAmelCase ) -> Union[str, Any]: '''simple docstring''' return FSMTTokenizer.from_pretrained(_A ) def UpperCAmelCase ( self , UpperCAmelCase ) -> Tuple: '''simple docstring''' __snake_case : Dict = FSMTForConditionalGeneration.from_pretrained(_A ).to(_A ) if torch_device == "cuda": model.half() return model @parameterized.expand( [ ["en-ru", 26.0], ["ru-en", 22.0], ["en-de", 22.0], ["de-en", 29.0], ] ) @slow def UpperCAmelCase ( self , UpperCAmelCase , UpperCAmelCase ) -> Dict: '''simple docstring''' __snake_case : List[str] = F"""facebook/wmt19-{pair}""" __snake_case : Union[str, Any] = self.get_tokenizer(_A ) __snake_case : Tuple = self.get_model(_A ) __snake_case : int = bleu_data[pair]["src"] __snake_case : str = bleu_data[pair]["tgt"] __snake_case : str = tokenizer(_A , return_tensors="pt" , truncation=_A , padding="longest" ).to(_A ) __snake_case : List[Any] = model.generate( input_ids=batch.input_ids , num_beams=8 , ) __snake_case : Dict = tokenizer.batch_decode( _A , skip_special_tokens=_A , clean_up_tokenization_spaces=_A ) __snake_case : Union[str, Any] = calculate_bleu(_A , _A ) print(_A ) self.assertGreaterEqual(scores["bleu"] , _A )
326
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_5_0, '''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_0_0, '''eval_accuracy''': 0.3, '''eval_loss''': 0.9}, }, ] ) class A_ (unittest.TestCase ): def _lowercase ( self ): '''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=_A , ) assert hasattr(self , '''env''' ) def _lowercase ( self , _A=1 ): '''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=_A , instance_type=self.instance_type , debugger_hook_config=_A , hyperparameters={**self.env.hyperparameters, '''model_name_or_path''': self.model_name_or_path} , metric_definitions=self.env.metric_definitions , py_version='''py36''' , ) def _lowercase ( self , _A ): '''simple docstring''' TrainingJobAnalytics(_A ).export_csv(F"""{self.env.test_path}/{job_name}_metrics.csv""" ) def _lowercase ( self ): '''simple docstring''' UpperCAmelCase = self.create_estimator() # run training estimator.fit() # result dataframe UpperCAmelCase = TrainingJobAnalytics(estimator.latest_training_job.name ).dataframe() # extract kpis UpperCAmelCase = list(result_metrics_df[result_metrics_df.metric_name == '''eval_accuracy''']['''value'''] ) UpperCAmelCase = list(result_metrics_df[result_metrics_df.metric_name == '''eval_loss''']['''value'''] ) # get train time from SageMaker job, this includes starting, preprocessing, stopping UpperCAmelCase = ( Session().describe_training_job(estimator.latest_training_job.name ).get('''TrainingTimeInSeconds''' , 9_9_9_9_9_9 ) ) # 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} , _A )
273
0
"""simple docstring""" import os from shutil import copyfile from typing import Any, Dict, List, Optional, Tuple import sentencepiece as spm from ...tokenization_utils import AddedToken, BatchEncoding, PreTrainedTokenizer from ...utils import logging __A = logging.get_logger(__name__) __A = "▁" __A = {"vocab_file": "sentencepiece.bpe.model"} __A = { "vocab_file": { "facebook/nllb-200-distilled-600M": ( "https://huggingface.co/facebook/nllb-200-distilled-600M/blob/main/sentencepiece.bpe.model" ), } } __A = { "facebook/nllb-200-distilled-600M": 1024, } # fmt: off __A = ["ace_Arab", "ace_Latn", "acm_Arab", "acq_Arab", "aeb_Arab", "afr_Latn", "ajp_Arab", "aka_Latn", "amh_Ethi", "apc_Arab", "arb_Arab", "ars_Arab", "ary_Arab", "arz_Arab", "asm_Beng", "ast_Latn", "awa_Deva", "ayr_Latn", "azb_Arab", "azj_Latn", "bak_Cyrl", "bam_Latn", "ban_Latn", "bel_Cyrl", "bem_Latn", "ben_Beng", "bho_Deva", "bjn_Arab", "bjn_Latn", "bod_Tibt", "bos_Latn", "bug_Latn", "bul_Cyrl", "cat_Latn", "ceb_Latn", "ces_Latn", "cjk_Latn", "ckb_Arab", "crh_Latn", "cym_Latn", "dan_Latn", "deu_Latn", "dik_Latn", "dyu_Latn", "dzo_Tibt", "ell_Grek", "eng_Latn", "epo_Latn", "est_Latn", "eus_Latn", "ewe_Latn", "fao_Latn", "pes_Arab", "fij_Latn", "fin_Latn", "fon_Latn", "fra_Latn", "fur_Latn", "fuv_Latn", "gla_Latn", "gle_Latn", "glg_Latn", "grn_Latn", "guj_Gujr", "hat_Latn", "hau_Latn", "heb_Hebr", "hin_Deva", "hne_Deva", "hrv_Latn", "hun_Latn", "hye_Armn", "ibo_Latn", "ilo_Latn", "ind_Latn", "isl_Latn", "ita_Latn", "jav_Latn", "jpn_Jpan", "kab_Latn", "kac_Latn", "kam_Latn", "kan_Knda", "kas_Arab", "kas_Deva", "kat_Geor", "knc_Arab", "knc_Latn", "kaz_Cyrl", "kbp_Latn", "kea_Latn", "khm_Khmr", "kik_Latn", "kin_Latn", "kir_Cyrl", "kmb_Latn", "kon_Latn", "kor_Hang", "kmr_Latn", "lao_Laoo", "lvs_Latn", "lij_Latn", "lim_Latn", "lin_Latn", "lit_Latn", "lmo_Latn", "ltg_Latn", "ltz_Latn", "lua_Latn", "lug_Latn", "luo_Latn", "lus_Latn", "mag_Deva", "mai_Deva", "mal_Mlym", "mar_Deva", "min_Latn", "mkd_Cyrl", "plt_Latn", "mlt_Latn", "mni_Beng", "khk_Cyrl", "mos_Latn", "mri_Latn", "zsm_Latn", "mya_Mymr", "nld_Latn", "nno_Latn", "nob_Latn", "npi_Deva", "nso_Latn", "nus_Latn", "nya_Latn", "oci_Latn", "gaz_Latn", "ory_Orya", "pag_Latn", "pan_Guru", "pap_Latn", "pol_Latn", "por_Latn", "prs_Arab", "pbt_Arab", "quy_Latn", "ron_Latn", "run_Latn", "rus_Cyrl", "sag_Latn", "san_Deva", "sat_Beng", "scn_Latn", "shn_Mymr", "sin_Sinh", "slk_Latn", "slv_Latn", "smo_Latn", "sna_Latn", "snd_Arab", "som_Latn", "sot_Latn", "spa_Latn", "als_Latn", "srd_Latn", "srp_Cyrl", "ssw_Latn", "sun_Latn", "swe_Latn", "swh_Latn", "szl_Latn", "tam_Taml", "tat_Cyrl", "tel_Telu", "tgk_Cyrl", "tgl_Latn", "tha_Thai", "tir_Ethi", "taq_Latn", "taq_Tfng", "tpi_Latn", "tsn_Latn", "tso_Latn", "tuk_Latn", "tum_Latn", "tur_Latn", "twi_Latn", "tzm_Tfng", "uig_Arab", "ukr_Cyrl", "umb_Latn", "urd_Arab", "uzn_Latn", "vec_Latn", "vie_Latn", "war_Latn", "wol_Latn", "xho_Latn", "ydd_Hebr", "yor_Latn", "yue_Hant", "zho_Hans", "zho_Hant", "zul_Latn"] class snake_case ( a_ ): SCREAMING_SNAKE_CASE_ : Tuple = VOCAB_FILES_NAMES SCREAMING_SNAKE_CASE_ : Any = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES SCREAMING_SNAKE_CASE_ : str = PRETRAINED_VOCAB_FILES_MAP SCREAMING_SNAKE_CASE_ : Dict = ["""input_ids""", """attention_mask"""] SCREAMING_SNAKE_CASE_ : str = [] SCREAMING_SNAKE_CASE_ : Tuple = [] def __init__( self : str , UpperCamelCase__ : Tuple , UpperCamelCase__ : List[Any]="<s>" , UpperCamelCase__ : Optional[Any]="</s>" , UpperCamelCase__ : int="</s>" , UpperCamelCase__ : List[Any]="<s>" , UpperCamelCase__ : str="<unk>" , UpperCamelCase__ : Dict="<pad>" , UpperCamelCase__ : str="<mask>" , UpperCamelCase__ : str=None , UpperCamelCase__ : Optional[int]=None , UpperCamelCase__ : List[str]=None , UpperCamelCase__ : Dict = None , UpperCamelCase__ : Optional[Any]=None , UpperCamelCase__ : Any=False , **UpperCamelCase__ : int , )-> List[Any]: '''simple docstring''' __lowerCAmelCase: Optional[int] = AddedToken(_A , lstrip=_A , rstrip=_A) if isinstance(_A , _A) else mask_token __lowerCAmelCase: Optional[int] = {} if sp_model_kwargs is None else sp_model_kwargs __lowerCAmelCase: List[str] = legacy_behaviour super().__init__( bos_token=_A , eos_token=_A , unk_token=_A , sep_token=_A , cls_token=_A , pad_token=_A , mask_token=_A , tokenizer_file=_A , src_lang=_A , tgt_lang=_A , additional_special_tokens=_A , sp_model_kwargs=self.sp_model_kwargs , legacy_behaviour=_A , **_A , ) __lowerCAmelCase: int = spm.SentencePieceProcessor(**self.sp_model_kwargs) self.sp_model.Load(str(_A)) __lowerCAmelCase: str = 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>' | 'an' | '▁n' | '▁m' | '▁t' | '▁k' | '▁a' # spm | '<unk>' | '<s>' | '</s>' | 'an' | '▁n' | '▁m' | '▁t' | '▁k' | '▁a' | '▁s' # Mimic fairseq token-to-id alignment for the first 4 token __lowerCAmelCase: Tuple = {"<s>": 0, "<pad>": 1, "</s>": 2, "<unk>": 3} # The first "real" token "," has position 4 in the original fairseq vocab and position 3 in the spm vocab __lowerCAmelCase: int = 1 __lowerCAmelCase: Optional[int] = len(self.sp_model) __lowerCAmelCase: Union[str, Any] = { code: self.sp_model_size + i + self.fairseq_offset for i, code in enumerate(_A) } __lowerCAmelCase: Dict = {v: k for k, v in self.lang_code_to_id.items()} __lowerCAmelCase: Optional[int] = len(self.sp_model) + len(self.lang_code_to_id) + self.fairseq_offset self.fairseq_tokens_to_ids.update(self.lang_code_to_id) __lowerCAmelCase: Union[str, Any] = {v: k for k, v in self.fairseq_tokens_to_ids.items()} __lowerCAmelCase: Optional[int] = list(self.lang_code_to_id.keys()) if additional_special_tokens is not None: # Only add those special tokens if they are not already there. self._additional_special_tokens.extend( [t for t in additional_special_tokens if t not in self._additional_special_tokens]) __lowerCAmelCase: str = src_lang if src_lang is not None else "eng_Latn" __lowerCAmelCase: Tuple = self.lang_code_to_id[self._src_lang] __lowerCAmelCase: Optional[int] = tgt_lang self.set_src_lang_special_tokens(self._src_lang) def __getstate__( self : List[Any])-> Union[str, Any]: '''simple docstring''' __lowerCAmelCase: Union[str, Any] = self.__dict__.copy() __lowerCAmelCase: Tuple = None __lowerCAmelCase: List[str] = self.sp_model.serialized_model_proto() return state def __setstate__( self : Dict , UpperCamelCase__ : Optional[int])-> Optional[Any]: '''simple docstring''' __lowerCAmelCase: Tuple = d # for backward compatibility if not hasattr(self , "sp_model_kwargs"): __lowerCAmelCase: Tuple = {} __lowerCAmelCase: List[str] = spm.SentencePieceProcessor(**self.sp_model_kwargs) self.sp_model.LoadFromSerializedProto(self.sp_model_proto) @property def lowercase_ ( self : Any)-> Optional[Any]: '''simple docstring''' return len(self.sp_model) + len(self.lang_code_to_id) + self.fairseq_offset + 1 # Plus 1 for the mask token @property def lowercase_ ( self : Union[str, Any])-> List[str]: '''simple docstring''' return self._src_lang @src_lang.setter def lowercase_ ( self : Optional[int] , UpperCamelCase__ : Optional[int])-> Union[str, Any]: '''simple docstring''' __lowerCAmelCase: List[Any] = new_src_lang self.set_src_lang_special_tokens(self._src_lang) def lowercase_ ( self : str , UpperCamelCase__ : Optional[int] , UpperCamelCase__ : str = None , UpperCamelCase__ : List[Any] = False)-> List[Any]: '''simple docstring''' if already_has_special_tokens: return super().get_special_tokens_mask( token_ids_a=_A , token_ids_a=_A , already_has_special_tokens=_A) __lowerCAmelCase: int = [1] * len(self.prefix_tokens) __lowerCAmelCase: Dict = [1] * len(self.suffix_tokens) if token_ids_a is None: return prefix_ones + ([0] * len(_A)) + suffix_ones return prefix_ones + ([0] * len(_A)) + ([0] * len(_A)) + suffix_ones def lowercase_ ( self : List[str] , UpperCamelCase__ : Dict , UpperCamelCase__ : Optional[Any] = None)-> Any: '''simple docstring''' if token_ids_a is None: return self.prefix_tokens + token_ids_a + self.suffix_tokens # We don't expect to process pairs, but leave the pair logic for API consistency return self.prefix_tokens + token_ids_a + token_ids_a + self.suffix_tokens def lowercase_ ( self : str , UpperCamelCase__ : Tuple , UpperCamelCase__ : Optional[int] = None)-> Tuple: '''simple docstring''' __lowerCAmelCase: Optional[Any] = [self.sep_token_id] __lowerCAmelCase: Optional[Any] = [self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep) * [0] return len(cls + token_ids_a + sep + sep + token_ids_a + sep) * [0] def lowercase_ ( self : int , UpperCamelCase__ : Dict , UpperCamelCase__ : str , UpperCamelCase__ : Optional[int] , UpperCamelCase__ : Tuple , **UpperCamelCase__ : Union[str, Any])-> Any: '''simple docstring''' if src_lang is None or tgt_lang is None: raise ValueError("Translation requires a `src_lang` and a `tgt_lang` for this model") __lowerCAmelCase: Dict = src_lang __lowerCAmelCase: Optional[Any] = self(_A , add_special_tokens=_A , return_tensors=_A , **_A) __lowerCAmelCase: Union[str, Any] = self.convert_tokens_to_ids(_A) __lowerCAmelCase: Dict = tgt_lang_id return inputs def lowercase_ ( self : List[str])-> Optional[Any]: '''simple docstring''' __lowerCAmelCase: str = {self.convert_ids_to_tokens(_A): i for i in range(self.vocab_size)} vocab.update(self.added_tokens_encoder) return vocab def lowercase_ ( self : Any , UpperCamelCase__ : Tuple)-> Optional[int]: '''simple docstring''' return self.sp_model.encode(_A , out_type=_A) def lowercase_ ( self : Optional[int] , UpperCamelCase__ : int)-> Union[str, Any]: '''simple docstring''' if token in self.fairseq_tokens_to_ids: return self.fairseq_tokens_to_ids[token] __lowerCAmelCase: List[Any] = self.sp_model.PieceToId(_A) # 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 lowercase_ ( self : str , UpperCamelCase__ : 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 lowercase_ ( self : Optional[Any] , UpperCamelCase__ : Dict)-> Optional[Any]: '''simple docstring''' __lowerCAmelCase: Dict = "".join(_A).replace(_A , " ").strip() return out_string def lowercase_ ( self : Union[str, Any] , UpperCamelCase__ : Any , UpperCamelCase__ : Dict = None)-> Optional[Any]: '''simple docstring''' if not os.path.isdir(_A): logger.error(f"Vocabulary path ({save_directory}) should be a directory") return __lowerCAmelCase: Tuple = os.path.join( _A , (filename_prefix + "-" if filename_prefix else "") + VOCAB_FILES_NAMES["vocab_file"]) if os.path.abspath(self.vocab_file) != os.path.abspath(_A) and os.path.isfile(self.vocab_file): copyfile(self.vocab_file , _A) elif not os.path.isfile(self.vocab_file): with open(_A , "wb") as fi: __lowerCAmelCase: str = self.sp_model.serialized_model_proto() fi.write(_A) return (out_vocab_file,) def lowercase_ ( self : str , UpperCamelCase__ : Optional[Any] , UpperCamelCase__ : Dict = "eng_Latn" , UpperCamelCase__ : Dict = None , UpperCamelCase__ : Tuple = "fra_Latn" , **UpperCamelCase__ : Optional[int] , )-> Union[str, Any]: '''simple docstring''' __lowerCAmelCase: Tuple = src_lang __lowerCAmelCase: Optional[Any] = tgt_lang return super().prepare_seqaseq_batch(_A , _A , **_A) def lowercase_ ( self : List[str])-> str: '''simple docstring''' return self.set_src_lang_special_tokens(self.src_lang) def lowercase_ ( self : Optional[Any])-> int: '''simple docstring''' return self.set_tgt_lang_special_tokens(self.tgt_lang) def lowercase_ ( self : Any , UpperCamelCase__ : Any)-> List[str]: '''simple docstring''' __lowerCAmelCase: Tuple = self.lang_code_to_id[src_lang] if self.legacy_behaviour: __lowerCAmelCase: str = [] __lowerCAmelCase: Tuple = [self.eos_token_id, self.cur_lang_code] else: __lowerCAmelCase: str = [self.cur_lang_code] __lowerCAmelCase: int = [self.eos_token_id] def lowercase_ ( self : str , UpperCamelCase__ : Any)-> Optional[Any]: '''simple docstring''' __lowerCAmelCase: Any = self.lang_code_to_id[lang] if self.legacy_behaviour: __lowerCAmelCase: Union[str, Any] = [] __lowerCAmelCase: List[str] = [self.eos_token_id, self.cur_lang_code] else: __lowerCAmelCase: Any = [self.cur_lang_code] __lowerCAmelCase: Dict = [self.eos_token_id]
217
from collections import OrderedDict from typing import Mapping from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging __A : int = logging.get_logger(__name__) __A : Tuple = { "google/bigbird-roberta-base": "https://huggingface.co/google/bigbird-roberta-base/resolve/main/config.json", "google/bigbird-roberta-large": "https://huggingface.co/google/bigbird-roberta-large/resolve/main/config.json", "google/bigbird-base-trivia-itc": "https://huggingface.co/google/bigbird-base-trivia-itc/resolve/main/config.json", # See all BigBird models at https://huggingface.co/models?filter=big_bird } class A_ (a_ ): UpperCAmelCase__ = '''big_bird''' def __init__( self , _A=5_0_3_5_8 , _A=7_6_8 , _A=1_2 , _A=1_2 , _A=3_0_7_2 , _A="gelu_new" , _A=0.1 , _A=0.1 , _A=4_0_9_6 , _A=2 , _A=0.02 , _A=1E-12 , _A=True , _A=0 , _A=1 , _A=2 , _A=6_6 , _A="block_sparse" , _A=True , _A=False , _A=6_4 , _A=3 , _A=None , **_A , ): '''simple docstring''' super().__init__( pad_token_id=_A , bos_token_id=_A , eos_token_id=_A , sep_token_id=_A , **_A , ) UpperCAmelCase = vocab_size UpperCAmelCase = max_position_embeddings UpperCAmelCase = hidden_size UpperCAmelCase = num_hidden_layers UpperCAmelCase = num_attention_heads UpperCAmelCase = intermediate_size UpperCAmelCase = hidden_act UpperCAmelCase = hidden_dropout_prob UpperCAmelCase = attention_probs_dropout_prob UpperCAmelCase = initializer_range UpperCAmelCase = type_vocab_size UpperCAmelCase = layer_norm_eps UpperCAmelCase = use_cache UpperCAmelCase = rescale_embeddings UpperCAmelCase = attention_type UpperCAmelCase = use_bias UpperCAmelCase = block_size UpperCAmelCase = num_random_blocks UpperCAmelCase = classifier_dropout class A_ (a_ ): @property def _lowercase ( self ): '''simple docstring''' if self.task == "multiple-choice": UpperCAmelCase = {0: '''batch''', 1: '''choice''', 2: '''sequence'''} else: UpperCAmelCase = {0: '''batch''', 1: '''sequence'''} return OrderedDict( [ ('''input_ids''', dynamic_axis), ('''attention_mask''', dynamic_axis), ] )
273
0
from typing import Optional, Tuple, Union import tensorflow as tf from ...activations_tf import ACTaFN from ...file_utils import add_code_sample_docstrings, add_start_docstrings, add_start_docstrings_to_model_forward from ...modeling_tf_outputs import ( TFBaseModelOutputWithNoAttention, TFBaseModelOutputWithPoolingAndNoAttention, TFSequenceClassifierOutput, ) from ...modeling_tf_utils import TFPreTrainedModel, TFSequenceClassificationLoss, keras_serializable, unpack_inputs from ...tf_utils import shape_list from ...utils import logging from .configuration_regnet import RegNetConfig _lowerCAmelCase : Optional[Any] = logging.get_logger(__name__) # General docstring _lowerCAmelCase : Optional[int] = "RegNetConfig" # Base docstring _lowerCAmelCase : str = "facebook/regnet-y-040" _lowerCAmelCase : str = [1, 1_088, 7, 7] # Image classification docstring _lowerCAmelCase : List[str] = "facebook/regnet-y-040" _lowerCAmelCase : Optional[Any] = "tabby, tabby cat" _lowerCAmelCase : Optional[Any] = [ "facebook/regnet-y-040", # See all regnet models at https://huggingface.co/models?filter=regnet ] class __magic_name__ ( tf.keras.layers.Layer ): """simple docstring""" def __init__( self :List[str] , snake_case :Tuple , snake_case :List[str] = 3 , snake_case :List[str] = 1 , snake_case :List[Any] = 1 , snake_case :Optional[int] = "relu" , **snake_case :Tuple , ): '''simple docstring''' super().__init__(**_A ) # The padding and conv has been verified in # https://colab.research.google.com/gist/sayakpaul/854bc10eeaf21c9ee2119e0b9f3841a7/scratchpad.ipynb A_ : int = tf.keras.layers.ZeroPaddingaD(padding=kernel_size // 2 ) A_ : int = tf.keras.layers.ConvaD( filters=_A , kernel_size=_A , strides=_A , padding="VALID" , groups=_A , use_bias=_A , name="convolution" , ) A_ : List[str] = tf.keras.layers.BatchNormalization(epsilon=1e-5 , momentum=0.9 , name="normalization" ) A_ : List[Any] = ACTaFN[activation] if activation is not None else tf.identity def SCREAMING_SNAKE_CASE ( self :str , snake_case :Tuple ): '''simple docstring''' A_ : Optional[int] = self.convolution(self.padding(_A ) ) A_ : Optional[int] = self.normalization(_A ) A_ : Tuple = self.activation(_A ) return hidden_state class __magic_name__ ( tf.keras.layers.Layer ): """simple docstring""" def __init__( self :Optional[Any] , snake_case :Union[str, Any] , **snake_case :List[Any] ): '''simple docstring''' super().__init__(**_A ) A_ : Any = config.num_channels A_ : int = TFRegNetConvLayer( out_channels=config.embedding_size , kernel_size=3 , stride=2 , activation=config.hidden_act , name="embedder" , ) def SCREAMING_SNAKE_CASE ( self :List[str] , snake_case :List[Any] ): '''simple docstring''' A_ : List[str] = shape_list(_A )[1] if tf.executing_eagerly() and num_channels != self.num_channels: raise ValueError( "Make sure that the channel dimension of the pixel values match with the one set in the configuration." ) # When running on CPU, `tf.keras.layers.Conv2D` doesn't support `NCHW` format. # So change the input format from `NCHW` to `NHWC`. # shape = (batch_size, in_height, in_width, in_channels=num_channels) A_ : int = tf.transpose(_A , perm=(0, 2, 3, 1) ) A_ : Optional[Any] = self.embedder(_A ) return hidden_state class __magic_name__ ( tf.keras.layers.Layer ): """simple docstring""" def __init__( self :List[Any] , snake_case :str , snake_case :str = 2 , **snake_case :Tuple ): '''simple docstring''' super().__init__(**_A ) A_ : List[Any] = tf.keras.layers.ConvaD( filters=_A , kernel_size=1 , strides=_A , use_bias=_A , name="convolution" ) A_ : List[Any] = tf.keras.layers.BatchNormalization(epsilon=1e-5 , momentum=0.9 , name="normalization" ) def SCREAMING_SNAKE_CASE ( self :Optional[Any] , snake_case :Dict , snake_case :str = False ): '''simple docstring''' return self.normalization(self.convolution(_A ) , training=_A ) class __magic_name__ ( tf.keras.layers.Layer ): """simple docstring""" def __init__( self :Any , snake_case :int , snake_case :str , **snake_case :Dict ): '''simple docstring''' super().__init__(**_A ) A_ : int = tf.keras.layers.GlobalAveragePoolingaD(keepdims=_A , name="pooler" ) A_ : Optional[Any] = [ tf.keras.layers.ConvaD(filters=_A , kernel_size=1 , activation="relu" , name="attention.0" ), tf.keras.layers.ConvaD(filters=_A , kernel_size=1 , activation="sigmoid" , name="attention.2" ), ] def SCREAMING_SNAKE_CASE ( self :Optional[int] , snake_case :str ): '''simple docstring''' A_ : int = self.pooler(_A ) for layer_module in self.attention: A_ : Optional[int] = layer_module(_A ) A_ : Dict = hidden_state * pooled return hidden_state class __magic_name__ ( tf.keras.layers.Layer ): """simple docstring""" def __init__( self :Any , snake_case :Union[str, Any] , snake_case :Dict , snake_case :Optional[Any] , snake_case :Dict = 1 , **snake_case :str ): '''simple docstring''' super().__init__(**_A ) A_ : Union[str, Any] = in_channels != out_channels or stride != 1 A_ : Tuple = max(1 , out_channels // config.groups_width ) A_ : Dict = ( TFRegNetShortCut(_A , stride=_A , name="shortcut" ) if should_apply_shortcut else tf.keras.layers.Activation("linear" , name="shortcut" ) ) # `self.layers` instead of `self.layer` because that is a reserved argument. A_ : Dict = [ TFRegNetConvLayer(_A , kernel_size=1 , activation=config.hidden_act , name="layer.0" ), TFRegNetConvLayer( _A , stride=_A , groups=_A , activation=config.hidden_act , name="layer.1" ), TFRegNetConvLayer(_A , kernel_size=1 , activation=_A , name="layer.2" ), ] A_ : Dict = ACTaFN[config.hidden_act] def SCREAMING_SNAKE_CASE ( self :Any , snake_case :List[Any] ): '''simple docstring''' A_ : str = hidden_state for layer_module in self.layers: A_ : int = layer_module(_A ) A_ : Optional[Any] = self.shortcut(_A ) hidden_state += residual A_ : List[Any] = self.activation(_A ) return hidden_state class __magic_name__ ( tf.keras.layers.Layer ): """simple docstring""" def __init__( self :Optional[Any] , snake_case :List[str] , snake_case :Any , snake_case :str , snake_case :List[Any] = 1 , **snake_case :Union[str, Any] ): '''simple docstring''' super().__init__(**_A ) A_ : Dict = in_channels != out_channels or stride != 1 A_ : Tuple = max(1 , out_channels // config.groups_width ) A_ : Any = ( TFRegNetShortCut(_A , stride=_A , name="shortcut" ) if should_apply_shortcut else tf.keras.layers.Activation("linear" , name="shortcut" ) ) A_ : Optional[int] = [ TFRegNetConvLayer(_A , kernel_size=1 , activation=config.hidden_act , name="layer.0" ), TFRegNetConvLayer( _A , stride=_A , groups=_A , activation=config.hidden_act , name="layer.1" ), TFRegNetSELayer(_A , reduced_channels=int(round(in_channels / 4 ) ) , name="layer.2" ), TFRegNetConvLayer(_A , kernel_size=1 , activation=_A , name="layer.3" ), ] A_ : Optional[Any] = ACTaFN[config.hidden_act] def SCREAMING_SNAKE_CASE ( self :Union[str, Any] , snake_case :Dict ): '''simple docstring''' A_ : List[str] = hidden_state for layer_module in self.layers: A_ : int = layer_module(_A ) A_ : Optional[int] = self.shortcut(_A ) hidden_state += residual A_ : List[Any] = self.activation(_A ) return hidden_state class __magic_name__ ( tf.keras.layers.Layer ): """simple docstring""" def __init__( self :int , snake_case :Optional[int] , snake_case :Tuple , snake_case :List[Any] , snake_case :Dict = 2 , snake_case :Dict = 2 , **snake_case :List[Any] ): '''simple docstring''' super().__init__(**_A ) A_ : Any = TFRegNetXLayer if config.layer_type == "x" else TFRegNetYLayer A_ : int = [ # downsampling is done in the first layer with stride of 2 layer(_A , _A , _A , stride=_A , name="layers.0" ), *[layer(_A , _A , _A , name=f"layers.{i+1}" ) for i in range(depth - 1 )], ] def SCREAMING_SNAKE_CASE ( self :Optional[int] , snake_case :Union[str, Any] ): '''simple docstring''' for layer_module in self.layers: A_ : str = layer_module(_A ) return hidden_state class __magic_name__ ( tf.keras.layers.Layer ): """simple docstring""" def __init__( self :Any , snake_case :Optional[Any] , **snake_case :Optional[int] ): '''simple docstring''' super().__init__(**_A ) A_ : Union[str, Any] = [] # based on `downsample_in_first_stage`, the first layer of the first stage may or may not downsample the input self.stages.append( TFRegNetStage( _A , config.embedding_size , config.hidden_sizes[0] , stride=2 if config.downsample_in_first_stage else 1 , depth=config.depths[0] , name="stages.0" , ) ) A_ : Optional[Any] = zip(config.hidden_sizes , config.hidden_sizes[1:] ) for i, ((in_channels, out_channels), depth) in enumerate(zip(_A , config.depths[1:] ) ): self.stages.append(TFRegNetStage(_A , _A , _A , depth=_A , name=f"stages.{i+1}" ) ) def SCREAMING_SNAKE_CASE ( self :Optional[int] , snake_case :List[str] , snake_case :Optional[int] = False , snake_case :Union[str, Any] = True ): '''simple docstring''' A_ : Optional[Any] = () if output_hidden_states else None for stage_module in self.stages: if output_hidden_states: A_ : Union[str, Any] = hidden_states + (hidden_state,) A_ : Optional[int] = stage_module(_A ) if output_hidden_states: A_ : Optional[int] = hidden_states + (hidden_state,) if not return_dict: return tuple(v for v in [hidden_state, hidden_states] if v is not None ) return TFBaseModelOutputWithNoAttention(last_hidden_state=_A , hidden_states=_A ) @keras_serializable class __magic_name__ ( tf.keras.layers.Layer ): """simple docstring""" __UpperCamelCase = RegNetConfig def __init__( self :Optional[Any] , snake_case :Optional[int] , **snake_case :Optional[int] ): '''simple docstring''' super().__init__(**_A ) A_ : List[str] = config A_ : Dict = TFRegNetEmbeddings(_A , name="embedder" ) A_ : List[str] = TFRegNetEncoder(_A , name="encoder" ) A_ : Tuple = tf.keras.layers.GlobalAveragePoolingaD(keepdims=_A , name="pooler" ) @unpack_inputs def SCREAMING_SNAKE_CASE ( self :Any , snake_case :List[Any] , snake_case :List[Any] = None , snake_case :Optional[Any] = None , snake_case :str = False , ): '''simple docstring''' A_ : str = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states ) A_ : List[str] = return_dict if return_dict is not None else self.config.use_return_dict A_ : Union[str, Any] = self.embedder(_A , training=_A ) A_ : Dict = self.encoder( _A , output_hidden_states=_A , return_dict=_A , training=_A ) A_ : Tuple = encoder_outputs[0] A_ : Tuple = self.pooler(_A ) # Change to NCHW output format have uniformity in the modules A_ : Optional[int] = tf.transpose(_A , perm=(0, 3, 1, 2) ) A_ : List[Any] = tf.transpose(_A , perm=(0, 3, 1, 2) ) # Change the other hidden state outputs to NCHW as well if output_hidden_states: A_ : Optional[Any] = tuple([tf.transpose(_A , perm=(0, 3, 1, 2) ) for h in encoder_outputs[1]] ) if not return_dict: return (last_hidden_state, pooled_output) + encoder_outputs[1:] return TFBaseModelOutputWithPoolingAndNoAttention( last_hidden_state=_A , pooler_output=_A , hidden_states=hidden_states if output_hidden_states else encoder_outputs.hidden_states , ) class __magic_name__ ( a_ ): """simple docstring""" __UpperCamelCase = RegNetConfig __UpperCamelCase = '''regnet''' __UpperCamelCase = '''pixel_values''' @property def SCREAMING_SNAKE_CASE ( self :Union[str, Any] ): '''simple docstring''' return {"pixel_values": tf.TensorSpec(shape=(None, self.config.num_channels, 224, 224) , dtype=tf.floataa )} _lowerCAmelCase : str = R"\n Parameters:\n This model is a Tensorflow\n [tf.keras.layers.Layer](https://www.tensorflow.org/api_docs/python/tf/keras/layers/Layer) sub-class. Use it as a\n regular Tensorflow Module and refer to the Tensorflow documentation for all matter related to general usage and\n behavior.\n config ([`RegNetConfig`]): Model configuration class with all the parameters of the model.\n Initializing with a config file does not load the weights associated with the model, only the\n configuration. Check out the [`~TFPreTrainedModel.from_pretrained`] method to load the model weights.\n" _lowerCAmelCase : List[Any] = R"\n Args:\n pixel_values (`tf.Tensor` of shape `(batch_size, num_channels, height, width)`):\n Pixel values. Pixel values can be obtained using [`AutoImageProcessor`]. See\n [`ConveNextImageProcessor.__call__`] for details.\n output_hidden_states (`bool`, *optional*):\n Whether or not to return the hidden states of all layers. See `hidden_states` under returned tensors for\n more detail.\n return_dict (`bool`, *optional*):\n Whether or not to return a [`~utils.ModelOutput`] instead of a plain tuple.\n" @add_start_docstrings( '''The bare RegNet model outputting raw features without any specific head on top.''' , a_ , ) class __magic_name__ ( a_ ): """simple docstring""" def __init__( self :str , snake_case :Dict , *snake_case :Union[str, Any] , **snake_case :Optional[int] ): '''simple docstring''' super().__init__(_A , *_A , **_A ) A_ : Tuple = TFRegNetMainLayer(_A , name="regnet" ) @unpack_inputs @add_start_docstrings_to_model_forward(_A ) @add_code_sample_docstrings( checkpoint=_CHECKPOINT_FOR_DOC , output_type=_A , config_class=_CONFIG_FOR_DOC , modality="vision" , expected_output=_EXPECTED_OUTPUT_SHAPE , ) def SCREAMING_SNAKE_CASE ( self :Any , snake_case :Optional[Any] , snake_case :int = None , snake_case :int = None , snake_case :List[Any]=False , ): '''simple docstring''' A_ : List[str] = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states ) A_ : Dict = return_dict if return_dict is not None else self.config.use_return_dict A_ : Optional[int] = self.regnet( pixel_values=_A , output_hidden_states=_A , return_dict=_A , training=_A , ) if not return_dict: return (outputs[0],) + outputs[1:] return TFBaseModelOutputWithPoolingAndNoAttention( last_hidden_state=outputs.last_hidden_state , pooler_output=outputs.pooler_output , hidden_states=outputs.hidden_states , ) @add_start_docstrings( ''' RegNet Model with an image classification head on top (a linear layer on top of the pooled features), e.g. for ImageNet. ''' , a_ , ) class __magic_name__ ( a_ , a_ ): """simple docstring""" def __init__( self :Optional[int] , snake_case :Optional[int] , *snake_case :Optional[Any] , **snake_case :Optional[int] ): '''simple docstring''' super().__init__(_A , *_A , **_A ) A_ : Optional[int] = config.num_labels A_ : Tuple = TFRegNetMainLayer(_A , name="regnet" ) # classification head A_ : Optional[Any] = [ tf.keras.layers.Flatten(), tf.keras.layers.Dense(config.num_labels , name="classifier.1" ) if config.num_labels > 0 else tf.identity, ] @unpack_inputs @add_start_docstrings_to_model_forward(_A ) @add_code_sample_docstrings( checkpoint=_IMAGE_CLASS_CHECKPOINT , output_type=_A , config_class=_CONFIG_FOR_DOC , expected_output=_IMAGE_CLASS_EXPECTED_OUTPUT , ) def SCREAMING_SNAKE_CASE ( self :Union[str, Any] , snake_case :List[str] = None , snake_case :Any = None , snake_case :Dict = None , snake_case :List[Any] = None , snake_case :str=False , ): '''simple docstring''' A_ : int = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states ) A_ : Union[str, Any] = return_dict if return_dict is not None else self.config.use_return_dict A_ : Optional[Any] = self.regnet( _A , output_hidden_states=_A , return_dict=_A , training=_A ) A_ : Optional[int] = outputs.pooler_output if return_dict else outputs[1] A_ : str = self.classifier[0](_A ) A_ : str = self.classifier[1](_A ) A_ : Tuple = None if labels is None else self.hf_compute_loss(labels=_A , logits=_A ) if not return_dict: A_ : str = (logits,) + outputs[2:] return ((loss,) + output) if loss is not None else output return TFSequenceClassifierOutput(loss=_A , logits=_A , hidden_states=outputs.hidden_states )
300
from __future__ import annotations import inspect import unittest from transformers import ViTConfig from transformers.testing_utils import require_tf, require_vision, slow from transformers.utils import cached_property, is_tf_available, is_vision_available from ...test_configuration_common import ConfigTester from ...test_modeling_tf_common import TFModelTesterMixin, floats_tensor, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_tf_available(): import tensorflow as tf from transformers import TFViTForImageClassification, TFViTModel if is_vision_available(): from PIL import Image from transformers import ViTImageProcessor class A_ : def __init__( self , _A , _A=1_3 , _A=3_0 , _A=2 , _A=3 , _A=True , _A=True , _A=3_2 , _A=2 , _A=4 , _A=3_7 , _A="gelu" , _A=0.1 , _A=0.1 , _A=1_0 , _A=0.02 , _A=3 , _A=None , ): '''simple docstring''' UpperCAmelCase = parent UpperCAmelCase = batch_size UpperCAmelCase = image_size UpperCAmelCase = patch_size UpperCAmelCase = num_channels UpperCAmelCase = is_training UpperCAmelCase = use_labels UpperCAmelCase = hidden_size UpperCAmelCase = num_hidden_layers UpperCAmelCase = num_attention_heads UpperCAmelCase = intermediate_size UpperCAmelCase = hidden_act UpperCAmelCase = hidden_dropout_prob UpperCAmelCase = attention_probs_dropout_prob UpperCAmelCase = type_sequence_label_size UpperCAmelCase = initializer_range UpperCAmelCase = scope # in ViT, the seq length equals the number of patches + 1 (we add 1 for the [CLS] token) UpperCAmelCase = (image_size // patch_size) ** 2 UpperCAmelCase = num_patches + 1 def _lowercase ( self ): '''simple docstring''' UpperCAmelCase = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) UpperCAmelCase = None if self.use_labels: UpperCAmelCase = ids_tensor([self.batch_size] , self.type_sequence_label_size ) UpperCAmelCase = self.get_config() return config, pixel_values, labels def _lowercase ( self ): '''simple docstring''' return ViTConfig( image_size=self.image_size , patch_size=self.patch_size , num_channels=self.num_channels , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , is_decoder=_A , initializer_range=self.initializer_range , ) def _lowercase ( self , _A , _A , _A ): '''simple docstring''' UpperCAmelCase = TFViTModel(config=_A ) UpperCAmelCase = model(_A , training=_A ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) # Test with an image with different size than the one specified in config. UpperCAmelCase = self.image_size // 2 UpperCAmelCase = pixel_values[:, :, :image_size, :image_size] UpperCAmelCase = model(_A , interpolate_pos_encoding=_A , training=_A ) UpperCAmelCase = (image_size // self.patch_size) ** 2 + 1 self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, seq_length, self.hidden_size) ) def _lowercase ( self , _A , _A , _A ): '''simple docstring''' UpperCAmelCase = self.type_sequence_label_size UpperCAmelCase = TFViTForImageClassification(_A ) UpperCAmelCase = model(_A , labels=_A , training=_A ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.type_sequence_label_size) ) # Test with an image with different size than the one specified in config. UpperCAmelCase = self.image_size // 2 UpperCAmelCase = pixel_values[:, :, :image_size, :image_size] UpperCAmelCase = model(_A , interpolate_pos_encoding=_A , training=_A ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.type_sequence_label_size) ) # test greyscale images UpperCAmelCase = 1 UpperCAmelCase = TFViTForImageClassification(_A ) UpperCAmelCase = floats_tensor([self.batch_size, 1, self.image_size, self.image_size] ) UpperCAmelCase = model(_A ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.type_sequence_label_size) ) def _lowercase ( self ): '''simple docstring''' UpperCAmelCase = self.prepare_config_and_inputs() UpperCAmelCase , UpperCAmelCase , UpperCAmelCase = config_and_inputs UpperCAmelCase = {'''pixel_values''': pixel_values} return config, inputs_dict @require_tf class A_ (a_ , a_ , unittest.TestCase ): UpperCAmelCase__ = (TFViTModel, TFViTForImageClassification) if is_tf_available() else () UpperCAmelCase__ = ( {'''feature-extraction''': TFViTModel, '''image-classification''': TFViTForImageClassification} if is_tf_available() else {} ) UpperCAmelCase__ = False UpperCAmelCase__ = False UpperCAmelCase__ = False def _lowercase ( self ): '''simple docstring''' UpperCAmelCase = TFViTModelTester(self ) UpperCAmelCase = ConfigTester(self , config_class=_A , has_text_modality=_A , hidden_size=3_7 ) def _lowercase ( self ): '''simple docstring''' self.config_tester.run_common_tests() @unittest.skip(reason='''ViT does not use inputs_embeds''' ) def _lowercase ( self ): '''simple docstring''' pass @unittest.skip(reason='''ViT does not use inputs_embeds''' ) def _lowercase ( self ): '''simple docstring''' pass def _lowercase ( self ): '''simple docstring''' UpperCAmelCase , UpperCAmelCase = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: UpperCAmelCase = model_class(_A ) self.assertIsInstance(model.get_input_embeddings() , (tf.keras.layers.Layer) ) UpperCAmelCase = model.get_output_embeddings() self.assertTrue(x is None or isinstance(_A , tf.keras.layers.Layer ) ) def _lowercase ( self ): '''simple docstring''' UpperCAmelCase , UpperCAmelCase = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: UpperCAmelCase = model_class(_A ) UpperCAmelCase = inspect.signature(model.call ) # signature.parameters is an OrderedDict => so arg_names order is deterministic UpperCAmelCase = [*signature.parameters.keys()] UpperCAmelCase = ['''pixel_values'''] self.assertListEqual(arg_names[:1] , _A ) def _lowercase ( self ): '''simple docstring''' UpperCAmelCase = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*_A ) def _lowercase ( self ): '''simple docstring''' UpperCAmelCase = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_image_classification(*_A ) @slow def _lowercase ( self ): '''simple docstring''' UpperCAmelCase = TFViTModel.from_pretrained('''google/vit-base-patch16-224''' ) self.assertIsNotNone(_A ) def __SCREAMING_SNAKE_CASE ( ) -> Optional[Any]: '''simple docstring''' UpperCAmelCase = Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png''' ) return image @require_tf @require_vision class A_ (unittest.TestCase ): @cached_property def _lowercase ( self ): '''simple docstring''' return ViTImageProcessor.from_pretrained('''google/vit-base-patch16-224''' ) if is_vision_available() else None @slow def _lowercase ( self ): '''simple docstring''' UpperCAmelCase = TFViTForImageClassification.from_pretrained('''google/vit-base-patch16-224''' ) UpperCAmelCase = self.default_image_processor UpperCAmelCase = prepare_img() UpperCAmelCase = image_processor(images=_A , return_tensors='''tf''' ) # forward pass UpperCAmelCase = model(**_A ) # verify the logits UpperCAmelCase = tf.TensorShape((1, 1_0_0_0) ) self.assertEqual(outputs.logits.shape , _A ) UpperCAmelCase = tf.constant([-0.27_44, 0.82_15, -0.08_36] ) tf.debugging.assert_near(outputs.logits[0, :3] , _A , atol=1E-4 )
273
0
"""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_ = logging.get_logger(__name__) # pylint: disable=invalid-name lowerCAmelCase_ = "\n Examples:\n ```py\n >>> import torch\n >>> import numpy as np\n\n >>> from diffusers import KandinskyV22PriorPipeline, KandinskyV22ControlnetPipeline\n >>> from transformers import pipeline\n >>> from diffusers.utils import load_image\n\n\n >>> def make_hint(image, depth_estimator):\n ... image = depth_estimator(image)[\"depth\"]\n ... image = np.array(image)\n ... image = image[:, :, None]\n ... image = np.concatenate([image, image, image], axis=2)\n ... detected_map = torch.from_numpy(image).float() / 255.0\n ... hint = detected_map.permute(2, 0, 1)\n ... return hint\n\n\n >>> depth_estimator = pipeline(\"depth-estimation\")\n\n >>> pipe_prior = KandinskyV22PriorPipeline.from_pretrained(\n ... \"kandinsky-community/kandinsky-2-2-prior\", torch_dtype=torch.float16\n ... )\n >>> pipe_prior = pipe_prior.to(\"cuda\")\n\n >>> pipe = KandinskyV22ControlnetPipeline.from_pretrained(\n ... \"kandinsky-community/kandinsky-2-2-controlnet-depth\", torch_dtype=torch.float16\n ... )\n >>> pipe = pipe.to(\"cuda\")\n\n\n >>> img = load_image(\n ... \"https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main\"\n ... \"/kandinsky/cat.png\"\n ... ).resize((768, 768))\n\n >>> hint = make_hint(img, depth_estimator).unsqueeze(0).half().to(\"cuda\")\n\n >>> prompt = \"A robot, 4k photo\"\n >>> negative_prior_prompt = \"lowres, text, error, cropped, worst quality, low quality, jpeg artifacts, ugly, duplicate, morbid, mutilated, out of frame, extra fingers, mutated hands, poorly drawn hands, poorly drawn face, mutation, deformed, blurry, dehydrated, bad anatomy, bad proportions, extra limbs, cloned face, disfigured, gross proportions, malformed limbs, missing arms, missing legs, extra arms, extra legs, fused fingers, too many fingers, long neck, username, watermark, signature\"\n\n >>> generator = torch.Generator(device=\"cuda\").manual_seed(43)\n\n >>> image_emb, zero_image_emb = pipe_prior(\n ... prompt=prompt, negative_prompt=negative_prior_prompt, generator=generator\n ... ).to_tuple()\n\n >>> images = pipe(\n ... image_embeds=image_emb,\n ... negative_image_embeds=zero_image_emb,\n ... hint=hint,\n ... num_inference_steps=50,\n ... generator=generator,\n ... height=768,\n ... width=768,\n ... ).images\n\n >>> images[0].save(\"robot_cat.png\")\n ```\n" def __UpperCAmelCase ( __lowerCamelCase , __lowerCamelCase , __lowerCamelCase=8 ) -> str: lowercase__ : Tuple = height // scale_factor**2 if height % scale_factor**2 != 0: new_height += 1 lowercase__ : str = width // scale_factor**2 if width % scale_factor**2 != 0: new_width += 1 return new_height * scale_factor, new_width * scale_factor class __A ( a_ ): '''simple docstring''' def __init__( self : List[Any] ,_snake_case : str ,_snake_case : List[str] ,_snake_case : int ,) -> List[Any]: """simple docstring""" super().__init__() self.register_modules( unet=_A ,scheduler=_A ,movq=_A ,) lowercase__ : Union[str, Any] = 2 ** (len(self.movq.config.block_out_channels ) - 1) def UpperCAmelCase ( self : Dict ,_snake_case : str ,_snake_case : List[str] ,_snake_case : Dict ,_snake_case : Optional[int] ,_snake_case : Any ,_snake_case : Union[str, Any] ) -> List[Any]: """simple docstring""" if latents is None: lowercase__ : Dict = randn_tensor(_A ,generator=_A ,device=_A ,dtype=_A ) else: if latents.shape != shape: raise ValueError(f"""Unexpected latents shape, got {latents.shape}, expected {shape}""" ) lowercase__ : Union[str, Any] = latents.to(_A ) lowercase__ : Tuple = latents * scheduler.init_noise_sigma return latents def UpperCAmelCase ( self : Optional[Any] ,_snake_case : List[Any]=0 ) -> Optional[Any]: """simple docstring""" if is_accelerate_available(): from accelerate import cpu_offload else: raise ImportError('''Please install accelerate via `pip install accelerate`''' ) lowercase__ : int = torch.device(f"""cuda:{gpu_id}""" ) lowercase__ : Union[str, Any] = [ self.unet, self.movq, ] for cpu_offloaded_model in models: if cpu_offloaded_model is not None: cpu_offload(_A ,_A ) def UpperCAmelCase ( self : Optional[int] ,_snake_case : List[Any]=0 ) -> List[str]: """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.''' ) lowercase__ : Optional[Any] = torch.device(f"""cuda:{gpu_id}""" ) if self.device.type != "cpu": self.to('''cpu''' ,silence_dtype_warnings=_A ) torch.cuda.empty_cache() # otherwise we don't see the memory savings (but they probably exist) lowercase__ : Optional[Any] = None for cpu_offloaded_model in [self.unet, self.movq]: lowercase__ , lowercase__ : str = cpu_offload_with_hook(_A ,_A ,prev_module_hook=_A ) # We'll offload the last model manually. lowercase__ : int = hook @property # Copied from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion.StableDiffusionPipeline._execution_device def UpperCAmelCase ( self : Optional[int] ) -> Any: """simple docstring""" if not hasattr(self.unet ,'''_hf_hook''' ): return self.device for module in self.unet.modules(): if ( hasattr(_A ,'''_hf_hook''' ) and hasattr(module._hf_hook ,'''execution_device''' ) and module._hf_hook.execution_device is not None ): return torch.device(module._hf_hook.execution_device ) return self.device @torch.no_grad() @replace_example_docstring(_A ) def __call__( self : Dict ,_snake_case : Any ,_snake_case : List[str] ,_snake_case : Any ,_snake_case : Dict = 512 ,_snake_case : Tuple = 512 ,_snake_case : Union[str, Any] = 100 ,_snake_case : Optional[int] = 4.0 ,_snake_case : int = 1 ,_snake_case : Any = None ,_snake_case : Any = None ,_snake_case : List[str] = "pil" ,_snake_case : List[str] = True ,) -> Any: """simple docstring""" lowercase__ : List[str] = self._execution_device lowercase__ : Optional[int] = guidance_scale > 1.0 if isinstance(_A ,_A ): lowercase__ : Union[str, Any] = torch.cat(_A ,dim=0 ) if isinstance(_A ,_A ): lowercase__ : Tuple = torch.cat(_A ,dim=0 ) if isinstance(_A ,_A ): lowercase__ : Optional[Any] = torch.cat(_A ,dim=0 ) lowercase__ : Tuple = image_embeds.shape[0] * num_images_per_prompt if do_classifier_free_guidance: lowercase__ : Union[str, Any] = image_embeds.repeat_interleave(_A ,dim=0 ) lowercase__ : Tuple = negative_image_embeds.repeat_interleave(_A ,dim=0 ) lowercase__ : str = hint.repeat_interleave(_A ,dim=0 ) lowercase__ : int = torch.cat([negative_image_embeds, image_embeds] ,dim=0 ).to(dtype=self.unet.dtype ,device=_A ) lowercase__ : Dict = torch.cat([hint, hint] ,dim=0 ).to(dtype=self.unet.dtype ,device=_A ) self.scheduler.set_timesteps(_A ,device=_A ) lowercase__ : str = self.scheduler.timesteps lowercase__ : str = self.movq.config.latent_channels lowercase__ , lowercase__ : Tuple = downscale_height_and_width(_A ,_A ,self.movq_scale_factor ) # create initial latent lowercase__ : str = self.prepare_latents( (batch_size, num_channels_latents, height, width) ,image_embeds.dtype ,_A ,_A ,_A ,self.scheduler ,) for i, t in enumerate(self.progress_bar(_A ) ): # expand the latents if we are doing classifier free guidance lowercase__ : List[str] = torch.cat([latents] * 2 ) if do_classifier_free_guidance else latents lowercase__ : Tuple = {'''image_embeds''': image_embeds, '''hint''': hint} lowercase__ : Union[str, Any] = self.unet( sample=_A ,timestep=_A ,encoder_hidden_states=_A ,added_cond_kwargs=_A ,return_dict=_A ,)[0] if do_classifier_free_guidance: lowercase__ , lowercase__ : int = noise_pred.split(latents.shape[1] ,dim=1 ) lowercase__ , lowercase__ : Optional[int] = noise_pred.chunk(2 ) lowercase__ , lowercase__ : str = variance_pred.chunk(2 ) lowercase__ : Any = noise_pred_uncond + guidance_scale * (noise_pred_text - noise_pred_uncond) lowercase__ : List[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"] ): lowercase__ , lowercase__ : str = noise_pred.split(latents.shape[1] ,dim=1 ) # compute the previous noisy sample x_t -> x_t-1 lowercase__ : List[Any] = self.scheduler.step( _A ,_A ,_A ,generator=_A ,)[0] # post-processing lowercase__ : str = self.movq.decode(_A ,force_not_quantize=_A )['''sample'''] if output_type not in ["pt", "np", "pil"]: raise ValueError(f"""Only the output types `pt`, `pil` and `np` are supported not output_type={output_type}""" ) if output_type in ["np", "pil"]: lowercase__ : List[str] = image * 0.5 + 0.5 lowercase__ : Optional[int] = image.clamp(0 ,1 ) lowercase__ : int = image.cpu().permute(0 ,2 ,3 ,1 ).float().numpy() if output_type == "pil": lowercase__ : List[str] = self.numpy_to_pil(_A ) if not return_dict: return (image,) return ImagePipelineOutput(images=_A )
16
import unittest from transformers import is_torch_available from transformers.testing_utils import require_sentencepiece, require_tokenizers, require_torch, slow if is_torch_available(): import torch from transformers import XLMRobertaModel @require_sentencepiece @require_tokenizers @require_torch class A_ (unittest.TestCase ): @slow def _lowercase ( self ): '''simple docstring''' UpperCAmelCase = XLMRobertaModel.from_pretrained('''xlm-roberta-base''' ) UpperCAmelCase = torch.tensor([[0, 5_8_1, 1_0_2_6_9, 8_3, 9_9_9_4_2, 1_3_6, 6_0_7_4_2, 2_3, 7_0, 8_0_5_8_3, 1_8_2_7_6, 2]] ) # The dog is cute and lives in the garden house UpperCAmelCase = torch.Size((1, 1_2, 7_6_8) ) # batch_size, sequence_length, embedding_vector_dim UpperCAmelCase = torch.tensor( [[-0.01_01, 0.12_18, -0.08_03, 0.08_01, 0.13_27, 0.07_76, -0.12_15, 0.23_83, 0.33_38, 0.31_06, 0.03_00, 0.02_52]] ) # xlmr = torch.hub.load('pytorch/fairseq', 'xlmr.base') # xlmr.eval() # expected_output_values_last_dim = xlmr.extract_features(input_ids[0])[:, :, -1] with torch.no_grad(): UpperCAmelCase = model(_A )['''last_hidden_state'''].detach() self.assertEqual(output.shape , _A ) # compare the actual values for a slice of last dim self.assertTrue(torch.allclose(output[:, :, -1] , _A , atol=1E-3 ) ) @slow def _lowercase ( self ): '''simple docstring''' UpperCAmelCase = XLMRobertaModel.from_pretrained('''xlm-roberta-large''' ) UpperCAmelCase = torch.tensor([[0, 5_8_1, 1_0_2_6_9, 8_3, 9_9_9_4_2, 1_3_6, 6_0_7_4_2, 2_3, 7_0, 8_0_5_8_3, 1_8_2_7_6, 2]] ) # The dog is cute and lives in the garden house UpperCAmelCase = torch.Size((1, 1_2, 1_0_2_4) ) # batch_size, sequence_length, embedding_vector_dim UpperCAmelCase = torch.tensor( [[-0.06_99, -0.03_18, 0.07_05, -0.12_41, 0.09_99, -0.05_20, 0.10_04, -0.18_38, -0.47_04, 0.14_37, 0.08_21, 0.01_26]] ) # xlmr = torch.hub.load('pytorch/fairseq', 'xlmr.large') # xlmr.eval() # expected_output_values_last_dim = xlmr.extract_features(input_ids[0])[:, :, -1] with torch.no_grad(): UpperCAmelCase = model(_A )['''last_hidden_state'''].detach() self.assertEqual(output.shape , _A ) # compare the actual values for a slice of last dim self.assertTrue(torch.allclose(output[:, :, -1] , _A , atol=1E-3 ) )
273
0
import os import unittest from transformers.models.transfo_xl.tokenization_transfo_xl import VOCAB_FILES_NAMES, TransfoXLTokenizer from ...test_tokenization_common import TokenizerTesterMixin class A_ ( a_ , unittest.TestCase ): '''simple docstring''' _UpperCamelCase : Tuple = TransfoXLTokenizer _UpperCamelCase : Union[str, Any] = False _UpperCamelCase : Optional[Any] = False def SCREAMING_SNAKE_CASE__ ( self ): super().setUp() lowercase = [ '<unk>', '[CLS]', '[SEP]', 'want', 'unwanted', 'wa', 'un', 'running', ',', 'low', 'l', ] lowercase = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES['vocab_file'] ) with open(self.vocab_file , 'w' , encoding='utf-8' ) as vocab_writer: vocab_writer.write(''.join([x + '\n' for x in vocab_tokens] ) ) def SCREAMING_SNAKE_CASE__ ( self , **snake_case ): lowercase = True return TransfoXLTokenizer.from_pretrained(self.tmpdirname , **_A ) def SCREAMING_SNAKE_CASE__ ( self , snake_case ): lowercase = '<unk> UNwanted , running' lowercase = '<unk> unwanted, running' return input_text, output_text def SCREAMING_SNAKE_CASE__ ( self ): lowercase = TransfoXLTokenizer(vocab_file=self.vocab_file , lower_case=_A ) lowercase = tokenizer.tokenize('<unk> UNwanted , running' ) self.assertListEqual(_A , ['<unk>', 'unwanted', ',', 'running'] ) self.assertListEqual(tokenizer.convert_tokens_to_ids(_A ) , [0, 4, 8, 7] ) def SCREAMING_SNAKE_CASE__ ( self ): lowercase = TransfoXLTokenizer(lower_case=_A ) self.assertListEqual( tokenizer.tokenize(' \tHeLLo ! how \n Are yoU ? ' ) , ['hello', '!', 'how', 'are', 'you', '?'] ) def SCREAMING_SNAKE_CASE__ ( self ): lowercase = TransfoXLTokenizer(lower_case=_A ) self.assertListEqual( tokenizer.tokenize(' \tHeLLo ! how \n Are yoU ? ' ) , ['HeLLo', '!', 'how', 'Are', 'yoU', '?'] ) def SCREAMING_SNAKE_CASE__ ( self ): lowercase = TransfoXLTokenizer(lower_case=_A ) lowercase = 'Hello (bracket) and side-scrolled [and] Henry\'s $5,000 with 3.34 m. What\'s up!?' lowercase = [ 'Hello', '(', 'bracket', ')', 'and', 'side', '@-@', 'scrolled', '[', 'and', ']', 'Henry', '\'s', '$', '5', '@,@', '000', 'with', '3', '@.@', '34', 'm', '.', 'What', '\'s', 'up', '!', '?', ] self.assertListEqual(tokenizer.tokenize(_A ) , _A ) self.assertEqual(tokenizer.convert_tokens_to_string(_A ) , _A ) def SCREAMING_SNAKE_CASE__ ( self ): lowercase = self.get_tokenizer() lowercase = len(_A ) tokenizer.add_tokens(['new1', 'new2'] ) tokenizer.move_added_token('new1' , 1 ) # Check that moved token is not copied (duplicate) self.assertEqual(len(_A ) , original_len + 2 ) # Check that token is moved to specified id self.assertEqual(tokenizer.encode('new1' ) , [1] ) self.assertEqual(tokenizer.decode([1] ) , 'new1' )
195
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") __A : Optional[int] = logging.getLogger(__name__) @dataclass class A_ : UpperCAmelCase__ = field( metadata={'''help''': '''Path to pretrained model or model identifier from huggingface.co/models'''} ) UpperCAmelCase__ = field( default=a_ , metadata={'''help''': '''Pretrained config name or path if not the same as model_name'''} ) UpperCAmelCase__ = field( default=a_ , metadata={'''help''': '''Pretrained tokenizer name or path if not the same as model_name'''} ) UpperCAmelCase__ = field( default=a_ , metadata={'''help''': '''Where do you want to store the pretrained models downloaded from huggingface.co'''} , ) UpperCAmelCase__ = field( default=a_ , 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=a_ , metadata={ '''help''': ( '''Will use the token generated when running `huggingface-cli login` (necessary to use this script ''' '''with private models).''' ) } , ) @dataclass class A_ : UpperCAmelCase__ = field(default=a_ , metadata={'''help''': '''The input training data file (a text file).'''} ) UpperCAmelCase__ = field( default=a_ , metadata={'''help''': '''An optional input evaluation data file to evaluate the perplexity on (a text file).'''} , ) UpperCAmelCase__ = field( default=a_ , metadata={'''help''': '''Overwrite the cached training and evaluation sets'''} ) UpperCAmelCase__ = field( default=a_ , metadata={'''help''': '''The number of processes to use for the preprocessing.'''} , ) UpperCAmelCase__ = field( default=a_ , 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=a_ , 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=a_ , metadata={ '''help''': ( '''For debugging purposes or quicker training, truncate the number of training examples to this ''' '''value if set.''' ) } , ) UpperCAmelCase__ = field( default=a_ , metadata={ '''help''': ( '''For debugging purposes or quicker training, truncate the number of evaluation examples to this ''' '''value if set.''' ) } , ) def _lowercase ( self ): '''simple docstring''' if self.train_file is not None: UpperCAmelCase = 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: UpperCAmelCase = self.validation_file.split('''.''' )[-1] assert extension in ["csv", "json"], "`validation_file` should be a csv or a json file." @dataclass class A_ : UpperCAmelCase__ = 42 UpperCAmelCase__ = True UpperCAmelCase__ = None UpperCAmelCase__ = None def __call__( self , _A ): '''simple docstring''' UpperCAmelCase = '''label''' if '''label''' in features[0].keys() else '''labels''' UpperCAmelCase = [feature.pop(_A ) for feature in features] UpperCAmelCase = len(_A ) UpperCAmelCase = len(features[0]['''input_ids'''] ) UpperCAmelCase = [ [{k: v[i] for k, v in feature.items()} for i in range(_A )] for feature in features ] UpperCAmelCase = list(chain(*_A ) ) UpperCAmelCase = self.tokenizer.pad( _A , padding=self.padding , max_length=self.max_length , pad_to_multiple_of=self.pad_to_multiple_of , return_tensors='''pt''' , ) # Un-flatten UpperCAmelCase = {k: v.view(_A , _A , -1 ) for k, v in batch.items()} # Add back labels UpperCAmelCase = torch.tensor(_A , dtype=torch.intaa ) return batch def __SCREAMING_SNAKE_CASE ( ) -> Optional[int]: '''simple docstring''' UpperCAmelCase = 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. UpperCAmelCase , UpperCAmelCase , UpperCAmelCase = parser.parse_json_file(json_file=os.path.abspath(sys.argv[1] ) ) else: UpperCAmelCase , UpperCAmelCase , UpperCAmelCase = 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''' , UpperCamelCase__ , UpperCamelCase__ ) # Setup logging logging.basicConfig( format='''%(asctime)s - %(levelname)s - %(name)s - %(message)s''' , datefmt='''%m/%d/%Y %H:%M:%S''' , handlers=[logging.StreamHandler(sys.stdout )] , ) if training_args.should_log: # The default of training_args.log_level is passive, so we set log level at info here to have that default. transformers.utils.logging.set_verbosity_info() UpperCAmelCase = training_args.get_process_log_level() logger.setLevel(UpperCamelCase__ ) datasets.utils.logging.set_verbosity(UpperCamelCase__ ) transformers.utils.logging.set_verbosity(UpperCamelCase__ ) transformers.utils.logging.enable_default_handler() transformers.utils.logging.enable_explicit_format() # Log on each process the small summary: logger.warning( F"""Process rank: {training_args.local_rank}, device: {training_args.device}, n_gpu: {training_args.n_gpu}""" + F"""distributed training: {bool(training_args.local_rank != -1 )}, 16-bits training: {training_args.fpaa}""" ) logger.info(F"""Training/evaluation parameters {training_args}""" ) # Detecting last checkpoint. UpperCAmelCase = None if os.path.isdir(training_args.output_dir ) and training_args.do_train and not training_args.overwrite_output_dir: UpperCAmelCase = 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: UpperCAmelCase = {} if data_args.train_file is not None: UpperCAmelCase = data_args.train_file if data_args.validation_file is not None: UpperCAmelCase = data_args.validation_file UpperCAmelCase = data_args.train_file.split('''.''' )[-1] UpperCAmelCase = load_dataset( UpperCamelCase__ , data_files=UpperCamelCase__ , 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. UpperCAmelCase = 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. UpperCAmelCase = 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 , ) UpperCAmelCase = 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 , ) UpperCAmelCase = AutoModelForMultipleChoice.from_pretrained( model_args.model_name_or_path , from_tf=bool('''.ckpt''' in model_args.model_name_or_path ) , config=UpperCamelCase__ , cache_dir=model_args.cache_dir , revision=model_args.model_revision , use_auth_token=True if model_args.use_auth_token else None , ) # When using your own dataset or a different dataset from swag, you will probably need to change this. UpperCAmelCase = [F"""ending{i}""" for i in range(4 )] UpperCAmelCase = '''sent1''' UpperCAmelCase = '''sent2''' if data_args.max_seq_length is None: UpperCAmelCase = tokenizer.model_max_length if max_seq_length > 1024: 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`.''' ) UpperCAmelCase = 1024 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}.""" ) UpperCAmelCase = min(data_args.max_seq_length , tokenizer.model_max_length ) # Preprocessing the datasets. def preprocess_function(UpperCamelCase__ ): UpperCAmelCase = [[context] * 4 for context in examples[context_name]] UpperCAmelCase = examples[question_header_name] UpperCAmelCase = [ [F"""{header} {examples[end][i]}""" for end in ending_names] for i, header in enumerate(UpperCamelCase__ ) ] # Flatten out UpperCAmelCase = list(chain(*UpperCamelCase__ ) ) UpperCAmelCase = list(chain(*UpperCamelCase__ ) ) # Tokenize UpperCAmelCase = tokenizer( UpperCamelCase__ , UpperCamelCase__ , truncation=UpperCamelCase__ , max_length=UpperCamelCase__ , 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(UpperCamelCase__ ) , 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''' ) UpperCAmelCase = raw_datasets['''train'''] if data_args.max_train_samples is not None: UpperCAmelCase = min(len(UpperCamelCase__ ) , data_args.max_train_samples ) UpperCAmelCase = train_dataset.select(range(UpperCamelCase__ ) ) with training_args.main_process_first(desc='''train dataset map pre-processing''' ): UpperCAmelCase = train_dataset.map( UpperCamelCase__ , batched=UpperCamelCase__ , 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''' ) UpperCAmelCase = raw_datasets['''validation'''] if data_args.max_eval_samples is not None: UpperCAmelCase = min(len(UpperCamelCase__ ) , data_args.max_eval_samples ) UpperCAmelCase = eval_dataset.select(range(UpperCamelCase__ ) ) with training_args.main_process_first(desc='''validation dataset map pre-processing''' ): UpperCAmelCase = eval_dataset.map( UpperCamelCase__ , batched=UpperCamelCase__ , num_proc=data_args.preprocessing_num_workers , load_from_cache_file=not data_args.overwrite_cache , ) # Data collator UpperCAmelCase = ( default_data_collator if data_args.pad_to_max_length else DataCollatorForMultipleChoice(tokenizer=UpperCamelCase__ , pad_to_multiple_of=8 if training_args.fpaa else None ) ) # Metric def compute_metrics(UpperCamelCase__ ): UpperCAmelCase , UpperCAmelCase = eval_predictions UpperCAmelCase = np.argmax(UpperCamelCase__ , axis=1 ) return {"accuracy": (preds == label_ids).astype(np.floataa ).mean().item()} # Initialize our Trainer UpperCAmelCase = Trainer( model=UpperCamelCase__ , args=UpperCamelCase__ , train_dataset=train_dataset if training_args.do_train else None , eval_dataset=eval_dataset if training_args.do_eval else None , tokenizer=UpperCamelCase__ , data_collator=UpperCamelCase__ , compute_metrics=UpperCamelCase__ , ) # Training if training_args.do_train: UpperCAmelCase = None if training_args.resume_from_checkpoint is not None: UpperCAmelCase = training_args.resume_from_checkpoint elif last_checkpoint is not None: UpperCAmelCase = last_checkpoint UpperCAmelCase = trainer.train(resume_from_checkpoint=UpperCamelCase__ ) trainer.save_model() # Saves the tokenizer too for easy upload UpperCAmelCase = train_result.metrics UpperCAmelCase = ( data_args.max_train_samples if data_args.max_train_samples is not None else len(UpperCamelCase__ ) ) UpperCAmelCase = min(UpperCamelCase__ , len(UpperCamelCase__ ) ) trainer.log_metrics('''train''' , UpperCamelCase__ ) trainer.save_metrics('''train''' , UpperCamelCase__ ) trainer.save_state() # Evaluation if training_args.do_eval: logger.info('''*** Evaluate ***''' ) UpperCAmelCase = trainer.evaluate() UpperCAmelCase = data_args.max_eval_samples if data_args.max_eval_samples is not None else len(UpperCamelCase__ ) UpperCAmelCase = min(UpperCamelCase__ , len(UpperCamelCase__ ) ) trainer.log_metrics('''eval''' , UpperCamelCase__ ) trainer.save_metrics('''eval''' , UpperCamelCase__ ) UpperCAmelCase = { '''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(**UpperCamelCase__ ) else: trainer.create_model_card(**UpperCamelCase__ ) def __SCREAMING_SNAKE_CASE ( UpperCamelCase__ ) -> int: '''simple docstring''' main() if __name__ == "__main__": main()
273
0
'''simple docstring''' import warnings from typing import List, Optional, Union from ...processing_utils import ProcessorMixin from ...tokenization_utils_base import BatchEncoding, PaddingStrategy, PreTokenizedInput, TextInput, TruncationStrategy from ...utils import TensorType class __lowerCAmelCase ( a_ ): """simple docstring""" _snake_case : int = ['image_processor', 'tokenizer'] _snake_case : Any = 'LayoutLMv2ImageProcessor' _snake_case : Any = ('LayoutXLMTokenizer', 'LayoutXLMTokenizerFast') def __init__( self : int , lowerCAmelCase__ : int=None , lowerCAmelCase__ : Union[str, Any]=None , **lowerCAmelCase__ : Optional[int] ) -> Any: '''simple docstring''' if "feature_extractor" in kwargs: warnings.warn( '''The `feature_extractor` argument is deprecated and will be removed in v5, use `image_processor`''' ''' instead.''' , _A , ) _UpperCamelCase = kwargs.pop('''feature_extractor''' ) _UpperCamelCase = image_processor if image_processor is not None else feature_extractor if image_processor is None: raise ValueError('''You need to specify an `image_processor`.''' ) if tokenizer is None: raise ValueError('''You need to specify a `tokenizer`.''' ) super().__init__(_A , _A ) def __call__( self : Optional[Any] , lowerCAmelCase__ : List[str] , lowerCAmelCase__ : Optional[int] = None , lowerCAmelCase__ : Tuple = None , lowerCAmelCase__ : Tuple = None , lowerCAmelCase__ : Tuple = None , lowerCAmelCase__ : Dict = True , lowerCAmelCase__ : List[Any] = False , lowerCAmelCase__ : str = None , lowerCAmelCase__ : int = None , lowerCAmelCase__ : str = 0 , lowerCAmelCase__ : Union[str, Any] = None , lowerCAmelCase__ : List[str] = None , lowerCAmelCase__ : str = None , lowerCAmelCase__ : Optional[Any] = False , lowerCAmelCase__ : Optional[int] = False , lowerCAmelCase__ : List[Any] = False , lowerCAmelCase__ : str = False , lowerCAmelCase__ : List[Any] = True , lowerCAmelCase__ : str = None , **lowerCAmelCase__ : Any , ) -> Optional[Any]: '''simple docstring''' if self.image_processor.apply_ocr and (boxes is not None): raise ValueError( '''You cannot provide bounding boxes ''' '''if you initialized the image processor with apply_ocr set to True.''' ) if self.image_processor.apply_ocr and (word_labels is not None): raise ValueError( '''You cannot provide word labels if you initialized the image processor with apply_ocr set to True.''' ) if return_overflowing_tokens is True and return_offsets_mapping is False: raise ValueError('''You cannot return overflowing tokens without returning the offsets mapping.''' ) # first, apply the image processor _UpperCamelCase = self.image_processor(images=_A , return_tensors=_A ) # second, apply the tokenizer if text is not None and self.image_processor.apply_ocr and text_pair is None: if isinstance(_A , _A ): _UpperCamelCase = [text] # add batch dimension (as the image processor always adds a batch dimension) _UpperCamelCase = features['''words'''] _UpperCamelCase = self.tokenizer( text=text if text is not None else features['''words'''] , text_pair=text_pair if text_pair is not None else None , boxes=boxes if boxes is not None else features['''boxes'''] , word_labels=_A , add_special_tokens=_A , padding=_A , truncation=_A , max_length=_A , stride=_A , pad_to_multiple_of=_A , return_token_type_ids=_A , return_attention_mask=_A , return_overflowing_tokens=_A , return_special_tokens_mask=_A , return_offsets_mapping=_A , return_length=_A , verbose=_A , return_tensors=_A , **_A , ) # add pixel values _UpperCamelCase = features.pop('''pixel_values''' ) if return_overflowing_tokens is True: _UpperCamelCase = self.get_overflowing_images(_A , encoded_inputs['''overflow_to_sample_mapping'''] ) _UpperCamelCase = images return encoded_inputs def snake_case__ ( self : Dict , lowerCAmelCase__ : Any , lowerCAmelCase__ : Tuple ) -> Optional[int]: '''simple docstring''' _UpperCamelCase = [] for sample_idx in overflow_to_sample_mapping: images_with_overflow.append(images[sample_idx] ) if len(_A ) != len(_A ): raise ValueError( '''Expected length of images to be the same as the length of `overflow_to_sample_mapping`, but got''' f""" {len(_A )} and {len(_A )}""" ) return images_with_overflow def snake_case__ ( self : List[str] , *lowerCAmelCase__ : List[Any] , **lowerCAmelCase__ : int ) -> int: '''simple docstring''' return self.tokenizer.batch_decode(*_A , **_A ) def snake_case__ ( self : Any , *lowerCAmelCase__ : List[str] , **lowerCAmelCase__ : Optional[int] ) -> int: '''simple docstring''' return self.tokenizer.decode(*_A , **_A ) @property def snake_case__ ( self : Union[str, Any] ) -> Optional[int]: '''simple docstring''' return ["input_ids", "bbox", "attention_mask", "image"] @property def snake_case__ ( self : List[Any] ) -> Dict: '''simple docstring''' warnings.warn( '''`feature_extractor_class` is deprecated and will be removed in v5. Use `image_processor_class` instead.''' , _A , ) return self.image_processor_class @property def snake_case__ ( self : Dict ) -> Dict: '''simple docstring''' warnings.warn( '''`feature_extractor` is deprecated and will be removed in v5. Use `image_processor` instead.''' , _A , ) return self.image_processor
324
from __future__ import annotations import unittest from transformers import AutoTokenizer, MBartConfig, is_tf_available from transformers.testing_utils import require_sentencepiece, require_tf, require_tokenizers, slow from transformers.utils import cached_property from ...test_configuration_common import ConfigTester from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_tf_available(): import tensorflow as tf from transformers import TFAutoModelForSeqaSeqLM, TFMBartForConditionalGeneration, TFMBartModel @require_tf class A_ : UpperCAmelCase__ = MBartConfig UpperCAmelCase__ = {} UpperCAmelCase__ = '''gelu''' def __init__( self , _A , _A=1_3 , _A=7 , _A=True , _A=False , _A=9_9 , _A=3_2 , _A=2 , _A=4 , _A=3_7 , _A=0.1 , _A=0.1 , _A=2_0 , _A=2 , _A=1 , _A=0 , ): '''simple docstring''' UpperCAmelCase = parent UpperCAmelCase = batch_size UpperCAmelCase = seq_length UpperCAmelCase = is_training UpperCAmelCase = use_labels UpperCAmelCase = vocab_size UpperCAmelCase = hidden_size UpperCAmelCase = num_hidden_layers UpperCAmelCase = num_attention_heads UpperCAmelCase = intermediate_size UpperCAmelCase = hidden_dropout_prob UpperCAmelCase = attention_probs_dropout_prob UpperCAmelCase = max_position_embeddings UpperCAmelCase = eos_token_id UpperCAmelCase = pad_token_id UpperCAmelCase = bos_token_id def _lowercase ( self ): '''simple docstring''' UpperCAmelCase = ids_tensor([self.batch_size, self.seq_length - 1] , self.vocab_size ) UpperCAmelCase = tf.expand_dims(tf.constant([self.eos_token_id] * self.batch_size ) , 1 ) UpperCAmelCase = tf.concat([input_ids, eos_tensor] , axis=1 ) UpperCAmelCase = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) UpperCAmelCase = self.config_cls( vocab_size=self.vocab_size , d_model=self.hidden_size , encoder_layers=self.num_hidden_layers , decoder_layers=self.num_hidden_layers , encoder_attention_heads=self.num_attention_heads , decoder_attention_heads=self.num_attention_heads , encoder_ffn_dim=self.intermediate_size , decoder_ffn_dim=self.intermediate_size , dropout=self.hidden_dropout_prob , attention_dropout=self.attention_probs_dropout_prob , max_position_embeddings=self.max_position_embeddings , eos_token_ids=[2] , bos_token_id=self.bos_token_id , pad_token_id=self.pad_token_id , decoder_start_token_id=self.pad_token_id , **self.config_updates , ) UpperCAmelCase = prepare_mbart_inputs_dict(_A , _A , _A ) return config, inputs_dict def _lowercase ( self , _A , _A ): '''simple docstring''' UpperCAmelCase = TFMBartModel(config=_A ).get_decoder() UpperCAmelCase = inputs_dict['''input_ids'''] UpperCAmelCase = input_ids[:1, :] UpperCAmelCase = inputs_dict['''attention_mask'''][:1, :] UpperCAmelCase = inputs_dict['''head_mask'''] UpperCAmelCase = 1 # first forward pass UpperCAmelCase = model(_A , attention_mask=_A , head_mask=_A , use_cache=_A ) UpperCAmelCase , UpperCAmelCase = outputs.to_tuple() UpperCAmelCase = past_key_values[1] def __SCREAMING_SNAKE_CASE ( UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__=None , UpperCamelCase__=None , UpperCamelCase__=None , UpperCamelCase__=None , UpperCamelCase__=None , ) -> List[str]: '''simple docstring''' if attention_mask is None: UpperCAmelCase = tf.cast(tf.math.not_equal(UpperCamelCase__ , config.pad_token_id ) , tf.inta ) if decoder_attention_mask is None: UpperCAmelCase = tf.concat( [ tf.ones(decoder_input_ids[:, :1].shape , dtype=tf.inta ), tf.cast(tf.math.not_equal(decoder_input_ids[:, 1:] , config.pad_token_id ) , tf.inta ), ] , axis=-1 , ) if head_mask is None: UpperCAmelCase = tf.ones((config.encoder_layers, config.encoder_attention_heads) ) if decoder_head_mask is None: UpperCAmelCase = tf.ones((config.decoder_layers, config.decoder_attention_heads) ) if cross_attn_head_mask is None: UpperCAmelCase = tf.ones((config.decoder_layers, config.decoder_attention_heads) ) return { "input_ids": input_ids, "decoder_input_ids": decoder_input_ids, "attention_mask": attention_mask, "decoder_attention_mask": decoder_attention_mask, "head_mask": head_mask, "decoder_head_mask": decoder_head_mask, "cross_attn_head_mask": cross_attn_head_mask, } @require_tf class A_ (a_ , a_ , unittest.TestCase ): UpperCAmelCase__ = (TFMBartForConditionalGeneration, TFMBartModel) if is_tf_available() else () UpperCAmelCase__ = (TFMBartForConditionalGeneration,) if is_tf_available() else () UpperCAmelCase__ = ( { '''conversational''': TFMBartForConditionalGeneration, '''feature-extraction''': TFMBartModel, '''summarization''': TFMBartForConditionalGeneration, '''text2text-generation''': TFMBartForConditionalGeneration, '''translation''': TFMBartForConditionalGeneration, } if is_tf_available() else {} ) UpperCAmelCase__ = True UpperCAmelCase__ = False UpperCAmelCase__ = False def _lowercase ( self , _A , _A , _A , _A , _A ): '''simple docstring''' if pipeline_test_casse_name != "FeatureExtractionPipelineTests": # Exception encountered when calling layer '...' return True return False def _lowercase ( self ): '''simple docstring''' UpperCAmelCase = TFMBartModelTester(self ) UpperCAmelCase = ConfigTester(self , config_class=_A ) def _lowercase ( self ): '''simple docstring''' self.config_tester.run_common_tests() def _lowercase ( self ): '''simple docstring''' UpperCAmelCase = self.model_tester.prepare_config_and_inputs_for_common() self.model_tester.check_decoder_model_past_large_inputs(*_A ) @require_sentencepiece @require_tokenizers @require_tf class A_ (unittest.TestCase ): UpperCAmelCase__ = [ ''' UN Chief Says There Is No Military Solution in Syria''', ] UpperCAmelCase__ = [ '''Şeful ONU declară că nu există o soluţie militară în Siria''', ] UpperCAmelCase__ = '''facebook/mbart-large-en-ro''' @cached_property def _lowercase ( self ): '''simple docstring''' return AutoTokenizer.from_pretrained(self.model_name ) @cached_property def _lowercase ( self ): '''simple docstring''' UpperCAmelCase = TFAutoModelForSeqaSeqLM.from_pretrained(self.model_name ) return model def _lowercase ( self , **_A ): '''simple docstring''' UpperCAmelCase = self.translate_src_text(**_A ) self.assertListEqual(self.expected_text , _A ) def _lowercase ( self , **_A ): '''simple docstring''' UpperCAmelCase = self.tokenizer(self.src_text , **_A , return_tensors='''tf''' ) UpperCAmelCase = self.model.generate( model_inputs.input_ids , attention_mask=model_inputs.attention_mask , num_beams=2 ) UpperCAmelCase = self.tokenizer.batch_decode(_A , skip_special_tokens=_A ) return generated_words @slow def _lowercase ( self ): '''simple docstring''' self._assert_generated_batch_equal_expected()
273
0
import os from shutil import copyfile from typing import Any, Dict, List, Optional, Tuple import sentencepiece as spm from ...tokenization_utils import AddedToken, PreTrainedTokenizer from ...utils import logging lowerCamelCase_ = logging.get_logger(__name__) lowerCamelCase_ = {"vocab_file": "sentencepiece.bpe.model"} lowerCamelCase_ = { "vocab_file": { "camembert-base": "https://huggingface.co/camembert-base/resolve/main/sentencepiece.bpe.model", } } lowerCamelCase_ = { "camembert-base": 5_12, } lowerCamelCase_ = "▁" class __A( a_ ): """simple docstring""" SCREAMING_SNAKE_CASE__ = VOCAB_FILES_NAMES SCREAMING_SNAKE_CASE__ = PRETRAINED_VOCAB_FILES_MAP SCREAMING_SNAKE_CASE__ = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES SCREAMING_SNAKE_CASE__ = ["""input_ids""", """attention_mask"""] def __init__(self , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_="<s>" , SCREAMING_SNAKE_CASE_="</s>" , SCREAMING_SNAKE_CASE_="</s>" , SCREAMING_SNAKE_CASE_="<s>" , SCREAMING_SNAKE_CASE_="<unk>" , SCREAMING_SNAKE_CASE_="<pad>" , SCREAMING_SNAKE_CASE_="<mask>" , SCREAMING_SNAKE_CASE_=["<s>NOTUSED", "</s>NOTUSED"] , SCREAMING_SNAKE_CASE_ = None , **SCREAMING_SNAKE_CASE_ , ): UpperCamelCase__ = AddedToken(_A , lstrip=_A , rstrip=_A ) if isinstance(_A , _A ) else mask_token UpperCamelCase__ = {} if sp_model_kwargs is None else sp_model_kwargs super().__init__( bos_token=_A , eos_token=_A , unk_token=_A , sep_token=_A , cls_token=_A , pad_token=_A , mask_token=_A , additional_special_tokens=_A , sp_model_kwargs=self.sp_model_kwargs , **_A , ) UpperCamelCase__ = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(str(_A ) ) UpperCamelCase__ = vocab_file # HACK: These tokens were added by fairseq but don't seem to be actually used when duplicated in the actual # sentencepiece vocabulary (this is the case for <s> and </s> UpperCamelCase__ = {"""<s>NOTUSED""": 0, """<pad>""": 1, """</s>NOTUSED""": 2, """<unk>""": 3} UpperCamelCase__ = len(self.fairseq_tokens_to_ids ) UpperCamelCase__ = len(self.sp_model ) + len(self.fairseq_tokens_to_ids ) UpperCamelCase__ = {v: k for k, v in self.fairseq_tokens_to_ids.items()} def UpperCAmelCase_ (self , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = None ): if token_ids_a is None: return [self.cls_token_id] + token_ids_a + [self.sep_token_id] UpperCamelCase__ = [self.cls_token_id] UpperCamelCase__ = [self.sep_token_id] return cls + token_ids_a + sep + sep + token_ids_a + sep def UpperCAmelCase_ (self , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = None , SCREAMING_SNAKE_CASE_ = False ): if already_has_special_tokens: return super().get_special_tokens_mask( token_ids_a=_A , token_ids_a=_A , already_has_special_tokens=_A ) if token_ids_a is None: return [1] + ([0] * len(_A )) + [1] return [1] + ([0] * len(_A )) + [1, 1] + ([0] * len(_A )) + [1] def UpperCAmelCase_ (self , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = None ): UpperCamelCase__ = [self.sep_token_id] UpperCamelCase__ = [self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep ) * [0] return len(cls + token_ids_a + sep + sep + token_ids_a + sep ) * [0] @property def UpperCAmelCase_ (self ): return len(self.fairseq_tokens_to_ids ) + len(self.sp_model ) def UpperCAmelCase_ (self ): UpperCamelCase__ = {self.convert_ids_to_tokens(_A ): i for i in range(self.vocab_size )} vocab.update(self.added_tokens_encoder ) return vocab def UpperCAmelCase_ (self , SCREAMING_SNAKE_CASE_ ): return self.sp_model.encode(_A , out_type=_A ) def UpperCAmelCase_ (self , SCREAMING_SNAKE_CASE_ ): if token in self.fairseq_tokens_to_ids: return self.fairseq_tokens_to_ids[token] elif self.sp_model.PieceToId(_A ) == 0: # Convert sentence piece unk token to fairseq unk token index return self.unk_token_id return self.fairseq_offset + self.sp_model.PieceToId(_A ) def UpperCAmelCase_ (self , SCREAMING_SNAKE_CASE_ ): 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 UpperCAmelCase_ (self , SCREAMING_SNAKE_CASE_ ): UpperCamelCase__ = [] UpperCamelCase__ = """""" UpperCamelCase__ = False for token in tokens: # make sure that special tokens are not decoded using sentencepiece model if token in self.all_special_tokens: if not prev_is_special: out_string += " " out_string += self.sp_model.decode(_A ) + token UpperCamelCase__ = True UpperCamelCase__ = [] else: current_sub_tokens.append(_A ) UpperCamelCase__ = False out_string += self.sp_model.decode(_A ) return out_string.strip() def __getstate__(self ): UpperCamelCase__ = self.__dict__.copy() UpperCamelCase__ = None return state def __setstate__(self , SCREAMING_SNAKE_CASE_ ): UpperCamelCase__ = d # for backward compatibility if not hasattr(self , """sp_model_kwargs""" ): UpperCamelCase__ = {} UpperCamelCase__ = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(self.vocab_file ) def UpperCAmelCase_ (self , SCREAMING_SNAKE_CASE_ , SCREAMING_SNAKE_CASE_ = None ): if not os.path.isdir(_A ): logger.error(F"Vocabulary path ({save_directory}) should be a directory" ) return UpperCamelCase__ = os.path.join( _A , (filename_prefix + """-""" if filename_prefix else """""") + VOCAB_FILES_NAMES["""vocab_file"""] ) if os.path.abspath(self.vocab_file ) != os.path.abspath(_A ) and os.path.isfile(self.vocab_file ): copyfile(self.vocab_file , _A ) elif not os.path.isfile(self.vocab_file ): with open(_A , """wb""" ) as fi: UpperCamelCase__ = self.sp_model.serialized_model_proto() fi.write(_A ) return (out_vocab_file,)
244
import gc import unittest from transformers import CTRLConfig, 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 ( CTRL_PRETRAINED_MODEL_ARCHIVE_LIST, CTRLForSequenceClassification, CTRLLMHeadModel, CTRLModel, ) class A_ : def __init__( self , _A , _A=1_4 , _A=7 , _A=True , _A=True , _A=True , _A=True , _A=True , _A=9_9 , _A=3_2 , _A=5 , _A=4 , _A=3_7 , _A="gelu" , _A=0.1 , _A=0.1 , _A=5_1_2 , _A=1_6 , _A=2 , _A=0.02 , _A=3 , _A=4 , _A=None , ): '''simple docstring''' UpperCAmelCase = parent UpperCAmelCase = batch_size UpperCAmelCase = seq_length UpperCAmelCase = is_training UpperCAmelCase = use_token_type_ids UpperCAmelCase = use_input_mask UpperCAmelCase = use_labels UpperCAmelCase = use_mc_token_ids UpperCAmelCase = vocab_size UpperCAmelCase = hidden_size UpperCAmelCase = num_hidden_layers UpperCAmelCase = num_attention_heads UpperCAmelCase = intermediate_size UpperCAmelCase = hidden_act UpperCAmelCase = hidden_dropout_prob UpperCAmelCase = attention_probs_dropout_prob UpperCAmelCase = max_position_embeddings UpperCAmelCase = type_vocab_size UpperCAmelCase = type_sequence_label_size UpperCAmelCase = initializer_range UpperCAmelCase = num_labels UpperCAmelCase = num_choices UpperCAmelCase = scope UpperCAmelCase = self.vocab_size - 1 def _lowercase ( self ): '''simple docstring''' UpperCAmelCase = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) UpperCAmelCase = None if self.use_input_mask: UpperCAmelCase = random_attention_mask([self.batch_size, self.seq_length] ) UpperCAmelCase = None if self.use_token_type_ids: UpperCAmelCase = ids_tensor([self.batch_size, self.seq_length] , self.type_vocab_size ) UpperCAmelCase = None if self.use_mc_token_ids: UpperCAmelCase = ids_tensor([self.batch_size, self.num_choices] , self.seq_length ) UpperCAmelCase = None UpperCAmelCase = None UpperCAmelCase = None if self.use_labels: UpperCAmelCase = ids_tensor([self.batch_size] , self.type_sequence_label_size ) UpperCAmelCase = ids_tensor([self.batch_size, self.seq_length] , self.num_labels ) UpperCAmelCase = ids_tensor([self.batch_size] , self.num_choices ) UpperCAmelCase = self.get_config() UpperCAmelCase = ids_tensor([self.num_hidden_layers, self.num_attention_heads] , 2 ) return ( config, input_ids, input_mask, head_mask, token_type_ids, mc_token_ids, sequence_labels, token_labels, choice_labels, ) def _lowercase ( self ): '''simple docstring''' return CTRLConfig( vocab_size=self.vocab_size , n_embd=self.hidden_size , n_layer=self.num_hidden_layers , n_head=self.num_attention_heads , n_positions=self.max_position_embeddings , pad_token_id=self.pad_token_id , ) def _lowercase ( self , _A , _A , _A , _A , _A , *_A ): '''simple docstring''' UpperCAmelCase = CTRLModel(config=_A ) model.to(_A ) model.eval() model(_A , token_type_ids=_A , head_mask=_A ) model(_A , token_type_ids=_A ) UpperCAmelCase = model(_A ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) self.parent.assertEqual(len(result.past_key_values ) , config.n_layer ) def _lowercase ( self , _A , _A , _A , _A , _A , *_A ): '''simple docstring''' UpperCAmelCase = CTRLLMHeadModel(_A ) model.to(_A ) model.eval() UpperCAmelCase = model(_A , token_type_ids=_A , labels=_A ) self.parent.assertEqual(result.loss.shape , () ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.vocab_size) ) def _lowercase ( self ): '''simple docstring''' UpperCAmelCase = self.prepare_config_and_inputs() ( ( UpperCAmelCase ) , ( UpperCAmelCase ) , ( UpperCAmelCase ) , ( UpperCAmelCase ) , ( UpperCAmelCase ) , ( UpperCAmelCase ) , ( UpperCAmelCase ) , ( UpperCAmelCase ) , ( UpperCAmelCase ) , ) = config_and_inputs UpperCAmelCase = {'''input_ids''': input_ids, '''token_type_ids''': token_type_ids, '''head_mask''': head_mask} return config, inputs_dict def _lowercase ( self , _A , _A , _A , _A , *_A ): '''simple docstring''' UpperCAmelCase = self.num_labels UpperCAmelCase = CTRLForSequenceClassification(_A ) model.to(_A ) model.eval() UpperCAmelCase = ids_tensor([self.batch_size] , self.type_sequence_label_size ) UpperCAmelCase = model(_A , token_type_ids=_A , labels=_A ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_labels) ) @require_torch class A_ (a_ , a_ , a_ , unittest.TestCase ): UpperCAmelCase__ = (CTRLModel, CTRLLMHeadModel, CTRLForSequenceClassification) if is_torch_available() else () UpperCAmelCase__ = (CTRLLMHeadModel,) if is_torch_available() else () UpperCAmelCase__ = ( { '''feature-extraction''': CTRLModel, '''text-classification''': CTRLForSequenceClassification, '''text-generation''': CTRLLMHeadModel, '''zero-shot''': CTRLForSequenceClassification, } if is_torch_available() else {} ) UpperCAmelCase__ = True UpperCAmelCase__ = False UpperCAmelCase__ = False def _lowercase ( self , _A , _A , _A , _A , _A ): '''simple docstring''' if pipeline_test_casse_name == "ZeroShotClassificationPipelineTests": # Get `tokenizer does not have a padding token` error for both fast/slow tokenizers. # `CTRLConfig` was never used in pipeline tests, either because of a missing checkpoint or because a tiny # config could not be created. return True return False def _lowercase ( self ): '''simple docstring''' UpperCAmelCase = CTRLModelTester(self ) UpperCAmelCase = ConfigTester(self , config_class=_A , n_embd=3_7 ) def _lowercase ( self ): '''simple docstring''' super().tearDown() # clean-up as much as possible GPU memory occupied by PyTorch gc.collect() torch.cuda.empty_cache() def _lowercase ( self ): '''simple docstring''' self.config_tester.run_common_tests() def _lowercase ( self ): '''simple docstring''' UpperCAmelCase = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_ctrl_model(*_A ) def _lowercase ( self ): '''simple docstring''' UpperCAmelCase = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_lm_head_model(*_A ) @unittest.skip('''Will be fixed soon by reducing the size of the model used for common tests.''' ) def _lowercase ( self ): '''simple docstring''' pass @slow def _lowercase ( self ): '''simple docstring''' for model_name in CTRL_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: UpperCAmelCase = CTRLModel.from_pretrained(_A ) self.assertIsNotNone(_A ) @unittest.skip('''The model doesn\'t support left padding''' ) # and it's not used enough to be worth fixing :) def _lowercase ( self ): '''simple docstring''' pass @require_torch class A_ (unittest.TestCase ): def _lowercase ( self ): '''simple docstring''' super().tearDown() # clean-up as much as possible GPU memory occupied by PyTorch gc.collect() torch.cuda.empty_cache() @slow def _lowercase ( self ): '''simple docstring''' UpperCAmelCase = CTRLLMHeadModel.from_pretrained('''ctrl''' ) model.to(_A ) UpperCAmelCase = torch.tensor( [[1_1_8_5_9, 0, 1_6_1_1, 8]] , dtype=torch.long , device=_A ) # Legal the president is UpperCAmelCase = [ 1_1_8_5_9, 0, 1_6_1_1, 8, 5, 1_5_0, 2_6_4_4_9, 2, 1_9, 3_4_8, 4_6_9, 3, 2_5_9_5, 4_8, 2_0_7_4_0, 2_4_6_5_3_3, 2_4_6_5_3_3, 1_9, 3_0, 5, ] # Legal the president is a good guy and I don't want to lose my job. \n \n I have a UpperCAmelCase = model.generate(_A , do_sample=_A ) self.assertListEqual(output_ids[0].tolist() , _A )
273
0
import inspect import os import unittest import torch import accelerate from accelerate import debug_launcher from accelerate.test_utils import ( execute_subprocess_async, require_cpu, require_huggingface_suite, require_multi_gpu, require_single_gpu, ) from accelerate.utils import patch_environment @require_huggingface_suite class snake_case_ (unittest.TestCase ): def lowerCamelCase__( self :Optional[Any] ) -> Dict: a__ = inspect.getfile(accelerate.test_utils ) a__ = os.path.sep.join( mod_file.split(os.path.sep )[:-1] + ['scripts', 'external_deps', 'test_metrics.py'] ) from accelerate.test_utils.scripts.external_deps import test_metrics # noqa: F401 a__ = test_metrics @require_cpu def lowerCamelCase__( self :Union[str, Any] ) -> Dict: debug_launcher(self.test_metrics.main ,num_processes=1 ) @require_cpu def lowerCamelCase__( self :List[Any] ) -> int: debug_launcher(self.test_metrics.main ) @require_single_gpu def lowerCamelCase__( self :Any ) -> Dict: self.test_metrics.main() @require_multi_gpu def lowerCamelCase__( self :Tuple ) -> str: print(F'Found {torch.cuda.device_count()} devices.' ) a__ = ['torchrun', F'--nproc_per_node={torch.cuda.device_count()}', self.test_file_path] with patch_environment(omp_num_threads=1 ): execute_subprocess_async(_A ,env=os.environ.copy() )
240
import cva import numpy as np class A_ : def __init__( self , _A , _A ): '''simple docstring''' if k in (0.04, 0.06): UpperCAmelCase = k UpperCAmelCase = window_size else: raise ValueError('''invalid k value''' ) def __str__( self ): '''simple docstring''' return str(self.k ) def _lowercase ( self , _A ): '''simple docstring''' UpperCAmelCase = cva.imread(_A , 0 ) UpperCAmelCase , UpperCAmelCase = img.shape UpperCAmelCase = [] UpperCAmelCase = img.copy() UpperCAmelCase = cva.cvtColor(_A , cva.COLOR_GRAY2RGB ) UpperCAmelCase , UpperCAmelCase = np.gradient(_A ) UpperCAmelCase = dx**2 UpperCAmelCase = dy**2 UpperCAmelCase = dx * dy UpperCAmelCase = 0.04 UpperCAmelCase = self.window_size // 2 for y in range(_A , h - offset ): for x in range(_A , w - offset ): UpperCAmelCase = ixx[ y - offset : y + offset + 1, x - offset : x + offset + 1 ].sum() UpperCAmelCase = iyy[ y - offset : y + offset + 1, x - offset : x + offset + 1 ].sum() UpperCAmelCase = ixy[ y - offset : y + offset + 1, x - offset : x + offset + 1 ].sum() UpperCAmelCase = (wxx * wyy) - (wxy**2) UpperCAmelCase = wxx + wyy UpperCAmelCase = det - k * (trace**2) # Can change the value if r > 0.5: corner_list.append([x, y, r] ) color_img.itemset((y, x, 0) , 0 ) color_img.itemset((y, x, 1) , 0 ) color_img.itemset((y, x, 2) , 2_5_5 ) return color_img, corner_list if __name__ == "__main__": __A : Tuple = HarrisCorner(0.04, 3) __A , __A : List[Any] = edge_detect.detect("path_to_image") cva.imwrite("detect.png", color_img)
273
0
'''simple docstring''' import pytest from datasets.utils.sharding import _distribute_shards, _number_of_shards_in_gen_kwargs, _split_gen_kwargs @pytest.mark.parametrize( "kwargs, expected" , [ ({"num_shards": 0, "max_num_jobs": 1}, []), ({"num_shards": 10, "max_num_jobs": 1}, [range(10 )]), ({"num_shards": 10, "max_num_jobs": 10}, [range(UpperCamelCase__ , i + 1 ) for i in range(10 )]), ({"num_shards": 1, "max_num_jobs": 10}, [range(1 )]), ({"num_shards": 10, "max_num_jobs": 3}, [range(0 , 4 ), range(4 , 7 ), range(7 , 10 )]), ({"num_shards": 3, "max_num_jobs": 10}, [range(0 , 1 ), range(1 , 2 ), range(2 , 3 )]), ] , ) def __lowerCAmelCase ( snake_case__ , snake_case__ ): __UpperCamelCase : List[str] = _distribute_shards(**UpperCamelCase__ ) assert out == expected @pytest.mark.parametrize( "gen_kwargs, max_num_jobs, expected" , [ ({"foo": 0}, 10, [{"foo": 0}]), ({"shards": [0, 1, 2, 3]}, 1, [{"shards": [0, 1, 2, 3]}]), ({"shards": [0, 1, 2, 3]}, 4, [{"shards": [0]}, {"shards": [1]}, {"shards": [2]}, {"shards": [3]}]), ({"shards": [0, 1]}, 4, [{"shards": [0]}, {"shards": [1]}]), ({"shards": [0, 1, 2, 3]}, 2, [{"shards": [0, 1]}, {"shards": [2, 3]}]), ] , ) def __lowerCAmelCase ( snake_case__ , snake_case__ , snake_case__ ): __UpperCamelCase : Union[str, Any] = _split_gen_kwargs(UpperCamelCase__ , UpperCamelCase__ ) assert out == expected @pytest.mark.parametrize( "gen_kwargs, expected" , [ ({"foo": 0}, 1), ({"shards": [0]}, 1), ({"shards": [0, 1, 2, 3]}, 4), ({"shards": [0, 1, 2, 3], "foo": 0}, 4), ({"shards": [0, 1, 2, 3], "other": (0, 1)}, 4), ({"shards": [0, 1, 2, 3], "shards2": [0, 1]}, RuntimeError), ] , ) def __lowerCAmelCase ( snake_case__ , snake_case__ ): if expected is RuntimeError: with pytest.raises(UpperCamelCase__ ): _number_of_shards_in_gen_kwargs(UpperCamelCase__ ) else: __UpperCamelCase : Dict = _number_of_shards_in_gen_kwargs(UpperCamelCase__ ) assert out == expected
298
from datetime import datetime import requests def __SCREAMING_SNAKE_CASE ( UpperCamelCase__ ) -> bytes: '''simple docstring''' UpperCAmelCase = '''https://downloadgram.net/wp-json/wppress/video-downloader/video?url=''' UpperCAmelCase = requests.get(base_url + url ).json()[0]['''urls'''][0]['''src'''] return requests.get(UpperCamelCase__ ).content if __name__ == "__main__": __A : Union[str, Any] = input("Enter Video/IGTV url: ").strip() __A : Tuple = F'{datetime.now():%Y-%m-%d_%H:%M:%S}.mp4' with open(file_name, "wb") as fp: fp.write(download_video(url)) print(F'Done. Video saved to disk as {file_name}.')
273
0
import torch from diffusers import UnCLIPScheduler from .test_schedulers import SchedulerCommonTest class __lowerCamelCase (a_ ): _lowercase = (UnCLIPScheduler,) def snake_case_ ( self: str,**A_: List[Any] ): '''simple docstring''' __UpperCamelCase = { 'num_train_timesteps': 1000, 'variance_type': 'fixed_small_log', 'clip_sample': True, 'clip_sample_range': 1.0, 'prediction_type': 'epsilon', } config.update(**_A ) return config def snake_case_ ( self: Optional[Any] ): '''simple docstring''' for timesteps in [1, 5, 100, 1000]: self.check_over_configs(num_train_timesteps=_A ) def snake_case_ ( self: Tuple ): '''simple docstring''' for variance in ["fixed_small_log", "learned_range"]: self.check_over_configs(variance_type=_A ) def snake_case_ ( self: List[str] ): '''simple docstring''' for clip_sample in [True, False]: self.check_over_configs(clip_sample=_A ) def snake_case_ ( self: List[str] ): '''simple docstring''' for clip_sample_range in [1, 5, 10, 20]: self.check_over_configs(clip_sample_range=_A ) def snake_case_ ( self: List[str] ): '''simple docstring''' for prediction_type in ["epsilon", "sample"]: self.check_over_configs(prediction_type=_A ) def snake_case_ ( self: List[str] ): '''simple docstring''' for time_step in [0, 500, 999]: for prev_timestep in [None, 5, 100, 250, 500, 750]: if prev_timestep is not None and prev_timestep >= time_step: continue self.check_over_forward(time_step=_A,prev_timestep=_A ) def snake_case_ ( self: List[Any] ): '''simple docstring''' __UpperCamelCase = self.scheduler_classes[0] __UpperCamelCase = self.get_scheduler_config(variance_type='fixed_small_log' ) __UpperCamelCase = scheduler_class(**_A ) assert torch.sum(torch.abs(scheduler._get_variance(0 ) - 1.0_000E-10 ) ) < 1E-5 assert torch.sum(torch.abs(scheduler._get_variance(487 ) - 0.0_5_4_9_6_2_5 ) ) < 1E-5 assert torch.sum(torch.abs(scheduler._get_variance(999 ) - 0.9_9_9_4_9_8_7 ) ) < 1E-5 def snake_case_ ( self: int ): '''simple docstring''' __UpperCamelCase = self.scheduler_classes[0] __UpperCamelCase = self.get_scheduler_config(variance_type='learned_range' ) __UpperCamelCase = scheduler_class(**_A ) __UpperCamelCase = 0.5 assert scheduler._get_variance(1,predicted_variance=_A ) - -1_0.1_7_1_2_7_9_0 < 1E-5 assert scheduler._get_variance(487,predicted_variance=_A ) - -5.7_9_9_8_0_5_2 < 1E-5 assert scheduler._get_variance(999,predicted_variance=_A ) - -0.0_0_1_0_0_1_1 < 1E-5 def snake_case_ ( self: int ): '''simple docstring''' __UpperCamelCase = self.scheduler_classes[0] __UpperCamelCase = self.get_scheduler_config() __UpperCamelCase = scheduler_class(**_A ) __UpperCamelCase = scheduler.timesteps __UpperCamelCase = self.dummy_model() __UpperCamelCase = self.dummy_sample_deter __UpperCamelCase = torch.manual_seed(0 ) for i, t in enumerate(_A ): # 1. predict noise residual __UpperCamelCase = model(_A,_A ) # 2. predict previous mean of sample x_t-1 __UpperCamelCase = scheduler.step(_A,_A,_A,generator=_A ).prev_sample __UpperCamelCase = pred_prev_sample __UpperCamelCase = torch.sum(torch.abs(_A ) ) __UpperCamelCase = torch.mean(torch.abs(_A ) ) assert abs(result_sum.item() - 252.268_2495 ) < 1E-2 assert abs(result_mean.item() - 0.3_2_8_4_7_4_3 ) < 1E-3 def snake_case_ ( self: List[Any] ): '''simple docstring''' __UpperCamelCase = self.scheduler_classes[0] __UpperCamelCase = self.get_scheduler_config() __UpperCamelCase = scheduler_class(**_A ) scheduler.set_timesteps(25 ) __UpperCamelCase = scheduler.timesteps __UpperCamelCase = self.dummy_model() __UpperCamelCase = self.dummy_sample_deter __UpperCamelCase = torch.manual_seed(0 ) for i, t in enumerate(_A ): # 1. predict noise residual __UpperCamelCase = model(_A,_A ) if i + 1 == timesteps.shape[0]: __UpperCamelCase = None else: __UpperCamelCase = timesteps[i + 1] # 2. predict previous mean of sample x_t-1 __UpperCamelCase = scheduler.step( _A,_A,_A,prev_timestep=_A,generator=_A ).prev_sample __UpperCamelCase = pred_prev_sample __UpperCamelCase = torch.sum(torch.abs(_A ) ) __UpperCamelCase = torch.mean(torch.abs(_A ) ) assert abs(result_sum.item() - 258.204_4983 ) < 1E-2 assert abs(result_mean.item() - 0.3_3_6_2_0_3_8 ) < 1E-3 def snake_case_ ( self: Any ): '''simple docstring''' pass def snake_case_ ( self: Tuple ): '''simple docstring''' pass
310
from __future__ import annotations from collections.abc import Callable def __SCREAMING_SNAKE_CASE ( UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ = 100 , ) -> float: '''simple docstring''' UpperCAmelCase = x_start UpperCAmelCase = fnc(UpperCamelCase__ ) UpperCAmelCase = 0.0 for _ in range(UpperCamelCase__ ): # Approximates small segments of curve as linear and solve # for trapezoidal area UpperCAmelCase = (x_end - x_start) / steps + xa UpperCAmelCase = fnc(UpperCamelCase__ ) area += abs(fxa + fxa ) * (xa - xa) / 2 # Increment step UpperCAmelCase = xa UpperCAmelCase = fxa return area if __name__ == "__main__": def __SCREAMING_SNAKE_CASE ( UpperCamelCase__ ) -> str: '''simple docstring''' return x**3 + x**2 print("f(x) = x^3 + x^2") print("The area between the curve, x = -5, x = 5 and the x axis is:") __A : List[Any] = 10 while i <= 100_000: print(F'with {i} steps: {trapezoidal_area(f, -5, 5, i)}') i *= 10
273
0
from typing import Dict, List, Optional, Union import numpy as np from transformers.utils import is_vision_available from transformers.utils.generic import TensorType from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size_dict from ...image_transforms import ( center_crop, get_resize_output_image_size, normalize, rescale, resize, to_channel_dimension_format, ) from ...image_utils import ( IMAGENET_STANDARD_MEAN, IMAGENET_STANDARD_STD, ChannelDimension, ImageInput, PILImageResampling, is_valid_image, to_numpy_array, valid_images, ) from ...utils import logging if is_vision_available(): import PIL lowerCamelCase = logging.get_logger(__name__) def lowerCamelCase_ ( _a ): """simple docstring""" if isinstance(UpperCamelCase__ , (list, tuple) ) and isinstance(videos[0] , (list, tuple) ) and is_valid_image(videos[0][0] ): return videos elif isinstance(UpperCamelCase__ , (list, tuple) ) and is_valid_image(videos[0] ): return [videos] elif is_valid_image(UpperCamelCase__ ): return [[videos]] raise ValueError(f'Could not make batched video from {videos}' ) class _a ( a_): _a : Optional[Any] = ['''pixel_values'''] def __init__( self : Optional[Any] , _SCREAMING_SNAKE_CASE : Dict = True , _SCREAMING_SNAKE_CASE : List[str] = None , _SCREAMING_SNAKE_CASE : Optional[int] = PILImageResampling.BILINEAR , _SCREAMING_SNAKE_CASE : Any = True , _SCREAMING_SNAKE_CASE : Union[str, Any] = None , _SCREAMING_SNAKE_CASE : int = True , _SCREAMING_SNAKE_CASE : Any = 1 / 255 , _SCREAMING_SNAKE_CASE : str = True , _SCREAMING_SNAKE_CASE : Any = True , _SCREAMING_SNAKE_CASE : List[Any] = None , _SCREAMING_SNAKE_CASE : str = None , **_SCREAMING_SNAKE_CASE : Any , )-> int: super().__init__(**_A ) lowerCAmelCase__ : str = size if size is not None else {'''shortest_edge''': 256} lowerCAmelCase__ : List[Any] = get_size_dict(_A , default_to_square=_A ) lowerCAmelCase__ : int = crop_size if crop_size is not None else {'''height''': 224, '''width''': 224} lowerCAmelCase__ : Tuple = get_size_dict(_A , param_name='''crop_size''' ) lowerCAmelCase__ : List[Any] = do_resize lowerCAmelCase__ : str = size lowerCAmelCase__ : str = do_center_crop lowerCAmelCase__ : List[Any] = crop_size lowerCAmelCase__ : Any = resample lowerCAmelCase__ : Tuple = do_rescale lowerCAmelCase__ : int = rescale_factor lowerCAmelCase__ : Optional[int] = offset lowerCAmelCase__ : Optional[Any] = do_normalize lowerCAmelCase__ : Dict = image_mean if image_mean is not None else IMAGENET_STANDARD_MEAN lowerCAmelCase__ : Optional[int] = image_std if image_std is not None else IMAGENET_STANDARD_STD def UpperCAmelCase__( self : List[Any] , _SCREAMING_SNAKE_CASE : Union[str, Any] , _SCREAMING_SNAKE_CASE : List[str] , _SCREAMING_SNAKE_CASE : Union[str, Any] = PILImageResampling.BILINEAR , _SCREAMING_SNAKE_CASE : Any = None , **_SCREAMING_SNAKE_CASE : str , )-> Dict: lowerCAmelCase__ : int = get_size_dict(_A , default_to_square=_A ) if "shortest_edge" in size: lowerCAmelCase__ : Tuple = get_resize_output_image_size(_A , size['''shortest_edge'''] , default_to_square=_A ) elif "height" in size and "width" in size: lowerCAmelCase__ : Tuple = (size['''height'''], size['''width''']) else: raise ValueError(F'Size must have \'height\' and \'width\' or \'shortest_edge\' as keys. Got {size.keys()}' ) return resize(_A , size=_A , resample=_A , data_format=_A , **_A ) def UpperCAmelCase__( self : int , _SCREAMING_SNAKE_CASE : List[str] , _SCREAMING_SNAKE_CASE : Optional[Any] , _SCREAMING_SNAKE_CASE : Tuple = None , **_SCREAMING_SNAKE_CASE : Dict , )-> List[Any]: lowerCAmelCase__ : List[str] = get_size_dict(_A ) if "height" not in size or "width" not in size: raise ValueError(F'Size must have \'height\' and \'width\' as keys. Got {size.keys()}' ) return center_crop(_A , size=(size['''height'''], size['''width''']) , data_format=_A , **_A ) def UpperCAmelCase__( self : Optional[Any] , _SCREAMING_SNAKE_CASE : List[Any] , _SCREAMING_SNAKE_CASE : str , _SCREAMING_SNAKE_CASE : Optional[Any] = True , _SCREAMING_SNAKE_CASE : Tuple = None , **_SCREAMING_SNAKE_CASE : str , )-> Union[str, Any]: lowerCAmelCase__ : List[Any] = image.astype(np.floataa ) if offset: lowerCAmelCase__ : str = image - (scale / 2) return rescale(_A , scale=_A , data_format=_A , **_A ) def UpperCAmelCase__( self : List[str] , _SCREAMING_SNAKE_CASE : List[str] , _SCREAMING_SNAKE_CASE : Optional[Any] , _SCREAMING_SNAKE_CASE : Dict , _SCREAMING_SNAKE_CASE : Any = None , **_SCREAMING_SNAKE_CASE : Dict , )-> Union[str, Any]: return normalize(_A , mean=_A , std=_A , data_format=_A , **_A ) def UpperCAmelCase__( self : Optional[Any] , _SCREAMING_SNAKE_CASE : Optional[int] , _SCREAMING_SNAKE_CASE : Optional[int] = None , _SCREAMING_SNAKE_CASE : Dict = None , _SCREAMING_SNAKE_CASE : Optional[Any] = None , _SCREAMING_SNAKE_CASE : Union[str, Any] = None , _SCREAMING_SNAKE_CASE : Union[str, Any] = None , _SCREAMING_SNAKE_CASE : Union[str, Any] = None , _SCREAMING_SNAKE_CASE : Any = None , _SCREAMING_SNAKE_CASE : List[Any] = None , _SCREAMING_SNAKE_CASE : Optional[Any] = None , _SCREAMING_SNAKE_CASE : Dict = None , _SCREAMING_SNAKE_CASE : Dict = None , _SCREAMING_SNAKE_CASE : Tuple = ChannelDimension.FIRST , )-> List[Any]: if do_resize and size is None or resample is None: raise ValueError('''Size and resample must be specified if do_resize is True.''' ) if do_center_crop and crop_size is None: raise ValueError('''Crop size must be specified if do_center_crop is True.''' ) if do_rescale and rescale_factor is None: raise ValueError('''Rescale factor must be specified if do_rescale is True.''' ) if do_normalize and (image_mean is None or image_std is None): raise ValueError('''Image mean and std must be specified if do_normalize is True.''' ) if offset and not do_rescale: raise ValueError('''For offset, do_rescale must also be set to True.''' ) # All transformations expect numpy arrays. lowerCAmelCase__ : List[str] = to_numpy_array(_A ) if do_resize: lowerCAmelCase__ : Optional[Any] = self.resize(image=_A , size=_A , resample=_A ) if do_center_crop: lowerCAmelCase__ : Any = self.center_crop(_A , size=_A ) if do_rescale: lowerCAmelCase__ : str = self.rescale(image=_A , scale=_A , offset=_A ) if do_normalize: lowerCAmelCase__ : List[Any] = self.normalize(image=_A , mean=_A , std=_A ) lowerCAmelCase__ : Dict = to_channel_dimension_format(_A , _A ) return image def UpperCAmelCase__( self : str , _SCREAMING_SNAKE_CASE : Optional[Any] , _SCREAMING_SNAKE_CASE : Optional[Any] = None , _SCREAMING_SNAKE_CASE : Union[str, Any] = None , _SCREAMING_SNAKE_CASE : int = None , _SCREAMING_SNAKE_CASE : List[Any] = None , _SCREAMING_SNAKE_CASE : List[str] = None , _SCREAMING_SNAKE_CASE : Any = None , _SCREAMING_SNAKE_CASE : Optional[int] = None , _SCREAMING_SNAKE_CASE : str = None , _SCREAMING_SNAKE_CASE : Optional[int] = None , _SCREAMING_SNAKE_CASE : List[Any] = None , _SCREAMING_SNAKE_CASE : List[str] = None , _SCREAMING_SNAKE_CASE : int = None , _SCREAMING_SNAKE_CASE : str = ChannelDimension.FIRST , **_SCREAMING_SNAKE_CASE : List[Any] , )-> List[str]: lowerCAmelCase__ : Optional[int] = do_resize if do_resize is not None else self.do_resize lowerCAmelCase__ : Any = resample if resample is not None else self.resample lowerCAmelCase__ : Any = do_center_crop if do_center_crop is not None else self.do_center_crop lowerCAmelCase__ : List[Any] = do_rescale if do_rescale is not None else self.do_rescale lowerCAmelCase__ : List[Any] = rescale_factor if rescale_factor is not None else self.rescale_factor lowerCAmelCase__ : Union[str, Any] = offset if offset is not None else self.offset lowerCAmelCase__ : str = do_normalize if do_normalize is not None else self.do_normalize lowerCAmelCase__ : List[str] = image_mean if image_mean is not None else self.image_mean lowerCAmelCase__ : Tuple = image_std if image_std is not None else self.image_std lowerCAmelCase__ : Dict = size if size is not None else self.size lowerCAmelCase__ : Dict = get_size_dict(_A , default_to_square=_A ) lowerCAmelCase__ : List[Any] = crop_size if crop_size is not None else self.crop_size lowerCAmelCase__ : Any = get_size_dict(_A , param_name='''crop_size''' ) if not valid_images(_A ): raise ValueError( '''Invalid image type. Must be of type PIL.Image.Image, numpy.ndarray, ''' '''torch.Tensor, tf.Tensor or jax.ndarray.''' ) lowerCAmelCase__ : Optional[Any] = make_batched(_A ) lowerCAmelCase__ : Optional[Any] = [ [ self._preprocess_image( image=_A , do_resize=_A , size=_A , resample=_A , do_center_crop=_A , crop_size=_A , do_rescale=_A , rescale_factor=_A , offset=_A , do_normalize=_A , image_mean=_A , image_std=_A , data_format=_A , ) for img in video ] for video in videos ] lowerCAmelCase__ : Optional[Any] = {'''pixel_values''': videos} return BatchFeature(data=_A , tensor_type=_A )
131
import json from typing import List, Optional, Tuple from tokenizers import normalizers from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import logging from .tokenization_squeezebert import SqueezeBertTokenizer __A : Dict = logging.get_logger(__name__) __A : Any = {"vocab_file": "vocab.txt", "tokenizer_file": "tokenizer.json"} __A : Tuple = { "vocab_file": { "squeezebert/squeezebert-uncased": ( "https://huggingface.co/squeezebert/squeezebert-uncased/resolve/main/vocab.txt" ), "squeezebert/squeezebert-mnli": "https://huggingface.co/squeezebert/squeezebert-mnli/resolve/main/vocab.txt", "squeezebert/squeezebert-mnli-headless": ( "https://huggingface.co/squeezebert/squeezebert-mnli-headless/resolve/main/vocab.txt" ), }, "tokenizer_file": { "squeezebert/squeezebert-uncased": ( "https://huggingface.co/squeezebert/squeezebert-uncased/resolve/main/tokenizer.json" ), "squeezebert/squeezebert-mnli": ( "https://huggingface.co/squeezebert/squeezebert-mnli/resolve/main/tokenizer.json" ), "squeezebert/squeezebert-mnli-headless": ( "https://huggingface.co/squeezebert/squeezebert-mnli-headless/resolve/main/tokenizer.json" ), }, } __A : List[Any] = { "squeezebert/squeezebert-uncased": 512, "squeezebert/squeezebert-mnli": 512, "squeezebert/squeezebert-mnli-headless": 512, } __A : List[Any] = { "squeezebert/squeezebert-uncased": {"do_lower_case": True}, "squeezebert/squeezebert-mnli": {"do_lower_case": True}, "squeezebert/squeezebert-mnli-headless": {"do_lower_case": True}, } class A_ (a_ ): UpperCAmelCase__ = VOCAB_FILES_NAMES UpperCAmelCase__ = PRETRAINED_VOCAB_FILES_MAP UpperCAmelCase__ = PRETRAINED_INIT_CONFIGURATION UpperCAmelCase__ = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES UpperCAmelCase__ = SqueezeBertTokenizer def __init__( self , _A=None , _A=None , _A=True , _A="[UNK]" , _A="[SEP]" , _A="[PAD]" , _A="[CLS]" , _A="[MASK]" , _A=True , _A=None , **_A , ): '''simple docstring''' super().__init__( _A , tokenizer_file=_A , do_lower_case=_A , unk_token=_A , sep_token=_A , pad_token=_A , cls_token=_A , mask_token=_A , tokenize_chinese_chars=_A , strip_accents=_A , **_A , ) UpperCAmelCase = json.loads(self.backend_tokenizer.normalizer.__getstate__() ) if ( normalizer_state.get('''lowercase''' , _A ) != do_lower_case or normalizer_state.get('''strip_accents''' , _A ) != strip_accents or normalizer_state.get('''handle_chinese_chars''' , _A ) != tokenize_chinese_chars ): UpperCAmelCase = getattr(_A , normalizer_state.pop('''type''' ) ) UpperCAmelCase = do_lower_case UpperCAmelCase = strip_accents UpperCAmelCase = tokenize_chinese_chars UpperCAmelCase = normalizer_class(**_A ) UpperCAmelCase = do_lower_case def _lowercase ( self , _A , _A=None ): '''simple docstring''' UpperCAmelCase = [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 _lowercase ( self , _A , _A = None ): '''simple docstring''' UpperCAmelCase = [self.sep_token_id] UpperCAmelCase = [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 _lowercase ( self , _A , _A = None ): '''simple docstring''' UpperCAmelCase = self._tokenizer.model.save(_A , name=_A ) return tuple(_A )
273
0
import argparse import logging import pickle from collections import Counter logging.basicConfig( format='''%(asctime)s - %(levelname)s - %(name)s - %(message)s''', datefmt='''%m/%d/%Y %H:%M:%S''', level=logging.INFO ) _UpperCamelCase = logging.getLogger(__name__) if __name__ == "__main__": _UpperCamelCase = argparse.ArgumentParser( description='''Token Counts for smoothing the masking probabilities in MLM (cf XLM/word2vec)''' ) parser.add_argument( '''--data_file''', type=str, default='''data/dump.bert-base-uncased.pickle''', help='''The binarized dataset.''' ) parser.add_argument( '''--token_counts_dump''', type=str, default='''data/token_counts.bert-base-uncased.pickle''', help='''The dump file.''' ) parser.add_argument('''--vocab_size''', default=3_0522, type=int) _UpperCamelCase = parser.parse_args() logger.info(F'''Loading data from {args.data_file}''') with open(args.data_file, '''rb''') as fp: _UpperCamelCase = pickle.load(fp) logger.info('''Counting occurrences for MLM.''') _UpperCamelCase = Counter() for tk_ids in data: counter.update(tk_ids) _UpperCamelCase = [0] * args.vocab_size for k, v in counter.items(): _UpperCamelCase = v logger.info(F'''Dump to {args.token_counts_dump}''') with open(args.token_counts_dump, '''wb''') as handle: pickle.dump(counts, handle, protocol=pickle.HIGHEST_PROTOCOL)
326
import argparse import re from flax.traverse_util import flatten_dict, unflatten_dict from tax import checkpoints from transformers import SwitchTransformersConfig, SwitchTransformersForConditionalGeneration from transformers.modeling_flax_pytorch_utils import load_flax_weights_in_pytorch_model from transformers.utils import logging logging.set_verbosity_info() # should not include what is already done by the `from_pt` argument __A : int = { "/attention/": "/0/SelfAttention/", "/self_attention/": "/0/SelfAttention/", "/encoder_decoder_attention/": "/1/EncDecAttention/", "value": "v", "query": "q", "key": "k", "out": "o", "pre_self_attention_layer_norm": "0/layer_norm", "pre_cross_attention_layer_norm": "1/layer_norm", "pre_attention_layer_norm": "0/layer_norm", # previously 1, but seems wrong "token_embedder": "shared", "encoder_norm": "final_layer_norm", "decoder_norm": "final_layer_norm", "relpos_bias/rel_embedding": "block/0/layer/0/SelfAttention/relative_attention_bias/weight", "router/router_weights/w/": "router/classifier/", "roer/roer_weights/w/": "router/classifier/", "logits_dense": "lm_head", } def __SCREAMING_SNAKE_CASE ( UpperCamelCase__ ) -> List[str]: '''simple docstring''' UpperCAmelCase = list(s_dict.keys() ) for key in keys: UpperCAmelCase = R'''.*/layers_(\d+)''' UpperCAmelCase = key if re.match(UpperCamelCase__ , UpperCamelCase__ ): UpperCAmelCase = re.sub(R'''layers_(\d+)''' , R'''block/\1/layer''' , UpperCamelCase__ ) UpperCAmelCase = R'''(encoder|decoder)\/''' if re.match(UpperCamelCase__ , UpperCamelCase__ ): UpperCAmelCase = re.match(UpperCamelCase__ , UpperCamelCase__ ).groups() if groups[0] == "encoder": UpperCAmelCase = re.sub(R'''/mlp/''' , R'''/1/mlp/''' , UpperCamelCase__ ) UpperCAmelCase = re.sub(R'''/pre_mlp_layer_norm/''' , R'''/1/layer_norm/''' , UpperCamelCase__ ) elif groups[0] == "decoder": UpperCAmelCase = re.sub(R'''/mlp/''' , R'''/2/mlp/''' , UpperCamelCase__ ) UpperCAmelCase = re.sub(R'''/pre_mlp_layer_norm/''' , R'''/2/layer_norm/''' , UpperCamelCase__ ) # 2. Convert other classic mappings for old_key, temp_key in MOE_LAYER_NAME_MAPPING.items(): if old_key in new_key: UpperCAmelCase = new_key.replace(UpperCamelCase__ , UpperCamelCase__ ) print(F"""{key} -> {new_key}""" ) UpperCAmelCase = s_dict.pop(UpperCamelCase__ ) if "encoder/block/0/layer/0/SelfAttention/relative_attention_bias/weight" in s_dict: UpperCAmelCase = s_dict[ '''encoder/block/0/layer/0/SelfAttention/relative_attention_bias/weight''' ].T if "decoder/block/0/layer/0/SelfAttention/relative_attention_bias/weight" in s_dict: UpperCAmelCase = s_dict[ '''decoder/block/0/layer/0/SelfAttention/relative_attention_bias/weight''' ].T # 3. Take extra care of the EXPERTS layer for key in list(s_dict.keys() ): if "expert" in key: UpperCAmelCase = s_dict[key].shape[0] UpperCAmelCase = s_dict[key] for idx in range(UpperCamelCase__ ): UpperCAmelCase = expert_weihts[idx] print(F"""{key} -> {key.replace("expert/" , "nested fstring" )}""" ) s_dict.pop(UpperCamelCase__ ) return s_dict __A : Optional[int] = { "NUM_ENCODER_LAYERS": "num_layers", "NUM_DECODER_LAYERS": "num_decoder_layers", "NUM_HEADS": "num_heads", "HEAD_DIM": "d_kv", "EMBED_DIM": "d_model", "MLP_DIM": "d_ff", "NUM_SELECTED_EXPERTS": "num_selected_experts", "NUM_ENCODER_SPARSE_LAYERS": "num_sparse_encoder_layers", "NUM_DECODER_SPARSE_LAYERS": "num_sparse_decoder_layers", "dense.MlpBlock.activations": "feed_forward_proj", } def __SCREAMING_SNAKE_CASE ( UpperCamelCase__ , UpperCamelCase__ ) -> List[str]: '''simple docstring''' import regex as re with open(UpperCamelCase__ , '''r''' ) as f: UpperCAmelCase = f.read() UpperCAmelCase = re.findall(R'''(.*) = ([0-9.]*)''' , UpperCamelCase__ ) UpperCAmelCase = {} for param, value in regex_match: if param in GIN_TO_CONFIG_MAPPING and value != "": UpperCAmelCase = float(UpperCamelCase__ ) if '''.''' in value else int(UpperCamelCase__ ) UpperCAmelCase = re.findall(R'''(.*activations) = \(\'(.*)\',\)''' , UpperCamelCase__ )[0] UpperCAmelCase = str(activation[1] ) UpperCAmelCase = num_experts UpperCAmelCase = SwitchTransformersConfig(**UpperCamelCase__ ) return config def __SCREAMING_SNAKE_CASE ( UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__=None , UpperCamelCase__="./" , UpperCamelCase__=8 ) -> List[Any]: '''simple docstring''' print(F"""Loading flax weights from : {flax_checkpoint_path}""" ) UpperCAmelCase = checkpoints.load_tax_checkpoint(UpperCamelCase__ ) if gin_file is not None: UpperCAmelCase = convert_gin_to_config(UpperCamelCase__ , UpperCamelCase__ ) else: UpperCAmelCase = SwitchTransformersConfig.from_pretrained(UpperCamelCase__ ) UpperCAmelCase = SwitchTransformersForConditionalGeneration(UpperCamelCase__ ) UpperCAmelCase = flax_params['''target'''] UpperCAmelCase = flatten_dict(UpperCamelCase__ , sep='''/''' ) UpperCAmelCase = rename_keys(UpperCamelCase__ ) UpperCAmelCase = unflatten_dict(UpperCamelCase__ , sep='''/''' ) # Load the flax params in the PT model load_flax_weights_in_pytorch_model(UpperCamelCase__ , UpperCamelCase__ ) print(F"""Save PyTorch model to {pytorch_dump_path}""" ) pt_model.save_pretrained(UpperCamelCase__ ) if __name__ == "__main__": __A : Tuple = argparse.ArgumentParser() # Required parameters parser.add_argument( "--switch_t5x_checkpoint_path", default=None, type=str, required=True, help=( "The config json file corresponding to the pre-trained SwitchTransformers model. \nThis specifies the" " model architecture. If not provided, a `gin_file` has to be provided." ), ) parser.add_argument( "--gin_file", default=None, type=str, required=False, help="Path to the gin config file. If not provided, a `config_file` has to be passed ", ) parser.add_argument( "--config_name", default=None, type=str, required=False, help="Config name of SwitchTransformers model." ) parser.add_argument( "--pytorch_dump_folder_path", default=None, type=str, required=True, help="Path to the output pytorch model." ) parser.add_argument("--num_experts", default=8, type=int, required=False, help="Number of experts") __A : Tuple = parser.parse_args() convert_flax_checkpoint_to_pytorch( args.switch_tax_checkpoint_path, args.config_name, args.gin_file, args.pytorch_dump_folder_path, args.num_experts, )
273
0
"""simple docstring""" from typing import List, Optional, Union import numpy as np import PIL.Image from ...image_processing_utils import BaseImageProcessor, BatchFeature from ...image_transforms import rescale, resize, to_channel_dimension_format from ...image_utils import ( ChannelDimension, PILImageResampling, get_image_size, make_list_of_images, to_numpy_array, valid_images, ) from ...utils import TensorType, logging __A = logging.get_logger(__name__) class snake_case ( a_ ): SCREAMING_SNAKE_CASE_ : Union[str, Any] = ["""pixel_values"""] def __init__( self : List[str] , UpperCamelCase__ : List[Any] = True , UpperCamelCase__ : Optional[Any] = 3_2 , UpperCamelCase__ : Tuple=PILImageResampling.BILINEAR , UpperCamelCase__ : int = True , **UpperCamelCase__ : str , )-> int: '''simple docstring''' __lowerCAmelCase: str = do_resize __lowerCAmelCase: int = do_rescale __lowerCAmelCase: List[Any] = size_divisor __lowerCAmelCase: Union[str, Any] = resample super().__init__(**_A) def lowercase_ ( self : str , UpperCamelCase__ : str , UpperCamelCase__ : Tuple , UpperCamelCase__ : int , UpperCamelCase__ : Any = None , **UpperCamelCase__ : str)-> Union[str, Any]: '''simple docstring''' __lowerCAmelCase , __lowerCAmelCase: List[Any] = get_image_size(_A) # Rounds the height and width down to the closest multiple of size_divisor __lowerCAmelCase: int = height // size_divisor * size_divisor __lowerCAmelCase: Any = width // size_divisor * size_divisor __lowerCAmelCase: Optional[Any] = resize(_A , (new_h, new_w) , resample=_A , data_format=_A , **_A) return image def lowercase_ ( self : Tuple , UpperCamelCase__ : List[Any] , UpperCamelCase__ : Tuple , UpperCamelCase__ : Any = None , **UpperCamelCase__ : int)-> Tuple: '''simple docstring''' return rescale(image=_A , scale=_A , data_format=_A , **_A) def lowercase_ ( self : Any , UpperCamelCase__ : Optional[Any] , UpperCamelCase__ : Optional[Any] = None , UpperCamelCase__ : Any = None , UpperCamelCase__ : Any=None , UpperCamelCase__ : List[Any] = None , UpperCamelCase__ : int = None , UpperCamelCase__ : Dict = ChannelDimension.FIRST , **UpperCamelCase__ : Optional[Any] , )-> Tuple: '''simple docstring''' __lowerCAmelCase: Dict = do_resize if do_resize is not None else self.do_resize __lowerCAmelCase: str = do_rescale if do_rescale is not None else self.do_rescale __lowerCAmelCase: Dict = size_divisor if size_divisor is not None else self.size_divisor __lowerCAmelCase: List[str] = resample if resample is not None else self.resample if do_resize and size_divisor is None: raise ValueError("size_divisor is required for resizing") __lowerCAmelCase: Optional[Any] = make_list_of_images(_A) if not valid_images(_A): raise ValueError("Invalid image(s)") # All transformations expect numpy arrays. __lowerCAmelCase: List[str] = [to_numpy_array(_A) for img in images] if do_resize: __lowerCAmelCase: Tuple = [self.resize(_A , size_divisor=_A , resample=_A) for image in images] if do_rescale: __lowerCAmelCase: Optional[int] = [self.rescale(_A , scale=1 / 2_5_5) for image in images] __lowerCAmelCase: Union[str, Any] = [to_channel_dimension_format(_A , _A) for image in images] __lowerCAmelCase: Tuple = {"pixel_values": images} return BatchFeature(data=_A , tensor_type=_A)
217
import tempfile import numpy as np import torch from transformers import AutoTokenizer, TaEncoderModel from diffusers import DDPMScheduler, UNetaDConditionModel from diffusers.models.attention_processor import AttnAddedKVProcessor from diffusers.pipelines.deepfloyd_if import IFWatermarker from diffusers.utils.testing_utils import torch_device from ..test_pipelines_common import to_np class A_ : def _lowercase ( self ): '''simple docstring''' torch.manual_seed(0 ) UpperCAmelCase = TaEncoderModel.from_pretrained('''hf-internal-testing/tiny-random-t5''' ) torch.manual_seed(0 ) UpperCAmelCase = AutoTokenizer.from_pretrained('''hf-internal-testing/tiny-random-t5''' ) torch.manual_seed(0 ) UpperCAmelCase = UNetaDConditionModel( sample_size=3_2 , layers_per_block=1 , block_out_channels=[3_2, 6_4] , down_block_types=[ '''ResnetDownsampleBlock2D''', '''SimpleCrossAttnDownBlock2D''', ] , mid_block_type='''UNetMidBlock2DSimpleCrossAttn''' , up_block_types=['''SimpleCrossAttnUpBlock2D''', '''ResnetUpsampleBlock2D'''] , in_channels=3 , out_channels=6 , cross_attention_dim=3_2 , encoder_hid_dim=3_2 , attention_head_dim=8 , addition_embed_type='''text''' , addition_embed_type_num_heads=2 , cross_attention_norm='''group_norm''' , resnet_time_scale_shift='''scale_shift''' , act_fn='''gelu''' , ) unet.set_attn_processor(AttnAddedKVProcessor() ) # For reproducibility tests torch.manual_seed(0 ) UpperCAmelCase = DDPMScheduler( num_train_timesteps=1_0_0_0 , beta_schedule='''squaredcos_cap_v2''' , beta_start=0.00_01 , beta_end=0.02 , thresholding=_A , dynamic_thresholding_ratio=0.95 , sample_max_value=1.0 , prediction_type='''epsilon''' , variance_type='''learned_range''' , ) torch.manual_seed(0 ) UpperCAmelCase = IFWatermarker() return { "text_encoder": text_encoder, "tokenizer": tokenizer, "unet": unet, "scheduler": scheduler, "watermarker": watermarker, "safety_checker": None, "feature_extractor": None, } def _lowercase ( self ): '''simple docstring''' torch.manual_seed(0 ) UpperCAmelCase = TaEncoderModel.from_pretrained('''hf-internal-testing/tiny-random-t5''' ) torch.manual_seed(0 ) UpperCAmelCase = AutoTokenizer.from_pretrained('''hf-internal-testing/tiny-random-t5''' ) torch.manual_seed(0 ) UpperCAmelCase = UNetaDConditionModel( sample_size=3_2 , layers_per_block=[1, 2] , block_out_channels=[3_2, 6_4] , down_block_types=[ '''ResnetDownsampleBlock2D''', '''SimpleCrossAttnDownBlock2D''', ] , mid_block_type='''UNetMidBlock2DSimpleCrossAttn''' , up_block_types=['''SimpleCrossAttnUpBlock2D''', '''ResnetUpsampleBlock2D'''] , in_channels=6 , out_channels=6 , cross_attention_dim=3_2 , encoder_hid_dim=3_2 , attention_head_dim=8 , addition_embed_type='''text''' , addition_embed_type_num_heads=2 , cross_attention_norm='''group_norm''' , resnet_time_scale_shift='''scale_shift''' , act_fn='''gelu''' , class_embed_type='''timestep''' , mid_block_scale_factor=1.4_14 , time_embedding_act_fn='''gelu''' , time_embedding_dim=3_2 , ) unet.set_attn_processor(AttnAddedKVProcessor() ) # For reproducibility tests torch.manual_seed(0 ) UpperCAmelCase = DDPMScheduler( num_train_timesteps=1_0_0_0 , beta_schedule='''squaredcos_cap_v2''' , beta_start=0.00_01 , beta_end=0.02 , thresholding=_A , dynamic_thresholding_ratio=0.95 , sample_max_value=1.0 , prediction_type='''epsilon''' , variance_type='''learned_range''' , ) torch.manual_seed(0 ) UpperCAmelCase = DDPMScheduler( num_train_timesteps=1_0_0_0 , beta_schedule='''squaredcos_cap_v2''' , beta_start=0.00_01 , beta_end=0.02 , ) torch.manual_seed(0 ) UpperCAmelCase = IFWatermarker() return { "text_encoder": text_encoder, "tokenizer": tokenizer, "unet": unet, "scheduler": scheduler, "image_noising_scheduler": image_noising_scheduler, "watermarker": watermarker, "safety_checker": None, "feature_extractor": None, } def _lowercase ( self ): '''simple docstring''' UpperCAmelCase = self.get_dummy_components() UpperCAmelCase = self.pipeline_class(**_A ) pipe.to(_A ) pipe.set_progress_bar_config(disable=_A ) UpperCAmelCase = self.get_dummy_inputs(_A ) UpperCAmelCase = inputs['''prompt'''] UpperCAmelCase = inputs['''generator'''] UpperCAmelCase = inputs['''num_inference_steps'''] UpperCAmelCase = inputs['''output_type'''] if "image" in inputs: UpperCAmelCase = inputs['''image'''] else: UpperCAmelCase = None if "mask_image" in inputs: UpperCAmelCase = inputs['''mask_image'''] else: UpperCAmelCase = None if "original_image" in inputs: UpperCAmelCase = inputs['''original_image'''] else: UpperCAmelCase = None UpperCAmelCase , UpperCAmelCase = pipe.encode_prompt(_A ) # inputs with prompt converted to embeddings UpperCAmelCase = { '''prompt_embeds''': prompt_embeds, '''negative_prompt_embeds''': negative_prompt_embeds, '''generator''': generator, '''num_inference_steps''': num_inference_steps, '''output_type''': output_type, } if image is not None: UpperCAmelCase = image if mask_image is not None: UpperCAmelCase = mask_image if original_image is not None: UpperCAmelCase = original_image # set all optional components to None for optional_component in pipe._optional_components: setattr(_A , _A , _A ) UpperCAmelCase = pipe(**_A )[0] with tempfile.TemporaryDirectory() as tmpdir: pipe.save_pretrained(_A ) UpperCAmelCase = self.pipeline_class.from_pretrained(_A ) pipe_loaded.to(_A ) pipe_loaded.set_progress_bar_config(disable=_A ) pipe_loaded.unet.set_attn_processor(AttnAddedKVProcessor() ) # For reproducibility tests for optional_component in pipe._optional_components: self.assertTrue( getattr(_A , _A ) is None , F"""`{optional_component}` did not stay set to None after loading.""" , ) UpperCAmelCase = self.get_dummy_inputs(_A ) UpperCAmelCase = inputs['''generator'''] UpperCAmelCase = inputs['''num_inference_steps'''] UpperCAmelCase = inputs['''output_type'''] # inputs with prompt converted to embeddings UpperCAmelCase = { '''prompt_embeds''': prompt_embeds, '''negative_prompt_embeds''': negative_prompt_embeds, '''generator''': generator, '''num_inference_steps''': num_inference_steps, '''output_type''': output_type, } if image is not None: UpperCAmelCase = image if mask_image is not None: UpperCAmelCase = mask_image if original_image is not None: UpperCAmelCase = original_image UpperCAmelCase = pipe_loaded(**_A )[0] UpperCAmelCase = np.abs(to_np(_A ) - to_np(_A ) ).max() self.assertLess(_A , 1E-4 ) def _lowercase ( self ): '''simple docstring''' UpperCAmelCase = self.get_dummy_components() UpperCAmelCase = self.pipeline_class(**_A ) pipe.to(_A ) pipe.set_progress_bar_config(disable=_A ) UpperCAmelCase = self.get_dummy_inputs(_A ) UpperCAmelCase = pipe(**_A )[0] with tempfile.TemporaryDirectory() as tmpdir: pipe.save_pretrained(_A ) UpperCAmelCase = self.pipeline_class.from_pretrained(_A ) pipe_loaded.to(_A ) pipe_loaded.set_progress_bar_config(disable=_A ) pipe_loaded.unet.set_attn_processor(AttnAddedKVProcessor() ) # For reproducibility tests UpperCAmelCase = self.get_dummy_inputs(_A ) UpperCAmelCase = pipe_loaded(**_A )[0] UpperCAmelCase = np.abs(to_np(_A ) - to_np(_A ) ).max() self.assertLess(_A , 1E-4 )
273
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 : Optional[Any] = logging.get_logger(__name__) _lowerCAmelCase : str = { "Salesforce/codegen-350M-nl": "https://huggingface.co/Salesforce/codegen-350M-nl/resolve/main/config.json", "Salesforce/codegen-350M-multi": "https://huggingface.co/Salesforce/codegen-350M-multi/resolve/main/config.json", "Salesforce/codegen-350M-mono": "https://huggingface.co/Salesforce/codegen-350M-mono/resolve/main/config.json", "Salesforce/codegen-2B-nl": "https://huggingface.co/Salesforce/codegen-2B-nl/resolve/main/config.json", "Salesforce/codegen-2B-multi": "https://huggingface.co/Salesforce/codegen-2B-multi/resolve/main/config.json", "Salesforce/codegen-2B-mono": "https://huggingface.co/Salesforce/codegen-2B-mono/resolve/main/config.json", "Salesforce/codegen-6B-nl": "https://huggingface.co/Salesforce/codegen-6B-nl/resolve/main/config.json", "Salesforce/codegen-6B-multi": "https://huggingface.co/Salesforce/codegen-6B-multi/resolve/main/config.json", "Salesforce/codegen-6B-mono": "https://huggingface.co/Salesforce/codegen-6B-mono/resolve/main/config.json", "Salesforce/codegen-16B-nl": "https://huggingface.co/Salesforce/codegen-16B-nl/resolve/main/config.json", "Salesforce/codegen-16B-multi": "https://huggingface.co/Salesforce/codegen-16B-multi/resolve/main/config.json", "Salesforce/codegen-16B-mono": "https://huggingface.co/Salesforce/codegen-16B-mono/resolve/main/config.json", } class __magic_name__ ( a_ ): """simple docstring""" __UpperCamelCase = '''codegen''' __UpperCamelCase = { '''max_position_embeddings''': '''n_positions''', '''hidden_size''': '''n_embd''', '''num_attention_heads''': '''n_head''', '''num_hidden_layers''': '''n_layer''', } def __init__( self :Union[str, Any] , snake_case :List[str]=50_400 , snake_case :Dict=2_048 , snake_case :str=2_048 , snake_case :List[Any]=4_096 , snake_case :Union[str, Any]=28 , snake_case :Any=16 , snake_case :Tuple=64 , snake_case :List[Any]=None , snake_case :List[str]="gelu_new" , snake_case :Tuple=0.0 , snake_case :int=0.0 , snake_case :Optional[int]=0.0 , snake_case :str=1e-5 , snake_case :Dict=0.02 , snake_case :Union[str, Any]=True , snake_case :Dict=50_256 , snake_case :Optional[int]=50_256 , snake_case :List[Any]=False , **snake_case :Optional[int] , ): '''simple docstring''' A_ : int = vocab_size A_ : Tuple = n_ctx A_ : Union[str, Any] = n_positions A_ : Union[str, Any] = n_embd A_ : Dict = n_layer A_ : Union[str, Any] = n_head A_ : Optional[int] = n_inner A_ : Tuple = rotary_dim A_ : List[Any] = activation_function A_ : Any = resid_pdrop A_ : Tuple = embd_pdrop A_ : Tuple = attn_pdrop A_ : Any = layer_norm_epsilon A_ : Tuple = initializer_range A_ : str = use_cache A_ : List[str] = bos_token_id A_ : List[str] = eos_token_id super().__init__( bos_token_id=_A , eos_token_id=_A , tie_word_embeddings=_A , **_A ) class __magic_name__ ( a_ ): """simple docstring""" def __init__( self :List[Any] , snake_case :Optional[Any] , snake_case :List[str] = "default" , snake_case :List[str] = None , snake_case :Tuple = False , ): '''simple docstring''' super().__init__(_A , task=_A , patching_specs=_A , use_past=_A ) if not getattr(self._config , "pad_token_id" , _A ): # TODO: how to do that better? A_ : str = 0 @property def SCREAMING_SNAKE_CASE ( self :int ): '''simple docstring''' A_ : Optional[int] = OrderedDict({"input_ids": {0: "batch", 1: "sequence"}} ) if self.use_past: self.fill_with_past_key_values_(_A , direction="inputs" ) A_ : Optional[Any] = {0: "batch", 1: "past_sequence + sequence"} else: A_ : Dict = {0: "batch", 1: "sequence"} return common_inputs @property def SCREAMING_SNAKE_CASE ( self :Dict ): '''simple docstring''' return self._config.n_layer @property def SCREAMING_SNAKE_CASE ( self :Optional[Any] ): '''simple docstring''' return self._config.n_head def SCREAMING_SNAKE_CASE ( self :List[Any] , snake_case :Tuple , snake_case :List[str] = -1 , snake_case :Dict = -1 , snake_case :Dict = False , snake_case :Dict = None , ): '''simple docstring''' A_ : str = super(_A , self ).generate_dummy_inputs( _A , batch_size=_A , seq_length=_A , is_pair=_A , framework=_A ) # We need to order the input in the way they appears in the forward() A_ : int = 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 A_ , A_ : Tuple = common_inputs["input_ids"].shape # Not using the same length for past_key_values A_ : Dict = seqlen + 2 A_ : Tuple = ( batch, self.num_attention_heads, past_key_values_length, self._config.hidden_size // self.num_attention_heads, ) A_ : Tuple = [ (torch.zeros(_A ), torch.zeros(_A )) for _ in range(self.num_layers ) ] A_ : Optional[Any] = common_inputs["attention_mask"] if self.use_past: A_ : List[Any] = ordered_inputs["attention_mask"].dtype A_ : int = torch.cat( [ordered_inputs["attention_mask"], torch.ones(_A , _A , dtype=_A )] , dim=1 ) return ordered_inputs @property def SCREAMING_SNAKE_CASE ( self :List[str] ): '''simple docstring''' return 13
300
from __future__ import annotations from collections import namedtuple def __SCREAMING_SNAKE_CASE ( UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ ) -> tuple: '''simple docstring''' UpperCAmelCase = namedtuple('''result''' , '''name value''' ) if (voltage, current, power).count(0 ) != 1: raise ValueError('''Only one argument must be 0''' ) elif power < 0: raise ValueError( '''Power cannot be negative in any electrical/electronics system''' ) elif voltage == 0: return result('''voltage''' , power / current ) elif current == 0: return result('''current''' , power / voltage ) elif power == 0: return result('''power''' , float(round(abs(voltage * current ) , 2 ) ) ) else: raise ValueError('''Exactly one argument must be 0''' ) if __name__ == "__main__": import doctest doctest.testmod()
273
0
"""simple docstring""" from __future__ import annotations import unittest from transformers import AutoTokenizer, MBartConfig, is_tf_available from transformers.testing_utils import require_sentencepiece, require_tf, require_tokenizers, slow from transformers.utils import cached_property from ...test_configuration_common import ConfigTester from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_tf_available(): import tensorflow as tf from transformers import TFAutoModelForSeqaSeqLM, TFMBartForConditionalGeneration, TFMBartModel @require_tf class __A : '''simple docstring''' lowerCAmelCase : Any = MBartConfig lowerCAmelCase : str = {} lowerCAmelCase : Tuple = "gelu" def __init__( self : Tuple ,_snake_case : Dict ,_snake_case : Dict=13 ,_snake_case : Dict=7 ,_snake_case : Union[str, Any]=True ,_snake_case : Optional[Any]=False ,_snake_case : Any=99 ,_snake_case : Any=32 ,_snake_case : Tuple=2 ,_snake_case : int=4 ,_snake_case : List[Any]=37 ,_snake_case : List[str]=0.1 ,_snake_case : List[Any]=0.1 ,_snake_case : Optional[int]=20 ,_snake_case : Union[str, Any]=2 ,_snake_case : Union[str, Any]=1 ,_snake_case : Tuple=0 ,) -> int: """simple docstring""" lowercase__ : Dict = parent lowercase__ : Dict = batch_size lowercase__ : int = seq_length lowercase__ : List[Any] = is_training lowercase__ : Union[str, Any] = use_labels lowercase__ : int = vocab_size lowercase__ : List[Any] = hidden_size lowercase__ : Optional[Any] = num_hidden_layers lowercase__ : List[Any] = num_attention_heads lowercase__ : List[Any] = intermediate_size lowercase__ : int = hidden_dropout_prob lowercase__ : int = attention_probs_dropout_prob lowercase__ : Dict = max_position_embeddings lowercase__ : Optional[Any] = eos_token_id lowercase__ : str = pad_token_id lowercase__ : Optional[Any] = bos_token_id def UpperCAmelCase ( self : Optional[Any] ) -> List[str]: """simple docstring""" lowercase__ : Optional[Any] = ids_tensor([self.batch_size, self.seq_length - 1] ,self.vocab_size ) lowercase__ : int = tf.expand_dims(tf.constant([self.eos_token_id] * self.batch_size ) ,1 ) lowercase__ : int = tf.concat([input_ids, eos_tensor] ,axis=1 ) lowercase__ : Union[str, Any] = ids_tensor([self.batch_size, self.seq_length] ,self.vocab_size ) lowercase__ : Optional[int] = self.config_cls( vocab_size=self.vocab_size ,d_model=self.hidden_size ,encoder_layers=self.num_hidden_layers ,decoder_layers=self.num_hidden_layers ,encoder_attention_heads=self.num_attention_heads ,decoder_attention_heads=self.num_attention_heads ,encoder_ffn_dim=self.intermediate_size ,decoder_ffn_dim=self.intermediate_size ,dropout=self.hidden_dropout_prob ,attention_dropout=self.attention_probs_dropout_prob ,max_position_embeddings=self.max_position_embeddings ,eos_token_ids=[2] ,bos_token_id=self.bos_token_id ,pad_token_id=self.pad_token_id ,decoder_start_token_id=self.pad_token_id ,**self.config_updates ,) lowercase__ : str = prepare_mbart_inputs_dict(_A ,_A ,_A ) return config, inputs_dict def UpperCAmelCase ( self : Any ,_snake_case : Optional[Any] ,_snake_case : Dict ) -> List[Any]: """simple docstring""" lowercase__ : Union[str, Any] = TFMBartModel(config=_A ).get_decoder() lowercase__ : Dict = inputs_dict['''input_ids'''] lowercase__ : Optional[Any] = input_ids[:1, :] lowercase__ : List[Any] = inputs_dict['''attention_mask'''][:1, :] lowercase__ : Dict = inputs_dict['''head_mask'''] lowercase__ : Union[str, Any] = 1 # first forward pass lowercase__ : Any = model(_A ,attention_mask=_A ,head_mask=_A ,use_cache=_A ) lowercase__ , lowercase__ : Any = outputs.to_tuple() lowercase__ : str = past_key_values[1] def __UpperCAmelCase ( __lowerCamelCase , __lowerCamelCase , __lowerCamelCase , __lowerCamelCase=None , __lowerCamelCase=None , __lowerCamelCase=None , __lowerCamelCase=None , __lowerCamelCase=None , ) -> List[str]: if attention_mask is None: lowercase__ : List[Any] = tf.cast(tf.math.not_equal(UpperCamelCase__ , config.pad_token_id ) , tf.inta ) if decoder_attention_mask is None: lowercase__ : Optional[int] = tf.concat( [ tf.ones(decoder_input_ids[:, :1].shape , dtype=tf.inta ), tf.cast(tf.math.not_equal(decoder_input_ids[:, 1:] , config.pad_token_id ) , tf.inta ), ] , axis=-1 , ) if head_mask is None: lowercase__ : Dict = tf.ones((config.encoder_layers, config.encoder_attention_heads) ) if decoder_head_mask is None: lowercase__ : Tuple = tf.ones((config.decoder_layers, config.decoder_attention_heads) ) if cross_attn_head_mask is None: lowercase__ : int = tf.ones((config.decoder_layers, config.decoder_attention_heads) ) return { "input_ids": input_ids, "decoder_input_ids": decoder_input_ids, "attention_mask": attention_mask, "decoder_attention_mask": decoder_attention_mask, "head_mask": head_mask, "decoder_head_mask": decoder_head_mask, "cross_attn_head_mask": cross_attn_head_mask, } @require_tf class __A ( a_ ,a_ ,unittest.TestCase ): '''simple docstring''' lowerCAmelCase : str = (TFMBartForConditionalGeneration, TFMBartModel) if is_tf_available() else () lowerCAmelCase : Optional[int] = (TFMBartForConditionalGeneration,) if is_tf_available() else () lowerCAmelCase : str = ( { "conversational": TFMBartForConditionalGeneration, "feature-extraction": TFMBartModel, "summarization": TFMBartForConditionalGeneration, "text2text-generation": TFMBartForConditionalGeneration, "translation": TFMBartForConditionalGeneration, } if is_tf_available() else {} ) lowerCAmelCase : Tuple = True lowerCAmelCase : List[Any] = False lowerCAmelCase : List[Any] = False def UpperCAmelCase ( self : Dict ,_snake_case : int ,_snake_case : int ,_snake_case : Optional[Any] ,_snake_case : Optional[Any] ,_snake_case : Dict ) -> List[str]: """simple docstring""" if pipeline_test_casse_name != "FeatureExtractionPipelineTests": # Exception encountered when calling layer '...' return True return False def UpperCAmelCase ( self : Optional[int] ) -> Optional[int]: """simple docstring""" lowercase__ : str = TFMBartModelTester(self ) lowercase__ : Dict = ConfigTester(self ,config_class=_A ) def UpperCAmelCase ( self : str ) -> int: """simple docstring""" self.config_tester.run_common_tests() def UpperCAmelCase ( self : Union[str, Any] ) -> int: """simple docstring""" lowercase__ : Optional[int] = self.model_tester.prepare_config_and_inputs_for_common() self.model_tester.check_decoder_model_past_large_inputs(*_A ) @require_sentencepiece @require_tokenizers @require_tf class __A ( unittest.TestCase ): '''simple docstring''' lowerCAmelCase : int = [ " UN Chief Says There Is No Military Solution in Syria", ] lowerCAmelCase : Optional[Any] = [ "Şeful ONU declară că nu există o soluţie militară în Siria", ] lowerCAmelCase : List[str] = "facebook/mbart-large-en-ro" @cached_property def UpperCAmelCase ( self : Optional[Any] ) -> Any: """simple docstring""" return AutoTokenizer.from_pretrained(self.model_name ) @cached_property def UpperCAmelCase ( self : Union[str, Any] ) -> int: """simple docstring""" lowercase__ : List[str] = TFAutoModelForSeqaSeqLM.from_pretrained(self.model_name ) return model def UpperCAmelCase ( self : Tuple ,**_snake_case : Union[str, Any] ) -> Any: """simple docstring""" lowercase__ : List[Any] = self.translate_src_text(**_A ) self.assertListEqual(self.expected_text ,_A ) def UpperCAmelCase ( self : Dict ,**_snake_case : Union[str, Any] ) -> Optional[Any]: """simple docstring""" lowercase__ : Dict = self.tokenizer(self.src_text ,**_A ,return_tensors='''tf''' ) lowercase__ : Union[str, Any] = self.model.generate( model_inputs.input_ids ,attention_mask=model_inputs.attention_mask ,num_beams=2 ) lowercase__ : int = self.tokenizer.batch_decode(_A ,skip_special_tokens=_A ) return generated_words @slow def UpperCAmelCase ( self : Union[str, Any] ) -> int: """simple docstring""" self._assert_generated_batch_equal_expected()
16
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_tf_available, is_tokenizers_available, is_torch_available, ) __A : Dict = { "configuration_roformer": ["ROFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP", "RoFormerConfig", "RoFormerOnnxConfig"], "tokenization_roformer": ["RoFormerTokenizer"], } try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __A : Union[str, Any] = ["RoFormerTokenizerFast"] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __A : str = [ "ROFORMER_PRETRAINED_MODEL_ARCHIVE_LIST", "RoFormerForCausalLM", "RoFormerForMaskedLM", "RoFormerForMultipleChoice", "RoFormerForQuestionAnswering", "RoFormerForSequenceClassification", "RoFormerForTokenClassification", "RoFormerLayer", "RoFormerModel", "RoFormerPreTrainedModel", "load_tf_weights_in_roformer", ] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __A : int = [ "TF_ROFORMER_PRETRAINED_MODEL_ARCHIVE_LIST", "TFRoFormerForCausalLM", "TFRoFormerForMaskedLM", "TFRoFormerForMultipleChoice", "TFRoFormerForQuestionAnswering", "TFRoFormerForSequenceClassification", "TFRoFormerForTokenClassification", "TFRoFormerLayer", "TFRoFormerModel", "TFRoFormerPreTrainedModel", ] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __A : Any = [ "FLAX_ROFORMER_PRETRAINED_MODEL_ARCHIVE_LIST", "FlaxRoFormerForMaskedLM", "FlaxRoFormerForMultipleChoice", "FlaxRoFormerForQuestionAnswering", "FlaxRoFormerForSequenceClassification", "FlaxRoFormerForTokenClassification", "FlaxRoFormerModel", "FlaxRoFormerPreTrainedModel", ] if TYPE_CHECKING: from .configuration_roformer import ROFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP, RoFormerConfig, RoFormerOnnxConfig from .tokenization_roformer import RoFormerTokenizer try: if not is_tokenizers_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_roformer_fast import RoFormerTokenizerFast try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_roformer import ( ROFORMER_PRETRAINED_MODEL_ARCHIVE_LIST, RoFormerForCausalLM, RoFormerForMaskedLM, RoFormerForMultipleChoice, RoFormerForQuestionAnswering, RoFormerForSequenceClassification, RoFormerForTokenClassification, RoFormerLayer, RoFormerModel, RoFormerPreTrainedModel, load_tf_weights_in_roformer, ) try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_roformer import ( TF_ROFORMER_PRETRAINED_MODEL_ARCHIVE_LIST, TFRoFormerForCausalLM, TFRoFormerForMaskedLM, TFRoFormerForMultipleChoice, TFRoFormerForQuestionAnswering, TFRoFormerForSequenceClassification, TFRoFormerForTokenClassification, TFRoFormerLayer, TFRoFormerModel, TFRoFormerPreTrainedModel, ) try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_flax_roformer import ( FLAX_ROFORMER_PRETRAINED_MODEL_ARCHIVE_LIST, FlaxRoFormerForMaskedLM, FlaxRoFormerForMultipleChoice, FlaxRoFormerForQuestionAnswering, FlaxRoFormerForSequenceClassification, FlaxRoFormerForTokenClassification, FlaxRoFormerModel, FlaxRoFormerPreTrainedModel, ) else: import sys __A : int = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
273
0
import os import sys import warnings from dataclasses import dataclass, field from io import BytesIO from typing import TYPE_CHECKING, Any, ClassVar, Dict, List, Optional, Union import numpy as np import pyarrow as pa from .. import config from ..download.streaming_download_manager import xopen from ..table import array_cast from ..utils.file_utils import is_local_path from ..utils.py_utils import first_non_null_value, no_op_if_value_is_null, string_to_dict if TYPE_CHECKING: import PIL.Image from .features import FeatureType UpperCAmelCase = None UpperCAmelCase = "<" if sys.byteorder == "little" else ">" # Origin: https://github.com/python-pillow/Pillow/blob/698951e19e19972aeed56df686868f1329981c12/src/PIL/Image.py#L3126 minus "|i1" which values are not preserved correctly when saving and loading an image UpperCAmelCase = [ np.dtype('''|b1'''), np.dtype('''|u1'''), np.dtype('''<u2'''), np.dtype('''>u2'''), np.dtype('''<i2'''), np.dtype('''>i2'''), np.dtype('''<u4'''), np.dtype('''>u4'''), np.dtype('''<i4'''), np.dtype('''>i4'''), np.dtype('''<f4'''), np.dtype('''>f4'''), np.dtype('''<f8'''), np.dtype('''>f8'''), ] @dataclass class A_ : '''simple docstring''' _UpperCamelCase : List[str] = True _UpperCamelCase : List[Any] = None # Automatically constructed _UpperCamelCase : Union[str, Any] = """PIL.Image.Image""" _UpperCamelCase : Dict = pa.struct({"""bytes""": pa.binary(), """path""": pa.string()} ) _UpperCamelCase : List[Any] = field(default="""Image""" , init=a_ , repr=a_ ) def __call__( self ): return self.pa_type def SCREAMING_SNAKE_CASE__ ( self , snake_case ): if config.PIL_AVAILABLE: import PIL.Image else: raise ImportError('To support encoding images, please install \'Pillow\'.' ) if isinstance(_A , _A ): lowercase = np.array(_A ) if isinstance(_A , _A ): return {"path": value, "bytes": None} elif isinstance(_A , _A ): return {"path": None, "bytes": value} elif isinstance(_A , np.ndarray ): # convert the image array to PNG/TIFF bytes return encode_np_array(_A ) elif isinstance(_A , PIL.Image.Image ): # convert the PIL image to bytes (default format is PNG/TIFF) return encode_pil_image(_A ) elif value.get('path' ) is not None and os.path.isfile(value['path'] ): # we set "bytes": None to not duplicate the data if they're already available locally return {"bytes": None, "path": value.get('path' )} elif value.get('bytes' ) is not None or value.get('path' ) is not None: # store the image bytes, and path is used to infer the image format using the file extension return {"bytes": value.get('bytes' ), "path": value.get('path' )} else: raise ValueError( F'''An image sample should have one of \'path\' or \'bytes\' but they are missing or None in {value}.''' ) def SCREAMING_SNAKE_CASE__ ( self , snake_case , snake_case=None ): if not self.decode: raise RuntimeError('Decoding is disabled for this feature. Please use Image(decode=True) instead.' ) if config.PIL_AVAILABLE: import PIL.Image else: raise ImportError('To support decoding images, please install \'Pillow\'.' ) if token_per_repo_id is None: lowercase = {} lowercase , lowercase = value['path'], value['bytes'] if bytes_ is None: if path is None: raise ValueError(F'''An image should have one of \'path\' or \'bytes\' but both are None in {value}.''' ) else: if is_local_path(_A ): lowercase = PIL.Image.open(_A ) else: lowercase = path.split('::' )[-1] try: lowercase = string_to_dict(_A , config.HUB_DATASETS_URL )['repo_id'] lowercase = token_per_repo_id.get(_A ) except ValueError: lowercase = None with xopen(_A , 'rb' , use_auth_token=_A ) as f: lowercase = BytesIO(f.read() ) lowercase = PIL.Image.open(bytes_ ) else: lowercase = PIL.Image.open(BytesIO(bytes_ ) ) image.load() # to avoid "Too many open files" errors return image def SCREAMING_SNAKE_CASE__ ( self ): from .features import Value return ( self if self.decode else { "bytes": Value('binary' ), "path": Value('string' ), } ) def SCREAMING_SNAKE_CASE__ ( self , snake_case ): if pa.types.is_string(storage.type ): lowercase = pa.array([None] * len(_A ) , type=pa.binary() ) lowercase = pa.StructArray.from_arrays([bytes_array, storage] , ['bytes', 'path'] , mask=storage.is_null() ) elif pa.types.is_binary(storage.type ): lowercase = pa.array([None] * len(_A ) , type=pa.string() ) lowercase = pa.StructArray.from_arrays([storage, path_array] , ['bytes', 'path'] , mask=storage.is_null() ) elif pa.types.is_struct(storage.type ): if storage.type.get_field_index('bytes' ) >= 0: lowercase = storage.field('bytes' ) else: lowercase = pa.array([None] * len(_A ) , type=pa.binary() ) if storage.type.get_field_index('path' ) >= 0: lowercase = storage.field('path' ) else: lowercase = pa.array([None] * len(_A ) , type=pa.string() ) lowercase = pa.StructArray.from_arrays([bytes_array, path_array] , ['bytes', 'path'] , mask=storage.is_null() ) elif pa.types.is_list(storage.type ): lowercase = pa.array( [encode_np_array(np.array(_A ) )['bytes'] if arr is not None else None for arr in storage.to_pylist()] , type=pa.binary() , ) lowercase = pa.array([None] * len(_A ) , type=pa.string() ) lowercase = pa.StructArray.from_arrays( [bytes_array, path_array] , ['bytes', 'path'] , mask=bytes_array.is_null() ) return array_cast(_A , self.pa_type ) def SCREAMING_SNAKE_CASE__ ( self , snake_case ): @no_op_if_value_is_null def path_to_bytes(snake_case ): with xopen(_A , 'rb' ) as f: lowercase = f.read() return bytes_ lowercase = pa.array( [ (path_to_bytes(x['path'] ) if x['bytes'] is None else x['bytes']) if x is not None else None for x in storage.to_pylist() ] , type=pa.binary() , ) lowercase = pa.array( [os.path.basename(_A ) if path is not None else None for path in storage.field('path' ).to_pylist()] , type=pa.string() , ) lowercase = pa.StructArray.from_arrays([bytes_array, path_array] , ['bytes', 'path'] , mask=bytes_array.is_null() ) return array_cast(_A , self.pa_type ) def UpperCAmelCase_ ( ): if config.PIL_AVAILABLE: import PIL.Image else: raise ImportError('To support encoding images, please install \'Pillow\'.' ) global _IMAGE_COMPRESSION_FORMATS if _IMAGE_COMPRESSION_FORMATS is None: PIL.Image.init() lowercase = list(set(PIL.Image.OPEN.keys() ) & set(PIL.Image.SAVE.keys() ) ) return _IMAGE_COMPRESSION_FORMATS def UpperCAmelCase_ ( __SCREAMING_SNAKE_CASE ): lowercase = BytesIO() if image.format in list_image_compression_formats(): lowercase = image.format else: lowercase = 'PNG' if image.mode in ['1', 'L', 'LA', 'RGB', 'RGBA'] else 'TIFF' image.save(UpperCamelCase__ , format=UpperCamelCase__ ) return buffer.getvalue() def UpperCAmelCase_ ( __SCREAMING_SNAKE_CASE ): if hasattr(UpperCamelCase__ , 'filename' ) and image.filename != "": return {"path": image.filename, "bytes": None} else: return {"path": None, "bytes": image_to_bytes(UpperCamelCase__ )} def UpperCAmelCase_ ( __SCREAMING_SNAKE_CASE ): if config.PIL_AVAILABLE: import PIL.Image else: raise ImportError('To support encoding images, please install \'Pillow\'.' ) lowercase = array.dtype lowercase = dtype.byteorder if dtype.byteorder != '=' else _NATIVE_BYTEORDER lowercase = dtype.kind lowercase = dtype.itemsize lowercase = None # Multi-channel array case (only np.dtype("|u1") is allowed) if array.shape[2:]: lowercase = np.dtype('|u1' ) if dtype_kind not in ["u", "i"]: raise TypeError( F'''Unsupported array dtype {dtype} for image encoding. Only {dest_dtype} is supported for multi-channel arrays.''' ) if dtype is not dest_dtype: warnings.warn(F'''Downcasting array dtype {dtype} to {dest_dtype} to be compatible with \'Pillow\'''' ) # Exact match elif dtype in _VALID_IMAGE_ARRAY_DTPYES: lowercase = dtype else: # Downcast the type within the kind (np.can_cast(from_type, to_type, casting="same_kind") doesn't behave as expected, so do it manually) while dtype_itemsize >= 1: lowercase = dtype_byteorder + dtype_kind + str(UpperCamelCase__ ) lowercase = np.dtype(UpperCamelCase__ ) if dest_dtype in _VALID_IMAGE_ARRAY_DTPYES: warnings.warn(F'''Downcasting array dtype {dtype} to {dest_dtype} to be compatible with \'Pillow\'''' ) break else: dtype_itemsize //= 2 if dest_dtype is None: raise TypeError( F'''Cannot convert dtype {dtype} to a valid image dtype. Valid image dtypes: {_VALID_IMAGE_ARRAY_DTPYES}''' ) lowercase = PIL.Image.fromarray(array.astype(UpperCamelCase__ ) ) return {"path": None, "bytes": image_to_bytes(UpperCamelCase__ )} def UpperCAmelCase_ ( __SCREAMING_SNAKE_CASE ): if config.PIL_AVAILABLE: import PIL.Image else: raise ImportError('To support encoding images, please install \'Pillow\'.' ) if objs: lowercase , lowercase = first_non_null_value(UpperCamelCase__ ) if isinstance(UpperCamelCase__ , UpperCamelCase__ ): return [{"path": obj, "bytes": None} if obj is not None else None for obj in objs] if isinstance(UpperCamelCase__ , np.ndarray ): lowercase = no_op_if_value_is_null(UpperCamelCase__ ) return [obj_to_image_dict_func(UpperCamelCase__ ) for obj in objs] elif isinstance(UpperCamelCase__ , PIL.Image.Image ): lowercase = no_op_if_value_is_null(UpperCamelCase__ ) return [obj_to_image_dict_func(UpperCamelCase__ ) for obj in objs] else: return objs else: return objs
195
import argparse import torch from transformers import YosoConfig, YosoForMaskedLM def __SCREAMING_SNAKE_CASE ( UpperCamelCase__ ) -> Union[str, Any]: '''simple docstring''' if "model" in orig_key: UpperCAmelCase = orig_key.replace('''model.''' , '''''' ) if "norm1" in orig_key: UpperCAmelCase = orig_key.replace('''norm1''' , '''attention.output.LayerNorm''' ) if "norm2" in orig_key: UpperCAmelCase = orig_key.replace('''norm2''' , '''output.LayerNorm''' ) if "norm" in orig_key: UpperCAmelCase = orig_key.replace('''norm''' , '''LayerNorm''' ) if "transformer" in orig_key: UpperCAmelCase = orig_key.split('''.''' )[0].split('''_''' )[-1] UpperCAmelCase = orig_key.replace(F"""transformer_{layer_num}""" , F"""encoder.layer.{layer_num}""" ) if "mha.attn" in orig_key: UpperCAmelCase = orig_key.replace('''mha.attn''' , '''attention.self''' ) if "mha" in orig_key: UpperCAmelCase = orig_key.replace('''mha''' , '''attention''' ) if "W_q" in orig_key: UpperCAmelCase = orig_key.replace('''W_q''' , '''self.query''' ) if "W_k" in orig_key: UpperCAmelCase = orig_key.replace('''W_k''' , '''self.key''' ) if "W_v" in orig_key: UpperCAmelCase = orig_key.replace('''W_v''' , '''self.value''' ) if "ff1" in orig_key: UpperCAmelCase = orig_key.replace('''ff1''' , '''intermediate.dense''' ) if "ff2" in orig_key: UpperCAmelCase = orig_key.replace('''ff2''' , '''output.dense''' ) if "ff" in orig_key: UpperCAmelCase = orig_key.replace('''ff''' , '''output.dense''' ) if "mlm_class" in orig_key: UpperCAmelCase = orig_key.replace('''mlm.mlm_class''' , '''cls.predictions.decoder''' ) if "mlm" in orig_key: UpperCAmelCase = orig_key.replace('''mlm''' , '''cls.predictions.transform''' ) if "cls" not in orig_key: UpperCAmelCase = '''yoso.''' + orig_key return orig_key def __SCREAMING_SNAKE_CASE ( UpperCamelCase__ , UpperCamelCase__ ) -> Dict: '''simple docstring''' for key in orig_state_dict.copy().keys(): UpperCAmelCase = orig_state_dict.pop(UpperCamelCase__ ) if ("pooler" in key) or ("sen_class" in key): continue else: UpperCAmelCase = val UpperCAmelCase = orig_state_dict['''cls.predictions.decoder.bias'''] UpperCAmelCase = torch.arange(UpperCamelCase__ ).expand((1, -1) ) + 2 return orig_state_dict def __SCREAMING_SNAKE_CASE ( UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ ) -> int: '''simple docstring''' UpperCAmelCase = torch.load(UpperCamelCase__ , map_location='''cpu''' )['''model_state_dict'''] UpperCAmelCase = YosoConfig.from_json_file(UpperCamelCase__ ) UpperCAmelCase = YosoForMaskedLM(UpperCamelCase__ ) UpperCAmelCase = convert_checkpoint_helper(config.max_position_embeddings , UpperCamelCase__ ) print(model.load_state_dict(UpperCamelCase__ ) ) model.eval() model.save_pretrained(UpperCamelCase__ ) print(F"""Checkpoint successfuly converted. Model saved at {pytorch_dump_path}""" ) if __name__ == "__main__": __A : Any = argparse.ArgumentParser() # Required parameters parser.add_argument( "--pytorch_model_path", default=None, type=str, required=True, help="Path to YOSO pytorch checkpoint." ) parser.add_argument( "--config_file", default=None, type=str, required=True, help="The json file for YOSO model config.", ) parser.add_argument( "--pytorch_dump_path", default=None, type=str, required=True, help="Path to the output PyTorch model." ) __A : List[str] = parser.parse_args() convert_yoso_checkpoint(args.pytorch_model_path, args.config_file, args.pytorch_dump_path)
273
0
'''simple docstring''' import tempfile import unittest from transformers import TaConfig, is_torch_available from transformers.testing_utils import ( require_sentencepiece, require_tokenizers, require_torch, slow, torch_device, ) from ...generation.test_utils import GenerationTesterMixin from ...test_modeling_common import ModelTesterMixin, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from transformers import AutoTokenizer, UMTaForConditionalGeneration, UMTaForQuestionAnswering, UMTaModel class __lowerCAmelCase : """simple docstring""" def __init__( self : int , lowerCAmelCase__ : str , lowerCAmelCase__ : Union[str, Any]=99 , lowerCAmelCase__ : Optional[int]=13 , lowerCAmelCase__ : List[Any]=7 , lowerCAmelCase__ : Optional[Any]=9 , lowerCAmelCase__ : List[Any]=True , lowerCAmelCase__ : Optional[Any]=True , lowerCAmelCase__ : Dict=False , lowerCAmelCase__ : Any=32 , lowerCAmelCase__ : List[str]=5 , lowerCAmelCase__ : Union[str, Any]=4 , lowerCAmelCase__ : Any=37 , lowerCAmelCase__ : Union[str, Any]=8 , lowerCAmelCase__ : str=0.1 , lowerCAmelCase__ : List[Any]=0.002 , lowerCAmelCase__ : Tuple=1 , lowerCAmelCase__ : List[Any]=0 , lowerCAmelCase__ : str=0 , lowerCAmelCase__ : Optional[int]=None , lowerCAmelCase__ : List[Any]=None , ) -> Optional[Any]: '''simple docstring''' _UpperCamelCase = parent _UpperCamelCase = batch_size _UpperCamelCase = encoder_seq_length _UpperCamelCase = decoder_seq_length # For common tests _UpperCamelCase = self.decoder_seq_length _UpperCamelCase = is_training _UpperCamelCase = use_attention_mask _UpperCamelCase = use_labels _UpperCamelCase = vocab_size _UpperCamelCase = hidden_size _UpperCamelCase = num_hidden_layers _UpperCamelCase = num_attention_heads _UpperCamelCase = d_ff _UpperCamelCase = relative_attention_num_buckets _UpperCamelCase = dropout_rate _UpperCamelCase = initializer_factor _UpperCamelCase = eos_token_id _UpperCamelCase = pad_token_id _UpperCamelCase = decoder_start_token_id _UpperCamelCase = None _UpperCamelCase = decoder_layers def snake_case__ ( self : Optional[Any] ) -> int: '''simple docstring''' return TaConfig.from_pretrained('''google/umt5-base''' ) def snake_case__ ( self : Dict , lowerCAmelCase__ : Union[str, Any] , lowerCAmelCase__ : List[Any] , lowerCAmelCase__ : Dict , lowerCAmelCase__ : Optional[Any]=None , lowerCAmelCase__ : Dict=None , lowerCAmelCase__ : Dict=None , lowerCAmelCase__ : Union[str, Any]=None , lowerCAmelCase__ : Tuple=None , ) -> Union[str, Any]: '''simple docstring''' if attention_mask is None: _UpperCamelCase = input_ids.ne(config.pad_token_id ) if decoder_attention_mask is None: _UpperCamelCase = decoder_input_ids.ne(config.pad_token_id ) if head_mask is None: _UpperCamelCase = torch.ones(config.num_hidden_layers , config.num_attention_heads , device=_A ) if decoder_head_mask is None: _UpperCamelCase = torch.ones(config.num_decoder_layers , config.num_attention_heads , device=_A ) if cross_attn_head_mask is None: _UpperCamelCase = torch.ones( config.num_decoder_layers , config.num_attention_heads , device=_A ) return { "input_ids": input_ids, "decoder_input_ids": decoder_input_ids, "attention_mask": attention_mask, "decoder_attention_mask": decoder_attention_mask, "head_mask": head_mask, "decoder_head_mask": decoder_head_mask, "cross_attn_head_mask": cross_attn_head_mask, } def snake_case__ ( self : Optional[int] ) -> str: '''simple docstring''' _UpperCamelCase = ids_tensor([self.batch_size, self.encoder_seq_length] , self.vocab_size ) _UpperCamelCase = ids_tensor([self.batch_size, self.decoder_seq_length] , self.vocab_size ) # we need to clamp the input ids here to avoid having pad token in between # this is because for NllbMoe the position_ids are prepared such that # all pad tokens have pos id = 2 and rest are between 2..seq_length # and the seq_length here is seq_length - num_pad_tokens # but when using past, there is no way of knowing if the past input ids had # pad tokens in them, which results in incorrect seq_lenth and which in turn results in # position_ids being off by num_pad_tokens in past input _UpperCamelCase = input_ids.clamp(self.pad_token_id + 1 ) _UpperCamelCase = decoder_input_ids.clamp(self.pad_token_id + 1 ) _UpperCamelCase = self.get_config() _UpperCamelCase = config.num_attention_heads _UpperCamelCase = self.prepare_inputs_dict(_A , _A , _A ) return config, input_dict def snake_case__ ( self : str ) -> Dict: '''simple docstring''' _UpperCamelCase , _UpperCamelCase = self.prepare_config_and_inputs() return config, inputs_dict def snake_case__ ( self : List[str] ) -> int: '''simple docstring''' return TaConfig( vocab_size=166 , d_model=self.hidden_size , d_ff=self.d_ff , d_kv=self.hidden_size // self.num_attention_heads , num_layers=self.num_hidden_layers , num_decoder_layers=self.decoder_layers , num_heads=self.num_attention_heads , relative_attention_num_buckets=self.relative_attention_num_buckets , dropout_rate=self.dropout_rate , initializer_factor=self.initializer_factor , eos_token_id=self.eos_token_id , bos_token_id=self.pad_token_id , pad_token_id=self.pad_token_id , decoder_start_token_id=self.decoder_start_token_id , ) def snake_case__ ( self : Union[str, Any] ) -> Tuple: '''simple docstring''' return TaConfig( vocab_size=self.vocab_size , d_model=self.hidden_size , d_ff=self.d_ff , d_kv=self.hidden_size // self.num_attention_heads , num_layers=self.num_hidden_layers , num_decoder_layers=self.decoder_layers , num_heads=self.num_attention_heads , relative_attention_num_buckets=self.relative_attention_num_buckets , dropout_rate=self.dropout_rate , initializer_factor=self.initializer_factor , eos_token_id=self.eos_token_id , bos_token_id=self.pad_token_id , pad_token_id=self.pad_token_id , decoder_start_token_id=self.decoder_start_token_id , ) def snake_case__ ( self : List[Any] , lowerCAmelCase__ : Any , lowerCAmelCase__ : Union[str, Any] , lowerCAmelCase__ : List[str] , lowerCAmelCase__ : List[str] , lowerCAmelCase__ : Tuple , lowerCAmelCase__ : List[Any] , ) -> int: '''simple docstring''' _UpperCamelCase = UMTaModel(config=_A ) model.to(_A ) model.eval() _UpperCamelCase = model( input_ids=_A , decoder_input_ids=_A , attention_mask=_A , decoder_attention_mask=_A , ) _UpperCamelCase = model(input_ids=_A , decoder_input_ids=_A ) _UpperCamelCase = result.last_hidden_state _UpperCamelCase = result.past_key_values _UpperCamelCase = result.encoder_last_hidden_state self.parent.assertEqual(encoder_output.size() , (self.batch_size, self.encoder_seq_length, self.hidden_size) ) self.parent.assertEqual(decoder_output.size() , (self.batch_size, self.decoder_seq_length, self.hidden_size) ) # There should be `num_layers` key value embeddings stored in decoder_past self.parent.assertEqual(len(_A ) , config.num_layers ) # There should be a self attn key, a self attn value, a cross attn key and a cross attn value stored in each decoder_past tuple self.parent.assertEqual(len(decoder_past[0] ) , 4 ) def snake_case__ ( self : List[Any] , lowerCAmelCase__ : Any , lowerCAmelCase__ : Optional[int] , lowerCAmelCase__ : List[str] , lowerCAmelCase__ : Union[str, Any] , lowerCAmelCase__ : Dict , lowerCAmelCase__ : Union[str, Any] , ) -> str: '''simple docstring''' _UpperCamelCase = UMTaModel(config=_A ).get_decoder().to(_A ).eval() # first forward pass _UpperCamelCase = model(_A , use_cache=_A ) _UpperCamelCase = model(_A ) _UpperCamelCase = model(_A , use_cache=_A ) self.parent.assertTrue(len(_A ) == len(_A ) ) self.parent.assertTrue(len(_A ) == len(_A ) + 1 ) _UpperCamelCase , _UpperCamelCase = outputs.to_tuple() # create hypothetical next token and extent to next_input_ids _UpperCamelCase = ids_tensor((self.batch_size, 1) , config.vocab_size ) # append to next input_ids and _UpperCamelCase = torch.cat([input_ids, next_tokens] , dim=-1 ) _UpperCamelCase = model(_A )['''last_hidden_state'''] _UpperCamelCase = model(_A , past_key_values=_A )['''last_hidden_state'''] # select random slice _UpperCamelCase = ids_tensor((1,) , output_from_past.shape[-1] ).item() _UpperCamelCase = output_from_no_past[:, -1, random_slice_idx].detach() _UpperCamelCase = output_from_past[:, 0, random_slice_idx].detach() # test that outputs are equal for slice self.parent.assertTrue(torch.allclose(_A , _A , atol=1e-3 ) ) def snake_case__ ( self : Union[str, Any] , lowerCAmelCase__ : str , lowerCAmelCase__ : List[str] , ) -> List[str]: '''simple docstring''' _UpperCamelCase = UMTaModel(config=_A ).to(_A ).half().eval() _UpperCamelCase = model(**_A )['''last_hidden_state'''] self.parent.assertFalse(torch.isnan(_A ).any().item() ) @require_torch class __lowerCAmelCase ( a_ , a_ , a_ , unittest.TestCase ): """simple docstring""" _snake_case : Dict = ( (UMTaModel, UMTaForConditionalGeneration, UMTaForQuestionAnswering) if is_torch_available() else () ) _snake_case : Optional[Any] = (UMTaForConditionalGeneration,) if is_torch_available() else () _snake_case : int = ( { 'conversational': UMTaForConditionalGeneration, 'feature-extraction': UMTaModel, 'summarization': UMTaForConditionalGeneration, 'text2text-generation': UMTaForConditionalGeneration, 'translation': UMTaForConditionalGeneration, 'question-answering': UMTaForQuestionAnswering, } if is_torch_available() else {} ) _snake_case : List[Any] = True _snake_case : Optional[Any] = False _snake_case : List[str] = False _snake_case : Any = True _snake_case : Tuple = True # The small UMT5 model needs higher percentages for CPU/MP tests _snake_case : str = [0.8, 0.9] def snake_case__ ( self : int ) -> Any: '''simple docstring''' _UpperCamelCase = UMTaModelTester(self ) @unittest.skip('''Test has a segmentation fault on torch 1.8.0''' ) def snake_case__ ( self : str ) -> Tuple: '''simple docstring''' _UpperCamelCase = self.model_tester.prepare_config_and_inputs() _UpperCamelCase = UMTaModel(config_and_inputs[0] ).to(_A ) with tempfile.TemporaryDirectory() as tmpdirname: torch.onnx.export( _A , (config_and_inputs[1], config_and_inputs[3], config_and_inputs[2]) , f"""{tmpdirname}/t5_test.onnx""" , export_params=_A , opset_version=9 , input_names=['''input_ids''', '''decoder_input_ids'''] , ) @unittest.skipIf(torch_device == '''cpu''' , '''Cant do half precision''' ) def snake_case__ ( self : List[Any] ) -> str: '''simple docstring''' _UpperCamelCase = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model_fpaa_forward(*_A ) def snake_case__ ( self : int ) -> Dict: '''simple docstring''' _UpperCamelCase = ['''encoder_attentions''', '''decoder_attentions''', '''cross_attentions'''] _UpperCamelCase = self.model_tester.prepare_config_and_inputs() _UpperCamelCase = config_and_inputs[0] _UpperCamelCase = UMTaForConditionalGeneration(_A ).eval() model.to(_A ) _UpperCamelCase = { '''head_mask''': torch.zeros(config.num_layers , config.num_heads , device=_A ), '''decoder_head_mask''': torch.zeros(config.num_decoder_layers , config.num_heads , device=_A ), '''cross_attn_head_mask''': torch.zeros(config.num_decoder_layers , config.num_heads , device=_A ), } for attn_name, (name, mask) in zip(_A , head_masking.items() ): _UpperCamelCase = {name: mask} # Explicitly pass decoder_head_mask as it is required from T5 model when head_mask specified if name == "head_mask": _UpperCamelCase = torch.ones( config.num_decoder_layers , config.num_heads , device=_A ) _UpperCamelCase = model.generate( config_and_inputs[1]['''input_ids'''] , num_beams=1 , max_length=3 , output_attentions=_A , return_dict_in_generate=_A , **_A , ) # We check the state of decoder_attentions and cross_attentions just from the last step _UpperCamelCase = out[attn_name] if attn_name == attention_names[0] else out[attn_name][-1] self.assertEqual(sum([w.sum().item() for w in attn_weights] ) , 0.0 ) @unittest.skip('''Does not work on the tiny model as we keep hitting edge cases.''' ) def snake_case__ ( self : int ) -> Tuple: '''simple docstring''' pass @require_torch @require_sentencepiece @require_tokenizers class __lowerCAmelCase ( unittest.TestCase ): """simple docstring""" @slow @unittest.skip( '''Unless we stop stripping left and right by default for all special tokens, the expected ids obtained here will not match the original ones. Wait for https://github.com/huggingface/transformers/pull/23909 to be merged''' ) def snake_case__ ( self : Tuple ) -> Dict: '''simple docstring''' _UpperCamelCase = UMTaForConditionalGeneration.from_pretrained('''google/umt5-small''' , return_dict=_A ).to(_A ) _UpperCamelCase = AutoTokenizer.from_pretrained('''google/umt5-small''' , use_fast=_A , legacy=_A ) _UpperCamelCase = [ '''Bonjour monsieur <extra_id_0> bien <extra_id_1>.''', '''No se como puedo <extra_id_0>.''', '''This is the reason why we <extra_id_0> them.''', '''The <extra_id_0> walks in <extra_id_1>, seats''', '''A <extra_id_0> walks into a bar and orders a <extra_id_1> with <extra_id_2> pinch of <extra_id_3>.''', ] _UpperCamelCase = tokenizer(_A , return_tensors='''pt''' , padding=_A ).input_ids # fmt: off _UpperCamelCase = torch.tensor( [ [ 38530, 210703, 256299, 1410, 256298, 274, 1, 0,0, 0, 0, 0, 0, 0, 0, 0,0, 0], [ 826, 321, 671, 25922, 256299, 274, 1, 0,0, 0, 0, 0, 0, 0, 0, 0,0, 0], [ 1460, 339, 312, 19014, 10620, 758, 256299, 2355,274, 1, 0, 0, 0, 0, 0, 0,0, 0], [ 517, 256299, 14869, 281, 301, 256298, 275, 119983,1, 0, 0, 0, 0, 0, 0, 0,0, 0], [ 320, 256299, 14869, 281, 2234, 289, 2275, 333,61391, 289, 256298, 543, 256297, 168714, 329, 256296,274, 1], ] ) # fmt: on torch.testing.assert_allclose(_A , _A ) _UpperCamelCase = model.generate(input_ids.to(_A ) ) _UpperCamelCase = [ '''<pad><extra_id_0> et<extra_id_1> [eod] <extra_id_2><extra_id_55>.. [eod] 💐 💐 💐 💐 💐 💐 💐 💐 💐 💐 💐 <extra_id_56>ajšietosto<extra_id_56>lleux<extra_id_19><extra_id_6>ajšie</s>''', '''<pad><extra_id_0>.<extra_id_1>.,<0x0A>...spech <0x0A><extra_id_20> <extra_id_21></s><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad>''', '''<pad><extra_id_0> are not going to be a part of the world. We are not going to be a part of<extra_id_1> and<extra_id_2><0x0A><extra_id_48>.<extra_id_48></s><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad>''', '''<pad><extra_id_0> door<extra_id_1>, the door<extra_id_2> 피해[/</s><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad>''', '''<pad><extra_id_0>nyone who<extra_id_1> drink<extra_id_2> a<extra_id_3> alcohol<extra_id_4> A<extra_id_5> A. This<extra_id_6> I<extra_id_7><extra_id_52><extra_id_53></s><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad><pad>''', ] _UpperCamelCase = tokenizer.batch_decode(_A ) self.assertEqual(_A , _A )
324
def __SCREAMING_SNAKE_CASE ( UpperCamelCase__ , UpperCamelCase__ , UpperCamelCase__ ) -> int: '''simple docstring''' if exponent == 1: return base if exponent % 2 == 0: UpperCAmelCase = _modexpt(UpperCamelCase__ , exponent // 2 , UpperCamelCase__ ) % modulo_value return (x * x) % modulo_value else: return (base * _modexpt(UpperCamelCase__ , exponent - 1 , UpperCamelCase__ )) % modulo_value def __SCREAMING_SNAKE_CASE ( UpperCamelCase__ = 1777 , UpperCamelCase__ = 1855 , UpperCamelCase__ = 8 ) -> int: '''simple docstring''' UpperCAmelCase = base for _ in range(1 , UpperCamelCase__ ): UpperCAmelCase = _modexpt(UpperCamelCase__ , UpperCamelCase__ , 10**digits ) return result if __name__ == "__main__": print(F'{solution() = }')
273
0
from dataclasses import dataclass, field from typing import ClassVar, Dict from ..features import Features, Value from .base import TaskTemplate @dataclass(frozen=a_ ) class __A( a_ ): """simple docstring""" SCREAMING_SNAKE_CASE__ = field(default="""language-modeling""" , metadata={"""include_in_asdict_even_if_is_default""": True} ) SCREAMING_SNAKE_CASE__ = Features({"""text""": Value("""string""" )} ) SCREAMING_SNAKE_CASE__ = Features({} ) SCREAMING_SNAKE_CASE__ = """text""" @property def UpperCAmelCase_ (self ): return {self.text_column: "text"}
244
from collections import OrderedDict from typing import TYPE_CHECKING, Any, List, Mapping, Optional, Union from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import TensorType, logging if TYPE_CHECKING: from ...onnx.config import PatchingSpec from ...tokenization_utils_base import PreTrainedTokenizerBase __A : Dict = logging.get_logger(__name__) __A : str = { "allenai/longformer-base-4096": "https://huggingface.co/allenai/longformer-base-4096/resolve/main/config.json", "allenai/longformer-large-4096": "https://huggingface.co/allenai/longformer-large-4096/resolve/main/config.json", "allenai/longformer-large-4096-finetuned-triviaqa": ( "https://huggingface.co/allenai/longformer-large-4096-finetuned-triviaqa/resolve/main/config.json" ), "allenai/longformer-base-4096-extra.pos.embd.only": ( "https://huggingface.co/allenai/longformer-base-4096-extra.pos.embd.only/resolve/main/config.json" ), "allenai/longformer-large-4096-extra.pos.embd.only": ( "https://huggingface.co/allenai/longformer-large-4096-extra.pos.embd.only/resolve/main/config.json" ), } class A_ (a_ ): UpperCAmelCase__ = '''longformer''' def __init__( self , _A = 5_1_2 , _A = 2 , _A = 1 , _A = 0 , _A = 2 , _A = 3_0_5_2_2 , _A = 7_6_8 , _A = 1_2 , _A = 1_2 , _A = 3_0_7_2 , _A = "gelu" , _A = 0.1 , _A = 0.1 , _A = 5_1_2 , _A = 2 , _A = 0.02 , _A = 1E-12 , _A = False , **_A , ): '''simple docstring''' super().__init__(pad_token_id=_A , **_A ) UpperCAmelCase = attention_window UpperCAmelCase = sep_token_id UpperCAmelCase = bos_token_id UpperCAmelCase = eos_token_id UpperCAmelCase = vocab_size UpperCAmelCase = hidden_size UpperCAmelCase = num_hidden_layers UpperCAmelCase = num_attention_heads UpperCAmelCase = hidden_act UpperCAmelCase = intermediate_size UpperCAmelCase = hidden_dropout_prob UpperCAmelCase = attention_probs_dropout_prob UpperCAmelCase = max_position_embeddings UpperCAmelCase = type_vocab_size UpperCAmelCase = initializer_range UpperCAmelCase = layer_norm_eps UpperCAmelCase = onnx_export class A_ (a_ ): def __init__( self , _A , _A = "default" , _A = None ): '''simple docstring''' super().__init__(_A , _A , _A ) UpperCAmelCase = True @property def _lowercase ( self ): '''simple docstring''' if self.task == "multiple-choice": UpperCAmelCase = {0: '''batch''', 1: '''choice''', 2: '''sequence'''} else: UpperCAmelCase = {0: '''batch''', 1: '''sequence'''} return OrderedDict( [ ('''input_ids''', dynamic_axis), ('''attention_mask''', dynamic_axis), ('''global_attention_mask''', dynamic_axis), ] ) @property def _lowercase ( self ): '''simple docstring''' UpperCAmelCase = super().outputs if self.task == "default": UpperCAmelCase = {0: '''batch'''} return outputs @property def _lowercase ( self ): '''simple docstring''' return 1E-4 @property def _lowercase ( self ): '''simple docstring''' return max(super().default_onnx_opset , 1_4 ) def _lowercase ( self , _A , _A = -1 , _A = -1 , _A = False , _A = None , ): '''simple docstring''' UpperCAmelCase = super().generate_dummy_inputs( preprocessor=_A , batch_size=_A , seq_length=_A , is_pair=_A , framework=_A ) import torch # for some reason, replacing this code by inputs["global_attention_mask"] = torch.randint(2, inputs["input_ids"].shape, dtype=torch.int64) # makes the export fail randomly UpperCAmelCase = torch.zeros_like(inputs['''input_ids'''] ) # make every second token global UpperCAmelCase = 1 return inputs
273
0
from typing import TYPE_CHECKING # rely on isort to merge the imports from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available snake_case : Tuple = { "configuration_autoformer": [ "AUTOFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP", "AutoformerConfig", ], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: snake_case : str = [ "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 snake_case : List[str] = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
240
from typing import List, Optional, Tuple, Union import torch from ...models import UNetaDModel from ...schedulers import KarrasVeScheduler from ...utils import randn_tensor from ..pipeline_utils import DiffusionPipeline, ImagePipelineOutput class A_ (a_ ): UpperCAmelCase__ = 42 UpperCAmelCase__ = 42 def __init__( self , _A , _A ): '''simple docstring''' super().__init__() self.register_modules(unet=_A , scheduler=_A ) @torch.no_grad() def __call__( self , _A = 1 , _A = 5_0 , _A = None , _A = "pil" , _A = True , **_A , ): '''simple docstring''' UpperCAmelCase = self.unet.config.sample_size UpperCAmelCase = (batch_size, 3, img_size, img_size) UpperCAmelCase = self.unet # sample x_0 ~ N(0, sigma_0^2 * I) UpperCAmelCase = randn_tensor(_A , generator=_A , device=self.device ) * self.scheduler.init_noise_sigma self.scheduler.set_timesteps(_A ) for t in self.progress_bar(self.scheduler.timesteps ): # here sigma_t == t_i from the paper UpperCAmelCase = self.scheduler.schedule[t] UpperCAmelCase = self.scheduler.schedule[t - 1] if t > 0 else 0 # 1. Select temporarily increased noise level sigma_hat # 2. Add new noise to move from sample_i to sample_hat UpperCAmelCase , UpperCAmelCase = self.scheduler.add_noise_to_input(_A , _A , generator=_A ) # 3. Predict the noise residual given the noise magnitude `sigma_hat` # The model inputs and output are adjusted by following eq. (213) in [1]. UpperCAmelCase = (sigma_hat / 2) * model((sample_hat + 1) / 2 , sigma_hat / 2 ).sample # 4. Evaluate dx/dt at sigma_hat # 5. Take Euler step from sigma to sigma_prev UpperCAmelCase = self.scheduler.step(_A , _A , _A , _A ) if sigma_prev != 0: # 6. Apply 2nd order correction # The model inputs and output are adjusted by following eq. (213) in [1]. UpperCAmelCase = (sigma_prev / 2) * model((step_output.prev_sample + 1) / 2 , sigma_prev / 2 ).sample UpperCAmelCase = self.scheduler.step_correct( _A , _A , _A , _A , step_output.prev_sample , step_output['''derivative'''] , ) UpperCAmelCase = step_output.prev_sample UpperCAmelCase = (sample / 2 + 0.5).clamp(0 , 1 ) UpperCAmelCase = sample.cpu().permute(0 , 2 , 3 , 1 ).numpy() if output_type == "pil": UpperCAmelCase = self.numpy_to_pil(_A ) if not return_dict: return (image,) return ImagePipelineOutput(images=_A )
273
0
'''simple docstring''' import os from typing import List, Optional, Union from ...image_processing_utils import BatchFeature from ...image_utils import ImageInput from ...processing_utils import ProcessorMixin from ...tokenization_utils_base import PaddingStrategy, PreTokenizedInput, TextInput, TruncationStrategy from ...utils import TensorType from ..auto import AutoTokenizer class A ( a_ ): '''simple docstring''' A = ["image_processor", "tokenizer"] A = "BlipImageProcessor" A = "AutoTokenizer" def __init__(self , _UpperCAmelCase , _UpperCAmelCase , _UpperCAmelCase ) -> List[str]: super().__init__(_A , _A ) # add QFormer tokenizer __UpperCamelCase : List[str] = qformer_tokenizer def __call__(self , _UpperCAmelCase = None , _UpperCAmelCase = None , _UpperCAmelCase = True , _UpperCAmelCase = False , _UpperCAmelCase = None , _UpperCAmelCase = None , _UpperCAmelCase = 0 , _UpperCAmelCase = None , _UpperCAmelCase = None , _UpperCAmelCase = False , _UpperCAmelCase = False , _UpperCAmelCase = False , _UpperCAmelCase = False , _UpperCAmelCase = False , _UpperCAmelCase = True , _UpperCAmelCase = None , **_UpperCAmelCase , ) -> Optional[int]: if images is None and text is None: raise ValueError("You have to specify at least images or text." ) __UpperCamelCase : Optional[Any] = BatchFeature() if text is not None: __UpperCamelCase : Union[str, Any] = self.tokenizer( text=_A , add_special_tokens=_A , padding=_A , truncation=_A , max_length=_A , stride=_A , pad_to_multiple_of=_A , return_attention_mask=_A , return_overflowing_tokens=_A , return_special_tokens_mask=_A , return_offsets_mapping=_A , return_token_type_ids=_A , return_length=_A , verbose=_A , return_tensors=_A , **_A , ) encoding.update(_A ) __UpperCamelCase : str = self.qformer_tokenizer( text=_A , add_special_tokens=_A , padding=_A , truncation=_A , max_length=_A , stride=_A , pad_to_multiple_of=_A , return_attention_mask=_A , return_overflowing_tokens=_A , return_special_tokens_mask=_A , return_offsets_mapping=_A , return_token_type_ids=_A , return_length=_A , verbose=_A , return_tensors=_A , **_A , ) __UpperCamelCase : Optional[int] = qformer_text_encoding.pop("input_ids" ) __UpperCamelCase : List[Any] = qformer_text_encoding.pop("attention_mask" ) if images is not None: __UpperCamelCase : List[Any] = self.image_processor(_A , return_tensors=_A ) encoding.update(_A ) return encoding def a_ (self , *_UpperCAmelCase , **_UpperCAmelCase ) -> List[str]: return self.tokenizer.batch_decode(*_A , **_A ) def a_ (self , *_UpperCAmelCase , **_UpperCAmelCase ) -> str: return self.tokenizer.decode(*_A , **_A ) @property # Copied from transformers.models.blip.processing_blip.BlipProcessor.model_input_names def a_ (self ) -> Any: __UpperCamelCase : Optional[Any] = self.tokenizer.model_input_names __UpperCamelCase : Optional[Any] = self.image_processor.model_input_names return list(dict.fromkeys(tokenizer_input_names + image_processor_input_names ) ) def a_ (self , _UpperCAmelCase , **_UpperCAmelCase ) -> Optional[Any]: if os.path.isfile(_A ): raise ValueError(f"Provided path ({save_directory}) should be a directory, not a file" ) os.makedirs(_A , exist_ok=_A ) __UpperCamelCase : Optional[int] = os.path.join(_A , "qformer_tokenizer" ) self.qformer_tokenizer.save_pretrained(_A ) return super().save_pretrained(_A , **_A ) @classmethod def a_ (cls , _UpperCAmelCase , **_UpperCAmelCase ) -> Optional[int]: __UpperCamelCase : Union[str, Any] = AutoTokenizer.from_pretrained(_A , subfolder="qformer_tokenizer" ) __UpperCamelCase : Optional[int] = cls._get_arguments_from_pretrained(_A , **_A ) args.append(_A ) return cls(*_A )
298
import itertools import random import unittest import numpy as np from transformers import BatchFeature, SpeechTaFeatureExtractor from transformers.testing_utils import require_torch from transformers.utils.import_utils import is_torch_available from ...test_sequence_feature_extraction_common import SequenceFeatureExtractionTestMixin if is_torch_available(): import torch __A : str = random.Random() def __SCREAMING_SNAKE_CASE ( UpperCamelCase__ , UpperCamelCase__=1.0 , UpperCamelCase__=None , UpperCamelCase__=None ) -> Tuple: '''simple docstring''' if rng is None: UpperCAmelCase = global_rng UpperCAmelCase = [] for batch_idx in range(shape[0] ): values.append([] ) for _ in range(shape[1] ): values[-1].append(rng.random() * scale ) return values @require_torch class A_ (unittest.TestCase ): def __init__( self , _A , _A=7 , _A=4_0_0 , _A=2_0_0_0 , _A=1 , _A=0.0 , _A=1_6_0_0_0 , _A=True , _A=8_0 , _A=1_6 , _A=6_4 , _A="hann_window" , _A=8_0 , _A=7_6_0_0 , _A=1E-10 , _A=True , ): '''simple docstring''' UpperCAmelCase = parent UpperCAmelCase = batch_size UpperCAmelCase = min_seq_length UpperCAmelCase = max_seq_length UpperCAmelCase = (self.max_seq_length - self.min_seq_length) // (self.batch_size - 1) UpperCAmelCase = feature_size UpperCAmelCase = padding_value UpperCAmelCase = sampling_rate UpperCAmelCase = do_normalize UpperCAmelCase = num_mel_bins UpperCAmelCase = hop_length UpperCAmelCase = win_length UpperCAmelCase = win_function UpperCAmelCase = fmin UpperCAmelCase = fmax UpperCAmelCase = mel_floor UpperCAmelCase = return_attention_mask def _lowercase ( self ): '''simple docstring''' return { "feature_size": self.feature_size, "padding_value": self.padding_value, "sampling_rate": self.sampling_rate, "do_normalize": self.do_normalize, "num_mel_bins": self.num_mel_bins, "hop_length": self.hop_length, "win_length": self.win_length, "win_function": self.win_function, "fmin": self.fmin, "fmax": self.fmax, "mel_floor": self.mel_floor, "return_attention_mask": self.return_attention_mask, } def _lowercase ( self , _A=False , _A=False ): '''simple docstring''' def _flatten(_A ): return list(itertools.chain(*_A ) ) if equal_length: UpperCAmelCase = floats_list((self.batch_size, self.max_seq_length) ) else: # make sure that inputs increase in size UpperCAmelCase = [ _flatten(floats_list((x, self.feature_size) ) ) for x in range(self.min_seq_length , self.max_seq_length , self.seq_length_diff ) ] if numpify: UpperCAmelCase = [np.asarray(_A ) for x in speech_inputs] return speech_inputs def _lowercase ( self , _A=False , _A=False ): '''simple docstring''' if equal_length: UpperCAmelCase = [floats_list((self.max_seq_length, self.num_mel_bins) ) for _ in range(self.batch_size )] else: # make sure that inputs increase in size UpperCAmelCase = [ floats_list((x, self.num_mel_bins) ) for x in range(self.min_seq_length , self.max_seq_length , self.seq_length_diff ) ] if numpify: UpperCAmelCase = [np.asarray(_A ) for x in speech_inputs] return speech_inputs @require_torch class A_ (a_ , unittest.TestCase ): UpperCAmelCase__ = SpeechTaFeatureExtractor def _lowercase ( self ): '''simple docstring''' UpperCAmelCase = SpeechTaFeatureExtractionTester(self ) def _lowercase ( self , _A ): '''simple docstring''' self.assertTrue(np.all(np.mean(_A , axis=0 ) < 1E-3 ) ) self.assertTrue(np.all(np.abs(np.var(_A , axis=0 ) - 1 ) < 1E-3 ) ) def _lowercase ( self ): '''simple docstring''' UpperCAmelCase = self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() ) # create three inputs of length 800, 1000, and 1200 UpperCAmelCase = [floats_list((1, x) )[0] for x in range(8_0_0 , 1_4_0_0 , 2_0_0 )] UpperCAmelCase = [np.asarray(_A ) for speech_input in speech_inputs] # Test not batched input UpperCAmelCase = feat_extract(speech_inputs[0] , return_tensors='''np''' ).input_values UpperCAmelCase = feat_extract(np_speech_inputs[0] , return_tensors='''np''' ).input_values self.assertTrue(np.allclose(_A , _A , atol=1E-3 ) ) # Test batched UpperCAmelCase = feat_extract(_A , return_tensors='''np''' ).input_values UpperCAmelCase = feat_extract(_A , return_tensors='''np''' ).input_values for enc_seq_a, enc_seq_a in zip(_A , _A ): self.assertTrue(np.allclose(_A , _A , atol=1E-3 ) ) def _lowercase ( self ): '''simple docstring''' UpperCAmelCase = self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() ) UpperCAmelCase = [floats_list((1, x) )[0] for x in range(8_0_0 , 1_4_0_0 , 2_0_0 )] UpperCAmelCase = ['''longest''', '''max_length''', '''do_not_pad'''] UpperCAmelCase = [None, 1_6_0_0, None] for max_length, padding in zip(_A , _A ): UpperCAmelCase = feat_extract(_A , padding=_A , max_length=_A , return_tensors='''np''' ) UpperCAmelCase = processed.input_values self._check_zero_mean_unit_variance(input_values[0][:8_0_0] ) self.assertTrue(input_values[0][8_0_0:].sum() < 1E-6 ) self._check_zero_mean_unit_variance(input_values[1][:1_0_0_0] ) self.assertTrue(input_values[0][1_0_0_0:].sum() < 1E-6 ) self._check_zero_mean_unit_variance(input_values[2][:1_2_0_0] ) def _lowercase ( self ): '''simple docstring''' UpperCAmelCase = self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() ) UpperCAmelCase = range(8_0_0 , 1_4_0_0 , 2_0_0 ) UpperCAmelCase = [floats_list((1, x) )[0] for x in lengths] UpperCAmelCase = ['''longest''', '''max_length''', '''do_not_pad'''] UpperCAmelCase = [None, 1_6_0_0, None] for max_length, padding in zip(_A , _A ): UpperCAmelCase = feat_extract(_A , max_length=_A , padding=_A ) UpperCAmelCase = processed.input_values self._check_zero_mean_unit_variance(input_values[0][:8_0_0] ) self._check_zero_mean_unit_variance(input_values[1][:1_0_0_0] ) self._check_zero_mean_unit_variance(input_values[2][:1_2_0_0] ) def _lowercase ( self ): '''simple docstring''' UpperCAmelCase = self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() ) UpperCAmelCase = [floats_list((1, x) )[0] for x in range(8_0_0 , 1_4_0_0 , 2_0_0 )] UpperCAmelCase = feat_extract( _A , truncation=_A , max_length=1_0_0_0 , padding='''max_length''' , return_tensors='''np''' ) UpperCAmelCase = processed.input_values self._check_zero_mean_unit_variance(input_values[0, :8_0_0] ) self._check_zero_mean_unit_variance(input_values[1] ) self._check_zero_mean_unit_variance(input_values[2] ) def _lowercase ( self ): '''simple docstring''' UpperCAmelCase = self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() ) UpperCAmelCase = [floats_list((1, x) )[0] for x in range(8_0_0 , 1_4_0_0 , 2_0_0 )] UpperCAmelCase = feat_extract( _A , truncation=_A , max_length=1_0_0_0 , padding='''longest''' , return_tensors='''np''' ) UpperCAmelCase = processed.input_values self._check_zero_mean_unit_variance(input_values[0, :8_0_0] ) self._check_zero_mean_unit_variance(input_values[1, :1_0_0_0] ) self._check_zero_mean_unit_variance(input_values[2] ) # make sure that if max_length < longest -> then pad to max_length self.assertTrue(input_values.shape == (3, 1_0_0_0) ) UpperCAmelCase = [floats_list((1, x) )[0] for x in range(8_0_0 , 1_4_0_0 , 2_0_0 )] UpperCAmelCase = feat_extract( _A , truncation=_A , max_length=2_0_0_0 , padding='''longest''' , return_tensors='''np''' ) UpperCAmelCase = processed.input_values self._check_zero_mean_unit_variance(input_values[0, :8_0_0] ) self._check_zero_mean_unit_variance(input_values[1, :1_0_0_0] ) self._check_zero_mean_unit_variance(input_values[2] ) # make sure that if max_length > longest -> then pad to longest self.assertTrue(input_values.shape == (3, 1_2_0_0) ) def _lowercase ( self ): '''simple docstring''' UpperCAmelCase = self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() ) UpperCAmelCase = np.random.rand(1_0_0 ).astype(np.floataa ) UpperCAmelCase = np_speech_inputs.tolist() for inputs in [py_speech_inputs, np_speech_inputs]: UpperCAmelCase = feature_extractor.pad([{'''input_values''': inputs}] , return_tensors='''np''' ) self.assertTrue(np_processed.input_values.dtype == np.floataa ) UpperCAmelCase = feature_extractor.pad([{'''input_values''': inputs}] , return_tensors='''pt''' ) self.assertTrue(pt_processed.input_values.dtype == torch.floataa ) def _lowercase ( self ): '''simple docstring''' UpperCAmelCase = self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() ) # create three inputs of length 800, 1000, and 1200 UpperCAmelCase = [floats_list((1, x) )[0] for x in range(8_0_0 , 1_4_0_0 , 2_0_0 )] UpperCAmelCase = [np.asarray(_A ) for speech_input in speech_inputs] # Test feature size UpperCAmelCase = feature_extractor(audio_target=_A , padding=_A , return_tensors='''np''' ).input_values self.assertTrue(input_values.ndim == 3 ) self.assertTrue(input_values.shape[-1] == feature_extractor.num_mel_bins ) # Test not batched input UpperCAmelCase = feature_extractor(speech_inputs[0] , return_tensors='''np''' ).input_values UpperCAmelCase = feature_extractor(np_speech_inputs[0] , return_tensors='''np''' ).input_values self.assertTrue(np.allclose(_A , _A , atol=1E-3 ) ) # Test batched UpperCAmelCase = feature_extractor(_A , return_tensors='''np''' ).input_values UpperCAmelCase = feature_extractor(_A , return_tensors='''np''' ).input_values for enc_seq_a, enc_seq_a in zip(_A , _A ): self.assertTrue(np.allclose(_A , _A , atol=1E-3 ) ) # Test 2-D numpy arrays are batched. UpperCAmelCase = [floats_list((1, x) )[0] for x in (8_0_0, 8_0_0, 8_0_0)] UpperCAmelCase = np.asarray(_A ) UpperCAmelCase = feature_extractor(_A , return_tensors='''np''' ).input_values UpperCAmelCase = feature_extractor(_A , return_tensors='''np''' ).input_values for enc_seq_a, enc_seq_a in zip(_A , _A ): self.assertTrue(np.allclose(_A , _A , atol=1E-3 ) ) def _lowercase ( self ): '''simple docstring''' UpperCAmelCase = self.feat_extract_tester.prepare_inputs_for_target() UpperCAmelCase = self.feature_extraction_class(**self.feat_extract_dict ) UpperCAmelCase = feat_extract.model_input_names[0] UpperCAmelCase = BatchFeature({input_name: speech_inputs} ) self.assertTrue(all(len(_A ) == len(_A ) for x, y in zip(_A , processed_features[input_name] ) ) ) UpperCAmelCase = self.feat_extract_tester.prepare_inputs_for_target(equal_length=_A ) UpperCAmelCase = BatchFeature({input_name: speech_inputs} , tensor_type='''np''' ) UpperCAmelCase = processed_features[input_name] if len(batch_features_input.shape ) < 3: UpperCAmelCase = batch_features_input[:, :, None] self.assertTrue( batch_features_input.shape == (self.feat_extract_tester.batch_size, len(speech_inputs[0] ), self.feat_extract_tester.num_mel_bins) ) @require_torch def _lowercase ( self ): '''simple docstring''' UpperCAmelCase = self.feat_extract_tester.prepare_inputs_for_target(equal_length=_A ) UpperCAmelCase = self.feature_extraction_class(**self.feat_extract_dict ) UpperCAmelCase = feat_extract.model_input_names[0] UpperCAmelCase = BatchFeature({input_name: speech_inputs} , tensor_type='''pt''' ) UpperCAmelCase = processed_features[input_name] if len(batch_features_input.shape ) < 3: UpperCAmelCase = batch_features_input[:, :, None] self.assertTrue( batch_features_input.shape == (self.feat_extract_tester.batch_size, len(speech_inputs[0] ), self.feat_extract_tester.num_mel_bins) ) @require_torch def _lowercase ( self ): '''simple docstring''' UpperCAmelCase = self.feature_extraction_class(**self.feat_extract_dict ) UpperCAmelCase = self.feat_extract_tester.prepare_inputs_for_target() UpperCAmelCase = feat_extract.model_input_names[0] UpperCAmelCase = BatchFeature({input_name: speech_inputs} ) UpperCAmelCase = feat_extract.num_mel_bins # hack! UpperCAmelCase = feat_extract.pad(_A , padding='''longest''' , return_tensors='''np''' )[input_name] UpperCAmelCase = feat_extract.pad(_A , padding='''longest''' , return_tensors='''pt''' )[input_name] self.assertTrue(abs(input_np.astype(np.floataa ).sum() - input_pt.numpy().astype(np.floataa ).sum() ) < 1E-2 ) def _lowercase ( self ): '''simple docstring''' UpperCAmelCase = self.feat_extract_dict UpperCAmelCase = True UpperCAmelCase = self.feature_extraction_class(**_A ) UpperCAmelCase = self.feat_extract_tester.prepare_inputs_for_target() UpperCAmelCase = [len(_A ) for x in speech_inputs] UpperCAmelCase = feat_extract.model_input_names[0] UpperCAmelCase = BatchFeature({input_name: speech_inputs} ) UpperCAmelCase = feat_extract.num_mel_bins # hack! UpperCAmelCase = feat_extract.pad(_A , padding='''longest''' , return_tensors='''np''' ) self.assertIn('''attention_mask''' , _A ) self.assertListEqual(list(processed.attention_mask.shape ) , list(processed[input_name].shape[:2] ) ) self.assertListEqual(processed.attention_mask.sum(-1 ).tolist() , _A ) def _lowercase ( self ): '''simple docstring''' UpperCAmelCase = self.feat_extract_dict UpperCAmelCase = True UpperCAmelCase = self.feature_extraction_class(**_A ) UpperCAmelCase = self.feat_extract_tester.prepare_inputs_for_target() UpperCAmelCase = [len(_A ) for x in speech_inputs] UpperCAmelCase = feat_extract.model_input_names[0] UpperCAmelCase = BatchFeature({input_name: speech_inputs} ) UpperCAmelCase = min(_A ) UpperCAmelCase = feat_extract.num_mel_bins # hack! UpperCAmelCase = feat_extract.pad( _A , padding='''max_length''' , max_length=_A , truncation=_A , return_tensors='''np''' ) self.assertIn('''attention_mask''' , _A ) self.assertListEqual( list(processed_pad.attention_mask.shape ) , [processed_pad[input_name].shape[0], max_length] ) self.assertListEqual( processed_pad.attention_mask[:, :max_length].sum(-1 ).tolist() , [max_length for x in speech_inputs] ) def _lowercase ( self , _A ): '''simple docstring''' from datasets import load_dataset UpperCAmelCase = load_dataset('''hf-internal-testing/librispeech_asr_dummy''' , '''clean''' , split='''validation''' ) # automatic decoding with librispeech UpperCAmelCase = ds.sort('''id''' ).select(range(_A ) )[:num_samples]['''audio'''] return [x["array"] for x in speech_samples] def _lowercase ( self ): '''simple docstring''' UpperCAmelCase = torch.tensor( [2.3804E-03, 2.0752E-03, 1.9836E-03, 2.1057E-03, 1.6174E-03, 3.0518E-04, 9.1553E-05, 3.3569E-04, 9.7656E-04, 1.8311E-03, 2.0142E-03, 2.1057E-03, 1.7395E-03, 4.5776E-04, -3.9673E-04, 4.5776E-04, 1.0071E-03, 9.1553E-05, 4.8828E-04, 1.1597E-03, 7.3242E-04, 9.4604E-04, 1.8005E-03, 1.8311E-03, 8.8501E-04, 4.2725E-04, 4.8828E-04, 7.3242E-04, 1.0986E-03, 2.1057E-03] ) # fmt: on UpperCAmelCase = self._load_datasamples(1 ) UpperCAmelCase = SpeechTaFeatureExtractor() UpperCAmelCase = feature_extractor(_A , return_tensors='''pt''' ).input_values self.assertEquals(input_values.shape , (1, 9_3_6_8_0) ) self.assertTrue(torch.allclose(input_values[0, :3_0] , _A , atol=1E-6 ) ) def _lowercase ( self ): '''simple docstring''' UpperCAmelCase = torch.tensor( [-2.68_70, -3.01_04, -3.13_56, -3.53_52, -3.00_44, -3.03_53, -3.47_19, -3.67_77, -3.15_20, -2.94_35, -2.65_53, -2.87_95, -2.99_44, -2.59_21, -3.02_79, -3.03_86, -3.08_64, -3.12_91, -3.23_53, -2.74_44, -2.68_31, -2.72_87, -3.17_61, -3.15_71, -3.27_26, -3.05_82, -3.10_07, -3.45_33, -3.46_95, -3.09_98] ) # fmt: on UpperCAmelCase = self._load_datasamples(1 ) UpperCAmelCase = SpeechTaFeatureExtractor() UpperCAmelCase = feature_extractor(audio_target=_A , return_tensors='''pt''' ).input_values self.assertEquals(input_values.shape , (1, 3_6_6, 8_0) ) self.assertTrue(torch.allclose(input_values[0, 0, :3_0] , _A , atol=1E-4 ) )
273
0
import gc import random import unittest import numpy as np import torch from PIL import Image from transformers import CLIPTextConfig, CLIPTextModel, CLIPTokenizer from diffusers import AutoencoderKL, PNDMScheduler, StableDiffusionInpaintPipeline, UNetaDConditionModel from diffusers.utils import floats_tensor, load_image, load_numpy, torch_device from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu, slow from ..pipeline_params import TEXT_GUIDED_IMAGE_INPAINTING_BATCH_PARAMS, TEXT_GUIDED_IMAGE_INPAINTING_PARAMS from ..test_pipelines_common import PipelineKarrasSchedulerTesterMixin, PipelineLatentTesterMixin, PipelineTesterMixin enable_full_determinism() class __lowerCamelCase (a_ , a_ , a_ , unittest.TestCase ): _lowercase = StableDiffusionInpaintPipeline _lowercase = TEXT_GUIDED_IMAGE_INPAINTING_PARAMS _lowercase = TEXT_GUIDED_IMAGE_INPAINTING_BATCH_PARAMS _lowercase = frozenset( [] ) # TO-DO: update image_params once pipeline is refactored with VaeImageProcessor.preprocess _lowercase = frozenset([] ) def snake_case_ ( self: Tuple ): '''simple docstring''' torch.manual_seed(0 ) __UpperCamelCase = UNetaDConditionModel( block_out_channels=(32, 64),layers_per_block=2,sample_size=32,in_channels=9,out_channels=4,down_block_types=('DownBlock2D', 'CrossAttnDownBlock2D'),up_block_types=('CrossAttnUpBlock2D', 'UpBlock2D'),cross_attention_dim=32,attention_head_dim=(2, 4),use_linear_projection=_A,) __UpperCamelCase = PNDMScheduler(skip_prk_steps=_A ) torch.manual_seed(0 ) __UpperCamelCase = AutoencoderKL( block_out_channels=[32, 64],in_channels=3,out_channels=3,down_block_types=['DownEncoderBlock2D', 'DownEncoderBlock2D'],up_block_types=['UpDecoderBlock2D', 'UpDecoderBlock2D'],latent_channels=4,sample_size=128,) torch.manual_seed(0 ) __UpperCamelCase = CLIPTextConfig( bos_token_id=0,eos_token_id=2,hidden_size=32,intermediate_size=37,layer_norm_eps=1E-05,num_attention_heads=4,num_hidden_layers=5,pad_token_id=1,vocab_size=1000,hidden_act='gelu',projection_dim=512,) __UpperCamelCase = CLIPTextModel(_A ) __UpperCamelCase = CLIPTokenizer.from_pretrained('hf-internal-testing/tiny-random-clip' ) __UpperCamelCase = { 'unet': unet, 'scheduler': scheduler, 'vae': vae, 'text_encoder': text_encoder, 'tokenizer': tokenizer, 'safety_checker': None, 'feature_extractor': None, } return components def snake_case_ ( self: List[Any],A_: str,A_: Optional[Any]=0 ): '''simple docstring''' __UpperCamelCase = floats_tensor((1, 3, 32, 32),rng=random.Random(_A ) ).to(_A ) __UpperCamelCase = image.cpu().permute(0,2,3,1 )[0] __UpperCamelCase = Image.fromarray(np.uinta(_A ) ).convert('RGB' ).resize((64, 64) ) __UpperCamelCase = Image.fromarray(np.uinta(image + 4 ) ).convert('RGB' ).resize((64, 64) ) if str(_A ).startswith('mps' ): __UpperCamelCase = torch.manual_seed(_A ) else: __UpperCamelCase = torch.Generator(device=_A ).manual_seed(_A ) __UpperCamelCase = { 'prompt': 'A painting of a squirrel eating a burger', 'image': init_image, 'mask_image': mask_image, 'generator': generator, 'num_inference_steps': 2, 'guidance_scale': 6.0, 'output_type': 'numpy', } return inputs def snake_case_ ( self: Optional[Any] ): '''simple docstring''' __UpperCamelCase = 'cpu' # ensure determinism for the device-dependent torch.Generator __UpperCamelCase = self.get_dummy_components() __UpperCamelCase = StableDiffusionInpaintPipeline(**_A ) __UpperCamelCase = sd_pipe.to(_A ) sd_pipe.set_progress_bar_config(disable=_A ) __UpperCamelCase = self.get_dummy_inputs(_A ) __UpperCamelCase = sd_pipe(**_A ).images __UpperCamelCase = image[0, -3:, -3:, -1] assert image.shape == (1, 64, 64, 3) __UpperCamelCase = np.array([0.4_7_2_7, 0.5_7_3_5, 0.3_9_4_1, 0.5_4_4_6, 0.5_9_2_6, 0.4_3_9_4, 0.5_0_6_2, 0.4_6_5_4, 0.4_4_7_6] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-2 def snake_case_ ( self: List[Any] ): '''simple docstring''' super().test_inference_batch_single_identical(expected_max_diff=3E-3 ) @slow @require_torch_gpu class __lowerCamelCase (unittest.TestCase ): def snake_case_ ( self: Optional[int] ): '''simple docstring''' super().tearDown() gc.collect() torch.cuda.empty_cache() def snake_case_ ( self: int ): '''simple docstring''' __UpperCamelCase = load_image( 'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main' '/sd2-inpaint/init_image.png' ) __UpperCamelCase = load_image( 'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd2-inpaint/mask.png' ) __UpperCamelCase = load_numpy( 'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd2-inpaint' '/yellow_cat_sitting_on_a_park_bench.npy' ) __UpperCamelCase = 'stabilityai/stable-diffusion-2-inpainting' __UpperCamelCase = StableDiffusionInpaintPipeline.from_pretrained(_A,safety_checker=_A ) pipe.to(_A ) pipe.set_progress_bar_config(disable=_A ) pipe.enable_attention_slicing() __UpperCamelCase = 'Face of a yellow cat, high resolution, sitting on a park bench' __UpperCamelCase = torch.manual_seed(0 ) __UpperCamelCase = pipe( prompt=_A,image=_A,mask_image=_A,generator=_A,output_type='np',) __UpperCamelCase = output.images[0] assert image.shape == (512, 512, 3) assert np.abs(expected_image - image ).max() < 9E-3 def snake_case_ ( self: List[str] ): '''simple docstring''' __UpperCamelCase = load_image( 'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main' '/sd2-inpaint/init_image.png' ) __UpperCamelCase = load_image( 'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd2-inpaint/mask.png' ) __UpperCamelCase = load_numpy( 'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd2-inpaint' '/yellow_cat_sitting_on_a_park_bench_fp16.npy' ) __UpperCamelCase = 'stabilityai/stable-diffusion-2-inpainting' __UpperCamelCase = StableDiffusionInpaintPipeline.from_pretrained( _A,torch_dtype=torch.floataa,safety_checker=_A,) pipe.to(_A ) pipe.set_progress_bar_config(disable=_A ) pipe.enable_attention_slicing() __UpperCamelCase = 'Face of a yellow cat, high resolution, sitting on a park bench' __UpperCamelCase = torch.manual_seed(0 ) __UpperCamelCase = pipe( prompt=_A,image=_A,mask_image=_A,generator=_A,output_type='np',) __UpperCamelCase = output.images[0] assert image.shape == (512, 512, 3) assert np.abs(expected_image - image ).max() < 5E-1 def snake_case_ ( self: List[Any] ): '''simple docstring''' torch.cuda.empty_cache() torch.cuda.reset_max_memory_allocated() torch.cuda.reset_peak_memory_stats() __UpperCamelCase = load_image( 'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main' '/sd2-inpaint/init_image.png' ) __UpperCamelCase = load_image( 'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd2-inpaint/mask.png' ) __UpperCamelCase = 'stabilityai/stable-diffusion-2-inpainting' __UpperCamelCase = PNDMScheduler.from_pretrained(_A,subfolder='scheduler' ) __UpperCamelCase = StableDiffusionInpaintPipeline.from_pretrained( _A,safety_checker=_A,scheduler=_A,torch_dtype=torch.floataa,) pipe.to(_A ) pipe.set_progress_bar_config(disable=_A ) pipe.enable_attention_slicing(1 ) pipe.enable_sequential_cpu_offload() __UpperCamelCase = 'Face of a yellow cat, high resolution, sitting on a park bench' __UpperCamelCase = torch.manual_seed(0 ) __UpperCamelCase = pipe( prompt=_A,image=_A,mask_image=_A,generator=_A,num_inference_steps=2,output_type='np',) __UpperCamelCase = torch.cuda.max_memory_allocated() # make sure that less than 2.65 GB is allocated assert mem_bytes < 2.6_5 * 10**9
310
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_sentencepiece_available, is_speech_available, is_tf_available, is_torch_available, ) __A : Union[str, Any] = { "configuration_speech_to_text": ["SPEECH_TO_TEXT_PRETRAINED_CONFIG_ARCHIVE_MAP", "Speech2TextConfig"], "processing_speech_to_text": ["Speech2TextProcessor"], } try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __A : List[Any] = ["Speech2TextTokenizer"] try: if not is_speech_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __A : List[Any] = ["Speech2TextFeatureExtractor"] try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __A : int = [ "TF_SPEECH_TO_TEXT_PRETRAINED_MODEL_ARCHIVE_LIST", "TFSpeech2TextForConditionalGeneration", "TFSpeech2TextModel", "TFSpeech2TextPreTrainedModel", ] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __A : Tuple = [ "SPEECH_TO_TEXT_PRETRAINED_MODEL_ARCHIVE_LIST", "Speech2TextForConditionalGeneration", "Speech2TextModel", "Speech2TextPreTrainedModel", ] if TYPE_CHECKING: from .configuration_speech_to_text import SPEECH_TO_TEXT_PRETRAINED_CONFIG_ARCHIVE_MAP, SpeechaTextConfig from .processing_speech_to_text import SpeechaTextProcessor try: if not is_sentencepiece_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .tokenization_speech_to_text import SpeechaTextTokenizer try: if not is_speech_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .feature_extraction_speech_to_text import SpeechaTextFeatureExtractor try: if not is_tf_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_tf_speech_to_text import ( TF_SPEECH_TO_TEXT_PRETRAINED_MODEL_ARCHIVE_LIST, TFSpeechaTextForConditionalGeneration, TFSpeechaTextModel, TFSpeechaTextPreTrainedModel, ) try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_speech_to_text import ( SPEECH_TO_TEXT_PRETRAINED_MODEL_ARCHIVE_LIST, SpeechaTextForConditionalGeneration, SpeechaTextModel, SpeechaTextPreTrainedModel, ) else: import sys __A : Dict = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
273
0
import math from collections.abc import Callable def lowerCamelCase_ ( _a , _a , _a ): """simple docstring""" lowerCAmelCase__ : Any = xa lowerCAmelCase__ : List[str] = xa while True: if x_n == x_na or function(UpperCamelCase__ ) == function(UpperCamelCase__ ): raise ZeroDivisionError('''float division by zero, could not find root''' ) lowerCAmelCase__ : Tuple = x_na - ( function(UpperCamelCase__ ) / ((function(UpperCamelCase__ ) - function(UpperCamelCase__ )) / (x_na - x_n)) ) if abs(x_na - x_na ) < 10**-5: return x_na lowerCAmelCase__ : Union[str, Any] = x_na lowerCAmelCase__ : List[str] = x_na def lowerCamelCase_ ( _a ): """simple docstring""" return math.pow(UpperCamelCase__ , 3 ) - (2 * x) - 5 if __name__ == "__main__": print(intersection(f, 3, 3.5))
131
def __SCREAMING_SNAKE_CASE ( UpperCamelCase__ ) -> list[int]: '''simple docstring''' if length <= 0 or not isinstance(UpperCamelCase__ , UpperCamelCase__ ): raise ValueError('''Length must be a positive integer.''' ) return [n * (2 * n - 1) for n in range(UpperCamelCase__ )] if __name__ == "__main__": print(hexagonal_numbers(length=5)) print(hexagonal_numbers(length=10))
273
0
import inspect import unittest import numpy as np from transformers import ViTConfig, is_flax_available from transformers.testing_utils import require_flax, slow from ...test_configuration_common import ConfigTester from ...test_modeling_flax_common import FlaxModelTesterMixin, floats_tensor if is_flax_available(): import jax from transformers.models.vit.modeling_flax_vit import FlaxViTForImageClassification, FlaxViTModel class _lowerCamelCase ( unittest.TestCase ): """simple docstring""" def __init__( self , UpperCAmelCase , UpperCAmelCase=13 , UpperCAmelCase=30 , UpperCAmelCase=2 , UpperCAmelCase=3 , UpperCAmelCase=True , UpperCAmelCase=True , UpperCAmelCase=32 , UpperCAmelCase=5 , UpperCAmelCase=4 , UpperCAmelCase=37 , UpperCAmelCase="gelu" , UpperCAmelCase=0.1 , UpperCAmelCase=0.1 , UpperCAmelCase=10 , UpperCAmelCase=0.02 , ) -> str: '''simple docstring''' __snake_case : List[Any] = parent __snake_case : List[str] = batch_size __snake_case : Tuple = image_size __snake_case : Any = patch_size __snake_case : Union[str, Any] = num_channels __snake_case : Dict = is_training __snake_case : Optional[int] = use_labels __snake_case : Optional[Any] = hidden_size __snake_case : List[Any] = num_hidden_layers __snake_case : Union[str, Any] = num_attention_heads __snake_case : Dict = intermediate_size __snake_case : str = hidden_act __snake_case : List[str] = hidden_dropout_prob __snake_case : List[Any] = attention_probs_dropout_prob __snake_case : Tuple = type_sequence_label_size __snake_case : int = initializer_range # in ViT, the seq length equals the number of patches + 1 (we add 1 for the [CLS] token) __snake_case : Dict = (image_size // patch_size) ** 2 __snake_case : List[Any] = num_patches + 1 def UpperCAmelCase ( self ) -> Optional[Any]: '''simple docstring''' __snake_case : Dict = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) __snake_case : Union[str, Any] = ViTConfig( image_size=self.image_size , patch_size=self.patch_size , num_channels=self.num_channels , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , is_decoder=_A , initializer_range=self.initializer_range , ) return config, pixel_values def UpperCAmelCase ( self , UpperCAmelCase , UpperCAmelCase ) -> int: '''simple docstring''' __snake_case : int = FlaxViTModel(config=_A ) __snake_case : Any = model(_A ) # expected sequence length = num_patches + 1 (we add 1 for the [CLS] token) __snake_case : List[Any] = (self.image_size, self.image_size) __snake_case : Tuple = (self.patch_size, self.patch_size) __snake_case : List[Any] = (image_size[1] // patch_size[1]) * (image_size[0] // patch_size[0]) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, num_patches + 1, self.hidden_size) ) def UpperCAmelCase ( self , UpperCAmelCase , UpperCAmelCase ) -> List[str]: '''simple docstring''' __snake_case : int = self.type_sequence_label_size __snake_case : Tuple = FlaxViTForImageClassification(config=_A ) __snake_case : List[str] = model(_A ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.type_sequence_label_size) ) # test greyscale images __snake_case : Union[str, Any] = 1 __snake_case : List[str] = FlaxViTForImageClassification(_A ) __snake_case : Optional[int] = floats_tensor([self.batch_size, 1, self.image_size, self.image_size] ) __snake_case : Tuple = model(_A ) def UpperCAmelCase ( self ) -> Optional[int]: '''simple docstring''' __snake_case : str = self.prepare_config_and_inputs() ( ( __snake_case ) , ( __snake_case ) , ) : List[str] = config_and_inputs __snake_case : Dict = {"pixel_values": pixel_values} return config, inputs_dict @require_flax class _lowerCamelCase ( a_ , unittest.TestCase ): """simple docstring""" UpperCAmelCase_ : Tuple =(FlaxViTModel, FlaxViTForImageClassification) if is_flax_available() else () def UpperCAmelCase ( self ) -> Optional[Any]: '''simple docstring''' __snake_case : List[Any] = FlaxViTModelTester(self ) __snake_case : List[Any] = ConfigTester(self , config_class=_A , has_text_modality=_A , hidden_size=37 ) def UpperCAmelCase ( self ) -> Any: '''simple docstring''' self.config_tester.run_common_tests() def UpperCAmelCase ( self ) -> int: '''simple docstring''' __snake_case : Optional[Any] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*_A ) def UpperCAmelCase ( self ) -> int: '''simple docstring''' __snake_case : Optional[int] = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_image_classification(*_A ) def UpperCAmelCase ( self ) -> Optional[int]: '''simple docstring''' __snake_case , __snake_case : Optional[int] = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: __snake_case : str = model_class(_A ) __snake_case : Optional[int] = inspect.signature(model.__call__ ) # signature.parameters is an OrderedDict => so arg_names order is deterministic __snake_case : str = [*signature.parameters.keys()] __snake_case : int = ["pixel_values"] self.assertListEqual(arg_names[:1] , _A ) def UpperCAmelCase ( self ) -> str: '''simple docstring''' __snake_case , __snake_case : Optional[Any] = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: with self.subTest(model_class.__name__ ): __snake_case : Union[str, Any] = self._prepare_for_class(_A , _A ) __snake_case : int = model_class(_A ) @jax.jit def model_jitted(UpperCAmelCase , **UpperCAmelCase ): return model(pixel_values=_A , **_A ) with self.subTest("JIT Enabled" ): __snake_case : List[Any] = model_jitted(**_A ).to_tuple() with self.subTest("JIT Disabled" ): with jax.disable_jit(): __snake_case : int = model_jitted(**_A ).to_tuple() self.assertEqual(len(_A ) , len(_A ) ) for jitted_output, output in zip(_A , _A ): self.assertEqual(jitted_output.shape , output.shape ) @slow def UpperCAmelCase ( self ) -> Optional[Any]: '''simple docstring''' for model_class_name in self.all_model_classes: __snake_case : Tuple = model_class_name.from_pretrained("google/vit-base-patch16-224" ) __snake_case : int = model(np.ones((1, 3, 224, 224) ) ) self.assertIsNotNone(_A )
326
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_5_0, '''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_0_0, '''eval_accuracy''': 0.3, '''eval_loss''': 0.9}, }, ] ) class A_ (unittest.TestCase ): def _lowercase ( self ): '''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=_A , ) assert hasattr(self , '''env''' ) def _lowercase ( self , _A=1 ): '''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=_A , instance_type=self.instance_type , debugger_hook_config=_A , hyperparameters={**self.env.hyperparameters, '''model_name_or_path''': self.model_name_or_path} , metric_definitions=self.env.metric_definitions , py_version='''py36''' , ) def _lowercase ( self , _A ): '''simple docstring''' TrainingJobAnalytics(_A ).export_csv(F"""{self.env.test_path}/{job_name}_metrics.csv""" ) def _lowercase ( self ): '''simple docstring''' UpperCAmelCase = self.create_estimator() # run training estimator.fit() # result dataframe UpperCAmelCase = TrainingJobAnalytics(estimator.latest_training_job.name ).dataframe() # extract kpis UpperCAmelCase = list(result_metrics_df[result_metrics_df.metric_name == '''eval_accuracy''']['''value'''] ) UpperCAmelCase = list(result_metrics_df[result_metrics_df.metric_name == '''eval_loss''']['''value'''] ) # get train time from SageMaker job, this includes starting, preprocessing, stopping UpperCAmelCase = ( Session().describe_training_job(estimator.latest_training_job.name ).get('''TrainingTimeInSeconds''' , 9_9_9_9_9_9 ) ) # 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} , _A )
273
0
"""simple docstring""" import inspect import re from transformers.utils import direct_transformers_import # All paths are set with the intent you should run this script from the root of the repo with the command # python utils/check_config_docstrings.py __A = "src/transformers" # This is to make sure the transformers module imported is the one in the repo. __A = direct_transformers_import(PATH_TO_TRANSFORMERS) __A = transformers.models.auto.configuration_auto.CONFIG_MAPPING # Regex pattern used to find the checkpoint mentioned in the docstring of `config_class`. # For example, `[bert-base-uncased](https://huggingface.co/bert-base-uncased)` __A = re.compile(r"\[(.+?)\]\((https://huggingface\.co/.+?)\)") __A = { "DecisionTransformerConfig", "EncoderDecoderConfig", "MusicgenConfig", "RagConfig", "SpeechEncoderDecoderConfig", "TimmBackboneConfig", "VisionEncoderDecoderConfig", "VisionTextDualEncoderConfig", "LlamaConfig", } def a__ ( __SCREAMING_SNAKE_CASE ) -> List[str]: __lowerCAmelCase: Optional[Any] = None # source code of `config_class` __lowerCAmelCase: Tuple = inspect.getsource(UpperCamelCase__ ) __lowerCAmelCase: Tuple = _re_checkpoint.findall(UpperCamelCase__ ) # Each `checkpoint` is a tuple of a checkpoint name and a checkpoint link. # For example, `('bert-base-uncased', 'https://huggingface.co/bert-base-uncased')` for ckpt_name, ckpt_link in checkpoints: # allow the link to end with `/` if ckpt_link.endswith("/" ): __lowerCAmelCase: int = ckpt_link[:-1] # verify the checkpoint name corresponds to the checkpoint link __lowerCAmelCase: Union[str, Any] = F"https://huggingface.co/{ckpt_name}" if ckpt_link == ckpt_link_from_name: __lowerCAmelCase: Optional[int] = ckpt_name break return checkpoint def a__ ( ) -> List[str]: __lowerCAmelCase: Optional[int] = [] for config_class in list(CONFIG_MAPPING.values() ): # Skip deprecated models if "models.deprecated" in config_class.__module__: continue __lowerCAmelCase: List[str] = get_checkpoint_from_config_class(UpperCamelCase__ ) __lowerCAmelCase: Dict = config_class.__name__ if checkpoint is None and name not in CONFIG_CLASSES_TO_IGNORE_FOR_DOCSTRING_CHECKPOINT_CHECK: configs_without_checkpoint.append(UpperCamelCase__ ) if len(UpperCamelCase__ ) > 0: __lowerCAmelCase: str = "\n".join(sorted(UpperCamelCase__ ) ) raise ValueError(F"The following configurations don't contain any valid checkpoint:\n{message}" ) if __name__ == "__main__": check_config_docstrings_have_checkpoints()
217
from collections import OrderedDict from typing import Mapping from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging __A : int = logging.get_logger(__name__) __A : Tuple = { "google/bigbird-roberta-base": "https://huggingface.co/google/bigbird-roberta-base/resolve/main/config.json", "google/bigbird-roberta-large": "https://huggingface.co/google/bigbird-roberta-large/resolve/main/config.json", "google/bigbird-base-trivia-itc": "https://huggingface.co/google/bigbird-base-trivia-itc/resolve/main/config.json", # See all BigBird models at https://huggingface.co/models?filter=big_bird } class A_ (a_ ): UpperCAmelCase__ = '''big_bird''' def __init__( self , _A=5_0_3_5_8 , _A=7_6_8 , _A=1_2 , _A=1_2 , _A=3_0_7_2 , _A="gelu_new" , _A=0.1 , _A=0.1 , _A=4_0_9_6 , _A=2 , _A=0.02 , _A=1E-12 , _A=True , _A=0 , _A=1 , _A=2 , _A=6_6 , _A="block_sparse" , _A=True , _A=False , _A=6_4 , _A=3 , _A=None , **_A , ): '''simple docstring''' super().__init__( pad_token_id=_A , bos_token_id=_A , eos_token_id=_A , sep_token_id=_A , **_A , ) UpperCAmelCase = vocab_size UpperCAmelCase = max_position_embeddings UpperCAmelCase = hidden_size UpperCAmelCase = num_hidden_layers UpperCAmelCase = num_attention_heads UpperCAmelCase = intermediate_size UpperCAmelCase = hidden_act UpperCAmelCase = hidden_dropout_prob UpperCAmelCase = attention_probs_dropout_prob UpperCAmelCase = initializer_range UpperCAmelCase = type_vocab_size UpperCAmelCase = layer_norm_eps UpperCAmelCase = use_cache UpperCAmelCase = rescale_embeddings UpperCAmelCase = attention_type UpperCAmelCase = use_bias UpperCAmelCase = block_size UpperCAmelCase = num_random_blocks UpperCAmelCase = classifier_dropout class A_ (a_ ): @property def _lowercase ( self ): '''simple docstring''' if self.task == "multiple-choice": UpperCAmelCase = {0: '''batch''', 1: '''choice''', 2: '''sequence'''} else: UpperCAmelCase = {0: '''batch''', 1: '''sequence'''} return OrderedDict( [ ('''input_ids''', dynamic_axis), ('''attention_mask''', dynamic_axis), ] )
273
0
def __lowerCamelCase ( __a :list[list[int]] , __a :int , __a :int , __a :set ) -> int: """simple docstring""" A__ , A__ = len(__a ), len(grid[0] ) if ( min(__a , __a ) < 0 or row == row_length or col == col_length or (row, col) in visit or grid[row][col] == 1 ): return 0 if row == row_length - 1 and col == col_length - 1: return 1 visit.add((row, col) ) A__ = 0 count += depth_first_search(__a , row + 1 , __a , __a ) count += depth_first_search(__a , row - 1 , __a , __a ) count += depth_first_search(__a , __a , col + 1 , __a ) count += depth_first_search(__a , __a , col - 1 , __a ) visit.remove((row, col) ) return count if __name__ == "__main__": import doctest doctest.testmod()
274
def __lowerCamelCase ( __a :int = 1_0_0_0_0_0_0 ) -> int: """simple docstring""" A__ = limit + 1 A__ = [0] * limit for first_term in range(1 , __a ): for n in range(__a , __a , __a ): A__ = first_term + n / first_term if common_difference % 4: # d must be divisble by 4 continue else: common_difference /= 4 if ( first_term > common_difference and first_term < 4 * common_difference ): # since x,y,z are positive integers frequency[n] += 1 # so z>0 and a>d ,also 4d<a A__ = sum(1 for x in frequency[1:limit] if x == 1_0 ) return count if __name__ == "__main__": print(F'''{solution() = }''')
274
1
import os import time import numpy as np import onnxruntime as ort A : List[Any] = '''1''' A : Union[str, Any] = '''0''' A : Union[str, Any] = '''1''' A : Any = ort.SessionOptions() A : Optional[int] = ort.GraphOptimizationLevel.ORT_DISABLE_ALL print('''Create inference session...''') A : str = ['''TensorrtExecutionProvider''', '''CUDAExecutionProvider'''] A : Any = ort.InferenceSession('''model.onnx''', sess_options=sess_opt, providers=execution_provider) A : Tuple = ort.RunOptions() A : str = 1_2_8 A : Union[str, Any] = 1 A : Any = np.ones((batch, sequence), dtype=np.intaa) A : List[str] = np.ones((batch, sequence), dtype=np.intaa) A : Any = np.ones((batch, sequence), dtype=np.intaa) print('''Warm up phase...''') sess.run( None, { sess.get_inputs()[0].name: input_ids, sess.get_inputs()[1].name: attention_mask, sess.get_inputs()[2].name: token_type_ids, }, run_options=run_opt, ) print('''Start inference...''') A : Any = time.time() A : str = 2_0_0_0 A : int = {} for iter in range(max_iters): A : Union[str, Any] = sess.run( None, { sess.get_inputs()[0].name: input_ids, sess.get_inputs()[1].name: attention_mask, sess.get_inputs()[2].name: token_type_ids, }, run_options=run_opt, ) print('''Average Inference Time = {:.3f} ms'''.format((time.time() - start_time) * 1_0_0_0 / max_iters))
274
class A (SCREAMING_SNAKE_CASE ): '''simple docstring''' pass class A (SCREAMING_SNAKE_CASE ): '''simple docstring''' pass class A : '''simple docstring''' def __init__( self : List[Any] ) -> str: """simple docstring""" A__ = [ [], [], [], ] def a_ ( self : Dict , __lowerCAmelCase : int , __lowerCAmelCase : int ) -> None: """simple docstring""" try: if len(self.queues[priority] ) >= 1_00: raise OverflowError("""Maximum queue size is 100""" ) self.queues[priority].append(__lowerCAmelCase ) except IndexError: raise ValueError("""Valid priorities are 0, 1, and 2""" ) def a_ ( self : Optional[Any] ) -> int: """simple docstring""" for queue in self.queues: if queue: return queue.pop(0 ) raise UnderFlowError("""All queues are empty""" ) def __str__( self : Tuple ) -> str: """simple docstring""" return "\n".join(f'Priority {i}: {q}' for i, q in enumerate(self.queues ) ) class A : '''simple docstring''' def __init__( self : int ) -> str: """simple docstring""" A__ = [] def a_ ( self : int , __lowerCAmelCase : int ) -> None: """simple docstring""" if len(self.queue ) == 1_00: raise OverFlowError("""Maximum queue size is 100""" ) self.queue.append(__lowerCAmelCase ) def a_ ( self : List[str] ) -> int: """simple docstring""" if not self.queue: raise UnderFlowError("""The queue is empty""" ) else: A__ = min(self.queue ) self.queue.remove(__lowerCAmelCase ) return data def __str__( self : List[Any] ) -> str: """simple docstring""" return str(self.queue ) def __lowerCamelCase ( ) -> Optional[Any]: """simple docstring""" A__ = FixedPriorityQueue() fpq.enqueue(0 , 1_0 ) fpq.enqueue(1 , 7_0 ) fpq.enqueue(0 , 1_0_0 ) fpq.enqueue(2 , 1 ) fpq.enqueue(2 , 5 ) fpq.enqueue(1 , 7 ) fpq.enqueue(2 , 4 ) fpq.enqueue(1 , 6_4 ) fpq.enqueue(0 , 1_2_8 ) print(__a ) print(fpq.dequeue() ) print(fpq.dequeue() ) print(fpq.dequeue() ) print(fpq.dequeue() ) print(fpq.dequeue() ) print(__a ) print(fpq.dequeue() ) print(fpq.dequeue() ) print(fpq.dequeue() ) print(fpq.dequeue() ) print(fpq.dequeue() ) def __lowerCamelCase ( ) -> int: """simple docstring""" A__ = ElementPriorityQueue() epq.enqueue(1_0 ) epq.enqueue(7_0 ) epq.enqueue(1_0_0 ) epq.enqueue(1 ) epq.enqueue(5 ) epq.enqueue(7 ) epq.enqueue(4 ) epq.enqueue(6_4 ) epq.enqueue(1_2_8 ) print(__a ) print(epq.dequeue() ) print(epq.dequeue() ) print(epq.dequeue() ) print(epq.dequeue() ) print(epq.dequeue() ) print(__a ) print(epq.dequeue() ) print(epq.dequeue() ) print(epq.dequeue() ) print(epq.dequeue() ) print(epq.dequeue() ) if __name__ == "__main__": fixed_priority_queue() element_priority_queue()
274
1
def __lowerCamelCase ( __a :int = 1_0_0_0 ) -> int: """simple docstring""" A__ , A__ = 1, 1 A__ = 2 while True: A__ = 0 A__ = fa + fa A__ , A__ = fa, f index += 1 for _ in str(__a ): i += 1 if i == n: break return index if __name__ == "__main__": print(solution(int(str(input()).strip())))
274
import json import os import shutil import tempfile import unittest import numpy as np import pytest from transformers import CLIPTokenizer, CLIPTokenizerFast from transformers.models.clip.tokenization_clip import VOCAB_FILES_NAMES from transformers.testing_utils import require_vision from transformers.utils import IMAGE_PROCESSOR_NAME, is_vision_available if is_vision_available(): from PIL import Image from transformers import CLIPImageProcessor, CLIPProcessor @require_vision class A (unittest.TestCase ): '''simple docstring''' def a_ ( self : Union[str, Any] ) -> Dict: """simple docstring""" A__ = tempfile.mkdtemp() # fmt: off A__ = ["""l""", """o""", """w""", """e""", """r""", """s""", """t""", """i""", """d""", """n""", """lo""", """l</w>""", """w</w>""", """r</w>""", """t</w>""", """low</w>""", """er</w>""", """lowest</w>""", """newer</w>""", """wider""", """<unk>""", """<|startoftext|>""", """<|endoftext|>"""] # fmt: on A__ = dict(zip(__lowerCAmelCase , range(len(__lowerCAmelCase ) ) ) ) A__ = ["""#version: 0.2""", """l o""", """lo w</w>""", """e r</w>""", """"""] A__ = {"""unk_token""": """<unk>"""} A__ = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES["""vocab_file"""] ) A__ = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES["""merges_file"""] ) with open(self.vocab_file , """w""" , encoding="""utf-8""" ) as fp: fp.write(json.dumps(__lowerCAmelCase ) + """\n""" ) with open(self.merges_file , """w""" , encoding="""utf-8""" ) as fp: fp.write("""\n""".join(__lowerCAmelCase ) ) A__ = { """do_resize""": True, """size""": 20, """do_center_crop""": True, """crop_size""": 18, """do_normalize""": True, """image_mean""": [0.4_8_1_4_5_4_6_6, 0.4_5_7_8_2_7_5, 0.4_0_8_2_1_0_7_3], """image_std""": [0.2_6_8_6_2_9_5_4, 0.2_6_1_3_0_2_5_8, 0.2_7_5_7_7_7_1_1], } A__ = os.path.join(self.tmpdirname , __lowerCAmelCase ) with open(self.image_processor_file , """w""" , encoding="""utf-8""" ) as fp: json.dump(__lowerCAmelCase , __lowerCAmelCase ) def a_ ( self : Tuple , **__lowerCAmelCase : Dict ) -> str: """simple docstring""" return CLIPTokenizer.from_pretrained(self.tmpdirname , **__lowerCAmelCase ) def a_ ( self : Union[str, Any] , **__lowerCAmelCase : Dict ) -> List[str]: """simple docstring""" return CLIPTokenizerFast.from_pretrained(self.tmpdirname , **__lowerCAmelCase ) def a_ ( self : List[str] , **__lowerCAmelCase : Optional[Any] ) -> Dict: """simple docstring""" return CLIPImageProcessor.from_pretrained(self.tmpdirname , **__lowerCAmelCase ) def a_ ( self : str ) -> Dict: """simple docstring""" shutil.rmtree(self.tmpdirname ) def a_ ( self : str ) -> Any: """simple docstring""" A__ = [np.random.randint(2_55 , size=(3, 30, 4_00) , dtype=np.uinta )] A__ = [Image.fromarray(np.moveaxis(__lowerCAmelCase , 0 , -1 ) ) for x in image_inputs] return image_inputs def a_ ( self : Optional[int] ) -> Tuple: """simple docstring""" A__ = self.get_tokenizer() A__ = self.get_rust_tokenizer() A__ = self.get_image_processor() A__ = CLIPProcessor(tokenizer=__lowerCAmelCase , image_processor=__lowerCAmelCase ) processor_slow.save_pretrained(self.tmpdirname ) A__ = CLIPProcessor.from_pretrained(self.tmpdirname , use_fast=__lowerCAmelCase ) A__ = CLIPProcessor(tokenizer=__lowerCAmelCase , image_processor=__lowerCAmelCase ) processor_fast.save_pretrained(self.tmpdirname ) A__ = CLIPProcessor.from_pretrained(self.tmpdirname ) self.assertEqual(processor_slow.tokenizer.get_vocab() , tokenizer_slow.get_vocab() ) self.assertEqual(processor_fast.tokenizer.get_vocab() , tokenizer_fast.get_vocab() ) self.assertEqual(tokenizer_slow.get_vocab() , tokenizer_fast.get_vocab() ) self.assertIsInstance(processor_slow.tokenizer , __lowerCAmelCase ) self.assertIsInstance(processor_fast.tokenizer , __lowerCAmelCase ) self.assertEqual(processor_slow.image_processor.to_json_string() , image_processor.to_json_string() ) self.assertEqual(processor_fast.image_processor.to_json_string() , image_processor.to_json_string() ) self.assertIsInstance(processor_slow.image_processor , __lowerCAmelCase ) self.assertIsInstance(processor_fast.image_processor , __lowerCAmelCase ) def a_ ( self : Optional[Any] ) -> Union[str, Any]: """simple docstring""" A__ = CLIPProcessor(tokenizer=self.get_tokenizer() , image_processor=self.get_image_processor() ) processor.save_pretrained(self.tmpdirname ) A__ = self.get_tokenizer(bos_token="""(BOS)""" , eos_token="""(EOS)""" ) A__ = self.get_image_processor(do_normalize=__lowerCAmelCase , padding_value=1.0 ) A__ = CLIPProcessor.from_pretrained( self.tmpdirname , bos_token="""(BOS)""" , eos_token="""(EOS)""" , do_normalize=__lowerCAmelCase , padding_value=1.0 ) self.assertEqual(processor.tokenizer.get_vocab() , tokenizer_add_kwargs.get_vocab() ) self.assertIsInstance(processor.tokenizer , __lowerCAmelCase ) self.assertEqual(processor.image_processor.to_json_string() , image_processor_add_kwargs.to_json_string() ) self.assertIsInstance(processor.image_processor , __lowerCAmelCase ) def a_ ( self : List[Any] ) -> Dict: """simple docstring""" A__ = self.get_image_processor() A__ = self.get_tokenizer() A__ = CLIPProcessor(tokenizer=__lowerCAmelCase , image_processor=__lowerCAmelCase ) A__ = self.prepare_image_inputs() A__ = image_processor(__lowerCAmelCase , return_tensors="""np""" ) A__ = processor(images=__lowerCAmelCase , return_tensors="""np""" ) for key in input_image_proc.keys(): self.assertAlmostEqual(input_image_proc[key].sum() , input_processor[key].sum() , delta=1e-2 ) def a_ ( self : Optional[Any] ) -> Any: """simple docstring""" A__ = self.get_image_processor() A__ = self.get_tokenizer() A__ = CLIPProcessor(tokenizer=__lowerCAmelCase , image_processor=__lowerCAmelCase ) A__ = """lower newer""" A__ = processor(text=__lowerCAmelCase ) A__ = tokenizer(__lowerCAmelCase ) for key in encoded_tok.keys(): self.assertListEqual(encoded_tok[key] , encoded_processor[key] ) def a_ ( self : Union[str, Any] ) -> Dict: """simple docstring""" A__ = self.get_image_processor() A__ = self.get_tokenizer() A__ = CLIPProcessor(tokenizer=__lowerCAmelCase , image_processor=__lowerCAmelCase ) A__ = """lower newer""" A__ = self.prepare_image_inputs() A__ = processor(text=__lowerCAmelCase , images=__lowerCAmelCase ) self.assertListEqual(list(inputs.keys() ) , ["""input_ids""", """attention_mask""", """pixel_values"""] ) # test if it raises when no input is passed with pytest.raises(__lowerCAmelCase ): processor() def a_ ( self : Tuple ) -> str: """simple docstring""" A__ = self.get_image_processor() A__ = self.get_tokenizer() A__ = CLIPProcessor(tokenizer=__lowerCAmelCase , image_processor=__lowerCAmelCase ) A__ = [[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]] A__ = processor.batch_decode(__lowerCAmelCase ) A__ = tokenizer.batch_decode(__lowerCAmelCase ) self.assertListEqual(__lowerCAmelCase , __lowerCAmelCase ) def a_ ( self : Optional[int] ) -> str: """simple docstring""" A__ = self.get_image_processor() A__ = self.get_tokenizer() A__ = CLIPProcessor(tokenizer=__lowerCAmelCase , image_processor=__lowerCAmelCase ) A__ = """lower newer""" A__ = self.prepare_image_inputs() A__ = processor(text=__lowerCAmelCase , images=__lowerCAmelCase ) self.assertListEqual(list(inputs.keys() ) , processor.model_input_names )
274
1
import math from collections.abc import Callable def __lowerCamelCase ( __a :Callable[[float], float] , __a :float , __a :float ) -> float: """simple docstring""" A__ = xa A__ = xa while True: if x_n == x_na or function(__a ) == function(__a ): raise ZeroDivisionError("""float division by zero, could not find root""" ) A__ = x_na - ( function(__a ) / ((function(__a ) - function(__a )) / (x_na - x_n)) ) if abs(x_na - x_na ) < 1_0**-5: return x_na A__ = x_na A__ = x_na def __lowerCamelCase ( __a :float ) -> float: """simple docstring""" return math.pow(__a , 3 ) - (2 * x) - 5 if __name__ == "__main__": print(intersection(f, 3, 3.5))
274
from multiprocessing import Lock, Pipe, Process # lock used to ensure that two processes do not access a pipe at the same time A : Dict = Lock() def __lowerCamelCase ( __a :Dict , __a :List[str] , __a :Optional[int] , __a :Optional[int] , __a :Optional[Any] , __a :Optional[int] , __a :int ) -> Dict: """simple docstring""" global process_lock # we perform n swaps since after n swaps we know we are sorted # we *could* stop early if we are sorted already, but it takes as long to # find out we are sorted as it does to sort the list with this algorithm for i in range(0 , 1_0 ): if (i + position) % 2 == 0 and r_send is not None: # send your value to your right neighbor process_lock.acquire() r_send[1].send(__a ) process_lock.release() # receive your right neighbor's value process_lock.acquire() A__ = rr_cv[0].recv() process_lock.release() # take the lower value since you are on the left A__ = min(__a , __a ) elif (i + position) % 2 != 0 and l_send is not None: # send your value to your left neighbor process_lock.acquire() l_send[1].send(__a ) process_lock.release() # receive your left neighbor's value process_lock.acquire() A__ = lr_cv[0].recv() process_lock.release() # take the higher value since you are on the right A__ = max(__a , __a ) # after all swaps are performed, send the values back to main result_pipe[1].send(__a ) def __lowerCamelCase ( __a :List[str] ) -> int: """simple docstring""" A__ = [] A__ = [] # initialize the list of pipes where the values will be retrieved for _ in arr: result_pipe.append(Pipe() ) # creates the processes # the first and last process only have one neighbor so they are made outside # of the loop A__ = Pipe() A__ = Pipe() process_array_.append( Process( target=__a , args=(0, arr[0], None, temp_rs, None, temp_rr, result_pipe[0]) , ) ) A__ = temp_rs A__ = temp_rr for i in range(1 , len(__a ) - 1 ): A__ = Pipe() A__ = Pipe() process_array_.append( Process( target=__a , args=(i, arr[i], temp_ls, temp_rs, temp_lr, temp_rr, result_pipe[i]) , ) ) A__ = temp_rs A__ = temp_rr process_array_.append( Process( target=__a , args=( len(__a ) - 1, arr[len(__a ) - 1], temp_ls, None, temp_lr, None, result_pipe[len(__a ) - 1], ) , ) ) # start the processes for p in process_array_: p.start() # wait for the processes to end and write their values to the list for p in range(0 , len(__a ) ): A__ = result_pipe[p][0].recv() process_array_[p].join() return arr def __lowerCamelCase ( ) -> str: """simple docstring""" A__ = list(range(1_0 , 0 , -1 ) ) print("""Initial List""" ) print(*__a ) A__ = odd_even_transposition(__a ) print("""Sorted List\n""" ) print(*__a ) if __name__ == "__main__": main()
274
1
import importlib.util import os import platform from argparse import ArgumentParser import huggingface_hub from .. import __version__ as version from ..utils import ( is_accelerate_available, is_flax_available, is_safetensors_available, is_tf_available, is_torch_available, ) from . import BaseTransformersCLICommand def __lowerCamelCase ( __a :List[Any] ) -> Tuple: """simple docstring""" return EnvironmentCommand() def __lowerCamelCase ( __a :Optional[int] ) -> Optional[Any]: """simple docstring""" return EnvironmentCommand(args.accelerate_config_file ) class A (SCREAMING_SNAKE_CASE ): '''simple docstring''' @staticmethod def a_ ( __lowerCAmelCase : ArgumentParser ) -> Any: """simple docstring""" A__ = parser.add_parser("""env""" ) download_parser.set_defaults(func=__lowerCAmelCase ) download_parser.add_argument( """--accelerate-config_file""" , default=__lowerCAmelCase , help="""The accelerate config file to use for the default values in the launching script.""" , ) download_parser.set_defaults(func=__lowerCAmelCase ) def __init__( self : Optional[Any] , __lowerCAmelCase : Dict , *__lowerCAmelCase : List[Any] ) -> None: """simple docstring""" A__ = accelerate_config_file def a_ ( self : Optional[int] ) -> List[Any]: """simple docstring""" A__ = """not installed""" if is_safetensors_available(): import safetensors A__ = safetensors.__version__ elif importlib.util.find_spec("""safetensors""" ) is not None: import safetensors A__ = f'{safetensors.__version__} but is ignored because of PyTorch version too old.' A__ = """not installed""" A__ = A__ = """not found""" if is_accelerate_available(): import accelerate from accelerate.commands.config import default_config_file, load_config_from_file A__ = accelerate.__version__ # Get the default from the config file. if self._accelerate_config_file is not None or os.path.isfile(__lowerCAmelCase ): A__ = load_config_from_file(self._accelerate_config_file ).to_dict() A__ = ( """\n""".join([f'\t- {prop}: {val}' for prop, val in accelerate_config.items()] ) if isinstance(__lowerCAmelCase , __lowerCAmelCase ) else f'\t{accelerate_config}' ) A__ = """not installed""" A__ = """NA""" if is_torch_available(): import torch A__ = torch.__version__ A__ = torch.cuda.is_available() A__ = """not installed""" A__ = """NA""" if is_tf_available(): import tensorflow as tf A__ = tf.__version__ try: # deprecated in v2.1 A__ = tf.test.is_gpu_available() except AttributeError: # returns list of devices, convert to bool A__ = bool(tf.config.list_physical_devices("""GPU""" ) ) A__ = """not installed""" A__ = """not installed""" A__ = """not installed""" A__ = """NA""" if is_flax_available(): import flax import jax import jaxlib A__ = flax.__version__ A__ = jax.__version__ A__ = jaxlib.__version__ A__ = jax.lib.xla_bridge.get_backend().platform A__ = { """`transformers` version""": version, """Platform""": platform.platform(), """Python version""": platform.python_version(), """Huggingface_hub version""": huggingface_hub.__version__, """Safetensors version""": f'{safetensors_version}', """Accelerate version""": f'{accelerate_version}', """Accelerate config""": f'{accelerate_config_str}', """PyTorch version (GPU?)""": f'{pt_version} ({pt_cuda_available})', """Tensorflow version (GPU?)""": f'{tf_version} ({tf_cuda_available})', """Flax version (CPU?/GPU?/TPU?)""": f'{flax_version} ({jax_backend})', """Jax version""": f'{jax_version}', """JaxLib version""": f'{jaxlib_version}', """Using GPU in script?""": """<fill in>""", """Using distributed or parallel set-up in script?""": """<fill in>""", } print("""\nCopy-and-paste the text below in your GitHub issue and FILL OUT the two last points.\n""" ) print(self.format_dict(__lowerCAmelCase ) ) return info @staticmethod def a_ ( __lowerCAmelCase : Union[str, Any] ) -> Any: """simple docstring""" return "\n".join([f'- {prop}: {val}' for prop, val in d.items()] ) + "\n"
274
import argparse from argparse import Namespace import torch from torch import nn from transformers import XGLMConfig, XGLMForCausalLM def __lowerCamelCase ( __a :Dict ) -> Any: """simple docstring""" A__ = [ """decoder.version""", """decoder.output_projection.weight""", """_float_tensor""", """decoder.embed_positions._float_tensor""", ] for k in ignore_keys: state_dict.pop(__a , __a ) def __lowerCamelCase ( __a :str ) -> Union[str, Any]: """simple docstring""" A__ , A__ = emb.weight.shape A__ = nn.Linear(__a , __a , bias=__a ) A__ = emb.weight.data return lin_layer def __lowerCamelCase ( __a :str ) -> List[str]: """simple docstring""" A__ = torch.load(__a , map_location="""cpu""" ) A__ = Namespace(**checkpoint["""cfg"""]["""model"""] ) A__ = checkpoint["""model"""] remove_ignore_keys_(__a ) A__ = state_dict["""decoder.embed_tokens.weight"""].shape[0] A__ = {key.replace("""decoder""" , """model""" ): val for key, val in state_dict.items()} A__ = 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 , ) A__ = XGLMForCausalLM(__a ) A__ = model.load_state_dict(__a , strict=__a ) print(__a ) A__ = make_linear_from_emb(model.model.embed_tokens ) return model if __name__ == "__main__": A : int = 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.''') A : str = parser.parse_args() A : str = convert_fairseq_xglm_checkpoint_from_disk(args.fairseq_path) model.save_pretrained(args.pytorch_dump_folder_path)
274
1
def __lowerCamelCase ( __a :str ) -> list: """simple docstring""" A__ = [0] * len(__a ) for i in range(1 , len(__a ) ): # use last results for better performance - dynamic programming A__ = prefix_result[i - 1] while j > 0 and input_string[i] != input_string[j]: A__ = prefix_result[j - 1] if input_string[i] == input_string[j]: j += 1 A__ = j return prefix_result def __lowerCamelCase ( __a :str ) -> int: """simple docstring""" return max(prefix_function(__a ) ) if __name__ == "__main__": import doctest doctest.testmod()
274
import unittest import numpy as np from transformers import AlbertConfig, is_flax_available from transformers.testing_utils import require_flax, slow from ...test_modeling_flax_common import FlaxModelTesterMixin, ids_tensor, random_attention_mask if is_flax_available(): import jax.numpy as jnp from transformers.models.albert.modeling_flax_albert import ( FlaxAlbertForMaskedLM, FlaxAlbertForMultipleChoice, FlaxAlbertForPreTraining, FlaxAlbertForQuestionAnswering, FlaxAlbertForSequenceClassification, FlaxAlbertForTokenClassification, FlaxAlbertModel, ) class A (unittest.TestCase ): '''simple docstring''' def __init__( self : List[str] , __lowerCAmelCase : int , __lowerCAmelCase : List[str]=13 , __lowerCAmelCase : int=7 , __lowerCAmelCase : Tuple=True , __lowerCAmelCase : int=True , __lowerCAmelCase : Dict=True , __lowerCAmelCase : Union[str, Any]=True , __lowerCAmelCase : List[Any]=99 , __lowerCAmelCase : Optional[Any]=32 , __lowerCAmelCase : Optional[Any]=5 , __lowerCAmelCase : Tuple=4 , __lowerCAmelCase : Any=37 , __lowerCAmelCase : str="gelu" , __lowerCAmelCase : Optional[Any]=0.1 , __lowerCAmelCase : Union[str, Any]=0.1 , __lowerCAmelCase : List[str]=5_12 , __lowerCAmelCase : int=16 , __lowerCAmelCase : Optional[int]=2 , __lowerCAmelCase : List[Any]=0.0_2 , __lowerCAmelCase : Tuple=4 , ) -> Dict: """simple docstring""" A__ = parent A__ = batch_size A__ = seq_length A__ = is_training A__ = use_attention_mask A__ = use_token_type_ids A__ = use_labels A__ = vocab_size A__ = hidden_size A__ = num_hidden_layers A__ = num_attention_heads A__ = intermediate_size A__ = hidden_act A__ = hidden_dropout_prob A__ = attention_probs_dropout_prob A__ = max_position_embeddings A__ = type_vocab_size A__ = type_sequence_label_size A__ = initializer_range A__ = num_choices def a_ ( self : Any ) -> str: """simple docstring""" A__ = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) A__ = None if self.use_attention_mask: A__ = random_attention_mask([self.batch_size, self.seq_length] ) A__ = None if self.use_token_type_ids: A__ = ids_tensor([self.batch_size, self.seq_length] , self.type_vocab_size ) A__ = AlbertConfig( vocab_size=self.vocab_size , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , max_position_embeddings=self.max_position_embeddings , type_vocab_size=self.type_vocab_size , is_decoder=__lowerCAmelCase , initializer_range=self.initializer_range , ) return config, input_ids, token_type_ids, attention_mask def a_ ( self : Optional[int] ) -> Optional[int]: """simple docstring""" A__ = self.prepare_config_and_inputs() A__ , A__ , A__ , A__ = config_and_inputs A__ = {"""input_ids""": input_ids, """token_type_ids""": token_type_ids, """attention_mask""": attention_mask} return config, inputs_dict @require_flax class A (SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' __lowerCamelCase : str = ( ( FlaxAlbertModel, FlaxAlbertForPreTraining, FlaxAlbertForMaskedLM, FlaxAlbertForMultipleChoice, FlaxAlbertForQuestionAnswering, FlaxAlbertForSequenceClassification, FlaxAlbertForTokenClassification, FlaxAlbertForQuestionAnswering, ) if is_flax_available() else () ) def a_ ( self : str ) -> Optional[int]: """simple docstring""" A__ = FlaxAlbertModelTester(self ) @slow def a_ ( self : int ) -> Tuple: """simple docstring""" for model_class_name in self.all_model_classes: A__ = model_class_name.from_pretrained("""albert-base-v2""" ) A__ = model(np.ones((1, 1) ) ) self.assertIsNotNone(__lowerCAmelCase ) @require_flax class A (unittest.TestCase ): '''simple docstring''' @slow def a_ ( self : Dict ) -> List[Any]: """simple docstring""" A__ = FlaxAlbertModel.from_pretrained("""albert-base-v2""" ) A__ = np.array([[0, 3_45, 2_32, 3_28, 7_40, 1_40, 16_95, 69, 60_78, 15_88, 2]] ) A__ = np.array([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]] ) A__ = model(__lowerCAmelCase , attention_mask=__lowerCAmelCase )[0] A__ = (1, 11, 7_68) self.assertEqual(output.shape , __lowerCAmelCase ) A__ = np.array( [[[-0.6_5_1_3, 1.5_0_3_5, -0.2_7_6_6], [-0.6_5_1_5, 1.5_0_4_6, -0.2_7_8_0], [-0.6_5_1_2, 1.5_0_4_9, -0.2_7_8_4]]] ) self.assertTrue(jnp.allclose(output[:, 1:4, 1:4] , __lowerCAmelCase , atol=1e-4 ) )
274
1
import shutil import tempfile import unittest from transformers import SPIECE_UNDERLINE, BatchEncoding, MBartTokenizer, MBartTokenizerFast, is_torch_available from transformers.testing_utils import ( get_tests_dir, nested_simplify, require_sentencepiece, require_tokenizers, require_torch, ) from ...test_tokenization_common import TokenizerTesterMixin A : str = get_tests_dir('''fixtures/test_sentencepiece.model''') if is_torch_available(): from transformers.models.mbart.modeling_mbart import shift_tokens_right A : Union[str, Any] = 2_5_0_0_0_4 A : List[str] = 2_5_0_0_2_0 @require_sentencepiece @require_tokenizers class A (SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' __lowerCamelCase : Dict = MBartTokenizer __lowerCamelCase : Tuple = MBartTokenizerFast __lowerCamelCase : Any = True __lowerCamelCase : Any = True def a_ ( self : Union[str, Any] ) -> Optional[Any]: """simple docstring""" super().setUp() # We have a SentencePiece fixture for testing A__ = MBartTokenizer(__lowerCAmelCase , keep_accents=__lowerCAmelCase ) tokenizer.save_pretrained(self.tmpdirname ) def a_ ( self : List[str] ) -> Dict: """simple docstring""" A__ = MBartTokenizer(__lowerCAmelCase , keep_accents=__lowerCAmelCase ) A__ = tokenizer.tokenize("""This is a test""" ) self.assertListEqual(__lowerCAmelCase , ["""▁This""", """▁is""", """▁a""", """▁t""", """est"""] ) self.assertListEqual( tokenizer.convert_tokens_to_ids(__lowerCAmelCase ) , [value + tokenizer.fairseq_offset for value in [2_85, 46, 10, 1_70, 3_82]] , ) A__ = tokenizer.tokenize("""I was born in 92000, and this is falsé.""" ) self.assertListEqual( __lowerCAmelCase , [ SPIECE_UNDERLINE + """I""", SPIECE_UNDERLINE + """was""", SPIECE_UNDERLINE + """b""", """or""", """n""", SPIECE_UNDERLINE + """in""", SPIECE_UNDERLINE + """""", """9""", """2""", """0""", """0""", """0""", """,""", SPIECE_UNDERLINE + """and""", SPIECE_UNDERLINE + """this""", SPIECE_UNDERLINE + """is""", SPIECE_UNDERLINE + """f""", """al""", """s""", """é""", """.""", ] , ) A__ = tokenizer.convert_tokens_to_ids(__lowerCAmelCase ) self.assertListEqual( __lowerCAmelCase , [ value + tokenizer.fairseq_offset for value in [8, 21, 84, 55, 24, 19, 7, 2, 6_02, 3_47, 3_47, 3_47, 3, 12, 66, 46, 72, 80, 6, 2, 4] # ^ unk: 2 + 1 = 3 unk: 2 + 1 = 3 ^ ] , ) A__ = tokenizer.convert_ids_to_tokens(__lowerCAmelCase ) self.assertListEqual( __lowerCAmelCase , [ SPIECE_UNDERLINE + """I""", SPIECE_UNDERLINE + """was""", SPIECE_UNDERLINE + """b""", """or""", """n""", SPIECE_UNDERLINE + """in""", SPIECE_UNDERLINE + """""", """<unk>""", """2""", """0""", """0""", """0""", """,""", SPIECE_UNDERLINE + """and""", SPIECE_UNDERLINE + """this""", SPIECE_UNDERLINE + """is""", SPIECE_UNDERLINE + """f""", """al""", """s""", """<unk>""", """.""", ] , ) def a_ ( self : str ) -> Any: """simple docstring""" if not self.test_slow_tokenizer: # as we don't have a slow version, we can't compare the outputs between slow and fast versions return A__ = (self.rust_tokenizer_class, """hf-internal-testing/tiny-random-mbart""", {}) for tokenizer, pretrained_name, kwargs in self.tokenizers_list: with self.subTest(f'{tokenizer.__class__.__name__} ({pretrained_name})' ): A__ = self.rust_tokenizer_class.from_pretrained(__lowerCAmelCase , **__lowerCAmelCase ) A__ = self.tokenizer_class.from_pretrained(__lowerCAmelCase , **__lowerCAmelCase ) A__ = tempfile.mkdtemp() A__ = tokenizer_r.save_pretrained(__lowerCAmelCase ) A__ = tokenizer_p.save_pretrained(__lowerCAmelCase ) # Checks it save with the same files + the tokenizer.json file for the fast one self.assertTrue(any("""tokenizer.json""" in f for f in tokenizer_r_files ) ) A__ = tuple(f for f in tokenizer_r_files if """tokenizer.json""" not in f ) self.assertSequenceEqual(__lowerCAmelCase , __lowerCAmelCase ) # Checks everything loads correctly in the same way A__ = tokenizer_r.from_pretrained(__lowerCAmelCase ) A__ = tokenizer_p.from_pretrained(__lowerCAmelCase ) # Check special tokens are set accordingly on Rust and Python for key in tokenizer_pp.special_tokens_map: self.assertTrue(hasattr(__lowerCAmelCase , __lowerCAmelCase ) ) # self.assertEqual(getattr(tokenizer_rp, key), getattr(tokenizer_pp, key)) # self.assertEqual(getattr(tokenizer_rp, key + "_id"), getattr(tokenizer_pp, key + "_id")) shutil.rmtree(__lowerCAmelCase ) # Save tokenizer rust, legacy_format=True A__ = tempfile.mkdtemp() A__ = tokenizer_r.save_pretrained(__lowerCAmelCase , legacy_format=__lowerCAmelCase ) A__ = tokenizer_p.save_pretrained(__lowerCAmelCase ) # Checks it save with the same files self.assertSequenceEqual(__lowerCAmelCase , __lowerCAmelCase ) # Checks everything loads correctly in the same way A__ = tokenizer_r.from_pretrained(__lowerCAmelCase ) A__ = tokenizer_p.from_pretrained(__lowerCAmelCase ) # Check special tokens are set accordingly on Rust and Python for key in tokenizer_pp.special_tokens_map: self.assertTrue(hasattr(__lowerCAmelCase , __lowerCAmelCase ) ) shutil.rmtree(__lowerCAmelCase ) # Save tokenizer rust, legacy_format=False A__ = tempfile.mkdtemp() A__ = tokenizer_r.save_pretrained(__lowerCAmelCase , legacy_format=__lowerCAmelCase ) A__ = tokenizer_p.save_pretrained(__lowerCAmelCase ) # Checks it saved the tokenizer.json file self.assertTrue(any("""tokenizer.json""" in f for f in tokenizer_r_files ) ) # Checks everything loads correctly in the same way A__ = tokenizer_r.from_pretrained(__lowerCAmelCase ) A__ = tokenizer_p.from_pretrained(__lowerCAmelCase ) # Check special tokens are set accordingly on Rust and Python for key in tokenizer_pp.special_tokens_map: self.assertTrue(hasattr(__lowerCAmelCase , __lowerCAmelCase ) ) shutil.rmtree(__lowerCAmelCase ) @require_torch @require_sentencepiece @require_tokenizers class A (unittest.TestCase ): '''simple docstring''' __lowerCamelCase : Dict = '''facebook/mbart-large-en-ro''' __lowerCamelCase : Dict = [ ''' UN Chief Says There Is No Military Solution in Syria''', ''' Secretary-General Ban Ki-moon says his response to Russia\'s stepped up military support for Syria is that "there is no military solution" to the nearly five-year conflict and more weapons will only worsen the violence and misery for millions of people.''', ] __lowerCamelCase : Tuple = [ '''Şeful ONU declară că nu există o soluţie militară în Siria''', '''Secretarul General Ban Ki-moon declară că răspunsul său la intensificarea sprijinului militar al Rusiei''' ''' pentru Siria este că "nu există o soluţie militară" la conflictul de aproape cinci ani şi că noi arme nu vor''' ''' face decât să înrăutăţească violenţele şi mizeria pentru milioane de oameni.''', ] __lowerCamelCase : Tuple = [8274, 127873, 25916, 7, 8622, 2071, 438, 67485, 53, 187895, 23, 51712, 2, EN_CODE] @classmethod def a_ ( cls : Any ) -> str: """simple docstring""" A__ = MBartTokenizer.from_pretrained( cls.checkpoint_name , src_lang="""en_XX""" , tgt_lang="""ro_RO""" ) A__ = 1 return cls def a_ ( self : Dict ) -> Dict: """simple docstring""" self.assertEqual(self.tokenizer.fairseq_tokens_to_ids["""ar_AR"""] , 25_00_01 ) self.assertEqual(self.tokenizer.fairseq_tokens_to_ids["""en_EN"""] , 25_00_04 ) self.assertEqual(self.tokenizer.fairseq_tokens_to_ids["""ro_RO"""] , 25_00_20 ) def a_ ( self : Optional[int] ) -> Any: """simple docstring""" A__ = self.tokenizer.batch_encode_plus(self.src_text ).input_ids[0] self.assertListEqual(self.expected_src_tokens , __lowerCAmelCase ) def a_ ( self : str ) -> Dict: """simple docstring""" self.assertIn(__lowerCAmelCase , self.tokenizer.all_special_ids ) A__ = [RO_CODE, 8_84, 90_19, 96, 9, 9_16, 8_67_92, 36, 1_87_43, 1_55_96, 5, 2] A__ = self.tokenizer.decode(__lowerCAmelCase , skip_special_tokens=__lowerCAmelCase ) A__ = self.tokenizer.decode(generated_ids[1:] , skip_special_tokens=__lowerCAmelCase ) self.assertEqual(__lowerCAmelCase , __lowerCAmelCase ) self.assertNotIn(self.tokenizer.eos_token , __lowerCAmelCase ) def a_ ( self : Any ) -> Tuple: """simple docstring""" A__ = ["""this is gunna be a long sentence """ * 20] assert isinstance(src_text[0] , __lowerCAmelCase ) A__ = 10 A__ = self.tokenizer(__lowerCAmelCase , max_length=__lowerCAmelCase , truncation=__lowerCAmelCase ).input_ids[0] self.assertEqual(ids[-2] , 2 ) self.assertEqual(ids[-1] , __lowerCAmelCase ) self.assertEqual(len(__lowerCAmelCase ) , __lowerCAmelCase ) def a_ ( self : Dict ) -> Any: """simple docstring""" self.assertListEqual(self.tokenizer.convert_tokens_to_ids(["""<mask>""", """ar_AR"""] ) , [25_00_26, 25_00_01] ) def a_ ( self : Optional[int] ) -> Union[str, Any]: """simple docstring""" A__ = tempfile.mkdtemp() A__ = self.tokenizer.fairseq_tokens_to_ids self.tokenizer.save_pretrained(__lowerCAmelCase ) A__ = MBartTokenizer.from_pretrained(__lowerCAmelCase ) self.assertDictEqual(new_tok.fairseq_tokens_to_ids , __lowerCAmelCase ) @require_torch def a_ ( self : List[Any] ) -> int: """simple docstring""" A__ = self.tokenizer(self.src_text , text_target=self.tgt_text , padding=__lowerCAmelCase , return_tensors="""pt""" ) A__ = shift_tokens_right(batch["""labels"""] , self.tokenizer.pad_token_id ) # fairseq batch: https://gist.github.com/sshleifer/cba08bc2109361a74ac3760a7e30e4f4 assert batch.input_ids[1][-2:].tolist() == [2, EN_CODE] assert batch.decoder_input_ids[1][0].tolist() == RO_CODE assert batch.decoder_input_ids[1][-1] == 2 assert batch.labels[1][-2:].tolist() == [2, RO_CODE] @require_torch def a_ ( self : List[Any] ) -> Any: """simple docstring""" A__ = self.tokenizer( self.src_text , text_target=self.tgt_text , padding=__lowerCAmelCase , truncation=__lowerCAmelCase , max_length=len(self.expected_src_tokens ) , return_tensors="""pt""" , ) A__ = shift_tokens_right(batch["""labels"""] , self.tokenizer.pad_token_id ) self.assertIsInstance(__lowerCAmelCase , __lowerCAmelCase ) self.assertEqual((2, 14) , batch.input_ids.shape ) self.assertEqual((2, 14) , batch.attention_mask.shape ) A__ = batch.input_ids.tolist()[0] self.assertListEqual(self.expected_src_tokens , __lowerCAmelCase ) self.assertEqual(2 , batch.decoder_input_ids[0, -1] ) # EOS # Test that special tokens are reset self.assertEqual(self.tokenizer.prefix_tokens , [] ) self.assertEqual(self.tokenizer.suffix_tokens , [self.tokenizer.eos_token_id, EN_CODE] ) def a_ ( self : List[Any] ) -> List[Any]: """simple docstring""" A__ = self.tokenizer(self.src_text , padding=__lowerCAmelCase , truncation=__lowerCAmelCase , max_length=3 , return_tensors="""pt""" ) A__ = self.tokenizer( text_target=self.tgt_text , padding=__lowerCAmelCase , truncation=__lowerCAmelCase , max_length=10 , return_tensors="""pt""" ) A__ = targets["""input_ids"""] A__ = shift_tokens_right(__lowerCAmelCase , self.tokenizer.pad_token_id ) self.assertEqual(batch.input_ids.shape[1] , 3 ) self.assertEqual(batch.decoder_input_ids.shape[1] , 10 ) @require_torch def a_ ( self : int ) -> Any: """simple docstring""" A__ = self.tokenizer._build_translation_inputs( """A test""" , return_tensors="""pt""" , src_lang="""en_XX""" , tgt_lang="""ar_AR""" ) self.assertEqual( nested_simplify(__lowerCAmelCase ) , { # A, test, EOS, en_XX """input_ids""": [[62, 30_34, 2, 25_00_04]], """attention_mask""": [[1, 1, 1, 1]], # ar_AR """forced_bos_token_id""": 25_00_01, } , )
274
from sklearn.metrics import fa_score import datasets A : Any = ''' The F1 score is the harmonic mean of the precision and recall. It can be computed with the equation: F1 = 2 * (precision * recall) / (precision + recall) ''' A : List[Any] = ''' Args: predictions (`list` of `int`): Predicted labels. references (`list` of `int`): Ground truth labels. 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. pos_label (`int`): The class to be considered the positive class, in the case where `average` is set to `binary`. Defaults to 1. 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\'`. - \'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. - \'micro\': Calculate metrics globally by counting the total true positives, false negatives and false positives. - \'macro\': Calculate metrics for each label, and find their unweighted mean. This does not take label imbalance into account. - \'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. - \'samples\': Calculate metrics for each instance, and find their average (only meaningful for multilabel classification). sample_weight (`list` of `float`): Sample weights Defaults to None. Returns: 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. Examples: Example 1-A simple binary example >>> f1_metric = datasets.load_metric("f1") >>> results = f1_metric.compute(references=[0, 1, 0, 1, 0], predictions=[0, 0, 1, 1, 0]) >>> print(results) {\'f1\': 0.5} Example 2-The same simple binary example as in Example 1, but with `pos_label` set to `0`. >>> f1_metric = datasets.load_metric("f1") >>> results = f1_metric.compute(references=[0, 1, 0, 1, 0], predictions=[0, 0, 1, 1, 0], pos_label=0) >>> print(round(results[\'f1\'], 2)) 0.67 Example 3-The same simple binary example as in Example 1, but with `sample_weight` included. >>> f1_metric = datasets.load_metric("f1") >>> 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]) >>> print(round(results[\'f1\'], 2)) 0.35 Example 4-A multiclass example, with different values for the `average` input. >>> predictions = [0, 2, 1, 0, 0, 1] >>> references = [0, 1, 2, 0, 1, 2] >>> results = f1_metric.compute(predictions=predictions, references=references, average="macro") >>> print(round(results[\'f1\'], 2)) 0.27 >>> results = f1_metric.compute(predictions=predictions, references=references, average="micro") >>> print(round(results[\'f1\'], 2)) 0.33 >>> results = f1_metric.compute(predictions=predictions, references=references, average="weighted") >>> print(round(results[\'f1\'], 2)) 0.27 >>> results = f1_metric.compute(predictions=predictions, references=references, average=None) >>> print(results) {\'f1\': array([0.8, 0. , 0. ])} ''' A : List[Any] = ''' @article{scikit-learn, title={Scikit-learn: Machine Learning in {P}ython}, author={Pedregosa, F. and Varoquaux, G. and Gramfort, A. and Michel, V. and Thirion, B. and Grisel, O. and Blondel, M. and Prettenhofer, P. and Weiss, R. and Dubourg, V. and Vanderplas, J. and Passos, A. and Cournapeau, D. and Brucher, M. and Perrot, M. and Duchesnay, E.}, journal={Journal of Machine Learning Research}, volume={12}, pages={2825--2830}, year={2011} } ''' @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class A (datasets.Metric ): '''simple docstring''' def a_ ( self : Optional[int] ) -> List[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 a_ ( self : Any , __lowerCAmelCase : Tuple , __lowerCAmelCase : Union[str, Any] , __lowerCAmelCase : Dict=None , __lowerCAmelCase : List[str]=1 , __lowerCAmelCase : Any="binary" , __lowerCAmelCase : Optional[int]=None ) -> List[Any]: """simple docstring""" A__ = fa_score( __lowerCAmelCase , __lowerCAmelCase , labels=__lowerCAmelCase , pos_label=__lowerCAmelCase , average=__lowerCAmelCase , sample_weight=__lowerCAmelCase ) return {"f1": float(__lowerCAmelCase ) if score.size == 1 else score}
274
1
import argparse import json import logging import os import sys from unittest.mock import patch from transformers.testing_utils import TestCasePlus, get_gpu_count, slow A : List[str] = [ os.path.join(os.path.dirname(__file__), dirname) for dirname in [ '''text-classification''', '''language-modeling''', '''summarization''', '''token-classification''', '''question-answering''', ] ] sys.path.extend(SRC_DIRS) if SRC_DIRS is not None: import run_clm_flax import run_flax_glue import run_flax_ner import run_mlm_flax import run_qa import run_summarization_flax import run_ta_mlm_flax logging.basicConfig(level=logging.DEBUG) A : str = logging.getLogger() def __lowerCamelCase ( ) -> Dict: """simple docstring""" A__ = argparse.ArgumentParser() parser.add_argument("""-f""" ) A__ = parser.parse_args() return args.f def __lowerCamelCase ( __a :Union[str, Any] , __a :str="eval" ) -> Any: """simple docstring""" A__ = os.path.join(__a , F'{split}_results.json' ) if os.path.exists(__a ): with open(__a , """r""" ) as f: return json.load(__a ) raise ValueError(F'can\'t find {path}' ) A : Optional[int] = logging.StreamHandler(sys.stdout) logger.addHandler(stream_handler) class A (SCREAMING_SNAKE_CASE ): '''simple docstring''' def a_ ( self : int ) -> Union[str, Any]: """simple docstring""" A__ = self.get_auto_remove_tmp_dir() A__ = f'\n run_glue.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 --eval_steps=2\n --warmup_steps=2\n --seed=42\n --max_seq_length=128\n '.split() with patch.object(__lowerCAmelCase , """argv""" , __lowerCAmelCase ): run_flax_glue.main() A__ = get_results(__lowerCAmelCase ) self.assertGreaterEqual(result["""eval_accuracy"""] , 0.7_5 ) @slow def a_ ( self : Union[str, Any] ) -> Union[str, Any]: """simple docstring""" A__ = self.get_auto_remove_tmp_dir() A__ = f'\n run_clm_flax.py\n --model_name_or_path distilgpt2\n --train_file ./tests/fixtures/sample_text.txt\n --validation_file ./tests/fixtures/sample_text.txt\n --do_train\n --do_eval\n --block_size 128\n --per_device_train_batch_size 4\n --per_device_eval_batch_size 4\n --num_train_epochs 2\n --logging_steps 2 --eval_steps 2\n --output_dir {tmp_dir}\n --overwrite_output_dir\n '.split() with patch.object(__lowerCAmelCase , """argv""" , __lowerCAmelCase ): run_clm_flax.main() A__ = get_results(__lowerCAmelCase ) self.assertLess(result["""eval_perplexity"""] , 1_00 ) @slow def a_ ( self : Dict ) -> Dict: """simple docstring""" A__ = self.get_auto_remove_tmp_dir() A__ = f'\n run_summarization.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 --test_file tests/fixtures/tests_samples/xsum/sample.json\n --output_dir {tmp_dir}\n --overwrite_output_dir\n --num_train_epochs=3\n --warmup_steps=8\n --do_train\n --do_eval\n --do_predict\n --learning_rate=2e-4\n --per_device_train_batch_size=2\n --per_device_eval_batch_size=1\n --predict_with_generate\n '.split() with patch.object(__lowerCAmelCase , """argv""" , __lowerCAmelCase ): run_summarization_flax.main() A__ = get_results(__lowerCAmelCase , split="""test""" ) self.assertGreaterEqual(result["""test_rouge1"""] , 10 ) self.assertGreaterEqual(result["""test_rouge2"""] , 2 ) self.assertGreaterEqual(result["""test_rougeL"""] , 7 ) self.assertGreaterEqual(result["""test_rougeLsum"""] , 7 ) @slow def a_ ( self : List[str] ) -> Union[str, Any]: """simple docstring""" A__ = self.get_auto_remove_tmp_dir() A__ = f'\n run_mlm.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 --overwrite_output_dir\n --max_seq_length 128\n --per_device_train_batch_size 4\n --per_device_eval_batch_size 4\n --logging_steps 2 --eval_steps 2\n --do_train\n --do_eval\n --num_train_epochs=1\n '.split() with patch.object(__lowerCAmelCase , """argv""" , __lowerCAmelCase ): run_mlm_flax.main() A__ = get_results(__lowerCAmelCase ) self.assertLess(result["""eval_perplexity"""] , 42 ) @slow def a_ ( self : Tuple ) -> Dict: """simple docstring""" A__ = self.get_auto_remove_tmp_dir() A__ = f'\n run_t5_mlm_flax.py\n --model_name_or_path t5-small\n --train_file ./tests/fixtures/sample_text.txt\n --validation_file ./tests/fixtures/sample_text.txt\n --do_train\n --do_eval\n --max_seq_length 128\n --per_device_train_batch_size 4\n --per_device_eval_batch_size 4\n --num_train_epochs 2\n --logging_steps 2 --eval_steps 2\n --output_dir {tmp_dir}\n --overwrite_output_dir\n '.split() with patch.object(__lowerCAmelCase , """argv""" , __lowerCAmelCase ): run_ta_mlm_flax.main() A__ = get_results(__lowerCAmelCase ) self.assertGreaterEqual(result["""eval_accuracy"""] , 0.4_2 ) @slow def a_ ( self : Dict ) -> Optional[int]: """simple docstring""" A__ = 7 if get_gpu_count() > 1 else 2 A__ = self.get_auto_remove_tmp_dir() A__ = f'\n run_flax_ner.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 --overwrite_output_dir\n --do_train\n --do_eval\n --warmup_steps=2\n --learning_rate=2e-4\n --logging_steps 2 --eval_steps 2\n --per_device_train_batch_size=2\n --per_device_eval_batch_size=2\n --num_train_epochs={epochs}\n --seed 7\n '.split() with patch.object(__lowerCAmelCase , """argv""" , __lowerCAmelCase ): run_flax_ner.main() A__ = get_results(__lowerCAmelCase ) self.assertGreaterEqual(result["""eval_accuracy"""] , 0.7_5 ) self.assertGreaterEqual(result["""eval_f1"""] , 0.3 ) @slow def a_ ( self : Optional[int] ) -> Tuple: """simple docstring""" A__ = self.get_auto_remove_tmp_dir() A__ = f'\n run_qa.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 --overwrite_output_dir\n --num_train_epochs=3\n --warmup_steps=2\n --do_train\n --do_eval\n --logging_steps 2 --eval_steps 2\n --learning_rate=2e-4\n --per_device_train_batch_size=2\n --per_device_eval_batch_size=1\n '.split() with patch.object(__lowerCAmelCase , """argv""" , __lowerCAmelCase ): run_qa.main() A__ = get_results(__lowerCAmelCase ) self.assertGreaterEqual(result["""eval_f1"""] , 30 ) self.assertGreaterEqual(result["""eval_exact"""] , 30 )
274
from collections import OrderedDict from typing import Mapping from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging A : Union[str, Any] = logging.get_logger(__name__) A : int = { '''xlm-roberta-base''': '''https://huggingface.co/xlm-roberta-base/resolve/main/config.json''', '''xlm-roberta-large''': '''https://huggingface.co/xlm-roberta-large/resolve/main/config.json''', '''xlm-roberta-large-finetuned-conll02-dutch''': ( '''https://huggingface.co/xlm-roberta-large-finetuned-conll02-dutch/resolve/main/config.json''' ), '''xlm-roberta-large-finetuned-conll02-spanish''': ( '''https://huggingface.co/xlm-roberta-large-finetuned-conll02-spanish/resolve/main/config.json''' ), '''xlm-roberta-large-finetuned-conll03-english''': ( '''https://huggingface.co/xlm-roberta-large-finetuned-conll03-english/resolve/main/config.json''' ), '''xlm-roberta-large-finetuned-conll03-german''': ( '''https://huggingface.co/xlm-roberta-large-finetuned-conll03-german/resolve/main/config.json''' ), } class A (SCREAMING_SNAKE_CASE ): '''simple docstring''' __lowerCamelCase : Any = '''xlm-roberta''' def __init__( self : Optional[Any] , __lowerCAmelCase : List[Any]=3_05_22 , __lowerCAmelCase : int=7_68 , __lowerCAmelCase : Tuple=12 , __lowerCAmelCase : str=12 , __lowerCAmelCase : Union[str, Any]=30_72 , __lowerCAmelCase : Union[str, Any]="gelu" , __lowerCAmelCase : Optional[int]=0.1 , __lowerCAmelCase : Dict=0.1 , __lowerCAmelCase : List[str]=5_12 , __lowerCAmelCase : Tuple=2 , __lowerCAmelCase : Dict=0.0_2 , __lowerCAmelCase : List[str]=1e-12 , __lowerCAmelCase : Union[str, Any]=1 , __lowerCAmelCase : str=0 , __lowerCAmelCase : Optional[int]=2 , __lowerCAmelCase : Tuple="absolute" , __lowerCAmelCase : Any=True , __lowerCAmelCase : Any=None , **__lowerCAmelCase : str , ) -> Optional[Any]: """simple docstring""" super().__init__(pad_token_id=__lowerCAmelCase , bos_token_id=__lowerCAmelCase , eos_token_id=__lowerCAmelCase , **__lowerCAmelCase ) A__ = vocab_size A__ = hidden_size A__ = num_hidden_layers A__ = num_attention_heads A__ = hidden_act A__ = intermediate_size A__ = hidden_dropout_prob A__ = attention_probs_dropout_prob A__ = max_position_embeddings A__ = type_vocab_size A__ = initializer_range A__ = layer_norm_eps A__ = position_embedding_type A__ = use_cache A__ = classifier_dropout class A (SCREAMING_SNAKE_CASE ): '''simple docstring''' @property def a_ ( self : int ) -> Mapping[str, Mapping[int, str]]: """simple docstring""" if self.task == "multiple-choice": A__ = {0: """batch""", 1: """choice""", 2: """sequence"""} else: A__ = {0: """batch""", 1: """sequence"""} return OrderedDict( [ ("""input_ids""", dynamic_axis), ("""attention_mask""", dynamic_axis), ] )
274
1
import argparse import os import re import packaging.version A : Dict = '''examples/''' A : Optional[Any] = { '''examples''': (re.compile(R'''^check_min_version\("[^"]+"\)\s*$''', re.MULTILINE), '''check_min_version("VERSION")\n'''), '''init''': (re.compile(R'''^__version__\s+=\s+"([^"]+)"\s*$''', re.MULTILINE), '''__version__ = "VERSION"\n'''), '''setup''': (re.compile(R'''^(\s*)version\s*=\s*"[^"]+",''', re.MULTILINE), R'''\1version="VERSION",'''), '''doc''': (re.compile(R'''^(\s*)release\s*=\s*"[^"]+"$''', re.MULTILINE), '''release = "VERSION"\n'''), } A : List[str] = { '''init''': '''src/diffusers/__init__.py''', '''setup''': '''setup.py''', } A : List[str] = '''README.md''' def __lowerCamelCase ( __a :List[Any] , __a :str , __a :Tuple ) -> Any: """simple docstring""" with open(__a , """r""" , encoding="""utf-8""" , newline="""\n""" ) as f: A__ = f.read() A__ , A__ = REPLACE_PATTERNS[pattern] A__ = replace.replace("""VERSION""" , __a ) A__ = re_pattern.sub(__a , __a ) with open(__a , """w""" , encoding="""utf-8""" , newline="""\n""" ) as f: f.write(__a ) def __lowerCamelCase ( __a :int ) -> Union[str, Any]: """simple docstring""" for folder, directories, fnames in os.walk(__a ): # Removing some of the folders with non-actively maintained examples from the walk if "research_projects" in directories: directories.remove("""research_projects""" ) if "legacy" in directories: directories.remove("""legacy""" ) for fname in fnames: if fname.endswith(""".py""" ): update_version_in_file(os.path.join(__a , __a ) , __a , pattern="""examples""" ) def __lowerCamelCase ( __a :int , __a :Union[str, Any]=False ) -> Tuple: """simple docstring""" for pattern, fname in REPLACE_FILES.items(): update_version_in_file(__a , __a , __a ) if not patch: update_version_in_examples(__a ) def __lowerCamelCase ( ) -> Optional[Any]: """simple docstring""" A__ = """🤗 Transformers currently provides the following architectures""" A__ = """1. Want to contribute a new model?""" with open(__a , """r""" , encoding="""utf-8""" , newline="""\n""" ) as f: A__ = f.readlines() # Find the start of the list. A__ = 0 while not lines[start_index].startswith(_start_prompt ): start_index += 1 start_index += 1 A__ = start_index # Update the lines in the model list. while not lines[index].startswith(_end_prompt ): if lines[index].startswith("""1.""" ): A__ = lines[index].replace( """https://huggingface.co/docs/diffusers/main/model_doc""" , """https://huggingface.co/docs/diffusers/model_doc""" , ) index += 1 with open(__a , """w""" , encoding="""utf-8""" , newline="""\n""" ) as f: f.writelines(__a ) def __lowerCamelCase ( ) -> str: """simple docstring""" with open(REPLACE_FILES["""init"""] , """r""" ) as f: A__ = f.read() A__ = REPLACE_PATTERNS["""init"""][0].search(__a ).groups()[0] return packaging.version.parse(__a ) def __lowerCamelCase ( __a :Any=False ) -> Union[str, Any]: """simple docstring""" A__ = get_version() if patch and default_version.is_devrelease: raise ValueError("""Can't create a patch version from the dev branch, checkout a released version!""" ) if default_version.is_devrelease: A__ = default_version.base_version elif patch: A__ = F'{default_version.major}.{default_version.minor}.{default_version.micro + 1}' else: A__ = F'{default_version.major}.{default_version.minor + 1}.0' # Now let's ask nicely if that's the right one. A__ = input(F'Which version are you releasing? [{default_version}]' ) if len(__a ) == 0: A__ = default_version print(F'Updating version to {version}.' ) global_version_update(__a , patch=__a ) def __lowerCamelCase ( ) -> List[Any]: """simple docstring""" A__ = get_version() A__ = F'{current_version.major}.{current_version.minor + 1}.0.dev0' A__ = current_version.base_version # Check with the user we got that right. A__ = input(F'Which version are we developing now? [{dev_version}]' ) if len(__a ) == 0: A__ = dev_version print(F'Updating version to {version}.' ) global_version_update(__a ) # print("Cleaning main README, don't forget to run `make fix-copies`.") # clean_main_ref_in_model_list() if __name__ == "__main__": A : List[Any] = argparse.ArgumentParser() parser.add_argument('''--post_release''', action='''store_true''', help='''Whether this is pre or post release.''') parser.add_argument('''--patch''', action='''store_true''', help='''Whether or not this is a patch release.''') A : Union[str, Any] = parser.parse_args() if not args.post_release: pre_release_work(patch=args.patch) elif args.patch: print('''Nothing to do after a patch :-)''') else: post_release_work()
274
from __future__ import annotations import time from math import sqrt # 1 for manhattan, 0 for euclidean A : str = 0 A : Any = [ [0, 0, 0, 0, 0, 0, 0], [0, 1, 0, 0, 0, 0, 0], # 0 are free path whereas 1's are obstacles [0, 0, 0, 0, 0, 0, 0], [0, 0, 1, 0, 0, 0, 0], [1, 0, 1, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 1, 0, 0], ] A : Dict = [[-1, 0], [0, -1], [1, 0], [0, 1]] # up, left, down, right A : Union[str, Any] = tuple[int, int] class A : '''simple docstring''' def __init__( self : int , __lowerCAmelCase : int , __lowerCAmelCase : int , __lowerCAmelCase : int , __lowerCAmelCase : int , __lowerCAmelCase : int , __lowerCAmelCase : Node | None , ) -> None: """simple docstring""" A__ = pos_x A__ = pos_y A__ = (pos_y, pos_x) A__ = goal_x A__ = goal_y A__ = g_cost A__ = parent A__ = self.calculate_heuristic() A__ = self.g_cost + self.h_cost def a_ ( self : Dict ) -> float: """simple docstring""" A__ = self.pos_x - self.goal_x A__ = self.pos_y - self.goal_y if HEURISTIC == 1: return abs(__lowerCAmelCase ) + abs(__lowerCAmelCase ) else: return sqrt(dy**2 + dx**2 ) def __lt__( self : int , __lowerCAmelCase : Node ) -> bool: """simple docstring""" return self.f_cost < other.f_cost class A : '''simple docstring''' def __init__( self : Union[str, Any] , __lowerCAmelCase : TPosition , __lowerCAmelCase : TPosition ) -> Tuple: """simple docstring""" A__ = Node(start[1] , start[0] , goal[1] , goal[0] , 0 , __lowerCAmelCase ) A__ = Node(goal[1] , goal[0] , goal[1] , goal[0] , 9_99_99 , __lowerCAmelCase ) A__ = [self.start] A__ = [] A__ = False def a_ ( self : List[str] ) -> list[TPosition]: """simple docstring""" while self.open_nodes: # Open Nodes are sorted using __lt__ self.open_nodes.sort() A__ = self.open_nodes.pop(0 ) if current_node.pos == self.target.pos: return self.retrace_path(__lowerCAmelCase ) self.closed_nodes.append(__lowerCAmelCase ) A__ = self.get_successors(__lowerCAmelCase ) for child_node in successors: if child_node in self.closed_nodes: continue if child_node not in self.open_nodes: self.open_nodes.append(__lowerCAmelCase ) else: # retrieve the best current path A__ = self.open_nodes.pop(self.open_nodes.index(__lowerCAmelCase ) ) if child_node.g_cost < better_node.g_cost: self.open_nodes.append(__lowerCAmelCase ) else: self.open_nodes.append(__lowerCAmelCase ) return [self.start.pos] def a_ ( self : Optional[Any] , __lowerCAmelCase : Node ) -> list[Node]: """simple docstring""" A__ = [] for action in delta: A__ = parent.pos_x + action[1] A__ = parent.pos_y + action[0] if not (0 <= pos_x <= len(grid[0] ) - 1 and 0 <= pos_y <= len(__lowerCAmelCase ) - 1): continue if grid[pos_y][pos_x] != 0: continue successors.append( Node( __lowerCAmelCase , __lowerCAmelCase , self.target.pos_y , self.target.pos_x , parent.g_cost + 1 , __lowerCAmelCase , ) ) return successors def a_ ( self : List[Any] , __lowerCAmelCase : Node | None ) -> list[TPosition]: """simple docstring""" A__ = node A__ = [] while current_node is not None: path.append((current_node.pos_y, current_node.pos_x) ) A__ = current_node.parent path.reverse() return path class A : '''simple docstring''' def __init__( self : Optional[Any] , __lowerCAmelCase : TPosition , __lowerCAmelCase : TPosition ) -> None: """simple docstring""" A__ = AStar(__lowerCAmelCase , __lowerCAmelCase ) A__ = AStar(__lowerCAmelCase , __lowerCAmelCase ) A__ = False def a_ ( self : int ) -> list[TPosition]: """simple docstring""" while self.fwd_astar.open_nodes or self.bwd_astar.open_nodes: self.fwd_astar.open_nodes.sort() self.bwd_astar.open_nodes.sort() A__ = self.fwd_astar.open_nodes.pop(0 ) A__ = self.bwd_astar.open_nodes.pop(0 ) if current_bwd_node.pos == current_fwd_node.pos: return self.retrace_bidirectional_path( __lowerCAmelCase , __lowerCAmelCase ) self.fwd_astar.closed_nodes.append(__lowerCAmelCase ) self.bwd_astar.closed_nodes.append(__lowerCAmelCase ) A__ = current_bwd_node A__ = current_fwd_node A__ = { self.fwd_astar: self.fwd_astar.get_successors(__lowerCAmelCase ), self.bwd_astar: self.bwd_astar.get_successors(__lowerCAmelCase ), } for astar in [self.fwd_astar, self.bwd_astar]: for child_node in successors[astar]: if child_node in astar.closed_nodes: continue if child_node not in astar.open_nodes: astar.open_nodes.append(__lowerCAmelCase ) else: # retrieve the best current path A__ = astar.open_nodes.pop( astar.open_nodes.index(__lowerCAmelCase ) ) if child_node.g_cost < better_node.g_cost: astar.open_nodes.append(__lowerCAmelCase ) else: astar.open_nodes.append(__lowerCAmelCase ) return [self.fwd_astar.start.pos] def a_ ( self : List[str] , __lowerCAmelCase : Node , __lowerCAmelCase : Node ) -> list[TPosition]: """simple docstring""" A__ = self.fwd_astar.retrace_path(__lowerCAmelCase ) A__ = self.bwd_astar.retrace_path(__lowerCAmelCase ) bwd_path.pop() bwd_path.reverse() A__ = fwd_path + bwd_path return path if __name__ == "__main__": # all coordinates are given in format [y,x] A : Optional[int] = (0, 0) A : int = (len(grid) - 1, len(grid[0]) - 1) for elem in grid: print(elem) A : Dict = time.time() A : Optional[Any] = AStar(init, goal) A : Optional[int] = a_star.search() A : Optional[int] = time.time() - start_time print(F'''AStar execution time = {end_time:f} seconds''') A : Dict = time.time() A : Tuple = BidirectionalAStar(init, goal) A : List[Any] = time.time() - bd_start_time print(F'''BidirectionalAStar execution time = {bd_end_time:f} seconds''')
274
1
import math import unittest from transformers import BioGptConfig, 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 ( BioGptForCausalLM, BioGptForSequenceClassification, BioGptForTokenClassification, BioGptModel, BioGptTokenizer, ) from transformers.models.biogpt.modeling_biogpt import BIOGPT_PRETRAINED_MODEL_ARCHIVE_LIST class A : '''simple docstring''' def __init__( self : List[Any] , __lowerCAmelCase : Union[str, Any] , __lowerCAmelCase : Union[str, Any]=13 , __lowerCAmelCase : Any=7 , __lowerCAmelCase : Optional[int]=True , __lowerCAmelCase : int=True , __lowerCAmelCase : Union[str, Any]=False , __lowerCAmelCase : Dict=True , __lowerCAmelCase : Dict=99 , __lowerCAmelCase : List[Any]=32 , __lowerCAmelCase : Tuple=5 , __lowerCAmelCase : Tuple=4 , __lowerCAmelCase : str=37 , __lowerCAmelCase : Dict="gelu" , __lowerCAmelCase : str=0.1 , __lowerCAmelCase : Any=0.1 , __lowerCAmelCase : Any=5_12 , __lowerCAmelCase : int=16 , __lowerCAmelCase : str=2 , __lowerCAmelCase : int=0.0_2 , __lowerCAmelCase : List[str]=3 , __lowerCAmelCase : Union[str, Any]=4 , __lowerCAmelCase : Any=None , ) -> Optional[Any]: """simple docstring""" A__ = parent A__ = batch_size A__ = seq_length A__ = is_training A__ = use_input_mask A__ = use_token_type_ids A__ = use_labels A__ = vocab_size A__ = hidden_size A__ = num_hidden_layers A__ = num_attention_heads A__ = intermediate_size A__ = hidden_act A__ = hidden_dropout_prob A__ = attention_probs_dropout_prob A__ = max_position_embeddings A__ = type_vocab_size A__ = type_sequence_label_size A__ = initializer_range A__ = num_labels A__ = num_choices A__ = scope def a_ ( self : List[str] ) -> List[Any]: """simple docstring""" A__ = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) A__ = None if self.use_input_mask: A__ = random_attention_mask([self.batch_size, self.seq_length] ) A__ = None if self.use_token_type_ids: A__ = ids_tensor([self.batch_size, self.seq_length] , self.type_vocab_size ) A__ = None A__ = None A__ = None if self.use_labels: A__ = ids_tensor([self.batch_size] , self.type_sequence_label_size ) A__ = ids_tensor([self.batch_size, self.seq_length] , self.num_labels ) A__ = ids_tensor([self.batch_size] , self.num_choices ) A__ = self.get_config() return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels def a_ ( self : Optional[int] ) -> Optional[Any]: """simple docstring""" return BioGptConfig( 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 , ) def a_ ( self : int , __lowerCAmelCase : Union[str, Any] , __lowerCAmelCase : Union[str, Any] , __lowerCAmelCase : Optional[Any] , __lowerCAmelCase : Union[str, Any] , __lowerCAmelCase : Union[str, Any] , __lowerCAmelCase : Optional[Any] , __lowerCAmelCase : List[str] ) -> Dict: """simple docstring""" A__ = BioGptModel(config=__lowerCAmelCase ) model.to(__lowerCAmelCase ) model.eval() A__ = model(__lowerCAmelCase , attention_mask=__lowerCAmelCase ) A__ = model(__lowerCAmelCase ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def a_ ( self : Tuple , __lowerCAmelCase : Dict , __lowerCAmelCase : Dict , __lowerCAmelCase : Dict , __lowerCAmelCase : Union[str, Any] , __lowerCAmelCase : Tuple , __lowerCAmelCase : int , __lowerCAmelCase : List[str] , __lowerCAmelCase : Union[str, Any] , __lowerCAmelCase : List[Any] , ) -> Union[str, Any]: """simple docstring""" A__ = BioGptForCausalLM(config=__lowerCAmelCase ) model.to(__lowerCAmelCase ) model.eval() A__ = 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 a_ ( self : List[Any] , __lowerCAmelCase : Optional[Any] , __lowerCAmelCase : Union[str, Any] , __lowerCAmelCase : int , __lowerCAmelCase : Tuple , __lowerCAmelCase : Optional[int] , *__lowerCAmelCase : str ) -> List[Any]: """simple docstring""" A__ = BioGptModel(config=__lowerCAmelCase ) model.to(__lowerCAmelCase ) model.eval() # create attention mask A__ = torch.ones(input_ids.shape , dtype=torch.long , device=__lowerCAmelCase ) A__ = self.seq_length // 2 A__ = 0 # first forward pass A__ , A__ = model(__lowerCAmelCase , attention_mask=__lowerCAmelCase ).to_tuple() # create hypothetical next token and extent to next_input_ids A__ = ids_tensor((self.batch_size, 1) , config.vocab_size ) # change a random masked slice from input_ids A__ = ids_tensor((1,) , __lowerCAmelCase ).item() + 1 A__ = ids_tensor((self.batch_size, 1) , config.vocab_size ).squeeze(-1 ) A__ = random_other_next_tokens # append to next input_ids and attn_mask A__ = torch.cat([input_ids, next_tokens] , dim=-1 ) A__ = torch.cat( [attn_mask, torch.ones((attn_mask.shape[0], 1) , dtype=torch.long , device=__lowerCAmelCase )] , dim=1 , ) # get two different outputs A__ = model(__lowerCAmelCase , attention_mask=__lowerCAmelCase )["""last_hidden_state"""] A__ = model(__lowerCAmelCase , past_key_values=__lowerCAmelCase , attention_mask=__lowerCAmelCase )["""last_hidden_state"""] # select random slice A__ = ids_tensor((1,) , output_from_past.shape[-1] ).item() A__ = output_from_no_past[:, -1, random_slice_idx].detach() A__ = output_from_past[:, 0, random_slice_idx].detach() # test that outputs are equal for slice self.parent.assertTrue(torch.allclose(__lowerCAmelCase , __lowerCAmelCase , atol=1e-3 ) ) def a_ ( self : int , __lowerCAmelCase : Union[str, Any] , __lowerCAmelCase : str , __lowerCAmelCase : List[Any] , __lowerCAmelCase : Optional[Any] , __lowerCAmelCase : Optional[Any] , *__lowerCAmelCase : Any ) -> List[Any]: """simple docstring""" A__ = BioGptModel(config=__lowerCAmelCase ).to(__lowerCAmelCase ).eval() A__ = torch.ones(input_ids.shape , dtype=torch.long , device=__lowerCAmelCase ) # first forward pass A__ = model(__lowerCAmelCase , attention_mask=__lowerCAmelCase , use_cache=__lowerCAmelCase ) A__ , A__ = outputs.to_tuple() # create hypothetical multiple next token and extent to next_input_ids A__ = ids_tensor((self.batch_size, 3) , config.vocab_size ) A__ = ids_tensor((self.batch_size, 3) , 2 ) # append to next input_ids and A__ = torch.cat([input_ids, next_tokens] , dim=-1 ) A__ = torch.cat([attention_mask, next_attn_mask] , dim=-1 ) A__ = model(__lowerCAmelCase , attention_mask=__lowerCAmelCase )["""last_hidden_state"""] A__ = model(__lowerCAmelCase , attention_mask=__lowerCAmelCase , past_key_values=__lowerCAmelCase )[ """last_hidden_state""" ] # select random slice A__ = ids_tensor((1,) , output_from_past.shape[-1] ).item() A__ = output_from_no_past[:, -3:, random_slice_idx].detach() A__ = 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 a_ ( self : int , __lowerCAmelCase : List[Any] , __lowerCAmelCase : str , __lowerCAmelCase : Optional[Any] , __lowerCAmelCase : Optional[Any] , __lowerCAmelCase : Optional[Any] , *__lowerCAmelCase : List[Any] , __lowerCAmelCase : List[Any]=False ) -> Dict: """simple docstring""" A__ = BioGptForCausalLM(__lowerCAmelCase ) model.to(__lowerCAmelCase ) if gradient_checkpointing: model.gradient_checkpointing_enable() A__ = model(__lowerCAmelCase , labels=__lowerCAmelCase ) self.parent.assertEqual(result.loss.shape , () ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.vocab_size) ) result.loss.backward() def a_ ( self : Optional[Any] , __lowerCAmelCase : Union[str, Any] , *__lowerCAmelCase : str ) -> str: """simple docstring""" A__ = BioGptModel(__lowerCAmelCase ) A__ = model.config.initializer_range / math.sqrt(2 * model.config.num_hidden_layers ) for key in model.state_dict().keys(): if "c_proj" in key and "weight" in key: self.parent.assertLessEqual(abs(torch.std(model.state_dict()[key] ) - model_std ) , 0.0_0_1 ) self.parent.assertLessEqual(abs(torch.mean(model.state_dict()[key] ) - 0.0 ) , 0.0_1 ) def a_ ( self : str , __lowerCAmelCase : Any , __lowerCAmelCase : Optional[Any] , __lowerCAmelCase : Optional[Any] , __lowerCAmelCase : List[str] , __lowerCAmelCase : Optional[Any] , *__lowerCAmelCase : Union[str, Any] ) -> Dict: """simple docstring""" A__ = self.num_labels A__ = BioGptForTokenClassification(__lowerCAmelCase ) model.to(__lowerCAmelCase ) model.eval() A__ = model(__lowerCAmelCase , attention_mask=__lowerCAmelCase , token_type_ids=__lowerCAmelCase ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.num_labels) ) def a_ ( self : Any ) -> Any: """simple docstring""" A__ = self.prepare_config_and_inputs() ( ( A__ ) , ( A__ ) , ( A__ ) , ( A__ ) , ( A__ ) , ( A__ ) , ( A__ ) , ) = config_and_inputs A__ = {"""input_ids""": input_ids, """attention_mask""": input_mask} return config, inputs_dict @require_torch class A (SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' __lowerCamelCase : Optional[int] = ( (BioGptModel, BioGptForCausalLM, BioGptForSequenceClassification, BioGptForTokenClassification) if is_torch_available() else () ) __lowerCamelCase : Dict = (BioGptForCausalLM,) if is_torch_available() else () __lowerCamelCase : List[str] = ( { '''feature-extraction''': BioGptModel, '''text-classification''': BioGptForSequenceClassification, '''text-generation''': BioGptForCausalLM, '''token-classification''': BioGptForTokenClassification, '''zero-shot''': BioGptForSequenceClassification, } if is_torch_available() else {} ) __lowerCamelCase : List[Any] = False def a_ ( self : int ) -> Union[str, Any]: """simple docstring""" A__ = BioGptModelTester(self ) A__ = ConfigTester(self , config_class=__lowerCAmelCase , hidden_size=37 ) def a_ ( self : List[str] ) -> int: """simple docstring""" self.config_tester.run_common_tests() def a_ ( self : Tuple ) -> Optional[Any]: """simple docstring""" A__ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*__lowerCAmelCase ) def a_ ( self : List[Any] ) -> List[str]: """simple docstring""" A__ = self.model_tester.prepare_config_and_inputs() for type in ["absolute", "relative_key", "relative_key_query"]: A__ = type self.model_tester.create_and_check_model(*__lowerCAmelCase ) def a_ ( self : int ) -> Tuple: """simple docstring""" A__ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_biogpt_model_attention_mask_past(*__lowerCAmelCase ) def a_ ( self : int ) -> List[str]: """simple docstring""" A__ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_forward_and_backwards(*__lowerCAmelCase , gradient_checkpointing=__lowerCAmelCase ) def a_ ( self : List[Any] ) -> Dict: """simple docstring""" A__ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_biogpt_model_past_large_inputs(*__lowerCAmelCase ) def a_ ( self : Union[str, Any] ) -> Optional[int]: """simple docstring""" A__ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_biogpt_weight_initialization(*__lowerCAmelCase ) def a_ ( self : Tuple ) -> Optional[Any]: """simple docstring""" A__ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_biogpt_for_token_classification(*__lowerCAmelCase ) @slow def a_ ( self : List[Any] ) -> Union[str, Any]: """simple docstring""" A__ = BioGptForCausalLM.from_pretrained("""microsoft/biogpt""" ) model.to(__lowerCAmelCase ) A__ = BioGptTokenizer.from_pretrained("""microsoft/biogpt""" ) A__ = """left""" # Define PAD Token = EOS Token = 50256 A__ = tokenizer.eos_token A__ = model.config.eos_token_id # use different length sentences to test batching A__ = [ """Hello, my dog is a little""", """Today, I""", ] A__ = tokenizer(__lowerCAmelCase , return_tensors="""pt""" , padding=__lowerCAmelCase ) A__ = inputs["""input_ids"""].to(__lowerCAmelCase ) A__ = model.generate( input_ids=__lowerCAmelCase , attention_mask=inputs["""attention_mask"""].to(__lowerCAmelCase ) , ) A__ = tokenizer(sentences[0] , return_tensors="""pt""" ).input_ids.to(__lowerCAmelCase ) A__ = model.generate(input_ids=__lowerCAmelCase ) A__ = inputs_non_padded.shape[-1] - inputs["""attention_mask"""][-1].long().sum().cpu().item() A__ = tokenizer(sentences[1] , return_tensors="""pt""" ).input_ids.to(__lowerCAmelCase ) A__ = model.generate(input_ids=__lowerCAmelCase , max_length=model.config.max_length - num_paddings ) A__ = tokenizer.batch_decode(__lowerCAmelCase , skip_special_tokens=__lowerCAmelCase ) A__ = tokenizer.decode(output_non_padded[0] , skip_special_tokens=__lowerCAmelCase ) A__ = tokenizer.decode(output_padded[0] , skip_special_tokens=__lowerCAmelCase ) A__ = [ """Hello, my dog is a little bit bigger than a little bit.""", """Today, I have a good idea of how to use the information""", ] self.assertListEqual(__lowerCAmelCase , __lowerCAmelCase ) self.assertListEqual(__lowerCAmelCase , [non_padded_sentence, padded_sentence] ) @slow def a_ ( self : Union[str, Any] ) -> Optional[Any]: """simple docstring""" for model_name in BIOGPT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: A__ = BioGptModel.from_pretrained(__lowerCAmelCase ) self.assertIsNotNone(__lowerCAmelCase ) def a_ ( self : int ) -> Any: """simple docstring""" A__ , A__ = self.model_tester.prepare_config_and_inputs_for_common() A__ = 3 A__ = input_dict["""input_ids"""] A__ = input_ids.ne(1 ).to(__lowerCAmelCase ) A__ = ids_tensor([self.model_tester.batch_size] , self.model_tester.type_sequence_label_size ) A__ = BioGptForSequenceClassification(__lowerCAmelCase ) model.to(__lowerCAmelCase ) model.eval() A__ = model(__lowerCAmelCase , attention_mask=__lowerCAmelCase , labels=__lowerCAmelCase ) self.assertEqual(result.logits.shape , (self.model_tester.batch_size, self.model_tester.num_labels) ) def a_ ( self : Optional[Any] ) -> Optional[int]: """simple docstring""" A__ , A__ = self.model_tester.prepare_config_and_inputs_for_common() A__ = 3 A__ = """multi_label_classification""" A__ = input_dict["""input_ids"""] A__ = input_ids.ne(1 ).to(__lowerCAmelCase ) A__ = ids_tensor( [self.model_tester.batch_size, config.num_labels] , self.model_tester.type_sequence_label_size ).to(torch.float ) A__ = BioGptForSequenceClassification(__lowerCAmelCase ) model.to(__lowerCAmelCase ) model.eval() A__ = model(__lowerCAmelCase , attention_mask=__lowerCAmelCase , labels=__lowerCAmelCase ) self.assertEqual(result.logits.shape , (self.model_tester.batch_size, self.model_tester.num_labels) ) @require_torch class A (unittest.TestCase ): '''simple docstring''' @slow def a_ ( self : List[Any] ) -> Optional[int]: """simple docstring""" A__ = BioGptForCausalLM.from_pretrained("""microsoft/biogpt""" ) A__ = torch.tensor([[2, 48_05, 9, 6_56, 21]] ) A__ = model(__lowerCAmelCase )[0] A__ = 4_23_84 A__ = torch.Size((1, 5, vocab_size) ) self.assertEqual(output.shape , __lowerCAmelCase ) A__ = torch.tensor( [[[-9.5_2_3_6, -9.8_9_1_8, 1_0.4_5_5_7], [-1_1.0_4_6_9, -9.6_4_2_3, 8.1_0_2_2], [-8.8_6_6_4, -7.8_8_2_6, 5.5_3_2_5]]] ) self.assertTrue(torch.allclose(output[:, :3, :3] , __lowerCAmelCase , atol=1e-4 ) ) @slow def a_ ( self : Any ) -> int: """simple docstring""" A__ = BioGptTokenizer.from_pretrained("""microsoft/biogpt""" ) A__ = BioGptForCausalLM.from_pretrained("""microsoft/biogpt""" ) model.to(__lowerCAmelCase ) torch.manual_seed(0 ) A__ = tokenizer("""COVID-19 is""" , return_tensors="""pt""" ).to(__lowerCAmelCase ) A__ = model.generate( **__lowerCAmelCase , min_length=1_00 , max_length=10_24 , num_beams=5 , early_stopping=__lowerCAmelCase , ) A__ = tokenizer.decode(output_ids[0] , skip_special_tokens=__lowerCAmelCase ) A__ = ( """COVID-19 is a global pandemic caused by severe acute respiratory syndrome coronavirus 2 (SARS-CoV-2), the""" """ causative agent of coronavirus disease 2019 (COVID-19), which has spread to more than 200 countries and""" """ territories, including the United States (US), Canada, Australia, New Zealand, the United Kingdom (UK),""" """ and the United States of America (USA), as of March 11, 2020, with more than 800,000 confirmed cases and""" """ more than 800,000 deaths.""" ) self.assertEqual(__lowerCAmelCase , __lowerCAmelCase )
274
import unittest from transformers.utils.backbone_utils import ( BackboneMixin, get_aligned_output_features_output_indices, verify_out_features_out_indices, ) class A (unittest.TestCase ): '''simple docstring''' def a_ ( self : Any ) -> Union[str, Any]: """simple docstring""" A__ = ["""a""", """b""", """c"""] # Defaults to last layer if both are None A__ , A__ = get_aligned_output_features_output_indices(__lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase ) self.assertEqual(__lowerCAmelCase , ["""c"""] ) self.assertEqual(__lowerCAmelCase , [2] ) # Out indices set to match out features A__ , A__ = get_aligned_output_features_output_indices(["""a""", """c"""] , __lowerCAmelCase , __lowerCAmelCase ) self.assertEqual(__lowerCAmelCase , ["""a""", """c"""] ) self.assertEqual(__lowerCAmelCase , [0, 2] ) # Out features set to match out indices A__ , A__ = get_aligned_output_features_output_indices(__lowerCAmelCase , [0, 2] , __lowerCAmelCase ) self.assertEqual(__lowerCAmelCase , ["""a""", """c"""] ) self.assertEqual(__lowerCAmelCase , [0, 2] ) # Out features selected from negative indices A__ , A__ = get_aligned_output_features_output_indices(__lowerCAmelCase , [-3, -1] , __lowerCAmelCase ) self.assertEqual(__lowerCAmelCase , ["""a""", """c"""] ) self.assertEqual(__lowerCAmelCase , [-3, -1] ) def a_ ( self : Optional[Any] ) -> Optional[Any]: """simple docstring""" with self.assertRaises(__lowerCAmelCase ): verify_out_features_out_indices(["""a""", """b"""] , (0, 1) , __lowerCAmelCase ) # Out features must be a list with self.assertRaises(__lowerCAmelCase ): verify_out_features_out_indices(("""a""", """b""") , (0, 1) , ["""a""", """b"""] ) # Out features must be a subset of stage names with self.assertRaises(__lowerCAmelCase ): verify_out_features_out_indices(["""a""", """b"""] , (0, 1) , ["""a"""] ) # Out indices must be a list or tuple with self.assertRaises(__lowerCAmelCase ): verify_out_features_out_indices(__lowerCAmelCase , 0 , ["""a""", """b"""] ) # Out indices must be a subset of stage names with self.assertRaises(__lowerCAmelCase ): verify_out_features_out_indices(__lowerCAmelCase , (0, 1) , ["""a"""] ) # Out features and out indices must be the same length with self.assertRaises(__lowerCAmelCase ): verify_out_features_out_indices(["""a""", """b"""] , (0,) , ["""a""", """b""", """c"""] ) # Out features should match out indices with self.assertRaises(__lowerCAmelCase ): verify_out_features_out_indices(["""a""", """b"""] , (0, 2) , ["""a""", """b""", """c"""] ) # Out features and out indices should be in order with self.assertRaises(__lowerCAmelCase ): verify_out_features_out_indices(["""b""", """a"""] , (0, 1) , ["""a""", """b"""] ) # Check passes with valid inputs verify_out_features_out_indices(["""a""", """b""", """d"""] , (0, 1, -1) , ["""a""", """b""", """c""", """d"""] ) def a_ ( self : Union[str, Any] ) -> List[str]: """simple docstring""" A__ = BackboneMixin() A__ = ["""a""", """b""", """c"""] A__ = ["""a""", """c"""] A__ = [0, 2] # Check that the output features and indices are set correctly self.assertEqual(backbone.out_features , ["""a""", """c"""] ) self.assertEqual(backbone.out_indices , [0, 2] ) # Check out features and indices are updated correctly A__ = ["""a""", """b"""] self.assertEqual(backbone.out_features , ["""a""", """b"""] ) self.assertEqual(backbone.out_indices , [0, 1] ) A__ = [-3, -1] self.assertEqual(backbone.out_features , ["""a""", """c"""] ) self.assertEqual(backbone.out_indices , [-3, -1] )
274
1
from collections import deque class A : '''simple docstring''' def __init__( self : List[str] , __lowerCAmelCase : str , __lowerCAmelCase : int , __lowerCAmelCase : int ) -> None: """simple docstring""" A__ = process_name # process name A__ = arrival_time # arrival time of the process # completion time of finished process or last interrupted time A__ = arrival_time A__ = burst_time # remaining burst time A__ = 0 # total time of the process wait in ready queue A__ = 0 # time from arrival time to completion time class A : '''simple docstring''' def __init__( self : Tuple , __lowerCAmelCase : int , __lowerCAmelCase : list[int] , __lowerCAmelCase : deque[Process] , __lowerCAmelCase : int , ) -> None: """simple docstring""" A__ = number_of_queues # time slice of queues that round robin algorithm applied A__ = time_slices # unfinished process is in this ready_queue A__ = queue # current time A__ = current_time # finished process is in this sequence queue A__ = deque() def a_ ( self : Dict ) -> list[str]: """simple docstring""" A__ = [] for i in range(len(self.finish_queue ) ): sequence.append(self.finish_queue[i].process_name ) return sequence def a_ ( self : Tuple , __lowerCAmelCase : list[Process] ) -> list[int]: """simple docstring""" A__ = [] for i in range(len(__lowerCAmelCase ) ): waiting_times.append(queue[i].waiting_time ) return waiting_times def a_ ( self : Optional[Any] , __lowerCAmelCase : list[Process] ) -> list[int]: """simple docstring""" A__ = [] for i in range(len(__lowerCAmelCase ) ): turnaround_times.append(queue[i].turnaround_time ) return turnaround_times def a_ ( self : Dict , __lowerCAmelCase : list[Process] ) -> list[int]: """simple docstring""" A__ = [] for i in range(len(__lowerCAmelCase ) ): completion_times.append(queue[i].stop_time ) return completion_times def a_ ( self : int , __lowerCAmelCase : deque[Process] ) -> list[int]: """simple docstring""" return [q.burst_time for q in queue] def a_ ( self : Any , __lowerCAmelCase : Process ) -> int: """simple docstring""" process.waiting_time += self.current_time - process.stop_time return process.waiting_time def a_ ( self : Union[str, Any] , __lowerCAmelCase : deque[Process] ) -> deque[Process]: """simple docstring""" A__ = deque() # sequence deque of finished process while len(__lowerCAmelCase ) != 0: A__ = ready_queue.popleft() # current process # if process's arrival time is later than current time, update current time if self.current_time < cp.arrival_time: self.current_time += cp.arrival_time # update waiting time of current process self.update_waiting_time(__lowerCAmelCase ) # update current time self.current_time += cp.burst_time # finish the process and set the process's burst-time 0 A__ = 0 # set the process's turnaround time because it is finished A__ = self.current_time - cp.arrival_time # set the completion time A__ = self.current_time # add the process to queue that has finished queue finished.append(__lowerCAmelCase ) self.finish_queue.extend(__lowerCAmelCase ) # add finished process to finish queue # FCFS will finish all remaining processes return finished def a_ ( self : Optional[Any] , __lowerCAmelCase : deque[Process] , __lowerCAmelCase : int ) -> tuple[deque[Process], deque[Process]]: """simple docstring""" A__ = deque() # sequence deque of terminated process # just for 1 cycle and unfinished processes will go back to queue for _ in range(len(__lowerCAmelCase ) ): A__ = ready_queue.popleft() # current process # if process's arrival time is later than current time, update current time if self.current_time < cp.arrival_time: self.current_time += cp.arrival_time # update waiting time of unfinished processes self.update_waiting_time(__lowerCAmelCase ) # if the burst time of process is bigger than time-slice if cp.burst_time > time_slice: # use CPU for only time-slice self.current_time += time_slice # update remaining burst time cp.burst_time -= time_slice # update end point time A__ = self.current_time # locate the process behind the queue because it is not finished ready_queue.append(__lowerCAmelCase ) else: # use CPU for remaining burst time self.current_time += cp.burst_time # set burst time 0 because the process is finished A__ = 0 # set the finish time A__ = self.current_time # update the process' turnaround time because it is finished A__ = self.current_time - cp.arrival_time # add the process to queue that has finished queue finished.append(__lowerCAmelCase ) self.finish_queue.extend(__lowerCAmelCase ) # add finished process to finish queue # return finished processes queue and remaining processes queue return finished, ready_queue def a_ ( self : List[Any] ) -> deque[Process]: """simple docstring""" for i in range(self.number_of_queues - 1 ): A__ , A__ = self.round_robin( self.ready_queue , self.time_slices[i] ) # the last queue has first_come_first_served algorithm self.first_come_first_served(self.ready_queue ) return self.finish_queue if __name__ == "__main__": import doctest A : Union[str, Any] = Process('''P1''', 0, 5_3) A : Optional[Any] = Process('''P2''', 0, 1_7) A : Optional[int] = Process('''P3''', 0, 6_8) A : int = Process('''P4''', 0, 2_4) A : Any = 3 A : List[Any] = [1_7, 2_5] A : Optional[Any] = deque([Pa, Pa, Pa, Pa]) if len(time_slices) != number_of_queues - 1: raise SystemExit(0) doctest.testmod(extraglobs={'''queue''': deque([Pa, Pa, Pa, Pa])}) A : Optional[Any] = Process('''P1''', 0, 5_3) A : int = Process('''P2''', 0, 1_7) A : Optional[int] = Process('''P3''', 0, 6_8) A : Tuple = Process('''P4''', 0, 2_4) A : Union[str, Any] = 3 A : Optional[Any] = [1_7, 2_5] A : Tuple = deque([Pa, Pa, Pa, Pa]) A : Optional[int] = MLFQ(number_of_queues, time_slices, queue, 0) A : Dict = mlfq.multi_level_feedback_queue() # print total waiting times of processes(P1, P2, P3, P4) print( F'''waiting time:\ \t\t\t{MLFQ.calculate_waiting_time(mlfq, [Pa, Pa, Pa, Pa])}''' ) # print completion times of processes(P1, P2, P3, P4) print( F'''completion time:\ \t\t{MLFQ.calculate_completion_time(mlfq, [Pa, Pa, Pa, Pa])}''' ) # print total turnaround times of processes(P1, P2, P3, P4) print( F'''turnaround time:\ \t\t{MLFQ.calculate_turnaround_time(mlfq, [Pa, Pa, Pa, Pa])}''' ) # print sequence of finished processes print( F'''sequence of finished processes:\ {mlfq.calculate_sequence_of_finish_queue()}''' )
274
from collections import deque class A : '''simple docstring''' def __init__( self : List[str] , __lowerCAmelCase : str , __lowerCAmelCase : int , __lowerCAmelCase : int ) -> None: """simple docstring""" A__ = process_name # process name A__ = arrival_time # arrival time of the process # completion time of finished process or last interrupted time A__ = arrival_time A__ = burst_time # remaining burst time A__ = 0 # total time of the process wait in ready queue A__ = 0 # time from arrival time to completion time class A : '''simple docstring''' def __init__( self : Tuple , __lowerCAmelCase : int , __lowerCAmelCase : list[int] , __lowerCAmelCase : deque[Process] , __lowerCAmelCase : int , ) -> None: """simple docstring""" A__ = number_of_queues # time slice of queues that round robin algorithm applied A__ = time_slices # unfinished process is in this ready_queue A__ = queue # current time A__ = current_time # finished process is in this sequence queue A__ = deque() def a_ ( self : Dict ) -> list[str]: """simple docstring""" A__ = [] for i in range(len(self.finish_queue ) ): sequence.append(self.finish_queue[i].process_name ) return sequence def a_ ( self : Tuple , __lowerCAmelCase : list[Process] ) -> list[int]: """simple docstring""" A__ = [] for i in range(len(__lowerCAmelCase ) ): waiting_times.append(queue[i].waiting_time ) return waiting_times def a_ ( self : Optional[Any] , __lowerCAmelCase : list[Process] ) -> list[int]: """simple docstring""" A__ = [] for i in range(len(__lowerCAmelCase ) ): turnaround_times.append(queue[i].turnaround_time ) return turnaround_times def a_ ( self : Dict , __lowerCAmelCase : list[Process] ) -> list[int]: """simple docstring""" A__ = [] for i in range(len(__lowerCAmelCase ) ): completion_times.append(queue[i].stop_time ) return completion_times def a_ ( self : int , __lowerCAmelCase : deque[Process] ) -> list[int]: """simple docstring""" return [q.burst_time for q in queue] def a_ ( self : Any , __lowerCAmelCase : Process ) -> int: """simple docstring""" process.waiting_time += self.current_time - process.stop_time return process.waiting_time def a_ ( self : Union[str, Any] , __lowerCAmelCase : deque[Process] ) -> deque[Process]: """simple docstring""" A__ = deque() # sequence deque of finished process while len(__lowerCAmelCase ) != 0: A__ = ready_queue.popleft() # current process # if process's arrival time is later than current time, update current time if self.current_time < cp.arrival_time: self.current_time += cp.arrival_time # update waiting time of current process self.update_waiting_time(__lowerCAmelCase ) # update current time self.current_time += cp.burst_time # finish the process and set the process's burst-time 0 A__ = 0 # set the process's turnaround time because it is finished A__ = self.current_time - cp.arrival_time # set the completion time A__ = self.current_time # add the process to queue that has finished queue finished.append(__lowerCAmelCase ) self.finish_queue.extend(__lowerCAmelCase ) # add finished process to finish queue # FCFS will finish all remaining processes return finished def a_ ( self : Optional[Any] , __lowerCAmelCase : deque[Process] , __lowerCAmelCase : int ) -> tuple[deque[Process], deque[Process]]: """simple docstring""" A__ = deque() # sequence deque of terminated process # just for 1 cycle and unfinished processes will go back to queue for _ in range(len(__lowerCAmelCase ) ): A__ = ready_queue.popleft() # current process # if process's arrival time is later than current time, update current time if self.current_time < cp.arrival_time: self.current_time += cp.arrival_time # update waiting time of unfinished processes self.update_waiting_time(__lowerCAmelCase ) # if the burst time of process is bigger than time-slice if cp.burst_time > time_slice: # use CPU for only time-slice self.current_time += time_slice # update remaining burst time cp.burst_time -= time_slice # update end point time A__ = self.current_time # locate the process behind the queue because it is not finished ready_queue.append(__lowerCAmelCase ) else: # use CPU for remaining burst time self.current_time += cp.burst_time # set burst time 0 because the process is finished A__ = 0 # set the finish time A__ = self.current_time # update the process' turnaround time because it is finished A__ = self.current_time - cp.arrival_time # add the process to queue that has finished queue finished.append(__lowerCAmelCase ) self.finish_queue.extend(__lowerCAmelCase ) # add finished process to finish queue # return finished processes queue and remaining processes queue return finished, ready_queue def a_ ( self : List[Any] ) -> deque[Process]: """simple docstring""" for i in range(self.number_of_queues - 1 ): A__ , A__ = self.round_robin( self.ready_queue , self.time_slices[i] ) # the last queue has first_come_first_served algorithm self.first_come_first_served(self.ready_queue ) return self.finish_queue if __name__ == "__main__": import doctest A : Union[str, Any] = Process('''P1''', 0, 5_3) A : Optional[Any] = Process('''P2''', 0, 1_7) A : Optional[int] = Process('''P3''', 0, 6_8) A : int = Process('''P4''', 0, 2_4) A : Any = 3 A : List[Any] = [1_7, 2_5] A : Optional[Any] = deque([Pa, Pa, Pa, Pa]) if len(time_slices) != number_of_queues - 1: raise SystemExit(0) doctest.testmod(extraglobs={'''queue''': deque([Pa, Pa, Pa, Pa])}) A : Optional[Any] = Process('''P1''', 0, 5_3) A : int = Process('''P2''', 0, 1_7) A : Optional[int] = Process('''P3''', 0, 6_8) A : Tuple = Process('''P4''', 0, 2_4) A : Union[str, Any] = 3 A : Optional[Any] = [1_7, 2_5] A : Tuple = deque([Pa, Pa, Pa, Pa]) A : Optional[int] = MLFQ(number_of_queues, time_slices, queue, 0) A : Dict = mlfq.multi_level_feedback_queue() # print total waiting times of processes(P1, P2, P3, P4) print( F'''waiting time:\ \t\t\t{MLFQ.calculate_waiting_time(mlfq, [Pa, Pa, Pa, Pa])}''' ) # print completion times of processes(P1, P2, P3, P4) print( F'''completion time:\ \t\t{MLFQ.calculate_completion_time(mlfq, [Pa, Pa, Pa, Pa])}''' ) # print total turnaround times of processes(P1, P2, P3, P4) print( F'''turnaround time:\ \t\t{MLFQ.calculate_turnaround_time(mlfq, [Pa, Pa, Pa, Pa])}''' ) # print sequence of finished processes print( F'''sequence of finished processes:\ {mlfq.calculate_sequence_of_finish_queue()}''' )
274
1
from collections.abc import Callable import numpy as np def __lowerCamelCase ( __a :Callable , __a :float , __a :float , __a :float , __a :float ) -> np.array: """simple docstring""" A__ = int(np.ceil((x_end - xa) / step_size ) ) A__ = np.zeros((n + 1,) ) A__ = ya A__ = xa for k in range(__a ): A__ = y[k] + step_size * ode_func(__a , y[k] ) A__ = y[k] + ( (step_size / 2) * (ode_func(__a , y[k] ) + ode_func(x + step_size , __a )) ) x += step_size return y if __name__ == "__main__": import doctest doctest.testmod()
274
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 A : str = logging.get_logger(__name__) A : Union[str, Any] = { '''microsoft/layoutlmv3-base''': '''https://huggingface.co/microsoft/layoutlmv3-base/resolve/main/config.json''', } class A (SCREAMING_SNAKE_CASE ): '''simple docstring''' __lowerCamelCase : Optional[Any] = '''layoutlmv3''' def __init__( self : Tuple , __lowerCAmelCase : Optional[int]=5_02_65 , __lowerCAmelCase : Tuple=7_68 , __lowerCAmelCase : Union[str, Any]=12 , __lowerCAmelCase : Any=12 , __lowerCAmelCase : List[Any]=30_72 , __lowerCAmelCase : Optional[int]="gelu" , __lowerCAmelCase : Dict=0.1 , __lowerCAmelCase : List[Any]=0.1 , __lowerCAmelCase : Dict=5_12 , __lowerCAmelCase : Any=2 , __lowerCAmelCase : Dict=0.0_2 , __lowerCAmelCase : List[str]=1e-5 , __lowerCAmelCase : List[str]=1 , __lowerCAmelCase : int=0 , __lowerCAmelCase : Dict=2 , __lowerCAmelCase : Tuple=10_24 , __lowerCAmelCase : List[str]=1_28 , __lowerCAmelCase : Optional[int]=1_28 , __lowerCAmelCase : Any=True , __lowerCAmelCase : Optional[Any]=32 , __lowerCAmelCase : Any=1_28 , __lowerCAmelCase : str=64 , __lowerCAmelCase : Optional[int]=2_56 , __lowerCAmelCase : int=True , __lowerCAmelCase : int=True , __lowerCAmelCase : List[str]=True , __lowerCAmelCase : int=2_24 , __lowerCAmelCase : Dict=3 , __lowerCAmelCase : List[Any]=16 , __lowerCAmelCase : Dict=None , **__lowerCAmelCase : Optional[Any] , ) -> Dict: """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 , ) A__ = max_ad_position_embeddings A__ = coordinate_size A__ = shape_size A__ = has_relative_attention_bias A__ = rel_pos_bins A__ = max_rel_pos A__ = has_spatial_attention_bias A__ = rel_ad_pos_bins A__ = max_rel_ad_pos A__ = text_embed A__ = visual_embed A__ = input_size A__ = num_channels A__ = patch_size A__ = classifier_dropout class A (SCREAMING_SNAKE_CASE ): '''simple docstring''' __lowerCamelCase : List[str] = version.parse('''1.12''' ) @property def a_ ( self : int ) -> 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 a_ ( self : Optional[int] ) -> float: """simple docstring""" return 1e-5 @property def a_ ( self : Tuple ) -> int: """simple docstring""" return 12 def a_ ( self : str , __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 A__ = 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 A__ = processor.tokenizer.num_special_tokens_to_add(__lowerCAmelCase ) A__ = 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 A__ = [[""" """.join([processor.tokenizer.unk_token] ) * seq_length]] * batch_size # Generate dummy bounding boxes A__ = [[[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) A__ = self._generate_dummy_images(__lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase ) A__ = dict( processor( __lowerCAmelCase , text=__lowerCAmelCase , boxes=__lowerCAmelCase , return_tensors=__lowerCAmelCase , ) ) return inputs
274
1
import itertools from dataclasses import dataclass from typing import Optional import pandas as pd import pyarrow as pa import datasets from datasets.table import table_cast @dataclass class A (datasets.BuilderConfig ): '''simple docstring''' __lowerCamelCase : Optional[datasets.Features] = None class A (datasets.ArrowBasedBuilder ): '''simple docstring''' __lowerCamelCase : Union[str, Any] = PandasConfig def a_ ( self : int ) -> str: """simple docstring""" return datasets.DatasetInfo(features=self.config.features ) def a_ ( self : Union[str, Any] , __lowerCAmelCase : Optional[int] ) -> Dict: """simple docstring""" if not self.config.data_files: raise ValueError(f'At least one data file must be specified, but got data_files={self.config.data_files}' ) A__ = dl_manager.download_and_extract(self.config.data_files ) if isinstance(__lowerCAmelCase , (str, list, tuple) ): A__ = data_files if isinstance(__lowerCAmelCase , __lowerCAmelCase ): A__ = [files] # Use `dl_manager.iter_files` to skip hidden files in an extracted archive A__ = [dl_manager.iter_files(__lowerCAmelCase ) for file in files] return [datasets.SplitGenerator(name=datasets.Split.TRAIN , gen_kwargs={"""files""": files} )] A__ = [] for split_name, files in data_files.items(): if isinstance(__lowerCAmelCase , __lowerCAmelCase ): A__ = [files] # Use `dl_manager.iter_files` to skip hidden files in an extracted archive A__ = [dl_manager.iter_files(__lowerCAmelCase ) for file in files] splits.append(datasets.SplitGenerator(name=__lowerCAmelCase , gen_kwargs={"""files""": files} ) ) return splits def a_ ( self : int , __lowerCAmelCase : pa.Table ) -> pa.Table: """simple docstring""" if self.config.features is not None: # more expensive cast to support nested features with keys in a different order # allows str <-> int/float or str to Audio for example A__ = table_cast(__lowerCAmelCase , self.config.features.arrow_schema ) return pa_table def a_ ( self : List[Any] , __lowerCAmelCase : List[Any] ) -> Tuple: """simple docstring""" for i, file in enumerate(itertools.chain.from_iterable(__lowerCAmelCase ) ): with open(__lowerCAmelCase , """rb""" ) as f: A__ = pa.Table.from_pandas(pd.read_pickle(__lowerCAmelCase ) ) yield i, self._cast_table(__lowerCAmelCase )
274
import json import os from pathlib import Path from shutil import copyfile from typing import Any, Dict, List, Optional, Tuple, Union import sentencepiece from ...tokenization_utils import BatchEncoding, PreTrainedTokenizer from ...utils import logging A : Optional[Any] = logging.get_logger(__name__) A : str = '''▁''' A : Any = { '''vocab_file''': '''vocab.json''', '''spm_file''': '''sentencepiece.bpe.model''', '''tokenizer_config_file''': '''tokenizer_config.json''', } A : List[Any] = { '''vocab_file''': { '''facebook/m2m100_418M''': '''https://huggingface.co/facebook/m2m100_418M/resolve/main/vocab.json''', '''facebook/m2m100_1.2B''': '''https://huggingface.co/facebook/m2m100_1.2B/resolve/main/vocab.json''', }, '''spm_file''': { '''facebook/m2m100_418M''': '''https://huggingface.co/facebook/m2m100_418M/resolve/main/sentencepiece.bpe.model''', '''facebook/m2m100_1.2B''': '''https://huggingface.co/facebook/m2m100_1.2B/resolve/main/sentencepiece.bpe.model''', }, '''tokenizer_config_file''': { '''facebook/m2m100_418M''': '''https://huggingface.co/facebook/m2m100_418M/resolve/main/tokenizer_config.json''', '''facebook/m2m100_1.2B''': '''https://huggingface.co/facebook/m2m100_1.2B/resolve/main/tokenizer_config.json''', }, } A : Tuple = { '''facebook/m2m100_418M''': 1_0_2_4, } # fmt: off A : Optional[int] = { '''m2m100''': ['''af''', '''am''', '''ar''', '''ast''', '''az''', '''ba''', '''be''', '''bg''', '''bn''', '''br''', '''bs''', '''ca''', '''ceb''', '''cs''', '''cy''', '''da''', '''de''', '''el''', '''en''', '''es''', '''et''', '''fa''', '''ff''', '''fi''', '''fr''', '''fy''', '''ga''', '''gd''', '''gl''', '''gu''', '''ha''', '''he''', '''hi''', '''hr''', '''ht''', '''hu''', '''hy''', '''id''', '''ig''', '''ilo''', '''is''', '''it''', '''ja''', '''jv''', '''ka''', '''kk''', '''km''', '''kn''', '''ko''', '''lb''', '''lg''', '''ln''', '''lo''', '''lt''', '''lv''', '''mg''', '''mk''', '''ml''', '''mn''', '''mr''', '''ms''', '''my''', '''ne''', '''nl''', '''no''', '''ns''', '''oc''', '''or''', '''pa''', '''pl''', '''ps''', '''pt''', '''ro''', '''ru''', '''sd''', '''si''', '''sk''', '''sl''', '''so''', '''sq''', '''sr''', '''ss''', '''su''', '''sv''', '''sw''', '''ta''', '''th''', '''tl''', '''tn''', '''tr''', '''uk''', '''ur''', '''uz''', '''vi''', '''wo''', '''xh''', '''yi''', '''yo''', '''zh''', '''zu'''], '''wmt21''': ['''en''', '''ha''', '''is''', '''ja''', '''cs''', '''ru''', '''zh''', '''de'''] } class A (SCREAMING_SNAKE_CASE ): '''simple docstring''' __lowerCamelCase : Union[str, Any] = VOCAB_FILES_NAMES __lowerCamelCase : List[str] = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES __lowerCamelCase : Optional[int] = PRETRAINED_VOCAB_FILES_MAP __lowerCamelCase : Dict = ['''input_ids''', '''attention_mask'''] __lowerCamelCase : List[int] = [] __lowerCamelCase : List[int] = [] def __init__( self : List[Any] , __lowerCAmelCase : List[Any] , __lowerCAmelCase : Dict , __lowerCAmelCase : Union[str, Any]=None , __lowerCAmelCase : str=None , __lowerCAmelCase : List[Any]="<s>" , __lowerCAmelCase : List[Any]="</s>" , __lowerCAmelCase : Optional[int]="</s>" , __lowerCAmelCase : Optional[Any]="<pad>" , __lowerCAmelCase : Any="<unk>" , __lowerCAmelCase : Any="m2m100" , __lowerCAmelCase : Optional[Dict[str, Any]] = None , __lowerCAmelCase : Dict=8 , **__lowerCAmelCase : Tuple , ) -> None: """simple docstring""" A__ = {} if sp_model_kwargs is None else sp_model_kwargs A__ = language_codes A__ = FAIRSEQ_LANGUAGE_CODES[language_codes] A__ = {lang_code: f'__{lang_code}__' for lang_code in fairseq_language_code} A__ = kwargs.get("""additional_special_tokens""" , [] ) kwargs["additional_special_tokens"] += [ self.get_lang_token(__lowerCAmelCase ) for lang_code in fairseq_language_code if self.get_lang_token(__lowerCAmelCase ) not in kwargs["additional_special_tokens"] ] super().__init__( src_lang=__lowerCAmelCase , tgt_lang=__lowerCAmelCase , bos_token=__lowerCAmelCase , eos_token=__lowerCAmelCase , sep_token=__lowerCAmelCase , unk_token=__lowerCAmelCase , pad_token=__lowerCAmelCase , language_codes=__lowerCAmelCase , sp_model_kwargs=self.sp_model_kwargs , num_madeup_words=__lowerCAmelCase , **__lowerCAmelCase , ) A__ = vocab_file A__ = load_json(__lowerCAmelCase ) A__ = {v: k for k, v in self.encoder.items()} A__ = spm_file A__ = load_spm(__lowerCAmelCase , self.sp_model_kwargs ) A__ = len(self.encoder ) A__ = { self.get_lang_token(__lowerCAmelCase ): self.encoder_size + i for i, lang_code in enumerate(__lowerCAmelCase ) } A__ = {lang_code: self.encoder_size + i for i, lang_code in enumerate(__lowerCAmelCase )} A__ = {v: k for k, v in self.lang_token_to_id.items()} A__ = src_lang if src_lang is not None else """en""" A__ = tgt_lang A__ = self.get_lang_id(self._src_lang ) self.set_src_lang_special_tokens(self._src_lang ) A__ = num_madeup_words @property def a_ ( self : Optional[int] ) -> int: """simple docstring""" return len(self.encoder ) + len(self.lang_token_to_id ) @property def a_ ( self : Optional[Any] ) -> str: """simple docstring""" return self._src_lang @src_lang.setter def a_ ( self : List[Any] , __lowerCAmelCase : str ) -> None: """simple docstring""" A__ = new_src_lang self.set_src_lang_special_tokens(self._src_lang ) def a_ ( self : Optional[int] , __lowerCAmelCase : str ) -> List[str]: """simple docstring""" return self.sp_model.encode(__lowerCAmelCase , out_type=__lowerCAmelCase ) def a_ ( self : Optional[Any] , __lowerCAmelCase : Dict ) -> Optional[Any]: """simple docstring""" if token in self.lang_token_to_id: return self.lang_token_to_id[token] return self.encoder.get(__lowerCAmelCase , self.encoder[self.unk_token] ) def a_ ( self : Optional[int] , __lowerCAmelCase : int ) -> str: """simple docstring""" if index in self.id_to_lang_token: return self.id_to_lang_token[index] return self.decoder.get(__lowerCAmelCase , self.unk_token ) def a_ ( self : Optional[int] , __lowerCAmelCase : Dict ) -> str: """simple docstring""" A__ = [] A__ = """""" for token in tokens: # make sure that special tokens are not decoded using sentencepiece model if token in self.all_special_tokens: out_string += self.sp_model.decode(__lowerCAmelCase ) + token A__ = [] else: current_sub_tokens.append(__lowerCAmelCase ) out_string += self.sp_model.decode(__lowerCAmelCase ) return out_string.strip() def a_ ( self : List[str] , __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 ) A__ = [1] * len(self.prefix_tokens ) A__ = [1] * len(self.suffix_tokens ) if token_ids_a is None: return prefix_ones + ([0] * len(__lowerCAmelCase )) + suffix_ones return prefix_ones + ([0] * len(__lowerCAmelCase )) + ([0] * len(__lowerCAmelCase )) + suffix_ones def a_ ( self : Tuple , __lowerCAmelCase : List[int] , __lowerCAmelCase : Optional[List[int]] = None ) -> List[int]: """simple docstring""" if token_ids_a is None: return self.prefix_tokens + token_ids_a + self.suffix_tokens # We don't expect to process pairs, but leave the pair logic for API consistency return self.prefix_tokens + token_ids_a + token_ids_a + self.suffix_tokens def a_ ( self : int ) -> Dict: """simple docstring""" A__ = {self.convert_ids_to_tokens(__lowerCAmelCase ): i for i in range(self.vocab_size )} vocab.update(self.added_tokens_encoder ) return vocab def __getstate__( self : Union[str, Any] ) -> Dict: """simple docstring""" A__ = self.__dict__.copy() A__ = None return state def __setstate__( self : str , __lowerCAmelCase : Dict ) -> None: """simple docstring""" A__ = d # for backward compatibility if not hasattr(self , """sp_model_kwargs""" ): A__ = {} A__ = load_spm(self.spm_file , self.sp_model_kwargs ) def a_ ( self : List[str] , __lowerCAmelCase : str , __lowerCAmelCase : Optional[str] = None ) -> Tuple[str]: """simple docstring""" A__ = Path(__lowerCAmelCase ) if not save_dir.is_dir(): raise OSError(f'{save_directory} should be a directory' ) A__ = save_dir / ( (filename_prefix + """-""" if filename_prefix else """""") + self.vocab_files_names["""vocab_file"""] ) A__ = save_dir / ( (filename_prefix + """-""" if filename_prefix else """""") + self.vocab_files_names["""spm_file"""] ) save_json(self.encoder , __lowerCAmelCase ) if os.path.abspath(self.spm_file ) != os.path.abspath(__lowerCAmelCase ) and os.path.isfile(self.spm_file ): copyfile(self.spm_file , __lowerCAmelCase ) elif not os.path.isfile(self.spm_file ): with open(__lowerCAmelCase , """wb""" ) as fi: A__ = self.sp_model.serialized_model_proto() fi.write(__lowerCAmelCase ) return (str(__lowerCAmelCase ), str(__lowerCAmelCase )) def a_ ( self : str , __lowerCAmelCase : List[str] , __lowerCAmelCase : str = "en" , __lowerCAmelCase : Optional[List[str]] = None , __lowerCAmelCase : str = "ro" , **__lowerCAmelCase : List[Any] , ) -> BatchEncoding: """simple docstring""" A__ = src_lang A__ = tgt_lang self.set_src_lang_special_tokens(self.src_lang ) return super().prepare_seqaseq_batch(__lowerCAmelCase , __lowerCAmelCase , **__lowerCAmelCase ) def a_ ( self : Optional[int] , __lowerCAmelCase : Any , __lowerCAmelCase : Optional[str] , __lowerCAmelCase : Optional[str] , **__lowerCAmelCase : Tuple ) -> Tuple: """simple docstring""" if src_lang is None or tgt_lang is None: raise ValueError("""Translation requires a `src_lang` and a `tgt_lang` for this model""" ) A__ = src_lang A__ = self(__lowerCAmelCase , add_special_tokens=__lowerCAmelCase , **__lowerCAmelCase ) A__ = self.get_lang_id(__lowerCAmelCase ) A__ = tgt_lang_id return inputs def a_ ( self : Dict ) -> int: """simple docstring""" self.set_src_lang_special_tokens(self.src_lang ) def a_ ( self : Union[str, Any] ) -> Union[str, Any]: """simple docstring""" self.set_tgt_lang_special_tokens(self.tgt_lang ) def a_ ( self : str , __lowerCAmelCase : str ) -> None: """simple docstring""" A__ = self.get_lang_token(__lowerCAmelCase ) A__ = self.lang_token_to_id[lang_token] A__ = [self.cur_lang_id] A__ = [self.eos_token_id] def a_ ( self : Tuple , __lowerCAmelCase : str ) -> None: """simple docstring""" A__ = self.get_lang_token(__lowerCAmelCase ) A__ = self.lang_token_to_id[lang_token] A__ = [self.cur_lang_id] A__ = [self.eos_token_id] def a_ ( self : Union[str, Any] , __lowerCAmelCase : str ) -> str: """simple docstring""" return self.lang_code_to_token[lang] def a_ ( self : Union[str, Any] , __lowerCAmelCase : str ) -> int: """simple docstring""" A__ = self.get_lang_token(__lowerCAmelCase ) return self.lang_token_to_id[lang_token] def __lowerCamelCase ( __a :str , __a :Dict[str, Any] ) -> sentencepiece.SentencePieceProcessor: """simple docstring""" A__ = sentencepiece.SentencePieceProcessor(**__a ) spm.Load(str(__a ) ) return spm def __lowerCamelCase ( __a :str ) -> Union[Dict, List]: """simple docstring""" with open(__a , """r""" ) as f: return json.load(__a ) def __lowerCamelCase ( __a :List[Any] , __a :str ) -> None: """simple docstring""" with open(__a , """w""" ) as f: json.dump(__a , __a , indent=2 )
274
1
# 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. from pathlib import Path import torch from ...utils import is_npu_available, is_xpu_available from .config_args import ClusterConfig, default_json_config_file from .config_utils import SubcommandHelpFormatter A : Optional[Any] = '''Create a default config file for Accelerate with only a few flags set.''' def __lowerCamelCase ( __a :Optional[Any]="no" , __a :str = default_json_config_file , __a :bool = False ) -> int: """simple docstring""" A__ = Path(__a ) path.parent.mkdir(parents=__a , exist_ok=__a ) if path.exists(): print( F'Configuration already exists at {save_location}, will not override. Run `accelerate config` manually or pass a different `save_location`.' ) return False A__ = mixed_precision.lower() if mixed_precision not in ["no", "fp16", "bf16", "fp8"]: raise ValueError( F'`mixed_precision` should be one of \'no\', \'fp16\', \'bf16\', or \'fp8\'. Received {mixed_precision}' ) A__ = { """compute_environment""": """LOCAL_MACHINE""", """mixed_precision""": mixed_precision, } if torch.cuda.is_available(): A__ = torch.cuda.device_count() A__ = num_gpus A__ = False if num_gpus > 1: A__ = """MULTI_GPU""" else: A__ = """NO""" elif is_xpu_available() and use_xpu: A__ = torch.xpu.device_count() A__ = num_xpus A__ = False if num_xpus > 1: A__ = """MULTI_XPU""" else: A__ = """NO""" elif is_npu_available(): A__ = torch.npu.device_count() A__ = num_npus A__ = False if num_npus > 1: A__ = """MULTI_NPU""" else: A__ = """NO""" else: A__ = 0 A__ = True A__ = 1 A__ = """NO""" A__ = ClusterConfig(**__a ) config.to_json_file(__a ) return path def __lowerCamelCase ( __a :Optional[Any] , __a :List[str] ) -> Union[str, Any]: """simple docstring""" A__ = parser.add_parser("""default""" , parents=__a , help=__a , formatter_class=__a ) 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'.""" ) , dest="""save_location""" , ) parser.add_argument( """--mixed_precision""" , choices=["""no""", """fp16""", """bf16"""] , type=__a , help="""Whether or not to use mixed precision training. """ """Choose between FP16 and BF16 (bfloat16) training. """ """BF16 training is only supported on Nvidia Ampere GPUs and PyTorch 1.10 or later.""" , default="""no""" , ) parser.set_defaults(func=__a ) return parser def __lowerCamelCase ( __a :List[str] ) -> List[str]: """simple docstring""" A__ = write_basic_config(args.mixed_precision , args.save_location ) if config_file: print(F'accelerate configuration saved at {config_file}' )
274
from __future__ import annotations from PIL import Image # Define glider example A : Any = [ [0, 1, 0, 0, 0, 0, 0, 0], [0, 0, 1, 0, 0, 0, 0, 0], [1, 1, 1, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], ] # Define blinker example A : Optional[Any] = [[0, 1, 0], [0, 1, 0], [0, 1, 0]] def __lowerCamelCase ( __a :list[list[int]] ) -> list[list[int]]: """simple docstring""" A__ = [] for i in range(len(__a ) ): A__ = [] for j in range(len(cells[i] ) ): # Get the number of live neighbours A__ = 0 if i > 0 and j > 0: neighbour_count += cells[i - 1][j - 1] if i > 0: neighbour_count += cells[i - 1][j] if i > 0 and j < len(cells[i] ) - 1: neighbour_count += cells[i - 1][j + 1] if j > 0: neighbour_count += cells[i][j - 1] if j < len(cells[i] ) - 1: neighbour_count += cells[i][j + 1] if i < len(__a ) - 1 and j > 0: neighbour_count += cells[i + 1][j - 1] if i < len(__a ) - 1: neighbour_count += cells[i + 1][j] if i < len(__a ) - 1 and j < len(cells[i] ) - 1: neighbour_count += cells[i + 1][j + 1] # Rules of the game of life (excerpt from Wikipedia): # 1. Any live cell with two or three live neighbours survives. # 2. Any dead cell with three live neighbours becomes a live cell. # 3. All other live cells die in the next generation. # Similarly, all other dead cells stay dead. A__ = cells[i][j] == 1 if ( (alive and 2 <= neighbour_count <= 3) or not alive and neighbour_count == 3 ): next_generation_row.append(1 ) else: next_generation_row.append(0 ) next_generation.append(__a ) return next_generation def __lowerCamelCase ( __a :list[list[int]] , __a :int ) -> list[Image.Image]: """simple docstring""" A__ = [] for _ in range(__a ): # Create output image A__ = Image.new("""RGB""" , (len(cells[0] ), len(__a )) ) A__ = img.load() # Save cells to image for x in range(len(__a ) ): for y in range(len(cells[0] ) ): A__ = 2_5_5 - cells[y][x] * 2_5_5 A__ = (colour, colour, colour) # Save image images.append(__a ) A__ = new_generation(__a ) return images if __name__ == "__main__": A : str = generate_images(GLIDER, 1_6) images[0].save('''out.gif''', save_all=True, append_images=images[1:])
274
1
from __future__ import annotations A : Optional[int] = list[tuple[int, int]] A : str = [ [0, 0, 0, 0, 0, 0, 0], [0, 1, 0, 0, 0, 0, 0], # 0 are free path whereas 1's are obstacles [0, 0, 0, 0, 0, 0, 0], [0, 0, 1, 0, 0, 0, 0], [1, 0, 1, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 1, 0, 0], ] A : Union[str, Any] = ([-1, 0], [0, -1], [1, 0], [0, 1]) # up, left, down, right class A : '''simple docstring''' def __init__( self : int , __lowerCAmelCase : int , __lowerCAmelCase : int , __lowerCAmelCase : int , __lowerCAmelCase : int , __lowerCAmelCase : float , __lowerCAmelCase : Node | None , ) -> Dict: """simple docstring""" A__ = pos_x A__ = pos_y A__ = (pos_y, pos_x) A__ = goal_x A__ = goal_y A__ = g_cost A__ = parent A__ = self.calculate_heuristic() def a_ ( self : Optional[int] ) -> float: """simple docstring""" A__ = abs(self.pos_x - self.goal_x ) A__ = abs(self.pos_y - self.goal_y ) return dx + dy def __lt__( self : Any , __lowerCAmelCase : List[str] ) -> bool: """simple docstring""" return self.f_cost < other.f_cost class A : '''simple docstring''' def __init__( self : List[str] , __lowerCAmelCase : tuple[int, int] , __lowerCAmelCase : tuple[int, int] ) -> List[Any]: """simple docstring""" A__ = Node(start[1] , start[0] , goal[1] , goal[0] , 0 , __lowerCAmelCase ) A__ = Node(goal[1] , goal[0] , goal[1] , goal[0] , 9_99_99 , __lowerCAmelCase ) A__ = [self.start] A__ = [] A__ = False def a_ ( self : Union[str, Any] ) -> Path | None: """simple docstring""" while self.open_nodes: # Open Nodes are sorted using __lt__ self.open_nodes.sort() A__ = self.open_nodes.pop(0 ) if current_node.pos == self.target.pos: A__ = True return self.retrace_path(__lowerCAmelCase ) self.closed_nodes.append(__lowerCAmelCase ) A__ = self.get_successors(__lowerCAmelCase ) for child_node in successors: if child_node in self.closed_nodes: continue if child_node not in self.open_nodes: self.open_nodes.append(__lowerCAmelCase ) else: # retrieve the best current path A__ = self.open_nodes.pop(self.open_nodes.index(__lowerCAmelCase ) ) if child_node.g_cost < better_node.g_cost: self.open_nodes.append(__lowerCAmelCase ) else: self.open_nodes.append(__lowerCAmelCase ) if not self.reached: return [self.start.pos] return None def a_ ( self : Tuple , __lowerCAmelCase : Node ) -> list[Node]: """simple docstring""" A__ = [] for action in delta: A__ = parent.pos_x + action[1] A__ = parent.pos_y + action[0] if not (0 <= pos_x <= len(grid[0] ) - 1 and 0 <= pos_y <= len(__lowerCAmelCase ) - 1): continue if grid[pos_y][pos_x] != 0: continue successors.append( Node( __lowerCAmelCase , __lowerCAmelCase , self.target.pos_y , self.target.pos_x , parent.g_cost + 1 , __lowerCAmelCase , ) ) return successors def a_ ( self : str , __lowerCAmelCase : Node | None ) -> Path: """simple docstring""" A__ = node A__ = [] while current_node is not None: path.append((current_node.pos_y, current_node.pos_x) ) A__ = current_node.parent path.reverse() return path if __name__ == "__main__": A : Any = (0, 0) A : Any = (len(grid) - 1, len(grid[0]) - 1) for elem in grid: print(elem) print('''------''') A : Any = GreedyBestFirst(init, goal) A : List[str] = greedy_bf.search() if path: for pos_x, pos_y in path: A : Union[str, Any] = 2 for elem in grid: print(elem)
274
from datetime import datetime import requests from bsa import BeautifulSoup if __name__ == "__main__": A : List[str] = input('''Enter image url: ''').strip() print(F'''Downloading image from {url} ...''') A : Any = BeautifulSoup(requests.get(url).content, '''html.parser''') # The image URL is in the content field of the first meta tag with property og:image A : List[Any] = soup.find('''meta''', {'''property''': '''og:image'''})['''content'''] A : Dict = requests.get(image_url).content A : Tuple = F'''{datetime.now():%Y-%m-%d_%H:%M:%S}.jpg''' with open(file_name, '''wb''') as fp: fp.write(image_data) print(F'''Done. Image saved to disk as {file_name}.''')
274
1
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''': 650, '''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''': 600, '''eval_accuracy''': 0.3, '''eval_loss''': 0.9}, }, ] ) class A (unittest.TestCase ): '''simple docstring''' def a_ ( self : int ) -> Optional[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 a_ ( self : List[str] , __lowerCAmelCase : str=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 a_ ( self : Union[str, Any] , __lowerCAmelCase : Dict ) -> Union[str, Any]: """simple docstring""" TrainingJobAnalytics(__lowerCAmelCase ).export_csv(f'{self.env.test_path}/{job_name}_metrics.csv' ) def a_ ( self : Optional[int] ) -> Optional[int]: """simple docstring""" A__ = self.create_estimator() # run training estimator.fit() # result dataframe A__ = TrainingJobAnalytics(estimator.latest_training_job.name ).dataframe() # extract kpis A__ = list(result_metrics_df[result_metrics_df.metric_name == """eval_accuracy"""]["""value"""] ) A__ = list(result_metrics_df[result_metrics_df.metric_name == """eval_loss"""]["""value"""] ) # get train time from SageMaker job, this includes starting, preprocessing, stopping A__ = ( 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 )
274
# This code is adapted from OpenAI's release # https://github.com/openai/human-eval/blob/master/human_eval/execution.py import contextlib import faulthandler import io import multiprocessing import os import platform import signal import tempfile def __lowerCamelCase ( __a :List[str] , __a :List[Any] , __a :Union[str, Any] , __a :List[Any] ) -> Dict: """simple docstring""" A__ = multiprocessing.Manager() A__ = manager.list() A__ = multiprocessing.Process(target=__a , args=(check_program, result, timeout) ) p.start() p.join(timeout=timeout + 1 ) if p.is_alive(): p.kill() if not result: result.append("""timed out""" ) return { "task_id": task_id, "passed": result[0] == "passed", "result": result[0], "completion_id": completion_id, } def __lowerCamelCase ( __a :Optional[Any] , __a :Any , __a :List[Any] ) -> Union[str, Any]: """simple docstring""" with create_tempdir(): # These system calls are needed when cleaning up tempdir. import os import shutil A__ = shutil.rmtree A__ = os.rmdir A__ = os.chdir # Disable functionalities that can make destructive changes to the test. reliability_guard() # Run program. try: A__ = {} with swallow_io(): with time_limit(__a ): exec(__a , __a ) result.append("""passed""" ) except TimeoutException: result.append("""timed out""" ) except BaseException as e: result.append(F'failed: {e}' ) # Needed for cleaning up. A__ = rmtree A__ = rmdir A__ = chdir @contextlib.contextmanager def __lowerCamelCase ( __a :List[str] ) -> Dict: """simple docstring""" def signal_handler(__a :List[Any] , __a :Optional[Any] ): raise TimeoutException("""Timed out!""" ) signal.setitimer(signal.ITIMER_REAL , __a ) signal.signal(signal.SIGALRM , __a ) try: yield finally: signal.setitimer(signal.ITIMER_REAL , 0 ) @contextlib.contextmanager def __lowerCamelCase ( ) -> Union[str, Any]: """simple docstring""" A__ = WriteOnlyStringIO() with contextlib.redirect_stdout(__a ): with contextlib.redirect_stderr(__a ): with redirect_stdin(__a ): yield @contextlib.contextmanager def __lowerCamelCase ( ) -> Dict: """simple docstring""" with tempfile.TemporaryDirectory() as dirname: with chdir(__a ): yield dirname class A (SCREAMING_SNAKE_CASE ): '''simple docstring''' pass class A (io.StringIO ): '''simple docstring''' def a_ ( self : Any , *__lowerCAmelCase : List[str] , **__lowerCAmelCase : str ) -> Dict: """simple docstring""" raise OSError def a_ ( self : Optional[Any] , *__lowerCAmelCase : Any , **__lowerCAmelCase : Optional[int] ) -> str: """simple docstring""" raise OSError def a_ ( self : Optional[Any] , *__lowerCAmelCase : Any , **__lowerCAmelCase : Any ) -> int: """simple docstring""" raise OSError def a_ ( self : str , *__lowerCAmelCase : Any , **__lowerCAmelCase : Union[str, Any] ) -> int: """simple docstring""" return False class A (contextlib._RedirectStream ): # type: ignore '''simple docstring''' __lowerCamelCase : Union[str, Any] = '''stdin''' @contextlib.contextmanager def __lowerCamelCase ( __a :Union[str, Any] ) -> List[str]: """simple docstring""" if root == ".": yield return A__ = os.getcwd() os.chdir(__a ) try: yield except BaseException as exc: raise exc finally: os.chdir(__a ) def __lowerCamelCase ( __a :Union[str, Any]=None ) -> Dict: """simple docstring""" if maximum_memory_bytes is not None: import resource resource.setrlimit(resource.RLIMIT_AS , (maximum_memory_bytes, maximum_memory_bytes) ) resource.setrlimit(resource.RLIMIT_DATA , (maximum_memory_bytes, maximum_memory_bytes) ) if not platform.uname().system == "Darwin": resource.setrlimit(resource.RLIMIT_STACK , (maximum_memory_bytes, maximum_memory_bytes) ) faulthandler.disable() import builtins A__ = None A__ = None import os A__ = """1""" A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None import shutil A__ = None A__ = None A__ = None import subprocess A__ = None # type: ignore A__ = None import sys A__ = None A__ = None A__ = None A__ = None A__ = None
274
1
import argparse import torch from ...utils import logging from . import AlbertConfig, AlbertForPreTraining, load_tf_weights_in_albert logging.set_verbosity_info() def __lowerCamelCase ( __a :Tuple , __a :str , __a :Union[str, Any] ) -> Dict: """simple docstring""" A__ = AlbertConfig.from_json_file(__a ) print(F'Building PyTorch model from configuration: {config}' ) A__ = AlbertForPreTraining(__a ) # Load weights from tf checkpoint load_tf_weights_in_albert(__a , __a , __a ) # Save pytorch-model print(F'Save PyTorch model to {pytorch_dump_path}' ) torch.save(model.state_dict() , __a ) if __name__ == "__main__": A : List[Any] = argparse.ArgumentParser() # Required parameters parser.add_argument( '''--tf_checkpoint_path''', default=None, type=str, required=True, help='''Path to the TensorFlow checkpoint path.''' ) parser.add_argument( '''--albert_config_file''', default=None, type=str, required=True, help=( '''The config json file corresponding to the pre-trained ALBERT model. \n''' '''This specifies the model architecture.''' ), ) parser.add_argument( '''--pytorch_dump_path''', default=None, type=str, required=True, help='''Path to the output PyTorch model.''' ) A : Optional[Any] = parser.parse_args() convert_tf_checkpoint_to_pytorch(args.tf_checkpoint_path, args.albert_config_file, args.pytorch_dump_path)
274
import os from shutil import copyfile from typing import List, Optional, Tuple from ...tokenization_utils import AddedToken from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import is_sentencepiece_available, logging if is_sentencepiece_available(): from .tokenization_albert import AlbertTokenizer else: A : Tuple = None A : Optional[Any] = logging.get_logger(__name__) A : Tuple = {'''vocab_file''': '''spiece.model''', '''tokenizer_file''': '''tokenizer.json'''} A : List[str] = { '''vocab_file''': { '''albert-base-v1''': '''https://huggingface.co/albert-base-v1/resolve/main/spiece.model''', '''albert-large-v1''': '''https://huggingface.co/albert-large-v1/resolve/main/spiece.model''', '''albert-xlarge-v1''': '''https://huggingface.co/albert-xlarge-v1/resolve/main/spiece.model''', '''albert-xxlarge-v1''': '''https://huggingface.co/albert-xxlarge-v1/resolve/main/spiece.model''', '''albert-base-v2''': '''https://huggingface.co/albert-base-v2/resolve/main/spiece.model''', '''albert-large-v2''': '''https://huggingface.co/albert-large-v2/resolve/main/spiece.model''', '''albert-xlarge-v2''': '''https://huggingface.co/albert-xlarge-v2/resolve/main/spiece.model''', '''albert-xxlarge-v2''': '''https://huggingface.co/albert-xxlarge-v2/resolve/main/spiece.model''', }, '''tokenizer_file''': { '''albert-base-v1''': '''https://huggingface.co/albert-base-v1/resolve/main/tokenizer.json''', '''albert-large-v1''': '''https://huggingface.co/albert-large-v1/resolve/main/tokenizer.json''', '''albert-xlarge-v1''': '''https://huggingface.co/albert-xlarge-v1/resolve/main/tokenizer.json''', '''albert-xxlarge-v1''': '''https://huggingface.co/albert-xxlarge-v1/resolve/main/tokenizer.json''', '''albert-base-v2''': '''https://huggingface.co/albert-base-v2/resolve/main/tokenizer.json''', '''albert-large-v2''': '''https://huggingface.co/albert-large-v2/resolve/main/tokenizer.json''', '''albert-xlarge-v2''': '''https://huggingface.co/albert-xlarge-v2/resolve/main/tokenizer.json''', '''albert-xxlarge-v2''': '''https://huggingface.co/albert-xxlarge-v2/resolve/main/tokenizer.json''', }, } A : List[str] = { '''albert-base-v1''': 5_1_2, '''albert-large-v1''': 5_1_2, '''albert-xlarge-v1''': 5_1_2, '''albert-xxlarge-v1''': 5_1_2, '''albert-base-v2''': 5_1_2, '''albert-large-v2''': 5_1_2, '''albert-xlarge-v2''': 5_1_2, '''albert-xxlarge-v2''': 5_1_2, } A : Optional[int] = '''▁''' class A (SCREAMING_SNAKE_CASE ): '''simple docstring''' __lowerCamelCase : str = VOCAB_FILES_NAMES __lowerCamelCase : List[Any] = PRETRAINED_VOCAB_FILES_MAP __lowerCamelCase : Dict = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES __lowerCamelCase : List[str] = AlbertTokenizer def __init__( self : Tuple , __lowerCAmelCase : Tuple=None , __lowerCAmelCase : List[str]=None , __lowerCAmelCase : Optional[Any]=True , __lowerCAmelCase : List[Any]=True , __lowerCAmelCase : str=False , __lowerCAmelCase : Union[str, Any]="[CLS]" , __lowerCAmelCase : int="[SEP]" , __lowerCAmelCase : Dict="<unk>" , __lowerCAmelCase : Dict="[SEP]" , __lowerCAmelCase : Union[str, Any]="<pad>" , __lowerCAmelCase : str="[CLS]" , __lowerCAmelCase : int="[MASK]" , **__lowerCAmelCase : Optional[Any] , ) -> Optional[Any]: """simple docstring""" A__ = ( AddedToken(__lowerCAmelCase , lstrip=__lowerCAmelCase , rstrip=__lowerCAmelCase , normalized=__lowerCAmelCase ) if isinstance(__lowerCAmelCase , __lowerCAmelCase ) else mask_token ) super().__init__( __lowerCAmelCase , tokenizer_file=__lowerCAmelCase , do_lower_case=__lowerCAmelCase , remove_space=__lowerCAmelCase , keep_accents=__lowerCAmelCase , bos_token=__lowerCAmelCase , eos_token=__lowerCAmelCase , unk_token=__lowerCAmelCase , sep_token=__lowerCAmelCase , pad_token=__lowerCAmelCase , cls_token=__lowerCAmelCase , mask_token=__lowerCAmelCase , **__lowerCAmelCase , ) A__ = do_lower_case A__ = remove_space A__ = keep_accents A__ = vocab_file A__ = False if not self.vocab_file else True def a_ ( self : List[Any] , __lowerCAmelCase : List[int] , __lowerCAmelCase : Optional[List[int]] = None ) -> List[int]: """simple docstring""" A__ = [self.sep_token_id] A__ = [self.cls_token_id] if token_ids_a is None: return cls + token_ids_a + sep return cls + token_ids_a + sep + token_ids_a + sep def a_ ( self : Union[str, Any] , __lowerCAmelCase : List[int] , __lowerCAmelCase : Optional[List[int]] = None ) -> List[int]: """simple docstring""" A__ = [self.sep_token_id] A__ = [self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep ) * [0] return len(cls + token_ids_a + sep ) * [0] + len(token_ids_a + sep ) * [1] def a_ ( self : Tuple , __lowerCAmelCase : str , __lowerCAmelCase : Optional[str] = None ) -> Tuple[str]: """simple docstring""" if not self.can_save_slow_tokenizer: raise ValueError( """Your fast tokenizer does not have the necessary information to save the vocabulary for a slow """ """tokenizer.""" ) if not os.path.isdir(__lowerCAmelCase ): logger.error(f'Vocabulary path ({save_directory}) should be a directory' ) return A__ = 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 ): copyfile(self.vocab_file , __lowerCAmelCase ) return (out_vocab_file,)
274
1
def __lowerCamelCase ( __a :int , __a :int ) -> int: """simple docstring""" return int(input_a == input_a == 0 ) def __lowerCamelCase ( ) -> 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()
274
import dataclasses import json import warnings from dataclasses import dataclass, field from time import time from typing import List from ..utils import logging A : Dict = logging.get_logger(__name__) def __lowerCamelCase ( __a :int=None , __a :Optional[Any]=None ) -> int: """simple docstring""" return field(default_factory=lambda: default , metadata=__a ) @dataclass class A : '''simple docstring''' __lowerCamelCase : List[str] = list_field( default=[] , metadata={ '''help''': ( '''Model checkpoints to be provided to the AutoModel classes. Leave blank to benchmark the base version''' ''' of all available models''' ) } , ) __lowerCamelCase : List[int] = list_field( default=[8] , metadata={'''help''': '''List of batch sizes for which memory and time performance will be evaluated'''} ) __lowerCamelCase : List[int] = list_field( default=[8, 32, 128, 512] , metadata={'''help''': '''List of sequence lengths for which memory and time performance will be evaluated'''} , ) __lowerCamelCase : bool = field( default=SCREAMING_SNAKE_CASE , metadata={'''help''': '''Whether to benchmark inference of model. Inference can be disabled via --no-inference.'''} , ) __lowerCamelCase : bool = field( default=SCREAMING_SNAKE_CASE , metadata={'''help''': '''Whether to run on available cuda devices. Cuda can be disabled via --no-cuda.'''} , ) __lowerCamelCase : bool = field( default=SCREAMING_SNAKE_CASE , metadata={'''help''': '''Whether to run on available tpu devices. TPU can be disabled via --no-tpu.'''} ) __lowerCamelCase : bool = field(default=SCREAMING_SNAKE_CASE , metadata={'''help''': '''Use FP16 to accelerate inference.'''} ) __lowerCamelCase : bool = field(default=SCREAMING_SNAKE_CASE , metadata={'''help''': '''Benchmark training of model'''} ) __lowerCamelCase : bool = field(default=SCREAMING_SNAKE_CASE , metadata={'''help''': '''Verbose memory tracing'''} ) __lowerCamelCase : bool = field( default=SCREAMING_SNAKE_CASE , metadata={'''help''': '''Whether to perform speed measurements. Speed measurements can be disabled via --no-speed.'''} , ) __lowerCamelCase : bool = field( default=SCREAMING_SNAKE_CASE , metadata={ '''help''': '''Whether to perform memory measurements. Memory measurements can be disabled via --no-memory''' } , ) __lowerCamelCase : bool = field(default=SCREAMING_SNAKE_CASE , metadata={'''help''': '''Trace memory line by line'''} ) __lowerCamelCase : bool = field(default=SCREAMING_SNAKE_CASE , metadata={'''help''': '''Save result to a CSV file'''} ) __lowerCamelCase : bool = field(default=SCREAMING_SNAKE_CASE , metadata={'''help''': '''Save all print statements in a log file'''} ) __lowerCamelCase : bool = field(default=SCREAMING_SNAKE_CASE , metadata={'''help''': '''Whether to print environment information'''} ) __lowerCamelCase : bool = field( default=SCREAMING_SNAKE_CASE , metadata={ '''help''': ( '''Whether to use multiprocessing for memory and speed measurement. It is highly recommended to use''' ''' multiprocessing for accurate CPU and GPU memory measurements. This option should only be disabled''' ''' for debugging / testing and on TPU.''' ) } , ) __lowerCamelCase : str = field( default=F'''inference_time_{round(time() )}.csv''' , metadata={'''help''': '''CSV filename used if saving time results to csv.'''} , ) __lowerCamelCase : str = field( default=F'''inference_memory_{round(time() )}.csv''' , metadata={'''help''': '''CSV filename used if saving memory results to csv.'''} , ) __lowerCamelCase : str = field( default=F'''train_time_{round(time() )}.csv''' , metadata={'''help''': '''CSV filename used if saving time results to csv for training.'''} , ) __lowerCamelCase : str = field( default=F'''train_memory_{round(time() )}.csv''' , metadata={'''help''': '''CSV filename used if saving memory results to csv for training.'''} , ) __lowerCamelCase : str = field( default=F'''env_info_{round(time() )}.csv''' , metadata={'''help''': '''CSV filename used if saving environment information.'''} , ) __lowerCamelCase : str = field( default=F'''log_{round(time() )}.csv''' , metadata={'''help''': '''Log filename used if print statements are saved in log.'''} , ) __lowerCamelCase : int = field(default=3 , metadata={'''help''': '''Times an experiment will be run.'''} ) __lowerCamelCase : bool = field( default=SCREAMING_SNAKE_CASE , metadata={ '''help''': ( '''Instead of loading the model as defined in `config.architectures` if exists, just load the pretrain''' ''' model weights.''' ) } , ) def a_ ( self : Dict ) -> Union[str, Any]: """simple docstring""" warnings.warn( f'The class {self.__class__} is deprecated. Hugging Face Benchmarking utils' """ are deprecated in general and it is advised to use external Benchmarking libraries """ """ to benchmark Transformer models.""" , __lowerCAmelCase , ) def a_ ( self : Union[str, Any] ) -> List[str]: """simple docstring""" return json.dumps(dataclasses.asdict(self ) , indent=2 ) @property def a_ ( self : Tuple ) -> List[str]: """simple docstring""" if len(self.models ) <= 0: raise ValueError( """Please make sure you provide at least one model name / model identifier, *e.g.* `--models""" """ bert-base-cased` or `args.models = ['bert-base-cased'].""" ) return self.models @property def a_ ( self : Union[str, Any] ) -> Optional[Any]: """simple docstring""" if not self.multi_process: return False elif self.is_tpu: logger.info("""Multiprocessing is currently not possible on TPU.""" ) return False else: return True
274
1
import enum import os from hashlib import shaaaa from typing import Optional from .. import config from .logging import get_logger A : str = get_logger(__name__) class A (enum.Enum ): '''simple docstring''' __lowerCamelCase : List[Any] = '''all_checks''' __lowerCamelCase : Optional[int] = '''basic_checks''' __lowerCamelCase : Any = '''no_checks''' class A (SCREAMING_SNAKE_CASE ): '''simple docstring''' class A (SCREAMING_SNAKE_CASE ): '''simple docstring''' class A (SCREAMING_SNAKE_CASE ): '''simple docstring''' class A (SCREAMING_SNAKE_CASE ): '''simple docstring''' def __lowerCamelCase ( __a :Optional[dict] , __a :dict , __a :int=None ) -> Optional[Any]: """simple docstring""" if expected_checksums is None: logger.info("""Unable to verify checksums.""" ) return if len(set(__a ) - set(__a ) ) > 0: raise ExpectedMoreDownloadedFiles(str(set(__a ) - set(__a ) ) ) if len(set(__a ) - set(__a ) ) > 0: raise UnexpectedDownloadedFile(str(set(__a ) - set(__a ) ) ) A__ = [url for url in expected_checksums if expected_checksums[url] != recorded_checksums[url]] A__ = """ for """ + verification_name if verification_name is not None else """""" if len(__a ) > 0: raise NonMatchingChecksumError( F'Checksums didn\'t match{for_verification_name}:\n' F'{bad_urls}\n' """Set `verification_mode='no_checks'` to skip checksums verification and ignore this error""" ) logger.info("""All the checksums matched successfully""" + for_verification_name ) class A (SCREAMING_SNAKE_CASE ): '''simple docstring''' class A (SCREAMING_SNAKE_CASE ): '''simple docstring''' class A (SCREAMING_SNAKE_CASE ): '''simple docstring''' class A (SCREAMING_SNAKE_CASE ): '''simple docstring''' def __lowerCamelCase ( __a :Optional[dict] , __a :dict ) -> List[str]: """simple docstring""" if expected_splits is None: logger.info("""Unable to verify splits sizes.""" ) return if len(set(__a ) - set(__a ) ) > 0: raise ExpectedMoreSplits(str(set(__a ) - set(__a ) ) ) if len(set(__a ) - set(__a ) ) > 0: raise UnexpectedSplits(str(set(__a ) - set(__a ) ) ) A__ = [ {"""expected""": expected_splits[name], """recorded""": recorded_splits[name]} for name in expected_splits if expected_splits[name].num_examples != recorded_splits[name].num_examples ] if len(__a ) > 0: raise NonMatchingSplitsSizesError(str(__a ) ) logger.info("""All the splits matched successfully.""" ) def __lowerCamelCase ( __a :str , __a :bool = True ) -> dict: """simple docstring""" if record_checksum: A__ = shaaaa() with open(__a , """rb""" ) as f: for chunk in iter(lambda: f.read(1 << 2_0 ) , b"""""" ): m.update(__a ) A__ = m.hexdigest() else: A__ = None return {"num_bytes": os.path.getsize(__a ), "checksum": checksum} def __lowerCamelCase ( __a :int ) -> str: """simple docstring""" if dataset_size and config.IN_MEMORY_MAX_SIZE: return dataset_size < config.IN_MEMORY_MAX_SIZE else: return False
274
from math import ceil def __lowerCamelCase ( __a :int = 1_0_0_1 ) -> int: """simple docstring""" A__ = 1 for i in range(1 , int(ceil(n / 2.0 ) ) ): A__ = 2 * i + 1 A__ = 2 * i A__ = total + 4 * odd**2 - 6 * even return total if __name__ == "__main__": import sys if len(sys.argv) == 1: print(solution()) else: try: A : List[str] = int(sys.argv[1]) print(solution(n)) except ValueError: print('''Invalid entry - please enter a number''')
274
1
from __future__ import annotations import math from collections import Counter from string import ascii_lowercase def __lowerCamelCase ( __a :str ) -> None: """simple docstring""" A__ , A__ = analyze_text(__a ) A__ = list(""" """ + ascii_lowercase ) # what is our total sum of probabilities. A__ = sum(single_char_strings.values() ) # one length string A__ = 0 # for each alpha we go in our dict and if it is in it we calculate entropy for ch in my_alphas: if ch in single_char_strings: A__ = single_char_strings[ch] A__ = my_str / all_sum my_fir_sum += prob * math.loga(__a ) # entropy formula. # print entropy print(F'{round(-1 * my_fir_sum ):.1f}' ) # two len string A__ = sum(two_char_strings.values() ) A__ = 0 # for each alpha (two in size) calculate entropy. for cha in my_alphas: for cha in my_alphas: A__ = cha + cha if sequence in two_char_strings: A__ = two_char_strings[sequence] A__ = int(__a ) / all_sum my_sec_sum += prob * math.loga(__a ) # print second entropy print(F'{round(-1 * my_sec_sum ):.1f}' ) # print the difference between them print(F'{round((-1 * my_sec_sum) - (-1 * my_fir_sum) ):.1f}' ) def __lowerCamelCase ( __a :str ) -> tuple[dict, dict]: """simple docstring""" A__ = Counter() # type: ignore A__ = Counter() # type: ignore single_char_strings[text[-1]] += 1 # first case when we have space at start. two_char_strings[" " + text[0]] += 1 for i in range(0 , len(__a ) - 1 ): single_char_strings[text[i]] += 1 two_char_strings[text[i : i + 2]] += 1 return single_char_strings, two_char_strings def __lowerCamelCase ( ) -> List[str]: """simple docstring""" import doctest doctest.testmod() # text = ( # "Had repulsive dashwoods suspicion sincerity but advantage now him. Remark " # "easily garret nor nay. Civil those mrs enjoy shy fat merry. You greatest " # "jointure saw horrible. He private he on be imagine suppose. Fertile " # "beloved evident through no service elderly is. Blind there if every no so " # "at. Own neglected you preferred way sincerity delivered his attempted. To " # "of message cottage windows do besides against uncivil. Delightful " # "unreserved impossible few estimating men favourable see entreaties. She " # "propriety immediate was improving. He or entrance humoured likewise " # "moderate. Much nor game son say feel. Fat make met can must form into " # "gate. Me we offending prevailed discovery. " # ) # calculate_prob(text) if __name__ == "__main__": main()
274
import argparse import csv import logging import os import random import numpy as np import torch from torch.utils.data import DataLoader, RandomSampler, SequentialSampler, TensorDataset from tqdm import tqdm, trange from transformers import ( CONFIG_NAME, WEIGHTS_NAME, AdamW, OpenAIGPTDoubleHeadsModel, OpenAIGPTTokenizer, get_linear_schedule_with_warmup, ) logging.basicConfig( format='''%(asctime)s - %(levelname)s - %(name)s - %(message)s''', datefmt='''%m/%d/%Y %H:%M:%S''', level=logging.INFO ) A : Tuple = logging.getLogger(__name__) def __lowerCamelCase ( __a :Optional[int] , __a :List[str] ) -> Tuple: """simple docstring""" A__ = np.argmax(__a , axis=1 ) return np.sum(outputs == labels ) def __lowerCamelCase ( __a :Tuple ) -> Dict: """simple docstring""" with open(__a , encoding="""utf_8""" ) as f: A__ = csv.reader(__a ) A__ = [] next(__a ) # skip the first line for line in tqdm(__a ): output.append((""" """.join(line[1:5] ), line[5], line[6], int(line[-1] ) - 1) ) return output def __lowerCamelCase ( __a :Optional[int] , __a :List[Any] , __a :Dict , __a :Optional[Any] , __a :Optional[Any] , __a :int ) -> Union[str, Any]: """simple docstring""" A__ = [] for dataset in encoded_datasets: A__ = len(__a ) A__ = np.zeros((n_batch, 2, input_len) , dtype=np.intaa ) A__ = np.zeros((n_batch, 2) , dtype=np.intaa ) A__ = np.full((n_batch, 2, input_len) , fill_value=-1_0_0 , dtype=np.intaa ) A__ = np.zeros((n_batch,) , dtype=np.intaa ) for ( i, (story, conta, conta, mc_label), ) in enumerate(__a ): A__ = [start_token] + story[:cap_length] + [delimiter_token] + conta[:cap_length] + [clf_token] A__ = [start_token] + story[:cap_length] + [delimiter_token] + conta[:cap_length] + [clf_token] A__ = with_conta A__ = with_conta A__ = len(__a ) - 1 A__ = len(__a ) - 1 A__ = with_conta A__ = with_conta A__ = mc_label A__ = (input_ids, mc_token_ids, lm_labels, mc_labels) tensor_datasets.append(tuple(torch.tensor(__a ) for t in all_inputs ) ) return tensor_datasets def __lowerCamelCase ( ) -> Union[str, Any]: """simple docstring""" A__ = argparse.ArgumentParser() parser.add_argument("""--model_name""" , type=__a , default="""openai-gpt""" , help="""pretrained model name""" ) parser.add_argument("""--do_train""" , action="""store_true""" , help="""Whether to run training.""" ) parser.add_argument("""--do_eval""" , action="""store_true""" , help="""Whether to run eval on the dev set.""" ) parser.add_argument( """--output_dir""" , default=__a , type=__a , required=__a , help="""The output directory where the model predictions and checkpoints will be written.""" , ) parser.add_argument("""--train_dataset""" , type=__a , default="""""" ) parser.add_argument("""--eval_dataset""" , type=__a , default="""""" ) parser.add_argument("""--seed""" , type=__a , default=4_2 ) parser.add_argument("""--num_train_epochs""" , type=__a , default=3 ) parser.add_argument("""--train_batch_size""" , type=__a , default=8 ) parser.add_argument("""--eval_batch_size""" , type=__a , default=1_6 ) parser.add_argument("""--adam_epsilon""" , default=1E-8 , type=__a , help="""Epsilon for Adam optimizer.""" ) parser.add_argument("""--max_grad_norm""" , type=__a , default=1 ) parser.add_argument( """--max_steps""" , default=-1 , type=__a , help=( """If > 0: set total number of training steps to perform. Override num_train_epochs.""" ) , ) parser.add_argument( """--gradient_accumulation_steps""" , type=__a , default=1 , help="""Number of updates steps to accumulate before performing a backward/update pass.""" , ) parser.add_argument("""--learning_rate""" , type=__a , default=6.25E-5 ) parser.add_argument("""--warmup_steps""" , default=0 , type=__a , help="""Linear warmup over warmup_steps.""" ) parser.add_argument("""--lr_schedule""" , type=__a , default="""warmup_linear""" ) parser.add_argument("""--weight_decay""" , type=__a , default=0.01 ) parser.add_argument("""--lm_coef""" , type=__a , default=0.9 ) parser.add_argument("""--n_valid""" , type=__a , default=3_7_4 ) parser.add_argument("""--server_ip""" , type=__a , default="""""" , help="""Can be used for distant debugging.""" ) parser.add_argument("""--server_port""" , type=__a , default="""""" , help="""Can be used for distant debugging.""" ) A__ = parser.parse_args() print(__a ) if args.server_ip and args.server_port: # Distant debugging - see https://code.visualstudio.com/docs/python/debugging#_attach-to-a-local-script import ptvsd print("""Waiting for debugger attach""" ) ptvsd.enable_attach(address=(args.server_ip, args.server_port) , redirect_output=__a ) ptvsd.wait_for_attach() random.seed(args.seed ) np.random.seed(args.seed ) torch.manual_seed(args.seed ) torch.cuda.manual_seed_all(args.seed ) A__ = torch.device("""cuda""" if torch.cuda.is_available() else """cpu""" ) A__ = torch.cuda.device_count() logger.info("""device: {}, n_gpu {}""".format(__a , __a ) ) if not args.do_train and not args.do_eval: raise ValueError("""At least one of `do_train` or `do_eval` must be True.""" ) if not os.path.exists(args.output_dir ): os.makedirs(args.output_dir ) # Load tokenizer and model # This loading functions also add new tokens and embeddings called `special tokens` # These new embeddings will be fine-tuned on the RocStories dataset A__ = ["""_start_""", """_delimiter_""", """_classify_"""] A__ = OpenAIGPTTokenizer.from_pretrained(args.model_name ) tokenizer.add_tokens(__a ) A__ = tokenizer.convert_tokens_to_ids(__a ) A__ = OpenAIGPTDoubleHeadsModel.from_pretrained(args.model_name ) model.resize_token_embeddings(len(__a ) ) model.to(__a ) # Load and encode the datasets def tokenize_and_encode(__a :Tuple ): if isinstance(__a , __a ): return tokenizer.convert_tokens_to_ids(tokenizer.tokenize(__a ) ) elif isinstance(__a , __a ): return obj return [tokenize_and_encode(__a ) for o in obj] logger.info("""Encoding dataset...""" ) A__ = load_rocstories_dataset(args.train_dataset ) A__ = load_rocstories_dataset(args.eval_dataset ) A__ = (train_dataset, eval_dataset) A__ = tokenize_and_encode(__a ) # Compute the max input length for the Transformer A__ = model.config.n_positions // 2 - 2 A__ = max( len(story[:max_length] ) + max(len(conta[:max_length] ) , len(conta[:max_length] ) ) + 3 for dataset in encoded_datasets for story, conta, conta, _ in dataset ) A__ = min(__a , model.config.n_positions ) # Max size of input for the pre-trained model # Prepare inputs tensors and dataloaders A__ = pre_process_datasets(__a , __a , __a , *__a ) A__ , A__ = tensor_datasets[0], tensor_datasets[1] A__ = TensorDataset(*__a ) A__ = RandomSampler(__a ) A__ = DataLoader(__a , sampler=__a , batch_size=args.train_batch_size ) A__ = TensorDataset(*__a ) A__ = SequentialSampler(__a ) A__ = DataLoader(__a , sampler=__a , batch_size=args.eval_batch_size ) # Prepare optimizer if args.do_train: if args.max_steps > 0: A__ = args.max_steps A__ = args.max_steps // (len(__a ) // args.gradient_accumulation_steps) + 1 else: A__ = len(__a ) // args.gradient_accumulation_steps * args.num_train_epochs A__ = list(model.named_parameters() ) A__ = ["""bias""", """LayerNorm.bias""", """LayerNorm.weight"""] A__ = [ { """params""": [p for n, p in param_optimizer if not any(nd in n for nd in no_decay )], """weight_decay""": args.weight_decay, }, {"""params""": [p for n, p in param_optimizer if any(nd in n for nd in no_decay )], """weight_decay""": 0.0}, ] A__ = AdamW(__a , lr=args.learning_rate , eps=args.adam_epsilon ) A__ = get_linear_schedule_with_warmup( __a , num_warmup_steps=args.warmup_steps , num_training_steps=__a ) if args.do_train: A__ , A__ , A__ = 0, 0, None model.train() for _ in trange(int(args.num_train_epochs ) , desc="""Epoch""" ): A__ = 0 A__ = 0 A__ = tqdm(__a , desc="""Training""" ) for step, batch in enumerate(__a ): A__ = tuple(t.to(__a ) for t in batch ) A__ , A__ , A__ , A__ = batch A__ = model(__a , mc_token_ids=__a , lm_labels=__a , mc_labels=__a ) A__ = args.lm_coef * losses[0] + losses[1] loss.backward() optimizer.step() scheduler.step() optimizer.zero_grad() tr_loss += loss.item() A__ = ( loss.item() if exp_average_loss is None else 0.7 * exp_average_loss + 0.3 * loss.item() ) nb_tr_steps += 1 A__ = """Training loss: {:.2e} lr: {:.2e}""".format(__a , scheduler.get_lr()[0] ) # Save a trained model if args.do_train: # Save a trained model, configuration and tokenizer A__ = model.module if hasattr(__a , """module""" ) else model # Only save the model itself # If we save using the predefined names, we can load using `from_pretrained` A__ = os.path.join(args.output_dir , __a ) A__ = os.path.join(args.output_dir , __a ) torch.save(model_to_save.state_dict() , __a ) model_to_save.config.to_json_file(__a ) tokenizer.save_vocabulary(args.output_dir ) # Load a trained model and vocabulary that you have fine-tuned A__ = OpenAIGPTDoubleHeadsModel.from_pretrained(args.output_dir ) A__ = OpenAIGPTTokenizer.from_pretrained(args.output_dir ) model.to(__a ) if args.do_eval: model.eval() A__ , A__ = 0, 0 A__ , A__ = 0, 0 for batch in tqdm(__a , desc="""Evaluating""" ): A__ = tuple(t.to(__a ) for t in batch ) A__ , A__ , A__ , A__ = batch with torch.no_grad(): A__ , A__ , A__ , A__ = model( __a , mc_token_ids=__a , lm_labels=__a , mc_labels=__a ) A__ = mc_logits.detach().cpu().numpy() A__ = mc_labels.to("""cpu""" ).numpy() A__ = accuracy(__a , __a ) eval_loss += mc_loss.mean().item() eval_accuracy += tmp_eval_accuracy nb_eval_examples += input_ids.size(0 ) nb_eval_steps += 1 A__ = eval_loss / nb_eval_steps A__ = eval_accuracy / nb_eval_examples A__ = tr_loss / nb_tr_steps if args.do_train else None A__ = {"""eval_loss""": eval_loss, """eval_accuracy""": eval_accuracy, """train_loss""": train_loss} A__ = os.path.join(args.output_dir , """eval_results.txt""" ) with open(__a , """w""" ) as writer: logger.info("""***** Eval results *****""" ) for key in sorted(result.keys() ): logger.info(""" %s = %s""" , __a , str(result[key] ) ) writer.write("""%s = %s\n""" % (key, str(result[key] )) ) if __name__ == "__main__": main()
274
1
import warnings from ...processing_utils import ProcessorMixin from ...tokenization_utils_base import BatchEncoding class A (SCREAMING_SNAKE_CASE ): '''simple docstring''' __lowerCamelCase : int = ['''image_processor''', '''tokenizer'''] __lowerCamelCase : Dict = '''ViTImageProcessor''' __lowerCamelCase : Any = ('''CLIPTokenizer''', '''CLIPTokenizerFast''') def __init__( self : int , __lowerCAmelCase : Optional[Any]=None , __lowerCAmelCase : Dict=None , **__lowerCAmelCase : str ) -> Optional[Any]: """simple docstring""" A__ = None if "feature_extractor" in kwargs: warnings.warn( """The `feature_extractor` argument is deprecated and will be removed in v5, use `image_processor`""" """ instead.""" , __lowerCAmelCase , ) A__ = kwargs.pop("""feature_extractor""" ) A__ = image_processor if image_processor is not None else feature_extractor if image_processor is None: raise ValueError("""You need to specify an `image_processor`.""" ) if tokenizer is None: raise ValueError("""You need to specify a `tokenizer`.""" ) super().__init__(__lowerCAmelCase , __lowerCAmelCase ) def __call__( self : int , __lowerCAmelCase : Union[str, Any]=None , __lowerCAmelCase : Dict=None , __lowerCAmelCase : Tuple=None , __lowerCAmelCase : Union[str, Any]=None , **__lowerCAmelCase : Optional[Any] ) -> Tuple: """simple docstring""" if text is None and visual_prompt is None and images is None: raise ValueError("""You have to specify either text, visual prompt or images.""" ) if text is not None and visual_prompt is not None: raise ValueError("""You have to specify exactly one type of prompt. Either text or visual prompt.""" ) if text is not None: A__ = self.tokenizer(__lowerCAmelCase , return_tensors=__lowerCAmelCase , **__lowerCAmelCase ) if visual_prompt is not None: A__ = self.image_processor(__lowerCAmelCase , return_tensors=__lowerCAmelCase , **__lowerCAmelCase ) if images is not None: A__ = self.image_processor(__lowerCAmelCase , return_tensors=__lowerCAmelCase , **__lowerCAmelCase ) if visual_prompt is not None and images is not None: A__ = { """pixel_values""": image_features.pixel_values, """conditional_pixel_values""": prompt_features.pixel_values, } return encoding elif text is not None and images is not None: A__ = image_features.pixel_values return encoding elif text is not None: return encoding elif visual_prompt is not None: A__ = { """conditional_pixel_values""": prompt_features.pixel_values, } return encoding else: return BatchEncoding(data=dict(**__lowerCAmelCase ) , tensor_type=__lowerCAmelCase ) def a_ ( self : str , *__lowerCAmelCase : List[Any] , **__lowerCAmelCase : Tuple ) -> Union[str, Any]: """simple docstring""" return self.tokenizer.batch_decode(*__lowerCAmelCase , **__lowerCAmelCase ) def a_ ( self : List[Any] , *__lowerCAmelCase : Tuple , **__lowerCAmelCase : int ) -> List[str]: """simple docstring""" return self.tokenizer.decode(*__lowerCAmelCase , **__lowerCAmelCase ) @property def a_ ( self : Dict ) -> List[Any]: """simple docstring""" warnings.warn( """`feature_extractor_class` is deprecated and will be removed in v5. Use `image_processor_class` instead.""" , __lowerCAmelCase , ) return self.image_processor_class @property def a_ ( self : str ) -> Optional[Any]: """simple docstring""" warnings.warn( """`feature_extractor` is deprecated and will be removed in v5. Use `image_processor` instead.""" , __lowerCAmelCase , ) return self.image_processor
274
import argparse from collections import defaultdict import yaml A : str = '''docs/source/en/_toctree.yml''' def __lowerCamelCase ( __a :str ) -> List[Any]: """simple docstring""" A__ = defaultdict(__a ) A__ = [] A__ = [] for doc in doc_list: if "local" in doc: counts[doc["local"]] += 1 if doc["title"].lower() == "overview": overview_doc.append({"""local""": doc["""local"""], """title""": doc["""title"""]} ) else: new_doc_list.append(__a ) A__ = new_doc_list A__ = [key for key, value in counts.items() if value > 1] A__ = [] for duplicate_key in duplicates: A__ = list({doc["""title"""] for doc in doc_list if doc["""local"""] == duplicate_key} ) if len(__a ) > 1: raise ValueError( F'{duplicate_key} is present several times in the documentation table of content at ' """`docs/source/en/_toctree.yml` with different *Title* values. Choose one of those and remove the """ """others.""" ) # Only add this once new_doc.append({"""local""": duplicate_key, """title""": titles[0]} ) # Add none duplicate-keys new_doc.extend([doc for doc in doc_list if """local""" not in counts or counts[doc["""local"""]] == 1] ) A__ = sorted(__a , key=lambda __a : s["title"].lower() ) # "overview" gets special treatment and is always first if len(__a ) > 1: raise ValueError("""{doc_list} has two 'overview' docs which is not allowed.""" ) overview_doc.extend(__a ) # Sort return overview_doc def __lowerCamelCase ( __a :Any=False ) -> List[str]: """simple docstring""" with open(__a , encoding="""utf-8""" ) as f: A__ = yaml.safe_load(f.read() ) # Get to the API doc A__ = 0 while content[api_idx]["title"] != "API": api_idx += 1 A__ = content[api_idx]["""sections"""] # Then to the model doc A__ = 0 while api_doc[scheduler_idx]["title"] != "Schedulers": scheduler_idx += 1 A__ = api_doc[scheduler_idx]["""sections"""] A__ = clean_doc_toc(__a ) A__ = False if new_scheduler_doc != scheduler_doc: A__ = True if overwrite: A__ = new_scheduler_doc if diff: if overwrite: A__ = api_doc with open(__a , """w""" , encoding="""utf-8""" ) as f: f.write(yaml.dump(__a , allow_unicode=__a ) ) else: raise ValueError( """The model doc part of the table of content is not properly sorted, run `make style` to fix this.""" ) def __lowerCamelCase ( __a :Optional[int]=False ) -> Dict: """simple docstring""" with open(__a , encoding="""utf-8""" ) as f: A__ = yaml.safe_load(f.read() ) # Get to the API doc A__ = 0 while content[api_idx]["title"] != "API": api_idx += 1 A__ = content[api_idx]["""sections"""] # Then to the model doc A__ = 0 while api_doc[pipeline_idx]["title"] != "Pipelines": pipeline_idx += 1 A__ = False A__ = api_doc[pipeline_idx]["""sections"""] A__ = [] # sort sub pipeline docs for pipeline_doc in pipeline_docs: if "section" in pipeline_doc: A__ = pipeline_doc["""section"""] A__ = clean_doc_toc(__a ) if overwrite: A__ = new_sub_pipeline_doc new_pipeline_docs.append(__a ) # sort overall pipeline doc A__ = clean_doc_toc(__a ) if new_pipeline_docs != pipeline_docs: A__ = True if overwrite: A__ = new_pipeline_docs if diff: if overwrite: A__ = api_doc with open(__a , """w""" , encoding="""utf-8""" ) as f: f.write(yaml.dump(__a , allow_unicode=__a ) ) else: raise ValueError( """The model doc part of the table of content is not properly sorted, run `make style` to fix this.""" ) if __name__ == "__main__": A : Tuple = argparse.ArgumentParser() parser.add_argument('''--fix_and_overwrite''', action='''store_true''', help='''Whether to fix inconsistencies.''') A : Optional[Any] = parser.parse_args() check_scheduler_doc(args.fix_and_overwrite) check_pipeline_doc(args.fix_and_overwrite)
274
1
import os from shutil import copyfile from typing import List, Optional, Tuple from ...tokenization_utils import AddedToken from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import is_sentencepiece_available, logging if is_sentencepiece_available(): from .tokenization_albert import AlbertTokenizer else: A : Tuple = None A : Optional[Any] = logging.get_logger(__name__) A : Tuple = {'''vocab_file''': '''spiece.model''', '''tokenizer_file''': '''tokenizer.json'''} A : List[str] = { '''vocab_file''': { '''albert-base-v1''': '''https://huggingface.co/albert-base-v1/resolve/main/spiece.model''', '''albert-large-v1''': '''https://huggingface.co/albert-large-v1/resolve/main/spiece.model''', '''albert-xlarge-v1''': '''https://huggingface.co/albert-xlarge-v1/resolve/main/spiece.model''', '''albert-xxlarge-v1''': '''https://huggingface.co/albert-xxlarge-v1/resolve/main/spiece.model''', '''albert-base-v2''': '''https://huggingface.co/albert-base-v2/resolve/main/spiece.model''', '''albert-large-v2''': '''https://huggingface.co/albert-large-v2/resolve/main/spiece.model''', '''albert-xlarge-v2''': '''https://huggingface.co/albert-xlarge-v2/resolve/main/spiece.model''', '''albert-xxlarge-v2''': '''https://huggingface.co/albert-xxlarge-v2/resolve/main/spiece.model''', }, '''tokenizer_file''': { '''albert-base-v1''': '''https://huggingface.co/albert-base-v1/resolve/main/tokenizer.json''', '''albert-large-v1''': '''https://huggingface.co/albert-large-v1/resolve/main/tokenizer.json''', '''albert-xlarge-v1''': '''https://huggingface.co/albert-xlarge-v1/resolve/main/tokenizer.json''', '''albert-xxlarge-v1''': '''https://huggingface.co/albert-xxlarge-v1/resolve/main/tokenizer.json''', '''albert-base-v2''': '''https://huggingface.co/albert-base-v2/resolve/main/tokenizer.json''', '''albert-large-v2''': '''https://huggingface.co/albert-large-v2/resolve/main/tokenizer.json''', '''albert-xlarge-v2''': '''https://huggingface.co/albert-xlarge-v2/resolve/main/tokenizer.json''', '''albert-xxlarge-v2''': '''https://huggingface.co/albert-xxlarge-v2/resolve/main/tokenizer.json''', }, } A : List[str] = { '''albert-base-v1''': 5_1_2, '''albert-large-v1''': 5_1_2, '''albert-xlarge-v1''': 5_1_2, '''albert-xxlarge-v1''': 5_1_2, '''albert-base-v2''': 5_1_2, '''albert-large-v2''': 5_1_2, '''albert-xlarge-v2''': 5_1_2, '''albert-xxlarge-v2''': 5_1_2, } A : Optional[int] = '''▁''' class A (SCREAMING_SNAKE_CASE ): '''simple docstring''' __lowerCamelCase : str = VOCAB_FILES_NAMES __lowerCamelCase : List[Any] = PRETRAINED_VOCAB_FILES_MAP __lowerCamelCase : Dict = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES __lowerCamelCase : List[str] = AlbertTokenizer def __init__( self : Tuple , __lowerCAmelCase : Tuple=None , __lowerCAmelCase : List[str]=None , __lowerCAmelCase : Optional[Any]=True , __lowerCAmelCase : List[Any]=True , __lowerCAmelCase : str=False , __lowerCAmelCase : Union[str, Any]="[CLS]" , __lowerCAmelCase : int="[SEP]" , __lowerCAmelCase : Dict="<unk>" , __lowerCAmelCase : Dict="[SEP]" , __lowerCAmelCase : Union[str, Any]="<pad>" , __lowerCAmelCase : str="[CLS]" , __lowerCAmelCase : int="[MASK]" , **__lowerCAmelCase : Optional[Any] , ) -> Optional[Any]: """simple docstring""" A__ = ( AddedToken(__lowerCAmelCase , lstrip=__lowerCAmelCase , rstrip=__lowerCAmelCase , normalized=__lowerCAmelCase ) if isinstance(__lowerCAmelCase , __lowerCAmelCase ) else mask_token ) super().__init__( __lowerCAmelCase , tokenizer_file=__lowerCAmelCase , do_lower_case=__lowerCAmelCase , remove_space=__lowerCAmelCase , keep_accents=__lowerCAmelCase , bos_token=__lowerCAmelCase , eos_token=__lowerCAmelCase , unk_token=__lowerCAmelCase , sep_token=__lowerCAmelCase , pad_token=__lowerCAmelCase , cls_token=__lowerCAmelCase , mask_token=__lowerCAmelCase , **__lowerCAmelCase , ) A__ = do_lower_case A__ = remove_space A__ = keep_accents A__ = vocab_file A__ = False if not self.vocab_file else True def a_ ( self : List[Any] , __lowerCAmelCase : List[int] , __lowerCAmelCase : Optional[List[int]] = None ) -> List[int]: """simple docstring""" A__ = [self.sep_token_id] A__ = [self.cls_token_id] if token_ids_a is None: return cls + token_ids_a + sep return cls + token_ids_a + sep + token_ids_a + sep def a_ ( self : Union[str, Any] , __lowerCAmelCase : List[int] , __lowerCAmelCase : Optional[List[int]] = None ) -> List[int]: """simple docstring""" A__ = [self.sep_token_id] A__ = [self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep ) * [0] return len(cls + token_ids_a + sep ) * [0] + len(token_ids_a + sep ) * [1] def a_ ( self : Tuple , __lowerCAmelCase : str , __lowerCAmelCase : Optional[str] = None ) -> Tuple[str]: """simple docstring""" if not self.can_save_slow_tokenizer: raise ValueError( """Your fast tokenizer does not have the necessary information to save the vocabulary for a slow """ """tokenizer.""" ) if not os.path.isdir(__lowerCAmelCase ): logger.error(f'Vocabulary path ({save_directory}) should be a directory' ) return A__ = 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 ): copyfile(self.vocab_file , __lowerCAmelCase ) return (out_vocab_file,)
274
def __lowerCamelCase ( __a :str ) -> list: """simple docstring""" A__ = [0] * len(__a ) for i in range(1 , len(__a ) ): # use last results for better performance - dynamic programming A__ = prefix_result[i - 1] while j > 0 and input_string[i] != input_string[j]: A__ = prefix_result[j - 1] if input_string[i] == input_string[j]: j += 1 A__ = j return prefix_result def __lowerCamelCase ( __a :str ) -> int: """simple docstring""" return max(prefix_function(__a ) ) if __name__ == "__main__": import doctest doctest.testmod()
274
1
import logging import os from dataclasses import dataclass from enum import Enum from typing import List, Optional, Union from filelock import FileLock from transformers import PreTrainedTokenizer, is_tf_available, is_torch_available A : Union[str, Any] = logging.getLogger(__name__) @dataclass class A : '''simple docstring''' __lowerCamelCase : str __lowerCamelCase : List[str] __lowerCamelCase : Optional[List[str]] @dataclass class A : '''simple docstring''' __lowerCamelCase : List[int] __lowerCamelCase : List[int] __lowerCamelCase : Optional[List[int]] = None __lowerCamelCase : Optional[List[int]] = None class A (SCREAMING_SNAKE_CASE ): '''simple docstring''' __lowerCamelCase : Dict = '''train''' __lowerCamelCase : int = '''dev''' __lowerCamelCase : Optional[Any] = '''test''' class A : '''simple docstring''' @staticmethod def a_ ( __lowerCAmelCase : List[Any] , __lowerCAmelCase : Union[Split, str] ) -> List[InputExample]: """simple docstring""" raise NotImplementedError @staticmethod def a_ ( __lowerCAmelCase : str ) -> List[str]: """simple docstring""" raise NotImplementedError @staticmethod def a_ ( __lowerCAmelCase : List[InputExample] , __lowerCAmelCase : List[str] , __lowerCAmelCase : int , __lowerCAmelCase : PreTrainedTokenizer , __lowerCAmelCase : Optional[Any]=False , __lowerCAmelCase : str="[CLS]" , __lowerCAmelCase : Dict=1 , __lowerCAmelCase : Tuple="[SEP]" , __lowerCAmelCase : List[Any]=False , __lowerCAmelCase : List[Any]=False , __lowerCAmelCase : Optional[Any]=0 , __lowerCAmelCase : List[Any]=0 , __lowerCAmelCase : Tuple=-1_00 , __lowerCAmelCase : Union[str, Any]=0 , __lowerCAmelCase : Any=True , ) -> List[InputFeatures]: """simple docstring""" A__ = {label: i for i, label in enumerate(__lowerCAmelCase )} A__ = [] for ex_index, example in enumerate(__lowerCAmelCase ): if ex_index % 1_00_00 == 0: logger.info("""Writing example %d of %d""" , __lowerCAmelCase , len(__lowerCAmelCase ) ) A__ = [] A__ = [] for word, label in zip(example.words , example.labels ): A__ = tokenizer.tokenize(__lowerCAmelCase ) # bert-base-multilingual-cased sometimes output "nothing ([]) when calling tokenize with just a space. if len(__lowerCAmelCase ) > 0: tokens.extend(__lowerCAmelCase ) # Use the real label id for the first token of the word, and padding ids for the remaining tokens label_ids.extend([label_map[label]] + [pad_token_label_id] * (len(__lowerCAmelCase ) - 1) ) # Account for [CLS] and [SEP] with "- 2" and with "- 3" for RoBERTa. A__ = tokenizer.num_special_tokens_to_add() if len(__lowerCAmelCase ) > max_seq_length - special_tokens_count: A__ = tokens[: (max_seq_length - special_tokens_count)] A__ = label_ids[: (max_seq_length - special_tokens_count)] # The convention in BERT is: # (a) For sequence pairs: # tokens: [CLS] is this jack ##son ##ville ? [SEP] no it is not . [SEP] # type_ids: 0 0 0 0 0 0 0 0 1 1 1 1 1 1 # (b) For single sequences: # tokens: [CLS] the dog is hairy . [SEP] # type_ids: 0 0 0 0 0 0 0 # # Where "type_ids" are used to indicate whether this is the first # sequence or the second sequence. The embedding vectors for `type=0` and # `type=1` were learned during pre-training and are added to the wordpiece # embedding vector (and position vector). This is not *strictly* necessary # since the [SEP] token unambiguously separates the sequences, but it makes # it easier for the model to learn the concept of sequences. # # For classification tasks, the first vector (corresponding to [CLS]) is # used as the "sentence vector". Note that this only makes sense because # the entire model is fine-tuned. tokens += [sep_token] label_ids += [pad_token_label_id] if sep_token_extra: # roberta uses an extra separator b/w pairs of sentences tokens += [sep_token] label_ids += [pad_token_label_id] A__ = [sequence_a_segment_id] * len(__lowerCAmelCase ) if cls_token_at_end: tokens += [cls_token] label_ids += [pad_token_label_id] segment_ids += [cls_token_segment_id] else: A__ = [cls_token] + tokens A__ = [pad_token_label_id] + label_ids A__ = [cls_token_segment_id] + segment_ids A__ = tokenizer.convert_tokens_to_ids(__lowerCAmelCase ) # The mask has 1 for real tokens and 0 for padding tokens. Only real # tokens are attended to. A__ = [1 if mask_padding_with_zero else 0] * len(__lowerCAmelCase ) # Zero-pad up to the sequence length. A__ = max_seq_length - len(__lowerCAmelCase ) if pad_on_left: A__ = ([pad_token] * padding_length) + input_ids A__ = ([0 if mask_padding_with_zero else 1] * padding_length) + input_mask A__ = ([pad_token_segment_id] * padding_length) + segment_ids A__ = ([pad_token_label_id] * padding_length) + label_ids else: input_ids += [pad_token] * padding_length input_mask += [0 if mask_padding_with_zero else 1] * padding_length segment_ids += [pad_token_segment_id] * padding_length label_ids += [pad_token_label_id] * padding_length assert len(__lowerCAmelCase ) == max_seq_length assert len(__lowerCAmelCase ) == max_seq_length assert len(__lowerCAmelCase ) == max_seq_length assert len(__lowerCAmelCase ) == max_seq_length if ex_index < 5: logger.info("""*** Example ***""" ) logger.info("""guid: %s""" , example.guid ) logger.info("""tokens: %s""" , """ """.join([str(__lowerCAmelCase ) for x in tokens] ) ) logger.info("""input_ids: %s""" , """ """.join([str(__lowerCAmelCase ) for x in input_ids] ) ) logger.info("""input_mask: %s""" , """ """.join([str(__lowerCAmelCase ) for x in input_mask] ) ) logger.info("""segment_ids: %s""" , """ """.join([str(__lowerCAmelCase ) for x in segment_ids] ) ) logger.info("""label_ids: %s""" , """ """.join([str(__lowerCAmelCase ) for x in label_ids] ) ) if "token_type_ids" not in tokenizer.model_input_names: A__ = None features.append( InputFeatures( input_ids=__lowerCAmelCase , attention_mask=__lowerCAmelCase , token_type_ids=__lowerCAmelCase , label_ids=__lowerCAmelCase ) ) return features if is_torch_available(): import torch from torch import nn from torch.utils.data import Dataset class A (SCREAMING_SNAKE_CASE ): '''simple docstring''' __lowerCamelCase : List[InputFeatures] __lowerCamelCase : int = nn.CrossEntropyLoss().ignore_index def __init__( self : Tuple , __lowerCAmelCase : TokenClassificationTask , __lowerCAmelCase : str , __lowerCAmelCase : PreTrainedTokenizer , __lowerCAmelCase : List[str] , __lowerCAmelCase : str , __lowerCAmelCase : Optional[int] = None , __lowerCAmelCase : List[Any]=False , __lowerCAmelCase : Split = Split.train , ) -> int: """simple docstring""" A__ = os.path.join( __lowerCAmelCase , """cached_{}_{}_{}""".format(mode.value , tokenizer.__class__.__name__ , str(__lowerCAmelCase ) ) , ) # Make sure only the first process in distributed training processes the dataset, # and the others will use the cache. A__ = cached_features_file + """.lock""" with FileLock(__lowerCAmelCase ): if os.path.exists(__lowerCAmelCase ) and not overwrite_cache: logger.info(f'Loading features from cached file {cached_features_file}' ) A__ = torch.load(__lowerCAmelCase ) else: logger.info(f'Creating features from dataset file at {data_dir}' ) A__ = token_classification_task.read_examples_from_file(__lowerCAmelCase , __lowerCAmelCase ) # TODO clean up all this to leverage built-in features of tokenizers A__ = token_classification_task.convert_examples_to_features( __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , cls_token_at_end=bool(model_type in ["""xlnet"""] ) , cls_token=tokenizer.cls_token , cls_token_segment_id=2 if model_type in ["""xlnet"""] else 0 , sep_token=tokenizer.sep_token , sep_token_extra=__lowerCAmelCase , pad_on_left=bool(tokenizer.padding_side == """left""" ) , pad_token=tokenizer.pad_token_id , pad_token_segment_id=tokenizer.pad_token_type_id , pad_token_label_id=self.pad_token_label_id , ) logger.info(f'Saving features into cached file {cached_features_file}' ) torch.save(self.features , __lowerCAmelCase ) def __len__( self : List[str] ) -> Any: """simple docstring""" return len(self.features ) def __getitem__( self : List[str] , __lowerCAmelCase : Tuple ) -> InputFeatures: """simple docstring""" return self.features[i] if is_tf_available(): import tensorflow as tf class A : '''simple docstring''' __lowerCamelCase : List[InputFeatures] __lowerCamelCase : int = -100 def __init__( self : Optional[int] , __lowerCAmelCase : TokenClassificationTask , __lowerCAmelCase : str , __lowerCAmelCase : PreTrainedTokenizer , __lowerCAmelCase : List[str] , __lowerCAmelCase : str , __lowerCAmelCase : Optional[int] = None , __lowerCAmelCase : Optional[int]=False , __lowerCAmelCase : Split = Split.train , ) -> int: """simple docstring""" A__ = token_classification_task.read_examples_from_file(__lowerCAmelCase , __lowerCAmelCase ) # TODO clean up all this to leverage built-in features of tokenizers A__ = token_classification_task.convert_examples_to_features( __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , cls_token_at_end=bool(model_type in ["""xlnet"""] ) , cls_token=tokenizer.cls_token , cls_token_segment_id=2 if model_type in ["""xlnet"""] else 0 , sep_token=tokenizer.sep_token , sep_token_extra=__lowerCAmelCase , pad_on_left=bool(tokenizer.padding_side == """left""" ) , pad_token=tokenizer.pad_token_id , pad_token_segment_id=tokenizer.pad_token_type_id , pad_token_label_id=self.pad_token_label_id , ) def gen(): for ex in self.features: if ex.token_type_ids is None: yield ( {"input_ids": ex.input_ids, "attention_mask": ex.attention_mask}, ex.label_ids, ) else: yield ( { "input_ids": ex.input_ids, "attention_mask": ex.attention_mask, "token_type_ids": ex.token_type_ids, }, ex.label_ids, ) if "token_type_ids" not in tokenizer.model_input_names: A__ = tf.data.Dataset.from_generator( __lowerCAmelCase , ({"""input_ids""": tf.intaa, """attention_mask""": tf.intaa}, tf.intaa) , ( {"""input_ids""": tf.TensorShape([None] ), """attention_mask""": tf.TensorShape([None] )}, tf.TensorShape([None] ), ) , ) else: A__ = tf.data.Dataset.from_generator( __lowerCAmelCase , ({"""input_ids""": tf.intaa, """attention_mask""": tf.intaa, """token_type_ids""": tf.intaa}, tf.intaa) , ( { """input_ids""": tf.TensorShape([None] ), """attention_mask""": tf.TensorShape([None] ), """token_type_ids""": tf.TensorShape([None] ), }, tf.TensorShape([None] ), ) , ) def a_ ( self : Dict ) -> Any: """simple docstring""" A__ = self.dataset.apply(tf.data.experimental.assert_cardinality(len(self.features ) ) ) return self.dataset def __len__( self : Tuple ) -> List[str]: """simple docstring""" return len(self.features ) def __getitem__( self : int , __lowerCAmelCase : Union[str, Any] ) -> InputFeatures: """simple docstring""" return self.features[i]
274
def __lowerCamelCase ( __a :int = 1_0_0_0_0_0_0 ) -> int: """simple docstring""" A__ = limit + 1 A__ = [0] * limit for first_term in range(1 , __a ): for n in range(__a , __a , __a ): A__ = first_term + n / first_term if common_difference % 4: # d must be divisble by 4 continue else: common_difference /= 4 if ( first_term > common_difference and first_term < 4 * common_difference ): # since x,y,z are positive integers frequency[n] += 1 # so z>0 and a>d ,also 4d<a A__ = sum(1 for x in frequency[1:limit] if x == 1_0 ) return count if __name__ == "__main__": print(F'''{solution() = }''')
274
1
import json import os import re import shutil import tempfile import unittest from typing import Tuple from transformers import AddedToken, BatchEncoding, PerceiverTokenizer from transformers.utils import cached_property, is_tf_available, is_torch_available from ...test_tokenization_common import TokenizerTesterMixin if is_torch_available(): A : Tuple = '''pt''' elif is_tf_available(): A : Union[str, Any] = '''tf''' else: A : List[str] = '''jax''' class A (SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' __lowerCamelCase : Optional[int] = PerceiverTokenizer __lowerCamelCase : Optional[int] = False def a_ ( self : Optional[int] ) -> Optional[int]: """simple docstring""" super().setUp() A__ = PerceiverTokenizer() tokenizer.save_pretrained(self.tmpdirname ) @cached_property def a_ ( self : Union[str, Any] ) -> List[str]: """simple docstring""" return PerceiverTokenizer.from_pretrained("""deepmind/language-perceiver""" ) def a_ ( self : Dict , **__lowerCAmelCase : Union[str, Any] ) -> PerceiverTokenizer: """simple docstring""" return self.tokenizer_class.from_pretrained(self.tmpdirname , **__lowerCAmelCase ) def a_ ( self : Union[str, Any] , __lowerCAmelCase : Union[str, Any] , __lowerCAmelCase : List[str]=False , __lowerCAmelCase : Optional[int]=20 , __lowerCAmelCase : Optional[Any]=5 ) -> Tuple[str, list]: """simple docstring""" A__ = [] for i in range(len(__lowerCAmelCase ) ): try: A__ = tokenizer.decode([i] , clean_up_tokenization_spaces=__lowerCAmelCase ) except UnicodeDecodeError: pass toks.append((i, tok) ) A__ = list(filter(lambda __lowerCAmelCase : re.match(R"""^[ a-zA-Z]+$""" , t[1] ) , __lowerCAmelCase ) ) A__ = list(filter(lambda __lowerCAmelCase : [t[0]] == tokenizer.encode(t[1] , add_special_tokens=__lowerCAmelCase ) , __lowerCAmelCase ) ) if max_length is not None and len(__lowerCAmelCase ) > max_length: A__ = toks[:max_length] if min_length is not None and len(__lowerCAmelCase ) < min_length and len(__lowerCAmelCase ) > 0: while len(__lowerCAmelCase ) < min_length: A__ = toks + toks # toks_str = [t[1] for t in toks] A__ = [t[0] for t in toks] # Ensure consistency A__ = tokenizer.decode(__lowerCAmelCase , clean_up_tokenization_spaces=__lowerCAmelCase ) if " " not in output_txt and len(__lowerCAmelCase ) > 1: A__ = ( tokenizer.decode([toks_ids[0]] , clean_up_tokenization_spaces=__lowerCAmelCase ) + """ """ + tokenizer.decode(toks_ids[1:] , clean_up_tokenization_spaces=__lowerCAmelCase ) ) if with_prefix_space: A__ = """ """ + output_txt A__ = tokenizer.encode(__lowerCAmelCase , add_special_tokens=__lowerCAmelCase ) return output_txt, output_ids def a_ ( self : List[Any] ) -> List[str]: """simple docstring""" A__ = self.perceiver_tokenizer A__ = """Unicode €.""" A__ = tokenizer(__lowerCAmelCase ) A__ = [4, 91, 1_16, 1_11, 1_05, 1_17, 1_06, 1_07, 38, 2_32, 1_36, 1_78, 52, 5] self.assertEqual(encoded["""input_ids"""] , __lowerCAmelCase ) # decoding A__ = tokenizer.decode(__lowerCAmelCase ) self.assertEqual(__lowerCAmelCase , """[CLS]Unicode €.[SEP]""" ) A__ = tokenizer("""e è é ê ë""" ) A__ = [4, 1_07, 38, 2_01, 1_74, 38, 2_01, 1_75, 38, 2_01, 1_76, 38, 2_01, 1_77, 5] self.assertEqual(encoded["""input_ids"""] , __lowerCAmelCase ) # decoding A__ = tokenizer.decode(__lowerCAmelCase ) self.assertEqual(__lowerCAmelCase , """[CLS]e è é ê ë[SEP]""" ) # encode/decode, but with `encode` instead of `__call__` self.assertEqual(tokenizer.decode(tokenizer.encode("""e è é ê ë""" ) ) , """[CLS]e è é ê ë[SEP]""" ) def a_ ( self : Tuple ) -> Tuple: """simple docstring""" A__ = self.perceiver_tokenizer A__ = ["""A long paragraph for summarization.""", """Another paragraph for summarization."""] # fmt: off A__ = [4, 71, 38, 1_14, 1_17, 1_16, 1_09, 38, 1_18, 1_03, 1_20, 1_03, 1_09, 1_20, 1_03, 1_18, 1_10, 38, 1_08, 1_17, 1_20, 38, 1_21, 1_23, 1_15, 1_15, 1_03, 1_20, 1_11, 1_28, 1_03, 1_22, 1_11, 1_17, 1_16, 52, 5, 0] # fmt: on A__ = tokenizer(__lowerCAmelCase , padding=__lowerCAmelCase , return_tensors=__lowerCAmelCase ) self.assertIsInstance(__lowerCAmelCase , __lowerCAmelCase ) if FRAMEWORK != "jax": A__ = list(batch.input_ids.numpy()[0] ) else: A__ = list(batch.input_ids.tolist()[0] ) self.assertListEqual(__lowerCAmelCase , __lowerCAmelCase ) self.assertEqual((2, 38) , batch.input_ids.shape ) self.assertEqual((2, 38) , batch.attention_mask.shape ) def a_ ( self : Union[str, Any] ) -> Tuple: """simple docstring""" A__ = self.perceiver_tokenizer A__ = ["""A long paragraph for summarization.""", """Another paragraph for summarization."""] A__ = tokenizer(__lowerCAmelCase , padding=__lowerCAmelCase , return_tensors=__lowerCAmelCase ) # check if input_ids are returned and no decoder_input_ids self.assertIn("""input_ids""" , __lowerCAmelCase ) self.assertIn("""attention_mask""" , __lowerCAmelCase ) self.assertNotIn("""decoder_input_ids""" , __lowerCAmelCase ) self.assertNotIn("""decoder_attention_mask""" , __lowerCAmelCase ) def a_ ( self : Optional[Any] ) -> Optional[Any]: """simple docstring""" A__ = self.perceiver_tokenizer A__ = [ """Summary of the text.""", """Another summary.""", ] A__ = tokenizer( text_target=__lowerCAmelCase , max_length=32 , padding="""max_length""" , truncation=__lowerCAmelCase , return_tensors=__lowerCAmelCase ) self.assertEqual(32 , targets["""input_ids"""].shape[1] ) def a_ ( self : int ) -> Optional[int]: """simple docstring""" A__ = self.get_tokenizers() for tokenizer in tokenizers: with self.subTest(f'{tokenizer.__class__.__name__}' ): self.assertNotEqual(tokenizer.model_max_length , 42 ) # Now let's start the test A__ = self.get_tokenizers() for tokenizer in tokenizers: with self.subTest(f'{tokenizer.__class__.__name__}' ): # Isolate this from the other tests because we save additional tokens/etc A__ = tempfile.mkdtemp() A__ = """ He is very happy, UNwant\u00E9d,running""" A__ = tokenizer.encode(__lowerCAmelCase , add_special_tokens=__lowerCAmelCase ) tokenizer.save_pretrained(__lowerCAmelCase ) A__ = tokenizer.__class__.from_pretrained(__lowerCAmelCase ) A__ = after_tokenizer.encode(__lowerCAmelCase , add_special_tokens=__lowerCAmelCase ) self.assertListEqual(__lowerCAmelCase , __lowerCAmelCase ) shutil.rmtree(__lowerCAmelCase ) A__ = self.get_tokenizers(model_max_length=42 ) for tokenizer in tokenizers: with self.subTest(f'{tokenizer.__class__.__name__}' ): # Isolate this from the other tests because we save additional tokens/etc A__ = tempfile.mkdtemp() A__ = """ He is very happy, UNwant\u00E9d,running""" tokenizer.add_tokens(["""bim""", """bambam"""] ) A__ = tokenizer.additional_special_tokens additional_special_tokens.append("""new_additional_special_token""" ) tokenizer.add_special_tokens({"""additional_special_tokens""": additional_special_tokens} ) A__ = tokenizer.encode(__lowerCAmelCase , add_special_tokens=__lowerCAmelCase ) tokenizer.save_pretrained(__lowerCAmelCase ) A__ = tokenizer.__class__.from_pretrained(__lowerCAmelCase ) A__ = after_tokenizer.encode(__lowerCAmelCase , add_special_tokens=__lowerCAmelCase ) self.assertListEqual(__lowerCAmelCase , __lowerCAmelCase ) self.assertIn("""new_additional_special_token""" , after_tokenizer.additional_special_tokens ) self.assertEqual(after_tokenizer.model_max_length , 42 ) A__ = tokenizer.__class__.from_pretrained(__lowerCAmelCase , model_max_length=43 ) self.assertEqual(tokenizer.model_max_length , 43 ) shutil.rmtree(__lowerCAmelCase ) def a_ ( self : Optional[int] ) -> Any: """simple docstring""" A__ = [] if self.test_slow_tokenizer: tokenizer_list.append((self.tokenizer_class, self.get_tokenizer()) ) if self.test_rust_tokenizer: tokenizer_list.append((self.rust_tokenizer_class, self.get_rust_tokenizer()) ) for tokenizer_class, tokenizer_utils in tokenizer_list: with tempfile.TemporaryDirectory() as tmp_dir: tokenizer_utils.save_pretrained(__lowerCAmelCase ) with open(os.path.join(__lowerCAmelCase , """special_tokens_map.json""" ) , encoding="""utf-8""" ) as json_file: A__ = json.load(__lowerCAmelCase ) with open(os.path.join(__lowerCAmelCase , """tokenizer_config.json""" ) , encoding="""utf-8""" ) as json_file: A__ = json.load(__lowerCAmelCase ) A__ = [f'<extra_id_{i}>' for i in range(1_25 )] A__ = added_tokens_extra_ids + [ """an_additional_special_token""" ] A__ = added_tokens_extra_ids + [ """an_additional_special_token""" ] with open(os.path.join(__lowerCAmelCase , """special_tokens_map.json""" ) , """w""" , encoding="""utf-8""" ) as outfile: json.dump(__lowerCAmelCase , __lowerCAmelCase ) with open(os.path.join(__lowerCAmelCase , """tokenizer_config.json""" ) , """w""" , encoding="""utf-8""" ) as outfile: json.dump(__lowerCAmelCase , __lowerCAmelCase ) # the following checks allow us to verify that our test works as expected, i.e. that the tokenizer takes # into account the new value of additional_special_tokens given in the "tokenizer_config.json" and # "special_tokens_map.json" files A__ = tokenizer_class.from_pretrained( __lowerCAmelCase , ) self.assertIn( """an_additional_special_token""" , tokenizer_without_change_in_init.additional_special_tokens ) self.assertEqual( ["""an_additional_special_token"""] , tokenizer_without_change_in_init.convert_ids_to_tokens( tokenizer_without_change_in_init.convert_tokens_to_ids(["""an_additional_special_token"""] ) ) , ) # Now we test that we can change the value of additional_special_tokens in the from_pretrained A__ = added_tokens_extra_ids + [AddedToken("""a_new_additional_special_token""" , lstrip=__lowerCAmelCase )] A__ = tokenizer_class.from_pretrained( __lowerCAmelCase , additional_special_tokens=__lowerCAmelCase , ) self.assertIn("""a_new_additional_special_token""" , tokenizer.additional_special_tokens ) self.assertEqual( ["""a_new_additional_special_token"""] , tokenizer.convert_ids_to_tokens( tokenizer.convert_tokens_to_ids(["""a_new_additional_special_token"""] ) ) , ) def a_ ( self : int ) -> int: """simple docstring""" A__ = self.perceiver_tokenizer self.assertEqual(tokenizer.decode([1_78] ) , """�""" ) def a_ ( self : str ) -> Optional[Any]: """simple docstring""" pass def a_ ( self : str ) -> str: """simple docstring""" pass def a_ ( self : Tuple ) -> Tuple: """simple docstring""" pass def a_ ( self : List[Any] ) -> str: """simple docstring""" pass def a_ ( self : Tuple ) -> str: """simple docstring""" A__ = self.get_tokenizers(fast=__lowerCAmelCase , do_lower_case=__lowerCAmelCase ) for tokenizer in tokenizers: with self.subTest(f'{tokenizer.__class__.__name__}' ): A__ = ["""[CLS]""", """t""", """h""", """i""", """s""", """ """, """i""", """s""", """ """, """a""", """ """, """t""", """e""", """s""", """t""", """[SEP]"""] A__ = tokenizer.convert_tokens_to_string(__lowerCAmelCase ) self.assertIsInstance(__lowerCAmelCase , __lowerCAmelCase )
274
class A (SCREAMING_SNAKE_CASE ): '''simple docstring''' pass class A (SCREAMING_SNAKE_CASE ): '''simple docstring''' pass class A : '''simple docstring''' def __init__( self : List[Any] ) -> str: """simple docstring""" A__ = [ [], [], [], ] def a_ ( self : Dict , __lowerCAmelCase : int , __lowerCAmelCase : int ) -> None: """simple docstring""" try: if len(self.queues[priority] ) >= 1_00: raise OverflowError("""Maximum queue size is 100""" ) self.queues[priority].append(__lowerCAmelCase ) except IndexError: raise ValueError("""Valid priorities are 0, 1, and 2""" ) def a_ ( self : Optional[Any] ) -> int: """simple docstring""" for queue in self.queues: if queue: return queue.pop(0 ) raise UnderFlowError("""All queues are empty""" ) def __str__( self : Tuple ) -> str: """simple docstring""" return "\n".join(f'Priority {i}: {q}' for i, q in enumerate(self.queues ) ) class A : '''simple docstring''' def __init__( self : int ) -> str: """simple docstring""" A__ = [] def a_ ( self : int , __lowerCAmelCase : int ) -> None: """simple docstring""" if len(self.queue ) == 1_00: raise OverFlowError("""Maximum queue size is 100""" ) self.queue.append(__lowerCAmelCase ) def a_ ( self : List[str] ) -> int: """simple docstring""" if not self.queue: raise UnderFlowError("""The queue is empty""" ) else: A__ = min(self.queue ) self.queue.remove(__lowerCAmelCase ) return data def __str__( self : List[Any] ) -> str: """simple docstring""" return str(self.queue ) def __lowerCamelCase ( ) -> Optional[Any]: """simple docstring""" A__ = FixedPriorityQueue() fpq.enqueue(0 , 1_0 ) fpq.enqueue(1 , 7_0 ) fpq.enqueue(0 , 1_0_0 ) fpq.enqueue(2 , 1 ) fpq.enqueue(2 , 5 ) fpq.enqueue(1 , 7 ) fpq.enqueue(2 , 4 ) fpq.enqueue(1 , 6_4 ) fpq.enqueue(0 , 1_2_8 ) print(__a ) print(fpq.dequeue() ) print(fpq.dequeue() ) print(fpq.dequeue() ) print(fpq.dequeue() ) print(fpq.dequeue() ) print(__a ) print(fpq.dequeue() ) print(fpq.dequeue() ) print(fpq.dequeue() ) print(fpq.dequeue() ) print(fpq.dequeue() ) def __lowerCamelCase ( ) -> int: """simple docstring""" A__ = ElementPriorityQueue() epq.enqueue(1_0 ) epq.enqueue(7_0 ) epq.enqueue(1_0_0 ) epq.enqueue(1 ) epq.enqueue(5 ) epq.enqueue(7 ) epq.enqueue(4 ) epq.enqueue(6_4 ) epq.enqueue(1_2_8 ) print(__a ) print(epq.dequeue() ) print(epq.dequeue() ) print(epq.dequeue() ) print(epq.dequeue() ) print(epq.dequeue() ) print(__a ) print(epq.dequeue() ) print(epq.dequeue() ) print(epq.dequeue() ) print(epq.dequeue() ) print(epq.dequeue() ) if __name__ == "__main__": fixed_priority_queue() element_priority_queue()
274
1
class A (SCREAMING_SNAKE_CASE ): '''simple docstring''' pass class A (SCREAMING_SNAKE_CASE ): '''simple docstring''' pass class A : '''simple docstring''' def __init__( self : List[Any] ) -> str: """simple docstring""" A__ = [ [], [], [], ] def a_ ( self : Dict , __lowerCAmelCase : int , __lowerCAmelCase : int ) -> None: """simple docstring""" try: if len(self.queues[priority] ) >= 1_00: raise OverflowError("""Maximum queue size is 100""" ) self.queues[priority].append(__lowerCAmelCase ) except IndexError: raise ValueError("""Valid priorities are 0, 1, and 2""" ) def a_ ( self : Optional[Any] ) -> int: """simple docstring""" for queue in self.queues: if queue: return queue.pop(0 ) raise UnderFlowError("""All queues are empty""" ) def __str__( self : Tuple ) -> str: """simple docstring""" return "\n".join(f'Priority {i}: {q}' for i, q in enumerate(self.queues ) ) class A : '''simple docstring''' def __init__( self : int ) -> str: """simple docstring""" A__ = [] def a_ ( self : int , __lowerCAmelCase : int ) -> None: """simple docstring""" if len(self.queue ) == 1_00: raise OverFlowError("""Maximum queue size is 100""" ) self.queue.append(__lowerCAmelCase ) def a_ ( self : List[str] ) -> int: """simple docstring""" if not self.queue: raise UnderFlowError("""The queue is empty""" ) else: A__ = min(self.queue ) self.queue.remove(__lowerCAmelCase ) return data def __str__( self : List[Any] ) -> str: """simple docstring""" return str(self.queue ) def __lowerCamelCase ( ) -> Optional[Any]: """simple docstring""" A__ = FixedPriorityQueue() fpq.enqueue(0 , 1_0 ) fpq.enqueue(1 , 7_0 ) fpq.enqueue(0 , 1_0_0 ) fpq.enqueue(2 , 1 ) fpq.enqueue(2 , 5 ) fpq.enqueue(1 , 7 ) fpq.enqueue(2 , 4 ) fpq.enqueue(1 , 6_4 ) fpq.enqueue(0 , 1_2_8 ) print(__a ) print(fpq.dequeue() ) print(fpq.dequeue() ) print(fpq.dequeue() ) print(fpq.dequeue() ) print(fpq.dequeue() ) print(__a ) print(fpq.dequeue() ) print(fpq.dequeue() ) print(fpq.dequeue() ) print(fpq.dequeue() ) print(fpq.dequeue() ) def __lowerCamelCase ( ) -> int: """simple docstring""" A__ = ElementPriorityQueue() epq.enqueue(1_0 ) epq.enqueue(7_0 ) epq.enqueue(1_0_0 ) epq.enqueue(1 ) epq.enqueue(5 ) epq.enqueue(7 ) epq.enqueue(4 ) epq.enqueue(6_4 ) epq.enqueue(1_2_8 ) print(__a ) print(epq.dequeue() ) print(epq.dequeue() ) print(epq.dequeue() ) print(epq.dequeue() ) print(epq.dequeue() ) print(__a ) print(epq.dequeue() ) print(epq.dequeue() ) print(epq.dequeue() ) print(epq.dequeue() ) print(epq.dequeue() ) if __name__ == "__main__": fixed_priority_queue() element_priority_queue()
274
import json import os import shutil import tempfile import unittest import numpy as np import pytest from transformers import CLIPTokenizer, CLIPTokenizerFast from transformers.models.clip.tokenization_clip import VOCAB_FILES_NAMES from transformers.testing_utils import require_vision from transformers.utils import IMAGE_PROCESSOR_NAME, is_vision_available if is_vision_available(): from PIL import Image from transformers import CLIPImageProcessor, CLIPProcessor @require_vision class A (unittest.TestCase ): '''simple docstring''' def a_ ( self : Union[str, Any] ) -> Dict: """simple docstring""" A__ = tempfile.mkdtemp() # fmt: off A__ = ["""l""", """o""", """w""", """e""", """r""", """s""", """t""", """i""", """d""", """n""", """lo""", """l</w>""", """w</w>""", """r</w>""", """t</w>""", """low</w>""", """er</w>""", """lowest</w>""", """newer</w>""", """wider""", """<unk>""", """<|startoftext|>""", """<|endoftext|>"""] # fmt: on A__ = dict(zip(__lowerCAmelCase , range(len(__lowerCAmelCase ) ) ) ) A__ = ["""#version: 0.2""", """l o""", """lo w</w>""", """e r</w>""", """"""] A__ = {"""unk_token""": """<unk>"""} A__ = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES["""vocab_file"""] ) A__ = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES["""merges_file"""] ) with open(self.vocab_file , """w""" , encoding="""utf-8""" ) as fp: fp.write(json.dumps(__lowerCAmelCase ) + """\n""" ) with open(self.merges_file , """w""" , encoding="""utf-8""" ) as fp: fp.write("""\n""".join(__lowerCAmelCase ) ) A__ = { """do_resize""": True, """size""": 20, """do_center_crop""": True, """crop_size""": 18, """do_normalize""": True, """image_mean""": [0.4_8_1_4_5_4_6_6, 0.4_5_7_8_2_7_5, 0.4_0_8_2_1_0_7_3], """image_std""": [0.2_6_8_6_2_9_5_4, 0.2_6_1_3_0_2_5_8, 0.2_7_5_7_7_7_1_1], } A__ = os.path.join(self.tmpdirname , __lowerCAmelCase ) with open(self.image_processor_file , """w""" , encoding="""utf-8""" ) as fp: json.dump(__lowerCAmelCase , __lowerCAmelCase ) def a_ ( self : Tuple , **__lowerCAmelCase : Dict ) -> str: """simple docstring""" return CLIPTokenizer.from_pretrained(self.tmpdirname , **__lowerCAmelCase ) def a_ ( self : Union[str, Any] , **__lowerCAmelCase : Dict ) -> List[str]: """simple docstring""" return CLIPTokenizerFast.from_pretrained(self.tmpdirname , **__lowerCAmelCase ) def a_ ( self : List[str] , **__lowerCAmelCase : Optional[Any] ) -> Dict: """simple docstring""" return CLIPImageProcessor.from_pretrained(self.tmpdirname , **__lowerCAmelCase ) def a_ ( self : str ) -> Dict: """simple docstring""" shutil.rmtree(self.tmpdirname ) def a_ ( self : str ) -> Any: """simple docstring""" A__ = [np.random.randint(2_55 , size=(3, 30, 4_00) , dtype=np.uinta )] A__ = [Image.fromarray(np.moveaxis(__lowerCAmelCase , 0 , -1 ) ) for x in image_inputs] return image_inputs def a_ ( self : Optional[int] ) -> Tuple: """simple docstring""" A__ = self.get_tokenizer() A__ = self.get_rust_tokenizer() A__ = self.get_image_processor() A__ = CLIPProcessor(tokenizer=__lowerCAmelCase , image_processor=__lowerCAmelCase ) processor_slow.save_pretrained(self.tmpdirname ) A__ = CLIPProcessor.from_pretrained(self.tmpdirname , use_fast=__lowerCAmelCase ) A__ = CLIPProcessor(tokenizer=__lowerCAmelCase , image_processor=__lowerCAmelCase ) processor_fast.save_pretrained(self.tmpdirname ) A__ = CLIPProcessor.from_pretrained(self.tmpdirname ) self.assertEqual(processor_slow.tokenizer.get_vocab() , tokenizer_slow.get_vocab() ) self.assertEqual(processor_fast.tokenizer.get_vocab() , tokenizer_fast.get_vocab() ) self.assertEqual(tokenizer_slow.get_vocab() , tokenizer_fast.get_vocab() ) self.assertIsInstance(processor_slow.tokenizer , __lowerCAmelCase ) self.assertIsInstance(processor_fast.tokenizer , __lowerCAmelCase ) self.assertEqual(processor_slow.image_processor.to_json_string() , image_processor.to_json_string() ) self.assertEqual(processor_fast.image_processor.to_json_string() , image_processor.to_json_string() ) self.assertIsInstance(processor_slow.image_processor , __lowerCAmelCase ) self.assertIsInstance(processor_fast.image_processor , __lowerCAmelCase ) def a_ ( self : Optional[Any] ) -> Union[str, Any]: """simple docstring""" A__ = CLIPProcessor(tokenizer=self.get_tokenizer() , image_processor=self.get_image_processor() ) processor.save_pretrained(self.tmpdirname ) A__ = self.get_tokenizer(bos_token="""(BOS)""" , eos_token="""(EOS)""" ) A__ = self.get_image_processor(do_normalize=__lowerCAmelCase , padding_value=1.0 ) A__ = CLIPProcessor.from_pretrained( self.tmpdirname , bos_token="""(BOS)""" , eos_token="""(EOS)""" , do_normalize=__lowerCAmelCase , padding_value=1.0 ) self.assertEqual(processor.tokenizer.get_vocab() , tokenizer_add_kwargs.get_vocab() ) self.assertIsInstance(processor.tokenizer , __lowerCAmelCase ) self.assertEqual(processor.image_processor.to_json_string() , image_processor_add_kwargs.to_json_string() ) self.assertIsInstance(processor.image_processor , __lowerCAmelCase ) def a_ ( self : List[Any] ) -> Dict: """simple docstring""" A__ = self.get_image_processor() A__ = self.get_tokenizer() A__ = CLIPProcessor(tokenizer=__lowerCAmelCase , image_processor=__lowerCAmelCase ) A__ = self.prepare_image_inputs() A__ = image_processor(__lowerCAmelCase , return_tensors="""np""" ) A__ = processor(images=__lowerCAmelCase , return_tensors="""np""" ) for key in input_image_proc.keys(): self.assertAlmostEqual(input_image_proc[key].sum() , input_processor[key].sum() , delta=1e-2 ) def a_ ( self : Optional[Any] ) -> Any: """simple docstring""" A__ = self.get_image_processor() A__ = self.get_tokenizer() A__ = CLIPProcessor(tokenizer=__lowerCAmelCase , image_processor=__lowerCAmelCase ) A__ = """lower newer""" A__ = processor(text=__lowerCAmelCase ) A__ = tokenizer(__lowerCAmelCase ) for key in encoded_tok.keys(): self.assertListEqual(encoded_tok[key] , encoded_processor[key] ) def a_ ( self : Union[str, Any] ) -> Dict: """simple docstring""" A__ = self.get_image_processor() A__ = self.get_tokenizer() A__ = CLIPProcessor(tokenizer=__lowerCAmelCase , image_processor=__lowerCAmelCase ) A__ = """lower newer""" A__ = self.prepare_image_inputs() A__ = processor(text=__lowerCAmelCase , images=__lowerCAmelCase ) self.assertListEqual(list(inputs.keys() ) , ["""input_ids""", """attention_mask""", """pixel_values"""] ) # test if it raises when no input is passed with pytest.raises(__lowerCAmelCase ): processor() def a_ ( self : Tuple ) -> str: """simple docstring""" A__ = self.get_image_processor() A__ = self.get_tokenizer() A__ = CLIPProcessor(tokenizer=__lowerCAmelCase , image_processor=__lowerCAmelCase ) A__ = [[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]] A__ = processor.batch_decode(__lowerCAmelCase ) A__ = tokenizer.batch_decode(__lowerCAmelCase ) self.assertListEqual(__lowerCAmelCase , __lowerCAmelCase ) def a_ ( self : Optional[int] ) -> str: """simple docstring""" A__ = self.get_image_processor() A__ = self.get_tokenizer() A__ = CLIPProcessor(tokenizer=__lowerCAmelCase , image_processor=__lowerCAmelCase ) A__ = """lower newer""" A__ = self.prepare_image_inputs() A__ = processor(text=__lowerCAmelCase , images=__lowerCAmelCase ) self.assertListEqual(list(inputs.keys() ) , processor.model_input_names )
274
1
import copy import os import tempfile from unittest import TestCase from unittest.mock import patch import numpy as np import pyarrow as pa import pyarrow.parquet as pq import pytest from datasets.arrow_writer import ArrowWriter, OptimizedTypedSequence, ParquetWriter, TypedSequence from datasets.features import ArrayaD, ClassLabel, Features, Image, Value from datasets.features.features import ArrayaDExtensionType, cast_to_python_objects from datasets.keyhash import DuplicatedKeysError, InvalidKeyError from .utils import require_pil class A (SCREAMING_SNAKE_CASE ): '''simple docstring''' def a_ ( self : Optional[int] ) -> Dict: """simple docstring""" A__ = pa.array(TypedSequence([1, 2, 3] ) ) self.assertEqual(arr.type , pa.intaa() ) def a_ ( self : Dict ) -> Union[str, Any]: """simple docstring""" with self.assertRaises(__lowerCAmelCase ): A__ = pa.array(TypedSequence([1, 2, 3] ) , type=pa.intaa() ) def a_ ( self : int ) -> Any: """simple docstring""" with self.assertRaises(__lowerCAmelCase ): A__ = pa.array(TypedSequence([1, 2, 3] , try_type=Value("""bool""" ) , type=Value("""int64""" ) ) ) def a_ ( self : List[Any] ) -> Optional[int]: """simple docstring""" A__ = pa.array(TypedSequence([1, 2, 3] , type=Value("""int32""" ) ) ) self.assertEqual(arr.type , pa.intaa() ) def a_ ( self : Dict ) -> str: """simple docstring""" with self.assertRaises((TypeError, pa.lib.ArrowInvalid) ): A__ = pa.array(TypedSequence(["""foo""", """bar"""] , type=Value("""int64""" ) ) ) def a_ ( self : Dict ) -> Dict: """simple docstring""" A__ = pa.array(TypedSequence([1, 2, 3] , try_type=Value("""int32""" ) ) ) self.assertEqual(arr.type , pa.intaa() ) def a_ ( self : Union[str, Any] ) -> Any: """simple docstring""" A__ = pa.array(TypedSequence(["""foo""", """bar"""] , try_type=Value("""int64""" ) ) ) self.assertEqual(arr.type , pa.string() ) def a_ ( self : Dict ) -> Dict: """simple docstring""" A__ = pa.array(TypedSequence([[[1, 2, 3]]] , type=ArrayaD((1, 3) , """int64""" ) ) ) self.assertEqual(arr.type , ArrayaDExtensionType((1, 3) , """int64""" ) ) def a_ ( self : Dict ) -> str: """simple docstring""" with self.assertRaises((TypeError, pa.lib.ArrowInvalid) ): A__ = pa.array(TypedSequence(["""foo""", """bar"""] , type=ArrayaD((1, 3) , """int64""" ) ) ) def a_ ( self : List[str] ) -> str: """simple docstring""" A__ = pa.array(TypedSequence([[[1, 2, 3]]] , try_type=ArrayaD((1, 3) , """int64""" ) ) ) self.assertEqual(arr.type , ArrayaDExtensionType((1, 3) , """int64""" ) ) def a_ ( self : List[str] ) -> List[Any]: """simple docstring""" A__ = pa.array(TypedSequence(["""foo""", """bar"""] , try_type=ArrayaD((1, 3) , """int64""" ) ) ) self.assertEqual(arr.type , pa.string() ) @require_pil def a_ ( self : str ) -> str: """simple docstring""" import PIL.Image A__ = PIL.Image.fromarray(np.arange(10 , dtype=np.uinta ).reshape(2 , 5 ) ) with patch( """datasets.arrow_writer.cast_to_python_objects""" , side_effect=__lowerCAmelCase ) as mock_cast_to_python_objects: A__ = pa.array(TypedSequence([{"""path""": None, """bytes""": B"""image_bytes"""}, pil_image] , type=Image() ) ) A__ , A__ = mock_cast_to_python_objects.call_args_list[-1] self.assertIn("""optimize_list_casting""" , __lowerCAmelCase ) self.assertFalse(kwargs["""optimize_list_casting"""] ) def __lowerCamelCase ( __a :Optional[Any] , __a :int ) -> Optional[Any]: """simple docstring""" A__ = pa.BufferReader(__a ) if isinstance(__a , pa.Buffer ) else pa.memory_map(__a ) A__ = pa.ipc.open_stream(__a ) A__ = f.read_all() assert len(pa_table.to_batches() ) == expected_num_chunks assert pa_table.to_pydict() == {"col_1": ["foo", "bar"], "col_2": [1, 2]} del pa_table @pytest.mark.parametrize("""writer_batch_size""" , [None, 1, 1_0] ) @pytest.mark.parametrize( """fields""" , [None, {"""col_1""": pa.string(), """col_2""": pa.intaa()}, {"""col_1""": pa.string(), """col_2""": pa.intaa()}] ) def __lowerCamelCase ( __a :Dict , __a :Optional[Any] ) -> List[str]: """simple docstring""" A__ = pa.BufferOutputStream() A__ = pa.schema(__a ) if fields else None with ArrowWriter(stream=__a , schema=__a , writer_batch_size=__a ) as writer: writer.write({"""col_1""": """foo""", """col_2""": 1} ) writer.write({"""col_1""": """bar""", """col_2""": 2} ) A__ , A__ = writer.finalize() assert num_examples == 2 assert num_bytes > 0 if not fields: A__ = {"""col_1""": pa.string(), """col_2""": pa.intaa()} assert writer._schema == pa.schema(__a , metadata=writer._schema.metadata ) _check_output(output.getvalue() , expected_num_chunks=num_examples if writer_batch_size == 1 else 1 ) def __lowerCamelCase ( ) -> int: """simple docstring""" A__ = pa.BufferOutputStream() A__ = Features({"""labels""": ClassLabel(names=["""neg""", """pos"""] )} ) with ArrowWriter(stream=__a , features=__a ) as writer: writer.write({"""labels""": 0} ) writer.write({"""labels""": 1} ) A__ , A__ = writer.finalize() assert num_examples == 2 assert num_bytes > 0 assert writer._schema == features.arrow_schema assert writer._schema.metadata == features.arrow_schema.metadata A__ = pa.BufferReader(output.getvalue() ) A__ = pa.ipc.open_stream(__a ) A__ = f.read_all() A__ = pa_table.schema assert pa_table.num_rows == 2 assert schema == features.arrow_schema assert schema.metadata == features.arrow_schema.metadata assert features == Features.from_arrow_schema(__a ) @pytest.mark.parametrize("""writer_batch_size""" , [None, 1, 1_0] ) def __lowerCamelCase ( __a :int ) -> Dict: """simple docstring""" A__ = pa.BufferOutputStream() with ArrowWriter( stream=__a , writer_batch_size=__a , hash_salt="""split_name""" , check_duplicates=__a , ) as writer: with pytest.raises(__a ): writer.write({"""col_1""": """foo""", """col_2""": 1} , key=[1, 2] ) A__ , A__ = writer.finalize() @pytest.mark.parametrize("""writer_batch_size""" , [None, 2, 1_0] ) def __lowerCamelCase ( __a :str ) -> Optional[int]: """simple docstring""" A__ = pa.BufferOutputStream() with ArrowWriter( stream=__a , writer_batch_size=__a , hash_salt="""split_name""" , check_duplicates=__a , ) as writer: with pytest.raises(__a ): writer.write({"""col_1""": """foo""", """col_2""": 1} , key=1_0 ) writer.write({"""col_1""": """bar""", """col_2""": 2} , key=1_0 ) A__ , A__ = writer.finalize() @pytest.mark.parametrize("""writer_batch_size""" , [None, 2, 1_0] ) def __lowerCamelCase ( __a :Union[str, Any] ) -> Optional[int]: """simple docstring""" A__ = pa.BufferOutputStream() with ArrowWriter( stream=__a , writer_batch_size=__a , hash_salt="""split_name""" , check_duplicates=__a , ) as writer: writer.write({"""col_1""": """foo""", """col_2""": 1} , key=1 ) writer.write({"""col_1""": """bar""", """col_2""": 2} , key=2 ) A__ , A__ = writer.finalize() assert num_examples == 2 assert num_bytes > 0 _check_output(output.getvalue() , expected_num_chunks=num_examples if writer_batch_size == 1 else 1 ) @pytest.mark.parametrize("""writer_batch_size""" , [None, 1, 1_0] ) @pytest.mark.parametrize( """fields""" , [None, {"""col_1""": pa.string(), """col_2""": pa.intaa()}, {"""col_1""": pa.string(), """col_2""": pa.intaa()}] ) def __lowerCamelCase ( __a :Any , __a :Any ) -> Optional[int]: """simple docstring""" A__ = pa.BufferOutputStream() A__ = pa.schema(__a ) if fields else None with ArrowWriter(stream=__a , schema=__a , writer_batch_size=__a ) as writer: writer.write_batch({"""col_1""": ["""foo""", """bar"""], """col_2""": [1, 2]} ) writer.write_batch({"""col_1""": [], """col_2""": []} ) A__ , A__ = writer.finalize() assert num_examples == 2 assert num_bytes > 0 if not fields: A__ = {"""col_1""": pa.string(), """col_2""": pa.intaa()} assert writer._schema == pa.schema(__a , metadata=writer._schema.metadata ) _check_output(output.getvalue() , expected_num_chunks=num_examples if writer_batch_size == 1 else 1 ) @pytest.mark.parametrize("""writer_batch_size""" , [None, 1, 1_0] ) @pytest.mark.parametrize( """fields""" , [None, {"""col_1""": pa.string(), """col_2""": pa.intaa()}, {"""col_1""": pa.string(), """col_2""": pa.intaa()}] ) def __lowerCamelCase ( __a :Tuple , __a :Optional[int] ) -> Union[str, Any]: """simple docstring""" A__ = pa.BufferOutputStream() A__ = pa.schema(__a ) if fields else None with ArrowWriter(stream=__a , schema=__a , writer_batch_size=__a ) as writer: writer.write_table(pa.Table.from_pydict({"""col_1""": ["""foo""", """bar"""], """col_2""": [1, 2]} ) ) A__ , A__ = writer.finalize() assert num_examples == 2 assert num_bytes > 0 if not fields: A__ = {"""col_1""": pa.string(), """col_2""": pa.intaa()} assert writer._schema == pa.schema(__a , metadata=writer._schema.metadata ) _check_output(output.getvalue() , expected_num_chunks=num_examples if writer_batch_size == 1 else 1 ) @pytest.mark.parametrize("""writer_batch_size""" , [None, 1, 1_0] ) @pytest.mark.parametrize( """fields""" , [None, {"""col_1""": pa.string(), """col_2""": pa.intaa()}, {"""col_1""": pa.string(), """col_2""": pa.intaa()}] ) def __lowerCamelCase ( __a :List[str] , __a :str ) -> Tuple: """simple docstring""" A__ = pa.BufferOutputStream() A__ = pa.schema(__a ) if fields else None with ArrowWriter(stream=__a , schema=__a , writer_batch_size=__a ) as writer: writer.write_row(pa.Table.from_pydict({"""col_1""": ["""foo"""], """col_2""": [1]} ) ) writer.write_row(pa.Table.from_pydict({"""col_1""": ["""bar"""], """col_2""": [2]} ) ) A__ , A__ = writer.finalize() assert num_examples == 2 assert num_bytes > 0 if not fields: A__ = {"""col_1""": pa.string(), """col_2""": pa.intaa()} assert writer._schema == pa.schema(__a , metadata=writer._schema.metadata ) _check_output(output.getvalue() , expected_num_chunks=num_examples if writer_batch_size == 1 else 1 ) def __lowerCamelCase ( ) -> Tuple: """simple docstring""" with tempfile.TemporaryDirectory() as tmp_dir: A__ = {"""col_1""": pa.string(), """col_2""": pa.intaa()} A__ = os.path.join(__a , """test.arrow""" ) with ArrowWriter(path=__a , schema=pa.schema(__a ) ) as writer: writer.write_batch({"""col_1""": ["""foo""", """bar"""], """col_2""": [1, 2]} ) A__ , A__ = writer.finalize() assert num_examples == 2 assert num_bytes > 0 assert writer._schema == pa.schema(__a , metadata=writer._schema.metadata ) _check_output(__a , 1 ) def __lowerCamelCase ( __a :Tuple ) -> List[str]: """simple docstring""" if pa.types.is_list(__a ): return get_base_dtype(arr_type.value_type ) else: return arr_type def __lowerCamelCase ( __a :Any , __a :Optional[int] ) -> int: """simple docstring""" if isinstance(lst[0] , __a ): change_first_primitive_element_in_list(lst[0] , __a ) else: A__ = value @pytest.mark.parametrize("""optimized_int_type, expected_dtype""" , [(None, pa.intaa()), (Value("""int32""" ), pa.intaa())] ) @pytest.mark.parametrize("""sequence""" , [[1, 2, 3], [[1, 2, 3]], [[[1, 2, 3]]]] ) def __lowerCamelCase ( __a :str , __a :Dict , __a :Dict ) -> Any: """simple docstring""" A__ = pa.array(TypedSequence(__a , optimized_int_type=__a ) ) assert get_base_dtype(arr.type ) == expected_dtype @pytest.mark.parametrize( """col, expected_dtype""" , [ ("""attention_mask""", pa.inta()), ("""special_tokens_mask""", pa.inta()), ("""token_type_ids""", pa.inta()), ("""input_ids""", pa.intaa()), ("""other""", pa.intaa()), ] , ) @pytest.mark.parametrize("""sequence""" , [[1, 2, 3], [[1, 2, 3]], [[[1, 2, 3]]]] ) def __lowerCamelCase ( __a :Tuple , __a :List[Any] , __a :Tuple ) -> Tuple: """simple docstring""" A__ = pa.array(OptimizedTypedSequence(__a , col=__a ) ) assert get_base_dtype(arr.type ) == expected_dtype # not in range if col != "other": # avoids errors due to in-place modifications A__ = copy.deepcopy(__a ) A__ = np.iinfo(expected_dtype.to_pandas_dtype() ).max + 1 change_first_primitive_element_in_list(__a , __a ) A__ = pa.array(OptimizedTypedSequence(__a , col=__a ) ) assert get_base_dtype(arr.type ) == pa.intaa() @pytest.mark.parametrize("""raise_exception""" , [False, True] ) def __lowerCamelCase ( __a :Optional[Any] , __a :int ) -> List[str]: """simple docstring""" A__ = str(tmp_path / """dataset-train.arrow""" ) try: with ArrowWriter(path=__a ) as writer: if raise_exception: raise pa.lib.ArrowInvalid() else: writer.stream.close() except pa.lib.ArrowInvalid: pass finally: assert writer.stream.closed def __lowerCamelCase ( __a :Tuple ) -> List[str]: """simple docstring""" A__ = """mock://dataset-train.arrow""" with ArrowWriter(path=__a , storage_options=mockfs.storage_options ) as writer: assert isinstance(writer._fs , type(__a ) ) assert writer._fs.storage_options == mockfs.storage_options writer.write({"""col_1""": """foo""", """col_2""": 1} ) writer.write({"""col_1""": """bar""", """col_2""": 2} ) A__ , A__ = writer.finalize() assert num_examples == 2 assert num_bytes > 0 assert mockfs.exists(__a ) def __lowerCamelCase ( ) -> int: """simple docstring""" A__ = pa.BufferOutputStream() with ParquetWriter(stream=__a ) as writer: writer.write({"""col_1""": """foo""", """col_2""": 1} ) writer.write({"""col_1""": """bar""", """col_2""": 2} ) A__ , A__ = writer.finalize() assert num_examples == 2 assert num_bytes > 0 A__ = pa.BufferReader(output.getvalue() ) A__ = pq.read_table(__a ) assert pa_table.to_pydict() == {"col_1": ["foo", "bar"], "col_2": [1, 2]} @require_pil @pytest.mark.parametrize("""embed_local_files""" , [False, True] ) def __lowerCamelCase ( __a :Dict , __a :Dict ) -> Union[str, Any]: """simple docstring""" import PIL.Image A__ = str(tmp_path / """test_image_rgb.jpg""" ) PIL.Image.fromarray(np.zeros((5, 5) , dtype=np.uinta ) ).save(__a , format="""png""" ) A__ = pa.BufferOutputStream() with ParquetWriter( stream=__a , features=Features({"""image""": Image()} ) , embed_local_files=__a ) as writer: writer.write({"""image""": image_path} ) writer.finalize() A__ = pa.BufferReader(output.getvalue() ) A__ = pq.read_table(__a ) A__ = pa_table.to_pydict() if embed_local_files: assert isinstance(out["""image"""][0]["""path"""] , __a ) with open(__a , """rb""" ) as f: assert out["image"][0]["bytes"] == f.read() else: assert out["image"][0]["path"] == image_path assert out["image"][0]["bytes"] is None def __lowerCamelCase ( ) -> Optional[Any]: """simple docstring""" A__ = pa.schema([pa.field("""col_1""" , pa.string() , nullable=__a )] ) A__ = pa.BufferOutputStream() with ArrowWriter(stream=__a ) as writer: writer._build_writer(inferred_schema=__a ) assert writer._schema == pa.schema([pa.field("""col_1""" , pa.string() )] )
274
from multiprocessing import Lock, Pipe, Process # lock used to ensure that two processes do not access a pipe at the same time A : Dict = Lock() def __lowerCamelCase ( __a :Dict , __a :List[str] , __a :Optional[int] , __a :Optional[int] , __a :Optional[Any] , __a :Optional[int] , __a :int ) -> Dict: """simple docstring""" global process_lock # we perform n swaps since after n swaps we know we are sorted # we *could* stop early if we are sorted already, but it takes as long to # find out we are sorted as it does to sort the list with this algorithm for i in range(0 , 1_0 ): if (i + position) % 2 == 0 and r_send is not None: # send your value to your right neighbor process_lock.acquire() r_send[1].send(__a ) process_lock.release() # receive your right neighbor's value process_lock.acquire() A__ = rr_cv[0].recv() process_lock.release() # take the lower value since you are on the left A__ = min(__a , __a ) elif (i + position) % 2 != 0 and l_send is not None: # send your value to your left neighbor process_lock.acquire() l_send[1].send(__a ) process_lock.release() # receive your left neighbor's value process_lock.acquire() A__ = lr_cv[0].recv() process_lock.release() # take the higher value since you are on the right A__ = max(__a , __a ) # after all swaps are performed, send the values back to main result_pipe[1].send(__a ) def __lowerCamelCase ( __a :List[str] ) -> int: """simple docstring""" A__ = [] A__ = [] # initialize the list of pipes where the values will be retrieved for _ in arr: result_pipe.append(Pipe() ) # creates the processes # the first and last process only have one neighbor so they are made outside # of the loop A__ = Pipe() A__ = Pipe() process_array_.append( Process( target=__a , args=(0, arr[0], None, temp_rs, None, temp_rr, result_pipe[0]) , ) ) A__ = temp_rs A__ = temp_rr for i in range(1 , len(__a ) - 1 ): A__ = Pipe() A__ = Pipe() process_array_.append( Process( target=__a , args=(i, arr[i], temp_ls, temp_rs, temp_lr, temp_rr, result_pipe[i]) , ) ) A__ = temp_rs A__ = temp_rr process_array_.append( Process( target=__a , args=( len(__a ) - 1, arr[len(__a ) - 1], temp_ls, None, temp_lr, None, result_pipe[len(__a ) - 1], ) , ) ) # start the processes for p in process_array_: p.start() # wait for the processes to end and write their values to the list for p in range(0 , len(__a ) ): A__ = result_pipe[p][0].recv() process_array_[p].join() return arr def __lowerCamelCase ( ) -> str: """simple docstring""" A__ = list(range(1_0 , 0 , -1 ) ) print("""Initial List""" ) print(*__a ) A__ = odd_even_transposition(__a ) print("""Sorted List\n""" ) print(*__a ) if __name__ == "__main__": main()
274
1
import os import time from dataclasses import dataclass, field from enum import Enum from typing import Dict, List, Optional, Union import torch from filelock import FileLock from torch.utils.data import Dataset from ...models.auto.modeling_auto import MODEL_FOR_QUESTION_ANSWERING_MAPPING from ...tokenization_utils import PreTrainedTokenizer from ...utils import logging from ..processors.squad import SquadFeatures, SquadVaProcessor, SquadVaProcessor, squad_convert_examples_to_features A : Optional[Any] = logging.get_logger(__name__) A : str = list(MODEL_FOR_QUESTION_ANSWERING_MAPPING.keys()) A : Tuple = tuple(conf.model_type for conf in MODEL_CONFIG_CLASSES) @dataclass class A : '''simple docstring''' __lowerCamelCase : str = field( default=SCREAMING_SNAKE_CASE , metadata={'''help''': '''Model type selected in the list: ''' + ''', '''.join(SCREAMING_SNAKE_CASE )} ) __lowerCamelCase : str = field( default=SCREAMING_SNAKE_CASE , metadata={'''help''': '''The input data dir. Should contain the .json files for the SQuAD task.'''} ) __lowerCamelCase : int = field( default=128 , metadata={ '''help''': ( '''The maximum total input sequence length after tokenization. Sequences longer ''' '''than this will be truncated, sequences shorter will be padded.''' ) } , ) __lowerCamelCase : int = field( default=128 , metadata={'''help''': '''When splitting up a long document into chunks, how much stride to take between chunks.'''} , ) __lowerCamelCase : int = field( default=64 , metadata={ '''help''': ( '''The maximum number of tokens for the question. Questions longer than this will ''' '''be truncated to this length.''' ) } , ) __lowerCamelCase : int = field( default=30 , metadata={ '''help''': ( '''The maximum length of an answer that can be generated. This is needed because the start ''' '''and end predictions are not conditioned on one another.''' ) } , ) __lowerCamelCase : bool = field( default=SCREAMING_SNAKE_CASE , metadata={'''help''': '''Overwrite the cached training and evaluation sets'''} ) __lowerCamelCase : bool = field( default=SCREAMING_SNAKE_CASE , metadata={'''help''': '''If true, the SQuAD examples contain some that do not have an answer.'''} ) __lowerCamelCase : float = field( default=0.0 , metadata={'''help''': '''If null_score - best_non_null is greater than the threshold predict null.'''} ) __lowerCamelCase : int = field( default=20 , metadata={'''help''': '''If null_score - best_non_null is greater than the threshold predict null.'''} ) __lowerCamelCase : int = field( default=0 , metadata={ '''help''': ( '''language id of input for language-specific xlm models (see''' ''' tokenization_xlm.PRETRAINED_INIT_CONFIGURATION)''' ) } , ) __lowerCamelCase : int = field(default=1 , metadata={'''help''': '''multiple threads for converting example to features'''} ) class A (SCREAMING_SNAKE_CASE ): '''simple docstring''' __lowerCamelCase : List[str] = '''train''' __lowerCamelCase : List[Any] = '''dev''' class A (SCREAMING_SNAKE_CASE ): '''simple docstring''' __lowerCamelCase : SquadDataTrainingArguments __lowerCamelCase : List[SquadFeatures] __lowerCamelCase : Split __lowerCamelCase : bool def __init__( self : Union[str, Any] , __lowerCAmelCase : SquadDataTrainingArguments , __lowerCAmelCase : PreTrainedTokenizer , __lowerCAmelCase : Optional[int] = None , __lowerCAmelCase : Union[str, Split] = Split.train , __lowerCAmelCase : Optional[bool] = False , __lowerCAmelCase : Optional[str] = None , __lowerCAmelCase : Optional[str] = "pt" , ) -> Union[str, Any]: """simple docstring""" A__ = args A__ = is_language_sensitive A__ = SquadVaProcessor() if args.version_2_with_negative else SquadVaProcessor() if isinstance(__lowerCAmelCase , __lowerCAmelCase ): try: A__ = Split[mode] except KeyError: raise KeyError("""mode is not a valid split name""" ) A__ = mode # Load data features from cache or dataset file A__ = """v2""" if args.version_2_with_negative else """v1""" A__ = os.path.join( cache_dir if cache_dir is not None else args.data_dir , f'cached_{mode.value}_{tokenizer.__class__.__name__}_{args.max_seq_length}_{version_tag}' , ) # Make sure only the first process in distributed training processes the dataset, # and the others will use the cache. A__ = cached_features_file + """.lock""" with FileLock(__lowerCAmelCase ): if os.path.exists(__lowerCAmelCase ) and not args.overwrite_cache: A__ = time.time() A__ = torch.load(__lowerCAmelCase ) # Legacy cache files have only features, while new cache files # will have dataset and examples also. A__ = self.old_features["""features"""] A__ = self.old_features.get("""dataset""" , __lowerCAmelCase ) A__ = self.old_features.get("""examples""" , __lowerCAmelCase ) logger.info( f'Loading features from cached file {cached_features_file} [took %.3f s]' , time.time() - start ) if self.dataset is None or self.examples is None: logger.warning( f'Deleting cached file {cached_features_file} will allow dataset and examples to be cached in' """ future run""" ) else: if mode == Split.dev: A__ = self.processor.get_dev_examples(args.data_dir ) else: A__ = self.processor.get_train_examples(args.data_dir ) A__ , A__ = squad_convert_examples_to_features( examples=self.examples , tokenizer=__lowerCAmelCase , max_seq_length=args.max_seq_length , doc_stride=args.doc_stride , max_query_length=args.max_query_length , is_training=mode == Split.train , threads=args.threads , return_dataset=__lowerCAmelCase , ) A__ = time.time() torch.save( {"""features""": self.features, """dataset""": self.dataset, """examples""": self.examples} , __lowerCAmelCase , ) # ^ This seems to take a lot of time so I want to investigate why and how we can improve. logger.info( f'Saving features into cached file {cached_features_file} [took {time.time() - start:.3f} s]' ) def __len__( self : Any ) -> Any: """simple docstring""" return len(self.features ) def __getitem__( self : List[str] , __lowerCAmelCase : List[Any] ) -> Dict[str, torch.Tensor]: """simple docstring""" A__ = self.features[i] A__ = torch.tensor(feature.input_ids , dtype=torch.long ) A__ = torch.tensor(feature.attention_mask , dtype=torch.long ) A__ = torch.tensor(feature.token_type_ids , dtype=torch.long ) A__ = torch.tensor(feature.cls_index , dtype=torch.long ) A__ = torch.tensor(feature.p_mask , dtype=torch.float ) A__ = torch.tensor(feature.is_impossible , dtype=torch.float ) A__ = { """input_ids""": input_ids, """attention_mask""": attention_mask, """token_type_ids""": token_type_ids, } if self.args.model_type in ["xlm", "roberta", "distilbert", "camembert"]: del inputs["token_type_ids"] if self.args.model_type in ["xlnet", "xlm"]: inputs.update({"""cls_index""": cls_index, """p_mask""": p_mask} ) if self.args.version_2_with_negative: inputs.update({"""is_impossible""": is_impossible} ) if self.is_language_sensitive: inputs.update({"""langs""": (torch.ones(input_ids.shape , dtype=torch.intaa ) * self.args.lang_id)} ) if self.mode == Split.train: A__ = torch.tensor(feature.start_position , dtype=torch.long ) A__ = torch.tensor(feature.end_position , dtype=torch.long ) inputs.update({"""start_positions""": start_positions, """end_positions""": end_positions} ) return inputs
274
import argparse from argparse import Namespace import torch from torch import nn from transformers import XGLMConfig, XGLMForCausalLM def __lowerCamelCase ( __a :Dict ) -> Any: """simple docstring""" A__ = [ """decoder.version""", """decoder.output_projection.weight""", """_float_tensor""", """decoder.embed_positions._float_tensor""", ] for k in ignore_keys: state_dict.pop(__a , __a ) def __lowerCamelCase ( __a :str ) -> Union[str, Any]: """simple docstring""" A__ , A__ = emb.weight.shape A__ = nn.Linear(__a , __a , bias=__a ) A__ = emb.weight.data return lin_layer def __lowerCamelCase ( __a :str ) -> List[str]: """simple docstring""" A__ = torch.load(__a , map_location="""cpu""" ) A__ = Namespace(**checkpoint["""cfg"""]["""model"""] ) A__ = checkpoint["""model"""] remove_ignore_keys_(__a ) A__ = state_dict["""decoder.embed_tokens.weight"""].shape[0] A__ = {key.replace("""decoder""" , """model""" ): val for key, val in state_dict.items()} A__ = 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 , ) A__ = XGLMForCausalLM(__a ) A__ = model.load_state_dict(__a , strict=__a ) print(__a ) A__ = make_linear_from_emb(model.model.embed_tokens ) return model if __name__ == "__main__": A : int = 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.''') A : str = parser.parse_args() A : str = convert_fairseq_xglm_checkpoint_from_disk(args.fairseq_path) model.save_pretrained(args.pytorch_dump_folder_path)
274
1
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tokenizers_available, is_torch_available A : Dict = { '''configuration_graphormer''': ['''GRAPHORMER_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''GraphormerConfig'''], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: A : Tuple = [ '''GRAPHORMER_PRETRAINED_MODEL_ARCHIVE_LIST''', '''GraphormerForGraphClassification''', '''GraphormerModel''', '''GraphormerPreTrainedModel''', ] if TYPE_CHECKING: from .configuration_graphormer import GRAPHORMER_PRETRAINED_CONFIG_ARCHIVE_MAP, GraphormerConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_graphormer import ( GRAPHORMER_PRETRAINED_MODEL_ARCHIVE_LIST, GraphormerForGraphClassification, GraphormerModel, GraphormerPreTrainedModel, ) else: import sys A : str = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
274
import unittest import numpy as np from transformers import AlbertConfig, is_flax_available from transformers.testing_utils import require_flax, slow from ...test_modeling_flax_common import FlaxModelTesterMixin, ids_tensor, random_attention_mask if is_flax_available(): import jax.numpy as jnp from transformers.models.albert.modeling_flax_albert import ( FlaxAlbertForMaskedLM, FlaxAlbertForMultipleChoice, FlaxAlbertForPreTraining, FlaxAlbertForQuestionAnswering, FlaxAlbertForSequenceClassification, FlaxAlbertForTokenClassification, FlaxAlbertModel, ) class A (unittest.TestCase ): '''simple docstring''' def __init__( self : List[str] , __lowerCAmelCase : int , __lowerCAmelCase : List[str]=13 , __lowerCAmelCase : int=7 , __lowerCAmelCase : Tuple=True , __lowerCAmelCase : int=True , __lowerCAmelCase : Dict=True , __lowerCAmelCase : Union[str, Any]=True , __lowerCAmelCase : List[Any]=99 , __lowerCAmelCase : Optional[Any]=32 , __lowerCAmelCase : Optional[Any]=5 , __lowerCAmelCase : Tuple=4 , __lowerCAmelCase : Any=37 , __lowerCAmelCase : str="gelu" , __lowerCAmelCase : Optional[Any]=0.1 , __lowerCAmelCase : Union[str, Any]=0.1 , __lowerCAmelCase : List[str]=5_12 , __lowerCAmelCase : int=16 , __lowerCAmelCase : Optional[int]=2 , __lowerCAmelCase : List[Any]=0.0_2 , __lowerCAmelCase : Tuple=4 , ) -> Dict: """simple docstring""" A__ = parent A__ = batch_size A__ = seq_length A__ = is_training A__ = use_attention_mask A__ = use_token_type_ids A__ = use_labels A__ = vocab_size A__ = hidden_size A__ = num_hidden_layers A__ = num_attention_heads A__ = intermediate_size A__ = hidden_act A__ = hidden_dropout_prob A__ = attention_probs_dropout_prob A__ = max_position_embeddings A__ = type_vocab_size A__ = type_sequence_label_size A__ = initializer_range A__ = num_choices def a_ ( self : Any ) -> str: """simple docstring""" A__ = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) A__ = None if self.use_attention_mask: A__ = random_attention_mask([self.batch_size, self.seq_length] ) A__ = None if self.use_token_type_ids: A__ = ids_tensor([self.batch_size, self.seq_length] , self.type_vocab_size ) A__ = AlbertConfig( vocab_size=self.vocab_size , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , max_position_embeddings=self.max_position_embeddings , type_vocab_size=self.type_vocab_size , is_decoder=__lowerCAmelCase , initializer_range=self.initializer_range , ) return config, input_ids, token_type_ids, attention_mask def a_ ( self : Optional[int] ) -> Optional[int]: """simple docstring""" A__ = self.prepare_config_and_inputs() A__ , A__ , A__ , A__ = config_and_inputs A__ = {"""input_ids""": input_ids, """token_type_ids""": token_type_ids, """attention_mask""": attention_mask} return config, inputs_dict @require_flax class A (SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' __lowerCamelCase : str = ( ( FlaxAlbertModel, FlaxAlbertForPreTraining, FlaxAlbertForMaskedLM, FlaxAlbertForMultipleChoice, FlaxAlbertForQuestionAnswering, FlaxAlbertForSequenceClassification, FlaxAlbertForTokenClassification, FlaxAlbertForQuestionAnswering, ) if is_flax_available() else () ) def a_ ( self : str ) -> Optional[int]: """simple docstring""" A__ = FlaxAlbertModelTester(self ) @slow def a_ ( self : int ) -> Tuple: """simple docstring""" for model_class_name in self.all_model_classes: A__ = model_class_name.from_pretrained("""albert-base-v2""" ) A__ = model(np.ones((1, 1) ) ) self.assertIsNotNone(__lowerCAmelCase ) @require_flax class A (unittest.TestCase ): '''simple docstring''' @slow def a_ ( self : Dict ) -> List[Any]: """simple docstring""" A__ = FlaxAlbertModel.from_pretrained("""albert-base-v2""" ) A__ = np.array([[0, 3_45, 2_32, 3_28, 7_40, 1_40, 16_95, 69, 60_78, 15_88, 2]] ) A__ = np.array([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]] ) A__ = model(__lowerCAmelCase , attention_mask=__lowerCAmelCase )[0] A__ = (1, 11, 7_68) self.assertEqual(output.shape , __lowerCAmelCase ) A__ = np.array( [[[-0.6_5_1_3, 1.5_0_3_5, -0.2_7_6_6], [-0.6_5_1_5, 1.5_0_4_6, -0.2_7_8_0], [-0.6_5_1_2, 1.5_0_4_9, -0.2_7_8_4]]] ) self.assertTrue(jnp.allclose(output[:, 1:4, 1:4] , __lowerCAmelCase , atol=1e-4 ) )
274
1
def __lowerCamelCase ( __a :int , __a :int ) -> int: """simple docstring""" return x if y == 0 else greatest_common_divisor(__a , x % y ) def __lowerCamelCase ( __a :int , __a :int ) -> int: """simple docstring""" return (x * y) // greatest_common_divisor(__a , __a ) def __lowerCamelCase ( __a :int = 2_0 ) -> int: """simple docstring""" A__ = 1 for i in range(1 , n + 1 ): A__ = lcm(__a , __a ) return g if __name__ == "__main__": print(F'''{solution() = }''')
274
from sklearn.metrics import fa_score import datasets A : Any = ''' The F1 score is the harmonic mean of the precision and recall. It can be computed with the equation: F1 = 2 * (precision * recall) / (precision + recall) ''' A : List[Any] = ''' Args: predictions (`list` of `int`): Predicted labels. references (`list` of `int`): Ground truth labels. 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. pos_label (`int`): The class to be considered the positive class, in the case where `average` is set to `binary`. Defaults to 1. 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\'`. - \'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. - \'micro\': Calculate metrics globally by counting the total true positives, false negatives and false positives. - \'macro\': Calculate metrics for each label, and find their unweighted mean. This does not take label imbalance into account. - \'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. - \'samples\': Calculate metrics for each instance, and find their average (only meaningful for multilabel classification). sample_weight (`list` of `float`): Sample weights Defaults to None. Returns: 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. Examples: Example 1-A simple binary example >>> f1_metric = datasets.load_metric("f1") >>> results = f1_metric.compute(references=[0, 1, 0, 1, 0], predictions=[0, 0, 1, 1, 0]) >>> print(results) {\'f1\': 0.5} Example 2-The same simple binary example as in Example 1, but with `pos_label` set to `0`. >>> f1_metric = datasets.load_metric("f1") >>> results = f1_metric.compute(references=[0, 1, 0, 1, 0], predictions=[0, 0, 1, 1, 0], pos_label=0) >>> print(round(results[\'f1\'], 2)) 0.67 Example 3-The same simple binary example as in Example 1, but with `sample_weight` included. >>> f1_metric = datasets.load_metric("f1") >>> 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]) >>> print(round(results[\'f1\'], 2)) 0.35 Example 4-A multiclass example, with different values for the `average` input. >>> predictions = [0, 2, 1, 0, 0, 1] >>> references = [0, 1, 2, 0, 1, 2] >>> results = f1_metric.compute(predictions=predictions, references=references, average="macro") >>> print(round(results[\'f1\'], 2)) 0.27 >>> results = f1_metric.compute(predictions=predictions, references=references, average="micro") >>> print(round(results[\'f1\'], 2)) 0.33 >>> results = f1_metric.compute(predictions=predictions, references=references, average="weighted") >>> print(round(results[\'f1\'], 2)) 0.27 >>> results = f1_metric.compute(predictions=predictions, references=references, average=None) >>> print(results) {\'f1\': array([0.8, 0. , 0. ])} ''' A : List[Any] = ''' @article{scikit-learn, title={Scikit-learn: Machine Learning in {P}ython}, author={Pedregosa, F. and Varoquaux, G. and Gramfort, A. and Michel, V. and Thirion, B. and Grisel, O. and Blondel, M. and Prettenhofer, P. and Weiss, R. and Dubourg, V. and Vanderplas, J. and Passos, A. and Cournapeau, D. and Brucher, M. and Perrot, M. and Duchesnay, E.}, journal={Journal of Machine Learning Research}, volume={12}, pages={2825--2830}, year={2011} } ''' @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class A (datasets.Metric ): '''simple docstring''' def a_ ( self : Optional[int] ) -> List[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 a_ ( self : Any , __lowerCAmelCase : Tuple , __lowerCAmelCase : Union[str, Any] , __lowerCAmelCase : Dict=None , __lowerCAmelCase : List[str]=1 , __lowerCAmelCase : Any="binary" , __lowerCAmelCase : Optional[int]=None ) -> List[Any]: """simple docstring""" A__ = fa_score( __lowerCAmelCase , __lowerCAmelCase , labels=__lowerCAmelCase , pos_label=__lowerCAmelCase , average=__lowerCAmelCase , sample_weight=__lowerCAmelCase ) return {"f1": float(__lowerCAmelCase ) if score.size == 1 else score}
274
1
from typing import Optional, Union import torch from torch import nn from ...configuration_utils import ConfigMixin, register_to_config from ...models.modeling_utils import ModelMixin class A (SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE ): '''simple docstring''' @register_to_config def __init__( self : List[Any] , __lowerCAmelCase : int = 7_68 , ) -> Tuple: """simple docstring""" super().__init__() A__ = nn.Parameter(torch.zeros(1 , __lowerCAmelCase ) ) A__ = nn.Parameter(torch.ones(1 , __lowerCAmelCase ) ) def a_ ( self : str , __lowerCAmelCase : Optional[Union[str, torch.device]] = None , __lowerCAmelCase : Optional[torch.dtype] = None , ) -> str: """simple docstring""" A__ = nn.Parameter(self.mean.to(__lowerCAmelCase ).to(__lowerCAmelCase ) ) A__ = nn.Parameter(self.std.to(__lowerCAmelCase ).to(__lowerCAmelCase ) ) return self def a_ ( self : str , __lowerCAmelCase : Tuple ) -> Union[str, Any]: """simple docstring""" A__ = (embeds - self.mean) * 1.0 / self.std return embeds def a_ ( self : Tuple , __lowerCAmelCase : Union[str, Any] ) -> str: """simple docstring""" A__ = (embeds * self.std) + self.mean return embeds
274
from collections import OrderedDict from typing import Mapping from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging A : Union[str, Any] = logging.get_logger(__name__) A : int = { '''xlm-roberta-base''': '''https://huggingface.co/xlm-roberta-base/resolve/main/config.json''', '''xlm-roberta-large''': '''https://huggingface.co/xlm-roberta-large/resolve/main/config.json''', '''xlm-roberta-large-finetuned-conll02-dutch''': ( '''https://huggingface.co/xlm-roberta-large-finetuned-conll02-dutch/resolve/main/config.json''' ), '''xlm-roberta-large-finetuned-conll02-spanish''': ( '''https://huggingface.co/xlm-roberta-large-finetuned-conll02-spanish/resolve/main/config.json''' ), '''xlm-roberta-large-finetuned-conll03-english''': ( '''https://huggingface.co/xlm-roberta-large-finetuned-conll03-english/resolve/main/config.json''' ), '''xlm-roberta-large-finetuned-conll03-german''': ( '''https://huggingface.co/xlm-roberta-large-finetuned-conll03-german/resolve/main/config.json''' ), } class A (SCREAMING_SNAKE_CASE ): '''simple docstring''' __lowerCamelCase : Any = '''xlm-roberta''' def __init__( self : Optional[Any] , __lowerCAmelCase : List[Any]=3_05_22 , __lowerCAmelCase : int=7_68 , __lowerCAmelCase : Tuple=12 , __lowerCAmelCase : str=12 , __lowerCAmelCase : Union[str, Any]=30_72 , __lowerCAmelCase : Union[str, Any]="gelu" , __lowerCAmelCase : Optional[int]=0.1 , __lowerCAmelCase : Dict=0.1 , __lowerCAmelCase : List[str]=5_12 , __lowerCAmelCase : Tuple=2 , __lowerCAmelCase : Dict=0.0_2 , __lowerCAmelCase : List[str]=1e-12 , __lowerCAmelCase : Union[str, Any]=1 , __lowerCAmelCase : str=0 , __lowerCAmelCase : Optional[int]=2 , __lowerCAmelCase : Tuple="absolute" , __lowerCAmelCase : Any=True , __lowerCAmelCase : Any=None , **__lowerCAmelCase : str , ) -> Optional[Any]: """simple docstring""" super().__init__(pad_token_id=__lowerCAmelCase , bos_token_id=__lowerCAmelCase , eos_token_id=__lowerCAmelCase , **__lowerCAmelCase ) A__ = vocab_size A__ = hidden_size A__ = num_hidden_layers A__ = num_attention_heads A__ = hidden_act A__ = intermediate_size A__ = hidden_dropout_prob A__ = attention_probs_dropout_prob A__ = max_position_embeddings A__ = type_vocab_size A__ = initializer_range A__ = layer_norm_eps A__ = position_embedding_type A__ = use_cache A__ = classifier_dropout class A (SCREAMING_SNAKE_CASE ): '''simple docstring''' @property def a_ ( self : int ) -> Mapping[str, Mapping[int, str]]: """simple docstring""" if self.task == "multiple-choice": A__ = {0: """batch""", 1: """choice""", 2: """sequence"""} else: A__ = {0: """batch""", 1: """sequence"""} return OrderedDict( [ ("""input_ids""", dynamic_axis), ("""attention_mask""", dynamic_axis), ] )
274
1
import warnings from ...utils import logging from .image_processing_flava import FlavaImageProcessor A : List[str] = logging.get_logger(__name__) class A (SCREAMING_SNAKE_CASE ): '''simple docstring''' def __init__( self : Optional[Any] , *__lowerCAmelCase : Optional[int] , **__lowerCAmelCase : List[str] ) -> None: """simple docstring""" warnings.warn( """The class FlavaFeatureExtractor is deprecated and will be removed in version 5 of Transformers. Please""" """ use FlavaImageProcessor instead.""" , __lowerCAmelCase , ) super().__init__(*__lowerCAmelCase , **__lowerCAmelCase )
274
from __future__ import annotations import time from math import sqrt # 1 for manhattan, 0 for euclidean A : str = 0 A : Any = [ [0, 0, 0, 0, 0, 0, 0], [0, 1, 0, 0, 0, 0, 0], # 0 are free path whereas 1's are obstacles [0, 0, 0, 0, 0, 0, 0], [0, 0, 1, 0, 0, 0, 0], [1, 0, 1, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 1, 0, 0], ] A : Dict = [[-1, 0], [0, -1], [1, 0], [0, 1]] # up, left, down, right A : Union[str, Any] = tuple[int, int] class A : '''simple docstring''' def __init__( self : int , __lowerCAmelCase : int , __lowerCAmelCase : int , __lowerCAmelCase : int , __lowerCAmelCase : int , __lowerCAmelCase : int , __lowerCAmelCase : Node | None , ) -> None: """simple docstring""" A__ = pos_x A__ = pos_y A__ = (pos_y, pos_x) A__ = goal_x A__ = goal_y A__ = g_cost A__ = parent A__ = self.calculate_heuristic() A__ = self.g_cost + self.h_cost def a_ ( self : Dict ) -> float: """simple docstring""" A__ = self.pos_x - self.goal_x A__ = self.pos_y - self.goal_y if HEURISTIC == 1: return abs(__lowerCAmelCase ) + abs(__lowerCAmelCase ) else: return sqrt(dy**2 + dx**2 ) def __lt__( self : int , __lowerCAmelCase : Node ) -> bool: """simple docstring""" return self.f_cost < other.f_cost class A : '''simple docstring''' def __init__( self : Union[str, Any] , __lowerCAmelCase : TPosition , __lowerCAmelCase : TPosition ) -> Tuple: """simple docstring""" A__ = Node(start[1] , start[0] , goal[1] , goal[0] , 0 , __lowerCAmelCase ) A__ = Node(goal[1] , goal[0] , goal[1] , goal[0] , 9_99_99 , __lowerCAmelCase ) A__ = [self.start] A__ = [] A__ = False def a_ ( self : List[str] ) -> list[TPosition]: """simple docstring""" while self.open_nodes: # Open Nodes are sorted using __lt__ self.open_nodes.sort() A__ = self.open_nodes.pop(0 ) if current_node.pos == self.target.pos: return self.retrace_path(__lowerCAmelCase ) self.closed_nodes.append(__lowerCAmelCase ) A__ = self.get_successors(__lowerCAmelCase ) for child_node in successors: if child_node in self.closed_nodes: continue if child_node not in self.open_nodes: self.open_nodes.append(__lowerCAmelCase ) else: # retrieve the best current path A__ = self.open_nodes.pop(self.open_nodes.index(__lowerCAmelCase ) ) if child_node.g_cost < better_node.g_cost: self.open_nodes.append(__lowerCAmelCase ) else: self.open_nodes.append(__lowerCAmelCase ) return [self.start.pos] def a_ ( self : Optional[Any] , __lowerCAmelCase : Node ) -> list[Node]: """simple docstring""" A__ = [] for action in delta: A__ = parent.pos_x + action[1] A__ = parent.pos_y + action[0] if not (0 <= pos_x <= len(grid[0] ) - 1 and 0 <= pos_y <= len(__lowerCAmelCase ) - 1): continue if grid[pos_y][pos_x] != 0: continue successors.append( Node( __lowerCAmelCase , __lowerCAmelCase , self.target.pos_y , self.target.pos_x , parent.g_cost + 1 , __lowerCAmelCase , ) ) return successors def a_ ( self : List[Any] , __lowerCAmelCase : Node | None ) -> list[TPosition]: """simple docstring""" A__ = node A__ = [] while current_node is not None: path.append((current_node.pos_y, current_node.pos_x) ) A__ = current_node.parent path.reverse() return path class A : '''simple docstring''' def __init__( self : Optional[Any] , __lowerCAmelCase : TPosition , __lowerCAmelCase : TPosition ) -> None: """simple docstring""" A__ = AStar(__lowerCAmelCase , __lowerCAmelCase ) A__ = AStar(__lowerCAmelCase , __lowerCAmelCase ) A__ = False def a_ ( self : int ) -> list[TPosition]: """simple docstring""" while self.fwd_astar.open_nodes or self.bwd_astar.open_nodes: self.fwd_astar.open_nodes.sort() self.bwd_astar.open_nodes.sort() A__ = self.fwd_astar.open_nodes.pop(0 ) A__ = self.bwd_astar.open_nodes.pop(0 ) if current_bwd_node.pos == current_fwd_node.pos: return self.retrace_bidirectional_path( __lowerCAmelCase , __lowerCAmelCase ) self.fwd_astar.closed_nodes.append(__lowerCAmelCase ) self.bwd_astar.closed_nodes.append(__lowerCAmelCase ) A__ = current_bwd_node A__ = current_fwd_node A__ = { self.fwd_astar: self.fwd_astar.get_successors(__lowerCAmelCase ), self.bwd_astar: self.bwd_astar.get_successors(__lowerCAmelCase ), } for astar in [self.fwd_astar, self.bwd_astar]: for child_node in successors[astar]: if child_node in astar.closed_nodes: continue if child_node not in astar.open_nodes: astar.open_nodes.append(__lowerCAmelCase ) else: # retrieve the best current path A__ = astar.open_nodes.pop( astar.open_nodes.index(__lowerCAmelCase ) ) if child_node.g_cost < better_node.g_cost: astar.open_nodes.append(__lowerCAmelCase ) else: astar.open_nodes.append(__lowerCAmelCase ) return [self.fwd_astar.start.pos] def a_ ( self : List[str] , __lowerCAmelCase : Node , __lowerCAmelCase : Node ) -> list[TPosition]: """simple docstring""" A__ = self.fwd_astar.retrace_path(__lowerCAmelCase ) A__ = self.bwd_astar.retrace_path(__lowerCAmelCase ) bwd_path.pop() bwd_path.reverse() A__ = fwd_path + bwd_path return path if __name__ == "__main__": # all coordinates are given in format [y,x] A : Optional[int] = (0, 0) A : int = (len(grid) - 1, len(grid[0]) - 1) for elem in grid: print(elem) A : Dict = time.time() A : Optional[Any] = AStar(init, goal) A : Optional[int] = a_star.search() A : Optional[int] = time.time() - start_time print(F'''AStar execution time = {end_time:f} seconds''') A : Dict = time.time() A : Tuple = BidirectionalAStar(init, goal) A : List[Any] = time.time() - bd_start_time print(F'''BidirectionalAStar execution time = {bd_end_time:f} seconds''')
274
1
from __future__ import annotations def __lowerCamelCase ( __a :list , __a :int | None = None , __a :int | None = None ) -> None: """simple docstring""" if start is None: A__ = 0 if end is None: A__ = len(__a ) - 1 if start >= end: return A__ = (start + end) // 2 slowsort(__a , __a , __a ) slowsort(__a , mid + 1 , __a ) if sequence[end] < sequence[mid]: A__ , A__ = sequence[mid], sequence[end] slowsort(__a , __a , end - 1 ) if __name__ == "__main__": from doctest import testmod testmod()
274
import unittest from transformers.utils.backbone_utils import ( BackboneMixin, get_aligned_output_features_output_indices, verify_out_features_out_indices, ) class A (unittest.TestCase ): '''simple docstring''' def a_ ( self : Any ) -> Union[str, Any]: """simple docstring""" A__ = ["""a""", """b""", """c"""] # Defaults to last layer if both are None A__ , A__ = get_aligned_output_features_output_indices(__lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase ) self.assertEqual(__lowerCAmelCase , ["""c"""] ) self.assertEqual(__lowerCAmelCase , [2] ) # Out indices set to match out features A__ , A__ = get_aligned_output_features_output_indices(["""a""", """c"""] , __lowerCAmelCase , __lowerCAmelCase ) self.assertEqual(__lowerCAmelCase , ["""a""", """c"""] ) self.assertEqual(__lowerCAmelCase , [0, 2] ) # Out features set to match out indices A__ , A__ = get_aligned_output_features_output_indices(__lowerCAmelCase , [0, 2] , __lowerCAmelCase ) self.assertEqual(__lowerCAmelCase , ["""a""", """c"""] ) self.assertEqual(__lowerCAmelCase , [0, 2] ) # Out features selected from negative indices A__ , A__ = get_aligned_output_features_output_indices(__lowerCAmelCase , [-3, -1] , __lowerCAmelCase ) self.assertEqual(__lowerCAmelCase , ["""a""", """c"""] ) self.assertEqual(__lowerCAmelCase , [-3, -1] ) def a_ ( self : Optional[Any] ) -> Optional[Any]: """simple docstring""" with self.assertRaises(__lowerCAmelCase ): verify_out_features_out_indices(["""a""", """b"""] , (0, 1) , __lowerCAmelCase ) # Out features must be a list with self.assertRaises(__lowerCAmelCase ): verify_out_features_out_indices(("""a""", """b""") , (0, 1) , ["""a""", """b"""] ) # Out features must be a subset of stage names with self.assertRaises(__lowerCAmelCase ): verify_out_features_out_indices(["""a""", """b"""] , (0, 1) , ["""a"""] ) # Out indices must be a list or tuple with self.assertRaises(__lowerCAmelCase ): verify_out_features_out_indices(__lowerCAmelCase , 0 , ["""a""", """b"""] ) # Out indices must be a subset of stage names with self.assertRaises(__lowerCAmelCase ): verify_out_features_out_indices(__lowerCAmelCase , (0, 1) , ["""a"""] ) # Out features and out indices must be the same length with self.assertRaises(__lowerCAmelCase ): verify_out_features_out_indices(["""a""", """b"""] , (0,) , ["""a""", """b""", """c"""] ) # Out features should match out indices with self.assertRaises(__lowerCAmelCase ): verify_out_features_out_indices(["""a""", """b"""] , (0, 2) , ["""a""", """b""", """c"""] ) # Out features and out indices should be in order with self.assertRaises(__lowerCAmelCase ): verify_out_features_out_indices(["""b""", """a"""] , (0, 1) , ["""a""", """b"""] ) # Check passes with valid inputs verify_out_features_out_indices(["""a""", """b""", """d"""] , (0, 1, -1) , ["""a""", """b""", """c""", """d"""] ) def a_ ( self : Union[str, Any] ) -> List[str]: """simple docstring""" A__ = BackboneMixin() A__ = ["""a""", """b""", """c"""] A__ = ["""a""", """c"""] A__ = [0, 2] # Check that the output features and indices are set correctly self.assertEqual(backbone.out_features , ["""a""", """c"""] ) self.assertEqual(backbone.out_indices , [0, 2] ) # Check out features and indices are updated correctly A__ = ["""a""", """b"""] self.assertEqual(backbone.out_features , ["""a""", """b"""] ) self.assertEqual(backbone.out_indices , [0, 1] ) A__ = [-3, -1] self.assertEqual(backbone.out_features , ["""a""", """c"""] ) self.assertEqual(backbone.out_indices , [-3, -1] )
274
1
from datetime import datetime import requests from bsa import BeautifulSoup if __name__ == "__main__": A : List[str] = input('''Enter image url: ''').strip() print(F'''Downloading image from {url} ...''') A : Any = BeautifulSoup(requests.get(url).content, '''html.parser''') # The image URL is in the content field of the first meta tag with property og:image A : List[Any] = soup.find('''meta''', {'''property''': '''og:image'''})['''content'''] A : Dict = requests.get(image_url).content A : Tuple = F'''{datetime.now():%Y-%m-%d_%H:%M:%S}.jpg''' with open(file_name, '''wb''') as fp: fp.write(image_data) print(F'''Done. Image saved to disk as {file_name}.''')
274
from collections import deque class A : '''simple docstring''' def __init__( self : List[str] , __lowerCAmelCase : str , __lowerCAmelCase : int , __lowerCAmelCase : int ) -> None: """simple docstring""" A__ = process_name # process name A__ = arrival_time # arrival time of the process # completion time of finished process or last interrupted time A__ = arrival_time A__ = burst_time # remaining burst time A__ = 0 # total time of the process wait in ready queue A__ = 0 # time from arrival time to completion time class A : '''simple docstring''' def __init__( self : Tuple , __lowerCAmelCase : int , __lowerCAmelCase : list[int] , __lowerCAmelCase : deque[Process] , __lowerCAmelCase : int , ) -> None: """simple docstring""" A__ = number_of_queues # time slice of queues that round robin algorithm applied A__ = time_slices # unfinished process is in this ready_queue A__ = queue # current time A__ = current_time # finished process is in this sequence queue A__ = deque() def a_ ( self : Dict ) -> list[str]: """simple docstring""" A__ = [] for i in range(len(self.finish_queue ) ): sequence.append(self.finish_queue[i].process_name ) return sequence def a_ ( self : Tuple , __lowerCAmelCase : list[Process] ) -> list[int]: """simple docstring""" A__ = [] for i in range(len(__lowerCAmelCase ) ): waiting_times.append(queue[i].waiting_time ) return waiting_times def a_ ( self : Optional[Any] , __lowerCAmelCase : list[Process] ) -> list[int]: """simple docstring""" A__ = [] for i in range(len(__lowerCAmelCase ) ): turnaround_times.append(queue[i].turnaround_time ) return turnaround_times def a_ ( self : Dict , __lowerCAmelCase : list[Process] ) -> list[int]: """simple docstring""" A__ = [] for i in range(len(__lowerCAmelCase ) ): completion_times.append(queue[i].stop_time ) return completion_times def a_ ( self : int , __lowerCAmelCase : deque[Process] ) -> list[int]: """simple docstring""" return [q.burst_time for q in queue] def a_ ( self : Any , __lowerCAmelCase : Process ) -> int: """simple docstring""" process.waiting_time += self.current_time - process.stop_time return process.waiting_time def a_ ( self : Union[str, Any] , __lowerCAmelCase : deque[Process] ) -> deque[Process]: """simple docstring""" A__ = deque() # sequence deque of finished process while len(__lowerCAmelCase ) != 0: A__ = ready_queue.popleft() # current process # if process's arrival time is later than current time, update current time if self.current_time < cp.arrival_time: self.current_time += cp.arrival_time # update waiting time of current process self.update_waiting_time(__lowerCAmelCase ) # update current time self.current_time += cp.burst_time # finish the process and set the process's burst-time 0 A__ = 0 # set the process's turnaround time because it is finished A__ = self.current_time - cp.arrival_time # set the completion time A__ = self.current_time # add the process to queue that has finished queue finished.append(__lowerCAmelCase ) self.finish_queue.extend(__lowerCAmelCase ) # add finished process to finish queue # FCFS will finish all remaining processes return finished def a_ ( self : Optional[Any] , __lowerCAmelCase : deque[Process] , __lowerCAmelCase : int ) -> tuple[deque[Process], deque[Process]]: """simple docstring""" A__ = deque() # sequence deque of terminated process # just for 1 cycle and unfinished processes will go back to queue for _ in range(len(__lowerCAmelCase ) ): A__ = ready_queue.popleft() # current process # if process's arrival time is later than current time, update current time if self.current_time < cp.arrival_time: self.current_time += cp.arrival_time # update waiting time of unfinished processes self.update_waiting_time(__lowerCAmelCase ) # if the burst time of process is bigger than time-slice if cp.burst_time > time_slice: # use CPU for only time-slice self.current_time += time_slice # update remaining burst time cp.burst_time -= time_slice # update end point time A__ = self.current_time # locate the process behind the queue because it is not finished ready_queue.append(__lowerCAmelCase ) else: # use CPU for remaining burst time self.current_time += cp.burst_time # set burst time 0 because the process is finished A__ = 0 # set the finish time A__ = self.current_time # update the process' turnaround time because it is finished A__ = self.current_time - cp.arrival_time # add the process to queue that has finished queue finished.append(__lowerCAmelCase ) self.finish_queue.extend(__lowerCAmelCase ) # add finished process to finish queue # return finished processes queue and remaining processes queue return finished, ready_queue def a_ ( self : List[Any] ) -> deque[Process]: """simple docstring""" for i in range(self.number_of_queues - 1 ): A__ , A__ = self.round_robin( self.ready_queue , self.time_slices[i] ) # the last queue has first_come_first_served algorithm self.first_come_first_served(self.ready_queue ) return self.finish_queue if __name__ == "__main__": import doctest A : Union[str, Any] = Process('''P1''', 0, 5_3) A : Optional[Any] = Process('''P2''', 0, 1_7) A : Optional[int] = Process('''P3''', 0, 6_8) A : int = Process('''P4''', 0, 2_4) A : Any = 3 A : List[Any] = [1_7, 2_5] A : Optional[Any] = deque([Pa, Pa, Pa, Pa]) if len(time_slices) != number_of_queues - 1: raise SystemExit(0) doctest.testmod(extraglobs={'''queue''': deque([Pa, Pa, Pa, Pa])}) A : Optional[Any] = Process('''P1''', 0, 5_3) A : int = Process('''P2''', 0, 1_7) A : Optional[int] = Process('''P3''', 0, 6_8) A : Tuple = Process('''P4''', 0, 2_4) A : Union[str, Any] = 3 A : Optional[Any] = [1_7, 2_5] A : Tuple = deque([Pa, Pa, Pa, Pa]) A : Optional[int] = MLFQ(number_of_queues, time_slices, queue, 0) A : Dict = mlfq.multi_level_feedback_queue() # print total waiting times of processes(P1, P2, P3, P4) print( F'''waiting time:\ \t\t\t{MLFQ.calculate_waiting_time(mlfq, [Pa, Pa, Pa, Pa])}''' ) # print completion times of processes(P1, P2, P3, P4) print( F'''completion time:\ \t\t{MLFQ.calculate_completion_time(mlfq, [Pa, Pa, Pa, Pa])}''' ) # print total turnaround times of processes(P1, P2, P3, P4) print( F'''turnaround time:\ \t\t{MLFQ.calculate_turnaround_time(mlfq, [Pa, Pa, Pa, Pa])}''' ) # print sequence of finished processes print( F'''sequence of finished processes:\ {mlfq.calculate_sequence_of_finish_queue()}''' )
274
1
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available A : Optional[int] = { '''configuration_table_transformer''': [ '''TABLE_TRANSFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''TableTransformerConfig''', '''TableTransformerOnnxConfig''', ] } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: A : Union[str, Any] = [ '''TABLE_TRANSFORMER_PRETRAINED_MODEL_ARCHIVE_LIST''', '''TableTransformerForObjectDetection''', '''TableTransformerModel''', '''TableTransformerPreTrainedModel''', ] if TYPE_CHECKING: from .configuration_table_transformer import ( TABLE_TRANSFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP, TableTransformerConfig, TableTransformerOnnxConfig, ) try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_table_transformer import ( TABLE_TRANSFORMER_PRETRAINED_MODEL_ARCHIVE_LIST, TableTransformerForObjectDetection, TableTransformerModel, TableTransformerPreTrainedModel, ) else: import sys A : Any = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
274
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 A : str = logging.get_logger(__name__) A : Union[str, Any] = { '''microsoft/layoutlmv3-base''': '''https://huggingface.co/microsoft/layoutlmv3-base/resolve/main/config.json''', } class A (SCREAMING_SNAKE_CASE ): '''simple docstring''' __lowerCamelCase : Optional[Any] = '''layoutlmv3''' def __init__( self : Tuple , __lowerCAmelCase : Optional[int]=5_02_65 , __lowerCAmelCase : Tuple=7_68 , __lowerCAmelCase : Union[str, Any]=12 , __lowerCAmelCase : Any=12 , __lowerCAmelCase : List[Any]=30_72 , __lowerCAmelCase : Optional[int]="gelu" , __lowerCAmelCase : Dict=0.1 , __lowerCAmelCase : List[Any]=0.1 , __lowerCAmelCase : Dict=5_12 , __lowerCAmelCase : Any=2 , __lowerCAmelCase : Dict=0.0_2 , __lowerCAmelCase : List[str]=1e-5 , __lowerCAmelCase : List[str]=1 , __lowerCAmelCase : int=0 , __lowerCAmelCase : Dict=2 , __lowerCAmelCase : Tuple=10_24 , __lowerCAmelCase : List[str]=1_28 , __lowerCAmelCase : Optional[int]=1_28 , __lowerCAmelCase : Any=True , __lowerCAmelCase : Optional[Any]=32 , __lowerCAmelCase : Any=1_28 , __lowerCAmelCase : str=64 , __lowerCAmelCase : Optional[int]=2_56 , __lowerCAmelCase : int=True , __lowerCAmelCase : int=True , __lowerCAmelCase : List[str]=True , __lowerCAmelCase : int=2_24 , __lowerCAmelCase : Dict=3 , __lowerCAmelCase : List[Any]=16 , __lowerCAmelCase : Dict=None , **__lowerCAmelCase : Optional[Any] , ) -> Dict: """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 , ) A__ = max_ad_position_embeddings A__ = coordinate_size A__ = shape_size A__ = has_relative_attention_bias A__ = rel_pos_bins A__ = max_rel_pos A__ = has_spatial_attention_bias A__ = rel_ad_pos_bins A__ = max_rel_ad_pos A__ = text_embed A__ = visual_embed A__ = input_size A__ = num_channels A__ = patch_size A__ = classifier_dropout class A (SCREAMING_SNAKE_CASE ): '''simple docstring''' __lowerCamelCase : List[str] = version.parse('''1.12''' ) @property def a_ ( self : int ) -> 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 a_ ( self : Optional[int] ) -> float: """simple docstring""" return 1e-5 @property def a_ ( self : Tuple ) -> int: """simple docstring""" return 12 def a_ ( self : str , __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 A__ = 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 A__ = processor.tokenizer.num_special_tokens_to_add(__lowerCAmelCase ) A__ = 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 A__ = [[""" """.join([processor.tokenizer.unk_token] ) * seq_length]] * batch_size # Generate dummy bounding boxes A__ = [[[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) A__ = self._generate_dummy_images(__lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase , __lowerCAmelCase ) A__ = dict( processor( __lowerCAmelCase , text=__lowerCAmelCase , boxes=__lowerCAmelCase , return_tensors=__lowerCAmelCase , ) ) return inputs
274
1
from math import pi def __lowerCamelCase ( __a :int , __a :int ) -> float: """simple docstring""" return 2 * pi * radius * (angle / 3_6_0) if __name__ == "__main__": print(arc_length(9_0, 1_0))
274
import json import os from pathlib import Path from shutil import copyfile from typing import Any, Dict, List, Optional, Tuple, Union import sentencepiece from ...tokenization_utils import BatchEncoding, PreTrainedTokenizer from ...utils import logging A : Optional[Any] = logging.get_logger(__name__) A : str = '''▁''' A : Any = { '''vocab_file''': '''vocab.json''', '''spm_file''': '''sentencepiece.bpe.model''', '''tokenizer_config_file''': '''tokenizer_config.json''', } A : List[Any] = { '''vocab_file''': { '''facebook/m2m100_418M''': '''https://huggingface.co/facebook/m2m100_418M/resolve/main/vocab.json''', '''facebook/m2m100_1.2B''': '''https://huggingface.co/facebook/m2m100_1.2B/resolve/main/vocab.json''', }, '''spm_file''': { '''facebook/m2m100_418M''': '''https://huggingface.co/facebook/m2m100_418M/resolve/main/sentencepiece.bpe.model''', '''facebook/m2m100_1.2B''': '''https://huggingface.co/facebook/m2m100_1.2B/resolve/main/sentencepiece.bpe.model''', }, '''tokenizer_config_file''': { '''facebook/m2m100_418M''': '''https://huggingface.co/facebook/m2m100_418M/resolve/main/tokenizer_config.json''', '''facebook/m2m100_1.2B''': '''https://huggingface.co/facebook/m2m100_1.2B/resolve/main/tokenizer_config.json''', }, } A : Tuple = { '''facebook/m2m100_418M''': 1_0_2_4, } # fmt: off A : Optional[int] = { '''m2m100''': ['''af''', '''am''', '''ar''', '''ast''', '''az''', '''ba''', '''be''', '''bg''', '''bn''', '''br''', '''bs''', '''ca''', '''ceb''', '''cs''', '''cy''', '''da''', '''de''', '''el''', '''en''', '''es''', '''et''', '''fa''', '''ff''', '''fi''', '''fr''', '''fy''', '''ga''', '''gd''', '''gl''', '''gu''', '''ha''', '''he''', '''hi''', '''hr''', '''ht''', '''hu''', '''hy''', '''id''', '''ig''', '''ilo''', '''is''', '''it''', '''ja''', '''jv''', '''ka''', '''kk''', '''km''', '''kn''', '''ko''', '''lb''', '''lg''', '''ln''', '''lo''', '''lt''', '''lv''', '''mg''', '''mk''', '''ml''', '''mn''', '''mr''', '''ms''', '''my''', '''ne''', '''nl''', '''no''', '''ns''', '''oc''', '''or''', '''pa''', '''pl''', '''ps''', '''pt''', '''ro''', '''ru''', '''sd''', '''si''', '''sk''', '''sl''', '''so''', '''sq''', '''sr''', '''ss''', '''su''', '''sv''', '''sw''', '''ta''', '''th''', '''tl''', '''tn''', '''tr''', '''uk''', '''ur''', '''uz''', '''vi''', '''wo''', '''xh''', '''yi''', '''yo''', '''zh''', '''zu'''], '''wmt21''': ['''en''', '''ha''', '''is''', '''ja''', '''cs''', '''ru''', '''zh''', '''de'''] } class A (SCREAMING_SNAKE_CASE ): '''simple docstring''' __lowerCamelCase : Union[str, Any] = VOCAB_FILES_NAMES __lowerCamelCase : List[str] = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES __lowerCamelCase : Optional[int] = PRETRAINED_VOCAB_FILES_MAP __lowerCamelCase : Dict = ['''input_ids''', '''attention_mask'''] __lowerCamelCase : List[int] = [] __lowerCamelCase : List[int] = [] def __init__( self : List[Any] , __lowerCAmelCase : List[Any] , __lowerCAmelCase : Dict , __lowerCAmelCase : Union[str, Any]=None , __lowerCAmelCase : str=None , __lowerCAmelCase : List[Any]="<s>" , __lowerCAmelCase : List[Any]="</s>" , __lowerCAmelCase : Optional[int]="</s>" , __lowerCAmelCase : Optional[Any]="<pad>" , __lowerCAmelCase : Any="<unk>" , __lowerCAmelCase : Any="m2m100" , __lowerCAmelCase : Optional[Dict[str, Any]] = None , __lowerCAmelCase : Dict=8 , **__lowerCAmelCase : Tuple , ) -> None: """simple docstring""" A__ = {} if sp_model_kwargs is None else sp_model_kwargs A__ = language_codes A__ = FAIRSEQ_LANGUAGE_CODES[language_codes] A__ = {lang_code: f'__{lang_code}__' for lang_code in fairseq_language_code} A__ = kwargs.get("""additional_special_tokens""" , [] ) kwargs["additional_special_tokens"] += [ self.get_lang_token(__lowerCAmelCase ) for lang_code in fairseq_language_code if self.get_lang_token(__lowerCAmelCase ) not in kwargs["additional_special_tokens"] ] super().__init__( src_lang=__lowerCAmelCase , tgt_lang=__lowerCAmelCase , bos_token=__lowerCAmelCase , eos_token=__lowerCAmelCase , sep_token=__lowerCAmelCase , unk_token=__lowerCAmelCase , pad_token=__lowerCAmelCase , language_codes=__lowerCAmelCase , sp_model_kwargs=self.sp_model_kwargs , num_madeup_words=__lowerCAmelCase , **__lowerCAmelCase , ) A__ = vocab_file A__ = load_json(__lowerCAmelCase ) A__ = {v: k for k, v in self.encoder.items()} A__ = spm_file A__ = load_spm(__lowerCAmelCase , self.sp_model_kwargs ) A__ = len(self.encoder ) A__ = { self.get_lang_token(__lowerCAmelCase ): self.encoder_size + i for i, lang_code in enumerate(__lowerCAmelCase ) } A__ = {lang_code: self.encoder_size + i for i, lang_code in enumerate(__lowerCAmelCase )} A__ = {v: k for k, v in self.lang_token_to_id.items()} A__ = src_lang if src_lang is not None else """en""" A__ = tgt_lang A__ = self.get_lang_id(self._src_lang ) self.set_src_lang_special_tokens(self._src_lang ) A__ = num_madeup_words @property def a_ ( self : Optional[int] ) -> int: """simple docstring""" return len(self.encoder ) + len(self.lang_token_to_id ) @property def a_ ( self : Optional[Any] ) -> str: """simple docstring""" return self._src_lang @src_lang.setter def a_ ( self : List[Any] , __lowerCAmelCase : str ) -> None: """simple docstring""" A__ = new_src_lang self.set_src_lang_special_tokens(self._src_lang ) def a_ ( self : Optional[int] , __lowerCAmelCase : str ) -> List[str]: """simple docstring""" return self.sp_model.encode(__lowerCAmelCase , out_type=__lowerCAmelCase ) def a_ ( self : Optional[Any] , __lowerCAmelCase : Dict ) -> Optional[Any]: """simple docstring""" if token in self.lang_token_to_id: return self.lang_token_to_id[token] return self.encoder.get(__lowerCAmelCase , self.encoder[self.unk_token] ) def a_ ( self : Optional[int] , __lowerCAmelCase : int ) -> str: """simple docstring""" if index in self.id_to_lang_token: return self.id_to_lang_token[index] return self.decoder.get(__lowerCAmelCase , self.unk_token ) def a_ ( self : Optional[int] , __lowerCAmelCase : Dict ) -> str: """simple docstring""" A__ = [] A__ = """""" for token in tokens: # make sure that special tokens are not decoded using sentencepiece model if token in self.all_special_tokens: out_string += self.sp_model.decode(__lowerCAmelCase ) + token A__ = [] else: current_sub_tokens.append(__lowerCAmelCase ) out_string += self.sp_model.decode(__lowerCAmelCase ) return out_string.strip() def a_ ( self : List[str] , __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 ) A__ = [1] * len(self.prefix_tokens ) A__ = [1] * len(self.suffix_tokens ) if token_ids_a is None: return prefix_ones + ([0] * len(__lowerCAmelCase )) + suffix_ones return prefix_ones + ([0] * len(__lowerCAmelCase )) + ([0] * len(__lowerCAmelCase )) + suffix_ones def a_ ( self : Tuple , __lowerCAmelCase : List[int] , __lowerCAmelCase : Optional[List[int]] = None ) -> List[int]: """simple docstring""" if token_ids_a is None: return self.prefix_tokens + token_ids_a + self.suffix_tokens # We don't expect to process pairs, but leave the pair logic for API consistency return self.prefix_tokens + token_ids_a + token_ids_a + self.suffix_tokens def a_ ( self : int ) -> Dict: """simple docstring""" A__ = {self.convert_ids_to_tokens(__lowerCAmelCase ): i for i in range(self.vocab_size )} vocab.update(self.added_tokens_encoder ) return vocab def __getstate__( self : Union[str, Any] ) -> Dict: """simple docstring""" A__ = self.__dict__.copy() A__ = None return state def __setstate__( self : str , __lowerCAmelCase : Dict ) -> None: """simple docstring""" A__ = d # for backward compatibility if not hasattr(self , """sp_model_kwargs""" ): A__ = {} A__ = load_spm(self.spm_file , self.sp_model_kwargs ) def a_ ( self : List[str] , __lowerCAmelCase : str , __lowerCAmelCase : Optional[str] = None ) -> Tuple[str]: """simple docstring""" A__ = Path(__lowerCAmelCase ) if not save_dir.is_dir(): raise OSError(f'{save_directory} should be a directory' ) A__ = save_dir / ( (filename_prefix + """-""" if filename_prefix else """""") + self.vocab_files_names["""vocab_file"""] ) A__ = save_dir / ( (filename_prefix + """-""" if filename_prefix else """""") + self.vocab_files_names["""spm_file"""] ) save_json(self.encoder , __lowerCAmelCase ) if os.path.abspath(self.spm_file ) != os.path.abspath(__lowerCAmelCase ) and os.path.isfile(self.spm_file ): copyfile(self.spm_file , __lowerCAmelCase ) elif not os.path.isfile(self.spm_file ): with open(__lowerCAmelCase , """wb""" ) as fi: A__ = self.sp_model.serialized_model_proto() fi.write(__lowerCAmelCase ) return (str(__lowerCAmelCase ), str(__lowerCAmelCase )) def a_ ( self : str , __lowerCAmelCase : List[str] , __lowerCAmelCase : str = "en" , __lowerCAmelCase : Optional[List[str]] = None , __lowerCAmelCase : str = "ro" , **__lowerCAmelCase : List[Any] , ) -> BatchEncoding: """simple docstring""" A__ = src_lang A__ = tgt_lang self.set_src_lang_special_tokens(self.src_lang ) return super().prepare_seqaseq_batch(__lowerCAmelCase , __lowerCAmelCase , **__lowerCAmelCase ) def a_ ( self : Optional[int] , __lowerCAmelCase : Any , __lowerCAmelCase : Optional[str] , __lowerCAmelCase : Optional[str] , **__lowerCAmelCase : Tuple ) -> Tuple: """simple docstring""" if src_lang is None or tgt_lang is None: raise ValueError("""Translation requires a `src_lang` and a `tgt_lang` for this model""" ) A__ = src_lang A__ = self(__lowerCAmelCase , add_special_tokens=__lowerCAmelCase , **__lowerCAmelCase ) A__ = self.get_lang_id(__lowerCAmelCase ) A__ = tgt_lang_id return inputs def a_ ( self : Dict ) -> int: """simple docstring""" self.set_src_lang_special_tokens(self.src_lang ) def a_ ( self : Union[str, Any] ) -> Union[str, Any]: """simple docstring""" self.set_tgt_lang_special_tokens(self.tgt_lang ) def a_ ( self : str , __lowerCAmelCase : str ) -> None: """simple docstring""" A__ = self.get_lang_token(__lowerCAmelCase ) A__ = self.lang_token_to_id[lang_token] A__ = [self.cur_lang_id] A__ = [self.eos_token_id] def a_ ( self : Tuple , __lowerCAmelCase : str ) -> None: """simple docstring""" A__ = self.get_lang_token(__lowerCAmelCase ) A__ = self.lang_token_to_id[lang_token] A__ = [self.cur_lang_id] A__ = [self.eos_token_id] def a_ ( self : Union[str, Any] , __lowerCAmelCase : str ) -> str: """simple docstring""" return self.lang_code_to_token[lang] def a_ ( self : Union[str, Any] , __lowerCAmelCase : str ) -> int: """simple docstring""" A__ = self.get_lang_token(__lowerCAmelCase ) return self.lang_token_to_id[lang_token] def __lowerCamelCase ( __a :str , __a :Dict[str, Any] ) -> sentencepiece.SentencePieceProcessor: """simple docstring""" A__ = sentencepiece.SentencePieceProcessor(**__a ) spm.Load(str(__a ) ) return spm def __lowerCamelCase ( __a :str ) -> Union[Dict, List]: """simple docstring""" with open(__a , """r""" ) as f: return json.load(__a ) def __lowerCamelCase ( __a :List[Any] , __a :str ) -> None: """simple docstring""" with open(__a , """w""" ) as f: json.dump(__a , __a , indent=2 )
274
1
from typing import List from ...configuration_utils import PretrainedConfig from ...utils import logging A : List[str] = logging.get_logger(__name__) A : Union[str, Any] = { '''snap-research/efficientformer-l1-300''': ( '''https://huggingface.co/snap-research/efficientformer-l1-300/resolve/main/config.json''' ), } class A (SCREAMING_SNAKE_CASE ): '''simple docstring''' __lowerCamelCase : Tuple = '''efficientformer''' def __init__( self : List[Any] , __lowerCAmelCase : List[int] = [3, 2, 6, 4] , __lowerCAmelCase : List[int] = [48, 96, 2_24, 4_48] , __lowerCAmelCase : List[bool] = [True, True, True, True] , __lowerCAmelCase : int = 4_48 , __lowerCAmelCase : int = 32 , __lowerCAmelCase : int = 4 , __lowerCAmelCase : int = 7 , __lowerCAmelCase : int = 5 , __lowerCAmelCase : int = 8 , __lowerCAmelCase : int = 4 , __lowerCAmelCase : float = 0.0 , __lowerCAmelCase : int = 16 , __lowerCAmelCase : int = 3 , __lowerCAmelCase : int = 3 , __lowerCAmelCase : int = 3 , __lowerCAmelCase : int = 2 , __lowerCAmelCase : int = 1 , __lowerCAmelCase : float = 0.0 , __lowerCAmelCase : int = 1 , __lowerCAmelCase : bool = True , __lowerCAmelCase : bool = True , __lowerCAmelCase : float = 1e-5 , __lowerCAmelCase : str = "gelu" , __lowerCAmelCase : float = 0.0_2 , __lowerCAmelCase : float = 1e-12 , __lowerCAmelCase : int = 2_24 , __lowerCAmelCase : float = 1e-05 , **__lowerCAmelCase : Optional[Any] , ) -> None: """simple docstring""" super().__init__(**__lowerCAmelCase ) A__ = hidden_act A__ = hidden_dropout_prob A__ = hidden_sizes A__ = num_hidden_layers A__ = num_attention_heads A__ = initializer_range A__ = layer_norm_eps A__ = patch_size A__ = num_channels A__ = depths A__ = mlp_expansion_ratio A__ = downsamples A__ = dim A__ = key_dim A__ = attention_ratio A__ = resolution A__ = pool_size A__ = downsample_patch_size A__ = downsample_stride A__ = downsample_pad A__ = drop_path_rate A__ = num_metaad_blocks A__ = distillation A__ = use_layer_scale A__ = layer_scale_init_value A__ = image_size A__ = batch_norm_eps
274
from __future__ import annotations from PIL import Image # Define glider example A : Any = [ [0, 1, 0, 0, 0, 0, 0, 0], [0, 0, 1, 0, 0, 0, 0, 0], [1, 1, 1, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], ] # Define blinker example A : Optional[Any] = [[0, 1, 0], [0, 1, 0], [0, 1, 0]] def __lowerCamelCase ( __a :list[list[int]] ) -> list[list[int]]: """simple docstring""" A__ = [] for i in range(len(__a ) ): A__ = [] for j in range(len(cells[i] ) ): # Get the number of live neighbours A__ = 0 if i > 0 and j > 0: neighbour_count += cells[i - 1][j - 1] if i > 0: neighbour_count += cells[i - 1][j] if i > 0 and j < len(cells[i] ) - 1: neighbour_count += cells[i - 1][j + 1] if j > 0: neighbour_count += cells[i][j - 1] if j < len(cells[i] ) - 1: neighbour_count += cells[i][j + 1] if i < len(__a ) - 1 and j > 0: neighbour_count += cells[i + 1][j - 1] if i < len(__a ) - 1: neighbour_count += cells[i + 1][j] if i < len(__a ) - 1 and j < len(cells[i] ) - 1: neighbour_count += cells[i + 1][j + 1] # Rules of the game of life (excerpt from Wikipedia): # 1. Any live cell with two or three live neighbours survives. # 2. Any dead cell with three live neighbours becomes a live cell. # 3. All other live cells die in the next generation. # Similarly, all other dead cells stay dead. A__ = cells[i][j] == 1 if ( (alive and 2 <= neighbour_count <= 3) or not alive and neighbour_count == 3 ): next_generation_row.append(1 ) else: next_generation_row.append(0 ) next_generation.append(__a ) return next_generation def __lowerCamelCase ( __a :list[list[int]] , __a :int ) -> list[Image.Image]: """simple docstring""" A__ = [] for _ in range(__a ): # Create output image A__ = Image.new("""RGB""" , (len(cells[0] ), len(__a )) ) A__ = img.load() # Save cells to image for x in range(len(__a ) ): for y in range(len(cells[0] ) ): A__ = 2_5_5 - cells[y][x] * 2_5_5 A__ = (colour, colour, colour) # Save image images.append(__a ) A__ = new_generation(__a ) return images if __name__ == "__main__": A : str = generate_images(GLIDER, 1_6) images[0].save('''out.gif''', save_all=True, append_images=images[1:])
274
1
import unittest import torch from diffusers import VQModel from diffusers.utils import floats_tensor, torch_device from diffusers.utils.testing_utils import enable_full_determinism from .test_modeling_common import ModelTesterMixin, UNetTesterMixin enable_full_determinism() class A (SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' __lowerCamelCase : Optional[Any] = VQModel __lowerCamelCase : Union[str, Any] = '''sample''' @property def a_ ( self : List[str] , __lowerCAmelCase : List[Any]=(32, 32) ) -> List[Any]: """simple docstring""" A__ = 4 A__ = 3 A__ = floats_tensor((batch_size, num_channels) + sizes ).to(__lowerCAmelCase ) return {"sample": image} @property def a_ ( self : Union[str, Any] ) -> str: """simple docstring""" return (3, 32, 32) @property def a_ ( self : int ) -> Optional[Any]: """simple docstring""" return (3, 32, 32) def a_ ( self : Tuple ) -> List[str]: """simple docstring""" A__ = { """block_out_channels""": [32, 64], """in_channels""": 3, """out_channels""": 3, """down_block_types""": ["""DownEncoderBlock2D""", """DownEncoderBlock2D"""], """up_block_types""": ["""UpDecoderBlock2D""", """UpDecoderBlock2D"""], """latent_channels""": 3, } A__ = self.dummy_input return init_dict, inputs_dict def a_ ( self : Optional[Any] ) -> int: """simple docstring""" pass def a_ ( self : List[Any] ) -> int: """simple docstring""" pass def a_ ( self : Union[str, Any] ) -> Optional[Any]: """simple docstring""" A__ , A__ = VQModel.from_pretrained("""fusing/vqgan-dummy""" , output_loading_info=__lowerCAmelCase ) self.assertIsNotNone(__lowerCAmelCase ) self.assertEqual(len(loading_info["""missing_keys"""] ) , 0 ) model.to(__lowerCAmelCase ) A__ = model(**self.dummy_input ) assert image is not None, "Make sure output is not None" def a_ ( self : List[Any] ) -> int: """simple docstring""" A__ = VQModel.from_pretrained("""fusing/vqgan-dummy""" ) model.to(__lowerCAmelCase ).eval() torch.manual_seed(0 ) if torch.cuda.is_available(): torch.cuda.manual_seed_all(0 ) A__ = torch.randn(1 , model.config.in_channels , model.config.sample_size , model.config.sample_size ) A__ = image.to(__lowerCAmelCase ) with torch.no_grad(): A__ = model(__lowerCAmelCase ).sample A__ = output[0, -1, -3:, -3:].flatten().cpu() # fmt: off A__ = torch.tensor([-0.0_1_5_3, -0.4_0_4_4, -0.1_8_8_0, -0.5_1_6_1, -0.2_4_1_8, -0.4_0_7_2, -0.1_6_1_2, -0.0_6_3_3, -0.0_1_4_3] ) # fmt: on self.assertTrue(torch.allclose(__lowerCAmelCase , __lowerCAmelCase , atol=1e-3 ) )
274
from datetime import datetime import requests from bsa import BeautifulSoup if __name__ == "__main__": A : List[str] = input('''Enter image url: ''').strip() print(F'''Downloading image from {url} ...''') A : Any = BeautifulSoup(requests.get(url).content, '''html.parser''') # The image URL is in the content field of the first meta tag with property og:image A : List[Any] = soup.find('''meta''', {'''property''': '''og:image'''})['''content'''] A : Dict = requests.get(image_url).content A : Tuple = F'''{datetime.now():%Y-%m-%d_%H:%M:%S}.jpg''' with open(file_name, '''wb''') as fp: fp.write(image_data) print(F'''Done. Image saved to disk as {file_name}.''')
274
1
def __lowerCamelCase ( __a :List[str] ) -> int: """simple docstring""" A__ = [] A__ = [] A__ = { """^""": 3, """*""": 2, """/""": 2, """%""": 2, """+""": 1, """-""": 1, } # Priority of each operator A__ = len(__a ) if (len(__a ) > 7) else 7 # Print table header for output print( """Symbol""".center(8 ) , """Stack""".center(__a ) , """Postfix""".center(__a ) , sep=""" | """ , ) print("""-""" * (print_width * 3 + 7) ) for x in infix: if x.isalpha() or x.isdigit(): post_fix.append(__a ) # if x is Alphabet / Digit, add it to Postfix elif x == "(": stack.append(__a ) # if x is "(" push to Stack elif x == ")": # if x is ")" pop stack until "(" is encountered while stack[-1] != "(": post_fix.append(stack.pop() ) # Pop stack & add the content to Postfix stack.pop() else: if len(__a ) == 0: stack.append(__a ) # If stack is empty, push x to stack else: # while priority of x is not > priority of element in the stack while len(__a ) > 0 and priority[x] <= priority[stack[-1]]: post_fix.append(stack.pop() ) # pop stack & add to Postfix stack.append(__a ) # push x to stack print( x.center(8 ) , ("""""".join(__a )).ljust(__a ) , ("""""".join(__a )).ljust(__a ) , sep=""" | """ , ) # Output in tabular format while len(__a ) > 0: # while stack is not empty post_fix.append(stack.pop() ) # pop stack & add to Postfix print( """ """.center(8 ) , ("""""".join(__a )).ljust(__a ) , ("""""".join(__a )).ljust(__a ) , sep=""" | """ , ) # Output in tabular format return "".join(__a ) # return Postfix as str def __lowerCamelCase ( __a :Tuple ) -> Optional[Any]: """simple docstring""" A__ = list(infix[::-1] ) # reverse the infix equation for i in range(len(__a ) ): if infix[i] == "(": A__ = """)""" # change "(" to ")" elif infix[i] == ")": A__ = """(""" # change ")" to "(" return (infix_2_postfix("""""".join(__a ) ))[ ::-1 ] # call infix_2_postfix on Infix, return reverse of Postfix if __name__ == "__main__": A : Tuple = input('''\nEnter an Infix Equation = ''') # Input an Infix equation A : List[Any] = ''''''.join(Infix.split()) # Remove spaces from the input print('''\n\t''', Infix, '''(Infix) -> ''', infix_2_prefix(Infix), '''(Prefix)''')
274
# This code is adapted from OpenAI's release # https://github.com/openai/human-eval/blob/master/human_eval/execution.py import contextlib import faulthandler import io import multiprocessing import os import platform import signal import tempfile def __lowerCamelCase ( __a :List[str] , __a :List[Any] , __a :Union[str, Any] , __a :List[Any] ) -> Dict: """simple docstring""" A__ = multiprocessing.Manager() A__ = manager.list() A__ = multiprocessing.Process(target=__a , args=(check_program, result, timeout) ) p.start() p.join(timeout=timeout + 1 ) if p.is_alive(): p.kill() if not result: result.append("""timed out""" ) return { "task_id": task_id, "passed": result[0] == "passed", "result": result[0], "completion_id": completion_id, } def __lowerCamelCase ( __a :Optional[Any] , __a :Any , __a :List[Any] ) -> Union[str, Any]: """simple docstring""" with create_tempdir(): # These system calls are needed when cleaning up tempdir. import os import shutil A__ = shutil.rmtree A__ = os.rmdir A__ = os.chdir # Disable functionalities that can make destructive changes to the test. reliability_guard() # Run program. try: A__ = {} with swallow_io(): with time_limit(__a ): exec(__a , __a ) result.append("""passed""" ) except TimeoutException: result.append("""timed out""" ) except BaseException as e: result.append(F'failed: {e}' ) # Needed for cleaning up. A__ = rmtree A__ = rmdir A__ = chdir @contextlib.contextmanager def __lowerCamelCase ( __a :List[str] ) -> Dict: """simple docstring""" def signal_handler(__a :List[Any] , __a :Optional[Any] ): raise TimeoutException("""Timed out!""" ) signal.setitimer(signal.ITIMER_REAL , __a ) signal.signal(signal.SIGALRM , __a ) try: yield finally: signal.setitimer(signal.ITIMER_REAL , 0 ) @contextlib.contextmanager def __lowerCamelCase ( ) -> Union[str, Any]: """simple docstring""" A__ = WriteOnlyStringIO() with contextlib.redirect_stdout(__a ): with contextlib.redirect_stderr(__a ): with redirect_stdin(__a ): yield @contextlib.contextmanager def __lowerCamelCase ( ) -> Dict: """simple docstring""" with tempfile.TemporaryDirectory() as dirname: with chdir(__a ): yield dirname class A (SCREAMING_SNAKE_CASE ): '''simple docstring''' pass class A (io.StringIO ): '''simple docstring''' def a_ ( self : Any , *__lowerCAmelCase : List[str] , **__lowerCAmelCase : str ) -> Dict: """simple docstring""" raise OSError def a_ ( self : Optional[Any] , *__lowerCAmelCase : Any , **__lowerCAmelCase : Optional[int] ) -> str: """simple docstring""" raise OSError def a_ ( self : Optional[Any] , *__lowerCAmelCase : Any , **__lowerCAmelCase : Any ) -> int: """simple docstring""" raise OSError def a_ ( self : str , *__lowerCAmelCase : Any , **__lowerCAmelCase : Union[str, Any] ) -> int: """simple docstring""" return False class A (contextlib._RedirectStream ): # type: ignore '''simple docstring''' __lowerCamelCase : Union[str, Any] = '''stdin''' @contextlib.contextmanager def __lowerCamelCase ( __a :Union[str, Any] ) -> List[str]: """simple docstring""" if root == ".": yield return A__ = os.getcwd() os.chdir(__a ) try: yield except BaseException as exc: raise exc finally: os.chdir(__a ) def __lowerCamelCase ( __a :Union[str, Any]=None ) -> Dict: """simple docstring""" if maximum_memory_bytes is not None: import resource resource.setrlimit(resource.RLIMIT_AS , (maximum_memory_bytes, maximum_memory_bytes) ) resource.setrlimit(resource.RLIMIT_DATA , (maximum_memory_bytes, maximum_memory_bytes) ) if not platform.uname().system == "Darwin": resource.setrlimit(resource.RLIMIT_STACK , (maximum_memory_bytes, maximum_memory_bytes) ) faulthandler.disable() import builtins A__ = None A__ = None import os A__ = """1""" A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None A__ = None import shutil A__ = None A__ = None A__ = None import subprocess A__ = None # type: ignore A__ = None import sys A__ = None A__ = None A__ = None A__ = None A__ = None
274
1
from __future__ import annotations import unittest from transformers import is_tf_available from transformers.testing_utils import require_sentencepiece, require_tf, require_tokenizers, slow from ...test_configuration_common import ConfigTester from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor, random_attention_mask from ...test_pipeline_mixin import PipelineTesterMixin if is_tf_available(): import numpy as np import tensorflow as tf from transformers import ( TF_FLAUBERT_PRETRAINED_MODEL_ARCHIVE_LIST, FlaubertConfig, TFFlaubertForMultipleChoice, TFFlaubertForQuestionAnsweringSimple, TFFlaubertForSequenceClassification, TFFlaubertForTokenClassification, TFFlaubertModel, TFFlaubertWithLMHeadModel, ) class A : '''simple docstring''' def __init__( self : Tuple , __lowerCAmelCase : int , ) -> Union[str, Any]: """simple docstring""" A__ = parent A__ = 13 A__ = 7 A__ = True A__ = True A__ = True A__ = True A__ = True A__ = False A__ = False A__ = False A__ = 2 A__ = 99 A__ = 0 A__ = 32 A__ = 2 A__ = 4 A__ = 0.1 A__ = 0.1 A__ = 5_12 A__ = 16 A__ = 2 A__ = 0.0_2 A__ = 3 A__ = 4 A__ = """last""" A__ = True A__ = None A__ = 0 def a_ ( self : Any ) -> Optional[Any]: """simple docstring""" A__ = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) A__ = random_attention_mask([self.batch_size, self.seq_length] , dtype=tf.floataa ) A__ = None if self.use_input_lengths: A__ = ( ids_tensor([self.batch_size] , vocab_size=2 ) + self.seq_length - 2 ) # small variation of seq_length A__ = None if self.use_token_type_ids: A__ = ids_tensor([self.batch_size, self.seq_length] , self.n_langs ) A__ = None A__ = None A__ = None if self.use_labels: A__ = ids_tensor([self.batch_size] , self.type_sequence_label_size ) A__ = ids_tensor([self.batch_size, self.seq_length] , self.num_labels ) A__ = ids_tensor([self.batch_size] , 2 , dtype=tf.floataa ) A__ = ids_tensor([self.batch_size] , self.num_choices ) A__ = FlaubertConfig( vocab_size=self.vocab_size , n_special=self.n_special , emb_dim=self.hidden_size , n_layers=self.num_hidden_layers , n_heads=self.num_attention_heads , dropout=self.hidden_dropout_prob , attention_dropout=self.attention_probs_dropout_prob , gelu_activation=self.gelu_activation , sinusoidal_embeddings=self.sinusoidal_embeddings , asm=self.asm , causal=self.causal , n_langs=self.n_langs , max_position_embeddings=self.max_position_embeddings , initializer_range=self.initializer_range , summary_type=self.summary_type , use_proj=self.use_proj , bos_token_id=self.bos_token_id , ) return ( config, input_ids, token_type_ids, input_lengths, sequence_labels, token_labels, is_impossible_labels, choice_labels, input_mask, ) def a_ ( self : Union[str, Any] , __lowerCAmelCase : Optional[int] , __lowerCAmelCase : List[str] , __lowerCAmelCase : Optional[Any] , __lowerCAmelCase : Tuple , __lowerCAmelCase : Optional[Any] , __lowerCAmelCase : int , __lowerCAmelCase : Optional[int] , __lowerCAmelCase : Any , __lowerCAmelCase : List[Any] , ) -> Optional[Any]: """simple docstring""" A__ = TFFlaubertModel(config=__lowerCAmelCase ) A__ = {"""input_ids""": input_ids, """lengths""": input_lengths, """langs""": token_type_ids} A__ = model(__lowerCAmelCase ) A__ = [input_ids, input_mask] A__ = model(__lowerCAmelCase ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def a_ ( self : int , __lowerCAmelCase : Tuple , __lowerCAmelCase : int , __lowerCAmelCase : List[Any] , __lowerCAmelCase : List[str] , __lowerCAmelCase : Tuple , __lowerCAmelCase : int , __lowerCAmelCase : List[str] , __lowerCAmelCase : str , __lowerCAmelCase : Tuple , ) -> Tuple: """simple docstring""" A__ = TFFlaubertWithLMHeadModel(__lowerCAmelCase ) A__ = {"""input_ids""": input_ids, """lengths""": input_lengths, """langs""": token_type_ids} A__ = model(__lowerCAmelCase ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.vocab_size) ) def a_ ( self : List[Any] , __lowerCAmelCase : List[Any] , __lowerCAmelCase : int , __lowerCAmelCase : List[Any] , __lowerCAmelCase : List[str] , __lowerCAmelCase : List[str] , __lowerCAmelCase : Any , __lowerCAmelCase : List[Any] , __lowerCAmelCase : List[Any] , __lowerCAmelCase : Union[str, Any] , ) -> str: """simple docstring""" A__ = TFFlaubertForQuestionAnsweringSimple(__lowerCAmelCase ) A__ = {"""input_ids""": input_ids, """lengths""": input_lengths} A__ = model(__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 a_ ( self : Any , __lowerCAmelCase : Optional[Any] , __lowerCAmelCase : List[Any] , __lowerCAmelCase : List[str] , __lowerCAmelCase : str , __lowerCAmelCase : Dict , __lowerCAmelCase : Optional[int] , __lowerCAmelCase : Optional[Any] , __lowerCAmelCase : Dict , __lowerCAmelCase : Any , ) -> Dict: """simple docstring""" A__ = TFFlaubertForSequenceClassification(__lowerCAmelCase ) A__ = {"""input_ids""": input_ids, """lengths""": input_lengths} A__ = model(__lowerCAmelCase ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.type_sequence_label_size) ) def a_ ( self : Optional[int] , __lowerCAmelCase : Union[str, Any] , __lowerCAmelCase : int , __lowerCAmelCase : Dict , __lowerCAmelCase : Optional[Any] , __lowerCAmelCase : List[str] , __lowerCAmelCase : Optional[int] , __lowerCAmelCase : Union[str, Any] , __lowerCAmelCase : int , __lowerCAmelCase : Optional[Any] , ) -> Any: """simple docstring""" A__ = self.num_labels A__ = TFFlaubertForTokenClassification(config=__lowerCAmelCase ) A__ = {"""input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids} A__ = model(__lowerCAmelCase ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.num_labels) ) def a_ ( self : List[Any] , __lowerCAmelCase : str , __lowerCAmelCase : Optional[int] , __lowerCAmelCase : Optional[int] , __lowerCAmelCase : Optional[int] , __lowerCAmelCase : int , __lowerCAmelCase : str , __lowerCAmelCase : str , __lowerCAmelCase : Union[str, Any] , __lowerCAmelCase : Optional[int] , ) -> Union[str, Any]: """simple docstring""" A__ = self.num_choices A__ = TFFlaubertForMultipleChoice(config=__lowerCAmelCase ) A__ = tf.tile(tf.expand_dims(__lowerCAmelCase , 1 ) , (1, self.num_choices, 1) ) A__ = tf.tile(tf.expand_dims(__lowerCAmelCase , 1 ) , (1, self.num_choices, 1) ) A__ = tf.tile(tf.expand_dims(__lowerCAmelCase , 1 ) , (1, self.num_choices, 1) ) A__ = { """input_ids""": multiple_choice_inputs_ids, """attention_mask""": multiple_choice_input_mask, """token_type_ids""": multiple_choice_token_type_ids, } A__ = model(__lowerCAmelCase ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_choices) ) def a_ ( self : List[Any] ) -> Dict: """simple docstring""" A__ = self.prepare_config_and_inputs() ( ( A__ ) , ( A__ ) , ( A__ ) , ( A__ ) , ( A__ ) , ( A__ ) , ( A__ ) , ( A__ ) , ( A__ ) , ) = config_and_inputs A__ = { """input_ids""": input_ids, """token_type_ids""": token_type_ids, """langs""": token_type_ids, """lengths""": input_lengths, } return config, inputs_dict @require_tf class A (SCREAMING_SNAKE_CASE , SCREAMING_SNAKE_CASE , unittest.TestCase ): '''simple docstring''' __lowerCamelCase : str = ( ( TFFlaubertModel, TFFlaubertWithLMHeadModel, TFFlaubertForSequenceClassification, TFFlaubertForQuestionAnsweringSimple, TFFlaubertForTokenClassification, TFFlaubertForMultipleChoice, ) if is_tf_available() else () ) __lowerCamelCase : str = ( (TFFlaubertWithLMHeadModel,) if is_tf_available() else () ) # TODO (PVP): Check other models whether language generation is also applicable __lowerCamelCase : List[Any] = ( { '''feature-extraction''': TFFlaubertModel, '''fill-mask''': TFFlaubertWithLMHeadModel, '''question-answering''': TFFlaubertForQuestionAnsweringSimple, '''text-classification''': TFFlaubertForSequenceClassification, '''token-classification''': TFFlaubertForTokenClassification, '''zero-shot''': TFFlaubertForSequenceClassification, } if is_tf_available() else {} ) __lowerCamelCase : Optional[int] = False __lowerCamelCase : Optional[Any] = False def a_ ( self : str , __lowerCAmelCase : Any , __lowerCAmelCase : Dict , __lowerCAmelCase : Optional[Any] , __lowerCAmelCase : Union[str, Any] , __lowerCAmelCase : Optional[int] ) -> str: """simple docstring""" if ( pipeline_test_casse_name == "QAPipelineTests" and tokenizer_name is not None and not tokenizer_name.endswith("""Fast""" ) ): # `QAPipelineTests` fails for a few models when the slower tokenizer are used. # (The slower tokenizers were never used for pipeline tests before the pipeline testing rework) # TODO: check (and possibly fix) the `QAPipelineTests` with slower tokenizer return True return False def a_ ( self : List[str] ) -> Union[str, Any]: """simple docstring""" A__ = TFFlaubertModelTester(self ) A__ = ConfigTester(self , config_class=__lowerCAmelCase , emb_dim=37 ) def a_ ( self : Optional[int] ) -> Union[str, Any]: """simple docstring""" self.config_tester.run_common_tests() def a_ ( self : Optional[Any] ) -> Dict: """simple docstring""" A__ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_flaubert_model(*__lowerCAmelCase ) def a_ ( self : Optional[Any] ) -> Optional[int]: """simple docstring""" A__ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_flaubert_lm_head(*__lowerCAmelCase ) def a_ ( self : Any ) -> List[Any]: """simple docstring""" A__ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_flaubert_qa(*__lowerCAmelCase ) def a_ ( self : str ) -> Dict: """simple docstring""" A__ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_flaubert_sequence_classif(*__lowerCAmelCase ) def a_ ( self : Tuple ) -> List[Any]: """simple docstring""" A__ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_flaubert_for_token_classification(*__lowerCAmelCase ) def a_ ( self : int ) -> Union[str, Any]: """simple docstring""" A__ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_flaubert_for_multiple_choice(*__lowerCAmelCase ) @slow def a_ ( self : int ) -> List[Any]: """simple docstring""" for model_name in TF_FLAUBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: A__ = TFFlaubertModel.from_pretrained(__lowerCAmelCase ) self.assertIsNotNone(__lowerCAmelCase ) @require_tf @require_sentencepiece @require_tokenizers class A (unittest.TestCase ): '''simple docstring''' @slow def a_ ( self : Any ) -> str: """simple docstring""" A__ = TFFlaubertModel.from_pretrained("""jplu/tf-flaubert-small-cased""" ) A__ = tf.convert_to_tensor( [[0, 1_58, 7_35, 25_92, 14_24, 67_27, 82, 1]] , dtype=tf.intaa , ) # "J'aime flaubert !" A__ = model(__lowerCAmelCase )[0] A__ = tf.TensorShape((1, 8, 5_12) ) self.assertEqual(output.shape , __lowerCAmelCase ) # compare the actual values for a slice. A__ = tf.convert_to_tensor( [ [ [-1.8_7_6_8_7_7_3, -1.5_6_6_5_5_5, 0.2_7_0_7_2_4_1_8], [-1.6_9_2_0_0_3_8, -0.5_8_7_3_5_0_5, 1.9_3_2_9_5_9_9], [-2.9_5_6_3_9_8_5, -1.6_9_9_3_8_3_5, 1.7_9_7_2_0_5_2], ] ] , dtype=tf.floataa , ) self.assertTrue(np.allclose(output[:, :3, :3].numpy() , expected_slice.numpy() , atol=1e-4 ) )
274
import os from shutil import copyfile from typing import List, Optional, Tuple from ...tokenization_utils import AddedToken from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import is_sentencepiece_available, logging if is_sentencepiece_available(): from .tokenization_albert import AlbertTokenizer else: A : Tuple = None A : Optional[Any] = logging.get_logger(__name__) A : Tuple = {'''vocab_file''': '''spiece.model''', '''tokenizer_file''': '''tokenizer.json'''} A : List[str] = { '''vocab_file''': { '''albert-base-v1''': '''https://huggingface.co/albert-base-v1/resolve/main/spiece.model''', '''albert-large-v1''': '''https://huggingface.co/albert-large-v1/resolve/main/spiece.model''', '''albert-xlarge-v1''': '''https://huggingface.co/albert-xlarge-v1/resolve/main/spiece.model''', '''albert-xxlarge-v1''': '''https://huggingface.co/albert-xxlarge-v1/resolve/main/spiece.model''', '''albert-base-v2''': '''https://huggingface.co/albert-base-v2/resolve/main/spiece.model''', '''albert-large-v2''': '''https://huggingface.co/albert-large-v2/resolve/main/spiece.model''', '''albert-xlarge-v2''': '''https://huggingface.co/albert-xlarge-v2/resolve/main/spiece.model''', '''albert-xxlarge-v2''': '''https://huggingface.co/albert-xxlarge-v2/resolve/main/spiece.model''', }, '''tokenizer_file''': { '''albert-base-v1''': '''https://huggingface.co/albert-base-v1/resolve/main/tokenizer.json''', '''albert-large-v1''': '''https://huggingface.co/albert-large-v1/resolve/main/tokenizer.json''', '''albert-xlarge-v1''': '''https://huggingface.co/albert-xlarge-v1/resolve/main/tokenizer.json''', '''albert-xxlarge-v1''': '''https://huggingface.co/albert-xxlarge-v1/resolve/main/tokenizer.json''', '''albert-base-v2''': '''https://huggingface.co/albert-base-v2/resolve/main/tokenizer.json''', '''albert-large-v2''': '''https://huggingface.co/albert-large-v2/resolve/main/tokenizer.json''', '''albert-xlarge-v2''': '''https://huggingface.co/albert-xlarge-v2/resolve/main/tokenizer.json''', '''albert-xxlarge-v2''': '''https://huggingface.co/albert-xxlarge-v2/resolve/main/tokenizer.json''', }, } A : List[str] = { '''albert-base-v1''': 5_1_2, '''albert-large-v1''': 5_1_2, '''albert-xlarge-v1''': 5_1_2, '''albert-xxlarge-v1''': 5_1_2, '''albert-base-v2''': 5_1_2, '''albert-large-v2''': 5_1_2, '''albert-xlarge-v2''': 5_1_2, '''albert-xxlarge-v2''': 5_1_2, } A : Optional[int] = '''▁''' class A (SCREAMING_SNAKE_CASE ): '''simple docstring''' __lowerCamelCase : str = VOCAB_FILES_NAMES __lowerCamelCase : List[Any] = PRETRAINED_VOCAB_FILES_MAP __lowerCamelCase : Dict = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES __lowerCamelCase : List[str] = AlbertTokenizer def __init__( self : Tuple , __lowerCAmelCase : Tuple=None , __lowerCAmelCase : List[str]=None , __lowerCAmelCase : Optional[Any]=True , __lowerCAmelCase : List[Any]=True , __lowerCAmelCase : str=False , __lowerCAmelCase : Union[str, Any]="[CLS]" , __lowerCAmelCase : int="[SEP]" , __lowerCAmelCase : Dict="<unk>" , __lowerCAmelCase : Dict="[SEP]" , __lowerCAmelCase : Union[str, Any]="<pad>" , __lowerCAmelCase : str="[CLS]" , __lowerCAmelCase : int="[MASK]" , **__lowerCAmelCase : Optional[Any] , ) -> Optional[Any]: """simple docstring""" A__ = ( AddedToken(__lowerCAmelCase , lstrip=__lowerCAmelCase , rstrip=__lowerCAmelCase , normalized=__lowerCAmelCase ) if isinstance(__lowerCAmelCase , __lowerCAmelCase ) else mask_token ) super().__init__( __lowerCAmelCase , tokenizer_file=__lowerCAmelCase , do_lower_case=__lowerCAmelCase , remove_space=__lowerCAmelCase , keep_accents=__lowerCAmelCase , bos_token=__lowerCAmelCase , eos_token=__lowerCAmelCase , unk_token=__lowerCAmelCase , sep_token=__lowerCAmelCase , pad_token=__lowerCAmelCase , cls_token=__lowerCAmelCase , mask_token=__lowerCAmelCase , **__lowerCAmelCase , ) A__ = do_lower_case A__ = remove_space A__ = keep_accents A__ = vocab_file A__ = False if not self.vocab_file else True def a_ ( self : List[Any] , __lowerCAmelCase : List[int] , __lowerCAmelCase : Optional[List[int]] = None ) -> List[int]: """simple docstring""" A__ = [self.sep_token_id] A__ = [self.cls_token_id] if token_ids_a is None: return cls + token_ids_a + sep return cls + token_ids_a + sep + token_ids_a + sep def a_ ( self : Union[str, Any] , __lowerCAmelCase : List[int] , __lowerCAmelCase : Optional[List[int]] = None ) -> List[int]: """simple docstring""" A__ = [self.sep_token_id] A__ = [self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep ) * [0] return len(cls + token_ids_a + sep ) * [0] + len(token_ids_a + sep ) * [1] def a_ ( self : Tuple , __lowerCAmelCase : str , __lowerCAmelCase : Optional[str] = None ) -> Tuple[str]: """simple docstring""" if not self.can_save_slow_tokenizer: raise ValueError( """Your fast tokenizer does not have the necessary information to save the vocabulary for a slow """ """tokenizer.""" ) if not os.path.isdir(__lowerCAmelCase ): logger.error(f'Vocabulary path ({save_directory}) should be a directory' ) return A__ = 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 ): copyfile(self.vocab_file , __lowerCAmelCase ) return (out_vocab_file,)
274
1
def __lowerCamelCase ( __a :List[Any] ) -> List[str]: """simple docstring""" stooge(__a , 0 , len(__a ) - 1 ) return arr def __lowerCamelCase ( __a :Dict , __a :Optional[int] , __a :List[Any] ) -> Tuple: """simple docstring""" if i >= h: return # If first element is smaller than the last then swap them if arr[i] > arr[h]: A__ , A__ = arr[h], arr[i] # If there are more than 2 elements in the array if h - i + 1 > 2: A__ = (int)((h - i + 1) / 3 ) # Recursively sort first 2/3 elements stooge(__a , __a , (h - t) ) # Recursively sort last 2/3 elements stooge(__a , i + t , (__a) ) # Recursively sort first 2/3 elements stooge(__a , __a , (h - t) ) if __name__ == "__main__": A : int = input('''Enter numbers separated by a comma:\n''').strip() A : Union[str, Any] = [int(item) for item in user_input.split(''',''')] print(stooge_sort(unsorted))
274
import dataclasses import json import warnings from dataclasses import dataclass, field from time import time from typing import List from ..utils import logging A : Dict = logging.get_logger(__name__) def __lowerCamelCase ( __a :int=None , __a :Optional[Any]=None ) -> int: """simple docstring""" return field(default_factory=lambda: default , metadata=__a ) @dataclass class A : '''simple docstring''' __lowerCamelCase : List[str] = list_field( default=[] , metadata={ '''help''': ( '''Model checkpoints to be provided to the AutoModel classes. Leave blank to benchmark the base version''' ''' of all available models''' ) } , ) __lowerCamelCase : List[int] = list_field( default=[8] , metadata={'''help''': '''List of batch sizes for which memory and time performance will be evaluated'''} ) __lowerCamelCase : List[int] = list_field( default=[8, 32, 128, 512] , metadata={'''help''': '''List of sequence lengths for which memory and time performance will be evaluated'''} , ) __lowerCamelCase : bool = field( default=SCREAMING_SNAKE_CASE , metadata={'''help''': '''Whether to benchmark inference of model. Inference can be disabled via --no-inference.'''} , ) __lowerCamelCase : bool = field( default=SCREAMING_SNAKE_CASE , metadata={'''help''': '''Whether to run on available cuda devices. Cuda can be disabled via --no-cuda.'''} , ) __lowerCamelCase : bool = field( default=SCREAMING_SNAKE_CASE , metadata={'''help''': '''Whether to run on available tpu devices. TPU can be disabled via --no-tpu.'''} ) __lowerCamelCase : bool = field(default=SCREAMING_SNAKE_CASE , metadata={'''help''': '''Use FP16 to accelerate inference.'''} ) __lowerCamelCase : bool = field(default=SCREAMING_SNAKE_CASE , metadata={'''help''': '''Benchmark training of model'''} ) __lowerCamelCase : bool = field(default=SCREAMING_SNAKE_CASE , metadata={'''help''': '''Verbose memory tracing'''} ) __lowerCamelCase : bool = field( default=SCREAMING_SNAKE_CASE , metadata={'''help''': '''Whether to perform speed measurements. Speed measurements can be disabled via --no-speed.'''} , ) __lowerCamelCase : bool = field( default=SCREAMING_SNAKE_CASE , metadata={ '''help''': '''Whether to perform memory measurements. Memory measurements can be disabled via --no-memory''' } , ) __lowerCamelCase : bool = field(default=SCREAMING_SNAKE_CASE , metadata={'''help''': '''Trace memory line by line'''} ) __lowerCamelCase : bool = field(default=SCREAMING_SNAKE_CASE , metadata={'''help''': '''Save result to a CSV file'''} ) __lowerCamelCase : bool = field(default=SCREAMING_SNAKE_CASE , metadata={'''help''': '''Save all print statements in a log file'''} ) __lowerCamelCase : bool = field(default=SCREAMING_SNAKE_CASE , metadata={'''help''': '''Whether to print environment information'''} ) __lowerCamelCase : bool = field( default=SCREAMING_SNAKE_CASE , metadata={ '''help''': ( '''Whether to use multiprocessing for memory and speed measurement. It is highly recommended to use''' ''' multiprocessing for accurate CPU and GPU memory measurements. This option should only be disabled''' ''' for debugging / testing and on TPU.''' ) } , ) __lowerCamelCase : str = field( default=F'''inference_time_{round(time() )}.csv''' , metadata={'''help''': '''CSV filename used if saving time results to csv.'''} , ) __lowerCamelCase : str = field( default=F'''inference_memory_{round(time() )}.csv''' , metadata={'''help''': '''CSV filename used if saving memory results to csv.'''} , ) __lowerCamelCase : str = field( default=F'''train_time_{round(time() )}.csv''' , metadata={'''help''': '''CSV filename used if saving time results to csv for training.'''} , ) __lowerCamelCase : str = field( default=F'''train_memory_{round(time() )}.csv''' , metadata={'''help''': '''CSV filename used if saving memory results to csv for training.'''} , ) __lowerCamelCase : str = field( default=F'''env_info_{round(time() )}.csv''' , metadata={'''help''': '''CSV filename used if saving environment information.'''} , ) __lowerCamelCase : str = field( default=F'''log_{round(time() )}.csv''' , metadata={'''help''': '''Log filename used if print statements are saved in log.'''} , ) __lowerCamelCase : int = field(default=3 , metadata={'''help''': '''Times an experiment will be run.'''} ) __lowerCamelCase : bool = field( default=SCREAMING_SNAKE_CASE , metadata={ '''help''': ( '''Instead of loading the model as defined in `config.architectures` if exists, just load the pretrain''' ''' model weights.''' ) } , ) def a_ ( self : Dict ) -> Union[str, Any]: """simple docstring""" warnings.warn( f'The class {self.__class__} is deprecated. Hugging Face Benchmarking utils' """ are deprecated in general and it is advised to use external Benchmarking libraries """ """ to benchmark Transformer models.""" , __lowerCAmelCase , ) def a_ ( self : Union[str, Any] ) -> List[str]: """simple docstring""" return json.dumps(dataclasses.asdict(self ) , indent=2 ) @property def a_ ( self : Tuple ) -> List[str]: """simple docstring""" if len(self.models ) <= 0: raise ValueError( """Please make sure you provide at least one model name / model identifier, *e.g.* `--models""" """ bert-base-cased` or `args.models = ['bert-base-cased'].""" ) return self.models @property def a_ ( self : Union[str, Any] ) -> Optional[Any]: """simple docstring""" if not self.multi_process: return False elif self.is_tpu: logger.info("""Multiprocessing is currently not possible on TPU.""" ) return False else: return True
274
1
import os # All paths are set with the intent you should run this script from the root of the repo with the command # python utils/check_doctest_list.py A : List[Any] = '''.''' if __name__ == "__main__": A : Optional[Any] = os.path.join(REPO_PATH, '''utils/documentation_tests.txt''') A : Dict = [] A : Dict = [] with open(doctest_file_path) as fp: for line in fp: A : Dict = line.strip() A : Optional[Any] = os.path.join(REPO_PATH, line) if not (os.path.isfile(path) or os.path.isdir(path)): non_existent_paths.append(line) all_paths.append(path) if len(non_existent_paths) > 0: A : Dict = '''\n'''.join(non_existent_paths) raise ValueError(F'''`utils/documentation_tests.txt` contains non-existent paths:\n{non_existent_paths}''') if all_paths != sorted(all_paths): raise ValueError('''Files in `utils/documentation_tests.txt` are not in alphabetical order.''')
274
from math import ceil def __lowerCamelCase ( __a :int = 1_0_0_1 ) -> int: """simple docstring""" A__ = 1 for i in range(1 , int(ceil(n / 2.0 ) ) ): A__ = 2 * i + 1 A__ = 2 * i A__ = total + 4 * odd**2 - 6 * even return total if __name__ == "__main__": import sys if len(sys.argv) == 1: print(solution()) else: try: A : List[str] = int(sys.argv[1]) print(solution(n)) except ValueError: print('''Invalid entry - please enter a number''')
274
1