code
stringlengths
81
54k
code_codestyle
int64
0
721
style_context
stringlengths
91
41.9k
style_context_codestyle
int64
0
699
label
int64
0
1
def _lowerCAmelCase( __A : Optional[Any] = 1000 ): UpperCAmelCase = 3 UpperCAmelCase = 0 while a < n: if a % 3 == 0 or a % 5 == 0: result += a elif a % 15 == 0: result -= a a += 1 return result if __name__ == "__main__": print(f"{solution() = }")
708
lowerCAmelCase__ = { "a": "AAAAA", "b": "AAAAB", "c": "AAABA", "d": "AAABB", "e": "AABAA", "f": "AABAB", "g": "AABBA", "h": "AABBB", "i": "ABAAA", "j": "BBBAA", "k": "ABAAB", "l": "ABABA", "m": "ABABB", "n": "ABBAA", "o": "ABBAB", "p": "ABBBA", "q": "ABBBB", "r": "BAAAA", "s": "BAAAB", "t": "BAABA", "u": "BAABB", "v": "BBBAB", "w": "BABAA", "x": "BABAB", "y": "BABBA", "z": "BABBB", " ": " ", } lowerCAmelCase__ = {value: key for key, value in encode_dict.items()} def _lowerCAmelCase( __A ): UpperCAmelCase = "" for letter in word.lower(): if letter.isalpha() or letter == " ": encoded += encode_dict[letter] else: raise Exception("encode() accepts only letters of the alphabet and spaces" ) return encoded def _lowerCAmelCase( __A ): if set(__A ) - {"A", "B", " "} != set(): raise Exception("decode() accepts only 'A', 'B' and spaces" ) UpperCAmelCase = "" for word in coded.split(): while len(__A ) != 0: decoded += decode_dict[word[:5]] UpperCAmelCase = word[5:] decoded += " " return decoded.strip() if __name__ == "__main__": from doctest import testmod testmod()
1
0
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available lowerCAmelCase__ = { "configuration_time_series_transformer": [ "TIME_SERIES_TRANSFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP", "TimeSeriesTransformerConfig", ], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase__ = [ "TIME_SERIES_TRANSFORMER_PRETRAINED_MODEL_ARCHIVE_LIST", "TimeSeriesTransformerForPrediction", "TimeSeriesTransformerModel", "TimeSeriesTransformerPreTrainedModel", ] if TYPE_CHECKING: from .configuration_time_series_transformer import ( TIME_SERIES_TRANSFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP, TimeSeriesTransformerConfig, ) try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_time_series_transformer import ( TIME_SERIES_TRANSFORMER_PRETRAINED_MODEL_ARCHIVE_LIST, TimeSeriesTransformerForPrediction, TimeSeriesTransformerModel, TimeSeriesTransformerPreTrainedModel, ) else: import sys lowerCAmelCase__ = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
709
from __future__ import annotations import json import requests from bsa import BeautifulSoup from fake_useragent import UserAgent lowerCAmelCase__ = {"UserAgent": UserAgent().random} def _lowerCAmelCase( __A ): UpperCAmelCase = script.contents[0] UpperCAmelCase = json.loads(data[data.find("{\"config\"" ) : -1] ) return info["entry_data"]["ProfilePage"][0]["graphql"]["user"] class __magic_name__ : def __init__( self : Optional[Any] , lowerCAmelCase__ : Optional[int] ) -> Any: UpperCAmelCase = f"https://www.instagram.com/{username}/" UpperCAmelCase = self.get_json() def _UpperCamelCase ( self : List[str] ) -> dict: UpperCAmelCase = requests.get(self.url , headers=lowerCAmelCase__ ).text UpperCAmelCase = BeautifulSoup(lowerCAmelCase__ , "html.parser" ).find_all("script" ) try: return extract_user_profile(scripts[4] ) except (json.decoder.JSONDecodeError, KeyError): return extract_user_profile(scripts[3] ) def __repr__( self : Tuple ) -> str: return f"{self.__class__.__name__}('{self.username}')" def __str__( self : Optional[int] ) -> str: return f"{self.fullname} ({self.username}) is {self.biography}" @property def _UpperCamelCase ( self : Any ) -> str: return self.user_data["username"] @property def _UpperCamelCase ( self : List[Any] ) -> str: return self.user_data["full_name"] @property def _UpperCamelCase ( self : List[str] ) -> str: return self.user_data["biography"] @property def _UpperCamelCase ( self : Optional[int] ) -> str: return self.user_data["business_email"] @property def _UpperCamelCase ( self : str ) -> str: return self.user_data["external_url"] @property def _UpperCamelCase ( self : int ) -> int: return self.user_data["edge_followed_by"]["count"] @property def _UpperCamelCase ( self : List[Any] ) -> int: return self.user_data["edge_follow"]["count"] @property def _UpperCamelCase ( self : List[str] ) -> int: return self.user_data["edge_owner_to_timeline_media"]["count"] @property def _UpperCamelCase ( self : Tuple ) -> str: return self.user_data["profile_pic_url_hd"] @property def _UpperCamelCase ( self : Optional[int] ) -> bool: return self.user_data["is_verified"] @property def _UpperCamelCase ( self : Optional[Any] ) -> bool: return self.user_data["is_private"] def _lowerCAmelCase( __A = "github" ): import os if os.environ.get("CI" ): return # test failing on GitHub Actions UpperCAmelCase = InstagramUser(__A ) assert instagram_user.user_data assert isinstance(instagram_user.user_data , __A ) assert instagram_user.username == username if username != "github": return assert instagram_user.fullname == "GitHub" assert instagram_user.biography == "Built for developers." assert instagram_user.number_of_posts > 150 assert instagram_user.number_of_followers > 120000 assert instagram_user.number_of_followings > 15 assert instagram_user.email == "support@github.com" assert instagram_user.website == "https://github.com/readme" assert instagram_user.profile_picture_url.startswith("https://instagram." ) assert instagram_user.is_verified is True assert instagram_user.is_private is False if __name__ == "__main__": import doctest doctest.testmod() lowerCAmelCase__ = InstagramUser("github") print(instagram_user) print(f"{instagram_user.number_of_posts = }") print(f"{instagram_user.number_of_followers = }") print(f"{instagram_user.number_of_followings = }") print(f"{instagram_user.email = }") print(f"{instagram_user.website = }") print(f"{instagram_user.profile_picture_url = }") print(f"{instagram_user.is_verified = }") print(f"{instagram_user.is_private = }")
1
0
import logging import re import pytorch_quantization import pytorch_quantization.nn as quant_nn import torch from pytorch_quantization import calib from pytorch_quantization.tensor_quant import QuantDescriptor lowerCAmelCase__ = logging.getLogger(__name__) lowerCAmelCase__ = 50 # max width of layer names lowerCAmelCase__ = 70 # max width of quantizer names def _lowerCAmelCase( __A ): UpperCAmelCase = parser.add_argument_group("quant_trainer arguments" ) group.add_argument("--wprec" , type=__A , default=8 , help="weight precision" ) group.add_argument("--aprec" , type=__A , default=8 , help="activation precision" ) group.add_argument("--quant-per-tensor" , action="store_true" , help="per tensor weight scaling" ) group.add_argument("--quant-disable" , action="store_true" , help="disable all quantizers" ) group.add_argument("--quant-disable-embeddings" , action="store_true" , help="disable all embeddings quantizers" ) group.add_argument("--quant-disable-keyword" , type=__A , nargs="+" , help="disable quantizers by keyword" ) group.add_argument("--quant-disable-layer-module" , type=__A , help="disable quantizers by keyword under layer." ) group.add_argument("--quant-enable-layer-module" , type=__A , help="enable quantizers by keyword under layer" ) group.add_argument("--calibrator" , default="max" , help="which quantization range calibrator to use" ) group.add_argument("--percentile" , default=__A , type=__A , help="percentile for PercentileCalibrator" ) group.add_argument("--fuse-qkv" , action="store_true" , help="use the same scale factor for qkv" ) group.add_argument("--clip-gelu" , metavar="N" , type=__A , help="clip gelu output maximum value to N" ) group.add_argument( "--recalibrate-weights" , action="store_true" , help=( "recalibrate weight amaxes by taking the max of the weights." " amaxes will be computed with the current quantization granularity (axis)." ) , ) def _lowerCAmelCase( __A ): if args.calibrator == "max": UpperCAmelCase = "max" elif args.calibrator == "percentile": if args.percentile is None: raise ValueError("Specify --percentile when using percentile calibrator" ) UpperCAmelCase = "histogram" elif args.calibrator == "mse": UpperCAmelCase = "histogram" else: raise ValueError(F"Invalid calibrator {args.calibrator}" ) UpperCAmelCase = QuantDescriptor(num_bits=args.aprec , calib_method=__A ) UpperCAmelCase = QuantDescriptor(num_bits=args.wprec , axis=(None if args.quant_per_tensor else (0,)) ) quant_nn.QuantLinear.set_default_quant_desc_input(__A ) quant_nn.QuantLinear.set_default_quant_desc_weight(__A ) def _lowerCAmelCase( __A , __A , __A=False , __A=False ): logger.info("Configuring Model for Quantization" ) logger.info(F"using quantization package {pytorch_quantization.__file__}" ) if not calib: if args.quant_disable_embeddings: set_quantizer_by_name(__A , ["embeddings"] , which="weight" , _disabled=__A ) if args.quant_disable: set_quantizer_by_name(__A , [""] , _disabled=__A ) if args.quant_disable_keyword: set_quantizer_by_name(__A , args.quant_disable_keyword , _disabled=__A ) if args.quant_disable_layer_module: set_quantizer_by_name(__A , [r"layer.\d+." + args.quant_disable_layer_module] , _disabled=__A ) if args.quant_enable_layer_module: set_quantizer_by_name(__A , [r"layer.\d+." + args.quant_enable_layer_module] , _disabled=__A ) if args.recalibrate_weights: recalibrate_weights(__A ) if args.fuse_qkv: fuse_qkv(__A , __A ) if args.clip_gelu: clip_gelu(__A , args.clip_gelu ) # if args.local_rank in [-1, 0] and not calib: print_quant_summary(__A ) def _lowerCAmelCase( __A ): logger.info("Enabling Calibration" ) for name, module in model.named_modules(): if name.endswith("_quantizer" ): if module._calibrator is not None: module.disable_quant() module.enable_calib() else: module.disable() logger.info(F"{name:80}: {module}" ) def _lowerCAmelCase( __A , __A ): logger.info("Loading calibrated amax" ) for name, module in model.named_modules(): if name.endswith("_quantizer" ): if module._calibrator is not None: if isinstance(module._calibrator , calib.MaxCalibrator ): module.load_calib_amax() else: module.load_calib_amax("percentile" , percentile=args.percentile ) module.enable_quant() module.disable_calib() else: module.enable() model.cuda() print_quant_summary(__A ) def _lowerCAmelCase( __A , __A ): def fusea(__A , __A , __A ): for mod in [qq, qk, qv]: if not hasattr(__A , "_amax" ): print(" WARNING: NO AMAX BUFFER" ) return UpperCAmelCase = qq._amax.detach().item() UpperCAmelCase = qk._amax.detach().item() UpperCAmelCase = qv._amax.detach().item() UpperCAmelCase = max(__A , __A , __A ) qq._amax.fill_(__A ) qk._amax.fill_(__A ) qv._amax.fill_(__A ) logger.info(F" q={q:5.2f} k={k:5.2f} v={v:5.2f} -> {amax:5.2f}" ) for name, mod in model.named_modules(): if name.endswith(".attention.self" ): logger.info(F"FUSE_QKV: {name:{name_width}}" ) fusea(mod.matmul_q_input_quantizer , mod.matmul_k_input_quantizer , mod.matmul_v_input_quantizer ) if args.quant_per_tensor: fusea(mod.query._weight_quantizer , mod.key._weight_quantizer , mod.value._weight_quantizer ) def _lowerCAmelCase( __A , __A ): for name, mod in model.named_modules(): if name.endswith(".output.dense" ) and not name.endswith("attention.output.dense" ): UpperCAmelCase = mod._input_quantizer._amax.data.detach().item() mod._input_quantizer._amax.data.detach().clamp_(max=__A ) UpperCAmelCase = mod._input_quantizer._amax.data.detach().item() logger.info(F"CLIP_GELU: {name:{name_width}} amax: {amax_init:5.2f} -> {amax:5.2f}" ) def _lowerCAmelCase( __A ): for name, mod in model.named_modules(): if hasattr(__A , "_weight_quantizer" ) and mod._weight_quantizer.axis is not None: UpperCAmelCase = mod.weight.shape[0] UpperCAmelCase = mod._weight_quantizer._amax.detach() UpperCAmelCase = torch.ones(__A , dtype=amax.dtype , device=amax.device ) * amax print(F"expanding {name} {amax} -> {mod._weight_quantizer._amax}" ) def _lowerCAmelCase( __A ): for name, mod in model.named_modules(): if hasattr(__A , "_weight_quantizer" ): if not hasattr(mod.weight_quantizer , "_amax" ): print("RECALIB: {name:{name_width}} WARNING: NO AMAX BUFFER" ) continue # determine which axes to reduce across # e.g. a 4D tensor quantized per axis 0 should reduce over (1,2,3) UpperCAmelCase = set() if mod._weight_quantizer.axis is None else set(mod._weight_quantizer.axis ) UpperCAmelCase = set(range(len(mod.weight.size() ) ) ) - axis_set UpperCAmelCase = pytorch_quantization.utils.reduce_amax(mod.weight , axis=__A , keepdims=__A ).detach() logger.info(F"RECALIB: {name:{name_width}} {mod._weight_quantizer._amax.flatten()} -> {amax.flatten()}" ) UpperCAmelCase = amax def _lowerCAmelCase( __A , __A=25 , __A=180 , __A=None ): if ignore is None: UpperCAmelCase = [] elif not isinstance(__A , __A ): UpperCAmelCase = [ignore] UpperCAmelCase = 0 for name, mod in model.named_modules(): if not hasattr(__A , "weight" ): continue UpperCAmelCase = max(__A , len(__A ) ) for name, mod in model.named_modules(): UpperCAmelCase = getattr(__A , "_input_quantizer" , __A ) UpperCAmelCase = getattr(__A , "_weight_quantizer" , __A ) if not hasattr(__A , "weight" ): continue if type(__A ) in ignore: continue if [True for s in ignore if type(__A ) is str and s in name]: continue UpperCAmelCase = F"Act:{input_q.extra_repr()}" UpperCAmelCase = F"Wgt:{weight_q.extra_repr()}" UpperCAmelCase = F"{name:{name_width}} {act_str} {wgt_str}" if len(__A ) <= line_width: logger.info(__A ) else: logger.info(F"{name:{name_width}} {act_str}" ) logger.info(F"{' ':{name_width}} {wgt_str}" ) def _lowerCAmelCase( __A ): UpperCAmelCase = 0 for name, mod in model.named_modules(): if isinstance(__A , pytorch_quantization.nn.TensorQuantizer ): print(F"{name:80} {mod}" ) count += 1 print(F"{count} TensorQuantizers found in model" ) def _lowerCAmelCase( __A , __A , __A , __A , __A ): UpperCAmelCase = getattr(__A , __A , __A ) if quantizer_mod is not None: assert hasattr(__A , __A ) setattr(__A , __A , __A ) else: logger.warning(F"{name} has no {quantizer}" ) def _lowerCAmelCase( __A , __A , __A="both" , **__A ): UpperCAmelCase = F"Warning: changing {which} quantizers of {name:{qname_width}}" for k, v in kwargs.items(): s += F" {k}={v}" if which in ["input", "both"]: set_quantizer(__A , __A , "_input_quantizer" , __A , __A ) if which in ["weight", "both"]: set_quantizer(__A , __A , "_weight_quantizer" , __A , __A ) logger.info(__A ) def _lowerCAmelCase( __A , __A , **__A ): for name, mod in model.named_modules(): if hasattr(__A , "_input_quantizer" ) or hasattr(__A , "_weight_quantizer" ): for n in names: if re.search(__A , __A ): set_quantizers(__A , __A , **__A ) elif name.endswith("_quantizer" ): for n in names: if re.search(__A , __A ): UpperCAmelCase = F"Warning: changing {name:{name_width}}" for k, v in kwargs.items(): s += F" {k}={v}" setattr(__A , __A , __A ) logger.info(__A )
710
import unittest import numpy as np def _lowerCAmelCase( __A , __A , __A , __A = None , ): UpperCAmelCase = np.shape(__A ) UpperCAmelCase = np.shape(__A ) UpperCAmelCase = np.shape(__A ) if shape_a[0] != shape_b[0]: UpperCAmelCase = ( "Expected the same number of rows for A and B. " F"Instead found A of size {shape_a} and B of size {shape_b}" ) raise ValueError(__A ) if shape_b[1] != shape_c[1]: UpperCAmelCase = ( "Expected the same number of columns for B and C. " F"Instead found B of size {shape_b} and C of size {shape_c}" ) raise ValueError(__A ) UpperCAmelCase = pseudo_inv if a_inv is None: try: UpperCAmelCase = np.linalg.inv(__A ) except np.linalg.LinAlgError: raise ValueError( "Input matrix A is not invertible. Cannot compute Schur complement." ) return mat_c - mat_b.T @ a_inv @ mat_b class __magic_name__ ( unittest.TestCase ): def _UpperCamelCase ( self : List[str] ) -> None: UpperCAmelCase = np.array([[1, 2, 1], [2, 1, 2], [3, 2, 4]] ) UpperCAmelCase = np.array([[0, 3], [3, 0], [2, 3]] ) UpperCAmelCase = np.array([[2, 1], [6, 3]] ) UpperCAmelCase = schur_complement(lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ ) UpperCAmelCase = np.block([[a, b], [b.T, c]] ) UpperCAmelCase = np.linalg.det(lowerCAmelCase__ ) UpperCAmelCase = np.linalg.det(lowerCAmelCase__ ) UpperCAmelCase = np.linalg.det(lowerCAmelCase__ ) self.assertAlmostEqual(lowerCAmelCase__ , det_a * det_s ) def _UpperCamelCase ( self : str ) -> None: UpperCAmelCase = np.array([[1, 2, 1], [2, 1, 2], [3, 2, 4]] ) UpperCAmelCase = np.array([[0, 3], [3, 0], [2, 3]] ) UpperCAmelCase = np.array([[2, 1], [6, 3]] ) with self.assertRaises(lowerCAmelCase__ ): schur_complement(lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ ) def _UpperCamelCase ( self : Dict ) -> None: UpperCAmelCase = np.array([[1, 2, 1], [2, 1, 2], [3, 2, 4]] ) UpperCAmelCase = np.array([[0, 3], [3, 0], [2, 3]] ) UpperCAmelCase = np.array([[2, 1, 3], [6, 3, 5]] ) with self.assertRaises(lowerCAmelCase__ ): schur_complement(lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ ) if __name__ == "__main__": import doctest doctest.testmod() unittest.main()
1
0
import argparse import tensorflow as tf import torch from transformers import BertConfig, BertForMaskedLM from transformers.models.bert.modeling_bert import ( BertIntermediate, BertLayer, BertOutput, BertPooler, BertSelfAttention, BertSelfOutput, ) from transformers.utils import logging logging.set_verbosity_info() def _lowerCAmelCase( __A , __A , __A ): def get_masked_lm_array(__A ): UpperCAmelCase = F"masked_lm/{name}/.ATTRIBUTES/VARIABLE_VALUE" UpperCAmelCase = tf.train.load_variable(__A , __A ) if "kernel" in name: UpperCAmelCase = array.transpose() return torch.from_numpy(__A ) def get_encoder_array(__A ): UpperCAmelCase = F"encoder/{name}/.ATTRIBUTES/VARIABLE_VALUE" UpperCAmelCase = tf.train.load_variable(__A , __A ) if "kernel" in name: UpperCAmelCase = array.transpose() return torch.from_numpy(__A ) def get_encoder_layer_array(__A , __A ): UpperCAmelCase = F"encoder/_transformer_layers/{layer_index}/{name}/.ATTRIBUTES/VARIABLE_VALUE" UpperCAmelCase = tf.train.load_variable(__A , __A ) if "kernel" in name: UpperCAmelCase = array.transpose() return torch.from_numpy(__A ) def get_encoder_attention_layer_array(__A , __A , __A ): UpperCAmelCase = F"encoder/_transformer_layers/{layer_index}/_attention_layer/{name}/.ATTRIBUTES/VARIABLE_VALUE" UpperCAmelCase = tf.train.load_variable(__A , __A ) UpperCAmelCase = array.reshape(__A ) if "kernel" in name: UpperCAmelCase = array.transpose() return torch.from_numpy(__A ) print(F"Loading model based on config from {config_path}..." ) UpperCAmelCase = BertConfig.from_json_file(__A ) UpperCAmelCase = BertForMaskedLM(__A ) # Layers for layer_index in range(0 , config.num_hidden_layers ): UpperCAmelCase = model.bert.encoder.layer[layer_index] # Self-attention UpperCAmelCase = layer.attention.self UpperCAmelCase = get_encoder_attention_layer_array( __A , "_query_dense/kernel" , self_attn.query.weight.data.shape ) UpperCAmelCase = get_encoder_attention_layer_array( __A , "_query_dense/bias" , self_attn.query.bias.data.shape ) UpperCAmelCase = get_encoder_attention_layer_array( __A , "_key_dense/kernel" , self_attn.key.weight.data.shape ) UpperCAmelCase = get_encoder_attention_layer_array( __A , "_key_dense/bias" , self_attn.key.bias.data.shape ) UpperCAmelCase = get_encoder_attention_layer_array( __A , "_value_dense/kernel" , self_attn.value.weight.data.shape ) UpperCAmelCase = get_encoder_attention_layer_array( __A , "_value_dense/bias" , self_attn.value.bias.data.shape ) # Self-attention Output UpperCAmelCase = layer.attention.output UpperCAmelCase = get_encoder_attention_layer_array( __A , "_output_dense/kernel" , self_output.dense.weight.data.shape ) UpperCAmelCase = get_encoder_attention_layer_array( __A , "_output_dense/bias" , self_output.dense.bias.data.shape ) UpperCAmelCase = get_encoder_layer_array(__A , "_attention_layer_norm/gamma" ) UpperCAmelCase = get_encoder_layer_array(__A , "_attention_layer_norm/beta" ) # Intermediate UpperCAmelCase = layer.intermediate UpperCAmelCase = get_encoder_layer_array(__A , "_intermediate_dense/kernel" ) UpperCAmelCase = get_encoder_layer_array(__A , "_intermediate_dense/bias" ) # Output UpperCAmelCase = layer.output UpperCAmelCase = get_encoder_layer_array(__A , "_output_dense/kernel" ) UpperCAmelCase = get_encoder_layer_array(__A , "_output_dense/bias" ) UpperCAmelCase = get_encoder_layer_array(__A , "_output_layer_norm/gamma" ) UpperCAmelCase = get_encoder_layer_array(__A , "_output_layer_norm/beta" ) # Embeddings UpperCAmelCase = get_encoder_array("_position_embedding_layer/embeddings" ) UpperCAmelCase = get_encoder_array("_type_embedding_layer/embeddings" ) UpperCAmelCase = get_encoder_array("_embedding_norm_layer/gamma" ) UpperCAmelCase = get_encoder_array("_embedding_norm_layer/beta" ) # LM Head UpperCAmelCase = model.cls.predictions.transform UpperCAmelCase = get_masked_lm_array("dense/kernel" ) UpperCAmelCase = get_masked_lm_array("dense/bias" ) UpperCAmelCase = get_masked_lm_array("layer_norm/gamma" ) UpperCAmelCase = get_masked_lm_array("layer_norm/beta" ) UpperCAmelCase = get_masked_lm_array("embedding_table" ) # Pooling UpperCAmelCase = BertPooler(config=__A ) UpperCAmelCase = get_encoder_array("_pooler_layer/kernel" ) UpperCAmelCase = get_encoder_array("_pooler_layer/bias" ) # Export final model model.save_pretrained(__A ) # Integration test - should load without any errors ;) UpperCAmelCase = BertForMaskedLM.from_pretrained(__A ) print(new_model.eval() ) print("Model conversion was done sucessfully!" ) if __name__ == "__main__": lowerCAmelCase__ = argparse.ArgumentParser() parser.add_argument( "--tf_checkpoint_path", type=str, required=True, help="Path to the TensorFlow Token Dropping checkpoint path." ) parser.add_argument( "--bert_config_file", type=str, required=True, help="The config json file corresponding to the BERT model. This specifies the model architecture.", ) parser.add_argument( "--pytorch_dump_path", type=str, required=True, help="Path to the output PyTorch model.", ) lowerCAmelCase__ = parser.parse_args() convert_checkpoint_to_pytorch(args.tf_checkpoint_path, args.bert_config_file, args.pytorch_dump_path)
711
import argparse import os import re import numpy as np import PIL import torch from timm import create_model from torch.optim.lr_scheduler import OneCycleLR from torch.utils.data import DataLoader, Dataset from torchvision.transforms import Compose, RandomResizedCrop, Resize, ToTensor from accelerate import Accelerator def _lowerCAmelCase( __A ): UpperCAmelCase = fname.split(os.path.sep )[-1] return re.search(r"^(.*)_\d+\.jpg$" , __A ).groups()[0] class __magic_name__ ( _snake_case ): def __init__( self : Any , lowerCAmelCase__ : int , lowerCAmelCase__ : Union[str, Any]=None , lowerCAmelCase__ : int=None ) -> Optional[Any]: UpperCAmelCase = file_names UpperCAmelCase = image_transform UpperCAmelCase = label_to_id def __len__( self : Tuple ) -> List[str]: return len(self.file_names ) def __getitem__( self : Optional[int] , lowerCAmelCase__ : Tuple ) -> Dict: UpperCAmelCase = self.file_names[idx] UpperCAmelCase = PIL.Image.open(lowerCAmelCase__ ) UpperCAmelCase = raw_image.convert("RGB" ) if self.image_transform is not None: UpperCAmelCase = self.image_transform(lowerCAmelCase__ ) UpperCAmelCase = extract_label(lowerCAmelCase__ ) if self.label_to_id is not None: UpperCAmelCase = self.label_to_id[label] return {"image": image, "label": label} def _lowerCAmelCase( __A , __A ): # Initialize accelerator if args.with_tracking: UpperCAmelCase = Accelerator( cpu=args.cpu , mixed_precision=args.mixed_precision , log_with="all" , project_dir=args.project_dir ) else: UpperCAmelCase = Accelerator(cpu=args.cpu , mixed_precision=args.mixed_precision ) # Sample hyper-parameters for learning rate, batch size, seed and a few other HPs UpperCAmelCase = config["lr"] UpperCAmelCase = int(config["num_epochs"] ) UpperCAmelCase = int(config["seed"] ) UpperCAmelCase = int(config["batch_size"] ) UpperCAmelCase = config["image_size"] if not isinstance(__A , (list, tuple) ): UpperCAmelCase = (image_size, image_size) # Parse out whether we are saving every epoch or after a certain number of batches if hasattr(args.checkpointing_steps , "isdigit" ): if args.checkpointing_steps == "epoch": UpperCAmelCase = args.checkpointing_steps elif args.checkpointing_steps.isdigit(): UpperCAmelCase = int(args.checkpointing_steps ) else: raise ValueError( F"Argument `checkpointing_steps` must be either a number or `epoch`. `{args.checkpointing_steps}` passed." ) else: UpperCAmelCase = None # We need to initialize the trackers we use, and also store our configuration if args.with_tracking: UpperCAmelCase = os.path.split(__A )[-1].split("." )[0] accelerator.init_trackers(__A , __A ) # Grab all the image filenames UpperCAmelCase = [os.path.join(args.data_dir , __A ) for fname in os.listdir(args.data_dir ) if fname.endswith(".jpg" )] # Build the label correspondences UpperCAmelCase = [extract_label(__A ) for fname in file_names] UpperCAmelCase = list(set(__A ) ) id_to_label.sort() UpperCAmelCase = {lbl: i for i, lbl in enumerate(__A )} # Set the seed before splitting the data. np.random.seed(__A ) torch.manual_seed(__A ) torch.cuda.manual_seed_all(__A ) # Split our filenames between train and validation UpperCAmelCase = np.random.permutation(len(__A ) ) UpperCAmelCase = int(0.8 * len(__A ) ) UpperCAmelCase = random_perm[:cut] UpperCAmelCase = random_perm[cut:] # For training we use a simple RandomResizedCrop UpperCAmelCase = Compose([RandomResizedCrop(__A , scale=(0.5, 1.0) ), ToTensor()] ) UpperCAmelCase = PetsDataset( [file_names[i] for i in train_split] , image_transform=__A , label_to_id=__A ) # For evaluation, we use a deterministic Resize UpperCAmelCase = Compose([Resize(__A ), ToTensor()] ) UpperCAmelCase = PetsDataset([file_names[i] for i in eval_split] , image_transform=__A , label_to_id=__A ) # Instantiate dataloaders. UpperCAmelCase = DataLoader(__A , shuffle=__A , batch_size=__A , num_workers=4 ) UpperCAmelCase = DataLoader(__A , shuffle=__A , batch_size=__A , num_workers=4 ) # Instantiate the model (we build the model here so that the seed also control new weights initialization) UpperCAmelCase = create_model("resnet50d" , pretrained=__A , num_classes=len(__A ) ) # We could avoid this line since the accelerator is set with `device_placement=True` (default value). # Note that if you are placing tensors on devices manually, this line absolutely needs to be before the optimizer # creation otherwise training will not work on TPU (`accelerate` will kindly throw an error to make us aware of that). UpperCAmelCase = model.to(accelerator.device ) # Freezing the base model for param in model.parameters(): UpperCAmelCase = False for param in model.get_classifier().parameters(): UpperCAmelCase = True # We normalize the batches of images to be a bit faster. UpperCAmelCase = torch.tensor(model.default_cfg["mean"] )[None, :, None, None].to(accelerator.device ) UpperCAmelCase = torch.tensor(model.default_cfg["std"] )[None, :, None, None].to(accelerator.device ) # Instantiate optimizer UpperCAmelCase = torch.optim.Adam(params=model.parameters() , lr=lr / 25 ) # Instantiate learning rate scheduler UpperCAmelCase = OneCycleLR(optimizer=__A , max_lr=__A , epochs=__A , steps_per_epoch=len(__A ) ) # Prepare everything # There is no specific order to remember, we just need to unpack the objects in the same order we gave them to the # prepare method. UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase = accelerator.prepare( __A , __A , __A , __A , __A ) # We need to keep track of how many total steps we have iterated over UpperCAmelCase = 0 # We also need to keep track of the starting epoch so files are named properly UpperCAmelCase = 0 # Potentially load in the weights and states from a previous save if args.resume_from_checkpoint: if args.resume_from_checkpoint is not None or args.resume_from_checkpoint != "": accelerator.print(F"Resumed from checkpoint: {args.resume_from_checkpoint}" ) accelerator.load_state(args.resume_from_checkpoint ) UpperCAmelCase = os.path.basename(args.resume_from_checkpoint ) else: # Get the most recent checkpoint UpperCAmelCase = [f.name for f in os.scandir(os.getcwd() ) if f.is_dir()] dirs.sort(key=os.path.getctime ) UpperCAmelCase = dirs[-1] # Sorts folders by date modified, most recent checkpoint is the last # Extract `epoch_{i}` or `step_{i}` UpperCAmelCase = os.path.splitext(__A )[0] if "epoch" in training_difference: UpperCAmelCase = int(training_difference.replace("epoch_" , "" ) ) + 1 UpperCAmelCase = None else: UpperCAmelCase = int(training_difference.replace("step_" , "" ) ) UpperCAmelCase = resume_step // len(__A ) resume_step -= starting_epoch * len(__A ) # Now we train the model for epoch in range(__A , __A ): model.train() if args.with_tracking: UpperCAmelCase = 0 if args.resume_from_checkpoint and epoch == starting_epoch and resume_step is not None: # We need to skip steps until we reach the resumed step UpperCAmelCase = accelerator.skip_first_batches(__A , __A ) overall_step += resume_step else: # After the first iteration though, we need to go back to the original dataloader UpperCAmelCase = train_dataloader for batch in active_dataloader: # We could avoid this line since we set the accelerator with `device_placement=True`. UpperCAmelCase = {k: v.to(accelerator.device ) for k, v in batch.items()} UpperCAmelCase = (batch["image"] - mean) / std UpperCAmelCase = model(__A ) UpperCAmelCase = torch.nn.functional.cross_entropy(__A , batch["label"] ) # We keep track of the loss at each epoch if args.with_tracking: total_loss += loss.detach().float() accelerator.backward(__A ) optimizer.step() lr_scheduler.step() optimizer.zero_grad() overall_step += 1 if isinstance(__A , __A ): UpperCAmelCase = F"step_{overall_step}" if overall_step % checkpointing_steps == 0: if args.output_dir is not None: UpperCAmelCase = os.path.join(args.output_dir , __A ) accelerator.save_state(__A ) model.eval() UpperCAmelCase = 0 UpperCAmelCase = 0 for step, batch in enumerate(__A ): # We could avoid this line since we set the accelerator with `device_placement=True`. UpperCAmelCase = {k: v.to(accelerator.device ) for k, v in batch.items()} UpperCAmelCase = (batch["image"] - mean) / std with torch.no_grad(): UpperCAmelCase = model(__A ) UpperCAmelCase = outputs.argmax(dim=-1 ) UpperCAmelCase , UpperCAmelCase = accelerator.gather_for_metrics((predictions, batch["label"]) ) UpperCAmelCase = predictions == references num_elems += accurate_preds.shape[0] accurate += accurate_preds.long().sum() UpperCAmelCase = accurate.item() / num_elems # Use accelerator.print to print only on the main process. accelerator.print(F"epoch {epoch}: {100 * eval_metric:.2f}" ) if args.with_tracking: accelerator.log( { "accuracy": 100 * eval_metric, "train_loss": total_loss.item() / len(__A ), "epoch": epoch, } , step=__A , ) if checkpointing_steps == "epoch": UpperCAmelCase = F"epoch_{epoch}" if args.output_dir is not None: UpperCAmelCase = os.path.join(args.output_dir , __A ) accelerator.save_state(__A ) if args.with_tracking: accelerator.end_training() def _lowerCAmelCase( ): UpperCAmelCase = argparse.ArgumentParser(description="Simple example of training script." ) parser.add_argument("--data_dir" , required=__A , help="The data folder on disk." ) parser.add_argument("--fp16" , action="store_true" , help="If passed, will use FP16 training." ) parser.add_argument( "--mixed_precision" , type=__A , default=__A , choices=["no", "fp16", "bf16", "fp8"] , help="Whether to use mixed precision. Choose" "between fp16 and bf16 (bfloat16). Bf16 requires PyTorch >= 1.10." "and an Nvidia Ampere GPU." , ) parser.add_argument("--cpu" , action="store_true" , help="If passed, will train on the CPU." ) parser.add_argument( "--checkpointing_steps" , type=__A , default=__A , help="Whether the various states should be saved at the end of every n steps, or 'epoch' for each epoch." , ) parser.add_argument( "--output_dir" , type=__A , default="." , help="Optional save directory where all checkpoint folders will be stored. Default is the current working directory." , ) parser.add_argument( "--resume_from_checkpoint" , type=__A , default=__A , help="If the training should continue from a checkpoint folder." , ) parser.add_argument( "--with_tracking" , action="store_true" , help="Whether to load in all available experiment trackers from the environment and use them for logging." , ) parser.add_argument( "--project_dir" , type=__A , default="logs" , help="Location on where to store experiment tracking logs` and relevent project information" , ) UpperCAmelCase = parser.parse_args() UpperCAmelCase = {"lr": 3E-2, "num_epochs": 3, "seed": 42, "batch_size": 64, "image_size": 224} training_function(__A , __A ) if __name__ == "__main__": main()
1
0
import inspect import logging import os import random import shutil import tempfile import unittest import pytest import torch from torch import nn from torch.utils.data import DataLoader, TensorDataset from accelerate import Accelerator from accelerate.test_utils import execute_subprocess_async, require_cuda from accelerate.utils import ProjectConfiguration, set_seed lowerCAmelCase__ = logging.getLogger(__name__) def _lowerCAmelCase( __A=2 , __A=3 , __A=16 , __A = 10 , __A = 2 ): def get_dataset(__A ): UpperCAmelCase = torch.randn(batch_size * n_batches , 1 ) return TensorDataset(__A , a * x + b + 0.1 * torch.randn(batch_size * n_batches , 1 ) ) UpperCAmelCase = get_dataset(__A ) UpperCAmelCase = get_dataset(__A ) UpperCAmelCase = DataLoader(__A , shuffle=__A , batch_size=__A , num_workers=4 ) UpperCAmelCase = DataLoader(__A , shuffle=__A , batch_size=__A , num_workers=4 ) return (train_dataloader, valid_dataloader) def _lowerCAmelCase( __A , __A , __A , __A , __A , __A=None ): UpperCAmelCase = [] for epoch in range(__A ): # Train quickly model.train() for batch in dataloader: UpperCAmelCase , UpperCAmelCase = batch UpperCAmelCase = model(__A ) UpperCAmelCase = torch.nn.functional.mse_loss(__A , __A ) accelerator.backward(__A ) optimizer.step() optimizer.zero_grad() rands.append(random.random() ) # Introduce some randomness if scheduler is not None: scheduler.step() return rands class __magic_name__ ( nn.Module ): def __init__( self : Union[str, Any] ) -> List[Any]: super().__init__() UpperCAmelCase = nn.Parameter(torch.randn(1 ) ) UpperCAmelCase = nn.Parameter(torch.randn(1 ) ) def _UpperCamelCase ( self : int , lowerCAmelCase__ : Optional[int] ) -> Union[str, Any]: return x * self.a + self.b class __magic_name__ ( unittest.TestCase ): def _UpperCamelCase ( self : Any ) -> Optional[int]: with tempfile.TemporaryDirectory() as tmpdir: set_seed(4_2 ) UpperCAmelCase = DummyModel() UpperCAmelCase = torch.optim.Adam(params=model.parameters() , lr=1e-3 ) UpperCAmelCase , UpperCAmelCase = dummy_dataloaders() UpperCAmelCase = ProjectConfiguration(total_limit=1 , project_dir=lowerCAmelCase__ , automatic_checkpoint_naming=lowerCAmelCase__ ) # Train baseline UpperCAmelCase = Accelerator(project_config=lowerCAmelCase__ ) UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase = accelerator.prepare( lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ ) # Save initial accelerator.save_state() # Save second state accelerator.save_state() self.assertEqual(len(os.listdir(accelerator.project_dir ) ) , 1 ) def _UpperCamelCase ( self : Optional[Any] ) -> Tuple: with tempfile.TemporaryDirectory() as tmpdir: set_seed(4_2 ) UpperCAmelCase = DummyModel() UpperCAmelCase = torch.optim.Adam(params=model.parameters() , lr=1e-3 ) UpperCAmelCase , UpperCAmelCase = dummy_dataloaders() # Train baseline UpperCAmelCase = Accelerator() UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase = accelerator.prepare( lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ ) # Save initial UpperCAmelCase = os.path.join(lowerCAmelCase__ , "initial" ) accelerator.save_state(lowerCAmelCase__ ) ((UpperCAmelCase) , (UpperCAmelCase)) = model.a.item(), model.b.item() UpperCAmelCase = optimizer.state_dict() UpperCAmelCase = train(3 , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ ) ((UpperCAmelCase) , (UpperCAmelCase)) = model.a.item(), model.b.item() UpperCAmelCase = optimizer.state_dict() # Train partially set_seed(4_2 ) UpperCAmelCase = DummyModel() UpperCAmelCase = torch.optim.Adam(params=model.parameters() , lr=1e-3 ) UpperCAmelCase , UpperCAmelCase = dummy_dataloaders() UpperCAmelCase = Accelerator() UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase = accelerator.prepare( lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ ) accelerator.load_state(lowerCAmelCase__ ) ((UpperCAmelCase) , (UpperCAmelCase)) = model.a.item(), model.b.item() UpperCAmelCase = optimizer.state_dict() self.assertEqual(lowerCAmelCase__ , lowerCAmelCase__ ) self.assertEqual(lowerCAmelCase__ , lowerCAmelCase__ ) self.assertEqual(lowerCAmelCase__ , lowerCAmelCase__ ) UpperCAmelCase = train(2 , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ ) # Save everything UpperCAmelCase = os.path.join(lowerCAmelCase__ , "checkpoint" ) accelerator.save_state(lowerCAmelCase__ ) # Load everything back in and make sure all states work accelerator.load_state(lowerCAmelCase__ ) test_rands += train(1 , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ ) ((UpperCAmelCase) , (UpperCAmelCase)) = model.a.item(), model.b.item() UpperCAmelCase = optimizer.state_dict() self.assertEqual(lowerCAmelCase__ , lowerCAmelCase__ ) self.assertEqual(lowerCAmelCase__ , lowerCAmelCase__ ) self.assertEqual(lowerCAmelCase__ , lowerCAmelCase__ ) self.assertEqual(lowerCAmelCase__ , lowerCAmelCase__ ) def _UpperCamelCase ( self : List[Any] ) -> List[Any]: with tempfile.TemporaryDirectory() as tmpdir: set_seed(4_2 ) UpperCAmelCase = DummyModel() UpperCAmelCase = torch.optim.Adam(params=model.parameters() , lr=1e-3 ) UpperCAmelCase , UpperCAmelCase = dummy_dataloaders() UpperCAmelCase = ProjectConfiguration(automatic_checkpoint_naming=lowerCAmelCase__ ) # Train baseline UpperCAmelCase = Accelerator(project_dir=lowerCAmelCase__ , project_config=lowerCAmelCase__ ) UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase = accelerator.prepare( lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ ) # Save initial accelerator.save_state() ((UpperCAmelCase) , (UpperCAmelCase)) = model.a.item(), model.b.item() UpperCAmelCase = optimizer.state_dict() UpperCAmelCase = train(3 , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ ) ((UpperCAmelCase) , (UpperCAmelCase)) = model.a.item(), model.b.item() UpperCAmelCase = optimizer.state_dict() # Train partially set_seed(4_2 ) UpperCAmelCase = DummyModel() UpperCAmelCase = torch.optim.Adam(params=model.parameters() , lr=1e-3 ) UpperCAmelCase , UpperCAmelCase = dummy_dataloaders() UpperCAmelCase = ProjectConfiguration(iteration=1 , automatic_checkpoint_naming=lowerCAmelCase__ ) UpperCAmelCase = Accelerator(project_dir=lowerCAmelCase__ , project_config=lowerCAmelCase__ ) UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase = accelerator.prepare( lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ ) accelerator.load_state(os.path.join(lowerCAmelCase__ , "checkpoints" , "checkpoint_0" ) ) ((UpperCAmelCase) , (UpperCAmelCase)) = model.a.item(), model.b.item() UpperCAmelCase = optimizer.state_dict() self.assertEqual(lowerCAmelCase__ , lowerCAmelCase__ ) self.assertEqual(lowerCAmelCase__ , lowerCAmelCase__ ) self.assertEqual(lowerCAmelCase__ , lowerCAmelCase__ ) UpperCAmelCase = train(2 , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ ) # Save everything accelerator.save_state() # Load everything back in and make sure all states work accelerator.load_state(os.path.join(lowerCAmelCase__ , "checkpoints" , "checkpoint_1" ) ) test_rands += train(1 , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ ) ((UpperCAmelCase) , (UpperCAmelCase)) = model.a.item(), model.b.item() UpperCAmelCase = optimizer.state_dict() self.assertEqual(lowerCAmelCase__ , lowerCAmelCase__ ) self.assertEqual(lowerCAmelCase__ , lowerCAmelCase__ ) self.assertEqual(lowerCAmelCase__ , lowerCAmelCase__ ) self.assertEqual(lowerCAmelCase__ , lowerCAmelCase__ ) def _UpperCamelCase ( self : Tuple ) -> List[Any]: UpperCAmelCase = torch.tensor([1, 2, 3] ) UpperCAmelCase = torch.tensor([2, 3, 4] ) UpperCAmelCase = DummyModel() UpperCAmelCase = torch.optim.Adam(net.parameters() ) UpperCAmelCase = Accelerator() with self.assertRaises(lowerCAmelCase__ ) as ve: accelerator.register_for_checkpointing(lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ ) UpperCAmelCase = str(ve.exception ) self.assertTrue("Item at index 0" in message ) self.assertTrue("Item at index 1" in message ) self.assertFalse("Item at index 2" in message ) self.assertFalse("Item at index 3" in message ) def _UpperCamelCase ( self : Tuple ) -> int: with tempfile.TemporaryDirectory() as tmpdir: set_seed(4_2 ) UpperCAmelCase = DummyModel() UpperCAmelCase = torch.optim.Adam(params=model.parameters() , lr=1e-3 ) UpperCAmelCase = torch.optim.lr_scheduler.StepLR(lowerCAmelCase__ , step_size=1 , gamma=0.99 ) UpperCAmelCase , UpperCAmelCase = dummy_dataloaders() UpperCAmelCase = ProjectConfiguration(automatic_checkpoint_naming=lowerCAmelCase__ ) # Train baseline UpperCAmelCase = Accelerator(project_dir=lowerCAmelCase__ , project_config=lowerCAmelCase__ ) UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase = accelerator.prepare( lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ ) # Save initial accelerator.save_state() UpperCAmelCase = scheduler.state_dict() train(3 , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ ) self.assertNotEqual(lowerCAmelCase__ , scheduler.state_dict() ) # Load everything back in and make sure all states work accelerator.load_state(os.path.join(lowerCAmelCase__ , "checkpoints" , "checkpoint_0" ) ) self.assertEqual(lowerCAmelCase__ , scheduler.state_dict() ) def _UpperCamelCase ( self : List[str] ) -> List[Any]: with tempfile.TemporaryDirectory() as tmpdir: set_seed(4_2 ) UpperCAmelCase = DummyModel() UpperCAmelCase = ProjectConfiguration(automatic_checkpoint_naming=lowerCAmelCase__ , total_limit=2 ) # Train baseline UpperCAmelCase = Accelerator(project_dir=lowerCAmelCase__ , project_config=lowerCAmelCase__ ) UpperCAmelCase = accelerator.prepare(lowerCAmelCase__ ) # Save 3 states: for _ in range(1_1 ): accelerator.save_state() self.assertTrue(not os.path.exists(os.path.join(lowerCAmelCase__ , "checkpoints" , "checkpoint_0" ) ) ) self.assertTrue(os.path.exists(os.path.join(lowerCAmelCase__ , "checkpoints" , "checkpoint_9" ) ) ) self.assertTrue(os.path.exists(os.path.join(lowerCAmelCase__ , "checkpoints" , "checkpoint_10" ) ) ) @require_cuda def _UpperCamelCase ( self : List[Any] ) -> Optional[int]: UpperCAmelCase = ["torchrun", f"--nproc_per_node={torch.cuda.device_count()}", inspect.getfile(self.__class__ )] execute_subprocess_async(lowerCAmelCase__ , env=os.environ.copy() ) if __name__ == "__main__": lowerCAmelCase__ = "/tmp/accelerate/state_checkpointing" lowerCAmelCase__ = DummyModel() lowerCAmelCase__ = torch.optim.Adam(params=model.parameters(), lr=1e-3) lowerCAmelCase__ = torch.optim.lr_scheduler.StepLR(optimizer, step_size=1, gamma=0.9_9) lowerCAmelCase__, lowerCAmelCase__ = dummy_dataloaders() lowerCAmelCase__ = ProjectConfiguration(automatic_checkpoint_naming=True) # Train baseline lowerCAmelCase__ = Accelerator(project_dir=savedir, project_config=project_config, mixed_precision="no") if accelerator.process_index == 0: if os.path.exists(savedir): shutil.rmtree(savedir) os.makedirs(savedir) lowerCAmelCase__, lowerCAmelCase__, lowerCAmelCase__, lowerCAmelCase__, lowerCAmelCase__ = accelerator.prepare( model, optimizer, train_dataloader, valid_dataloader, scheduler ) lowerCAmelCase__, lowerCAmelCase__ = accelerator.prepare(model, optimizer) train(3, model, train_dataloader, optimizer, accelerator, scheduler) # Check that the intial optimizer is loaded on the GPU for group in optimizer.param_groups: lowerCAmelCase__ = group["params"][0].device break assert param_device.type == accelerator.device.type lowerCAmelCase__ = model.cpu() accelerator.wait_for_everyone() accelerator.save_state() accelerator.wait_for_everyone() # Check CPU state accelerator.load_state(os.path.join(savedir, "checkpoints", "checkpoint_0"), map_location="cpu") for group in optimizer.param_groups: lowerCAmelCase__ = group["params"][0].device break assert ( param_device.type == torch.device("cpu").type ), f"Loaded optimizer states did not match, expected to be loaded on the CPU but got {param_device}" # Check device state model.to(accelerator.device) accelerator.load_state(os.path.join(savedir, "checkpoints", "checkpoint_0"), map_location="on_device") for group in optimizer.param_groups: lowerCAmelCase__ = group["params"][0].device break assert ( param_device.type == accelerator.device.type ), f"Loaded optimizer states did not match, expected to be loaded on {accelerator.device} but got {param_device}" # Check error with pytest.raises(TypeError, match="Unsupported optimizer map location passed"): accelerator.load_state(os.path.join(savedir, "checkpoints", "checkpoint_0"), map_location="invalid") accelerator.wait_for_everyone() if accelerator.process_index == 0: shutil.rmtree(savedir) accelerator.wait_for_everyone()
712
import glob import os import random from string import ascii_lowercase, digits import cva lowerCAmelCase__ = "" lowerCAmelCase__ = "" lowerCAmelCase__ = "" lowerCAmelCase__ = 1 # (0 is vertical, 1 is horizontal) def _lowerCAmelCase( ): UpperCAmelCase , UpperCAmelCase = get_dataset(__A , __A ) print("Processing..." ) UpperCAmelCase , UpperCAmelCase , UpperCAmelCase = update_image_and_anno(__A , __A , __A ) for index, image in enumerate(__A ): # Get random string code: '7b7ad245cdff75241935e4dd860f3bad' UpperCAmelCase = random_chars(32 ) UpperCAmelCase = paths[index].split(os.sep )[-1].rsplit("." , 1 )[0] UpperCAmelCase = F"{OUTPUT_DIR}/{file_name}_FLIP_{letter_code}" cva.imwrite(F"/{file_root}.jpg" , __A , [cva.IMWRITE_JPEG_QUALITY, 85] ) print(F"Success {index+1}/{len(__A )} with {file_name}" ) UpperCAmelCase = [] for anno in new_annos[index]: UpperCAmelCase = F"{anno[0]} {anno[1]} {anno[2]} {anno[3]} {anno[4]}" annos_list.append(__A ) with open(F"/{file_root}.txt" , "w" ) as outfile: outfile.write("\n".join(line for line in annos_list ) ) def _lowerCAmelCase( __A , __A ): UpperCAmelCase = [] UpperCAmelCase = [] for label_file in glob.glob(os.path.join(__A , "*.txt" ) ): UpperCAmelCase = label_file.split(os.sep )[-1].rsplit("." , 1 )[0] with open(__A ) as in_file: UpperCAmelCase = in_file.readlines() UpperCAmelCase = os.path.join(__A , F"{label_name}.jpg" ) UpperCAmelCase = [] for obj_list in obj_lists: UpperCAmelCase = obj_list.rstrip("\n" ).split(" " ) boxes.append( [ int(obj[0] ), float(obj[1] ), float(obj[2] ), float(obj[3] ), float(obj[4] ), ] ) if not boxes: continue img_paths.append(__A ) labels.append(__A ) return img_paths, labels def _lowerCAmelCase( __A , __A , __A = 1 ): UpperCAmelCase = [] UpperCAmelCase = [] UpperCAmelCase = [] for idx in range(len(__A ) ): UpperCAmelCase = [] UpperCAmelCase = img_list[idx] path_list.append(__A ) UpperCAmelCase = anno_list[idx] UpperCAmelCase = cva.imread(__A ) if flip_type == 1: UpperCAmelCase = cva.flip(__A , __A ) for bbox in img_annos: UpperCAmelCase = 1 - bbox[1] new_annos.append([bbox[0], x_center_new, bbox[2], bbox[3], bbox[4]] ) elif flip_type == 0: UpperCAmelCase = cva.flip(__A , __A ) for bbox in img_annos: UpperCAmelCase = 1 - bbox[2] new_annos.append([bbox[0], bbox[1], y_center_new, bbox[3], bbox[4]] ) new_annos_lists.append(__A ) new_imgs_list.append(__A ) return new_imgs_list, new_annos_lists, path_list def _lowerCAmelCase( __A = 32 ): assert number_char > 1, "The number of character should greater than 1" UpperCAmelCase = ascii_lowercase + digits return "".join(random.choice(__A ) for _ in range(__A ) ) if __name__ == "__main__": main() print("DONE ✅")
1
0
import glob import os import random from string import ascii_lowercase, digits import cva lowerCAmelCase__ = "" lowerCAmelCase__ = "" lowerCAmelCase__ = "" lowerCAmelCase__ = 1 # (0 is vertical, 1 is horizontal) def _lowerCAmelCase( ): UpperCAmelCase , UpperCAmelCase = get_dataset(__A , __A ) print("Processing..." ) UpperCAmelCase , UpperCAmelCase , UpperCAmelCase = update_image_and_anno(__A , __A , __A ) for index, image in enumerate(__A ): # Get random string code: '7b7ad245cdff75241935e4dd860f3bad' UpperCAmelCase = random_chars(32 ) UpperCAmelCase = paths[index].split(os.sep )[-1].rsplit("." , 1 )[0] UpperCAmelCase = F"{OUTPUT_DIR}/{file_name}_FLIP_{letter_code}" cva.imwrite(F"/{file_root}.jpg" , __A , [cva.IMWRITE_JPEG_QUALITY, 85] ) print(F"Success {index+1}/{len(__A )} with {file_name}" ) UpperCAmelCase = [] for anno in new_annos[index]: UpperCAmelCase = F"{anno[0]} {anno[1]} {anno[2]} {anno[3]} {anno[4]}" annos_list.append(__A ) with open(F"/{file_root}.txt" , "w" ) as outfile: outfile.write("\n".join(line for line in annos_list ) ) def _lowerCAmelCase( __A , __A ): UpperCAmelCase = [] UpperCAmelCase = [] for label_file in glob.glob(os.path.join(__A , "*.txt" ) ): UpperCAmelCase = label_file.split(os.sep )[-1].rsplit("." , 1 )[0] with open(__A ) as in_file: UpperCAmelCase = in_file.readlines() UpperCAmelCase = os.path.join(__A , F"{label_name}.jpg" ) UpperCAmelCase = [] for obj_list in obj_lists: UpperCAmelCase = obj_list.rstrip("\n" ).split(" " ) boxes.append( [ int(obj[0] ), float(obj[1] ), float(obj[2] ), float(obj[3] ), float(obj[4] ), ] ) if not boxes: continue img_paths.append(__A ) labels.append(__A ) return img_paths, labels def _lowerCAmelCase( __A , __A , __A = 1 ): UpperCAmelCase = [] UpperCAmelCase = [] UpperCAmelCase = [] for idx in range(len(__A ) ): UpperCAmelCase = [] UpperCAmelCase = img_list[idx] path_list.append(__A ) UpperCAmelCase = anno_list[idx] UpperCAmelCase = cva.imread(__A ) if flip_type == 1: UpperCAmelCase = cva.flip(__A , __A ) for bbox in img_annos: UpperCAmelCase = 1 - bbox[1] new_annos.append([bbox[0], x_center_new, bbox[2], bbox[3], bbox[4]] ) elif flip_type == 0: UpperCAmelCase = cva.flip(__A , __A ) for bbox in img_annos: UpperCAmelCase = 1 - bbox[2] new_annos.append([bbox[0], bbox[1], y_center_new, bbox[3], bbox[4]] ) new_annos_lists.append(__A ) new_imgs_list.append(__A ) return new_imgs_list, new_annos_lists, path_list def _lowerCAmelCase( __A = 32 ): assert number_char > 1, "The number of character should greater than 1" UpperCAmelCase = ascii_lowercase + digits return "".join(random.choice(__A ) for _ in range(__A ) ) if __name__ == "__main__": main() print("DONE ✅")
713
def _lowerCAmelCase( __A ): if not isinstance(__A , __A ): raise TypeError("only integers accepted as input" ) else: UpperCAmelCase = str(abs(__A ) ) UpperCAmelCase = [list(__A ) for char in range(len(__A ) )] for index in range(len(__A ) ): num_transpositions[index].pop(__A ) return max( int("".join(list(__A ) ) ) for transposition in num_transpositions ) if __name__ == "__main__": __import__("doctest").testmod()
1
0
# Copyright (c) 2021-, NVIDIA CORPORATION. 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. #################################################################################################### # # Note: If when running this conversion script you're getting an exception: # ModuleNotFoundError: No module named 'megatron.model.enums' # you need to tell python where to find the clone of Megatron-LM, e.g.: # # cd /tmp # git clone https://github.com/NVIDIA/Megatron-LM # PYTHONPATH=/tmp/Megatron-LM python src/transformers/models/megatron_gpt2/convert_megatron_gpt2_checkpoint.py ... # # if you already have it cloned elsewhere, simply adjust the path to the existing path # # If the training was done using a Megatron-LM fork, e.g., # https://github.com/microsoft/Megatron-DeepSpeed/ then chances are that you need to have that one # in your path, i.e., /path/to/Megatron-DeepSpeed/ # import argparse import os import re import zipfile import torch from transformers import AutoTokenizer, GPTaConfig def _lowerCAmelCase( __A , __A , __A=0 ): # Format the message. if name is None: UpperCAmelCase = None else: UpperCAmelCase = "." * max(0 , spaces - 2 ) + "# {:" + str(50 - spaces ) + "s}" UpperCAmelCase = fmt.format(__A ) # Print and recurse (if needed). if isinstance(__A , __A ): if msg is not None: print(__A ) for k in val.keys(): recursive_print(__A , val[k] , spaces + 2 ) elif isinstance(__A , torch.Tensor ): print(__A , ":" , val.size() ) else: print(__A , ":" , __A ) def _lowerCAmelCase( __A , __A , __A , __A , __A ): # Permutes layout of param tensor to [num_splits * num_heads * hidden_size, :] # for compatibility with later versions of NVIDIA Megatron-LM. # The inverse operation is performed inside Megatron-LM to read checkpoints: # https://github.com/NVIDIA/Megatron-LM/blob/v2.4/megatron/checkpointing.py#L209 # If param is the weight tensor of the self-attention block, the returned tensor # will have to be transposed one more time to be read by HuggingFace GPT2. UpperCAmelCase = param.size() if checkpoint_version == 1.0: # version 1.0 stores [num_heads * hidden_size * num_splits, :] UpperCAmelCase = (num_heads, hidden_size, num_splits) + input_shape[1:] UpperCAmelCase = param.view(*__A ) UpperCAmelCase = param.transpose(0 , 2 ) UpperCAmelCase = param.transpose(1 , 2 ).contiguous() elif checkpoint_version >= 2.0: # other versions store [num_heads * num_splits * hidden_size, :] UpperCAmelCase = (num_heads, num_splits, hidden_size) + input_shape[1:] UpperCAmelCase = param.view(*__A ) UpperCAmelCase = param.transpose(0 , 1 ).contiguous() UpperCAmelCase = param.view(*__A ) return param def _lowerCAmelCase( __A , __A , __A ): # The converted output model. UpperCAmelCase = {} # old versions did not store training args UpperCAmelCase = input_state_dict.get("args" , __A ) if ds_args is not None: # do not make the user write a config file when the exact dimensions/sizes are already in the checkpoint # from pprint import pprint # pprint(vars(ds_args)) UpperCAmelCase = ds_args.padded_vocab_size UpperCAmelCase = ds_args.max_position_embeddings UpperCAmelCase = ds_args.hidden_size UpperCAmelCase = ds_args.num_layers UpperCAmelCase = ds_args.num_attention_heads UpperCAmelCase = ds_args.ffn_hidden_size # pprint(config) # The number of heads. UpperCAmelCase = config.n_head # The hidden_size per head. UpperCAmelCase = config.n_embd // config.n_head # Megatron-LM checkpoint version if "checkpoint_version" in input_state_dict.keys(): UpperCAmelCase = input_state_dict["checkpoint_version"] else: UpperCAmelCase = 0.0 # The model. UpperCAmelCase = input_state_dict["model"] # The language model. UpperCAmelCase = model["language_model"] # The embeddings. UpperCAmelCase = lm["embedding"] # The word embeddings. UpperCAmelCase = embeddings["word_embeddings"]["weight"] # Truncate the embedding table to vocab_size rows. UpperCAmelCase = word_embeddings[: config.vocab_size, :] UpperCAmelCase = word_embeddings # The position embeddings. UpperCAmelCase = embeddings["position_embeddings"]["weight"] # Read the causal mask dimension (seqlen). [max_sequence_length, hidden_size] UpperCAmelCase = pos_embeddings.size(0 ) if n_positions != config.n_positions: raise ValueError( F"pos_embeddings.max_sequence_length={n_positions} and config.n_positions={config.n_positions} don't match" ) # Store the position embeddings. UpperCAmelCase = pos_embeddings # The transformer. UpperCAmelCase = lm["transformer"] if "transformer" in lm.keys() else lm["encoder"] # The regex to extract layer names. UpperCAmelCase = re.compile(r"layers\.(\d+)\.([a-z0-9_.]+)\.([a-z]+)" ) # The simple map of names for "automated" rules. UpperCAmelCase = { "attention.dense": ".attn.c_proj.", "self_attention.dense": ".attn.c_proj.", "mlp.dense_h_to_4h": ".mlp.c_fc.", "mlp.dense_4h_to_h": ".mlp.c_proj.", } # Extract the layers. for key, val in transformer.items(): # Match the name. UpperCAmelCase = layer_re.match(__A ) # Stop if that's not a layer if m is None: break # The index of the layer. UpperCAmelCase = int(m.group(1 ) ) # The name of the operation. UpperCAmelCase = m.group(2 ) # Is it a weight or a bias? UpperCAmelCase = m.group(3 ) # The name of the layer. UpperCAmelCase = F"transformer.h.{layer_idx}" # For layernorm(s), simply store the layer norm. if op_name.endswith("layernorm" ): UpperCAmelCase = "ln_1" if op_name.startswith("input" ) else "ln_2" UpperCAmelCase = val # Transpose the QKV matrix. elif ( op_name == "attention.query_key_value" or op_name == "self_attention.query_key_value" ) and weight_or_bias == "weight": # Insert a tensor of 1x1xDxD bias. UpperCAmelCase = torch.tril(torch.ones((n_positions, n_positions) , dtype=torch.floataa ) ).view( 1 , 1 , __A , __A ) UpperCAmelCase = causal_mask # Insert a "dummy" tensor for masked_bias. UpperCAmelCase = torch.tensor(-1E4 , dtype=torch.floataa ) UpperCAmelCase = masked_bias UpperCAmelCase = fix_query_key_value_ordering(__A , __A , 3 , __A , __A ) # Megatron stores (3*D) x D but transformers-GPT2 expects D x 3*D. UpperCAmelCase = out_val.transpose(0 , 1 ).contiguous() # Store. UpperCAmelCase = out_val # Transpose the bias. elif ( op_name == "attention.query_key_value" or op_name == "self_attention.query_key_value" ) and weight_or_bias == "bias": UpperCAmelCase = fix_query_key_value_ordering(__A , __A , 3 , __A , __A ) # Store. No change of shape. UpperCAmelCase = out_val # Transpose the weights. elif weight_or_bias == "weight": UpperCAmelCase = megatron_to_transformers[op_name] UpperCAmelCase = val.transpose(0 , 1 ) # Copy the bias. elif weight_or_bias == "bias": UpperCAmelCase = megatron_to_transformers[op_name] UpperCAmelCase = val # DEBUG. assert config.n_layer == layer_idx + 1 # The final layernorm. UpperCAmelCase = transformer["final_layernorm.weight"] UpperCAmelCase = transformer["final_layernorm.bias"] # For LM head, transformers' wants the matrix to weight embeddings. UpperCAmelCase = word_embeddings # It should be done! return output_state_dict def _lowerCAmelCase( ): # Create the argument parser. UpperCAmelCase = argparse.ArgumentParser() parser.add_argument("--print-checkpoint-structure" , action="store_true" ) parser.add_argument( "path_to_checkpoint" , type=__A , help="Path to the checkpoint file (.zip archive or direct .pt file)" , ) parser.add_argument( "--config_file" , default="" , type=__A , help="An optional config json file describing the pre-trained model." , ) UpperCAmelCase = parser.parse_args() # Extract the basename. UpperCAmelCase = os.path.dirname(args.path_to_checkpoint ) # Load the model. # the .zip is very optional, let's keep it for backward compatibility print(F"Extracting PyTorch state dictionary from {args.path_to_checkpoint}" ) if args.path_to_checkpoint.endswith(".zip" ): with zipfile.ZipFile(args.path_to_checkpoint , "r" ) as checkpoint: with checkpoint.open("release/mp_rank_00/model_optim_rng.pt" ) as pytorch_dict: UpperCAmelCase = torch.load(__A , map_location="cpu" ) else: UpperCAmelCase = torch.load(args.path_to_checkpoint , map_location="cpu" ) UpperCAmelCase = input_state_dict.get("args" , __A ) # Read the config, or default to the model released by NVIDIA. if args.config_file == "": if ds_args is not None: if ds_args.bias_gelu_fusion: UpperCAmelCase = "gelu_fast" elif ds_args.openai_gelu: UpperCAmelCase = "gelu_new" else: UpperCAmelCase = "gelu" else: # in the very early days this used to be "gelu_new" UpperCAmelCase = "gelu_new" # Spell out all parameters in case the defaults change. UpperCAmelCase = GPTaConfig( vocab_size=50257 , n_positions=1024 , n_embd=1024 , n_layer=24 , n_head=16 , n_inner=4096 , activation_function=__A , resid_pdrop=0.1 , embd_pdrop=0.1 , attn_pdrop=0.1 , layer_norm_epsilon=1E-5 , initializer_range=0.02 , summary_type="cls_index" , summary_use_proj=__A , summary_activation=__A , summary_proj_to_labels=__A , summary_first_dropout=0.1 , scale_attn_weights=__A , use_cache=__A , bos_token_id=50256 , eos_token_id=50256 , ) else: UpperCAmelCase = GPTaConfig.from_json_file(args.config_file ) UpperCAmelCase = ["GPT2LMHeadModel"] # Convert. print("Converting" ) UpperCAmelCase = convert_megatron_checkpoint(__A , __A , __A ) # Print the structure of converted state dict. if args.print_checkpoint_structure: recursive_print(__A , __A ) # Add tokenizer class info to config # see https://github.com/huggingface/transformers/issues/13906) if ds_args is not None: UpperCAmelCase = ds_args.tokenizer_type if tokenizer_type == "GPT2BPETokenizer": UpperCAmelCase = "gpt2" elif tokenizer_type == "PretrainedFromHF": UpperCAmelCase = ds_args.tokenizer_name_or_path else: raise ValueError(F"Unrecognized tokenizer_type {tokenizer_type}" ) else: UpperCAmelCase = "gpt2" UpperCAmelCase = AutoTokenizer.from_pretrained(__A ) UpperCAmelCase = type(__A ).__name__ UpperCAmelCase = tokenizer_class # Store the config to file. print("Saving config" ) config.save_pretrained(__A ) # Save tokenizer based on args print(F"Adding {tokenizer_class} tokenizer files" ) tokenizer.save_pretrained(__A ) # Store the state_dict to file. UpperCAmelCase = os.path.join(__A , "pytorch_model.bin" ) print(F"Saving checkpoint to \"{output_checkpoint_file}\"" ) torch.save(__A , __A ) #################################################################################################### if __name__ == "__main__": main() ####################################################################################################
714
import logging import re import pytorch_quantization import pytorch_quantization.nn as quant_nn import torch from pytorch_quantization import calib from pytorch_quantization.tensor_quant import QuantDescriptor lowerCAmelCase__ = logging.getLogger(__name__) lowerCAmelCase__ = 50 # max width of layer names lowerCAmelCase__ = 70 # max width of quantizer names def _lowerCAmelCase( __A ): UpperCAmelCase = parser.add_argument_group("quant_trainer arguments" ) group.add_argument("--wprec" , type=__A , default=8 , help="weight precision" ) group.add_argument("--aprec" , type=__A , default=8 , help="activation precision" ) group.add_argument("--quant-per-tensor" , action="store_true" , help="per tensor weight scaling" ) group.add_argument("--quant-disable" , action="store_true" , help="disable all quantizers" ) group.add_argument("--quant-disable-embeddings" , action="store_true" , help="disable all embeddings quantizers" ) group.add_argument("--quant-disable-keyword" , type=__A , nargs="+" , help="disable quantizers by keyword" ) group.add_argument("--quant-disable-layer-module" , type=__A , help="disable quantizers by keyword under layer." ) group.add_argument("--quant-enable-layer-module" , type=__A , help="enable quantizers by keyword under layer" ) group.add_argument("--calibrator" , default="max" , help="which quantization range calibrator to use" ) group.add_argument("--percentile" , default=__A , type=__A , help="percentile for PercentileCalibrator" ) group.add_argument("--fuse-qkv" , action="store_true" , help="use the same scale factor for qkv" ) group.add_argument("--clip-gelu" , metavar="N" , type=__A , help="clip gelu output maximum value to N" ) group.add_argument( "--recalibrate-weights" , action="store_true" , help=( "recalibrate weight amaxes by taking the max of the weights." " amaxes will be computed with the current quantization granularity (axis)." ) , ) def _lowerCAmelCase( __A ): if args.calibrator == "max": UpperCAmelCase = "max" elif args.calibrator == "percentile": if args.percentile is None: raise ValueError("Specify --percentile when using percentile calibrator" ) UpperCAmelCase = "histogram" elif args.calibrator == "mse": UpperCAmelCase = "histogram" else: raise ValueError(F"Invalid calibrator {args.calibrator}" ) UpperCAmelCase = QuantDescriptor(num_bits=args.aprec , calib_method=__A ) UpperCAmelCase = QuantDescriptor(num_bits=args.wprec , axis=(None if args.quant_per_tensor else (0,)) ) quant_nn.QuantLinear.set_default_quant_desc_input(__A ) quant_nn.QuantLinear.set_default_quant_desc_weight(__A ) def _lowerCAmelCase( __A , __A , __A=False , __A=False ): logger.info("Configuring Model for Quantization" ) logger.info(F"using quantization package {pytorch_quantization.__file__}" ) if not calib: if args.quant_disable_embeddings: set_quantizer_by_name(__A , ["embeddings"] , which="weight" , _disabled=__A ) if args.quant_disable: set_quantizer_by_name(__A , [""] , _disabled=__A ) if args.quant_disable_keyword: set_quantizer_by_name(__A , args.quant_disable_keyword , _disabled=__A ) if args.quant_disable_layer_module: set_quantizer_by_name(__A , [r"layer.\d+." + args.quant_disable_layer_module] , _disabled=__A ) if args.quant_enable_layer_module: set_quantizer_by_name(__A , [r"layer.\d+." + args.quant_enable_layer_module] , _disabled=__A ) if args.recalibrate_weights: recalibrate_weights(__A ) if args.fuse_qkv: fuse_qkv(__A , __A ) if args.clip_gelu: clip_gelu(__A , args.clip_gelu ) # if args.local_rank in [-1, 0] and not calib: print_quant_summary(__A ) def _lowerCAmelCase( __A ): logger.info("Enabling Calibration" ) for name, module in model.named_modules(): if name.endswith("_quantizer" ): if module._calibrator is not None: module.disable_quant() module.enable_calib() else: module.disable() logger.info(F"{name:80}: {module}" ) def _lowerCAmelCase( __A , __A ): logger.info("Loading calibrated amax" ) for name, module in model.named_modules(): if name.endswith("_quantizer" ): if module._calibrator is not None: if isinstance(module._calibrator , calib.MaxCalibrator ): module.load_calib_amax() else: module.load_calib_amax("percentile" , percentile=args.percentile ) module.enable_quant() module.disable_calib() else: module.enable() model.cuda() print_quant_summary(__A ) def _lowerCAmelCase( __A , __A ): def fusea(__A , __A , __A ): for mod in [qq, qk, qv]: if not hasattr(__A , "_amax" ): print(" WARNING: NO AMAX BUFFER" ) return UpperCAmelCase = qq._amax.detach().item() UpperCAmelCase = qk._amax.detach().item() UpperCAmelCase = qv._amax.detach().item() UpperCAmelCase = max(__A , __A , __A ) qq._amax.fill_(__A ) qk._amax.fill_(__A ) qv._amax.fill_(__A ) logger.info(F" q={q:5.2f} k={k:5.2f} v={v:5.2f} -> {amax:5.2f}" ) for name, mod in model.named_modules(): if name.endswith(".attention.self" ): logger.info(F"FUSE_QKV: {name:{name_width}}" ) fusea(mod.matmul_q_input_quantizer , mod.matmul_k_input_quantizer , mod.matmul_v_input_quantizer ) if args.quant_per_tensor: fusea(mod.query._weight_quantizer , mod.key._weight_quantizer , mod.value._weight_quantizer ) def _lowerCAmelCase( __A , __A ): for name, mod in model.named_modules(): if name.endswith(".output.dense" ) and not name.endswith("attention.output.dense" ): UpperCAmelCase = mod._input_quantizer._amax.data.detach().item() mod._input_quantizer._amax.data.detach().clamp_(max=__A ) UpperCAmelCase = mod._input_quantizer._amax.data.detach().item() logger.info(F"CLIP_GELU: {name:{name_width}} amax: {amax_init:5.2f} -> {amax:5.2f}" ) def _lowerCAmelCase( __A ): for name, mod in model.named_modules(): if hasattr(__A , "_weight_quantizer" ) and mod._weight_quantizer.axis is not None: UpperCAmelCase = mod.weight.shape[0] UpperCAmelCase = mod._weight_quantizer._amax.detach() UpperCAmelCase = torch.ones(__A , dtype=amax.dtype , device=amax.device ) * amax print(F"expanding {name} {amax} -> {mod._weight_quantizer._amax}" ) def _lowerCAmelCase( __A ): for name, mod in model.named_modules(): if hasattr(__A , "_weight_quantizer" ): if not hasattr(mod.weight_quantizer , "_amax" ): print("RECALIB: {name:{name_width}} WARNING: NO AMAX BUFFER" ) continue # determine which axes to reduce across # e.g. a 4D tensor quantized per axis 0 should reduce over (1,2,3) UpperCAmelCase = set() if mod._weight_quantizer.axis is None else set(mod._weight_quantizer.axis ) UpperCAmelCase = set(range(len(mod.weight.size() ) ) ) - axis_set UpperCAmelCase = pytorch_quantization.utils.reduce_amax(mod.weight , axis=__A , keepdims=__A ).detach() logger.info(F"RECALIB: {name:{name_width}} {mod._weight_quantizer._amax.flatten()} -> {amax.flatten()}" ) UpperCAmelCase = amax def _lowerCAmelCase( __A , __A=25 , __A=180 , __A=None ): if ignore is None: UpperCAmelCase = [] elif not isinstance(__A , __A ): UpperCAmelCase = [ignore] UpperCAmelCase = 0 for name, mod in model.named_modules(): if not hasattr(__A , "weight" ): continue UpperCAmelCase = max(__A , len(__A ) ) for name, mod in model.named_modules(): UpperCAmelCase = getattr(__A , "_input_quantizer" , __A ) UpperCAmelCase = getattr(__A , "_weight_quantizer" , __A ) if not hasattr(__A , "weight" ): continue if type(__A ) in ignore: continue if [True for s in ignore if type(__A ) is str and s in name]: continue UpperCAmelCase = F"Act:{input_q.extra_repr()}" UpperCAmelCase = F"Wgt:{weight_q.extra_repr()}" UpperCAmelCase = F"{name:{name_width}} {act_str} {wgt_str}" if len(__A ) <= line_width: logger.info(__A ) else: logger.info(F"{name:{name_width}} {act_str}" ) logger.info(F"{' ':{name_width}} {wgt_str}" ) def _lowerCAmelCase( __A ): UpperCAmelCase = 0 for name, mod in model.named_modules(): if isinstance(__A , pytorch_quantization.nn.TensorQuantizer ): print(F"{name:80} {mod}" ) count += 1 print(F"{count} TensorQuantizers found in model" ) def _lowerCAmelCase( __A , __A , __A , __A , __A ): UpperCAmelCase = getattr(__A , __A , __A ) if quantizer_mod is not None: assert hasattr(__A , __A ) setattr(__A , __A , __A ) else: logger.warning(F"{name} has no {quantizer}" ) def _lowerCAmelCase( __A , __A , __A="both" , **__A ): UpperCAmelCase = F"Warning: changing {which} quantizers of {name:{qname_width}}" for k, v in kwargs.items(): s += F" {k}={v}" if which in ["input", "both"]: set_quantizer(__A , __A , "_input_quantizer" , __A , __A ) if which in ["weight", "both"]: set_quantizer(__A , __A , "_weight_quantizer" , __A , __A ) logger.info(__A ) def _lowerCAmelCase( __A , __A , **__A ): for name, mod in model.named_modules(): if hasattr(__A , "_input_quantizer" ) or hasattr(__A , "_weight_quantizer" ): for n in names: if re.search(__A , __A ): set_quantizers(__A , __A , **__A ) elif name.endswith("_quantizer" ): for n in names: if re.search(__A , __A ): UpperCAmelCase = F"Warning: changing {name:{name_width}}" for k, v in kwargs.items(): s += F" {k}={v}" setattr(__A , __A , __A ) logger.info(__A )
1
0
from __future__ import annotations def _lowerCAmelCase( __A , __A ): UpperCAmelCase = [] UpperCAmelCase = [] UpperCAmelCase = 0 UpperCAmelCase = sum(__A ) create_state_space_tree(__A , __A , __A , __A , __A , __A ) return result def _lowerCAmelCase( __A , __A , __A , __A , __A , __A , ): if sum(__A ) > max_sum or (remaining_nums_sum + sum(__A )) < max_sum: return if sum(__A ) == max_sum: result.append(__A ) return for index in range(__A , len(__A ) ): create_state_space_tree( __A , __A , index + 1 , [*path, nums[index]] , __A , remaining_nums_sum - nums[index] , ) lowerCAmelCase__ = [3, 34, 4, 12, 5, 2] lowerCAmelCase__ = 9 lowerCAmelCase__ = generate_sum_of_subsets_soln(nums, max_sum) print(*result)
715
def _lowerCAmelCase( __A ): assert column_title.isupper() UpperCAmelCase = 0 UpperCAmelCase = len(__A ) - 1 UpperCAmelCase = 0 while index >= 0: UpperCAmelCase = (ord(column_title[index] ) - 64) * pow(26 , __A ) answer += value power += 1 index -= 1 return answer if __name__ == "__main__": from doctest import testmod testmod()
1
0
import math def _lowerCAmelCase( __A , __A ): if 0 not in (x, y): # We use the relation x^y = y*log10(x), where 10 is the base. return y * math.logaa(__A ) else: if x == 0: # 0 raised to any number is 0 return 0 elif y == 0: return 1 # any number raised to 0 is 1 raise AssertionError("This should never happen" ) if __name__ == "__main__": # Main function # Read two numbers from input and typecast them to int using map function. # Here x is the base and y is the power. lowerCAmelCase__ = "Enter the base and the power separated by a comma: " lowerCAmelCase__, lowerCAmelCase__ = map(int, input(prompt).split(",")) lowerCAmelCase__, lowerCAmelCase__ = map(int, input(prompt).split(",")) # We find the log of each number, using the function res(), which takes two # arguments. lowerCAmelCase__ = res(xa, ya) lowerCAmelCase__ = res(xa, ya) # We check for the largest number if resa > resa: print("Largest number is", xa, "^", ya) elif resa > resa: print("Largest number is", xa, "^", ya) else: print("Both are equal")
716
import sys import tempfile import unittest import unittest.mock as mock from pathlib import Path from huggingface_hub import HfFolder, delete_repo from requests.exceptions import HTTPError from transformers import AutoFeatureExtractor, WavaVecaFeatureExtractor from transformers.testing_utils import TOKEN, USER, get_tests_dir, is_staging_test sys.path.append(str(Path(__file__).parent.parent / "utils")) from test_module.custom_feature_extraction import CustomFeatureExtractor # noqa E402 lowerCAmelCase__ = get_tests_dir("fixtures") class __magic_name__ ( unittest.TestCase ): def _UpperCamelCase ( self : Tuple ) -> Union[str, Any]: # A mock response for an HTTP head request to emulate server down UpperCAmelCase = mock.Mock() UpperCAmelCase = 5_0_0 UpperCAmelCase = {} UpperCAmelCase = HTTPError UpperCAmelCase = {} # Download this model to make sure it's in the cache. UpperCAmelCase = WavaVecaFeatureExtractor.from_pretrained("hf-internal-testing/tiny-random-wav2vec2" ) # Under the mock environment we get a 500 error when trying to reach the model. with mock.patch("requests.Session.request" , return_value=lowerCAmelCase__ ) as mock_head: UpperCAmelCase = WavaVecaFeatureExtractor.from_pretrained("hf-internal-testing/tiny-random-wav2vec2" ) # This check we did call the fake head request mock_head.assert_called() def _UpperCamelCase ( self : List[Any] ) -> Dict: # This test is for deprecated behavior and can be removed in v5 UpperCAmelCase = WavaVecaFeatureExtractor.from_pretrained( "https://huggingface.co/hf-internal-testing/tiny-random-wav2vec2/resolve/main/preprocessor_config.json" ) @is_staging_test class __magic_name__ ( unittest.TestCase ): @classmethod def _UpperCamelCase ( cls : List[str] ) -> List[Any]: UpperCAmelCase = TOKEN HfFolder.save_token(lowerCAmelCase__ ) @classmethod def _UpperCamelCase ( cls : Optional[int] ) -> Union[str, Any]: try: delete_repo(token=cls._token , repo_id="test-feature-extractor" ) except HTTPError: pass try: delete_repo(token=cls._token , repo_id="valid_org/test-feature-extractor-org" ) except HTTPError: pass try: delete_repo(token=cls._token , repo_id="test-dynamic-feature-extractor" ) except HTTPError: pass def _UpperCamelCase ( self : Any ) -> Any: UpperCAmelCase = WavaVecaFeatureExtractor.from_pretrained(lowerCAmelCase__ ) feature_extractor.push_to_hub("test-feature-extractor" , use_auth_token=self._token ) UpperCAmelCase = WavaVecaFeatureExtractor.from_pretrained(f"{USER}/test-feature-extractor" ) for k, v in feature_extractor.__dict__.items(): self.assertEqual(lowerCAmelCase__ , getattr(lowerCAmelCase__ , lowerCAmelCase__ ) ) # Reset repo delete_repo(token=self._token , repo_id="test-feature-extractor" ) # Push to hub via save_pretrained with tempfile.TemporaryDirectory() as tmp_dir: feature_extractor.save_pretrained( lowerCAmelCase__ , repo_id="test-feature-extractor" , push_to_hub=lowerCAmelCase__ , use_auth_token=self._token ) UpperCAmelCase = WavaVecaFeatureExtractor.from_pretrained(f"{USER}/test-feature-extractor" ) for k, v in feature_extractor.__dict__.items(): self.assertEqual(lowerCAmelCase__ , getattr(lowerCAmelCase__ , lowerCAmelCase__ ) ) def _UpperCamelCase ( self : List[Any] ) -> Tuple: UpperCAmelCase = WavaVecaFeatureExtractor.from_pretrained(lowerCAmelCase__ ) feature_extractor.push_to_hub("valid_org/test-feature-extractor" , use_auth_token=self._token ) UpperCAmelCase = WavaVecaFeatureExtractor.from_pretrained("valid_org/test-feature-extractor" ) for k, v in feature_extractor.__dict__.items(): self.assertEqual(lowerCAmelCase__ , getattr(lowerCAmelCase__ , lowerCAmelCase__ ) ) # Reset repo delete_repo(token=self._token , repo_id="valid_org/test-feature-extractor" ) # Push to hub via save_pretrained with tempfile.TemporaryDirectory() as tmp_dir: feature_extractor.save_pretrained( lowerCAmelCase__ , repo_id="valid_org/test-feature-extractor-org" , push_to_hub=lowerCAmelCase__ , use_auth_token=self._token ) UpperCAmelCase = WavaVecaFeatureExtractor.from_pretrained("valid_org/test-feature-extractor-org" ) for k, v in feature_extractor.__dict__.items(): self.assertEqual(lowerCAmelCase__ , getattr(lowerCAmelCase__ , lowerCAmelCase__ ) ) def _UpperCamelCase ( self : Dict ) -> List[str]: CustomFeatureExtractor.register_for_auto_class() UpperCAmelCase = CustomFeatureExtractor.from_pretrained(lowerCAmelCase__ ) feature_extractor.push_to_hub("test-dynamic-feature-extractor" , use_auth_token=self._token ) # This has added the proper auto_map field to the config self.assertDictEqual( feature_extractor.auto_map , {"AutoFeatureExtractor": "custom_feature_extraction.CustomFeatureExtractor"} , ) UpperCAmelCase = AutoFeatureExtractor.from_pretrained( f"{USER}/test-dynamic-feature-extractor" , trust_remote_code=lowerCAmelCase__ ) # Can't make an isinstance check because the new_feature_extractor is from the CustomFeatureExtractor class of a dynamic module self.assertEqual(new_feature_extractor.__class__.__name__ , "CustomFeatureExtractor" )
1
0
import re from filelock import FileLock try: import nltk lowerCAmelCase__ = True except (ImportError, ModuleNotFoundError): lowerCAmelCase__ = False if NLTK_AVAILABLE: with FileLock(".lock") as lock: nltk.download("punkt", quiet=True) def _lowerCAmelCase( __A ): re.sub("<n>" , "" , __A ) # remove pegasus newline char assert NLTK_AVAILABLE, "nltk must be installed to separate newlines between sentences. (pip install nltk)" return "\n".join(nltk.sent_tokenize(__A ) )
717
import argparse import os import re # All paths are set with the intent you should run this script from the root of the repo with the command # python utils/check_dummies.py lowerCAmelCase__ = "src/diffusers" # Matches is_xxx_available() lowerCAmelCase__ = re.compile(r"is\_([a-z_]*)_available\(\)") # Matches from xxx import bla lowerCAmelCase__ = re.compile(r"\s+from\s+\S*\s+import\s+([^\(\s].*)\n") lowerCAmelCase__ = "\n{0} = None\n" lowerCAmelCase__ = "\nclass {0}(metaclass=DummyObject):\n _backends = {1}\n\n def __init__(self, *args, **kwargs):\n requires_backends(self, {1})\n\n @classmethod\n def from_config(cls, *args, **kwargs):\n requires_backends(cls, {1})\n\n @classmethod\n def from_pretrained(cls, *args, **kwargs):\n requires_backends(cls, {1})\n" lowerCAmelCase__ = "\ndef {0}(*args, **kwargs):\n requires_backends({0}, {1})\n" def _lowerCAmelCase( __A ): UpperCAmelCase = _re_backend.findall(__A ) if len(__A ) == 0: return None return "_and_".join(__A ) def _lowerCAmelCase( ): with open(os.path.join(__A , "__init__.py" ) , "r" , encoding="utf-8" , newline="\n" ) as f: UpperCAmelCase = f.readlines() # Get to the point we do the actual imports for type checking UpperCAmelCase = 0 UpperCAmelCase = {} # Go through the end of the file while line_index < len(__A ): # If the line contains is_backend_available, we grab all objects associated with the `else` block UpperCAmelCase = find_backend(lines[line_index] ) if backend is not None: while not lines[line_index].startswith("else:" ): line_index += 1 line_index += 1 UpperCAmelCase = [] # Until we unindent, add backend objects to the list while line_index < len(__A ) and len(lines[line_index] ) > 1: UpperCAmelCase = lines[line_index] UpperCAmelCase = _re_single_line_import.search(__A ) if single_line_import_search is not None: objects.extend(single_line_import_search.groups()[0].split(", " ) ) elif line.startswith(" " * 8 ): objects.append(line[8:-2] ) line_index += 1 if len(__A ) > 0: UpperCAmelCase = objects else: line_index += 1 return backend_specific_objects def _lowerCAmelCase( __A , __A ): if name.isupper(): return DUMMY_CONSTANT.format(__A ) elif name.islower(): return DUMMY_FUNCTION.format(__A , __A ) else: return DUMMY_CLASS.format(__A , __A ) def _lowerCAmelCase( __A=None ): if backend_specific_objects is None: UpperCAmelCase = read_init() # For special correspondence backend to module name as used in the function requires_modulename UpperCAmelCase = {} for backend, objects in backend_specific_objects.items(): UpperCAmelCase = "[" + ", ".join(F"\"{b}\"" for b in backend.split("_and_" ) ) + "]" UpperCAmelCase = "# This file is autogenerated by the command `make fix-copies`, do not edit.\n" dummy_file += "from ..utils import DummyObject, requires_backends\n\n" dummy_file += "\n".join([create_dummy_object(__A , __A ) for o in objects] ) UpperCAmelCase = dummy_file return dummy_files def _lowerCAmelCase( __A=False ): UpperCAmelCase = create_dummy_files() # For special correspondence backend to shortcut as used in utils/dummy_xxx_objects.py UpperCAmelCase = {"torch": "pt"} # Locate actual dummy modules and read their content. UpperCAmelCase = os.path.join(__A , "utils" ) UpperCAmelCase = { backend: os.path.join(__A , F"dummy_{short_names.get(__A , __A )}_objects.py" ) for backend in dummy_files.keys() } UpperCAmelCase = {} for backend, file_path in dummy_file_paths.items(): if os.path.isfile(__A ): with open(__A , "r" , encoding="utf-8" , newline="\n" ) as f: UpperCAmelCase = f.read() else: UpperCAmelCase = "" for backend in dummy_files.keys(): if dummy_files[backend] != actual_dummies[backend]: if overwrite: print( F"Updating diffusers.utils.dummy_{short_names.get(__A , __A )}_objects.py as the main " "__init__ has new objects." ) with open(dummy_file_paths[backend] , "w" , encoding="utf-8" , newline="\n" ) as f: f.write(dummy_files[backend] ) else: raise ValueError( "The main __init__ has objects that are not present in " F"diffusers.utils.dummy_{short_names.get(__A , __A )}_objects.py. Run `make fix-copies` " "to fix this." ) if __name__ == "__main__": lowerCAmelCase__ = argparse.ArgumentParser() parser.add_argument("--fix_and_overwrite", action="store_true", help="Whether to fix inconsistencies.") lowerCAmelCase__ = parser.parse_args() check_dummies(args.fix_and_overwrite)
1
0
from typing import List, Union from ..utils import ( add_end_docstrings, is_tf_available, is_torch_available, is_vision_available, logging, requires_backends, ) from .base import PIPELINE_INIT_ARGS, Pipeline if is_vision_available(): from PIL import Image from ..image_utils import load_image if is_tf_available(): import tensorflow as tf from ..models.auto.modeling_tf_auto import TF_MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING from ..tf_utils import stable_softmax if is_torch_available(): from ..models.auto.modeling_auto import MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING lowerCAmelCase__ = logging.get_logger(__name__) @add_end_docstrings(_snake_case ) class __magic_name__ ( _snake_case ): def __init__( self : str , *lowerCAmelCase__ : Any , **lowerCAmelCase__ : int ) -> Any: super().__init__(*lowerCAmelCase__ , **lowerCAmelCase__ ) requires_backends(self , "vision" ) self.check_model_type( TF_MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING if self.framework == "tf" else MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING ) def _UpperCamelCase ( self : Dict , lowerCAmelCase__ : Union[str, Any]=None ) -> int: UpperCAmelCase = {} if top_k is not None: UpperCAmelCase = top_k return {}, {}, postprocess_params def __call__( self : Tuple , lowerCAmelCase__ : Union[str, List[str], "Image.Image", List["Image.Image"]] , **lowerCAmelCase__ : str ) -> Union[str, Any]: return super().__call__(lowerCAmelCase__ , **lowerCAmelCase__ ) def _UpperCamelCase ( self : List[Any] , lowerCAmelCase__ : Any ) -> Optional[Any]: UpperCAmelCase = load_image(lowerCAmelCase__ ) UpperCAmelCase = self.image_processor(images=lowerCAmelCase__ , return_tensors=self.framework ) return model_inputs def _UpperCamelCase ( self : Any , lowerCAmelCase__ : Any ) -> Union[str, Any]: UpperCAmelCase = self.model(**lowerCAmelCase__ ) return model_outputs def _UpperCamelCase ( self : List[Any] , lowerCAmelCase__ : Any , lowerCAmelCase__ : Tuple=5 ) -> Union[str, Any]: if top_k > self.model.config.num_labels: UpperCAmelCase = self.model.config.num_labels if self.framework == "pt": UpperCAmelCase = model_outputs.logits.softmax(-1 )[0] UpperCAmelCase , UpperCAmelCase = probs.topk(lowerCAmelCase__ ) elif self.framework == "tf": UpperCAmelCase = stable_softmax(model_outputs.logits , axis=-1 )[0] UpperCAmelCase = tf.math.top_k(lowerCAmelCase__ , k=lowerCAmelCase__ ) UpperCAmelCase , UpperCAmelCase = topk.values.numpy(), topk.indices.numpy() else: raise ValueError(f"Unsupported framework: {self.framework}" ) UpperCAmelCase = scores.tolist() UpperCAmelCase = ids.tolist() return [{"score": score, "label": self.model.config.idalabel[_id]} for score, _id in zip(lowerCAmelCase__ , lowerCAmelCase__ )]
718
from ...configuration_utils import PretrainedConfig from ...utils import logging from ...utils.backbone_utils import BackboneConfigMixin, get_aligned_output_features_output_indices lowerCAmelCase__ = logging.get_logger(__name__) lowerCAmelCase__ = { "facebook/convnextv2-tiny-1k-224": "https://huggingface.co/facebook/convnextv2-tiny-1k-224/resolve/main/config.json", } class __magic_name__ ( _snake_case , _snake_case ): UpperCAmelCase = """convnextv2""" def __init__( self : Optional[Any] , lowerCAmelCase__ : List[Any]=3 , lowerCAmelCase__ : str=4 , lowerCAmelCase__ : Dict=4 , lowerCAmelCase__ : Optional[Any]=None , lowerCAmelCase__ : Optional[Any]=None , lowerCAmelCase__ : str="gelu" , lowerCAmelCase__ : Optional[int]=0.02 , lowerCAmelCase__ : Dict=1e-1_2 , lowerCAmelCase__ : str=0.0 , lowerCAmelCase__ : str=2_2_4 , lowerCAmelCase__ : int=None , lowerCAmelCase__ : List[Any]=None , **lowerCAmelCase__ : List[Any] , ) -> List[Any]: super().__init__(**lowerCAmelCase__ ) UpperCAmelCase = num_channels UpperCAmelCase = patch_size UpperCAmelCase = num_stages UpperCAmelCase = [9_6, 1_9_2, 3_8_4, 7_6_8] if hidden_sizes is None else hidden_sizes UpperCAmelCase = [3, 3, 9, 3] if depths is None else depths UpperCAmelCase = hidden_act UpperCAmelCase = initializer_range UpperCAmelCase = layer_norm_eps UpperCAmelCase = drop_path_rate UpperCAmelCase = image_size UpperCAmelCase = ["stem"] + [f"stage{idx}" for idx in range(1 , len(self.depths ) + 1 )] UpperCAmelCase , UpperCAmelCase = get_aligned_output_features_output_indices( out_features=lowerCAmelCase__ , out_indices=lowerCAmelCase__ , stage_names=self.stage_names )
1
0
'''simple docstring''' lowerCAmelCase__ = "Input must be a string of 8 numbers plus letter" lowerCAmelCase__ = "TRWAGMYFPDXBNJZSQVHLCKE" def _lowerCAmelCase( __A ): if not isinstance(__A , __A ): UpperCAmelCase = F"Expected string as input, found {type(__A ).__name__}" raise TypeError(__A ) UpperCAmelCase = spanish_id.replace("-" , "" ).upper() if len(__A ) != 9: raise ValueError(__A ) try: UpperCAmelCase = int(spanish_id_clean[0:8] ) UpperCAmelCase = spanish_id_clean[8] except ValueError as ex: raise ValueError(__A ) from ex if letter.isdigit(): raise ValueError(__A ) return letter == LOOKUP_LETTERS[number % 23] if __name__ == "__main__": import doctest doctest.testmod()
719
lowerCAmelCase__ = "\n# Transformers 설치 방법\n! pip install transformers datasets\n# 마지막 릴리스 대신 소스에서 설치하려면, 위 명령을 주석으로 바꾸고 아래 명령을 해제하세요.\n# ! pip install git+https://github.com/huggingface/transformers.git\n" lowerCAmelCase__ = [{"type": "code", "content": INSTALL_CONTENT}] lowerCAmelCase__ = { "{processor_class}": "FakeProcessorClass", "{model_class}": "FakeModelClass", "{object_class}": "FakeObjectClass", }
1
0
def _lowerCAmelCase( __A ): UpperCAmelCase = hex_num.strip() if not hex_num: raise ValueError("No value was passed to the function" ) UpperCAmelCase = hex_num[0] == "-" if is_negative: UpperCAmelCase = hex_num[1:] try: UpperCAmelCase = int(__A , 16 ) except ValueError: raise ValueError("Invalid value was passed to the function" ) UpperCAmelCase = "" while int_num > 0: UpperCAmelCase = str(int_num % 2 ) + bin_str int_num >>= 1 return int(("-" + bin_str) if is_negative else bin_str ) if __name__ == "__main__": import doctest doctest.testmod()
720
import gc import random import unittest import numpy as np import torch from PIL import Image from transformers import XLMRobertaTokenizerFast from diffusers import DDIMScheduler, KandinskyInpaintPipeline, KandinskyPriorPipeline, UNetaDConditionModel, VQModel from diffusers.pipelines.kandinsky.text_encoder import MCLIPConfig, MultilingualCLIP from diffusers.utils import floats_tensor, load_image, load_numpy, slow, torch_device from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu from ..test_pipelines_common import PipelineTesterMixin, assert_mean_pixel_difference enable_full_determinism() class __magic_name__ ( _snake_case , unittest.TestCase ): UpperCAmelCase = KandinskyInpaintPipeline UpperCAmelCase = ["""prompt""", """image_embeds""", """negative_image_embeds""", """image""", """mask_image"""] UpperCAmelCase = [ """prompt""", """negative_prompt""", """image_embeds""", """negative_image_embeds""", """image""", """mask_image""", ] UpperCAmelCase = [ """generator""", """height""", """width""", """latents""", """guidance_scale""", """negative_prompt""", """num_inference_steps""", """return_dict""", """guidance_scale""", """num_images_per_prompt""", """output_type""", """return_dict""", ] UpperCAmelCase = False @property def _UpperCamelCase ( self : Union[str, Any] ) -> Tuple: return 3_2 @property def _UpperCamelCase ( self : int ) -> List[Any]: return 3_2 @property def _UpperCamelCase ( self : List[Any] ) -> List[Any]: return self.time_input_dim @property def _UpperCamelCase ( self : Tuple ) -> Tuple: return self.time_input_dim * 4 @property def _UpperCamelCase ( self : Any ) -> Optional[int]: return 1_0_0 @property def _UpperCamelCase ( self : Optional[int] ) -> Optional[int]: UpperCAmelCase = XLMRobertaTokenizerFast.from_pretrained("YiYiXu/tiny-random-mclip-base" ) return tokenizer @property def _UpperCamelCase ( self : int ) -> Dict: torch.manual_seed(0 ) UpperCAmelCase = MCLIPConfig( numDims=self.cross_attention_dim , transformerDimensions=self.text_embedder_hidden_size , hidden_size=self.text_embedder_hidden_size , intermediate_size=3_7 , num_attention_heads=4 , num_hidden_layers=5 , vocab_size=1_0_0_5 , ) UpperCAmelCase = MultilingualCLIP(lowerCAmelCase__ ) UpperCAmelCase = text_encoder.eval() return text_encoder @property def _UpperCamelCase ( self : Dict ) -> Optional[int]: torch.manual_seed(0 ) UpperCAmelCase = { "in_channels": 9, # Out channels is double in channels because predicts mean and variance "out_channels": 8, "addition_embed_type": "text_image", "down_block_types": ("ResnetDownsampleBlock2D", "SimpleCrossAttnDownBlock2D"), "up_block_types": ("SimpleCrossAttnUpBlock2D", "ResnetUpsampleBlock2D"), "mid_block_type": "UNetMidBlock2DSimpleCrossAttn", "block_out_channels": (self.block_out_channels_a, self.block_out_channels_a * 2), "layers_per_block": 1, "encoder_hid_dim": self.text_embedder_hidden_size, "encoder_hid_dim_type": "text_image_proj", "cross_attention_dim": self.cross_attention_dim, "attention_head_dim": 4, "resnet_time_scale_shift": "scale_shift", "class_embed_type": None, } UpperCAmelCase = UNetaDConditionModel(**lowerCAmelCase__ ) return model @property def _UpperCamelCase ( self : str ) -> Optional[Any]: return { "block_out_channels": [3_2, 6_4], "down_block_types": ["DownEncoderBlock2D", "AttnDownEncoderBlock2D"], "in_channels": 3, "latent_channels": 4, "layers_per_block": 1, "norm_num_groups": 8, "norm_type": "spatial", "num_vq_embeddings": 1_2, "out_channels": 3, "up_block_types": [ "AttnUpDecoderBlock2D", "UpDecoderBlock2D", ], "vq_embed_dim": 4, } @property def _UpperCamelCase ( self : Dict ) -> List[Any]: torch.manual_seed(0 ) UpperCAmelCase = VQModel(**self.dummy_movq_kwargs ) return model def _UpperCamelCase ( self : Tuple ) -> Any: UpperCAmelCase = self.dummy_text_encoder UpperCAmelCase = self.dummy_tokenizer UpperCAmelCase = self.dummy_unet UpperCAmelCase = self.dummy_movq UpperCAmelCase = DDIMScheduler( num_train_timesteps=1_0_0_0 , beta_schedule="linear" , beta_start=0.00_085 , beta_end=0.012 , clip_sample=lowerCAmelCase__ , set_alpha_to_one=lowerCAmelCase__ , steps_offset=1 , prediction_type="epsilon" , thresholding=lowerCAmelCase__ , ) UpperCAmelCase = { "text_encoder": text_encoder, "tokenizer": tokenizer, "unet": unet, "scheduler": scheduler, "movq": movq, } return components def _UpperCamelCase ( self : Union[str, Any] , lowerCAmelCase__ : Any , lowerCAmelCase__ : Tuple=0 ) -> str: UpperCAmelCase = floats_tensor((1, self.cross_attention_dim) , rng=random.Random(lowerCAmelCase__ ) ).to(lowerCAmelCase__ ) UpperCAmelCase = floats_tensor((1, self.cross_attention_dim) , rng=random.Random(seed + 1 ) ).to(lowerCAmelCase__ ) # create init_image UpperCAmelCase = floats_tensor((1, 3, 6_4, 6_4) , rng=random.Random(lowerCAmelCase__ ) ).to(lowerCAmelCase__ ) UpperCAmelCase = image.cpu().permute(0 , 2 , 3 , 1 )[0] UpperCAmelCase = Image.fromarray(np.uinta(lowerCAmelCase__ ) ).convert("RGB" ).resize((2_5_6, 2_5_6) ) # create mask UpperCAmelCase = np.ones((6_4, 6_4) , dtype=np.floataa ) UpperCAmelCase = 0 if str(lowerCAmelCase__ ).startswith("mps" ): UpperCAmelCase = torch.manual_seed(lowerCAmelCase__ ) else: UpperCAmelCase = torch.Generator(device=lowerCAmelCase__ ).manual_seed(lowerCAmelCase__ ) UpperCAmelCase = { "prompt": "horse", "image": init_image, "mask_image": mask, "image_embeds": image_embeds, "negative_image_embeds": negative_image_embeds, "generator": generator, "height": 6_4, "width": 6_4, "num_inference_steps": 2, "guidance_scale": 4.0, "output_type": "np", } return inputs def _UpperCamelCase ( self : Dict ) -> List[str]: UpperCAmelCase = "cpu" UpperCAmelCase = self.get_dummy_components() UpperCAmelCase = self.pipeline_class(**lowerCAmelCase__ ) UpperCAmelCase = pipe.to(lowerCAmelCase__ ) pipe.set_progress_bar_config(disable=lowerCAmelCase__ ) UpperCAmelCase = pipe(**self.get_dummy_inputs(lowerCAmelCase__ ) ) UpperCAmelCase = output.images UpperCAmelCase = pipe( **self.get_dummy_inputs(lowerCAmelCase__ ) , return_dict=lowerCAmelCase__ , )[0] UpperCAmelCase = image[0, -3:, -3:, -1] UpperCAmelCase = image_from_tuple[0, -3:, -3:, -1] print(f"image.shape {image.shape}" ) assert image.shape == (1, 6_4, 6_4, 3) UpperCAmelCase = np.array( [0.8_326_919, 0.73_790_467, 0.20_918_581, 0.9_309_612, 0.5_511_791, 0.43_713_328, 0.5_513_321, 0.49_922_934, 0.59_497_786] ) assert ( np.abs(image_slice.flatten() - expected_slice ).max() < 1e-2 ), f" expected_slice {expected_slice}, but got {image_slice.flatten()}" assert ( np.abs(image_from_tuple_slice.flatten() - expected_slice ).max() < 1e-2 ), f" expected_slice {expected_slice}, but got {image_from_tuple_slice.flatten()}" def _UpperCamelCase ( self : str ) -> Tuple: super().test_inference_batch_single_identical(expected_max_diff=3e-3 ) @slow @require_torch_gpu class __magic_name__ ( unittest.TestCase ): def _UpperCamelCase ( self : str ) -> str: # clean up the VRAM after each test super().tearDown() gc.collect() torch.cuda.empty_cache() def _UpperCamelCase ( self : Tuple ) -> int: UpperCAmelCase = load_numpy( "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main" "/kandinsky/kandinsky_inpaint_cat_with_hat_fp16.npy" ) UpperCAmelCase = load_image( "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main" "/kandinsky/cat.png" ) UpperCAmelCase = np.ones((7_6_8, 7_6_8) , dtype=np.floataa ) UpperCAmelCase = 0 UpperCAmelCase = "a hat" UpperCAmelCase = KandinskyPriorPipeline.from_pretrained( "kandinsky-community/kandinsky-2-1-prior" , torch_dtype=torch.floataa ) pipe_prior.to(lowerCAmelCase__ ) UpperCAmelCase = KandinskyInpaintPipeline.from_pretrained( "kandinsky-community/kandinsky-2-1-inpaint" , torch_dtype=torch.floataa ) UpperCAmelCase = pipeline.to(lowerCAmelCase__ ) pipeline.set_progress_bar_config(disable=lowerCAmelCase__ ) UpperCAmelCase = torch.Generator(device="cpu" ).manual_seed(0 ) UpperCAmelCase , UpperCAmelCase = pipe_prior( lowerCAmelCase__ , generator=lowerCAmelCase__ , num_inference_steps=5 , negative_prompt="" , ).to_tuple() UpperCAmelCase = pipeline( lowerCAmelCase__ , image=lowerCAmelCase__ , mask_image=lowerCAmelCase__ , image_embeds=lowerCAmelCase__ , negative_image_embeds=lowerCAmelCase__ , generator=lowerCAmelCase__ , num_inference_steps=1_0_0 , height=7_6_8 , width=7_6_8 , output_type="np" , ) UpperCAmelCase = output.images[0] assert image.shape == (7_6_8, 7_6_8, 3) assert_mean_pixel_difference(lowerCAmelCase__ , lowerCAmelCase__ )
1
0
from .dependency_versions_table import deps from .utils.versions import require_version, require_version_core # define which module versions we always want to check at run time # (usually the ones defined in `install_requires` in setup.py) # # order specific notes: # - tqdm must be checked before tokenizers lowerCAmelCase__ = [ "python", "tqdm", "regex", "requests", "packaging", "filelock", "numpy", "tokenizers", "huggingface-hub", "safetensors", "accelerate", "pyyaml", ] for pkg in pkgs_to_check_at_runtime: if pkg in deps: if pkg == "tokenizers": # must be loaded here, or else tqdm check may fail from .utils import is_tokenizers_available if not is_tokenizers_available(): continue # not required, check version only if installed elif pkg == "accelerate": # must be loaded here, or else tqdm check may fail from .utils import is_accelerate_available # Maybe switch to is_torch_available in the future here so that Accelerate is hard dep of # Transformers with PyTorch if not is_accelerate_available(): continue # not required, check version only if installed require_version_core(deps[pkg]) else: raise ValueError(f"can't find {pkg} in {deps.keys()}, check dependency_versions_table.py") def _lowerCAmelCase( __A , __A=None ): require_version(deps[pkg] , __A )
721
def _lowerCAmelCase( __A , __A ): return (pointa[0] - pointa[0]) ** 2 + (pointa[1] - pointa[1]) ** 2 def _lowerCAmelCase( __A , __A=0 ): return sorted(__A , key=lambda __A : x[column] ) def _lowerCAmelCase( __A , __A , __A=float("inf" ) ): for i in range(points_counts - 1 ): for j in range(i + 1 , __A ): UpperCAmelCase = euclidean_distance_sqr(points[i] , points[j] ) if current_dis < min_dis: UpperCAmelCase = current_dis return min_dis def _lowerCAmelCase( __A , __A , __A=float("inf" ) ): for i in range(min(6 , points_counts - 1 ) , __A ): for j in range(max(0 , i - 6 ) , __A ): UpperCAmelCase = euclidean_distance_sqr(points[i] , points[j] ) if current_dis < min_dis: UpperCAmelCase = current_dis return min_dis def _lowerCAmelCase( __A , __A , __A ): # base case if points_counts <= 3: return dis_between_closest_pair(__A , __A ) # recursion UpperCAmelCase = points_counts // 2 UpperCAmelCase = closest_pair_of_points_sqr( __A , points_sorted_on_y[:mid] , __A ) UpperCAmelCase = closest_pair_of_points_sqr( __A , points_sorted_on_y[mid:] , points_counts - mid ) UpperCAmelCase = min(__A , __A ) UpperCAmelCase = [] for point in points_sorted_on_x: if abs(point[0] - points_sorted_on_x[mid][0] ) < closest_pair_dis: cross_strip.append(__A ) UpperCAmelCase = dis_between_closest_in_strip( __A , len(__A ) , __A ) return min(__A , __A ) def _lowerCAmelCase( __A , __A ): UpperCAmelCase = column_based_sort(__A , column=0 ) UpperCAmelCase = column_based_sort(__A , column=1 ) return ( closest_pair_of_points_sqr( __A , __A , __A ) ) ** 0.5 if __name__ == "__main__": lowerCAmelCase__ = [(2, 3), (12, 30), (40, 50), (5, 1), (12, 10), (3, 4)] print("Distance:", closest_pair_of_points(points, len(points)))
1
0
import os import unittest from transformers.models.cpmant.tokenization_cpmant import VOCAB_FILES_NAMES, CpmAntTokenizer from transformers.testing_utils import require_jieba, tooslow from ...test_tokenization_common import TokenizerTesterMixin @require_jieba class __magic_name__ ( _snake_case , unittest.TestCase ): UpperCAmelCase = CpmAntTokenizer UpperCAmelCase = False def _UpperCamelCase ( self : Optional[int] ) -> Dict: super().setUp() UpperCAmelCase = [ "<d>", "</d>", "<s>", "</s>", "</_>", "<unk>", "<pad>", "</n>", "我", "是", "C", "P", "M", "A", "n", "t", ] UpperCAmelCase = 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] ) ) @tooslow def _UpperCamelCase ( self : List[Any] ) -> Optional[Any]: UpperCAmelCase = CpmAntTokenizer.from_pretrained("openbmb/cpm-ant-10b" ) UpperCAmelCase = "今天天气真好!" UpperCAmelCase = ["今天", "天气", "真", "好", "!"] UpperCAmelCase = tokenizer.tokenize(lowerCAmelCase__ ) self.assertListEqual(lowerCAmelCase__ , lowerCAmelCase__ ) UpperCAmelCase = "今天天气真好!" UpperCAmelCase = [tokenizer.bos_token] + tokens UpperCAmelCase = [6, 9_8_0_2, 1_4_9_6_2, 2_0_8_2, 8_3_1, 2_4_4] self.assertListEqual(tokenizer.convert_tokens_to_ids(lowerCAmelCase__ ) , lowerCAmelCase__ ) UpperCAmelCase = tokenizer.decode(lowerCAmelCase__ ) self.assertEqual(lowerCAmelCase__ , lowerCAmelCase__ )
700
import copy import os import cva import numpy as np from matplotlib import pyplot as plt class __magic_name__ : def __init__( self : Optional[int] ) -> Optional[Any]: UpperCAmelCase = "" UpperCAmelCase = "" UpperCAmelCase = [] UpperCAmelCase = 0 UpperCAmelCase = 2_5_6 UpperCAmelCase = 0 UpperCAmelCase = 0 UpperCAmelCase = 0 UpperCAmelCase = 0 def _UpperCamelCase ( self : Any , lowerCAmelCase__ : Optional[Any] ) -> List[str]: UpperCAmelCase = cva.imread(lowerCAmelCase__ , 0 ) UpperCAmelCase = copy.deepcopy(self.img ) UpperCAmelCase , UpperCAmelCase , UpperCAmelCase = plt.hist(self.img.ravel() , 2_5_6 , [0, 2_5_6] , label="x" ) UpperCAmelCase = np.sum(lowerCAmelCase__ ) for i in range(len(lowerCAmelCase__ ) ): UpperCAmelCase = x[i] / self.k self.sk += prk UpperCAmelCase = (self.L - 1) * self.sk if self.rem != 0: UpperCAmelCase = int(last % last ) UpperCAmelCase = int(last + 1 if self.rem >= 0.5 else last ) self.last_list.append(lowerCAmelCase__ ) UpperCAmelCase = int(np.ma.count(self.img ) / self.img[1].size ) UpperCAmelCase = self.img[1].size for i in range(self.number_of_cols ): for j in range(self.number_of_rows ): UpperCAmelCase = self.img[j][i] if num != self.last_list[num]: UpperCAmelCase = self.last_list[num] cva.imwrite("output_data/output.jpg" , self.img ) def _UpperCamelCase ( self : str ) -> int: plt.hist(self.img.ravel() , 2_5_6 , [0, 2_5_6] ) def _UpperCamelCase ( self : Dict ) -> Optional[Any]: cva.imshow("Output-Image" , self.img ) cva.imshow("Input-Image" , self.original_image ) cva.waitKey(5_0_0_0 ) cva.destroyAllWindows() if __name__ == "__main__": lowerCAmelCase__ = os.path.join(os.path.basename(__file__), "image_data/input.jpg") lowerCAmelCase__ = ConstantStretch() stretcher.stretch(file_path) stretcher.plot_histogram() stretcher.show_image()
1
0
from __future__ import annotations import time from math import sqrt # 1 for manhattan, 0 for euclidean lowerCAmelCase__ = 0 lowerCAmelCase__ = [ [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], ] lowerCAmelCase__ = [[-1, 0], [0, -1], [1, 0], [0, 1]] # up, left, down, right lowerCAmelCase__ = tuple[int, int] class __magic_name__ : def __init__( self : Union[str, Any] , lowerCAmelCase__ : int , lowerCAmelCase__ : int , lowerCAmelCase__ : int , lowerCAmelCase__ : int , lowerCAmelCase__ : int , lowerCAmelCase__ : Node | None , ) -> None: UpperCAmelCase = pos_x UpperCAmelCase = pos_y UpperCAmelCase = (pos_y, pos_x) UpperCAmelCase = goal_x UpperCAmelCase = goal_y UpperCAmelCase = g_cost UpperCAmelCase = parent UpperCAmelCase = self.calculate_heuristic() UpperCAmelCase = self.g_cost + self.h_cost def _UpperCamelCase ( self : Union[str, Any] ) -> float: UpperCAmelCase = self.pos_x - self.goal_x UpperCAmelCase = 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: return self.f_cost < other.f_cost class __magic_name__ : def __init__( self : List[Any] , lowerCAmelCase__ : TPosition , lowerCAmelCase__ : TPosition ) -> Dict: UpperCAmelCase = Node(start[1] , start[0] , goal[1] , goal[0] , 0 , lowerCAmelCase__ ) UpperCAmelCase = Node(goal[1] , goal[0] , goal[1] , goal[0] , 9_9_9_9_9 , lowerCAmelCase__ ) UpperCAmelCase = [self.start] UpperCAmelCase = [] UpperCAmelCase = False def _UpperCamelCase ( self : Any ) -> list[TPosition]: while self.open_nodes: # Open Nodes are sorted using __lt__ self.open_nodes.sort() UpperCAmelCase = self.open_nodes.pop(0 ) if current_node.pos == self.target.pos: return self.retrace_path(lowerCAmelCase__ ) self.closed_nodes.append(lowerCAmelCase__ ) UpperCAmelCase = 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 UpperCAmelCase = 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 _UpperCamelCase ( self : Tuple , lowerCAmelCase__ : Node ) -> list[Node]: UpperCAmelCase = [] for action in delta: UpperCAmelCase = parent.pos_x + action[1] UpperCAmelCase = 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 _UpperCamelCase ( self : int , lowerCAmelCase__ : Node | None ) -> list[TPosition]: UpperCAmelCase = node UpperCAmelCase = [] while current_node is not None: path.append((current_node.pos_y, current_node.pos_x) ) UpperCAmelCase = current_node.parent path.reverse() return path class __magic_name__ : def __init__( self : List[Any] , lowerCAmelCase__ : TPosition , lowerCAmelCase__ : TPosition ) -> None: UpperCAmelCase = AStar(lowerCAmelCase__ , lowerCAmelCase__ ) UpperCAmelCase = AStar(lowerCAmelCase__ , lowerCAmelCase__ ) UpperCAmelCase = False def _UpperCamelCase ( self : Union[str, Any] ) -> list[TPosition]: while self.fwd_astar.open_nodes or self.bwd_astar.open_nodes: self.fwd_astar.open_nodes.sort() self.bwd_astar.open_nodes.sort() UpperCAmelCase = self.fwd_astar.open_nodes.pop(0 ) UpperCAmelCase = 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__ ) UpperCAmelCase = current_bwd_node UpperCAmelCase = current_fwd_node UpperCAmelCase = { 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 UpperCAmelCase = 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 _UpperCamelCase ( self : Optional[int] , lowerCAmelCase__ : Node , lowerCAmelCase__ : Node ) -> list[TPosition]: UpperCAmelCase = self.fwd_astar.retrace_path(lowerCAmelCase__ ) UpperCAmelCase = self.bwd_astar.retrace_path(lowerCAmelCase__ ) bwd_path.pop() bwd_path.reverse() UpperCAmelCase = fwd_path + bwd_path return path if __name__ == "__main__": # all coordinates are given in format [y,x] lowerCAmelCase__ = (0, 0) lowerCAmelCase__ = (len(grid) - 1, len(grid[0]) - 1) for elem in grid: print(elem) lowerCAmelCase__ = time.time() lowerCAmelCase__ = AStar(init, goal) lowerCAmelCase__ = a_star.search() lowerCAmelCase__ = time.time() - start_time print(f"AStar execution time = {end_time:f} seconds") lowerCAmelCase__ = time.time() lowerCAmelCase__ = BidirectionalAStar(init, goal) lowerCAmelCase__ = time.time() - bd_start_time print(f"BidirectionalAStar execution time = {bd_end_time:f} seconds")
701
import json import os import unittest from transformers import BatchEncoding, LEDTokenizer, LEDTokenizerFast from transformers.models.led.tokenization_led import VOCAB_FILES_NAMES from transformers.testing_utils import require_tokenizers, require_torch from transformers.utils import cached_property from ...test_tokenization_common import TokenizerTesterMixin @require_tokenizers class __magic_name__ ( _snake_case , unittest.TestCase ): UpperCAmelCase = LEDTokenizer UpperCAmelCase = LEDTokenizerFast UpperCAmelCase = True def _UpperCamelCase ( self : Tuple ) -> Union[str, Any]: super().setUp() UpperCAmelCase = [ "l", "o", "w", "e", "r", "s", "t", "i", "d", "n", "\u0120", "\u0120l", "\u0120n", "\u0120lo", "\u0120low", "er", "\u0120lowest", "\u0120newer", "\u0120wider", "<unk>", ] UpperCAmelCase = dict(zip(lowerCAmelCase__ , range(len(lowerCAmelCase__ ) ) ) ) UpperCAmelCase = ["#version: 0.2", "\u0120 l", "\u0120l o", "\u0120lo w", "e r", ""] UpperCAmelCase = {"unk_token": "<unk>"} UpperCAmelCase = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES["vocab_file"] ) UpperCAmelCase = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES["merges_file"] ) with open(self.vocab_file , "w" , encoding="utf-8" ) as fp: fp.write(json.dumps(lowerCAmelCase__ ) + "\n" ) with open(self.merges_file , "w" , encoding="utf-8" ) as fp: fp.write("\n".join(lowerCAmelCase__ ) ) def _UpperCamelCase ( self : Union[str, Any] , **lowerCAmelCase__ : Optional[int] ) -> Optional[int]: kwargs.update(self.special_tokens_map ) return self.tokenizer_class.from_pretrained(self.tmpdirname , **lowerCAmelCase__ ) def _UpperCamelCase ( self : str , **lowerCAmelCase__ : str ) -> Optional[int]: kwargs.update(self.special_tokens_map ) return self.rust_tokenizer_class.from_pretrained(self.tmpdirname , **lowerCAmelCase__ ) def _UpperCamelCase ( self : List[str] , lowerCAmelCase__ : List[Any] ) -> List[Any]: return "lower newer", "lower newer" @cached_property def _UpperCamelCase ( self : Dict ) -> str: return LEDTokenizer.from_pretrained("allenai/led-base-16384" ) @cached_property def _UpperCamelCase ( self : int ) -> Tuple: return LEDTokenizerFast.from_pretrained("allenai/led-base-16384" ) @require_torch def _UpperCamelCase ( self : Tuple ) -> List[str]: UpperCAmelCase = ["A long paragraph for summarization.", "Another paragraph for summarization."] UpperCAmelCase = [0, 2_5_0, 2_5_1, 1_7_8_1_8, 1_3, 3_9_1_8_6, 1_9_3_8, 4, 2] for tokenizer in [self.default_tokenizer, self.default_tokenizer_fast]: UpperCAmelCase = tokenizer(lowerCAmelCase__ , max_length=len(lowerCAmelCase__ ) , padding=lowerCAmelCase__ , return_tensors="pt" ) self.assertIsInstance(lowerCAmelCase__ , lowerCAmelCase__ ) self.assertEqual((2, 9) , batch.input_ids.shape ) self.assertEqual((2, 9) , batch.attention_mask.shape ) UpperCAmelCase = batch.input_ids.tolist()[0] self.assertListEqual(lowerCAmelCase__ , lowerCAmelCase__ ) @require_torch def _UpperCamelCase ( self : Union[str, Any] ) -> List[Any]: UpperCAmelCase = ["A long paragraph for summarization.", "Another paragraph for summarization."] for tokenizer in [self.default_tokenizer, self.default_tokenizer_fast]: UpperCAmelCase = tokenizer(lowerCAmelCase__ , padding=lowerCAmelCase__ , return_tensors="pt" ) self.assertIn("input_ids" , lowerCAmelCase__ ) self.assertIn("attention_mask" , lowerCAmelCase__ ) self.assertNotIn("labels" , lowerCAmelCase__ ) self.assertNotIn("decoder_attention_mask" , lowerCAmelCase__ ) @require_torch def _UpperCamelCase ( self : int ) -> int: UpperCAmelCase = [ "Summary of the text.", "Another summary.", ] for tokenizer in [self.default_tokenizer, self.default_tokenizer_fast]: UpperCAmelCase = tokenizer(text_target=lowerCAmelCase__ , max_length=3_2 , padding="max_length" , return_tensors="pt" ) self.assertEqual(3_2 , targets["input_ids"].shape[1] ) @require_torch def _UpperCamelCase ( self : Any ) -> int: for tokenizer in [self.default_tokenizer, self.default_tokenizer_fast]: UpperCAmelCase = tokenizer( ["I am a small frog" * 1_0_2_4, "I am a small frog"] , padding=lowerCAmelCase__ , truncation=lowerCAmelCase__ , return_tensors="pt" ) self.assertIsInstance(lowerCAmelCase__ , lowerCAmelCase__ ) self.assertEqual(batch.input_ids.shape , (2, 5_1_2_2) ) @require_torch def _UpperCamelCase ( self : Dict ) -> Tuple: UpperCAmelCase = ["A long paragraph for summarization."] UpperCAmelCase = [ "Summary of the text.", ] for tokenizer in [self.default_tokenizer, self.default_tokenizer_fast]: UpperCAmelCase = tokenizer(lowerCAmelCase__ , return_tensors="pt" ) UpperCAmelCase = tokenizer(text_target=lowerCAmelCase__ , return_tensors="pt" ) UpperCAmelCase = inputs["input_ids"] UpperCAmelCase = targets["input_ids"] self.assertTrue((input_ids[:, 0] == tokenizer.bos_token_id).all().item() ) self.assertTrue((labels[:, 0] == tokenizer.bos_token_id).all().item() ) self.assertTrue((input_ids[:, -1] == tokenizer.eos_token_id).all().item() ) self.assertTrue((labels[:, -1] == tokenizer.eos_token_id).all().item() ) @require_torch def _UpperCamelCase ( self : Optional[int] ) -> Optional[Any]: for tokenizer in [self.default_tokenizer, self.default_tokenizer_fast]: UpperCAmelCase = ["Summary of the text.", "Another summary."] UpperCAmelCase = [[0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, -1, -1]] UpperCAmelCase = tokenizer(lowerCAmelCase__ , padding=lowerCAmelCase__ ) UpperCAmelCase = [[0] * len(lowerCAmelCase__ ) for x in encoded_output["input_ids"]] UpperCAmelCase = tokenizer.pad(lowerCAmelCase__ ) self.assertSequenceEqual(outputs["global_attention_mask"] , lowerCAmelCase__ ) def _UpperCamelCase ( self : List[str] ) -> int: pass def _UpperCamelCase ( self : Optional[Any] ) -> Union[str, Any]: for tokenizer, pretrained_name, kwargs in self.tokenizers_list: with self.subTest(f"{tokenizer.__class__.__name__} ({pretrained_name})" ): UpperCAmelCase = self.rust_tokenizer_class.from_pretrained(lowerCAmelCase__ , **lowerCAmelCase__ ) UpperCAmelCase = self.tokenizer_class.from_pretrained(lowerCAmelCase__ , **lowerCAmelCase__ ) UpperCAmelCase = "A, <mask> AllenNLP sentence." UpperCAmelCase = tokenizer_r.encode_plus(lowerCAmelCase__ , add_special_tokens=lowerCAmelCase__ , return_token_type_ids=lowerCAmelCase__ ) UpperCAmelCase = tokenizer_p.encode_plus(lowerCAmelCase__ , add_special_tokens=lowerCAmelCase__ , return_token_type_ids=lowerCAmelCase__ ) self.assertEqual(sum(tokens_r["token_type_ids"] ) , sum(tokens_p["token_type_ids"] ) ) self.assertEqual( sum(tokens_r["attention_mask"] ) / len(tokens_r["attention_mask"] ) , sum(tokens_p["attention_mask"] ) / len(tokens_p["attention_mask"] ) , ) UpperCAmelCase = tokenizer_r.convert_ids_to_tokens(tokens_r["input_ids"] ) UpperCAmelCase = tokenizer_p.convert_ids_to_tokens(tokens_p["input_ids"] ) self.assertSequenceEqual(tokens_p["input_ids"] , [0, 2_5_0, 6, 5_0_2_6_4, 3_8_2_3, 4_8_7, 2_1_9_9_2, 3_6_4_5, 4, 2] ) self.assertSequenceEqual(tokens_r["input_ids"] , [0, 2_5_0, 6, 5_0_2_6_4, 3_8_2_3, 4_8_7, 2_1_9_9_2, 3_6_4_5, 4, 2] ) self.assertSequenceEqual( lowerCAmelCase__ , ["<s>", "A", ",", "<mask>", "ĠAllen", "N", "LP", "Ġsentence", ".", "</s>"] ) self.assertSequenceEqual( lowerCAmelCase__ , ["<s>", "A", ",", "<mask>", "ĠAllen", "N", "LP", "Ġsentence", ".", "</s>"] )
1
0
import argparse import json import os import fairseq import torch from fairseq.data import Dictionary from transformers import ( WavaVecaConfig, WavaVecaCTCTokenizer, WavaVecaFeatureExtractor, WavaVecaForCTC, WavaVecaForPreTraining, WavaVecaProcessor, logging, ) from transformers.models.wavaveca.modeling_wavaveca import WavaVecaForSequenceClassification logging.set_verbosity_info() lowerCAmelCase__ = logging.get_logger(__name__) lowerCAmelCase__ = { "post_extract_proj": "feature_projection.projection", "encoder.pos_conv.0": "encoder.pos_conv_embed.conv", "self_attn.k_proj": "encoder.layers.*.attention.k_proj", "self_attn.v_proj": "encoder.layers.*.attention.v_proj", "self_attn.q_proj": "encoder.layers.*.attention.q_proj", "self_attn.out_proj": "encoder.layers.*.attention.out_proj", "self_attn_layer_norm": "encoder.layers.*.layer_norm", "fc1": "encoder.layers.*.feed_forward.intermediate_dense", "fc2": "encoder.layers.*.feed_forward.output_dense", "final_layer_norm": "encoder.layers.*.final_layer_norm", "encoder.layer_norm": "encoder.layer_norm", "adapter_layer": "encoder.layers.*.adapter_layer", "w2v_model.layer_norm": "feature_projection.layer_norm", "quantizer.weight_proj": "quantizer.weight_proj", "quantizer.vars": "quantizer.codevectors", "project_q": "project_q", "final_proj": "project_hid", "w2v_encoder.proj": "lm_head", "mask_emb": "masked_spec_embed", "pooling_layer.linear": "projector", "pooling_layer.projection": "classifier", } lowerCAmelCase__ = [ "lm_head", "quantizer.weight_proj", "quantizer.codevectors", "project_q", "project_hid", "projector", "classifier", ] def _lowerCAmelCase( __A ): UpperCAmelCase = {} with open(__A , "r" ) as file: for line_number, line in enumerate(__A ): UpperCAmelCase = line.strip() if line: UpperCAmelCase = line.split() UpperCAmelCase = line_number UpperCAmelCase = words[0] UpperCAmelCase = value return result def _lowerCAmelCase( __A , __A , __A , __A , __A ): for attribute in key.split("." ): UpperCAmelCase = getattr(__A , __A ) UpperCAmelCase = None for param_key in PARAM_MAPPING.keys(): if full_name.endswith(__A ): UpperCAmelCase = PARAM_MAPPING[full_name.split("." )[-1]] UpperCAmelCase = "param" if weight_type is not None and weight_type != "param": UpperCAmelCase = getattr(__A , __A ).shape elif weight_type is not None and weight_type == "param": UpperCAmelCase = hf_pointer for attribute in hf_param_name.split("." ): UpperCAmelCase = getattr(__A , __A ) UpperCAmelCase = shape_pointer.shape # let's reduce dimension UpperCAmelCase = value[0] else: UpperCAmelCase = hf_pointer.shape if hf_shape != value.shape: raise ValueError( F"Shape of hf {key + '.' + weight_type if weight_type is not None else ''} is {hf_shape}, but should be" F" {value.shape} for {full_name}" ) if weight_type == "weight": UpperCAmelCase = value elif weight_type == "weight_g": UpperCAmelCase = value elif weight_type == "weight_v": UpperCAmelCase = value elif weight_type == "bias": UpperCAmelCase = value elif weight_type == "param": for attribute in hf_param_name.split("." ): UpperCAmelCase = getattr(__A , __A ) UpperCAmelCase = value else: UpperCAmelCase = value logger.info(F"{key + '.' + weight_type if weight_type is not None else ''} was initialized from {full_name}." ) def _lowerCAmelCase( __A , __A , __A , __A , __A ): UpperCAmelCase = None for param_key in PARAM_MAPPING.keys(): if full_name.endswith(__A ): UpperCAmelCase = PARAM_MAPPING[full_name.split("." )[-1]] UpperCAmelCase = "param" if weight_type is not None and weight_type != "param": UpperCAmelCase = ".".join([key, weight_type] ) elif weight_type is not None and weight_type == "param": UpperCAmelCase = ".".join([key, hf_param_name] ) else: UpperCAmelCase = key UpperCAmelCase = value if "lm_head" in full_key else value[0] lowerCAmelCase__ = { "W_a": "linear_1.weight", "W_b": "linear_2.weight", "b_a": "linear_1.bias", "b_b": "linear_2.bias", "ln_W": "norm.weight", "ln_b": "norm.bias", } def _lowerCAmelCase( __A , __A , __A=None , __A=None ): UpperCAmelCase = False for key, mapped_key in MAPPING.items(): UpperCAmelCase = "wav2vec2." + mapped_key if mapped_key not in TOP_LEVEL_KEYS else mapped_key if key in name or key.split("w2v_model." )[-1] == name.split("." )[0]: UpperCAmelCase = True if "*" in mapped_key: UpperCAmelCase = name.split(__A )[0].split("." )[-2] UpperCAmelCase = mapped_key.replace("*" , __A ) if "weight_g" in name: UpperCAmelCase = "weight_g" elif "weight_v" in name: UpperCAmelCase = "weight_v" elif "bias" in name: UpperCAmelCase = "bias" elif "weight" in name: # TODO: don't match quantizer.weight_proj UpperCAmelCase = "weight" else: UpperCAmelCase = None if hf_dict is not None: rename_dict(__A , __A , __A , __A , __A ) else: set_recursively(__A , __A , __A , __A , __A ) return is_used return is_used def _lowerCAmelCase( __A , __A , __A ): UpperCAmelCase = [] UpperCAmelCase = fairseq_model.state_dict() UpperCAmelCase = hf_model.wavaveca.feature_extractor for name, value in fairseq_dict.items(): UpperCAmelCase = False if "conv_layers" in name: load_conv_layer( __A , __A , __A , __A , hf_model.config.feat_extract_norm == "group" , ) UpperCAmelCase = True else: UpperCAmelCase = load_wavaveca_layer(__A , __A , __A ) if not is_used: unused_weights.append(__A ) logger.warning(F"Unused weights: {unused_weights}" ) def _lowerCAmelCase( __A , __A , __A , __A , __A ): UpperCAmelCase = full_name.split("conv_layers." )[-1] UpperCAmelCase = name.split("." ) UpperCAmelCase = int(items[0] ) UpperCAmelCase = int(items[1] ) if type_id == 0: if "bias" in name: if value.shape != feature_extractor.conv_layers[layer_id].conv.bias.data.shape: raise ValueError( F"{full_name} has size {value.shape}, but" F" {feature_extractor.conv_layers[layer_id].conv.bias.data.shape} was found." ) UpperCAmelCase = value logger.info(F"Feat extract conv layer {layer_id} was initialized from {full_name}." ) elif "weight" in name: if value.shape != feature_extractor.conv_layers[layer_id].conv.weight.data.shape: raise ValueError( F"{full_name} has size {value.shape}, but" F" {feature_extractor.conv_layers[layer_id].conv.weight.data.shape} was found." ) UpperCAmelCase = value logger.info(F"Feat extract conv layer {layer_id} was initialized from {full_name}." ) elif (type_id == 2 and not use_group_norm) or (type_id == 2 and layer_id == 0 and use_group_norm): if "bias" in name: if value.shape != feature_extractor.conv_layers[layer_id].layer_norm.bias.data.shape: raise ValueError( F"{full_name} has size {value.shape}, but" F" {feature_extractor.conv_layers[layer_id].layer_norm.bias.data.shape} was found." ) UpperCAmelCase = value logger.info(F"Feat extract layer norm weight of layer {layer_id} was initialized from {full_name}." ) elif "weight" in name: if value.shape != feature_extractor.conv_layers[layer_id].layer_norm.weight.data.shape: raise ValueError( F"{full_name} has size {value.shape}, but" F" {feature_extractor.conv_layers[layer_id].layer_norm.weight.data.shape} was found." ) UpperCAmelCase = value logger.info(F"Feat extract layer norm weight of layer {layer_id} was initialized from {full_name}." ) else: unused_weights.append(__A ) @torch.no_grad() def _lowerCAmelCase( __A , __A , __A=None , __A=None , __A=True , __A=False ): if config_path is not None: UpperCAmelCase = WavaVecaConfig.from_pretrained(__A ) else: UpperCAmelCase = WavaVecaConfig() if is_seq_class: UpperCAmelCase = read_txt_into_dict(__A ) UpperCAmelCase = idalabel UpperCAmelCase = WavaVecaForSequenceClassification(__A ) UpperCAmelCase = WavaVecaFeatureExtractor( feature_size=1 , sampling_rate=16000 , padding_value=0 , do_normalize=__A , return_attention_mask=__A , ) feature_extractor.save_pretrained(__A ) elif is_finetuned: if dict_path: UpperCAmelCase = Dictionary.load(__A ) # important change bos & pad token id since CTC symbol is <pad> and # not <s> as in fairseq UpperCAmelCase = target_dict.pad_index UpperCAmelCase = target_dict.bos_index UpperCAmelCase = target_dict.eos_index UpperCAmelCase = len(target_dict.symbols ) UpperCAmelCase = os.path.join(__A , "vocab.json" ) if not os.path.isdir(__A ): logger.error("--pytorch_dump_folder_path ({}) should be a directory".format(__A ) ) return os.makedirs(__A , exist_ok=__A ) UpperCAmelCase = target_dict.indices # fairseq has the <pad> and <s> switched UpperCAmelCase = 0 UpperCAmelCase = 1 with open(__A , "w" , encoding="utf-8" ) as vocab_handle: json.dump(__A , __A ) UpperCAmelCase = WavaVecaCTCTokenizer( __A , unk_token=target_dict.unk_word , pad_token=target_dict.pad_word , bos_token=target_dict.bos_word , eos_token=target_dict.eos_word , word_delimiter_token="|" , do_lower_case=__A , ) UpperCAmelCase = True if config.feat_extract_norm == "layer" else False UpperCAmelCase = WavaVecaFeatureExtractor( feature_size=1 , sampling_rate=16000 , padding_value=0 , do_normalize=__A , return_attention_mask=__A , ) UpperCAmelCase = WavaVecaProcessor(feature_extractor=__A , tokenizer=__A ) processor.save_pretrained(__A ) UpperCAmelCase = WavaVecaForCTC(__A ) else: UpperCAmelCase = WavaVecaForPreTraining(__A ) if is_finetuned or is_seq_class: UpperCAmelCase , UpperCAmelCase , UpperCAmelCase = fairseq.checkpoint_utils.load_model_ensemble_and_task( [checkpoint_path] , arg_overrides={"data": "/".join(dict_path.split("/" )[:-1] )} ) else: UpperCAmelCase = argparse.Namespace(task="audio_pretraining" ) UpperCAmelCase = fairseq.tasks.setup_task(__A ) UpperCAmelCase , UpperCAmelCase , UpperCAmelCase = fairseq.checkpoint_utils.load_model_ensemble_and_task([checkpoint_path] , task=__A ) UpperCAmelCase = model[0].eval() recursively_load_weights(__A , __A , not is_finetuned ) hf_wavavec.save_pretrained(__A ) if __name__ == "__main__": lowerCAmelCase__ = argparse.ArgumentParser() parser.add_argument("--pytorch_dump_folder_path", default=None, type=str, help="Path to the output PyTorch model.") parser.add_argument("--checkpoint_path", default=None, type=str, help="Path to fairseq checkpoint") parser.add_argument("--dict_path", default=None, type=str, help="Path to dict of fine-tuned model") parser.add_argument("--config_path", default=None, type=str, help="Path to hf config.json of model to convert") parser.add_argument( "--not_finetuned", action="store_true", help="Whether the model to convert is a fine-tuned model or not" ) parser.add_argument( "--is_seq_class", action="store_true", help="Whether the model to convert is a fine-tuned sequence classification model or not", ) lowerCAmelCase__ = parser.parse_args() lowerCAmelCase__ = not args.not_finetuned and not args.is_seq_class convert_wavaveca_checkpoint( args.checkpoint_path, args.pytorch_dump_folder_path, args.config_path, args.dict_path, is_finetuned, args.is_seq_class, )
702
import numpy as np from nltk.translate import meteor_score import datasets from datasets.config import importlib_metadata, version lowerCAmelCase__ = version.parse(importlib_metadata.version("nltk")) if NLTK_VERSION >= version.Version("3.6.4"): from nltk import word_tokenize lowerCAmelCase__ = "\\n@inproceedings{banarjee2005,\n title = {{METEOR}: An Automatic Metric for {MT} Evaluation with Improved Correlation with Human Judgments},\n author = {Banerjee, Satanjeev and Lavie, Alon},\n booktitle = {Proceedings of the {ACL} Workshop on Intrinsic and Extrinsic Evaluation Measures for Machine Translation and/or Summarization},\n month = jun,\n year = {2005},\n address = {Ann Arbor, Michigan},\n publisher = {Association for Computational Linguistics},\n url = {https://www.aclweb.org/anthology/W05-0909},\n pages = {65--72},\n}\n" lowerCAmelCase__ = "\\nMETEOR, an automatic metric for machine translation evaluation\nthat is based on a generalized concept of unigram matching between the\nmachine-produced translation and human-produced reference translations.\nUnigrams can be matched based on their surface forms, stemmed forms,\nand meanings; furthermore, METEOR can be easily extended to include more\nadvanced matching strategies. Once all generalized unigram matches\nbetween the two strings have been found, METEOR computes a score for\nthis matching using a combination of unigram-precision, unigram-recall, and\na measure of fragmentation that is designed to directly capture how\nwell-ordered the matched words in the machine translation are in relation\nto the reference.\n\nMETEOR gets an R correlation value of 0.347 with human evaluation on the Arabic\ndata and 0.331 on the Chinese data. This is shown to be an improvement on\nusing simply unigram-precision, unigram-recall and their harmonic F1\ncombination.\n" lowerCAmelCase__ = "\nComputes METEOR score of translated segments against one or more references.\nArgs:\n predictions: list of predictions to score. Each prediction\n should be a string with tokens separated by spaces.\n references: list of reference for each prediction. Each\n reference should be a string with tokens separated by spaces.\n alpha: Parameter for controlling relative weights of precision and recall. default: 0.9\n beta: Parameter for controlling shape of penalty as a function of fragmentation. default: 3\n gamma: Relative weight assigned to fragmentation penalty. default: 0.5\nReturns:\n 'meteor': meteor score.\nExamples:\n\n >>> meteor = datasets.load_metric('meteor')\n >>> predictions = [\"It is a guide to action which ensures that the military always obeys the commands of the party\"]\n >>> references = [\"It is a guide to action that ensures that the military will forever heed Party commands\"]\n >>> results = meteor.compute(predictions=predictions, references=references)\n >>> print(round(results[\"meteor\"], 4))\n 0.6944\n" @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class __magic_name__ ( datasets.Metric ): def _UpperCamelCase ( self : int ) -> str: return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { "predictions": datasets.Value("string" , id="sequence" ), "references": datasets.Value("string" , id="sequence" ), } ) , codebase_urls=["https://github.com/nltk/nltk/blob/develop/nltk/translate/meteor_score.py"] , reference_urls=[ "https://www.nltk.org/api/nltk.translate.html#module-nltk.translate.meteor_score", "https://en.wikipedia.org/wiki/METEOR", ] , ) def _UpperCamelCase ( self : Dict , lowerCAmelCase__ : List[Any] ) -> Dict: import nltk nltk.download("wordnet" ) if NLTK_VERSION >= version.Version("3.6.5" ): nltk.download("punkt" ) if NLTK_VERSION >= version.Version("3.6.6" ): nltk.download("omw-1.4" ) def _UpperCamelCase ( self : Union[str, Any] , lowerCAmelCase__ : int , lowerCAmelCase__ : Dict , lowerCAmelCase__ : Any=0.9 , lowerCAmelCase__ : Tuple=3 , lowerCAmelCase__ : Optional[int]=0.5 ) -> Any: if NLTK_VERSION >= version.Version("3.6.5" ): UpperCAmelCase = [ meteor_score.single_meteor_score( word_tokenize(lowerCAmelCase__ ) , word_tokenize(lowerCAmelCase__ ) , alpha=lowerCAmelCase__ , beta=lowerCAmelCase__ , gamma=lowerCAmelCase__ ) for ref, pred in zip(lowerCAmelCase__ , lowerCAmelCase__ ) ] else: UpperCAmelCase = [ meteor_score.single_meteor_score(lowerCAmelCase__ , lowerCAmelCase__ , alpha=lowerCAmelCase__ , beta=lowerCAmelCase__ , gamma=lowerCAmelCase__ ) for ref, pred in zip(lowerCAmelCase__ , lowerCAmelCase__ ) ] return {"meteor": np.mean(lowerCAmelCase__ )}
1
0
def _lowerCAmelCase( __A ): UpperCAmelCase = 0 while num > 0: digit_sum += num % 10 num //= 10 return digit_sum def _lowerCAmelCase( __A = 100 ): UpperCAmelCase = 1 UpperCAmelCase = 2 for i in range(2 , max_n + 1 ): UpperCAmelCase = pre_numerator UpperCAmelCase = 2 * i // 3 if i % 3 == 0 else 1 UpperCAmelCase = cur_numerator UpperCAmelCase = e_cont * pre_numerator + temp return sum_digits(__A ) if __name__ == "__main__": print(f"{solution() = }")
703
from ...configuration_utils import PretrainedConfig from ...utils import logging lowerCAmelCase__ = logging.get_logger(__name__) lowerCAmelCase__ = { "unc-nlp/lxmert-base-uncased": "https://huggingface.co/unc-nlp/lxmert-base-uncased/resolve/main/config.json", } class __magic_name__ ( _snake_case ): UpperCAmelCase = """lxmert""" UpperCAmelCase = {} def __init__( self : int , lowerCAmelCase__ : Any=3_0_5_2_2 , lowerCAmelCase__ : List[str]=7_6_8 , lowerCAmelCase__ : Union[str, Any]=1_2 , lowerCAmelCase__ : List[Any]=9_5_0_0 , lowerCAmelCase__ : Any=1_6_0_0 , lowerCAmelCase__ : Union[str, Any]=4_0_0 , lowerCAmelCase__ : Tuple=3_0_7_2 , lowerCAmelCase__ : Dict="gelu" , lowerCAmelCase__ : Tuple=0.1 , lowerCAmelCase__ : Tuple=0.1 , lowerCAmelCase__ : int=5_1_2 , lowerCAmelCase__ : List[str]=2 , lowerCAmelCase__ : List[str]=0.02 , lowerCAmelCase__ : str=1e-1_2 , lowerCAmelCase__ : str=9 , lowerCAmelCase__ : int=5 , lowerCAmelCase__ : Optional[int]=5 , lowerCAmelCase__ : List[Any]=2_0_4_8 , lowerCAmelCase__ : Any=4 , lowerCAmelCase__ : Dict=6.67 , lowerCAmelCase__ : Any=True , lowerCAmelCase__ : Union[str, Any]=True , lowerCAmelCase__ : Any=True , lowerCAmelCase__ : Tuple=True , lowerCAmelCase__ : Optional[Any]=True , lowerCAmelCase__ : Optional[int]=True , lowerCAmelCase__ : Tuple=True , **lowerCAmelCase__ : List[Any] , ) -> Dict: UpperCAmelCase = vocab_size UpperCAmelCase = hidden_size 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 = num_qa_labels UpperCAmelCase = num_object_labels UpperCAmelCase = num_attr_labels UpperCAmelCase = l_layers UpperCAmelCase = x_layers UpperCAmelCase = r_layers UpperCAmelCase = visual_feat_dim UpperCAmelCase = visual_pos_dim UpperCAmelCase = visual_loss_normalizer UpperCAmelCase = task_matched UpperCAmelCase = task_mask_lm UpperCAmelCase = task_obj_predict UpperCAmelCase = task_qa UpperCAmelCase = visual_obj_loss UpperCAmelCase = visual_attr_loss UpperCAmelCase = visual_feat_loss UpperCAmelCase = {"vision": r_layers, "cross_encoder": x_layers, "language": l_layers} super().__init__(**lowerCAmelCase__ )
1
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__)
704
def _lowerCAmelCase( __A ): UpperCAmelCase = 0 while num > 0: digit_sum += num % 10 num //= 10 return digit_sum def _lowerCAmelCase( __A = 100 ): UpperCAmelCase = 1 UpperCAmelCase = 2 for i in range(2 , max_n + 1 ): UpperCAmelCase = pre_numerator UpperCAmelCase = 2 * i // 3 if i % 3 == 0 else 1 UpperCAmelCase = cur_numerator UpperCAmelCase = e_cont * pre_numerator + temp return sum_digits(__A ) if __name__ == "__main__": print(f"{solution() = }")
1
0
from ...configuration_utils import PretrainedConfig from ...utils import logging from ...utils.backbone_utils import BackboneConfigMixin, get_aligned_output_features_output_indices lowerCAmelCase__ = logging.get_logger(__name__) lowerCAmelCase__ = { "facebook/convnextv2-tiny-1k-224": "https://huggingface.co/facebook/convnextv2-tiny-1k-224/resolve/main/config.json", } class __magic_name__ ( _snake_case , _snake_case ): UpperCAmelCase = """convnextv2""" def __init__( self : Optional[Any] , lowerCAmelCase__ : List[Any]=3 , lowerCAmelCase__ : str=4 , lowerCAmelCase__ : Dict=4 , lowerCAmelCase__ : Optional[Any]=None , lowerCAmelCase__ : Optional[Any]=None , lowerCAmelCase__ : str="gelu" , lowerCAmelCase__ : Optional[int]=0.02 , lowerCAmelCase__ : Dict=1e-1_2 , lowerCAmelCase__ : str=0.0 , lowerCAmelCase__ : str=2_2_4 , lowerCAmelCase__ : int=None , lowerCAmelCase__ : List[Any]=None , **lowerCAmelCase__ : List[Any] , ) -> List[Any]: super().__init__(**lowerCAmelCase__ ) UpperCAmelCase = num_channels UpperCAmelCase = patch_size UpperCAmelCase = num_stages UpperCAmelCase = [9_6, 1_9_2, 3_8_4, 7_6_8] if hidden_sizes is None else hidden_sizes UpperCAmelCase = [3, 3, 9, 3] if depths is None else depths UpperCAmelCase = hidden_act UpperCAmelCase = initializer_range UpperCAmelCase = layer_norm_eps UpperCAmelCase = drop_path_rate UpperCAmelCase = image_size UpperCAmelCase = ["stem"] + [f"stage{idx}" for idx in range(1 , len(self.depths ) + 1 )] UpperCAmelCase , UpperCAmelCase = get_aligned_output_features_output_indices( out_features=lowerCAmelCase__ , out_indices=lowerCAmelCase__ , stage_names=self.stage_names )
705
from . import ( albert, align, altclip, audio_spectrogram_transformer, auto, autoformer, bark, bart, barthez, bartpho, beit, bert, bert_generation, bert_japanese, bertweet, big_bird, bigbird_pegasus, biogpt, bit, blenderbot, blenderbot_small, blip, blip_a, bloom, bridgetower, byta, camembert, canine, chinese_clip, clap, clip, clipseg, codegen, conditional_detr, convbert, convnext, convnextva, cpm, cpmant, ctrl, cvt, dataavec, deberta, deberta_va, decision_transformer, deformable_detr, deit, deprecated, deta, detr, dialogpt, dinat, distilbert, dit, donut, dpr, dpt, efficientformer, efficientnet, electra, encodec, encoder_decoder, ernie, ernie_m, esm, falcon, flaubert, flava, fnet, focalnet, fsmt, funnel, git, glpn, gpta, gpt_bigcode, gpt_neo, gpt_neox, gpt_neox_japanese, gpt_swa, gptj, gptsan_japanese, graphormer, groupvit, herbert, hubert, ibert, imagegpt, informer, instructblip, jukebox, layoutlm, layoutlmva, layoutlmva, layoutxlm, led, levit, lilt, llama, longformer, longta, luke, lxmert, mam_aaa, marian, markuplm, maskaformer, maskformer, mbart, mbartaa, mega, megatron_bert, megatron_gpta, mgp_str, mluke, mobilebert, mobilenet_va, mobilenet_va, mobilevit, mobilevitva, mpnet, mra, mta, musicgen, mvp, nat, nezha, nllb, nllb_moe, nystromformer, oneformer, open_llama, openai, opt, owlvit, pegasus, pegasus_x, perceiver, phobert, pixastruct, plbart, poolformer, prophetnet, qdqbert, rag, realm, reformer, regnet, rembert, resnet, roberta, roberta_prelayernorm, roc_bert, roformer, rwkv, sam, segformer, sew, sew_d, speech_encoder_decoder, speech_to_text, speech_to_text_a, speechta, splinter, squeezebert, swiftformer, swin, swinasr, swinva, switch_transformers, ta, table_transformer, tapas, time_series_transformer, timesformer, timm_backbone, transfo_xl, trocr, tvlt, umta, unispeech, unispeech_sat, upernet, videomae, vilt, vision_encoder_decoder, vision_text_dual_encoder, visual_bert, vit, vit_hybrid, vit_mae, vit_msn, vivit, wavaveca, wavaveca_conformer, wavaveca_phoneme, wavaveca_with_lm, wavlm, whisper, x_clip, xglm, xlm, xlm_prophetnet, xlm_roberta, xlm_roberta_xl, xlnet, xmod, yolos, yoso, )
1
0
import datasets from .evaluate import evaluate lowerCAmelCase__ = "\\n@inproceedings{Rajpurkar2016SQuAD10,\n title={SQuAD: 100, 000+ Questions for Machine Comprehension of Text},\n author={Pranav Rajpurkar and Jian Zhang and Konstantin Lopyrev and Percy Liang},\n booktitle={EMNLP},\n year={2016}\n}\n" lowerCAmelCase__ = "\nThis metric wrap the official scoring script for version 1 of the Stanford Question Answering Dataset (SQuAD).\n\nStanford Question Answering Dataset (SQuAD) is a reading comprehension dataset, consisting of questions posed by\ncrowdworkers on a set of Wikipedia articles, where the answer to every question is a segment of text, or span,\nfrom the corresponding reading passage, or the question might be unanswerable.\n" lowerCAmelCase__ = "\nComputes SQuAD scores (F1 and EM).\nArgs:\n predictions: List of question-answers dictionaries with the following key-values:\n - 'id': id of the question-answer pair as given in the references (see below)\n - 'prediction_text': the text of the answer\n references: List of question-answers dictionaries with the following key-values:\n - 'id': id of the question-answer pair (see above),\n - 'answers': a Dict in the SQuAD dataset format\n {\n 'text': list of possible texts for the answer, as a list of strings\n 'answer_start': list of start positions for the answer, as a list of ints\n }\n Note that answer_start values are not taken into account to compute the metric.\nReturns:\n 'exact_match': Exact match (the normalized answer exactly match the gold answer)\n 'f1': The F-score of predicted tokens versus the gold answer\nExamples:\n\n >>> predictions = [{'prediction_text': '1976', 'id': '56e10a3be3433e1400422b22'}]\n >>> references = [{'answers': {'answer_start': [97], 'text': ['1976']}, 'id': '56e10a3be3433e1400422b22'}]\n >>> squad_metric = datasets.load_metric(\"squad\")\n >>> results = squad_metric.compute(predictions=predictions, references=references)\n >>> print(results)\n {'exact_match': 100.0, 'f1': 100.0}\n" @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class __magic_name__ ( datasets.Metric ): def _UpperCamelCase ( self : Optional[Any] ) -> Optional[Any]: return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { "predictions": {"id": datasets.Value("string" ), "prediction_text": datasets.Value("string" )}, "references": { "id": datasets.Value("string" ), "answers": datasets.features.Sequence( { "text": datasets.Value("string" ), "answer_start": datasets.Value("int32" ), } ), }, } ) , codebase_urls=["https://rajpurkar.github.io/SQuAD-explorer/"] , reference_urls=["https://rajpurkar.github.io/SQuAD-explorer/"] , ) def _UpperCamelCase ( self : Optional[Any] , lowerCAmelCase__ : Optional[int] , lowerCAmelCase__ : Union[str, Any] ) -> Dict: UpperCAmelCase = {prediction["id"]: prediction["prediction_text"] for prediction in predictions} UpperCAmelCase = [ { "paragraphs": [ { "qas": [ { "answers": [{"text": answer_text} for answer_text in ref["answers"]["text"]], "id": ref["id"], } for ref in references ] } ] } ] UpperCAmelCase = evaluate(dataset=lowerCAmelCase__ , predictions=lowerCAmelCase__ ) return score
706
import numpy # List of input, output pairs lowerCAmelCase__ = ( ((5, 2, 3), 15), ((6, 5, 9), 25), ((11, 12, 13), 41), ((1, 1, 1), 8), ((11, 12, 13), 41), ) lowerCAmelCase__ = (((515, 22, 13), 555), ((61, 35, 49), 150)) lowerCAmelCase__ = [2, 4, 1, 5] lowerCAmelCase__ = len(train_data) lowerCAmelCase__ = 0.0_0_9 def _lowerCAmelCase( __A , __A="train" ): return calculate_hypothesis_value(__A , __A ) - output( __A , __A ) def _lowerCAmelCase( __A ): UpperCAmelCase = 0 for i in range(len(__A ) - 1 ): hyp_val += data_input_tuple[i] * parameter_vector[i + 1] hyp_val += parameter_vector[0] return hyp_val def _lowerCAmelCase( __A , __A ): if data_set == "train": return train_data[example_no][1] elif data_set == "test": return test_data[example_no][1] return None def _lowerCAmelCase( __A , __A ): if data_set == "train": return _hypothesis_value(train_data[example_no][0] ) elif data_set == "test": return _hypothesis_value(test_data[example_no][0] ) return None def _lowerCAmelCase( __A , __A=m ): UpperCAmelCase = 0 for i in range(__A ): if index == -1: summation_value += _error(__A ) else: summation_value += _error(__A ) * train_data[i][0][index] return summation_value def _lowerCAmelCase( __A ): UpperCAmelCase = summation_of_cost_derivative(__A , __A ) / m return cost_derivative_value def _lowerCAmelCase( ): global parameter_vector # Tune these values to set a tolerance value for predicted output UpperCAmelCase = 0.000002 UpperCAmelCase = 0 UpperCAmelCase = 0 while True: j += 1 UpperCAmelCase = [0, 0, 0, 0] for i in range(0 , len(__A ) ): UpperCAmelCase = get_cost_derivative(i - 1 ) UpperCAmelCase = ( parameter_vector[i] - LEARNING_RATE * cost_derivative ) if numpy.allclose( __A , __A , atol=__A , rtol=__A , ): break UpperCAmelCase = temp_parameter_vector print(("Number of iterations:", j) ) def _lowerCAmelCase( ): for i in range(len(__A ) ): print(("Actual output value:", output(__A , "test" )) ) print(("Hypothesis output:", calculate_hypothesis_value(__A , "test" )) ) if __name__ == "__main__": run_gradient_descent() print("\nTesting gradient descent for a linear hypothesis function.\n") test_gradient_descent()
1
0
import warnings from ...utils import logging from .image_processing_glpn import GLPNImageProcessor lowerCAmelCase__ = logging.get_logger(__name__) class __magic_name__ ( _snake_case ): def __init__( self : Any , *lowerCAmelCase__ : Optional[int] , **lowerCAmelCase__ : Union[str, Any] ) -> None: warnings.warn( "The class GLPNFeatureExtractor is deprecated and will be removed in version 5 of Transformers. Please" " use GLPNImageProcessor instead." , lowerCAmelCase__ , ) super().__init__(*lowerCAmelCase__ , **lowerCAmelCase__ )
707
def _lowerCAmelCase( __A , __A , __A ): if n == 0: return 1 elif n % 2 == 1: return (binary_exponentiation(__A , n - 1 , __A ) * a) % mod else: UpperCAmelCase = binary_exponentiation(__A , n / 2 , __A ) return (b * b) % mod # a prime number lowerCAmelCase__ = 701 lowerCAmelCase__ = 1000000000 lowerCAmelCase__ = 10 # using binary exponentiation function, O(log(p)): print((a / b) % p == (a * binary_exponentiation(b, p - 2, p)) % p) print((a / b) % p == (a * b ** (p - 2)) % p)
1
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__ = { "bert-base-uncased": "https://huggingface.co/bert-base-uncased/resolve/main/config.json", "bert-large-uncased": "https://huggingface.co/bert-large-uncased/resolve/main/config.json", "bert-base-cased": "https://huggingface.co/bert-base-cased/resolve/main/config.json", "bert-large-cased": "https://huggingface.co/bert-large-cased/resolve/main/config.json", "bert-base-multilingual-uncased": "https://huggingface.co/bert-base-multilingual-uncased/resolve/main/config.json", "bert-base-multilingual-cased": "https://huggingface.co/bert-base-multilingual-cased/resolve/main/config.json", "bert-base-chinese": "https://huggingface.co/bert-base-chinese/resolve/main/config.json", "bert-base-german-cased": "https://huggingface.co/bert-base-german-cased/resolve/main/config.json", "bert-large-uncased-whole-word-masking": ( "https://huggingface.co/bert-large-uncased-whole-word-masking/resolve/main/config.json" ), "bert-large-cased-whole-word-masking": ( "https://huggingface.co/bert-large-cased-whole-word-masking/resolve/main/config.json" ), "bert-large-uncased-whole-word-masking-finetuned-squad": ( "https://huggingface.co/bert-large-uncased-whole-word-masking-finetuned-squad/resolve/main/config.json" ), "bert-large-cased-whole-word-masking-finetuned-squad": ( "https://huggingface.co/bert-large-cased-whole-word-masking-finetuned-squad/resolve/main/config.json" ), "bert-base-cased-finetuned-mrpc": "https://huggingface.co/bert-base-cased-finetuned-mrpc/resolve/main/config.json", "bert-base-german-dbmdz-cased": "https://huggingface.co/bert-base-german-dbmdz-cased/resolve/main/config.json", "bert-base-german-dbmdz-uncased": "https://huggingface.co/bert-base-german-dbmdz-uncased/resolve/main/config.json", "cl-tohoku/bert-base-japanese": "https://huggingface.co/cl-tohoku/bert-base-japanese/resolve/main/config.json", "cl-tohoku/bert-base-japanese-whole-word-masking": ( "https://huggingface.co/cl-tohoku/bert-base-japanese-whole-word-masking/resolve/main/config.json" ), "cl-tohoku/bert-base-japanese-char": ( "https://huggingface.co/cl-tohoku/bert-base-japanese-char/resolve/main/config.json" ), "cl-tohoku/bert-base-japanese-char-whole-word-masking": ( "https://huggingface.co/cl-tohoku/bert-base-japanese-char-whole-word-masking/resolve/main/config.json" ), "TurkuNLP/bert-base-finnish-cased-v1": ( "https://huggingface.co/TurkuNLP/bert-base-finnish-cased-v1/resolve/main/config.json" ), "TurkuNLP/bert-base-finnish-uncased-v1": ( "https://huggingface.co/TurkuNLP/bert-base-finnish-uncased-v1/resolve/main/config.json" ), "wietsedv/bert-base-dutch-cased": "https://huggingface.co/wietsedv/bert-base-dutch-cased/resolve/main/config.json", # See all BERT models at https://huggingface.co/models?filter=bert } class __magic_name__ ( _snake_case ): UpperCAmelCase = """bert""" def __init__( self : Optional[int] , lowerCAmelCase__ : int=3_0_5_2_2 , lowerCAmelCase__ : Union[str, Any]=7_6_8 , lowerCAmelCase__ : Dict=1_2 , lowerCAmelCase__ : Optional[Any]=1_2 , lowerCAmelCase__ : List[Any]=3_0_7_2 , lowerCAmelCase__ : Union[str, Any]="gelu" , lowerCAmelCase__ : str=0.1 , lowerCAmelCase__ : str=0.1 , lowerCAmelCase__ : str=5_1_2 , lowerCAmelCase__ : Union[str, Any]=2 , lowerCAmelCase__ : Dict=0.02 , lowerCAmelCase__ : Optional[Any]=1e-1_2 , lowerCAmelCase__ : str=0 , lowerCAmelCase__ : List[str]="absolute" , lowerCAmelCase__ : int=True , lowerCAmelCase__ : Any=None , **lowerCAmelCase__ : str , ) -> Dict: super().__init__(pad_token_id=lowerCAmelCase__ , **lowerCAmelCase__ ) 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 = position_embedding_type UpperCAmelCase = use_cache UpperCAmelCase = classifier_dropout class __magic_name__ ( _snake_case ): @property def _UpperCamelCase ( self : Optional[int] ) -> Mapping[str, Mapping[int, str]]: 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), ("token_type_ids", dynamic_axis), ] )
708
lowerCAmelCase__ = { "a": "AAAAA", "b": "AAAAB", "c": "AAABA", "d": "AAABB", "e": "AABAA", "f": "AABAB", "g": "AABBA", "h": "AABBB", "i": "ABAAA", "j": "BBBAA", "k": "ABAAB", "l": "ABABA", "m": "ABABB", "n": "ABBAA", "o": "ABBAB", "p": "ABBBA", "q": "ABBBB", "r": "BAAAA", "s": "BAAAB", "t": "BAABA", "u": "BAABB", "v": "BBBAB", "w": "BABAA", "x": "BABAB", "y": "BABBA", "z": "BABBB", " ": " ", } lowerCAmelCase__ = {value: key for key, value in encode_dict.items()} def _lowerCAmelCase( __A ): UpperCAmelCase = "" for letter in word.lower(): if letter.isalpha() or letter == " ": encoded += encode_dict[letter] else: raise Exception("encode() accepts only letters of the alphabet and spaces" ) return encoded def _lowerCAmelCase( __A ): if set(__A ) - {"A", "B", " "} != set(): raise Exception("decode() accepts only 'A', 'B' and spaces" ) UpperCAmelCase = "" for word in coded.split(): while len(__A ) != 0: decoded += decode_dict[word[:5]] UpperCAmelCase = word[5:] decoded += " " return decoded.strip() if __name__ == "__main__": from doctest import testmod testmod()
1
0
from __future__ import annotations def _lowerCAmelCase( __A , __A , __A ): if len(__A ) == 0: raise ValueError("find_max() arg is an empty sequence" ) if ( left >= len(__A ) or left < -len(__A ) or right >= len(__A ) or right < -len(__A ) ): raise IndexError("list index out of range" ) if left == right: return nums[left] UpperCAmelCase = (left + right) >> 1 # the middle UpperCAmelCase = find_max(__A , __A , __A ) # find max in range[left, mid] UpperCAmelCase = find_max(__A , mid + 1 , __A ) # find max in range[mid + 1, right] return left_max if left_max >= right_max else right_max if __name__ == "__main__": import doctest doctest.testmod(verbose=True)
709
from __future__ import annotations import json import requests from bsa import BeautifulSoup from fake_useragent import UserAgent lowerCAmelCase__ = {"UserAgent": UserAgent().random} def _lowerCAmelCase( __A ): UpperCAmelCase = script.contents[0] UpperCAmelCase = json.loads(data[data.find("{\"config\"" ) : -1] ) return info["entry_data"]["ProfilePage"][0]["graphql"]["user"] class __magic_name__ : def __init__( self : Optional[Any] , lowerCAmelCase__ : Optional[int] ) -> Any: UpperCAmelCase = f"https://www.instagram.com/{username}/" UpperCAmelCase = self.get_json() def _UpperCamelCase ( self : List[str] ) -> dict: UpperCAmelCase = requests.get(self.url , headers=lowerCAmelCase__ ).text UpperCAmelCase = BeautifulSoup(lowerCAmelCase__ , "html.parser" ).find_all("script" ) try: return extract_user_profile(scripts[4] ) except (json.decoder.JSONDecodeError, KeyError): return extract_user_profile(scripts[3] ) def __repr__( self : Tuple ) -> str: return f"{self.__class__.__name__}('{self.username}')" def __str__( self : Optional[int] ) -> str: return f"{self.fullname} ({self.username}) is {self.biography}" @property def _UpperCamelCase ( self : Any ) -> str: return self.user_data["username"] @property def _UpperCamelCase ( self : List[Any] ) -> str: return self.user_data["full_name"] @property def _UpperCamelCase ( self : List[str] ) -> str: return self.user_data["biography"] @property def _UpperCamelCase ( self : Optional[int] ) -> str: return self.user_data["business_email"] @property def _UpperCamelCase ( self : str ) -> str: return self.user_data["external_url"] @property def _UpperCamelCase ( self : int ) -> int: return self.user_data["edge_followed_by"]["count"] @property def _UpperCamelCase ( self : List[Any] ) -> int: return self.user_data["edge_follow"]["count"] @property def _UpperCamelCase ( self : List[str] ) -> int: return self.user_data["edge_owner_to_timeline_media"]["count"] @property def _UpperCamelCase ( self : Tuple ) -> str: return self.user_data["profile_pic_url_hd"] @property def _UpperCamelCase ( self : Optional[int] ) -> bool: return self.user_data["is_verified"] @property def _UpperCamelCase ( self : Optional[Any] ) -> bool: return self.user_data["is_private"] def _lowerCAmelCase( __A = "github" ): import os if os.environ.get("CI" ): return # test failing on GitHub Actions UpperCAmelCase = InstagramUser(__A ) assert instagram_user.user_data assert isinstance(instagram_user.user_data , __A ) assert instagram_user.username == username if username != "github": return assert instagram_user.fullname == "GitHub" assert instagram_user.biography == "Built for developers." assert instagram_user.number_of_posts > 150 assert instagram_user.number_of_followers > 120000 assert instagram_user.number_of_followings > 15 assert instagram_user.email == "support@github.com" assert instagram_user.website == "https://github.com/readme" assert instagram_user.profile_picture_url.startswith("https://instagram." ) assert instagram_user.is_verified is True assert instagram_user.is_private is False if __name__ == "__main__": import doctest doctest.testmod() lowerCAmelCase__ = InstagramUser("github") print(instagram_user) print(f"{instagram_user.number_of_posts = }") print(f"{instagram_user.number_of_followers = }") print(f"{instagram_user.number_of_followings = }") print(f"{instagram_user.email = }") print(f"{instagram_user.website = }") print(f"{instagram_user.profile_picture_url = }") print(f"{instagram_user.is_verified = }") print(f"{instagram_user.is_private = }")
1
0
import argparse import logging import os from pathlib import Path from typing import Any, Dict import pytorch_lightning as pl from pytorch_lightning.utilities import rank_zero_info from transformers import ( AdamW, AutoConfig, AutoModel, AutoModelForPreTraining, AutoModelForQuestionAnswering, AutoModelForSeqaSeqLM, AutoModelForSequenceClassification, AutoModelForTokenClassification, AutoModelWithLMHead, AutoTokenizer, PretrainedConfig, PreTrainedTokenizer, ) from transformers.optimization import ( Adafactor, get_cosine_schedule_with_warmup, get_cosine_with_hard_restarts_schedule_with_warmup, get_linear_schedule_with_warmup, get_polynomial_decay_schedule_with_warmup, ) from transformers.utils.versions import require_version lowerCAmelCase__ = logging.getLogger(__name__) require_version("pytorch_lightning>=1.0.4") lowerCAmelCase__ = { "base": AutoModel, "sequence-classification": AutoModelForSequenceClassification, "question-answering": AutoModelForQuestionAnswering, "pretraining": AutoModelForPreTraining, "token-classification": AutoModelForTokenClassification, "language-modeling": AutoModelWithLMHead, "summarization": AutoModelForSeqaSeqLM, "translation": AutoModelForSeqaSeqLM, } # update this and the import above to support new schedulers from transformers.optimization lowerCAmelCase__ = { "linear": get_linear_schedule_with_warmup, "cosine": get_cosine_schedule_with_warmup, "cosine_w_restarts": get_cosine_with_hard_restarts_schedule_with_warmup, "polynomial": get_polynomial_decay_schedule_with_warmup, # '': get_constant_schedule, # not supported for now # '': get_constant_schedule_with_warmup, # not supported for now } lowerCAmelCase__ = sorted(arg_to_scheduler.keys()) lowerCAmelCase__ = "{" + ", ".join(arg_to_scheduler_choices) + "}" class __magic_name__ ( pl.LightningModule ): def __init__( self : int , lowerCAmelCase__ : argparse.Namespace , lowerCAmelCase__ : Optional[Any]=None , lowerCAmelCase__ : int="base" , lowerCAmelCase__ : Dict=None , lowerCAmelCase__ : Tuple=None , lowerCAmelCase__ : Dict=None , **lowerCAmelCase__ : List[str] , ) -> str: super().__init__() # TODO: move to self.save_hyperparameters() # self.save_hyperparameters() # can also expand arguments into trainer signature for easier reading self.save_hyperparameters(lowerCAmelCase__ ) UpperCAmelCase = 0 UpperCAmelCase = Path(self.hparams.output_dir ) UpperCAmelCase = self.hparams.cache_dir if self.hparams.cache_dir else None if config is None: UpperCAmelCase = AutoConfig.from_pretrained( self.hparams.config_name if self.hparams.config_name else self.hparams.model_name_or_path , **({"num_labels": num_labels} if num_labels is not None else {}) , cache_dir=lowerCAmelCase__ , **lowerCAmelCase__ , ) else: UpperCAmelCase = config UpperCAmelCase = ("encoder_layerdrop", "decoder_layerdrop", "dropout", "attention_dropout") for p in extra_model_params: if getattr(self.hparams , lowerCAmelCase__ , lowerCAmelCase__ ): assert hasattr(self.config , lowerCAmelCase__ ), f"model config doesn't have a `{p}` attribute" setattr(self.config , lowerCAmelCase__ , getattr(self.hparams , lowerCAmelCase__ ) ) if tokenizer is None: UpperCAmelCase = AutoTokenizer.from_pretrained( self.hparams.tokenizer_name if self.hparams.tokenizer_name else self.hparams.model_name_or_path , cache_dir=lowerCAmelCase__ , ) else: UpperCAmelCase = tokenizer UpperCAmelCase = MODEL_MODES[mode] if model is None: UpperCAmelCase = self.model_type.from_pretrained( self.hparams.model_name_or_path , from_tf=bool(".ckpt" in self.hparams.model_name_or_path ) , config=self.config , cache_dir=lowerCAmelCase__ , ) else: UpperCAmelCase = model def _UpperCamelCase ( self : int , *lowerCAmelCase__ : List[Any] , **lowerCAmelCase__ : Optional[int] ) -> Tuple: UpperCAmelCase = self.model_type.from_pretrained(*lowerCAmelCase__ , **lowerCAmelCase__ ) def _UpperCamelCase ( self : Dict ) -> List[str]: UpperCAmelCase = arg_to_scheduler[self.hparams.lr_scheduler] UpperCAmelCase = get_schedule_func( self.opt , num_warmup_steps=self.hparams.warmup_steps , num_training_steps=self.total_steps() ) UpperCAmelCase = {"scheduler": scheduler, "interval": "step", "frequency": 1} return scheduler def _UpperCamelCase ( self : Union[str, Any] ) -> Union[str, Any]: UpperCAmelCase = self.model UpperCAmelCase = ["bias", "LayerNorm.weight"] UpperCAmelCase = [ { "params": [ p for n, p in model.named_parameters() if not any(nd in n for nd in no_decay ) ], # check this named paramters "weight_decay": self.hparams.weight_decay, }, { "params": [p for n, p in model.named_parameters() if any(nd in n for nd in no_decay )], "weight_decay": 0.0, }, ] if self.hparams.adafactor: UpperCAmelCase = Adafactor( lowerCAmelCase__ , lr=self.hparams.learning_rate , scale_parameter=lowerCAmelCase__ , relative_step=lowerCAmelCase__ ) else: UpperCAmelCase = AdamW( lowerCAmelCase__ , lr=self.hparams.learning_rate , eps=self.hparams.adam_epsilon ) UpperCAmelCase = optimizer UpperCAmelCase = self.get_lr_scheduler() return [optimizer], [scheduler] def _UpperCamelCase ( self : str , lowerCAmelCase__ : Union[str, Any] , lowerCAmelCase__ : Optional[Any] ) -> Optional[int]: return self.validation_step(lowerCAmelCase__ , lowerCAmelCase__ ) def _UpperCamelCase ( self : List[Any] , lowerCAmelCase__ : Tuple ) -> List[str]: return self.validation_end(lowerCAmelCase__ ) def _UpperCamelCase ( self : Optional[Any] ) -> int: UpperCAmelCase = max(1 , self.hparams.gpus ) # TODO: consider num_tpu_cores UpperCAmelCase = self.hparams.train_batch_size * self.hparams.accumulate_grad_batches * num_devices return (self.dataset_size / effective_batch_size) * self.hparams.max_epochs def _UpperCamelCase ( self : Union[str, Any] , lowerCAmelCase__ : List[str] ) -> Union[str, Any]: if stage == "test": UpperCAmelCase = len(self.test_dataloader().dataset ) else: UpperCAmelCase = self.get_dataloader("train" , self.hparams.train_batch_size , shuffle=lowerCAmelCase__ ) UpperCAmelCase = len(self.train_dataloader().dataset ) def _UpperCamelCase ( self : List[str] , lowerCAmelCase__ : str , lowerCAmelCase__ : int , lowerCAmelCase__ : bool = False ) -> str: raise NotImplementedError("You must implement this for your task" ) def _UpperCamelCase ( self : str ) -> Optional[int]: return self.train_loader def _UpperCamelCase ( self : Union[str, Any] ) -> Optional[Any]: return self.get_dataloader("dev" , self.hparams.eval_batch_size , shuffle=lowerCAmelCase__ ) def _UpperCamelCase ( self : Tuple ) -> List[str]: return self.get_dataloader("test" , self.hparams.eval_batch_size , shuffle=lowerCAmelCase__ ) def _UpperCamelCase ( self : int , lowerCAmelCase__ : Tuple ) -> str: return os.path.join( self.hparams.data_dir , "cached_{}_{}_{}".format( lowerCAmelCase__ , list(filter(lowerCAmelCase__ , self.hparams.model_name_or_path.split("/" ) ) ).pop() , str(self.hparams.max_seq_length ) , ) , ) @pl.utilities.rank_zero_only def _UpperCamelCase ( self : List[str] , lowerCAmelCase__ : Dict[str, Any] ) -> None: UpperCAmelCase = self.output_dir.joinpath("best_tfmr" ) UpperCAmelCase = self.step_count self.model.save_pretrained(lowerCAmelCase__ ) self.tokenizer.save_pretrained(lowerCAmelCase__ ) @staticmethod def _UpperCamelCase ( lowerCAmelCase__ : Union[str, Any] , lowerCAmelCase__ : Optional[Any] ) -> Optional[Any]: parser.add_argument( "--model_name_or_path" , default=lowerCAmelCase__ , type=lowerCAmelCase__ , required=lowerCAmelCase__ , help="Path to pretrained model or model identifier from huggingface.co/models" , ) parser.add_argument( "--config_name" , default="" , type=lowerCAmelCase__ , help="Pretrained config name or path if not the same as model_name" ) parser.add_argument( "--tokenizer_name" , default=lowerCAmelCase__ , type=lowerCAmelCase__ , help="Pretrained tokenizer name or path if not the same as model_name" , ) parser.add_argument( "--cache_dir" , default=str(Path(lowerCAmelCase__ ).parent / "test_run" / "cache" ) , type=lowerCAmelCase__ , help="Where do you want to store the pre-trained models downloaded from huggingface.co" , ) parser.add_argument( "--encoder_layerdrop" , type=lowerCAmelCase__ , help="Encoder layer dropout probability (Optional). Goes into model.config" , ) parser.add_argument( "--decoder_layerdrop" , type=lowerCAmelCase__ , help="Decoder layer dropout probability (Optional). Goes into model.config" , ) parser.add_argument( "--dropout" , type=lowerCAmelCase__ , help="Dropout probability (Optional). Goes into model.config" , ) parser.add_argument( "--attention_dropout" , type=lowerCAmelCase__ , help="Attention dropout probability (Optional). Goes into model.config" , ) parser.add_argument("--learning_rate" , default=5e-5 , type=lowerCAmelCase__ , help="The initial learning rate for Adam." ) parser.add_argument( "--lr_scheduler" , default="linear" , choices=lowerCAmelCase__ , metavar=lowerCAmelCase__ , type=lowerCAmelCase__ , help="Learning rate scheduler" , ) parser.add_argument("--weight_decay" , default=0.0 , type=lowerCAmelCase__ , help="Weight decay if we apply some." ) parser.add_argument("--adam_epsilon" , default=1e-8 , type=lowerCAmelCase__ , help="Epsilon for Adam optimizer." ) parser.add_argument("--warmup_steps" , default=0 , type=lowerCAmelCase__ , help="Linear warmup over warmup_steps." ) parser.add_argument("--num_workers" , default=4 , type=lowerCAmelCase__ , help="kwarg passed to DataLoader" ) parser.add_argument("--num_train_epochs" , dest="max_epochs" , default=3 , type=lowerCAmelCase__ ) parser.add_argument("--train_batch_size" , default=3_2 , type=lowerCAmelCase__ ) parser.add_argument("--eval_batch_size" , default=3_2 , type=lowerCAmelCase__ ) parser.add_argument("--adafactor" , action="store_true" ) class __magic_name__ ( pl.Callback ): def _UpperCamelCase ( self : int , lowerCAmelCase__ : Union[str, Any] , lowerCAmelCase__ : Tuple ) -> Any: if ( trainer.is_global_zero and trainer.global_rank == 0 ): # we initialize the retriever only on master worker with RAY. In new pytorch-lightning accelorators are removed. pl_module.model.rag.retriever.init_retrieval() # better to use hook functions. class __magic_name__ ( pl.Callback ): def _UpperCamelCase ( self : int , lowerCAmelCase__ : List[str] , lowerCAmelCase__ : Optional[int] ) -> Any: # print(pl_module.model.rag) for name, param in pl_module.model.rag.named_parameters(): if param.grad is None: print(lowerCAmelCase__ ) class __magic_name__ ( pl.Callback ): def _UpperCamelCase ( self : Optional[Any] , lowerCAmelCase__ : Dict , lowerCAmelCase__ : Tuple ) -> int: UpperCAmelCase = trainer.lr_schedulers[0]["scheduler"] UpperCAmelCase = {f"lr_group_{i}": lr for i, lr in enumerate(lr_scheduler.get_lr() )} pl_module.logger.log_metrics(lowerCAmelCase__ ) def _UpperCamelCase ( self : List[str] , lowerCAmelCase__ : pl.Trainer , lowerCAmelCase__ : pl.LightningModule ) -> Union[str, Any]: rank_zero_info("***** Validation results *****" ) UpperCAmelCase = trainer.callback_metrics # Log results for key in sorted(lowerCAmelCase__ ): if key not in ["log", "progress_bar"]: rank_zero_info("{} = {}\n".format(lowerCAmelCase__ , str(metrics[key] ) ) ) def _UpperCamelCase ( self : Any , lowerCAmelCase__ : pl.Trainer , lowerCAmelCase__ : pl.LightningModule ) -> Optional[int]: rank_zero_info("***** Test results *****" ) UpperCAmelCase = trainer.callback_metrics # Log and save results to file UpperCAmelCase = os.path.join(pl_module.hparams.output_dir , "test_results.txt" ) with open(lowerCAmelCase__ , "w" ) as writer: for key in sorted(lowerCAmelCase__ ): if key not in ["log", "progress_bar"]: rank_zero_info("{} = {}\n".format(lowerCAmelCase__ , str(metrics[key] ) ) ) writer.write("{} = {}\n".format(lowerCAmelCase__ , str(metrics[key] ) ) ) def _lowerCAmelCase( __A , __A ): # To allow all pl args uncomment the following line # parser = pl.Trainer.add_argparse_args(parser) parser.add_argument( "--output_dir" , default=str(Path(__A ).parent / "test_run" / "model_checkpoints" ) , type=__A , help="The output directory where the model predictions and checkpoints will be written." , ) parser.add_argument( "--fp16" , action="store_true" , help="Whether to use 16-bit (mixed) precision (through NVIDIA apex) instead of 32-bit" , ) parser.add_argument( "--fp16_opt_level" , type=__A , default="O2" , help=( "For fp16: Apex AMP optimization level selected in ['O0', 'O1', 'O2', and 'O3']." "See details at https://nvidia.github.io/apex/amp.html" ) , ) parser.add_argument("--n_tpu_cores" , dest="tpu_cores" , type=__A ) parser.add_argument("--max_grad_norm" , dest="gradient_clip_val" , default=1.0 , type=__A , help="Max gradient norm" ) parser.add_argument("--do_train" , action="store_true" , help="Whether to run training." ) parser.add_argument("--do_predict" , action="store_true" , help="Whether to run predictions on the test set." ) parser.add_argument( "--gradient_accumulation_steps" , dest="accumulate_grad_batches" , type=__A , default=1 , help="Number of updates steps to accumulate before performing a backward/update pass." , ) parser.add_argument("--seed" , type=__A , default=42 , help="random seed for initialization" ) parser.add_argument( "--data_dir" , default=str(Path(__A ).parent / "test_run" / "dummy-train-data" ) , type=__A , help="The input data dir. Should contain the training files for the CoNLL-2003 NER task." , ) def _lowerCAmelCase( __A , __A , __A=None , __A=True , __A=[] , __A=None , __A=None , **__A , ): pl.seed_everything(args.seed ) # init model UpperCAmelCase = Path(model.hparams.output_dir ) odir.mkdir(exist_ok=__A ) # add custom checkpoints if checkpoint_callback is None: UpperCAmelCase = pl.callbacks.ModelCheckpoint( filepath=args.output_dir , prefix="checkpoint" , monitor="val_loss" , mode="min" , save_top_k=1 ) if early_stopping_callback: extra_callbacks.append(__A ) if logging_callback is None: UpperCAmelCase = LoggingCallback() UpperCAmelCase = {} if args.fpaa: UpperCAmelCase = 16 if args.gpus > 1: UpperCAmelCase = "auto" UpperCAmelCase = "ddp" UpperCAmelCase = args.accumulate_grad_batches UpperCAmelCase = None UpperCAmelCase = "auto" UpperCAmelCase = pl.Trainer.from_argparse_args( __A , weights_summary=__A , callbacks=[logging_callback] + extra_callbacks + [InitCallback()] + [checkpoint_callback] , logger=__A , val_check_interval=1 , num_sanity_val_steps=2 , **__A , ) if args.do_train: trainer.fit(__A ) else: print("RAG modeling tests with new set functions successfuly executed!" ) return trainer
710
import unittest import numpy as np def _lowerCAmelCase( __A , __A , __A , __A = None , ): UpperCAmelCase = np.shape(__A ) UpperCAmelCase = np.shape(__A ) UpperCAmelCase = np.shape(__A ) if shape_a[0] != shape_b[0]: UpperCAmelCase = ( "Expected the same number of rows for A and B. " F"Instead found A of size {shape_a} and B of size {shape_b}" ) raise ValueError(__A ) if shape_b[1] != shape_c[1]: UpperCAmelCase = ( "Expected the same number of columns for B and C. " F"Instead found B of size {shape_b} and C of size {shape_c}" ) raise ValueError(__A ) UpperCAmelCase = pseudo_inv if a_inv is None: try: UpperCAmelCase = np.linalg.inv(__A ) except np.linalg.LinAlgError: raise ValueError( "Input matrix A is not invertible. Cannot compute Schur complement." ) return mat_c - mat_b.T @ a_inv @ mat_b class __magic_name__ ( unittest.TestCase ): def _UpperCamelCase ( self : List[str] ) -> None: UpperCAmelCase = np.array([[1, 2, 1], [2, 1, 2], [3, 2, 4]] ) UpperCAmelCase = np.array([[0, 3], [3, 0], [2, 3]] ) UpperCAmelCase = np.array([[2, 1], [6, 3]] ) UpperCAmelCase = schur_complement(lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ ) UpperCAmelCase = np.block([[a, b], [b.T, c]] ) UpperCAmelCase = np.linalg.det(lowerCAmelCase__ ) UpperCAmelCase = np.linalg.det(lowerCAmelCase__ ) UpperCAmelCase = np.linalg.det(lowerCAmelCase__ ) self.assertAlmostEqual(lowerCAmelCase__ , det_a * det_s ) def _UpperCamelCase ( self : str ) -> None: UpperCAmelCase = np.array([[1, 2, 1], [2, 1, 2], [3, 2, 4]] ) UpperCAmelCase = np.array([[0, 3], [3, 0], [2, 3]] ) UpperCAmelCase = np.array([[2, 1], [6, 3]] ) with self.assertRaises(lowerCAmelCase__ ): schur_complement(lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ ) def _UpperCamelCase ( self : Dict ) -> None: UpperCAmelCase = np.array([[1, 2, 1], [2, 1, 2], [3, 2, 4]] ) UpperCAmelCase = np.array([[0, 3], [3, 0], [2, 3]] ) UpperCAmelCase = np.array([[2, 1, 3], [6, 3, 5]] ) with self.assertRaises(lowerCAmelCase__ ): schur_complement(lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ ) if __name__ == "__main__": import doctest doctest.testmod() unittest.main()
1
0
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_tokenizers_available, is_torch_available, is_vision_available, ) lowerCAmelCase__ = { "configuration_perceiver": ["PERCEIVER_PRETRAINED_CONFIG_ARCHIVE_MAP", "PerceiverConfig", "PerceiverOnnxConfig"], "tokenization_perceiver": ["PerceiverTokenizer"], } try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase__ = ["PerceiverFeatureExtractor"] lowerCAmelCase__ = ["PerceiverImageProcessor"] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase__ = [ "PERCEIVER_PRETRAINED_MODEL_ARCHIVE_LIST", "PerceiverForImageClassificationConvProcessing", "PerceiverForImageClassificationFourier", "PerceiverForImageClassificationLearned", "PerceiverForMaskedLM", "PerceiverForMultimodalAutoencoding", "PerceiverForOpticalFlow", "PerceiverForSequenceClassification", "PerceiverLayer", "PerceiverModel", "PerceiverPreTrainedModel", ] if TYPE_CHECKING: from .configuration_perceiver import PERCEIVER_PRETRAINED_CONFIG_ARCHIVE_MAP, PerceiverConfig, PerceiverOnnxConfig from .tokenization_perceiver import PerceiverTokenizer try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .feature_extraction_perceiver import PerceiverFeatureExtractor from .image_processing_perceiver import PerceiverImageProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_perceiver import ( PERCEIVER_PRETRAINED_MODEL_ARCHIVE_LIST, PerceiverForImageClassificationConvProcessing, PerceiverForImageClassificationFourier, PerceiverForImageClassificationLearned, PerceiverForMaskedLM, PerceiverForMultimodalAutoencoding, PerceiverForOpticalFlow, PerceiverForSequenceClassification, PerceiverLayer, PerceiverModel, PerceiverPreTrainedModel, ) else: import sys lowerCAmelCase__ = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
711
import argparse import os import re import numpy as np import PIL import torch from timm import create_model from torch.optim.lr_scheduler import OneCycleLR from torch.utils.data import DataLoader, Dataset from torchvision.transforms import Compose, RandomResizedCrop, Resize, ToTensor from accelerate import Accelerator def _lowerCAmelCase( __A ): UpperCAmelCase = fname.split(os.path.sep )[-1] return re.search(r"^(.*)_\d+\.jpg$" , __A ).groups()[0] class __magic_name__ ( _snake_case ): def __init__( self : Any , lowerCAmelCase__ : int , lowerCAmelCase__ : Union[str, Any]=None , lowerCAmelCase__ : int=None ) -> Optional[Any]: UpperCAmelCase = file_names UpperCAmelCase = image_transform UpperCAmelCase = label_to_id def __len__( self : Tuple ) -> List[str]: return len(self.file_names ) def __getitem__( self : Optional[int] , lowerCAmelCase__ : Tuple ) -> Dict: UpperCAmelCase = self.file_names[idx] UpperCAmelCase = PIL.Image.open(lowerCAmelCase__ ) UpperCAmelCase = raw_image.convert("RGB" ) if self.image_transform is not None: UpperCAmelCase = self.image_transform(lowerCAmelCase__ ) UpperCAmelCase = extract_label(lowerCAmelCase__ ) if self.label_to_id is not None: UpperCAmelCase = self.label_to_id[label] return {"image": image, "label": label} def _lowerCAmelCase( __A , __A ): # Initialize accelerator if args.with_tracking: UpperCAmelCase = Accelerator( cpu=args.cpu , mixed_precision=args.mixed_precision , log_with="all" , project_dir=args.project_dir ) else: UpperCAmelCase = Accelerator(cpu=args.cpu , mixed_precision=args.mixed_precision ) # Sample hyper-parameters for learning rate, batch size, seed and a few other HPs UpperCAmelCase = config["lr"] UpperCAmelCase = int(config["num_epochs"] ) UpperCAmelCase = int(config["seed"] ) UpperCAmelCase = int(config["batch_size"] ) UpperCAmelCase = config["image_size"] if not isinstance(__A , (list, tuple) ): UpperCAmelCase = (image_size, image_size) # Parse out whether we are saving every epoch or after a certain number of batches if hasattr(args.checkpointing_steps , "isdigit" ): if args.checkpointing_steps == "epoch": UpperCAmelCase = args.checkpointing_steps elif args.checkpointing_steps.isdigit(): UpperCAmelCase = int(args.checkpointing_steps ) else: raise ValueError( F"Argument `checkpointing_steps` must be either a number or `epoch`. `{args.checkpointing_steps}` passed." ) else: UpperCAmelCase = None # We need to initialize the trackers we use, and also store our configuration if args.with_tracking: UpperCAmelCase = os.path.split(__A )[-1].split("." )[0] accelerator.init_trackers(__A , __A ) # Grab all the image filenames UpperCAmelCase = [os.path.join(args.data_dir , __A ) for fname in os.listdir(args.data_dir ) if fname.endswith(".jpg" )] # Build the label correspondences UpperCAmelCase = [extract_label(__A ) for fname in file_names] UpperCAmelCase = list(set(__A ) ) id_to_label.sort() UpperCAmelCase = {lbl: i for i, lbl in enumerate(__A )} # Set the seed before splitting the data. np.random.seed(__A ) torch.manual_seed(__A ) torch.cuda.manual_seed_all(__A ) # Split our filenames between train and validation UpperCAmelCase = np.random.permutation(len(__A ) ) UpperCAmelCase = int(0.8 * len(__A ) ) UpperCAmelCase = random_perm[:cut] UpperCAmelCase = random_perm[cut:] # For training we use a simple RandomResizedCrop UpperCAmelCase = Compose([RandomResizedCrop(__A , scale=(0.5, 1.0) ), ToTensor()] ) UpperCAmelCase = PetsDataset( [file_names[i] for i in train_split] , image_transform=__A , label_to_id=__A ) # For evaluation, we use a deterministic Resize UpperCAmelCase = Compose([Resize(__A ), ToTensor()] ) UpperCAmelCase = PetsDataset([file_names[i] for i in eval_split] , image_transform=__A , label_to_id=__A ) # Instantiate dataloaders. UpperCAmelCase = DataLoader(__A , shuffle=__A , batch_size=__A , num_workers=4 ) UpperCAmelCase = DataLoader(__A , shuffle=__A , batch_size=__A , num_workers=4 ) # Instantiate the model (we build the model here so that the seed also control new weights initialization) UpperCAmelCase = create_model("resnet50d" , pretrained=__A , num_classes=len(__A ) ) # We could avoid this line since the accelerator is set with `device_placement=True` (default value). # Note that if you are placing tensors on devices manually, this line absolutely needs to be before the optimizer # creation otherwise training will not work on TPU (`accelerate` will kindly throw an error to make us aware of that). UpperCAmelCase = model.to(accelerator.device ) # Freezing the base model for param in model.parameters(): UpperCAmelCase = False for param in model.get_classifier().parameters(): UpperCAmelCase = True # We normalize the batches of images to be a bit faster. UpperCAmelCase = torch.tensor(model.default_cfg["mean"] )[None, :, None, None].to(accelerator.device ) UpperCAmelCase = torch.tensor(model.default_cfg["std"] )[None, :, None, None].to(accelerator.device ) # Instantiate optimizer UpperCAmelCase = torch.optim.Adam(params=model.parameters() , lr=lr / 25 ) # Instantiate learning rate scheduler UpperCAmelCase = OneCycleLR(optimizer=__A , max_lr=__A , epochs=__A , steps_per_epoch=len(__A ) ) # Prepare everything # There is no specific order to remember, we just need to unpack the objects in the same order we gave them to the # prepare method. UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase = accelerator.prepare( __A , __A , __A , __A , __A ) # We need to keep track of how many total steps we have iterated over UpperCAmelCase = 0 # We also need to keep track of the starting epoch so files are named properly UpperCAmelCase = 0 # Potentially load in the weights and states from a previous save if args.resume_from_checkpoint: if args.resume_from_checkpoint is not None or args.resume_from_checkpoint != "": accelerator.print(F"Resumed from checkpoint: {args.resume_from_checkpoint}" ) accelerator.load_state(args.resume_from_checkpoint ) UpperCAmelCase = os.path.basename(args.resume_from_checkpoint ) else: # Get the most recent checkpoint UpperCAmelCase = [f.name for f in os.scandir(os.getcwd() ) if f.is_dir()] dirs.sort(key=os.path.getctime ) UpperCAmelCase = dirs[-1] # Sorts folders by date modified, most recent checkpoint is the last # Extract `epoch_{i}` or `step_{i}` UpperCAmelCase = os.path.splitext(__A )[0] if "epoch" in training_difference: UpperCAmelCase = int(training_difference.replace("epoch_" , "" ) ) + 1 UpperCAmelCase = None else: UpperCAmelCase = int(training_difference.replace("step_" , "" ) ) UpperCAmelCase = resume_step // len(__A ) resume_step -= starting_epoch * len(__A ) # Now we train the model for epoch in range(__A , __A ): model.train() if args.with_tracking: UpperCAmelCase = 0 if args.resume_from_checkpoint and epoch == starting_epoch and resume_step is not None: # We need to skip steps until we reach the resumed step UpperCAmelCase = accelerator.skip_first_batches(__A , __A ) overall_step += resume_step else: # After the first iteration though, we need to go back to the original dataloader UpperCAmelCase = train_dataloader for batch in active_dataloader: # We could avoid this line since we set the accelerator with `device_placement=True`. UpperCAmelCase = {k: v.to(accelerator.device ) for k, v in batch.items()} UpperCAmelCase = (batch["image"] - mean) / std UpperCAmelCase = model(__A ) UpperCAmelCase = torch.nn.functional.cross_entropy(__A , batch["label"] ) # We keep track of the loss at each epoch if args.with_tracking: total_loss += loss.detach().float() accelerator.backward(__A ) optimizer.step() lr_scheduler.step() optimizer.zero_grad() overall_step += 1 if isinstance(__A , __A ): UpperCAmelCase = F"step_{overall_step}" if overall_step % checkpointing_steps == 0: if args.output_dir is not None: UpperCAmelCase = os.path.join(args.output_dir , __A ) accelerator.save_state(__A ) model.eval() UpperCAmelCase = 0 UpperCAmelCase = 0 for step, batch in enumerate(__A ): # We could avoid this line since we set the accelerator with `device_placement=True`. UpperCAmelCase = {k: v.to(accelerator.device ) for k, v in batch.items()} UpperCAmelCase = (batch["image"] - mean) / std with torch.no_grad(): UpperCAmelCase = model(__A ) UpperCAmelCase = outputs.argmax(dim=-1 ) UpperCAmelCase , UpperCAmelCase = accelerator.gather_for_metrics((predictions, batch["label"]) ) UpperCAmelCase = predictions == references num_elems += accurate_preds.shape[0] accurate += accurate_preds.long().sum() UpperCAmelCase = accurate.item() / num_elems # Use accelerator.print to print only on the main process. accelerator.print(F"epoch {epoch}: {100 * eval_metric:.2f}" ) if args.with_tracking: accelerator.log( { "accuracy": 100 * eval_metric, "train_loss": total_loss.item() / len(__A ), "epoch": epoch, } , step=__A , ) if checkpointing_steps == "epoch": UpperCAmelCase = F"epoch_{epoch}" if args.output_dir is not None: UpperCAmelCase = os.path.join(args.output_dir , __A ) accelerator.save_state(__A ) if args.with_tracking: accelerator.end_training() def _lowerCAmelCase( ): UpperCAmelCase = argparse.ArgumentParser(description="Simple example of training script." ) parser.add_argument("--data_dir" , required=__A , help="The data folder on disk." ) parser.add_argument("--fp16" , action="store_true" , help="If passed, will use FP16 training." ) parser.add_argument( "--mixed_precision" , type=__A , default=__A , choices=["no", "fp16", "bf16", "fp8"] , help="Whether to use mixed precision. Choose" "between fp16 and bf16 (bfloat16). Bf16 requires PyTorch >= 1.10." "and an Nvidia Ampere GPU." , ) parser.add_argument("--cpu" , action="store_true" , help="If passed, will train on the CPU." ) parser.add_argument( "--checkpointing_steps" , type=__A , default=__A , help="Whether the various states should be saved at the end of every n steps, or 'epoch' for each epoch." , ) parser.add_argument( "--output_dir" , type=__A , default="." , help="Optional save directory where all checkpoint folders will be stored. Default is the current working directory." , ) parser.add_argument( "--resume_from_checkpoint" , type=__A , default=__A , help="If the training should continue from a checkpoint folder." , ) parser.add_argument( "--with_tracking" , action="store_true" , help="Whether to load in all available experiment trackers from the environment and use them for logging." , ) parser.add_argument( "--project_dir" , type=__A , default="logs" , help="Location on where to store experiment tracking logs` and relevent project information" , ) UpperCAmelCase = parser.parse_args() UpperCAmelCase = {"lr": 3E-2, "num_epochs": 3, "seed": 42, "batch_size": 64, "image_size": 224} training_function(__A , __A ) if __name__ == "__main__": main()
1
0
from copy import deepcopy class __magic_name__ : def __init__( self : List[str] , lowerCAmelCase__ : list[int] | None = None , lowerCAmelCase__ : int | None = None ) -> None: if arr is None and size is not None: UpperCAmelCase = size UpperCAmelCase = [0] * size elif arr is not None: self.init(lowerCAmelCase__ ) else: raise ValueError("Either arr or size must be specified" ) def _UpperCamelCase ( self : int , lowerCAmelCase__ : list[int] ) -> None: UpperCAmelCase = len(lowerCAmelCase__ ) UpperCAmelCase = deepcopy(lowerCAmelCase__ ) for i in range(1 , self.size ): UpperCAmelCase = self.next_(lowerCAmelCase__ ) if j < self.size: self.tree[j] += self.tree[i] def _UpperCamelCase ( self : Dict ) -> list[int]: UpperCAmelCase = self.tree[:] for i in range(self.size - 1 , 0 , -1 ): UpperCAmelCase = self.next_(lowerCAmelCase__ ) if j < self.size: arr[j] -= arr[i] return arr @staticmethod def _UpperCamelCase ( lowerCAmelCase__ : int ) -> int: return index + (index & (-index)) @staticmethod def _UpperCamelCase ( lowerCAmelCase__ : int ) -> int: return index - (index & (-index)) def _UpperCamelCase ( self : List[Any] , lowerCAmelCase__ : int , lowerCAmelCase__ : int ) -> None: if index == 0: self.tree[0] += value return while index < self.size: self.tree[index] += value UpperCAmelCase = self.next_(lowerCAmelCase__ ) def _UpperCamelCase ( self : List[str] , lowerCAmelCase__ : int , lowerCAmelCase__ : int ) -> None: self.add(lowerCAmelCase__ , value - self.get(lowerCAmelCase__ ) ) def _UpperCamelCase ( self : Optional[int] , lowerCAmelCase__ : int ) -> int: if right == 0: return 0 UpperCAmelCase = self.tree[0] right -= 1 # make right inclusive while right > 0: result += self.tree[right] UpperCAmelCase = self.prev(lowerCAmelCase__ ) return result def _UpperCamelCase ( self : List[str] , lowerCAmelCase__ : int , lowerCAmelCase__ : int ) -> int: return self.prefix(lowerCAmelCase__ ) - self.prefix(lowerCAmelCase__ ) def _UpperCamelCase ( self : int , lowerCAmelCase__ : int ) -> int: return self.query(lowerCAmelCase__ , index + 1 ) def _UpperCamelCase ( self : Any , lowerCAmelCase__ : int ) -> int: value -= self.tree[0] if value < 0: return -1 UpperCAmelCase = 1 # Largest power of 2 <= size while j * 2 < self.size: j *= 2 UpperCAmelCase = 0 while j > 0: if i + j < self.size and self.tree[i + j] <= value: value -= self.tree[i + j] i += j j //= 2 return i if __name__ == "__main__": import doctest doctest.testmod()
712
import glob import os import random from string import ascii_lowercase, digits import cva lowerCAmelCase__ = "" lowerCAmelCase__ = "" lowerCAmelCase__ = "" lowerCAmelCase__ = 1 # (0 is vertical, 1 is horizontal) def _lowerCAmelCase( ): UpperCAmelCase , UpperCAmelCase = get_dataset(__A , __A ) print("Processing..." ) UpperCAmelCase , UpperCAmelCase , UpperCAmelCase = update_image_and_anno(__A , __A , __A ) for index, image in enumerate(__A ): # Get random string code: '7b7ad245cdff75241935e4dd860f3bad' UpperCAmelCase = random_chars(32 ) UpperCAmelCase = paths[index].split(os.sep )[-1].rsplit("." , 1 )[0] UpperCAmelCase = F"{OUTPUT_DIR}/{file_name}_FLIP_{letter_code}" cva.imwrite(F"/{file_root}.jpg" , __A , [cva.IMWRITE_JPEG_QUALITY, 85] ) print(F"Success {index+1}/{len(__A )} with {file_name}" ) UpperCAmelCase = [] for anno in new_annos[index]: UpperCAmelCase = F"{anno[0]} {anno[1]} {anno[2]} {anno[3]} {anno[4]}" annos_list.append(__A ) with open(F"/{file_root}.txt" , "w" ) as outfile: outfile.write("\n".join(line for line in annos_list ) ) def _lowerCAmelCase( __A , __A ): UpperCAmelCase = [] UpperCAmelCase = [] for label_file in glob.glob(os.path.join(__A , "*.txt" ) ): UpperCAmelCase = label_file.split(os.sep )[-1].rsplit("." , 1 )[0] with open(__A ) as in_file: UpperCAmelCase = in_file.readlines() UpperCAmelCase = os.path.join(__A , F"{label_name}.jpg" ) UpperCAmelCase = [] for obj_list in obj_lists: UpperCAmelCase = obj_list.rstrip("\n" ).split(" " ) boxes.append( [ int(obj[0] ), float(obj[1] ), float(obj[2] ), float(obj[3] ), float(obj[4] ), ] ) if not boxes: continue img_paths.append(__A ) labels.append(__A ) return img_paths, labels def _lowerCAmelCase( __A , __A , __A = 1 ): UpperCAmelCase = [] UpperCAmelCase = [] UpperCAmelCase = [] for idx in range(len(__A ) ): UpperCAmelCase = [] UpperCAmelCase = img_list[idx] path_list.append(__A ) UpperCAmelCase = anno_list[idx] UpperCAmelCase = cva.imread(__A ) if flip_type == 1: UpperCAmelCase = cva.flip(__A , __A ) for bbox in img_annos: UpperCAmelCase = 1 - bbox[1] new_annos.append([bbox[0], x_center_new, bbox[2], bbox[3], bbox[4]] ) elif flip_type == 0: UpperCAmelCase = cva.flip(__A , __A ) for bbox in img_annos: UpperCAmelCase = 1 - bbox[2] new_annos.append([bbox[0], bbox[1], y_center_new, bbox[3], bbox[4]] ) new_annos_lists.append(__A ) new_imgs_list.append(__A ) return new_imgs_list, new_annos_lists, path_list def _lowerCAmelCase( __A = 32 ): assert number_char > 1, "The number of character should greater than 1" UpperCAmelCase = ascii_lowercase + digits return "".join(random.choice(__A ) for _ in range(__A ) ) if __name__ == "__main__": main() print("DONE ✅")
1
0
from functools import reduce lowerCAmelCase__ = ( "73167176531330624919225119674426574742355349194934" "96983520312774506326239578318016984801869478851843" "85861560789112949495459501737958331952853208805511" "12540698747158523863050715693290963295227443043557" "66896648950445244523161731856403098711121722383113" "62229893423380308135336276614282806444486645238749" "30358907296290491560440772390713810515859307960866" "70172427121883998797908792274921901699720888093776" "65727333001053367881220235421809751254540594752243" "52584907711670556013604839586446706324415722155397" "53697817977846174064955149290862569321978468622482" "83972241375657056057490261407972968652414535100474" "82166370484403199890008895243450658541227588666881" "16427171479924442928230863465674813919123162824586" "17866458359124566529476545682848912883142607690042" "24219022671055626321111109370544217506941658960408" "07198403850962455444362981230987879927244284909188" "84580156166097919133875499200524063689912560717606" "05886116467109405077541002256983155200055935729725" "71636269561882670428252483600823257530420752963450" ) def _lowerCAmelCase( __A = N ): return max( # mypy cannot properly interpret reduce int(reduce(lambda __A , __A : str(int(__A ) * int(__A ) ) , n[i : i + 13] ) ) for i in range(len(__A ) - 12 ) ) if __name__ == "__main__": print(f"{solution() = }")
713
def _lowerCAmelCase( __A ): if not isinstance(__A , __A ): raise TypeError("only integers accepted as input" ) else: UpperCAmelCase = str(abs(__A ) ) UpperCAmelCase = [list(__A ) for char in range(len(__A ) )] for index in range(len(__A ) ): num_transpositions[index].pop(__A ) return max( int("".join(list(__A ) ) ) for transposition in num_transpositions ) if __name__ == "__main__": __import__("doctest").testmod()
1
0
from ...configuration_utils import PretrainedConfig from ...utils import logging lowerCAmelCase__ = logging.get_logger(__name__) lowerCAmelCase__ = { "unc-nlp/lxmert-base-uncased": "https://huggingface.co/unc-nlp/lxmert-base-uncased/resolve/main/config.json", } class __magic_name__ ( _snake_case ): UpperCAmelCase = """lxmert""" UpperCAmelCase = {} def __init__( self : int , lowerCAmelCase__ : Any=3_0_5_2_2 , lowerCAmelCase__ : List[str]=7_6_8 , lowerCAmelCase__ : Union[str, Any]=1_2 , lowerCAmelCase__ : List[Any]=9_5_0_0 , lowerCAmelCase__ : Any=1_6_0_0 , lowerCAmelCase__ : Union[str, Any]=4_0_0 , lowerCAmelCase__ : Tuple=3_0_7_2 , lowerCAmelCase__ : Dict="gelu" , lowerCAmelCase__ : Tuple=0.1 , lowerCAmelCase__ : Tuple=0.1 , lowerCAmelCase__ : int=5_1_2 , lowerCAmelCase__ : List[str]=2 , lowerCAmelCase__ : List[str]=0.02 , lowerCAmelCase__ : str=1e-1_2 , lowerCAmelCase__ : str=9 , lowerCAmelCase__ : int=5 , lowerCAmelCase__ : Optional[int]=5 , lowerCAmelCase__ : List[Any]=2_0_4_8 , lowerCAmelCase__ : Any=4 , lowerCAmelCase__ : Dict=6.67 , lowerCAmelCase__ : Any=True , lowerCAmelCase__ : Union[str, Any]=True , lowerCAmelCase__ : Any=True , lowerCAmelCase__ : Tuple=True , lowerCAmelCase__ : Optional[Any]=True , lowerCAmelCase__ : Optional[int]=True , lowerCAmelCase__ : Tuple=True , **lowerCAmelCase__ : List[Any] , ) -> Dict: UpperCAmelCase = vocab_size UpperCAmelCase = hidden_size 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 = num_qa_labels UpperCAmelCase = num_object_labels UpperCAmelCase = num_attr_labels UpperCAmelCase = l_layers UpperCAmelCase = x_layers UpperCAmelCase = r_layers UpperCAmelCase = visual_feat_dim UpperCAmelCase = visual_pos_dim UpperCAmelCase = visual_loss_normalizer UpperCAmelCase = task_matched UpperCAmelCase = task_mask_lm UpperCAmelCase = task_obj_predict UpperCAmelCase = task_qa UpperCAmelCase = visual_obj_loss UpperCAmelCase = visual_attr_loss UpperCAmelCase = visual_feat_loss UpperCAmelCase = {"vision": r_layers, "cross_encoder": x_layers, "language": l_layers} super().__init__(**lowerCAmelCase__ )
714
import logging import re import pytorch_quantization import pytorch_quantization.nn as quant_nn import torch from pytorch_quantization import calib from pytorch_quantization.tensor_quant import QuantDescriptor lowerCAmelCase__ = logging.getLogger(__name__) lowerCAmelCase__ = 50 # max width of layer names lowerCAmelCase__ = 70 # max width of quantizer names def _lowerCAmelCase( __A ): UpperCAmelCase = parser.add_argument_group("quant_trainer arguments" ) group.add_argument("--wprec" , type=__A , default=8 , help="weight precision" ) group.add_argument("--aprec" , type=__A , default=8 , help="activation precision" ) group.add_argument("--quant-per-tensor" , action="store_true" , help="per tensor weight scaling" ) group.add_argument("--quant-disable" , action="store_true" , help="disable all quantizers" ) group.add_argument("--quant-disable-embeddings" , action="store_true" , help="disable all embeddings quantizers" ) group.add_argument("--quant-disable-keyword" , type=__A , nargs="+" , help="disable quantizers by keyword" ) group.add_argument("--quant-disable-layer-module" , type=__A , help="disable quantizers by keyword under layer." ) group.add_argument("--quant-enable-layer-module" , type=__A , help="enable quantizers by keyword under layer" ) group.add_argument("--calibrator" , default="max" , help="which quantization range calibrator to use" ) group.add_argument("--percentile" , default=__A , type=__A , help="percentile for PercentileCalibrator" ) group.add_argument("--fuse-qkv" , action="store_true" , help="use the same scale factor for qkv" ) group.add_argument("--clip-gelu" , metavar="N" , type=__A , help="clip gelu output maximum value to N" ) group.add_argument( "--recalibrate-weights" , action="store_true" , help=( "recalibrate weight amaxes by taking the max of the weights." " amaxes will be computed with the current quantization granularity (axis)." ) , ) def _lowerCAmelCase( __A ): if args.calibrator == "max": UpperCAmelCase = "max" elif args.calibrator == "percentile": if args.percentile is None: raise ValueError("Specify --percentile when using percentile calibrator" ) UpperCAmelCase = "histogram" elif args.calibrator == "mse": UpperCAmelCase = "histogram" else: raise ValueError(F"Invalid calibrator {args.calibrator}" ) UpperCAmelCase = QuantDescriptor(num_bits=args.aprec , calib_method=__A ) UpperCAmelCase = QuantDescriptor(num_bits=args.wprec , axis=(None if args.quant_per_tensor else (0,)) ) quant_nn.QuantLinear.set_default_quant_desc_input(__A ) quant_nn.QuantLinear.set_default_quant_desc_weight(__A ) def _lowerCAmelCase( __A , __A , __A=False , __A=False ): logger.info("Configuring Model for Quantization" ) logger.info(F"using quantization package {pytorch_quantization.__file__}" ) if not calib: if args.quant_disable_embeddings: set_quantizer_by_name(__A , ["embeddings"] , which="weight" , _disabled=__A ) if args.quant_disable: set_quantizer_by_name(__A , [""] , _disabled=__A ) if args.quant_disable_keyword: set_quantizer_by_name(__A , args.quant_disable_keyword , _disabled=__A ) if args.quant_disable_layer_module: set_quantizer_by_name(__A , [r"layer.\d+." + args.quant_disable_layer_module] , _disabled=__A ) if args.quant_enable_layer_module: set_quantizer_by_name(__A , [r"layer.\d+." + args.quant_enable_layer_module] , _disabled=__A ) if args.recalibrate_weights: recalibrate_weights(__A ) if args.fuse_qkv: fuse_qkv(__A , __A ) if args.clip_gelu: clip_gelu(__A , args.clip_gelu ) # if args.local_rank in [-1, 0] and not calib: print_quant_summary(__A ) def _lowerCAmelCase( __A ): logger.info("Enabling Calibration" ) for name, module in model.named_modules(): if name.endswith("_quantizer" ): if module._calibrator is not None: module.disable_quant() module.enable_calib() else: module.disable() logger.info(F"{name:80}: {module}" ) def _lowerCAmelCase( __A , __A ): logger.info("Loading calibrated amax" ) for name, module in model.named_modules(): if name.endswith("_quantizer" ): if module._calibrator is not None: if isinstance(module._calibrator , calib.MaxCalibrator ): module.load_calib_amax() else: module.load_calib_amax("percentile" , percentile=args.percentile ) module.enable_quant() module.disable_calib() else: module.enable() model.cuda() print_quant_summary(__A ) def _lowerCAmelCase( __A , __A ): def fusea(__A , __A , __A ): for mod in [qq, qk, qv]: if not hasattr(__A , "_amax" ): print(" WARNING: NO AMAX BUFFER" ) return UpperCAmelCase = qq._amax.detach().item() UpperCAmelCase = qk._amax.detach().item() UpperCAmelCase = qv._amax.detach().item() UpperCAmelCase = max(__A , __A , __A ) qq._amax.fill_(__A ) qk._amax.fill_(__A ) qv._amax.fill_(__A ) logger.info(F" q={q:5.2f} k={k:5.2f} v={v:5.2f} -> {amax:5.2f}" ) for name, mod in model.named_modules(): if name.endswith(".attention.self" ): logger.info(F"FUSE_QKV: {name:{name_width}}" ) fusea(mod.matmul_q_input_quantizer , mod.matmul_k_input_quantizer , mod.matmul_v_input_quantizer ) if args.quant_per_tensor: fusea(mod.query._weight_quantizer , mod.key._weight_quantizer , mod.value._weight_quantizer ) def _lowerCAmelCase( __A , __A ): for name, mod in model.named_modules(): if name.endswith(".output.dense" ) and not name.endswith("attention.output.dense" ): UpperCAmelCase = mod._input_quantizer._amax.data.detach().item() mod._input_quantizer._amax.data.detach().clamp_(max=__A ) UpperCAmelCase = mod._input_quantizer._amax.data.detach().item() logger.info(F"CLIP_GELU: {name:{name_width}} amax: {amax_init:5.2f} -> {amax:5.2f}" ) def _lowerCAmelCase( __A ): for name, mod in model.named_modules(): if hasattr(__A , "_weight_quantizer" ) and mod._weight_quantizer.axis is not None: UpperCAmelCase = mod.weight.shape[0] UpperCAmelCase = mod._weight_quantizer._amax.detach() UpperCAmelCase = torch.ones(__A , dtype=amax.dtype , device=amax.device ) * amax print(F"expanding {name} {amax} -> {mod._weight_quantizer._amax}" ) def _lowerCAmelCase( __A ): for name, mod in model.named_modules(): if hasattr(__A , "_weight_quantizer" ): if not hasattr(mod.weight_quantizer , "_amax" ): print("RECALIB: {name:{name_width}} WARNING: NO AMAX BUFFER" ) continue # determine which axes to reduce across # e.g. a 4D tensor quantized per axis 0 should reduce over (1,2,3) UpperCAmelCase = set() if mod._weight_quantizer.axis is None else set(mod._weight_quantizer.axis ) UpperCAmelCase = set(range(len(mod.weight.size() ) ) ) - axis_set UpperCAmelCase = pytorch_quantization.utils.reduce_amax(mod.weight , axis=__A , keepdims=__A ).detach() logger.info(F"RECALIB: {name:{name_width}} {mod._weight_quantizer._amax.flatten()} -> {amax.flatten()}" ) UpperCAmelCase = amax def _lowerCAmelCase( __A , __A=25 , __A=180 , __A=None ): if ignore is None: UpperCAmelCase = [] elif not isinstance(__A , __A ): UpperCAmelCase = [ignore] UpperCAmelCase = 0 for name, mod in model.named_modules(): if not hasattr(__A , "weight" ): continue UpperCAmelCase = max(__A , len(__A ) ) for name, mod in model.named_modules(): UpperCAmelCase = getattr(__A , "_input_quantizer" , __A ) UpperCAmelCase = getattr(__A , "_weight_quantizer" , __A ) if not hasattr(__A , "weight" ): continue if type(__A ) in ignore: continue if [True for s in ignore if type(__A ) is str and s in name]: continue UpperCAmelCase = F"Act:{input_q.extra_repr()}" UpperCAmelCase = F"Wgt:{weight_q.extra_repr()}" UpperCAmelCase = F"{name:{name_width}} {act_str} {wgt_str}" if len(__A ) <= line_width: logger.info(__A ) else: logger.info(F"{name:{name_width}} {act_str}" ) logger.info(F"{' ':{name_width}} {wgt_str}" ) def _lowerCAmelCase( __A ): UpperCAmelCase = 0 for name, mod in model.named_modules(): if isinstance(__A , pytorch_quantization.nn.TensorQuantizer ): print(F"{name:80} {mod}" ) count += 1 print(F"{count} TensorQuantizers found in model" ) def _lowerCAmelCase( __A , __A , __A , __A , __A ): UpperCAmelCase = getattr(__A , __A , __A ) if quantizer_mod is not None: assert hasattr(__A , __A ) setattr(__A , __A , __A ) else: logger.warning(F"{name} has no {quantizer}" ) def _lowerCAmelCase( __A , __A , __A="both" , **__A ): UpperCAmelCase = F"Warning: changing {which} quantizers of {name:{qname_width}}" for k, v in kwargs.items(): s += F" {k}={v}" if which in ["input", "both"]: set_quantizer(__A , __A , "_input_quantizer" , __A , __A ) if which in ["weight", "both"]: set_quantizer(__A , __A , "_weight_quantizer" , __A , __A ) logger.info(__A ) def _lowerCAmelCase( __A , __A , **__A ): for name, mod in model.named_modules(): if hasattr(__A , "_input_quantizer" ) or hasattr(__A , "_weight_quantizer" ): for n in names: if re.search(__A , __A ): set_quantizers(__A , __A , **__A ) elif name.endswith("_quantizer" ): for n in names: if re.search(__A , __A ): UpperCAmelCase = F"Warning: changing {name:{name_width}}" for k, v in kwargs.items(): s += F" {k}={v}" setattr(__A , __A , __A ) logger.info(__A )
1
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__ = { "facebook/data2vec-text-base": "https://huggingface.co/data2vec/resolve/main/config.json", } class __magic_name__ ( _snake_case ): UpperCAmelCase = """data2vec-text""" def __init__( self : Optional[int] , lowerCAmelCase__ : Optional[Any]=3_0_5_2_2 , lowerCAmelCase__ : List[str]=7_6_8 , lowerCAmelCase__ : Union[str, Any]=1_2 , lowerCAmelCase__ : Union[str, Any]=1_2 , lowerCAmelCase__ : Optional[int]=3_0_7_2 , lowerCAmelCase__ : Optional[Any]="gelu" , lowerCAmelCase__ : Tuple=0.1 , lowerCAmelCase__ : Optional[int]=0.1 , lowerCAmelCase__ : Any=5_1_2 , lowerCAmelCase__ : int=2 , lowerCAmelCase__ : List[str]=0.02 , lowerCAmelCase__ : int=1e-1_2 , lowerCAmelCase__ : Tuple=1 , lowerCAmelCase__ : str=0 , lowerCAmelCase__ : Any=2 , lowerCAmelCase__ : List[Any]="absolute" , lowerCAmelCase__ : Tuple=True , lowerCAmelCase__ : Optional[Any]=None , **lowerCAmelCase__ : str , ) -> List[str]: super().__init__(pad_token_id=lowerCAmelCase__ , bos_token_id=lowerCAmelCase__ , eos_token_id=lowerCAmelCase__ , **lowerCAmelCase__ ) 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 = position_embedding_type UpperCAmelCase = use_cache UpperCAmelCase = classifier_dropout class __magic_name__ ( _snake_case ): @property def _UpperCamelCase ( self : Optional[int] ) -> Mapping[str, Mapping[int, str]]: 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), ] )
715
def _lowerCAmelCase( __A ): assert column_title.isupper() UpperCAmelCase = 0 UpperCAmelCase = len(__A ) - 1 UpperCAmelCase = 0 while index >= 0: UpperCAmelCase = (ord(column_title[index] ) - 64) * pow(26 , __A ) answer += value power += 1 index -= 1 return answer if __name__ == "__main__": from doctest import testmod testmod()
1
0
lowerCAmelCase__ = "\n# Transformers 설치 방법\n! pip install transformers datasets\n# 마지막 릴리스 대신 소스에서 설치하려면, 위 명령을 주석으로 바꾸고 아래 명령을 해제하세요.\n# ! pip install git+https://github.com/huggingface/transformers.git\n" lowerCAmelCase__ = [{"type": "code", "content": INSTALL_CONTENT}] lowerCAmelCase__ = { "{processor_class}": "FakeProcessorClass", "{model_class}": "FakeModelClass", "{object_class}": "FakeObjectClass", }
716
import sys import tempfile import unittest import unittest.mock as mock from pathlib import Path from huggingface_hub import HfFolder, delete_repo from requests.exceptions import HTTPError from transformers import AutoFeatureExtractor, WavaVecaFeatureExtractor from transformers.testing_utils import TOKEN, USER, get_tests_dir, is_staging_test sys.path.append(str(Path(__file__).parent.parent / "utils")) from test_module.custom_feature_extraction import CustomFeatureExtractor # noqa E402 lowerCAmelCase__ = get_tests_dir("fixtures") class __magic_name__ ( unittest.TestCase ): def _UpperCamelCase ( self : Tuple ) -> Union[str, Any]: # A mock response for an HTTP head request to emulate server down UpperCAmelCase = mock.Mock() UpperCAmelCase = 5_0_0 UpperCAmelCase = {} UpperCAmelCase = HTTPError UpperCAmelCase = {} # Download this model to make sure it's in the cache. UpperCAmelCase = WavaVecaFeatureExtractor.from_pretrained("hf-internal-testing/tiny-random-wav2vec2" ) # Under the mock environment we get a 500 error when trying to reach the model. with mock.patch("requests.Session.request" , return_value=lowerCAmelCase__ ) as mock_head: UpperCAmelCase = WavaVecaFeatureExtractor.from_pretrained("hf-internal-testing/tiny-random-wav2vec2" ) # This check we did call the fake head request mock_head.assert_called() def _UpperCamelCase ( self : List[Any] ) -> Dict: # This test is for deprecated behavior and can be removed in v5 UpperCAmelCase = WavaVecaFeatureExtractor.from_pretrained( "https://huggingface.co/hf-internal-testing/tiny-random-wav2vec2/resolve/main/preprocessor_config.json" ) @is_staging_test class __magic_name__ ( unittest.TestCase ): @classmethod def _UpperCamelCase ( cls : List[str] ) -> List[Any]: UpperCAmelCase = TOKEN HfFolder.save_token(lowerCAmelCase__ ) @classmethod def _UpperCamelCase ( cls : Optional[int] ) -> Union[str, Any]: try: delete_repo(token=cls._token , repo_id="test-feature-extractor" ) except HTTPError: pass try: delete_repo(token=cls._token , repo_id="valid_org/test-feature-extractor-org" ) except HTTPError: pass try: delete_repo(token=cls._token , repo_id="test-dynamic-feature-extractor" ) except HTTPError: pass def _UpperCamelCase ( self : Any ) -> Any: UpperCAmelCase = WavaVecaFeatureExtractor.from_pretrained(lowerCAmelCase__ ) feature_extractor.push_to_hub("test-feature-extractor" , use_auth_token=self._token ) UpperCAmelCase = WavaVecaFeatureExtractor.from_pretrained(f"{USER}/test-feature-extractor" ) for k, v in feature_extractor.__dict__.items(): self.assertEqual(lowerCAmelCase__ , getattr(lowerCAmelCase__ , lowerCAmelCase__ ) ) # Reset repo delete_repo(token=self._token , repo_id="test-feature-extractor" ) # Push to hub via save_pretrained with tempfile.TemporaryDirectory() as tmp_dir: feature_extractor.save_pretrained( lowerCAmelCase__ , repo_id="test-feature-extractor" , push_to_hub=lowerCAmelCase__ , use_auth_token=self._token ) UpperCAmelCase = WavaVecaFeatureExtractor.from_pretrained(f"{USER}/test-feature-extractor" ) for k, v in feature_extractor.__dict__.items(): self.assertEqual(lowerCAmelCase__ , getattr(lowerCAmelCase__ , lowerCAmelCase__ ) ) def _UpperCamelCase ( self : List[Any] ) -> Tuple: UpperCAmelCase = WavaVecaFeatureExtractor.from_pretrained(lowerCAmelCase__ ) feature_extractor.push_to_hub("valid_org/test-feature-extractor" , use_auth_token=self._token ) UpperCAmelCase = WavaVecaFeatureExtractor.from_pretrained("valid_org/test-feature-extractor" ) for k, v in feature_extractor.__dict__.items(): self.assertEqual(lowerCAmelCase__ , getattr(lowerCAmelCase__ , lowerCAmelCase__ ) ) # Reset repo delete_repo(token=self._token , repo_id="valid_org/test-feature-extractor" ) # Push to hub via save_pretrained with tempfile.TemporaryDirectory() as tmp_dir: feature_extractor.save_pretrained( lowerCAmelCase__ , repo_id="valid_org/test-feature-extractor-org" , push_to_hub=lowerCAmelCase__ , use_auth_token=self._token ) UpperCAmelCase = WavaVecaFeatureExtractor.from_pretrained("valid_org/test-feature-extractor-org" ) for k, v in feature_extractor.__dict__.items(): self.assertEqual(lowerCAmelCase__ , getattr(lowerCAmelCase__ , lowerCAmelCase__ ) ) def _UpperCamelCase ( self : Dict ) -> List[str]: CustomFeatureExtractor.register_for_auto_class() UpperCAmelCase = CustomFeatureExtractor.from_pretrained(lowerCAmelCase__ ) feature_extractor.push_to_hub("test-dynamic-feature-extractor" , use_auth_token=self._token ) # This has added the proper auto_map field to the config self.assertDictEqual( feature_extractor.auto_map , {"AutoFeatureExtractor": "custom_feature_extraction.CustomFeatureExtractor"} , ) UpperCAmelCase = AutoFeatureExtractor.from_pretrained( f"{USER}/test-dynamic-feature-extractor" , trust_remote_code=lowerCAmelCase__ ) # Can't make an isinstance check because the new_feature_extractor is from the CustomFeatureExtractor class of a dynamic module self.assertEqual(new_feature_extractor.__class__.__name__ , "CustomFeatureExtractor" )
1
0
from typing import Dict, List from nltk.translate import gleu_score import datasets from datasets import MetricInfo lowerCAmelCase__ = "\\n@misc{wu2016googles,\n title={Google's Neural Machine Translation System: Bridging the Gap between Human and Machine Translation},\n author={Yonghui Wu and Mike Schuster and Zhifeng Chen and Quoc V. Le and Mohammad Norouzi and Wolfgang Macherey\n and Maxim Krikun and Yuan Cao and Qin Gao and Klaus Macherey and Jeff Klingner and Apurva Shah and Melvin\n Johnson and Xiaobing Liu and Łukasz Kaiser and Stephan Gouws and Yoshikiyo Kato and Taku Kudo and Hideto\n Kazawa and Keith Stevens and George Kurian and Nishant Patil and Wei Wang and Cliff Young and\n Jason Smith and Jason Riesa and Alex Rudnick and Oriol Vinyals and Greg Corrado and Macduff Hughes\n and Jeffrey Dean},\n year={2016},\n eprint={1609.08144},\n archivePrefix={arXiv},\n primaryClass={cs.CL}\n}\n" lowerCAmelCase__ = "\\nThe BLEU score has some undesirable properties when used for single\nsentences, as it was designed to be a corpus measure. We therefore\nuse a slightly different score for our RL experiments which we call\nthe 'GLEU score'. For the GLEU score, we record all sub-sequences of\n1, 2, 3 or 4 tokens in output and target sequence (n-grams). We then\ncompute a recall, which is the ratio of the number of matching n-grams\nto the number of total n-grams in the target (ground truth) sequence,\nand a precision, which is the ratio of the number of matching n-grams\nto the number of total n-grams in the generated output sequence. Then\nGLEU score is simply the minimum of recall and precision. This GLEU\nscore's range is always between 0 (no matches) and 1 (all match) and\nit is symmetrical when switching output and target. According to\nour experiments, GLEU score correlates quite well with the BLEU\nmetric on a corpus level but does not have its drawbacks for our per\nsentence reward objective.\n" lowerCAmelCase__ = "\\nComputes corpus-level Google BLEU (GLEU) score of translated segments against one or more references.\nInstead of averaging the sentence level GLEU scores (i.e. macro-average precision), Wu et al. (2016) sum up the matching\ntokens and the max of hypothesis and reference tokens for each sentence, then compute using the aggregate values.\n\nArgs:\n predictions (list of str): list of translations to score.\n Each translation should be tokenized into a list of tokens.\n references (list of list of str): list of lists of references for each translation.\n Each reference should be tokenized into a list of tokens.\n min_len (int): The minimum order of n-gram this function should extract. Defaults to 1.\n max_len (int): The maximum order of n-gram this function should extract. Defaults to 4.\n\nReturns:\n 'google_bleu': google_bleu score\n\nExamples:\n Example 1:\n >>> hyp1 = ['It', 'is', 'a', 'guide', 'to', 'action', 'which',\n ... 'ensures', 'that', 'the', 'rubber', 'duck', 'always',\n ... 'disobeys', 'the', 'commands', 'of', 'the', 'cat']\n >>> ref1a = ['It', 'is', 'the', 'guiding', 'principle', 'which',\n ... 'guarantees', 'the', 'rubber', 'duck', 'forces', 'never',\n ... 'being', 'under', 'the', 'command', 'of', 'the', 'cat']\n\n >>> hyp2 = ['he', 'read', 'the', 'book', 'because', 'he', 'was',\n ... 'interested', 'in', 'world', 'history']\n >>> ref2a = ['he', 'was', 'interested', 'in', 'world', 'history',\n ... 'because', 'he', 'read', 'the', 'book']\n\n >>> list_of_references = [[ref1a], [ref2a]]\n >>> hypotheses = [hyp1, hyp2]\n >>> google_bleu = datasets.load_metric(\"google_bleu\")\n >>> results = google_bleu.compute(predictions=hypotheses, references=list_of_references)\n >>> print(round(results[\"google_bleu\"], 2))\n 0.44\n\n Example 2:\n >>> hyp1 = ['It', 'is', 'a', 'guide', 'to', 'action', 'which',\n ... 'ensures', 'that', 'the', 'rubber', 'duck', 'always',\n ... 'disobeys', 'the', 'commands', 'of', 'the', 'cat']\n >>> ref1a = ['It', 'is', 'the', 'guiding', 'principle', 'which',\n ... 'guarantees', 'the', 'rubber', 'duck', 'forces', 'never',\n ... 'being', 'under', 'the', 'command', 'of', 'the', 'cat']\n >>> ref1b = ['It', 'is', 'a', 'guide', 'to', 'action', 'that',\n ... 'ensures', 'that', 'the', 'rubber', 'duck', 'will', 'never',\n ... 'heed', 'the', 'cat', 'commands']\n >>> ref1c = ['It', 'is', 'the', 'practical', 'guide', 'for', 'the',\n ... 'rubber', 'duck', 'army', 'never', 'to', 'heed', 'the', 'directions',\n ... 'of', 'the', 'cat']\n\n >>> hyp2 = ['he', 'read', 'the', 'book', 'because', 'he', 'was',\n ... 'interested', 'in', 'world', 'history']\n >>> ref2a = ['he', 'was', 'interested', 'in', 'world', 'history',\n ... 'because', 'he', 'read', 'the', 'book']\n\n >>> list_of_references = [[ref1a, ref1b, ref1c], [ref2a]]\n >>> hypotheses = [hyp1, hyp2]\n >>> google_bleu = datasets.load_metric(\"google_bleu\")\n >>> results = google_bleu.compute(predictions=hypotheses, references=list_of_references)\n >>> print(round(results[\"google_bleu\"], 2))\n 0.61\n\n Example 3:\n >>> hyp1 = ['It', 'is', 'a', 'guide', 'to', 'action', 'which',\n ... 'ensures', 'that', 'the', 'rubber', 'duck', 'always',\n ... 'disobeys', 'the', 'commands', 'of', 'the', 'cat']\n >>> ref1a = ['It', 'is', 'the', 'guiding', 'principle', 'which',\n ... 'guarantees', 'the', 'rubber', 'duck', 'forces', 'never',\n ... 'being', 'under', 'the', 'command', 'of', 'the', 'cat']\n >>> ref1b = ['It', 'is', 'a', 'guide', 'to', 'action', 'that',\n ... 'ensures', 'that', 'the', 'rubber', 'duck', 'will', 'never',\n ... 'heed', 'the', 'cat', 'commands']\n >>> ref1c = ['It', 'is', 'the', 'practical', 'guide', 'for', 'the',\n ... 'rubber', 'duck', 'army', 'never', 'to', 'heed', 'the', 'directions',\n ... 'of', 'the', 'cat']\n\n >>> hyp2 = ['he', 'read', 'the', 'book', 'because', 'he', 'was',\n ... 'interested', 'in', 'world', 'history']\n >>> ref2a = ['he', 'was', 'interested', 'in', 'world', 'history',\n ... 'because', 'he', 'read', 'the', 'book']\n\n >>> list_of_references = [[ref1a, ref1b, ref1c], [ref2a]]\n >>> hypotheses = [hyp1, hyp2]\n >>> google_bleu = datasets.load_metric(\"google_bleu\")\n >>> results = google_bleu.compute(predictions=hypotheses, references=list_of_references, min_len=2)\n >>> print(round(results[\"google_bleu\"], 2))\n 0.53\n\n Example 4:\n >>> hyp1 = ['It', 'is', 'a', 'guide', 'to', 'action', 'which',\n ... 'ensures', 'that', 'the', 'rubber', 'duck', 'always',\n ... 'disobeys', 'the', 'commands', 'of', 'the', 'cat']\n >>> ref1a = ['It', 'is', 'the', 'guiding', 'principle', 'which',\n ... 'guarantees', 'the', 'rubber', 'duck', 'forces', 'never',\n ... 'being', 'under', 'the', 'command', 'of', 'the', 'cat']\n >>> ref1b = ['It', 'is', 'a', 'guide', 'to', 'action', 'that',\n ... 'ensures', 'that', 'the', 'rubber', 'duck', 'will', 'never',\n ... 'heed', 'the', 'cat', 'commands']\n >>> ref1c = ['It', 'is', 'the', 'practical', 'guide', 'for', 'the',\n ... 'rubber', 'duck', 'army', 'never', 'to', 'heed', 'the', 'directions',\n ... 'of', 'the', 'cat']\n\n >>> hyp2 = ['he', 'read', 'the', 'book', 'because', 'he', 'was',\n ... 'interested', 'in', 'world', 'history']\n >>> ref2a = ['he', 'was', 'interested', 'in', 'world', 'history',\n ... 'because', 'he', 'read', 'the', 'book']\n\n >>> list_of_references = [[ref1a, ref1b, ref1c], [ref2a]]\n >>> hypotheses = [hyp1, hyp2]\n >>> google_bleu = datasets.load_metric(\"google_bleu\")\n >>> results = google_bleu.compute(predictions=hypotheses,references=list_of_references, min_len=2, max_len=6)\n >>> print(round(results[\"google_bleu\"], 2))\n 0.4\n" @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class __magic_name__ ( datasets.Metric ): def _UpperCamelCase ( self : Optional[Any] ) -> MetricInfo: return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { "predictions": datasets.Sequence(datasets.Value("string" , id="token" ) , id="sequence" ), "references": datasets.Sequence( datasets.Sequence(datasets.Value("string" , id="token" ) , id="sequence" ) , id="references" ), } ) , ) def _UpperCamelCase ( self : Any , lowerCAmelCase__ : List[List[List[str]]] , lowerCAmelCase__ : List[List[str]] , lowerCAmelCase__ : int = 1 , lowerCAmelCase__ : int = 4 , ) -> Dict[str, float]: return { "google_bleu": gleu_score.corpus_gleu( list_of_references=lowerCAmelCase__ , hypotheses=lowerCAmelCase__ , min_len=lowerCAmelCase__ , max_len=lowerCAmelCase__ ) }
717
import argparse import os import re # All paths are set with the intent you should run this script from the root of the repo with the command # python utils/check_dummies.py lowerCAmelCase__ = "src/diffusers" # Matches is_xxx_available() lowerCAmelCase__ = re.compile(r"is\_([a-z_]*)_available\(\)") # Matches from xxx import bla lowerCAmelCase__ = re.compile(r"\s+from\s+\S*\s+import\s+([^\(\s].*)\n") lowerCAmelCase__ = "\n{0} = None\n" lowerCAmelCase__ = "\nclass {0}(metaclass=DummyObject):\n _backends = {1}\n\n def __init__(self, *args, **kwargs):\n requires_backends(self, {1})\n\n @classmethod\n def from_config(cls, *args, **kwargs):\n requires_backends(cls, {1})\n\n @classmethod\n def from_pretrained(cls, *args, **kwargs):\n requires_backends(cls, {1})\n" lowerCAmelCase__ = "\ndef {0}(*args, **kwargs):\n requires_backends({0}, {1})\n" def _lowerCAmelCase( __A ): UpperCAmelCase = _re_backend.findall(__A ) if len(__A ) == 0: return None return "_and_".join(__A ) def _lowerCAmelCase( ): with open(os.path.join(__A , "__init__.py" ) , "r" , encoding="utf-8" , newline="\n" ) as f: UpperCAmelCase = f.readlines() # Get to the point we do the actual imports for type checking UpperCAmelCase = 0 UpperCAmelCase = {} # Go through the end of the file while line_index < len(__A ): # If the line contains is_backend_available, we grab all objects associated with the `else` block UpperCAmelCase = find_backend(lines[line_index] ) if backend is not None: while not lines[line_index].startswith("else:" ): line_index += 1 line_index += 1 UpperCAmelCase = [] # Until we unindent, add backend objects to the list while line_index < len(__A ) and len(lines[line_index] ) > 1: UpperCAmelCase = lines[line_index] UpperCAmelCase = _re_single_line_import.search(__A ) if single_line_import_search is not None: objects.extend(single_line_import_search.groups()[0].split(", " ) ) elif line.startswith(" " * 8 ): objects.append(line[8:-2] ) line_index += 1 if len(__A ) > 0: UpperCAmelCase = objects else: line_index += 1 return backend_specific_objects def _lowerCAmelCase( __A , __A ): if name.isupper(): return DUMMY_CONSTANT.format(__A ) elif name.islower(): return DUMMY_FUNCTION.format(__A , __A ) else: return DUMMY_CLASS.format(__A , __A ) def _lowerCAmelCase( __A=None ): if backend_specific_objects is None: UpperCAmelCase = read_init() # For special correspondence backend to module name as used in the function requires_modulename UpperCAmelCase = {} for backend, objects in backend_specific_objects.items(): UpperCAmelCase = "[" + ", ".join(F"\"{b}\"" for b in backend.split("_and_" ) ) + "]" UpperCAmelCase = "# This file is autogenerated by the command `make fix-copies`, do not edit.\n" dummy_file += "from ..utils import DummyObject, requires_backends\n\n" dummy_file += "\n".join([create_dummy_object(__A , __A ) for o in objects] ) UpperCAmelCase = dummy_file return dummy_files def _lowerCAmelCase( __A=False ): UpperCAmelCase = create_dummy_files() # For special correspondence backend to shortcut as used in utils/dummy_xxx_objects.py UpperCAmelCase = {"torch": "pt"} # Locate actual dummy modules and read their content. UpperCAmelCase = os.path.join(__A , "utils" ) UpperCAmelCase = { backend: os.path.join(__A , F"dummy_{short_names.get(__A , __A )}_objects.py" ) for backend in dummy_files.keys() } UpperCAmelCase = {} for backend, file_path in dummy_file_paths.items(): if os.path.isfile(__A ): with open(__A , "r" , encoding="utf-8" , newline="\n" ) as f: UpperCAmelCase = f.read() else: UpperCAmelCase = "" for backend in dummy_files.keys(): if dummy_files[backend] != actual_dummies[backend]: if overwrite: print( F"Updating diffusers.utils.dummy_{short_names.get(__A , __A )}_objects.py as the main " "__init__ has new objects." ) with open(dummy_file_paths[backend] , "w" , encoding="utf-8" , newline="\n" ) as f: f.write(dummy_files[backend] ) else: raise ValueError( "The main __init__ has objects that are not present in " F"diffusers.utils.dummy_{short_names.get(__A , __A )}_objects.py. Run `make fix-copies` " "to fix this." ) if __name__ == "__main__": lowerCAmelCase__ = argparse.ArgumentParser() parser.add_argument("--fix_and_overwrite", action="store_true", help="Whether to fix inconsistencies.") lowerCAmelCase__ = parser.parse_args() check_dummies(args.fix_and_overwrite)
1
0
from __future__ import annotations def _lowerCAmelCase( __A ): if not nums: raise ValueError("List is empty" ) return sum(__A ) / len(__A ) if __name__ == "__main__": import doctest doctest.testmod()
718
from ...configuration_utils import PretrainedConfig from ...utils import logging from ...utils.backbone_utils import BackboneConfigMixin, get_aligned_output_features_output_indices lowerCAmelCase__ = logging.get_logger(__name__) lowerCAmelCase__ = { "facebook/convnextv2-tiny-1k-224": "https://huggingface.co/facebook/convnextv2-tiny-1k-224/resolve/main/config.json", } class __magic_name__ ( _snake_case , _snake_case ): UpperCAmelCase = """convnextv2""" def __init__( self : Optional[Any] , lowerCAmelCase__ : List[Any]=3 , lowerCAmelCase__ : str=4 , lowerCAmelCase__ : Dict=4 , lowerCAmelCase__ : Optional[Any]=None , lowerCAmelCase__ : Optional[Any]=None , lowerCAmelCase__ : str="gelu" , lowerCAmelCase__ : Optional[int]=0.02 , lowerCAmelCase__ : Dict=1e-1_2 , lowerCAmelCase__ : str=0.0 , lowerCAmelCase__ : str=2_2_4 , lowerCAmelCase__ : int=None , lowerCAmelCase__ : List[Any]=None , **lowerCAmelCase__ : List[Any] , ) -> List[Any]: super().__init__(**lowerCAmelCase__ ) UpperCAmelCase = num_channels UpperCAmelCase = patch_size UpperCAmelCase = num_stages UpperCAmelCase = [9_6, 1_9_2, 3_8_4, 7_6_8] if hidden_sizes is None else hidden_sizes UpperCAmelCase = [3, 3, 9, 3] if depths is None else depths UpperCAmelCase = hidden_act UpperCAmelCase = initializer_range UpperCAmelCase = layer_norm_eps UpperCAmelCase = drop_path_rate UpperCAmelCase = image_size UpperCAmelCase = ["stem"] + [f"stage{idx}" for idx in range(1 , len(self.depths ) + 1 )] UpperCAmelCase , UpperCAmelCase = get_aligned_output_features_output_indices( out_features=lowerCAmelCase__ , out_indices=lowerCAmelCase__ , stage_names=self.stage_names )
1
0
'''simple docstring''' import math import sys def _lowerCAmelCase( __A ): UpperCAmelCase = "" try: with open(__A , "rb" ) as binary_file: UpperCAmelCase = binary_file.read() for dat in data: UpperCAmelCase = F"{dat:08b}" result += curr_byte return result except OSError: print("File not accessible" ) sys.exit() def _lowerCAmelCase( __A ): UpperCAmelCase = {"0": "0", "1": "1"} UpperCAmelCase , UpperCAmelCase = "", "" UpperCAmelCase = len(__A ) for i in range(len(__A ) ): curr_string += data_bits[i] if curr_string not in lexicon: continue UpperCAmelCase = lexicon[curr_string] result += last_match_id UpperCAmelCase = last_match_id + "0" if math.loga(__A ).is_integer(): UpperCAmelCase = {} for curr_key in list(__A ): UpperCAmelCase = lexicon.pop(__A ) UpperCAmelCase = new_lex UpperCAmelCase = last_match_id + "1" index += 1 UpperCAmelCase = "" return result def _lowerCAmelCase( __A , __A ): UpperCAmelCase = 8 try: with open(__A , "wb" ) as opened_file: UpperCAmelCase = [ to_write[i : i + byte_length] for i in range(0 , len(__A ) , __A ) ] if len(result_byte_array[-1] ) % byte_length == 0: result_byte_array.append("10000000" ) else: result_byte_array[-1] += "1" + "0" * ( byte_length - len(result_byte_array[-1] ) - 1 ) for elem in result_byte_array[:-1]: opened_file.write(int(__A , 2 ).to_bytes(1 , byteorder="big" ) ) except OSError: print("File not accessible" ) sys.exit() def _lowerCAmelCase( __A ): UpperCAmelCase = 0 for letter in data_bits: if letter == "1": break counter += 1 UpperCAmelCase = data_bits[counter:] UpperCAmelCase = data_bits[counter + 1 :] return data_bits def _lowerCAmelCase( __A , __A ): UpperCAmelCase = read_file_binary(__A ) UpperCAmelCase = remove_prefix(__A ) UpperCAmelCase = decompress_data(__A ) write_file_binary(__A , __A ) if __name__ == "__main__": compress(sys.argv[1], sys.argv[2])
719
lowerCAmelCase__ = "\n# Transformers 설치 방법\n! pip install transformers datasets\n# 마지막 릴리스 대신 소스에서 설치하려면, 위 명령을 주석으로 바꾸고 아래 명령을 해제하세요.\n# ! pip install git+https://github.com/huggingface/transformers.git\n" lowerCAmelCase__ = [{"type": "code", "content": INSTALL_CONTENT}] lowerCAmelCase__ = { "{processor_class}": "FakeProcessorClass", "{model_class}": "FakeModelClass", "{object_class}": "FakeObjectClass", }
1
0
from __future__ import annotations from functools import lru_cache from math import ceil lowerCAmelCase__ = 100 lowerCAmelCase__ = set(range(3, NUM_PRIMES, 2)) primes.add(2) lowerCAmelCase__ = 42 for prime in range(3, ceil(NUM_PRIMES**0.5), 2): if prime not in primes: continue primes.difference_update(set(range(prime * prime, NUM_PRIMES, prime))) @lru_cache(maxsize=100 ) def _lowerCAmelCase( __A ): if number_to_partition < 0: return set() elif number_to_partition == 0: return {1} UpperCAmelCase = set() UpperCAmelCase = 42 UpperCAmelCase = 42 for prime in primes: if prime > number_to_partition: continue for sub in partition(number_to_partition - prime ): ret.add(sub * prime ) return ret def _lowerCAmelCase( __A = 5000 ): for number_to_partition in range(1 , __A ): if len(partition(__A ) ) > number_unique_partitions: return number_to_partition return None if __name__ == "__main__": print(f"{solution() = }")
720
import gc import random import unittest import numpy as np import torch from PIL import Image from transformers import XLMRobertaTokenizerFast from diffusers import DDIMScheduler, KandinskyInpaintPipeline, KandinskyPriorPipeline, UNetaDConditionModel, VQModel from diffusers.pipelines.kandinsky.text_encoder import MCLIPConfig, MultilingualCLIP from diffusers.utils import floats_tensor, load_image, load_numpy, slow, torch_device from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu from ..test_pipelines_common import PipelineTesterMixin, assert_mean_pixel_difference enable_full_determinism() class __magic_name__ ( _snake_case , unittest.TestCase ): UpperCAmelCase = KandinskyInpaintPipeline UpperCAmelCase = ["""prompt""", """image_embeds""", """negative_image_embeds""", """image""", """mask_image"""] UpperCAmelCase = [ """prompt""", """negative_prompt""", """image_embeds""", """negative_image_embeds""", """image""", """mask_image""", ] UpperCAmelCase = [ """generator""", """height""", """width""", """latents""", """guidance_scale""", """negative_prompt""", """num_inference_steps""", """return_dict""", """guidance_scale""", """num_images_per_prompt""", """output_type""", """return_dict""", ] UpperCAmelCase = False @property def _UpperCamelCase ( self : Union[str, Any] ) -> Tuple: return 3_2 @property def _UpperCamelCase ( self : int ) -> List[Any]: return 3_2 @property def _UpperCamelCase ( self : List[Any] ) -> List[Any]: return self.time_input_dim @property def _UpperCamelCase ( self : Tuple ) -> Tuple: return self.time_input_dim * 4 @property def _UpperCamelCase ( self : Any ) -> Optional[int]: return 1_0_0 @property def _UpperCamelCase ( self : Optional[int] ) -> Optional[int]: UpperCAmelCase = XLMRobertaTokenizerFast.from_pretrained("YiYiXu/tiny-random-mclip-base" ) return tokenizer @property def _UpperCamelCase ( self : int ) -> Dict: torch.manual_seed(0 ) UpperCAmelCase = MCLIPConfig( numDims=self.cross_attention_dim , transformerDimensions=self.text_embedder_hidden_size , hidden_size=self.text_embedder_hidden_size , intermediate_size=3_7 , num_attention_heads=4 , num_hidden_layers=5 , vocab_size=1_0_0_5 , ) UpperCAmelCase = MultilingualCLIP(lowerCAmelCase__ ) UpperCAmelCase = text_encoder.eval() return text_encoder @property def _UpperCamelCase ( self : Dict ) -> Optional[int]: torch.manual_seed(0 ) UpperCAmelCase = { "in_channels": 9, # Out channels is double in channels because predicts mean and variance "out_channels": 8, "addition_embed_type": "text_image", "down_block_types": ("ResnetDownsampleBlock2D", "SimpleCrossAttnDownBlock2D"), "up_block_types": ("SimpleCrossAttnUpBlock2D", "ResnetUpsampleBlock2D"), "mid_block_type": "UNetMidBlock2DSimpleCrossAttn", "block_out_channels": (self.block_out_channels_a, self.block_out_channels_a * 2), "layers_per_block": 1, "encoder_hid_dim": self.text_embedder_hidden_size, "encoder_hid_dim_type": "text_image_proj", "cross_attention_dim": self.cross_attention_dim, "attention_head_dim": 4, "resnet_time_scale_shift": "scale_shift", "class_embed_type": None, } UpperCAmelCase = UNetaDConditionModel(**lowerCAmelCase__ ) return model @property def _UpperCamelCase ( self : str ) -> Optional[Any]: return { "block_out_channels": [3_2, 6_4], "down_block_types": ["DownEncoderBlock2D", "AttnDownEncoderBlock2D"], "in_channels": 3, "latent_channels": 4, "layers_per_block": 1, "norm_num_groups": 8, "norm_type": "spatial", "num_vq_embeddings": 1_2, "out_channels": 3, "up_block_types": [ "AttnUpDecoderBlock2D", "UpDecoderBlock2D", ], "vq_embed_dim": 4, } @property def _UpperCamelCase ( self : Dict ) -> List[Any]: torch.manual_seed(0 ) UpperCAmelCase = VQModel(**self.dummy_movq_kwargs ) return model def _UpperCamelCase ( self : Tuple ) -> Any: UpperCAmelCase = self.dummy_text_encoder UpperCAmelCase = self.dummy_tokenizer UpperCAmelCase = self.dummy_unet UpperCAmelCase = self.dummy_movq UpperCAmelCase = DDIMScheduler( num_train_timesteps=1_0_0_0 , beta_schedule="linear" , beta_start=0.00_085 , beta_end=0.012 , clip_sample=lowerCAmelCase__ , set_alpha_to_one=lowerCAmelCase__ , steps_offset=1 , prediction_type="epsilon" , thresholding=lowerCAmelCase__ , ) UpperCAmelCase = { "text_encoder": text_encoder, "tokenizer": tokenizer, "unet": unet, "scheduler": scheduler, "movq": movq, } return components def _UpperCamelCase ( self : Union[str, Any] , lowerCAmelCase__ : Any , lowerCAmelCase__ : Tuple=0 ) -> str: UpperCAmelCase = floats_tensor((1, self.cross_attention_dim) , rng=random.Random(lowerCAmelCase__ ) ).to(lowerCAmelCase__ ) UpperCAmelCase = floats_tensor((1, self.cross_attention_dim) , rng=random.Random(seed + 1 ) ).to(lowerCAmelCase__ ) # create init_image UpperCAmelCase = floats_tensor((1, 3, 6_4, 6_4) , rng=random.Random(lowerCAmelCase__ ) ).to(lowerCAmelCase__ ) UpperCAmelCase = image.cpu().permute(0 , 2 , 3 , 1 )[0] UpperCAmelCase = Image.fromarray(np.uinta(lowerCAmelCase__ ) ).convert("RGB" ).resize((2_5_6, 2_5_6) ) # create mask UpperCAmelCase = np.ones((6_4, 6_4) , dtype=np.floataa ) UpperCAmelCase = 0 if str(lowerCAmelCase__ ).startswith("mps" ): UpperCAmelCase = torch.manual_seed(lowerCAmelCase__ ) else: UpperCAmelCase = torch.Generator(device=lowerCAmelCase__ ).manual_seed(lowerCAmelCase__ ) UpperCAmelCase = { "prompt": "horse", "image": init_image, "mask_image": mask, "image_embeds": image_embeds, "negative_image_embeds": negative_image_embeds, "generator": generator, "height": 6_4, "width": 6_4, "num_inference_steps": 2, "guidance_scale": 4.0, "output_type": "np", } return inputs def _UpperCamelCase ( self : Dict ) -> List[str]: UpperCAmelCase = "cpu" UpperCAmelCase = self.get_dummy_components() UpperCAmelCase = self.pipeline_class(**lowerCAmelCase__ ) UpperCAmelCase = pipe.to(lowerCAmelCase__ ) pipe.set_progress_bar_config(disable=lowerCAmelCase__ ) UpperCAmelCase = pipe(**self.get_dummy_inputs(lowerCAmelCase__ ) ) UpperCAmelCase = output.images UpperCAmelCase = pipe( **self.get_dummy_inputs(lowerCAmelCase__ ) , return_dict=lowerCAmelCase__ , )[0] UpperCAmelCase = image[0, -3:, -3:, -1] UpperCAmelCase = image_from_tuple[0, -3:, -3:, -1] print(f"image.shape {image.shape}" ) assert image.shape == (1, 6_4, 6_4, 3) UpperCAmelCase = np.array( [0.8_326_919, 0.73_790_467, 0.20_918_581, 0.9_309_612, 0.5_511_791, 0.43_713_328, 0.5_513_321, 0.49_922_934, 0.59_497_786] ) assert ( np.abs(image_slice.flatten() - expected_slice ).max() < 1e-2 ), f" expected_slice {expected_slice}, but got {image_slice.flatten()}" assert ( np.abs(image_from_tuple_slice.flatten() - expected_slice ).max() < 1e-2 ), f" expected_slice {expected_slice}, but got {image_from_tuple_slice.flatten()}" def _UpperCamelCase ( self : str ) -> Tuple: super().test_inference_batch_single_identical(expected_max_diff=3e-3 ) @slow @require_torch_gpu class __magic_name__ ( unittest.TestCase ): def _UpperCamelCase ( self : str ) -> str: # clean up the VRAM after each test super().tearDown() gc.collect() torch.cuda.empty_cache() def _UpperCamelCase ( self : Tuple ) -> int: UpperCAmelCase = load_numpy( "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main" "/kandinsky/kandinsky_inpaint_cat_with_hat_fp16.npy" ) UpperCAmelCase = load_image( "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main" "/kandinsky/cat.png" ) UpperCAmelCase = np.ones((7_6_8, 7_6_8) , dtype=np.floataa ) UpperCAmelCase = 0 UpperCAmelCase = "a hat" UpperCAmelCase = KandinskyPriorPipeline.from_pretrained( "kandinsky-community/kandinsky-2-1-prior" , torch_dtype=torch.floataa ) pipe_prior.to(lowerCAmelCase__ ) UpperCAmelCase = KandinskyInpaintPipeline.from_pretrained( "kandinsky-community/kandinsky-2-1-inpaint" , torch_dtype=torch.floataa ) UpperCAmelCase = pipeline.to(lowerCAmelCase__ ) pipeline.set_progress_bar_config(disable=lowerCAmelCase__ ) UpperCAmelCase = torch.Generator(device="cpu" ).manual_seed(0 ) UpperCAmelCase , UpperCAmelCase = pipe_prior( lowerCAmelCase__ , generator=lowerCAmelCase__ , num_inference_steps=5 , negative_prompt="" , ).to_tuple() UpperCAmelCase = pipeline( lowerCAmelCase__ , image=lowerCAmelCase__ , mask_image=lowerCAmelCase__ , image_embeds=lowerCAmelCase__ , negative_image_embeds=lowerCAmelCase__ , generator=lowerCAmelCase__ , num_inference_steps=1_0_0 , height=7_6_8 , width=7_6_8 , output_type="np" , ) UpperCAmelCase = output.images[0] assert image.shape == (7_6_8, 7_6_8, 3) assert_mean_pixel_difference(lowerCAmelCase__ , lowerCAmelCase__ )
1
0
import warnings from ...processing_utils import ProcessorMixin from ...tokenization_utils_base import BatchEncoding class __magic_name__ ( _snake_case ): UpperCAmelCase = ["""image_processor""", """tokenizer"""] UpperCAmelCase = """CLIPImageProcessor""" UpperCAmelCase = ("""XLMRobertaTokenizer""", """XLMRobertaTokenizerFast""") def __init__( self : int , lowerCAmelCase__ : List[str]=None , lowerCAmelCase__ : Tuple=None , **lowerCAmelCase__ : Optional[int] ) -> Union[str, Any]: UpperCAmelCase = 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__ , ) 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__(lowerCAmelCase__ , lowerCAmelCase__ ) def __call__( self : Optional[Any] , lowerCAmelCase__ : str=None , lowerCAmelCase__ : int=None , lowerCAmelCase__ : Tuple=None , **lowerCAmelCase__ : List[str] ) -> Optional[Any]: if text is None and images is None: raise ValueError("You have to specify either text or images. Both cannot be none." ) if text is not None: UpperCAmelCase = self.tokenizer(lowerCAmelCase__ , return_tensors=lowerCAmelCase__ , **lowerCAmelCase__ ) if images is not None: UpperCAmelCase = self.image_processor(lowerCAmelCase__ , return_tensors=lowerCAmelCase__ , **lowerCAmelCase__ ) if text is not None and images is not None: UpperCAmelCase = image_features.pixel_values return encoding elif text is not None: return encoding else: return BatchEncoding(data=dict(**lowerCAmelCase__ ) , tensor_type=lowerCAmelCase__ ) def _UpperCamelCase ( self : Optional[Any] , *lowerCAmelCase__ : Tuple , **lowerCAmelCase__ : Optional[Any] ) -> Any: return self.tokenizer.batch_decode(*lowerCAmelCase__ , **lowerCAmelCase__ ) def _UpperCamelCase ( self : Optional[Any] , *lowerCAmelCase__ : Tuple , **lowerCAmelCase__ : str ) -> Union[str, Any]: return self.tokenizer.decode(*lowerCAmelCase__ , **lowerCAmelCase__ ) @property def _UpperCamelCase ( self : Optional[int] ) -> List[Any]: UpperCAmelCase = self.tokenizer.model_input_names UpperCAmelCase = self.image_processor.model_input_names return list(dict.fromkeys(tokenizer_input_names + image_processor_input_names ) )
721
def _lowerCAmelCase( __A , __A ): return (pointa[0] - pointa[0]) ** 2 + (pointa[1] - pointa[1]) ** 2 def _lowerCAmelCase( __A , __A=0 ): return sorted(__A , key=lambda __A : x[column] ) def _lowerCAmelCase( __A , __A , __A=float("inf" ) ): for i in range(points_counts - 1 ): for j in range(i + 1 , __A ): UpperCAmelCase = euclidean_distance_sqr(points[i] , points[j] ) if current_dis < min_dis: UpperCAmelCase = current_dis return min_dis def _lowerCAmelCase( __A , __A , __A=float("inf" ) ): for i in range(min(6 , points_counts - 1 ) , __A ): for j in range(max(0 , i - 6 ) , __A ): UpperCAmelCase = euclidean_distance_sqr(points[i] , points[j] ) if current_dis < min_dis: UpperCAmelCase = current_dis return min_dis def _lowerCAmelCase( __A , __A , __A ): # base case if points_counts <= 3: return dis_between_closest_pair(__A , __A ) # recursion UpperCAmelCase = points_counts // 2 UpperCAmelCase = closest_pair_of_points_sqr( __A , points_sorted_on_y[:mid] , __A ) UpperCAmelCase = closest_pair_of_points_sqr( __A , points_sorted_on_y[mid:] , points_counts - mid ) UpperCAmelCase = min(__A , __A ) UpperCAmelCase = [] for point in points_sorted_on_x: if abs(point[0] - points_sorted_on_x[mid][0] ) < closest_pair_dis: cross_strip.append(__A ) UpperCAmelCase = dis_between_closest_in_strip( __A , len(__A ) , __A ) return min(__A , __A ) def _lowerCAmelCase( __A , __A ): UpperCAmelCase = column_based_sort(__A , column=0 ) UpperCAmelCase = column_based_sort(__A , column=1 ) return ( closest_pair_of_points_sqr( __A , __A , __A ) ) ** 0.5 if __name__ == "__main__": lowerCAmelCase__ = [(2, 3), (12, 30), (40, 50), (5, 1), (12, 10), (3, 4)] print("Distance:", closest_pair_of_points(points, len(points)))
1
0
import argparse import os import re # All paths are set with the intent you should run this script from the root of the repo with the command # python utils/check_dummies.py lowerCAmelCase__ = "src/diffusers" # Matches is_xxx_available() lowerCAmelCase__ = re.compile(r"is\_([a-z_]*)_available\(\)") # Matches from xxx import bla lowerCAmelCase__ = re.compile(r"\s+from\s+\S*\s+import\s+([^\(\s].*)\n") lowerCAmelCase__ = "\n{0} = None\n" lowerCAmelCase__ = "\nclass {0}(metaclass=DummyObject):\n _backends = {1}\n\n def __init__(self, *args, **kwargs):\n requires_backends(self, {1})\n\n @classmethod\n def from_config(cls, *args, **kwargs):\n requires_backends(cls, {1})\n\n @classmethod\n def from_pretrained(cls, *args, **kwargs):\n requires_backends(cls, {1})\n" lowerCAmelCase__ = "\ndef {0}(*args, **kwargs):\n requires_backends({0}, {1})\n" def _lowerCAmelCase( __A ): UpperCAmelCase = _re_backend.findall(__A ) if len(__A ) == 0: return None return "_and_".join(__A ) def _lowerCAmelCase( ): with open(os.path.join(__A , "__init__.py" ) , "r" , encoding="utf-8" , newline="\n" ) as f: UpperCAmelCase = f.readlines() # Get to the point we do the actual imports for type checking UpperCAmelCase = 0 UpperCAmelCase = {} # Go through the end of the file while line_index < len(__A ): # If the line contains is_backend_available, we grab all objects associated with the `else` block UpperCAmelCase = find_backend(lines[line_index] ) if backend is not None: while not lines[line_index].startswith("else:" ): line_index += 1 line_index += 1 UpperCAmelCase = [] # Until we unindent, add backend objects to the list while line_index < len(__A ) and len(lines[line_index] ) > 1: UpperCAmelCase = lines[line_index] UpperCAmelCase = _re_single_line_import.search(__A ) if single_line_import_search is not None: objects.extend(single_line_import_search.groups()[0].split(", " ) ) elif line.startswith(" " * 8 ): objects.append(line[8:-2] ) line_index += 1 if len(__A ) > 0: UpperCAmelCase = objects else: line_index += 1 return backend_specific_objects def _lowerCAmelCase( __A , __A ): if name.isupper(): return DUMMY_CONSTANT.format(__A ) elif name.islower(): return DUMMY_FUNCTION.format(__A , __A ) else: return DUMMY_CLASS.format(__A , __A ) def _lowerCAmelCase( __A=None ): if backend_specific_objects is None: UpperCAmelCase = read_init() # For special correspondence backend to module name as used in the function requires_modulename UpperCAmelCase = {} for backend, objects in backend_specific_objects.items(): UpperCAmelCase = "[" + ", ".join(F"\"{b}\"" for b in backend.split("_and_" ) ) + "]" UpperCAmelCase = "# This file is autogenerated by the command `make fix-copies`, do not edit.\n" dummy_file += "from ..utils import DummyObject, requires_backends\n\n" dummy_file += "\n".join([create_dummy_object(__A , __A ) for o in objects] ) UpperCAmelCase = dummy_file return dummy_files def _lowerCAmelCase( __A=False ): UpperCAmelCase = create_dummy_files() # For special correspondence backend to shortcut as used in utils/dummy_xxx_objects.py UpperCAmelCase = {"torch": "pt"} # Locate actual dummy modules and read their content. UpperCAmelCase = os.path.join(__A , "utils" ) UpperCAmelCase = { backend: os.path.join(__A , F"dummy_{short_names.get(__A , __A )}_objects.py" ) for backend in dummy_files.keys() } UpperCAmelCase = {} for backend, file_path in dummy_file_paths.items(): if os.path.isfile(__A ): with open(__A , "r" , encoding="utf-8" , newline="\n" ) as f: UpperCAmelCase = f.read() else: UpperCAmelCase = "" for backend in dummy_files.keys(): if dummy_files[backend] != actual_dummies[backend]: if overwrite: print( F"Updating diffusers.utils.dummy_{short_names.get(__A , __A )}_objects.py as the main " "__init__ has new objects." ) with open(dummy_file_paths[backend] , "w" , encoding="utf-8" , newline="\n" ) as f: f.write(dummy_files[backend] ) else: raise ValueError( "The main __init__ has objects that are not present in " F"diffusers.utils.dummy_{short_names.get(__A , __A )}_objects.py. Run `make fix-copies` " "to fix this." ) if __name__ == "__main__": lowerCAmelCase__ = argparse.ArgumentParser() parser.add_argument("--fix_and_overwrite", action="store_true", help="Whether to fix inconsistencies.") lowerCAmelCase__ = parser.parse_args() check_dummies(args.fix_and_overwrite)
700
import copy import os import cva import numpy as np from matplotlib import pyplot as plt class __magic_name__ : def __init__( self : Optional[int] ) -> Optional[Any]: UpperCAmelCase = "" UpperCAmelCase = "" UpperCAmelCase = [] UpperCAmelCase = 0 UpperCAmelCase = 2_5_6 UpperCAmelCase = 0 UpperCAmelCase = 0 UpperCAmelCase = 0 UpperCAmelCase = 0 def _UpperCamelCase ( self : Any , lowerCAmelCase__ : Optional[Any] ) -> List[str]: UpperCAmelCase = cva.imread(lowerCAmelCase__ , 0 ) UpperCAmelCase = copy.deepcopy(self.img ) UpperCAmelCase , UpperCAmelCase , UpperCAmelCase = plt.hist(self.img.ravel() , 2_5_6 , [0, 2_5_6] , label="x" ) UpperCAmelCase = np.sum(lowerCAmelCase__ ) for i in range(len(lowerCAmelCase__ ) ): UpperCAmelCase = x[i] / self.k self.sk += prk UpperCAmelCase = (self.L - 1) * self.sk if self.rem != 0: UpperCAmelCase = int(last % last ) UpperCAmelCase = int(last + 1 if self.rem >= 0.5 else last ) self.last_list.append(lowerCAmelCase__ ) UpperCAmelCase = int(np.ma.count(self.img ) / self.img[1].size ) UpperCAmelCase = self.img[1].size for i in range(self.number_of_cols ): for j in range(self.number_of_rows ): UpperCAmelCase = self.img[j][i] if num != self.last_list[num]: UpperCAmelCase = self.last_list[num] cva.imwrite("output_data/output.jpg" , self.img ) def _UpperCamelCase ( self : str ) -> int: plt.hist(self.img.ravel() , 2_5_6 , [0, 2_5_6] ) def _UpperCamelCase ( self : Dict ) -> Optional[Any]: cva.imshow("Output-Image" , self.img ) cva.imshow("Input-Image" , self.original_image ) cva.waitKey(5_0_0_0 ) cva.destroyAllWindows() if __name__ == "__main__": lowerCAmelCase__ = os.path.join(os.path.basename(__file__), "image_data/input.jpg") lowerCAmelCase__ = ConstantStretch() stretcher.stretch(file_path) stretcher.plot_histogram() stretcher.show_image()
1
0
from ..utils import DummyObject, requires_backends class __magic_name__ ( metaclass=_snake_case ): UpperCAmelCase = ["""flax""", """transformers"""] def __init__( self : str , *lowerCAmelCase__ : Optional[Any] , **lowerCAmelCase__ : Any ) -> Union[str, Any]: requires_backends(self , ["flax", "transformers"] ) @classmethod def _UpperCamelCase ( cls : Union[str, Any] , *lowerCAmelCase__ : List[str] , **lowerCAmelCase__ : Tuple ) -> Optional[int]: requires_backends(cls , ["flax", "transformers"] ) @classmethod def _UpperCamelCase ( cls : List[str] , *lowerCAmelCase__ : str , **lowerCAmelCase__ : Union[str, Any] ) -> Any: requires_backends(cls , ["flax", "transformers"] ) class __magic_name__ ( metaclass=_snake_case ): UpperCAmelCase = ["""flax""", """transformers"""] def __init__( self : List[str] , *lowerCAmelCase__ : List[Any] , **lowerCAmelCase__ : Union[str, Any] ) -> Optional[Any]: requires_backends(self , ["flax", "transformers"] ) @classmethod def _UpperCamelCase ( cls : List[str] , *lowerCAmelCase__ : int , **lowerCAmelCase__ : Any ) -> Optional[Any]: requires_backends(cls , ["flax", "transformers"] ) @classmethod def _UpperCamelCase ( cls : List[str] , *lowerCAmelCase__ : List[str] , **lowerCAmelCase__ : Tuple ) -> int: requires_backends(cls , ["flax", "transformers"] ) class __magic_name__ ( metaclass=_snake_case ): UpperCAmelCase = ["""flax""", """transformers"""] def __init__( self : Dict , *lowerCAmelCase__ : Union[str, Any] , **lowerCAmelCase__ : List[Any] ) -> Optional[int]: requires_backends(self , ["flax", "transformers"] ) @classmethod def _UpperCamelCase ( cls : Tuple , *lowerCAmelCase__ : Optional[int] , **lowerCAmelCase__ : List[Any] ) -> Tuple: requires_backends(cls , ["flax", "transformers"] ) @classmethod def _UpperCamelCase ( cls : Any , *lowerCAmelCase__ : Union[str, Any] , **lowerCAmelCase__ : Any ) -> Optional[int]: requires_backends(cls , ["flax", "transformers"] ) class __magic_name__ ( metaclass=_snake_case ): UpperCAmelCase = ["""flax""", """transformers"""] def __init__( self : Optional[int] , *lowerCAmelCase__ : Tuple , **lowerCAmelCase__ : List[Any] ) -> int: requires_backends(self , ["flax", "transformers"] ) @classmethod def _UpperCamelCase ( cls : List[Any] , *lowerCAmelCase__ : int , **lowerCAmelCase__ : Optional[Any] ) -> List[Any]: requires_backends(cls , ["flax", "transformers"] ) @classmethod def _UpperCamelCase ( cls : Optional[Any] , *lowerCAmelCase__ : Tuple , **lowerCAmelCase__ : int ) -> List[str]: requires_backends(cls , ["flax", "transformers"] )
701
import json import os import unittest from transformers import BatchEncoding, LEDTokenizer, LEDTokenizerFast from transformers.models.led.tokenization_led import VOCAB_FILES_NAMES from transformers.testing_utils import require_tokenizers, require_torch from transformers.utils import cached_property from ...test_tokenization_common import TokenizerTesterMixin @require_tokenizers class __magic_name__ ( _snake_case , unittest.TestCase ): UpperCAmelCase = LEDTokenizer UpperCAmelCase = LEDTokenizerFast UpperCAmelCase = True def _UpperCamelCase ( self : Tuple ) -> Union[str, Any]: super().setUp() UpperCAmelCase = [ "l", "o", "w", "e", "r", "s", "t", "i", "d", "n", "\u0120", "\u0120l", "\u0120n", "\u0120lo", "\u0120low", "er", "\u0120lowest", "\u0120newer", "\u0120wider", "<unk>", ] UpperCAmelCase = dict(zip(lowerCAmelCase__ , range(len(lowerCAmelCase__ ) ) ) ) UpperCAmelCase = ["#version: 0.2", "\u0120 l", "\u0120l o", "\u0120lo w", "e r", ""] UpperCAmelCase = {"unk_token": "<unk>"} UpperCAmelCase = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES["vocab_file"] ) UpperCAmelCase = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES["merges_file"] ) with open(self.vocab_file , "w" , encoding="utf-8" ) as fp: fp.write(json.dumps(lowerCAmelCase__ ) + "\n" ) with open(self.merges_file , "w" , encoding="utf-8" ) as fp: fp.write("\n".join(lowerCAmelCase__ ) ) def _UpperCamelCase ( self : Union[str, Any] , **lowerCAmelCase__ : Optional[int] ) -> Optional[int]: kwargs.update(self.special_tokens_map ) return self.tokenizer_class.from_pretrained(self.tmpdirname , **lowerCAmelCase__ ) def _UpperCamelCase ( self : str , **lowerCAmelCase__ : str ) -> Optional[int]: kwargs.update(self.special_tokens_map ) return self.rust_tokenizer_class.from_pretrained(self.tmpdirname , **lowerCAmelCase__ ) def _UpperCamelCase ( self : List[str] , lowerCAmelCase__ : List[Any] ) -> List[Any]: return "lower newer", "lower newer" @cached_property def _UpperCamelCase ( self : Dict ) -> str: return LEDTokenizer.from_pretrained("allenai/led-base-16384" ) @cached_property def _UpperCamelCase ( self : int ) -> Tuple: return LEDTokenizerFast.from_pretrained("allenai/led-base-16384" ) @require_torch def _UpperCamelCase ( self : Tuple ) -> List[str]: UpperCAmelCase = ["A long paragraph for summarization.", "Another paragraph for summarization."] UpperCAmelCase = [0, 2_5_0, 2_5_1, 1_7_8_1_8, 1_3, 3_9_1_8_6, 1_9_3_8, 4, 2] for tokenizer in [self.default_tokenizer, self.default_tokenizer_fast]: UpperCAmelCase = tokenizer(lowerCAmelCase__ , max_length=len(lowerCAmelCase__ ) , padding=lowerCAmelCase__ , return_tensors="pt" ) self.assertIsInstance(lowerCAmelCase__ , lowerCAmelCase__ ) self.assertEqual((2, 9) , batch.input_ids.shape ) self.assertEqual((2, 9) , batch.attention_mask.shape ) UpperCAmelCase = batch.input_ids.tolist()[0] self.assertListEqual(lowerCAmelCase__ , lowerCAmelCase__ ) @require_torch def _UpperCamelCase ( self : Union[str, Any] ) -> List[Any]: UpperCAmelCase = ["A long paragraph for summarization.", "Another paragraph for summarization."] for tokenizer in [self.default_tokenizer, self.default_tokenizer_fast]: UpperCAmelCase = tokenizer(lowerCAmelCase__ , padding=lowerCAmelCase__ , return_tensors="pt" ) self.assertIn("input_ids" , lowerCAmelCase__ ) self.assertIn("attention_mask" , lowerCAmelCase__ ) self.assertNotIn("labels" , lowerCAmelCase__ ) self.assertNotIn("decoder_attention_mask" , lowerCAmelCase__ ) @require_torch def _UpperCamelCase ( self : int ) -> int: UpperCAmelCase = [ "Summary of the text.", "Another summary.", ] for tokenizer in [self.default_tokenizer, self.default_tokenizer_fast]: UpperCAmelCase = tokenizer(text_target=lowerCAmelCase__ , max_length=3_2 , padding="max_length" , return_tensors="pt" ) self.assertEqual(3_2 , targets["input_ids"].shape[1] ) @require_torch def _UpperCamelCase ( self : Any ) -> int: for tokenizer in [self.default_tokenizer, self.default_tokenizer_fast]: UpperCAmelCase = tokenizer( ["I am a small frog" * 1_0_2_4, "I am a small frog"] , padding=lowerCAmelCase__ , truncation=lowerCAmelCase__ , return_tensors="pt" ) self.assertIsInstance(lowerCAmelCase__ , lowerCAmelCase__ ) self.assertEqual(batch.input_ids.shape , (2, 5_1_2_2) ) @require_torch def _UpperCamelCase ( self : Dict ) -> Tuple: UpperCAmelCase = ["A long paragraph for summarization."] UpperCAmelCase = [ "Summary of the text.", ] for tokenizer in [self.default_tokenizer, self.default_tokenizer_fast]: UpperCAmelCase = tokenizer(lowerCAmelCase__ , return_tensors="pt" ) UpperCAmelCase = tokenizer(text_target=lowerCAmelCase__ , return_tensors="pt" ) UpperCAmelCase = inputs["input_ids"] UpperCAmelCase = targets["input_ids"] self.assertTrue((input_ids[:, 0] == tokenizer.bos_token_id).all().item() ) self.assertTrue((labels[:, 0] == tokenizer.bos_token_id).all().item() ) self.assertTrue((input_ids[:, -1] == tokenizer.eos_token_id).all().item() ) self.assertTrue((labels[:, -1] == tokenizer.eos_token_id).all().item() ) @require_torch def _UpperCamelCase ( self : Optional[int] ) -> Optional[Any]: for tokenizer in [self.default_tokenizer, self.default_tokenizer_fast]: UpperCAmelCase = ["Summary of the text.", "Another summary."] UpperCAmelCase = [[0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, -1, -1]] UpperCAmelCase = tokenizer(lowerCAmelCase__ , padding=lowerCAmelCase__ ) UpperCAmelCase = [[0] * len(lowerCAmelCase__ ) for x in encoded_output["input_ids"]] UpperCAmelCase = tokenizer.pad(lowerCAmelCase__ ) self.assertSequenceEqual(outputs["global_attention_mask"] , lowerCAmelCase__ ) def _UpperCamelCase ( self : List[str] ) -> int: pass def _UpperCamelCase ( self : Optional[Any] ) -> Union[str, Any]: for tokenizer, pretrained_name, kwargs in self.tokenizers_list: with self.subTest(f"{tokenizer.__class__.__name__} ({pretrained_name})" ): UpperCAmelCase = self.rust_tokenizer_class.from_pretrained(lowerCAmelCase__ , **lowerCAmelCase__ ) UpperCAmelCase = self.tokenizer_class.from_pretrained(lowerCAmelCase__ , **lowerCAmelCase__ ) UpperCAmelCase = "A, <mask> AllenNLP sentence." UpperCAmelCase = tokenizer_r.encode_plus(lowerCAmelCase__ , add_special_tokens=lowerCAmelCase__ , return_token_type_ids=lowerCAmelCase__ ) UpperCAmelCase = tokenizer_p.encode_plus(lowerCAmelCase__ , add_special_tokens=lowerCAmelCase__ , return_token_type_ids=lowerCAmelCase__ ) self.assertEqual(sum(tokens_r["token_type_ids"] ) , sum(tokens_p["token_type_ids"] ) ) self.assertEqual( sum(tokens_r["attention_mask"] ) / len(tokens_r["attention_mask"] ) , sum(tokens_p["attention_mask"] ) / len(tokens_p["attention_mask"] ) , ) UpperCAmelCase = tokenizer_r.convert_ids_to_tokens(tokens_r["input_ids"] ) UpperCAmelCase = tokenizer_p.convert_ids_to_tokens(tokens_p["input_ids"] ) self.assertSequenceEqual(tokens_p["input_ids"] , [0, 2_5_0, 6, 5_0_2_6_4, 3_8_2_3, 4_8_7, 2_1_9_9_2, 3_6_4_5, 4, 2] ) self.assertSequenceEqual(tokens_r["input_ids"] , [0, 2_5_0, 6, 5_0_2_6_4, 3_8_2_3, 4_8_7, 2_1_9_9_2, 3_6_4_5, 4, 2] ) self.assertSequenceEqual( lowerCAmelCase__ , ["<s>", "A", ",", "<mask>", "ĠAllen", "N", "LP", "Ġsentence", ".", "</s>"] ) self.assertSequenceEqual( lowerCAmelCase__ , ["<s>", "A", ",", "<mask>", "ĠAllen", "N", "LP", "Ġsentence", ".", "</s>"] )
1
0
import gc import unittest import numpy as np import torch from diffusers import AutoencoderKL, DDIMScheduler, DiTPipeline, DPMSolverMultistepScheduler, TransformeraDModel from diffusers.utils import is_xformers_available, load_numpy, slow, torch_device from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu from ..pipeline_params import ( CLASS_CONDITIONED_IMAGE_GENERATION_BATCH_PARAMS, CLASS_CONDITIONED_IMAGE_GENERATION_PARAMS, ) from ..test_pipelines_common import PipelineTesterMixin enable_full_determinism() class __magic_name__ ( _snake_case , unittest.TestCase ): UpperCAmelCase = DiTPipeline UpperCAmelCase = CLASS_CONDITIONED_IMAGE_GENERATION_PARAMS UpperCAmelCase = PipelineTesterMixin.required_optional_params - { """latents""", """num_images_per_prompt""", """callback""", """callback_steps""", } UpperCAmelCase = CLASS_CONDITIONED_IMAGE_GENERATION_BATCH_PARAMS UpperCAmelCase = False def _UpperCamelCase ( self : List[Any] ) -> Any: torch.manual_seed(0 ) UpperCAmelCase = TransformeraDModel( sample_size=1_6 , num_layers=2 , patch_size=4 , attention_head_dim=8 , num_attention_heads=2 , in_channels=4 , out_channels=8 , attention_bias=lowerCAmelCase__ , activation_fn="gelu-approximate" , num_embeds_ada_norm=1_0_0_0 , norm_type="ada_norm_zero" , norm_elementwise_affine=lowerCAmelCase__ , ) UpperCAmelCase = AutoencoderKL() UpperCAmelCase = DDIMScheduler() UpperCAmelCase = {"transformer": transformer.eval(), "vae": vae.eval(), "scheduler": scheduler} return components def _UpperCamelCase ( self : int , lowerCAmelCase__ : Optional[int] , lowerCAmelCase__ : List[Any]=0 ) -> Tuple: if str(lowerCAmelCase__ ).startswith("mps" ): UpperCAmelCase = torch.manual_seed(lowerCAmelCase__ ) else: UpperCAmelCase = torch.Generator(device=lowerCAmelCase__ ).manual_seed(lowerCAmelCase__ ) UpperCAmelCase = { "class_labels": [1], "generator": generator, "num_inference_steps": 2, "output_type": "numpy", } return inputs def _UpperCamelCase ( self : Union[str, Any] ) -> Any: UpperCAmelCase = "cpu" UpperCAmelCase = self.get_dummy_components() UpperCAmelCase = self.pipeline_class(**lowerCAmelCase__ ) pipe.to(lowerCAmelCase__ ) pipe.set_progress_bar_config(disable=lowerCAmelCase__ ) UpperCAmelCase = self.get_dummy_inputs(lowerCAmelCase__ ) UpperCAmelCase = pipe(**lowerCAmelCase__ ).images UpperCAmelCase = image[0, -3:, -3:, -1] self.assertEqual(image.shape , (1, 1_6, 1_6, 3) ) UpperCAmelCase = np.array([0.2_946, 0.6_601, 0.4_329, 0.3_296, 0.4_144, 0.5_319, 0.7_273, 0.5_013, 0.4_457] ) UpperCAmelCase = np.abs(image_slice.flatten() - expected_slice ).max() self.assertLessEqual(lowerCAmelCase__ , 1e-3 ) def _UpperCamelCase ( self : Optional[int] ) -> Union[str, Any]: self._test_inference_batch_single_identical(relax_max_difference=lowerCAmelCase__ , expected_max_diff=1e-3 ) @unittest.skipIf( torch_device != "cuda" or not is_xformers_available() , reason="XFormers attention is only available with CUDA and `xformers` installed" , ) def _UpperCamelCase ( self : Any ) -> str: self._test_xformers_attention_forwardGenerator_pass(expected_max_diff=1e-3 ) @require_torch_gpu @slow class __magic_name__ ( unittest.TestCase ): def _UpperCamelCase ( self : str ) -> Dict: super().tearDown() gc.collect() torch.cuda.empty_cache() def _UpperCamelCase ( self : List[str] ) -> Optional[Any]: UpperCAmelCase = torch.manual_seed(0 ) UpperCAmelCase = DiTPipeline.from_pretrained("facebook/DiT-XL-2-256" ) pipe.to("cuda" ) UpperCAmelCase = ["vase", "umbrella", "white shark", "white wolf"] UpperCAmelCase = pipe.get_label_ids(lowerCAmelCase__ ) UpperCAmelCase = pipe(lowerCAmelCase__ , generator=lowerCAmelCase__ , num_inference_steps=4_0 , output_type="np" ).images for word, image in zip(lowerCAmelCase__ , lowerCAmelCase__ ): UpperCAmelCase = load_numpy( f"https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/dit/{word}.npy" ) assert np.abs((expected_image - image).max() ) < 1e-2 def _UpperCamelCase ( self : Optional[int] ) -> List[Any]: UpperCAmelCase = DiTPipeline.from_pretrained("facebook/DiT-XL-2-512" ) UpperCAmelCase = DPMSolverMultistepScheduler.from_config(pipe.scheduler.config ) pipe.to("cuda" ) UpperCAmelCase = ["vase", "umbrella"] UpperCAmelCase = pipe.get_label_ids(lowerCAmelCase__ ) UpperCAmelCase = torch.manual_seed(0 ) UpperCAmelCase = pipe(lowerCAmelCase__ , generator=lowerCAmelCase__ , num_inference_steps=2_5 , output_type="np" ).images for word, image in zip(lowerCAmelCase__ , lowerCAmelCase__ ): UpperCAmelCase = load_numpy( "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main" f"/dit/{word}_512.npy" ) assert np.abs((expected_image - image).max() ) < 1e-1
702
import numpy as np from nltk.translate import meteor_score import datasets from datasets.config import importlib_metadata, version lowerCAmelCase__ = version.parse(importlib_metadata.version("nltk")) if NLTK_VERSION >= version.Version("3.6.4"): from nltk import word_tokenize lowerCAmelCase__ = "\\n@inproceedings{banarjee2005,\n title = {{METEOR}: An Automatic Metric for {MT} Evaluation with Improved Correlation with Human Judgments},\n author = {Banerjee, Satanjeev and Lavie, Alon},\n booktitle = {Proceedings of the {ACL} Workshop on Intrinsic and Extrinsic Evaluation Measures for Machine Translation and/or Summarization},\n month = jun,\n year = {2005},\n address = {Ann Arbor, Michigan},\n publisher = {Association for Computational Linguistics},\n url = {https://www.aclweb.org/anthology/W05-0909},\n pages = {65--72},\n}\n" lowerCAmelCase__ = "\\nMETEOR, an automatic metric for machine translation evaluation\nthat is based on a generalized concept of unigram matching between the\nmachine-produced translation and human-produced reference translations.\nUnigrams can be matched based on their surface forms, stemmed forms,\nand meanings; furthermore, METEOR can be easily extended to include more\nadvanced matching strategies. Once all generalized unigram matches\nbetween the two strings have been found, METEOR computes a score for\nthis matching using a combination of unigram-precision, unigram-recall, and\na measure of fragmentation that is designed to directly capture how\nwell-ordered the matched words in the machine translation are in relation\nto the reference.\n\nMETEOR gets an R correlation value of 0.347 with human evaluation on the Arabic\ndata and 0.331 on the Chinese data. This is shown to be an improvement on\nusing simply unigram-precision, unigram-recall and their harmonic F1\ncombination.\n" lowerCAmelCase__ = "\nComputes METEOR score of translated segments against one or more references.\nArgs:\n predictions: list of predictions to score. Each prediction\n should be a string with tokens separated by spaces.\n references: list of reference for each prediction. Each\n reference should be a string with tokens separated by spaces.\n alpha: Parameter for controlling relative weights of precision and recall. default: 0.9\n beta: Parameter for controlling shape of penalty as a function of fragmentation. default: 3\n gamma: Relative weight assigned to fragmentation penalty. default: 0.5\nReturns:\n 'meteor': meteor score.\nExamples:\n\n >>> meteor = datasets.load_metric('meteor')\n >>> predictions = [\"It is a guide to action which ensures that the military always obeys the commands of the party\"]\n >>> references = [\"It is a guide to action that ensures that the military will forever heed Party commands\"]\n >>> results = meteor.compute(predictions=predictions, references=references)\n >>> print(round(results[\"meteor\"], 4))\n 0.6944\n" @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class __magic_name__ ( datasets.Metric ): def _UpperCamelCase ( self : int ) -> str: return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { "predictions": datasets.Value("string" , id="sequence" ), "references": datasets.Value("string" , id="sequence" ), } ) , codebase_urls=["https://github.com/nltk/nltk/blob/develop/nltk/translate/meteor_score.py"] , reference_urls=[ "https://www.nltk.org/api/nltk.translate.html#module-nltk.translate.meteor_score", "https://en.wikipedia.org/wiki/METEOR", ] , ) def _UpperCamelCase ( self : Dict , lowerCAmelCase__ : List[Any] ) -> Dict: import nltk nltk.download("wordnet" ) if NLTK_VERSION >= version.Version("3.6.5" ): nltk.download("punkt" ) if NLTK_VERSION >= version.Version("3.6.6" ): nltk.download("omw-1.4" ) def _UpperCamelCase ( self : Union[str, Any] , lowerCAmelCase__ : int , lowerCAmelCase__ : Dict , lowerCAmelCase__ : Any=0.9 , lowerCAmelCase__ : Tuple=3 , lowerCAmelCase__ : Optional[int]=0.5 ) -> Any: if NLTK_VERSION >= version.Version("3.6.5" ): UpperCAmelCase = [ meteor_score.single_meteor_score( word_tokenize(lowerCAmelCase__ ) , word_tokenize(lowerCAmelCase__ ) , alpha=lowerCAmelCase__ , beta=lowerCAmelCase__ , gamma=lowerCAmelCase__ ) for ref, pred in zip(lowerCAmelCase__ , lowerCAmelCase__ ) ] else: UpperCAmelCase = [ meteor_score.single_meteor_score(lowerCAmelCase__ , lowerCAmelCase__ , alpha=lowerCAmelCase__ , beta=lowerCAmelCase__ , gamma=lowerCAmelCase__ ) for ref, pred in zip(lowerCAmelCase__ , lowerCAmelCase__ ) ] return {"meteor": np.mean(lowerCAmelCase__ )}
1
0
# This is the module that test_patching.py uses to test patch_submodule() import os # noqa: this is just for tests import os as renamed_os # noqa: this is just for tests from os import path # noqa: this is just for tests from os import path as renamed_path # noqa: this is just for tests from os.path import join # noqa: this is just for tests from os.path import join as renamed_join # noqa: this is just for tests lowerCAmelCase__ : Optional[int] = open # noqa: we just need to have a builtin inside this module to test it properly
703
from ...configuration_utils import PretrainedConfig from ...utils import logging lowerCAmelCase__ = logging.get_logger(__name__) lowerCAmelCase__ = { "unc-nlp/lxmert-base-uncased": "https://huggingface.co/unc-nlp/lxmert-base-uncased/resolve/main/config.json", } class __magic_name__ ( _snake_case ): UpperCAmelCase = """lxmert""" UpperCAmelCase = {} def __init__( self : int , lowerCAmelCase__ : Any=3_0_5_2_2 , lowerCAmelCase__ : List[str]=7_6_8 , lowerCAmelCase__ : Union[str, Any]=1_2 , lowerCAmelCase__ : List[Any]=9_5_0_0 , lowerCAmelCase__ : Any=1_6_0_0 , lowerCAmelCase__ : Union[str, Any]=4_0_0 , lowerCAmelCase__ : Tuple=3_0_7_2 , lowerCAmelCase__ : Dict="gelu" , lowerCAmelCase__ : Tuple=0.1 , lowerCAmelCase__ : Tuple=0.1 , lowerCAmelCase__ : int=5_1_2 , lowerCAmelCase__ : List[str]=2 , lowerCAmelCase__ : List[str]=0.02 , lowerCAmelCase__ : str=1e-1_2 , lowerCAmelCase__ : str=9 , lowerCAmelCase__ : int=5 , lowerCAmelCase__ : Optional[int]=5 , lowerCAmelCase__ : List[Any]=2_0_4_8 , lowerCAmelCase__ : Any=4 , lowerCAmelCase__ : Dict=6.67 , lowerCAmelCase__ : Any=True , lowerCAmelCase__ : Union[str, Any]=True , lowerCAmelCase__ : Any=True , lowerCAmelCase__ : Tuple=True , lowerCAmelCase__ : Optional[Any]=True , lowerCAmelCase__ : Optional[int]=True , lowerCAmelCase__ : Tuple=True , **lowerCAmelCase__ : List[Any] , ) -> Dict: UpperCAmelCase = vocab_size UpperCAmelCase = hidden_size 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 = num_qa_labels UpperCAmelCase = num_object_labels UpperCAmelCase = num_attr_labels UpperCAmelCase = l_layers UpperCAmelCase = x_layers UpperCAmelCase = r_layers UpperCAmelCase = visual_feat_dim UpperCAmelCase = visual_pos_dim UpperCAmelCase = visual_loss_normalizer UpperCAmelCase = task_matched UpperCAmelCase = task_mask_lm UpperCAmelCase = task_obj_predict UpperCAmelCase = task_qa UpperCAmelCase = visual_obj_loss UpperCAmelCase = visual_attr_loss UpperCAmelCase = visual_feat_loss UpperCAmelCase = {"vision": r_layers, "cross_encoder": x_layers, "language": l_layers} super().__init__(**lowerCAmelCase__ )
1
0
import argparse import requests import torch # pip3 install salesforce-lavis # I'm actually installing a slightly modified version: pip3 install git+https://github.com/nielsrogge/LAVIS.git@fix_lavis from lavis.models import load_model_and_preprocess from PIL import Image from transformers import ( AutoTokenizer, BlipaConfig, BlipaForConditionalGeneration, BlipaProcessor, BlipaVisionConfig, BlipImageProcessor, OPTConfig, TaConfig, ) from transformers.utils.constants import OPENAI_CLIP_MEAN, OPENAI_CLIP_STD def _lowerCAmelCase( ): UpperCAmelCase = "https://storage.googleapis.com/sfr-vision-language-research/LAVIS/assets/merlion.png" UpperCAmelCase = Image.open(requests.get(__A , stream=__A ).raw ).convert("RGB" ) return image def _lowerCAmelCase( __A ): UpperCAmelCase = [] # fmt: off # vision encoder rename_keys.append(("visual_encoder.cls_token", "vision_model.embeddings.class_embedding") ) rename_keys.append(("visual_encoder.pos_embed", "vision_model.embeddings.position_embedding") ) rename_keys.append(("visual_encoder.patch_embed.proj.weight", "vision_model.embeddings.patch_embedding.weight") ) rename_keys.append(("visual_encoder.patch_embed.proj.bias", "vision_model.embeddings.patch_embedding.bias") ) rename_keys.append(("ln_vision.weight", "vision_model.post_layernorm.weight") ) rename_keys.append(("ln_vision.bias", "vision_model.post_layernorm.bias") ) for i in range(config.vision_config.num_hidden_layers ): rename_keys.append((F"visual_encoder.blocks.{i}.norm1.weight", F"vision_model.encoder.layers.{i}.layer_norm1.weight") ) rename_keys.append((F"visual_encoder.blocks.{i}.norm1.bias", F"vision_model.encoder.layers.{i}.layer_norm1.bias") ) rename_keys.append((F"visual_encoder.blocks.{i}.norm2.weight", F"vision_model.encoder.layers.{i}.layer_norm2.weight") ) rename_keys.append((F"visual_encoder.blocks.{i}.norm2.bias", F"vision_model.encoder.layers.{i}.layer_norm2.bias") ) rename_keys.append((F"visual_encoder.blocks.{i}.attn.qkv.weight", F"vision_model.encoder.layers.{i}.self_attn.qkv.weight") ) rename_keys.append((F"visual_encoder.blocks.{i}.attn.proj.weight", F"vision_model.encoder.layers.{i}.self_attn.projection.weight",) ) rename_keys.append((F"visual_encoder.blocks.{i}.attn.proj.bias", F"vision_model.encoder.layers.{i}.self_attn.projection.bias") ) rename_keys.append((F"visual_encoder.blocks.{i}.mlp.fc1.weight", F"vision_model.encoder.layers.{i}.mlp.fc1.weight") ) rename_keys.append((F"visual_encoder.blocks.{i}.mlp.fc1.bias", F"vision_model.encoder.layers.{i}.mlp.fc1.bias") ) rename_keys.append((F"visual_encoder.blocks.{i}.mlp.fc2.weight", F"vision_model.encoder.layers.{i}.mlp.fc2.weight") ) rename_keys.append((F"visual_encoder.blocks.{i}.mlp.fc2.bias", F"vision_model.encoder.layers.{i}.mlp.fc2.bias") ) # QFormer rename_keys.append(("Qformer.bert.embeddings.LayerNorm.weight", "qformer.layernorm.weight") ) rename_keys.append(("Qformer.bert.embeddings.LayerNorm.bias", "qformer.layernorm.bias") ) # fmt: on return rename_keys def _lowerCAmelCase( __A , __A , __A ): UpperCAmelCase = dct.pop(__A ) UpperCAmelCase = val def _lowerCAmelCase( __A , __A ): for i in range(config.vision_config.num_hidden_layers ): # read in original q and v biases UpperCAmelCase = state_dict.pop(F"visual_encoder.blocks.{i}.attn.q_bias" ) UpperCAmelCase = state_dict.pop(F"visual_encoder.blocks.{i}.attn.v_bias" ) # next, set bias in the state dict UpperCAmelCase = torch.cat((q_bias, torch.zeros_like(__A , requires_grad=__A ), v_bias) ) UpperCAmelCase = qkv_bias def _lowerCAmelCase( __A , __A ): UpperCAmelCase = 364 if "coco" in model_name else 224 UpperCAmelCase = BlipaVisionConfig(image_size=__A ).to_dict() # make sure the models have proper bos_token_id and eos_token_id set (important for generation) # seems like flan-T5 models don't have bos_token_id properly set? if "opt-2.7b" in model_name: UpperCAmelCase = OPTConfig.from_pretrained("facebook/opt-2.7b" , eos_token_id=__A ).to_dict() elif "opt-6.7b" in model_name: UpperCAmelCase = OPTConfig.from_pretrained("facebook/opt-6.7b" , eos_token_id=__A ).to_dict() elif "t5-xl" in model_name: UpperCAmelCase = TaConfig.from_pretrained("google/flan-t5-xl" , dense_act_fn="gelu" , bos_token_id=1 ).to_dict() elif "t5-xxl" in model_name: UpperCAmelCase = TaConfig.from_pretrained("google/flan-t5-xxl" , dense_act_fn="gelu" , bos_token_id=1 ).to_dict() UpperCAmelCase = BlipaConfig(vision_config=__A , text_config=__A ) return config, image_size @torch.no_grad() def _lowerCAmelCase( __A , __A=None , __A=False ): UpperCAmelCase = ( AutoTokenizer.from_pretrained("facebook/opt-2.7b" ) if "opt" in model_name else AutoTokenizer.from_pretrained("google/flan-t5-xl" ) ) UpperCAmelCase = tokenizer("\n" , add_special_tokens=__A ).input_ids[0] UpperCAmelCase , UpperCAmelCase = get_blipa_config(__A , eos_token_id=__A ) UpperCAmelCase = BlipaForConditionalGeneration(__A ).eval() UpperCAmelCase = { "blip2-opt-2.7b": ("blip2_opt", "pretrain_opt2.7b"), "blip2-opt-6.7b": ("blip2_opt", "pretrain_opt6.7b"), "blip2-opt-2.7b-coco": ("blip2_opt", "caption_coco_opt2.7b"), "blip2-opt-6.7b-coco": ("blip2_opt", "caption_coco_opt6.7b"), "blip2-flan-t5-xl": ("blip2_t5", "pretrain_flant5xl"), "blip2-flan-t5-xl-coco": ("blip2_t5", "caption_coco_flant5xl"), "blip2-flan-t5-xxl": ("blip2_t5", "pretrain_flant5xxl"), } UpperCAmelCase , UpperCAmelCase = model_name_to_original[model_name] # load original model print("Loading original model..." ) UpperCAmelCase = "cuda" if torch.cuda.is_available() else "cpu" UpperCAmelCase , UpperCAmelCase , UpperCAmelCase = load_model_and_preprocess( name=__A , model_type=__A , is_eval=__A , device=__A ) original_model.eval() print("Done!" ) # update state dict keys UpperCAmelCase = original_model.state_dict() UpperCAmelCase = create_rename_keys(__A ) for src, dest in rename_keys: rename_key(__A , __A , __A ) # some keys can be renamed efficiently for key, val in state_dict.copy().items(): UpperCAmelCase = state_dict.pop(__A ) if key.startswith("Qformer.bert" ): UpperCAmelCase = key.replace("Qformer.bert" , "qformer" ) if "attention.self" in key: UpperCAmelCase = key.replace("self" , "attention" ) if "opt_proj" in key: UpperCAmelCase = key.replace("opt_proj" , "language_projection" ) if "t5_proj" in key: UpperCAmelCase = key.replace("t5_proj" , "language_projection" ) if key.startswith("opt" ): UpperCAmelCase = key.replace("opt" , "language" ) if key.startswith("t5" ): UpperCAmelCase = key.replace("t5" , "language" ) UpperCAmelCase = val # read in qv biases read_in_q_v_bias(__A , __A ) UpperCAmelCase , UpperCAmelCase = hf_model.load_state_dict(__A , strict=__A ) assert len(__A ) == 0 assert unexpected_keys == ["qformer.embeddings.position_ids"] UpperCAmelCase = load_demo_image() UpperCAmelCase = vis_processors["eval"](__A ).unsqueeze(0 ).to(__A ) UpperCAmelCase = tokenizer(["\n"] , return_tensors="pt" ).input_ids.to(__A ) # create processor UpperCAmelCase = BlipImageProcessor( size={"height": image_size, "width": image_size} , image_mean=__A , image_std=__A ) UpperCAmelCase = BlipaProcessor(image_processor=__A , tokenizer=__A ) UpperCAmelCase = processor(images=__A , return_tensors="pt" ).pixel_values.to(__A ) # make sure processor creates exact same pixel values assert torch.allclose(__A , __A ) original_model.to(__A ) hf_model.to(__A ) with torch.no_grad(): if "opt" in model_name: UpperCAmelCase = original_model({"image": original_pixel_values, "text_input": [""]} ).logits UpperCAmelCase = hf_model(__A , __A ).logits else: UpperCAmelCase = original_model( {"image": original_pixel_values, "text_input": ["\n"], "text_output": ["\n"]} ).logits UpperCAmelCase = input_ids.masked_fill(input_ids == tokenizer.pad_token_id , -100 ) UpperCAmelCase = hf_model(__A , __A , labels=__A ).logits assert original_logits.shape == logits.shape print("First values of original logits:" , original_logits[0, :3, :3] ) print("First values of HF logits:" , logits[0, :3, :3] ) # assert values if model_name == "blip2-flan-t5-xl": UpperCAmelCase = torch.tensor( [[-41.5850, -4.4440, -8.9922], [-47.4322, -5.9143, -1.7340]] , device=__A ) assert torch.allclose(logits[0, :3, :3] , __A , atol=1E-4 ) elif model_name == "blip2-flan-t5-xl-coco": UpperCAmelCase = torch.tensor( [[-57.0109, -9.8967, -12.6280], [-68.6578, -12.7191, -10.5065]] , device=__A ) else: # cast to same type UpperCAmelCase = logits.dtype assert torch.allclose(original_logits.to(__A ) , __A , atol=1E-2 ) print("Looks ok!" ) print("Generating a caption..." ) UpperCAmelCase = "" UpperCAmelCase = tokenizer(__A , return_tensors="pt" ).input_ids.to(__A ) UpperCAmelCase = original_model.generate({"image": original_pixel_values} ) UpperCAmelCase = hf_model.generate( __A , __A , do_sample=__A , num_beams=5 , max_length=30 , min_length=1 , top_p=0.9 , repetition_penalty=1.0 , length_penalty=1.0 , temperature=1 , ) print("Original generation:" , __A ) UpperCAmelCase = input_ids.shape[1] UpperCAmelCase = processor.batch_decode(outputs[:, prompt_length:] , skip_special_tokens=__A ) UpperCAmelCase = [text.strip() for text in output_text] print("HF generation:" , __A ) if pytorch_dump_folder_path is not None: processor.save_pretrained(__A ) hf_model.save_pretrained(__A ) if push_to_hub: processor.push_to_hub(F"nielsr/{model_name}" ) hf_model.push_to_hub(F"nielsr/{model_name}" ) if __name__ == "__main__": lowerCAmelCase__ = argparse.ArgumentParser() lowerCAmelCase__ = [ "blip2-opt-2.7b", "blip2-opt-6.7b", "blip2-opt-2.7b-coco", "blip2-opt-6.7b-coco", "blip2-flan-t5-xl", "blip2-flan-t5-xl-coco", "blip2-flan-t5-xxl", ] parser.add_argument( "--model_name", default="blip2-opt-2.7b", choices=choices, type=str, help="Path to hf config.json of model to convert", ) parser.add_argument("--pytorch_dump_folder_path", default=None, type=str, help="Path to the output PyTorch model.") parser.add_argument( "--push_to_hub", action="store_true", help="Whether to push the model and processor to the hub after converting", ) lowerCAmelCase__ = parser.parse_args() convert_blipa_checkpoint(args.model_name, args.pytorch_dump_folder_path, args.push_to_hub)
704
def _lowerCAmelCase( __A ): UpperCAmelCase = 0 while num > 0: digit_sum += num % 10 num //= 10 return digit_sum def _lowerCAmelCase( __A = 100 ): UpperCAmelCase = 1 UpperCAmelCase = 2 for i in range(2 , max_n + 1 ): UpperCAmelCase = pre_numerator UpperCAmelCase = 2 * i // 3 if i % 3 == 0 else 1 UpperCAmelCase = cur_numerator UpperCAmelCase = e_cont * pre_numerator + temp return sum_digits(__A ) if __name__ == "__main__": print(f"{solution() = }")
1
0
import argparse from transformers import ( TapasConfig, TapasForMaskedLM, TapasForQuestionAnswering, TapasForSequenceClassification, TapasModel, TapasTokenizer, load_tf_weights_in_tapas, ) from transformers.utils import logging logging.set_verbosity_info() def _lowerCAmelCase( __A , __A , __A , __A , __A ): # Initialise PyTorch model. # If you want to convert a checkpoint that uses absolute position embeddings, make sure to set reset_position_index_per_cell of # TapasConfig to False. # initialize configuration from json file UpperCAmelCase = TapasConfig.from_json_file(__A ) # set absolute/relative position embeddings parameter UpperCAmelCase = reset_position_index_per_cell # set remaining parameters of TapasConfig as well as the model based on the task if task == "SQA": UpperCAmelCase = TapasForQuestionAnswering(config=__A ) elif task == "WTQ": # run_task_main.py hparams UpperCAmelCase = 4 UpperCAmelCase = True # hparam_utils.py hparams UpperCAmelCase = 0.664694 UpperCAmelCase = 0.207951 UpperCAmelCase = 0.121194 UpperCAmelCase = True UpperCAmelCase = True UpperCAmelCase = False UpperCAmelCase = 0.0352513 UpperCAmelCase = TapasForQuestionAnswering(config=__A ) elif task == "WIKISQL_SUPERVISED": # run_task_main.py hparams UpperCAmelCase = 4 UpperCAmelCase = False # hparam_utils.py hparams UpperCAmelCase = 36.4519 UpperCAmelCase = 0.903421 UpperCAmelCase = 222.088 UpperCAmelCase = True UpperCAmelCase = True UpperCAmelCase = True UpperCAmelCase = 0.763141 UpperCAmelCase = TapasForQuestionAnswering(config=__A ) elif task == "TABFACT": UpperCAmelCase = TapasForSequenceClassification(config=__A ) elif task == "MLM": UpperCAmelCase = TapasForMaskedLM(config=__A ) elif task == "INTERMEDIATE_PRETRAINING": UpperCAmelCase = TapasModel(config=__A ) else: raise ValueError(F"Task {task} not supported." ) print(F"Building PyTorch model from configuration: {config}" ) # Load weights from tf checkpoint load_tf_weights_in_tapas(__A , __A , __A ) # Save pytorch-model (weights and configuration) print(F"Save PyTorch model to {pytorch_dump_path}" ) model.save_pretrained(__A ) # Save tokenizer files print(F"Save tokenizer files to {pytorch_dump_path}" ) UpperCAmelCase = TapasTokenizer(vocab_file=tf_checkpoint_path[:-10] + "vocab.txt" , model_max_length=512 ) tokenizer.save_pretrained(__A ) print("Used relative position embeddings:" , model.config.reset_position_index_per_cell ) if __name__ == "__main__": lowerCAmelCase__ = argparse.ArgumentParser() # Required parameters parser.add_argument( "--task", default="SQA", type=str, help="Model task for which to convert a checkpoint. Defaults to SQA." ) parser.add_argument( "--reset_position_index_per_cell", default=False, action="store_true", help="Whether to use relative position embeddings or not. Defaults to True.", ) parser.add_argument( "--tf_checkpoint_path", default=None, type=str, required=True, help="Path to the TensorFlow checkpoint path." ) parser.add_argument( "--tapas_config_file", default=None, type=str, required=True, help=( "The config json file corresponding to the pre-trained TAPAS model. \n" "This specifies the model architecture." ), ) parser.add_argument( "--pytorch_dump_path", default=None, type=str, required=True, help="Path to the output PyTorch model." ) lowerCAmelCase__ = parser.parse_args() convert_tf_checkpoint_to_pytorch( args.task, args.reset_position_index_per_cell, args.tf_checkpoint_path, args.tapas_config_file, args.pytorch_dump_path, )
705
from . import ( albert, align, altclip, audio_spectrogram_transformer, auto, autoformer, bark, bart, barthez, bartpho, beit, bert, bert_generation, bert_japanese, bertweet, big_bird, bigbird_pegasus, biogpt, bit, blenderbot, blenderbot_small, blip, blip_a, bloom, bridgetower, byta, camembert, canine, chinese_clip, clap, clip, clipseg, codegen, conditional_detr, convbert, convnext, convnextva, cpm, cpmant, ctrl, cvt, dataavec, deberta, deberta_va, decision_transformer, deformable_detr, deit, deprecated, deta, detr, dialogpt, dinat, distilbert, dit, donut, dpr, dpt, efficientformer, efficientnet, electra, encodec, encoder_decoder, ernie, ernie_m, esm, falcon, flaubert, flava, fnet, focalnet, fsmt, funnel, git, glpn, gpta, gpt_bigcode, gpt_neo, gpt_neox, gpt_neox_japanese, gpt_swa, gptj, gptsan_japanese, graphormer, groupvit, herbert, hubert, ibert, imagegpt, informer, instructblip, jukebox, layoutlm, layoutlmva, layoutlmva, layoutxlm, led, levit, lilt, llama, longformer, longta, luke, lxmert, mam_aaa, marian, markuplm, maskaformer, maskformer, mbart, mbartaa, mega, megatron_bert, megatron_gpta, mgp_str, mluke, mobilebert, mobilenet_va, mobilenet_va, mobilevit, mobilevitva, mpnet, mra, mta, musicgen, mvp, nat, nezha, nllb, nllb_moe, nystromformer, oneformer, open_llama, openai, opt, owlvit, pegasus, pegasus_x, perceiver, phobert, pixastruct, plbart, poolformer, prophetnet, qdqbert, rag, realm, reformer, regnet, rembert, resnet, roberta, roberta_prelayernorm, roc_bert, roformer, rwkv, sam, segformer, sew, sew_d, speech_encoder_decoder, speech_to_text, speech_to_text_a, speechta, splinter, squeezebert, swiftformer, swin, swinasr, swinva, switch_transformers, ta, table_transformer, tapas, time_series_transformer, timesformer, timm_backbone, transfo_xl, trocr, tvlt, umta, unispeech, unispeech_sat, upernet, videomae, vilt, vision_encoder_decoder, vision_text_dual_encoder, visual_bert, vit, vit_hybrid, vit_mae, vit_msn, vivit, wavaveca, wavaveca_conformer, wavaveca_phoneme, wavaveca_with_lm, wavlm, whisper, x_clip, xglm, xlm, xlm_prophetnet, xlm_roberta, xlm_roberta_xl, xlnet, xmod, yolos, yoso, )
1
0
from copy import deepcopy import torch import torch.nn.functional as F from torch.optim import AdamW from torch.optim.lr_scheduler import LambdaLR from torch.utils.data import DataLoader from accelerate.accelerator import Accelerator from accelerate.state import GradientState from accelerate.test_utils import RegressionDataset, RegressionModel from accelerate.utils import DistributedType, is_torch_version, set_seed def _lowerCAmelCase( __A , __A , __A , __A ): for param, grad_param in zip(model_a.parameters() , model_b.parameters() ): if not param.requires_grad: continue if not did_step: # Grads should not be in sync assert ( torch.allclose(param.grad , grad_param.grad ) is False ), F"Gradients in sync when they should not be at iteration {iteration}:\nmodel_a grad ({param.grad}) == model_b grad ({grad_param.grad})" else: # Grads should be in sync assert ( torch.allclose(param.grad , grad_param.grad ) is True ), F"Gradients not in sync when they should be at iteration {iteration}:\nmodel_a grad ({param.grad}) != model_b grad ({grad_param.grad})" def _lowerCAmelCase( __A , __A , __A , __A , __A=True ): model.train() UpperCAmelCase = model(__A ) UpperCAmelCase = F.mse_loss(__A , target.to(output.device ) ) if not do_backward: loss /= accelerator.gradient_accumulation_steps loss.backward() else: accelerator.backward(__A ) def _lowerCAmelCase( __A , __A=False ): set_seed(42 ) UpperCAmelCase = RegressionModel() UpperCAmelCase = deepcopy(__A ) UpperCAmelCase = RegressionDataset(length=80 ) UpperCAmelCase = DataLoader(__A , batch_size=16 ) model.to(accelerator.device ) if sched: UpperCAmelCase = AdamW(params=model.parameters() , lr=1E-3 ) UpperCAmelCase = AdamW(params=ddp_model.parameters() , lr=1E-3 ) UpperCAmelCase = LambdaLR(__A , lr_lambda=lambda __A : epoch**0.65 ) UpperCAmelCase = LambdaLR(__A , lr_lambda=lambda __A : epoch**0.65 ) # Make a copy of `model` if sched: UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase = accelerator.prepare(__A , __A , __A , __A ) else: UpperCAmelCase , UpperCAmelCase = accelerator.prepare(__A , __A ) if sched: return (model, opt, sched, dataloader, ddp_model, ddp_opt, ddp_sched) return model, ddp_model, dataloader def _lowerCAmelCase( __A ): # Test when on a single CPU or GPU that the context manager does nothing UpperCAmelCase , UpperCAmelCase , UpperCAmelCase = get_training_setup(__A ) # Use a single batch UpperCAmelCase , UpperCAmelCase = next(iter(__A ) ).values() for iteration in range(3 ): # Gather the distributed inputs and targs for the base model UpperCAmelCase , UpperCAmelCase = accelerator.gather((ddp_input, ddp_target) ) UpperCAmelCase , UpperCAmelCase = input.to(accelerator.device ), target.to(accelerator.device ) # Perform our initial ground truth step in non "DDP" step_model(__A , __A , __A , __A ) # Do "gradient accumulation" (noop) if iteration % 2 == 0: # Accumulate grads locally with accelerator.no_sync(__A ): step_model(__A , __A , __A , __A ) else: # Sync grads step_model(__A , __A , __A , __A ) # Since `no_sync` is a noop, `ddp_model` and `model` grads should always be in sync check_model_parameters(__A , __A , __A , __A ) for param, ddp_param in zip(model.parameters() , ddp_model.parameters() ): if not param.requires_grad: continue assert torch.allclose( param.grad , ddp_param.grad ), F"Gradients not in sync when they should be:\nModel grad ({param.grad}) != DDP grad ({ddp_param.grad})" # Shuffle ddp_input on each iteration torch.manual_seed(1337 + iteration ) UpperCAmelCase = ddp_input[torch.randperm(len(__A ) )] def _lowerCAmelCase( __A ): # Test on distributed setup that context manager behaves properly UpperCAmelCase , UpperCAmelCase , UpperCAmelCase = get_training_setup(__A ) # Use a single batch UpperCAmelCase , UpperCAmelCase = next(iter(__A ) ).values() for iteration in range(3 ): # Gather the distributed inputs and targs for the base model UpperCAmelCase , UpperCAmelCase = accelerator.gather((ddp_input, ddp_target) ) UpperCAmelCase , UpperCAmelCase = input.to(accelerator.device ), target.to(accelerator.device ) # Perform our initial ground truth step in non "DDP" step_model(__A , __A , __A , __A ) # Do "gradient accumulation" (noop) if iteration % 2 == 0: # Accumulate grads locally with accelerator.no_sync(__A ): step_model(__A , __A , __A , __A ) else: # Sync grads step_model(__A , __A , __A , __A ) # DDP model and model should only be in sync when not (iteration % 2 == 0) for param, ddp_param in zip(model.parameters() , ddp_model.parameters() ): if not param.requires_grad: continue if iteration % 2 == 0: # Grads should not be in sync assert ( torch.allclose(param.grad , ddp_param.grad ) is False ), F"Gradients in sync when they should not be:\nModel grad ({param.grad}) == DDP grad ({ddp_param.grad})" else: # Grads should be in sync assert ( torch.allclose(param.grad , ddp_param.grad ) is True ), F"Gradients not in sync when they should be:\nModel grad ({param.grad}) != DDP grad ({ddp_param.grad})" # Shuffle ddp_input on each iteration torch.manual_seed(1337 + iteration ) UpperCAmelCase = ddp_input[torch.randperm(len(__A ) )] def _lowerCAmelCase( __A=False , __A=False ): UpperCAmelCase = Accelerator( split_batches=__A , dispatch_batches=__A , gradient_accumulation_steps=2 ) # Test that context manager behaves properly UpperCAmelCase , UpperCAmelCase , UpperCAmelCase = get_training_setup(__A ) for iteration, batch in enumerate(__A ): UpperCAmelCase , UpperCAmelCase = batch.values() # Gather the distributed inputs and targs for the base model UpperCAmelCase , UpperCAmelCase = accelerator.gather((ddp_input, ddp_target) ) UpperCAmelCase , UpperCAmelCase = input.to(accelerator.device ), target.to(accelerator.device ) # Perform our initial ground truth step in non "DDP" step_model(__A , __A , __A , __A , __A ) # Do "gradient accumulation" (noop) with accelerator.accumulate(__A ): step_model(__A , __A , __A , __A ) # DDP model and model should only be in sync when not (iteration % 2 == 0) for param, ddp_param in zip(model.parameters() , ddp_model.parameters() ): if not param.requires_grad: continue if ((iteration + 1) % 2 == 0) or (iteration == len(__A ) - 1): # Grads should be in sync assert ( torch.allclose(param.grad , ddp_param.grad ) is True ), F"Gradients not in sync when they should be at iteration {iteration}:\nModel grad ({param.grad}) != DDP grad ({ddp_param.grad})" else: # Grads should not be in sync assert ( torch.allclose(param.grad , ddp_param.grad ) is False ), F"Gradients in sync when they should not be at iteration {iteration}:\nModel grad ({param.grad}) == DDP grad ({ddp_param.grad})" # Shuffle ddp_input on each iteration torch.manual_seed(1337 + iteration ) UpperCAmelCase = ddp_input[torch.randperm(len(__A ) )] GradientState._reset_state() def _lowerCAmelCase( __A=False , __A=False ): UpperCAmelCase = Accelerator( split_batches=__A , dispatch_batches=__A , gradient_accumulation_steps=2 ) # Test that context manager behaves properly UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase = get_training_setup(__A , __A ) for iteration, batch in enumerate(__A ): UpperCAmelCase , UpperCAmelCase = batch.values() # Gather the distributed inputs and targs for the base model UpperCAmelCase , UpperCAmelCase = accelerator.gather((ddp_input, ddp_target) ) UpperCAmelCase , UpperCAmelCase = input.to(accelerator.device ), target.to(accelerator.device ) # Perform our initial ground truth step in non "DDP" model.train() ddp_model.train() step_model(__A , __A , __A , __A , __A ) opt.step() if ((iteration + 1) % 2 == 0) or ((iteration + 1) == len(__A )): if split_batches: sched.step() else: for _ in range(accelerator.num_processes ): sched.step() opt.zero_grad() # Perform gradient accumulation under wrapper with accelerator.accumulate(__A ): step_model(__A , __A , __A , __A ) ddp_opt.step() ddp_sched.step() ddp_opt.zero_grad() # Learning rates should be the same assert ( opt.param_groups[0]["lr"] == ddp_opt.param_groups[0]["lr"] ), F"Learning rates found in each optimizer did not align\nopt: {opt.param_groups[0]['lr']}\nDDP opt: {ddp_opt.param_groups[0]['lr']}\n" UpperCAmelCase = (((iteration + 1) % 2) == 0) or ((iteration + 1) == len(__A )) if accelerator.num_processes > 1: check_model_parameters(__A , __A , __A , __A ) # Shuffle ddp_input on each iteration torch.manual_seed(1337 + iteration ) GradientState._reset_state() def _lowerCAmelCase( ): UpperCAmelCase = Accelerator() UpperCAmelCase = RegressionDataset(length=80 ) UpperCAmelCase = DataLoader(__A , batch_size=16 ) UpperCAmelCase = RegressionDataset(length=96 ) UpperCAmelCase = DataLoader(__A , batch_size=16 ) UpperCAmelCase , UpperCAmelCase = accelerator.prepare(__A , __A ) assert accelerator.gradient_state.active_dataloader is None for iteration, _ in enumerate(__A ): assert id(accelerator.gradient_state.active_dataloader ) == id(__A ) if iteration < len(__A ) - 1: assert not accelerator.gradient_state.end_of_dataloader if iteration == 1: for batch_num, _ in enumerate(__A ): assert id(accelerator.gradient_state.active_dataloader ) == id(__A ) if batch_num < len(__A ) - 1: assert not accelerator.gradient_state.end_of_dataloader else: assert accelerator.gradient_state.end_of_dataloader else: assert accelerator.gradient_state.end_of_dataloader assert accelerator.gradient_state.active_dataloader is None def _lowerCAmelCase( ): UpperCAmelCase = Accelerator() UpperCAmelCase = accelerator.state if state.local_process_index == 0: print("**Test `accumulate` gradient accumulation with dataloader break**" ) test_dataloader_break() if state.distributed_type == DistributedType.NO: if state.local_process_index == 0: print("**Test NOOP `no_sync` context manager**" ) test_noop_sync(__A ) if state.distributed_type in (DistributedType.MULTI_GPU, DistributedType.MULTI_CPU): if state.local_process_index == 0: print("**Test Distributed `no_sync` context manager**" ) test_distributed_sync(__A ) if state.distributed_type == DistributedType.MULTI_GPU: for split_batch in [True, False]: for dispatch_batches in [True, False]: if state.local_process_index == 0: print( "**Test `accumulate` gradient accumulation, " , F"`split_batches={split_batch}` and `dispatch_batches={dispatch_batches}`**" , ) test_gradient_accumulation(__A , __A ) # Currently will break on torch 2.0 +, need to investigate why if is_torch_version("<" , "2.0" ) or state.distributed_type == DistributedType.NO: if state.local_process_index == 0: print( "**Test `accumulate` gradient accumulation with optimizer and scheduler, " , "`split_batches=False`, `dispatch_batches=False`**" , ) test_gradient_accumulation_with_opt_and_scheduler() if state.distributed_type == DistributedType.MULTI_GPU: for split_batch in [True, False]: for dispatch_batches in [True, False]: if not split_batch and not dispatch_batches: continue if state.local_process_index == 0: print( "**Test `accumulate` gradient accumulation with optimizer and scheduler, " , F"`split_batches={split_batch}` and `dispatch_batches={dispatch_batches}`**" , ) test_gradient_accumulation_with_opt_and_scheduler(__A , __A ) def _lowerCAmelCase( __A ): # For xla_spawn (TPUs) main() if __name__ == "__main__": main()
706
import numpy # List of input, output pairs lowerCAmelCase__ = ( ((5, 2, 3), 15), ((6, 5, 9), 25), ((11, 12, 13), 41), ((1, 1, 1), 8), ((11, 12, 13), 41), ) lowerCAmelCase__ = (((515, 22, 13), 555), ((61, 35, 49), 150)) lowerCAmelCase__ = [2, 4, 1, 5] lowerCAmelCase__ = len(train_data) lowerCAmelCase__ = 0.0_0_9 def _lowerCAmelCase( __A , __A="train" ): return calculate_hypothesis_value(__A , __A ) - output( __A , __A ) def _lowerCAmelCase( __A ): UpperCAmelCase = 0 for i in range(len(__A ) - 1 ): hyp_val += data_input_tuple[i] * parameter_vector[i + 1] hyp_val += parameter_vector[0] return hyp_val def _lowerCAmelCase( __A , __A ): if data_set == "train": return train_data[example_no][1] elif data_set == "test": return test_data[example_no][1] return None def _lowerCAmelCase( __A , __A ): if data_set == "train": return _hypothesis_value(train_data[example_no][0] ) elif data_set == "test": return _hypothesis_value(test_data[example_no][0] ) return None def _lowerCAmelCase( __A , __A=m ): UpperCAmelCase = 0 for i in range(__A ): if index == -1: summation_value += _error(__A ) else: summation_value += _error(__A ) * train_data[i][0][index] return summation_value def _lowerCAmelCase( __A ): UpperCAmelCase = summation_of_cost_derivative(__A , __A ) / m return cost_derivative_value def _lowerCAmelCase( ): global parameter_vector # Tune these values to set a tolerance value for predicted output UpperCAmelCase = 0.000002 UpperCAmelCase = 0 UpperCAmelCase = 0 while True: j += 1 UpperCAmelCase = [0, 0, 0, 0] for i in range(0 , len(__A ) ): UpperCAmelCase = get_cost_derivative(i - 1 ) UpperCAmelCase = ( parameter_vector[i] - LEARNING_RATE * cost_derivative ) if numpy.allclose( __A , __A , atol=__A , rtol=__A , ): break UpperCAmelCase = temp_parameter_vector print(("Number of iterations:", j) ) def _lowerCAmelCase( ): for i in range(len(__A ) ): print(("Actual output value:", output(__A , "test" )) ) print(("Hypothesis output:", calculate_hypothesis_value(__A , "test" )) ) if __name__ == "__main__": run_gradient_descent() print("\nTesting gradient descent for a linear hypothesis function.\n") test_gradient_descent()
1
0
import unittest from typing import Dict, List, Optional, Union import numpy as np from transformers.testing_utils import require_torch, require_vision from transformers.utils import is_torch_available, is_vision_available from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs if is_torch_available(): import torch if is_vision_available(): from PIL import Image from transformers import BridgeTowerImageProcessor class __magic_name__ ( unittest.TestCase ): def __init__( self : Any , lowerCAmelCase__ : str , lowerCAmelCase__ : bool = True , lowerCAmelCase__ : Dict[str, int] = None , lowerCAmelCase__ : int = 3_2 , lowerCAmelCase__ : bool = True , lowerCAmelCase__ : Union[int, float] = 1 / 2_5_5 , lowerCAmelCase__ : bool = True , lowerCAmelCase__ : bool = True , lowerCAmelCase__ : Optional[Union[float, List[float]]] = [0.48_145_466, 0.4_578_275, 0.40_821_073] , lowerCAmelCase__ : Optional[Union[float, List[float]]] = [0.26_862_954, 0.26_130_258, 0.27_577_711] , lowerCAmelCase__ : bool = True , lowerCAmelCase__ : Union[str, Any]=7 , lowerCAmelCase__ : Dict=3_0 , lowerCAmelCase__ : Dict=4_0_0 , lowerCAmelCase__ : Dict=3 , ) -> Union[str, Any]: UpperCAmelCase = parent UpperCAmelCase = do_resize UpperCAmelCase = size if size is not None else {"shortest_edge": 2_8_8} UpperCAmelCase = size_divisor UpperCAmelCase = do_rescale UpperCAmelCase = rescale_factor UpperCAmelCase = do_normalize UpperCAmelCase = do_center_crop UpperCAmelCase = image_mean UpperCAmelCase = image_std UpperCAmelCase = do_pad UpperCAmelCase = batch_size UpperCAmelCase = num_channels UpperCAmelCase = min_resolution UpperCAmelCase = max_resolution def _UpperCamelCase ( self : Union[str, Any] ) -> str: return { "image_mean": self.image_mean, "image_std": self.image_std, "do_normalize": self.do_normalize, "do_resize": self.do_resize, "size": self.size, "size_divisor": self.size_divisor, } def _UpperCamelCase ( self : List[str] , lowerCAmelCase__ : Any , lowerCAmelCase__ : Union[str, Any]=False ) -> int: if not batched: UpperCAmelCase = self.size["shortest_edge"] UpperCAmelCase = image_inputs[0] if isinstance(lowerCAmelCase__ , Image.Image ): UpperCAmelCase , UpperCAmelCase = image.size else: UpperCAmelCase , UpperCAmelCase = image.shape[1], image.shape[2] UpperCAmelCase = size / min(lowerCAmelCase__ , lowerCAmelCase__ ) if h < w: UpperCAmelCase , UpperCAmelCase = size, scale * w else: UpperCAmelCase , UpperCAmelCase = scale * h, size UpperCAmelCase = int((1_3_3_3 / 8_0_0) * size ) if max(lowerCAmelCase__ , lowerCAmelCase__ ) > max_size: UpperCAmelCase = max_size / max(lowerCAmelCase__ , lowerCAmelCase__ ) UpperCAmelCase = newh * scale UpperCAmelCase = neww * scale UpperCAmelCase , UpperCAmelCase = int(newh + 0.5 ), int(neww + 0.5 ) UpperCAmelCase , UpperCAmelCase = ( newh // self.size_divisor * self.size_divisor, neww // self.size_divisor * self.size_divisor, ) else: UpperCAmelCase = [] for image in image_inputs: UpperCAmelCase , UpperCAmelCase = self.get_expected_values([image] ) expected_values.append((expected_height, expected_width) ) UpperCAmelCase = max(lowerCAmelCase__ , key=lambda lowerCAmelCase__ : item[0] )[0] UpperCAmelCase = max(lowerCAmelCase__ , key=lambda lowerCAmelCase__ : item[1] )[1] return expected_height, expected_width @require_torch @require_vision class __magic_name__ ( _snake_case , unittest.TestCase ): UpperCAmelCase = BridgeTowerImageProcessor if is_vision_available() else None def _UpperCamelCase ( self : Dict ) -> Tuple: UpperCAmelCase = BridgeTowerImageProcessingTester(self ) @property def _UpperCamelCase ( self : List[str] ) -> Union[str, Any]: return self.image_processor_tester.prepare_image_processor_dict() def _UpperCamelCase ( self : str ) -> Dict: UpperCAmelCase = self.image_processing_class(**self.image_processor_dict ) self.assertTrue(hasattr(lowerCAmelCase__ , "image_mean" ) ) self.assertTrue(hasattr(lowerCAmelCase__ , "image_std" ) ) self.assertTrue(hasattr(lowerCAmelCase__ , "do_normalize" ) ) self.assertTrue(hasattr(lowerCAmelCase__ , "do_resize" ) ) self.assertTrue(hasattr(lowerCAmelCase__ , "size" ) ) self.assertTrue(hasattr(lowerCAmelCase__ , "size_divisor" ) ) def _UpperCamelCase ( self : Dict ) -> Tuple: pass def _UpperCamelCase ( self : Dict ) -> int: # Initialize image processor UpperCAmelCase = self.image_processing_class(**self.image_processor_dict ) # create random PIL images UpperCAmelCase = prepare_image_inputs(self.image_processor_tester , equal_resolution=lowerCAmelCase__ ) for image in image_inputs: self.assertIsInstance(lowerCAmelCase__ , Image.Image ) # Test not batched input UpperCAmelCase = image_processing(image_inputs[0] , return_tensors="pt" ).pixel_values UpperCAmelCase , UpperCAmelCase = self.image_processor_tester.get_expected_values(lowerCAmelCase__ ) self.assertEqual( encoded_images.shape , (1, self.image_processor_tester.num_channels, expected_height, expected_width) , ) # Test batched UpperCAmelCase = image_processing(lowerCAmelCase__ , return_tensors="pt" ).pixel_values UpperCAmelCase , UpperCAmelCase = self.image_processor_tester.get_expected_values(lowerCAmelCase__ , batched=lowerCAmelCase__ ) self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, expected_height, expected_width, ) , ) def _UpperCamelCase ( self : Any ) -> Union[str, Any]: # Initialize image processor UpperCAmelCase = self.image_processing_class(**self.image_processor_dict ) # create random numpy tensors UpperCAmelCase = prepare_image_inputs(self.image_processor_tester , equal_resolution=lowerCAmelCase__ , numpify=lowerCAmelCase__ ) for image in image_inputs: self.assertIsInstance(lowerCAmelCase__ , np.ndarray ) # Test not batched input UpperCAmelCase = image_processing(image_inputs[0] , return_tensors="pt" ).pixel_values UpperCAmelCase , UpperCAmelCase = self.image_processor_tester.get_expected_values(lowerCAmelCase__ ) self.assertEqual( encoded_images.shape , (1, self.image_processor_tester.num_channels, expected_height, expected_width) , ) # Test batched UpperCAmelCase = image_processing(lowerCAmelCase__ , return_tensors="pt" ).pixel_values UpperCAmelCase , UpperCAmelCase = self.image_processor_tester.get_expected_values(lowerCAmelCase__ , batched=lowerCAmelCase__ ) self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, expected_height, expected_width, ) , ) def _UpperCamelCase ( self : Optional[int] ) -> Dict: # Initialize image processor UpperCAmelCase = self.image_processing_class(**self.image_processor_dict ) # create random PyTorch tensors UpperCAmelCase = prepare_image_inputs(self.image_processor_tester , equal_resolution=lowerCAmelCase__ , torchify=lowerCAmelCase__ ) for image in image_inputs: self.assertIsInstance(lowerCAmelCase__ , torch.Tensor ) # Test not batched input UpperCAmelCase = image_processing(image_inputs[0] , return_tensors="pt" ).pixel_values UpperCAmelCase , UpperCAmelCase = self.image_processor_tester.get_expected_values(lowerCAmelCase__ ) self.assertEqual( encoded_images.shape , (1, self.image_processor_tester.num_channels, expected_height, expected_width) , ) # Test batched UpperCAmelCase = image_processing(lowerCAmelCase__ , return_tensors="pt" ).pixel_values UpperCAmelCase , UpperCAmelCase = self.image_processor_tester.get_expected_values(lowerCAmelCase__ , batched=lowerCAmelCase__ ) self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, expected_height, expected_width, ) , )
707
def _lowerCAmelCase( __A , __A , __A ): if n == 0: return 1 elif n % 2 == 1: return (binary_exponentiation(__A , n - 1 , __A ) * a) % mod else: UpperCAmelCase = binary_exponentiation(__A , n / 2 , __A ) return (b * b) % mod # a prime number lowerCAmelCase__ = 701 lowerCAmelCase__ = 1000000000 lowerCAmelCase__ = 10 # using binary exponentiation function, O(log(p)): print((a / b) % p == (a * binary_exponentiation(b, p - 2, p)) % p) print((a / b) % p == (a * b ** (p - 2)) % p)
1
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 lowerCAmelCase__ = None lowerCAmelCase__ = "<" 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 lowerCAmelCase__ = [ 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 __magic_name__ : UpperCAmelCase = True UpperCAmelCase = None # Automatically constructed UpperCAmelCase = """PIL.Image.Image""" UpperCAmelCase = pa.struct({"""bytes""": pa.binary(), """path""": pa.string()} ) UpperCAmelCase = field(default="""Image""" , init=_snake_case , repr=_snake_case ) def __call__( self : Tuple ) -> Dict: return self.pa_type def _UpperCamelCase ( self : List[Any] , lowerCAmelCase__ : Union[str, bytes, dict, np.ndarray, "PIL.Image.Image"] ) -> dict: if config.PIL_AVAILABLE: import PIL.Image else: raise ImportError("To support encoding images, please install 'Pillow'." ) if isinstance(lowerCAmelCase__ , lowerCAmelCase__ ): UpperCAmelCase = np.array(lowerCAmelCase__ ) if isinstance(lowerCAmelCase__ , lowerCAmelCase__ ): return {"path": value, "bytes": None} elif isinstance(lowerCAmelCase__ , lowerCAmelCase__ ): return {"path": None, "bytes": value} elif isinstance(lowerCAmelCase__ , np.ndarray ): # convert the image array to PNG/TIFF bytes return encode_np_array(lowerCAmelCase__ ) elif isinstance(lowerCAmelCase__ , PIL.Image.Image ): # convert the PIL image to bytes (default format is PNG/TIFF) return encode_pil_image(lowerCAmelCase__ ) 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 _UpperCamelCase ( self : List[str] , lowerCAmelCase__ : dict , lowerCAmelCase__ : List[Any]=None ) -> "PIL.Image.Image": 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: UpperCAmelCase = {} UpperCAmelCase , UpperCAmelCase = 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(lowerCAmelCase__ ): UpperCAmelCase = PIL.Image.open(lowerCAmelCase__ ) else: UpperCAmelCase = path.split("::" )[-1] try: UpperCAmelCase = string_to_dict(lowerCAmelCase__ , config.HUB_DATASETS_URL )["repo_id"] UpperCAmelCase = token_per_repo_id.get(lowerCAmelCase__ ) except ValueError: UpperCAmelCase = None with xopen(lowerCAmelCase__ , "rb" , use_auth_token=lowerCAmelCase__ ) as f: UpperCAmelCase = BytesIO(f.read() ) UpperCAmelCase = PIL.Image.open(bytes_ ) else: UpperCAmelCase = PIL.Image.open(BytesIO(bytes_ ) ) image.load() # to avoid "Too many open files" errors return image def _UpperCamelCase ( self : Tuple ) -> Union["FeatureType", Dict[str, "FeatureType"]]: from .features import Value return ( self if self.decode else { "bytes": Value("binary" ), "path": Value("string" ), } ) def _UpperCamelCase ( self : List[str] , lowerCAmelCase__ : Union[pa.StringArray, pa.StructArray, pa.ListArray] ) -> pa.StructArray: if pa.types.is_string(storage.type ): UpperCAmelCase = pa.array([None] * len(lowerCAmelCase__ ) , type=pa.binary() ) UpperCAmelCase = pa.StructArray.from_arrays([bytes_array, storage] , ["bytes", "path"] , mask=storage.is_null() ) elif pa.types.is_binary(storage.type ): UpperCAmelCase = pa.array([None] * len(lowerCAmelCase__ ) , type=pa.string() ) UpperCAmelCase = 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: UpperCAmelCase = storage.field("bytes" ) else: UpperCAmelCase = pa.array([None] * len(lowerCAmelCase__ ) , type=pa.binary() ) if storage.type.get_field_index("path" ) >= 0: UpperCAmelCase = storage.field("path" ) else: UpperCAmelCase = pa.array([None] * len(lowerCAmelCase__ ) , type=pa.string() ) UpperCAmelCase = pa.StructArray.from_arrays([bytes_array, path_array] , ["bytes", "path"] , mask=storage.is_null() ) elif pa.types.is_list(storage.type ): UpperCAmelCase = pa.array( [encode_np_array(np.array(lowerCAmelCase__ ) )["bytes"] if arr is not None else None for arr in storage.to_pylist()] , type=pa.binary() , ) UpperCAmelCase = pa.array([None] * len(lowerCAmelCase__ ) , type=pa.string() ) UpperCAmelCase = pa.StructArray.from_arrays( [bytes_array, path_array] , ["bytes", "path"] , mask=bytes_array.is_null() ) return array_cast(lowerCAmelCase__ , self.pa_type ) def _UpperCamelCase ( self : Dict , lowerCAmelCase__ : pa.StructArray ) -> pa.StructArray: @no_op_if_value_is_null def path_to_bytes(lowerCAmelCase__ : Union[str, Any] ): with xopen(lowerCAmelCase__ , "rb" ) as f: UpperCAmelCase = f.read() return bytes_ UpperCAmelCase = 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() , ) UpperCAmelCase = pa.array( [os.path.basename(lowerCAmelCase__ ) if path is not None else None for path in storage.field("path" ).to_pylist()] , type=pa.string() , ) UpperCAmelCase = pa.StructArray.from_arrays([bytes_array, path_array] , ["bytes", "path"] , mask=bytes_array.is_null() ) return array_cast(lowerCAmelCase__ , self.pa_type ) def _lowerCAmelCase( ): 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() UpperCAmelCase = list(set(PIL.Image.OPEN.keys() ) & set(PIL.Image.SAVE.keys() ) ) return _IMAGE_COMPRESSION_FORMATS def _lowerCAmelCase( __A : Union[str, Any] ): UpperCAmelCase = BytesIO() if image.format in list_image_compression_formats(): UpperCAmelCase = image.format else: UpperCAmelCase = "PNG" if image.mode in ["1", "L", "LA", "RGB", "RGBA"] else "TIFF" image.save(__A , format=__A ) return buffer.getvalue() def _lowerCAmelCase( __A : int ): if hasattr(__A , "filename" ) and image.filename != "": return {"path": image.filename, "bytes": None} else: return {"path": None, "bytes": image_to_bytes(__A )} def _lowerCAmelCase( __A : int ): if config.PIL_AVAILABLE: import PIL.Image else: raise ImportError("To support encoding images, please install 'Pillow'." ) UpperCAmelCase = array.dtype UpperCAmelCase = dtype.byteorder if dtype.byteorder != "=" else _NATIVE_BYTEORDER UpperCAmelCase = dtype.kind UpperCAmelCase = dtype.itemsize UpperCAmelCase = None # Multi-channel array case (only np.dtype("|u1") is allowed) if array.shape[2:]: UpperCAmelCase = 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: UpperCAmelCase = 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: UpperCAmelCase = dtype_byteorder + dtype_kind + str(__A ) UpperCAmelCase = np.dtype(__A ) 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}" ) UpperCAmelCase = PIL.Image.fromarray(array.astype(__A ) ) return {"path": None, "bytes": image_to_bytes(__A )} def _lowerCAmelCase( __A : Tuple ): if config.PIL_AVAILABLE: import PIL.Image else: raise ImportError("To support encoding images, please install 'Pillow'." ) if objs: UpperCAmelCase , UpperCAmelCase = first_non_null_value(__A ) if isinstance(__A , __A ): return [{"path": obj, "bytes": None} if obj is not None else None for obj in objs] if isinstance(__A , np.ndarray ): UpperCAmelCase = no_op_if_value_is_null(__A ) return [obj_to_image_dict_func(__A ) for obj in objs] elif isinstance(__A , PIL.Image.Image ): UpperCAmelCase = no_op_if_value_is_null(__A ) return [obj_to_image_dict_func(__A ) for obj in objs] else: return objs else: return objs
708
lowerCAmelCase__ = { "a": "AAAAA", "b": "AAAAB", "c": "AAABA", "d": "AAABB", "e": "AABAA", "f": "AABAB", "g": "AABBA", "h": "AABBB", "i": "ABAAA", "j": "BBBAA", "k": "ABAAB", "l": "ABABA", "m": "ABABB", "n": "ABBAA", "o": "ABBAB", "p": "ABBBA", "q": "ABBBB", "r": "BAAAA", "s": "BAAAB", "t": "BAABA", "u": "BAABB", "v": "BBBAB", "w": "BABAA", "x": "BABAB", "y": "BABBA", "z": "BABBB", " ": " ", } lowerCAmelCase__ = {value: key for key, value in encode_dict.items()} def _lowerCAmelCase( __A ): UpperCAmelCase = "" for letter in word.lower(): if letter.isalpha() or letter == " ": encoded += encode_dict[letter] else: raise Exception("encode() accepts only letters of the alphabet and spaces" ) return encoded def _lowerCAmelCase( __A ): if set(__A ) - {"A", "B", " "} != set(): raise Exception("decode() accepts only 'A', 'B' and spaces" ) UpperCAmelCase = "" for word in coded.split(): while len(__A ) != 0: decoded += decode_dict[word[:5]] UpperCAmelCase = word[5:] decoded += " " return decoded.strip() if __name__ == "__main__": from doctest import testmod testmod()
1
0
import math def _lowerCAmelCase( __A , __A ): return math.pow(__A , 2 ) - a def _lowerCAmelCase( __A ): return 2 * x def _lowerCAmelCase( __A ): UpperCAmelCase = 2.0 while start <= a: UpperCAmelCase = math.pow(__A , 2 ) return start def _lowerCAmelCase( __A , __A = 9999 , __A = 0.00000000000001 ): if a < 0: raise ValueError("math domain error" ) UpperCAmelCase = get_initial_point(__A ) for _ in range(__A ): UpperCAmelCase = value UpperCAmelCase = value - fx(__A , __A ) / fx_derivative(__A ) if abs(prev_value - value ) < tolerance: return value return value if __name__ == "__main__": from doctest import testmod testmod()
709
from __future__ import annotations import json import requests from bsa import BeautifulSoup from fake_useragent import UserAgent lowerCAmelCase__ = {"UserAgent": UserAgent().random} def _lowerCAmelCase( __A ): UpperCAmelCase = script.contents[0] UpperCAmelCase = json.loads(data[data.find("{\"config\"" ) : -1] ) return info["entry_data"]["ProfilePage"][0]["graphql"]["user"] class __magic_name__ : def __init__( self : Optional[Any] , lowerCAmelCase__ : Optional[int] ) -> Any: UpperCAmelCase = f"https://www.instagram.com/{username}/" UpperCAmelCase = self.get_json() def _UpperCamelCase ( self : List[str] ) -> dict: UpperCAmelCase = requests.get(self.url , headers=lowerCAmelCase__ ).text UpperCAmelCase = BeautifulSoup(lowerCAmelCase__ , "html.parser" ).find_all("script" ) try: return extract_user_profile(scripts[4] ) except (json.decoder.JSONDecodeError, KeyError): return extract_user_profile(scripts[3] ) def __repr__( self : Tuple ) -> str: return f"{self.__class__.__name__}('{self.username}')" def __str__( self : Optional[int] ) -> str: return f"{self.fullname} ({self.username}) is {self.biography}" @property def _UpperCamelCase ( self : Any ) -> str: return self.user_data["username"] @property def _UpperCamelCase ( self : List[Any] ) -> str: return self.user_data["full_name"] @property def _UpperCamelCase ( self : List[str] ) -> str: return self.user_data["biography"] @property def _UpperCamelCase ( self : Optional[int] ) -> str: return self.user_data["business_email"] @property def _UpperCamelCase ( self : str ) -> str: return self.user_data["external_url"] @property def _UpperCamelCase ( self : int ) -> int: return self.user_data["edge_followed_by"]["count"] @property def _UpperCamelCase ( self : List[Any] ) -> int: return self.user_data["edge_follow"]["count"] @property def _UpperCamelCase ( self : List[str] ) -> int: return self.user_data["edge_owner_to_timeline_media"]["count"] @property def _UpperCamelCase ( self : Tuple ) -> str: return self.user_data["profile_pic_url_hd"] @property def _UpperCamelCase ( self : Optional[int] ) -> bool: return self.user_data["is_verified"] @property def _UpperCamelCase ( self : Optional[Any] ) -> bool: return self.user_data["is_private"] def _lowerCAmelCase( __A = "github" ): import os if os.environ.get("CI" ): return # test failing on GitHub Actions UpperCAmelCase = InstagramUser(__A ) assert instagram_user.user_data assert isinstance(instagram_user.user_data , __A ) assert instagram_user.username == username if username != "github": return assert instagram_user.fullname == "GitHub" assert instagram_user.biography == "Built for developers." assert instagram_user.number_of_posts > 150 assert instagram_user.number_of_followers > 120000 assert instagram_user.number_of_followings > 15 assert instagram_user.email == "support@github.com" assert instagram_user.website == "https://github.com/readme" assert instagram_user.profile_picture_url.startswith("https://instagram." ) assert instagram_user.is_verified is True assert instagram_user.is_private is False if __name__ == "__main__": import doctest doctest.testmod() lowerCAmelCase__ = InstagramUser("github") print(instagram_user) print(f"{instagram_user.number_of_posts = }") print(f"{instagram_user.number_of_followers = }") print(f"{instagram_user.number_of_followings = }") print(f"{instagram_user.email = }") print(f"{instagram_user.website = }") print(f"{instagram_user.profile_picture_url = }") print(f"{instagram_user.is_verified = }") print(f"{instagram_user.is_private = }")
1
0
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_torch_available, is_vision_available, ) lowerCAmelCase__ = {"configuration_beit": ["BEIT_PRETRAINED_CONFIG_ARCHIVE_MAP", "BeitConfig", "BeitOnnxConfig"]} try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase__ = ["BeitFeatureExtractor"] lowerCAmelCase__ = ["BeitImageProcessor"] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase__ = [ "BEIT_PRETRAINED_MODEL_ARCHIVE_LIST", "BeitForImageClassification", "BeitForMaskedImageModeling", "BeitForSemanticSegmentation", "BeitModel", "BeitPreTrainedModel", ] try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase__ = [ "FlaxBeitForImageClassification", "FlaxBeitForMaskedImageModeling", "FlaxBeitModel", "FlaxBeitPreTrainedModel", ] if TYPE_CHECKING: from .configuration_beit import BEIT_PRETRAINED_CONFIG_ARCHIVE_MAP, BeitConfig, BeitOnnxConfig try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .feature_extraction_beit import BeitFeatureExtractor from .image_processing_beit import BeitImageProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_beit import ( BEIT_PRETRAINED_MODEL_ARCHIVE_LIST, BeitForImageClassification, BeitForMaskedImageModeling, BeitForSemanticSegmentation, BeitModel, BeitPreTrainedModel, ) try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_flax_beit import ( FlaxBeitForImageClassification, FlaxBeitForMaskedImageModeling, FlaxBeitModel, FlaxBeitPreTrainedModel, ) else: import sys lowerCAmelCase__ = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
710
import unittest import numpy as np def _lowerCAmelCase( __A , __A , __A , __A = None , ): UpperCAmelCase = np.shape(__A ) UpperCAmelCase = np.shape(__A ) UpperCAmelCase = np.shape(__A ) if shape_a[0] != shape_b[0]: UpperCAmelCase = ( "Expected the same number of rows for A and B. " F"Instead found A of size {shape_a} and B of size {shape_b}" ) raise ValueError(__A ) if shape_b[1] != shape_c[1]: UpperCAmelCase = ( "Expected the same number of columns for B and C. " F"Instead found B of size {shape_b} and C of size {shape_c}" ) raise ValueError(__A ) UpperCAmelCase = pseudo_inv if a_inv is None: try: UpperCAmelCase = np.linalg.inv(__A ) except np.linalg.LinAlgError: raise ValueError( "Input matrix A is not invertible. Cannot compute Schur complement." ) return mat_c - mat_b.T @ a_inv @ mat_b class __magic_name__ ( unittest.TestCase ): def _UpperCamelCase ( self : List[str] ) -> None: UpperCAmelCase = np.array([[1, 2, 1], [2, 1, 2], [3, 2, 4]] ) UpperCAmelCase = np.array([[0, 3], [3, 0], [2, 3]] ) UpperCAmelCase = np.array([[2, 1], [6, 3]] ) UpperCAmelCase = schur_complement(lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ ) UpperCAmelCase = np.block([[a, b], [b.T, c]] ) UpperCAmelCase = np.linalg.det(lowerCAmelCase__ ) UpperCAmelCase = np.linalg.det(lowerCAmelCase__ ) UpperCAmelCase = np.linalg.det(lowerCAmelCase__ ) self.assertAlmostEqual(lowerCAmelCase__ , det_a * det_s ) def _UpperCamelCase ( self : str ) -> None: UpperCAmelCase = np.array([[1, 2, 1], [2, 1, 2], [3, 2, 4]] ) UpperCAmelCase = np.array([[0, 3], [3, 0], [2, 3]] ) UpperCAmelCase = np.array([[2, 1], [6, 3]] ) with self.assertRaises(lowerCAmelCase__ ): schur_complement(lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ ) def _UpperCamelCase ( self : Dict ) -> None: UpperCAmelCase = np.array([[1, 2, 1], [2, 1, 2], [3, 2, 4]] ) UpperCAmelCase = np.array([[0, 3], [3, 0], [2, 3]] ) UpperCAmelCase = np.array([[2, 1, 3], [6, 3, 5]] ) with self.assertRaises(lowerCAmelCase__ ): schur_complement(lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ ) if __name__ == "__main__": import doctest doctest.testmod() unittest.main()
1
0
import json from typing import Iterator, List, Union from tokenizers import AddedToken, Regex, Tokenizer, decoders, normalizers, pre_tokenizers, trainers from tokenizers.implementations.base_tokenizer import BaseTokenizer from tokenizers.models import Unigram from tokenizers.processors import TemplateProcessing class __magic_name__ ( _snake_case ): def __init__( self : Any , lowerCAmelCase__ : str = "▁" , lowerCAmelCase__ : bool = True , lowerCAmelCase__ : Union[str, AddedToken] = "<unk>" , lowerCAmelCase__ : Union[str, AddedToken] = "</s>" , lowerCAmelCase__ : Union[str, AddedToken] = "<pad>" , ) -> Optional[Any]: UpperCAmelCase = { "pad": {"id": 0, "token": pad_token}, "eos": {"id": 1, "token": eos_token}, "unk": {"id": 2, "token": unk_token}, } UpperCAmelCase = [None] * len(self.special_tokens ) for token_dict in self.special_tokens.values(): UpperCAmelCase = token_dict["token"] UpperCAmelCase = Tokenizer(Unigram() ) UpperCAmelCase = normalizers.Sequence( [ normalizers.Nmt(), normalizers.NFKC(), normalizers.Replace(Regex(" {2,}" ) , " " ), normalizers.Lowercase(), ] ) UpperCAmelCase = pre_tokenizers.Sequence( [ pre_tokenizers.Metaspace(replacement=lowerCAmelCase__ , add_prefix_space=lowerCAmelCase__ ), pre_tokenizers.Digits(individual_digits=lowerCAmelCase__ ), pre_tokenizers.Punctuation(), ] ) UpperCAmelCase = decoders.Metaspace(replacement=lowerCAmelCase__ , add_prefix_space=lowerCAmelCase__ ) UpperCAmelCase = TemplateProcessing( single=f"$A {self.special_tokens['eos']['token']}" , special_tokens=[(self.special_tokens["eos"]["token"], self.special_tokens["eos"]["id"])] , ) UpperCAmelCase = { "model": "SentencePieceUnigram", "replacement": replacement, "add_prefix_space": add_prefix_space, } super().__init__(lowerCAmelCase__ , lowerCAmelCase__ ) def _UpperCamelCase ( self : Any , lowerCAmelCase__ : Union[str, List[str]] , lowerCAmelCase__ : int = 8_0_0_0 , lowerCAmelCase__ : bool = True , ) -> List[str]: UpperCAmelCase = trainers.UnigramTrainer( vocab_size=lowerCAmelCase__ , special_tokens=self.special_tokens_list , show_progress=lowerCAmelCase__ , ) if isinstance(lowerCAmelCase__ , lowerCAmelCase__ ): UpperCAmelCase = [files] self._tokenizer.train(lowerCAmelCase__ , trainer=lowerCAmelCase__ ) self.add_unk_id() def _UpperCamelCase ( self : Tuple , lowerCAmelCase__ : Union[Iterator[str], Iterator[Iterator[str]]] , lowerCAmelCase__ : int = 8_0_0_0 , lowerCAmelCase__ : bool = True , ) -> List[Any]: UpperCAmelCase = trainers.UnigramTrainer( vocab_size=lowerCAmelCase__ , special_tokens=self.special_tokens_list , show_progress=lowerCAmelCase__ , ) self._tokenizer.train_from_iterator(lowerCAmelCase__ , trainer=lowerCAmelCase__ ) self.add_unk_id() def _UpperCamelCase ( self : List[Any] ) -> Dict: UpperCAmelCase = json.loads(self._tokenizer.to_str() ) UpperCAmelCase = self.special_tokens["unk"]["id"] UpperCAmelCase = Tokenizer.from_str(json.dumps(lowerCAmelCase__ ) )
711
import argparse import os import re import numpy as np import PIL import torch from timm import create_model from torch.optim.lr_scheduler import OneCycleLR from torch.utils.data import DataLoader, Dataset from torchvision.transforms import Compose, RandomResizedCrop, Resize, ToTensor from accelerate import Accelerator def _lowerCAmelCase( __A ): UpperCAmelCase = fname.split(os.path.sep )[-1] return re.search(r"^(.*)_\d+\.jpg$" , __A ).groups()[0] class __magic_name__ ( _snake_case ): def __init__( self : Any , lowerCAmelCase__ : int , lowerCAmelCase__ : Union[str, Any]=None , lowerCAmelCase__ : int=None ) -> Optional[Any]: UpperCAmelCase = file_names UpperCAmelCase = image_transform UpperCAmelCase = label_to_id def __len__( self : Tuple ) -> List[str]: return len(self.file_names ) def __getitem__( self : Optional[int] , lowerCAmelCase__ : Tuple ) -> Dict: UpperCAmelCase = self.file_names[idx] UpperCAmelCase = PIL.Image.open(lowerCAmelCase__ ) UpperCAmelCase = raw_image.convert("RGB" ) if self.image_transform is not None: UpperCAmelCase = self.image_transform(lowerCAmelCase__ ) UpperCAmelCase = extract_label(lowerCAmelCase__ ) if self.label_to_id is not None: UpperCAmelCase = self.label_to_id[label] return {"image": image, "label": label} def _lowerCAmelCase( __A , __A ): # Initialize accelerator if args.with_tracking: UpperCAmelCase = Accelerator( cpu=args.cpu , mixed_precision=args.mixed_precision , log_with="all" , project_dir=args.project_dir ) else: UpperCAmelCase = Accelerator(cpu=args.cpu , mixed_precision=args.mixed_precision ) # Sample hyper-parameters for learning rate, batch size, seed and a few other HPs UpperCAmelCase = config["lr"] UpperCAmelCase = int(config["num_epochs"] ) UpperCAmelCase = int(config["seed"] ) UpperCAmelCase = int(config["batch_size"] ) UpperCAmelCase = config["image_size"] if not isinstance(__A , (list, tuple) ): UpperCAmelCase = (image_size, image_size) # Parse out whether we are saving every epoch or after a certain number of batches if hasattr(args.checkpointing_steps , "isdigit" ): if args.checkpointing_steps == "epoch": UpperCAmelCase = args.checkpointing_steps elif args.checkpointing_steps.isdigit(): UpperCAmelCase = int(args.checkpointing_steps ) else: raise ValueError( F"Argument `checkpointing_steps` must be either a number or `epoch`. `{args.checkpointing_steps}` passed." ) else: UpperCAmelCase = None # We need to initialize the trackers we use, and also store our configuration if args.with_tracking: UpperCAmelCase = os.path.split(__A )[-1].split("." )[0] accelerator.init_trackers(__A , __A ) # Grab all the image filenames UpperCAmelCase = [os.path.join(args.data_dir , __A ) for fname in os.listdir(args.data_dir ) if fname.endswith(".jpg" )] # Build the label correspondences UpperCAmelCase = [extract_label(__A ) for fname in file_names] UpperCAmelCase = list(set(__A ) ) id_to_label.sort() UpperCAmelCase = {lbl: i for i, lbl in enumerate(__A )} # Set the seed before splitting the data. np.random.seed(__A ) torch.manual_seed(__A ) torch.cuda.manual_seed_all(__A ) # Split our filenames between train and validation UpperCAmelCase = np.random.permutation(len(__A ) ) UpperCAmelCase = int(0.8 * len(__A ) ) UpperCAmelCase = random_perm[:cut] UpperCAmelCase = random_perm[cut:] # For training we use a simple RandomResizedCrop UpperCAmelCase = Compose([RandomResizedCrop(__A , scale=(0.5, 1.0) ), ToTensor()] ) UpperCAmelCase = PetsDataset( [file_names[i] for i in train_split] , image_transform=__A , label_to_id=__A ) # For evaluation, we use a deterministic Resize UpperCAmelCase = Compose([Resize(__A ), ToTensor()] ) UpperCAmelCase = PetsDataset([file_names[i] for i in eval_split] , image_transform=__A , label_to_id=__A ) # Instantiate dataloaders. UpperCAmelCase = DataLoader(__A , shuffle=__A , batch_size=__A , num_workers=4 ) UpperCAmelCase = DataLoader(__A , shuffle=__A , batch_size=__A , num_workers=4 ) # Instantiate the model (we build the model here so that the seed also control new weights initialization) UpperCAmelCase = create_model("resnet50d" , pretrained=__A , num_classes=len(__A ) ) # We could avoid this line since the accelerator is set with `device_placement=True` (default value). # Note that if you are placing tensors on devices manually, this line absolutely needs to be before the optimizer # creation otherwise training will not work on TPU (`accelerate` will kindly throw an error to make us aware of that). UpperCAmelCase = model.to(accelerator.device ) # Freezing the base model for param in model.parameters(): UpperCAmelCase = False for param in model.get_classifier().parameters(): UpperCAmelCase = True # We normalize the batches of images to be a bit faster. UpperCAmelCase = torch.tensor(model.default_cfg["mean"] )[None, :, None, None].to(accelerator.device ) UpperCAmelCase = torch.tensor(model.default_cfg["std"] )[None, :, None, None].to(accelerator.device ) # Instantiate optimizer UpperCAmelCase = torch.optim.Adam(params=model.parameters() , lr=lr / 25 ) # Instantiate learning rate scheduler UpperCAmelCase = OneCycleLR(optimizer=__A , max_lr=__A , epochs=__A , steps_per_epoch=len(__A ) ) # Prepare everything # There is no specific order to remember, we just need to unpack the objects in the same order we gave them to the # prepare method. UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase = accelerator.prepare( __A , __A , __A , __A , __A ) # We need to keep track of how many total steps we have iterated over UpperCAmelCase = 0 # We also need to keep track of the starting epoch so files are named properly UpperCAmelCase = 0 # Potentially load in the weights and states from a previous save if args.resume_from_checkpoint: if args.resume_from_checkpoint is not None or args.resume_from_checkpoint != "": accelerator.print(F"Resumed from checkpoint: {args.resume_from_checkpoint}" ) accelerator.load_state(args.resume_from_checkpoint ) UpperCAmelCase = os.path.basename(args.resume_from_checkpoint ) else: # Get the most recent checkpoint UpperCAmelCase = [f.name for f in os.scandir(os.getcwd() ) if f.is_dir()] dirs.sort(key=os.path.getctime ) UpperCAmelCase = dirs[-1] # Sorts folders by date modified, most recent checkpoint is the last # Extract `epoch_{i}` or `step_{i}` UpperCAmelCase = os.path.splitext(__A )[0] if "epoch" in training_difference: UpperCAmelCase = int(training_difference.replace("epoch_" , "" ) ) + 1 UpperCAmelCase = None else: UpperCAmelCase = int(training_difference.replace("step_" , "" ) ) UpperCAmelCase = resume_step // len(__A ) resume_step -= starting_epoch * len(__A ) # Now we train the model for epoch in range(__A , __A ): model.train() if args.with_tracking: UpperCAmelCase = 0 if args.resume_from_checkpoint and epoch == starting_epoch and resume_step is not None: # We need to skip steps until we reach the resumed step UpperCAmelCase = accelerator.skip_first_batches(__A , __A ) overall_step += resume_step else: # After the first iteration though, we need to go back to the original dataloader UpperCAmelCase = train_dataloader for batch in active_dataloader: # We could avoid this line since we set the accelerator with `device_placement=True`. UpperCAmelCase = {k: v.to(accelerator.device ) for k, v in batch.items()} UpperCAmelCase = (batch["image"] - mean) / std UpperCAmelCase = model(__A ) UpperCAmelCase = torch.nn.functional.cross_entropy(__A , batch["label"] ) # We keep track of the loss at each epoch if args.with_tracking: total_loss += loss.detach().float() accelerator.backward(__A ) optimizer.step() lr_scheduler.step() optimizer.zero_grad() overall_step += 1 if isinstance(__A , __A ): UpperCAmelCase = F"step_{overall_step}" if overall_step % checkpointing_steps == 0: if args.output_dir is not None: UpperCAmelCase = os.path.join(args.output_dir , __A ) accelerator.save_state(__A ) model.eval() UpperCAmelCase = 0 UpperCAmelCase = 0 for step, batch in enumerate(__A ): # We could avoid this line since we set the accelerator with `device_placement=True`. UpperCAmelCase = {k: v.to(accelerator.device ) for k, v in batch.items()} UpperCAmelCase = (batch["image"] - mean) / std with torch.no_grad(): UpperCAmelCase = model(__A ) UpperCAmelCase = outputs.argmax(dim=-1 ) UpperCAmelCase , UpperCAmelCase = accelerator.gather_for_metrics((predictions, batch["label"]) ) UpperCAmelCase = predictions == references num_elems += accurate_preds.shape[0] accurate += accurate_preds.long().sum() UpperCAmelCase = accurate.item() / num_elems # Use accelerator.print to print only on the main process. accelerator.print(F"epoch {epoch}: {100 * eval_metric:.2f}" ) if args.with_tracking: accelerator.log( { "accuracy": 100 * eval_metric, "train_loss": total_loss.item() / len(__A ), "epoch": epoch, } , step=__A , ) if checkpointing_steps == "epoch": UpperCAmelCase = F"epoch_{epoch}" if args.output_dir is not None: UpperCAmelCase = os.path.join(args.output_dir , __A ) accelerator.save_state(__A ) if args.with_tracking: accelerator.end_training() def _lowerCAmelCase( ): UpperCAmelCase = argparse.ArgumentParser(description="Simple example of training script." ) parser.add_argument("--data_dir" , required=__A , help="The data folder on disk." ) parser.add_argument("--fp16" , action="store_true" , help="If passed, will use FP16 training." ) parser.add_argument( "--mixed_precision" , type=__A , default=__A , choices=["no", "fp16", "bf16", "fp8"] , help="Whether to use mixed precision. Choose" "between fp16 and bf16 (bfloat16). Bf16 requires PyTorch >= 1.10." "and an Nvidia Ampere GPU." , ) parser.add_argument("--cpu" , action="store_true" , help="If passed, will train on the CPU." ) parser.add_argument( "--checkpointing_steps" , type=__A , default=__A , help="Whether the various states should be saved at the end of every n steps, or 'epoch' for each epoch." , ) parser.add_argument( "--output_dir" , type=__A , default="." , help="Optional save directory where all checkpoint folders will be stored. Default is the current working directory." , ) parser.add_argument( "--resume_from_checkpoint" , type=__A , default=__A , help="If the training should continue from a checkpoint folder." , ) parser.add_argument( "--with_tracking" , action="store_true" , help="Whether to load in all available experiment trackers from the environment and use them for logging." , ) parser.add_argument( "--project_dir" , type=__A , default="logs" , help="Location on where to store experiment tracking logs` and relevent project information" , ) UpperCAmelCase = parser.parse_args() UpperCAmelCase = {"lr": 3E-2, "num_epochs": 3, "seed": 42, "batch_size": 64, "image_size": 224} training_function(__A , __A ) if __name__ == "__main__": main()
1
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 convert_to_rgb, normalize, rescale, resize, to_channel_dimension_format from ...image_utils import ( OPENAI_CLIP_MEAN, OPENAI_CLIP_STD, ChannelDimension, ImageInput, PILImageResampling, make_list_of_images, to_numpy_array, valid_images, ) from ...utils import TensorType, is_vision_available, logging if is_vision_available(): import PIL lowerCAmelCase__ = logging.get_logger(__name__) class __magic_name__ ( _snake_case ): UpperCAmelCase = ["""pixel_values"""] def __init__( self : List[Any] , lowerCAmelCase__ : bool = True , lowerCAmelCase__ : Dict[str, int] = None , lowerCAmelCase__ : PILImageResampling = PILImageResampling.BICUBIC , lowerCAmelCase__ : bool = True , lowerCAmelCase__ : Union[int, float] = 1 / 2_5_5 , lowerCAmelCase__ : bool = True , lowerCAmelCase__ : Optional[Union[float, List[float]]] = None , lowerCAmelCase__ : Optional[Union[float, List[float]]] = None , lowerCAmelCase__ : bool = True , **lowerCAmelCase__ : Any , ) -> None: super().__init__(**lowerCAmelCase__ ) UpperCAmelCase = size if size is not None else {"height": 3_8_4, "width": 3_8_4} UpperCAmelCase = get_size_dict(lowerCAmelCase__ , default_to_square=lowerCAmelCase__ ) UpperCAmelCase = do_resize UpperCAmelCase = size UpperCAmelCase = resample UpperCAmelCase = do_rescale UpperCAmelCase = rescale_factor UpperCAmelCase = do_normalize UpperCAmelCase = image_mean if image_mean is not None else OPENAI_CLIP_MEAN UpperCAmelCase = image_std if image_std is not None else OPENAI_CLIP_STD UpperCAmelCase = do_convert_rgb def _UpperCamelCase ( self : int , lowerCAmelCase__ : np.ndarray , lowerCAmelCase__ : Dict[str, int] , lowerCAmelCase__ : PILImageResampling = PILImageResampling.BICUBIC , lowerCAmelCase__ : Optional[Union[str, ChannelDimension]] = None , **lowerCAmelCase__ : List[Any] , ) -> np.ndarray: UpperCAmelCase = get_size_dict(lowerCAmelCase__ , default_to_square=lowerCAmelCase__ ) if "height" not in size or "width" not in size: raise ValueError(f"The `size` dictionary must contain the keys `height` and `width`. Got {size.keys()}" ) UpperCAmelCase = (size["height"], size["width"]) return resize(lowerCAmelCase__ , size=lowerCAmelCase__ , resample=lowerCAmelCase__ , data_format=lowerCAmelCase__ , **lowerCAmelCase__ ) def _UpperCamelCase ( self : Union[str, Any] , lowerCAmelCase__ : np.ndarray , lowerCAmelCase__ : Union[int, float] , lowerCAmelCase__ : Optional[Union[str, ChannelDimension]] = None , **lowerCAmelCase__ : Optional[int] , ) -> Optional[Any]: return rescale(lowerCAmelCase__ , scale=lowerCAmelCase__ , data_format=lowerCAmelCase__ , **lowerCAmelCase__ ) def _UpperCamelCase ( self : Any , lowerCAmelCase__ : np.ndarray , lowerCAmelCase__ : Union[float, List[float]] , lowerCAmelCase__ : Union[float, List[float]] , lowerCAmelCase__ : Optional[Union[str, ChannelDimension]] = None , **lowerCAmelCase__ : List[Any] , ) -> np.ndarray: return normalize(lowerCAmelCase__ , mean=lowerCAmelCase__ , std=lowerCAmelCase__ , data_format=lowerCAmelCase__ , **lowerCAmelCase__ ) def _UpperCamelCase ( self : Union[str, Any] , lowerCAmelCase__ : ImageInput , lowerCAmelCase__ : Optional[bool] = None , lowerCAmelCase__ : Optional[Dict[str, int]] = None , lowerCAmelCase__ : PILImageResampling = None , lowerCAmelCase__ : Optional[bool] = None , lowerCAmelCase__ : Optional[float] = None , lowerCAmelCase__ : Optional[bool] = None , lowerCAmelCase__ : Optional[Union[float, List[float]]] = None , lowerCAmelCase__ : Optional[Union[float, List[float]]] = None , lowerCAmelCase__ : Optional[Union[str, TensorType]] = None , lowerCAmelCase__ : bool = None , lowerCAmelCase__ : ChannelDimension = ChannelDimension.FIRST , **lowerCAmelCase__ : int , ) -> PIL.Image.Image: UpperCAmelCase = do_resize if do_resize is not None else self.do_resize UpperCAmelCase = resample if resample is not None else self.resample UpperCAmelCase = do_rescale if do_rescale is not None else self.do_rescale UpperCAmelCase = rescale_factor if rescale_factor is not None else self.rescale_factor UpperCAmelCase = do_normalize if do_normalize is not None else self.do_normalize UpperCAmelCase = image_mean if image_mean is not None else self.image_mean UpperCAmelCase = image_std if image_std is not None else self.image_std UpperCAmelCase = do_convert_rgb if do_convert_rgb is not None else self.do_convert_rgb UpperCAmelCase = size if size is not None else self.size UpperCAmelCase = get_size_dict(lowerCAmelCase__ , default_to_square=lowerCAmelCase__ ) UpperCAmelCase = make_list_of_images(lowerCAmelCase__ ) if not valid_images(lowerCAmelCase__ ): raise ValueError( "Invalid image type. Must be of type PIL.Image.Image, numpy.ndarray, " "torch.Tensor, tf.Tensor or jax.ndarray." ) if do_resize and size is None or resample is None: raise ValueError("Size and resample must be specified if do_resize 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." ) # PIL RGBA images are converted to RGB if do_convert_rgb: UpperCAmelCase = [convert_to_rgb(lowerCAmelCase__ ) for image in images] # All transformations expect numpy arrays. UpperCAmelCase = [to_numpy_array(lowerCAmelCase__ ) for image in images] if do_resize: UpperCAmelCase = [self.resize(image=lowerCAmelCase__ , size=lowerCAmelCase__ , resample=lowerCAmelCase__ ) for image in images] if do_rescale: UpperCAmelCase = [self.rescale(image=lowerCAmelCase__ , scale=lowerCAmelCase__ ) for image in images] if do_normalize: UpperCAmelCase = [self.normalize(image=lowerCAmelCase__ , mean=lowerCAmelCase__ , std=lowerCAmelCase__ ) for image in images] UpperCAmelCase = [to_channel_dimension_format(lowerCAmelCase__ , lowerCAmelCase__ ) for image in images] UpperCAmelCase = BatchFeature(data={"pixel_values": images} , tensor_type=lowerCAmelCase__ ) return encoded_outputs
712
import glob import os import random from string import ascii_lowercase, digits import cva lowerCAmelCase__ = "" lowerCAmelCase__ = "" lowerCAmelCase__ = "" lowerCAmelCase__ = 1 # (0 is vertical, 1 is horizontal) def _lowerCAmelCase( ): UpperCAmelCase , UpperCAmelCase = get_dataset(__A , __A ) print("Processing..." ) UpperCAmelCase , UpperCAmelCase , UpperCAmelCase = update_image_and_anno(__A , __A , __A ) for index, image in enumerate(__A ): # Get random string code: '7b7ad245cdff75241935e4dd860f3bad' UpperCAmelCase = random_chars(32 ) UpperCAmelCase = paths[index].split(os.sep )[-1].rsplit("." , 1 )[0] UpperCAmelCase = F"{OUTPUT_DIR}/{file_name}_FLIP_{letter_code}" cva.imwrite(F"/{file_root}.jpg" , __A , [cva.IMWRITE_JPEG_QUALITY, 85] ) print(F"Success {index+1}/{len(__A )} with {file_name}" ) UpperCAmelCase = [] for anno in new_annos[index]: UpperCAmelCase = F"{anno[0]} {anno[1]} {anno[2]} {anno[3]} {anno[4]}" annos_list.append(__A ) with open(F"/{file_root}.txt" , "w" ) as outfile: outfile.write("\n".join(line for line in annos_list ) ) def _lowerCAmelCase( __A , __A ): UpperCAmelCase = [] UpperCAmelCase = [] for label_file in glob.glob(os.path.join(__A , "*.txt" ) ): UpperCAmelCase = label_file.split(os.sep )[-1].rsplit("." , 1 )[0] with open(__A ) as in_file: UpperCAmelCase = in_file.readlines() UpperCAmelCase = os.path.join(__A , F"{label_name}.jpg" ) UpperCAmelCase = [] for obj_list in obj_lists: UpperCAmelCase = obj_list.rstrip("\n" ).split(" " ) boxes.append( [ int(obj[0] ), float(obj[1] ), float(obj[2] ), float(obj[3] ), float(obj[4] ), ] ) if not boxes: continue img_paths.append(__A ) labels.append(__A ) return img_paths, labels def _lowerCAmelCase( __A , __A , __A = 1 ): UpperCAmelCase = [] UpperCAmelCase = [] UpperCAmelCase = [] for idx in range(len(__A ) ): UpperCAmelCase = [] UpperCAmelCase = img_list[idx] path_list.append(__A ) UpperCAmelCase = anno_list[idx] UpperCAmelCase = cva.imread(__A ) if flip_type == 1: UpperCAmelCase = cva.flip(__A , __A ) for bbox in img_annos: UpperCAmelCase = 1 - bbox[1] new_annos.append([bbox[0], x_center_new, bbox[2], bbox[3], bbox[4]] ) elif flip_type == 0: UpperCAmelCase = cva.flip(__A , __A ) for bbox in img_annos: UpperCAmelCase = 1 - bbox[2] new_annos.append([bbox[0], bbox[1], y_center_new, bbox[3], bbox[4]] ) new_annos_lists.append(__A ) new_imgs_list.append(__A ) return new_imgs_list, new_annos_lists, path_list def _lowerCAmelCase( __A = 32 ): assert number_char > 1, "The number of character should greater than 1" UpperCAmelCase = ascii_lowercase + digits return "".join(random.choice(__A ) for _ in range(__A ) ) if __name__ == "__main__": main() print("DONE ✅")
1
0
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 lowerCAmelCase__ = [ 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) lowerCAmelCase__ = logging.getLogger() def _lowerCAmelCase( ): UpperCAmelCase = argparse.ArgumentParser() parser.add_argument("-f" ) UpperCAmelCase = parser.parse_args() return args.f def _lowerCAmelCase( __A , __A="eval" ): UpperCAmelCase = 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}" ) lowerCAmelCase__ = logging.StreamHandler(sys.stdout) logger.addHandler(stream_handler) class __magic_name__ ( _snake_case ): def _UpperCamelCase ( self : Any ) -> List[str]: UpperCAmelCase = self.get_auto_remove_tmp_dir() UpperCAmelCase = 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() UpperCAmelCase = get_results(lowerCAmelCase__ ) self.assertGreaterEqual(result["eval_accuracy"] , 0.75 ) @slow def _UpperCamelCase ( self : Any ) -> List[str]: UpperCAmelCase = self.get_auto_remove_tmp_dir() UpperCAmelCase = 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() UpperCAmelCase = get_results(lowerCAmelCase__ ) self.assertLess(result["eval_perplexity"] , 1_0_0 ) @slow def _UpperCamelCase ( self : Union[str, Any] ) -> Any: UpperCAmelCase = self.get_auto_remove_tmp_dir() UpperCAmelCase = 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() UpperCAmelCase = get_results(lowerCAmelCase__ , split="test" ) self.assertGreaterEqual(result["test_rouge1"] , 1_0 ) self.assertGreaterEqual(result["test_rouge2"] , 2 ) self.assertGreaterEqual(result["test_rougeL"] , 7 ) self.assertGreaterEqual(result["test_rougeLsum"] , 7 ) @slow def _UpperCamelCase ( self : Optional[Any] ) -> Union[str, Any]: UpperCAmelCase = self.get_auto_remove_tmp_dir() UpperCAmelCase = 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() UpperCAmelCase = get_results(lowerCAmelCase__ ) self.assertLess(result["eval_perplexity"] , 4_2 ) @slow def _UpperCamelCase ( self : int ) -> Dict: UpperCAmelCase = self.get_auto_remove_tmp_dir() UpperCAmelCase = 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() UpperCAmelCase = get_results(lowerCAmelCase__ ) self.assertGreaterEqual(result["eval_accuracy"] , 0.42 ) @slow def _UpperCamelCase ( self : int ) -> Any: # with so little data distributed training needs more epochs to get the score on par with 0/1 gpu UpperCAmelCase = 7 if get_gpu_count() > 1 else 2 UpperCAmelCase = self.get_auto_remove_tmp_dir() UpperCAmelCase = 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() UpperCAmelCase = get_results(lowerCAmelCase__ ) self.assertGreaterEqual(result["eval_accuracy"] , 0.75 ) self.assertGreaterEqual(result["eval_f1"] , 0.3 ) @slow def _UpperCamelCase ( self : Optional[Any] ) -> int: UpperCAmelCase = self.get_auto_remove_tmp_dir() UpperCAmelCase = 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() UpperCAmelCase = get_results(lowerCAmelCase__ ) self.assertGreaterEqual(result["eval_f1"] , 3_0 ) self.assertGreaterEqual(result["eval_exact"] , 3_0 )
713
def _lowerCAmelCase( __A ): if not isinstance(__A , __A ): raise TypeError("only integers accepted as input" ) else: UpperCAmelCase = str(abs(__A ) ) UpperCAmelCase = [list(__A ) for char in range(len(__A ) )] for index in range(len(__A ) ): num_transpositions[index].pop(__A ) return max( int("".join(list(__A ) ) ) for transposition in num_transpositions ) if __name__ == "__main__": __import__("doctest").testmod()
1
0
import math def _lowerCAmelCase( __A ): return math.sqrt(__A ) * math.sqrt(__A ) == num def _lowerCAmelCase( __A ): UpperCAmelCase = 0 UpperCAmelCase = n while left <= right: UpperCAmelCase = (left + right) // 2 if mid**2 == n: return True elif mid**2 > n: UpperCAmelCase = mid - 1 else: UpperCAmelCase = mid + 1 return False if __name__ == "__main__": import doctest doctest.testmod()
714
import logging import re import pytorch_quantization import pytorch_quantization.nn as quant_nn import torch from pytorch_quantization import calib from pytorch_quantization.tensor_quant import QuantDescriptor lowerCAmelCase__ = logging.getLogger(__name__) lowerCAmelCase__ = 50 # max width of layer names lowerCAmelCase__ = 70 # max width of quantizer names def _lowerCAmelCase( __A ): UpperCAmelCase = parser.add_argument_group("quant_trainer arguments" ) group.add_argument("--wprec" , type=__A , default=8 , help="weight precision" ) group.add_argument("--aprec" , type=__A , default=8 , help="activation precision" ) group.add_argument("--quant-per-tensor" , action="store_true" , help="per tensor weight scaling" ) group.add_argument("--quant-disable" , action="store_true" , help="disable all quantizers" ) group.add_argument("--quant-disable-embeddings" , action="store_true" , help="disable all embeddings quantizers" ) group.add_argument("--quant-disable-keyword" , type=__A , nargs="+" , help="disable quantizers by keyword" ) group.add_argument("--quant-disable-layer-module" , type=__A , help="disable quantizers by keyword under layer." ) group.add_argument("--quant-enable-layer-module" , type=__A , help="enable quantizers by keyword under layer" ) group.add_argument("--calibrator" , default="max" , help="which quantization range calibrator to use" ) group.add_argument("--percentile" , default=__A , type=__A , help="percentile for PercentileCalibrator" ) group.add_argument("--fuse-qkv" , action="store_true" , help="use the same scale factor for qkv" ) group.add_argument("--clip-gelu" , metavar="N" , type=__A , help="clip gelu output maximum value to N" ) group.add_argument( "--recalibrate-weights" , action="store_true" , help=( "recalibrate weight amaxes by taking the max of the weights." " amaxes will be computed with the current quantization granularity (axis)." ) , ) def _lowerCAmelCase( __A ): if args.calibrator == "max": UpperCAmelCase = "max" elif args.calibrator == "percentile": if args.percentile is None: raise ValueError("Specify --percentile when using percentile calibrator" ) UpperCAmelCase = "histogram" elif args.calibrator == "mse": UpperCAmelCase = "histogram" else: raise ValueError(F"Invalid calibrator {args.calibrator}" ) UpperCAmelCase = QuantDescriptor(num_bits=args.aprec , calib_method=__A ) UpperCAmelCase = QuantDescriptor(num_bits=args.wprec , axis=(None if args.quant_per_tensor else (0,)) ) quant_nn.QuantLinear.set_default_quant_desc_input(__A ) quant_nn.QuantLinear.set_default_quant_desc_weight(__A ) def _lowerCAmelCase( __A , __A , __A=False , __A=False ): logger.info("Configuring Model for Quantization" ) logger.info(F"using quantization package {pytorch_quantization.__file__}" ) if not calib: if args.quant_disable_embeddings: set_quantizer_by_name(__A , ["embeddings"] , which="weight" , _disabled=__A ) if args.quant_disable: set_quantizer_by_name(__A , [""] , _disabled=__A ) if args.quant_disable_keyword: set_quantizer_by_name(__A , args.quant_disable_keyword , _disabled=__A ) if args.quant_disable_layer_module: set_quantizer_by_name(__A , [r"layer.\d+." + args.quant_disable_layer_module] , _disabled=__A ) if args.quant_enable_layer_module: set_quantizer_by_name(__A , [r"layer.\d+." + args.quant_enable_layer_module] , _disabled=__A ) if args.recalibrate_weights: recalibrate_weights(__A ) if args.fuse_qkv: fuse_qkv(__A , __A ) if args.clip_gelu: clip_gelu(__A , args.clip_gelu ) # if args.local_rank in [-1, 0] and not calib: print_quant_summary(__A ) def _lowerCAmelCase( __A ): logger.info("Enabling Calibration" ) for name, module in model.named_modules(): if name.endswith("_quantizer" ): if module._calibrator is not None: module.disable_quant() module.enable_calib() else: module.disable() logger.info(F"{name:80}: {module}" ) def _lowerCAmelCase( __A , __A ): logger.info("Loading calibrated amax" ) for name, module in model.named_modules(): if name.endswith("_quantizer" ): if module._calibrator is not None: if isinstance(module._calibrator , calib.MaxCalibrator ): module.load_calib_amax() else: module.load_calib_amax("percentile" , percentile=args.percentile ) module.enable_quant() module.disable_calib() else: module.enable() model.cuda() print_quant_summary(__A ) def _lowerCAmelCase( __A , __A ): def fusea(__A , __A , __A ): for mod in [qq, qk, qv]: if not hasattr(__A , "_amax" ): print(" WARNING: NO AMAX BUFFER" ) return UpperCAmelCase = qq._amax.detach().item() UpperCAmelCase = qk._amax.detach().item() UpperCAmelCase = qv._amax.detach().item() UpperCAmelCase = max(__A , __A , __A ) qq._amax.fill_(__A ) qk._amax.fill_(__A ) qv._amax.fill_(__A ) logger.info(F" q={q:5.2f} k={k:5.2f} v={v:5.2f} -> {amax:5.2f}" ) for name, mod in model.named_modules(): if name.endswith(".attention.self" ): logger.info(F"FUSE_QKV: {name:{name_width}}" ) fusea(mod.matmul_q_input_quantizer , mod.matmul_k_input_quantizer , mod.matmul_v_input_quantizer ) if args.quant_per_tensor: fusea(mod.query._weight_quantizer , mod.key._weight_quantizer , mod.value._weight_quantizer ) def _lowerCAmelCase( __A , __A ): for name, mod in model.named_modules(): if name.endswith(".output.dense" ) and not name.endswith("attention.output.dense" ): UpperCAmelCase = mod._input_quantizer._amax.data.detach().item() mod._input_quantizer._amax.data.detach().clamp_(max=__A ) UpperCAmelCase = mod._input_quantizer._amax.data.detach().item() logger.info(F"CLIP_GELU: {name:{name_width}} amax: {amax_init:5.2f} -> {amax:5.2f}" ) def _lowerCAmelCase( __A ): for name, mod in model.named_modules(): if hasattr(__A , "_weight_quantizer" ) and mod._weight_quantizer.axis is not None: UpperCAmelCase = mod.weight.shape[0] UpperCAmelCase = mod._weight_quantizer._amax.detach() UpperCAmelCase = torch.ones(__A , dtype=amax.dtype , device=amax.device ) * amax print(F"expanding {name} {amax} -> {mod._weight_quantizer._amax}" ) def _lowerCAmelCase( __A ): for name, mod in model.named_modules(): if hasattr(__A , "_weight_quantizer" ): if not hasattr(mod.weight_quantizer , "_amax" ): print("RECALIB: {name:{name_width}} WARNING: NO AMAX BUFFER" ) continue # determine which axes to reduce across # e.g. a 4D tensor quantized per axis 0 should reduce over (1,2,3) UpperCAmelCase = set() if mod._weight_quantizer.axis is None else set(mod._weight_quantizer.axis ) UpperCAmelCase = set(range(len(mod.weight.size() ) ) ) - axis_set UpperCAmelCase = pytorch_quantization.utils.reduce_amax(mod.weight , axis=__A , keepdims=__A ).detach() logger.info(F"RECALIB: {name:{name_width}} {mod._weight_quantizer._amax.flatten()} -> {amax.flatten()}" ) UpperCAmelCase = amax def _lowerCAmelCase( __A , __A=25 , __A=180 , __A=None ): if ignore is None: UpperCAmelCase = [] elif not isinstance(__A , __A ): UpperCAmelCase = [ignore] UpperCAmelCase = 0 for name, mod in model.named_modules(): if not hasattr(__A , "weight" ): continue UpperCAmelCase = max(__A , len(__A ) ) for name, mod in model.named_modules(): UpperCAmelCase = getattr(__A , "_input_quantizer" , __A ) UpperCAmelCase = getattr(__A , "_weight_quantizer" , __A ) if not hasattr(__A , "weight" ): continue if type(__A ) in ignore: continue if [True for s in ignore if type(__A ) is str and s in name]: continue UpperCAmelCase = F"Act:{input_q.extra_repr()}" UpperCAmelCase = F"Wgt:{weight_q.extra_repr()}" UpperCAmelCase = F"{name:{name_width}} {act_str} {wgt_str}" if len(__A ) <= line_width: logger.info(__A ) else: logger.info(F"{name:{name_width}} {act_str}" ) logger.info(F"{' ':{name_width}} {wgt_str}" ) def _lowerCAmelCase( __A ): UpperCAmelCase = 0 for name, mod in model.named_modules(): if isinstance(__A , pytorch_quantization.nn.TensorQuantizer ): print(F"{name:80} {mod}" ) count += 1 print(F"{count} TensorQuantizers found in model" ) def _lowerCAmelCase( __A , __A , __A , __A , __A ): UpperCAmelCase = getattr(__A , __A , __A ) if quantizer_mod is not None: assert hasattr(__A , __A ) setattr(__A , __A , __A ) else: logger.warning(F"{name} has no {quantizer}" ) def _lowerCAmelCase( __A , __A , __A="both" , **__A ): UpperCAmelCase = F"Warning: changing {which} quantizers of {name:{qname_width}}" for k, v in kwargs.items(): s += F" {k}={v}" if which in ["input", "both"]: set_quantizer(__A , __A , "_input_quantizer" , __A , __A ) if which in ["weight", "both"]: set_quantizer(__A , __A , "_weight_quantizer" , __A , __A ) logger.info(__A ) def _lowerCAmelCase( __A , __A , **__A ): for name, mod in model.named_modules(): if hasattr(__A , "_input_quantizer" ) or hasattr(__A , "_weight_quantizer" ): for n in names: if re.search(__A , __A ): set_quantizers(__A , __A , **__A ) elif name.endswith("_quantizer" ): for n in names: if re.search(__A , __A ): UpperCAmelCase = F"Warning: changing {name:{name_width}}" for k, v in kwargs.items(): s += F" {k}={v}" setattr(__A , __A , __A ) logger.info(__A )
1
0
import os from shutil import copyfile from typing import Any, Dict, List, Optional, Tuple import sentencepiece as spm from ...tokenization_utils import PreTrainedTokenizer from ...utils import logging lowerCAmelCase__ = "▁" lowerCAmelCase__ = {"vocab_file": "spiece.model"} lowerCAmelCase__ = { "vocab_file": {"google/pegasus-xsum": "https://huggingface.co/google/pegasus-xsum/resolve/main/spiece.model"} } lowerCAmelCase__ = { "google/pegasus-xsum": 512, } lowerCAmelCase__ = logging.get_logger(__name__) class __magic_name__ ( _snake_case ): UpperCAmelCase = VOCAB_FILES_NAMES UpperCAmelCase = VOCAB_FILES_NAMES UpperCAmelCase = PRETRAINED_VOCAB_FILES_MAP UpperCAmelCase = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES UpperCAmelCase = ["""input_ids""", """attention_mask"""] def __init__( self : Optional[Any] , lowerCAmelCase__ : List[str] , lowerCAmelCase__ : Tuple="<pad>" , lowerCAmelCase__ : List[str]="</s>" , lowerCAmelCase__ : Union[str, Any]="<unk>" , lowerCAmelCase__ : Tuple="<mask_2>" , lowerCAmelCase__ : int="<mask_1>" , lowerCAmelCase__ : Optional[int]=None , lowerCAmelCase__ : List[Any]=1_0_3 , lowerCAmelCase__ : Optional[Dict[str, Any]] = None , **lowerCAmelCase__ : Optional[Any] , ) -> None: UpperCAmelCase = offset if additional_special_tokens is not None: if not isinstance(lowerCAmelCase__ , lowerCAmelCase__ ): raise TypeError( f"additional_special_tokens should be of type {type(lowerCAmelCase__ )}, but is" f" {type(lowerCAmelCase__ )}" ) UpperCAmelCase = ( ([mask_token_sent] + additional_special_tokens) if mask_token_sent not in additional_special_tokens and mask_token_sent is not None else additional_special_tokens ) # fill additional tokens with ..., <unk_token_102> in case not all additional tokens are already taken additional_special_tokens_extended += [ f"<unk_{i}>" for i in range(len(lowerCAmelCase__ ) , self.offset - 1 ) ] if len(set(lowerCAmelCase__ ) ) != len(lowerCAmelCase__ ): raise ValueError( "Please make sure that the provided additional_special_tokens do not contain an incorrectly" f" shifted list of <unk_x> tokens. Found {additional_special_tokens_extended}." ) UpperCAmelCase = additional_special_tokens_extended else: UpperCAmelCase = [mask_token_sent] if mask_token_sent is not None else [] additional_special_tokens += [f"<unk_{i}>" for i in range(2 , self.offset )] UpperCAmelCase = {} if sp_model_kwargs is None else sp_model_kwargs super().__init__( eos_token=lowerCAmelCase__ , unk_token=lowerCAmelCase__ , mask_token=lowerCAmelCase__ , pad_token=lowerCAmelCase__ , mask_token_sent=lowerCAmelCase__ , offset=lowerCAmelCase__ , additional_special_tokens=lowerCAmelCase__ , sp_model_kwargs=self.sp_model_kwargs , **lowerCAmelCase__ , ) UpperCAmelCase = mask_token_sent UpperCAmelCase = vocab_file UpperCAmelCase = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(lowerCAmelCase__ ) # add special tokens to encoder dict UpperCAmelCase = { 0: self.pad_token, 1: self.eos_token, } if self.mask_token_sent is not None: self.encoder.update( { 2: self.mask_token_sent, 3: self.mask_token, } ) if self.offset > 0: # entries 2-104 are only used for pretraining and called <mask_1>, <mask_2>, unk_2, ...unk_102 # mask_token_sent is already added to list -> so start at 1 self.encoder.update({i + 3: additional_special_tokens[i] for i in range(1 , self.offset - 1 )} ) UpperCAmelCase = {v: k for k, v in self.encoder.items()} @property def _UpperCamelCase ( self : List[Any] ) -> int: return len(self.sp_model ) + self.offset def _UpperCamelCase ( self : str ) -> Dict[str, int]: UpperCAmelCase = {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] ) -> List[Any]: UpperCAmelCase = self.__dict__.copy() UpperCAmelCase = None return state def __setstate__( self : List[Any] , lowerCAmelCase__ : List[Any] ) -> List[Any]: 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 : Optional[Any] , lowerCAmelCase__ : str ) -> List[str]: return self.sp_model.encode(lowerCAmelCase__ , out_type=lowerCAmelCase__ ) def _UpperCamelCase ( self : Optional[Any] , lowerCAmelCase__ : str ) -> int: if token in self.decoder: return self.decoder[token] elif token in self.added_tokens_decoder: return self.added_tokens_decoder[token] UpperCAmelCase = self.sp_model.piece_to_id(lowerCAmelCase__ ) return sp_id + self.offset def _UpperCamelCase ( self : Union[str, Any] , lowerCAmelCase__ : int ) -> str: if index in self.encoder: return self.encoder[index] elif index in self.added_tokens_encoder: return self.added_tokens_encoder[index] else: UpperCAmelCase = self.sp_model.IdToPiece(index - self.offset ) return token def _UpperCamelCase ( self : str , lowerCAmelCase__ : Optional[int] ) -> str: UpperCAmelCase = [] UpperCAmelCase = "" 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 UpperCAmelCase = [] else: current_sub_tokens.append(lowerCAmelCase__ ) out_string += self.sp_model.decode(lowerCAmelCase__ ) return out_string.strip() def _UpperCamelCase ( self : Optional[Any] , lowerCAmelCase__ : int=False ) -> int: return 1 def _UpperCamelCase ( self : Tuple , lowerCAmelCase__ : Any ) -> str: UpperCAmelCase = set(self.all_special_ids ) # call it once instead of inside list comp all_special_ids.remove(self.unk_token_id ) # <unk> is only sometimes special return [1 if x in all_special_ids else 0 for x in seq] def _UpperCamelCase ( self : int , lowerCAmelCase__ : List , lowerCAmelCase__ : Optional[List] = None , lowerCAmelCase__ : bool = False ) -> List[int]: if already_has_special_tokens: return self._special_token_mask(lowerCAmelCase__ ) elif token_ids_a is None: return self._special_token_mask(lowerCAmelCase__ ) + [1] else: return self._special_token_mask(token_ids_a + token_ids_a ) + [1] def _UpperCamelCase ( self : Optional[int] , lowerCAmelCase__ : Optional[int] , lowerCAmelCase__ : int=None ) -> List[int]: if token_ids_a is None: return token_ids_a + [self.eos_token_id] # We don't expect to process pairs, but leave the pair logic for API consistency return token_ids_a + token_ids_a + [self.eos_token_id] def _UpperCamelCase ( self : int , lowerCAmelCase__ : str , lowerCAmelCase__ : Optional[str] = None ) -> Tuple[str]: if not os.path.isdir(lowerCAmelCase__ ): logger.error(f"Vocabulary path ({save_directory}) should be a directory" ) return UpperCAmelCase = os.path.join( lowerCAmelCase__ , (filename_prefix + "-" if filename_prefix else "") + VOCAB_FILES_NAMES["vocab_file"] ) if os.path.abspath(self.vocab_file ) != os.path.abspath(lowerCAmelCase__ ) and os.path.isfile(self.vocab_file ): copyfile(self.vocab_file , lowerCAmelCase__ ) elif not os.path.isfile(self.vocab_file ): with open(lowerCAmelCase__ , "wb" ) as fi: UpperCAmelCase = self.sp_model.serialized_model_proto() fi.write(lowerCAmelCase__ ) return (out_vocab_file,)
715
def _lowerCAmelCase( __A ): assert column_title.isupper() UpperCAmelCase = 0 UpperCAmelCase = len(__A ) - 1 UpperCAmelCase = 0 while index >= 0: UpperCAmelCase = (ord(column_title[index] ) - 64) * pow(26 , __A ) answer += value power += 1 index -= 1 return answer if __name__ == "__main__": from doctest import testmod testmod()
1
0
# flake8: noqa # Lint as: python3 lowerCAmelCase__ = [ "VerificationMode", "Version", "disable_progress_bar", "enable_progress_bar", "is_progress_bar_enabled", "experimental", ] from .info_utils import VerificationMode from .logging import disable_progress_bar, enable_progress_bar, is_progress_bar_enabled from .version import Version from .experimental import experimental
716
import sys import tempfile import unittest import unittest.mock as mock from pathlib import Path from huggingface_hub import HfFolder, delete_repo from requests.exceptions import HTTPError from transformers import AutoFeatureExtractor, WavaVecaFeatureExtractor from transformers.testing_utils import TOKEN, USER, get_tests_dir, is_staging_test sys.path.append(str(Path(__file__).parent.parent / "utils")) from test_module.custom_feature_extraction import CustomFeatureExtractor # noqa E402 lowerCAmelCase__ = get_tests_dir("fixtures") class __magic_name__ ( unittest.TestCase ): def _UpperCamelCase ( self : Tuple ) -> Union[str, Any]: # A mock response for an HTTP head request to emulate server down UpperCAmelCase = mock.Mock() UpperCAmelCase = 5_0_0 UpperCAmelCase = {} UpperCAmelCase = HTTPError UpperCAmelCase = {} # Download this model to make sure it's in the cache. UpperCAmelCase = WavaVecaFeatureExtractor.from_pretrained("hf-internal-testing/tiny-random-wav2vec2" ) # Under the mock environment we get a 500 error when trying to reach the model. with mock.patch("requests.Session.request" , return_value=lowerCAmelCase__ ) as mock_head: UpperCAmelCase = WavaVecaFeatureExtractor.from_pretrained("hf-internal-testing/tiny-random-wav2vec2" ) # This check we did call the fake head request mock_head.assert_called() def _UpperCamelCase ( self : List[Any] ) -> Dict: # This test is for deprecated behavior and can be removed in v5 UpperCAmelCase = WavaVecaFeatureExtractor.from_pretrained( "https://huggingface.co/hf-internal-testing/tiny-random-wav2vec2/resolve/main/preprocessor_config.json" ) @is_staging_test class __magic_name__ ( unittest.TestCase ): @classmethod def _UpperCamelCase ( cls : List[str] ) -> List[Any]: UpperCAmelCase = TOKEN HfFolder.save_token(lowerCAmelCase__ ) @classmethod def _UpperCamelCase ( cls : Optional[int] ) -> Union[str, Any]: try: delete_repo(token=cls._token , repo_id="test-feature-extractor" ) except HTTPError: pass try: delete_repo(token=cls._token , repo_id="valid_org/test-feature-extractor-org" ) except HTTPError: pass try: delete_repo(token=cls._token , repo_id="test-dynamic-feature-extractor" ) except HTTPError: pass def _UpperCamelCase ( self : Any ) -> Any: UpperCAmelCase = WavaVecaFeatureExtractor.from_pretrained(lowerCAmelCase__ ) feature_extractor.push_to_hub("test-feature-extractor" , use_auth_token=self._token ) UpperCAmelCase = WavaVecaFeatureExtractor.from_pretrained(f"{USER}/test-feature-extractor" ) for k, v in feature_extractor.__dict__.items(): self.assertEqual(lowerCAmelCase__ , getattr(lowerCAmelCase__ , lowerCAmelCase__ ) ) # Reset repo delete_repo(token=self._token , repo_id="test-feature-extractor" ) # Push to hub via save_pretrained with tempfile.TemporaryDirectory() as tmp_dir: feature_extractor.save_pretrained( lowerCAmelCase__ , repo_id="test-feature-extractor" , push_to_hub=lowerCAmelCase__ , use_auth_token=self._token ) UpperCAmelCase = WavaVecaFeatureExtractor.from_pretrained(f"{USER}/test-feature-extractor" ) for k, v in feature_extractor.__dict__.items(): self.assertEqual(lowerCAmelCase__ , getattr(lowerCAmelCase__ , lowerCAmelCase__ ) ) def _UpperCamelCase ( self : List[Any] ) -> Tuple: UpperCAmelCase = WavaVecaFeatureExtractor.from_pretrained(lowerCAmelCase__ ) feature_extractor.push_to_hub("valid_org/test-feature-extractor" , use_auth_token=self._token ) UpperCAmelCase = WavaVecaFeatureExtractor.from_pretrained("valid_org/test-feature-extractor" ) for k, v in feature_extractor.__dict__.items(): self.assertEqual(lowerCAmelCase__ , getattr(lowerCAmelCase__ , lowerCAmelCase__ ) ) # Reset repo delete_repo(token=self._token , repo_id="valid_org/test-feature-extractor" ) # Push to hub via save_pretrained with tempfile.TemporaryDirectory() as tmp_dir: feature_extractor.save_pretrained( lowerCAmelCase__ , repo_id="valid_org/test-feature-extractor-org" , push_to_hub=lowerCAmelCase__ , use_auth_token=self._token ) UpperCAmelCase = WavaVecaFeatureExtractor.from_pretrained("valid_org/test-feature-extractor-org" ) for k, v in feature_extractor.__dict__.items(): self.assertEqual(lowerCAmelCase__ , getattr(lowerCAmelCase__ , lowerCAmelCase__ ) ) def _UpperCamelCase ( self : Dict ) -> List[str]: CustomFeatureExtractor.register_for_auto_class() UpperCAmelCase = CustomFeatureExtractor.from_pretrained(lowerCAmelCase__ ) feature_extractor.push_to_hub("test-dynamic-feature-extractor" , use_auth_token=self._token ) # This has added the proper auto_map field to the config self.assertDictEqual( feature_extractor.auto_map , {"AutoFeatureExtractor": "custom_feature_extraction.CustomFeatureExtractor"} , ) UpperCAmelCase = AutoFeatureExtractor.from_pretrained( f"{USER}/test-dynamic-feature-extractor" , trust_remote_code=lowerCAmelCase__ ) # Can't make an isinstance check because the new_feature_extractor is from the CustomFeatureExtractor class of a dynamic module self.assertEqual(new_feature_extractor.__class__.__name__ , "CustomFeatureExtractor" )
1
0
import pytest from datasets import inspect_metric, list_metrics, load_metric @pytest.fixture def _lowerCAmelCase( __A ): monkeypatch.setattr("datasets.utils.deprecation_utils._emitted_deprecation_warnings" , set() ) @pytest.fixture def _lowerCAmelCase( __A ): class __magic_name__ : def __init__( self : Optional[int] , lowerCAmelCase__ : int ) -> int: UpperCAmelCase = metric_id class __magic_name__ : UpperCAmelCase = [MetricMock(_snake_case ) for metric_id in ["""accuracy""", """mse""", """precision""", """codeparrot/apps_metric"""]] def _UpperCamelCase ( self : List[str] ) -> Dict: return self._metrics monkeypatch.setattr("datasets.inspect.huggingface_hub" , HfhMock() ) @pytest.mark.parametrize( "func, args" , [(load_metric, ("metrics/mse",)), (list_metrics, ()), (inspect_metric, ("metrics/mse", "tmp_path"))] ) def _lowerCAmelCase( __A , __A , __A , __A , __A ): if "tmp_path" in args: UpperCAmelCase = tuple(arg if arg != "tmp_path" else tmp_path for arg in args ) with pytest.warns(__A , match="https://huggingface.co/docs/evaluate" ): func(*__A )
717
import argparse import os import re # All paths are set with the intent you should run this script from the root of the repo with the command # python utils/check_dummies.py lowerCAmelCase__ = "src/diffusers" # Matches is_xxx_available() lowerCAmelCase__ = re.compile(r"is\_([a-z_]*)_available\(\)") # Matches from xxx import bla lowerCAmelCase__ = re.compile(r"\s+from\s+\S*\s+import\s+([^\(\s].*)\n") lowerCAmelCase__ = "\n{0} = None\n" lowerCAmelCase__ = "\nclass {0}(metaclass=DummyObject):\n _backends = {1}\n\n def __init__(self, *args, **kwargs):\n requires_backends(self, {1})\n\n @classmethod\n def from_config(cls, *args, **kwargs):\n requires_backends(cls, {1})\n\n @classmethod\n def from_pretrained(cls, *args, **kwargs):\n requires_backends(cls, {1})\n" lowerCAmelCase__ = "\ndef {0}(*args, **kwargs):\n requires_backends({0}, {1})\n" def _lowerCAmelCase( __A ): UpperCAmelCase = _re_backend.findall(__A ) if len(__A ) == 0: return None return "_and_".join(__A ) def _lowerCAmelCase( ): with open(os.path.join(__A , "__init__.py" ) , "r" , encoding="utf-8" , newline="\n" ) as f: UpperCAmelCase = f.readlines() # Get to the point we do the actual imports for type checking UpperCAmelCase = 0 UpperCAmelCase = {} # Go through the end of the file while line_index < len(__A ): # If the line contains is_backend_available, we grab all objects associated with the `else` block UpperCAmelCase = find_backend(lines[line_index] ) if backend is not None: while not lines[line_index].startswith("else:" ): line_index += 1 line_index += 1 UpperCAmelCase = [] # Until we unindent, add backend objects to the list while line_index < len(__A ) and len(lines[line_index] ) > 1: UpperCAmelCase = lines[line_index] UpperCAmelCase = _re_single_line_import.search(__A ) if single_line_import_search is not None: objects.extend(single_line_import_search.groups()[0].split(", " ) ) elif line.startswith(" " * 8 ): objects.append(line[8:-2] ) line_index += 1 if len(__A ) > 0: UpperCAmelCase = objects else: line_index += 1 return backend_specific_objects def _lowerCAmelCase( __A , __A ): if name.isupper(): return DUMMY_CONSTANT.format(__A ) elif name.islower(): return DUMMY_FUNCTION.format(__A , __A ) else: return DUMMY_CLASS.format(__A , __A ) def _lowerCAmelCase( __A=None ): if backend_specific_objects is None: UpperCAmelCase = read_init() # For special correspondence backend to module name as used in the function requires_modulename UpperCAmelCase = {} for backend, objects in backend_specific_objects.items(): UpperCAmelCase = "[" + ", ".join(F"\"{b}\"" for b in backend.split("_and_" ) ) + "]" UpperCAmelCase = "# This file is autogenerated by the command `make fix-copies`, do not edit.\n" dummy_file += "from ..utils import DummyObject, requires_backends\n\n" dummy_file += "\n".join([create_dummy_object(__A , __A ) for o in objects] ) UpperCAmelCase = dummy_file return dummy_files def _lowerCAmelCase( __A=False ): UpperCAmelCase = create_dummy_files() # For special correspondence backend to shortcut as used in utils/dummy_xxx_objects.py UpperCAmelCase = {"torch": "pt"} # Locate actual dummy modules and read their content. UpperCAmelCase = os.path.join(__A , "utils" ) UpperCAmelCase = { backend: os.path.join(__A , F"dummy_{short_names.get(__A , __A )}_objects.py" ) for backend in dummy_files.keys() } UpperCAmelCase = {} for backend, file_path in dummy_file_paths.items(): if os.path.isfile(__A ): with open(__A , "r" , encoding="utf-8" , newline="\n" ) as f: UpperCAmelCase = f.read() else: UpperCAmelCase = "" for backend in dummy_files.keys(): if dummy_files[backend] != actual_dummies[backend]: if overwrite: print( F"Updating diffusers.utils.dummy_{short_names.get(__A , __A )}_objects.py as the main " "__init__ has new objects." ) with open(dummy_file_paths[backend] , "w" , encoding="utf-8" , newline="\n" ) as f: f.write(dummy_files[backend] ) else: raise ValueError( "The main __init__ has objects that are not present in " F"diffusers.utils.dummy_{short_names.get(__A , __A )}_objects.py. Run `make fix-copies` " "to fix this." ) if __name__ == "__main__": lowerCAmelCase__ = argparse.ArgumentParser() parser.add_argument("--fix_and_overwrite", action="store_true", help="Whether to fix inconsistencies.") lowerCAmelCase__ = parser.parse_args() check_dummies(args.fix_and_overwrite)
1
0
import copy import tempfile import unittest from huggingface_hub import HfFolder, delete_repo from parameterized import parameterized from requests.exceptions import HTTPError from transformers import AutoConfig, GenerationConfig from transformers.testing_utils import TOKEN, USER, is_staging_test class __magic_name__ ( unittest.TestCase ): @parameterized.expand([(None,), ("foo.json",)] ) def _UpperCamelCase ( self : str , lowerCAmelCase__ : Union[str, Any] ) -> Optional[int]: UpperCAmelCase = GenerationConfig( do_sample=lowerCAmelCase__ , temperature=0.7 , length_penalty=1.0 , bad_words_ids=[[1, 2, 3], [4, 5]] , ) with tempfile.TemporaryDirectory() as tmp_dir: config.save_pretrained(lowerCAmelCase__ , config_name=lowerCAmelCase__ ) UpperCAmelCase = GenerationConfig.from_pretrained(lowerCAmelCase__ , config_name=lowerCAmelCase__ ) # Checks parameters that were specified self.assertEqual(loaded_config.do_sample , lowerCAmelCase__ ) self.assertEqual(loaded_config.temperature , 0.7 ) self.assertEqual(loaded_config.length_penalty , 1.0 ) self.assertEqual(loaded_config.bad_words_ids , [[1, 2, 3], [4, 5]] ) # Checks parameters that were not specified (defaults) self.assertEqual(loaded_config.top_k , 5_0 ) self.assertEqual(loaded_config.max_length , 2_0 ) self.assertEqual(loaded_config.max_time , lowerCAmelCase__ ) def _UpperCamelCase ( self : Dict ) -> Optional[Any]: UpperCAmelCase = AutoConfig.from_pretrained("gpt2" ) UpperCAmelCase = GenerationConfig.from_model_config(lowerCAmelCase__ ) UpperCAmelCase = GenerationConfig() # The generation config has loaded a few non-default parameters from the model config self.assertNotEqual(lowerCAmelCase__ , lowerCAmelCase__ ) # One of those parameters is eos_token_id -- check if it matches self.assertNotEqual(generation_config_from_model.eos_token_id , default_generation_config.eos_token_id ) self.assertEqual(generation_config_from_model.eos_token_id , model_config.eos_token_id ) def _UpperCamelCase ( self : int ) -> int: UpperCAmelCase = GenerationConfig() UpperCAmelCase = { "max_new_tokens": 1_0_2_4, "foo": "bar", } UpperCAmelCase = copy.deepcopy(lowerCAmelCase__ ) UpperCAmelCase = generation_config.update(**lowerCAmelCase__ ) # update_kwargs was not modified (no side effects) self.assertEqual(lowerCAmelCase__ , lowerCAmelCase__ ) # update_kwargs was used to update the config on valid attributes self.assertEqual(generation_config.max_new_tokens , 1_0_2_4 ) # `.update()` returns a dictionary of unused kwargs self.assertEqual(lowerCAmelCase__ , {"foo": "bar"} ) def _UpperCamelCase ( self : List[Any] ) -> Dict: UpperCAmelCase = GenerationConfig() UpperCAmelCase = "bar" with tempfile.TemporaryDirectory("test-generation-config" ) as tmp_dir: generation_config.save_pretrained(lowerCAmelCase__ ) UpperCAmelCase = GenerationConfig.from_pretrained(lowerCAmelCase__ ) # update_kwargs was used to update the config on valid attributes self.assertEqual(new_config.foo , "bar" ) UpperCAmelCase = GenerationConfig.from_model_config(lowerCAmelCase__ ) assert not hasattr(lowerCAmelCase__ , "foo" ) # no new kwargs should be initialized if from config def _UpperCamelCase ( self : Tuple ) -> Dict: UpperCAmelCase = GenerationConfig() self.assertEqual(default_config.temperature , 1.0 ) self.assertEqual(default_config.do_sample , lowerCAmelCase__ ) self.assertEqual(default_config.num_beams , 1 ) UpperCAmelCase = GenerationConfig( do_sample=lowerCAmelCase__ , temperature=0.7 , length_penalty=1.0 , bad_words_ids=[[1, 2, 3], [4, 5]] , ) self.assertEqual(config.temperature , 0.7 ) self.assertEqual(config.do_sample , lowerCAmelCase__ ) self.assertEqual(config.num_beams , 1 ) with tempfile.TemporaryDirectory() as tmp_dir: config.save_pretrained(lowerCAmelCase__ ) UpperCAmelCase = GenerationConfig.from_pretrained(lowerCAmelCase__ , temperature=1.0 ) self.assertEqual(loaded_config.temperature , 1.0 ) self.assertEqual(loaded_config.do_sample , lowerCAmelCase__ ) self.assertEqual(loaded_config.num_beams , 1 ) # default value @is_staging_test class __magic_name__ ( unittest.TestCase ): @classmethod def _UpperCamelCase ( cls : Optional[int] ) -> Union[str, Any]: UpperCAmelCase = TOKEN HfFolder.save_token(lowerCAmelCase__ ) @classmethod def _UpperCamelCase ( cls : List[str] ) -> int: try: delete_repo(token=cls._token , repo_id="test-generation-config" ) except HTTPError: pass try: delete_repo(token=cls._token , repo_id="valid_org/test-generation-config-org" ) except HTTPError: pass def _UpperCamelCase ( self : List[Any] ) -> Any: UpperCAmelCase = GenerationConfig( do_sample=lowerCAmelCase__ , temperature=0.7 , length_penalty=1.0 , ) config.push_to_hub("test-generation-config" , use_auth_token=self._token ) UpperCAmelCase = GenerationConfig.from_pretrained(f"{USER}/test-generation-config" ) for k, v in config.to_dict().items(): if k != "transformers_version": self.assertEqual(lowerCAmelCase__ , getattr(lowerCAmelCase__ , lowerCAmelCase__ ) ) # Reset repo delete_repo(token=self._token , repo_id="test-generation-config" ) # Push to hub via save_pretrained with tempfile.TemporaryDirectory() as tmp_dir: config.save_pretrained( lowerCAmelCase__ , repo_id="test-generation-config" , push_to_hub=lowerCAmelCase__ , use_auth_token=self._token ) UpperCAmelCase = GenerationConfig.from_pretrained(f"{USER}/test-generation-config" ) for k, v in config.to_dict().items(): if k != "transformers_version": self.assertEqual(lowerCAmelCase__ , getattr(lowerCAmelCase__ , lowerCAmelCase__ ) ) def _UpperCamelCase ( self : Any ) -> Dict: UpperCAmelCase = GenerationConfig( do_sample=lowerCAmelCase__ , temperature=0.7 , length_penalty=1.0 , ) config.push_to_hub("valid_org/test-generation-config-org" , use_auth_token=self._token ) UpperCAmelCase = GenerationConfig.from_pretrained("valid_org/test-generation-config-org" ) for k, v in config.to_dict().items(): if k != "transformers_version": self.assertEqual(lowerCAmelCase__ , getattr(lowerCAmelCase__ , lowerCAmelCase__ ) ) # Reset repo delete_repo(token=self._token , repo_id="valid_org/test-generation-config-org" ) # Push to hub via save_pretrained with tempfile.TemporaryDirectory() as tmp_dir: config.save_pretrained( lowerCAmelCase__ , repo_id="valid_org/test-generation-config-org" , push_to_hub=lowerCAmelCase__ , use_auth_token=self._token ) UpperCAmelCase = GenerationConfig.from_pretrained("valid_org/test-generation-config-org" ) for k, v in config.to_dict().items(): if k != "transformers_version": self.assertEqual(lowerCAmelCase__ , getattr(lowerCAmelCase__ , lowerCAmelCase__ ) )
718
from ...configuration_utils import PretrainedConfig from ...utils import logging from ...utils.backbone_utils import BackboneConfigMixin, get_aligned_output_features_output_indices lowerCAmelCase__ = logging.get_logger(__name__) lowerCAmelCase__ = { "facebook/convnextv2-tiny-1k-224": "https://huggingface.co/facebook/convnextv2-tiny-1k-224/resolve/main/config.json", } class __magic_name__ ( _snake_case , _snake_case ): UpperCAmelCase = """convnextv2""" def __init__( self : Optional[Any] , lowerCAmelCase__ : List[Any]=3 , lowerCAmelCase__ : str=4 , lowerCAmelCase__ : Dict=4 , lowerCAmelCase__ : Optional[Any]=None , lowerCAmelCase__ : Optional[Any]=None , lowerCAmelCase__ : str="gelu" , lowerCAmelCase__ : Optional[int]=0.02 , lowerCAmelCase__ : Dict=1e-1_2 , lowerCAmelCase__ : str=0.0 , lowerCAmelCase__ : str=2_2_4 , lowerCAmelCase__ : int=None , lowerCAmelCase__ : List[Any]=None , **lowerCAmelCase__ : List[Any] , ) -> List[Any]: super().__init__(**lowerCAmelCase__ ) UpperCAmelCase = num_channels UpperCAmelCase = patch_size UpperCAmelCase = num_stages UpperCAmelCase = [9_6, 1_9_2, 3_8_4, 7_6_8] if hidden_sizes is None else hidden_sizes UpperCAmelCase = [3, 3, 9, 3] if depths is None else depths UpperCAmelCase = hidden_act UpperCAmelCase = initializer_range UpperCAmelCase = layer_norm_eps UpperCAmelCase = drop_path_rate UpperCAmelCase = image_size UpperCAmelCase = ["stem"] + [f"stage{idx}" for idx in range(1 , len(self.depths ) + 1 )] UpperCAmelCase , UpperCAmelCase = get_aligned_output_features_output_indices( out_features=lowerCAmelCase__ , out_indices=lowerCAmelCase__ , stage_names=self.stage_names )
1
0
'''simple docstring''' import json import os from functools import lru_cache from typing import List, Optional, Tuple import regex as re from ...tokenization_utils import AddedToken, PreTrainedTokenizer from ...utils import logging lowerCAmelCase__ = logging.get_logger(__name__) lowerCAmelCase__ = {"vocab_file": "vocab.json", "merges_file": "merges.txt"} lowerCAmelCase__ = { "vocab_file": { "allenai/longformer-base-4096": "https://huggingface.co/allenai/longformer-base-4096/resolve/main/vocab.json", "allenai/longformer-large-4096": ( "https://huggingface.co/allenai/longformer-large-4096/resolve/main/vocab.json" ), "allenai/longformer-large-4096-finetuned-triviaqa": ( "https://huggingface.co/allenai/longformer-large-4096-finetuned-triviaqa/resolve/main/vocab.json" ), "allenai/longformer-base-4096-extra.pos.embd.only": ( "https://huggingface.co/allenai/longformer-base-4096-extra.pos.embd.only/resolve/main/vocab.json" ), "allenai/longformer-large-4096-extra.pos.embd.only": ( "https://huggingface.co/allenai/longformer-large-4096-extra.pos.embd.only/resolve/main/vocab.json" ), }, "merges_file": { "allenai/longformer-base-4096": "https://huggingface.co/allenai/longformer-base-4096/resolve/main/merges.txt", "allenai/longformer-large-4096": ( "https://huggingface.co/allenai/longformer-large-4096/resolve/main/merges.txt" ), "allenai/longformer-large-4096-finetuned-triviaqa": ( "https://huggingface.co/allenai/longformer-large-4096-finetuned-triviaqa/resolve/main/merges.txt" ), "allenai/longformer-base-4096-extra.pos.embd.only": ( "https://huggingface.co/allenai/longformer-base-4096-extra.pos.embd.only/resolve/main/merges.txt" ), "allenai/longformer-large-4096-extra.pos.embd.only": ( "https://huggingface.co/allenai/longformer-large-4096-extra.pos.embd.only/resolve/main/merges.txt" ), }, } lowerCAmelCase__ = { "allenai/longformer-base-4096": 4096, "allenai/longformer-large-4096": 4096, "allenai/longformer-large-4096-finetuned-triviaqa": 4096, "allenai/longformer-base-4096-extra.pos.embd.only": 4096, "allenai/longformer-large-4096-extra.pos.embd.only": 4096, } @lru_cache() # Copied from transformers.models.roberta.tokenization_roberta.bytes_to_unicode def _lowerCAmelCase( ): UpperCAmelCase = ( list(range(ord("!" ) , ord("~" ) + 1 ) ) + list(range(ord("¡" ) , ord("¬" ) + 1 ) ) + list(range(ord("®" ) , ord("ÿ" ) + 1 ) ) ) UpperCAmelCase = bs[:] UpperCAmelCase = 0 for b in range(2**8 ): if b not in bs: bs.append(__A ) cs.append(2**8 + n ) n += 1 UpperCAmelCase = [chr(__A ) for n in cs] return dict(zip(__A , __A ) ) def _lowerCAmelCase( __A ): UpperCAmelCase = set() UpperCAmelCase = word[0] for char in word[1:]: pairs.add((prev_char, char) ) UpperCAmelCase = char return pairs class __magic_name__ ( _snake_case ): UpperCAmelCase = VOCAB_FILES_NAMES UpperCAmelCase = PRETRAINED_VOCAB_FILES_MAP UpperCAmelCase = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES UpperCAmelCase = ["""input_ids""", """attention_mask"""] def __init__( self : List[str] , lowerCAmelCase__ : Any , lowerCAmelCase__ : str , lowerCAmelCase__ : Tuple="replace" , lowerCAmelCase__ : str="<s>" , lowerCAmelCase__ : int="</s>" , lowerCAmelCase__ : int="</s>" , lowerCAmelCase__ : Dict="<s>" , lowerCAmelCase__ : Dict="<unk>" , lowerCAmelCase__ : Optional[Any]="<pad>" , lowerCAmelCase__ : int="<mask>" , lowerCAmelCase__ : str=False , **lowerCAmelCase__ : List[Any] , ) -> List[str]: UpperCAmelCase = AddedToken(lowerCAmelCase__ , lstrip=lowerCAmelCase__ , rstrip=lowerCAmelCase__ ) if isinstance(lowerCAmelCase__ , lowerCAmelCase__ ) else bos_token UpperCAmelCase = AddedToken(lowerCAmelCase__ , lstrip=lowerCAmelCase__ , rstrip=lowerCAmelCase__ ) if isinstance(lowerCAmelCase__ , lowerCAmelCase__ ) else eos_token UpperCAmelCase = AddedToken(lowerCAmelCase__ , lstrip=lowerCAmelCase__ , rstrip=lowerCAmelCase__ ) if isinstance(lowerCAmelCase__ , lowerCAmelCase__ ) else sep_token UpperCAmelCase = AddedToken(lowerCAmelCase__ , lstrip=lowerCAmelCase__ , rstrip=lowerCAmelCase__ ) if isinstance(lowerCAmelCase__ , lowerCAmelCase__ ) else cls_token UpperCAmelCase = AddedToken(lowerCAmelCase__ , lstrip=lowerCAmelCase__ , rstrip=lowerCAmelCase__ ) if isinstance(lowerCAmelCase__ , lowerCAmelCase__ ) else unk_token UpperCAmelCase = AddedToken(lowerCAmelCase__ , lstrip=lowerCAmelCase__ , rstrip=lowerCAmelCase__ ) if isinstance(lowerCAmelCase__ , lowerCAmelCase__ ) else pad_token # Mask token behave like a normal word, i.e. include the space before it UpperCAmelCase = AddedToken(lowerCAmelCase__ , lstrip=lowerCAmelCase__ , rstrip=lowerCAmelCase__ ) if isinstance(lowerCAmelCase__ , lowerCAmelCase__ ) else mask_token super().__init__( errors=lowerCAmelCase__ , bos_token=lowerCAmelCase__ , eos_token=lowerCAmelCase__ , unk_token=lowerCAmelCase__ , sep_token=lowerCAmelCase__ , cls_token=lowerCAmelCase__ , pad_token=lowerCAmelCase__ , mask_token=lowerCAmelCase__ , add_prefix_space=lowerCAmelCase__ , **lowerCAmelCase__ , ) with open(lowerCAmelCase__ , encoding="utf-8" ) as vocab_handle: UpperCAmelCase = json.load(lowerCAmelCase__ ) UpperCAmelCase = {v: k for k, v in self.encoder.items()} UpperCAmelCase = errors # how to handle errors in decoding UpperCAmelCase = bytes_to_unicode() UpperCAmelCase = {v: k for k, v in self.byte_encoder.items()} with open(lowerCAmelCase__ , encoding="utf-8" ) as merges_handle: UpperCAmelCase = merges_handle.read().split("\n" )[1:-1] UpperCAmelCase = [tuple(merge.split() ) for merge in bpe_merges] UpperCAmelCase = dict(zip(lowerCAmelCase__ , range(len(lowerCAmelCase__ ) ) ) ) UpperCAmelCase = {} UpperCAmelCase = add_prefix_space # Should have added re.IGNORECASE so BPE merges can happen for capitalized versions of contractions UpperCAmelCase = re.compile(r"'s|'t|'re|'ve|'m|'ll|'d| ?\p{L}+| ?\p{N}+| ?[^\s\p{L}\p{N}]+|\s+(?!\S)|\s+" ) @property def _UpperCamelCase ( self : List[Any] ) -> Optional[int]: return len(self.encoder ) def _UpperCamelCase ( self : Optional[int] ) -> str: return dict(self.encoder , **self.added_tokens_encoder ) def _UpperCamelCase ( self : List[str] , lowerCAmelCase__ : Optional[int] ) -> int: if token in self.cache: return self.cache[token] UpperCAmelCase = tuple(lowerCAmelCase__ ) UpperCAmelCase = get_pairs(lowerCAmelCase__ ) if not pairs: return token while True: UpperCAmelCase = min(lowerCAmelCase__ , key=lambda lowerCAmelCase__ : self.bpe_ranks.get(lowerCAmelCase__ , float("inf" ) ) ) if bigram not in self.bpe_ranks: break UpperCAmelCase , UpperCAmelCase = bigram UpperCAmelCase = [] UpperCAmelCase = 0 while i < len(lowerCAmelCase__ ): try: UpperCAmelCase = word.index(lowerCAmelCase__ , lowerCAmelCase__ ) except ValueError: new_word.extend(word[i:] ) break else: new_word.extend(word[i:j] ) UpperCAmelCase = j if word[i] == first and i < len(lowerCAmelCase__ ) - 1 and word[i + 1] == second: new_word.append(first + second ) i += 2 else: new_word.append(word[i] ) i += 1 UpperCAmelCase = tuple(lowerCAmelCase__ ) UpperCAmelCase = new_word if len(lowerCAmelCase__ ) == 1: break else: UpperCAmelCase = get_pairs(lowerCAmelCase__ ) UpperCAmelCase = " ".join(lowerCAmelCase__ ) UpperCAmelCase = word return word def _UpperCamelCase ( self : List[Any] , lowerCAmelCase__ : List[str] ) -> str: UpperCAmelCase = [] for token in re.findall(self.pat , lowerCAmelCase__ ): UpperCAmelCase = "".join( self.byte_encoder[b] for b in token.encode("utf-8" ) ) # Maps all our bytes to unicode strings, avoiding control tokens of the BPE (spaces in our case) bpe_tokens.extend(bpe_token for bpe_token in self.bpe(lowerCAmelCase__ ).split(" " ) ) return bpe_tokens def _UpperCamelCase ( self : str , lowerCAmelCase__ : List[str] ) -> str: return self.encoder.get(lowerCAmelCase__ , self.encoder.get(self.unk_token ) ) def _UpperCamelCase ( self : Tuple , lowerCAmelCase__ : Dict ) -> Tuple: return self.decoder.get(lowerCAmelCase__ ) def _UpperCamelCase ( self : List[str] , lowerCAmelCase__ : Dict ) -> Tuple: UpperCAmelCase = "".join(lowerCAmelCase__ ) UpperCAmelCase = bytearray([self.byte_decoder[c] for c in text] ).decode("utf-8" , errors=self.errors ) return text def _UpperCamelCase ( self : str , lowerCAmelCase__ : str , lowerCAmelCase__ : Optional[str] = None ) -> Tuple[str]: if not os.path.isdir(lowerCAmelCase__ ): logger.error(f"Vocabulary path ({save_directory}) should be a directory" ) return UpperCAmelCase = os.path.join( lowerCAmelCase__ , (filename_prefix + "-" if filename_prefix else "") + VOCAB_FILES_NAMES["vocab_file"] ) UpperCAmelCase = os.path.join( lowerCAmelCase__ , (filename_prefix + "-" if filename_prefix else "") + VOCAB_FILES_NAMES["merges_file"] ) with open(lowerCAmelCase__ , "w" , encoding="utf-8" ) as f: f.write(json.dumps(self.encoder , indent=2 , sort_keys=lowerCAmelCase__ , ensure_ascii=lowerCAmelCase__ ) + "\n" ) UpperCAmelCase = 0 with open(lowerCAmelCase__ , "w" , encoding="utf-8" ) as writer: writer.write("#version: 0.2\n" ) for bpe_tokens, token_index in sorted(self.bpe_ranks.items() , key=lambda lowerCAmelCase__ : kv[1] ): if index != token_index: logger.warning( f"Saving vocabulary to {merge_file}: BPE merge indices are not consecutive." " Please check that the tokenizer is not corrupted!" ) UpperCAmelCase = token_index writer.write(" ".join(lowerCAmelCase__ ) + "\n" ) index += 1 return vocab_file, merge_file def _UpperCamelCase ( self : Any , lowerCAmelCase__ : List[int] , lowerCAmelCase__ : Optional[List[int]] = None ) -> List[int]: 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 : str , lowerCAmelCase__ : List[int] , lowerCAmelCase__ : Optional[List[int]] = None , lowerCAmelCase__ : bool = False ) -> List[int]: if already_has_special_tokens: return super().get_special_tokens_mask( token_ids_a=lowerCAmelCase__ , token_ids_a=lowerCAmelCase__ , already_has_special_tokens=lowerCAmelCase__ ) if token_ids_a is None: return [1] + ([0] * len(lowerCAmelCase__ )) + [1] return [1] + ([0] * len(lowerCAmelCase__ )) + [1, 1] + ([0] * len(lowerCAmelCase__ )) + [1] def _UpperCamelCase ( self : Optional[int] , lowerCAmelCase__ : List[int] , lowerCAmelCase__ : Optional[List[int]] = None ) -> List[int]: 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] def _UpperCamelCase ( self : str , lowerCAmelCase__ : Optional[int] , lowerCAmelCase__ : Optional[int]=False , **lowerCAmelCase__ : List[Any] ) -> str: UpperCAmelCase = kwargs.pop("add_prefix_space" , self.add_prefix_space ) if (is_split_into_words or add_prefix_space) and (len(lowerCAmelCase__ ) > 0 and not text[0].isspace()): UpperCAmelCase = " " + text return (text, kwargs)
719
lowerCAmelCase__ = "\n# Transformers 설치 방법\n! pip install transformers datasets\n# 마지막 릴리스 대신 소스에서 설치하려면, 위 명령을 주석으로 바꾸고 아래 명령을 해제하세요.\n# ! pip install git+https://github.com/huggingface/transformers.git\n" lowerCAmelCase__ = [{"type": "code", "content": INSTALL_CONTENT}] lowerCAmelCase__ = { "{processor_class}": "FakeProcessorClass", "{model_class}": "FakeModelClass", "{object_class}": "FakeObjectClass", }
1
0
import pytest import datasets # Import fixture modules as plugins lowerCAmelCase__ = ["tests.fixtures.files", "tests.fixtures.hub", "tests.fixtures.fsspec"] def _lowerCAmelCase( __A , __A ): # Mark tests as "unit" by default if not marked as "integration" (or already marked as "unit") for item in items: if any(marker in item.keywords for marker in ["integration", "unit"] ): continue item.add_marker(pytest.mark.unit ) def _lowerCAmelCase( __A ): config.addinivalue_line("markers" , "torchaudio_latest: mark test to run with torchaudio>=0.12" ) @pytest.fixture(autouse=__A ) def _lowerCAmelCase( __A , __A ): # test_hf_cache_home = tmp_path_factory.mktemp("cache") # TODO: why a cache dir per test function does not work? UpperCAmelCase = tmp_path_factory.getbasetemp() / "cache" UpperCAmelCase = test_hf_cache_home / "datasets" UpperCAmelCase = test_hf_cache_home / "metrics" UpperCAmelCase = test_hf_cache_home / "modules" monkeypatch.setattr("datasets.config.HF_DATASETS_CACHE" , str(__A ) ) monkeypatch.setattr("datasets.config.HF_METRICS_CACHE" , str(__A ) ) monkeypatch.setattr("datasets.config.HF_MODULES_CACHE" , str(__A ) ) UpperCAmelCase = test_hf_datasets_cache / "downloads" monkeypatch.setattr("datasets.config.DOWNLOADED_DATASETS_PATH" , str(__A ) ) UpperCAmelCase = test_hf_datasets_cache / "downloads" / "extracted" monkeypatch.setattr("datasets.config.EXTRACTED_DATASETS_PATH" , str(__A ) ) @pytest.fixture(autouse=__A , scope="session" ) def _lowerCAmelCase( ): datasets.disable_progress_bar() @pytest.fixture(autouse=__A ) def _lowerCAmelCase( __A ): # don't take tests into account when counting downloads monkeypatch.setattr("datasets.config.HF_UPDATE_DOWNLOAD_COUNTS" , __A ) @pytest.fixture def _lowerCAmelCase( __A ): # Required to suppress RemovedIn20Warning when feature(s) are not compatible with SQLAlchemy 2.0 # To be removed once SQLAlchemy 2.0 supported monkeypatch.setattr("sqlalchemy.util.deprecations.SILENCE_UBER_WARNING" , __A )
720
import gc import random import unittest import numpy as np import torch from PIL import Image from transformers import XLMRobertaTokenizerFast from diffusers import DDIMScheduler, KandinskyInpaintPipeline, KandinskyPriorPipeline, UNetaDConditionModel, VQModel from diffusers.pipelines.kandinsky.text_encoder import MCLIPConfig, MultilingualCLIP from diffusers.utils import floats_tensor, load_image, load_numpy, slow, torch_device from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu from ..test_pipelines_common import PipelineTesterMixin, assert_mean_pixel_difference enable_full_determinism() class __magic_name__ ( _snake_case , unittest.TestCase ): UpperCAmelCase = KandinskyInpaintPipeline UpperCAmelCase = ["""prompt""", """image_embeds""", """negative_image_embeds""", """image""", """mask_image"""] UpperCAmelCase = [ """prompt""", """negative_prompt""", """image_embeds""", """negative_image_embeds""", """image""", """mask_image""", ] UpperCAmelCase = [ """generator""", """height""", """width""", """latents""", """guidance_scale""", """negative_prompt""", """num_inference_steps""", """return_dict""", """guidance_scale""", """num_images_per_prompt""", """output_type""", """return_dict""", ] UpperCAmelCase = False @property def _UpperCamelCase ( self : Union[str, Any] ) -> Tuple: return 3_2 @property def _UpperCamelCase ( self : int ) -> List[Any]: return 3_2 @property def _UpperCamelCase ( self : List[Any] ) -> List[Any]: return self.time_input_dim @property def _UpperCamelCase ( self : Tuple ) -> Tuple: return self.time_input_dim * 4 @property def _UpperCamelCase ( self : Any ) -> Optional[int]: return 1_0_0 @property def _UpperCamelCase ( self : Optional[int] ) -> Optional[int]: UpperCAmelCase = XLMRobertaTokenizerFast.from_pretrained("YiYiXu/tiny-random-mclip-base" ) return tokenizer @property def _UpperCamelCase ( self : int ) -> Dict: torch.manual_seed(0 ) UpperCAmelCase = MCLIPConfig( numDims=self.cross_attention_dim , transformerDimensions=self.text_embedder_hidden_size , hidden_size=self.text_embedder_hidden_size , intermediate_size=3_7 , num_attention_heads=4 , num_hidden_layers=5 , vocab_size=1_0_0_5 , ) UpperCAmelCase = MultilingualCLIP(lowerCAmelCase__ ) UpperCAmelCase = text_encoder.eval() return text_encoder @property def _UpperCamelCase ( self : Dict ) -> Optional[int]: torch.manual_seed(0 ) UpperCAmelCase = { "in_channels": 9, # Out channels is double in channels because predicts mean and variance "out_channels": 8, "addition_embed_type": "text_image", "down_block_types": ("ResnetDownsampleBlock2D", "SimpleCrossAttnDownBlock2D"), "up_block_types": ("SimpleCrossAttnUpBlock2D", "ResnetUpsampleBlock2D"), "mid_block_type": "UNetMidBlock2DSimpleCrossAttn", "block_out_channels": (self.block_out_channels_a, self.block_out_channels_a * 2), "layers_per_block": 1, "encoder_hid_dim": self.text_embedder_hidden_size, "encoder_hid_dim_type": "text_image_proj", "cross_attention_dim": self.cross_attention_dim, "attention_head_dim": 4, "resnet_time_scale_shift": "scale_shift", "class_embed_type": None, } UpperCAmelCase = UNetaDConditionModel(**lowerCAmelCase__ ) return model @property def _UpperCamelCase ( self : str ) -> Optional[Any]: return { "block_out_channels": [3_2, 6_4], "down_block_types": ["DownEncoderBlock2D", "AttnDownEncoderBlock2D"], "in_channels": 3, "latent_channels": 4, "layers_per_block": 1, "norm_num_groups": 8, "norm_type": "spatial", "num_vq_embeddings": 1_2, "out_channels": 3, "up_block_types": [ "AttnUpDecoderBlock2D", "UpDecoderBlock2D", ], "vq_embed_dim": 4, } @property def _UpperCamelCase ( self : Dict ) -> List[Any]: torch.manual_seed(0 ) UpperCAmelCase = VQModel(**self.dummy_movq_kwargs ) return model def _UpperCamelCase ( self : Tuple ) -> Any: UpperCAmelCase = self.dummy_text_encoder UpperCAmelCase = self.dummy_tokenizer UpperCAmelCase = self.dummy_unet UpperCAmelCase = self.dummy_movq UpperCAmelCase = DDIMScheduler( num_train_timesteps=1_0_0_0 , beta_schedule="linear" , beta_start=0.00_085 , beta_end=0.012 , clip_sample=lowerCAmelCase__ , set_alpha_to_one=lowerCAmelCase__ , steps_offset=1 , prediction_type="epsilon" , thresholding=lowerCAmelCase__ , ) UpperCAmelCase = { "text_encoder": text_encoder, "tokenizer": tokenizer, "unet": unet, "scheduler": scheduler, "movq": movq, } return components def _UpperCamelCase ( self : Union[str, Any] , lowerCAmelCase__ : Any , lowerCAmelCase__ : Tuple=0 ) -> str: UpperCAmelCase = floats_tensor((1, self.cross_attention_dim) , rng=random.Random(lowerCAmelCase__ ) ).to(lowerCAmelCase__ ) UpperCAmelCase = floats_tensor((1, self.cross_attention_dim) , rng=random.Random(seed + 1 ) ).to(lowerCAmelCase__ ) # create init_image UpperCAmelCase = floats_tensor((1, 3, 6_4, 6_4) , rng=random.Random(lowerCAmelCase__ ) ).to(lowerCAmelCase__ ) UpperCAmelCase = image.cpu().permute(0 , 2 , 3 , 1 )[0] UpperCAmelCase = Image.fromarray(np.uinta(lowerCAmelCase__ ) ).convert("RGB" ).resize((2_5_6, 2_5_6) ) # create mask UpperCAmelCase = np.ones((6_4, 6_4) , dtype=np.floataa ) UpperCAmelCase = 0 if str(lowerCAmelCase__ ).startswith("mps" ): UpperCAmelCase = torch.manual_seed(lowerCAmelCase__ ) else: UpperCAmelCase = torch.Generator(device=lowerCAmelCase__ ).manual_seed(lowerCAmelCase__ ) UpperCAmelCase = { "prompt": "horse", "image": init_image, "mask_image": mask, "image_embeds": image_embeds, "negative_image_embeds": negative_image_embeds, "generator": generator, "height": 6_4, "width": 6_4, "num_inference_steps": 2, "guidance_scale": 4.0, "output_type": "np", } return inputs def _UpperCamelCase ( self : Dict ) -> List[str]: UpperCAmelCase = "cpu" UpperCAmelCase = self.get_dummy_components() UpperCAmelCase = self.pipeline_class(**lowerCAmelCase__ ) UpperCAmelCase = pipe.to(lowerCAmelCase__ ) pipe.set_progress_bar_config(disable=lowerCAmelCase__ ) UpperCAmelCase = pipe(**self.get_dummy_inputs(lowerCAmelCase__ ) ) UpperCAmelCase = output.images UpperCAmelCase = pipe( **self.get_dummy_inputs(lowerCAmelCase__ ) , return_dict=lowerCAmelCase__ , )[0] UpperCAmelCase = image[0, -3:, -3:, -1] UpperCAmelCase = image_from_tuple[0, -3:, -3:, -1] print(f"image.shape {image.shape}" ) assert image.shape == (1, 6_4, 6_4, 3) UpperCAmelCase = np.array( [0.8_326_919, 0.73_790_467, 0.20_918_581, 0.9_309_612, 0.5_511_791, 0.43_713_328, 0.5_513_321, 0.49_922_934, 0.59_497_786] ) assert ( np.abs(image_slice.flatten() - expected_slice ).max() < 1e-2 ), f" expected_slice {expected_slice}, but got {image_slice.flatten()}" assert ( np.abs(image_from_tuple_slice.flatten() - expected_slice ).max() < 1e-2 ), f" expected_slice {expected_slice}, but got {image_from_tuple_slice.flatten()}" def _UpperCamelCase ( self : str ) -> Tuple: super().test_inference_batch_single_identical(expected_max_diff=3e-3 ) @slow @require_torch_gpu class __magic_name__ ( unittest.TestCase ): def _UpperCamelCase ( self : str ) -> str: # clean up the VRAM after each test super().tearDown() gc.collect() torch.cuda.empty_cache() def _UpperCamelCase ( self : Tuple ) -> int: UpperCAmelCase = load_numpy( "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main" "/kandinsky/kandinsky_inpaint_cat_with_hat_fp16.npy" ) UpperCAmelCase = load_image( "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main" "/kandinsky/cat.png" ) UpperCAmelCase = np.ones((7_6_8, 7_6_8) , dtype=np.floataa ) UpperCAmelCase = 0 UpperCAmelCase = "a hat" UpperCAmelCase = KandinskyPriorPipeline.from_pretrained( "kandinsky-community/kandinsky-2-1-prior" , torch_dtype=torch.floataa ) pipe_prior.to(lowerCAmelCase__ ) UpperCAmelCase = KandinskyInpaintPipeline.from_pretrained( "kandinsky-community/kandinsky-2-1-inpaint" , torch_dtype=torch.floataa ) UpperCAmelCase = pipeline.to(lowerCAmelCase__ ) pipeline.set_progress_bar_config(disable=lowerCAmelCase__ ) UpperCAmelCase = torch.Generator(device="cpu" ).manual_seed(0 ) UpperCAmelCase , UpperCAmelCase = pipe_prior( lowerCAmelCase__ , generator=lowerCAmelCase__ , num_inference_steps=5 , negative_prompt="" , ).to_tuple() UpperCAmelCase = pipeline( lowerCAmelCase__ , image=lowerCAmelCase__ , mask_image=lowerCAmelCase__ , image_embeds=lowerCAmelCase__ , negative_image_embeds=lowerCAmelCase__ , generator=lowerCAmelCase__ , num_inference_steps=1_0_0 , height=7_6_8 , width=7_6_8 , output_type="np" , ) UpperCAmelCase = output.images[0] assert image.shape == (7_6_8, 7_6_8, 3) assert_mean_pixel_difference(lowerCAmelCase__ , lowerCAmelCase__ )
1
0
from typing import Callable, List, Optional, Tuple, Union import torch from transformers import CLIPTextModel, CLIPTokenizer from ...configuration_utils import ConfigMixin, register_to_config from ...models import ModelMixin, TransformeraDModel, VQModel from ...schedulers import VQDiffusionScheduler from ...utils import logging from ..pipeline_utils import DiffusionPipeline, ImagePipelineOutput lowerCAmelCase__ = logging.get_logger(__name__) # pylint: disable=invalid-name class __magic_name__ ( _snake_case , _snake_case ): @register_to_config def __init__( self : Optional[Any] , lowerCAmelCase__ : bool , lowerCAmelCase__ : Optional[int] = None , lowerCAmelCase__ : Optional[int] = None ) -> Optional[int]: super().__init__() UpperCAmelCase = learnable if self.learnable: assert hidden_size is not None, "learnable=True requires `hidden_size` to be set" assert length is not None, "learnable=True requires `length` to be set" UpperCAmelCase = torch.zeros(lowerCAmelCase__ , lowerCAmelCase__ ) else: UpperCAmelCase = None UpperCAmelCase = torch.nn.Parameter(lowerCAmelCase__ ) class __magic_name__ ( _snake_case ): UpperCAmelCase = 42 UpperCAmelCase = 42 UpperCAmelCase = 42 UpperCAmelCase = 42 UpperCAmelCase = 42 UpperCAmelCase = 42 def __init__( self : str , lowerCAmelCase__ : VQModel , lowerCAmelCase__ : CLIPTextModel , lowerCAmelCase__ : CLIPTokenizer , lowerCAmelCase__ : TransformeraDModel , lowerCAmelCase__ : VQDiffusionScheduler , lowerCAmelCase__ : LearnedClassifierFreeSamplingEmbeddings , ) -> int: super().__init__() self.register_modules( vqvae=lowerCAmelCase__ , transformer=lowerCAmelCase__ , text_encoder=lowerCAmelCase__ , tokenizer=lowerCAmelCase__ , scheduler=lowerCAmelCase__ , learned_classifier_free_sampling_embeddings=lowerCAmelCase__ , ) def _UpperCamelCase ( self : List[Any] , lowerCAmelCase__ : Optional[Any] , lowerCAmelCase__ : Optional[int] , lowerCAmelCase__ : int ) -> Union[str, Any]: UpperCAmelCase = len(lowerCAmelCase__ ) if isinstance(lowerCAmelCase__ , lowerCAmelCase__ ) else 1 # get prompt text embeddings UpperCAmelCase = self.tokenizer( lowerCAmelCase__ , padding="max_length" , max_length=self.tokenizer.model_max_length , return_tensors="pt" , ) UpperCAmelCase = text_inputs.input_ids if text_input_ids.shape[-1] > self.tokenizer.model_max_length: UpperCAmelCase = self.tokenizer.batch_decode(text_input_ids[:, self.tokenizer.model_max_length :] ) logger.warning( "The following part of your input was truncated because CLIP can only handle sequences up to" f" {self.tokenizer.model_max_length} tokens: {removed_text}" ) UpperCAmelCase = text_input_ids[:, : self.tokenizer.model_max_length] UpperCAmelCase = self.text_encoder(text_input_ids.to(self.device ) )[0] # NOTE: This additional step of normalizing the text embeddings is from VQ-Diffusion. # While CLIP does normalize the pooled output of the text transformer when combining # the image and text embeddings, CLIP does not directly normalize the last hidden state. # # CLIP normalizing the pooled output. # https://github.com/huggingface/transformers/blob/d92e22d1f28324f513f3080e5c47c071a3916721/src/transformers/models/clip/modeling_clip.py#L1052-L1053 UpperCAmelCase = prompt_embeds / prompt_embeds.norm(dim=-1 , keepdim=lowerCAmelCase__ ) # duplicate text embeddings for each generation per prompt UpperCAmelCase = prompt_embeds.repeat_interleave(lowerCAmelCase__ , dim=0 ) if do_classifier_free_guidance: if self.learned_classifier_free_sampling_embeddings.learnable: UpperCAmelCase = self.learned_classifier_free_sampling_embeddings.embeddings UpperCAmelCase = negative_prompt_embeds.unsqueeze(0 ).repeat(lowerCAmelCase__ , 1 , 1 ) else: UpperCAmelCase = [""] * batch_size UpperCAmelCase = text_input_ids.shape[-1] UpperCAmelCase = self.tokenizer( lowerCAmelCase__ , padding="max_length" , max_length=lowerCAmelCase__ , truncation=lowerCAmelCase__ , return_tensors="pt" , ) UpperCAmelCase = self.text_encoder(uncond_input.input_ids.to(self.device ) )[0] # See comment for normalizing text embeddings UpperCAmelCase = negative_prompt_embeds / negative_prompt_embeds.norm(dim=-1 , keepdim=lowerCAmelCase__ ) # duplicate unconditional embeddings for each generation per prompt, using mps friendly method UpperCAmelCase = negative_prompt_embeds.shape[1] UpperCAmelCase = negative_prompt_embeds.repeat(1 , lowerCAmelCase__ , 1 ) UpperCAmelCase = negative_prompt_embeds.view(batch_size * num_images_per_prompt , lowerCAmelCase__ , -1 ) # For classifier free guidance, we need to do two forward passes. # Here we concatenate the unconditional and text embeddings into a single batch # to avoid doing two forward passes UpperCAmelCase = torch.cat([negative_prompt_embeds, prompt_embeds] ) return prompt_embeds @torch.no_grad() def __call__( self : Optional[Any] , lowerCAmelCase__ : Union[str, List[str]] , lowerCAmelCase__ : int = 1_0_0 , lowerCAmelCase__ : float = 5.0 , lowerCAmelCase__ : float = 1.0 , lowerCAmelCase__ : int = 1 , lowerCAmelCase__ : Optional[Union[torch.Generator, List[torch.Generator]]] = None , lowerCAmelCase__ : Optional[torch.FloatTensor] = None , lowerCAmelCase__ : Optional[str] = "pil" , lowerCAmelCase__ : bool = True , lowerCAmelCase__ : Optional[Callable[[int, int, torch.FloatTensor], None]] = None , lowerCAmelCase__ : int = 1 , ) -> Union[ImagePipelineOutput, Tuple]: if isinstance(lowerCAmelCase__ , lowerCAmelCase__ ): UpperCAmelCase = 1 elif isinstance(lowerCAmelCase__ , lowerCAmelCase__ ): UpperCAmelCase = len(lowerCAmelCase__ ) else: raise ValueError(f"`prompt` has to be of type `str` or `list` but is {type(lowerCAmelCase__ )}" ) UpperCAmelCase = batch_size * num_images_per_prompt UpperCAmelCase = guidance_scale > 1.0 UpperCAmelCase = self._encode_prompt(lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ ) if (callback_steps is None) or ( callback_steps is not None and (not isinstance(lowerCAmelCase__ , lowerCAmelCase__ ) or callback_steps <= 0) ): raise ValueError( f"`callback_steps` has to be a positive integer but is {callback_steps} of type" f" {type(lowerCAmelCase__ )}." ) # get the initial completely masked latents unless the user supplied it UpperCAmelCase = (batch_size, self.transformer.num_latent_pixels) if latents is None: UpperCAmelCase = self.transformer.num_vector_embeds - 1 UpperCAmelCase = torch.full(lowerCAmelCase__ , lowerCAmelCase__ ).to(self.device ) else: if latents.shape != latents_shape: raise ValueError(f"Unexpected latents shape, got {latents.shape}, expected {latents_shape}" ) if (latents < 0).any() or (latents >= self.transformer.num_vector_embeds).any(): raise ValueError( "Unexpected latents value(s). All latents be valid embedding indices i.e. in the range 0," f" {self.transformer.num_vector_embeds - 1} (inclusive)." ) UpperCAmelCase = latents.to(self.device ) # set timesteps self.scheduler.set_timesteps(lowerCAmelCase__ , device=self.device ) UpperCAmelCase = self.scheduler.timesteps.to(self.device ) UpperCAmelCase = latents for i, t in enumerate(self.progress_bar(lowerCAmelCase__ ) ): # expand the sample if we are doing classifier free guidance UpperCAmelCase = torch.cat([sample] * 2 ) if do_classifier_free_guidance else sample # predict the un-noised image # model_output == `log_p_x_0` UpperCAmelCase = self.transformer(lowerCAmelCase__ , encoder_hidden_states=lowerCAmelCase__ , timestep=lowerCAmelCase__ ).sample if do_classifier_free_guidance: UpperCAmelCase , UpperCAmelCase = model_output.chunk(2 ) UpperCAmelCase = model_output_uncond + guidance_scale * (model_output_text - model_output_uncond) model_output -= torch.logsumexp(lowerCAmelCase__ , dim=1 , keepdim=lowerCAmelCase__ ) UpperCAmelCase = self.truncate(lowerCAmelCase__ , lowerCAmelCase__ ) # remove `log(0)`'s (`-inf`s) UpperCAmelCase = model_output.clamp(-7_0 ) # compute the previous noisy sample x_t -> x_t-1 UpperCAmelCase = self.scheduler.step(lowerCAmelCase__ , timestep=lowerCAmelCase__ , sample=lowerCAmelCase__ , generator=lowerCAmelCase__ ).prev_sample # call the callback, if provided if callback is not None and i % callback_steps == 0: callback(lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ ) UpperCAmelCase = self.vqvae.config.vq_embed_dim UpperCAmelCase = (batch_size, self.transformer.height, self.transformer.width, embedding_channels) UpperCAmelCase = self.vqvae.quantize.get_codebook_entry(lowerCAmelCase__ , shape=lowerCAmelCase__ ) UpperCAmelCase = self.vqvae.decode(lowerCAmelCase__ , force_not_quantize=lowerCAmelCase__ ).sample UpperCAmelCase = (image / 2 + 0.5).clamp(0 , 1 ) UpperCAmelCase = image.cpu().permute(0 , 2 , 3 , 1 ).numpy() if output_type == "pil": UpperCAmelCase = self.numpy_to_pil(lowerCAmelCase__ ) if not return_dict: return (image,) return ImagePipelineOutput(images=lowerCAmelCase__ ) def _UpperCamelCase ( self : Union[str, Any] , lowerCAmelCase__ : torch.FloatTensor , lowerCAmelCase__ : float ) -> torch.FloatTensor: UpperCAmelCase , UpperCAmelCase = torch.sort(lowerCAmelCase__ , 1 , descending=lowerCAmelCase__ ) UpperCAmelCase = torch.exp(lowerCAmelCase__ ) UpperCAmelCase = sorted_p_x_0.cumsum(dim=1 ) < truncation_rate # Ensure that at least the largest probability is not zeroed out UpperCAmelCase = torch.full_like(keep_mask[:, 0:1, :] , lowerCAmelCase__ ) UpperCAmelCase = torch.cat((all_true, keep_mask) , dim=1 ) UpperCAmelCase = keep_mask[:, :-1, :] UpperCAmelCase = keep_mask.gather(1 , indices.argsort(1 ) ) UpperCAmelCase = log_p_x_0.clone() UpperCAmelCase = -torch.inf # -inf = log(0) return rv
721
def _lowerCAmelCase( __A , __A ): return (pointa[0] - pointa[0]) ** 2 + (pointa[1] - pointa[1]) ** 2 def _lowerCAmelCase( __A , __A=0 ): return sorted(__A , key=lambda __A : x[column] ) def _lowerCAmelCase( __A , __A , __A=float("inf" ) ): for i in range(points_counts - 1 ): for j in range(i + 1 , __A ): UpperCAmelCase = euclidean_distance_sqr(points[i] , points[j] ) if current_dis < min_dis: UpperCAmelCase = current_dis return min_dis def _lowerCAmelCase( __A , __A , __A=float("inf" ) ): for i in range(min(6 , points_counts - 1 ) , __A ): for j in range(max(0 , i - 6 ) , __A ): UpperCAmelCase = euclidean_distance_sqr(points[i] , points[j] ) if current_dis < min_dis: UpperCAmelCase = current_dis return min_dis def _lowerCAmelCase( __A , __A , __A ): # base case if points_counts <= 3: return dis_between_closest_pair(__A , __A ) # recursion UpperCAmelCase = points_counts // 2 UpperCAmelCase = closest_pair_of_points_sqr( __A , points_sorted_on_y[:mid] , __A ) UpperCAmelCase = closest_pair_of_points_sqr( __A , points_sorted_on_y[mid:] , points_counts - mid ) UpperCAmelCase = min(__A , __A ) UpperCAmelCase = [] for point in points_sorted_on_x: if abs(point[0] - points_sorted_on_x[mid][0] ) < closest_pair_dis: cross_strip.append(__A ) UpperCAmelCase = dis_between_closest_in_strip( __A , len(__A ) , __A ) return min(__A , __A ) def _lowerCAmelCase( __A , __A ): UpperCAmelCase = column_based_sort(__A , column=0 ) UpperCAmelCase = column_based_sort(__A , column=1 ) return ( closest_pair_of_points_sqr( __A , __A , __A ) ) ** 0.5 if __name__ == "__main__": lowerCAmelCase__ = [(2, 3), (12, 30), (40, 50), (5, 1), (12, 10), (3, 4)] print("Distance:", closest_pair_of_points(points, len(points)))
1
0
from typing import Any class __magic_name__ : def __init__( self : int , lowerCAmelCase__ : Any ) -> Tuple: UpperCAmelCase = data UpperCAmelCase = None class __magic_name__ : def __init__( self : Tuple ) -> Tuple: UpperCAmelCase = None def _UpperCamelCase ( self : Dict ) -> List[str]: UpperCAmelCase = self.head while temp is not None: print(temp.data , end=" " ) UpperCAmelCase = temp.next print() def _UpperCamelCase ( self : str , lowerCAmelCase__ : Any ) -> Optional[int]: UpperCAmelCase = Node(lowerCAmelCase__ ) UpperCAmelCase = self.head UpperCAmelCase = new_node def _UpperCamelCase ( self : str , lowerCAmelCase__ : Any , lowerCAmelCase__ : int ) -> str: if node_data_a == node_data_a: return else: UpperCAmelCase = self.head while node_a is not None and node_a.data != node_data_a: UpperCAmelCase = node_a.next UpperCAmelCase = self.head while node_a is not None and node_a.data != node_data_a: UpperCAmelCase = node_a.next if node_a is None or node_a is None: return UpperCAmelCase , UpperCAmelCase = node_a.data, node_a.data if __name__ == "__main__": lowerCAmelCase__ = LinkedList() for i in range(5, 0, -1): ll.push(i) ll.print_list() ll.swap_nodes(1, 4) print("After swapping") ll.print_list()
700
import copy import os import cva import numpy as np from matplotlib import pyplot as plt class __magic_name__ : def __init__( self : Optional[int] ) -> Optional[Any]: UpperCAmelCase = "" UpperCAmelCase = "" UpperCAmelCase = [] UpperCAmelCase = 0 UpperCAmelCase = 2_5_6 UpperCAmelCase = 0 UpperCAmelCase = 0 UpperCAmelCase = 0 UpperCAmelCase = 0 def _UpperCamelCase ( self : Any , lowerCAmelCase__ : Optional[Any] ) -> List[str]: UpperCAmelCase = cva.imread(lowerCAmelCase__ , 0 ) UpperCAmelCase = copy.deepcopy(self.img ) UpperCAmelCase , UpperCAmelCase , UpperCAmelCase = plt.hist(self.img.ravel() , 2_5_6 , [0, 2_5_6] , label="x" ) UpperCAmelCase = np.sum(lowerCAmelCase__ ) for i in range(len(lowerCAmelCase__ ) ): UpperCAmelCase = x[i] / self.k self.sk += prk UpperCAmelCase = (self.L - 1) * self.sk if self.rem != 0: UpperCAmelCase = int(last % last ) UpperCAmelCase = int(last + 1 if self.rem >= 0.5 else last ) self.last_list.append(lowerCAmelCase__ ) UpperCAmelCase = int(np.ma.count(self.img ) / self.img[1].size ) UpperCAmelCase = self.img[1].size for i in range(self.number_of_cols ): for j in range(self.number_of_rows ): UpperCAmelCase = self.img[j][i] if num != self.last_list[num]: UpperCAmelCase = self.last_list[num] cva.imwrite("output_data/output.jpg" , self.img ) def _UpperCamelCase ( self : str ) -> int: plt.hist(self.img.ravel() , 2_5_6 , [0, 2_5_6] ) def _UpperCamelCase ( self : Dict ) -> Optional[Any]: cva.imshow("Output-Image" , self.img ) cva.imshow("Input-Image" , self.original_image ) cva.waitKey(5_0_0_0 ) cva.destroyAllWindows() if __name__ == "__main__": lowerCAmelCase__ = os.path.join(os.path.basename(__file__), "image_data/input.jpg") lowerCAmelCase__ = ConstantStretch() stretcher.stretch(file_path) stretcher.plot_histogram() stretcher.show_image()
1
0
import inspect import unittest class __magic_name__ ( unittest.TestCase ): def _UpperCamelCase ( self : Dict ) -> Tuple: try: import diffusers # noqa: F401 except ImportError: assert False def _UpperCamelCase ( self : Tuple ) -> List[Any]: import diffusers from diffusers.dependency_versions_table import deps UpperCAmelCase = inspect.getmembers(lowerCAmelCase__ , inspect.isclass ) for cls_name, cls_module in all_classes: if "dummy_" in cls_module.__module__: for backend in cls_module._backends: if backend == "k_diffusion": UpperCAmelCase = "k-diffusion" elif backend == "invisible_watermark": UpperCAmelCase = "invisible-watermark" assert backend in deps, f"{backend} is not in the deps table!"
701
import json import os import unittest from transformers import BatchEncoding, LEDTokenizer, LEDTokenizerFast from transformers.models.led.tokenization_led import VOCAB_FILES_NAMES from transformers.testing_utils import require_tokenizers, require_torch from transformers.utils import cached_property from ...test_tokenization_common import TokenizerTesterMixin @require_tokenizers class __magic_name__ ( _snake_case , unittest.TestCase ): UpperCAmelCase = LEDTokenizer UpperCAmelCase = LEDTokenizerFast UpperCAmelCase = True def _UpperCamelCase ( self : Tuple ) -> Union[str, Any]: super().setUp() UpperCAmelCase = [ "l", "o", "w", "e", "r", "s", "t", "i", "d", "n", "\u0120", "\u0120l", "\u0120n", "\u0120lo", "\u0120low", "er", "\u0120lowest", "\u0120newer", "\u0120wider", "<unk>", ] UpperCAmelCase = dict(zip(lowerCAmelCase__ , range(len(lowerCAmelCase__ ) ) ) ) UpperCAmelCase = ["#version: 0.2", "\u0120 l", "\u0120l o", "\u0120lo w", "e r", ""] UpperCAmelCase = {"unk_token": "<unk>"} UpperCAmelCase = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES["vocab_file"] ) UpperCAmelCase = os.path.join(self.tmpdirname , VOCAB_FILES_NAMES["merges_file"] ) with open(self.vocab_file , "w" , encoding="utf-8" ) as fp: fp.write(json.dumps(lowerCAmelCase__ ) + "\n" ) with open(self.merges_file , "w" , encoding="utf-8" ) as fp: fp.write("\n".join(lowerCAmelCase__ ) ) def _UpperCamelCase ( self : Union[str, Any] , **lowerCAmelCase__ : Optional[int] ) -> Optional[int]: kwargs.update(self.special_tokens_map ) return self.tokenizer_class.from_pretrained(self.tmpdirname , **lowerCAmelCase__ ) def _UpperCamelCase ( self : str , **lowerCAmelCase__ : str ) -> Optional[int]: kwargs.update(self.special_tokens_map ) return self.rust_tokenizer_class.from_pretrained(self.tmpdirname , **lowerCAmelCase__ ) def _UpperCamelCase ( self : List[str] , lowerCAmelCase__ : List[Any] ) -> List[Any]: return "lower newer", "lower newer" @cached_property def _UpperCamelCase ( self : Dict ) -> str: return LEDTokenizer.from_pretrained("allenai/led-base-16384" ) @cached_property def _UpperCamelCase ( self : int ) -> Tuple: return LEDTokenizerFast.from_pretrained("allenai/led-base-16384" ) @require_torch def _UpperCamelCase ( self : Tuple ) -> List[str]: UpperCAmelCase = ["A long paragraph for summarization.", "Another paragraph for summarization."] UpperCAmelCase = [0, 2_5_0, 2_5_1, 1_7_8_1_8, 1_3, 3_9_1_8_6, 1_9_3_8, 4, 2] for tokenizer in [self.default_tokenizer, self.default_tokenizer_fast]: UpperCAmelCase = tokenizer(lowerCAmelCase__ , max_length=len(lowerCAmelCase__ ) , padding=lowerCAmelCase__ , return_tensors="pt" ) self.assertIsInstance(lowerCAmelCase__ , lowerCAmelCase__ ) self.assertEqual((2, 9) , batch.input_ids.shape ) self.assertEqual((2, 9) , batch.attention_mask.shape ) UpperCAmelCase = batch.input_ids.tolist()[0] self.assertListEqual(lowerCAmelCase__ , lowerCAmelCase__ ) @require_torch def _UpperCamelCase ( self : Union[str, Any] ) -> List[Any]: UpperCAmelCase = ["A long paragraph for summarization.", "Another paragraph for summarization."] for tokenizer in [self.default_tokenizer, self.default_tokenizer_fast]: UpperCAmelCase = tokenizer(lowerCAmelCase__ , padding=lowerCAmelCase__ , return_tensors="pt" ) self.assertIn("input_ids" , lowerCAmelCase__ ) self.assertIn("attention_mask" , lowerCAmelCase__ ) self.assertNotIn("labels" , lowerCAmelCase__ ) self.assertNotIn("decoder_attention_mask" , lowerCAmelCase__ ) @require_torch def _UpperCamelCase ( self : int ) -> int: UpperCAmelCase = [ "Summary of the text.", "Another summary.", ] for tokenizer in [self.default_tokenizer, self.default_tokenizer_fast]: UpperCAmelCase = tokenizer(text_target=lowerCAmelCase__ , max_length=3_2 , padding="max_length" , return_tensors="pt" ) self.assertEqual(3_2 , targets["input_ids"].shape[1] ) @require_torch def _UpperCamelCase ( self : Any ) -> int: for tokenizer in [self.default_tokenizer, self.default_tokenizer_fast]: UpperCAmelCase = tokenizer( ["I am a small frog" * 1_0_2_4, "I am a small frog"] , padding=lowerCAmelCase__ , truncation=lowerCAmelCase__ , return_tensors="pt" ) self.assertIsInstance(lowerCAmelCase__ , lowerCAmelCase__ ) self.assertEqual(batch.input_ids.shape , (2, 5_1_2_2) ) @require_torch def _UpperCamelCase ( self : Dict ) -> Tuple: UpperCAmelCase = ["A long paragraph for summarization."] UpperCAmelCase = [ "Summary of the text.", ] for tokenizer in [self.default_tokenizer, self.default_tokenizer_fast]: UpperCAmelCase = tokenizer(lowerCAmelCase__ , return_tensors="pt" ) UpperCAmelCase = tokenizer(text_target=lowerCAmelCase__ , return_tensors="pt" ) UpperCAmelCase = inputs["input_ids"] UpperCAmelCase = targets["input_ids"] self.assertTrue((input_ids[:, 0] == tokenizer.bos_token_id).all().item() ) self.assertTrue((labels[:, 0] == tokenizer.bos_token_id).all().item() ) self.assertTrue((input_ids[:, -1] == tokenizer.eos_token_id).all().item() ) self.assertTrue((labels[:, -1] == tokenizer.eos_token_id).all().item() ) @require_torch def _UpperCamelCase ( self : Optional[int] ) -> Optional[Any]: for tokenizer in [self.default_tokenizer, self.default_tokenizer_fast]: UpperCAmelCase = ["Summary of the text.", "Another summary."] UpperCAmelCase = [[0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, -1, -1]] UpperCAmelCase = tokenizer(lowerCAmelCase__ , padding=lowerCAmelCase__ ) UpperCAmelCase = [[0] * len(lowerCAmelCase__ ) for x in encoded_output["input_ids"]] UpperCAmelCase = tokenizer.pad(lowerCAmelCase__ ) self.assertSequenceEqual(outputs["global_attention_mask"] , lowerCAmelCase__ ) def _UpperCamelCase ( self : List[str] ) -> int: pass def _UpperCamelCase ( self : Optional[Any] ) -> Union[str, Any]: for tokenizer, pretrained_name, kwargs in self.tokenizers_list: with self.subTest(f"{tokenizer.__class__.__name__} ({pretrained_name})" ): UpperCAmelCase = self.rust_tokenizer_class.from_pretrained(lowerCAmelCase__ , **lowerCAmelCase__ ) UpperCAmelCase = self.tokenizer_class.from_pretrained(lowerCAmelCase__ , **lowerCAmelCase__ ) UpperCAmelCase = "A, <mask> AllenNLP sentence." UpperCAmelCase = tokenizer_r.encode_plus(lowerCAmelCase__ , add_special_tokens=lowerCAmelCase__ , return_token_type_ids=lowerCAmelCase__ ) UpperCAmelCase = tokenizer_p.encode_plus(lowerCAmelCase__ , add_special_tokens=lowerCAmelCase__ , return_token_type_ids=lowerCAmelCase__ ) self.assertEqual(sum(tokens_r["token_type_ids"] ) , sum(tokens_p["token_type_ids"] ) ) self.assertEqual( sum(tokens_r["attention_mask"] ) / len(tokens_r["attention_mask"] ) , sum(tokens_p["attention_mask"] ) / len(tokens_p["attention_mask"] ) , ) UpperCAmelCase = tokenizer_r.convert_ids_to_tokens(tokens_r["input_ids"] ) UpperCAmelCase = tokenizer_p.convert_ids_to_tokens(tokens_p["input_ids"] ) self.assertSequenceEqual(tokens_p["input_ids"] , [0, 2_5_0, 6, 5_0_2_6_4, 3_8_2_3, 4_8_7, 2_1_9_9_2, 3_6_4_5, 4, 2] ) self.assertSequenceEqual(tokens_r["input_ids"] , [0, 2_5_0, 6, 5_0_2_6_4, 3_8_2_3, 4_8_7, 2_1_9_9_2, 3_6_4_5, 4, 2] ) self.assertSequenceEqual( lowerCAmelCase__ , ["<s>", "A", ",", "<mask>", "ĠAllen", "N", "LP", "Ġsentence", ".", "</s>"] ) self.assertSequenceEqual( lowerCAmelCase__ , ["<s>", "A", ",", "<mask>", "ĠAllen", "N", "LP", "Ġsentence", ".", "</s>"] )
1
0
import unittest from transformers import AutoTokenizer, FalconConfig, is_torch_available from transformers.testing_utils import require_torch, slow, torch_device from ...generation.test_utils import GenerationTesterMixin from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, ids_tensor, random_attention_mask from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from transformers import ( FalconForCausalLM, FalconForQuestionAnswering, FalconForSequenceClassification, FalconForTokenClassification, FalconModel, ) class __magic_name__ : def __init__( self : Optional[Any] , lowerCAmelCase__ : Tuple , lowerCAmelCase__ : Dict=3 , lowerCAmelCase__ : str=7 , lowerCAmelCase__ : Dict=True , lowerCAmelCase__ : Tuple=True , lowerCAmelCase__ : int=False , lowerCAmelCase__ : Optional[Any]=True , lowerCAmelCase__ : List[Any]=9_9 , lowerCAmelCase__ : Union[str, Any]=3_2 , lowerCAmelCase__ : Optional[Any]=5 , lowerCAmelCase__ : Any=4 , lowerCAmelCase__ : Tuple=3_7 , lowerCAmelCase__ : Optional[int]="gelu" , lowerCAmelCase__ : int=0.1 , lowerCAmelCase__ : Tuple=0.1 , lowerCAmelCase__ : List[str]=5_1_2 , lowerCAmelCase__ : Any=1_6 , lowerCAmelCase__ : Any=2 , lowerCAmelCase__ : int=0.02 , lowerCAmelCase__ : Dict=3 , lowerCAmelCase__ : List[Any]=4 , lowerCAmelCase__ : Optional[int]=None , ) -> Dict: UpperCAmelCase = parent UpperCAmelCase = batch_size UpperCAmelCase = seq_length UpperCAmelCase = is_training UpperCAmelCase = use_input_mask UpperCAmelCase = use_token_type_ids UpperCAmelCase = use_labels 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 def _UpperCamelCase ( self : int ) -> Tuple: 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 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() return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels def _UpperCamelCase ( self : List[Any] ) -> Tuple: return FalconConfig( vocab_size=self.vocab_size , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , max_position_embeddings=self.max_position_embeddings , type_vocab_size=self.type_vocab_size , is_decoder=lowerCAmelCase__ , initializer_range=self.initializer_range , pad_token_id=1 , new_decoder_architecture=lowerCAmelCase__ , ) def _UpperCamelCase ( self : Dict , lowerCAmelCase__ : Optional[int] , lowerCAmelCase__ : List[Any] , lowerCAmelCase__ : Optional[int] , lowerCAmelCase__ : Tuple , lowerCAmelCase__ : Union[str, Any] , lowerCAmelCase__ : str , lowerCAmelCase__ : Union[str, Any] ) -> Dict: UpperCAmelCase = FalconModel(config=lowerCAmelCase__ ) model.to(lowerCAmelCase__ ) model.eval() UpperCAmelCase = model(lowerCAmelCase__ , attention_mask=lowerCAmelCase__ ) UpperCAmelCase = model(lowerCAmelCase__ ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def _UpperCamelCase ( self : Union[str, Any] , lowerCAmelCase__ : Tuple , lowerCAmelCase__ : Tuple , lowerCAmelCase__ : Union[str, Any] , lowerCAmelCase__ : Optional[int] , lowerCAmelCase__ : Union[str, Any] , lowerCAmelCase__ : str , lowerCAmelCase__ : Tuple , lowerCAmelCase__ : int , lowerCAmelCase__ : Union[str, Any] , ) -> Tuple: UpperCAmelCase = True UpperCAmelCase = FalconModel(lowerCAmelCase__ ) model.to(lowerCAmelCase__ ) model.eval() UpperCAmelCase = model( lowerCAmelCase__ , attention_mask=lowerCAmelCase__ , encoder_hidden_states=lowerCAmelCase__ , encoder_attention_mask=lowerCAmelCase__ , ) UpperCAmelCase = model( lowerCAmelCase__ , attention_mask=lowerCAmelCase__ , encoder_hidden_states=lowerCAmelCase__ , ) UpperCAmelCase = model(lowerCAmelCase__ , attention_mask=lowerCAmelCase__ ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def _UpperCamelCase ( self : Optional[Any] , lowerCAmelCase__ : List[str] , lowerCAmelCase__ : List[Any] , lowerCAmelCase__ : int , lowerCAmelCase__ : List[Any] , lowerCAmelCase__ : int , lowerCAmelCase__ : List[str] , lowerCAmelCase__ : int , lowerCAmelCase__ : Any , lowerCAmelCase__ : Dict , ) -> str: UpperCAmelCase = FalconForCausalLM(config=lowerCAmelCase__ ) model.to(lowerCAmelCase__ ) model.eval() UpperCAmelCase = model(lowerCAmelCase__ , attention_mask=lowerCAmelCase__ , labels=lowerCAmelCase__ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.vocab_size) ) def _UpperCamelCase ( self : Union[str, Any] , lowerCAmelCase__ : Optional[int] , lowerCAmelCase__ : Dict , lowerCAmelCase__ : int , lowerCAmelCase__ : Optional[int] , lowerCAmelCase__ : Tuple , lowerCAmelCase__ : Optional[Any] , lowerCAmelCase__ : List[Any] , lowerCAmelCase__ : str , lowerCAmelCase__ : Dict , ) -> Optional[Any]: UpperCAmelCase = True UpperCAmelCase = True UpperCAmelCase = FalconForCausalLM(config=lowerCAmelCase__ ) model.to(lowerCAmelCase__ ) model.eval() # first forward pass UpperCAmelCase = model( lowerCAmelCase__ , attention_mask=lowerCAmelCase__ , encoder_hidden_states=lowerCAmelCase__ , encoder_attention_mask=lowerCAmelCase__ , use_cache=lowerCAmelCase__ , ) UpperCAmelCase = outputs.past_key_values # create hypothetical multiple next token and extent to next_input_ids UpperCAmelCase = ids_tensor((self.batch_size, 3) , config.vocab_size ) UpperCAmelCase = ids_tensor((self.batch_size, 3) , vocab_size=2 ) # append to next input_ids and UpperCAmelCase = torch.cat([input_ids, next_tokens] , dim=-1 ) UpperCAmelCase = torch.cat([input_mask, next_mask] , dim=-1 ) UpperCAmelCase = model( lowerCAmelCase__ , attention_mask=lowerCAmelCase__ , encoder_hidden_states=lowerCAmelCase__ , encoder_attention_mask=lowerCAmelCase__ , output_hidden_states=lowerCAmelCase__ , )["hidden_states"][0] UpperCAmelCase = model( lowerCAmelCase__ , attention_mask=lowerCAmelCase__ , encoder_hidden_states=lowerCAmelCase__ , encoder_attention_mask=lowerCAmelCase__ , past_key_values=lowerCAmelCase__ , output_hidden_states=lowerCAmelCase__ , )["hidden_states"][0] # select random slice UpperCAmelCase = ids_tensor((1,) , output_from_past.shape[-1] ).item() UpperCAmelCase = output_from_no_past[:, -3:, random_slice_idx].detach() UpperCAmelCase = 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 _UpperCamelCase ( self : List[str] ) -> str: UpperCAmelCase = self.prepare_config_and_inputs() ( ( UpperCAmelCase ) , ( UpperCAmelCase ) , ( UpperCAmelCase ) , ( UpperCAmelCase ) , ( UpperCAmelCase ) , ( UpperCAmelCase ) , ( UpperCAmelCase ) , ) = config_and_inputs UpperCAmelCase = {"input_ids": input_ids, "attention_mask": input_mask} return config, inputs_dict @require_torch class __magic_name__ ( _snake_case , _snake_case , _snake_case , unittest.TestCase ): UpperCAmelCase = ( ( FalconModel, FalconForCausalLM, FalconForSequenceClassification, FalconForTokenClassification, FalconForQuestionAnswering, ) if is_torch_available() else () ) UpperCAmelCase = (FalconForCausalLM,) if is_torch_available() else () UpperCAmelCase = ( { """feature-extraction""": FalconModel, """text-classification""": FalconForSequenceClassification, """text-generation""": FalconForCausalLM, """question-answering""": FalconForQuestionAnswering, """token-classification""": FalconForTokenClassification, """zero-shot""": FalconForSequenceClassification, } if is_torch_available() else {} ) UpperCAmelCase = False UpperCAmelCase = False def _UpperCamelCase ( self : Any ) -> Dict: UpperCAmelCase = FalconModelTester(self ) UpperCAmelCase = ConfigTester(self , config_class=lowerCAmelCase__ , hidden_size=3_7 ) def _UpperCamelCase ( self : List[str] ) -> Any: self.config_tester.run_common_tests() def _UpperCamelCase ( self : List[str] ) -> int: UpperCAmelCase = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*lowerCAmelCase__ ) def _UpperCamelCase ( self : Any ) -> Union[str, Any]: UpperCAmelCase , *UpperCAmelCase = self.model_tester.prepare_config_and_inputs() for alibi in [True, False]: UpperCAmelCase = alibi self.model_tester.create_and_check_model(lowerCAmelCase__ , *lowerCAmelCase__ ) def _UpperCamelCase ( self : Dict ) -> Dict: UpperCAmelCase , UpperCAmelCase = self.model_tester.prepare_config_and_inputs_for_common() UpperCAmelCase = 3 UpperCAmelCase = input_dict["input_ids"] UpperCAmelCase = input_ids.ne(1 ).to(lowerCAmelCase__ ) UpperCAmelCase = ids_tensor([self.model_tester.batch_size] , self.model_tester.type_sequence_label_size ) UpperCAmelCase = FalconForSequenceClassification(lowerCAmelCase__ ) model.to(lowerCAmelCase__ ) model.eval() UpperCAmelCase = model(lowerCAmelCase__ , attention_mask=lowerCAmelCase__ , labels=lowerCAmelCase__ ) self.assertEqual(result.logits.shape , (self.model_tester.batch_size, self.model_tester.num_labels) ) def _UpperCamelCase ( self : Optional[Any] ) -> Any: UpperCAmelCase , UpperCAmelCase = self.model_tester.prepare_config_and_inputs_for_common() UpperCAmelCase = 3 UpperCAmelCase = "single_label_classification" UpperCAmelCase = input_dict["input_ids"] UpperCAmelCase = input_ids.ne(1 ).to(lowerCAmelCase__ ) UpperCAmelCase = ids_tensor([self.model_tester.batch_size] , self.model_tester.type_sequence_label_size ) UpperCAmelCase = FalconForSequenceClassification(lowerCAmelCase__ ) model.to(lowerCAmelCase__ ) model.eval() UpperCAmelCase = model(lowerCAmelCase__ , attention_mask=lowerCAmelCase__ , labels=lowerCAmelCase__ ) self.assertEqual(result.logits.shape , (self.model_tester.batch_size, self.model_tester.num_labels) ) def _UpperCamelCase ( self : List[str] ) -> Any: UpperCAmelCase , UpperCAmelCase = self.model_tester.prepare_config_and_inputs_for_common() UpperCAmelCase = input_dict["input_ids"] UpperCAmelCase = FalconForCausalLM(lowerCAmelCase__ ) model.to(lowerCAmelCase__ ) model.eval() UpperCAmelCase = model(lowerCAmelCase__ , use_cache=lowerCAmelCase__ ) UpperCAmelCase = input_ids.shape[0] UpperCAmelCase = model._convert_to_rw_cache(result.past_key_values ) UpperCAmelCase = model._convert_cache_to_standard_format(lowerCAmelCase__ , lowerCAmelCase__ ) for layer in range(len(lowerCAmelCase__ ) ): for tensor_idx in range(2 ): self.assertTrue(rw_cache[layer][tensor_idx].ndim == 3 ) self.assertTrue(result.past_key_values[layer][tensor_idx].ndim == 4 ) self.assertTrue( torch.all(result.past_key_values[layer][tensor_idx] == standard_cache[layer][tensor_idx] ) ) def _UpperCamelCase ( self : List[str] ) -> Dict: UpperCAmelCase , UpperCAmelCase = self.model_tester.prepare_config_and_inputs_for_common() UpperCAmelCase = 3 UpperCAmelCase = "multi_label_classification" UpperCAmelCase = input_dict["input_ids"] UpperCAmelCase = input_ids.ne(1 ).to(lowerCAmelCase__ ) UpperCAmelCase = ids_tensor( [self.model_tester.batch_size, config.num_labels] , self.model_tester.type_sequence_label_size ).to(torch.float ) UpperCAmelCase = FalconForSequenceClassification(lowerCAmelCase__ ) model.to(lowerCAmelCase__ ) model.eval() UpperCAmelCase = model(lowerCAmelCase__ , attention_mask=lowerCAmelCase__ , labels=lowerCAmelCase__ ) self.assertEqual(result.logits.shape , (self.model_tester.batch_size, self.model_tester.num_labels) ) def _UpperCamelCase ( self : Union[str, Any] ) -> Tuple: # Falcon can have different numbers of KV-heads than the number of query heads, so we need # to override this test to use the right head counts. for model_class in self.all_generative_model_classes: UpperCAmelCase , UpperCAmelCase = self.model_tester.prepare_config_and_inputs_for_common() # If it doesn't support cache, pass the test if not hasattr(lowerCAmelCase__ , "use_cache" ): return UpperCAmelCase = model_class(lowerCAmelCase__ ).to(lowerCAmelCase__ ) if "use_cache" not in inputs: UpperCAmelCase = True UpperCAmelCase = model(**lowerCAmelCase__ ) # If "past_key_values" is not returned, pass the test (e.g. RWKV uses a different cache name and format) if "past_key_values" not in outputs: return UpperCAmelCase = ( getattr(lowerCAmelCase__ , "decoder_layers" , lowerCAmelCase__ ) or getattr(lowerCAmelCase__ , "num_decoder_layers" , lowerCAmelCase__ ) or config.num_hidden_layers ) UpperCAmelCase = getattr(lowerCAmelCase__ , "num_kv_heads" , config.num_attention_heads ) UpperCAmelCase = getattr(lowerCAmelCase__ , "d_model" , config.hidden_size ) UpperCAmelCase = embed_dim // num_attention_heads UpperCAmelCase = outputs["past_key_values"] self.assertEqual(len(lowerCAmelCase__ ) , lowerCAmelCase__ ) UpperCAmelCase , UpperCAmelCase = inputs["input_ids"].shape for i in range(lowerCAmelCase__ ): if config.new_decoder_architecture: UpperCAmelCase = config.num_attention_heads elif config.multi_query: UpperCAmelCase = 1 self.assertEqual(len(past_kv[0] ) , 2 ) # K V for the decoder = 2 self.assertEqual( past_kv[i][0].shape , (batch_size, num_attention_heads, seq_length, per_head_embed_dim) ) self.assertEqual( past_kv[i][1].shape , (batch_size, num_attention_heads, seq_length, per_head_embed_dim) ) @require_torch class __magic_name__ ( unittest.TestCase ): @slow def _UpperCamelCase ( self : Optional[Any] ) -> Optional[Any]: UpperCAmelCase = AutoTokenizer.from_pretrained("Rocketknight1/falcon-rw-1b" ) UpperCAmelCase = FalconForCausalLM.from_pretrained("Rocketknight1/falcon-rw-1b" ) model.eval() model.to(lowerCAmelCase__ ) UpperCAmelCase = tokenizer("My favorite food is" , return_tensors="pt" ).to(lowerCAmelCase__ ) UpperCAmelCase = ( "My favorite food is pizza. I love it so much that I have a pizza party every year for my birthday." ) UpperCAmelCase = model.generate(**lowerCAmelCase__ , do_sample=lowerCAmelCase__ , max_new_tokens=1_9 ) UpperCAmelCase = tokenizer.batch_decode(lowerCAmelCase__ )[0] self.assertEqual(lowerCAmelCase__ , lowerCAmelCase__ ) @slow def _UpperCamelCase ( self : Optional[int] ) -> Optional[int]: # The big models are way too big for the CI, so we use tiny random models that resemble their # architectures but with much smaller and fewer layers for repo in ["Rocketknight1/tiny-random-falcon-7b", "Rocketknight1/tiny-random-falcon-40b"]: UpperCAmelCase = AutoTokenizer.from_pretrained(lowerCAmelCase__ ) UpperCAmelCase = FalconForCausalLM.from_pretrained(lowerCAmelCase__ ) model.eval() model.to(lowerCAmelCase__ ) UpperCAmelCase = tokenizer("My favorite food is" , return_tensors="pt" ).to(lowerCAmelCase__ ) # We just test that these run without errors - the models are randomly initialized # and so the actual text outputs will be garbage model.generate(**lowerCAmelCase__ , do_sample=lowerCAmelCase__ , max_new_tokens=4 ) model.generate(**lowerCAmelCase__ , do_sample=lowerCAmelCase__ , max_new_tokens=4 ) model.generate(**lowerCAmelCase__ , num_beams=2 , max_new_tokens=4 ) @slow def _UpperCamelCase ( self : List[str] ) -> Tuple: # The big models are way too big for the CI, so we use tiny random models that resemble their # architectures but with much smaller and fewer layers with torch.no_grad(): for repo in [ "Rocketknight1/falcon-rw-1b", "Rocketknight1/tiny-random-falcon-7b", "Rocketknight1/tiny-random-falcon-40b", ]: UpperCAmelCase = AutoTokenizer.from_pretrained(lowerCAmelCase__ ) UpperCAmelCase = FalconForCausalLM.from_pretrained(lowerCAmelCase__ ) model.eval() model.to(device=lowerCAmelCase__ ) UpperCAmelCase = tokenizer("My favorite food is" , return_tensors="pt" ).to(lowerCAmelCase__ ) # Test results are the same with and without cache UpperCAmelCase = model.generate(**lowerCAmelCase__ , do_sample=lowerCAmelCase__ , max_new_tokens=2_0 , use_cache=lowerCAmelCase__ ) UpperCAmelCase = model.generate(**lowerCAmelCase__ , do_sample=lowerCAmelCase__ , max_new_tokens=2_0 , use_cache=lowerCAmelCase__ ) self.assertTrue((outputs_cache - outputs_no_cache).sum().item() == 0 )
702
import numpy as np from nltk.translate import meteor_score import datasets from datasets.config import importlib_metadata, version lowerCAmelCase__ = version.parse(importlib_metadata.version("nltk")) if NLTK_VERSION >= version.Version("3.6.4"): from nltk import word_tokenize lowerCAmelCase__ = "\\n@inproceedings{banarjee2005,\n title = {{METEOR}: An Automatic Metric for {MT} Evaluation with Improved Correlation with Human Judgments},\n author = {Banerjee, Satanjeev and Lavie, Alon},\n booktitle = {Proceedings of the {ACL} Workshop on Intrinsic and Extrinsic Evaluation Measures for Machine Translation and/or Summarization},\n month = jun,\n year = {2005},\n address = {Ann Arbor, Michigan},\n publisher = {Association for Computational Linguistics},\n url = {https://www.aclweb.org/anthology/W05-0909},\n pages = {65--72},\n}\n" lowerCAmelCase__ = "\\nMETEOR, an automatic metric for machine translation evaluation\nthat is based on a generalized concept of unigram matching between the\nmachine-produced translation and human-produced reference translations.\nUnigrams can be matched based on their surface forms, stemmed forms,\nand meanings; furthermore, METEOR can be easily extended to include more\nadvanced matching strategies. Once all generalized unigram matches\nbetween the two strings have been found, METEOR computes a score for\nthis matching using a combination of unigram-precision, unigram-recall, and\na measure of fragmentation that is designed to directly capture how\nwell-ordered the matched words in the machine translation are in relation\nto the reference.\n\nMETEOR gets an R correlation value of 0.347 with human evaluation on the Arabic\ndata and 0.331 on the Chinese data. This is shown to be an improvement on\nusing simply unigram-precision, unigram-recall and their harmonic F1\ncombination.\n" lowerCAmelCase__ = "\nComputes METEOR score of translated segments against one or more references.\nArgs:\n predictions: list of predictions to score. Each prediction\n should be a string with tokens separated by spaces.\n references: list of reference for each prediction. Each\n reference should be a string with tokens separated by spaces.\n alpha: Parameter for controlling relative weights of precision and recall. default: 0.9\n beta: Parameter for controlling shape of penalty as a function of fragmentation. default: 3\n gamma: Relative weight assigned to fragmentation penalty. default: 0.5\nReturns:\n 'meteor': meteor score.\nExamples:\n\n >>> meteor = datasets.load_metric('meteor')\n >>> predictions = [\"It is a guide to action which ensures that the military always obeys the commands of the party\"]\n >>> references = [\"It is a guide to action that ensures that the military will forever heed Party commands\"]\n >>> results = meteor.compute(predictions=predictions, references=references)\n >>> print(round(results[\"meteor\"], 4))\n 0.6944\n" @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class __magic_name__ ( datasets.Metric ): def _UpperCamelCase ( self : int ) -> str: return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { "predictions": datasets.Value("string" , id="sequence" ), "references": datasets.Value("string" , id="sequence" ), } ) , codebase_urls=["https://github.com/nltk/nltk/blob/develop/nltk/translate/meteor_score.py"] , reference_urls=[ "https://www.nltk.org/api/nltk.translate.html#module-nltk.translate.meteor_score", "https://en.wikipedia.org/wiki/METEOR", ] , ) def _UpperCamelCase ( self : Dict , lowerCAmelCase__ : List[Any] ) -> Dict: import nltk nltk.download("wordnet" ) if NLTK_VERSION >= version.Version("3.6.5" ): nltk.download("punkt" ) if NLTK_VERSION >= version.Version("3.6.6" ): nltk.download("omw-1.4" ) def _UpperCamelCase ( self : Union[str, Any] , lowerCAmelCase__ : int , lowerCAmelCase__ : Dict , lowerCAmelCase__ : Any=0.9 , lowerCAmelCase__ : Tuple=3 , lowerCAmelCase__ : Optional[int]=0.5 ) -> Any: if NLTK_VERSION >= version.Version("3.6.5" ): UpperCAmelCase = [ meteor_score.single_meteor_score( word_tokenize(lowerCAmelCase__ ) , word_tokenize(lowerCAmelCase__ ) , alpha=lowerCAmelCase__ , beta=lowerCAmelCase__ , gamma=lowerCAmelCase__ ) for ref, pred in zip(lowerCAmelCase__ , lowerCAmelCase__ ) ] else: UpperCAmelCase = [ meteor_score.single_meteor_score(lowerCAmelCase__ , lowerCAmelCase__ , alpha=lowerCAmelCase__ , beta=lowerCAmelCase__ , gamma=lowerCAmelCase__ ) for ref, pred in zip(lowerCAmelCase__ , lowerCAmelCase__ ) ] return {"meteor": np.mean(lowerCAmelCase__ )}
1
0
from __future__ import annotations import string from itertools import cycle, product from pathlib import Path lowerCAmelCase__ : List[str] = ( string.ascii_letters + string.digits + string.punctuation + string.whitespace ) lowerCAmelCase__ : Tuple = [ord(letter) for letter in string.ascii_lowercase] lowerCAmelCase__ : Any = {ord(char) for char in VALID_CHARS} lowerCAmelCase__ : Tuple = ["the", "be", "to", "of", "and", "in", "that", "have"] def _lowerCAmelCase( __A , __A ): UpperCAmelCase = "" UpperCAmelCase = 42 UpperCAmelCase = 42 UpperCAmelCase = 42 for keychar, cipherchar in zip(cycle(__A ) , __A ): UpperCAmelCase = cipherchar ^ keychar if decodedchar not in VALID_INTS: return None decoded += chr(__A ) return decoded def _lowerCAmelCase( __A ): UpperCAmelCase = [] for key in product(__A , repeat=3 ): UpperCAmelCase = try_key(__A , __A ) if encoded is not None: possibles.append(__A ) return possibles def _lowerCAmelCase( __A , __A ): return [possible for possible in possibles if common_word in possible.lower()] def _lowerCAmelCase( __A = "p059_cipher.txt" ): UpperCAmelCase = 42 UpperCAmelCase = 42 UpperCAmelCase = 42 UpperCAmelCase = 42 UpperCAmelCase = Path(__A ).parent.joinpath(__A ).read_text(encoding="utf-8" ) UpperCAmelCase = [int(__A ) for number in data.strip().split("," )] UpperCAmelCase = filter_valid_chars(__A ) for common_word in COMMON_WORDS: UpperCAmelCase = filter_common_word(__A , __A ) if len(__A ) == 1: break UpperCAmelCase = possibles[0] return sum(ord(__A ) for char in decoded_text ) if __name__ == "__main__": print(f"{solution() = }")
703
from ...configuration_utils import PretrainedConfig from ...utils import logging lowerCAmelCase__ = logging.get_logger(__name__) lowerCAmelCase__ = { "unc-nlp/lxmert-base-uncased": "https://huggingface.co/unc-nlp/lxmert-base-uncased/resolve/main/config.json", } class __magic_name__ ( _snake_case ): UpperCAmelCase = """lxmert""" UpperCAmelCase = {} def __init__( self : int , lowerCAmelCase__ : Any=3_0_5_2_2 , lowerCAmelCase__ : List[str]=7_6_8 , lowerCAmelCase__ : Union[str, Any]=1_2 , lowerCAmelCase__ : List[Any]=9_5_0_0 , lowerCAmelCase__ : Any=1_6_0_0 , lowerCAmelCase__ : Union[str, Any]=4_0_0 , lowerCAmelCase__ : Tuple=3_0_7_2 , lowerCAmelCase__ : Dict="gelu" , lowerCAmelCase__ : Tuple=0.1 , lowerCAmelCase__ : Tuple=0.1 , lowerCAmelCase__ : int=5_1_2 , lowerCAmelCase__ : List[str]=2 , lowerCAmelCase__ : List[str]=0.02 , lowerCAmelCase__ : str=1e-1_2 , lowerCAmelCase__ : str=9 , lowerCAmelCase__ : int=5 , lowerCAmelCase__ : Optional[int]=5 , lowerCAmelCase__ : List[Any]=2_0_4_8 , lowerCAmelCase__ : Any=4 , lowerCAmelCase__ : Dict=6.67 , lowerCAmelCase__ : Any=True , lowerCAmelCase__ : Union[str, Any]=True , lowerCAmelCase__ : Any=True , lowerCAmelCase__ : Tuple=True , lowerCAmelCase__ : Optional[Any]=True , lowerCAmelCase__ : Optional[int]=True , lowerCAmelCase__ : Tuple=True , **lowerCAmelCase__ : List[Any] , ) -> Dict: UpperCAmelCase = vocab_size UpperCAmelCase = hidden_size 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 = num_qa_labels UpperCAmelCase = num_object_labels UpperCAmelCase = num_attr_labels UpperCAmelCase = l_layers UpperCAmelCase = x_layers UpperCAmelCase = r_layers UpperCAmelCase = visual_feat_dim UpperCAmelCase = visual_pos_dim UpperCAmelCase = visual_loss_normalizer UpperCAmelCase = task_matched UpperCAmelCase = task_mask_lm UpperCAmelCase = task_obj_predict UpperCAmelCase = task_qa UpperCAmelCase = visual_obj_loss UpperCAmelCase = visual_attr_loss UpperCAmelCase = visual_feat_loss UpperCAmelCase = {"vision": r_layers, "cross_encoder": x_layers, "language": l_layers} super().__init__(**lowerCAmelCase__ )
1
0
import json import os import re import shutil import tempfile import unittest from typing import Tuple from transformers import AddedToken, BatchEncoding, ByTaTokenizer from transformers.utils import cached_property, is_tf_available, is_torch_available from ...test_tokenization_common import TokenizerTesterMixin if is_torch_available(): lowerCAmelCase__ = "pt" elif is_tf_available(): lowerCAmelCase__ = "tf" else: lowerCAmelCase__ = "jax" class __magic_name__ ( _snake_case , unittest.TestCase ): UpperCAmelCase = ByTaTokenizer UpperCAmelCase = False def _UpperCamelCase ( self : List[str] ) -> Any: super().setUp() UpperCAmelCase = ByTaTokenizer() tokenizer.save_pretrained(self.tmpdirname ) @cached_property def _UpperCamelCase ( self : Dict ) -> Optional[int]: return ByTaTokenizer.from_pretrained("google/byt5-small" ) def _UpperCamelCase ( self : Any , **lowerCAmelCase__ : Union[str, Any] ) -> ByTaTokenizer: return self.tokenizer_class.from_pretrained(self.tmpdirname , **lowerCAmelCase__ ) def _UpperCamelCase ( self : Tuple , lowerCAmelCase__ : Dict , lowerCAmelCase__ : Union[str, Any]=False , lowerCAmelCase__ : Optional[int]=2_0 , lowerCAmelCase__ : str=5 ) -> Tuple[str, list]: # XXX The default common tokenizer tests assume that every ID is decodable on its own. # This assumption is invalid for ByT5 because single bytes might not be # valid utf-8 (byte 128 for instance). # Here we're overriding the smallest possible method to provide # a clean sequence without making the same assumption. UpperCAmelCase = [] for i in range(len(lowerCAmelCase__ ) ): try: UpperCAmelCase = tokenizer.decode([i] , clean_up_tokenization_spaces=lowerCAmelCase__ ) except UnicodeDecodeError: pass toks.append((i, tok) ) UpperCAmelCase = list(filter(lambda lowerCAmelCase__ : re.match(r"^[ a-zA-Z]+$" , t[1] ) , lowerCAmelCase__ ) ) UpperCAmelCase = 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: UpperCAmelCase = toks[:max_length] if min_length is not None and len(lowerCAmelCase__ ) < min_length and len(lowerCAmelCase__ ) > 0: while len(lowerCAmelCase__ ) < min_length: UpperCAmelCase = toks + toks # toks_str = [t[1] for t in toks] UpperCAmelCase = [t[0] for t in toks] # Ensure consistency UpperCAmelCase = tokenizer.decode(lowerCAmelCase__ , clean_up_tokenization_spaces=lowerCAmelCase__ ) if " " not in output_txt and len(lowerCAmelCase__ ) > 1: UpperCAmelCase = ( tokenizer.decode([toks_ids[0]] , clean_up_tokenization_spaces=lowerCAmelCase__ ) + " " + tokenizer.decode(toks_ids[1:] , clean_up_tokenization_spaces=lowerCAmelCase__ ) ) if with_prefix_space: UpperCAmelCase = " " + output_txt UpperCAmelCase = tokenizer.encode(lowerCAmelCase__ , add_special_tokens=lowerCAmelCase__ ) return output_txt, output_ids def _UpperCamelCase ( self : List[Any] ) -> List[str]: UpperCAmelCase = self.ta_base_tokenizer UpperCAmelCase = tokenizer(["hi</s>", "I went to the gym</s>", "</s>"] ) UpperCAmelCase = tokenizer(["hi", "I went to the gym", ""] ) self.assertListEqual(batch_with_eos_added["input_ids"] , batch_without_eos_added["input_ids"] ) def _UpperCamelCase ( self : int ) -> List[Any]: UpperCAmelCase = self.ta_base_tokenizer UpperCAmelCase = "Unicode €." UpperCAmelCase = tokenizer(lowerCAmelCase__ ) UpperCAmelCase = [8_8, 1_1_3, 1_0_8, 1_0_2, 1_1_4, 1_0_3, 1_0_4, 3_5, 2_2_9, 1_3_3, 1_7_5, 4_9, 1] self.assertEqual(encoded["input_ids"] , lowerCAmelCase__ ) # decoding UpperCAmelCase = tokenizer.decode(lowerCAmelCase__ ) self.assertEqual(lowerCAmelCase__ , "Unicode €.</s>" ) UpperCAmelCase = tokenizer("e è é ê ë" ) UpperCAmelCase = [1_0_4, 3_5, 1_9_8, 1_7_1, 3_5, 1_9_8, 1_7_2, 3_5, 1_9_8, 1_7_3, 3_5, 1_9_8, 1_7_4, 1] self.assertEqual(encoded["input_ids"] , lowerCAmelCase__ ) # decoding UpperCAmelCase = tokenizer.decode(lowerCAmelCase__ ) self.assertEqual(lowerCAmelCase__ , "e è é ê ë</s>" ) # encode/decode, but with `encode` instead of `__call__` self.assertEqual(tokenizer.decode(tokenizer.encode("e è é ê ë" ) ) , "e è é ê ë</s>" ) def _UpperCamelCase ( self : Optional[Any] ) -> Optional[Any]: UpperCAmelCase = self.ta_base_tokenizer UpperCAmelCase = ["A long paragraph for summarization.", "Another paragraph for summarization."] # fmt: off UpperCAmelCase = [6_8, 3_5, 1_1_1, 1_1_4, 1_1_3, 1_0_6, 3_5, 1_1_5, 1_0_0, 1_1_7, 1_0_0, 1_0_6, 1_1_7, 1_0_0, 1_1_5, 1_0_7, 3_5, 1_0_5, 1_1_4, 1_1_7, 3_5, 1_1_8, 1_2_0, 1_1_2, 1_1_2, 1_0_0, 1_1_7, 1_0_8, 1_2_5, 1_0_0, 1_1_9, 1_0_8, 1_1_4, 1_1_3, 4_9, 1, 0] # fmt: on UpperCAmelCase = tokenizer(lowerCAmelCase__ , padding=lowerCAmelCase__ , return_tensors=lowerCAmelCase__ ) self.assertIsInstance(lowerCAmelCase__ , lowerCAmelCase__ ) if FRAMEWORK != "jax": UpperCAmelCase = list(batch.input_ids.numpy()[0] ) else: UpperCAmelCase = list(batch.input_ids.tolist()[0] ) self.assertListEqual(lowerCAmelCase__ , lowerCAmelCase__ ) self.assertEqual((2, 3_7) , batch.input_ids.shape ) self.assertEqual((2, 3_7) , batch.attention_mask.shape ) def _UpperCamelCase ( self : Optional[int] ) -> Optional[int]: UpperCAmelCase = self.ta_base_tokenizer UpperCAmelCase = ["A long paragraph for summarization.", "Another paragraph for summarization."] UpperCAmelCase = 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 _UpperCamelCase ( self : Tuple ) -> List[Any]: UpperCAmelCase = self.ta_base_tokenizer UpperCAmelCase = [ "Summary of the text.", "Another summary.", ] UpperCAmelCase = tokenizer( text_target=lowerCAmelCase__ , max_length=3_2 , padding="max_length" , truncation=lowerCAmelCase__ , return_tensors=lowerCAmelCase__ ) self.assertEqual(3_2 , targets["input_ids"].shape[1] ) def _UpperCamelCase ( self : Optional[Any] ) -> List[Any]: UpperCAmelCase = self.ta_base_tokenizer UpperCAmelCase = ["A long paragraph for summarization. </s>"] UpperCAmelCase = ["Summary of the text. </s>"] # fmt: off UpperCAmelCase = [6_8, 3_5, 1_1_1, 1_1_4, 1_1_3, 1_0_6, 3_5, 1_1_5, 1_0_0, 1_1_7, 1_0_0, 1_0_6, 1_1_7, 1_0_0, 1_1_5, 1_0_7, 3_5, 1_0_5, 1_1_4, 1_1_7, 3_5, 1_1_8, 1_2_0, 1_1_2, 1_1_2, 1_0_0, 1_1_7, 1_0_8, 1_2_5, 1_0_0, 1_1_9, 1_0_8, 1_1_4, 1_1_3, 4_9, 3_5, 1] UpperCAmelCase = [8_6, 1_2_0, 1_1_2, 1_1_2, 1_0_0, 1_1_7, 1_2_4, 3_5, 1_1_4, 1_0_5, 3_5, 1_1_9, 1_0_7, 1_0_4, 3_5, 1_1_9, 1_0_4, 1_2_3, 1_1_9, 4_9, 3_5, 1] # fmt: on UpperCAmelCase = tokenizer(lowerCAmelCase__ , text_target=lowerCAmelCase__ ) self.assertEqual(lowerCAmelCase__ , batch["input_ids"][0] ) self.assertEqual(lowerCAmelCase__ , batch["labels"][0] ) def _UpperCamelCase ( self : List[str] ) -> int: # safety check on max_len default value so we are sure the test works UpperCAmelCase = self.get_tokenizers() for tokenizer in tokenizers: with self.subTest(f"{tokenizer.__class__.__name__}" ): self.assertNotEqual(tokenizer.model_max_length , 4_2 ) # Now let's start the test UpperCAmelCase = self.get_tokenizers() for tokenizer in tokenizers: with self.subTest(f"{tokenizer.__class__.__name__}" ): # Isolate this from the other tests because we save additional tokens/etc UpperCAmelCase = tempfile.mkdtemp() UpperCAmelCase = " He is very happy, UNwant\u00E9d,running" UpperCAmelCase = tokenizer.encode(lowerCAmelCase__ , add_special_tokens=lowerCAmelCase__ ) tokenizer.save_pretrained(lowerCAmelCase__ ) UpperCAmelCase = tokenizer.__class__.from_pretrained(lowerCAmelCase__ ) UpperCAmelCase = after_tokenizer.encode(lowerCAmelCase__ , add_special_tokens=lowerCAmelCase__ ) self.assertListEqual(lowerCAmelCase__ , lowerCAmelCase__ ) shutil.rmtree(lowerCAmelCase__ ) UpperCAmelCase = self.get_tokenizers(model_max_length=4_2 ) for tokenizer in tokenizers: with self.subTest(f"{tokenizer.__class__.__name__}" ): # Isolate this from the other tests because we save additional tokens/etc UpperCAmelCase = tempfile.mkdtemp() UpperCAmelCase = " He is very happy, UNwant\u00E9d,running" tokenizer.add_tokens(["bim", "bambam"] ) UpperCAmelCase = tokenizer.additional_special_tokens additional_special_tokens.append("new_additional_special_token" ) tokenizer.add_special_tokens({"additional_special_tokens": additional_special_tokens} ) UpperCAmelCase = tokenizer.encode(lowerCAmelCase__ , add_special_tokens=lowerCAmelCase__ ) tokenizer.save_pretrained(lowerCAmelCase__ ) UpperCAmelCase = tokenizer.__class__.from_pretrained(lowerCAmelCase__ ) UpperCAmelCase = 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 , 4_2 ) UpperCAmelCase = tokenizer.__class__.from_pretrained(lowerCAmelCase__ , model_max_length=4_3 ) self.assertEqual(tokenizer.model_max_length , 4_3 ) shutil.rmtree(lowerCAmelCase__ ) def _UpperCamelCase ( self : Any ) -> int: UpperCAmelCase = [] if self.test_slow_tokenizer: tokenizer_list.append((self.tokenizer_class, self.get_tokenizer()) ) if self.test_rust_tokenizer: tokenizer_list.append((self.rust_tokenizer_class, self.get_rust_tokenizer()) ) for tokenizer_class, tokenizer_utils in tokenizer_list: with tempfile.TemporaryDirectory() as tmp_dir: tokenizer_utils.save_pretrained(lowerCAmelCase__ ) with open(os.path.join(lowerCAmelCase__ , "special_tokens_map.json" ) , encoding="utf-8" ) as json_file: UpperCAmelCase = json.load(lowerCAmelCase__ ) with open(os.path.join(lowerCAmelCase__ , "tokenizer_config.json" ) , encoding="utf-8" ) as json_file: UpperCAmelCase = json.load(lowerCAmelCase__ ) UpperCAmelCase = [f"<extra_id_{i}>" for i in range(1_2_5 )] UpperCAmelCase = added_tokens_extra_ids + [ "an_additional_special_token" ] UpperCAmelCase = 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 UpperCAmelCase = tokenizer_class.from_pretrained( lowerCAmelCase__ , ) self.assertIn( "an_additional_special_token" , tokenizer_without_change_in_init.additional_special_tokens ) # self.assertIn("an_additional_special_token",tokenizer_without_change_in_init.get_vocab()) # ByT5Tokenization no vocab self.assertEqual( ["an_additional_special_token"] , tokenizer_without_change_in_init.convert_ids_to_tokens( tokenizer_without_change_in_init.convert_tokens_to_ids(["an_additional_special_token"] ) ) , ) # Now we test that we can change the value of additional_special_tokens in the from_pretrained UpperCAmelCase = added_tokens_extra_ids + [AddedToken("a_new_additional_special_token" , lstrip=lowerCAmelCase__ )] UpperCAmelCase = 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 _UpperCamelCase ( self : str ) -> Tuple: UpperCAmelCase = [] if self.test_slow_tokenizer: tokenizer_list.append((self.tokenizer_class, self.get_tokenizer()) ) if self.test_rust_tokenizer: tokenizer_list.append((self.rust_tokenizer_class, self.get_rust_tokenizer()) ) for tokenizer_class, tokenizer_utils in tokenizer_list: with tempfile.TemporaryDirectory() as tmp_dir: tokenizer_utils.save_pretrained(lowerCAmelCase__ ) UpperCAmelCase = tokenizer_class.from_pretrained(lowerCAmelCase__ ) self.assertTrue(tokenizer.decode([2_5_5] ) == "" ) def _UpperCamelCase ( self : Optional[Any] ) -> Dict: pass def _UpperCamelCase ( self : Optional[Any] ) -> Tuple: pass def _UpperCamelCase ( self : Dict ) -> List[str]: pass def _UpperCamelCase ( self : Optional[int] ) -> int: pass def _UpperCamelCase ( self : int ) -> int: # The default common tokenizer tests uses invalid tokens for ByT5 that can only accept one-character strings # and special added tokens as tokens UpperCAmelCase = self.get_tokenizers(fast=lowerCAmelCase__ , do_lower_case=lowerCAmelCase__ ) for tokenizer in tokenizers: with self.subTest(f"{tokenizer.__class__.__name__}" ): UpperCAmelCase = ["t", "h", "i", "s", " ", "i", "s", " ", "a", " ", "t", "e", "x", "t", "</s>"] UpperCAmelCase = tokenizer.convert_tokens_to_string(lowerCAmelCase__ ) self.assertIsInstance(lowerCAmelCase__ , lowerCAmelCase__ ) def _UpperCamelCase ( self : Optional[int] ) -> List[str]: UpperCAmelCase = self.get_tokenizers() for tokenizer in tokenizers: with self.subTest(f"{tokenizer.__class__.__name__}" ): UpperCAmelCase = [ "bos_token", "eos_token", "unk_token", "sep_token", "pad_token", "cls_token", "mask_token", ] UpperCAmelCase = 0 UpperCAmelCase = tokenizer.convert_ids_to_tokens( lowerCAmelCase__ , skip_special_tokens=lowerCAmelCase__ ) for attr in attributes_list: setattr(lowerCAmelCase__ , attr + "_id" , lowerCAmelCase__ ) self.assertEqual(getattr(lowerCAmelCase__ , lowerCAmelCase__ ) , lowerCAmelCase__ ) self.assertEqual(getattr(lowerCAmelCase__ , attr + "_id" ) , lowerCAmelCase__ ) setattr(lowerCAmelCase__ , attr + "_id" , lowerCAmelCase__ ) self.assertEqual(getattr(lowerCAmelCase__ , lowerCAmelCase__ ) , lowerCAmelCase__ ) self.assertEqual(getattr(lowerCAmelCase__ , attr + "_id" ) , lowerCAmelCase__ ) setattr(lowerCAmelCase__ , "additional_special_tokens_ids" , [] ) self.assertListEqual(getattr(lowerCAmelCase__ , "additional_special_tokens" ) , [] ) self.assertListEqual(getattr(lowerCAmelCase__ , "additional_special_tokens_ids" ) , [] ) setattr(lowerCAmelCase__ , "additional_special_tokens_ids" , [token_id_to_test_setters] ) self.assertListEqual(getattr(lowerCAmelCase__ , "additional_special_tokens" ) , [token_to_test_setters] ) self.assertListEqual(getattr(lowerCAmelCase__ , "additional_special_tokens_ids" ) , [token_id_to_test_setters] )
704
def _lowerCAmelCase( __A ): UpperCAmelCase = 0 while num > 0: digit_sum += num % 10 num //= 10 return digit_sum def _lowerCAmelCase( __A = 100 ): UpperCAmelCase = 1 UpperCAmelCase = 2 for i in range(2 , max_n + 1 ): UpperCAmelCase = pre_numerator UpperCAmelCase = 2 * i // 3 if i % 3 == 0 else 1 UpperCAmelCase = cur_numerator UpperCAmelCase = e_cont * pre_numerator + temp return sum_digits(__A ) if __name__ == "__main__": print(f"{solution() = }")
1
0
import os from dataclasses import dataclass, field from io import BytesIO from typing import TYPE_CHECKING, Any, ClassVar, Dict, Optional, Union import numpy as np import pyarrow as pa from .. import config from ..download.streaming_download_manager import xopen, xsplitext from ..table import array_cast from ..utils.py_utils import no_op_if_value_is_null, string_to_dict if TYPE_CHECKING: from .features import FeatureType lowerCAmelCase__, lowerCAmelCase__, lowerCAmelCase__ = False, False, False @dataclass class __magic_name__ : UpperCAmelCase = None UpperCAmelCase = True UpperCAmelCase = True UpperCAmelCase = None # Automatically constructed UpperCAmelCase = """dict""" UpperCAmelCase = pa.struct({"""bytes""": pa.binary(), """path""": pa.string()} ) UpperCAmelCase = field(default="""Audio""" , init=_snake_case , repr=_snake_case ) def __call__( self : List[str] ) -> List[Any]: return self.pa_type def _UpperCamelCase ( self : str , lowerCAmelCase__ : Union[str, bytes, dict] ) -> dict: try: import soundfile as sf # soundfile is a dependency of librosa, needed to decode audio files. except ImportError as err: raise ImportError("To support encoding audio data, please install 'soundfile'." ) from err if isinstance(lowerCAmelCase__ , lowerCAmelCase__ ): return {"bytes": None, "path": value} elif isinstance(lowerCAmelCase__ , lowerCAmelCase__ ): return {"bytes": value, "path": None} elif "array" in value: # convert the audio array to wav bytes UpperCAmelCase = BytesIO() sf.write(lowerCAmelCase__ , value["array"] , value["sampling_rate"] , format="wav" ) return {"bytes": buffer.getvalue(), "path": None} 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 if value["path"].endswith("pcm" ): # "PCM" only has raw audio bytes if value.get("sampling_rate" ) is None: # At least, If you want to convert "PCM-byte" to "WAV-byte", you have to know sampling rate raise KeyError("To use PCM files, please specify a 'sampling_rate' in Audio object" ) if value.get("bytes" ): # If we already had PCM-byte, we don`t have to make "read file, make bytes" (just use it!) UpperCAmelCase = np.frombuffer(value["bytes"] , dtype=np.intaa ).astype(np.floataa ) / 3_2_7_6_7 else: UpperCAmelCase = np.memmap(value["path"] , dtype="h" , mode="r" ).astype(np.floataa ) / 3_2_7_6_7 UpperCAmelCase = BytesIO(bytes() ) sf.write(lowerCAmelCase__ , lowerCAmelCase__ , value["sampling_rate"] , format="wav" ) return {"bytes": buffer.getvalue(), "path": None} else: return {"bytes": None, "path": value.get("path" )} elif value.get("bytes" ) is not None or value.get("path" ) is not None: # store the audio bytes, and path is used to infer the audio format using the file extension return {"bytes": value.get("bytes" ), "path": value.get("path" )} else: raise ValueError( f"An audio sample should have one of 'path' or 'bytes' but they are missing or None in {value}." ) def _UpperCamelCase ( self : Any , lowerCAmelCase__ : dict , lowerCAmelCase__ : Optional[Dict[str, Union[str, bool, None]]] = None ) -> dict: if not self.decode: raise RuntimeError("Decoding is disabled for this feature. Please use Audio(decode=True) instead." ) UpperCAmelCase , UpperCAmelCase = (value["path"], BytesIO(value["bytes"] )) if value["bytes"] is not None else (value["path"], None) if path is None and file is None: raise ValueError(f"An audio sample should have one of 'path' or 'bytes' but both are None in {value}." ) try: import librosa import soundfile as sf except ImportError as err: raise ImportError("To support decoding audio files, please install 'librosa' and 'soundfile'." ) from err UpperCAmelCase = xsplitext(lowerCAmelCase__ )[1][1:].lower() if path is not None else None if not config.IS_OPUS_SUPPORTED and audio_format == "opus": raise RuntimeError( "Decoding 'opus' files requires system library 'libsndfile'>=1.0.31, " "You can try to update `soundfile` python library: `pip install \"soundfile>=0.12.1\"`. " ) elif not config.IS_MP3_SUPPORTED and audio_format == "mp3": raise RuntimeError( "Decoding 'mp3' files requires system library 'libsndfile'>=1.1.0, " "You can try to update `soundfile` python library: `pip install \"soundfile>=0.12.1\"`. " ) if file is None: UpperCAmelCase = token_per_repo_id or {} UpperCAmelCase = path.split("::" )[-1] try: UpperCAmelCase = string_to_dict(lowerCAmelCase__ , config.HUB_DATASETS_URL )["repo_id"] UpperCAmelCase = token_per_repo_id[repo_id] except (ValueError, KeyError): UpperCAmelCase = None with xopen(lowerCAmelCase__ , "rb" , use_auth_token=lowerCAmelCase__ ) as f: UpperCAmelCase , UpperCAmelCase = sf.read(lowerCAmelCase__ ) else: UpperCAmelCase , UpperCAmelCase = sf.read(lowerCAmelCase__ ) UpperCAmelCase = array.T if self.mono: UpperCAmelCase = librosa.to_mono(lowerCAmelCase__ ) if self.sampling_rate and self.sampling_rate != sampling_rate: UpperCAmelCase = librosa.resample(lowerCAmelCase__ , orig_sr=lowerCAmelCase__ , target_sr=self.sampling_rate ) UpperCAmelCase = self.sampling_rate return {"path": path, "array": array, "sampling_rate": sampling_rate} def _UpperCamelCase ( self : Any ) -> Union["FeatureType", Dict[str, "FeatureType"]]: from .features import Value if self.decode: raise ValueError("Cannot flatten a decoded Audio feature." ) return { "bytes": Value("binary" ), "path": Value("string" ), } def _UpperCamelCase ( self : Dict , lowerCAmelCase__ : Union[pa.StringArray, pa.StructArray] ) -> pa.StructArray: if pa.types.is_string(storage.type ): UpperCAmelCase = pa.array([None] * len(lowerCAmelCase__ ) , type=pa.binary() ) UpperCAmelCase = pa.StructArray.from_arrays([bytes_array, storage] , ["bytes", "path"] , mask=storage.is_null() ) elif pa.types.is_binary(storage.type ): UpperCAmelCase = pa.array([None] * len(lowerCAmelCase__ ) , type=pa.string() ) UpperCAmelCase = pa.StructArray.from_arrays([storage, path_array] , ["bytes", "path"] , mask=storage.is_null() ) elif pa.types.is_struct(storage.type ) and storage.type.get_all_field_indices("array" ): UpperCAmelCase = pa.array([Audio().encode_example(lowerCAmelCase__ ) if x is not None else None for x in storage.to_pylist()] ) elif pa.types.is_struct(storage.type ): if storage.type.get_field_index("bytes" ) >= 0: UpperCAmelCase = storage.field("bytes" ) else: UpperCAmelCase = pa.array([None] * len(lowerCAmelCase__ ) , type=pa.binary() ) if storage.type.get_field_index("path" ) >= 0: UpperCAmelCase = storage.field("path" ) else: UpperCAmelCase = pa.array([None] * len(lowerCAmelCase__ ) , type=pa.string() ) UpperCAmelCase = pa.StructArray.from_arrays([bytes_array, path_array] , ["bytes", "path"] , mask=storage.is_null() ) return array_cast(lowerCAmelCase__ , self.pa_type ) def _UpperCamelCase ( self : Dict , lowerCAmelCase__ : pa.StructArray ) -> pa.StructArray: @no_op_if_value_is_null def path_to_bytes(lowerCAmelCase__ : List[Any] ): with xopen(lowerCAmelCase__ , "rb" ) as f: UpperCAmelCase = f.read() return bytes_ UpperCAmelCase = 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() , ) UpperCAmelCase = pa.array( [os.path.basename(lowerCAmelCase__ ) if path is not None else None for path in storage.field("path" ).to_pylist()] , type=pa.string() , ) UpperCAmelCase = pa.StructArray.from_arrays([bytes_array, path_array] , ["bytes", "path"] , mask=bytes_array.is_null() ) return array_cast(lowerCAmelCase__ , self.pa_type )
705
from . import ( albert, align, altclip, audio_spectrogram_transformer, auto, autoformer, bark, bart, barthez, bartpho, beit, bert, bert_generation, bert_japanese, bertweet, big_bird, bigbird_pegasus, biogpt, bit, blenderbot, blenderbot_small, blip, blip_a, bloom, bridgetower, byta, camembert, canine, chinese_clip, clap, clip, clipseg, codegen, conditional_detr, convbert, convnext, convnextva, cpm, cpmant, ctrl, cvt, dataavec, deberta, deberta_va, decision_transformer, deformable_detr, deit, deprecated, deta, detr, dialogpt, dinat, distilbert, dit, donut, dpr, dpt, efficientformer, efficientnet, electra, encodec, encoder_decoder, ernie, ernie_m, esm, falcon, flaubert, flava, fnet, focalnet, fsmt, funnel, git, glpn, gpta, gpt_bigcode, gpt_neo, gpt_neox, gpt_neox_japanese, gpt_swa, gptj, gptsan_japanese, graphormer, groupvit, herbert, hubert, ibert, imagegpt, informer, instructblip, jukebox, layoutlm, layoutlmva, layoutlmva, layoutxlm, led, levit, lilt, llama, longformer, longta, luke, lxmert, mam_aaa, marian, markuplm, maskaformer, maskformer, mbart, mbartaa, mega, megatron_bert, megatron_gpta, mgp_str, mluke, mobilebert, mobilenet_va, mobilenet_va, mobilevit, mobilevitva, mpnet, mra, mta, musicgen, mvp, nat, nezha, nllb, nllb_moe, nystromformer, oneformer, open_llama, openai, opt, owlvit, pegasus, pegasus_x, perceiver, phobert, pixastruct, plbart, poolformer, prophetnet, qdqbert, rag, realm, reformer, regnet, rembert, resnet, roberta, roberta_prelayernorm, roc_bert, roformer, rwkv, sam, segformer, sew, sew_d, speech_encoder_decoder, speech_to_text, speech_to_text_a, speechta, splinter, squeezebert, swiftformer, swin, swinasr, swinva, switch_transformers, ta, table_transformer, tapas, time_series_transformer, timesformer, timm_backbone, transfo_xl, trocr, tvlt, umta, unispeech, unispeech_sat, upernet, videomae, vilt, vision_encoder_decoder, vision_text_dual_encoder, visual_bert, vit, vit_hybrid, vit_mae, vit_msn, vivit, wavaveca, wavaveca_conformer, wavaveca_phoneme, wavaveca_with_lm, wavlm, whisper, x_clip, xglm, xlm, xlm_prophetnet, xlm_roberta, xlm_roberta_xl, xlnet, xmod, yolos, yoso, )
1
0
import numpy as np from PIL import Image def _lowerCAmelCase( __A , __A , __A ): UpperCAmelCase = np.array(__A ) if arr.shape[0] != arr.shape[1]: raise ValueError("The input array is not a square matrix" ) UpperCAmelCase = 0 UpperCAmelCase = 0 UpperCAmelCase = 0 UpperCAmelCase = 0 # compute the shape of the output matrix UpperCAmelCase = (arr.shape[0] - size) // stride + 1 # initialize the output matrix with zeros of shape maxpool_shape UpperCAmelCase = np.zeros((maxpool_shape, maxpool_shape) ) while i < arr.shape[0]: if i + size > arr.shape[0]: # if the end of the matrix is reached, break break while j < arr.shape[1]: # if the end of the matrix is reached, break if j + size > arr.shape[1]: break # compute the maximum of the pooling matrix UpperCAmelCase = np.max(arr[i : i + size, j : j + size] ) # shift the pooling matrix by stride of column pixels j += stride mat_j += 1 # shift the pooling matrix by stride of row pixels i += stride mat_i += 1 # reset the column index to 0 UpperCAmelCase = 0 UpperCAmelCase = 0 return updated_arr def _lowerCAmelCase( __A , __A , __A ): UpperCAmelCase = np.array(__A ) if arr.shape[0] != arr.shape[1]: raise ValueError("The input array is not a square matrix" ) UpperCAmelCase = 0 UpperCAmelCase = 0 UpperCAmelCase = 0 UpperCAmelCase = 0 # compute the shape of the output matrix UpperCAmelCase = (arr.shape[0] - size) // stride + 1 # initialize the output matrix with zeros of shape avgpool_shape UpperCAmelCase = np.zeros((avgpool_shape, avgpool_shape) ) while i < arr.shape[0]: # if the end of the matrix is reached, break if i + size > arr.shape[0]: break while j < arr.shape[1]: # if the end of the matrix is reached, break if j + size > arr.shape[1]: break # compute the average of the pooling matrix UpperCAmelCase = int(np.average(arr[i : i + size, j : j + size] ) ) # shift the pooling matrix by stride of column pixels j += stride mat_j += 1 # shift the pooling matrix by stride of row pixels i += stride mat_i += 1 # reset the column index to 0 UpperCAmelCase = 0 UpperCAmelCase = 0 return updated_arr # Main Function if __name__ == "__main__": from doctest import testmod testmod(name="avgpooling", verbose=True) # Loading the image lowerCAmelCase__ = Image.open("path_to_image") # Converting the image to numpy array and maxpooling, displaying the result # Ensure that the image is a square matrix Image.fromarray(maxpooling(np.array(image), size=3, stride=2)).show() # Converting the image to numpy array and averagepooling, displaying the result # Ensure that the image is a square matrix Image.fromarray(avgpooling(np.array(image), size=3, stride=2)).show()
706
import numpy # List of input, output pairs lowerCAmelCase__ = ( ((5, 2, 3), 15), ((6, 5, 9), 25), ((11, 12, 13), 41), ((1, 1, 1), 8), ((11, 12, 13), 41), ) lowerCAmelCase__ = (((515, 22, 13), 555), ((61, 35, 49), 150)) lowerCAmelCase__ = [2, 4, 1, 5] lowerCAmelCase__ = len(train_data) lowerCAmelCase__ = 0.0_0_9 def _lowerCAmelCase( __A , __A="train" ): return calculate_hypothesis_value(__A , __A ) - output( __A , __A ) def _lowerCAmelCase( __A ): UpperCAmelCase = 0 for i in range(len(__A ) - 1 ): hyp_val += data_input_tuple[i] * parameter_vector[i + 1] hyp_val += parameter_vector[0] return hyp_val def _lowerCAmelCase( __A , __A ): if data_set == "train": return train_data[example_no][1] elif data_set == "test": return test_data[example_no][1] return None def _lowerCAmelCase( __A , __A ): if data_set == "train": return _hypothesis_value(train_data[example_no][0] ) elif data_set == "test": return _hypothesis_value(test_data[example_no][0] ) return None def _lowerCAmelCase( __A , __A=m ): UpperCAmelCase = 0 for i in range(__A ): if index == -1: summation_value += _error(__A ) else: summation_value += _error(__A ) * train_data[i][0][index] return summation_value def _lowerCAmelCase( __A ): UpperCAmelCase = summation_of_cost_derivative(__A , __A ) / m return cost_derivative_value def _lowerCAmelCase( ): global parameter_vector # Tune these values to set a tolerance value for predicted output UpperCAmelCase = 0.000002 UpperCAmelCase = 0 UpperCAmelCase = 0 while True: j += 1 UpperCAmelCase = [0, 0, 0, 0] for i in range(0 , len(__A ) ): UpperCAmelCase = get_cost_derivative(i - 1 ) UpperCAmelCase = ( parameter_vector[i] - LEARNING_RATE * cost_derivative ) if numpy.allclose( __A , __A , atol=__A , rtol=__A , ): break UpperCAmelCase = temp_parameter_vector print(("Number of iterations:", j) ) def _lowerCAmelCase( ): for i in range(len(__A ) ): print(("Actual output value:", output(__A , "test" )) ) print(("Hypothesis output:", calculate_hypothesis_value(__A , "test" )) ) if __name__ == "__main__": run_gradient_descent() print("\nTesting gradient descent for a linear hypothesis function.\n") test_gradient_descent()
1
0
import torch from torch import nn from ...configuration_utils import ConfigMixin, register_to_config from ...models import ModelMixin class __magic_name__ ( _snake_case , _snake_case ): @register_to_config def __init__( self : Optional[Any] , *, lowerCAmelCase__ : int = 4 , lowerCAmelCase__ : int = 7_6_8 , lowerCAmelCase__ : int , lowerCAmelCase__ : Optional[int] , ) -> Optional[int]: super().__init__() UpperCAmelCase = nn.Parameter(torch.zeros(lowerCAmelCase__ ) ) # parameters for additional clip time embeddings UpperCAmelCase = nn.Linear(lowerCAmelCase__ , lowerCAmelCase__ ) UpperCAmelCase = nn.Linear(lowerCAmelCase__ , lowerCAmelCase__ ) # parameters for encoder hidden states UpperCAmelCase = clip_extra_context_tokens UpperCAmelCase = nn.Linear( lowerCAmelCase__ , self.clip_extra_context_tokens * cross_attention_dim ) UpperCAmelCase = nn.Linear(lowerCAmelCase__ , lowerCAmelCase__ ) UpperCAmelCase = nn.LayerNorm(lowerCAmelCase__ ) def _UpperCamelCase ( self : str , *, lowerCAmelCase__ : Dict , lowerCAmelCase__ : Any , lowerCAmelCase__ : Dict , lowerCAmelCase__ : Optional[int] ) -> List[Any]: if do_classifier_free_guidance: # Add the classifier free guidance embeddings to the image embeddings UpperCAmelCase = image_embeddings.shape[0] UpperCAmelCase = self.learned_classifier_free_guidance_embeddings.unsqueeze(0 ) UpperCAmelCase = classifier_free_guidance_embeddings.expand( lowerCAmelCase__ , -1 ) UpperCAmelCase = torch.cat([classifier_free_guidance_embeddings, image_embeddings] , dim=0 ) # The image embeddings batch size and the text embeddings batch size are equal assert image_embeddings.shape[0] == prompt_embeds.shape[0] UpperCAmelCase = prompt_embeds.shape[0] # "Specifically, we modify the architecture described in Nichol et al. (2021) by projecting and # adding CLIP embeddings to the existing timestep embedding, ... UpperCAmelCase = self.embedding_proj(lowerCAmelCase__ ) UpperCAmelCase = self.clip_image_embeddings_project_to_time_embeddings(lowerCAmelCase__ ) UpperCAmelCase = time_projected_image_embeddings + time_projected_prompt_embeds # ... and by projecting CLIP embeddings into four # extra tokens of context that are concatenated to the sequence of outputs from the GLIDE text encoder" UpperCAmelCase = self.clip_extra_context_tokens_proj(lowerCAmelCase__ ) UpperCAmelCase = clip_extra_context_tokens.reshape(lowerCAmelCase__ , -1 , self.clip_extra_context_tokens ) UpperCAmelCase = clip_extra_context_tokens.permute(0 , 2 , 1 ) UpperCAmelCase = self.encoder_hidden_states_proj(lowerCAmelCase__ ) UpperCAmelCase = self.text_encoder_hidden_states_norm(lowerCAmelCase__ ) UpperCAmelCase = torch.cat([clip_extra_context_tokens, text_encoder_hidden_states] , dim=1 ) return text_encoder_hidden_states, additive_clip_time_embeddings
707
def _lowerCAmelCase( __A , __A , __A ): if n == 0: return 1 elif n % 2 == 1: return (binary_exponentiation(__A , n - 1 , __A ) * a) % mod else: UpperCAmelCase = binary_exponentiation(__A , n / 2 , __A ) return (b * b) % mod # a prime number lowerCAmelCase__ = 701 lowerCAmelCase__ = 1000000000 lowerCAmelCase__ = 10 # using binary exponentiation function, O(log(p)): print((a / b) % p == (a * binary_exponentiation(b, p - 2, p)) % p) print((a / b) % p == (a * b ** (p - 2)) % p)
1
0
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_tf_available, is_torch_available, ) lowerCAmelCase__ = {"configuration_unispeech": ["UNISPEECH_PRETRAINED_CONFIG_ARCHIVE_MAP", "UniSpeechConfig"]} try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase__ = [ "UNISPEECH_PRETRAINED_MODEL_ARCHIVE_LIST", "UniSpeechForCTC", "UniSpeechForPreTraining", "UniSpeechForSequenceClassification", "UniSpeechModel", "UniSpeechPreTrainedModel", ] if TYPE_CHECKING: from .configuration_unispeech import UNISPEECH_PRETRAINED_CONFIG_ARCHIVE_MAP, UniSpeechConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_unispeech import ( UNISPEECH_PRETRAINED_MODEL_ARCHIVE_LIST, UniSpeechForCTC, UniSpeechForPreTraining, UniSpeechForSequenceClassification, UniSpeechModel, UniSpeechPreTrainedModel, ) else: import sys lowerCAmelCase__ = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
708
lowerCAmelCase__ = { "a": "AAAAA", "b": "AAAAB", "c": "AAABA", "d": "AAABB", "e": "AABAA", "f": "AABAB", "g": "AABBA", "h": "AABBB", "i": "ABAAA", "j": "BBBAA", "k": "ABAAB", "l": "ABABA", "m": "ABABB", "n": "ABBAA", "o": "ABBAB", "p": "ABBBA", "q": "ABBBB", "r": "BAAAA", "s": "BAAAB", "t": "BAABA", "u": "BAABB", "v": "BBBAB", "w": "BABAA", "x": "BABAB", "y": "BABBA", "z": "BABBB", " ": " ", } lowerCAmelCase__ = {value: key for key, value in encode_dict.items()} def _lowerCAmelCase( __A ): UpperCAmelCase = "" for letter in word.lower(): if letter.isalpha() or letter == " ": encoded += encode_dict[letter] else: raise Exception("encode() accepts only letters of the alphabet and spaces" ) return encoded def _lowerCAmelCase( __A ): if set(__A ) - {"A", "B", " "} != set(): raise Exception("decode() accepts only 'A', 'B' and spaces" ) UpperCAmelCase = "" for word in coded.split(): while len(__A ) != 0: decoded += decode_dict[word[:5]] UpperCAmelCase = word[5:] decoded += " " return decoded.strip() if __name__ == "__main__": from doctest import testmod testmod()
1
0
from ...configuration_utils import PretrainedConfig from ...utils import logging lowerCAmelCase__ = logging.get_logger(__name__) lowerCAmelCase__ = { "google/fnet-base": "https://huggingface.co/google/fnet-base/resolve/main/config.json", "google/fnet-large": "https://huggingface.co/google/fnet-large/resolve/main/config.json" # See all FNet models at https://huggingface.co/models?filter=fnet } class __magic_name__ ( _snake_case ): UpperCAmelCase = """fnet""" def __init__( self : Union[str, Any] , lowerCAmelCase__ : List[str]=3_2_0_0_0 , lowerCAmelCase__ : Tuple=7_6_8 , lowerCAmelCase__ : int=1_2 , lowerCAmelCase__ : int=3_0_7_2 , lowerCAmelCase__ : Optional[Any]="gelu_new" , lowerCAmelCase__ : Dict=0.1 , lowerCAmelCase__ : Any=5_1_2 , lowerCAmelCase__ : List[str]=4 , lowerCAmelCase__ : Union[str, Any]=0.02 , lowerCAmelCase__ : List[Any]=1e-1_2 , lowerCAmelCase__ : Dict=False , lowerCAmelCase__ : Dict=5_1_2 , lowerCAmelCase__ : str=3 , lowerCAmelCase__ : Tuple=1 , lowerCAmelCase__ : List[Any]=2 , **lowerCAmelCase__ : Optional[Any] , ) -> str: super().__init__(pad_token_id=lowerCAmelCase__ , bos_token_id=lowerCAmelCase__ , eos_token_id=lowerCAmelCase__ , **lowerCAmelCase__ ) UpperCAmelCase = vocab_size UpperCAmelCase = max_position_embeddings UpperCAmelCase = hidden_size UpperCAmelCase = num_hidden_layers UpperCAmelCase = intermediate_size UpperCAmelCase = hidden_act UpperCAmelCase = hidden_dropout_prob UpperCAmelCase = initializer_range UpperCAmelCase = type_vocab_size UpperCAmelCase = layer_norm_eps UpperCAmelCase = use_tpu_fourier_optimizations UpperCAmelCase = tpu_short_seq_length
709
from __future__ import annotations import json import requests from bsa import BeautifulSoup from fake_useragent import UserAgent lowerCAmelCase__ = {"UserAgent": UserAgent().random} def _lowerCAmelCase( __A ): UpperCAmelCase = script.contents[0] UpperCAmelCase = json.loads(data[data.find("{\"config\"" ) : -1] ) return info["entry_data"]["ProfilePage"][0]["graphql"]["user"] class __magic_name__ : def __init__( self : Optional[Any] , lowerCAmelCase__ : Optional[int] ) -> Any: UpperCAmelCase = f"https://www.instagram.com/{username}/" UpperCAmelCase = self.get_json() def _UpperCamelCase ( self : List[str] ) -> dict: UpperCAmelCase = requests.get(self.url , headers=lowerCAmelCase__ ).text UpperCAmelCase = BeautifulSoup(lowerCAmelCase__ , "html.parser" ).find_all("script" ) try: return extract_user_profile(scripts[4] ) except (json.decoder.JSONDecodeError, KeyError): return extract_user_profile(scripts[3] ) def __repr__( self : Tuple ) -> str: return f"{self.__class__.__name__}('{self.username}')" def __str__( self : Optional[int] ) -> str: return f"{self.fullname} ({self.username}) is {self.biography}" @property def _UpperCamelCase ( self : Any ) -> str: return self.user_data["username"] @property def _UpperCamelCase ( self : List[Any] ) -> str: return self.user_data["full_name"] @property def _UpperCamelCase ( self : List[str] ) -> str: return self.user_data["biography"] @property def _UpperCamelCase ( self : Optional[int] ) -> str: return self.user_data["business_email"] @property def _UpperCamelCase ( self : str ) -> str: return self.user_data["external_url"] @property def _UpperCamelCase ( self : int ) -> int: return self.user_data["edge_followed_by"]["count"] @property def _UpperCamelCase ( self : List[Any] ) -> int: return self.user_data["edge_follow"]["count"] @property def _UpperCamelCase ( self : List[str] ) -> int: return self.user_data["edge_owner_to_timeline_media"]["count"] @property def _UpperCamelCase ( self : Tuple ) -> str: return self.user_data["profile_pic_url_hd"] @property def _UpperCamelCase ( self : Optional[int] ) -> bool: return self.user_data["is_verified"] @property def _UpperCamelCase ( self : Optional[Any] ) -> bool: return self.user_data["is_private"] def _lowerCAmelCase( __A = "github" ): import os if os.environ.get("CI" ): return # test failing on GitHub Actions UpperCAmelCase = InstagramUser(__A ) assert instagram_user.user_data assert isinstance(instagram_user.user_data , __A ) assert instagram_user.username == username if username != "github": return assert instagram_user.fullname == "GitHub" assert instagram_user.biography == "Built for developers." assert instagram_user.number_of_posts > 150 assert instagram_user.number_of_followers > 120000 assert instagram_user.number_of_followings > 15 assert instagram_user.email == "support@github.com" assert instagram_user.website == "https://github.com/readme" assert instagram_user.profile_picture_url.startswith("https://instagram." ) assert instagram_user.is_verified is True assert instagram_user.is_private is False if __name__ == "__main__": import doctest doctest.testmod() lowerCAmelCase__ = InstagramUser("github") print(instagram_user) print(f"{instagram_user.number_of_posts = }") print(f"{instagram_user.number_of_followers = }") print(f"{instagram_user.number_of_followings = }") print(f"{instagram_user.email = }") print(f"{instagram_user.website = }") print(f"{instagram_user.profile_picture_url = }") print(f"{instagram_user.is_verified = }") print(f"{instagram_user.is_private = }")
1
0
import argparse import json import numpy import torch from transformers.models.xlm.tokenization_xlm import VOCAB_FILES_NAMES from transformers.utils import CONFIG_NAME, WEIGHTS_NAME, logging logging.set_verbosity_info() def _lowerCAmelCase( __A , __A ): # Load checkpoint UpperCAmelCase = torch.load(__A , map_location="cpu" ) UpperCAmelCase = chkpt["model"] # We have the base model one level deeper than the original XLM repository UpperCAmelCase = {} for k, v in state_dict.items(): if "pred_layer" in k: UpperCAmelCase = v else: UpperCAmelCase = v UpperCAmelCase = chkpt["params"] UpperCAmelCase = {n: v for n, v in config.items() if not isinstance(__A , (torch.FloatTensor, numpy.ndarray) )} UpperCAmelCase = chkpt["dico_word2id"] UpperCAmelCase = {s + "</w>" if s.find("@@" ) == -1 and i > 13 else s.replace("@@" , "" ): i for s, i in vocab.items()} # Save pytorch-model UpperCAmelCase = pytorch_dump_folder_path + "/" + WEIGHTS_NAME UpperCAmelCase = pytorch_dump_folder_path + "/" + CONFIG_NAME UpperCAmelCase = pytorch_dump_folder_path + "/" + VOCAB_FILES_NAMES["vocab_file"] print(F"Save PyTorch model to {pytorch_weights_dump_path}" ) torch.save(__A , __A ) print(F"Save configuration file to {pytorch_config_dump_path}" ) with open(__A , "w" , encoding="utf-8" ) as f: f.write(json.dumps(__A , indent=2 ) + "\n" ) print(F"Save vocab file to {pytorch_config_dump_path}" ) with open(__A , "w" , encoding="utf-8" ) as f: f.write(json.dumps(__A , indent=2 ) + "\n" ) if __name__ == "__main__": lowerCAmelCase__ = argparse.ArgumentParser() # Required parameters parser.add_argument( "--xlm_checkpoint_path", default=None, type=str, required=True, help="Path the official PyTorch dump." ) parser.add_argument( "--pytorch_dump_folder_path", default=None, type=str, required=True, help="Path to the output PyTorch model." ) lowerCAmelCase__ = parser.parse_args() convert_xlm_checkpoint_to_pytorch(args.xlm_checkpoint_path, args.pytorch_dump_folder_path)
710
import unittest import numpy as np def _lowerCAmelCase( __A , __A , __A , __A = None , ): UpperCAmelCase = np.shape(__A ) UpperCAmelCase = np.shape(__A ) UpperCAmelCase = np.shape(__A ) if shape_a[0] != shape_b[0]: UpperCAmelCase = ( "Expected the same number of rows for A and B. " F"Instead found A of size {shape_a} and B of size {shape_b}" ) raise ValueError(__A ) if shape_b[1] != shape_c[1]: UpperCAmelCase = ( "Expected the same number of columns for B and C. " F"Instead found B of size {shape_b} and C of size {shape_c}" ) raise ValueError(__A ) UpperCAmelCase = pseudo_inv if a_inv is None: try: UpperCAmelCase = np.linalg.inv(__A ) except np.linalg.LinAlgError: raise ValueError( "Input matrix A is not invertible. Cannot compute Schur complement." ) return mat_c - mat_b.T @ a_inv @ mat_b class __magic_name__ ( unittest.TestCase ): def _UpperCamelCase ( self : List[str] ) -> None: UpperCAmelCase = np.array([[1, 2, 1], [2, 1, 2], [3, 2, 4]] ) UpperCAmelCase = np.array([[0, 3], [3, 0], [2, 3]] ) UpperCAmelCase = np.array([[2, 1], [6, 3]] ) UpperCAmelCase = schur_complement(lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ ) UpperCAmelCase = np.block([[a, b], [b.T, c]] ) UpperCAmelCase = np.linalg.det(lowerCAmelCase__ ) UpperCAmelCase = np.linalg.det(lowerCAmelCase__ ) UpperCAmelCase = np.linalg.det(lowerCAmelCase__ ) self.assertAlmostEqual(lowerCAmelCase__ , det_a * det_s ) def _UpperCamelCase ( self : str ) -> None: UpperCAmelCase = np.array([[1, 2, 1], [2, 1, 2], [3, 2, 4]] ) UpperCAmelCase = np.array([[0, 3], [3, 0], [2, 3]] ) UpperCAmelCase = np.array([[2, 1], [6, 3]] ) with self.assertRaises(lowerCAmelCase__ ): schur_complement(lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ ) def _UpperCamelCase ( self : Dict ) -> None: UpperCAmelCase = np.array([[1, 2, 1], [2, 1, 2], [3, 2, 4]] ) UpperCAmelCase = np.array([[0, 3], [3, 0], [2, 3]] ) UpperCAmelCase = np.array([[2, 1, 3], [6, 3, 5]] ) with self.assertRaises(lowerCAmelCase__ ): schur_complement(lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ ) if __name__ == "__main__": import doctest doctest.testmod() unittest.main()
1
0
from copy import deepcopy from typing import Optional, Union import numpy as np from ...processing_utils import ProcessorMixin from ...tokenization_utils_base import BatchEncoding from ...utils import TensorType, is_tf_available, is_torch_available if is_torch_available(): import torch if is_tf_available(): import tensorflow as tf class __magic_name__ ( _snake_case ): UpperCAmelCase = ["""image_processor"""] UpperCAmelCase = """SamImageProcessor""" def __init__( self : List[Any] , lowerCAmelCase__ : int ) -> Dict: super().__init__(lowerCAmelCase__ ) UpperCAmelCase = self.image_processor UpperCAmelCase = -1_0 UpperCAmelCase = self.image_processor.size["longest_edge"] def __call__( self : Dict , lowerCAmelCase__ : Optional[Any]=None , lowerCAmelCase__ : str=None , lowerCAmelCase__ : str=None , lowerCAmelCase__ : Union[str, Any]=None , lowerCAmelCase__ : Optional[Union[str, TensorType]] = None , **lowerCAmelCase__ : Optional[Any] , ) -> BatchEncoding: UpperCAmelCase = self.image_processor( lowerCAmelCase__ , return_tensors=lowerCAmelCase__ , **lowerCAmelCase__ , ) # pop arguments that are not used in the foward but used nevertheless UpperCAmelCase = encoding_image_processor["original_sizes"] if hasattr(lowerCAmelCase__ , "numpy" ): # Checks if Torch or TF tensor UpperCAmelCase = original_sizes.numpy() UpperCAmelCase , UpperCAmelCase , UpperCAmelCase = self._check_and_preprocess_points( input_points=lowerCAmelCase__ , input_labels=lowerCAmelCase__ , input_boxes=lowerCAmelCase__ , ) UpperCAmelCase = self._normalize_and_convert( lowerCAmelCase__ , lowerCAmelCase__ , input_points=lowerCAmelCase__ , input_labels=lowerCAmelCase__ , input_boxes=lowerCAmelCase__ , return_tensors=lowerCAmelCase__ , ) return encoding_image_processor def _UpperCamelCase ( self : int , lowerCAmelCase__ : Tuple , lowerCAmelCase__ : Optional[int] , lowerCAmelCase__ : Optional[Any]=None , lowerCAmelCase__ : Tuple=None , lowerCAmelCase__ : Optional[int]=None , lowerCAmelCase__ : Optional[Any]="pt" , ) -> Optional[int]: if input_points is not None: if len(lowerCAmelCase__ ) != len(lowerCAmelCase__ ): UpperCAmelCase = [ self._normalize_coordinates(self.target_size , lowerCAmelCase__ , original_sizes[0] ) for point in input_points ] else: UpperCAmelCase = [ self._normalize_coordinates(self.target_size , lowerCAmelCase__ , lowerCAmelCase__ ) for point, original_size in zip(lowerCAmelCase__ , lowerCAmelCase__ ) ] # check that all arrays have the same shape if not all(point.shape == input_points[0].shape for point in input_points ): if input_labels is not None: UpperCAmelCase , UpperCAmelCase = self._pad_points_and_labels(lowerCAmelCase__ , lowerCAmelCase__ ) UpperCAmelCase = np.array(lowerCAmelCase__ ) if input_labels is not None: UpperCAmelCase = np.array(lowerCAmelCase__ ) if input_boxes is not None: if len(lowerCAmelCase__ ) != len(lowerCAmelCase__ ): UpperCAmelCase = [ self._normalize_coordinates(self.target_size , lowerCAmelCase__ , original_sizes[0] , is_bounding_box=lowerCAmelCase__ ) for box in input_boxes ] else: UpperCAmelCase = [ self._normalize_coordinates(self.target_size , lowerCAmelCase__ , lowerCAmelCase__ , is_bounding_box=lowerCAmelCase__ ) for box, original_size in zip(lowerCAmelCase__ , lowerCAmelCase__ ) ] UpperCAmelCase = np.array(lowerCAmelCase__ ) if input_boxes is not None: if return_tensors == "pt": UpperCAmelCase = torch.from_numpy(lowerCAmelCase__ ) # boxes batch size of 1 by default UpperCAmelCase = input_boxes.unsqueeze(1 ) if len(input_boxes.shape ) != 3 else input_boxes elif return_tensors == "tf": UpperCAmelCase = tf.convert_to_tensor(lowerCAmelCase__ ) # boxes batch size of 1 by default UpperCAmelCase = tf.expand_dims(lowerCAmelCase__ , 1 ) if len(input_boxes.shape ) != 3 else input_boxes encoding_image_processor.update({"input_boxes": input_boxes} ) if input_points is not None: if return_tensors == "pt": UpperCAmelCase = torch.from_numpy(lowerCAmelCase__ ) # point batch size of 1 by default UpperCAmelCase = input_points.unsqueeze(1 ) if len(input_points.shape ) != 4 else input_points elif return_tensors == "tf": UpperCAmelCase = tf.convert_to_tensor(lowerCAmelCase__ ) # point batch size of 1 by default UpperCAmelCase = tf.expand_dims(lowerCAmelCase__ , 1 ) if len(input_points.shape ) != 4 else input_points encoding_image_processor.update({"input_points": input_points} ) if input_labels is not None: if return_tensors == "pt": UpperCAmelCase = torch.from_numpy(lowerCAmelCase__ ) # point batch size of 1 by default UpperCAmelCase = input_labels.unsqueeze(1 ) if len(input_labels.shape ) != 3 else input_labels elif return_tensors == "tf": UpperCAmelCase = tf.convert_to_tensor(lowerCAmelCase__ ) # point batch size of 1 by default UpperCAmelCase = tf.expand_dims(lowerCAmelCase__ , 1 ) if len(input_labels.shape ) != 3 else input_labels encoding_image_processor.update({"input_labels": input_labels} ) return encoding_image_processor def _UpperCamelCase ( self : List[str] , lowerCAmelCase__ : List[Any] , lowerCAmelCase__ : Any ) -> Any: UpperCAmelCase = max([point.shape[0] for point in input_points] ) UpperCAmelCase = [] for i, point in enumerate(lowerCAmelCase__ ): if point.shape[0] != expected_nb_points: UpperCAmelCase = np.concatenate( [point, np.zeros((expected_nb_points - point.shape[0], 2) ) + self.point_pad_value] , axis=0 ) UpperCAmelCase = np.append(input_labels[i] , [self.point_pad_value] ) processed_input_points.append(lowerCAmelCase__ ) UpperCAmelCase = processed_input_points return input_points, input_labels def _UpperCamelCase ( self : int , lowerCAmelCase__ : int , lowerCAmelCase__ : np.ndarray , lowerCAmelCase__ : Any , lowerCAmelCase__ : Any=False ) -> np.ndarray: UpperCAmelCase , UpperCAmelCase = original_size UpperCAmelCase , UpperCAmelCase = self.image_processor._get_preprocess_shape(lowerCAmelCase__ , longest_edge=lowerCAmelCase__ ) UpperCAmelCase = deepcopy(lowerCAmelCase__ ).astype(lowerCAmelCase__ ) if is_bounding_box: UpperCAmelCase = coords.reshape(-1 , 2 , 2 ) UpperCAmelCase = coords[..., 0] * (new_w / old_w) UpperCAmelCase = coords[..., 1] * (new_h / old_h) if is_bounding_box: UpperCAmelCase = coords.reshape(-1 , 4 ) return coords def _UpperCamelCase ( self : List[str] , lowerCAmelCase__ : int=None , lowerCAmelCase__ : Any=None , lowerCAmelCase__ : Optional[int]=None , ) -> int: if input_points is not None: if hasattr(lowerCAmelCase__ , "numpy" ): # Checks for TF or Torch tensor UpperCAmelCase = input_points.numpy().tolist() if not isinstance(lowerCAmelCase__ , lowerCAmelCase__ ) or not isinstance(input_points[0] , lowerCAmelCase__ ): raise ValueError("Input points must be a list of list of floating points." ) UpperCAmelCase = [np.array(lowerCAmelCase__ ) for input_point in input_points] else: UpperCAmelCase = None if input_labels is not None: if hasattr(lowerCAmelCase__ , "numpy" ): UpperCAmelCase = input_labels.numpy().tolist() if not isinstance(lowerCAmelCase__ , lowerCAmelCase__ ) or not isinstance(input_labels[0] , lowerCAmelCase__ ): raise ValueError("Input labels must be a list of list integers." ) UpperCAmelCase = [np.array(lowerCAmelCase__ ) for label in input_labels] else: UpperCAmelCase = None if input_boxes is not None: if hasattr(lowerCAmelCase__ , "numpy" ): UpperCAmelCase = input_boxes.numpy().tolist() if ( not isinstance(lowerCAmelCase__ , lowerCAmelCase__ ) or not isinstance(input_boxes[0] , lowerCAmelCase__ ) or not isinstance(input_boxes[0][0] , lowerCAmelCase__ ) ): raise ValueError("Input boxes must be a list of list of list of floating points." ) UpperCAmelCase = [np.array(lowerCAmelCase__ ).astype(np.floataa ) for box in input_boxes] else: UpperCAmelCase = None return input_points, input_labels, input_boxes @property def _UpperCamelCase ( self : Optional[Any] ) -> int: UpperCAmelCase = self.image_processor.model_input_names return list(dict.fromkeys(lowerCAmelCase__ ) ) def _UpperCamelCase ( self : List[str] , *lowerCAmelCase__ : Optional[int] , **lowerCAmelCase__ : Tuple ) -> List[Any]: return self.image_processor.post_process_masks(*lowerCAmelCase__ , **lowerCAmelCase__ )
711
import argparse import os import re import numpy as np import PIL import torch from timm import create_model from torch.optim.lr_scheduler import OneCycleLR from torch.utils.data import DataLoader, Dataset from torchvision.transforms import Compose, RandomResizedCrop, Resize, ToTensor from accelerate import Accelerator def _lowerCAmelCase( __A ): UpperCAmelCase = fname.split(os.path.sep )[-1] return re.search(r"^(.*)_\d+\.jpg$" , __A ).groups()[0] class __magic_name__ ( _snake_case ): def __init__( self : Any , lowerCAmelCase__ : int , lowerCAmelCase__ : Union[str, Any]=None , lowerCAmelCase__ : int=None ) -> Optional[Any]: UpperCAmelCase = file_names UpperCAmelCase = image_transform UpperCAmelCase = label_to_id def __len__( self : Tuple ) -> List[str]: return len(self.file_names ) def __getitem__( self : Optional[int] , lowerCAmelCase__ : Tuple ) -> Dict: UpperCAmelCase = self.file_names[idx] UpperCAmelCase = PIL.Image.open(lowerCAmelCase__ ) UpperCAmelCase = raw_image.convert("RGB" ) if self.image_transform is not None: UpperCAmelCase = self.image_transform(lowerCAmelCase__ ) UpperCAmelCase = extract_label(lowerCAmelCase__ ) if self.label_to_id is not None: UpperCAmelCase = self.label_to_id[label] return {"image": image, "label": label} def _lowerCAmelCase( __A , __A ): # Initialize accelerator if args.with_tracking: UpperCAmelCase = Accelerator( cpu=args.cpu , mixed_precision=args.mixed_precision , log_with="all" , project_dir=args.project_dir ) else: UpperCAmelCase = Accelerator(cpu=args.cpu , mixed_precision=args.mixed_precision ) # Sample hyper-parameters for learning rate, batch size, seed and a few other HPs UpperCAmelCase = config["lr"] UpperCAmelCase = int(config["num_epochs"] ) UpperCAmelCase = int(config["seed"] ) UpperCAmelCase = int(config["batch_size"] ) UpperCAmelCase = config["image_size"] if not isinstance(__A , (list, tuple) ): UpperCAmelCase = (image_size, image_size) # Parse out whether we are saving every epoch or after a certain number of batches if hasattr(args.checkpointing_steps , "isdigit" ): if args.checkpointing_steps == "epoch": UpperCAmelCase = args.checkpointing_steps elif args.checkpointing_steps.isdigit(): UpperCAmelCase = int(args.checkpointing_steps ) else: raise ValueError( F"Argument `checkpointing_steps` must be either a number or `epoch`. `{args.checkpointing_steps}` passed." ) else: UpperCAmelCase = None # We need to initialize the trackers we use, and also store our configuration if args.with_tracking: UpperCAmelCase = os.path.split(__A )[-1].split("." )[0] accelerator.init_trackers(__A , __A ) # Grab all the image filenames UpperCAmelCase = [os.path.join(args.data_dir , __A ) for fname in os.listdir(args.data_dir ) if fname.endswith(".jpg" )] # Build the label correspondences UpperCAmelCase = [extract_label(__A ) for fname in file_names] UpperCAmelCase = list(set(__A ) ) id_to_label.sort() UpperCAmelCase = {lbl: i for i, lbl in enumerate(__A )} # Set the seed before splitting the data. np.random.seed(__A ) torch.manual_seed(__A ) torch.cuda.manual_seed_all(__A ) # Split our filenames between train and validation UpperCAmelCase = np.random.permutation(len(__A ) ) UpperCAmelCase = int(0.8 * len(__A ) ) UpperCAmelCase = random_perm[:cut] UpperCAmelCase = random_perm[cut:] # For training we use a simple RandomResizedCrop UpperCAmelCase = Compose([RandomResizedCrop(__A , scale=(0.5, 1.0) ), ToTensor()] ) UpperCAmelCase = PetsDataset( [file_names[i] for i in train_split] , image_transform=__A , label_to_id=__A ) # For evaluation, we use a deterministic Resize UpperCAmelCase = Compose([Resize(__A ), ToTensor()] ) UpperCAmelCase = PetsDataset([file_names[i] for i in eval_split] , image_transform=__A , label_to_id=__A ) # Instantiate dataloaders. UpperCAmelCase = DataLoader(__A , shuffle=__A , batch_size=__A , num_workers=4 ) UpperCAmelCase = DataLoader(__A , shuffle=__A , batch_size=__A , num_workers=4 ) # Instantiate the model (we build the model here so that the seed also control new weights initialization) UpperCAmelCase = create_model("resnet50d" , pretrained=__A , num_classes=len(__A ) ) # We could avoid this line since the accelerator is set with `device_placement=True` (default value). # Note that if you are placing tensors on devices manually, this line absolutely needs to be before the optimizer # creation otherwise training will not work on TPU (`accelerate` will kindly throw an error to make us aware of that). UpperCAmelCase = model.to(accelerator.device ) # Freezing the base model for param in model.parameters(): UpperCAmelCase = False for param in model.get_classifier().parameters(): UpperCAmelCase = True # We normalize the batches of images to be a bit faster. UpperCAmelCase = torch.tensor(model.default_cfg["mean"] )[None, :, None, None].to(accelerator.device ) UpperCAmelCase = torch.tensor(model.default_cfg["std"] )[None, :, None, None].to(accelerator.device ) # Instantiate optimizer UpperCAmelCase = torch.optim.Adam(params=model.parameters() , lr=lr / 25 ) # Instantiate learning rate scheduler UpperCAmelCase = OneCycleLR(optimizer=__A , max_lr=__A , epochs=__A , steps_per_epoch=len(__A ) ) # Prepare everything # There is no specific order to remember, we just need to unpack the objects in the same order we gave them to the # prepare method. UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase = accelerator.prepare( __A , __A , __A , __A , __A ) # We need to keep track of how many total steps we have iterated over UpperCAmelCase = 0 # We also need to keep track of the starting epoch so files are named properly UpperCAmelCase = 0 # Potentially load in the weights and states from a previous save if args.resume_from_checkpoint: if args.resume_from_checkpoint is not None or args.resume_from_checkpoint != "": accelerator.print(F"Resumed from checkpoint: {args.resume_from_checkpoint}" ) accelerator.load_state(args.resume_from_checkpoint ) UpperCAmelCase = os.path.basename(args.resume_from_checkpoint ) else: # Get the most recent checkpoint UpperCAmelCase = [f.name for f in os.scandir(os.getcwd() ) if f.is_dir()] dirs.sort(key=os.path.getctime ) UpperCAmelCase = dirs[-1] # Sorts folders by date modified, most recent checkpoint is the last # Extract `epoch_{i}` or `step_{i}` UpperCAmelCase = os.path.splitext(__A )[0] if "epoch" in training_difference: UpperCAmelCase = int(training_difference.replace("epoch_" , "" ) ) + 1 UpperCAmelCase = None else: UpperCAmelCase = int(training_difference.replace("step_" , "" ) ) UpperCAmelCase = resume_step // len(__A ) resume_step -= starting_epoch * len(__A ) # Now we train the model for epoch in range(__A , __A ): model.train() if args.with_tracking: UpperCAmelCase = 0 if args.resume_from_checkpoint and epoch == starting_epoch and resume_step is not None: # We need to skip steps until we reach the resumed step UpperCAmelCase = accelerator.skip_first_batches(__A , __A ) overall_step += resume_step else: # After the first iteration though, we need to go back to the original dataloader UpperCAmelCase = train_dataloader for batch in active_dataloader: # We could avoid this line since we set the accelerator with `device_placement=True`. UpperCAmelCase = {k: v.to(accelerator.device ) for k, v in batch.items()} UpperCAmelCase = (batch["image"] - mean) / std UpperCAmelCase = model(__A ) UpperCAmelCase = torch.nn.functional.cross_entropy(__A , batch["label"] ) # We keep track of the loss at each epoch if args.with_tracking: total_loss += loss.detach().float() accelerator.backward(__A ) optimizer.step() lr_scheduler.step() optimizer.zero_grad() overall_step += 1 if isinstance(__A , __A ): UpperCAmelCase = F"step_{overall_step}" if overall_step % checkpointing_steps == 0: if args.output_dir is not None: UpperCAmelCase = os.path.join(args.output_dir , __A ) accelerator.save_state(__A ) model.eval() UpperCAmelCase = 0 UpperCAmelCase = 0 for step, batch in enumerate(__A ): # We could avoid this line since we set the accelerator with `device_placement=True`. UpperCAmelCase = {k: v.to(accelerator.device ) for k, v in batch.items()} UpperCAmelCase = (batch["image"] - mean) / std with torch.no_grad(): UpperCAmelCase = model(__A ) UpperCAmelCase = outputs.argmax(dim=-1 ) UpperCAmelCase , UpperCAmelCase = accelerator.gather_for_metrics((predictions, batch["label"]) ) UpperCAmelCase = predictions == references num_elems += accurate_preds.shape[0] accurate += accurate_preds.long().sum() UpperCAmelCase = accurate.item() / num_elems # Use accelerator.print to print only on the main process. accelerator.print(F"epoch {epoch}: {100 * eval_metric:.2f}" ) if args.with_tracking: accelerator.log( { "accuracy": 100 * eval_metric, "train_loss": total_loss.item() / len(__A ), "epoch": epoch, } , step=__A , ) if checkpointing_steps == "epoch": UpperCAmelCase = F"epoch_{epoch}" if args.output_dir is not None: UpperCAmelCase = os.path.join(args.output_dir , __A ) accelerator.save_state(__A ) if args.with_tracking: accelerator.end_training() def _lowerCAmelCase( ): UpperCAmelCase = argparse.ArgumentParser(description="Simple example of training script." ) parser.add_argument("--data_dir" , required=__A , help="The data folder on disk." ) parser.add_argument("--fp16" , action="store_true" , help="If passed, will use FP16 training." ) parser.add_argument( "--mixed_precision" , type=__A , default=__A , choices=["no", "fp16", "bf16", "fp8"] , help="Whether to use mixed precision. Choose" "between fp16 and bf16 (bfloat16). Bf16 requires PyTorch >= 1.10." "and an Nvidia Ampere GPU." , ) parser.add_argument("--cpu" , action="store_true" , help="If passed, will train on the CPU." ) parser.add_argument( "--checkpointing_steps" , type=__A , default=__A , help="Whether the various states should be saved at the end of every n steps, or 'epoch' for each epoch." , ) parser.add_argument( "--output_dir" , type=__A , default="." , help="Optional save directory where all checkpoint folders will be stored. Default is the current working directory." , ) parser.add_argument( "--resume_from_checkpoint" , type=__A , default=__A , help="If the training should continue from a checkpoint folder." , ) parser.add_argument( "--with_tracking" , action="store_true" , help="Whether to load in all available experiment trackers from the environment and use them for logging." , ) parser.add_argument( "--project_dir" , type=__A , default="logs" , help="Location on where to store experiment tracking logs` and relevent project information" , ) UpperCAmelCase = parser.parse_args() UpperCAmelCase = {"lr": 3E-2, "num_epochs": 3, "seed": 42, "batch_size": 64, "image_size": 224} training_function(__A , __A ) if __name__ == "__main__": main()
1
0
from typing import TYPE_CHECKING # rely on isort to merge the imports from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available lowerCAmelCase__ = { "configuration_autoformer": [ "AUTOFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP", "AutoformerConfig", ], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: lowerCAmelCase__ = [ "AUTOFORMER_PRETRAINED_MODEL_ARCHIVE_LIST", "AutoformerForPrediction", "AutoformerModel", "AutoformerPreTrainedModel", ] if TYPE_CHECKING: from .configuration_autoformer import ( AUTOFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP, AutoformerConfig, ) try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_autoformer import ( AUTOFORMER_PRETRAINED_MODEL_ARCHIVE_LIST, AutoformerForPrediction, AutoformerModel, AutoformerPreTrainedModel, ) else: import sys lowerCAmelCase__ = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
712
import glob import os import random from string import ascii_lowercase, digits import cva lowerCAmelCase__ = "" lowerCAmelCase__ = "" lowerCAmelCase__ = "" lowerCAmelCase__ = 1 # (0 is vertical, 1 is horizontal) def _lowerCAmelCase( ): UpperCAmelCase , UpperCAmelCase = get_dataset(__A , __A ) print("Processing..." ) UpperCAmelCase , UpperCAmelCase , UpperCAmelCase = update_image_and_anno(__A , __A , __A ) for index, image in enumerate(__A ): # Get random string code: '7b7ad245cdff75241935e4dd860f3bad' UpperCAmelCase = random_chars(32 ) UpperCAmelCase = paths[index].split(os.sep )[-1].rsplit("." , 1 )[0] UpperCAmelCase = F"{OUTPUT_DIR}/{file_name}_FLIP_{letter_code}" cva.imwrite(F"/{file_root}.jpg" , __A , [cva.IMWRITE_JPEG_QUALITY, 85] ) print(F"Success {index+1}/{len(__A )} with {file_name}" ) UpperCAmelCase = [] for anno in new_annos[index]: UpperCAmelCase = F"{anno[0]} {anno[1]} {anno[2]} {anno[3]} {anno[4]}" annos_list.append(__A ) with open(F"/{file_root}.txt" , "w" ) as outfile: outfile.write("\n".join(line for line in annos_list ) ) def _lowerCAmelCase( __A , __A ): UpperCAmelCase = [] UpperCAmelCase = [] for label_file in glob.glob(os.path.join(__A , "*.txt" ) ): UpperCAmelCase = label_file.split(os.sep )[-1].rsplit("." , 1 )[0] with open(__A ) as in_file: UpperCAmelCase = in_file.readlines() UpperCAmelCase = os.path.join(__A , F"{label_name}.jpg" ) UpperCAmelCase = [] for obj_list in obj_lists: UpperCAmelCase = obj_list.rstrip("\n" ).split(" " ) boxes.append( [ int(obj[0] ), float(obj[1] ), float(obj[2] ), float(obj[3] ), float(obj[4] ), ] ) if not boxes: continue img_paths.append(__A ) labels.append(__A ) return img_paths, labels def _lowerCAmelCase( __A , __A , __A = 1 ): UpperCAmelCase = [] UpperCAmelCase = [] UpperCAmelCase = [] for idx in range(len(__A ) ): UpperCAmelCase = [] UpperCAmelCase = img_list[idx] path_list.append(__A ) UpperCAmelCase = anno_list[idx] UpperCAmelCase = cva.imread(__A ) if flip_type == 1: UpperCAmelCase = cva.flip(__A , __A ) for bbox in img_annos: UpperCAmelCase = 1 - bbox[1] new_annos.append([bbox[0], x_center_new, bbox[2], bbox[3], bbox[4]] ) elif flip_type == 0: UpperCAmelCase = cva.flip(__A , __A ) for bbox in img_annos: UpperCAmelCase = 1 - bbox[2] new_annos.append([bbox[0], bbox[1], y_center_new, bbox[3], bbox[4]] ) new_annos_lists.append(__A ) new_imgs_list.append(__A ) return new_imgs_list, new_annos_lists, path_list def _lowerCAmelCase( __A = 32 ): assert number_char > 1, "The number of character should greater than 1" UpperCAmelCase = ascii_lowercase + digits return "".join(random.choice(__A ) for _ in range(__A ) ) if __name__ == "__main__": main() print("DONE ✅")
1
0
import os from shutil import copyfile from typing import List, Optional, Tuple from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import is_sentencepiece_available, logging if is_sentencepiece_available(): from .tokenization_pegasus import PegasusTokenizer else: lowerCAmelCase__ = None lowerCAmelCase__ = logging.get_logger(__name__) lowerCAmelCase__ = "▁" lowerCAmelCase__ = {"vocab_file": "spiece.model", "tokenizer_file": "tokenizer.json"} lowerCAmelCase__ = { "vocab_file": {"google/pegasus-xsum": "https://huggingface.co/google/pegasus-xsum/resolve/main/spiece.model"}, "tokenizer_file": { "google/pegasus-xsum": "https://huggingface.co/google/pegasus-xsum/resolve/main/tokenizer.json" }, } lowerCAmelCase__ = { "google/pegasus-xsum": 512, } class __magic_name__ ( _snake_case ): UpperCAmelCase = VOCAB_FILES_NAMES UpperCAmelCase = PRETRAINED_VOCAB_FILES_MAP UpperCAmelCase = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES UpperCAmelCase = PegasusTokenizer UpperCAmelCase = ["""input_ids""", """attention_mask"""] def __init__( self : List[str] , lowerCAmelCase__ : List[str]=None , lowerCAmelCase__ : Tuple=None , lowerCAmelCase__ : Dict="<pad>" , lowerCAmelCase__ : Union[str, Any]="</s>" , lowerCAmelCase__ : str="<unk>" , lowerCAmelCase__ : Dict="<mask_2>" , lowerCAmelCase__ : str="<mask_1>" , lowerCAmelCase__ : int=None , lowerCAmelCase__ : Optional[Any]=1_0_3 , **lowerCAmelCase__ : Any , ) -> int: UpperCAmelCase = offset if additional_special_tokens is not None: if not isinstance(lowerCAmelCase__ , lowerCAmelCase__ ): raise TypeError( f"additional_special_tokens should be of type {type(lowerCAmelCase__ )}, but is" f" {type(lowerCAmelCase__ )}" ) UpperCAmelCase = ( ([mask_token_sent] + additional_special_tokens) if mask_token_sent not in additional_special_tokens and mask_token_sent is not None else additional_special_tokens ) # fill additional tokens with ..., <unk_token_102> in case not all additional tokens are already taken additional_special_tokens_extended += [ f"<unk_{i}>" for i in range(len(lowerCAmelCase__ ) , self.offset - 1 ) ] if len(set(lowerCAmelCase__ ) ) != len(lowerCAmelCase__ ): raise ValueError( "Please make sure that the provided additional_special_tokens do not contain an incorrectly" f" shifted list of <unk_x> tokens. Found {additional_special_tokens_extended}." ) UpperCAmelCase = additional_special_tokens_extended else: UpperCAmelCase = [mask_token_sent] if mask_token_sent is not None else [] additional_special_tokens += [f"<unk_{i}>" for i in range(2 , self.offset )] super().__init__( lowerCAmelCase__ , tokenizer_file=lowerCAmelCase__ , pad_token=lowerCAmelCase__ , eos_token=lowerCAmelCase__ , unk_token=lowerCAmelCase__ , mask_token=lowerCAmelCase__ , mask_token_sent=lowerCAmelCase__ , offset=lowerCAmelCase__ , additional_special_tokens=lowerCAmelCase__ , **lowerCAmelCase__ , ) UpperCAmelCase = vocab_file UpperCAmelCase = False if not self.vocab_file else True def _UpperCamelCase ( self : List[str] , lowerCAmelCase__ : Any ) -> List[str]: UpperCAmelCase = set(self.all_special_ids ) # call it once instead of inside list comp all_special_ids.remove(self.unk_token_id ) # <unk> is only sometimes special if all_special_ids != set(range(len(self.additional_special_tokens ) + 3 ) ): raise ValueError( "There should be 3 special tokens: mask_token, pad_token, and eos_token +" f" {len(self.additional_special_tokens )} additional_special_tokens, but got {all_special_ids}" ) return [1 if x in all_special_ids else 0 for x in seq] def _UpperCamelCase ( self : Any , lowerCAmelCase__ : List , lowerCAmelCase__ : Optional[List] = None , lowerCAmelCase__ : bool = False ) -> List[int]: if already_has_special_tokens: return self._special_token_mask(lowerCAmelCase__ ) elif token_ids_a is None: return self._special_token_mask(lowerCAmelCase__ ) + [1] else: return self._special_token_mask(token_ids_a + token_ids_a ) + [1] def _UpperCamelCase ( self : Optional[Any] , lowerCAmelCase__ : str , lowerCAmelCase__ : Dict=None ) -> List[int]: if token_ids_a is None: return token_ids_a + [self.eos_token_id] # We don't expect to process pairs, but leave the pair logic for API consistency return token_ids_a + token_ids_a + [self.eos_token_id] def _UpperCamelCase ( self : int , lowerCAmelCase__ : str , lowerCAmelCase__ : Optional[str] = None ) -> Tuple[str]: if not self.can_save_slow_tokenizer: raise ValueError( "Your fast tokenizer does not have the necessary information to save the vocabulary for a slow " "tokenizer." ) if not os.path.isdir(lowerCAmelCase__ ): logger.error(f"Vocabulary path ({save_directory}) should be a directory" ) return UpperCAmelCase = 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,)
713
def _lowerCAmelCase( __A ): if not isinstance(__A , __A ): raise TypeError("only integers accepted as input" ) else: UpperCAmelCase = str(abs(__A ) ) UpperCAmelCase = [list(__A ) for char in range(len(__A ) )] for index in range(len(__A ) ): num_transpositions[index].pop(__A ) return max( int("".join(list(__A ) ) ) for transposition in num_transpositions ) if __name__ == "__main__": __import__("doctest").testmod()
1
0
from math import pi def _lowerCAmelCase( __A , __A ): return 2 * pi * radius * (angle / 360) if __name__ == "__main__": print(arc_length(90, 10))
714
import logging import re import pytorch_quantization import pytorch_quantization.nn as quant_nn import torch from pytorch_quantization import calib from pytorch_quantization.tensor_quant import QuantDescriptor lowerCAmelCase__ = logging.getLogger(__name__) lowerCAmelCase__ = 50 # max width of layer names lowerCAmelCase__ = 70 # max width of quantizer names def _lowerCAmelCase( __A ): UpperCAmelCase = parser.add_argument_group("quant_trainer arguments" ) group.add_argument("--wprec" , type=__A , default=8 , help="weight precision" ) group.add_argument("--aprec" , type=__A , default=8 , help="activation precision" ) group.add_argument("--quant-per-tensor" , action="store_true" , help="per tensor weight scaling" ) group.add_argument("--quant-disable" , action="store_true" , help="disable all quantizers" ) group.add_argument("--quant-disable-embeddings" , action="store_true" , help="disable all embeddings quantizers" ) group.add_argument("--quant-disable-keyword" , type=__A , nargs="+" , help="disable quantizers by keyword" ) group.add_argument("--quant-disable-layer-module" , type=__A , help="disable quantizers by keyword under layer." ) group.add_argument("--quant-enable-layer-module" , type=__A , help="enable quantizers by keyword under layer" ) group.add_argument("--calibrator" , default="max" , help="which quantization range calibrator to use" ) group.add_argument("--percentile" , default=__A , type=__A , help="percentile for PercentileCalibrator" ) group.add_argument("--fuse-qkv" , action="store_true" , help="use the same scale factor for qkv" ) group.add_argument("--clip-gelu" , metavar="N" , type=__A , help="clip gelu output maximum value to N" ) group.add_argument( "--recalibrate-weights" , action="store_true" , help=( "recalibrate weight amaxes by taking the max of the weights." " amaxes will be computed with the current quantization granularity (axis)." ) , ) def _lowerCAmelCase( __A ): if args.calibrator == "max": UpperCAmelCase = "max" elif args.calibrator == "percentile": if args.percentile is None: raise ValueError("Specify --percentile when using percentile calibrator" ) UpperCAmelCase = "histogram" elif args.calibrator == "mse": UpperCAmelCase = "histogram" else: raise ValueError(F"Invalid calibrator {args.calibrator}" ) UpperCAmelCase = QuantDescriptor(num_bits=args.aprec , calib_method=__A ) UpperCAmelCase = QuantDescriptor(num_bits=args.wprec , axis=(None if args.quant_per_tensor else (0,)) ) quant_nn.QuantLinear.set_default_quant_desc_input(__A ) quant_nn.QuantLinear.set_default_quant_desc_weight(__A ) def _lowerCAmelCase( __A , __A , __A=False , __A=False ): logger.info("Configuring Model for Quantization" ) logger.info(F"using quantization package {pytorch_quantization.__file__}" ) if not calib: if args.quant_disable_embeddings: set_quantizer_by_name(__A , ["embeddings"] , which="weight" , _disabled=__A ) if args.quant_disable: set_quantizer_by_name(__A , [""] , _disabled=__A ) if args.quant_disable_keyword: set_quantizer_by_name(__A , args.quant_disable_keyword , _disabled=__A ) if args.quant_disable_layer_module: set_quantizer_by_name(__A , [r"layer.\d+." + args.quant_disable_layer_module] , _disabled=__A ) if args.quant_enable_layer_module: set_quantizer_by_name(__A , [r"layer.\d+." + args.quant_enable_layer_module] , _disabled=__A ) if args.recalibrate_weights: recalibrate_weights(__A ) if args.fuse_qkv: fuse_qkv(__A , __A ) if args.clip_gelu: clip_gelu(__A , args.clip_gelu ) # if args.local_rank in [-1, 0] and not calib: print_quant_summary(__A ) def _lowerCAmelCase( __A ): logger.info("Enabling Calibration" ) for name, module in model.named_modules(): if name.endswith("_quantizer" ): if module._calibrator is not None: module.disable_quant() module.enable_calib() else: module.disable() logger.info(F"{name:80}: {module}" ) def _lowerCAmelCase( __A , __A ): logger.info("Loading calibrated amax" ) for name, module in model.named_modules(): if name.endswith("_quantizer" ): if module._calibrator is not None: if isinstance(module._calibrator , calib.MaxCalibrator ): module.load_calib_amax() else: module.load_calib_amax("percentile" , percentile=args.percentile ) module.enable_quant() module.disable_calib() else: module.enable() model.cuda() print_quant_summary(__A ) def _lowerCAmelCase( __A , __A ): def fusea(__A , __A , __A ): for mod in [qq, qk, qv]: if not hasattr(__A , "_amax" ): print(" WARNING: NO AMAX BUFFER" ) return UpperCAmelCase = qq._amax.detach().item() UpperCAmelCase = qk._amax.detach().item() UpperCAmelCase = qv._amax.detach().item() UpperCAmelCase = max(__A , __A , __A ) qq._amax.fill_(__A ) qk._amax.fill_(__A ) qv._amax.fill_(__A ) logger.info(F" q={q:5.2f} k={k:5.2f} v={v:5.2f} -> {amax:5.2f}" ) for name, mod in model.named_modules(): if name.endswith(".attention.self" ): logger.info(F"FUSE_QKV: {name:{name_width}}" ) fusea(mod.matmul_q_input_quantizer , mod.matmul_k_input_quantizer , mod.matmul_v_input_quantizer ) if args.quant_per_tensor: fusea(mod.query._weight_quantizer , mod.key._weight_quantizer , mod.value._weight_quantizer ) def _lowerCAmelCase( __A , __A ): for name, mod in model.named_modules(): if name.endswith(".output.dense" ) and not name.endswith("attention.output.dense" ): UpperCAmelCase = mod._input_quantizer._amax.data.detach().item() mod._input_quantizer._amax.data.detach().clamp_(max=__A ) UpperCAmelCase = mod._input_quantizer._amax.data.detach().item() logger.info(F"CLIP_GELU: {name:{name_width}} amax: {amax_init:5.2f} -> {amax:5.2f}" ) def _lowerCAmelCase( __A ): for name, mod in model.named_modules(): if hasattr(__A , "_weight_quantizer" ) and mod._weight_quantizer.axis is not None: UpperCAmelCase = mod.weight.shape[0] UpperCAmelCase = mod._weight_quantizer._amax.detach() UpperCAmelCase = torch.ones(__A , dtype=amax.dtype , device=amax.device ) * amax print(F"expanding {name} {amax} -> {mod._weight_quantizer._amax}" ) def _lowerCAmelCase( __A ): for name, mod in model.named_modules(): if hasattr(__A , "_weight_quantizer" ): if not hasattr(mod.weight_quantizer , "_amax" ): print("RECALIB: {name:{name_width}} WARNING: NO AMAX BUFFER" ) continue # determine which axes to reduce across # e.g. a 4D tensor quantized per axis 0 should reduce over (1,2,3) UpperCAmelCase = set() if mod._weight_quantizer.axis is None else set(mod._weight_quantizer.axis ) UpperCAmelCase = set(range(len(mod.weight.size() ) ) ) - axis_set UpperCAmelCase = pytorch_quantization.utils.reduce_amax(mod.weight , axis=__A , keepdims=__A ).detach() logger.info(F"RECALIB: {name:{name_width}} {mod._weight_quantizer._amax.flatten()} -> {amax.flatten()}" ) UpperCAmelCase = amax def _lowerCAmelCase( __A , __A=25 , __A=180 , __A=None ): if ignore is None: UpperCAmelCase = [] elif not isinstance(__A , __A ): UpperCAmelCase = [ignore] UpperCAmelCase = 0 for name, mod in model.named_modules(): if not hasattr(__A , "weight" ): continue UpperCAmelCase = max(__A , len(__A ) ) for name, mod in model.named_modules(): UpperCAmelCase = getattr(__A , "_input_quantizer" , __A ) UpperCAmelCase = getattr(__A , "_weight_quantizer" , __A ) if not hasattr(__A , "weight" ): continue if type(__A ) in ignore: continue if [True for s in ignore if type(__A ) is str and s in name]: continue UpperCAmelCase = F"Act:{input_q.extra_repr()}" UpperCAmelCase = F"Wgt:{weight_q.extra_repr()}" UpperCAmelCase = F"{name:{name_width}} {act_str} {wgt_str}" if len(__A ) <= line_width: logger.info(__A ) else: logger.info(F"{name:{name_width}} {act_str}" ) logger.info(F"{' ':{name_width}} {wgt_str}" ) def _lowerCAmelCase( __A ): UpperCAmelCase = 0 for name, mod in model.named_modules(): if isinstance(__A , pytorch_quantization.nn.TensorQuantizer ): print(F"{name:80} {mod}" ) count += 1 print(F"{count} TensorQuantizers found in model" ) def _lowerCAmelCase( __A , __A , __A , __A , __A ): UpperCAmelCase = getattr(__A , __A , __A ) if quantizer_mod is not None: assert hasattr(__A , __A ) setattr(__A , __A , __A ) else: logger.warning(F"{name} has no {quantizer}" ) def _lowerCAmelCase( __A , __A , __A="both" , **__A ): UpperCAmelCase = F"Warning: changing {which} quantizers of {name:{qname_width}}" for k, v in kwargs.items(): s += F" {k}={v}" if which in ["input", "both"]: set_quantizer(__A , __A , "_input_quantizer" , __A , __A ) if which in ["weight", "both"]: set_quantizer(__A , __A , "_weight_quantizer" , __A , __A ) logger.info(__A ) def _lowerCAmelCase( __A , __A , **__A ): for name, mod in model.named_modules(): if hasattr(__A , "_input_quantizer" ) or hasattr(__A , "_weight_quantizer" ): for n in names: if re.search(__A , __A ): set_quantizers(__A , __A , **__A ) elif name.endswith("_quantizer" ): for n in names: if re.search(__A , __A ): UpperCAmelCase = F"Warning: changing {name:{name_width}}" for k, v in kwargs.items(): s += F" {k}={v}" setattr(__A , __A , __A ) logger.info(__A )
1
0
import string def _lowerCAmelCase( __A ): for key in range(len(string.ascii_uppercase ) ): UpperCAmelCase = "" for symbol in message: if symbol in string.ascii_uppercase: UpperCAmelCase = string.ascii_uppercase.find(__A ) UpperCAmelCase = num - key if num < 0: UpperCAmelCase = num + len(string.ascii_uppercase ) UpperCAmelCase = translated + string.ascii_uppercase[num] else: UpperCAmelCase = translated + symbol print(F"Decryption using Key #{key}: {translated}" ) def _lowerCAmelCase( ): UpperCAmelCase = input("Encrypted message: " ) UpperCAmelCase = message.upper() decrypt(__A ) if __name__ == "__main__": import doctest doctest.testmod() main()
715
def _lowerCAmelCase( __A ): assert column_title.isupper() UpperCAmelCase = 0 UpperCAmelCase = len(__A ) - 1 UpperCAmelCase = 0 while index >= 0: UpperCAmelCase = (ord(column_title[index] ) - 64) * pow(26 , __A ) answer += value power += 1 index -= 1 return answer if __name__ == "__main__": from doctest import testmod testmod()
1
0
from __future__ import annotations import inspect import unittest import numpy as np from transformers import ResNetConfig from transformers.testing_utils import require_tf, require_vision, slow from transformers.utils import cached_property, is_tf_available, is_vision_available from ...test_configuration_common import ConfigTester from ...test_modeling_tf_common import TFModelTesterMixin, floats_tensor, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_tf_available(): import tensorflow as tf from transformers import TFResNetForImageClassification, TFResNetModel from transformers.models.resnet.modeling_tf_resnet import TF_RESNET_PRETRAINED_MODEL_ARCHIVE_LIST if is_vision_available(): from PIL import Image from transformers import AutoImageProcessor class __magic_name__ : def __init__( self : int , lowerCAmelCase__ : Any , lowerCAmelCase__ : Union[str, Any]=3 , lowerCAmelCase__ : Optional[int]=3_2 , lowerCAmelCase__ : Tuple=3 , lowerCAmelCase__ : str=1_0 , lowerCAmelCase__ : Optional[Any]=[1_0, 2_0, 3_0, 4_0] , lowerCAmelCase__ : List[str]=[1, 1, 2, 1] , lowerCAmelCase__ : List[Any]=True , lowerCAmelCase__ : int=True , lowerCAmelCase__ : Optional[int]="relu" , lowerCAmelCase__ : str=3 , lowerCAmelCase__ : Optional[int]=None , ) -> Optional[Any]: UpperCAmelCase = parent UpperCAmelCase = batch_size UpperCAmelCase = image_size UpperCAmelCase = num_channels UpperCAmelCase = embeddings_size UpperCAmelCase = hidden_sizes UpperCAmelCase = depths UpperCAmelCase = is_training UpperCAmelCase = use_labels UpperCAmelCase = hidden_act UpperCAmelCase = num_labels UpperCAmelCase = scope UpperCAmelCase = len(lowerCAmelCase__ ) def _UpperCamelCase ( self : Tuple ) -> List[str]: 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.num_labels ) UpperCAmelCase = self.get_config() return config, pixel_values, labels def _UpperCamelCase ( self : Any ) -> Dict: return ResNetConfig( num_channels=self.num_channels , embeddings_size=self.embeddings_size , hidden_sizes=self.hidden_sizes , depths=self.depths , hidden_act=self.hidden_act , num_labels=self.num_labels , image_size=self.image_size , ) def _UpperCamelCase ( self : Optional[Any] , lowerCAmelCase__ : Union[str, Any] , lowerCAmelCase__ : Union[str, Any] , lowerCAmelCase__ : List[str] ) -> Optional[int]: UpperCAmelCase = TFResNetModel(config=lowerCAmelCase__ ) UpperCAmelCase = model(lowerCAmelCase__ ) # expected last hidden states: B, C, H // 32, W // 32 self.parent.assertEqual( result.last_hidden_state.shape , (self.batch_size, self.hidden_sizes[-1], self.image_size // 3_2, self.image_size // 3_2) , ) def _UpperCamelCase ( self : Union[str, Any] , lowerCAmelCase__ : Tuple , lowerCAmelCase__ : str , lowerCAmelCase__ : int ) -> Optional[int]: UpperCAmelCase = self.num_labels UpperCAmelCase = TFResNetForImageClassification(lowerCAmelCase__ ) UpperCAmelCase = model(lowerCAmelCase__ , labels=lowerCAmelCase__ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_labels) ) def _UpperCamelCase ( self : str ) -> Any: UpperCAmelCase = self.prepare_config_and_inputs() UpperCAmelCase , UpperCAmelCase , UpperCAmelCase = config_and_inputs UpperCAmelCase = {"pixel_values": pixel_values} return config, inputs_dict @require_tf class __magic_name__ ( _snake_case , _snake_case , unittest.TestCase ): UpperCAmelCase = (TFResNetModel, TFResNetForImageClassification) if is_tf_available() else () UpperCAmelCase = ( {"""feature-extraction""": TFResNetModel, """image-classification""": TFResNetForImageClassification} if is_tf_available() else {} ) UpperCAmelCase = False UpperCAmelCase = False UpperCAmelCase = False UpperCAmelCase = False UpperCAmelCase = False def _UpperCamelCase ( self : Union[str, Any] ) -> Union[str, Any]: UpperCAmelCase = TFResNetModelTester(self ) UpperCAmelCase = ConfigTester(self , config_class=lowerCAmelCase__ , has_text_modality=lowerCAmelCase__ ) def _UpperCamelCase ( self : Dict ) -> Union[str, Any]: self.create_and_test_config_common_properties() self.config_tester.create_and_test_config_to_json_string() self.config_tester.create_and_test_config_to_json_file() self.config_tester.create_and_test_config_from_and_save_pretrained() self.config_tester.create_and_test_config_with_num_labels() self.config_tester.check_config_can_be_init_without_params() self.config_tester.check_config_arguments_init() def _UpperCamelCase ( self : Optional[Any] ) -> Optional[int]: return @unittest.skip(reason="ResNet does not use inputs_embeds" ) def _UpperCamelCase ( self : int ) -> Any: pass @unittest.skip(reason="ResNet does not support input and output embeddings" ) def _UpperCamelCase ( self : str ) -> Dict: pass def _UpperCamelCase ( self : Dict ) -> int: UpperCAmelCase , UpperCAmelCase = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: UpperCAmelCase = model_class(lowerCAmelCase__ ) 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] , lowerCAmelCase__ ) def _UpperCamelCase ( self : int ) -> str: UpperCAmelCase = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*lowerCAmelCase__ ) def _UpperCamelCase ( self : Optional[Any] ) -> List[Any]: def check_hidden_states_output(lowerCAmelCase__ : Dict , lowerCAmelCase__ : Optional[Any] , lowerCAmelCase__ : List[Any] ): UpperCAmelCase = model_class(lowerCAmelCase__ ) UpperCAmelCase = model(**self._prepare_for_class(lowerCAmelCase__ , lowerCAmelCase__ ) ) UpperCAmelCase = outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states UpperCAmelCase = self.model_tester.num_stages self.assertEqual(len(lowerCAmelCase__ ) , expected_num_stages + 1 ) # ResNet's feature maps are of shape (batch_size, num_channels, height, width) self.assertListEqual( list(hidden_states[0].shape[-2:] ) , [self.model_tester.image_size // 4, self.model_tester.image_size // 4] , ) UpperCAmelCase , UpperCAmelCase = self.model_tester.prepare_config_and_inputs_for_common() UpperCAmelCase = ["basic", "bottleneck"] for model_class in self.all_model_classes: for layer_type in layers_type: UpperCAmelCase = layer_type UpperCAmelCase = True check_hidden_states_output(lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ ) # check that output_hidden_states also work using config del inputs_dict["output_hidden_states"] UpperCAmelCase = True check_hidden_states_output(lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ ) def _UpperCamelCase ( self : Tuple ) -> Optional[Any]: UpperCAmelCase = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_image_classification(*lowerCAmelCase__ ) @slow def _UpperCamelCase ( self : Union[str, Any] ) -> int: for model_name in TF_RESNET_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: UpperCAmelCase = TFResNetModel.from_pretrained(lowerCAmelCase__ ) self.assertIsNotNone(lowerCAmelCase__ ) def _lowerCAmelCase( ): UpperCAmelCase = Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png" ) return image @require_tf @require_vision class __magic_name__ ( unittest.TestCase ): @cached_property def _UpperCamelCase ( self : Optional[Any] ) -> Any: return ( AutoImageProcessor.from_pretrained(TF_RESNET_PRETRAINED_MODEL_ARCHIVE_LIST[0] ) if is_vision_available() else None ) @slow def _UpperCamelCase ( self : List[str] ) -> Union[str, Any]: UpperCAmelCase = TFResNetForImageClassification.from_pretrained(TF_RESNET_PRETRAINED_MODEL_ARCHIVE_LIST[0] ) UpperCAmelCase = self.default_image_processor UpperCAmelCase = prepare_img() UpperCAmelCase = image_processor(images=lowerCAmelCase__ , return_tensors="tf" ) # forward pass UpperCAmelCase = model(**lowerCAmelCase__ ) # verify the logits UpperCAmelCase = tf.TensorShape((1, 1_0_0_0) ) self.assertEqual(outputs.logits.shape , lowerCAmelCase__ ) UpperCAmelCase = tf.constant([-11.1_069, -9.7_877, -8.3_777] ) self.assertTrue(np.allclose(outputs.logits[0, :3].numpy() , lowerCAmelCase__ , atol=1e-4 ) )
716
import sys import tempfile import unittest import unittest.mock as mock from pathlib import Path from huggingface_hub import HfFolder, delete_repo from requests.exceptions import HTTPError from transformers import AutoFeatureExtractor, WavaVecaFeatureExtractor from transformers.testing_utils import TOKEN, USER, get_tests_dir, is_staging_test sys.path.append(str(Path(__file__).parent.parent / "utils")) from test_module.custom_feature_extraction import CustomFeatureExtractor # noqa E402 lowerCAmelCase__ = get_tests_dir("fixtures") class __magic_name__ ( unittest.TestCase ): def _UpperCamelCase ( self : Tuple ) -> Union[str, Any]: # A mock response for an HTTP head request to emulate server down UpperCAmelCase = mock.Mock() UpperCAmelCase = 5_0_0 UpperCAmelCase = {} UpperCAmelCase = HTTPError UpperCAmelCase = {} # Download this model to make sure it's in the cache. UpperCAmelCase = WavaVecaFeatureExtractor.from_pretrained("hf-internal-testing/tiny-random-wav2vec2" ) # Under the mock environment we get a 500 error when trying to reach the model. with mock.patch("requests.Session.request" , return_value=lowerCAmelCase__ ) as mock_head: UpperCAmelCase = WavaVecaFeatureExtractor.from_pretrained("hf-internal-testing/tiny-random-wav2vec2" ) # This check we did call the fake head request mock_head.assert_called() def _UpperCamelCase ( self : List[Any] ) -> Dict: # This test is for deprecated behavior and can be removed in v5 UpperCAmelCase = WavaVecaFeatureExtractor.from_pretrained( "https://huggingface.co/hf-internal-testing/tiny-random-wav2vec2/resolve/main/preprocessor_config.json" ) @is_staging_test class __magic_name__ ( unittest.TestCase ): @classmethod def _UpperCamelCase ( cls : List[str] ) -> List[Any]: UpperCAmelCase = TOKEN HfFolder.save_token(lowerCAmelCase__ ) @classmethod def _UpperCamelCase ( cls : Optional[int] ) -> Union[str, Any]: try: delete_repo(token=cls._token , repo_id="test-feature-extractor" ) except HTTPError: pass try: delete_repo(token=cls._token , repo_id="valid_org/test-feature-extractor-org" ) except HTTPError: pass try: delete_repo(token=cls._token , repo_id="test-dynamic-feature-extractor" ) except HTTPError: pass def _UpperCamelCase ( self : Any ) -> Any: UpperCAmelCase = WavaVecaFeatureExtractor.from_pretrained(lowerCAmelCase__ ) feature_extractor.push_to_hub("test-feature-extractor" , use_auth_token=self._token ) UpperCAmelCase = WavaVecaFeatureExtractor.from_pretrained(f"{USER}/test-feature-extractor" ) for k, v in feature_extractor.__dict__.items(): self.assertEqual(lowerCAmelCase__ , getattr(lowerCAmelCase__ , lowerCAmelCase__ ) ) # Reset repo delete_repo(token=self._token , repo_id="test-feature-extractor" ) # Push to hub via save_pretrained with tempfile.TemporaryDirectory() as tmp_dir: feature_extractor.save_pretrained( lowerCAmelCase__ , repo_id="test-feature-extractor" , push_to_hub=lowerCAmelCase__ , use_auth_token=self._token ) UpperCAmelCase = WavaVecaFeatureExtractor.from_pretrained(f"{USER}/test-feature-extractor" ) for k, v in feature_extractor.__dict__.items(): self.assertEqual(lowerCAmelCase__ , getattr(lowerCAmelCase__ , lowerCAmelCase__ ) ) def _UpperCamelCase ( self : List[Any] ) -> Tuple: UpperCAmelCase = WavaVecaFeatureExtractor.from_pretrained(lowerCAmelCase__ ) feature_extractor.push_to_hub("valid_org/test-feature-extractor" , use_auth_token=self._token ) UpperCAmelCase = WavaVecaFeatureExtractor.from_pretrained("valid_org/test-feature-extractor" ) for k, v in feature_extractor.__dict__.items(): self.assertEqual(lowerCAmelCase__ , getattr(lowerCAmelCase__ , lowerCAmelCase__ ) ) # Reset repo delete_repo(token=self._token , repo_id="valid_org/test-feature-extractor" ) # Push to hub via save_pretrained with tempfile.TemporaryDirectory() as tmp_dir: feature_extractor.save_pretrained( lowerCAmelCase__ , repo_id="valid_org/test-feature-extractor-org" , push_to_hub=lowerCAmelCase__ , use_auth_token=self._token ) UpperCAmelCase = WavaVecaFeatureExtractor.from_pretrained("valid_org/test-feature-extractor-org" ) for k, v in feature_extractor.__dict__.items(): self.assertEqual(lowerCAmelCase__ , getattr(lowerCAmelCase__ , lowerCAmelCase__ ) ) def _UpperCamelCase ( self : Dict ) -> List[str]: CustomFeatureExtractor.register_for_auto_class() UpperCAmelCase = CustomFeatureExtractor.from_pretrained(lowerCAmelCase__ ) feature_extractor.push_to_hub("test-dynamic-feature-extractor" , use_auth_token=self._token ) # This has added the proper auto_map field to the config self.assertDictEqual( feature_extractor.auto_map , {"AutoFeatureExtractor": "custom_feature_extraction.CustomFeatureExtractor"} , ) UpperCAmelCase = AutoFeatureExtractor.from_pretrained( f"{USER}/test-dynamic-feature-extractor" , trust_remote_code=lowerCAmelCase__ ) # Can't make an isinstance check because the new_feature_extractor is from the CustomFeatureExtractor class of a dynamic module self.assertEqual(new_feature_extractor.__class__.__name__ , "CustomFeatureExtractor" )
1
0
import ast import os import re import shutil import tempfile import unittest from unittest import mock import torch from accelerate.test_utils.examples import compare_against_test from accelerate.test_utils.testing import TempDirTestCase, require_trackers, run_command, slow from accelerate.utils import write_basic_config # DataLoaders built from `test_samples/MRPC` for quick testing # Should mock `{script_name}.get_dataloaders` via: # @mock.patch("{script_name}.get_dataloaders", mocked_dataloaders) lowerCAmelCase__ = [ "cross_validation.py", "gradient_accumulation.py", "local_sgd.py", "multi_process_metrics.py", "memory.py", "automatic_gradient_accumulation.py", "fsdp_with_peak_mem_tracking.py", "deepspeed_with_config_support.py", "megatron_lm_gpt_pretraining.py", ] class __magic_name__ ( unittest.TestCase ): def _UpperCamelCase ( self : Union[str, Any] , lowerCAmelCase__ : str , lowerCAmelCase__ : bool , lowerCAmelCase__ : str = None , lowerCAmelCase__ : list = None ) -> List[Any]: UpperCAmelCase = None UpperCAmelCase = os.path.abspath(os.path.join("examples" , "by_feature" ) ) UpperCAmelCase = os.path.abspath("examples" ) for item in os.listdir(lowerCAmelCase__ ): if item not in EXCLUDE_EXAMPLES: UpperCAmelCase = os.path.join(lowerCAmelCase__ , lowerCAmelCase__ ) if os.path.isfile(lowerCAmelCase__ ) and ".py" in item_path: with self.subTest( tested_script=lowerCAmelCase__ , feature_script=lowerCAmelCase__ , tested_section="main()" if parser_only else "training_function()" , ): UpperCAmelCase = compare_against_test( os.path.join(lowerCAmelCase__ , lowerCAmelCase__ ) , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ ) UpperCAmelCase = "\n".join(lowerCAmelCase__ ) if special_strings is not None: for string in special_strings: UpperCAmelCase = diff.replace(lowerCAmelCase__ , "" ) self.assertEqual(lowerCAmelCase__ , "" ) def _UpperCamelCase ( self : str ) -> Dict: self.one_complete_example("complete_nlp_example.py" , lowerCAmelCase__ ) self.one_complete_example("complete_nlp_example.py" , lowerCAmelCase__ ) def _UpperCamelCase ( self : List[str] ) -> Optional[int]: UpperCAmelCase = os.path.abspath(os.path.join("examples" , "cv_example.py" ) ) UpperCAmelCase = [ " " * 1_6 + "{\n\n", " " * 2_0 + "\"accuracy\": eval_metric[\"accuracy\"],\n\n", " " * 2_0 + "\"f1\": eval_metric[\"f1\"],\n\n", " " * 2_0 + "\"train_loss\": total_loss.item() / len(train_dataloader),\n\n", " " * 2_0 + "\"epoch\": epoch,\n\n", " " * 1_6 + "},\n\n", " " * 1_6 + "step=epoch,\n", " " * 1_2, " " * 8 + "for step, batch in enumerate(active_dataloader):\n", ] self.one_complete_example("complete_cv_example.py" , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ ) self.one_complete_example("complete_cv_example.py" , lowerCAmelCase__ , lowerCAmelCase__ , lowerCAmelCase__ ) @mock.patch.dict(os.environ , {"""TESTING_MOCKED_DATALOADERS""": """1"""} ) class __magic_name__ ( _snake_case ): UpperCAmelCase = False @classmethod def _UpperCamelCase ( cls : int ) -> Optional[Any]: super().setUpClass() UpperCAmelCase = tempfile.mkdtemp() UpperCAmelCase = os.path.join(cls._tmpdir , "default_config.yml" ) write_basic_config(save_location=cls.configPath ) UpperCAmelCase = ["accelerate", "launch", "--config_file", cls.configPath] @classmethod def _UpperCamelCase ( cls : Optional[Any] ) -> str: super().tearDownClass() shutil.rmtree(cls._tmpdir ) def _UpperCamelCase ( self : List[Any] ) -> Optional[Any]: UpperCAmelCase = f"\n examples/by_feature/checkpointing.py\n --checkpointing_steps epoch\n --output_dir {self.tmpdir}\n ".split() run_command(self._launch_args + testargs ) self.assertTrue(os.path.exists(os.path.join(self.tmpdir , "epoch_0" ) ) ) def _UpperCamelCase ( self : str ) -> Optional[int]: UpperCAmelCase = f"\n examples/by_feature/checkpointing.py\n --checkpointing_steps 1\n --output_dir {self.tmpdir}\n ".split() UpperCAmelCase = run_command(self._launch_args + testargs ) self.assertTrue(os.path.exists(os.path.join(self.tmpdir , "step_2" ) ) ) def _UpperCamelCase ( self : str ) -> Optional[int]: UpperCAmelCase = f"\n examples/by_feature/checkpointing.py\n --resume_from_checkpoint {os.path.join(self.tmpdir , 'epoch_0' )}\n ".split() UpperCAmelCase = run_command(self._launch_args + testargs , return_stdout=lowerCAmelCase__ ) self.assertNotIn("epoch 0:" , lowerCAmelCase__ ) self.assertIn("epoch 1:" , lowerCAmelCase__ ) def _UpperCamelCase ( self : List[str] ) -> Optional[int]: UpperCAmelCase = f"\n examples/by_feature/checkpointing.py\n --resume_from_checkpoint {os.path.join(self.tmpdir , 'step_2' )}\n ".split() UpperCAmelCase = run_command(self._launch_args + testargs , return_stdout=lowerCAmelCase__ ) if torch.cuda.is_available(): UpperCAmelCase = torch.cuda.device_count() else: UpperCAmelCase = 1 if num_processes > 1: self.assertNotIn("epoch 0:" , lowerCAmelCase__ ) self.assertIn("epoch 1:" , lowerCAmelCase__ ) else: self.assertIn("epoch 0:" , lowerCAmelCase__ ) self.assertIn("epoch 1:" , lowerCAmelCase__ ) @slow def _UpperCamelCase ( self : str ) -> Dict: UpperCAmelCase = "\n examples/by_feature/cross_validation.py\n --num_folds 2\n ".split() with mock.patch.dict(os.environ , {"TESTING_MOCKED_DATALOADERS": "0"} ): UpperCAmelCase = run_command(self._launch_args + testargs , return_stdout=lowerCAmelCase__ ) UpperCAmelCase = re.findall("({.+})" , lowerCAmelCase__ ) UpperCAmelCase = [r for r in results if "accuracy" in r][-1] UpperCAmelCase = ast.literal_eval(lowerCAmelCase__ ) self.assertGreaterEqual(results["accuracy"] , 0.75 ) def _UpperCamelCase ( self : List[str] ) -> Any: UpperCAmelCase = ["examples/by_feature/multi_process_metrics.py"] run_command(self._launch_args + testargs ) @require_trackers @mock.patch.dict(os.environ , {"WANDB_MODE": "offline"} ) def _UpperCamelCase ( self : Tuple ) -> Union[str, Any]: with tempfile.TemporaryDirectory() as tmpdir: UpperCAmelCase = f"\n examples/by_feature/tracking.py\n --with_tracking\n --project_dir {tmpdir}\n ".split() run_command(self._launch_args + testargs ) self.assertTrue(os.path.exists(os.path.join(lowerCAmelCase__ , "tracking" ) ) ) def _UpperCamelCase ( self : Optional[Any] ) -> Tuple: UpperCAmelCase = ["examples/by_feature/gradient_accumulation.py"] run_command(self._launch_args + testargs ) def _UpperCamelCase ( self : List[Any] ) -> int: UpperCAmelCase = ["examples/by_feature/local_sgd.py"] run_command(self._launch_args + testargs )
717
import argparse import os import re # All paths are set with the intent you should run this script from the root of the repo with the command # python utils/check_dummies.py lowerCAmelCase__ = "src/diffusers" # Matches is_xxx_available() lowerCAmelCase__ = re.compile(r"is\_([a-z_]*)_available\(\)") # Matches from xxx import bla lowerCAmelCase__ = re.compile(r"\s+from\s+\S*\s+import\s+([^\(\s].*)\n") lowerCAmelCase__ = "\n{0} = None\n" lowerCAmelCase__ = "\nclass {0}(metaclass=DummyObject):\n _backends = {1}\n\n def __init__(self, *args, **kwargs):\n requires_backends(self, {1})\n\n @classmethod\n def from_config(cls, *args, **kwargs):\n requires_backends(cls, {1})\n\n @classmethod\n def from_pretrained(cls, *args, **kwargs):\n requires_backends(cls, {1})\n" lowerCAmelCase__ = "\ndef {0}(*args, **kwargs):\n requires_backends({0}, {1})\n" def _lowerCAmelCase( __A ): UpperCAmelCase = _re_backend.findall(__A ) if len(__A ) == 0: return None return "_and_".join(__A ) def _lowerCAmelCase( ): with open(os.path.join(__A , "__init__.py" ) , "r" , encoding="utf-8" , newline="\n" ) as f: UpperCAmelCase = f.readlines() # Get to the point we do the actual imports for type checking UpperCAmelCase = 0 UpperCAmelCase = {} # Go through the end of the file while line_index < len(__A ): # If the line contains is_backend_available, we grab all objects associated with the `else` block UpperCAmelCase = find_backend(lines[line_index] ) if backend is not None: while not lines[line_index].startswith("else:" ): line_index += 1 line_index += 1 UpperCAmelCase = [] # Until we unindent, add backend objects to the list while line_index < len(__A ) and len(lines[line_index] ) > 1: UpperCAmelCase = lines[line_index] UpperCAmelCase = _re_single_line_import.search(__A ) if single_line_import_search is not None: objects.extend(single_line_import_search.groups()[0].split(", " ) ) elif line.startswith(" " * 8 ): objects.append(line[8:-2] ) line_index += 1 if len(__A ) > 0: UpperCAmelCase = objects else: line_index += 1 return backend_specific_objects def _lowerCAmelCase( __A , __A ): if name.isupper(): return DUMMY_CONSTANT.format(__A ) elif name.islower(): return DUMMY_FUNCTION.format(__A , __A ) else: return DUMMY_CLASS.format(__A , __A ) def _lowerCAmelCase( __A=None ): if backend_specific_objects is None: UpperCAmelCase = read_init() # For special correspondence backend to module name as used in the function requires_modulename UpperCAmelCase = {} for backend, objects in backend_specific_objects.items(): UpperCAmelCase = "[" + ", ".join(F"\"{b}\"" for b in backend.split("_and_" ) ) + "]" UpperCAmelCase = "# This file is autogenerated by the command `make fix-copies`, do not edit.\n" dummy_file += "from ..utils import DummyObject, requires_backends\n\n" dummy_file += "\n".join([create_dummy_object(__A , __A ) for o in objects] ) UpperCAmelCase = dummy_file return dummy_files def _lowerCAmelCase( __A=False ): UpperCAmelCase = create_dummy_files() # For special correspondence backend to shortcut as used in utils/dummy_xxx_objects.py UpperCAmelCase = {"torch": "pt"} # Locate actual dummy modules and read their content. UpperCAmelCase = os.path.join(__A , "utils" ) UpperCAmelCase = { backend: os.path.join(__A , F"dummy_{short_names.get(__A , __A )}_objects.py" ) for backend in dummy_files.keys() } UpperCAmelCase = {} for backend, file_path in dummy_file_paths.items(): if os.path.isfile(__A ): with open(__A , "r" , encoding="utf-8" , newline="\n" ) as f: UpperCAmelCase = f.read() else: UpperCAmelCase = "" for backend in dummy_files.keys(): if dummy_files[backend] != actual_dummies[backend]: if overwrite: print( F"Updating diffusers.utils.dummy_{short_names.get(__A , __A )}_objects.py as the main " "__init__ has new objects." ) with open(dummy_file_paths[backend] , "w" , encoding="utf-8" , newline="\n" ) as f: f.write(dummy_files[backend] ) else: raise ValueError( "The main __init__ has objects that are not present in " F"diffusers.utils.dummy_{short_names.get(__A , __A )}_objects.py. Run `make fix-copies` " "to fix this." ) if __name__ == "__main__": lowerCAmelCase__ = argparse.ArgumentParser() parser.add_argument("--fix_and_overwrite", action="store_true", help="Whether to fix inconsistencies.") lowerCAmelCase__ = parser.parse_args() check_dummies(args.fix_and_overwrite)
1
0
import argparse import torch from transformers import OpenAIGPTConfig, OpenAIGPTModel, load_tf_weights_in_openai_gpt from transformers.utils import CONFIG_NAME, WEIGHTS_NAME, logging logging.set_verbosity_info() def _lowerCAmelCase( __A , __A , __A ): # Construct model if openai_config_file == "": UpperCAmelCase = OpenAIGPTConfig() else: UpperCAmelCase = OpenAIGPTConfig.from_json_file(__A ) UpperCAmelCase = OpenAIGPTModel(__A ) # Load weights from numpy load_tf_weights_in_openai_gpt(__A , __A , __A ) # Save pytorch-model UpperCAmelCase = pytorch_dump_folder_path + "/" + WEIGHTS_NAME UpperCAmelCase = pytorch_dump_folder_path + "/" + CONFIG_NAME print(F"Save PyTorch model to {pytorch_weights_dump_path}" ) torch.save(model.state_dict() , __A ) print(F"Save configuration file to {pytorch_config_dump_path}" ) with open(__A , "w" , encoding="utf-8" ) as f: f.write(config.to_json_string() ) if __name__ == "__main__": lowerCAmelCase__ = argparse.ArgumentParser() # Required parameters parser.add_argument( "--openai_checkpoint_folder_path", default=None, type=str, required=True, help="Path to the TensorFlow checkpoint path.", ) parser.add_argument( "--pytorch_dump_folder_path", default=None, type=str, required=True, help="Path to the output PyTorch model." ) parser.add_argument( "--openai_config_file", default="", type=str, help=( "An optional config json file corresponding to the pre-trained OpenAI model. \n" "This specifies the model architecture." ), ) lowerCAmelCase__ = parser.parse_args() convert_openai_checkpoint_to_pytorch( args.openai_checkpoint_folder_path, args.openai_config_file, args.pytorch_dump_folder_path )
718
from ...configuration_utils import PretrainedConfig from ...utils import logging from ...utils.backbone_utils import BackboneConfigMixin, get_aligned_output_features_output_indices lowerCAmelCase__ = logging.get_logger(__name__) lowerCAmelCase__ = { "facebook/convnextv2-tiny-1k-224": "https://huggingface.co/facebook/convnextv2-tiny-1k-224/resolve/main/config.json", } class __magic_name__ ( _snake_case , _snake_case ): UpperCAmelCase = """convnextv2""" def __init__( self : Optional[Any] , lowerCAmelCase__ : List[Any]=3 , lowerCAmelCase__ : str=4 , lowerCAmelCase__ : Dict=4 , lowerCAmelCase__ : Optional[Any]=None , lowerCAmelCase__ : Optional[Any]=None , lowerCAmelCase__ : str="gelu" , lowerCAmelCase__ : Optional[int]=0.02 , lowerCAmelCase__ : Dict=1e-1_2 , lowerCAmelCase__ : str=0.0 , lowerCAmelCase__ : str=2_2_4 , lowerCAmelCase__ : int=None , lowerCAmelCase__ : List[Any]=None , **lowerCAmelCase__ : List[Any] , ) -> List[Any]: super().__init__(**lowerCAmelCase__ ) UpperCAmelCase = num_channels UpperCAmelCase = patch_size UpperCAmelCase = num_stages UpperCAmelCase = [9_6, 1_9_2, 3_8_4, 7_6_8] if hidden_sizes is None else hidden_sizes UpperCAmelCase = [3, 3, 9, 3] if depths is None else depths UpperCAmelCase = hidden_act UpperCAmelCase = initializer_range UpperCAmelCase = layer_norm_eps UpperCAmelCase = drop_path_rate UpperCAmelCase = image_size UpperCAmelCase = ["stem"] + [f"stage{idx}" for idx in range(1 , len(self.depths ) + 1 )] UpperCAmelCase , UpperCAmelCase = get_aligned_output_features_output_indices( out_features=lowerCAmelCase__ , out_indices=lowerCAmelCase__ , stage_names=self.stage_names )
1
0
'''simple docstring''' import unittest from transformers import SqueezeBertConfig, is_torch_available from transformers.testing_utils import require_sentencepiece, require_tokenizers, require_torch, slow, torch_device from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, ids_tensor, random_attention_mask from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from transformers import ( SQUEEZEBERT_PRETRAINED_MODEL_ARCHIVE_LIST, SqueezeBertForMaskedLM, SqueezeBertForMultipleChoice, SqueezeBertForQuestionAnswering, SqueezeBertForSequenceClassification, SqueezeBertForTokenClassification, SqueezeBertModel, ) class __magic_name__ ( _snake_case ): def __init__( self : Optional[Any] , lowerCAmelCase__ : List[str] , lowerCAmelCase__ : Union[str, Any]=1_3 , lowerCAmelCase__ : List[Any]=7 , lowerCAmelCase__ : Dict=True , lowerCAmelCase__ : int=True , lowerCAmelCase__ : List[str]=False , lowerCAmelCase__ : Union[str, Any]=True , lowerCAmelCase__ : str=9_9 , lowerCAmelCase__ : int=3_2 , lowerCAmelCase__ : Optional[Any]=5 , lowerCAmelCase__ : Tuple=4 , lowerCAmelCase__ : Optional[int]=6_4 , lowerCAmelCase__ : int="gelu" , lowerCAmelCase__ : Optional[int]=0.1 , lowerCAmelCase__ : int=0.1 , lowerCAmelCase__ : Any=5_1_2 , lowerCAmelCase__ : Dict=1_6 , lowerCAmelCase__ : List[str]=2 , lowerCAmelCase__ : Tuple=0.02 , lowerCAmelCase__ : Any=3 , lowerCAmelCase__ : Optional[int]=4 , lowerCAmelCase__ : List[str]=None , lowerCAmelCase__ : Union[str, Any]=2 , lowerCAmelCase__ : Dict=2 , lowerCAmelCase__ : Dict=2 , lowerCAmelCase__ : int=2 , lowerCAmelCase__ : List[str]=4 , lowerCAmelCase__ : Any=1 , ) -> List[str]: UpperCAmelCase = parent UpperCAmelCase = batch_size UpperCAmelCase = seq_length UpperCAmelCase = is_training UpperCAmelCase = use_input_mask UpperCAmelCase = use_token_type_ids UpperCAmelCase = use_labels 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 = q_groups UpperCAmelCase = k_groups UpperCAmelCase = v_groups UpperCAmelCase = post_attention_groups UpperCAmelCase = intermediate_groups UpperCAmelCase = output_groups def _UpperCamelCase ( self : Optional[Any] ) -> str: 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 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() return config, input_ids, input_mask, sequence_labels, token_labels, choice_labels def _UpperCamelCase ( self : str ) -> Optional[int]: return SqueezeBertConfig( embedding_size=self.hidden_size , 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 , attention_probs_dropout_prob=self.hidden_dropout_prob , attention_dropout=self.attention_probs_dropout_prob , max_position_embeddings=self.max_position_embeddings , initializer_range=self.initializer_range , q_groups=self.q_groups , k_groups=self.k_groups , v_groups=self.v_groups , post_attention_groups=self.post_attention_groups , intermediate_groups=self.intermediate_groups , output_groups=self.output_groups , ) def _UpperCamelCase ( self : Union[str, Any] , lowerCAmelCase__ : List[str] , lowerCAmelCase__ : Any , lowerCAmelCase__ : Tuple , lowerCAmelCase__ : str , lowerCAmelCase__ : int , lowerCAmelCase__ : Any ) -> Optional[Any]: UpperCAmelCase = SqueezeBertModel(config=lowerCAmelCase__ ) model.to(lowerCAmelCase__ ) model.eval() UpperCAmelCase = model(lowerCAmelCase__ , lowerCAmelCase__ ) UpperCAmelCase = model(lowerCAmelCase__ ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def _UpperCamelCase ( self : Tuple , lowerCAmelCase__ : List[Any] , lowerCAmelCase__ : int , lowerCAmelCase__ : Dict , lowerCAmelCase__ : Optional[Any] , lowerCAmelCase__ : Optional[Any] , lowerCAmelCase__ : Dict ) -> Optional[int]: UpperCAmelCase = SqueezeBertForMaskedLM(config=lowerCAmelCase__ ) model.to(lowerCAmelCase__ ) model.eval() UpperCAmelCase = model(lowerCAmelCase__ , attention_mask=lowerCAmelCase__ , labels=lowerCAmelCase__ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.vocab_size) ) def _UpperCamelCase ( self : Optional[int] , lowerCAmelCase__ : Dict , lowerCAmelCase__ : str , lowerCAmelCase__ : Any , lowerCAmelCase__ : List[Any] , lowerCAmelCase__ : Union[str, Any] , lowerCAmelCase__ : Optional[Any] ) -> int: UpperCAmelCase = SqueezeBertForQuestionAnswering(config=lowerCAmelCase__ ) model.to(lowerCAmelCase__ ) model.eval() UpperCAmelCase = model( lowerCAmelCase__ , attention_mask=lowerCAmelCase__ , start_positions=lowerCAmelCase__ , end_positions=lowerCAmelCase__ ) self.parent.assertEqual(result.start_logits.shape , (self.batch_size, self.seq_length) ) self.parent.assertEqual(result.end_logits.shape , (self.batch_size, self.seq_length) ) def _UpperCamelCase ( self : Dict , lowerCAmelCase__ : str , lowerCAmelCase__ : List[Any] , lowerCAmelCase__ : int , lowerCAmelCase__ : str , lowerCAmelCase__ : str , lowerCAmelCase__ : Any ) -> List[Any]: UpperCAmelCase = self.num_labels UpperCAmelCase = SqueezeBertForSequenceClassification(lowerCAmelCase__ ) model.to(lowerCAmelCase__ ) model.eval() UpperCAmelCase = model(lowerCAmelCase__ , attention_mask=lowerCAmelCase__ , labels=lowerCAmelCase__ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_labels) ) def _UpperCamelCase ( self : str , lowerCAmelCase__ : Optional[int] , lowerCAmelCase__ : Optional[int] , lowerCAmelCase__ : List[Any] , lowerCAmelCase__ : int , lowerCAmelCase__ : int , lowerCAmelCase__ : Union[str, Any] ) -> Union[str, Any]: UpperCAmelCase = self.num_labels UpperCAmelCase = SqueezeBertForTokenClassification(config=lowerCAmelCase__ ) model.to(lowerCAmelCase__ ) model.eval() UpperCAmelCase = model(lowerCAmelCase__ , attention_mask=lowerCAmelCase__ , labels=lowerCAmelCase__ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.num_labels) ) def _UpperCamelCase ( self : Optional[int] , lowerCAmelCase__ : List[str] , lowerCAmelCase__ : Dict , lowerCAmelCase__ : Union[str, Any] , lowerCAmelCase__ : Any , lowerCAmelCase__ : Optional[Any] , lowerCAmelCase__ : Dict ) -> Any: UpperCAmelCase = self.num_choices UpperCAmelCase = SqueezeBertForMultipleChoice(config=lowerCAmelCase__ ) model.to(lowerCAmelCase__ ) model.eval() UpperCAmelCase = input_ids.unsqueeze(1 ).expand(-1 , self.num_choices , -1 ).contiguous() UpperCAmelCase = input_mask.unsqueeze(1 ).expand(-1 , self.num_choices , -1 ).contiguous() UpperCAmelCase = model( lowerCAmelCase__ , attention_mask=lowerCAmelCase__ , labels=lowerCAmelCase__ , ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_choices) ) def _UpperCamelCase ( self : int ) -> Dict: UpperCAmelCase = self.prepare_config_and_inputs() ((UpperCAmelCase) , (UpperCAmelCase) , (UpperCAmelCase) , (UpperCAmelCase) , (UpperCAmelCase) , (UpperCAmelCase)) = config_and_inputs UpperCAmelCase = {"input_ids": input_ids, "attention_mask": input_mask} return config, inputs_dict @require_torch class __magic_name__ ( _snake_case , _snake_case , unittest.TestCase ): UpperCAmelCase = ( ( SqueezeBertModel, SqueezeBertForMaskedLM, SqueezeBertForMultipleChoice, SqueezeBertForQuestionAnswering, SqueezeBertForSequenceClassification, SqueezeBertForTokenClassification, ) if is_torch_available() else None ) UpperCAmelCase = ( { """feature-extraction""": SqueezeBertModel, """fill-mask""": SqueezeBertForMaskedLM, """question-answering""": SqueezeBertForQuestionAnswering, """text-classification""": SqueezeBertForSequenceClassification, """token-classification""": SqueezeBertForTokenClassification, """zero-shot""": SqueezeBertForSequenceClassification, } if is_torch_available() else {} ) UpperCAmelCase = False UpperCAmelCase = True UpperCAmelCase = False def _UpperCamelCase ( self : List[Any] ) -> Optional[Any]: UpperCAmelCase = SqueezeBertModelTester(self ) UpperCAmelCase = ConfigTester(self , config_class=lowerCAmelCase__ , dim=3_7 ) def _UpperCamelCase ( self : str ) -> Any: self.config_tester.run_common_tests() def _UpperCamelCase ( self : Any ) -> Optional[int]: UpperCAmelCase = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_squeezebert_model(*lowerCAmelCase__ ) def _UpperCamelCase ( self : Union[str, Any] ) -> int: UpperCAmelCase = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_squeezebert_for_masked_lm(*lowerCAmelCase__ ) def _UpperCamelCase ( self : List[Any] ) -> Dict: UpperCAmelCase = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_squeezebert_for_question_answering(*lowerCAmelCase__ ) def _UpperCamelCase ( self : Optional[Any] ) -> List[str]: UpperCAmelCase = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_squeezebert_for_sequence_classification(*lowerCAmelCase__ ) def _UpperCamelCase ( self : List[str] ) -> str: UpperCAmelCase = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_squeezebert_for_token_classification(*lowerCAmelCase__ ) def _UpperCamelCase ( self : List[Any] ) -> List[str]: UpperCAmelCase = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_squeezebert_for_multiple_choice(*lowerCAmelCase__ ) @slow def _UpperCamelCase ( self : Dict ) -> Optional[int]: for model_name in SQUEEZEBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: UpperCAmelCase = SqueezeBertModel.from_pretrained(lowerCAmelCase__ ) self.assertIsNotNone(lowerCAmelCase__ ) @require_sentencepiece @require_tokenizers @require_torch class __magic_name__ ( unittest.TestCase ): @slow def _UpperCamelCase ( self : Any ) -> Any: UpperCAmelCase = SqueezeBertForSequenceClassification.from_pretrained("squeezebert/squeezebert-mnli" ) UpperCAmelCase = torch.tensor([[1, 2_9_4_1_4, 2_3_2, 3_2_8, 7_4_0, 1_1_4_0, 1_2_6_9_5, 6_9, 1_3, 1_5_8_8, 2]] ) UpperCAmelCase = model(lowerCAmelCase__ )[0] UpperCAmelCase = torch.Size((1, 3) ) self.assertEqual(output.shape , lowerCAmelCase__ ) UpperCAmelCase = torch.tensor([[0.6_401, -0.0_349, -0.6_041]] ) self.assertTrue(torch.allclose(lowerCAmelCase__ , lowerCAmelCase__ , atol=1e-4 ) )
719
lowerCAmelCase__ = "\n# Transformers 설치 방법\n! pip install transformers datasets\n# 마지막 릴리스 대신 소스에서 설치하려면, 위 명령을 주석으로 바꾸고 아래 명령을 해제하세요.\n# ! pip install git+https://github.com/huggingface/transformers.git\n" lowerCAmelCase__ = [{"type": "code", "content": INSTALL_CONTENT}] lowerCAmelCase__ = { "{processor_class}": "FakeProcessorClass", "{model_class}": "FakeModelClass", "{object_class}": "FakeObjectClass", }
1
0
lowerCAmelCase__ = { "Pillow": "Pillow", "accelerate": "accelerate>=0.11.0", "compel": "compel==0.1.8", "black": "black~=23.1", "datasets": "datasets", "filelock": "filelock", "flax": "flax>=0.4.1", "hf-doc-builder": "hf-doc-builder>=0.3.0", "huggingface-hub": "huggingface-hub>=0.13.2", "requests-mock": "requests-mock==1.10.0", "importlib_metadata": "importlib_metadata", "invisible-watermark": "invisible-watermark", "isort": "isort>=5.5.4", "jax": "jax>=0.2.8,!=0.3.2", "jaxlib": "jaxlib>=0.1.65", "Jinja2": "Jinja2", "k-diffusion": "k-diffusion>=0.0.12", "torchsde": "torchsde", "note_seq": "note_seq", "librosa": "librosa", "numpy": "numpy", "omegaconf": "omegaconf", "parameterized": "parameterized", "protobuf": "protobuf>=3.20.3,<4", "pytest": "pytest", "pytest-timeout": "pytest-timeout", "pytest-xdist": "pytest-xdist", "ruff": "ruff>=0.0.241", "safetensors": "safetensors", "sentencepiece": "sentencepiece>=0.1.91,!=0.1.92", "scipy": "scipy", "onnx": "onnx", "regex": "regex!=2019.12.17", "requests": "requests", "tensorboard": "tensorboard", "torch": "torch>=1.4", "torchvision": "torchvision", "transformers": "transformers>=4.25.1", "urllib3": "urllib3<=2.0.0", }
720
import gc import random import unittest import numpy as np import torch from PIL import Image from transformers import XLMRobertaTokenizerFast from diffusers import DDIMScheduler, KandinskyInpaintPipeline, KandinskyPriorPipeline, UNetaDConditionModel, VQModel from diffusers.pipelines.kandinsky.text_encoder import MCLIPConfig, MultilingualCLIP from diffusers.utils import floats_tensor, load_image, load_numpy, slow, torch_device from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu from ..test_pipelines_common import PipelineTesterMixin, assert_mean_pixel_difference enable_full_determinism() class __magic_name__ ( _snake_case , unittest.TestCase ): UpperCAmelCase = KandinskyInpaintPipeline UpperCAmelCase = ["""prompt""", """image_embeds""", """negative_image_embeds""", """image""", """mask_image"""] UpperCAmelCase = [ """prompt""", """negative_prompt""", """image_embeds""", """negative_image_embeds""", """image""", """mask_image""", ] UpperCAmelCase = [ """generator""", """height""", """width""", """latents""", """guidance_scale""", """negative_prompt""", """num_inference_steps""", """return_dict""", """guidance_scale""", """num_images_per_prompt""", """output_type""", """return_dict""", ] UpperCAmelCase = False @property def _UpperCamelCase ( self : Union[str, Any] ) -> Tuple: return 3_2 @property def _UpperCamelCase ( self : int ) -> List[Any]: return 3_2 @property def _UpperCamelCase ( self : List[Any] ) -> List[Any]: return self.time_input_dim @property def _UpperCamelCase ( self : Tuple ) -> Tuple: return self.time_input_dim * 4 @property def _UpperCamelCase ( self : Any ) -> Optional[int]: return 1_0_0 @property def _UpperCamelCase ( self : Optional[int] ) -> Optional[int]: UpperCAmelCase = XLMRobertaTokenizerFast.from_pretrained("YiYiXu/tiny-random-mclip-base" ) return tokenizer @property def _UpperCamelCase ( self : int ) -> Dict: torch.manual_seed(0 ) UpperCAmelCase = MCLIPConfig( numDims=self.cross_attention_dim , transformerDimensions=self.text_embedder_hidden_size , hidden_size=self.text_embedder_hidden_size , intermediate_size=3_7 , num_attention_heads=4 , num_hidden_layers=5 , vocab_size=1_0_0_5 , ) UpperCAmelCase = MultilingualCLIP(lowerCAmelCase__ ) UpperCAmelCase = text_encoder.eval() return text_encoder @property def _UpperCamelCase ( self : Dict ) -> Optional[int]: torch.manual_seed(0 ) UpperCAmelCase = { "in_channels": 9, # Out channels is double in channels because predicts mean and variance "out_channels": 8, "addition_embed_type": "text_image", "down_block_types": ("ResnetDownsampleBlock2D", "SimpleCrossAttnDownBlock2D"), "up_block_types": ("SimpleCrossAttnUpBlock2D", "ResnetUpsampleBlock2D"), "mid_block_type": "UNetMidBlock2DSimpleCrossAttn", "block_out_channels": (self.block_out_channels_a, self.block_out_channels_a * 2), "layers_per_block": 1, "encoder_hid_dim": self.text_embedder_hidden_size, "encoder_hid_dim_type": "text_image_proj", "cross_attention_dim": self.cross_attention_dim, "attention_head_dim": 4, "resnet_time_scale_shift": "scale_shift", "class_embed_type": None, } UpperCAmelCase = UNetaDConditionModel(**lowerCAmelCase__ ) return model @property def _UpperCamelCase ( self : str ) -> Optional[Any]: return { "block_out_channels": [3_2, 6_4], "down_block_types": ["DownEncoderBlock2D", "AttnDownEncoderBlock2D"], "in_channels": 3, "latent_channels": 4, "layers_per_block": 1, "norm_num_groups": 8, "norm_type": "spatial", "num_vq_embeddings": 1_2, "out_channels": 3, "up_block_types": [ "AttnUpDecoderBlock2D", "UpDecoderBlock2D", ], "vq_embed_dim": 4, } @property def _UpperCamelCase ( self : Dict ) -> List[Any]: torch.manual_seed(0 ) UpperCAmelCase = VQModel(**self.dummy_movq_kwargs ) return model def _UpperCamelCase ( self : Tuple ) -> Any: UpperCAmelCase = self.dummy_text_encoder UpperCAmelCase = self.dummy_tokenizer UpperCAmelCase = self.dummy_unet UpperCAmelCase = self.dummy_movq UpperCAmelCase = DDIMScheduler( num_train_timesteps=1_0_0_0 , beta_schedule="linear" , beta_start=0.00_085 , beta_end=0.012 , clip_sample=lowerCAmelCase__ , set_alpha_to_one=lowerCAmelCase__ , steps_offset=1 , prediction_type="epsilon" , thresholding=lowerCAmelCase__ , ) UpperCAmelCase = { "text_encoder": text_encoder, "tokenizer": tokenizer, "unet": unet, "scheduler": scheduler, "movq": movq, } return components def _UpperCamelCase ( self : Union[str, Any] , lowerCAmelCase__ : Any , lowerCAmelCase__ : Tuple=0 ) -> str: UpperCAmelCase = floats_tensor((1, self.cross_attention_dim) , rng=random.Random(lowerCAmelCase__ ) ).to(lowerCAmelCase__ ) UpperCAmelCase = floats_tensor((1, self.cross_attention_dim) , rng=random.Random(seed + 1 ) ).to(lowerCAmelCase__ ) # create init_image UpperCAmelCase = floats_tensor((1, 3, 6_4, 6_4) , rng=random.Random(lowerCAmelCase__ ) ).to(lowerCAmelCase__ ) UpperCAmelCase = image.cpu().permute(0 , 2 , 3 , 1 )[0] UpperCAmelCase = Image.fromarray(np.uinta(lowerCAmelCase__ ) ).convert("RGB" ).resize((2_5_6, 2_5_6) ) # create mask UpperCAmelCase = np.ones((6_4, 6_4) , dtype=np.floataa ) UpperCAmelCase = 0 if str(lowerCAmelCase__ ).startswith("mps" ): UpperCAmelCase = torch.manual_seed(lowerCAmelCase__ ) else: UpperCAmelCase = torch.Generator(device=lowerCAmelCase__ ).manual_seed(lowerCAmelCase__ ) UpperCAmelCase = { "prompt": "horse", "image": init_image, "mask_image": mask, "image_embeds": image_embeds, "negative_image_embeds": negative_image_embeds, "generator": generator, "height": 6_4, "width": 6_4, "num_inference_steps": 2, "guidance_scale": 4.0, "output_type": "np", } return inputs def _UpperCamelCase ( self : Dict ) -> List[str]: UpperCAmelCase = "cpu" UpperCAmelCase = self.get_dummy_components() UpperCAmelCase = self.pipeline_class(**lowerCAmelCase__ ) UpperCAmelCase = pipe.to(lowerCAmelCase__ ) pipe.set_progress_bar_config(disable=lowerCAmelCase__ ) UpperCAmelCase = pipe(**self.get_dummy_inputs(lowerCAmelCase__ ) ) UpperCAmelCase = output.images UpperCAmelCase = pipe( **self.get_dummy_inputs(lowerCAmelCase__ ) , return_dict=lowerCAmelCase__ , )[0] UpperCAmelCase = image[0, -3:, -3:, -1] UpperCAmelCase = image_from_tuple[0, -3:, -3:, -1] print(f"image.shape {image.shape}" ) assert image.shape == (1, 6_4, 6_4, 3) UpperCAmelCase = np.array( [0.8_326_919, 0.73_790_467, 0.20_918_581, 0.9_309_612, 0.5_511_791, 0.43_713_328, 0.5_513_321, 0.49_922_934, 0.59_497_786] ) assert ( np.abs(image_slice.flatten() - expected_slice ).max() < 1e-2 ), f" expected_slice {expected_slice}, but got {image_slice.flatten()}" assert ( np.abs(image_from_tuple_slice.flatten() - expected_slice ).max() < 1e-2 ), f" expected_slice {expected_slice}, but got {image_from_tuple_slice.flatten()}" def _UpperCamelCase ( self : str ) -> Tuple: super().test_inference_batch_single_identical(expected_max_diff=3e-3 ) @slow @require_torch_gpu class __magic_name__ ( unittest.TestCase ): def _UpperCamelCase ( self : str ) -> str: # clean up the VRAM after each test super().tearDown() gc.collect() torch.cuda.empty_cache() def _UpperCamelCase ( self : Tuple ) -> int: UpperCAmelCase = load_numpy( "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main" "/kandinsky/kandinsky_inpaint_cat_with_hat_fp16.npy" ) UpperCAmelCase = load_image( "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main" "/kandinsky/cat.png" ) UpperCAmelCase = np.ones((7_6_8, 7_6_8) , dtype=np.floataa ) UpperCAmelCase = 0 UpperCAmelCase = "a hat" UpperCAmelCase = KandinskyPriorPipeline.from_pretrained( "kandinsky-community/kandinsky-2-1-prior" , torch_dtype=torch.floataa ) pipe_prior.to(lowerCAmelCase__ ) UpperCAmelCase = KandinskyInpaintPipeline.from_pretrained( "kandinsky-community/kandinsky-2-1-inpaint" , torch_dtype=torch.floataa ) UpperCAmelCase = pipeline.to(lowerCAmelCase__ ) pipeline.set_progress_bar_config(disable=lowerCAmelCase__ ) UpperCAmelCase = torch.Generator(device="cpu" ).manual_seed(0 ) UpperCAmelCase , UpperCAmelCase = pipe_prior( lowerCAmelCase__ , generator=lowerCAmelCase__ , num_inference_steps=5 , negative_prompt="" , ).to_tuple() UpperCAmelCase = pipeline( lowerCAmelCase__ , image=lowerCAmelCase__ , mask_image=lowerCAmelCase__ , image_embeds=lowerCAmelCase__ , negative_image_embeds=lowerCAmelCase__ , generator=lowerCAmelCase__ , num_inference_steps=1_0_0 , height=7_6_8 , width=7_6_8 , output_type="np" , ) UpperCAmelCase = output.images[0] assert image.shape == (7_6_8, 7_6_8, 3) assert_mean_pixel_difference(lowerCAmelCase__ , lowerCAmelCase__ )
1
0
import requests from bsa import BeautifulSoup def _lowerCAmelCase( __A , __A ): UpperCAmelCase = BeautifulSoup(requests.get(__A , params=__A ).content , "html.parser" ) UpperCAmelCase = soup.find("div" , attrs={"class": "gs_ri"} ) UpperCAmelCase = div.find("div" , attrs={"class": "gs_fl"} ).find_all("a" ) return anchors[2].get_text() if __name__ == "__main__": lowerCAmelCase__ = { "title": ( "Precisely geometry controlled microsupercapacitors for ultrahigh areal " "capacitance, volumetric capacitance, and energy density" ), "journal": "Chem. Mater.", "volume": 30, "pages": "3979-3990", "year": 2018, "hl": "en", } print(get_citation("https://scholar.google.com/scholar_lookup", params=params))
721
def _lowerCAmelCase( __A , __A ): return (pointa[0] - pointa[0]) ** 2 + (pointa[1] - pointa[1]) ** 2 def _lowerCAmelCase( __A , __A=0 ): return sorted(__A , key=lambda __A : x[column] ) def _lowerCAmelCase( __A , __A , __A=float("inf" ) ): for i in range(points_counts - 1 ): for j in range(i + 1 , __A ): UpperCAmelCase = euclidean_distance_sqr(points[i] , points[j] ) if current_dis < min_dis: UpperCAmelCase = current_dis return min_dis def _lowerCAmelCase( __A , __A , __A=float("inf" ) ): for i in range(min(6 , points_counts - 1 ) , __A ): for j in range(max(0 , i - 6 ) , __A ): UpperCAmelCase = euclidean_distance_sqr(points[i] , points[j] ) if current_dis < min_dis: UpperCAmelCase = current_dis return min_dis def _lowerCAmelCase( __A , __A , __A ): # base case if points_counts <= 3: return dis_between_closest_pair(__A , __A ) # recursion UpperCAmelCase = points_counts // 2 UpperCAmelCase = closest_pair_of_points_sqr( __A , points_sorted_on_y[:mid] , __A ) UpperCAmelCase = closest_pair_of_points_sqr( __A , points_sorted_on_y[mid:] , points_counts - mid ) UpperCAmelCase = min(__A , __A ) UpperCAmelCase = [] for point in points_sorted_on_x: if abs(point[0] - points_sorted_on_x[mid][0] ) < closest_pair_dis: cross_strip.append(__A ) UpperCAmelCase = dis_between_closest_in_strip( __A , len(__A ) , __A ) return min(__A , __A ) def _lowerCAmelCase( __A , __A ): UpperCAmelCase = column_based_sort(__A , column=0 ) UpperCAmelCase = column_based_sort(__A , column=1 ) return ( closest_pair_of_points_sqr( __A , __A , __A ) ) ** 0.5 if __name__ == "__main__": lowerCAmelCase__ = [(2, 3), (12, 30), (40, 50), (5, 1), (12, 10), (3, 4)] print("Distance:", closest_pair_of_points(points, len(points)))
1
0
"""simple docstring""" import argparse import json import requests import torch from huggingface_hub import hf_hub_download from PIL import Image from transformers import ViTImageProcessor, ViTMSNConfig, ViTMSNModel from transformers.image_utils import IMAGENET_DEFAULT_MEAN, IMAGENET_DEFAULT_STD torch.set_grad_enabled(False) def _a ( _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE=False ) -> Tuple: snake_case_ = [] for i in range(config.num_hidden_layers ): # encoder layers: output projection, 2 feedforward neural networks and 2 layernorms rename_keys.append((f"""module.blocks.{i}.norm1.weight""", f"""vit.encoder.layer.{i}.layernorm_before.weight""") ) rename_keys.append((f"""module.blocks.{i}.norm1.bias""", f"""vit.encoder.layer.{i}.layernorm_before.bias""") ) rename_keys.append( (f"""module.blocks.{i}.attn.proj.weight""", f"""vit.encoder.layer.{i}.attention.output.dense.weight""") ) rename_keys.append((f"""module.blocks.{i}.attn.proj.bias""", f"""vit.encoder.layer.{i}.attention.output.dense.bias""") ) rename_keys.append((f"""module.blocks.{i}.norm2.weight""", f"""vit.encoder.layer.{i}.layernorm_after.weight""") ) rename_keys.append((f"""module.blocks.{i}.norm2.bias""", f"""vit.encoder.layer.{i}.layernorm_after.bias""") ) rename_keys.append((f"""module.blocks.{i}.mlp.fc1.weight""", f"""vit.encoder.layer.{i}.intermediate.dense.weight""") ) rename_keys.append((f"""module.blocks.{i}.mlp.fc1.bias""", f"""vit.encoder.layer.{i}.intermediate.dense.bias""") ) rename_keys.append((f"""module.blocks.{i}.mlp.fc2.weight""", f"""vit.encoder.layer.{i}.output.dense.weight""") ) rename_keys.append((f"""module.blocks.{i}.mlp.fc2.bias""", f"""vit.encoder.layer.{i}.output.dense.bias""") ) # projection layer + position embeddings rename_keys.extend( [ ("""module.cls_token""", """vit.embeddings.cls_token"""), ("""module.patch_embed.proj.weight""", """vit.embeddings.patch_embeddings.projection.weight"""), ("""module.patch_embed.proj.bias""", """vit.embeddings.patch_embeddings.projection.bias"""), ("""module.pos_embed""", """vit.embeddings.position_embeddings"""), ] ) if base_model: # layernorm + pooler rename_keys.extend( [ ("""module.norm.weight""", """layernorm.weight"""), ("""module.norm.bias""", """layernorm.bias"""), ] ) # if just the base model, we should remove "vit" from all keys that start with "vit" snake_case_ = [(pair[0], pair[1][4:]) if pair[1].startswith("""vit""" ) else pair for pair in rename_keys] else: # layernorm + classification head rename_keys.extend( [ ("""norm.weight""", """vit.layernorm.weight"""), ("""norm.bias""", """vit.layernorm.bias"""), ("""head.weight""", """classifier.weight"""), ("""head.bias""", """classifier.bias"""), ] ) return rename_keys def _a ( _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE=False ) -> int: for i in range(config.num_hidden_layers ): if base_model: snake_case_ = """""" else: snake_case_ = """vit.""" # read in weights + bias of input projection layer (in timm, this is a single matrix + bias) snake_case_ = state_dict.pop(f"""module.blocks.{i}.attn.qkv.weight""" ) snake_case_ = state_dict.pop(f"""module.blocks.{i}.attn.qkv.bias""" ) # next, add query, keys and values (in that order) to the state dict snake_case_ = in_proj_weight[ : config.hidden_size, : ] snake_case_ = in_proj_bias[: config.hidden_size] snake_case_ = in_proj_weight[ config.hidden_size : config.hidden_size * 2, : ] snake_case_ = in_proj_bias[ config.hidden_size : config.hidden_size * 2 ] snake_case_ = in_proj_weight[ -config.hidden_size :, : ] snake_case_ = in_proj_bias[-config.hidden_size :] def _a ( _SCREAMING_SNAKE_CASE ) -> List[str]: snake_case_ = ["""head.weight""", """head.bias"""] for k in ignore_keys: state_dict.pop(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) def _a ( _SCREAMING_SNAKE_CASE ) -> List[Any]: # projection head is used in the self-supervised pre-training in MSN, # for downstream task it's not needed. snake_case_ = [ """module.fc.fc1.weight""", """module.fc.fc1.bias""", """module.fc.bn1.weight""", """module.fc.bn1.bias""", """module.fc.bn1.running_mean""", """module.fc.bn1.running_var""", """module.fc.bn1.num_batches_tracked""", """module.fc.fc2.weight""", """module.fc.fc2.bias""", """module.fc.bn2.weight""", """module.fc.bn2.bias""", """module.fc.bn2.running_mean""", """module.fc.bn2.running_var""", """module.fc.bn2.num_batches_tracked""", """module.fc.fc3.weight""", """module.fc.fc3.bias""", ] for k in ignore_keys: state_dict.pop(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) def _a ( _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> Optional[int]: snake_case_ = dct.pop(_SCREAMING_SNAKE_CASE ) snake_case_ = val def _a ( _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> List[str]: snake_case_ = ViTMSNConfig() snake_case_ = 1_000 snake_case_ = """datasets/huggingface/label-files""" snake_case_ = """imagenet-1k-id2label.json""" snake_case_ = json.load(open(hf_hub_download(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) , """r""" ) ) snake_case_ = {int(_SCREAMING_SNAKE_CASE ): v for k, v in idalabel.items()} snake_case_ = idalabel snake_case_ = {v: k for k, v in idalabel.items()} if "s16" in checkpoint_url: snake_case_ = 384 snake_case_ = 1_536 snake_case_ = 6 elif "l16" in checkpoint_url: snake_case_ = 1_024 snake_case_ = 4_096 snake_case_ = 24 snake_case_ = 16 snake_case_ = 0.1 elif "b4" in checkpoint_url: snake_case_ = 4 elif "l7" in checkpoint_url: snake_case_ = 7 snake_case_ = 1_024 snake_case_ = 4_096 snake_case_ = 24 snake_case_ = 16 snake_case_ = 0.1 snake_case_ = ViTMSNModel(_SCREAMING_SNAKE_CASE ) snake_case_ = torch.hub.load_state_dict_from_url(_SCREAMING_SNAKE_CASE , map_location="""cpu""" )["""target_encoder"""] snake_case_ = ViTImageProcessor(size=config.image_size ) remove_projection_head(_SCREAMING_SNAKE_CASE ) snake_case_ = create_rename_keys(_SCREAMING_SNAKE_CASE , base_model=_SCREAMING_SNAKE_CASE ) for src, dest in rename_keys: rename_key(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) read_in_q_k_v(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , base_model=_SCREAMING_SNAKE_CASE ) model.load_state_dict(_SCREAMING_SNAKE_CASE ) model.eval() snake_case_ = """http://images.cocodataset.org/val2017/000000039769.jpg""" snake_case_ = Image.open(requests.get(_SCREAMING_SNAKE_CASE , stream=_SCREAMING_SNAKE_CASE ).raw ) snake_case_ = ViTImageProcessor( size=config.image_size , image_mean=_SCREAMING_SNAKE_CASE , image_std=_SCREAMING_SNAKE_CASE ) snake_case_ = image_processor(images=_SCREAMING_SNAKE_CASE , return_tensors="""pt""" ) # forward pass torch.manual_seed(2 ) snake_case_ = model(**_SCREAMING_SNAKE_CASE ) snake_case_ = outputs.last_hidden_state # The following Colab Notebook was used to generate these outputs: # https://colab.research.google.com/gist/sayakpaul/3672419a04f5997827503fd84079bdd1/scratchpad.ipynb if "s16" in checkpoint_url: snake_case_ = torch.tensor([[-1.0915, -1.4876, -1.1809]] ) elif "b16" in checkpoint_url: snake_case_ = torch.tensor([[14.2889, -18.9045, 11.7281]] ) elif "l16" in checkpoint_url: snake_case_ = torch.tensor([[41.5028, -22.8681, 45.6475]] ) elif "b4" in checkpoint_url: snake_case_ = torch.tensor([[-4.3868, 5.2932, -0.4137]] ) else: snake_case_ = torch.tensor([[-0.1792, -0.6465, 2.4263]] ) # verify logits assert torch.allclose(last_hidden_state[:, 0, :3] , _SCREAMING_SNAKE_CASE , atol=1E-4 ) print(f"""Saving model to {pytorch_dump_folder_path}""" ) model.save_pretrained(_SCREAMING_SNAKE_CASE ) print(f"""Saving image processor to {pytorch_dump_folder_path}""" ) image_processor.save_pretrained(_SCREAMING_SNAKE_CASE ) if __name__ == "__main__": __SCREAMING_SNAKE_CASE : Any = argparse.ArgumentParser() # Required parameters parser.add_argument( '--checkpoint_url', default='https://dl.fbaipublicfiles.com/msn/vits16_800ep.pth.tar', type=str, help='URL of the checkpoint you\'d like to convert.', ) parser.add_argument( '--pytorch_dump_folder_path', default=None, type=str, help='Path to the output PyTorch model directory.' ) __SCREAMING_SNAKE_CASE : Any = parser.parse_args() convert_vit_msn_checkpoint(args.checkpoint_url, args.pytorch_dump_folder_path)
2
"""simple docstring""" import copy from dataclasses import dataclass, field from typing import ClassVar, Dict from ..features import Audio, Features, Value from .base import TaskTemplate @dataclass(frozen=snake_case__) class __A (snake_case__): '''simple docstring''' __lowercase: str = field(default="""automatic-speech-recognition""" , metadata={"""include_in_asdict_even_if_is_default""": True}) __lowercase: ClassVar[Features] = Features({"""audio""": Audio()}) __lowercase: ClassVar[Features] = Features({"""transcription""": Value("""string""")}) __lowercase: str = "audio" __lowercase: str = "transcription" def lowerCAmelCase ( self : List[Any] , UpperCAmelCase_ : Any ) ->int: """simple docstring""" if self.audio_column not in features: raise ValueError(F"""Column {self.audio_column} is not present in features.""" ) if not isinstance(features[self.audio_column] , UpperCAmelCase_ ): raise ValueError(F"""Column {self.audio_column} is not an Audio type.""" ) snake_case_ = copy.deepcopy(self ) snake_case_ = self.input_schema.copy() snake_case_ = features[self.audio_column] snake_case_ = input_schema return task_template @property def lowerCAmelCase ( self : List[str] ) ->Dict[str, str]: """simple docstring""" return {self.audio_column: "audio", self.transcription_column: "transcription"}
2
1
"""simple docstring""" import logging import os from .state import PartialState class __A (logging.LoggerAdapter): '''simple docstring''' @staticmethod def lowerCAmelCase ( UpperCAmelCase_ : List[str] ) ->List[Any]: """simple docstring""" snake_case_ = PartialState() return not main_process_only or (main_process_only and state.is_main_process) def lowerCAmelCase ( self : List[str] , UpperCAmelCase_ : Any , UpperCAmelCase_ : List[Any] , *UpperCAmelCase_ : Any , **UpperCAmelCase_ : int ) ->str: """simple docstring""" if PartialState._shared_state == {}: raise RuntimeError( """You must initialize the accelerate state by calling either `PartialState()` or `Accelerator()` before using the logging utility.""" ) snake_case_ = kwargs.pop("""main_process_only""" , UpperCAmelCase_ ) snake_case_ = kwargs.pop("""in_order""" , UpperCAmelCase_ ) if self.isEnabledFor(UpperCAmelCase_ ): if self._should_log(UpperCAmelCase_ ): snake_case_ , snake_case_ = self.process(UpperCAmelCase_ , UpperCAmelCase_ ) self.logger.log(UpperCAmelCase_ , UpperCAmelCase_ , *UpperCAmelCase_ , **UpperCAmelCase_ ) elif in_order: snake_case_ = PartialState() for i in range(state.num_processes ): if i == state.process_index: snake_case_ , snake_case_ = self.process(UpperCAmelCase_ , UpperCAmelCase_ ) self.logger.log(UpperCAmelCase_ , UpperCAmelCase_ , *UpperCAmelCase_ , **UpperCAmelCase_ ) state.wait_for_everyone() def _a ( _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE = None ) -> Optional[Any]: if log_level is None: snake_case_ = os.environ.get("""ACCELERATE_LOG_LEVEL""" , _SCREAMING_SNAKE_CASE ) snake_case_ = logging.getLogger(_SCREAMING_SNAKE_CASE ) if log_level is not None: logger.setLevel(log_level.upper() ) logger.root.setLevel(log_level.upper() ) return MultiProcessAdapter(_SCREAMING_SNAKE_CASE , {} )
2
"""simple docstring""" from functools import reduce __SCREAMING_SNAKE_CASE : Tuple = ( '73167176531330624919225119674426574742355349194934' '96983520312774506326239578318016984801869478851843' '85861560789112949495459501737958331952853208805511' '12540698747158523863050715693290963295227443043557' '66896648950445244523161731856403098711121722383113' '62229893423380308135336276614282806444486645238749' '30358907296290491560440772390713810515859307960866' '70172427121883998797908792274921901699720888093776' '65727333001053367881220235421809751254540594752243' '52584907711670556013604839586446706324415722155397' '53697817977846174064955149290862569321978468622482' '83972241375657056057490261407972968652414535100474' '82166370484403199890008895243450658541227588666881' '16427171479924442928230863465674813919123162824586' '17866458359124566529476545682848912883142607690042' '24219022671055626321111109370544217506941658960408' '07198403850962455444362981230987879927244284909188' '84580156166097919133875499200524063689912560717606' '05886116467109405077541002256983155200055935729725' '71636269561882670428252483600823257530420752963450' ) def _a ( _SCREAMING_SNAKE_CASE = N ) -> int: return max( # mypy cannot properly interpret reduce int(reduce(lambda _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE : str(int(_SCREAMING_SNAKE_CASE ) * int(_SCREAMING_SNAKE_CASE ) ) , n[i : i + 13] ) ) for i in range(len(_SCREAMING_SNAKE_CASE ) - 12 ) ) if __name__ == "__main__": print(f"""{solution() = }""")
2
1
"""simple docstring""" from typing import TYPE_CHECKING from ....utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available __SCREAMING_SNAKE_CASE : int = {'configuration_mmbt': ['MMBTConfig']} try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __SCREAMING_SNAKE_CASE : List[Any] = ['MMBTForClassification', 'MMBTModel', 'ModalEmbeddings'] if TYPE_CHECKING: from .configuration_mmbt import MMBTConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_mmbt import MMBTForClassification, MMBTModel, ModalEmbeddings else: import sys __SCREAMING_SNAKE_CASE : Any = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
2
"""simple docstring""" from ....configuration_utils import PretrainedConfig from ....utils import logging __SCREAMING_SNAKE_CASE : str = logging.get_logger(__name__) __SCREAMING_SNAKE_CASE : str = { 'speechbrain/m-ctc-t-large': 'https://huggingface.co/speechbrain/m-ctc-t-large/resolve/main/config.json', # See all M-CTC-T models at https://huggingface.co/models?filter=mctct } class __A (snake_case__): '''simple docstring''' __lowercase: Any = """mctct""" def __init__( self : Dict , UpperCAmelCase_ : List[Any]=8_065 , UpperCAmelCase_ : Tuple=1_536 , UpperCAmelCase_ : Optional[Any]=36 , UpperCAmelCase_ : int=6_144 , UpperCAmelCase_ : Dict=4 , UpperCAmelCase_ : Any=384 , UpperCAmelCase_ : List[str]=920 , UpperCAmelCase_ : Any=1E-5 , UpperCAmelCase_ : Any=0.3 , UpperCAmelCase_ : Tuple="relu" , UpperCAmelCase_ : Union[str, Any]=0.02 , UpperCAmelCase_ : Dict=0.3 , UpperCAmelCase_ : str=0.3 , UpperCAmelCase_ : Any=1 , UpperCAmelCase_ : Any=0 , UpperCAmelCase_ : int=2 , UpperCAmelCase_ : str=1 , UpperCAmelCase_ : Tuple=0.3 , UpperCAmelCase_ : Optional[Any]=1 , UpperCAmelCase_ : Optional[Any]=(7,) , UpperCAmelCase_ : Optional[Any]=(3,) , UpperCAmelCase_ : List[str]=80 , UpperCAmelCase_ : Tuple=1 , UpperCAmelCase_ : List[Any]=None , UpperCAmelCase_ : List[str]="sum" , UpperCAmelCase_ : Union[str, Any]=False , **UpperCAmelCase_ : Any , ) ->Dict: """simple docstring""" super().__init__(**UpperCAmelCase_ , pad_token_id=UpperCAmelCase_ , bos_token_id=UpperCAmelCase_ , eos_token_id=UpperCAmelCase_ ) snake_case_ = vocab_size snake_case_ = hidden_size snake_case_ = num_hidden_layers snake_case_ = intermediate_size snake_case_ = num_attention_heads snake_case_ = attention_head_dim snake_case_ = max_position_embeddings snake_case_ = layer_norm_eps snake_case_ = layerdrop snake_case_ = hidden_act snake_case_ = initializer_range snake_case_ = hidden_dropout_prob snake_case_ = attention_probs_dropout_prob snake_case_ = pad_token_id snake_case_ = bos_token_id snake_case_ = eos_token_id snake_case_ = conv_glu_dim snake_case_ = conv_dropout snake_case_ = num_conv_layers snake_case_ = input_feat_per_channel snake_case_ = input_channels snake_case_ = conv_channels snake_case_ = ctc_loss_reduction snake_case_ = ctc_zero_infinity # prevents config testing fail with exporting to json snake_case_ = list(UpperCAmelCase_ ) snake_case_ = list(UpperCAmelCase_ ) if len(self.conv_kernel ) != self.num_conv_layers: raise ValueError( """Configuration for convolutional module is incorrect. """ """It is required that `len(config.conv_kernel)` == `config.num_conv_layers` """ F"""but is `len(config.conv_kernel) = {len(self.conv_kernel )}`, """ F"""`config.num_conv_layers = {self.num_conv_layers}`.""" )
2
1
"""simple docstring""" from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available __SCREAMING_SNAKE_CASE : str = { 'configuration_lilt': ['LILT_PRETRAINED_CONFIG_ARCHIVE_MAP', 'LiltConfig'], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __SCREAMING_SNAKE_CASE : Tuple = [ 'LILT_PRETRAINED_MODEL_ARCHIVE_LIST', 'LiltForQuestionAnswering', 'LiltForSequenceClassification', 'LiltForTokenClassification', 'LiltModel', 'LiltPreTrainedModel', ] if TYPE_CHECKING: from .configuration_lilt import LILT_PRETRAINED_CONFIG_ARCHIVE_MAP, LiltConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_lilt import ( LILT_PRETRAINED_MODEL_ARCHIVE_LIST, LiltForQuestionAnswering, LiltForSequenceClassification, LiltForTokenClassification, LiltModel, LiltPreTrainedModel, ) else: import sys __SCREAMING_SNAKE_CASE : Tuple = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
2
"""simple docstring""" from math import factorial def _a ( _SCREAMING_SNAKE_CASE = 100 ) -> int: return sum(int(_SCREAMING_SNAKE_CASE ) for x in str(factorial(_SCREAMING_SNAKE_CASE ) ) ) if __name__ == "__main__": print(solution(int(input('Enter the Number: ').strip())))
2
1
"""simple docstring""" import pprint import requests __SCREAMING_SNAKE_CASE : Tuple = 'https://zenquotes.io/api' def _a ( ) -> list: return requests.get(API_ENDPOINT_URL + """/today""" ).json() def _a ( ) -> list: return requests.get(API_ENDPOINT_URL + """/random""" ).json() if __name__ == "__main__": __SCREAMING_SNAKE_CASE : Optional[int] = random_quotes() pprint.pprint(response)
2
"""simple docstring""" 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 (snake_case__ , snake_case__ , unittest.TestCase): '''simple docstring''' __lowercase: str = VQModel __lowercase: Union[str, Any] = """sample""" @property def lowerCAmelCase ( self : List[str] , UpperCAmelCase_ : List[str]=(32, 32) ) ->Tuple: """simple docstring""" snake_case_ = 4 snake_case_ = 3 snake_case_ = floats_tensor((batch_size, num_channels) + sizes ).to(UpperCAmelCase_ ) return {"sample": image} @property def lowerCAmelCase ( self : Tuple ) ->str: """simple docstring""" return (3, 32, 32) @property def lowerCAmelCase ( self : List[Any] ) ->Any: """simple docstring""" return (3, 32, 32) def lowerCAmelCase ( self : Optional[int] ) ->Dict: """simple docstring""" snake_case_ = { """block_out_channels""": [32, 64], """in_channels""": 3, """out_channels""": 3, """down_block_types""": ["""DownEncoderBlock2D""", """DownEncoderBlock2D"""], """up_block_types""": ["""UpDecoderBlock2D""", """UpDecoderBlock2D"""], """latent_channels""": 3, } snake_case_ = self.dummy_input return init_dict, inputs_dict def lowerCAmelCase ( self : List[str] ) ->Dict: """simple docstring""" pass def lowerCAmelCase ( self : Optional[Any] ) ->List[str]: """simple docstring""" pass def lowerCAmelCase ( self : Any ) ->Union[str, Any]: """simple docstring""" snake_case_ , snake_case_ = VQModel.from_pretrained("""fusing/vqgan-dummy""" , output_loading_info=UpperCAmelCase_ ) self.assertIsNotNone(UpperCAmelCase_ ) self.assertEqual(len(loading_info["""missing_keys"""] ) , 0 ) model.to(UpperCAmelCase_ ) snake_case_ = model(**self.dummy_input ) assert image is not None, "Make sure output is not None" def lowerCAmelCase ( self : Tuple ) ->Optional[Any]: """simple docstring""" snake_case_ = VQModel.from_pretrained("""fusing/vqgan-dummy""" ) model.to(UpperCAmelCase_ ).eval() torch.manual_seed(0 ) if torch.cuda.is_available(): torch.cuda.manual_seed_all(0 ) snake_case_ = torch.randn(1 , model.config.in_channels , model.config.sample_size , model.config.sample_size ) snake_case_ = image.to(UpperCAmelCase_ ) with torch.no_grad(): snake_case_ = model(UpperCAmelCase_ ).sample snake_case_ = output[0, -1, -3:, -3:].flatten().cpu() # fmt: off snake_case_ = torch.tensor([-0.0_153, -0.4_044, -0.1_880, -0.5_161, -0.2_418, -0.4_072, -0.1_612, -0.0_633, -0.0_143] ) # fmt: on self.assertTrue(torch.allclose(UpperCAmelCase_ , UpperCAmelCase_ , atol=1E-3 ) )
2
1
"""simple docstring""" def _a ( _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> int: if index == number_of_items: return 0 snake_case_ = 0 snake_case_ = 0 snake_case_ = knapsack(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , index + 1 ) if weights[index] <= max_weight: snake_case_ = values[index] + knapsack( _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , max_weight - weights[index] , index + 1 ) return max(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) if __name__ == "__main__": import doctest doctest.testmod()
2
"""simple docstring""" import gc import random import unittest import numpy as np import torch from diffusers import ( DDIMScheduler, KandinskyVaaControlnetPipeline, KandinskyVaaPriorPipeline, UNetaDConditionModel, VQModel, ) from diffusers.utils import floats_tensor, load_image, load_numpy, slow, torch_device from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu from ..test_pipelines_common import PipelineTesterMixin, assert_mean_pixel_difference enable_full_determinism() class __A (snake_case__ , unittest.TestCase): '''simple docstring''' __lowercase: Dict = KandinskyVaaControlnetPipeline __lowercase: str = ["""image_embeds""", """negative_image_embeds""", """hint"""] __lowercase: List[str] = ["""image_embeds""", """negative_image_embeds""", """hint"""] __lowercase: Union[str, Any] = [ """generator""", """height""", """width""", """latents""", """guidance_scale""", """num_inference_steps""", """return_dict""", """guidance_scale""", """num_images_per_prompt""", """output_type""", """return_dict""", ] __lowercase: Tuple = False @property def lowerCAmelCase ( self : Any ) ->Union[str, Any]: """simple docstring""" return 32 @property def lowerCAmelCase ( self : Optional[Any] ) ->Union[str, Any]: """simple docstring""" return 32 @property def lowerCAmelCase ( self : int ) ->List[str]: """simple docstring""" return self.time_input_dim @property def lowerCAmelCase ( self : Union[str, Any] ) ->Union[str, Any]: """simple docstring""" return self.time_input_dim * 4 @property def lowerCAmelCase ( self : Dict ) ->Optional[int]: """simple docstring""" return 100 @property def lowerCAmelCase ( self : str ) ->List[Any]: """simple docstring""" torch.manual_seed(0 ) snake_case_ = { """in_channels""": 8, # Out channels is double in channels because predicts mean and variance """out_channels""": 8, """addition_embed_type""": """image_hint""", """down_block_types""": ("""ResnetDownsampleBlock2D""", """SimpleCrossAttnDownBlock2D"""), """up_block_types""": ("""SimpleCrossAttnUpBlock2D""", """ResnetUpsampleBlock2D"""), """mid_block_type""": """UNetMidBlock2DSimpleCrossAttn""", """block_out_channels""": (self.block_out_channels_a, self.block_out_channels_a * 2), """layers_per_block""": 1, """encoder_hid_dim""": self.text_embedder_hidden_size, """encoder_hid_dim_type""": """image_proj""", """cross_attention_dim""": self.cross_attention_dim, """attention_head_dim""": 4, """resnet_time_scale_shift""": """scale_shift""", """class_embed_type""": None, } snake_case_ = UNetaDConditionModel(**UpperCAmelCase_ ) return model @property def lowerCAmelCase ( self : Any ) ->Optional[Any]: """simple docstring""" return { "block_out_channels": [32, 32, 64, 64], "down_block_types": [ "DownEncoderBlock2D", "DownEncoderBlock2D", "DownEncoderBlock2D", "AttnDownEncoderBlock2D", ], "in_channels": 3, "latent_channels": 4, "layers_per_block": 1, "norm_num_groups": 8, "norm_type": "spatial", "num_vq_embeddings": 12, "out_channels": 3, "up_block_types": ["AttnUpDecoderBlock2D", "UpDecoderBlock2D", "UpDecoderBlock2D", "UpDecoderBlock2D"], "vq_embed_dim": 4, } @property def lowerCAmelCase ( self : Any ) ->int: """simple docstring""" torch.manual_seed(0 ) snake_case_ = VQModel(**self.dummy_movq_kwargs ) return model def lowerCAmelCase ( self : Dict ) ->str: """simple docstring""" snake_case_ = self.dummy_unet snake_case_ = self.dummy_movq snake_case_ = DDIMScheduler( num_train_timesteps=1_000 , beta_schedule="""linear""" , beta_start=0.00_085 , beta_end=0.012 , clip_sample=UpperCAmelCase_ , set_alpha_to_one=UpperCAmelCase_ , steps_offset=1 , prediction_type="""epsilon""" , thresholding=UpperCAmelCase_ , ) snake_case_ = { """unet""": unet, """scheduler""": scheduler, """movq""": movq, } return components def lowerCAmelCase ( self : Union[str, Any] , UpperCAmelCase_ : List[Any] , UpperCAmelCase_ : Optional[Any]=0 ) ->List[str]: """simple docstring""" snake_case_ = floats_tensor((1, self.text_embedder_hidden_size) , rng=random.Random(UpperCAmelCase_ ) ).to(UpperCAmelCase_ ) snake_case_ = floats_tensor((1, self.text_embedder_hidden_size) , rng=random.Random(seed + 1 ) ).to( UpperCAmelCase_ ) # create hint snake_case_ = floats_tensor((1, 3, 64, 64) , rng=random.Random(UpperCAmelCase_ ) ).to(UpperCAmelCase_ ) if str(UpperCAmelCase_ ).startswith("""mps""" ): snake_case_ = torch.manual_seed(UpperCAmelCase_ ) else: snake_case_ = torch.Generator(device=UpperCAmelCase_ ).manual_seed(UpperCAmelCase_ ) snake_case_ = { """image_embeds""": image_embeds, """negative_image_embeds""": negative_image_embeds, """hint""": hint, """generator""": generator, """height""": 64, """width""": 64, """guidance_scale""": 4.0, """num_inference_steps""": 2, """output_type""": """np""", } return inputs def lowerCAmelCase ( self : List[str] ) ->List[Any]: """simple docstring""" snake_case_ = """cpu""" snake_case_ = self.get_dummy_components() snake_case_ = self.pipeline_class(**UpperCAmelCase_ ) snake_case_ = pipe.to(UpperCAmelCase_ ) pipe.set_progress_bar_config(disable=UpperCAmelCase_ ) snake_case_ = pipe(**self.get_dummy_inputs(UpperCAmelCase_ ) ) snake_case_ = output.images snake_case_ = pipe( **self.get_dummy_inputs(UpperCAmelCase_ ) , return_dict=UpperCAmelCase_ , )[0] snake_case_ = image[0, -3:, -3:, -1] snake_case_ = image_from_tuple[0, -3:, -3:, -1] assert image.shape == (1, 64, 64, 3) snake_case_ = np.array( [0.6_959_826, 0.868_279, 0.7_558_092, 0.68_769_467, 0.85_805_804, 0.65_977_496, 0.44_885_302, 0.5_959_111, 0.4_251_595] ) assert ( np.abs(image_slice.flatten() - expected_slice ).max() < 1E-2 ), F""" expected_slice {expected_slice}, but got {image_slice.flatten()}""" assert ( np.abs(image_from_tuple_slice.flatten() - expected_slice ).max() < 1E-2 ), F""" expected_slice {expected_slice}, but got {image_from_tuple_slice.flatten()}""" @slow @require_torch_gpu class __A (unittest.TestCase): '''simple docstring''' def lowerCAmelCase ( self : List[Any] ) ->Union[str, Any]: """simple docstring""" super().tearDown() gc.collect() torch.cuda.empty_cache() def lowerCAmelCase ( self : List[str] ) ->List[str]: """simple docstring""" snake_case_ = load_numpy( """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main""" """/kandinskyv22/kandinskyv22_controlnet_robotcat_fp16.npy""" ) snake_case_ = load_image( """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main""" """/kandinskyv22/hint_image_cat.png""" ) snake_case_ = torch.from_numpy(np.array(UpperCAmelCase_ ) ).float() / 255.0 snake_case_ = hint.permute(2 , 0 , 1 ).unsqueeze(0 ) snake_case_ = KandinskyVaaPriorPipeline.from_pretrained( """kandinsky-community/kandinsky-2-2-prior""" , torch_dtype=torch.floataa ) pipe_prior.to(UpperCAmelCase_ ) snake_case_ = KandinskyVaaControlnetPipeline.from_pretrained( """kandinsky-community/kandinsky-2-2-controlnet-depth""" , torch_dtype=torch.floataa ) snake_case_ = pipeline.to(UpperCAmelCase_ ) pipeline.set_progress_bar_config(disable=UpperCAmelCase_ ) snake_case_ = """A robot, 4k photo""" snake_case_ = torch.Generator(device="""cuda""" ).manual_seed(0 ) snake_case_ , snake_case_ = pipe_prior( UpperCAmelCase_ , generator=UpperCAmelCase_ , num_inference_steps=5 , negative_prompt="""""" , ).to_tuple() snake_case_ = torch.Generator(device="""cuda""" ).manual_seed(0 ) snake_case_ = pipeline( image_embeds=UpperCAmelCase_ , negative_image_embeds=UpperCAmelCase_ , hint=UpperCAmelCase_ , generator=UpperCAmelCase_ , num_inference_steps=100 , output_type="""np""" , ) snake_case_ = output.images[0] assert image.shape == (512, 512, 3) assert_mean_pixel_difference(UpperCAmelCase_ , UpperCAmelCase_ )
2
1
"""simple docstring""" # using dfs for finding eulerian path traversal def _a ( _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE=None ) -> Dict: snake_case_ = (path or []) + [u] for v in graph[u]: if visited_edge[u][v] is False: snake_case_ , snake_case_ = True, True snake_case_ = dfs(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) return path def _a ( _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> List[Any]: snake_case_ = 0 snake_case_ = -1 for i in range(_SCREAMING_SNAKE_CASE ): if i not in graph.keys(): continue if len(graph[i] ) % 2 == 1: odd_degree_nodes += 1 snake_case_ = i if odd_degree_nodes == 0: return 1, odd_node if odd_degree_nodes == 2: return 2, odd_node return 3, odd_node def _a ( _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> str: snake_case_ = [[False for _ in range(max_node + 1 )] for _ in range(max_node + 1 )] snake_case_ , snake_case_ = check_circuit_or_path(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) if check == 3: print("""graph is not Eulerian""" ) print("""no path""" ) return snake_case_ = 1 if check == 2: snake_case_ = odd_node print("""graph has a Euler path""" ) if check == 1: print("""graph has a Euler cycle""" ) snake_case_ = dfs(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) print(_SCREAMING_SNAKE_CASE ) def _a ( ) -> Any: snake_case_ = {1: [2, 3, 4], 2: [1, 3], 3: [1, 2], 4: [1, 5], 5: [4]} snake_case_ = {1: [2, 3, 4, 5], 2: [1, 3], 3: [1, 2], 4: [1, 5], 5: [1, 4]} snake_case_ = {1: [2, 3, 4], 2: [1, 3, 4], 3: [1, 2], 4: [1, 2, 5], 5: [4]} snake_case_ = {1: [2, 3], 2: [1, 3], 3: [1, 2]} snake_case_ = { 1: [], 2: [] # all degree is zero } snake_case_ = 10 check_euler(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) check_euler(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) check_euler(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) check_euler(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) check_euler(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) if __name__ == "__main__": main()
2
"""simple docstring""" from __future__ import annotations from collections import deque class __A : '''simple docstring''' def __init__( self : List[Any] , UpperCAmelCase_ : list[str] ) ->List[Any]: """simple docstring""" snake_case_ = [] self.adlist.append( {"""value""": """""", """next_states""": [], """fail_state""": 0, """output""": []} ) for keyword in keywords: self.add_keyword(UpperCAmelCase_ ) self.set_fail_transitions() def lowerCAmelCase ( self : int , UpperCAmelCase_ : int , UpperCAmelCase_ : str ) ->int | None: """simple docstring""" for state in self.adlist[current_state]["next_states"]: if char == self.adlist[state]["value"]: return state return None def lowerCAmelCase ( self : int , UpperCAmelCase_ : str ) ->None: """simple docstring""" snake_case_ = 0 for character in keyword: snake_case_ = self.find_next_state(UpperCAmelCase_ , UpperCAmelCase_ ) if next_state is None: self.adlist.append( { """value""": character, """next_states""": [], """fail_state""": 0, """output""": [], } ) self.adlist[current_state]["next_states"].append(len(self.adlist ) - 1 ) snake_case_ = len(self.adlist ) - 1 else: snake_case_ = next_state self.adlist[current_state]["output"].append(UpperCAmelCase_ ) def lowerCAmelCase ( self : Optional[Any] ) ->None: """simple docstring""" snake_case_ = deque() for node in self.adlist[0]["next_states"]: q.append(UpperCAmelCase_ ) snake_case_ = 0 while q: snake_case_ = q.popleft() for child in self.adlist[r]["next_states"]: q.append(UpperCAmelCase_ ) snake_case_ = self.adlist[r]["""fail_state"""] while ( self.find_next_state(UpperCAmelCase_ , self.adlist[child]["""value"""] ) is None and state != 0 ): snake_case_ = self.adlist[state]["""fail_state"""] snake_case_ = self.find_next_state( UpperCAmelCase_ , self.adlist[child]["""value"""] ) if self.adlist[child]["fail_state"] is None: snake_case_ = 0 snake_case_ = ( self.adlist[child]["""output"""] + self.adlist[self.adlist[child]["""fail_state"""]]["""output"""] ) def lowerCAmelCase ( self : Union[str, Any] , UpperCAmelCase_ : str ) ->dict[str, list[int]]: """simple docstring""" snake_case_ = {} # returns a dict with keywords and list of its occurrences snake_case_ = 0 for i in range(len(UpperCAmelCase_ ) ): while ( self.find_next_state(UpperCAmelCase_ , string[i] ) is None and current_state != 0 ): snake_case_ = self.adlist[current_state]["""fail_state"""] snake_case_ = self.find_next_state(UpperCAmelCase_ , string[i] ) if next_state is None: snake_case_ = 0 else: snake_case_ = next_state for key in self.adlist[current_state]["output"]: if key not in result: snake_case_ = [] result[key].append(i - len(UpperCAmelCase_ ) + 1 ) return result if __name__ == "__main__": import doctest doctest.testmod()
2
1
"""simple docstring""" from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available, is_vision_available __SCREAMING_SNAKE_CASE : Optional[int] = { 'configuration_conditional_detr': [ 'CONDITIONAL_DETR_PRETRAINED_CONFIG_ARCHIVE_MAP', 'ConditionalDetrConfig', 'ConditionalDetrOnnxConfig', ] } try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __SCREAMING_SNAKE_CASE : Union[str, Any] = ['ConditionalDetrFeatureExtractor'] __SCREAMING_SNAKE_CASE : Dict = ['ConditionalDetrImageProcessor'] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __SCREAMING_SNAKE_CASE : Tuple = [ 'CONDITIONAL_DETR_PRETRAINED_MODEL_ARCHIVE_LIST', 'ConditionalDetrForObjectDetection', 'ConditionalDetrForSegmentation', 'ConditionalDetrModel', 'ConditionalDetrPreTrainedModel', ] if TYPE_CHECKING: from .configuration_conditional_detr import ( CONDITIONAL_DETR_PRETRAINED_CONFIG_ARCHIVE_MAP, ConditionalDetrConfig, ConditionalDetrOnnxConfig, ) try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .feature_extraction_conditional_detr import ConditionalDetrFeatureExtractor from .image_processing_conditional_detr import ConditionalDetrImageProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_conditional_detr import ( CONDITIONAL_DETR_PRETRAINED_MODEL_ARCHIVE_LIST, ConditionalDetrForObjectDetection, ConditionalDetrForSegmentation, ConditionalDetrModel, ConditionalDetrPreTrainedModel, ) else: import sys __SCREAMING_SNAKE_CASE : Union[str, Any] = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
2
"""simple docstring""" import inspect import unittest from transformers import ConvNextVaConfig from transformers.models.auto import get_values from transformers.models.auto.modeling_auto import MODEL_FOR_BACKBONE_MAPPING_NAMES, MODEL_MAPPING_NAMES from transformers.testing_utils import require_torch, require_vision, slow, torch_device from transformers.utils import cached_property, is_torch_available, is_vision_available from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from transformers import ConvNextVaBackbone, ConvNextVaForImageClassification, ConvNextVaModel from transformers.models.convnextva.modeling_convnextva import CONVNEXTV2_PRETRAINED_MODEL_ARCHIVE_LIST if is_vision_available(): from PIL import Image from transformers import AutoImageProcessor class __A : '''simple docstring''' def __init__( self : Optional[int] , UpperCAmelCase_ : Tuple , UpperCAmelCase_ : Optional[int]=13 , UpperCAmelCase_ : str=32 , UpperCAmelCase_ : List[Any]=3 , UpperCAmelCase_ : Optional[Any]=4 , UpperCAmelCase_ : Dict=[10, 20, 30, 40] , UpperCAmelCase_ : List[Any]=[2, 2, 3, 2] , UpperCAmelCase_ : Tuple=True , UpperCAmelCase_ : Tuple=True , UpperCAmelCase_ : Any=37 , UpperCAmelCase_ : str="gelu" , UpperCAmelCase_ : Optional[int]=10 , UpperCAmelCase_ : Dict=0.02 , UpperCAmelCase_ : int=["stage2", "stage3", "stage4"] , UpperCAmelCase_ : Optional[int]=[2, 3, 4] , UpperCAmelCase_ : List[str]=None , ) ->Union[str, Any]: """simple docstring""" snake_case_ = parent snake_case_ = batch_size snake_case_ = image_size snake_case_ = num_channels snake_case_ = num_stages snake_case_ = hidden_sizes snake_case_ = depths snake_case_ = is_training snake_case_ = use_labels snake_case_ = intermediate_size snake_case_ = hidden_act snake_case_ = num_labels snake_case_ = initializer_range snake_case_ = out_features snake_case_ = out_indices snake_case_ = scope def lowerCAmelCase ( self : List[str] ) ->str: """simple docstring""" snake_case_ = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) snake_case_ = None if self.use_labels: snake_case_ = ids_tensor([self.batch_size] , self.num_labels ) snake_case_ = self.get_config() return config, pixel_values, labels def lowerCAmelCase ( self : Dict ) ->Optional[int]: """simple docstring""" return ConvNextVaConfig( num_channels=self.num_channels , hidden_sizes=self.hidden_sizes , depths=self.depths , num_stages=self.num_stages , hidden_act=self.hidden_act , is_decoder=UpperCAmelCase_ , initializer_range=self.initializer_range , out_features=self.out_features , out_indices=self.out_indices , num_labels=self.num_labels , ) def lowerCAmelCase ( self : List[Any] , UpperCAmelCase_ : List[Any] , UpperCAmelCase_ : List[str] , UpperCAmelCase_ : Optional[Any] ) ->List[Any]: """simple docstring""" snake_case_ = ConvNextVaModel(config=UpperCAmelCase_ ) model.to(UpperCAmelCase_ ) model.eval() snake_case_ = model(UpperCAmelCase_ ) # expected last hidden states: B, C, H // 32, W // 32 self.parent.assertEqual( result.last_hidden_state.shape , (self.batch_size, self.hidden_sizes[-1], self.image_size // 32, self.image_size // 32) , ) def lowerCAmelCase ( self : List[str] , UpperCAmelCase_ : Tuple , UpperCAmelCase_ : str , UpperCAmelCase_ : Optional[Any] ) ->Any: """simple docstring""" snake_case_ = ConvNextVaForImageClassification(UpperCAmelCase_ ) model.to(UpperCAmelCase_ ) model.eval() snake_case_ = model(UpperCAmelCase_ , labels=UpperCAmelCase_ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_labels) ) def lowerCAmelCase ( self : Optional[Any] , UpperCAmelCase_ : Tuple , UpperCAmelCase_ : Optional[Any] , UpperCAmelCase_ : Union[str, Any] ) ->Tuple: """simple docstring""" snake_case_ = ConvNextVaBackbone(config=UpperCAmelCase_ ) model.to(UpperCAmelCase_ ) model.eval() snake_case_ = model(UpperCAmelCase_ ) # verify hidden states self.parent.assertEqual(len(result.feature_maps ) , len(config.out_features ) ) self.parent.assertListEqual(list(result.feature_maps[0].shape ) , [self.batch_size, self.hidden_sizes[1], 4, 4] ) # verify channels self.parent.assertEqual(len(model.channels ) , len(config.out_features ) ) self.parent.assertListEqual(model.channels , config.hidden_sizes[1:] ) # verify backbone works with out_features=None snake_case_ = None snake_case_ = ConvNextVaBackbone(config=UpperCAmelCase_ ) model.to(UpperCAmelCase_ ) model.eval() snake_case_ = model(UpperCAmelCase_ ) # verify feature maps self.parent.assertEqual(len(result.feature_maps ) , 1 ) self.parent.assertListEqual(list(result.feature_maps[0].shape ) , [self.batch_size, self.hidden_sizes[-1], 1, 1] ) # verify channels self.parent.assertEqual(len(model.channels ) , 1 ) self.parent.assertListEqual(model.channels , [config.hidden_sizes[-1]] ) def lowerCAmelCase ( self : Optional[int] ) ->List[str]: """simple docstring""" snake_case_ = self.prepare_config_and_inputs() snake_case_ , snake_case_ , snake_case_ = config_and_inputs snake_case_ = {"""pixel_values""": pixel_values} return config, inputs_dict def lowerCAmelCase ( self : List[str] ) ->Union[str, Any]: """simple docstring""" snake_case_ = self.prepare_config_and_inputs() snake_case_ , snake_case_ , snake_case_ = config_and_inputs snake_case_ = {"""pixel_values""": pixel_values, """labels""": labels} return config, inputs_dict @require_torch class __A (snake_case__ , snake_case__ , unittest.TestCase): '''simple docstring''' __lowercase: Optional[Any] = ( ( ConvNextVaModel, ConvNextVaForImageClassification, ConvNextVaBackbone, ) if is_torch_available() else () ) __lowercase: Union[str, Any] = ( {"""feature-extraction""": ConvNextVaModel, """image-classification""": ConvNextVaForImageClassification} if is_torch_available() else {} ) __lowercase: Union[str, Any] = False __lowercase: Optional[Any] = False __lowercase: Any = False __lowercase: Union[str, Any] = False __lowercase: Dict = False def lowerCAmelCase ( self : Union[str, Any] ) ->Tuple: """simple docstring""" snake_case_ = ConvNextVaModelTester(self ) snake_case_ = ConfigTester(self , config_class=UpperCAmelCase_ , has_text_modality=UpperCAmelCase_ , hidden_size=37 ) def lowerCAmelCase ( self : List[Any] ) ->Optional[Any]: """simple docstring""" self.create_and_test_config_common_properties() self.config_tester.create_and_test_config_to_json_string() self.config_tester.create_and_test_config_to_json_file() self.config_tester.create_and_test_config_from_and_save_pretrained() self.config_tester.create_and_test_config_with_num_labels() self.config_tester.check_config_can_be_init_without_params() self.config_tester.check_config_arguments_init() def lowerCAmelCase ( self : str ) ->Optional[Any]: """simple docstring""" return @unittest.skip(reason="""ConvNextV2 does not use inputs_embeds""" ) def lowerCAmelCase ( self : Optional[int] ) ->Union[str, Any]: """simple docstring""" pass @unittest.skip(reason="""ConvNextV2 does not support input and output embeddings""" ) def lowerCAmelCase ( self : Optional[Any] ) ->List[str]: """simple docstring""" pass @unittest.skip(reason="""ConvNextV2 does not use feedforward chunking""" ) def lowerCAmelCase ( self : Optional[int] ) ->List[str]: """simple docstring""" pass def lowerCAmelCase ( self : Dict ) ->Optional[int]: """simple docstring""" if not self.model_tester.is_training: return for model_class in self.all_model_classes: snake_case_ , snake_case_ = self.model_tester.prepare_config_and_inputs_with_labels() snake_case_ = True if model_class.__name__ in [ *get_values(UpperCAmelCase_ ), *get_values(UpperCAmelCase_ ), ]: continue snake_case_ = model_class(UpperCAmelCase_ ) model.to(UpperCAmelCase_ ) model.train() snake_case_ = self._prepare_for_class(UpperCAmelCase_ , UpperCAmelCase_ , return_labels=UpperCAmelCase_ ) snake_case_ = model(**UpperCAmelCase_ ).loss loss.backward() def lowerCAmelCase ( self : Optional[int] ) ->Any: """simple docstring""" if not self.model_tester.is_training: return for model_class in self.all_model_classes: snake_case_ , snake_case_ = self.model_tester.prepare_config_and_inputs_with_labels() snake_case_ = False snake_case_ = True if ( model_class.__name__ in [*get_values(UpperCAmelCase_ ), *get_values(UpperCAmelCase_ )] or not model_class.supports_gradient_checkpointing ): continue snake_case_ = model_class(UpperCAmelCase_ ) model.to(UpperCAmelCase_ ) model.gradient_checkpointing_enable() model.train() snake_case_ = self._prepare_for_class(UpperCAmelCase_ , UpperCAmelCase_ , return_labels=UpperCAmelCase_ ) snake_case_ = model(**UpperCAmelCase_ ).loss loss.backward() def lowerCAmelCase ( self : List[Any] ) ->Union[str, Any]: """simple docstring""" snake_case_ , snake_case_ = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: snake_case_ = model_class(UpperCAmelCase_ ) snake_case_ = inspect.signature(model.forward ) # signature.parameters is an OrderedDict => so arg_names order is deterministic snake_case_ = [*signature.parameters.keys()] snake_case_ = ["""pixel_values"""] self.assertListEqual(arg_names[:1] , UpperCAmelCase_ ) def lowerCAmelCase ( self : Optional[int] ) ->Union[str, Any]: """simple docstring""" snake_case_ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*UpperCAmelCase_ ) def lowerCAmelCase ( self : Optional[Any] ) ->Dict: """simple docstring""" def check_hidden_states_output(UpperCAmelCase_ : int , UpperCAmelCase_ : Optional[Any] , UpperCAmelCase_ : str ): snake_case_ = model_class(UpperCAmelCase_ ) model.to(UpperCAmelCase_ ) model.eval() with torch.no_grad(): snake_case_ = model(**self._prepare_for_class(UpperCAmelCase_ , UpperCAmelCase_ ) ) snake_case_ = outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states snake_case_ = self.model_tester.num_stages self.assertEqual(len(UpperCAmelCase_ ) , expected_num_stages + 1 ) # ConvNextV2's feature maps are of shape (batch_size, num_channels, height, width) self.assertListEqual( list(hidden_states[0].shape[-2:] ) , [self.model_tester.image_size // 4, self.model_tester.image_size // 4] , ) snake_case_ , snake_case_ = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: snake_case_ = True check_hidden_states_output(UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ ) # check that output_hidden_states also work using config del inputs_dict["output_hidden_states"] snake_case_ = True check_hidden_states_output(UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ ) def lowerCAmelCase ( self : Union[str, Any] ) ->Dict: """simple docstring""" snake_case_ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_image_classification(*UpperCAmelCase_ ) @slow def lowerCAmelCase ( self : Tuple ) ->str: """simple docstring""" for model_name in CONVNEXTV2_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: snake_case_ = ConvNextVaModel.from_pretrained(UpperCAmelCase_ ) self.assertIsNotNone(UpperCAmelCase_ ) def _a ( ) -> str: snake_case_ = Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png""" ) return image @require_torch @require_vision class __A (unittest.TestCase): '''simple docstring''' @cached_property def lowerCAmelCase ( self : Union[str, Any] ) ->Optional[int]: """simple docstring""" return AutoImageProcessor.from_pretrained("""facebook/convnextv2-tiny-1k-224""" ) if is_vision_available() else None @slow def lowerCAmelCase ( self : Tuple ) ->int: """simple docstring""" snake_case_ = ConvNextVaForImageClassification.from_pretrained("""facebook/convnextv2-tiny-1k-224""" ).to(UpperCAmelCase_ ) snake_case_ = self.default_image_processor snake_case_ = prepare_img() snake_case_ = preprocessor(images=UpperCAmelCase_ , return_tensors="""pt""" ).to(UpperCAmelCase_ ) # forward pass with torch.no_grad(): snake_case_ = model(**UpperCAmelCase_ ) # verify the logits snake_case_ = torch.Size((1, 1_000) ) self.assertEqual(outputs.logits.shape , UpperCAmelCase_ ) snake_case_ = torch.tensor([0.9_996, 0.1_966, -0.4_386] ).to(UpperCAmelCase_ ) self.assertTrue(torch.allclose(outputs.logits[0, :3] , UpperCAmelCase_ , atol=1E-4 ) )
2
1
"""simple docstring""" import os import sys from contextlib import contextmanager # Windows only if os.name == "nt": import ctypes import msvcrt # noqa class __A (ctypes.Structure): '''simple docstring''' __lowercase: Optional[Any] = [("""size""", ctypes.c_int), ("""visible""", ctypes.c_byte)] def _a ( ) -> str: if os.name == "nt": snake_case_ = CursorInfo() snake_case_ = ctypes.windll.kernelaa.GetStdHandle(-11 ) ctypes.windll.kernelaa.GetConsoleCursorInfo(_SCREAMING_SNAKE_CASE , ctypes.byref(_SCREAMING_SNAKE_CASE ) ) snake_case_ = False ctypes.windll.kernelaa.SetConsoleCursorInfo(_SCREAMING_SNAKE_CASE , ctypes.byref(_SCREAMING_SNAKE_CASE ) ) elif os.name == "posix": sys.stdout.write("""\033[?25l""" ) sys.stdout.flush() def _a ( ) -> List[Any]: if os.name == "nt": snake_case_ = CursorInfo() snake_case_ = ctypes.windll.kernelaa.GetStdHandle(-11 ) ctypes.windll.kernelaa.GetConsoleCursorInfo(_SCREAMING_SNAKE_CASE , ctypes.byref(_SCREAMING_SNAKE_CASE ) ) snake_case_ = True ctypes.windll.kernelaa.SetConsoleCursorInfo(_SCREAMING_SNAKE_CASE , ctypes.byref(_SCREAMING_SNAKE_CASE ) ) elif os.name == "posix": sys.stdout.write("""\033[?25h""" ) sys.stdout.flush() @contextmanager def _a ( ) -> List[str]: try: hide_cursor() yield finally: show_cursor()
2
"""simple docstring""" import argparse from pathlib import Path from typing import Dict, OrderedDict, Tuple import torch from audiocraft.models import MusicGen from transformers import ( AutoFeatureExtractor, AutoTokenizer, EncodecModel, MusicgenDecoderConfig, MusicgenForConditionalGeneration, MusicgenProcessor, TaEncoderModel, ) from transformers.models.musicgen.modeling_musicgen import MusicgenForCausalLM from transformers.utils import logging logging.set_verbosity_info() __SCREAMING_SNAKE_CASE : Union[str, Any] = logging.get_logger(__name__) __SCREAMING_SNAKE_CASE : int = ['model.decoder.embed_positions.weights'] def _a ( _SCREAMING_SNAKE_CASE ) -> str: if "emb" in name: snake_case_ = name.replace("""emb""" , """model.decoder.embed_tokens""" ) if "transformer" in name: snake_case_ = name.replace("""transformer""" , """model.decoder""" ) if "cross_attention" in name: snake_case_ = name.replace("""cross_attention""" , """encoder_attn""" ) if "linear1" in name: snake_case_ = name.replace("""linear1""" , """fc1""" ) if "linear2" in name: snake_case_ = name.replace("""linear2""" , """fc2""" ) if "norm1" in name: snake_case_ = name.replace("""norm1""" , """self_attn_layer_norm""" ) if "norm_cross" in name: snake_case_ = name.replace("""norm_cross""" , """encoder_attn_layer_norm""" ) if "norm2" in name: snake_case_ = name.replace("""norm2""" , """final_layer_norm""" ) if "out_norm" in name: snake_case_ = name.replace("""out_norm""" , """model.decoder.layer_norm""" ) if "linears" in name: snake_case_ = name.replace("""linears""" , """lm_heads""" ) if "condition_provider.conditioners.description.output_proj" in name: snake_case_ = name.replace("""condition_provider.conditioners.description.output_proj""" , """enc_to_dec_proj""" ) return name def _a ( _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> Tuple[Dict, Dict]: snake_case_ = list(state_dict.keys() ) snake_case_ = {} for key in keys: snake_case_ = state_dict.pop(_SCREAMING_SNAKE_CASE ) snake_case_ = rename_keys(_SCREAMING_SNAKE_CASE ) if "in_proj_weight" in key: # split fused qkv proj snake_case_ = val[:hidden_size, :] snake_case_ = val[hidden_size : 2 * hidden_size, :] snake_case_ = val[-hidden_size:, :] elif "enc_to_dec_proj" in key: snake_case_ = val else: snake_case_ = val return state_dict, enc_dec_proj_state_dict def _a ( _SCREAMING_SNAKE_CASE ) -> MusicgenDecoderConfig: if checkpoint == "small": # default config values snake_case_ = 1_024 snake_case_ = 24 snake_case_ = 16 elif checkpoint == "medium": snake_case_ = 1_536 snake_case_ = 48 snake_case_ = 24 elif checkpoint == "large": snake_case_ = 2_048 snake_case_ = 48 snake_case_ = 32 else: raise ValueError(f"""Checkpoint should be one of `['small', 'medium', 'large']`, got {checkpoint}.""" ) snake_case_ = MusicgenDecoderConfig( hidden_size=_SCREAMING_SNAKE_CASE , ffn_dim=hidden_size * 4 , num_hidden_layers=_SCREAMING_SNAKE_CASE , num_attention_heads=_SCREAMING_SNAKE_CASE , ) return config @torch.no_grad() def _a ( _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE=None , _SCREAMING_SNAKE_CASE=None , _SCREAMING_SNAKE_CASE="cpu" ) -> Tuple: snake_case_ = MusicGen.get_pretrained(_SCREAMING_SNAKE_CASE , device=_SCREAMING_SNAKE_CASE ) snake_case_ = decoder_config_from_checkpoint(_SCREAMING_SNAKE_CASE ) snake_case_ = fairseq_model.lm.state_dict() snake_case_ , snake_case_ = rename_state_dict( _SCREAMING_SNAKE_CASE , hidden_size=decoder_config.hidden_size ) snake_case_ = TaEncoderModel.from_pretrained("""t5-base""" ) snake_case_ = EncodecModel.from_pretrained("""facebook/encodec_32khz""" ) snake_case_ = MusicgenForCausalLM(_SCREAMING_SNAKE_CASE ).eval() # load all decoder weights - expect that we'll be missing embeddings and enc-dec projection snake_case_ , snake_case_ = decoder.load_state_dict(_SCREAMING_SNAKE_CASE , strict=_SCREAMING_SNAKE_CASE ) for key in missing_keys.copy(): if key.startswith(("""text_encoder""", """audio_encoder""") ) or key in EXPECTED_MISSING_KEYS: missing_keys.remove(_SCREAMING_SNAKE_CASE ) if len(_SCREAMING_SNAKE_CASE ) > 0: raise ValueError(f"""Missing key(s) in state_dict: {missing_keys}""" ) if len(_SCREAMING_SNAKE_CASE ) > 0: raise ValueError(f"""Unexpected key(s) in state_dict: {unexpected_keys}""" ) # init the composite model snake_case_ = MusicgenForConditionalGeneration(text_encoder=_SCREAMING_SNAKE_CASE , audio_encoder=_SCREAMING_SNAKE_CASE , decoder=_SCREAMING_SNAKE_CASE ) # load the pre-trained enc-dec projection (from the decoder state dict) model.enc_to_dec_proj.load_state_dict(_SCREAMING_SNAKE_CASE ) # check we can do a forward pass snake_case_ = torch.arange(0 , 8 , dtype=torch.long ).reshape(2 , -1 ) snake_case_ = input_ids.reshape(2 * 4 , -1 ) with torch.no_grad(): snake_case_ = model(input_ids=_SCREAMING_SNAKE_CASE , decoder_input_ids=_SCREAMING_SNAKE_CASE ).logits if logits.shape != (8, 1, 2_048): raise ValueError("""Incorrect shape for logits""" ) # now construct the processor snake_case_ = AutoTokenizer.from_pretrained("""t5-base""" ) snake_case_ = AutoFeatureExtractor.from_pretrained("""facebook/encodec_32khz""" , padding_side="""left""" ) snake_case_ = MusicgenProcessor(feature_extractor=_SCREAMING_SNAKE_CASE , tokenizer=_SCREAMING_SNAKE_CASE ) # set the appropriate bos/pad token ids snake_case_ = 2_048 snake_case_ = 2_048 # set other default generation config params snake_case_ = int(30 * audio_encoder.config.frame_rate ) snake_case_ = True snake_case_ = 3.0 if pytorch_dump_folder is not None: Path(_SCREAMING_SNAKE_CASE ).mkdir(exist_ok=_SCREAMING_SNAKE_CASE ) logger.info(f"""Saving model {checkpoint} to {pytorch_dump_folder}""" ) model.save_pretrained(_SCREAMING_SNAKE_CASE ) processor.save_pretrained(_SCREAMING_SNAKE_CASE ) if repo_id: logger.info(f"""Pushing model {checkpoint} to {repo_id}""" ) model.push_to_hub(_SCREAMING_SNAKE_CASE ) processor.push_to_hub(_SCREAMING_SNAKE_CASE ) if __name__ == "__main__": __SCREAMING_SNAKE_CASE : Tuple = argparse.ArgumentParser() # Required parameters parser.add_argument( '--checkpoint', default='small', type=str, help='Checkpoint size of the MusicGen model you\'d like to convert. Can be one of: `[\'small\', \'medium\', \'large\']`.', ) parser.add_argument( '--pytorch_dump_folder', required=True, default=None, type=str, help='Path to the output PyTorch model directory.', ) parser.add_argument( '--push_to_hub', default=None, type=str, help='Where to upload the converted model on the 🤗 hub.' ) parser.add_argument( '--device', default='cpu', type=str, help='Torch device to run the conversion, either cpu or cuda.' ) __SCREAMING_SNAKE_CASE : int = parser.parse_args() convert_musicgen_checkpoint(args.checkpoint, args.pytorch_dump_folder, args.push_to_hub)
2
1
"""simple docstring""" from dataclasses import dataclass from typing import Optional, Tuple, Union import torch import torch.nn as nn from ..configuration_utils import ConfigMixin, register_to_config from ..utils import BaseOutput from .embeddings import GaussianFourierProjection, TimestepEmbedding, Timesteps from .modeling_utils import ModelMixin from .unet_ad_blocks import get_down_block, get_mid_block, get_out_block, get_up_block @dataclass class __A (snake_case__): '''simple docstring''' __lowercase: torch.FloatTensor class __A (snake_case__ , snake_case__): '''simple docstring''' @register_to_config def __init__( self : Tuple , UpperCAmelCase_ : int = 65_536 , UpperCAmelCase_ : Optional[int] = None , UpperCAmelCase_ : int = 2 , UpperCAmelCase_ : int = 2 , UpperCAmelCase_ : int = 0 , UpperCAmelCase_ : str = "fourier" , UpperCAmelCase_ : bool = True , UpperCAmelCase_ : bool = False , UpperCAmelCase_ : float = 0.0 , UpperCAmelCase_ : Tuple[str] = ("DownBlock1DNoSkip", "DownBlock1D", "AttnDownBlock1D") , UpperCAmelCase_ : Tuple[str] = ("AttnUpBlock1D", "UpBlock1D", "UpBlock1DNoSkip") , UpperCAmelCase_ : Tuple[str] = "UNetMidBlock1D" , UpperCAmelCase_ : str = None , UpperCAmelCase_ : Tuple[int] = (32, 32, 64) , UpperCAmelCase_ : str = None , UpperCAmelCase_ : int = 8 , UpperCAmelCase_ : int = 1 , UpperCAmelCase_ : bool = False , ) ->Dict: """simple docstring""" super().__init__() snake_case_ = sample_size # time if time_embedding_type == "fourier": snake_case_ = GaussianFourierProjection( embedding_size=8 , set_W_to_weight=UpperCAmelCase_ , log=UpperCAmelCase_ , flip_sin_to_cos=UpperCAmelCase_ ) snake_case_ = 2 * block_out_channels[0] elif time_embedding_type == "positional": snake_case_ = Timesteps( block_out_channels[0] , flip_sin_to_cos=UpperCAmelCase_ , downscale_freq_shift=UpperCAmelCase_ ) snake_case_ = block_out_channels[0] if use_timestep_embedding: snake_case_ = block_out_channels[0] * 4 snake_case_ = TimestepEmbedding( in_channels=UpperCAmelCase_ , time_embed_dim=UpperCAmelCase_ , act_fn=UpperCAmelCase_ , out_dim=block_out_channels[0] , ) snake_case_ = nn.ModuleList([] ) snake_case_ = None snake_case_ = nn.ModuleList([] ) snake_case_ = None # down snake_case_ = in_channels for i, down_block_type in enumerate(UpperCAmelCase_ ): snake_case_ = output_channel snake_case_ = block_out_channels[i] if i == 0: input_channel += extra_in_channels snake_case_ = i == len(UpperCAmelCase_ ) - 1 snake_case_ = get_down_block( UpperCAmelCase_ , num_layers=UpperCAmelCase_ , in_channels=UpperCAmelCase_ , out_channels=UpperCAmelCase_ , temb_channels=block_out_channels[0] , add_downsample=not is_final_block or downsample_each_block , ) self.down_blocks.append(UpperCAmelCase_ ) # mid snake_case_ = get_mid_block( UpperCAmelCase_ , in_channels=block_out_channels[-1] , mid_channels=block_out_channels[-1] , out_channels=block_out_channels[-1] , embed_dim=block_out_channels[0] , num_layers=UpperCAmelCase_ , add_downsample=UpperCAmelCase_ , ) # up snake_case_ = list(reversed(UpperCAmelCase_ ) ) snake_case_ = reversed_block_out_channels[0] if out_block_type is None: snake_case_ = out_channels else: snake_case_ = block_out_channels[0] for i, up_block_type in enumerate(UpperCAmelCase_ ): snake_case_ = output_channel snake_case_ = ( reversed_block_out_channels[i + 1] if i < len(UpperCAmelCase_ ) - 1 else final_upsample_channels ) snake_case_ = i == len(UpperCAmelCase_ ) - 1 snake_case_ = get_up_block( UpperCAmelCase_ , num_layers=UpperCAmelCase_ , in_channels=UpperCAmelCase_ , out_channels=UpperCAmelCase_ , temb_channels=block_out_channels[0] , add_upsample=not is_final_block , ) self.up_blocks.append(UpperCAmelCase_ ) snake_case_ = output_channel # out snake_case_ = norm_num_groups if norm_num_groups is not None else min(block_out_channels[0] // 4 , 32 ) snake_case_ = get_out_block( out_block_type=UpperCAmelCase_ , num_groups_out=UpperCAmelCase_ , embed_dim=block_out_channels[0] , out_channels=UpperCAmelCase_ , act_fn=UpperCAmelCase_ , fc_dim=block_out_channels[-1] // 4 , ) def lowerCAmelCase ( self : Any , UpperCAmelCase_ : torch.FloatTensor , UpperCAmelCase_ : Union[torch.Tensor, float, int] , UpperCAmelCase_ : bool = True , ) ->Union[UNetaDOutput, Tuple]: """simple docstring""" snake_case_ = timestep if not torch.is_tensor(UpperCAmelCase_ ): snake_case_ = torch.tensor([timesteps] , dtype=torch.long , device=sample.device ) elif torch.is_tensor(UpperCAmelCase_ ) and len(timesteps.shape ) == 0: snake_case_ = timesteps[None].to(sample.device ) snake_case_ = self.time_proj(UpperCAmelCase_ ) if self.config.use_timestep_embedding: snake_case_ = self.time_mlp(UpperCAmelCase_ ) else: snake_case_ = timestep_embed[..., None] snake_case_ = timestep_embed.repeat([1, 1, sample.shape[2]] ).to(sample.dtype ) snake_case_ = timestep_embed.broadcast_to((sample.shape[:1] + timestep_embed.shape[1:]) ) # 2. down snake_case_ = () for downsample_block in self.down_blocks: snake_case_ , snake_case_ = downsample_block(hidden_states=UpperCAmelCase_ , temb=UpperCAmelCase_ ) down_block_res_samples += res_samples # 3. mid if self.mid_block: snake_case_ = self.mid_block(UpperCAmelCase_ , UpperCAmelCase_ ) # 4. up for i, upsample_block in enumerate(self.up_blocks ): snake_case_ = down_block_res_samples[-1:] snake_case_ = down_block_res_samples[:-1] snake_case_ = upsample_block(UpperCAmelCase_ , res_hidden_states_tuple=UpperCAmelCase_ , temb=UpperCAmelCase_ ) # 5. post-process if self.out_block: snake_case_ = self.out_block(UpperCAmelCase_ , UpperCAmelCase_ ) if not return_dict: return (sample,) return UNetaDOutput(sample=UpperCAmelCase_ )
2
"""simple docstring""" def _a ( _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> int: if index == number_of_items: return 0 snake_case_ = 0 snake_case_ = 0 snake_case_ = knapsack(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , index + 1 ) if weights[index] <= max_weight: snake_case_ = values[index] + knapsack( _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , max_weight - weights[index] , index + 1 ) return max(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) if __name__ == "__main__": import doctest doctest.testmod()
2
1
"""simple docstring""" import itertools import os from collections import Counter, defaultdict from concurrent.futures import ThreadPoolExecutor, as_completed import numpy as np import datasets from .execute import check_correctness __SCREAMING_SNAKE_CASE : List[str] = '\\n@misc{chen2021evaluating,\n title={Evaluating Large Language Models Trained on Code},\n author={Mark Chen and Jerry Tworek and Heewoo Jun and Qiming Yuan \\nand Henrique Ponde de Oliveira Pinto and Jared Kaplan and Harri Edwards \\nand Yuri Burda and Nicholas Joseph and Greg Brockman and Alex Ray \\nand Raul Puri and Gretchen Krueger and Michael Petrov and Heidy Khlaaf \\nand Girish Sastry and Pamela Mishkin and Brooke Chan and Scott Gray \\nand Nick Ryder and Mikhail Pavlov and Alethea Power and Lukasz Kaiser \\nand Mohammad Bavarian and Clemens Winter and Philippe Tillet \\nand Felipe Petroski Such and Dave Cummings and Matthias Plappert \\nand Fotios Chantzis and Elizabeth Barnes and Ariel Herbert-Voss \\nand William Hebgen Guss and Alex Nichol and Alex Paino and Nikolas Tezak \\nand Jie Tang and Igor Babuschkin and Suchir Balaji and Shantanu Jain \\nand William Saunders and Christopher Hesse and Andrew N. Carr \\nand Jan Leike and Josh Achiam and Vedant Misra and Evan Morikawa \\nand Alec Radford and Matthew Knight and Miles Brundage and Mira Murati \\nand Katie Mayer and Peter Welinder and Bob McGrew and Dario Amodei \\nand Sam McCandlish and Ilya Sutskever and Wojciech Zaremba},\n year={2021},\n eprint={2107.03374},\n archivePrefix={arXiv},\n primaryClass={cs.LG}\n}\n' __SCREAMING_SNAKE_CASE : List[str] = '\\nThis metric implements the evaluation harness for the HumanEval problem solving dataset\ndescribed in the paper "Evaluating Large Language Models Trained on Code"\n(https://arxiv.org/abs/2107.03374).\n' __SCREAMING_SNAKE_CASE : Optional[Any] = '\nCalculates how good are predictions given some references, using certain scores\nArgs:\n predictions: list of candidates to evaluate. Each candidates should be a list\n of strings with several code candidates to solve the problem.\n references: a list with a test for each prediction. Each test should evaluate the\n correctness of a code candidate.\n k: number of code candidates to consider in the evaluation (Default: [1, 10, 100])\n num_workers: number of workers used to evaluate the canidate programs (Default: 4).\n timeout:\nReturns:\n pass_at_k: dict with pass rates for each k\n results: dict with granular results of each unittest\nExamples:\n >>> code_eval = datasets.load_metric("code_eval")\n >>> test_cases = ["assert add(2,3)==5"]\n >>> candidates = [["def add(a,b): return a*b", "def add(a, b): return a+b"]]\n >>> pass_at_k, results = code_eval.compute(references=test_cases, predictions=candidates, k=[1, 2])\n >>> print(pass_at_k)\n {\'pass@1\': 0.5, \'pass@2\': 1.0}\n' __SCREAMING_SNAKE_CASE : int = '\n################################################################################\n !!!WARNING!!!\n################################################################################\nThe "code_eval" metric executes untrusted model-generated code in Python.\nAlthough it is highly unlikely that model-generated code will do something\novertly malicious in response to this test suite, model-generated code may act\ndestructively due to a lack of model capability or alignment.\nUsers are strongly encouraged to sandbox this evaluation suite so that it\ndoes not perform destructive actions on their host or network. For more\ninformation on how OpenAI sandboxes its code, see the paper "Evaluating Large\nLanguage Models Trained on Code" (https://arxiv.org/abs/2107.03374).\n\nOnce you have read this disclaimer and taken appropriate precautions,\nset the environment variable HF_ALLOW_CODE_EVAL="1". Within Python you can to this\nwith:\n\n>>> import os\n>>> os.environ["HF_ALLOW_CODE_EVAL"] = "1"\n\n################################################################################\\n' __SCREAMING_SNAKE_CASE : List[str] = 'The MIT License\n\nCopyright (c) OpenAI (https://openai.com)\n\nPermission is hereby granted, free of charge, to any person obtaining a copy\nof this software and associated documentation files (the "Software"), to deal\nin the Software without restriction, including without limitation the rights\nto use, copy, modify, merge, publish, distribute, sublicense, and/or sell\ncopies of the Software, and to permit persons to whom the Software is\nfurnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in\nall copies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\nAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\nOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN\nTHE SOFTWARE.' @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION) class __A (datasets.Metric): '''simple docstring''' def lowerCAmelCase ( self : Any ) ->Any: """simple docstring""" return datasets.MetricInfo( # This is the description that will appear on the metrics page. description=_DESCRIPTION , citation=_CITATION , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { """predictions""": datasets.Sequence(datasets.Value("""string""" ) ), """references""": datasets.Value("""string""" ), } ) , homepage="""https://github.com/openai/human-eval""" , codebase_urls=["""https://github.com/openai/human-eval"""] , reference_urls=["""https://github.com/openai/human-eval"""] , license=_LICENSE , ) def lowerCAmelCase ( self : List[Any] , UpperCAmelCase_ : Tuple , UpperCAmelCase_ : List[Any] , UpperCAmelCase_ : Union[str, Any]=[1, 10, 100] , UpperCAmelCase_ : Tuple=4 , UpperCAmelCase_ : Dict=3.0 ) ->Any: """simple docstring""" if os.getenv("""HF_ALLOW_CODE_EVAL""" , 0 ) != "1": raise ValueError(_WARNING ) if os.name == "nt": raise NotImplementedError("""This metric is currently not supported on Windows.""" ) with ThreadPoolExecutor(max_workers=UpperCAmelCase_ ) as executor: snake_case_ = [] snake_case_ = Counter() snake_case_ = 0 snake_case_ = defaultdict(UpperCAmelCase_ ) for task_id, (candidates, test_case) in enumerate(zip(UpperCAmelCase_ , UpperCAmelCase_ ) ): for candidate in candidates: snake_case_ = candidate + """\n""" + test_case snake_case_ = (test_program, timeout, task_id, completion_id[task_id]) snake_case_ = executor.submit(UpperCAmelCase_ , *UpperCAmelCase_ ) futures.append(UpperCAmelCase_ ) completion_id[task_id] += 1 n_samples += 1 for future in as_completed(UpperCAmelCase_ ): snake_case_ = future.result() results[result["task_id"]].append((result["""completion_id"""], result) ) snake_case_ , snake_case_ = [], [] for result in results.values(): result.sort() snake_case_ = [r[1]["""passed"""] for r in result] total.append(len(UpperCAmelCase_ ) ) correct.append(sum(UpperCAmelCase_ ) ) snake_case_ = np.array(UpperCAmelCase_ ) snake_case_ = np.array(UpperCAmelCase_ ) snake_case_ = k snake_case_ = {F"""pass@{k}""": estimate_pass_at_k(UpperCAmelCase_ , UpperCAmelCase_ , UpperCAmelCase_ ).mean() for k in ks if (total >= k).all()} return pass_at_k, results def _a ( _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> Optional[Any]: def estimator(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> float: if n - c < k: return 1.0 return 1.0 - np.prod(1.0 - k / np.arange(n - c + 1 , n + 1 ) ) if isinstance(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ): snake_case_ = itertools.repeat(_SCREAMING_SNAKE_CASE , len(_SCREAMING_SNAKE_CASE ) ) else: assert len(_SCREAMING_SNAKE_CASE ) == len(_SCREAMING_SNAKE_CASE ) snake_case_ = iter(_SCREAMING_SNAKE_CASE ) return np.array([estimator(int(_SCREAMING_SNAKE_CASE ) , int(_SCREAMING_SNAKE_CASE ) , _SCREAMING_SNAKE_CASE ) for n, c in zip(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE )] )
2
"""simple docstring""" from math import factorial def _a ( _SCREAMING_SNAKE_CASE = 20 ) -> int: snake_case_ = 2 * n # middle entry of odd rows starting at row 3 is the solution for n = 1, # 2, 3,... snake_case_ = n // 2 return int(factorial(_SCREAMING_SNAKE_CASE ) / (factorial(_SCREAMING_SNAKE_CASE ) * factorial(n - k )) ) if __name__ == "__main__": import sys if len(sys.argv) == 1: print(solution(20)) else: try: __SCREAMING_SNAKE_CASE : Optional[int] = int(sys.argv[1]) print(solution(n)) except ValueError: print('Invalid entry - please enter a number.')
2
1
"""simple docstring""" from datetime import datetime import matplotlib.pyplot as plt import torch def _a ( _SCREAMING_SNAKE_CASE ) -> str: for param in module.parameters(): snake_case_ = False def _a ( ) -> Any: snake_case_ = """cuda""" if torch.cuda.is_available() else """cpu""" if torch.backends.mps.is_available() and torch.backends.mps.is_built(): snake_case_ = """mps""" if device == "mps": print( """WARNING: MPS currently doesn't seem to work, and messes up backpropagation without any visible torch""" """ errors. I recommend using CUDA on a colab notebook or CPU instead if you're facing inexplicable issues""" """ with generations.""" ) return device def _a ( _SCREAMING_SNAKE_CASE ) -> int: snake_case_ = plt.imshow(_SCREAMING_SNAKE_CASE ) fig.axes.get_xaxis().set_visible(_SCREAMING_SNAKE_CASE ) fig.axes.get_yaxis().set_visible(_SCREAMING_SNAKE_CASE ) plt.show() def _a ( ) -> Dict: snake_case_ = datetime.now() snake_case_ = current_time.strftime("""%H:%M:%S""" ) return timestamp
2
"""simple docstring""" import secrets from random import shuffle from string import ascii_letters, ascii_lowercase, ascii_uppercase, digits, punctuation def _a ( _SCREAMING_SNAKE_CASE = 8 ) -> str: snake_case_ = ascii_letters + digits + punctuation return "".join(secrets.choice(_SCREAMING_SNAKE_CASE ) for _ in range(_SCREAMING_SNAKE_CASE ) ) def _a ( _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> str: # Password Generator = full boot with random_number, random_letters, and # random_character FUNCTIONS # Put your code here... i -= len(_SCREAMING_SNAKE_CASE ) snake_case_ = i // 3 snake_case_ = i % 3 # chars = chars_incl + random_letters(ascii_letters, i / 3 + remainder) + # random_number(digits, i / 3) + random_characters(punctuation, i / 3) snake_case_ = ( chars_incl + random(_SCREAMING_SNAKE_CASE , quotient + remainder ) + random(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) + random(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) ) snake_case_ = list(_SCREAMING_SNAKE_CASE ) shuffle(_SCREAMING_SNAKE_CASE ) return "".join(_SCREAMING_SNAKE_CASE ) # random is a generalised function for letters, characters and numbers def _a ( _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> str: return "".join(secrets.choice(_SCREAMING_SNAKE_CASE ) for _ in range(_SCREAMING_SNAKE_CASE ) ) def _a ( _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> Any: pass # Put your code here... def _a ( _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> List[str]: pass # Put your code here... def _a ( _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> Optional[int]: pass # Put your code here... def _a ( _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE = 8 ) -> bool: if len(_SCREAMING_SNAKE_CASE ) < min_length: # Your Password must be at least 8 characters long return False snake_case_ = any(char in ascii_uppercase for char in password ) snake_case_ = any(char in ascii_lowercase for char in password ) snake_case_ = any(char in digits for char in password ) snake_case_ = any(char in punctuation for char in password ) return upper and lower and num and spec_char # Passwords should contain UPPERCASE, lowerase # numbers, and special characters def _a ( ) -> str: snake_case_ = int(input("""Please indicate the max length of your password: """ ).strip() ) snake_case_ = input( """Please indicate the characters that must be in your password: """ ).strip() print("""Password generated:""" , password_generator(_SCREAMING_SNAKE_CASE ) ) print( """Alternative Password generated:""" , alternative_password_generator(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) , ) print("""[If you are thinking of using this passsword, You better save it.]""" ) if __name__ == "__main__": main()
2
1
"""simple docstring""" import re import time from typing import Optional import IPython.display as disp from ..trainer_callback import TrainerCallback from ..trainer_utils import IntervalStrategy, has_length def _a ( _SCREAMING_SNAKE_CASE ) -> Any: snake_case_ = int(_SCREAMING_SNAKE_CASE ) snake_case_ , snake_case_ , snake_case_ = t // 3_600, (t // 60) % 60, t % 60 return f"""{h}:{m:02d}:{s:02d}""" if h != 0 else f"""{m:02d}:{s:02d}""" def _a ( _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE=300 ) -> Any: # docstyle-ignore return f""" <div> {prefix} <progress value='{value}' max='{total}' style='width:{width}px; height:20px; vertical-align: middle;'></progress> {label} </div> """ def _a ( _SCREAMING_SNAKE_CASE ) -> Tuple: snake_case_ = """<table border=\"1\" class=\"dataframe\">\n""" html_code += """ <thead>\n <tr style="text-align: left;">\n""" for i in items[0]: html_code += f""" <th>{i}</th>\n""" html_code += " </tr>\n </thead>\n <tbody>\n" for line in items[1:]: html_code += " <tr>\n" for elt in line: snake_case_ = f"""{elt:.6f}""" if isinstance(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) else str(_SCREAMING_SNAKE_CASE ) html_code += f""" <td>{elt}</td>\n""" html_code += " </tr>\n" html_code += " </tbody>\n</table><p>" return html_code class __A : '''simple docstring''' __lowercase: List[str] = 5 __lowercase: Union[str, Any] = 0.2 def __init__( self : List[Any] , UpperCAmelCase_ : int , UpperCAmelCase_ : Optional[str] = None , UpperCAmelCase_ : bool = True , UpperCAmelCase_ : Optional["NotebookTrainingTracker"] = None , UpperCAmelCase_ : int = 300 , ) ->List[str]: """simple docstring""" snake_case_ = total snake_case_ = """""" if prefix is None else prefix snake_case_ = leave snake_case_ = parent snake_case_ = width snake_case_ = None snake_case_ = None snake_case_ = None def lowerCAmelCase ( self : Optional[Any] , UpperCAmelCase_ : int , UpperCAmelCase_ : bool = False , UpperCAmelCase_ : str = None ) ->Dict: """simple docstring""" snake_case_ = value if comment is not None: snake_case_ = comment if self.last_value is None: snake_case_ = snake_case_ = time.time() snake_case_ = snake_case_ = value snake_case_ = snake_case_ = None snake_case_ = self.warmup snake_case_ = 1 self.update_bar(UpperCAmelCase_ ) elif value <= self.last_value and not force_update: return elif force_update or self.first_calls > 0 or value >= min(self.last_value + self.wait_for , self.total ): if self.first_calls > 0: self.first_calls -= 1 snake_case_ = time.time() snake_case_ = current_time - self.start_time # We could have value = self.start_value if the update is called twixe with the same start value. if value > self.start_value: snake_case_ = self.elapsed_time / (value - self.start_value) else: snake_case_ = None if value >= self.total: snake_case_ = self.total snake_case_ = None if not self.leave: self.close() elif self.average_time_per_item is not None: snake_case_ = self.average_time_per_item * (self.total - value) self.update_bar(UpperCAmelCase_ ) snake_case_ = value snake_case_ = current_time if self.average_time_per_item is None: snake_case_ = 1 else: snake_case_ = max(int(self.update_every / self.average_time_per_item ) , 1 ) def lowerCAmelCase ( self : List[str] , UpperCAmelCase_ : List[Any] , UpperCAmelCase_ : Union[str, Any]=None ) ->int: """simple docstring""" snake_case_ = """ """ * (len(str(self.total ) ) - len(str(UpperCAmelCase_ ) )) + str(UpperCAmelCase_ ) if self.elapsed_time is None: snake_case_ = F"""[{spaced_value}/{self.total} : < :""" elif self.predicted_remaining is None: snake_case_ = F"""[{spaced_value}/{self.total} {format_time(self.elapsed_time )}""" else: snake_case_ = ( F"""[{spaced_value}/{self.total} {format_time(self.elapsed_time )} <""" F""" {format_time(self.predicted_remaining )}""" ) self.label += F""", {1/self.average_time_per_item:.2f} it/s""" self.label += "]" if self.comment is None or len(self.comment ) == 0 else F""", {self.comment}]""" self.display() def lowerCAmelCase ( self : Any ) ->Optional[int]: """simple docstring""" snake_case_ = html_progress_bar(self.value , self.total , self.prefix , self.label , self.width ) if self.parent is not None: # If this is a child bar, the parent will take care of the display. self.parent.display() return if self.output is None: snake_case_ = disp.display(disp.HTML(self.html_code ) , display_id=UpperCAmelCase_ ) else: self.output.update(disp.HTML(self.html_code ) ) def lowerCAmelCase ( self : Optional[int] ) ->Optional[Any]: """simple docstring""" if self.parent is None and self.output is not None: self.output.update(disp.HTML("""""" ) ) class __A (snake_case__): '''simple docstring''' def __init__( self : Tuple , UpperCAmelCase_ : Any , UpperCAmelCase_ : Any=None ) ->Tuple: """simple docstring""" super().__init__(UpperCAmelCase_ ) snake_case_ = None if column_names is None else [column_names] snake_case_ = None def lowerCAmelCase ( self : List[str] ) ->Optional[int]: """simple docstring""" snake_case_ = html_progress_bar(self.value , self.total , self.prefix , self.label , self.width ) if self.inner_table is not None: self.html_code += text_to_html_table(self.inner_table ) if self.child_bar is not None: self.html_code += self.child_bar.html_code if self.output is None: snake_case_ = disp.display(disp.HTML(self.html_code ) , display_id=UpperCAmelCase_ ) else: self.output.update(disp.HTML(self.html_code ) ) def lowerCAmelCase ( self : Optional[int] , UpperCAmelCase_ : Any ) ->int: """simple docstring""" if self.inner_table is None: snake_case_ = [list(values.keys() ), list(values.values() )] else: snake_case_ = self.inner_table[0] if len(self.inner_table ) == 1: # We give a chance to update the column names at the first iteration for key in values.keys(): if key not in columns: columns.append(UpperCAmelCase_ ) snake_case_ = columns self.inner_table.append([values[c] for c in columns] ) def lowerCAmelCase ( self : Optional[Any] , UpperCAmelCase_ : Dict , UpperCAmelCase_ : Any=None , UpperCAmelCase_ : List[Any]=300 ) ->Tuple: """simple docstring""" snake_case_ = NotebookProgressBar(UpperCAmelCase_ , prefix=UpperCAmelCase_ , parent=self , width=UpperCAmelCase_ ) return self.child_bar def lowerCAmelCase ( self : int ) ->List[str]: """simple docstring""" snake_case_ = None self.display() class __A (snake_case__): '''simple docstring''' def __init__( self : Optional[Any] ) ->Dict: """simple docstring""" snake_case_ = None snake_case_ = None snake_case_ = False def lowerCAmelCase ( self : List[str] , UpperCAmelCase_ : str , UpperCAmelCase_ : Any , UpperCAmelCase_ : List[Any] , **UpperCAmelCase_ : List[str] ) ->List[Any]: """simple docstring""" snake_case_ = """Epoch""" if args.evaluation_strategy == IntervalStrategy.EPOCH else """Step""" snake_case_ = 0 snake_case_ = 0 snake_case_ = [self.first_column] + ["""Training Loss"""] if args.evaluation_strategy != IntervalStrategy.NO: column_names.append("""Validation Loss""" ) snake_case_ = NotebookTrainingTracker(state.max_steps , UpperCAmelCase_ ) def lowerCAmelCase ( self : Optional[int] , UpperCAmelCase_ : List[str] , UpperCAmelCase_ : List[str] , UpperCAmelCase_ : str , **UpperCAmelCase_ : Optional[Any] ) ->Dict: """simple docstring""" snake_case_ = int(state.epoch ) if int(state.epoch ) == state.epoch else F"""{state.epoch:.2f}""" self.training_tracker.update( state.global_step + 1 , comment=F"""Epoch {epoch}/{state.num_train_epochs}""" , force_update=self._force_next_update , ) snake_case_ = False def lowerCAmelCase ( self : Union[str, Any] , UpperCAmelCase_ : Dict , UpperCAmelCase_ : Optional[int] , UpperCAmelCase_ : List[Any] , UpperCAmelCase_ : List[Any]=None , **UpperCAmelCase_ : Dict ) ->Optional[int]: """simple docstring""" if not has_length(UpperCAmelCase_ ): return if self.prediction_bar is None: if self.training_tracker is not None: snake_case_ = self.training_tracker.add_child(len(UpperCAmelCase_ ) ) else: snake_case_ = NotebookProgressBar(len(UpperCAmelCase_ ) ) self.prediction_bar.update(1 ) else: self.prediction_bar.update(self.prediction_bar.value + 1 ) def lowerCAmelCase ( self : Union[str, Any] , UpperCAmelCase_ : Dict , UpperCAmelCase_ : int , UpperCAmelCase_ : List[str] , **UpperCAmelCase_ : Union[str, Any] ) ->Tuple: """simple docstring""" if self.prediction_bar is not None: self.prediction_bar.close() snake_case_ = None def lowerCAmelCase ( self : List[str] , UpperCAmelCase_ : str , UpperCAmelCase_ : List[Any] , UpperCAmelCase_ : str , UpperCAmelCase_ : List[Any]=None , **UpperCAmelCase_ : List[Any] ) ->Union[str, Any]: """simple docstring""" if args.evaluation_strategy == IntervalStrategy.NO and "loss" in logs: snake_case_ = {"""Training Loss""": logs["""loss"""]} # First column is necessarily Step sine we're not in epoch eval strategy snake_case_ = state.global_step self.training_tracker.write_line(UpperCAmelCase_ ) def lowerCAmelCase ( self : List[str] , UpperCAmelCase_ : str , UpperCAmelCase_ : List[str] , UpperCAmelCase_ : Any , UpperCAmelCase_ : List[Any]=None , **UpperCAmelCase_ : Optional[Any] ) ->int: """simple docstring""" if self.training_tracker is not None: snake_case_ = {"""Training Loss""": """No log""", """Validation Loss""": """No log"""} for log in reversed(state.log_history ): if "loss" in log: snake_case_ = log["""loss"""] break if self.first_column == "Epoch": snake_case_ = int(state.epoch ) else: snake_case_ = state.global_step snake_case_ = """eval""" for k in metrics: if k.endswith("""_loss""" ): snake_case_ = re.sub(R"""\_loss$""" , """""" , UpperCAmelCase_ ) snake_case_ = metrics.pop("""total_flos""" , UpperCAmelCase_ ) snake_case_ = metrics.pop("""epoch""" , UpperCAmelCase_ ) snake_case_ = metrics.pop(F"""{metric_key_prefix}_runtime""" , UpperCAmelCase_ ) snake_case_ = metrics.pop(F"""{metric_key_prefix}_samples_per_second""" , UpperCAmelCase_ ) snake_case_ = metrics.pop(F"""{metric_key_prefix}_steps_per_second""" , UpperCAmelCase_ ) snake_case_ = metrics.pop(F"""{metric_key_prefix}_jit_compilation_time""" , UpperCAmelCase_ ) for k, v in metrics.items(): if k == F"""{metric_key_prefix}_loss""": snake_case_ = v else: snake_case_ = k.split("""_""" ) snake_case_ = """ """.join([part.capitalize() for part in splits[1:]] ) snake_case_ = v self.training_tracker.write_line(UpperCAmelCase_ ) self.training_tracker.remove_child() snake_case_ = None # Evaluation takes a long time so we should force the next update. snake_case_ = True def lowerCAmelCase ( self : int , UpperCAmelCase_ : Union[str, Any] , UpperCAmelCase_ : Any , UpperCAmelCase_ : int , **UpperCAmelCase_ : Optional[Any] ) ->Tuple: """simple docstring""" self.training_tracker.update( state.global_step , comment=F"""Epoch {int(state.epoch )}/{state.num_train_epochs}""" , force_update=UpperCAmelCase_ ) snake_case_ = None
2
"""simple docstring""" import json import os import tempfile import unittest import numpy as np from datasets import load_dataset from transformers.testing_utils import require_torch, require_vision, slow from transformers.utils import is_torch_available, is_vision_available from ...test_image_processing_common import ImageProcessingSavingTestMixin if is_torch_available(): import torch if is_vision_available(): from PIL import Image from transformers import ImageGPTImageProcessor class __A (unittest.TestCase): '''simple docstring''' def __init__( self : int , UpperCAmelCase_ : List[str] , UpperCAmelCase_ : Tuple=7 , UpperCAmelCase_ : Dict=3 , UpperCAmelCase_ : Tuple=18 , UpperCAmelCase_ : Optional[Any]=30 , UpperCAmelCase_ : str=400 , UpperCAmelCase_ : int=True , UpperCAmelCase_ : Optional[int]=None , UpperCAmelCase_ : Optional[Any]=True , ) ->Optional[Any]: """simple docstring""" snake_case_ = size if size is not None else {"""height""": 18, """width""": 18} snake_case_ = parent snake_case_ = batch_size snake_case_ = num_channels snake_case_ = image_size snake_case_ = min_resolution snake_case_ = max_resolution snake_case_ = do_resize snake_case_ = size snake_case_ = do_normalize def lowerCAmelCase ( self : List[str] ) ->Optional[Any]: """simple docstring""" return { # here we create 2 clusters for the sake of simplicity "clusters": np.asarray( [ [0.8_866_443_634_033_203, 0.6_618_829_369_544_983, 0.3_891_746_401_786_804], [-0.6_042_559_146_881_104, -0.02_295_008_860_528_469, 0.5_423_797_369_003_296], ] ), "do_resize": self.do_resize, "size": self.size, "do_normalize": self.do_normalize, } @require_torch @require_vision class __A (snake_case__ , unittest.TestCase): '''simple docstring''' __lowercase: List[Any] = ImageGPTImageProcessor if is_vision_available() else None def lowerCAmelCase ( self : Optional[int] ) ->Optional[int]: """simple docstring""" snake_case_ = ImageGPTImageProcessingTester(self ) @property def lowerCAmelCase ( self : Tuple ) ->List[Any]: """simple docstring""" return self.image_processor_tester.prepare_image_processor_dict() def lowerCAmelCase ( self : Optional[Any] ) ->List[str]: """simple docstring""" snake_case_ = self.image_processing_class(**self.image_processor_dict ) self.assertTrue(hasattr(UpperCAmelCase_ , """clusters""" ) ) self.assertTrue(hasattr(UpperCAmelCase_ , """do_resize""" ) ) self.assertTrue(hasattr(UpperCAmelCase_ , """size""" ) ) self.assertTrue(hasattr(UpperCAmelCase_ , """do_normalize""" ) ) def lowerCAmelCase ( self : Optional[int] ) ->Optional[Any]: """simple docstring""" snake_case_ = self.image_processing_class.from_dict(self.image_processor_dict ) self.assertEqual(image_processor.size , {"""height""": 18, """width""": 18} ) snake_case_ = self.image_processing_class.from_dict(self.image_processor_dict , size=42 ) self.assertEqual(image_processor.size , {"""height""": 42, """width""": 42} ) def lowerCAmelCase ( self : Any ) ->List[Any]: """simple docstring""" snake_case_ = self.image_processing_class(**self.image_processor_dict ) snake_case_ = json.loads(image_processor.to_json_string() ) for key, value in self.image_processor_dict.items(): if key == "clusters": self.assertTrue(np.array_equal(UpperCAmelCase_ , obj[key] ) ) else: self.assertEqual(obj[key] , UpperCAmelCase_ ) def lowerCAmelCase ( self : Optional[int] ) ->Dict: """simple docstring""" snake_case_ = self.image_processing_class(**self.image_processor_dict ) with tempfile.TemporaryDirectory() as tmpdirname: snake_case_ = os.path.join(UpperCAmelCase_ , """image_processor.json""" ) image_processor_first.to_json_file(UpperCAmelCase_ ) snake_case_ = self.image_processing_class.from_json_file(UpperCAmelCase_ ).to_dict() snake_case_ = image_processor_first.to_dict() for key, value in image_processor_first.items(): if key == "clusters": self.assertTrue(np.array_equal(UpperCAmelCase_ , image_processor_second[key] ) ) else: self.assertEqual(image_processor_first[key] , UpperCAmelCase_ ) def lowerCAmelCase ( self : Optional[Any] ) ->List[Any]: """simple docstring""" snake_case_ = self.image_processing_class(**self.image_processor_dict ) with tempfile.TemporaryDirectory() as tmpdirname: image_processor_first.save_pretrained(UpperCAmelCase_ ) snake_case_ = self.image_processing_class.from_pretrained(UpperCAmelCase_ ).to_dict() snake_case_ = image_processor_first.to_dict() for key, value in image_processor_first.items(): if key == "clusters": self.assertTrue(np.array_equal(UpperCAmelCase_ , image_processor_second[key] ) ) else: self.assertEqual(image_processor_first[key] , UpperCAmelCase_ ) @unittest.skip("""ImageGPT requires clusters at initialization""" ) def lowerCAmelCase ( self : List[Any] ) ->Tuple: """simple docstring""" pass def _a ( ) -> str: snake_case_ = load_dataset("""hf-internal-testing/fixtures_image_utils""" , split="""test""" ) snake_case_ = Image.open(dataset[4]["""file"""] ) snake_case_ = Image.open(dataset[5]["""file"""] ) snake_case_ = [imagea, imagea] return images @require_vision @require_torch class __A (unittest.TestCase): '''simple docstring''' @slow def lowerCAmelCase ( self : Tuple ) ->List[str]: """simple docstring""" snake_case_ = ImageGPTImageProcessor.from_pretrained("""openai/imagegpt-small""" ) snake_case_ = prepare_images() # test non-batched snake_case_ = image_processing(images[0] , return_tensors="""pt""" ) self.assertIsInstance(encoding.input_ids , torch.LongTensor ) self.assertEqual(encoding.input_ids.shape , (1, 1_024) ) snake_case_ = [306, 191, 191] self.assertEqual(encoding.input_ids[0, :3].tolist() , UpperCAmelCase_ ) # test batched snake_case_ = image_processing(UpperCAmelCase_ , return_tensors="""pt""" ) self.assertIsInstance(encoding.input_ids , torch.LongTensor ) self.assertEqual(encoding.input_ids.shape , (2, 1_024) ) snake_case_ = [303, 13, 13] self.assertEqual(encoding.input_ids[1, -3:].tolist() , UpperCAmelCase_ )
2
1
"""simple docstring""" import logging import os import sys from dataclasses import dataclass, field from typing import Optional from seqaseq_trainer import SeqaSeqTrainer from seqaseq_training_args import SeqaSeqTrainingArguments import transformers from transformers import ( AutoConfig, AutoModelForSeqaSeqLM, AutoTokenizer, HfArgumentParser, MBartTokenizer, MBartTokenizerFast, set_seed, ) from transformers.trainer_utils import EvaluationStrategy, is_main_process from transformers.training_args import ParallelMode from utils import ( SeqaSeqDataCollator, SeqaSeqDataset, assert_all_frozen, build_compute_metrics_fn, check_output_dir, freeze_embeds, freeze_params, lmap, save_json, use_task_specific_params, write_txt_file, ) __SCREAMING_SNAKE_CASE : int = logging.getLogger(__name__) @dataclass class __A : '''simple docstring''' __lowercase: str = field( metadata={"""help""": """Path to pretrained model or model identifier from huggingface.co/models"""}) __lowercase: Optional[str] = field( default=snake_case__ , metadata={"""help""": """Pretrained config name or path if not the same as model_name"""}) __lowercase: Optional[str] = field( default=snake_case__ , metadata={"""help""": """Pretrained tokenizer name or path if not the same as model_name"""}) __lowercase: Optional[str] = field( default=snake_case__ , metadata={"""help""": """Where do you want to store the pretrained models downloaded from huggingface.co"""} , ) __lowercase: bool = field(default=snake_case__ , metadata={"""help""": """Whether tp freeze the encoder."""}) __lowercase: bool = field(default=snake_case__ , metadata={"""help""": """Whether to freeze the embeddings."""}) @dataclass class __A : '''simple docstring''' __lowercase: str = field( metadata={"""help""": """The input data dir. Should contain the .tsv files (or other data files) for the task."""}) __lowercase: Optional[str] = field( default="""summarization""" , metadata={"""help""": """Task name, summarization (or summarization_{dataset} for pegasus) or translation"""} , ) __lowercase: Optional[int] = field( default=10_24 , metadata={ """help""": ( """The maximum total input sequence length after tokenization. Sequences longer """ """than this will be truncated, sequences shorter will be padded.""" ) } , ) __lowercase: Optional[int] = field( default=1_28 , metadata={ """help""": ( """The maximum total sequence length for target text after tokenization. Sequences longer """ """than this will be truncated, sequences shorter will be padded.""" ) } , ) __lowercase: Optional[int] = field( default=1_42 , metadata={ """help""": ( """The maximum total sequence length for validation target text after tokenization. Sequences longer """ """than this will be truncated, sequences shorter will be padded. """ """This argument is also used to override the ``max_length`` param of ``model.generate``, which is used """ """during ``evaluate`` and ``predict``.""" ) } , ) __lowercase: Optional[int] = field( default=1_42 , metadata={ """help""": ( """The maximum total sequence length for test target text after tokenization. Sequences longer """ """than this will be truncated, sequences shorter will be padded.""" ) } , ) __lowercase: Optional[int] = field(default=-1 , metadata={"""help""": """# training examples. -1 means use all."""}) __lowercase: Optional[int] = field(default=-1 , metadata={"""help""": """# validation examples. -1 means use all."""}) __lowercase: Optional[int] = field(default=-1 , metadata={"""help""": """# test examples. -1 means use all."""}) __lowercase: Optional[str] = field(default=snake_case__ , metadata={"""help""": """Source language id for translation."""}) __lowercase: Optional[str] = field(default=snake_case__ , metadata={"""help""": """Target language id for translation."""}) __lowercase: Optional[int] = field(default=snake_case__ , metadata={"""help""": """# num_beams to use for evaluation."""}) __lowercase: bool = field( default=snake_case__ , metadata={"""help""": """If only pad tokens should be ignored. This assumes that `config.pad_token_id` is defined."""} , ) def _a ( _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> Optional[int]: logger.info(f"""***** {split} metrics *****""" ) for key in sorted(metrics.keys() ): logger.info(f""" {key} = {metrics[key]}""" ) save_json(_SCREAMING_SNAKE_CASE , os.path.join(_SCREAMING_SNAKE_CASE , f"""{split}_results.json""" ) ) def _a ( ) -> Union[str, Any]: # See all possible arguments in src/transformers/training_args.py # or by passing the --help flag to this script. # We now keep distinct sets of args, for a cleaner separation of concerns. snake_case_ = HfArgumentParser((ModelArguments, DataTrainingArguments, SeqaSeqTrainingArguments) ) 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. snake_case_ , snake_case_ , snake_case_ = parser.parse_json_file(json_file=os.path.abspath(sys.argv[1] ) ) else: snake_case_ , snake_case_ , snake_case_ = parser.parse_args_into_dataclasses() check_output_dir(_SCREAMING_SNAKE_CASE ) # Setup logging logging.basicConfig( format="""%(asctime)s - %(levelname)s - %(name)s - %(message)s""" , datefmt="""%m/%d/%Y %H:%M:%S""" , level=logging.INFO if training_args.local_rank in [-1, 0] else logging.WARN , ) logger.warning( """Process rank: %s, device: %s, n_gpu: %s, distributed training: %s, 16-bits training: %s""" , training_args.local_rank , training_args.device , training_args.n_gpu , bool(training_args.parallel_mode == ParallelMode.DISTRIBUTED ) , training_args.fpaa , ) transformers.utils.logging.enable_default_handler() transformers.utils.logging.enable_explicit_format() # Set the verbosity to info of the Transformers logger (on main process only): if is_main_process(training_args.local_rank ): transformers.utils.logging.set_verbosity_info() logger.info("""Training/evaluation parameters %s""" , _SCREAMING_SNAKE_CASE ) # Set seed set_seed(training_args.seed ) # Load pretrained model and tokenizer # # Distributed training: # The .from_pretrained methods guarantee that only one local process can concurrently # download model & vocab. snake_case_ = 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 , ) snake_case_ = ("""encoder_layerdrop""", """decoder_layerdrop""", """dropout""", """attention_dropout""") for p in extra_model_params: if getattr(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ): assert hasattr(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ), f"""({config.__class__.__name__}) doesn't have a `{p}` attribute""" setattr(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , getattr(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) ) snake_case_ = 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 , ) snake_case_ = AutoModelForSeqaSeqLM.from_pretrained( model_args.model_name_or_path , from_tf=""".ckpt""" in model_args.model_name_or_path , config=_SCREAMING_SNAKE_CASE , cache_dir=model_args.cache_dir , ) # use task specific params use_task_specific_params(_SCREAMING_SNAKE_CASE , data_args.task ) # set num_beams for evaluation if data_args.eval_beams is None: snake_case_ = model.config.num_beams # set decoder_start_token_id for MBart if model.config.decoder_start_token_id is None and isinstance(_SCREAMING_SNAKE_CASE , (MBartTokenizer, MBartTokenizerFast) ): assert ( data_args.tgt_lang is not None and data_args.src_lang is not None ), "mBart requires --tgt_lang and --src_lang" if isinstance(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ): snake_case_ = tokenizer.lang_code_to_id[data_args.tgt_lang] else: snake_case_ = tokenizer.convert_tokens_to_ids(data_args.tgt_lang ) if model_args.freeze_embeds: freeze_embeds(_SCREAMING_SNAKE_CASE ) if model_args.freeze_encoder: freeze_params(model.get_encoder() ) assert_all_frozen(model.get_encoder() ) snake_case_ = SeqaSeqDataset # Get datasets snake_case_ = ( dataset_class( _SCREAMING_SNAKE_CASE , type_path="""train""" , data_dir=data_args.data_dir , n_obs=data_args.n_train , max_target_length=data_args.max_target_length , max_source_length=data_args.max_source_length , prefix=model.config.prefix or """""" , ) if training_args.do_train else None ) snake_case_ = ( dataset_class( _SCREAMING_SNAKE_CASE , type_path="""val""" , data_dir=data_args.data_dir , n_obs=data_args.n_val , max_target_length=data_args.val_max_target_length , max_source_length=data_args.max_source_length , prefix=model.config.prefix or """""" , ) if training_args.do_eval or training_args.evaluation_strategy != EvaluationStrategy.NO else None ) snake_case_ = ( dataset_class( _SCREAMING_SNAKE_CASE , type_path="""test""" , data_dir=data_args.data_dir , n_obs=data_args.n_test , max_target_length=data_args.test_max_target_length , max_source_length=data_args.max_source_length , prefix=model.config.prefix or """""" , ) if training_args.do_predict else None ) # Initialize our Trainer snake_case_ = ( build_compute_metrics_fn(data_args.task , _SCREAMING_SNAKE_CASE ) if training_args.predict_with_generate else None ) snake_case_ = SeqaSeqTrainer( model=_SCREAMING_SNAKE_CASE , args=_SCREAMING_SNAKE_CASE , data_args=_SCREAMING_SNAKE_CASE , train_dataset=_SCREAMING_SNAKE_CASE , eval_dataset=_SCREAMING_SNAKE_CASE , data_collator=SeqaSeqDataCollator( _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , model.config.decoder_start_token_id , training_args.tpu_num_cores ) , compute_metrics=_SCREAMING_SNAKE_CASE , tokenizer=_SCREAMING_SNAKE_CASE , ) snake_case_ = {} # Training if training_args.do_train: logger.info("""*** Train ***""" ) snake_case_ = trainer.train( model_path=model_args.model_name_or_path if os.path.isdir(model_args.model_name_or_path ) else None ) snake_case_ = train_result.metrics snake_case_ = data_args.n_train trainer.save_model() # this also saves the tokenizer if trainer.is_world_process_zero(): handle_metrics("""train""" , _SCREAMING_SNAKE_CASE , training_args.output_dir ) all_metrics.update(_SCREAMING_SNAKE_CASE ) # Need to save the state, since Trainer.save_model saves only the tokenizer with the model trainer.state.save_to_json(os.path.join(training_args.output_dir , """trainer_state.json""" ) ) # For convenience, we also re-save the tokenizer to the same directory, # so that you can share your model easily on huggingface.co/models =) tokenizer.save_pretrained(training_args.output_dir ) # Evaluation if training_args.do_eval: logger.info("""*** Evaluate ***""" ) snake_case_ = trainer.evaluate(metric_key_prefix="""val""" ) snake_case_ = data_args.n_val snake_case_ = round(metrics["""val_loss"""] , 4 ) if trainer.is_world_process_zero(): handle_metrics("""val""" , _SCREAMING_SNAKE_CASE , training_args.output_dir ) all_metrics.update(_SCREAMING_SNAKE_CASE ) if training_args.do_predict: logger.info("""*** Predict ***""" ) snake_case_ = trainer.predict(test_dataset=_SCREAMING_SNAKE_CASE , metric_key_prefix="""test""" ) snake_case_ = test_output.metrics snake_case_ = data_args.n_test if trainer.is_world_process_zero(): snake_case_ = round(metrics["""test_loss"""] , 4 ) handle_metrics("""test""" , _SCREAMING_SNAKE_CASE , training_args.output_dir ) all_metrics.update(_SCREAMING_SNAKE_CASE ) if training_args.predict_with_generate: snake_case_ = tokenizer.batch_decode( test_output.predictions , skip_special_tokens=_SCREAMING_SNAKE_CASE , clean_up_tokenization_spaces=_SCREAMING_SNAKE_CASE ) snake_case_ = lmap(str.strip , _SCREAMING_SNAKE_CASE ) write_txt_file(_SCREAMING_SNAKE_CASE , os.path.join(training_args.output_dir , """test_generations.txt""" ) ) if trainer.is_world_process_zero(): save_json(_SCREAMING_SNAKE_CASE , os.path.join(training_args.output_dir , """all_results.json""" ) ) return all_metrics def _a ( _SCREAMING_SNAKE_CASE ) -> str: # For xla_spawn (TPUs) main() if __name__ == "__main__": main()
2
"""simple docstring""" import unittest from transformers import LiltConfig, 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 from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from transformers import ( LiltForQuestionAnswering, LiltForSequenceClassification, LiltForTokenClassification, LiltModel, ) from transformers.models.lilt.modeling_lilt import LILT_PRETRAINED_MODEL_ARCHIVE_LIST class __A : '''simple docstring''' def __init__( self : Optional[int] , UpperCAmelCase_ : Optional[int] , UpperCAmelCase_ : List[Any]=13 , UpperCAmelCase_ : Optional[int]=7 , UpperCAmelCase_ : List[str]=True , UpperCAmelCase_ : int=True , UpperCAmelCase_ : List[Any]=True , UpperCAmelCase_ : Optional[int]=True , UpperCAmelCase_ : List[str]=99 , UpperCAmelCase_ : Dict=24 , UpperCAmelCase_ : List[str]=2 , UpperCAmelCase_ : Optional[Any]=6 , UpperCAmelCase_ : int=37 , UpperCAmelCase_ : Optional[Any]="gelu" , UpperCAmelCase_ : Optional[Any]=0.1 , UpperCAmelCase_ : List[Any]=0.1 , UpperCAmelCase_ : Any=512 , UpperCAmelCase_ : str=16 , UpperCAmelCase_ : List[str]=2 , UpperCAmelCase_ : Optional[int]=0.02 , UpperCAmelCase_ : Tuple=3 , UpperCAmelCase_ : Union[str, Any]=None , UpperCAmelCase_ : Any=1_000 , ) ->Tuple: """simple docstring""" snake_case_ = parent snake_case_ = batch_size snake_case_ = seq_length snake_case_ = is_training snake_case_ = use_input_mask snake_case_ = use_token_type_ids snake_case_ = use_labels snake_case_ = vocab_size snake_case_ = hidden_size snake_case_ = num_hidden_layers snake_case_ = num_attention_heads snake_case_ = intermediate_size snake_case_ = hidden_act snake_case_ = hidden_dropout_prob snake_case_ = attention_probs_dropout_prob snake_case_ = max_position_embeddings snake_case_ = type_vocab_size snake_case_ = type_sequence_label_size snake_case_ = initializer_range snake_case_ = num_labels snake_case_ = scope snake_case_ = range_bbox def lowerCAmelCase ( self : Tuple ) ->int: """simple docstring""" snake_case_ = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) snake_case_ = ids_tensor([self.batch_size, self.seq_length, 4] , self.range_bbox ) # Ensure that bbox is legal for i in range(bbox.shape[0] ): for j in range(bbox.shape[1] ): if bbox[i, j, 3] < bbox[i, j, 1]: snake_case_ = bbox[i, j, 3] snake_case_ = bbox[i, j, 1] snake_case_ = t if bbox[i, j, 2] < bbox[i, j, 0]: snake_case_ = bbox[i, j, 2] snake_case_ = bbox[i, j, 0] snake_case_ = t snake_case_ = None if self.use_input_mask: snake_case_ = ids_tensor([self.batch_size, self.seq_length] , vocab_size=2 ) snake_case_ = None if self.use_token_type_ids: snake_case_ = ids_tensor([self.batch_size, self.seq_length] , self.type_vocab_size ) snake_case_ = None snake_case_ = None if self.use_labels: snake_case_ = ids_tensor([self.batch_size] , self.type_sequence_label_size ) snake_case_ = ids_tensor([self.batch_size, self.seq_length] , self.num_labels ) snake_case_ = self.get_config() return config, input_ids, bbox, token_type_ids, input_mask, sequence_labels, token_labels def lowerCAmelCase ( self : int ) ->Optional[int]: """simple docstring""" return LiltConfig( 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 , ) def lowerCAmelCase ( self : Any , UpperCAmelCase_ : Union[str, Any] , UpperCAmelCase_ : Any , UpperCAmelCase_ : Dict , UpperCAmelCase_ : int , UpperCAmelCase_ : Union[str, Any] , UpperCAmelCase_ : Optional[Any] , UpperCAmelCase_ : Optional[int] , ) ->str: """simple docstring""" snake_case_ = LiltModel(config=UpperCAmelCase_ ) model.to(UpperCAmelCase_ ) model.eval() snake_case_ = model(UpperCAmelCase_ , bbox=UpperCAmelCase_ , attention_mask=UpperCAmelCase_ , token_type_ids=UpperCAmelCase_ ) snake_case_ = model(UpperCAmelCase_ , bbox=UpperCAmelCase_ , token_type_ids=UpperCAmelCase_ ) snake_case_ = model(UpperCAmelCase_ , bbox=UpperCAmelCase_ ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) self.parent.assertEqual(result.pooler_output.shape , (self.batch_size, self.hidden_size) ) def lowerCAmelCase ( self : int , UpperCAmelCase_ : str , UpperCAmelCase_ : Any , UpperCAmelCase_ : Tuple , UpperCAmelCase_ : Union[str, Any] , UpperCAmelCase_ : Optional[int] , UpperCAmelCase_ : str , UpperCAmelCase_ : List[Any] , ) ->Dict: """simple docstring""" snake_case_ = self.num_labels snake_case_ = LiltForTokenClassification(config=UpperCAmelCase_ ) model.to(UpperCAmelCase_ ) model.eval() snake_case_ = model( UpperCAmelCase_ , bbox=UpperCAmelCase_ , attention_mask=UpperCAmelCase_ , token_type_ids=UpperCAmelCase_ , labels=UpperCAmelCase_ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.num_labels) ) def lowerCAmelCase ( self : Any , UpperCAmelCase_ : List[str] , UpperCAmelCase_ : Dict , UpperCAmelCase_ : str , UpperCAmelCase_ : List[str] , UpperCAmelCase_ : int , UpperCAmelCase_ : List[Any] , UpperCAmelCase_ : List[str] , ) ->Dict: """simple docstring""" snake_case_ = LiltForQuestionAnswering(config=UpperCAmelCase_ ) model.to(UpperCAmelCase_ ) model.eval() snake_case_ = model( UpperCAmelCase_ , bbox=UpperCAmelCase_ , attention_mask=UpperCAmelCase_ , token_type_ids=UpperCAmelCase_ , start_positions=UpperCAmelCase_ , end_positions=UpperCAmelCase_ , ) self.parent.assertEqual(result.start_logits.shape , (self.batch_size, self.seq_length) ) self.parent.assertEqual(result.end_logits.shape , (self.batch_size, self.seq_length) ) def lowerCAmelCase ( self : int ) ->Optional[int]: """simple docstring""" snake_case_ = self.prepare_config_and_inputs() ( ( snake_case_ ) , ( snake_case_ ) , ( snake_case_ ) , ( snake_case_ ) , ( snake_case_ ) , ( snake_case_ ) , ( snake_case_ ) , ) = config_and_inputs snake_case_ = { """input_ids""": input_ids, """bbox""": bbox, """token_type_ids""": token_type_ids, """attention_mask""": input_mask, } return config, inputs_dict @require_torch class __A (snake_case__ , snake_case__ , snake_case__ , unittest.TestCase): '''simple docstring''' __lowercase: Optional[int] = ( ( LiltModel, LiltForSequenceClassification, LiltForTokenClassification, LiltForQuestionAnswering, ) if is_torch_available() else () ) __lowercase: Optional[Any] = ( { """feature-extraction""": LiltModel, """question-answering""": LiltForQuestionAnswering, """text-classification""": LiltForSequenceClassification, """token-classification""": LiltForTokenClassification, """zero-shot""": LiltForSequenceClassification, } if is_torch_available() else {} ) __lowercase: Union[str, Any] = False __lowercase: List[str] = False def lowerCAmelCase ( self : str , UpperCAmelCase_ : int , UpperCAmelCase_ : List[str] , UpperCAmelCase_ : Dict , UpperCAmelCase_ : int , UpperCAmelCase_ : Optional[int] ) ->Optional[int]: """simple docstring""" return True def lowerCAmelCase ( self : Dict ) ->Union[str, Any]: """simple docstring""" snake_case_ = LiltModelTester(self ) snake_case_ = ConfigTester(self , config_class=UpperCAmelCase_ , hidden_size=37 ) def lowerCAmelCase ( self : str ) ->List[Any]: """simple docstring""" self.config_tester.run_common_tests() def lowerCAmelCase ( self : List[str] ) ->int: """simple docstring""" snake_case_ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*UpperCAmelCase_ ) def lowerCAmelCase ( self : Union[str, Any] ) ->List[str]: """simple docstring""" snake_case_ = self.model_tester.prepare_config_and_inputs() for type in ["absolute", "relative_key", "relative_key_query"]: snake_case_ = type self.model_tester.create_and_check_model(*UpperCAmelCase_ ) def lowerCAmelCase ( self : List[Any] ) ->Dict: """simple docstring""" snake_case_ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_token_classification(*UpperCAmelCase_ ) def lowerCAmelCase ( self : Optional[Any] ) ->Dict: """simple docstring""" snake_case_ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_question_answering(*UpperCAmelCase_ ) @slow def lowerCAmelCase ( self : Union[str, Any] ) ->Optional[int]: """simple docstring""" for model_name in LILT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: snake_case_ = LiltModel.from_pretrained(UpperCAmelCase_ ) self.assertIsNotNone(UpperCAmelCase_ ) @require_torch @slow class __A (unittest.TestCase): '''simple docstring''' def lowerCAmelCase ( self : Optional[int] ) ->Dict: """simple docstring""" snake_case_ = LiltModel.from_pretrained("""SCUT-DLVCLab/lilt-roberta-en-base""" ).to(UpperCAmelCase_ ) snake_case_ = torch.tensor([[1, 2]] , device=UpperCAmelCase_ ) snake_case_ = torch.tensor([[[1, 2, 3, 4], [5, 6, 7, 8]]] , device=UpperCAmelCase_ ) # forward pass with torch.no_grad(): snake_case_ = model(input_ids=UpperCAmelCase_ , bbox=UpperCAmelCase_ ) snake_case_ = torch.Size([1, 2, 768] ) snake_case_ = torch.tensor( [[-0.0_653, 0.0_950, -0.0_061], [-0.0_545, 0.0_926, -0.0_324]] , device=UpperCAmelCase_ , ) self.assertTrue(outputs.last_hidden_state.shape , UpperCAmelCase_ ) self.assertTrue(torch.allclose(outputs.last_hidden_state[0, :, :3] , UpperCAmelCase_ , atol=1E-3 ) )
2
1
"""simple docstring""" import os from typing import Dict, List, Union import tensorflow as tf from keras_nlp.tokenizers import BytePairTokenizer from tensorflow_text import pad_model_inputs from .tokenization_gpta import GPTaTokenizer class __A (tf.keras.layers.Layer): '''simple docstring''' def __init__( self : List[Any] , UpperCAmelCase_ : Dict[str, int] , UpperCAmelCase_ : List[str] , UpperCAmelCase_ : int = None , UpperCAmelCase_ : int = None ) ->Dict: """simple docstring""" super().__init__() snake_case_ = pad_token_id snake_case_ = max_length snake_case_ = vocab snake_case_ = merges snake_case_ = BytePairTokenizer(UpperCAmelCase_ , UpperCAmelCase_ , sequence_length=UpperCAmelCase_ ) @classmethod def lowerCAmelCase ( cls : Tuple , UpperCAmelCase_ : GPTaTokenizer , *UpperCAmelCase_ : List[Any] , **UpperCAmelCase_ : List[Any] ) ->Optional[int]: """simple docstring""" snake_case_ = [""" """.join(UpperCAmelCase_ ) for m in tokenizer.bpe_ranks.keys()] snake_case_ = tokenizer.get_vocab() return cls(UpperCAmelCase_ , UpperCAmelCase_ , *UpperCAmelCase_ , **UpperCAmelCase_ ) @classmethod def lowerCAmelCase ( cls : Union[str, Any] , UpperCAmelCase_ : Union[str, os.PathLike] , *UpperCAmelCase_ : Optional[Any] , **UpperCAmelCase_ : Union[str, Any] ) ->List[Any]: """simple docstring""" snake_case_ = GPTaTokenizer.from_pretrained(UpperCAmelCase_ , *UpperCAmelCase_ , **UpperCAmelCase_ ) return cls.from_tokenizer(UpperCAmelCase_ , *UpperCAmelCase_ , **UpperCAmelCase_ ) @classmethod def lowerCAmelCase ( cls : List[str] , UpperCAmelCase_ : Union[str, Any] ) ->List[str]: """simple docstring""" return cls(**UpperCAmelCase_ ) def lowerCAmelCase ( self : Dict ) ->int: """simple docstring""" return { "vocab": self.vocab, "merges": self.merges, "max_length": self.max_length, "pad_token_id": self.pad_token_id, } def lowerCAmelCase ( self : Optional[Any] , UpperCAmelCase_ : Any , UpperCAmelCase_ : int = None ) ->Any: """simple docstring""" snake_case_ = self.tf_tokenizer(UpperCAmelCase_ ) snake_case_ = tf.ones_like(UpperCAmelCase_ ) if self.pad_token_id is not None: # pad the tokens up to max length snake_case_ = max_length if max_length is not None else self.max_length if max_length is not None: snake_case_ , snake_case_ = pad_model_inputs( UpperCAmelCase_ , max_seq_length=UpperCAmelCase_ , pad_value=self.pad_token_id ) return {"attention_mask": attention_mask, "input_ids": input_ids}
2
"""simple docstring""" from __future__ import annotations def _a ( _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> list[int]: snake_case_ = 0 snake_case_ = len(_SCREAMING_SNAKE_CASE ) - 1 while i < j: if nums[i] + nums[j] == target: return [i, j] elif nums[i] + nums[j] < target: snake_case_ = i + 1 else: snake_case_ = j - 1 return [] if __name__ == "__main__": import doctest doctest.testmod() print(f"""{two_pointer([2, 7, 11, 15], 9) = }""")
2
1
"""simple docstring""" from operator import delitem, getitem, setitem import pytest from data_structures.hashing.hash_map import HashMap def _a ( _SCREAMING_SNAKE_CASE ) -> str: return getitem, k def _a ( _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) -> Optional[Any]: return setitem, k, v def _a ( _SCREAMING_SNAKE_CASE ) -> Optional[int]: return delitem, k def _a ( _SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , *_SCREAMING_SNAKE_CASE ) -> Any: try: return fun(_SCREAMING_SNAKE_CASE , *_SCREAMING_SNAKE_CASE ), None except Exception as e: return None, e __SCREAMING_SNAKE_CASE : str = ( _set('key_a', 'val_a'), _set('key_b', 'val_b'), ) __SCREAMING_SNAKE_CASE : int = [ _set('key_a', 'val_a'), _set('key_a', 'val_b'), ] __SCREAMING_SNAKE_CASE : int = [ _set('key_a', 'val_a'), _set('key_b', 'val_b'), _del('key_a'), _del('key_b'), _set('key_a', 'val_a'), _del('key_a'), ] __SCREAMING_SNAKE_CASE : Union[str, Any] = [ _get('key_a'), _del('key_a'), _set('key_a', 'val_a'), _del('key_a'), _del('key_a'), _get('key_a'), ] __SCREAMING_SNAKE_CASE : int = [ *[_set(x, x) for x in range(5)], # guaranteed upsize ] __SCREAMING_SNAKE_CASE : List[str] = [ *[_set(x, x) for x in range(5)], # guaranteed upsize *[_del(x) for x in range(5)], _set('key_a', 'val_b'), ] @pytest.mark.parametrize( """operations""" , ( pytest.param(_add_items , id="""add items""" ), pytest.param(_overwrite_items , id="""overwrite items""" ), pytest.param(_delete_items , id="""delete items""" ), pytest.param(_access_absent_items , id="""access absent items""" ), pytest.param(_add_with_resize_up , id="""add with resize up""" ), pytest.param(_add_with_resize_down , id="""add with resize down""" ), ) , ) def _a ( _SCREAMING_SNAKE_CASE ) -> Optional[int]: snake_case_ = HashMap(initial_block_size=4 ) snake_case_ = {} for _, (fun, *args) in enumerate(_SCREAMING_SNAKE_CASE ): snake_case_ , snake_case_ = _run_operation(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , *_SCREAMING_SNAKE_CASE ) snake_case_ , snake_case_ = _run_operation(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE , *_SCREAMING_SNAKE_CASE ) assert my_res == py_res assert str(_SCREAMING_SNAKE_CASE ) == str(_SCREAMING_SNAKE_CASE ) assert set(_SCREAMING_SNAKE_CASE ) == set(_SCREAMING_SNAKE_CASE ) assert len(_SCREAMING_SNAKE_CASE ) == len(_SCREAMING_SNAKE_CASE ) assert set(my.items() ) == set(py.items() ) def _a ( ) -> Dict: def is_public(_SCREAMING_SNAKE_CASE ) -> bool: return not name.startswith("""_""" ) snake_case_ = {name for name in dir({} ) if is_public(_SCREAMING_SNAKE_CASE )} snake_case_ = {name for name in dir(HashMap() ) if is_public(_SCREAMING_SNAKE_CASE )} assert dict_public_names > hash_public_names
2
"""simple docstring""" from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tokenizers_available, is_torch_available __SCREAMING_SNAKE_CASE : Optional[Any] = { 'configuration_biogpt': ['BIOGPT_PRETRAINED_CONFIG_ARCHIVE_MAP', 'BioGptConfig'], 'tokenization_biogpt': ['BioGptTokenizer'], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __SCREAMING_SNAKE_CASE : Optional[Any] = [ 'BIOGPT_PRETRAINED_MODEL_ARCHIVE_LIST', 'BioGptForCausalLM', 'BioGptForTokenClassification', 'BioGptForSequenceClassification', 'BioGptModel', 'BioGptPreTrainedModel', ] if TYPE_CHECKING: from .configuration_biogpt import BIOGPT_PRETRAINED_CONFIG_ARCHIVE_MAP, BioGptConfig from .tokenization_biogpt import BioGptTokenizer try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_biogpt import ( BIOGPT_PRETRAINED_MODEL_ARCHIVE_LIST, BioGptForCausalLM, BioGptForSequenceClassification, BioGptForTokenClassification, BioGptModel, BioGptPreTrainedModel, ) else: import sys __SCREAMING_SNAKE_CASE : Dict = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
2
1
"""simple docstring""" import os import tempfile import unittest from transformers import DistilBertConfig, is_torch_available from transformers.testing_utils import require_torch, require_torch_gpu, slow, torch_device from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, ids_tensor, random_attention_mask from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from transformers import ( DISTILBERT_PRETRAINED_MODEL_ARCHIVE_LIST, DistilBertForMaskedLM, DistilBertForMultipleChoice, DistilBertForQuestionAnswering, DistilBertForSequenceClassification, DistilBertForTokenClassification, DistilBertModel, ) class __A (snake_case__): '''simple docstring''' def __init__( self : Optional[Any] , UpperCAmelCase_ : Optional[int] , UpperCAmelCase_ : int=13 , UpperCAmelCase_ : List[str]=7 , UpperCAmelCase_ : Tuple=True , UpperCAmelCase_ : int=True , UpperCAmelCase_ : List[str]=False , UpperCAmelCase_ : Tuple=True , UpperCAmelCase_ : Union[str, Any]=99 , UpperCAmelCase_ : str=32 , UpperCAmelCase_ : int=5 , UpperCAmelCase_ : Union[str, Any]=4 , UpperCAmelCase_ : str=37 , UpperCAmelCase_ : Optional[Any]="gelu" , UpperCAmelCase_ : List[Any]=0.1 , UpperCAmelCase_ : Optional[Any]=0.1 , UpperCAmelCase_ : Optional[int]=512 , UpperCAmelCase_ : Union[str, Any]=16 , UpperCAmelCase_ : Optional[Any]=2 , UpperCAmelCase_ : Tuple=0.02 , UpperCAmelCase_ : int=3 , UpperCAmelCase_ : List[Any]=4 , UpperCAmelCase_ : List[Any]=None , ) ->Any: """simple docstring""" snake_case_ = parent snake_case_ = batch_size snake_case_ = seq_length snake_case_ = is_training snake_case_ = use_input_mask snake_case_ = use_token_type_ids snake_case_ = use_labels snake_case_ = vocab_size snake_case_ = hidden_size snake_case_ = num_hidden_layers snake_case_ = num_attention_heads snake_case_ = intermediate_size snake_case_ = hidden_act snake_case_ = hidden_dropout_prob snake_case_ = attention_probs_dropout_prob snake_case_ = max_position_embeddings snake_case_ = type_vocab_size snake_case_ = type_sequence_label_size snake_case_ = initializer_range snake_case_ = num_labels snake_case_ = num_choices snake_case_ = scope def lowerCAmelCase ( self : Tuple ) ->int: """simple docstring""" snake_case_ = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) snake_case_ = None if self.use_input_mask: snake_case_ = random_attention_mask([self.batch_size, self.seq_length] ) snake_case_ = None snake_case_ = None snake_case_ = None if self.use_labels: snake_case_ = ids_tensor([self.batch_size] , self.type_sequence_label_size ) snake_case_ = ids_tensor([self.batch_size, self.seq_length] , self.num_labels ) snake_case_ = ids_tensor([self.batch_size] , self.num_choices ) snake_case_ = self.get_config() return config, input_ids, input_mask, sequence_labels, token_labels, choice_labels def lowerCAmelCase ( self : List[Any] ) ->Union[str, Any]: """simple docstring""" return DistilBertConfig( vocab_size=self.vocab_size , dim=self.hidden_size , n_layers=self.num_hidden_layers , n_heads=self.num_attention_heads , hidden_dim=self.intermediate_size , hidden_act=self.hidden_act , dropout=self.hidden_dropout_prob , attention_dropout=self.attention_probs_dropout_prob , max_position_embeddings=self.max_position_embeddings , initializer_range=self.initializer_range , ) def lowerCAmelCase ( self : Optional[Any] , UpperCAmelCase_ : List[str] , UpperCAmelCase_ : int , UpperCAmelCase_ : Union[str, Any] , UpperCAmelCase_ : Union[str, Any] , UpperCAmelCase_ : List[str] , UpperCAmelCase_ : Tuple ) ->List[str]: """simple docstring""" snake_case_ = DistilBertModel(config=UpperCAmelCase_ ) model.to(UpperCAmelCase_ ) model.eval() snake_case_ = model(UpperCAmelCase_ , UpperCAmelCase_ ) snake_case_ = model(UpperCAmelCase_ ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def lowerCAmelCase ( self : Dict , UpperCAmelCase_ : Optional[int] , UpperCAmelCase_ : Optional[Any] , UpperCAmelCase_ : Any , UpperCAmelCase_ : List[Any] , UpperCAmelCase_ : Optional[Any] , UpperCAmelCase_ : Tuple ) ->int: """simple docstring""" snake_case_ = DistilBertForMaskedLM(config=UpperCAmelCase_ ) model.to(UpperCAmelCase_ ) model.eval() snake_case_ = model(UpperCAmelCase_ , attention_mask=UpperCAmelCase_ , labels=UpperCAmelCase_ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.vocab_size) ) def lowerCAmelCase ( self : int , UpperCAmelCase_ : List[Any] , UpperCAmelCase_ : int , UpperCAmelCase_ : Tuple , UpperCAmelCase_ : Union[str, Any] , UpperCAmelCase_ : int , UpperCAmelCase_ : Any ) ->Optional[Any]: """simple docstring""" snake_case_ = DistilBertForQuestionAnswering(config=UpperCAmelCase_ ) model.to(UpperCAmelCase_ ) model.eval() snake_case_ = model( UpperCAmelCase_ , attention_mask=UpperCAmelCase_ , start_positions=UpperCAmelCase_ , end_positions=UpperCAmelCase_ ) self.parent.assertEqual(result.start_logits.shape , (self.batch_size, self.seq_length) ) self.parent.assertEqual(result.end_logits.shape , (self.batch_size, self.seq_length) ) def lowerCAmelCase ( self : Union[str, Any] , UpperCAmelCase_ : Dict , UpperCAmelCase_ : Union[str, Any] , UpperCAmelCase_ : Optional[int] , UpperCAmelCase_ : List[str] , UpperCAmelCase_ : List[str] , UpperCAmelCase_ : Tuple ) ->Dict: """simple docstring""" snake_case_ = self.num_labels snake_case_ = DistilBertForSequenceClassification(UpperCAmelCase_ ) model.to(UpperCAmelCase_ ) model.eval() snake_case_ = model(UpperCAmelCase_ , attention_mask=UpperCAmelCase_ , labels=UpperCAmelCase_ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_labels) ) def lowerCAmelCase ( self : List[Any] , UpperCAmelCase_ : List[str] , UpperCAmelCase_ : List[str] , UpperCAmelCase_ : List[Any] , UpperCAmelCase_ : Union[str, Any] , UpperCAmelCase_ : Union[str, Any] , UpperCAmelCase_ : Optional[int] ) ->Dict: """simple docstring""" snake_case_ = self.num_labels snake_case_ = DistilBertForTokenClassification(config=UpperCAmelCase_ ) model.to(UpperCAmelCase_ ) model.eval() snake_case_ = model(UpperCAmelCase_ , attention_mask=UpperCAmelCase_ , labels=UpperCAmelCase_ ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.num_labels) ) def lowerCAmelCase ( self : Optional[Any] , UpperCAmelCase_ : int , UpperCAmelCase_ : Tuple , UpperCAmelCase_ : List[Any] , UpperCAmelCase_ : Tuple , UpperCAmelCase_ : int , UpperCAmelCase_ : Optional[Any] ) ->Tuple: """simple docstring""" snake_case_ = self.num_choices snake_case_ = DistilBertForMultipleChoice(config=UpperCAmelCase_ ) model.to(UpperCAmelCase_ ) model.eval() snake_case_ = input_ids.unsqueeze(1 ).expand(-1 , self.num_choices , -1 ).contiguous() snake_case_ = input_mask.unsqueeze(1 ).expand(-1 , self.num_choices , -1 ).contiguous() snake_case_ = model( UpperCAmelCase_ , attention_mask=UpperCAmelCase_ , labels=UpperCAmelCase_ , ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_choices) ) def lowerCAmelCase ( self : Optional[Any] ) ->Tuple: """simple docstring""" snake_case_ = self.prepare_config_and_inputs() ((snake_case_) , (snake_case_) , (snake_case_) , (snake_case_) , (snake_case_) , (snake_case_)) = config_and_inputs snake_case_ = {"""input_ids""": input_ids, """attention_mask""": input_mask} return config, inputs_dict @require_torch class __A (snake_case__ , snake_case__ , unittest.TestCase): '''simple docstring''' __lowercase: str = ( ( DistilBertModel, DistilBertForMaskedLM, DistilBertForMultipleChoice, DistilBertForQuestionAnswering, DistilBertForSequenceClassification, DistilBertForTokenClassification, ) if is_torch_available() else None ) __lowercase: Tuple = ( { """feature-extraction""": DistilBertModel, """fill-mask""": DistilBertForMaskedLM, """question-answering""": DistilBertForQuestionAnswering, """text-classification""": DistilBertForSequenceClassification, """token-classification""": DistilBertForTokenClassification, """zero-shot""": DistilBertForSequenceClassification, } if is_torch_available() else {} ) __lowercase: List[str] = True __lowercase: Tuple = True __lowercase: Dict = True __lowercase: Union[str, Any] = True def lowerCAmelCase ( self : Tuple ) ->List[str]: """simple docstring""" snake_case_ = DistilBertModelTester(self ) snake_case_ = ConfigTester(self , config_class=UpperCAmelCase_ , dim=37 ) def lowerCAmelCase ( self : Any ) ->Optional[Any]: """simple docstring""" self.config_tester.run_common_tests() def lowerCAmelCase ( self : Any ) ->Any: """simple docstring""" snake_case_ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_distilbert_model(*UpperCAmelCase_ ) def lowerCAmelCase ( self : int ) ->Union[str, Any]: """simple docstring""" snake_case_ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_distilbert_for_masked_lm(*UpperCAmelCase_ ) def lowerCAmelCase ( self : Any ) ->Optional[int]: """simple docstring""" snake_case_ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_distilbert_for_question_answering(*UpperCAmelCase_ ) def lowerCAmelCase ( self : str ) ->Any: """simple docstring""" snake_case_ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_distilbert_for_sequence_classification(*UpperCAmelCase_ ) def lowerCAmelCase ( self : Union[str, Any] ) ->str: """simple docstring""" snake_case_ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_distilbert_for_token_classification(*UpperCAmelCase_ ) def lowerCAmelCase ( self : str ) ->Union[str, Any]: """simple docstring""" snake_case_ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_distilbert_for_multiple_choice(*UpperCAmelCase_ ) @slow def lowerCAmelCase ( self : Any ) ->Union[str, Any]: """simple docstring""" for model_name in DISTILBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: snake_case_ = DistilBertModel.from_pretrained(UpperCAmelCase_ ) self.assertIsNotNone(UpperCAmelCase_ ) @slow @require_torch_gpu def lowerCAmelCase ( self : Union[str, Any] ) ->Dict: """simple docstring""" snake_case_ , snake_case_ = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: # BertForMultipleChoice behaves incorrectly in JIT environments. if model_class == DistilBertForMultipleChoice: return snake_case_ = True snake_case_ = model_class(config=UpperCAmelCase_ ) snake_case_ = self._prepare_for_class(UpperCAmelCase_ , UpperCAmelCase_ ) snake_case_ = torch.jit.trace( UpperCAmelCase_ , (inputs_dict["""input_ids"""].to("""cpu""" ), inputs_dict["""attention_mask"""].to("""cpu""" )) ) with tempfile.TemporaryDirectory() as tmp: torch.jit.save(UpperCAmelCase_ , os.path.join(UpperCAmelCase_ , """traced_model.pt""" ) ) snake_case_ = torch.jit.load(os.path.join(UpperCAmelCase_ , """traced_model.pt""" ) , map_location=UpperCAmelCase_ ) loaded(inputs_dict["""input_ids"""].to(UpperCAmelCase_ ) , inputs_dict["""attention_mask"""].to(UpperCAmelCase_ ) ) @require_torch class __A (unittest.TestCase): '''simple docstring''' @slow def lowerCAmelCase ( self : Optional[Any] ) ->List[Any]: """simple docstring""" snake_case_ = DistilBertModel.from_pretrained("""distilbert-base-uncased""" ) snake_case_ = torch.tensor([[0, 345, 232, 328, 740, 140, 1_695, 69, 6_078, 1_588, 2]] ) snake_case_ = torch.tensor([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]] ) with torch.no_grad(): snake_case_ = model(UpperCAmelCase_ , attention_mask=UpperCAmelCase_ )[0] snake_case_ = torch.Size((1, 11, 768) ) self.assertEqual(output.shape , UpperCAmelCase_ ) snake_case_ = torch.tensor( [[[-0.1_639, 0.3_299, 0.1_648], [-0.1_746, 0.3_289, 0.1_710], [-0.1_884, 0.3_357, 0.1_810]]] ) self.assertTrue(torch.allclose(output[:, 1:4, 1:4] , UpperCAmelCase_ , atol=1E-4 ) )
2
"""simple docstring""" __SCREAMING_SNAKE_CASE : str = 'Input must be a string of 8 numbers plus letter' __SCREAMING_SNAKE_CASE : Dict = 'TRWAGMYFPDXBNJZSQVHLCKE' def _a ( _SCREAMING_SNAKE_CASE ) -> bool: if not isinstance(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ): snake_case_ = f"""Expected string as input, found {type(_SCREAMING_SNAKE_CASE ).__name__}""" raise TypeError(_SCREAMING_SNAKE_CASE ) snake_case_ = spanish_id.replace("""-""" , """""" ).upper() if len(_SCREAMING_SNAKE_CASE ) != 9: raise ValueError(_SCREAMING_SNAKE_CASE ) try: snake_case_ = int(spanish_id_clean[0:8] ) snake_case_ = spanish_id_clean[8] except ValueError as ex: raise ValueError(_SCREAMING_SNAKE_CASE ) from ex if letter.isdigit(): raise ValueError(_SCREAMING_SNAKE_CASE ) return letter == LOOKUP_LETTERS[number % 23] if __name__ == "__main__": import doctest doctest.testmod()
2
1
"""simple docstring""" import warnings from ...configuration_utils import PretrainedConfig from ...utils import logging __SCREAMING_SNAKE_CASE : Optional[Any] = logging.get_logger(__name__) __SCREAMING_SNAKE_CASE : Tuple = { 'xlnet-base-cased': 'https://huggingface.co/xlnet-base-cased/resolve/main/config.json', 'xlnet-large-cased': 'https://huggingface.co/xlnet-large-cased/resolve/main/config.json', } class __A (snake_case__): '''simple docstring''' __lowercase: int = """xlnet""" __lowercase: Optional[Any] = ["""mems"""] __lowercase: List[Any] = { """n_token""": """vocab_size""", # Backward compatibility """hidden_size""": """d_model""", """num_attention_heads""": """n_head""", """num_hidden_layers""": """n_layer""", } def __init__( self : int , UpperCAmelCase_ : Tuple=32_000 , UpperCAmelCase_ : str=1_024 , UpperCAmelCase_ : Union[str, Any]=24 , UpperCAmelCase_ : Dict=16 , UpperCAmelCase_ : int=4_096 , UpperCAmelCase_ : str="gelu" , UpperCAmelCase_ : int=True , UpperCAmelCase_ : List[Any]="bi" , UpperCAmelCase_ : Union[str, Any]=0.02 , UpperCAmelCase_ : str=1E-12 , UpperCAmelCase_ : int=0.1 , UpperCAmelCase_ : Tuple=512 , UpperCAmelCase_ : Any=None , UpperCAmelCase_ : Any=True , UpperCAmelCase_ : Tuple=False , UpperCAmelCase_ : List[Any]=False , UpperCAmelCase_ : List[Any]=-1 , UpperCAmelCase_ : Union[str, Any]=False , UpperCAmelCase_ : Optional[Any]="last" , UpperCAmelCase_ : List[str]=True , UpperCAmelCase_ : Tuple="tanh" , UpperCAmelCase_ : Union[str, Any]=0.1 , UpperCAmelCase_ : Tuple=5 , UpperCAmelCase_ : Optional[int]=5 , UpperCAmelCase_ : Optional[int]=5 , UpperCAmelCase_ : Any=1 , UpperCAmelCase_ : Any=2 , **UpperCAmelCase_ : int , ) ->List[str]: """simple docstring""" snake_case_ = vocab_size snake_case_ = d_model snake_case_ = n_layer snake_case_ = n_head if d_model % n_head != 0: raise ValueError(F"""'d_model % n_head' ({d_model % n_head}) should be equal to 0""" ) if "d_head" in kwargs: if kwargs["d_head"] != d_model // n_head: raise ValueError( F"""`d_head` ({kwargs["d_head"]}) should be equal to `d_model // n_head` ({d_model // n_head})""" ) snake_case_ = d_model // n_head snake_case_ = ff_activation snake_case_ = d_inner snake_case_ = untie_r snake_case_ = attn_type snake_case_ = initializer_range snake_case_ = layer_norm_eps snake_case_ = dropout snake_case_ = mem_len snake_case_ = reuse_len snake_case_ = bi_data snake_case_ = clamp_len snake_case_ = same_length snake_case_ = summary_type snake_case_ = summary_use_proj snake_case_ = summary_activation snake_case_ = summary_last_dropout snake_case_ = start_n_top snake_case_ = end_n_top snake_case_ = bos_token_id snake_case_ = pad_token_id snake_case_ = eos_token_id if "use_cache" in kwargs: warnings.warn( """The `use_cache` argument is deprecated and will be removed in a future version, use `use_mems_eval`""" """ instead.""" , UpperCAmelCase_ , ) snake_case_ = kwargs["""use_cache"""] snake_case_ = use_mems_eval snake_case_ = use_mems_train super().__init__(pad_token_id=UpperCAmelCase_ , bos_token_id=UpperCAmelCase_ , eos_token_id=UpperCAmelCase_ , **UpperCAmelCase_ ) @property def lowerCAmelCase ( self : Union[str, Any] ) ->Union[str, Any]: """simple docstring""" logger.info(F"""The model {self.model_type} is one of the few models that has no sequence length limit.""" ) return -1 @max_position_embeddings.setter def lowerCAmelCase ( self : Dict , UpperCAmelCase_ : str ) ->str: """simple docstring""" raise NotImplementedError( F"""The model {self.model_type} is one of the few models that has no sequence length limit.""" )
2
"""simple docstring""" import os import unicodedata from shutil import copyfile from typing import Any, Dict, List, Optional, Tuple import sentencepiece as spm from ...tokenization_utils import AddedToken, PreTrainedTokenizer from ...utils import logging __SCREAMING_SNAKE_CASE : List[str] = logging.get_logger(__name__) __SCREAMING_SNAKE_CASE : Optional[int] = {'vocab_file': 'spiece.model'} __SCREAMING_SNAKE_CASE : 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', } } __SCREAMING_SNAKE_CASE : List[str] = { 'albert-base-v1': 512, 'albert-large-v1': 512, 'albert-xlarge-v1': 512, 'albert-xxlarge-v1': 512, 'albert-base-v2': 512, 'albert-large-v2': 512, 'albert-xlarge-v2': 512, 'albert-xxlarge-v2': 512, } __SCREAMING_SNAKE_CASE : int = '▁' class __A (snake_case__): '''simple docstring''' __lowercase: Optional[Any] = VOCAB_FILES_NAMES __lowercase: Optional[int] = PRETRAINED_VOCAB_FILES_MAP __lowercase: Tuple = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES def __init__( self : int , UpperCAmelCase_ : Any , UpperCAmelCase_ : Dict=True , UpperCAmelCase_ : Dict=True , UpperCAmelCase_ : List[str]=False , UpperCAmelCase_ : List[Any]="[CLS]" , UpperCAmelCase_ : Any="[SEP]" , UpperCAmelCase_ : str="<unk>" , UpperCAmelCase_ : str="[SEP]" , UpperCAmelCase_ : Optional[Any]="<pad>" , UpperCAmelCase_ : Optional[int]="[CLS]" , UpperCAmelCase_ : int="[MASK]" , UpperCAmelCase_ : Optional[Dict[str, Any]] = None , **UpperCAmelCase_ : Union[str, Any] , ) ->None: """simple docstring""" snake_case_ = ( AddedToken(UpperCAmelCase_ , lstrip=UpperCAmelCase_ , rstrip=UpperCAmelCase_ , normalized=UpperCAmelCase_ ) if isinstance(UpperCAmelCase_ , UpperCAmelCase_ ) else mask_token ) snake_case_ = {} if sp_model_kwargs is None else sp_model_kwargs super().__init__( do_lower_case=UpperCAmelCase_ , remove_space=UpperCAmelCase_ , keep_accents=UpperCAmelCase_ , bos_token=UpperCAmelCase_ , eos_token=UpperCAmelCase_ , unk_token=UpperCAmelCase_ , sep_token=UpperCAmelCase_ , pad_token=UpperCAmelCase_ , cls_token=UpperCAmelCase_ , mask_token=UpperCAmelCase_ , sp_model_kwargs=self.sp_model_kwargs , **UpperCAmelCase_ , ) snake_case_ = do_lower_case snake_case_ = remove_space snake_case_ = keep_accents snake_case_ = vocab_file snake_case_ = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(UpperCAmelCase_ ) @property def lowerCAmelCase ( self : List[Any] ) ->Dict: """simple docstring""" return len(self.sp_model ) def lowerCAmelCase ( self : str ) ->List[Any]: """simple docstring""" snake_case_ = {self.convert_ids_to_tokens(UpperCAmelCase_ ): i for i in range(self.vocab_size )} vocab.update(self.added_tokens_encoder ) return vocab def __getstate__( self : Dict ) ->List[str]: """simple docstring""" snake_case_ = self.__dict__.copy() snake_case_ = None return state def __setstate__( self : Tuple , UpperCAmelCase_ : Optional[int] ) ->Optional[int]: """simple docstring""" snake_case_ = d # for backward compatibility if not hasattr(self , """sp_model_kwargs""" ): snake_case_ = {} snake_case_ = spm.SentencePieceProcessor(**self.sp_model_kwargs ) self.sp_model.Load(self.vocab_file ) def lowerCAmelCase ( self : Tuple , UpperCAmelCase_ : Any ) ->str: """simple docstring""" if self.remove_space: snake_case_ = """ """.join(inputs.strip().split() ) else: snake_case_ = inputs snake_case_ = outputs.replace("""``""" , """\"""" ).replace("""''""" , """\"""" ) if not self.keep_accents: snake_case_ = unicodedata.normalize("""NFKD""" , UpperCAmelCase_ ) snake_case_ = """""".join([c for c in outputs if not unicodedata.combining(UpperCAmelCase_ )] ) if self.do_lower_case: snake_case_ = outputs.lower() return outputs def lowerCAmelCase ( self : Dict , UpperCAmelCase_ : str ) ->List[str]: """simple docstring""" snake_case_ = self.preprocess_text(UpperCAmelCase_ ) snake_case_ = self.sp_model.encode(UpperCAmelCase_ , out_type=UpperCAmelCase_ ) snake_case_ = [] for piece in pieces: if len(UpperCAmelCase_ ) > 1 and piece[-1] == str(""",""" ) and piece[-2].isdigit(): snake_case_ = self.sp_model.EncodeAsPieces(piece[:-1].replace(UpperCAmelCase_ , """""" ) ) if piece[0] != SPIECE_UNDERLINE and cur_pieces[0][0] == SPIECE_UNDERLINE: if len(cur_pieces[0] ) == 1: snake_case_ = cur_pieces[1:] else: snake_case_ = cur_pieces[0][1:] cur_pieces.append(piece[-1] ) new_pieces.extend(UpperCAmelCase_ ) else: new_pieces.append(UpperCAmelCase_ ) return new_pieces def lowerCAmelCase ( self : Optional[int] , UpperCAmelCase_ : Optional[int] ) ->Dict: """simple docstring""" return self.sp_model.PieceToId(UpperCAmelCase_ ) def lowerCAmelCase ( self : str , UpperCAmelCase_ : Union[str, Any] ) ->Optional[int]: """simple docstring""" return self.sp_model.IdToPiece(UpperCAmelCase_ ) def lowerCAmelCase ( self : str , UpperCAmelCase_ : Dict ) ->Any: """simple docstring""" snake_case_ = [] snake_case_ = """""" snake_case_ = 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(UpperCAmelCase_ ) + token snake_case_ = True snake_case_ = [] else: current_sub_tokens.append(UpperCAmelCase_ ) snake_case_ = False out_string += self.sp_model.decode(UpperCAmelCase_ ) return out_string.strip() def lowerCAmelCase ( self : Tuple , UpperCAmelCase_ : List[int] , UpperCAmelCase_ : Optional[List[int]] = None ) ->List[int]: """simple docstring""" snake_case_ = [self.sep_token_id] snake_case_ = [self.cls_token_id] if token_ids_a is None: return cls + token_ids_a + sep return cls + token_ids_a + sep + token_ids_a + sep def lowerCAmelCase ( self : Tuple , UpperCAmelCase_ : List[int] , UpperCAmelCase_ : Optional[List[int]] = None , UpperCAmelCase_ : bool = False ) ->List[int]: """simple docstring""" if already_has_special_tokens: return super().get_special_tokens_mask( token_ids_a=UpperCAmelCase_ , token_ids_a=UpperCAmelCase_ , already_has_special_tokens=UpperCAmelCase_ ) if token_ids_a is not None: return [1] + ([0] * len(UpperCAmelCase_ )) + [1] + ([0] * len(UpperCAmelCase_ )) + [1] return [1] + ([0] * len(UpperCAmelCase_ )) + [1] def lowerCAmelCase ( self : Dict , UpperCAmelCase_ : List[int] , UpperCAmelCase_ : Optional[List[int]] = None ) ->List[int]: """simple docstring""" snake_case_ = [self.sep_token_id] snake_case_ = [self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep ) * [0] return len(cls + token_ids_a + sep ) * [0] + len(token_ids_a + sep ) * [1] def lowerCAmelCase ( self : str , UpperCAmelCase_ : str , UpperCAmelCase_ : Optional[str] = None ) ->Tuple[str]: """simple docstring""" if not os.path.isdir(UpperCAmelCase_ ): logger.error(F"""Vocabulary path ({save_directory}) should be a directory""" ) return snake_case_ = os.path.join( UpperCAmelCase_ , (filename_prefix + """-""" if filename_prefix else """""") + VOCAB_FILES_NAMES["""vocab_file"""] ) if os.path.abspath(self.vocab_file ) != os.path.abspath(UpperCAmelCase_ ) and os.path.isfile(self.vocab_file ): copyfile(self.vocab_file , UpperCAmelCase_ ) elif not os.path.isfile(self.vocab_file ): with open(UpperCAmelCase_ , """wb""" ) as fi: snake_case_ = self.sp_model.serialized_model_proto() fi.write(UpperCAmelCase_ ) return (out_vocab_file,)
2
1
"""simple docstring""" from math import factorial def _a ( _SCREAMING_SNAKE_CASE = 20 ) -> int: snake_case_ = 2 * n # middle entry of odd rows starting at row 3 is the solution for n = 1, # 2, 3,... snake_case_ = n // 2 return int(factorial(_SCREAMING_SNAKE_CASE ) / (factorial(_SCREAMING_SNAKE_CASE ) * factorial(n - k )) ) if __name__ == "__main__": import sys if len(sys.argv) == 1: print(solution(20)) else: try: __SCREAMING_SNAKE_CASE : Optional[int] = int(sys.argv[1]) print(solution(n)) except ValueError: print('Invalid entry - please enter a number.')
2
"""simple docstring""" def _a ( _SCREAMING_SNAKE_CASE ) -> list: # bit count represents no. of bits in the gray code if bit_count < 0: raise ValueError("""The given input must be positive""" ) # get the generated string sequence snake_case_ = gray_code_sequence_string(_SCREAMING_SNAKE_CASE ) # # convert them to integers for i in range(len(_SCREAMING_SNAKE_CASE ) ): snake_case_ = int(sequence[i] , 2 ) return sequence def _a ( _SCREAMING_SNAKE_CASE ) -> list: # The approach is a recursive one # Base case achieved when either n = 0 or n=1 if bit_count == 0: return ["0"] if bit_count == 1: return ["0", "1"] snake_case_ = 1 << bit_count # defines the length of the sequence # 1<< n is equivalent to 2^n # recursive answer will generate answer for n-1 bits snake_case_ = gray_code_sequence_string(bit_count - 1 ) snake_case_ = [] # append 0 to first half of the smaller sequence generated for i in range(seq_len // 2 ): snake_case_ = """0""" + smaller_sequence[i] sequence.append(_SCREAMING_SNAKE_CASE ) # append 1 to second half ... start from the end of the list for i in reversed(range(seq_len // 2 ) ): snake_case_ = """1""" + smaller_sequence[i] sequence.append(_SCREAMING_SNAKE_CASE ) return sequence if __name__ == "__main__": import doctest doctest.testmod()
2
1
"""simple docstring""" def _a ( _SCREAMING_SNAKE_CASE ) -> int: snake_case_ = len(_SCREAMING_SNAKE_CASE ) snake_case_ = len(matrix[0] ) snake_case_ = min(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ) for row in range(_SCREAMING_SNAKE_CASE ): # Check if diagonal element is not zero if matrix[row][row] != 0: # Eliminate all the elements below the diagonal for col in range(row + 1 , _SCREAMING_SNAKE_CASE ): snake_case_ = matrix[col][row] / matrix[row][row] for i in range(_SCREAMING_SNAKE_CASE , _SCREAMING_SNAKE_CASE ): matrix[col][i] -= multiplier * matrix[row][i] else: # Find a non-zero diagonal element to swap rows snake_case_ = True for i in range(row + 1 , _SCREAMING_SNAKE_CASE ): if matrix[i][row] != 0: snake_case_ , snake_case_ = matrix[i], matrix[row] snake_case_ = False break if reduce: rank -= 1 for i in range(_SCREAMING_SNAKE_CASE ): snake_case_ = matrix[i][rank] # Reduce the row pointer by one to stay on the same row row -= 1 return rank if __name__ == "__main__": import doctest doctest.testmod()
2
"""simple docstring""" from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available __SCREAMING_SNAKE_CASE : Optional[Any] = { 'configuration_megatron_bert': ['MEGATRON_BERT_PRETRAINED_CONFIG_ARCHIVE_MAP', 'MegatronBertConfig'], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __SCREAMING_SNAKE_CASE : Tuple = [ 'MEGATRON_BERT_PRETRAINED_MODEL_ARCHIVE_LIST', 'MegatronBertForCausalLM', 'MegatronBertForMaskedLM', 'MegatronBertForMultipleChoice', 'MegatronBertForNextSentencePrediction', 'MegatronBertForPreTraining', 'MegatronBertForQuestionAnswering', 'MegatronBertForSequenceClassification', 'MegatronBertForTokenClassification', 'MegatronBertModel', 'MegatronBertPreTrainedModel', ] if TYPE_CHECKING: from .configuration_megatron_bert import MEGATRON_BERT_PRETRAINED_CONFIG_ARCHIVE_MAP, MegatronBertConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_megatron_bert import ( MEGATRON_BERT_PRETRAINED_MODEL_ARCHIVE_LIST, MegatronBertForCausalLM, MegatronBertForMaskedLM, MegatronBertForMultipleChoice, MegatronBertForNextSentencePrediction, MegatronBertForPreTraining, MegatronBertForQuestionAnswering, MegatronBertForSequenceClassification, MegatronBertForTokenClassification, MegatronBertModel, MegatronBertPreTrainedModel, ) else: import sys __SCREAMING_SNAKE_CASE : int = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
2
1