code
stringlengths
87
55.2k
code_codestyle
int64
0
349
style_context
stringlengths
135
49.1k
style_context_codestyle
int64
0
349
label
int64
0
1
# Copyright 2022 The HuggingFace Team. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import argparse import os import subprocess from packaging.version import Version, parse from accelerate.commands.config.config_args import default_config_file, load_config_from_file __a :str = 'Run commands across TPU VMs for initial setup before running `accelerate launch`.' def __snake_case ( __UpperCamelCase : Optional[Any]=None ): """simple docstring""" if subparsers is not None: A_ = subparsers.add_parser("tpu-config" ,description=_description ) else: A_ = argparse.ArgumentParser("Accelerate tpu-config command" ,description=_description ) # Core arguments A_ = parser.add_argument_group( "Config Arguments" ,"Arguments that can be configured through `accelerate config`." ) config_args.add_argument( "--config_file" ,type=__UpperCamelCase ,default=__UpperCamelCase ,help="Path to the config file to use for accelerate." ,) config_args.add_argument( "--tpu_name" ,default=__UpperCamelCase ,help="The name of the TPU to use. If not specified, will use the TPU specified in the config file." ,) config_args.add_argument( "--tpu_zone" ,default=__UpperCamelCase ,help="The zone of the TPU to use. If not specified, will use the zone specified in the config file." ,) A_ = parser.add_argument_group("TPU Arguments" ,"Arguments for options ran inside the TPU." ) pod_args.add_argument( "--use_alpha" ,action="store_true" ,help="Whether to use `gcloud alpha` when running the TPU training script instead of `gcloud`." ,) pod_args.add_argument( "--command_file" ,default=__UpperCamelCase ,help="The path to the file containing the commands to run on the pod on startup." ,) pod_args.add_argument( "--command" ,action="append" ,nargs="+" ,help="A command to run on the pod. Can be passed multiple times." ,) pod_args.add_argument( "--install_accelerate" ,action="store_true" ,help="Whether to install accelerate on the pod. Defaults to False." ,) pod_args.add_argument( "--accelerate_version" ,default="latest" ,help="The version of accelerate to install on the pod. If not specified, will use the latest pypi version. Specify 'dev' to install from GitHub." ,) pod_args.add_argument( "--debug" ,action="store_true" ,help="If set, will print the command that would be run instead of running it." ) if subparsers is not None: parser.set_defaults(func=__UpperCamelCase ) return parser def __snake_case ( __UpperCamelCase : int ): """simple docstring""" A_ = None # Get the default from the config file if it exists. if args.config_file is not None or os.path.isfile(__UpperCamelCase ): A_ = load_config_from_file(args.config_file ) if not args.command_file and defaults.command_file is not None and not args.command: A_ = defaults.command_file if not args.command and defaults.commands is not None: A_ = defaults.commands if not args.tpu_name: A_ = defaults.tpu_name if not args.tpu_zone: A_ = defaults.tpu_zone if args.accelerate_version == "dev": A_ = "git+https://github.com/huggingface/accelerate.git" elif args.accelerate_version == "latest": A_ = "accelerate -U" elif isinstance(parse(args.accelerate_version ) ,__UpperCamelCase ): A_ = f'''accelerate=={args.accelerate_version}''' if not args.command_file and not args.command: raise ValueError("You must specify either a command file or a command to run on the pod." ) if args.command_file: with open(args.command_file ,"r" ) as f: A_ = [f.read().splitlines()] # To turn list of lists into list of strings if isinstance(args.command[0] ,__UpperCamelCase ): A_ = [line for cmd in args.command for line in cmd] # Default to the shared folder and install accelerate A_ = ["cd /usr/share"] if args.install_accelerate: new_cmd += [f'''pip install {args.accelerate_version}'''] new_cmd += args.command A_ = "; ".join(__UpperCamelCase ) # Then send it to gcloud # Eventually try to use google-api-core to do this instead of subprocess A_ = ["gcloud"] if args.use_alpha: cmd += ["alpha"] cmd += [ "compute", "tpus", "tpu-vm", "ssh", args.tpu_name, "--zone", args.tpu_zone, "--command", args.command, "--worker", "all", ] if args.debug: print(f'''Running {" ".join(__UpperCamelCase )}''' ) return subprocess.run(__UpperCamelCase ) print("Successfully setup pod." ) def __snake_case ( ): """simple docstring""" A_ = tpu_command_parser() A_ = parser.parse_args() tpu_command_launcher(__UpperCamelCase )
312
import unittest from typing import Tuple import torch from diffusers.utils import floats_tensor, randn_tensor, torch_all_close, torch_device from diffusers.utils.testing_utils import require_torch @require_torch class _a : """simple docstring""" @property def __A ( self : Union[str, Any] ): return self.get_dummy_input() @property def __A ( self : int ): if self.block_type == "down": return (4, 32, 16, 16) elif self.block_type == "mid": return (4, 32, 32, 32) elif self.block_type == "up": return (4, 32, 64, 64) raise ValueError(f'''\'{self.block_type}\' is not a supported block_type. Set it to \'up\', \'mid\', or \'down\'.''' ) def __A ( self : Union[str, Any] , UpperCAmelCase : List[Any]=True , UpperCAmelCase : str=False , UpperCAmelCase : Tuple=False , UpperCAmelCase : Optional[Any]=False , ): A_ = 4 A_ = 32 A_ = (32, 32) A_ = torch.manual_seed(0 ) A_ = torch.device(UpperCAmelCase ) A_ = (batch_size, num_channels) + sizes A_ = randn_tensor(UpperCAmelCase , generator=UpperCAmelCase , device=UpperCAmelCase ) A_ = {"hidden_states": hidden_states} if include_temb: A_ = 128 A_ = randn_tensor((batch_size, temb_channels) , generator=UpperCAmelCase , device=UpperCAmelCase ) if include_res_hidden_states_tuple: A_ = torch.manual_seed(1 ) A_ = (randn_tensor(UpperCAmelCase , generator=UpperCAmelCase , device=UpperCAmelCase ),) if include_encoder_hidden_states: A_ = floats_tensor((batch_size, 32, 32) ).to(UpperCAmelCase ) if include_skip_sample: A_ = randn_tensor(((batch_size, 3) + sizes) , generator=UpperCAmelCase , device=UpperCAmelCase ) return dummy_input def __A ( self : Optional[int] ): A_ = { "in_channels": 32, "out_channels": 32, "temb_channels": 128, } if self.block_type == "up": A_ = 32 if self.block_type == "mid": init_dict.pop("out_channels" ) A_ = self.dummy_input return init_dict, inputs_dict def __A ( self : List[str] , UpperCAmelCase : Optional[Any] ): A_ , A_ = self.prepare_init_args_and_inputs_for_common() A_ = self.block_class(**UpperCAmelCase ) unet_block.to(UpperCAmelCase ) unet_block.eval() with torch.no_grad(): A_ = unet_block(**UpperCAmelCase ) if isinstance(UpperCAmelCase , UpperCAmelCase ): A_ = output[0] self.assertEqual(output.shape , self.output_shape ) A_ = output[0, -1, -3:, -3:] A_ = torch.tensor(UpperCAmelCase ).to(UpperCAmelCase ) assert torch_all_close(output_slice.flatten() , UpperCAmelCase , atol=5E-3 ) @unittest.skipIf(torch_device == "mps" , "Training is not supported in mps" ) def __A ( self : Union[str, Any] ): A_ , A_ = self.prepare_init_args_and_inputs_for_common() A_ = self.block_class(**UpperCAmelCase ) model.to(UpperCAmelCase ) model.train() A_ = model(**UpperCAmelCase ) if isinstance(UpperCAmelCase , UpperCAmelCase ): A_ = output[0] A_ = torch.device(UpperCAmelCase ) A_ = randn_tensor(output.shape , device=UpperCAmelCase ) A_ = torch.nn.functional.mse_loss(UpperCAmelCase , UpperCAmelCase ) loss.backward()
312
1
def __snake_case ( __UpperCamelCase : int ): """simple docstring""" if isinstance(__UpperCamelCase ,__UpperCamelCase ): raise TypeError("'float' object cannot be interpreted as an integer" ) if isinstance(__UpperCamelCase ,__UpperCamelCase ): raise TypeError("'str' object cannot be interpreted as an integer" ) if num == 0: return "0b0" A_ = False if num < 0: A_ = True A_ = -num A_ = [] while num > 0: binary.insert(0 ,num % 2 ) num >>= 1 if negative: return "-0b" + "".join(str(__UpperCamelCase ) for e in binary ) return "0b" + "".join(str(__UpperCamelCase ) for e in binary ) if __name__ == "__main__": import doctest doctest.testmod()
312
import copy import fnmatch import json import os import pickle as pkl import shutil import sys import tarfile import tempfile from collections import OrderedDict from contextlib import contextmanager from functools import partial from hashlib import shaaaa from io import BytesIO from pathlib import Path from urllib.parse import urlparse from zipfile import ZipFile, is_zipfile import cva import numpy as np import requests import wget from filelock import FileLock from PIL import Image from tqdm.auto import tqdm from yaml import Loader, dump, load try: import torch __a :int = True except ImportError: __a :Optional[Any] = False try: from torch.hub import _get_torch_home __a :Optional[Any] = _get_torch_home() except ImportError: __a :Tuple = os.path.expanduser( os.getenv('TORCH_HOME', os.path.join(os.getenv('XDG_CACHE_HOME', '~/.cache'), 'torch')) ) __a :Optional[Any] = os.path.join(torch_cache_home, 'transformers') __a :int = 'https://cdn.huggingface.co' __a :Any = 'https://s3.amazonaws.com/models.huggingface.co/bert' __a :Optional[Any] = '/'.join(str(Path(__file__).resolve()).split('/')[:-1]) __a :str = os.path.join(PATH, 'config.yaml') __a :str = os.path.join(PATH, 'attributes.txt') __a :Optional[Any] = os.path.join(PATH, 'objects.txt') __a :Optional[int] = os.getenv('PYTORCH_PRETRAINED_BERT_CACHE', default_cache_path) __a :Dict = os.getenv('PYTORCH_TRANSFORMERS_CACHE', PYTORCH_PRETRAINED_BERT_CACHE) __a :List[Any] = os.getenv('TRANSFORMERS_CACHE', PYTORCH_TRANSFORMERS_CACHE) __a :List[str] = 'pytorch_model.bin' __a :Tuple = 'config.yaml' def __snake_case ( __UpperCamelCase : Optional[Any]=OBJECTS ,__UpperCamelCase : List[str]=ATTRIBUTES ): """simple docstring""" A_ = [] with open(__UpperCamelCase ) as f: for object in f.readlines(): vg_classes.append(object.split("," )[0].lower().strip() ) A_ = [] with open(__UpperCamelCase ) as f: for object in f.readlines(): vg_attrs.append(object.split("," )[0].lower().strip() ) return vg_classes, vg_attrs def __snake_case ( __UpperCamelCase : List[Any] ): """simple docstring""" A_ = OrderedDict() with open(__UpperCamelCase ,"rb" ) as f: A_ = pkl.load(__UpperCamelCase )["model"] for k in copy.deepcopy(list(ckp.keys() ) ): A_ = ckp.pop(__UpperCamelCase ) if isinstance(__UpperCamelCase ,np.ndarray ): A_ = torch.tensor(__UpperCamelCase ) else: assert isinstance(__UpperCamelCase ,torch.tensor ), type(__UpperCamelCase ) A_ = v return r class _a : """simple docstring""" _lowerCamelCase : Union[str, Any] = {} def __init__( self : str , UpperCAmelCase : dict , UpperCAmelCase : str = "root" , UpperCAmelCase : List[str]=0 ): A_ = name A_ = level A_ = {} for k, v in dictionary.items(): if v is None: raise ValueError() A_ = copy.deepcopy(UpperCAmelCase ) A_ = copy.deepcopy(UpperCAmelCase ) if isinstance(UpperCAmelCase , UpperCAmelCase ): A_ = Config(UpperCAmelCase , name=UpperCAmelCase , level=level + 1 ) A_ = v setattr(self , UpperCAmelCase , UpperCAmelCase ) A_ = d def __repr__( self : Optional[Any] ): return str(list((self._pointer.keys()) ) ) def __setattr__( self : Any , UpperCAmelCase : Any , UpperCAmelCase : Any ): A_ = val A_ = val A_ = key.split("." ) A_ = len(UpperCAmelCase ) - 1 A_ = self._pointer if len(UpperCAmelCase ) > 1: for i, l in enumerate(UpperCAmelCase ): if hasattr(self , UpperCAmelCase ) and isinstance(getattr(self , UpperCAmelCase ) , UpperCAmelCase ): setattr(getattr(self , UpperCAmelCase ) , ".".join(levels[i:] ) , UpperCAmelCase ) if l == last_level: A_ = val else: A_ = pointer[l] def __A ( self : List[str] ): return self._pointer def __A ( self : int , UpperCAmelCase : Tuple , UpperCAmelCase : int ): with open(f'''{file_name}''' , "w" ) as stream: dump(UpperCAmelCase , UpperCAmelCase ) def __A ( self : List[Any] , UpperCAmelCase : str , UpperCAmelCase : Tuple ): with open(f'''{file_name}''' , "w" ) as stream: json.dump(UpperCAmelCase , UpperCAmelCase ) @staticmethod def __A ( UpperCAmelCase : Optional[int] ): with open(UpperCAmelCase ) as stream: A_ = load(UpperCAmelCase , Loader=UpperCAmelCase ) return data def __str__( self : str ): A_ = " " if self._name != "root": A_ = f'''{t * (self._level-1)}{self._name}:\n''' else: A_ = "" A_ = self._level for i, (k, v) in enumerate(self._pointer.items() ): if isinstance(UpperCAmelCase , UpperCAmelCase ): r += f'''{t * (self._level)}{v}\n''' self._level += 1 else: r += f'''{t * (self._level)}{k}: {v} ({type(UpperCAmelCase ).__name__})\n''' A_ = level return r[:-1] @classmethod def __A ( cls : Optional[Any] , UpperCAmelCase : str , **UpperCAmelCase : str ): A_ , A_ = cls.get_config_dict(UpperCAmelCase , **UpperCAmelCase ) return cls(UpperCAmelCase ) @classmethod def __A ( cls : int , UpperCAmelCase : str , **UpperCAmelCase : int ): A_ = kwargs.pop("cache_dir" , UpperCAmelCase ) A_ = kwargs.pop("force_download" , UpperCAmelCase ) A_ = kwargs.pop("resume_download" , UpperCAmelCase ) A_ = kwargs.pop("proxies" , UpperCAmelCase ) A_ = kwargs.pop("local_files_only" , UpperCAmelCase ) if os.path.isdir(UpperCAmelCase ): A_ = os.path.join(UpperCAmelCase , UpperCAmelCase ) elif os.path.isfile(UpperCAmelCase ) or is_remote_url(UpperCAmelCase ): A_ = pretrained_model_name_or_path else: A_ = hf_bucket_url(UpperCAmelCase , filename=UpperCAmelCase , use_cdn=UpperCAmelCase ) try: # Load from URL or cache if already cached A_ = cached_path( UpperCAmelCase , cache_dir=UpperCAmelCase , force_download=UpperCAmelCase , proxies=UpperCAmelCase , resume_download=UpperCAmelCase , local_files_only=UpperCAmelCase , ) # Load config dict if resolved_config_file is None: raise EnvironmentError A_ = Config.load_yaml(UpperCAmelCase ) except EnvironmentError: A_ = "Can't load config for" raise EnvironmentError(UpperCAmelCase ) if resolved_config_file == config_file: print("loading configuration file from path" ) else: print("loading configuration file cache" ) return Config.load_yaml(UpperCAmelCase ), kwargs def __snake_case ( __UpperCamelCase : Union[str, Any] ): """simple docstring""" A_ = torch.load("dump.pt" ,map_location=in_tensor.device ) A_ = in_tensor.numpy() A_ = out_tensor.numpy()[0] print(na.shape ,na[0, 0, :5] ) print(na.shape ,na[0, 0, :5] ) assert np.allclose(__UpperCamelCase ,__UpperCamelCase ,rtol=0.01 ,atol=0.1 ), ( f'''{sum([1 for x in np.isclose(__UpperCamelCase ,__UpperCamelCase ,rtol=0.01 ,atol=0.1 ).flatten() if x is False] )/len(na.flatten() )*100:.4f} %''' " element-wise mismatch" ) raise Exception("tensors are all good" ) # Hugging face functions below def __snake_case ( __UpperCamelCase : Optional[int] ): """simple docstring""" A_ = urlparse(__UpperCamelCase ) return parsed.scheme in ("http", "https") def __snake_case ( __UpperCamelCase : str ,__UpperCamelCase : str ,__UpperCamelCase : str=True ): """simple docstring""" A_ = CLOUDFRONT_DISTRIB_PREFIX if use_cdn else S3_BUCKET_PREFIX A_ = "/" not in model_id if legacy_format: return f'''{endpoint}/{model_id}-{filename}''' else: return f'''{endpoint}/{model_id}/{filename}''' def __snake_case ( __UpperCamelCase : List[str] ,__UpperCamelCase : Union[str, Any] ,__UpperCamelCase : List[str]=None ,__UpperCamelCase : int=0 ,__UpperCamelCase : int=None ,): """simple docstring""" A_ = "python/{}".format(sys.version.split()[0] ) if _torch_available: ua += "; torch/{}".format(torch.__version__ ) if isinstance(__UpperCamelCase ,__UpperCamelCase ): ua += "; " + "; ".join("{}/{}".format(__UpperCamelCase ,__UpperCamelCase ) for k, v in user_agent.items() ) elif isinstance(__UpperCamelCase ,__UpperCamelCase ): ua += "; " + user_agent A_ = {"user-agent": ua} if resume_size > 0: A_ = "bytes=%d-" % (resume_size,) A_ = requests.get(__UpperCamelCase ,stream=__UpperCamelCase ,proxies=__UpperCamelCase ,headers=__UpperCamelCase ) if response.status_code == 416: # Range not satisfiable return A_ = response.headers.get("Content-Length" ) A_ = resume_size + int(__UpperCamelCase ) if content_length is not None else None A_ = tqdm( unit="B" ,unit_scale=__UpperCamelCase ,total=__UpperCamelCase ,initial=__UpperCamelCase ,desc="Downloading" ,) for chunk in response.iter_content(chunk_size=1024 ): if chunk: # filter out keep-alive new chunks progress.update(len(__UpperCamelCase ) ) temp_file.write(__UpperCamelCase ) progress.close() def __snake_case ( __UpperCamelCase : str ,__UpperCamelCase : Any=None ,__UpperCamelCase : Dict=False ,__UpperCamelCase : Union[str, Any]=None ,__UpperCamelCase : Any=10 ,__UpperCamelCase : int=False ,__UpperCamelCase : Optional[Any]=None ,__UpperCamelCase : str=False ,): """simple docstring""" if cache_dir is None: A_ = TRANSFORMERS_CACHE if isinstance(__UpperCamelCase ,__UpperCamelCase ): A_ = str(__UpperCamelCase ) os.makedirs(__UpperCamelCase ,exist_ok=__UpperCamelCase ) A_ = None if not local_files_only: try: A_ = requests.head(__UpperCamelCase ,allow_redirects=__UpperCamelCase ,proxies=__UpperCamelCase ,timeout=__UpperCamelCase ) if response.status_code == 200: A_ = response.headers.get("ETag" ) except (EnvironmentError, requests.exceptions.Timeout): # etag is already None pass A_ = url_to_filename(__UpperCamelCase ,__UpperCamelCase ) # get cache path to put the file A_ = os.path.join(__UpperCamelCase ,__UpperCamelCase ) # etag is None = we don't have a connection, or url doesn't exist, or is otherwise inaccessible. # try to get the last downloaded one if etag is None: if os.path.exists(__UpperCamelCase ): return cache_path else: A_ = [ file for file in fnmatch.filter(os.listdir(__UpperCamelCase ) ,filename + ".*" ) if not file.endswith(".json" ) and not file.endswith(".lock" ) ] if len(__UpperCamelCase ) > 0: return os.path.join(__UpperCamelCase ,matching_files[-1] ) else: # If files cannot be found and local_files_only=True, # the models might've been found if local_files_only=False # Notify the user about that if local_files_only: raise ValueError( "Cannot find the requested files in the cached path and outgoing traffic has been" " disabled. To enable model look-ups and downloads online, set 'local_files_only'" " to False." ) return None # From now on, etag is not None. if os.path.exists(__UpperCamelCase ) and not force_download: return cache_path # Prevent parallel downloads of the same file with a lock. A_ = cache_path + ".lock" with FileLock(__UpperCamelCase ): # If the download just completed while the lock was activated. if os.path.exists(__UpperCamelCase ) and not force_download: # Even if returning early like here, the lock will be released. return cache_path if resume_download: A_ = cache_path + ".incomplete" @contextmanager def _resumable_file_manager(): with open(__UpperCamelCase ,"a+b" ) as f: yield f A_ = _resumable_file_manager if os.path.exists(__UpperCamelCase ): A_ = os.stat(__UpperCamelCase ).st_size else: A_ = 0 else: A_ = partial(tempfile.NamedTemporaryFile ,dir=__UpperCamelCase ,delete=__UpperCamelCase ) A_ = 0 # Download to temporary file, then copy to cache dir once finished. # Otherwise you get corrupt cache entries if the download gets interrupted. with temp_file_manager() as temp_file: print( "%s not found in cache or force_download set to True, downloading to %s" ,__UpperCamelCase ,temp_file.name ,) http_get( __UpperCamelCase ,__UpperCamelCase ,proxies=__UpperCamelCase ,resume_size=__UpperCamelCase ,user_agent=__UpperCamelCase ,) os.replace(temp_file.name ,__UpperCamelCase ) A_ = {"url": url, "etag": etag} A_ = cache_path + ".json" with open(__UpperCamelCase ,"w" ) as meta_file: json.dump(__UpperCamelCase ,__UpperCamelCase ) return cache_path def __snake_case ( __UpperCamelCase : List[Any] ,__UpperCamelCase : str=None ): """simple docstring""" A_ = url.encode("utf-8" ) A_ = shaaaa(__UpperCamelCase ) A_ = url_hash.hexdigest() if etag: A_ = etag.encode("utf-8" ) A_ = shaaaa(__UpperCamelCase ) filename += "." + etag_hash.hexdigest() if url.endswith(".h5" ): filename += ".h5" return filename def __snake_case ( __UpperCamelCase : Union[str, Any] ,__UpperCamelCase : Union[str, Any]=None ,__UpperCamelCase : List[Any]=False ,__UpperCamelCase : List[str]=None ,__UpperCamelCase : Any=False ,__UpperCamelCase : Optional[int]=None ,__UpperCamelCase : Optional[Any]=False ,__UpperCamelCase : Dict=False ,__UpperCamelCase : Optional[Any]=False ,): """simple docstring""" if cache_dir is None: A_ = TRANSFORMERS_CACHE if isinstance(__UpperCamelCase ,__UpperCamelCase ): A_ = str(__UpperCamelCase ) if isinstance(__UpperCamelCase ,__UpperCamelCase ): A_ = str(__UpperCamelCase ) if is_remote_url(__UpperCamelCase ): # URL, so get it from the cache (downloading if necessary) A_ = get_from_cache( __UpperCamelCase ,cache_dir=__UpperCamelCase ,force_download=__UpperCamelCase ,proxies=__UpperCamelCase ,resume_download=__UpperCamelCase ,user_agent=__UpperCamelCase ,local_files_only=__UpperCamelCase ,) elif os.path.exists(__UpperCamelCase ): # File, and it exists. A_ = url_or_filename elif urlparse(__UpperCamelCase ).scheme == "": # File, but it doesn't exist. raise EnvironmentError("file {} not found".format(__UpperCamelCase ) ) else: # Something unknown raise ValueError("unable to parse {} as a URL or as a local path".format(__UpperCamelCase ) ) if extract_compressed_file: if not is_zipfile(__UpperCamelCase ) and not tarfile.is_tarfile(__UpperCamelCase ): return output_path # Path where we extract compressed archives # We avoid '.' in dir name and add "-extracted" at the end: "./model.zip" => "./model-zip-extracted/" A_ , A_ = os.path.split(__UpperCamelCase ) A_ = output_file.replace("." ,"-" ) + "-extracted" A_ = os.path.join(__UpperCamelCase ,__UpperCamelCase ) if os.path.isdir(__UpperCamelCase ) and os.listdir(__UpperCamelCase ) and not force_extract: return output_path_extracted # Prevent parallel extractions A_ = output_path + ".lock" with FileLock(__UpperCamelCase ): shutil.rmtree(__UpperCamelCase ,ignore_errors=__UpperCamelCase ) os.makedirs(__UpperCamelCase ) if is_zipfile(__UpperCamelCase ): with ZipFile(__UpperCamelCase ,"r" ) as zip_file: zip_file.extractall(__UpperCamelCase ) zip_file.close() elif tarfile.is_tarfile(__UpperCamelCase ): A_ = tarfile.open(__UpperCamelCase ) tar_file.extractall(__UpperCamelCase ) tar_file.close() else: raise EnvironmentError("Archive format of {} could not be identified".format(__UpperCamelCase ) ) return output_path_extracted return output_path def __snake_case ( __UpperCamelCase : str ,__UpperCamelCase : Any="," ): """simple docstring""" assert isinstance(__UpperCamelCase ,__UpperCamelCase ) if os.path.isfile(__UpperCamelCase ): with open(__UpperCamelCase ) as f: A_ = eval(f.read() ) else: A_ = requests.get(__UpperCamelCase ) try: A_ = requests.json() except Exception: A_ = req.content.decode() assert data is not None, "could not connect" try: A_ = eval(__UpperCamelCase ) except Exception: A_ = data.split("\n" ) req.close() return data def __snake_case ( __UpperCamelCase : int ): """simple docstring""" A_ = requests.get(__UpperCamelCase ) A_ = np.array(Image.open(BytesIO(response.content ) ) ) return img def __snake_case ( __UpperCamelCase : Tuple ): """simple docstring""" A_ = url.split("/" )[-1] if fn not in os.listdir(os.getcwd() ): wget.download(__UpperCamelCase ) with open(__UpperCamelCase ,"rb" ) as stream: A_ = pkl.load(__UpperCamelCase ) A_ = weights.pop("model" ) A_ = {} for k, v in model.items(): A_ = torch.from_numpy(__UpperCamelCase ) if "running_var" in k: A_ = torch.tensor([0] ) A_ = k.replace("running_var" ,"num_batches_tracked" ) A_ = zero return new def __snake_case ( ): """simple docstring""" print(f'''{os.path.abspath(os.path.join(__UpperCamelCase ,os.pardir ) )}/demo.ipynb''' ) def __snake_case ( __UpperCamelCase : Optional[Any] ,__UpperCamelCase : Optional[int]="RGB" ): """simple docstring""" assert isinstance(__UpperCamelCase ,__UpperCamelCase ) if os.path.isfile(__UpperCamelCase ): A_ = cva.imread(__UpperCamelCase ) else: A_ = get_image_from_url(__UpperCamelCase ) assert img is not None, f'''could not connect to: {im}''' A_ = cva.cvtColor(__UpperCamelCase ,cva.COLOR_BGR2RGB ) if input_format == "RGB": A_ = img[:, :, ::-1] return img def __snake_case ( __UpperCamelCase : List[str] ,__UpperCamelCase : List[str]=1 ): """simple docstring""" return (images[i : i + batch] for i in range(0 ,len(__UpperCamelCase ) ,__UpperCamelCase ))
312
1
from collections import OrderedDict from typing import Mapping from packaging import version from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging __a :List[Any] = logging.get_logger(__name__) __a :Optional[int] = { 'microsoft/beit-base-patch16-224-pt22k': ( 'https://huggingface.co/microsoft/beit-base-patch16-224-pt22k/resolve/main/config.json' ), # See all BEiT models at https://huggingface.co/models?filter=beit } class _a ( snake_case_ ): """simple docstring""" _lowerCamelCase : int = 'beit' def __init__( self : Optional[Any] , UpperCAmelCase : Dict=8192 , UpperCAmelCase : Optional[Any]=768 , UpperCAmelCase : str=12 , UpperCAmelCase : Union[str, Any]=12 , UpperCAmelCase : List[Any]=3072 , UpperCAmelCase : Union[str, Any]="gelu" , UpperCAmelCase : int=0.0 , UpperCAmelCase : Optional[Any]=0.0 , UpperCAmelCase : Dict=0.02 , UpperCAmelCase : str=1E-12 , UpperCAmelCase : Union[str, Any]=224 , UpperCAmelCase : Union[str, Any]=16 , UpperCAmelCase : List[str]=3 , UpperCAmelCase : int=False , UpperCAmelCase : List[Any]=False , UpperCAmelCase : Union[str, Any]=False , UpperCAmelCase : Union[str, Any]=False , UpperCAmelCase : List[Any]=0.1 , UpperCAmelCase : Tuple=0.1 , UpperCAmelCase : Dict=True , UpperCAmelCase : Dict=[3, 5, 7, 11] , UpperCAmelCase : Union[str, Any]=[1, 2, 3, 6] , UpperCAmelCase : List[str]=True , UpperCAmelCase : str=0.4 , UpperCAmelCase : Optional[int]=256 , UpperCAmelCase : List[str]=1 , UpperCAmelCase : Any=False , UpperCAmelCase : List[Any]=255 , **UpperCAmelCase : List[Any] , ): super().__init__(**UpperCAmelCase ) A_ = vocab_size A_ = hidden_size A_ = num_hidden_layers A_ = num_attention_heads A_ = intermediate_size A_ = hidden_act A_ = hidden_dropout_prob A_ = attention_probs_dropout_prob A_ = initializer_range A_ = layer_norm_eps A_ = image_size A_ = patch_size A_ = num_channels A_ = use_mask_token A_ = use_absolute_position_embeddings A_ = use_relative_position_bias A_ = use_shared_relative_position_bias A_ = layer_scale_init_value A_ = drop_path_rate A_ = use_mean_pooling # decode head attributes (semantic segmentation) A_ = out_indices A_ = pool_scales # auxiliary head attributes (semantic segmentation) A_ = use_auxiliary_head A_ = auxiliary_loss_weight A_ = auxiliary_channels A_ = auxiliary_num_convs A_ = auxiliary_concat_input A_ = semantic_loss_ignore_index class _a ( snake_case_ ): """simple docstring""" _lowerCamelCase : str = version.parse('1.11' ) @property def __A ( self : Dict ): return OrderedDict( [ ("pixel_values", {0: "batch", 1: "num_channels", 2: "height", 3: "width"}), ] ) @property def __A ( self : Dict ): return 1E-4
312
from __future__ import annotations def __snake_case ( __UpperCamelCase : list[list[int]] ): """simple docstring""" for i in range(1 ,len(matrix[0] ) ): matrix[0][i] += matrix[0][i - 1] # preprocessing the first column for i in range(1 ,len(__UpperCamelCase ) ): matrix[i][0] += matrix[i - 1][0] # updating the path cost for current position for i in range(1 ,len(__UpperCamelCase ) ): for j in range(1 ,len(matrix[0] ) ): matrix[i][j] += min(matrix[i - 1][j] ,matrix[i][j - 1] ) return matrix[-1][-1] if __name__ == "__main__": import doctest doctest.testmod()
312
1
import random import unittest import numpy as np from diffusers import ( DPMSolverMultistepScheduler, EulerAncestralDiscreteScheduler, EulerDiscreteScheduler, LMSDiscreteScheduler, OnnxStableDiffusionImgaImgPipeline, PNDMScheduler, ) from diffusers.utils import floats_tensor from diffusers.utils.testing_utils import ( is_onnx_available, load_image, nightly, require_onnxruntime, require_torch_gpu, ) from ..test_pipelines_onnx_common import OnnxPipelineTesterMixin if is_onnx_available(): import onnxruntime as ort class _a ( snake_case_ , unittest.TestCase ): """simple docstring""" _lowerCamelCase : List[str] = 'hf-internal-testing/tiny-random-OnnxStableDiffusionPipeline' def __A ( self : Optional[Any] , UpperCAmelCase : List[str]=0 ): A_ = floats_tensor((1, 3, 128, 128) , rng=random.Random(UpperCAmelCase ) ) A_ = np.random.RandomState(UpperCAmelCase ) A_ = { "prompt": "A painting of a squirrel eating a burger", "image": image, "generator": generator, "num_inference_steps": 3, "strength": 0.75, "guidance_scale": 7.5, "output_type": "numpy", } return inputs def __A ( self : Tuple ): A_ = OnnxStableDiffusionImgaImgPipeline.from_pretrained(self.hub_checkpoint , provider="CPUExecutionProvider" ) pipe.set_progress_bar_config(disable=UpperCAmelCase ) A_ = self.get_dummy_inputs() A_ = pipe(**UpperCAmelCase ).images A_ = image[0, -3:, -3:, -1].flatten() assert image.shape == (1, 128, 128, 3) A_ = np.array([0.69_643, 0.58_484, 0.50_314, 0.58_760, 0.55_368, 0.59_643, 0.51_529, 0.41_217, 0.49_087] ) assert np.abs(image_slice - expected_slice ).max() < 1E-1 def __A ( self : List[str] ): A_ = OnnxStableDiffusionImgaImgPipeline.from_pretrained(self.hub_checkpoint , provider="CPUExecutionProvider" ) A_ = PNDMScheduler.from_config(pipe.scheduler.config , skip_prk_steps=UpperCAmelCase ) pipe.set_progress_bar_config(disable=UpperCAmelCase ) A_ = self.get_dummy_inputs() A_ = pipe(**UpperCAmelCase ).images A_ = image[0, -3:, -3:, -1] assert image.shape == (1, 128, 128, 3) A_ = np.array([0.61_737, 0.54_642, 0.53_183, 0.54_465, 0.52_742, 0.60_525, 0.49_969, 0.40_655, 0.48_154] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-1 def __A ( self : int ): A_ = OnnxStableDiffusionImgaImgPipeline.from_pretrained(self.hub_checkpoint , provider="CPUExecutionProvider" ) A_ = LMSDiscreteScheduler.from_config(pipe.scheduler.config ) pipe.set_progress_bar_config(disable=UpperCAmelCase ) # warmup pass to apply optimizations A_ = pipe(**self.get_dummy_inputs() ) A_ = self.get_dummy_inputs() A_ = pipe(**UpperCAmelCase ).images A_ = image[0, -3:, -3:, -1] assert image.shape == (1, 128, 128, 3) A_ = np.array([0.52_761, 0.59_977, 0.49_033, 0.49_619, 0.54_282, 0.50_311, 0.47_600, 0.40_918, 0.45_203] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-1 def __A ( self : Dict ): A_ = OnnxStableDiffusionImgaImgPipeline.from_pretrained(self.hub_checkpoint , provider="CPUExecutionProvider" ) A_ = EulerDiscreteScheduler.from_config(pipe.scheduler.config ) pipe.set_progress_bar_config(disable=UpperCAmelCase ) A_ = self.get_dummy_inputs() A_ = pipe(**UpperCAmelCase ).images A_ = image[0, -3:, -3:, -1] assert image.shape == (1, 128, 128, 3) A_ = np.array([0.52_911, 0.60_004, 0.49_229, 0.49_805, 0.54_502, 0.50_680, 0.47_777, 0.41_028, 0.45_304] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-1 def __A ( self : List[str] ): A_ = OnnxStableDiffusionImgaImgPipeline.from_pretrained(self.hub_checkpoint , provider="CPUExecutionProvider" ) A_ = EulerAncestralDiscreteScheduler.from_config(pipe.scheduler.config ) pipe.set_progress_bar_config(disable=UpperCAmelCase ) A_ = self.get_dummy_inputs() A_ = pipe(**UpperCAmelCase ).images A_ = image[0, -3:, -3:, -1] assert image.shape == (1, 128, 128, 3) A_ = np.array([0.52_911, 0.60_004, 0.49_229, 0.49_805, 0.54_502, 0.50_680, 0.47_777, 0.41_028, 0.45_304] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-1 def __A ( self : Tuple ): A_ = OnnxStableDiffusionImgaImgPipeline.from_pretrained(self.hub_checkpoint , provider="CPUExecutionProvider" ) A_ = DPMSolverMultistepScheduler.from_config(pipe.scheduler.config ) pipe.set_progress_bar_config(disable=UpperCAmelCase ) A_ = self.get_dummy_inputs() A_ = pipe(**UpperCAmelCase ).images A_ = image[0, -3:, -3:, -1] assert image.shape == (1, 128, 128, 3) A_ = np.array([0.65_331, 0.58_277, 0.48_204, 0.56_059, 0.53_665, 0.56_235, 0.50_969, 0.40_009, 0.46_552] ) assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-1 @nightly @require_onnxruntime @require_torch_gpu class _a ( unittest.TestCase ): """simple docstring""" @property def __A ( self : Optional[Any] ): return ( "CUDAExecutionProvider", { "gpu_mem_limit": "15000000000", # 15GB "arena_extend_strategy": "kSameAsRequested", }, ) @property def __A ( self : str ): A_ = ort.SessionOptions() A_ = False return options def __A ( self : Any ): A_ = load_image( "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main" "/img2img/sketch-mountains-input.jpg" ) A_ = init_image.resize((768, 512) ) # using the PNDM scheduler by default A_ = OnnxStableDiffusionImgaImgPipeline.from_pretrained( "CompVis/stable-diffusion-v1-4" , revision="onnx" , safety_checker=UpperCAmelCase , feature_extractor=UpperCAmelCase , provider=self.gpu_provider , sess_options=self.gpu_options , ) pipe.set_progress_bar_config(disable=UpperCAmelCase ) A_ = "A fantasy landscape, trending on artstation" A_ = np.random.RandomState(0 ) A_ = pipe( prompt=UpperCAmelCase , image=UpperCAmelCase , strength=0.75 , guidance_scale=7.5 , num_inference_steps=10 , generator=UpperCAmelCase , output_type="np" , ) A_ = output.images A_ = images[0, 255:258, 383:386, -1] assert images.shape == (1, 512, 768, 3) A_ = np.array([0.4_909, 0.5_059, 0.5_372, 0.4_623, 0.4_876, 0.5_049, 0.4_820, 0.4_956, 0.5_019] ) # TODO: lower the tolerance after finding the cause of onnxruntime reproducibility issues assert np.abs(image_slice.flatten() - expected_slice ).max() < 2E-2 def __A ( self : Optional[Any] ): A_ = load_image( "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main" "/img2img/sketch-mountains-input.jpg" ) A_ = init_image.resize((768, 512) ) A_ = LMSDiscreteScheduler.from_pretrained( "runwayml/stable-diffusion-v1-5" , subfolder="scheduler" , revision="onnx" ) A_ = OnnxStableDiffusionImgaImgPipeline.from_pretrained( "runwayml/stable-diffusion-v1-5" , revision="onnx" , scheduler=UpperCAmelCase , safety_checker=UpperCAmelCase , feature_extractor=UpperCAmelCase , provider=self.gpu_provider , sess_options=self.gpu_options , ) pipe.set_progress_bar_config(disable=UpperCAmelCase ) A_ = "A fantasy landscape, trending on artstation" A_ = np.random.RandomState(0 ) A_ = pipe( prompt=UpperCAmelCase , image=UpperCAmelCase , strength=0.75 , guidance_scale=7.5 , num_inference_steps=20 , generator=UpperCAmelCase , output_type="np" , ) A_ = output.images A_ = images[0, 255:258, 383:386, -1] assert images.shape == (1, 512, 768, 3) A_ = np.array([0.8_043, 0.926, 0.9_581, 0.8_119, 0.8_954, 0.913, 0.7_209, 0.7_463, 0.7_431] ) # TODO: lower the tolerance after finding the cause of onnxruntime reproducibility issues assert np.abs(image_slice.flatten() - expected_slice ).max() < 2E-2
312
from typing import Dict from transformers import EvalPrediction, HfArgumentParser, TrainingArguments, is_torch_available from transformers.testing_utils import ( TestCasePlus, execute_subprocess_async, get_torch_dist_unique_port, require_torch_multi_gpu, require_torch_neuroncore, ) from transformers.training_args import ParallelMode from transformers.utils import logging __a :int = logging.get_logger(__name__) if is_torch_available(): import torch from torch import nn from torch.utils.data import Dataset from transformers import Trainer class _a ( snake_case_ ): """simple docstring""" def __init__( self : Tuple , UpperCAmelCase : int = 101 ): A_ = length def __len__( self : int ): return self.length def __getitem__( self : Optional[int] , UpperCAmelCase : Optional[int] ): return i class _a : """simple docstring""" def __call__( self : Any , UpperCAmelCase : Optional[Any] ): return {"input_ids": torch.tensor(UpperCAmelCase ), "labels": torch.tensor(UpperCAmelCase )} class _a ( nn.Module ): """simple docstring""" def __init__( self : int ): super().__init__() # Add some (unused) params otherwise DDP will complain. A_ = nn.Linear(120 , 80 ) def __A ( self : Tuple , UpperCAmelCase : Dict , UpperCAmelCase : Tuple=None ): if labels is not None: return torch.tensor(0.0 , device=input_ids.device ), input_ids else: return input_ids class _a ( snake_case_ ): """simple docstring""" @require_torch_neuroncore def __A ( self : List[str] ): A_ = f'''--nproc_per_node=2 --master_port={get_torch_dist_unique_port()} {self.test_file_dir}/test_trainer_distributed.py '''.split() A_ = self.get_auto_remove_tmp_dir() A_ = f'''--output_dir {output_dir}'''.split() A_ = ["torchrun"] + distributed_args + args execute_subprocess_async(UpperCAmelCase , env=self.get_env() ) # successful return here == success - any errors would have caused an error in the sub-call class _a ( snake_case_ ): """simple docstring""" @require_torch_multi_gpu def __A ( self : List[str] ): A_ = f'''--nproc_per_node={torch.cuda.device_count()} --master_port={get_torch_dist_unique_port()} {self.test_file_dir}/test_trainer_distributed.py '''.split() A_ = self.get_auto_remove_tmp_dir() A_ = f'''--output_dir {output_dir}'''.split() A_ = ["torchrun"] + distributed_args + args execute_subprocess_async(UpperCAmelCase , env=self.get_env() ) # successful return here == success - any errors would have caused an error in the sub-call if __name__ == "__main__": # The script below is meant to be run under torch.distributed, on a machine with multiple GPUs: # # PYTHONPATH="src" python -m torch.distributed.run --nproc_per_node 2 --output_dir output_dir ./tests/test_trainer_distributed.py __a :Union[str, Any] = HfArgumentParser((TrainingArguments,)) __a :Tuple = parser.parse_args_into_dataclasses()[0] logger.warning( F"Process rank: {training_args.local_rank}, device: {training_args.device}, n_gpu: {training_args.n_gpu}, " F"distributed training: {training_args.parallel_mode != ParallelMode.NOT_DISTRIBUTED}" ) # Essentially, what we want to verify in the distributed case is that we get all samples back, # in the right order. (this is crucial for prediction for instance) for dataset_length in [101, 40, 7]: __a :int = DummyDataset(dataset_length) def __snake_case ( __UpperCamelCase : EvalPrediction ): """simple docstring""" A_ = list(range(len(__UpperCamelCase ) ) ) A_ = p.predictions.tolist() == sequential and p.label_ids.tolist() == sequential if not success and training_args.local_rank == 0: logger.warning( "Predictions and/or labels do not match expected results:\n - predictions: " f'''{p.predictions.tolist()}\n - labels: {p.label_ids.tolist()}\n - expected: {sequential}''' ) return {"success": success} __a :str = Trainer( model=DummyModel(), args=training_args, data_collator=DummyDataCollator(), eval_dataset=dataset, compute_metrics=compute_metrics, ) __a :str = trainer.evaluate() logger.info(metrics) if metrics["eval_success"] is not True: logger.error(metrics) exit(1) __a :str = trainer.predict(dataset) logger.info(p.metrics) if p.metrics["test_success"] is not True: logger.error(p.metrics) exit(1) __a :Optional[int] = 2 __a :List[Any] = trainer.evaluate() logger.info(metrics) if metrics["eval_success"] is not True: logger.error(metrics) exit(1) __a :str = trainer.predict(dataset) logger.info(p.metrics) if p.metrics["test_success"] is not True: logger.error(p.metrics) exit(1) __a :Union[str, Any] = None
312
1
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available __a :List[Any] = { 'configuration_bigbird_pegasus': [ 'BIGBIRD_PEGASUS_PRETRAINED_CONFIG_ARCHIVE_MAP', 'BigBirdPegasusConfig', 'BigBirdPegasusOnnxConfig', ], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __a :Union[str, Any] = [ 'BIGBIRD_PEGASUS_PRETRAINED_MODEL_ARCHIVE_LIST', 'BigBirdPegasusForCausalLM', 'BigBirdPegasusForConditionalGeneration', 'BigBirdPegasusForQuestionAnswering', 'BigBirdPegasusForSequenceClassification', 'BigBirdPegasusModel', 'BigBirdPegasusPreTrainedModel', ] if TYPE_CHECKING: from .configuration_bigbird_pegasus import ( BIGBIRD_PEGASUS_PRETRAINED_CONFIG_ARCHIVE_MAP, BigBirdPegasusConfig, BigBirdPegasusOnnxConfig, ) try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_bigbird_pegasus import ( BIGBIRD_PEGASUS_PRETRAINED_MODEL_ARCHIVE_LIST, BigBirdPegasusForCausalLM, BigBirdPegasusForConditionalGeneration, BigBirdPegasusForQuestionAnswering, BigBirdPegasusForSequenceClassification, BigBirdPegasusModel, BigBirdPegasusPreTrainedModel, ) else: import sys __a :List[Any] = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
312
import argparse import json from pathlib import Path import requests import torch from huggingface_hub import hf_hub_download from PIL import Image from timm import create_model from timm.data import resolve_data_config from timm.data.transforms_factory import create_transform from transformers import BitConfig, BitForImageClassification, BitImageProcessor from transformers.image_utils import PILImageResampling from transformers.utils import logging logging.set_verbosity_info() __a :Any = logging.get_logger(__name__) def __snake_case ( __UpperCamelCase : Optional[int] ): """simple docstring""" A_ = "huggingface/label-files" A_ = "imagenet-1k-id2label.json" A_ = json.load(open(hf_hub_download(__UpperCamelCase ,__UpperCamelCase ,repo_type="dataset" ) ,"r" ) ) A_ = {int(__UpperCamelCase ): v for k, v in idalabel.items()} A_ = {v: k for k, v in idalabel.items()} A_ = "std_conv" if "bit" in model_name else False # note that when using BiT as backbone for ViT-hybrid checkpoints, # one needs to additionally set config.layer_type = "bottleneck", config.stem_type = "same", # config.conv_layer = "std_conv_same" A_ = BitConfig( conv_layer=__UpperCamelCase ,num_labels=1000 ,idalabel=__UpperCamelCase ,labelaid=__UpperCamelCase ,) return config def __snake_case ( __UpperCamelCase : Union[str, Any] ): """simple docstring""" if "stem.conv" in name: A_ = name.replace("stem.conv" ,"bit.embedder.convolution" ) if "blocks" in name: A_ = name.replace("blocks" ,"layers" ) if "head.fc" in name: A_ = name.replace("head.fc" ,"classifier.1" ) if name.startswith("norm" ): A_ = "bit." + name if "bit" not in name and "classifier" not in name: A_ = "bit.encoder." + name return name def __snake_case ( ): """simple docstring""" A_ = "http://images.cocodataset.org/val2017/000000039769.jpg" A_ = Image.open(requests.get(__UpperCamelCase ,stream=__UpperCamelCase ).raw ) return im @torch.no_grad() def __snake_case ( __UpperCamelCase : Any ,__UpperCamelCase : Optional[Any] ,__UpperCamelCase : Tuple=False ): """simple docstring""" A_ = get_config(__UpperCamelCase ) # load original model from timm A_ = create_model(__UpperCamelCase ,pretrained=__UpperCamelCase ) timm_model.eval() # load state_dict of original model A_ = timm_model.state_dict() for key in state_dict.copy().keys(): A_ = state_dict.pop(__UpperCamelCase ) A_ = val.squeeze() if "head" in key else val # load HuggingFace model A_ = BitForImageClassification(__UpperCamelCase ) model.eval() model.load_state_dict(__UpperCamelCase ) # create image processor A_ = create_transform(**resolve_data_config({} ,model=__UpperCamelCase ) ) A_ = transform.transforms A_ = { "bilinear": PILImageResampling.BILINEAR, "bicubic": PILImageResampling.BICUBIC, "nearest": PILImageResampling.NEAREST, } A_ = BitImageProcessor( do_resize=__UpperCamelCase ,size={"shortest_edge": timm_transforms[0].size} ,resample=pillow_resamplings[timm_transforms[0].interpolation.value] ,do_center_crop=__UpperCamelCase ,crop_size={"height": timm_transforms[1].size[0], "width": timm_transforms[1].size[1]} ,do_normalize=__UpperCamelCase ,image_mean=timm_transforms[-1].mean.tolist() ,image_std=timm_transforms[-1].std.tolist() ,) A_ = prepare_img() A_ = transform(__UpperCamelCase ).unsqueeze(0 ) A_ = processor(__UpperCamelCase ,return_tensors="pt" ).pixel_values # verify pixel values assert torch.allclose(__UpperCamelCase ,__UpperCamelCase ) # verify logits with torch.no_grad(): A_ = model(__UpperCamelCase ) A_ = outputs.logits print("Logits:" ,logits[0, :3] ) print("Predicted class:" ,model.config.idalabel[logits.argmax(-1 ).item()] ) A_ = timm_model(__UpperCamelCase ) assert timm_logits.shape == outputs.logits.shape assert torch.allclose(__UpperCamelCase ,outputs.logits ,atol=1E-3 ) print("Looks ok!" ) if pytorch_dump_folder_path is not None: Path(__UpperCamelCase ).mkdir(exist_ok=__UpperCamelCase ) print(f'''Saving model {model_name} and processor to {pytorch_dump_folder_path}''' ) model.save_pretrained(__UpperCamelCase ) processor.save_pretrained(__UpperCamelCase ) if push_to_hub: print(f'''Pushing model {model_name} and processor to the hub''' ) model.push_to_hub(f'''ybelkada/{model_name}''' ) processor.push_to_hub(f'''ybelkada/{model_name}''' ) if __name__ == "__main__": __a :List[Any] = argparse.ArgumentParser() # Required parameters parser.add_argument( '--model_name', default='resnetv2_50x1_bitm', type=str, help='Name of the BiT timm model you\'d like to convert.', ) parser.add_argument( '--pytorch_dump_folder_path', default=None, type=str, help='Path to the output PyTorch model directory.' ) parser.add_argument( '--push_to_hub', action='store_true', help='Whether to push the model to the hub.', ) __a :str = parser.parse_args() convert_bit_checkpoint(args.model_name, args.pytorch_dump_folder_path, args.push_to_hub)
312
1
from typing import Callable, List, Optional, Union import PIL import torch from transformers import ( CLIPImageProcessor, CLIPSegForImageSegmentation, CLIPSegProcessor, CLIPTextModel, CLIPTokenizer, ) from diffusers import DiffusionPipeline from diffusers.configuration_utils import FrozenDict from diffusers.models import AutoencoderKL, UNetaDConditionModel from diffusers.pipelines.stable_diffusion import StableDiffusionInpaintPipeline from diffusers.pipelines.stable_diffusion.safety_checker import StableDiffusionSafetyChecker from diffusers.schedulers import DDIMScheduler, LMSDiscreteScheduler, PNDMScheduler from diffusers.utils import deprecate, is_accelerate_available, logging __a :Union[str, Any] = logging.get_logger(__name__) # pylint: disable=invalid-name class _a ( snake_case_ ): """simple docstring""" def __init__( self : Optional[int] , UpperCAmelCase : CLIPSegForImageSegmentation , UpperCAmelCase : CLIPSegProcessor , UpperCAmelCase : AutoencoderKL , UpperCAmelCase : CLIPTextModel , UpperCAmelCase : CLIPTokenizer , UpperCAmelCase : UNetaDConditionModel , UpperCAmelCase : Union[DDIMScheduler, PNDMScheduler, LMSDiscreteScheduler] , UpperCAmelCase : StableDiffusionSafetyChecker , UpperCAmelCase : CLIPImageProcessor , ): super().__init__() if hasattr(scheduler.config , "steps_offset" ) and scheduler.config.steps_offset != 1: A_ = ( f'''The configuration file of this scheduler: {scheduler} is outdated. `steps_offset`''' f''' should be set to 1 instead of {scheduler.config.steps_offset}. Please make sure ''' "to update the config accordingly as leaving `steps_offset` might led to incorrect results" " in future versions. If you have downloaded this checkpoint from the Hugging Face Hub," " it would be very nice if you could open a Pull request for the `scheduler/scheduler_config.json`" " file" ) deprecate("steps_offset!=1" , "1.0.0" , UpperCAmelCase , standard_warn=UpperCAmelCase ) A_ = dict(scheduler.config ) A_ = 1 A_ = FrozenDict(UpperCAmelCase ) if hasattr(scheduler.config , "skip_prk_steps" ) and scheduler.config.skip_prk_steps is False: A_ = ( f'''The configuration file of this scheduler: {scheduler} has not set the configuration''' " `skip_prk_steps`. `skip_prk_steps` should be set to True in the configuration file. Please make" " sure to update the config accordingly as not setting `skip_prk_steps` in the config might lead to" " incorrect results in future versions. If you have downloaded this checkpoint from the Hugging Face" " Hub, it would be very nice if you could open a Pull request for the" " `scheduler/scheduler_config.json` file" ) deprecate("skip_prk_steps not set" , "1.0.0" , UpperCAmelCase , standard_warn=UpperCAmelCase ) A_ = dict(scheduler.config ) A_ = True A_ = FrozenDict(UpperCAmelCase ) if safety_checker is None: logger.warning( f'''You have disabled the safety checker for {self.__class__} by passing `safety_checker=None`. Ensure''' " that you abide to the conditions of the Stable Diffusion license and do not expose unfiltered" " results in services or applications open to the public. Both the diffusers team and Hugging Face" " strongly recommend to keep the safety filter enabled in all public facing circumstances, disabling" " it only for use-cases that involve analyzing network behavior or auditing its results. For more" " information, please have a look at https://github.com/huggingface/diffusers/pull/254 ." ) self.register_modules( segmentation_model=UpperCAmelCase , segmentation_processor=UpperCAmelCase , vae=UpperCAmelCase , text_encoder=UpperCAmelCase , tokenizer=UpperCAmelCase , unet=UpperCAmelCase , scheduler=UpperCAmelCase , safety_checker=UpperCAmelCase , feature_extractor=UpperCAmelCase , ) def __A ( self : List[Any] , UpperCAmelCase : Optional[Union[str, int]] = "auto" ): if slice_size == "auto": # half the attention head size is usually a good trade-off between # speed and memory A_ = self.unet.config.attention_head_dim // 2 self.unet.set_attention_slice(UpperCAmelCase ) def __A ( self : str ): self.enable_attention_slicing(UpperCAmelCase ) def __A ( self : str ): if is_accelerate_available(): from accelerate import cpu_offload else: raise ImportError("Please install accelerate via `pip install accelerate`" ) A_ = torch.device("cuda" ) for cpu_offloaded_model in [self.unet, self.text_encoder, self.vae, self.safety_checker]: if cpu_offloaded_model is not None: cpu_offload(UpperCAmelCase , UpperCAmelCase ) @property # Copied from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion.StableDiffusionPipeline._execution_device def __A ( self : Any ): if self.device != torch.device("meta" ) or not hasattr(self.unet , "_hf_hook" ): return self.device for module in self.unet.modules(): if ( hasattr(UpperCAmelCase , "_hf_hook" ) and hasattr(module._hf_hook , "execution_device" ) and module._hf_hook.execution_device is not None ): return torch.device(module._hf_hook.execution_device ) return self.device @torch.no_grad() def __call__( self : Union[str, Any] , UpperCAmelCase : Union[str, List[str]] , UpperCAmelCase : Union[torch.FloatTensor, PIL.Image.Image] , UpperCAmelCase : str , UpperCAmelCase : int = 512 , UpperCAmelCase : int = 512 , UpperCAmelCase : int = 50 , UpperCAmelCase : float = 7.5 , UpperCAmelCase : Optional[Union[str, List[str]]] = None , UpperCAmelCase : Optional[int] = 1 , UpperCAmelCase : float = 0.0 , UpperCAmelCase : Optional[torch.Generator] = None , UpperCAmelCase : Optional[torch.FloatTensor] = None , UpperCAmelCase : Optional[str] = "pil" , UpperCAmelCase : bool = True , UpperCAmelCase : Optional[Callable[[int, int, torch.FloatTensor], None]] = None , UpperCAmelCase : int = 1 , **UpperCAmelCase : List[Any] , ): A_ = self.segmentation_processor( text=[text] , images=[image] , padding="max_length" , return_tensors="pt" ).to(self.device ) A_ = self.segmentation_model(**UpperCAmelCase ) A_ = torch.sigmoid(outputs.logits ).cpu().detach().unsqueeze(-1 ).numpy() A_ = self.numpy_to_pil(UpperCAmelCase )[0].resize(image.size ) # Run inpainting pipeline with the generated mask A_ = StableDiffusionInpaintPipeline( vae=self.vae , text_encoder=self.text_encoder , tokenizer=self.tokenizer , unet=self.unet , scheduler=self.scheduler , safety_checker=self.safety_checker , feature_extractor=self.feature_extractor , ) return inpainting_pipeline( prompt=UpperCAmelCase , image=UpperCAmelCase , mask_image=UpperCAmelCase , height=UpperCAmelCase , width=UpperCAmelCase , num_inference_steps=UpperCAmelCase , guidance_scale=UpperCAmelCase , negative_prompt=UpperCAmelCase , num_images_per_prompt=UpperCAmelCase , eta=UpperCAmelCase , generator=UpperCAmelCase , latents=UpperCAmelCase , output_type=UpperCAmelCase , return_dict=UpperCAmelCase , callback=UpperCAmelCase , callback_steps=UpperCAmelCase , )
312
import os import re import sys import traceback import warnings from pathlib import Path from typing import Dict, Optional, Union from uuid import uuida from huggingface_hub import HfFolder, ModelCard, ModelCardData, hf_hub_download, whoami from huggingface_hub.file_download import REGEX_COMMIT_HASH from huggingface_hub.utils import ( EntryNotFoundError, RepositoryNotFoundError, RevisionNotFoundError, is_jinja_available, ) from packaging import version from requests import HTTPError from .. import __version__ from .constants import ( DEPRECATED_REVISION_ARGS, DIFFUSERS_CACHE, HUGGINGFACE_CO_RESOLVE_ENDPOINT, SAFETENSORS_WEIGHTS_NAME, WEIGHTS_NAME, ) from .import_utils import ( ENV_VARS_TRUE_VALUES, _flax_version, _jax_version, _onnxruntime_version, _torch_version, is_flax_available, is_onnx_available, is_torch_available, ) from .logging import get_logger __a :Dict = get_logger(__name__) __a :Union[str, Any] = Path(__file__).parent / 'model_card_template.md' __a :Tuple = uuida().hex __a :List[Any] = os.getenv('HF_HUB_OFFLINE', '').upper() in ENV_VARS_TRUE_VALUES __a :Union[str, Any] = os.getenv('DISABLE_TELEMETRY', '').upper() in ENV_VARS_TRUE_VALUES __a :Tuple = HUGGINGFACE_CO_RESOLVE_ENDPOINT + '/api/telemetry/' def __snake_case ( __UpperCamelCase : Union[Dict, str, None] = None ): """simple docstring""" A_ = f'''diffusers/{__version__}; python/{sys.version.split()[0]}; session_id/{SESSION_ID}''' if DISABLE_TELEMETRY or HF_HUB_OFFLINE: return ua + "; telemetry/off" if is_torch_available(): ua += f'''; torch/{_torch_version}''' if is_flax_available(): ua += f'''; jax/{_jax_version}''' ua += f'''; flax/{_flax_version}''' if is_onnx_available(): ua += f'''; onnxruntime/{_onnxruntime_version}''' # CI will set this value to True if os.environ.get("DIFFUSERS_IS_CI" ,"" ).upper() in ENV_VARS_TRUE_VALUES: ua += "; is_ci/true" if isinstance(__UpperCamelCase ,__UpperCamelCase ): ua += "; " + "; ".join(f'''{k}/{v}''' for k, v in user_agent.items() ) elif isinstance(__UpperCamelCase ,__UpperCamelCase ): ua += "; " + user_agent return ua def __snake_case ( __UpperCamelCase : str ,__UpperCamelCase : Optional[str] = None ,__UpperCamelCase : Optional[str] = None ): """simple docstring""" if token is None: A_ = HfFolder.get_token() if organization is None: A_ = whoami(__UpperCamelCase )["name"] return f'''{username}/{model_id}''' else: return f'''{organization}/{model_id}''' def __snake_case ( __UpperCamelCase : Tuple ,__UpperCamelCase : Union[str, Any] ): """simple docstring""" if not is_jinja_available(): raise ValueError( "Modelcard rendering is based on Jinja templates." " Please make sure to have `jinja` installed before using `create_model_card`." " To install it, please run `pip install Jinja2`." ) if hasattr(__UpperCamelCase ,"local_rank" ) and args.local_rank not in [-1, 0]: return A_ = args.hub_token if hasattr(__UpperCamelCase ,"hub_token" ) else None A_ = get_full_repo_name(__UpperCamelCase ,token=__UpperCamelCase ) A_ = ModelCard.from_template( card_data=ModelCardData( # Card metadata object that will be converted to YAML block language="en" ,license="apache-2.0" ,library_name="diffusers" ,tags=[] ,datasets=args.dataset_name ,metrics=[] ,) ,template_path=__UpperCamelCase ,model_name=__UpperCamelCase ,repo_name=__UpperCamelCase ,dataset_name=args.dataset_name if hasattr(__UpperCamelCase ,"dataset_name" ) else None ,learning_rate=args.learning_rate ,train_batch_size=args.train_batch_size ,eval_batch_size=args.eval_batch_size ,gradient_accumulation_steps=( args.gradient_accumulation_steps if hasattr(__UpperCamelCase ,"gradient_accumulation_steps" ) else None ) ,adam_betaa=args.adam_betaa if hasattr(__UpperCamelCase ,"adam_beta1" ) else None ,adam_betaa=args.adam_betaa if hasattr(__UpperCamelCase ,"adam_beta2" ) else None ,adam_weight_decay=args.adam_weight_decay if hasattr(__UpperCamelCase ,"adam_weight_decay" ) else None ,adam_epsilon=args.adam_epsilon if hasattr(__UpperCamelCase ,"adam_epsilon" ) else None ,lr_scheduler=args.lr_scheduler if hasattr(__UpperCamelCase ,"lr_scheduler" ) else None ,lr_warmup_steps=args.lr_warmup_steps if hasattr(__UpperCamelCase ,"lr_warmup_steps" ) else None ,ema_inv_gamma=args.ema_inv_gamma if hasattr(__UpperCamelCase ,"ema_inv_gamma" ) else None ,ema_power=args.ema_power if hasattr(__UpperCamelCase ,"ema_power" ) else None ,ema_max_decay=args.ema_max_decay if hasattr(__UpperCamelCase ,"ema_max_decay" ) else None ,mixed_precision=args.mixed_precision ,) A_ = os.path.join(args.output_dir ,"README.md" ) model_card.save(__UpperCamelCase ) def __snake_case ( __UpperCamelCase : Optional[str] ,__UpperCamelCase : Optional[str] = None ): """simple docstring""" if resolved_file is None or commit_hash is not None: return commit_hash A_ = str(Path(__UpperCamelCase ).as_posix() ) A_ = re.search(R"snapshots/([^/]+)/" ,__UpperCamelCase ) if search is None: return None A_ = search.groups()[0] return commit_hash if REGEX_COMMIT_HASH.match(__UpperCamelCase ) else None # Old default cache path, potentially to be migrated. # This logic was more or less taken from `transformers`, with the following differences: # - Diffusers doesn't use custom environment variables to specify the cache path. # - There is no need to migrate the cache format, just move the files to the new location. __a :str = os.path.expanduser( os.getenv('HF_HOME', os.path.join(os.getenv('XDG_CACHE_HOME', '~/.cache'), 'huggingface')) ) __a :List[Any] = os.path.join(hf_cache_home, 'diffusers') def __snake_case ( __UpperCamelCase : Optional[str] = None ,__UpperCamelCase : Optional[str] = None ): """simple docstring""" if new_cache_dir is None: A_ = DIFFUSERS_CACHE if old_cache_dir is None: A_ = old_diffusers_cache A_ = Path(__UpperCamelCase ).expanduser() A_ = Path(__UpperCamelCase ).expanduser() for old_blob_path in old_cache_dir.glob("**/blobs/*" ): if old_blob_path.is_file() and not old_blob_path.is_symlink(): A_ = new_cache_dir / old_blob_path.relative_to(__UpperCamelCase ) new_blob_path.parent.mkdir(parents=__UpperCamelCase ,exist_ok=__UpperCamelCase ) os.replace(__UpperCamelCase ,__UpperCamelCase ) try: os.symlink(__UpperCamelCase ,__UpperCamelCase ) except OSError: logger.warning( "Could not create symlink between old cache and new cache. If you use an older version of diffusers again, files will be re-downloaded." ) # At this point, old_cache_dir contains symlinks to the new cache (it can still be used). __a :Dict = os.path.join(DIFFUSERS_CACHE, 'version_diffusers_cache.txt') if not os.path.isfile(cache_version_file): __a :Optional[int] = 0 else: with open(cache_version_file) as f: try: __a :Dict = int(f.read()) except ValueError: __a :str = 0 if cache_version < 1: __a :Optional[Any] = os.path.isdir(old_diffusers_cache) and len(os.listdir(old_diffusers_cache)) > 0 if old_cache_is_not_empty: logger.warning( 'The cache for model files in Diffusers v0.14.0 has moved to a new location. Moving your ' 'existing cached models. This is a one-time operation, you can interrupt it or run it ' 'later by calling `diffusers.utils.hub_utils.move_cache()`.' ) try: move_cache() except Exception as e: __a :Optional[Any] = '\n'.join(traceback.format_tb(e.__traceback__)) logger.error( F"There was a problem when trying to move your cache:\n\n{trace}\n{e.__class__.__name__}: {e}\n\nPlease " 'file an issue at https://github.com/huggingface/diffusers/issues/new/choose, copy paste this whole ' 'message and we will do our best to help.' ) if cache_version < 1: try: os.makedirs(DIFFUSERS_CACHE, exist_ok=True) with open(cache_version_file, 'w') as f: f.write('1') except Exception: logger.warning( F"There was a problem when trying to write in your cache folder ({DIFFUSERS_CACHE}). Please, ensure " 'the directory exists and can be written to.' ) def __snake_case ( __UpperCamelCase : str ,__UpperCamelCase : Optional[str] = None ): """simple docstring""" if variant is not None: A_ = weights_name.split("." ) A_ = splits[:-1] + [variant] + splits[-1:] A_ = ".".join(__UpperCamelCase ) return weights_name def __snake_case ( __UpperCamelCase : Optional[Any] ,*, __UpperCamelCase : Union[str, Any] ,__UpperCamelCase : Any ,__UpperCamelCase : Tuple ,__UpperCamelCase : Union[str, Any] ,__UpperCamelCase : str ,__UpperCamelCase : int ,__UpperCamelCase : Union[str, Any] ,__UpperCamelCase : int ,__UpperCamelCase : Optional[int] ,__UpperCamelCase : Tuple ,__UpperCamelCase : Optional[int]=None ,): """simple docstring""" A_ = str(__UpperCamelCase ) if os.path.isfile(__UpperCamelCase ): return pretrained_model_name_or_path elif os.path.isdir(__UpperCamelCase ): if os.path.isfile(os.path.join(__UpperCamelCase ,__UpperCamelCase ) ): # Load from a PyTorch checkpoint A_ = os.path.join(__UpperCamelCase ,__UpperCamelCase ) return model_file elif subfolder is not None and os.path.isfile( os.path.join(__UpperCamelCase ,__UpperCamelCase ,__UpperCamelCase ) ): A_ = os.path.join(__UpperCamelCase ,__UpperCamelCase ,__UpperCamelCase ) return model_file else: raise EnvironmentError( f'''Error no file named {weights_name} found in directory {pretrained_model_name_or_path}.''' ) else: # 1. First check if deprecated way of loading from branches is used if ( revision in DEPRECATED_REVISION_ARGS and (weights_name == WEIGHTS_NAME or weights_name == SAFETENSORS_WEIGHTS_NAME) and version.parse(version.parse(__UpperCamelCase ).base_version ) >= version.parse("0.20.0" ) ): try: A_ = hf_hub_download( __UpperCamelCase ,filename=_add_variant(__UpperCamelCase ,__UpperCamelCase ) ,cache_dir=__UpperCamelCase ,force_download=__UpperCamelCase ,proxies=__UpperCamelCase ,resume_download=__UpperCamelCase ,local_files_only=__UpperCamelCase ,use_auth_token=__UpperCamelCase ,user_agent=__UpperCamelCase ,subfolder=__UpperCamelCase ,revision=revision or commit_hash ,) warnings.warn( f'''Loading the variant {revision} from {pretrained_model_name_or_path} via `revision=\'{revision}\'` is deprecated. Loading instead from `revision=\'main\'` with `variant={revision}`. Loading model variants via `revision=\'{revision}\'` will be removed in diffusers v1. Please use `variant=\'{revision}\'` instead.''' ,__UpperCamelCase ,) return model_file except: # noqa: E722 warnings.warn( f'''You are loading the variant {revision} from {pretrained_model_name_or_path} via `revision=\'{revision}\'`. This behavior is deprecated and will be removed in diffusers v1. One should use `variant=\'{revision}\'` instead. However, it appears that {pretrained_model_name_or_path} currently does not have a {_add_variant(__UpperCamelCase ,__UpperCamelCase )} file in the \'main\' branch of {pretrained_model_name_or_path}. \n The Diffusers team and community would be very grateful if you could open an issue: https://github.com/huggingface/diffusers/issues/new with the title \'{pretrained_model_name_or_path} is missing {_add_variant(__UpperCamelCase ,__UpperCamelCase )}\' so that the correct variant file can be added.''' ,__UpperCamelCase ,) try: # 2. Load model file as usual A_ = hf_hub_download( __UpperCamelCase ,filename=__UpperCamelCase ,cache_dir=__UpperCamelCase ,force_download=__UpperCamelCase ,proxies=__UpperCamelCase ,resume_download=__UpperCamelCase ,local_files_only=__UpperCamelCase ,use_auth_token=__UpperCamelCase ,user_agent=__UpperCamelCase ,subfolder=__UpperCamelCase ,revision=revision or commit_hash ,) return model_file except RepositoryNotFoundError: raise EnvironmentError( f'''{pretrained_model_name_or_path} is not a local folder and is not a valid model identifier ''' "listed on 'https://huggingface.co/models'\nIf this is a private repository, make sure to pass a " "token having permission to this repo with `use_auth_token` or log in with `huggingface-cli " "login`." ) except RevisionNotFoundError: raise EnvironmentError( f'''{revision} is not a valid git identifier (branch name, tag name or commit id) that exists for ''' "this model name. Check the model page at " f'''\'https://huggingface.co/{pretrained_model_name_or_path}\' for available revisions.''' ) except EntryNotFoundError: raise EnvironmentError( f'''{pretrained_model_name_or_path} does not appear to have a file named {weights_name}.''' ) except HTTPError as err: raise EnvironmentError( f'''There was a specific connection error when trying to load {pretrained_model_name_or_path}:\n{err}''' ) except ValueError: raise EnvironmentError( f'''We couldn\'t connect to \'{HUGGINGFACE_CO_RESOLVE_ENDPOINT}\' to load this model, couldn\'t find it''' f''' in the cached files and it looks like {pretrained_model_name_or_path} is not the path to a''' f''' directory containing a file named {weights_name} or''' " \nCheckout your internet connection or see how to run the library in" " offline mode at 'https://huggingface.co/docs/diffusers/installation#offline-mode'." ) except EnvironmentError: raise EnvironmentError( f'''Can\'t load the model for \'{pretrained_model_name_or_path}\'. If you were trying to load it from ''' "'https://huggingface.co/models', make sure you don't have a local directory with the same name. " f'''Otherwise, make sure \'{pretrained_model_name_or_path}\' is the correct path to a directory ''' f'''containing a file named {weights_name}''' )
312
1
from maths.is_square_free import is_square_free from maths.prime_factors import prime_factors def __snake_case ( __UpperCamelCase : int ): """simple docstring""" A_ = prime_factors(__UpperCamelCase ) if is_square_free(__UpperCamelCase ): return -1 if len(__UpperCamelCase ) % 2 else 1 return 0 if __name__ == "__main__": import doctest doctest.testmod()
312
# Copyright 2023 The HuggingFace Team. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available __a :Any = { 'configuration_mgp_str': ['MGP_STR_PRETRAINED_CONFIG_ARCHIVE_MAP', 'MgpstrConfig'], 'processing_mgp_str': ['MgpstrProcessor'], 'tokenization_mgp_str': ['MgpstrTokenizer'], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __a :Optional[Any] = [ 'MGP_STR_PRETRAINED_MODEL_ARCHIVE_LIST', 'MgpstrModel', 'MgpstrPreTrainedModel', 'MgpstrForSceneTextRecognition', ] if TYPE_CHECKING: from .configuration_mgp_str import MGP_STR_PRETRAINED_CONFIG_ARCHIVE_MAP, MgpstrConfig from .processing_mgp_str import MgpstrProcessor from .tokenization_mgp_str import MgpstrTokenizer try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_mgp_str import ( MGP_STR_PRETRAINED_MODEL_ARCHIVE_LIST, MgpstrForSceneTextRecognition, MgpstrModel, MgpstrPreTrainedModel, ) else: import sys __a :List[Any] = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
312
1
import copy from dataclasses import dataclass, field from typing import ClassVar, Dict from ..features import Audio, ClassLabel, Features from .base import TaskTemplate @dataclass(frozen=snake_case_ ) class _a ( snake_case_ ): """simple docstring""" _lowerCamelCase : str = field(default='audio-classification' , metadata={'include_in_asdict_even_if_is_default': True} ) _lowerCamelCase : ClassVar[Features] = Features({'audio': Audio()} ) _lowerCamelCase : ClassVar[Features] = Features({'labels': ClassLabel} ) _lowerCamelCase : str = "audio" _lowerCamelCase : str = "labels" def __A ( self : str , UpperCAmelCase : List[Any] ): if self.label_column not in features: raise ValueError(f'''Column {self.label_column} is not present in features.''' ) if not isinstance(features[self.label_column] , UpperCAmelCase ): raise ValueError(f'''Column {self.label_column} is not a ClassLabel.''' ) A_ = copy.deepcopy(self ) A_ = self.label_schema.copy() A_ = features[self.label_column] A_ = label_schema return task_template @property def __A ( self : List[str] ): return { self.audio_column: "audio", self.label_column: "labels", }
312
import functools from typing import Any def __snake_case ( __UpperCamelCase : str ,__UpperCamelCase : list[str] ): """simple docstring""" if not isinstance(__UpperCamelCase ,__UpperCamelCase ) or len(__UpperCamelCase ) == 0: raise ValueError("the string should be not empty string" ) if not isinstance(__UpperCamelCase ,__UpperCamelCase ) or not all( isinstance(__UpperCamelCase ,__UpperCamelCase ) and len(__UpperCamelCase ) > 0 for item in words ): raise ValueError("the words should be a list of non-empty strings" ) # Build trie A_ = {} A_ = "WORD_KEEPER" for word in words: A_ = trie for c in word: if c not in trie_node: A_ = {} A_ = trie_node[c] A_ = True A_ = len(__UpperCamelCase ) # Dynamic programming method @functools.cache def is_breakable(__UpperCamelCase : int ) -> bool: if index == len_string: return True A_ = trie for i in range(__UpperCamelCase ,__UpperCamelCase ): A_ = trie_node.get(string[i] ,__UpperCamelCase ) if trie_node is None: return False if trie_node.get(__UpperCamelCase ,__UpperCamelCase ) and is_breakable(i + 1 ): return True return False return is_breakable(0 ) if __name__ == "__main__": import doctest doctest.testmod()
312
1
from ...configuration_utils import PretrainedConfig from ...utils import logging __a :Dict = logging.get_logger(__name__) __a :Tuple = { 'bigcode/gpt_bigcode-santacoder': 'https://huggingface.co/bigcode/gpt_bigcode-santacoder/resolve/main/config.json', } class _a ( snake_case_ ): """simple docstring""" _lowerCamelCase : Dict = 'gpt_bigcode' _lowerCamelCase : Dict = ['past_key_values'] _lowerCamelCase : Dict = { 'hidden_size': 'n_embd', 'max_position_embeddings': 'n_positions', 'num_attention_heads': 'n_head', 'num_hidden_layers': 'n_layer', } def __init__( self : Optional[int] , UpperCAmelCase : Any=50257 , UpperCAmelCase : int=1024 , UpperCAmelCase : str=768 , UpperCAmelCase : List[str]=12 , UpperCAmelCase : Optional[Any]=12 , UpperCAmelCase : Optional[int]=None , UpperCAmelCase : Optional[int]="gelu_pytorch_tanh" , UpperCAmelCase : Tuple=0.1 , UpperCAmelCase : str=0.1 , UpperCAmelCase : Optional[Any]=0.1 , UpperCAmelCase : Any=1E-5 , UpperCAmelCase : Optional[int]=0.02 , UpperCAmelCase : Tuple=True , UpperCAmelCase : List[Any]=True , UpperCAmelCase : Union[str, Any]=50256 , UpperCAmelCase : Optional[Any]=50256 , UpperCAmelCase : List[str]=True , UpperCAmelCase : str=True , UpperCAmelCase : Dict=True , **UpperCAmelCase : Optional[int] , ): A_ = vocab_size A_ = n_positions A_ = n_embd A_ = n_layer A_ = n_head A_ = n_inner A_ = activation_function A_ = resid_pdrop A_ = embd_pdrop A_ = attn_pdrop A_ = layer_norm_epsilon A_ = initializer_range A_ = scale_attn_weights A_ = use_cache A_ = attention_softmax_in_fpaa A_ = scale_attention_softmax_in_fpaa A_ = multi_query A_ = bos_token_id A_ = eos_token_id super().__init__(bos_token_id=UpperCAmelCase , eos_token_id=UpperCAmelCase , **UpperCAmelCase )
312
import json from typing import List, Optional, Tuple from tokenizers import normalizers from ...tokenization_utils_fast import PreTrainedTokenizerFast from .tokenization_electra import ElectraTokenizer __a :List[str] = {'vocab_file': 'vocab.txt', 'tokenizer_file': 'tokenizer.json'} __a :Union[str, Any] = { 'vocab_file': { 'google/electra-small-generator': ( 'https://huggingface.co/google/electra-small-generator/resolve/main/vocab.txt' ), 'google/electra-base-generator': 'https://huggingface.co/google/electra-base-generator/resolve/main/vocab.txt', 'google/electra-large-generator': ( 'https://huggingface.co/google/electra-large-generator/resolve/main/vocab.txt' ), 'google/electra-small-discriminator': ( 'https://huggingface.co/google/electra-small-discriminator/resolve/main/vocab.txt' ), 'google/electra-base-discriminator': ( 'https://huggingface.co/google/electra-base-discriminator/resolve/main/vocab.txt' ), 'google/electra-large-discriminator': ( 'https://huggingface.co/google/electra-large-discriminator/resolve/main/vocab.txt' ), }, 'tokenizer_file': { 'google/electra-small-generator': ( 'https://huggingface.co/google/electra-small-generator/resolve/main/tokenizer.json' ), 'google/electra-base-generator': ( 'https://huggingface.co/google/electra-base-generator/resolve/main/tokenizer.json' ), 'google/electra-large-generator': ( 'https://huggingface.co/google/electra-large-generator/resolve/main/tokenizer.json' ), 'google/electra-small-discriminator': ( 'https://huggingface.co/google/electra-small-discriminator/resolve/main/tokenizer.json' ), 'google/electra-base-discriminator': ( 'https://huggingface.co/google/electra-base-discriminator/resolve/main/tokenizer.json' ), 'google/electra-large-discriminator': ( 'https://huggingface.co/google/electra-large-discriminator/resolve/main/tokenizer.json' ), }, } __a :Optional[int] = { 'google/electra-small-generator': 512, 'google/electra-base-generator': 512, 'google/electra-large-generator': 512, 'google/electra-small-discriminator': 512, 'google/electra-base-discriminator': 512, 'google/electra-large-discriminator': 512, } __a :str = { 'google/electra-small-generator': {'do_lower_case': True}, 'google/electra-base-generator': {'do_lower_case': True}, 'google/electra-large-generator': {'do_lower_case': True}, 'google/electra-small-discriminator': {'do_lower_case': True}, 'google/electra-base-discriminator': {'do_lower_case': True}, 'google/electra-large-discriminator': {'do_lower_case': True}, } class _a ( snake_case_ ): """simple docstring""" _lowerCamelCase : Tuple = VOCAB_FILES_NAMES _lowerCamelCase : Union[str, Any] = PRETRAINED_VOCAB_FILES_MAP _lowerCamelCase : int = PRETRAINED_INIT_CONFIGURATION _lowerCamelCase : List[Any] = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES _lowerCamelCase : int = ElectraTokenizer def __init__( self : Tuple , UpperCAmelCase : Dict=None , UpperCAmelCase : Optional[int]=None , UpperCAmelCase : Any=True , UpperCAmelCase : Any="[UNK]" , UpperCAmelCase : Union[str, Any]="[SEP]" , UpperCAmelCase : List[Any]="[PAD]" , UpperCAmelCase : Union[str, Any]="[CLS]" , UpperCAmelCase : List[Any]="[MASK]" , UpperCAmelCase : List[str]=True , UpperCAmelCase : Any=None , **UpperCAmelCase : Union[str, Any] , ): super().__init__( UpperCAmelCase , tokenizer_file=UpperCAmelCase , do_lower_case=UpperCAmelCase , unk_token=UpperCAmelCase , sep_token=UpperCAmelCase , pad_token=UpperCAmelCase , cls_token=UpperCAmelCase , mask_token=UpperCAmelCase , tokenize_chinese_chars=UpperCAmelCase , strip_accents=UpperCAmelCase , **UpperCAmelCase , ) A_ = json.loads(self.backend_tokenizer.normalizer.__getstate__() ) if ( normalizer_state.get("lowercase" , UpperCAmelCase ) != do_lower_case or normalizer_state.get("strip_accents" , UpperCAmelCase ) != strip_accents or normalizer_state.get("handle_chinese_chars" , UpperCAmelCase ) != tokenize_chinese_chars ): A_ = getattr(UpperCAmelCase , normalizer_state.pop("type" ) ) A_ = do_lower_case A_ = strip_accents A_ = tokenize_chinese_chars A_ = normalizer_class(**UpperCAmelCase ) A_ = do_lower_case def __A ( self : int , UpperCAmelCase : List[Any] , UpperCAmelCase : Union[str, Any]=None ): A_ = [self.cls_token_id] + token_ids_a + [self.sep_token_id] if token_ids_a: output += token_ids_a + [self.sep_token_id] return output def __A ( self : Union[str, Any] , UpperCAmelCase : List[int] , UpperCAmelCase : Optional[List[int]] = None ): A_ = [self.sep_token_id] A_ = [self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep ) * [0] return len(cls + token_ids_a + sep ) * [0] + len(token_ids_a + sep ) * [1] def __A ( self : Tuple , UpperCAmelCase : str , UpperCAmelCase : Optional[str] = None ): A_ = self._tokenizer.model.save(UpperCAmelCase , name=UpperCAmelCase ) return tuple(UpperCAmelCase )
312
1
import os from bleurt import score # From: git+https://github.com/google-research/bleurt.git import datasets __a :Tuple = datasets.logging.get_logger(__name__) __a :Tuple = '\\n@inproceedings{bleurt,\n title={BLEURT: Learning Robust Metrics for Text Generation},\n author={Thibault Sellam and Dipanjan Das and Ankur P. Parikh},\n booktitle={ACL},\n year={2020},\n url={https://arxiv.org/abs/2004.04696}\n}\n' __a :List[Any] = '\\nBLEURT a learnt evaluation metric for Natural Language Generation. It is built using multiple phases of transfer learning starting from a pretrained BERT model (Devlin et al. 2018)\nand then employing another pre-training phrase using synthetic data. Finally it is trained on WMT human annotations. You may run BLEURT out-of-the-box or fine-tune\nit for your specific application (the latter is expected to perform better).\n\nSee the project\'s README at https://github.com/google-research/bleurt#readme for more information.\n' __a :Any = '\nBLEURT score.\n\nArgs:\n `predictions` (list of str): prediction/candidate sentences\n `references` (list of str): reference sentences\n `checkpoint` BLEURT checkpoint. Will default to BLEURT-tiny if None.\n\nReturns:\n \'scores\': List of scores.\nExamples:\n\n >>> predictions = ["hello there", "general kenobi"]\n >>> references = ["hello there", "general kenobi"]\n >>> bleurt = datasets.load_metric("bleurt")\n >>> results = bleurt.compute(predictions=predictions, references=references)\n >>> print([round(v, 2) for v in results["scores"]])\n [1.03, 1.04]\n' __a :Tuple = { 'bleurt-tiny-128': 'https://storage.googleapis.com/bleurt-oss/bleurt-tiny-128.zip', 'bleurt-tiny-512': 'https://storage.googleapis.com/bleurt-oss/bleurt-tiny-512.zip', 'bleurt-base-128': 'https://storage.googleapis.com/bleurt-oss/bleurt-base-128.zip', 'bleurt-base-512': 'https://storage.googleapis.com/bleurt-oss/bleurt-base-512.zip', 'bleurt-large-128': 'https://storage.googleapis.com/bleurt-oss/bleurt-large-128.zip', 'bleurt-large-512': 'https://storage.googleapis.com/bleurt-oss/bleurt-large-512.zip', 'BLEURT-20-D3': 'https://storage.googleapis.com/bleurt-oss-21/BLEURT-20-D3.zip', 'BLEURT-20-D6': 'https://storage.googleapis.com/bleurt-oss-21/BLEURT-20-D6.zip', 'BLEURT-20-D12': 'https://storage.googleapis.com/bleurt-oss-21/BLEURT-20-D12.zip', 'BLEURT-20': 'https://storage.googleapis.com/bleurt-oss-21/BLEURT-20.zip', } @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION , _KWARGS_DESCRIPTION ) class _a ( datasets.Metric ): """simple docstring""" def __A ( self : Tuple ): return datasets.MetricInfo( description=_DESCRIPTION , citation=_CITATION , homepage="https://github.com/google-research/bleurt" , inputs_description=_KWARGS_DESCRIPTION , features=datasets.Features( { "predictions": datasets.Value("string" , id="sequence" ), "references": datasets.Value("string" , id="sequence" ), } ) , codebase_urls=["https://github.com/google-research/bleurt"] , reference_urls=["https://github.com/google-research/bleurt", "https://arxiv.org/abs/2004.04696"] , ) def __A ( self : List[str] , UpperCAmelCase : Dict ): # check that config name specifies a valid BLEURT model if self.config_name == "default": logger.warning( "Using default BLEURT-Base checkpoint for sequence maximum length 128. " "You can use a bigger model for better results with e.g.: datasets.load_metric('bleurt', 'bleurt-large-512')." ) A_ = "bleurt-base-128" if self.config_name.lower() in CHECKPOINT_URLS: A_ = self.config_name.lower() elif self.config_name.upper() in CHECKPOINT_URLS: A_ = self.config_name.upper() else: raise KeyError( f'''{self.config_name} model not found. You should supply the name of a model checkpoint for bleurt in {CHECKPOINT_URLS.keys()}''' ) # download the model checkpoint specified by self.config_name and set up the scorer A_ = dl_manager.download_and_extract(CHECKPOINT_URLS[checkpoint_name] ) A_ = score.BleurtScorer(os.path.join(UpperCAmelCase , UpperCAmelCase ) ) def __A ( self : str , UpperCAmelCase : Optional[int] , UpperCAmelCase : Union[str, Any] ): A_ = self.scorer.score(references=UpperCAmelCase , candidates=UpperCAmelCase ) return {"scores": scores}
312
# flake8: noqa # Lint as: python3 from typing import Dict, List, Optional, Type from .. import config from ..utils import logging from .formatting import ( ArrowFormatter, CustomFormatter, Formatter, PandasFormatter, PythonFormatter, TensorFormatter, format_table, query_table, ) from .np_formatter import NumpyFormatter __a :Optional[Any] = logging.get_logger(__name__) __a :Dict[Optional[str], Type[Formatter]] = {} __a :Dict[Optional[str], str] = {} __a :Dict[Optional[str], Exception] = {} def __snake_case ( __UpperCamelCase : type ,__UpperCamelCase : Optional[str] ,__UpperCamelCase : Optional[List[str]] = None ,): """simple docstring""" A_ = aliases if aliases is not None else [] if format_type in _FORMAT_TYPES: logger.warning( f'''Overwriting format type \'{format_type}\' ({_FORMAT_TYPES[format_type].__name__} -> {formatter_cls.__name__})''' ) A_ = formatter_cls for alias in set(aliases + [format_type] ): if alias in _FORMAT_TYPES_ALIASES: logger.warning( f'''Overwriting format type alias \'{alias}\' ({_FORMAT_TYPES_ALIASES[alias]} -> {format_type})''' ) A_ = format_type def __snake_case ( __UpperCamelCase : Exception ,__UpperCamelCase : Optional[str] ,__UpperCamelCase : Optional[List[str]] = None ): """simple docstring""" A_ = aliases if aliases is not None else [] for alias in set(aliases + [format_type] ): A_ = unavailable_error # Here we define all the available formatting functions that can be used by `Dataset.set_format` _register_formatter(PythonFormatter, None, aliases=['python']) _register_formatter(ArrowFormatter, 'arrow', aliases=['pa', 'pyarrow']) _register_formatter(NumpyFormatter, 'numpy', aliases=['np']) _register_formatter(PandasFormatter, 'pandas', aliases=['pd']) _register_formatter(CustomFormatter, 'custom') if config.TORCH_AVAILABLE: from .torch_formatter import TorchFormatter _register_formatter(TorchFormatter, 'torch', aliases=['pt', 'pytorch']) else: __a :List[Any] = ValueError('PyTorch needs to be installed to be able to return PyTorch tensors.') _register_unavailable_formatter(_torch_error, 'torch', aliases=['pt', 'pytorch']) if config.TF_AVAILABLE: from .tf_formatter import TFFormatter _register_formatter(TFFormatter, 'tensorflow', aliases=['tf']) else: __a :List[str] = ValueError('Tensorflow needs to be installed to be able to return Tensorflow tensors.') _register_unavailable_formatter(_tf_error, 'tensorflow', aliases=['tf']) if config.JAX_AVAILABLE: from .jax_formatter import JaxFormatter _register_formatter(JaxFormatter, 'jax', aliases=[]) else: __a :Tuple = ValueError('JAX needs to be installed to be able to return JAX arrays.') _register_unavailable_formatter(_jax_error, 'jax', aliases=[]) def __snake_case ( __UpperCamelCase : Optional[str] ): """simple docstring""" if format_type in _FORMAT_TYPES_ALIASES: return _FORMAT_TYPES_ALIASES[format_type] else: return format_type def __snake_case ( __UpperCamelCase : Optional[str] ,**__UpperCamelCase : List[Any] ): """simple docstring""" A_ = get_format_type_from_alias(__UpperCamelCase ) if format_type in _FORMAT_TYPES: return _FORMAT_TYPES[format_type](**__UpperCamelCase ) if format_type in _FORMAT_TYPES_ALIASES_UNAVAILABLE: raise _FORMAT_TYPES_ALIASES_UNAVAILABLE[format_type] else: raise ValueError( f'''Return type should be None or selected in {list(type for type in _FORMAT_TYPES.keys() if type != None )}, but got \'{format_type}\'''' )
312
1
def __snake_case ( __UpperCamelCase : int = 1000 ): """simple docstring""" return sum(e for e in range(3 ,__UpperCamelCase ) if e % 3 == 0 or e % 5 == 0 ) if __name__ == "__main__": print(F"{solution() = }")
312
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available, is_vision_available __a :int = { 'configuration_mask2former': [ 'MASK2FORMER_PRETRAINED_CONFIG_ARCHIVE_MAP', 'Mask2FormerConfig', ], } try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __a :Union[str, Any] = ['Mask2FormerImageProcessor'] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __a :Optional[Any] = [ 'MASK2FORMER_PRETRAINED_MODEL_ARCHIVE_LIST', 'Mask2FormerForUniversalSegmentation', 'Mask2FormerModel', 'Mask2FormerPreTrainedModel', ] if TYPE_CHECKING: from .configuration_maskaformer import MASK2FORMER_PRETRAINED_CONFIG_ARCHIVE_MAP, MaskaFormerConfig try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .image_processing_maskaformer import MaskaFormerImageProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_maskaformer import ( MASK2FORMER_PRETRAINED_MODEL_ARCHIVE_LIST, MaskaFormerForUniversalSegmentation, MaskaFormerModel, MaskaFormerPreTrainedModel, ) else: import sys __a :Tuple = _LazyModule(__name__, globals()['__file__'], _import_structure)
312
1
import argparse import os import pickle import sys import torch from transformers import TransfoXLConfig, TransfoXLLMHeadModel, load_tf_weights_in_transfo_xl from transformers.models.transfo_xl import tokenization_transfo_xl as data_utils from transformers.models.transfo_xl.tokenization_transfo_xl import CORPUS_NAME, VOCAB_FILES_NAMES from transformers.utils import CONFIG_NAME, WEIGHTS_NAME, logging logging.set_verbosity_info() # We do this to be able to load python 2 datasets pickles # See e.g. https://stackoverflow.com/questions/2121874/python-pickling-after-changing-a-modules-directory/2121918#2121918 __a :Tuple = data_utils.TransfoXLTokenizer __a :Optional[int] = data_utils.TransfoXLCorpus __a :str = data_utils __a :Union[str, Any] = data_utils def __snake_case ( __UpperCamelCase : List[Any] ,__UpperCamelCase : Tuple ,__UpperCamelCase : Any ,__UpperCamelCase : List[Any] ): """simple docstring""" if transfo_xl_dataset_file: # Convert a pre-processed corpus (see original TensorFlow repo) with open(__UpperCamelCase ,"rb" ) as fp: A_ = pickle.load(__UpperCamelCase ,encoding="latin1" ) # Save vocabulary and dataset cache as Dictionaries (should be better than pickles for the long-term) A_ = pytorch_dump_folder_path + "/" + VOCAB_FILES_NAMES["pretrained_vocab_file"] print(f'''Save vocabulary to {pytorch_vocab_dump_path}''' ) A_ = corpus.vocab.__dict__ torch.save(__UpperCamelCase ,__UpperCamelCase ) A_ = corpus.__dict__ corpus_dict_no_vocab.pop("vocab" ,__UpperCamelCase ) A_ = pytorch_dump_folder_path + "/" + CORPUS_NAME print(f'''Save dataset to {pytorch_dataset_dump_path}''' ) torch.save(__UpperCamelCase ,__UpperCamelCase ) if tf_checkpoint_path: # Convert a pre-trained TensorFlow model A_ = os.path.abspath(__UpperCamelCase ) A_ = os.path.abspath(__UpperCamelCase ) print(f'''Converting Transformer XL checkpoint from {tf_path} with config at {config_path}.''' ) # Initialise PyTorch model if transfo_xl_config_file == "": A_ = TransfoXLConfig() else: A_ = TransfoXLConfig.from_json_file(__UpperCamelCase ) print(f'''Building PyTorch model from configuration: {config}''' ) A_ = TransfoXLLMHeadModel(__UpperCamelCase ) A_ = load_tf_weights_in_transfo_xl(__UpperCamelCase ,__UpperCamelCase ,__UpperCamelCase ) # Save pytorch-model A_ = os.path.join(__UpperCamelCase ,__UpperCamelCase ) A_ = os.path.join(__UpperCamelCase ,__UpperCamelCase ) print(f'''Save PyTorch model to {os.path.abspath(__UpperCamelCase )}''' ) torch.save(model.state_dict() ,__UpperCamelCase ) print(f'''Save configuration file to {os.path.abspath(__UpperCamelCase )}''' ) with open(__UpperCamelCase ,"w" ,encoding="utf-8" ) as f: f.write(config.to_json_string() ) if __name__ == "__main__": __a :Tuple = argparse.ArgumentParser() parser.add_argument( '--pytorch_dump_folder_path', default=None, type=str, required=True, help='Path to the folder to store the PyTorch model or dataset/vocab.', ) parser.add_argument( '--tf_checkpoint_path', default='', type=str, help='An optional path to a TensorFlow checkpoint path to be converted.', ) parser.add_argument( '--transfo_xl_config_file', default='', type=str, help=( 'An optional config json file corresponding to the pre-trained BERT model. \n' 'This specifies the model architecture.' ), ) parser.add_argument( '--transfo_xl_dataset_file', default='', type=str, help='An optional dataset file to be converted in a vocabulary.', ) __a :Dict = parser.parse_args() convert_transfo_xl_checkpoint_to_pytorch( args.tf_checkpoint_path, args.transfo_xl_config_file, args.pytorch_dump_folder_path, args.transfo_xl_dataset_file, )
312
import copy from dataclasses import dataclass, field from typing import ClassVar, Dict from ..features import Audio, ClassLabel, Features from .base import TaskTemplate @dataclass(frozen=snake_case_ ) class _a ( snake_case_ ): """simple docstring""" _lowerCamelCase : str = field(default='audio-classification' , metadata={'include_in_asdict_even_if_is_default': True} ) _lowerCamelCase : ClassVar[Features] = Features({'audio': Audio()} ) _lowerCamelCase : ClassVar[Features] = Features({'labels': ClassLabel} ) _lowerCamelCase : str = "audio" _lowerCamelCase : str = "labels" def __A ( self : str , UpperCAmelCase : List[Any] ): if self.label_column not in features: raise ValueError(f'''Column {self.label_column} is not present in features.''' ) if not isinstance(features[self.label_column] , UpperCAmelCase ): raise ValueError(f'''Column {self.label_column} is not a ClassLabel.''' ) A_ = copy.deepcopy(self ) A_ = self.label_schema.copy() A_ = features[self.label_column] A_ = label_schema return task_template @property def __A ( self : List[str] ): return { self.audio_column: "audio", self.label_column: "labels", }
312
1
import argparse import json import os import fairseq import torch from torch import nn from transformers import ( SpeechaTextaConfig, SpeechaTextaForCausalLM, SpeechaTextaTokenizer, SpeechEncoderDecoderConfig, SpeechEncoderDecoderModel, WavaVecaConfig, WavaVecaFeatureExtractor, WavaVecaModel, logging, ) logging.set_verbosity_info() __a :Any = logging.get_logger(__name__) __a :Dict = { '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', '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', } __a :Tuple = [ 'lm_head', 'quantizer.weight_proj', 'quantizer.codevectors', 'project_q', 'project_hid', ] def __snake_case ( __UpperCamelCase : List[Any] ,__UpperCamelCase : Tuple ,__UpperCamelCase : Union[str, Any] ,__UpperCamelCase : Dict ,__UpperCamelCase : int ): """simple docstring""" for attribute in key.split("." ): A_ = getattr(__UpperCamelCase ,__UpperCamelCase ) if weight_type is not None: A_ = getattr(__UpperCamelCase ,__UpperCamelCase ).shape else: A_ = hf_pointer.shape assert hf_shape == value.shape, ( 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": A_ = value elif weight_type == "weight_g": A_ = value elif weight_type == "weight_v": A_ = value elif weight_type == "bias": A_ = value else: A_ = value logger.info(f'''{key + "." + weight_type if weight_type is not None else ""} was initialized from {full_name}.''' ) def __snake_case ( __UpperCamelCase : Tuple ,__UpperCamelCase : Union[str, Any] ): """simple docstring""" A_ = [] A_ = fairseq_model.state_dict() A_ = hf_model.feature_extractor # if encoder has different dim to decoder -> use proj_weight A_ = None for name, value in fairseq_dict.items(): A_ = False if "conv_layers" in name: load_conv_layer( __UpperCamelCase ,__UpperCamelCase ,__UpperCamelCase ,__UpperCamelCase ,hf_model.config.feat_extract_norm == "group" ,) A_ = True elif name.split("." )[0] == "proj": A_ = fairseq_model.proj A_ = True else: for key, mapped_key in MAPPING.items(): if key in name or key.split("w2v_model." )[-1] == name.split("." )[0]: A_ = True if "*" in mapped_key: A_ = name.split(__UpperCamelCase )[0].split("." )[-2] A_ = mapped_key.replace("*" ,__UpperCamelCase ) if "weight_g" in name: A_ = "weight_g" elif "weight_v" in name: A_ = "weight_v" elif "bias" in name: A_ = "bias" elif "weight" in name: A_ = "weight" else: A_ = None set_recursively(__UpperCamelCase ,__UpperCamelCase ,__UpperCamelCase ,__UpperCamelCase ,__UpperCamelCase ) continue if not is_used: unused_weights.append(__UpperCamelCase ) logger.warning(f'''Unused weights: {unused_weights}''' ) return proj_weight def __snake_case ( __UpperCamelCase : Any ,__UpperCamelCase : List[Any] ,__UpperCamelCase : Tuple ,__UpperCamelCase : Any ,__UpperCamelCase : Optional[Any] ): """simple docstring""" A_ = full_name.split("conv_layers." )[-1] A_ = name.split("." ) A_ = int(items[0] ) A_ = int(items[1] ) if type_id == 0: if "bias" in name: assert value.shape == feature_extractor.conv_layers[layer_id].conv.bias.data.shape, ( f'''{full_name} has size {value.shape}, but''' f''' {feature_extractor.conv_layers[layer_id].conv.bias.data.shape} was found.''' ) A_ = value logger.info(f'''Feat extract conv layer {layer_id} was initialized from {full_name}.''' ) elif "weight" in name: assert value.shape == feature_extractor.conv_layers[layer_id].conv.weight.data.shape, ( f'''{full_name} has size {value.shape}, but''' f''' {feature_extractor.conv_layers[layer_id].conv.weight.data.shape} was found.''' ) A_ = 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: assert value.shape == feature_extractor.conv_layers[layer_id].layer_norm.bias.data.shape, ( f'''{full_name} has size {value.shape}, but {feature_extractor[layer_id].layer_norm.bias.data.shape} was''' " found." ) A_ = value logger.info(f'''Feat extract layer norm weight of layer {layer_id} was initialized from {full_name}.''' ) elif "weight" in name: assert value.shape == feature_extractor.conv_layers[layer_id].layer_norm.weight.data.shape, ( f'''{full_name} has size {value.shape}, but''' f''' {feature_extractor[layer_id].layer_norm.weight.data.shape} was found.''' ) A_ = value logger.info(f'''Feat extract layer norm weight of layer {layer_id} was initialized from {full_name}.''' ) else: unused_weights.append(__UpperCamelCase ) def __snake_case ( __UpperCamelCase : Any ): """simple docstring""" A_ , A_ = emb.weight.shape A_ = nn.Linear(__UpperCamelCase ,__UpperCamelCase ,bias=__UpperCamelCase ) A_ = emb.weight.data return lin_layer def __snake_case ( __UpperCamelCase : List[Any] ): """simple docstring""" with open(__UpperCamelCase ,"r" ,encoding="utf-8" ) as f: A_ = f.readlines() A_ = [line.split(" " )[0] for line in lines] A_ = len(__UpperCamelCase ) A_ = { "<s>": 0, "<pad>": 1, "</s>": 2, "<unk>": 3, } vocab_dict.update(dict(zip(__UpperCamelCase ,range(4 ,num_words + 4 ) ) ) ) return vocab_dict @torch.no_grad() def __snake_case ( __UpperCamelCase : int ,__UpperCamelCase : Dict ,__UpperCamelCase : Optional[int] ,__UpperCamelCase : Optional[int] ,__UpperCamelCase : str ,__UpperCamelCase : List[str] ,__UpperCamelCase : Dict ,): """simple docstring""" A_ = WavaVecaConfig.from_pretrained(__UpperCamelCase ) A_ = SpeechaTextaConfig.from_pretrained( __UpperCamelCase ,vocab_size=__UpperCamelCase ,decoder_layers=__UpperCamelCase ,do_stable_layer_norm=__UpperCamelCase ) A_ = WavaVecaFeatureExtractor( feature_size=1 ,sampling_rate=1_6000 ,padding_value=0 ,do_normalize=__UpperCamelCase ,return_attention_mask=__UpperCamelCase ,) A_ , A_ , A_ = fairseq.checkpoint_utils.load_model_ensemble_and_task( [checkpoint_path] ,arg_overrides={"data": "/".join(dict_path.split("/" )[:-1] )} ) A_ = model[0].eval() # set weights for wav2vec2 encoder A_ = WavaVecaModel(__UpperCamelCase ) A_ = recursively_load_weights_wavaveca(model.encoder ,__UpperCamelCase ) A_ = SpeechaTextaForCausalLM(__UpperCamelCase ) A_ , A_ = hf_decoder.model.decoder.load_state_dict(model.decoder.state_dict() ,strict=__UpperCamelCase ) # set output linear layer unexpected_keys.remove("embed_out" ) A_ = nn.Parameter(model.decoder.embed_out.detach() ) # layer norm is init to identity matrix so leaving it is fine logger.warning(f'''The following keys are missing when loading the decoder weights: {missing_keys}''' ) logger.warning(f'''The following keys are unexpected when loading the decoder weights: {unexpected_keys}''' ) A_ = SpeechEncoderDecoderModel(encoder=__UpperCamelCase ,decoder=__UpperCamelCase ) A_ = False # add projection layer A_ = nn.Parameter(projection_layer.weight ) A_ = nn.Parameter(projection_layer.bias ) A_ = create_vocab_dict(__UpperCamelCase ) with open(os.path.join(__UpperCamelCase ,"vocab.json" ) ,"w" ) as fp: json.dump(__UpperCamelCase ,__UpperCamelCase ) A_ = SpeechaTextaTokenizer(os.path.join(__UpperCamelCase ,"vocab.json" ) ) tokenizer.save_pretrained(__UpperCamelCase ) A_ = hf_wavavec.config.to_dict() A_ = tokenizer.pad_token_id A_ = tokenizer.bos_token_id A_ = tokenizer.eos_token_id A_ = "speech_to_text_2" A_ = "wav2vec2" A_ = SpeechEncoderDecoderConfig.from_dict(__UpperCamelCase ) hf_wavavec.save_pretrained(__UpperCamelCase ) feature_extractor.save_pretrained(__UpperCamelCase ) if __name__ == "__main__": __a :int = 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( '--encoder_config_path', default='facebook/wav2vec2-large-lv60', type=str, help='Path to hf encoder wav2vec2 checkpoint config', ) parser.add_argument( '--decoder_config_path', default='facebook/s2t-small-mustc-en-fr-st', type=str, help='Path to hf decoder s2t checkpoint config', ) parser.add_argument('--vocab_size', default=1_0224, type=int, help='Vocab size of decoder') parser.add_argument('--num_decoder_layers', default=7, type=int, help='Number of decoder layers') __a :str = parser.parse_args() convert_wavaveca_checkpoint( args.checkpoint_path, args.pytorch_dump_folder_path, args.dict_path, encoder_config_path=args.encoder_config_path, decoder_config_path=args.decoder_config_path, vocab_size=args.vocab_size, num_decoder_layers=args.num_decoder_layers, )
312
def __snake_case ( __UpperCamelCase : bytes ): """simple docstring""" return "".join([hex(__UpperCamelCase )[2:].zfill(2 ).upper() for byte in list(__UpperCamelCase )] ) def __snake_case ( __UpperCamelCase : str ): """simple docstring""" if (len(__UpperCamelCase ) % 2) != 0: raise ValueError( "Base16 encoded data is invalid:\nData does not have an even number of hex digits." ) # Check the character set - the standard base16 alphabet # is uppercase according to RFC3548 section 6 if not set(__UpperCamelCase ) <= set("0123456789ABCDEF" ): raise ValueError( "Base16 encoded data is invalid:\nData is not uppercase hex or it contains invalid characters." ) # For every two hexadecimal digits (= a byte), turn it into an integer. # Then, string the result together into bytes, and return it. return bytes(int(data[i] + data[i + 1] ,16 ) for i in range(0 ,len(__UpperCamelCase ) ,2 ) ) if __name__ == "__main__": import doctest doctest.testmod()
312
1
import argparse import json from pathlib import Path import requests import torch from huggingface_hub import hf_hub_download from PIL import Image from transformers import BeitConfig, BeitForImageClassification, BeitForMaskedImageModeling, BeitImageProcessor from transformers.image_utils import PILImageResampling from transformers.utils import logging logging.set_verbosity_info() __a :Tuple = logging.get_logger(__name__) def __snake_case ( __UpperCamelCase : Union[str, Any] ,__UpperCamelCase : List[str]=False ,__UpperCamelCase : str=False ): """simple docstring""" A_ = "backbone." if is_semantic else "" A_ = [] for i in range(config.num_hidden_layers ): # encoder layers: output projection, 2 feedforward neural networks and 2 layernorms rename_keys.append((f'''{prefix}blocks.{i}.norm1.weight''', f'''beit.encoder.layer.{i}.layernorm_before.weight''') ) rename_keys.append((f'''{prefix}blocks.{i}.norm1.bias''', f'''beit.encoder.layer.{i}.layernorm_before.bias''') ) rename_keys.append( (f'''{prefix}blocks.{i}.attn.proj.weight''', f'''beit.encoder.layer.{i}.attention.output.dense.weight''') ) rename_keys.append( (f'''{prefix}blocks.{i}.attn.proj.bias''', f'''beit.encoder.layer.{i}.attention.output.dense.bias''') ) rename_keys.append((f'''{prefix}blocks.{i}.norm2.weight''', f'''beit.encoder.layer.{i}.layernorm_after.weight''') ) rename_keys.append((f'''{prefix}blocks.{i}.norm2.bias''', f'''beit.encoder.layer.{i}.layernorm_after.bias''') ) rename_keys.append((f'''{prefix}blocks.{i}.mlp.fc1.weight''', f'''beit.encoder.layer.{i}.intermediate.dense.weight''') ) rename_keys.append((f'''{prefix}blocks.{i}.mlp.fc1.bias''', f'''beit.encoder.layer.{i}.intermediate.dense.bias''') ) rename_keys.append((f'''{prefix}blocks.{i}.mlp.fc2.weight''', f'''beit.encoder.layer.{i}.output.dense.weight''') ) rename_keys.append((f'''{prefix}blocks.{i}.mlp.fc2.bias''', f'''beit.encoder.layer.{i}.output.dense.bias''') ) # projection layer + position embeddings rename_keys.extend( [ (f'''{prefix}cls_token''', "beit.embeddings.cls_token"), (f'''{prefix}patch_embed.proj.weight''', "beit.embeddings.patch_embeddings.projection.weight"), (f'''{prefix}patch_embed.proj.bias''', "beit.embeddings.patch_embeddings.projection.bias"), (f'''{prefix}pos_embed''', "beit.embeddings.position_embeddings"), ] ) if has_lm_head: # mask token + layernorm rename_keys.extend( [ ("mask_token", "beit.embeddings.mask_token"), ("norm.weight", "layernorm.weight"), ("norm.bias", "layernorm.bias"), ] ) else: # layernorm + classification head rename_keys.extend( [ ("fc_norm.weight", "beit.pooler.layernorm.weight"), ("fc_norm.bias", "beit.pooler.layernorm.bias"), ("head.weight", "classifier.weight"), ("head.bias", "classifier.bias"), ] ) return rename_keys def __snake_case ( __UpperCamelCase : Any ,__UpperCamelCase : Union[str, Any] ,__UpperCamelCase : Tuple=False ,__UpperCamelCase : Dict=False ): """simple docstring""" for i in range(config.num_hidden_layers ): A_ = "backbone." if is_semantic else "" # queries, keys and values A_ = state_dict.pop(f'''{prefix}blocks.{i}.attn.qkv.weight''' ) A_ = state_dict.pop(f'''{prefix}blocks.{i}.attn.q_bias''' ) A_ = state_dict.pop(f'''{prefix}blocks.{i}.attn.v_bias''' ) A_ = in_proj_weight[ : config.hidden_size, : ] A_ = q_bias A_ = in_proj_weight[ config.hidden_size : config.hidden_size * 2, : ] A_ = in_proj_weight[ -config.hidden_size :, : ] A_ = v_bias # gamma_1 and gamma_2 # we call them lambda because otherwise they are renamed when using .from_pretrained A_ = state_dict.pop(f'''{prefix}blocks.{i}.gamma_1''' ) A_ = state_dict.pop(f'''{prefix}blocks.{i}.gamma_2''' ) A_ = gamma_a A_ = gamma_a def __snake_case ( __UpperCamelCase : str ,__UpperCamelCase : List[str] ,__UpperCamelCase : Optional[Any] ): """simple docstring""" A_ = dct.pop(__UpperCamelCase ) A_ = val def __snake_case ( ): """simple docstring""" A_ = "http://images.cocodataset.org/val2017/000000039769.jpg" A_ = Image.open(requests.get(__UpperCamelCase ,stream=__UpperCamelCase ).raw ) return im @torch.no_grad() def __snake_case ( __UpperCamelCase : Optional[int] ,__UpperCamelCase : Optional[int] ,__UpperCamelCase : Tuple=False ): """simple docstring""" A_ = False if "rvlcdip" in checkpoint_url else True A_ = BeitConfig(use_absolute_position_embeddings=__UpperCamelCase ,use_mask_token=__UpperCamelCase ) # size of the architecture if "large" in checkpoint_url or "dit-l" in checkpoint_url: A_ = 1024 A_ = 4096 A_ = 24 A_ = 16 # labels if "rvlcdip" in checkpoint_url: A_ = 16 A_ = "huggingface/label-files" A_ = "rvlcdip-id2label.json" A_ = json.load(open(hf_hub_download(__UpperCamelCase ,__UpperCamelCase ,repo_type="dataset" ) ,"r" ) ) A_ = {int(__UpperCamelCase ): v for k, v in idalabel.items()} A_ = idalabel A_ = {v: k for k, v in idalabel.items()} # load state_dict of original model, remove and rename some keys A_ = torch.hub.load_state_dict_from_url(__UpperCamelCase ,map_location="cpu" )["model"] A_ = create_rename_keys(__UpperCamelCase ,has_lm_head=__UpperCamelCase ) for src, dest in rename_keys: rename_key(__UpperCamelCase ,__UpperCamelCase ,__UpperCamelCase ) read_in_q_k_v(__UpperCamelCase ,__UpperCamelCase ,has_lm_head=__UpperCamelCase ) # load HuggingFace model A_ = BeitForMaskedImageModeling(__UpperCamelCase ) if has_lm_head else BeitForImageClassification(__UpperCamelCase ) model.eval() model.load_state_dict(__UpperCamelCase ) # Check outputs on an image A_ = BeitImageProcessor( size=config.image_size ,resample=PILImageResampling.BILINEAR ,do_center_crop=__UpperCamelCase ) A_ = prepare_img() A_ = image_processor(images=__UpperCamelCase ,return_tensors="pt" ) A_ = encoding["pixel_values"] A_ = model(__UpperCamelCase ) A_ = outputs.logits # verify logits A_ = [1, 16] if "rvlcdip" in checkpoint_url else [1, 196, 8192] assert logits.shape == torch.Size(__UpperCamelCase ), "Shape of logits not as expected" Path(__UpperCamelCase ).mkdir(exist_ok=__UpperCamelCase ) print(f'''Saving model to {pytorch_dump_folder_path}''' ) model.save_pretrained(__UpperCamelCase ) print(f'''Saving image processor to {pytorch_dump_folder_path}''' ) image_processor.save_pretrained(__UpperCamelCase ) if push_to_hub: if has_lm_head: A_ = "dit-base" if "base" in checkpoint_url else "dit-large" else: A_ = "dit-base-finetuned-rvlcdip" if "dit-b" in checkpoint_url else "dit-large-finetuned-rvlcdip" image_processor.push_to_hub( repo_path_or_name=Path(__UpperCamelCase ,__UpperCamelCase ) ,organization="nielsr" ,commit_message="Add image processor" ,use_temp_dir=__UpperCamelCase ,) model.push_to_hub( repo_path_or_name=Path(__UpperCamelCase ,__UpperCamelCase ) ,organization="nielsr" ,commit_message="Add model" ,use_temp_dir=__UpperCamelCase ,) if __name__ == "__main__": __a :Tuple = argparse.ArgumentParser() parser.add_argument( '--checkpoint_url', default='https://layoutlm.blob.core.windows.net/dit/dit-pts/dit-base-224-p16-500k-62d53a.pth', type=str, help='URL to the original PyTorch checkpoint (.pth file).', ) parser.add_argument( '--pytorch_dump_folder_path', default=None, type=str, help='Path to the folder to output PyTorch model.' ) parser.add_argument( '--push_to_hub', action='store_true', ) __a :Dict = parser.parse_args() convert_dit_checkpoint(args.checkpoint_url, args.pytorch_dump_folder_path, args.push_to_hub)
312
import cva import numpy as np class _a : """simple docstring""" def __init__( self : Any , UpperCAmelCase : float , UpperCAmelCase : int ): if k in (0.04, 0.06): A_ = k A_ = window_size else: raise ValueError("invalid k value" ) def __str__( self : Optional[Any] ): return str(self.k ) def __A ( self : int , UpperCAmelCase : str ): A_ = cva.imread(UpperCAmelCase , 0 ) A_ , A_ = img.shape A_ = [] A_ = img.copy() A_ = cva.cvtColor(UpperCAmelCase , cva.COLOR_GRAY2RGB ) A_ , A_ = np.gradient(UpperCAmelCase ) A_ = dx**2 A_ = dy**2 A_ = dx * dy A_ = 0.04 A_ = self.window_size // 2 for y in range(UpperCAmelCase , h - offset ): for x in range(UpperCAmelCase , w - offset ): A_ = ixx[ y - offset : y + offset + 1, x - offset : x + offset + 1 ].sum() A_ = iyy[ y - offset : y + offset + 1, x - offset : x + offset + 1 ].sum() A_ = ixy[ y - offset : y + offset + 1, x - offset : x + offset + 1 ].sum() A_ = (wxx * wyy) - (wxy**2) A_ = wxx + wyy A_ = det - k * (trace**2) # Can change the value if r > 0.5: corner_list.append([x, y, r] ) color_img.itemset((y, x, 0) , 0 ) color_img.itemset((y, x, 1) , 0 ) color_img.itemset((y, x, 2) , 255 ) return color_img, corner_list if __name__ == "__main__": __a :List[str] = HarrisCorner(0.04, 3) __a , __a :str = edge_detect.detect('path_to_image') cva.imwrite('detect.png', color_img)
312
1
from ...configuration_utils import PretrainedConfig from ...utils import logging __a :List[str] = logging.get_logger(__name__) __a :Optional[Any] = { 'EleutherAI/gpt-neox-20b': 'https://huggingface.co/EleutherAI/gpt-neox-20b/resolve/main/config.json', # See all GPTNeoX models at https://huggingface.co/models?filter=gpt_neox } class _a ( snake_case_ ): """simple docstring""" _lowerCamelCase : int = 'gpt_neox' def __init__( self : Dict , UpperCAmelCase : Tuple=50432 , UpperCAmelCase : Optional[Any]=6144 , UpperCAmelCase : Optional[Any]=44 , UpperCAmelCase : List[Any]=64 , UpperCAmelCase : Optional[int]=24576 , UpperCAmelCase : Optional[Any]="gelu" , UpperCAmelCase : Tuple=0.25 , UpperCAmelCase : Union[str, Any]=10000 , UpperCAmelCase : List[Any]=0.0 , UpperCAmelCase : Tuple=0.0 , UpperCAmelCase : Optional[int]=0.1 , UpperCAmelCase : int=2048 , UpperCAmelCase : int=0.02 , UpperCAmelCase : Optional[Any]=1E-5 , UpperCAmelCase : Optional[int]=True , UpperCAmelCase : Optional[Any]=0 , UpperCAmelCase : Optional[Any]=2 , UpperCAmelCase : Any=False , UpperCAmelCase : Any=True , UpperCAmelCase : Any=None , **UpperCAmelCase : List[Any] , ): super().__init__(bos_token_id=UpperCAmelCase , eos_token_id=UpperCAmelCase , **UpperCAmelCase ) A_ = vocab_size A_ = max_position_embeddings A_ = hidden_size A_ = num_hidden_layers A_ = num_attention_heads A_ = intermediate_size A_ = hidden_act A_ = rotary_pct A_ = rotary_emb_base A_ = attention_dropout A_ = hidden_dropout A_ = classifier_dropout A_ = initializer_range A_ = layer_norm_eps A_ = use_cache A_ = tie_word_embeddings A_ = use_parallel_residual A_ = rope_scaling self._rope_scaling_validation() if self.hidden_size % self.num_attention_heads != 0: raise ValueError( "The hidden size is not divisble by the number of attention heads! Make sure to update them!" ) def __A ( self : Optional[Any] ): if self.rope_scaling is None: return if not isinstance(self.rope_scaling , UpperCAmelCase ) or len(self.rope_scaling ) != 2: raise ValueError( "`rope_scaling` must be a dictionary with with two fields, `name` and `factor`, " f'''got {self.rope_scaling}''' ) A_ = self.rope_scaling.get("type" , UpperCAmelCase ) A_ = self.rope_scaling.get("factor" , UpperCAmelCase ) if rope_scaling_type is None or rope_scaling_type not in ["linear", "dynamic"]: raise ValueError( f'''`rope_scaling`\'s name field must be one of [\'linear\', \'dynamic\'], got {rope_scaling_type}''' ) if rope_scaling_factor is None or not isinstance(UpperCAmelCase , UpperCAmelCase ) or rope_scaling_factor <= 1.0: raise ValueError(f'''`rope_scaling`\'s factor field must be an float > 1, got {rope_scaling_factor}''' )
312
def __snake_case ( __UpperCamelCase : int = 1000 ): """simple docstring""" return sum(2 * a * ((a - 1) // 2) for a in range(3 ,n + 1 ) ) if __name__ == "__main__": print(solution())
312
1
from typing import TYPE_CHECKING from ...utils import ( OptionalDependencyNotAvailable, _LazyModule, is_torch_available, ) __a :List[Any] = { 'configuration_swiftformer': [ 'SWIFTFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP', 'SwiftFormerConfig', 'SwiftFormerOnnxConfig', ] } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __a :Optional[Any] = [ 'SWIFTFORMER_PRETRAINED_MODEL_ARCHIVE_LIST', 'SwiftFormerForImageClassification', 'SwiftFormerModel', 'SwiftFormerPreTrainedModel', ] if TYPE_CHECKING: from .configuration_swiftformer import ( SWIFTFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP, SwiftFormerConfig, SwiftFormerOnnxConfig, ) try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_swiftformer import ( SWIFTFORMER_PRETRAINED_MODEL_ARCHIVE_LIST, SwiftFormerForImageClassification, SwiftFormerModel, SwiftFormerPreTrainedModel, ) else: import sys __a :Union[str, Any] = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
312
import warnings from typing import List import numpy as np from ...processing_utils import ProcessorMixin from ...tokenization_utils_base import BatchEncoding from ...utils import is_flax_available, is_tf_available, is_torch_available class _a ( snake_case_ ): """simple docstring""" _lowerCamelCase : Union[str, Any] = ['image_processor', 'tokenizer'] _lowerCamelCase : Tuple = 'OwlViTImageProcessor' _lowerCamelCase : List[Any] = ('CLIPTokenizer', 'CLIPTokenizerFast') def __init__( self : Optional[Any] , UpperCAmelCase : int=None , UpperCAmelCase : Union[str, Any]=None , **UpperCAmelCase : Any ): A_ = None if "feature_extractor" in kwargs: warnings.warn( "The `feature_extractor` argument is deprecated and will be removed in v5, use `image_processor`" " instead." , UpperCAmelCase , ) A_ = kwargs.pop("feature_extractor" ) A_ = image_processor if image_processor is not None else feature_extractor if image_processor is None: raise ValueError("You need to specify an `image_processor`." ) if tokenizer is None: raise ValueError("You need to specify a `tokenizer`." ) super().__init__(UpperCAmelCase , UpperCAmelCase ) def __call__( self : Optional[int] , UpperCAmelCase : List[str]=None , UpperCAmelCase : List[Any]=None , UpperCAmelCase : Optional[int]=None , UpperCAmelCase : Dict="max_length" , UpperCAmelCase : Optional[Any]="np" , **UpperCAmelCase : Optional[int] ): if text is None and query_images is None and images is None: raise ValueError( "You have to specify at least one text or query image or image. All three cannot be none." ) if text is not None: if isinstance(UpperCAmelCase , UpperCAmelCase ) or (isinstance(UpperCAmelCase , UpperCAmelCase ) and not isinstance(text[0] , UpperCAmelCase )): A_ = [self.tokenizer(UpperCAmelCase , padding=UpperCAmelCase , return_tensors=UpperCAmelCase , **UpperCAmelCase )] elif isinstance(UpperCAmelCase , UpperCAmelCase ) and isinstance(text[0] , UpperCAmelCase ): A_ = [] # Maximum number of queries across batch A_ = max([len(UpperCAmelCase ) for t in text] ) # Pad all batch samples to max number of text queries for t in text: if len(UpperCAmelCase ) != max_num_queries: A_ = t + [" "] * (max_num_queries - len(UpperCAmelCase )) A_ = self.tokenizer(UpperCAmelCase , padding=UpperCAmelCase , return_tensors=UpperCAmelCase , **UpperCAmelCase ) encodings.append(UpperCAmelCase ) else: raise TypeError("Input text should be a string, a list of strings or a nested list of strings" ) if return_tensors == "np": A_ = np.concatenate([encoding["input_ids"] for encoding in encodings] , axis=0 ) A_ = np.concatenate([encoding["attention_mask"] for encoding in encodings] , axis=0 ) elif return_tensors == "jax" and is_flax_available(): import jax.numpy as jnp A_ = jnp.concatenate([encoding["input_ids"] for encoding in encodings] , axis=0 ) A_ = jnp.concatenate([encoding["attention_mask"] for encoding in encodings] , axis=0 ) elif return_tensors == "pt" and is_torch_available(): import torch A_ = torch.cat([encoding["input_ids"] for encoding in encodings] , dim=0 ) A_ = torch.cat([encoding["attention_mask"] for encoding in encodings] , dim=0 ) elif return_tensors == "tf" and is_tf_available(): import tensorflow as tf A_ = tf.stack([encoding["input_ids"] for encoding in encodings] , axis=0 ) A_ = tf.stack([encoding["attention_mask"] for encoding in encodings] , axis=0 ) else: raise ValueError("Target return tensor type could not be returned" ) A_ = BatchEncoding() A_ = input_ids A_ = attention_mask if query_images is not None: A_ = BatchEncoding() A_ = self.image_processor( UpperCAmelCase , return_tensors=UpperCAmelCase , **UpperCAmelCase ).pixel_values A_ = query_pixel_values if images is not None: A_ = self.image_processor(UpperCAmelCase , return_tensors=UpperCAmelCase , **UpperCAmelCase ) if text is not None and images is not None: A_ = image_features.pixel_values return encoding elif query_images is not None and images is not None: A_ = image_features.pixel_values return encoding elif text is not None or query_images is not None: return encoding else: return BatchEncoding(data=dict(**UpperCAmelCase ) , tensor_type=UpperCAmelCase ) def __A ( self : Optional[Any] , *UpperCAmelCase : Union[str, Any] , **UpperCAmelCase : List[Any] ): return self.image_processor.post_process(*UpperCAmelCase , **UpperCAmelCase ) def __A ( self : str , *UpperCAmelCase : str , **UpperCAmelCase : Union[str, Any] ): return self.image_processor.post_process_object_detection(*UpperCAmelCase , **UpperCAmelCase ) def __A ( self : List[Any] , *UpperCAmelCase : int , **UpperCAmelCase : int ): return self.image_processor.post_process_image_guided_detection(*UpperCAmelCase , **UpperCAmelCase ) def __A ( self : List[Any] , *UpperCAmelCase : Optional[int] , **UpperCAmelCase : Any ): return self.tokenizer.batch_decode(*UpperCAmelCase , **UpperCAmelCase ) def __A ( self : Tuple , *UpperCAmelCase : Dict , **UpperCAmelCase : str ): return self.tokenizer.decode(*UpperCAmelCase , **UpperCAmelCase ) @property def __A ( self : Union[str, Any] ): warnings.warn( "`feature_extractor_class` is deprecated and will be removed in v5. Use `image_processor_class` instead." , UpperCAmelCase , ) return self.image_processor_class @property def __A ( self : Optional[Any] ): warnings.warn( "`feature_extractor` is deprecated and will be removed in v5. Use `image_processor` instead." , UpperCAmelCase , ) return self.image_processor
312
1
import os import sys import unittest __a :List[Any] = os.path.abspath(os.path.dirname(os.path.dirname(os.path.dirname(__file__)))) sys.path.append(os.path.join(git_repo_path, 'utils')) import get_test_info # noqa: E402 from get_test_info import ( # noqa: E402 get_model_to_test_mapping, get_model_to_tester_mapping, get_test_to_tester_mapping, ) __a :List[Any] = os.path.join('tests', 'models', 'bert', 'test_modeling_bert.py') __a :Tuple = os.path.join('tests', 'models', 'blip', 'test_modeling_blip.py') class _a ( unittest.TestCase ): """simple docstring""" def __A ( self : Optional[int] ): A_ = get_test_to_tester_mapping(UpperCAmelCase ) A_ = get_test_to_tester_mapping(UpperCAmelCase ) A_ = {"BertModelTest": "BertModelTester"} A_ = { "BlipModelTest": "BlipModelTester", "BlipTextImageModelTest": "BlipTextImageModelsModelTester", "BlipTextModelTest": "BlipTextModelTester", "BlipTextRetrievalModelTest": "BlipTextRetrievalModelTester", "BlipVQAModelTest": "BlipVQAModelTester", "BlipVisionModelTest": "BlipVisionModelTester", } self.assertEqual(get_test_info.to_json(UpperCAmelCase ) , UpperCAmelCase ) self.assertEqual(get_test_info.to_json(UpperCAmelCase ) , UpperCAmelCase ) def __A ( self : str ): A_ = get_model_to_test_mapping(UpperCAmelCase ) A_ = get_model_to_test_mapping(UpperCAmelCase ) A_ = { "BertForMaskedLM": ["BertModelTest"], "BertForMultipleChoice": ["BertModelTest"], "BertForNextSentencePrediction": ["BertModelTest"], "BertForPreTraining": ["BertModelTest"], "BertForQuestionAnswering": ["BertModelTest"], "BertForSequenceClassification": ["BertModelTest"], "BertForTokenClassification": ["BertModelTest"], "BertLMHeadModel": ["BertModelTest"], "BertModel": ["BertModelTest"], } A_ = { "BlipForConditionalGeneration": ["BlipTextImageModelTest"], "BlipForImageTextRetrieval": ["BlipTextRetrievalModelTest"], "BlipForQuestionAnswering": ["BlipVQAModelTest"], "BlipModel": ["BlipModelTest"], "BlipTextModel": ["BlipTextModelTest"], "BlipVisionModel": ["BlipVisionModelTest"], } self.assertEqual(get_test_info.to_json(UpperCAmelCase ) , UpperCAmelCase ) self.assertEqual(get_test_info.to_json(UpperCAmelCase ) , UpperCAmelCase ) def __A ( self : Any ): A_ = get_model_to_tester_mapping(UpperCAmelCase ) A_ = get_model_to_tester_mapping(UpperCAmelCase ) A_ = { "BertForMaskedLM": ["BertModelTester"], "BertForMultipleChoice": ["BertModelTester"], "BertForNextSentencePrediction": ["BertModelTester"], "BertForPreTraining": ["BertModelTester"], "BertForQuestionAnswering": ["BertModelTester"], "BertForSequenceClassification": ["BertModelTester"], "BertForTokenClassification": ["BertModelTester"], "BertLMHeadModel": ["BertModelTester"], "BertModel": ["BertModelTester"], } A_ = { "BlipForConditionalGeneration": ["BlipTextImageModelsModelTester"], "BlipForImageTextRetrieval": ["BlipTextRetrievalModelTester"], "BlipForQuestionAnswering": ["BlipVQAModelTester"], "BlipModel": ["BlipModelTester"], "BlipTextModel": ["BlipTextModelTester"], "BlipVisionModel": ["BlipVisionModelTester"], } self.assertEqual(get_test_info.to_json(UpperCAmelCase ) , UpperCAmelCase ) self.assertEqual(get_test_info.to_json(UpperCAmelCase ) , UpperCAmelCase )
312
from typing import Optional, Union import torch from torch import nn from ...configuration_utils import ConfigMixin, register_to_config from ...models.modeling_utils import ModelMixin class _a ( snake_case_ , snake_case_ ): """simple docstring""" @register_to_config def __init__( self : Dict , UpperCAmelCase : int = 768 , ): super().__init__() A_ = nn.Parameter(torch.zeros(1 , UpperCAmelCase ) ) A_ = nn.Parameter(torch.ones(1 , UpperCAmelCase ) ) def __A ( self : str , UpperCAmelCase : Optional[Union[str, torch.device]] = None , UpperCAmelCase : Optional[torch.dtype] = None , ): A_ = nn.Parameter(self.mean.to(UpperCAmelCase ).to(UpperCAmelCase ) ) A_ = nn.Parameter(self.std.to(UpperCAmelCase ).to(UpperCAmelCase ) ) return self def __A ( self : Dict , UpperCAmelCase : List[Any] ): A_ = (embeds - self.mean) * 1.0 / self.std return embeds def __A ( self : int , UpperCAmelCase : int ): A_ = (embeds * self.std) + self.mean return embeds
312
1
import tempfile import unittest from transformers import AutoModelForSeqaSeqLM, AutoTokenizer from transformers.testing_utils import ( is_torch_available, require_optimum, require_torch, slow, ) if is_torch_available(): import torch @require_torch @require_optimum @slow class _a ( unittest.TestCase ): """simple docstring""" def __A ( self : List[Any] ): A_ = "hf-internal-testing/tiny-random-t5" A_ = AutoTokenizer.from_pretrained(UpperCAmelCase ) A_ = AutoModelForSeqaSeqLM.from_pretrained(UpperCAmelCase ) A_ = tokenizer("This is me" , return_tensors="pt" ) A_ = model.to_bettertransformer() self.assertTrue(any("BetterTransformer" in mod.__class__.__name__ for _, mod in model.named_modules() ) ) A_ = model.generate(**UpperCAmelCase ) A_ = model.reverse_bettertransformer() self.assertFalse(any("BetterTransformer" in mod.__class__.__name__ for _, mod in model.named_modules() ) ) with tempfile.TemporaryDirectory() as tmpdirname: model.save_pretrained(UpperCAmelCase ) A_ = AutoModelForSeqaSeqLM.from_pretrained(UpperCAmelCase ) self.assertFalse( any("BetterTransformer" in mod.__class__.__name__ for _, mod in model_reloaded.named_modules() ) ) A_ = model_reloaded.generate(**UpperCAmelCase ) self.assertTrue(torch.allclose(UpperCAmelCase , UpperCAmelCase ) ) def __A ( self : Union[str, Any] ): A_ = "hf-internal-testing/tiny-random-t5" A_ = AutoModelForSeqaSeqLM.from_pretrained(UpperCAmelCase ) A_ = model.to_bettertransformer() with tempfile.TemporaryDirectory() as tmpdirname: with self.assertRaises(UpperCAmelCase ): model.save_pretrained(UpperCAmelCase ) A_ = model.reverse_bettertransformer() model.save_pretrained(UpperCAmelCase )
312
from __future__ import annotations import numpy as np from numpy import floataa from numpy.typing import NDArray def __snake_case ( __UpperCamelCase : NDArray[floataa] ,__UpperCamelCase : NDArray[floataa] ,__UpperCamelCase : list[int] ,__UpperCamelCase : int ,): """simple docstring""" A_ , A_ = coefficient_matrix.shape A_ , A_ = constant_matrix.shape if rowsa != colsa: A_ = f'''Coefficient matrix dimensions must be nxn but received {rowsa}x{colsa}''' raise ValueError(__UpperCamelCase ) if colsa != 1: A_ = f'''Constant matrix must be nx1 but received {rowsa}x{colsa}''' raise ValueError(__UpperCamelCase ) if rowsa != rowsa: A_ = ( "Coefficient and constant matrices dimensions must be nxn and nx1 but " f'''received {rowsa}x{colsa} and {rowsa}x{colsa}''' ) raise ValueError(__UpperCamelCase ) if len(__UpperCamelCase ) != rowsa: A_ = ( "Number of initial values must be equal to number of rows in coefficient " f'''matrix but received {len(__UpperCamelCase )} and {rowsa}''' ) raise ValueError(__UpperCamelCase ) if iterations <= 0: raise ValueError("Iterations must be at least 1" ) A_ = np.concatenate( (coefficient_matrix, constant_matrix) ,axis=1 ) A_ , A_ = table.shape strictly_diagonally_dominant(__UpperCamelCase ) # Iterates the whole matrix for given number of times for _ in range(__UpperCamelCase ): A_ = [] for row in range(__UpperCamelCase ): A_ = 0 for col in range(__UpperCamelCase ): if col == row: A_ = table[row][col] elif col == cols - 1: A_ = table[row][col] else: temp += (-1) * table[row][col] * init_val[col] A_ = (temp + val) / denom new_val.append(__UpperCamelCase ) A_ = new_val return [float(__UpperCamelCase ) for i in new_val] def __snake_case ( __UpperCamelCase : NDArray[floataa] ): """simple docstring""" A_ , A_ = table.shape A_ = True for i in range(0 ,__UpperCamelCase ): A_ = 0 for j in range(0 ,cols - 1 ): if i == j: continue else: total += table[i][j] if table[i][i] <= total: raise ValueError("Coefficient matrix is not strictly diagonally dominant" ) return is_diagonally_dominant # Test Cases if __name__ == "__main__": import doctest doctest.testmod()
312
1
import io import math from typing import Dict, Optional, Union import numpy as np from huggingface_hub import hf_hub_download from ...image_processing_utils import BaseImageProcessor, BatchFeature from ...image_transforms import convert_to_rgb, normalize, to_channel_dimension_format, to_pil_image from ...image_utils import ( ChannelDimension, ImageInput, get_image_size, infer_channel_dimension_format, make_list_of_images, to_numpy_array, valid_images, ) from ...utils import TensorType, is_torch_available, is_vision_available, logging from ...utils.import_utils import requires_backends if is_vision_available(): import textwrap from PIL import Image, ImageDraw, ImageFont if is_torch_available(): import torch from transformers.pytorch_utils import is_torch_greater_or_equal_than_1_11 else: __a :Union[str, Any] = False __a :Optional[Any] = logging.get_logger(__name__) __a :int = 'ybelkada/fonts' def __snake_case ( ): """simple docstring""" if is_torch_available() and not is_torch_greater_or_equal_than_1_11: raise ImportError( f'''You are using torch=={torch.__version__}, but torch>=1.11.0 is required to use ''' "Pix2StructImageProcessor. Please upgrade torch." ) def __snake_case ( __UpperCamelCase : List[Any] ,__UpperCamelCase : Dict ,__UpperCamelCase : Any ): """simple docstring""" requires_backends(__UpperCamelCase ,["torch"] ) _check_torch_version() A_ = image_tensor.unsqueeze(0 ) A_ = torch.nn.functional.unfold(__UpperCamelCase ,(patch_height, patch_width) ,stride=(patch_height, patch_width) ) A_ = patches.reshape(image_tensor.size(0 ) ,image_tensor.size(1 ) ,__UpperCamelCase ,__UpperCamelCase ,-1 ) A_ = patches.permute(0 ,4 ,2 ,3 ,1 ).reshape( image_tensor.size(2 ) // patch_height ,image_tensor.size(3 ) // patch_width ,image_tensor.size(1 ) * patch_height * patch_width ,) return patches.unsqueeze(0 ) def __snake_case ( __UpperCamelCase : str ,__UpperCamelCase : int = 36 ,__UpperCamelCase : str = "black" ,__UpperCamelCase : str = "white" ,__UpperCamelCase : int = 5 ,__UpperCamelCase : int = 5 ,__UpperCamelCase : int = 5 ,__UpperCamelCase : int = 5 ,__UpperCamelCase : Optional[bytes] = None ,__UpperCamelCase : Optional[str] = None ,): """simple docstring""" requires_backends(__UpperCamelCase ,"vision" ) # Add new lines so that each line is no more than 80 characters. A_ = textwrap.TextWrapper(width=80 ) A_ = wrapper.wrap(text=__UpperCamelCase ) A_ = "\n".join(__UpperCamelCase ) if font_bytes is not None and font_path is None: A_ = io.BytesIO(__UpperCamelCase ) elif font_path is not None: A_ = font_path else: A_ = hf_hub_download(__UpperCamelCase ,"Arial.TTF" ) A_ = ImageFont.truetype(__UpperCamelCase ,encoding="UTF-8" ,size=__UpperCamelCase ) # Use a temporary canvas to determine the width and height in pixels when # rendering the text. A_ = ImageDraw.Draw(Image.new("RGB" ,(1, 1) ,__UpperCamelCase ) ) A_ , A_ , A_ , A_ = temp_draw.textbbox((0, 0) ,__UpperCamelCase ,__UpperCamelCase ) # Create the actual image with a bit of padding around the text. A_ = text_width + left_padding + right_padding A_ = text_height + top_padding + bottom_padding A_ = Image.new("RGB" ,(image_width, image_height) ,__UpperCamelCase ) A_ = ImageDraw.Draw(__UpperCamelCase ) draw.text(xy=(left_padding, top_padding) ,text=__UpperCamelCase ,fill=__UpperCamelCase ,font=__UpperCamelCase ) return image def __snake_case ( __UpperCamelCase : np.ndarray ,__UpperCamelCase : str ,**__UpperCamelCase : Union[str, Any] ): """simple docstring""" requires_backends(__UpperCamelCase ,"vision" ) # Convert to PIL image if necessary A_ = to_pil_image(__UpperCamelCase ) A_ = render_text(__UpperCamelCase ,**__UpperCamelCase ) A_ = max(header_image.width ,image.width ) A_ = int(image.height * (new_width / image.width) ) A_ = int(header_image.height * (new_width / header_image.width) ) A_ = Image.new("RGB" ,(new_width, new_height + new_header_height) ,"white" ) new_image.paste(header_image.resize((new_width, new_header_height) ) ,(0, 0) ) new_image.paste(image.resize((new_width, new_height) ) ,(0, new_header_height) ) # Convert back to the original framework if necessary A_ = to_numpy_array(__UpperCamelCase ) if infer_channel_dimension_format(__UpperCamelCase ) == ChannelDimension.LAST: A_ = to_channel_dimension_format(__UpperCamelCase ,ChannelDimension.LAST ) return new_image class _a ( snake_case_ ): """simple docstring""" _lowerCamelCase : Optional[int] = ['flattened_patches'] def __init__( self : Dict , UpperCAmelCase : bool = True , UpperCAmelCase : bool = True , UpperCAmelCase : Dict[str, int] = None , UpperCAmelCase : int = 2048 , UpperCAmelCase : bool = False , **UpperCAmelCase : int , ): super().__init__(**UpperCAmelCase ) A_ = patch_size if patch_size is not None else {"height": 16, "width": 16} A_ = do_normalize A_ = do_convert_rgb A_ = max_patches A_ = is_vqa def __A ( self : int , UpperCAmelCase : np.ndarray , UpperCAmelCase : int , UpperCAmelCase : dict , **UpperCAmelCase : str ): requires_backends(self.extract_flattened_patches , "torch" ) _check_torch_version() # convert to torch A_ = to_channel_dimension_format(UpperCAmelCase , ChannelDimension.FIRST ) A_ = torch.from_numpy(UpperCAmelCase ) A_ , A_ = patch_size["height"], patch_size["width"] A_ , A_ = get_image_size(UpperCAmelCase ) # maximize scale s.t. A_ = math.sqrt(max_patches * (patch_height / image_height) * (patch_width / image_width) ) A_ = max(min(math.floor(scale * image_height / patch_height ) , UpperCAmelCase ) , 1 ) A_ = max(min(math.floor(scale * image_width / patch_width ) , UpperCAmelCase ) , 1 ) A_ = max(num_feasible_rows * patch_height , 1 ) A_ = max(num_feasible_cols * patch_width , 1 ) A_ = torch.nn.functional.interpolate( image.unsqueeze(0 ) , size=(resized_height, resized_width) , mode="bilinear" , align_corners=UpperCAmelCase , antialias=UpperCAmelCase , ).squeeze(0 ) # [1, rows, columns, patch_height * patch_width * image_channels] A_ = torch_extract_patches(UpperCAmelCase , UpperCAmelCase , UpperCAmelCase ) A_ = patches.shape A_ = patches_shape[1] A_ = patches_shape[2] A_ = patches_shape[3] # [rows * columns, patch_height * patch_width * image_channels] A_ = patches.reshape([rows * columns, depth] ) # [rows * columns, 1] A_ = torch.arange(UpperCAmelCase ).reshape([rows, 1] ).repeat(1 , UpperCAmelCase ).reshape([rows * columns, 1] ) A_ = torch.arange(UpperCAmelCase ).reshape([1, columns] ).repeat(UpperCAmelCase , 1 ).reshape([rows * columns, 1] ) # Offset by 1 so the ids do not contain zeros, which represent padding. row_ids += 1 col_ids += 1 # Prepare additional patch features. # [rows * columns, 1] A_ = row_ids.to(torch.floataa ) A_ = col_ids.to(torch.floataa ) # [rows * columns, 2 + patch_height * patch_width * image_channels] A_ = torch.cat([row_ids, col_ids, patches] , -1 ) # [max_patches, 2 + patch_height * patch_width * image_channels] A_ = torch.nn.functional.pad(UpperCAmelCase , [0, 0, 0, max_patches - (rows * columns)] ).float() A_ = to_numpy_array(UpperCAmelCase ) return result def __A ( self : Optional[Any] , UpperCAmelCase : np.ndarray , UpperCAmelCase : Optional[Union[str, ChannelDimension]] = None , **UpperCAmelCase : Any ): if image.dtype == np.uinta: A_ = image.astype(np.floataa ) # take mean across the whole `image` A_ = np.mean(UpperCAmelCase ) A_ = np.std(UpperCAmelCase ) A_ = max(UpperCAmelCase , 1.0 / math.sqrt(np.prod(image.shape ) ) ) return normalize(UpperCAmelCase , mean=UpperCAmelCase , std=UpperCAmelCase , **UpperCAmelCase ) def __A ( self : List[Any] , UpperCAmelCase : ImageInput , UpperCAmelCase : Optional[str] = None , UpperCAmelCase : bool = None , UpperCAmelCase : Optional[bool] = None , UpperCAmelCase : Optional[int] = None , UpperCAmelCase : Optional[Dict[str, int]] = None , UpperCAmelCase : Optional[Union[str, TensorType]] = None , UpperCAmelCase : ChannelDimension = ChannelDimension.FIRST , **UpperCAmelCase : Union[str, Any] , ): A_ = do_normalize if do_normalize is not None else self.do_normalize A_ = do_convert_rgb if do_convert_rgb is not None else self.do_convert_rgb A_ = patch_size if patch_size is not None else self.patch_size A_ = max_patches if max_patches is not None else self.max_patches A_ = self.is_vqa if kwargs.get("data_format" , UpperCAmelCase ) is not None: raise ValueError("data_format is not an accepted input as the outputs are " ) A_ = make_list_of_images(UpperCAmelCase ) if not valid_images(UpperCAmelCase ): raise ValueError( "Invalid image type. Must be of type PIL.Image.Image, numpy.ndarray, " "torch.Tensor, tf.Tensor or jax.ndarray." ) # PIL RGBA images are converted to RGB if do_convert_rgb: A_ = [convert_to_rgb(UpperCAmelCase ) for image in images] # All transformations expect numpy arrays. A_ = [to_numpy_array(UpperCAmelCase ) for image in images] if is_vqa: if header_text is None: raise ValueError("A header text must be provided for VQA models." ) A_ = kwargs.pop("font_bytes" , UpperCAmelCase ) A_ = kwargs.pop("font_path" , UpperCAmelCase ) if isinstance(UpperCAmelCase , UpperCAmelCase ): A_ = [header_text] * len(UpperCAmelCase ) A_ = [ render_header(UpperCAmelCase , header_text[i] , font_bytes=UpperCAmelCase , font_path=UpperCAmelCase ) for i, image in enumerate(UpperCAmelCase ) ] if do_normalize: A_ = [self.normalize(image=UpperCAmelCase ) for image in images] # convert to torch tensor and permute A_ = [ self.extract_flattened_patches(image=UpperCAmelCase , max_patches=UpperCAmelCase , patch_size=UpperCAmelCase ) for image in images ] # create attention mask in numpy A_ = [(image.sum(axis=-1 ) != 0).astype(np.floataa ) for image in images] A_ = BatchFeature( data={"flattened_patches": images, "attention_mask": attention_masks} , tensor_type=UpperCAmelCase ) return encoded_outputs
312
from unittest import TestCase from datasets import Dataset from minhash_deduplication import deduplicate_dataset, make_duplicate_clusters def __snake_case ( ): """simple docstring""" A_ = { "repo_name": ["test_repo1", "test_repo2", "test_repo3"], "path": ["test_1.py", "test_2.py", "unit_test.py"], "content": ["a " * 20, "a " * 30, "b " * 7], } A_ = Dataset.from_dict(__UpperCamelCase ) return dataset class _a ( snake_case_ ): """simple docstring""" def __A ( self : Union[str, Any] ): A_ = get_dataset() A_ = make_duplicate_clusters(UpperCAmelCase , 0.85 ) self.assertEqual(len(duplicate_clusters[0] ) , 2 ) def __A ( self : List[Any] ): A_ = get_dataset() A_ , A_ = deduplicate_dataset(UpperCAmelCase ) self.assertEqual(len(UpperCAmelCase ) , 2 ) print(UpperCAmelCase ) self.assertEqual(duplicate_clusters[0][0]["copies"] , 2 ) self.assertEqual(duplicate_clusters[0][0]["is_extreme"] , UpperCAmelCase )
312
1
import os from glob import glob import imageio import torch import torchvision import wandb from img_processing import custom_to_pil, loop_post_process, preprocess, preprocess_vqgan from loaders import load_vqgan from PIL import Image from torch import nn from transformers import CLIPModel, CLIPTokenizerFast from utils import get_device, get_timestamp, show_pil class _a : """simple docstring""" def __init__( self : Dict , UpperCAmelCase : str = "cpu" , UpperCAmelCase : str = "openai/clip-vit-large-patch14" ): A_ = device A_ = CLIPTokenizerFast.from_pretrained(UpperCAmelCase ) A_ = [0.48_145_466, 0.4_578_275, 0.40_821_073] A_ = [0.26_862_954, 0.26_130_258, 0.27_577_711] A_ = torchvision.transforms.Normalize(self.image_mean , self.image_std ) A_ = torchvision.transforms.Resize(224 ) A_ = torchvision.transforms.CenterCrop(224 ) def __A ( self : Dict , UpperCAmelCase : Tuple ): A_ = self.resize(UpperCAmelCase ) A_ = self.center_crop(UpperCAmelCase ) A_ = self.normalize(UpperCAmelCase ) return images def __call__( self : List[str] , UpperCAmelCase : Optional[Any]=None , UpperCAmelCase : Dict=None , **UpperCAmelCase : Optional[int] ): A_ = self.tokenizer(text=UpperCAmelCase , **UpperCAmelCase ) A_ = self.preprocess_img(UpperCAmelCase ) A_ = {key: value.to(self.device ) for (key, value) in encoding.items()} return encoding class _a ( nn.Module ): """simple docstring""" def __init__( self : Union[str, Any] , UpperCAmelCase : int=10 , UpperCAmelCase : List[str]=0.01 , UpperCAmelCase : str=None , UpperCAmelCase : List[str]=None , UpperCAmelCase : Optional[Any]=None , UpperCAmelCase : Union[str, Any]=None , UpperCAmelCase : Tuple=None , UpperCAmelCase : Dict=None , UpperCAmelCase : Any=False , UpperCAmelCase : Optional[Any]=True , UpperCAmelCase : Tuple="image" , UpperCAmelCase : Dict=True , UpperCAmelCase : Optional[int]=False , UpperCAmelCase : List[Any]=False , UpperCAmelCase : Union[str, Any]=False , ): super().__init__() A_ = None A_ = device if device else get_device() if vqgan: A_ = vqgan else: A_ = load_vqgan(self.device , conf_path=UpperCAmelCase , ckpt_path=UpperCAmelCase ) self.vqgan.eval() if clip: A_ = clip else: A_ = CLIPModel.from_pretrained("openai/clip-vit-base-patch32" ) self.clip.to(self.device ) A_ = ProcessorGradientFlow(device=self.device ) A_ = iterations A_ = lr A_ = log A_ = make_grid A_ = return_val A_ = quantize A_ = self.vqgan.decoder.z_shape def __A ( self : str , UpperCAmelCase : List[Any]=None , UpperCAmelCase : int=None , UpperCAmelCase : List[Any]=5 , UpperCAmelCase : Optional[Any]=True ): A_ = [] if output_path is None: A_ = "./animation.gif" if input_path is None: A_ = self.save_path A_ = sorted(glob(input_path + "/*" ) ) if not len(UpperCAmelCase ): raise ValueError( "No images found in save path, aborting (did you pass save_intermediate=True to the generate" " function?)" ) if len(UpperCAmelCase ) == 1: print("Only one image found in save path, (did you pass save_intermediate=True to the generate function?)" ) A_ = total_duration / len(UpperCAmelCase ) A_ = [frame_duration] * len(UpperCAmelCase ) if extend_frames: A_ = 1.5 A_ = 3 for file_name in paths: if file_name.endswith(".png" ): images.append(imageio.imread(UpperCAmelCase ) ) imageio.mimsave(UpperCAmelCase , UpperCAmelCase , duration=UpperCAmelCase ) print(f'''gif saved to {output_path}''' ) def __A ( self : str , UpperCAmelCase : Optional[Any]=None , UpperCAmelCase : List[Any]=None ): if not (path or img): raise ValueError("Input either path or tensor" ) if img is not None: raise NotImplementedError A_ = preprocess(Image.open(UpperCAmelCase ) , target_image_size=256 ).to(self.device ) A_ = preprocess_vqgan(UpperCAmelCase ) A_ , *A_ = self.vqgan.encode(UpperCAmelCase ) return z def __A ( self : Tuple , UpperCAmelCase : str ): A_ = self.latent.detach().requires_grad_() A_ = base_latent + transform_vector if self.quantize: A_ , *A_ = self.vqgan.quantize(UpperCAmelCase ) else: A_ = trans_latent return self.vqgan.decode(UpperCAmelCase ) def __A ( self : Dict , UpperCAmelCase : Tuple , UpperCAmelCase : Tuple , UpperCAmelCase : Optional[int]=None ): A_ = self.clip_preprocessor(text=UpperCAmelCase , images=UpperCAmelCase , return_tensors="pt" , padding=UpperCAmelCase ) A_ = self.clip(**UpperCAmelCase ) A_ = clip_outputs.logits_per_image if weights is not None: A_ = similarity_logits * weights return similarity_logits.sum() def __A ( self : Union[str, Any] , UpperCAmelCase : Dict , UpperCAmelCase : int , UpperCAmelCase : List[str] ): A_ = self._get_clip_similarity(pos_prompts["prompts"] , UpperCAmelCase , weights=(1 / pos_prompts["weights"]) ) if neg_prompts: A_ = self._get_clip_similarity(neg_prompts["prompts"] , UpperCAmelCase , weights=neg_prompts["weights"] ) else: A_ = torch.tensor([1] , device=self.device ) A_ = -torch.log(UpperCAmelCase ) + torch.log(UpperCAmelCase ) return loss def __A ( self : Any , UpperCAmelCase : Tuple , UpperCAmelCase : Union[str, Any] , UpperCAmelCase : Optional[int] ): A_ = torch.randn_like(self.latent , requires_grad=UpperCAmelCase , device=self.device ) A_ = torch.optim.Adam([vector] , lr=self.lr ) for i in range(self.iterations ): optim.zero_grad() A_ = self._add_vector(UpperCAmelCase ) A_ = loop_post_process(UpperCAmelCase ) A_ = self._get_CLIP_loss(UpperCAmelCase , UpperCAmelCase , UpperCAmelCase ) print("CLIP loss" , UpperCAmelCase ) if self.log: wandb.log({"CLIP Loss": clip_loss} ) clip_loss.backward(retain_graph=UpperCAmelCase ) optim.step() if self.return_val == "image": yield custom_to_pil(transformed_img[0] ) else: yield vector def __A ( self : List[str] , UpperCAmelCase : Optional[int] , UpperCAmelCase : Any , UpperCAmelCase : Union[str, Any] ): wandb.init(reinit=UpperCAmelCase , project="face-editor" ) wandb.config.update({"Positive Prompts": positive_prompts} ) wandb.config.update({"Negative Prompts": negative_prompts} ) wandb.config.update({"lr": self.lr, "iterations": self.iterations} ) if image_path: A_ = Image.open(UpperCAmelCase ) A_ = image.resize((256, 256) ) wandb.log("Original Image" , wandb.Image(UpperCAmelCase ) ) def __A ( self : List[str] , UpperCAmelCase : Any ): if not prompts: return [] A_ = [] A_ = [] if isinstance(UpperCAmelCase , UpperCAmelCase ): A_ = [prompt.strip() for prompt in prompts.split("|" )] for prompt in prompts: if isinstance(UpperCAmelCase , (tuple, list) ): A_ = prompt[0] A_ = float(prompt[1] ) elif ":" in prompt: A_ , A_ = prompt.split(":" ) A_ = float(UpperCAmelCase ) else: A_ = prompt A_ = 1.0 processed_prompts.append(UpperCAmelCase ) weights.append(UpperCAmelCase ) return { "prompts": processed_prompts, "weights": torch.tensor(UpperCAmelCase , device=self.device ), } def __A ( self : Tuple , UpperCAmelCase : str , UpperCAmelCase : Union[str, Any]=None , UpperCAmelCase : Optional[Any]=None , UpperCAmelCase : Union[str, Any]=True , UpperCAmelCase : str=False , UpperCAmelCase : List[str]=True , UpperCAmelCase : Union[str, Any]=True , UpperCAmelCase : str=None , ): if image_path: A_ = self._get_latent(UpperCAmelCase ) else: A_ = torch.randn(self.latent_dim , device=self.device ) if self.log: self._init_logging(UpperCAmelCase , UpperCAmelCase , UpperCAmelCase ) assert pos_prompts, "You must provide at least one positive prompt." A_ = self.process_prompts(UpperCAmelCase ) A_ = self.process_prompts(UpperCAmelCase ) if save_final and save_path is None: A_ = os.path.join("./outputs/" , "_".join(pos_prompts["prompts"] ) ) if not os.path.exists(UpperCAmelCase ): os.makedirs(UpperCAmelCase ) else: A_ = save_path + "_" + get_timestamp() os.makedirs(UpperCAmelCase ) A_ = save_path A_ = self.vqgan.decode(self.latent )[0] if show_intermediate: print("Original Image" ) show_pil(custom_to_pil(UpperCAmelCase ) ) A_ = loop_post_process(UpperCAmelCase ) for iter, transformed_img in enumerate(self._optimize_CLIP(UpperCAmelCase , UpperCAmelCase , UpperCAmelCase ) ): if show_intermediate: show_pil(UpperCAmelCase ) if save_intermediate: transformed_img.save(os.path.join(self.save_path , f'''iter_{iter:03d}.png''' ) ) if self.log: wandb.log({"Image": wandb.Image(UpperCAmelCase )} ) if show_final: show_pil(UpperCAmelCase ) if save_final: transformed_img.save(os.path.join(self.save_path , f'''iter_{iter:03d}_final.png''' ) )
312
import os from typing import Dict, List, Tuple, TypeVar, Union __a :Any = TypeVar('T') __a :Union[str, Any] = Union[List[T], Tuple[T, ...]] __a :List[str] = Union[T, List[T], Dict[str, T]] __a :Any = Union[str, bytes, os.PathLike]
312
1
import json import os import tempfile from unittest.mock import patch import torch from torch.utils.data import DataLoader, TensorDataset from accelerate import DistributedType, infer_auto_device_map, init_empty_weights from accelerate.accelerator import Accelerator from accelerate.state import GradientState, PartialState from accelerate.test_utils import require_bnb, require_multi_gpu, slow from accelerate.test_utils.testing import AccelerateTestCase, require_cuda from accelerate.utils import patch_environment def __snake_case ( ): """simple docstring""" A_ = torch.nn.Linear(2 ,4 ) A_ = torch.optim.AdamW(model.parameters() ,lr=1.0 ) A_ = torch.optim.lr_scheduler.OneCycleLR(__UpperCamelCase ,max_lr=0.01 ,steps_per_epoch=2 ,epochs=1 ) A_ = DataLoader(TensorDataset(torch.tensor([1, 2, 3] ) ) ) A_ = DataLoader(TensorDataset(torch.tensor([4, 5, 6] ) ) ) return model, optimizer, scheduler, train_dl, valid_dl def __snake_case ( __UpperCamelCase : Tuple ): """simple docstring""" return (model.weight.abs().sum() + model.bias.abs().sum()).item() def __snake_case ( __UpperCamelCase : List[Any] ): """simple docstring""" A_ = torch.nn.Linear(*tuple(model.weight.T.shape ) ).state_dict() model.load_state_dict(__UpperCamelCase ) class _a ( snake_case_ ): """simple docstring""" @require_cuda def __A ( self : Any ): A_ = Accelerator() assert PartialState._shared_state["_cpu"] is False assert PartialState._shared_state["device"].type == "cuda" with self.assertRaises(UpperCAmelCase ): A_ = Accelerator(cpu=UpperCAmelCase ) def __A ( self : Optional[Any] ): A_ = Accelerator() A_ = GradientState() assert state.num_steps == 1 A_ = 4 assert state.num_steps == 4 assert state.sync_gradients is True A_ = False assert state.sync_gradients is False GradientState._reset_state() def __A ( self : List[str] ): A_ = Accelerator() A_ , A_ , A_ , A_ , A_ = create_components() ( ( A_ ) , ( A_ ) , ( A_ ) , ( A_ ) , ( A_ ) , ) = accelerator.prepare(UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase ) self.assertTrue(prepared_model in accelerator._models ) self.assertTrue(prepared_optimizer in accelerator._optimizers ) self.assertTrue(prepared_scheduler in accelerator._schedulers ) self.assertTrue(prepared_train_dl in accelerator._dataloaders ) self.assertTrue(prepared_valid_dl in accelerator._dataloaders ) def __A ( self : Dict ): A_ = Accelerator() A_ , A_ , A_ , A_ , A_ = create_components() accelerator.prepare(UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase ) accelerator.free_memory() self.assertTrue(len(accelerator._models ) == 0 ) self.assertTrue(len(accelerator._optimizers ) == 0 ) self.assertTrue(len(accelerator._schedulers ) == 0 ) self.assertTrue(len(accelerator._dataloaders ) == 0 ) def __A ( self : List[str] ): PartialState._reset_state() # Mock torch.cuda.set_device to avoid an exception as the device doesn't exist def noop(*UpperCAmelCase : Dict , **UpperCAmelCase : List[Any] ): pass with patch("torch.cuda.set_device" , UpperCAmelCase ), patch_environment(ACCELERATE_TORCH_DEVICE="cuda:64" ): A_ = Accelerator() self.assertEqual(str(accelerator.state.device ) , "cuda:64" ) def __A ( self : List[Any] ): A_ = Accelerator() A_ , A_ , A_ , A_ , A_ = create_components() accelerator.prepare(UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase ) A_ = get_signature(UpperCAmelCase ) with tempfile.TemporaryDirectory() as tmpdirname: accelerator.save_state(UpperCAmelCase ) # make sure random weights don't match load_random_weights(UpperCAmelCase ) self.assertTrue(abs(model_signature - get_signature(UpperCAmelCase ) ) > 1E-3 ) # make sure loaded weights match accelerator.load_state(UpperCAmelCase ) self.assertTrue(abs(model_signature - get_signature(UpperCAmelCase ) ) < 1E-3 ) def __A ( self : Tuple ): A_ = Accelerator() A_ , A_ , A_ , A_ , A_ = create_components() accelerator.prepare(UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase ) A_ = get_signature(UpperCAmelCase ) # saving hook def save_config(UpperCAmelCase : Optional[Any] , UpperCAmelCase : Union[str, Any] , UpperCAmelCase : List[str] ): A_ = {"class_name": models[0].__class__.__name__} with open(os.path.join(UpperCAmelCase , "data.json" ) , "w" ) as f: json.dump(UpperCAmelCase , UpperCAmelCase ) # loading hook def load_config(UpperCAmelCase : Optional[int] , UpperCAmelCase : str ): with open(os.path.join(UpperCAmelCase , "data.json" ) , "r" ) as f: A_ = json.load(UpperCAmelCase ) A_ = config["class_name"] A_ = accelerator.register_save_state_pre_hook(UpperCAmelCase ) A_ = accelerator.register_load_state_pre_hook(UpperCAmelCase ) with tempfile.TemporaryDirectory() as tmpdirname: accelerator.save_state(UpperCAmelCase ) # make sure random weights don't match with hooks load_random_weights(UpperCAmelCase ) self.assertTrue(abs(model_signature - get_signature(UpperCAmelCase ) ) > 1E-3 ) # random class name to verify correct one is loaded A_ = "random" # make sure loaded weights match with hooks accelerator.load_state(UpperCAmelCase ) self.assertTrue(abs(model_signature - get_signature(UpperCAmelCase ) ) < 1E-3 ) # mode.class_name is loaded from config self.assertTrue(model.class_name == model.__class__.__name__ ) # remove hooks save_hook.remove() load_hook.remove() with tempfile.TemporaryDirectory() as tmpdirname: accelerator.save_state(UpperCAmelCase ) # make sure random weights don't match with hooks removed load_random_weights(UpperCAmelCase ) self.assertTrue(abs(model_signature - get_signature(UpperCAmelCase ) ) > 1E-3 ) # random class name to verify correct one is loaded A_ = "random" # make sure loaded weights match with hooks removed accelerator.load_state(UpperCAmelCase ) self.assertTrue(abs(model_signature - get_signature(UpperCAmelCase ) ) < 1E-3 ) # mode.class_name is NOT loaded from config self.assertTrue(model.class_name != model.__class__.__name__ ) def __A ( self : Dict ): A_ = Accelerator() A_ , A_ , A_ , A_ , A_ = create_components() A_ = None # This should work A_ , A_ , A_ , A_ , A_ , A_ = accelerator.prepare( UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase ) self.assertTrue(dummy_obj is None ) def __A ( self : Union[str, Any] ): A_ = Accelerator() A_ , A_ , A_ , A_ , A_ = create_components() A_ = [1, 2, 3] # This should work A_ , A_ , A_ , A_ , A_ , A_ = accelerator.prepare( UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase ) self.assertEqual( getattr(UpperCAmelCase , "_is_accelerate_prepared" , UpperCAmelCase ) , UpperCAmelCase , "Dummy object should have `_is_accelerate_prepared` set to `True`" , ) self.assertEqual( getattr(UpperCAmelCase , "_is_accelerate_prepared" , UpperCAmelCase ) , UpperCAmelCase , "Model is missing `_is_accelerator_prepared` or is set to `False`" , ) self.assertEqual( getattr(UpperCAmelCase , "_is_accelerate_prepared" , UpperCAmelCase ) , UpperCAmelCase , "Optimizer is missing `_is_accelerator_prepared` or is set to `False`" , ) self.assertEqual( getattr(UpperCAmelCase , "_is_accelerate_prepared" , UpperCAmelCase ) , UpperCAmelCase , "Scheduler is missing `_is_accelerator_prepared` or is set to `False`" , ) self.assertEqual( getattr(UpperCAmelCase , "_is_accelerate_prepared" , UpperCAmelCase ) , UpperCAmelCase , "Train Dataloader is missing `_is_accelerator_prepared` or is set to `False`" , ) self.assertEqual( getattr(UpperCAmelCase , "_is_accelerate_prepared" , UpperCAmelCase ) , UpperCAmelCase , "Valid Dataloader is missing `_is_accelerator_prepared` or is set to `False`" , ) @slow @require_bnb def __A ( self : List[Any] ): from transformers import AutoModelForCausalLM A_ = AutoModelForCausalLM.from_pretrained( "EleutherAI/gpt-neo-125m" , load_in_abit=UpperCAmelCase , device_map={"": 0} , ) A_ = Accelerator() # This should work A_ = accelerator.prepare(UpperCAmelCase ) @slow @require_bnb def __A ( self : List[str] ): from transformers import AutoModelForCausalLM A_ = Accelerator() with init_empty_weights(): A_ = AutoModelForCausalLM.from_pretrained( "EleutherAI/gpt-neo-125m" , ) model.tie_weights() A_ = infer_auto_device_map(UpperCAmelCase ) A_ = "cpu" A_ = AutoModelForCausalLM.from_pretrained( "EleutherAI/gpt-neo-125m" , device_map=UpperCAmelCase , load_in_abit=UpperCAmelCase , llm_inta_enable_fpaa_cpu_offload=UpperCAmelCase ) # This should not work and get value error with self.assertRaises(UpperCAmelCase ): A_ = accelerator.prepare(UpperCAmelCase ) @slow @require_bnb @require_multi_gpu def __A ( self : Any ): from transformers import AutoModelForCausalLM A_ = {"distributed_type": DistributedType.MULTI_GPU} with init_empty_weights(): A_ = AutoModelForCausalLM.from_pretrained( "EleutherAI/gpt-neo-125m" , ) model.tie_weights() A_ = infer_auto_device_map(UpperCAmelCase ) A_ = 1 A_ = AutoModelForCausalLM.from_pretrained( "EleutherAI/gpt-neo-125m" , load_in_abit=UpperCAmelCase , device_map=UpperCAmelCase , ) A_ = Accelerator() # This should not work and get value error with self.assertRaises(UpperCAmelCase ): A_ = accelerator.prepare(UpperCAmelCase ) PartialState._reset_state() @slow @require_bnb @require_multi_gpu def __A ( self : Optional[Any] ): from transformers import AutoModelForCausalLM with init_empty_weights(): A_ = AutoModelForCausalLM.from_pretrained( "EleutherAI/gpt-neo-125m" , ) A_ = infer_auto_device_map(UpperCAmelCase ) A_ = 1 A_ = AutoModelForCausalLM.from_pretrained( "EleutherAI/gpt-neo-125m" , load_in_abit=UpperCAmelCase , device_map=UpperCAmelCase , ) A_ = Accelerator() # This should work A_ = accelerator.prepare(UpperCAmelCase ) @require_cuda def __A ( self : List[Any] ): A_ = torch.nn.Linear(10 , 10 ) A_ = torch.optim.SGD(model.parameters() , lr=0.01 ) A_ = Accelerator(cpu=UpperCAmelCase ) A_ = accelerator.prepare(UpperCAmelCase )
312
__a :Dict = '0.18.2' from .configuration_utils import ConfigMixin from .utils import ( OptionalDependencyNotAvailable, is_flax_available, is_inflect_available, is_invisible_watermark_available, is_k_diffusion_available, is_k_diffusion_version, is_librosa_available, is_note_seq_available, is_onnx_available, is_scipy_available, is_torch_available, is_torchsde_available, is_transformers_available, is_transformers_version, is_unidecode_available, logging, ) try: if not is_onnx_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from .utils.dummy_onnx_objects import * # noqa F403 else: from .pipelines import OnnxRuntimeModel try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from .utils.dummy_pt_objects import * # noqa F403 else: from .models import ( AutoencoderKL, ControlNetModel, ModelMixin, PriorTransformer, TaFilmDecoder, TransformeraDModel, UNetaDModel, UNetaDConditionModel, UNetaDModel, UNetaDConditionModel, VQModel, ) from .optimization import ( get_constant_schedule, get_constant_schedule_with_warmup, get_cosine_schedule_with_warmup, get_cosine_with_hard_restarts_schedule_with_warmup, get_linear_schedule_with_warmup, get_polynomial_decay_schedule_with_warmup, get_scheduler, ) from .pipelines import ( AudioPipelineOutput, ConsistencyModelPipeline, DanceDiffusionPipeline, DDIMPipeline, DDPMPipeline, DiffusionPipeline, DiTPipeline, ImagePipelineOutput, KarrasVePipeline, LDMPipeline, LDMSuperResolutionPipeline, PNDMPipeline, RePaintPipeline, ScoreSdeVePipeline, ) from .schedulers import ( CMStochasticIterativeScheduler, DDIMInverseScheduler, DDIMParallelScheduler, DDIMScheduler, DDPMParallelScheduler, DDPMScheduler, DEISMultistepScheduler, DPMSolverMultistepInverseScheduler, DPMSolverMultistepScheduler, DPMSolverSinglestepScheduler, EulerAncestralDiscreteScheduler, EulerDiscreteScheduler, HeunDiscreteScheduler, IPNDMScheduler, KarrasVeScheduler, KDPMaAncestralDiscreteScheduler, KDPMaDiscreteScheduler, PNDMScheduler, RePaintScheduler, SchedulerMixin, ScoreSdeVeScheduler, UnCLIPScheduler, UniPCMultistepScheduler, VQDiffusionScheduler, ) from .training_utils import EMAModel try: if not (is_torch_available() and is_scipy_available()): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from .utils.dummy_torch_and_scipy_objects import * # noqa F403 else: from .schedulers import LMSDiscreteScheduler try: if not (is_torch_available() and is_torchsde_available()): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from .utils.dummy_torch_and_torchsde_objects import * # noqa F403 else: from .schedulers import DPMSolverSDEScheduler try: if not (is_torch_available() and is_transformers_available()): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from .utils.dummy_torch_and_transformers_objects import * # noqa F403 else: from .pipelines import ( AltDiffusionImgaImgPipeline, AltDiffusionPipeline, AudioLDMPipeline, CycleDiffusionPipeline, IFImgaImgPipeline, IFImgaImgSuperResolutionPipeline, IFInpaintingPipeline, IFInpaintingSuperResolutionPipeline, IFPipeline, IFSuperResolutionPipeline, ImageTextPipelineOutput, KandinskyImgaImgPipeline, KandinskyInpaintPipeline, KandinskyPipeline, KandinskyPriorPipeline, KandinskyVaaControlnetImgaImgPipeline, KandinskyVaaControlnetPipeline, KandinskyVaaImgaImgPipeline, KandinskyVaaInpaintPipeline, KandinskyVaaPipeline, KandinskyVaaPriorEmbaEmbPipeline, KandinskyVaaPriorPipeline, LDMTextToImagePipeline, PaintByExamplePipeline, SemanticStableDiffusionPipeline, ShapEImgaImgPipeline, ShapEPipeline, StableDiffusionAttendAndExcitePipeline, StableDiffusionControlNetImgaImgPipeline, StableDiffusionControlNetInpaintPipeline, StableDiffusionControlNetPipeline, StableDiffusionDepthaImgPipeline, StableDiffusionDiffEditPipeline, StableDiffusionImageVariationPipeline, StableDiffusionImgaImgPipeline, StableDiffusionInpaintPipeline, StableDiffusionInpaintPipelineLegacy, StableDiffusionInstructPixaPixPipeline, StableDiffusionLatentUpscalePipeline, StableDiffusionLDMaDPipeline, StableDiffusionModelEditingPipeline, StableDiffusionPanoramaPipeline, StableDiffusionParadigmsPipeline, StableDiffusionPipeline, StableDiffusionPipelineSafe, StableDiffusionPixaPixZeroPipeline, StableDiffusionSAGPipeline, StableDiffusionUpscalePipeline, StableUnCLIPImgaImgPipeline, StableUnCLIPPipeline, TextToVideoSDPipeline, TextToVideoZeroPipeline, UnCLIPImageVariationPipeline, UnCLIPPipeline, UniDiffuserModel, UniDiffuserPipeline, UniDiffuserTextDecoder, VersatileDiffusionDualGuidedPipeline, VersatileDiffusionImageVariationPipeline, VersatileDiffusionPipeline, VersatileDiffusionTextToImagePipeline, VideoToVideoSDPipeline, VQDiffusionPipeline, ) try: if not (is_torch_available() and is_transformers_available() and is_invisible_watermark_available()): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from .utils.dummy_torch_and_transformers_and_invisible_watermark_objects import * # noqa F403 else: from .pipelines import StableDiffusionXLImgaImgPipeline, StableDiffusionXLPipeline try: if not (is_torch_available() and is_transformers_available() and is_k_diffusion_available()): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from .utils.dummy_torch_and_transformers_and_k_diffusion_objects import * # noqa F403 else: from .pipelines import StableDiffusionKDiffusionPipeline try: if not (is_torch_available() and is_transformers_available() and is_onnx_available()): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from .utils.dummy_torch_and_transformers_and_onnx_objects import * # noqa F403 else: from .pipelines import ( OnnxStableDiffusionImgaImgPipeline, OnnxStableDiffusionInpaintPipeline, OnnxStableDiffusionInpaintPipelineLegacy, OnnxStableDiffusionPipeline, OnnxStableDiffusionUpscalePipeline, StableDiffusionOnnxPipeline, ) try: if not (is_torch_available() and is_librosa_available()): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from .utils.dummy_torch_and_librosa_objects import * # noqa F403 else: from .pipelines import AudioDiffusionPipeline, Mel try: if not (is_transformers_available() and is_torch_available() and is_note_seq_available()): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from .utils.dummy_transformers_and_torch_and_note_seq_objects import * # noqa F403 else: from .pipelines import SpectrogramDiffusionPipeline try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from .utils.dummy_flax_objects import * # noqa F403 else: from .models.controlnet_flax import FlaxControlNetModel from .models.modeling_flax_utils import FlaxModelMixin from .models.unet_ad_condition_flax import FlaxUNetaDConditionModel from .models.vae_flax import FlaxAutoencoderKL from .pipelines import FlaxDiffusionPipeline from .schedulers import ( FlaxDDIMScheduler, FlaxDDPMScheduler, FlaxDPMSolverMultistepScheduler, FlaxKarrasVeScheduler, FlaxLMSDiscreteScheduler, FlaxPNDMScheduler, FlaxSchedulerMixin, FlaxScoreSdeVeScheduler, ) try: if not (is_flax_available() and is_transformers_available()): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from .utils.dummy_flax_and_transformers_objects import * # noqa F403 else: from .pipelines import ( FlaxStableDiffusionControlNetPipeline, FlaxStableDiffusionImgaImgPipeline, FlaxStableDiffusionInpaintPipeline, FlaxStableDiffusionPipeline, ) try: if not (is_note_seq_available()): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from .utils.dummy_note_seq_objects import * # noqa F403 else: from .pipelines import MidiProcessor
312
1
import importlib.util import json import os import warnings from dataclasses import dataclass, field import torch from ..training_args import TrainingArguments from ..utils import cached_property, is_sagemaker_dp_enabled, logging __a :int = logging.get_logger(__name__) def __snake_case ( ): """simple docstring""" A_ = os.getenv("SM_HP_MP_PARAMETERS" ,"{}" ) try: # Parse it and check the field "partitions" is included, it is required for model parallel. A_ = json.loads(__UpperCamelCase ) if "partitions" not in smp_options: return False except json.JSONDecodeError: return False # Get the sagemaker specific framework parameters from mpi_options variable. A_ = os.getenv("SM_FRAMEWORK_PARAMS" ,"{}" ) try: # Parse it and check the field "sagemaker_distributed_dataparallel_enabled". A_ = json.loads(__UpperCamelCase ) if not mpi_options.get("sagemaker_mpi_enabled" ,__UpperCamelCase ): return False except json.JSONDecodeError: return False # Lastly, check if the `smdistributed` module is present. return importlib.util.find_spec("smdistributed" ) is not None if is_sagemaker_model_parallel_available(): import smdistributed.modelparallel.torch as smp smp.init() @dataclass class _a ( snake_case_ ): """simple docstring""" _lowerCamelCase : str = field( default='' , metadata={'help': 'Used by the SageMaker launcher to send mp-specific args. Ignored in SageMakerTrainer'} , ) def __A ( self : Tuple ): super().__post_init__() warnings.warn( "`SageMakerTrainingArguments` is deprecated and will be removed in v5 of Transformers. You can use " "`TrainingArguments` instead." , UpperCAmelCase , ) @cached_property def __A ( self : Optional[Any] ): logger.info("PyTorch: setting up devices" ) if torch.distributed.is_available() and torch.distributed.is_initialized() and self.local_rank == -1: logger.warning( "torch.distributed process group is initialized, but local_rank == -1. " "In order to use Torch DDP, launch your script with `python -m torch.distributed.launch" ) if self.no_cuda: A_ = torch.device("cpu" ) A_ = 0 elif is_sagemaker_model_parallel_available(): A_ = smp.local_rank() A_ = torch.device("cuda" , UpperCAmelCase ) A_ = 1 elif is_sagemaker_dp_enabled(): import smdistributed.dataparallel.torch.torch_smddp # noqa: F401 torch.distributed.init_process_group(backend="smddp" , timeout=self.ddp_timeout_delta ) A_ = int(os.getenv("SMDATAPARALLEL_LOCAL_RANK" ) ) A_ = torch.device("cuda" , self.local_rank ) A_ = 1 elif self.local_rank == -1: # if n_gpu is > 1 we'll use nn.DataParallel. # If you only want to use a specific subset of GPUs use `CUDA_VISIBLE_DEVICES=0` # Explicitly set CUDA to the first (index 0) CUDA device, otherwise `set_device` will # trigger an error that a device index is missing. Index 0 takes into account the # GPUs available in the environment, so `CUDA_VISIBLE_DEVICES=1,2` with `cuda:0` # will use the first GPU in that env, i.e. GPU#1 A_ = torch.device("cuda:0" if torch.cuda.is_available() else "cpu" ) # Sometimes the line in the postinit has not been run before we end up here, so just checking we're not at # the default value. A_ = torch.cuda.device_count() else: # Here, we'll use torch.distributed. # Initializes the distributed backend which will take care of synchronizing nodes/GPUs if not torch.distributed.is_initialized(): torch.distributed.init_process_group(backend="nccl" , timeout=self.ddp_timeout_delta ) A_ = torch.device("cuda" , self.local_rank ) A_ = 1 if device.type == "cuda": torch.cuda.set_device(UpperCAmelCase ) return device @property def __A ( self : Optional[Any] ): if is_sagemaker_model_parallel_available(): return smp.dp_size() return super().world_size @property def __A ( self : Union[str, Any] ): return not is_sagemaker_model_parallel_available() @property def __A ( self : int ): return False
312
def __snake_case ( __UpperCamelCase : int = 1000 ): """simple docstring""" return sum(e for e in range(3 ,__UpperCamelCase ) if e % 3 == 0 or e % 5 == 0 ) if __name__ == "__main__": print(F"{solution() = }")
312
1
from __future__ import annotations import copy import inspect import json import math import os import tempfile import unittest from importlib import import_module import numpy as np from transformers import ViTMAEConfig from transformers.file_utils import cached_property, is_tf_available, is_vision_available from transformers.testing_utils import require_tf, require_vision, slow 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 TFViTMAEForPreTraining, TFViTMAEModel if is_vision_available(): from PIL import Image from transformers import ViTImageProcessor class _a : """simple docstring""" def __init__( self : Tuple , UpperCAmelCase : Any , UpperCAmelCase : Dict=13 , UpperCAmelCase : str=30 , UpperCAmelCase : Optional[int]=2 , UpperCAmelCase : Any=3 , UpperCAmelCase : List[Any]=True , UpperCAmelCase : Optional[int]=True , UpperCAmelCase : Tuple=32 , UpperCAmelCase : Dict=2 , UpperCAmelCase : Dict=4 , UpperCAmelCase : Dict=37 , UpperCAmelCase : Optional[int]="gelu" , UpperCAmelCase : Union[str, Any]=0.1 , UpperCAmelCase : Dict=0.1 , UpperCAmelCase : Optional[int]=10 , UpperCAmelCase : int=0.02 , UpperCAmelCase : Tuple=3 , UpperCAmelCase : int=0.6 , UpperCAmelCase : Optional[int]=None , ): A_ = parent A_ = batch_size A_ = image_size A_ = patch_size A_ = num_channels A_ = is_training A_ = use_labels A_ = hidden_size A_ = num_hidden_layers A_ = num_attention_heads A_ = intermediate_size A_ = hidden_act A_ = hidden_dropout_prob A_ = attention_probs_dropout_prob A_ = type_sequence_label_size A_ = initializer_range A_ = mask_ratio A_ = scope # in ViTMAE, the expected sequence length = (num_patches + 1) * (1 - config.mask_ratio), rounded above # (we add 1 for the [CLS] token) A_ = (image_size // patch_size) ** 2 A_ = int(math.ceil((1 - mask_ratio) * (num_patches + 1) ) ) def __A ( self : List[str] ): A_ = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] ) A_ = None if self.use_labels: A_ = ids_tensor([self.batch_size] , self.type_sequence_label_size ) A_ = self.get_config() return config, pixel_values, labels def __A ( self : int ): return ViTMAEConfig( image_size=self.image_size , patch_size=self.patch_size , num_channels=self.num_channels , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , decoder_hidden_size=self.hidden_size , decoder_num_hidden_layers=self.num_hidden_layers , decoder_num_attention_heads=self.num_attention_heads , decoder_intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , is_decoder=UpperCAmelCase , initializer_range=self.initializer_range , mask_ratio=self.mask_ratio , ) def __A ( self : Dict , UpperCAmelCase : Union[str, Any] , UpperCAmelCase : Union[str, Any] , UpperCAmelCase : int ): A_ = TFViTMAEModel(config=UpperCAmelCase ) A_ = model(UpperCAmelCase , training=UpperCAmelCase ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def __A ( self : Any , UpperCAmelCase : Dict , UpperCAmelCase : str , UpperCAmelCase : Tuple ): A_ = TFViTMAEForPreTraining(UpperCAmelCase ) A_ = model(UpperCAmelCase , training=UpperCAmelCase ) # expected sequence length = num_patches A_ = (self.image_size // self.patch_size) ** 2 A_ = self.patch_size**2 * self.num_channels self.parent.assertEqual(result.logits.shape , (self.batch_size, num_patches, expected_num_channels) ) # test greyscale images A_ = 1 A_ = TFViTMAEForPreTraining(UpperCAmelCase ) A_ = floats_tensor([self.batch_size, 1, self.image_size, self.image_size] ) A_ = model(UpperCAmelCase , training=UpperCAmelCase ) A_ = self.patch_size**2 self.parent.assertEqual(result.logits.shape , (self.batch_size, num_patches, expected_num_channels) ) def __A ( self : Any ): A_ = self.prepare_config_and_inputs() ((A_) , (A_) , (A_)) = config_and_inputs A_ = {"pixel_values": pixel_values} return config, inputs_dict @require_tf class _a ( snake_case_ , snake_case_ , unittest.TestCase ): """simple docstring""" _lowerCamelCase : str = (TFViTMAEModel, TFViTMAEForPreTraining) if is_tf_available() else () _lowerCamelCase : Union[str, Any] = {'feature-extraction': TFViTMAEModel} if is_tf_available() else {} _lowerCamelCase : str = False _lowerCamelCase : List[Any] = False _lowerCamelCase : List[str] = False _lowerCamelCase : List[str] = False def __A ( self : List[str] ): A_ = TFViTMAEModelTester(self ) A_ = ConfigTester(self , config_class=UpperCAmelCase , has_text_modality=UpperCAmelCase , hidden_size=37 ) def __A ( self : Union[str, Any] ): self.config_tester.run_common_tests() @unittest.skip(reason="ViTMAE does not use inputs_embeds" ) def __A ( self : List[Any] ): pass def __A ( self : str ): A_ , A_ = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: A_ = model_class(UpperCAmelCase ) self.assertIsInstance(model.get_input_embeddings() , (tf.keras.layers.Layer) ) A_ = model.get_output_embeddings() self.assertTrue(x is None or isinstance(UpperCAmelCase , tf.keras.layers.Layer ) ) def __A ( self : Union[str, Any] ): A_ , A_ = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: A_ = model_class(UpperCAmelCase ) A_ = inspect.signature(model.call ) # signature.parameters is an OrderedDict => so arg_names order is deterministic A_ = [*signature.parameters.keys()] A_ = ["pixel_values"] self.assertListEqual(arg_names[:1] , UpperCAmelCase ) def __A ( self : Optional[int] ): A_ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*UpperCAmelCase ) def __A ( self : Optional[int] ): A_ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_pretraining(*UpperCAmelCase ) def __A ( self : Dict ): # make the mask reproducible np.random.seed(2 ) A_ , A_ = self.model_tester.prepare_config_and_inputs_for_common() A_ = int((config.image_size // config.patch_size) ** 2 ) A_ = np.random.uniform(size=(self.model_tester.batch_size, num_patches) ) for model_class in self.all_model_classes: A_ = model_class(UpperCAmelCase ) A_ = self._prepare_for_class(UpperCAmelCase , UpperCAmelCase ) A_ = model(UpperCAmelCase , noise=UpperCAmelCase ) A_ = copy.deepcopy(self._prepare_for_class(UpperCAmelCase , UpperCAmelCase ) ) A_ = model(**UpperCAmelCase , noise=UpperCAmelCase ) A_ = outputs_dict[0].numpy() A_ = outputs_keywords[0].numpy() self.assertLess(np.sum(np.abs(output_dict - output_keywords ) ) , 1E-6 ) def __A ( self : Dict ): # make the mask reproducible np.random.seed(2 ) A_ , A_ = self.model_tester.prepare_config_and_inputs_for_common() A_ = int((config.image_size // config.patch_size) ** 2 ) A_ = np.random.uniform(size=(self.model_tester.batch_size, num_patches) ) def prepare_numpy_arrays(UpperCAmelCase : str ): A_ = {} for k, v in inputs_dict.items(): if tf.is_tensor(UpperCAmelCase ): A_ = v.numpy() else: A_ = np.array(UpperCAmelCase ) return inputs_np_dict for model_class in self.all_model_classes: A_ = model_class(UpperCAmelCase ) A_ = self._prepare_for_class(UpperCAmelCase , UpperCAmelCase ) A_ = prepare_numpy_arrays(UpperCAmelCase ) A_ = model(UpperCAmelCase , noise=UpperCAmelCase ) A_ = model(**UpperCAmelCase , noise=UpperCAmelCase ) self.assert_outputs_same(UpperCAmelCase , UpperCAmelCase ) def __A ( self : List[Any] , UpperCAmelCase : List[Any] , UpperCAmelCase : Any , UpperCAmelCase : Optional[int] ): # make masks reproducible np.random.seed(2 ) A_ = int((tf_model.config.image_size // tf_model.config.patch_size) ** 2 ) A_ = np.random.uniform(size=(self.model_tester.batch_size, num_patches) ) A_ = tf.constant(UpperCAmelCase ) # Add `noise` argument. # PT inputs will be prepared in `super().check_pt_tf_models()` with this added `noise` argument A_ = tf_noise super().check_pt_tf_models(UpperCAmelCase , UpperCAmelCase , UpperCAmelCase ) def __A ( self : Any ): # make mask reproducible np.random.seed(2 ) A_ , A_ = self.model_tester.prepare_config_and_inputs_for_common() A_ = { module_member for model_class in self.all_model_classes for module in (import_module(model_class.__module__ ),) for module_member_name in dir(UpperCAmelCase ) if module_member_name.endswith("MainLayer" ) # This condition is required, since `modeling_tf_clip.py` has 3 classes whose names end with `MainLayer`. and module_member_name[: -len("MainLayer" )] == model_class.__name__[: -len("Model" )] for module_member in (getattr(UpperCAmelCase , UpperCAmelCase ),) if isinstance(UpperCAmelCase , UpperCAmelCase ) and tf.keras.layers.Layer in module_member.__bases__ and getattr(UpperCAmelCase , "_keras_serializable" , UpperCAmelCase ) } A_ = int((config.image_size // config.patch_size) ** 2 ) A_ = np.random.uniform(size=(self.model_tester.batch_size, num_patches) ) A_ = tf.convert_to_tensor(UpperCAmelCase ) inputs_dict.update({"noise": noise} ) for main_layer_class in tf_main_layer_classes: A_ = main_layer_class(UpperCAmelCase ) A_ = { name: tf.keras.Input(tensor.shape[1:] , dtype=tensor.dtype ) for name, tensor in inputs_dict.items() } A_ = tf.keras.Model(UpperCAmelCase , outputs=main_layer(UpperCAmelCase ) ) A_ = model(UpperCAmelCase ) with tempfile.TemporaryDirectory() as tmpdirname: A_ = os.path.join(UpperCAmelCase , "keras_model.h5" ) model.save(UpperCAmelCase ) A_ = tf.keras.models.load_model( UpperCAmelCase , custom_objects={main_layer_class.__name__: main_layer_class} ) assert isinstance(UpperCAmelCase , tf.keras.Model ) A_ = model(UpperCAmelCase ) self.assert_outputs_same(UpperCAmelCase , UpperCAmelCase ) @slow def __A ( self : Any ): # make mask reproducible np.random.seed(2 ) A_ , A_ = self.model_tester.prepare_config_and_inputs_for_common() A_ = int((config.image_size // config.patch_size) ** 2 ) A_ = np.random.uniform(size=(self.model_tester.batch_size, num_patches) ) for model_class in self.all_model_classes: A_ = model_class(UpperCAmelCase ) A_ = self._prepare_for_class(UpperCAmelCase , UpperCAmelCase ) A_ = model(UpperCAmelCase , noise=UpperCAmelCase ) if model_class.__name__ == "TFViTMAEModel": A_ = outputs.last_hidden_state.numpy() A_ = 0 else: A_ = outputs.logits.numpy() A_ = 0 with tempfile.TemporaryDirectory() as tmpdirname: model.save_pretrained(UpperCAmelCase , saved_model=UpperCAmelCase ) A_ = model_class.from_pretrained(UpperCAmelCase ) A_ = model(UpperCAmelCase , noise=UpperCAmelCase ) if model_class.__name__ == "TFViTMAEModel": A_ = after_outputs["last_hidden_state"].numpy() A_ = 0 else: A_ = after_outputs["logits"].numpy() A_ = 0 A_ = np.amax(np.abs(out_a - out_a ) ) self.assertLessEqual(UpperCAmelCase , 1E-5 ) def __A ( self : str ): # make mask reproducible np.random.seed(2 ) A_ , A_ = self.model_tester.prepare_config_and_inputs_for_common() A_ = int((config.image_size // config.patch_size) ** 2 ) A_ = np.random.uniform(size=(self.model_tester.batch_size, num_patches) ) for model_class in self.all_model_classes: A_ = model_class(UpperCAmelCase ) A_ = self._prepare_for_class(UpperCAmelCase , UpperCAmelCase ) A_ = model(UpperCAmelCase , noise=UpperCAmelCase ) A_ = model.get_config() # make sure that returned config is jsonifiable, which is required by keras json.dumps(UpperCAmelCase ) A_ = model_class.from_config(model.get_config() ) # make sure it also accepts a normal config A_ = model_class.from_config(model.config ) A_ = new_model(UpperCAmelCase ) # Build model new_model.set_weights(model.get_weights() ) A_ = new_model(UpperCAmelCase , noise=UpperCAmelCase ) self.assert_outputs_same(UpperCAmelCase , UpperCAmelCase ) @unittest.skip( reason="ViTMAE returns a random mask + ids_restore in each forward pass. See test_save_load\n to get deterministic results." ) def __A ( self : str ): pass @unittest.skip(reason="ViTMAE returns a random mask + ids_restore in each forward pass. See test_save_load" ) def __A ( self : Dict ): pass @slow def __A ( self : Tuple ): A_ = TFViTMAEModel.from_pretrained("google/vit-base-patch16-224" ) self.assertIsNotNone(UpperCAmelCase ) def __snake_case ( ): """simple docstring""" A_ = Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png" ) return image @require_tf @require_vision class _a ( unittest.TestCase ): """simple docstring""" @cached_property def __A ( self : str ): return ViTImageProcessor.from_pretrained("facebook/vit-mae-base" ) if is_vision_available() else None @slow def __A ( self : int ): # make random mask reproducible across the PT and TF model np.random.seed(2 ) A_ = TFViTMAEForPreTraining.from_pretrained("facebook/vit-mae-base" ) A_ = self.default_image_processor A_ = prepare_img() A_ = image_processor(images=UpperCAmelCase , return_tensors="tf" ) # prepare a noise vector that will be also used for testing the TF model # (this way we can ensure that the PT and TF models operate on the same inputs) A_ = ViTMAEConfig() A_ = int((vit_mae_config.image_size // vit_mae_config.patch_size) ** 2 ) A_ = np.random.uniform(size=(1, num_patches) ) # forward pass A_ = model(**UpperCAmelCase , noise=UpperCAmelCase ) # verify the logits A_ = tf.convert_to_tensor([1, 196, 768] ) self.assertEqual(outputs.logits.shape , UpperCAmelCase ) A_ = tf.convert_to_tensor( [[-0.0_548, -1.7_023, -0.9_325], [0.3_721, -0.5_670, -0.2_233], [0.8_235, -1.3_878, -0.3_524]] ) tf.debugging.assert_near(outputs.logits[0, :3, :3] , UpperCAmelCase , atol=1E-4 )
312
import unittest from typing import Tuple import torch from diffusers.utils import floats_tensor, randn_tensor, torch_all_close, torch_device from diffusers.utils.testing_utils import require_torch @require_torch class _a : """simple docstring""" @property def __A ( self : Union[str, Any] ): return self.get_dummy_input() @property def __A ( self : int ): if self.block_type == "down": return (4, 32, 16, 16) elif self.block_type == "mid": return (4, 32, 32, 32) elif self.block_type == "up": return (4, 32, 64, 64) raise ValueError(f'''\'{self.block_type}\' is not a supported block_type. Set it to \'up\', \'mid\', or \'down\'.''' ) def __A ( self : Union[str, Any] , UpperCAmelCase : List[Any]=True , UpperCAmelCase : str=False , UpperCAmelCase : Tuple=False , UpperCAmelCase : Optional[Any]=False , ): A_ = 4 A_ = 32 A_ = (32, 32) A_ = torch.manual_seed(0 ) A_ = torch.device(UpperCAmelCase ) A_ = (batch_size, num_channels) + sizes A_ = randn_tensor(UpperCAmelCase , generator=UpperCAmelCase , device=UpperCAmelCase ) A_ = {"hidden_states": hidden_states} if include_temb: A_ = 128 A_ = randn_tensor((batch_size, temb_channels) , generator=UpperCAmelCase , device=UpperCAmelCase ) if include_res_hidden_states_tuple: A_ = torch.manual_seed(1 ) A_ = (randn_tensor(UpperCAmelCase , generator=UpperCAmelCase , device=UpperCAmelCase ),) if include_encoder_hidden_states: A_ = floats_tensor((batch_size, 32, 32) ).to(UpperCAmelCase ) if include_skip_sample: A_ = randn_tensor(((batch_size, 3) + sizes) , generator=UpperCAmelCase , device=UpperCAmelCase ) return dummy_input def __A ( self : Optional[int] ): A_ = { "in_channels": 32, "out_channels": 32, "temb_channels": 128, } if self.block_type == "up": A_ = 32 if self.block_type == "mid": init_dict.pop("out_channels" ) A_ = self.dummy_input return init_dict, inputs_dict def __A ( self : List[str] , UpperCAmelCase : Optional[Any] ): A_ , A_ = self.prepare_init_args_and_inputs_for_common() A_ = self.block_class(**UpperCAmelCase ) unet_block.to(UpperCAmelCase ) unet_block.eval() with torch.no_grad(): A_ = unet_block(**UpperCAmelCase ) if isinstance(UpperCAmelCase , UpperCAmelCase ): A_ = output[0] self.assertEqual(output.shape , self.output_shape ) A_ = output[0, -1, -3:, -3:] A_ = torch.tensor(UpperCAmelCase ).to(UpperCAmelCase ) assert torch_all_close(output_slice.flatten() , UpperCAmelCase , atol=5E-3 ) @unittest.skipIf(torch_device == "mps" , "Training is not supported in mps" ) def __A ( self : Union[str, Any] ): A_ , A_ = self.prepare_init_args_and_inputs_for_common() A_ = self.block_class(**UpperCAmelCase ) model.to(UpperCAmelCase ) model.train() A_ = model(**UpperCAmelCase ) if isinstance(UpperCAmelCase , UpperCAmelCase ): A_ = output[0] A_ = torch.device(UpperCAmelCase ) A_ = randn_tensor(output.shape , device=UpperCAmelCase ) A_ = torch.nn.functional.mse_loss(UpperCAmelCase , UpperCAmelCase ) loss.backward()
312
1
import random import unittest import numpy as np import transformers from transformers import is_flax_available, is_torch_available from transformers.testing_utils import is_pt_flax_cross_test, require_flax if is_flax_available(): import os import jax.numpy as jnp from jax import jit from transformers import AutoTokenizer, FlaxAutoModelForCausalLM from transformers.modeling_flax_pytorch_utils import load_flax_weights_in_pytorch_model __a :str = '0.12' # assumed parallelism: 8 if is_torch_available(): import torch def __snake_case ( __UpperCamelCase : int ,__UpperCamelCase : Optional[int] ,__UpperCamelCase : List[str]=None ): """simple docstring""" if rng is None: A_ = random.Random() A_ = 1 for dim in shape: total_dims *= dim A_ = [] for _ in range(__UpperCamelCase ): values.append(rng.randint(0 ,vocab_size - 1 ) ) A_ = np.array(__UpperCamelCase ,dtype=jnp.intaa ).reshape(__UpperCamelCase ) return output def __snake_case ( __UpperCamelCase : Tuple ,__UpperCamelCase : Union[str, Any]=None ): """simple docstring""" A_ = ids_tensor(__UpperCamelCase ,vocab_size=2 ,rng=__UpperCamelCase ) # make sure that at least one token is attended to for each batch A_ = 1 return attn_mask @require_flax class _a : """simple docstring""" _lowerCamelCase : List[str] = None _lowerCamelCase : List[Any] = () def __A ( self : List[str] ): A_ , A_ = self.model_tester.prepare_config_and_inputs_for_common() # cut to half length & take max batch_size 3 A_ = 2 A_ = inputs["input_ids"].shape[-1] // 2 A_ = inputs["input_ids"][:max_batch_size, :sequence_length] A_ = jnp.ones_like(UpperCAmelCase ) A_ = attention_mask[:max_batch_size, :sequence_length] # generate max 5 tokens A_ = input_ids.shape[-1] + 5 if config.eos_token_id is not None and config.pad_token_id is None: # hack to allow generate for models such as GPT2 as is done in `generate()` A_ = config.eos_token_id return config, input_ids, attention_mask, max_length @is_pt_flax_cross_test def __A ( self : Dict ): A_ , A_ , A_ , A_ = self._get_input_ids_and_config() A_ = False A_ = max_length A_ = 0 for model_class in self.all_generative_model_classes: A_ = model_class(UpperCAmelCase ) A_ = model_class.__name__[4:] # Skip the "Flax" at the beginning A_ = getattr(UpperCAmelCase , UpperCAmelCase ) A_ = pt_model_class(UpperCAmelCase ).eval() A_ = load_flax_weights_in_pytorch_model(UpperCAmelCase , flax_model.params ) A_ = flax_model.generate(UpperCAmelCase ).sequences A_ = pt_model.generate(torch.tensor(UpperCAmelCase , dtype=torch.long ) ) if flax_generation_outputs.shape[-1] > pt_generation_outputs.shape[-1]: A_ = flax_generation_outputs[:, : pt_generation_outputs.shape[-1]] self.assertListEqual(pt_generation_outputs.numpy().tolist() , flax_generation_outputs.tolist() ) def __A ( self : int ): A_ , A_ , A_ , A_ = self._get_input_ids_and_config() A_ = False A_ = max_length for model_class in self.all_generative_model_classes: A_ = model_class(UpperCAmelCase ) A_ = model.generate(UpperCAmelCase ).sequences self.assertEqual(generation_outputs.shape[-1] , UpperCAmelCase ) A_ = jit(model.generate ) A_ = jit_generate(UpperCAmelCase ).sequences self.assertListEqual(generation_outputs.tolist() , jit_generation_outputs.tolist() ) def __A ( self : List[str] ): A_ , A_ , A_ , A_ = self._get_input_ids_and_config() A_ = True A_ = max_length for model_class in self.all_generative_model_classes: A_ = model_class(UpperCAmelCase ) A_ = model.generate(UpperCAmelCase ).sequences self.assertEqual(generation_outputs.shape[-1] , UpperCAmelCase ) A_ = jit(model.generate ) A_ = jit_generate(UpperCAmelCase ).sequences self.assertListEqual(generation_outputs.tolist() , jit_generation_outputs.tolist() ) def __A ( self : Optional[Any] ): A_ , A_ , A_ , A_ = self._get_input_ids_and_config() A_ = False A_ = max_length A_ = 2 for model_class in self.all_generative_model_classes: A_ = model_class(UpperCAmelCase ) A_ = model.generate(UpperCAmelCase ).sequences self.assertEqual(generation_outputs.shape[-1] , UpperCAmelCase ) A_ = jit(model.generate ) A_ = jit_generate(UpperCAmelCase ).sequences self.assertListEqual(generation_outputs.tolist() , jit_generation_outputs.tolist() ) def __A ( self : Tuple ): A_ , A_ , A_ , A_ = self._get_input_ids_and_config() A_ = False A_ = max_length A_ = 2 A_ = 2 for model_class in self.all_generative_model_classes: A_ = model_class(UpperCAmelCase ) A_ = model.generate(UpperCAmelCase ).sequences self.assertEqual(generation_outputs.shape[0] , input_ids.shape[0] * config.num_return_sequences ) def __A ( self : str ): A_ , A_ , A_ , A_ = self._get_input_ids_and_config() A_ = True A_ = max_length A_ = 0.8 A_ = 10 A_ = 0.3 A_ = 1 A_ = 8 A_ = 9 for model_class in self.all_generative_model_classes: A_ = model_class(UpperCAmelCase ) A_ = model.generate(UpperCAmelCase ).sequences self.assertEqual(generation_outputs.shape[-1] , UpperCAmelCase ) A_ = jit(model.generate ) A_ = jit_generate(UpperCAmelCase ).sequences self.assertListEqual(generation_outputs.tolist() , jit_generation_outputs.tolist() ) def __A ( self : int ): A_ , A_ , A_ , A_ = self._get_input_ids_and_config() A_ = max_length A_ = 1 A_ = 8 A_ = 9 for model_class in self.all_generative_model_classes: A_ = model_class(UpperCAmelCase ) A_ = model.generate(UpperCAmelCase ).sequences self.assertEqual(generation_outputs.shape[-1] , UpperCAmelCase ) A_ = jit(model.generate ) A_ = jit_generate(UpperCAmelCase ).sequences self.assertListEqual(generation_outputs.tolist() , jit_generation_outputs.tolist() ) def __A ( self : Optional[Any] ): A_ , A_ , A_ , A_ = self._get_input_ids_and_config() A_ = max_length A_ = 2 A_ = 1 A_ = 8 A_ = 9 for model_class in self.all_generative_model_classes: A_ = model_class(UpperCAmelCase ) A_ = model.generate(UpperCAmelCase ).sequences self.assertEqual(generation_outputs.shape[-1] , UpperCAmelCase ) A_ = jit(model.generate ) A_ = jit_generate(UpperCAmelCase ).sequences self.assertListEqual(generation_outputs.tolist() , jit_generation_outputs.tolist() ) def __A ( self : Union[str, Any] ): A_ , A_ , A_ , A_ = self._get_input_ids_and_config() # pad attention mask on the left A_ = attention_mask.at[(0, 0)].set(0 ) A_ = False A_ = max_length for model_class in self.all_generative_model_classes: A_ = model_class(UpperCAmelCase ) A_ = model.generate(UpperCAmelCase , attention_mask=UpperCAmelCase ).sequences self.assertEqual(generation_outputs.shape[-1] , UpperCAmelCase ) A_ = jit(model.generate ) A_ = jit_generate(UpperCAmelCase , attention_mask=UpperCAmelCase ).sequences self.assertListEqual(generation_outputs.tolist() , jit_generation_outputs.tolist() ) def __A ( self : Any ): A_ , A_ , A_ , A_ = self._get_input_ids_and_config() # pad attention mask on the left A_ = attention_mask.at[(0, 0)].set(0 ) A_ = True A_ = max_length for model_class in self.all_generative_model_classes: A_ = model_class(UpperCAmelCase ) A_ = model.generate(UpperCAmelCase , attention_mask=UpperCAmelCase ).sequences self.assertEqual(generation_outputs.shape[-1] , UpperCAmelCase ) A_ = jit(model.generate ) A_ = jit_generate(UpperCAmelCase , attention_mask=UpperCAmelCase ).sequences self.assertListEqual(generation_outputs.tolist() , jit_generation_outputs.tolist() ) def __A ( self : Dict ): A_ , A_ , A_ , A_ = self._get_input_ids_and_config() # pad attention mask on the left A_ = attention_mask.at[(0, 0)].set(0 ) A_ = 2 A_ = max_length for model_class in self.all_generative_model_classes: A_ = model_class(UpperCAmelCase ) A_ = model.generate(UpperCAmelCase , attention_mask=UpperCAmelCase ).sequences self.assertEqual(generation_outputs.shape[-1] , UpperCAmelCase ) A_ = jit(model.generate ) A_ = jit_generate(UpperCAmelCase , attention_mask=UpperCAmelCase ).sequences self.assertListEqual(generation_outputs.tolist() , jit_generation_outputs.tolist() ) @require_flax class _a ( unittest.TestCase ): """simple docstring""" def __A ( self : int ): A_ = AutoTokenizer.from_pretrained("hf-internal-testing/tiny-bert" ) A_ = FlaxAutoModelForCausalLM.from_pretrained("hf-internal-testing/tiny-bert-flax-only" ) A_ = "Hello world" A_ = tokenizer(UpperCAmelCase , return_tensors="np" ).input_ids # typos are quickly detected (the correct argument is `do_sample`) with self.assertRaisesRegex(UpperCAmelCase , "do_samples" ): model.generate(UpperCAmelCase , do_samples=UpperCAmelCase ) # arbitrary arguments that will not be used anywhere are also not accepted with self.assertRaisesRegex(UpperCAmelCase , "foo" ): A_ = {"foo": "bar"} model.generate(UpperCAmelCase , **UpperCAmelCase )
312
import copy import fnmatch import json import os import pickle as pkl import shutil import sys import tarfile import tempfile from collections import OrderedDict from contextlib import contextmanager from functools import partial from hashlib import shaaaa from io import BytesIO from pathlib import Path from urllib.parse import urlparse from zipfile import ZipFile, is_zipfile import cva import numpy as np import requests import wget from filelock import FileLock from PIL import Image from tqdm.auto import tqdm from yaml import Loader, dump, load try: import torch __a :int = True except ImportError: __a :Optional[Any] = False try: from torch.hub import _get_torch_home __a :Optional[Any] = _get_torch_home() except ImportError: __a :Tuple = os.path.expanduser( os.getenv('TORCH_HOME', os.path.join(os.getenv('XDG_CACHE_HOME', '~/.cache'), 'torch')) ) __a :Optional[Any] = os.path.join(torch_cache_home, 'transformers') __a :int = 'https://cdn.huggingface.co' __a :Any = 'https://s3.amazonaws.com/models.huggingface.co/bert' __a :Optional[Any] = '/'.join(str(Path(__file__).resolve()).split('/')[:-1]) __a :str = os.path.join(PATH, 'config.yaml') __a :str = os.path.join(PATH, 'attributes.txt') __a :Optional[Any] = os.path.join(PATH, 'objects.txt') __a :Optional[int] = os.getenv('PYTORCH_PRETRAINED_BERT_CACHE', default_cache_path) __a :Dict = os.getenv('PYTORCH_TRANSFORMERS_CACHE', PYTORCH_PRETRAINED_BERT_CACHE) __a :List[Any] = os.getenv('TRANSFORMERS_CACHE', PYTORCH_TRANSFORMERS_CACHE) __a :List[str] = 'pytorch_model.bin' __a :Tuple = 'config.yaml' def __snake_case ( __UpperCamelCase : Optional[Any]=OBJECTS ,__UpperCamelCase : List[str]=ATTRIBUTES ): """simple docstring""" A_ = [] with open(__UpperCamelCase ) as f: for object in f.readlines(): vg_classes.append(object.split("," )[0].lower().strip() ) A_ = [] with open(__UpperCamelCase ) as f: for object in f.readlines(): vg_attrs.append(object.split("," )[0].lower().strip() ) return vg_classes, vg_attrs def __snake_case ( __UpperCamelCase : List[Any] ): """simple docstring""" A_ = OrderedDict() with open(__UpperCamelCase ,"rb" ) as f: A_ = pkl.load(__UpperCamelCase )["model"] for k in copy.deepcopy(list(ckp.keys() ) ): A_ = ckp.pop(__UpperCamelCase ) if isinstance(__UpperCamelCase ,np.ndarray ): A_ = torch.tensor(__UpperCamelCase ) else: assert isinstance(__UpperCamelCase ,torch.tensor ), type(__UpperCamelCase ) A_ = v return r class _a : """simple docstring""" _lowerCamelCase : Union[str, Any] = {} def __init__( self : str , UpperCAmelCase : dict , UpperCAmelCase : str = "root" , UpperCAmelCase : List[str]=0 ): A_ = name A_ = level A_ = {} for k, v in dictionary.items(): if v is None: raise ValueError() A_ = copy.deepcopy(UpperCAmelCase ) A_ = copy.deepcopy(UpperCAmelCase ) if isinstance(UpperCAmelCase , UpperCAmelCase ): A_ = Config(UpperCAmelCase , name=UpperCAmelCase , level=level + 1 ) A_ = v setattr(self , UpperCAmelCase , UpperCAmelCase ) A_ = d def __repr__( self : Optional[Any] ): return str(list((self._pointer.keys()) ) ) def __setattr__( self : Any , UpperCAmelCase : Any , UpperCAmelCase : Any ): A_ = val A_ = val A_ = key.split("." ) A_ = len(UpperCAmelCase ) - 1 A_ = self._pointer if len(UpperCAmelCase ) > 1: for i, l in enumerate(UpperCAmelCase ): if hasattr(self , UpperCAmelCase ) and isinstance(getattr(self , UpperCAmelCase ) , UpperCAmelCase ): setattr(getattr(self , UpperCAmelCase ) , ".".join(levels[i:] ) , UpperCAmelCase ) if l == last_level: A_ = val else: A_ = pointer[l] def __A ( self : List[str] ): return self._pointer def __A ( self : int , UpperCAmelCase : Tuple , UpperCAmelCase : int ): with open(f'''{file_name}''' , "w" ) as stream: dump(UpperCAmelCase , UpperCAmelCase ) def __A ( self : List[Any] , UpperCAmelCase : str , UpperCAmelCase : Tuple ): with open(f'''{file_name}''' , "w" ) as stream: json.dump(UpperCAmelCase , UpperCAmelCase ) @staticmethod def __A ( UpperCAmelCase : Optional[int] ): with open(UpperCAmelCase ) as stream: A_ = load(UpperCAmelCase , Loader=UpperCAmelCase ) return data def __str__( self : str ): A_ = " " if self._name != "root": A_ = f'''{t * (self._level-1)}{self._name}:\n''' else: A_ = "" A_ = self._level for i, (k, v) in enumerate(self._pointer.items() ): if isinstance(UpperCAmelCase , UpperCAmelCase ): r += f'''{t * (self._level)}{v}\n''' self._level += 1 else: r += f'''{t * (self._level)}{k}: {v} ({type(UpperCAmelCase ).__name__})\n''' A_ = level return r[:-1] @classmethod def __A ( cls : Optional[Any] , UpperCAmelCase : str , **UpperCAmelCase : str ): A_ , A_ = cls.get_config_dict(UpperCAmelCase , **UpperCAmelCase ) return cls(UpperCAmelCase ) @classmethod def __A ( cls : int , UpperCAmelCase : str , **UpperCAmelCase : int ): A_ = kwargs.pop("cache_dir" , UpperCAmelCase ) A_ = kwargs.pop("force_download" , UpperCAmelCase ) A_ = kwargs.pop("resume_download" , UpperCAmelCase ) A_ = kwargs.pop("proxies" , UpperCAmelCase ) A_ = kwargs.pop("local_files_only" , UpperCAmelCase ) if os.path.isdir(UpperCAmelCase ): A_ = os.path.join(UpperCAmelCase , UpperCAmelCase ) elif os.path.isfile(UpperCAmelCase ) or is_remote_url(UpperCAmelCase ): A_ = pretrained_model_name_or_path else: A_ = hf_bucket_url(UpperCAmelCase , filename=UpperCAmelCase , use_cdn=UpperCAmelCase ) try: # Load from URL or cache if already cached A_ = cached_path( UpperCAmelCase , cache_dir=UpperCAmelCase , force_download=UpperCAmelCase , proxies=UpperCAmelCase , resume_download=UpperCAmelCase , local_files_only=UpperCAmelCase , ) # Load config dict if resolved_config_file is None: raise EnvironmentError A_ = Config.load_yaml(UpperCAmelCase ) except EnvironmentError: A_ = "Can't load config for" raise EnvironmentError(UpperCAmelCase ) if resolved_config_file == config_file: print("loading configuration file from path" ) else: print("loading configuration file cache" ) return Config.load_yaml(UpperCAmelCase ), kwargs def __snake_case ( __UpperCamelCase : Union[str, Any] ): """simple docstring""" A_ = torch.load("dump.pt" ,map_location=in_tensor.device ) A_ = in_tensor.numpy() A_ = out_tensor.numpy()[0] print(na.shape ,na[0, 0, :5] ) print(na.shape ,na[0, 0, :5] ) assert np.allclose(__UpperCamelCase ,__UpperCamelCase ,rtol=0.01 ,atol=0.1 ), ( f'''{sum([1 for x in np.isclose(__UpperCamelCase ,__UpperCamelCase ,rtol=0.01 ,atol=0.1 ).flatten() if x is False] )/len(na.flatten() )*100:.4f} %''' " element-wise mismatch" ) raise Exception("tensors are all good" ) # Hugging face functions below def __snake_case ( __UpperCamelCase : Optional[int] ): """simple docstring""" A_ = urlparse(__UpperCamelCase ) return parsed.scheme in ("http", "https") def __snake_case ( __UpperCamelCase : str ,__UpperCamelCase : str ,__UpperCamelCase : str=True ): """simple docstring""" A_ = CLOUDFRONT_DISTRIB_PREFIX if use_cdn else S3_BUCKET_PREFIX A_ = "/" not in model_id if legacy_format: return f'''{endpoint}/{model_id}-{filename}''' else: return f'''{endpoint}/{model_id}/{filename}''' def __snake_case ( __UpperCamelCase : List[str] ,__UpperCamelCase : Union[str, Any] ,__UpperCamelCase : List[str]=None ,__UpperCamelCase : int=0 ,__UpperCamelCase : int=None ,): """simple docstring""" A_ = "python/{}".format(sys.version.split()[0] ) if _torch_available: ua += "; torch/{}".format(torch.__version__ ) if isinstance(__UpperCamelCase ,__UpperCamelCase ): ua += "; " + "; ".join("{}/{}".format(__UpperCamelCase ,__UpperCamelCase ) for k, v in user_agent.items() ) elif isinstance(__UpperCamelCase ,__UpperCamelCase ): ua += "; " + user_agent A_ = {"user-agent": ua} if resume_size > 0: A_ = "bytes=%d-" % (resume_size,) A_ = requests.get(__UpperCamelCase ,stream=__UpperCamelCase ,proxies=__UpperCamelCase ,headers=__UpperCamelCase ) if response.status_code == 416: # Range not satisfiable return A_ = response.headers.get("Content-Length" ) A_ = resume_size + int(__UpperCamelCase ) if content_length is not None else None A_ = tqdm( unit="B" ,unit_scale=__UpperCamelCase ,total=__UpperCamelCase ,initial=__UpperCamelCase ,desc="Downloading" ,) for chunk in response.iter_content(chunk_size=1024 ): if chunk: # filter out keep-alive new chunks progress.update(len(__UpperCamelCase ) ) temp_file.write(__UpperCamelCase ) progress.close() def __snake_case ( __UpperCamelCase : str ,__UpperCamelCase : Any=None ,__UpperCamelCase : Dict=False ,__UpperCamelCase : Union[str, Any]=None ,__UpperCamelCase : Any=10 ,__UpperCamelCase : int=False ,__UpperCamelCase : Optional[Any]=None ,__UpperCamelCase : str=False ,): """simple docstring""" if cache_dir is None: A_ = TRANSFORMERS_CACHE if isinstance(__UpperCamelCase ,__UpperCamelCase ): A_ = str(__UpperCamelCase ) os.makedirs(__UpperCamelCase ,exist_ok=__UpperCamelCase ) A_ = None if not local_files_only: try: A_ = requests.head(__UpperCamelCase ,allow_redirects=__UpperCamelCase ,proxies=__UpperCamelCase ,timeout=__UpperCamelCase ) if response.status_code == 200: A_ = response.headers.get("ETag" ) except (EnvironmentError, requests.exceptions.Timeout): # etag is already None pass A_ = url_to_filename(__UpperCamelCase ,__UpperCamelCase ) # get cache path to put the file A_ = os.path.join(__UpperCamelCase ,__UpperCamelCase ) # etag is None = we don't have a connection, or url doesn't exist, or is otherwise inaccessible. # try to get the last downloaded one if etag is None: if os.path.exists(__UpperCamelCase ): return cache_path else: A_ = [ file for file in fnmatch.filter(os.listdir(__UpperCamelCase ) ,filename + ".*" ) if not file.endswith(".json" ) and not file.endswith(".lock" ) ] if len(__UpperCamelCase ) > 0: return os.path.join(__UpperCamelCase ,matching_files[-1] ) else: # If files cannot be found and local_files_only=True, # the models might've been found if local_files_only=False # Notify the user about that if local_files_only: raise ValueError( "Cannot find the requested files in the cached path and outgoing traffic has been" " disabled. To enable model look-ups and downloads online, set 'local_files_only'" " to False." ) return None # From now on, etag is not None. if os.path.exists(__UpperCamelCase ) and not force_download: return cache_path # Prevent parallel downloads of the same file with a lock. A_ = cache_path + ".lock" with FileLock(__UpperCamelCase ): # If the download just completed while the lock was activated. if os.path.exists(__UpperCamelCase ) and not force_download: # Even if returning early like here, the lock will be released. return cache_path if resume_download: A_ = cache_path + ".incomplete" @contextmanager def _resumable_file_manager(): with open(__UpperCamelCase ,"a+b" ) as f: yield f A_ = _resumable_file_manager if os.path.exists(__UpperCamelCase ): A_ = os.stat(__UpperCamelCase ).st_size else: A_ = 0 else: A_ = partial(tempfile.NamedTemporaryFile ,dir=__UpperCamelCase ,delete=__UpperCamelCase ) A_ = 0 # Download to temporary file, then copy to cache dir once finished. # Otherwise you get corrupt cache entries if the download gets interrupted. with temp_file_manager() as temp_file: print( "%s not found in cache or force_download set to True, downloading to %s" ,__UpperCamelCase ,temp_file.name ,) http_get( __UpperCamelCase ,__UpperCamelCase ,proxies=__UpperCamelCase ,resume_size=__UpperCamelCase ,user_agent=__UpperCamelCase ,) os.replace(temp_file.name ,__UpperCamelCase ) A_ = {"url": url, "etag": etag} A_ = cache_path + ".json" with open(__UpperCamelCase ,"w" ) as meta_file: json.dump(__UpperCamelCase ,__UpperCamelCase ) return cache_path def __snake_case ( __UpperCamelCase : List[Any] ,__UpperCamelCase : str=None ): """simple docstring""" A_ = url.encode("utf-8" ) A_ = shaaaa(__UpperCamelCase ) A_ = url_hash.hexdigest() if etag: A_ = etag.encode("utf-8" ) A_ = shaaaa(__UpperCamelCase ) filename += "." + etag_hash.hexdigest() if url.endswith(".h5" ): filename += ".h5" return filename def __snake_case ( __UpperCamelCase : Union[str, Any] ,__UpperCamelCase : Union[str, Any]=None ,__UpperCamelCase : List[Any]=False ,__UpperCamelCase : List[str]=None ,__UpperCamelCase : Any=False ,__UpperCamelCase : Optional[int]=None ,__UpperCamelCase : Optional[Any]=False ,__UpperCamelCase : Dict=False ,__UpperCamelCase : Optional[Any]=False ,): """simple docstring""" if cache_dir is None: A_ = TRANSFORMERS_CACHE if isinstance(__UpperCamelCase ,__UpperCamelCase ): A_ = str(__UpperCamelCase ) if isinstance(__UpperCamelCase ,__UpperCamelCase ): A_ = str(__UpperCamelCase ) if is_remote_url(__UpperCamelCase ): # URL, so get it from the cache (downloading if necessary) A_ = get_from_cache( __UpperCamelCase ,cache_dir=__UpperCamelCase ,force_download=__UpperCamelCase ,proxies=__UpperCamelCase ,resume_download=__UpperCamelCase ,user_agent=__UpperCamelCase ,local_files_only=__UpperCamelCase ,) elif os.path.exists(__UpperCamelCase ): # File, and it exists. A_ = url_or_filename elif urlparse(__UpperCamelCase ).scheme == "": # File, but it doesn't exist. raise EnvironmentError("file {} not found".format(__UpperCamelCase ) ) else: # Something unknown raise ValueError("unable to parse {} as a URL or as a local path".format(__UpperCamelCase ) ) if extract_compressed_file: if not is_zipfile(__UpperCamelCase ) and not tarfile.is_tarfile(__UpperCamelCase ): return output_path # Path where we extract compressed archives # We avoid '.' in dir name and add "-extracted" at the end: "./model.zip" => "./model-zip-extracted/" A_ , A_ = os.path.split(__UpperCamelCase ) A_ = output_file.replace("." ,"-" ) + "-extracted" A_ = os.path.join(__UpperCamelCase ,__UpperCamelCase ) if os.path.isdir(__UpperCamelCase ) and os.listdir(__UpperCamelCase ) and not force_extract: return output_path_extracted # Prevent parallel extractions A_ = output_path + ".lock" with FileLock(__UpperCamelCase ): shutil.rmtree(__UpperCamelCase ,ignore_errors=__UpperCamelCase ) os.makedirs(__UpperCamelCase ) if is_zipfile(__UpperCamelCase ): with ZipFile(__UpperCamelCase ,"r" ) as zip_file: zip_file.extractall(__UpperCamelCase ) zip_file.close() elif tarfile.is_tarfile(__UpperCamelCase ): A_ = tarfile.open(__UpperCamelCase ) tar_file.extractall(__UpperCamelCase ) tar_file.close() else: raise EnvironmentError("Archive format of {} could not be identified".format(__UpperCamelCase ) ) return output_path_extracted return output_path def __snake_case ( __UpperCamelCase : str ,__UpperCamelCase : Any="," ): """simple docstring""" assert isinstance(__UpperCamelCase ,__UpperCamelCase ) if os.path.isfile(__UpperCamelCase ): with open(__UpperCamelCase ) as f: A_ = eval(f.read() ) else: A_ = requests.get(__UpperCamelCase ) try: A_ = requests.json() except Exception: A_ = req.content.decode() assert data is not None, "could not connect" try: A_ = eval(__UpperCamelCase ) except Exception: A_ = data.split("\n" ) req.close() return data def __snake_case ( __UpperCamelCase : int ): """simple docstring""" A_ = requests.get(__UpperCamelCase ) A_ = np.array(Image.open(BytesIO(response.content ) ) ) return img def __snake_case ( __UpperCamelCase : Tuple ): """simple docstring""" A_ = url.split("/" )[-1] if fn not in os.listdir(os.getcwd() ): wget.download(__UpperCamelCase ) with open(__UpperCamelCase ,"rb" ) as stream: A_ = pkl.load(__UpperCamelCase ) A_ = weights.pop("model" ) A_ = {} for k, v in model.items(): A_ = torch.from_numpy(__UpperCamelCase ) if "running_var" in k: A_ = torch.tensor([0] ) A_ = k.replace("running_var" ,"num_batches_tracked" ) A_ = zero return new def __snake_case ( ): """simple docstring""" print(f'''{os.path.abspath(os.path.join(__UpperCamelCase ,os.pardir ) )}/demo.ipynb''' ) def __snake_case ( __UpperCamelCase : Optional[Any] ,__UpperCamelCase : Optional[int]="RGB" ): """simple docstring""" assert isinstance(__UpperCamelCase ,__UpperCamelCase ) if os.path.isfile(__UpperCamelCase ): A_ = cva.imread(__UpperCamelCase ) else: A_ = get_image_from_url(__UpperCamelCase ) assert img is not None, f'''could not connect to: {im}''' A_ = cva.cvtColor(__UpperCamelCase ,cva.COLOR_BGR2RGB ) if input_format == "RGB": A_ = img[:, :, ::-1] return img def __snake_case ( __UpperCamelCase : List[str] ,__UpperCamelCase : List[str]=1 ): """simple docstring""" return (images[i : i + batch] for i in range(0 ,len(__UpperCamelCase ) ,__UpperCamelCase ))
312
1
import math def __snake_case ( __UpperCamelCase : int = 100 ): """simple docstring""" A_ = sum(i * i for i in range(1 ,n + 1 ) ) A_ = int(math.pow(sum(range(1 ,n + 1 ) ) ,2 ) ) return square_of_sum - sum_of_squares if __name__ == "__main__": print(F"{solution() = }")
312
from __future__ import annotations def __snake_case ( __UpperCamelCase : list[list[int]] ): """simple docstring""" for i in range(1 ,len(matrix[0] ) ): matrix[0][i] += matrix[0][i - 1] # preprocessing the first column for i in range(1 ,len(__UpperCamelCase ) ): matrix[i][0] += matrix[i - 1][0] # updating the path cost for current position for i in range(1 ,len(__UpperCamelCase ) ): for j in range(1 ,len(matrix[0] ) ): matrix[i][j] += min(matrix[i - 1][j] ,matrix[i][j - 1] ) return matrix[-1][-1] if __name__ == "__main__": import doctest doctest.testmod()
312
1
# flake8: noqa # Lint as: python3 from typing import Dict, List, Optional, Type from .. import config from ..utils import logging from .formatting import ( ArrowFormatter, CustomFormatter, Formatter, PandasFormatter, PythonFormatter, TensorFormatter, format_table, query_table, ) from .np_formatter import NumpyFormatter __a :Optional[Any] = logging.get_logger(__name__) __a :Dict[Optional[str], Type[Formatter]] = {} __a :Dict[Optional[str], str] = {} __a :Dict[Optional[str], Exception] = {} def __snake_case ( __UpperCamelCase : type ,__UpperCamelCase : Optional[str] ,__UpperCamelCase : Optional[List[str]] = None ,): """simple docstring""" A_ = aliases if aliases is not None else [] if format_type in _FORMAT_TYPES: logger.warning( f'''Overwriting format type \'{format_type}\' ({_FORMAT_TYPES[format_type].__name__} -> {formatter_cls.__name__})''' ) A_ = formatter_cls for alias in set(aliases + [format_type] ): if alias in _FORMAT_TYPES_ALIASES: logger.warning( f'''Overwriting format type alias \'{alias}\' ({_FORMAT_TYPES_ALIASES[alias]} -> {format_type})''' ) A_ = format_type def __snake_case ( __UpperCamelCase : Exception ,__UpperCamelCase : Optional[str] ,__UpperCamelCase : Optional[List[str]] = None ): """simple docstring""" A_ = aliases if aliases is not None else [] for alias in set(aliases + [format_type] ): A_ = unavailable_error # Here we define all the available formatting functions that can be used by `Dataset.set_format` _register_formatter(PythonFormatter, None, aliases=['python']) _register_formatter(ArrowFormatter, 'arrow', aliases=['pa', 'pyarrow']) _register_formatter(NumpyFormatter, 'numpy', aliases=['np']) _register_formatter(PandasFormatter, 'pandas', aliases=['pd']) _register_formatter(CustomFormatter, 'custom') if config.TORCH_AVAILABLE: from .torch_formatter import TorchFormatter _register_formatter(TorchFormatter, 'torch', aliases=['pt', 'pytorch']) else: __a :List[Any] = ValueError('PyTorch needs to be installed to be able to return PyTorch tensors.') _register_unavailable_formatter(_torch_error, 'torch', aliases=['pt', 'pytorch']) if config.TF_AVAILABLE: from .tf_formatter import TFFormatter _register_formatter(TFFormatter, 'tensorflow', aliases=['tf']) else: __a :List[str] = ValueError('Tensorflow needs to be installed to be able to return Tensorflow tensors.') _register_unavailable_formatter(_tf_error, 'tensorflow', aliases=['tf']) if config.JAX_AVAILABLE: from .jax_formatter import JaxFormatter _register_formatter(JaxFormatter, 'jax', aliases=[]) else: __a :Tuple = ValueError('JAX needs to be installed to be able to return JAX arrays.') _register_unavailable_formatter(_jax_error, 'jax', aliases=[]) def __snake_case ( __UpperCamelCase : Optional[str] ): """simple docstring""" if format_type in _FORMAT_TYPES_ALIASES: return _FORMAT_TYPES_ALIASES[format_type] else: return format_type def __snake_case ( __UpperCamelCase : Optional[str] ,**__UpperCamelCase : List[Any] ): """simple docstring""" A_ = get_format_type_from_alias(__UpperCamelCase ) if format_type in _FORMAT_TYPES: return _FORMAT_TYPES[format_type](**__UpperCamelCase ) if format_type in _FORMAT_TYPES_ALIASES_UNAVAILABLE: raise _FORMAT_TYPES_ALIASES_UNAVAILABLE[format_type] else: raise ValueError( f'''Return type should be None or selected in {list(type for type in _FORMAT_TYPES.keys() if type != None )}, but got \'{format_type}\'''' )
312
from typing import Dict from transformers import EvalPrediction, HfArgumentParser, TrainingArguments, is_torch_available from transformers.testing_utils import ( TestCasePlus, execute_subprocess_async, get_torch_dist_unique_port, require_torch_multi_gpu, require_torch_neuroncore, ) from transformers.training_args import ParallelMode from transformers.utils import logging __a :int = logging.get_logger(__name__) if is_torch_available(): import torch from torch import nn from torch.utils.data import Dataset from transformers import Trainer class _a ( snake_case_ ): """simple docstring""" def __init__( self : Tuple , UpperCAmelCase : int = 101 ): A_ = length def __len__( self : int ): return self.length def __getitem__( self : Optional[int] , UpperCAmelCase : Optional[int] ): return i class _a : """simple docstring""" def __call__( self : Any , UpperCAmelCase : Optional[Any] ): return {"input_ids": torch.tensor(UpperCAmelCase ), "labels": torch.tensor(UpperCAmelCase )} class _a ( nn.Module ): """simple docstring""" def __init__( self : int ): super().__init__() # Add some (unused) params otherwise DDP will complain. A_ = nn.Linear(120 , 80 ) def __A ( self : Tuple , UpperCAmelCase : Dict , UpperCAmelCase : Tuple=None ): if labels is not None: return torch.tensor(0.0 , device=input_ids.device ), input_ids else: return input_ids class _a ( snake_case_ ): """simple docstring""" @require_torch_neuroncore def __A ( self : List[str] ): A_ = f'''--nproc_per_node=2 --master_port={get_torch_dist_unique_port()} {self.test_file_dir}/test_trainer_distributed.py '''.split() A_ = self.get_auto_remove_tmp_dir() A_ = f'''--output_dir {output_dir}'''.split() A_ = ["torchrun"] + distributed_args + args execute_subprocess_async(UpperCAmelCase , env=self.get_env() ) # successful return here == success - any errors would have caused an error in the sub-call class _a ( snake_case_ ): """simple docstring""" @require_torch_multi_gpu def __A ( self : List[str] ): A_ = f'''--nproc_per_node={torch.cuda.device_count()} --master_port={get_torch_dist_unique_port()} {self.test_file_dir}/test_trainer_distributed.py '''.split() A_ = self.get_auto_remove_tmp_dir() A_ = f'''--output_dir {output_dir}'''.split() A_ = ["torchrun"] + distributed_args + args execute_subprocess_async(UpperCAmelCase , env=self.get_env() ) # successful return here == success - any errors would have caused an error in the sub-call if __name__ == "__main__": # The script below is meant to be run under torch.distributed, on a machine with multiple GPUs: # # PYTHONPATH="src" python -m torch.distributed.run --nproc_per_node 2 --output_dir output_dir ./tests/test_trainer_distributed.py __a :Union[str, Any] = HfArgumentParser((TrainingArguments,)) __a :Tuple = parser.parse_args_into_dataclasses()[0] logger.warning( F"Process rank: {training_args.local_rank}, device: {training_args.device}, n_gpu: {training_args.n_gpu}, " F"distributed training: {training_args.parallel_mode != ParallelMode.NOT_DISTRIBUTED}" ) # Essentially, what we want to verify in the distributed case is that we get all samples back, # in the right order. (this is crucial for prediction for instance) for dataset_length in [101, 40, 7]: __a :int = DummyDataset(dataset_length) def __snake_case ( __UpperCamelCase : EvalPrediction ): """simple docstring""" A_ = list(range(len(__UpperCamelCase ) ) ) A_ = p.predictions.tolist() == sequential and p.label_ids.tolist() == sequential if not success and training_args.local_rank == 0: logger.warning( "Predictions and/or labels do not match expected results:\n - predictions: " f'''{p.predictions.tolist()}\n - labels: {p.label_ids.tolist()}\n - expected: {sequential}''' ) return {"success": success} __a :str = Trainer( model=DummyModel(), args=training_args, data_collator=DummyDataCollator(), eval_dataset=dataset, compute_metrics=compute_metrics, ) __a :str = trainer.evaluate() logger.info(metrics) if metrics["eval_success"] is not True: logger.error(metrics) exit(1) __a :str = trainer.predict(dataset) logger.info(p.metrics) if p.metrics["test_success"] is not True: logger.error(p.metrics) exit(1) __a :Optional[int] = 2 __a :List[Any] = trainer.evaluate() logger.info(metrics) if metrics["eval_success"] is not True: logger.error(metrics) exit(1) __a :str = trainer.predict(dataset) logger.info(p.metrics) if p.metrics["test_success"] is not True: logger.error(p.metrics) exit(1) __a :Union[str, Any] = None
312
1
import functools def __snake_case ( __UpperCamelCase : list[int] ,__UpperCamelCase : list[int] ): """simple docstring""" if not isinstance(__UpperCamelCase ,__UpperCamelCase ) or not all(isinstance(__UpperCamelCase ,__UpperCamelCase ) for day in days ): raise ValueError("The parameter days should be a list of integers" ) if len(__UpperCamelCase ) != 3 or not all(isinstance(__UpperCamelCase ,__UpperCamelCase ) for cost in costs ): raise ValueError("The parameter costs should be a list of three integers" ) if len(__UpperCamelCase ) == 0: return 0 if min(__UpperCamelCase ) <= 0: raise ValueError("All days elements should be greater than 0" ) if max(__UpperCamelCase ) >= 366: raise ValueError("All days elements should be less than 366" ) A_ = set(__UpperCamelCase ) @functools.cache def dynamic_programming(__UpperCamelCase : int ) -> int: if index > 365: return 0 if index not in days_set: return dynamic_programming(index + 1 ) return min( costs[0] + dynamic_programming(index + 1 ) ,costs[1] + dynamic_programming(index + 7 ) ,costs[2] + dynamic_programming(index + 30 ) ,) return dynamic_programming(1 ) if __name__ == "__main__": import doctest doctest.testmod()
312
import argparse import json from pathlib import Path import requests import torch from huggingface_hub import hf_hub_download from PIL import Image from timm import create_model from timm.data import resolve_data_config from timm.data.transforms_factory import create_transform from transformers import BitConfig, BitForImageClassification, BitImageProcessor from transformers.image_utils import PILImageResampling from transformers.utils import logging logging.set_verbosity_info() __a :Any = logging.get_logger(__name__) def __snake_case ( __UpperCamelCase : Optional[int] ): """simple docstring""" A_ = "huggingface/label-files" A_ = "imagenet-1k-id2label.json" A_ = json.load(open(hf_hub_download(__UpperCamelCase ,__UpperCamelCase ,repo_type="dataset" ) ,"r" ) ) A_ = {int(__UpperCamelCase ): v for k, v in idalabel.items()} A_ = {v: k for k, v in idalabel.items()} A_ = "std_conv" if "bit" in model_name else False # note that when using BiT as backbone for ViT-hybrid checkpoints, # one needs to additionally set config.layer_type = "bottleneck", config.stem_type = "same", # config.conv_layer = "std_conv_same" A_ = BitConfig( conv_layer=__UpperCamelCase ,num_labels=1000 ,idalabel=__UpperCamelCase ,labelaid=__UpperCamelCase ,) return config def __snake_case ( __UpperCamelCase : Union[str, Any] ): """simple docstring""" if "stem.conv" in name: A_ = name.replace("stem.conv" ,"bit.embedder.convolution" ) if "blocks" in name: A_ = name.replace("blocks" ,"layers" ) if "head.fc" in name: A_ = name.replace("head.fc" ,"classifier.1" ) if name.startswith("norm" ): A_ = "bit." + name if "bit" not in name and "classifier" not in name: A_ = "bit.encoder." + name return name def __snake_case ( ): """simple docstring""" A_ = "http://images.cocodataset.org/val2017/000000039769.jpg" A_ = Image.open(requests.get(__UpperCamelCase ,stream=__UpperCamelCase ).raw ) return im @torch.no_grad() def __snake_case ( __UpperCamelCase : Any ,__UpperCamelCase : Optional[Any] ,__UpperCamelCase : Tuple=False ): """simple docstring""" A_ = get_config(__UpperCamelCase ) # load original model from timm A_ = create_model(__UpperCamelCase ,pretrained=__UpperCamelCase ) timm_model.eval() # load state_dict of original model A_ = timm_model.state_dict() for key in state_dict.copy().keys(): A_ = state_dict.pop(__UpperCamelCase ) A_ = val.squeeze() if "head" in key else val # load HuggingFace model A_ = BitForImageClassification(__UpperCamelCase ) model.eval() model.load_state_dict(__UpperCamelCase ) # create image processor A_ = create_transform(**resolve_data_config({} ,model=__UpperCamelCase ) ) A_ = transform.transforms A_ = { "bilinear": PILImageResampling.BILINEAR, "bicubic": PILImageResampling.BICUBIC, "nearest": PILImageResampling.NEAREST, } A_ = BitImageProcessor( do_resize=__UpperCamelCase ,size={"shortest_edge": timm_transforms[0].size} ,resample=pillow_resamplings[timm_transforms[0].interpolation.value] ,do_center_crop=__UpperCamelCase ,crop_size={"height": timm_transforms[1].size[0], "width": timm_transforms[1].size[1]} ,do_normalize=__UpperCamelCase ,image_mean=timm_transforms[-1].mean.tolist() ,image_std=timm_transforms[-1].std.tolist() ,) A_ = prepare_img() A_ = transform(__UpperCamelCase ).unsqueeze(0 ) A_ = processor(__UpperCamelCase ,return_tensors="pt" ).pixel_values # verify pixel values assert torch.allclose(__UpperCamelCase ,__UpperCamelCase ) # verify logits with torch.no_grad(): A_ = model(__UpperCamelCase ) A_ = outputs.logits print("Logits:" ,logits[0, :3] ) print("Predicted class:" ,model.config.idalabel[logits.argmax(-1 ).item()] ) A_ = timm_model(__UpperCamelCase ) assert timm_logits.shape == outputs.logits.shape assert torch.allclose(__UpperCamelCase ,outputs.logits ,atol=1E-3 ) print("Looks ok!" ) if pytorch_dump_folder_path is not None: Path(__UpperCamelCase ).mkdir(exist_ok=__UpperCamelCase ) print(f'''Saving model {model_name} and processor to {pytorch_dump_folder_path}''' ) model.save_pretrained(__UpperCamelCase ) processor.save_pretrained(__UpperCamelCase ) if push_to_hub: print(f'''Pushing model {model_name} and processor to the hub''' ) model.push_to_hub(f'''ybelkada/{model_name}''' ) processor.push_to_hub(f'''ybelkada/{model_name}''' ) if __name__ == "__main__": __a :List[Any] = argparse.ArgumentParser() # Required parameters parser.add_argument( '--model_name', default='resnetv2_50x1_bitm', type=str, help='Name of the BiT timm model you\'d like to convert.', ) parser.add_argument( '--pytorch_dump_folder_path', default=None, type=str, help='Path to the output PyTorch model directory.' ) parser.add_argument( '--push_to_hub', action='store_true', help='Whether to push the model to the hub.', ) __a :str = parser.parse_args() convert_bit_checkpoint(args.model_name, args.pytorch_dump_folder_path, args.push_to_hub)
312
1
import os import numpy import onnx def __snake_case ( __UpperCamelCase : List[str] ,__UpperCamelCase : Optional[int] ): """simple docstring""" A_ = a.name A_ = b.name A_ = "" A_ = "" A_ = a == b A_ = name_a A_ = name_b return res def __snake_case ( __UpperCamelCase : Any ,__UpperCamelCase : List[Any] ,__UpperCamelCase : Tuple ): """simple docstring""" for i, input_name in enumerate(node_proto.input ): if input_name == name: node_proto.input.insert(__UpperCamelCase ,__UpperCamelCase ) node_proto.input.pop(i + 1 ) if node_proto.op_type == "If": _graph_replace_input_with(node_proto.attribute[0].g ,__UpperCamelCase ,__UpperCamelCase ) _graph_replace_input_with(node_proto.attribute[1].g ,__UpperCamelCase ,__UpperCamelCase ) if node_proto.op_type == "Loop": _graph_replace_input_with(node_proto.attribute[0].g ,__UpperCamelCase ,__UpperCamelCase ) def __snake_case ( __UpperCamelCase : int ,__UpperCamelCase : List[Any] ,__UpperCamelCase : int ): """simple docstring""" for n in graph_proto.node: _node_replace_input_with(__UpperCamelCase ,__UpperCamelCase ,__UpperCamelCase ) def __snake_case ( __UpperCamelCase : int ,__UpperCamelCase : Optional[int] ,__UpperCamelCase : Union[str, Any] ): """simple docstring""" A_ = list(model.graph.initializer ) A_ = list(model_without_ext.graph.initializer ) for i, ref_i in ind_to_replace: assert inits_with_data[i].name == inits[i].name assert inits_with_data[ref_i].name == inits[ref_i].name assert i > ref_i A_ = inits[i].name A_ = inits[ref_i].name model_without_ext.graph.initializer.remove(inits[i] ) # for n in model.graph.node: _graph_replace_input_with(model_without_ext.graph ,__UpperCamelCase ,__UpperCamelCase ) def __snake_case ( __UpperCamelCase : str ): """simple docstring""" A_ = os.path.dirname(__UpperCamelCase ) A_ = os.path.basename(__UpperCamelCase ) A_ = onnx.load(os.path.join(__UpperCamelCase ,__UpperCamelCase ) ) A_ = list(model.graph.initializer ) A_ = set() A_ = {} A_ = [] A_ = 0 for i in range(len(__UpperCamelCase ) ): if i in dup_set: continue for j in range(i + 1 ,len(__UpperCamelCase ) ): if j in dup_set: continue if _is_equal_tensor_proto(inits[i] ,inits[j] ): dup_set.add(__UpperCamelCase ) dup_set.add(__UpperCamelCase ) A_ = inits[j].data_type A_ = numpy.prod(inits[j].dims ) if dtype == 1: mem_size *= 4 elif dtype == 6: mem_size *= 4 elif dtype == 7 or dtype == 11: mem_size *= 8 else: print("unexpected data type: " ,__UpperCamelCase ) total_reduced_size += mem_size A_ = inits[i].name A_ = inits[j].name if name_i in dup_map: dup_map[name_i].append(__UpperCamelCase ) else: A_ = [name_j] ind_to_replace.append((j, i) ) print("total reduced size: " ,total_reduced_size / 1024 / 1024 / 1024 ,"GB" ) A_ = sorted(__UpperCamelCase ) _remove_dup_initializers_from_model(__UpperCamelCase ,__UpperCamelCase ,__UpperCamelCase ) A_ = "optimized_" + model_file_name A_ = os.path.join(__UpperCamelCase ,__UpperCamelCase ) onnx.save(__UpperCamelCase ,__UpperCamelCase ) return new_model
312
import os import re import sys import traceback import warnings from pathlib import Path from typing import Dict, Optional, Union from uuid import uuida from huggingface_hub import HfFolder, ModelCard, ModelCardData, hf_hub_download, whoami from huggingface_hub.file_download import REGEX_COMMIT_HASH from huggingface_hub.utils import ( EntryNotFoundError, RepositoryNotFoundError, RevisionNotFoundError, is_jinja_available, ) from packaging import version from requests import HTTPError from .. import __version__ from .constants import ( DEPRECATED_REVISION_ARGS, DIFFUSERS_CACHE, HUGGINGFACE_CO_RESOLVE_ENDPOINT, SAFETENSORS_WEIGHTS_NAME, WEIGHTS_NAME, ) from .import_utils import ( ENV_VARS_TRUE_VALUES, _flax_version, _jax_version, _onnxruntime_version, _torch_version, is_flax_available, is_onnx_available, is_torch_available, ) from .logging import get_logger __a :Dict = get_logger(__name__) __a :Union[str, Any] = Path(__file__).parent / 'model_card_template.md' __a :Tuple = uuida().hex __a :List[Any] = os.getenv('HF_HUB_OFFLINE', '').upper() in ENV_VARS_TRUE_VALUES __a :Union[str, Any] = os.getenv('DISABLE_TELEMETRY', '').upper() in ENV_VARS_TRUE_VALUES __a :Tuple = HUGGINGFACE_CO_RESOLVE_ENDPOINT + '/api/telemetry/' def __snake_case ( __UpperCamelCase : Union[Dict, str, None] = None ): """simple docstring""" A_ = f'''diffusers/{__version__}; python/{sys.version.split()[0]}; session_id/{SESSION_ID}''' if DISABLE_TELEMETRY or HF_HUB_OFFLINE: return ua + "; telemetry/off" if is_torch_available(): ua += f'''; torch/{_torch_version}''' if is_flax_available(): ua += f'''; jax/{_jax_version}''' ua += f'''; flax/{_flax_version}''' if is_onnx_available(): ua += f'''; onnxruntime/{_onnxruntime_version}''' # CI will set this value to True if os.environ.get("DIFFUSERS_IS_CI" ,"" ).upper() in ENV_VARS_TRUE_VALUES: ua += "; is_ci/true" if isinstance(__UpperCamelCase ,__UpperCamelCase ): ua += "; " + "; ".join(f'''{k}/{v}''' for k, v in user_agent.items() ) elif isinstance(__UpperCamelCase ,__UpperCamelCase ): ua += "; " + user_agent return ua def __snake_case ( __UpperCamelCase : str ,__UpperCamelCase : Optional[str] = None ,__UpperCamelCase : Optional[str] = None ): """simple docstring""" if token is None: A_ = HfFolder.get_token() if organization is None: A_ = whoami(__UpperCamelCase )["name"] return f'''{username}/{model_id}''' else: return f'''{organization}/{model_id}''' def __snake_case ( __UpperCamelCase : Tuple ,__UpperCamelCase : Union[str, Any] ): """simple docstring""" if not is_jinja_available(): raise ValueError( "Modelcard rendering is based on Jinja templates." " Please make sure to have `jinja` installed before using `create_model_card`." " To install it, please run `pip install Jinja2`." ) if hasattr(__UpperCamelCase ,"local_rank" ) and args.local_rank not in [-1, 0]: return A_ = args.hub_token if hasattr(__UpperCamelCase ,"hub_token" ) else None A_ = get_full_repo_name(__UpperCamelCase ,token=__UpperCamelCase ) A_ = ModelCard.from_template( card_data=ModelCardData( # Card metadata object that will be converted to YAML block language="en" ,license="apache-2.0" ,library_name="diffusers" ,tags=[] ,datasets=args.dataset_name ,metrics=[] ,) ,template_path=__UpperCamelCase ,model_name=__UpperCamelCase ,repo_name=__UpperCamelCase ,dataset_name=args.dataset_name if hasattr(__UpperCamelCase ,"dataset_name" ) else None ,learning_rate=args.learning_rate ,train_batch_size=args.train_batch_size ,eval_batch_size=args.eval_batch_size ,gradient_accumulation_steps=( args.gradient_accumulation_steps if hasattr(__UpperCamelCase ,"gradient_accumulation_steps" ) else None ) ,adam_betaa=args.adam_betaa if hasattr(__UpperCamelCase ,"adam_beta1" ) else None ,adam_betaa=args.adam_betaa if hasattr(__UpperCamelCase ,"adam_beta2" ) else None ,adam_weight_decay=args.adam_weight_decay if hasattr(__UpperCamelCase ,"adam_weight_decay" ) else None ,adam_epsilon=args.adam_epsilon if hasattr(__UpperCamelCase ,"adam_epsilon" ) else None ,lr_scheduler=args.lr_scheduler if hasattr(__UpperCamelCase ,"lr_scheduler" ) else None ,lr_warmup_steps=args.lr_warmup_steps if hasattr(__UpperCamelCase ,"lr_warmup_steps" ) else None ,ema_inv_gamma=args.ema_inv_gamma if hasattr(__UpperCamelCase ,"ema_inv_gamma" ) else None ,ema_power=args.ema_power if hasattr(__UpperCamelCase ,"ema_power" ) else None ,ema_max_decay=args.ema_max_decay if hasattr(__UpperCamelCase ,"ema_max_decay" ) else None ,mixed_precision=args.mixed_precision ,) A_ = os.path.join(args.output_dir ,"README.md" ) model_card.save(__UpperCamelCase ) def __snake_case ( __UpperCamelCase : Optional[str] ,__UpperCamelCase : Optional[str] = None ): """simple docstring""" if resolved_file is None or commit_hash is not None: return commit_hash A_ = str(Path(__UpperCamelCase ).as_posix() ) A_ = re.search(R"snapshots/([^/]+)/" ,__UpperCamelCase ) if search is None: return None A_ = search.groups()[0] return commit_hash if REGEX_COMMIT_HASH.match(__UpperCamelCase ) else None # Old default cache path, potentially to be migrated. # This logic was more or less taken from `transformers`, with the following differences: # - Diffusers doesn't use custom environment variables to specify the cache path. # - There is no need to migrate the cache format, just move the files to the new location. __a :str = os.path.expanduser( os.getenv('HF_HOME', os.path.join(os.getenv('XDG_CACHE_HOME', '~/.cache'), 'huggingface')) ) __a :List[Any] = os.path.join(hf_cache_home, 'diffusers') def __snake_case ( __UpperCamelCase : Optional[str] = None ,__UpperCamelCase : Optional[str] = None ): """simple docstring""" if new_cache_dir is None: A_ = DIFFUSERS_CACHE if old_cache_dir is None: A_ = old_diffusers_cache A_ = Path(__UpperCamelCase ).expanduser() A_ = Path(__UpperCamelCase ).expanduser() for old_blob_path in old_cache_dir.glob("**/blobs/*" ): if old_blob_path.is_file() and not old_blob_path.is_symlink(): A_ = new_cache_dir / old_blob_path.relative_to(__UpperCamelCase ) new_blob_path.parent.mkdir(parents=__UpperCamelCase ,exist_ok=__UpperCamelCase ) os.replace(__UpperCamelCase ,__UpperCamelCase ) try: os.symlink(__UpperCamelCase ,__UpperCamelCase ) except OSError: logger.warning( "Could not create symlink between old cache and new cache. If you use an older version of diffusers again, files will be re-downloaded." ) # At this point, old_cache_dir contains symlinks to the new cache (it can still be used). __a :Dict = os.path.join(DIFFUSERS_CACHE, 'version_diffusers_cache.txt') if not os.path.isfile(cache_version_file): __a :Optional[int] = 0 else: with open(cache_version_file) as f: try: __a :Dict = int(f.read()) except ValueError: __a :str = 0 if cache_version < 1: __a :Optional[Any] = os.path.isdir(old_diffusers_cache) and len(os.listdir(old_diffusers_cache)) > 0 if old_cache_is_not_empty: logger.warning( 'The cache for model files in Diffusers v0.14.0 has moved to a new location. Moving your ' 'existing cached models. This is a one-time operation, you can interrupt it or run it ' 'later by calling `diffusers.utils.hub_utils.move_cache()`.' ) try: move_cache() except Exception as e: __a :Optional[Any] = '\n'.join(traceback.format_tb(e.__traceback__)) logger.error( F"There was a problem when trying to move your cache:\n\n{trace}\n{e.__class__.__name__}: {e}\n\nPlease " 'file an issue at https://github.com/huggingface/diffusers/issues/new/choose, copy paste this whole ' 'message and we will do our best to help.' ) if cache_version < 1: try: os.makedirs(DIFFUSERS_CACHE, exist_ok=True) with open(cache_version_file, 'w') as f: f.write('1') except Exception: logger.warning( F"There was a problem when trying to write in your cache folder ({DIFFUSERS_CACHE}). Please, ensure " 'the directory exists and can be written to.' ) def __snake_case ( __UpperCamelCase : str ,__UpperCamelCase : Optional[str] = None ): """simple docstring""" if variant is not None: A_ = weights_name.split("." ) A_ = splits[:-1] + [variant] + splits[-1:] A_ = ".".join(__UpperCamelCase ) return weights_name def __snake_case ( __UpperCamelCase : Optional[Any] ,*, __UpperCamelCase : Union[str, Any] ,__UpperCamelCase : Any ,__UpperCamelCase : Tuple ,__UpperCamelCase : Union[str, Any] ,__UpperCamelCase : str ,__UpperCamelCase : int ,__UpperCamelCase : Union[str, Any] ,__UpperCamelCase : int ,__UpperCamelCase : Optional[int] ,__UpperCamelCase : Tuple ,__UpperCamelCase : Optional[int]=None ,): """simple docstring""" A_ = str(__UpperCamelCase ) if os.path.isfile(__UpperCamelCase ): return pretrained_model_name_or_path elif os.path.isdir(__UpperCamelCase ): if os.path.isfile(os.path.join(__UpperCamelCase ,__UpperCamelCase ) ): # Load from a PyTorch checkpoint A_ = os.path.join(__UpperCamelCase ,__UpperCamelCase ) return model_file elif subfolder is not None and os.path.isfile( os.path.join(__UpperCamelCase ,__UpperCamelCase ,__UpperCamelCase ) ): A_ = os.path.join(__UpperCamelCase ,__UpperCamelCase ,__UpperCamelCase ) return model_file else: raise EnvironmentError( f'''Error no file named {weights_name} found in directory {pretrained_model_name_or_path}.''' ) else: # 1. First check if deprecated way of loading from branches is used if ( revision in DEPRECATED_REVISION_ARGS and (weights_name == WEIGHTS_NAME or weights_name == SAFETENSORS_WEIGHTS_NAME) and version.parse(version.parse(__UpperCamelCase ).base_version ) >= version.parse("0.20.0" ) ): try: A_ = hf_hub_download( __UpperCamelCase ,filename=_add_variant(__UpperCamelCase ,__UpperCamelCase ) ,cache_dir=__UpperCamelCase ,force_download=__UpperCamelCase ,proxies=__UpperCamelCase ,resume_download=__UpperCamelCase ,local_files_only=__UpperCamelCase ,use_auth_token=__UpperCamelCase ,user_agent=__UpperCamelCase ,subfolder=__UpperCamelCase ,revision=revision or commit_hash ,) warnings.warn( f'''Loading the variant {revision} from {pretrained_model_name_or_path} via `revision=\'{revision}\'` is deprecated. Loading instead from `revision=\'main\'` with `variant={revision}`. Loading model variants via `revision=\'{revision}\'` will be removed in diffusers v1. Please use `variant=\'{revision}\'` instead.''' ,__UpperCamelCase ,) return model_file except: # noqa: E722 warnings.warn( f'''You are loading the variant {revision} from {pretrained_model_name_or_path} via `revision=\'{revision}\'`. This behavior is deprecated and will be removed in diffusers v1. One should use `variant=\'{revision}\'` instead. However, it appears that {pretrained_model_name_or_path} currently does not have a {_add_variant(__UpperCamelCase ,__UpperCamelCase )} file in the \'main\' branch of {pretrained_model_name_or_path}. \n The Diffusers team and community would be very grateful if you could open an issue: https://github.com/huggingface/diffusers/issues/new with the title \'{pretrained_model_name_or_path} is missing {_add_variant(__UpperCamelCase ,__UpperCamelCase )}\' so that the correct variant file can be added.''' ,__UpperCamelCase ,) try: # 2. Load model file as usual A_ = hf_hub_download( __UpperCamelCase ,filename=__UpperCamelCase ,cache_dir=__UpperCamelCase ,force_download=__UpperCamelCase ,proxies=__UpperCamelCase ,resume_download=__UpperCamelCase ,local_files_only=__UpperCamelCase ,use_auth_token=__UpperCamelCase ,user_agent=__UpperCamelCase ,subfolder=__UpperCamelCase ,revision=revision or commit_hash ,) return model_file except RepositoryNotFoundError: raise EnvironmentError( f'''{pretrained_model_name_or_path} is not a local folder and is not a valid model identifier ''' "listed on 'https://huggingface.co/models'\nIf this is a private repository, make sure to pass a " "token having permission to this repo with `use_auth_token` or log in with `huggingface-cli " "login`." ) except RevisionNotFoundError: raise EnvironmentError( f'''{revision} is not a valid git identifier (branch name, tag name or commit id) that exists for ''' "this model name. Check the model page at " f'''\'https://huggingface.co/{pretrained_model_name_or_path}\' for available revisions.''' ) except EntryNotFoundError: raise EnvironmentError( f'''{pretrained_model_name_or_path} does not appear to have a file named {weights_name}.''' ) except HTTPError as err: raise EnvironmentError( f'''There was a specific connection error when trying to load {pretrained_model_name_or_path}:\n{err}''' ) except ValueError: raise EnvironmentError( f'''We couldn\'t connect to \'{HUGGINGFACE_CO_RESOLVE_ENDPOINT}\' to load this model, couldn\'t find it''' f''' in the cached files and it looks like {pretrained_model_name_or_path} is not the path to a''' f''' directory containing a file named {weights_name} or''' " \nCheckout your internet connection or see how to run the library in" " offline mode at 'https://huggingface.co/docs/diffusers/installation#offline-mode'." ) except EnvironmentError: raise EnvironmentError( f'''Can\'t load the model for \'{pretrained_model_name_or_path}\'. If you were trying to load it from ''' "'https://huggingface.co/models', make sure you don't have a local directory with the same name. " f'''Otherwise, make sure \'{pretrained_model_name_or_path}\' is the correct path to a directory ''' f'''containing a file named {weights_name}''' )
312
1
# Copyright 2023 The HuggingFace Team. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available __a :int = { 'configuration_xmod': [ 'XMOD_PRETRAINED_CONFIG_ARCHIVE_MAP', 'XmodConfig', 'XmodOnnxConfig', ], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __a :Dict = [ 'XMOD_PRETRAINED_MODEL_ARCHIVE_LIST', 'XmodForCausalLM', 'XmodForMaskedLM', 'XmodForMultipleChoice', 'XmodForQuestionAnswering', 'XmodForSequenceClassification', 'XmodForTokenClassification', 'XmodModel', 'XmodPreTrainedModel', ] if TYPE_CHECKING: from .configuration_xmod import XMOD_PRETRAINED_CONFIG_ARCHIVE_MAP, XmodConfig, XmodOnnxConfig try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_xmod import ( XMOD_PRETRAINED_MODEL_ARCHIVE_LIST, XmodForCausalLM, XmodForMaskedLM, XmodForMultipleChoice, XmodForQuestionAnswering, XmodForSequenceClassification, XmodForTokenClassification, XmodModel, XmodPreTrainedModel, ) else: import sys __a :str = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
312
# Copyright 2023 The HuggingFace Team. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available __a :Any = { 'configuration_mgp_str': ['MGP_STR_PRETRAINED_CONFIG_ARCHIVE_MAP', 'MgpstrConfig'], 'processing_mgp_str': ['MgpstrProcessor'], 'tokenization_mgp_str': ['MgpstrTokenizer'], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __a :Optional[Any] = [ 'MGP_STR_PRETRAINED_MODEL_ARCHIVE_LIST', 'MgpstrModel', 'MgpstrPreTrainedModel', 'MgpstrForSceneTextRecognition', ] if TYPE_CHECKING: from .configuration_mgp_str import MGP_STR_PRETRAINED_CONFIG_ARCHIVE_MAP, MgpstrConfig from .processing_mgp_str import MgpstrProcessor from .tokenization_mgp_str import MgpstrTokenizer try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_mgp_str import ( MGP_STR_PRETRAINED_MODEL_ARCHIVE_LIST, MgpstrForSceneTextRecognition, MgpstrModel, MgpstrPreTrainedModel, ) else: import sys __a :List[Any] = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
312
1
from __future__ import annotations import numpy as np from numpy import floataa from numpy.typing import NDArray def __snake_case ( __UpperCamelCase : NDArray[floataa] ,__UpperCamelCase : NDArray[floataa] ,__UpperCamelCase : list[int] ,__UpperCamelCase : int ,): """simple docstring""" A_ , A_ = coefficient_matrix.shape A_ , A_ = constant_matrix.shape if rowsa != colsa: A_ = f'''Coefficient matrix dimensions must be nxn but received {rowsa}x{colsa}''' raise ValueError(__UpperCamelCase ) if colsa != 1: A_ = f'''Constant matrix must be nx1 but received {rowsa}x{colsa}''' raise ValueError(__UpperCamelCase ) if rowsa != rowsa: A_ = ( "Coefficient and constant matrices dimensions must be nxn and nx1 but " f'''received {rowsa}x{colsa} and {rowsa}x{colsa}''' ) raise ValueError(__UpperCamelCase ) if len(__UpperCamelCase ) != rowsa: A_ = ( "Number of initial values must be equal to number of rows in coefficient " f'''matrix but received {len(__UpperCamelCase )} and {rowsa}''' ) raise ValueError(__UpperCamelCase ) if iterations <= 0: raise ValueError("Iterations must be at least 1" ) A_ = np.concatenate( (coefficient_matrix, constant_matrix) ,axis=1 ) A_ , A_ = table.shape strictly_diagonally_dominant(__UpperCamelCase ) # Iterates the whole matrix for given number of times for _ in range(__UpperCamelCase ): A_ = [] for row in range(__UpperCamelCase ): A_ = 0 for col in range(__UpperCamelCase ): if col == row: A_ = table[row][col] elif col == cols - 1: A_ = table[row][col] else: temp += (-1) * table[row][col] * init_val[col] A_ = (temp + val) / denom new_val.append(__UpperCamelCase ) A_ = new_val return [float(__UpperCamelCase ) for i in new_val] def __snake_case ( __UpperCamelCase : NDArray[floataa] ): """simple docstring""" A_ , A_ = table.shape A_ = True for i in range(0 ,__UpperCamelCase ): A_ = 0 for j in range(0 ,cols - 1 ): if i == j: continue else: total += table[i][j] if table[i][i] <= total: raise ValueError("Coefficient matrix is not strictly diagonally dominant" ) return is_diagonally_dominant # Test Cases if __name__ == "__main__": import doctest doctest.testmod()
312
import functools from typing import Any def __snake_case ( __UpperCamelCase : str ,__UpperCamelCase : list[str] ): """simple docstring""" if not isinstance(__UpperCamelCase ,__UpperCamelCase ) or len(__UpperCamelCase ) == 0: raise ValueError("the string should be not empty string" ) if not isinstance(__UpperCamelCase ,__UpperCamelCase ) or not all( isinstance(__UpperCamelCase ,__UpperCamelCase ) and len(__UpperCamelCase ) > 0 for item in words ): raise ValueError("the words should be a list of non-empty strings" ) # Build trie A_ = {} A_ = "WORD_KEEPER" for word in words: A_ = trie for c in word: if c not in trie_node: A_ = {} A_ = trie_node[c] A_ = True A_ = len(__UpperCamelCase ) # Dynamic programming method @functools.cache def is_breakable(__UpperCamelCase : int ) -> bool: if index == len_string: return True A_ = trie for i in range(__UpperCamelCase ,__UpperCamelCase ): A_ = trie_node.get(string[i] ,__UpperCamelCase ) if trie_node is None: return False if trie_node.get(__UpperCamelCase ,__UpperCamelCase ) and is_breakable(i + 1 ): return True return False return is_breakable(0 ) if __name__ == "__main__": import doctest doctest.testmod()
312
1
from ...configuration_utils import PretrainedConfig from ...utils import logging __a :Optional[Any] = logging.get_logger(__name__) __a :Tuple = { 'abeja/gpt-neox-japanese-2.7b': 'https://huggingface.co/abeja/gpt-neox-japanese-2.7b/resolve/main/config.json', } class _a ( snake_case_ ): """simple docstring""" _lowerCamelCase : Optional[int] = 'gpt_neox_japanese' def __init__( self : Optional[Any] , UpperCAmelCase : Any=32000 , UpperCAmelCase : Union[str, Any]=2560 , UpperCAmelCase : Optional[Any]=32 , UpperCAmelCase : List[Any]=32 , UpperCAmelCase : Tuple=4 , UpperCAmelCase : Optional[Any]="gelu" , UpperCAmelCase : List[str]=1.00 , UpperCAmelCase : int=10000 , UpperCAmelCase : str=2048 , UpperCAmelCase : Optional[Any]=0.02 , UpperCAmelCase : Dict=1E-5 , UpperCAmelCase : Optional[Any]=True , UpperCAmelCase : Optional[Any]=31996 , UpperCAmelCase : List[Any]=31999 , UpperCAmelCase : str=0.1 , UpperCAmelCase : List[str]=0.0 , **UpperCAmelCase : Any , ): super().__init__(bos_token_id=UpperCAmelCase , eos_token_id=UpperCAmelCase , **UpperCAmelCase ) A_ = vocab_size A_ = max_position_embeddings A_ = hidden_size A_ = num_hidden_layers A_ = num_attention_heads A_ = intermediate_multiple_size A_ = hidden_act A_ = rotary_pct A_ = rotary_emb_base A_ = initializer_range A_ = layer_norm_eps A_ = use_cache A_ = attention_dropout A_ = hidden_dropout
312
import json from typing import List, Optional, Tuple from tokenizers import normalizers from ...tokenization_utils_fast import PreTrainedTokenizerFast from .tokenization_electra import ElectraTokenizer __a :List[str] = {'vocab_file': 'vocab.txt', 'tokenizer_file': 'tokenizer.json'} __a :Union[str, Any] = { 'vocab_file': { 'google/electra-small-generator': ( 'https://huggingface.co/google/electra-small-generator/resolve/main/vocab.txt' ), 'google/electra-base-generator': 'https://huggingface.co/google/electra-base-generator/resolve/main/vocab.txt', 'google/electra-large-generator': ( 'https://huggingface.co/google/electra-large-generator/resolve/main/vocab.txt' ), 'google/electra-small-discriminator': ( 'https://huggingface.co/google/electra-small-discriminator/resolve/main/vocab.txt' ), 'google/electra-base-discriminator': ( 'https://huggingface.co/google/electra-base-discriminator/resolve/main/vocab.txt' ), 'google/electra-large-discriminator': ( 'https://huggingface.co/google/electra-large-discriminator/resolve/main/vocab.txt' ), }, 'tokenizer_file': { 'google/electra-small-generator': ( 'https://huggingface.co/google/electra-small-generator/resolve/main/tokenizer.json' ), 'google/electra-base-generator': ( 'https://huggingface.co/google/electra-base-generator/resolve/main/tokenizer.json' ), 'google/electra-large-generator': ( 'https://huggingface.co/google/electra-large-generator/resolve/main/tokenizer.json' ), 'google/electra-small-discriminator': ( 'https://huggingface.co/google/electra-small-discriminator/resolve/main/tokenizer.json' ), 'google/electra-base-discriminator': ( 'https://huggingface.co/google/electra-base-discriminator/resolve/main/tokenizer.json' ), 'google/electra-large-discriminator': ( 'https://huggingface.co/google/electra-large-discriminator/resolve/main/tokenizer.json' ), }, } __a :Optional[int] = { 'google/electra-small-generator': 512, 'google/electra-base-generator': 512, 'google/electra-large-generator': 512, 'google/electra-small-discriminator': 512, 'google/electra-base-discriminator': 512, 'google/electra-large-discriminator': 512, } __a :str = { 'google/electra-small-generator': {'do_lower_case': True}, 'google/electra-base-generator': {'do_lower_case': True}, 'google/electra-large-generator': {'do_lower_case': True}, 'google/electra-small-discriminator': {'do_lower_case': True}, 'google/electra-base-discriminator': {'do_lower_case': True}, 'google/electra-large-discriminator': {'do_lower_case': True}, } class _a ( snake_case_ ): """simple docstring""" _lowerCamelCase : Tuple = VOCAB_FILES_NAMES _lowerCamelCase : Union[str, Any] = PRETRAINED_VOCAB_FILES_MAP _lowerCamelCase : int = PRETRAINED_INIT_CONFIGURATION _lowerCamelCase : List[Any] = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES _lowerCamelCase : int = ElectraTokenizer def __init__( self : Tuple , UpperCAmelCase : Dict=None , UpperCAmelCase : Optional[int]=None , UpperCAmelCase : Any=True , UpperCAmelCase : Any="[UNK]" , UpperCAmelCase : Union[str, Any]="[SEP]" , UpperCAmelCase : List[Any]="[PAD]" , UpperCAmelCase : Union[str, Any]="[CLS]" , UpperCAmelCase : List[Any]="[MASK]" , UpperCAmelCase : List[str]=True , UpperCAmelCase : Any=None , **UpperCAmelCase : Union[str, Any] , ): super().__init__( UpperCAmelCase , tokenizer_file=UpperCAmelCase , do_lower_case=UpperCAmelCase , unk_token=UpperCAmelCase , sep_token=UpperCAmelCase , pad_token=UpperCAmelCase , cls_token=UpperCAmelCase , mask_token=UpperCAmelCase , tokenize_chinese_chars=UpperCAmelCase , strip_accents=UpperCAmelCase , **UpperCAmelCase , ) A_ = json.loads(self.backend_tokenizer.normalizer.__getstate__() ) if ( normalizer_state.get("lowercase" , UpperCAmelCase ) != do_lower_case or normalizer_state.get("strip_accents" , UpperCAmelCase ) != strip_accents or normalizer_state.get("handle_chinese_chars" , UpperCAmelCase ) != tokenize_chinese_chars ): A_ = getattr(UpperCAmelCase , normalizer_state.pop("type" ) ) A_ = do_lower_case A_ = strip_accents A_ = tokenize_chinese_chars A_ = normalizer_class(**UpperCAmelCase ) A_ = do_lower_case def __A ( self : int , UpperCAmelCase : List[Any] , UpperCAmelCase : Union[str, Any]=None ): A_ = [self.cls_token_id] + token_ids_a + [self.sep_token_id] if token_ids_a: output += token_ids_a + [self.sep_token_id] return output def __A ( self : Union[str, Any] , UpperCAmelCase : List[int] , UpperCAmelCase : Optional[List[int]] = None ): A_ = [self.sep_token_id] A_ = [self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep ) * [0] return len(cls + token_ids_a + sep ) * [0] + len(token_ids_a + sep ) * [1] def __A ( self : Tuple , UpperCAmelCase : str , UpperCAmelCase : Optional[str] = None ): A_ = self._tokenizer.model.save(UpperCAmelCase , name=UpperCAmelCase ) return tuple(UpperCAmelCase )
312
1
import unittest from diffusers import FlaxAutoencoderKL from diffusers.utils import is_flax_available from diffusers.utils.testing_utils import require_flax from .test_modeling_common_flax import FlaxModelTesterMixin if is_flax_available(): import jax @require_flax class _a ( snake_case_ , unittest.TestCase ): """simple docstring""" _lowerCamelCase : Optional[Any] = FlaxAutoencoderKL @property def __A ( self : Dict ): A_ = 4 A_ = 3 A_ = (32, 32) A_ = jax.random.PRNGKey(0 ) A_ = jax.random.uniform(UpperCAmelCase , ((batch_size, num_channels) + sizes) ) return {"sample": image, "prng_key": prng_key} def __A ( self : Any ): A_ = { "block_out_channels": [32, 64], "in_channels": 3, "out_channels": 3, "down_block_types": ["DownEncoderBlock2D", "DownEncoderBlock2D"], "up_block_types": ["UpDecoderBlock2D", "UpDecoderBlock2D"], "latent_channels": 4, } A_ = self.dummy_input return init_dict, inputs_dict
312
# flake8: noqa # Lint as: python3 from typing import Dict, List, Optional, Type from .. import config from ..utils import logging from .formatting import ( ArrowFormatter, CustomFormatter, Formatter, PandasFormatter, PythonFormatter, TensorFormatter, format_table, query_table, ) from .np_formatter import NumpyFormatter __a :Optional[Any] = logging.get_logger(__name__) __a :Dict[Optional[str], Type[Formatter]] = {} __a :Dict[Optional[str], str] = {} __a :Dict[Optional[str], Exception] = {} def __snake_case ( __UpperCamelCase : type ,__UpperCamelCase : Optional[str] ,__UpperCamelCase : Optional[List[str]] = None ,): """simple docstring""" A_ = aliases if aliases is not None else [] if format_type in _FORMAT_TYPES: logger.warning( f'''Overwriting format type \'{format_type}\' ({_FORMAT_TYPES[format_type].__name__} -> {formatter_cls.__name__})''' ) A_ = formatter_cls for alias in set(aliases + [format_type] ): if alias in _FORMAT_TYPES_ALIASES: logger.warning( f'''Overwriting format type alias \'{alias}\' ({_FORMAT_TYPES_ALIASES[alias]} -> {format_type})''' ) A_ = format_type def __snake_case ( __UpperCamelCase : Exception ,__UpperCamelCase : Optional[str] ,__UpperCamelCase : Optional[List[str]] = None ): """simple docstring""" A_ = aliases if aliases is not None else [] for alias in set(aliases + [format_type] ): A_ = unavailable_error # Here we define all the available formatting functions that can be used by `Dataset.set_format` _register_formatter(PythonFormatter, None, aliases=['python']) _register_formatter(ArrowFormatter, 'arrow', aliases=['pa', 'pyarrow']) _register_formatter(NumpyFormatter, 'numpy', aliases=['np']) _register_formatter(PandasFormatter, 'pandas', aliases=['pd']) _register_formatter(CustomFormatter, 'custom') if config.TORCH_AVAILABLE: from .torch_formatter import TorchFormatter _register_formatter(TorchFormatter, 'torch', aliases=['pt', 'pytorch']) else: __a :List[Any] = ValueError('PyTorch needs to be installed to be able to return PyTorch tensors.') _register_unavailable_formatter(_torch_error, 'torch', aliases=['pt', 'pytorch']) if config.TF_AVAILABLE: from .tf_formatter import TFFormatter _register_formatter(TFFormatter, 'tensorflow', aliases=['tf']) else: __a :List[str] = ValueError('Tensorflow needs to be installed to be able to return Tensorflow tensors.') _register_unavailable_formatter(_tf_error, 'tensorflow', aliases=['tf']) if config.JAX_AVAILABLE: from .jax_formatter import JaxFormatter _register_formatter(JaxFormatter, 'jax', aliases=[]) else: __a :Tuple = ValueError('JAX needs to be installed to be able to return JAX arrays.') _register_unavailable_formatter(_jax_error, 'jax', aliases=[]) def __snake_case ( __UpperCamelCase : Optional[str] ): """simple docstring""" if format_type in _FORMAT_TYPES_ALIASES: return _FORMAT_TYPES_ALIASES[format_type] else: return format_type def __snake_case ( __UpperCamelCase : Optional[str] ,**__UpperCamelCase : List[Any] ): """simple docstring""" A_ = get_format_type_from_alias(__UpperCamelCase ) if format_type in _FORMAT_TYPES: return _FORMAT_TYPES[format_type](**__UpperCamelCase ) if format_type in _FORMAT_TYPES_ALIASES_UNAVAILABLE: raise _FORMAT_TYPES_ALIASES_UNAVAILABLE[format_type] else: raise ValueError( f'''Return type should be None or selected in {list(type for type in _FORMAT_TYPES.keys() if type != None )}, but got \'{format_type}\'''' )
312
1
from typing import Dict, Iterable, List, Optional, Union import numpy as np from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size_dict from ...image_transforms import ( center_crop, get_resize_output_image_size, normalize, rescale, resize, to_channel_dimension_format, ) from ...image_utils import ( IMAGENET_DEFAULT_MEAN, IMAGENET_DEFAULT_STD, ChannelDimension, ImageInput, PILImageResampling, make_list_of_images, to_numpy_array, valid_images, ) from ...utils import TensorType, logging __a :Any = logging.get_logger(__name__) class _a ( snake_case_ ): """simple docstring""" _lowerCamelCase : str = ['pixel_values'] def __init__( self : int , UpperCAmelCase : bool = True , UpperCAmelCase : Dict[str, int] = None , UpperCAmelCase : PILImageResampling = PILImageResampling.BICUBIC , UpperCAmelCase : bool = True , UpperCAmelCase : Dict[str, int] = None , UpperCAmelCase : bool = True , UpperCAmelCase : Union[int, float] = 1 / 255 , UpperCAmelCase : bool = True , UpperCAmelCase : Optional[Union[float, Iterable[float]]] = IMAGENET_DEFAULT_MEAN , UpperCAmelCase : Optional[Union[float, Iterable[float]]] = IMAGENET_DEFAULT_STD , **UpperCAmelCase : Dict , ): super().__init__(**UpperCAmelCase ) A_ = size if size is not None else {"shortest_edge": 224} A_ = get_size_dict(UpperCAmelCase , default_to_square=UpperCAmelCase ) A_ = crop_size if crop_size is not None else {"height": 224, "width": 224} A_ = get_size_dict(UpperCAmelCase , param_name="crop_size" ) A_ = do_resize A_ = size A_ = resample A_ = do_center_crop A_ = crop_size A_ = do_rescale A_ = rescale_factor A_ = do_normalize A_ = image_mean if image_mean is not None else IMAGENET_DEFAULT_MEAN A_ = image_std if image_std is not None else IMAGENET_DEFAULT_STD def __A ( self : int , UpperCAmelCase : np.ndarray , UpperCAmelCase : Dict[str, int] , UpperCAmelCase : PILImageResampling = PILImageResampling.BICUBIC , UpperCAmelCase : Optional[Union[str, ChannelDimension]] = None , **UpperCAmelCase : List[str] , ): A_ = get_size_dict(UpperCAmelCase , default_to_square=UpperCAmelCase ) # size_dict is a dict with either keys "height" and "width" or "shortest_edge" if "shortest_edge" in size: A_ = int((256 / 224) * size["shortest_edge"] ) A_ = get_resize_output_image_size(UpperCAmelCase , size=UpperCAmelCase , default_to_square=UpperCAmelCase ) A_ = {"height": output_size[0], "width": output_size[1]} if "height" not in size_dict or "width" not in size_dict: raise ValueError( f'''Size dict must have keys \'height\' and \'width\' or \'shortest_edge\'. Got {size_dict.keys()}''' ) return resize( UpperCAmelCase , size=(size_dict["height"], size_dict["width"]) , resample=UpperCAmelCase , data_format=UpperCAmelCase , **UpperCAmelCase ) def __A ( self : Union[str, Any] , UpperCAmelCase : np.ndarray , UpperCAmelCase : Dict[str, int] , UpperCAmelCase : Optional[Union[str, ChannelDimension]] = None , **UpperCAmelCase : str , ): A_ = get_size_dict(UpperCAmelCase ) if "height" not in size or "width" not in size: raise ValueError(f'''Size dict must have keys \'height\' and \'width\'. Got {size.keys()}''' ) return center_crop(UpperCAmelCase , size=(size["height"], size["width"]) , data_format=UpperCAmelCase , **UpperCAmelCase ) def __A ( self : int , UpperCAmelCase : np.ndarray , UpperCAmelCase : Union[int, float] , UpperCAmelCase : Optional[Union[str, ChannelDimension]] = None , **UpperCAmelCase : Tuple , ): return rescale(UpperCAmelCase , scale=UpperCAmelCase , data_format=UpperCAmelCase , **UpperCAmelCase ) def __A ( self : Any , UpperCAmelCase : np.ndarray , UpperCAmelCase : Union[float, List[float]] , UpperCAmelCase : Union[float, List[float]] , UpperCAmelCase : Optional[Union[str, ChannelDimension]] = None , **UpperCAmelCase : Any , ): return normalize(UpperCAmelCase , mean=UpperCAmelCase , std=UpperCAmelCase , data_format=UpperCAmelCase , **UpperCAmelCase ) def __A ( self : Optional[int] , UpperCAmelCase : ImageInput , UpperCAmelCase : Optional[bool] = None , UpperCAmelCase : Optional[Dict[str, int]] = None , UpperCAmelCase : PILImageResampling = None , UpperCAmelCase : Optional[bool] = None , UpperCAmelCase : Optional[Dict[str, int]] = None , UpperCAmelCase : Optional[bool] = None , UpperCAmelCase : Optional[float] = None , UpperCAmelCase : Optional[bool] = None , UpperCAmelCase : Optional[Union[float, Iterable[float]]] = None , UpperCAmelCase : Optional[Union[float, Iterable[float]]] = None , UpperCAmelCase : Optional[TensorType] = None , UpperCAmelCase : ChannelDimension = ChannelDimension.FIRST , **UpperCAmelCase : Optional[Any] , ): A_ = do_resize if do_resize is not None else self.do_resize A_ = resample if resample is not None else self.resample A_ = do_center_crop if do_center_crop is not None else self.do_center_crop A_ = do_rescale if do_rescale is not None else self.do_rescale A_ = rescale_factor if rescale_factor is not None else self.rescale_factor A_ = do_normalize if do_normalize is not None else self.do_normalize A_ = image_mean if image_mean is not None else self.image_mean A_ = image_std if image_std is not None else self.image_std A_ = size if size is not None else self.size A_ = get_size_dict(UpperCAmelCase , default_to_square=UpperCAmelCase ) A_ = crop_size if crop_size is not None else self.crop_size A_ = get_size_dict(UpperCAmelCase , param_name="crop_size" ) A_ = make_list_of_images(UpperCAmelCase ) if not valid_images(UpperCAmelCase ): raise ValueError( "Invalid image type. Must be of type PIL.Image.Image, numpy.ndarray, " "torch.Tensor, tf.Tensor or jax.ndarray." ) if do_resize and size is None: raise ValueError("Size must be specified if do_resize is True." ) if do_center_crop and crop_size is None: raise ValueError("Crop size must be specified if do_center_crop is True." ) if do_rescale and rescale_factor is None: raise ValueError("Rescale factor must be specified if do_rescale is True." ) 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." ) # All transformations expect numpy arrays. A_ = [to_numpy_array(UpperCAmelCase ) for image in images] if do_resize: A_ = [self.resize(UpperCAmelCase , UpperCAmelCase , UpperCAmelCase ) for image in images] if do_center_crop: A_ = [self.center_crop(UpperCAmelCase , UpperCAmelCase ) for image in images] if do_rescale: A_ = [self.rescale(UpperCAmelCase , UpperCAmelCase ) for image in images] if do_normalize: A_ = [self.normalize(UpperCAmelCase , UpperCAmelCase , UpperCAmelCase ) for image in images] A_ = [to_channel_dimension_format(UpperCAmelCase , UpperCAmelCase ) for image in images] A_ = {"pixel_values": images} return BatchFeature(data=UpperCAmelCase , tensor_type=UpperCAmelCase )
312
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available, is_vision_available __a :int = { 'configuration_mask2former': [ 'MASK2FORMER_PRETRAINED_CONFIG_ARCHIVE_MAP', 'Mask2FormerConfig', ], } try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __a :Union[str, Any] = ['Mask2FormerImageProcessor'] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __a :Optional[Any] = [ 'MASK2FORMER_PRETRAINED_MODEL_ARCHIVE_LIST', 'Mask2FormerForUniversalSegmentation', 'Mask2FormerModel', 'Mask2FormerPreTrainedModel', ] if TYPE_CHECKING: from .configuration_maskaformer import MASK2FORMER_PRETRAINED_CONFIG_ARCHIVE_MAP, MaskaFormerConfig try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .image_processing_maskaformer import MaskaFormerImageProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_maskaformer import ( MASK2FORMER_PRETRAINED_MODEL_ARCHIVE_LIST, MaskaFormerForUniversalSegmentation, MaskaFormerModel, MaskaFormerPreTrainedModel, ) else: import sys __a :Tuple = _LazyModule(__name__, globals()['__file__'], _import_structure)
312
1
import cva import numpy as np class _a : """simple docstring""" def __init__( self : Any , UpperCAmelCase : float , UpperCAmelCase : int ): if k in (0.04, 0.06): A_ = k A_ = window_size else: raise ValueError("invalid k value" ) def __str__( self : Optional[Any] ): return str(self.k ) def __A ( self : int , UpperCAmelCase : str ): A_ = cva.imread(UpperCAmelCase , 0 ) A_ , A_ = img.shape A_ = [] A_ = img.copy() A_ = cva.cvtColor(UpperCAmelCase , cva.COLOR_GRAY2RGB ) A_ , A_ = np.gradient(UpperCAmelCase ) A_ = dx**2 A_ = dy**2 A_ = dx * dy A_ = 0.04 A_ = self.window_size // 2 for y in range(UpperCAmelCase , h - offset ): for x in range(UpperCAmelCase , w - offset ): A_ = ixx[ y - offset : y + offset + 1, x - offset : x + offset + 1 ].sum() A_ = iyy[ y - offset : y + offset + 1, x - offset : x + offset + 1 ].sum() A_ = ixy[ y - offset : y + offset + 1, x - offset : x + offset + 1 ].sum() A_ = (wxx * wyy) - (wxy**2) A_ = wxx + wyy A_ = det - k * (trace**2) # Can change the value if r > 0.5: corner_list.append([x, y, r] ) color_img.itemset((y, x, 0) , 0 ) color_img.itemset((y, x, 1) , 0 ) color_img.itemset((y, x, 2) , 255 ) return color_img, corner_list if __name__ == "__main__": __a :List[str] = HarrisCorner(0.04, 3) __a , __a :str = edge_detect.detect('path_to_image') cva.imwrite('detect.png', color_img)
312
import copy from dataclasses import dataclass, field from typing import ClassVar, Dict from ..features import Audio, ClassLabel, Features from .base import TaskTemplate @dataclass(frozen=snake_case_ ) class _a ( snake_case_ ): """simple docstring""" _lowerCamelCase : str = field(default='audio-classification' , metadata={'include_in_asdict_even_if_is_default': True} ) _lowerCamelCase : ClassVar[Features] = Features({'audio': Audio()} ) _lowerCamelCase : ClassVar[Features] = Features({'labels': ClassLabel} ) _lowerCamelCase : str = "audio" _lowerCamelCase : str = "labels" def __A ( self : str , UpperCAmelCase : List[Any] ): if self.label_column not in features: raise ValueError(f'''Column {self.label_column} is not present in features.''' ) if not isinstance(features[self.label_column] , UpperCAmelCase ): raise ValueError(f'''Column {self.label_column} is not a ClassLabel.''' ) A_ = copy.deepcopy(self ) A_ = self.label_schema.copy() A_ = features[self.label_column] A_ = label_schema return task_template @property def __A ( self : List[str] ): return { self.audio_column: "audio", self.label_column: "labels", }
312
1
import argparse import intel_extension_for_pytorch as ipex import torch from diffusers import DPMSolverMultistepScheduler, StableDiffusionPipeline __a :str = argparse.ArgumentParser('Stable Diffusion script with intel optimization', add_help=False) parser.add_argument('--dpm', action='store_true', help='Enable DPMSolver or not') parser.add_argument('--steps', default=None, type=int, help='Num inference steps') __a :Any = parser.parse_args() __a :Dict = 'cpu' __a :Dict = 'a lovely <dicoo> in red dress and hat, in the snowly and brightly night, with many brighly buildings' __a :Optional[Any] = 'path-to-your-trained-model' __a :Dict = StableDiffusionPipeline.from_pretrained(model_id) if args.dpm: __a :List[Any] = DPMSolverMultistepScheduler.from_config(pipe.scheduler.config) __a :Optional[int] = pipe.to(device) # to channels last __a :Any = pipe.unet.to(memory_format=torch.channels_last) __a :Any = pipe.vae.to(memory_format=torch.channels_last) __a :Any = pipe.text_encoder.to(memory_format=torch.channels_last) if pipe.requires_safety_checker: __a :Any = pipe.safety_checker.to(memory_format=torch.channels_last) # optimize with ipex __a :str = torch.randn(2, 4, 64, 64) __a :Optional[int] = torch.rand(1) * 999 __a :Optional[int] = torch.randn(2, 77, 768) __a :int = (sample, timestep, encoder_hidden_status) try: __a :Optional[Any] = ipex.optimize(pipe.unet.eval(), dtype=torch.bfloataa, inplace=True, sample_input=input_example) except Exception: __a :List[str] = ipex.optimize(pipe.unet.eval(), dtype=torch.bfloataa, inplace=True) __a :List[Any] = ipex.optimize(pipe.vae.eval(), dtype=torch.bfloataa, inplace=True) __a :List[Any] = ipex.optimize(pipe.text_encoder.eval(), dtype=torch.bfloataa, inplace=True) if pipe.requires_safety_checker: __a :Optional[int] = ipex.optimize(pipe.safety_checker.eval(), dtype=torch.bfloataa, inplace=True) # compute __a :List[str] = 666 __a :List[Any] = torch.Generator(device).manual_seed(seed) __a :int = {'generator': generator} if args.steps is not None: __a :Optional[int] = args.steps with torch.cpu.amp.autocast(enabled=True, dtype=torch.bfloataa): __a :Optional[Any] = pipe(prompt, **generate_kwargs).images[0] # save image image.save('generated.png')
312
def __snake_case ( __UpperCamelCase : bytes ): """simple docstring""" return "".join([hex(__UpperCamelCase )[2:].zfill(2 ).upper() for byte in list(__UpperCamelCase )] ) def __snake_case ( __UpperCamelCase : str ): """simple docstring""" if (len(__UpperCamelCase ) % 2) != 0: raise ValueError( "Base16 encoded data is invalid:\nData does not have an even number of hex digits." ) # Check the character set - the standard base16 alphabet # is uppercase according to RFC3548 section 6 if not set(__UpperCamelCase ) <= set("0123456789ABCDEF" ): raise ValueError( "Base16 encoded data is invalid:\nData is not uppercase hex or it contains invalid characters." ) # For every two hexadecimal digits (= a byte), turn it into an integer. # Then, string the result together into bytes, and return it. return bytes(int(data[i] + data[i + 1] ,16 ) for i in range(0 ,len(__UpperCamelCase ) ,2 ) ) if __name__ == "__main__": import doctest doctest.testmod()
312
1
from pathlib import Path import fire from tqdm import tqdm def __snake_case ( __UpperCamelCase : str="ro" ,__UpperCamelCase : Optional[int]="en" ,__UpperCamelCase : Optional[int]="wmt16" ,__UpperCamelCase : int=None ): """simple docstring""" try: import datasets except (ModuleNotFoundError, ImportError): raise ImportError("run pip install datasets" ) A_ = f'''{src_lang}-{tgt_lang}''' print(f'''Converting {dataset}-{pair}''' ) A_ = datasets.load_dataset(__UpperCamelCase ,__UpperCamelCase ) if save_dir is None: A_ = f'''{dataset}-{pair}''' A_ = Path(__UpperCamelCase ) save_dir.mkdir(exist_ok=__UpperCamelCase ) for split in ds.keys(): print(f'''Splitting {split} with {ds[split].num_rows} records''' ) # to save to val.source, val.target like summary datasets A_ = "val" if split == "validation" else split A_ = save_dir.joinpath(f'''{fn}.source''' ) A_ = save_dir.joinpath(f'''{fn}.target''' ) A_ = src_path.open("w+" ) A_ = tgt_path.open("w+" ) # reader is the bottleneck so writing one record at a time doesn't slow things down for x in tqdm(ds[split] ): A_ = x["translation"] src_fp.write(ex[src_lang] + "\n" ) tgt_fp.write(ex[tgt_lang] + "\n" ) print(f'''Saved {dataset} dataset to {save_dir}''' ) if __name__ == "__main__": fire.Fire(download_wmt_dataset)
312
import cva import numpy as np class _a : """simple docstring""" def __init__( self : Any , UpperCAmelCase : float , UpperCAmelCase : int ): if k in (0.04, 0.06): A_ = k A_ = window_size else: raise ValueError("invalid k value" ) def __str__( self : Optional[Any] ): return str(self.k ) def __A ( self : int , UpperCAmelCase : str ): A_ = cva.imread(UpperCAmelCase , 0 ) A_ , A_ = img.shape A_ = [] A_ = img.copy() A_ = cva.cvtColor(UpperCAmelCase , cva.COLOR_GRAY2RGB ) A_ , A_ = np.gradient(UpperCAmelCase ) A_ = dx**2 A_ = dy**2 A_ = dx * dy A_ = 0.04 A_ = self.window_size // 2 for y in range(UpperCAmelCase , h - offset ): for x in range(UpperCAmelCase , w - offset ): A_ = ixx[ y - offset : y + offset + 1, x - offset : x + offset + 1 ].sum() A_ = iyy[ y - offset : y + offset + 1, x - offset : x + offset + 1 ].sum() A_ = ixy[ y - offset : y + offset + 1, x - offset : x + offset + 1 ].sum() A_ = (wxx * wyy) - (wxy**2) A_ = wxx + wyy A_ = det - k * (trace**2) # Can change the value if r > 0.5: corner_list.append([x, y, r] ) color_img.itemset((y, x, 0) , 0 ) color_img.itemset((y, x, 1) , 0 ) color_img.itemset((y, x, 2) , 255 ) return color_img, corner_list if __name__ == "__main__": __a :List[str] = HarrisCorner(0.04, 3) __a , __a :str = edge_detect.detect('path_to_image') cva.imwrite('detect.png', color_img)
312
1
from __future__ import annotations import numpy as np def __snake_case ( __UpperCamelCase : list[float] ): """simple docstring""" return np.maximum(0 ,__UpperCamelCase ) if __name__ == "__main__": print(np.array(relu([-1, 0, 5]))) # --> [0, 0, 5]
312
def __snake_case ( __UpperCamelCase : int = 1000 ): """simple docstring""" return sum(2 * a * ((a - 1) // 2) for a in range(3 ,n + 1 ) ) if __name__ == "__main__": print(solution())
312
1
from __future__ import annotations import math def __snake_case ( __UpperCamelCase : int ,__UpperCamelCase : int ,__UpperCamelCase : bool ,__UpperCamelCase : list[int] ,__UpperCamelCase : float ): """simple docstring""" if depth < 0: raise ValueError("Depth cannot be less than 0" ) if not scores: raise ValueError("Scores cannot be empty" ) if depth == height: return scores[node_index] return ( max( minimax(depth + 1 ,node_index * 2 ,__UpperCamelCase ,__UpperCamelCase ,__UpperCamelCase ) ,minimax(depth + 1 ,node_index * 2 + 1 ,__UpperCamelCase ,__UpperCamelCase ,__UpperCamelCase ) ,) if is_max else min( minimax(depth + 1 ,node_index * 2 ,__UpperCamelCase ,__UpperCamelCase ,__UpperCamelCase ) ,minimax(depth + 1 ,node_index * 2 + 1 ,__UpperCamelCase ,__UpperCamelCase ,__UpperCamelCase ) ,) ) def __snake_case ( ): """simple docstring""" A_ = [90, 23, 6, 33, 21, 65, 123, 3_4423] A_ = math.log(len(__UpperCamelCase ) ,2 ) print(f'''Optimal value : {minimax(0 ,0 ,__UpperCamelCase ,__UpperCamelCase ,__UpperCamelCase )}''' ) if __name__ == "__main__": import doctest doctest.testmod() main()
312
import warnings from typing import List import numpy as np from ...processing_utils import ProcessorMixin from ...tokenization_utils_base import BatchEncoding from ...utils import is_flax_available, is_tf_available, is_torch_available class _a ( snake_case_ ): """simple docstring""" _lowerCamelCase : Union[str, Any] = ['image_processor', 'tokenizer'] _lowerCamelCase : Tuple = 'OwlViTImageProcessor' _lowerCamelCase : List[Any] = ('CLIPTokenizer', 'CLIPTokenizerFast') def __init__( self : Optional[Any] , UpperCAmelCase : int=None , UpperCAmelCase : Union[str, Any]=None , **UpperCAmelCase : Any ): A_ = None if "feature_extractor" in kwargs: warnings.warn( "The `feature_extractor` argument is deprecated and will be removed in v5, use `image_processor`" " instead." , UpperCAmelCase , ) A_ = kwargs.pop("feature_extractor" ) A_ = image_processor if image_processor is not None else feature_extractor if image_processor is None: raise ValueError("You need to specify an `image_processor`." ) if tokenizer is None: raise ValueError("You need to specify a `tokenizer`." ) super().__init__(UpperCAmelCase , UpperCAmelCase ) def __call__( self : Optional[int] , UpperCAmelCase : List[str]=None , UpperCAmelCase : List[Any]=None , UpperCAmelCase : Optional[int]=None , UpperCAmelCase : Dict="max_length" , UpperCAmelCase : Optional[Any]="np" , **UpperCAmelCase : Optional[int] ): if text is None and query_images is None and images is None: raise ValueError( "You have to specify at least one text or query image or image. All three cannot be none." ) if text is not None: if isinstance(UpperCAmelCase , UpperCAmelCase ) or (isinstance(UpperCAmelCase , UpperCAmelCase ) and not isinstance(text[0] , UpperCAmelCase )): A_ = [self.tokenizer(UpperCAmelCase , padding=UpperCAmelCase , return_tensors=UpperCAmelCase , **UpperCAmelCase )] elif isinstance(UpperCAmelCase , UpperCAmelCase ) and isinstance(text[0] , UpperCAmelCase ): A_ = [] # Maximum number of queries across batch A_ = max([len(UpperCAmelCase ) for t in text] ) # Pad all batch samples to max number of text queries for t in text: if len(UpperCAmelCase ) != max_num_queries: A_ = t + [" "] * (max_num_queries - len(UpperCAmelCase )) A_ = self.tokenizer(UpperCAmelCase , padding=UpperCAmelCase , return_tensors=UpperCAmelCase , **UpperCAmelCase ) encodings.append(UpperCAmelCase ) else: raise TypeError("Input text should be a string, a list of strings or a nested list of strings" ) if return_tensors == "np": A_ = np.concatenate([encoding["input_ids"] for encoding in encodings] , axis=0 ) A_ = np.concatenate([encoding["attention_mask"] for encoding in encodings] , axis=0 ) elif return_tensors == "jax" and is_flax_available(): import jax.numpy as jnp A_ = jnp.concatenate([encoding["input_ids"] for encoding in encodings] , axis=0 ) A_ = jnp.concatenate([encoding["attention_mask"] for encoding in encodings] , axis=0 ) elif return_tensors == "pt" and is_torch_available(): import torch A_ = torch.cat([encoding["input_ids"] for encoding in encodings] , dim=0 ) A_ = torch.cat([encoding["attention_mask"] for encoding in encodings] , dim=0 ) elif return_tensors == "tf" and is_tf_available(): import tensorflow as tf A_ = tf.stack([encoding["input_ids"] for encoding in encodings] , axis=0 ) A_ = tf.stack([encoding["attention_mask"] for encoding in encodings] , axis=0 ) else: raise ValueError("Target return tensor type could not be returned" ) A_ = BatchEncoding() A_ = input_ids A_ = attention_mask if query_images is not None: A_ = BatchEncoding() A_ = self.image_processor( UpperCAmelCase , return_tensors=UpperCAmelCase , **UpperCAmelCase ).pixel_values A_ = query_pixel_values if images is not None: A_ = self.image_processor(UpperCAmelCase , return_tensors=UpperCAmelCase , **UpperCAmelCase ) if text is not None and images is not None: A_ = image_features.pixel_values return encoding elif query_images is not None and images is not None: A_ = image_features.pixel_values return encoding elif text is not None or query_images is not None: return encoding else: return BatchEncoding(data=dict(**UpperCAmelCase ) , tensor_type=UpperCAmelCase ) def __A ( self : Optional[Any] , *UpperCAmelCase : Union[str, Any] , **UpperCAmelCase : List[Any] ): return self.image_processor.post_process(*UpperCAmelCase , **UpperCAmelCase ) def __A ( self : str , *UpperCAmelCase : str , **UpperCAmelCase : Union[str, Any] ): return self.image_processor.post_process_object_detection(*UpperCAmelCase , **UpperCAmelCase ) def __A ( self : List[Any] , *UpperCAmelCase : int , **UpperCAmelCase : int ): return self.image_processor.post_process_image_guided_detection(*UpperCAmelCase , **UpperCAmelCase ) def __A ( self : List[Any] , *UpperCAmelCase : Optional[int] , **UpperCAmelCase : Any ): return self.tokenizer.batch_decode(*UpperCAmelCase , **UpperCAmelCase ) def __A ( self : Tuple , *UpperCAmelCase : Dict , **UpperCAmelCase : str ): return self.tokenizer.decode(*UpperCAmelCase , **UpperCAmelCase ) @property def __A ( self : Union[str, Any] ): warnings.warn( "`feature_extractor_class` is deprecated and will be removed in v5. Use `image_processor_class` instead." , UpperCAmelCase , ) return self.image_processor_class @property def __A ( self : Optional[Any] ): warnings.warn( "`feature_extractor` is deprecated and will be removed in v5. Use `image_processor` instead." , UpperCAmelCase , ) return self.image_processor
312
1
import inspect import unittest from transformers import YolosConfig 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 from ...test_pipeline_mixin import PipelineTesterMixin if is_torch_available(): import torch from torch import nn from transformers import YolosForObjectDetection, YolosModel from transformers.models.yolos.modeling_yolos import YOLOS_PRETRAINED_MODEL_ARCHIVE_LIST if is_vision_available(): from PIL import Image from transformers import AutoImageProcessor class _a : """simple docstring""" def __init__( self : Dict , UpperCAmelCase : Union[str, Any] , UpperCAmelCase : Optional[int]=13 , UpperCAmelCase : List[Any]=[30, 30] , UpperCAmelCase : Union[str, Any]=2 , UpperCAmelCase : str=3 , UpperCAmelCase : Optional[int]=True , UpperCAmelCase : Any=True , UpperCAmelCase : List[str]=32 , UpperCAmelCase : Optional[Any]=5 , UpperCAmelCase : List[str]=4 , UpperCAmelCase : str=37 , UpperCAmelCase : Optional[int]="gelu" , UpperCAmelCase : Tuple=0.1 , UpperCAmelCase : str=0.1 , UpperCAmelCase : str=10 , UpperCAmelCase : str=0.02 , UpperCAmelCase : Union[str, Any]=3 , UpperCAmelCase : str=None , UpperCAmelCase : List[str]=8 , UpperCAmelCase : Union[str, Any]=10 , ): A_ = parent A_ = batch_size A_ = image_size A_ = patch_size A_ = num_channels A_ = is_training A_ = use_labels A_ = hidden_size A_ = num_hidden_layers A_ = num_attention_heads A_ = intermediate_size A_ = hidden_act A_ = hidden_dropout_prob A_ = attention_probs_dropout_prob A_ = type_sequence_label_size A_ = initializer_range A_ = num_labels A_ = scope A_ = n_targets A_ = num_detection_tokens # we set the expected sequence length (which is used in several tests) # expected sequence length = num_patches + 1 (we add 1 for the [CLS] token) + num_detection_tokens A_ = (image_size[1] // patch_size) * (image_size[0] // patch_size) A_ = num_patches + 1 + self.num_detection_tokens def __A ( self : Optional[int] ): A_ = floats_tensor([self.batch_size, self.num_channels, self.image_size[0], self.image_size[1]] ) A_ = None if self.use_labels: # labels is a list of Dict (each Dict being the labels for a given example in the batch) A_ = [] for i in range(self.batch_size ): A_ = {} A_ = torch.randint( high=self.num_labels , size=(self.n_targets,) , device=UpperCAmelCase ) A_ = torch.rand(self.n_targets , 4 , device=UpperCAmelCase ) labels.append(UpperCAmelCase ) A_ = self.get_config() return config, pixel_values, labels def __A ( self : Any ): return YolosConfig( image_size=self.image_size , patch_size=self.patch_size , num_channels=self.num_channels , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , is_decoder=UpperCAmelCase , initializer_range=self.initializer_range , num_detection_tokens=self.num_detection_tokens , num_labels=self.num_labels , ) def __A ( self : Union[str, Any] , UpperCAmelCase : Optional[Any] , UpperCAmelCase : List[Any] , UpperCAmelCase : Dict ): A_ = YolosModel(config=UpperCAmelCase ) model.to(UpperCAmelCase ) model.eval() A_ = model(UpperCAmelCase ) self.parent.assertEqual( result.last_hidden_state.shape , (self.batch_size, self.expected_seq_len, self.hidden_size) ) def __A ( self : List[Any] , UpperCAmelCase : int , UpperCAmelCase : Dict , UpperCAmelCase : List[Any] ): A_ = YolosForObjectDetection(UpperCAmelCase ) model.to(UpperCAmelCase ) model.eval() A_ = model(pixel_values=UpperCAmelCase ) A_ = model(UpperCAmelCase ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_detection_tokens, self.num_labels + 1) ) self.parent.assertEqual(result.pred_boxes.shape , (self.batch_size, self.num_detection_tokens, 4) ) A_ = model(pixel_values=UpperCAmelCase , labels=UpperCAmelCase ) self.parent.assertEqual(result.loss.shape , () ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_detection_tokens, self.num_labels + 1) ) self.parent.assertEqual(result.pred_boxes.shape , (self.batch_size, self.num_detection_tokens, 4) ) def __A ( self : Tuple ): A_ = self.prepare_config_and_inputs() A_ , A_ , A_ = config_and_inputs A_ = {"pixel_values": pixel_values} return config, inputs_dict @require_torch class _a ( snake_case_ , snake_case_ , unittest.TestCase ): """simple docstring""" _lowerCamelCase : Optional[int] = (YolosModel, YolosForObjectDetection) if is_torch_available() else () _lowerCamelCase : List[Any] = ( {'feature-extraction': YolosModel, 'object-detection': YolosForObjectDetection} if is_torch_available() else {} ) _lowerCamelCase : Any = False _lowerCamelCase : Tuple = False _lowerCamelCase : Optional[Any] = False _lowerCamelCase : Optional[int] = False def __A ( self : str , UpperCAmelCase : Dict , UpperCAmelCase : List[Any] , UpperCAmelCase : Optional[Any]=False ): A_ = super()._prepare_for_class(UpperCAmelCase , UpperCAmelCase , return_labels=UpperCAmelCase ) if return_labels: if model_class.__name__ == "YolosForObjectDetection": A_ = [] for i in range(self.model_tester.batch_size ): A_ = {} A_ = torch.ones( size=(self.model_tester.n_targets,) , device=UpperCAmelCase , dtype=torch.long ) A_ = torch.ones( self.model_tester.n_targets , 4 , device=UpperCAmelCase , dtype=torch.float ) labels.append(UpperCAmelCase ) A_ = labels return inputs_dict def __A ( self : List[str] ): A_ = YolosModelTester(self ) A_ = ConfigTester(self , config_class=UpperCAmelCase , has_text_modality=UpperCAmelCase , hidden_size=37 ) def __A ( self : Tuple ): self.config_tester.run_common_tests() def __A ( self : str ): # YOLOS does not use inputs_embeds pass def __A ( self : Tuple ): A_ , A_ = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: A_ = model_class(UpperCAmelCase ) self.assertIsInstance(model.get_input_embeddings() , (nn.Module) ) A_ = model.get_output_embeddings() self.assertTrue(x is None or isinstance(UpperCAmelCase , nn.Linear ) ) def __A ( self : Optional[Any] ): A_ , A_ = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: A_ = model_class(UpperCAmelCase ) A_ = inspect.signature(model.forward ) # signature.parameters is an OrderedDict => so arg_names order is deterministic A_ = [*signature.parameters.keys()] A_ = ["pixel_values"] self.assertListEqual(arg_names[:1] , UpperCAmelCase ) def __A ( self : List[Any] ): A_ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_model(*UpperCAmelCase ) def __A ( self : Dict ): A_ , A_ = self.model_tester.prepare_config_and_inputs_for_common() A_ = True # in YOLOS, the seq_len is different A_ = self.model_tester.expected_seq_len for model_class in self.all_model_classes: A_ = True A_ = False A_ = True A_ = model_class(UpperCAmelCase ) model.to(UpperCAmelCase ) model.eval() with torch.no_grad(): A_ = model(**self._prepare_for_class(UpperCAmelCase , UpperCAmelCase ) ) A_ = outputs.attentions self.assertEqual(len(UpperCAmelCase ) , self.model_tester.num_hidden_layers ) # check that output_attentions also work using config del inputs_dict["output_attentions"] A_ = True A_ = model_class(UpperCAmelCase ) model.to(UpperCAmelCase ) model.eval() with torch.no_grad(): A_ = model(**self._prepare_for_class(UpperCAmelCase , UpperCAmelCase ) ) A_ = outputs.attentions self.assertEqual(len(UpperCAmelCase ) , self.model_tester.num_hidden_layers ) self.assertListEqual( list(attentions[0].shape[-3:] ) , [self.model_tester.num_attention_heads, seq_len, seq_len] , ) A_ = len(UpperCAmelCase ) # Check attention is always last and order is fine A_ = True A_ = True A_ = model_class(UpperCAmelCase ) model.to(UpperCAmelCase ) model.eval() with torch.no_grad(): A_ = model(**self._prepare_for_class(UpperCAmelCase , UpperCAmelCase ) ) A_ = 1 self.assertEqual(out_len + added_hidden_states , len(UpperCAmelCase ) ) A_ = outputs.attentions self.assertEqual(len(UpperCAmelCase ) , self.model_tester.num_hidden_layers ) self.assertListEqual( list(self_attentions[0].shape[-3:] ) , [self.model_tester.num_attention_heads, seq_len, seq_len] , ) def __A ( self : Tuple ): def check_hidden_states_output(UpperCAmelCase : Dict , UpperCAmelCase : List[str] , UpperCAmelCase : Dict ): A_ = model_class(UpperCAmelCase ) model.to(UpperCAmelCase ) model.eval() with torch.no_grad(): A_ = model(**self._prepare_for_class(UpperCAmelCase , UpperCAmelCase ) ) A_ = outputs.hidden_states A_ = getattr( self.model_tester , "expected_num_hidden_layers" , self.model_tester.num_hidden_layers + 1 ) self.assertEqual(len(UpperCAmelCase ) , UpperCAmelCase ) # YOLOS has a different seq_length A_ = self.model_tester.expected_seq_len self.assertListEqual( list(hidden_states[0].shape[-2:] ) , [seq_length, self.model_tester.hidden_size] , ) A_ , A_ = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: A_ = True check_hidden_states_output(UpperCAmelCase , UpperCAmelCase , UpperCAmelCase ) # check that output_hidden_states also work using config del inputs_dict["output_hidden_states"] A_ = True check_hidden_states_output(UpperCAmelCase , UpperCAmelCase , UpperCAmelCase ) def __A ( self : Union[str, Any] ): A_ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_for_object_detection(*UpperCAmelCase ) @slow def __A ( self : Dict ): for model_name in YOLOS_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: A_ = YolosModel.from_pretrained(UpperCAmelCase ) self.assertIsNotNone(UpperCAmelCase ) def __snake_case ( ): """simple docstring""" A_ = Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png" ) return image @require_torch @require_vision class _a ( unittest.TestCase ): """simple docstring""" @cached_property def __A ( self : int ): return AutoImageProcessor.from_pretrained("hustvl/yolos-small" ) if is_vision_available() else None @slow def __A ( self : List[str] ): A_ = YolosForObjectDetection.from_pretrained("hustvl/yolos-small" ).to(UpperCAmelCase ) A_ = self.default_image_processor A_ = prepare_img() A_ = image_processor(images=UpperCAmelCase , return_tensors="pt" ).to(UpperCAmelCase ) # forward pass with torch.no_grad(): A_ = model(inputs.pixel_values ) # verify outputs A_ = torch.Size((1, 100, 92) ) self.assertEqual(outputs.logits.shape , UpperCAmelCase ) A_ = torch.tensor( [[-24.0_248, -10.3_024, -14.8_290], [-42.0_392, -16.8_200, -27.4_334], [-27.2_743, -11.8_154, -18.7_148]] , device=UpperCAmelCase , ) A_ = torch.tensor( [[0.2_559, 0.5_455, 0.4_706], [0.2_989, 0.7_279, 0.1_875], [0.7_732, 0.4_017, 0.4_462]] , device=UpperCAmelCase ) self.assertTrue(torch.allclose(outputs.logits[0, :3, :3] , UpperCAmelCase , atol=1E-4 ) ) self.assertTrue(torch.allclose(outputs.pred_boxes[0, :3, :3] , UpperCAmelCase , atol=1E-4 ) ) # verify postprocessing A_ = image_processor.post_process_object_detection( UpperCAmelCase , threshold=0.3 , target_sizes=[image.size[::-1]] )[0] A_ = torch.tensor([0.9_994, 0.9_790, 0.9_964, 0.9_972, 0.9_861] ).to(UpperCAmelCase ) A_ = [75, 75, 17, 63, 17] A_ = torch.tensor([335.0_609, 79.3_848, 375.4_216, 187.2_495] ).to(UpperCAmelCase ) self.assertEqual(len(results["scores"] ) , 5 ) self.assertTrue(torch.allclose(results["scores"] , UpperCAmelCase , atol=1E-4 ) ) self.assertSequenceEqual(results["labels"].tolist() , UpperCAmelCase ) self.assertTrue(torch.allclose(results["boxes"][0, :] , UpperCAmelCase ) )
312
from typing import Optional, Union import torch from torch import nn from ...configuration_utils import ConfigMixin, register_to_config from ...models.modeling_utils import ModelMixin class _a ( snake_case_ , snake_case_ ): """simple docstring""" @register_to_config def __init__( self : Dict , UpperCAmelCase : int = 768 , ): super().__init__() A_ = nn.Parameter(torch.zeros(1 , UpperCAmelCase ) ) A_ = nn.Parameter(torch.ones(1 , UpperCAmelCase ) ) def __A ( self : str , UpperCAmelCase : Optional[Union[str, torch.device]] = None , UpperCAmelCase : Optional[torch.dtype] = None , ): A_ = nn.Parameter(self.mean.to(UpperCAmelCase ).to(UpperCAmelCase ) ) A_ = nn.Parameter(self.std.to(UpperCAmelCase ).to(UpperCAmelCase ) ) return self def __A ( self : Dict , UpperCAmelCase : List[Any] ): A_ = (embeds - self.mean) * 1.0 / self.std return embeds def __A ( self : int , UpperCAmelCase : int ): A_ = (embeds * self.std) + self.mean return embeds
312
1
import copy from dataclasses import dataclass from pathlib import Path from typing import Dict, Optional, Union @dataclass class _a : """simple docstring""" _lowerCamelCase : Optional[Union[str, Path]] = None _lowerCamelCase : bool = False _lowerCamelCase : bool = False _lowerCamelCase : bool = False _lowerCamelCase : Optional[Dict] = None _lowerCamelCase : Optional[str] = None _lowerCamelCase : bool = False _lowerCamelCase : bool = False _lowerCamelCase : bool = False _lowerCamelCase : bool = True _lowerCamelCase : Optional[int] = None _lowerCamelCase : int = 1 _lowerCamelCase : Optional[Union[str, bool]] = None _lowerCamelCase : bool = False _lowerCamelCase : Optional[Dict] = None _lowerCamelCase : Optional[str] = None def __A ( self : Optional[int] ): return self.__class__(**{k: copy.deepcopy(UpperCAmelCase ) for k, v in self.__dict__.items()} )
312
from __future__ import annotations import numpy as np from numpy import floataa from numpy.typing import NDArray def __snake_case ( __UpperCamelCase : NDArray[floataa] ,__UpperCamelCase : NDArray[floataa] ,__UpperCamelCase : list[int] ,__UpperCamelCase : int ,): """simple docstring""" A_ , A_ = coefficient_matrix.shape A_ , A_ = constant_matrix.shape if rowsa != colsa: A_ = f'''Coefficient matrix dimensions must be nxn but received {rowsa}x{colsa}''' raise ValueError(__UpperCamelCase ) if colsa != 1: A_ = f'''Constant matrix must be nx1 but received {rowsa}x{colsa}''' raise ValueError(__UpperCamelCase ) if rowsa != rowsa: A_ = ( "Coefficient and constant matrices dimensions must be nxn and nx1 but " f'''received {rowsa}x{colsa} and {rowsa}x{colsa}''' ) raise ValueError(__UpperCamelCase ) if len(__UpperCamelCase ) != rowsa: A_ = ( "Number of initial values must be equal to number of rows in coefficient " f'''matrix but received {len(__UpperCamelCase )} and {rowsa}''' ) raise ValueError(__UpperCamelCase ) if iterations <= 0: raise ValueError("Iterations must be at least 1" ) A_ = np.concatenate( (coefficient_matrix, constant_matrix) ,axis=1 ) A_ , A_ = table.shape strictly_diagonally_dominant(__UpperCamelCase ) # Iterates the whole matrix for given number of times for _ in range(__UpperCamelCase ): A_ = [] for row in range(__UpperCamelCase ): A_ = 0 for col in range(__UpperCamelCase ): if col == row: A_ = table[row][col] elif col == cols - 1: A_ = table[row][col] else: temp += (-1) * table[row][col] * init_val[col] A_ = (temp + val) / denom new_val.append(__UpperCamelCase ) A_ = new_val return [float(__UpperCamelCase ) for i in new_val] def __snake_case ( __UpperCamelCase : NDArray[floataa] ): """simple docstring""" A_ , A_ = table.shape A_ = True for i in range(0 ,__UpperCamelCase ): A_ = 0 for j in range(0 ,cols - 1 ): if i == j: continue else: total += table[i][j] if table[i][i] <= total: raise ValueError("Coefficient matrix is not strictly diagonally dominant" ) return is_diagonally_dominant # Test Cases if __name__ == "__main__": import doctest doctest.testmod()
312
1
from sympy import diff, lambdify, symbols from sympy.functions import * # noqa: F403 def __snake_case ( __UpperCamelCase : str ,__UpperCamelCase : complex ,__UpperCamelCase : str = "x" ,__UpperCamelCase : float = 10**-10 ,__UpperCamelCase : int = 1 ,): """simple docstring""" A_ = symbols(__UpperCamelCase ) A_ = lambdify(__UpperCamelCase ,__UpperCamelCase ) A_ = lambdify(__UpperCamelCase ,diff(__UpperCamelCase ,__UpperCamelCase ) ) A_ = starting_point while True: if diff_function(__UpperCamelCase ) != 0: A_ = prev_guess - multiplicity * func(__UpperCamelCase ) / diff_function( __UpperCamelCase ) else: raise ZeroDivisionError("Could not find root" ) from None # Precision is checked by comparing the difference of consecutive guesses if abs(next_guess - prev_guess ) < precision: return next_guess A_ = next_guess # Let's Execute if __name__ == "__main__": # Find root of trigonometric function # Find value of pi print(F"The root of sin(x) = 0 is {newton_raphson('sin(x)', 2)}") # Find root of polynomial # Find fourth Root of 5 print(F"The root of x**4 - 5 = 0 is {newton_raphson('x**4 -5', 0.4 +5J)}") # Find value of e print( 'The root of log(y) - 1 = 0 is ', F"{newton_raphson('log(y) - 1', 2, variable='y')}", ) # Exponential Roots print( 'The root of exp(x) - 1 = 0 is', F"{newton_raphson('exp(x) - 1', 10, precision=0.005)}", ) # Find root of cos(x) print(F"The root of cos(x) = 0 is {newton_raphson('cos(x)', 0)}")
312
from unittest import TestCase from datasets import Dataset from minhash_deduplication import deduplicate_dataset, make_duplicate_clusters def __snake_case ( ): """simple docstring""" A_ = { "repo_name": ["test_repo1", "test_repo2", "test_repo3"], "path": ["test_1.py", "test_2.py", "unit_test.py"], "content": ["a " * 20, "a " * 30, "b " * 7], } A_ = Dataset.from_dict(__UpperCamelCase ) return dataset class _a ( snake_case_ ): """simple docstring""" def __A ( self : Union[str, Any] ): A_ = get_dataset() A_ = make_duplicate_clusters(UpperCAmelCase , 0.85 ) self.assertEqual(len(duplicate_clusters[0] ) , 2 ) def __A ( self : List[Any] ): A_ = get_dataset() A_ , A_ = deduplicate_dataset(UpperCAmelCase ) self.assertEqual(len(UpperCAmelCase ) , 2 ) print(UpperCAmelCase ) self.assertEqual(duplicate_clusters[0][0]["copies"] , 2 ) self.assertEqual(duplicate_clusters[0][0]["is_extreme"] , UpperCAmelCase )
312
1
import warnings from ...utils import logging from .image_processing_imagegpt import ImageGPTImageProcessor __a :Optional[int] = logging.get_logger(__name__) class _a ( snake_case_ ): """simple docstring""" def __init__( self : List[Any] , *UpperCAmelCase : Union[str, Any] , **UpperCAmelCase : int ): warnings.warn( "The class ImageGPTFeatureExtractor is deprecated and will be removed in version 5 of Transformers." " Please use ImageGPTImageProcessor instead." , UpperCAmelCase , ) super().__init__(*UpperCAmelCase , **UpperCAmelCase )
312
import os from typing import Dict, List, Tuple, TypeVar, Union __a :Any = TypeVar('T') __a :Union[str, Any] = Union[List[T], Tuple[T, ...]] __a :List[str] = Union[T, List[T], Dict[str, T]] __a :Any = Union[str, bytes, os.PathLike]
312
1
def __snake_case ( __UpperCamelCase : int ,__UpperCamelCase : int ,__UpperCamelCase : int ): """simple docstring""" A_ = (num_of_terms / 2) * (2 * first_term + (num_of_terms - 1) * common_diff) # formula for sum of series return total def __snake_case ( ): """simple docstring""" print(sum_of_series(1 ,1 ,10 ) ) if __name__ == "__main__": import doctest doctest.testmod()
312
__a :Dict = '0.18.2' from .configuration_utils import ConfigMixin from .utils import ( OptionalDependencyNotAvailable, is_flax_available, is_inflect_available, is_invisible_watermark_available, is_k_diffusion_available, is_k_diffusion_version, is_librosa_available, is_note_seq_available, is_onnx_available, is_scipy_available, is_torch_available, is_torchsde_available, is_transformers_available, is_transformers_version, is_unidecode_available, logging, ) try: if not is_onnx_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from .utils.dummy_onnx_objects import * # noqa F403 else: from .pipelines import OnnxRuntimeModel try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from .utils.dummy_pt_objects import * # noqa F403 else: from .models import ( AutoencoderKL, ControlNetModel, ModelMixin, PriorTransformer, TaFilmDecoder, TransformeraDModel, UNetaDModel, UNetaDConditionModel, UNetaDModel, UNetaDConditionModel, VQModel, ) from .optimization import ( get_constant_schedule, get_constant_schedule_with_warmup, get_cosine_schedule_with_warmup, get_cosine_with_hard_restarts_schedule_with_warmup, get_linear_schedule_with_warmup, get_polynomial_decay_schedule_with_warmup, get_scheduler, ) from .pipelines import ( AudioPipelineOutput, ConsistencyModelPipeline, DanceDiffusionPipeline, DDIMPipeline, DDPMPipeline, DiffusionPipeline, DiTPipeline, ImagePipelineOutput, KarrasVePipeline, LDMPipeline, LDMSuperResolutionPipeline, PNDMPipeline, RePaintPipeline, ScoreSdeVePipeline, ) from .schedulers import ( CMStochasticIterativeScheduler, DDIMInverseScheduler, DDIMParallelScheduler, DDIMScheduler, DDPMParallelScheduler, DDPMScheduler, DEISMultistepScheduler, DPMSolverMultistepInverseScheduler, DPMSolverMultistepScheduler, DPMSolverSinglestepScheduler, EulerAncestralDiscreteScheduler, EulerDiscreteScheduler, HeunDiscreteScheduler, IPNDMScheduler, KarrasVeScheduler, KDPMaAncestralDiscreteScheduler, KDPMaDiscreteScheduler, PNDMScheduler, RePaintScheduler, SchedulerMixin, ScoreSdeVeScheduler, UnCLIPScheduler, UniPCMultistepScheduler, VQDiffusionScheduler, ) from .training_utils import EMAModel try: if not (is_torch_available() and is_scipy_available()): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from .utils.dummy_torch_and_scipy_objects import * # noqa F403 else: from .schedulers import LMSDiscreteScheduler try: if not (is_torch_available() and is_torchsde_available()): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from .utils.dummy_torch_and_torchsde_objects import * # noqa F403 else: from .schedulers import DPMSolverSDEScheduler try: if not (is_torch_available() and is_transformers_available()): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from .utils.dummy_torch_and_transformers_objects import * # noqa F403 else: from .pipelines import ( AltDiffusionImgaImgPipeline, AltDiffusionPipeline, AudioLDMPipeline, CycleDiffusionPipeline, IFImgaImgPipeline, IFImgaImgSuperResolutionPipeline, IFInpaintingPipeline, IFInpaintingSuperResolutionPipeline, IFPipeline, IFSuperResolutionPipeline, ImageTextPipelineOutput, KandinskyImgaImgPipeline, KandinskyInpaintPipeline, KandinskyPipeline, KandinskyPriorPipeline, KandinskyVaaControlnetImgaImgPipeline, KandinskyVaaControlnetPipeline, KandinskyVaaImgaImgPipeline, KandinskyVaaInpaintPipeline, KandinskyVaaPipeline, KandinskyVaaPriorEmbaEmbPipeline, KandinskyVaaPriorPipeline, LDMTextToImagePipeline, PaintByExamplePipeline, SemanticStableDiffusionPipeline, ShapEImgaImgPipeline, ShapEPipeline, StableDiffusionAttendAndExcitePipeline, StableDiffusionControlNetImgaImgPipeline, StableDiffusionControlNetInpaintPipeline, StableDiffusionControlNetPipeline, StableDiffusionDepthaImgPipeline, StableDiffusionDiffEditPipeline, StableDiffusionImageVariationPipeline, StableDiffusionImgaImgPipeline, StableDiffusionInpaintPipeline, StableDiffusionInpaintPipelineLegacy, StableDiffusionInstructPixaPixPipeline, StableDiffusionLatentUpscalePipeline, StableDiffusionLDMaDPipeline, StableDiffusionModelEditingPipeline, StableDiffusionPanoramaPipeline, StableDiffusionParadigmsPipeline, StableDiffusionPipeline, StableDiffusionPipelineSafe, StableDiffusionPixaPixZeroPipeline, StableDiffusionSAGPipeline, StableDiffusionUpscalePipeline, StableUnCLIPImgaImgPipeline, StableUnCLIPPipeline, TextToVideoSDPipeline, TextToVideoZeroPipeline, UnCLIPImageVariationPipeline, UnCLIPPipeline, UniDiffuserModel, UniDiffuserPipeline, UniDiffuserTextDecoder, VersatileDiffusionDualGuidedPipeline, VersatileDiffusionImageVariationPipeline, VersatileDiffusionPipeline, VersatileDiffusionTextToImagePipeline, VideoToVideoSDPipeline, VQDiffusionPipeline, ) try: if not (is_torch_available() and is_transformers_available() and is_invisible_watermark_available()): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from .utils.dummy_torch_and_transformers_and_invisible_watermark_objects import * # noqa F403 else: from .pipelines import StableDiffusionXLImgaImgPipeline, StableDiffusionXLPipeline try: if not (is_torch_available() and is_transformers_available() and is_k_diffusion_available()): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from .utils.dummy_torch_and_transformers_and_k_diffusion_objects import * # noqa F403 else: from .pipelines import StableDiffusionKDiffusionPipeline try: if not (is_torch_available() and is_transformers_available() and is_onnx_available()): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from .utils.dummy_torch_and_transformers_and_onnx_objects import * # noqa F403 else: from .pipelines import ( OnnxStableDiffusionImgaImgPipeline, OnnxStableDiffusionInpaintPipeline, OnnxStableDiffusionInpaintPipelineLegacy, OnnxStableDiffusionPipeline, OnnxStableDiffusionUpscalePipeline, StableDiffusionOnnxPipeline, ) try: if not (is_torch_available() and is_librosa_available()): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from .utils.dummy_torch_and_librosa_objects import * # noqa F403 else: from .pipelines import AudioDiffusionPipeline, Mel try: if not (is_transformers_available() and is_torch_available() and is_note_seq_available()): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from .utils.dummy_transformers_and_torch_and_note_seq_objects import * # noqa F403 else: from .pipelines import SpectrogramDiffusionPipeline try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from .utils.dummy_flax_objects import * # noqa F403 else: from .models.controlnet_flax import FlaxControlNetModel from .models.modeling_flax_utils import FlaxModelMixin from .models.unet_ad_condition_flax import FlaxUNetaDConditionModel from .models.vae_flax import FlaxAutoencoderKL from .pipelines import FlaxDiffusionPipeline from .schedulers import ( FlaxDDIMScheduler, FlaxDDPMScheduler, FlaxDPMSolverMultistepScheduler, FlaxKarrasVeScheduler, FlaxLMSDiscreteScheduler, FlaxPNDMScheduler, FlaxSchedulerMixin, FlaxScoreSdeVeScheduler, ) try: if not (is_flax_available() and is_transformers_available()): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from .utils.dummy_flax_and_transformers_objects import * # noqa F403 else: from .pipelines import ( FlaxStableDiffusionControlNetPipeline, FlaxStableDiffusionImgaImgPipeline, FlaxStableDiffusionInpaintPipeline, FlaxStableDiffusionPipeline, ) try: if not (is_note_seq_available()): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from .utils.dummy_note_seq_objects import * # noqa F403 else: from .pipelines import MidiProcessor
312
1
import os def __snake_case ( ): """simple docstring""" with open(os.path.dirname(__UpperCamelCase ) + "/p022_names.txt" ) as file: A_ = str(file.readlines()[0] ) A_ = names.replace("\"" ,"" ).split("," ) names.sort() A_ = 0 A_ = 0 for i, name in enumerate(__UpperCamelCase ): for letter in name: name_score += ord(__UpperCamelCase ) - 64 total_score += (i + 1) * name_score A_ = 0 return total_score if __name__ == "__main__": print(solution())
312
def __snake_case ( __UpperCamelCase : int = 1000 ): """simple docstring""" return sum(e for e in range(3 ,__UpperCamelCase ) if e % 3 == 0 or e % 5 == 0 ) if __name__ == "__main__": print(F"{solution() = }")
312
1
import unittest from datasets import load_dataset from transformers import BloomTokenizerFast from transformers.testing_utils import require_tokenizers from ...test_tokenization_common import TokenizerTesterMixin @require_tokenizers class _a ( snake_case_ , unittest.TestCase ): """simple docstring""" _lowerCamelCase : Dict = None _lowerCamelCase : List[Any] = BloomTokenizerFast _lowerCamelCase : Dict = BloomTokenizerFast _lowerCamelCase : str = True _lowerCamelCase : Optional[int] = False _lowerCamelCase : Optional[int] = 'tokenizer_file' _lowerCamelCase : Optional[int] = {'bos_token': '<s>', 'eos_token': '</s>', 'unk_token': '<unk>', 'pad_token': '<pad>'} def __A ( self : Dict ): super().setUp() A_ = BloomTokenizerFast.from_pretrained("bigscience/tokenizer" ) tokenizer.save_pretrained(self.tmpdirname ) def __A ( self : Dict , **UpperCAmelCase : int ): kwargs.update(self.special_tokens_map ) return BloomTokenizerFast.from_pretrained(self.tmpdirname , **UpperCAmelCase ) def __A ( self : Any ): A_ = self.get_rust_tokenizer() A_ = ["The quick brown fox</s>", "jumps over the lazy dog</s>"] A_ = [[2175, 23714, 73173, 144252, 2], [77, 132619, 3478, 368, 109586, 35433, 2]] A_ = tokenizer.batch_encode_plus(UpperCAmelCase )["input_ids"] self.assertListEqual(UpperCAmelCase , UpperCAmelCase ) A_ = tokenizer.batch_decode(UpperCAmelCase ) self.assertListEqual(UpperCAmelCase , UpperCAmelCase ) def __A ( self : int , UpperCAmelCase : List[Any]=6 ): for tokenizer, pretrained_name, kwargs in self.tokenizers_list: with self.subTest(f'''{tokenizer.__class__.__name__} ({pretrained_name})''' ): A_ = self.rust_tokenizer_class.from_pretrained(UpperCAmelCase , **UpperCAmelCase ) # tokenizer_r.pad_token = None # Hotfixing padding = None # Simple input A_ = "This is a simple input" A_ = ["This is a simple input 1", "This is a simple input 2"] A_ = ("This is a simple input", "This is a pair") A_ = [ ("This is a simple input 1", "This is a simple input 2"), ("This is a simple pair 1", "This is a simple pair 2"), ] # Simple input tests try: tokenizer_r.encode(UpperCAmelCase , max_length=UpperCAmelCase ) tokenizer_r.encode_plus(UpperCAmelCase , max_length=UpperCAmelCase ) tokenizer_r.batch_encode_plus(UpperCAmelCase , max_length=UpperCAmelCase ) tokenizer_r.encode(UpperCAmelCase , max_length=UpperCAmelCase ) tokenizer_r.batch_encode_plus(UpperCAmelCase , max_length=UpperCAmelCase ) except ValueError: self.fail("Bloom Tokenizer should be able to deal with padding" ) A_ = None # Hotfixing padding = None self.assertRaises(UpperCAmelCase , tokenizer_r.encode , UpperCAmelCase , max_length=UpperCAmelCase , padding="max_length" ) # Simple input self.assertRaises(UpperCAmelCase , tokenizer_r.encode_plus , UpperCAmelCase , max_length=UpperCAmelCase , padding="max_length" ) # Simple input self.assertRaises( UpperCAmelCase , tokenizer_r.batch_encode_plus , UpperCAmelCase , max_length=UpperCAmelCase , padding="max_length" , ) # Pair input self.assertRaises(UpperCAmelCase , tokenizer_r.encode , UpperCAmelCase , max_length=UpperCAmelCase , padding="max_length" ) # Pair input self.assertRaises(UpperCAmelCase , tokenizer_r.encode_plus , UpperCAmelCase , max_length=UpperCAmelCase , padding="max_length" ) # Pair input self.assertRaises( UpperCAmelCase , tokenizer_r.batch_encode_plus , UpperCAmelCase , max_length=UpperCAmelCase , padding="max_length" , ) def __A ( self : Union[str, Any] ): A_ = self.get_rust_tokenizer() A_ = load_dataset("xnli" , "all_languages" , split="test" , streaming=UpperCAmelCase ) A_ = next(iter(UpperCAmelCase ) )["premise"] # pick up one data A_ = list(sample_data.values() ) A_ = list(map(tokenizer.encode , UpperCAmelCase ) ) A_ = [tokenizer.decode(UpperCAmelCase , clean_up_tokenization_spaces=UpperCAmelCase ) for x in output_tokens] self.assertListEqual(UpperCAmelCase , UpperCAmelCase ) def __A ( self : Any ): # The test has to be overriden because BLOOM uses ALiBi positional embeddings that does not have # any sequence length constraints. This test of the parent class will fail since it relies on the # maximum sequence length of the positoonal embeddings. self.assertGreaterEqual(len(self.tokenizer_class.pretrained_vocab_files_map ) , 1 ) self.assertGreaterEqual(len(list(self.tokenizer_class.pretrained_vocab_files_map.values() )[0] ) , 1 )
312
import unittest from typing import Tuple import torch from diffusers.utils import floats_tensor, randn_tensor, torch_all_close, torch_device from diffusers.utils.testing_utils import require_torch @require_torch class _a : """simple docstring""" @property def __A ( self : Union[str, Any] ): return self.get_dummy_input() @property def __A ( self : int ): if self.block_type == "down": return (4, 32, 16, 16) elif self.block_type == "mid": return (4, 32, 32, 32) elif self.block_type == "up": return (4, 32, 64, 64) raise ValueError(f'''\'{self.block_type}\' is not a supported block_type. Set it to \'up\', \'mid\', or \'down\'.''' ) def __A ( self : Union[str, Any] , UpperCAmelCase : List[Any]=True , UpperCAmelCase : str=False , UpperCAmelCase : Tuple=False , UpperCAmelCase : Optional[Any]=False , ): A_ = 4 A_ = 32 A_ = (32, 32) A_ = torch.manual_seed(0 ) A_ = torch.device(UpperCAmelCase ) A_ = (batch_size, num_channels) + sizes A_ = randn_tensor(UpperCAmelCase , generator=UpperCAmelCase , device=UpperCAmelCase ) A_ = {"hidden_states": hidden_states} if include_temb: A_ = 128 A_ = randn_tensor((batch_size, temb_channels) , generator=UpperCAmelCase , device=UpperCAmelCase ) if include_res_hidden_states_tuple: A_ = torch.manual_seed(1 ) A_ = (randn_tensor(UpperCAmelCase , generator=UpperCAmelCase , device=UpperCAmelCase ),) if include_encoder_hidden_states: A_ = floats_tensor((batch_size, 32, 32) ).to(UpperCAmelCase ) if include_skip_sample: A_ = randn_tensor(((batch_size, 3) + sizes) , generator=UpperCAmelCase , device=UpperCAmelCase ) return dummy_input def __A ( self : Optional[int] ): A_ = { "in_channels": 32, "out_channels": 32, "temb_channels": 128, } if self.block_type == "up": A_ = 32 if self.block_type == "mid": init_dict.pop("out_channels" ) A_ = self.dummy_input return init_dict, inputs_dict def __A ( self : List[str] , UpperCAmelCase : Optional[Any] ): A_ , A_ = self.prepare_init_args_and_inputs_for_common() A_ = self.block_class(**UpperCAmelCase ) unet_block.to(UpperCAmelCase ) unet_block.eval() with torch.no_grad(): A_ = unet_block(**UpperCAmelCase ) if isinstance(UpperCAmelCase , UpperCAmelCase ): A_ = output[0] self.assertEqual(output.shape , self.output_shape ) A_ = output[0, -1, -3:, -3:] A_ = torch.tensor(UpperCAmelCase ).to(UpperCAmelCase ) assert torch_all_close(output_slice.flatten() , UpperCAmelCase , atol=5E-3 ) @unittest.skipIf(torch_device == "mps" , "Training is not supported in mps" ) def __A ( self : Union[str, Any] ): A_ , A_ = self.prepare_init_args_and_inputs_for_common() A_ = self.block_class(**UpperCAmelCase ) model.to(UpperCAmelCase ) model.train() A_ = model(**UpperCAmelCase ) if isinstance(UpperCAmelCase , UpperCAmelCase ): A_ = output[0] A_ = torch.device(UpperCAmelCase ) A_ = randn_tensor(output.shape , device=UpperCAmelCase ) A_ = torch.nn.functional.mse_loss(UpperCAmelCase , UpperCAmelCase ) loss.backward()
312
1
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available, is_vision_available __a :int = { 'configuration_mask2former': [ 'MASK2FORMER_PRETRAINED_CONFIG_ARCHIVE_MAP', 'Mask2FormerConfig', ], } try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __a :Union[str, Any] = ['Mask2FormerImageProcessor'] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __a :Optional[Any] = [ 'MASK2FORMER_PRETRAINED_MODEL_ARCHIVE_LIST', 'Mask2FormerForUniversalSegmentation', 'Mask2FormerModel', 'Mask2FormerPreTrainedModel', ] if TYPE_CHECKING: from .configuration_maskaformer import MASK2FORMER_PRETRAINED_CONFIG_ARCHIVE_MAP, MaskaFormerConfig try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .image_processing_maskaformer import MaskaFormerImageProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_maskaformer import ( MASK2FORMER_PRETRAINED_MODEL_ARCHIVE_LIST, MaskaFormerForUniversalSegmentation, MaskaFormerModel, MaskaFormerPreTrainedModel, ) else: import sys __a :Tuple = _LazyModule(__name__, globals()['__file__'], _import_structure)
312
import copy import fnmatch import json import os import pickle as pkl import shutil import sys import tarfile import tempfile from collections import OrderedDict from contextlib import contextmanager from functools import partial from hashlib import shaaaa from io import BytesIO from pathlib import Path from urllib.parse import urlparse from zipfile import ZipFile, is_zipfile import cva import numpy as np import requests import wget from filelock import FileLock from PIL import Image from tqdm.auto import tqdm from yaml import Loader, dump, load try: import torch __a :int = True except ImportError: __a :Optional[Any] = False try: from torch.hub import _get_torch_home __a :Optional[Any] = _get_torch_home() except ImportError: __a :Tuple = os.path.expanduser( os.getenv('TORCH_HOME', os.path.join(os.getenv('XDG_CACHE_HOME', '~/.cache'), 'torch')) ) __a :Optional[Any] = os.path.join(torch_cache_home, 'transformers') __a :int = 'https://cdn.huggingface.co' __a :Any = 'https://s3.amazonaws.com/models.huggingface.co/bert' __a :Optional[Any] = '/'.join(str(Path(__file__).resolve()).split('/')[:-1]) __a :str = os.path.join(PATH, 'config.yaml') __a :str = os.path.join(PATH, 'attributes.txt') __a :Optional[Any] = os.path.join(PATH, 'objects.txt') __a :Optional[int] = os.getenv('PYTORCH_PRETRAINED_BERT_CACHE', default_cache_path) __a :Dict = os.getenv('PYTORCH_TRANSFORMERS_CACHE', PYTORCH_PRETRAINED_BERT_CACHE) __a :List[Any] = os.getenv('TRANSFORMERS_CACHE', PYTORCH_TRANSFORMERS_CACHE) __a :List[str] = 'pytorch_model.bin' __a :Tuple = 'config.yaml' def __snake_case ( __UpperCamelCase : Optional[Any]=OBJECTS ,__UpperCamelCase : List[str]=ATTRIBUTES ): """simple docstring""" A_ = [] with open(__UpperCamelCase ) as f: for object in f.readlines(): vg_classes.append(object.split("," )[0].lower().strip() ) A_ = [] with open(__UpperCamelCase ) as f: for object in f.readlines(): vg_attrs.append(object.split("," )[0].lower().strip() ) return vg_classes, vg_attrs def __snake_case ( __UpperCamelCase : List[Any] ): """simple docstring""" A_ = OrderedDict() with open(__UpperCamelCase ,"rb" ) as f: A_ = pkl.load(__UpperCamelCase )["model"] for k in copy.deepcopy(list(ckp.keys() ) ): A_ = ckp.pop(__UpperCamelCase ) if isinstance(__UpperCamelCase ,np.ndarray ): A_ = torch.tensor(__UpperCamelCase ) else: assert isinstance(__UpperCamelCase ,torch.tensor ), type(__UpperCamelCase ) A_ = v return r class _a : """simple docstring""" _lowerCamelCase : Union[str, Any] = {} def __init__( self : str , UpperCAmelCase : dict , UpperCAmelCase : str = "root" , UpperCAmelCase : List[str]=0 ): A_ = name A_ = level A_ = {} for k, v in dictionary.items(): if v is None: raise ValueError() A_ = copy.deepcopy(UpperCAmelCase ) A_ = copy.deepcopy(UpperCAmelCase ) if isinstance(UpperCAmelCase , UpperCAmelCase ): A_ = Config(UpperCAmelCase , name=UpperCAmelCase , level=level + 1 ) A_ = v setattr(self , UpperCAmelCase , UpperCAmelCase ) A_ = d def __repr__( self : Optional[Any] ): return str(list((self._pointer.keys()) ) ) def __setattr__( self : Any , UpperCAmelCase : Any , UpperCAmelCase : Any ): A_ = val A_ = val A_ = key.split("." ) A_ = len(UpperCAmelCase ) - 1 A_ = self._pointer if len(UpperCAmelCase ) > 1: for i, l in enumerate(UpperCAmelCase ): if hasattr(self , UpperCAmelCase ) and isinstance(getattr(self , UpperCAmelCase ) , UpperCAmelCase ): setattr(getattr(self , UpperCAmelCase ) , ".".join(levels[i:] ) , UpperCAmelCase ) if l == last_level: A_ = val else: A_ = pointer[l] def __A ( self : List[str] ): return self._pointer def __A ( self : int , UpperCAmelCase : Tuple , UpperCAmelCase : int ): with open(f'''{file_name}''' , "w" ) as stream: dump(UpperCAmelCase , UpperCAmelCase ) def __A ( self : List[Any] , UpperCAmelCase : str , UpperCAmelCase : Tuple ): with open(f'''{file_name}''' , "w" ) as stream: json.dump(UpperCAmelCase , UpperCAmelCase ) @staticmethod def __A ( UpperCAmelCase : Optional[int] ): with open(UpperCAmelCase ) as stream: A_ = load(UpperCAmelCase , Loader=UpperCAmelCase ) return data def __str__( self : str ): A_ = " " if self._name != "root": A_ = f'''{t * (self._level-1)}{self._name}:\n''' else: A_ = "" A_ = self._level for i, (k, v) in enumerate(self._pointer.items() ): if isinstance(UpperCAmelCase , UpperCAmelCase ): r += f'''{t * (self._level)}{v}\n''' self._level += 1 else: r += f'''{t * (self._level)}{k}: {v} ({type(UpperCAmelCase ).__name__})\n''' A_ = level return r[:-1] @classmethod def __A ( cls : Optional[Any] , UpperCAmelCase : str , **UpperCAmelCase : str ): A_ , A_ = cls.get_config_dict(UpperCAmelCase , **UpperCAmelCase ) return cls(UpperCAmelCase ) @classmethod def __A ( cls : int , UpperCAmelCase : str , **UpperCAmelCase : int ): A_ = kwargs.pop("cache_dir" , UpperCAmelCase ) A_ = kwargs.pop("force_download" , UpperCAmelCase ) A_ = kwargs.pop("resume_download" , UpperCAmelCase ) A_ = kwargs.pop("proxies" , UpperCAmelCase ) A_ = kwargs.pop("local_files_only" , UpperCAmelCase ) if os.path.isdir(UpperCAmelCase ): A_ = os.path.join(UpperCAmelCase , UpperCAmelCase ) elif os.path.isfile(UpperCAmelCase ) or is_remote_url(UpperCAmelCase ): A_ = pretrained_model_name_or_path else: A_ = hf_bucket_url(UpperCAmelCase , filename=UpperCAmelCase , use_cdn=UpperCAmelCase ) try: # Load from URL or cache if already cached A_ = cached_path( UpperCAmelCase , cache_dir=UpperCAmelCase , force_download=UpperCAmelCase , proxies=UpperCAmelCase , resume_download=UpperCAmelCase , local_files_only=UpperCAmelCase , ) # Load config dict if resolved_config_file is None: raise EnvironmentError A_ = Config.load_yaml(UpperCAmelCase ) except EnvironmentError: A_ = "Can't load config for" raise EnvironmentError(UpperCAmelCase ) if resolved_config_file == config_file: print("loading configuration file from path" ) else: print("loading configuration file cache" ) return Config.load_yaml(UpperCAmelCase ), kwargs def __snake_case ( __UpperCamelCase : Union[str, Any] ): """simple docstring""" A_ = torch.load("dump.pt" ,map_location=in_tensor.device ) A_ = in_tensor.numpy() A_ = out_tensor.numpy()[0] print(na.shape ,na[0, 0, :5] ) print(na.shape ,na[0, 0, :5] ) assert np.allclose(__UpperCamelCase ,__UpperCamelCase ,rtol=0.01 ,atol=0.1 ), ( f'''{sum([1 for x in np.isclose(__UpperCamelCase ,__UpperCamelCase ,rtol=0.01 ,atol=0.1 ).flatten() if x is False] )/len(na.flatten() )*100:.4f} %''' " element-wise mismatch" ) raise Exception("tensors are all good" ) # Hugging face functions below def __snake_case ( __UpperCamelCase : Optional[int] ): """simple docstring""" A_ = urlparse(__UpperCamelCase ) return parsed.scheme in ("http", "https") def __snake_case ( __UpperCamelCase : str ,__UpperCamelCase : str ,__UpperCamelCase : str=True ): """simple docstring""" A_ = CLOUDFRONT_DISTRIB_PREFIX if use_cdn else S3_BUCKET_PREFIX A_ = "/" not in model_id if legacy_format: return f'''{endpoint}/{model_id}-{filename}''' else: return f'''{endpoint}/{model_id}/{filename}''' def __snake_case ( __UpperCamelCase : List[str] ,__UpperCamelCase : Union[str, Any] ,__UpperCamelCase : List[str]=None ,__UpperCamelCase : int=0 ,__UpperCamelCase : int=None ,): """simple docstring""" A_ = "python/{}".format(sys.version.split()[0] ) if _torch_available: ua += "; torch/{}".format(torch.__version__ ) if isinstance(__UpperCamelCase ,__UpperCamelCase ): ua += "; " + "; ".join("{}/{}".format(__UpperCamelCase ,__UpperCamelCase ) for k, v in user_agent.items() ) elif isinstance(__UpperCamelCase ,__UpperCamelCase ): ua += "; " + user_agent A_ = {"user-agent": ua} if resume_size > 0: A_ = "bytes=%d-" % (resume_size,) A_ = requests.get(__UpperCamelCase ,stream=__UpperCamelCase ,proxies=__UpperCamelCase ,headers=__UpperCamelCase ) if response.status_code == 416: # Range not satisfiable return A_ = response.headers.get("Content-Length" ) A_ = resume_size + int(__UpperCamelCase ) if content_length is not None else None A_ = tqdm( unit="B" ,unit_scale=__UpperCamelCase ,total=__UpperCamelCase ,initial=__UpperCamelCase ,desc="Downloading" ,) for chunk in response.iter_content(chunk_size=1024 ): if chunk: # filter out keep-alive new chunks progress.update(len(__UpperCamelCase ) ) temp_file.write(__UpperCamelCase ) progress.close() def __snake_case ( __UpperCamelCase : str ,__UpperCamelCase : Any=None ,__UpperCamelCase : Dict=False ,__UpperCamelCase : Union[str, Any]=None ,__UpperCamelCase : Any=10 ,__UpperCamelCase : int=False ,__UpperCamelCase : Optional[Any]=None ,__UpperCamelCase : str=False ,): """simple docstring""" if cache_dir is None: A_ = TRANSFORMERS_CACHE if isinstance(__UpperCamelCase ,__UpperCamelCase ): A_ = str(__UpperCamelCase ) os.makedirs(__UpperCamelCase ,exist_ok=__UpperCamelCase ) A_ = None if not local_files_only: try: A_ = requests.head(__UpperCamelCase ,allow_redirects=__UpperCamelCase ,proxies=__UpperCamelCase ,timeout=__UpperCamelCase ) if response.status_code == 200: A_ = response.headers.get("ETag" ) except (EnvironmentError, requests.exceptions.Timeout): # etag is already None pass A_ = url_to_filename(__UpperCamelCase ,__UpperCamelCase ) # get cache path to put the file A_ = os.path.join(__UpperCamelCase ,__UpperCamelCase ) # etag is None = we don't have a connection, or url doesn't exist, or is otherwise inaccessible. # try to get the last downloaded one if etag is None: if os.path.exists(__UpperCamelCase ): return cache_path else: A_ = [ file for file in fnmatch.filter(os.listdir(__UpperCamelCase ) ,filename + ".*" ) if not file.endswith(".json" ) and not file.endswith(".lock" ) ] if len(__UpperCamelCase ) > 0: return os.path.join(__UpperCamelCase ,matching_files[-1] ) else: # If files cannot be found and local_files_only=True, # the models might've been found if local_files_only=False # Notify the user about that if local_files_only: raise ValueError( "Cannot find the requested files in the cached path and outgoing traffic has been" " disabled. To enable model look-ups and downloads online, set 'local_files_only'" " to False." ) return None # From now on, etag is not None. if os.path.exists(__UpperCamelCase ) and not force_download: return cache_path # Prevent parallel downloads of the same file with a lock. A_ = cache_path + ".lock" with FileLock(__UpperCamelCase ): # If the download just completed while the lock was activated. if os.path.exists(__UpperCamelCase ) and not force_download: # Even if returning early like here, the lock will be released. return cache_path if resume_download: A_ = cache_path + ".incomplete" @contextmanager def _resumable_file_manager(): with open(__UpperCamelCase ,"a+b" ) as f: yield f A_ = _resumable_file_manager if os.path.exists(__UpperCamelCase ): A_ = os.stat(__UpperCamelCase ).st_size else: A_ = 0 else: A_ = partial(tempfile.NamedTemporaryFile ,dir=__UpperCamelCase ,delete=__UpperCamelCase ) A_ = 0 # Download to temporary file, then copy to cache dir once finished. # Otherwise you get corrupt cache entries if the download gets interrupted. with temp_file_manager() as temp_file: print( "%s not found in cache or force_download set to True, downloading to %s" ,__UpperCamelCase ,temp_file.name ,) http_get( __UpperCamelCase ,__UpperCamelCase ,proxies=__UpperCamelCase ,resume_size=__UpperCamelCase ,user_agent=__UpperCamelCase ,) os.replace(temp_file.name ,__UpperCamelCase ) A_ = {"url": url, "etag": etag} A_ = cache_path + ".json" with open(__UpperCamelCase ,"w" ) as meta_file: json.dump(__UpperCamelCase ,__UpperCamelCase ) return cache_path def __snake_case ( __UpperCamelCase : List[Any] ,__UpperCamelCase : str=None ): """simple docstring""" A_ = url.encode("utf-8" ) A_ = shaaaa(__UpperCamelCase ) A_ = url_hash.hexdigest() if etag: A_ = etag.encode("utf-8" ) A_ = shaaaa(__UpperCamelCase ) filename += "." + etag_hash.hexdigest() if url.endswith(".h5" ): filename += ".h5" return filename def __snake_case ( __UpperCamelCase : Union[str, Any] ,__UpperCamelCase : Union[str, Any]=None ,__UpperCamelCase : List[Any]=False ,__UpperCamelCase : List[str]=None ,__UpperCamelCase : Any=False ,__UpperCamelCase : Optional[int]=None ,__UpperCamelCase : Optional[Any]=False ,__UpperCamelCase : Dict=False ,__UpperCamelCase : Optional[Any]=False ,): """simple docstring""" if cache_dir is None: A_ = TRANSFORMERS_CACHE if isinstance(__UpperCamelCase ,__UpperCamelCase ): A_ = str(__UpperCamelCase ) if isinstance(__UpperCamelCase ,__UpperCamelCase ): A_ = str(__UpperCamelCase ) if is_remote_url(__UpperCamelCase ): # URL, so get it from the cache (downloading if necessary) A_ = get_from_cache( __UpperCamelCase ,cache_dir=__UpperCamelCase ,force_download=__UpperCamelCase ,proxies=__UpperCamelCase ,resume_download=__UpperCamelCase ,user_agent=__UpperCamelCase ,local_files_only=__UpperCamelCase ,) elif os.path.exists(__UpperCamelCase ): # File, and it exists. A_ = url_or_filename elif urlparse(__UpperCamelCase ).scheme == "": # File, but it doesn't exist. raise EnvironmentError("file {} not found".format(__UpperCamelCase ) ) else: # Something unknown raise ValueError("unable to parse {} as a URL or as a local path".format(__UpperCamelCase ) ) if extract_compressed_file: if not is_zipfile(__UpperCamelCase ) and not tarfile.is_tarfile(__UpperCamelCase ): return output_path # Path where we extract compressed archives # We avoid '.' in dir name and add "-extracted" at the end: "./model.zip" => "./model-zip-extracted/" A_ , A_ = os.path.split(__UpperCamelCase ) A_ = output_file.replace("." ,"-" ) + "-extracted" A_ = os.path.join(__UpperCamelCase ,__UpperCamelCase ) if os.path.isdir(__UpperCamelCase ) and os.listdir(__UpperCamelCase ) and not force_extract: return output_path_extracted # Prevent parallel extractions A_ = output_path + ".lock" with FileLock(__UpperCamelCase ): shutil.rmtree(__UpperCamelCase ,ignore_errors=__UpperCamelCase ) os.makedirs(__UpperCamelCase ) if is_zipfile(__UpperCamelCase ): with ZipFile(__UpperCamelCase ,"r" ) as zip_file: zip_file.extractall(__UpperCamelCase ) zip_file.close() elif tarfile.is_tarfile(__UpperCamelCase ): A_ = tarfile.open(__UpperCamelCase ) tar_file.extractall(__UpperCamelCase ) tar_file.close() else: raise EnvironmentError("Archive format of {} could not be identified".format(__UpperCamelCase ) ) return output_path_extracted return output_path def __snake_case ( __UpperCamelCase : str ,__UpperCamelCase : Any="," ): """simple docstring""" assert isinstance(__UpperCamelCase ,__UpperCamelCase ) if os.path.isfile(__UpperCamelCase ): with open(__UpperCamelCase ) as f: A_ = eval(f.read() ) else: A_ = requests.get(__UpperCamelCase ) try: A_ = requests.json() except Exception: A_ = req.content.decode() assert data is not None, "could not connect" try: A_ = eval(__UpperCamelCase ) except Exception: A_ = data.split("\n" ) req.close() return data def __snake_case ( __UpperCamelCase : int ): """simple docstring""" A_ = requests.get(__UpperCamelCase ) A_ = np.array(Image.open(BytesIO(response.content ) ) ) return img def __snake_case ( __UpperCamelCase : Tuple ): """simple docstring""" A_ = url.split("/" )[-1] if fn not in os.listdir(os.getcwd() ): wget.download(__UpperCamelCase ) with open(__UpperCamelCase ,"rb" ) as stream: A_ = pkl.load(__UpperCamelCase ) A_ = weights.pop("model" ) A_ = {} for k, v in model.items(): A_ = torch.from_numpy(__UpperCamelCase ) if "running_var" in k: A_ = torch.tensor([0] ) A_ = k.replace("running_var" ,"num_batches_tracked" ) A_ = zero return new def __snake_case ( ): """simple docstring""" print(f'''{os.path.abspath(os.path.join(__UpperCamelCase ,os.pardir ) )}/demo.ipynb''' ) def __snake_case ( __UpperCamelCase : Optional[Any] ,__UpperCamelCase : Optional[int]="RGB" ): """simple docstring""" assert isinstance(__UpperCamelCase ,__UpperCamelCase ) if os.path.isfile(__UpperCamelCase ): A_ = cva.imread(__UpperCamelCase ) else: A_ = get_image_from_url(__UpperCamelCase ) assert img is not None, f'''could not connect to: {im}''' A_ = cva.cvtColor(__UpperCamelCase ,cva.COLOR_BGR2RGB ) if input_format == "RGB": A_ = img[:, :, ::-1] return img def __snake_case ( __UpperCamelCase : List[str] ,__UpperCamelCase : List[str]=1 ): """simple docstring""" return (images[i : i + batch] for i in range(0 ,len(__UpperCamelCase ) ,__UpperCamelCase ))
312
1
from collections.abc import Sequence def __snake_case ( __UpperCamelCase : Sequence[int] | None = None ): """simple docstring""" if nums is None or not nums: raise ValueError("Input sequence should not be empty" ) A_ = nums[0] for i in range(1 ,len(__UpperCamelCase ) ): A_ = nums[i] A_ = max(__UpperCamelCase ,ans + num ,__UpperCamelCase ) return ans if __name__ == "__main__": import doctest doctest.testmod() # Try on a sample input from the user __a :Union[str, Any] = int(input('Enter number of elements : ').strip()) __a :Dict = list(map(int, input('\nEnter the numbers : ').strip().split()))[:n] print(max_subsequence_sum(array))
312
from __future__ import annotations def __snake_case ( __UpperCamelCase : list[list[int]] ): """simple docstring""" for i in range(1 ,len(matrix[0] ) ): matrix[0][i] += matrix[0][i - 1] # preprocessing the first column for i in range(1 ,len(__UpperCamelCase ) ): matrix[i][0] += matrix[i - 1][0] # updating the path cost for current position for i in range(1 ,len(__UpperCamelCase ) ): for j in range(1 ,len(matrix[0] ) ): matrix[i][j] += min(matrix[i - 1][j] ,matrix[i][j - 1] ) return matrix[-1][-1] if __name__ == "__main__": import doctest doctest.testmod()
312
1
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 _a ( snake_case_ , unittest.TestCase ): """simple docstring""" _lowerCamelCase : Tuple = DiTPipeline _lowerCamelCase : Dict = CLASS_CONDITIONED_IMAGE_GENERATION_PARAMS _lowerCamelCase : Tuple = PipelineTesterMixin.required_optional_params - { 'latents', 'num_images_per_prompt', 'callback', 'callback_steps', } _lowerCamelCase : List[str] = CLASS_CONDITIONED_IMAGE_GENERATION_BATCH_PARAMS _lowerCamelCase : Tuple = False def __A ( self : Any ): torch.manual_seed(0 ) A_ = TransformeraDModel( sample_size=16 , num_layers=2 , patch_size=4 , attention_head_dim=8 , num_attention_heads=2 , in_channels=4 , out_channels=8 , attention_bias=UpperCAmelCase , activation_fn="gelu-approximate" , num_embeds_ada_norm=1000 , norm_type="ada_norm_zero" , norm_elementwise_affine=UpperCAmelCase , ) A_ = AutoencoderKL() A_ = DDIMScheduler() A_ = {"transformer": transformer.eval(), "vae": vae.eval(), "scheduler": scheduler} return components def __A ( self : Optional[int] , UpperCAmelCase : str , UpperCAmelCase : Any=0 ): if str(UpperCAmelCase ).startswith("mps" ): A_ = torch.manual_seed(UpperCAmelCase ) else: A_ = torch.Generator(device=UpperCAmelCase ).manual_seed(UpperCAmelCase ) A_ = { "class_labels": [1], "generator": generator, "num_inference_steps": 2, "output_type": "numpy", } return inputs def __A ( self : Union[str, Any] ): A_ = "cpu" A_ = self.get_dummy_components() A_ = self.pipeline_class(**UpperCAmelCase ) pipe.to(UpperCAmelCase ) pipe.set_progress_bar_config(disable=UpperCAmelCase ) A_ = self.get_dummy_inputs(UpperCAmelCase ) A_ = pipe(**UpperCAmelCase ).images A_ = image[0, -3:, -3:, -1] self.assertEqual(image.shape , (1, 16, 16, 3) ) A_ = 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] ) A_ = np.abs(image_slice.flatten() - expected_slice ).max() self.assertLessEqual(UpperCAmelCase , 1E-3 ) def __A ( self : Optional[Any] ): self._test_inference_batch_single_identical(relax_max_difference=UpperCAmelCase , expected_max_diff=1E-3 ) @unittest.skipIf( torch_device != "cuda" or not is_xformers_available() , reason="XFormers attention is only available with CUDA and `xformers` installed" , ) def __A ( self : Union[str, Any] ): self._test_xformers_attention_forwardGenerator_pass(expected_max_diff=1E-3 ) @require_torch_gpu @slow class _a ( unittest.TestCase ): """simple docstring""" def __A ( self : Tuple ): super().tearDown() gc.collect() torch.cuda.empty_cache() def __A ( self : str ): A_ = torch.manual_seed(0 ) A_ = DiTPipeline.from_pretrained("facebook/DiT-XL-2-256" ) pipe.to("cuda" ) A_ = ["vase", "umbrella", "white shark", "white wolf"] A_ = pipe.get_label_ids(UpperCAmelCase ) A_ = pipe(UpperCAmelCase , generator=UpperCAmelCase , num_inference_steps=40 , output_type="np" ).images for word, image in zip(UpperCAmelCase , UpperCAmelCase ): A_ = load_numpy( f'''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/dit/{word}.npy''' ) assert np.abs((expected_image - image).max() ) < 1E-2 def __A ( self : Tuple ): A_ = DiTPipeline.from_pretrained("facebook/DiT-XL-2-512" ) A_ = DPMSolverMultistepScheduler.from_config(pipe.scheduler.config ) pipe.to("cuda" ) A_ = ["vase", "umbrella"] A_ = pipe.get_label_ids(UpperCAmelCase ) A_ = torch.manual_seed(0 ) A_ = pipe(UpperCAmelCase , generator=UpperCAmelCase , num_inference_steps=25 , output_type="np" ).images for word, image in zip(UpperCAmelCase , UpperCAmelCase ): A_ = 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
312
from typing import Dict from transformers import EvalPrediction, HfArgumentParser, TrainingArguments, is_torch_available from transformers.testing_utils import ( TestCasePlus, execute_subprocess_async, get_torch_dist_unique_port, require_torch_multi_gpu, require_torch_neuroncore, ) from transformers.training_args import ParallelMode from transformers.utils import logging __a :int = logging.get_logger(__name__) if is_torch_available(): import torch from torch import nn from torch.utils.data import Dataset from transformers import Trainer class _a ( snake_case_ ): """simple docstring""" def __init__( self : Tuple , UpperCAmelCase : int = 101 ): A_ = length def __len__( self : int ): return self.length def __getitem__( self : Optional[int] , UpperCAmelCase : Optional[int] ): return i class _a : """simple docstring""" def __call__( self : Any , UpperCAmelCase : Optional[Any] ): return {"input_ids": torch.tensor(UpperCAmelCase ), "labels": torch.tensor(UpperCAmelCase )} class _a ( nn.Module ): """simple docstring""" def __init__( self : int ): super().__init__() # Add some (unused) params otherwise DDP will complain. A_ = nn.Linear(120 , 80 ) def __A ( self : Tuple , UpperCAmelCase : Dict , UpperCAmelCase : Tuple=None ): if labels is not None: return torch.tensor(0.0 , device=input_ids.device ), input_ids else: return input_ids class _a ( snake_case_ ): """simple docstring""" @require_torch_neuroncore def __A ( self : List[str] ): A_ = f'''--nproc_per_node=2 --master_port={get_torch_dist_unique_port()} {self.test_file_dir}/test_trainer_distributed.py '''.split() A_ = self.get_auto_remove_tmp_dir() A_ = f'''--output_dir {output_dir}'''.split() A_ = ["torchrun"] + distributed_args + args execute_subprocess_async(UpperCAmelCase , env=self.get_env() ) # successful return here == success - any errors would have caused an error in the sub-call class _a ( snake_case_ ): """simple docstring""" @require_torch_multi_gpu def __A ( self : List[str] ): A_ = f'''--nproc_per_node={torch.cuda.device_count()} --master_port={get_torch_dist_unique_port()} {self.test_file_dir}/test_trainer_distributed.py '''.split() A_ = self.get_auto_remove_tmp_dir() A_ = f'''--output_dir {output_dir}'''.split() A_ = ["torchrun"] + distributed_args + args execute_subprocess_async(UpperCAmelCase , env=self.get_env() ) # successful return here == success - any errors would have caused an error in the sub-call if __name__ == "__main__": # The script below is meant to be run under torch.distributed, on a machine with multiple GPUs: # # PYTHONPATH="src" python -m torch.distributed.run --nproc_per_node 2 --output_dir output_dir ./tests/test_trainer_distributed.py __a :Union[str, Any] = HfArgumentParser((TrainingArguments,)) __a :Tuple = parser.parse_args_into_dataclasses()[0] logger.warning( F"Process rank: {training_args.local_rank}, device: {training_args.device}, n_gpu: {training_args.n_gpu}, " F"distributed training: {training_args.parallel_mode != ParallelMode.NOT_DISTRIBUTED}" ) # Essentially, what we want to verify in the distributed case is that we get all samples back, # in the right order. (this is crucial for prediction for instance) for dataset_length in [101, 40, 7]: __a :int = DummyDataset(dataset_length) def __snake_case ( __UpperCamelCase : EvalPrediction ): """simple docstring""" A_ = list(range(len(__UpperCamelCase ) ) ) A_ = p.predictions.tolist() == sequential and p.label_ids.tolist() == sequential if not success and training_args.local_rank == 0: logger.warning( "Predictions and/or labels do not match expected results:\n - predictions: " f'''{p.predictions.tolist()}\n - labels: {p.label_ids.tolist()}\n - expected: {sequential}''' ) return {"success": success} __a :str = Trainer( model=DummyModel(), args=training_args, data_collator=DummyDataCollator(), eval_dataset=dataset, compute_metrics=compute_metrics, ) __a :str = trainer.evaluate() logger.info(metrics) if metrics["eval_success"] is not True: logger.error(metrics) exit(1) __a :str = trainer.predict(dataset) logger.info(p.metrics) if p.metrics["test_success"] is not True: logger.error(p.metrics) exit(1) __a :Optional[int] = 2 __a :List[Any] = trainer.evaluate() logger.info(metrics) if metrics["eval_success"] is not True: logger.error(metrics) exit(1) __a :str = trainer.predict(dataset) logger.info(p.metrics) if p.metrics["test_success"] is not True: logger.error(p.metrics) exit(1) __a :Union[str, Any] = None
312
1
import argparse import json from dataclasses import dataclass, field from functools import partial from pathlib import Path from typing import List import timm import torch import torch.nn as nn from huggingface_hub import hf_hub_download from torch import Tensor from transformers import AutoImageProcessor, ResNetConfig, ResNetForImageClassification from transformers.utils import logging logging.set_verbosity_info() __a :List[Any] = logging.get_logger() @dataclass class _a : """simple docstring""" _lowerCamelCase : nn.Module _lowerCamelCase : List[nn.Module] = field(default_factory=snake_case_ ) _lowerCamelCase : list = field(default_factory=snake_case_ ) def __A ( self : List[Any] , UpperCAmelCase : Tuple , UpperCAmelCase : Tensor , UpperCAmelCase : Tensor ): A_ = len(list(m.modules() ) ) == 1 or isinstance(UpperCAmelCase , nn.Convad ) or isinstance(UpperCAmelCase , nn.BatchNormad ) if has_not_submodules: self.traced.append(UpperCAmelCase ) def __call__( self : Optional[int] , UpperCAmelCase : Tensor ): for m in self.module.modules(): self.handles.append(m.register_forward_hook(self._forward_hook ) ) self.module(UpperCAmelCase ) [x.remove() for x in self.handles] return self @property def __A ( self : List[Any] ): # check the len of the state_dict keys to see if we have learnable params return list(filter(lambda UpperCAmelCase : len(list(x.state_dict().keys() ) ) > 0 , self.traced ) ) @dataclass class _a : """simple docstring""" _lowerCamelCase : nn.Module _lowerCamelCase : nn.Module _lowerCamelCase : int = 0 _lowerCamelCase : List = field(default_factory=snake_case_ ) _lowerCamelCase : List = field(default_factory=snake_case_ ) def __call__( self : str , UpperCAmelCase : Tensor ): A_ = Tracker(self.dest )(UpperCAmelCase ).parametrized A_ = Tracker(self.src )(UpperCAmelCase ).parametrized A_ = list(filter(lambda UpperCAmelCase : type(UpperCAmelCase ) not in self.src_skip , UpperCAmelCase ) ) A_ = list(filter(lambda UpperCAmelCase : type(UpperCAmelCase ) not in self.dest_skip , UpperCAmelCase ) ) if len(UpperCAmelCase ) != len(UpperCAmelCase ): raise Exception( f'''Numbers of operations are different. Source module has {len(UpperCAmelCase )} operations while''' f''' destination module has {len(UpperCAmelCase )}.''' ) for dest_m, src_m in zip(UpperCAmelCase , UpperCAmelCase ): dest_m.load_state_dict(src_m.state_dict() ) if self.verbose == 1: print(f'''Transfered from={src_m} to={dest_m}''' ) def __snake_case ( __UpperCamelCase : str ,__UpperCamelCase : ResNetConfig ,__UpperCamelCase : Path ,__UpperCamelCase : bool = True ): """simple docstring""" print(f'''Converting {name}...''' ) with torch.no_grad(): A_ = timm.create_model(__UpperCamelCase ,pretrained=__UpperCamelCase ).eval() A_ = ResNetForImageClassification(__UpperCamelCase ).eval() A_ = ModuleTransfer(src=__UpperCamelCase ,dest=__UpperCamelCase ) A_ = torch.randn((1, 3, 224, 224) ) module_transfer(__UpperCamelCase ) assert torch.allclose(from_model(__UpperCamelCase ) ,our_model(__UpperCamelCase ).logits ), "The model logits don't match the original one." A_ = f'''resnet{"-".join(name.split("resnet" ) )}''' print(__UpperCamelCase ) if push_to_hub: our_model.push_to_hub( repo_path_or_name=save_directory / checkpoint_name ,commit_message="Add model" ,use_temp_dir=__UpperCamelCase ,) # we can use the convnext one A_ = AutoImageProcessor.from_pretrained("facebook/convnext-base-224-22k-1k" ) image_processor.push_to_hub( repo_path_or_name=save_directory / checkpoint_name ,commit_message="Add image processor" ,use_temp_dir=__UpperCamelCase ,) print(f'''Pushed {checkpoint_name}''' ) def __snake_case ( __UpperCamelCase : Path ,__UpperCamelCase : str = None ,__UpperCamelCase : bool = True ): """simple docstring""" A_ = "imagenet-1k-id2label.json" A_ = 1000 A_ = (1, num_labels) A_ = "huggingface/label-files" A_ = num_labels A_ = json.load(open(hf_hub_download(__UpperCamelCase ,__UpperCamelCase ,repo_type="dataset" ) ,"r" ) ) A_ = {int(__UpperCamelCase ): v for k, v in idalabel.items()} A_ = idalabel A_ = {v: k for k, v in idalabel.items()} A_ = partial(__UpperCamelCase ,num_labels=__UpperCamelCase ,idalabel=__UpperCamelCase ,labelaid=__UpperCamelCase ) A_ = { "resnet18": ImageNetPreTrainedConfig( depths=[2, 2, 2, 2] ,hidden_sizes=[64, 128, 256, 512] ,layer_type="basic" ), "resnet26": ImageNetPreTrainedConfig( depths=[2, 2, 2, 2] ,hidden_sizes=[256, 512, 1024, 2048] ,layer_type="bottleneck" ), "resnet34": ImageNetPreTrainedConfig( depths=[3, 4, 6, 3] ,hidden_sizes=[64, 128, 256, 512] ,layer_type="basic" ), "resnet50": ImageNetPreTrainedConfig( depths=[3, 4, 6, 3] ,hidden_sizes=[256, 512, 1024, 2048] ,layer_type="bottleneck" ), "resnet101": ImageNetPreTrainedConfig( depths=[3, 4, 23, 3] ,hidden_sizes=[256, 512, 1024, 2048] ,layer_type="bottleneck" ), "resnet152": ImageNetPreTrainedConfig( depths=[3, 8, 36, 3] ,hidden_sizes=[256, 512, 1024, 2048] ,layer_type="bottleneck" ), } if model_name: convert_weight_and_push(__UpperCamelCase ,names_to_config[model_name] ,__UpperCamelCase ,__UpperCamelCase ) else: for model_name, config in names_to_config.items(): convert_weight_and_push(__UpperCamelCase ,__UpperCamelCase ,__UpperCamelCase ,__UpperCamelCase ) return config, expected_shape if __name__ == "__main__": __a :int = argparse.ArgumentParser() # Required parameters parser.add_argument( '--model_name', default=None, type=str, help=( 'The name of the model you wish to convert, it must be one of the supported resnet* architecture,' ' currently: resnet18,26,34,50,101,152. If `None`, all of them will the converted.' ), ) parser.add_argument( '--pytorch_dump_folder_path', default=None, type=Path, required=True, help='Path to the output PyTorch model directory.', ) parser.add_argument( '--push_to_hub', default=True, type=bool, required=False, help='If True, push model and image processor to the hub.', ) __a :str = parser.parse_args() __a :Path = args.pytorch_dump_folder_path pytorch_dump_folder_path.mkdir(exist_ok=True, parents=True) convert_weights_and_push(pytorch_dump_folder_path, args.model_name, args.push_to_hub)
312
import argparse import json from pathlib import Path import requests import torch from huggingface_hub import hf_hub_download from PIL import Image from timm import create_model from timm.data import resolve_data_config from timm.data.transforms_factory import create_transform from transformers import BitConfig, BitForImageClassification, BitImageProcessor from transformers.image_utils import PILImageResampling from transformers.utils import logging logging.set_verbosity_info() __a :Any = logging.get_logger(__name__) def __snake_case ( __UpperCamelCase : Optional[int] ): """simple docstring""" A_ = "huggingface/label-files" A_ = "imagenet-1k-id2label.json" A_ = json.load(open(hf_hub_download(__UpperCamelCase ,__UpperCamelCase ,repo_type="dataset" ) ,"r" ) ) A_ = {int(__UpperCamelCase ): v for k, v in idalabel.items()} A_ = {v: k for k, v in idalabel.items()} A_ = "std_conv" if "bit" in model_name else False # note that when using BiT as backbone for ViT-hybrid checkpoints, # one needs to additionally set config.layer_type = "bottleneck", config.stem_type = "same", # config.conv_layer = "std_conv_same" A_ = BitConfig( conv_layer=__UpperCamelCase ,num_labels=1000 ,idalabel=__UpperCamelCase ,labelaid=__UpperCamelCase ,) return config def __snake_case ( __UpperCamelCase : Union[str, Any] ): """simple docstring""" if "stem.conv" in name: A_ = name.replace("stem.conv" ,"bit.embedder.convolution" ) if "blocks" in name: A_ = name.replace("blocks" ,"layers" ) if "head.fc" in name: A_ = name.replace("head.fc" ,"classifier.1" ) if name.startswith("norm" ): A_ = "bit." + name if "bit" not in name and "classifier" not in name: A_ = "bit.encoder." + name return name def __snake_case ( ): """simple docstring""" A_ = "http://images.cocodataset.org/val2017/000000039769.jpg" A_ = Image.open(requests.get(__UpperCamelCase ,stream=__UpperCamelCase ).raw ) return im @torch.no_grad() def __snake_case ( __UpperCamelCase : Any ,__UpperCamelCase : Optional[Any] ,__UpperCamelCase : Tuple=False ): """simple docstring""" A_ = get_config(__UpperCamelCase ) # load original model from timm A_ = create_model(__UpperCamelCase ,pretrained=__UpperCamelCase ) timm_model.eval() # load state_dict of original model A_ = timm_model.state_dict() for key in state_dict.copy().keys(): A_ = state_dict.pop(__UpperCamelCase ) A_ = val.squeeze() if "head" in key else val # load HuggingFace model A_ = BitForImageClassification(__UpperCamelCase ) model.eval() model.load_state_dict(__UpperCamelCase ) # create image processor A_ = create_transform(**resolve_data_config({} ,model=__UpperCamelCase ) ) A_ = transform.transforms A_ = { "bilinear": PILImageResampling.BILINEAR, "bicubic": PILImageResampling.BICUBIC, "nearest": PILImageResampling.NEAREST, } A_ = BitImageProcessor( do_resize=__UpperCamelCase ,size={"shortest_edge": timm_transforms[0].size} ,resample=pillow_resamplings[timm_transforms[0].interpolation.value] ,do_center_crop=__UpperCamelCase ,crop_size={"height": timm_transforms[1].size[0], "width": timm_transforms[1].size[1]} ,do_normalize=__UpperCamelCase ,image_mean=timm_transforms[-1].mean.tolist() ,image_std=timm_transforms[-1].std.tolist() ,) A_ = prepare_img() A_ = transform(__UpperCamelCase ).unsqueeze(0 ) A_ = processor(__UpperCamelCase ,return_tensors="pt" ).pixel_values # verify pixel values assert torch.allclose(__UpperCamelCase ,__UpperCamelCase ) # verify logits with torch.no_grad(): A_ = model(__UpperCamelCase ) A_ = outputs.logits print("Logits:" ,logits[0, :3] ) print("Predicted class:" ,model.config.idalabel[logits.argmax(-1 ).item()] ) A_ = timm_model(__UpperCamelCase ) assert timm_logits.shape == outputs.logits.shape assert torch.allclose(__UpperCamelCase ,outputs.logits ,atol=1E-3 ) print("Looks ok!" ) if pytorch_dump_folder_path is not None: Path(__UpperCamelCase ).mkdir(exist_ok=__UpperCamelCase ) print(f'''Saving model {model_name} and processor to {pytorch_dump_folder_path}''' ) model.save_pretrained(__UpperCamelCase ) processor.save_pretrained(__UpperCamelCase ) if push_to_hub: print(f'''Pushing model {model_name} and processor to the hub''' ) model.push_to_hub(f'''ybelkada/{model_name}''' ) processor.push_to_hub(f'''ybelkada/{model_name}''' ) if __name__ == "__main__": __a :List[Any] = argparse.ArgumentParser() # Required parameters parser.add_argument( '--model_name', default='resnetv2_50x1_bitm', type=str, help='Name of the BiT timm model you\'d like to convert.', ) parser.add_argument( '--pytorch_dump_folder_path', default=None, type=str, help='Path to the output PyTorch model directory.' ) parser.add_argument( '--push_to_hub', action='store_true', help='Whether to push the model to the hub.', ) __a :str = parser.parse_args() convert_bit_checkpoint(args.model_name, args.pytorch_dump_folder_path, args.push_to_hub)
312
1
from ...configuration_utils import PretrainedConfig from ...utils import logging from ...utils.backbone_utils import BackboneConfigMixin, get_aligned_output_features_output_indices __a :List[str] = logging.get_logger(__name__) __a :int = { 'facebook/convnextv2-tiny-1k-224': 'https://huggingface.co/facebook/convnextv2-tiny-1k-224/resolve/main/config.json', } class _a ( snake_case_ , snake_case_ ): """simple docstring""" _lowerCamelCase : Optional[int] = 'convnextv2' def __init__( self : Optional[Any] , UpperCAmelCase : str=3 , UpperCAmelCase : Union[str, Any]=4 , UpperCAmelCase : Union[str, Any]=4 , UpperCAmelCase : Any=None , UpperCAmelCase : Optional[int]=None , UpperCAmelCase : int="gelu" , UpperCAmelCase : Any=0.02 , UpperCAmelCase : Tuple=1E-12 , UpperCAmelCase : str=0.0 , UpperCAmelCase : Any=224 , UpperCAmelCase : str=None , UpperCAmelCase : Union[str, Any]=None , **UpperCAmelCase : Optional[int] , ): super().__init__(**UpperCAmelCase ) A_ = num_channels A_ = patch_size A_ = num_stages A_ = [96, 192, 384, 768] if hidden_sizes is None else hidden_sizes A_ = [3, 3, 9, 3] if depths is None else depths A_ = hidden_act A_ = initializer_range A_ = layer_norm_eps A_ = drop_path_rate A_ = image_size A_ = ["stem"] + [f'''stage{idx}''' for idx in range(1 , len(self.depths ) + 1 )] A_ , A_ = get_aligned_output_features_output_indices( out_features=UpperCAmelCase , out_indices=UpperCAmelCase , stage_names=self.stage_names )
312
import os import re import sys import traceback import warnings from pathlib import Path from typing import Dict, Optional, Union from uuid import uuida from huggingface_hub import HfFolder, ModelCard, ModelCardData, hf_hub_download, whoami from huggingface_hub.file_download import REGEX_COMMIT_HASH from huggingface_hub.utils import ( EntryNotFoundError, RepositoryNotFoundError, RevisionNotFoundError, is_jinja_available, ) from packaging import version from requests import HTTPError from .. import __version__ from .constants import ( DEPRECATED_REVISION_ARGS, DIFFUSERS_CACHE, HUGGINGFACE_CO_RESOLVE_ENDPOINT, SAFETENSORS_WEIGHTS_NAME, WEIGHTS_NAME, ) from .import_utils import ( ENV_VARS_TRUE_VALUES, _flax_version, _jax_version, _onnxruntime_version, _torch_version, is_flax_available, is_onnx_available, is_torch_available, ) from .logging import get_logger __a :Dict = get_logger(__name__) __a :Union[str, Any] = Path(__file__).parent / 'model_card_template.md' __a :Tuple = uuida().hex __a :List[Any] = os.getenv('HF_HUB_OFFLINE', '').upper() in ENV_VARS_TRUE_VALUES __a :Union[str, Any] = os.getenv('DISABLE_TELEMETRY', '').upper() in ENV_VARS_TRUE_VALUES __a :Tuple = HUGGINGFACE_CO_RESOLVE_ENDPOINT + '/api/telemetry/' def __snake_case ( __UpperCamelCase : Union[Dict, str, None] = None ): """simple docstring""" A_ = f'''diffusers/{__version__}; python/{sys.version.split()[0]}; session_id/{SESSION_ID}''' if DISABLE_TELEMETRY or HF_HUB_OFFLINE: return ua + "; telemetry/off" if is_torch_available(): ua += f'''; torch/{_torch_version}''' if is_flax_available(): ua += f'''; jax/{_jax_version}''' ua += f'''; flax/{_flax_version}''' if is_onnx_available(): ua += f'''; onnxruntime/{_onnxruntime_version}''' # CI will set this value to True if os.environ.get("DIFFUSERS_IS_CI" ,"" ).upper() in ENV_VARS_TRUE_VALUES: ua += "; is_ci/true" if isinstance(__UpperCamelCase ,__UpperCamelCase ): ua += "; " + "; ".join(f'''{k}/{v}''' for k, v in user_agent.items() ) elif isinstance(__UpperCamelCase ,__UpperCamelCase ): ua += "; " + user_agent return ua def __snake_case ( __UpperCamelCase : str ,__UpperCamelCase : Optional[str] = None ,__UpperCamelCase : Optional[str] = None ): """simple docstring""" if token is None: A_ = HfFolder.get_token() if organization is None: A_ = whoami(__UpperCamelCase )["name"] return f'''{username}/{model_id}''' else: return f'''{organization}/{model_id}''' def __snake_case ( __UpperCamelCase : Tuple ,__UpperCamelCase : Union[str, Any] ): """simple docstring""" if not is_jinja_available(): raise ValueError( "Modelcard rendering is based on Jinja templates." " Please make sure to have `jinja` installed before using `create_model_card`." " To install it, please run `pip install Jinja2`." ) if hasattr(__UpperCamelCase ,"local_rank" ) and args.local_rank not in [-1, 0]: return A_ = args.hub_token if hasattr(__UpperCamelCase ,"hub_token" ) else None A_ = get_full_repo_name(__UpperCamelCase ,token=__UpperCamelCase ) A_ = ModelCard.from_template( card_data=ModelCardData( # Card metadata object that will be converted to YAML block language="en" ,license="apache-2.0" ,library_name="diffusers" ,tags=[] ,datasets=args.dataset_name ,metrics=[] ,) ,template_path=__UpperCamelCase ,model_name=__UpperCamelCase ,repo_name=__UpperCamelCase ,dataset_name=args.dataset_name if hasattr(__UpperCamelCase ,"dataset_name" ) else None ,learning_rate=args.learning_rate ,train_batch_size=args.train_batch_size ,eval_batch_size=args.eval_batch_size ,gradient_accumulation_steps=( args.gradient_accumulation_steps if hasattr(__UpperCamelCase ,"gradient_accumulation_steps" ) else None ) ,adam_betaa=args.adam_betaa if hasattr(__UpperCamelCase ,"adam_beta1" ) else None ,adam_betaa=args.adam_betaa if hasattr(__UpperCamelCase ,"adam_beta2" ) else None ,adam_weight_decay=args.adam_weight_decay if hasattr(__UpperCamelCase ,"adam_weight_decay" ) else None ,adam_epsilon=args.adam_epsilon if hasattr(__UpperCamelCase ,"adam_epsilon" ) else None ,lr_scheduler=args.lr_scheduler if hasattr(__UpperCamelCase ,"lr_scheduler" ) else None ,lr_warmup_steps=args.lr_warmup_steps if hasattr(__UpperCamelCase ,"lr_warmup_steps" ) else None ,ema_inv_gamma=args.ema_inv_gamma if hasattr(__UpperCamelCase ,"ema_inv_gamma" ) else None ,ema_power=args.ema_power if hasattr(__UpperCamelCase ,"ema_power" ) else None ,ema_max_decay=args.ema_max_decay if hasattr(__UpperCamelCase ,"ema_max_decay" ) else None ,mixed_precision=args.mixed_precision ,) A_ = os.path.join(args.output_dir ,"README.md" ) model_card.save(__UpperCamelCase ) def __snake_case ( __UpperCamelCase : Optional[str] ,__UpperCamelCase : Optional[str] = None ): """simple docstring""" if resolved_file is None or commit_hash is not None: return commit_hash A_ = str(Path(__UpperCamelCase ).as_posix() ) A_ = re.search(R"snapshots/([^/]+)/" ,__UpperCamelCase ) if search is None: return None A_ = search.groups()[0] return commit_hash if REGEX_COMMIT_HASH.match(__UpperCamelCase ) else None # Old default cache path, potentially to be migrated. # This logic was more or less taken from `transformers`, with the following differences: # - Diffusers doesn't use custom environment variables to specify the cache path. # - There is no need to migrate the cache format, just move the files to the new location. __a :str = os.path.expanduser( os.getenv('HF_HOME', os.path.join(os.getenv('XDG_CACHE_HOME', '~/.cache'), 'huggingface')) ) __a :List[Any] = os.path.join(hf_cache_home, 'diffusers') def __snake_case ( __UpperCamelCase : Optional[str] = None ,__UpperCamelCase : Optional[str] = None ): """simple docstring""" if new_cache_dir is None: A_ = DIFFUSERS_CACHE if old_cache_dir is None: A_ = old_diffusers_cache A_ = Path(__UpperCamelCase ).expanduser() A_ = Path(__UpperCamelCase ).expanduser() for old_blob_path in old_cache_dir.glob("**/blobs/*" ): if old_blob_path.is_file() and not old_blob_path.is_symlink(): A_ = new_cache_dir / old_blob_path.relative_to(__UpperCamelCase ) new_blob_path.parent.mkdir(parents=__UpperCamelCase ,exist_ok=__UpperCamelCase ) os.replace(__UpperCamelCase ,__UpperCamelCase ) try: os.symlink(__UpperCamelCase ,__UpperCamelCase ) except OSError: logger.warning( "Could not create symlink between old cache and new cache. If you use an older version of diffusers again, files will be re-downloaded." ) # At this point, old_cache_dir contains symlinks to the new cache (it can still be used). __a :Dict = os.path.join(DIFFUSERS_CACHE, 'version_diffusers_cache.txt') if not os.path.isfile(cache_version_file): __a :Optional[int] = 0 else: with open(cache_version_file) as f: try: __a :Dict = int(f.read()) except ValueError: __a :str = 0 if cache_version < 1: __a :Optional[Any] = os.path.isdir(old_diffusers_cache) and len(os.listdir(old_diffusers_cache)) > 0 if old_cache_is_not_empty: logger.warning( 'The cache for model files in Diffusers v0.14.0 has moved to a new location. Moving your ' 'existing cached models. This is a one-time operation, you can interrupt it or run it ' 'later by calling `diffusers.utils.hub_utils.move_cache()`.' ) try: move_cache() except Exception as e: __a :Optional[Any] = '\n'.join(traceback.format_tb(e.__traceback__)) logger.error( F"There was a problem when trying to move your cache:\n\n{trace}\n{e.__class__.__name__}: {e}\n\nPlease " 'file an issue at https://github.com/huggingface/diffusers/issues/new/choose, copy paste this whole ' 'message and we will do our best to help.' ) if cache_version < 1: try: os.makedirs(DIFFUSERS_CACHE, exist_ok=True) with open(cache_version_file, 'w') as f: f.write('1') except Exception: logger.warning( F"There was a problem when trying to write in your cache folder ({DIFFUSERS_CACHE}). Please, ensure " 'the directory exists and can be written to.' ) def __snake_case ( __UpperCamelCase : str ,__UpperCamelCase : Optional[str] = None ): """simple docstring""" if variant is not None: A_ = weights_name.split("." ) A_ = splits[:-1] + [variant] + splits[-1:] A_ = ".".join(__UpperCamelCase ) return weights_name def __snake_case ( __UpperCamelCase : Optional[Any] ,*, __UpperCamelCase : Union[str, Any] ,__UpperCamelCase : Any ,__UpperCamelCase : Tuple ,__UpperCamelCase : Union[str, Any] ,__UpperCamelCase : str ,__UpperCamelCase : int ,__UpperCamelCase : Union[str, Any] ,__UpperCamelCase : int ,__UpperCamelCase : Optional[int] ,__UpperCamelCase : Tuple ,__UpperCamelCase : Optional[int]=None ,): """simple docstring""" A_ = str(__UpperCamelCase ) if os.path.isfile(__UpperCamelCase ): return pretrained_model_name_or_path elif os.path.isdir(__UpperCamelCase ): if os.path.isfile(os.path.join(__UpperCamelCase ,__UpperCamelCase ) ): # Load from a PyTorch checkpoint A_ = os.path.join(__UpperCamelCase ,__UpperCamelCase ) return model_file elif subfolder is not None and os.path.isfile( os.path.join(__UpperCamelCase ,__UpperCamelCase ,__UpperCamelCase ) ): A_ = os.path.join(__UpperCamelCase ,__UpperCamelCase ,__UpperCamelCase ) return model_file else: raise EnvironmentError( f'''Error no file named {weights_name} found in directory {pretrained_model_name_or_path}.''' ) else: # 1. First check if deprecated way of loading from branches is used if ( revision in DEPRECATED_REVISION_ARGS and (weights_name == WEIGHTS_NAME or weights_name == SAFETENSORS_WEIGHTS_NAME) and version.parse(version.parse(__UpperCamelCase ).base_version ) >= version.parse("0.20.0" ) ): try: A_ = hf_hub_download( __UpperCamelCase ,filename=_add_variant(__UpperCamelCase ,__UpperCamelCase ) ,cache_dir=__UpperCamelCase ,force_download=__UpperCamelCase ,proxies=__UpperCamelCase ,resume_download=__UpperCamelCase ,local_files_only=__UpperCamelCase ,use_auth_token=__UpperCamelCase ,user_agent=__UpperCamelCase ,subfolder=__UpperCamelCase ,revision=revision or commit_hash ,) warnings.warn( f'''Loading the variant {revision} from {pretrained_model_name_or_path} via `revision=\'{revision}\'` is deprecated. Loading instead from `revision=\'main\'` with `variant={revision}`. Loading model variants via `revision=\'{revision}\'` will be removed in diffusers v1. Please use `variant=\'{revision}\'` instead.''' ,__UpperCamelCase ,) return model_file except: # noqa: E722 warnings.warn( f'''You are loading the variant {revision} from {pretrained_model_name_or_path} via `revision=\'{revision}\'`. This behavior is deprecated and will be removed in diffusers v1. One should use `variant=\'{revision}\'` instead. However, it appears that {pretrained_model_name_or_path} currently does not have a {_add_variant(__UpperCamelCase ,__UpperCamelCase )} file in the \'main\' branch of {pretrained_model_name_or_path}. \n The Diffusers team and community would be very grateful if you could open an issue: https://github.com/huggingface/diffusers/issues/new with the title \'{pretrained_model_name_or_path} is missing {_add_variant(__UpperCamelCase ,__UpperCamelCase )}\' so that the correct variant file can be added.''' ,__UpperCamelCase ,) try: # 2. Load model file as usual A_ = hf_hub_download( __UpperCamelCase ,filename=__UpperCamelCase ,cache_dir=__UpperCamelCase ,force_download=__UpperCamelCase ,proxies=__UpperCamelCase ,resume_download=__UpperCamelCase ,local_files_only=__UpperCamelCase ,use_auth_token=__UpperCamelCase ,user_agent=__UpperCamelCase ,subfolder=__UpperCamelCase ,revision=revision or commit_hash ,) return model_file except RepositoryNotFoundError: raise EnvironmentError( f'''{pretrained_model_name_or_path} is not a local folder and is not a valid model identifier ''' "listed on 'https://huggingface.co/models'\nIf this is a private repository, make sure to pass a " "token having permission to this repo with `use_auth_token` or log in with `huggingface-cli " "login`." ) except RevisionNotFoundError: raise EnvironmentError( f'''{revision} is not a valid git identifier (branch name, tag name or commit id) that exists for ''' "this model name. Check the model page at " f'''\'https://huggingface.co/{pretrained_model_name_or_path}\' for available revisions.''' ) except EntryNotFoundError: raise EnvironmentError( f'''{pretrained_model_name_or_path} does not appear to have a file named {weights_name}.''' ) except HTTPError as err: raise EnvironmentError( f'''There was a specific connection error when trying to load {pretrained_model_name_or_path}:\n{err}''' ) except ValueError: raise EnvironmentError( f'''We couldn\'t connect to \'{HUGGINGFACE_CO_RESOLVE_ENDPOINT}\' to load this model, couldn\'t find it''' f''' in the cached files and it looks like {pretrained_model_name_or_path} is not the path to a''' f''' directory containing a file named {weights_name} or''' " \nCheckout your internet connection or see how to run the library in" " offline mode at 'https://huggingface.co/docs/diffusers/installation#offline-mode'." ) except EnvironmentError: raise EnvironmentError( f'''Can\'t load the model for \'{pretrained_model_name_or_path}\'. If you were trying to load it from ''' "'https://huggingface.co/models', make sure you don't have a local directory with the same name. " f'''Otherwise, make sure \'{pretrained_model_name_or_path}\' is the correct path to a directory ''' f'''containing a file named {weights_name}''' )
312
1
from transformers import DistilBertTokenizer, DistilBertTokenizerFast from transformers.testing_utils import require_tokenizers, slow from ..bert.test_tokenization_bert import BertTokenizationTest @require_tokenizers class _a ( snake_case_ ): """simple docstring""" _lowerCamelCase : Dict = DistilBertTokenizer _lowerCamelCase : int = DistilBertTokenizerFast _lowerCamelCase : Tuple = True @slow def __A ( self : Dict ): A_ = DistilBertTokenizer.from_pretrained("distilbert-base-uncased" ) A_ = tokenizer.encode("sequence builders" , add_special_tokens=UpperCAmelCase ) A_ = tokenizer.encode("multi-sequence build" , add_special_tokens=UpperCAmelCase ) A_ = tokenizer.build_inputs_with_special_tokens(UpperCAmelCase ) A_ = tokenizer.build_inputs_with_special_tokens(UpperCAmelCase , UpperCAmelCase ) assert encoded_sentence == [tokenizer.cls_token_id] + text + [tokenizer.sep_token_id] assert encoded_pair == [tokenizer.cls_token_id] + text + [tokenizer.sep_token_id] + text_a + [ tokenizer.sep_token_id ]
312
# Copyright 2023 The HuggingFace Team. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available __a :Any = { 'configuration_mgp_str': ['MGP_STR_PRETRAINED_CONFIG_ARCHIVE_MAP', 'MgpstrConfig'], 'processing_mgp_str': ['MgpstrProcessor'], 'tokenization_mgp_str': ['MgpstrTokenizer'], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __a :Optional[Any] = [ 'MGP_STR_PRETRAINED_MODEL_ARCHIVE_LIST', 'MgpstrModel', 'MgpstrPreTrainedModel', 'MgpstrForSceneTextRecognition', ] if TYPE_CHECKING: from .configuration_mgp_str import MGP_STR_PRETRAINED_CONFIG_ARCHIVE_MAP, MgpstrConfig from .processing_mgp_str import MgpstrProcessor from .tokenization_mgp_str import MgpstrTokenizer try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_mgp_str import ( MGP_STR_PRETRAINED_MODEL_ARCHIVE_LIST, MgpstrForSceneTextRecognition, MgpstrModel, MgpstrPreTrainedModel, ) else: import sys __a :List[Any] = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
312
1
from . import __version__ # Backward compatibility imports, to make sure all those objects can be found in file_utils from .utils import ( CLOUDFRONT_DISTRIB_PREFIX, CONFIG_NAME, DISABLE_TELEMETRY, DUMMY_INPUTS, DUMMY_MASK, ENV_VARS_TRUE_AND_AUTO_VALUES, ENV_VARS_TRUE_VALUES, FEATURE_EXTRACTOR_NAME, FLAX_WEIGHTS_NAME, HF_MODULES_CACHE, HUGGINGFACE_CO_PREFIX, HUGGINGFACE_CO_RESOLVE_ENDPOINT, MODEL_CARD_NAME, MULTIPLE_CHOICE_DUMMY_INPUTS, PYTORCH_PRETRAINED_BERT_CACHE, PYTORCH_TRANSFORMERS_CACHE, S3_BUCKET_PREFIX, SENTENCEPIECE_UNDERLINE, SPIECE_UNDERLINE, TF2_WEIGHTS_NAME, TF_WEIGHTS_NAME, TORCH_FX_REQUIRED_VERSION, TRANSFORMERS_CACHE, TRANSFORMERS_DYNAMIC_MODULE_NAME, USE_JAX, USE_TF, USE_TORCH, WEIGHTS_INDEX_NAME, WEIGHTS_NAME, ContextManagers, DummyObject, EntryNotFoundError, ExplicitEnum, ModelOutput, PaddingStrategy, PushToHubMixin, RepositoryNotFoundError, RevisionNotFoundError, TensorType, _LazyModule, add_code_sample_docstrings, add_end_docstrings, add_start_docstrings, add_start_docstrings_to_model_forward, cached_property, copy_func, default_cache_path, define_sagemaker_information, get_cached_models, get_file_from_repo, get_full_repo_name, get_torch_version, has_file, http_user_agent, is_apex_available, is_bsa_available, is_coloredlogs_available, is_datasets_available, is_detectrona_available, is_faiss_available, is_flax_available, is_ftfy_available, is_in_notebook, is_ipex_available, is_librosa_available, is_offline_mode, is_onnx_available, is_pandas_available, is_phonemizer_available, is_protobuf_available, is_psutil_available, is_pyanvml_available, is_pyctcdecode_available, is_pytesseract_available, is_pytorch_quantization_available, is_rjieba_available, is_sagemaker_dp_enabled, is_sagemaker_mp_enabled, is_scipy_available, is_sentencepiece_available, is_seqio_available, is_sklearn_available, is_soundfile_availble, is_spacy_available, is_speech_available, is_tensor, is_tensorflow_probability_available, is_tfaonnx_available, is_tf_available, is_timm_available, is_tokenizers_available, is_torch_available, is_torch_bfaa_available, is_torch_cuda_available, is_torch_fx_available, is_torch_fx_proxy, is_torch_mps_available, is_torch_tfaa_available, is_torch_tpu_available, is_torchaudio_available, is_training_run_on_sagemaker, is_vision_available, replace_return_docstrings, requires_backends, to_numpy, to_py_obj, torch_only_method, )
312
import functools from typing import Any def __snake_case ( __UpperCamelCase : str ,__UpperCamelCase : list[str] ): """simple docstring""" if not isinstance(__UpperCamelCase ,__UpperCamelCase ) or len(__UpperCamelCase ) == 0: raise ValueError("the string should be not empty string" ) if not isinstance(__UpperCamelCase ,__UpperCamelCase ) or not all( isinstance(__UpperCamelCase ,__UpperCamelCase ) and len(__UpperCamelCase ) > 0 for item in words ): raise ValueError("the words should be a list of non-empty strings" ) # Build trie A_ = {} A_ = "WORD_KEEPER" for word in words: A_ = trie for c in word: if c not in trie_node: A_ = {} A_ = trie_node[c] A_ = True A_ = len(__UpperCamelCase ) # Dynamic programming method @functools.cache def is_breakable(__UpperCamelCase : int ) -> bool: if index == len_string: return True A_ = trie for i in range(__UpperCamelCase ,__UpperCamelCase ): A_ = trie_node.get(string[i] ,__UpperCamelCase ) if trie_node is None: return False if trie_node.get(__UpperCamelCase ,__UpperCamelCase ) and is_breakable(i + 1 ): return True return False return is_breakable(0 ) if __name__ == "__main__": import doctest doctest.testmod()
312
1
import os from shutil import copyfile from typing import List, Optional, Tuple from ...tokenization_utils import AddedToken from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import is_sentencepiece_available, logging if is_sentencepiece_available(): from .tokenization_barthez import BarthezTokenizer else: __a :Dict = None __a :Union[str, Any] = logging.get_logger(__name__) __a :List[Any] = {'vocab_file': 'sentencepiece.bpe.model', 'tokenizer_file': 'tokenizer.json'} __a :List[str] = { 'vocab_file': { 'moussaKam/mbarthez': 'https://huggingface.co/moussaKam/mbarthez/resolve/main/sentencepiece.bpe.model', 'moussaKam/barthez': 'https://huggingface.co/moussaKam/barthez/resolve/main/sentencepiece.bpe.model', 'moussaKam/barthez-orangesum-title': ( 'https://huggingface.co/moussaKam/barthez-orangesum-title/resolve/main/sentencepiece.bpe.model' ), }, 'tokenizer_file': { 'moussaKam/mbarthez': 'https://huggingface.co/moussaKam/mbarthez/resolve/main/tokenizer.json', 'moussaKam/barthez': 'https://huggingface.co/moussaKam/barthez/resolve/main/tokenizer.json', 'moussaKam/barthez-orangesum-title': ( 'https://huggingface.co/moussaKam/barthez-orangesum-title/resolve/main/tokenizer.json' ), }, } __a :Dict = { 'moussaKam/mbarthez': 1024, 'moussaKam/barthez': 1024, 'moussaKam/barthez-orangesum-title': 1024, } __a :Optional[int] = '▁' class _a ( snake_case_ ): """simple docstring""" _lowerCamelCase : Any = VOCAB_FILES_NAMES _lowerCamelCase : Optional[Any] = PRETRAINED_VOCAB_FILES_MAP _lowerCamelCase : Optional[Any] = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES _lowerCamelCase : Tuple = ['input_ids', 'attention_mask'] _lowerCamelCase : Dict = BarthezTokenizer def __init__( self : str , UpperCAmelCase : Tuple=None , UpperCAmelCase : List[str]=None , UpperCAmelCase : Optional[int]="<s>" , UpperCAmelCase : int="</s>" , UpperCAmelCase : int="</s>" , UpperCAmelCase : str="<s>" , UpperCAmelCase : Dict="<unk>" , UpperCAmelCase : Any="<pad>" , UpperCAmelCase : int="<mask>" , **UpperCAmelCase : List[str] , ): # Mask token behave like a normal word, i.e. include the space before it A_ = AddedToken(UpperCAmelCase , lstrip=UpperCAmelCase , rstrip=UpperCAmelCase ) if isinstance(UpperCAmelCase , UpperCAmelCase ) else mask_token super().__init__( UpperCAmelCase , tokenizer_file=UpperCAmelCase , bos_token=UpperCAmelCase , eos_token=UpperCAmelCase , unk_token=UpperCAmelCase , sep_token=UpperCAmelCase , cls_token=UpperCAmelCase , pad_token=UpperCAmelCase , mask_token=UpperCAmelCase , **UpperCAmelCase , ) A_ = vocab_file A_ = False if not self.vocab_file else True def __A ( self : Tuple , UpperCAmelCase : List[int] , UpperCAmelCase : Optional[List[int]] = None ): if token_ids_a is None: return [self.cls_token_id] + token_ids_a + [self.sep_token_id] A_ = [self.cls_token_id] A_ = [self.sep_token_id] return cls + token_ids_a + sep + sep + token_ids_a + sep def __A ( self : int , UpperCAmelCase : List[int] , UpperCAmelCase : Optional[List[int]] = None ): A_ = [self.sep_token_id] A_ = [self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep ) * [0] return len(cls + token_ids_a + sep + sep + token_ids_a + sep ) * [0] def __A ( self : Dict , UpperCAmelCase : str , UpperCAmelCase : Optional[str] = None ): 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(UpperCAmelCase ): logger.error(f'''Vocabulary path ({save_directory}) should be a directory''' ) return A_ = 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 ): copyfile(self.vocab_file , UpperCAmelCase ) return (out_vocab_file,)
312
import json from typing import List, Optional, Tuple from tokenizers import normalizers from ...tokenization_utils_fast import PreTrainedTokenizerFast from .tokenization_electra import ElectraTokenizer __a :List[str] = {'vocab_file': 'vocab.txt', 'tokenizer_file': 'tokenizer.json'} __a :Union[str, Any] = { 'vocab_file': { 'google/electra-small-generator': ( 'https://huggingface.co/google/electra-small-generator/resolve/main/vocab.txt' ), 'google/electra-base-generator': 'https://huggingface.co/google/electra-base-generator/resolve/main/vocab.txt', 'google/electra-large-generator': ( 'https://huggingface.co/google/electra-large-generator/resolve/main/vocab.txt' ), 'google/electra-small-discriminator': ( 'https://huggingface.co/google/electra-small-discriminator/resolve/main/vocab.txt' ), 'google/electra-base-discriminator': ( 'https://huggingface.co/google/electra-base-discriminator/resolve/main/vocab.txt' ), 'google/electra-large-discriminator': ( 'https://huggingface.co/google/electra-large-discriminator/resolve/main/vocab.txt' ), }, 'tokenizer_file': { 'google/electra-small-generator': ( 'https://huggingface.co/google/electra-small-generator/resolve/main/tokenizer.json' ), 'google/electra-base-generator': ( 'https://huggingface.co/google/electra-base-generator/resolve/main/tokenizer.json' ), 'google/electra-large-generator': ( 'https://huggingface.co/google/electra-large-generator/resolve/main/tokenizer.json' ), 'google/electra-small-discriminator': ( 'https://huggingface.co/google/electra-small-discriminator/resolve/main/tokenizer.json' ), 'google/electra-base-discriminator': ( 'https://huggingface.co/google/electra-base-discriminator/resolve/main/tokenizer.json' ), 'google/electra-large-discriminator': ( 'https://huggingface.co/google/electra-large-discriminator/resolve/main/tokenizer.json' ), }, } __a :Optional[int] = { 'google/electra-small-generator': 512, 'google/electra-base-generator': 512, 'google/electra-large-generator': 512, 'google/electra-small-discriminator': 512, 'google/electra-base-discriminator': 512, 'google/electra-large-discriminator': 512, } __a :str = { 'google/electra-small-generator': {'do_lower_case': True}, 'google/electra-base-generator': {'do_lower_case': True}, 'google/electra-large-generator': {'do_lower_case': True}, 'google/electra-small-discriminator': {'do_lower_case': True}, 'google/electra-base-discriminator': {'do_lower_case': True}, 'google/electra-large-discriminator': {'do_lower_case': True}, } class _a ( snake_case_ ): """simple docstring""" _lowerCamelCase : Tuple = VOCAB_FILES_NAMES _lowerCamelCase : Union[str, Any] = PRETRAINED_VOCAB_FILES_MAP _lowerCamelCase : int = PRETRAINED_INIT_CONFIGURATION _lowerCamelCase : List[Any] = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES _lowerCamelCase : int = ElectraTokenizer def __init__( self : Tuple , UpperCAmelCase : Dict=None , UpperCAmelCase : Optional[int]=None , UpperCAmelCase : Any=True , UpperCAmelCase : Any="[UNK]" , UpperCAmelCase : Union[str, Any]="[SEP]" , UpperCAmelCase : List[Any]="[PAD]" , UpperCAmelCase : Union[str, Any]="[CLS]" , UpperCAmelCase : List[Any]="[MASK]" , UpperCAmelCase : List[str]=True , UpperCAmelCase : Any=None , **UpperCAmelCase : Union[str, Any] , ): super().__init__( UpperCAmelCase , tokenizer_file=UpperCAmelCase , do_lower_case=UpperCAmelCase , unk_token=UpperCAmelCase , sep_token=UpperCAmelCase , pad_token=UpperCAmelCase , cls_token=UpperCAmelCase , mask_token=UpperCAmelCase , tokenize_chinese_chars=UpperCAmelCase , strip_accents=UpperCAmelCase , **UpperCAmelCase , ) A_ = json.loads(self.backend_tokenizer.normalizer.__getstate__() ) if ( normalizer_state.get("lowercase" , UpperCAmelCase ) != do_lower_case or normalizer_state.get("strip_accents" , UpperCAmelCase ) != strip_accents or normalizer_state.get("handle_chinese_chars" , UpperCAmelCase ) != tokenize_chinese_chars ): A_ = getattr(UpperCAmelCase , normalizer_state.pop("type" ) ) A_ = do_lower_case A_ = strip_accents A_ = tokenize_chinese_chars A_ = normalizer_class(**UpperCAmelCase ) A_ = do_lower_case def __A ( self : int , UpperCAmelCase : List[Any] , UpperCAmelCase : Union[str, Any]=None ): A_ = [self.cls_token_id] + token_ids_a + [self.sep_token_id] if token_ids_a: output += token_ids_a + [self.sep_token_id] return output def __A ( self : Union[str, Any] , UpperCAmelCase : List[int] , UpperCAmelCase : Optional[List[int]] = None ): A_ = [self.sep_token_id] A_ = [self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep ) * [0] return len(cls + token_ids_a + sep ) * [0] + len(token_ids_a + sep ) * [1] def __A ( self : Tuple , UpperCAmelCase : str , UpperCAmelCase : Optional[str] = None ): A_ = self._tokenizer.model.save(UpperCAmelCase , name=UpperCAmelCase ) return tuple(UpperCAmelCase )
312
1
from collections import OrderedDict from typing import Mapping from packaging import version from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging __a :Union[str, Any] = logging.get_logger(__name__) __a :Optional[Any] = { 'facebook/deit-base-distilled-patch16-224': ( 'https://huggingface.co/facebook/deit-base-patch16-224/resolve/main/config.json' ), # See all DeiT models at https://huggingface.co/models?filter=deit } class _a ( snake_case_ ): """simple docstring""" _lowerCamelCase : List[Any] = 'deit' def __init__( self : Tuple , UpperCAmelCase : Dict=768 , UpperCAmelCase : int=12 , UpperCAmelCase : Optional[int]=12 , UpperCAmelCase : int=3072 , UpperCAmelCase : Dict="gelu" , UpperCAmelCase : Optional[Any]=0.0 , UpperCAmelCase : Any=0.0 , UpperCAmelCase : Optional[int]=0.02 , UpperCAmelCase : Union[str, Any]=1E-12 , UpperCAmelCase : Union[str, Any]=224 , UpperCAmelCase : Dict=16 , UpperCAmelCase : Optional[int]=3 , UpperCAmelCase : List[Any]=True , UpperCAmelCase : Tuple=16 , **UpperCAmelCase : List[Any] , ): super().__init__(**UpperCAmelCase ) A_ = hidden_size A_ = num_hidden_layers A_ = num_attention_heads A_ = intermediate_size A_ = hidden_act A_ = hidden_dropout_prob A_ = attention_probs_dropout_prob A_ = initializer_range A_ = layer_norm_eps A_ = image_size A_ = patch_size A_ = num_channels A_ = qkv_bias A_ = encoder_stride class _a ( snake_case_ ): """simple docstring""" _lowerCamelCase : Union[str, Any] = version.parse('1.11' ) @property def __A ( self : str ): return OrderedDict( [ ("pixel_values", {0: "batch", 1: "num_channels", 2: "height", 3: "width"}), ] ) @property def __A ( self : Any ): return 1E-4
312
# flake8: noqa # Lint as: python3 from typing import Dict, List, Optional, Type from .. import config from ..utils import logging from .formatting import ( ArrowFormatter, CustomFormatter, Formatter, PandasFormatter, PythonFormatter, TensorFormatter, format_table, query_table, ) from .np_formatter import NumpyFormatter __a :Optional[Any] = logging.get_logger(__name__) __a :Dict[Optional[str], Type[Formatter]] = {} __a :Dict[Optional[str], str] = {} __a :Dict[Optional[str], Exception] = {} def __snake_case ( __UpperCamelCase : type ,__UpperCamelCase : Optional[str] ,__UpperCamelCase : Optional[List[str]] = None ,): """simple docstring""" A_ = aliases if aliases is not None else [] if format_type in _FORMAT_TYPES: logger.warning( f'''Overwriting format type \'{format_type}\' ({_FORMAT_TYPES[format_type].__name__} -> {formatter_cls.__name__})''' ) A_ = formatter_cls for alias in set(aliases + [format_type] ): if alias in _FORMAT_TYPES_ALIASES: logger.warning( f'''Overwriting format type alias \'{alias}\' ({_FORMAT_TYPES_ALIASES[alias]} -> {format_type})''' ) A_ = format_type def __snake_case ( __UpperCamelCase : Exception ,__UpperCamelCase : Optional[str] ,__UpperCamelCase : Optional[List[str]] = None ): """simple docstring""" A_ = aliases if aliases is not None else [] for alias in set(aliases + [format_type] ): A_ = unavailable_error # Here we define all the available formatting functions that can be used by `Dataset.set_format` _register_formatter(PythonFormatter, None, aliases=['python']) _register_formatter(ArrowFormatter, 'arrow', aliases=['pa', 'pyarrow']) _register_formatter(NumpyFormatter, 'numpy', aliases=['np']) _register_formatter(PandasFormatter, 'pandas', aliases=['pd']) _register_formatter(CustomFormatter, 'custom') if config.TORCH_AVAILABLE: from .torch_formatter import TorchFormatter _register_formatter(TorchFormatter, 'torch', aliases=['pt', 'pytorch']) else: __a :List[Any] = ValueError('PyTorch needs to be installed to be able to return PyTorch tensors.') _register_unavailable_formatter(_torch_error, 'torch', aliases=['pt', 'pytorch']) if config.TF_AVAILABLE: from .tf_formatter import TFFormatter _register_formatter(TFFormatter, 'tensorflow', aliases=['tf']) else: __a :List[str] = ValueError('Tensorflow needs to be installed to be able to return Tensorflow tensors.') _register_unavailable_formatter(_tf_error, 'tensorflow', aliases=['tf']) if config.JAX_AVAILABLE: from .jax_formatter import JaxFormatter _register_formatter(JaxFormatter, 'jax', aliases=[]) else: __a :Tuple = ValueError('JAX needs to be installed to be able to return JAX arrays.') _register_unavailable_formatter(_jax_error, 'jax', aliases=[]) def __snake_case ( __UpperCamelCase : Optional[str] ): """simple docstring""" if format_type in _FORMAT_TYPES_ALIASES: return _FORMAT_TYPES_ALIASES[format_type] else: return format_type def __snake_case ( __UpperCamelCase : Optional[str] ,**__UpperCamelCase : List[Any] ): """simple docstring""" A_ = get_format_type_from_alias(__UpperCamelCase ) if format_type in _FORMAT_TYPES: return _FORMAT_TYPES[format_type](**__UpperCamelCase ) if format_type in _FORMAT_TYPES_ALIASES_UNAVAILABLE: raise _FORMAT_TYPES_ALIASES_UNAVAILABLE[format_type] else: raise ValueError( f'''Return type should be None or selected in {list(type for type in _FORMAT_TYPES.keys() if type != None )}, but got \'{format_type}\'''' )
312
1
import math from typing import List, Optional, Tuple, Union import numpy as np import torch from ..configuration_utils import ConfigMixin, register_to_config from .scheduling_utils import SchedulerMixin, SchedulerOutput class _a ( snake_case_ , snake_case_ ): """simple docstring""" _lowerCamelCase : Optional[Any] = 1 @register_to_config def __init__( self : Union[str, Any] , UpperCAmelCase : int = 1000 , UpperCAmelCase : Optional[Union[np.ndarray, List[float]]] = None ): # set `betas`, `alphas`, `timesteps` self.set_timesteps(UpperCAmelCase ) # standard deviation of the initial noise distribution A_ = 1.0 # For now we only support F-PNDM, i.e. the runge-kutta method # For more information on the algorithm please take a look at the paper: https://arxiv.org/pdf/2202.09778.pdf # mainly at formula (9), (12), (13) and the Algorithm 2. A_ = 4 # running values A_ = [] def __A ( self : int , UpperCAmelCase : int , UpperCAmelCase : Union[str, torch.device] = None ): A_ = num_inference_steps A_ = torch.linspace(1 , 0 , num_inference_steps + 1 )[:-1] A_ = torch.cat([steps, torch.tensor([0.0] )] ) if self.config.trained_betas is not None: A_ = torch.tensor(self.config.trained_betas , dtype=torch.floataa ) else: A_ = torch.sin(steps * math.pi / 2 ) ** 2 A_ = (1.0 - self.betas**2) ** 0.5 A_ = (torch.atana(self.betas , self.alphas ) / math.pi * 2)[:-1] A_ = timesteps.to(UpperCAmelCase ) A_ = [] def __A ( self : Any , UpperCAmelCase : torch.FloatTensor , UpperCAmelCase : int , UpperCAmelCase : torch.FloatTensor , UpperCAmelCase : bool = True , ): if self.num_inference_steps is None: raise ValueError( "Number of inference steps is 'None', you need to run 'set_timesteps' after creating the scheduler" ) A_ = (self.timesteps == timestep).nonzero().item() A_ = timestep_index + 1 A_ = sample * self.betas[timestep_index] + model_output * self.alphas[timestep_index] self.ets.append(UpperCAmelCase ) if len(self.ets ) == 1: A_ = self.ets[-1] elif len(self.ets ) == 2: A_ = (3 * self.ets[-1] - self.ets[-2]) / 2 elif len(self.ets ) == 3: A_ = (23 * self.ets[-1] - 16 * self.ets[-2] + 5 * self.ets[-3]) / 12 else: A_ = (1 / 24) * (55 * self.ets[-1] - 59 * self.ets[-2] + 37 * self.ets[-3] - 9 * self.ets[-4]) A_ = self._get_prev_sample(UpperCAmelCase , UpperCAmelCase , UpperCAmelCase , UpperCAmelCase ) if not return_dict: return (prev_sample,) return SchedulerOutput(prev_sample=UpperCAmelCase ) def __A ( self : Tuple , UpperCAmelCase : torch.FloatTensor , *UpperCAmelCase : int , **UpperCAmelCase : Dict ): return sample def __A ( self : Dict , UpperCAmelCase : Dict , UpperCAmelCase : Dict , UpperCAmelCase : Dict , UpperCAmelCase : Optional[Any] ): A_ = self.alphas[timestep_index] A_ = self.betas[timestep_index] A_ = self.alphas[prev_timestep_index] A_ = self.betas[prev_timestep_index] A_ = (sample - sigma * ets) / max(UpperCAmelCase , 1E-8 ) A_ = next_alpha * pred + ets * next_sigma return prev_sample def __len__( self : Dict ): return self.config.num_train_timesteps
312
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available, is_vision_available __a :int = { 'configuration_mask2former': [ 'MASK2FORMER_PRETRAINED_CONFIG_ARCHIVE_MAP', 'Mask2FormerConfig', ], } try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __a :Union[str, Any] = ['Mask2FormerImageProcessor'] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __a :Optional[Any] = [ 'MASK2FORMER_PRETRAINED_MODEL_ARCHIVE_LIST', 'Mask2FormerForUniversalSegmentation', 'Mask2FormerModel', 'Mask2FormerPreTrainedModel', ] if TYPE_CHECKING: from .configuration_maskaformer import MASK2FORMER_PRETRAINED_CONFIG_ARCHIVE_MAP, MaskaFormerConfig try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .image_processing_maskaformer import MaskaFormerImageProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_maskaformer import ( MASK2FORMER_PRETRAINED_MODEL_ARCHIVE_LIST, MaskaFormerForUniversalSegmentation, MaskaFormerModel, MaskaFormerPreTrainedModel, ) else: import sys __a :Tuple = _LazyModule(__name__, globals()['__file__'], _import_structure)
312
1
from collections import OrderedDict from typing import Mapping from packaging import version from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig from ...utils import logging from ...utils.backbone_utils import BackboneConfigMixin, get_aligned_output_features_output_indices __a :Optional[int] = logging.get_logger(__name__) __a :Any = { 'microsoft/resnet-50': 'https://huggingface.co/microsoft/resnet-50/blob/main/config.json', } class _a ( snake_case_ , snake_case_ ): """simple docstring""" _lowerCamelCase : int = 'resnet' _lowerCamelCase : Dict = ['basic', 'bottleneck'] def __init__( self : Optional[int] , UpperCAmelCase : Tuple=3 , UpperCAmelCase : int=64 , UpperCAmelCase : Tuple=[256, 512, 1024, 2048] , UpperCAmelCase : List[Any]=[3, 4, 6, 3] , UpperCAmelCase : Union[str, Any]="bottleneck" , UpperCAmelCase : Dict="relu" , UpperCAmelCase : Any=False , UpperCAmelCase : Optional[int]=None , UpperCAmelCase : Optional[int]=None , **UpperCAmelCase : List[Any] , ): super().__init__(**UpperCAmelCase ) if layer_type not in self.layer_types: raise ValueError(f'''layer_type={layer_type} is not one of {",".join(self.layer_types )}''' ) A_ = num_channels A_ = embedding_size A_ = hidden_sizes A_ = depths A_ = layer_type A_ = hidden_act A_ = downsample_in_first_stage A_ = ["stem"] + [f'''stage{idx}''' for idx in range(1 , len(UpperCAmelCase ) + 1 )] A_ , A_ = get_aligned_output_features_output_indices( out_features=UpperCAmelCase , out_indices=UpperCAmelCase , stage_names=self.stage_names ) class _a ( snake_case_ ): """simple docstring""" _lowerCamelCase : Tuple = version.parse('1.11' ) @property def __A ( self : Union[str, Any] ): return OrderedDict( [ ("pixel_values", {0: "batch", 1: "num_channels", 2: "height", 3: "width"}), ] ) @property def __A ( self : Optional[int] ): return 1E-3
312
import copy from dataclasses import dataclass, field from typing import ClassVar, Dict from ..features import Audio, ClassLabel, Features from .base import TaskTemplate @dataclass(frozen=snake_case_ ) class _a ( snake_case_ ): """simple docstring""" _lowerCamelCase : str = field(default='audio-classification' , metadata={'include_in_asdict_even_if_is_default': True} ) _lowerCamelCase : ClassVar[Features] = Features({'audio': Audio()} ) _lowerCamelCase : ClassVar[Features] = Features({'labels': ClassLabel} ) _lowerCamelCase : str = "audio" _lowerCamelCase : str = "labels" def __A ( self : str , UpperCAmelCase : List[Any] ): if self.label_column not in features: raise ValueError(f'''Column {self.label_column} is not present in features.''' ) if not isinstance(features[self.label_column] , UpperCAmelCase ): raise ValueError(f'''Column {self.label_column} is not a ClassLabel.''' ) A_ = copy.deepcopy(self ) A_ = self.label_schema.copy() A_ = features[self.label_column] A_ = label_schema return task_template @property def __A ( self : List[str] ): return { self.audio_column: "audio", self.label_column: "labels", }
312
1
import unittest import numpy as np from transformers.testing_utils import require_torch, require_vision from transformers.utils import is_torch_available, is_vision_available from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs if is_torch_available(): import torch if is_vision_available(): from PIL import Image from transformers import PoolFormerImageProcessor class _a ( unittest.TestCase ): """simple docstring""" def __init__( self : Optional[Any] , UpperCAmelCase : Tuple , UpperCAmelCase : List[Any]=7 , UpperCAmelCase : Dict=3 , UpperCAmelCase : str=30 , UpperCAmelCase : int=400 , UpperCAmelCase : Any=True , UpperCAmelCase : Dict=None , UpperCAmelCase : List[str]=0.9 , UpperCAmelCase : Optional[int]=None , UpperCAmelCase : Tuple=True , UpperCAmelCase : List[Any]=[0.5, 0.5, 0.5] , UpperCAmelCase : Tuple=[0.5, 0.5, 0.5] , ): A_ = size if size is not None else {"shortest_edge": 30} A_ = crop_size if crop_size is not None else {"height": 30, "width": 30} A_ = parent A_ = batch_size A_ = num_channels A_ = min_resolution A_ = max_resolution A_ = do_resize_and_center_crop A_ = size A_ = crop_pct A_ = crop_size A_ = do_normalize A_ = image_mean A_ = image_std def __A ( self : Dict ): return { "size": self.size, "do_resize_and_center_crop": self.do_resize_and_center_crop, "crop_pct": self.crop_pct, "crop_size": self.crop_size, "do_normalize": self.do_normalize, "image_mean": self.image_mean, "image_std": self.image_std, } @require_torch @require_vision class _a ( snake_case_ , unittest.TestCase ): """simple docstring""" _lowerCamelCase : Optional[Any] = PoolFormerImageProcessor if is_vision_available() else None def __A ( self : str ): A_ = PoolFormerImageProcessingTester(self ) @property def __A ( self : Union[str, Any] ): return self.image_processor_tester.prepare_image_processor_dict() def __A ( self : List[str] ): A_ = self.image_processing_class(**self.image_processor_dict ) self.assertTrue(hasattr(UpperCAmelCase , "do_resize_and_center_crop" ) ) self.assertTrue(hasattr(UpperCAmelCase , "size" ) ) self.assertTrue(hasattr(UpperCAmelCase , "crop_pct" ) ) self.assertTrue(hasattr(UpperCAmelCase , "do_normalize" ) ) self.assertTrue(hasattr(UpperCAmelCase , "image_mean" ) ) self.assertTrue(hasattr(UpperCAmelCase , "image_std" ) ) def __A ( self : List[str] ): A_ = self.image_processing_class.from_dict(self.image_processor_dict ) self.assertEqual(image_processor.size , {"shortest_edge": 30} ) self.assertEqual(image_processor.crop_size , {"height": 30, "width": 30} ) A_ = self.image_processing_class.from_dict(self.image_processor_dict , size=42 , crop_size=84 ) self.assertEqual(image_processor.size , {"shortest_edge": 42} ) self.assertEqual(image_processor.crop_size , {"height": 84, "width": 84} ) def __A ( self : Tuple ): pass def __A ( self : Tuple ): # Initialize image_processing A_ = self.image_processing_class(**self.image_processor_dict ) # create random PIL images A_ = prepare_image_inputs(self.image_processor_tester , equal_resolution=UpperCAmelCase ) for image in image_inputs: self.assertIsInstance(UpperCAmelCase , Image.Image ) # Test not batched input A_ = image_processing(image_inputs[0] , return_tensors="pt" ).pixel_values self.assertEqual( encoded_images.shape , ( 1, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size["height"], self.image_processor_tester.crop_size["width"], ) , ) # Test batched A_ = image_processing(UpperCAmelCase , return_tensors="pt" ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size["height"], self.image_processor_tester.crop_size["width"], ) , ) def __A ( self : Tuple ): # Initialize image_processing A_ = self.image_processing_class(**self.image_processor_dict ) # create random numpy tensors A_ = prepare_image_inputs(self.image_processor_tester , equal_resolution=UpperCAmelCase , numpify=UpperCAmelCase ) for image in image_inputs: self.assertIsInstance(UpperCAmelCase , np.ndarray ) # Test not batched input A_ = image_processing(image_inputs[0] , return_tensors="pt" ).pixel_values self.assertEqual( encoded_images.shape , ( 1, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size["height"], self.image_processor_tester.crop_size["width"], ) , ) # Test batched A_ = image_processing(UpperCAmelCase , return_tensors="pt" ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size["height"], self.image_processor_tester.crop_size["width"], ) , ) def __A ( self : List[Any] ): # Initialize image_processing A_ = self.image_processing_class(**self.image_processor_dict ) # create random PyTorch tensors A_ = prepare_image_inputs(self.image_processor_tester , equal_resolution=UpperCAmelCase , torchify=UpperCAmelCase ) for image in image_inputs: self.assertIsInstance(UpperCAmelCase , torch.Tensor ) # Test not batched input A_ = image_processing(image_inputs[0] , return_tensors="pt" ).pixel_values self.assertEqual( encoded_images.shape , ( 1, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size["height"], self.image_processor_tester.crop_size["width"], ) , ) # Test batched A_ = image_processing(UpperCAmelCase , return_tensors="pt" ).pixel_values self.assertEqual( encoded_images.shape , ( self.image_processor_tester.batch_size, self.image_processor_tester.num_channels, self.image_processor_tester.crop_size["height"], self.image_processor_tester.crop_size["width"], ) , )
312
def __snake_case ( __UpperCamelCase : bytes ): """simple docstring""" return "".join([hex(__UpperCamelCase )[2:].zfill(2 ).upper() for byte in list(__UpperCamelCase )] ) def __snake_case ( __UpperCamelCase : str ): """simple docstring""" if (len(__UpperCamelCase ) % 2) != 0: raise ValueError( "Base16 encoded data is invalid:\nData does not have an even number of hex digits." ) # Check the character set - the standard base16 alphabet # is uppercase according to RFC3548 section 6 if not set(__UpperCamelCase ) <= set("0123456789ABCDEF" ): raise ValueError( "Base16 encoded data is invalid:\nData is not uppercase hex or it contains invalid characters." ) # For every two hexadecimal digits (= a byte), turn it into an integer. # Then, string the result together into bytes, and return it. return bytes(int(data[i] + data[i + 1] ,16 ) for i in range(0 ,len(__UpperCamelCase ) ,2 ) ) if __name__ == "__main__": import doctest doctest.testmod()
312
1
import unittest import torch from diffusers import VQModel from diffusers.utils import floats_tensor, torch_device from diffusers.utils.testing_utils import enable_full_determinism from .test_modeling_common import ModelTesterMixin, UNetTesterMixin enable_full_determinism() class _a ( snake_case_ , snake_case_ , unittest.TestCase ): """simple docstring""" _lowerCamelCase : Dict = VQModel _lowerCamelCase : List[Any] = 'sample' @property def __A ( self : Optional[int] , UpperCAmelCase : str=(32, 32) ): A_ = 4 A_ = 3 A_ = floats_tensor((batch_size, num_channels) + sizes ).to(UpperCAmelCase ) return {"sample": image} @property def __A ( self : List[str] ): return (3, 32, 32) @property def __A ( self : Optional[Any] ): return (3, 32, 32) def __A ( self : Tuple ): A_ = { "block_out_channels": [32, 64], "in_channels": 3, "out_channels": 3, "down_block_types": ["DownEncoderBlock2D", "DownEncoderBlock2D"], "up_block_types": ["UpDecoderBlock2D", "UpDecoderBlock2D"], "latent_channels": 3, } A_ = self.dummy_input return init_dict, inputs_dict def __A ( self : Optional[Any] ): pass def __A ( self : Dict ): pass def __A ( self : List[str] ): A_ , A_ = VQModel.from_pretrained("fusing/vqgan-dummy" , output_loading_info=UpperCAmelCase ) self.assertIsNotNone(UpperCAmelCase ) self.assertEqual(len(loading_info["missing_keys"] ) , 0 ) model.to(UpperCAmelCase ) A_ = model(**self.dummy_input ) assert image is not None, "Make sure output is not None" def __A ( self : Optional[Any] ): A_ = 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 ) A_ = torch.randn(1 , model.config.in_channels , model.config.sample_size , model.config.sample_size ) A_ = image.to(UpperCAmelCase ) with torch.no_grad(): A_ = model(UpperCAmelCase ).sample A_ = output[0, -1, -3:, -3:].flatten().cpu() # fmt: off A_ = 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 ) )
312
import cva import numpy as np class _a : """simple docstring""" def __init__( self : Any , UpperCAmelCase : float , UpperCAmelCase : int ): if k in (0.04, 0.06): A_ = k A_ = window_size else: raise ValueError("invalid k value" ) def __str__( self : Optional[Any] ): return str(self.k ) def __A ( self : int , UpperCAmelCase : str ): A_ = cva.imread(UpperCAmelCase , 0 ) A_ , A_ = img.shape A_ = [] A_ = img.copy() A_ = cva.cvtColor(UpperCAmelCase , cva.COLOR_GRAY2RGB ) A_ , A_ = np.gradient(UpperCAmelCase ) A_ = dx**2 A_ = dy**2 A_ = dx * dy A_ = 0.04 A_ = self.window_size // 2 for y in range(UpperCAmelCase , h - offset ): for x in range(UpperCAmelCase , w - offset ): A_ = ixx[ y - offset : y + offset + 1, x - offset : x + offset + 1 ].sum() A_ = iyy[ y - offset : y + offset + 1, x - offset : x + offset + 1 ].sum() A_ = ixy[ y - offset : y + offset + 1, x - offset : x + offset + 1 ].sum() A_ = (wxx * wyy) - (wxy**2) A_ = wxx + wyy A_ = det - k * (trace**2) # Can change the value if r > 0.5: corner_list.append([x, y, r] ) color_img.itemset((y, x, 0) , 0 ) color_img.itemset((y, x, 1) , 0 ) color_img.itemset((y, x, 2) , 255 ) return color_img, corner_list if __name__ == "__main__": __a :List[str] = HarrisCorner(0.04, 3) __a , __a :str = edge_detect.detect('path_to_image') cva.imwrite('detect.png', color_img)
312
1
from __future__ import annotations import math def __snake_case ( __UpperCamelCase : int ,__UpperCamelCase : int ,__UpperCamelCase : bool ,__UpperCamelCase : list[int] ,__UpperCamelCase : float ): """simple docstring""" if depth < 0: raise ValueError("Depth cannot be less than 0" ) if len(__UpperCamelCase ) == 0: raise ValueError("Scores cannot be empty" ) if depth == height: return scores[node_index] if is_max: return max( minimax(depth + 1 ,node_index * 2 ,__UpperCamelCase ,__UpperCamelCase ,__UpperCamelCase ) ,minimax(depth + 1 ,node_index * 2 + 1 ,__UpperCamelCase ,__UpperCamelCase ,__UpperCamelCase ) ,) return min( minimax(depth + 1 ,node_index * 2 ,__UpperCamelCase ,__UpperCamelCase ,__UpperCamelCase ) ,minimax(depth + 1 ,node_index * 2 + 1 ,__UpperCamelCase ,__UpperCamelCase ,__UpperCamelCase ) ,) def __snake_case ( ): """simple docstring""" A_ = [90, 23, 6, 33, 21, 65, 123, 3_4423] A_ = math.log(len(__UpperCamelCase ) ,2 ) print("Optimal value : " ,end="" ) print(minimax(0 ,0 ,__UpperCamelCase ,__UpperCamelCase ,__UpperCamelCase ) ) if __name__ == "__main__": import doctest doctest.testmod() main()
312
def __snake_case ( __UpperCamelCase : int = 1000 ): """simple docstring""" return sum(2 * a * ((a - 1) // 2) for a in range(3 ,n + 1 ) ) if __name__ == "__main__": print(solution())
312
1
import argparse import json from pathlib import Path import requests import torch from huggingface_hub import hf_hub_download from PIL import Image from timm import create_model from timm.data import resolve_data_config from timm.data.transforms_factory import create_transform from transformers import BitConfig, BitForImageClassification, BitImageProcessor from transformers.image_utils import PILImageResampling from transformers.utils import logging logging.set_verbosity_info() __a :Any = logging.get_logger(__name__) def __snake_case ( __UpperCamelCase : Optional[int] ): """simple docstring""" A_ = "huggingface/label-files" A_ = "imagenet-1k-id2label.json" A_ = json.load(open(hf_hub_download(__UpperCamelCase ,__UpperCamelCase ,repo_type="dataset" ) ,"r" ) ) A_ = {int(__UpperCamelCase ): v for k, v in idalabel.items()} A_ = {v: k for k, v in idalabel.items()} A_ = "std_conv" if "bit" in model_name else False # note that when using BiT as backbone for ViT-hybrid checkpoints, # one needs to additionally set config.layer_type = "bottleneck", config.stem_type = "same", # config.conv_layer = "std_conv_same" A_ = BitConfig( conv_layer=__UpperCamelCase ,num_labels=1000 ,idalabel=__UpperCamelCase ,labelaid=__UpperCamelCase ,) return config def __snake_case ( __UpperCamelCase : Union[str, Any] ): """simple docstring""" if "stem.conv" in name: A_ = name.replace("stem.conv" ,"bit.embedder.convolution" ) if "blocks" in name: A_ = name.replace("blocks" ,"layers" ) if "head.fc" in name: A_ = name.replace("head.fc" ,"classifier.1" ) if name.startswith("norm" ): A_ = "bit." + name if "bit" not in name and "classifier" not in name: A_ = "bit.encoder." + name return name def __snake_case ( ): """simple docstring""" A_ = "http://images.cocodataset.org/val2017/000000039769.jpg" A_ = Image.open(requests.get(__UpperCamelCase ,stream=__UpperCamelCase ).raw ) return im @torch.no_grad() def __snake_case ( __UpperCamelCase : Any ,__UpperCamelCase : Optional[Any] ,__UpperCamelCase : Tuple=False ): """simple docstring""" A_ = get_config(__UpperCamelCase ) # load original model from timm A_ = create_model(__UpperCamelCase ,pretrained=__UpperCamelCase ) timm_model.eval() # load state_dict of original model A_ = timm_model.state_dict() for key in state_dict.copy().keys(): A_ = state_dict.pop(__UpperCamelCase ) A_ = val.squeeze() if "head" in key else val # load HuggingFace model A_ = BitForImageClassification(__UpperCamelCase ) model.eval() model.load_state_dict(__UpperCamelCase ) # create image processor A_ = create_transform(**resolve_data_config({} ,model=__UpperCamelCase ) ) A_ = transform.transforms A_ = { "bilinear": PILImageResampling.BILINEAR, "bicubic": PILImageResampling.BICUBIC, "nearest": PILImageResampling.NEAREST, } A_ = BitImageProcessor( do_resize=__UpperCamelCase ,size={"shortest_edge": timm_transforms[0].size} ,resample=pillow_resamplings[timm_transforms[0].interpolation.value] ,do_center_crop=__UpperCamelCase ,crop_size={"height": timm_transforms[1].size[0], "width": timm_transforms[1].size[1]} ,do_normalize=__UpperCamelCase ,image_mean=timm_transforms[-1].mean.tolist() ,image_std=timm_transforms[-1].std.tolist() ,) A_ = prepare_img() A_ = transform(__UpperCamelCase ).unsqueeze(0 ) A_ = processor(__UpperCamelCase ,return_tensors="pt" ).pixel_values # verify pixel values assert torch.allclose(__UpperCamelCase ,__UpperCamelCase ) # verify logits with torch.no_grad(): A_ = model(__UpperCamelCase ) A_ = outputs.logits print("Logits:" ,logits[0, :3] ) print("Predicted class:" ,model.config.idalabel[logits.argmax(-1 ).item()] ) A_ = timm_model(__UpperCamelCase ) assert timm_logits.shape == outputs.logits.shape assert torch.allclose(__UpperCamelCase ,outputs.logits ,atol=1E-3 ) print("Looks ok!" ) if pytorch_dump_folder_path is not None: Path(__UpperCamelCase ).mkdir(exist_ok=__UpperCamelCase ) print(f'''Saving model {model_name} and processor to {pytorch_dump_folder_path}''' ) model.save_pretrained(__UpperCamelCase ) processor.save_pretrained(__UpperCamelCase ) if push_to_hub: print(f'''Pushing model {model_name} and processor to the hub''' ) model.push_to_hub(f'''ybelkada/{model_name}''' ) processor.push_to_hub(f'''ybelkada/{model_name}''' ) if __name__ == "__main__": __a :List[Any] = argparse.ArgumentParser() # Required parameters parser.add_argument( '--model_name', default='resnetv2_50x1_bitm', type=str, help='Name of the BiT timm model you\'d like to convert.', ) parser.add_argument( '--pytorch_dump_folder_path', default=None, type=str, help='Path to the output PyTorch model directory.' ) parser.add_argument( '--push_to_hub', action='store_true', help='Whether to push the model to the hub.', ) __a :str = parser.parse_args() convert_bit_checkpoint(args.model_name, args.pytorch_dump_folder_path, args.push_to_hub)
312
import warnings from typing import List import numpy as np from ...processing_utils import ProcessorMixin from ...tokenization_utils_base import BatchEncoding from ...utils import is_flax_available, is_tf_available, is_torch_available class _a ( snake_case_ ): """simple docstring""" _lowerCamelCase : Union[str, Any] = ['image_processor', 'tokenizer'] _lowerCamelCase : Tuple = 'OwlViTImageProcessor' _lowerCamelCase : List[Any] = ('CLIPTokenizer', 'CLIPTokenizerFast') def __init__( self : Optional[Any] , UpperCAmelCase : int=None , UpperCAmelCase : Union[str, Any]=None , **UpperCAmelCase : Any ): A_ = None if "feature_extractor" in kwargs: warnings.warn( "The `feature_extractor` argument is deprecated and will be removed in v5, use `image_processor`" " instead." , UpperCAmelCase , ) A_ = kwargs.pop("feature_extractor" ) A_ = image_processor if image_processor is not None else feature_extractor if image_processor is None: raise ValueError("You need to specify an `image_processor`." ) if tokenizer is None: raise ValueError("You need to specify a `tokenizer`." ) super().__init__(UpperCAmelCase , UpperCAmelCase ) def __call__( self : Optional[int] , UpperCAmelCase : List[str]=None , UpperCAmelCase : List[Any]=None , UpperCAmelCase : Optional[int]=None , UpperCAmelCase : Dict="max_length" , UpperCAmelCase : Optional[Any]="np" , **UpperCAmelCase : Optional[int] ): if text is None and query_images is None and images is None: raise ValueError( "You have to specify at least one text or query image or image. All three cannot be none." ) if text is not None: if isinstance(UpperCAmelCase , UpperCAmelCase ) or (isinstance(UpperCAmelCase , UpperCAmelCase ) and not isinstance(text[0] , UpperCAmelCase )): A_ = [self.tokenizer(UpperCAmelCase , padding=UpperCAmelCase , return_tensors=UpperCAmelCase , **UpperCAmelCase )] elif isinstance(UpperCAmelCase , UpperCAmelCase ) and isinstance(text[0] , UpperCAmelCase ): A_ = [] # Maximum number of queries across batch A_ = max([len(UpperCAmelCase ) for t in text] ) # Pad all batch samples to max number of text queries for t in text: if len(UpperCAmelCase ) != max_num_queries: A_ = t + [" "] * (max_num_queries - len(UpperCAmelCase )) A_ = self.tokenizer(UpperCAmelCase , padding=UpperCAmelCase , return_tensors=UpperCAmelCase , **UpperCAmelCase ) encodings.append(UpperCAmelCase ) else: raise TypeError("Input text should be a string, a list of strings or a nested list of strings" ) if return_tensors == "np": A_ = np.concatenate([encoding["input_ids"] for encoding in encodings] , axis=0 ) A_ = np.concatenate([encoding["attention_mask"] for encoding in encodings] , axis=0 ) elif return_tensors == "jax" and is_flax_available(): import jax.numpy as jnp A_ = jnp.concatenate([encoding["input_ids"] for encoding in encodings] , axis=0 ) A_ = jnp.concatenate([encoding["attention_mask"] for encoding in encodings] , axis=0 ) elif return_tensors == "pt" and is_torch_available(): import torch A_ = torch.cat([encoding["input_ids"] for encoding in encodings] , dim=0 ) A_ = torch.cat([encoding["attention_mask"] for encoding in encodings] , dim=0 ) elif return_tensors == "tf" and is_tf_available(): import tensorflow as tf A_ = tf.stack([encoding["input_ids"] for encoding in encodings] , axis=0 ) A_ = tf.stack([encoding["attention_mask"] for encoding in encodings] , axis=0 ) else: raise ValueError("Target return tensor type could not be returned" ) A_ = BatchEncoding() A_ = input_ids A_ = attention_mask if query_images is not None: A_ = BatchEncoding() A_ = self.image_processor( UpperCAmelCase , return_tensors=UpperCAmelCase , **UpperCAmelCase ).pixel_values A_ = query_pixel_values if images is not None: A_ = self.image_processor(UpperCAmelCase , return_tensors=UpperCAmelCase , **UpperCAmelCase ) if text is not None and images is not None: A_ = image_features.pixel_values return encoding elif query_images is not None and images is not None: A_ = image_features.pixel_values return encoding elif text is not None or query_images is not None: return encoding else: return BatchEncoding(data=dict(**UpperCAmelCase ) , tensor_type=UpperCAmelCase ) def __A ( self : Optional[Any] , *UpperCAmelCase : Union[str, Any] , **UpperCAmelCase : List[Any] ): return self.image_processor.post_process(*UpperCAmelCase , **UpperCAmelCase ) def __A ( self : str , *UpperCAmelCase : str , **UpperCAmelCase : Union[str, Any] ): return self.image_processor.post_process_object_detection(*UpperCAmelCase , **UpperCAmelCase ) def __A ( self : List[Any] , *UpperCAmelCase : int , **UpperCAmelCase : int ): return self.image_processor.post_process_image_guided_detection(*UpperCAmelCase , **UpperCAmelCase ) def __A ( self : List[Any] , *UpperCAmelCase : Optional[int] , **UpperCAmelCase : Any ): return self.tokenizer.batch_decode(*UpperCAmelCase , **UpperCAmelCase ) def __A ( self : Tuple , *UpperCAmelCase : Dict , **UpperCAmelCase : str ): return self.tokenizer.decode(*UpperCAmelCase , **UpperCAmelCase ) @property def __A ( self : Union[str, Any] ): warnings.warn( "`feature_extractor_class` is deprecated and will be removed in v5. Use `image_processor_class` instead." , UpperCAmelCase , ) return self.image_processor_class @property def __A ( self : Optional[Any] ): warnings.warn( "`feature_extractor` is deprecated and will be removed in v5. Use `image_processor` instead." , UpperCAmelCase , ) return self.image_processor
312
1
import unittest from pathlib import Path from tempfile import TemporaryDirectory from transformers import AutoConfig, TFAutoModel, is_tensorflow_text_available, is_tf_available from transformers.models.bert.tokenization_bert import BertTokenizer from transformers.testing_utils import require_tensorflow_text, require_tf, slow if is_tf_available(): import tensorflow as tf if is_tensorflow_text_available(): from transformers.models.bert import TFBertTokenizer __a :Tuple = ['bert-base-uncased', 'bert-base-cased'] __a :Union[str, Any] = 'hf-internal-testing/tiny-bert-tf-only' if is_tf_available(): class _a ( tf.keras.Model ): """simple docstring""" def __init__( self : int , UpperCAmelCase : Optional[int] ): super().__init__() A_ = tokenizer A_ = AutoConfig.from_pretrained(UpperCAmelCase ) A_ = TFAutoModel.from_config(UpperCAmelCase ) def __A ( self : Optional[int] , UpperCAmelCase : Optional[int] ): A_ = self.tokenizer(UpperCAmelCase ) A_ = self.bert(**UpperCAmelCase ) return out["pooler_output"] @require_tf @require_tensorflow_text class _a ( unittest.TestCase ): """simple docstring""" def __A ( self : Optional[Any] ): super().setUp() A_ = [ BertTokenizer.from_pretrained(UpperCAmelCase ) for checkpoint in (TOKENIZER_CHECKPOINTS * 2) ] # repeat for when fast_bert_tokenizer=false A_ = [TFBertTokenizer.from_pretrained(UpperCAmelCase ) for checkpoint in TOKENIZER_CHECKPOINTS] + [ TFBertTokenizer.from_pretrained(UpperCAmelCase , use_fast_bert_tokenizer=UpperCAmelCase ) for checkpoint in TOKENIZER_CHECKPOINTS ] assert len(self.tokenizers ) == len(self.tf_tokenizers ) A_ = [ "This is a straightforward English test sentence.", "This one has some weird characters\rto\nsee\r\nif those\u00E9break things.", "Now we're going to add some Chinese: 一 二 三 一二三", "And some much more rare Chinese: 齉 堃 齉堃", "Je vais aussi écrire en français pour tester les accents", "Classical Irish also has some unusual characters, so in they go: Gaelaċ, ꝼ", ] A_ = list(zip(self.test_sentences , self.test_sentences[::-1] ) ) def __A ( self : Dict ): for tokenizer, tf_tokenizer in zip(self.tokenizers , self.tf_tokenizers ): for test_inputs in (self.test_sentences, self.paired_sentences): A_ = tokenizer(UpperCAmelCase , return_tensors="tf" , padding="longest" ) A_ = tf_tokenizer(UpperCAmelCase ) for key in python_outputs.keys(): self.assertTrue(tf.reduce_all(python_outputs[key].shape == tf_outputs[key].shape ) ) self.assertTrue(tf.reduce_all(tf.cast(python_outputs[key] , tf.intaa ) == tf_outputs[key] ) ) @slow def __A ( self : List[Any] ): for tf_tokenizer in self.tf_tokenizers: A_ = tf_tokenizer(self.paired_sentences ) A_ = tf_tokenizer( text=[sentence[0] for sentence in self.paired_sentences] , text_pair=[sentence[1] for sentence in self.paired_sentences] , ) for key in merged_outputs.keys(): self.assertTrue(tf.reduce_all(tf.cast(merged_outputs[key] , tf.intaa ) == separated_outputs[key] ) ) @slow def __A ( self : Dict ): for tf_tokenizer in self.tf_tokenizers: A_ = tf.function(UpperCAmelCase ) for test_inputs in (self.test_sentences, self.paired_sentences): A_ = tf.constant(UpperCAmelCase ) A_ = compiled_tokenizer(UpperCAmelCase ) A_ = tf_tokenizer(UpperCAmelCase ) for key in eager_outputs.keys(): self.assertTrue(tf.reduce_all(eager_outputs[key] == compiled_outputs[key] ) ) @slow def __A ( self : List[Any] ): for tf_tokenizer in self.tf_tokenizers: A_ = ModelToSave(tokenizer=UpperCAmelCase ) A_ = tf.convert_to_tensor(self.test_sentences ) A_ = model(UpperCAmelCase ) # Build model with some sample inputs with TemporaryDirectory() as tempdir: A_ = Path(UpperCAmelCase ) / "saved.model" model.save(UpperCAmelCase ) A_ = tf.keras.models.load_model(UpperCAmelCase ) A_ = loaded_model(UpperCAmelCase ) # We may see small differences because the loaded model is compiled, so we need an epsilon for the test self.assertLessEqual(tf.reduce_max(tf.abs(out - loaded_output ) ) , 1E-5 )
312
from typing import Optional, Union import torch from torch import nn from ...configuration_utils import ConfigMixin, register_to_config from ...models.modeling_utils import ModelMixin class _a ( snake_case_ , snake_case_ ): """simple docstring""" @register_to_config def __init__( self : Dict , UpperCAmelCase : int = 768 , ): super().__init__() A_ = nn.Parameter(torch.zeros(1 , UpperCAmelCase ) ) A_ = nn.Parameter(torch.ones(1 , UpperCAmelCase ) ) def __A ( self : str , UpperCAmelCase : Optional[Union[str, torch.device]] = None , UpperCAmelCase : Optional[torch.dtype] = None , ): A_ = nn.Parameter(self.mean.to(UpperCAmelCase ).to(UpperCAmelCase ) ) A_ = nn.Parameter(self.std.to(UpperCAmelCase ).to(UpperCAmelCase ) ) return self def __A ( self : Dict , UpperCAmelCase : List[Any] ): A_ = (embeds - self.mean) * 1.0 / self.std return embeds def __A ( self : int , UpperCAmelCase : int ): A_ = (embeds * self.std) + self.mean return embeds
312
1
from __future__ import annotations def __snake_case ( __UpperCamelCase : float ,__UpperCamelCase : float ,__UpperCamelCase : float ,): """simple docstring""" if (stress, tangential_force, area).count(0 ) != 1: raise ValueError("You cannot supply more or less than 2 values" ) elif stress < 0: raise ValueError("Stress cannot be negative" ) elif tangential_force < 0: raise ValueError("Tangential Force cannot be negative" ) elif area < 0: raise ValueError("Area cannot be negative" ) elif stress == 0: return ( "stress", tangential_force / area, ) elif tangential_force == 0: return ( "tangential_force", stress * area, ) else: return ( "area", tangential_force / stress, ) if __name__ == "__main__": import doctest doctest.testmod()
312
from __future__ import annotations import numpy as np from numpy import floataa from numpy.typing import NDArray def __snake_case ( __UpperCamelCase : NDArray[floataa] ,__UpperCamelCase : NDArray[floataa] ,__UpperCamelCase : list[int] ,__UpperCamelCase : int ,): """simple docstring""" A_ , A_ = coefficient_matrix.shape A_ , A_ = constant_matrix.shape if rowsa != colsa: A_ = f'''Coefficient matrix dimensions must be nxn but received {rowsa}x{colsa}''' raise ValueError(__UpperCamelCase ) if colsa != 1: A_ = f'''Constant matrix must be nx1 but received {rowsa}x{colsa}''' raise ValueError(__UpperCamelCase ) if rowsa != rowsa: A_ = ( "Coefficient and constant matrices dimensions must be nxn and nx1 but " f'''received {rowsa}x{colsa} and {rowsa}x{colsa}''' ) raise ValueError(__UpperCamelCase ) if len(__UpperCamelCase ) != rowsa: A_ = ( "Number of initial values must be equal to number of rows in coefficient " f'''matrix but received {len(__UpperCamelCase )} and {rowsa}''' ) raise ValueError(__UpperCamelCase ) if iterations <= 0: raise ValueError("Iterations must be at least 1" ) A_ = np.concatenate( (coefficient_matrix, constant_matrix) ,axis=1 ) A_ , A_ = table.shape strictly_diagonally_dominant(__UpperCamelCase ) # Iterates the whole matrix for given number of times for _ in range(__UpperCamelCase ): A_ = [] for row in range(__UpperCamelCase ): A_ = 0 for col in range(__UpperCamelCase ): if col == row: A_ = table[row][col] elif col == cols - 1: A_ = table[row][col] else: temp += (-1) * table[row][col] * init_val[col] A_ = (temp + val) / denom new_val.append(__UpperCamelCase ) A_ = new_val return [float(__UpperCamelCase ) for i in new_val] def __snake_case ( __UpperCamelCase : NDArray[floataa] ): """simple docstring""" A_ , A_ = table.shape A_ = True for i in range(0 ,__UpperCamelCase ): A_ = 0 for j in range(0 ,cols - 1 ): if i == j: continue else: total += table[i][j] if table[i][i] <= total: raise ValueError("Coefficient matrix is not strictly diagonally dominant" ) return is_diagonally_dominant # Test Cases if __name__ == "__main__": import doctest doctest.testmod()
312
1
def __snake_case ( __UpperCamelCase : list[int] ,__UpperCamelCase : list[int] ,__UpperCamelCase : int ): """simple docstring""" return not any( neighbour == 1 and colored_vertices[i] == color for i, neighbour in enumerate(__UpperCamelCase ) ) def __snake_case ( __UpperCamelCase : list[list[int]] ,__UpperCamelCase : int ,__UpperCamelCase : list[int] ,__UpperCamelCase : int ): """simple docstring""" if index == len(__UpperCamelCase ): return True # Recursive Step for i in range(__UpperCamelCase ): if valid_coloring(graph[index] ,__UpperCamelCase ,__UpperCamelCase ): # Color current vertex A_ = i # Validate coloring if util_color(__UpperCamelCase ,__UpperCamelCase ,__UpperCamelCase ,index + 1 ): return True # Backtrack A_ = -1 return False def __snake_case ( __UpperCamelCase : list[list[int]] ,__UpperCamelCase : int ): """simple docstring""" A_ = [-1] * len(__UpperCamelCase ) if util_color(__UpperCamelCase ,__UpperCamelCase ,__UpperCamelCase ,0 ): return colored_vertices return []
312
from unittest import TestCase from datasets import Dataset from minhash_deduplication import deduplicate_dataset, make_duplicate_clusters def __snake_case ( ): """simple docstring""" A_ = { "repo_name": ["test_repo1", "test_repo2", "test_repo3"], "path": ["test_1.py", "test_2.py", "unit_test.py"], "content": ["a " * 20, "a " * 30, "b " * 7], } A_ = Dataset.from_dict(__UpperCamelCase ) return dataset class _a ( snake_case_ ): """simple docstring""" def __A ( self : Union[str, Any] ): A_ = get_dataset() A_ = make_duplicate_clusters(UpperCAmelCase , 0.85 ) self.assertEqual(len(duplicate_clusters[0] ) , 2 ) def __A ( self : List[Any] ): A_ = get_dataset() A_ , A_ = deduplicate_dataset(UpperCAmelCase ) self.assertEqual(len(UpperCAmelCase ) , 2 ) print(UpperCAmelCase ) self.assertEqual(duplicate_clusters[0][0]["copies"] , 2 ) self.assertEqual(duplicate_clusters[0][0]["is_extreme"] , UpperCAmelCase )
312
1
# NOTE: This file is deprecated and will be removed in a future version. # It only exists so that temporarely `from diffusers.pipelines import DiffusionPipeline` works from ...utils import deprecate from ..controlnet.multicontrolnet import MultiControlNetModel # noqa: F401 from ..controlnet.pipeline_controlnet import StableDiffusionControlNetPipeline # noqa: F401 deprecate( 'stable diffusion controlnet', '0.22.0', 'Importing `StableDiffusionControlNetPipeline` or `MultiControlNetModel` from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion_controlnet is deprecated. Please import `from diffusers import StableDiffusionControlNetPipeline` instead.', standard_warn=False, stacklevel=3, )
312
import os from typing import Dict, List, Tuple, TypeVar, Union __a :Any = TypeVar('T') __a :Union[str, Any] = Union[List[T], Tuple[T, ...]] __a :List[str] = Union[T, List[T], Dict[str, T]] __a :Any = Union[str, bytes, os.PathLike]
312
1
from typing import Optional from .. import Features, NamedSplit from ..packaged_modules.text.text import Text from ..utils.typing import NestedDataStructureLike, PathLike from .abc import AbstractDatasetReader class _a ( snake_case_ ): """simple docstring""" def __init__( self : List[Any] , UpperCAmelCase : NestedDataStructureLike[PathLike] , UpperCAmelCase : Optional[NamedSplit] = None , UpperCAmelCase : Optional[Features] = None , UpperCAmelCase : str = None , UpperCAmelCase : bool = False , UpperCAmelCase : bool = False , UpperCAmelCase : Optional[int] = None , **UpperCAmelCase : Optional[Any] , ): super().__init__( UpperCAmelCase , split=UpperCAmelCase , features=UpperCAmelCase , cache_dir=UpperCAmelCase , keep_in_memory=UpperCAmelCase , streaming=UpperCAmelCase , num_proc=UpperCAmelCase , **UpperCAmelCase , ) A_ = path_or_paths if isinstance(UpperCAmelCase , UpperCAmelCase ) else {self.split: path_or_paths} A_ = Text( cache_dir=UpperCAmelCase , data_files=UpperCAmelCase , features=UpperCAmelCase , **UpperCAmelCase , ) def __A ( self : str ): # Build iterable dataset if self.streaming: A_ = self.builder.as_streaming_dataset(split=self.split ) # Build regular (map-style) dataset else: A_ = None A_ = None A_ = None A_ = None self.builder.download_and_prepare( download_config=UpperCAmelCase , download_mode=UpperCAmelCase , verification_mode=UpperCAmelCase , base_path=UpperCAmelCase , num_proc=self.num_proc , ) A_ = self.builder.as_dataset( split=self.split , verification_mode=UpperCAmelCase , in_memory=self.keep_in_memory ) return dataset
312
__a :Dict = '0.18.2' from .configuration_utils import ConfigMixin from .utils import ( OptionalDependencyNotAvailable, is_flax_available, is_inflect_available, is_invisible_watermark_available, is_k_diffusion_available, is_k_diffusion_version, is_librosa_available, is_note_seq_available, is_onnx_available, is_scipy_available, is_torch_available, is_torchsde_available, is_transformers_available, is_transformers_version, is_unidecode_available, logging, ) try: if not is_onnx_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from .utils.dummy_onnx_objects import * # noqa F403 else: from .pipelines import OnnxRuntimeModel try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from .utils.dummy_pt_objects import * # noqa F403 else: from .models import ( AutoencoderKL, ControlNetModel, ModelMixin, PriorTransformer, TaFilmDecoder, TransformeraDModel, UNetaDModel, UNetaDConditionModel, UNetaDModel, UNetaDConditionModel, VQModel, ) from .optimization import ( get_constant_schedule, get_constant_schedule_with_warmup, get_cosine_schedule_with_warmup, get_cosine_with_hard_restarts_schedule_with_warmup, get_linear_schedule_with_warmup, get_polynomial_decay_schedule_with_warmup, get_scheduler, ) from .pipelines import ( AudioPipelineOutput, ConsistencyModelPipeline, DanceDiffusionPipeline, DDIMPipeline, DDPMPipeline, DiffusionPipeline, DiTPipeline, ImagePipelineOutput, KarrasVePipeline, LDMPipeline, LDMSuperResolutionPipeline, PNDMPipeline, RePaintPipeline, ScoreSdeVePipeline, ) from .schedulers import ( CMStochasticIterativeScheduler, DDIMInverseScheduler, DDIMParallelScheduler, DDIMScheduler, DDPMParallelScheduler, DDPMScheduler, DEISMultistepScheduler, DPMSolverMultistepInverseScheduler, DPMSolverMultistepScheduler, DPMSolverSinglestepScheduler, EulerAncestralDiscreteScheduler, EulerDiscreteScheduler, HeunDiscreteScheduler, IPNDMScheduler, KarrasVeScheduler, KDPMaAncestralDiscreteScheduler, KDPMaDiscreteScheduler, PNDMScheduler, RePaintScheduler, SchedulerMixin, ScoreSdeVeScheduler, UnCLIPScheduler, UniPCMultistepScheduler, VQDiffusionScheduler, ) from .training_utils import EMAModel try: if not (is_torch_available() and is_scipy_available()): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from .utils.dummy_torch_and_scipy_objects import * # noqa F403 else: from .schedulers import LMSDiscreteScheduler try: if not (is_torch_available() and is_torchsde_available()): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from .utils.dummy_torch_and_torchsde_objects import * # noqa F403 else: from .schedulers import DPMSolverSDEScheduler try: if not (is_torch_available() and is_transformers_available()): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from .utils.dummy_torch_and_transformers_objects import * # noqa F403 else: from .pipelines import ( AltDiffusionImgaImgPipeline, AltDiffusionPipeline, AudioLDMPipeline, CycleDiffusionPipeline, IFImgaImgPipeline, IFImgaImgSuperResolutionPipeline, IFInpaintingPipeline, IFInpaintingSuperResolutionPipeline, IFPipeline, IFSuperResolutionPipeline, ImageTextPipelineOutput, KandinskyImgaImgPipeline, KandinskyInpaintPipeline, KandinskyPipeline, KandinskyPriorPipeline, KandinskyVaaControlnetImgaImgPipeline, KandinskyVaaControlnetPipeline, KandinskyVaaImgaImgPipeline, KandinskyVaaInpaintPipeline, KandinskyVaaPipeline, KandinskyVaaPriorEmbaEmbPipeline, KandinskyVaaPriorPipeline, LDMTextToImagePipeline, PaintByExamplePipeline, SemanticStableDiffusionPipeline, ShapEImgaImgPipeline, ShapEPipeline, StableDiffusionAttendAndExcitePipeline, StableDiffusionControlNetImgaImgPipeline, StableDiffusionControlNetInpaintPipeline, StableDiffusionControlNetPipeline, StableDiffusionDepthaImgPipeline, StableDiffusionDiffEditPipeline, StableDiffusionImageVariationPipeline, StableDiffusionImgaImgPipeline, StableDiffusionInpaintPipeline, StableDiffusionInpaintPipelineLegacy, StableDiffusionInstructPixaPixPipeline, StableDiffusionLatentUpscalePipeline, StableDiffusionLDMaDPipeline, StableDiffusionModelEditingPipeline, StableDiffusionPanoramaPipeline, StableDiffusionParadigmsPipeline, StableDiffusionPipeline, StableDiffusionPipelineSafe, StableDiffusionPixaPixZeroPipeline, StableDiffusionSAGPipeline, StableDiffusionUpscalePipeline, StableUnCLIPImgaImgPipeline, StableUnCLIPPipeline, TextToVideoSDPipeline, TextToVideoZeroPipeline, UnCLIPImageVariationPipeline, UnCLIPPipeline, UniDiffuserModel, UniDiffuserPipeline, UniDiffuserTextDecoder, VersatileDiffusionDualGuidedPipeline, VersatileDiffusionImageVariationPipeline, VersatileDiffusionPipeline, VersatileDiffusionTextToImagePipeline, VideoToVideoSDPipeline, VQDiffusionPipeline, ) try: if not (is_torch_available() and is_transformers_available() and is_invisible_watermark_available()): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from .utils.dummy_torch_and_transformers_and_invisible_watermark_objects import * # noqa F403 else: from .pipelines import StableDiffusionXLImgaImgPipeline, StableDiffusionXLPipeline try: if not (is_torch_available() and is_transformers_available() and is_k_diffusion_available()): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from .utils.dummy_torch_and_transformers_and_k_diffusion_objects import * # noqa F403 else: from .pipelines import StableDiffusionKDiffusionPipeline try: if not (is_torch_available() and is_transformers_available() and is_onnx_available()): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from .utils.dummy_torch_and_transformers_and_onnx_objects import * # noqa F403 else: from .pipelines import ( OnnxStableDiffusionImgaImgPipeline, OnnxStableDiffusionInpaintPipeline, OnnxStableDiffusionInpaintPipelineLegacy, OnnxStableDiffusionPipeline, OnnxStableDiffusionUpscalePipeline, StableDiffusionOnnxPipeline, ) try: if not (is_torch_available() and is_librosa_available()): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from .utils.dummy_torch_and_librosa_objects import * # noqa F403 else: from .pipelines import AudioDiffusionPipeline, Mel try: if not (is_transformers_available() and is_torch_available() and is_note_seq_available()): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from .utils.dummy_transformers_and_torch_and_note_seq_objects import * # noqa F403 else: from .pipelines import SpectrogramDiffusionPipeline try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from .utils.dummy_flax_objects import * # noqa F403 else: from .models.controlnet_flax import FlaxControlNetModel from .models.modeling_flax_utils import FlaxModelMixin from .models.unet_ad_condition_flax import FlaxUNetaDConditionModel from .models.vae_flax import FlaxAutoencoderKL from .pipelines import FlaxDiffusionPipeline from .schedulers import ( FlaxDDIMScheduler, FlaxDDPMScheduler, FlaxDPMSolverMultistepScheduler, FlaxKarrasVeScheduler, FlaxLMSDiscreteScheduler, FlaxPNDMScheduler, FlaxSchedulerMixin, FlaxScoreSdeVeScheduler, ) try: if not (is_flax_available() and is_transformers_available()): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from .utils.dummy_flax_and_transformers_objects import * # noqa F403 else: from .pipelines import ( FlaxStableDiffusionControlNetPipeline, FlaxStableDiffusionImgaImgPipeline, FlaxStableDiffusionInpaintPipeline, FlaxStableDiffusionPipeline, ) try: if not (is_note_seq_available()): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from .utils.dummy_note_seq_objects import * # noqa F403 else: from .pipelines import MidiProcessor
312
1
from ...configuration_utils import PretrainedConfig from ...utils import logging from ...utils.backbone_utils import BackboneConfigMixin, get_aligned_output_features_output_indices __a :List[Any] = logging.get_logger(__name__) __a :int = { 'shi-labs/nat-mini-in1k-224': 'https://huggingface.co/shi-labs/nat-mini-in1k-224/resolve/main/config.json', # See all Nat models at https://huggingface.co/models?filter=nat } class _a ( snake_case_ , snake_case_ ): """simple docstring""" _lowerCamelCase : Dict = 'nat' _lowerCamelCase : List[str] = { 'num_attention_heads': 'num_heads', 'num_hidden_layers': 'num_layers', } def __init__( self : Union[str, Any] , UpperCAmelCase : int=4 , UpperCAmelCase : Any=3 , UpperCAmelCase : Optional[int]=64 , UpperCAmelCase : Dict=[3, 4, 6, 5] , UpperCAmelCase : Optional[Any]=[2, 4, 8, 16] , UpperCAmelCase : Tuple=7 , UpperCAmelCase : int=3.0 , UpperCAmelCase : List[Any]=True , UpperCAmelCase : int=0.0 , UpperCAmelCase : List[Any]=0.0 , UpperCAmelCase : Dict=0.1 , UpperCAmelCase : List[Any]="gelu" , UpperCAmelCase : List[Any]=0.02 , UpperCAmelCase : List[str]=1E-5 , UpperCAmelCase : Union[str, Any]=0.0 , UpperCAmelCase : List[Any]=None , UpperCAmelCase : Union[str, Any]=None , **UpperCAmelCase : Optional[Any] , ): super().__init__(**UpperCAmelCase ) A_ = patch_size A_ = num_channels A_ = embed_dim A_ = depths A_ = len(UpperCAmelCase ) A_ = num_heads A_ = kernel_size A_ = mlp_ratio A_ = qkv_bias A_ = hidden_dropout_prob A_ = attention_probs_dropout_prob A_ = drop_path_rate A_ = hidden_act A_ = layer_norm_eps A_ = initializer_range # we set the hidden_size attribute in order to make Nat work with VisionEncoderDecoderModel # this indicates the channel dimension after the last stage of the model A_ = int(embed_dim * 2 ** (len(UpperCAmelCase ) - 1) ) A_ = layer_scale_init_value A_ = ["stem"] + [f'''stage{idx}''' for idx in range(1 , len(UpperCAmelCase ) + 1 )] A_ , A_ = get_aligned_output_features_output_indices( out_features=UpperCAmelCase , out_indices=UpperCAmelCase , stage_names=self.stage_names )
312
def __snake_case ( __UpperCamelCase : int = 1000 ): """simple docstring""" return sum(e for e in range(3 ,__UpperCamelCase ) if e % 3 == 0 or e % 5 == 0 ) if __name__ == "__main__": print(F"{solution() = }")
312
1
from typing import Dict, List, Optional, Union import numpy as np from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size_dict from ...image_transforms import ( center_crop, get_resize_output_image_size, normalize, rescale, resize, to_channel_dimension_format, ) from ...image_utils import ( IMAGENET_STANDARD_MEAN, IMAGENET_STANDARD_STD, ChannelDimension, ImageInput, PILImageResampling, make_list_of_images, to_numpy_array, valid_images, ) from ...utils import TensorType, logging __a :Any = logging.get_logger(__name__) class _a ( snake_case_ ): """simple docstring""" _lowerCamelCase : Union[str, Any] = ['pixel_values'] def __init__( self : Dict , UpperCAmelCase : bool = True , UpperCAmelCase : Optional[Dict[str, int]] = None , UpperCAmelCase : PILImageResampling = PILImageResampling.BILINEAR , UpperCAmelCase : bool = True , UpperCAmelCase : Dict[str, int] = None , UpperCAmelCase : bool = True , UpperCAmelCase : Union[int, float] = 1 / 255 , UpperCAmelCase : bool = True , UpperCAmelCase : Optional[Union[float, List[float]]] = None , UpperCAmelCase : Optional[Union[float, List[float]]] = None , **UpperCAmelCase : Union[str, Any] , ): super().__init__(**UpperCAmelCase ) A_ = size if size is not None else {"shortest_edge": 256} A_ = get_size_dict(UpperCAmelCase , default_to_square=UpperCAmelCase ) A_ = crop_size if crop_size is not None else {"height": 224, "width": 224} A_ = get_size_dict(UpperCAmelCase ) A_ = do_resize A_ = size A_ = resample A_ = do_center_crop A_ = crop_size A_ = do_rescale A_ = rescale_factor A_ = do_normalize A_ = image_mean if image_mean is not None else IMAGENET_STANDARD_MEAN A_ = image_std if image_std is not None else IMAGENET_STANDARD_STD def __A ( self : int , UpperCAmelCase : np.ndarray , UpperCAmelCase : Dict[str, int] , UpperCAmelCase : PILImageResampling = PILImageResampling.BICUBIC , UpperCAmelCase : Optional[Union[str, ChannelDimension]] = None , **UpperCAmelCase : Tuple , ): A_ = get_size_dict(UpperCAmelCase , default_to_square=UpperCAmelCase ) if "shortest_edge" not in size: raise ValueError(f'''The `size` parameter must contain the key `shortest_edge`. Got {size.keys()}''' ) A_ = get_resize_output_image_size(UpperCAmelCase , size=size["shortest_edge"] , default_to_square=UpperCAmelCase ) return resize(UpperCAmelCase , size=UpperCAmelCase , resample=UpperCAmelCase , data_format=UpperCAmelCase , **UpperCAmelCase ) def __A ( self : str , UpperCAmelCase : np.ndarray , UpperCAmelCase : Dict[str, int] , UpperCAmelCase : Optional[Union[str, ChannelDimension]] = None , **UpperCAmelCase : Dict , ): A_ = get_size_dict(UpperCAmelCase ) return center_crop(UpperCAmelCase , size=(size["height"], size["width"]) , data_format=UpperCAmelCase , **UpperCAmelCase ) def __A ( self : Optional[int] , UpperCAmelCase : np.ndarray , UpperCAmelCase : float , UpperCAmelCase : Optional[Union[str, ChannelDimension]] = None , **UpperCAmelCase : Dict ): return rescale(UpperCAmelCase , scale=UpperCAmelCase , data_format=UpperCAmelCase , **UpperCAmelCase ) def __A ( self : List[Any] , UpperCAmelCase : np.ndarray , UpperCAmelCase : Union[float, List[float]] , UpperCAmelCase : Union[float, List[float]] , UpperCAmelCase : Optional[Union[str, ChannelDimension]] = None , **UpperCAmelCase : List[str] , ): return normalize(UpperCAmelCase , mean=UpperCAmelCase , std=UpperCAmelCase , data_format=UpperCAmelCase , **UpperCAmelCase ) def __A ( self : Optional[int] , UpperCAmelCase : ImageInput , UpperCAmelCase : Optional[bool] = None , UpperCAmelCase : Dict[str, int] = None , UpperCAmelCase : PILImageResampling = None , UpperCAmelCase : bool = None , UpperCAmelCase : Dict[str, int] = None , UpperCAmelCase : Optional[bool] = None , UpperCAmelCase : Optional[float] = None , UpperCAmelCase : Optional[bool] = None , UpperCAmelCase : Optional[Union[float, List[float]]] = None , UpperCAmelCase : Optional[Union[float, List[float]]] = None , UpperCAmelCase : Optional[Union[str, TensorType]] = None , UpperCAmelCase : Union[str, ChannelDimension] = ChannelDimension.FIRST , **UpperCAmelCase : Dict , ): A_ = do_resize if do_resize is not None else self.do_resize A_ = size if size is not None else self.size A_ = get_size_dict(UpperCAmelCase , default_to_square=UpperCAmelCase ) A_ = resample if resample is not None else self.resample A_ = do_center_crop if do_center_crop is not None else self.do_center_crop A_ = crop_size if crop_size is not None else self.crop_size A_ = get_size_dict(UpperCAmelCase ) A_ = do_rescale if do_rescale is not None else self.do_rescale A_ = rescale_factor if rescale_factor is not None else self.rescale_factor A_ = do_normalize if do_normalize is not None else self.do_normalize A_ = image_mean if image_mean is not None else self.image_mean A_ = image_std if image_std is not None else self.image_std A_ = make_list_of_images(UpperCAmelCase ) if not valid_images(UpperCAmelCase ): raise ValueError( "Invalid image type. Must be of type PIL.Image.Image, numpy.ndarray, " "torch.Tensor, tf.Tensor or jax.ndarray." ) if do_resize and size is None: raise ValueError("Size must be specified if do_resize is True." ) if do_center_crop and crop_size is None: raise ValueError("Crop size must be specified if do_center_crop is True." ) if do_rescale and rescale_factor is None: raise ValueError("Rescale factor must be specified if do_rescale is True." ) 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." ) # All transformations expect numpy arrays. A_ = [to_numpy_array(UpperCAmelCase ) for image in images] if do_resize: A_ = [self.resize(image=UpperCAmelCase , size=UpperCAmelCase , resample=UpperCAmelCase ) for image in images] if do_center_crop: A_ = [self.center_crop(image=UpperCAmelCase , size=UpperCAmelCase ) for image in images] if do_rescale: A_ = [self.rescale(image=UpperCAmelCase , scale=UpperCAmelCase ) for image in images] if do_normalize: A_ = [self.normalize(image=UpperCAmelCase , mean=UpperCAmelCase , std=UpperCAmelCase ) for image in images] A_ = [to_channel_dimension_format(UpperCAmelCase , UpperCAmelCase ) for image in images] A_ = {"pixel_values": images} return BatchFeature(data=UpperCAmelCase , tensor_type=UpperCAmelCase )
312
import unittest from typing import Tuple import torch from diffusers.utils import floats_tensor, randn_tensor, torch_all_close, torch_device from diffusers.utils.testing_utils import require_torch @require_torch class _a : """simple docstring""" @property def __A ( self : Union[str, Any] ): return self.get_dummy_input() @property def __A ( self : int ): if self.block_type == "down": return (4, 32, 16, 16) elif self.block_type == "mid": return (4, 32, 32, 32) elif self.block_type == "up": return (4, 32, 64, 64) raise ValueError(f'''\'{self.block_type}\' is not a supported block_type. Set it to \'up\', \'mid\', or \'down\'.''' ) def __A ( self : Union[str, Any] , UpperCAmelCase : List[Any]=True , UpperCAmelCase : str=False , UpperCAmelCase : Tuple=False , UpperCAmelCase : Optional[Any]=False , ): A_ = 4 A_ = 32 A_ = (32, 32) A_ = torch.manual_seed(0 ) A_ = torch.device(UpperCAmelCase ) A_ = (batch_size, num_channels) + sizes A_ = randn_tensor(UpperCAmelCase , generator=UpperCAmelCase , device=UpperCAmelCase ) A_ = {"hidden_states": hidden_states} if include_temb: A_ = 128 A_ = randn_tensor((batch_size, temb_channels) , generator=UpperCAmelCase , device=UpperCAmelCase ) if include_res_hidden_states_tuple: A_ = torch.manual_seed(1 ) A_ = (randn_tensor(UpperCAmelCase , generator=UpperCAmelCase , device=UpperCAmelCase ),) if include_encoder_hidden_states: A_ = floats_tensor((batch_size, 32, 32) ).to(UpperCAmelCase ) if include_skip_sample: A_ = randn_tensor(((batch_size, 3) + sizes) , generator=UpperCAmelCase , device=UpperCAmelCase ) return dummy_input def __A ( self : Optional[int] ): A_ = { "in_channels": 32, "out_channels": 32, "temb_channels": 128, } if self.block_type == "up": A_ = 32 if self.block_type == "mid": init_dict.pop("out_channels" ) A_ = self.dummy_input return init_dict, inputs_dict def __A ( self : List[str] , UpperCAmelCase : Optional[Any] ): A_ , A_ = self.prepare_init_args_and_inputs_for_common() A_ = self.block_class(**UpperCAmelCase ) unet_block.to(UpperCAmelCase ) unet_block.eval() with torch.no_grad(): A_ = unet_block(**UpperCAmelCase ) if isinstance(UpperCAmelCase , UpperCAmelCase ): A_ = output[0] self.assertEqual(output.shape , self.output_shape ) A_ = output[0, -1, -3:, -3:] A_ = torch.tensor(UpperCAmelCase ).to(UpperCAmelCase ) assert torch_all_close(output_slice.flatten() , UpperCAmelCase , atol=5E-3 ) @unittest.skipIf(torch_device == "mps" , "Training is not supported in mps" ) def __A ( self : Union[str, Any] ): A_ , A_ = self.prepare_init_args_and_inputs_for_common() A_ = self.block_class(**UpperCAmelCase ) model.to(UpperCAmelCase ) model.train() A_ = model(**UpperCAmelCase ) if isinstance(UpperCAmelCase , UpperCAmelCase ): A_ = output[0] A_ = torch.device(UpperCAmelCase ) A_ = randn_tensor(output.shape , device=UpperCAmelCase ) A_ = torch.nn.functional.mse_loss(UpperCAmelCase , UpperCAmelCase ) loss.backward()
312
1
def __snake_case ( __UpperCamelCase : int = 1000 ): """simple docstring""" A_ = 2**power A_ = 0 while n: A_ , A_ = r + n % 10, n // 10 return r if __name__ == "__main__": print(solution(int(str(input()).strip())))
312
import copy import fnmatch import json import os import pickle as pkl import shutil import sys import tarfile import tempfile from collections import OrderedDict from contextlib import contextmanager from functools import partial from hashlib import shaaaa from io import BytesIO from pathlib import Path from urllib.parse import urlparse from zipfile import ZipFile, is_zipfile import cva import numpy as np import requests import wget from filelock import FileLock from PIL import Image from tqdm.auto import tqdm from yaml import Loader, dump, load try: import torch __a :int = True except ImportError: __a :Optional[Any] = False try: from torch.hub import _get_torch_home __a :Optional[Any] = _get_torch_home() except ImportError: __a :Tuple = os.path.expanduser( os.getenv('TORCH_HOME', os.path.join(os.getenv('XDG_CACHE_HOME', '~/.cache'), 'torch')) ) __a :Optional[Any] = os.path.join(torch_cache_home, 'transformers') __a :int = 'https://cdn.huggingface.co' __a :Any = 'https://s3.amazonaws.com/models.huggingface.co/bert' __a :Optional[Any] = '/'.join(str(Path(__file__).resolve()).split('/')[:-1]) __a :str = os.path.join(PATH, 'config.yaml') __a :str = os.path.join(PATH, 'attributes.txt') __a :Optional[Any] = os.path.join(PATH, 'objects.txt') __a :Optional[int] = os.getenv('PYTORCH_PRETRAINED_BERT_CACHE', default_cache_path) __a :Dict = os.getenv('PYTORCH_TRANSFORMERS_CACHE', PYTORCH_PRETRAINED_BERT_CACHE) __a :List[Any] = os.getenv('TRANSFORMERS_CACHE', PYTORCH_TRANSFORMERS_CACHE) __a :List[str] = 'pytorch_model.bin' __a :Tuple = 'config.yaml' def __snake_case ( __UpperCamelCase : Optional[Any]=OBJECTS ,__UpperCamelCase : List[str]=ATTRIBUTES ): """simple docstring""" A_ = [] with open(__UpperCamelCase ) as f: for object in f.readlines(): vg_classes.append(object.split("," )[0].lower().strip() ) A_ = [] with open(__UpperCamelCase ) as f: for object in f.readlines(): vg_attrs.append(object.split("," )[0].lower().strip() ) return vg_classes, vg_attrs def __snake_case ( __UpperCamelCase : List[Any] ): """simple docstring""" A_ = OrderedDict() with open(__UpperCamelCase ,"rb" ) as f: A_ = pkl.load(__UpperCamelCase )["model"] for k in copy.deepcopy(list(ckp.keys() ) ): A_ = ckp.pop(__UpperCamelCase ) if isinstance(__UpperCamelCase ,np.ndarray ): A_ = torch.tensor(__UpperCamelCase ) else: assert isinstance(__UpperCamelCase ,torch.tensor ), type(__UpperCamelCase ) A_ = v return r class _a : """simple docstring""" _lowerCamelCase : Union[str, Any] = {} def __init__( self : str , UpperCAmelCase : dict , UpperCAmelCase : str = "root" , UpperCAmelCase : List[str]=0 ): A_ = name A_ = level A_ = {} for k, v in dictionary.items(): if v is None: raise ValueError() A_ = copy.deepcopy(UpperCAmelCase ) A_ = copy.deepcopy(UpperCAmelCase ) if isinstance(UpperCAmelCase , UpperCAmelCase ): A_ = Config(UpperCAmelCase , name=UpperCAmelCase , level=level + 1 ) A_ = v setattr(self , UpperCAmelCase , UpperCAmelCase ) A_ = d def __repr__( self : Optional[Any] ): return str(list((self._pointer.keys()) ) ) def __setattr__( self : Any , UpperCAmelCase : Any , UpperCAmelCase : Any ): A_ = val A_ = val A_ = key.split("." ) A_ = len(UpperCAmelCase ) - 1 A_ = self._pointer if len(UpperCAmelCase ) > 1: for i, l in enumerate(UpperCAmelCase ): if hasattr(self , UpperCAmelCase ) and isinstance(getattr(self , UpperCAmelCase ) , UpperCAmelCase ): setattr(getattr(self , UpperCAmelCase ) , ".".join(levels[i:] ) , UpperCAmelCase ) if l == last_level: A_ = val else: A_ = pointer[l] def __A ( self : List[str] ): return self._pointer def __A ( self : int , UpperCAmelCase : Tuple , UpperCAmelCase : int ): with open(f'''{file_name}''' , "w" ) as stream: dump(UpperCAmelCase , UpperCAmelCase ) def __A ( self : List[Any] , UpperCAmelCase : str , UpperCAmelCase : Tuple ): with open(f'''{file_name}''' , "w" ) as stream: json.dump(UpperCAmelCase , UpperCAmelCase ) @staticmethod def __A ( UpperCAmelCase : Optional[int] ): with open(UpperCAmelCase ) as stream: A_ = load(UpperCAmelCase , Loader=UpperCAmelCase ) return data def __str__( self : str ): A_ = " " if self._name != "root": A_ = f'''{t * (self._level-1)}{self._name}:\n''' else: A_ = "" A_ = self._level for i, (k, v) in enumerate(self._pointer.items() ): if isinstance(UpperCAmelCase , UpperCAmelCase ): r += f'''{t * (self._level)}{v}\n''' self._level += 1 else: r += f'''{t * (self._level)}{k}: {v} ({type(UpperCAmelCase ).__name__})\n''' A_ = level return r[:-1] @classmethod def __A ( cls : Optional[Any] , UpperCAmelCase : str , **UpperCAmelCase : str ): A_ , A_ = cls.get_config_dict(UpperCAmelCase , **UpperCAmelCase ) return cls(UpperCAmelCase ) @classmethod def __A ( cls : int , UpperCAmelCase : str , **UpperCAmelCase : int ): A_ = kwargs.pop("cache_dir" , UpperCAmelCase ) A_ = kwargs.pop("force_download" , UpperCAmelCase ) A_ = kwargs.pop("resume_download" , UpperCAmelCase ) A_ = kwargs.pop("proxies" , UpperCAmelCase ) A_ = kwargs.pop("local_files_only" , UpperCAmelCase ) if os.path.isdir(UpperCAmelCase ): A_ = os.path.join(UpperCAmelCase , UpperCAmelCase ) elif os.path.isfile(UpperCAmelCase ) or is_remote_url(UpperCAmelCase ): A_ = pretrained_model_name_or_path else: A_ = hf_bucket_url(UpperCAmelCase , filename=UpperCAmelCase , use_cdn=UpperCAmelCase ) try: # Load from URL or cache if already cached A_ = cached_path( UpperCAmelCase , cache_dir=UpperCAmelCase , force_download=UpperCAmelCase , proxies=UpperCAmelCase , resume_download=UpperCAmelCase , local_files_only=UpperCAmelCase , ) # Load config dict if resolved_config_file is None: raise EnvironmentError A_ = Config.load_yaml(UpperCAmelCase ) except EnvironmentError: A_ = "Can't load config for" raise EnvironmentError(UpperCAmelCase ) if resolved_config_file == config_file: print("loading configuration file from path" ) else: print("loading configuration file cache" ) return Config.load_yaml(UpperCAmelCase ), kwargs def __snake_case ( __UpperCamelCase : Union[str, Any] ): """simple docstring""" A_ = torch.load("dump.pt" ,map_location=in_tensor.device ) A_ = in_tensor.numpy() A_ = out_tensor.numpy()[0] print(na.shape ,na[0, 0, :5] ) print(na.shape ,na[0, 0, :5] ) assert np.allclose(__UpperCamelCase ,__UpperCamelCase ,rtol=0.01 ,atol=0.1 ), ( f'''{sum([1 for x in np.isclose(__UpperCamelCase ,__UpperCamelCase ,rtol=0.01 ,atol=0.1 ).flatten() if x is False] )/len(na.flatten() )*100:.4f} %''' " element-wise mismatch" ) raise Exception("tensors are all good" ) # Hugging face functions below def __snake_case ( __UpperCamelCase : Optional[int] ): """simple docstring""" A_ = urlparse(__UpperCamelCase ) return parsed.scheme in ("http", "https") def __snake_case ( __UpperCamelCase : str ,__UpperCamelCase : str ,__UpperCamelCase : str=True ): """simple docstring""" A_ = CLOUDFRONT_DISTRIB_PREFIX if use_cdn else S3_BUCKET_PREFIX A_ = "/" not in model_id if legacy_format: return f'''{endpoint}/{model_id}-{filename}''' else: return f'''{endpoint}/{model_id}/{filename}''' def __snake_case ( __UpperCamelCase : List[str] ,__UpperCamelCase : Union[str, Any] ,__UpperCamelCase : List[str]=None ,__UpperCamelCase : int=0 ,__UpperCamelCase : int=None ,): """simple docstring""" A_ = "python/{}".format(sys.version.split()[0] ) if _torch_available: ua += "; torch/{}".format(torch.__version__ ) if isinstance(__UpperCamelCase ,__UpperCamelCase ): ua += "; " + "; ".join("{}/{}".format(__UpperCamelCase ,__UpperCamelCase ) for k, v in user_agent.items() ) elif isinstance(__UpperCamelCase ,__UpperCamelCase ): ua += "; " + user_agent A_ = {"user-agent": ua} if resume_size > 0: A_ = "bytes=%d-" % (resume_size,) A_ = requests.get(__UpperCamelCase ,stream=__UpperCamelCase ,proxies=__UpperCamelCase ,headers=__UpperCamelCase ) if response.status_code == 416: # Range not satisfiable return A_ = response.headers.get("Content-Length" ) A_ = resume_size + int(__UpperCamelCase ) if content_length is not None else None A_ = tqdm( unit="B" ,unit_scale=__UpperCamelCase ,total=__UpperCamelCase ,initial=__UpperCamelCase ,desc="Downloading" ,) for chunk in response.iter_content(chunk_size=1024 ): if chunk: # filter out keep-alive new chunks progress.update(len(__UpperCamelCase ) ) temp_file.write(__UpperCamelCase ) progress.close() def __snake_case ( __UpperCamelCase : str ,__UpperCamelCase : Any=None ,__UpperCamelCase : Dict=False ,__UpperCamelCase : Union[str, Any]=None ,__UpperCamelCase : Any=10 ,__UpperCamelCase : int=False ,__UpperCamelCase : Optional[Any]=None ,__UpperCamelCase : str=False ,): """simple docstring""" if cache_dir is None: A_ = TRANSFORMERS_CACHE if isinstance(__UpperCamelCase ,__UpperCamelCase ): A_ = str(__UpperCamelCase ) os.makedirs(__UpperCamelCase ,exist_ok=__UpperCamelCase ) A_ = None if not local_files_only: try: A_ = requests.head(__UpperCamelCase ,allow_redirects=__UpperCamelCase ,proxies=__UpperCamelCase ,timeout=__UpperCamelCase ) if response.status_code == 200: A_ = response.headers.get("ETag" ) except (EnvironmentError, requests.exceptions.Timeout): # etag is already None pass A_ = url_to_filename(__UpperCamelCase ,__UpperCamelCase ) # get cache path to put the file A_ = os.path.join(__UpperCamelCase ,__UpperCamelCase ) # etag is None = we don't have a connection, or url doesn't exist, or is otherwise inaccessible. # try to get the last downloaded one if etag is None: if os.path.exists(__UpperCamelCase ): return cache_path else: A_ = [ file for file in fnmatch.filter(os.listdir(__UpperCamelCase ) ,filename + ".*" ) if not file.endswith(".json" ) and not file.endswith(".lock" ) ] if len(__UpperCamelCase ) > 0: return os.path.join(__UpperCamelCase ,matching_files[-1] ) else: # If files cannot be found and local_files_only=True, # the models might've been found if local_files_only=False # Notify the user about that if local_files_only: raise ValueError( "Cannot find the requested files in the cached path and outgoing traffic has been" " disabled. To enable model look-ups and downloads online, set 'local_files_only'" " to False." ) return None # From now on, etag is not None. if os.path.exists(__UpperCamelCase ) and not force_download: return cache_path # Prevent parallel downloads of the same file with a lock. A_ = cache_path + ".lock" with FileLock(__UpperCamelCase ): # If the download just completed while the lock was activated. if os.path.exists(__UpperCamelCase ) and not force_download: # Even if returning early like here, the lock will be released. return cache_path if resume_download: A_ = cache_path + ".incomplete" @contextmanager def _resumable_file_manager(): with open(__UpperCamelCase ,"a+b" ) as f: yield f A_ = _resumable_file_manager if os.path.exists(__UpperCamelCase ): A_ = os.stat(__UpperCamelCase ).st_size else: A_ = 0 else: A_ = partial(tempfile.NamedTemporaryFile ,dir=__UpperCamelCase ,delete=__UpperCamelCase ) A_ = 0 # Download to temporary file, then copy to cache dir once finished. # Otherwise you get corrupt cache entries if the download gets interrupted. with temp_file_manager() as temp_file: print( "%s not found in cache or force_download set to True, downloading to %s" ,__UpperCamelCase ,temp_file.name ,) http_get( __UpperCamelCase ,__UpperCamelCase ,proxies=__UpperCamelCase ,resume_size=__UpperCamelCase ,user_agent=__UpperCamelCase ,) os.replace(temp_file.name ,__UpperCamelCase ) A_ = {"url": url, "etag": etag} A_ = cache_path + ".json" with open(__UpperCamelCase ,"w" ) as meta_file: json.dump(__UpperCamelCase ,__UpperCamelCase ) return cache_path def __snake_case ( __UpperCamelCase : List[Any] ,__UpperCamelCase : str=None ): """simple docstring""" A_ = url.encode("utf-8" ) A_ = shaaaa(__UpperCamelCase ) A_ = url_hash.hexdigest() if etag: A_ = etag.encode("utf-8" ) A_ = shaaaa(__UpperCamelCase ) filename += "." + etag_hash.hexdigest() if url.endswith(".h5" ): filename += ".h5" return filename def __snake_case ( __UpperCamelCase : Union[str, Any] ,__UpperCamelCase : Union[str, Any]=None ,__UpperCamelCase : List[Any]=False ,__UpperCamelCase : List[str]=None ,__UpperCamelCase : Any=False ,__UpperCamelCase : Optional[int]=None ,__UpperCamelCase : Optional[Any]=False ,__UpperCamelCase : Dict=False ,__UpperCamelCase : Optional[Any]=False ,): """simple docstring""" if cache_dir is None: A_ = TRANSFORMERS_CACHE if isinstance(__UpperCamelCase ,__UpperCamelCase ): A_ = str(__UpperCamelCase ) if isinstance(__UpperCamelCase ,__UpperCamelCase ): A_ = str(__UpperCamelCase ) if is_remote_url(__UpperCamelCase ): # URL, so get it from the cache (downloading if necessary) A_ = get_from_cache( __UpperCamelCase ,cache_dir=__UpperCamelCase ,force_download=__UpperCamelCase ,proxies=__UpperCamelCase ,resume_download=__UpperCamelCase ,user_agent=__UpperCamelCase ,local_files_only=__UpperCamelCase ,) elif os.path.exists(__UpperCamelCase ): # File, and it exists. A_ = url_or_filename elif urlparse(__UpperCamelCase ).scheme == "": # File, but it doesn't exist. raise EnvironmentError("file {} not found".format(__UpperCamelCase ) ) else: # Something unknown raise ValueError("unable to parse {} as a URL or as a local path".format(__UpperCamelCase ) ) if extract_compressed_file: if not is_zipfile(__UpperCamelCase ) and not tarfile.is_tarfile(__UpperCamelCase ): return output_path # Path where we extract compressed archives # We avoid '.' in dir name and add "-extracted" at the end: "./model.zip" => "./model-zip-extracted/" A_ , A_ = os.path.split(__UpperCamelCase ) A_ = output_file.replace("." ,"-" ) + "-extracted" A_ = os.path.join(__UpperCamelCase ,__UpperCamelCase ) if os.path.isdir(__UpperCamelCase ) and os.listdir(__UpperCamelCase ) and not force_extract: return output_path_extracted # Prevent parallel extractions A_ = output_path + ".lock" with FileLock(__UpperCamelCase ): shutil.rmtree(__UpperCamelCase ,ignore_errors=__UpperCamelCase ) os.makedirs(__UpperCamelCase ) if is_zipfile(__UpperCamelCase ): with ZipFile(__UpperCamelCase ,"r" ) as zip_file: zip_file.extractall(__UpperCamelCase ) zip_file.close() elif tarfile.is_tarfile(__UpperCamelCase ): A_ = tarfile.open(__UpperCamelCase ) tar_file.extractall(__UpperCamelCase ) tar_file.close() else: raise EnvironmentError("Archive format of {} could not be identified".format(__UpperCamelCase ) ) return output_path_extracted return output_path def __snake_case ( __UpperCamelCase : str ,__UpperCamelCase : Any="," ): """simple docstring""" assert isinstance(__UpperCamelCase ,__UpperCamelCase ) if os.path.isfile(__UpperCamelCase ): with open(__UpperCamelCase ) as f: A_ = eval(f.read() ) else: A_ = requests.get(__UpperCamelCase ) try: A_ = requests.json() except Exception: A_ = req.content.decode() assert data is not None, "could not connect" try: A_ = eval(__UpperCamelCase ) except Exception: A_ = data.split("\n" ) req.close() return data def __snake_case ( __UpperCamelCase : int ): """simple docstring""" A_ = requests.get(__UpperCamelCase ) A_ = np.array(Image.open(BytesIO(response.content ) ) ) return img def __snake_case ( __UpperCamelCase : Tuple ): """simple docstring""" A_ = url.split("/" )[-1] if fn not in os.listdir(os.getcwd() ): wget.download(__UpperCamelCase ) with open(__UpperCamelCase ,"rb" ) as stream: A_ = pkl.load(__UpperCamelCase ) A_ = weights.pop("model" ) A_ = {} for k, v in model.items(): A_ = torch.from_numpy(__UpperCamelCase ) if "running_var" in k: A_ = torch.tensor([0] ) A_ = k.replace("running_var" ,"num_batches_tracked" ) A_ = zero return new def __snake_case ( ): """simple docstring""" print(f'''{os.path.abspath(os.path.join(__UpperCamelCase ,os.pardir ) )}/demo.ipynb''' ) def __snake_case ( __UpperCamelCase : Optional[Any] ,__UpperCamelCase : Optional[int]="RGB" ): """simple docstring""" assert isinstance(__UpperCamelCase ,__UpperCamelCase ) if os.path.isfile(__UpperCamelCase ): A_ = cva.imread(__UpperCamelCase ) else: A_ = get_image_from_url(__UpperCamelCase ) assert img is not None, f'''could not connect to: {im}''' A_ = cva.cvtColor(__UpperCamelCase ,cva.COLOR_BGR2RGB ) if input_format == "RGB": A_ = img[:, :, ::-1] return img def __snake_case ( __UpperCamelCase : List[str] ,__UpperCamelCase : List[str]=1 ): """simple docstring""" return (images[i : i + batch] for i in range(0 ,len(__UpperCamelCase ) ,__UpperCamelCase ))
312
1
import logging import os import sys from dataclasses import dataclass, field from itertools import chain from typing import Optional, Union import datasets import numpy as np import torch from datasets import load_dataset import transformers from transformers import ( AutoConfig, AutoModelForMultipleChoice, AutoTokenizer, HfArgumentParser, Trainer, TrainingArguments, default_data_collator, set_seed, ) from transformers.tokenization_utils_base import PreTrainedTokenizerBase from transformers.trainer_utils import get_last_checkpoint from transformers.utils import PaddingStrategy, check_min_version, send_example_telemetry # Will error if the minimal version of Transformers is not installed. Remove at your own risks. check_min_version('4.31.0') __a :Union[str, Any] = logging.getLogger(__name__) @dataclass class _a : """simple docstring""" _lowerCamelCase : str = field( metadata={'help': 'Path to pretrained model or model identifier from huggingface.co/models'} ) _lowerCamelCase : Optional[str] = field( default=snake_case_ , metadata={'help': 'Pretrained config name or path if not the same as model_name'} ) _lowerCamelCase : Optional[str] = field( default=snake_case_ , metadata={'help': 'Pretrained tokenizer name or path if not the same as model_name'} ) _lowerCamelCase : Optional[str] = field( default=snake_case_ , metadata={'help': 'Where do you want to store the pretrained models downloaded from huggingface.co'} , ) _lowerCamelCase : bool = field( default=snake_case_ , metadata={'help': 'Whether to use one of the fast tokenizer (backed by the tokenizers library) or not.'} , ) _lowerCamelCase : str = field( default='main' , metadata={'help': 'The specific model version to use (can be a branch name, tag name or commit id).'} , ) _lowerCamelCase : bool = field( default=snake_case_ , metadata={ 'help': ( 'Will use the token generated when running `huggingface-cli login` (necessary to use this script ' 'with private models).' ) } , ) @dataclass class _a : """simple docstring""" _lowerCamelCase : Optional[str] = field(default=snake_case_ , metadata={'help': 'The input training data file (a text file).'} ) _lowerCamelCase : Optional[str] = field( default=snake_case_ , metadata={'help': 'An optional input evaluation data file to evaluate the perplexity on (a text file).'} , ) _lowerCamelCase : bool = field( default=snake_case_ , metadata={'help': 'Overwrite the cached training and evaluation sets'} ) _lowerCamelCase : Optional[int] = field( default=snake_case_ , metadata={'help': 'The number of processes to use for the preprocessing.'} , ) _lowerCamelCase : Optional[int] = field( default=snake_case_ , metadata={ 'help': ( 'The maximum total input sequence length after tokenization. If passed, sequences longer ' 'than this will be truncated, sequences shorter will be padded.' ) } , ) _lowerCamelCase : bool = field( default=snake_case_ , metadata={ 'help': ( 'Whether to pad all samples to the maximum sentence length. ' 'If False, will pad the samples dynamically when batching to the maximum length in the batch. More ' 'efficient on GPU but very bad for TPU.' ) } , ) _lowerCamelCase : Optional[int] = field( default=snake_case_ , metadata={ 'help': ( 'For debugging purposes or quicker training, truncate the number of training examples to this ' 'value if set.' ) } , ) _lowerCamelCase : Optional[int] = field( default=snake_case_ , metadata={ 'help': ( 'For debugging purposes or quicker training, truncate the number of evaluation examples to this ' 'value if set.' ) } , ) def __A ( self : Dict ): if self.train_file is not None: A_ = self.train_file.split("." )[-1] assert extension in ["csv", "json"], "`train_file` should be a csv or a json file." if self.validation_file is not None: A_ = self.validation_file.split("." )[-1] assert extension in ["csv", "json"], "`validation_file` should be a csv or a json file." @dataclass class _a : """simple docstring""" _lowerCamelCase : PreTrainedTokenizerBase _lowerCamelCase : Union[bool, str, PaddingStrategy] = True _lowerCamelCase : Optional[int] = None _lowerCamelCase : Optional[int] = None def __call__( self : List[str] , UpperCAmelCase : int ): A_ = "label" if "label" in features[0].keys() else "labels" A_ = [feature.pop(UpperCAmelCase ) for feature in features] A_ = len(UpperCAmelCase ) A_ = len(features[0]["input_ids"] ) A_ = [ [{k: v[i] for k, v in feature.items()} for i in range(UpperCAmelCase )] for feature in features ] A_ = list(chain(*UpperCAmelCase ) ) A_ = self.tokenizer.pad( UpperCAmelCase , padding=self.padding , max_length=self.max_length , pad_to_multiple_of=self.pad_to_multiple_of , return_tensors="pt" , ) # Un-flatten A_ = {k: v.view(UpperCAmelCase , UpperCAmelCase , -1 ) for k, v in batch.items()} # Add back labels A_ = torch.tensor(UpperCAmelCase , dtype=torch.intaa ) return batch def __snake_case ( ): """simple docstring""" A_ = HfArgumentParser((ModelArguments, DataTrainingArguments, TrainingArguments) ) if len(sys.argv ) == 2 and sys.argv[1].endswith(".json" ): # If we pass only one argument to the script and it's the path to a json file, # let's parse it to get our arguments. A_ , A_ , A_ = parser.parse_json_file(json_file=os.path.abspath(sys.argv[1] ) ) else: A_ , A_ , A_ = parser.parse_args_into_dataclasses() # Sending telemetry. Tracking the example usage helps us better allocate resources to maintain them. The # information sent is the one passed as arguments along with your Python/PyTorch versions. send_example_telemetry("run_swag" ,__UpperCamelCase ,__UpperCamelCase ) # Setup logging logging.basicConfig( format="%(asctime)s - %(levelname)s - %(name)s - %(message)s" ,datefmt="%m/%d/%Y %H:%M:%S" ,handlers=[logging.StreamHandler(sys.stdout )] ,) if training_args.should_log: # The default of training_args.log_level is passive, so we set log level at info here to have that default. transformers.utils.logging.set_verbosity_info() A_ = training_args.get_process_log_level() logger.setLevel(__UpperCamelCase ) datasets.utils.logging.set_verbosity(__UpperCamelCase ) transformers.utils.logging.set_verbosity(__UpperCamelCase ) transformers.utils.logging.enable_default_handler() transformers.utils.logging.enable_explicit_format() # Log on each process the small summary: logger.warning( f'''Process rank: {training_args.local_rank}, device: {training_args.device}, n_gpu: {training_args.n_gpu}''' + f'''distributed training: {bool(training_args.local_rank != -1 )}, 16-bits training: {training_args.fpaa}''' ) logger.info(f'''Training/evaluation parameters {training_args}''' ) # Detecting last checkpoint. A_ = None if os.path.isdir(training_args.output_dir ) and training_args.do_train and not training_args.overwrite_output_dir: A_ = get_last_checkpoint(training_args.output_dir ) if last_checkpoint is None and len(os.listdir(training_args.output_dir ) ) > 0: raise ValueError( f'''Output directory ({training_args.output_dir}) already exists and is not empty. ''' "Use --overwrite_output_dir to overcome." ) elif last_checkpoint is not None and training_args.resume_from_checkpoint is None: logger.info( f'''Checkpoint detected, resuming training at {last_checkpoint}. To avoid this behavior, change ''' "the `--output_dir` or add `--overwrite_output_dir` to train from scratch." ) # Set seed before initializing model. set_seed(training_args.seed ) # Get the datasets: you can either provide your own CSV/JSON/TXT training and evaluation files (see below) # or just provide the name of one of the public datasets available on the hub at https://huggingface.co/datasets/ # (the dataset will be downloaded automatically from the datasets Hub). # For CSV/JSON files, this script will use the column called 'text' or the first column if no column called # 'text' is found. You can easily tweak this behavior (see below). # In distributed training, the load_dataset function guarantee that only one local process can concurrently # download the dataset. if data_args.train_file is not None or data_args.validation_file is not None: A_ = {} if data_args.train_file is not None: A_ = data_args.train_file if data_args.validation_file is not None: A_ = data_args.validation_file A_ = data_args.train_file.split("." )[-1] A_ = load_dataset( __UpperCamelCase ,data_files=__UpperCamelCase ,cache_dir=model_args.cache_dir ,use_auth_token=True if model_args.use_auth_token else None ,) else: # Downloading and loading the swag dataset from the hub. A_ = load_dataset( "swag" ,"regular" ,cache_dir=model_args.cache_dir ,use_auth_token=True if model_args.use_auth_token else None ,) # See more about loading any type of standard or custom dataset (from files, python dict, pandas DataFrame, etc) at # https://huggingface.co/docs/datasets/loading_datasets.html. # Load pretrained model and tokenizer # Distributed training: # The .from_pretrained methods guarantee that only one local process can concurrently # download model & vocab. A_ = AutoConfig.from_pretrained( model_args.config_name if model_args.config_name else model_args.model_name_or_path ,cache_dir=model_args.cache_dir ,revision=model_args.model_revision ,use_auth_token=True if model_args.use_auth_token else None ,) A_ = AutoTokenizer.from_pretrained( model_args.tokenizer_name if model_args.tokenizer_name else model_args.model_name_or_path ,cache_dir=model_args.cache_dir ,use_fast=model_args.use_fast_tokenizer ,revision=model_args.model_revision ,use_auth_token=True if model_args.use_auth_token else None ,) A_ = AutoModelForMultipleChoice.from_pretrained( model_args.model_name_or_path ,from_tf=bool(".ckpt" in model_args.model_name_or_path ) ,config=__UpperCamelCase ,cache_dir=model_args.cache_dir ,revision=model_args.model_revision ,use_auth_token=True if model_args.use_auth_token else None ,) # When using your own dataset or a different dataset from swag, you will probably need to change this. A_ = [f'''ending{i}''' for i in range(4 )] A_ = "sent1" A_ = "sent2" if data_args.max_seq_length is None: A_ = tokenizer.model_max_length if max_seq_length > 1024: logger.warning( "The chosen tokenizer supports a `model_max_length` that is longer than the default `block_size` value" " of 1024. If you would like to use a longer `block_size` up to `tokenizer.model_max_length` you can" " override this default with `--block_size xxx`." ) A_ = 1024 else: if data_args.max_seq_length > tokenizer.model_max_length: logger.warning( f'''The max_seq_length passed ({data_args.max_seq_length}) is larger than the maximum length for the''' f'''model ({tokenizer.model_max_length}). Using max_seq_length={tokenizer.model_max_length}.''' ) A_ = min(data_args.max_seq_length ,tokenizer.model_max_length ) # Preprocessing the datasets. def preprocess_function(__UpperCamelCase : int ): A_ = [[context] * 4 for context in examples[context_name]] A_ = examples[question_header_name] A_ = [ [f'''{header} {examples[end][i]}''' for end in ending_names] for i, header in enumerate(__UpperCamelCase ) ] # Flatten out A_ = list(chain(*__UpperCamelCase ) ) A_ = list(chain(*__UpperCamelCase ) ) # Tokenize A_ = tokenizer( __UpperCamelCase ,__UpperCamelCase ,truncation=__UpperCamelCase ,max_length=__UpperCamelCase ,padding="max_length" if data_args.pad_to_max_length else False ,) # Un-flatten return {k: [v[i : i + 4] for i in range(0 ,len(__UpperCamelCase ) ,4 )] for k, v in tokenized_examples.items()} if training_args.do_train: if "train" not in raw_datasets: raise ValueError("--do_train requires a train dataset" ) A_ = raw_datasets["train"] if data_args.max_train_samples is not None: A_ = min(len(__UpperCamelCase ) ,data_args.max_train_samples ) A_ = train_dataset.select(range(__UpperCamelCase ) ) with training_args.main_process_first(desc="train dataset map pre-processing" ): A_ = train_dataset.map( __UpperCamelCase ,batched=__UpperCamelCase ,num_proc=data_args.preprocessing_num_workers ,load_from_cache_file=not data_args.overwrite_cache ,) if training_args.do_eval: if "validation" not in raw_datasets: raise ValueError("--do_eval requires a validation dataset" ) A_ = raw_datasets["validation"] if data_args.max_eval_samples is not None: A_ = min(len(__UpperCamelCase ) ,data_args.max_eval_samples ) A_ = eval_dataset.select(range(__UpperCamelCase ) ) with training_args.main_process_first(desc="validation dataset map pre-processing" ): A_ = eval_dataset.map( __UpperCamelCase ,batched=__UpperCamelCase ,num_proc=data_args.preprocessing_num_workers ,load_from_cache_file=not data_args.overwrite_cache ,) # Data collator A_ = ( default_data_collator if data_args.pad_to_max_length else DataCollatorForMultipleChoice(tokenizer=__UpperCamelCase ,pad_to_multiple_of=8 if training_args.fpaa else None ) ) # Metric def compute_metrics(__UpperCamelCase : List[str] ): A_ , A_ = eval_predictions A_ = np.argmax(__UpperCamelCase ,axis=1 ) return {"accuracy": (preds == label_ids).astype(np.floataa ).mean().item()} # Initialize our Trainer A_ = Trainer( model=__UpperCamelCase ,args=__UpperCamelCase ,train_dataset=train_dataset if training_args.do_train else None ,eval_dataset=eval_dataset if training_args.do_eval else None ,tokenizer=__UpperCamelCase ,data_collator=__UpperCamelCase ,compute_metrics=__UpperCamelCase ,) # Training if training_args.do_train: A_ = None if training_args.resume_from_checkpoint is not None: A_ = training_args.resume_from_checkpoint elif last_checkpoint is not None: A_ = last_checkpoint A_ = trainer.train(resume_from_checkpoint=__UpperCamelCase ) trainer.save_model() # Saves the tokenizer too for easy upload A_ = train_result.metrics A_ = ( data_args.max_train_samples if data_args.max_train_samples is not None else len(__UpperCamelCase ) ) A_ = min(__UpperCamelCase ,len(__UpperCamelCase ) ) trainer.log_metrics("train" ,__UpperCamelCase ) trainer.save_metrics("train" ,__UpperCamelCase ) trainer.save_state() # Evaluation if training_args.do_eval: logger.info("*** Evaluate ***" ) A_ = trainer.evaluate() A_ = data_args.max_eval_samples if data_args.max_eval_samples is not None else len(__UpperCamelCase ) A_ = min(__UpperCamelCase ,len(__UpperCamelCase ) ) trainer.log_metrics("eval" ,__UpperCamelCase ) trainer.save_metrics("eval" ,__UpperCamelCase ) A_ = { "finetuned_from": model_args.model_name_or_path, "tasks": "multiple-choice", "dataset_tags": "swag", "dataset_args": "regular", "dataset": "SWAG", "language": "en", } if training_args.push_to_hub: trainer.push_to_hub(**__UpperCamelCase ) else: trainer.create_model_card(**__UpperCamelCase ) def __snake_case ( __UpperCamelCase : Any ): """simple docstring""" main() if __name__ == "__main__": main()
312
from __future__ import annotations def __snake_case ( __UpperCamelCase : list[list[int]] ): """simple docstring""" for i in range(1 ,len(matrix[0] ) ): matrix[0][i] += matrix[0][i - 1] # preprocessing the first column for i in range(1 ,len(__UpperCamelCase ) ): matrix[i][0] += matrix[i - 1][0] # updating the path cost for current position for i in range(1 ,len(__UpperCamelCase ) ): for j in range(1 ,len(matrix[0] ) ): matrix[i][j] += min(matrix[i - 1][j] ,matrix[i][j - 1] ) return matrix[-1][-1] if __name__ == "__main__": import doctest doctest.testmod()
312
1
import argparse import logging import sys from unittest.mock import patch import run_glue_deebert from transformers.testing_utils import TestCasePlus, get_gpu_count, require_torch_non_multi_gpu, slow logging.basicConfig(level=logging.DEBUG) __a :List[str] = logging.getLogger() def __snake_case ( ): """simple docstring""" A_ = argparse.ArgumentParser() parser.add_argument("-f" ) A_ = parser.parse_args() return args.f class _a ( snake_case_ ): """simple docstring""" def __A ( self : List[Any] ): A_ = logging.StreamHandler(sys.stdout ) logger.addHandler(UpperCAmelCase ) def __A ( self : List[str] , UpperCAmelCase : Any ): A_ = get_gpu_count() if n_gpu > 1: pass # XXX: doesn't quite work with n_gpu > 1 https://github.com/huggingface/transformers/issues/10560 # script = f"{self.examples_dir_str}/research_projects/deebert/run_glue_deebert.py" # distributed_args = f"-m torch.distributed.launch --nproc_per_node={n_gpu} {script}".split() # cmd = [sys.executable] + distributed_args + args # execute_subprocess_async(cmd, env=self.get_env()) # XXX: test the results - need to save them first into .json file else: args.insert(0 , "run_glue_deebert.py" ) with patch.object(UpperCAmelCase , "argv" , UpperCAmelCase ): A_ = run_glue_deebert.main() for value in result.values(): self.assertGreaterEqual(UpperCAmelCase , 0.666 ) @slow @require_torch_non_multi_gpu def __A ( self : Tuple ): A_ = "\n --model_type roberta\n --model_name_or_path roberta-base\n --task_name MRPC\n --do_train\n --do_eval\n --do_lower_case\n --data_dir ./tests/fixtures/tests_samples/MRPC/\n --max_seq_length 128\n --per_gpu_eval_batch_size=1\n --per_gpu_train_batch_size=8\n --learning_rate 2e-4\n --num_train_epochs 3\n --overwrite_output_dir\n --seed 42\n --output_dir ./examples/deebert/saved_models/roberta-base/MRPC/two_stage\n --plot_data_dir ./examples/deebert/results/\n --save_steps 0\n --overwrite_cache\n --eval_after_first_stage\n ".split() self.run_and_check(UpperCAmelCase ) A_ = "\n --model_type roberta\n --model_name_or_path ./examples/deebert/saved_models/roberta-base/MRPC/two_stage\n --task_name MRPC\n --do_eval\n --do_lower_case\n --data_dir ./tests/fixtures/tests_samples/MRPC/\n --output_dir ./examples/deebert/saved_models/roberta-base/MRPC/two_stage\n --plot_data_dir ./examples/deebert/results/\n --max_seq_length 128\n --eval_each_highway\n --eval_highway\n --overwrite_cache\n --per_gpu_eval_batch_size=1\n ".split() self.run_and_check(UpperCAmelCase ) A_ = "\n --model_type roberta\n --model_name_or_path ./examples/deebert/saved_models/roberta-base/MRPC/two_stage\n --task_name MRPC\n --do_eval\n --do_lower_case\n --data_dir ./tests/fixtures/tests_samples/MRPC/\n --output_dir ./examples/deebert/saved_models/roberta-base/MRPC/two_stage\n --plot_data_dir ./examples/deebert/results/\n --max_seq_length 128\n --early_exit_entropy 0.1\n --eval_highway\n --overwrite_cache\n --per_gpu_eval_batch_size=1\n ".split() self.run_and_check(UpperCAmelCase )
312
from typing import Dict from transformers import EvalPrediction, HfArgumentParser, TrainingArguments, is_torch_available from transformers.testing_utils import ( TestCasePlus, execute_subprocess_async, get_torch_dist_unique_port, require_torch_multi_gpu, require_torch_neuroncore, ) from transformers.training_args import ParallelMode from transformers.utils import logging __a :int = logging.get_logger(__name__) if is_torch_available(): import torch from torch import nn from torch.utils.data import Dataset from transformers import Trainer class _a ( snake_case_ ): """simple docstring""" def __init__( self : Tuple , UpperCAmelCase : int = 101 ): A_ = length def __len__( self : int ): return self.length def __getitem__( self : Optional[int] , UpperCAmelCase : Optional[int] ): return i class _a : """simple docstring""" def __call__( self : Any , UpperCAmelCase : Optional[Any] ): return {"input_ids": torch.tensor(UpperCAmelCase ), "labels": torch.tensor(UpperCAmelCase )} class _a ( nn.Module ): """simple docstring""" def __init__( self : int ): super().__init__() # Add some (unused) params otherwise DDP will complain. A_ = nn.Linear(120 , 80 ) def __A ( self : Tuple , UpperCAmelCase : Dict , UpperCAmelCase : Tuple=None ): if labels is not None: return torch.tensor(0.0 , device=input_ids.device ), input_ids else: return input_ids class _a ( snake_case_ ): """simple docstring""" @require_torch_neuroncore def __A ( self : List[str] ): A_ = f'''--nproc_per_node=2 --master_port={get_torch_dist_unique_port()} {self.test_file_dir}/test_trainer_distributed.py '''.split() A_ = self.get_auto_remove_tmp_dir() A_ = f'''--output_dir {output_dir}'''.split() A_ = ["torchrun"] + distributed_args + args execute_subprocess_async(UpperCAmelCase , env=self.get_env() ) # successful return here == success - any errors would have caused an error in the sub-call class _a ( snake_case_ ): """simple docstring""" @require_torch_multi_gpu def __A ( self : List[str] ): A_ = f'''--nproc_per_node={torch.cuda.device_count()} --master_port={get_torch_dist_unique_port()} {self.test_file_dir}/test_trainer_distributed.py '''.split() A_ = self.get_auto_remove_tmp_dir() A_ = f'''--output_dir {output_dir}'''.split() A_ = ["torchrun"] + distributed_args + args execute_subprocess_async(UpperCAmelCase , env=self.get_env() ) # successful return here == success - any errors would have caused an error in the sub-call if __name__ == "__main__": # The script below is meant to be run under torch.distributed, on a machine with multiple GPUs: # # PYTHONPATH="src" python -m torch.distributed.run --nproc_per_node 2 --output_dir output_dir ./tests/test_trainer_distributed.py __a :Union[str, Any] = HfArgumentParser((TrainingArguments,)) __a :Tuple = parser.parse_args_into_dataclasses()[0] logger.warning( F"Process rank: {training_args.local_rank}, device: {training_args.device}, n_gpu: {training_args.n_gpu}, " F"distributed training: {training_args.parallel_mode != ParallelMode.NOT_DISTRIBUTED}" ) # Essentially, what we want to verify in the distributed case is that we get all samples back, # in the right order. (this is crucial for prediction for instance) for dataset_length in [101, 40, 7]: __a :int = DummyDataset(dataset_length) def __snake_case ( __UpperCamelCase : EvalPrediction ): """simple docstring""" A_ = list(range(len(__UpperCamelCase ) ) ) A_ = p.predictions.tolist() == sequential and p.label_ids.tolist() == sequential if not success and training_args.local_rank == 0: logger.warning( "Predictions and/or labels do not match expected results:\n - predictions: " f'''{p.predictions.tolist()}\n - labels: {p.label_ids.tolist()}\n - expected: {sequential}''' ) return {"success": success} __a :str = Trainer( model=DummyModel(), args=training_args, data_collator=DummyDataCollator(), eval_dataset=dataset, compute_metrics=compute_metrics, ) __a :str = trainer.evaluate() logger.info(metrics) if metrics["eval_success"] is not True: logger.error(metrics) exit(1) __a :str = trainer.predict(dataset) logger.info(p.metrics) if p.metrics["test_success"] is not True: logger.error(p.metrics) exit(1) __a :Optional[int] = 2 __a :List[Any] = trainer.evaluate() logger.info(metrics) if metrics["eval_success"] is not True: logger.error(metrics) exit(1) __a :str = trainer.predict(dataset) logger.info(p.metrics) if p.metrics["test_success"] is not True: logger.error(p.metrics) exit(1) __a :Union[str, Any] = None
312
1
from __future__ import annotations from itertools import permutations from random import randint from timeit import repeat def __snake_case ( ): """simple docstring""" A_ = [randint(-1000 ,1000 ) for i in range(10 )] A_ = randint(-5000 ,5000 ) return (arr, r) __a :Optional[int] = make_dataset() def __snake_case ( __UpperCamelCase : list[int] ,__UpperCamelCase : int ): """simple docstring""" for triplet in permutations(__UpperCamelCase ,3 ): if sum(__UpperCamelCase ) == target: return tuple(sorted(__UpperCamelCase ) ) return (0, 0, 0) def __snake_case ( __UpperCamelCase : list[int] ,__UpperCamelCase : int ): """simple docstring""" arr.sort() A_ = len(__UpperCamelCase ) for i in range(n - 1 ): A_ , A_ = i + 1, n - 1 while left < right: if arr[i] + arr[left] + arr[right] == target: return (arr[i], arr[left], arr[right]) elif arr[i] + arr[left] + arr[right] < target: left += 1 elif arr[i] + arr[left] + arr[right] > target: right -= 1 return (0, 0, 0) def __snake_case ( ): """simple docstring""" A_ = "\nfrom __main__ import dataset, triplet_sum1, triplet_sum2\n" A_ = "\ntriplet_sum1(*dataset)\n" A_ = "\ntriplet_sum2(*dataset)\n" A_ = repeat(setup=__UpperCamelCase ,stmt=__UpperCamelCase ,repeat=5 ,number=1_0000 ) A_ = repeat(setup=__UpperCamelCase ,stmt=__UpperCamelCase ,repeat=5 ,number=1_0000 ) return (min(__UpperCamelCase ), min(__UpperCamelCase )) if __name__ == "__main__": from doctest import testmod testmod() __a :List[Any] = solution_times() print(F"The time for naive implementation is {times[0]}.") print(F"The time for optimized implementation is {times[1]}.")
312
import argparse import json from pathlib import Path import requests import torch from huggingface_hub import hf_hub_download from PIL import Image from timm import create_model from timm.data import resolve_data_config from timm.data.transforms_factory import create_transform from transformers import BitConfig, BitForImageClassification, BitImageProcessor from transformers.image_utils import PILImageResampling from transformers.utils import logging logging.set_verbosity_info() __a :Any = logging.get_logger(__name__) def __snake_case ( __UpperCamelCase : Optional[int] ): """simple docstring""" A_ = "huggingface/label-files" A_ = "imagenet-1k-id2label.json" A_ = json.load(open(hf_hub_download(__UpperCamelCase ,__UpperCamelCase ,repo_type="dataset" ) ,"r" ) ) A_ = {int(__UpperCamelCase ): v for k, v in idalabel.items()} A_ = {v: k for k, v in idalabel.items()} A_ = "std_conv" if "bit" in model_name else False # note that when using BiT as backbone for ViT-hybrid checkpoints, # one needs to additionally set config.layer_type = "bottleneck", config.stem_type = "same", # config.conv_layer = "std_conv_same" A_ = BitConfig( conv_layer=__UpperCamelCase ,num_labels=1000 ,idalabel=__UpperCamelCase ,labelaid=__UpperCamelCase ,) return config def __snake_case ( __UpperCamelCase : Union[str, Any] ): """simple docstring""" if "stem.conv" in name: A_ = name.replace("stem.conv" ,"bit.embedder.convolution" ) if "blocks" in name: A_ = name.replace("blocks" ,"layers" ) if "head.fc" in name: A_ = name.replace("head.fc" ,"classifier.1" ) if name.startswith("norm" ): A_ = "bit." + name if "bit" not in name and "classifier" not in name: A_ = "bit.encoder." + name return name def __snake_case ( ): """simple docstring""" A_ = "http://images.cocodataset.org/val2017/000000039769.jpg" A_ = Image.open(requests.get(__UpperCamelCase ,stream=__UpperCamelCase ).raw ) return im @torch.no_grad() def __snake_case ( __UpperCamelCase : Any ,__UpperCamelCase : Optional[Any] ,__UpperCamelCase : Tuple=False ): """simple docstring""" A_ = get_config(__UpperCamelCase ) # load original model from timm A_ = create_model(__UpperCamelCase ,pretrained=__UpperCamelCase ) timm_model.eval() # load state_dict of original model A_ = timm_model.state_dict() for key in state_dict.copy().keys(): A_ = state_dict.pop(__UpperCamelCase ) A_ = val.squeeze() if "head" in key else val # load HuggingFace model A_ = BitForImageClassification(__UpperCamelCase ) model.eval() model.load_state_dict(__UpperCamelCase ) # create image processor A_ = create_transform(**resolve_data_config({} ,model=__UpperCamelCase ) ) A_ = transform.transforms A_ = { "bilinear": PILImageResampling.BILINEAR, "bicubic": PILImageResampling.BICUBIC, "nearest": PILImageResampling.NEAREST, } A_ = BitImageProcessor( do_resize=__UpperCamelCase ,size={"shortest_edge": timm_transforms[0].size} ,resample=pillow_resamplings[timm_transforms[0].interpolation.value] ,do_center_crop=__UpperCamelCase ,crop_size={"height": timm_transforms[1].size[0], "width": timm_transforms[1].size[1]} ,do_normalize=__UpperCamelCase ,image_mean=timm_transforms[-1].mean.tolist() ,image_std=timm_transforms[-1].std.tolist() ,) A_ = prepare_img() A_ = transform(__UpperCamelCase ).unsqueeze(0 ) A_ = processor(__UpperCamelCase ,return_tensors="pt" ).pixel_values # verify pixel values assert torch.allclose(__UpperCamelCase ,__UpperCamelCase ) # verify logits with torch.no_grad(): A_ = model(__UpperCamelCase ) A_ = outputs.logits print("Logits:" ,logits[0, :3] ) print("Predicted class:" ,model.config.idalabel[logits.argmax(-1 ).item()] ) A_ = timm_model(__UpperCamelCase ) assert timm_logits.shape == outputs.logits.shape assert torch.allclose(__UpperCamelCase ,outputs.logits ,atol=1E-3 ) print("Looks ok!" ) if pytorch_dump_folder_path is not None: Path(__UpperCamelCase ).mkdir(exist_ok=__UpperCamelCase ) print(f'''Saving model {model_name} and processor to {pytorch_dump_folder_path}''' ) model.save_pretrained(__UpperCamelCase ) processor.save_pretrained(__UpperCamelCase ) if push_to_hub: print(f'''Pushing model {model_name} and processor to the hub''' ) model.push_to_hub(f'''ybelkada/{model_name}''' ) processor.push_to_hub(f'''ybelkada/{model_name}''' ) if __name__ == "__main__": __a :List[Any] = argparse.ArgumentParser() # Required parameters parser.add_argument( '--model_name', default='resnetv2_50x1_bitm', type=str, help='Name of the BiT timm model you\'d like to convert.', ) parser.add_argument( '--pytorch_dump_folder_path', default=None, type=str, help='Path to the output PyTorch model directory.' ) parser.add_argument( '--push_to_hub', action='store_true', help='Whether to push the model to the hub.', ) __a :str = parser.parse_args() convert_bit_checkpoint(args.model_name, args.pytorch_dump_folder_path, args.push_to_hub)
312
1
from .imports import is_tqdm_available if is_tqdm_available(): from tqdm.auto import tqdm as _tqdm from ..state import PartialState def __snake_case ( __UpperCamelCase : bool = True ,*__UpperCamelCase : Tuple ,**__UpperCamelCase : int ): """simple docstring""" if not is_tqdm_available(): raise ImportError("Accelerate's `tqdm` module requires `tqdm` to be installed. Please run `pip install tqdm`." ) A_ = False if main_process_only: A_ = PartialState().local_process_index == 0 return _tqdm(*__UpperCamelCase ,**__UpperCamelCase ,disable=__UpperCamelCase )
312
import os import re import sys import traceback import warnings from pathlib import Path from typing import Dict, Optional, Union from uuid import uuida from huggingface_hub import HfFolder, ModelCard, ModelCardData, hf_hub_download, whoami from huggingface_hub.file_download import REGEX_COMMIT_HASH from huggingface_hub.utils import ( EntryNotFoundError, RepositoryNotFoundError, RevisionNotFoundError, is_jinja_available, ) from packaging import version from requests import HTTPError from .. import __version__ from .constants import ( DEPRECATED_REVISION_ARGS, DIFFUSERS_CACHE, HUGGINGFACE_CO_RESOLVE_ENDPOINT, SAFETENSORS_WEIGHTS_NAME, WEIGHTS_NAME, ) from .import_utils import ( ENV_VARS_TRUE_VALUES, _flax_version, _jax_version, _onnxruntime_version, _torch_version, is_flax_available, is_onnx_available, is_torch_available, ) from .logging import get_logger __a :Dict = get_logger(__name__) __a :Union[str, Any] = Path(__file__).parent / 'model_card_template.md' __a :Tuple = uuida().hex __a :List[Any] = os.getenv('HF_HUB_OFFLINE', '').upper() in ENV_VARS_TRUE_VALUES __a :Union[str, Any] = os.getenv('DISABLE_TELEMETRY', '').upper() in ENV_VARS_TRUE_VALUES __a :Tuple = HUGGINGFACE_CO_RESOLVE_ENDPOINT + '/api/telemetry/' def __snake_case ( __UpperCamelCase : Union[Dict, str, None] = None ): """simple docstring""" A_ = f'''diffusers/{__version__}; python/{sys.version.split()[0]}; session_id/{SESSION_ID}''' if DISABLE_TELEMETRY or HF_HUB_OFFLINE: return ua + "; telemetry/off" if is_torch_available(): ua += f'''; torch/{_torch_version}''' if is_flax_available(): ua += f'''; jax/{_jax_version}''' ua += f'''; flax/{_flax_version}''' if is_onnx_available(): ua += f'''; onnxruntime/{_onnxruntime_version}''' # CI will set this value to True if os.environ.get("DIFFUSERS_IS_CI" ,"" ).upper() in ENV_VARS_TRUE_VALUES: ua += "; is_ci/true" if isinstance(__UpperCamelCase ,__UpperCamelCase ): ua += "; " + "; ".join(f'''{k}/{v}''' for k, v in user_agent.items() ) elif isinstance(__UpperCamelCase ,__UpperCamelCase ): ua += "; " + user_agent return ua def __snake_case ( __UpperCamelCase : str ,__UpperCamelCase : Optional[str] = None ,__UpperCamelCase : Optional[str] = None ): """simple docstring""" if token is None: A_ = HfFolder.get_token() if organization is None: A_ = whoami(__UpperCamelCase )["name"] return f'''{username}/{model_id}''' else: return f'''{organization}/{model_id}''' def __snake_case ( __UpperCamelCase : Tuple ,__UpperCamelCase : Union[str, Any] ): """simple docstring""" if not is_jinja_available(): raise ValueError( "Modelcard rendering is based on Jinja templates." " Please make sure to have `jinja` installed before using `create_model_card`." " To install it, please run `pip install Jinja2`." ) if hasattr(__UpperCamelCase ,"local_rank" ) and args.local_rank not in [-1, 0]: return A_ = args.hub_token if hasattr(__UpperCamelCase ,"hub_token" ) else None A_ = get_full_repo_name(__UpperCamelCase ,token=__UpperCamelCase ) A_ = ModelCard.from_template( card_data=ModelCardData( # Card metadata object that will be converted to YAML block language="en" ,license="apache-2.0" ,library_name="diffusers" ,tags=[] ,datasets=args.dataset_name ,metrics=[] ,) ,template_path=__UpperCamelCase ,model_name=__UpperCamelCase ,repo_name=__UpperCamelCase ,dataset_name=args.dataset_name if hasattr(__UpperCamelCase ,"dataset_name" ) else None ,learning_rate=args.learning_rate ,train_batch_size=args.train_batch_size ,eval_batch_size=args.eval_batch_size ,gradient_accumulation_steps=( args.gradient_accumulation_steps if hasattr(__UpperCamelCase ,"gradient_accumulation_steps" ) else None ) ,adam_betaa=args.adam_betaa if hasattr(__UpperCamelCase ,"adam_beta1" ) else None ,adam_betaa=args.adam_betaa if hasattr(__UpperCamelCase ,"adam_beta2" ) else None ,adam_weight_decay=args.adam_weight_decay if hasattr(__UpperCamelCase ,"adam_weight_decay" ) else None ,adam_epsilon=args.adam_epsilon if hasattr(__UpperCamelCase ,"adam_epsilon" ) else None ,lr_scheduler=args.lr_scheduler if hasattr(__UpperCamelCase ,"lr_scheduler" ) else None ,lr_warmup_steps=args.lr_warmup_steps if hasattr(__UpperCamelCase ,"lr_warmup_steps" ) else None ,ema_inv_gamma=args.ema_inv_gamma if hasattr(__UpperCamelCase ,"ema_inv_gamma" ) else None ,ema_power=args.ema_power if hasattr(__UpperCamelCase ,"ema_power" ) else None ,ema_max_decay=args.ema_max_decay if hasattr(__UpperCamelCase ,"ema_max_decay" ) else None ,mixed_precision=args.mixed_precision ,) A_ = os.path.join(args.output_dir ,"README.md" ) model_card.save(__UpperCamelCase ) def __snake_case ( __UpperCamelCase : Optional[str] ,__UpperCamelCase : Optional[str] = None ): """simple docstring""" if resolved_file is None or commit_hash is not None: return commit_hash A_ = str(Path(__UpperCamelCase ).as_posix() ) A_ = re.search(R"snapshots/([^/]+)/" ,__UpperCamelCase ) if search is None: return None A_ = search.groups()[0] return commit_hash if REGEX_COMMIT_HASH.match(__UpperCamelCase ) else None # Old default cache path, potentially to be migrated. # This logic was more or less taken from `transformers`, with the following differences: # - Diffusers doesn't use custom environment variables to specify the cache path. # - There is no need to migrate the cache format, just move the files to the new location. __a :str = os.path.expanduser( os.getenv('HF_HOME', os.path.join(os.getenv('XDG_CACHE_HOME', '~/.cache'), 'huggingface')) ) __a :List[Any] = os.path.join(hf_cache_home, 'diffusers') def __snake_case ( __UpperCamelCase : Optional[str] = None ,__UpperCamelCase : Optional[str] = None ): """simple docstring""" if new_cache_dir is None: A_ = DIFFUSERS_CACHE if old_cache_dir is None: A_ = old_diffusers_cache A_ = Path(__UpperCamelCase ).expanduser() A_ = Path(__UpperCamelCase ).expanduser() for old_blob_path in old_cache_dir.glob("**/blobs/*" ): if old_blob_path.is_file() and not old_blob_path.is_symlink(): A_ = new_cache_dir / old_blob_path.relative_to(__UpperCamelCase ) new_blob_path.parent.mkdir(parents=__UpperCamelCase ,exist_ok=__UpperCamelCase ) os.replace(__UpperCamelCase ,__UpperCamelCase ) try: os.symlink(__UpperCamelCase ,__UpperCamelCase ) except OSError: logger.warning( "Could not create symlink between old cache and new cache. If you use an older version of diffusers again, files will be re-downloaded." ) # At this point, old_cache_dir contains symlinks to the new cache (it can still be used). __a :Dict = os.path.join(DIFFUSERS_CACHE, 'version_diffusers_cache.txt') if not os.path.isfile(cache_version_file): __a :Optional[int] = 0 else: with open(cache_version_file) as f: try: __a :Dict = int(f.read()) except ValueError: __a :str = 0 if cache_version < 1: __a :Optional[Any] = os.path.isdir(old_diffusers_cache) and len(os.listdir(old_diffusers_cache)) > 0 if old_cache_is_not_empty: logger.warning( 'The cache for model files in Diffusers v0.14.0 has moved to a new location. Moving your ' 'existing cached models. This is a one-time operation, you can interrupt it or run it ' 'later by calling `diffusers.utils.hub_utils.move_cache()`.' ) try: move_cache() except Exception as e: __a :Optional[Any] = '\n'.join(traceback.format_tb(e.__traceback__)) logger.error( F"There was a problem when trying to move your cache:\n\n{trace}\n{e.__class__.__name__}: {e}\n\nPlease " 'file an issue at https://github.com/huggingface/diffusers/issues/new/choose, copy paste this whole ' 'message and we will do our best to help.' ) if cache_version < 1: try: os.makedirs(DIFFUSERS_CACHE, exist_ok=True) with open(cache_version_file, 'w') as f: f.write('1') except Exception: logger.warning( F"There was a problem when trying to write in your cache folder ({DIFFUSERS_CACHE}). Please, ensure " 'the directory exists and can be written to.' ) def __snake_case ( __UpperCamelCase : str ,__UpperCamelCase : Optional[str] = None ): """simple docstring""" if variant is not None: A_ = weights_name.split("." ) A_ = splits[:-1] + [variant] + splits[-1:] A_ = ".".join(__UpperCamelCase ) return weights_name def __snake_case ( __UpperCamelCase : Optional[Any] ,*, __UpperCamelCase : Union[str, Any] ,__UpperCamelCase : Any ,__UpperCamelCase : Tuple ,__UpperCamelCase : Union[str, Any] ,__UpperCamelCase : str ,__UpperCamelCase : int ,__UpperCamelCase : Union[str, Any] ,__UpperCamelCase : int ,__UpperCamelCase : Optional[int] ,__UpperCamelCase : Tuple ,__UpperCamelCase : Optional[int]=None ,): """simple docstring""" A_ = str(__UpperCamelCase ) if os.path.isfile(__UpperCamelCase ): return pretrained_model_name_or_path elif os.path.isdir(__UpperCamelCase ): if os.path.isfile(os.path.join(__UpperCamelCase ,__UpperCamelCase ) ): # Load from a PyTorch checkpoint A_ = os.path.join(__UpperCamelCase ,__UpperCamelCase ) return model_file elif subfolder is not None and os.path.isfile( os.path.join(__UpperCamelCase ,__UpperCamelCase ,__UpperCamelCase ) ): A_ = os.path.join(__UpperCamelCase ,__UpperCamelCase ,__UpperCamelCase ) return model_file else: raise EnvironmentError( f'''Error no file named {weights_name} found in directory {pretrained_model_name_or_path}.''' ) else: # 1. First check if deprecated way of loading from branches is used if ( revision in DEPRECATED_REVISION_ARGS and (weights_name == WEIGHTS_NAME or weights_name == SAFETENSORS_WEIGHTS_NAME) and version.parse(version.parse(__UpperCamelCase ).base_version ) >= version.parse("0.20.0" ) ): try: A_ = hf_hub_download( __UpperCamelCase ,filename=_add_variant(__UpperCamelCase ,__UpperCamelCase ) ,cache_dir=__UpperCamelCase ,force_download=__UpperCamelCase ,proxies=__UpperCamelCase ,resume_download=__UpperCamelCase ,local_files_only=__UpperCamelCase ,use_auth_token=__UpperCamelCase ,user_agent=__UpperCamelCase ,subfolder=__UpperCamelCase ,revision=revision or commit_hash ,) warnings.warn( f'''Loading the variant {revision} from {pretrained_model_name_or_path} via `revision=\'{revision}\'` is deprecated. Loading instead from `revision=\'main\'` with `variant={revision}`. Loading model variants via `revision=\'{revision}\'` will be removed in diffusers v1. Please use `variant=\'{revision}\'` instead.''' ,__UpperCamelCase ,) return model_file except: # noqa: E722 warnings.warn( f'''You are loading the variant {revision} from {pretrained_model_name_or_path} via `revision=\'{revision}\'`. This behavior is deprecated and will be removed in diffusers v1. One should use `variant=\'{revision}\'` instead. However, it appears that {pretrained_model_name_or_path} currently does not have a {_add_variant(__UpperCamelCase ,__UpperCamelCase )} file in the \'main\' branch of {pretrained_model_name_or_path}. \n The Diffusers team and community would be very grateful if you could open an issue: https://github.com/huggingface/diffusers/issues/new with the title \'{pretrained_model_name_or_path} is missing {_add_variant(__UpperCamelCase ,__UpperCamelCase )}\' so that the correct variant file can be added.''' ,__UpperCamelCase ,) try: # 2. Load model file as usual A_ = hf_hub_download( __UpperCamelCase ,filename=__UpperCamelCase ,cache_dir=__UpperCamelCase ,force_download=__UpperCamelCase ,proxies=__UpperCamelCase ,resume_download=__UpperCamelCase ,local_files_only=__UpperCamelCase ,use_auth_token=__UpperCamelCase ,user_agent=__UpperCamelCase ,subfolder=__UpperCamelCase ,revision=revision or commit_hash ,) return model_file except RepositoryNotFoundError: raise EnvironmentError( f'''{pretrained_model_name_or_path} is not a local folder and is not a valid model identifier ''' "listed on 'https://huggingface.co/models'\nIf this is a private repository, make sure to pass a " "token having permission to this repo with `use_auth_token` or log in with `huggingface-cli " "login`." ) except RevisionNotFoundError: raise EnvironmentError( f'''{revision} is not a valid git identifier (branch name, tag name or commit id) that exists for ''' "this model name. Check the model page at " f'''\'https://huggingface.co/{pretrained_model_name_or_path}\' for available revisions.''' ) except EntryNotFoundError: raise EnvironmentError( f'''{pretrained_model_name_or_path} does not appear to have a file named {weights_name}.''' ) except HTTPError as err: raise EnvironmentError( f'''There was a specific connection error when trying to load {pretrained_model_name_or_path}:\n{err}''' ) except ValueError: raise EnvironmentError( f'''We couldn\'t connect to \'{HUGGINGFACE_CO_RESOLVE_ENDPOINT}\' to load this model, couldn\'t find it''' f''' in the cached files and it looks like {pretrained_model_name_or_path} is not the path to a''' f''' directory containing a file named {weights_name} or''' " \nCheckout your internet connection or see how to run the library in" " offline mode at 'https://huggingface.co/docs/diffusers/installation#offline-mode'." ) except EnvironmentError: raise EnvironmentError( f'''Can\'t load the model for \'{pretrained_model_name_or_path}\'. If you were trying to load it from ''' "'https://huggingface.co/models', make sure you don't have a local directory with the same name. " f'''Otherwise, make sure \'{pretrained_model_name_or_path}\' is the correct path to a directory ''' f'''containing a file named {weights_name}''' )
312
1
import warnings from typing import List import numpy as np from ...processing_utils import ProcessorMixin from ...tokenization_utils_base import BatchEncoding from ...utils import is_flax_available, is_tf_available, is_torch_available class _a ( snake_case_ ): """simple docstring""" _lowerCamelCase : Union[str, Any] = ['image_processor', 'tokenizer'] _lowerCamelCase : Tuple = 'OwlViTImageProcessor' _lowerCamelCase : List[Any] = ('CLIPTokenizer', 'CLIPTokenizerFast') def __init__( self : Optional[Any] , UpperCAmelCase : int=None , UpperCAmelCase : Union[str, Any]=None , **UpperCAmelCase : Any ): A_ = None if "feature_extractor" in kwargs: warnings.warn( "The `feature_extractor` argument is deprecated and will be removed in v5, use `image_processor`" " instead." , UpperCAmelCase , ) A_ = kwargs.pop("feature_extractor" ) A_ = image_processor if image_processor is not None else feature_extractor if image_processor is None: raise ValueError("You need to specify an `image_processor`." ) if tokenizer is None: raise ValueError("You need to specify a `tokenizer`." ) super().__init__(UpperCAmelCase , UpperCAmelCase ) def __call__( self : Optional[int] , UpperCAmelCase : List[str]=None , UpperCAmelCase : List[Any]=None , UpperCAmelCase : Optional[int]=None , UpperCAmelCase : Dict="max_length" , UpperCAmelCase : Optional[Any]="np" , **UpperCAmelCase : Optional[int] ): if text is None and query_images is None and images is None: raise ValueError( "You have to specify at least one text or query image or image. All three cannot be none." ) if text is not None: if isinstance(UpperCAmelCase , UpperCAmelCase ) or (isinstance(UpperCAmelCase , UpperCAmelCase ) and not isinstance(text[0] , UpperCAmelCase )): A_ = [self.tokenizer(UpperCAmelCase , padding=UpperCAmelCase , return_tensors=UpperCAmelCase , **UpperCAmelCase )] elif isinstance(UpperCAmelCase , UpperCAmelCase ) and isinstance(text[0] , UpperCAmelCase ): A_ = [] # Maximum number of queries across batch A_ = max([len(UpperCAmelCase ) for t in text] ) # Pad all batch samples to max number of text queries for t in text: if len(UpperCAmelCase ) != max_num_queries: A_ = t + [" "] * (max_num_queries - len(UpperCAmelCase )) A_ = self.tokenizer(UpperCAmelCase , padding=UpperCAmelCase , return_tensors=UpperCAmelCase , **UpperCAmelCase ) encodings.append(UpperCAmelCase ) else: raise TypeError("Input text should be a string, a list of strings or a nested list of strings" ) if return_tensors == "np": A_ = np.concatenate([encoding["input_ids"] for encoding in encodings] , axis=0 ) A_ = np.concatenate([encoding["attention_mask"] for encoding in encodings] , axis=0 ) elif return_tensors == "jax" and is_flax_available(): import jax.numpy as jnp A_ = jnp.concatenate([encoding["input_ids"] for encoding in encodings] , axis=0 ) A_ = jnp.concatenate([encoding["attention_mask"] for encoding in encodings] , axis=0 ) elif return_tensors == "pt" and is_torch_available(): import torch A_ = torch.cat([encoding["input_ids"] for encoding in encodings] , dim=0 ) A_ = torch.cat([encoding["attention_mask"] for encoding in encodings] , dim=0 ) elif return_tensors == "tf" and is_tf_available(): import tensorflow as tf A_ = tf.stack([encoding["input_ids"] for encoding in encodings] , axis=0 ) A_ = tf.stack([encoding["attention_mask"] for encoding in encodings] , axis=0 ) else: raise ValueError("Target return tensor type could not be returned" ) A_ = BatchEncoding() A_ = input_ids A_ = attention_mask if query_images is not None: A_ = BatchEncoding() A_ = self.image_processor( UpperCAmelCase , return_tensors=UpperCAmelCase , **UpperCAmelCase ).pixel_values A_ = query_pixel_values if images is not None: A_ = self.image_processor(UpperCAmelCase , return_tensors=UpperCAmelCase , **UpperCAmelCase ) if text is not None and images is not None: A_ = image_features.pixel_values return encoding elif query_images is not None and images is not None: A_ = image_features.pixel_values return encoding elif text is not None or query_images is not None: return encoding else: return BatchEncoding(data=dict(**UpperCAmelCase ) , tensor_type=UpperCAmelCase ) def __A ( self : Optional[Any] , *UpperCAmelCase : Union[str, Any] , **UpperCAmelCase : List[Any] ): return self.image_processor.post_process(*UpperCAmelCase , **UpperCAmelCase ) def __A ( self : str , *UpperCAmelCase : str , **UpperCAmelCase : Union[str, Any] ): return self.image_processor.post_process_object_detection(*UpperCAmelCase , **UpperCAmelCase ) def __A ( self : List[Any] , *UpperCAmelCase : int , **UpperCAmelCase : int ): return self.image_processor.post_process_image_guided_detection(*UpperCAmelCase , **UpperCAmelCase ) def __A ( self : List[Any] , *UpperCAmelCase : Optional[int] , **UpperCAmelCase : Any ): return self.tokenizer.batch_decode(*UpperCAmelCase , **UpperCAmelCase ) def __A ( self : Tuple , *UpperCAmelCase : Dict , **UpperCAmelCase : str ): return self.tokenizer.decode(*UpperCAmelCase , **UpperCAmelCase ) @property def __A ( self : Union[str, Any] ): warnings.warn( "`feature_extractor_class` is deprecated and will be removed in v5. Use `image_processor_class` instead." , UpperCAmelCase , ) return self.image_processor_class @property def __A ( self : Optional[Any] ): warnings.warn( "`feature_extractor` is deprecated and will be removed in v5. Use `image_processor` instead." , UpperCAmelCase , ) return self.image_processor
312
# Copyright 2023 The HuggingFace Team. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available __a :Any = { 'configuration_mgp_str': ['MGP_STR_PRETRAINED_CONFIG_ARCHIVE_MAP', 'MgpstrConfig'], 'processing_mgp_str': ['MgpstrProcessor'], 'tokenization_mgp_str': ['MgpstrTokenizer'], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __a :Optional[Any] = [ 'MGP_STR_PRETRAINED_MODEL_ARCHIVE_LIST', 'MgpstrModel', 'MgpstrPreTrainedModel', 'MgpstrForSceneTextRecognition', ] if TYPE_CHECKING: from .configuration_mgp_str import MGP_STR_PRETRAINED_CONFIG_ARCHIVE_MAP, MgpstrConfig from .processing_mgp_str import MgpstrProcessor from .tokenization_mgp_str import MgpstrTokenizer try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_mgp_str import ( MGP_STR_PRETRAINED_MODEL_ARCHIVE_LIST, MgpstrForSceneTextRecognition, MgpstrModel, MgpstrPreTrainedModel, ) else: import sys __a :List[Any] = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
312
1
import argparse import json import os import time import zipfile from get_ci_error_statistics import download_artifact, get_artifacts_links from transformers import logging __a :int = logging.get_logger(__name__) def __snake_case ( __UpperCamelCase : Optional[Any] ,__UpperCamelCase : List[Any] ): """simple docstring""" A_ = set() A_ = [] def parse_line(__UpperCamelCase : List[Any] ): for line in fp: if isinstance(__UpperCamelCase ,__UpperCamelCase ): A_ = line.decode("UTF-8" ) if "warnings summary (final)" in line: continue # This means we are outside the body of a warning elif not line.startswith(" " ): # process a single warning and move it to `selected_warnings`. if len(__UpperCamelCase ) > 0: A_ = "\n".join(__UpperCamelCase ) # Only keep the warnings specified in `targets` if any(f''': {x}: ''' in warning for x in targets ): selected_warnings.add(__UpperCamelCase ) buffer.clear() continue else: A_ = line.strip() buffer.append(__UpperCamelCase ) if from_gh: for filename in os.listdir(__UpperCamelCase ): A_ = os.path.join(__UpperCamelCase ,__UpperCamelCase ) if not os.path.isdir(__UpperCamelCase ): # read the file if filename != "warnings.txt": continue with open(__UpperCamelCase ) as fp: parse_line(__UpperCamelCase ) else: try: with zipfile.ZipFile(__UpperCamelCase ) as z: for filename in z.namelist(): if not os.path.isdir(__UpperCamelCase ): # read the file if filename != "warnings.txt": continue with z.open(__UpperCamelCase ) as fp: parse_line(__UpperCamelCase ) except Exception: logger.warning( f'''{artifact_path} is either an invalid zip file or something else wrong. This file is skipped.''' ) return selected_warnings def __snake_case ( __UpperCamelCase : Tuple ,__UpperCamelCase : Any ): """simple docstring""" A_ = set() A_ = [os.path.join(__UpperCamelCase ,__UpperCamelCase ) for p in os.listdir(__UpperCamelCase ) if (p.endswith(".zip" ) or from_gh)] for p in paths: selected_warnings.update(extract_warnings_from_single_artifact(__UpperCamelCase ,__UpperCamelCase ) ) return selected_warnings if __name__ == "__main__": def __snake_case ( __UpperCamelCase : Dict ): """simple docstring""" return values.split("," ) __a :Optional[Any] = argparse.ArgumentParser() # Required parameters parser.add_argument('--workflow_run_id', type=str, required=True, help='A GitHub Actions workflow run id.') parser.add_argument( '--output_dir', type=str, required=True, help='Where to store the downloaded artifacts and other result files.', ) parser.add_argument('--token', default=None, type=str, help='A token that has actions:read permission.') # optional parameters parser.add_argument( '--targets', default='DeprecationWarning,UserWarning,FutureWarning', type=list_str, help='Comma-separated list of target warning(s) which we want to extract.', ) parser.add_argument( '--from_gh', action='store_true', help='If running from a GitHub action workflow and collecting warnings from its artifacts.', ) __a :str = parser.parse_args() __a :str = args.from_gh if from_gh: # The artifacts have to be downloaded using `actions/download-artifact@v3` pass else: os.makedirs(args.output_dir, exist_ok=True) # get download links __a :Any = get_artifacts_links(args.workflow_run_id, token=args.token) with open(os.path.join(args.output_dir, 'artifacts.json'), 'w', encoding='UTF-8') as fp: json.dump(artifacts, fp, ensure_ascii=False, indent=4) # download artifacts for idx, (name, url) in enumerate(artifacts.items()): print(name) print(url) print('=' * 80) download_artifact(name, url, args.output_dir, args.token) # Be gentle to GitHub time.sleep(1) # extract warnings from artifacts __a :Optional[int] = extract_warnings(args.output_dir, args.targets) __a :Optional[int] = sorted(selected_warnings) with open(os.path.join(args.output_dir, 'selected_warnings.json'), 'w', encoding='UTF-8') as fp: json.dump(selected_warnings, fp, ensure_ascii=False, indent=4)
312
import functools from typing import Any def __snake_case ( __UpperCamelCase : str ,__UpperCamelCase : list[str] ): """simple docstring""" if not isinstance(__UpperCamelCase ,__UpperCamelCase ) or len(__UpperCamelCase ) == 0: raise ValueError("the string should be not empty string" ) if not isinstance(__UpperCamelCase ,__UpperCamelCase ) or not all( isinstance(__UpperCamelCase ,__UpperCamelCase ) and len(__UpperCamelCase ) > 0 for item in words ): raise ValueError("the words should be a list of non-empty strings" ) # Build trie A_ = {} A_ = "WORD_KEEPER" for word in words: A_ = trie for c in word: if c not in trie_node: A_ = {} A_ = trie_node[c] A_ = True A_ = len(__UpperCamelCase ) # Dynamic programming method @functools.cache def is_breakable(__UpperCamelCase : int ) -> bool: if index == len_string: return True A_ = trie for i in range(__UpperCamelCase ,__UpperCamelCase ): A_ = trie_node.get(string[i] ,__UpperCamelCase ) if trie_node is None: return False if trie_node.get(__UpperCamelCase ,__UpperCamelCase ) and is_breakable(i + 1 ): return True return False return is_breakable(0 ) if __name__ == "__main__": import doctest doctest.testmod()
312
1
import argparse import torch from transformers import ( WavaVecaConfig, WavaVecaFeatureExtractor, WavaVecaForAudioFrameClassification, WavaVecaForSequenceClassification, WavaVecaForXVector, logging, ) logging.set_verbosity_info() __a :Optional[Any] = logging.get_logger(__name__) def __snake_case ( __UpperCamelCase : Optional[int] ,__UpperCamelCase : Union[str, Any] ,__UpperCamelCase : str ): """simple docstring""" A_ = WavaVecaForSequenceClassification.from_pretrained(__UpperCamelCase ,config=__UpperCamelCase ) A_ = downstream_dict["projector.weight"] A_ = downstream_dict["projector.bias"] A_ = downstream_dict["model.post_net.linear.weight"] A_ = downstream_dict["model.post_net.linear.bias"] return model def __snake_case ( __UpperCamelCase : Any ,__UpperCamelCase : Optional[Any] ,__UpperCamelCase : Tuple ): """simple docstring""" A_ = WavaVecaForAudioFrameClassification.from_pretrained(__UpperCamelCase ,config=__UpperCamelCase ) A_ = downstream_dict["model.linear.weight"] A_ = downstream_dict["model.linear.bias"] return model def __snake_case ( __UpperCamelCase : int ,__UpperCamelCase : List[str] ,__UpperCamelCase : List[Any] ): """simple docstring""" A_ = WavaVecaForXVector.from_pretrained(__UpperCamelCase ,config=__UpperCamelCase ) A_ = downstream_dict["connector.weight"] A_ = downstream_dict["connector.bias"] for i, kernel_size in enumerate(hf_config.tdnn_kernel ): A_ = downstream_dict[ f'''model.framelevel_feature_extractor.module.{i}.kernel.weight''' ] A_ = downstream_dict[f'''model.framelevel_feature_extractor.module.{i}.kernel.bias'''] A_ = downstream_dict["model.utterancelevel_feature_extractor.linear1.weight"] A_ = downstream_dict["model.utterancelevel_feature_extractor.linear1.bias"] A_ = downstream_dict["model.utterancelevel_feature_extractor.linear2.weight"] A_ = downstream_dict["model.utterancelevel_feature_extractor.linear2.bias"] A_ = downstream_dict["objective.W"] return model @torch.no_grad() def __snake_case ( __UpperCamelCase : Union[str, Any] ,__UpperCamelCase : Dict ,__UpperCamelCase : Dict ,__UpperCamelCase : Optional[int] ): """simple docstring""" A_ = torch.load(__UpperCamelCase ,map_location="cpu" ) A_ = checkpoint["Downstream"] A_ = WavaVecaConfig.from_pretrained(__UpperCamelCase ) A_ = WavaVecaFeatureExtractor.from_pretrained( __UpperCamelCase ,return_attention_mask=__UpperCamelCase ,do_normalize=__UpperCamelCase ) A_ = hf_config.architectures[0] if arch.endswith("ForSequenceClassification" ): A_ = convert_classification(__UpperCamelCase ,__UpperCamelCase ,__UpperCamelCase ) elif arch.endswith("ForAudioFrameClassification" ): A_ = convert_diarization(__UpperCamelCase ,__UpperCamelCase ,__UpperCamelCase ) elif arch.endswith("ForXVector" ): A_ = convert_xvector(__UpperCamelCase ,__UpperCamelCase ,__UpperCamelCase ) else: raise NotImplementedError(f'''S3PRL weights conversion is not supported for {arch}''' ) if hf_config.use_weighted_layer_sum: A_ = checkpoint["Featurizer"]["weights"] hf_feature_extractor.save_pretrained(__UpperCamelCase ) hf_model.save_pretrained(__UpperCamelCase ) if __name__ == "__main__": __a :Dict = argparse.ArgumentParser() parser.add_argument( '--base_model_name', default=None, type=str, help='Name of the huggingface pretrained base model.' ) parser.add_argument('--config_path', default=None, type=str, help='Path to the huggingface classifier config.') parser.add_argument('--checkpoint_path', default=None, type=str, help='Path to the s3prl checkpoint.') parser.add_argument('--model_dump_path', default=None, type=str, help='Path to the final converted model.') __a :List[Any] = parser.parse_args() convert_saprl_checkpoint(args.base_model_name, args.config_path, args.checkpoint_path, args.model_dump_path)
312
import json from typing import List, Optional, Tuple from tokenizers import normalizers from ...tokenization_utils_fast import PreTrainedTokenizerFast from .tokenization_electra import ElectraTokenizer __a :List[str] = {'vocab_file': 'vocab.txt', 'tokenizer_file': 'tokenizer.json'} __a :Union[str, Any] = { 'vocab_file': { 'google/electra-small-generator': ( 'https://huggingface.co/google/electra-small-generator/resolve/main/vocab.txt' ), 'google/electra-base-generator': 'https://huggingface.co/google/electra-base-generator/resolve/main/vocab.txt', 'google/electra-large-generator': ( 'https://huggingface.co/google/electra-large-generator/resolve/main/vocab.txt' ), 'google/electra-small-discriminator': ( 'https://huggingface.co/google/electra-small-discriminator/resolve/main/vocab.txt' ), 'google/electra-base-discriminator': ( 'https://huggingface.co/google/electra-base-discriminator/resolve/main/vocab.txt' ), 'google/electra-large-discriminator': ( 'https://huggingface.co/google/electra-large-discriminator/resolve/main/vocab.txt' ), }, 'tokenizer_file': { 'google/electra-small-generator': ( 'https://huggingface.co/google/electra-small-generator/resolve/main/tokenizer.json' ), 'google/electra-base-generator': ( 'https://huggingface.co/google/electra-base-generator/resolve/main/tokenizer.json' ), 'google/electra-large-generator': ( 'https://huggingface.co/google/electra-large-generator/resolve/main/tokenizer.json' ), 'google/electra-small-discriminator': ( 'https://huggingface.co/google/electra-small-discriminator/resolve/main/tokenizer.json' ), 'google/electra-base-discriminator': ( 'https://huggingface.co/google/electra-base-discriminator/resolve/main/tokenizer.json' ), 'google/electra-large-discriminator': ( 'https://huggingface.co/google/electra-large-discriminator/resolve/main/tokenizer.json' ), }, } __a :Optional[int] = { 'google/electra-small-generator': 512, 'google/electra-base-generator': 512, 'google/electra-large-generator': 512, 'google/electra-small-discriminator': 512, 'google/electra-base-discriminator': 512, 'google/electra-large-discriminator': 512, } __a :str = { 'google/electra-small-generator': {'do_lower_case': True}, 'google/electra-base-generator': {'do_lower_case': True}, 'google/electra-large-generator': {'do_lower_case': True}, 'google/electra-small-discriminator': {'do_lower_case': True}, 'google/electra-base-discriminator': {'do_lower_case': True}, 'google/electra-large-discriminator': {'do_lower_case': True}, } class _a ( snake_case_ ): """simple docstring""" _lowerCamelCase : Tuple = VOCAB_FILES_NAMES _lowerCamelCase : Union[str, Any] = PRETRAINED_VOCAB_FILES_MAP _lowerCamelCase : int = PRETRAINED_INIT_CONFIGURATION _lowerCamelCase : List[Any] = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES _lowerCamelCase : int = ElectraTokenizer def __init__( self : Tuple , UpperCAmelCase : Dict=None , UpperCAmelCase : Optional[int]=None , UpperCAmelCase : Any=True , UpperCAmelCase : Any="[UNK]" , UpperCAmelCase : Union[str, Any]="[SEP]" , UpperCAmelCase : List[Any]="[PAD]" , UpperCAmelCase : Union[str, Any]="[CLS]" , UpperCAmelCase : List[Any]="[MASK]" , UpperCAmelCase : List[str]=True , UpperCAmelCase : Any=None , **UpperCAmelCase : Union[str, Any] , ): super().__init__( UpperCAmelCase , tokenizer_file=UpperCAmelCase , do_lower_case=UpperCAmelCase , unk_token=UpperCAmelCase , sep_token=UpperCAmelCase , pad_token=UpperCAmelCase , cls_token=UpperCAmelCase , mask_token=UpperCAmelCase , tokenize_chinese_chars=UpperCAmelCase , strip_accents=UpperCAmelCase , **UpperCAmelCase , ) A_ = json.loads(self.backend_tokenizer.normalizer.__getstate__() ) if ( normalizer_state.get("lowercase" , UpperCAmelCase ) != do_lower_case or normalizer_state.get("strip_accents" , UpperCAmelCase ) != strip_accents or normalizer_state.get("handle_chinese_chars" , UpperCAmelCase ) != tokenize_chinese_chars ): A_ = getattr(UpperCAmelCase , normalizer_state.pop("type" ) ) A_ = do_lower_case A_ = strip_accents A_ = tokenize_chinese_chars A_ = normalizer_class(**UpperCAmelCase ) A_ = do_lower_case def __A ( self : int , UpperCAmelCase : List[Any] , UpperCAmelCase : Union[str, Any]=None ): A_ = [self.cls_token_id] + token_ids_a + [self.sep_token_id] if token_ids_a: output += token_ids_a + [self.sep_token_id] return output def __A ( self : Union[str, Any] , UpperCAmelCase : List[int] , UpperCAmelCase : Optional[List[int]] = None ): A_ = [self.sep_token_id] A_ = [self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep ) * [0] return len(cls + token_ids_a + sep ) * [0] + len(token_ids_a + sep ) * [1] def __A ( self : Tuple , UpperCAmelCase : str , UpperCAmelCase : Optional[str] = None ): A_ = self._tokenizer.model.save(UpperCAmelCase , name=UpperCAmelCase ) return tuple(UpperCAmelCase )
312
1
from ..utils import DummyObject, requires_backends class _a ( metaclass=snake_case_ ): """simple docstring""" _lowerCamelCase : str = ['torch', 'transformers', 'onnx'] def __init__( self : int , *UpperCAmelCase : str , **UpperCAmelCase : Any ): requires_backends(self , ["torch", "transformers", "onnx"] ) @classmethod def __A ( cls : Optional[int] , *UpperCAmelCase : int , **UpperCAmelCase : Union[str, Any] ): requires_backends(cls , ["torch", "transformers", "onnx"] ) @classmethod def __A ( cls : Optional[Any] , *UpperCAmelCase : Optional[int] , **UpperCAmelCase : Tuple ): requires_backends(cls , ["torch", "transformers", "onnx"] ) class _a ( metaclass=snake_case_ ): """simple docstring""" _lowerCamelCase : Tuple = ['torch', 'transformers', 'onnx'] def __init__( self : Tuple , *UpperCAmelCase : Dict , **UpperCAmelCase : Optional[Any] ): requires_backends(self , ["torch", "transformers", "onnx"] ) @classmethod def __A ( cls : Dict , *UpperCAmelCase : List[str] , **UpperCAmelCase : Optional[Any] ): requires_backends(cls , ["torch", "transformers", "onnx"] ) @classmethod def __A ( cls : Tuple , *UpperCAmelCase : Any , **UpperCAmelCase : Tuple ): requires_backends(cls , ["torch", "transformers", "onnx"] ) class _a ( metaclass=snake_case_ ): """simple docstring""" _lowerCamelCase : Any = ['torch', 'transformers', 'onnx'] def __init__( self : int , *UpperCAmelCase : Any , **UpperCAmelCase : str ): requires_backends(self , ["torch", "transformers", "onnx"] ) @classmethod def __A ( cls : Tuple , *UpperCAmelCase : Optional[Any] , **UpperCAmelCase : int ): requires_backends(cls , ["torch", "transformers", "onnx"] ) @classmethod def __A ( cls : List[str] , *UpperCAmelCase : Any , **UpperCAmelCase : Tuple ): requires_backends(cls , ["torch", "transformers", "onnx"] ) class _a ( metaclass=snake_case_ ): """simple docstring""" _lowerCamelCase : List[Any] = ['torch', 'transformers', 'onnx'] def __init__( self : Optional[int] , *UpperCAmelCase : str , **UpperCAmelCase : Tuple ): requires_backends(self , ["torch", "transformers", "onnx"] ) @classmethod def __A ( cls : Optional[int] , *UpperCAmelCase : Optional[int] , **UpperCAmelCase : Union[str, Any] ): requires_backends(cls , ["torch", "transformers", "onnx"] ) @classmethod def __A ( cls : Union[str, Any] , *UpperCAmelCase : int , **UpperCAmelCase : Optional[Any] ): requires_backends(cls , ["torch", "transformers", "onnx"] ) class _a ( metaclass=snake_case_ ): """simple docstring""" _lowerCamelCase : List[Any] = ['torch', 'transformers', 'onnx'] def __init__( self : Any , *UpperCAmelCase : Union[str, Any] , **UpperCAmelCase : List[Any] ): requires_backends(self , ["torch", "transformers", "onnx"] ) @classmethod def __A ( cls : Dict , *UpperCAmelCase : Tuple , **UpperCAmelCase : List[Any] ): requires_backends(cls , ["torch", "transformers", "onnx"] ) @classmethod def __A ( cls : Any , *UpperCAmelCase : Dict , **UpperCAmelCase : int ): requires_backends(cls , ["torch", "transformers", "onnx"] ) class _a ( metaclass=snake_case_ ): """simple docstring""" _lowerCamelCase : int = ['torch', 'transformers', 'onnx'] def __init__( self : str , *UpperCAmelCase : int , **UpperCAmelCase : Optional[int] ): requires_backends(self , ["torch", "transformers", "onnx"] ) @classmethod def __A ( cls : int , *UpperCAmelCase : Optional[int] , **UpperCAmelCase : Optional[Any] ): requires_backends(cls , ["torch", "transformers", "onnx"] ) @classmethod def __A ( cls : str , *UpperCAmelCase : Any , **UpperCAmelCase : Optional[Any] ): requires_backends(cls , ["torch", "transformers", "onnx"] )
312
# flake8: noqa # Lint as: python3 from typing import Dict, List, Optional, Type from .. import config from ..utils import logging from .formatting import ( ArrowFormatter, CustomFormatter, Formatter, PandasFormatter, PythonFormatter, TensorFormatter, format_table, query_table, ) from .np_formatter import NumpyFormatter __a :Optional[Any] = logging.get_logger(__name__) __a :Dict[Optional[str], Type[Formatter]] = {} __a :Dict[Optional[str], str] = {} __a :Dict[Optional[str], Exception] = {} def __snake_case ( __UpperCamelCase : type ,__UpperCamelCase : Optional[str] ,__UpperCamelCase : Optional[List[str]] = None ,): """simple docstring""" A_ = aliases if aliases is not None else [] if format_type in _FORMAT_TYPES: logger.warning( f'''Overwriting format type \'{format_type}\' ({_FORMAT_TYPES[format_type].__name__} -> {formatter_cls.__name__})''' ) A_ = formatter_cls for alias in set(aliases + [format_type] ): if alias in _FORMAT_TYPES_ALIASES: logger.warning( f'''Overwriting format type alias \'{alias}\' ({_FORMAT_TYPES_ALIASES[alias]} -> {format_type})''' ) A_ = format_type def __snake_case ( __UpperCamelCase : Exception ,__UpperCamelCase : Optional[str] ,__UpperCamelCase : Optional[List[str]] = None ): """simple docstring""" A_ = aliases if aliases is not None else [] for alias in set(aliases + [format_type] ): A_ = unavailable_error # Here we define all the available formatting functions that can be used by `Dataset.set_format` _register_formatter(PythonFormatter, None, aliases=['python']) _register_formatter(ArrowFormatter, 'arrow', aliases=['pa', 'pyarrow']) _register_formatter(NumpyFormatter, 'numpy', aliases=['np']) _register_formatter(PandasFormatter, 'pandas', aliases=['pd']) _register_formatter(CustomFormatter, 'custom') if config.TORCH_AVAILABLE: from .torch_formatter import TorchFormatter _register_formatter(TorchFormatter, 'torch', aliases=['pt', 'pytorch']) else: __a :List[Any] = ValueError('PyTorch needs to be installed to be able to return PyTorch tensors.') _register_unavailable_formatter(_torch_error, 'torch', aliases=['pt', 'pytorch']) if config.TF_AVAILABLE: from .tf_formatter import TFFormatter _register_formatter(TFFormatter, 'tensorflow', aliases=['tf']) else: __a :List[str] = ValueError('Tensorflow needs to be installed to be able to return Tensorflow tensors.') _register_unavailable_formatter(_tf_error, 'tensorflow', aliases=['tf']) if config.JAX_AVAILABLE: from .jax_formatter import JaxFormatter _register_formatter(JaxFormatter, 'jax', aliases=[]) else: __a :Tuple = ValueError('JAX needs to be installed to be able to return JAX arrays.') _register_unavailable_formatter(_jax_error, 'jax', aliases=[]) def __snake_case ( __UpperCamelCase : Optional[str] ): """simple docstring""" if format_type in _FORMAT_TYPES_ALIASES: return _FORMAT_TYPES_ALIASES[format_type] else: return format_type def __snake_case ( __UpperCamelCase : Optional[str] ,**__UpperCamelCase : List[Any] ): """simple docstring""" A_ = get_format_type_from_alias(__UpperCamelCase ) if format_type in _FORMAT_TYPES: return _FORMAT_TYPES[format_type](**__UpperCamelCase ) if format_type in _FORMAT_TYPES_ALIASES_UNAVAILABLE: raise _FORMAT_TYPES_ALIASES_UNAVAILABLE[format_type] else: raise ValueError( f'''Return type should be None or selected in {list(type for type in _FORMAT_TYPES.keys() if type != None )}, but got \'{format_type}\'''' )
312
1
def __snake_case ( __UpperCamelCase : int = 50 ): """simple docstring""" A_ = [[0] * 3 for _ in range(length + 1 )] for row_length in range(length + 1 ): for tile_length in range(2 ,5 ): for tile_start in range(row_length - tile_length + 1 ): different_colour_ways_number[row_length][tile_length - 2] += ( different_colour_ways_number[row_length - tile_start - tile_length][ tile_length - 2 ] + 1 ) return sum(different_colour_ways_number[length] ) if __name__ == "__main__": print(F"{solution() = }")
312
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available, is_vision_available __a :int = { 'configuration_mask2former': [ 'MASK2FORMER_PRETRAINED_CONFIG_ARCHIVE_MAP', 'Mask2FormerConfig', ], } try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __a :Union[str, Any] = ['Mask2FormerImageProcessor'] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __a :Optional[Any] = [ 'MASK2FORMER_PRETRAINED_MODEL_ARCHIVE_LIST', 'Mask2FormerForUniversalSegmentation', 'Mask2FormerModel', 'Mask2FormerPreTrainedModel', ] if TYPE_CHECKING: from .configuration_maskaformer import MASK2FORMER_PRETRAINED_CONFIG_ARCHIVE_MAP, MaskaFormerConfig try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .image_processing_maskaformer import MaskaFormerImageProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_maskaformer import ( MASK2FORMER_PRETRAINED_MODEL_ARCHIVE_LIST, MaskaFormerForUniversalSegmentation, MaskaFormerModel, MaskaFormerPreTrainedModel, ) else: import sys __a :Tuple = _LazyModule(__name__, globals()['__file__'], _import_structure)
312
1
from collections import deque from math import floor from random import random from time import time class _a : """simple docstring""" def __init__( self : int ): A_ = {} def __A ( self : str , UpperCAmelCase : List[Any] , UpperCAmelCase : str , UpperCAmelCase : Dict=1 ): if self.graph.get(UpperCAmelCase ): if self.graph[u].count([w, v] ) == 0: self.graph[u].append([w, v] ) else: A_ = [[w, v]] if not self.graph.get(UpperCAmelCase ): A_ = [] def __A ( self : int ): return list(self.graph ) def __A ( self : Tuple , UpperCAmelCase : Union[str, Any] , UpperCAmelCase : str ): if self.graph.get(UpperCAmelCase ): for _ in self.graph[u]: if _[1] == v: self.graph[u].remove(UpperCAmelCase ) def __A ( self : List[str] , UpperCAmelCase : str=-2 , UpperCAmelCase : List[str]=-1 ): if s == d: return [] A_ = [] A_ = [] if s == -2: A_ = list(self.graph )[0] stack.append(UpperCAmelCase ) visited.append(UpperCAmelCase ) A_ = s while True: # check if there is any non isolated nodes if len(self.graph[s] ) != 0: A_ = s for node in self.graph[s]: if visited.count(node[1] ) < 1: if node[1] == d: visited.append(UpperCAmelCase ) return visited else: stack.append(node[1] ) visited.append(node[1] ) A_ = node[1] break # check if all the children are visited if s == ss: stack.pop() if len(UpperCAmelCase ) != 0: A_ = stack[len(UpperCAmelCase ) - 1] else: A_ = ss # check if se have reached the starting point if len(UpperCAmelCase ) == 0: return visited def __A ( self : List[str] , UpperCAmelCase : str=-1 ): if c == -1: A_ = floor(random() * 10000 ) + 10 for i in range(UpperCAmelCase ): # every vertex has max 100 edges for _ in range(floor(random() * 102 ) + 1 ): A_ = floor(random() * c ) + 1 if n != i: self.add_pair(UpperCAmelCase , UpperCAmelCase , 1 ) def __A ( self : Optional[int] , UpperCAmelCase : List[str]=-2 ): A_ = deque() A_ = [] if s == -2: A_ = list(self.graph )[0] d.append(UpperCAmelCase ) visited.append(UpperCAmelCase ) while d: A_ = d.popleft() if len(self.graph[s] ) != 0: for node in self.graph[s]: if visited.count(node[1] ) < 1: d.append(node[1] ) visited.append(node[1] ) return visited def __A ( self : Tuple , UpperCAmelCase : Any ): A_ = 0 for x in self.graph: for y in self.graph[x]: if y[1] == u: count += 1 return count def __A ( self : Any , UpperCAmelCase : Tuple ): return len(self.graph[u] ) def __A ( self : List[Any] , UpperCAmelCase : Tuple=-2 ): A_ = [] A_ = [] if s == -2: A_ = list(self.graph )[0] stack.append(UpperCAmelCase ) visited.append(UpperCAmelCase ) A_ = s A_ = [] while True: # check if there is any non isolated nodes if len(self.graph[s] ) != 0: A_ = s for node in self.graph[s]: if visited.count(node[1] ) < 1: stack.append(node[1] ) visited.append(node[1] ) A_ = node[1] break # check if all the children are visited if s == ss: sorted_nodes.append(stack.pop() ) if len(UpperCAmelCase ) != 0: A_ = stack[len(UpperCAmelCase ) - 1] else: A_ = ss # check if se have reached the starting point if len(UpperCAmelCase ) == 0: return sorted_nodes def __A ( self : List[Any] ): A_ = [] A_ = [] A_ = list(self.graph )[0] stack.append(UpperCAmelCase ) visited.append(UpperCAmelCase ) A_ = -2 A_ = [] A_ = s A_ = False A_ = set() while True: # check if there is any non isolated nodes if len(self.graph[s] ) != 0: A_ = s for node in self.graph[s]: if ( visited.count(node[1] ) > 0 and node[1] != parent and indirect_parents.count(node[1] ) > 0 and not on_the_way_back ): A_ = len(UpperCAmelCase ) - 1 while len_stack >= 0: if stack[len_stack] == node[1]: anticipating_nodes.add(node[1] ) break else: anticipating_nodes.add(stack[len_stack] ) len_stack -= 1 if visited.count(node[1] ) < 1: stack.append(node[1] ) visited.append(node[1] ) A_ = node[1] break # check if all the children are visited if s == ss: stack.pop() A_ = True if len(UpperCAmelCase ) != 0: A_ = stack[len(UpperCAmelCase ) - 1] else: A_ = False indirect_parents.append(UpperCAmelCase ) A_ = s A_ = ss # check if se have reached the starting point if len(UpperCAmelCase ) == 0: return list(UpperCAmelCase ) def __A ( self : str ): A_ = [] A_ = [] A_ = list(self.graph )[0] stack.append(UpperCAmelCase ) visited.append(UpperCAmelCase ) A_ = -2 A_ = [] A_ = s A_ = False A_ = set() while True: # check if there is any non isolated nodes if len(self.graph[s] ) != 0: A_ = s for node in self.graph[s]: if ( visited.count(node[1] ) > 0 and node[1] != parent and indirect_parents.count(node[1] ) > 0 and not on_the_way_back ): A_ = len(UpperCAmelCase ) - 1 while len_stack_minus_one >= 0: if stack[len_stack_minus_one] == node[1]: anticipating_nodes.add(node[1] ) break else: return True if visited.count(node[1] ) < 1: stack.append(node[1] ) visited.append(node[1] ) A_ = node[1] break # check if all the children are visited if s == ss: stack.pop() A_ = True if len(UpperCAmelCase ) != 0: A_ = stack[len(UpperCAmelCase ) - 1] else: A_ = False indirect_parents.append(UpperCAmelCase ) A_ = s A_ = ss # check if se have reached the starting point if len(UpperCAmelCase ) == 0: return False def __A ( self : Optional[int] , UpperCAmelCase : Tuple=-2 , UpperCAmelCase : int=-1 ): A_ = time() self.dfs(UpperCAmelCase , UpperCAmelCase ) A_ = time() return end - begin def __A ( self : List[str] , UpperCAmelCase : Optional[int]=-2 ): A_ = time() self.bfs(UpperCAmelCase ) A_ = time() return end - begin class _a : """simple docstring""" def __init__( self : List[Any] ): A_ = {} def __A ( self : int , UpperCAmelCase : List[str] , UpperCAmelCase : Tuple , UpperCAmelCase : str=1 ): # check if the u exists if self.graph.get(UpperCAmelCase ): # if there already is a edge if self.graph[u].count([w, v] ) == 0: self.graph[u].append([w, v] ) else: # if u does not exist A_ = [[w, v]] # add the other way if self.graph.get(UpperCAmelCase ): # if there already is a edge if self.graph[v].count([w, u] ) == 0: self.graph[v].append([w, u] ) else: # if u does not exist A_ = [[w, u]] def __A ( self : Dict , UpperCAmelCase : Optional[int] , UpperCAmelCase : Union[str, Any] ): if self.graph.get(UpperCAmelCase ): for _ in self.graph[u]: if _[1] == v: self.graph[u].remove(UpperCAmelCase ) # the other way round if self.graph.get(UpperCAmelCase ): for _ in self.graph[v]: if _[1] == u: self.graph[v].remove(UpperCAmelCase ) def __A ( self : int , UpperCAmelCase : Tuple=-2 , UpperCAmelCase : Optional[int]=-1 ): if s == d: return [] A_ = [] A_ = [] if s == -2: A_ = list(self.graph )[0] stack.append(UpperCAmelCase ) visited.append(UpperCAmelCase ) A_ = s while True: # check if there is any non isolated nodes if len(self.graph[s] ) != 0: A_ = s for node in self.graph[s]: if visited.count(node[1] ) < 1: if node[1] == d: visited.append(UpperCAmelCase ) return visited else: stack.append(node[1] ) visited.append(node[1] ) A_ = node[1] break # check if all the children are visited if s == ss: stack.pop() if len(UpperCAmelCase ) != 0: A_ = stack[len(UpperCAmelCase ) - 1] else: A_ = ss # check if se have reached the starting point if len(UpperCAmelCase ) == 0: return visited def __A ( self : Optional[Any] , UpperCAmelCase : List[str]=-1 ): if c == -1: A_ = floor(random() * 10000 ) + 10 for i in range(UpperCAmelCase ): # every vertex has max 100 edges for _ in range(floor(random() * 102 ) + 1 ): A_ = floor(random() * c ) + 1 if n != i: self.add_pair(UpperCAmelCase , UpperCAmelCase , 1 ) def __A ( self : str , UpperCAmelCase : List[Any]=-2 ): A_ = deque() A_ = [] if s == -2: A_ = list(self.graph )[0] d.append(UpperCAmelCase ) visited.append(UpperCAmelCase ) while d: A_ = d.popleft() if len(self.graph[s] ) != 0: for node in self.graph[s]: if visited.count(node[1] ) < 1: d.append(node[1] ) visited.append(node[1] ) return visited def __A ( self : str , UpperCAmelCase : List[str] ): return len(self.graph[u] ) def __A ( self : Dict ): A_ = [] A_ = [] A_ = list(self.graph )[0] stack.append(UpperCAmelCase ) visited.append(UpperCAmelCase ) A_ = -2 A_ = [] A_ = s A_ = False A_ = set() while True: # check if there is any non isolated nodes if len(self.graph[s] ) != 0: A_ = s for node in self.graph[s]: if ( visited.count(node[1] ) > 0 and node[1] != parent and indirect_parents.count(node[1] ) > 0 and not on_the_way_back ): A_ = len(UpperCAmelCase ) - 1 while len_stack >= 0: if stack[len_stack] == node[1]: anticipating_nodes.add(node[1] ) break else: anticipating_nodes.add(stack[len_stack] ) len_stack -= 1 if visited.count(node[1] ) < 1: stack.append(node[1] ) visited.append(node[1] ) A_ = node[1] break # check if all the children are visited if s == ss: stack.pop() A_ = True if len(UpperCAmelCase ) != 0: A_ = stack[len(UpperCAmelCase ) - 1] else: A_ = False indirect_parents.append(UpperCAmelCase ) A_ = s A_ = ss # check if se have reached the starting point if len(UpperCAmelCase ) == 0: return list(UpperCAmelCase ) def __A ( self : Any ): A_ = [] A_ = [] A_ = list(self.graph )[0] stack.append(UpperCAmelCase ) visited.append(UpperCAmelCase ) A_ = -2 A_ = [] A_ = s A_ = False A_ = set() while True: # check if there is any non isolated nodes if len(self.graph[s] ) != 0: A_ = s for node in self.graph[s]: if ( visited.count(node[1] ) > 0 and node[1] != parent and indirect_parents.count(node[1] ) > 0 and not on_the_way_back ): A_ = len(UpperCAmelCase ) - 1 while len_stack_minus_one >= 0: if stack[len_stack_minus_one] == node[1]: anticipating_nodes.add(node[1] ) break else: return True if visited.count(node[1] ) < 1: stack.append(node[1] ) visited.append(node[1] ) A_ = node[1] break # check if all the children are visited if s == ss: stack.pop() A_ = True if len(UpperCAmelCase ) != 0: A_ = stack[len(UpperCAmelCase ) - 1] else: A_ = False indirect_parents.append(UpperCAmelCase ) A_ = s A_ = ss # check if se have reached the starting point if len(UpperCAmelCase ) == 0: return False def __A ( self : Any ): return list(self.graph ) def __A ( self : Any , UpperCAmelCase : Optional[Any]=-2 , UpperCAmelCase : Union[str, Any]=-1 ): A_ = time() self.dfs(UpperCAmelCase , UpperCAmelCase ) A_ = time() return end - begin def __A ( self : Optional[Any] , UpperCAmelCase : int=-2 ): A_ = time() self.bfs(UpperCAmelCase ) A_ = time() return end - begin
312
import copy from dataclasses import dataclass, field from typing import ClassVar, Dict from ..features import Audio, ClassLabel, Features from .base import TaskTemplate @dataclass(frozen=snake_case_ ) class _a ( snake_case_ ): """simple docstring""" _lowerCamelCase : str = field(default='audio-classification' , metadata={'include_in_asdict_even_if_is_default': True} ) _lowerCamelCase : ClassVar[Features] = Features({'audio': Audio()} ) _lowerCamelCase : ClassVar[Features] = Features({'labels': ClassLabel} ) _lowerCamelCase : str = "audio" _lowerCamelCase : str = "labels" def __A ( self : str , UpperCAmelCase : List[Any] ): if self.label_column not in features: raise ValueError(f'''Column {self.label_column} is not present in features.''' ) if not isinstance(features[self.label_column] , UpperCAmelCase ): raise ValueError(f'''Column {self.label_column} is not a ClassLabel.''' ) A_ = copy.deepcopy(self ) A_ = self.label_schema.copy() A_ = features[self.label_column] A_ = label_schema return task_template @property def __A ( self : List[str] ): return { self.audio_column: "audio", self.label_column: "labels", }
312
1
import json from typing import List, Optional, Tuple from tokenizers import normalizers from ....tokenization_utils_fast import PreTrainedTokenizerFast from ....utils import logging from .tokenization_retribert import RetriBertTokenizer __a :Dict = logging.get_logger(__name__) __a :str = {'vocab_file': 'vocab.txt', 'tokenizer_file': 'tokenizer.json'} __a :List[str] = { 'vocab_file': { 'yjernite/retribert-base-uncased': ( 'https://huggingface.co/yjernite/retribert-base-uncased/resolve/main/vocab.txt' ), }, 'tokenizer_file': { 'yjernite/retribert-base-uncased': ( 'https://huggingface.co/yjernite/retribert-base-uncased/resolve/main/tokenizer.json' ), }, } __a :List[Any] = { 'yjernite/retribert-base-uncased': 512, } __a :Dict = { 'yjernite/retribert-base-uncased': {'do_lower_case': True}, } class _a ( snake_case_ ): """simple docstring""" _lowerCamelCase : List[Any] = VOCAB_FILES_NAMES _lowerCamelCase : str = PRETRAINED_VOCAB_FILES_MAP _lowerCamelCase : str = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES _lowerCamelCase : Optional[Any] = PRETRAINED_INIT_CONFIGURATION _lowerCamelCase : str = RetriBertTokenizer _lowerCamelCase : int = ['input_ids', 'attention_mask'] def __init__( self : Tuple , UpperCAmelCase : Optional[int]=None , UpperCAmelCase : Optional[int]=None , UpperCAmelCase : Optional[Any]=True , UpperCAmelCase : Any="[UNK]" , UpperCAmelCase : Union[str, Any]="[SEP]" , UpperCAmelCase : Any="[PAD]" , UpperCAmelCase : List[Any]="[CLS]" , UpperCAmelCase : Tuple="[MASK]" , UpperCAmelCase : Optional[Any]=True , UpperCAmelCase : str=None , **UpperCAmelCase : Tuple , ): super().__init__( UpperCAmelCase , tokenizer_file=UpperCAmelCase , do_lower_case=UpperCAmelCase , unk_token=UpperCAmelCase , sep_token=UpperCAmelCase , pad_token=UpperCAmelCase , cls_token=UpperCAmelCase , mask_token=UpperCAmelCase , tokenize_chinese_chars=UpperCAmelCase , strip_accents=UpperCAmelCase , **UpperCAmelCase , ) A_ = json.loads(self.backend_tokenizer.normalizer.__getstate__() ) if ( normalizer_state.get("lowercase" , UpperCAmelCase ) != do_lower_case or normalizer_state.get("strip_accents" , UpperCAmelCase ) != strip_accents or normalizer_state.get("handle_chinese_chars" , UpperCAmelCase ) != tokenize_chinese_chars ): A_ = getattr(UpperCAmelCase , normalizer_state.pop("type" ) ) A_ = do_lower_case A_ = strip_accents A_ = tokenize_chinese_chars A_ = normalizer_class(**UpperCAmelCase ) A_ = do_lower_case def __A ( self : Tuple , UpperCAmelCase : Dict , UpperCAmelCase : Dict=None ): A_ = [self.cls_token_id] + token_ids_a + [self.sep_token_id] if token_ids_a: output += token_ids_a + [self.sep_token_id] return output def __A ( self : str , UpperCAmelCase : List[int] , UpperCAmelCase : Optional[List[int]] = None ): A_ = [self.sep_token_id] A_ = [self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep ) * [0] return len(cls + token_ids_a + sep ) * [0] + len(token_ids_a + sep ) * [1] def __A ( self : Any , UpperCAmelCase : str , UpperCAmelCase : Optional[str] = None ): A_ = self._tokenizer.model.save(UpperCAmelCase , name=UpperCAmelCase ) return tuple(UpperCAmelCase )
312
def __snake_case ( __UpperCamelCase : bytes ): """simple docstring""" return "".join([hex(__UpperCamelCase )[2:].zfill(2 ).upper() for byte in list(__UpperCamelCase )] ) def __snake_case ( __UpperCamelCase : str ): """simple docstring""" if (len(__UpperCamelCase ) % 2) != 0: raise ValueError( "Base16 encoded data is invalid:\nData does not have an even number of hex digits." ) # Check the character set - the standard base16 alphabet # is uppercase according to RFC3548 section 6 if not set(__UpperCamelCase ) <= set("0123456789ABCDEF" ): raise ValueError( "Base16 encoded data is invalid:\nData is not uppercase hex or it contains invalid characters." ) # For every two hexadecimal digits (= a byte), turn it into an integer. # Then, string the result together into bytes, and return it. return bytes(int(data[i] + data[i + 1] ,16 ) for i in range(0 ,len(__UpperCamelCase ) ,2 ) ) if __name__ == "__main__": import doctest doctest.testmod()
312
1
from math import pi def __snake_case ( __UpperCamelCase : int ,__UpperCamelCase : int ): """simple docstring""" return 2 * pi * radius * (angle / 360) if __name__ == "__main__": print(arc_length(90, 10))
312
import cva import numpy as np class _a : """simple docstring""" def __init__( self : Any , UpperCAmelCase : float , UpperCAmelCase : int ): if k in (0.04, 0.06): A_ = k A_ = window_size else: raise ValueError("invalid k value" ) def __str__( self : Optional[Any] ): return str(self.k ) def __A ( self : int , UpperCAmelCase : str ): A_ = cva.imread(UpperCAmelCase , 0 ) A_ , A_ = img.shape A_ = [] A_ = img.copy() A_ = cva.cvtColor(UpperCAmelCase , cva.COLOR_GRAY2RGB ) A_ , A_ = np.gradient(UpperCAmelCase ) A_ = dx**2 A_ = dy**2 A_ = dx * dy A_ = 0.04 A_ = self.window_size // 2 for y in range(UpperCAmelCase , h - offset ): for x in range(UpperCAmelCase , w - offset ): A_ = ixx[ y - offset : y + offset + 1, x - offset : x + offset + 1 ].sum() A_ = iyy[ y - offset : y + offset + 1, x - offset : x + offset + 1 ].sum() A_ = ixy[ y - offset : y + offset + 1, x - offset : x + offset + 1 ].sum() A_ = (wxx * wyy) - (wxy**2) A_ = wxx + wyy A_ = det - k * (trace**2) # Can change the value if r > 0.5: corner_list.append([x, y, r] ) color_img.itemset((y, x, 0) , 0 ) color_img.itemset((y, x, 1) , 0 ) color_img.itemset((y, x, 2) , 255 ) return color_img, corner_list if __name__ == "__main__": __a :List[str] = HarrisCorner(0.04, 3) __a , __a :str = edge_detect.detect('path_to_image') cva.imwrite('detect.png', color_img)
312
1
import logging import os from dataclasses import dataclass, field from typing import Dict, Optional import numpy as np from utils_multiple_choice import MultipleChoiceDataset, Split, processors import transformers from transformers import ( AutoConfig, AutoModelForMultipleChoice, AutoTokenizer, DataCollatorWithPadding, EvalPrediction, HfArgumentParser, Trainer, TrainingArguments, set_seed, ) from transformers.trainer_utils import is_main_process __a :List[str] = logging.getLogger(__name__) def __snake_case ( __UpperCamelCase : List[str] ,__UpperCamelCase : List[Any] ): """simple docstring""" return (preds == labels).mean() @dataclass class _a : """simple docstring""" _lowerCamelCase : str = field( metadata={'help': 'Path to pretrained model or model identifier from huggingface.co/models'} ) _lowerCamelCase : Optional[str] = field( default=snake_case_ , metadata={'help': 'Pretrained config name or path if not the same as model_name'} ) _lowerCamelCase : Optional[str] = field( default=snake_case_ , metadata={'help': 'Pretrained tokenizer name or path if not the same as model_name'} ) _lowerCamelCase : Optional[str] = field( default=snake_case_ , metadata={'help': 'Where do you want to store the pretrained models downloaded from huggingface.co'} , ) @dataclass class _a : """simple docstring""" _lowerCamelCase : str = field(metadata={'help': 'The name of the task to train on: ' + ', '.join(processors.keys() )} ) _lowerCamelCase : str = field(metadata={'help': 'Should contain the data files for the task.'} ) _lowerCamelCase : int = field( default=1_2_8 , metadata={ 'help': ( 'The maximum total input sequence length after tokenization. Sequences longer ' 'than this will be truncated, sequences shorter will be padded.' ) } , ) _lowerCamelCase : bool = field( default=snake_case_ , metadata={'help': 'Overwrite the cached training and evaluation sets'} ) def __snake_case ( ): """simple docstring""" A_ = HfArgumentParser((ModelArguments, DataTrainingArguments, TrainingArguments) ) A_ , A_ , A_ = parser.parse_args_into_dataclasses() if ( os.path.exists(training_args.output_dir ) and os.listdir(training_args.output_dir ) and training_args.do_train and not training_args.overwrite_output_dir ): raise ValueError( f'''Output directory ({training_args.output_dir}) already exists and is not empty. Use''' " --overwrite_output_dir to overcome." ) # 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.local_rank != -1 ) ,training_args.fpaa ,) # 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() transformers.utils.logging.enable_default_handler() transformers.utils.logging.enable_explicit_format() logger.info("Training/evaluation parameters %s" ,__UpperCamelCase ) # Set seed set_seed(training_args.seed ) try: A_ = processors[data_args.task_name]() A_ = processor.get_labels() A_ = len(__UpperCamelCase ) except KeyError: raise ValueError("Task not found: %s" % (data_args.task_name) ) # Load pretrained model and tokenizer # # Distributed training: # The .from_pretrained methods guarantee that only one local process can concurrently # download model & vocab. A_ = AutoConfig.from_pretrained( model_args.config_name if model_args.config_name else model_args.model_name_or_path ,num_labels=__UpperCamelCase ,finetuning_task=data_args.task_name ,cache_dir=model_args.cache_dir ,) A_ = 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 ,) A_ = AutoModelForMultipleChoice.from_pretrained( model_args.model_name_or_path ,from_tf=bool(".ckpt" in model_args.model_name_or_path ) ,config=__UpperCamelCase ,cache_dir=model_args.cache_dir ,) # Get datasets A_ = ( MultipleChoiceDataset( data_dir=data_args.data_dir ,tokenizer=__UpperCamelCase ,task=data_args.task_name ,max_seq_length=data_args.max_seq_length ,overwrite_cache=data_args.overwrite_cache ,mode=Split.train ,) if training_args.do_train else None ) A_ = ( MultipleChoiceDataset( data_dir=data_args.data_dir ,tokenizer=__UpperCamelCase ,task=data_args.task_name ,max_seq_length=data_args.max_seq_length ,overwrite_cache=data_args.overwrite_cache ,mode=Split.dev ,) if training_args.do_eval else None ) def compute_metrics(__UpperCamelCase : EvalPrediction ) -> Dict: A_ = np.argmax(p.predictions ,axis=1 ) return {"acc": simple_accuracy(__UpperCamelCase ,p.label_ids )} # Data collator A_ = DataCollatorWithPadding(__UpperCamelCase ,pad_to_multiple_of=8 ) if training_args.fpaa else None # Initialize our Trainer A_ = Trainer( model=__UpperCamelCase ,args=__UpperCamelCase ,train_dataset=__UpperCamelCase ,eval_dataset=__UpperCamelCase ,compute_metrics=__UpperCamelCase ,data_collator=__UpperCamelCase ,) # Training if training_args.do_train: trainer.train( model_path=model_args.model_name_or_path if os.path.isdir(model_args.model_name_or_path ) else None ) trainer.save_model() # For convenience, we also re-save the tokenizer to the same directory, # so that you can share your model easily on huggingface.co/models =) if trainer.is_world_master(): tokenizer.save_pretrained(training_args.output_dir ) # Evaluation A_ = {} if training_args.do_eval: logger.info("*** Evaluate ***" ) A_ = trainer.evaluate() A_ = os.path.join(training_args.output_dir ,"eval_results.txt" ) if trainer.is_world_master(): with open(__UpperCamelCase ,"w" ) as writer: logger.info("***** Eval results *****" ) for key, value in result.items(): logger.info(" %s = %s" ,__UpperCamelCase ,__UpperCamelCase ) writer.write("%s = %s\n" % (key, value) ) results.update(__UpperCamelCase ) return results def __snake_case ( __UpperCamelCase : Dict ): """simple docstring""" main() if __name__ == "__main__": main()
312
def __snake_case ( __UpperCamelCase : int = 1000 ): """simple docstring""" return sum(2 * a * ((a - 1) // 2) for a in range(3 ,n + 1 ) ) if __name__ == "__main__": print(solution())
312
1
from collections import OrderedDict from typing import Any, List, Mapping, Optional from ... import PreTrainedTokenizer, TensorType, is_torch_available from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfigWithPast, PatchingSpec from ...utils import logging __a :Optional[int] = logging.get_logger(__name__) __a :Tuple = { 'Salesforce/codegen-350M-nl': 'https://huggingface.co/Salesforce/codegen-350M-nl/resolve/main/config.json', 'Salesforce/codegen-350M-multi': 'https://huggingface.co/Salesforce/codegen-350M-multi/resolve/main/config.json', 'Salesforce/codegen-350M-mono': 'https://huggingface.co/Salesforce/codegen-350M-mono/resolve/main/config.json', 'Salesforce/codegen-2B-nl': 'https://huggingface.co/Salesforce/codegen-2B-nl/resolve/main/config.json', 'Salesforce/codegen-2B-multi': 'https://huggingface.co/Salesforce/codegen-2B-multi/resolve/main/config.json', 'Salesforce/codegen-2B-mono': 'https://huggingface.co/Salesforce/codegen-2B-mono/resolve/main/config.json', 'Salesforce/codegen-6B-nl': 'https://huggingface.co/Salesforce/codegen-6B-nl/resolve/main/config.json', 'Salesforce/codegen-6B-multi': 'https://huggingface.co/Salesforce/codegen-6B-multi/resolve/main/config.json', 'Salesforce/codegen-6B-mono': 'https://huggingface.co/Salesforce/codegen-6B-mono/resolve/main/config.json', 'Salesforce/codegen-16B-nl': 'https://huggingface.co/Salesforce/codegen-16B-nl/resolve/main/config.json', 'Salesforce/codegen-16B-multi': 'https://huggingface.co/Salesforce/codegen-16B-multi/resolve/main/config.json', 'Salesforce/codegen-16B-mono': 'https://huggingface.co/Salesforce/codegen-16B-mono/resolve/main/config.json', } class _a ( snake_case_ ): """simple docstring""" _lowerCamelCase : Optional[Any] = 'codegen' _lowerCamelCase : int = { 'max_position_embeddings': 'n_positions', 'hidden_size': 'n_embd', 'num_attention_heads': 'n_head', 'num_hidden_layers': 'n_layer', } def __init__( self : Tuple , UpperCAmelCase : str=50400 , UpperCAmelCase : str=2048 , UpperCAmelCase : Optional[Any]=2048 , UpperCAmelCase : Tuple=4096 , UpperCAmelCase : str=28 , UpperCAmelCase : int=16 , UpperCAmelCase : int=64 , UpperCAmelCase : Union[str, Any]=None , UpperCAmelCase : Optional[Any]="gelu_new" , UpperCAmelCase : str=0.0 , UpperCAmelCase : int=0.0 , UpperCAmelCase : Dict=0.0 , UpperCAmelCase : str=1E-5 , UpperCAmelCase : Optional[int]=0.02 , UpperCAmelCase : int=True , UpperCAmelCase : Union[str, Any]=50256 , UpperCAmelCase : Dict=50256 , UpperCAmelCase : Dict=False , **UpperCAmelCase : Any , ): A_ = vocab_size A_ = n_ctx A_ = n_positions A_ = n_embd A_ = n_layer A_ = n_head A_ = n_inner A_ = rotary_dim A_ = activation_function A_ = resid_pdrop A_ = embd_pdrop A_ = attn_pdrop A_ = layer_norm_epsilon A_ = initializer_range A_ = use_cache A_ = bos_token_id A_ = eos_token_id super().__init__( bos_token_id=UpperCAmelCase , eos_token_id=UpperCAmelCase , tie_word_embeddings=UpperCAmelCase , **UpperCAmelCase ) class _a ( snake_case_ ): """simple docstring""" def __init__( self : Optional[int] , UpperCAmelCase : PretrainedConfig , UpperCAmelCase : str = "default" , UpperCAmelCase : List[PatchingSpec] = None , UpperCAmelCase : bool = False , ): super().__init__(UpperCAmelCase , task=UpperCAmelCase , patching_specs=UpperCAmelCase , use_past=UpperCAmelCase ) if not getattr(self._config , "pad_token_id" , UpperCAmelCase ): # TODO: how to do that better? A_ = 0 @property def __A ( self : Dict ): A_ = OrderedDict({"input_ids": {0: "batch", 1: "sequence"}} ) if self.use_past: self.fill_with_past_key_values_(UpperCAmelCase , direction="inputs" ) A_ = {0: "batch", 1: "past_sequence + sequence"} else: A_ = {0: "batch", 1: "sequence"} return common_inputs @property def __A ( self : Union[str, Any] ): return self._config.n_layer @property def __A ( self : Optional[Any] ): return self._config.n_head def __A ( self : Optional[int] , UpperCAmelCase : PreTrainedTokenizer , UpperCAmelCase : int = -1 , UpperCAmelCase : int = -1 , UpperCAmelCase : bool = False , UpperCAmelCase : Optional[TensorType] = None , ): A_ = super(UpperCAmelCase , self ).generate_dummy_inputs( UpperCAmelCase , batch_size=UpperCAmelCase , seq_length=UpperCAmelCase , is_pair=UpperCAmelCase , framework=UpperCAmelCase ) # We need to order the input in the way they appears in the forward() A_ = OrderedDict({"input_ids": common_inputs["input_ids"]} ) # Need to add the past_keys if self.use_past: if not is_torch_available(): raise ValueError("Cannot generate dummy past_keys inputs without PyTorch installed." ) else: import torch A_ , A_ = common_inputs["input_ids"].shape # Not using the same length for past_key_values A_ = seqlen + 2 A_ = ( batch, self.num_attention_heads, past_key_values_length, self._config.hidden_size // self.num_attention_heads, ) A_ = [ (torch.zeros(UpperCAmelCase ), torch.zeros(UpperCAmelCase )) for _ in range(self.num_layers ) ] A_ = common_inputs["attention_mask"] if self.use_past: A_ = ordered_inputs["attention_mask"].dtype A_ = torch.cat( [ordered_inputs["attention_mask"], torch.ones(UpperCAmelCase , UpperCAmelCase , dtype=UpperCAmelCase )] , dim=1 ) return ordered_inputs @property def __A ( self : str ): return 13
312
import warnings from typing import List import numpy as np from ...processing_utils import ProcessorMixin from ...tokenization_utils_base import BatchEncoding from ...utils import is_flax_available, is_tf_available, is_torch_available class _a ( snake_case_ ): """simple docstring""" _lowerCamelCase : Union[str, Any] = ['image_processor', 'tokenizer'] _lowerCamelCase : Tuple = 'OwlViTImageProcessor' _lowerCamelCase : List[Any] = ('CLIPTokenizer', 'CLIPTokenizerFast') def __init__( self : Optional[Any] , UpperCAmelCase : int=None , UpperCAmelCase : Union[str, Any]=None , **UpperCAmelCase : Any ): A_ = None if "feature_extractor" in kwargs: warnings.warn( "The `feature_extractor` argument is deprecated and will be removed in v5, use `image_processor`" " instead." , UpperCAmelCase , ) A_ = kwargs.pop("feature_extractor" ) A_ = image_processor if image_processor is not None else feature_extractor if image_processor is None: raise ValueError("You need to specify an `image_processor`." ) if tokenizer is None: raise ValueError("You need to specify a `tokenizer`." ) super().__init__(UpperCAmelCase , UpperCAmelCase ) def __call__( self : Optional[int] , UpperCAmelCase : List[str]=None , UpperCAmelCase : List[Any]=None , UpperCAmelCase : Optional[int]=None , UpperCAmelCase : Dict="max_length" , UpperCAmelCase : Optional[Any]="np" , **UpperCAmelCase : Optional[int] ): if text is None and query_images is None and images is None: raise ValueError( "You have to specify at least one text or query image or image. All three cannot be none." ) if text is not None: if isinstance(UpperCAmelCase , UpperCAmelCase ) or (isinstance(UpperCAmelCase , UpperCAmelCase ) and not isinstance(text[0] , UpperCAmelCase )): A_ = [self.tokenizer(UpperCAmelCase , padding=UpperCAmelCase , return_tensors=UpperCAmelCase , **UpperCAmelCase )] elif isinstance(UpperCAmelCase , UpperCAmelCase ) and isinstance(text[0] , UpperCAmelCase ): A_ = [] # Maximum number of queries across batch A_ = max([len(UpperCAmelCase ) for t in text] ) # Pad all batch samples to max number of text queries for t in text: if len(UpperCAmelCase ) != max_num_queries: A_ = t + [" "] * (max_num_queries - len(UpperCAmelCase )) A_ = self.tokenizer(UpperCAmelCase , padding=UpperCAmelCase , return_tensors=UpperCAmelCase , **UpperCAmelCase ) encodings.append(UpperCAmelCase ) else: raise TypeError("Input text should be a string, a list of strings or a nested list of strings" ) if return_tensors == "np": A_ = np.concatenate([encoding["input_ids"] for encoding in encodings] , axis=0 ) A_ = np.concatenate([encoding["attention_mask"] for encoding in encodings] , axis=0 ) elif return_tensors == "jax" and is_flax_available(): import jax.numpy as jnp A_ = jnp.concatenate([encoding["input_ids"] for encoding in encodings] , axis=0 ) A_ = jnp.concatenate([encoding["attention_mask"] for encoding in encodings] , axis=0 ) elif return_tensors == "pt" and is_torch_available(): import torch A_ = torch.cat([encoding["input_ids"] for encoding in encodings] , dim=0 ) A_ = torch.cat([encoding["attention_mask"] for encoding in encodings] , dim=0 ) elif return_tensors == "tf" and is_tf_available(): import tensorflow as tf A_ = tf.stack([encoding["input_ids"] for encoding in encodings] , axis=0 ) A_ = tf.stack([encoding["attention_mask"] for encoding in encodings] , axis=0 ) else: raise ValueError("Target return tensor type could not be returned" ) A_ = BatchEncoding() A_ = input_ids A_ = attention_mask if query_images is not None: A_ = BatchEncoding() A_ = self.image_processor( UpperCAmelCase , return_tensors=UpperCAmelCase , **UpperCAmelCase ).pixel_values A_ = query_pixel_values if images is not None: A_ = self.image_processor(UpperCAmelCase , return_tensors=UpperCAmelCase , **UpperCAmelCase ) if text is not None and images is not None: A_ = image_features.pixel_values return encoding elif query_images is not None and images is not None: A_ = image_features.pixel_values return encoding elif text is not None or query_images is not None: return encoding else: return BatchEncoding(data=dict(**UpperCAmelCase ) , tensor_type=UpperCAmelCase ) def __A ( self : Optional[Any] , *UpperCAmelCase : Union[str, Any] , **UpperCAmelCase : List[Any] ): return self.image_processor.post_process(*UpperCAmelCase , **UpperCAmelCase ) def __A ( self : str , *UpperCAmelCase : str , **UpperCAmelCase : Union[str, Any] ): return self.image_processor.post_process_object_detection(*UpperCAmelCase , **UpperCAmelCase ) def __A ( self : List[Any] , *UpperCAmelCase : int , **UpperCAmelCase : int ): return self.image_processor.post_process_image_guided_detection(*UpperCAmelCase , **UpperCAmelCase ) def __A ( self : List[Any] , *UpperCAmelCase : Optional[int] , **UpperCAmelCase : Any ): return self.tokenizer.batch_decode(*UpperCAmelCase , **UpperCAmelCase ) def __A ( self : Tuple , *UpperCAmelCase : Dict , **UpperCAmelCase : str ): return self.tokenizer.decode(*UpperCAmelCase , **UpperCAmelCase ) @property def __A ( self : Union[str, Any] ): warnings.warn( "`feature_extractor_class` is deprecated and will be removed in v5. Use `image_processor_class` instead." , UpperCAmelCase , ) return self.image_processor_class @property def __A ( self : Optional[Any] ): warnings.warn( "`feature_extractor` is deprecated and will be removed in v5. Use `image_processor` instead." , UpperCAmelCase , ) return self.image_processor
312
1
import pytest from datasets.parallel import ParallelBackendConfig, parallel_backend from datasets.utils.py_utils import map_nested from .utils import require_dill_gt_0_3_2, require_joblibspark, require_not_windows def __snake_case ( __UpperCamelCase : Optional[Any] ): # picklable for multiprocessing """simple docstring""" return i + 1 @require_dill_gt_0_3_2 @require_joblibspark @require_not_windows def __snake_case ( ): """simple docstring""" with parallel_backend("spark" ): assert ParallelBackendConfig.backend_name == "spark" A_ = [1, 2, 3] with pytest.raises(__UpperCamelCase ): with parallel_backend("unsupported backend" ): map_nested(__UpperCamelCase ,__UpperCamelCase ,num_proc=2 ) with pytest.raises(__UpperCamelCase ): with parallel_backend("unsupported backend" ): map_nested(__UpperCamelCase ,__UpperCamelCase ,num_proc=-1 ) @require_dill_gt_0_3_2 @require_joblibspark @require_not_windows @pytest.mark.parametrize("num_proc" ,[2, -1] ) def __snake_case ( __UpperCamelCase : Tuple ): """simple docstring""" A_ = [1, 2] A_ = {"a": 1, "b": 2} A_ = {"a": [1, 2], "b": [3, 4]} A_ = {"a": {"1": 1}, "b": 2} A_ = {"a": 1, "b": 2, "c": 3, "d": 4} A_ = [2, 3] A_ = {"a": 2, "b": 3} A_ = {"a": [2, 3], "b": [4, 5]} A_ = {"a": {"1": 2}, "b": 3} A_ = {"a": 2, "b": 3, "c": 4, "d": 5} with parallel_backend("spark" ): assert map_nested(__UpperCamelCase ,__UpperCamelCase ,num_proc=__UpperCamelCase ) == expected_map_nested_sa assert map_nested(__UpperCamelCase ,__UpperCamelCase ,num_proc=__UpperCamelCase ) == expected_map_nested_sa assert map_nested(__UpperCamelCase ,__UpperCamelCase ,num_proc=__UpperCamelCase ) == expected_map_nested_sa assert map_nested(__UpperCamelCase ,__UpperCamelCase ,num_proc=__UpperCamelCase ) == expected_map_nested_sa assert map_nested(__UpperCamelCase ,__UpperCamelCase ,num_proc=__UpperCamelCase ) == expected_map_nested_sa
312
from typing import Optional, Union import torch from torch import nn from ...configuration_utils import ConfigMixin, register_to_config from ...models.modeling_utils import ModelMixin class _a ( snake_case_ , snake_case_ ): """simple docstring""" @register_to_config def __init__( self : Dict , UpperCAmelCase : int = 768 , ): super().__init__() A_ = nn.Parameter(torch.zeros(1 , UpperCAmelCase ) ) A_ = nn.Parameter(torch.ones(1 , UpperCAmelCase ) ) def __A ( self : str , UpperCAmelCase : Optional[Union[str, torch.device]] = None , UpperCAmelCase : Optional[torch.dtype] = None , ): A_ = nn.Parameter(self.mean.to(UpperCAmelCase ).to(UpperCAmelCase ) ) A_ = nn.Parameter(self.std.to(UpperCAmelCase ).to(UpperCAmelCase ) ) return self def __A ( self : Dict , UpperCAmelCase : List[Any] ): A_ = (embeds - self.mean) * 1.0 / self.std return embeds def __A ( self : int , UpperCAmelCase : int ): A_ = (embeds * self.std) + self.mean return embeds
312
1
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 _a ( snake_case_ ): """simple docstring""" def __init__( self : int , UpperCAmelCase : int , UpperCAmelCase : Any=13 , UpperCAmelCase : Optional[int]=7 , UpperCAmelCase : int=True , UpperCAmelCase : Union[str, Any]=True , UpperCAmelCase : Optional[int]=False , UpperCAmelCase : Any=True , UpperCAmelCase : List[str]=99 , UpperCAmelCase : Optional[int]=32 , UpperCAmelCase : Tuple=5 , UpperCAmelCase : int=4 , UpperCAmelCase : Any=64 , UpperCAmelCase : List[str]="gelu" , UpperCAmelCase : int=0.1 , UpperCAmelCase : Any=0.1 , UpperCAmelCase : Optional[Any]=512 , UpperCAmelCase : Union[str, Any]=16 , UpperCAmelCase : Any=2 , UpperCAmelCase : int=0.02 , UpperCAmelCase : str=3 , UpperCAmelCase : List[str]=4 , UpperCAmelCase : str=None , UpperCAmelCase : Dict=2 , UpperCAmelCase : Any=2 , UpperCAmelCase : List[str]=2 , UpperCAmelCase : int=2 , UpperCAmelCase : str=4 , UpperCAmelCase : Tuple=1 , ): A_ = parent A_ = batch_size A_ = seq_length A_ = is_training A_ = use_input_mask A_ = use_token_type_ids A_ = use_labels A_ = vocab_size A_ = hidden_size A_ = num_hidden_layers A_ = num_attention_heads A_ = intermediate_size A_ = hidden_act A_ = hidden_dropout_prob A_ = attention_probs_dropout_prob A_ = max_position_embeddings A_ = type_vocab_size A_ = type_sequence_label_size A_ = initializer_range A_ = num_labels A_ = num_choices A_ = scope A_ = q_groups A_ = k_groups A_ = v_groups A_ = post_attention_groups A_ = intermediate_groups A_ = output_groups def __A ( self : Tuple ): A_ = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size ) A_ = None if self.use_input_mask: A_ = random_attention_mask([self.batch_size, self.seq_length] ) A_ = None A_ = None A_ = None if self.use_labels: A_ = ids_tensor([self.batch_size] , self.type_sequence_label_size ) A_ = ids_tensor([self.batch_size, self.seq_length] , self.num_labels ) A_ = ids_tensor([self.batch_size] , self.num_choices ) A_ = self.get_config() return config, input_ids, input_mask, sequence_labels, token_labels, choice_labels def __A ( self : 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 __A ( self : Any , UpperCAmelCase : Tuple , UpperCAmelCase : List[str] , UpperCAmelCase : Tuple , UpperCAmelCase : Tuple , UpperCAmelCase : Optional[int] , UpperCAmelCase : Optional[Any] ): A_ = SqueezeBertModel(config=UpperCAmelCase ) model.to(UpperCAmelCase ) model.eval() A_ = model(UpperCAmelCase , UpperCAmelCase ) A_ = model(UpperCAmelCase ) self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size) ) def __A ( self : Union[str, Any] , UpperCAmelCase : Union[str, Any] , UpperCAmelCase : int , UpperCAmelCase : Union[str, Any] , UpperCAmelCase : Any , UpperCAmelCase : List[str] , UpperCAmelCase : Tuple ): A_ = SqueezeBertForMaskedLM(config=UpperCAmelCase ) model.to(UpperCAmelCase ) model.eval() A_ = model(UpperCAmelCase , attention_mask=UpperCAmelCase , labels=UpperCAmelCase ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.vocab_size) ) def __A ( self : Dict , UpperCAmelCase : Optional[Any] , UpperCAmelCase : Union[str, Any] , UpperCAmelCase : List[str] , UpperCAmelCase : Dict , UpperCAmelCase : Tuple , UpperCAmelCase : Any ): A_ = SqueezeBertForQuestionAnswering(config=UpperCAmelCase ) model.to(UpperCAmelCase ) model.eval() A_ = 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 __A ( self : Dict , UpperCAmelCase : List[Any] , UpperCAmelCase : Union[str, Any] , UpperCAmelCase : Any , UpperCAmelCase : Optional[Any] , UpperCAmelCase : List[str] , UpperCAmelCase : Any ): A_ = self.num_labels A_ = SqueezeBertForSequenceClassification(UpperCAmelCase ) model.to(UpperCAmelCase ) model.eval() A_ = model(UpperCAmelCase , attention_mask=UpperCAmelCase , labels=UpperCAmelCase ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_labels) ) def __A ( self : int , UpperCAmelCase : Any , UpperCAmelCase : Dict , UpperCAmelCase : Any , UpperCAmelCase : List[Any] , UpperCAmelCase : str , UpperCAmelCase : str ): A_ = self.num_labels A_ = SqueezeBertForTokenClassification(config=UpperCAmelCase ) model.to(UpperCAmelCase ) model.eval() A_ = model(UpperCAmelCase , attention_mask=UpperCAmelCase , labels=UpperCAmelCase ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.num_labels) ) def __A ( self : str , UpperCAmelCase : Optional[Any] , UpperCAmelCase : Optional[Any] , UpperCAmelCase : Union[str, Any] , UpperCAmelCase : Dict , UpperCAmelCase : Optional[int] , UpperCAmelCase : Optional[int] ): A_ = self.num_choices A_ = SqueezeBertForMultipleChoice(config=UpperCAmelCase ) model.to(UpperCAmelCase ) model.eval() A_ = input_ids.unsqueeze(1 ).expand(-1 , self.num_choices , -1 ).contiguous() A_ = input_mask.unsqueeze(1 ).expand(-1 , self.num_choices , -1 ).contiguous() A_ = model( UpperCAmelCase , attention_mask=UpperCAmelCase , labels=UpperCAmelCase , ) self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_choices) ) def __A ( self : Union[str, Any] ): A_ = self.prepare_config_and_inputs() ((A_) , (A_) , (A_) , (A_) , (A_) , (A_)) = config_and_inputs A_ = {"input_ids": input_ids, "attention_mask": input_mask} return config, inputs_dict @require_torch class _a ( snake_case_ , snake_case_ , unittest.TestCase ): """simple docstring""" _lowerCamelCase : Any = ( ( SqueezeBertModel, SqueezeBertForMaskedLM, SqueezeBertForMultipleChoice, SqueezeBertForQuestionAnswering, SqueezeBertForSequenceClassification, SqueezeBertForTokenClassification, ) if is_torch_available() else None ) _lowerCamelCase : List[Any] = ( { 'feature-extraction': SqueezeBertModel, 'fill-mask': SqueezeBertForMaskedLM, 'question-answering': SqueezeBertForQuestionAnswering, 'text-classification': SqueezeBertForSequenceClassification, 'token-classification': SqueezeBertForTokenClassification, 'zero-shot': SqueezeBertForSequenceClassification, } if is_torch_available() else {} ) _lowerCamelCase : Optional[int] = False _lowerCamelCase : Optional[int] = True _lowerCamelCase : List[Any] = False def __A ( self : int ): A_ = SqueezeBertModelTester(self ) A_ = ConfigTester(self , config_class=UpperCAmelCase , dim=37 ) def __A ( self : Union[str, Any] ): self.config_tester.run_common_tests() def __A ( self : List[str] ): A_ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_squeezebert_model(*UpperCAmelCase ) def __A ( self : int ): A_ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_squeezebert_for_masked_lm(*UpperCAmelCase ) def __A ( self : Optional[Any] ): A_ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_squeezebert_for_question_answering(*UpperCAmelCase ) def __A ( self : Optional[int] ): A_ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_squeezebert_for_sequence_classification(*UpperCAmelCase ) def __A ( self : Optional[int] ): A_ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_squeezebert_for_token_classification(*UpperCAmelCase ) def __A ( self : List[str] ): A_ = self.model_tester.prepare_config_and_inputs() self.model_tester.create_and_check_squeezebert_for_multiple_choice(*UpperCAmelCase ) @slow def __A ( self : int ): for model_name in SQUEEZEBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: A_ = SqueezeBertModel.from_pretrained(UpperCAmelCase ) self.assertIsNotNone(UpperCAmelCase ) @require_sentencepiece @require_tokenizers @require_torch class _a ( unittest.TestCase ): """simple docstring""" @slow def __A ( self : Dict ): A_ = SqueezeBertForSequenceClassification.from_pretrained("squeezebert/squeezebert-mnli" ) A_ = torch.tensor([[1, 29414, 232, 328, 740, 1140, 12695, 69, 13, 1588, 2]] ) A_ = model(UpperCAmelCase )[0] A_ = torch.Size((1, 3) ) self.assertEqual(output.shape , UpperCAmelCase ) A_ = torch.tensor([[0.6_401, -0.0_349, -0.6_041]] ) self.assertTrue(torch.allclose(UpperCAmelCase , UpperCAmelCase , atol=1E-4 ) )
312
from __future__ import annotations import numpy as np from numpy import floataa from numpy.typing import NDArray def __snake_case ( __UpperCamelCase : NDArray[floataa] ,__UpperCamelCase : NDArray[floataa] ,__UpperCamelCase : list[int] ,__UpperCamelCase : int ,): """simple docstring""" A_ , A_ = coefficient_matrix.shape A_ , A_ = constant_matrix.shape if rowsa != colsa: A_ = f'''Coefficient matrix dimensions must be nxn but received {rowsa}x{colsa}''' raise ValueError(__UpperCamelCase ) if colsa != 1: A_ = f'''Constant matrix must be nx1 but received {rowsa}x{colsa}''' raise ValueError(__UpperCamelCase ) if rowsa != rowsa: A_ = ( "Coefficient and constant matrices dimensions must be nxn and nx1 but " f'''received {rowsa}x{colsa} and {rowsa}x{colsa}''' ) raise ValueError(__UpperCamelCase ) if len(__UpperCamelCase ) != rowsa: A_ = ( "Number of initial values must be equal to number of rows in coefficient " f'''matrix but received {len(__UpperCamelCase )} and {rowsa}''' ) raise ValueError(__UpperCamelCase ) if iterations <= 0: raise ValueError("Iterations must be at least 1" ) A_ = np.concatenate( (coefficient_matrix, constant_matrix) ,axis=1 ) A_ , A_ = table.shape strictly_diagonally_dominant(__UpperCamelCase ) # Iterates the whole matrix for given number of times for _ in range(__UpperCamelCase ): A_ = [] for row in range(__UpperCamelCase ): A_ = 0 for col in range(__UpperCamelCase ): if col == row: A_ = table[row][col] elif col == cols - 1: A_ = table[row][col] else: temp += (-1) * table[row][col] * init_val[col] A_ = (temp + val) / denom new_val.append(__UpperCamelCase ) A_ = new_val return [float(__UpperCamelCase ) for i in new_val] def __snake_case ( __UpperCamelCase : NDArray[floataa] ): """simple docstring""" A_ , A_ = table.shape A_ = True for i in range(0 ,__UpperCamelCase ): A_ = 0 for j in range(0 ,cols - 1 ): if i == j: continue else: total += table[i][j] if table[i][i] <= total: raise ValueError("Coefficient matrix is not strictly diagonally dominant" ) return is_diagonally_dominant # Test Cases if __name__ == "__main__": import doctest doctest.testmod()
312
1
from typing import TYPE_CHECKING from ...utils import _LazyModule __a :Any = {'tokenization_wav2vec2_phoneme': ['Wav2Vec2PhonemeCTCTokenizer']} if TYPE_CHECKING: from .tokenization_wavaveca_phoneme import WavaVecaPhonemeCTCTokenizer else: import sys __a :Union[str, Any] = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
312
from unittest import TestCase from datasets import Dataset from minhash_deduplication import deduplicate_dataset, make_duplicate_clusters def __snake_case ( ): """simple docstring""" A_ = { "repo_name": ["test_repo1", "test_repo2", "test_repo3"], "path": ["test_1.py", "test_2.py", "unit_test.py"], "content": ["a " * 20, "a " * 30, "b " * 7], } A_ = Dataset.from_dict(__UpperCamelCase ) return dataset class _a ( snake_case_ ): """simple docstring""" def __A ( self : Union[str, Any] ): A_ = get_dataset() A_ = make_duplicate_clusters(UpperCAmelCase , 0.85 ) self.assertEqual(len(duplicate_clusters[0] ) , 2 ) def __A ( self : List[Any] ): A_ = get_dataset() A_ , A_ = deduplicate_dataset(UpperCAmelCase ) self.assertEqual(len(UpperCAmelCase ) , 2 ) print(UpperCAmelCase ) self.assertEqual(duplicate_clusters[0][0]["copies"] , 2 ) self.assertEqual(duplicate_clusters[0][0]["is_extreme"] , UpperCAmelCase )
312
1
import os import re import shutil import sys import tempfile import unittest import black __a :List[Any] = os.path.abspath(os.path.dirname(os.path.dirname(os.path.dirname(__file__)))) sys.path.append(os.path.join(git_repo_path, 'utils')) import check_copies # noqa: E402 # This is the reference code that will be used in the tests. # If BertLMPredictionHead is changed in modeling_bert.py, this code needs to be manually updated. __a :Optional[Any] = ' def __init__(self, config):\n super().__init__()\n self.transform = BertPredictionHeadTransform(config)\n\n # The output weights are the same as the input embeddings, but there is\n # an output-only bias for each token.\n self.decoder = nn.Linear(config.hidden_size, config.vocab_size, bias=False)\n\n self.bias = nn.Parameter(torch.zeros(config.vocab_size))\n\n # Need a link between the two variables so that the bias is correctly resized with `resize_token_embeddings`\n self.decoder.bias = self.bias\n\n def forward(self, hidden_states):\n hidden_states = self.transform(hidden_states)\n hidden_states = self.decoder(hidden_states)\n return hidden_states\n' class _a ( unittest.TestCase ): """simple docstring""" def __A ( self : Dict ): A_ = tempfile.mkdtemp() os.makedirs(os.path.join(self.transformer_dir , "models/bert/" ) ) A_ = self.transformer_dir shutil.copy( os.path.join(UpperCAmelCase , "src/transformers/models/bert/modeling_bert.py" ) , os.path.join(self.transformer_dir , "models/bert/modeling_bert.py" ) , ) def __A ( self : int ): A_ = "src/transformers" shutil.rmtree(self.transformer_dir ) def __A ( self : Any , UpperCAmelCase : Optional[Any] , UpperCAmelCase : int , UpperCAmelCase : Tuple , UpperCAmelCase : str=None ): A_ = comment + f'''\nclass {class_name}(nn.Module):\n''' + class_code if overwrite_result is not None: A_ = comment + f'''\nclass {class_name}(nn.Module):\n''' + overwrite_result A_ = black.Mode(target_versions={black.TargetVersion.PYaa} , line_length=119 ) A_ = black.format_str(UpperCAmelCase , mode=UpperCAmelCase ) A_ = os.path.join(self.transformer_dir , "new_code.py" ) with open(UpperCAmelCase , "w" , newline="\n" ) as f: f.write(UpperCAmelCase ) if overwrite_result is None: self.assertTrue(len(check_copies.is_copy_consistent(UpperCAmelCase ) ) == 0 ) else: check_copies.is_copy_consistent(f.name , overwrite=UpperCAmelCase ) with open(UpperCAmelCase , "r" ) as f: self.assertTrue(f.read() , UpperCAmelCase ) def __A ( self : Union[str, Any] ): A_ = check_copies.find_code_in_transformers("models.bert.modeling_bert.BertLMPredictionHead" ) self.assertEqual(UpperCAmelCase , UpperCAmelCase ) def __A ( self : int ): # Base copy consistency self.check_copy_consistency( "# Copied from transformers.models.bert.modeling_bert.BertLMPredictionHead" , "BertLMPredictionHead" , REFERENCE_CODE + "\n" , ) # With no empty line at the end self.check_copy_consistency( "# Copied from transformers.models.bert.modeling_bert.BertLMPredictionHead" , "BertLMPredictionHead" , UpperCAmelCase , ) # Copy consistency with rename self.check_copy_consistency( "# Copied from transformers.models.bert.modeling_bert.BertLMPredictionHead with Bert->TestModel" , "TestModelLMPredictionHead" , re.sub("Bert" , "TestModel" , UpperCAmelCase ) , ) # Copy consistency with a really long name A_ = "TestModelWithAReallyLongNameBecauseSomePeopleLikeThatForSomeReason" self.check_copy_consistency( f'''# Copied from transformers.models.bert.modeling_bert.BertLMPredictionHead with Bert->{long_class_name}''' , f'''{long_class_name}LMPredictionHead''' , re.sub("Bert" , UpperCAmelCase , UpperCAmelCase ) , ) # Copy consistency with overwrite self.check_copy_consistency( "# Copied from transformers.models.bert.modeling_bert.BertLMPredictionHead with Bert->TestModel" , "TestModelLMPredictionHead" , UpperCAmelCase , overwrite_result=re.sub("Bert" , "TestModel" , UpperCAmelCase ) , ) def __A ( self : int ): A_ = check_copies.LOCALIZED_READMES["README_zh-hans.md"] A_ = ( "1. **[ALBERT](https://huggingface.co/transformers/model_doc/albert.html)** (from Google Research and the" " Toyota Technological Institute at Chicago) released with the paper [ALBERT: A Lite BERT for" " Self-supervised Learning of Language Representations](https://arxiv.org/abs/1909.11942), by Zhenzhong" " Lan, Mingda Chen, Sebastian Goodman, Kevin Gimpel, Piyush Sharma, Radu Soricut.\n1." " **[DistilBERT](https://huggingface.co/transformers/model_doc/distilbert.html)** (from HuggingFace)," " released together with the paper [DistilBERT, a distilled version of BERT: smaller, faster, cheaper and" " lighter](https://arxiv.org/abs/1910.01108) by Victor Sanh, Lysandre Debut and Thomas Wolf. The same" " method has been applied to compress GPT2 into" " [DistilGPT2](https://github.com/huggingface/transformers/tree/main/examples/distillation), RoBERTa into" " [DistilRoBERTa](https://github.com/huggingface/transformers/tree/main/examples/distillation)," " Multilingual BERT into" " [DistilmBERT](https://github.com/huggingface/transformers/tree/main/examples/distillation) and a German" " version of DistilBERT.\n1. **[ELECTRA](https://huggingface.co/transformers/model_doc/electra.html)**" " (from Google Research/Stanford University) released with the paper [ELECTRA: Pre-training text encoders" " as discriminators rather than generators](https://arxiv.org/abs/2003.10555) by Kevin Clark, Minh-Thang" " Luong, Quoc V. Le, Christopher D. Manning." ) A_ = ( "1. **[ALBERT](https://huggingface.co/transformers/model_doc/albert.html)** (来自 Google Research and the" " Toyota Technological Institute at Chicago) 伴随论文 [ALBERT: A Lite BERT for Self-supervised Learning of" " Language Representations](https://arxiv.org/abs/1909.11942), 由 Zhenzhong Lan, Mingda Chen, Sebastian" " Goodman, Kevin Gimpel, Piyush Sharma, Radu Soricut 发布。\n" ) A_ = ( "1. **[ALBERT](https://huggingface.co/transformers/model_doc/albert.html)** (来自 Google Research and the" " Toyota Technological Institute at Chicago) 伴随论文 [ALBERT: A Lite BERT for Self-supervised Learning of" " Language Representations](https://arxiv.org/abs/1909.11942), 由 Zhenzhong Lan, Mingda Chen, Sebastian" " Goodman, Kevin Gimpel, Piyush Sharma, Radu Soricut 发布。\n1." " **[DistilBERT](https://huggingface.co/transformers/model_doc/distilbert.html)** (来自 HuggingFace) 伴随论文" " [DistilBERT, a distilled version of BERT: smaller, faster, cheaper and" " lighter](https://arxiv.org/abs/1910.01108) 由 Victor Sanh, Lysandre Debut and Thomas Wolf 发布。 The same" " method has been applied to compress GPT2 into" " [DistilGPT2](https://github.com/huggingface/transformers/tree/main/examples/distillation), RoBERTa into" " [DistilRoBERTa](https://github.com/huggingface/transformers/tree/main/examples/distillation)," " Multilingual BERT into" " [DistilmBERT](https://github.com/huggingface/transformers/tree/main/examples/distillation) and a German" " version of DistilBERT.\n1. **[ELECTRA](https://huggingface.co/transformers/model_doc/electra.html)** (来自" " Google Research/Stanford University) 伴随论文 [ELECTRA: Pre-training text encoders as discriminators rather" " than generators](https://arxiv.org/abs/2003.10555) 由 Kevin Clark, Minh-Thang Luong, Quoc V. Le," " Christopher D. Manning 发布。\n" ) A_ , A_ = check_copies.convert_to_localized_md( UpperCAmelCase , UpperCAmelCase , localized_readme["format_model_list"] ) self.assertFalse(UpperCAmelCase ) self.assertEqual(UpperCAmelCase , UpperCAmelCase ) A_ , A_ = check_copies.convert_to_localized_md( UpperCAmelCase , UpperCAmelCase , localized_readme["format_model_list"] ) # Check whether the number of models is equal to README.md after conversion. self.assertTrue(UpperCAmelCase ) A_ = ( "1. **[ALBERT](https://huggingface.co/transformers/model_doc/albert.html)** (from Google Research and the" " Toyota Technological Institute at Chicago) released with the paper [ALBERT: A Lite BERT for" " Self-supervised Learning of Language Representations](https://arxiv.org/abs/1909.11942), by Zhenzhong" " Lan, Mingda Chen, Sebastian Goodman, Kevin Gimpel, Piyush Sharma, Radu Soricut." ) A_ = ( "1. **[ALBERT](https://huggingface.co/transformers/main/model_doc/albert.html)** (来自 Google Research and" " the Toyota Technological Institute at Chicago) 伴随论文 [ALBERT: A Lite BERT for Self-supervised Learning of" " Language Representations](https://arxiv.org/abs/1909.11942), 由 Zhenzhong Lan, Mingda Chen, Sebastian" " Goodman, Kevin Gimpel, Piyush Sharma, Radu Soricut 发布。\n" ) A_ = ( "1. **[ALBERT](https://huggingface.co/transformers/model_doc/albert.html)** (来自 Google Research and the" " Toyota Technological Institute at Chicago) 伴随论文 [ALBERT: A Lite BERT for Self-supervised Learning of" " Language Representations](https://arxiv.org/abs/1909.11942), 由 Zhenzhong Lan, Mingda Chen, Sebastian" " Goodman, Kevin Gimpel, Piyush Sharma, Radu Soricut 发布。\n" ) A_ , A_ = check_copies.convert_to_localized_md( UpperCAmelCase , UpperCAmelCase , localized_readme["format_model_list"] ) # Check if the model link is synchronized. self.assertEqual(UpperCAmelCase , UpperCAmelCase )
312
import os from typing import Dict, List, Tuple, TypeVar, Union __a :Any = TypeVar('T') __a :Union[str, Any] = Union[List[T], Tuple[T, ...]] __a :List[str] = Union[T, List[T], Dict[str, T]] __a :Any = Union[str, bytes, os.PathLike]
312
1
def __snake_case ( __UpperCamelCase : list[list[int]] ,__UpperCamelCase : int ,__UpperCamelCase : int ,__UpperCamelCase : list[int] ): """simple docstring""" if graph[path[curr_ind - 1]][next_ver] == 0: return False # 2. Validate that next vertex is not already in path return not any(vertex == next_ver for vertex in path ) def __snake_case ( __UpperCamelCase : list[list[int]] ,__UpperCamelCase : list[int] ,__UpperCamelCase : int ): """simple docstring""" if curr_ind == len(__UpperCamelCase ): # return whether path exists between current and starting vertices return graph[path[curr_ind - 1]][path[0]] == 1 # Recursive Step for next_ver in range(0 ,len(__UpperCamelCase ) ): if valid_connection(__UpperCamelCase ,__UpperCamelCase ,__UpperCamelCase ,__UpperCamelCase ): # Insert current vertex into path as next transition A_ = next_ver # Validate created path if util_hamilton_cycle(__UpperCamelCase ,__UpperCamelCase ,curr_ind + 1 ): return True # Backtrack A_ = -1 return False def __snake_case ( __UpperCamelCase : list[list[int]] ,__UpperCamelCase : int = 0 ): """simple docstring""" A_ = [-1] * (len(__UpperCamelCase ) + 1) # initialize start and end of path with starting index A_ = A_ = start_index # evaluate and if we find answer return path either return empty array return path if util_hamilton_cycle(__UpperCamelCase ,__UpperCamelCase ,1 ) else []
312
__a :Dict = '0.18.2' from .configuration_utils import ConfigMixin from .utils import ( OptionalDependencyNotAvailable, is_flax_available, is_inflect_available, is_invisible_watermark_available, is_k_diffusion_available, is_k_diffusion_version, is_librosa_available, is_note_seq_available, is_onnx_available, is_scipy_available, is_torch_available, is_torchsde_available, is_transformers_available, is_transformers_version, is_unidecode_available, logging, ) try: if not is_onnx_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from .utils.dummy_onnx_objects import * # noqa F403 else: from .pipelines import OnnxRuntimeModel try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from .utils.dummy_pt_objects import * # noqa F403 else: from .models import ( AutoencoderKL, ControlNetModel, ModelMixin, PriorTransformer, TaFilmDecoder, TransformeraDModel, UNetaDModel, UNetaDConditionModel, UNetaDModel, UNetaDConditionModel, VQModel, ) from .optimization import ( get_constant_schedule, get_constant_schedule_with_warmup, get_cosine_schedule_with_warmup, get_cosine_with_hard_restarts_schedule_with_warmup, get_linear_schedule_with_warmup, get_polynomial_decay_schedule_with_warmup, get_scheduler, ) from .pipelines import ( AudioPipelineOutput, ConsistencyModelPipeline, DanceDiffusionPipeline, DDIMPipeline, DDPMPipeline, DiffusionPipeline, DiTPipeline, ImagePipelineOutput, KarrasVePipeline, LDMPipeline, LDMSuperResolutionPipeline, PNDMPipeline, RePaintPipeline, ScoreSdeVePipeline, ) from .schedulers import ( CMStochasticIterativeScheduler, DDIMInverseScheduler, DDIMParallelScheduler, DDIMScheduler, DDPMParallelScheduler, DDPMScheduler, DEISMultistepScheduler, DPMSolverMultistepInverseScheduler, DPMSolverMultistepScheduler, DPMSolverSinglestepScheduler, EulerAncestralDiscreteScheduler, EulerDiscreteScheduler, HeunDiscreteScheduler, IPNDMScheduler, KarrasVeScheduler, KDPMaAncestralDiscreteScheduler, KDPMaDiscreteScheduler, PNDMScheduler, RePaintScheduler, SchedulerMixin, ScoreSdeVeScheduler, UnCLIPScheduler, UniPCMultistepScheduler, VQDiffusionScheduler, ) from .training_utils import EMAModel try: if not (is_torch_available() and is_scipy_available()): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from .utils.dummy_torch_and_scipy_objects import * # noqa F403 else: from .schedulers import LMSDiscreteScheduler try: if not (is_torch_available() and is_torchsde_available()): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from .utils.dummy_torch_and_torchsde_objects import * # noqa F403 else: from .schedulers import DPMSolverSDEScheduler try: if not (is_torch_available() and is_transformers_available()): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from .utils.dummy_torch_and_transformers_objects import * # noqa F403 else: from .pipelines import ( AltDiffusionImgaImgPipeline, AltDiffusionPipeline, AudioLDMPipeline, CycleDiffusionPipeline, IFImgaImgPipeline, IFImgaImgSuperResolutionPipeline, IFInpaintingPipeline, IFInpaintingSuperResolutionPipeline, IFPipeline, IFSuperResolutionPipeline, ImageTextPipelineOutput, KandinskyImgaImgPipeline, KandinskyInpaintPipeline, KandinskyPipeline, KandinskyPriorPipeline, KandinskyVaaControlnetImgaImgPipeline, KandinskyVaaControlnetPipeline, KandinskyVaaImgaImgPipeline, KandinskyVaaInpaintPipeline, KandinskyVaaPipeline, KandinskyVaaPriorEmbaEmbPipeline, KandinskyVaaPriorPipeline, LDMTextToImagePipeline, PaintByExamplePipeline, SemanticStableDiffusionPipeline, ShapEImgaImgPipeline, ShapEPipeline, StableDiffusionAttendAndExcitePipeline, StableDiffusionControlNetImgaImgPipeline, StableDiffusionControlNetInpaintPipeline, StableDiffusionControlNetPipeline, StableDiffusionDepthaImgPipeline, StableDiffusionDiffEditPipeline, StableDiffusionImageVariationPipeline, StableDiffusionImgaImgPipeline, StableDiffusionInpaintPipeline, StableDiffusionInpaintPipelineLegacy, StableDiffusionInstructPixaPixPipeline, StableDiffusionLatentUpscalePipeline, StableDiffusionLDMaDPipeline, StableDiffusionModelEditingPipeline, StableDiffusionPanoramaPipeline, StableDiffusionParadigmsPipeline, StableDiffusionPipeline, StableDiffusionPipelineSafe, StableDiffusionPixaPixZeroPipeline, StableDiffusionSAGPipeline, StableDiffusionUpscalePipeline, StableUnCLIPImgaImgPipeline, StableUnCLIPPipeline, TextToVideoSDPipeline, TextToVideoZeroPipeline, UnCLIPImageVariationPipeline, UnCLIPPipeline, UniDiffuserModel, UniDiffuserPipeline, UniDiffuserTextDecoder, VersatileDiffusionDualGuidedPipeline, VersatileDiffusionImageVariationPipeline, VersatileDiffusionPipeline, VersatileDiffusionTextToImagePipeline, VideoToVideoSDPipeline, VQDiffusionPipeline, ) try: if not (is_torch_available() and is_transformers_available() and is_invisible_watermark_available()): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from .utils.dummy_torch_and_transformers_and_invisible_watermark_objects import * # noqa F403 else: from .pipelines import StableDiffusionXLImgaImgPipeline, StableDiffusionXLPipeline try: if not (is_torch_available() and is_transformers_available() and is_k_diffusion_available()): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from .utils.dummy_torch_and_transformers_and_k_diffusion_objects import * # noqa F403 else: from .pipelines import StableDiffusionKDiffusionPipeline try: if not (is_torch_available() and is_transformers_available() and is_onnx_available()): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from .utils.dummy_torch_and_transformers_and_onnx_objects import * # noqa F403 else: from .pipelines import ( OnnxStableDiffusionImgaImgPipeline, OnnxStableDiffusionInpaintPipeline, OnnxStableDiffusionInpaintPipelineLegacy, OnnxStableDiffusionPipeline, OnnxStableDiffusionUpscalePipeline, StableDiffusionOnnxPipeline, ) try: if not (is_torch_available() and is_librosa_available()): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from .utils.dummy_torch_and_librosa_objects import * # noqa F403 else: from .pipelines import AudioDiffusionPipeline, Mel try: if not (is_transformers_available() and is_torch_available() and is_note_seq_available()): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from .utils.dummy_transformers_and_torch_and_note_seq_objects import * # noqa F403 else: from .pipelines import SpectrogramDiffusionPipeline try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from .utils.dummy_flax_objects import * # noqa F403 else: from .models.controlnet_flax import FlaxControlNetModel from .models.modeling_flax_utils import FlaxModelMixin from .models.unet_ad_condition_flax import FlaxUNetaDConditionModel from .models.vae_flax import FlaxAutoencoderKL from .pipelines import FlaxDiffusionPipeline from .schedulers import ( FlaxDDIMScheduler, FlaxDDPMScheduler, FlaxDPMSolverMultistepScheduler, FlaxKarrasVeScheduler, FlaxLMSDiscreteScheduler, FlaxPNDMScheduler, FlaxSchedulerMixin, FlaxScoreSdeVeScheduler, ) try: if not (is_flax_available() and is_transformers_available()): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from .utils.dummy_flax_and_transformers_objects import * # noqa F403 else: from .pipelines import ( FlaxStableDiffusionControlNetPipeline, FlaxStableDiffusionImgaImgPipeline, FlaxStableDiffusionInpaintPipeline, FlaxStableDiffusionPipeline, ) try: if not (is_note_seq_available()): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from .utils.dummy_note_seq_objects import * # noqa F403 else: from .pipelines import MidiProcessor
312
1
import argparse import json import math import os import time import traceback import zipfile from collections import Counter import requests def __snake_case ( __UpperCamelCase : List[Any] ,__UpperCamelCase : List[Any]=None ): """simple docstring""" A_ = None if token is not None: A_ = {"Accept": "application/vnd.github+json", "Authorization": f'''Bearer {token}'''} A_ = f'''https://api.github.com/repos/huggingface/transformers/actions/runs/{workflow_run_id}/jobs?per_page=100''' A_ = requests.get(__UpperCamelCase ,headers=__UpperCamelCase ).json() A_ = {} try: job_links.update({job["name"]: job["html_url"] for job in result["jobs"]} ) A_ = math.ceil((result["total_count"] - 100) / 100 ) for i in range(__UpperCamelCase ): A_ = requests.get(url + f'''&page={i + 2}''' ,headers=__UpperCamelCase ).json() job_links.update({job["name"]: job["html_url"] for job in result["jobs"]} ) return job_links except Exception: print(f'''Unknown error, could not fetch links:\n{traceback.format_exc()}''' ) return {} def __snake_case ( __UpperCamelCase : int ,__UpperCamelCase : List[Any]=None ): """simple docstring""" A_ = None if token is not None: A_ = {"Accept": "application/vnd.github+json", "Authorization": f'''Bearer {token}'''} A_ = f'''https://api.github.com/repos/huggingface/transformers/actions/runs/{worflow_run_id}/artifacts?per_page=100''' A_ = requests.get(__UpperCamelCase ,headers=__UpperCamelCase ).json() A_ = {} try: artifacts.update({artifact["name"]: artifact["archive_download_url"] for artifact in result["artifacts"]} ) A_ = math.ceil((result["total_count"] - 100) / 100 ) for i in range(__UpperCamelCase ): A_ = requests.get(url + f'''&page={i + 2}''' ,headers=__UpperCamelCase ).json() artifacts.update({artifact["name"]: artifact["archive_download_url"] for artifact in result["artifacts"]} ) return artifacts except Exception: print(f'''Unknown error, could not fetch links:\n{traceback.format_exc()}''' ) return {} def __snake_case ( __UpperCamelCase : Any ,__UpperCamelCase : List[str] ,__UpperCamelCase : Optional[Any] ,__UpperCamelCase : Union[str, Any] ): """simple docstring""" A_ = None if token is not None: A_ = {"Accept": "application/vnd.github+json", "Authorization": f'''Bearer {token}'''} A_ = requests.get(__UpperCamelCase ,headers=__UpperCamelCase ,allow_redirects=__UpperCamelCase ) A_ = result.headers["Location"] A_ = requests.get(__UpperCamelCase ,allow_redirects=__UpperCamelCase ) A_ = os.path.join(__UpperCamelCase ,f'''{artifact_name}.zip''' ) with open(__UpperCamelCase ,"wb" ) as fp: fp.write(response.content ) def __snake_case ( __UpperCamelCase : List[Any] ,__UpperCamelCase : List[str]=None ): """simple docstring""" A_ = [] A_ = [] A_ = None with zipfile.ZipFile(__UpperCamelCase ) as z: for filename in z.namelist(): if not os.path.isdir(__UpperCamelCase ): # read the file if filename in ["failures_line.txt", "summary_short.txt", "job_name.txt"]: with z.open(__UpperCamelCase ) as f: for line in f: A_ = line.decode("UTF-8" ).strip() if filename == "failures_line.txt": try: # `error_line` is the place where `error` occurs A_ = line[: line.index(": " )] A_ = line[line.index(": " ) + len(": " ) :] errors.append([error_line, error] ) except Exception: # skip un-related lines pass elif filename == "summary_short.txt" and line.startswith("FAILED " ): # `test` is the test method that failed A_ = line[len("FAILED " ) :] failed_tests.append(__UpperCamelCase ) elif filename == "job_name.txt": A_ = line if len(__UpperCamelCase ) != len(__UpperCamelCase ): raise ValueError( f'''`errors` and `failed_tests` should have the same number of elements. Got {len(__UpperCamelCase )} for `errors` ''' f'''and {len(__UpperCamelCase )} for `failed_tests` instead. The test reports in {artifact_zip_path} have some''' " problem." ) A_ = None if job_name and job_links: A_ = job_links.get(__UpperCamelCase ,__UpperCamelCase ) # A list with elements of the form (line of error, error, failed test) A_ = [x + [y] + [job_link] for x, y in zip(__UpperCamelCase ,__UpperCamelCase )] return result def __snake_case ( __UpperCamelCase : Optional[Any] ,__UpperCamelCase : Union[str, Any]=None ): """simple docstring""" A_ = [] A_ = [os.path.join(__UpperCamelCase ,__UpperCamelCase ) for p in os.listdir(__UpperCamelCase ) if p.endswith(".zip" )] for p in paths: errors.extend(get_errors_from_single_artifact(__UpperCamelCase ,job_links=__UpperCamelCase ) ) return errors def __snake_case ( __UpperCamelCase : List[str] ,__UpperCamelCase : int=None ): """simple docstring""" A_ = Counter() counter.update([x[1] for x in logs] ) A_ = counter.most_common() A_ = {} for error, count in counts: if error_filter is None or error not in error_filter: A_ = {"count": count, "failed_tests": [(x[2], x[0]) for x in logs if x[1] == error]} A_ = dict(sorted(r.items() ,key=lambda __UpperCamelCase : item[1]["count"] ,reverse=__UpperCamelCase ) ) return r def __snake_case ( __UpperCamelCase : List[Any] ): """simple docstring""" A_ = test.split("::" )[0] if test.startswith("tests/models/" ): A_ = test.split("/" )[2] else: A_ = None return test def __snake_case ( __UpperCamelCase : Dict ,__UpperCamelCase : Tuple=None ): """simple docstring""" A_ = [(x[0], x[1], get_model(x[2] )) for x in logs] A_ = [x for x in logs if x[2] is not None] A_ = {x[2] for x in logs} A_ = {} for test in tests: A_ = Counter() # count by errors in `test` counter.update([x[1] for x in logs if x[2] == test] ) A_ = counter.most_common() A_ = {error: count for error, count in counts if (error_filter is None or error not in error_filter)} A_ = sum(error_counts.values() ) if n_errors > 0: A_ = {"count": n_errors, "errors": error_counts} A_ = dict(sorted(r.items() ,key=lambda __UpperCamelCase : item[1]["count"] ,reverse=__UpperCamelCase ) ) return r def __snake_case ( __UpperCamelCase : Optional[Any] ): """simple docstring""" A_ = "| no. | error | status |" A_ = "|-:|:-|:-|" A_ = [header, sep] for error in reduced_by_error: A_ = reduced_by_error[error]["count"] A_ = f'''| {count} | {error[:100]} | |''' lines.append(__UpperCamelCase ) return "\n".join(__UpperCamelCase ) def __snake_case ( __UpperCamelCase : Tuple ): """simple docstring""" A_ = "| model | no. of errors | major error | count |" A_ = "|-:|-:|-:|-:|" A_ = [header, sep] for model in reduced_by_model: A_ = reduced_by_model[model]["count"] A_ , A_ = list(reduced_by_model[model]["errors"].items() )[0] A_ = f'''| {model} | {count} | {error[:60]} | {_count} |''' lines.append(__UpperCamelCase ) return "\n".join(__UpperCamelCase ) if __name__ == "__main__": __a :Optional[int] = argparse.ArgumentParser() # Required parameters parser.add_argument('--workflow_run_id', type=str, required=True, help='A GitHub Actions workflow run id.') parser.add_argument( '--output_dir', type=str, required=True, help='Where to store the downloaded artifacts and other result files.', ) parser.add_argument('--token', default=None, type=str, help='A token that has actions:read permission.') __a :Optional[int] = parser.parse_args() os.makedirs(args.output_dir, exist_ok=True) __a :Tuple = get_job_links(args.workflow_run_id, token=args.token) __a :int = {} # To deal with `workflow_call` event, where a job name is the combination of the job names in the caller and callee. # For example, `PyTorch 1.11 / Model tests (models/albert, single-gpu)`. if _job_links: for k, v in _job_links.items(): # This is how GitHub actions combine job names. if " / " in k: __a :int = k.find(' / ') __a :int = k[index + len(' / ') :] __a :List[str] = v with open(os.path.join(args.output_dir, 'job_links.json'), 'w', encoding='UTF-8') as fp: json.dump(job_links, fp, ensure_ascii=False, indent=4) __a :int = get_artifacts_links(args.workflow_run_id, token=args.token) with open(os.path.join(args.output_dir, 'artifacts.json'), 'w', encoding='UTF-8') as fp: json.dump(artifacts, fp, ensure_ascii=False, indent=4) for idx, (name, url) in enumerate(artifacts.items()): download_artifact(name, url, args.output_dir, args.token) # Be gentle to GitHub time.sleep(1) __a :Union[str, Any] = get_all_errors(args.output_dir, job_links=job_links) # `e[1]` is the error __a :Tuple = Counter() counter.update([e[1] for e in errors]) # print the top 30 most common test errors __a :List[str] = counter.most_common(30) for item in most_common: print(item) with open(os.path.join(args.output_dir, 'errors.json'), 'w', encoding='UTF-8') as fp: json.dump(errors, fp, ensure_ascii=False, indent=4) __a :Any = reduce_by_error(errors) __a :Any = reduce_by_model(errors) __a :Tuple = make_github_table(reduced_by_error) __a :Optional[Any] = make_github_table_per_model(reduced_by_model) with open(os.path.join(args.output_dir, 'reduced_by_error.txt'), 'w', encoding='UTF-8') as fp: fp.write(sa) with open(os.path.join(args.output_dir, 'reduced_by_model.txt'), 'w', encoding='UTF-8') as fp: fp.write(sa)
312
def __snake_case ( __UpperCamelCase : int = 1000 ): """simple docstring""" return sum(e for e in range(3 ,__UpperCamelCase ) if e % 3 == 0 or e % 5 == 0 ) if __name__ == "__main__": print(F"{solution() = }")
312
1
import copy import re class _a : """simple docstring""" _lowerCamelCase : Optional[int] = 'hp' _lowerCamelCase : str = {} _lowerCamelCase : List[Any] = None @classmethod def __A ( cls : Optional[int] , UpperCAmelCase : Dict , UpperCAmelCase : Dict ): A_ = prefix A_ = defaults cls.build_naming_info() @staticmethod def __A ( UpperCAmelCase : Any , UpperCAmelCase : Any ): if len(UpperCAmelCase ) == 0: return "" A_ = None if any(char.isdigit() for char in word ): raise Exception(f'''Parameters should not contain numbers: \'{word}\' contains a number''' ) if word in info["short_word"]: return info["short_word"][word] for prefix_len in range(1 , len(UpperCAmelCase ) + 1 ): A_ = word[:prefix_len] if prefix in info["reverse_short_word"]: continue else: A_ = prefix break if short_word is None: # Paranoid fallback def int_to_alphabetic(UpperCAmelCase : Tuple ): A_ = "" while integer != 0: A_ = chr(ord("A" ) + integer % 10 ) + s integer //= 10 return s A_ = 0 while True: A_ = word + "#" + int_to_alphabetic(UpperCAmelCase ) if sword in info["reverse_short_word"]: continue else: A_ = sword break A_ = short_word A_ = word return short_word @staticmethod def __A ( UpperCAmelCase : int , UpperCAmelCase : Optional[int] ): A_ = param_name.split("_" ) A_ = [TrialShortNamer.shortname_for_word(UpperCAmelCase , UpperCAmelCase ) for word in words] # We try to create a separatorless short name, but if there is a collision we have to fallback # to a separated short name A_ = ["", "_"] for separator in separators: A_ = separator.join(UpperCAmelCase ) if shortname not in info["reverse_short_param"]: A_ = shortname A_ = param_name return shortname return param_name @staticmethod def __A ( UpperCAmelCase : str , UpperCAmelCase : Any ): A_ = TrialShortNamer.shortname_for_key(UpperCAmelCase , UpperCAmelCase ) A_ = short_name A_ = param_name @classmethod def __A ( cls : int ): if cls.NAMING_INFO is not None: return A_ = { "short_word": {}, "reverse_short_word": {}, "short_param": {}, "reverse_short_param": {}, } A_ = list(cls.DEFAULTS.keys() ) for k in field_keys: cls.add_new_param_name(UpperCAmelCase , UpperCAmelCase ) A_ = info @classmethod def __A ( cls : List[Any] , UpperCAmelCase : Optional[int] ): cls.build_naming_info() assert cls.PREFIX is not None A_ = [copy.copy(cls.PREFIX )] for k, v in params.items(): if k not in cls.DEFAULTS: raise Exception(f'''You should provide a default value for the param name {k} with value {v}''' ) if v == cls.DEFAULTS[k]: # The default value is not added to the name continue A_ = cls.NAMING_INFO["short_param"][k] if isinstance(UpperCAmelCase , UpperCAmelCase ): A_ = 1 if v else 0 A_ = "" if isinstance(UpperCAmelCase , (int, float) ) else "-" A_ = f'''{key}{sep}{v}''' name.append(UpperCAmelCase ) return "_".join(UpperCAmelCase ) @classmethod def __A ( cls : Tuple , UpperCAmelCase : Optional[Any] ): A_ = repr[len(cls.PREFIX ) + 1 :] if repr == "": A_ = [] else: A_ = repr.split("_" ) A_ = {} for value in values: if "-" in value: A_ , A_ = value.split("-" ) else: A_ = re.sub("[0-9.]" , "" , UpperCAmelCase ) A_ = float(re.sub("[^0-9.]" , "" , UpperCAmelCase ) ) A_ = cls.NAMING_INFO["reverse_short_param"][p_k] A_ = p_v for k in cls.DEFAULTS: if k not in parameters: A_ = cls.DEFAULTS[k] return parameters
312
import unittest from typing import Tuple import torch from diffusers.utils import floats_tensor, randn_tensor, torch_all_close, torch_device from diffusers.utils.testing_utils import require_torch @require_torch class _a : """simple docstring""" @property def __A ( self : Union[str, Any] ): return self.get_dummy_input() @property def __A ( self : int ): if self.block_type == "down": return (4, 32, 16, 16) elif self.block_type == "mid": return (4, 32, 32, 32) elif self.block_type == "up": return (4, 32, 64, 64) raise ValueError(f'''\'{self.block_type}\' is not a supported block_type. Set it to \'up\', \'mid\', or \'down\'.''' ) def __A ( self : Union[str, Any] , UpperCAmelCase : List[Any]=True , UpperCAmelCase : str=False , UpperCAmelCase : Tuple=False , UpperCAmelCase : Optional[Any]=False , ): A_ = 4 A_ = 32 A_ = (32, 32) A_ = torch.manual_seed(0 ) A_ = torch.device(UpperCAmelCase ) A_ = (batch_size, num_channels) + sizes A_ = randn_tensor(UpperCAmelCase , generator=UpperCAmelCase , device=UpperCAmelCase ) A_ = {"hidden_states": hidden_states} if include_temb: A_ = 128 A_ = randn_tensor((batch_size, temb_channels) , generator=UpperCAmelCase , device=UpperCAmelCase ) if include_res_hidden_states_tuple: A_ = torch.manual_seed(1 ) A_ = (randn_tensor(UpperCAmelCase , generator=UpperCAmelCase , device=UpperCAmelCase ),) if include_encoder_hidden_states: A_ = floats_tensor((batch_size, 32, 32) ).to(UpperCAmelCase ) if include_skip_sample: A_ = randn_tensor(((batch_size, 3) + sizes) , generator=UpperCAmelCase , device=UpperCAmelCase ) return dummy_input def __A ( self : Optional[int] ): A_ = { "in_channels": 32, "out_channels": 32, "temb_channels": 128, } if self.block_type == "up": A_ = 32 if self.block_type == "mid": init_dict.pop("out_channels" ) A_ = self.dummy_input return init_dict, inputs_dict def __A ( self : List[str] , UpperCAmelCase : Optional[Any] ): A_ , A_ = self.prepare_init_args_and_inputs_for_common() A_ = self.block_class(**UpperCAmelCase ) unet_block.to(UpperCAmelCase ) unet_block.eval() with torch.no_grad(): A_ = unet_block(**UpperCAmelCase ) if isinstance(UpperCAmelCase , UpperCAmelCase ): A_ = output[0] self.assertEqual(output.shape , self.output_shape ) A_ = output[0, -1, -3:, -3:] A_ = torch.tensor(UpperCAmelCase ).to(UpperCAmelCase ) assert torch_all_close(output_slice.flatten() , UpperCAmelCase , atol=5E-3 ) @unittest.skipIf(torch_device == "mps" , "Training is not supported in mps" ) def __A ( self : Union[str, Any] ): A_ , A_ = self.prepare_init_args_and_inputs_for_common() A_ = self.block_class(**UpperCAmelCase ) model.to(UpperCAmelCase ) model.train() A_ = model(**UpperCAmelCase ) if isinstance(UpperCAmelCase , UpperCAmelCase ): A_ = output[0] A_ = torch.device(UpperCAmelCase ) A_ = randn_tensor(output.shape , device=UpperCAmelCase ) A_ = torch.nn.functional.mse_loss(UpperCAmelCase , UpperCAmelCase ) loss.backward()
312
1
__a :Dict = '0.18.2' from .configuration_utils import ConfigMixin from .utils import ( OptionalDependencyNotAvailable, is_flax_available, is_inflect_available, is_invisible_watermark_available, is_k_diffusion_available, is_k_diffusion_version, is_librosa_available, is_note_seq_available, is_onnx_available, is_scipy_available, is_torch_available, is_torchsde_available, is_transformers_available, is_transformers_version, is_unidecode_available, logging, ) try: if not is_onnx_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from .utils.dummy_onnx_objects import * # noqa F403 else: from .pipelines import OnnxRuntimeModel try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from .utils.dummy_pt_objects import * # noqa F403 else: from .models import ( AutoencoderKL, ControlNetModel, ModelMixin, PriorTransformer, TaFilmDecoder, TransformeraDModel, UNetaDModel, UNetaDConditionModel, UNetaDModel, UNetaDConditionModel, VQModel, ) from .optimization import ( get_constant_schedule, get_constant_schedule_with_warmup, get_cosine_schedule_with_warmup, get_cosine_with_hard_restarts_schedule_with_warmup, get_linear_schedule_with_warmup, get_polynomial_decay_schedule_with_warmup, get_scheduler, ) from .pipelines import ( AudioPipelineOutput, ConsistencyModelPipeline, DanceDiffusionPipeline, DDIMPipeline, DDPMPipeline, DiffusionPipeline, DiTPipeline, ImagePipelineOutput, KarrasVePipeline, LDMPipeline, LDMSuperResolutionPipeline, PNDMPipeline, RePaintPipeline, ScoreSdeVePipeline, ) from .schedulers import ( CMStochasticIterativeScheduler, DDIMInverseScheduler, DDIMParallelScheduler, DDIMScheduler, DDPMParallelScheduler, DDPMScheduler, DEISMultistepScheduler, DPMSolverMultistepInverseScheduler, DPMSolverMultistepScheduler, DPMSolverSinglestepScheduler, EulerAncestralDiscreteScheduler, EulerDiscreteScheduler, HeunDiscreteScheduler, IPNDMScheduler, KarrasVeScheduler, KDPMaAncestralDiscreteScheduler, KDPMaDiscreteScheduler, PNDMScheduler, RePaintScheduler, SchedulerMixin, ScoreSdeVeScheduler, UnCLIPScheduler, UniPCMultistepScheduler, VQDiffusionScheduler, ) from .training_utils import EMAModel try: if not (is_torch_available() and is_scipy_available()): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from .utils.dummy_torch_and_scipy_objects import * # noqa F403 else: from .schedulers import LMSDiscreteScheduler try: if not (is_torch_available() and is_torchsde_available()): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from .utils.dummy_torch_and_torchsde_objects import * # noqa F403 else: from .schedulers import DPMSolverSDEScheduler try: if not (is_torch_available() and is_transformers_available()): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from .utils.dummy_torch_and_transformers_objects import * # noqa F403 else: from .pipelines import ( AltDiffusionImgaImgPipeline, AltDiffusionPipeline, AudioLDMPipeline, CycleDiffusionPipeline, IFImgaImgPipeline, IFImgaImgSuperResolutionPipeline, IFInpaintingPipeline, IFInpaintingSuperResolutionPipeline, IFPipeline, IFSuperResolutionPipeline, ImageTextPipelineOutput, KandinskyImgaImgPipeline, KandinskyInpaintPipeline, KandinskyPipeline, KandinskyPriorPipeline, KandinskyVaaControlnetImgaImgPipeline, KandinskyVaaControlnetPipeline, KandinskyVaaImgaImgPipeline, KandinskyVaaInpaintPipeline, KandinskyVaaPipeline, KandinskyVaaPriorEmbaEmbPipeline, KandinskyVaaPriorPipeline, LDMTextToImagePipeline, PaintByExamplePipeline, SemanticStableDiffusionPipeline, ShapEImgaImgPipeline, ShapEPipeline, StableDiffusionAttendAndExcitePipeline, StableDiffusionControlNetImgaImgPipeline, StableDiffusionControlNetInpaintPipeline, StableDiffusionControlNetPipeline, StableDiffusionDepthaImgPipeline, StableDiffusionDiffEditPipeline, StableDiffusionImageVariationPipeline, StableDiffusionImgaImgPipeline, StableDiffusionInpaintPipeline, StableDiffusionInpaintPipelineLegacy, StableDiffusionInstructPixaPixPipeline, StableDiffusionLatentUpscalePipeline, StableDiffusionLDMaDPipeline, StableDiffusionModelEditingPipeline, StableDiffusionPanoramaPipeline, StableDiffusionParadigmsPipeline, StableDiffusionPipeline, StableDiffusionPipelineSafe, StableDiffusionPixaPixZeroPipeline, StableDiffusionSAGPipeline, StableDiffusionUpscalePipeline, StableUnCLIPImgaImgPipeline, StableUnCLIPPipeline, TextToVideoSDPipeline, TextToVideoZeroPipeline, UnCLIPImageVariationPipeline, UnCLIPPipeline, UniDiffuserModel, UniDiffuserPipeline, UniDiffuserTextDecoder, VersatileDiffusionDualGuidedPipeline, VersatileDiffusionImageVariationPipeline, VersatileDiffusionPipeline, VersatileDiffusionTextToImagePipeline, VideoToVideoSDPipeline, VQDiffusionPipeline, ) try: if not (is_torch_available() and is_transformers_available() and is_invisible_watermark_available()): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from .utils.dummy_torch_and_transformers_and_invisible_watermark_objects import * # noqa F403 else: from .pipelines import StableDiffusionXLImgaImgPipeline, StableDiffusionXLPipeline try: if not (is_torch_available() and is_transformers_available() and is_k_diffusion_available()): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from .utils.dummy_torch_and_transformers_and_k_diffusion_objects import * # noqa F403 else: from .pipelines import StableDiffusionKDiffusionPipeline try: if not (is_torch_available() and is_transformers_available() and is_onnx_available()): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from .utils.dummy_torch_and_transformers_and_onnx_objects import * # noqa F403 else: from .pipelines import ( OnnxStableDiffusionImgaImgPipeline, OnnxStableDiffusionInpaintPipeline, OnnxStableDiffusionInpaintPipelineLegacy, OnnxStableDiffusionPipeline, OnnxStableDiffusionUpscalePipeline, StableDiffusionOnnxPipeline, ) try: if not (is_torch_available() and is_librosa_available()): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from .utils.dummy_torch_and_librosa_objects import * # noqa F403 else: from .pipelines import AudioDiffusionPipeline, Mel try: if not (is_transformers_available() and is_torch_available() and is_note_seq_available()): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from .utils.dummy_transformers_and_torch_and_note_seq_objects import * # noqa F403 else: from .pipelines import SpectrogramDiffusionPipeline try: if not is_flax_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from .utils.dummy_flax_objects import * # noqa F403 else: from .models.controlnet_flax import FlaxControlNetModel from .models.modeling_flax_utils import FlaxModelMixin from .models.unet_ad_condition_flax import FlaxUNetaDConditionModel from .models.vae_flax import FlaxAutoencoderKL from .pipelines import FlaxDiffusionPipeline from .schedulers import ( FlaxDDIMScheduler, FlaxDDPMScheduler, FlaxDPMSolverMultistepScheduler, FlaxKarrasVeScheduler, FlaxLMSDiscreteScheduler, FlaxPNDMScheduler, FlaxSchedulerMixin, FlaxScoreSdeVeScheduler, ) try: if not (is_flax_available() and is_transformers_available()): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from .utils.dummy_flax_and_transformers_objects import * # noqa F403 else: from .pipelines import ( FlaxStableDiffusionControlNetPipeline, FlaxStableDiffusionImgaImgPipeline, FlaxStableDiffusionInpaintPipeline, FlaxStableDiffusionPipeline, ) try: if not (is_note_seq_available()): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from .utils.dummy_note_seq_objects import * # noqa F403 else: from .pipelines import MidiProcessor
312
import copy import fnmatch import json import os import pickle as pkl import shutil import sys import tarfile import tempfile from collections import OrderedDict from contextlib import contextmanager from functools import partial from hashlib import shaaaa from io import BytesIO from pathlib import Path from urllib.parse import urlparse from zipfile import ZipFile, is_zipfile import cva import numpy as np import requests import wget from filelock import FileLock from PIL import Image from tqdm.auto import tqdm from yaml import Loader, dump, load try: import torch __a :int = True except ImportError: __a :Optional[Any] = False try: from torch.hub import _get_torch_home __a :Optional[Any] = _get_torch_home() except ImportError: __a :Tuple = os.path.expanduser( os.getenv('TORCH_HOME', os.path.join(os.getenv('XDG_CACHE_HOME', '~/.cache'), 'torch')) ) __a :Optional[Any] = os.path.join(torch_cache_home, 'transformers') __a :int = 'https://cdn.huggingface.co' __a :Any = 'https://s3.amazonaws.com/models.huggingface.co/bert' __a :Optional[Any] = '/'.join(str(Path(__file__).resolve()).split('/')[:-1]) __a :str = os.path.join(PATH, 'config.yaml') __a :str = os.path.join(PATH, 'attributes.txt') __a :Optional[Any] = os.path.join(PATH, 'objects.txt') __a :Optional[int] = os.getenv('PYTORCH_PRETRAINED_BERT_CACHE', default_cache_path) __a :Dict = os.getenv('PYTORCH_TRANSFORMERS_CACHE', PYTORCH_PRETRAINED_BERT_CACHE) __a :List[Any] = os.getenv('TRANSFORMERS_CACHE', PYTORCH_TRANSFORMERS_CACHE) __a :List[str] = 'pytorch_model.bin' __a :Tuple = 'config.yaml' def __snake_case ( __UpperCamelCase : Optional[Any]=OBJECTS ,__UpperCamelCase : List[str]=ATTRIBUTES ): """simple docstring""" A_ = [] with open(__UpperCamelCase ) as f: for object in f.readlines(): vg_classes.append(object.split("," )[0].lower().strip() ) A_ = [] with open(__UpperCamelCase ) as f: for object in f.readlines(): vg_attrs.append(object.split("," )[0].lower().strip() ) return vg_classes, vg_attrs def __snake_case ( __UpperCamelCase : List[Any] ): """simple docstring""" A_ = OrderedDict() with open(__UpperCamelCase ,"rb" ) as f: A_ = pkl.load(__UpperCamelCase )["model"] for k in copy.deepcopy(list(ckp.keys() ) ): A_ = ckp.pop(__UpperCamelCase ) if isinstance(__UpperCamelCase ,np.ndarray ): A_ = torch.tensor(__UpperCamelCase ) else: assert isinstance(__UpperCamelCase ,torch.tensor ), type(__UpperCamelCase ) A_ = v return r class _a : """simple docstring""" _lowerCamelCase : Union[str, Any] = {} def __init__( self : str , UpperCAmelCase : dict , UpperCAmelCase : str = "root" , UpperCAmelCase : List[str]=0 ): A_ = name A_ = level A_ = {} for k, v in dictionary.items(): if v is None: raise ValueError() A_ = copy.deepcopy(UpperCAmelCase ) A_ = copy.deepcopy(UpperCAmelCase ) if isinstance(UpperCAmelCase , UpperCAmelCase ): A_ = Config(UpperCAmelCase , name=UpperCAmelCase , level=level + 1 ) A_ = v setattr(self , UpperCAmelCase , UpperCAmelCase ) A_ = d def __repr__( self : Optional[Any] ): return str(list((self._pointer.keys()) ) ) def __setattr__( self : Any , UpperCAmelCase : Any , UpperCAmelCase : Any ): A_ = val A_ = val A_ = key.split("." ) A_ = len(UpperCAmelCase ) - 1 A_ = self._pointer if len(UpperCAmelCase ) > 1: for i, l in enumerate(UpperCAmelCase ): if hasattr(self , UpperCAmelCase ) and isinstance(getattr(self , UpperCAmelCase ) , UpperCAmelCase ): setattr(getattr(self , UpperCAmelCase ) , ".".join(levels[i:] ) , UpperCAmelCase ) if l == last_level: A_ = val else: A_ = pointer[l] def __A ( self : List[str] ): return self._pointer def __A ( self : int , UpperCAmelCase : Tuple , UpperCAmelCase : int ): with open(f'''{file_name}''' , "w" ) as stream: dump(UpperCAmelCase , UpperCAmelCase ) def __A ( self : List[Any] , UpperCAmelCase : str , UpperCAmelCase : Tuple ): with open(f'''{file_name}''' , "w" ) as stream: json.dump(UpperCAmelCase , UpperCAmelCase ) @staticmethod def __A ( UpperCAmelCase : Optional[int] ): with open(UpperCAmelCase ) as stream: A_ = load(UpperCAmelCase , Loader=UpperCAmelCase ) return data def __str__( self : str ): A_ = " " if self._name != "root": A_ = f'''{t * (self._level-1)}{self._name}:\n''' else: A_ = "" A_ = self._level for i, (k, v) in enumerate(self._pointer.items() ): if isinstance(UpperCAmelCase , UpperCAmelCase ): r += f'''{t * (self._level)}{v}\n''' self._level += 1 else: r += f'''{t * (self._level)}{k}: {v} ({type(UpperCAmelCase ).__name__})\n''' A_ = level return r[:-1] @classmethod def __A ( cls : Optional[Any] , UpperCAmelCase : str , **UpperCAmelCase : str ): A_ , A_ = cls.get_config_dict(UpperCAmelCase , **UpperCAmelCase ) return cls(UpperCAmelCase ) @classmethod def __A ( cls : int , UpperCAmelCase : str , **UpperCAmelCase : int ): A_ = kwargs.pop("cache_dir" , UpperCAmelCase ) A_ = kwargs.pop("force_download" , UpperCAmelCase ) A_ = kwargs.pop("resume_download" , UpperCAmelCase ) A_ = kwargs.pop("proxies" , UpperCAmelCase ) A_ = kwargs.pop("local_files_only" , UpperCAmelCase ) if os.path.isdir(UpperCAmelCase ): A_ = os.path.join(UpperCAmelCase , UpperCAmelCase ) elif os.path.isfile(UpperCAmelCase ) or is_remote_url(UpperCAmelCase ): A_ = pretrained_model_name_or_path else: A_ = hf_bucket_url(UpperCAmelCase , filename=UpperCAmelCase , use_cdn=UpperCAmelCase ) try: # Load from URL or cache if already cached A_ = cached_path( UpperCAmelCase , cache_dir=UpperCAmelCase , force_download=UpperCAmelCase , proxies=UpperCAmelCase , resume_download=UpperCAmelCase , local_files_only=UpperCAmelCase , ) # Load config dict if resolved_config_file is None: raise EnvironmentError A_ = Config.load_yaml(UpperCAmelCase ) except EnvironmentError: A_ = "Can't load config for" raise EnvironmentError(UpperCAmelCase ) if resolved_config_file == config_file: print("loading configuration file from path" ) else: print("loading configuration file cache" ) return Config.load_yaml(UpperCAmelCase ), kwargs def __snake_case ( __UpperCamelCase : Union[str, Any] ): """simple docstring""" A_ = torch.load("dump.pt" ,map_location=in_tensor.device ) A_ = in_tensor.numpy() A_ = out_tensor.numpy()[0] print(na.shape ,na[0, 0, :5] ) print(na.shape ,na[0, 0, :5] ) assert np.allclose(__UpperCamelCase ,__UpperCamelCase ,rtol=0.01 ,atol=0.1 ), ( f'''{sum([1 for x in np.isclose(__UpperCamelCase ,__UpperCamelCase ,rtol=0.01 ,atol=0.1 ).flatten() if x is False] )/len(na.flatten() )*100:.4f} %''' " element-wise mismatch" ) raise Exception("tensors are all good" ) # Hugging face functions below def __snake_case ( __UpperCamelCase : Optional[int] ): """simple docstring""" A_ = urlparse(__UpperCamelCase ) return parsed.scheme in ("http", "https") def __snake_case ( __UpperCamelCase : str ,__UpperCamelCase : str ,__UpperCamelCase : str=True ): """simple docstring""" A_ = CLOUDFRONT_DISTRIB_PREFIX if use_cdn else S3_BUCKET_PREFIX A_ = "/" not in model_id if legacy_format: return f'''{endpoint}/{model_id}-{filename}''' else: return f'''{endpoint}/{model_id}/{filename}''' def __snake_case ( __UpperCamelCase : List[str] ,__UpperCamelCase : Union[str, Any] ,__UpperCamelCase : List[str]=None ,__UpperCamelCase : int=0 ,__UpperCamelCase : int=None ,): """simple docstring""" A_ = "python/{}".format(sys.version.split()[0] ) if _torch_available: ua += "; torch/{}".format(torch.__version__ ) if isinstance(__UpperCamelCase ,__UpperCamelCase ): ua += "; " + "; ".join("{}/{}".format(__UpperCamelCase ,__UpperCamelCase ) for k, v in user_agent.items() ) elif isinstance(__UpperCamelCase ,__UpperCamelCase ): ua += "; " + user_agent A_ = {"user-agent": ua} if resume_size > 0: A_ = "bytes=%d-" % (resume_size,) A_ = requests.get(__UpperCamelCase ,stream=__UpperCamelCase ,proxies=__UpperCamelCase ,headers=__UpperCamelCase ) if response.status_code == 416: # Range not satisfiable return A_ = response.headers.get("Content-Length" ) A_ = resume_size + int(__UpperCamelCase ) if content_length is not None else None A_ = tqdm( unit="B" ,unit_scale=__UpperCamelCase ,total=__UpperCamelCase ,initial=__UpperCamelCase ,desc="Downloading" ,) for chunk in response.iter_content(chunk_size=1024 ): if chunk: # filter out keep-alive new chunks progress.update(len(__UpperCamelCase ) ) temp_file.write(__UpperCamelCase ) progress.close() def __snake_case ( __UpperCamelCase : str ,__UpperCamelCase : Any=None ,__UpperCamelCase : Dict=False ,__UpperCamelCase : Union[str, Any]=None ,__UpperCamelCase : Any=10 ,__UpperCamelCase : int=False ,__UpperCamelCase : Optional[Any]=None ,__UpperCamelCase : str=False ,): """simple docstring""" if cache_dir is None: A_ = TRANSFORMERS_CACHE if isinstance(__UpperCamelCase ,__UpperCamelCase ): A_ = str(__UpperCamelCase ) os.makedirs(__UpperCamelCase ,exist_ok=__UpperCamelCase ) A_ = None if not local_files_only: try: A_ = requests.head(__UpperCamelCase ,allow_redirects=__UpperCamelCase ,proxies=__UpperCamelCase ,timeout=__UpperCamelCase ) if response.status_code == 200: A_ = response.headers.get("ETag" ) except (EnvironmentError, requests.exceptions.Timeout): # etag is already None pass A_ = url_to_filename(__UpperCamelCase ,__UpperCamelCase ) # get cache path to put the file A_ = os.path.join(__UpperCamelCase ,__UpperCamelCase ) # etag is None = we don't have a connection, or url doesn't exist, or is otherwise inaccessible. # try to get the last downloaded one if etag is None: if os.path.exists(__UpperCamelCase ): return cache_path else: A_ = [ file for file in fnmatch.filter(os.listdir(__UpperCamelCase ) ,filename + ".*" ) if not file.endswith(".json" ) and not file.endswith(".lock" ) ] if len(__UpperCamelCase ) > 0: return os.path.join(__UpperCamelCase ,matching_files[-1] ) else: # If files cannot be found and local_files_only=True, # the models might've been found if local_files_only=False # Notify the user about that if local_files_only: raise ValueError( "Cannot find the requested files in the cached path and outgoing traffic has been" " disabled. To enable model look-ups and downloads online, set 'local_files_only'" " to False." ) return None # From now on, etag is not None. if os.path.exists(__UpperCamelCase ) and not force_download: return cache_path # Prevent parallel downloads of the same file with a lock. A_ = cache_path + ".lock" with FileLock(__UpperCamelCase ): # If the download just completed while the lock was activated. if os.path.exists(__UpperCamelCase ) and not force_download: # Even if returning early like here, the lock will be released. return cache_path if resume_download: A_ = cache_path + ".incomplete" @contextmanager def _resumable_file_manager(): with open(__UpperCamelCase ,"a+b" ) as f: yield f A_ = _resumable_file_manager if os.path.exists(__UpperCamelCase ): A_ = os.stat(__UpperCamelCase ).st_size else: A_ = 0 else: A_ = partial(tempfile.NamedTemporaryFile ,dir=__UpperCamelCase ,delete=__UpperCamelCase ) A_ = 0 # Download to temporary file, then copy to cache dir once finished. # Otherwise you get corrupt cache entries if the download gets interrupted. with temp_file_manager() as temp_file: print( "%s not found in cache or force_download set to True, downloading to %s" ,__UpperCamelCase ,temp_file.name ,) http_get( __UpperCamelCase ,__UpperCamelCase ,proxies=__UpperCamelCase ,resume_size=__UpperCamelCase ,user_agent=__UpperCamelCase ,) os.replace(temp_file.name ,__UpperCamelCase ) A_ = {"url": url, "etag": etag} A_ = cache_path + ".json" with open(__UpperCamelCase ,"w" ) as meta_file: json.dump(__UpperCamelCase ,__UpperCamelCase ) return cache_path def __snake_case ( __UpperCamelCase : List[Any] ,__UpperCamelCase : str=None ): """simple docstring""" A_ = url.encode("utf-8" ) A_ = shaaaa(__UpperCamelCase ) A_ = url_hash.hexdigest() if etag: A_ = etag.encode("utf-8" ) A_ = shaaaa(__UpperCamelCase ) filename += "." + etag_hash.hexdigest() if url.endswith(".h5" ): filename += ".h5" return filename def __snake_case ( __UpperCamelCase : Union[str, Any] ,__UpperCamelCase : Union[str, Any]=None ,__UpperCamelCase : List[Any]=False ,__UpperCamelCase : List[str]=None ,__UpperCamelCase : Any=False ,__UpperCamelCase : Optional[int]=None ,__UpperCamelCase : Optional[Any]=False ,__UpperCamelCase : Dict=False ,__UpperCamelCase : Optional[Any]=False ,): """simple docstring""" if cache_dir is None: A_ = TRANSFORMERS_CACHE if isinstance(__UpperCamelCase ,__UpperCamelCase ): A_ = str(__UpperCamelCase ) if isinstance(__UpperCamelCase ,__UpperCamelCase ): A_ = str(__UpperCamelCase ) if is_remote_url(__UpperCamelCase ): # URL, so get it from the cache (downloading if necessary) A_ = get_from_cache( __UpperCamelCase ,cache_dir=__UpperCamelCase ,force_download=__UpperCamelCase ,proxies=__UpperCamelCase ,resume_download=__UpperCamelCase ,user_agent=__UpperCamelCase ,local_files_only=__UpperCamelCase ,) elif os.path.exists(__UpperCamelCase ): # File, and it exists. A_ = url_or_filename elif urlparse(__UpperCamelCase ).scheme == "": # File, but it doesn't exist. raise EnvironmentError("file {} not found".format(__UpperCamelCase ) ) else: # Something unknown raise ValueError("unable to parse {} as a URL or as a local path".format(__UpperCamelCase ) ) if extract_compressed_file: if not is_zipfile(__UpperCamelCase ) and not tarfile.is_tarfile(__UpperCamelCase ): return output_path # Path where we extract compressed archives # We avoid '.' in dir name and add "-extracted" at the end: "./model.zip" => "./model-zip-extracted/" A_ , A_ = os.path.split(__UpperCamelCase ) A_ = output_file.replace("." ,"-" ) + "-extracted" A_ = os.path.join(__UpperCamelCase ,__UpperCamelCase ) if os.path.isdir(__UpperCamelCase ) and os.listdir(__UpperCamelCase ) and not force_extract: return output_path_extracted # Prevent parallel extractions A_ = output_path + ".lock" with FileLock(__UpperCamelCase ): shutil.rmtree(__UpperCamelCase ,ignore_errors=__UpperCamelCase ) os.makedirs(__UpperCamelCase ) if is_zipfile(__UpperCamelCase ): with ZipFile(__UpperCamelCase ,"r" ) as zip_file: zip_file.extractall(__UpperCamelCase ) zip_file.close() elif tarfile.is_tarfile(__UpperCamelCase ): A_ = tarfile.open(__UpperCamelCase ) tar_file.extractall(__UpperCamelCase ) tar_file.close() else: raise EnvironmentError("Archive format of {} could not be identified".format(__UpperCamelCase ) ) return output_path_extracted return output_path def __snake_case ( __UpperCamelCase : str ,__UpperCamelCase : Any="," ): """simple docstring""" assert isinstance(__UpperCamelCase ,__UpperCamelCase ) if os.path.isfile(__UpperCamelCase ): with open(__UpperCamelCase ) as f: A_ = eval(f.read() ) else: A_ = requests.get(__UpperCamelCase ) try: A_ = requests.json() except Exception: A_ = req.content.decode() assert data is not None, "could not connect" try: A_ = eval(__UpperCamelCase ) except Exception: A_ = data.split("\n" ) req.close() return data def __snake_case ( __UpperCamelCase : int ): """simple docstring""" A_ = requests.get(__UpperCamelCase ) A_ = np.array(Image.open(BytesIO(response.content ) ) ) return img def __snake_case ( __UpperCamelCase : Tuple ): """simple docstring""" A_ = url.split("/" )[-1] if fn not in os.listdir(os.getcwd() ): wget.download(__UpperCamelCase ) with open(__UpperCamelCase ,"rb" ) as stream: A_ = pkl.load(__UpperCamelCase ) A_ = weights.pop("model" ) A_ = {} for k, v in model.items(): A_ = torch.from_numpy(__UpperCamelCase ) if "running_var" in k: A_ = torch.tensor([0] ) A_ = k.replace("running_var" ,"num_batches_tracked" ) A_ = zero return new def __snake_case ( ): """simple docstring""" print(f'''{os.path.abspath(os.path.join(__UpperCamelCase ,os.pardir ) )}/demo.ipynb''' ) def __snake_case ( __UpperCamelCase : Optional[Any] ,__UpperCamelCase : Optional[int]="RGB" ): """simple docstring""" assert isinstance(__UpperCamelCase ,__UpperCamelCase ) if os.path.isfile(__UpperCamelCase ): A_ = cva.imread(__UpperCamelCase ) else: A_ = get_image_from_url(__UpperCamelCase ) assert img is not None, f'''could not connect to: {im}''' A_ = cva.cvtColor(__UpperCamelCase ,cva.COLOR_BGR2RGB ) if input_format == "RGB": A_ = img[:, :, ::-1] return img def __snake_case ( __UpperCamelCase : List[str] ,__UpperCamelCase : List[str]=1 ): """simple docstring""" return (images[i : i + batch] for i in range(0 ,len(__UpperCamelCase ) ,__UpperCamelCase ))
312
1
import shutil import tempfile import unittest from transformers import SPIECE_UNDERLINE, BatchEncoding, MBartTokenizer, MBartTokenizerFast, is_torch_available from transformers.testing_utils import ( get_tests_dir, nested_simplify, require_sentencepiece, require_tokenizers, require_torch, ) from ...test_tokenization_common import TokenizerTesterMixin __a :Union[str, Any] = get_tests_dir('fixtures/test_sentencepiece.model') if is_torch_available(): from transformers.models.mbart.modeling_mbart import shift_tokens_right __a :Optional[int] = 25_0004 __a :Union[str, Any] = 25_0020 @require_sentencepiece @require_tokenizers class _a ( snake_case_ , unittest.TestCase ): """simple docstring""" _lowerCamelCase : Tuple = MBartTokenizer _lowerCamelCase : List[str] = MBartTokenizerFast _lowerCamelCase : Tuple = True _lowerCamelCase : Tuple = True def __A ( self : List[Any] ): super().setUp() # We have a SentencePiece fixture for testing A_ = MBartTokenizer(UpperCAmelCase , keep_accents=UpperCAmelCase ) tokenizer.save_pretrained(self.tmpdirname ) def __A ( self : int ): A_ = MBartTokenizer(UpperCAmelCase , keep_accents=UpperCAmelCase ) A_ = tokenizer.tokenize("This is a test" ) self.assertListEqual(UpperCAmelCase , ["▁This", "▁is", "▁a", "▁t", "est"] ) self.assertListEqual( tokenizer.convert_tokens_to_ids(UpperCAmelCase ) , [value + tokenizer.fairseq_offset for value in [285, 46, 10, 170, 382]] , ) A_ = tokenizer.tokenize("I was born in 92000, and this is falsé." ) self.assertListEqual( UpperCAmelCase , [ SPIECE_UNDERLINE + "I", SPIECE_UNDERLINE + "was", SPIECE_UNDERLINE + "b", "or", "n", SPIECE_UNDERLINE + "in", SPIECE_UNDERLINE + "", "9", "2", "0", "0", "0", ",", SPIECE_UNDERLINE + "and", SPIECE_UNDERLINE + "this", SPIECE_UNDERLINE + "is", SPIECE_UNDERLINE + "f", "al", "s", "é", ".", ] , ) A_ = tokenizer.convert_tokens_to_ids(UpperCAmelCase ) self.assertListEqual( UpperCAmelCase , [ value + tokenizer.fairseq_offset for value in [8, 21, 84, 55, 24, 19, 7, 2, 602, 347, 347, 347, 3, 12, 66, 46, 72, 80, 6, 2, 4] # ^ unk: 2 + 1 = 3 unk: 2 + 1 = 3 ^ ] , ) A_ = tokenizer.convert_ids_to_tokens(UpperCAmelCase ) self.assertListEqual( UpperCAmelCase , [ SPIECE_UNDERLINE + "I", SPIECE_UNDERLINE + "was", SPIECE_UNDERLINE + "b", "or", "n", SPIECE_UNDERLINE + "in", SPIECE_UNDERLINE + "", "<unk>", "2", "0", "0", "0", ",", SPIECE_UNDERLINE + "and", SPIECE_UNDERLINE + "this", SPIECE_UNDERLINE + "is", SPIECE_UNDERLINE + "f", "al", "s", "<unk>", ".", ] , ) def __A ( self : List[str] ): if not self.test_slow_tokenizer: # as we don't have a slow version, we can't compare the outputs between slow and fast versions return A_ = (self.rust_tokenizer_class, "hf-internal-testing/tiny-random-mbart", {}) for tokenizer, pretrained_name, kwargs in self.tokenizers_list: with self.subTest(f'''{tokenizer.__class__.__name__} ({pretrained_name})''' ): A_ = self.rust_tokenizer_class.from_pretrained(UpperCAmelCase , **UpperCAmelCase ) A_ = self.tokenizer_class.from_pretrained(UpperCAmelCase , **UpperCAmelCase ) A_ = tempfile.mkdtemp() A_ = tokenizer_r.save_pretrained(UpperCAmelCase ) A_ = tokenizer_p.save_pretrained(UpperCAmelCase ) # Checks it save with the same files + the tokenizer.json file for the fast one self.assertTrue(any("tokenizer.json" in f for f in tokenizer_r_files ) ) A_ = tuple(f for f in tokenizer_r_files if "tokenizer.json" not in f ) self.assertSequenceEqual(UpperCAmelCase , UpperCAmelCase ) # Checks everything loads correctly in the same way A_ = tokenizer_r.from_pretrained(UpperCAmelCase ) A_ = tokenizer_p.from_pretrained(UpperCAmelCase ) # Check special tokens are set accordingly on Rust and Python for key in tokenizer_pp.special_tokens_map: self.assertTrue(hasattr(UpperCAmelCase , UpperCAmelCase ) ) # self.assertEqual(getattr(tokenizer_rp, key), getattr(tokenizer_pp, key)) # self.assertEqual(getattr(tokenizer_rp, key + "_id"), getattr(tokenizer_pp, key + "_id")) shutil.rmtree(UpperCAmelCase ) # Save tokenizer rust, legacy_format=True A_ = tempfile.mkdtemp() A_ = tokenizer_r.save_pretrained(UpperCAmelCase , legacy_format=UpperCAmelCase ) A_ = tokenizer_p.save_pretrained(UpperCAmelCase ) # Checks it save with the same files self.assertSequenceEqual(UpperCAmelCase , UpperCAmelCase ) # Checks everything loads correctly in the same way A_ = tokenizer_r.from_pretrained(UpperCAmelCase ) A_ = tokenizer_p.from_pretrained(UpperCAmelCase ) # Check special tokens are set accordingly on Rust and Python for key in tokenizer_pp.special_tokens_map: self.assertTrue(hasattr(UpperCAmelCase , UpperCAmelCase ) ) shutil.rmtree(UpperCAmelCase ) # Save tokenizer rust, legacy_format=False A_ = tempfile.mkdtemp() A_ = tokenizer_r.save_pretrained(UpperCAmelCase , legacy_format=UpperCAmelCase ) A_ = tokenizer_p.save_pretrained(UpperCAmelCase ) # Checks it saved the tokenizer.json file self.assertTrue(any("tokenizer.json" in f for f in tokenizer_r_files ) ) # Checks everything loads correctly in the same way A_ = tokenizer_r.from_pretrained(UpperCAmelCase ) A_ = tokenizer_p.from_pretrained(UpperCAmelCase ) # Check special tokens are set accordingly on Rust and Python for key in tokenizer_pp.special_tokens_map: self.assertTrue(hasattr(UpperCAmelCase , UpperCAmelCase ) ) shutil.rmtree(UpperCAmelCase ) @require_torch @require_sentencepiece @require_tokenizers class _a ( unittest.TestCase ): """simple docstring""" _lowerCamelCase : List[str] = 'facebook/mbart-large-en-ro' _lowerCamelCase : str = [ ' UN Chief Says There Is No Military Solution in Syria', ' Secretary-General Ban Ki-moon says his response to Russia\'s stepped up military support for Syria is that "there is no military solution" to the nearly five-year conflict and more weapons will only worsen the violence and misery for millions of people.', ] _lowerCamelCase : Union[str, Any] = [ 'Şeful ONU declară că nu există o soluţie militară în Siria', 'Secretarul General Ban Ki-moon declară că răspunsul său la intensificarea sprijinului militar al Rusiei' ' pentru Siria este că "nu există o soluţie militară" la conflictul de aproape cinci ani şi că noi arme nu vor' ' face decât să înrăutăţească violenţele şi mizeria pentru milioane de oameni.', ] _lowerCamelCase : Tuple = [8_2_7_4, 1_2_7_8_7_3, 2_5_9_1_6, 7, 8_6_2_2, 2_0_7_1, 4_3_8, 6_7_4_8_5, 5_3, 1_8_7_8_9_5, 2_3, 5_1_7_1_2, 2, EN_CODE] @classmethod def __A ( cls : Tuple ): A_ = MBartTokenizer.from_pretrained( cls.checkpoint_name , src_lang="en_XX" , tgt_lang="ro_RO" ) A_ = 1 return cls def __A ( self : Any ): self.assertEqual(self.tokenizer.fairseq_tokens_to_ids["ar_AR"] , 250001 ) self.assertEqual(self.tokenizer.fairseq_tokens_to_ids["en_EN"] , 250004 ) self.assertEqual(self.tokenizer.fairseq_tokens_to_ids["ro_RO"] , 250020 ) def __A ( self : Tuple ): A_ = self.tokenizer.batch_encode_plus(self.src_text ).input_ids[0] self.assertListEqual(self.expected_src_tokens , UpperCAmelCase ) def __A ( self : Dict ): self.assertIn(UpperCAmelCase , self.tokenizer.all_special_ids ) A_ = [RO_CODE, 884, 9019, 96, 9, 916, 86792, 36, 18743, 15596, 5, 2] A_ = self.tokenizer.decode(UpperCAmelCase , skip_special_tokens=UpperCAmelCase ) A_ = self.tokenizer.decode(generated_ids[1:] , skip_special_tokens=UpperCAmelCase ) self.assertEqual(UpperCAmelCase , UpperCAmelCase ) self.assertNotIn(self.tokenizer.eos_token , UpperCAmelCase ) def __A ( self : Optional[int] ): A_ = ["this is gunna be a long sentence " * 20] assert isinstance(src_text[0] , UpperCAmelCase ) A_ = 10 A_ = self.tokenizer(UpperCAmelCase , max_length=UpperCAmelCase , truncation=UpperCAmelCase ).input_ids[0] self.assertEqual(ids[-2] , 2 ) self.assertEqual(ids[-1] , UpperCAmelCase ) self.assertEqual(len(UpperCAmelCase ) , UpperCAmelCase ) def __A ( self : Tuple ): self.assertListEqual(self.tokenizer.convert_tokens_to_ids(["<mask>", "ar_AR"] ) , [250026, 250001] ) def __A ( self : Optional[Any] ): A_ = tempfile.mkdtemp() A_ = self.tokenizer.fairseq_tokens_to_ids self.tokenizer.save_pretrained(UpperCAmelCase ) A_ = MBartTokenizer.from_pretrained(UpperCAmelCase ) self.assertDictEqual(new_tok.fairseq_tokens_to_ids , UpperCAmelCase ) @require_torch def __A ( self : int ): A_ = self.tokenizer(self.src_text , text_target=self.tgt_text , padding=UpperCAmelCase , return_tensors="pt" ) A_ = shift_tokens_right(batch["labels"] , self.tokenizer.pad_token_id ) # fairseq batch: https://gist.github.com/sshleifer/cba08bc2109361a74ac3760a7e30e4f4 assert batch.input_ids[1][-2:].tolist() == [2, EN_CODE] assert batch.decoder_input_ids[1][0].tolist() == RO_CODE assert batch.decoder_input_ids[1][-1] == 2 assert batch.labels[1][-2:].tolist() == [2, RO_CODE] @require_torch def __A ( self : Optional[Any] ): A_ = self.tokenizer( self.src_text , text_target=self.tgt_text , padding=UpperCAmelCase , truncation=UpperCAmelCase , max_length=len(self.expected_src_tokens ) , return_tensors="pt" , ) A_ = shift_tokens_right(batch["labels"] , self.tokenizer.pad_token_id ) self.assertIsInstance(UpperCAmelCase , UpperCAmelCase ) self.assertEqual((2, 14) , batch.input_ids.shape ) self.assertEqual((2, 14) , batch.attention_mask.shape ) A_ = batch.input_ids.tolist()[0] self.assertListEqual(self.expected_src_tokens , UpperCAmelCase ) self.assertEqual(2 , batch.decoder_input_ids[0, -1] ) # EOS # Test that special tokens are reset self.assertEqual(self.tokenizer.prefix_tokens , [] ) self.assertEqual(self.tokenizer.suffix_tokens , [self.tokenizer.eos_token_id, EN_CODE] ) def __A ( self : Optional[int] ): A_ = self.tokenizer(self.src_text , padding=UpperCAmelCase , truncation=UpperCAmelCase , max_length=3 , return_tensors="pt" ) A_ = self.tokenizer( text_target=self.tgt_text , padding=UpperCAmelCase , truncation=UpperCAmelCase , max_length=10 , return_tensors="pt" ) A_ = targets["input_ids"] A_ = shift_tokens_right(UpperCAmelCase , self.tokenizer.pad_token_id ) self.assertEqual(batch.input_ids.shape[1] , 3 ) self.assertEqual(batch.decoder_input_ids.shape[1] , 10 ) @require_torch def __A ( self : Optional[int] ): A_ = self.tokenizer._build_translation_inputs( "A test" , return_tensors="pt" , src_lang="en_XX" , tgt_lang="ar_AR" ) self.assertEqual( nested_simplify(UpperCAmelCase ) , { # A, test, EOS, en_XX "input_ids": [[62, 3034, 2, 250004]], "attention_mask": [[1, 1, 1, 1]], # ar_AR "forced_bos_token_id": 250001, } , )
312
from __future__ import annotations def __snake_case ( __UpperCamelCase : list[list[int]] ): """simple docstring""" for i in range(1 ,len(matrix[0] ) ): matrix[0][i] += matrix[0][i - 1] # preprocessing the first column for i in range(1 ,len(__UpperCamelCase ) ): matrix[i][0] += matrix[i - 1][0] # updating the path cost for current position for i in range(1 ,len(__UpperCamelCase ) ): for j in range(1 ,len(matrix[0] ) ): matrix[i][j] += min(matrix[i - 1][j] ,matrix[i][j - 1] ) return matrix[-1][-1] if __name__ == "__main__": import doctest doctest.testmod()
312
1
from __future__ import annotations __a :Tuple = { 'A': ['B', 'C', 'E'], 'B': ['A', 'D', 'E'], 'C': ['A', 'F', 'G'], 'D': ['B'], 'E': ['A', 'B', 'D'], 'F': ['C'], 'G': ['C'], } class _a : """simple docstring""" def __init__( self : Any , UpperCAmelCase : dict[str, list[str]] , UpperCAmelCase : str ): A_ = graph # mapping node to its parent in resulting breadth first tree A_ = {} A_ = source_vertex def __A ( self : Optional[Any] ): A_ = {self.source_vertex} A_ = None A_ = [self.source_vertex] # first in first out queue while queue: A_ = queue.pop(0 ) for adjacent_vertex in self.graph[vertex]: if adjacent_vertex not in visited: visited.add(UpperCAmelCase ) A_ = vertex queue.append(UpperCAmelCase ) def __A ( self : str , UpperCAmelCase : str ): if target_vertex == self.source_vertex: return self.source_vertex A_ = self.parent.get(UpperCAmelCase ) if target_vertex_parent is None: A_ = ( f'''No path from vertex: {self.source_vertex} to vertex: {target_vertex}''' ) raise ValueError(UpperCAmelCase ) return self.shortest_path(UpperCAmelCase ) + f'''->{target_vertex}''' if __name__ == "__main__": __a :Optional[int] = Graph(graph, 'G') g.breath_first_search() print(g.shortest_path('D')) print(g.shortest_path('G')) print(g.shortest_path('Foo'))
312
from typing import Dict from transformers import EvalPrediction, HfArgumentParser, TrainingArguments, is_torch_available from transformers.testing_utils import ( TestCasePlus, execute_subprocess_async, get_torch_dist_unique_port, require_torch_multi_gpu, require_torch_neuroncore, ) from transformers.training_args import ParallelMode from transformers.utils import logging __a :int = logging.get_logger(__name__) if is_torch_available(): import torch from torch import nn from torch.utils.data import Dataset from transformers import Trainer class _a ( snake_case_ ): """simple docstring""" def __init__( self : Tuple , UpperCAmelCase : int = 101 ): A_ = length def __len__( self : int ): return self.length def __getitem__( self : Optional[int] , UpperCAmelCase : Optional[int] ): return i class _a : """simple docstring""" def __call__( self : Any , UpperCAmelCase : Optional[Any] ): return {"input_ids": torch.tensor(UpperCAmelCase ), "labels": torch.tensor(UpperCAmelCase )} class _a ( nn.Module ): """simple docstring""" def __init__( self : int ): super().__init__() # Add some (unused) params otherwise DDP will complain. A_ = nn.Linear(120 , 80 ) def __A ( self : Tuple , UpperCAmelCase : Dict , UpperCAmelCase : Tuple=None ): if labels is not None: return torch.tensor(0.0 , device=input_ids.device ), input_ids else: return input_ids class _a ( snake_case_ ): """simple docstring""" @require_torch_neuroncore def __A ( self : List[str] ): A_ = f'''--nproc_per_node=2 --master_port={get_torch_dist_unique_port()} {self.test_file_dir}/test_trainer_distributed.py '''.split() A_ = self.get_auto_remove_tmp_dir() A_ = f'''--output_dir {output_dir}'''.split() A_ = ["torchrun"] + distributed_args + args execute_subprocess_async(UpperCAmelCase , env=self.get_env() ) # successful return here == success - any errors would have caused an error in the sub-call class _a ( snake_case_ ): """simple docstring""" @require_torch_multi_gpu def __A ( self : List[str] ): A_ = f'''--nproc_per_node={torch.cuda.device_count()} --master_port={get_torch_dist_unique_port()} {self.test_file_dir}/test_trainer_distributed.py '''.split() A_ = self.get_auto_remove_tmp_dir() A_ = f'''--output_dir {output_dir}'''.split() A_ = ["torchrun"] + distributed_args + args execute_subprocess_async(UpperCAmelCase , env=self.get_env() ) # successful return here == success - any errors would have caused an error in the sub-call if __name__ == "__main__": # The script below is meant to be run under torch.distributed, on a machine with multiple GPUs: # # PYTHONPATH="src" python -m torch.distributed.run --nproc_per_node 2 --output_dir output_dir ./tests/test_trainer_distributed.py __a :Union[str, Any] = HfArgumentParser((TrainingArguments,)) __a :Tuple = parser.parse_args_into_dataclasses()[0] logger.warning( F"Process rank: {training_args.local_rank}, device: {training_args.device}, n_gpu: {training_args.n_gpu}, " F"distributed training: {training_args.parallel_mode != ParallelMode.NOT_DISTRIBUTED}" ) # Essentially, what we want to verify in the distributed case is that we get all samples back, # in the right order. (this is crucial for prediction for instance) for dataset_length in [101, 40, 7]: __a :int = DummyDataset(dataset_length) def __snake_case ( __UpperCamelCase : EvalPrediction ): """simple docstring""" A_ = list(range(len(__UpperCamelCase ) ) ) A_ = p.predictions.tolist() == sequential and p.label_ids.tolist() == sequential if not success and training_args.local_rank == 0: logger.warning( "Predictions and/or labels do not match expected results:\n - predictions: " f'''{p.predictions.tolist()}\n - labels: {p.label_ids.tolist()}\n - expected: {sequential}''' ) return {"success": success} __a :str = Trainer( model=DummyModel(), args=training_args, data_collator=DummyDataCollator(), eval_dataset=dataset, compute_metrics=compute_metrics, ) __a :str = trainer.evaluate() logger.info(metrics) if metrics["eval_success"] is not True: logger.error(metrics) exit(1) __a :str = trainer.predict(dataset) logger.info(p.metrics) if p.metrics["test_success"] is not True: logger.error(p.metrics) exit(1) __a :Optional[int] = 2 __a :List[Any] = trainer.evaluate() logger.info(metrics) if metrics["eval_success"] is not True: logger.error(metrics) exit(1) __a :str = trainer.predict(dataset) logger.info(p.metrics) if p.metrics["test_success"] is not True: logger.error(p.metrics) exit(1) __a :Union[str, Any] = None
312
1
from typing import Tuple, Union from ...modeling_outputs import BackboneOutput from ...modeling_utils import PreTrainedModel from ...utils import is_timm_available, is_torch_available, requires_backends from ...utils.backbone_utils import BackboneMixin from .configuration_timm_backbone import TimmBackboneConfig if is_timm_available(): import timm if is_torch_available(): from torch import Tensor class _a ( snake_case_ , snake_case_ ): """simple docstring""" _lowerCamelCase : Tuple = 'pixel_values' _lowerCamelCase : Optional[Any] = False _lowerCamelCase : Dict = TimmBackboneConfig def __init__( self : int , UpperCAmelCase : str , **UpperCAmelCase : Optional[Any] ): requires_backends(self , "timm" ) super().__init__(UpperCAmelCase ) A_ = config if config.backbone is None: raise ValueError("backbone is not set in the config. Please set it to a timm model name." ) if config.backbone not in timm.list_models(): raise ValueError(f'''backbone {config.backbone} is not supported by timm.''' ) if hasattr(UpperCAmelCase , "out_features" ) and config.out_features is not None: raise ValueError("out_features is not supported by TimmBackbone. Please use out_indices instead." ) A_ = getattr(UpperCAmelCase , "use_pretrained_backbone" , UpperCAmelCase ) if pretrained is None: raise ValueError("use_pretrained_backbone is not set in the config. Please set it to True or False." ) # We just take the final layer by default. This matches the default for the transformers models. A_ = config.out_indices if getattr(UpperCAmelCase , "out_indices" , UpperCAmelCase ) is not None else (-1,) A_ = timm.create_model( config.backbone , pretrained=UpperCAmelCase , features_only=config.features_only , in_chans=config.num_channels , out_indices=UpperCAmelCase , **UpperCAmelCase , ) # These are used to control the output of the model when called. If output_hidden_states is True, then # return_layers is modified to include all layers. A_ = self._backbone.return_layers A_ = {layer["module"]: str(UpperCAmelCase ) for i, layer in enumerate(self._backbone.feature_info.info )} super()._init_backbone(UpperCAmelCase ) @classmethod def __A ( cls : Optional[Any] , UpperCAmelCase : Optional[Any] , *UpperCAmelCase : List[Any] , **UpperCAmelCase : int ): requires_backends(cls , ["vision", "timm"] ) from ...models.timm_backbone import TimmBackboneConfig A_ = kwargs.pop("config" , TimmBackboneConfig() ) A_ = kwargs.pop("use_timm_backbone" , UpperCAmelCase ) if not use_timm: raise ValueError("use_timm_backbone must be True for timm backbones" ) A_ = kwargs.pop("num_channels" , config.num_channels ) A_ = kwargs.pop("features_only" , config.features_only ) A_ = kwargs.pop("use_pretrained_backbone" , config.use_pretrained_backbone ) A_ = kwargs.pop("out_indices" , config.out_indices ) A_ = TimmBackboneConfig( backbone=UpperCAmelCase , num_channels=UpperCAmelCase , features_only=UpperCAmelCase , use_pretrained_backbone=UpperCAmelCase , out_indices=UpperCAmelCase , ) return super()._from_config(UpperCAmelCase , **UpperCAmelCase ) def __A ( self : Any , UpperCAmelCase : Tuple ): pass def __A ( self : Any , UpperCAmelCase : int , UpperCAmelCase : int=None , UpperCAmelCase : int=None , UpperCAmelCase : Optional[Any]=None , **UpperCAmelCase : Optional[Any] ): A_ = return_dict if return_dict is not None else self.config.use_return_dict A_ = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states ) A_ = output_attentions if output_attentions is not None else self.config.output_attentions if output_attentions: raise ValueError("Cannot output attentions for timm backbones at the moment" ) if output_hidden_states: # We modify the return layers to include all the stages of the backbone A_ = self._all_layers A_ = self._backbone(UpperCAmelCase , **UpperCAmelCase ) A_ = self._return_layers A_ = tuple(hidden_states[i] for i in self.out_indices ) else: A_ = self._backbone(UpperCAmelCase , **UpperCAmelCase ) A_ = None A_ = tuple(UpperCAmelCase ) A_ = tuple(UpperCAmelCase ) if hidden_states is not None else None if not return_dict: A_ = (feature_maps,) if output_hidden_states: A_ = output + (hidden_states,) return output return BackboneOutput(feature_maps=UpperCAmelCase , hidden_states=UpperCAmelCase , attentions=UpperCAmelCase )
312
import argparse import json from pathlib import Path import requests import torch from huggingface_hub import hf_hub_download from PIL import Image from timm import create_model from timm.data import resolve_data_config from timm.data.transforms_factory import create_transform from transformers import BitConfig, BitForImageClassification, BitImageProcessor from transformers.image_utils import PILImageResampling from transformers.utils import logging logging.set_verbosity_info() __a :Any = logging.get_logger(__name__) def __snake_case ( __UpperCamelCase : Optional[int] ): """simple docstring""" A_ = "huggingface/label-files" A_ = "imagenet-1k-id2label.json" A_ = json.load(open(hf_hub_download(__UpperCamelCase ,__UpperCamelCase ,repo_type="dataset" ) ,"r" ) ) A_ = {int(__UpperCamelCase ): v for k, v in idalabel.items()} A_ = {v: k for k, v in idalabel.items()} A_ = "std_conv" if "bit" in model_name else False # note that when using BiT as backbone for ViT-hybrid checkpoints, # one needs to additionally set config.layer_type = "bottleneck", config.stem_type = "same", # config.conv_layer = "std_conv_same" A_ = BitConfig( conv_layer=__UpperCamelCase ,num_labels=1000 ,idalabel=__UpperCamelCase ,labelaid=__UpperCamelCase ,) return config def __snake_case ( __UpperCamelCase : Union[str, Any] ): """simple docstring""" if "stem.conv" in name: A_ = name.replace("stem.conv" ,"bit.embedder.convolution" ) if "blocks" in name: A_ = name.replace("blocks" ,"layers" ) if "head.fc" in name: A_ = name.replace("head.fc" ,"classifier.1" ) if name.startswith("norm" ): A_ = "bit." + name if "bit" not in name and "classifier" not in name: A_ = "bit.encoder." + name return name def __snake_case ( ): """simple docstring""" A_ = "http://images.cocodataset.org/val2017/000000039769.jpg" A_ = Image.open(requests.get(__UpperCamelCase ,stream=__UpperCamelCase ).raw ) return im @torch.no_grad() def __snake_case ( __UpperCamelCase : Any ,__UpperCamelCase : Optional[Any] ,__UpperCamelCase : Tuple=False ): """simple docstring""" A_ = get_config(__UpperCamelCase ) # load original model from timm A_ = create_model(__UpperCamelCase ,pretrained=__UpperCamelCase ) timm_model.eval() # load state_dict of original model A_ = timm_model.state_dict() for key in state_dict.copy().keys(): A_ = state_dict.pop(__UpperCamelCase ) A_ = val.squeeze() if "head" in key else val # load HuggingFace model A_ = BitForImageClassification(__UpperCamelCase ) model.eval() model.load_state_dict(__UpperCamelCase ) # create image processor A_ = create_transform(**resolve_data_config({} ,model=__UpperCamelCase ) ) A_ = transform.transforms A_ = { "bilinear": PILImageResampling.BILINEAR, "bicubic": PILImageResampling.BICUBIC, "nearest": PILImageResampling.NEAREST, } A_ = BitImageProcessor( do_resize=__UpperCamelCase ,size={"shortest_edge": timm_transforms[0].size} ,resample=pillow_resamplings[timm_transforms[0].interpolation.value] ,do_center_crop=__UpperCamelCase ,crop_size={"height": timm_transforms[1].size[0], "width": timm_transforms[1].size[1]} ,do_normalize=__UpperCamelCase ,image_mean=timm_transforms[-1].mean.tolist() ,image_std=timm_transforms[-1].std.tolist() ,) A_ = prepare_img() A_ = transform(__UpperCamelCase ).unsqueeze(0 ) A_ = processor(__UpperCamelCase ,return_tensors="pt" ).pixel_values # verify pixel values assert torch.allclose(__UpperCamelCase ,__UpperCamelCase ) # verify logits with torch.no_grad(): A_ = model(__UpperCamelCase ) A_ = outputs.logits print("Logits:" ,logits[0, :3] ) print("Predicted class:" ,model.config.idalabel[logits.argmax(-1 ).item()] ) A_ = timm_model(__UpperCamelCase ) assert timm_logits.shape == outputs.logits.shape assert torch.allclose(__UpperCamelCase ,outputs.logits ,atol=1E-3 ) print("Looks ok!" ) if pytorch_dump_folder_path is not None: Path(__UpperCamelCase ).mkdir(exist_ok=__UpperCamelCase ) print(f'''Saving model {model_name} and processor to {pytorch_dump_folder_path}''' ) model.save_pretrained(__UpperCamelCase ) processor.save_pretrained(__UpperCamelCase ) if push_to_hub: print(f'''Pushing model {model_name} and processor to the hub''' ) model.push_to_hub(f'''ybelkada/{model_name}''' ) processor.push_to_hub(f'''ybelkada/{model_name}''' ) if __name__ == "__main__": __a :List[Any] = argparse.ArgumentParser() # Required parameters parser.add_argument( '--model_name', default='resnetv2_50x1_bitm', type=str, help='Name of the BiT timm model you\'d like to convert.', ) parser.add_argument( '--pytorch_dump_folder_path', default=None, type=str, help='Path to the output PyTorch model directory.' ) parser.add_argument( '--push_to_hub', action='store_true', help='Whether to push the model to the hub.', ) __a :str = parser.parse_args() convert_bit_checkpoint(args.model_name, args.pytorch_dump_folder_path, args.push_to_hub)
312
1
import os def __snake_case ( __UpperCamelCase : str = "input.txt" ): """simple docstring""" with open(os.path.join(os.path.dirname(__UpperCamelCase ) ,__UpperCamelCase ) ) as input_file: A_ = [ [int(__UpperCamelCase ) for element in line.split("," )] for line in input_file.readlines() ] A_ = len(__UpperCamelCase ) A_ = len(matrix[0] ) A_ = [[-1 for _ in range(__UpperCamelCase )] for _ in range(__UpperCamelCase )] for i in range(__UpperCamelCase ): A_ = matrix[i][0] for j in range(1 ,__UpperCamelCase ): for i in range(__UpperCamelCase ): A_ = minimal_path_sums[i][j - 1] + matrix[i][j] for i in range(1 ,__UpperCamelCase ): A_ = min( minimal_path_sums[i][j] ,minimal_path_sums[i - 1][j] + matrix[i][j] ) for i in range(rows - 2 ,-1 ,-1 ): A_ = min( minimal_path_sums[i][j] ,minimal_path_sums[i + 1][j] + matrix[i][j] ) return min(minimal_path_sums_row[-1] for minimal_path_sums_row in minimal_path_sums ) if __name__ == "__main__": print(F"{solution() = }")
312
import os import re import sys import traceback import warnings from pathlib import Path from typing import Dict, Optional, Union from uuid import uuida from huggingface_hub import HfFolder, ModelCard, ModelCardData, hf_hub_download, whoami from huggingface_hub.file_download import REGEX_COMMIT_HASH from huggingface_hub.utils import ( EntryNotFoundError, RepositoryNotFoundError, RevisionNotFoundError, is_jinja_available, ) from packaging import version from requests import HTTPError from .. import __version__ from .constants import ( DEPRECATED_REVISION_ARGS, DIFFUSERS_CACHE, HUGGINGFACE_CO_RESOLVE_ENDPOINT, SAFETENSORS_WEIGHTS_NAME, WEIGHTS_NAME, ) from .import_utils import ( ENV_VARS_TRUE_VALUES, _flax_version, _jax_version, _onnxruntime_version, _torch_version, is_flax_available, is_onnx_available, is_torch_available, ) from .logging import get_logger __a :Dict = get_logger(__name__) __a :Union[str, Any] = Path(__file__).parent / 'model_card_template.md' __a :Tuple = uuida().hex __a :List[Any] = os.getenv('HF_HUB_OFFLINE', '').upper() in ENV_VARS_TRUE_VALUES __a :Union[str, Any] = os.getenv('DISABLE_TELEMETRY', '').upper() in ENV_VARS_TRUE_VALUES __a :Tuple = HUGGINGFACE_CO_RESOLVE_ENDPOINT + '/api/telemetry/' def __snake_case ( __UpperCamelCase : Union[Dict, str, None] = None ): """simple docstring""" A_ = f'''diffusers/{__version__}; python/{sys.version.split()[0]}; session_id/{SESSION_ID}''' if DISABLE_TELEMETRY or HF_HUB_OFFLINE: return ua + "; telemetry/off" if is_torch_available(): ua += f'''; torch/{_torch_version}''' if is_flax_available(): ua += f'''; jax/{_jax_version}''' ua += f'''; flax/{_flax_version}''' if is_onnx_available(): ua += f'''; onnxruntime/{_onnxruntime_version}''' # CI will set this value to True if os.environ.get("DIFFUSERS_IS_CI" ,"" ).upper() in ENV_VARS_TRUE_VALUES: ua += "; is_ci/true" if isinstance(__UpperCamelCase ,__UpperCamelCase ): ua += "; " + "; ".join(f'''{k}/{v}''' for k, v in user_agent.items() ) elif isinstance(__UpperCamelCase ,__UpperCamelCase ): ua += "; " + user_agent return ua def __snake_case ( __UpperCamelCase : str ,__UpperCamelCase : Optional[str] = None ,__UpperCamelCase : Optional[str] = None ): """simple docstring""" if token is None: A_ = HfFolder.get_token() if organization is None: A_ = whoami(__UpperCamelCase )["name"] return f'''{username}/{model_id}''' else: return f'''{organization}/{model_id}''' def __snake_case ( __UpperCamelCase : Tuple ,__UpperCamelCase : Union[str, Any] ): """simple docstring""" if not is_jinja_available(): raise ValueError( "Modelcard rendering is based on Jinja templates." " Please make sure to have `jinja` installed before using `create_model_card`." " To install it, please run `pip install Jinja2`." ) if hasattr(__UpperCamelCase ,"local_rank" ) and args.local_rank not in [-1, 0]: return A_ = args.hub_token if hasattr(__UpperCamelCase ,"hub_token" ) else None A_ = get_full_repo_name(__UpperCamelCase ,token=__UpperCamelCase ) A_ = ModelCard.from_template( card_data=ModelCardData( # Card metadata object that will be converted to YAML block language="en" ,license="apache-2.0" ,library_name="diffusers" ,tags=[] ,datasets=args.dataset_name ,metrics=[] ,) ,template_path=__UpperCamelCase ,model_name=__UpperCamelCase ,repo_name=__UpperCamelCase ,dataset_name=args.dataset_name if hasattr(__UpperCamelCase ,"dataset_name" ) else None ,learning_rate=args.learning_rate ,train_batch_size=args.train_batch_size ,eval_batch_size=args.eval_batch_size ,gradient_accumulation_steps=( args.gradient_accumulation_steps if hasattr(__UpperCamelCase ,"gradient_accumulation_steps" ) else None ) ,adam_betaa=args.adam_betaa if hasattr(__UpperCamelCase ,"adam_beta1" ) else None ,adam_betaa=args.adam_betaa if hasattr(__UpperCamelCase ,"adam_beta2" ) else None ,adam_weight_decay=args.adam_weight_decay if hasattr(__UpperCamelCase ,"adam_weight_decay" ) else None ,adam_epsilon=args.adam_epsilon if hasattr(__UpperCamelCase ,"adam_epsilon" ) else None ,lr_scheduler=args.lr_scheduler if hasattr(__UpperCamelCase ,"lr_scheduler" ) else None ,lr_warmup_steps=args.lr_warmup_steps if hasattr(__UpperCamelCase ,"lr_warmup_steps" ) else None ,ema_inv_gamma=args.ema_inv_gamma if hasattr(__UpperCamelCase ,"ema_inv_gamma" ) else None ,ema_power=args.ema_power if hasattr(__UpperCamelCase ,"ema_power" ) else None ,ema_max_decay=args.ema_max_decay if hasattr(__UpperCamelCase ,"ema_max_decay" ) else None ,mixed_precision=args.mixed_precision ,) A_ = os.path.join(args.output_dir ,"README.md" ) model_card.save(__UpperCamelCase ) def __snake_case ( __UpperCamelCase : Optional[str] ,__UpperCamelCase : Optional[str] = None ): """simple docstring""" if resolved_file is None or commit_hash is not None: return commit_hash A_ = str(Path(__UpperCamelCase ).as_posix() ) A_ = re.search(R"snapshots/([^/]+)/" ,__UpperCamelCase ) if search is None: return None A_ = search.groups()[0] return commit_hash if REGEX_COMMIT_HASH.match(__UpperCamelCase ) else None # Old default cache path, potentially to be migrated. # This logic was more or less taken from `transformers`, with the following differences: # - Diffusers doesn't use custom environment variables to specify the cache path. # - There is no need to migrate the cache format, just move the files to the new location. __a :str = os.path.expanduser( os.getenv('HF_HOME', os.path.join(os.getenv('XDG_CACHE_HOME', '~/.cache'), 'huggingface')) ) __a :List[Any] = os.path.join(hf_cache_home, 'diffusers') def __snake_case ( __UpperCamelCase : Optional[str] = None ,__UpperCamelCase : Optional[str] = None ): """simple docstring""" if new_cache_dir is None: A_ = DIFFUSERS_CACHE if old_cache_dir is None: A_ = old_diffusers_cache A_ = Path(__UpperCamelCase ).expanduser() A_ = Path(__UpperCamelCase ).expanduser() for old_blob_path in old_cache_dir.glob("**/blobs/*" ): if old_blob_path.is_file() and not old_blob_path.is_symlink(): A_ = new_cache_dir / old_blob_path.relative_to(__UpperCamelCase ) new_blob_path.parent.mkdir(parents=__UpperCamelCase ,exist_ok=__UpperCamelCase ) os.replace(__UpperCamelCase ,__UpperCamelCase ) try: os.symlink(__UpperCamelCase ,__UpperCamelCase ) except OSError: logger.warning( "Could not create symlink between old cache and new cache. If you use an older version of diffusers again, files will be re-downloaded." ) # At this point, old_cache_dir contains symlinks to the new cache (it can still be used). __a :Dict = os.path.join(DIFFUSERS_CACHE, 'version_diffusers_cache.txt') if not os.path.isfile(cache_version_file): __a :Optional[int] = 0 else: with open(cache_version_file) as f: try: __a :Dict = int(f.read()) except ValueError: __a :str = 0 if cache_version < 1: __a :Optional[Any] = os.path.isdir(old_diffusers_cache) and len(os.listdir(old_diffusers_cache)) > 0 if old_cache_is_not_empty: logger.warning( 'The cache for model files in Diffusers v0.14.0 has moved to a new location. Moving your ' 'existing cached models. This is a one-time operation, you can interrupt it or run it ' 'later by calling `diffusers.utils.hub_utils.move_cache()`.' ) try: move_cache() except Exception as e: __a :Optional[Any] = '\n'.join(traceback.format_tb(e.__traceback__)) logger.error( F"There was a problem when trying to move your cache:\n\n{trace}\n{e.__class__.__name__}: {e}\n\nPlease " 'file an issue at https://github.com/huggingface/diffusers/issues/new/choose, copy paste this whole ' 'message and we will do our best to help.' ) if cache_version < 1: try: os.makedirs(DIFFUSERS_CACHE, exist_ok=True) with open(cache_version_file, 'w') as f: f.write('1') except Exception: logger.warning( F"There was a problem when trying to write in your cache folder ({DIFFUSERS_CACHE}). Please, ensure " 'the directory exists and can be written to.' ) def __snake_case ( __UpperCamelCase : str ,__UpperCamelCase : Optional[str] = None ): """simple docstring""" if variant is not None: A_ = weights_name.split("." ) A_ = splits[:-1] + [variant] + splits[-1:] A_ = ".".join(__UpperCamelCase ) return weights_name def __snake_case ( __UpperCamelCase : Optional[Any] ,*, __UpperCamelCase : Union[str, Any] ,__UpperCamelCase : Any ,__UpperCamelCase : Tuple ,__UpperCamelCase : Union[str, Any] ,__UpperCamelCase : str ,__UpperCamelCase : int ,__UpperCamelCase : Union[str, Any] ,__UpperCamelCase : int ,__UpperCamelCase : Optional[int] ,__UpperCamelCase : Tuple ,__UpperCamelCase : Optional[int]=None ,): """simple docstring""" A_ = str(__UpperCamelCase ) if os.path.isfile(__UpperCamelCase ): return pretrained_model_name_or_path elif os.path.isdir(__UpperCamelCase ): if os.path.isfile(os.path.join(__UpperCamelCase ,__UpperCamelCase ) ): # Load from a PyTorch checkpoint A_ = os.path.join(__UpperCamelCase ,__UpperCamelCase ) return model_file elif subfolder is not None and os.path.isfile( os.path.join(__UpperCamelCase ,__UpperCamelCase ,__UpperCamelCase ) ): A_ = os.path.join(__UpperCamelCase ,__UpperCamelCase ,__UpperCamelCase ) return model_file else: raise EnvironmentError( f'''Error no file named {weights_name} found in directory {pretrained_model_name_or_path}.''' ) else: # 1. First check if deprecated way of loading from branches is used if ( revision in DEPRECATED_REVISION_ARGS and (weights_name == WEIGHTS_NAME or weights_name == SAFETENSORS_WEIGHTS_NAME) and version.parse(version.parse(__UpperCamelCase ).base_version ) >= version.parse("0.20.0" ) ): try: A_ = hf_hub_download( __UpperCamelCase ,filename=_add_variant(__UpperCamelCase ,__UpperCamelCase ) ,cache_dir=__UpperCamelCase ,force_download=__UpperCamelCase ,proxies=__UpperCamelCase ,resume_download=__UpperCamelCase ,local_files_only=__UpperCamelCase ,use_auth_token=__UpperCamelCase ,user_agent=__UpperCamelCase ,subfolder=__UpperCamelCase ,revision=revision or commit_hash ,) warnings.warn( f'''Loading the variant {revision} from {pretrained_model_name_or_path} via `revision=\'{revision}\'` is deprecated. Loading instead from `revision=\'main\'` with `variant={revision}`. Loading model variants via `revision=\'{revision}\'` will be removed in diffusers v1. Please use `variant=\'{revision}\'` instead.''' ,__UpperCamelCase ,) return model_file except: # noqa: E722 warnings.warn( f'''You are loading the variant {revision} from {pretrained_model_name_or_path} via `revision=\'{revision}\'`. This behavior is deprecated and will be removed in diffusers v1. One should use `variant=\'{revision}\'` instead. However, it appears that {pretrained_model_name_or_path} currently does not have a {_add_variant(__UpperCamelCase ,__UpperCamelCase )} file in the \'main\' branch of {pretrained_model_name_or_path}. \n The Diffusers team and community would be very grateful if you could open an issue: https://github.com/huggingface/diffusers/issues/new with the title \'{pretrained_model_name_or_path} is missing {_add_variant(__UpperCamelCase ,__UpperCamelCase )}\' so that the correct variant file can be added.''' ,__UpperCamelCase ,) try: # 2. Load model file as usual A_ = hf_hub_download( __UpperCamelCase ,filename=__UpperCamelCase ,cache_dir=__UpperCamelCase ,force_download=__UpperCamelCase ,proxies=__UpperCamelCase ,resume_download=__UpperCamelCase ,local_files_only=__UpperCamelCase ,use_auth_token=__UpperCamelCase ,user_agent=__UpperCamelCase ,subfolder=__UpperCamelCase ,revision=revision or commit_hash ,) return model_file except RepositoryNotFoundError: raise EnvironmentError( f'''{pretrained_model_name_or_path} is not a local folder and is not a valid model identifier ''' "listed on 'https://huggingface.co/models'\nIf this is a private repository, make sure to pass a " "token having permission to this repo with `use_auth_token` or log in with `huggingface-cli " "login`." ) except RevisionNotFoundError: raise EnvironmentError( f'''{revision} is not a valid git identifier (branch name, tag name or commit id) that exists for ''' "this model name. Check the model page at " f'''\'https://huggingface.co/{pretrained_model_name_or_path}\' for available revisions.''' ) except EntryNotFoundError: raise EnvironmentError( f'''{pretrained_model_name_or_path} does not appear to have a file named {weights_name}.''' ) except HTTPError as err: raise EnvironmentError( f'''There was a specific connection error when trying to load {pretrained_model_name_or_path}:\n{err}''' ) except ValueError: raise EnvironmentError( f'''We couldn\'t connect to \'{HUGGINGFACE_CO_RESOLVE_ENDPOINT}\' to load this model, couldn\'t find it''' f''' in the cached files and it looks like {pretrained_model_name_or_path} is not the path to a''' f''' directory containing a file named {weights_name} or''' " \nCheckout your internet connection or see how to run the library in" " offline mode at 'https://huggingface.co/docs/diffusers/installation#offline-mode'." ) except EnvironmentError: raise EnvironmentError( f'''Can\'t load the model for \'{pretrained_model_name_or_path}\'. If you were trying to load it from ''' "'https://huggingface.co/models', make sure you don't have a local directory with the same name. " f'''Otherwise, make sure \'{pretrained_model_name_or_path}\' is the correct path to a directory ''' f'''containing a file named {weights_name}''' )
312
1
from __future__ import annotations from collections import deque from collections.abc import Iterator from dataclasses import dataclass @dataclass class _a : """simple docstring""" _lowerCamelCase : int _lowerCamelCase : int class _a : """simple docstring""" def __init__( self : Any , UpperCAmelCase : int ): A_ = [[] for _ in range(UpperCAmelCase )] A_ = size def __getitem__( self : List[Any] , UpperCAmelCase : int ): return iter(self._graph[vertex] ) @property def __A ( self : Optional[Any] ): return self._size def __A ( self : Optional[Any] , UpperCAmelCase : int , UpperCAmelCase : int , UpperCAmelCase : int ): if weight not in (0, 1): raise ValueError("Edge weight must be either 0 or 1." ) if to_vertex < 0 or to_vertex >= self.size: raise ValueError("Vertex indexes must be in [0; size)." ) self._graph[from_vertex].append(Edge(UpperCAmelCase , UpperCAmelCase ) ) def __A ( self : int , UpperCAmelCase : int , UpperCAmelCase : int ): A_ = deque([start_vertex] ) A_ = [None] * self.size A_ = 0 while queue: A_ = queue.popleft() A_ = distances[current_vertex] if current_distance is None: continue for edge in self[current_vertex]: A_ = current_distance + edge.weight A_ = distances[edge.destination_vertex] if ( isinstance(UpperCAmelCase , UpperCAmelCase ) and new_distance >= dest_vertex_distance ): continue A_ = new_distance if edge.weight == 0: queue.appendleft(edge.destination_vertex ) else: queue.append(edge.destination_vertex ) if distances[finish_vertex] is None: raise ValueError("No path from start_vertex to finish_vertex." ) return distances[finish_vertex] if __name__ == "__main__": import doctest doctest.testmod()
312
# Copyright 2023 The HuggingFace Team. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available __a :Any = { 'configuration_mgp_str': ['MGP_STR_PRETRAINED_CONFIG_ARCHIVE_MAP', 'MgpstrConfig'], 'processing_mgp_str': ['MgpstrProcessor'], 'tokenization_mgp_str': ['MgpstrTokenizer'], } try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __a :Optional[Any] = [ 'MGP_STR_PRETRAINED_MODEL_ARCHIVE_LIST', 'MgpstrModel', 'MgpstrPreTrainedModel', 'MgpstrForSceneTextRecognition', ] if TYPE_CHECKING: from .configuration_mgp_str import MGP_STR_PRETRAINED_CONFIG_ARCHIVE_MAP, MgpstrConfig from .processing_mgp_str import MgpstrProcessor from .tokenization_mgp_str import MgpstrTokenizer try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_mgp_str import ( MGP_STR_PRETRAINED_MODEL_ARCHIVE_LIST, MgpstrForSceneTextRecognition, MgpstrModel, MgpstrPreTrainedModel, ) else: import sys __a :List[Any] = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
312
1
from math import ceil def __snake_case ( __UpperCamelCase : Dict ,__UpperCamelCase : int ): """simple docstring""" A_ = list(range(0 ,__UpperCamelCase ) ) A_ = [item for sublist in list(device_map.values() ) for item in sublist] # Duplicate check A_ = [] for i in device_map_blocks: if device_map_blocks.count(__UpperCamelCase ) > 1 and i not in duplicate_blocks: duplicate_blocks.append(__UpperCamelCase ) # Missing blocks A_ = [i for i in blocks if i not in device_map_blocks] A_ = [i for i in device_map_blocks if i not in blocks] if len(__UpperCamelCase ) != 0: raise ValueError( "Duplicate attention blocks specified in device_map. Attention blocks must be specified to one device." " These attention blocks were specified more than once: " + str(__UpperCamelCase ) ) if len(__UpperCamelCase ) != 0: raise ValueError( "There are attention blocks for this model that are not specified in the device_map. Add these attention " "blocks to a device on the device_map: " + str(__UpperCamelCase ) ) if len(__UpperCamelCase ) != 0: raise ValueError( "The device_map contains more attention blocks than this model has. Remove these from the device_map:" + str(__UpperCamelCase ) ) def __snake_case ( __UpperCamelCase : Any ,__UpperCamelCase : Union[str, Any] ): """simple docstring""" A_ = list(range(__UpperCamelCase ) ) A_ = int(ceil(n_layers / len(__UpperCamelCase ) ) ) A_ = [layers[i : i + n_blocks] for i in range(0 ,__UpperCamelCase ,__UpperCamelCase )] return dict(zip(__UpperCamelCase ,__UpperCamelCase ) )
312
import functools from typing import Any def __snake_case ( __UpperCamelCase : str ,__UpperCamelCase : list[str] ): """simple docstring""" if not isinstance(__UpperCamelCase ,__UpperCamelCase ) or len(__UpperCamelCase ) == 0: raise ValueError("the string should be not empty string" ) if not isinstance(__UpperCamelCase ,__UpperCamelCase ) or not all( isinstance(__UpperCamelCase ,__UpperCamelCase ) and len(__UpperCamelCase ) > 0 for item in words ): raise ValueError("the words should be a list of non-empty strings" ) # Build trie A_ = {} A_ = "WORD_KEEPER" for word in words: A_ = trie for c in word: if c not in trie_node: A_ = {} A_ = trie_node[c] A_ = True A_ = len(__UpperCamelCase ) # Dynamic programming method @functools.cache def is_breakable(__UpperCamelCase : int ) -> bool: if index == len_string: return True A_ = trie for i in range(__UpperCamelCase ,__UpperCamelCase ): A_ = trie_node.get(string[i] ,__UpperCamelCase ) if trie_node is None: return False if trie_node.get(__UpperCamelCase ,__UpperCamelCase ) and is_breakable(i + 1 ): return True return False return is_breakable(0 ) if __name__ == "__main__": import doctest doctest.testmod()
312
1
import importlib import sys from argparse import REMAINDER, ArgumentParser from pathlib import Path import torch_xla.distributed.xla_multiprocessing as xmp def __snake_case ( ): """simple docstring""" A_ = ArgumentParser( description=( "PyTorch TPU distributed training launch helper utility that will spawn up multiple distributed processes" ) ) # Optional arguments for the launch helper parser.add_argument("--num_cores" ,type=__UpperCamelCase ,default=1 ,help="Number of TPU cores to use (1 or 8)." ) # positional parser.add_argument( "training_script" ,type=__UpperCamelCase ,help=( "The full path to the single TPU training " "program/script to be launched in parallel, " "followed by all the arguments for the " "training script" ) ,) # rest from the training program parser.add_argument("training_script_args" ,nargs=__UpperCamelCase ) return parser.parse_args() def __snake_case ( ): """simple docstring""" A_ = parse_args() # Import training_script as a module. A_ = Path(args.training_script ) sys.path.append(str(script_fpath.parent.resolve() ) ) A_ = script_fpath.stem A_ = importlib.import_module(__UpperCamelCase ) # Patch sys.argv A_ = [args.training_script] + args.training_script_args + ["--tpu_num_cores", str(args.num_cores )] xmp.spawn(mod._mp_fn ,args=() ,nprocs=args.num_cores ) if __name__ == "__main__": main()
312
import json from typing import List, Optional, Tuple from tokenizers import normalizers from ...tokenization_utils_fast import PreTrainedTokenizerFast from .tokenization_electra import ElectraTokenizer __a :List[str] = {'vocab_file': 'vocab.txt', 'tokenizer_file': 'tokenizer.json'} __a :Union[str, Any] = { 'vocab_file': { 'google/electra-small-generator': ( 'https://huggingface.co/google/electra-small-generator/resolve/main/vocab.txt' ), 'google/electra-base-generator': 'https://huggingface.co/google/electra-base-generator/resolve/main/vocab.txt', 'google/electra-large-generator': ( 'https://huggingface.co/google/electra-large-generator/resolve/main/vocab.txt' ), 'google/electra-small-discriminator': ( 'https://huggingface.co/google/electra-small-discriminator/resolve/main/vocab.txt' ), 'google/electra-base-discriminator': ( 'https://huggingface.co/google/electra-base-discriminator/resolve/main/vocab.txt' ), 'google/electra-large-discriminator': ( 'https://huggingface.co/google/electra-large-discriminator/resolve/main/vocab.txt' ), }, 'tokenizer_file': { 'google/electra-small-generator': ( 'https://huggingface.co/google/electra-small-generator/resolve/main/tokenizer.json' ), 'google/electra-base-generator': ( 'https://huggingface.co/google/electra-base-generator/resolve/main/tokenizer.json' ), 'google/electra-large-generator': ( 'https://huggingface.co/google/electra-large-generator/resolve/main/tokenizer.json' ), 'google/electra-small-discriminator': ( 'https://huggingface.co/google/electra-small-discriminator/resolve/main/tokenizer.json' ), 'google/electra-base-discriminator': ( 'https://huggingface.co/google/electra-base-discriminator/resolve/main/tokenizer.json' ), 'google/electra-large-discriminator': ( 'https://huggingface.co/google/electra-large-discriminator/resolve/main/tokenizer.json' ), }, } __a :Optional[int] = { 'google/electra-small-generator': 512, 'google/electra-base-generator': 512, 'google/electra-large-generator': 512, 'google/electra-small-discriminator': 512, 'google/electra-base-discriminator': 512, 'google/electra-large-discriminator': 512, } __a :str = { 'google/electra-small-generator': {'do_lower_case': True}, 'google/electra-base-generator': {'do_lower_case': True}, 'google/electra-large-generator': {'do_lower_case': True}, 'google/electra-small-discriminator': {'do_lower_case': True}, 'google/electra-base-discriminator': {'do_lower_case': True}, 'google/electra-large-discriminator': {'do_lower_case': True}, } class _a ( snake_case_ ): """simple docstring""" _lowerCamelCase : Tuple = VOCAB_FILES_NAMES _lowerCamelCase : Union[str, Any] = PRETRAINED_VOCAB_FILES_MAP _lowerCamelCase : int = PRETRAINED_INIT_CONFIGURATION _lowerCamelCase : List[Any] = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES _lowerCamelCase : int = ElectraTokenizer def __init__( self : Tuple , UpperCAmelCase : Dict=None , UpperCAmelCase : Optional[int]=None , UpperCAmelCase : Any=True , UpperCAmelCase : Any="[UNK]" , UpperCAmelCase : Union[str, Any]="[SEP]" , UpperCAmelCase : List[Any]="[PAD]" , UpperCAmelCase : Union[str, Any]="[CLS]" , UpperCAmelCase : List[Any]="[MASK]" , UpperCAmelCase : List[str]=True , UpperCAmelCase : Any=None , **UpperCAmelCase : Union[str, Any] , ): super().__init__( UpperCAmelCase , tokenizer_file=UpperCAmelCase , do_lower_case=UpperCAmelCase , unk_token=UpperCAmelCase , sep_token=UpperCAmelCase , pad_token=UpperCAmelCase , cls_token=UpperCAmelCase , mask_token=UpperCAmelCase , tokenize_chinese_chars=UpperCAmelCase , strip_accents=UpperCAmelCase , **UpperCAmelCase , ) A_ = json.loads(self.backend_tokenizer.normalizer.__getstate__() ) if ( normalizer_state.get("lowercase" , UpperCAmelCase ) != do_lower_case or normalizer_state.get("strip_accents" , UpperCAmelCase ) != strip_accents or normalizer_state.get("handle_chinese_chars" , UpperCAmelCase ) != tokenize_chinese_chars ): A_ = getattr(UpperCAmelCase , normalizer_state.pop("type" ) ) A_ = do_lower_case A_ = strip_accents A_ = tokenize_chinese_chars A_ = normalizer_class(**UpperCAmelCase ) A_ = do_lower_case def __A ( self : int , UpperCAmelCase : List[Any] , UpperCAmelCase : Union[str, Any]=None ): A_ = [self.cls_token_id] + token_ids_a + [self.sep_token_id] if token_ids_a: output += token_ids_a + [self.sep_token_id] return output def __A ( self : Union[str, Any] , UpperCAmelCase : List[int] , UpperCAmelCase : Optional[List[int]] = None ): A_ = [self.sep_token_id] A_ = [self.cls_token_id] if token_ids_a is None: return len(cls + token_ids_a + sep ) * [0] return len(cls + token_ids_a + sep ) * [0] + len(token_ids_a + sep ) * [1] def __A ( self : Tuple , UpperCAmelCase : str , UpperCAmelCase : Optional[str] = None ): A_ = self._tokenizer.model.save(UpperCAmelCase , name=UpperCAmelCase ) return tuple(UpperCAmelCase )
312
1
from typing import Dict, Iterable, Optional, Union import numpy as np from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size_dict from ...image_transforms import normalize, rescale, resize, to_channel_dimension_format, to_pil_image from ...image_utils import ( IMAGENET_STANDARD_MEAN, IMAGENET_STANDARD_STD, ChannelDimension, ImageInput, PILImageResampling, make_list_of_images, to_numpy_array, valid_images, ) from ...utils import TensorType, is_pytesseract_available, is_vision_available, logging, requires_backends if is_vision_available(): import PIL # soft dependency if is_pytesseract_available(): import pytesseract __a :List[Any] = logging.get_logger(__name__) def __snake_case ( __UpperCamelCase : Union[str, Any] ,__UpperCamelCase : Any ,__UpperCamelCase : str ): """simple docstring""" return [ int(1000 * (box[0] / width) ), int(1000 * (box[1] / height) ), int(1000 * (box[2] / width) ), int(1000 * (box[3] / height) ), ] def __snake_case ( __UpperCamelCase : np.ndarray ,__UpperCamelCase : Optional[str] ,__UpperCamelCase : Optional[str] ): """simple docstring""" A_ = to_pil_image(__UpperCamelCase ) A_ , A_ = pil_image.size A_ = pytesseract.image_to_data(__UpperCamelCase ,lang=__UpperCamelCase ,output_type="dict" ,config=__UpperCamelCase ) A_ , A_ , A_ , A_ , A_ = data["text"], data["left"], data["top"], data["width"], data["height"] # filter empty words and corresponding coordinates A_ = [idx for idx, word in enumerate(__UpperCamelCase ) if not word.strip()] A_ = [word for idx, word in enumerate(__UpperCamelCase ) if idx not in irrelevant_indices] A_ = [coord for idx, coord in enumerate(__UpperCamelCase ) if idx not in irrelevant_indices] A_ = [coord for idx, coord in enumerate(__UpperCamelCase ) if idx not in irrelevant_indices] A_ = [coord for idx, coord in enumerate(__UpperCamelCase ) if idx not in irrelevant_indices] A_ = [coord for idx, coord in enumerate(__UpperCamelCase ) if idx not in irrelevant_indices] # turn coordinates into (left, top, left+width, top+height) format A_ = [] for x, y, w, h in zip(__UpperCamelCase ,__UpperCamelCase ,__UpperCamelCase ,__UpperCamelCase ): A_ = [x, y, x + w, y + h] actual_boxes.append(__UpperCamelCase ) # finally, normalize the bounding boxes A_ = [] for box in actual_boxes: normalized_boxes.append(normalize_box(__UpperCamelCase ,__UpperCamelCase ,__UpperCamelCase ) ) assert len(__UpperCamelCase ) == len(__UpperCamelCase ), "Not as many words as there are bounding boxes" return words, normalized_boxes class _a ( snake_case_ ): """simple docstring""" _lowerCamelCase : List[str] = ['pixel_values'] def __init__( self : Optional[Any] , UpperCAmelCase : bool = True , UpperCAmelCase : Dict[str, int] = None , UpperCAmelCase : PILImageResampling = PILImageResampling.BILINEAR , UpperCAmelCase : bool = True , UpperCAmelCase : float = 1 / 255 , UpperCAmelCase : bool = True , UpperCAmelCase : Union[float, Iterable[float]] = None , UpperCAmelCase : Union[float, Iterable[float]] = None , UpperCAmelCase : bool = True , UpperCAmelCase : Optional[str] = None , UpperCAmelCase : Optional[str] = "" , **UpperCAmelCase : Union[str, Any] , ): super().__init__(**UpperCAmelCase ) A_ = size if size is not None else {"height": 224, "width": 224} A_ = get_size_dict(UpperCAmelCase ) A_ = do_resize A_ = size A_ = resample A_ = do_rescale A_ = rescale_value A_ = do_normalize A_ = image_mean if image_mean is not None else IMAGENET_STANDARD_MEAN A_ = image_std if image_std is not None else IMAGENET_STANDARD_STD A_ = apply_ocr A_ = ocr_lang A_ = tesseract_config def __A ( self : str , UpperCAmelCase : np.ndarray , UpperCAmelCase : Dict[str, int] , UpperCAmelCase : PILImageResampling = PILImageResampling.BILINEAR , UpperCAmelCase : Optional[Union[str, ChannelDimension]] = None , **UpperCAmelCase : int , ): A_ = get_size_dict(UpperCAmelCase ) 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()}''' ) A_ = (size["height"], size["width"]) return resize(UpperCAmelCase , size=UpperCAmelCase , resample=UpperCAmelCase , data_format=UpperCAmelCase , **UpperCAmelCase ) def __A ( self : Optional[int] , UpperCAmelCase : np.ndarray , UpperCAmelCase : Union[int, float] , UpperCAmelCase : Optional[Union[str, ChannelDimension]] = None , **UpperCAmelCase : List[Any] , ): return rescale(UpperCAmelCase , scale=UpperCAmelCase , data_format=UpperCAmelCase , **UpperCAmelCase ) def __A ( self : Dict , UpperCAmelCase : np.ndarray , UpperCAmelCase : Union[float, Iterable[float]] , UpperCAmelCase : Union[float, Iterable[float]] , UpperCAmelCase : Optional[Union[str, ChannelDimension]] = None , **UpperCAmelCase : List[Any] , ): return normalize(UpperCAmelCase , mean=UpperCAmelCase , std=UpperCAmelCase , data_format=UpperCAmelCase , **UpperCAmelCase ) def __A ( self : str , UpperCAmelCase : ImageInput , UpperCAmelCase : bool = None , UpperCAmelCase : Dict[str, int] = None , UpperCAmelCase : Tuple=None , UpperCAmelCase : bool = None , UpperCAmelCase : float = None , UpperCAmelCase : bool = None , UpperCAmelCase : Union[float, Iterable[float]] = None , UpperCAmelCase : Union[float, Iterable[float]] = None , UpperCAmelCase : bool = None , UpperCAmelCase : Optional[str] = None , UpperCAmelCase : Optional[str] = None , UpperCAmelCase : Optional[Union[str, TensorType]] = None , UpperCAmelCase : ChannelDimension = ChannelDimension.FIRST , **UpperCAmelCase : List[str] , ): A_ = do_resize if do_resize is not None else self.do_resize A_ = size if size is not None else self.size A_ = get_size_dict(UpperCAmelCase ) A_ = resample if resample is not None else self.resample A_ = do_rescale if do_rescale is not None else self.do_rescale A_ = rescale_factor if rescale_factor is not None else self.rescale_factor A_ = do_normalize if do_normalize is not None else self.do_normalize A_ = image_mean if image_mean is not None else self.image_mean A_ = image_std if image_std is not None else self.image_std A_ = apply_ocr if apply_ocr is not None else self.apply_ocr A_ = ocr_lang if ocr_lang is not None else self.ocr_lang A_ = tesseract_config if tesseract_config is not None else self.tesseract_config A_ = make_list_of_images(UpperCAmelCase ) if not valid_images(UpperCAmelCase ): raise ValueError( "Invalid image type. Must be of type PIL.Image.Image, numpy.ndarray, " "torch.Tensor, tf.Tensor or jax.ndarray." ) if do_resize and size is None: raise ValueError("Size must be specified if do_resize is True." ) if do_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("If do_normalize is True, image_mean and image_std must be specified." ) # All transformations expect numpy arrays. A_ = [to_numpy_array(UpperCAmelCase ) for image in images] # Tesseract OCR to get words + normalized bounding boxes if apply_ocr: requires_backends(self , "pytesseract" ) A_ = [] A_ = [] for image in images: A_ , A_ = apply_tesseract(UpperCAmelCase , UpperCAmelCase , UpperCAmelCase ) words_batch.append(UpperCAmelCase ) boxes_batch.append(UpperCAmelCase ) if do_resize: A_ = [self.resize(image=UpperCAmelCase , size=UpperCAmelCase , resample=UpperCAmelCase ) for image in images] if do_rescale: A_ = [self.rescale(image=UpperCAmelCase , scale=UpperCAmelCase ) for image in images] if do_normalize: A_ = [self.normalize(image=UpperCAmelCase , mean=UpperCAmelCase , std=UpperCAmelCase ) for image in images] A_ = [to_channel_dimension_format(UpperCAmelCase , UpperCAmelCase ) for image in images] A_ = BatchFeature(data={"pixel_values": images} , tensor_type=UpperCAmelCase ) if apply_ocr: A_ = words_batch A_ = boxes_batch return data
312
# flake8: noqa # Lint as: python3 from typing import Dict, List, Optional, Type from .. import config from ..utils import logging from .formatting import ( ArrowFormatter, CustomFormatter, Formatter, PandasFormatter, PythonFormatter, TensorFormatter, format_table, query_table, ) from .np_formatter import NumpyFormatter __a :Optional[Any] = logging.get_logger(__name__) __a :Dict[Optional[str], Type[Formatter]] = {} __a :Dict[Optional[str], str] = {} __a :Dict[Optional[str], Exception] = {} def __snake_case ( __UpperCamelCase : type ,__UpperCamelCase : Optional[str] ,__UpperCamelCase : Optional[List[str]] = None ,): """simple docstring""" A_ = aliases if aliases is not None else [] if format_type in _FORMAT_TYPES: logger.warning( f'''Overwriting format type \'{format_type}\' ({_FORMAT_TYPES[format_type].__name__} -> {formatter_cls.__name__})''' ) A_ = formatter_cls for alias in set(aliases + [format_type] ): if alias in _FORMAT_TYPES_ALIASES: logger.warning( f'''Overwriting format type alias \'{alias}\' ({_FORMAT_TYPES_ALIASES[alias]} -> {format_type})''' ) A_ = format_type def __snake_case ( __UpperCamelCase : Exception ,__UpperCamelCase : Optional[str] ,__UpperCamelCase : Optional[List[str]] = None ): """simple docstring""" A_ = aliases if aliases is not None else [] for alias in set(aliases + [format_type] ): A_ = unavailable_error # Here we define all the available formatting functions that can be used by `Dataset.set_format` _register_formatter(PythonFormatter, None, aliases=['python']) _register_formatter(ArrowFormatter, 'arrow', aliases=['pa', 'pyarrow']) _register_formatter(NumpyFormatter, 'numpy', aliases=['np']) _register_formatter(PandasFormatter, 'pandas', aliases=['pd']) _register_formatter(CustomFormatter, 'custom') if config.TORCH_AVAILABLE: from .torch_formatter import TorchFormatter _register_formatter(TorchFormatter, 'torch', aliases=['pt', 'pytorch']) else: __a :List[Any] = ValueError('PyTorch needs to be installed to be able to return PyTorch tensors.') _register_unavailable_formatter(_torch_error, 'torch', aliases=['pt', 'pytorch']) if config.TF_AVAILABLE: from .tf_formatter import TFFormatter _register_formatter(TFFormatter, 'tensorflow', aliases=['tf']) else: __a :List[str] = ValueError('Tensorflow needs to be installed to be able to return Tensorflow tensors.') _register_unavailable_formatter(_tf_error, 'tensorflow', aliases=['tf']) if config.JAX_AVAILABLE: from .jax_formatter import JaxFormatter _register_formatter(JaxFormatter, 'jax', aliases=[]) else: __a :Tuple = ValueError('JAX needs to be installed to be able to return JAX arrays.') _register_unavailable_formatter(_jax_error, 'jax', aliases=[]) def __snake_case ( __UpperCamelCase : Optional[str] ): """simple docstring""" if format_type in _FORMAT_TYPES_ALIASES: return _FORMAT_TYPES_ALIASES[format_type] else: return format_type def __snake_case ( __UpperCamelCase : Optional[str] ,**__UpperCamelCase : List[Any] ): """simple docstring""" A_ = get_format_type_from_alias(__UpperCamelCase ) if format_type in _FORMAT_TYPES: return _FORMAT_TYPES[format_type](**__UpperCamelCase ) if format_type in _FORMAT_TYPES_ALIASES_UNAVAILABLE: raise _FORMAT_TYPES_ALIASES_UNAVAILABLE[format_type] else: raise ValueError( f'''Return type should be None or selected in {list(type for type in _FORMAT_TYPES.keys() if type != None )}, but got \'{format_type}\'''' )
312
1
from math import isqrt def __snake_case ( __UpperCamelCase : int ): """simple docstring""" A_ = [True] * max_number for i in range(2 ,isqrt(max_number - 1 ) + 1 ): if is_prime[i]: for j in range(i**2 ,__UpperCamelCase ,__UpperCamelCase ): A_ = False return [i for i in range(2 ,__UpperCamelCase ) if is_prime[i]] def __snake_case ( __UpperCamelCase : int = 10**8 ): """simple docstring""" A_ = calculate_prime_numbers(max_number // 2 ) A_ = 0 A_ = 0 A_ = len(__UpperCamelCase ) - 1 while left <= right: while prime_numbers[left] * prime_numbers[right] >= max_number: right -= 1 semiprimes_count += right - left + 1 left += 1 return semiprimes_count if __name__ == "__main__": print(F"{solution() = }")
312
from typing import TYPE_CHECKING from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available, is_vision_available __a :int = { 'configuration_mask2former': [ 'MASK2FORMER_PRETRAINED_CONFIG_ARCHIVE_MAP', 'Mask2FormerConfig', ], } try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __a :Union[str, Any] = ['Mask2FormerImageProcessor'] try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: __a :Optional[Any] = [ 'MASK2FORMER_PRETRAINED_MODEL_ARCHIVE_LIST', 'Mask2FormerForUniversalSegmentation', 'Mask2FormerModel', 'Mask2FormerPreTrainedModel', ] if TYPE_CHECKING: from .configuration_maskaformer import MASK2FORMER_PRETRAINED_CONFIG_ARCHIVE_MAP, MaskaFormerConfig try: if not is_vision_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .image_processing_maskaformer import MaskaFormerImageProcessor try: if not is_torch_available(): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: pass else: from .modeling_maskaformer import ( MASK2FORMER_PRETRAINED_MODEL_ARCHIVE_LIST, MaskaFormerForUniversalSegmentation, MaskaFormerModel, MaskaFormerPreTrainedModel, ) else: import sys __a :Tuple = _LazyModule(__name__, globals()['__file__'], _import_structure)
312
1
import uuid from typing import Any, Dict, List, Optional, Union from ..utils import add_end_docstrings, is_tf_available, is_torch_available, logging from .base import PIPELINE_INIT_ARGS, Pipeline if is_tf_available(): import tensorflow as tf if is_torch_available(): import torch __a :List[Any] = logging.get_logger(__name__) class _a : """simple docstring""" def __init__( self : int , UpperCAmelCase : str = None , UpperCAmelCase : uuid.UUID = None , UpperCAmelCase : Union[str, Any]=None , UpperCAmelCase : Optional[int]=None ): if not conversation_id: A_ = uuid.uuida() if past_user_inputs is None: A_ = [] if generated_responses is None: A_ = [] A_ = conversation_id A_ = past_user_inputs A_ = generated_responses A_ = text def __eq__( self : List[Any] , UpperCAmelCase : Tuple ): if not isinstance(UpperCAmelCase , UpperCAmelCase ): return False if self.uuid == other.uuid: return True return ( self.new_user_input == other.new_user_input and self.past_user_inputs == other.past_user_inputs and self.generated_responses == other.generated_responses ) def __A ( self : Union[str, Any] , UpperCAmelCase : str , UpperCAmelCase : bool = False ): if self.new_user_input: if overwrite: logger.warning( f'''User input added while unprocessed input was existing: "{self.new_user_input}" was overwritten ''' f'''with: "{text}".''' ) A_ = text else: logger.warning( f'''User input added while unprocessed input was existing: "{self.new_user_input}" new input ''' f'''ignored: "{text}". Set `overwrite` to True to overwrite unprocessed user input''' ) else: A_ = text def __A ( self : Dict ): if self.new_user_input: self.past_user_inputs.append(self.new_user_input ) A_ = None def __A ( self : Any , UpperCAmelCase : str ): self.generated_responses.append(UpperCAmelCase ) def __A ( self : List[Any] ): for user_input, generated_response in zip(self.past_user_inputs , self.generated_responses ): yield True, user_input yield False, generated_response if self.new_user_input: yield True, self.new_user_input def __repr__( self : int ): A_ = f'''Conversation id: {self.uuid} \n''' for is_user, text in self.iter_texts(): A_ = "user" if is_user else "bot" output += f'''{name} >> {text} \n''' return output @add_end_docstrings( snake_case_ , R'\n min_length_for_response (`int`, *optional*, defaults to 32):\n The minimum length (in number of tokens) for a response.\n minimum_tokens (`int`, *optional*, defaults to 10):\n The minimum length of tokens to leave for a response.\n ' , ) class _a ( snake_case_ ): """simple docstring""" def __init__( self : Dict , *UpperCAmelCase : Optional[int] , **UpperCAmelCase : Tuple ): super().__init__(*UpperCAmelCase , **UpperCAmelCase ) if self.tokenizer.pad_token_id is None: A_ = self.tokenizer.eos_token def __A ( self : Tuple , UpperCAmelCase : Union[str, Any]=None , UpperCAmelCase : Optional[int]=None , UpperCAmelCase : List[str]=None , **UpperCAmelCase : Dict ): A_ = {} A_ = {} A_ = {} if min_length_for_response is not None: A_ = min_length_for_response if minimum_tokens is not None: A_ = minimum_tokens if "max_length" in generate_kwargs: A_ = generate_kwargs["max_length"] # self.max_length = generate_kwargs.get("max_length", self.model.config.max_length) if clean_up_tokenization_spaces is not None: A_ = clean_up_tokenization_spaces if generate_kwargs: forward_params.update(UpperCAmelCase ) return preprocess_params, forward_params, postprocess_params def __call__( self : Optional[int] , UpperCAmelCase : Union[Conversation, List[Conversation]] , UpperCAmelCase : Any=0 , **UpperCAmelCase : int ): A_ = super().__call__(UpperCAmelCase , num_workers=UpperCAmelCase , **UpperCAmelCase ) if isinstance(UpperCAmelCase , UpperCAmelCase ) and len(UpperCAmelCase ) == 1: return outputs[0] return outputs def __A ( self : Dict , UpperCAmelCase : Conversation , UpperCAmelCase : Dict=32 ): if not isinstance(UpperCAmelCase , UpperCAmelCase ): raise ValueError("ConversationalPipeline, expects Conversation as inputs" ) if conversation.new_user_input is None: raise ValueError( f'''Conversation with UUID {type(conversation.uuid )} does not contain new user input to process. ''' "Add user inputs with the conversation's `add_user_input` method" ) if hasattr(self.tokenizer , "_build_conversation_input_ids" ): A_ = self.tokenizer._build_conversation_input_ids(UpperCAmelCase ) else: # If the tokenizer cannot handle conversations, we default to only the old version A_ = self._legacy_parse_and_tokenize(UpperCAmelCase ) if self.framework == "pt": A_ = torch.LongTensor([input_ids] ) elif self.framework == "tf": A_ = tf.constant([input_ids] ) return {"input_ids": input_ids, "conversation": conversation} def __A ( self : Any , UpperCAmelCase : int , UpperCAmelCase : Tuple=10 , **UpperCAmelCase : str ): A_ = generate_kwargs.get("max_length" , self.model.config.max_length ) A_ = model_inputs["input_ids"].shape[1] if max_length - minimum_tokens < n: logger.warning(f'''Conversation input is to long ({n}), trimming it to ({max_length} - {minimum_tokens})''' ) A_ = max_length - minimum_tokens A_ = model_inputs["input_ids"][:, -trim:] if "attention_mask" in model_inputs: A_ = model_inputs["attention_mask"][:, -trim:] A_ = model_inputs.pop("conversation" ) A_ = max_length A_ = self.model.generate(**UpperCAmelCase , **UpperCAmelCase ) if self.model.config.is_encoder_decoder: A_ = 1 else: A_ = n return {"output_ids": output_ids[:, start_position:], "conversation": conversation} def __A ( self : Tuple , UpperCAmelCase : List[str] , UpperCAmelCase : Optional[int]=True ): A_ = model_outputs["output_ids"] A_ = self.tokenizer.decode( output_ids[0] , skip_special_tokens=UpperCAmelCase , clean_up_tokenization_spaces=UpperCAmelCase , ) A_ = model_outputs["conversation"] conversation.mark_processed() conversation.append_response(UpperCAmelCase ) return conversation def __A ( self : str , UpperCAmelCase : Conversation ): A_ = self.tokenizer.eos_token_id A_ = [] for is_user, text in conversation.iter_texts(): if eos_token_id is not None: input_ids.extend(self.tokenizer.encode(UpperCAmelCase , add_special_tokens=UpperCAmelCase ) + [eos_token_id] ) else: input_ids.extend(self.tokenizer.encode(UpperCAmelCase , add_special_tokens=UpperCAmelCase ) ) if len(UpperCAmelCase ) > self.tokenizer.model_max_length: A_ = input_ids[-self.tokenizer.model_max_length :] return input_ids
312
import copy from dataclasses import dataclass, field from typing import ClassVar, Dict from ..features import Audio, ClassLabel, Features from .base import TaskTemplate @dataclass(frozen=snake_case_ ) class _a ( snake_case_ ): """simple docstring""" _lowerCamelCase : str = field(default='audio-classification' , metadata={'include_in_asdict_even_if_is_default': True} ) _lowerCamelCase : ClassVar[Features] = Features({'audio': Audio()} ) _lowerCamelCase : ClassVar[Features] = Features({'labels': ClassLabel} ) _lowerCamelCase : str = "audio" _lowerCamelCase : str = "labels" def __A ( self : str , UpperCAmelCase : List[Any] ): if self.label_column not in features: raise ValueError(f'''Column {self.label_column} is not present in features.''' ) if not isinstance(features[self.label_column] , UpperCAmelCase ): raise ValueError(f'''Column {self.label_column} is not a ClassLabel.''' ) A_ = copy.deepcopy(self ) A_ = self.label_schema.copy() A_ = features[self.label_column] A_ = label_schema return task_template @property def __A ( self : List[str] ): return { self.audio_column: "audio", self.label_column: "labels", }
312
1